xref: /titanic_52/usr/src/uts/common/io/igb/igb_tx.c (revision c2785286330b58810071fc24f6ca69f401682dff)
1 /*
2  * CDDL HEADER START
3  *
4  * Copyright(c) 2007-2008 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:
10  *	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 using or redistributing this file, you may do so under the
15  * License only. No other modification of this header is permitted.
16  *
17  * If applicable, add the following below this CDDL HEADER, with the
18  * fields enclosed by brackets "[]" replaced with your own identifying
19  * information: Portions Copyright [yyyy] [name of copyright owner]
20  *
21  * CDDL HEADER END
22  */
23 
24 /*
25  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms of the CDDL.
27  */
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include "igb_sw.h"
32 
33 static boolean_t igb_tx(igb_tx_ring_t *, mblk_t *);
34 static int igb_tx_copy(igb_tx_ring_t *, tx_control_block_t *, mblk_t *,
35     uint32_t, boolean_t, boolean_t);
36 static int igb_tx_bind(igb_tx_ring_t *, tx_control_block_t *, mblk_t *,
37     uint32_t);
38 static int igb_tx_fill_ring(igb_tx_ring_t *, link_list_t *, hcksum_context_t *);
39 static void igb_save_desc(tx_control_block_t *, uint64_t, size_t);
40 static tx_control_block_t *igb_get_free_list(igb_tx_ring_t *);
41 
42 static void igb_get_hcksum_context(mblk_t *, hcksum_context_t *);
43 static boolean_t igb_check_hcksum_context(igb_tx_ring_t *, hcksum_context_t *);
44 static void igb_fill_hcksum_context(struct e1000_adv_tx_context_desc *,
45     hcksum_context_t *);
46 
47 #ifndef IGB_DEBUG
48 #pragma inline(igb_save_desc)
49 #pragma inline(igb_get_hcksum_context)
50 #pragma inline(igb_check_hcksum_context)
51 #pragma inline(igb_fill_hcksum_context)
52 #endif
53 
54 /*
55  * igb_m_tx
56  *
57  * The GLDv3 interface to call driver's tx routine to transmit
58  * the mblks.
59  */
60 mblk_t *
61 igb_m_tx(void *arg, mblk_t *mp)
62 {
63 	igb_t *igb = (igb_t *)arg;
64 	mblk_t *next;
65 	igb_tx_ring_t *tx_ring;
66 
67 	/*
68 	 * If the adapter is suspended, or it is not started, or the link
69 	 * is not up, the mblks are simply dropped.
70 	 */
71 	if (((igb->igb_state & IGB_SUSPENDED) != 0) ||
72 	    ((igb->igb_state & IGB_STARTED) == 0) ||
73 	    (igb->link_state != LINK_STATE_UP)) {
74 		/* Free the mblk chain */
75 		while (mp != NULL) {
76 			next = mp->b_next;
77 			mp->b_next = NULL;
78 
79 			freemsg(mp);
80 			mp = next;
81 		}
82 
83 		return (NULL);
84 	}
85 
86 	/*
87 	 * Decide which tx ring is used to transmit the packets.
88 	 * This needs to be updated later to fit the new interface
89 	 * of the multiple rings support.
90 	 */
91 	tx_ring = &igb->tx_rings[0];
92 
93 	while (mp != NULL) {
94 		next = mp->b_next;
95 		mp->b_next = NULL;
96 
97 		if (!igb_tx(tx_ring, mp)) {
98 			mp->b_next = next;
99 			break;
100 		}
101 
102 		mp = next;
103 	}
104 
105 	return (mp);
106 }
107 
108 /*
109  * igb_tx - Main transmit processing
110  *
111  * Called from igb_m_tx with an mblk ready to transmit. this
112  * routine sets up the transmit descriptors and sends data to
113  * the wire.
114  *
115  * One mblk can consist of several fragments, each fragment
116  * will be processed with different methods based on the size.
117  * For the fragments with size less than the bcopy threshold,
118  * they will be processed by using bcopy; otherwise, they will
119  * be processed by using DMA binding.
120  *
121  * To process the mblk, a tx control block is got from the
122  * free list. One tx control block contains one tx buffer, which
123  * is used to copy mblk fragments' data; and one tx DMA handle,
124  * which is used to bind a mblk fragment with DMA resource.
125  *
126  * Several small mblk fragments can be copied into one tx control
127  * block's buffer, and then the buffer will be transmitted with
128  * one tx descriptor.
129  *
130  * A large fragment only binds with one tx control block's DMA
131  * handle, and it can span several tx descriptors for transmitting.
132  *
133  * So to transmit a packet (mblk), several tx control blocks can
134  * be used. After the processing, those tx control blocks will
135  * be put to the work list.
136  */
137 static boolean_t
138 igb_tx(igb_tx_ring_t *tx_ring, mblk_t *mp)
139 {
140 	igb_t *igb = tx_ring->igb;
141 	tx_type_t current_flag, next_flag;
142 	uint32_t current_len, next_len;
143 	uint32_t desc_total;
144 	size_t mbsize;
145 	int desc_num;
146 	boolean_t copy_done, eop;
147 	mblk_t *current_mp, *next_mp, *nmp;
148 	tx_control_block_t *tcb;
149 	hcksum_context_t hcksum_context, *hcksum;
150 	link_list_t pending_list;
151 
152 	/* Get the mblk size */
153 	mbsize = 0;
154 	for (nmp = mp; nmp != NULL; nmp = nmp->b_cont) {
155 		mbsize += MBLK_LEN(nmp);
156 	}
157 
158 	/*
159 	 * If the mblk size exceeds the max frame size,
160 	 * discard this mblk, and return B_TRUE
161 	 */
162 	if (mbsize > (igb->max_frame_size - ETHERFCSL)) {
163 		freemsg(mp);
164 		IGB_DEBUGLOG_0(igb, "igb_tx: packet oversize");
165 		return (B_TRUE);
166 	}
167 
168 	/*
169 	 * Check and recycle tx descriptors.
170 	 * The recycle threshold here should be selected carefully
171 	 */
172 	if (tx_ring->tbd_free < tx_ring->recycle_thresh)
173 		tx_ring->tx_recycle(tx_ring);
174 
175 	/*
176 	 * After the recycling, if the tbd_free is less than the
177 	 * overload_threshold, assert overload, return B_FALSE;
178 	 * and we need to re-schedule the tx again.
179 	 */
180 	if (tx_ring->tbd_free < tx_ring->overload_thresh) {
181 		tx_ring->reschedule = B_TRUE;
182 		IGB_DEBUG_STAT(tx_ring->stat_overload);
183 		return (B_FALSE);
184 	}
185 
186 	/*
187 	 * The pending_list is a linked list that is used to save
188 	 * the tx control blocks that have packet data processed
189 	 * but have not put the data to the tx descriptor ring.
190 	 * It is used to reduce the lock contention of the tx_lock.
191 	 */
192 	LINK_LIST_INIT(&pending_list);
193 	desc_num = 0;
194 	desc_total = 0;
195 
196 	current_mp = mp;
197 	current_len = MBLK_LEN(current_mp);
198 	/*
199 	 * Decide which method to use for the first fragment
200 	 */
201 	current_flag = (current_len <= tx_ring->copy_thresh) ?
202 	    USE_COPY : USE_DMA;
203 	/*
204 	 * If the mblk includes several contiguous small fragments,
205 	 * they may be copied into one buffer. This flag is used to
206 	 * indicate whether there are pending fragments that need to
207 	 * be copied to the current tx buffer.
208 	 *
209 	 * If this flag is B_TRUE, it indicates that a new tx control
210 	 * block is needed to process the next fragment using either
211 	 * copy or DMA binding.
212 	 *
213 	 * Otherwise, it indicates that the next fragment will be
214 	 * copied to the current tx buffer that is maintained by the
215 	 * current tx control block. No new tx control block is needed.
216 	 */
217 	copy_done = B_TRUE;
218 	while (current_mp) {
219 		next_mp = current_mp->b_cont;
220 		eop = (next_mp == NULL); /* Last fragment of the packet? */
221 		next_len = eop ? 0: MBLK_LEN(next_mp);
222 
223 		/*
224 		 * When the current fragment is an empty fragment, if
225 		 * the next fragment will still be copied to the current
226 		 * tx buffer, we cannot skip this fragment here. Because
227 		 * the copy processing is pending for completion. We have
228 		 * to process this empty fragment in the tx_copy routine.
229 		 *
230 		 * If the copy processing is completed or a DMA binding
231 		 * processing is just completed, we can just skip this
232 		 * empty fragment.
233 		 */
234 		if ((current_len == 0) && (copy_done)) {
235 			current_mp = next_mp;
236 			current_len = next_len;
237 			current_flag = (current_len <= tx_ring->copy_thresh) ?
238 			    USE_COPY : USE_DMA;
239 			continue;
240 		}
241 
242 		if (copy_done) {
243 			/*
244 			 * Get a new tx control block from the free list
245 			 */
246 			tcb = igb_get_free_list(tx_ring);
247 
248 			if (tcb == NULL) {
249 				IGB_DEBUG_STAT(tx_ring->stat_fail_no_tcb);
250 				goto tx_failure;
251 			}
252 
253 			/*
254 			 * Push the tx control block to the pending list
255 			 * to avoid using lock too early
256 			 */
257 			LIST_PUSH_TAIL(&pending_list, &tcb->link);
258 		}
259 
260 		if (current_flag == USE_COPY) {
261 			/*
262 			 * Check whether to use bcopy or DMA binding to process
263 			 * the next fragment, and if using bcopy, whether we
264 			 * need to continue copying the next fragment into the
265 			 * current tx buffer.
266 			 */
267 			ASSERT((tcb->tx_buf.len + current_len) <=
268 			    tcb->tx_buf.size);
269 
270 			if (eop) {
271 				/*
272 				 * This is the last fragment of the packet, so
273 				 * the copy processing will be completed with
274 				 * this fragment.
275 				 */
276 				next_flag = USE_NONE;
277 				copy_done = B_TRUE;
278 			} else if ((tcb->tx_buf.len + current_len + next_len) >
279 			    tcb->tx_buf.size) {
280 				/*
281 				 * If the next fragment is too large to be
282 				 * copied to the current tx buffer, we need
283 				 * to complete the current copy processing.
284 				 */
285 				next_flag = (next_len > tx_ring->copy_thresh) ?
286 				    USE_DMA: USE_COPY;
287 				copy_done = B_TRUE;
288 			} else if (next_len > tx_ring->copy_thresh) {
289 				/*
290 				 * The next fragment needs to be processed with
291 				 * DMA binding. So the copy prcessing will be
292 				 * completed with the current fragment.
293 				 */
294 				next_flag = USE_DMA;
295 				copy_done = B_TRUE;
296 			} else {
297 				/*
298 				 * Continue to copy the next fragment to the
299 				 * current tx buffer.
300 				 */
301 				next_flag = USE_COPY;
302 				copy_done = B_FALSE;
303 			}
304 
305 			desc_num = igb_tx_copy(tx_ring, tcb, current_mp,
306 			    current_len, copy_done, eop);
307 		} else {
308 			/*
309 			 * Check whether to use bcopy or DMA binding to process
310 			 * the next fragment.
311 			 */
312 			next_flag = (next_len > tx_ring->copy_thresh) ?
313 			    USE_DMA: USE_COPY;
314 			ASSERT(copy_done == B_TRUE);
315 
316 			desc_num = igb_tx_bind(tx_ring, tcb, current_mp,
317 			    current_len);
318 		}
319 
320 		if (desc_num > 0)
321 			desc_total += desc_num;
322 		else if (desc_num < 0)
323 			goto tx_failure;
324 
325 		current_mp = next_mp;
326 		current_len = next_len;
327 		current_flag = next_flag;
328 	}
329 
330 	/*
331 	 * Attach the mblk to the last tx control block
332 	 */
333 	ASSERT(tcb);
334 	ASSERT(tcb->mp == NULL);
335 	tcb->mp = mp;
336 
337 	if (igb->tx_hcksum_enable) {
338 		/*
339 		 * Retrieve checksum context information from the mblk that will
340 		 * be used to decide whether/how to fill the context descriptor.
341 		 */
342 		hcksum = &hcksum_context;
343 		igb_get_hcksum_context(mp, hcksum);
344 	} else {
345 		hcksum = NULL;
346 	}
347 
348 	/*
349 	 * Before fill the tx descriptor ring with the data, we need to
350 	 * ensure there are adequate free descriptors for transmit
351 	 * (including one context descriptor).
352 	 */
353 	if (tx_ring->tbd_free < (desc_total + 1)) {
354 		tx_ring->tx_recycle(tx_ring);
355 	}
356 
357 	mutex_enter(&tx_ring->tx_lock);
358 
359 	/*
360 	 * If the number of free tx descriptors is not enough for transmit
361 	 * then return failure.
362 	 *
363 	 * Note: we must put this check under the mutex protection to
364 	 * ensure the correctness when multiple threads access it in
365 	 * parallel.
366 	 */
367 	if (tx_ring->tbd_free < (desc_total + 1)) {
368 		IGB_DEBUG_STAT(tx_ring->stat_fail_no_tbd);
369 		mutex_exit(&tx_ring->tx_lock);
370 		goto tx_failure;
371 	}
372 
373 	desc_num = igb_tx_fill_ring(tx_ring, &pending_list, hcksum);
374 
375 	ASSERT((desc_num == desc_total) || (desc_num == (desc_total + 1)));
376 
377 	mutex_exit(&tx_ring->tx_lock);
378 
379 	return (B_TRUE);
380 
381 tx_failure:
382 	/*
383 	 * Discard the mblk and free the used resources
384 	 */
385 	tcb = (tx_control_block_t *)LIST_GET_HEAD(&pending_list);
386 	while (tcb) {
387 		tcb->mp = NULL;
388 
389 		igb_free_tcb(tcb);
390 
391 		tcb = (tx_control_block_t *)
392 		    LIST_GET_NEXT(&pending_list, &tcb->link);
393 	}
394 
395 	/*
396 	 * Return the tx control blocks in the pending list to the free list.
397 	 */
398 	igb_put_free_list(tx_ring, &pending_list);
399 
400 	/* Transmit failed, do not drop the mblk, rechedule the transmit */
401 	tx_ring->reschedule = B_TRUE;
402 
403 	return (B_FALSE);
404 }
405 
406 /*
407  * igb_tx_copy
408  *
409  * Copy the mblk fragment to the pre-allocated tx buffer
410  */
411 static int
412 igb_tx_copy(igb_tx_ring_t *tx_ring, tx_control_block_t *tcb, mblk_t *mp,
413     uint32_t len, boolean_t copy_done, boolean_t eop)
414 {
415 	dma_buffer_t *tx_buf;
416 	uint32_t desc_num;
417 	_NOTE(ARGUNUSED(tx_ring));
418 
419 	tx_buf = &tcb->tx_buf;
420 
421 	/*
422 	 * Copy the packet data of the mblk fragment into the
423 	 * pre-allocated tx buffer, which is maintained by the
424 	 * tx control block.
425 	 *
426 	 * Several mblk fragments can be copied into one tx buffer.
427 	 * The destination address of the current copied fragment in
428 	 * the tx buffer is next to the end of the previous copied
429 	 * fragment.
430 	 */
431 	if (len > 0) {
432 		bcopy(mp->b_rptr, tx_buf->address + tx_buf->len, len);
433 
434 		tx_buf->len += len;
435 		tcb->frag_num++;
436 	}
437 
438 	desc_num = 0;
439 
440 	/*
441 	 * If it is the last fragment copied to the current tx buffer,
442 	 * in other words, if there's no remaining fragment or the remaining
443 	 * fragment requires a new tx control block to process, we need to
444 	 * complete the current copy processing by syncing up the current
445 	 * DMA buffer and saving the descriptor data.
446 	 */
447 	if (copy_done) {
448 		/*
449 		 * For the packet smaller than 64 bytes, we need to
450 		 * pad it to 60 bytes. The NIC hardware will add 4
451 		 * bytes of CRC.
452 		 */
453 		if (eop && (tx_buf->len < ETHERMIN)) {
454 			bzero(tx_buf->address + tx_buf->len,
455 			    ETHERMIN - tx_buf->len);
456 			tx_buf->len = ETHERMIN;
457 		}
458 
459 		/*
460 		 * Sync the DMA buffer of the packet data
461 		 */
462 		DMA_SYNC(tx_buf, DDI_DMA_SYNC_FORDEV);
463 
464 		tcb->tx_type = USE_COPY;
465 
466 		/*
467 		 * Save the address and length to the private data structure
468 		 * of the tx control block, which will be used to fill the
469 		 * tx descriptor ring after all the fragments are processed.
470 		 */
471 		igb_save_desc(tcb, tx_buf->dma_address, tx_buf->len);
472 		desc_num++;
473 	}
474 
475 	return (desc_num);
476 }
477 
478 /*
479  * igb_tx_bind
480  *
481  * Bind the mblk fragment with DMA
482  */
483 static int
484 igb_tx_bind(igb_tx_ring_t *tx_ring, tx_control_block_t *tcb, mblk_t *mp,
485     uint32_t len)
486 {
487 	int status, i;
488 	ddi_dma_cookie_t dma_cookie;
489 	uint_t ncookies;
490 	int desc_num;
491 
492 	/*
493 	 * Use DMA binding to process the mblk fragment
494 	 */
495 	status = ddi_dma_addr_bind_handle(tcb->tx_dma_handle, NULL,
496 	    (caddr_t)mp->b_rptr, len,
497 	    DDI_DMA_WRITE | DDI_DMA_STREAMING, DDI_DMA_DONTWAIT,
498 	    0, &dma_cookie, &ncookies);
499 
500 	if (status != DDI_DMA_MAPPED) {
501 		IGB_DEBUG_STAT(tx_ring->stat_fail_dma_bind);
502 		return (-1);
503 	}
504 
505 	tcb->frag_num++;
506 	tcb->tx_type = USE_DMA;
507 	/*
508 	 * Each fragment can span several cookies. One cookie will have
509 	 * one tx descriptor to transmit.
510 	 */
511 	desc_num = 0;
512 	for (i = ncookies; i > 0; i--) {
513 		/*
514 		 * Save the address and length to the private data structure
515 		 * of the tx control block, which will be used to fill the
516 		 * tx descriptor ring after all the fragments are processed.
517 		 */
518 		igb_save_desc(tcb,
519 		    dma_cookie.dmac_laddress,
520 		    dma_cookie.dmac_size);
521 
522 		desc_num++;
523 
524 		if (i > 1)
525 			ddi_dma_nextcookie(tcb->tx_dma_handle, &dma_cookie);
526 	}
527 
528 	return (desc_num);
529 }
530 
531 /*
532  * igb_get_hcksum_context
533  *
534  * Get the hcksum context information from the mblk
535  */
536 static void
537 igb_get_hcksum_context(mblk_t *mp, hcksum_context_t *hcksum)
538 {
539 	uint32_t start;
540 	uint32_t flags;
541 	uint32_t len;
542 	uint32_t size;
543 	uint32_t offset;
544 	unsigned char *pos;
545 	ushort_t etype;
546 	uint32_t mac_hdr_len;
547 	uint32_t l4_proto;
548 
549 	ASSERT(mp != NULL);
550 
551 	hcksum_retrieve(mp, NULL, NULL, &start, NULL, NULL, NULL, &flags);
552 
553 	hcksum->hcksum_flags = flags;
554 
555 	if (flags == 0)
556 		return;
557 
558 	etype = 0;
559 	mac_hdr_len = 0;
560 	l4_proto = 0;
561 
562 	/*
563 	 * Firstly get the position of the ether_type/ether_tpid.
564 	 * Here we don't assume the ether (VLAN) header is fully included
565 	 * in one mblk fragment, so we go thourgh the fragments to parse
566 	 * the ether type.
567 	 */
568 	size = len = MBLK_LEN(mp);
569 	offset = offsetof(struct ether_header, ether_type);
570 	while (size <= offset) {
571 		mp = mp->b_cont;
572 		ASSERT(mp != NULL);
573 		len = MBLK_LEN(mp);
574 		size += len;
575 	}
576 	pos = mp->b_rptr + offset + len - size;
577 
578 	etype = ntohs(*(ushort_t *)(uintptr_t)pos);
579 	if (etype == ETHERTYPE_VLAN) {
580 		/*
581 		 * Get the position of the ether_type in VLAN header
582 		 */
583 		offset = offsetof(struct ether_vlan_header, ether_type);
584 		while (size <= offset) {
585 			mp = mp->b_cont;
586 			ASSERT(mp != NULL);
587 			len = MBLK_LEN(mp);
588 			size += len;
589 		}
590 		pos = mp->b_rptr + offset + len - size;
591 
592 		etype = ntohs(*(ushort_t *)(uintptr_t)pos);
593 		mac_hdr_len = sizeof (struct ether_vlan_header);
594 	} else {
595 		mac_hdr_len = sizeof (struct ether_header);
596 	}
597 
598 	/*
599 	 * Here we don't assume the IP(V6) header is fully included in
600 	 * one mblk fragment, so we go thourgh the fragments to parse
601 	 * the protocol type.
602 	 */
603 	switch (etype) {
604 	case ETHERTYPE_IP:
605 		offset = offsetof(ipha_t, ipha_protocol) + mac_hdr_len;
606 		while (size <= offset) {
607 			mp = mp->b_cont;
608 			ASSERT(mp != NULL);
609 			len = MBLK_LEN(mp);
610 			size += len;
611 		}
612 		pos = mp->b_rptr + offset + len - size;
613 
614 		l4_proto = *(uint8_t *)pos;
615 		break;
616 	case ETHERTYPE_IPV6:
617 		offset = offsetof(ip6_t, ip6_nxt) + mac_hdr_len;
618 		while (size <= offset) {
619 			mp = mp->b_cont;
620 			ASSERT(mp != NULL);
621 			len = MBLK_LEN(mp);
622 			size += len;
623 		}
624 		pos = mp->b_rptr + offset + len - size;
625 
626 		l4_proto = *(uint8_t *)pos;
627 		break;
628 	default:
629 		/* Unrecoverable error */
630 		IGB_DEBUGLOG_0(NULL, "Ether type error with tx hcksum");
631 		return;
632 	}
633 
634 	hcksum->mac_hdr_len = mac_hdr_len;
635 	hcksum->ip_hdr_len = start;
636 	hcksum->l4_proto = l4_proto;
637 }
638 
639 /*
640  * igb_check_hcksum_context
641  *
642  * Check if a new context descriptor is needed
643  */
644 static boolean_t
645 igb_check_hcksum_context(igb_tx_ring_t *tx_ring, hcksum_context_t *hcksum)
646 {
647 	hcksum_context_t *last;
648 
649 	if (hcksum == NULL)
650 		return (B_FALSE);
651 
652 	/*
653 	 * Compare the checksum data retrieved from the mblk and the
654 	 * stored checksum data of the last context descriptor. The data
655 	 * need to be checked are:
656 	 *	hcksum_flags
657 	 *	l4_proto
658 	 *	mac_hdr_len
659 	 *	ip_hdr_len
660 	 * Either one of the above data is changed, a new context descriptor
661 	 * will be needed.
662 	 */
663 	last = &tx_ring->hcksum_context;
664 
665 	if (hcksum->hcksum_flags != 0) {
666 		if ((hcksum->hcksum_flags != last->hcksum_flags) ||
667 		    (hcksum->l4_proto != last->l4_proto) ||
668 		    (hcksum->mac_hdr_len != last->mac_hdr_len) ||
669 		    (hcksum->ip_hdr_len != last->ip_hdr_len)) {
670 
671 			return (B_TRUE);
672 		}
673 	}
674 
675 	return (B_FALSE);
676 }
677 
678 /*
679  * igb_fill_hcksum_context
680  *
681  * Fill the context descriptor with hardware checksum informations
682  */
683 static void
684 igb_fill_hcksum_context(struct e1000_adv_tx_context_desc *ctx_tbd,
685     hcksum_context_t *hcksum)
686 {
687 	/*
688 	 * Fill the context descriptor with the checksum
689 	 * context information we've got
690 	 */
691 	ctx_tbd->vlan_macip_lens = hcksum->ip_hdr_len;
692 	ctx_tbd->vlan_macip_lens |= hcksum->mac_hdr_len <<
693 	    E1000_ADVTXD_MACLEN_SHIFT;
694 
695 	ctx_tbd->type_tucmd_mlhl =
696 	    E1000_ADVTXD_DCMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
697 
698 	if (hcksum->hcksum_flags & HCK_IPV4_HDRCKSUM)
699 		ctx_tbd->type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_IPV4;
700 
701 	if (hcksum->hcksum_flags & HCK_PARTIALCKSUM) {
702 		switch (hcksum->l4_proto) {
703 		case IPPROTO_TCP:
704 			ctx_tbd->type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_TCP;
705 			break;
706 		case IPPROTO_UDP:
707 			/*
708 			 * We don't have to explicitly set:
709 			 *	ctx_tbd->type_tucmd_mlhl |=
710 			 *	    E1000_ADVTXD_TUCMD_L4T_UDP;
711 			 * Because E1000_ADVTXD_TUCMD_L4T_UDP == 0b
712 			 */
713 			break;
714 		default:
715 			/* Unrecoverable error */
716 			IGB_DEBUGLOG_0(NULL, "L4 type error with tx hcksum");
717 			break;
718 		}
719 	}
720 
721 	ctx_tbd->seqnum_seed = 0;
722 	ctx_tbd->mss_l4len_idx = 0;
723 }
724 
725 /*
726  * igb_tx_fill_ring
727  *
728  * Fill the tx descriptor ring with the data
729  */
730 static int
731 igb_tx_fill_ring(igb_tx_ring_t *tx_ring, link_list_t *pending_list,
732     hcksum_context_t *hcksum)
733 {
734 	struct e1000_hw *hw = &tx_ring->igb->hw;
735 	boolean_t load_context;
736 	uint32_t index, tcb_index, desc_num;
737 	union e1000_adv_tx_desc *tbd, *first_tbd;
738 	tx_control_block_t *tcb, *first_tcb;
739 	uint32_t hcksum_flags;
740 	int i;
741 	igb_t *igb = tx_ring->igb;
742 
743 	ASSERT(mutex_owned(&tx_ring->tx_lock));
744 
745 	tbd = NULL;
746 	first_tbd = NULL;
747 	first_tcb = NULL;
748 	desc_num = 0;
749 	hcksum_flags = 0;
750 	load_context = B_FALSE;
751 
752 	/*
753 	 * Get the index of the first tx descriptor that will be filled,
754 	 * and the index of the first work list item that will be attached
755 	 * with the first used tx control block in the pending list.
756 	 * Note: the two indexes are the same.
757 	 */
758 	index = tx_ring->tbd_tail;
759 	tcb_index = tx_ring->tbd_tail;
760 
761 	if (hcksum != NULL) {
762 		hcksum_flags = hcksum->hcksum_flags;
763 
764 		/*
765 		 * Check if a new context descriptor is needed for this packet
766 		 */
767 		load_context = igb_check_hcksum_context(tx_ring, hcksum);
768 		if (load_context) {
769 			first_tcb = (tx_control_block_t *)
770 			    LIST_GET_HEAD(pending_list);
771 			tbd = &tx_ring->tbd_ring[index];
772 
773 			/*
774 			 * Fill the context descriptor with the
775 			 * hardware checksum offload informations.
776 			 */
777 			igb_fill_hcksum_context(
778 			    (struct e1000_adv_tx_context_desc *)tbd, hcksum);
779 
780 			index = NEXT_INDEX(index, 1, tx_ring->ring_size);
781 			desc_num++;
782 
783 			/*
784 			 * Store the checksum context data if
785 			 * a new context descriptor is added
786 			 */
787 			tx_ring->hcksum_context = *hcksum;
788 		}
789 	}
790 
791 	first_tbd = &tx_ring->tbd_ring[index];
792 
793 	/*
794 	 * Fill tx data descriptors with the data saved in the pending list.
795 	 * The tx control blocks in the pending list are added to the work list
796 	 * at the same time.
797 	 *
798 	 * The work list is strictly 1:1 corresponding to the descriptor ring.
799 	 * One item of the work list corresponds to one tx descriptor. Because
800 	 * one tx control block can span multiple tx descriptors, the tx
801 	 * control block will be added to the first work list item that
802 	 * corresponds to the first tx descriptor generated from that tx
803 	 * control block.
804 	 */
805 	tcb = (tx_control_block_t *)LIST_POP_HEAD(pending_list);
806 	while (tcb != NULL) {
807 
808 		for (i = 0; i < tcb->desc_num; i++) {
809 			tbd = &tx_ring->tbd_ring[index];
810 
811 			tbd->read.buffer_addr = tcb->desc[i].address;
812 			tbd->read.cmd_type_len = tcb->desc[i].length;
813 
814 			tbd->read.cmd_type_len |= E1000_ADVTXD_DCMD_RS |
815 			    E1000_ADVTXD_DCMD_DEXT | E1000_ADVTXD_DTYP_DATA;
816 
817 			tbd->read.olinfo_status = 0;
818 
819 			index = NEXT_INDEX(index, 1, tx_ring->ring_size);
820 			desc_num++;
821 		}
822 
823 		if (first_tcb != NULL) {
824 			/*
825 			 * Count the checksum context descriptor for
826 			 * the first tx control block.
827 			 */
828 			first_tcb->desc_num++;
829 			first_tcb = NULL;
830 		}
831 
832 		/*
833 		 * Add the tx control block to the work list
834 		 */
835 		ASSERT(tx_ring->work_list[tcb_index] == NULL);
836 		tx_ring->work_list[tcb_index] = tcb;
837 
838 		tcb_index = index;
839 		tcb = (tx_control_block_t *)LIST_POP_HEAD(pending_list);
840 	}
841 
842 	/*
843 	 * The Insert Ethernet CRC (IFCS) bit and the checksum fields are only
844 	 * valid in the first descriptor of the packet.
845 	 */
846 	ASSERT(first_tbd != NULL);
847 	first_tbd->read.cmd_type_len |= E1000_ADVTXD_DCMD_IFCS;
848 
849 	/* Set hardware checksum bits */
850 	if (hcksum_flags != 0) {
851 		if (hcksum_flags & HCK_IPV4_HDRCKSUM)
852 			first_tbd->read.olinfo_status |=
853 			    E1000_TXD_POPTS_IXSM << 8;
854 		if (hcksum_flags & HCK_PARTIALCKSUM)
855 			first_tbd->read.olinfo_status |=
856 			    E1000_TXD_POPTS_TXSM << 8;
857 	}
858 
859 	/*
860 	 * The last descriptor of packet needs End Of Packet (EOP),
861 	 * and Report Status (RS) bits set
862 	 */
863 	ASSERT(tbd != NULL);
864 	tbd->read.cmd_type_len |=
865 	    E1000_ADVTXD_DCMD_EOP | E1000_ADVTXD_DCMD_RS;
866 
867 	/*
868 	 * Sync the DMA buffer of the tx descriptor ring
869 	 */
870 	DMA_SYNC(&tx_ring->tbd_area, DDI_DMA_SYNC_FORDEV);
871 
872 	/*
873 	 * Update the number of the free tx descriptors.
874 	 * The mutual exclusion between the transmission and the recycling
875 	 * (for the tx descriptor ring and the work list) is implemented
876 	 * with the atomic operation on the number of the free tx descriptors.
877 	 *
878 	 * Note: we should always decrement the counter tbd_free before
879 	 * advancing the hardware TDT pointer to avoid the race condition -
880 	 * before the counter tbd_free is decremented, the transmit of the
881 	 * tx descriptors has done and the counter tbd_free is increased by
882 	 * the tx recycling.
883 	 */
884 	i = igb_atomic_reserve(&tx_ring->tbd_free, desc_num);
885 	ASSERT(i >= 0);
886 
887 	tx_ring->tbd_tail = index;
888 
889 	/*
890 	 * Advance the hardware TDT pointer of the tx descriptor ring
891 	 */
892 	E1000_WRITE_REG(hw, E1000_TDT(tx_ring->index), index);
893 
894 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
895 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
896 	}
897 
898 	return (desc_num);
899 }
900 
901 /*
902  * igb_save_desc
903  *
904  * Save the address/length pair to the private array
905  * of the tx control block. The address/length pairs
906  * will be filled into the tx descriptor ring later.
907  */
908 static void
909 igb_save_desc(tx_control_block_t *tcb, uint64_t address, size_t length)
910 {
911 	sw_desc_t *desc;
912 
913 	desc = &tcb->desc[tcb->desc_num];
914 	desc->address = address;
915 	desc->length = length;
916 
917 	tcb->desc_num++;
918 }
919 
920 /*
921  * igb_tx_recycle_legacy
922  *
923  * Recycle the tx descriptors and tx control blocks.
924  *
925  * The work list is traversed to check if the corresponding
926  * tx descriptors have been transmitted. If so, the resources
927  * bound to the tx control blocks will be freed, and those
928  * tx control blocks will be returned to the free list.
929  */
930 uint32_t
931 igb_tx_recycle_legacy(igb_tx_ring_t *tx_ring)
932 {
933 	uint32_t index, last_index;
934 	int desc_num;
935 	boolean_t desc_done;
936 	tx_control_block_t *tcb;
937 	link_list_t pending_list;
938 	igb_t *igb = tx_ring->igb;
939 
940 	/*
941 	 * The mutex_tryenter() is used to avoid unnecessary
942 	 * lock contention.
943 	 */
944 	if (mutex_tryenter(&tx_ring->recycle_lock) == 0)
945 		return (0);
946 
947 	ASSERT(tx_ring->tbd_free <= tx_ring->ring_size);
948 
949 	if (tx_ring->tbd_free == tx_ring->ring_size) {
950 		tx_ring->recycle_fail = 0;
951 		tx_ring->stall_watchdog = 0;
952 		mutex_exit(&tx_ring->recycle_lock);
953 		return (0);
954 	}
955 
956 	/*
957 	 * Sync the DMA buffer of the tx descriptor ring
958 	 */
959 	DMA_SYNC(&tx_ring->tbd_area, DDI_DMA_SYNC_FORKERNEL);
960 
961 	if (igb_check_dma_handle(
962 	    tx_ring->tbd_area.dma_handle) != DDI_FM_OK) {
963 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
964 	}
965 
966 	LINK_LIST_INIT(&pending_list);
967 	desc_num = 0;
968 	index = tx_ring->tbd_head;	/* Index of next tbd/tcb to recycle */
969 
970 	tcb = tx_ring->work_list[index];
971 	ASSERT(tcb != NULL);
972 
973 	desc_done = B_TRUE;
974 	while (desc_done && (tcb != NULL)) {
975 
976 		/*
977 		 * Get the last tx descriptor of the tx control block.
978 		 * If the last tx descriptor is done, it is done with
979 		 * all the tx descriptors of the tx control block.
980 		 * Then the tx control block and all the corresponding
981 		 * tx descriptors can be recycled.
982 		 */
983 		last_index = NEXT_INDEX(index, tcb->desc_num - 1,
984 		    tx_ring->ring_size);
985 
986 		/*
987 		 * Check if the Descriptor Done bit is set
988 		 */
989 		desc_done = tx_ring->tbd_ring[last_index].wb.status &
990 		    E1000_TXD_STAT_DD;
991 		if (desc_done) {
992 			/*
993 			 * Strip off the tx control block from the work list,
994 			 * and add it to the pending list.
995 			 */
996 			tx_ring->work_list[index] = NULL;
997 			LIST_PUSH_TAIL(&pending_list, &tcb->link);
998 
999 			/*
1000 			 * Count the total number of the tx descriptors recycled
1001 			 */
1002 			desc_num += tcb->desc_num;
1003 
1004 			/*
1005 			 * Advance the index of the tx descriptor ring
1006 			 */
1007 			index = NEXT_INDEX(last_index, 1, tx_ring->ring_size);
1008 
1009 			tcb = tx_ring->work_list[index];
1010 		}
1011 	}
1012 
1013 	/*
1014 	 * If no tx descriptors are recycled, no need to do more processing
1015 	 */
1016 	if (desc_num == 0) {
1017 		tx_ring->recycle_fail++;
1018 		mutex_exit(&tx_ring->recycle_lock);
1019 		return (0);
1020 	}
1021 
1022 	tx_ring->recycle_fail = 0;
1023 	tx_ring->stall_watchdog = 0;
1024 
1025 	/*
1026 	 * Update the head index of the tx descriptor ring
1027 	 */
1028 	tx_ring->tbd_head = index;
1029 
1030 	/*
1031 	 * Update the number of the free tx descriptors with atomic operations
1032 	 */
1033 	atomic_add_32(&tx_ring->tbd_free, desc_num);
1034 
1035 	mutex_exit(&tx_ring->recycle_lock);
1036 
1037 	/*
1038 	 * Free the resources used by the tx control blocks
1039 	 * in the pending list
1040 	 */
1041 	tcb = (tx_control_block_t *)LIST_GET_HEAD(&pending_list);
1042 	while (tcb != NULL) {
1043 		/*
1044 		 * Release the resources occupied by the tx control block
1045 		 */
1046 		igb_free_tcb(tcb);
1047 
1048 		tcb = (tx_control_block_t *)
1049 		    LIST_GET_NEXT(&pending_list, &tcb->link);
1050 	}
1051 
1052 	/*
1053 	 * Add the tx control blocks in the pending list to the free list.
1054 	 */
1055 	igb_put_free_list(tx_ring, &pending_list);
1056 
1057 	return (desc_num);
1058 }
1059 
1060 /*
1061  * igb_tx_recycle_head_wb
1062  *
1063  * Check the head write-back, and recycle all the transmitted
1064  * tx descriptors and tx control blocks.
1065  */
1066 uint32_t
1067 igb_tx_recycle_head_wb(igb_tx_ring_t *tx_ring)
1068 {
1069 	uint32_t index;
1070 	uint32_t head_wb;
1071 	int desc_num;
1072 	tx_control_block_t *tcb;
1073 	link_list_t pending_list;
1074 	igb_t *igb = tx_ring->igb;
1075 
1076 	/*
1077 	 * The mutex_tryenter() is used to avoid unnecessary
1078 	 * lock contention.
1079 	 */
1080 	if (mutex_tryenter(&tx_ring->recycle_lock) == 0)
1081 		return (0);
1082 
1083 	ASSERT(tx_ring->tbd_free <= tx_ring->ring_size);
1084 
1085 	if (tx_ring->tbd_free == tx_ring->ring_size) {
1086 		tx_ring->recycle_fail = 0;
1087 		tx_ring->stall_watchdog = 0;
1088 		mutex_exit(&tx_ring->recycle_lock);
1089 		return (0);
1090 	}
1091 
1092 	/*
1093 	 * Sync the DMA buffer of the tx descriptor ring
1094 	 *
1095 	 * Note: For head write-back mode, the tx descriptors will not
1096 	 * be written back, but the head write-back value is stored at
1097 	 * the last extra tbd at the end of the DMA area, we still need
1098 	 * to sync the head write-back value for kernel.
1099 	 *
1100 	 * DMA_SYNC(&tx_ring->tbd_area, DDI_DMA_SYNC_FORKERNEL);
1101 	 */
1102 	(void) ddi_dma_sync(tx_ring->tbd_area.dma_handle,
1103 	    sizeof (union e1000_adv_tx_desc) * tx_ring->ring_size,
1104 	    sizeof (uint32_t),
1105 	    DDI_DMA_SYNC_FORKERNEL);
1106 
1107 	if (igb_check_dma_handle(
1108 	    tx_ring->tbd_area.dma_handle) != DDI_FM_OK) {
1109 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
1110 	}
1111 
1112 	LINK_LIST_INIT(&pending_list);
1113 	desc_num = 0;
1114 	index = tx_ring->tbd_head;	/* Next index to clean */
1115 
1116 	/*
1117 	 * Get the value of head write-back
1118 	 */
1119 	head_wb = *tx_ring->tbd_head_wb;
1120 	while (index != head_wb) {
1121 		tcb = tx_ring->work_list[index];
1122 		ASSERT(tcb != NULL);
1123 
1124 		if (OFFSET(index, head_wb, tx_ring->ring_size) <
1125 		    tcb->desc_num) {
1126 			/*
1127 			 * The current tx control block is not
1128 			 * completely transmitted, stop recycling
1129 			 */
1130 			break;
1131 		}
1132 
1133 		/*
1134 		 * Strip off the tx control block from the work list,
1135 		 * and add it to the pending list.
1136 		 */
1137 		tx_ring->work_list[index] = NULL;
1138 		LIST_PUSH_TAIL(&pending_list, &tcb->link);
1139 
1140 		/*
1141 		 * Advance the index of the tx descriptor ring
1142 		 */
1143 		index = NEXT_INDEX(index, tcb->desc_num, tx_ring->ring_size);
1144 
1145 		/*
1146 		 * Count the total number of the tx descriptors recycled
1147 		 */
1148 		desc_num += tcb->desc_num;
1149 	}
1150 
1151 	/*
1152 	 * If no tx descriptors are recycled, no need to do more processing
1153 	 */
1154 	if (desc_num == 0) {
1155 		tx_ring->recycle_fail++;
1156 		mutex_exit(&tx_ring->recycle_lock);
1157 		return (0);
1158 	}
1159 
1160 	tx_ring->recycle_fail = 0;
1161 	tx_ring->stall_watchdog = 0;
1162 
1163 	/*
1164 	 * Update the head index of the tx descriptor ring
1165 	 */
1166 	tx_ring->tbd_head = index;
1167 
1168 	/*
1169 	 * Update the number of the free tx descriptors with atomic operations
1170 	 */
1171 	atomic_add_32(&tx_ring->tbd_free, desc_num);
1172 
1173 	mutex_exit(&tx_ring->recycle_lock);
1174 
1175 	/*
1176 	 * Free the resources used by the tx control blocks
1177 	 * in the pending list
1178 	 */
1179 	tcb = (tx_control_block_t *)LIST_GET_HEAD(&pending_list);
1180 	while (tcb) {
1181 		/*
1182 		 * Release the resources occupied by the tx control block
1183 		 */
1184 		igb_free_tcb(tcb);
1185 
1186 		tcb = (tx_control_block_t *)
1187 		    LIST_GET_NEXT(&pending_list, &tcb->link);
1188 	}
1189 
1190 	/*
1191 	 * Add the tx control blocks in the pending list to the free list.
1192 	 */
1193 	igb_put_free_list(tx_ring, &pending_list);
1194 
1195 	return (desc_num);
1196 }
1197 
1198 /*
1199  * igb_free_tcb - free up the tx control block
1200  *
1201  * Free the resources of the tx control block, including
1202  * unbind the previously bound DMA handle, and reset other
1203  * control fields.
1204  */
1205 void
1206 igb_free_tcb(tx_control_block_t *tcb)
1207 {
1208 	switch (tcb->tx_type) {
1209 	case USE_COPY:
1210 		/*
1211 		 * Reset the buffer length that is used for copy
1212 		 */
1213 		tcb->tx_buf.len = 0;
1214 		break;
1215 	case USE_DMA:
1216 		/*
1217 		 * Release the DMA resource that is used for
1218 		 * DMA binding.
1219 		 */
1220 		(void) ddi_dma_unbind_handle(tcb->tx_dma_handle);
1221 		break;
1222 	default:
1223 		break;
1224 	}
1225 
1226 	/*
1227 	 * Free the mblk
1228 	 */
1229 	if (tcb->mp != NULL) {
1230 		freemsg(tcb->mp);
1231 		tcb->mp = NULL;
1232 	}
1233 
1234 	tcb->tx_type = USE_NONE;
1235 	tcb->frag_num = 0;
1236 	tcb->desc_num = 0;
1237 }
1238 
1239 /*
1240  * igb_get_free_list - Get a free tx control block from the free list
1241  *
1242  * The atomic operation on the number of the available tx control block
1243  * in the free list is used to keep this routine mutual exclusive with
1244  * the routine igb_put_check_list.
1245  */
1246 static tx_control_block_t *
1247 igb_get_free_list(igb_tx_ring_t *tx_ring)
1248 {
1249 	tx_control_block_t *tcb;
1250 
1251 	/*
1252 	 * Check and update the number of the free tx control block
1253 	 * in the free list.
1254 	 */
1255 	if (igb_atomic_reserve(&tx_ring->tcb_free, 1) < 0)
1256 		return (NULL);
1257 
1258 	mutex_enter(&tx_ring->tcb_head_lock);
1259 
1260 	tcb = tx_ring->free_list[tx_ring->tcb_head];
1261 	ASSERT(tcb != NULL);
1262 	tx_ring->free_list[tx_ring->tcb_head] = NULL;
1263 	tx_ring->tcb_head = NEXT_INDEX(tx_ring->tcb_head, 1,
1264 	    tx_ring->free_list_size);
1265 
1266 	mutex_exit(&tx_ring->tcb_head_lock);
1267 
1268 	return (tcb);
1269 }
1270 
1271 /*
1272  * igb_put_free_list
1273  *
1274  * Put a list of used tx control blocks back to the free list
1275  *
1276  * A mutex is used here to ensure the serialization. The mutual exclusion
1277  * between igb_get_free_list and igb_put_free_list is implemented with
1278  * the atomic operation on the counter tcb_free.
1279  */
1280 void
1281 igb_put_free_list(igb_tx_ring_t *tx_ring, link_list_t *pending_list)
1282 {
1283 	uint32_t index;
1284 	int tcb_num;
1285 	tx_control_block_t *tcb;
1286 
1287 	mutex_enter(&tx_ring->tcb_tail_lock);
1288 
1289 	index = tx_ring->tcb_tail;
1290 
1291 	tcb_num = 0;
1292 	tcb = (tx_control_block_t *)LIST_POP_HEAD(pending_list);
1293 	while (tcb != NULL) {
1294 		ASSERT(tx_ring->free_list[index] == NULL);
1295 		tx_ring->free_list[index] = tcb;
1296 
1297 		tcb_num++;
1298 
1299 		index = NEXT_INDEX(index, 1, tx_ring->free_list_size);
1300 
1301 		tcb = (tx_control_block_t *)LIST_POP_HEAD(pending_list);
1302 	}
1303 
1304 	tx_ring->tcb_tail = index;
1305 
1306 	/*
1307 	 * Update the number of the free tx control block
1308 	 * in the free list. This operation must be placed
1309 	 * under the protection of the lock.
1310 	 */
1311 	atomic_add_32(&tx_ring->tcb_free, tcb_num);
1312 
1313 	mutex_exit(&tx_ring->tcb_tail_lock);
1314 }
1315