2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2013-2014 Qlogic Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * and ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
32 * Author : David C Somayajulu, Qlogic Corporation, Aliso Viejo, CA 92656.
35 #include <sys/cdefs.h>
39 #include "qls_inline.h"
46 * Some PCI Configuration Space Related Defines
49 #ifndef PCI_VENDOR_QLOGIC
50 #define PCI_VENDOR_QLOGIC 0x1077
53 #ifndef PCI_DEVICE_QLOGIC_8000
54 #define PCI_DEVICE_QLOGIC_8000 0x8000
57 #define PCI_QLOGIC_DEV8000 \
58 ((PCI_DEVICE_QLOGIC_8000 << 16) | PCI_VENDOR_QLOGIC)
63 static int qls_alloc_parent_dma_tag(qla_host_t *ha);
64 static void qls_free_parent_dma_tag(qla_host_t *ha);
66 static void qls_flush_xmt_bufs(qla_host_t *ha);
68 static int qls_alloc_rcv_bufs(qla_host_t *ha);
69 static void qls_free_rcv_bufs(qla_host_t *ha);
71 static void qls_init_ifnet(device_t dev, qla_host_t *ha);
72 static void qls_release(qla_host_t *ha);
73 static void qls_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs,
75 static void qls_stop(qla_host_t *ha);
76 static int qls_send(qla_host_t *ha, struct mbuf **m_headp);
77 static void qls_tx_done(void *context, int pending);
79 static int qls_config_lro(qla_host_t *ha);
80 static void qls_free_lro(qla_host_t *ha);
82 static void qls_error_recovery(void *context, int pending);
85 * Hooks to the Operating Systems
87 static int qls_pci_probe (device_t);
88 static int qls_pci_attach (device_t);
89 static int qls_pci_detach (device_t);
91 static void qls_start(if_t ifp);
92 static void qls_init(void *arg);
93 static int qls_ioctl(if_t ifp, u_long cmd, caddr_t data);
94 static int qls_media_change(if_t ifp);
95 static void qls_media_status(if_t ifp, struct ifmediareq *ifmr);
97 static device_method_t qla_pci_methods[] = {
98 /* Device interface */
99 DEVMETHOD(device_probe, qls_pci_probe),
100 DEVMETHOD(device_attach, qls_pci_attach),
101 DEVMETHOD(device_detach, qls_pci_detach),
105 static driver_t qla_pci_driver = {
106 "ql", qla_pci_methods, sizeof (qla_host_t),
109 DRIVER_MODULE(qla8000, pci, qla_pci_driver, 0, 0);
111 MODULE_DEPEND(qla8000, pci, 1, 1, 1);
112 MODULE_DEPEND(qla8000, ether, 1, 1, 1);
114 MALLOC_DEFINE(M_QLA8000BUF, "qla8000buf", "Buffers for qla8000 driver");
116 static char dev_str[64];
117 static char ver_str[64];
120 * Name: qls_pci_probe
121 * Function: Validate the PCI device to be a QLA80XX device
124 qls_pci_probe(device_t dev)
126 switch ((pci_get_device(dev) << 16) | (pci_get_vendor(dev))) {
127 case PCI_QLOGIC_DEV8000:
128 snprintf(dev_str, sizeof(dev_str), "%s v%d.%d.%d",
129 "Qlogic ISP 8000 PCI CNA Adapter-Ethernet Function",
130 QLA_VERSION_MAJOR, QLA_VERSION_MINOR,
132 snprintf(ver_str, sizeof(ver_str), "v%d.%d.%d",
133 QLA_VERSION_MAJOR, QLA_VERSION_MINOR,
135 device_set_desc(dev, dev_str);
142 printf("%s: %s\n ", __func__, dev_str);
144 return (BUS_PROBE_DEFAULT);
148 qls_sysctl_get_drvr_stats(SYSCTL_HANDLER_ARGS)
154 err = sysctl_handle_int(oidp, &ret, 0, req);
156 if (err || !req->newptr)
160 ha = (qla_host_t *)arg1;
162 for (i = 0; i < ha->num_tx_rings; i++) {
163 device_printf(ha->pci_dev,
164 "%s: tx_ring[%d].tx_frames= %p\n",
166 (void *)ha->tx_ring[i].tx_frames);
168 device_printf(ha->pci_dev,
169 "%s: tx_ring[%d].tx_tso_frames= %p\n",
171 (void *)ha->tx_ring[i].tx_tso_frames);
173 device_printf(ha->pci_dev,
174 "%s: tx_ring[%d].tx_vlan_frames= %p\n",
176 (void *)ha->tx_ring[i].tx_vlan_frames);
178 device_printf(ha->pci_dev,
179 "%s: tx_ring[%d].txr_free= 0x%08x\n",
181 ha->tx_ring[i].txr_free);
183 device_printf(ha->pci_dev,
184 "%s: tx_ring[%d].txr_next= 0x%08x\n",
186 ha->tx_ring[i].txr_next);
188 device_printf(ha->pci_dev,
189 "%s: tx_ring[%d].txr_done= 0x%08x\n",
191 ha->tx_ring[i].txr_done);
193 device_printf(ha->pci_dev,
194 "%s: tx_ring[%d].txr_cons_idx= 0x%08x\n",
196 *(ha->tx_ring[i].txr_cons_vaddr));
199 for (i = 0; i < ha->num_rx_rings; i++) {
200 device_printf(ha->pci_dev,
201 "%s: rx_ring[%d].rx_int= %p\n",
203 (void *)ha->rx_ring[i].rx_int);
205 device_printf(ha->pci_dev,
206 "%s: rx_ring[%d].rss_int= %p\n",
208 (void *)ha->rx_ring[i].rss_int);
210 device_printf(ha->pci_dev,
211 "%s: rx_ring[%d].lbq_next= 0x%08x\n",
213 ha->rx_ring[i].lbq_next);
215 device_printf(ha->pci_dev,
216 "%s: rx_ring[%d].lbq_free= 0x%08x\n",
218 ha->rx_ring[i].lbq_free);
220 device_printf(ha->pci_dev,
221 "%s: rx_ring[%d].lbq_in= 0x%08x\n",
223 ha->rx_ring[i].lbq_in);
225 device_printf(ha->pci_dev,
226 "%s: rx_ring[%d].sbq_next= 0x%08x\n",
228 ha->rx_ring[i].sbq_next);
230 device_printf(ha->pci_dev,
231 "%s: rx_ring[%d].sbq_free= 0x%08x\n",
233 ha->rx_ring[i].sbq_free);
235 device_printf(ha->pci_dev,
236 "%s: rx_ring[%d].sbq_in= 0x%08x\n",
238 ha->rx_ring[i].sbq_in);
241 device_printf(ha->pci_dev, "%s: err_m_getcl = 0x%08x\n",
242 __func__, ha->err_m_getcl);
243 device_printf(ha->pci_dev, "%s: err_m_getjcl = 0x%08x\n",
244 __func__, ha->err_m_getjcl);
245 device_printf(ha->pci_dev,
246 "%s: err_tx_dmamap_create = 0x%08x\n",
247 __func__, ha->err_tx_dmamap_create);
248 device_printf(ha->pci_dev,
249 "%s: err_tx_dmamap_load = 0x%08x\n",
250 __func__, ha->err_tx_dmamap_load);
251 device_printf(ha->pci_dev,
252 "%s: err_tx_defrag = 0x%08x\n",
253 __func__, ha->err_tx_defrag);
259 qls_add_sysctls(qla_host_t *ha)
261 device_t dev = ha->pci_dev;
263 SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
264 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
265 OID_AUTO, "version", CTLFLAG_RD,
266 ver_str, 0, "Driver Version");
269 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
270 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
271 OID_AUTO, "debug", CTLFLAG_RW,
272 &qls_dbg_level, qls_dbg_level, "Debug Level");
274 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
275 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
276 OID_AUTO, "drvr_stats",
277 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, (void *)ha, 0,
278 qls_sysctl_get_drvr_stats, "I", "Driver Maintained Statistics");
284 qls_watchdog(void *arg)
286 qla_host_t *ha = arg;
291 if (ha->flags.qla_watchdog_exit) {
292 ha->qla_watchdog_exited = 1;
295 ha->qla_watchdog_exited = 0;
297 if (!ha->flags.qla_watchdog_pause) {
298 if (ha->qla_initiate_recovery) {
299 ha->qla_watchdog_paused = 1;
300 ha->qla_initiate_recovery = 0;
302 taskqueue_enqueue(ha->err_tq, &ha->err_task);
304 } else if (!if_sendq_empty(ifp) && QL_RUNNING(ifp)) {
305 taskqueue_enqueue(ha->tx_tq, &ha->tx_task);
308 ha->qla_watchdog_paused = 0;
310 ha->qla_watchdog_paused = 1;
313 ha->watchdog_ticks = (ha->watchdog_ticks + 1) % 1000;
314 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
321 * Name: qls_pci_attach
322 * Function: attaches the device to the operating system
325 qls_pci_attach(device_t dev)
327 qla_host_t *ha = NULL;
330 QL_DPRINT2((dev, "%s: enter\n", __func__));
332 if ((ha = device_get_softc(dev)) == NULL) {
333 device_printf(dev, "cannot get softc\n");
337 memset(ha, 0, sizeof (qla_host_t));
339 if (pci_get_device(dev) != PCI_DEVICE_QLOGIC_8000) {
340 device_printf(dev, "device is not QLE8000\n");
344 ha->pci_func = pci_get_function(dev);
348 pci_enable_busmaster(dev);
350 ha->reg_rid = PCIR_BAR(1);
351 ha->pci_reg = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &ha->reg_rid,
354 if (ha->pci_reg == NULL) {
355 device_printf(dev, "unable to map any ports\n");
356 goto qls_pci_attach_err;
359 ha->reg_rid1 = PCIR_BAR(3);
360 ha->pci_reg1 = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
361 &ha->reg_rid1, RF_ACTIVE);
363 if (ha->pci_reg1 == NULL) {
364 device_printf(dev, "unable to map any ports\n");
365 goto qls_pci_attach_err;
368 mtx_init(&ha->hw_lock, "qla80xx_hw_lock", MTX_NETWORK_LOCK, MTX_DEF);
369 mtx_init(&ha->tx_lock, "qla80xx_tx_lock", MTX_NETWORK_LOCK, MTX_DEF);
372 qls_hw_add_sysctls(ha);
374 ha->flags.lock_init = 1;
376 ha->msix_count = pci_msix_count(dev);
378 if (ha->msix_count < qls_get_msix_count(ha)) {
379 device_printf(dev, "%s: msix_count[%d] not enough\n", __func__,
381 goto qls_pci_attach_err;
384 ha->msix_count = qls_get_msix_count(ha);
386 device_printf(dev, "\n%s: ha %p pci_func 0x%x msix_count 0x%x"
387 " pci_reg %p pci_reg1 %p\n", __func__, ha,
388 ha->pci_func, ha->msix_count, ha->pci_reg, ha->pci_reg1);
390 if (pci_alloc_msix(dev, &ha->msix_count)) {
391 device_printf(dev, "%s: pci_alloc_msi[%d] failed\n", __func__,
394 goto qls_pci_attach_err;
397 for (i = 0; i < ha->num_rx_rings; i++) {
398 ha->irq_vec[i].cq_idx = i;
399 ha->irq_vec[i].ha = ha;
400 ha->irq_vec[i].irq_rid = 1 + i;
402 ha->irq_vec[i].irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
403 &ha->irq_vec[i].irq_rid,
404 (RF_ACTIVE | RF_SHAREABLE));
406 if (ha->irq_vec[i].irq == NULL) {
407 device_printf(dev, "could not allocate interrupt\n");
408 goto qls_pci_attach_err;
411 if (bus_setup_intr(dev, ha->irq_vec[i].irq,
412 (INTR_TYPE_NET | INTR_MPSAFE), NULL, qls_isr,
413 &ha->irq_vec[i], &ha->irq_vec[i].handle)) {
415 "could not setup interrupt\n");
416 goto qls_pci_attach_err;
420 qls_rd_nic_params(ha);
422 /* allocate parent dma tag */
423 if (qls_alloc_parent_dma_tag(ha)) {
424 device_printf(dev, "%s: qls_alloc_parent_dma_tag failed\n",
426 goto qls_pci_attach_err;
429 /* alloc all dma buffers */
430 if (qls_alloc_dma(ha)) {
431 device_printf(dev, "%s: qls_alloc_dma failed\n", __func__);
432 goto qls_pci_attach_err;
435 /* create the o.s ethernet interface */
436 qls_init_ifnet(dev, ha);
438 ha->flags.qla_watchdog_active = 1;
439 ha->flags.qla_watchdog_pause = 1;
441 TASK_INIT(&ha->tx_task, 0, qls_tx_done, ha);
442 ha->tx_tq = taskqueue_create_fast("qla_txq", M_NOWAIT,
443 taskqueue_thread_enqueue, &ha->tx_tq);
444 taskqueue_start_threads(&ha->tx_tq, 1, PI_NET, "%s txq",
445 device_get_nameunit(ha->pci_dev));
447 callout_init(&ha->tx_callout, 1);
448 ha->flags.qla_callout_init = 1;
450 /* create ioctl device interface */
451 if (qls_make_cdev(ha)) {
452 device_printf(dev, "%s: qls_make_cdev failed\n", __func__);
453 goto qls_pci_attach_err;
456 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
459 TASK_INIT(&ha->err_task, 0, qls_error_recovery, ha);
460 ha->err_tq = taskqueue_create_fast("qla_errq", M_NOWAIT,
461 taskqueue_thread_enqueue, &ha->err_tq);
462 taskqueue_start_threads(&ha->err_tq, 1, PI_NET, "%s errq",
463 device_get_nameunit(ha->pci_dev));
465 QL_DPRINT2((dev, "%s: exit 0\n", __func__));
472 QL_DPRINT2((dev, "%s: exit ENXIO\n", __func__));
477 * Name: qls_pci_detach
478 * Function: Unhooks the device from the operating system
481 qls_pci_detach(device_t dev)
483 qla_host_t *ha = NULL;
485 QL_DPRINT2((dev, "%s: enter\n", __func__));
487 if ((ha = device_get_softc(dev)) == NULL) {
488 device_printf(dev, "cannot get softc\n");
492 (void)QLA_LOCK(ha, __func__, 0);
494 QLA_UNLOCK(ha, __func__);
498 QL_DPRINT2((dev, "%s: exit\n", __func__));
505 * Function: Releases the resources allocated for the device
508 qls_release(qla_host_t *ha)
516 taskqueue_drain(ha->err_tq, &ha->err_task);
517 taskqueue_free(ha->err_tq);
521 taskqueue_drain(ha->tx_tq, &ha->tx_task);
522 taskqueue_free(ha->tx_tq);
527 if (ha->flags.qla_watchdog_active) {
528 ha->flags.qla_watchdog_exit = 1;
530 while (ha->qla_watchdog_exited == 0)
531 qls_mdelay(__func__, 1);
534 if (ha->flags.qla_callout_init)
535 callout_stop(&ha->tx_callout);
538 ether_ifdetach(ha->ifp);
541 qls_free_parent_dma_tag(ha);
543 for (i = 0; i < ha->num_rx_rings; i++) {
544 if (ha->irq_vec[i].handle) {
545 (void)bus_teardown_intr(dev, ha->irq_vec[i].irq,
546 ha->irq_vec[i].handle);
549 if (ha->irq_vec[i].irq) {
550 (void)bus_release_resource(dev, SYS_RES_IRQ,
551 ha->irq_vec[i].irq_rid,
557 pci_release_msi(dev);
559 if (ha->flags.lock_init) {
560 mtx_destroy(&ha->tx_lock);
561 mtx_destroy(&ha->hw_lock);
565 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid,
569 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid1,
574 * DMA Related Functions
578 qls_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
580 *((bus_addr_t *)arg) = 0;
583 printf("%s: bus_dmamap_load failed (%d)\n", __func__, error);
587 *((bus_addr_t *)arg) = segs[0].ds_addr;
593 qls_alloc_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
601 QL_DPRINT2((dev, "%s: enter\n", __func__));
603 ret = bus_dma_tag_create(
604 ha->parent_tag,/* parent */
606 ((bus_size_t)(1ULL << 32)),/* boundary */
607 BUS_SPACE_MAXADDR, /* lowaddr */
608 BUS_SPACE_MAXADDR, /* highaddr */
609 NULL, NULL, /* filter, filterarg */
610 dma_buf->size, /* maxsize */
612 dma_buf->size, /* maxsegsize */
614 NULL, NULL, /* lockfunc, lockarg */
618 device_printf(dev, "%s: could not create dma tag\n", __func__);
619 goto qls_alloc_dmabuf_exit;
621 ret = bus_dmamem_alloc(dma_buf->dma_tag,
622 (void **)&dma_buf->dma_b,
623 (BUS_DMA_ZERO | BUS_DMA_COHERENT | BUS_DMA_NOWAIT),
626 bus_dma_tag_destroy(dma_buf->dma_tag);
627 device_printf(dev, "%s: bus_dmamem_alloc failed\n", __func__);
628 goto qls_alloc_dmabuf_exit;
631 ret = bus_dmamap_load(dma_buf->dma_tag,
636 &b_addr, BUS_DMA_NOWAIT);
638 if (ret || !b_addr) {
639 bus_dma_tag_destroy(dma_buf->dma_tag);
640 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b,
643 goto qls_alloc_dmabuf_exit;
646 dma_buf->dma_addr = b_addr;
648 qls_alloc_dmabuf_exit:
649 QL_DPRINT2((dev, "%s: exit ret 0x%08x tag %p map %p b %p sz 0x%x\n",
650 __func__, ret, (void *)dma_buf->dma_tag,
651 (void *)dma_buf->dma_map, (void *)dma_buf->dma_b,
658 qls_free_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
660 bus_dmamap_unload(dma_buf->dma_tag, dma_buf->dma_map);
661 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b, dma_buf->dma_map);
662 bus_dma_tag_destroy(dma_buf->dma_tag);
666 qls_alloc_parent_dma_tag(qla_host_t *ha)
674 * Allocate parent DMA Tag
676 ret = bus_dma_tag_create(
677 bus_get_dma_tag(dev), /* parent */
678 1,((bus_size_t)(1ULL << 32)),/* alignment, boundary */
679 BUS_SPACE_MAXADDR, /* lowaddr */
680 BUS_SPACE_MAXADDR, /* highaddr */
681 NULL, NULL, /* filter, filterarg */
682 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
684 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
686 NULL, NULL, /* lockfunc, lockarg */
690 device_printf(dev, "%s: could not create parent dma tag\n",
695 ha->flags.parent_tag = 1;
701 qls_free_parent_dma_tag(qla_host_t *ha)
703 if (ha->flags.parent_tag) {
704 bus_dma_tag_destroy(ha->parent_tag);
705 ha->flags.parent_tag = 0;
710 * Name: qls_init_ifnet
711 * Function: Creates the Network Device Interface and Registers it with the O.S
715 qls_init_ifnet(device_t dev, qla_host_t *ha)
719 QL_DPRINT2((dev, "%s: enter\n", __func__));
721 ifp = ha->ifp = if_alloc(IFT_ETHER);
724 panic("%s: cannot if_alloc()\n", device_get_nameunit(dev));
726 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
727 if_setbaudrate(ifp, IF_Gbps(10));
728 if_setinitfn(ifp, qls_init);
729 if_setsoftc(ifp, ha);
730 if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
731 if_setioctlfn(ifp, qls_ioctl);
732 if_setstartfn(ifp, qls_start);
734 if_setsendqlen(ifp, qls_get_ifq_snd_maxlen(ha));
735 if_setsendqready(ifp);
737 ha->max_frame_size = if_getmtu(ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN;
738 if (ha->max_frame_size <= MCLBYTES) {
739 ha->msize = MCLBYTES;
740 } else if (ha->max_frame_size <= MJUMPAGESIZE) {
741 ha->msize = MJUMPAGESIZE;
743 ha->msize = MJUM9BYTES;
745 ether_ifattach(ifp, qls_get_mac_addr(ha));
747 if_setcapabilities(ifp, IFCAP_JUMBO_MTU);
749 if_setcapabilitiesbit(ifp, IFCAP_HWCSUM, 0);
750 if_setcapabilitiesbit(ifp, IFCAP_VLAN_MTU, 0);
752 if_setcapabilitiesbit(ifp, IFCAP_TSO4, 0);
753 if_setcapabilitiesbit(ifp, IFCAP_VLAN_HWTAGGING, 0);
754 if_setcapabilitiesbit(ifp, IFCAP_VLAN_HWTSO, 0);
755 if_setcapabilitiesbit(ifp, IFCAP_LINKSTATE, 0);
757 if_setcapenable(ifp, if_getcapabilities(ifp));
759 if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
761 ifmedia_init(&ha->media, IFM_IMASK, qls_media_change, qls_media_status);
763 ifmedia_add(&ha->media, (IFM_ETHER | qls_get_optics(ha) | IFM_FDX), 0,
765 ifmedia_add(&ha->media, (IFM_ETHER | IFM_AUTO), 0, NULL);
767 ifmedia_set(&ha->media, (IFM_ETHER | IFM_AUTO));
769 QL_DPRINT2((dev, "%s: exit\n", __func__));
775 qls_init_locked(qla_host_t *ha)
781 qls_flush_xmt_bufs(ha);
783 if (qls_alloc_rcv_bufs(ha) != 0)
786 if (qls_config_lro(ha))
789 bcopy(if_getlladdr(ha->ifp), ha->mac_addr, ETHER_ADDR_LEN);
791 if_sethwassist(ifp, CSUM_IP);
792 if_sethwassistbits(ifp, CSUM_TCP, 0);
793 if_sethwassistbits(ifp, CSUM_UDP, 0);
794 if_sethwassistbits(ifp, CSUM_TSO, 0);
796 if (qls_init_hw_if(ha) == 0) {
798 if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
799 if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
800 ha->flags.qla_watchdog_pause = 0;
811 ha = (qla_host_t *)arg;
813 QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
815 (void)QLA_LOCK(ha, __func__, 0);
817 QLA_UNLOCK(ha, __func__);
819 QL_DPRINT2((ha->pci_dev, "%s: exit\n", __func__));
823 qls_copy_maddr(void *arg, struct sockaddr_dl *sdl, u_int mcnt)
827 if (mcnt == Q8_MAX_NUM_MULTICAST_ADDRS)
830 bcopy(LLADDR(sdl), &mta[mcnt * Q8_MAC_ADDR_LEN], Q8_MAC_ADDR_LEN);
836 qls_set_multi(qla_host_t *ha, uint32_t add_multi)
838 uint8_t mta[Q8_MAX_NUM_MULTICAST_ADDRS * Q8_MAC_ADDR_LEN];
842 mcnt = if_foreach_llmaddr(ifp, qls_copy_maddr, mta);
844 if (QLA_LOCK(ha, __func__, 1) == 0) {
845 qls_hw_set_multi(ha, mta, mcnt, add_multi);
846 QLA_UNLOCK(ha, __func__);
853 qls_ioctl(if_t ifp, u_long cmd, caddr_t data)
856 struct ifreq *ifr = (struct ifreq *)data;
858 struct ifaddr *ifa = (struct ifaddr *)data;
862 ha = (qla_host_t *)if_getsoftc(ifp);
866 QL_DPRINT4((ha->pci_dev, "%s: SIOCSIFADDR (0x%lx)\n",
870 if (ifa->ifa_addr->sa_family == AF_INET) {
871 if_setflagbits(ifp, IFF_UP, 0);
872 if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
873 (void)QLA_LOCK(ha, __func__, 0);
875 QLA_UNLOCK(ha, __func__);
877 QL_DPRINT4((ha->pci_dev,
878 "%s: SIOCSIFADDR (0x%lx) ipv4 [0x%08x]\n",
880 ntohl(IA_SIN(ifa)->sin_addr.s_addr)));
882 arp_ifinit(ifp, ifa);
886 ether_ioctl(ifp, cmd, data);
890 QL_DPRINT4((ha->pci_dev, "%s: SIOCSIFMTU (0x%lx)\n",
893 if (ifr->ifr_mtu > QLA_MAX_MTU) {
896 (void) QLA_LOCK(ha, __func__, 0);
898 if_setmtu(ifp, ifr->ifr_mtu);
900 if_getmtu(ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN;
902 QLA_UNLOCK(ha, __func__);
911 QL_DPRINT4((ha->pci_dev, "%s: SIOCSIFFLAGS (0x%lx)\n",
914 (void)QLA_LOCK(ha, __func__, 0);
916 if (if_getflags(ifp) & IFF_UP) {
917 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
918 if ((if_getflags(ifp) ^ ha->if_flags) &
920 ret = qls_set_promisc(ha);
921 } else if ((if_getflags(ifp) ^ ha->if_flags) &
923 ret = qls_set_allmulti(ha);
926 ha->max_frame_size = if_getmtu(ifp) +
927 ETHER_HDR_LEN + ETHER_CRC_LEN;
931 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
933 ha->if_flags = if_getflags(ifp);
936 QLA_UNLOCK(ha, __func__);
940 QL_DPRINT4((ha->pci_dev,
941 "%s: %s (0x%lx)\n", __func__, "SIOCADDMULTI", cmd));
943 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
944 qls_set_multi(ha, 1);
949 QL_DPRINT4((ha->pci_dev,
950 "%s: %s (0x%lx)\n", __func__, "SIOCDELMULTI", cmd));
952 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
953 qls_set_multi(ha, 0);
959 QL_DPRINT4((ha->pci_dev,
960 "%s: SIOCSIFMEDIA/SIOCGIFMEDIA (0x%lx)\n",
962 ret = ifmedia_ioctl(ifp, ifr, &ha->media, cmd);
967 int mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
969 QL_DPRINT4((ha->pci_dev, "%s: SIOCSIFCAP (0x%lx)\n",
972 if (mask & IFCAP_HWCSUM)
973 if_togglecapenable(ifp, IFCAP_HWCSUM);
974 if (mask & IFCAP_TSO4)
975 if_togglecapenable(ifp, IFCAP_TSO4);
976 if (mask & IFCAP_VLAN_HWTAGGING)
977 if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
978 if (mask & IFCAP_VLAN_HWTSO)
979 if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
981 if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
984 VLAN_CAPABILITIES(ifp);
989 QL_DPRINT4((ha->pci_dev, "%s: default (0x%lx)\n",
991 ret = ether_ioctl(ifp, cmd, data);
999 qls_media_change(if_t ifp)
1002 struct ifmedia *ifm;
1005 ha = (qla_host_t *)if_getsoftc(ifp);
1007 QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
1011 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1014 QL_DPRINT2((ha->pci_dev, "%s: exit\n", __func__));
1020 qls_media_status(if_t ifp, struct ifmediareq *ifmr)
1024 ha = (qla_host_t *)if_getsoftc(ifp);
1026 QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
1028 ifmr->ifm_status = IFM_AVALID;
1029 ifmr->ifm_active = IFM_ETHER;
1031 qls_update_link_state(ha);
1033 ifmr->ifm_status |= IFM_ACTIVE;
1034 ifmr->ifm_active |= (IFM_FDX | qls_get_optics(ha));
1037 QL_DPRINT2((ha->pci_dev, "%s: exit (%s)\n", __func__,\
1038 (ha->link_up ? "link_up" : "link_down")));
1047 struct mbuf *m_head;
1048 qla_host_t *ha = (qla_host_t *)if_getsoftc(ifp);
1050 QL_DPRINT8((ha->pci_dev, "%s: enter\n", __func__));
1052 if (!mtx_trylock(&ha->tx_lock)) {
1053 QL_DPRINT8((ha->pci_dev,
1054 "%s: mtx_trylock(&ha->tx_lock) failed\n", __func__));
1058 if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) ==
1060 for (i = 0; i < ha->num_tx_rings; i++) {
1061 ret |= qls_hw_tx_done(ha, i);
1065 if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
1068 if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1070 QL_DPRINT8((ha->pci_dev, "%s: !IFF_DRV_RUNNING\n", __func__));
1076 qls_update_link_state(ha);
1078 QL_DPRINT8((ha->pci_dev, "%s: link down\n", __func__));
1084 while (!if_sendq_empty(ifp)) {
1085 m_head = if_dequeue(ifp);
1087 if (m_head == NULL) {
1088 QL_DPRINT8((ha->pci_dev, "%s: m_head == NULL\n",
1093 if (qls_send(ha, &m_head)) {
1096 QL_DPRINT8((ha->pci_dev, "%s: PREPEND\n", __func__));
1097 if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
1098 if_sendq_prepend(ifp, m_head);
1101 /* Send a copy of the frame to the BPF listener */
1102 ETHER_BPF_MTAP(ifp, m_head);
1106 QL_DPRINT8((ha->pci_dev, "%s: exit\n", __func__));
1111 qls_send(qla_host_t *ha, struct mbuf **m_headp)
1113 bus_dma_segment_t segs[QLA_MAX_SEGMENTS];
1118 struct mbuf *m_head = *m_headp;
1119 uint32_t txr_idx = 0;
1121 QL_DPRINT8((ha->pci_dev, "%s: enter\n", __func__));
1123 /* check if flowid is set */
1124 if (M_HASHTYPE_GET(m_head) != M_HASHTYPE_NONE)
1125 txr_idx = m_head->m_pkthdr.flowid & (ha->num_tx_rings - 1);
1127 tx_idx = ha->tx_ring[txr_idx].txr_next;
1129 map = ha->tx_ring[txr_idx].tx_buf[tx_idx].map;
1131 ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head, segs, &nsegs,
1137 QL_DPRINT8((ha->pci_dev, "%s: EFBIG [%d]\n", __func__,
1138 m_head->m_pkthdr.len));
1140 m = m_defrag(m_head, M_NOWAIT);
1142 ha->err_tx_defrag++;
1145 device_printf(ha->pci_dev,
1146 "%s: m_defrag() = NULL [%d]\n",
1153 if ((ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head,
1154 segs, &nsegs, BUS_DMA_NOWAIT))) {
1155 ha->err_tx_dmamap_load++;
1157 device_printf(ha->pci_dev,
1158 "%s: bus_dmamap_load_mbuf_sg failed0[%d, %d]\n",
1159 __func__, ret, m_head->m_pkthdr.len);
1161 if (ret != ENOMEM) {
1169 ha->err_tx_dmamap_load++;
1171 device_printf(ha->pci_dev,
1172 "%s: bus_dmamap_load_mbuf_sg failed1[%d, %d]\n",
1173 __func__, ret, m_head->m_pkthdr.len);
1175 if (ret != ENOMEM) {
1182 QL_ASSERT(ha, (nsegs != 0), ("qls_send: empty packet"));
1184 bus_dmamap_sync(ha->tx_tag, map, BUS_DMASYNC_PREWRITE);
1186 if (!(ret = qls_hw_send(ha, segs, nsegs, tx_idx, m_head, txr_idx))) {
1187 ha->tx_ring[txr_idx].count++;
1188 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head = m_head;
1189 ha->tx_ring[txr_idx].tx_buf[tx_idx].map = map;
1191 if (ret == EINVAL) {
1198 QL_DPRINT8((ha->pci_dev, "%s: exit\n", __func__));
1203 qls_stop(qla_host_t *ha)
1207 if_setdrvflagbits(ifp, 0, (IFF_DRV_OACTIVE | IFF_DRV_RUNNING));
1209 ha->flags.qla_watchdog_pause = 1;
1211 while (!ha->qla_watchdog_paused)
1212 qls_mdelay(__func__, 1);
1218 qls_flush_xmt_bufs(ha);
1219 qls_free_rcv_bufs(ha);
1225 * Buffer Management Functions for Transmit and Receive Rings
1228 * Release mbuf after it sent on the wire
1231 qls_flush_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb)
1233 QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
1236 bus_dmamap_unload(ha->tx_tag, txb->map);
1238 m_freem(txb->m_head);
1242 QL_DPRINT2((ha->pci_dev, "%s: exit\n", __func__));
1246 qls_flush_xmt_bufs(qla_host_t *ha)
1250 for (j = 0; j < ha->num_tx_rings; j++) {
1251 for (i = 0; i < NUM_TX_DESCRIPTORS; i++)
1252 qls_flush_tx_buf(ha, &ha->tx_ring[j].tx_buf[i]);
1259 qls_alloc_rcv_mbufs(qla_host_t *ha, int r)
1263 qla_rx_ring_t *rx_ring;
1264 volatile q81_bq_addr_e_t *sbq_e;
1266 rx_ring = &ha->rx_ring[r];
1268 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1269 rxb = &rx_ring->rx_buf[i];
1271 ret = bus_dmamap_create(ha->rx_tag, BUS_DMA_NOWAIT, &rxb->map);
1274 device_printf(ha->pci_dev,
1275 "%s: dmamap[%d, %d] failed\n", __func__, r, i);
1277 for (j = 0; j < i; j++) {
1278 rxb = &rx_ring->rx_buf[j];
1279 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1281 goto qls_alloc_rcv_mbufs_err;
1285 rx_ring = &ha->rx_ring[r];
1287 sbq_e = rx_ring->sbq_vaddr;
1289 rxb = &rx_ring->rx_buf[0];
1291 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1292 if (!(ret = qls_get_mbuf(ha, rxb, NULL))) {
1294 * set the physical address in the
1295 * corresponding descriptor entry in the
1296 * receive ring/queue for the hba
1299 sbq_e->addr_lo = rxb->paddr & 0xFFFFFFFF;
1300 sbq_e->addr_hi = (rxb->paddr >> 32) & 0xFFFFFFFF;
1303 device_printf(ha->pci_dev,
1304 "%s: qls_get_mbuf [%d, %d] failed\n",
1306 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1307 goto qls_alloc_rcv_mbufs_err;
1315 qls_alloc_rcv_mbufs_err:
1320 qls_free_rcv_bufs(qla_host_t *ha)
1326 for (r = 0; r < ha->num_rx_rings; r++) {
1327 rxr = &ha->rx_ring[r];
1329 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1330 rxb = &rxr->rx_buf[i];
1332 if (rxb->m_head != NULL) {
1333 bus_dmamap_unload(ha->rx_tag, rxb->map);
1334 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1335 m_freem(rxb->m_head);
1338 bzero(rxr->rx_buf, (sizeof(qla_rx_buf_t) * NUM_RX_DESCRIPTORS));
1344 qls_alloc_rcv_bufs(qla_host_t *ha)
1349 for (r = 0; r < ha->num_rx_rings; r++) {
1350 rxr = &ha->rx_ring[r];
1351 bzero(rxr->rx_buf, (sizeof(qla_rx_buf_t) * NUM_RX_DESCRIPTORS));
1354 for (r = 0; r < ha->num_rx_rings; r++) {
1355 ret = qls_alloc_rcv_mbufs(ha, r);
1358 qls_free_rcv_bufs(ha);
1365 qls_get_mbuf(qla_host_t *ha, qla_rx_buf_t *rxb, struct mbuf *nmp)
1367 struct mbuf *mp = nmp;
1370 bus_dma_segment_t segs[1];
1373 QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
1376 mp = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, ha->msize);
1379 if (ha->msize == MCLBYTES)
1385 device_printf(ha->pci_dev,
1386 "%s: m_getcl failed\n", __func__);
1387 goto exit_qls_get_mbuf;
1389 mp->m_len = mp->m_pkthdr.len = ha->msize;
1391 mp->m_len = mp->m_pkthdr.len = ha->msize;
1392 mp->m_data = mp->m_ext.ext_buf;
1396 /* align the receive buffers to 8 byte boundary */
1397 offset = (uint32_t)((unsigned long long)mp->m_data & 0x7ULL);
1399 offset = 8 - offset;
1404 * Using memory from the mbuf cluster pool, invoke the bus_dma
1405 * machinery to arrange the memory mapping.
1407 ret = bus_dmamap_load_mbuf_sg(ha->rx_tag, rxb->map,
1408 mp, segs, &nsegs, BUS_DMA_NOWAIT);
1409 rxb->paddr = segs[0].ds_addr;
1411 if (ret || !rxb->paddr || (nsegs != 1)) {
1414 device_printf(ha->pci_dev,
1415 "%s: bus_dmamap_load failed[%d, 0x%016llx, %d]\n",
1416 __func__, ret, (long long unsigned int)rxb->paddr,
1419 goto exit_qls_get_mbuf;
1422 bus_dmamap_sync(ha->rx_tag, rxb->map, BUS_DMASYNC_PREREAD);
1425 QL_DPRINT2((ha->pci_dev, "%s: exit ret = 0x%08x\n", __func__, ret));
1430 qls_tx_done(void *context, int pending)
1432 qla_host_t *ha = context;
1440 if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
1441 QL_DPRINT8((ha->pci_dev, "%s: !IFF_DRV_RUNNING\n", __func__));
1450 qls_config_lro(qla_host_t *ha)
1452 #if defined(INET) || defined(INET6)
1454 struct lro_ctrl *lro;
1456 for (i = 0; i < ha->num_rx_rings; i++) {
1457 lro = &ha->rx_ring[i].lro;
1458 if (tcp_lro_init(lro)) {
1459 device_printf(ha->pci_dev, "%s: tcp_lro_init failed\n",
1465 ha->flags.lro_init = 1;
1467 QL_DPRINT2((ha->pci_dev, "%s: LRO initialized\n", __func__));
1473 qls_free_lro(qla_host_t *ha)
1475 #if defined(INET) || defined(INET6)
1477 struct lro_ctrl *lro;
1479 if (!ha->flags.lro_init)
1482 for (i = 0; i < ha->num_rx_rings; i++) {
1483 lro = &ha->rx_ring[i].lro;
1486 ha->flags.lro_init = 0;
1491 qls_error_recovery(void *context, int pending)
1493 qla_host_t *ha = context;