]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/qlxgbe/ql_os.c
MFC r306790
[FreeBSD/stable/10.git] / sys / dev / qlxgbe / ql_os.c
1 /*
2  * Copyright (c) 2013-2016 Qlogic Corporation
3  * All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions
7  *  are met:
8  *
9  *  1. Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  *  2. Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *
15  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  *  and ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  *  POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 /*
29  * File: ql_os.c
30  * Author : David C Somayajulu, Qlogic Corporation, Aliso Viejo, CA 92656.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36
37 #include "ql_os.h"
38 #include "ql_hw.h"
39 #include "ql_def.h"
40 #include "ql_inline.h"
41 #include "ql_ver.h"
42 #include "ql_glbl.h"
43 #include "ql_dbg.h"
44 #include <sys/smp.h>
45
46 /*
47  * Some PCI Configuration Space Related Defines
48  */
49
50 #ifndef PCI_VENDOR_QLOGIC
51 #define PCI_VENDOR_QLOGIC       0x1077
52 #endif
53
54 #ifndef PCI_PRODUCT_QLOGIC_ISP8030
55 #define PCI_PRODUCT_QLOGIC_ISP8030      0x8030
56 #endif
57
58 #define PCI_QLOGIC_ISP8030 \
59         ((PCI_PRODUCT_QLOGIC_ISP8030 << 16) | PCI_VENDOR_QLOGIC)
60
61 /*
62  * static functions
63  */
64 static int qla_alloc_parent_dma_tag(qla_host_t *ha);
65 static void qla_free_parent_dma_tag(qla_host_t *ha);
66 static int qla_alloc_xmt_bufs(qla_host_t *ha);
67 static void qla_free_xmt_bufs(qla_host_t *ha);
68 static int qla_alloc_rcv_bufs(qla_host_t *ha);
69 static void qla_free_rcv_bufs(qla_host_t *ha);
70 static void qla_clear_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb);
71
72 static void qla_init_ifnet(device_t dev, qla_host_t *ha);
73 static int qla_sysctl_get_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         /* check if flowid is set */
1204
1205         if (M_HASHTYPE_GET(m_head) != M_HASHTYPE_NONE) {
1206 #ifdef QL_ENABLE_ISCSI_TLV
1207                 if (qla_iscsi_pdu(ha, m_head) == 0) {
1208                         iscsi_pdu = 1;
1209                         txr_idx = m_head->m_pkthdr.flowid &
1210                                         ((ha->hw.num_tx_rings >> 1) - 1);
1211                 } else {
1212                         txr_idx = m_head->m_pkthdr.flowid &
1213                                         (ha->hw.num_tx_rings - 1);
1214                 }
1215 #else
1216                 txr_idx = m_head->m_pkthdr.flowid & (ha->hw.num_tx_rings - 1);
1217 #endif /* #ifdef QL_ENABLE_ISCSI_TLV */
1218         }
1219
1220
1221         tx_idx = ha->hw.tx_cntxt[txr_idx].txr_next;
1222         map = ha->tx_ring[txr_idx].tx_buf[tx_idx].map;
1223
1224         ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head, segs, &nsegs,
1225                         BUS_DMA_NOWAIT);
1226
1227         if (ret == EFBIG) {
1228
1229                 struct mbuf *m;
1230
1231                 QL_DPRINT8(ha, (ha->pci_dev, "%s: EFBIG [%d]\n", __func__,
1232                         m_head->m_pkthdr.len));
1233
1234                 m = m_defrag(m_head, M_NOWAIT);
1235                 if (m == NULL) {
1236                         ha->err_tx_defrag++;
1237                         m_freem(m_head);
1238                         *m_headp = NULL;
1239                         device_printf(ha->pci_dev,
1240                                 "%s: m_defrag() = NULL [%d]\n",
1241                                 __func__, ret);
1242                         return (ENOBUFS);
1243                 }
1244                 m_head = m;
1245                 *m_headp = m_head;
1246
1247                 if ((ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head,
1248                                         segs, &nsegs, BUS_DMA_NOWAIT))) {
1249
1250                         ha->err_tx_dmamap_load++;
1251
1252                         device_printf(ha->pci_dev,
1253                                 "%s: bus_dmamap_load_mbuf_sg failed0[%d, %d]\n",
1254                                 __func__, ret, m_head->m_pkthdr.len);
1255
1256                         if (ret != ENOMEM) {
1257                                 m_freem(m_head);
1258                                 *m_headp = NULL;
1259                         }
1260                         return (ret);
1261                 }
1262
1263         } else if (ret) {
1264
1265                 ha->err_tx_dmamap_load++;
1266
1267                 device_printf(ha->pci_dev,
1268                         "%s: bus_dmamap_load_mbuf_sg failed1[%d, %d]\n",
1269                         __func__, ret, m_head->m_pkthdr.len);
1270
1271                 if (ret != ENOMEM) {
1272                         m_freem(m_head);
1273                         *m_headp = NULL;
1274                 }
1275                 return (ret);
1276         }
1277
1278         QL_ASSERT(ha, (nsegs != 0), ("qla_send: empty packet"));
1279
1280         bus_dmamap_sync(ha->tx_tag, map, BUS_DMASYNC_PREWRITE);
1281
1282         if (!(ret = ql_hw_send(ha, segs, nsegs, tx_idx, m_head, txr_idx,
1283                                 iscsi_pdu))) {
1284                 ha->tx_ring[txr_idx].count++;
1285                 ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head = m_head;
1286         } else {
1287                 if (ret == EINVAL) {
1288                         if (m_head)
1289                                 m_freem(m_head);
1290                         *m_headp = NULL;
1291                 }
1292         }
1293
1294         QL_DPRINT8(ha, (ha->pci_dev, "%s: exit\n", __func__));
1295         return (ret);
1296 }
1297
1298 static void
1299 qla_stop(qla_host_t *ha)
1300 {
1301         struct ifnet *ifp = ha->ifp;
1302         device_t        dev;
1303
1304         dev = ha->pci_dev;
1305
1306         ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE | IFF_DRV_RUNNING);
1307         QLA_TX_LOCK(ha); QLA_TX_UNLOCK(ha);
1308
1309         ha->flags.qla_watchdog_pause = 1;
1310
1311         while (!ha->qla_watchdog_paused)
1312                 qla_mdelay(__func__, 1);
1313
1314         ha->flags.qla_interface_up = 0;
1315
1316         ql_hw_stop_rcv(ha);
1317
1318         ql_del_hw_if(ha);
1319
1320         qla_free_xmt_bufs(ha);
1321         qla_free_rcv_bufs(ha);
1322
1323         return;
1324 }
1325
1326 /*
1327  * Buffer Management Functions for Transmit and Receive Rings
1328  */
1329 static int
1330 qla_alloc_xmt_bufs(qla_host_t *ha)
1331 {
1332         int ret = 0;
1333         uint32_t i, j;
1334         qla_tx_buf_t *txb;
1335
1336         if (bus_dma_tag_create(NULL,    /* parent */
1337                 1, 0,    /* alignment, bounds */
1338                 BUS_SPACE_MAXADDR,       /* lowaddr */
1339                 BUS_SPACE_MAXADDR,       /* highaddr */
1340                 NULL, NULL,      /* filter, filterarg */
1341                 QLA_MAX_TSO_FRAME_SIZE,     /* maxsize */
1342                 QLA_MAX_SEGMENTS,        /* nsegments */
1343                 PAGE_SIZE,        /* maxsegsize */
1344                 BUS_DMA_ALLOCNOW,        /* flags */
1345                 NULL,    /* lockfunc */
1346                 NULL,    /* lockfuncarg */
1347                 &ha->tx_tag)) {
1348                 device_printf(ha->pci_dev, "%s: tx_tag alloc failed\n",
1349                         __func__);
1350                 return (ENOMEM);
1351         }
1352
1353         for (i = 0; i < ha->hw.num_tx_rings; i++) {
1354                 bzero((void *)ha->tx_ring[i].tx_buf,
1355                         (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1356         }
1357
1358         for (j = 0; j < ha->hw.num_tx_rings; j++) {
1359                 for (i = 0; i < NUM_TX_DESCRIPTORS; i++) {
1360
1361                         txb = &ha->tx_ring[j].tx_buf[i];
1362
1363                         if ((ret = bus_dmamap_create(ha->tx_tag,
1364                                         BUS_DMA_NOWAIT, &txb->map))) {
1365
1366                                 ha->err_tx_dmamap_create++;
1367                                 device_printf(ha->pci_dev,
1368                                         "%s: bus_dmamap_create failed[%d]\n",
1369                                         __func__, ret);
1370
1371                                 qla_free_xmt_bufs(ha);
1372
1373                                 return (ret);
1374                         }
1375                 }
1376         }
1377
1378         return 0;
1379 }
1380
1381 /*
1382  * Release mbuf after it sent on the wire
1383  */
1384 static void
1385 qla_clear_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb)
1386 {
1387         QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1388
1389         if (txb->m_head && txb->map) {
1390
1391                 bus_dmamap_unload(ha->tx_tag, txb->map);
1392
1393                 m_freem(txb->m_head);
1394                 txb->m_head = NULL;
1395         }
1396
1397         if (txb->map)
1398                 bus_dmamap_destroy(ha->tx_tag, txb->map);
1399
1400         QL_DPRINT2(ha, (ha->pci_dev, "%s: exit\n", __func__));
1401 }
1402
1403 static void
1404 qla_free_xmt_bufs(qla_host_t *ha)
1405 {
1406         int             i, j;
1407
1408         for (j = 0; j < ha->hw.num_tx_rings; j++) {
1409                 for (i = 0; i < NUM_TX_DESCRIPTORS; i++)
1410                         qla_clear_tx_buf(ha, &ha->tx_ring[j].tx_buf[i]);
1411         }
1412
1413         if (ha->tx_tag != NULL) {
1414                 bus_dma_tag_destroy(ha->tx_tag);
1415                 ha->tx_tag = NULL;
1416         }
1417
1418         for (i = 0; i < ha->hw.num_tx_rings; i++) {
1419                 bzero((void *)ha->tx_ring[i].tx_buf,
1420                         (sizeof(qla_tx_buf_t) * NUM_TX_DESCRIPTORS));
1421         }
1422         return;
1423 }
1424
1425
1426 static int
1427 qla_alloc_rcv_std(qla_host_t *ha)
1428 {
1429         int             i, j, k, r, ret = 0;
1430         qla_rx_buf_t    *rxb;
1431         qla_rx_ring_t   *rx_ring;
1432
1433         for (r = 0; r < ha->hw.num_rds_rings; r++) {
1434
1435                 rx_ring = &ha->rx_ring[r];
1436
1437                 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1438
1439                         rxb = &rx_ring->rx_buf[i];
1440
1441                         ret = bus_dmamap_create(ha->rx_tag, BUS_DMA_NOWAIT,
1442                                         &rxb->map);
1443
1444                         if (ret) {
1445                                 device_printf(ha->pci_dev,
1446                                         "%s: dmamap[%d, %d] failed\n",
1447                                         __func__, r, i);
1448
1449                                 for (k = 0; k < r; k++) {
1450                                         for (j = 0; j < NUM_RX_DESCRIPTORS;
1451                                                 j++) {
1452                                                 rxb = &ha->rx_ring[k].rx_buf[j];
1453                                                 bus_dmamap_destroy(ha->rx_tag,
1454                                                         rxb->map);
1455                                         }
1456                                 }
1457
1458                                 for (j = 0; j < i; j++) {
1459                                         bus_dmamap_destroy(ha->rx_tag,
1460                                                 rx_ring->rx_buf[j].map);
1461                                 }
1462                                 goto qla_alloc_rcv_std_err;
1463                         }
1464                 }
1465         }
1466
1467         qla_init_hw_rcv_descriptors(ha);
1468
1469         
1470         for (r = 0; r < ha->hw.num_rds_rings; r++) {
1471
1472                 rx_ring = &ha->rx_ring[r];
1473
1474                 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1475                         rxb = &rx_ring->rx_buf[i];
1476                         rxb->handle = i;
1477                         if (!(ret = ql_get_mbuf(ha, rxb, NULL))) {
1478                                 /*
1479                                  * set the physical address in the
1480                                  * corresponding descriptor entry in the
1481                                  * receive ring/queue for the hba 
1482                                  */
1483                                 qla_set_hw_rcv_desc(ha, r, i, rxb->handle,
1484                                         rxb->paddr,
1485                                         (rxb->m_head)->m_pkthdr.len);
1486                         } else {
1487                                 device_printf(ha->pci_dev,
1488                                         "%s: ql_get_mbuf [%d, %d] failed\n",
1489                                         __func__, r, i);
1490                                 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1491                                 goto qla_alloc_rcv_std_err;
1492                         }
1493                 }
1494         }
1495         return 0;
1496
1497 qla_alloc_rcv_std_err:
1498         return (-1);
1499 }
1500
1501 static void
1502 qla_free_rcv_std(qla_host_t *ha)
1503 {
1504         int             i, r;
1505         qla_rx_buf_t    *rxb;
1506
1507         for (r = 0; r < ha->hw.num_rds_rings; r++) {
1508                 for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1509                         rxb = &ha->rx_ring[r].rx_buf[i];
1510                         if (rxb->m_head != NULL) {
1511                                 bus_dmamap_unload(ha->rx_tag, rxb->map);
1512                                 bus_dmamap_destroy(ha->rx_tag, rxb->map);
1513                                 m_freem(rxb->m_head);
1514                                 rxb->m_head = NULL;
1515                         }
1516                 }
1517         }
1518         return;
1519 }
1520
1521 static int
1522 qla_alloc_rcv_bufs(qla_host_t *ha)
1523 {
1524         int             i, ret = 0;
1525
1526         if (bus_dma_tag_create(NULL,    /* parent */
1527                         1, 0,    /* alignment, bounds */
1528                         BUS_SPACE_MAXADDR,       /* lowaddr */
1529                         BUS_SPACE_MAXADDR,       /* highaddr */
1530                         NULL, NULL,      /* filter, filterarg */
1531                         MJUM9BYTES,     /* maxsize */
1532                         1,        /* nsegments */
1533                         MJUM9BYTES,        /* maxsegsize */
1534                         BUS_DMA_ALLOCNOW,        /* flags */
1535                         NULL,    /* lockfunc */
1536                         NULL,    /* lockfuncarg */
1537                         &ha->rx_tag)) {
1538
1539                 device_printf(ha->pci_dev, "%s: rx_tag alloc failed\n",
1540                         __func__);
1541
1542                 return (ENOMEM);
1543         }
1544
1545         bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1546
1547         for (i = 0; i < ha->hw.num_sds_rings; i++) {
1548                 ha->hw.sds[i].sdsr_next = 0;
1549                 ha->hw.sds[i].rxb_free = NULL;
1550                 ha->hw.sds[i].rx_free = 0;
1551         }
1552
1553         ret = qla_alloc_rcv_std(ha);
1554
1555         return (ret);
1556 }
1557
1558 static void
1559 qla_free_rcv_bufs(qla_host_t *ha)
1560 {
1561         int             i;
1562
1563         qla_free_rcv_std(ha);
1564
1565         if (ha->rx_tag != NULL) {
1566                 bus_dma_tag_destroy(ha->rx_tag);
1567                 ha->rx_tag = NULL;
1568         }
1569
1570         bzero((void *)ha->rx_ring, (sizeof(qla_rx_ring_t) * MAX_RDS_RINGS));
1571
1572         for (i = 0; i < ha->hw.num_sds_rings; i++) {
1573                 ha->hw.sds[i].sdsr_next = 0;
1574                 ha->hw.sds[i].rxb_free = NULL;
1575                 ha->hw.sds[i].rx_free = 0;
1576         }
1577
1578         return;
1579 }
1580
1581 int
1582 ql_get_mbuf(qla_host_t *ha, qla_rx_buf_t *rxb, struct mbuf *nmp)
1583 {
1584         register struct mbuf *mp = nmp;
1585         struct ifnet            *ifp;
1586         int                     ret = 0;
1587         uint32_t                offset;
1588         bus_dma_segment_t       segs[1];
1589         int                     nsegs, mbuf_size;
1590
1591         QL_DPRINT2(ha, (ha->pci_dev, "%s: enter\n", __func__));
1592
1593         ifp = ha->ifp;
1594
1595         if (ha->hw.enable_9kb)
1596                 mbuf_size = MJUM9BYTES;
1597         else
1598                 mbuf_size = MCLBYTES;
1599
1600         if (mp == NULL) {
1601
1602                 if (QL_ERR_INJECT(ha, INJCT_M_GETCL_M_GETJCL_FAILURE))
1603                         return(-1);
1604
1605                 if (ha->hw.enable_9kb)
1606                         mp = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, mbuf_size);
1607                 else
1608                         mp = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1609
1610                 if (mp == NULL) {
1611                         ha->err_m_getcl++;
1612                         ret = ENOBUFS;
1613                         device_printf(ha->pci_dev,
1614                                         "%s: m_getcl failed\n", __func__);
1615                         goto exit_ql_get_mbuf;
1616                 }
1617                 mp->m_len = mp->m_pkthdr.len = mbuf_size;
1618         } else {
1619                 mp->m_len = mp->m_pkthdr.len = mbuf_size;
1620                 mp->m_data = mp->m_ext.ext_buf;
1621                 mp->m_next = NULL;
1622         }
1623
1624         offset = (uint32_t)((unsigned long long)mp->m_data & 0x7ULL);
1625         if (offset) {
1626                 offset = 8 - offset;
1627                 m_adj(mp, offset);
1628         }
1629
1630         /*
1631          * Using memory from the mbuf cluster pool, invoke the bus_dma
1632          * machinery to arrange the memory mapping.
1633          */
1634         ret = bus_dmamap_load_mbuf_sg(ha->rx_tag, rxb->map,
1635                         mp, segs, &nsegs, BUS_DMA_NOWAIT);
1636         rxb->paddr = segs[0].ds_addr;
1637
1638         if (ret || !rxb->paddr || (nsegs != 1)) {
1639                 m_free(mp);
1640                 rxb->m_head = NULL;
1641                 device_printf(ha->pci_dev,
1642                         "%s: bus_dmamap_load failed[%d, 0x%016llx, %d]\n",
1643                         __func__, ret, (long long unsigned int)rxb->paddr,
1644                         nsegs);
1645                 ret = -1;
1646                 goto exit_ql_get_mbuf;
1647         }
1648         rxb->m_head = mp;
1649         bus_dmamap_sync(ha->rx_tag, rxb->map, BUS_DMASYNC_PREREAD);
1650
1651 exit_ql_get_mbuf:
1652         QL_DPRINT2(ha, (ha->pci_dev, "%s: exit ret = 0x%08x\n", __func__, ret));
1653         return (ret);
1654 }
1655
1656 static void
1657 qla_tx_done(void *context, int pending)
1658 {
1659         qla_host_t *ha = context;
1660         struct ifnet   *ifp;
1661
1662         ifp = ha->ifp;
1663
1664         if (!ifp) 
1665                 return;
1666
1667         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1668                 QL_DPRINT8(ha, (ha->pci_dev, "%s: !IFF_DRV_RUNNING\n", __func__));
1669                 return;
1670         }
1671         ql_hw_tx_done(ha);
1672
1673         qla_start(ha->ifp);
1674 }
1675
1676 static void
1677 qla_get_peer(qla_host_t *ha)
1678 {
1679         device_t *peers;
1680         int count, i, slot;
1681         int my_slot = pci_get_slot(ha->pci_dev);
1682
1683         if (device_get_children(device_get_parent(ha->pci_dev), &peers, &count))
1684                 return;
1685
1686         for (i = 0; i < count; i++) {
1687                 slot = pci_get_slot(peers[i]);
1688
1689                 if ((slot >= 0) && (slot == my_slot) &&
1690                         (pci_get_device(peers[i]) ==
1691                                 pci_get_device(ha->pci_dev))) {
1692                         if (ha->pci_dev != peers[i]) 
1693                                 ha->peer_dev = peers[i];
1694                 }
1695         }
1696 }
1697
1698 static void
1699 qla_send_msg_to_peer(qla_host_t *ha, uint32_t msg_to_peer)
1700 {
1701         qla_host_t *ha_peer;
1702         
1703         if (ha->peer_dev) {
1704                 if ((ha_peer = device_get_softc(ha->peer_dev)) != NULL) {
1705
1706                         ha_peer->msg_from_peer = msg_to_peer;
1707                 }
1708         }
1709 }
1710
1711 static void
1712 qla_error_recovery(void *context, int pending)
1713 {
1714         qla_host_t *ha = context;
1715         uint32_t msecs_100 = 100;
1716         struct ifnet *ifp = ha->ifp;
1717
1718         (void)QLA_LOCK(ha, __func__, 0);
1719
1720         if (ha->flags.qla_interface_up) {
1721
1722         ha->hw.imd_compl = 1;
1723         qla_mdelay(__func__, 300);
1724
1725         ql_hw_stop_rcv(ha);
1726
1727         ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE | IFF_DRV_RUNNING);
1728                 QLA_TX_LOCK(ha); QLA_TX_UNLOCK(ha);
1729         }
1730
1731         QLA_UNLOCK(ha, __func__);
1732
1733         if ((ha->pci_func & 0x1) == 0) {
1734
1735                 if (!ha->msg_from_peer) {
1736                         qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
1737
1738                         while ((ha->msg_from_peer != QL_PEER_MSG_ACK) &&
1739                                 msecs_100--)
1740                                 qla_mdelay(__func__, 100);
1741                 }
1742
1743                 ha->msg_from_peer = 0;
1744
1745                 (void)QLA_LOCK(ha, __func__, 0);
1746                 ql_minidump(ha);
1747                 QLA_UNLOCK(ha, __func__);
1748
1749                 (void) ql_init_hw(ha);
1750
1751                 (void)QLA_LOCK(ha, __func__, 0);
1752                 if (ha->flags.qla_interface_up) {
1753                 qla_free_xmt_bufs(ha);
1754                 qla_free_rcv_bufs(ha);
1755                 }
1756                 QLA_UNLOCK(ha, __func__);
1757
1758                 qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
1759
1760         } else {
1761                 if (ha->msg_from_peer == QL_PEER_MSG_RESET) {
1762
1763                         ha->msg_from_peer = 0;
1764
1765                         qla_send_msg_to_peer(ha, QL_PEER_MSG_ACK);
1766                 } else {
1767                         qla_send_msg_to_peer(ha, QL_PEER_MSG_RESET);
1768                 }
1769
1770                 while ((ha->msg_from_peer != QL_PEER_MSG_ACK)  && msecs_100--)
1771                         qla_mdelay(__func__, 100);
1772                 ha->msg_from_peer = 0;
1773
1774                 (void) ql_init_hw(ha);
1775
1776                 (void)QLA_LOCK(ha, __func__, 0);
1777                 if (ha->flags.qla_interface_up) {
1778                 qla_free_xmt_bufs(ha);
1779                 qla_free_rcv_bufs(ha);
1780         }
1781                 QLA_UNLOCK(ha, __func__);
1782         }
1783
1784         (void)QLA_LOCK(ha, __func__, 0);
1785
1786         if (ha->flags.qla_interface_up) {
1787         if (qla_alloc_xmt_bufs(ha) != 0) {
1788                 QLA_UNLOCK(ha, __func__);
1789                 return;
1790         }
1791         qla_confirm_9kb_enable(ha);
1792
1793         if (qla_alloc_rcv_bufs(ha) != 0) {
1794                 QLA_UNLOCK(ha, __func__);
1795                 return;
1796         }
1797
1798         ha->flags.stop_rcv = 0;
1799         if (ql_init_hw_if(ha) == 0) {
1800                 ifp = ha->ifp;
1801                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1802                 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1803                 ha->flags.qla_watchdog_pause = 0;
1804         }
1805         } else
1806                 ha->flags.qla_watchdog_pause = 0;
1807
1808         QLA_UNLOCK(ha, __func__);
1809 }
1810
1811 static void
1812 qla_async_event(void *context, int pending)
1813 {
1814         qla_host_t *ha = context;
1815
1816         (void)QLA_LOCK(ha, __func__, 0);
1817         qla_hw_async_event(ha);
1818         QLA_UNLOCK(ha, __func__);
1819 }
1820