]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/dev/qlxgbe/ql_os.c
MFC r324538
[FreeBSD/stable/9.git] / sys / dev / qlxgbe / ql_os.c
1 /*
2  * Copyright (c) 2013-2016 Qlogic Corporation
3  * All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions
7  *  are met:
8  *
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.
14  *
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.
26  */
27
28 /*
29  * File: ql_os.c
30  * Author : David C Somayajulu, Qlogic Corporation, Aliso Viejo, CA 92656.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36
37 #include "ql_os.h"
38 #include "ql_hw.h"
39 #include "ql_def.h"
40 #include "ql_inline.h"
41 #include "ql_ver.h"
42 #include "ql_glbl.h"
43 #include "ql_dbg.h"
44 #include <sys/smp.h>
45
46 /*
47  * Some PCI Configuration Space Related Defines
48  */
49
50 #ifndef PCI_VENDOR_QLOGIC
51 #define PCI_VENDOR_QLOGIC       0x1077
52 #endif
53
54 #ifndef PCI_PRODUCT_QLOGIC_ISP8030
55 #define PCI_PRODUCT_QLOGIC_ISP8030      0x8030
56 #endif
57
58 #define PCI_QLOGIC_ISP8030 \
59         ((PCI_PRODUCT_QLOGIC_ISP8030 << 16) | PCI_VENDOR_QLOGIC)
60
61 /*
62  * static functions
63  */
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);
71
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,
76                 int error);
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,
83                 uint32_t iscsi_pdu);
84
85 /*
86  * Hooks to the Operating Systems
87  */
88 static int qla_pci_probe (device_t);
89 static int qla_pci_attach (device_t);
90 static int qla_pci_detach (device_t);
91
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);
96
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);
104
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),
110         { 0, 0 }
111 };
112
113 static driver_t qla_pci_driver = {
114         "ql", qla_pci_methods, sizeof (qla_host_t),
115 };
116
117 static devclass_t qla83xx_devclass;
118
119 DRIVER_MODULE(qla83xx, pci, qla_pci_driver, qla83xx_devclass, 0, 0);
120
121 MODULE_DEPEND(qla83xx, pci, 1, 1, 1);
122 MODULE_DEPEND(qla83xx, ether, 1, 1, 1);
123
124 MALLOC_DEFINE(M_QLA83XXBUF, "qla83xxbuf", "Buffers for qla83xx driver");
125
126 #define QL_STD_REPLENISH_THRES          0
127 #define QL_JUMBO_REPLENISH_THRES        32
128
129
130 static char dev_str[64];
131 static char ver_str[64];
132
133 /*
134  * Name:        qla_pci_probe
135  * Function:    Validate the PCI device to be a QLA80XX device
136  */
137 static int
138 qla_pci_probe(device_t dev)
139 {
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,
145                         QLA_VERSION_BUILD);
146                 snprintf(ver_str, sizeof(ver_str), "v%d.%d.%d",
147                         QLA_VERSION_MAJOR, QLA_VERSION_MINOR,
148                         QLA_VERSION_BUILD);
149                 device_set_desc(dev, dev_str);
150                 break;
151         default:
152                 return (ENXIO);
153         }
154
155         if (bootverbose)
156                 printf("%s: %s\n ", __func__, dev_str);
157
158         return (BUS_PROBE_DEFAULT);
159 }
160
161 static void
162 qla_add_sysctls(qla_host_t *ha)
163 {
164         device_t dev = ha->pci_dev;
165
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");
170
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");
175
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,
179                 (void *)ha, 0,
180                 qla_sysctl_get_link_status, "I", "Link Status");
181
182         ha->dbg_level = 0;
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");
187
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 is enabled only when this is set");
194
195         ha->std_replenish = QL_STD_REPLENISH_THRES;
196         SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
197                 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
198                 OID_AUTO, "std_replenish", CTLFLAG_RW,
199                 &ha->std_replenish, ha->std_replenish,
200                 "Threshold for Replenishing Standard Frames");
201
202         SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
203                 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
204                 OID_AUTO, "ipv4_lro",
205                 CTLFLAG_RD, &ha->ipv4_lro,
206                 "number of ipv4 lro completions");
207
208         SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
209                 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
210                 OID_AUTO, "ipv6_lro",
211                 CTLFLAG_RD, &ha->ipv6_lro,
212                 "number of ipv6 lro completions");
213
214         SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
215                 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
216                 OID_AUTO, "tx_tso_frames",
217                 CTLFLAG_RD, &ha->tx_tso_frames,
218                 "number of Tx TSO Frames");
219
220         SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
221                 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
222                 OID_AUTO, "hw_vlan_tx_frames",
223                 CTLFLAG_RD, &ha->hw_vlan_tx_frames,
224                 "number of Tx VLAN Frames");
225
226         SYSCTL_ADD_QUAD(device_get_sysctl_ctx(dev),
227                 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
228                 OID_AUTO, "hw_lock_failed",
229                 CTLFLAG_RD, &ha->hw_lock_failed,
230                 "number of hw_lock failures");
231
232         return;
233 }
234
235 static void
236 qla_watchdog(void *arg)
237 {
238         qla_host_t *ha = arg;
239         qla_hw_t *hw;
240         struct ifnet *ifp;
241
242         hw = &ha->hw;
243         ifp = ha->ifp;
244
245         if (ha->qla_watchdog_exit) {
246                 ha->qla_watchdog_exited = 1;
247                 return;
248         }
249         ha->qla_watchdog_exited = 0;
250
251         if (!ha->qla_watchdog_pause) {
252                 if (ql_hw_check_health(ha) || ha->qla_initiate_recovery ||
253                         (ha->msg_from_peer == QL_PEER_MSG_RESET)) {
254
255                         if (!(ha->dbg_level & 0x8000)) {
256                                 ha->qla_watchdog_paused = 1;
257                                 ha->qla_watchdog_pause = 1;
258                                 ha->qla_initiate_recovery = 0;
259                                 ha->err_inject = 0;
260                                 device_printf(ha->pci_dev,
261                                         "%s: taskqueue_enqueue(err_task) \n",
262                                         __func__);
263                                 taskqueue_enqueue(ha->err_tq, &ha->err_task);
264                                 return;
265                         }
266
267                 } else if (ha->qla_interface_up) {
268
269                         ha->watchdog_ticks++;
270
271                         if (ha->watchdog_ticks > 1000)
272                                 ha->watchdog_ticks = 0;
273
274                         if (!ha->watchdog_ticks && QL_RUNNING(ifp)) {
275                                 taskqueue_enqueue(ha->stats_tq, &ha->stats_task);
276                         }
277
278                         if (ha->async_event) {
279                                 taskqueue_enqueue(ha->async_event_tq,
280                                         &ha->async_event_task);
281                         }
282
283 #if 0
284                         for (i = 0; ((i < ha->hw.num_sds_rings) &&
285                                         !ha->watchdog_ticks); i++) {
286                                 qla_tx_fp_t *fp = &ha->tx_fp[i];
287
288                                 if (fp->fp_taskqueue != NULL)
289                                         taskqueue_enqueue(fp->fp_taskqueue,
290                                                 &fp->fp_task);
291                         }
292 #endif
293                         ha->qla_watchdog_paused = 0;
294                 } else {
295                         ha->qla_watchdog_paused = 0;
296                 }
297         } else {
298                 ha->qla_watchdog_paused = 1;
299         }
300
301         callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
302                 qla_watchdog, ha);
303 }
304
305 /*
306  * Name:        qla_pci_attach
307  * Function:    attaches the device to the operating system
308  */
309 static int
310 qla_pci_attach(device_t dev)
311 {
312         qla_host_t *ha = NULL;
313         uint32_t rsrc_len;
314         int i;
315         uint32_t num_rcvq = 0;
316
317         if ((ha = device_get_softc(dev)) == NULL) {
318                 device_printf(dev, "cannot get softc\n");
319                 return (ENOMEM);
320         }
321
322         memset(ha, 0, sizeof (qla_host_t));
323
324         if (pci_get_device(dev) != PCI_PRODUCT_QLOGIC_ISP8030) {
325                 device_printf(dev, "device is not ISP8030\n");
326                 return (ENXIO);
327         }
328
329         ha->pci_func = pci_get_function(dev) & 0x1;
330
331         ha->pci_dev = dev;
332
333         pci_enable_busmaster(dev);
334
335         ha->reg_rid = PCIR_BAR(0);
336         ha->pci_reg = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &ha->reg_rid,
337                                 RF_ACTIVE);
338
339         if (ha->pci_reg == NULL) {
340                 device_printf(dev, "unable to map any ports\n");
341                 goto qla_pci_attach_err;
342         }
343
344         rsrc_len = (uint32_t) bus_get_resource_count(dev, SYS_RES_MEMORY,
345                                         ha->reg_rid);
346
347         mtx_init(&ha->hw_lock, "qla83xx_hw_lock", MTX_NETWORK_LOCK, MTX_DEF);
348         ha->flags.lock_init = 1;
349
350         qla_add_sysctls(ha);
351
352         ha->hw.num_sds_rings = MAX_SDS_RINGS;
353         ha->hw.num_rds_rings = MAX_RDS_RINGS;
354         ha->hw.num_tx_rings = NUM_TX_RINGS;
355
356         ha->reg_rid1 = PCIR_BAR(2);
357         ha->pci_reg1 = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
358                         &ha->reg_rid1, RF_ACTIVE);
359
360         ha->msix_count = pci_msix_count(dev);
361
362         if (ha->msix_count < 1 ) {
363                 device_printf(dev, "%s: msix_count[%d] not enough\n", __func__,
364                         ha->msix_count);
365                 goto qla_pci_attach_err;
366         }
367
368         if (ha->msix_count < (ha->hw.num_sds_rings + 1)) {
369                 ha->hw.num_sds_rings = ha->msix_count - 1;
370         }
371
372         QL_DPRINT2(ha, (dev, "%s: ha %p pci_func 0x%x rsrc_count 0x%08x"
373                 " msix_count 0x%x pci_reg %p pci_reg1 %p\n", __func__, ha,
374                 ha->pci_func, rsrc_len, ha->msix_count, ha->pci_reg,
375                 ha->pci_reg1));
376
377         /* initialize hardware */
378         if (ql_init_hw(ha)) {
379                 device_printf(dev, "%s: ql_init_hw failed\n", __func__);
380                 goto qla_pci_attach_err;
381         }
382
383         device_printf(dev, "%s: firmware[%d.%d.%d.%d]\n", __func__,
384                 ha->fw_ver_major, ha->fw_ver_minor, ha->fw_ver_sub,
385                 ha->fw_ver_build);
386         snprintf(ha->fw_ver_str, sizeof(ha->fw_ver_str), "%d.%d.%d.%d",
387                         ha->fw_ver_major, ha->fw_ver_minor, ha->fw_ver_sub,
388                         ha->fw_ver_build);
389
390         if (qla_get_nic_partition(ha, NULL, &num_rcvq)) {
391                 device_printf(dev, "%s: qla_get_nic_partition failed\n",
392                         __func__);
393                 goto qla_pci_attach_err;
394         }
395         device_printf(dev, "%s: ha %p pci_func 0x%x rsrc_count 0x%08x"
396                 " msix_count 0x%x pci_reg %p pci_reg1 %p num_rcvq = %d\n",
397                 __func__, ha, ha->pci_func, rsrc_len, ha->msix_count,
398                 ha->pci_reg, ha->pci_reg1, num_rcvq);
399
400         if ((ha->msix_count  < 64) || (num_rcvq != 32)) {
401                 if (ha->hw.num_sds_rings > 15) {
402                         ha->hw.num_sds_rings = 15;
403                 }
404         }
405
406         ha->hw.num_rds_rings = ha->hw.num_sds_rings;
407         ha->hw.num_tx_rings = ha->hw.num_sds_rings;
408
409 #ifdef QL_ENABLE_ISCSI_TLV
410         ha->hw.num_tx_rings = ha->hw.num_sds_rings * 2;
411 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
412
413         ql_hw_add_sysctls(ha);
414
415         ha->msix_count = ha->hw.num_sds_rings + 1;
416
417         if (pci_alloc_msix(dev, &ha->msix_count)) {
418                 device_printf(dev, "%s: pci_alloc_msi[%d] failed\n", __func__,
419                         ha->msix_count);
420                 ha->msix_count = 0;
421                 goto qla_pci_attach_err;
422         }
423
424         ha->mbx_irq_rid = 1;
425         ha->mbx_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
426                                 &ha->mbx_irq_rid,
427                                 (RF_ACTIVE | RF_SHAREABLE));
428         if (ha->mbx_irq == NULL) {
429                 device_printf(dev, "could not allocate mbx interrupt\n");
430                 goto qla_pci_attach_err;
431         }
432         if (bus_setup_intr(dev, ha->mbx_irq, (INTR_TYPE_NET | INTR_MPSAFE),
433                 NULL, ql_mbx_isr, ha, &ha->mbx_handle)) {
434                 device_printf(dev, "could not setup mbx interrupt\n");
435                 goto qla_pci_attach_err;
436         }
437
438         for (i = 0; i < ha->hw.num_sds_rings; i++) {
439                 ha->irq_vec[i].sds_idx = i;
440                 ha->irq_vec[i].ha = ha;
441                 ha->irq_vec[i].irq_rid = 2 + i;
442
443                 ha->irq_vec[i].irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
444                                 &ha->irq_vec[i].irq_rid,
445                                 (RF_ACTIVE | RF_SHAREABLE));
446
447                 if (ha->irq_vec[i].irq == NULL) {
448                         device_printf(dev, "could not allocate interrupt\n");
449                         goto qla_pci_attach_err;
450                 }
451                 if (bus_setup_intr(dev, ha->irq_vec[i].irq,
452                         (INTR_TYPE_NET | INTR_MPSAFE),
453                         NULL, ql_isr, &ha->irq_vec[i],
454                         &ha->irq_vec[i].handle)) {
455                         device_printf(dev, "could not setup interrupt\n");
456                         goto qla_pci_attach_err;
457                 }
458
459                 ha->tx_fp[i].ha = ha;
460                 ha->tx_fp[i].txr_idx = i;
461
462                 if (qla_alloc_tx_br(ha, &ha->tx_fp[i])) {
463                         device_printf(dev, "%s: could not allocate tx_br[%d]\n",
464                                 __func__, i);
465                         goto qla_pci_attach_err;
466                 }
467         }
468
469         if (qla_create_fp_taskqueues(ha) != 0)
470                 goto qla_pci_attach_err;
471
472         printf("%s: mp__ncpus %d sds %d rds %d msi-x %d\n", __func__, mp_ncpus,
473                 ha->hw.num_sds_rings, ha->hw.num_rds_rings, ha->msix_count);
474
475         ql_read_mac_addr(ha);
476
477         /* allocate parent dma tag */
478         if (qla_alloc_parent_dma_tag(ha)) {
479                 device_printf(dev, "%s: qla_alloc_parent_dma_tag failed\n",
480                         __func__);
481                 goto qla_pci_attach_err;
482         }
483
484         /* alloc all dma buffers */
485         if (ql_alloc_dma(ha)) {
486                 device_printf(dev, "%s: ql_alloc_dma failed\n", __func__);
487                 goto qla_pci_attach_err;
488         }
489         qla_get_peer(ha);
490
491         if (ql_minidump_init(ha) != 0) {
492                 device_printf(dev, "%s: ql_minidump_init failed\n", __func__);
493                 goto qla_pci_attach_err;
494         }
495         ql_alloc_drvr_state_buffer(ha);
496         /* create the o.s ethernet interface */
497         qla_init_ifnet(dev, ha);
498
499         ha->flags.qla_watchdog_active = 1;
500         ha->qla_watchdog_pause = 0;
501
502         callout_init(&ha->tx_callout, TRUE);
503         ha->flags.qla_callout_init = 1;
504
505         /* create ioctl device interface */
506         if (ql_make_cdev(ha)) {
507                 device_printf(dev, "%s: ql_make_cdev failed\n", __func__);
508                 goto qla_pci_attach_err;
509         }
510
511         callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
512                 qla_watchdog, ha);
513
514         TASK_INIT(&ha->err_task, 0, qla_error_recovery, ha);
515         ha->err_tq = taskqueue_create("qla_errq", M_NOWAIT,
516                         taskqueue_thread_enqueue, &ha->err_tq);
517         taskqueue_start_threads(&ha->err_tq, 1, PI_NET, "%s errq",
518                 device_get_nameunit(ha->pci_dev));
519
520         TASK_INIT(&ha->async_event_task, 0, qla_async_event, ha);
521         ha->async_event_tq = taskqueue_create("qla_asyncq", M_NOWAIT,
522                         taskqueue_thread_enqueue, &ha->async_event_tq);
523         taskqueue_start_threads(&ha->async_event_tq, 1, PI_NET, "%s asyncq",
524                 device_get_nameunit(ha->pci_dev));
525
526         TASK_INIT(&ha->stats_task, 0, qla_stats, ha);
527         ha->stats_tq = taskqueue_create("qla_statsq", M_NOWAIT,
528                         taskqueue_thread_enqueue, &ha->stats_tq);
529         taskqueue_start_threads(&ha->stats_tq, 1, PI_NET, "%s taskq",
530                 device_get_nameunit(ha->pci_dev));
531
532         QL_DPRINT2(ha, (dev, "%s: exit 0\n", __func__));
533         return (0);
534
535 qla_pci_attach_err:
536
537         qla_release(ha);
538
539         if (ha->flags.lock_init) {
540                 mtx_destroy(&ha->hw_lock);
541         }
542
543         QL_DPRINT2(ha, (dev, "%s: exit ENXIO\n", __func__));
544         return (ENXIO);
545 }
546
547 /*
548  * Name:        qla_pci_detach
549  * Function:    Unhooks the device from the operating system
550  */
551 static int
552 qla_pci_detach(device_t dev)
553 {
554         qla_host_t *ha = NULL;
555         struct ifnet *ifp;
556
557
558         if ((ha = device_get_softc(dev)) == NULL) {
559                 device_printf(dev, "cannot get softc\n");
560                 return (ENOMEM);
561         }
562
563         QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
564
565         ifp = ha->ifp;
566
567         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
568         QLA_LOCK(ha, __func__, -1, 0);
569
570         ha->qla_detach_active = 1;
571         qla_stop(ha);
572
573         qla_release(ha);
574
575         QLA_UNLOCK(ha, __func__);
576
577         if (ha->flags.lock_init) {
578                 mtx_destroy(&ha->hw_lock);
579         }
580
581         QL_DPRINT2(ha, (dev, "%s: exit\n", __func__));
582
583         return (0);
584 }
585
586 /*
587  * SYSCTL Related Callbacks
588  */
589 static int
590 qla_sysctl_get_link_status(SYSCTL_HANDLER_ARGS)
591 {
592         int err, ret = 0;
593         qla_host_t *ha;
594
595         err = sysctl_handle_int(oidp, &ret, 0, req);
596
597         if (err || !req->newptr)
598                 return (err);
599
600         if (ret == 1) {
601                 ha = (qla_host_t *)arg1;
602                 ql_hw_link_status(ha);
603         }
604         return (err);
605 }
606
607 /*
608  * Name:        qla_release
609  * Function:    Releases the resources allocated for the device
610  */
611 static void
612 qla_release(qla_host_t *ha)
613 {
614         device_t dev;
615         int i;
616
617         dev = ha->pci_dev;
618
619         if (ha->async_event_tq) {
620                 taskqueue_drain(ha->async_event_tq, &ha->async_event_task);
621                 taskqueue_free(ha->async_event_tq);
622         }
623
624         if (ha->err_tq) {
625                 taskqueue_drain(ha->err_tq, &ha->err_task);
626                 taskqueue_free(ha->err_tq);
627         }
628
629         if (ha->stats_tq) {
630                 taskqueue_drain(ha->stats_tq, &ha->stats_task);
631                 taskqueue_free(ha->stats_tq);
632         }
633
634         ql_del_cdev(ha);
635
636         if (ha->flags.qla_watchdog_active) {
637                 ha->qla_watchdog_exit = 1;
638
639                 while (ha->qla_watchdog_exited == 0)
640                         qla_mdelay(__func__, 1);
641         }
642
643         if (ha->flags.qla_callout_init)
644                 callout_stop(&ha->tx_callout);
645
646         if (ha->ifp != NULL)
647                 ether_ifdetach(ha->ifp);
648
649         ql_free_drvr_state_buffer(ha);
650         ql_free_dma(ha); 
651         qla_free_parent_dma_tag(ha);
652
653         if (ha->mbx_handle)
654                 (void)bus_teardown_intr(dev, ha->mbx_irq, ha->mbx_handle);
655
656         if (ha->mbx_irq)
657                 (void) bus_release_resource(dev, SYS_RES_IRQ, ha->mbx_irq_rid,
658                                 ha->mbx_irq);
659
660         for (i = 0; i < ha->hw.num_sds_rings; i++) {
661
662                 if (ha->irq_vec[i].handle) {
663                         (void)bus_teardown_intr(dev, ha->irq_vec[i].irq,
664                                         ha->irq_vec[i].handle);
665                 }
666                         
667                 if (ha->irq_vec[i].irq) {
668                         (void)bus_release_resource(dev, SYS_RES_IRQ,
669                                 ha->irq_vec[i].irq_rid,
670                                 ha->irq_vec[i].irq);
671                 }
672
673                 qla_free_tx_br(ha, &ha->tx_fp[i]);
674         }
675         qla_destroy_fp_taskqueues(ha);
676
677         if (ha->msix_count)
678                 pci_release_msi(dev);
679
680 //      if (ha->flags.lock_init) {
681 //              mtx_destroy(&ha->hw_lock);
682 //      }
683
684         if (ha->pci_reg)
685                 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid,
686                                 ha->pci_reg);
687
688         if (ha->pci_reg1)
689                 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid1,
690                                 ha->pci_reg1);
691
692         return;
693 }
694
695 /*
696  * DMA Related Functions
697  */
698
699 static void
700 qla_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
701 {
702         *((bus_addr_t *)arg) = 0;
703
704         if (error) {
705                 printf("%s: bus_dmamap_load failed (%d)\n", __func__, error);
706                 return;
707         }
708
709         *((bus_addr_t *)arg) = segs[0].ds_addr;
710
711         return;
712 }
713
714 int
715 ql_alloc_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
716 {
717         int             ret = 0;
718         device_t        dev;
719         bus_addr_t      b_addr;
720
721         dev = ha->pci_dev;
722
723         QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
724
725         ret = bus_dma_tag_create(
726                         ha->parent_tag,/* parent */
727                         dma_buf->alignment,
728                         ((bus_size_t)(1ULL << 32)),/* boundary */
729                         BUS_SPACE_MAXADDR,      /* lowaddr */
730                         BUS_SPACE_MAXADDR,      /* highaddr */
731                         NULL, NULL,             /* filter, filterarg */
732                         dma_buf->size,          /* maxsize */
733                         1,                      /* nsegments */
734                         dma_buf->size,          /* maxsegsize */
735                         0,                      /* flags */
736                         NULL, NULL,             /* lockfunc, lockarg */
737                         &dma_buf->dma_tag);
738
739         if (ret) {
740                 device_printf(dev, "%s: could not create dma tag\n", __func__);
741                 goto ql_alloc_dmabuf_exit;
742         }
743         ret = bus_dmamem_alloc(dma_buf->dma_tag,
744                         (void **)&dma_buf->dma_b,
745                         (BUS_DMA_ZERO | BUS_DMA_COHERENT | BUS_DMA_NOWAIT),
746                         &dma_buf->dma_map);
747         if (ret) {
748                 bus_dma_tag_destroy(dma_buf->dma_tag);
749                 device_printf(dev, "%s: bus_dmamem_alloc failed\n", __func__);
750                 goto ql_alloc_dmabuf_exit;
751         }
752
753         ret = bus_dmamap_load(dma_buf->dma_tag,
754                         dma_buf->dma_map,
755                         dma_buf->dma_b,
756                         dma_buf->size,
757                         qla_dmamap_callback,
758                         &b_addr, BUS_DMA_NOWAIT);
759
760         if (ret || !b_addr) {
761                 bus_dma_tag_destroy(dma_buf->dma_tag);
762                 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b,
763                         dma_buf->dma_map);
764                 ret = -1;
765                 goto ql_alloc_dmabuf_exit;
766         }
767
768         dma_buf->dma_addr = b_addr;
769
770 ql_alloc_dmabuf_exit:
771         QL_DPRINT2(ha, (dev, "%s: exit ret 0x%08x tag %p map %p b %p sz 0x%x\n",
772                 __func__, ret, (void *)dma_buf->dma_tag,
773                 (void *)dma_buf->dma_map, (void *)dma_buf->dma_b,
774                 dma_buf->size));
775
776         return ret;
777 }
778
779 void
780 ql_free_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
781 {
782         bus_dmamap_unload(dma_buf->dma_tag, dma_buf->dma_map); 
783         bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b, dma_buf->dma_map);
784         bus_dma_tag_destroy(dma_buf->dma_tag);
785 }
786
787 static int
788 qla_alloc_parent_dma_tag(qla_host_t *ha)
789 {
790         int             ret;
791         device_t        dev;
792
793         dev = ha->pci_dev;
794
795         /*
796          * Allocate parent DMA Tag
797          */
798         ret = bus_dma_tag_create(
799                         bus_get_dma_tag(dev),   /* parent */
800                         1,((bus_size_t)(1ULL << 32)),/* alignment, boundary */
801                         BUS_SPACE_MAXADDR,      /* lowaddr */
802                         BUS_SPACE_MAXADDR,      /* highaddr */
803                         NULL, NULL,             /* filter, filterarg */
804                         BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
805                         0,                      /* nsegments */
806                         BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
807                         0,                      /* flags */
808                         NULL, NULL,             /* lockfunc, lockarg */
809                         &ha->parent_tag);
810
811         if (ret) {
812                 device_printf(dev, "%s: could not create parent dma tag\n",
813                         __func__);
814                 return (-1);
815         }
816
817         ha->flags.parent_tag = 1;
818         
819         return (0);
820 }
821
822 static void
823 qla_free_parent_dma_tag(qla_host_t *ha)
824 {
825         if (ha->flags.parent_tag) {
826                 bus_dma_tag_destroy(ha->parent_tag);
827                 ha->flags.parent_tag = 0;
828         }
829 }
830
831 /*
832  * Name: qla_init_ifnet
833  * Function: Creates the Network Device Interface and Registers it with the O.S
834  */
835
836 static void
837 qla_init_ifnet(device_t dev, qla_host_t *ha)
838 {
839         struct ifnet *ifp;
840
841         QL_DPRINT2(ha, (dev, "%s: enter\n", __func__));
842
843         ifp = ha->ifp = if_alloc(IFT_ETHER);
844
845         if (ifp == NULL)
846                 panic("%s: cannot if_alloc()\n", device_get_nameunit(dev));
847
848         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
849
850 #if __FreeBSD_version >= 1000000
851         if_initbaudrate(ifp, IF_Gbps(10));
852         ifp->if_capabilities = IFCAP_LINKSTATE;
853 #else
854         ifp->if_mtu = ETHERMTU;
855         ifp->if_baudrate = (1 * 1000 * 1000 *1000);
856
857 #endif /* #if __FreeBSD_version >= 1000000 */
858
859         ifp->if_init = qla_init;
860         ifp->if_softc = ha;
861         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
862         ifp->if_ioctl = qla_ioctl;
863
864         ifp->if_transmit = qla_transmit;
865         ifp->if_qflush = qla_qflush;
866
867         IFQ_SET_MAXLEN(&ifp->if_snd, qla_get_ifq_snd_maxlen(ha));
868         ifp->if_snd.ifq_drv_maxlen = qla_get_ifq_snd_maxlen(ha);
869         IFQ_SET_READY(&ifp->if_snd);
870
871         ha->max_frame_size = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
872
873         ether_ifattach(ifp, qla_get_mac_addr(ha));
874
875         ifp->if_capabilities |= IFCAP_HWCSUM |
876                                 IFCAP_TSO4 |
877                                 IFCAP_JUMBO_MTU |
878                                 IFCAP_VLAN_HWTAGGING |
879                                 IFCAP_VLAN_MTU |
880                                 IFCAP_VLAN_HWTSO |
881                                 IFCAP_LRO;
882
883         ifp->if_capenable = ifp->if_capabilities;
884
885         ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
886
887         ifmedia_init(&ha->media, IFM_IMASK, qla_media_change, qla_media_status);
888
889         ifmedia_add(&ha->media, (IFM_ETHER | qla_get_optics(ha) | IFM_FDX), 0,
890                 NULL);
891         ifmedia_add(&ha->media, (IFM_ETHER | IFM_AUTO), 0, NULL);
892
893         ifmedia_set(&ha->media, (IFM_ETHER | IFM_AUTO));
894
895         QL_DPRINT2(ha, (dev, "%s: exit\n", __func__));
896
897         return;
898 }
899
900 static void
901 qla_init_locked(qla_host_t *ha)
902 {
903         struct ifnet *ifp = ha->ifp;
904
905         qla_stop(ha);
906
907         if (qla_alloc_xmt_bufs(ha) != 0) 
908                 return;
909
910         qla_confirm_9kb_enable(ha);
911
912         if (qla_alloc_rcv_bufs(ha) != 0)
913                 return;
914
915         bcopy(IF_LLADDR(ha->ifp), ha->hw.mac_addr, ETHER_ADDR_LEN);
916
917         ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_TSO;
918         ifp->if_hwassist |= CSUM_TCP_IPV6 | CSUM_UDP_IPV6;
919
920         ha->stop_rcv = 0;
921         if (ql_init_hw_if(ha) == 0) {
922                 ifp = ha->ifp;
923                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
924                 ha->qla_watchdog_pause = 0;
925                 ha->hw_vlan_tx_frames = 0;
926                 ha->tx_tso_frames = 0;
927                 ha->qla_interface_up = 1;
928                 ql_update_link_state(ha);
929         }
930
931         return;
932 }
933
934 static void
935 qla_init(void *arg)
936 {
937         qla_host_t *ha;
938
939         ha = (qla_host_t *)arg;
940
941         QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
942
943         if (QLA_LOCK(ha, __func__, -1, 0) != 0)
944                 return;
945
946         qla_init_locked(ha);
947
948         QLA_UNLOCK(ha, __func__);
949
950         QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
951 }
952
953 static int
954 qla_set_multi(qla_host_t *ha, uint32_t add_multi)
955 {
956         uint8_t mta[Q8_MAX_NUM_MULTICAST_ADDRS * Q8_MAC_ADDR_LEN];
957         struct ifmultiaddr *ifma;
958         int mcnt = 0;
959         struct ifnet *ifp = ha->ifp;
960         int ret = 0;
961
962         if_maddr_rlock(ifp);
963
964         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
965
966                 if (ifma->ifma_addr->sa_family != AF_LINK)
967                         continue;
968
969                 if (mcnt == Q8_MAX_NUM_MULTICAST_ADDRS)
970                         break;
971
972                 bcopy(LLADDR((struct sockaddr_dl *) ifma->ifma_addr),
973                         &mta[mcnt * Q8_MAC_ADDR_LEN], Q8_MAC_ADDR_LEN);
974
975                 mcnt++;
976         }
977
978         if_maddr_runlock(ifp);
979
980         if (QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
981                 QLA_LOCK_NO_SLEEP) != 0)
982                 return (-1);
983
984         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
985
986                 if (!add_multi) {
987                         ret = qla_hw_del_all_mcast(ha);
988
989                         if (ret)
990                                 device_printf(ha->pci_dev,
991                                         "%s: qla_hw_del_all_mcast() failed\n",
992                                 __func__);
993                 }
994
995                 if (!ret)
996                         ret = ql_hw_set_multi(ha, mta, mcnt, 1);
997
998         }
999
1000         QLA_UNLOCK(ha, __func__);
1001
1002         return (ret);
1003 }
1004
1005 static int
1006 qla_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1007 {
1008         int ret = 0;
1009         struct ifreq *ifr = (struct ifreq *)data;
1010         struct ifaddr *ifa = (struct ifaddr *)data;
1011         qla_host_t *ha;
1012
1013         ha = (qla_host_t *)ifp->if_softc;
1014
1015         switch (cmd) {
1016         case SIOCSIFADDR:
1017                 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFADDR (0x%lx)\n",
1018                         __func__, cmd));
1019
1020                 if (ifa->ifa_addr->sa_family == AF_INET) {
1021
1022                         ret = QLA_LOCK(ha, __func__,
1023                                         QLA_LOCK_DEFAULT_MS_TIMEOUT,
1024                                         QLA_LOCK_NO_SLEEP);
1025                         if (ret)
1026                                 break;
1027
1028                         ifp->if_flags |= IFF_UP;
1029
1030                         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1031                                 qla_init_locked(ha);
1032                         }
1033
1034                         QLA_UNLOCK(ha, __func__);
1035                         QL_DPRINT4(ha, (ha->pci_dev,
1036                                 "%s: SIOCSIFADDR (0x%lx) ipv4 [0x%08x]\n",
1037                                 __func__, cmd,
1038                                 ntohl(IA_SIN(ifa)->sin_addr.s_addr)));
1039
1040                         arp_ifinit(ifp, ifa);
1041                 } else {
1042                         ether_ioctl(ifp, cmd, data);
1043                 }
1044                 break;
1045
1046         case SIOCSIFMTU:
1047                 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFMTU (0x%lx)\n",
1048                         __func__, cmd));
1049
1050                 if (ifr->ifr_mtu > QLA_MAX_MTU) {
1051                         ret = EINVAL;
1052                 } else {
1053                         ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1054                                         QLA_LOCK_NO_SLEEP);
1055
1056                         if (ret)
1057                                 break;
1058
1059                         ifp->if_mtu = ifr->ifr_mtu;
1060                         ha->max_frame_size =
1061                                 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
1062
1063                         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1064                                 qla_init_locked(ha);
1065                         }
1066
1067                         if (ifp->if_mtu > ETHERMTU)
1068                                 ha->std_replenish = QL_JUMBO_REPLENISH_THRES;
1069                         else
1070                                 ha->std_replenish = QL_STD_REPLENISH_THRES;
1071                                 
1072
1073                         QLA_UNLOCK(ha, __func__);
1074                 }
1075
1076                 break;
1077
1078         case SIOCSIFFLAGS:
1079                 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFFLAGS (0x%lx)\n",
1080                         __func__, cmd));
1081
1082                 ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1083                                 QLA_LOCK_NO_SLEEP);
1084
1085                 if (ret)
1086                         break;
1087
1088                 if (ifp->if_flags & IFF_UP) {
1089
1090                         ha->max_frame_size = ifp->if_mtu +
1091                                         ETHER_HDR_LEN + ETHER_CRC_LEN;
1092                         qla_init_locked(ha);
1093                                                 
1094                         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1095                                 if ((ifp->if_flags ^ ha->if_flags) &
1096                                         IFF_PROMISC) {
1097                                         ret = ql_set_promisc(ha);
1098                                 } else if ((ifp->if_flags ^ ha->if_flags) &
1099                                         IFF_ALLMULTI) {
1100                                         ret = ql_set_allmulti(ha);
1101                                 }
1102                         }
1103                 } else {
1104                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1105                                 qla_stop(ha);
1106                         ha->if_flags = ifp->if_flags;
1107                 }
1108
1109                 QLA_UNLOCK(ha, __func__);
1110                 break;
1111
1112         case SIOCADDMULTI:
1113                 QL_DPRINT4(ha, (ha->pci_dev,
1114                         "%s: %s (0x%lx)\n", __func__, "SIOCADDMULTI", cmd));
1115
1116                 if (qla_set_multi(ha, 1))
1117                         ret = EINVAL;
1118                 break;
1119
1120         case SIOCDELMULTI:
1121                 QL_DPRINT4(ha, (ha->pci_dev,
1122                         "%s: %s (0x%lx)\n", __func__, "SIOCDELMULTI", cmd));
1123
1124                 if (qla_set_multi(ha, 0))
1125                         ret = EINVAL;
1126                 break;
1127
1128         case SIOCSIFMEDIA:
1129         case SIOCGIFMEDIA:
1130                 QL_DPRINT4(ha, (ha->pci_dev,
1131                         "%s: SIOCSIFMEDIA/SIOCGIFMEDIA (0x%lx)\n",
1132                         __func__, cmd));
1133                 ret = ifmedia_ioctl(ifp, ifr, &ha->media, cmd);
1134                 break;
1135
1136         case SIOCSIFCAP:
1137         {
1138                 int mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1139
1140                 QL_DPRINT4(ha, (ha->pci_dev, "%s: SIOCSIFCAP (0x%lx)\n",
1141                         __func__, cmd));
1142
1143                 if (mask & IFCAP_HWCSUM)
1144                         ifp->if_capenable ^= IFCAP_HWCSUM;
1145                 if (mask & IFCAP_TSO4)
1146                         ifp->if_capenable ^= IFCAP_TSO4;
1147                 if (mask & IFCAP_TSO6)
1148                         ifp->if_capenable ^= IFCAP_TSO6;
1149                 if (mask & IFCAP_VLAN_HWTAGGING)
1150                         ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1151                 if (mask & IFCAP_VLAN_HWTSO)
1152                         ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1153                 if (mask & IFCAP_LRO)
1154                         ifp->if_capenable ^= IFCAP_LRO;
1155
1156                 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1157                         ret = QLA_LOCK(ha, __func__, QLA_LOCK_DEFAULT_MS_TIMEOUT,
1158                                 QLA_LOCK_NO_SLEEP);
1159
1160                         if (ret)
1161                                 break;
1162
1163                         qla_init_locked(ha);
1164
1165                         QLA_UNLOCK(ha, __func__);
1166
1167                 }
1168                 VLAN_CAPABILITIES(ifp);
1169                 break;
1170         }
1171
1172         default:
1173                 QL_DPRINT4(ha, (ha->pci_dev, "%s: default (0x%lx)\n",
1174                         __func__, cmd));
1175                 ret = ether_ioctl(ifp, cmd, data);
1176                 break;
1177         }
1178
1179         return (ret);
1180 }
1181
1182 static int
1183 qla_media_change(struct ifnet *ifp)
1184 {
1185         qla_host_t *ha;
1186         struct ifmedia *ifm;
1187         int ret = 0;
1188
1189         ha = (qla_host_t *)ifp->if_softc;
1190
1191         QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1192
1193         ifm = &ha->media;
1194
1195         if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1196                 ret = EINVAL;
1197
1198         QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1199
1200         return (ret);
1201 }
1202
1203 static void
1204 qla_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1205 {
1206         qla_host_t *ha;
1207
1208         ha = (qla_host_t *)ifp->if_softc;
1209
1210         QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1211
1212         ifmr->ifm_status = IFM_AVALID;
1213         ifmr->ifm_active = IFM_ETHER;
1214         
1215         ql_update_link_state(ha);
1216         if (ha->hw.link_up) {
1217                 ifmr->ifm_status |= IFM_ACTIVE;
1218                 ifmr->ifm_active |= (IFM_FDX | qla_get_optics(ha));
1219         }
1220
1221         QL_DPRINT2(ha, (ha->pci_dev, "%s: exit (%s)\n", __func__,\
1222                 (ha->hw.link_up ? "link_up" : "link_down")));
1223
1224         return;
1225 }
1226
1227
1228 static int
1229 qla_send(qla_host_t *ha, struct mbuf **m_headp, uint32_t txr_idx,
1230         uint32_t iscsi_pdu)
1231 {
1232         bus_dma_segment_t       segs[QLA_MAX_SEGMENTS];
1233         bus_dmamap_t            map;
1234         int                     nsegs;
1235         int                     ret = -1;
1236         uint32_t                tx_idx;
1237         struct mbuf             *m_head = *m_headp;
1238
1239         QL_DPRINT8(ha, (ha->pci_dev, "%s: enter\n", __func__));
1240
1241         if (m_head->m_flags & M_FLOWID) {
1242 #ifdef QL_ENABLE_ISCSI_TLV
1243                 if (qla_iscsi_pdu(ha, m_head) == 0) {
1244                         iscsi_pdu = 1;
1245                         txr_idx = m_head->m_pkthdr.flowid &
1246                                         ((ha->hw.num_tx_rings >> 1) - 1);
1247                 } else {
1248                         txr_idx = m_head->m_pkthdr.flowid &
1249                                         (ha->hw.num_tx_rings - 1);
1250                 }
1251 #else
1252                 txr_idx = m_head->m_pkthdr.flowid & (ha->hw.num_tx_rings - 1);
1253 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1254         }
1255
1256
1257         tx_idx = ha->hw.tx_cntxt[txr_idx].txr_next;
1258
1259         if (NULL != ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head) {
1260                 QL_ASSERT(ha, 0, ("%s [%d]: txr_idx = %d tx_idx = %d "\
1261                         "mbuf = %p\n", __func__, __LINE__, txr_idx, tx_idx,\
1262                         ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head));
1263                 if (m_head)
1264                         m_freem(m_head);
1265                 *m_headp = NULL;
1266                 return (ret);
1267         }
1268
1269         map = ha->tx_ring[txr_idx].tx_buf[tx_idx].map;
1270
1271         ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head, segs, &nsegs,
1272                         BUS_DMA_NOWAIT);
1273
1274         if (ret == EFBIG) {
1275
1276                 struct mbuf *m;
1277
1278                 QL_DPRINT8(ha, (ha->pci_dev, "%s: EFBIG [%d]\n", __func__,
1279                         m_head->m_pkthdr.len));
1280
1281                 m = m_defrag(m_head, M_NOWAIT);
1282                 if (m == NULL) {
1283                         ha->err_tx_defrag++;
1284                         m_freem(m_head);
1285                         *m_headp = NULL;
1286                         device_printf(ha->pci_dev,
1287                                 "%s: m_defrag() = NULL [%d]\n",
1288                                 __func__, ret);
1289                         return (ENOBUFS);
1290                 }
1291                 m_head = m;
1292                 *m_headp = m_head;
1293
1294                 if ((ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head,
1295                                         segs, &nsegs, BUS_DMA_NOWAIT))) {
1296
1297                         ha->err_tx_dmamap_load++;
1298
1299                         device_printf(ha->pci_dev,
1300                                 "%s: bus_dmamap_load_mbuf_sg failed0[%d, %d]\n",
1301                                 __func__, ret, m_head->m_pkthdr.len);
1302
1303                         if (ret != ENOMEM) {
1304                                 m_freem(m_head);
1305                                 *m_headp = NULL;
1306                         }
1307                         return (ret);
1308                 }
1309
1310         } else if (ret) {
1311
1312                 ha->err_tx_dmamap_load++;
1313
1314                 device_printf(ha->pci_dev,
1315                         "%s: bus_dmamap_load_mbuf_sg failed1[%d, %d]\n",
1316                         __func__, ret, m_head->m_pkthdr.len);
1317
1318                 if (ret != ENOMEM) {
1319                         m_freem(m_head);
1320                         *m_headp = NULL;
1321                 }
1322                 return (ret);
1323         }
1324
1325         QL_ASSERT(ha, (nsegs != 0), ("qla_send: empty packet"));
1326
1327         bus_dmamap_sync(ha->tx_tag, map, BUS_DMASYNC_PREWRITE);
1328
1329         if (!(ret = ql_hw_send(ha, segs, nsegs, tx_idx, m_head, txr_idx,
1330                                 iscsi_pdu))) {
1331                 ha->tx_ring[txr_idx].count++;
1332                 if (iscsi_pdu)
1333                         ha->tx_ring[txr_idx].iscsi_pkt_count++;
1334                 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head = m_head;
1335         } else {
1336                 bus_dmamap_unload(ha->tx_tag, map); 
1337                 if (ret == EINVAL) {
1338                         if (m_head)
1339                                 m_freem(m_head);
1340                         *m_headp = NULL;
1341                 }
1342         }
1343
1344         QL_DPRINT8(ha, (ha->pci_dev, "%s: exit\n", __func__));
1345         return (ret);
1346 }
1347
1348 static int
1349 qla_alloc_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1350 {
1351         snprintf(fp->tx_mtx_name, sizeof(fp->tx_mtx_name),
1352                 "qla%d_fp%d_tx_mq_lock", ha->pci_func, fp->txr_idx);
1353
1354         mtx_init(&fp->tx_mtx, fp->tx_mtx_name, NULL, MTX_DEF);
1355
1356         fp->tx_br = buf_ring_alloc(NUM_TX_DESCRIPTORS, M_DEVBUF,
1357                                    M_NOWAIT, &fp->tx_mtx);
1358         if (fp->tx_br == NULL) {
1359             QL_DPRINT1(ha, (ha->pci_dev, "buf_ring_alloc failed for "
1360                 " fp[%d, %d]\n", ha->pci_func, fp->txr_idx));
1361             return (-ENOMEM);
1362         }
1363         return 0;
1364 }
1365
1366 static void
1367 qla_free_tx_br(qla_host_t *ha, qla_tx_fp_t *fp)
1368 {
1369         struct mbuf *mp;
1370         struct ifnet *ifp = ha->ifp;
1371
1372         if (mtx_initialized(&fp->tx_mtx)) {
1373
1374                 if (fp->tx_br != NULL) {
1375
1376                         mtx_lock(&fp->tx_mtx);
1377
1378                         while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1379                                 m_freem(mp);
1380                         }
1381
1382                         mtx_unlock(&fp->tx_mtx);
1383
1384                         buf_ring_free(fp->tx_br, M_DEVBUF);
1385                         fp->tx_br = NULL;
1386                 }
1387                 mtx_destroy(&fp->tx_mtx);
1388         }
1389         return;
1390 }
1391
1392 static void
1393 qla_fp_taskqueue(void *context, int pending)
1394 {
1395         qla_tx_fp_t *fp;
1396         qla_host_t *ha;
1397         struct ifnet *ifp;
1398         struct mbuf  *mp = NULL;
1399         int ret = 0;
1400         uint32_t txr_idx;
1401         uint32_t iscsi_pdu = 0;
1402         uint32_t rx_pkts_left = -1;
1403
1404         fp = context;
1405
1406         if (fp == NULL)
1407                 return;
1408
1409         ha = (qla_host_t *)fp->ha;
1410
1411         ifp = ha->ifp;
1412
1413         txr_idx = fp->txr_idx;
1414
1415         mtx_lock(&fp->tx_mtx);
1416
1417         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING) || (!ha->hw.link_up)) {
1418                 mtx_unlock(&fp->tx_mtx);
1419                 goto qla_fp_taskqueue_exit;
1420         }
1421
1422         while (rx_pkts_left && !ha->stop_rcv &&
1423                 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1424                 rx_pkts_left = ql_rcv_isr(ha, fp->txr_idx, 64);
1425
1426 #ifdef QL_ENABLE_ISCSI_TLV
1427                 ql_hw_tx_done_locked(ha, fp->txr_idx);
1428                 ql_hw_tx_done_locked(ha, (fp->txr_idx + (ha->hw.num_tx_rings >> 1)));
1429 #else
1430                 ql_hw_tx_done_locked(ha, fp->txr_idx);
1431 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1432
1433                 mp = drbr_peek(ifp, fp->tx_br);
1434
1435                 while (mp != NULL) {
1436
1437                         if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE) {
1438 #ifdef QL_ENABLE_ISCSI_TLV
1439                                 if (ql_iscsi_pdu(ha, mp) == 0) {
1440                                         txr_idx = txr_idx +
1441                                                 (ha->hw.num_tx_rings >> 1);
1442                                         iscsi_pdu = 1;
1443                                 } else {
1444                                         iscsi_pdu = 0;
1445                                         txr_idx = fp->txr_idx;
1446                                 }
1447 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1448                         }
1449
1450                         ret = qla_send(ha, &mp, txr_idx, iscsi_pdu);
1451
1452                         if (ret) {
1453                                 if (mp != NULL)
1454                                         drbr_putback(ifp, fp->tx_br, mp);
1455                                 else {
1456                                         drbr_advance(ifp, fp->tx_br);
1457                                 }
1458
1459                                 mtx_unlock(&fp->tx_mtx);
1460
1461                                 goto qla_fp_taskqueue_exit0;
1462                         } else {
1463                                 drbr_advance(ifp, fp->tx_br);
1464                         }
1465
1466                         /* Send a copy of the frame to the BPF listener */
1467                         ETHER_BPF_MTAP(ifp, mp);
1468                         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1469                                 break;
1470
1471                         mp = drbr_peek(ifp, fp->tx_br);
1472                 }
1473         }
1474         mtx_unlock(&fp->tx_mtx);
1475
1476 qla_fp_taskqueue_exit0:
1477
1478         if (rx_pkts_left || ((mp != NULL) && ret)) {
1479                 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1480         } else {
1481                 if (!ha->stop_rcv) {
1482                         QL_ENABLE_INTERRUPTS(ha, fp->txr_idx);
1483                 }
1484         }
1485
1486 qla_fp_taskqueue_exit:
1487
1488         QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1489         return;
1490 }
1491
1492 static int
1493 qla_create_fp_taskqueues(qla_host_t *ha)
1494 {
1495         int     i;
1496         uint8_t tq_name[32];
1497
1498         for (i = 0; i < ha->hw.num_sds_rings; i++) {
1499
1500                 qla_tx_fp_t *fp = &ha->tx_fp[i];
1501
1502                 bzero(tq_name, sizeof (tq_name));
1503                 snprintf(tq_name, sizeof (tq_name), "ql_fp_tq_%d", i);
1504
1505                 TASK_INIT(&fp->fp_task, 0, qla_fp_taskqueue, fp);
1506
1507                 fp->fp_taskqueue = taskqueue_create_fast(tq_name, M_NOWAIT,
1508                                         taskqueue_thread_enqueue,
1509                                         &fp->fp_taskqueue);
1510
1511                 if (fp->fp_taskqueue == NULL)
1512                         return (-1);
1513
1514                 taskqueue_start_threads(&fp->fp_taskqueue, 1, PI_NET, "%s",
1515                         tq_name);
1516
1517                 QL_DPRINT1(ha, (ha->pci_dev, "%s: %p\n", __func__,
1518                         fp->fp_taskqueue));
1519         }
1520
1521         return (0);
1522 }
1523
1524 static void
1525 qla_destroy_fp_taskqueues(qla_host_t *ha)
1526 {
1527         int     i;
1528
1529         for (i = 0; i < ha->hw.num_sds_rings; i++) {
1530
1531                 qla_tx_fp_t *fp = &ha->tx_fp[i];
1532
1533                 if (fp->fp_taskqueue != NULL) {
1534                         taskqueue_drain(fp->fp_taskqueue, &fp->fp_task);
1535                         taskqueue_free(fp->fp_taskqueue);
1536                         fp->fp_taskqueue = NULL;
1537                 }
1538         }
1539         return;
1540 }
1541
1542 static void
1543 qla_drain_fp_taskqueues(qla_host_t *ha)
1544 {
1545         int     i;
1546
1547         for (i = 0; i < ha->hw.num_sds_rings; i++) {
1548                 qla_tx_fp_t *fp = &ha->tx_fp[i];
1549
1550                 if (fp->fp_taskqueue != NULL) {
1551                         taskqueue_drain(fp->fp_taskqueue, &fp->fp_task);
1552                 }
1553         }
1554         return;
1555 }
1556
1557 static int
1558 qla_transmit(struct ifnet *ifp, struct mbuf  *mp)
1559 {
1560         qla_host_t *ha = (qla_host_t *)ifp->if_softc;
1561         qla_tx_fp_t *fp;
1562         int rss_id = 0;
1563         int ret = 0;
1564
1565         QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1566
1567 #if __FreeBSD_version >= 1100000
1568         if (M_HASHTYPE_GET(mp) != M_HASHTYPE_NONE)
1569 #else
1570         if (mp->m_flags & M_FLOWID)
1571 #endif
1572                 rss_id = (mp->m_pkthdr.flowid & Q8_RSS_IND_TBL_MAX_IDX) %
1573                                         ha->hw.num_sds_rings;
1574         fp = &ha->tx_fp[rss_id];
1575
1576         if (fp->tx_br == NULL) {
1577                 ret = EINVAL;
1578                 goto qla_transmit_exit;
1579         }
1580
1581         if (mp != NULL) {
1582                 ret = drbr_enqueue(ifp, fp->tx_br, mp);
1583         }
1584
1585         if (fp->fp_taskqueue != NULL)
1586                 taskqueue_enqueue(fp->fp_taskqueue, &fp->fp_task);
1587
1588         ret = 0;
1589
1590 qla_transmit_exit:
1591
1592         QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = %d\n", __func__, ret));
1593         return ret;
1594 }
1595
1596 static void
1597 qla_qflush(struct ifnet *ifp)
1598 {
1599         int                     i;
1600         qla_tx_fp_t             *fp;
1601         struct mbuf             *mp;
1602         qla_host_t              *ha;
1603
1604         ha = (qla_host_t *)ifp->if_softc;
1605
1606         QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1607
1608         for (i = 0; i < ha->hw.num_sds_rings; i++) {
1609
1610                 fp = &ha->tx_fp[i];
1611
1612                 if (fp == NULL)
1613                         continue;
1614
1615                 if (fp->tx_br) {
1616                         mtx_lock(&fp->tx_mtx);
1617
1618                         while ((mp = drbr_dequeue(ifp, fp->tx_br)) != NULL) {
1619                                 m_freem(mp);
1620                         }
1621                         mtx_unlock(&fp->tx_mtx);
1622                 }
1623         }
1624         QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1625
1626         return;
1627 }
1628
1629 static void
1630 qla_stop(qla_host_t *ha)
1631 {
1632         struct ifnet *ifp = ha->ifp;
1633         device_t        dev;
1634         int i = 0;
1635
1636         dev = ha->pci_dev;
1637
1638         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1639         ha->qla_watchdog_pause = 1;
1640
1641         for (i = 0; i < ha->hw.num_sds_rings; i++) {
1642                 qla_tx_fp_t *fp;
1643
1644                 fp = &ha->tx_fp[i];
1645
1646                 if (fp == NULL)
1647                         continue;
1648
1649                 if (fp->tx_br != NULL) {
1650                         mtx_lock(&fp->tx_mtx);
1651                         mtx_unlock(&fp->tx_mtx);
1652                 }
1653         }
1654
1655         while (!ha->qla_watchdog_paused)
1656                 qla_mdelay(__func__, 1);
1657
1658         ha->qla_interface_up = 0;
1659
1660         qla_drain_fp_taskqueues(ha);
1661
1662         ql_del_hw_if(ha);
1663
1664         qla_free_xmt_bufs(ha);
1665         qla_free_rcv_bufs(ha);
1666
1667         return;
1668 }
1669
1670 /*
1671  * Buffer Management Functions for Transmit and Receive Rings
1672  */
1673 static int
1674 qla_alloc_xmt_bufs(qla_host_t *ha)
1675 {
1676         int ret = 0;
1677         uint32_t i, j;
1678         qla_tx_buf_t *txb;
1679
1680         if (bus_dma_tag_create(NULL,    /* parent */
1681                 1, 0,    /* alignment, bounds */
1682                 BUS_SPACE_MAXADDR,       /* lowaddr */
1683                 BUS_SPACE_MAXADDR,       /* highaddr */
1684                 NULL, NULL,      /* filter, filterarg */
1685                 QLA_MAX_TSO_FRAME_SIZE,     /* maxsize */
1686                 QLA_MAX_SEGMENTS,        /* nsegments */
1687                 PAGE_SIZE,        /* maxsegsize */
1688                 BUS_DMA_ALLOCNOW,        /* flags */
1689                 NULL,    /* lockfunc */
1690                 NULL,    /* lockfuncarg */
1691                 &ha->tx_tag)) {
1692                 device_printf(ha->pci_dev, "%s: tx_tag alloc failed\n",
1693                         __func__);
1694                 return (ENOMEM);
1695         }
1696
1697         for (i = 0; i < ha->hw.num_tx_rings; i++) {
1698                 bzero((void *)ha->tx_ring[i].tx_buf,
1699                         (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1700         }
1701
1702         for (j = 0; j < ha->hw.num_tx_rings; j++) {
1703                 for (i = 0; i < NUM_TX_DESCRIPTORS; i++) {
1704
1705                         txb = &ha->tx_ring[j].tx_buf[i];
1706
1707                         if ((ret = bus_dmamap_create(ha->tx_tag,
1708                                         BUS_DMA_NOWAIT, &txb->map))) {
1709
1710                                 ha->err_tx_dmamap_create++;
1711                                 device_printf(ha->pci_dev,
1712                                         "%s: bus_dmamap_create failed[%d]\n",
1713                                         __func__, ret);
1714
1715                                 qla_free_xmt_bufs(ha);
1716
1717                                 return (ret);
1718                         }
1719                 }
1720         }
1721
1722         return 0;
1723 }
1724
1725 /*
1726  * Release mbuf after it sent on the wire
1727  */
1728 static void
1729 qla_clear_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb)
1730 {
1731         QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1732
1733         if (txb->m_head) {
1734                 bus_dmamap_sync(ha->tx_tag, txb->map,
1735                         BUS_DMASYNC_POSTWRITE);
1736
1737                 bus_dmamap_unload(ha->tx_tag, txb->map);
1738
1739                 m_freem(txb->m_head);
1740                 txb->m_head = NULL;
1741
1742                 bus_dmamap_destroy(ha->tx_tag, txb->map);
1743                 txb->map = NULL;
1744         }
1745
1746         if (txb->map) {
1747                 bus_dmamap_unload(ha->tx_tag, txb->map);
1748                 bus_dmamap_destroy(ha->tx_tag, txb->map);
1749                 txb->map = NULL;
1750         }
1751
1752         QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1753 }
1754
1755 static void
1756 qla_free_xmt_bufs(qla_host_t *ha)
1757 {
1758         int             i, j;
1759
1760         for (j = 0; j < ha->hw.num_tx_rings; j++) {
1761                 for (i = 0; i < NUM_TX_DESCRIPTORS; i++)
1762                         qla_clear_tx_buf(ha, &ha->tx_ring[j].tx_buf[i]);
1763         }
1764
1765         if (ha->tx_tag != NULL) {
1766                 bus_dma_tag_destroy(ha->tx_tag);
1767                 ha->tx_tag = NULL;
1768         }
1769
1770         for (i = 0; i < ha->hw.num_tx_rings; i++) {
1771                 bzero((void *)ha->tx_ring[i].tx_buf,
1772                         (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1773         }
1774         return;
1775 }
1776
1777
1778 static int
1779 qla_alloc_rcv_std(qla_host_t *ha)
1780 {
1781         int             i, j, k, r, ret = 0;
1782         qla_rx_buf_t    *rxb;
1783         qla_rx_ring_t   *rx_ring;
1784
1785         for (r = 0; r < ha->hw.num_rds_rings; r++) {
1786
1787                 rx_ring = &ha->rx_ring[r];
1788
1789                 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1790
1791                         rxb = &rx_ring->rx_buf[i];
1792
1793                         ret = bus_dmamap_create(ha->rx_tag, BUS_DMA_NOWAIT,
1794                                         &rxb->map);
1795
1796                         if (ret) {
1797                                 device_printf(ha->pci_dev,
1798                                         "%s: dmamap[%d, %d] failed\n",
1799                                         __func__, r, i);
1800
1801                                 for (k = 0; k < r; k++) {
1802                                         for (j = 0; j < NUM_RX_DESCRIPTORS;
1803                                                 j++) {
1804                                                 rxb = &ha->rx_ring[k].rx_buf[j];
1805                                                 bus_dmamap_destroy(ha->rx_tag,
1806                                                         rxb->map);
1807                                         }
1808                                 }
1809
1810                                 for (j = 0; j < i; j++) {
1811                                         bus_dmamap_destroy(ha->rx_tag,
1812                                                 rx_ring->rx_buf[j].map);
1813                                 }
1814                                 goto qla_alloc_rcv_std_err;
1815                         }
1816                 }
1817         }
1818
1819         qla_init_hw_rcv_descriptors(ha);
1820
1821         
1822         for (r = 0; r < ha->hw.num_rds_rings; r++) {
1823
1824                 rx_ring = &ha->rx_ring[r];
1825
1826                 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1827                         rxb = &rx_ring->rx_buf[i];
1828                         rxb->handle = i;
1829                         if (!(ret = ql_get_mbuf(ha, rxb, NULL))) {
1830                                 /*
1831                                  * set the physical address in the
1832                                  * corresponding descriptor entry in the
1833                                  * receive ring/queue for the hba 
1834                                  */
1835                                 qla_set_hw_rcv_desc(ha, r, i, rxb->handle,
1836                                         rxb->paddr,
1837                                         (rxb->m_head)->m_pkthdr.len);
1838                         } else {
1839                                 device_printf(ha->pci_dev,
1840                                         "%s: ql_get_mbuf [%d, %d] failed\n",
1841                                         __func__, r, i);
1842                                 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1843                                 goto qla_alloc_rcv_std_err;
1844                         }
1845                 }
1846         }
1847         return 0;
1848
1849 qla_alloc_rcv_std_err:
1850         return (-1);
1851 }
1852
1853 static void
1854 qla_free_rcv_std(qla_host_t *ha)
1855 {
1856         int             i, r;
1857         qla_rx_buf_t    *rxb;
1858
1859         for (r = 0; r < ha->hw.num_rds_rings; r++) {
1860                 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1861                         rxb = &ha->rx_ring[r].rx_buf[i];
1862                         if (rxb->m_head != NULL) {
1863                                 bus_dmamap_unload(ha->rx_tag, rxb->map);
1864                                 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1865                                 m_freem(rxb->m_head);
1866                                 rxb->m_head = NULL;
1867                         }
1868                 }
1869         }
1870         return;
1871 }
1872
1873 static int
1874 qla_alloc_rcv_bufs(qla_host_t *ha)
1875 {
1876         int             i, ret = 0;
1877
1878         if (bus_dma_tag_create(NULL,    /* parent */
1879                         1, 0,    /* alignment, bounds */
1880                         BUS_SPACE_MAXADDR,       /* lowaddr */
1881                         BUS_SPACE_MAXADDR,       /* highaddr */
1882                         NULL, NULL,      /* filter, filterarg */
1883                         MJUM9BYTES,     /* maxsize */
1884                         1,        /* nsegments */
1885                         MJUM9BYTES,        /* maxsegsize */
1886                         BUS_DMA_ALLOCNOW,        /* flags */
1887                         NULL,    /* lockfunc */
1888                         NULL,    /* lockfuncarg */
1889                         &ha->rx_tag)) {
1890
1891                 device_printf(ha->pci_dev, "%s: rx_tag alloc failed\n",
1892                         __func__);
1893
1894                 return (ENOMEM);
1895         }
1896
1897         bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1898
1899         for (i = 0; i < ha->hw.num_sds_rings; i++) {
1900                 ha->hw.sds[i].sdsr_next = 0;
1901                 ha->hw.sds[i].rxb_free = NULL;
1902                 ha->hw.sds[i].rx_free = 0;
1903         }
1904
1905         ret = qla_alloc_rcv_std(ha);
1906
1907         return (ret);
1908 }
1909
1910 static void
1911 qla_free_rcv_bufs(qla_host_t *ha)
1912 {
1913         int             i;
1914
1915         qla_free_rcv_std(ha);
1916
1917         if (ha->rx_tag != NULL) {
1918                 bus_dma_tag_destroy(ha->rx_tag);
1919                 ha->rx_tag = NULL;
1920         }
1921
1922         bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1923
1924         for (i = 0; i < ha->hw.num_sds_rings; i++) {
1925                 ha->hw.sds[i].sdsr_next = 0;
1926                 ha->hw.sds[i].rxb_free = NULL;
1927                 ha->hw.sds[i].rx_free = 0;
1928         }
1929
1930         return;
1931 }
1932
1933 int
1934 ql_get_mbuf(qla_host_t *ha, qla_rx_buf_t *rxb, struct mbuf *nmp)
1935 {
1936         register struct mbuf *mp = nmp;
1937         struct ifnet            *ifp;
1938         int                     ret = 0;
1939         uint32_t                offset;
1940         bus_dma_segment_t       segs[1];
1941         int                     nsegs, mbuf_size;
1942
1943         QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1944
1945         ifp = ha->ifp;
1946
1947         if (ha->hw.enable_9kb)
1948                 mbuf_size = MJUM9BYTES;
1949         else
1950                 mbuf_size = MCLBYTES;
1951
1952         if (mp == NULL) {
1953
1954                 if (QL_ERR_INJECT(ha, INJCT_M_GETCL_M_GETJCL_FAILURE))
1955                         return(-1);
1956
1957                 if (ha->hw.enable_9kb)
1958                         mp = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, mbuf_size);
1959                 else
1960                         mp = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1961
1962                 if (mp == NULL) {
1963                         ha->err_m_getcl++;
1964                         ret = ENOBUFS;
1965                         device_printf(ha->pci_dev,
1966                                         "%s: m_getcl failed\n", __func__);
1967                         goto exit_ql_get_mbuf;
1968                 }
1969                 mp->m_len = mp->m_pkthdr.len = mbuf_size;
1970         } else {
1971                 mp->m_len = mp->m_pkthdr.len = mbuf_size;
1972                 mp->m_data = mp->m_ext.ext_buf;
1973                 mp->m_next = NULL;
1974         }
1975
1976         offset = (uint32_t)((unsigned long long)mp->m_data & 0x7ULL);
1977         if (offset) {
1978                 offset = 8 - offset;
1979                 m_adj(mp, offset);
1980         }
1981
1982         /*
1983          * Using memory from the mbuf cluster pool, invoke the bus_dma
1984          * machinery to arrange the memory mapping.
1985          */
1986         ret = bus_dmamap_load_mbuf_sg(ha->rx_tag, rxb->map,
1987                         mp, segs, &nsegs, BUS_DMA_NOWAIT);
1988         rxb->paddr = segs[0].ds_addr;
1989
1990         if (ret || !rxb->paddr || (nsegs != 1)) {
1991                 m_free(mp);
1992                 rxb->m_head = NULL;
1993                 device_printf(ha->pci_dev,
1994                         "%s: bus_dmamap_load failed[%d, 0x%016llx, %d]\n",
1995                         __func__, ret, (long long unsigned int)rxb->paddr,
1996                         nsegs);
1997                 ret = -1;
1998                 goto exit_ql_get_mbuf;
1999         }
2000         rxb->m_head = mp;
2001         bus_dmamap_sync(ha->rx_tag, rxb->map, BUS_DMASYNC_PREREAD);
2002
2003 exit_ql_get_mbuf:
2004         QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = 0x%08x\n", __func__, ret));
2005         return (ret);
2006 }
2007
2008
2009 static void
2010 qla_get_peer(qla_host_t *ha)
2011 {
2012         device_t *peers;
2013         int count, i, slot;
2014         int my_slot = pci_get_slot(ha->pci_dev);
2015
2016         if (device_get_children(device_get_parent(ha->pci_dev), &peers, &count))
2017                 return;
2018
2019         for (i = 0; i < count; i++) {
2020                 slot = pci_get_slot(peers[i]);
2021
2022                 if ((slot >= 0) && (slot == my_slot) &&
2023                         (pci_get_device(peers[i]) ==
2024                                 pci_get_device(ha->pci_dev))) {
2025                         if (ha->pci_dev != peers[i]) 
2026                                 ha->peer_dev = peers[i];
2027                 }
2028         }
2029 }
2030
2031 static void
2032 qla_send_msg_to_peer(qla_host_t *ha, uint32_t msg_to_peer)
2033 {
2034         qla_host_t *ha_peer;
2035         
2036         if (ha->peer_dev) {
2037                 if ((ha_peer = device_get_softc(ha->peer_dev)) != NULL) {
2038
2039                         ha_peer->msg_from_peer = msg_to_peer;
2040                 }
2041         }
2042 }
2043
2044 static void
2045 qla_error_recovery(void *context, int pending)
2046 {
2047         qla_host_t *ha = context;
2048         uint32_t msecs_100 = 100;
2049         struct ifnet *ifp = ha->ifp;
2050         int i = 0;
2051
2052 device_printf(ha->pci_dev, "%s: \n", __func__);
2053         ha->hw.imd_compl = 1;
2054
2055         if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2056                 return;
2057
2058 device_printf(ha->pci_dev, "%s: enter\n", __func__);
2059
2060         if (ha->qla_interface_up) {
2061
2062                 qla_mdelay(__func__, 300);
2063
2064                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2065
2066                 for (i = 0; i < ha->hw.num_sds_rings; i++) {
2067                         qla_tx_fp_t *fp;
2068
2069                         fp = &ha->tx_fp[i];
2070
2071                         if (fp == NULL)
2072                                 continue;
2073
2074                         if (fp->tx_br != NULL) {
2075                                 mtx_lock(&fp->tx_mtx);
2076                                 mtx_unlock(&fp->tx_mtx);
2077                         }
2078                 }
2079         }
2080
2081
2082         qla_drain_fp_taskqueues(ha);
2083
2084         if ((ha->pci_func & 0x1) == 0) {
2085
2086                 if (!ha->msg_from_peer) {
2087                         qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2088
2089                         while ((ha->msg_from_peer != QL_PEER_MSG_ACK) &&
2090                                 msecs_100--)
2091                                 qla_mdelay(__func__, 100);
2092                 }
2093
2094                 ha->msg_from_peer = 0;
2095
2096                 if (ha->enable_minidump)
2097                         ql_minidump(ha);
2098
2099                 (void) ql_init_hw(ha);
2100
2101                 if (ha->qla_interface_up) {
2102                         qla_free_xmt_bufs(ha);
2103                         qla_free_rcv_bufs(ha);
2104                 }
2105
2106                 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2107
2108         } else {
2109                 if (ha->msg_from_peer == QL_PEER_MSG_RESET) {
2110
2111                         ha->msg_from_peer = 0;
2112
2113                         qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
2114                 } else {
2115                         qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
2116                 }
2117
2118                 while ((ha->msg_from_peer != QL_PEER_MSG_ACK)  && msecs_100--)
2119                         qla_mdelay(__func__, 100);
2120                 ha->msg_from_peer = 0;
2121
2122                 (void) ql_init_hw(ha);
2123
2124                 qla_mdelay(__func__, 1000);
2125
2126                 if (ha->qla_interface_up) {
2127                         qla_free_xmt_bufs(ha);
2128                         qla_free_rcv_bufs(ha);
2129                 }
2130         }
2131
2132         if (ha->qla_interface_up) {
2133
2134                 if (qla_alloc_xmt_bufs(ha) != 0) {
2135                         goto qla_error_recovery_exit;
2136                 }
2137                 qla_confirm_9kb_enable(ha);
2138
2139                 if (qla_alloc_rcv_bufs(ha) != 0) {
2140                         goto qla_error_recovery_exit;
2141                 }
2142
2143                 ha->stop_rcv = 0;
2144
2145                 if (ql_init_hw_if(ha) == 0) {
2146                         ifp = ha->ifp;
2147                         ifp->if_drv_flags |= IFF_DRV_RUNNING;
2148                         ha->qla_watchdog_pause = 0;
2149                 }
2150         } else
2151                 ha->qla_watchdog_pause = 0;
2152
2153 qla_error_recovery_exit:
2154
2155 device_printf(ha->pci_dev, "%s: exit\n", __func__);
2156
2157         QLA_UNLOCK(ha, __func__);
2158
2159         callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
2160                 qla_watchdog, ha);
2161         return;
2162 }
2163
2164 static void
2165 qla_async_event(void *context, int pending)
2166 {
2167         qla_host_t *ha = context;
2168
2169         if (QLA_LOCK(ha, __func__, -1, 0) != 0)
2170                 return;
2171
2172         if (ha->async_event) {
2173                 ha->async_event = 0;
2174                 qla_hw_async_event(ha);
2175         }
2176
2177         QLA_UNLOCK(ha, __func__);
2178
2179         return;
2180 }
2181
2182 static void
2183 qla_stats(void *context, int pending)
2184 {
2185         qla_host_t *ha;
2186
2187         ha = context;
2188
2189         ql_get_stats(ha);
2190         return;
2191 }
2192