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