2 * Copyright (c) 2013-2014 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 "qls_inline.h"
47 * Some PCI Configuration Space Related Defines
50 #ifndef PCI_VENDOR_QLOGIC
51 #define PCI_VENDOR_QLOGIC 0x1077
54 #ifndef PCI_DEVICE_QLOGIC_8000
55 #define PCI_DEVICE_QLOGIC_8000 0x8000
58 #define PCI_QLOGIC_DEV8000 \
59 ((PCI_DEVICE_QLOGIC_8000 << 16) | PCI_VENDOR_QLOGIC)
64 static int qls_alloc_parent_dma_tag(qla_host_t *ha);
65 static void qls_free_parent_dma_tag(qla_host_t *ha);
67 static void qls_flush_xmt_bufs(qla_host_t *ha);
69 static int qls_alloc_rcv_bufs(qla_host_t *ha);
70 static void qls_free_rcv_bufs(qla_host_t *ha);
72 static void qls_init_ifnet(device_t dev, qla_host_t *ha);
73 static void qls_release(qla_host_t *ha);
74 static void qls_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs,
76 static void qls_stop(qla_host_t *ha);
77 static int qls_send(qla_host_t *ha, struct mbuf **m_headp);
78 static void qls_tx_done(void *context, int pending);
80 static int qls_config_lro(qla_host_t *ha);
81 static void qls_free_lro(qla_host_t *ha);
83 static void qls_error_recovery(void *context, int pending);
86 * Hooks to the Operating Systems
88 static int qls_pci_probe (device_t);
89 static int qls_pci_attach (device_t);
90 static int qls_pci_detach (device_t);
92 static void qls_start(struct ifnet *ifp);
93 static void qls_init(void *arg);
94 static int qls_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
95 static int qls_media_change(struct ifnet *ifp);
96 static void qls_media_status(struct ifnet *ifp, struct ifmediareq *ifmr);
98 static device_method_t qla_pci_methods[] = {
99 /* Device interface */
100 DEVMETHOD(device_probe, qls_pci_probe),
101 DEVMETHOD(device_attach, qls_pci_attach),
102 DEVMETHOD(device_detach, qls_pci_detach),
106 static driver_t qla_pci_driver = {
107 "ql", qla_pci_methods, sizeof (qla_host_t),
110 static devclass_t qla8000_devclass;
112 DRIVER_MODULE(qla8000, pci, qla_pci_driver, qla8000_devclass, 0, 0);
114 MODULE_DEPEND(qla8000, pci, 1, 1, 1);
115 MODULE_DEPEND(qla8000, ether, 1, 1, 1);
117 MALLOC_DEFINE(M_QLA8000BUF, "qla8000buf", "Buffers for qla8000 driver");
119 static char dev_str[64];
120 static char ver_str[64];
123 * Name: qls_pci_probe
124 * Function: Validate the PCI device to be a QLA80XX device
127 qls_pci_probe(device_t dev)
129 switch ((pci_get_device(dev) << 16) | (pci_get_vendor(dev))) {
130 case PCI_QLOGIC_DEV8000:
131 snprintf(dev_str, sizeof(dev_str), "%s v%d.%d.%d",
132 "Qlogic ISP 8000 PCI CNA Adapter-Ethernet Function",
133 QLA_VERSION_MAJOR, QLA_VERSION_MINOR,
135 snprintf(ver_str, sizeof(ver_str), "v%d.%d.%d",
136 QLA_VERSION_MAJOR, QLA_VERSION_MINOR,
138 device_set_desc(dev, dev_str);
145 printf("%s: %s\n ", __func__, dev_str);
147 return (BUS_PROBE_DEFAULT);
151 qls_sysctl_get_drvr_stats(SYSCTL_HANDLER_ARGS)
157 err = sysctl_handle_int(oidp, &ret, 0, req);
159 if (err || !req->newptr)
164 ha = (qla_host_t *)arg1;
166 for (i = 0; i < ha->num_tx_rings; i++) {
168 device_printf(ha->pci_dev,
169 "%s: tx_ring[%d].tx_frames= %p\n",
171 (void *)ha->tx_ring[i].tx_frames);
173 device_printf(ha->pci_dev,
174 "%s: tx_ring[%d].tx_tso_frames= %p\n",
176 (void *)ha->tx_ring[i].tx_tso_frames);
178 device_printf(ha->pci_dev,
179 "%s: tx_ring[%d].tx_vlan_frames= %p\n",
181 (void *)ha->tx_ring[i].tx_vlan_frames);
183 device_printf(ha->pci_dev,
184 "%s: tx_ring[%d].txr_free= 0x%08x\n",
186 ha->tx_ring[i].txr_free);
188 device_printf(ha->pci_dev,
189 "%s: tx_ring[%d].txr_next= 0x%08x\n",
191 ha->tx_ring[i].txr_next);
193 device_printf(ha->pci_dev,
194 "%s: tx_ring[%d].txr_done= 0x%08x\n",
196 ha->tx_ring[i].txr_done);
198 device_printf(ha->pci_dev,
199 "%s: tx_ring[%d].txr_cons_idx= 0x%08x\n",
201 *(ha->tx_ring[i].txr_cons_vaddr));
204 for (i = 0; i < ha->num_rx_rings; i++) {
206 device_printf(ha->pci_dev,
207 "%s: rx_ring[%d].rx_int= %p\n",
209 (void *)ha->rx_ring[i].rx_int);
211 device_printf(ha->pci_dev,
212 "%s: rx_ring[%d].rss_int= %p\n",
214 (void *)ha->rx_ring[i].rss_int);
216 device_printf(ha->pci_dev,
217 "%s: rx_ring[%d].lbq_next= 0x%08x\n",
219 ha->rx_ring[i].lbq_next);
221 device_printf(ha->pci_dev,
222 "%s: rx_ring[%d].lbq_free= 0x%08x\n",
224 ha->rx_ring[i].lbq_free);
226 device_printf(ha->pci_dev,
227 "%s: rx_ring[%d].lbq_in= 0x%08x\n",
229 ha->rx_ring[i].lbq_in);
231 device_printf(ha->pci_dev,
232 "%s: rx_ring[%d].sbq_next= 0x%08x\n",
234 ha->rx_ring[i].sbq_next);
236 device_printf(ha->pci_dev,
237 "%s: rx_ring[%d].sbq_free= 0x%08x\n",
239 ha->rx_ring[i].sbq_free);
241 device_printf(ha->pci_dev,
242 "%s: rx_ring[%d].sbq_in= 0x%08x\n",
244 ha->rx_ring[i].sbq_in);
247 device_printf(ha->pci_dev, "%s: err_m_getcl = 0x%08x\n",
248 __func__, ha->err_m_getcl);
249 device_printf(ha->pci_dev, "%s: err_m_getjcl = 0x%08x\n",
250 __func__, ha->err_m_getjcl);
251 device_printf(ha->pci_dev,
252 "%s: err_tx_dmamap_create = 0x%08x\n",
253 __func__, ha->err_tx_dmamap_create);
254 device_printf(ha->pci_dev,
255 "%s: err_tx_dmamap_load = 0x%08x\n",
256 __func__, ha->err_tx_dmamap_load);
257 device_printf(ha->pci_dev,
258 "%s: err_tx_defrag = 0x%08x\n",
259 __func__, ha->err_tx_defrag);
265 qls_add_sysctls(qla_host_t *ha)
267 device_t dev = ha->pci_dev;
269 SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
270 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
271 OID_AUTO, "version", CTLFLAG_RD,
272 ver_str, 0, "Driver Version");
275 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
276 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
277 OID_AUTO, "debug", CTLFLAG_RW,
278 &qls_dbg_level, qls_dbg_level, "Debug Level");
280 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
281 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
282 OID_AUTO, "drvr_stats", CTLTYPE_INT | CTLFLAG_RW,
284 qls_sysctl_get_drvr_stats, "I", "Driver Maintained Statistics");
290 qls_watchdog(void *arg)
292 qla_host_t *ha = arg;
297 if (ha->flags.qla_watchdog_exit) {
298 ha->qla_watchdog_exited = 1;
301 ha->qla_watchdog_exited = 0;
303 if (!ha->flags.qla_watchdog_pause) {
305 if (ha->qla_initiate_recovery) {
307 ha->qla_watchdog_paused = 1;
308 ha->qla_initiate_recovery = 0;
310 taskqueue_enqueue(ha->err_tq, &ha->err_task);
312 } else if ((ifp->if_snd.ifq_head != NULL) && QL_RUNNING(ifp)) {
314 taskqueue_enqueue(ha->tx_tq, &ha->tx_task);
317 ha->qla_watchdog_paused = 0;
319 ha->qla_watchdog_paused = 1;
322 ha->watchdog_ticks = ha->watchdog_ticks++ % 1000;
323 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
330 * Name: qls_pci_attach
331 * Function: attaches the device to the operating system
334 qls_pci_attach(device_t dev)
336 qla_host_t *ha = NULL;
339 QL_DPRINT2((dev, "%s: enter\n", __func__));
341 if ((ha = device_get_softc(dev)) == NULL) {
342 device_printf(dev, "cannot get softc\n");
346 memset(ha, 0, sizeof (qla_host_t));
348 if (pci_get_device(dev) != PCI_DEVICE_QLOGIC_8000) {
349 device_printf(dev, "device is not QLE8000\n");
353 ha->pci_func = pci_get_function(dev);
357 pci_enable_busmaster(dev);
359 ha->reg_rid = PCIR_BAR(1);
360 ha->pci_reg = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &ha->reg_rid,
363 if (ha->pci_reg == NULL) {
364 device_printf(dev, "unable to map any ports\n");
365 goto qls_pci_attach_err;
368 ha->reg_rid1 = PCIR_BAR(3);
369 ha->pci_reg1 = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
370 &ha->reg_rid1, RF_ACTIVE);
372 if (ha->pci_reg1 == NULL) {
373 device_printf(dev, "unable to map any ports\n");
374 goto qls_pci_attach_err;
377 mtx_init(&ha->hw_lock, "qla80xx_hw_lock", MTX_NETWORK_LOCK, MTX_DEF);
378 mtx_init(&ha->tx_lock, "qla80xx_tx_lock", MTX_NETWORK_LOCK, MTX_DEF);
381 qls_hw_add_sysctls(ha);
383 ha->flags.lock_init = 1;
385 ha->msix_count = pci_msix_count(dev);
387 if (ha->msix_count < qls_get_msix_count(ha)) {
388 device_printf(dev, "%s: msix_count[%d] not enough\n", __func__,
390 goto qls_pci_attach_err;
393 ha->msix_count = qls_get_msix_count(ha);
395 device_printf(dev, "\n%s: ha %p pci_func 0x%x msix_count 0x%x"
396 " pci_reg %p pci_reg1 %p\n", __func__, ha,
397 ha->pci_func, ha->msix_count, ha->pci_reg, ha->pci_reg1);
399 if (pci_alloc_msix(dev, &ha->msix_count)) {
400 device_printf(dev, "%s: pci_alloc_msi[%d] failed\n", __func__,
403 goto qls_pci_attach_err;
406 for (i = 0; i < ha->num_rx_rings; i++) {
407 ha->irq_vec[i].cq_idx = i;
408 ha->irq_vec[i].ha = ha;
409 ha->irq_vec[i].irq_rid = 1 + i;
411 ha->irq_vec[i].irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
412 &ha->irq_vec[i].irq_rid,
413 (RF_ACTIVE | RF_SHAREABLE));
415 if (ha->irq_vec[i].irq == NULL) {
416 device_printf(dev, "could not allocate interrupt\n");
417 goto qls_pci_attach_err;
420 if (bus_setup_intr(dev, ha->irq_vec[i].irq,
421 (INTR_TYPE_NET | INTR_MPSAFE), NULL, qls_isr,
422 &ha->irq_vec[i], &ha->irq_vec[i].handle)) {
424 "could not setup interrupt\n");
425 goto qls_pci_attach_err;
429 qls_rd_nic_params(ha);
431 /* allocate parent dma tag */
432 if (qls_alloc_parent_dma_tag(ha)) {
433 device_printf(dev, "%s: qls_alloc_parent_dma_tag failed\n",
435 goto qls_pci_attach_err;
438 /* alloc all dma buffers */
439 if (qls_alloc_dma(ha)) {
440 device_printf(dev, "%s: qls_alloc_dma failed\n", __func__);
441 goto qls_pci_attach_err;
444 /* create the o.s ethernet interface */
445 qls_init_ifnet(dev, ha);
447 ha->flags.qla_watchdog_active = 1;
448 ha->flags.qla_watchdog_pause = 1;
450 TASK_INIT(&ha->tx_task, 0, qls_tx_done, ha);
451 ha->tx_tq = taskqueue_create_fast("qla_txq", M_NOWAIT,
452 taskqueue_thread_enqueue, &ha->tx_tq);
453 taskqueue_start_threads(&ha->tx_tq, 1, PI_NET, "%s txq",
454 device_get_nameunit(ha->pci_dev));
456 callout_init(&ha->tx_callout, TRUE);
457 ha->flags.qla_callout_init = 1;
459 /* create ioctl device interface */
460 if (qls_make_cdev(ha)) {
461 device_printf(dev, "%s: qls_make_cdev failed\n", __func__);
462 goto qls_pci_attach_err;
465 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
468 TASK_INIT(&ha->err_task, 0, qls_error_recovery, ha);
469 ha->err_tq = taskqueue_create_fast("qla_errq", M_NOWAIT,
470 taskqueue_thread_enqueue, &ha->err_tq);
471 taskqueue_start_threads(&ha->err_tq, 1, PI_NET, "%s errq",
472 device_get_nameunit(ha->pci_dev));
474 QL_DPRINT2((dev, "%s: exit 0\n", __func__));
481 QL_DPRINT2((dev, "%s: exit ENXIO\n", __func__));
486 * Name: qls_pci_detach
487 * Function: Unhooks the device from the operating system
490 qls_pci_detach(device_t dev)
492 qla_host_t *ha = NULL;
495 QL_DPRINT2((dev, "%s: enter\n", __func__));
497 if ((ha = device_get_softc(dev)) == NULL) {
498 device_printf(dev, "cannot get softc\n");
504 (void)QLA_LOCK(ha, __func__, 0);
506 QLA_UNLOCK(ha, __func__);
510 QL_DPRINT2((dev, "%s: exit\n", __func__));
517 * Function: Releases the resources allocated for the device
520 qls_release(qla_host_t *ha)
528 taskqueue_drain(ha->err_tq, &ha->err_task);
529 taskqueue_free(ha->err_tq);
533 taskqueue_drain(ha->tx_tq, &ha->tx_task);
534 taskqueue_free(ha->tx_tq);
539 if (ha->flags.qla_watchdog_active) {
540 ha->flags.qla_watchdog_exit = 1;
542 while (ha->qla_watchdog_exited == 0)
543 qls_mdelay(__func__, 1);
546 if (ha->flags.qla_callout_init)
547 callout_stop(&ha->tx_callout);
550 ether_ifdetach(ha->ifp);
553 qls_free_parent_dma_tag(ha);
555 for (i = 0; i < ha->num_rx_rings; i++) {
557 if (ha->irq_vec[i].handle) {
558 (void)bus_teardown_intr(dev, ha->irq_vec[i].irq,
559 ha->irq_vec[i].handle);
562 if (ha->irq_vec[i].irq) {
563 (void)bus_release_resource(dev, SYS_RES_IRQ,
564 ha->irq_vec[i].irq_rid,
570 pci_release_msi(dev);
572 if (ha->flags.lock_init) {
573 mtx_destroy(&ha->tx_lock);
574 mtx_destroy(&ha->hw_lock);
578 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid,
582 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid1,
587 * DMA Related Functions
591 qls_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
593 *((bus_addr_t *)arg) = 0;
596 printf("%s: bus_dmamap_load failed (%d)\n", __func__, error);
600 *((bus_addr_t *)arg) = segs[0].ds_addr;
606 qls_alloc_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
614 QL_DPRINT2((dev, "%s: enter\n", __func__));
616 ret = bus_dma_tag_create(
617 ha->parent_tag,/* parent */
619 ((bus_size_t)(1ULL << 32)),/* boundary */
620 BUS_SPACE_MAXADDR, /* lowaddr */
621 BUS_SPACE_MAXADDR, /* highaddr */
622 NULL, NULL, /* filter, filterarg */
623 dma_buf->size, /* maxsize */
625 dma_buf->size, /* maxsegsize */
627 NULL, NULL, /* lockfunc, lockarg */
631 device_printf(dev, "%s: could not create dma tag\n", __func__);
632 goto qls_alloc_dmabuf_exit;
634 ret = bus_dmamem_alloc(dma_buf->dma_tag,
635 (void **)&dma_buf->dma_b,
636 (BUS_DMA_ZERO | BUS_DMA_COHERENT | BUS_DMA_NOWAIT),
639 bus_dma_tag_destroy(dma_buf->dma_tag);
640 device_printf(dev, "%s: bus_dmamem_alloc failed\n", __func__);
641 goto qls_alloc_dmabuf_exit;
644 ret = bus_dmamap_load(dma_buf->dma_tag,
649 &b_addr, BUS_DMA_NOWAIT);
651 if (ret || !b_addr) {
652 bus_dma_tag_destroy(dma_buf->dma_tag);
653 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b,
656 goto qls_alloc_dmabuf_exit;
659 dma_buf->dma_addr = b_addr;
661 qls_alloc_dmabuf_exit:
662 QL_DPRINT2((dev, "%s: exit ret 0x%08x tag %p map %p b %p sz 0x%x\n",
663 __func__, ret, (void *)dma_buf->dma_tag,
664 (void *)dma_buf->dma_map, (void *)dma_buf->dma_b,
671 qls_free_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
673 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b, dma_buf->dma_map);
674 bus_dma_tag_destroy(dma_buf->dma_tag);
678 qls_alloc_parent_dma_tag(qla_host_t *ha)
686 * Allocate parent DMA Tag
688 ret = bus_dma_tag_create(
689 bus_get_dma_tag(dev), /* parent */
690 1,((bus_size_t)(1ULL << 32)),/* alignment, boundary */
691 BUS_SPACE_MAXADDR, /* lowaddr */
692 BUS_SPACE_MAXADDR, /* highaddr */
693 NULL, NULL, /* filter, filterarg */
694 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
696 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
698 NULL, NULL, /* lockfunc, lockarg */
702 device_printf(dev, "%s: could not create parent dma tag\n",
707 ha->flags.parent_tag = 1;
713 qls_free_parent_dma_tag(qla_host_t *ha)
715 if (ha->flags.parent_tag) {
716 bus_dma_tag_destroy(ha->parent_tag);
717 ha->flags.parent_tag = 0;
722 * Name: qls_init_ifnet
723 * Function: Creates the Network Device Interface and Registers it with the O.S
727 qls_init_ifnet(device_t dev, qla_host_t *ha)
731 QL_DPRINT2((dev, "%s: enter\n", __func__));
733 ifp = ha->ifp = if_alloc(IFT_ETHER);
736 panic("%s: cannot if_alloc()\n", device_get_nameunit(dev));
738 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
740 #if __FreeBSD_version >= 1000000
741 if_initbaudrate(ifp, IF_Gbps(10));
743 ifp->if_baudrate = 1 * 1000 * 1000 * 1000;
744 #endif /* #if (__FreeBSD_version > 1000000) */
746 ifp->if_init = qls_init;
748 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
749 ifp->if_ioctl = qls_ioctl;
750 ifp->if_start = qls_start;
752 IFQ_SET_MAXLEN(&ifp->if_snd, qls_get_ifq_snd_maxlen(ha));
753 ifp->if_snd.ifq_drv_maxlen = qls_get_ifq_snd_maxlen(ha);
754 IFQ_SET_READY(&ifp->if_snd);
756 ha->max_frame_size = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
757 if (ha->max_frame_size <= MCLBYTES) {
758 ha->msize = MCLBYTES;
759 } else if (ha->max_frame_size <= MJUMPAGESIZE) {
760 ha->msize = MJUMPAGESIZE;
762 ha->msize = MJUM9BYTES;
764 ether_ifattach(ifp, qls_get_mac_addr(ha));
766 ifp->if_capabilities = IFCAP_JUMBO_MTU;
768 ifp->if_capabilities |= IFCAP_HWCSUM;
769 ifp->if_capabilities |= IFCAP_VLAN_MTU;
771 ifp->if_capabilities |= IFCAP_TSO4;
772 ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING;
773 ifp->if_capabilities |= IFCAP_VLAN_HWTSO;
774 ifp->if_capabilities |= IFCAP_LINKSTATE;
776 ifp->if_capenable = ifp->if_capabilities;
778 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
780 ifmedia_init(&ha->media, IFM_IMASK, qls_media_change, qls_media_status);
782 ifmedia_add(&ha->media, (IFM_ETHER | qls_get_optics(ha) | IFM_FDX), 0,
784 ifmedia_add(&ha->media, (IFM_ETHER | IFM_AUTO), 0, NULL);
786 ifmedia_set(&ha->media, (IFM_ETHER | IFM_AUTO));
788 QL_DPRINT2((dev, "%s: exit\n", __func__));
794 qls_init_locked(qla_host_t *ha)
796 struct ifnet *ifp = ha->ifp;
800 qls_flush_xmt_bufs(ha);
802 if (qls_alloc_rcv_bufs(ha) != 0)
805 if (qls_config_lro(ha))
808 bcopy(IF_LLADDR(ha->ifp), ha->mac_addr, ETHER_ADDR_LEN);
810 ifp->if_hwassist = CSUM_IP;
811 ifp->if_hwassist |= CSUM_TCP;
812 ifp->if_hwassist |= CSUM_UDP;
813 ifp->if_hwassist |= CSUM_TSO;
815 if (qls_init_hw_if(ha) == 0) {
817 ifp->if_drv_flags |= IFF_DRV_RUNNING;
818 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
819 ha->flags.qla_watchdog_pause = 0;
830 ha = (qla_host_t *)arg;
832 QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
834 (void)QLA_LOCK(ha, __func__, 0);
836 QLA_UNLOCK(ha, __func__);
838 QL_DPRINT2((ha->pci_dev, "%s: exit\n", __func__));
842 qls_set_multi(qla_host_t *ha, uint32_t add_multi)
844 uint8_t mta[Q8_MAX_NUM_MULTICAST_ADDRS * Q8_MAC_ADDR_LEN];
845 struct ifmultiaddr *ifma;
847 struct ifnet *ifp = ha->ifp;
851 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
853 if (ifma->ifma_addr->sa_family != AF_LINK)
856 if (mcnt == Q8_MAX_NUM_MULTICAST_ADDRS)
859 bcopy(LLADDR((struct sockaddr_dl *) ifma->ifma_addr),
860 &mta[mcnt * Q8_MAC_ADDR_LEN], Q8_MAC_ADDR_LEN);
865 if_maddr_runlock(ifp);
867 if (QLA_LOCK(ha, __func__, 1) == 0) {
868 qls_hw_set_multi(ha, mta, mcnt, add_multi);
869 QLA_UNLOCK(ha, __func__);
876 qls_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
879 struct ifreq *ifr = (struct ifreq *)data;
880 struct ifaddr *ifa = (struct ifaddr *)data;
883 ha = (qla_host_t *)ifp->if_softc;
887 QL_DPRINT4((ha->pci_dev, "%s: SIOCSIFADDR (0x%lx)\n",
890 if (ifa->ifa_addr->sa_family == AF_INET) {
891 ifp->if_flags |= IFF_UP;
892 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
893 (void)QLA_LOCK(ha, __func__, 0);
895 QLA_UNLOCK(ha, __func__);
897 QL_DPRINT4((ha->pci_dev,
898 "%s: SIOCSIFADDR (0x%lx) ipv4 [0x%08x]\n",
900 ntohl(IA_SIN(ifa)->sin_addr.s_addr)));
902 arp_ifinit(ifp, ifa);
904 ether_ioctl(ifp, cmd, data);
909 QL_DPRINT4((ha->pci_dev, "%s: SIOCSIFMTU (0x%lx)\n",
912 if (ifr->ifr_mtu > QLA_MAX_MTU) {
915 (void) QLA_LOCK(ha, __func__, 0);
917 ifp->if_mtu = ifr->ifr_mtu;
919 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
921 QLA_UNLOCK(ha, __func__);
930 QL_DPRINT4((ha->pci_dev, "%s: SIOCSIFFLAGS (0x%lx)\n",
933 (void)QLA_LOCK(ha, __func__, 0);
935 if (ifp->if_flags & IFF_UP) {
936 if ((ifp->if_drv_flags & IFF_DRV_RUNNING)) {
937 if ((ifp->if_flags ^ ha->if_flags) &
939 ret = qls_set_promisc(ha);
940 } else if ((ifp->if_flags ^ ha->if_flags) &
942 ret = qls_set_allmulti(ha);
945 ha->max_frame_size = ifp->if_mtu +
946 ETHER_HDR_LEN + ETHER_CRC_LEN;
950 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
952 ha->if_flags = ifp->if_flags;
955 QLA_UNLOCK(ha, __func__);
959 QL_DPRINT4((ha->pci_dev,
960 "%s: %s (0x%lx)\n", __func__, "SIOCADDMULTI", cmd));
962 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
963 qls_set_multi(ha, 1);
968 QL_DPRINT4((ha->pci_dev,
969 "%s: %s (0x%lx)\n", __func__, "SIOCDELMULTI", cmd));
971 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
972 qls_set_multi(ha, 0);
978 QL_DPRINT4((ha->pci_dev,
979 "%s: SIOCSIFMEDIA/SIOCGIFMEDIA (0x%lx)\n",
981 ret = ifmedia_ioctl(ifp, ifr, &ha->media, cmd);
986 int mask = ifr->ifr_reqcap ^ ifp->if_capenable;
988 QL_DPRINT4((ha->pci_dev, "%s: SIOCSIFCAP (0x%lx)\n",
991 if (mask & IFCAP_HWCSUM)
992 ifp->if_capenable ^= IFCAP_HWCSUM;
993 if (mask & IFCAP_TSO4)
994 ifp->if_capenable ^= IFCAP_TSO4;
995 if (mask & IFCAP_VLAN_HWTAGGING)
996 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
997 if (mask & IFCAP_VLAN_HWTSO)
998 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1000 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1003 VLAN_CAPABILITIES(ifp);
1008 QL_DPRINT4((ha->pci_dev, "%s: default (0x%lx)\n",
1010 ret = ether_ioctl(ifp, cmd, data);
1018 qls_media_change(struct ifnet *ifp)
1021 struct ifmedia *ifm;
1024 ha = (qla_host_t *)ifp->if_softc;
1026 QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
1030 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1033 QL_DPRINT2((ha->pci_dev, "%s: exit\n", __func__));
1039 qls_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1043 ha = (qla_host_t *)ifp->if_softc;
1045 QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
1047 ifmr->ifm_status = IFM_AVALID;
1048 ifmr->ifm_active = IFM_ETHER;
1050 qls_update_link_state(ha);
1052 ifmr->ifm_status |= IFM_ACTIVE;
1053 ifmr->ifm_active |= (IFM_FDX | qls_get_optics(ha));
1056 QL_DPRINT2((ha->pci_dev, "%s: exit (%s)\n", __func__,\
1057 (ha->link_up ? "link_up" : "link_down")));
1063 qls_start(struct ifnet *ifp)
1066 struct mbuf *m_head;
1067 qla_host_t *ha = (qla_host_t *)ifp->if_softc;
1069 QL_DPRINT8((ha->pci_dev, "%s: enter\n", __func__));
1071 if (!mtx_trylock(&ha->tx_lock)) {
1072 QL_DPRINT8((ha->pci_dev,
1073 "%s: mtx_trylock(&ha->tx_lock) failed\n", __func__));
1077 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) ==
1080 for (i = 0; i < ha->num_tx_rings; i++) {
1081 ret |= qls_hw_tx_done(ha, i);
1085 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1088 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1090 QL_DPRINT8((ha->pci_dev, "%s: !IFF_DRV_RUNNING\n", __func__));
1096 qls_update_link_state(ha);
1098 QL_DPRINT8((ha->pci_dev, "%s: link down\n", __func__));
1104 while (ifp->if_snd.ifq_head != NULL) {
1106 IF_DEQUEUE(&ifp->if_snd, m_head);
1108 if (m_head == NULL) {
1109 QL_DPRINT8((ha->pci_dev, "%s: m_head == NULL\n",
1114 if (qls_send(ha, &m_head)) {
1117 QL_DPRINT8((ha->pci_dev, "%s: PREPEND\n", __func__));
1118 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1119 IF_PREPEND(&ifp->if_snd, m_head);
1122 /* Send a copy of the frame to the BPF listener */
1123 ETHER_BPF_MTAP(ifp, m_head);
1127 QL_DPRINT8((ha->pci_dev, "%s: exit\n", __func__));
1132 qls_send(qla_host_t *ha, struct mbuf **m_headp)
1134 bus_dma_segment_t segs[QLA_MAX_SEGMENTS];
1139 struct mbuf *m_head = *m_headp;
1140 uint32_t txr_idx = 0;
1142 QL_DPRINT8((ha->pci_dev, "%s: enter\n", __func__));
1144 if (m_head->m_flags & M_FLOWID)
1145 txr_idx = m_head->m_pkthdr.flowid & (ha->num_tx_rings - 1);
1147 tx_idx = ha->tx_ring[txr_idx].txr_next;
1149 map = ha->tx_ring[txr_idx].tx_buf[tx_idx].map;
1151 ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head, segs, &nsegs,
1158 QL_DPRINT8((ha->pci_dev, "%s: EFBIG [%d]\n", __func__,
1159 m_head->m_pkthdr.len));
1161 m = m_defrag(m_head, M_DONTWAIT);
1163 ha->err_tx_defrag++;
1166 device_printf(ha->pci_dev,
1167 "%s: m_defrag() = NULL [%d]\n",
1174 if ((ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head,
1175 segs, &nsegs, BUS_DMA_NOWAIT))) {
1177 ha->err_tx_dmamap_load++;
1179 device_printf(ha->pci_dev,
1180 "%s: bus_dmamap_load_mbuf_sg failed0[%d, %d]\n",
1181 __func__, ret, m_head->m_pkthdr.len);
1183 if (ret != ENOMEM) {
1192 ha->err_tx_dmamap_load++;
1194 device_printf(ha->pci_dev,
1195 "%s: bus_dmamap_load_mbuf_sg failed1[%d, %d]\n",
1196 __func__, ret, m_head->m_pkthdr.len);
1198 if (ret != ENOMEM) {
1205 QL_ASSERT(ha, (nsegs != 0), ("qls_send: empty packet"));
1207 bus_dmamap_sync(ha->tx_tag, map, BUS_DMASYNC_PREWRITE);
1209 if (!(ret = qls_hw_send(ha, segs, nsegs, tx_idx, m_head, txr_idx))) {
1211 ha->tx_ring[txr_idx].count++;
1212 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head = m_head;
1213 ha->tx_ring[txr_idx].tx_buf[tx_idx].map = map;
1215 if (ret == EINVAL) {
1222 QL_DPRINT8((ha->pci_dev, "%s: exit\n", __func__));
1227 qls_stop(qla_host_t *ha)
1229 struct ifnet *ifp = ha->ifp;
1234 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE | IFF_DRV_RUNNING);
1236 ha->flags.qla_watchdog_pause = 1;
1238 while (!ha->qla_watchdog_paused)
1239 qls_mdelay(__func__, 1);
1245 qls_flush_xmt_bufs(ha);
1246 qls_free_rcv_bufs(ha);
1252 * Buffer Management Functions for Transmit and Receive Rings
1255 * Release mbuf after it sent on the wire
1258 qls_flush_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb)
1260 QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
1264 bus_dmamap_unload(ha->tx_tag, txb->map);
1266 m_freem(txb->m_head);
1270 QL_DPRINT2((ha->pci_dev, "%s: exit\n", __func__));
1274 qls_flush_xmt_bufs(qla_host_t *ha)
1278 for (j = 0; j < ha->num_tx_rings; j++) {
1279 for (i = 0; i < NUM_TX_DESCRIPTORS; i++)
1280 qls_flush_tx_buf(ha, &ha->tx_ring[j].tx_buf[i]);
1288 qls_alloc_rcv_mbufs(qla_host_t *ha, int r)
1292 qla_rx_ring_t *rx_ring;
1293 volatile q81_bq_addr_e_t *sbq_e;
1296 rx_ring = &ha->rx_ring[r];
1298 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1300 rxb = &rx_ring->rx_buf[i];
1302 ret = bus_dmamap_create(ha->rx_tag, BUS_DMA_NOWAIT, &rxb->map);
1305 device_printf(ha->pci_dev,
1306 "%s: dmamap[%d, %d] failed\n", __func__, r, i);
1308 for (j = 0; j < i; j++) {
1309 rxb = &rx_ring->rx_buf[j];
1310 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1312 goto qls_alloc_rcv_mbufs_err;
1316 rx_ring = &ha->rx_ring[r];
1318 sbq_e = rx_ring->sbq_vaddr;
1320 rxb = &rx_ring->rx_buf[0];
1322 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1324 if (!(ret = qls_get_mbuf(ha, rxb, NULL))) {
1327 * set the physical address in the
1328 * corresponding descriptor entry in the
1329 * receive ring/queue for the hba
1332 sbq_e->addr_lo = rxb->paddr & 0xFFFFFFFF;
1333 sbq_e->addr_hi = (rxb->paddr >> 32) & 0xFFFFFFFF;
1336 device_printf(ha->pci_dev,
1337 "%s: qls_get_mbuf [%d, %d] failed\n",
1339 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1340 goto qls_alloc_rcv_mbufs_err;
1348 qls_alloc_rcv_mbufs_err:
1353 qls_free_rcv_bufs(qla_host_t *ha)
1359 for (r = 0; r < ha->num_rx_rings; r++) {
1361 rxr = &ha->rx_ring[r];
1363 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1365 rxb = &rxr->rx_buf[i];
1367 if (rxb->m_head != NULL) {
1368 bus_dmamap_unload(ha->rx_tag, rxb->map);
1369 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1370 m_freem(rxb->m_head);
1373 bzero(rxr->rx_buf, (sizeof(qla_rx_buf_t) * NUM_RX_DESCRIPTORS));
1379 qls_alloc_rcv_bufs(qla_host_t *ha)
1384 for (r = 0; r < ha->num_rx_rings; r++) {
1385 rxr = &ha->rx_ring[r];
1386 bzero(rxr->rx_buf, (sizeof(qla_rx_buf_t) * NUM_RX_DESCRIPTORS));
1389 for (r = 0; r < ha->num_rx_rings; r++) {
1391 ret = qls_alloc_rcv_mbufs(ha, r);
1394 qls_free_rcv_bufs(ha);
1401 qls_get_mbuf(qla_host_t *ha, qla_rx_buf_t *rxb, struct mbuf *nmp)
1403 register struct mbuf *mp = nmp;
1407 bus_dma_segment_t segs[1];
1410 QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
1416 mp = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, ha->msize);
1420 if (ha->msize == MCLBYTES)
1426 device_printf(ha->pci_dev,
1427 "%s: m_getcl failed\n", __func__);
1428 goto exit_qls_get_mbuf;
1430 mp->m_len = mp->m_pkthdr.len = ha->msize;
1432 mp->m_len = mp->m_pkthdr.len = ha->msize;
1433 mp->m_data = mp->m_ext.ext_buf;
1437 /* align the receive buffers to 8 byte boundary */
1438 offset = (uint32_t)((unsigned long long)mp->m_data & 0x7ULL);
1440 offset = 8 - offset;
1445 * Using memory from the mbuf cluster pool, invoke the bus_dma
1446 * machinery to arrange the memory mapping.
1448 ret = bus_dmamap_load_mbuf_sg(ha->rx_tag, rxb->map,
1449 mp, segs, &nsegs, BUS_DMA_NOWAIT);
1450 rxb->paddr = segs[0].ds_addr;
1452 if (ret || !rxb->paddr || (nsegs != 1)) {
1455 device_printf(ha->pci_dev,
1456 "%s: bus_dmamap_load failed[%d, 0x%016llx, %d]\n",
1457 __func__, ret, (long long unsigned int)rxb->paddr,
1460 goto exit_qls_get_mbuf;
1463 bus_dmamap_sync(ha->rx_tag, rxb->map, BUS_DMASYNC_PREREAD);
1466 QL_DPRINT2((ha->pci_dev, "%s: exit ret = 0x%08x\n", __func__, ret));
1471 qls_tx_done(void *context, int pending)
1473 qla_host_t *ha = context;
1481 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1482 QL_DPRINT8((ha->pci_dev, "%s: !IFF_DRV_RUNNING\n", __func__));
1491 qls_config_lro(qla_host_t *ha)
1494 struct lro_ctrl *lro;
1496 for (i = 0; i < ha->num_rx_rings; i++) {
1497 lro = &ha->rx_ring[i].lro;
1498 if (tcp_lro_init(lro)) {
1499 device_printf(ha->pci_dev, "%s: tcp_lro_init failed\n",
1505 ha->flags.lro_init = 1;
1507 QL_DPRINT2((ha->pci_dev, "%s: LRO initialized\n", __func__));
1512 qls_free_lro(qla_host_t *ha)
1515 struct lro_ctrl *lro;
1517 if (!ha->flags.lro_init)
1520 for (i = 0; i < ha->num_rx_rings; i++) {
1521 lro = &ha->rx_ring[i].lro;
1524 ha->flags.lro_init = 0;
1528 qls_error_recovery(void *context, int pending)
1530 qla_host_t *ha = context;