xref: /titanic_50/usr/src/uts/common/io/ixgbe/ixgbe_tx.c (revision a01a4735489b25416b449c3231ab8b710caaeb1c)
1 /*
2  * CDDL HEADER START
3  *
4  * Copyright(c) 2007-2009 Intel Corporation. All rights reserved.
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #include "ixgbe_sw.h"
29 
30 static int ixgbe_tx_copy(ixgbe_tx_ring_t *, tx_control_block_t *, mblk_t *,
31     uint32_t, boolean_t);
32 static int ixgbe_tx_bind(ixgbe_tx_ring_t *, tx_control_block_t *, mblk_t *,
33     uint32_t);
34 static int ixgbe_tx_fill_ring(ixgbe_tx_ring_t *, link_list_t *,
35     ixgbe_tx_context_t *, size_t);
36 static void ixgbe_save_desc(tx_control_block_t *, uint64_t, size_t);
37 static tx_control_block_t *ixgbe_get_free_list(ixgbe_tx_ring_t *);
38 
39 static int ixgbe_get_context(mblk_t *, ixgbe_tx_context_t *);
40 static boolean_t ixgbe_check_context(ixgbe_tx_ring_t *,
41     ixgbe_tx_context_t *);
42 static void ixgbe_fill_context(struct ixgbe_adv_tx_context_desc *,
43     ixgbe_tx_context_t *);
44 
45 #ifndef IXGBE_DEBUG
46 #pragma inline(ixgbe_save_desc)
47 #pragma inline(ixgbe_get_context)
48 #pragma inline(ixgbe_check_context)
49 #pragma inline(ixgbe_fill_context)
50 #endif
51 
52 /*
53  * ixgbe_ring_tx
54  *
55  * To transmit one mblk through one specified ring.
56  *
57  * One mblk can consist of several fragments, each fragment
58  * will be processed with different methods based on the size.
59  * For the fragments with size less than the bcopy threshold,
60  * they will be processed by using bcopy; otherwise, they will
61  * be processed by using DMA binding.
62  *
63  * To process the mblk, a tx control block is got from the
64  * free list. One tx control block contains one tx buffer, which
65  * is used to copy mblk fragments' data; and one tx DMA handle,
66  * which is used to bind a mblk fragment with DMA resource.
67  *
68  * Several small mblk fragments can be copied into one tx control
69  * block's buffer, and then the buffer will be transmitted with
70  * one tx descriptor.
71  *
72  * A large fragment only binds with one tx control block's DMA
73  * handle, and it can span several tx descriptors for transmitting.
74  *
75  * So to transmit a packet (mblk), several tx control blocks can
76  * be used. After the processing, those tx control blocks will
77  * be put to the work list.
78  */
79 mblk_t *
80 ixgbe_ring_tx(void *arg, mblk_t *mp)
81 {
82 	ixgbe_tx_ring_t *tx_ring = (ixgbe_tx_ring_t *)arg;
83 	ixgbe_t *ixgbe = tx_ring->ixgbe;
84 	tx_type_t current_flag, next_flag;
85 	uint32_t current_len, next_len;
86 	uint32_t desc_total;
87 	size_t mbsize;
88 	int desc_num;
89 	boolean_t copy_done, eop;
90 	mblk_t *current_mp, *next_mp, *nmp;
91 	tx_control_block_t *tcb;
92 	ixgbe_tx_context_t tx_context, *ctx;
93 	link_list_t pending_list;
94 	uint32_t len, hdr_frag_len, hdr_len;
95 	uint32_t copy_thresh;
96 	mblk_t *new_mp;
97 	mblk_t *pre_mp;
98 
99 	ASSERT(mp->b_next == NULL);
100 
101 	copy_thresh = tx_ring->copy_thresh;
102 
103 	/* Get the mblk size */
104 	mbsize = 0;
105 	for (nmp = mp; nmp != NULL; nmp = nmp->b_cont) {
106 		mbsize += MBLKL(nmp);
107 	}
108 
109 	if (ixgbe->tx_hcksum_enable) {
110 		/*
111 		 * Retrieve checksum context information from the mblk
112 		 * that will be used to decide whether/how to fill the
113 		 * context descriptor.
114 		 */
115 		ctx = &tx_context;
116 		if (ixgbe_get_context(mp, ctx) < 0) {
117 			freemsg(mp);
118 			return (NULL);
119 		}
120 
121 		/*
122 		 * If the mblk size exceeds the max size ixgbe could
123 		 * process, then discard this mblk, and return NULL.
124 		 */
125 		if ((ctx->lso_flag &&
126 		    ((mbsize - ctx->mac_hdr_len) > IXGBE_LSO_MAXLEN)) ||
127 		    (!ctx->lso_flag &&
128 		    (mbsize > (ixgbe->max_frame_size - ETHERFCSL)))) {
129 			freemsg(mp);
130 			IXGBE_DEBUGLOG_0(ixgbe, "ixgbe_tx: packet oversize");
131 			return (NULL);
132 		}
133 	} else {
134 		ctx = NULL;
135 	}
136 
137 	/*
138 	 * Check and recycle tx descriptors.
139 	 * The recycle threshold here should be selected carefully
140 	 */
141 	if (tx_ring->tbd_free < tx_ring->recycle_thresh) {
142 		tx_ring->tx_recycle(tx_ring);
143 	}
144 
145 	/*
146 	 * After the recycling, if the tbd_free is less than the
147 	 * overload_threshold, assert overload, return mp;
148 	 * and we need to re-schedule the tx again.
149 	 */
150 	if (tx_ring->tbd_free < tx_ring->overload_thresh) {
151 		tx_ring->reschedule = B_TRUE;
152 		IXGBE_DEBUG_STAT(tx_ring->stat_overload);
153 		return (mp);
154 	}
155 
156 	/*
157 	 * The pending_list is a linked list that is used to save
158 	 * the tx control blocks that have packet data processed
159 	 * but have not put the data to the tx descriptor ring.
160 	 * It is used to reduce the lock contention of the tx_lock.
161 	 */
162 	LINK_LIST_INIT(&pending_list);
163 	desc_num = 0;
164 	desc_total = 0;
165 
166 	/*
167 	 * The software should guarantee LSO packet header(MAC+IP+TCP)
168 	 * to be within one descriptor. Here we reallocate and refill the
169 	 * the header if it's physical memory non-contiguous.
170 	 */
171 	if ((ctx != NULL) && ctx->lso_flag) {
172 		/* find the last fragment of the header */
173 		len = MBLKL(mp);
174 		ASSERT(len > 0);
175 		nmp = mp;
176 		pre_mp = NULL;
177 		hdr_len = ctx->ip_hdr_len + ctx->mac_hdr_len + ctx->l4_hdr_len;
178 		while (len < hdr_len) {
179 			pre_mp = nmp;
180 			nmp = nmp->b_cont;
181 			len += MBLKL(nmp);
182 		}
183 		/*
184 		 * If the header and the payload are in different mblks,
185 		 * we simply force the header to be copied into pre-allocated
186 		 * page-aligned buffer.
187 		 */
188 		if (len == hdr_len)
189 			goto adjust_threshold;
190 
191 		hdr_frag_len = hdr_len - (len - MBLKL(nmp));
192 		/*
193 		 * There are two cases we need to reallocate a mblk for the
194 		 * last header fragment:
195 		 * 1. the header is in multiple mblks and the last fragment
196 		 * share the same mblk with the payload
197 		 * 2. the header is in a single mblk shared with the payload
198 		 * and the header is physical memory non-contiguous
199 		 */
200 		if ((nmp != mp) ||
201 		    (P2NPHASE((uintptr_t)nmp->b_rptr, ixgbe->sys_page_size)
202 		    < hdr_len)) {
203 			IXGBE_DEBUG_STAT(tx_ring->stat_lso_header_fail);
204 			/*
205 			 * reallocate the mblk for the last header fragment,
206 			 * expect to bcopy into pre-allocated page-aligned
207 			 * buffer
208 			 */
209 			new_mp = allocb(hdr_frag_len, NULL);
210 			if (!new_mp)
211 				return (mp);
212 			bcopy(nmp->b_rptr, new_mp->b_rptr, hdr_frag_len);
213 			/* link the new header fragment with the other parts */
214 			new_mp->b_wptr = new_mp->b_rptr + hdr_frag_len;
215 			new_mp->b_cont = nmp;
216 			if (pre_mp)
217 				pre_mp->b_cont = new_mp;
218 			nmp->b_rptr += hdr_frag_len;
219 			if (hdr_frag_len == hdr_len)
220 				mp = new_mp;
221 		}
222 adjust_threshold:
223 		/*
224 		 * adjust the bcopy threshhold to guarantee
225 		 * the header to use bcopy way
226 		 */
227 		if (copy_thresh < hdr_len)
228 			copy_thresh = hdr_len;
229 	}
230 
231 	current_mp = mp;
232 	current_len = MBLKL(current_mp);
233 	/*
234 	 * Decide which method to use for the first fragment
235 	 */
236 	current_flag = (current_len <= copy_thresh) ?
237 	    USE_COPY : USE_DMA;
238 	/*
239 	 * If the mblk includes several contiguous small fragments,
240 	 * they may be copied into one buffer. This flag is used to
241 	 * indicate whether there are pending fragments that need to
242 	 * be copied to the current tx buffer.
243 	 *
244 	 * If this flag is B_TRUE, it indicates that a new tx control
245 	 * block is needed to process the next fragment using either
246 	 * copy or DMA binding.
247 	 *
248 	 * Otherwise, it indicates that the next fragment will be
249 	 * copied to the current tx buffer that is maintained by the
250 	 * current tx control block. No new tx control block is needed.
251 	 */
252 	copy_done = B_TRUE;
253 	while (current_mp) {
254 		next_mp = current_mp->b_cont;
255 		eop = (next_mp == NULL); /* Last fragment of the packet? */
256 		next_len = eop ? 0: MBLKL(next_mp);
257 
258 		/*
259 		 * When the current fragment is an empty fragment, if
260 		 * the next fragment will still be copied to the current
261 		 * tx buffer, we cannot skip this fragment here. Because
262 		 * the copy processing is pending for completion. We have
263 		 * to process this empty fragment in the tx_copy routine.
264 		 *
265 		 * If the copy processing is completed or a DMA binding
266 		 * processing is just completed, we can just skip this
267 		 * empty fragment.
268 		 */
269 		if ((current_len == 0) && (copy_done)) {
270 			current_mp = next_mp;
271 			current_len = next_len;
272 			current_flag = (current_len <= copy_thresh) ?
273 			    USE_COPY : USE_DMA;
274 			continue;
275 		}
276 
277 		if (copy_done) {
278 			/*
279 			 * Get a new tx control block from the free list
280 			 */
281 			tcb = ixgbe_get_free_list(tx_ring);
282 
283 			if (tcb == NULL) {
284 				IXGBE_DEBUG_STAT(tx_ring->stat_fail_no_tcb);
285 				goto tx_failure;
286 			}
287 
288 			/*
289 			 * Push the tx control block to the pending list
290 			 * to avoid using lock too early
291 			 */
292 			LIST_PUSH_TAIL(&pending_list, &tcb->link);
293 		}
294 
295 		if (current_flag == USE_COPY) {
296 			/*
297 			 * Check whether to use bcopy or DMA binding to process
298 			 * the next fragment, and if using bcopy, whether we
299 			 * need to continue copying the next fragment into the
300 			 * current tx buffer.
301 			 */
302 			ASSERT((tcb->tx_buf.len + current_len) <=
303 			    tcb->tx_buf.size);
304 
305 			if (eop) {
306 				/*
307 				 * This is the last fragment of the packet, so
308 				 * the copy processing will be completed with
309 				 * this fragment.
310 				 */
311 				next_flag = USE_NONE;
312 				copy_done = B_TRUE;
313 			} else if ((tcb->tx_buf.len + current_len + next_len) >
314 			    tcb->tx_buf.size) {
315 				/*
316 				 * If the next fragment is too large to be
317 				 * copied to the current tx buffer, we need
318 				 * to complete the current copy processing.
319 				 */
320 				next_flag = (next_len > copy_thresh) ?
321 				    USE_DMA: USE_COPY;
322 				copy_done = B_TRUE;
323 			} else if (next_len > copy_thresh) {
324 				/*
325 				 * The next fragment needs to be processed with
326 				 * DMA binding. So the copy prcessing will be
327 				 * completed with the current fragment.
328 				 */
329 				next_flag = USE_DMA;
330 				copy_done = B_TRUE;
331 			} else {
332 				/*
333 				 * Continue to copy the next fragment to the
334 				 * current tx buffer.
335 				 */
336 				next_flag = USE_COPY;
337 				copy_done = B_FALSE;
338 			}
339 
340 			desc_num = ixgbe_tx_copy(tx_ring, tcb, current_mp,
341 			    current_len, copy_done);
342 		} else {
343 			/*
344 			 * Check whether to use bcopy or DMA binding to process
345 			 * the next fragment.
346 			 */
347 			next_flag = (next_len > copy_thresh) ?
348 			    USE_DMA: USE_COPY;
349 			ASSERT(copy_done == B_TRUE);
350 
351 			desc_num = ixgbe_tx_bind(tx_ring, tcb, current_mp,
352 			    current_len);
353 		}
354 
355 		if (desc_num > 0)
356 			desc_total += desc_num;
357 		else if (desc_num < 0)
358 			goto tx_failure;
359 
360 		current_mp = next_mp;
361 		current_len = next_len;
362 		current_flag = next_flag;
363 	}
364 
365 	/*
366 	 * Attach the mblk to the last tx control block
367 	 */
368 	ASSERT(tcb);
369 	ASSERT(tcb->mp == NULL);
370 	tcb->mp = mp;
371 
372 	/*
373 	 * 82598/82599 chipset has a limitation that no more than 32 tx
374 	 * descriptors can be transmited out at one time.
375 	 *
376 	 * Here is a workaround for it: pull up the mblk then send it
377 	 * out with bind way. By doing so, no more than MAX_COOKIE (18)
378 	 * descriptors is needed.
379 	 */
380 	if (desc_total + 1 > IXGBE_TX_DESC_LIMIT) {
381 		IXGBE_DEBUG_STAT(tx_ring->stat_break_tbd_limit);
382 
383 		/*
384 		 * Discard the mblk and free the used resources
385 		 */
386 		tcb = (tx_control_block_t *)LIST_GET_HEAD(&pending_list);
387 		while (tcb) {
388 			tcb->mp = NULL;
389 			ixgbe_free_tcb(tcb);
390 			tcb = (tx_control_block_t *)
391 			    LIST_GET_NEXT(&pending_list, &tcb->link);
392 		}
393 
394 		/*
395 		 * Return the tx control blocks in the pending list to
396 		 * the free list.
397 		 */
398 		ixgbe_put_free_list(tx_ring, &pending_list);
399 
400 		/*
401 		 * pull up the mblk and send it out with bind way
402 		 */
403 		if ((nmp = msgpullup(mp, -1)) == NULL) {
404 			freemsg(mp);
405 			return (NULL);
406 		} else {
407 			freemsg(mp);
408 			mp = nmp;
409 		}
410 
411 		LINK_LIST_INIT(&pending_list);
412 		tcb = ixgbe_get_free_list(tx_ring);
413 		if (tcb == NULL) {
414 			IXGBE_DEBUG_STAT(tx_ring->stat_fail_no_tcb);
415 			freemsg(mp);
416 			return (NULL);
417 		}
418 		LIST_PUSH_TAIL(&pending_list, &tcb->link);
419 
420 		desc_num = ixgbe_tx_bind(tx_ring, tcb, mp, mbsize);
421 		if ((desc_num < 0) ||
422 		    ((desc_num + 1) > IXGBE_TX_DESC_LIMIT)) {
423 			ixgbe_free_tcb(tcb);
424 			ixgbe_put_free_list(tx_ring, &pending_list);
425 			freemsg(mp);
426 			return (NULL);
427 		}
428 
429 		desc_total = desc_num;
430 		tcb->mp = mp;
431 	}
432 
433 	/*
434 	 * Before fill the tx descriptor ring with the data, we need to
435 	 * ensure there are adequate free descriptors for transmit
436 	 * (including one context descriptor).
437 	 */
438 	if (tx_ring->tbd_free < (desc_total + 1)) {
439 		tx_ring->tx_recycle(tx_ring);
440 	}
441 
442 	mutex_enter(&tx_ring->tx_lock);
443 
444 	/*
445 	 * If the number of free tx descriptors is not enough for transmit
446 	 * then return mp.
447 	 *
448 	 * Note: we must put this check under the mutex protection to
449 	 * ensure the correctness when multiple threads access it in
450 	 * parallel.
451 	 */
452 	if (tx_ring->tbd_free < (desc_total + 1)) {
453 		IXGBE_DEBUG_STAT(tx_ring->stat_fail_no_tbd);
454 		mutex_exit(&tx_ring->tx_lock);
455 		goto tx_failure;
456 	}
457 
458 	desc_num = ixgbe_tx_fill_ring(tx_ring, &pending_list, ctx,
459 	    mbsize);
460 
461 	ASSERT((desc_num == desc_total) || (desc_num == (desc_total + 1)));
462 
463 	mutex_exit(&tx_ring->tx_lock);
464 
465 	return (NULL);
466 
467 tx_failure:
468 	/*
469 	 * Discard the mblk and free the used resources
470 	 */
471 	tcb = (tx_control_block_t *)LIST_GET_HEAD(&pending_list);
472 	while (tcb) {
473 		tcb->mp = NULL;
474 
475 		ixgbe_free_tcb(tcb);
476 
477 		tcb = (tx_control_block_t *)
478 		    LIST_GET_NEXT(&pending_list, &tcb->link);
479 	}
480 
481 	/*
482 	 * Return the tx control blocks in the pending list to the free list.
483 	 */
484 	ixgbe_put_free_list(tx_ring, &pending_list);
485 
486 	/* Transmit failed, do not drop the mblk, rechedule the transmit */
487 	tx_ring->reschedule = B_TRUE;
488 
489 	return (mp);
490 }
491 
492 /*
493  * ixgbe_tx_copy
494  *
495  * Copy the mblk fragment to the pre-allocated tx buffer
496  */
497 static int
498 ixgbe_tx_copy(ixgbe_tx_ring_t *tx_ring, tx_control_block_t *tcb, mblk_t *mp,
499     uint32_t len, boolean_t copy_done)
500 {
501 	dma_buffer_t *tx_buf;
502 	uint32_t desc_num;
503 	_NOTE(ARGUNUSED(tx_ring));
504 
505 	tx_buf = &tcb->tx_buf;
506 
507 	/*
508 	 * Copy the packet data of the mblk fragment into the
509 	 * pre-allocated tx buffer, which is maintained by the
510 	 * tx control block.
511 	 *
512 	 * Several mblk fragments can be copied into one tx buffer.
513 	 * The destination address of the current copied fragment in
514 	 * the tx buffer is next to the end of the previous copied
515 	 * fragment.
516 	 */
517 	if (len > 0) {
518 		bcopy(mp->b_rptr, tx_buf->address + tx_buf->len, len);
519 
520 		tx_buf->len += len;
521 		tcb->frag_num++;
522 	}
523 
524 	desc_num = 0;
525 
526 	/*
527 	 * If it is the last fragment copied to the current tx buffer,
528 	 * in other words, if there's no remaining fragment or the remaining
529 	 * fragment requires a new tx control block to process, we need to
530 	 * complete the current copy processing by syncing up the current
531 	 * DMA buffer and saving the descriptor data.
532 	 */
533 	if (copy_done) {
534 		/*
535 		 * Sync the DMA buffer of the packet data
536 		 */
537 		DMA_SYNC(tx_buf, DDI_DMA_SYNC_FORDEV);
538 
539 		tcb->tx_type = USE_COPY;
540 
541 		/*
542 		 * Save the address and length to the private data structure
543 		 * of the tx control block, which will be used to fill the
544 		 * tx descriptor ring after all the fragments are processed.
545 		 */
546 		ixgbe_save_desc(tcb, tx_buf->dma_address, tx_buf->len);
547 		desc_num++;
548 	}
549 
550 	return (desc_num);
551 }
552 
553 /*
554  * ixgbe_tx_bind
555  *
556  * Bind the mblk fragment with DMA
557  */
558 static int
559 ixgbe_tx_bind(ixgbe_tx_ring_t *tx_ring, tx_control_block_t *tcb, mblk_t *mp,
560     uint32_t len)
561 {
562 	int status, i;
563 	ddi_dma_cookie_t dma_cookie;
564 	uint_t ncookies;
565 	int desc_num;
566 
567 	/*
568 	 * Use DMA binding to process the mblk fragment
569 	 */
570 	status = ddi_dma_addr_bind_handle(tcb->tx_dma_handle, NULL,
571 	    (caddr_t)mp->b_rptr, len,
572 	    DDI_DMA_WRITE | DDI_DMA_STREAMING, DDI_DMA_DONTWAIT,
573 	    0, &dma_cookie, &ncookies);
574 
575 	if (status != DDI_DMA_MAPPED) {
576 		IXGBE_DEBUG_STAT(tx_ring->stat_fail_dma_bind);
577 		return (-1);
578 	}
579 
580 	tcb->frag_num++;
581 	tcb->tx_type = USE_DMA;
582 	/*
583 	 * Each fragment can span several cookies. One cookie will have
584 	 * one tx descriptor to transmit.
585 	 */
586 	desc_num = 0;
587 	for (i = ncookies; i > 0; i--) {
588 		/*
589 		 * Save the address and length to the private data structure
590 		 * of the tx control block, which will be used to fill the
591 		 * tx descriptor ring after all the fragments are processed.
592 		 */
593 		ixgbe_save_desc(tcb,
594 		    dma_cookie.dmac_laddress,
595 		    dma_cookie.dmac_size);
596 
597 		desc_num++;
598 
599 		if (i > 1)
600 			ddi_dma_nextcookie(tcb->tx_dma_handle, &dma_cookie);
601 	}
602 
603 	return (desc_num);
604 }
605 
606 /*
607  * ixgbe_get_context
608  *
609  * Get the context information from the mblk
610  */
611 static int
612 ixgbe_get_context(mblk_t *mp, ixgbe_tx_context_t *ctx)
613 {
614 	uint32_t start;
615 	uint32_t hckflags;
616 	uint32_t lsoflags;
617 	uint32_t mss;
618 	uint32_t len;
619 	uint32_t size;
620 	uint32_t offset;
621 	unsigned char *pos;
622 	ushort_t etype;
623 	uint32_t mac_hdr_len;
624 	uint32_t l4_proto;
625 	uint32_t l4_hdr_len;
626 
627 	ASSERT(mp != NULL);
628 
629 	hcksum_retrieve(mp, NULL, NULL, &start, NULL, NULL, NULL, &hckflags);
630 	bzero(ctx, sizeof (ixgbe_tx_context_t));
631 
632 	if (hckflags == 0) {
633 		return (0);
634 	}
635 
636 	ctx->hcksum_flags = hckflags;
637 
638 	lso_info_get(mp, &mss, &lsoflags);
639 	ctx->mss = mss;
640 	ctx->lso_flag = (lsoflags == HW_LSO);
641 
642 	/*
643 	 * LSO relies on tx h/w checksum, so here will drop the package
644 	 * if h/w checksum flag is not declared.
645 	 */
646 	if (ctx->lso_flag) {
647 		if (!((ctx->hcksum_flags & HCK_PARTIALCKSUM) &&
648 		    (ctx->hcksum_flags & HCK_IPV4_HDRCKSUM))) {
649 			IXGBE_DEBUGLOG_0(NULL, "ixgbe_tx: h/w "
650 			    "checksum flags are not specified when doing LSO");
651 			return (-1);
652 		}
653 	}
654 
655 	etype = 0;
656 	mac_hdr_len = 0;
657 	l4_proto = 0;
658 
659 	/*
660 	 * Firstly get the position of the ether_type/ether_tpid.
661 	 * Here we don't assume the ether (VLAN) header is fully included
662 	 * in one mblk fragment, so we go thourgh the fragments to parse
663 	 * the ether type.
664 	 */
665 	size = len = MBLKL(mp);
666 	offset = offsetof(struct ether_header, ether_type);
667 	while (size <= offset) {
668 		mp = mp->b_cont;
669 		ASSERT(mp != NULL);
670 		len = MBLKL(mp);
671 		size += len;
672 	}
673 	pos = mp->b_rptr + offset + len - size;
674 
675 	etype = ntohs(*(ushort_t *)(uintptr_t)pos);
676 	if (etype == ETHERTYPE_VLAN) {
677 		/*
678 		 * Get the position of the ether_type in VLAN header
679 		 */
680 		offset = offsetof(struct ether_vlan_header, ether_type);
681 		while (size <= offset) {
682 			mp = mp->b_cont;
683 			ASSERT(mp != NULL);
684 			len = MBLKL(mp);
685 			size += len;
686 		}
687 		pos = mp->b_rptr + offset + len - size;
688 
689 		etype = ntohs(*(ushort_t *)(uintptr_t)pos);
690 		mac_hdr_len = sizeof (struct ether_vlan_header);
691 	} else {
692 		mac_hdr_len = sizeof (struct ether_header);
693 	}
694 
695 	/*
696 	 * Here we don't assume the IP(V6) header is fully included in
697 	 * one mblk fragment.
698 	 */
699 	switch (etype) {
700 	case ETHERTYPE_IP:
701 		if (ctx->lso_flag) {
702 			offset = offsetof(ipha_t, ipha_length) + mac_hdr_len;
703 			while (size <= offset) {
704 				mp = mp->b_cont;
705 				ASSERT(mp != NULL);
706 				len = MBLKL(mp);
707 				size += len;
708 			}
709 			pos = mp->b_rptr + offset + len - size;
710 			*((uint16_t *)(uintptr_t)(pos)) = 0;
711 
712 			offset = offsetof(ipha_t, ipha_hdr_checksum) +
713 			    mac_hdr_len;
714 			while (size <= offset) {
715 				mp = mp->b_cont;
716 				ASSERT(mp != NULL);
717 				len = MBLKL(mp);
718 				size += len;
719 			}
720 			pos = mp->b_rptr + offset + len - size;
721 			*((uint16_t *)(uintptr_t)(pos)) = 0;
722 
723 			/*
724 			 * To perform ixgbe LSO, here also need to fill
725 			 * the tcp checksum field of the packet with the
726 			 * following pseudo-header checksum:
727 			 * (ip_source_addr, ip_destination_addr, l4_proto)
728 			 * Currently the tcp/ip stack has done it.
729 			 */
730 		}
731 
732 		offset = offsetof(ipha_t, ipha_protocol) + mac_hdr_len;
733 		while (size <= offset) {
734 			mp = mp->b_cont;
735 			ASSERT(mp != NULL);
736 			len = MBLKL(mp);
737 			size += len;
738 		}
739 		pos = mp->b_rptr + offset + len - size;
740 
741 		l4_proto = *(uint8_t *)pos;
742 		break;
743 	case ETHERTYPE_IPV6:
744 		offset = offsetof(ip6_t, ip6_nxt) + mac_hdr_len;
745 		while (size <= offset) {
746 			mp = mp->b_cont;
747 			ASSERT(mp != NULL);
748 			len = MBLKL(mp);
749 			size += len;
750 		}
751 		pos = mp->b_rptr + offset + len - size;
752 
753 		l4_proto = *(uint8_t *)pos;
754 		break;
755 	default:
756 		/* Unrecoverable error */
757 		IXGBE_DEBUGLOG_0(NULL, "Ether type error with tx hcksum");
758 		return (-2);
759 	}
760 
761 	if (ctx->lso_flag) {
762 		offset = mac_hdr_len + start;
763 		while (size <= offset) {
764 			mp = mp->b_cont;
765 			ASSERT(mp != NULL);
766 			len = MBLKL(mp);
767 			size += len;
768 		}
769 		pos = mp->b_rptr + offset + len - size;
770 
771 		l4_hdr_len = TCP_HDR_LENGTH((tcph_t *)pos);
772 	} else {
773 		/*
774 		 * l4 header length is only required for LSO
775 		 */
776 		l4_hdr_len = 0;
777 	}
778 
779 	ctx->mac_hdr_len = mac_hdr_len;
780 	ctx->ip_hdr_len = start;
781 	ctx->l4_proto = l4_proto;
782 	ctx->l4_hdr_len = l4_hdr_len;
783 
784 	return (0);
785 }
786 
787 /*
788  * ixgbe_check_context
789  *
790  * Check if a new context descriptor is needed
791  */
792 static boolean_t
793 ixgbe_check_context(ixgbe_tx_ring_t *tx_ring, ixgbe_tx_context_t *ctx)
794 {
795 	ixgbe_tx_context_t *last;
796 
797 	if (ctx == NULL)
798 		return (B_FALSE);
799 
800 	/*
801 	 * Compare the context data retrieved from the mblk and the
802 	 * stored data of the last context descriptor. The data need
803 	 * to be checked are:
804 	 *	hcksum_flags
805 	 *	l4_proto
806 	 *	mac_hdr_len
807 	 *	ip_hdr_len
808 	 *	lso_flag
809 	 *	mss (only checked for LSO)
810 	 *	l4_hr_len (only checked for LSO)
811 	 * Either one of the above data is changed, a new context descriptor
812 	 * will be needed.
813 	 */
814 	last = &tx_ring->tx_context;
815 
816 	if ((ctx->hcksum_flags != last->hcksum_flags) ||
817 	    (ctx->l4_proto != last->l4_proto) ||
818 	    (ctx->mac_hdr_len != last->mac_hdr_len) ||
819 	    (ctx->ip_hdr_len != last->ip_hdr_len) ||
820 	    (ctx->lso_flag != last->lso_flag) ||
821 	    (ctx->lso_flag && ((ctx->mss != last->mss) ||
822 	    (ctx->l4_hdr_len != last->l4_hdr_len)))) {
823 		return (B_TRUE);
824 	}
825 
826 	return (B_FALSE);
827 }
828 
829 /*
830  * ixgbe_fill_context
831  *
832  * Fill the context descriptor with hardware checksum informations
833  */
834 static void
835 ixgbe_fill_context(struct ixgbe_adv_tx_context_desc *ctx_tbd,
836     ixgbe_tx_context_t *ctx)
837 {
838 	/*
839 	 * Fill the context descriptor with the checksum
840 	 * context information we've got.
841 	 */
842 	ctx_tbd->vlan_macip_lens = ctx->ip_hdr_len;
843 	ctx_tbd->vlan_macip_lens |= ctx->mac_hdr_len <<
844 	    IXGBE_ADVTXD_MACLEN_SHIFT;
845 
846 	ctx_tbd->type_tucmd_mlhl =
847 	    IXGBE_ADVTXD_DCMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
848 
849 	if (ctx->hcksum_flags & HCK_IPV4_HDRCKSUM)
850 		ctx_tbd->type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
851 
852 	if (ctx->hcksum_flags & HCK_PARTIALCKSUM) {
853 		switch (ctx->l4_proto) {
854 		case IPPROTO_TCP:
855 			ctx_tbd->type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
856 			break;
857 		case IPPROTO_UDP:
858 			/*
859 			 * We don't have to explicitly set:
860 			 *	ctx_tbd->type_tucmd_mlhl |=
861 			 *	    IXGBE_ADVTXD_TUCMD_L4T_UDP;
862 			 * Because IXGBE_ADVTXD_TUCMD_L4T_UDP == 0b
863 			 */
864 			break;
865 		default:
866 			/* Unrecoverable error */
867 			IXGBE_DEBUGLOG_0(NULL, "L4 type error with tx hcksum");
868 			break;
869 		}
870 	}
871 
872 	ctx_tbd->seqnum_seed = 0;
873 
874 	if (ctx->lso_flag) {
875 		ctx_tbd->mss_l4len_idx =
876 		    (ctx->l4_hdr_len << IXGBE_ADVTXD_L4LEN_SHIFT) |
877 		    (ctx->mss << IXGBE_ADVTXD_MSS_SHIFT);
878 	} else {
879 		ctx_tbd->mss_l4len_idx = 0;
880 	}
881 }
882 
883 /*
884  * ixgbe_tx_fill_ring
885  *
886  * Fill the tx descriptor ring with the data
887  */
888 static int
889 ixgbe_tx_fill_ring(ixgbe_tx_ring_t *tx_ring, link_list_t *pending_list,
890     ixgbe_tx_context_t *ctx, size_t mbsize)
891 {
892 	struct ixgbe_hw *hw = &tx_ring->ixgbe->hw;
893 	boolean_t load_context;
894 	uint32_t index, tcb_index, desc_num;
895 	union ixgbe_adv_tx_desc *tbd, *first_tbd;
896 	tx_control_block_t *tcb, *first_tcb;
897 	uint32_t hcksum_flags;
898 	int i;
899 
900 	ASSERT(mutex_owned(&tx_ring->tx_lock));
901 
902 	tbd = NULL;
903 	first_tbd = NULL;
904 	first_tcb = NULL;
905 	desc_num = 0;
906 	hcksum_flags = 0;
907 	load_context = B_FALSE;
908 
909 	/*
910 	 * Get the index of the first tx descriptor that will be filled,
911 	 * and the index of the first work list item that will be attached
912 	 * with the first used tx control block in the pending list.
913 	 * Note: the two indexes are the same.
914 	 */
915 	index = tx_ring->tbd_tail;
916 	tcb_index = tx_ring->tbd_tail;
917 
918 	if (ctx != NULL) {
919 		hcksum_flags = ctx->hcksum_flags;
920 
921 		/*
922 		 * Check if a new context descriptor is needed for this packet
923 		 */
924 		load_context = ixgbe_check_context(tx_ring, ctx);
925 
926 		if (load_context) {
927 			tbd = &tx_ring->tbd_ring[index];
928 
929 			/*
930 			 * Fill the context descriptor with the
931 			 * hardware checksum offload informations.
932 			 */
933 			ixgbe_fill_context(
934 			    (struct ixgbe_adv_tx_context_desc *)tbd, ctx);
935 
936 			index = NEXT_INDEX(index, 1, tx_ring->ring_size);
937 			desc_num++;
938 
939 			/*
940 			 * Store the checksum context data if
941 			 * a new context descriptor is added
942 			 */
943 			tx_ring->tx_context = *ctx;
944 		}
945 	}
946 
947 	first_tbd = &tx_ring->tbd_ring[index];
948 
949 	/*
950 	 * Fill tx data descriptors with the data saved in the pending list.
951 	 * The tx control blocks in the pending list are added to the work list
952 	 * at the same time.
953 	 *
954 	 * The work list is strictly 1:1 corresponding to the descriptor ring.
955 	 * One item of the work list corresponds to one tx descriptor. Because
956 	 * one tx control block can span multiple tx descriptors, the tx
957 	 * control block will be added to the first work list item that
958 	 * corresponds to the first tx descriptor generated from that tx
959 	 * control block.
960 	 */
961 	tcb = (tx_control_block_t *)LIST_POP_HEAD(pending_list);
962 	first_tcb = tcb;
963 	while (tcb != NULL) {
964 
965 		for (i = 0; i < tcb->desc_num; i++) {
966 			tbd = &tx_ring->tbd_ring[index];
967 
968 			tbd->read.buffer_addr = tcb->desc[i].address;
969 			tbd->read.cmd_type_len = tcb->desc[i].length;
970 
971 			tbd->read.cmd_type_len |= IXGBE_ADVTXD_DCMD_DEXT
972 			    | IXGBE_ADVTXD_DTYP_DATA;
973 
974 			tbd->read.olinfo_status = 0;
975 
976 			index = NEXT_INDEX(index, 1, tx_ring->ring_size);
977 			desc_num++;
978 		}
979 
980 		/*
981 		 * Add the tx control block to the work list
982 		 */
983 		ASSERT(tx_ring->work_list[tcb_index] == NULL);
984 		tx_ring->work_list[tcb_index] = tcb;
985 
986 		tcb_index = index;
987 		tcb = (tx_control_block_t *)LIST_POP_HEAD(pending_list);
988 	}
989 
990 	if (load_context) {
991 		/*
992 		 * Count the context descriptor for
993 		 * the first tx control block.
994 		 */
995 		first_tcb->desc_num++;
996 	}
997 	first_tcb->last_index = PREV_INDEX(index, 1, tx_ring->ring_size);
998 
999 	/*
1000 	 * The Insert Ethernet CRC (IFCS) bit and the checksum fields are only
1001 	 * valid in the first descriptor of the packet.
1002 	 * Setting paylen in every first_tbd for all parts.
1003 	 * 82599 requires the packet length in paylen field with or without
1004 	 * LSO and 82598 will ignore it in non-LSO mode.
1005 	 */
1006 	ASSERT(first_tbd != NULL);
1007 	first_tbd->read.cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS;
1008 
1009 	switch (hw->mac.type) {
1010 	case ixgbe_mac_82599EB:
1011 		if (ctx != NULL && ctx->lso_flag) {
1012 			first_tbd->read.cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
1013 			first_tbd->read.olinfo_status |=
1014 			    (mbsize - ctx->mac_hdr_len - ctx->ip_hdr_len
1015 			    - ctx->l4_hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT;
1016 		} else {
1017 			first_tbd->read.olinfo_status |=
1018 			    (mbsize << IXGBE_ADVTXD_PAYLEN_SHIFT);
1019 		}
1020 		break;
1021 	case ixgbe_mac_82598EB:
1022 		if (ctx != NULL && ctx->lso_flag) {
1023 			first_tbd->read.cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
1024 			first_tbd->read.olinfo_status |=
1025 			    (mbsize - ctx->mac_hdr_len - ctx->ip_hdr_len
1026 			    - ctx->l4_hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT;
1027 		}
1028 		break;
1029 	default:
1030 		break;
1031 	}
1032 
1033 	/* Set hardware checksum bits */
1034 	if (hcksum_flags != 0) {
1035 		if (hcksum_flags & HCK_IPV4_HDRCKSUM)
1036 			first_tbd->read.olinfo_status |=
1037 			    IXGBE_ADVTXD_POPTS_IXSM;
1038 		if (hcksum_flags & HCK_PARTIALCKSUM)
1039 			first_tbd->read.olinfo_status |=
1040 			    IXGBE_ADVTXD_POPTS_TXSM;
1041 	}
1042 
1043 	/*
1044 	 * The last descriptor of packet needs End Of Packet (EOP),
1045 	 * and Report Status (RS) bits set
1046 	 */
1047 	ASSERT(tbd != NULL);
1048 	tbd->read.cmd_type_len |=
1049 	    IXGBE_ADVTXD_DCMD_EOP | IXGBE_ADVTXD_DCMD_RS;
1050 
1051 	/*
1052 	 * Sync the DMA buffer of the tx descriptor ring
1053 	 */
1054 	DMA_SYNC(&tx_ring->tbd_area, DDI_DMA_SYNC_FORDEV);
1055 
1056 	if (ixgbe_check_dma_handle(tx_ring->tbd_area.dma_handle) != DDI_FM_OK) {
1057 		ddi_fm_service_impact(tx_ring->ixgbe->dip,
1058 		    DDI_SERVICE_DEGRADED);
1059 	}
1060 
1061 	/*
1062 	 * Update the number of the free tx descriptors.
1063 	 * The mutual exclusion between the transmission and the recycling
1064 	 * (for the tx descriptor ring and the work list) is implemented
1065 	 * with the atomic operation on the number of the free tx descriptors.
1066 	 *
1067 	 * Note: we should always decrement the counter tbd_free before
1068 	 * advancing the hardware TDT pointer to avoid the race condition -
1069 	 * before the counter tbd_free is decremented, the transmit of the
1070 	 * tx descriptors has done and the counter tbd_free is increased by
1071 	 * the tx recycling.
1072 	 */
1073 	i = ixgbe_atomic_reserve(&tx_ring->tbd_free, desc_num);
1074 	ASSERT(i >= 0);
1075 
1076 	tx_ring->tbd_tail = index;
1077 
1078 	/*
1079 	 * Advance the hardware TDT pointer of the tx descriptor ring
1080 	 */
1081 	IXGBE_WRITE_REG(hw, IXGBE_TDT(tx_ring->index), index);
1082 
1083 	if (ixgbe_check_acc_handle(tx_ring->ixgbe->osdep.reg_handle) !=
1084 	    DDI_FM_OK) {
1085 		ddi_fm_service_impact(tx_ring->ixgbe->dip,
1086 		    DDI_SERVICE_DEGRADED);
1087 	}
1088 
1089 	return (desc_num);
1090 }
1091 
1092 /*
1093  * ixgbe_save_desc
1094  *
1095  * Save the address/length pair to the private array
1096  * of the tx control block. The address/length pairs
1097  * will be filled into the tx descriptor ring later.
1098  */
1099 static void
1100 ixgbe_save_desc(tx_control_block_t *tcb, uint64_t address, size_t length)
1101 {
1102 	sw_desc_t *desc;
1103 
1104 	desc = &tcb->desc[tcb->desc_num];
1105 	desc->address = address;
1106 	desc->length = length;
1107 
1108 	tcb->desc_num++;
1109 }
1110 
1111 /*
1112  * ixgbe_tx_recycle_legacy
1113  *
1114  * Recycle the tx descriptors and tx control blocks.
1115  *
1116  * The work list is traversed to check if the corresponding
1117  * tx descriptors have been transmitted. If so, the resources
1118  * bound to the tx control blocks will be freed, and those
1119  * tx control blocks will be returned to the free list.
1120  */
1121 uint32_t
1122 ixgbe_tx_recycle_legacy(ixgbe_tx_ring_t *tx_ring)
1123 {
1124 	uint32_t index, last_index, prev_index;
1125 	int desc_num;
1126 	boolean_t desc_done;
1127 	tx_control_block_t *tcb;
1128 	link_list_t pending_list;
1129 
1130 	mutex_enter(&tx_ring->recycle_lock);
1131 
1132 	ASSERT(tx_ring->tbd_free <= tx_ring->ring_size);
1133 
1134 	if (tx_ring->tbd_free == tx_ring->ring_size) {
1135 		tx_ring->recycle_fail = 0;
1136 		tx_ring->stall_watchdog = 0;
1137 		if (tx_ring->reschedule) {
1138 			tx_ring->reschedule = B_FALSE;
1139 			mac_tx_ring_update(tx_ring->ixgbe->mac_hdl,
1140 			    tx_ring->ring_handle);
1141 		}
1142 		mutex_exit(&tx_ring->recycle_lock);
1143 		return (0);
1144 	}
1145 
1146 	/*
1147 	 * Sync the DMA buffer of the tx descriptor ring
1148 	 */
1149 	DMA_SYNC(&tx_ring->tbd_area, DDI_DMA_SYNC_FORKERNEL);
1150 
1151 	if (ixgbe_check_dma_handle(tx_ring->tbd_area.dma_handle) != DDI_FM_OK) {
1152 		ddi_fm_service_impact(tx_ring->ixgbe->dip,
1153 		    DDI_SERVICE_DEGRADED);
1154 	}
1155 
1156 	LINK_LIST_INIT(&pending_list);
1157 	desc_num = 0;
1158 	index = tx_ring->tbd_head;	/* Index of next tbd/tcb to recycle */
1159 
1160 	tcb = tx_ring->work_list[index];
1161 	ASSERT(tcb != NULL);
1162 
1163 	while (tcb != NULL) {
1164 		/*
1165 		 * Get the last tx descriptor of this packet.
1166 		 * If the last tx descriptor is done, then
1167 		 * we can recycle all descriptors of a packet
1168 		 * which usually includes several tx control blocks.
1169 		 * For 82599, LSO descriptors can not be recycled
1170 		 * unless the whole packet's transmission is done.
1171 		 * That's why packet level recycling is used here.
1172 		 * For 82598, there's not such limit.
1173 		 */
1174 		last_index = tcb->last_index;
1175 		/*
1176 		 * MAX_TX_RING_SIZE is used to judge whether
1177 		 * the index is a valid value or not.
1178 		 */
1179 		if (last_index == MAX_TX_RING_SIZE)
1180 			break;
1181 
1182 		/*
1183 		 * Check if the Descriptor Done bit is set
1184 		 */
1185 		desc_done = tx_ring->tbd_ring[last_index].wb.status &
1186 		    IXGBE_TXD_STAT_DD;
1187 		if (desc_done) {
1188 			/*
1189 			 * recycle all descriptors of the packet
1190 			 */
1191 			while (tcb != NULL) {
1192 				/*
1193 				 * Strip off the tx control block from
1194 				 * the work list, and add it to the
1195 				 * pending list.
1196 				 */
1197 				tx_ring->work_list[index] = NULL;
1198 				LIST_PUSH_TAIL(&pending_list, &tcb->link);
1199 
1200 				/*
1201 				 * Count the total number of the tx
1202 				 * descriptors recycled
1203 				 */
1204 				desc_num += tcb->desc_num;
1205 
1206 				index = NEXT_INDEX(index, tcb->desc_num,
1207 				    tx_ring->ring_size);
1208 
1209 				tcb = tx_ring->work_list[index];
1210 
1211 				prev_index = PREV_INDEX(index, 1,
1212 				    tx_ring->ring_size);
1213 				if (prev_index == last_index)
1214 					break;
1215 			}
1216 		} else {
1217 			break;
1218 		}
1219 	}
1220 
1221 	/*
1222 	 * If no tx descriptors are recycled, no need to do more processing
1223 	 */
1224 	if (desc_num == 0) {
1225 		tx_ring->recycle_fail++;
1226 		mutex_exit(&tx_ring->recycle_lock);
1227 		return (0);
1228 	}
1229 
1230 	tx_ring->recycle_fail = 0;
1231 	tx_ring->stall_watchdog = 0;
1232 
1233 	/*
1234 	 * Update the head index of the tx descriptor ring
1235 	 */
1236 	tx_ring->tbd_head = index;
1237 
1238 	/*
1239 	 * Update the number of the free tx descriptors with atomic operations
1240 	 */
1241 	atomic_add_32(&tx_ring->tbd_free, desc_num);
1242 
1243 	if ((tx_ring->tbd_free >= tx_ring->resched_thresh) &&
1244 	    (tx_ring->reschedule)) {
1245 		tx_ring->reschedule = B_FALSE;
1246 		mac_tx_ring_update(tx_ring->ixgbe->mac_hdl,
1247 		    tx_ring->ring_handle);
1248 	}
1249 	mutex_exit(&tx_ring->recycle_lock);
1250 
1251 	/*
1252 	 * Free the resources used by the tx control blocks
1253 	 * in the pending list
1254 	 */
1255 	tcb = (tx_control_block_t *)LIST_GET_HEAD(&pending_list);
1256 	while (tcb != NULL) {
1257 		/*
1258 		 * Release the resources occupied by the tx control block
1259 		 */
1260 		ixgbe_free_tcb(tcb);
1261 
1262 		tcb = (tx_control_block_t *)
1263 		    LIST_GET_NEXT(&pending_list, &tcb->link);
1264 	}
1265 
1266 	/*
1267 	 * Add the tx control blocks in the pending list to the free list.
1268 	 */
1269 	ixgbe_put_free_list(tx_ring, &pending_list);
1270 
1271 	return (desc_num);
1272 }
1273 
1274 /*
1275  * ixgbe_tx_recycle_head_wb
1276  *
1277  * Check the head write-back, and recycle all the transmitted
1278  * tx descriptors and tx control blocks.
1279  */
1280 uint32_t
1281 ixgbe_tx_recycle_head_wb(ixgbe_tx_ring_t *tx_ring)
1282 {
1283 	uint32_t index;
1284 	uint32_t head_wb;
1285 	int desc_num;
1286 	tx_control_block_t *tcb;
1287 	link_list_t pending_list;
1288 
1289 	/*
1290 	 * The mutex_tryenter() is used to avoid unnecessary
1291 	 * lock contention.
1292 	 */
1293 	mutex_enter(&tx_ring->recycle_lock);
1294 
1295 	ASSERT(tx_ring->tbd_free <= tx_ring->ring_size);
1296 
1297 	if (tx_ring->tbd_free == tx_ring->ring_size) {
1298 		tx_ring->recycle_fail = 0;
1299 		tx_ring->stall_watchdog = 0;
1300 		if (tx_ring->reschedule) {
1301 			tx_ring->reschedule = B_FALSE;
1302 			mac_tx_ring_update(tx_ring->ixgbe->mac_hdl,
1303 			    tx_ring->ring_handle);
1304 		}
1305 		mutex_exit(&tx_ring->recycle_lock);
1306 		return (0);
1307 	}
1308 
1309 	/*
1310 	 * Sync the DMA buffer of the tx descriptor ring
1311 	 *
1312 	 * Note: For head write-back mode, the tx descriptors will not
1313 	 * be written back, but the head write-back value is stored at
1314 	 * the last extra tbd at the end of the DMA area, we still need
1315 	 * to sync the head write-back value for kernel.
1316 	 *
1317 	 * DMA_SYNC(&tx_ring->tbd_area, DDI_DMA_SYNC_FORKERNEL);
1318 	 */
1319 	(void) ddi_dma_sync(tx_ring->tbd_area.dma_handle,
1320 	    sizeof (union ixgbe_adv_tx_desc) * tx_ring->ring_size,
1321 	    sizeof (uint32_t),
1322 	    DDI_DMA_SYNC_FORKERNEL);
1323 
1324 	if (ixgbe_check_dma_handle(tx_ring->tbd_area.dma_handle) != DDI_FM_OK) {
1325 		ddi_fm_service_impact(tx_ring->ixgbe->dip,
1326 		    DDI_SERVICE_DEGRADED);
1327 	}
1328 
1329 	LINK_LIST_INIT(&pending_list);
1330 	desc_num = 0;
1331 	index = tx_ring->tbd_head;	/* Next index to clean */
1332 
1333 	/*
1334 	 * Get the value of head write-back
1335 	 */
1336 	head_wb = *tx_ring->tbd_head_wb;
1337 	while (index != head_wb) {
1338 		tcb = tx_ring->work_list[index];
1339 		ASSERT(tcb != NULL);
1340 
1341 		if (OFFSET(index, head_wb, tx_ring->ring_size) <
1342 		    tcb->desc_num) {
1343 			/*
1344 			 * The current tx control block is not
1345 			 * completely transmitted, stop recycling
1346 			 */
1347 			break;
1348 		}
1349 
1350 		/*
1351 		 * Strip off the tx control block from the work list,
1352 		 * and add it to the pending list.
1353 		 */
1354 		tx_ring->work_list[index] = NULL;
1355 		LIST_PUSH_TAIL(&pending_list, &tcb->link);
1356 
1357 		/*
1358 		 * Advance the index of the tx descriptor ring
1359 		 */
1360 		index = NEXT_INDEX(index, tcb->desc_num, tx_ring->ring_size);
1361 
1362 		/*
1363 		 * Count the total number of the tx descriptors recycled
1364 		 */
1365 		desc_num += tcb->desc_num;
1366 	}
1367 
1368 	/*
1369 	 * If no tx descriptors are recycled, no need to do more processing
1370 	 */
1371 	if (desc_num == 0) {
1372 		tx_ring->recycle_fail++;
1373 		mutex_exit(&tx_ring->recycle_lock);
1374 		return (0);
1375 	}
1376 
1377 	tx_ring->recycle_fail = 0;
1378 	tx_ring->stall_watchdog = 0;
1379 
1380 	/*
1381 	 * Update the head index of the tx descriptor ring
1382 	 */
1383 	tx_ring->tbd_head = index;
1384 
1385 	/*
1386 	 * Update the number of the free tx descriptors with atomic operations
1387 	 */
1388 	atomic_add_32(&tx_ring->tbd_free, desc_num);
1389 
1390 	if ((tx_ring->tbd_free >= tx_ring->resched_thresh) &&
1391 	    (tx_ring->reschedule)) {
1392 		tx_ring->reschedule = B_FALSE;
1393 		mac_tx_ring_update(tx_ring->ixgbe->mac_hdl,
1394 		    tx_ring->ring_handle);
1395 	}
1396 	mutex_exit(&tx_ring->recycle_lock);
1397 
1398 	/*
1399 	 * Free the resources used by the tx control blocks
1400 	 * in the pending list
1401 	 */
1402 	tcb = (tx_control_block_t *)LIST_GET_HEAD(&pending_list);
1403 	while (tcb) {
1404 		/*
1405 		 * Release the resources occupied by the tx control block
1406 		 */
1407 		ixgbe_free_tcb(tcb);
1408 
1409 		tcb = (tx_control_block_t *)
1410 		    LIST_GET_NEXT(&pending_list, &tcb->link);
1411 	}
1412 
1413 	/*
1414 	 * Add the tx control blocks in the pending list to the free list.
1415 	 */
1416 	ixgbe_put_free_list(tx_ring, &pending_list);
1417 
1418 	return (desc_num);
1419 }
1420 
1421 /*
1422  * ixgbe_free_tcb - free up the tx control block
1423  *
1424  * Free the resources of the tx control block, including
1425  * unbind the previously bound DMA handle, and reset other
1426  * control fields.
1427  */
1428 void
1429 ixgbe_free_tcb(tx_control_block_t *tcb)
1430 {
1431 	switch (tcb->tx_type) {
1432 	case USE_COPY:
1433 		/*
1434 		 * Reset the buffer length that is used for copy
1435 		 */
1436 		tcb->tx_buf.len = 0;
1437 		break;
1438 	case USE_DMA:
1439 		/*
1440 		 * Release the DMA resource that is used for
1441 		 * DMA binding.
1442 		 */
1443 		(void) ddi_dma_unbind_handle(tcb->tx_dma_handle);
1444 		break;
1445 	default:
1446 		break;
1447 	}
1448 
1449 	/*
1450 	 * Free the mblk
1451 	 */
1452 	if (tcb->mp != NULL) {
1453 		freemsg(tcb->mp);
1454 		tcb->mp = NULL;
1455 	}
1456 
1457 	tcb->tx_type = USE_NONE;
1458 	tcb->last_index = MAX_TX_RING_SIZE;
1459 	tcb->frag_num = 0;
1460 	tcb->desc_num = 0;
1461 }
1462 
1463 /*
1464  * ixgbe_get_free_list - Get a free tx control block from the free list
1465  *
1466  * The atomic operation on the number of the available tx control block
1467  * in the free list is used to keep this routine mutual exclusive with
1468  * the routine ixgbe_put_check_list.
1469  */
1470 static tx_control_block_t *
1471 ixgbe_get_free_list(ixgbe_tx_ring_t *tx_ring)
1472 {
1473 	tx_control_block_t *tcb;
1474 
1475 	/*
1476 	 * Check and update the number of the free tx control block
1477 	 * in the free list.
1478 	 */
1479 	if (ixgbe_atomic_reserve(&tx_ring->tcb_free, 1) < 0)
1480 		return (NULL);
1481 
1482 	mutex_enter(&tx_ring->tcb_head_lock);
1483 
1484 	tcb = tx_ring->free_list[tx_ring->tcb_head];
1485 	ASSERT(tcb != NULL);
1486 	tx_ring->free_list[tx_ring->tcb_head] = NULL;
1487 	tx_ring->tcb_head = NEXT_INDEX(tx_ring->tcb_head, 1,
1488 	    tx_ring->free_list_size);
1489 
1490 	mutex_exit(&tx_ring->tcb_head_lock);
1491 
1492 	return (tcb);
1493 }
1494 
1495 /*
1496  * ixgbe_put_free_list
1497  *
1498  * Put a list of used tx control blocks back to the free list
1499  *
1500  * A mutex is used here to ensure the serialization. The mutual exclusion
1501  * between ixgbe_get_free_list and ixgbe_put_free_list is implemented with
1502  * the atomic operation on the counter tcb_free.
1503  */
1504 void
1505 ixgbe_put_free_list(ixgbe_tx_ring_t *tx_ring, link_list_t *pending_list)
1506 {
1507 	uint32_t index;
1508 	int tcb_num;
1509 	tx_control_block_t *tcb;
1510 
1511 	mutex_enter(&tx_ring->tcb_tail_lock);
1512 
1513 	index = tx_ring->tcb_tail;
1514 
1515 	tcb_num = 0;
1516 	tcb = (tx_control_block_t *)LIST_POP_HEAD(pending_list);
1517 	while (tcb != NULL) {
1518 		ASSERT(tx_ring->free_list[index] == NULL);
1519 		tx_ring->free_list[index] = tcb;
1520 
1521 		tcb_num++;
1522 
1523 		index = NEXT_INDEX(index, 1, tx_ring->free_list_size);
1524 
1525 		tcb = (tx_control_block_t *)LIST_POP_HEAD(pending_list);
1526 	}
1527 
1528 	tx_ring->tcb_tail = index;
1529 
1530 	/*
1531 	 * Update the number of the free tx control block
1532 	 * in the free list. This operation must be placed
1533 	 * under the protection of the lock.
1534 	 */
1535 	atomic_add_32(&tx_ring->tcb_free, tcb_num);
1536 
1537 	mutex_exit(&tx_ring->tcb_tail_lock);
1538 }
1539