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