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