xref: /titanic_51/usr/src/uts/common/io/myri10ge/drv/myri10ge.c (revision b72c368a02e0464faeef362bc5a1cf0fc69981da)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Copyright 2007-2009 Myricom, Inc.  All rights reserved.
29  * Use is subject to license terms.
30  */
31 
32 #ifndef	lint
33 static const char __idstring[] =
34 	"@(#)$Id: myri10ge.c,v 1.186 2009-06-29 13:47:22 gallatin Exp $";
35 #endif
36 
37 #define	MXGEFW_NDIS
38 #include "myri10ge_var.h"
39 #include "rss_eth_z8e.h"
40 #include "rss_ethp_z8e.h"
41 #include "mcp_gen_header.h"
42 
43 #define	MYRI10GE_MAX_ETHER_MTU 9014
44 
45 #define	MYRI10GE_ETH_STOPPED 0
46 #define	MYRI10GE_ETH_STOPPING 1
47 #define	MYRI10GE_ETH_STARTING 2
48 #define	MYRI10GE_ETH_RUNNING 3
49 #define	MYRI10GE_ETH_OPEN_FAILED 4
50 #define	MYRI10GE_ETH_SUSPENDED_RUNNING 5
51 
52 static int myri10ge_small_bytes = 510;
53 static int myri10ge_intr_coal_delay = 125;
54 static int myri10ge_flow_control = 1;
55 #if #cpu(i386) || defined __i386 || defined i386 ||	\
56 	defined __i386__ || #cpu(x86_64) || defined __x86_64__
57 static int myri10ge_nvidia_ecrc_enable = 1;
58 #endif
59 static int myri10ge_mtu_override = 0;
60 static int myri10ge_tx_copylen = 512;
61 static int myri10ge_deassert_wait = 1;
62 static int myri10ge_verbose = 0;
63 static int myri10ge_watchdog_reset = 0;
64 static int myri10ge_use_msix = 1;
65 static int myri10ge_max_slices = -1;
66 static int myri10ge_use_msi = 1;
67 int myri10ge_force_firmware = 0;
68 static boolean_t myri10ge_use_lso = B_TRUE;
69 static int myri10ge_rss_hash = MXGEFW_RSS_HASH_TYPE_SRC_DST_PORT;
70 static int myri10ge_tx_hash = 1;
71 static int myri10ge_lro = 1;
72 static int myri10ge_lro_cnt = 8;
73 int myri10ge_lro_max_aggr = 2;
74 static int myri10ge_lso_copy = 0;
75 static mblk_t *myri10ge_send_wrapper(void *arg, mblk_t *mp);
76 int myri10ge_tx_handles_initial = 128;
77 
78 static 	kmutex_t myri10ge_param_lock;
79 static void* myri10ge_db_lastfree;
80 
81 static int myri10ge_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
82 static int myri10ge_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
83 static int myri10ge_quiesce(dev_info_t *dip);
84 
85 DDI_DEFINE_STREAM_OPS(myri10ge_ops, nulldev, nulldev, myri10ge_attach,
86     myri10ge_detach, nodev, NULL, D_MP, NULL, myri10ge_quiesce);
87 
88 
89 static struct modldrv modldrv = {
90 	&mod_driverops,
91 	"Myricom 10G driver (10GbE)",
92 	&myri10ge_ops,
93 };
94 
95 
96 static struct modlinkage modlinkage = {
97 	MODREV_1,
98 	{&modldrv, NULL},
99 };
100 
101 unsigned char myri10ge_broadcastaddr[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
102 
103 static ddi_dma_attr_t myri10ge_misc_dma_attr = {
104 	DMA_ATTR_V0,			/* version number. */
105 	(uint64_t)0, 			/* low address */
106 	(uint64_t)0xffffffffffffffffULL, /* high address */
107 	(uint64_t)0x7ffffff,		/* address counter max */
108 	(uint64_t)4096,			/* alignment */
109 	(uint_t)0x7f,			/* burstsizes for 32b and 64b xfers */
110 	(uint32_t)0x1,			/* minimum transfer size */
111 	(uint64_t)0x7fffffff,		/* maximum transfer size */
112 	(uint64_t)0x7fffffff,		/* maximum segment size */
113 	1,				/* scatter/gather list length */
114 	1,				/* granularity */
115 	0				/* attribute flags */
116 };
117 
118 /*
119  * The Myri10GE NIC has the following constraints on receive buffers:
120  * 1) Buffers which cross a 4KB boundary must be aligned to 4KB
121  * 2) Buffers which are not aligned to 4KB must not cross a 4KB boundary
122  */
123 
124 static ddi_dma_attr_t myri10ge_rx_jumbo_dma_attr = {
125 	DMA_ATTR_V0,			/* version number. */
126 	(uint64_t)0, 			/* low address */
127 	(uint64_t)0xffffffffffffffffULL, /* high address */
128 	(uint64_t)0x7ffffff,		/* address counter max */
129 	(uint64_t)4096,			/* alignment */
130 	(uint_t)0x7f,			/* burstsizes for 32b and 64b xfers */
131 	(uint32_t)0x1,			/* minimum transfer size */
132 	(uint64_t)0x7fffffff,		/* maximum transfer size */
133 	UINT64_MAX,			/* maximum segment size */
134 	1,				/* scatter/gather list length */
135 	1,				/* granularity */
136 	0				/* attribute flags */
137 };
138 
139 static ddi_dma_attr_t myri10ge_rx_std_dma_attr = {
140 	DMA_ATTR_V0,			/* version number. */
141 	(uint64_t)0, 			/* low address */
142 	(uint64_t)0xffffffffffffffffULL, /* high address */
143 	(uint64_t)0x7ffffff,		/* address counter max */
144 #if defined sparc64 || defined __sparcv9
145 	(uint64_t)4096,			/* alignment */
146 #else
147 	(uint64_t)0x80,			/* alignment */
148 #endif
149 	(uint_t)0x7f,			/* burstsizes for 32b and 64b xfers */
150 	(uint32_t)0x1,			/* minimum transfer size */
151 	(uint64_t)0x7fffffff,		/* maximum transfer size */
152 #if defined sparc64 || defined __sparcv9
153 	UINT64_MAX,			/* maximum segment size */
154 #else
155 	(uint64_t)0xfff,		/* maximum segment size */
156 #endif
157 	1,				/* scatter/gather list length */
158 	1,				/* granularity */
159 	0				/* attribute flags */
160 };
161 
162 static ddi_dma_attr_t myri10ge_tx_dma_attr = {
163 	DMA_ATTR_V0,			/* version number. */
164 	(uint64_t)0, 			/* low address */
165 	(uint64_t)0xffffffffffffffffULL, /* high address */
166 	(uint64_t)0x7ffffff,		/* address counter max */
167 	(uint64_t)1,			/* alignment */
168 	(uint_t)0x7f,			/* burstsizes for 32b and 64b xfers */
169 	(uint32_t)0x1,			/* minimum transfer size */
170 	(uint64_t)0x7fffffff,		/* maximum transfer size */
171 	UINT64_MAX,			/* maximum segment size */
172 	INT32_MAX,			/* scatter/gather list length */
173 	1,				/* granularity */
174 	0			/* attribute flags */
175 };
176 
177 #if defined sparc64 || defined __sparcv9
178 #define	WC 0
179 #else
180 #define	WC 1
181 #endif
182 
183 struct ddi_device_acc_attr myri10ge_dev_access_attr = {
184 	DDI_DEVICE_ATTR_V0,		/* version */
185 	DDI_NEVERSWAP_ACC,		/* endian flash */
186 #if WC
187 	DDI_MERGING_OK_ACC		/* data order */
188 #else
189 	DDI_STRICTORDER_ACC
190 #endif
191 };
192 
193 static void myri10ge_watchdog(void *arg);
194 
195 #ifdef MYRICOM_PRIV
196 int myri10ge_mtu = MYRI10GE_MAX_ETHER_MTU + MXGEFW_PAD + VLAN_TAGSZ;
197 #else
198 int myri10ge_mtu = ETHERMAX + MXGEFW_PAD + VLAN_TAGSZ;
199 #endif
200 int myri10ge_bigbufs_initial = 1024;
201 int myri10ge_bigbufs_max = 4096;
202 
203 
204 caddr_t
205 myri10ge_dma_alloc(dev_info_t *dip, size_t len,
206     ddi_dma_attr_t *attr, ddi_device_acc_attr_t  *accattr,
207     uint_t alloc_flags, int bind_flags, struct myri10ge_dma_stuff *dma,
208     int warn, int (*wait)(caddr_t))
209 {
210 	caddr_t  kaddr;
211 	size_t real_length;
212 	ddi_dma_cookie_t cookie;
213 	uint_t count;
214 	int err;
215 
216 	err = ddi_dma_alloc_handle(dip, attr, wait,
217 	    NULL, &dma->handle);
218 	if (err != DDI_SUCCESS) {
219 		if (warn)
220 			cmn_err(CE_WARN,
221 			    "myri10ge: ddi_dma_alloc_handle failed\n");
222 		goto abort_with_nothing;
223 	}
224 
225 	err = ddi_dma_mem_alloc(dma->handle, len, accattr, alloc_flags,
226 	    wait, NULL, &kaddr, &real_length,
227 	    &dma->acc_handle);
228 	if (err != DDI_SUCCESS) {
229 		if (warn)
230 			cmn_err(CE_WARN,
231 			    "myri10ge: ddi_dma_mem_alloc failed\n");
232 		goto abort_with_handle;
233 	}
234 
235 	err = ddi_dma_addr_bind_handle(dma->handle, NULL, kaddr, len,
236 	    bind_flags, wait, NULL, &cookie, &count);
237 
238 	if (err != DDI_SUCCESS) {
239 		if (warn)
240 			cmn_err(CE_WARN,
241 			    "myri10ge: ddi_dma_addr_bind_handle failed\n");
242 		goto abort_with_mem;
243 	}
244 
245 	if (count != 1) {
246 		if (warn)
247 			cmn_err(CE_WARN,
248 			    "myri10ge: got too many dma segments ");
249 		goto abort_with_bind;
250 	}
251 	dma->low = htonl(MYRI10GE_LOWPART_TO_U32(cookie.dmac_laddress));
252 	dma->high = htonl(MYRI10GE_HIGHPART_TO_U32(cookie.dmac_laddress));
253 	return (kaddr);
254 
255 abort_with_bind:
256 	(void) ddi_dma_unbind_handle(dma->handle);
257 
258 abort_with_mem:
259 	ddi_dma_mem_free(&dma->acc_handle);
260 
261 abort_with_handle:
262 	ddi_dma_free_handle(&dma->handle);
263 abort_with_nothing:
264 	if (warn) {
265 		cmn_err(CE_WARN, "myri10ge: myri10ge_dma_alloc failed.\n  ");
266 		cmn_err(CE_WARN, "args: dip=%p len=0x%lx ddi_dma_attr=%p\n",
267 		    (void*) dip, len, (void*) attr);
268 		cmn_err(CE_WARN,
269 		    "args: ddi_device_acc_attr=%p  alloc_flags=0x%x\n",
270 		    (void*) accattr, alloc_flags);
271 		cmn_err(CE_WARN, "args: bind_flags=0x%x  dmastuff=%p",
272 		    bind_flags, (void*) dma);
273 	}
274 	return (NULL);
275 
276 }
277 
278 void
279 myri10ge_dma_free(struct myri10ge_dma_stuff *dma)
280 {
281 	(void) ddi_dma_unbind_handle(dma->handle);
282 	ddi_dma_mem_free(&dma->acc_handle);
283 	ddi_dma_free_handle(&dma->handle);
284 }
285 
286 static inline void
287 myri10ge_pio_copy32(void *to, uint32_t *from32, size_t size)
288 {
289 	register volatile uint32_t *to32;
290 	size_t i;
291 
292 	to32 = (volatile uint32_t *) to;
293 	for (i = (size / 4); i; i--) {
294 		*to32 = *from32;
295 		to32++;
296 		from32++;
297 	}
298 }
299 
300 #if defined(_LP64)
301 static inline void
302 myri10ge_pio_copy64(void *to, uint64_t *from64, size_t size)
303 {
304 	register volatile uint64_t *to64;
305 	size_t i;
306 
307 	to64 = (volatile uint64_t *) to;
308 	for (i = (size / 8); i; i--) {
309 		*to64 = *from64;
310 		to64++;
311 		from64++;
312 	}
313 }
314 #endif
315 
316 /*
317  * This routine copies memory from the host to the NIC.
318  * The "size" argument must always be a multiple of
319  * the size of long (4 or 8 bytes), and to/from must also
320  * be naturally aligned.
321  */
322 static inline void
323 myri10ge_pio_copy(void *to, void *from, size_t size)
324 {
325 #if !defined(_LP64)
326 	ASSERT((size % 4) == 0);
327 	myri10ge_pio_copy32(to, (uint32_t *)from, size);
328 #else
329 	ASSERT((size % 8) == 0);
330 	myri10ge_pio_copy64(to, (uint64_t *)from, size);
331 #endif
332 }
333 
334 
335 /*
336  * Due to various bugs in Solaris (especially bug 6186772 where the
337  * TCP/UDP checksum is calculated incorrectly on mblk chains with more
338  * than two elements), and the design bug where hardware checksums are
339  * ignored on mblk chains with more than 2 elements, we need to
340  * allocate private pool of physically contiguous receive buffers.
341  */
342 
343 static void
344 myri10ge_jpool_init(struct myri10ge_slice_state *ss)
345 {
346 	struct myri10ge_jpool_stuff *jpool = &ss->jpool;
347 
348 	bzero(jpool, sizeof (*jpool));
349 	mutex_init(&jpool->mtx, NULL, MUTEX_DRIVER,
350 	    ss->mgp->icookie);
351 	jpool->head = NULL;
352 }
353 
354 static void
355 myri10ge_jpool_fini(struct myri10ge_slice_state *ss)
356 {
357 	struct myri10ge_jpool_stuff *jpool = &ss->jpool;
358 
359 	if (jpool->head != NULL) {
360 		cmn_err(CE_WARN,
361 		    "%s: BUG! myri10ge_jpool_fini called on non-empty pool\n",
362 		    ss->mgp->name);
363 	}
364 	mutex_destroy(&jpool->mtx);
365 }
366 
367 
368 /*
369  * copy an array of mcp_kreq_ether_recv_t's to the mcp.  Copy
370  * at most 32 bytes at a time, so as to avoid involving the software
371  * pio handler in the nic.   We re-write the first segment's low
372  * DMA address to mark it valid only after we write the entire chunk
373  * in a burst
374  */
375 static inline void
376 myri10ge_submit_8rx(mcp_kreq_ether_recv_t *dst, mcp_kreq_ether_recv_t *src)
377 {
378 	src->addr_low |= BE_32(1);
379 	myri10ge_pio_copy(dst, src, 4 * sizeof (*src));
380 	mb();
381 	myri10ge_pio_copy(dst + 4, src + 4, 4 * sizeof (*src));
382 	mb();
383 	src->addr_low &= ~(BE_32(1));
384 	dst->addr_low = src->addr_low;
385 	mb();
386 }
387 
388 static void
389 myri10ge_pull_jpool(struct myri10ge_slice_state *ss)
390 {
391 	struct myri10ge_jpool_stuff *jpool = &ss->jpool;
392 	struct myri10ge_jpool_entry *jtail, *j, *jfree;
393 	volatile uintptr_t *putp;
394 	uintptr_t put;
395 	int i;
396 
397 	/* find tail */
398 	jtail = NULL;
399 	if (jpool->head != NULL) {
400 		j = jpool->head;
401 		while (j->next != NULL)
402 			j = j->next;
403 		jtail = j;
404 	}
405 
406 	/*
407 	 * iterate over all per-CPU caches, and add contents into
408 	 * jpool
409 	 */
410 	for (i = 0; i < MYRI10GE_MAX_CPUS; i++) {
411 		/* take per-CPU free list */
412 		putp = (void *)&jpool->cpu[i & MYRI10GE_MAX_CPU_MASK].head;
413 		if (*putp == NULL)
414 			continue;
415 		put = atomic_swap_ulong(putp, 0);
416 		jfree = (struct myri10ge_jpool_entry *)put;
417 
418 		/* append to pool */
419 		if (jtail == NULL) {
420 			jpool->head = jfree;
421 		} else {
422 			jtail->next = jfree;
423 		}
424 		j = jfree;
425 		while (j->next != NULL)
426 			j = j->next;
427 		jtail = j;
428 	}
429 }
430 
431 /*
432  * Transfers buffers from the free pool to the nic
433  * Must be called holding the jpool mutex.
434  */
435 
436 static inline void
437 myri10ge_restock_jumbos(struct myri10ge_slice_state *ss)
438 {
439 	struct myri10ge_jpool_stuff *jpool = &ss->jpool;
440 	struct myri10ge_jpool_entry *j;
441 	myri10ge_rx_ring_t *rx;
442 	int i, idx, limit;
443 
444 	rx = &ss->rx_big;
445 	limit = ss->j_rx_cnt + (rx->mask + 1);
446 
447 	for (i = rx->cnt; i != limit; i++) {
448 		idx = i & (rx->mask);
449 		j = jpool->head;
450 		if (j == NULL) {
451 			myri10ge_pull_jpool(ss);
452 			j = jpool->head;
453 			if (j == NULL) {
454 				break;
455 			}
456 		}
457 		jpool->head = j->next;
458 		rx->info[idx].j = j;
459 		rx->shadow[idx].addr_low = j->dma.low;
460 		rx->shadow[idx].addr_high = j->dma.high;
461 		/* copy 4 descriptors (32-bytes) to the mcp at a time */
462 		if ((idx & 7) == 7) {
463 			myri10ge_submit_8rx(&rx->lanai[idx - 7],
464 			    &rx->shadow[idx - 7]);
465 		}
466 	}
467 	rx->cnt = i;
468 }
469 
470 /*
471  * Transfer buffers from the nic to the free pool.
472  * Should be called holding the jpool mutex
473  */
474 
475 static inline void
476 myri10ge_unstock_jumbos(struct myri10ge_slice_state *ss)
477 {
478 	struct myri10ge_jpool_stuff *jpool = &ss->jpool;
479 	struct myri10ge_jpool_entry *j;
480 	myri10ge_rx_ring_t *rx;
481 	int i;
482 
483 	mutex_enter(&jpool->mtx);
484 	rx = &ss->rx_big;
485 
486 	for (i = 0; i < rx->mask + 1; i++) {
487 		j = rx->info[i].j;
488 		rx->info[i].j = NULL;
489 		if (j == NULL)
490 			continue;
491 		j->next = jpool->head;
492 		jpool->head = j;
493 	}
494 	mutex_exit(&jpool->mtx);
495 
496 }
497 
498 
499 /*
500  * Free routine which is called when the mblk allocated via
501  * esballoc() is freed.   Here we return the jumbo buffer
502  * to the free pool, and possibly pass some jumbo buffers
503  * to the nic
504  */
505 
506 static void
507 myri10ge_jfree_rtn(void *arg)
508 {
509 	struct myri10ge_jpool_entry *j = (struct myri10ge_jpool_entry *)arg;
510 	struct myri10ge_jpool_stuff *jpool;
511 	volatile uintptr_t *putp;
512 	uintptr_t old, new;
513 
514 	jpool = &j->ss->jpool;
515 
516 	/* prepend buffer locklessly to per-CPU freelist */
517 	putp = (void *)&jpool->cpu[CPU->cpu_seqid & MYRI10GE_MAX_CPU_MASK].head;
518 	new = (uintptr_t)j;
519 	do {
520 		old = *putp;
521 		j->next = (void *)old;
522 	} while (atomic_cas_ulong(putp, old, new) != old);
523 }
524 
525 static void
526 myri10ge_remove_jbuf(struct myri10ge_jpool_entry *j)
527 {
528 	(void) ddi_dma_unbind_handle(j->dma_handle);
529 	ddi_dma_mem_free(&j->acc_handle);
530 	ddi_dma_free_handle(&j->dma_handle);
531 	kmem_free(j, sizeof (*j));
532 }
533 
534 
535 /*
536  * Allocates one physically contiguous descriptor
537  * and add it to the jumbo buffer pool.
538  */
539 
540 static int
541 myri10ge_add_jbuf(struct myri10ge_slice_state *ss)
542 {
543 	struct myri10ge_jpool_entry *j;
544 	struct myri10ge_jpool_stuff *jpool = &ss->jpool;
545 	ddi_dma_attr_t *rx_dma_attr;
546 	size_t real_length;
547 	ddi_dma_cookie_t cookie;
548 	uint_t count;
549 	int err;
550 
551 	if (myri10ge_mtu < 2048)
552 		rx_dma_attr = &myri10ge_rx_std_dma_attr;
553 	else
554 		rx_dma_attr = &myri10ge_rx_jumbo_dma_attr;
555 
556 again:
557 	j = (struct myri10ge_jpool_entry *)
558 	    kmem_alloc(sizeof (*j), KM_SLEEP);
559 	err = ddi_dma_alloc_handle(ss->mgp->dip, rx_dma_attr,
560 	    DDI_DMA_DONTWAIT, NULL, &j->dma_handle);
561 	if (err != DDI_SUCCESS)
562 		goto abort_with_j;
563 
564 	err = ddi_dma_mem_alloc(j->dma_handle, myri10ge_mtu,
565 	    &myri10ge_dev_access_attr,  DDI_DMA_STREAMING, DDI_DMA_DONTWAIT,
566 	    NULL, &j->buf, &real_length, &j->acc_handle);
567 	if (err != DDI_SUCCESS)
568 		goto abort_with_handle;
569 
570 	err = ddi_dma_addr_bind_handle(j->dma_handle, NULL, j->buf,
571 	    real_length, DDI_DMA_READ|DDI_DMA_STREAMING, DDI_DMA_DONTWAIT,
572 	    NULL, &cookie, &count);
573 	if (err != DDI_SUCCESS)
574 		goto abort_with_mem;
575 
576 	/*
577 	 * Make certain std MTU buffers do not cross a 4KB boundary:
578 	 *
579 	 * Setting dma_attr_align=4096 will do this, but the system
580 	 * will only allocate 1 RX buffer per 4KB page, rather than 2.
581 	 * Setting dma_attr_granular=4096 *seems* to work around this,
582 	 * but I'm paranoid about future systems no longer honoring
583 	 * this, so fall back to the safe, but memory wasting way if a
584 	 * buffer crosses a 4KB boundary.
585 	 */
586 
587 	if (rx_dma_attr == &myri10ge_rx_std_dma_attr &&
588 	    rx_dma_attr->dma_attr_align != 4096) {
589 		uint32_t start, end;
590 
591 		start = MYRI10GE_LOWPART_TO_U32(cookie.dmac_laddress);
592 		end = start + myri10ge_mtu;
593 		if (((end >> 12) != (start >> 12)) && (start & 4095U)) {
594 			printf("std buffer crossed a 4KB boundary!\n");
595 			myri10ge_remove_jbuf(j);
596 			rx_dma_attr->dma_attr_align = 4096;
597 			rx_dma_attr->dma_attr_seg = UINT64_MAX;
598 			goto again;
599 		}
600 	}
601 
602 	j->dma.low =
603 	    htonl(MYRI10GE_LOWPART_TO_U32(cookie.dmac_laddress));
604 	j->dma.high =
605 	    htonl(MYRI10GE_HIGHPART_TO_U32(cookie.dmac_laddress));
606 	j->ss = ss;
607 
608 
609 	j->free_func.free_func = myri10ge_jfree_rtn;
610 	j->free_func.free_arg = (char *)j;
611 	mutex_enter(&jpool->mtx);
612 	j->next = jpool->head;
613 	jpool->head = j;
614 	jpool->num_alloc++;
615 	mutex_exit(&jpool->mtx);
616 	return (0);
617 
618 abort_with_mem:
619 	ddi_dma_mem_free(&j->acc_handle);
620 
621 abort_with_handle:
622 	ddi_dma_free_handle(&j->dma_handle);
623 
624 abort_with_j:
625 	kmem_free(j, sizeof (*j));
626 
627 	/*
628 	 * If an allocation failed, perhaps it failed because it could
629 	 * not satisfy granularity requirement.  Disable that, and
630 	 * try agin.
631 	 */
632 	if (rx_dma_attr == &myri10ge_rx_std_dma_attr &&
633 	    rx_dma_attr->dma_attr_align != 4096) {
634 			cmn_err(CE_NOTE,
635 			    "!alloc failed, reverting to gran=1\n");
636 			rx_dma_attr->dma_attr_align = 4096;
637 			rx_dma_attr->dma_attr_seg = UINT64_MAX;
638 			goto again;
639 	}
640 	return (err);
641 }
642 
643 static int
644 myri10ge_jfree_cnt(struct myri10ge_jpool_stuff *jpool)
645 {
646 	int i;
647 	struct myri10ge_jpool_entry *j;
648 
649 	mutex_enter(&jpool->mtx);
650 	j = jpool->head;
651 	i = 0;
652 	while (j != NULL) {
653 		i++;
654 		j = j->next;
655 	}
656 	mutex_exit(&jpool->mtx);
657 	return (i);
658 }
659 
660 static int
661 myri10ge_add_jbufs(struct myri10ge_slice_state *ss, int num, int total)
662 {
663 	struct myri10ge_jpool_stuff *jpool = &ss->jpool;
664 	int allocated = 0;
665 	int err;
666 	int needed;
667 
668 	/*
669 	 * if total is set, user wants "num" jbufs in the pool,
670 	 * otherwise the user wants to "num" additional jbufs
671 	 * added to the pool
672 	 */
673 	if (total && jpool->num_alloc) {
674 		allocated = myri10ge_jfree_cnt(jpool);
675 		needed = num - allocated;
676 	} else {
677 		needed = num;
678 	}
679 
680 	while (needed > 0) {
681 		needed--;
682 		err = myri10ge_add_jbuf(ss);
683 		if (err == 0) {
684 			allocated++;
685 		}
686 	}
687 	return (allocated);
688 }
689 
690 static void
691 myri10ge_remove_jbufs(struct myri10ge_slice_state *ss)
692 {
693 	struct myri10ge_jpool_stuff *jpool = &ss->jpool;
694 	struct myri10ge_jpool_entry *j;
695 
696 	mutex_enter(&jpool->mtx);
697 	myri10ge_pull_jpool(ss);
698 	while (jpool->head != NULL) {
699 		jpool->num_alloc--;
700 		j = jpool->head;
701 		jpool->head = j->next;
702 		myri10ge_remove_jbuf(j);
703 	}
704 	mutex_exit(&jpool->mtx);
705 }
706 
707 static void
708 myri10ge_carve_up_jbufs_into_small_ring(struct myri10ge_slice_state *ss)
709 {
710 	struct myri10ge_jpool_stuff *jpool = &ss->jpool;
711 	struct myri10ge_jpool_entry *j = NULL;
712 	caddr_t ptr;
713 	uint32_t dma_low, dma_high;
714 	int idx, len;
715 	unsigned int alloc_size;
716 
717 	dma_low = dma_high = len = 0;
718 	alloc_size = myri10ge_small_bytes + MXGEFW_PAD;
719 	ptr = NULL;
720 	for (idx = 0; idx < ss->rx_small.mask + 1; idx++) {
721 		/* Allocate a jumbo frame and carve it into small frames */
722 		if (len < alloc_size) {
723 			mutex_enter(&jpool->mtx);
724 			/* remove jumbo from freelist */
725 			j = jpool->head;
726 			jpool->head = j->next;
727 			/* place it onto small list */
728 			j->next = ss->small_jpool;
729 			ss->small_jpool = j;
730 			mutex_exit(&jpool->mtx);
731 			len = myri10ge_mtu;
732 			dma_low = ntohl(j->dma.low);
733 			dma_high = ntohl(j->dma.high);
734 			ptr = j->buf;
735 		}
736 		ss->rx_small.info[idx].ptr = ptr;
737 		ss->rx_small.shadow[idx].addr_low = htonl(dma_low);
738 		ss->rx_small.shadow[idx].addr_high = htonl(dma_high);
739 		len -= alloc_size;
740 		ptr += alloc_size;
741 		dma_low += alloc_size;
742 	}
743 }
744 
745 /*
746  * Return the jumbo bufs we carved up for small to the jumbo pool
747  */
748 
749 static void
750 myri10ge_release_small_jbufs(struct myri10ge_slice_state *ss)
751 {
752 	struct myri10ge_jpool_stuff *jpool = &ss->jpool;
753 	struct myri10ge_jpool_entry *j = NULL;
754 
755 	mutex_enter(&jpool->mtx);
756 	while (ss->small_jpool != NULL) {
757 		j = ss->small_jpool;
758 		ss->small_jpool = j->next;
759 		j->next = jpool->head;
760 		jpool->head = j;
761 	}
762 	mutex_exit(&jpool->mtx);
763 	ss->jbufs_for_smalls = 0;
764 }
765 
766 static int
767 myri10ge_add_tx_handle(struct myri10ge_slice_state *ss)
768 {
769 	myri10ge_tx_ring_t *tx = &ss->tx;
770 	struct myri10ge_priv *mgp = ss->mgp;
771 	struct myri10ge_tx_dma_handle *handle;
772 	int err;
773 
774 	handle = kmem_zalloc(sizeof (*handle), KM_SLEEP);
775 	err = ddi_dma_alloc_handle(mgp->dip,
776 	    &myri10ge_tx_dma_attr,
777 	    DDI_DMA_SLEEP, NULL,
778 	    &handle->h);
779 	if (err) {
780 		static int limit = 0;
781 		if (limit == 0)
782 			cmn_err(CE_WARN, "%s: Falled to alloc tx dma handle\n",
783 			    mgp->name);
784 		limit++;
785 		kmem_free(handle, sizeof (*handle));
786 		return (err);
787 	}
788 	mutex_enter(&tx->handle_lock);
789 	MYRI10GE_SLICE_STAT_INC(tx_handles_alloced);
790 	handle->next = tx->free_tx_handles;
791 	tx->free_tx_handles = handle;
792 	mutex_exit(&tx->handle_lock);
793 	return (DDI_SUCCESS);
794 }
795 
796 static void
797 myri10ge_remove_tx_handles(struct myri10ge_slice_state *ss)
798 {
799 	myri10ge_tx_ring_t *tx = &ss->tx;
800 	struct myri10ge_tx_dma_handle *handle;
801 	mutex_enter(&tx->handle_lock);
802 
803 	handle = tx->free_tx_handles;
804 	while (handle != NULL) {
805 		tx->free_tx_handles = handle->next;
806 		ddi_dma_free_handle(&handle->h);
807 		kmem_free(handle, sizeof (*handle));
808 		handle = tx->free_tx_handles;
809 		MYRI10GE_SLICE_STAT_DEC(tx_handles_alloced);
810 	}
811 	mutex_exit(&tx->handle_lock);
812 	if (MYRI10GE_SLICE_STAT(tx_handles_alloced) != 0) {
813 		cmn_err(CE_WARN, "%s: %d tx dma handles allocated at close\n",
814 		    ss->mgp->name,
815 		    (int)MYRI10GE_SLICE_STAT(tx_handles_alloced));
816 	}
817 }
818 
819 static void
820 myri10ge_free_tx_handles(myri10ge_tx_ring_t *tx,
821     struct myri10ge_tx_dma_handle_head *list)
822 {
823 	mutex_enter(&tx->handle_lock);
824 	list->tail->next = tx->free_tx_handles;
825 	tx->free_tx_handles = list->head;
826 	mutex_exit(&tx->handle_lock);
827 }
828 
829 static void
830 myri10ge_free_tx_handle_slist(myri10ge_tx_ring_t *tx,
831     struct myri10ge_tx_dma_handle *handle)
832 {
833 	struct myri10ge_tx_dma_handle_head list;
834 
835 	if (handle == NULL)
836 		return;
837 	list.head = handle;
838 	list.tail = handle;
839 	while (handle != NULL) {
840 		list.tail = handle;
841 		handle = handle->next;
842 	}
843 	myri10ge_free_tx_handles(tx, &list);
844 }
845 
846 static int
847 myri10ge_alloc_tx_handles(struct myri10ge_slice_state *ss, int count,
848     struct myri10ge_tx_dma_handle **ret)
849 {
850 	myri10ge_tx_ring_t *tx = &ss->tx;
851 	struct myri10ge_tx_dma_handle *handle;
852 	int err, i;
853 
854 	mutex_enter(&tx->handle_lock);
855 	for (i = 0; i < count; i++) {
856 		handle = tx->free_tx_handles;
857 		while (handle == NULL) {
858 			mutex_exit(&tx->handle_lock);
859 			err = myri10ge_add_tx_handle(ss);
860 			if (err != DDI_SUCCESS) {
861 				goto abort_with_handles;
862 			}
863 			mutex_enter(&tx->handle_lock);
864 			handle = tx->free_tx_handles;
865 		}
866 		tx->free_tx_handles = handle->next;
867 		handle->next = *ret;
868 		*ret = handle;
869 	}
870 	mutex_exit(&tx->handle_lock);
871 	return (DDI_SUCCESS);
872 
873 abort_with_handles:
874 	myri10ge_free_tx_handle_slist(tx, *ret);
875 	return (err);
876 }
877 
878 
879 /*
880  * Frees DMA resources associated with the send ring
881  */
882 static void
883 myri10ge_unprepare_tx_ring(struct myri10ge_slice_state *ss)
884 {
885 	myri10ge_tx_ring_t *tx;
886 	struct myri10ge_tx_dma_handle_head handles;
887 	size_t bytes;
888 	int idx;
889 
890 	tx = &ss->tx;
891 	handles.head = NULL;
892 	handles.tail = NULL;
893 	for (idx = 0; idx < ss->tx.mask + 1; idx++) {
894 		if (tx->info[idx].m) {
895 			(void) ddi_dma_unbind_handle(tx->info[idx].handle->h);
896 			handles.head = tx->info[idx].handle;
897 			if (handles.tail == NULL)
898 				handles.tail = tx->info[idx].handle;
899 			freeb(tx->info[idx].m);
900 			tx->info[idx].m = 0;
901 			tx->info[idx].handle = 0;
902 		}
903 		tx->cp[idx].va = NULL;
904 		myri10ge_dma_free(&tx->cp[idx].dma);
905 	}
906 	bytes = sizeof (*tx->cp) * (tx->mask + 1);
907 	kmem_free(tx->cp, bytes);
908 	tx->cp = NULL;
909 	if (handles.head != NULL)
910 		myri10ge_free_tx_handles(tx, &handles);
911 	myri10ge_remove_tx_handles(ss);
912 }
913 
914 /*
915  * Allocates DMA handles associated with the send ring
916  */
917 static inline int
918 myri10ge_prepare_tx_ring(struct myri10ge_slice_state *ss)
919 {
920 	struct myri10ge_tx_dma_handle *handles;
921 	int h;
922 	size_t bytes;
923 
924 	bytes = sizeof (*ss->tx.cp) * (ss->tx.mask + 1);
925 	ss->tx.cp = kmem_zalloc(bytes, KM_SLEEP);
926 	if (ss->tx.cp == NULL) {
927 		cmn_err(CE_WARN,
928 		    "%s: Failed to allocate tx copyblock storage\n",
929 		    ss->mgp->name);
930 		return (DDI_FAILURE);
931 	}
932 
933 
934 	/* allocate the TX copyblocks */
935 	for (h = 0; h < ss->tx.mask + 1; h++) {
936 		ss->tx.cp[h].va = myri10ge_dma_alloc(ss->mgp->dip,
937 		    4096, &myri10ge_rx_jumbo_dma_attr,
938 		    &myri10ge_dev_access_attr, DDI_DMA_STREAMING,
939 		    DDI_DMA_WRITE|DDI_DMA_STREAMING, &ss->tx.cp[h].dma, 1,
940 		    DDI_DMA_DONTWAIT);
941 		if (ss->tx.cp[h].va == NULL) {
942 			cmn_err(CE_WARN, "%s: Failed to allocate tx "
943 			    "copyblock %d\n", ss->mgp->name, h);
944 			goto abort_with_copyblocks;
945 		}
946 	}
947 	/* pre-allocate transmit handles */
948 	handles = NULL;
949 	(void) myri10ge_alloc_tx_handles(ss, myri10ge_tx_handles_initial,
950 	    &handles);
951 	if (handles != NULL)
952 		myri10ge_free_tx_handle_slist(&ss->tx, handles);
953 
954 	return (DDI_SUCCESS);
955 
956 abort_with_copyblocks:
957 	while (h > 0)  {
958 		h--;
959 		myri10ge_dma_free(&ss->tx.cp[h].dma);
960 	}
961 
962 	bytes = sizeof (*ss->tx.cp) * (ss->tx.mask + 1);
963 	kmem_free(ss->tx.cp, bytes);
964 	ss->tx.cp = NULL;
965 	return (DDI_FAILURE);
966 }
967 
968 /*
969  * The eeprom strings on the lanaiX have the format
970  * SN=x\0
971  * MAC=x:x:x:x:x:x\0
972  * PT:ddd mmm xx xx:xx:xx xx\0
973  * PV:ddd mmm xx xx:xx:xx xx\0
974  */
975 static int
976 myri10ge_read_mac_addr(struct myri10ge_priv *mgp)
977 {
978 #define	MYRI10GE_NEXT_STRING(p) while (ptr < limit && *ptr++)
979 #define	myri10ge_digit(c) (((c) >= '0' && (c) <= '9') ? ((c) - '0') :	\
980 		(((c) >= 'A' && (c) <= 'F') ? (10 + (c) - 'A') :	\
981 		(((c) >= 'a' && (c) <= 'f') ? (10 + (c) - 'a') : -1)))
982 
983 	char *ptr, *limit;
984 	int i, hv, lv;
985 
986 	ptr = mgp->eeprom_strings;
987 	limit = mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE;
988 
989 	while (*ptr != '\0' && ptr < limit) {
990 		if (memcmp(ptr, "MAC=", 4) == 0) {
991 			ptr += 4;
992 			if (myri10ge_verbose)
993 				printf("%s: mac address = %s\n", mgp->name,
994 				    ptr);
995 			mgp->mac_addr_string = ptr;
996 			for (i = 0; i < 6; i++) {
997 				if ((ptr + 2) > limit)
998 					goto abort;
999 
1000 				if (*(ptr+1) == ':') {
1001 					hv = 0;
1002 					lv = myri10ge_digit(*ptr); ptr++;
1003 				} else {
1004 					hv = myri10ge_digit(*ptr); ptr++;
1005 					lv = myri10ge_digit(*ptr); ptr++;
1006 				}
1007 				mgp->mac_addr[i] = (hv << 4) | lv;
1008 				ptr++;
1009 			}
1010 		}
1011 		if (memcmp((const void *)ptr, "SN=", 3) == 0) {
1012 			ptr += 3;
1013 			mgp->sn_str = (char *)ptr;
1014 		}
1015 		if (memcmp((const void *)ptr, "PC=", 3) == 0) {
1016 			ptr += 3;
1017 			mgp->pc_str = (char *)ptr;
1018 		}
1019 		MYRI10GE_NEXT_STRING(ptr);
1020 	}
1021 
1022 	return (0);
1023 
1024 abort:
1025 	cmn_err(CE_WARN, "%s: failed to parse eeprom_strings", mgp->name);
1026 	return (ENXIO);
1027 }
1028 
1029 
1030 /*
1031  * Determine the register set containing the PCI resource we
1032  * want to map: the memory-mappable part of the interface. We do
1033  * this by scanning the DDI "reg" property of the interface,
1034  * which is an array of mx_ddi_reg_set structures.
1035  */
1036 static int
1037 myri10ge_reg_set(dev_info_t *dip, int *reg_set, int *span,
1038     unsigned long *busno, unsigned long *devno,
1039     unsigned long *funcno)
1040 {
1041 
1042 #define	REGISTER_NUMBER(ip)	(ip[0] >>  0 & 0xff)
1043 #define	FUNCTION_NUMBER(ip)	(ip[0] >>  8 & 0x07)
1044 #define	DEVICE_NUMBER(ip)	(ip[0] >> 11 & 0x1f)
1045 #define	BUS_NUMBER(ip)		(ip[0] >> 16 & 0xff)
1046 #define	ADDRESS_SPACE(ip)	(ip[0] >> 24 & 0x03)
1047 #define	PCI_ADDR_HIGH(ip)	(ip[1])
1048 #define	PCI_ADDR_LOW(ip) 	(ip[2])
1049 #define	PCI_SPAN_HIGH(ip)	(ip[3])
1050 #define	PCI_SPAN_LOW(ip)	(ip[4])
1051 
1052 #define	MX_DDI_REG_SET_32_BIT_MEMORY_SPACE 2
1053 #define	MX_DDI_REG_SET_64_BIT_MEMORY_SPACE 3
1054 
1055 	int *data, i, *rs;
1056 	uint32_t nelementsp;
1057 
1058 #ifdef MYRI10GE_REGSET_VERBOSE
1059 	char *address_space_name[] = { "Configuration Space",
1060 					"I/O Space",
1061 					"32-bit Memory Space",
1062 					"64-bit Memory Space"
1063 	};
1064 #endif
1065 
1066 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1067 	    "reg", &data, &nelementsp) != DDI_SUCCESS) {
1068 		printf("Could not determine register set.\n");
1069 		return (ENXIO);
1070 	}
1071 
1072 #ifdef MYRI10GE_REGSET_VERBOSE
1073 	printf("There are %d register sets.\n", nelementsp / 5);
1074 #endif
1075 	if (!nelementsp) {
1076 		printf("Didn't find any \"reg\" properties.\n");
1077 		ddi_prop_free(data);
1078 		return (ENODEV);
1079 	}
1080 
1081 	/* Scan for the register number. */
1082 	rs = &data[0];
1083 	*busno = BUS_NUMBER(rs);
1084 	*devno = DEVICE_NUMBER(rs);
1085 	*funcno = FUNCTION_NUMBER(rs);
1086 
1087 #ifdef MYRI10GE_REGSET_VERBOSE
1088 	printf("*** Scanning for register number.\n");
1089 #endif
1090 	for (i = 0; i < nelementsp / 5; i++) {
1091 		rs = &data[5 * i];
1092 #ifdef MYRI10GE_REGSET_VERBOSE
1093 		printf("Examining register set %d:\n", i);
1094 		printf("  Register number = %d.\n", REGISTER_NUMBER(rs));
1095 		printf("  Function number = %d.\n", FUNCTION_NUMBER(rs));
1096 		printf("  Device number   = %d.\n", DEVICE_NUMBER(rs));
1097 		printf("  Bus number      = %d.\n", BUS_NUMBER(rs));
1098 		printf("  Address space   = %d (%s ).\n", ADDRESS_SPACE(rs),
1099 		    address_space_name[ADDRESS_SPACE(rs)]);
1100 		printf("  pci address 0x%08x %08x\n", PCI_ADDR_HIGH(rs),
1101 		    PCI_ADDR_LOW(rs));
1102 		printf("  pci span 0x%08x %08x\n", PCI_SPAN_HIGH(rs),
1103 		    PCI_SPAN_LOW(rs));
1104 #endif
1105 		/* We are looking for a memory property. */
1106 
1107 		if (ADDRESS_SPACE(rs) == MX_DDI_REG_SET_64_BIT_MEMORY_SPACE ||
1108 		    ADDRESS_SPACE(rs) == MX_DDI_REG_SET_32_BIT_MEMORY_SPACE) {
1109 			*reg_set = i;
1110 
1111 #ifdef MYRI10GE_REGSET_VERBOSE
1112 			printf("%s uses register set %d.\n",
1113 			    address_space_name[ADDRESS_SPACE(rs)], *reg_set);
1114 #endif
1115 
1116 			*span = (PCI_SPAN_LOW(rs));
1117 #ifdef MYRI10GE_REGSET_VERBOSE
1118 			printf("Board span is 0x%x\n", *span);
1119 #endif
1120 			break;
1121 		}
1122 	}
1123 
1124 	ddi_prop_free(data);
1125 
1126 	/* If no match, fail. */
1127 	if (i >= nelementsp / 5) {
1128 		return (EIO);
1129 	}
1130 
1131 	return (0);
1132 }
1133 
1134 
1135 static int
1136 myri10ge_load_firmware_from_zlib(struct myri10ge_priv *mgp, uint32_t *limit)
1137 {
1138 	void *inflate_buffer;
1139 	int rv, status;
1140 	size_t sram_size = mgp->sram_size - MYRI10GE_EEPROM_STRINGS_SIZE;
1141 	size_t destlen;
1142 	mcp_gen_header_t *hdr;
1143 	unsigned hdr_offset, i;
1144 
1145 
1146 	*limit = 0; /* -Wuninitialized */
1147 	status = 0;
1148 
1149 	inflate_buffer = kmem_zalloc(sram_size, KM_NOSLEEP);
1150 	if (!inflate_buffer) {
1151 		cmn_err(CE_WARN,
1152 		    "%s: Could not allocate buffer to inflate mcp\n",
1153 		    mgp->name);
1154 		return (ENOMEM);
1155 	}
1156 
1157 	destlen = sram_size;
1158 	rv = z_uncompress(inflate_buffer, &destlen, mgp->eth_z8e,
1159 	    mgp->eth_z8e_length);
1160 
1161 	if (rv != Z_OK) {
1162 		cmn_err(CE_WARN, "%s: Could not inflate mcp: %s\n",
1163 		    mgp->name, z_strerror(rv));
1164 		status = ENXIO;
1165 		goto abort;
1166 	}
1167 
1168 	*limit = (uint32_t)destlen;
1169 
1170 	hdr_offset = htonl(*(uint32_t *)(void *)((char *)inflate_buffer +
1171 	    MCP_HEADER_PTR_OFFSET));
1172 	hdr = (void *)((char *)inflate_buffer + hdr_offset);
1173 	if (ntohl(hdr->mcp_type) != MCP_TYPE_ETH) {
1174 		cmn_err(CE_WARN, "%s: Bad firmware type: 0x%x\n", mgp->name,
1175 		    ntohl(hdr->mcp_type));
1176 		status = EIO;
1177 		goto abort;
1178 	}
1179 
1180 	/* save firmware version for kstat */
1181 	(void) strncpy(mgp->fw_version, hdr->version, sizeof (mgp->fw_version));
1182 	if (myri10ge_verbose)
1183 		printf("%s: firmware id: %s\n", mgp->name, hdr->version);
1184 
1185 	/* Copy the inflated firmware to NIC SRAM. */
1186 	for (i = 0; i < *limit; i += 256) {
1187 		myri10ge_pio_copy((char *)mgp->sram + MYRI10GE_FW_OFFSET + i,
1188 		    (char *)inflate_buffer + i,
1189 		    min(256U, (unsigned)(*limit - i)));
1190 		mb();
1191 		(void) *(int *)(void *)mgp->sram;
1192 		mb();
1193 	}
1194 
1195 abort:
1196 	kmem_free(inflate_buffer, sram_size);
1197 
1198 	return (status);
1199 
1200 }
1201 
1202 
1203 int
1204 myri10ge_send_cmd(struct myri10ge_priv *mgp, uint32_t cmd,
1205 		myri10ge_cmd_t *data)
1206 {
1207 	mcp_cmd_t *buf;
1208 	char buf_bytes[sizeof (*buf) + 8];
1209 	volatile mcp_cmd_response_t *response = mgp->cmd;
1210 	volatile char *cmd_addr =
1211 	    (volatile char *)mgp->sram + MXGEFW_ETH_CMD;
1212 	int sleep_total = 0;
1213 
1214 	/* ensure buf is aligned to 8 bytes */
1215 	buf = (mcp_cmd_t *)((unsigned long)(buf_bytes + 7) & ~7UL);
1216 
1217 	buf->data0 = htonl(data->data0);
1218 	buf->data1 = htonl(data->data1);
1219 	buf->data2 = htonl(data->data2);
1220 	buf->cmd = htonl(cmd);
1221 	buf->response_addr.low = mgp->cmd_dma.low;
1222 	buf->response_addr.high = mgp->cmd_dma.high;
1223 	mutex_enter(&mgp->cmd_lock);
1224 	response->result = 0xffffffff;
1225 	mb();
1226 
1227 	myri10ge_pio_copy((void *)cmd_addr, buf, sizeof (*buf));
1228 
1229 	/* wait up to 20ms */
1230 	for (sleep_total = 0; sleep_total < 20; sleep_total++) {
1231 		mb();
1232 		if (response->result != 0xffffffff) {
1233 			if (response->result == 0) {
1234 				data->data0 = ntohl(response->data);
1235 				mutex_exit(&mgp->cmd_lock);
1236 				return (0);
1237 			} else if (ntohl(response->result)
1238 			    == MXGEFW_CMD_UNKNOWN) {
1239 				mutex_exit(&mgp->cmd_lock);
1240 				return (ENOSYS);
1241 			} else if (ntohl(response->result)
1242 			    == MXGEFW_CMD_ERROR_UNALIGNED) {
1243 				mutex_exit(&mgp->cmd_lock);
1244 				return (E2BIG);
1245 			} else {
1246 				cmn_err(CE_WARN,
1247 				    "%s: command %d failed, result = %d\n",
1248 				    mgp->name, cmd, ntohl(response->result));
1249 				mutex_exit(&mgp->cmd_lock);
1250 				return (ENXIO);
1251 			}
1252 		}
1253 		drv_usecwait(1000);
1254 	}
1255 	mutex_exit(&mgp->cmd_lock);
1256 	cmn_err(CE_WARN, "%s: command %d timed out, result = %d\n",
1257 	    mgp->name, cmd, ntohl(response->result));
1258 	return (EAGAIN);
1259 }
1260 
1261 /*
1262  * Enable or disable periodic RDMAs from the host to make certain
1263  * chipsets resend dropped PCIe messages
1264  */
1265 
1266 static void
1267 myri10ge_dummy_rdma(struct myri10ge_priv *mgp, int enable)
1268 {
1269 	char buf_bytes[72];
1270 	volatile uint32_t *confirm;
1271 	volatile char *submit;
1272 	uint32_t *buf;
1273 	int i;
1274 
1275 	buf = (uint32_t *)((unsigned long)(buf_bytes + 7) & ~7UL);
1276 
1277 	/* clear confirmation addr */
1278 	confirm = (volatile uint32_t *)mgp->cmd;
1279 	*confirm = 0;
1280 	mb();
1281 
1282 	/*
1283 	 * send an rdma command to the PCIe engine, and wait for the
1284 	 * response in the confirmation address.  The firmware should
1285 	 *  write a -1 there to indicate it is alive and well
1286 	 */
1287 
1288 	buf[0] = mgp->cmd_dma.high;		/* confirm addr MSW */
1289 	buf[1] = mgp->cmd_dma.low;		/* confirm addr LSW */
1290 	buf[2] = htonl(0xffffffff);		/* confirm data */
1291 	buf[3] = htonl(mgp->cmd_dma.high); 	/* dummy addr MSW */
1292 	buf[4] = htonl(mgp->cmd_dma.low); 	/* dummy addr LSW */
1293 	buf[5] = htonl(enable);			/* enable? */
1294 
1295 
1296 	submit = (volatile char *)(mgp->sram + MXGEFW_BOOT_DUMMY_RDMA);
1297 
1298 	myri10ge_pio_copy((char *)submit, buf, 64);
1299 	mb();
1300 	drv_usecwait(1000);
1301 	mb();
1302 	i = 0;
1303 	while (*confirm != 0xffffffff && i < 20) {
1304 		drv_usecwait(1000);
1305 		i++;
1306 	}
1307 	if (*confirm != 0xffffffff) {
1308 		cmn_err(CE_WARN, "%s: dummy rdma %s failed (%p = 0x%x)",
1309 		    mgp->name,
1310 		    (enable ? "enable" : "disable"), (void*) confirm, *confirm);
1311 	}
1312 }
1313 
1314 static int
1315 myri10ge_load_firmware(struct myri10ge_priv *mgp)
1316 {
1317 	myri10ge_cmd_t cmd;
1318 	volatile uint32_t *confirm;
1319 	volatile char *submit;
1320 	char buf_bytes[72];
1321 	uint32_t *buf, size;
1322 	int status, i;
1323 
1324 	buf = (uint32_t *)((unsigned long)(buf_bytes + 7) & ~7UL);
1325 
1326 	status = myri10ge_load_firmware_from_zlib(mgp, &size);
1327 	if (status) {
1328 		cmn_err(CE_WARN, "%s: firmware loading failed\n", mgp->name);
1329 		return (status);
1330 	}
1331 
1332 	/* clear confirmation addr */
1333 	confirm = (volatile uint32_t *)mgp->cmd;
1334 	*confirm = 0;
1335 	mb();
1336 
1337 	/*
1338 	 * send a reload command to the bootstrap MCP, and wait for the
1339 	 * response in the confirmation address.  The firmware should
1340 	 * write a -1 there to indicate it is alive and well
1341 	 */
1342 
1343 	buf[0] = mgp->cmd_dma.high;	/* confirm addr MSW */
1344 	buf[1] = mgp->cmd_dma.low;	/* confirm addr LSW */
1345 	buf[2] = htonl(0xffffffff);	/* confirm data */
1346 
1347 	/*
1348 	 * FIX: All newest firmware should un-protect the bottom of
1349 	 * the sram before handoff. However, the very first interfaces
1350 	 * do not. Therefore the handoff copy must skip the first 8 bytes
1351 	 */
1352 	buf[3] = htonl(MYRI10GE_FW_OFFSET + 8); /* where the code starts */
1353 	buf[4] = htonl(size - 8); 	/* length of code */
1354 	buf[5] = htonl(8);		/* where to copy to */
1355 	buf[6] = htonl(0);		/* where to jump to */
1356 
1357 	submit = (volatile char *)(mgp->sram + MXGEFW_BOOT_HANDOFF);
1358 
1359 	myri10ge_pio_copy((char *)submit, buf, 64);
1360 	mb();
1361 	drv_usecwait(1000);
1362 	mb();
1363 	i = 0;
1364 	while (*confirm != 0xffffffff && i < 1000) {
1365 		drv_usecwait(1000);
1366 		i++;
1367 	}
1368 	if (*confirm != 0xffffffff) {
1369 		cmn_err(CE_WARN, "%s: handoff failed (%p = 0x%x)",
1370 		    mgp->name, (void *) confirm, *confirm);
1371 
1372 		return (ENXIO);
1373 	}
1374 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd);
1375 	if (status != 0) {
1376 		cmn_err(CE_WARN, "%s: failed MXGEFW_CMD_GET_RX_RING_SIZE\n",
1377 		    mgp->name);
1378 		return (ENXIO);
1379 	}
1380 
1381 	mgp->max_intr_slots = 2 * (cmd.data0 / sizeof (mcp_dma_addr_t));
1382 	myri10ge_dummy_rdma(mgp, 1);
1383 	return (0);
1384 }
1385 
1386 static int
1387 myri10ge_m_unicst(void *arg, const uint8_t *addr)
1388 {
1389 	struct myri10ge_priv *mgp = arg;
1390 	myri10ge_cmd_t cmd;
1391 	int status;
1392 
1393 	cmd.data0 = ((addr[0] << 24) | (addr[1] << 16)
1394 	    | (addr[2] << 8) | addr[3]);
1395 
1396 	cmd.data1 = ((addr[4] << 8) | (addr[5]));
1397 
1398 	status = myri10ge_send_cmd(mgp, MXGEFW_SET_MAC_ADDRESS, &cmd);
1399 	if (status == 0 && (addr != mgp->mac_addr))
1400 		(void) memcpy(mgp->mac_addr, addr, sizeof (mgp->mac_addr));
1401 
1402 	return (status);
1403 }
1404 
1405 static int
1406 myri10ge_change_pause(struct myri10ge_priv *mgp, int pause)
1407 {
1408 	myri10ge_cmd_t cmd;
1409 	int status;
1410 
1411 	if (pause)
1412 		status = myri10ge_send_cmd(mgp, MXGEFW_ENABLE_FLOW_CONTROL,
1413 		    &cmd);
1414 	else
1415 		status = myri10ge_send_cmd(mgp, MXGEFW_DISABLE_FLOW_CONTROL,
1416 		    &cmd);
1417 
1418 	if (status) {
1419 		cmn_err(CE_WARN, "%s: Failed to set flow control mode\n",
1420 		    mgp->name);
1421 		return (ENXIO);
1422 	}
1423 	mgp->pause = pause;
1424 	return (0);
1425 }
1426 
1427 static void
1428 myri10ge_change_promisc(struct myri10ge_priv *mgp, int promisc)
1429 {
1430 	myri10ge_cmd_t cmd;
1431 	int status;
1432 
1433 	if (promisc)
1434 		status = myri10ge_send_cmd(mgp, MXGEFW_ENABLE_PROMISC, &cmd);
1435 	else
1436 		status = myri10ge_send_cmd(mgp, MXGEFW_DISABLE_PROMISC, &cmd);
1437 
1438 	if (status) {
1439 		cmn_err(CE_WARN, "%s: Failed to set promisc mode\n",
1440 		    mgp->name);
1441 	}
1442 }
1443 
1444 static int
1445 myri10ge_dma_test(struct myri10ge_priv *mgp, int test_type)
1446 {
1447 	myri10ge_cmd_t cmd;
1448 	int status;
1449 	uint32_t len;
1450 	void *dmabench;
1451 	struct myri10ge_dma_stuff dmabench_dma;
1452 	char *test = " ";
1453 
1454 	/*
1455 	 * Run a small DMA test.
1456 	 * The magic multipliers to the length tell the firmware
1457 	 * tp do DMA read, write, or read+write tests.  The
1458 	 * results are returned in cmd.data0.  The upper 16
1459 	 * bits or the return is the number of transfers completed.
1460 	 * The lower 16 bits is the time in 0.5us ticks that the
1461 	 * transfers took to complete
1462 	 */
1463 
1464 	len = mgp->tx_boundary;
1465 
1466 	dmabench = myri10ge_dma_alloc(mgp->dip, len,
1467 	    &myri10ge_rx_jumbo_dma_attr, &myri10ge_dev_access_attr,
1468 	    DDI_DMA_STREAMING,  DDI_DMA_RDWR|DDI_DMA_STREAMING,
1469 	    &dmabench_dma, 1, DDI_DMA_DONTWAIT);
1470 	mgp->read_dma = mgp->write_dma = mgp->read_write_dma = 0;
1471 	if (dmabench == NULL) {
1472 		cmn_err(CE_WARN, "%s dma benchmark aborted\n", mgp->name);
1473 		return (ENOMEM);
1474 	}
1475 
1476 	cmd.data0 = ntohl(dmabench_dma.low);
1477 	cmd.data1 = ntohl(dmabench_dma.high);
1478 	cmd.data2 = len * 0x10000;
1479 	status = myri10ge_send_cmd(mgp, test_type, &cmd);
1480 	if (status != 0) {
1481 		test = "read";
1482 		goto abort;
1483 	}
1484 	mgp->read_dma = ((cmd.data0>>16) * len * 2) / (cmd.data0 & 0xffff);
1485 
1486 	cmd.data0 = ntohl(dmabench_dma.low);
1487 	cmd.data1 = ntohl(dmabench_dma.high);
1488 	cmd.data2 = len * 0x1;
1489 	status = myri10ge_send_cmd(mgp, test_type, &cmd);
1490 	if (status != 0) {
1491 		test = "write";
1492 		goto abort;
1493 	}
1494 	mgp->write_dma = ((cmd.data0>>16) * len * 2) / (cmd.data0 & 0xffff);
1495 
1496 	cmd.data0 = ntohl(dmabench_dma.low);
1497 	cmd.data1 = ntohl(dmabench_dma.high);
1498 	cmd.data2 = len * 0x10001;
1499 	status = myri10ge_send_cmd(mgp, test_type, &cmd);
1500 	if (status != 0) {
1501 		test = "read/write";
1502 		goto abort;
1503 	}
1504 	mgp->read_write_dma = ((cmd.data0>>16) * len * 2 * 2) /
1505 	    (cmd.data0 & 0xffff);
1506 
1507 
1508 abort:
1509 	myri10ge_dma_free(&dmabench_dma);
1510 	if (status != 0 && test_type != MXGEFW_CMD_UNALIGNED_TEST)
1511 		cmn_err(CE_WARN, "%s %s dma benchmark failed\n", mgp->name,
1512 		    test);
1513 	return (status);
1514 }
1515 
1516 static int
1517 myri10ge_reset(struct myri10ge_priv *mgp)
1518 {
1519 	myri10ge_cmd_t cmd;
1520 	struct myri10ge_nic_stat *ethstat;
1521 	struct myri10ge_slice_state *ss;
1522 	int i, status;
1523 	size_t bytes;
1524 
1525 	/* send a reset command to the card to see if it is alive */
1526 	(void) memset(&cmd, 0, sizeof (cmd));
1527 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_RESET, &cmd);
1528 	if (status != 0) {
1529 		cmn_err(CE_WARN, "%s: failed reset\n", mgp->name);
1530 		return (ENXIO);
1531 	}
1532 
1533 	/* Now exchange information about interrupts  */
1534 
1535 	bytes = mgp->max_intr_slots * sizeof (*mgp->ss[0].rx_done.entry);
1536 	cmd.data0 = (uint32_t)bytes;
1537 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd);
1538 
1539 	/*
1540 	 * Even though we already know how many slices are supported
1541 	 * via myri10ge_probe_slices() MXGEFW_CMD_GET_MAX_RSS_QUEUES
1542 	 * has magic side effects, and must be called after a reset.
1543 	 * It must be called prior to calling any RSS related cmds,
1544 	 * including assigning an interrupt queue for anything but
1545 	 * slice 0.  It must also be called *after*
1546 	 * MXGEFW_CMD_SET_INTRQ_SIZE, since the intrq size is used by
1547 	 * the firmware to compute offsets.
1548 	 */
1549 
1550 	if (mgp->num_slices > 1) {
1551 
1552 		/* ask the maximum number of slices it supports */
1553 		status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_RSS_QUEUES,
1554 		    &cmd);
1555 		if (status != 0) {
1556 			cmn_err(CE_WARN,
1557 			    "%s: failed to get number of slices\n",
1558 			    mgp->name);
1559 			return (status);
1560 		}
1561 
1562 		/*
1563 		 * MXGEFW_CMD_ENABLE_RSS_QUEUES must be called prior
1564 		 * to setting up the interrupt queue DMA
1565 		 */
1566 
1567 		cmd.data0 = mgp->num_slices;
1568 		cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE |
1569 		    MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES;
1570 		status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ENABLE_RSS_QUEUES,
1571 		    &cmd);
1572 		if (status != 0) {
1573 			cmn_err(CE_WARN,
1574 			    "%s: failed to set number of slices\n",
1575 			    mgp->name);
1576 			return (status);
1577 		}
1578 	}
1579 	for (i = 0; i < mgp->num_slices; i++) {
1580 		ss = &mgp->ss[i];
1581 		cmd.data0 = ntohl(ss->rx_done.dma.low);
1582 		cmd.data1 = ntohl(ss->rx_done.dma.high);
1583 		cmd.data2 = i;
1584 		status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_DMA,
1585 		    &cmd);
1586 	};
1587 
1588 	status |= myri10ge_send_cmd(mgp,  MXGEFW_CMD_GET_IRQ_ACK_OFFSET, &cmd);
1589 	for (i = 0; i < mgp->num_slices; i++) {
1590 		ss = &mgp->ss[i];
1591 		ss->irq_claim = (volatile unsigned int *)
1592 		    (void *)(mgp->sram + cmd.data0 + 8 * i);
1593 	}
1594 
1595 	if (mgp->ddi_intr_type == DDI_INTR_TYPE_FIXED) {
1596 		status |= myri10ge_send_cmd(mgp,
1597 		    MXGEFW_CMD_GET_IRQ_DEASSERT_OFFSET, &cmd);
1598 		mgp->irq_deassert = (uint32_t *)(void *)(mgp->sram + cmd.data0);
1599 	}
1600 
1601 	status |= myri10ge_send_cmd(mgp,
1602 	    MXGEFW_CMD_GET_INTR_COAL_DELAY_OFFSET, &cmd);
1603 	mgp->intr_coal_delay_ptr = (uint32_t *)(void *)(mgp->sram + cmd.data0);
1604 
1605 	if (status != 0) {
1606 		cmn_err(CE_WARN, "%s: failed set interrupt parameters\n",
1607 		    mgp->name);
1608 		return (status);
1609 	}
1610 
1611 	*mgp->intr_coal_delay_ptr = htonl(mgp->intr_coal_delay);
1612 	(void) myri10ge_dma_test(mgp, MXGEFW_DMA_TEST);
1613 
1614 	/* reset mcp/driver shared state back to 0 */
1615 
1616 	for (i = 0; i < mgp->num_slices; i++) {
1617 		ss = &mgp->ss[i];
1618 		bytes = mgp->max_intr_slots *
1619 		    sizeof (*mgp->ss[0].rx_done.entry);
1620 		(void) memset(ss->rx_done.entry, 0, bytes);
1621 		ss->tx.req = 0;
1622 		ss->tx.done = 0;
1623 		ss->tx.pkt_done = 0;
1624 		ss->rx_big.cnt = 0;
1625 		ss->rx_small.cnt = 0;
1626 		ss->rx_done.idx = 0;
1627 		ss->rx_done.cnt = 0;
1628 		ss->rx_token = 0;
1629 		ss->tx.watchdog_done = 0;
1630 		ss->tx.watchdog_req = 0;
1631 		ss->tx.active = 0;
1632 		ss->tx.activate = 0;
1633 	}
1634 	mgp->watchdog_rx_pause = 0;
1635 	if (mgp->ksp_stat != NULL) {
1636 		ethstat = (struct myri10ge_nic_stat *)mgp->ksp_stat->ks_data;
1637 		ethstat->link_changes.value.ul = 0;
1638 	}
1639 	status = myri10ge_m_unicst(mgp, mgp->mac_addr);
1640 	myri10ge_change_promisc(mgp, 0);
1641 	(void) myri10ge_change_pause(mgp, mgp->pause);
1642 	return (status);
1643 }
1644 
1645 static int
1646 myri10ge_init_toeplitz(struct myri10ge_priv *mgp)
1647 {
1648 	myri10ge_cmd_t cmd;
1649 	int i, b, s, t, j;
1650 	int status;
1651 	uint32_t k[8];
1652 	uint32_t tmp;
1653 	uint8_t *key;
1654 
1655 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RSS_KEY_OFFSET,
1656 	    &cmd);
1657 	if (status != 0) {
1658 		cmn_err(CE_WARN, "%s: failed to get rss key\n",
1659 		    mgp->name);
1660 		return (EIO);
1661 	}
1662 	myri10ge_pio_copy32(mgp->rss_key,
1663 	    (uint32_t *)(void*)((char *)mgp->sram + cmd.data0),
1664 	    sizeof (mgp->rss_key));
1665 
1666 	mgp->toeplitz_hash_table = kmem_alloc(sizeof (uint32_t) * 12 * 256,
1667 	    KM_SLEEP);
1668 	key = (uint8_t *)mgp->rss_key;
1669 	t = 0;
1670 	for (b = 0; b < 12; b++) {
1671 		for (s = 0; s < 8; s++) {
1672 			/* Bits: b*8+s, ..., b*8+s+31 */
1673 			k[s] = 0;
1674 			for (j = 0; j < 32; j++) {
1675 				int bit = b*8+s+j;
1676 				bit = 0x1 & (key[bit / 8] >> (7 -(bit & 0x7)));
1677 				k[s] |= bit << (31 - j);
1678 			}
1679 		}
1680 
1681 		for (i = 0; i <= 0xff; i++) {
1682 			tmp = 0;
1683 			if (i & (1 << 7)) { tmp ^= k[0]; }
1684 			if (i & (1 << 6)) { tmp ^= k[1]; }
1685 			if (i & (1 << 5)) { tmp ^= k[2]; }
1686 			if (i & (1 << 4)) { tmp ^= k[3]; }
1687 			if (i & (1 << 3)) { tmp ^= k[4]; }
1688 			if (i & (1 << 2)) { tmp ^= k[5]; }
1689 			if (i & (1 << 1)) { tmp ^= k[6]; }
1690 			if (i & (1 << 0)) { tmp ^= k[7]; }
1691 			mgp->toeplitz_hash_table[t++] = tmp;
1692 		}
1693 	}
1694 	return (0);
1695 }
1696 
1697 static inline struct myri10ge_slice_state *
1698 myri10ge_toeplitz_send_hash(struct myri10ge_priv *mgp, struct ip *ip)
1699 {
1700 	struct tcphdr *hdr;
1701 	uint32_t saddr, daddr;
1702 	uint32_t hash, slice;
1703 	uint32_t *table = mgp->toeplitz_hash_table;
1704 	uint16_t src, dst;
1705 
1706 	/*
1707 	 * Note hashing order is reversed from how it is done
1708 	 * in the NIC, so as to generate the same hash value
1709 	 * for the connection to try to keep connections CPU local
1710 	 */
1711 
1712 	/* hash on IPv4 src/dst address */
1713 	saddr = ntohl(ip->ip_src.s_addr);
1714 	daddr = ntohl(ip->ip_dst.s_addr);
1715 	hash = table[(256 * 0) + ((daddr >> 24) & 0xff)];
1716 	hash ^= table[(256 * 1) + ((daddr >> 16) & 0xff)];
1717 	hash ^= table[(256 * 2) + ((daddr >> 8) & 0xff)];
1718 	hash ^= table[(256 * 3) + ((daddr) & 0xff)];
1719 	hash ^= table[(256 * 4) + ((saddr >> 24) & 0xff)];
1720 	hash ^= table[(256 * 5) + ((saddr >> 16) & 0xff)];
1721 	hash ^= table[(256 * 6) + ((saddr >> 8) & 0xff)];
1722 	hash ^= table[(256 * 7) + ((saddr) & 0xff)];
1723 	/* hash on TCP port, if required */
1724 	if ((myri10ge_rss_hash & MXGEFW_RSS_HASH_TYPE_TCP_IPV4) &&
1725 	    ip->ip_p == IPPROTO_TCP) {
1726 		hdr = (struct tcphdr *)(void *)
1727 		    (((uint8_t *)ip) +  (ip->ip_hl << 2));
1728 		src = ntohs(hdr->th_sport);
1729 		dst = ntohs(hdr->th_dport);
1730 
1731 		hash ^= table[(256 * 8) + ((dst >> 8) & 0xff)];
1732 		hash ^= table[(256 * 9) + ((dst) & 0xff)];
1733 		hash ^= table[(256 * 10) + ((src >> 8) & 0xff)];
1734 		hash ^= table[(256 * 11) + ((src) & 0xff)];
1735 	}
1736 	slice = (mgp->num_slices - 1) & hash;
1737 	return (&mgp->ss[slice]);
1738 
1739 }
1740 
1741 static inline struct myri10ge_slice_state *
1742 myri10ge_simple_send_hash(struct myri10ge_priv *mgp, struct ip *ip)
1743 {
1744 	struct tcphdr *hdr;
1745 	uint32_t slice, hash_val;
1746 
1747 
1748 	if (ip->ip_p != IPPROTO_TCP && ip->ip_p != IPPROTO_UDP) {
1749 		return (&mgp->ss[0]);
1750 	}
1751 	hdr = (struct tcphdr *)(void *)(((uint8_t *)ip) +  (ip->ip_hl << 2));
1752 
1753 	/*
1754 	 * Use the second byte of the *destination* address for
1755 	 * MXGEFW_RSS_HASH_TYPE_SRC_PORT, so as to match NIC's hashing
1756 	 */
1757 	hash_val = ntohs(hdr->th_dport) & 0xff;
1758 	if (myri10ge_rss_hash == MXGEFW_RSS_HASH_TYPE_SRC_DST_PORT)
1759 		hash_val += ntohs(hdr->th_sport) & 0xff;
1760 
1761 	slice = (mgp->num_slices - 1) & hash_val;
1762 	return (&mgp->ss[slice]);
1763 }
1764 
1765 static inline struct myri10ge_slice_state *
1766 myri10ge_send_hash(struct myri10ge_priv *mgp, mblk_t *mp)
1767 {
1768 	unsigned int slice = 0;
1769 	struct ether_header *eh;
1770 	struct ether_vlan_header *vh;
1771 	struct ip *ip;
1772 	int ehl, ihl;
1773 
1774 	if (mgp->num_slices == 1)
1775 		return (&mgp->ss[0]);
1776 
1777 	if (myri10ge_tx_hash == 0) {
1778 		slice = CPU->cpu_id & (mgp->num_slices - 1);
1779 		return (&mgp->ss[slice]);
1780 	}
1781 
1782 	/*
1783 	 *  ensure it is a TCP or UDP over IPv4 packet, and that the
1784 	 *  headers are in the 1st mblk.  Otherwise, punt
1785 	 */
1786 	ehl = sizeof (*eh);
1787 	ihl = sizeof (*ip);
1788 	if ((MBLKL(mp)) <  (ehl + ihl + 8))
1789 		return (&mgp->ss[0]);
1790 	eh = (struct ether_header *)(void *)mp->b_rptr;
1791 	ip = (struct ip *)(void *)(eh + 1);
1792 	if (eh->ether_type != BE_16(ETHERTYPE_IP)) {
1793 		if (eh->ether_type != BE_16(ETHERTYPE_VLAN))
1794 			return (&mgp->ss[0]);
1795 		vh = (struct ether_vlan_header *)(void *)mp->b_rptr;
1796 		if (vh->ether_type != BE_16(ETHERTYPE_IP))
1797 			return (&mgp->ss[0]);
1798 		ehl += 4;
1799 		ip = (struct ip *)(void *)(vh + 1);
1800 	}
1801 	ihl = ip->ip_hl << 2;
1802 	if (MBLKL(mp) <  (ehl + ihl + 8))
1803 		return (&mgp->ss[0]);
1804 	switch (myri10ge_rss_hash) {
1805 	case MXGEFW_RSS_HASH_TYPE_IPV4:
1806 		/* fallthru */
1807 	case MXGEFW_RSS_HASH_TYPE_TCP_IPV4:
1808 		/* fallthru */
1809 	case (MXGEFW_RSS_HASH_TYPE_IPV4|MXGEFW_RSS_HASH_TYPE_TCP_IPV4):
1810 		return (myri10ge_toeplitz_send_hash(mgp, ip));
1811 	case MXGEFW_RSS_HASH_TYPE_SRC_PORT:
1812 		/* fallthru */
1813 	case MXGEFW_RSS_HASH_TYPE_SRC_DST_PORT:
1814 		return (myri10ge_simple_send_hash(mgp, ip));
1815 	default:
1816 		break;
1817 	}
1818 	return (&mgp->ss[0]);
1819 }
1820 
1821 static int
1822 myri10ge_setup_slice(struct myri10ge_slice_state *ss)
1823 {
1824 	struct myri10ge_priv *mgp = ss->mgp;
1825 	myri10ge_cmd_t cmd;
1826 	int tx_ring_size, rx_ring_size;
1827 	int tx_ring_entries, rx_ring_entries;
1828 	int slice, status;
1829 	int allocated, idx;
1830 	size_t bytes;
1831 
1832 	slice = ss - mgp->ss;
1833 	cmd.data0 = slice;
1834 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_RING_SIZE, &cmd);
1835 	tx_ring_size = cmd.data0;
1836 	cmd.data0 = slice;
1837 	status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd);
1838 	if (status != 0)
1839 		return (status);
1840 	rx_ring_size = cmd.data0;
1841 
1842 	tx_ring_entries = tx_ring_size / sizeof (struct mcp_kreq_ether_send);
1843 	rx_ring_entries = rx_ring_size / sizeof (struct mcp_dma_addr);
1844 	ss->tx.mask = tx_ring_entries - 1;
1845 	ss->rx_small.mask = ss->rx_big.mask = rx_ring_entries - 1;
1846 
1847 	/* get the lanai pointers to the send and receive rings */
1848 
1849 	cmd.data0 = slice;
1850 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_OFFSET, &cmd);
1851 	ss->tx.lanai = (mcp_kreq_ether_send_t *)(void *)(mgp->sram + cmd.data0);
1852 	if (mgp->num_slices > 1) {
1853 		ss->tx.go = (char *)mgp->sram + MXGEFW_ETH_SEND_GO + 64 * slice;
1854 		ss->tx.stop = (char *)mgp->sram + MXGEFW_ETH_SEND_STOP +
1855 		    64 * slice;
1856 	} else {
1857 		ss->tx.go = NULL;
1858 		ss->tx.stop = NULL;
1859 	}
1860 
1861 	cmd.data0 = slice;
1862 	status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SMALL_RX_OFFSET, &cmd);
1863 	ss->rx_small.lanai = (mcp_kreq_ether_recv_t *)
1864 	    (void *)(mgp->sram + cmd.data0);
1865 
1866 	cmd.data0 = slice;
1867 	status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_BIG_RX_OFFSET, &cmd);
1868 	ss->rx_big.lanai = (mcp_kreq_ether_recv_t *)(void *)
1869 	    (mgp->sram + cmd.data0);
1870 
1871 	if (status != 0) {
1872 		cmn_err(CE_WARN,
1873 		    "%s: failed to get ring sizes or locations\n", mgp->name);
1874 		return (status);
1875 	}
1876 
1877 	status = ENOMEM;
1878 	bytes = rx_ring_entries * sizeof (*ss->rx_small.shadow);
1879 	ss->rx_small.shadow = kmem_zalloc(bytes, KM_SLEEP);
1880 	if (ss->rx_small.shadow == NULL)
1881 		goto abort;
1882 	(void) memset(ss->rx_small.shadow, 0, bytes);
1883 
1884 	bytes = rx_ring_entries * sizeof (*ss->rx_big.shadow);
1885 	ss->rx_big.shadow = kmem_zalloc(bytes, KM_SLEEP);
1886 	if (ss->rx_big.shadow == NULL)
1887 		goto abort_with_rx_small_shadow;
1888 	(void) memset(ss->rx_big.shadow, 0, bytes);
1889 
1890 	/* allocate the host info rings */
1891 
1892 	bytes = tx_ring_entries * sizeof (*ss->tx.info);
1893 	ss->tx.info = kmem_zalloc(bytes, KM_SLEEP);
1894 	if (ss->tx.info == NULL)
1895 		goto abort_with_rx_big_shadow;
1896 	(void) memset(ss->tx.info, 0, bytes);
1897 
1898 	bytes = rx_ring_entries * sizeof (*ss->rx_small.info);
1899 	ss->rx_small.info = kmem_zalloc(bytes, KM_SLEEP);
1900 	if (ss->rx_small.info == NULL)
1901 		goto abort_with_tx_info;
1902 	(void) memset(ss->rx_small.info, 0, bytes);
1903 
1904 	bytes = rx_ring_entries * sizeof (*ss->rx_big.info);
1905 	ss->rx_big.info = kmem_zalloc(bytes, KM_SLEEP);
1906 	if (ss->rx_big.info == NULL)
1907 		goto abort_with_rx_small_info;
1908 	(void) memset(ss->rx_big.info, 0, bytes);
1909 
1910 	ss->tx.stall = ss->tx.sched = 0;
1911 	ss->tx.stall_early = ss->tx.stall_late = 0;
1912 
1913 	ss->jbufs_for_smalls = 1 + (1 + ss->rx_small.mask) /
1914 	    (myri10ge_mtu / (myri10ge_small_bytes + MXGEFW_PAD));
1915 
1916 	allocated = myri10ge_add_jbufs(ss,
1917 	    myri10ge_bigbufs_initial + ss->jbufs_for_smalls, 1);
1918 	if (allocated < ss->jbufs_for_smalls + myri10ge_bigbufs_initial) {
1919 		cmn_err(CE_WARN,
1920 		    "%s: Could not allocate enough receive buffers (%d/%d)\n",
1921 		    mgp->name, allocated,
1922 		    myri10ge_bigbufs_initial + ss->jbufs_for_smalls);
1923 		goto abort_with_jumbos;
1924 	}
1925 
1926 	myri10ge_carve_up_jbufs_into_small_ring(ss);
1927 	ss->j_rx_cnt = 0;
1928 
1929 	mutex_enter(&ss->jpool.mtx);
1930 	if (allocated < rx_ring_entries)
1931 		ss->jpool.low_water = allocated / 4;
1932 	else
1933 		ss->jpool.low_water = rx_ring_entries / 2;
1934 
1935 	/*
1936 	 * invalidate the big receive ring in case we do not
1937 	 * allocate sufficient jumbos to fill it
1938 	 */
1939 	(void) memset(ss->rx_big.shadow, 1,
1940 	    (ss->rx_big.mask + 1) * sizeof (ss->rx_big.shadow[0]));
1941 	for (idx = 7; idx <= ss->rx_big.mask; idx += 8) {
1942 		myri10ge_submit_8rx(&ss->rx_big.lanai[idx - 7],
1943 		    &ss->rx_big.shadow[idx - 7]);
1944 		mb();
1945 	}
1946 
1947 
1948 	myri10ge_restock_jumbos(ss);
1949 
1950 	for (idx = 7; idx <= ss->rx_small.mask; idx += 8) {
1951 		myri10ge_submit_8rx(&ss->rx_small.lanai[idx - 7],
1952 		    &ss->rx_small.shadow[idx - 7]);
1953 		mb();
1954 	}
1955 	ss->rx_small.cnt = ss->rx_small.mask + 1;
1956 
1957 	mutex_exit(&ss->jpool.mtx);
1958 
1959 	status = myri10ge_prepare_tx_ring(ss);
1960 
1961 	if (status != 0)
1962 		goto abort_with_small_jbufs;
1963 
1964 	cmd.data0 = ntohl(ss->fw_stats_dma.low);
1965 	cmd.data1 = ntohl(ss->fw_stats_dma.high);
1966 	cmd.data2 = sizeof (mcp_irq_data_t);
1967 	cmd.data2 |= (slice << 16);
1968 	bzero(ss->fw_stats, sizeof (*ss->fw_stats));
1969 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_STATS_DMA_V2, &cmd);
1970 	if (status == ENOSYS) {
1971 		cmd.data0 = ntohl(ss->fw_stats_dma.low) +
1972 		    offsetof(mcp_irq_data_t, send_done_count);
1973 		cmd.data1 = ntohl(ss->fw_stats_dma.high);
1974 		status = myri10ge_send_cmd(mgp,
1975 		    MXGEFW_CMD_SET_STATS_DMA_OBSOLETE, &cmd);
1976 	}
1977 	if (status) {
1978 		cmn_err(CE_WARN, "%s: Couldn't set stats DMA\n", mgp->name);
1979 		goto abort_with_tx;
1980 	}
1981 
1982 	return (0);
1983 
1984 abort_with_tx:
1985 	myri10ge_unprepare_tx_ring(ss);
1986 
1987 abort_with_small_jbufs:
1988 	myri10ge_release_small_jbufs(ss);
1989 
1990 abort_with_jumbos:
1991 	if (allocated != 0) {
1992 		mutex_enter(&ss->jpool.mtx);
1993 		ss->jpool.low_water = 0;
1994 		mutex_exit(&ss->jpool.mtx);
1995 		myri10ge_unstock_jumbos(ss);
1996 		myri10ge_remove_jbufs(ss);
1997 	}
1998 
1999 	bytes = rx_ring_entries * sizeof (*ss->rx_big.info);
2000 	kmem_free(ss->rx_big.info, bytes);
2001 
2002 abort_with_rx_small_info:
2003 	bytes = rx_ring_entries * sizeof (*ss->rx_small.info);
2004 	kmem_free(ss->rx_small.info, bytes);
2005 
2006 abort_with_tx_info:
2007 	bytes = tx_ring_entries * sizeof (*ss->tx.info);
2008 	kmem_free(ss->tx.info, bytes);
2009 
2010 abort_with_rx_big_shadow:
2011 	bytes = rx_ring_entries * sizeof (*ss->rx_big.shadow);
2012 	kmem_free(ss->rx_big.shadow, bytes);
2013 
2014 abort_with_rx_small_shadow:
2015 	bytes = rx_ring_entries * sizeof (*ss->rx_small.shadow);
2016 	kmem_free(ss->rx_small.shadow, bytes);
2017 abort:
2018 	return (status);
2019 
2020 }
2021 
2022 static void
2023 myri10ge_teardown_slice(struct myri10ge_slice_state *ss)
2024 {
2025 	int tx_ring_entries, rx_ring_entries;
2026 	size_t bytes;
2027 
2028 	/* ignore slices that have not been fully setup */
2029 	if (ss->tx.cp == NULL)
2030 		return;
2031 	/* Free the TX copy buffers */
2032 	myri10ge_unprepare_tx_ring(ss);
2033 
2034 	/* stop passing returned buffers to firmware */
2035 
2036 	mutex_enter(&ss->jpool.mtx);
2037 	ss->jpool.low_water = 0;
2038 	mutex_exit(&ss->jpool.mtx);
2039 	myri10ge_release_small_jbufs(ss);
2040 
2041 	/* Release the free jumbo frame pool */
2042 	myri10ge_unstock_jumbos(ss);
2043 	myri10ge_remove_jbufs(ss);
2044 
2045 	rx_ring_entries = ss->rx_big.mask + 1;
2046 	tx_ring_entries = ss->tx.mask + 1;
2047 
2048 	bytes = rx_ring_entries * sizeof (*ss->rx_big.info);
2049 	kmem_free(ss->rx_big.info, bytes);
2050 
2051 	bytes = rx_ring_entries * sizeof (*ss->rx_small.info);
2052 	kmem_free(ss->rx_small.info, bytes);
2053 
2054 	bytes = tx_ring_entries * sizeof (*ss->tx.info);
2055 	kmem_free(ss->tx.info, bytes);
2056 
2057 	bytes = rx_ring_entries * sizeof (*ss->rx_big.shadow);
2058 	kmem_free(ss->rx_big.shadow, bytes);
2059 
2060 	bytes = rx_ring_entries * sizeof (*ss->rx_small.shadow);
2061 	kmem_free(ss->rx_small.shadow, bytes);
2062 
2063 }
2064 static int
2065 myri10ge_start_locked(struct myri10ge_priv *mgp)
2066 {
2067 	myri10ge_cmd_t cmd;
2068 	int status, big_pow2, i;
2069 	volatile uint8_t *itable;
2070 
2071 	status = DDI_SUCCESS;
2072 	/* Allocate DMA resources and receive buffers */
2073 
2074 	status = myri10ge_reset(mgp);
2075 	if (status != 0) {
2076 		cmn_err(CE_WARN, "%s: failed reset\n", mgp->name);
2077 		return (DDI_FAILURE);
2078 	}
2079 
2080 	if (mgp->num_slices > 1) {
2081 		cmd.data0 = mgp->num_slices;
2082 		cmd.data1 = 1; /* use MSI-X */
2083 		status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ENABLE_RSS_QUEUES,
2084 		    &cmd);
2085 		if (status != 0) {
2086 			cmn_err(CE_WARN,
2087 			    "%s: failed to set number of slices\n",
2088 			    mgp->name);
2089 			goto abort_with_nothing;
2090 		}
2091 		/* setup the indirection table */
2092 		cmd.data0 = mgp->num_slices;
2093 		status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_TABLE_SIZE,
2094 		    &cmd);
2095 
2096 		status |= myri10ge_send_cmd(mgp,
2097 		    MXGEFW_CMD_GET_RSS_TABLE_OFFSET, &cmd);
2098 		if (status != 0) {
2099 			cmn_err(CE_WARN,
2100 			    "%s: failed to setup rss tables\n", mgp->name);
2101 		}
2102 
2103 		/* just enable an identity mapping */
2104 		itable = mgp->sram + cmd.data0;
2105 		for (i = 0; i < mgp->num_slices; i++)
2106 			itable[i] = (uint8_t)i;
2107 
2108 		if (myri10ge_rss_hash & MYRI10GE_TOEPLITZ_HASH) {
2109 			status = myri10ge_init_toeplitz(mgp);
2110 			if (status != 0) {
2111 				cmn_err(CE_WARN, "%s: failed to setup "
2112 				    "toeplitz tx hash table", mgp->name);
2113 				goto abort_with_nothing;
2114 			}
2115 		}
2116 		cmd.data0 = 1;
2117 		cmd.data1 = myri10ge_rss_hash;
2118 		status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_ENABLE,
2119 		    &cmd);
2120 		if (status != 0) {
2121 			cmn_err(CE_WARN,
2122 			    "%s: failed to enable slices\n", mgp->name);
2123 			goto abort_with_toeplitz;
2124 		}
2125 	}
2126 
2127 	for (i = 0; i < mgp->num_slices; i++) {
2128 		status = myri10ge_setup_slice(&mgp->ss[i]);
2129 		if (status != 0)
2130 			goto abort_with_slices;
2131 	}
2132 
2133 	/*
2134 	 * Tell the MCP how many buffers he has, and to
2135 	 *  bring the ethernet interface up
2136 	 *
2137 	 * Firmware needs the big buff size as a power of 2.  Lie and
2138 	 * tell him the buffer is larger, because we only use 1
2139 	 * buffer/pkt, and the mtu will prevent overruns
2140 	 */
2141 	big_pow2 = myri10ge_mtu + MXGEFW_PAD;
2142 	while ((big_pow2 & (big_pow2 - 1)) != 0)
2143 		big_pow2++;
2144 
2145 	/* now give firmware buffers sizes, and MTU */
2146 	cmd.data0 = myri10ge_mtu;
2147 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_MTU, &cmd);
2148 	cmd.data0 = myri10ge_small_bytes;
2149 	status |=
2150 	    myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_SMALL_BUFFER_SIZE, &cmd);
2151 	cmd.data0 = big_pow2;
2152 	status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_BIG_BUFFER_SIZE, &cmd);
2153 	if (status) {
2154 		cmn_err(CE_WARN, "%s: Couldn't set buffer sizes\n", mgp->name);
2155 		goto abort_with_slices;
2156 	}
2157 
2158 
2159 	cmd.data0 = 1;
2160 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_TSO_MODE, &cmd);
2161 	if (status) {
2162 		cmn_err(CE_WARN, "%s: unable to setup TSO (%d)\n",
2163 		    mgp->name, status);
2164 	} else {
2165 		mgp->features |= MYRI10GE_TSO;
2166 	}
2167 
2168 	mgp->link_state = -1;
2169 	mgp->rdma_tags_available = 15;
2170 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_UP, &cmd);
2171 	if (status) {
2172 		cmn_err(CE_WARN, "%s: unable to start ethernet\n", mgp->name);
2173 		goto abort_with_slices;
2174 	}
2175 	mgp->running = MYRI10GE_ETH_RUNNING;
2176 	return (DDI_SUCCESS);
2177 
2178 abort_with_slices:
2179 	for (i = 0; i < mgp->num_slices; i++)
2180 		myri10ge_teardown_slice(&mgp->ss[i]);
2181 
2182 	mgp->running = MYRI10GE_ETH_STOPPED;
2183 
2184 abort_with_toeplitz:
2185 	if (mgp->toeplitz_hash_table != NULL) {
2186 		kmem_free(mgp->toeplitz_hash_table,
2187 		    sizeof (uint32_t) * 12 * 256);
2188 		mgp->toeplitz_hash_table = NULL;
2189 	}
2190 
2191 abort_with_nothing:
2192 	return (DDI_FAILURE);
2193 }
2194 
2195 static void
2196 myri10ge_stop_locked(struct myri10ge_priv *mgp)
2197 {
2198 	int status, old_down_cnt;
2199 	myri10ge_cmd_t cmd;
2200 	int wait_time = 10;
2201 	int i, polling;
2202 
2203 	old_down_cnt = mgp->down_cnt;
2204 	mb();
2205 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_DOWN, &cmd);
2206 	if (status) {
2207 		cmn_err(CE_WARN, "%s: Couldn't bring down link\n", mgp->name);
2208 	}
2209 
2210 	while (old_down_cnt == *((volatile int *)&mgp->down_cnt)) {
2211 		delay(1 * drv_usectohz(1000000));
2212 		wait_time--;
2213 		if (wait_time == 0)
2214 			break;
2215 	}
2216 again:
2217 	if (old_down_cnt == *((volatile int *)&mgp->down_cnt)) {
2218 		cmn_err(CE_WARN, "%s: didn't get down irq\n", mgp->name);
2219 		for (i = 0; i < mgp->num_slices; i++) {
2220 			/*
2221 			 * take and release the rx lock to ensure
2222 			 * that no interrupt thread is blocked
2223 			 * elsewhere in the stack, preventing
2224 			 * completion
2225 			 */
2226 
2227 			mutex_enter(&mgp->ss[i].rx_lock);
2228 			printf("%s: slice %d rx irq idle\n",
2229 			    mgp->name, i);
2230 			mutex_exit(&mgp->ss[i].rx_lock);
2231 
2232 			/* verify that the poll handler is inactive */
2233 			mutex_enter(&mgp->ss->poll_lock);
2234 			polling = mgp->ss->rx_polling;
2235 			mutex_exit(&mgp->ss->poll_lock);
2236 			if (polling) {
2237 				printf("%s: slice %d is polling\n",
2238 				    mgp->name, i);
2239 				delay(1 * drv_usectohz(1000000));
2240 				goto again;
2241 			}
2242 		}
2243 		delay(1 * drv_usectohz(1000000));
2244 		if (old_down_cnt == *((volatile int *)&mgp->down_cnt)) {
2245 			cmn_err(CE_WARN, "%s: Never got down irq\n", mgp->name);
2246 		}
2247 	}
2248 
2249 	for (i = 0; i < mgp->num_slices; i++)
2250 		myri10ge_teardown_slice(&mgp->ss[i]);
2251 
2252 	if (mgp->toeplitz_hash_table != NULL) {
2253 		kmem_free(mgp->toeplitz_hash_table,
2254 		    sizeof (uint32_t) * 12 * 256);
2255 		mgp->toeplitz_hash_table = NULL;
2256 	}
2257 	mgp->running = MYRI10GE_ETH_STOPPED;
2258 }
2259 
2260 static int
2261 myri10ge_m_start(void *arg)
2262 {
2263 	struct myri10ge_priv *mgp = arg;
2264 	int status;
2265 
2266 	mutex_enter(&mgp->intrlock);
2267 
2268 	if (mgp->running != MYRI10GE_ETH_STOPPED) {
2269 		mutex_exit(&mgp->intrlock);
2270 		return (DDI_FAILURE);
2271 	}
2272 	status = myri10ge_start_locked(mgp);
2273 	mutex_exit(&mgp->intrlock);
2274 
2275 	if (status != DDI_SUCCESS)
2276 		return (status);
2277 
2278 	/* start the watchdog timer */
2279 	mgp->timer_id = timeout(myri10ge_watchdog, mgp,
2280 	    mgp->timer_ticks);
2281 	return (DDI_SUCCESS);
2282 
2283 }
2284 
2285 static void
2286 myri10ge_m_stop(void *arg)
2287 {
2288 	struct myri10ge_priv *mgp = arg;
2289 
2290 	mutex_enter(&mgp->intrlock);
2291 	/* if the device not running give up */
2292 	if (mgp->running != MYRI10GE_ETH_RUNNING) {
2293 		mutex_exit(&mgp->intrlock);
2294 		return;
2295 	}
2296 
2297 	mgp->running = MYRI10GE_ETH_STOPPING;
2298 	mutex_exit(&mgp->intrlock);
2299 	(void) untimeout(mgp->timer_id);
2300 	mutex_enter(&mgp->intrlock);
2301 	myri10ge_stop_locked(mgp);
2302 	mutex_exit(&mgp->intrlock);
2303 
2304 }
2305 
2306 static inline void
2307 myri10ge_rx_csum(mblk_t *mp, struct myri10ge_rx_ring_stats *s, uint32_t csum)
2308 {
2309 	struct ether_header *eh;
2310 	struct ip *ip;
2311 	struct ip6_hdr *ip6;
2312 	uint32_t start, stuff, end, partial, hdrlen;
2313 
2314 
2315 	csum = ntohs((uint16_t)csum);
2316 	eh = (struct ether_header *)(void *)mp->b_rptr;
2317 	hdrlen = sizeof (*eh);
2318 	if (eh->ether_dhost.ether_addr_octet[0] & 1) {
2319 		if (0 == (bcmp(eh->ether_dhost.ether_addr_octet,
2320 		    myri10ge_broadcastaddr, sizeof (eh->ether_dhost))))
2321 			s->brdcstrcv++;
2322 		else
2323 			s->multircv++;
2324 	}
2325 
2326 	if (eh->ether_type == BE_16(ETHERTYPE_VLAN)) {
2327 		/*
2328 		 * fix checksum by subtracting 4 bytes after what the
2329 		 * firmware thought was the end of the ether hdr
2330 		 */
2331 		partial = *(uint32_t *)
2332 		    (void *)(mp->b_rptr + ETHERNET_HEADER_SIZE);
2333 		csum += ~partial;
2334 		csum +=  (csum < ~partial);
2335 		csum = (csum >> 16) + (csum & 0xFFFF);
2336 		csum = (csum >> 16) + (csum & 0xFFFF);
2337 		hdrlen += VLAN_TAGSZ;
2338 	}
2339 
2340 	if (eh->ether_type ==  BE_16(ETHERTYPE_IP)) {
2341 		ip = (struct ip *)(void *)(mp->b_rptr + hdrlen);
2342 		start = ip->ip_hl << 2;
2343 
2344 		if (ip->ip_p == IPPROTO_TCP)
2345 			stuff = start + offsetof(struct tcphdr, th_sum);
2346 		else if (ip->ip_p == IPPROTO_UDP)
2347 			stuff = start + offsetof(struct udphdr, uh_sum);
2348 		else
2349 			return;
2350 		end = ntohs(ip->ip_len);
2351 	} else if (eh->ether_type ==  BE_16(ETHERTYPE_IPV6)) {
2352 		ip6 = (struct ip6_hdr *)(void *)(mp->b_rptr + hdrlen);
2353 		start = sizeof (*ip6);
2354 		if (ip6->ip6_nxt == IPPROTO_TCP) {
2355 			stuff = start + offsetof(struct tcphdr, th_sum);
2356 		} else if (ip6->ip6_nxt == IPPROTO_UDP)
2357 			stuff = start + offsetof(struct udphdr, uh_sum);
2358 		else
2359 			return;
2360 		end = start + ntohs(ip6->ip6_plen);
2361 		/*
2362 		 * IPv6 headers do not contain a checksum, and hence
2363 		 * do not checksum to zero, so they don't "fall out"
2364 		 * of the partial checksum calculation like IPv4
2365 		 * headers do.  We need to fix the partial checksum by
2366 		 * subtracting the checksum of the IPv6 header.
2367 		 */
2368 
2369 		partial = myri10ge_csum_generic((uint16_t *)ip6, sizeof (*ip6));
2370 		csum += ~partial;
2371 		csum +=  (csum < ~partial);
2372 		csum = (csum >> 16) + (csum & 0xFFFF);
2373 		csum = (csum >> 16) + (csum & 0xFFFF);
2374 	} else {
2375 		return;
2376 	}
2377 
2378 	if (MBLKL(mp) > hdrlen + end) {
2379 		/* padded frame, so hw csum may be invalid */
2380 		return;
2381 	}
2382 
2383 	(void) hcksum_assoc(mp, NULL, NULL, start, stuff, end,
2384 	    csum, HCK_PARTIALCKSUM, 0);
2385 }
2386 
2387 static mblk_t *
2388 myri10ge_rx_done_small(struct myri10ge_slice_state *ss, uint32_t len,
2389     uint32_t csum)
2390 {
2391 	mblk_t *mp;
2392 	myri10ge_rx_ring_t *rx;
2393 	int idx;
2394 
2395 	rx = &ss->rx_small;
2396 	idx = rx->cnt & rx->mask;
2397 	ss->rx_small.cnt++;
2398 
2399 	/* allocate a new buffer to pass up the stack */
2400 	mp = allocb(len + MXGEFW_PAD, 0);
2401 	if (mp == NULL) {
2402 		MYRI10GE_ATOMIC_SLICE_STAT_INC(rx_small_nobuf);
2403 		goto abort;
2404 	}
2405 	bcopy(ss->rx_small.info[idx].ptr,
2406 	    (caddr_t)mp->b_wptr, len + MXGEFW_PAD);
2407 	mp->b_wptr += len + MXGEFW_PAD;
2408 	mp->b_rptr += MXGEFW_PAD;
2409 
2410 	ss->rx_stats.ibytes += len;
2411 	ss->rx_stats.ipackets += 1;
2412 	myri10ge_rx_csum(mp, &ss->rx_stats, csum);
2413 
2414 abort:
2415 	if ((idx & 7) == 7) {
2416 		myri10ge_submit_8rx(&rx->lanai[idx - 7],
2417 		    &rx->shadow[idx - 7]);
2418 	}
2419 
2420 	return (mp);
2421 }
2422 
2423 
2424 static mblk_t *
2425 myri10ge_rx_done_big(struct myri10ge_slice_state *ss, uint32_t len,
2426     uint32_t csum)
2427 {
2428 	struct myri10ge_jpool_stuff *jpool;
2429 	struct myri10ge_jpool_entry *j;
2430 	mblk_t *mp;
2431 	int idx, num_owned_by_mcp;
2432 
2433 	jpool = &ss->jpool;
2434 	idx = ss->j_rx_cnt & ss->rx_big.mask;
2435 	j = ss->rx_big.info[idx].j;
2436 
2437 	if (j == NULL) {
2438 		printf("%s: null j at idx=%d, rx_big.cnt = %d, j_rx_cnt=%d\n",
2439 		    ss->mgp->name, idx, ss->rx_big.cnt, ss->j_rx_cnt);
2440 		return (NULL);
2441 	}
2442 
2443 
2444 	ss->rx_big.info[idx].j = NULL;
2445 	ss->j_rx_cnt++;
2446 
2447 
2448 	/*
2449 	 * Check to see if we are low on rx buffers.
2450 	 * Note that we must leave at least 8 free so there are
2451 	 * enough to free in a single 64-byte write.
2452 	 */
2453 	num_owned_by_mcp = ss->rx_big.cnt - ss->j_rx_cnt;
2454 	if (num_owned_by_mcp < jpool->low_water) {
2455 		mutex_enter(&jpool->mtx);
2456 		myri10ge_restock_jumbos(ss);
2457 		mutex_exit(&jpool->mtx);
2458 		num_owned_by_mcp = ss->rx_big.cnt - ss->j_rx_cnt;
2459 		/* if we are still low, then we have to copy */
2460 		if (num_owned_by_mcp < 16) {
2461 			MYRI10GE_ATOMIC_SLICE_STAT_INC(rx_copy);
2462 			/* allocate a new buffer to pass up the stack */
2463 			mp = allocb(len + MXGEFW_PAD, 0);
2464 			if (mp == NULL) {
2465 				goto abort;
2466 			}
2467 			bcopy(j->buf,
2468 			    (caddr_t)mp->b_wptr, len + MXGEFW_PAD);
2469 			myri10ge_jfree_rtn(j);
2470 			/* push buffer back to NIC */
2471 			mutex_enter(&jpool->mtx);
2472 			myri10ge_restock_jumbos(ss);
2473 			mutex_exit(&jpool->mtx);
2474 			goto set_len;
2475 		}
2476 	}
2477 
2478 	/* loan our buffer to the stack */
2479 	mp = desballoc((unsigned char *)j->buf, myri10ge_mtu, 0, &j->free_func);
2480 	if (mp == NULL) {
2481 		goto abort;
2482 	}
2483 
2484 set_len:
2485 	mp->b_rptr += MXGEFW_PAD;
2486 	mp->b_wptr = ((unsigned char *) mp->b_rptr + len);
2487 
2488 	ss->rx_stats.ibytes += len;
2489 	ss->rx_stats.ipackets += 1;
2490 	myri10ge_rx_csum(mp, &ss->rx_stats, csum);
2491 
2492 	return (mp);
2493 
2494 abort:
2495 	myri10ge_jfree_rtn(j);
2496 	MYRI10GE_ATOMIC_SLICE_STAT_INC(rx_big_nobuf);
2497 	return (NULL);
2498 }
2499 
2500 /*
2501  * Free all transmit buffers up until the specified index
2502  */
2503 static inline void
2504 myri10ge_tx_done(struct myri10ge_slice_state *ss, uint32_t mcp_index)
2505 {
2506 	myri10ge_tx_ring_t *tx;
2507 	struct myri10ge_tx_dma_handle_head handles;
2508 	int idx;
2509 	int limit = 0;
2510 
2511 	tx = &ss->tx;
2512 	handles.head = NULL;
2513 	handles.tail = NULL;
2514 	while (tx->pkt_done != (int)mcp_index) {
2515 		idx = tx->done & tx->mask;
2516 
2517 		/*
2518 		 * mblk & DMA handle attached only to first slot
2519 		 * per buffer in the packet
2520 		 */
2521 
2522 		if (tx->info[idx].m) {
2523 			(void) ddi_dma_unbind_handle(tx->info[idx].handle->h);
2524 			tx->info[idx].handle->next = handles.head;
2525 			handles.head = tx->info[idx].handle;
2526 			if (handles.tail == NULL)
2527 				handles.tail = tx->info[idx].handle;
2528 			freeb(tx->info[idx].m);
2529 			tx->info[idx].m = 0;
2530 			tx->info[idx].handle = 0;
2531 		}
2532 		if (tx->info[idx].ostat.opackets != 0) {
2533 			tx->stats.multixmt += tx->info[idx].ostat.multixmt;
2534 			tx->stats.brdcstxmt += tx->info[idx].ostat.brdcstxmt;
2535 			tx->stats.obytes += tx->info[idx].ostat.obytes;
2536 			tx->stats.opackets += tx->info[idx].ostat.opackets;
2537 			tx->info[idx].stat.un.all = 0;
2538 			tx->pkt_done++;
2539 		}
2540 
2541 		tx->done++;
2542 		/*
2543 		 * if we stalled the queue, wake it.  But Wait until
2544 		 * we have at least 1/2 our slots free.
2545 		 */
2546 		if ((tx->req - tx->done) < (tx->mask >> 1) &&
2547 		    tx->stall != tx->sched) {
2548 			mutex_enter(&ss->tx.lock);
2549 			tx->sched = tx->stall;
2550 			mutex_exit(&ss->tx.lock);
2551 			mac_tx_ring_update(ss->mgp->mh, tx->rh);
2552 		}
2553 
2554 		/* limit potential for livelock */
2555 		if (unlikely(++limit >  2 * tx->mask))
2556 			break;
2557 	}
2558 	if (tx->req == tx->done && tx->stop != NULL) {
2559 		/*
2560 		 * Nic has sent all pending requests, allow him
2561 		 * to stop polling this queue
2562 		 */
2563 		mutex_enter(&tx->lock);
2564 		if (tx->req == tx->done && tx->active) {
2565 			*(int *)(void *)tx->stop = 1;
2566 			tx->active = 0;
2567 			mb();
2568 		}
2569 		mutex_exit(&tx->lock);
2570 	}
2571 	if (handles.head != NULL)
2572 		myri10ge_free_tx_handles(tx, &handles);
2573 }
2574 
2575 static void
2576 myri10ge_mbl_init(struct myri10ge_mblk_list *mbl)
2577 {
2578 	mbl->head = NULL;
2579 	mbl->tail = &mbl->head;
2580 	mbl->cnt = 0;
2581 }
2582 
2583 /*ARGSUSED*/
2584 void
2585 myri10ge_mbl_append(struct myri10ge_slice_state *ss,
2586     struct myri10ge_mblk_list *mbl, mblk_t *mp)
2587 {
2588 	*(mbl->tail) = mp;
2589 	mbl->tail = &mp->b_next;
2590 	mp->b_next = NULL;
2591 	mbl->cnt++;
2592 }
2593 
2594 
2595 static inline void
2596 myri10ge_clean_rx_done(struct myri10ge_slice_state *ss,
2597     struct myri10ge_mblk_list *mbl, int limit, boolean_t *stop)
2598 {
2599 	myri10ge_rx_done_t *rx_done = &ss->rx_done;
2600 	struct myri10ge_priv *mgp = ss->mgp;
2601 	mblk_t *mp;
2602 	struct lro_entry *lro;
2603 	uint16_t length;
2604 	uint16_t checksum;
2605 
2606 
2607 	while (rx_done->entry[rx_done->idx].length != 0) {
2608 		if (unlikely (*stop)) {
2609 			break;
2610 		}
2611 		length = ntohs(rx_done->entry[rx_done->idx].length);
2612 		length &= (~MXGEFW_RSS_HASH_MASK);
2613 
2614 		/* limit potential for livelock */
2615 		limit -= length;
2616 		if (unlikely(limit < 0))
2617 			break;
2618 
2619 		rx_done->entry[rx_done->idx].length = 0;
2620 		checksum = ntohs(rx_done->entry[rx_done->idx].checksum);
2621 		if (length <= myri10ge_small_bytes)
2622 			mp = myri10ge_rx_done_small(ss, length, checksum);
2623 		else
2624 			mp = myri10ge_rx_done_big(ss, length, checksum);
2625 		if (mp != NULL) {
2626 			if (!myri10ge_lro ||
2627 			    0 != myri10ge_lro_rx(ss, mp, checksum, mbl))
2628 				myri10ge_mbl_append(ss, mbl, mp);
2629 		}
2630 		rx_done->cnt++;
2631 		rx_done->idx = rx_done->cnt & (mgp->max_intr_slots - 1);
2632 	}
2633 	while (ss->lro_active != NULL) {
2634 		lro = ss->lro_active;
2635 		ss->lro_active = lro->next;
2636 		myri10ge_lro_flush(ss, lro, mbl);
2637 	}
2638 }
2639 
2640 static void
2641 myri10ge_intr_rx(struct myri10ge_slice_state *ss)
2642 {
2643 	uint64_t gen;
2644 	struct myri10ge_mblk_list mbl;
2645 
2646 	myri10ge_mbl_init(&mbl);
2647 	if (mutex_tryenter(&ss->rx_lock) == 0)
2648 		return;
2649 	gen = ss->rx_gen_num;
2650 	myri10ge_clean_rx_done(ss, &mbl, MYRI10GE_POLL_NULL,
2651 	    &ss->rx_polling);
2652 	if (mbl.head != NULL)
2653 		mac_rx_ring(ss->mgp->mh, ss->rx_rh, mbl.head, gen);
2654 	mutex_exit(&ss->rx_lock);
2655 
2656 }
2657 
2658 static mblk_t *
2659 myri10ge_poll_rx(void *arg, int bytes)
2660 {
2661 	struct myri10ge_slice_state *ss = arg;
2662 	struct myri10ge_mblk_list mbl;
2663 	boolean_t dummy = B_FALSE;
2664 
2665 	if (bytes == 0)
2666 		return (NULL);
2667 
2668 	myri10ge_mbl_init(&mbl);
2669 	mutex_enter(&ss->rx_lock);
2670 	if (ss->rx_polling)
2671 		myri10ge_clean_rx_done(ss, &mbl, bytes, &dummy);
2672 	else
2673 		printf("%d: poll_rx: token=%d, polling=%d\n", (int)(ss -
2674 		    ss->mgp->ss), ss->rx_token, ss->rx_polling);
2675 	mutex_exit(&ss->rx_lock);
2676 	return (mbl.head);
2677 }
2678 
2679 /*ARGSUSED*/
2680 static uint_t
2681 myri10ge_intr(caddr_t arg0, caddr_t arg1)
2682 {
2683 	struct myri10ge_slice_state *ss =
2684 	    (struct myri10ge_slice_state *)(void *)arg0;
2685 	struct myri10ge_priv *mgp = ss->mgp;
2686 	mcp_irq_data_t *stats = ss->fw_stats;
2687 	myri10ge_tx_ring_t *tx = &ss->tx;
2688 	uint32_t send_done_count;
2689 	uint8_t valid;
2690 
2691 
2692 	/* make sure the DMA has finished */
2693 	if (!stats->valid) {
2694 		return (DDI_INTR_UNCLAIMED);
2695 	}
2696 	valid = stats->valid;
2697 
2698 	/* low bit indicates receives are present */
2699 	if (valid & 1)
2700 		myri10ge_intr_rx(ss);
2701 
2702 	if (mgp->ddi_intr_type == DDI_INTR_TYPE_FIXED) {
2703 		/* lower legacy IRQ  */
2704 		*mgp->irq_deassert = 0;
2705 		if (!myri10ge_deassert_wait)
2706 			/* don't wait for conf. that irq is low */
2707 			stats->valid = 0;
2708 		mb();
2709 	} else {
2710 		/* no need to wait for conf. that irq is low */
2711 		stats->valid = 0;
2712 	}
2713 
2714 	do {
2715 		/* check for transmit completes and receives */
2716 		send_done_count = ntohl(stats->send_done_count);
2717 		if (send_done_count != tx->pkt_done)
2718 			myri10ge_tx_done(ss, (int)send_done_count);
2719 	} while (*((volatile uint8_t *) &stats->valid));
2720 
2721 	if (stats->stats_updated) {
2722 		if (mgp->link_state != stats->link_up || stats->link_down) {
2723 			mgp->link_state = stats->link_up;
2724 			if (stats->link_down) {
2725 				mgp->down_cnt += stats->link_down;
2726 				mgp->link_state = 0;
2727 			}
2728 			if (mgp->link_state) {
2729 				if (myri10ge_verbose)
2730 					printf("%s: link up\n", mgp->name);
2731 				mac_link_update(mgp->mh, LINK_STATE_UP);
2732 			} else {
2733 				if (myri10ge_verbose)
2734 					printf("%s: link down\n", mgp->name);
2735 				mac_link_update(mgp->mh, LINK_STATE_DOWN);
2736 			}
2737 			MYRI10GE_NIC_STAT_INC(link_changes);
2738 		}
2739 		if (mgp->rdma_tags_available !=
2740 		    ntohl(ss->fw_stats->rdma_tags_available)) {
2741 			mgp->rdma_tags_available =
2742 			    ntohl(ss->fw_stats->rdma_tags_available);
2743 			cmn_err(CE_NOTE, "%s: RDMA timed out! "
2744 			    "%d tags left\n", mgp->name,
2745 			    mgp->rdma_tags_available);
2746 		}
2747 	}
2748 
2749 	mb();
2750 	/* check to see if we have rx token to pass back */
2751 	if (valid & 0x1) {
2752 		mutex_enter(&ss->poll_lock);
2753 		if (ss->rx_polling) {
2754 			ss->rx_token = 1;
2755 		} else {
2756 			*ss->irq_claim = BE_32(3);
2757 			ss->rx_token = 0;
2758 		}
2759 		mutex_exit(&ss->poll_lock);
2760 	}
2761 	*(ss->irq_claim + 1) = BE_32(3);
2762 	return (DDI_INTR_CLAIMED);
2763 }
2764 
2765 /*
2766  * Add or remove a multicast address.  This is called with our
2767  * macinfo's lock held by GLD, so we do not need to worry about
2768  * our own locking here.
2769  */
2770 static int
2771 myri10ge_m_multicst(void *arg, boolean_t add, const uint8_t *multicastaddr)
2772 {
2773 	myri10ge_cmd_t cmd;
2774 	struct myri10ge_priv *mgp = arg;
2775 	int status, join_leave;
2776 
2777 	if (add)
2778 		join_leave = MXGEFW_JOIN_MULTICAST_GROUP;
2779 	else
2780 		join_leave = MXGEFW_LEAVE_MULTICAST_GROUP;
2781 	(void) memcpy(&cmd.data0, multicastaddr, 4);
2782 	(void) memcpy(&cmd.data1, multicastaddr + 4, 2);
2783 	cmd.data0 = htonl(cmd.data0);
2784 	cmd.data1 = htonl(cmd.data1);
2785 	status = myri10ge_send_cmd(mgp, join_leave, &cmd);
2786 	if (status == 0)
2787 		return (0);
2788 
2789 	cmn_err(CE_WARN, "%s: failed to set multicast address\n",
2790 	    mgp->name);
2791 	return (status);
2792 }
2793 
2794 
2795 static int
2796 myri10ge_m_promisc(void *arg, boolean_t on)
2797 {
2798 	struct myri10ge_priv *mgp = arg;
2799 
2800 	myri10ge_change_promisc(mgp, on);
2801 	return (0);
2802 }
2803 
2804 /*
2805  * copy an array of mcp_kreq_ether_send_t's to the mcp.  Copy
2806  *  backwards one at a time and handle ring wraps
2807  */
2808 
2809 static inline void
2810 myri10ge_submit_req_backwards(myri10ge_tx_ring_t *tx,
2811     mcp_kreq_ether_send_t *src, int cnt)
2812 {
2813 	int idx, starting_slot;
2814 	starting_slot = tx->req;
2815 	while (cnt > 1) {
2816 		cnt--;
2817 		idx = (starting_slot + cnt) & tx->mask;
2818 		myri10ge_pio_copy(&tx->lanai[idx],
2819 		    &src[cnt], sizeof (*src));
2820 		mb();
2821 	}
2822 }
2823 
2824 /*
2825  * copy an array of mcp_kreq_ether_send_t's to the mcp.  Copy
2826  * at most 32 bytes at a time, so as to avoid involving the software
2827  * pio handler in the nic.   We re-write the first segment's flags
2828  * to mark them valid only after writing the entire chain
2829  */
2830 
2831 static inline void
2832 myri10ge_submit_req(myri10ge_tx_ring_t *tx, mcp_kreq_ether_send_t *src,
2833     int cnt)
2834 {
2835 	int idx, i;
2836 	uint32_t *src_ints, *dst_ints;
2837 	mcp_kreq_ether_send_t *srcp, *dstp, *dst;
2838 	uint8_t last_flags;
2839 
2840 	idx = tx->req & tx->mask;
2841 
2842 	last_flags = src->flags;
2843 	src->flags = 0;
2844 	mb();
2845 	dst = dstp = &tx->lanai[idx];
2846 	srcp = src;
2847 
2848 	if ((idx + cnt) < tx->mask) {
2849 		for (i = 0; i < (cnt - 1); i += 2) {
2850 			myri10ge_pio_copy(dstp, srcp, 2 * sizeof (*src));
2851 			mb(); /* force write every 32 bytes */
2852 			srcp += 2;
2853 			dstp += 2;
2854 		}
2855 	} else {
2856 		/*
2857 		 * submit all but the first request, and ensure
2858 		 *  that it is submitted below
2859 		 */
2860 		myri10ge_submit_req_backwards(tx, src, cnt);
2861 		i = 0;
2862 	}
2863 	if (i < cnt) {
2864 		/* submit the first request */
2865 		myri10ge_pio_copy(dstp, srcp, sizeof (*src));
2866 		mb(); /* barrier before setting valid flag */
2867 	}
2868 
2869 	/* re-write the last 32-bits with the valid flags */
2870 	src->flags |= last_flags;
2871 	src_ints = (uint32_t *)src;
2872 	src_ints += 3;
2873 	dst_ints = (uint32_t *)dst;
2874 	dst_ints += 3;
2875 	*dst_ints =  *src_ints;
2876 	tx->req += cnt;
2877 	mb();
2878 	/* notify NIC to poll this tx ring */
2879 	if (!tx->active && tx->go != NULL) {
2880 		*(int *)(void *)tx->go = 1;
2881 		tx->active = 1;
2882 		tx->activate++;
2883 		mb();
2884 	}
2885 }
2886 
2887 /* ARGSUSED */
2888 static inline void
2889 myri10ge_lso_info_get(mblk_t *mp, uint32_t *mss, uint32_t *flags)
2890 {
2891 	uint32_t lso_flag;
2892 	lso_info_get(mp, mss, &lso_flag);
2893 	(*flags) |= lso_flag;
2894 }
2895 
2896 
2897 /* like pullupmsg, except preserve hcksum/LSO attributes */
2898 static int
2899 myri10ge_pullup(struct myri10ge_slice_state *ss, mblk_t *mp)
2900 {
2901 	uint32_t start, stuff, tx_offload_flags, mss;
2902 	int ok;
2903 
2904 	mss = 0;
2905 	hcksum_retrieve(mp, NULL, NULL, &start, &stuff, NULL, NULL,
2906 	    &tx_offload_flags);
2907 	myri10ge_lso_info_get(mp, &mss, &tx_offload_flags);
2908 
2909 	ok = pullupmsg(mp, -1);
2910 	if (!ok) {
2911 		printf("pullupmsg failed");
2912 		return (DDI_FAILURE);
2913 	}
2914 	MYRI10GE_ATOMIC_SLICE_STAT_INC(xmit_pullup);
2915 	(void) hcksum_assoc(mp, NULL, NULL, start, stuff, NULL,
2916 	    NULL, tx_offload_flags, 0);
2917 	if (tx_offload_flags & HW_LSO)
2918 		DB_LSOMSS(mp) = (uint16_t)mss;
2919 	lso_info_set(mp, mss, tx_offload_flags);
2920 	return (DDI_SUCCESS);
2921 }
2922 
2923 static inline void
2924 myri10ge_tx_stat(struct myri10ge_tx_pkt_stats *s, struct ether_header *eh,
2925     int opackets, int obytes)
2926 {
2927 	s->un.all = 0;
2928 	if (eh->ether_dhost.ether_addr_octet[0] & 1) {
2929 		if (0 == (bcmp(eh->ether_dhost.ether_addr_octet,
2930 		    myri10ge_broadcastaddr, sizeof (eh->ether_dhost))))
2931 			s->un.s.brdcstxmt = 1;
2932 		else
2933 			s->un.s.multixmt = 1;
2934 	}
2935 	s->un.s.opackets = (uint16_t)opackets;
2936 	s->un.s.obytes = obytes;
2937 }
2938 
2939 static int
2940 myri10ge_tx_copy(struct myri10ge_slice_state *ss, mblk_t *mp,
2941     mcp_kreq_ether_send_t *req)
2942 {
2943 	myri10ge_tx_ring_t *tx = &ss->tx;
2944 	caddr_t ptr;
2945 	struct myri10ge_tx_copybuf *cp;
2946 	mblk_t *bp;
2947 	int idx, mblen, avail;
2948 	uint16_t len;
2949 
2950 	mutex_enter(&tx->lock);
2951 	avail = tx->mask - (tx->req - tx->done);
2952 	if (avail <= 1) {
2953 		mutex_exit(&tx->lock);
2954 		return (EBUSY);
2955 	}
2956 	idx = tx->req & tx->mask;
2957 	cp = &tx->cp[idx];
2958 	ptr = cp->va;
2959 	for (len = 0, bp = mp; bp != NULL; bp = bp->b_cont) {
2960 		mblen = MBLKL(bp);
2961 		bcopy(bp->b_rptr, ptr, mblen);
2962 		ptr += mblen;
2963 		len += mblen;
2964 	}
2965 	/* ensure runts are padded to 60 bytes */
2966 	if (len < 60) {
2967 		bzero(ptr, 64 - len);
2968 		len = 60;
2969 	}
2970 	req->addr_low = cp->dma.low;
2971 	req->addr_high = cp->dma.high;
2972 	req->length = htons(len);
2973 	req->pad = 0;
2974 	req->rdma_count = 1;
2975 	myri10ge_tx_stat(&tx->info[idx].stat,
2976 	    (struct ether_header *)(void *)cp->va, 1, len);
2977 	(void) ddi_dma_sync(cp->dma.handle, 0, len, DDI_DMA_SYNC_FORDEV);
2978 	myri10ge_submit_req(&ss->tx, req, 1);
2979 	mutex_exit(&tx->lock);
2980 	freemsg(mp);
2981 	return (DDI_SUCCESS);
2982 }
2983 
2984 
2985 static void
2986 myri10ge_send_locked(myri10ge_tx_ring_t *tx, mcp_kreq_ether_send_t *req_list,
2987     struct myri10ge_tx_buffer_state *tx_info,
2988     int count)
2989 {
2990 	int i, idx;
2991 
2992 	idx = 0; /* gcc -Wuninitialized */
2993 	/* store unmapping and bp info for tx irq handler */
2994 	for (i = 0; i < count; i++) {
2995 		idx = (tx->req + i) & tx->mask;
2996 		tx->info[idx].m = tx_info[i].m;
2997 		tx->info[idx].handle = tx_info[i].handle;
2998 	}
2999 	tx->info[idx].stat.un.all = tx_info[0].stat.un.all;
3000 
3001 	/* submit the frame to the nic */
3002 	myri10ge_submit_req(tx, req_list, count);
3003 
3004 
3005 }
3006 
3007 
3008 
3009 static void
3010 myri10ge_copydata(mblk_t *mp, int off, int len, caddr_t buf)
3011 {
3012 	mblk_t *bp;
3013 	int seglen;
3014 	uint_t count;
3015 
3016 	bp = mp;
3017 
3018 	while (off > 0) {
3019 		seglen = MBLKL(bp);
3020 		if (off < seglen)
3021 			break;
3022 		off -= seglen;
3023 		bp = bp->b_cont;
3024 	}
3025 	while (len > 0) {
3026 		seglen = MBLKL(bp);
3027 		count = min(seglen - off, len);
3028 		bcopy(bp->b_rptr + off, buf, count);
3029 		len -= count;
3030 		buf += count;
3031 		off = 0;
3032 		bp = bp->b_cont;
3033 	}
3034 }
3035 
3036 static int
3037 myri10ge_ether_parse_header(mblk_t *mp)
3038 {
3039 	struct ether_header eh_copy;
3040 	struct ether_header *eh;
3041 	int eth_hdr_len, seglen;
3042 
3043 	seglen = MBLKL(mp);
3044 	eth_hdr_len = sizeof (*eh);
3045 	if (seglen < eth_hdr_len) {
3046 		myri10ge_copydata(mp, 0, eth_hdr_len, (caddr_t)&eh_copy);
3047 		eh = &eh_copy;
3048 	} else {
3049 		eh = (struct ether_header *)(void *)mp->b_rptr;
3050 	}
3051 	if (eh->ether_type == BE_16(ETHERTYPE_VLAN)) {
3052 		eth_hdr_len += 4;
3053 	}
3054 
3055 	return (eth_hdr_len);
3056 }
3057 
3058 static int
3059 myri10ge_lso_parse_header(mblk_t *mp, int off)
3060 {
3061 	char buf[128];
3062 	int seglen;
3063 	struct ip *ip;
3064 	struct tcphdr *tcp;
3065 
3066 	seglen = MBLKL(mp);
3067 	if (seglen < off + sizeof (*ip)) {
3068 		myri10ge_copydata(mp, off, sizeof (*ip), buf);
3069 		ip = (struct ip *)(void *)buf;
3070 	} else {
3071 		ip = (struct ip *)(void *)(mp->b_rptr + off);
3072 	}
3073 	if (seglen < off + (ip->ip_hl << 2) + sizeof (*tcp)) {
3074 		myri10ge_copydata(mp, off,
3075 		    (ip->ip_hl << 2) + sizeof (*tcp), buf);
3076 		ip = (struct ip *)(void *)buf;
3077 	}
3078 	tcp = (struct tcphdr *)(void *)((char *)ip + (ip->ip_hl << 2));
3079 	return (off + ((ip->ip_hl + tcp->th_off) << 2));
3080 }
3081 
3082 static int
3083 myri10ge_tx_tso_copy(struct myri10ge_slice_state *ss, mblk_t *mp,
3084     mcp_kreq_ether_send_t *req_list, int hdr_size, int pkt_size,
3085     uint16_t mss, uint8_t cksum_offset)
3086 {
3087 	myri10ge_tx_ring_t *tx = &ss->tx;
3088 	struct myri10ge_priv *mgp = ss->mgp;
3089 	mblk_t *bp;
3090 	mcp_kreq_ether_send_t *req;
3091 	struct myri10ge_tx_copybuf *cp;
3092 	caddr_t rptr, ptr;
3093 	int mblen, count, cum_len, mss_resid, tx_req, pkt_size_tmp;
3094 	int resid, avail, idx, hdr_size_tmp, tx_boundary;
3095 	int rdma_count;
3096 	uint32_t seglen, len, boundary, low, high_swapped;
3097 	uint16_t pseudo_hdr_offset = htons(mss);
3098 	uint8_t flags;
3099 
3100 	tx_boundary = mgp->tx_boundary;
3101 	hdr_size_tmp = hdr_size;
3102 	resid = tx_boundary;
3103 	count = 1;
3104 	mutex_enter(&tx->lock);
3105 
3106 	/* check to see if the slots are really there */
3107 	avail = tx->mask - (tx->req - tx->done);
3108 	if (unlikely(avail <=  MYRI10GE_MAX_SEND_DESC_TSO)) {
3109 		atomic_add_32(&tx->stall, 1);
3110 		mutex_exit(&tx->lock);
3111 		return (EBUSY);
3112 	}
3113 
3114 	/* copy */
3115 	cum_len = -hdr_size;
3116 	count = 0;
3117 	req = req_list;
3118 	idx = tx->mask & tx->req;
3119 	cp = &tx->cp[idx];
3120 	low = ntohl(cp->dma.low);
3121 	ptr = cp->va;
3122 	cp->len = 0;
3123 	if (mss) {
3124 		int payload = pkt_size - hdr_size;
3125 		uint16_t opackets = (payload / mss) + ((payload % mss) != 0);
3126 		tx->info[idx].ostat.opackets = opackets;
3127 		tx->info[idx].ostat.obytes = (opackets - 1) * hdr_size
3128 		    + pkt_size;
3129 	}
3130 	hdr_size_tmp = hdr_size;
3131 	mss_resid = mss;
3132 	flags = (MXGEFW_FLAGS_TSO_HDR | MXGEFW_FLAGS_FIRST);
3133 	tx_req = tx->req;
3134 	for (bp = mp; bp != NULL; bp = bp->b_cont) {
3135 		mblen = MBLKL(bp);
3136 		rptr = (caddr_t)bp->b_rptr;
3137 		len = min(hdr_size_tmp, mblen);
3138 		if (len) {
3139 			bcopy(rptr, ptr, len);
3140 			rptr += len;
3141 			ptr += len;
3142 			resid -= len;
3143 			mblen -= len;
3144 			hdr_size_tmp -= len;
3145 			cp->len += len;
3146 			if (hdr_size_tmp)
3147 				continue;
3148 			if (resid < mss) {
3149 				tx_req++;
3150 				idx = tx->mask & tx_req;
3151 				cp = &tx->cp[idx];
3152 				low = ntohl(cp->dma.low);
3153 				ptr = cp->va;
3154 				resid = tx_boundary;
3155 			}
3156 		}
3157 		while (mblen) {
3158 			len = min(mss_resid, mblen);
3159 			bcopy(rptr, ptr, len);
3160 			mss_resid -= len;
3161 			resid -= len;
3162 			mblen -= len;
3163 			rptr += len;
3164 			ptr += len;
3165 			cp->len += len;
3166 			if (mss_resid == 0) {
3167 				mss_resid = mss;
3168 				if (resid < mss) {
3169 					tx_req++;
3170 					idx = tx->mask & tx_req;
3171 					cp = &tx->cp[idx];
3172 					cp->len = 0;
3173 					low = ntohl(cp->dma.low);
3174 					ptr = cp->va;
3175 					resid = tx_boundary;
3176 				}
3177 			}
3178 		}
3179 	}
3180 
3181 	req = req_list;
3182 	pkt_size_tmp = pkt_size;
3183 	count = 0;
3184 	rdma_count = 0;
3185 	tx_req = tx->req;
3186 	while (pkt_size_tmp) {
3187 		idx = tx->mask & tx_req;
3188 		cp = &tx->cp[idx];
3189 		high_swapped = cp->dma.high;
3190 		low = ntohl(cp->dma.low);
3191 		len = cp->len;
3192 		if (len == 0) {
3193 			printf("len=0! pkt_size_tmp=%d, pkt_size=%d\n",
3194 			    pkt_size_tmp, pkt_size);
3195 			for (bp = mp; bp != NULL; bp = bp->b_cont) {
3196 				mblen = MBLKL(bp);
3197 				printf("mblen:%d\n", mblen);
3198 			}
3199 			pkt_size_tmp = pkt_size;
3200 			tx_req = tx->req;
3201 			while (pkt_size_tmp > 0) {
3202 				idx = tx->mask & tx_req;
3203 				cp = &tx->cp[idx];
3204 				printf("cp->len = %d\n", cp->len);
3205 				pkt_size_tmp -= cp->len;
3206 				tx_req++;
3207 			}
3208 			printf("dropped\n");
3209 			MYRI10GE_ATOMIC_SLICE_STAT_INC(xmit_err);
3210 			goto done;
3211 		}
3212 		pkt_size_tmp -= len;
3213 		while (len) {
3214 			while (len) {
3215 				uint8_t flags_next;
3216 				int cum_len_next;
3217 
3218 				boundary = (low + mgp->tx_boundary) &
3219 				    ~(mgp->tx_boundary - 1);
3220 				seglen = boundary - low;
3221 				if (seglen > len)
3222 					seglen = len;
3223 
3224 				flags_next = flags & ~MXGEFW_FLAGS_FIRST;
3225 				cum_len_next = cum_len + seglen;
3226 				(req-rdma_count)->rdma_count = rdma_count + 1;
3227 				if (likely(cum_len >= 0)) {
3228 					/* payload */
3229 					int next_is_first, chop;
3230 
3231 					chop = (cum_len_next > mss);
3232 					cum_len_next = cum_len_next % mss;
3233 					next_is_first = (cum_len_next == 0);
3234 					flags |= chop *
3235 					    MXGEFW_FLAGS_TSO_CHOP;
3236 					flags_next |= next_is_first *
3237 					    MXGEFW_FLAGS_FIRST;
3238 					rdma_count |= -(chop | next_is_first);
3239 					rdma_count += chop & !next_is_first;
3240 				} else if (likely(cum_len_next >= 0)) {
3241 					/* header ends */
3242 					int small;
3243 
3244 					rdma_count = -1;
3245 					cum_len_next = 0;
3246 					seglen = -cum_len;
3247 					small = (mss <= MXGEFW_SEND_SMALL_SIZE);
3248 					flags_next = MXGEFW_FLAGS_TSO_PLD |
3249 					    MXGEFW_FLAGS_FIRST |
3250 					    (small * MXGEFW_FLAGS_SMALL);
3251 				}
3252 				req->addr_high = high_swapped;
3253 				req->addr_low = htonl(low);
3254 				req->pseudo_hdr_offset = pseudo_hdr_offset;
3255 				req->pad = 0; /* complete solid 16-byte block */
3256 				req->rdma_count = 1;
3257 				req->cksum_offset = cksum_offset;
3258 				req->length = htons(seglen);
3259 				req->flags = flags | ((cum_len & 1) *
3260 				    MXGEFW_FLAGS_ALIGN_ODD);
3261 				if (cksum_offset > seglen)
3262 					cksum_offset -= seglen;
3263 				else
3264 					cksum_offset = 0;
3265 				low += seglen;
3266 				len -= seglen;
3267 				cum_len = cum_len_next;
3268 				req++;
3269 				req->flags = 0;
3270 				flags = flags_next;
3271 				count++;
3272 				rdma_count++;
3273 			}
3274 		}
3275 		tx_req++;
3276 	}
3277 	(req-rdma_count)->rdma_count = (uint8_t)rdma_count;
3278 	do {
3279 		req--;
3280 		req->flags |= MXGEFW_FLAGS_TSO_LAST;
3281 	} while (!(req->flags & (MXGEFW_FLAGS_TSO_CHOP |
3282 	    MXGEFW_FLAGS_FIRST)));
3283 
3284 	myri10ge_submit_req(tx, req_list, count);
3285 done:
3286 	mutex_exit(&tx->lock);
3287 	freemsg(mp);
3288 	return (DDI_SUCCESS);
3289 }
3290 
3291 /*
3292  * Try to send the chain of buffers described by the mp.  We must not
3293  * encapsulate more than eth->tx.req - eth->tx.done, or
3294  * MXGEFW_MAX_SEND_DESC, whichever is more.
3295  */
3296 
3297 static int
3298 myri10ge_send(struct myri10ge_slice_state *ss, mblk_t *mp,
3299     mcp_kreq_ether_send_t *req_list, struct myri10ge_tx_buffer_state *tx_info)
3300 {
3301 	struct myri10ge_priv *mgp = ss->mgp;
3302 	myri10ge_tx_ring_t *tx = &ss->tx;
3303 	mcp_kreq_ether_send_t *req;
3304 	struct myri10ge_tx_dma_handle *handles, *dma_handle = NULL;
3305 	mblk_t  *bp;
3306 	ddi_dma_cookie_t cookie;
3307 	int err, rv, count, avail, mblen, try_pullup, i, max_segs, maclen,
3308 	    rdma_count, cum_len, lso_hdr_size;
3309 	uint32_t start, stuff, tx_offload_flags;
3310 	uint32_t seglen, len, mss, boundary, low, high_swapped;
3311 	uint_t ncookies;
3312 	uint16_t pseudo_hdr_offset;
3313 	uint8_t flags, cksum_offset, odd_flag;
3314 	int pkt_size;
3315 	int lso_copy = myri10ge_lso_copy;
3316 	try_pullup = 1;
3317 
3318 again:
3319 	/* Setup checksum offloading, if needed */
3320 	hcksum_retrieve(mp, NULL, NULL, &start, &stuff, NULL, NULL,
3321 	    &tx_offload_flags);
3322 	myri10ge_lso_info_get(mp, &mss, &tx_offload_flags);
3323 	if (tx_offload_flags & HW_LSO) {
3324 		max_segs = MYRI10GE_MAX_SEND_DESC_TSO;
3325 		if ((tx_offload_flags & HCK_PARTIALCKSUM) == 0) {
3326 			MYRI10GE_ATOMIC_SLICE_STAT_INC(xmit_lsobadflags);
3327 			freemsg(mp);
3328 			return (DDI_SUCCESS);
3329 		}
3330 	} else {
3331 		max_segs = MXGEFW_MAX_SEND_DESC;
3332 		mss = 0;
3333 	}
3334 	req = req_list;
3335 	cksum_offset = 0;
3336 	pseudo_hdr_offset = 0;
3337 
3338 	/* leave an extra slot keep the ring from wrapping */
3339 	avail = tx->mask - (tx->req - tx->done);
3340 
3341 	/*
3342 	 * If we have > MXGEFW_MAX_SEND_DESC, then any over-length
3343 	 * message will need to be pulled up in order to fit.
3344 	 * Otherwise, we are low on transmit descriptors, it is
3345 	 * probably better to stall and try again rather than pullup a
3346 	 * message to fit.
3347 	 */
3348 
3349 	if (avail < max_segs) {
3350 		err = EBUSY;
3351 		atomic_add_32(&tx->stall_early, 1);
3352 		goto stall;
3353 	}
3354 
3355 	/* find out how long the frame is and how many segments it is */
3356 	count = 0;
3357 	odd_flag = 0;
3358 	pkt_size = 0;
3359 	flags = (MXGEFW_FLAGS_NO_TSO | MXGEFW_FLAGS_FIRST);
3360 	for (bp = mp; bp != NULL; bp = bp->b_cont) {
3361 		dblk_t *dbp;
3362 		mblen = MBLKL(bp);
3363 		if (mblen == 0) {
3364 			/*
3365 			 * we can't simply skip over 0-length mblks
3366 			 * because the hardware can't deal with them,
3367 			 * and we could leak them.
3368 			 */
3369 			MYRI10GE_ATOMIC_SLICE_STAT_INC(xmit_zero_len);
3370 			err = EIO;
3371 			goto pullup;
3372 		}
3373 		/*
3374 		 * There's no advantage to copying most gesballoc
3375 		 * attached blocks, so disable lso copy in that case
3376 		 */
3377 		if (mss && lso_copy == 1 && ((dbp = bp->b_datap) != NULL)) {
3378 			if ((void *)dbp->db_lastfree != myri10ge_db_lastfree) {
3379 				lso_copy = 0;
3380 			}
3381 		}
3382 		pkt_size += mblen;
3383 		count++;
3384 	}
3385 
3386 	/* Try to pull up excessivly long chains */
3387 	if (count >= max_segs) {
3388 		err = myri10ge_pullup(ss, mp);
3389 		if (likely(err == DDI_SUCCESS)) {
3390 			count = 1;
3391 		} else {
3392 			if (count <  MYRI10GE_MAX_SEND_DESC_TSO) {
3393 				/*
3394 				 * just let the h/w send it, it will be
3395 				 * inefficient, but us better than dropping
3396 				 */
3397 				max_segs = MYRI10GE_MAX_SEND_DESC_TSO;
3398 			} else {
3399 				/* drop it */
3400 				MYRI10GE_ATOMIC_SLICE_STAT_INC(xmit_err);
3401 				freemsg(mp);
3402 				return (0);
3403 			}
3404 		}
3405 	}
3406 
3407 	cum_len = 0;
3408 	maclen = myri10ge_ether_parse_header(mp);
3409 
3410 	if (tx_offload_flags & HCK_PARTIALCKSUM) {
3411 
3412 		cksum_offset = start + maclen;
3413 		pseudo_hdr_offset = htons(stuff + maclen);
3414 		odd_flag = MXGEFW_FLAGS_ALIGN_ODD;
3415 		flags |= MXGEFW_FLAGS_CKSUM;
3416 	}
3417 
3418 	lso_hdr_size = 0; /* -Wunitinialized */
3419 	if (mss) { /* LSO */
3420 		/* this removes any CKSUM flag from before */
3421 		flags = (MXGEFW_FLAGS_TSO_HDR | MXGEFW_FLAGS_FIRST);
3422 		/*
3423 		 * parse the headers and set cum_len to a negative
3424 		 * value to reflect the offset of the TCP payload
3425 		 */
3426 		lso_hdr_size =  myri10ge_lso_parse_header(mp, maclen);
3427 		cum_len = -lso_hdr_size;
3428 		if ((mss < mgp->tx_boundary) && lso_copy) {
3429 			err = myri10ge_tx_tso_copy(ss, mp, req_list,
3430 			    lso_hdr_size, pkt_size, mss, cksum_offset);
3431 			return (err);
3432 		}
3433 
3434 		/*
3435 		 * for TSO, pseudo_hdr_offset holds mss.  The firmware
3436 		 * figures out where to put the checksum by parsing
3437 		 * the header.
3438 		 */
3439 
3440 		pseudo_hdr_offset = htons(mss);
3441 	} else if (pkt_size <= MXGEFW_SEND_SMALL_SIZE) {
3442 		flags |= MXGEFW_FLAGS_SMALL;
3443 		if (pkt_size < myri10ge_tx_copylen) {
3444 			req->cksum_offset = cksum_offset;
3445 			req->pseudo_hdr_offset = pseudo_hdr_offset;
3446 			req->flags = flags;
3447 			err = myri10ge_tx_copy(ss, mp, req);
3448 			return (err);
3449 		}
3450 		cum_len = 0;
3451 	}
3452 
3453 	/* pull one DMA handle for each bp from our freelist */
3454 	handles = NULL;
3455 	err = myri10ge_alloc_tx_handles(ss, count, &handles);
3456 	if (err != DDI_SUCCESS) {
3457 		err = DDI_FAILURE;
3458 		goto stall;
3459 	}
3460 	count = 0;
3461 	rdma_count = 0;
3462 	for (bp = mp; bp != NULL; bp = bp->b_cont) {
3463 		mblen = MBLKL(bp);
3464 		dma_handle = handles;
3465 		handles = handles->next;
3466 
3467 		rv = ddi_dma_addr_bind_handle(dma_handle->h, NULL,
3468 		    (caddr_t)bp->b_rptr, mblen,
3469 		    DDI_DMA_WRITE | DDI_DMA_STREAMING, DDI_DMA_SLEEP, NULL,
3470 		    &cookie, &ncookies);
3471 		if (unlikely(rv != DDI_DMA_MAPPED)) {
3472 			err = EIO;
3473 			try_pullup = 0;
3474 			dma_handle->next = handles;
3475 			handles = dma_handle;
3476 			goto abort_with_handles;
3477 		}
3478 
3479 		/* reserve the slot */
3480 		tx_info[count].m = bp;
3481 		tx_info[count].handle = dma_handle;
3482 
3483 		for (; ; ) {
3484 			low = MYRI10GE_LOWPART_TO_U32(cookie.dmac_laddress);
3485 			high_swapped =
3486 			    htonl(MYRI10GE_HIGHPART_TO_U32(
3487 			    cookie.dmac_laddress));
3488 			len = (uint32_t)cookie.dmac_size;
3489 			while (len) {
3490 				uint8_t flags_next;
3491 				int cum_len_next;
3492 
3493 				boundary = (low + mgp->tx_boundary) &
3494 				    ~(mgp->tx_boundary - 1);
3495 				seglen = boundary - low;
3496 				if (seglen > len)
3497 					seglen = len;
3498 
3499 				flags_next = flags & ~MXGEFW_FLAGS_FIRST;
3500 				cum_len_next = cum_len + seglen;
3501 				if (mss) {
3502 					(req-rdma_count)->rdma_count =
3503 					    rdma_count + 1;
3504 					if (likely(cum_len >= 0)) {
3505 						/* payload */
3506 						int next_is_first, chop;
3507 
3508 						chop = (cum_len_next > mss);
3509 						cum_len_next =
3510 						    cum_len_next % mss;
3511 						next_is_first =
3512 						    (cum_len_next == 0);
3513 						flags |= chop *
3514 						    MXGEFW_FLAGS_TSO_CHOP;
3515 						flags_next |= next_is_first *
3516 						    MXGEFW_FLAGS_FIRST;
3517 						rdma_count |=
3518 						    -(chop | next_is_first);
3519 						rdma_count +=
3520 						    chop & !next_is_first;
3521 					} else if (likely(cum_len_next >= 0)) {
3522 						/* header ends */
3523 						int small;
3524 
3525 						rdma_count = -1;
3526 						cum_len_next = 0;
3527 						seglen = -cum_len;
3528 						small = (mss <=
3529 						    MXGEFW_SEND_SMALL_SIZE);
3530 						flags_next =
3531 						    MXGEFW_FLAGS_TSO_PLD
3532 						    | MXGEFW_FLAGS_FIRST
3533 						    | (small *
3534 						    MXGEFW_FLAGS_SMALL);
3535 					}
3536 				}
3537 				req->addr_high = high_swapped;
3538 				req->addr_low = htonl(low);
3539 				req->pseudo_hdr_offset = pseudo_hdr_offset;
3540 				req->pad = 0; /* complete solid 16-byte block */
3541 				req->rdma_count = 1;
3542 				req->cksum_offset = cksum_offset;
3543 				req->length = htons(seglen);
3544 				req->flags = flags | ((cum_len & 1) * odd_flag);
3545 				if (cksum_offset > seglen)
3546 					cksum_offset -= seglen;
3547 				else
3548 					cksum_offset = 0;
3549 				low += seglen;
3550 				len -= seglen;
3551 				cum_len = cum_len_next;
3552 				count++;
3553 				rdma_count++;
3554 				/*  make sure all the segments will fit */
3555 				if (unlikely(count >= max_segs)) {
3556 					MYRI10GE_ATOMIC_SLICE_STAT_INC(
3557 					    xmit_lowbuf);
3558 					/* may try a pullup */
3559 					err = EBUSY;
3560 					if (try_pullup)
3561 						try_pullup = 2;
3562 					goto abort_with_handles;
3563 				}
3564 				req++;
3565 				req->flags = 0;
3566 				flags = flags_next;
3567 				tx_info[count].m = 0;
3568 			}
3569 			ncookies--;
3570 			if (ncookies == 0)
3571 				break;
3572 			ddi_dma_nextcookie(dma_handle->h, &cookie);
3573 		}
3574 	}
3575 	(req-rdma_count)->rdma_count = (uint8_t)rdma_count;
3576 
3577 	if (mss) {
3578 		do {
3579 			req--;
3580 			req->flags |= MXGEFW_FLAGS_TSO_LAST;
3581 		} while (!(req->flags & (MXGEFW_FLAGS_TSO_CHOP |
3582 		    MXGEFW_FLAGS_FIRST)));
3583 	}
3584 
3585 	/* calculate tx stats */
3586 	if (mss) {
3587 		uint16_t opackets;
3588 		int payload;
3589 
3590 		payload = pkt_size - lso_hdr_size;
3591 		opackets = (payload / mss) + ((payload % mss) != 0);
3592 		tx_info[0].stat.un.all = 0;
3593 		tx_info[0].ostat.opackets = opackets;
3594 		tx_info[0].ostat.obytes = (opackets - 1) * lso_hdr_size
3595 		    + pkt_size;
3596 	} else {
3597 		myri10ge_tx_stat(&tx_info[0].stat,
3598 		    (struct ether_header *)(void *)mp->b_rptr, 1, pkt_size);
3599 	}
3600 	mutex_enter(&tx->lock);
3601 
3602 	/* check to see if the slots are really there */
3603 	avail = tx->mask - (tx->req - tx->done);
3604 	if (unlikely(avail <= count)) {
3605 		mutex_exit(&tx->lock);
3606 		err = 0;
3607 		goto late_stall;
3608 	}
3609 
3610 	myri10ge_send_locked(tx, req_list, tx_info, count);
3611 	mutex_exit(&tx->lock);
3612 	return (DDI_SUCCESS);
3613 
3614 late_stall:
3615 	try_pullup = 0;
3616 	atomic_add_32(&tx->stall_late, 1);
3617 
3618 abort_with_handles:
3619 	/* unbind and free handles from previous mblks */
3620 	for (i = 0; i < count; i++) {
3621 		bp = tx_info[i].m;
3622 		tx_info[i].m = 0;
3623 		if (bp) {
3624 			dma_handle = tx_info[i].handle;
3625 			(void) ddi_dma_unbind_handle(dma_handle->h);
3626 			dma_handle->next = handles;
3627 			handles = dma_handle;
3628 			tx_info[i].handle = NULL;
3629 			tx_info[i].m = NULL;
3630 		}
3631 	}
3632 	myri10ge_free_tx_handle_slist(tx, handles);
3633 pullup:
3634 	if (try_pullup) {
3635 		err = myri10ge_pullup(ss, mp);
3636 		if (err != DDI_SUCCESS && try_pullup == 2) {
3637 			/* drop */
3638 			MYRI10GE_ATOMIC_SLICE_STAT_INC(xmit_err);
3639 			freemsg(mp);
3640 			return (0);
3641 		}
3642 		try_pullup = 0;
3643 		goto again;
3644 	}
3645 
3646 stall:
3647 	if (err != 0) {
3648 		if (err == EBUSY) {
3649 			atomic_add_32(&tx->stall, 1);
3650 		} else {
3651 			MYRI10GE_ATOMIC_SLICE_STAT_INC(xmit_err);
3652 		}
3653 	}
3654 	return (err);
3655 }
3656 
3657 static mblk_t *
3658 myri10ge_send_wrapper(void *arg, mblk_t *mp)
3659 {
3660 	struct myri10ge_slice_state *ss = arg;
3661 	int err = 0;
3662 	mcp_kreq_ether_send_t *req_list;
3663 #if defined(__i386)
3664 	/*
3665 	 * We need about 2.5KB of scratch space to handle transmits.
3666 	 * i86pc has only 8KB of kernel stack space, so we malloc the
3667 	 * scratch space there rather than keeping it on the stack.
3668 	 */
3669 	size_t req_size, tx_info_size;
3670 	struct myri10ge_tx_buffer_state *tx_info;
3671 	caddr_t req_bytes;
3672 
3673 	req_size = sizeof (*req_list) * (MYRI10GE_MAX_SEND_DESC_TSO + 4)
3674 	    + 8;
3675 	req_bytes = kmem_alloc(req_size, KM_SLEEP);
3676 	tx_info_size = sizeof (*tx_info) * (MYRI10GE_MAX_SEND_DESC_TSO + 1);
3677 	tx_info = kmem_alloc(tx_info_size, KM_SLEEP);
3678 #else
3679 	char req_bytes[sizeof (*req_list) * (MYRI10GE_MAX_SEND_DESC_TSO + 4)
3680 	    + 8];
3681 	struct myri10ge_tx_buffer_state tx_info[MYRI10GE_MAX_SEND_DESC_TSO + 1];
3682 #endif
3683 
3684 	/* ensure req_list entries are aligned to 8 bytes */
3685 	req_list = (struct mcp_kreq_ether_send *)
3686 	    (((unsigned long)req_bytes + 7UL) & ~7UL);
3687 
3688 	err = myri10ge_send(ss, mp, req_list, tx_info);
3689 
3690 #if defined(__i386)
3691 	kmem_free(tx_info, tx_info_size);
3692 	kmem_free(req_bytes, req_size);
3693 #endif
3694 	if (err)
3695 		return (mp);
3696 	else
3697 		return (NULL);
3698 }
3699 
3700 static int
3701 myri10ge_addmac(void *arg, const uint8_t *mac_addr)
3702 {
3703 	struct myri10ge_priv *mgp = arg;
3704 	int err;
3705 
3706 	if (mac_addr == NULL)
3707 		return (EINVAL);
3708 
3709 	mutex_enter(&mgp->intrlock);
3710 	if (mgp->macaddr_cnt) {
3711 		mutex_exit(&mgp->intrlock);
3712 		return (ENOSPC);
3713 	}
3714 	err = myri10ge_m_unicst(mgp, mac_addr);
3715 	if (!err)
3716 		mgp->macaddr_cnt++;
3717 
3718 	mutex_exit(&mgp->intrlock);
3719 	if (err)
3720 		return (err);
3721 
3722 	bcopy(mac_addr, mgp->mac_addr, sizeof (mgp->mac_addr));
3723 	return (0);
3724 }
3725 
3726 /*ARGSUSED*/
3727 static int
3728 myri10ge_remmac(void *arg, const uint8_t *mac_addr)
3729 {
3730 	struct myri10ge_priv *mgp = arg;
3731 
3732 	mutex_enter(&mgp->intrlock);
3733 	mgp->macaddr_cnt--;
3734 	mutex_exit(&mgp->intrlock);
3735 
3736 	return (0);
3737 }
3738 
3739 /*ARGSUSED*/
3740 static void
3741 myri10ge_fill_group(void *arg, mac_ring_type_t rtype, const int index,
3742     mac_group_info_t *infop, mac_group_handle_t gh)
3743 {
3744 	struct myri10ge_priv *mgp = arg;
3745 
3746 	if (rtype != MAC_RING_TYPE_RX)
3747 		return;
3748 
3749 	infop->mgi_driver = (mac_group_driver_t)mgp;
3750 	infop->mgi_start = NULL;
3751 	infop->mgi_stop = NULL;
3752 	infop->mgi_addmac = myri10ge_addmac;
3753 	infop->mgi_remmac = myri10ge_remmac;
3754 	infop->mgi_count = mgp->num_slices;
3755 }
3756 
3757 static int
3758 myri10ge_ring_start(mac_ring_driver_t rh, uint64_t mr_gen_num)
3759 {
3760 	struct myri10ge_slice_state *ss;
3761 
3762 	ss = (struct myri10ge_slice_state *)rh;
3763 	mutex_enter(&ss->rx_lock);
3764 	ss->rx_gen_num = mr_gen_num;
3765 	mutex_exit(&ss->rx_lock);
3766 	return (0);
3767 }
3768 
3769 static int
3770 myri10ge_rx_ring_intr_disable(mac_intr_handle_t intrh)
3771 {
3772 	struct myri10ge_slice_state *ss;
3773 
3774 	ss = (struct myri10ge_slice_state *)intrh;
3775 	mutex_enter(&ss->poll_lock);
3776 	ss->rx_polling = B_TRUE;
3777 	mutex_exit(&ss->poll_lock);
3778 	return (0);
3779 }
3780 
3781 static int
3782 myri10ge_rx_ring_intr_enable(mac_intr_handle_t intrh)
3783 {
3784 	struct myri10ge_slice_state *ss;
3785 
3786 	ss = (struct myri10ge_slice_state *)intrh;
3787 	mutex_enter(&ss->poll_lock);
3788 	ss->rx_polling = B_FALSE;
3789 	if (ss->rx_token) {
3790 		*ss->irq_claim = BE_32(3);
3791 		ss->rx_token = 0;
3792 	}
3793 	mutex_exit(&ss->poll_lock);
3794 	return (0);
3795 }
3796 
3797 /*ARGSUSED*/
3798 static void
3799 myri10ge_fill_ring(void *arg, mac_ring_type_t rtype, const int rg_index,
3800     const int ring_index, mac_ring_info_t *infop, mac_ring_handle_t rh)
3801 {
3802 	struct myri10ge_priv *mgp = arg;
3803 	struct myri10ge_slice_state *ss;
3804 	mac_intr_t *mintr = &infop->mri_intr;
3805 
3806 	ASSERT((unsigned int)ring_index < mgp->num_slices);
3807 
3808 	ss = &mgp->ss[ring_index];
3809 	switch (rtype) {
3810 	case MAC_RING_TYPE_RX:
3811 		ss->rx_rh = rh;
3812 		infop->mri_driver = (mac_ring_driver_t)ss;
3813 		infop->mri_start = myri10ge_ring_start;
3814 		infop->mri_stop = NULL;
3815 		infop->mri_poll = myri10ge_poll_rx;
3816 		mintr->mi_handle = (mac_intr_handle_t)ss;
3817 		mintr->mi_enable = myri10ge_rx_ring_intr_enable;
3818 		mintr->mi_disable = myri10ge_rx_ring_intr_disable;
3819 		break;
3820 	case MAC_RING_TYPE_TX:
3821 		ss->tx.rh = rh;
3822 		infop->mri_driver = (mac_ring_driver_t)ss;
3823 		infop->mri_start = NULL;
3824 		infop->mri_stop = NULL;
3825 		infop->mri_tx = myri10ge_send_wrapper;
3826 		break;
3827 	default:
3828 		break;
3829 	}
3830 }
3831 
3832 static void
3833 myri10ge_nic_stat_destroy(struct myri10ge_priv *mgp)
3834 {
3835 	if (mgp->ksp_stat == NULL)
3836 		return;
3837 
3838 	kstat_delete(mgp->ksp_stat);
3839 	mgp->ksp_stat = NULL;
3840 }
3841 
3842 static void
3843 myri10ge_slice_stat_destroy(struct myri10ge_slice_state *ss)
3844 {
3845 	if (ss->ksp_stat == NULL)
3846 		return;
3847 
3848 	kstat_delete(ss->ksp_stat);
3849 	ss->ksp_stat = NULL;
3850 }
3851 
3852 static void
3853 myri10ge_info_destroy(struct myri10ge_priv *mgp)
3854 {
3855 	if (mgp->ksp_info == NULL)
3856 		return;
3857 
3858 	kstat_delete(mgp->ksp_info);
3859 	mgp->ksp_info = NULL;
3860 }
3861 
3862 static int
3863 myri10ge_nic_stat_kstat_update(kstat_t *ksp, int rw)
3864 {
3865 	struct myri10ge_nic_stat *ethstat;
3866 	struct myri10ge_priv *mgp;
3867 	mcp_irq_data_t *fw_stats;
3868 
3869 
3870 	if (rw == KSTAT_WRITE)
3871 		return (EACCES);
3872 
3873 	ethstat = (struct myri10ge_nic_stat *)ksp->ks_data;
3874 	mgp = (struct myri10ge_priv *)ksp->ks_private;
3875 	fw_stats = mgp->ss[0].fw_stats;
3876 
3877 	ethstat->dma_read_bw_MBs.value.ul = mgp->read_dma;
3878 	ethstat->dma_write_bw_MBs.value.ul = mgp->write_dma;
3879 	ethstat->dma_read_write_bw_MBs.value.ul = mgp->read_write_dma;
3880 	if (myri10ge_tx_dma_attr.dma_attr_flags & DDI_DMA_FORCE_PHYSICAL)
3881 		ethstat->dma_force_physical.value.ul = 1;
3882 	else
3883 		ethstat->dma_force_physical.value.ul = 0;
3884 	ethstat->lanes.value.ul = mgp->pcie_link_width;
3885 	ethstat->dropped_bad_crc32.value.ul =
3886 	    ntohl(fw_stats->dropped_bad_crc32);
3887 	ethstat->dropped_bad_phy.value.ul =
3888 	    ntohl(fw_stats->dropped_bad_phy);
3889 	ethstat->dropped_link_error_or_filtered.value.ul =
3890 	    ntohl(fw_stats->dropped_link_error_or_filtered);
3891 	ethstat->dropped_link_overflow.value.ul =
3892 	    ntohl(fw_stats->dropped_link_overflow);
3893 	ethstat->dropped_multicast_filtered.value.ul =
3894 	    ntohl(fw_stats->dropped_multicast_filtered);
3895 	ethstat->dropped_no_big_buffer.value.ul =
3896 	    ntohl(fw_stats->dropped_no_big_buffer);
3897 	ethstat->dropped_no_small_buffer.value.ul =
3898 	    ntohl(fw_stats->dropped_no_small_buffer);
3899 	ethstat->dropped_overrun.value.ul =
3900 	    ntohl(fw_stats->dropped_overrun);
3901 	ethstat->dropped_pause.value.ul =
3902 	    ntohl(fw_stats->dropped_pause);
3903 	ethstat->dropped_runt.value.ul =
3904 	    ntohl(fw_stats->dropped_runt);
3905 	ethstat->link_up.value.ul =
3906 	    ntohl(fw_stats->link_up);
3907 	ethstat->dropped_unicast_filtered.value.ul =
3908 	    ntohl(fw_stats->dropped_unicast_filtered);
3909 	return (0);
3910 }
3911 
3912 static int
3913 myri10ge_slice_stat_kstat_update(kstat_t *ksp, int rw)
3914 {
3915 	struct myri10ge_slice_stat *ethstat;
3916 	struct myri10ge_slice_state *ss;
3917 
3918 	if (rw == KSTAT_WRITE)
3919 		return (EACCES);
3920 
3921 	ethstat = (struct myri10ge_slice_stat *)ksp->ks_data;
3922 	ss = (struct myri10ge_slice_state *)ksp->ks_private;
3923 
3924 	ethstat->rx_big.value.ul = ss->j_rx_cnt;
3925 	ethstat->rx_bigbuf_firmware.value.ul = ss->rx_big.cnt - ss->j_rx_cnt;
3926 	ethstat->rx_bigbuf_pool.value.ul =
3927 	    ss->jpool.num_alloc - ss->jbufs_for_smalls;
3928 	ethstat->rx_bigbuf_smalls.value.ul = ss->jbufs_for_smalls;
3929 	ethstat->rx_small.value.ul = ss->rx_small.cnt -
3930 	    (ss->rx_small.mask + 1);
3931 	ethstat->tx_done.value.ul = ss->tx.done;
3932 	ethstat->tx_req.value.ul = ss->tx.req;
3933 	ethstat->tx_activate.value.ul = ss->tx.activate;
3934 	ethstat->xmit_sched.value.ul = ss->tx.sched;
3935 	ethstat->xmit_stall.value.ul = ss->tx.stall;
3936 	ethstat->xmit_stall_early.value.ul = ss->tx.stall_early;
3937 	ethstat->xmit_stall_late.value.ul = ss->tx.stall_late;
3938 	ethstat->xmit_err.value.ul =  MYRI10GE_SLICE_STAT(xmit_err);
3939 	return (0);
3940 }
3941 
3942 static int
3943 myri10ge_info_kstat_update(kstat_t *ksp, int rw)
3944 {
3945 	struct myri10ge_info *info;
3946 	struct myri10ge_priv *mgp;
3947 
3948 
3949 	if (rw == KSTAT_WRITE)
3950 		return (EACCES);
3951 
3952 	info = (struct myri10ge_info *)ksp->ks_data;
3953 	mgp = (struct myri10ge_priv *)ksp->ks_private;
3954 	kstat_named_setstr(&info->driver_version, MYRI10GE_VERSION_STR);
3955 	kstat_named_setstr(&info->firmware_version, mgp->fw_version);
3956 	kstat_named_setstr(&info->firmware_name, mgp->fw_name);
3957 	kstat_named_setstr(&info->interrupt_type, mgp->intr_type);
3958 	kstat_named_setstr(&info->product_code, mgp->pc_str);
3959 	kstat_named_setstr(&info->serial_number, mgp->sn_str);
3960 	return (0);
3961 }
3962 
3963 static struct myri10ge_info myri10ge_info_template = {
3964 	{ "driver_version",	KSTAT_DATA_STRING },
3965 	{ "firmware_version",	KSTAT_DATA_STRING },
3966 	{ "firmware_name",	KSTAT_DATA_STRING },
3967 	{ "interrupt_type",	KSTAT_DATA_STRING },
3968 	{ "product_code",	KSTAT_DATA_STRING },
3969 	{ "serial_number",	KSTAT_DATA_STRING },
3970 };
3971 static kmutex_t myri10ge_info_template_lock;
3972 
3973 
3974 static int
3975 myri10ge_info_init(struct myri10ge_priv *mgp)
3976 {
3977 	struct kstat *ksp;
3978 
3979 	ksp = kstat_create("myri10ge", ddi_get_instance(mgp->dip),
3980 	    "myri10ge_info", "net", KSTAT_TYPE_NAMED,
3981 	    sizeof (myri10ge_info_template) /
3982 	    sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
3983 	if (ksp == NULL) {
3984 		cmn_err(CE_WARN,
3985 		    "%s: myri10ge_info_init: kstat_create failed", mgp->name);
3986 		return (DDI_FAILURE);
3987 	}
3988 	mgp->ksp_info = ksp;
3989 	ksp->ks_update = myri10ge_info_kstat_update;
3990 	ksp->ks_private = (void *) mgp;
3991 	ksp->ks_data = &myri10ge_info_template;
3992 	ksp->ks_lock = &myri10ge_info_template_lock;
3993 	if (MYRI10GE_VERSION_STR != NULL)
3994 		ksp->ks_data_size += strlen(MYRI10GE_VERSION_STR) + 1;
3995 	if (mgp->fw_version != NULL)
3996 		ksp->ks_data_size += strlen(mgp->fw_version) + 1;
3997 	ksp->ks_data_size += strlen(mgp->fw_name) + 1;
3998 	ksp->ks_data_size += strlen(mgp->intr_type) + 1;
3999 	if (mgp->pc_str != NULL)
4000 		ksp->ks_data_size += strlen(mgp->pc_str) + 1;
4001 	if (mgp->sn_str != NULL)
4002 		ksp->ks_data_size += strlen(mgp->sn_str) + 1;
4003 
4004 	kstat_install(ksp);
4005 	return (DDI_SUCCESS);
4006 }
4007 
4008 
4009 static int
4010 myri10ge_nic_stat_init(struct myri10ge_priv *mgp)
4011 {
4012 	struct kstat *ksp;
4013 	struct myri10ge_nic_stat *ethstat;
4014 
4015 	ksp = kstat_create("myri10ge", ddi_get_instance(mgp->dip),
4016 	    "myri10ge_nic_stats", "net", KSTAT_TYPE_NAMED,
4017 	    sizeof (*ethstat) / sizeof (kstat_named_t), 0);
4018 	if (ksp == NULL) {
4019 		cmn_err(CE_WARN,
4020 		    "%s: myri10ge_stat_init: kstat_create failed", mgp->name);
4021 		return (DDI_FAILURE);
4022 	}
4023 	mgp->ksp_stat = ksp;
4024 	ethstat = (struct myri10ge_nic_stat *)(ksp->ks_data);
4025 
4026 	kstat_named_init(&ethstat->dma_read_bw_MBs,
4027 	    "dma_read_bw_MBs", KSTAT_DATA_ULONG);
4028 	kstat_named_init(&ethstat->dma_write_bw_MBs,
4029 	    "dma_write_bw_MBs", KSTAT_DATA_ULONG);
4030 	kstat_named_init(&ethstat->dma_read_write_bw_MBs,
4031 	    "dma_read_write_bw_MBs", KSTAT_DATA_ULONG);
4032 	kstat_named_init(&ethstat->dma_force_physical,
4033 	    "dma_force_physical", KSTAT_DATA_ULONG);
4034 	kstat_named_init(&ethstat->lanes,
4035 	    "lanes", KSTAT_DATA_ULONG);
4036 	kstat_named_init(&ethstat->dropped_bad_crc32,
4037 	    "dropped_bad_crc32", KSTAT_DATA_ULONG);
4038 	kstat_named_init(&ethstat->dropped_bad_phy,
4039 	    "dropped_bad_phy", KSTAT_DATA_ULONG);
4040 	kstat_named_init(&ethstat->dropped_link_error_or_filtered,
4041 	    "dropped_link_error_or_filtered", KSTAT_DATA_ULONG);
4042 	kstat_named_init(&ethstat->dropped_link_overflow,
4043 	    "dropped_link_overflow", KSTAT_DATA_ULONG);
4044 	kstat_named_init(&ethstat->dropped_multicast_filtered,
4045 	    "dropped_multicast_filtered", KSTAT_DATA_ULONG);
4046 	kstat_named_init(&ethstat->dropped_no_big_buffer,
4047 	    "dropped_no_big_buffer", KSTAT_DATA_ULONG);
4048 	kstat_named_init(&ethstat->dropped_no_small_buffer,
4049 	    "dropped_no_small_buffer", KSTAT_DATA_ULONG);
4050 	kstat_named_init(&ethstat->dropped_overrun,
4051 	    "dropped_overrun", KSTAT_DATA_ULONG);
4052 	kstat_named_init(&ethstat->dropped_pause,
4053 	    "dropped_pause", KSTAT_DATA_ULONG);
4054 	kstat_named_init(&ethstat->dropped_runt,
4055 	    "dropped_runt", KSTAT_DATA_ULONG);
4056 	kstat_named_init(&ethstat->dropped_unicast_filtered,
4057 	    "dropped_unicast_filtered", KSTAT_DATA_ULONG);
4058 	kstat_named_init(&ethstat->dropped_runt, "dropped_runt",
4059 	    KSTAT_DATA_ULONG);
4060 	kstat_named_init(&ethstat->link_up, "link_up", KSTAT_DATA_ULONG);
4061 	kstat_named_init(&ethstat->link_changes, "link_changes",
4062 	    KSTAT_DATA_ULONG);
4063 	ksp->ks_update = myri10ge_nic_stat_kstat_update;
4064 	ksp->ks_private = (void *) mgp;
4065 	kstat_install(ksp);
4066 	return (DDI_SUCCESS);
4067 }
4068 
4069 static int
4070 myri10ge_slice_stat_init(struct myri10ge_slice_state *ss)
4071 {
4072 	struct myri10ge_priv *mgp = ss->mgp;
4073 	struct kstat *ksp;
4074 	struct myri10ge_slice_stat *ethstat;
4075 	int instance;
4076 
4077 	/*
4078 	 * fake an instance so that the same slice numbers from
4079 	 * different instances do not collide
4080 	 */
4081 	instance = (ddi_get_instance(mgp->dip) * 1000) +  (int)(ss - mgp->ss);
4082 	ksp = kstat_create("myri10ge", instance,
4083 	    "myri10ge_slice_stats", "net", KSTAT_TYPE_NAMED,
4084 	    sizeof (*ethstat) / sizeof (kstat_named_t), 0);
4085 	if (ksp == NULL) {
4086 		cmn_err(CE_WARN,
4087 		    "%s: myri10ge_stat_init: kstat_create failed", mgp->name);
4088 		return (DDI_FAILURE);
4089 	}
4090 	ss->ksp_stat = ksp;
4091 	ethstat = (struct myri10ge_slice_stat *)(ksp->ks_data);
4092 	kstat_named_init(&ethstat->lro_bad_csum, "lro_bad_csum",
4093 	    KSTAT_DATA_ULONG);
4094 	kstat_named_init(&ethstat->lro_flushed, "lro_flushed",
4095 	    KSTAT_DATA_ULONG);
4096 	kstat_named_init(&ethstat->lro_queued, "lro_queued",
4097 	    KSTAT_DATA_ULONG);
4098 	kstat_named_init(&ethstat->rx_bigbuf_firmware, "rx_bigbuf_firmware",
4099 	    KSTAT_DATA_ULONG);
4100 	kstat_named_init(&ethstat->rx_bigbuf_pool, "rx_bigbuf_pool",
4101 	    KSTAT_DATA_ULONG);
4102 	kstat_named_init(&ethstat->rx_bigbuf_smalls, "rx_bigbuf_smalls",
4103 	    KSTAT_DATA_ULONG);
4104 	kstat_named_init(&ethstat->rx_copy, "rx_copy",
4105 	    KSTAT_DATA_ULONG);
4106 	kstat_named_init(&ethstat->rx_big_nobuf, "rx_big_nobuf",
4107 	    KSTAT_DATA_ULONG);
4108 	kstat_named_init(&ethstat->rx_small_nobuf, "rx_small_nobuf",
4109 	    KSTAT_DATA_ULONG);
4110 	kstat_named_init(&ethstat->xmit_zero_len, "xmit_zero_len",
4111 	    KSTAT_DATA_ULONG);
4112 	kstat_named_init(&ethstat->xmit_pullup, "xmit_pullup",
4113 	    KSTAT_DATA_ULONG);
4114 	kstat_named_init(&ethstat->xmit_pullup_first, "xmit_pullup_first",
4115 	    KSTAT_DATA_ULONG);
4116 	kstat_named_init(&ethstat->xmit_lowbuf, "xmit_lowbuf",
4117 	    KSTAT_DATA_ULONG);
4118 	kstat_named_init(&ethstat->xmit_lsobadflags, "xmit_lsobadflags",
4119 	    KSTAT_DATA_ULONG);
4120 	kstat_named_init(&ethstat->xmit_sched, "xmit_sched",
4121 	    KSTAT_DATA_ULONG);
4122 	kstat_named_init(&ethstat->xmit_stall, "xmit_stall",
4123 	    KSTAT_DATA_ULONG);
4124 	kstat_named_init(&ethstat->xmit_stall_early, "xmit_stall_early",
4125 	    KSTAT_DATA_ULONG);
4126 	kstat_named_init(&ethstat->xmit_stall_late, "xmit_stall_late",
4127 	    KSTAT_DATA_ULONG);
4128 	kstat_named_init(&ethstat->xmit_err, "xmit_err",
4129 	    KSTAT_DATA_ULONG);
4130 	kstat_named_init(&ethstat->tx_req, "tx_req",
4131 	    KSTAT_DATA_ULONG);
4132 	kstat_named_init(&ethstat->tx_activate, "tx_activate",
4133 	    KSTAT_DATA_ULONG);
4134 	kstat_named_init(&ethstat->tx_done, "tx_done",
4135 	    KSTAT_DATA_ULONG);
4136 	kstat_named_init(&ethstat->tx_handles_alloced, "tx_handles_alloced",
4137 	    KSTAT_DATA_ULONG);
4138 	kstat_named_init(&ethstat->rx_big, "rx_big",
4139 	    KSTAT_DATA_ULONG);
4140 	kstat_named_init(&ethstat->rx_small, "rx_small",
4141 	    KSTAT_DATA_ULONG);
4142 	ksp->ks_update = myri10ge_slice_stat_kstat_update;
4143 	ksp->ks_private = (void *) ss;
4144 	kstat_install(ksp);
4145 	return (DDI_SUCCESS);
4146 }
4147 
4148 
4149 
4150 #if #cpu(i386) || defined __i386 || defined i386 ||	\
4151 	defined __i386__ || #cpu(x86_64) || defined __x86_64__
4152 
4153 #include <vm/hat.h>
4154 #include <sys/ddi_isa.h>
4155 void *device_arena_alloc(size_t size, int vm_flag);
4156 void device_arena_free(void *vaddr, size_t size);
4157 
4158 static void
4159 myri10ge_enable_nvidia_ecrc(struct myri10ge_priv *mgp)
4160 {
4161 	dev_info_t *parent_dip;
4162 	ddi_acc_handle_t handle;
4163 	unsigned long bus_number, dev_number, func_number;
4164 	unsigned long cfg_pa, paddr, base, pgoffset;
4165 	char 		*cvaddr, *ptr;
4166 	uint32_t	*ptr32;
4167 	int 		retval = DDI_FAILURE;
4168 	int dontcare;
4169 	uint16_t read_vid, read_did, vendor_id, device_id;
4170 
4171 	if (!myri10ge_nvidia_ecrc_enable)
4172 		return;
4173 
4174 	parent_dip = ddi_get_parent(mgp->dip);
4175 	if (parent_dip == NULL) {
4176 		cmn_err(CE_WARN, "%s: I'm an orphan?", mgp->name);
4177 		return;
4178 	}
4179 
4180 	if (pci_config_setup(parent_dip, &handle) != DDI_SUCCESS) {
4181 		cmn_err(CE_WARN,
4182 		    "%s: Could not access my parent's registers", mgp->name);
4183 		return;
4184 	}
4185 
4186 	vendor_id = pci_config_get16(handle, PCI_CONF_VENID);
4187 	device_id = pci_config_get16(handle, PCI_CONF_DEVID);
4188 	pci_config_teardown(&handle);
4189 
4190 	if (myri10ge_verbose) {
4191 		unsigned long 	bus_number, dev_number, func_number;
4192 		int 		reg_set, span;
4193 		(void) myri10ge_reg_set(parent_dip, &reg_set, &span,
4194 		    &bus_number, &dev_number, &func_number);
4195 		if (myri10ge_verbose)
4196 			printf("%s: parent at %ld:%ld:%ld\n", mgp->name,
4197 			    bus_number, dev_number, func_number);
4198 	}
4199 
4200 	if (vendor_id !=  0x10de)
4201 		return;
4202 
4203 	if (device_id != 0x005d /* CK804 */ &&
4204 	    (device_id < 0x374 || device_id > 0x378) /* MCP55 */) {
4205 		return;
4206 	}
4207 	(void) myri10ge_reg_set(parent_dip, &dontcare, &dontcare,
4208 	    &bus_number, &dev_number, &func_number);
4209 
4210 	for (cfg_pa = 0xf0000000UL;
4211 	    retval != DDI_SUCCESS && cfg_pa >= 0xe0000000UL;
4212 	    cfg_pa -= 0x10000000UL) {
4213 		/* find the config space address for the nvidia bridge */
4214 		paddr = (cfg_pa + bus_number * 0x00100000UL +
4215 		    (dev_number * 8 + func_number) * 0x00001000UL);
4216 
4217 		base = paddr & (~MMU_PAGEOFFSET);
4218 		pgoffset = paddr & MMU_PAGEOFFSET;
4219 
4220 		/* map it into the kernel */
4221 		cvaddr =  device_arena_alloc(ptob(1), VM_NOSLEEP);
4222 		if (cvaddr == NULL)
4223 			cmn_err(CE_WARN, "%s: failed to map nf4: cvaddr\n",
4224 			    mgp->name);
4225 
4226 		hat_devload(kas.a_hat, cvaddr, mmu_ptob(1),
4227 		    i_ddi_paddr_to_pfn(base),
4228 		    PROT_WRITE|HAT_STRICTORDER, HAT_LOAD_LOCK);
4229 
4230 		ptr = cvaddr + pgoffset;
4231 		read_vid = *(uint16_t *)(void *)(ptr + PCI_CONF_VENID);
4232 		read_did = *(uint16_t *)(void *)(ptr + PCI_CONF_DEVID);
4233 		if (vendor_id ==  read_did || device_id == read_did) {
4234 			ptr32 = (uint32_t *)(void *)(ptr + 0x178);
4235 			if (myri10ge_verbose)
4236 				printf("%s: Enabling ECRC on upstream "
4237 				    "Nvidia bridge (0x%x:0x%x) "
4238 				    "at %ld:%ld:%ld\n", mgp->name,
4239 				    read_vid, read_did, bus_number,
4240 				    dev_number, func_number);
4241 			*ptr32 |= 0x40;
4242 			retval = DDI_SUCCESS;
4243 		}
4244 		hat_unload(kas.a_hat, cvaddr, ptob(1), HAT_UNLOAD_UNLOCK);
4245 		device_arena_free(cvaddr, ptob(1));
4246 	}
4247 }
4248 
4249 #else
4250 /*ARGSUSED*/
4251 static void
4252 myri10ge_enable_nvidia_ecrc(struct myri10ge_priv *mgp)
4253 {
4254 }
4255 #endif /* i386 */
4256 
4257 
4258 /*
4259  * The Lanai Z8E PCI-E interface achieves higher Read-DMA throughput
4260  * when the PCI-E Completion packets are aligned on an 8-byte
4261  * boundary.  Some PCI-E chip sets always align Completion packets; on
4262  * the ones that do not, the alignment can be enforced by enabling
4263  * ECRC generation (if supported).
4264  *
4265  * When PCI-E Completion packets are not aligned, it is actually more
4266  * efficient to limit Read-DMA transactions to 2KB, rather than 4KB.
4267  *
4268  * If the driver can neither enable ECRC nor verify that it has
4269  * already been enabled, then it must use a firmware image which works
4270  * around unaligned completion packets (ethp_z8e.dat), and it should
4271  * also ensure that it never gives the device a Read-DMA which is
4272  * larger than 2KB by setting the tx.boundary to 2KB.  If ECRC is
4273  * enabled, then the driver should use the aligned (eth_z8e.dat)
4274  * firmware image, and set tx.boundary to 4KB.
4275  */
4276 
4277 
4278 static int
4279 myri10ge_firmware_probe(struct myri10ge_priv *mgp)
4280 {
4281 	int status;
4282 
4283 	mgp->tx_boundary = 4096;
4284 	/*
4285 	 * Verify the max read request size was set to 4KB
4286 	 * before trying the test with 4KB.
4287 	 */
4288 	if (mgp->max_read_request_4k == 0)
4289 		mgp->tx_boundary = 2048;
4290 	/*
4291 	 * load the optimized firmware which assumes aligned PCIe
4292 	 * completions in order to see if it works on this host.
4293 	 */
4294 
4295 	mgp->fw_name = "rss_eth_z8e";
4296 	mgp->eth_z8e = (unsigned char *)rss_eth_z8e;
4297 	mgp->eth_z8e_length = rss_eth_z8e_length;
4298 
4299 	status = myri10ge_load_firmware(mgp);
4300 	if (status != 0) {
4301 		return (status);
4302 	}
4303 	/*
4304 	 * Enable ECRC if possible
4305 	 */
4306 	myri10ge_enable_nvidia_ecrc(mgp);
4307 
4308 	/*
4309 	 * Run a DMA test which watches for unaligned completions and
4310 	 * aborts on the first one seen.
4311 	 */
4312 	status = myri10ge_dma_test(mgp, MXGEFW_CMD_UNALIGNED_TEST);
4313 	if (status == 0)
4314 		return (0); /* keep the aligned firmware */
4315 
4316 	if (status != E2BIG)
4317 		cmn_err(CE_WARN, "%s: DMA test failed: %d\n",
4318 		    mgp->name, status);
4319 	if (status == ENOSYS)
4320 		cmn_err(CE_WARN, "%s: Falling back to ethp! "
4321 		    "Please install up to date fw\n", mgp->name);
4322 	return (status);
4323 }
4324 
4325 static int
4326 myri10ge_select_firmware(struct myri10ge_priv *mgp)
4327 {
4328 	int aligned;
4329 
4330 	aligned = 0;
4331 
4332 	if (myri10ge_force_firmware == 1) {
4333 		if (myri10ge_verbose)
4334 			printf("%s: Assuming aligned completions (forced)\n",
4335 			    mgp->name);
4336 		aligned = 1;
4337 		goto done;
4338 	}
4339 
4340 	if (myri10ge_force_firmware == 2) {
4341 		if (myri10ge_verbose)
4342 			printf("%s: Assuming unaligned completions (forced)\n",
4343 			    mgp->name);
4344 		aligned = 0;
4345 		goto done;
4346 	}
4347 
4348 	/* If the width is less than 8, we may used the aligned firmware */
4349 	if (mgp->pcie_link_width != 0 && mgp->pcie_link_width < 8) {
4350 		cmn_err(CE_WARN, "!%s: PCIe link running at x%d\n",
4351 		    mgp->name, mgp->pcie_link_width);
4352 		aligned = 1;
4353 		goto done;
4354 	}
4355 
4356 	if (0 == myri10ge_firmware_probe(mgp))
4357 		return (0);  /* keep optimized firmware */
4358 
4359 done:
4360 	if (aligned) {
4361 		mgp->fw_name = "rss_eth_z8e";
4362 		mgp->eth_z8e = (unsigned char *)rss_eth_z8e;
4363 		mgp->eth_z8e_length = rss_eth_z8e_length;
4364 		mgp->tx_boundary = 4096;
4365 	} else {
4366 		mgp->fw_name = "rss_ethp_z8e";
4367 		mgp->eth_z8e = (unsigned char *)rss_ethp_z8e;
4368 		mgp->eth_z8e_length = rss_ethp_z8e_length;
4369 		mgp->tx_boundary = 2048;
4370 	}
4371 
4372 	return (myri10ge_load_firmware(mgp));
4373 }
4374 
4375 static int
4376 myri10ge_add_intrs(struct myri10ge_priv *mgp, int add_handler)
4377 {
4378 	dev_info_t *devinfo = mgp->dip;
4379 	int count, avail, actual, intr_types;
4380 	int x, y, rc, inum = 0;
4381 
4382 
4383 	rc = ddi_intr_get_supported_types(devinfo, &intr_types);
4384 	if (rc != DDI_SUCCESS) {
4385 		cmn_err(CE_WARN,
4386 		    "!%s: ddi_intr_get_nintrs() failure, rc = %d\n", mgp->name,
4387 		    rc);
4388 		return (DDI_FAILURE);
4389 	}
4390 
4391 	if (!myri10ge_use_msi)
4392 		intr_types &= ~DDI_INTR_TYPE_MSI;
4393 	if (!myri10ge_use_msix)
4394 		intr_types &= ~DDI_INTR_TYPE_MSIX;
4395 
4396 	if (intr_types & DDI_INTR_TYPE_MSIX) {
4397 		mgp->ddi_intr_type = DDI_INTR_TYPE_MSIX;
4398 		mgp->intr_type = "MSI-X";
4399 	} else if (intr_types & DDI_INTR_TYPE_MSI) {
4400 		mgp->ddi_intr_type = DDI_INTR_TYPE_MSI;
4401 		mgp->intr_type = "MSI";
4402 	} else {
4403 		mgp->ddi_intr_type = DDI_INTR_TYPE_FIXED;
4404 		mgp->intr_type = "Legacy";
4405 	}
4406 	/* Get number of interrupts */
4407 	rc = ddi_intr_get_nintrs(devinfo, mgp->ddi_intr_type, &count);
4408 	if ((rc != DDI_SUCCESS) || (count == 0)) {
4409 		cmn_err(CE_WARN, "%s: ddi_intr_get_nintrs() failure, rc: %d, "
4410 		    "count: %d", mgp->name, rc, count);
4411 
4412 		return (DDI_FAILURE);
4413 	}
4414 
4415 	/* Get number of available interrupts */
4416 	rc = ddi_intr_get_navail(devinfo, mgp->ddi_intr_type, &avail);
4417 	if ((rc != DDI_SUCCESS) || (avail == 0)) {
4418 		cmn_err(CE_WARN, "%s: ddi_intr_get_navail() failure, "
4419 		    "rc: %d, avail: %d\n", mgp->name, rc, avail);
4420 		return (DDI_FAILURE);
4421 	}
4422 	if (avail < count) {
4423 		cmn_err(CE_NOTE,
4424 		    "!%s: nintrs() returned %d, navail returned %d",
4425 		    mgp->name, count, avail);
4426 		count = avail;
4427 	}
4428 
4429 	if (count < mgp->num_slices)
4430 		return (DDI_FAILURE);
4431 
4432 	if (count > mgp->num_slices)
4433 		count = mgp->num_slices;
4434 
4435 	/* Allocate memory for MSI interrupts */
4436 	mgp->intr_size = count * sizeof (ddi_intr_handle_t);
4437 	mgp->htable = kmem_alloc(mgp->intr_size, KM_SLEEP);
4438 
4439 	rc = ddi_intr_alloc(devinfo, mgp->htable, mgp->ddi_intr_type, inum,
4440 	    count, &actual, DDI_INTR_ALLOC_NORMAL);
4441 
4442 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
4443 		cmn_err(CE_WARN, "%s: ddi_intr_alloc() failed: %d",
4444 		    mgp->name, rc);
4445 
4446 		kmem_free(mgp->htable, mgp->intr_size);
4447 		mgp->htable = NULL;
4448 		return (DDI_FAILURE);
4449 	}
4450 
4451 	if ((actual < count) && myri10ge_verbose) {
4452 		cmn_err(CE_NOTE, "%s: got %d/%d slices",
4453 		    mgp->name, actual, count);
4454 	}
4455 
4456 	mgp->intr_cnt = actual;
4457 
4458 	/*
4459 	 * Get priority for first irq, assume remaining are all the same
4460 	 */
4461 	if (ddi_intr_get_pri(mgp->htable[0], &mgp->intr_pri)
4462 	    != DDI_SUCCESS) {
4463 		cmn_err(CE_WARN, "%s: ddi_intr_get_pri() failed", mgp->name);
4464 
4465 		/* Free already allocated intr */
4466 		for (y = 0; y < actual; y++) {
4467 			(void) ddi_intr_free(mgp->htable[y]);
4468 		}
4469 
4470 		kmem_free(mgp->htable, mgp->intr_size);
4471 		mgp->htable = NULL;
4472 		return (DDI_FAILURE);
4473 	}
4474 
4475 	mgp->icookie = (void *)(uintptr_t)mgp->intr_pri;
4476 
4477 	if (!add_handler)
4478 		return (DDI_SUCCESS);
4479 
4480 	/* Call ddi_intr_add_handler() */
4481 	for (x = 0; x < actual; x++) {
4482 		if (ddi_intr_add_handler(mgp->htable[x], myri10ge_intr,
4483 		    (caddr_t)&mgp->ss[x], NULL) != DDI_SUCCESS) {
4484 			cmn_err(CE_WARN, "%s: ddi_intr_add_handler() failed",
4485 			    mgp->name);
4486 
4487 			/* Free already allocated intr */
4488 			for (y = 0; y < actual; y++) {
4489 				(void) ddi_intr_free(mgp->htable[y]);
4490 			}
4491 
4492 			kmem_free(mgp->htable, mgp->intr_size);
4493 			mgp->htable = NULL;
4494 			return (DDI_FAILURE);
4495 		}
4496 	}
4497 
4498 	(void) ddi_intr_get_cap(mgp->htable[0], &mgp->intr_cap);
4499 	if (mgp->intr_cap & DDI_INTR_FLAG_BLOCK) {
4500 		/* Call ddi_intr_block_enable() for MSI */
4501 		(void) ddi_intr_block_enable(mgp->htable, mgp->intr_cnt);
4502 	} else {
4503 		/* Call ddi_intr_enable() for MSI non block enable */
4504 		for (x = 0; x < mgp->intr_cnt; x++) {
4505 			(void) ddi_intr_enable(mgp->htable[x]);
4506 		}
4507 	}
4508 
4509 	return (DDI_SUCCESS);
4510 }
4511 
4512 static void
4513 myri10ge_rem_intrs(struct myri10ge_priv *mgp, int handler_installed)
4514 {
4515 	int x, err;
4516 
4517 	/* Disable all interrupts */
4518 	if (handler_installed) {
4519 		if (mgp->intr_cap & DDI_INTR_FLAG_BLOCK) {
4520 			/* Call ddi_intr_block_disable() */
4521 			(void) ddi_intr_block_disable(mgp->htable,
4522 			    mgp->intr_cnt);
4523 		} else {
4524 			for (x = 0; x < mgp->intr_cnt; x++) {
4525 				(void) ddi_intr_disable(mgp->htable[x]);
4526 			}
4527 		}
4528 	}
4529 
4530 	for (x = 0; x < mgp->intr_cnt; x++) {
4531 		if (handler_installed) {
4532 		/* Call ddi_intr_remove_handler() */
4533 			err = ddi_intr_remove_handler(mgp->htable[x]);
4534 			if (err != DDI_SUCCESS) {
4535 				cmn_err(CE_WARN,
4536 				    "%s: ddi_intr_remove_handler for"
4537 				    "vec %d returned %d\n", mgp->name,
4538 				    x, err);
4539 			}
4540 		}
4541 		err = ddi_intr_free(mgp->htable[x]);
4542 		if (err != DDI_SUCCESS) {
4543 			cmn_err(CE_WARN,
4544 			    "%s: ddi_intr_free for vec %d returned %d\n",
4545 			    mgp->name, x, err);
4546 		}
4547 	}
4548 	kmem_free(mgp->htable, mgp->intr_size);
4549 	mgp->htable = NULL;
4550 }
4551 
4552 static void
4553 myri10ge_test_physical(dev_info_t *dip)
4554 {
4555 	ddi_dma_handle_t	handle;
4556 	struct myri10ge_dma_stuff dma;
4557 	void *addr;
4558 	int err;
4559 
4560 	/* test #1, sufficient for older sparc systems */
4561 	myri10ge_tx_dma_attr.dma_attr_flags = DDI_DMA_FORCE_PHYSICAL;
4562 	err = ddi_dma_alloc_handle(dip, &myri10ge_tx_dma_attr,
4563 	    DDI_DMA_DONTWAIT, NULL, &handle);
4564 	if (err == DDI_DMA_BADATTR)
4565 		goto fail;
4566 	ddi_dma_free_handle(&handle);
4567 
4568 	/* test #2, required on Olympis where the bind is what fails */
4569 	addr = myri10ge_dma_alloc(dip, 128, &myri10ge_tx_dma_attr,
4570 	    &myri10ge_dev_access_attr, DDI_DMA_STREAMING,
4571 	    DDI_DMA_WRITE|DDI_DMA_STREAMING, &dma, 0, DDI_DMA_DONTWAIT);
4572 	if (addr == NULL)
4573 		goto fail;
4574 	myri10ge_dma_free(&dma);
4575 	return;
4576 
4577 fail:
4578 	if (myri10ge_verbose)
4579 		printf("myri10ge%d: DDI_DMA_FORCE_PHYSICAL failed, "
4580 		    "using IOMMU\n", ddi_get_instance(dip));
4581 
4582 	myri10ge_tx_dma_attr.dma_attr_flags &= ~DDI_DMA_FORCE_PHYSICAL;
4583 }
4584 
4585 static void
4586 myri10ge_get_props(dev_info_t *dip)
4587 {
4588 
4589 	myri10ge_flow_control =  ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4590 	    "myri10ge_flow_control", myri10ge_flow_control);
4591 
4592 	myri10ge_intr_coal_delay = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4593 	    "myri10ge_intr_coal_delay", myri10ge_intr_coal_delay);
4594 
4595 #if #cpu(i386) || defined __i386 || defined i386 ||	\
4596 	defined __i386__ || #cpu(x86_64) || defined __x86_64__
4597 	myri10ge_nvidia_ecrc_enable = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4598 	    "myri10ge_nvidia_ecrc_enable", 1);
4599 #endif
4600 
4601 
4602 	myri10ge_use_msi = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4603 	    "myri10ge_use_msi", myri10ge_use_msi);
4604 
4605 	myri10ge_deassert_wait = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4606 	    "myri10ge_deassert_wait",  myri10ge_deassert_wait);
4607 
4608 	myri10ge_verbose = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4609 	    "myri10ge_verbose", myri10ge_verbose);
4610 
4611 	myri10ge_tx_copylen = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4612 	    "myri10ge_tx_copylen", myri10ge_tx_copylen);
4613 
4614 	if (myri10ge_tx_copylen < 60) {
4615 		cmn_err(CE_WARN,
4616 		    "myri10ge_tx_copylen must be >= 60 bytes\n");
4617 		myri10ge_tx_copylen = 60;
4618 	}
4619 
4620 	myri10ge_mtu_override = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4621 	    "myri10ge_mtu_override", myri10ge_mtu_override);
4622 
4623 	if (myri10ge_mtu_override >= 1500 && myri10ge_mtu_override <= 9000)
4624 		myri10ge_mtu = myri10ge_mtu_override +
4625 		    sizeof (struct ether_header) + MXGEFW_PAD + VLAN_TAGSZ;
4626 	else if (myri10ge_mtu_override != 0) {
4627 		cmn_err(CE_WARN,
4628 		    "myri10ge_mtu_override must be between 1500 and "
4629 		    "9000 bytes\n");
4630 	}
4631 
4632 	myri10ge_bigbufs_initial = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4633 	    "myri10ge_bigbufs_initial", myri10ge_bigbufs_initial);
4634 	myri10ge_bigbufs_max = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4635 	    "myri10ge_bigbufs_max", myri10ge_bigbufs_max);
4636 
4637 	myri10ge_watchdog_reset = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4638 	    "myri10ge_watchdog_reset", myri10ge_watchdog_reset);
4639 
4640 	if (myri10ge_bigbufs_initial < 128) {
4641 		cmn_err(CE_WARN,
4642 		    "myri10ge_bigbufs_initial be at least 128\n");
4643 		myri10ge_bigbufs_initial = 128;
4644 	}
4645 	if (myri10ge_bigbufs_max < 128) {
4646 		cmn_err(CE_WARN,
4647 		    "myri10ge_bigbufs_max be at least 128\n");
4648 		myri10ge_bigbufs_max = 128;
4649 	}
4650 
4651 	if (myri10ge_bigbufs_max < myri10ge_bigbufs_initial) {
4652 		cmn_err(CE_WARN,
4653 		    "myri10ge_bigbufs_max must be >=  "
4654 		    "myri10ge_bigbufs_initial\n");
4655 		myri10ge_bigbufs_max = myri10ge_bigbufs_initial;
4656 	}
4657 
4658 	myri10ge_force_firmware = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4659 	    "myri10ge_force_firmware", myri10ge_force_firmware);
4660 
4661 	myri10ge_max_slices = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4662 	    "myri10ge_max_slices", myri10ge_max_slices);
4663 
4664 	myri10ge_use_msix = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4665 	    "myri10ge_use_msix", myri10ge_use_msix);
4666 
4667 	myri10ge_rss_hash = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4668 	    "myri10ge_rss_hash", myri10ge_rss_hash);
4669 
4670 	if (myri10ge_rss_hash > MXGEFW_RSS_HASH_TYPE_MAX ||
4671 	    myri10ge_rss_hash < MXGEFW_RSS_HASH_TYPE_IPV4) {
4672 		cmn_err(CE_WARN, "myri10ge: Illegal rssh hash type %d\n",
4673 		    myri10ge_rss_hash);
4674 		myri10ge_rss_hash = MXGEFW_RSS_HASH_TYPE_SRC_DST_PORT;
4675 	}
4676 	myri10ge_lro = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4677 	    "myri10ge_lro", myri10ge_lro);
4678 	myri10ge_lro_cnt = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4679 	    "myri10ge_lro_cnt", myri10ge_lro_cnt);
4680 	myri10ge_lro_max_aggr = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4681 	    "myri10ge_lro_max_aggr", myri10ge_lro_max_aggr);
4682 	myri10ge_tx_hash = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4683 	    "myri10ge_tx_hash", myri10ge_tx_hash);
4684 	myri10ge_use_lso = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4685 	    "myri10ge_use_lso", myri10ge_use_lso);
4686 	myri10ge_lso_copy = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4687 	    "myri10ge_lso_copy", myri10ge_lso_copy);
4688 	myri10ge_tx_handles_initial = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4689 	    "myri10ge_tx_handles_initial", myri10ge_tx_handles_initial);
4690 	myri10ge_small_bytes = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
4691 	    "myri10ge_small_bytes", myri10ge_small_bytes);
4692 	if ((myri10ge_small_bytes + MXGEFW_PAD) & (128 -1)) {
4693 		cmn_err(CE_WARN, "myri10ge: myri10ge_small_bytes (%d)\n",
4694 		    myri10ge_small_bytes);
4695 		cmn_err(CE_WARN, "must be aligned on 128b bndry -2\n");
4696 		myri10ge_small_bytes += 128;
4697 		myri10ge_small_bytes &= ~(128 -1);
4698 		myri10ge_small_bytes -= MXGEFW_PAD;
4699 		cmn_err(CE_WARN, "rounded up to %d\n",
4700 		    myri10ge_small_bytes);
4701 
4702 		myri10ge_rss_hash = MXGEFW_RSS_HASH_TYPE_SRC_DST_PORT;
4703 	}
4704 }
4705 
4706 #ifndef	PCI_EXP_LNKSTA
4707 #define	PCI_EXP_LNKSTA 18
4708 #endif
4709 
4710 static int
4711 myri10ge_find_cap(ddi_acc_handle_t handle, uint8_t *capptr, uint8_t capid)
4712 {
4713 	uint16_t	status;
4714 	uint8_t 	ptr;
4715 
4716 	/* check to see if we have capabilities */
4717 	status = pci_config_get16(handle, PCI_CONF_STAT);
4718 	if (!(status & PCI_STAT_CAP)) {
4719 		cmn_err(CE_WARN, "PCI_STAT_CAP not found\n");
4720 		return (ENXIO);
4721 	}
4722 
4723 	ptr = pci_config_get8(handle, PCI_CONF_CAP_PTR);
4724 
4725 	/* Walk the capabilities list, looking for a PCI Express cap */
4726 	while (ptr != PCI_CAP_NEXT_PTR_NULL) {
4727 		if (pci_config_get8(handle, ptr + PCI_CAP_ID) == capid)
4728 			break;
4729 		ptr = pci_config_get8(handle, ptr + PCI_CAP_NEXT_PTR);
4730 	}
4731 	if (ptr < 64) {
4732 		cmn_err(CE_WARN, "Bad capability offset %d\n", ptr);
4733 		return (ENXIO);
4734 	}
4735 	*capptr = ptr;
4736 	return (0);
4737 }
4738 
4739 static int
4740 myri10ge_set_max_readreq(ddi_acc_handle_t handle)
4741 {
4742 	int err;
4743 	uint16_t	val;
4744 	uint8_t		ptr;
4745 
4746 	err = myri10ge_find_cap(handle, &ptr, PCI_CAP_ID_PCI_E);
4747 	if (err != 0) {
4748 		cmn_err(CE_WARN, "could not find PCIe cap\n");
4749 		return (ENXIO);
4750 	}
4751 
4752 	/* set max read req to 4096 */
4753 	val = pci_config_get16(handle, ptr + PCIE_DEVCTL);
4754 	val = (val & ~PCIE_DEVCTL_MAX_READ_REQ_MASK) |
4755 	    PCIE_DEVCTL_MAX_READ_REQ_4096;
4756 	pci_config_put16(handle, ptr + PCIE_DEVCTL, val);
4757 	val = pci_config_get16(handle, ptr + PCIE_DEVCTL);
4758 	if ((val & (PCIE_DEVCTL_MAX_READ_REQ_4096)) !=
4759 	    PCIE_DEVCTL_MAX_READ_REQ_4096) {
4760 		cmn_err(CE_WARN, "could not set max read req (%x)\n", val);
4761 		return (EINVAL);
4762 	}
4763 	return (0);
4764 }
4765 
4766 static int
4767 myri10ge_read_pcie_link_width(ddi_acc_handle_t handle, int *link)
4768 {
4769 	int err;
4770 	uint16_t	val;
4771 	uint8_t		ptr;
4772 
4773 	err = myri10ge_find_cap(handle, &ptr, PCI_CAP_ID_PCI_E);
4774 	if (err != 0) {
4775 		cmn_err(CE_WARN, "could not set max read req\n");
4776 		return (ENXIO);
4777 	}
4778 
4779 	/* read link width */
4780 	val = pci_config_get16(handle, ptr + PCIE_LINKSTS);
4781 	val &= PCIE_LINKSTS_NEG_WIDTH_MASK;
4782 	*link = (val >> 4);
4783 	return (0);
4784 }
4785 
4786 static int
4787 myri10ge_reset_nic(struct myri10ge_priv *mgp)
4788 {
4789 	ddi_acc_handle_t handle = mgp->cfg_hdl;
4790 	uint32_t reboot;
4791 	uint16_t cmd;
4792 	int err;
4793 
4794 	cmd = pci_config_get16(handle, PCI_CONF_COMM);
4795 	if ((cmd & PCI_COMM_ME) == 0) {
4796 		/*
4797 		 * Bus master DMA disabled?  Check to see if the card
4798 		 * rebooted due to a parity error For now, just report
4799 		 * it
4800 		 */
4801 
4802 		/* enter read32 mode */
4803 		pci_config_put8(handle, mgp->vso + 0x10, 0x3);
4804 		/* read REBOOT_STATUS (0xfffffff0) */
4805 		pci_config_put32(handle, mgp->vso + 0x18, 0xfffffff0);
4806 		reboot = pci_config_get16(handle, mgp->vso + 0x14);
4807 		cmn_err(CE_WARN, "%s NIC rebooted 0x%x\n", mgp->name, reboot);
4808 		return (0);
4809 	}
4810 	if (!myri10ge_watchdog_reset) {
4811 		cmn_err(CE_WARN, "%s: not resetting\n", mgp->name);
4812 		return (1);
4813 	}
4814 
4815 	myri10ge_stop_locked(mgp);
4816 	err = myri10ge_start_locked(mgp);
4817 	if (err == DDI_FAILURE) {
4818 		return (0);
4819 	}
4820 	mac_tx_update(mgp->mh);
4821 	return (1);
4822 }
4823 
4824 static inline int
4825 myri10ge_ring_stalled(myri10ge_tx_ring_t *tx)
4826 {
4827 	if (tx->sched != tx->stall &&
4828 	    tx->done == tx->watchdog_done &&
4829 	    tx->watchdog_req != tx->watchdog_done)
4830 		return (1);
4831 	return (0);
4832 }
4833 
4834 static void
4835 myri10ge_watchdog(void *arg)
4836 {
4837 	struct myri10ge_priv *mgp;
4838 	struct myri10ge_slice_state *ss;
4839 	myri10ge_tx_ring_t *tx;
4840 	int nic_ok = 1;
4841 	int slices_stalled, rx_pause, i;
4842 	int add_rx;
4843 
4844 	mgp = arg;
4845 	mutex_enter(&mgp->intrlock);
4846 	if (mgp->running != MYRI10GE_ETH_RUNNING) {
4847 		cmn_err(CE_WARN,
4848 		    "%s not running, not rearming watchdog (%d)\n",
4849 		    mgp->name, mgp->running);
4850 		mutex_exit(&mgp->intrlock);
4851 		return;
4852 	}
4853 
4854 	rx_pause = ntohl(mgp->ss[0].fw_stats->dropped_pause);
4855 
4856 	/*
4857 	 * make sure nic is stalled before we reset the nic, so as to
4858 	 * ensure we don't rip the transmit data structures out from
4859 	 * under a pending transmit
4860 	 */
4861 
4862 	for (slices_stalled = 0, i = 0; i < mgp->num_slices; i++) {
4863 		tx = &mgp->ss[i].tx;
4864 		slices_stalled = myri10ge_ring_stalled(tx);
4865 		if (slices_stalled)
4866 			break;
4867 	}
4868 
4869 	if (slices_stalled) {
4870 		if (mgp->watchdog_rx_pause == rx_pause) {
4871 			cmn_err(CE_WARN,
4872 			    "%s slice %d stalled:(%d, %d, %d, %d, %d %d %d\n)",
4873 			    mgp->name, i, tx->sched, tx->stall,
4874 			    tx->done, tx->watchdog_done, tx->req, tx->pkt_done,
4875 			    (int)ntohl(mgp->ss[i].fw_stats->send_done_count));
4876 			nic_ok = myri10ge_reset_nic(mgp);
4877 		} else {
4878 			cmn_err(CE_WARN,
4879 			    "%s Flow controlled, check link partner\n",
4880 			    mgp->name);
4881 		}
4882 	}
4883 
4884 	if (!nic_ok) {
4885 		cmn_err(CE_WARN,
4886 		    "%s Nic dead, not rearming watchdog\n", mgp->name);
4887 		mutex_exit(&mgp->intrlock);
4888 		return;
4889 	}
4890 	for (i = 0; i < mgp->num_slices; i++) {
4891 		ss = &mgp->ss[i];
4892 		tx = &ss->tx;
4893 		tx->watchdog_done = tx->done;
4894 		tx->watchdog_req = tx->req;
4895 		if (ss->watchdog_rx_copy != MYRI10GE_SLICE_STAT(rx_copy)) {
4896 			ss->watchdog_rx_copy = MYRI10GE_SLICE_STAT(rx_copy);
4897 			add_rx =
4898 			    min(ss->jpool.num_alloc,
4899 			    myri10ge_bigbufs_max -
4900 			    (ss->jpool.num_alloc -
4901 			    ss->jbufs_for_smalls));
4902 			if (add_rx != 0) {
4903 				(void) myri10ge_add_jbufs(ss, add_rx, 0);
4904 				/* now feed them to the firmware */
4905 				mutex_enter(&ss->jpool.mtx);
4906 				myri10ge_restock_jumbos(ss);
4907 				mutex_exit(&ss->jpool.mtx);
4908 			}
4909 		}
4910 	}
4911 	mgp->watchdog_rx_pause = rx_pause;
4912 
4913 	mgp->timer_id = timeout(myri10ge_watchdog, mgp,
4914 	    mgp->timer_ticks);
4915 	mutex_exit(&mgp->intrlock);
4916 }
4917 
4918 /*ARGSUSED*/
4919 static int
4920 myri10ge_get_coalesce(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *credp)
4921 
4922 {
4923 	struct myri10ge_priv *mgp = (struct myri10ge_priv *)(void *)cp;
4924 	(void) mi_mpprintf(mp, "%d", mgp->intr_coal_delay);
4925 	return (0);
4926 }
4927 
4928 /*ARGSUSED*/
4929 static int
4930 myri10ge_set_coalesce(queue_t *q, mblk_t *mp, char *value,
4931     caddr_t cp, cred_t *credp)
4932 
4933 {
4934 	struct myri10ge_priv *mgp = (struct myri10ge_priv *)(void *)cp;
4935 	char *end;
4936 	size_t new_value;
4937 
4938 	new_value = mi_strtol(value, &end, 10);
4939 	if (end == value)
4940 		return (EINVAL);
4941 
4942 	mutex_enter(&myri10ge_param_lock);
4943 	mgp->intr_coal_delay = (int)new_value;
4944 	*mgp->intr_coal_delay_ptr = htonl(mgp->intr_coal_delay);
4945 	mutex_exit(&myri10ge_param_lock);
4946 	return (0);
4947 }
4948 
4949 /*ARGSUSED*/
4950 static int
4951 myri10ge_get_pauseparam(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *credp)
4952 
4953 {
4954 	struct myri10ge_priv *mgp = (struct myri10ge_priv *)(void *)cp;
4955 	(void) mi_mpprintf(mp, "%d", mgp->pause);
4956 	return (0);
4957 }
4958 
4959 /*ARGSUSED*/
4960 static int
4961 myri10ge_set_pauseparam(queue_t *q, mblk_t *mp, char *value,
4962 			caddr_t cp, cred_t *credp)
4963 
4964 {
4965 	struct myri10ge_priv *mgp = (struct myri10ge_priv *)(void *)cp;
4966 	char *end;
4967 	size_t new_value;
4968 	int err = 0;
4969 
4970 	new_value = mi_strtol(value, &end, 10);
4971 	if (end == value)
4972 		return (EINVAL);
4973 	if (new_value != 0)
4974 		new_value = 1;
4975 
4976 	mutex_enter(&myri10ge_param_lock);
4977 	if (new_value != mgp->pause)
4978 		err = myri10ge_change_pause(mgp, new_value);
4979 	mutex_exit(&myri10ge_param_lock);
4980 	return (err);
4981 }
4982 
4983 /*ARGSUSED*/
4984 static int
4985 myri10ge_get_int(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *credp)
4986 
4987 {
4988 	(void) mi_mpprintf(mp, "%d", *(int *)(void *)cp);
4989 	return (0);
4990 }
4991 
4992 /*ARGSUSED*/
4993 static int
4994 myri10ge_set_int(queue_t *q, mblk_t *mp, char *value,
4995     caddr_t cp, cred_t *credp)
4996 
4997 {
4998 	char *end;
4999 	size_t new_value;
5000 
5001 	new_value = mi_strtol(value, &end, 10);
5002 	if (end == value)
5003 		return (EINVAL);
5004 	*(int *)(void *)cp = new_value;
5005 
5006 	return (0);
5007 }
5008 
5009 static void
5010 myri10ge_ndd_init(struct myri10ge_priv *mgp)
5011 {
5012 	mgp->nd_head = NULL;
5013 
5014 	(void) nd_load(&mgp->nd_head, "myri10ge_intr_coal_delay",
5015 	    myri10ge_get_coalesce, myri10ge_set_coalesce, (caddr_t)mgp);
5016 	(void) nd_load(&mgp->nd_head, "myri10ge_flow_control",
5017 	    myri10ge_get_pauseparam, myri10ge_set_pauseparam, (caddr_t)mgp);
5018 	(void) nd_load(&mgp->nd_head, "myri10ge_verbose",
5019 	    myri10ge_get_int, myri10ge_set_int, (caddr_t)&myri10ge_verbose);
5020 	(void) nd_load(&mgp->nd_head, "myri10ge_deassert_wait",
5021 	    myri10ge_get_int, myri10ge_set_int,
5022 	    (caddr_t)&myri10ge_deassert_wait);
5023 	(void) nd_load(&mgp->nd_head, "myri10ge_bigbufs_max",
5024 	    myri10ge_get_int, myri10ge_set_int,
5025 	    (caddr_t)&myri10ge_bigbufs_max);
5026 	(void) nd_load(&mgp->nd_head, "myri10ge_lro",
5027 	    myri10ge_get_int, myri10ge_set_int,
5028 	    (caddr_t)&myri10ge_lro);
5029 	(void) nd_load(&mgp->nd_head, "myri10ge_lro_max_aggr",
5030 	    myri10ge_get_int, myri10ge_set_int,
5031 	    (caddr_t)&myri10ge_lro_max_aggr);
5032 	(void) nd_load(&mgp->nd_head, "myri10ge_tx_hash",
5033 	    myri10ge_get_int, myri10ge_set_int,
5034 	    (caddr_t)&myri10ge_tx_hash);
5035 	(void) nd_load(&mgp->nd_head, "myri10ge_lso_copy",
5036 	    myri10ge_get_int, myri10ge_set_int,
5037 	    (caddr_t)&myri10ge_lso_copy);
5038 }
5039 
5040 static void
5041 myri10ge_ndd_fini(struct myri10ge_priv *mgp)
5042 {
5043 	nd_free(&mgp->nd_head);
5044 }
5045 
5046 static void
5047 myri10ge_m_ioctl(void *arg, queue_t *wq, mblk_t *mp)
5048 {
5049 	struct iocblk *iocp;
5050 	struct myri10ge_priv *mgp = arg;
5051 	int cmd, ok, err;
5052 
5053 	iocp = (struct iocblk *)(void *)mp->b_rptr;
5054 	cmd = iocp->ioc_cmd;
5055 
5056 	ok = 0;
5057 	err = 0;
5058 
5059 	switch (cmd) {
5060 	case ND_GET:
5061 	case ND_SET:
5062 		ok = nd_getset(wq, mgp->nd_head, mp);
5063 		break;
5064 	default:
5065 		break;
5066 	}
5067 	if (!ok)
5068 		err = EINVAL;
5069 	else
5070 		err = iocp->ioc_error;
5071 
5072 	if (!err)
5073 		miocack(wq, mp, iocp->ioc_count, err);
5074 	else
5075 		miocnak(wq, mp, 0, err);
5076 }
5077 
5078 static struct myri10ge_priv *mgp_list;
5079 
5080 struct myri10ge_priv *
5081 myri10ge_get_instance(uint_t unit)
5082 {
5083 	struct myri10ge_priv *mgp;
5084 
5085 	mutex_enter(&myri10ge_param_lock);
5086 	for (mgp = mgp_list; mgp != NULL; mgp = mgp->next) {
5087 		if (unit == ddi_get_instance(mgp->dip)) {
5088 			mgp->refcnt++;
5089 			break;
5090 		}
5091 	}
5092 	mutex_exit(&myri10ge_param_lock);
5093 	return (mgp);
5094 }
5095 
5096 void
5097 myri10ge_put_instance(struct myri10ge_priv *mgp)
5098 {
5099 	mutex_enter(&myri10ge_param_lock);
5100 	mgp->refcnt--;
5101 	mutex_exit(&myri10ge_param_lock);
5102 }
5103 
5104 static boolean_t
5105 myri10ge_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
5106 {
5107 	struct myri10ge_priv *mgp = arg;
5108 	uint32_t *cap_hcksum;
5109 	mac_capab_lso_t *cap_lso;
5110 	mac_capab_rings_t *cap_rings;
5111 
5112 	switch (cap) {
5113 	case MAC_CAPAB_HCKSUM:
5114 		cap_hcksum = cap_data;
5115 		*cap_hcksum = HCKSUM_INET_PARTIAL;
5116 		break;
5117 	case MAC_CAPAB_RINGS:
5118 		cap_rings = cap_data;
5119 		switch (cap_rings->mr_type) {
5120 		case MAC_RING_TYPE_RX:
5121 			cap_rings->mr_group_type = MAC_GROUP_TYPE_STATIC;
5122 			cap_rings->mr_rnum = mgp->num_slices;
5123 			cap_rings->mr_gnum = 1;
5124 			cap_rings->mr_rget = myri10ge_fill_ring;
5125 			cap_rings->mr_gget = myri10ge_fill_group;
5126 			break;
5127 		case MAC_RING_TYPE_TX:
5128 			cap_rings->mr_group_type = MAC_GROUP_TYPE_STATIC;
5129 			cap_rings->mr_rnum = mgp->num_slices;
5130 			cap_rings->mr_gnum = 0;
5131 			cap_rings->mr_rget = myri10ge_fill_ring;
5132 			cap_rings->mr_gget = NULL;
5133 			break;
5134 		default:
5135 			return (B_FALSE);
5136 		}
5137 		break;
5138 	case MAC_CAPAB_LSO:
5139 		cap_lso = cap_data;
5140 		if (!myri10ge_use_lso)
5141 			return (B_FALSE);
5142 		if (!(mgp->features & MYRI10GE_TSO))
5143 			return (B_FALSE);
5144 		cap_lso->lso_flags = LSO_TX_BASIC_TCP_IPV4;
5145 		cap_lso->lso_basic_tcp_ipv4.lso_max = (uint16_t)-1;
5146 		break;
5147 
5148 	default:
5149 		return (B_FALSE);
5150 	}
5151 	return (B_TRUE);
5152 }
5153 
5154 
5155 static int
5156 myri10ge_m_stat(void *arg, uint_t stat, uint64_t *val)
5157 {
5158 	struct myri10ge_priv *mgp = arg;
5159 	struct myri10ge_rx_ring_stats *rstat;
5160 	struct myri10ge_tx_ring_stats *tstat;
5161 	mcp_irq_data_t *fw_stats = mgp->ss[0].fw_stats;
5162 	struct myri10ge_slice_state *ss;
5163 	uint64_t tmp = 0;
5164 	int i;
5165 
5166 	switch (stat) {
5167 	case MAC_STAT_IFSPEED:
5168 		*val = 10ull * 1000ull * 1000000ull;
5169 		break;
5170 
5171 	case MAC_STAT_MULTIRCV:
5172 		for (i = 0; i < mgp->num_slices; i++) {
5173 			rstat = &mgp->ss[i].rx_stats;
5174 			tmp += rstat->multircv;
5175 		}
5176 		*val = tmp;
5177 		break;
5178 
5179 	case MAC_STAT_BRDCSTRCV:
5180 		for (i = 0; i < mgp->num_slices; i++) {
5181 			rstat = &mgp->ss[i].rx_stats;
5182 			tmp += rstat->brdcstrcv;
5183 		}
5184 		*val = tmp;
5185 		break;
5186 
5187 	case MAC_STAT_MULTIXMT:
5188 		for (i = 0; i < mgp->num_slices; i++) {
5189 			tstat = &mgp->ss[i].tx.stats;
5190 			tmp += tstat->multixmt;
5191 		}
5192 		*val = tmp;
5193 		break;
5194 
5195 	case MAC_STAT_BRDCSTXMT:
5196 		for (i = 0; i < mgp->num_slices; i++) {
5197 			tstat = &mgp->ss[i].tx.stats;
5198 			tmp += tstat->brdcstxmt;
5199 		}
5200 		*val = tmp;
5201 		break;
5202 
5203 	case MAC_STAT_NORCVBUF:
5204 		tmp = ntohl(fw_stats->dropped_no_big_buffer);
5205 		tmp += ntohl(fw_stats->dropped_no_small_buffer);
5206 		tmp += ntohl(fw_stats->dropped_link_overflow);
5207 		for (i = 0; i < mgp->num_slices; i++) {
5208 			ss = &mgp->ss[i];
5209 			tmp += MYRI10GE_SLICE_STAT(rx_big_nobuf);
5210 			tmp += MYRI10GE_SLICE_STAT(rx_small_nobuf);
5211 		}
5212 		*val = tmp;
5213 		break;
5214 
5215 	case MAC_STAT_IERRORS:
5216 		tmp += ntohl(fw_stats->dropped_bad_crc32);
5217 		tmp += ntohl(fw_stats->dropped_bad_phy);
5218 		tmp += ntohl(fw_stats->dropped_runt);
5219 		tmp += ntohl(fw_stats->dropped_overrun);
5220 		*val = tmp;
5221 		break;
5222 
5223 	case MAC_STAT_OERRORS:
5224 		for (i = 0; i < mgp->num_slices; i++) {
5225 			ss = &mgp->ss[i];
5226 			tmp += MYRI10GE_SLICE_STAT(xmit_lsobadflags);
5227 			tmp += MYRI10GE_SLICE_STAT(xmit_err);
5228 		}
5229 		*val = tmp;
5230 		break;
5231 
5232 	case MAC_STAT_RBYTES:
5233 		for (i = 0; i < mgp->num_slices; i++) {
5234 			rstat = &mgp->ss[i].rx_stats;
5235 			tmp += rstat->ibytes;
5236 		}
5237 		*val = tmp;
5238 		break;
5239 
5240 	case MAC_STAT_IPACKETS:
5241 		for (i = 0; i < mgp->num_slices; i++) {
5242 			rstat = &mgp->ss[i].rx_stats;
5243 			tmp += rstat->ipackets;
5244 		}
5245 		*val = tmp;
5246 		break;
5247 
5248 	case MAC_STAT_OBYTES:
5249 		for (i = 0; i < mgp->num_slices; i++) {
5250 			tstat = &mgp->ss[i].tx.stats;
5251 			tmp += tstat->obytes;
5252 		}
5253 		*val = tmp;
5254 		break;
5255 
5256 	case MAC_STAT_OPACKETS:
5257 		for (i = 0; i < mgp->num_slices; i++) {
5258 			tstat = &mgp->ss[i].tx.stats;
5259 			tmp += tstat->opackets;
5260 		}
5261 		*val = tmp;
5262 		break;
5263 
5264 	case ETHER_STAT_TOOLONG_ERRORS:
5265 		*val = ntohl(fw_stats->dropped_overrun);
5266 		break;
5267 
5268 #ifdef SOLARIS_S11
5269 	case ETHER_STAT_TOOSHORT_ERRORS:
5270 		*val = ntohl(fw_stats->dropped_runt);
5271 		break;
5272 #endif
5273 
5274 	case ETHER_STAT_LINK_PAUSE:
5275 		*val = mgp->pause;
5276 		break;
5277 
5278 	case ETHER_STAT_LINK_AUTONEG:
5279 		*val = 1;
5280 		break;
5281 
5282 	case ETHER_STAT_LINK_DUPLEX:
5283 		*val = LINK_DUPLEX_FULL;
5284 		break;
5285 
5286 	default:
5287 		return (ENOTSUP);
5288 	}
5289 
5290 	return (0);
5291 }
5292 
5293 static mac_callbacks_t myri10ge_m_callbacks = {
5294 	(MC_IOCTL | MC_GETCAPAB),
5295 	myri10ge_m_stat,
5296 	myri10ge_m_start,
5297 	myri10ge_m_stop,
5298 	myri10ge_m_promisc,
5299 	myri10ge_m_multicst,
5300 	NULL,
5301 	NULL,
5302 	myri10ge_m_ioctl,
5303 	myri10ge_m_getcapab
5304 };
5305 
5306 
5307 static int
5308 myri10ge_probe_slices(struct myri10ge_priv *mgp)
5309 {
5310 	myri10ge_cmd_t cmd;
5311 	int status;
5312 
5313 	mgp->num_slices = 1;
5314 
5315 	/* hit the board with a reset to ensure it is alive */
5316 	(void) memset(&cmd, 0, sizeof (cmd));
5317 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_RESET, &cmd);
5318 	if (status != 0) {
5319 		cmn_err(CE_WARN, "%s: failed reset\n", mgp->name);
5320 		return (ENXIO);
5321 	}
5322 
5323 	if (myri10ge_use_msix == 0)
5324 		return (0);
5325 
5326 	/* tell it the size of the interrupt queues */
5327 	cmd.data0 = mgp->max_intr_slots * sizeof (struct mcp_slot);
5328 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd);
5329 	if (status != 0) {
5330 		cmn_err(CE_WARN, "%s: failed MXGEFW_CMD_SET_INTRQ_SIZE\n",
5331 		    mgp->name);
5332 		return (ENXIO);
5333 	}
5334 
5335 	/* ask the maximum number of slices it supports */
5336 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_RSS_QUEUES,
5337 	    &cmd);
5338 	if (status != 0)
5339 		return (0);
5340 
5341 	mgp->num_slices = cmd.data0;
5342 
5343 	/*
5344 	 * if the admin did not specify a limit to how many
5345 	 * slices we should use, cap it automatically to the
5346 	 * number of CPUs currently online
5347 	 */
5348 	if (myri10ge_max_slices == -1)
5349 		myri10ge_max_slices = ncpus;
5350 
5351 	if (mgp->num_slices > myri10ge_max_slices)
5352 		mgp->num_slices = myri10ge_max_slices;
5353 
5354 
5355 	/*
5356 	 * Now try to allocate as many MSI-X vectors as we have
5357 	 * slices. We give up on MSI-X if we can only get a single
5358 	 * vector.
5359 	 */
5360 	while (mgp->num_slices > 1) {
5361 		/* make sure it is a power of two */
5362 		while (mgp->num_slices & (mgp->num_slices - 1))
5363 			mgp->num_slices--;
5364 		if (mgp->num_slices == 1)
5365 			return (0);
5366 
5367 		status = myri10ge_add_intrs(mgp, 0);
5368 		if (status == 0) {
5369 			myri10ge_rem_intrs(mgp, 0);
5370 			if (mgp->intr_cnt == mgp->num_slices) {
5371 				if (myri10ge_verbose)
5372 					printf("Got %d slices!\n",
5373 					    mgp->num_slices);
5374 				return (0);
5375 			}
5376 			mgp->num_slices = mgp->intr_cnt;
5377 		} else {
5378 			mgp->num_slices = mgp->num_slices / 2;
5379 		}
5380 	}
5381 
5382 	if (myri10ge_verbose)
5383 		printf("Got %d slices\n", mgp->num_slices);
5384 	return (0);
5385 }
5386 
5387 static void
5388 myri10ge_lro_free(struct myri10ge_slice_state *ss)
5389 {
5390 	struct lro_entry *lro;
5391 
5392 	while (ss->lro_free != NULL) {
5393 		lro = ss->lro_free;
5394 		ss->lro_free = lro->next;
5395 		kmem_free(lro, sizeof (*lro));
5396 	}
5397 }
5398 
5399 static void
5400 myri10ge_lro_alloc(struct myri10ge_slice_state *ss)
5401 {
5402 	struct lro_entry *lro;
5403 	int idx;
5404 
5405 	ss->lro_free = NULL;
5406 	ss->lro_active = NULL;
5407 
5408 	for (idx = 0; idx < myri10ge_lro_cnt; idx++) {
5409 		lro = kmem_zalloc(sizeof (*lro), KM_SLEEP);
5410 		if (lro == NULL)
5411 			continue;
5412 		lro->next = ss->lro_free;
5413 		ss->lro_free = lro;
5414 	}
5415 }
5416 
5417 static void
5418 myri10ge_free_slices(struct myri10ge_priv *mgp)
5419 {
5420 	struct myri10ge_slice_state *ss;
5421 	size_t bytes;
5422 	int i;
5423 
5424 	if (mgp->ss == NULL)
5425 		return;
5426 
5427 	for (i = 0; i < mgp->num_slices; i++) {
5428 		ss = &mgp->ss[i];
5429 		if (ss->rx_done.entry == NULL)
5430 			continue;
5431 		myri10ge_dma_free(&ss->rx_done.dma);
5432 		ss->rx_done.entry = NULL;
5433 		if (ss->fw_stats == NULL)
5434 			continue;
5435 		myri10ge_dma_free(&ss->fw_stats_dma);
5436 		ss->fw_stats = NULL;
5437 		mutex_destroy(&ss->rx_lock);
5438 		mutex_destroy(&ss->tx.lock);
5439 		mutex_destroy(&ss->tx.handle_lock);
5440 		mutex_destroy(&ss->poll_lock);
5441 		myri10ge_jpool_fini(ss);
5442 		myri10ge_slice_stat_destroy(ss);
5443 		myri10ge_lro_free(ss);
5444 	}
5445 	bytes = sizeof (*mgp->ss) * mgp->num_slices;
5446 	kmem_free(mgp->ss, bytes);
5447 	mgp->ss = NULL;
5448 }
5449 
5450 
5451 static int
5452 myri10ge_alloc_slices(struct myri10ge_priv *mgp)
5453 {
5454 	struct myri10ge_slice_state *ss;
5455 	size_t bytes;
5456 	int i;
5457 
5458 	bytes = sizeof (*mgp->ss) * mgp->num_slices;
5459 	mgp->ss = kmem_zalloc(bytes, KM_SLEEP);
5460 	if (mgp->ss == NULL)
5461 		return (ENOMEM);
5462 	for (i = 0; i < mgp->num_slices; i++) {
5463 		ss = &mgp->ss[i];
5464 
5465 		ss->mgp = mgp;
5466 
5467 		/* allocate the per-slice firmware stats */
5468 		bytes = sizeof (*ss->fw_stats);
5469 		ss->fw_stats = (mcp_irq_data_t *)(void *)
5470 		    myri10ge_dma_alloc(mgp->dip, bytes,
5471 		    &myri10ge_misc_dma_attr, &myri10ge_dev_access_attr,
5472 		    DDI_DMA_CONSISTENT, DDI_DMA_READ|DDI_DMA_CONSISTENT,
5473 		    &ss->fw_stats_dma, 1, DDI_DMA_DONTWAIT);
5474 		if (ss->fw_stats == NULL)
5475 			goto abort;
5476 		(void) memset(ss->fw_stats, 0, bytes);
5477 
5478 		/* allocate rx done ring */
5479 		bytes = mgp->max_intr_slots *
5480 		    sizeof (*ss->rx_done.entry);
5481 		ss->rx_done.entry = (mcp_slot_t *)(void *)
5482 		    myri10ge_dma_alloc(mgp->dip, bytes,
5483 		    &myri10ge_misc_dma_attr, &myri10ge_dev_access_attr,
5484 		    DDI_DMA_CONSISTENT, DDI_DMA_READ|DDI_DMA_CONSISTENT,
5485 		    &ss->rx_done.dma, 1, DDI_DMA_DONTWAIT);
5486 		if (ss->rx_done.entry == NULL) {
5487 			goto abort;
5488 		}
5489 		(void) memset(ss->rx_done.entry, 0, bytes);
5490 		mutex_init(&ss->rx_lock,   NULL, MUTEX_DEFAULT, mgp->icookie);
5491 		mutex_init(&ss->tx.lock,   NULL, MUTEX_DEFAULT, NULL);
5492 		mutex_init(&ss->tx.handle_lock,   NULL, MUTEX_DEFAULT, NULL);
5493 		mutex_init(&ss->poll_lock,   NULL, MUTEX_DEFAULT, NULL);
5494 		myri10ge_jpool_init(ss);
5495 		(void) myri10ge_slice_stat_init(ss);
5496 		myri10ge_lro_alloc(ss);
5497 	}
5498 
5499 	return (0);
5500 
5501 abort:
5502 	myri10ge_free_slices(mgp);
5503 	return (ENOMEM);
5504 }
5505 
5506 static int
5507 myri10ge_save_msi_state(struct myri10ge_priv *mgp,
5508     ddi_acc_handle_t handle)
5509 {
5510 	uint8_t ptr;
5511 	int err;
5512 
5513 	err = myri10ge_find_cap(handle, &ptr, PCI_CAP_ID_MSI);
5514 	if (err != 0) {
5515 		cmn_err(CE_WARN, "%s: could not find MSI cap\n",
5516 		    mgp->name);
5517 		return (DDI_FAILURE);
5518 	}
5519 	mgp->pci_saved_state.msi_ctrl =
5520 	    pci_config_get16(handle, ptr + PCI_MSI_CTRL);
5521 	mgp->pci_saved_state.msi_addr_low =
5522 	    pci_config_get32(handle, ptr + PCI_MSI_ADDR_OFFSET);
5523 	mgp->pci_saved_state.msi_addr_high =
5524 	    pci_config_get32(handle, ptr + PCI_MSI_ADDR_OFFSET + 4);
5525 	mgp->pci_saved_state.msi_data_32 =
5526 	    pci_config_get16(handle, ptr + PCI_MSI_32BIT_DATA);
5527 	mgp->pci_saved_state.msi_data_64 =
5528 	    pci_config_get16(handle, ptr + PCI_MSI_64BIT_DATA);
5529 	return (DDI_SUCCESS);
5530 }
5531 
5532 static int
5533 myri10ge_restore_msi_state(struct myri10ge_priv *mgp,
5534     ddi_acc_handle_t handle)
5535 {
5536 	uint8_t ptr;
5537 	int err;
5538 
5539 	err = myri10ge_find_cap(handle, &ptr, PCI_CAP_ID_MSI);
5540 	if (err != 0) {
5541 		cmn_err(CE_WARN, "%s: could not find MSI cap\n",
5542 		    mgp->name);
5543 		return (DDI_FAILURE);
5544 	}
5545 
5546 	pci_config_put16(handle, ptr + PCI_MSI_CTRL,
5547 	    mgp->pci_saved_state.msi_ctrl);
5548 	pci_config_put32(handle, ptr + PCI_MSI_ADDR_OFFSET,
5549 	    mgp->pci_saved_state.msi_addr_low);
5550 	pci_config_put32(handle, ptr + PCI_MSI_ADDR_OFFSET + 4,
5551 	    mgp->pci_saved_state.msi_addr_high);
5552 	pci_config_put16(handle, ptr + PCI_MSI_32BIT_DATA,
5553 	    mgp->pci_saved_state.msi_data_32);
5554 	pci_config_put16(handle, ptr + PCI_MSI_64BIT_DATA,
5555 	    mgp->pci_saved_state.msi_data_64);
5556 
5557 	return (DDI_SUCCESS);
5558 }
5559 
5560 static int
5561 myri10ge_save_pci_state(struct myri10ge_priv *mgp)
5562 {
5563 	ddi_acc_handle_t handle = mgp->cfg_hdl;
5564 	int i;
5565 	int err = DDI_SUCCESS;
5566 
5567 
5568 	/* Save the non-extended PCI config space 32-bits at a time */
5569 	for (i = 0; i < 16; i++)
5570 		mgp->pci_saved_state.base[i] =
5571 		    pci_config_get32(handle, i*4);
5572 
5573 	/* now save MSI interrupt state *, if needed */
5574 	if (mgp->ddi_intr_type == DDI_INTR_TYPE_MSI)
5575 		err = myri10ge_save_msi_state(mgp, handle);
5576 
5577 	return (err);
5578 }
5579 
5580 static int
5581 myri10ge_restore_pci_state(struct myri10ge_priv *mgp)
5582 {
5583 	ddi_acc_handle_t handle = mgp->cfg_hdl;
5584 	int i;
5585 	int err = DDI_SUCCESS;
5586 
5587 
5588 	/* Restore the non-extended PCI config space 32-bits at a time */
5589 	for (i = 15; i >= 0; i--)
5590 		pci_config_put32(handle, i*4, mgp->pci_saved_state.base[i]);
5591 
5592 	/* now restore MSI interrupt state *, if needed */
5593 	if (mgp->ddi_intr_type == DDI_INTR_TYPE_MSI)
5594 		err = myri10ge_restore_msi_state(mgp, handle);
5595 
5596 	if (mgp->max_read_request_4k)
5597 		(void) myri10ge_set_max_readreq(handle);
5598 	return (err);
5599 }
5600 
5601 
5602 static int
5603 myri10ge_suspend(dev_info_t *dip)
5604 {
5605 	struct myri10ge_priv *mgp = ddi_get_driver_private(dip);
5606 	int status;
5607 
5608 	if (mgp == NULL) {
5609 		cmn_err(CE_WARN, "null dip in myri10ge_suspend\n");
5610 		return (DDI_FAILURE);
5611 	}
5612 	if (mgp->dip != dip) {
5613 		cmn_err(CE_WARN, "bad dip in myri10ge_suspend\n");
5614 		return (DDI_FAILURE);
5615 	}
5616 	mutex_enter(&mgp->intrlock);
5617 	if (mgp->running == MYRI10GE_ETH_RUNNING) {
5618 		mgp->running = MYRI10GE_ETH_STOPPING;
5619 		mutex_exit(&mgp->intrlock);
5620 		(void) untimeout(mgp->timer_id);
5621 		mutex_enter(&mgp->intrlock);
5622 		myri10ge_stop_locked(mgp);
5623 		mgp->running = MYRI10GE_ETH_SUSPENDED_RUNNING;
5624 	}
5625 	status = myri10ge_save_pci_state(mgp);
5626 	mutex_exit(&mgp->intrlock);
5627 	return (status);
5628 }
5629 
5630 static int
5631 myri10ge_resume(dev_info_t *dip)
5632 {
5633 	struct myri10ge_priv *mgp = ddi_get_driver_private(dip);
5634 	int status = DDI_SUCCESS;
5635 
5636 	if (mgp == NULL) {
5637 		cmn_err(CE_WARN, "null dip in myri10ge_resume\n");
5638 		return (DDI_FAILURE);
5639 	}
5640 	if (mgp->dip != dip) {
5641 		cmn_err(CE_WARN, "bad dip in myri10ge_resume\n");
5642 		return (DDI_FAILURE);
5643 	}
5644 
5645 	mutex_enter(&mgp->intrlock);
5646 	status = myri10ge_restore_pci_state(mgp);
5647 	if (status == DDI_SUCCESS &&
5648 	    mgp->running == MYRI10GE_ETH_SUSPENDED_RUNNING) {
5649 		status = myri10ge_start_locked(mgp);
5650 	}
5651 	mutex_exit(&mgp->intrlock);
5652 	if (status != DDI_SUCCESS)
5653 		return (status);
5654 
5655 	/* start the watchdog timer */
5656 	mgp->timer_id = timeout(myri10ge_watchdog, mgp,
5657 	    mgp->timer_ticks);
5658 	return (DDI_SUCCESS);
5659 }
5660 
5661 static int
5662 myri10ge_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5663 {
5664 
5665 	struct myri10ge_priv *mgp;
5666 	mac_register_t *macp, *omacp;
5667 	ddi_acc_handle_t handle;
5668 	uint32_t csr, hdr_offset;
5669 	int status, span, link_width, max_read_request_4k;
5670 	unsigned long bus_number, dev_number, func_number;
5671 	size_t bytes;
5672 	offset_t ss_offset;
5673 	uint8_t vso;
5674 
5675 	if (cmd == DDI_RESUME) {
5676 		return (myri10ge_resume(dip));
5677 	}
5678 
5679 	if (cmd != DDI_ATTACH)
5680 		return (DDI_FAILURE);
5681 	if (pci_config_setup(dip, &handle) != DDI_SUCCESS)
5682 		return (DDI_FAILURE);
5683 
5684 	/* enable busmater and io space access */
5685 	csr = pci_config_get32(handle, PCI_CONF_COMM);
5686 	pci_config_put32(handle, PCI_CONF_COMM,
5687 	    (csr |PCI_COMM_ME|PCI_COMM_MAE));
5688 	status = myri10ge_read_pcie_link_width(handle, &link_width);
5689 	if (status != 0) {
5690 		cmn_err(CE_WARN, "could not read link width!\n");
5691 		link_width = 0;
5692 	}
5693 	max_read_request_4k = !myri10ge_set_max_readreq(handle);
5694 	status = myri10ge_find_cap(handle, &vso, PCI_CAP_ID_VS);
5695 	if (status != 0)
5696 		goto abort_with_cfg_hdl;
5697 	if ((omacp = mac_alloc(MAC_VERSION)) == NULL)
5698 		goto abort_with_cfg_hdl;
5699 	/*
5700 	 * XXXX Hack: mac_register_t grows in newer kernels.  To be
5701 	 * able to write newer fields, such as m_margin, without
5702 	 * writing outside allocated memory, we allocate our own macp
5703 	 * and pass that to mac_register()
5704 	 */
5705 	macp = kmem_zalloc(sizeof (*macp) * 8, KM_SLEEP);
5706 	macp->m_version = omacp->m_version;
5707 
5708 	if ((mgp = (struct myri10ge_priv *)
5709 	    kmem_zalloc(sizeof (*mgp), KM_SLEEP)) == NULL) {
5710 		goto abort_with_macinfo;
5711 	}
5712 	ddi_set_driver_private(dip, mgp);
5713 
5714 	/* setup device name for log messages */
5715 	(void) sprintf(mgp->name, "myri10ge%d", ddi_get_instance(dip));
5716 
5717 	mutex_enter(&myri10ge_param_lock);
5718 	myri10ge_get_props(dip);
5719 	mgp->intr_coal_delay = myri10ge_intr_coal_delay;
5720 	mgp->pause = myri10ge_flow_control;
5721 	mutex_exit(&myri10ge_param_lock);
5722 
5723 	mgp->max_read_request_4k = max_read_request_4k;
5724 	mgp->pcie_link_width = link_width;
5725 	mgp->running = MYRI10GE_ETH_STOPPED;
5726 	mgp->vso = vso;
5727 	mgp->dip = dip;
5728 	mgp->cfg_hdl = handle;
5729 
5730 	mgp->timer_ticks = 5 * drv_usectohz(1000000); /* 5 seconds */
5731 	myri10ge_test_physical(dip);
5732 
5733 	/* allocate command page */
5734 	bytes = sizeof (*mgp->cmd);
5735 	mgp->cmd = (mcp_cmd_response_t *)
5736 	    (void *)myri10ge_dma_alloc(dip, bytes,
5737 	    &myri10ge_misc_dma_attr, &myri10ge_dev_access_attr,
5738 	    DDI_DMA_CONSISTENT,	DDI_DMA_RDWR|DDI_DMA_CONSISTENT,
5739 	    &mgp->cmd_dma, 1, DDI_DMA_DONTWAIT);
5740 	if (mgp->cmd == NULL)
5741 		goto abort_with_mgp;
5742 
5743 	(void) myri10ge_reg_set(dip, &mgp->reg_set, &span, &bus_number,
5744 	    &dev_number, &func_number);
5745 	if (myri10ge_verbose)
5746 		printf("%s at %ld:%ld:%ld attaching\n", mgp->name,
5747 		    bus_number, dev_number, func_number);
5748 	status = ddi_regs_map_setup(dip, mgp->reg_set, (caddr_t *)&mgp->sram,
5749 	    (offset_t)0, (offset_t)span,  &myri10ge_dev_access_attr,
5750 	    &mgp->io_handle);
5751 	if (status != DDI_SUCCESS) {
5752 		cmn_err(CE_WARN, "%s: couldn't map memory space", mgp->name);
5753 		printf("%s: reg_set = %d, span = %d, status = %d",
5754 		    mgp->name, mgp->reg_set, span, status);
5755 		goto abort_with_mgp;
5756 	}
5757 
5758 	hdr_offset = *(uint32_t *)(void*)(mgp->sram +  MCP_HEADER_PTR_OFFSET);
5759 	hdr_offset = ntohl(hdr_offset) & 0xffffc;
5760 	ss_offset = hdr_offset +
5761 	    offsetof(struct mcp_gen_header, string_specs);
5762 	mgp->sram_size = ntohl(*(uint32_t *)(void*)(mgp->sram + ss_offset));
5763 	myri10ge_pio_copy32(mgp->eeprom_strings,
5764 	    (uint32_t *)(void*)((char *)mgp->sram + mgp->sram_size),
5765 	    MYRI10GE_EEPROM_STRINGS_SIZE);
5766 	(void) memset(mgp->eeprom_strings +
5767 	    MYRI10GE_EEPROM_STRINGS_SIZE - 2, 0, 2);
5768 
5769 	status = myri10ge_read_mac_addr(mgp);
5770 	if (status) {
5771 		goto abort_with_mapped;
5772 	}
5773 
5774 	status = myri10ge_select_firmware(mgp);
5775 	if (status != 0) {
5776 		cmn_err(CE_WARN, "%s: failed to load firmware\n", mgp->name);
5777 		goto abort_with_mapped;
5778 	}
5779 
5780 	status = myri10ge_probe_slices(mgp);
5781 	if (status != 0) {
5782 		cmn_err(CE_WARN, "%s: failed to probe slices\n", mgp->name);
5783 		goto abort_with_dummy_rdma;
5784 	}
5785 
5786 	status = myri10ge_alloc_slices(mgp);
5787 	if (status != 0) {
5788 		cmn_err(CE_WARN, "%s: failed to alloc slices\n", mgp->name);
5789 		goto abort_with_dummy_rdma;
5790 	}
5791 
5792 	/* add the interrupt handler */
5793 	status = myri10ge_add_intrs(mgp, 1);
5794 	if (status != 0) {
5795 		cmn_err(CE_WARN, "%s: Failed to add interrupt\n",
5796 		    mgp->name);
5797 		goto abort_with_slices;
5798 	}
5799 
5800 	/* now that we have an iblock_cookie, init the mutexes */
5801 	mutex_init(&mgp->cmd_lock, NULL, MUTEX_DRIVER, mgp->icookie);
5802 	mutex_init(&mgp->intrlock, NULL, MUTEX_DRIVER, mgp->icookie);
5803 
5804 
5805 	status = myri10ge_nic_stat_init(mgp);
5806 	if (status != DDI_SUCCESS)
5807 		goto abort_with_interrupts;
5808 	status = myri10ge_info_init(mgp);
5809 	if (status != DDI_SUCCESS)
5810 		goto abort_with_stats;
5811 
5812 	/*
5813 	 *	Initialize  GLD state
5814 	 */
5815 
5816 	macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
5817 	macp->m_driver = mgp;
5818 	macp->m_dip = dip;
5819 	macp->m_src_addr = mgp->mac_addr;
5820 	macp->m_callbacks = &myri10ge_m_callbacks;
5821 	macp->m_min_sdu = 0;
5822 	macp->m_max_sdu = myri10ge_mtu -
5823 	    (sizeof (struct ether_header) + MXGEFW_PAD + VLAN_TAGSZ);
5824 #ifdef SOLARIS_S11
5825 	macp->m_margin = VLAN_TAGSZ;
5826 #endif
5827 	macp->m_v12n = MAC_VIRT_LEVEL1;
5828 	status = mac_register(macp, &mgp->mh);
5829 	if (status != 0) {
5830 		cmn_err(CE_WARN, "%s: mac_register failed with %d\n",
5831 		    mgp->name, status);
5832 		goto abort_with_info;
5833 	}
5834 	myri10ge_ndd_init(mgp);
5835 	if (myri10ge_verbose)
5836 		printf("%s: %s, tx bndry %d, fw %s\n", mgp->name,
5837 		    mgp->intr_type, mgp->tx_boundary, mgp->fw_name);
5838 	mutex_enter(&myri10ge_param_lock);
5839 	mgp->next = mgp_list;
5840 	mgp_list = mgp;
5841 	mutex_exit(&myri10ge_param_lock);
5842 	kmem_free(macp, sizeof (*macp) * 8);
5843 	mac_free(omacp);
5844 	return (DDI_SUCCESS);
5845 
5846 abort_with_info:
5847 	myri10ge_info_destroy(mgp);
5848 
5849 abort_with_stats:
5850 	myri10ge_nic_stat_destroy(mgp);
5851 
5852 abort_with_interrupts:
5853 	mutex_destroy(&mgp->cmd_lock);
5854 	mutex_destroy(&mgp->intrlock);
5855 	myri10ge_rem_intrs(mgp, 1);
5856 
5857 abort_with_slices:
5858 	myri10ge_free_slices(mgp);
5859 
5860 abort_with_dummy_rdma:
5861 	myri10ge_dummy_rdma(mgp, 0);
5862 
5863 abort_with_mapped:
5864 	ddi_regs_map_free(&mgp->io_handle);
5865 
5866 	myri10ge_dma_free(&mgp->cmd_dma);
5867 
5868 abort_with_mgp:
5869 	kmem_free(mgp, sizeof (*mgp));
5870 
5871 abort_with_macinfo:
5872 	kmem_free(macp, sizeof (*macp) * 8);
5873 	mac_free(omacp);
5874 
5875 abort_with_cfg_hdl:
5876 	pci_config_teardown(&handle);
5877 	return (DDI_FAILURE);
5878 
5879 }
5880 
5881 
5882 static int
5883 myri10ge_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
5884 {
5885 	struct myri10ge_priv	*mgp, *tmp;
5886 	int 			status, i, jbufs_alloced;
5887 
5888 	if (cmd == DDI_SUSPEND) {
5889 		status = myri10ge_suspend(dip);
5890 		return (status);
5891 	}
5892 
5893 	if (cmd != DDI_DETACH) {
5894 		return (DDI_FAILURE);
5895 	}
5896 	/* Get the driver private (gld_mac_info_t) structure */
5897 	mgp = ddi_get_driver_private(dip);
5898 
5899 	mutex_enter(&mgp->intrlock);
5900 	jbufs_alloced = 0;
5901 	for (i = 0; i < mgp->num_slices; i++) {
5902 		myri10ge_remove_jbufs(&mgp->ss[i]);
5903 		jbufs_alloced += mgp->ss[i].jpool.num_alloc;
5904 	}
5905 	mutex_exit(&mgp->intrlock);
5906 	if (jbufs_alloced != 0) {
5907 		cmn_err(CE_NOTE, "%s: %d loaned rx buffers remain\n",
5908 		    mgp->name, jbufs_alloced);
5909 		return (DDI_FAILURE);
5910 	}
5911 
5912 	mutex_enter(&myri10ge_param_lock);
5913 	if (mgp->refcnt != 0) {
5914 		mutex_exit(&myri10ge_param_lock);
5915 		cmn_err(CE_NOTE, "%s: %d external refs remain\n",
5916 		    mgp->name, mgp->refcnt);
5917 		return (DDI_FAILURE);
5918 	}
5919 	mutex_exit(&myri10ge_param_lock);
5920 
5921 	status = mac_unregister(mgp->mh);
5922 	if (status != DDI_SUCCESS)
5923 		return (status);
5924 
5925 	myri10ge_ndd_fini(mgp);
5926 	myri10ge_dummy_rdma(mgp, 0);
5927 	myri10ge_nic_stat_destroy(mgp);
5928 	myri10ge_info_destroy(mgp);
5929 
5930 	mutex_destroy(&mgp->cmd_lock);
5931 	mutex_destroy(&mgp->intrlock);
5932 
5933 	myri10ge_rem_intrs(mgp, 1);
5934 
5935 	myri10ge_free_slices(mgp);
5936 	ddi_regs_map_free(&mgp->io_handle);
5937 	myri10ge_dma_free(&mgp->cmd_dma);
5938 	pci_config_teardown(&mgp->cfg_hdl);
5939 
5940 	mutex_enter(&myri10ge_param_lock);
5941 	if (mgp_list == mgp) {
5942 		mgp_list = mgp->next;
5943 	} else {
5944 		tmp = mgp_list;
5945 		while (tmp->next != mgp && tmp->next != NULL)
5946 			tmp = tmp->next;
5947 		if (tmp->next != NULL)
5948 			tmp->next = tmp->next->next;
5949 	}
5950 	kmem_free(mgp, sizeof (*mgp));
5951 	mutex_exit(&myri10ge_param_lock);
5952 	return (DDI_SUCCESS);
5953 }
5954 
5955 /*
5956  * Helper for quiesce entry point: Interrupt threads are not being
5957  * scheduled, so we must poll for the confirmation DMA to arrive in
5958  * the firmware stats block for slice 0.  We're essentially running
5959  * the guts of the interrupt handler, and just cherry picking the
5960  * confirmation that the NIC is queuesced (stats->link_down)
5961  */
5962 
5963 static int
5964 myri10ge_poll_down(struct myri10ge_priv *mgp)
5965 {
5966 	struct myri10ge_slice_state *ss = mgp->ss;
5967 	mcp_irq_data_t *stats = ss->fw_stats;
5968 	int valid;
5969 	int found_down = 0;
5970 
5971 
5972 	/* check for a pending IRQ */
5973 
5974 	if (! *((volatile uint8_t *)& stats->valid))
5975 		return (0);
5976 	valid = stats->valid;
5977 
5978 	/*
5979 	 * Make sure to tell the NIC to lower a legacy IRQ, else
5980 	 * it may have corrupt state after restarting
5981 	 */
5982 
5983 	if (mgp->ddi_intr_type == DDI_INTR_TYPE_FIXED) {
5984 		/* lower legacy IRQ  */
5985 		*mgp->irq_deassert = 0;
5986 		mb();
5987 		/* wait for irq conf DMA */
5988 		while (*((volatile uint8_t *)& stats->valid))
5989 			;
5990 	}
5991 	if (stats->stats_updated && stats->link_down)
5992 		found_down = 1;
5993 
5994 	if (valid & 0x1)
5995 		*ss->irq_claim = BE_32(3);
5996 	*(ss->irq_claim + 1) = BE_32(3);
5997 
5998 	return (found_down);
5999 }
6000 
6001 static int
6002 myri10ge_quiesce(dev_info_t *dip)
6003 {
6004 	struct myri10ge_priv *mgp;
6005 	myri10ge_cmd_t cmd;
6006 	int status, down, i;
6007 
6008 	mgp = ddi_get_driver_private(dip);
6009 	if (mgp == NULL)
6010 		return (DDI_FAILURE);
6011 
6012 	/* if devices was unplumbed, it is guaranteed to be quiescent */
6013 	if (mgp->running == MYRI10GE_ETH_STOPPED)
6014 		return (DDI_SUCCESS);
6015 
6016 	/* send a down CMD to queuesce NIC */
6017 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_DOWN, &cmd);
6018 	if (status) {
6019 		cmn_err(CE_WARN, "%s: Couldn't bring down link\n", mgp->name);
6020 		return (DDI_FAILURE);
6021 	}
6022 
6023 	for (i = 0; i < 20; i++) {
6024 		down = myri10ge_poll_down(mgp);
6025 		if (down)
6026 			break;
6027 		delay(drv_usectohz(100000));
6028 		mb();
6029 	}
6030 	if (down)
6031 		return (DDI_SUCCESS);
6032 	return (DDI_FAILURE);
6033 }
6034 
6035 /*
6036  * Distinguish between allocb'ed blocks, and gesballoc'ed attached
6037  * storage.
6038  */
6039 static void
6040 myri10ge_find_lastfree(void)
6041 {
6042 	mblk_t *mp = allocb(1024, 0);
6043 	dblk_t *dbp;
6044 
6045 	if (mp == NULL) {
6046 		cmn_err(CE_WARN, "myri10ge_find_lastfree failed\n");
6047 		return;
6048 	}
6049 	dbp = mp->b_datap;
6050 	myri10ge_db_lastfree = (void *)dbp->db_lastfree;
6051 }
6052 
6053 int
6054 _init(void)
6055 {
6056 	int i;
6057 
6058 	if (myri10ge_verbose)
6059 		cmn_err(CE_NOTE,
6060 		    "Myricom 10G driver (10GbE) version %s loading\n",
6061 		    MYRI10GE_VERSION_STR);
6062 	myri10ge_find_lastfree();
6063 	mac_init_ops(&myri10ge_ops, "myri10ge");
6064 	mutex_init(&myri10ge_param_lock, NULL, MUTEX_DEFAULT, NULL);
6065 	if ((i = mod_install(&modlinkage)) != 0) {
6066 		cmn_err(CE_WARN, "mod_install returned %d\n", i);
6067 		mac_fini_ops(&myri10ge_ops);
6068 		mutex_destroy(&myri10ge_param_lock);
6069 	}
6070 	return (i);
6071 }
6072 
6073 int
6074 _fini(void)
6075 {
6076 	int i;
6077 	i = mod_remove(&modlinkage);
6078 	if (i != 0) {
6079 		return (i);
6080 	}
6081 	mac_fini_ops(&myri10ge_ops);
6082 	mutex_destroy(&myri10ge_param_lock);
6083 	return (0);
6084 }
6085 
6086 int
6087 _info(struct modinfo *modinfop)
6088 {
6089 	return (mod_info(&modlinkage, modinfop));
6090 }
6091 
6092 
6093 /*
6094  *  This file uses MyriGE driver indentation.
6095  *
6096  * Local Variables:
6097  * c-file-style:"sun"
6098  * tab-width:8
6099  * End:
6100  */
6101