2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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>
36 __FBSDID("$FreeBSD$");
42 #include "qls_inline.h"
49 * Some PCI Configuration Space Related Defines
52 #ifndef PCI_VENDOR_QLOGIC
53 #define PCI_VENDOR_QLOGIC 0x1077
56 #ifndef PCI_DEVICE_QLOGIC_8000
57 #define PCI_DEVICE_QLOGIC_8000 0x8000
60 #define PCI_QLOGIC_DEV8000 \
61 ((PCI_DEVICE_QLOGIC_8000 << 16) | PCI_VENDOR_QLOGIC)
66 static int qls_alloc_parent_dma_tag(qla_host_t *ha);
67 static void qls_free_parent_dma_tag(qla_host_t *ha);
69 static void qls_flush_xmt_bufs(qla_host_t *ha);
71 static int qls_alloc_rcv_bufs(qla_host_t *ha);
72 static void qls_free_rcv_bufs(qla_host_t *ha);
74 static void qls_init_ifnet(device_t dev, qla_host_t *ha);
75 static void qls_release(qla_host_t *ha);
76 static void qls_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs,
78 static void qls_stop(qla_host_t *ha);
79 static int qls_send(qla_host_t *ha, struct mbuf **m_headp);
80 static void qls_tx_done(void *context, int pending);
82 static int qls_config_lro(qla_host_t *ha);
83 static void qls_free_lro(qla_host_t *ha);
85 static void qls_error_recovery(void *context, int pending);
88 * Hooks to the Operating Systems
90 static int qls_pci_probe (device_t);
91 static int qls_pci_attach (device_t);
92 static int qls_pci_detach (device_t);
94 static void qls_start(struct ifnet *ifp);
95 static void qls_init(void *arg);
96 static int qls_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
97 static int qls_media_change(struct ifnet *ifp);
98 static void qls_media_status(struct ifnet *ifp, struct ifmediareq *ifmr);
100 static device_method_t qla_pci_methods[] = {
101 /* Device interface */
102 DEVMETHOD(device_probe, qls_pci_probe),
103 DEVMETHOD(device_attach, qls_pci_attach),
104 DEVMETHOD(device_detach, qls_pci_detach),
108 static driver_t qla_pci_driver = {
109 "ql", qla_pci_methods, sizeof (qla_host_t),
112 static devclass_t qla8000_devclass;
114 DRIVER_MODULE(qla8000, pci, qla_pci_driver, qla8000_devclass, 0, 0);
116 MODULE_DEPEND(qla8000, pci, 1, 1, 1);
117 MODULE_DEPEND(qla8000, ether, 1, 1, 1);
119 MALLOC_DEFINE(M_QLA8000BUF, "qla8000buf", "Buffers for qla8000 driver");
121 static char dev_str[64];
122 static char ver_str[64];
125 * Name: qls_pci_probe
126 * Function: Validate the PCI device to be a QLA80XX device
129 qls_pci_probe(device_t dev)
131 switch ((pci_get_device(dev) << 16) | (pci_get_vendor(dev))) {
132 case PCI_QLOGIC_DEV8000:
133 snprintf(dev_str, sizeof(dev_str), "%s v%d.%d.%d",
134 "Qlogic ISP 8000 PCI CNA Adapter-Ethernet Function",
135 QLA_VERSION_MAJOR, QLA_VERSION_MINOR,
137 snprintf(ver_str, sizeof(ver_str), "v%d.%d.%d",
138 QLA_VERSION_MAJOR, QLA_VERSION_MINOR,
140 device_set_desc(dev, dev_str);
147 printf("%s: %s\n ", __func__, dev_str);
149 return (BUS_PROBE_DEFAULT);
153 qls_sysctl_get_drvr_stats(SYSCTL_HANDLER_ARGS)
159 err = sysctl_handle_int(oidp, &ret, 0, req);
161 if (err || !req->newptr)
166 ha = (qla_host_t *)arg1;
168 for (i = 0; i < ha->num_tx_rings; i++) {
170 device_printf(ha->pci_dev,
171 "%s: tx_ring[%d].tx_frames= %p\n",
173 (void *)ha->tx_ring[i].tx_frames);
175 device_printf(ha->pci_dev,
176 "%s: tx_ring[%d].tx_tso_frames= %p\n",
178 (void *)ha->tx_ring[i].tx_tso_frames);
180 device_printf(ha->pci_dev,
181 "%s: tx_ring[%d].tx_vlan_frames= %p\n",
183 (void *)ha->tx_ring[i].tx_vlan_frames);
185 device_printf(ha->pci_dev,
186 "%s: tx_ring[%d].txr_free= 0x%08x\n",
188 ha->tx_ring[i].txr_free);
190 device_printf(ha->pci_dev,
191 "%s: tx_ring[%d].txr_next= 0x%08x\n",
193 ha->tx_ring[i].txr_next);
195 device_printf(ha->pci_dev,
196 "%s: tx_ring[%d].txr_done= 0x%08x\n",
198 ha->tx_ring[i].txr_done);
200 device_printf(ha->pci_dev,
201 "%s: tx_ring[%d].txr_cons_idx= 0x%08x\n",
203 *(ha->tx_ring[i].txr_cons_vaddr));
206 for (i = 0; i < ha->num_rx_rings; i++) {
208 device_printf(ha->pci_dev,
209 "%s: rx_ring[%d].rx_int= %p\n",
211 (void *)ha->rx_ring[i].rx_int);
213 device_printf(ha->pci_dev,
214 "%s: rx_ring[%d].rss_int= %p\n",
216 (void *)ha->rx_ring[i].rss_int);
218 device_printf(ha->pci_dev,
219 "%s: rx_ring[%d].lbq_next= 0x%08x\n",
221 ha->rx_ring[i].lbq_next);
223 device_printf(ha->pci_dev,
224 "%s: rx_ring[%d].lbq_free= 0x%08x\n",
226 ha->rx_ring[i].lbq_free);
228 device_printf(ha->pci_dev,
229 "%s: rx_ring[%d].lbq_in= 0x%08x\n",
231 ha->rx_ring[i].lbq_in);
233 device_printf(ha->pci_dev,
234 "%s: rx_ring[%d].sbq_next= 0x%08x\n",
236 ha->rx_ring[i].sbq_next);
238 device_printf(ha->pci_dev,
239 "%s: rx_ring[%d].sbq_free= 0x%08x\n",
241 ha->rx_ring[i].sbq_free);
243 device_printf(ha->pci_dev,
244 "%s: rx_ring[%d].sbq_in= 0x%08x\n",
246 ha->rx_ring[i].sbq_in);
249 device_printf(ha->pci_dev, "%s: err_m_getcl = 0x%08x\n",
250 __func__, ha->err_m_getcl);
251 device_printf(ha->pci_dev, "%s: err_m_getjcl = 0x%08x\n",
252 __func__, ha->err_m_getjcl);
253 device_printf(ha->pci_dev,
254 "%s: err_tx_dmamap_create = 0x%08x\n",
255 __func__, ha->err_tx_dmamap_create);
256 device_printf(ha->pci_dev,
257 "%s: err_tx_dmamap_load = 0x%08x\n",
258 __func__, ha->err_tx_dmamap_load);
259 device_printf(ha->pci_dev,
260 "%s: err_tx_defrag = 0x%08x\n",
261 __func__, ha->err_tx_defrag);
267 qls_add_sysctls(qla_host_t *ha)
269 device_t dev = ha->pci_dev;
271 SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
272 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
273 OID_AUTO, "version", CTLFLAG_RD,
274 ver_str, 0, "Driver Version");
277 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
278 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
279 OID_AUTO, "debug", CTLFLAG_RW,
280 &qls_dbg_level, qls_dbg_level, "Debug Level");
282 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
283 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
284 OID_AUTO, "drvr_stats", CTLTYPE_INT | CTLFLAG_RW,
286 qls_sysctl_get_drvr_stats, "I", "Driver Maintained Statistics");
292 qls_watchdog(void *arg)
294 qla_host_t *ha = arg;
299 if (ha->flags.qla_watchdog_exit) {
300 ha->qla_watchdog_exited = 1;
303 ha->qla_watchdog_exited = 0;
305 if (!ha->flags.qla_watchdog_pause) {
307 if (ha->qla_initiate_recovery) {
309 ha->qla_watchdog_paused = 1;
310 ha->qla_initiate_recovery = 0;
312 taskqueue_enqueue(ha->err_tq, &ha->err_task);
314 } else if ((ifp->if_snd.ifq_head != NULL) && QL_RUNNING(ifp)) {
316 taskqueue_enqueue(ha->tx_tq, &ha->tx_task);
319 ha->qla_watchdog_paused = 0;
321 ha->qla_watchdog_paused = 1;
324 ha->watchdog_ticks = ha->watchdog_ticks++ % 1000;
325 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
332 * Name: qls_pci_attach
333 * Function: attaches the device to the operating system
336 qls_pci_attach(device_t dev)
338 qla_host_t *ha = NULL;
341 QL_DPRINT2((dev, "%s: enter\n", __func__));
343 if ((ha = device_get_softc(dev)) == NULL) {
344 device_printf(dev, "cannot get softc\n");
348 memset(ha, 0, sizeof (qla_host_t));
350 if (pci_get_device(dev) != PCI_DEVICE_QLOGIC_8000) {
351 device_printf(dev, "device is not QLE8000\n");
355 ha->pci_func = pci_get_function(dev);
359 pci_enable_busmaster(dev);
361 ha->reg_rid = PCIR_BAR(1);
362 ha->pci_reg = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &ha->reg_rid,
365 if (ha->pci_reg == NULL) {
366 device_printf(dev, "unable to map any ports\n");
367 goto qls_pci_attach_err;
370 ha->reg_rid1 = PCIR_BAR(3);
371 ha->pci_reg1 = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
372 &ha->reg_rid1, RF_ACTIVE);
374 if (ha->pci_reg1 == NULL) {
375 device_printf(dev, "unable to map any ports\n");
376 goto qls_pci_attach_err;
379 mtx_init(&ha->hw_lock, "qla80xx_hw_lock", MTX_NETWORK_LOCK, MTX_DEF);
380 mtx_init(&ha->tx_lock, "qla80xx_tx_lock", MTX_NETWORK_LOCK, MTX_DEF);
383 qls_hw_add_sysctls(ha);
385 ha->flags.lock_init = 1;
387 ha->msix_count = pci_msix_count(dev);
389 if (ha->msix_count < qls_get_msix_count(ha)) {
390 device_printf(dev, "%s: msix_count[%d] not enough\n", __func__,
392 goto qls_pci_attach_err;
395 ha->msix_count = qls_get_msix_count(ha);
397 device_printf(dev, "\n%s: ha %p pci_func 0x%x msix_count 0x%x"
398 " pci_reg %p pci_reg1 %p\n", __func__, ha,
399 ha->pci_func, ha->msix_count, ha->pci_reg, ha->pci_reg1);
401 if (pci_alloc_msix(dev, &ha->msix_count)) {
402 device_printf(dev, "%s: pci_alloc_msi[%d] failed\n", __func__,
405 goto qls_pci_attach_err;
408 for (i = 0; i < ha->num_rx_rings; i++) {
409 ha->irq_vec[i].cq_idx = i;
410 ha->irq_vec[i].ha = ha;
411 ha->irq_vec[i].irq_rid = 1 + i;
413 ha->irq_vec[i].irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
414 &ha->irq_vec[i].irq_rid,
415 (RF_ACTIVE | RF_SHAREABLE));
417 if (ha->irq_vec[i].irq == NULL) {
418 device_printf(dev, "could not allocate interrupt\n");
419 goto qls_pci_attach_err;
422 if (bus_setup_intr(dev, ha->irq_vec[i].irq,
423 (INTR_TYPE_NET | INTR_MPSAFE), NULL, qls_isr,
424 &ha->irq_vec[i], &ha->irq_vec[i].handle)) {
426 "could not setup interrupt\n");
427 goto qls_pci_attach_err;
431 qls_rd_nic_params(ha);
433 /* allocate parent dma tag */
434 if (qls_alloc_parent_dma_tag(ha)) {
435 device_printf(dev, "%s: qls_alloc_parent_dma_tag failed\n",
437 goto qls_pci_attach_err;
440 /* alloc all dma buffers */
441 if (qls_alloc_dma(ha)) {
442 device_printf(dev, "%s: qls_alloc_dma failed\n", __func__);
443 goto qls_pci_attach_err;
446 /* create the o.s ethernet interface */
447 qls_init_ifnet(dev, ha);
449 ha->flags.qla_watchdog_active = 1;
450 ha->flags.qla_watchdog_pause = 1;
452 TASK_INIT(&ha->tx_task, 0, qls_tx_done, ha);
453 ha->tx_tq = taskqueue_create_fast("qla_txq", M_NOWAIT,
454 taskqueue_thread_enqueue, &ha->tx_tq);
455 taskqueue_start_threads(&ha->tx_tq, 1, PI_NET, "%s txq",
456 device_get_nameunit(ha->pci_dev));
458 callout_init(&ha->tx_callout, 1);
459 ha->flags.qla_callout_init = 1;
461 /* create ioctl device interface */
462 if (qls_make_cdev(ha)) {
463 device_printf(dev, "%s: qls_make_cdev failed\n", __func__);
464 goto qls_pci_attach_err;
467 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
470 TASK_INIT(&ha->err_task, 0, qls_error_recovery, ha);
471 ha->err_tq = taskqueue_create_fast("qla_errq", M_NOWAIT,
472 taskqueue_thread_enqueue, &ha->err_tq);
473 taskqueue_start_threads(&ha->err_tq, 1, PI_NET, "%s errq",
474 device_get_nameunit(ha->pci_dev));
476 QL_DPRINT2((dev, "%s: exit 0\n", __func__));
483 QL_DPRINT2((dev, "%s: exit ENXIO\n", __func__));
488 * Name: qls_pci_detach
489 * Function: Unhooks the device from the operating system
492 qls_pci_detach(device_t dev)
494 qla_host_t *ha = NULL;
497 QL_DPRINT2((dev, "%s: enter\n", __func__));
499 if ((ha = device_get_softc(dev)) == NULL) {
500 device_printf(dev, "cannot get softc\n");
506 (void)QLA_LOCK(ha, __func__, 0);
508 QLA_UNLOCK(ha, __func__);
512 QL_DPRINT2((dev, "%s: exit\n", __func__));
519 * Function: Releases the resources allocated for the device
522 qls_release(qla_host_t *ha)
530 taskqueue_drain(ha->err_tq, &ha->err_task);
531 taskqueue_free(ha->err_tq);
535 taskqueue_drain(ha->tx_tq, &ha->tx_task);
536 taskqueue_free(ha->tx_tq);
541 if (ha->flags.qla_watchdog_active) {
542 ha->flags.qla_watchdog_exit = 1;
544 while (ha->qla_watchdog_exited == 0)
545 qls_mdelay(__func__, 1);
548 if (ha->flags.qla_callout_init)
549 callout_stop(&ha->tx_callout);
552 ether_ifdetach(ha->ifp);
555 qls_free_parent_dma_tag(ha);
557 for (i = 0; i < ha->num_rx_rings; i++) {
559 if (ha->irq_vec[i].handle) {
560 (void)bus_teardown_intr(dev, ha->irq_vec[i].irq,
561 ha->irq_vec[i].handle);
564 if (ha->irq_vec[i].irq) {
565 (void)bus_release_resource(dev, SYS_RES_IRQ,
566 ha->irq_vec[i].irq_rid,
572 pci_release_msi(dev);
574 if (ha->flags.lock_init) {
575 mtx_destroy(&ha->tx_lock);
576 mtx_destroy(&ha->hw_lock);
580 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid,
584 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid1,
589 * DMA Related Functions
593 qls_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
595 *((bus_addr_t *)arg) = 0;
598 printf("%s: bus_dmamap_load failed (%d)\n", __func__, error);
602 *((bus_addr_t *)arg) = segs[0].ds_addr;
608 qls_alloc_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
616 QL_DPRINT2((dev, "%s: enter\n", __func__));
618 ret = bus_dma_tag_create(
619 ha->parent_tag,/* parent */
621 ((bus_size_t)(1ULL << 32)),/* boundary */
622 BUS_SPACE_MAXADDR, /* lowaddr */
623 BUS_SPACE_MAXADDR, /* highaddr */
624 NULL, NULL, /* filter, filterarg */
625 dma_buf->size, /* maxsize */
627 dma_buf->size, /* maxsegsize */
629 NULL, NULL, /* lockfunc, lockarg */
633 device_printf(dev, "%s: could not create dma tag\n", __func__);
634 goto qls_alloc_dmabuf_exit;
636 ret = bus_dmamem_alloc(dma_buf->dma_tag,
637 (void **)&dma_buf->dma_b,
638 (BUS_DMA_ZERO | BUS_DMA_COHERENT | BUS_DMA_NOWAIT),
641 bus_dma_tag_destroy(dma_buf->dma_tag);
642 device_printf(dev, "%s: bus_dmamem_alloc failed\n", __func__);
643 goto qls_alloc_dmabuf_exit;
646 ret = bus_dmamap_load(dma_buf->dma_tag,
651 &b_addr, BUS_DMA_NOWAIT);
653 if (ret || !b_addr) {
654 bus_dma_tag_destroy(dma_buf->dma_tag);
655 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b,
658 goto qls_alloc_dmabuf_exit;
661 dma_buf->dma_addr = b_addr;
663 qls_alloc_dmabuf_exit:
664 QL_DPRINT2((dev, "%s: exit ret 0x%08x tag %p map %p b %p sz 0x%x\n",
665 __func__, ret, (void *)dma_buf->dma_tag,
666 (void *)dma_buf->dma_map, (void *)dma_buf->dma_b,
673 qls_free_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
675 bus_dmamap_unload(dma_buf->dma_tag, dma_buf->dma_map);
676 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b, dma_buf->dma_map);
677 bus_dma_tag_destroy(dma_buf->dma_tag);
681 qls_alloc_parent_dma_tag(qla_host_t *ha)
689 * Allocate parent DMA Tag
691 ret = bus_dma_tag_create(
692 bus_get_dma_tag(dev), /* parent */
693 1,((bus_size_t)(1ULL << 32)),/* alignment, boundary */
694 BUS_SPACE_MAXADDR, /* lowaddr */
695 BUS_SPACE_MAXADDR, /* highaddr */
696 NULL, NULL, /* filter, filterarg */
697 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
699 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
701 NULL, NULL, /* lockfunc, lockarg */
705 device_printf(dev, "%s: could not create parent dma tag\n",
710 ha->flags.parent_tag = 1;
716 qls_free_parent_dma_tag(qla_host_t *ha)
718 if (ha->flags.parent_tag) {
719 bus_dma_tag_destroy(ha->parent_tag);
720 ha->flags.parent_tag = 0;
725 * Name: qls_init_ifnet
726 * Function: Creates the Network Device Interface and Registers it with the O.S
730 qls_init_ifnet(device_t dev, qla_host_t *ha)
734 QL_DPRINT2((dev, "%s: enter\n", __func__));
736 ifp = ha->ifp = if_alloc(IFT_ETHER);
739 panic("%s: cannot if_alloc()\n", device_get_nameunit(dev));
741 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
742 ifp->if_baudrate = IF_Gbps(10);
743 ifp->if_init = qls_init;
745 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
746 ifp->if_ioctl = qls_ioctl;
747 ifp->if_start = qls_start;
749 IFQ_SET_MAXLEN(&ifp->if_snd, qls_get_ifq_snd_maxlen(ha));
750 ifp->if_snd.ifq_drv_maxlen = qls_get_ifq_snd_maxlen(ha);
751 IFQ_SET_READY(&ifp->if_snd);
753 ha->max_frame_size = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
754 if (ha->max_frame_size <= MCLBYTES) {
755 ha->msize = MCLBYTES;
756 } else if (ha->max_frame_size <= MJUMPAGESIZE) {
757 ha->msize = MJUMPAGESIZE;
759 ha->msize = MJUM9BYTES;
761 ether_ifattach(ifp, qls_get_mac_addr(ha));
763 ifp->if_capabilities = IFCAP_JUMBO_MTU;
765 ifp->if_capabilities |= IFCAP_HWCSUM;
766 ifp->if_capabilities |= IFCAP_VLAN_MTU;
768 ifp->if_capabilities |= IFCAP_TSO4;
769 ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING;
770 ifp->if_capabilities |= IFCAP_VLAN_HWTSO;
771 ifp->if_capabilities |= IFCAP_LINKSTATE;
773 ifp->if_capenable = ifp->if_capabilities;
775 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
777 ifmedia_init(&ha->media, IFM_IMASK, qls_media_change, qls_media_status);
779 ifmedia_add(&ha->media, (IFM_ETHER | qls_get_optics(ha) | IFM_FDX), 0,
781 ifmedia_add(&ha->media, (IFM_ETHER | IFM_AUTO), 0, NULL);
783 ifmedia_set(&ha->media, (IFM_ETHER | IFM_AUTO));
785 QL_DPRINT2((dev, "%s: exit\n", __func__));
791 qls_init_locked(qla_host_t *ha)
793 struct ifnet *ifp = ha->ifp;
797 qls_flush_xmt_bufs(ha);
799 if (qls_alloc_rcv_bufs(ha) != 0)
802 if (qls_config_lro(ha))
805 bcopy(IF_LLADDR(ha->ifp), ha->mac_addr, ETHER_ADDR_LEN);
807 ifp->if_hwassist = CSUM_IP;
808 ifp->if_hwassist |= CSUM_TCP;
809 ifp->if_hwassist |= CSUM_UDP;
810 ifp->if_hwassist |= CSUM_TSO;
812 if (qls_init_hw_if(ha) == 0) {
814 ifp->if_drv_flags |= IFF_DRV_RUNNING;
815 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
816 ha->flags.qla_watchdog_pause = 0;
827 ha = (qla_host_t *)arg;
829 QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
831 (void)QLA_LOCK(ha, __func__, 0);
833 QLA_UNLOCK(ha, __func__);
835 QL_DPRINT2((ha->pci_dev, "%s: exit\n", __func__));
839 qls_set_multi(qla_host_t *ha, uint32_t add_multi)
841 uint8_t mta[Q8_MAX_NUM_MULTICAST_ADDRS * Q8_MAC_ADDR_LEN];
842 struct ifmultiaddr *ifma;
844 struct ifnet *ifp = ha->ifp;
848 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
850 if (ifma->ifma_addr->sa_family != AF_LINK)
853 if (mcnt == Q8_MAX_NUM_MULTICAST_ADDRS)
856 bcopy(LLADDR((struct sockaddr_dl *) ifma->ifma_addr),
857 &mta[mcnt * Q8_MAC_ADDR_LEN], Q8_MAC_ADDR_LEN);
862 if_maddr_runlock(ifp);
864 if (QLA_LOCK(ha, __func__, 1) == 0) {
865 qls_hw_set_multi(ha, mta, mcnt, add_multi);
866 QLA_UNLOCK(ha, __func__);
873 qls_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
876 struct ifreq *ifr = (struct ifreq *)data;
877 struct ifaddr *ifa = (struct ifaddr *)data;
880 ha = (qla_host_t *)ifp->if_softc;
884 QL_DPRINT4((ha->pci_dev, "%s: SIOCSIFADDR (0x%lx)\n",
887 if (ifa->ifa_addr->sa_family == AF_INET) {
888 ifp->if_flags |= IFF_UP;
889 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
890 (void)QLA_LOCK(ha, __func__, 0);
892 QLA_UNLOCK(ha, __func__);
894 QL_DPRINT4((ha->pci_dev,
895 "%s: SIOCSIFADDR (0x%lx) ipv4 [0x%08x]\n",
897 ntohl(IA_SIN(ifa)->sin_addr.s_addr)));
899 arp_ifinit(ifp, ifa);
901 ether_ioctl(ifp, cmd, data);
906 QL_DPRINT4((ha->pci_dev, "%s: SIOCSIFMTU (0x%lx)\n",
909 if (ifr->ifr_mtu > QLA_MAX_MTU) {
912 (void) QLA_LOCK(ha, __func__, 0);
914 ifp->if_mtu = ifr->ifr_mtu;
916 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
918 QLA_UNLOCK(ha, __func__);
927 QL_DPRINT4((ha->pci_dev, "%s: SIOCSIFFLAGS (0x%lx)\n",
930 (void)QLA_LOCK(ha, __func__, 0);
932 if (ifp->if_flags & IFF_UP) {
933 if ((ifp->if_drv_flags & IFF_DRV_RUNNING)) {
934 if ((ifp->if_flags ^ ha->if_flags) &
936 ret = qls_set_promisc(ha);
937 } else if ((ifp->if_flags ^ ha->if_flags) &
939 ret = qls_set_allmulti(ha);
942 ha->max_frame_size = ifp->if_mtu +
943 ETHER_HDR_LEN + ETHER_CRC_LEN;
947 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
949 ha->if_flags = ifp->if_flags;
952 QLA_UNLOCK(ha, __func__);
956 QL_DPRINT4((ha->pci_dev,
957 "%s: %s (0x%lx)\n", __func__, "SIOCADDMULTI", cmd));
959 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
960 qls_set_multi(ha, 1);
965 QL_DPRINT4((ha->pci_dev,
966 "%s: %s (0x%lx)\n", __func__, "SIOCDELMULTI", cmd));
968 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
969 qls_set_multi(ha, 0);
975 QL_DPRINT4((ha->pci_dev,
976 "%s: SIOCSIFMEDIA/SIOCGIFMEDIA (0x%lx)\n",
978 ret = ifmedia_ioctl(ifp, ifr, &ha->media, cmd);
983 int mask = ifr->ifr_reqcap ^ ifp->if_capenable;
985 QL_DPRINT4((ha->pci_dev, "%s: SIOCSIFCAP (0x%lx)\n",
988 if (mask & IFCAP_HWCSUM)
989 ifp->if_capenable ^= IFCAP_HWCSUM;
990 if (mask & IFCAP_TSO4)
991 ifp->if_capenable ^= IFCAP_TSO4;
992 if (mask & IFCAP_VLAN_HWTAGGING)
993 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
994 if (mask & IFCAP_VLAN_HWTSO)
995 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
997 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1000 VLAN_CAPABILITIES(ifp);
1005 QL_DPRINT4((ha->pci_dev, "%s: default (0x%lx)\n",
1007 ret = ether_ioctl(ifp, cmd, data);
1015 qls_media_change(struct ifnet *ifp)
1018 struct ifmedia *ifm;
1021 ha = (qla_host_t *)ifp->if_softc;
1023 QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
1027 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1030 QL_DPRINT2((ha->pci_dev, "%s: exit\n", __func__));
1036 qls_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1040 ha = (qla_host_t *)ifp->if_softc;
1042 QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
1044 ifmr->ifm_status = IFM_AVALID;
1045 ifmr->ifm_active = IFM_ETHER;
1047 qls_update_link_state(ha);
1049 ifmr->ifm_status |= IFM_ACTIVE;
1050 ifmr->ifm_active |= (IFM_FDX | qls_get_optics(ha));
1053 QL_DPRINT2((ha->pci_dev, "%s: exit (%s)\n", __func__,\
1054 (ha->link_up ? "link_up" : "link_down")));
1060 qls_start(struct ifnet *ifp)
1063 struct mbuf *m_head;
1064 qla_host_t *ha = (qla_host_t *)ifp->if_softc;
1066 QL_DPRINT8((ha->pci_dev, "%s: enter\n", __func__));
1068 if (!mtx_trylock(&ha->tx_lock)) {
1069 QL_DPRINT8((ha->pci_dev,
1070 "%s: mtx_trylock(&ha->tx_lock) failed\n", __func__));
1074 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) ==
1077 for (i = 0; i < ha->num_tx_rings; i++) {
1078 ret |= qls_hw_tx_done(ha, i);
1082 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1085 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1087 QL_DPRINT8((ha->pci_dev, "%s: !IFF_DRV_RUNNING\n", __func__));
1093 qls_update_link_state(ha);
1095 QL_DPRINT8((ha->pci_dev, "%s: link down\n", __func__));
1101 while (ifp->if_snd.ifq_head != NULL) {
1103 IF_DEQUEUE(&ifp->if_snd, m_head);
1105 if (m_head == NULL) {
1106 QL_DPRINT8((ha->pci_dev, "%s: m_head == NULL\n",
1111 if (qls_send(ha, &m_head)) {
1114 QL_DPRINT8((ha->pci_dev, "%s: PREPEND\n", __func__));
1115 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1116 IF_PREPEND(&ifp->if_snd, m_head);
1119 /* Send a copy of the frame to the BPF listener */
1120 ETHER_BPF_MTAP(ifp, m_head);
1124 QL_DPRINT8((ha->pci_dev, "%s: exit\n", __func__));
1129 qls_send(qla_host_t *ha, struct mbuf **m_headp)
1131 bus_dma_segment_t segs[QLA_MAX_SEGMENTS];
1136 struct mbuf *m_head = *m_headp;
1137 uint32_t txr_idx = 0;
1139 QL_DPRINT8((ha->pci_dev, "%s: enter\n", __func__));
1141 /* check if flowid is set */
1142 if (M_HASHTYPE_GET(m_head) != M_HASHTYPE_NONE)
1143 txr_idx = m_head->m_pkthdr.flowid & (ha->num_tx_rings - 1);
1145 tx_idx = ha->tx_ring[txr_idx].txr_next;
1147 map = ha->tx_ring[txr_idx].tx_buf[tx_idx].map;
1149 ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head, segs, &nsegs,
1156 QL_DPRINT8((ha->pci_dev, "%s: EFBIG [%d]\n", __func__,
1157 m_head->m_pkthdr.len));
1159 m = m_defrag(m_head, M_NOWAIT);
1161 ha->err_tx_defrag++;
1164 device_printf(ha->pci_dev,
1165 "%s: m_defrag() = NULL [%d]\n",
1172 if ((ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head,
1173 segs, &nsegs, BUS_DMA_NOWAIT))) {
1175 ha->err_tx_dmamap_load++;
1177 device_printf(ha->pci_dev,
1178 "%s: bus_dmamap_load_mbuf_sg failed0[%d, %d]\n",
1179 __func__, ret, m_head->m_pkthdr.len);
1181 if (ret != ENOMEM) {
1190 ha->err_tx_dmamap_load++;
1192 device_printf(ha->pci_dev,
1193 "%s: bus_dmamap_load_mbuf_sg failed1[%d, %d]\n",
1194 __func__, ret, m_head->m_pkthdr.len);
1196 if (ret != ENOMEM) {
1203 QL_ASSERT(ha, (nsegs != 0), ("qls_send: empty packet"));
1205 bus_dmamap_sync(ha->tx_tag, map, BUS_DMASYNC_PREWRITE);
1207 if (!(ret = qls_hw_send(ha, segs, nsegs, tx_idx, m_head, txr_idx))) {
1209 ha->tx_ring[txr_idx].count++;
1210 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head = m_head;
1211 ha->tx_ring[txr_idx].tx_buf[tx_idx].map = map;
1213 if (ret == EINVAL) {
1220 QL_DPRINT8((ha->pci_dev, "%s: exit\n", __func__));
1225 qls_stop(qla_host_t *ha)
1227 struct ifnet *ifp = ha->ifp;
1232 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE | IFF_DRV_RUNNING);
1234 ha->flags.qla_watchdog_pause = 1;
1236 while (!ha->qla_watchdog_paused)
1237 qls_mdelay(__func__, 1);
1243 qls_flush_xmt_bufs(ha);
1244 qls_free_rcv_bufs(ha);
1250 * Buffer Management Functions for Transmit and Receive Rings
1253 * Release mbuf after it sent on the wire
1256 qls_flush_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb)
1258 QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
1262 bus_dmamap_unload(ha->tx_tag, txb->map);
1264 m_freem(txb->m_head);
1268 QL_DPRINT2((ha->pci_dev, "%s: exit\n", __func__));
1272 qls_flush_xmt_bufs(qla_host_t *ha)
1276 for (j = 0; j < ha->num_tx_rings; j++) {
1277 for (i = 0; i < NUM_TX_DESCRIPTORS; i++)
1278 qls_flush_tx_buf(ha, &ha->tx_ring[j].tx_buf[i]);
1286 qls_alloc_rcv_mbufs(qla_host_t *ha, int r)
1290 qla_rx_ring_t *rx_ring;
1291 volatile q81_bq_addr_e_t *sbq_e;
1294 rx_ring = &ha->rx_ring[r];
1296 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1298 rxb = &rx_ring->rx_buf[i];
1300 ret = bus_dmamap_create(ha->rx_tag, BUS_DMA_NOWAIT, &rxb->map);
1303 device_printf(ha->pci_dev,
1304 "%s: dmamap[%d, %d] failed\n", __func__, r, i);
1306 for (j = 0; j < i; j++) {
1307 rxb = &rx_ring->rx_buf[j];
1308 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1310 goto qls_alloc_rcv_mbufs_err;
1314 rx_ring = &ha->rx_ring[r];
1316 sbq_e = rx_ring->sbq_vaddr;
1318 rxb = &rx_ring->rx_buf[0];
1320 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1322 if (!(ret = qls_get_mbuf(ha, rxb, NULL))) {
1325 * set the physical address in the
1326 * corresponding descriptor entry in the
1327 * receive ring/queue for the hba
1330 sbq_e->addr_lo = rxb->paddr & 0xFFFFFFFF;
1331 sbq_e->addr_hi = (rxb->paddr >> 32) & 0xFFFFFFFF;
1334 device_printf(ha->pci_dev,
1335 "%s: qls_get_mbuf [%d, %d] failed\n",
1337 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1338 goto qls_alloc_rcv_mbufs_err;
1346 qls_alloc_rcv_mbufs_err:
1351 qls_free_rcv_bufs(qla_host_t *ha)
1357 for (r = 0; r < ha->num_rx_rings; r++) {
1359 rxr = &ha->rx_ring[r];
1361 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1363 rxb = &rxr->rx_buf[i];
1365 if (rxb->m_head != NULL) {
1366 bus_dmamap_unload(ha->rx_tag, rxb->map);
1367 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1368 m_freem(rxb->m_head);
1371 bzero(rxr->rx_buf, (sizeof(qla_rx_buf_t) * NUM_RX_DESCRIPTORS));
1377 qls_alloc_rcv_bufs(qla_host_t *ha)
1382 for (r = 0; r < ha->num_rx_rings; r++) {
1383 rxr = &ha->rx_ring[r];
1384 bzero(rxr->rx_buf, (sizeof(qla_rx_buf_t) * NUM_RX_DESCRIPTORS));
1387 for (r = 0; r < ha->num_rx_rings; r++) {
1389 ret = qls_alloc_rcv_mbufs(ha, r);
1392 qls_free_rcv_bufs(ha);
1399 qls_get_mbuf(qla_host_t *ha, qla_rx_buf_t *rxb, struct mbuf *nmp)
1401 struct mbuf *mp = nmp;
1405 bus_dma_segment_t segs[1];
1408 QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
1414 mp = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, ha->msize);
1418 if (ha->msize == MCLBYTES)
1424 device_printf(ha->pci_dev,
1425 "%s: m_getcl failed\n", __func__);
1426 goto exit_qls_get_mbuf;
1428 mp->m_len = mp->m_pkthdr.len = ha->msize;
1430 mp->m_len = mp->m_pkthdr.len = ha->msize;
1431 mp->m_data = mp->m_ext.ext_buf;
1435 /* align the receive buffers to 8 byte boundary */
1436 offset = (uint32_t)((unsigned long long)mp->m_data & 0x7ULL);
1438 offset = 8 - offset;
1443 * Using memory from the mbuf cluster pool, invoke the bus_dma
1444 * machinery to arrange the memory mapping.
1446 ret = bus_dmamap_load_mbuf_sg(ha->rx_tag, rxb->map,
1447 mp, segs, &nsegs, BUS_DMA_NOWAIT);
1448 rxb->paddr = segs[0].ds_addr;
1450 if (ret || !rxb->paddr || (nsegs != 1)) {
1453 device_printf(ha->pci_dev,
1454 "%s: bus_dmamap_load failed[%d, 0x%016llx, %d]\n",
1455 __func__, ret, (long long unsigned int)rxb->paddr,
1458 goto exit_qls_get_mbuf;
1461 bus_dmamap_sync(ha->rx_tag, rxb->map, BUS_DMASYNC_PREREAD);
1464 QL_DPRINT2((ha->pci_dev, "%s: exit ret = 0x%08x\n", __func__, ret));
1469 qls_tx_done(void *context, int pending)
1471 qla_host_t *ha = context;
1479 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1480 QL_DPRINT8((ha->pci_dev, "%s: !IFF_DRV_RUNNING\n", __func__));
1489 qls_config_lro(qla_host_t *ha)
1492 struct lro_ctrl *lro;
1494 for (i = 0; i < ha->num_rx_rings; i++) {
1495 lro = &ha->rx_ring[i].lro;
1496 if (tcp_lro_init(lro)) {
1497 device_printf(ha->pci_dev, "%s: tcp_lro_init failed\n",
1503 ha->flags.lro_init = 1;
1505 QL_DPRINT2((ha->pci_dev, "%s: LRO initialized\n", __func__));
1510 qls_free_lro(qla_host_t *ha)
1513 struct lro_ctrl *lro;
1515 if (!ha->flags.lro_init)
1518 for (i = 0; i < ha->num_rx_rings; i++) {
1519 lro = &ha->rx_ring[i].lro;
1522 ha->flags.lro_init = 0;
1526 qls_error_recovery(void *context, int pending)
1528 qla_host_t *ha = context;