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