2 * Copyright (c) 2013-2016 Qlogic Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * and ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
30 * Author : David C Somayajulu, Qlogic Corporation, Aliso Viejo, CA 92656.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
40 #include "ql_inline.h"
47 * Some PCI Configuration Space Related Defines
50 #ifndef PCI_VENDOR_QLOGIC
51 #define PCI_VENDOR_QLOGIC 0x1077
54 #ifndef PCI_PRODUCT_QLOGIC_ISP8030
55 #define PCI_PRODUCT_QLOGIC_ISP8030 0x8030
58 #define PCI_QLOGIC_ISP8030 \
59 ((PCI_PRODUCT_QLOGIC_ISP8030 << 16) | PCI_VENDOR_QLOGIC)
64 static int qla_alloc_parent_dma_tag(qla_host_t *ha);
65 static void qla_free_parent_dma_tag(qla_host_t *ha);
66 static int qla_alloc_xmt_bufs(qla_host_t *ha);
67 static void qla_free_xmt_bufs(qla_host_t *ha);
68 static int qla_alloc_rcv_bufs(qla_host_t *ha);
69 static void qla_free_rcv_bufs(qla_host_t *ha);
70 static void qla_clear_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb);
72 static void qla_init_ifnet(device_t dev, qla_host_t *ha);
73 static int qla_sysctl_get_link_status(SYSCTL_HANDLER_ARGS);
74 static void qla_release(qla_host_t *ha);
75 static void qla_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs,
77 static void qla_stop(qla_host_t *ha);
78 static void qla_get_peer(qla_host_t *ha);
79 static void qla_error_recovery(void *context, int pending);
80 static void qla_async_event(void *context, int pending);
81 static void qla_stats(void *context, int pending);
82 static int qla_send(qla_host_t *ha, struct mbuf **m_headp, uint32_t txr_idx,
86 * Hooks to the Operating Systems
88 static int qla_pci_probe (device_t);
89 static int qla_pci_attach (device_t);
90 static int qla_pci_detach (device_t);
92 static void qla_init(void *arg);
93 static int qla_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
94 static int qla_media_change(struct ifnet *ifp);
95 static void qla_media_status(struct ifnet *ifp, struct ifmediareq *ifmr);
97 static int qla_transmit(struct ifnet *ifp, struct mbuf *mp);
98 static void qla_qflush(struct ifnet *ifp);
99 static int qla_alloc_tx_br(qla_host_t *ha, qla_tx_fp_t *tx_fp);
100 static void qla_free_tx_br(qla_host_t *ha, qla_tx_fp_t *tx_fp);
101 static int qla_create_fp_taskqueues(qla_host_t *ha);
102 static void qla_destroy_fp_taskqueues(qla_host_t *ha);
103 static void qla_drain_fp_taskqueues(qla_host_t *ha);
105 static device_method_t qla_pci_methods[] = {
106 /* Device interface */
107 DEVMETHOD(device_probe, qla_pci_probe),
108 DEVMETHOD(device_attach, qla_pci_attach),
109 DEVMETHOD(device_detach, qla_pci_detach),
113 static driver_t qla_pci_driver = {
114 "ql", qla_pci_methods, sizeof (qla_host_t),
117 static devclass_t qla83xx_devclass;
119 DRIVER_MODULE(qla83xx, pci, qla_pci_driver, qla83xx_devclass, 0, 0);
121 MODULE_DEPEND(qla83xx, pci, 1, 1, 1);
122 MODULE_DEPEND(qla83xx, ether, 1, 1, 1);
124 MALLOC_DEFINE(M_QLA83XXBUF, "qla83xxbuf", "Buffers for qla83xx driver");
126 #define QL_STD_REPLENISH_THRES 0
127 #define QL_JUMBO_REPLENISH_THRES 32
130 static char dev_str[64];
131 static char ver_str[64];
134 * Name: qla_pci_probe
135 * Function: Validate the PCI device to be a QLA80XX device
138 qla_pci_probe(device_t dev)
140 switch ((pci_get_device(dev) << 16) | (pci_get_vendor(dev))) {
141 case PCI_QLOGIC_ISP8030:
142 snprintf(dev_str, sizeof(dev_str), "%s v%d.%d.%d",
143 "Qlogic ISP 83xx PCI CNA Adapter-Ethernet Function",
144 QLA_VERSION_MAJOR, QLA_VERSION_MINOR,
146 snprintf(ver_str, sizeof(ver_str), "v%d.%d.%d",
147 QLA_VERSION_MAJOR, QLA_VERSION_MINOR,
149 device_set_desc(dev, dev_str);
156 printf("%s: %s\n ", __func__, dev_str);
158 return (BUS_PROBE_DEFAULT);
162 qla_add_sysctls(qla_host_t *ha)
164 device_t dev = ha->pci_dev;
166 SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
167 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
168 OID_AUTO, "version", CTLFLAG_RD,
169 ver_str, 0, "Driver Version");
171 SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
172 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
173 OID_AUTO, "fw_version", CTLFLAG_RD,
174 ha->fw_ver_str, 0, "firmware version");
176 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
177 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
178 OID_AUTO, "link_status", CTLTYPE_INT | CTLFLAG_RW,
180 qla_sysctl_get_link_status, "I", "Link Status");
183 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
184 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
185 OID_AUTO, "debug", CTLFLAG_RW,
186 &ha->dbg_level, ha->dbg_level, "Debug Level");
188 ha->enable_minidump = 1;
189 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
190 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
191 OID_AUTO, "enable_minidump", CTLFLAG_RW,
192 &ha->enable_minidump, ha->enable_minidump,
193 "Minidump retrival prior to error recovery "
194 "is enabled only when this is set");
196 ha->enable_driverstate_dump = 1;
197 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
198 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
199 OID_AUTO, "enable_driverstate_dump", CTLFLAG_RW,
200 &ha->enable_driverstate_dump, ha->enable_driverstate_dump,
201 "Driver State retrival prior to error recovery "
202 "is enabled only when this is set");
204 ha->enable_error_recovery = 1;
205 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
206 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
207 OID_AUTO, "enable_error_recovery", CTLFLAG_RW,
208 &ha->enable_error_recovery, ha->enable_error_recovery,
209 "when set error recovery is enabled on fatal errors "
210 "otherwise the port is turned offline");
212 ha->ms_delay_after_init = 1000;
213 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
214 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
215 OID_AUTO, "ms_delay_after_init", CTLFLAG_RW,
216 &ha->ms_delay_after_init, ha->ms_delay_after_init,
217 "millisecond delay after hw_init");
219 ha->std_replenish = QL_STD_REPLENISH_THRES;
220 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
221 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
222 OID_AUTO, "std_replenish", CTLFLAG_RW,
223 &ha->std_replenish, ha->std_replenish,
224 "Threshold for Replenishing Standard Frames");
226 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
227 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
228 OID_AUTO, "ipv4_lro",
229 CTLFLAG_RD, &ha->ipv4_lro,
230 "number of ipv4 lro completions");
232 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
233 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
234 OID_AUTO, "ipv6_lro",
235 CTLFLAG_RD, &ha->ipv6_lro,
236 "number of ipv6 lro completions");
238 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
239 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
240 OID_AUTO, "tx_tso_frames",
241 CTLFLAG_RD, &ha->tx_tso_frames,
242 "number of Tx TSO Frames");
244 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
245 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
246 OID_AUTO, "hw_vlan_tx_frames",
247 CTLFLAG_RD, &ha->hw_vlan_tx_frames,
248 "number of Tx VLAN Frames");
250 SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
251 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
252 OID_AUTO, "hw_lock_failed",
253 CTLFLAG_RD, &ha->hw_lock_failed,
254 "number of hw_lock failures");
260 qla_watchdog(void *arg)
262 qla_host_t *ha = arg;
269 if (ha->qla_watchdog_exit) {
270 ha->qla_watchdog_exited = 1;
273 ha->qla_watchdog_exited = 0;
275 if (!ha->qla_watchdog_pause) {
277 (ql_hw_check_health(ha) || ha->qla_initiate_recovery ||
278 (ha->msg_from_peer == QL_PEER_MSG_RESET))) {
280 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
281 ql_update_link_state(ha);
283 if (ha->enable_error_recovery) {
284 ha->qla_watchdog_paused = 1;
285 ha->qla_watchdog_pause = 1;
287 device_printf(ha->pci_dev,
288 "%s: taskqueue_enqueue(err_task) \n",
290 taskqueue_enqueue(ha->err_tq, &ha->err_task);
293 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
299 if (ha->qla_interface_up) {
301 ha->watchdog_ticks++;
303 if (ha->watchdog_ticks > 1000)
304 ha->watchdog_ticks = 0;
306 if (!ha->watchdog_ticks && QL_RUNNING(ifp)) {
307 taskqueue_enqueue(ha->stats_tq,
311 if (ha->async_event) {
312 taskqueue_enqueue(ha->async_event_tq,
313 &ha->async_event_task);
317 ha->qla_watchdog_paused = 0;
320 ha->qla_watchdog_paused = 1;
323 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
328 * Name: qla_pci_attach
329 * Function: attaches the device to the operating system
332 qla_pci_attach(device_t dev)
334 qla_host_t *ha = NULL;
337 uint32_t num_rcvq = 0;
339 if ((ha = device_get_softc(dev)) == NULL) {
340 device_printf(dev, "cannot get softc\n");
344 memset(ha, 0, sizeof (qla_host_t));
346 if (pci_get_device(dev) != PCI_PRODUCT_QLOGIC_ISP8030) {
347 device_printf(dev, "device is not ISP8030\n");
351 ha->pci_func = pci_get_function(dev) & 0x1;
355 pci_enable_busmaster(dev);
357 ha->reg_rid = PCIR_BAR(0);
358 ha->pci_reg = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &ha->reg_rid,
361 if (ha->pci_reg == NULL) {
362 device_printf(dev, "unable to map any ports\n");
363 goto qla_pci_attach_err;
366 rsrc_len = (uint32_t) bus_get_resource_count(dev, SYS_RES_MEMORY,
369 mtx_init(&ha->hw_lock, "qla83xx_hw_lock", MTX_NETWORK_LOCK, MTX_DEF);
370 mtx_init(&ha->sp_log_lock, "qla83xx_sp_log_lock", MTX_NETWORK_LOCK, MTX_DEF);
371 ha->flags.lock_init = 1;
375 ha->hw.num_sds_rings = MAX_SDS_RINGS;
376 ha->hw.num_rds_rings = MAX_RDS_RINGS;
377 ha->hw.num_tx_rings = NUM_TX_RINGS;
379 ha->reg_rid1 = PCIR_BAR(2);
380 ha->pci_reg1 = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
381 &ha->reg_rid1, RF_ACTIVE);
383 ha->msix_count = pci_msix_count(dev);
385 if (ha->msix_count < 1 ) {
386 device_printf(dev, "%s: msix_count[%d] not enough\n", __func__,
388 goto qla_pci_attach_err;
391 if (ha->msix_count < (ha->hw.num_sds_rings + 1)) {
392 ha->hw.num_sds_rings = ha->msix_count - 1;
395 QL_DPRINT2(ha, (dev, "%s: ha %p pci_func 0x%x rsrc_count 0x%08x"
396 " msix_count 0x%x pci_reg %p pci_reg1 %p\n", __func__, ha,
397 ha->pci_func, rsrc_len, ha->msix_count, ha->pci_reg,
400 /* initialize hardware */
401 if (ql_init_hw(ha)) {
402 device_printf(dev, "%s: ql_init_hw failed\n", __func__);
403 goto qla_pci_attach_err;
406 device_printf(dev, "%s: firmware[%d.%d.%d.%d]\n", __func__,
407 ha->fw_ver_major, ha->fw_ver_minor, ha->fw_ver_sub,
409 snprintf(ha->fw_ver_str, sizeof(ha->fw_ver_str), "%d.%d.%d.%d",
410 ha->fw_ver_major, ha->fw_ver_minor, ha->fw_ver_sub,
413 if (qla_get_nic_partition(ha, NULL, &num_rcvq)) {
414 device_printf(dev, "%s: qla_get_nic_partition failed\n",
416 goto qla_pci_attach_err;
418 device_printf(dev, "%s: ha %p pci_func 0x%x rsrc_count 0x%08x"
419 " msix_count 0x%x pci_reg %p pci_reg1 %p num_rcvq = %d\n",
420 __func__, ha, ha->pci_func, rsrc_len, ha->msix_count,
421 ha->pci_reg, ha->pci_reg1, num_rcvq);
423 if ((ha->msix_count < 64) || (num_rcvq != 32)) {
424 if (ha->hw.num_sds_rings > 15) {
425 ha->hw.num_sds_rings = 15;
429 ha->hw.num_rds_rings = ha->hw.num_sds_rings;
430 ha->hw.num_tx_rings = ha->hw.num_sds_rings;
432 #ifdef QL_ENABLE_ISCSI_TLV
433 ha->hw.num_tx_rings = ha->hw.num_sds_rings * 2;
434 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
436 ql_hw_add_sysctls(ha);
438 ha->msix_count = ha->hw.num_sds_rings + 1;
440 if (pci_alloc_msix(dev, &ha->msix_count)) {
441 device_printf(dev, "%s: pci_alloc_msi[%d] failed\n", __func__,
444 goto qla_pci_attach_err;
448 ha->mbx_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
450 (RF_ACTIVE | RF_SHAREABLE));
451 if (ha->mbx_irq == NULL) {
452 device_printf(dev, "could not allocate mbx interrupt\n");
453 goto qla_pci_attach_err;
455 if (bus_setup_intr(dev, ha->mbx_irq, (INTR_TYPE_NET | INTR_MPSAFE),
456 NULL, ql_mbx_isr, ha, &ha->mbx_handle)) {
457 device_printf(dev, "could not setup mbx interrupt\n");
458 goto qla_pci_attach_err;
461 for (i = 0; i < ha->hw.num_sds_rings; i++) {
462 ha->irq_vec[i].sds_idx = i;
463 ha->irq_vec[i].ha = ha;
464 ha->irq_vec[i].irq_rid = 2 + i;
466 ha->irq_vec[i].irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
467 &ha->irq_vec[i].irq_rid,
468 (RF_ACTIVE | RF_SHAREABLE));
470 if (ha->irq_vec[i].irq == NULL) {
471 device_printf(dev, "could not allocate interrupt\n");
472 goto qla_pci_attach_err;
474 if (bus_setup_intr(dev, ha->irq_vec[i].irq,
475 (INTR_TYPE_NET | INTR_MPSAFE),
476 NULL, ql_isr, &ha->irq_vec[i],
477 &ha->irq_vec[i].handle)) {
478 device_printf(dev, "could not setup interrupt\n");
479 goto qla_pci_attach_err;
482 ha->tx_fp[i].ha = ha;
483 ha->tx_fp[i].txr_idx = i;
485 if (qla_alloc_tx_br(ha, &ha->tx_fp[i])) {
486 device_printf(dev, "%s: could not allocate tx_br[%d]\n",
488 goto qla_pci_attach_err;
492 if (qla_create_fp_taskqueues(ha) != 0)
493 goto qla_pci_attach_err;
495 printf("%s: mp__ncpus %d sds %d rds %d msi-x %d\n", __func__, mp_ncpus,
496 ha->hw.num_sds_rings, ha->hw.num_rds_rings, ha->msix_count);
498 ql_read_mac_addr(ha);
500 /* allocate parent dma tag */
501 if (qla_alloc_parent_dma_tag(ha)) {
502 device_printf(dev, "%s: qla_alloc_parent_dma_tag failed\n",
504 goto qla_pci_attach_err;
507 /* alloc all dma buffers */
508 if (ql_alloc_dma(ha)) {
509 device_printf(dev, "%s: ql_alloc_dma failed\n", __func__);
510 goto qla_pci_attach_err;
514 if (ql_minidump_init(ha) != 0) {
515 device_printf(dev, "%s: ql_minidump_init failed\n", __func__);
516 goto qla_pci_attach_err;
518 ql_alloc_drvr_state_buffer(ha);
519 ql_alloc_sp_log_buffer(ha);
520 /* create the o.s ethernet interface */
521 qla_init_ifnet(dev, ha);
523 ha->flags.qla_watchdog_active = 1;
524 ha->qla_watchdog_pause = 0;
526 callout_init(&ha->tx_callout, TRUE);
527 ha->flags.qla_callout_init = 1;
529 /* create ioctl device interface */
530 if (ql_make_cdev(ha)) {
531 device_printf(dev, "%s: ql_make_cdev failed\n", __func__);
532 goto qla_pci_attach_err;
535 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
538 TASK_INIT(&ha->err_task, 0, qla_error_recovery, ha);
539 ha->err_tq = taskqueue_create("qla_errq", M_NOWAIT,
540 taskqueue_thread_enqueue, &ha->err_tq);
541 taskqueue_start_threads(&ha->err_tq, 1, PI_NET, "%s errq",
542 device_get_nameunit(ha->pci_dev));
544 TASK_INIT(&ha->async_event_task, 0, qla_async_event, ha);
545 ha->async_event_tq = taskqueue_create("qla_asyncq", M_NOWAIT,
546 taskqueue_thread_enqueue, &ha->async_event_tq);
547 taskqueue_start_threads(&ha->async_event_tq, 1, PI_NET, "%s asyncq",
548 device_get_nameunit(ha->pci_dev));
550 TASK_INIT(&ha->stats_task, 0, qla_stats, ha);
551 ha->stats_tq = taskqueue_create("qla_statsq", M_NOWAIT,
552 taskqueue_thread_enqueue, &ha->stats_tq);
553 taskqueue_start_threads(&ha->stats_tq, 1, PI_NET, "%s taskq",
554 device_get_nameunit(ha->pci_dev));
556 QL_DPRINT2(ha, (dev, "%s: exit 0\n", __func__));
563 if (ha->flags.lock_init) {
564 mtx_destroy(&ha->hw_lock);
565 mtx_destroy(&ha->sp_log_lock);
568 QL_DPRINT2(ha, (dev, "%s: exit ENXIO\n", __func__));
573 * Name: qla_pci_detach
574 * Function: Unhooks the device from the operating system
577 qla_pci_detach(device_t dev)
579 qla_host_t *ha = NULL;
583 if ((ha = device_get_softc(dev)) == NULL) {
584 device_printf(dev, "cannot get softc\n");
588 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
592 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
593 QLA_LOCK(ha, __func__, -1, 0);
595 ha->qla_detach_active = 1;
600 QLA_UNLOCK(ha, __func__);
602 if (ha->flags.lock_init) {
603 mtx_destroy(&ha->hw_lock);
604 mtx_destroy(&ha->sp_log_lock);
607 QL_DPRINT2(ha, (dev, "%s: exit\n", __func__));
613 * SYSCTL Related Callbacks
616 qla_sysctl_get_link_status(SYSCTL_HANDLER_ARGS)
621 err = sysctl_handle_int(oidp, &ret, 0, req);
623 if (err || !req->newptr)
627 ha = (qla_host_t *)arg1;
628 ql_hw_link_status(ha);
635 * Function: Releases the resources allocated for the device
638 qla_release(qla_host_t *ha)
645 if (ha->async_event_tq) {
646 taskqueue_drain_all(ha->async_event_tq);
647 taskqueue_free(ha->async_event_tq);
651 taskqueue_drain_all(ha->err_tq);
652 taskqueue_free(ha->err_tq);
656 taskqueue_drain_all(ha->stats_tq);
657 taskqueue_free(ha->stats_tq);
662 if (ha->flags.qla_watchdog_active) {
663 ha->qla_watchdog_exit = 1;
665 while (ha->qla_watchdog_exited == 0)
666 qla_mdelay(__func__, 1);
669 if (ha->flags.qla_callout_init)
670 callout_stop(&ha->tx_callout);
673 ether_ifdetach(ha->ifp);
675 ql_free_drvr_state_buffer(ha);
676 ql_free_sp_log_buffer(ha);
678 qla_free_parent_dma_tag(ha);
681 (void)bus_teardown_intr(dev, ha->mbx_irq, ha->mbx_handle);
684 (void) bus_release_resource(dev, SYS_RES_IRQ, ha->mbx_irq_rid,
687 for (i = 0; i < ha->hw.num_sds_rings; i++) {
689 if (ha->irq_vec[i].handle) {
690 (void)bus_teardown_intr(dev, ha->irq_vec[i].irq,
691 ha->irq_vec[i].handle);
694 if (ha->irq_vec[i].irq) {
695 (void)bus_release_resource(dev, SYS_RES_IRQ,
696 ha->irq_vec[i].irq_rid,
700 qla_free_tx_br(ha, &ha->tx_fp[i]);
702 qla_destroy_fp_taskqueues(ha);
705 pci_release_msi(dev);
708 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid,
712 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid1,
719 * DMA Related Functions
723 qla_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
725 *((bus_addr_t *)arg) = 0;
728 printf("%s: bus_dmamap_load failed (%d)\n", __func__, error);
732 *((bus_addr_t *)arg) = segs[0].ds_addr;
738 ql_alloc_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
746 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
748 ret = bus_dma_tag_create(
749 ha->parent_tag,/* parent */
751 ((bus_size_t)(1ULL << 32)),/* boundary */
752 BUS_SPACE_MAXADDR, /* lowaddr */
753 BUS_SPACE_MAXADDR, /* highaddr */
754 NULL, NULL, /* filter, filterarg */
755 dma_buf->size, /* maxsize */
757 dma_buf->size, /* maxsegsize */
759 NULL, NULL, /* lockfunc, lockarg */
763 device_printf(dev, "%s: could not create dma tag\n", __func__);
764 goto ql_alloc_dmabuf_exit;
766 ret = bus_dmamem_alloc(dma_buf->dma_tag,
767 (void **)&dma_buf->dma_b,
768 (BUS_DMA_ZERO | BUS_DMA_COHERENT | BUS_DMA_NOWAIT),
771 bus_dma_tag_destroy(dma_buf->dma_tag);
772 device_printf(dev, "%s: bus_dmamem_alloc failed\n", __func__);
773 goto ql_alloc_dmabuf_exit;
776 ret = bus_dmamap_load(dma_buf->dma_tag,
781 &b_addr, BUS_DMA_NOWAIT);
783 if (ret || !b_addr) {
784 bus_dma_tag_destroy(dma_buf->dma_tag);
785 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b,
788 goto ql_alloc_dmabuf_exit;
791 dma_buf->dma_addr = b_addr;
793 ql_alloc_dmabuf_exit:
794 QL_DPRINT2(ha, (dev, "%s: exit ret 0x%08x tag %p map %p b %p sz 0x%x\n",
795 __func__, ret, (void *)dma_buf->dma_tag,
796 (void *)dma_buf->dma_map, (void *)dma_buf->dma_b,
803 ql_free_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
805 bus_dmamap_unload(dma_buf->dma_tag, dma_buf->dma_map);
806 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b, dma_buf->dma_map);
807 bus_dma_tag_destroy(dma_buf->dma_tag);
811 qla_alloc_parent_dma_tag(qla_host_t *ha)
819 * Allocate parent DMA Tag
821 ret = bus_dma_tag_create(
822 bus_get_dma_tag(dev), /* parent */
823 1,((bus_size_t)(1ULL << 32)),/* alignment, boundary */
824 BUS_SPACE_MAXADDR, /* lowaddr */
825 BUS_SPACE_MAXADDR, /* highaddr */
826 NULL, NULL, /* filter, filterarg */
827 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
829 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
831 NULL, NULL, /* lockfunc, lockarg */
835 device_printf(dev, "%s: could not create parent dma tag\n",
840 ha->flags.parent_tag = 1;
846 qla_free_parent_dma_tag(qla_host_t *ha)
848 if (ha->flags.parent_tag) {
849 bus_dma_tag_destroy(ha->parent_tag);
850 ha->flags.parent_tag = 0;
855 * Name: qla_init_ifnet
856 * Function: Creates the Network Device Interface and Registers it with the O.S
860 qla_init_ifnet(device_t dev, qla_host_t *ha)
864 QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
866 ifp = ha->ifp = if_alloc(IFT_ETHER);
869 panic("%s: cannot if_alloc()\n", device_get_nameunit(dev));
871 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
873 #if __FreeBSD_version >= 1000000
874 if_initbaudrate(ifp, IF_Gbps(10));
875 ifp->if_capabilities = IFCAP_LINKSTATE;
877 ifp->if_mtu = ETHERMTU;
878 ifp->if_baudrate = (1 * 1000 * 1000 *1000);
880 #endif /* #if __FreeBSD_version >= 1000000 */
882 ifp->if_init = qla_init;
884 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
885 ifp->if_ioctl = qla_ioctl;
887 ifp->if_transmit = qla_transmit;
888 ifp->if_qflush = qla_qflush;
890 IFQ_SET_MAXLEN(&ifp->if_snd, qla_get_ifq_snd_maxlen(ha));
891 ifp->if_snd.ifq_drv_maxlen = qla_get_ifq_snd_maxlen(ha);
892 IFQ_SET_READY(&ifp->if_snd);
894 ha->max_frame_size = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
896 ether_ifattach(ifp, qla_get_mac_addr(ha));
898 ifp->if_capabilities |= IFCAP_HWCSUM |
902 IFCAP_VLAN_HWTAGGING |
907 ifp->if_capenable = ifp->if_capabilities;
909 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
911 ifmedia_init(&ha->media, IFM_IMASK, qla_media_change, qla_media_status);
913 ifmedia_add(&ha->media, (IFM_ETHER | qla_get_optics(ha) | IFM_FDX), 0,
915 ifmedia_add(&ha->media, (IFM_ETHER | IFM_AUTO), 0, NULL);
917 ifmedia_set(&ha->media, (IFM_ETHER | IFM_AUTO));
919 QL_DPRINT2(ha, (dev, "%s: exit\n", __func__));
925 qla_init_locked(qla_host_t *ha)
927 struct ifnet *ifp = ha->ifp;
929 ql_sp_log(ha, 14, 0, 0, 0, 0, 0, 0);
933 if (qla_alloc_xmt_bufs(ha) != 0)
936 qla_confirm_9kb_enable(ha);
938 if (qla_alloc_rcv_bufs(ha) != 0)
941 bcopy(IF_LLADDR(ha->ifp), ha->hw.mac_addr, ETHER_ADDR_LEN);
943 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_TSO;
944 ifp->if_hwassist |= CSUM_TCP_IPV6 | CSUM_UDP_IPV6;
947 if (ql_init_hw_if(ha) == 0) {
949 ifp->if_drv_flags |= IFF_DRV_RUNNING;
950 ha->hw_vlan_tx_frames = 0;
951 ha->tx_tso_frames = 0;
952 ha->qla_interface_up = 1;
953 ql_update_link_state(ha);
955 if (ha->hw.sp_log_stop_events & Q8_SP_LOG_STOP_IF_START_FAILURE)
956 ha->hw.sp_log_stop = -1;
959 ha->qla_watchdog_pause = 0;
969 ha = (qla_host_t *)arg;
971 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
973 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
978 QLA_UNLOCK(ha, __func__);
980 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
984 qla_set_multi(qla_host_t *ha, uint32_t add_multi)
986 uint8_t mta[Q8_MAX_NUM_MULTICAST_ADDRS * Q8_MAC_ADDR_LEN];
987 struct ifmultiaddr *ifma;
989 struct ifnet *ifp = ha->ifp;
994 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
996 if (ifma->ifma_addr->sa_family != AF_LINK)
999 if (mcnt == Q8_MAX_NUM_MULTICAST_ADDRS)
1002 bcopy(LLADDR((struct sockaddr_dl *) ifma->ifma_addr),
1003 &mta[mcnt * Q8_MAC_ADDR_LEN], Q8_MAC_ADDR_LEN);
1008 if_maddr_runlock(ifp);
1010 if (QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1011 QLA_LOCK_NO_SLEEP) != 0)
1014 ql_sp_log(ha, 12, 4, ifp->if_drv_flags,
1015 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1016 add_multi, (uint32_t)mcnt, 0);
1018 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1021 ret = qla_hw_del_all_mcast(ha);
1024 device_printf(ha->pci_dev,
1025 "%s: qla_hw_del_all_mcast() failed\n",
1030 ret = ql_hw_set_multi(ha, mta, mcnt, 1);
1034 QLA_UNLOCK(ha, __func__);
1040 qla_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1043 struct ifreq *ifr = (struct ifreq *)data;
1044 struct ifaddr *ifa = (struct ifaddr *)data;
1047 ha = (qla_host_t *)ifp->if_softc;
1048 if (ha->offline || ha->qla_initiate_recovery)
1053 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFADDR (0x%lx)\n",
1056 if (ifa->ifa_addr->sa_family == AF_INET) {
1058 ret = QLA_LOCK(ha, __func__,
1059 QLA_LOCK_DEFAULT_MS_TIMEOUT,
1064 ifp->if_flags |= IFF_UP;
1066 ql_sp_log(ha, 8, 3, ifp->if_drv_flags,
1067 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1068 ntohl(IA_SIN(ifa)->sin_addr.s_addr), 0, 0);
1070 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1071 qla_init_locked(ha);
1074 QLA_UNLOCK(ha, __func__);
1075 QL_DPRINT4(ha, (ha->pci_dev,
1076 "%s: SIOCSIFADDR (0x%lx) ipv4 [0x%08x]\n",
1078 ntohl(IA_SIN(ifa)->sin_addr.s_addr)));
1080 arp_ifinit(ifp, ifa);
1082 ether_ioctl(ifp, cmd, data);
1087 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFMTU (0x%lx)\n",
1090 if (ifr->ifr_mtu > QLA_MAX_MTU) {
1093 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1099 ifp->if_mtu = ifr->ifr_mtu;
1100 ha->max_frame_size =
1101 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
1103 ql_sp_log(ha, 9, 4, ifp->if_drv_flags,
1104 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1105 ha->max_frame_size, ifp->if_mtu, 0);
1107 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1108 qla_init_locked(ha);
1111 if (ifp->if_mtu > ETHERMTU)
1112 ha->std_replenish = QL_JUMBO_REPLENISH_THRES;
1114 ha->std_replenish = QL_STD_REPLENISH_THRES;
1117 QLA_UNLOCK(ha, __func__);
1123 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFFLAGS (0x%lx)\n",
1126 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1132 ql_sp_log(ha, 10, 4, ifp->if_drv_flags,
1133 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1134 ha->if_flags, ifp->if_flags, 0);
1136 if (ifp->if_flags & IFF_UP) {
1138 ha->max_frame_size = ifp->if_mtu +
1139 ETHER_HDR_LEN + ETHER_CRC_LEN;
1140 qla_init_locked(ha);
1142 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1143 if ((ifp->if_flags ^ ha->if_flags) &
1145 ret = ql_set_promisc(ha);
1146 } else if ((ifp->if_flags ^ ha->if_flags) &
1148 ret = ql_set_allmulti(ha);
1152 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1154 ha->if_flags = ifp->if_flags;
1157 QLA_UNLOCK(ha, __func__);
1161 QL_DPRINT4(ha, (ha->pci_dev,
1162 "%s: %s (0x%lx)\n", __func__, "SIOCADDMULTI", cmd));
1164 if (qla_set_multi(ha, 1))
1169 QL_DPRINT4(ha, (ha->pci_dev,
1170 "%s: %s (0x%lx)\n", __func__, "SIOCDELMULTI", cmd));
1172 if (qla_set_multi(ha, 0))
1178 QL_DPRINT4(ha, (ha->pci_dev,
1179 "%s: SIOCSIFMEDIA/SIOCGIFMEDIA (0x%lx)\n",
1181 ret = ifmedia_ioctl(ifp, ifr, &ha->media, cmd);
1186 int mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1188 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFCAP (0x%lx)\n",
1191 if (mask & IFCAP_HWCSUM)
1192 ifp->if_capenable ^= IFCAP_HWCSUM;
1193 if (mask & IFCAP_TSO4)
1194 ifp->if_capenable ^= IFCAP_TSO4;
1195 if (mask & IFCAP_TSO6)
1196 ifp->if_capenable ^= IFCAP_TSO6;
1197 if (mask & IFCAP_VLAN_HWTAGGING)
1198 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1199 if (mask & IFCAP_VLAN_HWTSO)
1200 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1201 if (mask & IFCAP_LRO)
1202 ifp->if_capenable ^= IFCAP_LRO;
1204 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1205 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1211 ql_sp_log(ha, 11, 4, ifp->if_drv_flags,
1212 (ifp->if_drv_flags & IFF_DRV_RUNNING),
1213 mask, ifp->if_capenable, 0);
1215 qla_init_locked(ha);
1217 QLA_UNLOCK(ha, __func__);
1220 VLAN_CAPABILITIES(ifp);
1225 QL_DPRINT4(ha, (ha->pci_dev, "%s: default (0x%lx)\n",
1227 ret = ether_ioctl(ifp, cmd, data);
1235 qla_media_change(struct ifnet *ifp)
1238 struct ifmedia *ifm;
1241 ha = (qla_host_t *)ifp->if_softc;
1243 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1247 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1250 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1256 qla_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1260 ha = (qla_host_t *)ifp->if_softc;
1262 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1264 ifmr->ifm_status = IFM_AVALID;
1265 ifmr->ifm_active = IFM_ETHER;
1267 ql_update_link_state(ha);
1268 if (ha->hw.link_up) {
1269 ifmr->ifm_status |= IFM_ACTIVE;
1270 ifmr->ifm_active |= (IFM_FDX | qla_get_optics(ha));
1273 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit (%s)\n", __func__,\
1274 (ha->hw.link_up ? "link_up" : "link_down")));
1281 qla_send(qla_host_t *ha, struct mbuf **m_headp, uint32_t txr_idx,
1284 bus_dma_segment_t segs[QLA_MAX_SEGMENTS];
1289 struct mbuf *m_head = *m_headp;
1291 QL_DPRINT8(ha, (ha->pci_dev, "%s: enter\n", __func__));
1293 if (m_head->m_flags & M_FLOWID) {
1294 #ifdef QL_ENABLE_ISCSI_TLV
1295 if (qla_iscsi_pdu(ha, m_head) == 0) {
1297 txr_idx = m_head->m_pkthdr.flowid &
1298 ((ha->hw.num_tx_rings >> 1) - 1);
1300 txr_idx = m_head->m_pkthdr.flowid &
1301 (ha->hw.num_tx_rings - 1);
1304 txr_idx = m_head->m_pkthdr.flowid & (ha->hw.num_tx_rings - 1);
1305 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1309 tx_idx = ha->hw.tx_cntxt[txr_idx].txr_next;
1311 if ((NULL != ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head) ||
1312 (QL_ERR_INJECT(ha, INJCT_TXBUF_MBUF_NON_NULL))){
1313 QL_ASSERT(ha, 0, ("%s [%d]: txr_idx = %d tx_idx = %d "\
1314 "mbuf = %p\n", __func__, __LINE__, txr_idx, tx_idx,\
1315 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head));
1317 device_printf(ha->pci_dev, "%s [%d]: txr_idx = %d tx_idx = %d "
1318 "mbuf = %p\n", __func__, __LINE__, txr_idx, tx_idx,
1319 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head);
1324 QL_INITIATE_RECOVERY(ha);
1328 map = ha->tx_ring[txr_idx].tx_buf[tx_idx].map;
1330 ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head, segs, &nsegs,
1337 QL_DPRINT8(ha, (ha->pci_dev, "%s: EFBIG [%d]\n", __func__,
1338 m_head->m_pkthdr.len));
1340 m = m_defrag(m_head, M_NOWAIT);
1342 ha->err_tx_defrag++;
1345 device_printf(ha->pci_dev,
1346 "%s: m_defrag() = NULL [%d]\n",
1353 if ((ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head,
1354 segs, &nsegs, BUS_DMA_NOWAIT))) {
1356 ha->err_tx_dmamap_load++;
1358 device_printf(ha->pci_dev,
1359 "%s: bus_dmamap_load_mbuf_sg failed0[%d, %d]\n",
1360 __func__, ret, m_head->m_pkthdr.len);
1362 if (ret != ENOMEM) {
1371 ha->err_tx_dmamap_load++;
1373 device_printf(ha->pci_dev,
1374 "%s: bus_dmamap_load_mbuf_sg failed1[%d, %d]\n",
1375 __func__, ret, m_head->m_pkthdr.len);
1377 if (ret != ENOMEM) {
1384 QL_ASSERT(ha, (nsegs != 0), ("qla_send: empty packet"));
1386 bus_dmamap_sync(ha->tx_tag, map, BUS_DMASYNC_PREWRITE);
1388 if (!(ret = ql_hw_send(ha, segs, nsegs, tx_idx, m_head, txr_idx,
1390 ha->tx_ring[txr_idx].count++;
1392 ha->tx_ring[txr_idx].iscsi_pkt_count++;
1393 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head = m_head;
1395 bus_dmamap_unload(ha->tx_tag, map);
1396 if (ret == EINVAL) {
1403 QL_DPRINT8(ha, (ha->pci_dev, "%s: exit\n", __func__));
1408 qla_alloc_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1410 snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
1411 "qla%d_fp%d_tx_mq_lock", ha->pci_func, fp->txr_idx);
1413 mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
1415 fp->tx_br = buf_ring_alloc(NUM_TX_DESCRIPTORS, M_DEVBUF,
1416 M_NOWAIT, &fp->tx_mtx);
1417 if (fp->tx_br == NULL) {
1418 QL_DPRINT1(ha, (ha->pci_dev, "buf_ring_alloc failed for "
1419 " fp[%d, %d]\n", ha->pci_func, fp->txr_idx));
1426 qla_free_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1429 struct ifnet *ifp = ha->ifp;
1431 if (mtx_initialized(&fp->tx_mtx)) {
1433 if (fp->tx_br != NULL) {
1435 mtx_lock(&fp->tx_mtx);
1437 while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1441 mtx_unlock(&fp->tx_mtx);
1443 buf_ring_free(fp->tx_br, M_DEVBUF);
1446 mtx_destroy(&fp->tx_mtx);
1452 qla_fp_taskqueue(void *context, int pending)
1457 struct mbuf *mp = NULL;
1460 uint32_t iscsi_pdu = 0;
1461 uint32_t rx_pkts_left = -1;
1468 ha = (qla_host_t *)fp->ha;
1472 txr_idx = fp->txr_idx;
1474 mtx_lock(&fp->tx_mtx);
1476 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING) || (!ha->hw.link_up)) {
1477 mtx_unlock(&fp->tx_mtx);
1478 goto qla_fp_taskqueue_exit;
1481 while (rx_pkts_left && !ha->stop_rcv &&
1482 (ifp->if_drv_flags & IFF_DRV_RUNNING) && ha->hw.link_up) {
1483 rx_pkts_left = ql_rcv_isr(ha, fp->txr_idx, 64);
1485 #ifdef QL_ENABLE_ISCSI_TLV
1486 ql_hw_tx_done_locked(ha, fp->txr_idx);
1487 ql_hw_tx_done_locked(ha, (fp->txr_idx + (ha->hw.num_tx_rings >> 1)));
1489 ql_hw_tx_done_locked(ha, fp->txr_idx);
1490 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1492 mp = drbr_peek(ifp, fp->tx_br);
1494 while (mp != NULL) {
1496 if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE) {
1497 #ifdef QL_ENABLE_ISCSI_TLV
1498 if (ql_iscsi_pdu(ha, mp) == 0) {
1500 (ha->hw.num_tx_rings >> 1);
1504 txr_idx = fp->txr_idx;
1506 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1509 ret = qla_send(ha, &mp, txr_idx, iscsi_pdu);
1513 drbr_putback(ifp, fp->tx_br, mp);
1515 drbr_advance(ifp, fp->tx_br);
1518 mtx_unlock(&fp->tx_mtx);
1520 goto qla_fp_taskqueue_exit0;
1522 drbr_advance(ifp, fp->tx_br);
1525 /* Send a copy of the frame to the BPF listener */
1526 ETHER_BPF_MTAP(ifp, mp);
1528 if (((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) ||
1532 mp = drbr_peek(ifp, fp->tx_br);
1535 mtx_unlock(&fp->tx_mtx);
1537 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1538 goto qla_fp_taskqueue_exit;
1540 qla_fp_taskqueue_exit0:
1542 if (rx_pkts_left || ((mp != NULL) && ret)) {
1543 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1545 if (!ha->stop_rcv) {
1546 QL_ENABLE_INTERRUPTS(ha, fp->txr_idx);
1550 qla_fp_taskqueue_exit:
1552 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1557 qla_create_fp_taskqueues(qla_host_t *ha)
1560 uint8_t tq_name[32];
1562 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1564 qla_tx_fp_t *fp = &ha->tx_fp[i];
1566 bzero(tq_name, sizeof (tq_name));
1567 snprintf(tq_name, sizeof (tq_name), "ql_fp_tq_%d", i);
1569 TASK_INIT(&fp->fp_task, 0, qla_fp_taskqueue, fp);
1571 fp->fp_taskqueue = taskqueue_create_fast(tq_name, M_NOWAIT,
1572 taskqueue_thread_enqueue,
1575 if (fp->fp_taskqueue == NULL)
1578 taskqueue_start_threads(&fp->fp_taskqueue, 1, PI_NET, "%s",
1581 QL_DPRINT1(ha, (ha->pci_dev, "%s: %p\n", __func__,
1589 qla_destroy_fp_taskqueues(qla_host_t *ha)
1593 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1595 qla_tx_fp_t *fp = &ha->tx_fp[i];
1597 if (fp->fp_taskqueue != NULL) {
1598 taskqueue_drain_all(fp->fp_taskqueue);
1599 taskqueue_free(fp->fp_taskqueue);
1600 fp->fp_taskqueue = NULL;
1607 qla_drain_fp_taskqueues(qla_host_t *ha)
1611 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1612 qla_tx_fp_t *fp = &ha->tx_fp[i];
1614 if (fp->fp_taskqueue != NULL) {
1615 taskqueue_drain_all(fp->fp_taskqueue);
1622 qla_transmit(struct ifnet *ifp, struct mbuf *mp)
1624 qla_host_t *ha = (qla_host_t *)ifp->if_softc;
1629 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1631 #if __FreeBSD_version >= 1100000
1632 if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE)
1634 if (mp->m_flags & M_FLOWID)
1636 rss_id = (mp->m_pkthdr.flowid & Q8_RSS_IND_TBL_MAX_IDX) %
1637 ha->hw.num_sds_rings;
1638 fp = &ha->tx_fp[rss_id];
1640 if (fp->tx_br == NULL) {
1642 goto qla_transmit_exit;
1646 ret = drbr_enqueue(ifp, fp->tx_br, mp);
1649 if (fp->fp_taskqueue != NULL)
1650 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1656 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1661 qla_qflush(struct ifnet *ifp)
1668 ha = (qla_host_t *)ifp->if_softc;
1670 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1672 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1680 mtx_lock(&fp->tx_mtx);
1682 while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1685 mtx_unlock(&fp->tx_mtx);
1688 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1694 qla_stop(qla_host_t *ha)
1696 struct ifnet *ifp = ha->ifp;
1700 ql_sp_log(ha, 13, 0, 0, 0, 0, 0, 0);
1704 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1705 ha->qla_watchdog_pause = 1;
1707 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1715 if (fp->tx_br != NULL) {
1716 mtx_lock(&fp->tx_mtx);
1717 mtx_unlock(&fp->tx_mtx);
1721 while (!ha->qla_watchdog_paused)
1722 qla_mdelay(__func__, 1);
1724 ha->qla_interface_up = 0;
1726 qla_drain_fp_taskqueues(ha);
1730 qla_free_xmt_bufs(ha);
1731 qla_free_rcv_bufs(ha);
1737 * Buffer Management Functions for Transmit and Receive Rings
1740 qla_alloc_xmt_bufs(qla_host_t *ha)
1746 if (bus_dma_tag_create(NULL, /* parent */
1747 1, 0, /* alignment, bounds */
1748 BUS_SPACE_MAXADDR, /* lowaddr */
1749 BUS_SPACE_MAXADDR, /* highaddr */
1750 NULL, NULL, /* filter, filterarg */
1751 QLA_MAX_TSO_FRAME_SIZE, /* maxsize */
1752 QLA_MAX_SEGMENTS, /* nsegments */
1753 PAGE_SIZE, /* maxsegsize */
1754 BUS_DMA_ALLOCNOW, /* flags */
1755 NULL, /* lockfunc */
1756 NULL, /* lockfuncarg */
1758 device_printf(ha->pci_dev, "%s: tx_tag alloc failed\n",
1763 for (i = 0; i < ha->hw.num_tx_rings; i++) {
1764 bzero((void *)ha->tx_ring[i].tx_buf,
1765 (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1768 for (j = 0; j < ha->hw.num_tx_rings; j++) {
1769 for (i = 0; i < NUM_TX_DESCRIPTORS; i++) {
1771 txb = &ha->tx_ring[j].tx_buf[i];
1773 if ((ret = bus_dmamap_create(ha->tx_tag,
1774 BUS_DMA_NOWAIT, &txb->map))) {
1776 ha->err_tx_dmamap_create++;
1777 device_printf(ha->pci_dev,
1778 "%s: bus_dmamap_create failed[%d]\n",
1781 qla_free_xmt_bufs(ha);
1792 * Release mbuf after it sent on the wire
1795 qla_clear_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb)
1797 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1800 bus_dmamap_sync(ha->tx_tag, txb->map,
1801 BUS_DMASYNC_POSTWRITE);
1803 bus_dmamap_unload(ha->tx_tag, txb->map);
1805 m_freem(txb->m_head);
1808 bus_dmamap_destroy(ha->tx_tag, txb->map);
1813 bus_dmamap_unload(ha->tx_tag, txb->map);
1814 bus_dmamap_destroy(ha->tx_tag, txb->map);
1818 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1822 qla_free_xmt_bufs(qla_host_t *ha)
1826 for (j = 0; j < ha->hw.num_tx_rings; j++) {
1827 for (i = 0; i < NUM_TX_DESCRIPTORS; i++)
1828 qla_clear_tx_buf(ha, &ha->tx_ring[j].tx_buf[i]);
1831 if (ha->tx_tag != NULL) {
1832 bus_dma_tag_destroy(ha->tx_tag);
1836 for (i = 0; i < ha->hw.num_tx_rings; i++) {
1837 bzero((void *)ha->tx_ring[i].tx_buf,
1838 (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1845 qla_alloc_rcv_std(qla_host_t *ha)
1847 int i, j, k, r, ret = 0;
1849 qla_rx_ring_t *rx_ring;
1851 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1853 rx_ring = &ha->rx_ring[r];
1855 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1857 rxb = &rx_ring->rx_buf[i];
1859 ret = bus_dmamap_create(ha->rx_tag, BUS_DMA_NOWAIT,
1863 device_printf(ha->pci_dev,
1864 "%s: dmamap[%d, %d] failed\n",
1867 for (k = 0; k < r; k++) {
1868 for (j = 0; j < NUM_RX_DESCRIPTORS;
1870 rxb = &ha->rx_ring[k].rx_buf[j];
1871 bus_dmamap_destroy(ha->rx_tag,
1876 for (j = 0; j < i; j++) {
1877 bus_dmamap_destroy(ha->rx_tag,
1878 rx_ring->rx_buf[j].map);
1880 goto qla_alloc_rcv_std_err;
1885 qla_init_hw_rcv_descriptors(ha);
1888 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1890 rx_ring = &ha->rx_ring[r];
1892 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1893 rxb = &rx_ring->rx_buf[i];
1895 if (!(ret = ql_get_mbuf(ha, rxb, NULL))) {
1897 * set the physical address in the
1898 * corresponding descriptor entry in the
1899 * receive ring/queue for the hba
1901 qla_set_hw_rcv_desc(ha, r, i, rxb->handle,
1903 (rxb->m_head)->m_pkthdr.len);
1905 device_printf(ha->pci_dev,
1906 "%s: ql_get_mbuf [%d, %d] failed\n",
1908 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1909 goto qla_alloc_rcv_std_err;
1915 qla_alloc_rcv_std_err:
1920 qla_free_rcv_std(qla_host_t *ha)
1925 for (r = 0; r < ha->hw.num_rds_rings; r++) {
1926 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1927 rxb = &ha->rx_ring[r].rx_buf[i];
1928 if (rxb->m_head != NULL) {
1929 bus_dmamap_unload(ha->rx_tag, rxb->map);
1930 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1931 m_freem(rxb->m_head);
1940 qla_alloc_rcv_bufs(qla_host_t *ha)
1944 if (bus_dma_tag_create(NULL, /* parent */
1945 1, 0, /* alignment, bounds */
1946 BUS_SPACE_MAXADDR, /* lowaddr */
1947 BUS_SPACE_MAXADDR, /* highaddr */
1948 NULL, NULL, /* filter, filterarg */
1949 MJUM9BYTES, /* maxsize */
1951 MJUM9BYTES, /* maxsegsize */
1952 BUS_DMA_ALLOCNOW, /* flags */
1953 NULL, /* lockfunc */
1954 NULL, /* lockfuncarg */
1957 device_printf(ha->pci_dev, "%s: rx_tag alloc failed\n",
1963 bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1965 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1966 ha->hw.sds[i].sdsr_next = 0;
1967 ha->hw.sds[i].rxb_free = NULL;
1968 ha->hw.sds[i].rx_free = 0;
1971 ret = qla_alloc_rcv_std(ha);
1977 qla_free_rcv_bufs(qla_host_t *ha)
1981 qla_free_rcv_std(ha);
1983 if (ha->rx_tag != NULL) {
1984 bus_dma_tag_destroy(ha->rx_tag);
1988 bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1990 for (i = 0; i < ha->hw.num_sds_rings; i++) {
1991 ha->hw.sds[i].sdsr_next = 0;
1992 ha->hw.sds[i].rxb_free = NULL;
1993 ha->hw.sds[i].rx_free = 0;
2000 ql_get_mbuf(qla_host_t *ha, qla_rx_buf_t *rxb, struct mbuf *nmp)
2002 register struct mbuf *mp = nmp;
2006 bus_dma_segment_t segs[1];
2007 int nsegs, mbuf_size;
2009 QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
2013 if (ha->hw.enable_9kb)
2014 mbuf_size = MJUM9BYTES;
2016 mbuf_size = MCLBYTES;
2020 if (QL_ERR_INJECT(ha, INJCT_M_GETCL_M_GETJCL_FAILURE))
2023 if (ha->hw.enable_9kb)
2024 mp = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, mbuf_size);
2026 mp = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2031 device_printf(ha->pci_dev,
2032 "%s: m_getcl failed\n", __func__);
2033 goto exit_ql_get_mbuf;
2035 mp->m_len = mp->m_pkthdr.len = mbuf_size;
2037 mp->m_len = mp->m_pkthdr.len = mbuf_size;
2038 mp->m_data = mp->m_ext.ext_buf;
2042 offset = (uint32_t)((unsigned long long)mp->m_data & 0x7ULL);
2044 offset = 8 - offset;
2049 * Using memory from the mbuf cluster pool, invoke the bus_dma
2050 * machinery to arrange the memory mapping.
2052 ret = bus_dmamap_load_mbuf_sg(ha->rx_tag, rxb->map,
2053 mp, segs, &nsegs, BUS_DMA_NOWAIT);
2054 rxb->paddr = segs[0].ds_addr;
2056 if (ret || !rxb->paddr || (nsegs != 1)) {
2059 device_printf(ha->pci_dev,
2060 "%s: bus_dmamap_load failed[%d, 0x%016llx, %d]\n",
2061 __func__, ret, (long long unsigned int)rxb->paddr,
2064 goto exit_ql_get_mbuf;
2067 bus_dmamap_sync(ha->rx_tag, rxb->map, BUS_DMASYNC_PREREAD);
2070 QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = 0x%08x\n", __func__, ret));
2076 qla_get_peer(qla_host_t *ha)
2080 int my_slot = pci_get_slot(ha->pci_dev);
2082 if (device_get_children(device_get_parent(ha->pci_dev), &peers, &count))
2085 for (i = 0; i < count; i++) {
2086 slot = pci_get_slot(peers[i]);
2088 if ((slot >= 0) && (slot == my_slot) &&
2089 (pci_get_device(peers[i]) ==
2090 pci_get_device(ha->pci_dev))) {
2091 if (ha->pci_dev != peers[i])
2092 ha->peer_dev = peers[i];
2098 qla_send_msg_to_peer(qla_host_t *ha, uint32_t msg_to_peer)
2100 qla_host_t *ha_peer;
2103 if ((ha_peer = device_get_softc(ha->peer_dev)) != NULL) {
2105 ha_peer->msg_from_peer = msg_to_peer;
2111 qla_set_error_recovery(qla_host_t *ha)
2113 struct ifnet *ifp = ha->ifp;
2115 if (!cold && ha->enable_error_recovery) {
2117 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2118 ha->qla_initiate_recovery = 1;
2125 qla_error_recovery(void *context, int pending)
2127 qla_host_t *ha = context;
2128 uint32_t msecs_100 = 400;
2129 struct ifnet *ifp = ha->ifp;
2132 device_printf(ha->pci_dev, "%s: enter\n", __func__);
2133 ha->hw.imd_compl = 1;
2135 taskqueue_drain_all(ha->stats_tq);
2136 taskqueue_drain_all(ha->async_event_tq);
2138 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2141 device_printf(ha->pci_dev, "%s: ts_usecs = %ld start\n",
2142 __func__, qla_get_usec_timestamp());
2144 if (ha->qla_interface_up) {
2146 qla_mdelay(__func__, 300);
2148 //ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2150 for (i = 0; i < ha->hw.num_sds_rings; i++) {
2158 if (fp->tx_br != NULL) {
2159 mtx_lock(&fp->tx_mtx);
2160 mtx_unlock(&fp->tx_mtx);
2165 qla_drain_fp_taskqueues(ha);
2167 if ((ha->pci_func & 0x1) == 0) {
2169 if (!ha->msg_from_peer) {
2170 qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2172 while ((ha->msg_from_peer != QL_PEER_MSG_ACK) &&
2174 qla_mdelay(__func__, 100);
2177 ha->msg_from_peer = 0;
2179 if (ha->enable_minidump)
2182 if (ha->enable_driverstate_dump)
2183 ql_capture_drvr_state(ha);
2185 if (ql_init_hw(ha)) {
2186 device_printf(ha->pci_dev,
2187 "%s: ts_usecs = %ld exit: ql_init_hw failed\n",
2188 __func__, qla_get_usec_timestamp());
2190 goto qla_error_recovery_exit;
2193 if (ha->qla_interface_up) {
2194 qla_free_xmt_bufs(ha);
2195 qla_free_rcv_bufs(ha);
2198 if (!QL_ERR_INJECT(ha, INJCT_PEER_PORT_FAILURE_ERR_RECOVERY))
2199 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2202 if (ha->msg_from_peer == QL_PEER_MSG_RESET) {
2204 ha->msg_from_peer = 0;
2206 if (!QL_ERR_INJECT(ha, INJCT_PEER_PORT_FAILURE_ERR_RECOVERY))
2207 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2209 qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2212 while ((ha->msg_from_peer != QL_PEER_MSG_ACK) && msecs_100--)
2213 qla_mdelay(__func__, 100);
2214 ha->msg_from_peer = 0;
2216 if (ha->enable_driverstate_dump)
2217 ql_capture_drvr_state(ha);
2219 if (msecs_100 == 0) {
2220 device_printf(ha->pci_dev,
2221 "%s: ts_usecs = %ld exit: QL_PEER_MSG_ACK not received\n",
2222 __func__, qla_get_usec_timestamp());
2224 goto qla_error_recovery_exit;
2227 if (ql_init_hw(ha)) {
2228 device_printf(ha->pci_dev,
2229 "%s: ts_usecs = %ld exit: ql_init_hw failed\n",
2230 __func__, qla_get_usec_timestamp());
2232 goto qla_error_recovery_exit;
2235 if (ha->qla_interface_up) {
2236 qla_free_xmt_bufs(ha);
2237 qla_free_rcv_bufs(ha);
2241 qla_mdelay(__func__, ha->ms_delay_after_init);
2243 *((uint32_t *)&ha->hw.flags) = 0;
2244 ha->qla_initiate_recovery = 0;
2246 if (ha->qla_interface_up) {
2248 if (qla_alloc_xmt_bufs(ha) != 0) {
2250 goto qla_error_recovery_exit;
2253 qla_confirm_9kb_enable(ha);
2255 if (qla_alloc_rcv_bufs(ha) != 0) {
2257 goto qla_error_recovery_exit;
2262 if (ql_init_hw_if(ha) == 0) {
2264 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2265 ha->qla_watchdog_pause = 0;
2266 ql_update_link_state(ha);
2270 if (ha->hw.sp_log_stop_events &
2271 Q8_SP_LOG_STOP_IF_START_FAILURE)
2272 ha->hw.sp_log_stop = -1;
2275 ha->qla_watchdog_pause = 0;
2278 qla_error_recovery_exit:
2281 device_printf(ha->pci_dev, "%s: ts_usecs = %ld port offline\n",
2282 __func__, qla_get_usec_timestamp());
2283 if (ha->hw.sp_log_stop_events &
2284 Q8_SP_LOG_STOP_ERR_RECOVERY_FAILURE)
2285 ha->hw.sp_log_stop = -1;
2289 QLA_UNLOCK(ha, __func__);
2292 callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
2295 device_printf(ha->pci_dev,
2296 "%s: ts_usecs = %ld exit\n",
2297 __func__, qla_get_usec_timestamp());
2302 qla_async_event(void *context, int pending)
2304 qla_host_t *ha = context;
2306 if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2309 if (ha->async_event) {
2310 ha->async_event = 0;
2311 qla_hw_async_event(ha);
2314 QLA_UNLOCK(ha, __func__);
2320 qla_stats(void *context, int pending)