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