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