xref: /freebsd/sys/dev/qlxge/qls_os.c (revision 690b7ea081790eef2c890f63a4fe7e195cf51df0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013-2014 Qlogic Corporation
5  * All rights reserved.
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions
9  *  are met:
10  *
11  *  1. Redistributions of source code must retain the above copyright
12  *     notice, this list of conditions and the following disclaimer.
13  *  2. Redistributions in binary form must reproduce the above copyright
14  *     notice, this list of conditions and the following disclaimer in the
15  *     documentation and/or other materials provided with the distribution.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  *  and ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  *  POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * File: qls_os.c
32  * Author : David C Somayajulu, Qlogic Corporation, Aliso Viejo, CA 92656.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include "qls_os.h"
39 #include "qls_hw.h"
40 #include "qls_def.h"
41 #include "qls_inline.h"
42 #include "qls_ver.h"
43 #include "qls_glbl.h"
44 #include "qls_dbg.h"
45 #include <sys/smp.h>
46 
47 /*
48  * Some PCI Configuration Space Related Defines
49  */
50 
51 #ifndef PCI_VENDOR_QLOGIC
52 #define PCI_VENDOR_QLOGIC	0x1077
53 #endif
54 
55 #ifndef PCI_DEVICE_QLOGIC_8000
56 #define PCI_DEVICE_QLOGIC_8000	0x8000
57 #endif
58 
59 #define PCI_QLOGIC_DEV8000 \
60 	((PCI_DEVICE_QLOGIC_8000 << 16) | PCI_VENDOR_QLOGIC)
61 
62 /*
63  * static functions
64  */
65 static int qls_alloc_parent_dma_tag(qla_host_t *ha);
66 static void qls_free_parent_dma_tag(qla_host_t *ha);
67 
68 static void qls_flush_xmt_bufs(qla_host_t *ha);
69 
70 static int qls_alloc_rcv_bufs(qla_host_t *ha);
71 static void qls_free_rcv_bufs(qla_host_t *ha);
72 
73 static void qls_init_ifnet(device_t dev, qla_host_t *ha);
74 static void qls_release(qla_host_t *ha);
75 static void qls_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs,
76 		int error);
77 static void qls_stop(qla_host_t *ha);
78 static int qls_send(qla_host_t *ha, struct mbuf **m_headp);
79 static void qls_tx_done(void *context, int pending);
80 
81 static int qls_config_lro(qla_host_t *ha);
82 static void qls_free_lro(qla_host_t *ha);
83 
84 static void qls_error_recovery(void *context, int pending);
85 
86 /*
87  * Hooks to the Operating Systems
88  */
89 static int qls_pci_probe (device_t);
90 static int qls_pci_attach (device_t);
91 static int qls_pci_detach (device_t);
92 
93 static void qls_start(struct ifnet *ifp);
94 static void qls_init(void *arg);
95 static int qls_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
96 static int qls_media_change(struct ifnet *ifp);
97 static void qls_media_status(struct ifnet *ifp, struct ifmediareq *ifmr);
98 
99 static device_method_t qla_pci_methods[] = {
100 	/* Device interface */
101 	DEVMETHOD(device_probe, qls_pci_probe),
102 	DEVMETHOD(device_attach, qls_pci_attach),
103 	DEVMETHOD(device_detach, qls_pci_detach),
104 	{ 0, 0 }
105 };
106 
107 static driver_t qla_pci_driver = {
108 	"ql", qla_pci_methods, sizeof (qla_host_t),
109 };
110 
111 static devclass_t qla8000_devclass;
112 
113 DRIVER_MODULE(qla8000, pci, qla_pci_driver, qla8000_devclass, 0, 0);
114 
115 MODULE_DEPEND(qla8000, pci, 1, 1, 1);
116 MODULE_DEPEND(qla8000, ether, 1, 1, 1);
117 
118 MALLOC_DEFINE(M_QLA8000BUF, "qla8000buf", "Buffers for qla8000 driver");
119 
120 static char dev_str[64];
121 static char ver_str[64];
122 
123 /*
124  * Name:	qls_pci_probe
125  * Function:	Validate the PCI device to be a QLA80XX device
126  */
127 static int
128 qls_pci_probe(device_t dev)
129 {
130         switch ((pci_get_device(dev) << 16) | (pci_get_vendor(dev))) {
131         case PCI_QLOGIC_DEV8000:
132 		snprintf(dev_str, sizeof(dev_str), "%s v%d.%d.%d",
133 			"Qlogic ISP 8000 PCI CNA Adapter-Ethernet Function",
134 			QLA_VERSION_MAJOR, QLA_VERSION_MINOR,
135 			QLA_VERSION_BUILD);
136 		snprintf(ver_str, sizeof(ver_str), "v%d.%d.%d",
137 			QLA_VERSION_MAJOR, QLA_VERSION_MINOR,
138 			QLA_VERSION_BUILD);
139                 device_set_desc(dev, dev_str);
140                 break;
141         default:
142                 return (ENXIO);
143         }
144 
145         if (bootverbose)
146                 printf("%s: %s\n ", __func__, dev_str);
147 
148         return (BUS_PROBE_DEFAULT);
149 }
150 
151 static int
152 qls_sysctl_get_drvr_stats(SYSCTL_HANDLER_ARGS)
153 {
154         int err = 0, ret;
155         qla_host_t *ha;
156         uint32_t i;
157 
158         err = sysctl_handle_int(oidp, &ret, 0, req);
159 
160         if (err || !req->newptr)
161                 return (err);
162 
163         if (ret == 1) {
164                 ha = (qla_host_t *)arg1;
165 
166                 for (i = 0; i < ha->num_tx_rings; i++) {
167                         device_printf(ha->pci_dev,
168                                 "%s: tx_ring[%d].tx_frames= %p\n",
169 				__func__, i,
170                                 (void *)ha->tx_ring[i].tx_frames);
171 
172                         device_printf(ha->pci_dev,
173                                 "%s: tx_ring[%d].tx_tso_frames= %p\n",
174 				__func__, i,
175                                 (void *)ha->tx_ring[i].tx_tso_frames);
176 
177                         device_printf(ha->pci_dev,
178                                 "%s: tx_ring[%d].tx_vlan_frames= %p\n",
179 				__func__, i,
180                                 (void *)ha->tx_ring[i].tx_vlan_frames);
181 
182                         device_printf(ha->pci_dev,
183                                 "%s: tx_ring[%d].txr_free= 0x%08x\n",
184 				__func__, i,
185                                 ha->tx_ring[i].txr_free);
186 
187                         device_printf(ha->pci_dev,
188                                 "%s: tx_ring[%d].txr_next= 0x%08x\n",
189 				__func__, i,
190                                 ha->tx_ring[i].txr_next);
191 
192                         device_printf(ha->pci_dev,
193                                 "%s: tx_ring[%d].txr_done= 0x%08x\n",
194 				__func__, i,
195                                 ha->tx_ring[i].txr_done);
196 
197                         device_printf(ha->pci_dev,
198                                 "%s: tx_ring[%d].txr_cons_idx= 0x%08x\n",
199 				__func__, i,
200                                 *(ha->tx_ring[i].txr_cons_vaddr));
201 		}
202 
203                 for (i = 0; i < ha->num_rx_rings; i++) {
204                         device_printf(ha->pci_dev,
205                                 "%s: rx_ring[%d].rx_int= %p\n",
206 				__func__, i,
207                                 (void *)ha->rx_ring[i].rx_int);
208 
209                         device_printf(ha->pci_dev,
210                                 "%s: rx_ring[%d].rss_int= %p\n",
211 				__func__, i,
212                                 (void *)ha->rx_ring[i].rss_int);
213 
214                         device_printf(ha->pci_dev,
215                                 "%s: rx_ring[%d].lbq_next= 0x%08x\n",
216 				__func__, i,
217                                 ha->rx_ring[i].lbq_next);
218 
219                         device_printf(ha->pci_dev,
220                                 "%s: rx_ring[%d].lbq_free= 0x%08x\n",
221 				__func__, i,
222                                 ha->rx_ring[i].lbq_free);
223 
224                         device_printf(ha->pci_dev,
225                                 "%s: rx_ring[%d].lbq_in= 0x%08x\n",
226 				__func__, i,
227                                 ha->rx_ring[i].lbq_in);
228 
229                         device_printf(ha->pci_dev,
230                                 "%s: rx_ring[%d].sbq_next= 0x%08x\n",
231 				__func__, i,
232                                 ha->rx_ring[i].sbq_next);
233 
234                         device_printf(ha->pci_dev,
235                                 "%s: rx_ring[%d].sbq_free= 0x%08x\n",
236 				__func__, i,
237                                 ha->rx_ring[i].sbq_free);
238 
239                         device_printf(ha->pci_dev,
240                                 "%s: rx_ring[%d].sbq_in= 0x%08x\n",
241 				__func__, i,
242                                 ha->rx_ring[i].sbq_in);
243 		}
244 
245 		device_printf(ha->pci_dev, "%s: err_m_getcl = 0x%08x\n",
246 				__func__, ha->err_m_getcl);
247 		device_printf(ha->pci_dev, "%s: err_m_getjcl = 0x%08x\n",
248 				__func__, ha->err_m_getjcl);
249 		device_printf(ha->pci_dev,
250 				"%s: err_tx_dmamap_create = 0x%08x\n",
251 				__func__, ha->err_tx_dmamap_create);
252 		device_printf(ha->pci_dev,
253 				"%s: err_tx_dmamap_load = 0x%08x\n",
254 				__func__, ha->err_tx_dmamap_load);
255 		device_printf(ha->pci_dev,
256 				"%s: err_tx_defrag = 0x%08x\n",
257 				__func__, ha->err_tx_defrag);
258         }
259         return (err);
260 }
261 
262 static void
263 qls_add_sysctls(qla_host_t *ha)
264 {
265         device_t dev = ha->pci_dev;
266 
267 	SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
268 		SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
269 		OID_AUTO, "version", CTLFLAG_RD,
270 		ver_str, 0, "Driver Version");
271 
272 	qls_dbg_level = 0;
273         SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
274                 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
275                 OID_AUTO, "debug", CTLFLAG_RW,
276                 &qls_dbg_level, qls_dbg_level, "Debug Level");
277 
278         SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
279             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
280             OID_AUTO, "drvr_stats",
281 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, (void *)ha, 0,
282 	    qls_sysctl_get_drvr_stats, "I", "Driver Maintained Statistics");
283 
284         return;
285 }
286 
287 static void
288 qls_watchdog(void *arg)
289 {
290 	qla_host_t *ha = arg;
291 	struct ifnet *ifp;
292 
293 	ifp = ha->ifp;
294 
295         if (ha->flags.qla_watchdog_exit) {
296 		ha->qla_watchdog_exited = 1;
297 		return;
298 	}
299 	ha->qla_watchdog_exited = 0;
300 
301 	if (!ha->flags.qla_watchdog_pause) {
302 		if (ha->qla_initiate_recovery) {
303 			ha->qla_watchdog_paused = 1;
304 			ha->qla_initiate_recovery = 0;
305 			ha->err_inject = 0;
306 			taskqueue_enqueue(ha->err_tq, &ha->err_task);
307 
308 		} else if ((ifp->if_snd.ifq_head != NULL) && QL_RUNNING(ifp)) {
309 			taskqueue_enqueue(ha->tx_tq, &ha->tx_task);
310 		}
311 
312 		ha->qla_watchdog_paused = 0;
313 	} else {
314 		ha->qla_watchdog_paused = 1;
315 	}
316 
317 	ha->watchdog_ticks = (ha->watchdog_ticks + 1) % 1000;
318 	callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
319 		qls_watchdog, ha);
320 
321 	return;
322 }
323 
324 /*
325  * Name:	qls_pci_attach
326  * Function:	attaches the device to the operating system
327  */
328 static int
329 qls_pci_attach(device_t dev)
330 {
331 	qla_host_t *ha = NULL;
332 	int i;
333 
334 	QL_DPRINT2((dev, "%s: enter\n", __func__));
335 
336         if ((ha = device_get_softc(dev)) == NULL) {
337                 device_printf(dev, "cannot get softc\n");
338                 return (ENOMEM);
339         }
340 
341         memset(ha, 0, sizeof (qla_host_t));
342 
343         if (pci_get_device(dev) != PCI_DEVICE_QLOGIC_8000) {
344                 device_printf(dev, "device is not QLE8000\n");
345                 return (ENXIO);
346 	}
347 
348         ha->pci_func = pci_get_function(dev);
349 
350         ha->pci_dev = dev;
351 
352 	pci_enable_busmaster(dev);
353 
354 	ha->reg_rid = PCIR_BAR(1);
355 	ha->pci_reg = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &ha->reg_rid,
356 				RF_ACTIVE);
357 
358         if (ha->pci_reg == NULL) {
359                 device_printf(dev, "unable to map any ports\n");
360                 goto qls_pci_attach_err;
361         }
362 
363 	ha->reg_rid1 = PCIR_BAR(3);
364 	ha->pci_reg1 = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
365 			&ha->reg_rid1, RF_ACTIVE);
366 
367         if (ha->pci_reg1 == NULL) {
368                 device_printf(dev, "unable to map any ports\n");
369                 goto qls_pci_attach_err;
370         }
371 
372 	mtx_init(&ha->hw_lock, "qla80xx_hw_lock", MTX_NETWORK_LOCK, MTX_DEF);
373 	mtx_init(&ha->tx_lock, "qla80xx_tx_lock", MTX_NETWORK_LOCK, MTX_DEF);
374 
375 	qls_add_sysctls(ha);
376 	qls_hw_add_sysctls(ha);
377 
378 	ha->flags.lock_init = 1;
379 
380 	ha->msix_count = pci_msix_count(dev);
381 
382 	if (ha->msix_count < qls_get_msix_count(ha)) {
383 		device_printf(dev, "%s: msix_count[%d] not enough\n", __func__,
384 			ha->msix_count);
385 		goto qls_pci_attach_err;
386 	}
387 
388 	ha->msix_count = qls_get_msix_count(ha);
389 
390 	device_printf(dev, "\n%s: ha %p pci_func 0x%x  msix_count 0x%x"
391 		" pci_reg %p pci_reg1 %p\n", __func__, ha,
392 		ha->pci_func, ha->msix_count, ha->pci_reg, ha->pci_reg1);
393 
394 	if (pci_alloc_msix(dev, &ha->msix_count)) {
395 		device_printf(dev, "%s: pci_alloc_msi[%d] failed\n", __func__,
396 			ha->msix_count);
397 		ha->msix_count = 0;
398 		goto qls_pci_attach_err;
399 	}
400 
401         for (i = 0; i < ha->num_rx_rings; i++) {
402                 ha->irq_vec[i].cq_idx = i;
403                 ha->irq_vec[i].ha = ha;
404                 ha->irq_vec[i].irq_rid = 1 + i;
405 
406                 ha->irq_vec[i].irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
407                                 &ha->irq_vec[i].irq_rid,
408                                 (RF_ACTIVE | RF_SHAREABLE));
409 
410                 if (ha->irq_vec[i].irq == NULL) {
411                         device_printf(dev, "could not allocate interrupt\n");
412                         goto qls_pci_attach_err;
413                 }
414 
415 		if (bus_setup_intr(dev, ha->irq_vec[i].irq,
416 			(INTR_TYPE_NET | INTR_MPSAFE), NULL, qls_isr,
417 			&ha->irq_vec[i], &ha->irq_vec[i].handle)) {
418 				device_printf(dev,
419 					"could not setup interrupt\n");
420 			goto qls_pci_attach_err;
421 		}
422         }
423 
424 	qls_rd_nic_params(ha);
425 
426 	/* allocate parent dma tag */
427 	if (qls_alloc_parent_dma_tag(ha)) {
428 		device_printf(dev, "%s: qls_alloc_parent_dma_tag failed\n",
429 			__func__);
430 		goto qls_pci_attach_err;
431 	}
432 
433 	/* alloc all dma buffers */
434 	if (qls_alloc_dma(ha)) {
435 		device_printf(dev, "%s: qls_alloc_dma failed\n", __func__);
436 		goto qls_pci_attach_err;
437 	}
438 
439 	/* create the o.s ethernet interface */
440 	qls_init_ifnet(dev, ha);
441 
442 	ha->flags.qla_watchdog_active = 1;
443 	ha->flags.qla_watchdog_pause = 1;
444 
445 	TASK_INIT(&ha->tx_task, 0, qls_tx_done, ha);
446 	ha->tx_tq = taskqueue_create_fast("qla_txq", M_NOWAIT,
447 			taskqueue_thread_enqueue, &ha->tx_tq);
448 	taskqueue_start_threads(&ha->tx_tq, 1, PI_NET, "%s txq",
449 		device_get_nameunit(ha->pci_dev));
450 
451 	callout_init(&ha->tx_callout, 1);
452 	ha->flags.qla_callout_init = 1;
453 
454         /* create ioctl device interface */
455         if (qls_make_cdev(ha)) {
456                 device_printf(dev, "%s: qls_make_cdev failed\n", __func__);
457                 goto qls_pci_attach_err;
458         }
459 
460 	callout_reset(&ha->tx_callout, QLA_WATCHDOG_CALLOUT_TICKS,
461 		qls_watchdog, ha);
462 
463         TASK_INIT(&ha->err_task, 0, qls_error_recovery, ha);
464         ha->err_tq = taskqueue_create_fast("qla_errq", M_NOWAIT,
465                         taskqueue_thread_enqueue, &ha->err_tq);
466         taskqueue_start_threads(&ha->err_tq, 1, PI_NET, "%s errq",
467                 device_get_nameunit(ha->pci_dev));
468 
469 	QL_DPRINT2((dev, "%s: exit 0\n", __func__));
470         return (0);
471 
472 qls_pci_attach_err:
473 
474 	qls_release(ha);
475 
476 	QL_DPRINT2((dev, "%s: exit ENXIO\n", __func__));
477         return (ENXIO);
478 }
479 
480 /*
481  * Name:	qls_pci_detach
482  * Function:	Unhooks the device from the operating system
483  */
484 static int
485 qls_pci_detach(device_t dev)
486 {
487 	qla_host_t *ha = NULL;
488 
489 	QL_DPRINT2((dev, "%s: enter\n", __func__));
490 
491         if ((ha = device_get_softc(dev)) == NULL) {
492                 device_printf(dev, "cannot get softc\n");
493                 return (ENOMEM);
494         }
495 
496 	(void)QLA_LOCK(ha, __func__, 0);
497 	qls_stop(ha);
498 	QLA_UNLOCK(ha, __func__);
499 
500 	qls_release(ha);
501 
502 	QL_DPRINT2((dev, "%s: exit\n", __func__));
503 
504         return (0);
505 }
506 
507 /*
508  * Name:	qls_release
509  * Function:	Releases the resources allocated for the device
510  */
511 static void
512 qls_release(qla_host_t *ha)
513 {
514 	device_t dev;
515 	int i;
516 
517 	dev = ha->pci_dev;
518 
519 	if (ha->err_tq) {
520 		taskqueue_drain(ha->err_tq, &ha->err_task);
521 		taskqueue_free(ha->err_tq);
522 	}
523 
524 	if (ha->tx_tq) {
525 		taskqueue_drain(ha->tx_tq, &ha->tx_task);
526 		taskqueue_free(ha->tx_tq);
527 	}
528 
529 	qls_del_cdev(ha);
530 
531 	if (ha->flags.qla_watchdog_active) {
532 		ha->flags.qla_watchdog_exit = 1;
533 
534 		while (ha->qla_watchdog_exited == 0)
535 			qls_mdelay(__func__, 1);
536 	}
537 
538 	if (ha->flags.qla_callout_init)
539 		callout_stop(&ha->tx_callout);
540 
541 	if (ha->ifp != NULL)
542 		ether_ifdetach(ha->ifp);
543 
544 	qls_free_dma(ha);
545 	qls_free_parent_dma_tag(ha);
546 
547         for (i = 0; i < ha->num_rx_rings; i++) {
548                 if (ha->irq_vec[i].handle) {
549                         (void)bus_teardown_intr(dev, ha->irq_vec[i].irq,
550                                         ha->irq_vec[i].handle);
551                 }
552 
553                 if (ha->irq_vec[i].irq) {
554                         (void)bus_release_resource(dev, SYS_RES_IRQ,
555                                 ha->irq_vec[i].irq_rid,
556                                 ha->irq_vec[i].irq);
557                 }
558         }
559 
560 	if (ha->msix_count)
561 		pci_release_msi(dev);
562 
563 	if (ha->flags.lock_init) {
564 		mtx_destroy(&ha->tx_lock);
565 		mtx_destroy(&ha->hw_lock);
566 	}
567 
568         if (ha->pci_reg)
569                 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid,
570 				ha->pci_reg);
571 
572         if (ha->pci_reg1)
573                 (void) bus_release_resource(dev, SYS_RES_MEMORY, ha->reg_rid1,
574 				ha->pci_reg1);
575 }
576 
577 /*
578  * DMA Related Functions
579  */
580 
581 static void
582 qls_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
583 {
584         *((bus_addr_t *)arg) = 0;
585 
586         if (error) {
587                 printf("%s: bus_dmamap_load failed (%d)\n", __func__, error);
588                 return;
589 	}
590 
591         *((bus_addr_t *)arg) = segs[0].ds_addr;
592 
593 	return;
594 }
595 
596 int
597 qls_alloc_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
598 {
599         int             ret = 0;
600         device_t        dev;
601         bus_addr_t      b_addr;
602 
603         dev = ha->pci_dev;
604 
605         QL_DPRINT2((dev, "%s: enter\n", __func__));
606 
607         ret = bus_dma_tag_create(
608                         ha->parent_tag,/* parent */
609                         dma_buf->alignment,
610                         ((bus_size_t)(1ULL << 32)),/* boundary */
611                         BUS_SPACE_MAXADDR,      /* lowaddr */
612                         BUS_SPACE_MAXADDR,      /* highaddr */
613                         NULL, NULL,             /* filter, filterarg */
614                         dma_buf->size,          /* maxsize */
615                         1,                      /* nsegments */
616                         dma_buf->size,          /* maxsegsize */
617                         0,                      /* flags */
618                         NULL, NULL,             /* lockfunc, lockarg */
619                         &dma_buf->dma_tag);
620 
621         if (ret) {
622                 device_printf(dev, "%s: could not create dma tag\n", __func__);
623                 goto qls_alloc_dmabuf_exit;
624         }
625         ret = bus_dmamem_alloc(dma_buf->dma_tag,
626                         (void **)&dma_buf->dma_b,
627                         (BUS_DMA_ZERO | BUS_DMA_COHERENT | BUS_DMA_NOWAIT),
628                         &dma_buf->dma_map);
629         if (ret) {
630                 bus_dma_tag_destroy(dma_buf->dma_tag);
631                 device_printf(dev, "%s: bus_dmamem_alloc failed\n", __func__);
632                 goto qls_alloc_dmabuf_exit;
633         }
634 
635         ret = bus_dmamap_load(dma_buf->dma_tag,
636                         dma_buf->dma_map,
637                         dma_buf->dma_b,
638                         dma_buf->size,
639                         qls_dmamap_callback,
640                         &b_addr, BUS_DMA_NOWAIT);
641 
642         if (ret || !b_addr) {
643                 bus_dma_tag_destroy(dma_buf->dma_tag);
644                 bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b,
645                         dma_buf->dma_map);
646                 ret = -1;
647                 goto qls_alloc_dmabuf_exit;
648         }
649 
650         dma_buf->dma_addr = b_addr;
651 
652 qls_alloc_dmabuf_exit:
653         QL_DPRINT2((dev, "%s: exit ret 0x%08x tag %p map %p b %p sz 0x%x\n",
654                 __func__, ret, (void *)dma_buf->dma_tag,
655                 (void *)dma_buf->dma_map, (void *)dma_buf->dma_b,
656 		dma_buf->size));
657 
658         return ret;
659 }
660 
661 void
662 qls_free_dmabuf(qla_host_t *ha, qla_dma_t *dma_buf)
663 {
664         bus_dmamap_unload(dma_buf->dma_tag, dma_buf->dma_map);
665         bus_dmamem_free(dma_buf->dma_tag, dma_buf->dma_b, dma_buf->dma_map);
666         bus_dma_tag_destroy(dma_buf->dma_tag);
667 }
668 
669 static int
670 qls_alloc_parent_dma_tag(qla_host_t *ha)
671 {
672 	int		ret;
673 	device_t	dev;
674 
675 	dev = ha->pci_dev;
676 
677         /*
678          * Allocate parent DMA Tag
679          */
680         ret = bus_dma_tag_create(
681                         bus_get_dma_tag(dev),   /* parent */
682                         1,((bus_size_t)(1ULL << 32)),/* alignment, boundary */
683                         BUS_SPACE_MAXADDR,      /* lowaddr */
684                         BUS_SPACE_MAXADDR,      /* highaddr */
685                         NULL, NULL,             /* filter, filterarg */
686                         BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
687                         0,                      /* nsegments */
688                         BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
689                         0,                      /* flags */
690                         NULL, NULL,             /* lockfunc, lockarg */
691                         &ha->parent_tag);
692 
693         if (ret) {
694                 device_printf(dev, "%s: could not create parent dma tag\n",
695                         __func__);
696 		return (-1);
697         }
698 
699         ha->flags.parent_tag = 1;
700 
701 	return (0);
702 }
703 
704 static void
705 qls_free_parent_dma_tag(qla_host_t *ha)
706 {
707         if (ha->flags.parent_tag) {
708                 bus_dma_tag_destroy(ha->parent_tag);
709                 ha->flags.parent_tag = 0;
710         }
711 }
712 
713 /*
714  * Name: qls_init_ifnet
715  * Function: Creates the Network Device Interface and Registers it with the O.S
716  */
717 
718 static void
719 qls_init_ifnet(device_t dev, qla_host_t *ha)
720 {
721 	struct ifnet *ifp;
722 
723 	QL_DPRINT2((dev, "%s: enter\n", __func__));
724 
725 	ifp = ha->ifp = if_alloc(IFT_ETHER);
726 
727 	if (ifp == NULL)
728 		panic("%s: cannot if_alloc()\n", device_get_nameunit(dev));
729 
730 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
731 	ifp->if_baudrate = IF_Gbps(10);
732 	ifp->if_init = qls_init;
733 	ifp->if_softc = ha;
734 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
735 	ifp->if_ioctl = qls_ioctl;
736 	ifp->if_start = qls_start;
737 
738 	IFQ_SET_MAXLEN(&ifp->if_snd, qls_get_ifq_snd_maxlen(ha));
739 	ifp->if_snd.ifq_drv_maxlen = qls_get_ifq_snd_maxlen(ha);
740 	IFQ_SET_READY(&ifp->if_snd);
741 
742 	ha->max_frame_size = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
743 	if (ha->max_frame_size <= MCLBYTES) {
744 		ha->msize = MCLBYTES;
745 	} else if (ha->max_frame_size <= MJUMPAGESIZE) {
746 		ha->msize = MJUMPAGESIZE;
747 	} else
748 		ha->msize = MJUM9BYTES;
749 
750 	ether_ifattach(ifp, qls_get_mac_addr(ha));
751 
752 	ifp->if_capabilities = IFCAP_JUMBO_MTU;
753 
754 	ifp->if_capabilities |= IFCAP_HWCSUM;
755 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
756 
757 	ifp->if_capabilities |= IFCAP_TSO4;
758 	ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING;
759 	ifp->if_capabilities |= IFCAP_VLAN_HWTSO;
760 	ifp->if_capabilities |= IFCAP_LINKSTATE;
761 
762 	ifp->if_capenable = ifp->if_capabilities;
763 
764 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
765 
766 	ifmedia_init(&ha->media, IFM_IMASK, qls_media_change, qls_media_status);
767 
768 	ifmedia_add(&ha->media, (IFM_ETHER | qls_get_optics(ha) | IFM_FDX), 0,
769 		NULL);
770 	ifmedia_add(&ha->media, (IFM_ETHER | IFM_AUTO), 0, NULL);
771 
772 	ifmedia_set(&ha->media, (IFM_ETHER | IFM_AUTO));
773 
774 	QL_DPRINT2((dev, "%s: exit\n", __func__));
775 
776 	return;
777 }
778 
779 static void
780 qls_init_locked(qla_host_t *ha)
781 {
782 	struct ifnet *ifp = ha->ifp;
783 
784 	qls_stop(ha);
785 
786 	qls_flush_xmt_bufs(ha);
787 
788 	if (qls_alloc_rcv_bufs(ha) != 0)
789 		return;
790 
791 	if (qls_config_lro(ha))
792 		return;
793 
794 	bcopy(IF_LLADDR(ha->ifp), ha->mac_addr, ETHER_ADDR_LEN);
795 
796 	ifp->if_hwassist = CSUM_IP;
797 	ifp->if_hwassist |= CSUM_TCP;
798 	ifp->if_hwassist |= CSUM_UDP;
799 	ifp->if_hwassist |= CSUM_TSO;
800 
801  	if (qls_init_hw_if(ha) == 0) {
802 		ifp = ha->ifp;
803 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
804 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
805 		ha->flags.qla_watchdog_pause = 0;
806 	}
807 
808 	return;
809 }
810 
811 static void
812 qls_init(void *arg)
813 {
814 	qla_host_t *ha;
815 
816 	ha = (qla_host_t *)arg;
817 
818 	QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
819 
820 	(void)QLA_LOCK(ha, __func__, 0);
821 	qls_init_locked(ha);
822 	QLA_UNLOCK(ha, __func__);
823 
824 	QL_DPRINT2((ha->pci_dev, "%s: exit\n", __func__));
825 }
826 
827 static u_int
828 qls_copy_maddr(void *arg, struct sockaddr_dl *sdl, u_int mcnt)
829 {
830 	uint8_t *mta = arg;
831 
832 	if (mcnt == Q8_MAX_NUM_MULTICAST_ADDRS)
833 		return (0);
834 
835 	bcopy(LLADDR(sdl), &mta[mcnt * Q8_MAC_ADDR_LEN], Q8_MAC_ADDR_LEN);
836 
837 	return (1);
838 }
839 
840 static void
841 qls_set_multi(qla_host_t *ha, uint32_t add_multi)
842 {
843 	uint8_t mta[Q8_MAX_NUM_MULTICAST_ADDRS * Q8_MAC_ADDR_LEN];
844 	struct ifnet *ifp = ha->ifp;
845 	int mcnt;
846 
847 	mcnt = if_foreach_llmaddr(ifp, qls_copy_maddr, mta);
848 
849 	if (QLA_LOCK(ha, __func__, 1) == 0) {
850 		qls_hw_set_multi(ha, mta, mcnt, add_multi);
851 		QLA_UNLOCK(ha, __func__);
852 	}
853 
854 	return;
855 }
856 
857 static int
858 qls_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
859 {
860 	int ret = 0;
861 	struct ifreq *ifr = (struct ifreq *)data;
862 #ifdef INET
863 	struct ifaddr *ifa = (struct ifaddr *)data;
864 #endif
865 	qla_host_t *ha;
866 
867 	ha = (qla_host_t *)ifp->if_softc;
868 
869 	switch (cmd) {
870 	case SIOCSIFADDR:
871 		QL_DPRINT4((ha->pci_dev, "%s: SIOCSIFADDR (0x%lx)\n",
872 			__func__, cmd));
873 
874 #ifdef INET
875 		if (ifa->ifa_addr->sa_family == AF_INET) {
876 			ifp->if_flags |= IFF_UP;
877 			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
878 				(void)QLA_LOCK(ha, __func__, 0);
879 				qls_init_locked(ha);
880 				QLA_UNLOCK(ha, __func__);
881 			}
882 			QL_DPRINT4((ha->pci_dev,
883 				"%s: SIOCSIFADDR (0x%lx) ipv4 [0x%08x]\n",
884 				__func__, cmd,
885 				ntohl(IA_SIN(ifa)->sin_addr.s_addr)));
886 
887 			arp_ifinit(ifp, ifa);
888 			break;
889 		}
890 #endif
891 		ether_ioctl(ifp, cmd, data);
892 		break;
893 
894 	case SIOCSIFMTU:
895 		QL_DPRINT4((ha->pci_dev, "%s: SIOCSIFMTU (0x%lx)\n",
896 			__func__, cmd));
897 
898 		if (ifr->ifr_mtu > QLA_MAX_MTU) {
899 			ret = EINVAL;
900 		} else {
901 			(void) QLA_LOCK(ha, __func__, 0);
902 
903 			ifp->if_mtu = ifr->ifr_mtu;
904 			ha->max_frame_size =
905 				ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
906 
907 			QLA_UNLOCK(ha, __func__);
908 
909 			if (ret)
910 				ret = EINVAL;
911 		}
912 
913 		break;
914 
915 	case SIOCSIFFLAGS:
916 		QL_DPRINT4((ha->pci_dev, "%s: SIOCSIFFLAGS (0x%lx)\n",
917 			__func__, cmd));
918 
919 		(void)QLA_LOCK(ha, __func__, 0);
920 
921 		if (ifp->if_flags & IFF_UP) {
922 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING)) {
923 				if ((ifp->if_flags ^ ha->if_flags) &
924 					IFF_PROMISC) {
925 					ret = qls_set_promisc(ha);
926 				} else if ((ifp->if_flags ^ ha->if_flags) &
927 					IFF_ALLMULTI) {
928 					ret = qls_set_allmulti(ha);
929 				}
930 			} else {
931 				ha->max_frame_size = ifp->if_mtu +
932 					ETHER_HDR_LEN + ETHER_CRC_LEN;
933 				qls_init_locked(ha);
934 			}
935 		} else {
936 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
937 				qls_stop(ha);
938 			ha->if_flags = ifp->if_flags;
939 		}
940 
941 		QLA_UNLOCK(ha, __func__);
942 		break;
943 
944 	case SIOCADDMULTI:
945 		QL_DPRINT4((ha->pci_dev,
946 			"%s: %s (0x%lx)\n", __func__, "SIOCADDMULTI", cmd));
947 
948 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
949 			qls_set_multi(ha, 1);
950 		}
951 		break;
952 
953 	case SIOCDELMULTI:
954 		QL_DPRINT4((ha->pci_dev,
955 			"%s: %s (0x%lx)\n", __func__, "SIOCDELMULTI", cmd));
956 
957 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
958 			qls_set_multi(ha, 0);
959 		}
960 		break;
961 
962 	case SIOCSIFMEDIA:
963 	case SIOCGIFMEDIA:
964 		QL_DPRINT4((ha->pci_dev,
965 			"%s: SIOCSIFMEDIA/SIOCGIFMEDIA (0x%lx)\n",
966 			__func__, cmd));
967 		ret = ifmedia_ioctl(ifp, ifr, &ha->media, cmd);
968 		break;
969 
970 	case SIOCSIFCAP:
971 	{
972 		int mask = ifr->ifr_reqcap ^ ifp->if_capenable;
973 
974 		QL_DPRINT4((ha->pci_dev, "%s: SIOCSIFCAP (0x%lx)\n",
975 			__func__, cmd));
976 
977 		if (mask & IFCAP_HWCSUM)
978 			ifp->if_capenable ^= IFCAP_HWCSUM;
979 		if (mask & IFCAP_TSO4)
980 			ifp->if_capenable ^= IFCAP_TSO4;
981 		if (mask & IFCAP_VLAN_HWTAGGING)
982 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
983 		if (mask & IFCAP_VLAN_HWTSO)
984 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
985 
986 		if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
987 			qls_init(ha);
988 
989 		VLAN_CAPABILITIES(ifp);
990 		break;
991 	}
992 
993 	default:
994 		QL_DPRINT4((ha->pci_dev, "%s: default (0x%lx)\n",
995 			__func__, cmd));
996 		ret = ether_ioctl(ifp, cmd, data);
997 		break;
998 	}
999 
1000 	return (ret);
1001 }
1002 
1003 static int
1004 qls_media_change(struct ifnet *ifp)
1005 {
1006 	qla_host_t *ha;
1007 	struct ifmedia *ifm;
1008 	int ret = 0;
1009 
1010 	ha = (qla_host_t *)ifp->if_softc;
1011 
1012 	QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
1013 
1014 	ifm = &ha->media;
1015 
1016 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1017 		ret = EINVAL;
1018 
1019 	QL_DPRINT2((ha->pci_dev, "%s: exit\n", __func__));
1020 
1021 	return (ret);
1022 }
1023 
1024 static void
1025 qls_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1026 {
1027 	qla_host_t *ha;
1028 
1029 	ha = (qla_host_t *)ifp->if_softc;
1030 
1031 	QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
1032 
1033 	ifmr->ifm_status = IFM_AVALID;
1034 	ifmr->ifm_active = IFM_ETHER;
1035 
1036 	qls_update_link_state(ha);
1037 	if (ha->link_up) {
1038 		ifmr->ifm_status |= IFM_ACTIVE;
1039 		ifmr->ifm_active |= (IFM_FDX | qls_get_optics(ha));
1040 	}
1041 
1042 	QL_DPRINT2((ha->pci_dev, "%s: exit (%s)\n", __func__,\
1043 		(ha->link_up ? "link_up" : "link_down")));
1044 
1045 	return;
1046 }
1047 
1048 static void
1049 qls_start(struct ifnet *ifp)
1050 {
1051 	int		i, ret = 0;
1052 	struct mbuf	*m_head;
1053 	qla_host_t	*ha = (qla_host_t *)ifp->if_softc;
1054 
1055 	QL_DPRINT8((ha->pci_dev, "%s: enter\n", __func__));
1056 
1057 	if (!mtx_trylock(&ha->tx_lock)) {
1058 		QL_DPRINT8((ha->pci_dev,
1059 			"%s: mtx_trylock(&ha->tx_lock) failed\n", __func__));
1060 		return;
1061 	}
1062 
1063 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) ==
1064 		IFF_DRV_RUNNING) {
1065 		for (i = 0; i < ha->num_tx_rings; i++) {
1066 			ret |= qls_hw_tx_done(ha, i);
1067 		}
1068 
1069 		if (ret == 0)
1070 			ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1071 	}
1072 
1073 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1074 		IFF_DRV_RUNNING) {
1075 		QL_DPRINT8((ha->pci_dev, "%s: !IFF_DRV_RUNNING\n", __func__));
1076 		QLA_TX_UNLOCK(ha);
1077 		return;
1078 	}
1079 
1080 	if (!ha->link_up) {
1081 		qls_update_link_state(ha);
1082 		if (!ha->link_up) {
1083 			QL_DPRINT8((ha->pci_dev, "%s: link down\n", __func__));
1084 			QLA_TX_UNLOCK(ha);
1085 			return;
1086 		}
1087 	}
1088 
1089 	while (ifp->if_snd.ifq_head != NULL) {
1090 		IF_DEQUEUE(&ifp->if_snd, m_head);
1091 
1092 		if (m_head == NULL) {
1093 			QL_DPRINT8((ha->pci_dev, "%s: m_head == NULL\n",
1094 				__func__));
1095 			break;
1096 		}
1097 
1098 		if (qls_send(ha, &m_head)) {
1099 			if (m_head == NULL)
1100 				break;
1101 			QL_DPRINT8((ha->pci_dev, "%s: PREPEND\n", __func__));
1102 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1103 			IF_PREPEND(&ifp->if_snd, m_head);
1104 			break;
1105 		}
1106 		/* Send a copy of the frame to the BPF listener */
1107 		ETHER_BPF_MTAP(ifp, m_head);
1108 	}
1109 
1110 	QLA_TX_UNLOCK(ha);
1111 	QL_DPRINT8((ha->pci_dev, "%s: exit\n", __func__));
1112 	return;
1113 }
1114 
1115 static int
1116 qls_send(qla_host_t *ha, struct mbuf **m_headp)
1117 {
1118 	bus_dma_segment_t	segs[QLA_MAX_SEGMENTS];
1119 	bus_dmamap_t		map;
1120 	int			nsegs;
1121 	int			ret = -1;
1122 	uint32_t		tx_idx;
1123 	struct mbuf		*m_head = *m_headp;
1124 	uint32_t		txr_idx = 0;
1125 
1126 	QL_DPRINT8((ha->pci_dev, "%s: enter\n", __func__));
1127 
1128 	/* check if flowid is set */
1129 	if (M_HASHTYPE_GET(m_head) != M_HASHTYPE_NONE)
1130 		txr_idx = m_head->m_pkthdr.flowid & (ha->num_tx_rings - 1);
1131 
1132 	tx_idx = ha->tx_ring[txr_idx].txr_next;
1133 
1134 	map = ha->tx_ring[txr_idx].tx_buf[tx_idx].map;
1135 
1136 	ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head, segs, &nsegs,
1137 			BUS_DMA_NOWAIT);
1138 
1139 	if (ret == EFBIG) {
1140 		struct mbuf *m;
1141 
1142 		QL_DPRINT8((ha->pci_dev, "%s: EFBIG [%d]\n", __func__,
1143 			m_head->m_pkthdr.len));
1144 
1145 		m = m_defrag(m_head, M_NOWAIT);
1146 		if (m == NULL) {
1147 			ha->err_tx_defrag++;
1148 			m_freem(m_head);
1149 			*m_headp = NULL;
1150 			device_printf(ha->pci_dev,
1151 				"%s: m_defrag() = NULL [%d]\n",
1152 				__func__, ret);
1153 			return (ENOBUFS);
1154 		}
1155 		m_head = m;
1156 		*m_headp = m_head;
1157 
1158 		if ((ret = bus_dmamap_load_mbuf_sg(ha->tx_tag, map, m_head,
1159 					segs, &nsegs, BUS_DMA_NOWAIT))) {
1160 			ha->err_tx_dmamap_load++;
1161 
1162 			device_printf(ha->pci_dev,
1163 				"%s: bus_dmamap_load_mbuf_sg failed0[%d, %d]\n",
1164 				__func__, ret, m_head->m_pkthdr.len);
1165 
1166 			if (ret != ENOMEM) {
1167 				m_freem(m_head);
1168 				*m_headp = NULL;
1169 			}
1170 			return (ret);
1171 		}
1172 
1173 	} else if (ret) {
1174 		ha->err_tx_dmamap_load++;
1175 
1176 		device_printf(ha->pci_dev,
1177 			"%s: bus_dmamap_load_mbuf_sg failed1[%d, %d]\n",
1178 			__func__, ret, m_head->m_pkthdr.len);
1179 
1180 		if (ret != ENOMEM) {
1181 			m_freem(m_head);
1182 			*m_headp = NULL;
1183 		}
1184 		return (ret);
1185 	}
1186 
1187 	QL_ASSERT(ha, (nsegs != 0), ("qls_send: empty packet"));
1188 
1189 	bus_dmamap_sync(ha->tx_tag, map, BUS_DMASYNC_PREWRITE);
1190 
1191         if (!(ret = qls_hw_send(ha, segs, nsegs, tx_idx, m_head, txr_idx))) {
1192 		ha->tx_ring[txr_idx].count++;
1193 		ha->tx_ring[txr_idx].tx_buf[tx_idx].m_head = m_head;
1194 		ha->tx_ring[txr_idx].tx_buf[tx_idx].map = map;
1195 	} else {
1196 		if (ret == EINVAL) {
1197 			if (m_head)
1198 				m_freem(m_head);
1199 			*m_headp = NULL;
1200 		}
1201 	}
1202 
1203 	QL_DPRINT8((ha->pci_dev, "%s: exit\n", __func__));
1204 	return (ret);
1205 }
1206 
1207 static void
1208 qls_stop(qla_host_t *ha)
1209 {
1210 	struct ifnet *ifp = ha->ifp;
1211 
1212 	ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE | IFF_DRV_RUNNING);
1213 
1214 	ha->flags.qla_watchdog_pause = 1;
1215 
1216 	while (!ha->qla_watchdog_paused)
1217 		qls_mdelay(__func__, 1);
1218 
1219 	qls_del_hw_if(ha);
1220 
1221 	qls_free_lro(ha);
1222 
1223 	qls_flush_xmt_bufs(ha);
1224 	qls_free_rcv_bufs(ha);
1225 
1226 	return;
1227 }
1228 
1229 /*
1230  * Buffer Management Functions for Transmit and Receive Rings
1231  */
1232 /*
1233  * Release mbuf after it sent on the wire
1234  */
1235 static void
1236 qls_flush_tx_buf(qla_host_t *ha, qla_tx_buf_t *txb)
1237 {
1238 	QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
1239 
1240 	if (txb->m_head) {
1241 		bus_dmamap_unload(ha->tx_tag, txb->map);
1242 
1243 		m_freem(txb->m_head);
1244 		txb->m_head = NULL;
1245 	}
1246 
1247 	QL_DPRINT2((ha->pci_dev, "%s: exit\n", __func__));
1248 }
1249 
1250 static void
1251 qls_flush_xmt_bufs(qla_host_t *ha)
1252 {
1253 	int		i, j;
1254 
1255 	for (j = 0; j < ha->num_tx_rings; j++) {
1256 		for (i = 0; i < NUM_TX_DESCRIPTORS; i++)
1257 			qls_flush_tx_buf(ha, &ha->tx_ring[j].tx_buf[i]);
1258 	}
1259 
1260 	return;
1261 }
1262 
1263 static int
1264 qls_alloc_rcv_mbufs(qla_host_t *ha, int r)
1265 {
1266 	int			i, j, ret = 0;
1267 	qla_rx_buf_t		*rxb;
1268 	qla_rx_ring_t		*rx_ring;
1269 	volatile q81_bq_addr_e_t *sbq_e;
1270 
1271 	rx_ring = &ha->rx_ring[r];
1272 
1273 	for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1274 		rxb = &rx_ring->rx_buf[i];
1275 
1276 		ret = bus_dmamap_create(ha->rx_tag, BUS_DMA_NOWAIT, &rxb->map);
1277 
1278 		if (ret) {
1279 			device_printf(ha->pci_dev,
1280 				"%s: dmamap[%d, %d] failed\n", __func__, r, i);
1281 
1282 			for (j = 0; j < i; j++) {
1283 				rxb = &rx_ring->rx_buf[j];
1284 				bus_dmamap_destroy(ha->rx_tag, rxb->map);
1285 			}
1286 			goto qls_alloc_rcv_mbufs_err;
1287 		}
1288 	}
1289 
1290 	rx_ring = &ha->rx_ring[r];
1291 
1292 	sbq_e = rx_ring->sbq_vaddr;
1293 
1294 	rxb = &rx_ring->rx_buf[0];
1295 
1296 	for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1297 		if (!(ret = qls_get_mbuf(ha, rxb, NULL))) {
1298 			/*
1299 		 	 * set the physical address in the
1300 			 * corresponding descriptor entry in the
1301 			 * receive ring/queue for the hba
1302 			 */
1303 
1304 			sbq_e->addr_lo = rxb->paddr & 0xFFFFFFFF;
1305 			sbq_e->addr_hi = (rxb->paddr >> 32) & 0xFFFFFFFF;
1306 
1307 		} else {
1308 			device_printf(ha->pci_dev,
1309 				"%s: qls_get_mbuf [%d, %d] failed\n",
1310 					__func__, r, i);
1311 			bus_dmamap_destroy(ha->rx_tag, rxb->map);
1312 			goto qls_alloc_rcv_mbufs_err;
1313 		}
1314 
1315 		rxb++;
1316 		sbq_e++;
1317 	}
1318 	return 0;
1319 
1320 qls_alloc_rcv_mbufs_err:
1321 	return (-1);
1322 }
1323 
1324 static void
1325 qls_free_rcv_bufs(qla_host_t *ha)
1326 {
1327 	int		i, r;
1328 	qla_rx_buf_t	*rxb;
1329 	qla_rx_ring_t	*rxr;
1330 
1331 	for (r = 0; r < ha->num_rx_rings; r++) {
1332 		rxr = &ha->rx_ring[r];
1333 
1334 		for (i = 0; i < NUM_RX_DESCRIPTORS; i++) {
1335 			rxb = &rxr->rx_buf[i];
1336 
1337 			if (rxb->m_head != NULL) {
1338 				bus_dmamap_unload(ha->rx_tag, rxb->map);
1339 				bus_dmamap_destroy(ha->rx_tag, rxb->map);
1340 				m_freem(rxb->m_head);
1341 			}
1342 		}
1343 		bzero(rxr->rx_buf, (sizeof(qla_rx_buf_t) * NUM_RX_DESCRIPTORS));
1344 	}
1345 	return;
1346 }
1347 
1348 static int
1349 qls_alloc_rcv_bufs(qla_host_t *ha)
1350 {
1351 	int		r, ret = 0;
1352 	qla_rx_ring_t	*rxr;
1353 
1354 	for (r = 0; r < ha->num_rx_rings; r++) {
1355 		rxr = &ha->rx_ring[r];
1356 		bzero(rxr->rx_buf, (sizeof(qla_rx_buf_t) * NUM_RX_DESCRIPTORS));
1357 	}
1358 
1359 	for (r = 0; r < ha->num_rx_rings; r++) {
1360 		ret = qls_alloc_rcv_mbufs(ha, r);
1361 
1362 		if (ret)
1363 			qls_free_rcv_bufs(ha);
1364 	}
1365 
1366 	return (ret);
1367 }
1368 
1369 int
1370 qls_get_mbuf(qla_host_t *ha, qla_rx_buf_t *rxb, struct mbuf *nmp)
1371 {
1372 	struct mbuf *mp = nmp;
1373 	int            		ret = 0;
1374 	uint32_t		offset;
1375 	bus_dma_segment_t	segs[1];
1376 	int			nsegs;
1377 
1378 	QL_DPRINT2((ha->pci_dev, "%s: enter\n", __func__));
1379 
1380 	if (mp == NULL) {
1381 		mp = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, ha->msize);
1382 
1383 		if (mp == NULL) {
1384 			if (ha->msize == MCLBYTES)
1385 				ha->err_m_getcl++;
1386 			else
1387 				ha->err_m_getjcl++;
1388 
1389 			ret = ENOBUFS;
1390 			device_printf(ha->pci_dev,
1391 					"%s: m_getcl failed\n", __func__);
1392 			goto exit_qls_get_mbuf;
1393 		}
1394 		mp->m_len = mp->m_pkthdr.len = ha->msize;
1395 	} else {
1396 		mp->m_len = mp->m_pkthdr.len = ha->msize;
1397 		mp->m_data = mp->m_ext.ext_buf;
1398 		mp->m_next = NULL;
1399 	}
1400 
1401 	/* align the receive buffers to 8 byte boundary */
1402 	offset = (uint32_t)((unsigned long long)mp->m_data & 0x7ULL);
1403 	if (offset) {
1404 		offset = 8 - offset;
1405 		m_adj(mp, offset);
1406 	}
1407 
1408 	/*
1409 	 * Using memory from the mbuf cluster pool, invoke the bus_dma
1410 	 * machinery to arrange the memory mapping.
1411 	 */
1412 	ret = bus_dmamap_load_mbuf_sg(ha->rx_tag, rxb->map,
1413 			mp, segs, &nsegs, BUS_DMA_NOWAIT);
1414 	rxb->paddr = segs[0].ds_addr;
1415 
1416 	if (ret || !rxb->paddr || (nsegs != 1)) {
1417 		m_freem(mp);
1418 		rxb->m_head = NULL;
1419 		device_printf(ha->pci_dev,
1420 			"%s: bus_dmamap_load failed[%d, 0x%016llx, %d]\n",
1421 			__func__, ret, (long long unsigned int)rxb->paddr,
1422 			nsegs);
1423                 ret = -1;
1424 		goto exit_qls_get_mbuf;
1425 	}
1426 	rxb->m_head = mp;
1427 	bus_dmamap_sync(ha->rx_tag, rxb->map, BUS_DMASYNC_PREREAD);
1428 
1429 exit_qls_get_mbuf:
1430 	QL_DPRINT2((ha->pci_dev, "%s: exit ret = 0x%08x\n", __func__, ret));
1431 	return (ret);
1432 }
1433 
1434 static void
1435 qls_tx_done(void *context, int pending)
1436 {
1437 	qla_host_t *ha = context;
1438 	struct ifnet   *ifp;
1439 
1440 	ifp = ha->ifp;
1441 
1442 	if (!ifp)
1443 		return;
1444 
1445 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1446 		QL_DPRINT8((ha->pci_dev, "%s: !IFF_DRV_RUNNING\n", __func__));
1447 		return;
1448 	}
1449 
1450 	qls_start(ha->ifp);
1451 	return;
1452 }
1453 
1454 static int
1455 qls_config_lro(qla_host_t *ha)
1456 {
1457 #if defined(INET) || defined(INET6)
1458         int i;
1459         struct lro_ctrl *lro;
1460 
1461         for (i = 0; i < ha->num_rx_rings; i++) {
1462                 lro = &ha->rx_ring[i].lro;
1463                 if (tcp_lro_init(lro)) {
1464                         device_printf(ha->pci_dev, "%s: tcp_lro_init failed\n",
1465                                 __func__);
1466                         return (-1);
1467                 }
1468                 lro->ifp = ha->ifp;
1469         }
1470         ha->flags.lro_init = 1;
1471 
1472         QL_DPRINT2((ha->pci_dev, "%s: LRO initialized\n", __func__));
1473 #endif
1474         return (0);
1475 }
1476 
1477 static void
1478 qls_free_lro(qla_host_t *ha)
1479 {
1480 #if defined(INET) || defined(INET6)
1481         int i;
1482         struct lro_ctrl *lro;
1483 
1484         if (!ha->flags.lro_init)
1485                 return;
1486 
1487         for (i = 0; i < ha->num_rx_rings; i++) {
1488                 lro = &ha->rx_ring[i].lro;
1489                 tcp_lro_free(lro);
1490         }
1491         ha->flags.lro_init = 0;
1492 #endif
1493 }
1494 
1495 static void
1496 qls_error_recovery(void *context, int pending)
1497 {
1498         qla_host_t *ha = context;
1499 
1500 	qls_init(ha);
1501 
1502 	return;
1503 }
1504