2 * Copyright (c) 2010-2011 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 "qla_inline.h"
46 * Some PCI Configuration Space Related Defines
49 #ifndef PCI_VENDOR_QLOGIC
50 #define PCI_VENDOR_QLOGIC 0x1077
53 #ifndef PCI_PRODUCT_QLOGIC_ISP8020
54 #define PCI_PRODUCT_QLOGIC_ISP8020 0x8020
57 #define PCI_QLOGIC_ISP8020 \
58 ((PCI_PRODUCT_QLOGIC_ISP8020 << 16) | PCI_VENDOR_QLOGIC)
63 static int qla_alloc_parent_dma_tag(qla_host_t *ha);
64 static void qla_free_parent_dma_tag(qla_host_t *ha);
65 static int qla_alloc_xmt_bufs(qla_host_t *ha);
66 static void qla_free_xmt_bufs(qla_host_t *ha);
67 static int qla_alloc_rcv_bufs(qla_host_t *ha);
68 static void qla_free_rcv_bufs(qla_host_t *ha);
70 static void qla_init_ifnet(device_t dev, qla_host_t *ha);
71 static int qla_sysctl_get_stats(SYSCTL_HANDLER_ARGS);
72 static void qla_release(qla_host_t *ha);
73 static void qla_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs,
75 static void qla_stop(qla_host_t *ha);
76 static int qla_send(qla_host_t *ha, struct mbuf **m_headp);
77 static void qla_tx_done(void *context, int pending);
80 * Hooks to the Operating Systems
82 static int qla_pci_probe (device_t);
83 static int qla_pci_attach (device_t);
84 static int qla_pci_detach (device_t);
86 static void qla_init(void *arg);
87 static int qla_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
88 static int qla_media_change(struct ifnet *ifp);
89 static void qla_media_status(struct ifnet *ifp, struct ifmediareq *ifmr);
91 static device_method_t qla_pci_methods[] = {
92 /* Device interface */
93 DEVMETHOD(device_probe, qla_pci_probe),
94 DEVMETHOD(device_attach, qla_pci_attach),
95 DEVMETHOD(device_detach, qla_pci_detach),
99 static driver_t qla_pci_driver = {
100 "ql", qla_pci_methods, sizeof (qla_host_t),
103 static devclass_t qla80xx_devclass;
105 DRIVER_MODULE(qla80xx, pci, qla_pci_driver, qla80xx_devclass, 0, 0);
107 MODULE_DEPEND(qla80xx, pci, 1, 1, 1);
108 MODULE_DEPEND(qla80xx, ether, 1, 1, 1);
110 MALLOC_DEFINE(M_QLA8XXXBUF, "qla80xxbuf", "Buffers for qla80xx driver");
112 uint32_t std_replenish = 8;
113 uint32_t jumbo_replenish = 2;
114 uint32_t rcv_pkt_thres = 128;
115 uint32_t rcv_pkt_thres_d = 32;
116 uint32_t snd_pkt_thres = 16;
117 uint32_t free_pkt_thres = (NUM_TX_DESCRIPTORS / 2);
119 static char dev_str[64];
122 * Name: qla_pci_probe
123 * Function: Validate the PCI device to be a QLA80XX device
126 qla_pci_probe(device_t dev)
128 switch ((pci_get_device(dev) << 16) | (pci_get_vendor(dev))) {
129 case PCI_QLOGIC_ISP8020:
130 snprintf(dev_str, sizeof(dev_str), "%s v%d.%d.%d",
131 "Qlogic ISP 80xx PCI CNA Adapter-Ethernet Function",
132 QLA_VERSION_MAJOR, QLA_VERSION_MINOR,
134 device_set_desc(dev, dev_str);
141 printf("%s: %s\n ", __func__, dev_str);
143 return (BUS_PROBE_DEFAULT);
147 qla_add_sysctls(qla_host_t *ha)
149 device_t dev = ha->pci_dev;
151 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
152 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
153 OID_AUTO, "stats", CTLTYPE_INT | CTLFLAG_RD,
155 qla_sysctl_get_stats, "I", "Statistics");
158 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
159 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
160 OID_AUTO, "debug", CTLFLAG_RW,
161 &dbg_level, dbg_level, "Debug Level");
163 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
164 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
165 OID_AUTO, "std_replenish", CTLFLAG_RW,
166 &std_replenish, std_replenish,
167 "Threshold for Replenishing Standard Frames");
169 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
170 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
171 OID_AUTO, "jumbo_replenish", CTLFLAG_RW,
172 &jumbo_replenish, jumbo_replenish,
173 "Threshold for Replenishing Jumbo Frames");
175 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
176 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
177 OID_AUTO, "rcv_pkt_thres", CTLFLAG_RW,
178 &rcv_pkt_thres, rcv_pkt_thres,
179 "Threshold for # of rcv pkts to trigger indication isr");
181 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
182 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
183 OID_AUTO, "rcv_pkt_thres_d", CTLFLAG_RW,
184 &rcv_pkt_thres_d, rcv_pkt_thres_d,
185 "Threshold for # of rcv pkts to trigger indication defered");
187 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
188 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
189 OID_AUTO, "snd_pkt_thres", CTLFLAG_RW,
190 &snd_pkt_thres, snd_pkt_thres,
191 "Threshold for # of snd packets");
193 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
194 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
195 OID_AUTO, "free_pkt_thres", CTLFLAG_RW,
196 &free_pkt_thres, free_pkt_thres,
197 "Threshold for # of packets to free at a time");
203 qla_watchdog(void *arg)
205 qla_host_t *ha = arg;
212 if (ha->flags.qla_watchdog_exit)
215 if (!ha->flags.qla_watchdog_pause) {
216 if (qla_le32_to_host(*(hw->tx_cons)) != hw->txr_comp) {
217 taskqueue_enqueue(ha->tx_tq, &ha->tx_task);
218 } else if ((ifp->if_snd.ifq_head != NULL) && QL_RUNNING(ifp)) {
219 taskqueue_enqueue(ha->tx_tq, &ha->tx_task);
222 ha->watchdog_ticks = ha->watchdog_ticks++ % 1000;
223 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
228 * Name: qla_pci_attach
229 * Function: attaches the device to the operating system
232 qla_pci_attach(device_t dev)
234 qla_host_t *ha = NULL;
235 uint32_t rsrc_len, i;
237 QL_DPRINT2((dev, "%s: enter\n", __func__));
239 if ((ha = device_get_softc(dev)) == NULL) {
240 device_printf(dev, "cannot get softc\n");
244 memset(ha, 0, sizeof (qla_host_t));
246 if (pci_get_device(dev) != PCI_PRODUCT_QLOGIC_ISP8020) {
247 device_printf(dev, "device is not ISP8020\n");
251 ha->pci_func = pci_get_function(dev);
255 pci_enable_busmaster(dev);
257 ha->reg_rid = PCIR_BAR(0);
258 ha->pci_reg = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &ha->reg_rid,
261 if (ha->pci_reg == NULL) {
262 device_printf(dev, "unable to map any ports\n");
263 goto qla_pci_attach_err;
266 rsrc_len = (uint32_t) bus_get_resource_count(dev, SYS_RES_MEMORY,
269 mtx_init(&ha->hw_lock, "qla80xx_hw_lock", MTX_NETWORK_LOCK, MTX_DEF);
270 mtx_init(&ha->tx_lock, "qla80xx_tx_lock", MTX_NETWORK_LOCK, MTX_DEF);
271 mtx_init(&ha->rx_lock, "qla80xx_rx_lock", MTX_NETWORK_LOCK, MTX_DEF);
272 mtx_init(&ha->rxj_lock, "qla80xx_rxj_lock", MTX_NETWORK_LOCK, MTX_DEF);
273 ha->flags.lock_init = 1;
275 ha->msix_count = pci_msix_count(dev);
277 if (ha->msix_count < qla_get_msix_count(ha)) {
278 device_printf(dev, "%s: msix_count[%d] not enough\n", __func__,
280 goto qla_pci_attach_err;
283 QL_DPRINT2((dev, "%s: ha %p irq %p pci_func 0x%x rsrc_count 0x%08x"
284 " msix_count 0x%x pci_reg %p\n", __func__, ha,
285 ha->irq, ha->pci_func, rsrc_len, ha->msix_count, ha->pci_reg));
287 ha->msix_count = qla_get_msix_count(ha);
289 if (pci_alloc_msix(dev, &ha->msix_count)) {
290 device_printf(dev, "%s: pci_alloc_msi[%d] failed\n", __func__,
293 goto qla_pci_attach_err;
296 TASK_INIT(&ha->tx_task, 0, qla_tx_done, ha);
297 ha->tx_tq = taskqueue_create_fast("qla_txq", M_NOWAIT,
298 taskqueue_thread_enqueue, &ha->tx_tq);
299 taskqueue_start_threads(&ha->tx_tq, 1, PI_NET, "%s txq",
300 device_get_nameunit(ha->pci_dev));
302 for (i = 0; i < ha->msix_count; i++) {
303 ha->irq_vec[i].irq_rid = i+1;
304 ha->irq_vec[i].ha = ha;
306 ha->irq_vec[i].irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
307 &ha->irq_vec[i].irq_rid,
308 (RF_ACTIVE | RF_SHAREABLE));
310 if (ha->irq_vec[i].irq == NULL) {
311 device_printf(dev, "could not allocate interrupt\n");
312 goto qla_pci_attach_err;
315 if (bus_setup_intr(dev, ha->irq_vec[i].irq,
316 (INTR_TYPE_NET | INTR_MPSAFE),
317 NULL, qla_isr, &ha->irq_vec[i],
318 &ha->irq_vec[i].handle)) {
319 device_printf(dev, "could not setup interrupt\n");
320 goto qla_pci_attach_err;
323 TASK_INIT(&ha->irq_vec[i].rcv_task, 0, qla_rcv,\
326 ha->irq_vec[i].rcv_tq = taskqueue_create_fast("qla_rcvq",
327 M_NOWAIT, taskqueue_thread_enqueue,
328 &ha->irq_vec[i].rcv_tq);
330 taskqueue_start_threads(&ha->irq_vec[i].rcv_tq, 1, PI_NET,
332 device_get_nameunit(ha->pci_dev));
337 /* add hardware specific sysctls */
338 qla_hw_add_sysctls(ha);
340 /* initialize hardware */
341 if (qla_init_hw(ha)) {
342 device_printf(dev, "%s: qla_init_hw failed\n", __func__);
343 goto qla_pci_attach_err;
346 device_printf(dev, "%s: firmware[%d.%d.%d.%d]\n", __func__,
347 ha->fw_ver_major, ha->fw_ver_minor, ha->fw_ver_sub,
350 //qla_get_hw_caps(ha);
351 qla_read_mac_addr(ha);
353 /* allocate parent dma tag */
354 if (qla_alloc_parent_dma_tag(ha)) {
355 device_printf(dev, "%s: qla_alloc_parent_dma_tag failed\n",
357 goto qla_pci_attach_err;
360 /* alloc all dma buffers */
361 if (qla_alloc_dma(ha)) {
362 device_printf(dev, "%s: qla_alloc_dma failed\n", __func__);
363 goto qla_pci_attach_err;
366 /* create the o.s ethernet interface */
367 qla_init_ifnet(dev, ha);
369 ha->flags.qla_watchdog_active = 1;
370 ha->flags.qla_watchdog_pause = 1;
372 callout_init(&ha->tx_callout, TRUE);
374 /* create ioctl device interface */
375 if (qla_make_cdev(ha)) {
376 device_printf(dev, "%s: qla_make_cdev failed\n", __func__);
377 goto qla_pci_attach_err;
380 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
383 QL_DPRINT2((dev, "%s: exit 0\n", __func__));
390 QL_DPRINT2((dev, "%s: exit ENXIO\n", __func__));
395 * Name: qla_pci_detach
396 * Function: Unhooks the device from the operating system
399 qla_pci_detach(device_t dev)
401 qla_host_t *ha = NULL;
405 QL_DPRINT2((dev, "%s: enter\n", __func__));
407 if ((ha = device_get_softc(dev)) == NULL) {
408 device_printf(dev, "cannot get softc\n");
414 QLA_LOCK(ha, __func__);
416 QLA_UNLOCK(ha, __func__);
419 taskqueue_drain(ha->tx_tq, &ha->tx_task);
420 taskqueue_free(ha->tx_tq);
423 for (i = 0; i < ha->msix_count; i++) {
424 taskqueue_drain(ha->irq_vec[i].rcv_tq,
425 &ha->irq_vec[i].rcv_task);
426 taskqueue_free(ha->irq_vec[i].rcv_tq);
431 QL_DPRINT2((dev, "%s: exit\n", __func__));
437 * SYSCTL Related Callbacks
440 qla_sysctl_get_stats(SYSCTL_HANDLER_ARGS)
445 err = sysctl_handle_int(oidp, &ret, 0, req);
450 ha = (qla_host_t *)arg1;
452 QL_DPRINT2((ha->pci_dev, "%s: called ret %d\n", __func__, ret));
459 * Function: Releases the resources allocated for the device
462 qla_release(qla_host_t *ha)
471 if (ha->flags.qla_watchdog_active)
472 ha->flags.qla_watchdog_exit = 1;
474 callout_stop(&ha->tx_callout);
475 qla_mdelay(__func__, 100);
478 ether_ifdetach(ha->ifp);
481 qla_free_parent_dma_tag(ha);
483 for (i = 0; i < ha->msix_count; i++) {
484 if (ha->irq_vec[i].handle)
485 (void)bus_teardown_intr(dev, ha->irq_vec[i].irq,
486 ha->irq_vec[i].handle);
487 if (ha->irq_vec[i].irq)
488 (void) bus_release_resource(dev, SYS_RES_IRQ,
489 ha->irq_vec[i].irq_rid,
493 pci_release_msi(dev);
495 if (ha->flags.lock_init) {
496 mtx_destroy(&ha->tx_lock);
497 mtx_destroy(&ha->rx_lock);
498 mtx_destroy(&ha->rxj_lock);
499 mtx_destroy(&ha->hw_lock);
503 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid,
508 * DMA Related Functions
512 qla_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
514 *((bus_addr_t *)arg) = 0;
517 printf("%s: bus_dmamap_load failed (%d)\n", __func__, error);
521 QL_ASSERT((nsegs == 1), ("%s: %d segments returned!", __func__, nsegs));
523 *((bus_addr_t *)arg) = segs[0].ds_addr;
529 qla_alloc_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
537 QL_DPRINT2((dev, "%s: enter\n", __func__));
539 ret = bus_dma_tag_create(
540 ha->parent_tag,/* parent */
542 ((bus_size_t)(1ULL << 32)),/* boundary */
543 BUS_SPACE_MAXADDR, /* lowaddr */
544 BUS_SPACE_MAXADDR, /* highaddr */
545 NULL, NULL, /* filter, filterarg */
546 dma_buf->size, /* maxsize */
548 dma_buf->size, /* maxsegsize */
550 NULL, NULL, /* lockfunc, lockarg */
554 device_printf(dev, "%s: could not create dma tag\n", __func__);
555 goto qla_alloc_dmabuf_exit;
557 ret = bus_dmamem_alloc(dma_buf->dma_tag,
558 (void **)&dma_buf->dma_b,
559 (BUS_DMA_ZERO | BUS_DMA_COHERENT | BUS_DMA_NOWAIT),
562 bus_dma_tag_destroy(dma_buf->dma_tag);
563 device_printf(dev, "%s: bus_dmamem_alloc failed\n", __func__);
564 goto qla_alloc_dmabuf_exit;
567 ret = bus_dmamap_load(dma_buf->dma_tag,
572 &b_addr, BUS_DMA_NOWAIT);
574 if (ret || !b_addr) {
575 bus_dma_tag_destroy(dma_buf->dma_tag);
576 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b,
579 goto qla_alloc_dmabuf_exit;
582 dma_buf->dma_addr = b_addr;
584 qla_alloc_dmabuf_exit:
585 QL_DPRINT2((dev, "%s: exit ret 0x%08x tag %p map %p b %p sz 0x%x\n",
586 __func__, ret, (void *)dma_buf->dma_tag,
587 (void *)dma_buf->dma_map, (void *)dma_buf->dma_b,
594 qla_free_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
596 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b, dma_buf->dma_map);
597 bus_dma_tag_destroy(dma_buf->dma_tag);
601 qla_alloc_parent_dma_tag(qla_host_t *ha)
609 * Allocate parent DMA Tag
611 ret = bus_dma_tag_create(
612 bus_get_dma_tag(dev), /* parent */
613 1,((bus_size_t)(1ULL << 32)),/* alignment, boundary */
614 BUS_SPACE_MAXADDR, /* lowaddr */
615 BUS_SPACE_MAXADDR, /* highaddr */
616 NULL, NULL, /* filter, filterarg */
617 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
619 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
621 NULL, NULL, /* lockfunc, lockarg */
625 device_printf(dev, "%s: could not create parent dma tag\n",
630 ha->flags.parent_tag = 1;
636 qla_free_parent_dma_tag(qla_host_t *ha)
638 if (ha->flags.parent_tag) {
639 bus_dma_tag_destroy(ha->parent_tag);
640 ha->flags.parent_tag = 0;
645 * Name: qla_init_ifnet
646 * Function: Creates the Network Device Interface and Registers it with the O.S
650 qla_init_ifnet(device_t dev, qla_host_t *ha)
654 QL_DPRINT2((dev, "%s: enter\n", __func__));
656 ifp = ha->ifp = if_alloc(IFT_ETHER);
659 panic("%s: cannot if_alloc()\n", device_get_nameunit(dev));
661 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
663 ifp->if_mtu = ETHERMTU;
664 ifp->if_baudrate = (1 * 1000 * 1000 *1000);
665 ifp->if_init = qla_init;
667 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
668 ifp->if_ioctl = qla_ioctl;
669 ifp->if_start = qla_start;
671 IFQ_SET_MAXLEN(&ifp->if_snd, qla_get_ifq_snd_maxlen(ha));
672 ifp->if_snd.ifq_drv_maxlen = qla_get_ifq_snd_maxlen(ha);
673 IFQ_SET_READY(&ifp->if_snd);
675 ha->max_frame_size = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
677 ether_ifattach(ifp, qla_get_mac_addr(ha));
679 ifp->if_capabilities = IFCAP_HWCSUM |
684 ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
686 #if defined(__FreeBSD_version) && (__FreeBSD_version < 900002)
688 ifp->if_watchdog = NULL;
689 #endif /* #if defined(__FreeBSD_version) && (__FreeBSD_version < 900002) */
691 ifp->if_capenable = ifp->if_capabilities;
693 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
695 ifmedia_init(&ha->media, IFM_IMASK, qla_media_change, qla_media_status);
697 ifmedia_add(&ha->media, (IFM_ETHER | qla_get_optics(ha) | IFM_FDX), 0,
699 ifmedia_add(&ha->media, (IFM_ETHER | IFM_AUTO), 0, NULL);
701 ifmedia_set(&ha->media, (IFM_ETHER | IFM_AUTO));
703 QL_DPRINT2((dev, "%s: exit\n", __func__));
709 qla_init_locked(qla_host_t *ha)
711 struct ifnet *ifp = ha->ifp;
715 if (qla_alloc_xmt_bufs(ha) != 0)
718 if (qla_alloc_rcv_bufs(ha) != 0)
721 if (qla_config_lro(ha))
724 bcopy(IF_LLADDR(ha->ifp), ha->hw.mac_addr, ETHER_ADDR_LEN);
726 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_TSO;
728 ha->flags.stop_rcv = 0;
729 if (qla_init_hw_if(ha) == 0) {
731 ifp->if_drv_flags |= IFF_DRV_RUNNING;
732 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
733 ha->flags.qla_watchdog_pause = 0;
744 ha = (qla_host_t *)arg;
746 QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
748 QLA_LOCK(ha, __func__);
750 QLA_UNLOCK(ha, __func__);
752 QL_DPRINT2((ha->pci_dev, "%s: exit\n", __func__));
756 qla_set_multi(qla_host_t *ha, uint32_t add_multi)
758 uint8_t mta[Q8_MAX_NUM_MULTICAST_ADDRS * Q8_MAC_ADDR_LEN];
759 struct ifmultiaddr *ifma;
761 struct ifnet *ifp = ha->ifp;
765 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
767 if (ifma->ifma_addr->sa_family != AF_LINK)
770 if (mcnt == Q8_MAX_NUM_MULTICAST_ADDRS)
773 bcopy(LLADDR((struct sockaddr_dl *) ifma->ifma_addr),
774 &mta[mcnt * Q8_MAC_ADDR_LEN], Q8_MAC_ADDR_LEN);
781 qla_hw_set_multi(ha, mta, mcnt, add_multi);
787 qla_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
790 struct ifreq *ifr = (struct ifreq *)data;
791 struct ifaddr *ifa = (struct ifaddr *)data;
794 ha = (qla_host_t *)ifp->if_softc;
798 QL_DPRINT4((ha->pci_dev, "%s: SIOCSIFADDR (0x%lx)\n",
801 if (ifa->ifa_addr->sa_family == AF_INET) {
802 ifp->if_flags |= IFF_UP;
803 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
804 QLA_LOCK(ha, __func__);
806 QLA_UNLOCK(ha, __func__);
808 QL_DPRINT4((ha->pci_dev,
809 "%s: SIOCSIFADDR (0x%lx) ipv4 [0x%08x]\n",
810 __func__, cmd, ntohl(IA_SIN(ifa)->sin_addr.s_addr)));
812 arp_ifinit(ifp, ifa);
813 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) {
814 qla_config_ipv4_addr(ha,
815 (IA_SIN(ifa)->sin_addr.s_addr));
818 ether_ioctl(ifp, cmd, data);
823 QL_DPRINT4((ha->pci_dev, "%s: SIOCSIFMTU (0x%lx)\n",
826 if (ifr->ifr_mtu > QLA_MAX_FRAME_SIZE - ETHER_HDR_LEN) {
829 QLA_LOCK(ha, __func__);
830 ifp->if_mtu = ifr->ifr_mtu;
832 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
833 if ((ifp->if_drv_flags & IFF_DRV_RUNNING)) {
834 ret = qla_set_max_mtu(ha, ha->max_frame_size,
835 (ha->hw.rx_cntxt_rsp)->rx_rsp.cntxt_id);
837 QLA_UNLOCK(ha, __func__);
846 QL_DPRINT4((ha->pci_dev, "%s: SIOCSIFFLAGS (0x%lx)\n",
849 if (ifp->if_flags & IFF_UP) {
850 if ((ifp->if_drv_flags & IFF_DRV_RUNNING)) {
851 if ((ifp->if_flags ^ ha->if_flags) &
854 } else if ((ifp->if_flags ^ ha->if_flags) &
856 qla_set_allmulti(ha);
859 QLA_LOCK(ha, __func__);
861 ha->max_frame_size = ifp->if_mtu +
862 ETHER_HDR_LEN + ETHER_CRC_LEN;
863 ret = qla_set_max_mtu(ha, ha->max_frame_size,
864 (ha->hw.rx_cntxt_rsp)->rx_rsp.cntxt_id);
865 QLA_UNLOCK(ha, __func__);
868 QLA_LOCK(ha, __func__);
869 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
871 ha->if_flags = ifp->if_flags;
872 QLA_UNLOCK(ha, __func__);
877 QL_DPRINT4((ha->pci_dev,
878 "%s: %s (0x%lx)\n", __func__, "SIOCADDMULTI", cmd));
880 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
881 qla_set_multi(ha, 1);
886 QL_DPRINT4((ha->pci_dev,
887 "%s: %s (0x%lx)\n", __func__, "SIOCDELMULTI", cmd));
889 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
890 qla_set_multi(ha, 0);
896 QL_DPRINT4((ha->pci_dev,
897 "%s: SIOCSIFMEDIA/SIOCGIFMEDIA (0x%lx)\n",
899 ret = ifmedia_ioctl(ifp, ifr, &ha->media, cmd);
904 int mask = ifr->ifr_reqcap ^ ifp->if_capenable;
906 QL_DPRINT4((ha->pci_dev, "%s: SIOCSIFCAP (0x%lx)\n",
909 if (mask & IFCAP_HWCSUM)
910 ifp->if_capenable ^= IFCAP_HWCSUM;
911 if (mask & IFCAP_TSO4)
912 ifp->if_capenable ^= IFCAP_TSO4;
913 if (mask & IFCAP_TSO6)
914 ifp->if_capenable ^= IFCAP_TSO6;
915 if (mask & IFCAP_VLAN_HWTAGGING)
916 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
918 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
921 VLAN_CAPABILITIES(ifp);
926 QL_DPRINT4((ha->pci_dev, "%s: default (0x%lx)\n",
928 ret = ether_ioctl(ifp, cmd, data);
936 qla_media_change(struct ifnet *ifp)
942 ha = (qla_host_t *)ifp->if_softc;
944 QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
948 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
951 QL_DPRINT2((ha->pci_dev, "%s: exit\n", __func__));
957 qla_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
961 ha = (qla_host_t *)ifp->if_softc;
963 QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
965 ifmr->ifm_status = IFM_AVALID;
966 ifmr->ifm_active = IFM_ETHER;
968 qla_update_link_state(ha);
969 if (ha->hw.flags.link_up) {
970 ifmr->ifm_status |= IFM_ACTIVE;
971 ifmr->ifm_active |= (IFM_FDX | qla_get_optics(ha));
974 QL_DPRINT2((ha->pci_dev, "%s: exit (%s)\n", __func__,\
975 (ha->hw.flags.link_up ? "link_up" : "link_down")));
981 qla_start(struct ifnet *ifp)
984 qla_host_t *ha = (qla_host_t *)ifp->if_softc;
986 QL_DPRINT8((ha->pci_dev, "%s: enter\n", __func__));
988 if (!mtx_trylock(&ha->tx_lock)) {
989 QL_DPRINT8((ha->pci_dev,
990 "%s: mtx_trylock(&ha->tx_lock) failed\n", __func__));
994 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
996 QL_DPRINT8((ha->pci_dev, "%s: !IFF_DRV_RUNNING\n", __func__));
1001 if (!ha->watchdog_ticks)
1002 qla_update_link_state(ha);
1004 if (!ha->hw.flags.link_up) {
1005 QL_DPRINT8((ha->pci_dev, "%s: link down\n", __func__));
1010 while (ifp->if_snd.ifq_head != NULL) {
1011 IF_DEQUEUE(&ifp->if_snd, m_head);
1013 if (m_head == NULL) {
1014 QL_DPRINT8((ha->pci_dev, "%s: m_head == NULL\n",
1019 if (qla_send(ha, &m_head)) {
1022 QL_DPRINT8((ha->pci_dev, "%s: PREPEND\n", __func__));
1023 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1024 IF_PREPEND(&ifp->if_snd, m_head);
1027 /* Send a copy of the frame to the BPF listener */
1028 ETHER_BPF_MTAP(ifp, m_head);
1031 QL_DPRINT8((ha->pci_dev, "%s: exit\n", __func__));
1036 qla_send(qla_host_t *ha, struct mbuf **m_headp)
1038 bus_dma_segment_t segs[QLA_MAX_SEGMENTS];
1043 struct mbuf *m_head = *m_headp;
1045 QL_DPRINT8((ha->pci_dev, "%s: enter\n", __func__));
1047 if ((ret = bus_dmamap_create(ha->tx_tag, BUS_DMA_NOWAIT, &map))) {
1048 ha->err_tx_dmamap_create++;
1049 device_printf(ha->pci_dev,
1050 "%s: bus_dmamap_create failed[%d, %d]\n",
1051 __func__, ret, m_head->m_pkthdr.len);
1055 ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head, segs, &nsegs,
1058 if ((ret == EFBIG) ||
1059 ((nsegs > Q8_TX_MAX_SEGMENTS) &&
1060 (((m_head->m_pkthdr.csum_flags & CSUM_TSO) == 0) ||
1061 (m_head->m_pkthdr.len <= ha->max_frame_size)))) {
1065 QL_DPRINT8((ha->pci_dev, "%s: EFBIG [%d]\n", __func__,
1066 m_head->m_pkthdr.len));
1068 m = m_defrag(m_head, M_DONTWAIT);
1070 ha->err_tx_defrag++;
1073 device_printf(ha->pci_dev,
1074 "%s: m_defrag() = NULL [%d]\n",
1080 if ((ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head,
1081 segs, &nsegs, BUS_DMA_NOWAIT))) {
1083 ha->err_tx_dmamap_load++;
1085 device_printf(ha->pci_dev,
1086 "%s: bus_dmamap_load_mbuf_sg failed0[%d, %d]\n",
1087 __func__, ret, m_head->m_pkthdr.len);
1089 bus_dmamap_destroy(ha->tx_tag, map);
1090 if (ret != ENOMEM) {
1097 ha->err_tx_dmamap_load++;
1099 device_printf(ha->pci_dev,
1100 "%s: bus_dmamap_load_mbuf_sg failed1[%d, %d]\n",
1101 __func__, ret, m_head->m_pkthdr.len);
1103 bus_dmamap_destroy(ha->tx_tag, map);
1105 if (ret != ENOMEM) {
1112 QL_ASSERT((nsegs != 0), ("qla_send: empty packet"));
1114 bus_dmamap_sync(ha->tx_tag, map, BUS_DMASYNC_PREWRITE);
1116 if (!(ret = qla_hw_send(ha, segs, nsegs, &tx_idx, m_head))) {
1117 ha->tx_buf[tx_idx].m_head = m_head;
1118 ha->tx_buf[tx_idx].map = map;
1120 if (ret == EINVAL) {
1126 QL_DPRINT8((ha->pci_dev, "%s: exit\n", __func__));
1131 qla_stop(qla_host_t *ha)
1133 struct ifnet *ifp = ha->ifp;
1138 ha->flags.qla_watchdog_pause = 1;
1139 qla_mdelay(__func__, 100);
1141 ha->flags.stop_rcv = 1;
1142 qla_hw_stop_rcv(ha);
1148 qla_free_xmt_bufs(ha);
1149 qla_free_rcv_bufs(ha);
1151 ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE | IFF_DRV_RUNNING);
1157 * Buffer Management Functions for Transmit and Receive Rings
1160 qla_alloc_xmt_bufs(qla_host_t *ha)
1162 if (bus_dma_tag_create(NULL, /* parent */
1163 1, 0, /* alignment, bounds */
1164 BUS_SPACE_MAXADDR, /* lowaddr */
1165 BUS_SPACE_MAXADDR, /* highaddr */
1166 NULL, NULL, /* filter, filterarg */
1167 QLA_MAX_TSO_FRAME_SIZE, /* maxsize */
1168 QLA_MAX_SEGMENTS, /* nsegments */
1169 PAGE_SIZE, /* maxsegsize */
1170 BUS_DMA_ALLOCNOW, /* flags */
1171 NULL, /* lockfunc */
1172 NULL, /* lockfuncarg */
1174 device_printf(ha->pci_dev, "%s: tx_tag alloc failed\n",
1178 bzero((void *)ha->tx_buf, (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1184 * Release mbuf after it sent on the wire
1187 qla_clear_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb)
1189 QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
1193 bus_dmamap_unload(ha->tx_tag, txb->map);
1194 bus_dmamap_destroy(ha->tx_tag, txb->map);
1196 m_freem(txb->m_head);
1200 QL_DPRINT2((ha->pci_dev, "%s: exit\n", __func__));
1204 qla_free_xmt_bufs(qla_host_t *ha)
1208 for (i = 0; i < NUM_TX_DESCRIPTORS; i++)
1209 qla_clear_tx_buf(ha, &ha->tx_buf[i]);
1211 if (ha->tx_tag != NULL) {
1212 bus_dma_tag_destroy(ha->tx_tag);
1215 bzero((void *)ha->tx_buf, (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1222 qla_alloc_rcv_bufs(qla_host_t *ha)
1227 if (bus_dma_tag_create(NULL, /* parent */
1228 1, 0, /* alignment, bounds */
1229 BUS_SPACE_MAXADDR, /* lowaddr */
1230 BUS_SPACE_MAXADDR, /* highaddr */
1231 NULL, NULL, /* filter, filterarg */
1232 MJUM9BYTES, /* maxsize */
1234 MJUM9BYTES, /* maxsegsize */
1235 BUS_DMA_ALLOCNOW, /* flags */
1236 NULL, /* lockfunc */
1237 NULL, /* lockfuncarg */
1240 device_printf(ha->pci_dev, "%s: rx_tag alloc failed\n",
1246 bzero((void *)ha->rx_buf, (sizeof(qla_rx_buf_t) * NUM_RX_DESCRIPTORS));
1247 bzero((void *)ha->rx_jbuf,
1248 (sizeof(qla_rx_buf_t) * NUM_RX_JUMBO_DESCRIPTORS));
1250 for (i = 0; i < MAX_SDS_RINGS; i++) {
1251 ha->hw.sds[i].sdsr_next = 0;
1252 ha->hw.sds[i].rxb_free = NULL;
1253 ha->hw.sds[i].rx_free = 0;
1254 ha->hw.sds[i].rxjb_free = NULL;
1255 ha->hw.sds[i].rxj_free = 0;
1258 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1260 rxb = &ha->rx_buf[i];
1262 ret = bus_dmamap_create(ha->rx_tag, BUS_DMA_NOWAIT, &rxb->map);
1265 device_printf(ha->pci_dev,
1266 "%s: dmamap[%d] failed\n", __func__, i);
1268 for (j = 0; j < i; j++) {
1269 bus_dmamap_destroy(ha->rx_tag,
1272 goto qla_alloc_rcv_bufs_failed;
1276 qla_init_hw_rcv_descriptors(ha, RDS_RING_INDEX_NORMAL);
1278 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1279 rxb = &ha->rx_buf[i];
1281 if (!(ret = qla_get_mbuf(ha, rxb, NULL, 0))) {
1283 * set the physical address in the corresponding
1284 * descriptor entry in the receive ring/queue for the
1287 qla_set_hw_rcv_desc(ha, RDS_RING_INDEX_NORMAL, i,
1288 rxb->handle, rxb->paddr,
1289 (rxb->m_head)->m_pkthdr.len);
1291 device_printf(ha->pci_dev,
1292 "%s: qla_get_mbuf [standard(%d)] failed\n",
1294 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1295 goto qla_alloc_rcv_bufs_failed;
1300 for (i = 0; i < NUM_RX_JUMBO_DESCRIPTORS; i++) {
1302 rxb = &ha->rx_jbuf[i];
1304 ret = bus_dmamap_create(ha->rx_tag, BUS_DMA_NOWAIT, &rxb->map);
1307 device_printf(ha->pci_dev,
1308 "%s: dmamap[%d] failed\n", __func__, i);
1310 for (j = 0; j < i; j++) {
1311 bus_dmamap_destroy(ha->rx_tag,
1312 ha->rx_jbuf[j].map);
1314 goto qla_alloc_rcv_bufs_failed;
1318 qla_init_hw_rcv_descriptors(ha, RDS_RING_INDEX_JUMBO);
1320 for (i = 0; i < NUM_RX_JUMBO_DESCRIPTORS; i++) {
1321 rxb = &ha->rx_jbuf[i];
1323 if (!(ret = qla_get_mbuf(ha, rxb, NULL, 1))) {
1325 * set the physical address in the corresponding
1326 * descriptor entry in the receive ring/queue for the
1329 qla_set_hw_rcv_desc(ha, RDS_RING_INDEX_JUMBO, i,
1330 rxb->handle, rxb->paddr,
1331 (rxb->m_head)->m_pkthdr.len);
1333 device_printf(ha->pci_dev,
1334 "%s: qla_get_mbuf [jumbo(%d)] failed\n",
1336 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1337 goto qla_alloc_rcv_bufs_failed;
1343 qla_alloc_rcv_bufs_failed:
1344 qla_free_rcv_bufs(ha);
1349 qla_free_rcv_bufs(qla_host_t *ha)
1354 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1355 rxb = &ha->rx_buf[i];
1356 if (rxb->m_head != NULL) {
1357 bus_dmamap_unload(ha->rx_tag, rxb->map);
1358 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1359 m_freem(rxb->m_head);
1364 for (i = 0; i < NUM_RX_JUMBO_DESCRIPTORS; i++) {
1365 rxb = &ha->rx_jbuf[i];
1366 if (rxb->m_head != NULL) {
1367 bus_dmamap_unload(ha->rx_tag, rxb->map);
1368 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1369 m_freem(rxb->m_head);
1374 if (ha->rx_tag != NULL) {
1375 bus_dma_tag_destroy(ha->rx_tag);
1379 bzero((void *)ha->rx_buf, (sizeof(qla_rx_buf_t) * NUM_RX_DESCRIPTORS));
1380 bzero((void *)ha->rx_jbuf,
1381 (sizeof(qla_rx_buf_t) * NUM_RX_JUMBO_DESCRIPTORS));
1383 for (i = 0; i < MAX_SDS_RINGS; i++) {
1384 ha->hw.sds[i].sdsr_next = 0;
1385 ha->hw.sds[i].rxb_free = NULL;
1386 ha->hw.sds[i].rx_free = 0;
1387 ha->hw.sds[i].rxjb_free = NULL;
1388 ha->hw.sds[i].rxj_free = 0;
1395 qla_get_mbuf(qla_host_t *ha, qla_rx_buf_t *rxb, struct mbuf *nmp,
1398 register struct mbuf *mp = nmp;
1403 QL_DPRINT2((ha->pci_dev, "%s: jumbo(0x%x) enter\n", __func__, jumbo));
1410 mp = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1415 device_printf(ha->pci_dev,
1416 "%s: m_getcl failed\n", __func__);
1417 goto exit_qla_get_mbuf;
1419 mp->m_len = mp->m_pkthdr.len = MCLBYTES;
1421 mp = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR,
1426 device_printf(ha->pci_dev,
1427 "%s: m_getjcl failed\n", __func__);
1428 goto exit_qla_get_mbuf;
1430 mp->m_len = mp->m_pkthdr.len = MJUM9BYTES;
1434 mp->m_len = mp->m_pkthdr.len = MCLBYTES;
1436 mp->m_len = mp->m_pkthdr.len = MJUM9BYTES;
1438 mp->m_data = mp->m_ext.ext_buf;
1443 offset = (uint32_t)((unsigned long long)mp->m_data & 0x7ULL);
1445 offset = 8 - offset;
1450 * Using memory from the mbuf cluster pool, invoke the bus_dma
1451 * machinery to arrange the memory mapping.
1453 ret = bus_dmamap_load(ha->rx_tag, rxb->map,
1454 mtod(mp, void *), mp->m_len,
1455 qla_dmamap_callback, &rxb->paddr,
1457 if (ret || !rxb->paddr) {
1460 device_printf(ha->pci_dev,
1461 "%s: bus_dmamap_load failed\n", __func__);
1463 goto exit_qla_get_mbuf;
1466 bus_dmamap_sync(ha->rx_tag, rxb->map, BUS_DMASYNC_PREREAD);
1469 QL_DPRINT2((ha->pci_dev, "%s: exit ret = 0x%08x\n", __func__, ret));
1474 qla_tx_done(void *context, int pending)
1476 qla_host_t *ha = context;