15779Sxy150489 /*
25779Sxy150489 * CDDL HEADER START
35779Sxy150489 *
45779Sxy150489 * The contents of this file are subject to the terms of the
55779Sxy150489 * Common Development and Distribution License (the "License").
65779Sxy150489 * You may not use this file except in compliance with the License.
75779Sxy150489 *
811878SVenu.Iyer@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
911878SVenu.Iyer@Sun.COM * or http://www.opensolaris.org/os/licensing.
105779Sxy150489 * See the License for the specific language governing permissions
115779Sxy150489 * and limitations under the License.
125779Sxy150489 *
1311878SVenu.Iyer@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
1411878SVenu.Iyer@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155779Sxy150489 * If applicable, add the following below this CDDL HEADER, with the
165779Sxy150489 * fields enclosed by brackets "[]" replaced with your own identifying
175779Sxy150489 * information: Portions Copyright [yyyy] [name of copyright owner]
185779Sxy150489 *
195779Sxy150489 * CDDL HEADER END
205779Sxy150489 */
215779Sxy150489
225779Sxy150489 /*
23*12980SGuoqing.Zhu@Sun.COM * Copyright(c) 2007-2010 Intel Corporation. All rights reserved.
24*12980SGuoqing.Zhu@Sun.COM */
25*12980SGuoqing.Zhu@Sun.COM
26*12980SGuoqing.Zhu@Sun.COM /*
27*12980SGuoqing.Zhu@Sun.COM * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
288275SEric Cheng */
295779Sxy150489
305779Sxy150489 #include "igb_sw.h"
315779Sxy150489
325779Sxy150489 static boolean_t igb_tx(igb_tx_ring_t *, mblk_t *);
335779Sxy150489 static int igb_tx_copy(igb_tx_ring_t *, tx_control_block_t *, mblk_t *,
347072Sxy150489 uint32_t, boolean_t);
355779Sxy150489 static int igb_tx_bind(igb_tx_ring_t *, tx_control_block_t *, mblk_t *,
365779Sxy150489 uint32_t);
379188SPaul.Guo@Sun.COM static int igb_tx_fill_ring(igb_tx_ring_t *, link_list_t *, tx_context_t *,
389188SPaul.Guo@Sun.COM size_t);
395779Sxy150489 static void igb_save_desc(tx_control_block_t *, uint64_t, size_t);
405779Sxy150489 static tx_control_block_t *igb_get_free_list(igb_tx_ring_t *);
419188SPaul.Guo@Sun.COM static int igb_get_tx_context(mblk_t *, tx_context_t *);
429188SPaul.Guo@Sun.COM static boolean_t igb_check_tx_context(igb_tx_ring_t *, tx_context_t *);
439188SPaul.Guo@Sun.COM static void igb_fill_tx_context(struct e1000_adv_tx_context_desc *,
449188SPaul.Guo@Sun.COM tx_context_t *, uint32_t);
455779Sxy150489
465779Sxy150489 #ifndef IGB_DEBUG
475779Sxy150489 #pragma inline(igb_save_desc)
489188SPaul.Guo@Sun.COM #pragma inline(igb_get_tx_context)
499188SPaul.Guo@Sun.COM #pragma inline(igb_check_tx_context)
509188SPaul.Guo@Sun.COM #pragma inline(igb_fill_tx_context)
515779Sxy150489 #endif
525779Sxy150489
535779Sxy150489 mblk_t *
igb_tx_ring_send(void * arg,mblk_t * mp)548275SEric Cheng igb_tx_ring_send(void *arg, mblk_t *mp)
555779Sxy150489 {
568275SEric Cheng igb_tx_ring_t *tx_ring = (igb_tx_ring_t *)arg;
575779Sxy150489
588275SEric Cheng ASSERT(tx_ring != NULL);
595779Sxy150489
6011367SJason.Xu@Sun.COM if ((tx_ring->igb->igb_state & IGB_SUSPENDED) ||
6111367SJason.Xu@Sun.COM (tx_ring->igb->igb_state & IGB_ERROR) ||
6211367SJason.Xu@Sun.COM !(tx_ring->igb->igb_state & IGB_STARTED)) {
6311367SJason.Xu@Sun.COM freemsg(mp);
6411367SJason.Xu@Sun.COM return (NULL);
6511367SJason.Xu@Sun.COM }
6611367SJason.Xu@Sun.COM
678275SEric Cheng return ((igb_tx(tx_ring, mp)) ? NULL : mp);
685779Sxy150489 }
695779Sxy150489
705779Sxy150489 /*
715779Sxy150489 * igb_tx - Main transmit processing
725779Sxy150489 *
735779Sxy150489 * Called from igb_m_tx with an mblk ready to transmit. this
745779Sxy150489 * routine sets up the transmit descriptors and sends data to
755779Sxy150489 * the wire.
765779Sxy150489 *
775779Sxy150489 * One mblk can consist of several fragments, each fragment
785779Sxy150489 * will be processed with different methods based on the size.
795779Sxy150489 * For the fragments with size less than the bcopy threshold,
805779Sxy150489 * they will be processed by using bcopy; otherwise, they will
815779Sxy150489 * be processed by using DMA binding.
825779Sxy150489 *
835779Sxy150489 * To process the mblk, a tx control block is got from the
845779Sxy150489 * free list. One tx control block contains one tx buffer, which
855779Sxy150489 * is used to copy mblk fragments' data; and one tx DMA handle,
865779Sxy150489 * which is used to bind a mblk fragment with DMA resource.
875779Sxy150489 *
885779Sxy150489 * Several small mblk fragments can be copied into one tx control
895779Sxy150489 * block's buffer, and then the buffer will be transmitted with
905779Sxy150489 * one tx descriptor.
915779Sxy150489 *
925779Sxy150489 * A large fragment only binds with one tx control block's DMA
935779Sxy150489 * handle, and it can span several tx descriptors for transmitting.
945779Sxy150489 *
955779Sxy150489 * So to transmit a packet (mblk), several tx control blocks can
965779Sxy150489 * be used. After the processing, those tx control blocks will
975779Sxy150489 * be put to the work list.
985779Sxy150489 */
995779Sxy150489 static boolean_t
igb_tx(igb_tx_ring_t * tx_ring,mblk_t * mp)1005779Sxy150489 igb_tx(igb_tx_ring_t *tx_ring, mblk_t *mp)
1015779Sxy150489 {
1025779Sxy150489 igb_t *igb = tx_ring->igb;
1035779Sxy150489 tx_type_t current_flag, next_flag;
1045779Sxy150489 uint32_t current_len, next_len;
1055779Sxy150489 uint32_t desc_total;
1065779Sxy150489 size_t mbsize;
1075779Sxy150489 int desc_num;
1085779Sxy150489 boolean_t copy_done, eop;
1095779Sxy150489 mblk_t *current_mp, *next_mp, *nmp;
1105779Sxy150489 tx_control_block_t *tcb;
1119188SPaul.Guo@Sun.COM tx_context_t tx_context, *ctx;
1125779Sxy150489 link_list_t pending_list;
11311502SChenlu.Chen@Sun.COM mblk_t *hdr_new_mp = NULL;
11411502SChenlu.Chen@Sun.COM mblk_t *hdr_previous_mp = NULL;
11511502SChenlu.Chen@Sun.COM mblk_t *hdr_current_mp = NULL;
1169188SPaul.Guo@Sun.COM uint32_t hdr_frag_len;
1179188SPaul.Guo@Sun.COM uint32_t hdr_len, len;
1189188SPaul.Guo@Sun.COM uint32_t copy_thresh;
1199188SPaul.Guo@Sun.COM
12011502SChenlu.Chen@Sun.COM copy_thresh = igb->tx_copy_thresh;
1215779Sxy150489
1225779Sxy150489 /* Get the mblk size */
1235779Sxy150489 mbsize = 0;
1245779Sxy150489 for (nmp = mp; nmp != NULL; nmp = nmp->b_cont) {
1259188SPaul.Guo@Sun.COM mbsize += MBLKL(nmp);
1265779Sxy150489 }
1275779Sxy150489
1289188SPaul.Guo@Sun.COM if (igb->tx_hcksum_enable) {
1299188SPaul.Guo@Sun.COM ctx = &tx_context;
1309188SPaul.Guo@Sun.COM /*
1319188SPaul.Guo@Sun.COM * Retrieve offloading context information from the mblk
1329188SPaul.Guo@Sun.COM * that will be used to decide whether/how to fill the
1339188SPaul.Guo@Sun.COM * context descriptor.
1349188SPaul.Guo@Sun.COM */
1359188SPaul.Guo@Sun.COM if (igb_get_tx_context(mp, ctx) != TX_CXT_SUCCESS) {
1369188SPaul.Guo@Sun.COM freemsg(mp);
1379188SPaul.Guo@Sun.COM return (B_TRUE);
1389188SPaul.Guo@Sun.COM }
1399188SPaul.Guo@Sun.COM
1409188SPaul.Guo@Sun.COM if ((ctx->lso_flag &&
1419188SPaul.Guo@Sun.COM (mbsize > (ctx->mac_hdr_len + IGB_LSO_MAXLEN))) ||
1429188SPaul.Guo@Sun.COM (!ctx->lso_flag &&
1439188SPaul.Guo@Sun.COM (mbsize > (igb->max_frame_size - ETHERFCSL)))) {
1449188SPaul.Guo@Sun.COM freemsg(mp);
1459188SPaul.Guo@Sun.COM IGB_DEBUGLOG_0(igb, "igb_tx: packet oversize");
1469188SPaul.Guo@Sun.COM return (B_TRUE);
1479188SPaul.Guo@Sun.COM }
1489188SPaul.Guo@Sun.COM } else {
1499188SPaul.Guo@Sun.COM ctx = NULL;
1509188SPaul.Guo@Sun.COM if (mbsize > (igb->max_frame_size - ETHERFCSL)) {
1519188SPaul.Guo@Sun.COM freemsg(mp);
1529188SPaul.Guo@Sun.COM IGB_DEBUGLOG_0(igb, "igb_tx: packet oversize");
1539188SPaul.Guo@Sun.COM return (B_TRUE);
1549188SPaul.Guo@Sun.COM }
1555779Sxy150489 }
1565779Sxy150489
1575779Sxy150489 /*
1585779Sxy150489 * Check and recycle tx descriptors.
1595779Sxy150489 * The recycle threshold here should be selected carefully
1605779Sxy150489 */
16111502SChenlu.Chen@Sun.COM if (tx_ring->tbd_free < igb->tx_recycle_thresh)
1625779Sxy150489 tx_ring->tx_recycle(tx_ring);
1635779Sxy150489
1645779Sxy150489 /*
1655779Sxy150489 * After the recycling, if the tbd_free is less than the
16611502SChenlu.Chen@Sun.COM * tx_overload_threshold, assert overload, return B_FALSE;
1675779Sxy150489 * and we need to re-schedule the tx again.
1685779Sxy150489 */
16911502SChenlu.Chen@Sun.COM if (tx_ring->tbd_free < igb->tx_overload_thresh) {
1705779Sxy150489 tx_ring->reschedule = B_TRUE;
1715779Sxy150489 IGB_DEBUG_STAT(tx_ring->stat_overload);
1725779Sxy150489 return (B_FALSE);
1735779Sxy150489 }
1745779Sxy150489
1755779Sxy150489 /*
1769188SPaul.Guo@Sun.COM * The software should guarantee LSO packet header(MAC+IP+TCP)
1779188SPaul.Guo@Sun.COM * to be within one descriptor - this is required by h/w.
1789188SPaul.Guo@Sun.COM * Here will reallocate and refill the header if
1799188SPaul.Guo@Sun.COM * the headers(MAC+IP+TCP) is physical memory non-contiguous.
1809188SPaul.Guo@Sun.COM */
1819188SPaul.Guo@Sun.COM if (ctx && ctx->lso_flag) {
18211502SChenlu.Chen@Sun.COM hdr_len = ctx->mac_hdr_len + ctx->ip_hdr_len + ctx->l4_hdr_len;
1839188SPaul.Guo@Sun.COM len = MBLKL(mp);
18411502SChenlu.Chen@Sun.COM hdr_current_mp = mp;
1859188SPaul.Guo@Sun.COM while (len < hdr_len) {
18611502SChenlu.Chen@Sun.COM hdr_previous_mp = hdr_current_mp;
18711502SChenlu.Chen@Sun.COM hdr_current_mp = hdr_current_mp->b_cont;
18811502SChenlu.Chen@Sun.COM len += MBLKL(hdr_current_mp);
1899188SPaul.Guo@Sun.COM }
1909188SPaul.Guo@Sun.COM /*
19111502SChenlu.Chen@Sun.COM * If the header and the payload are in different mblks,
19211502SChenlu.Chen@Sun.COM * we simply force the header to be copied into pre-allocated
19311502SChenlu.Chen@Sun.COM * page-aligned buffer.
1949188SPaul.Guo@Sun.COM */
19511502SChenlu.Chen@Sun.COM if (len == hdr_len)
19611502SChenlu.Chen@Sun.COM goto adjust_threshold;
1979188SPaul.Guo@Sun.COM
19811502SChenlu.Chen@Sun.COM hdr_frag_len = hdr_len - (len - MBLKL(hdr_current_mp));
19911502SChenlu.Chen@Sun.COM /*
20011502SChenlu.Chen@Sun.COM * There are two cases we will reallocate
20111502SChenlu.Chen@Sun.COM * a mblk for the last header fragment.
20211502SChenlu.Chen@Sun.COM * 1. the header is in multiple mblks and
20311502SChenlu.Chen@Sun.COM * the last fragment shares the same mblk
20411502SChenlu.Chen@Sun.COM * with the payload
20511502SChenlu.Chen@Sun.COM * 2. the header is in a single mblk shared
20611502SChenlu.Chen@Sun.COM * with the payload but the header crosses
20711502SChenlu.Chen@Sun.COM * a page.
20811502SChenlu.Chen@Sun.COM */
20911502SChenlu.Chen@Sun.COM if ((hdr_current_mp != mp) ||
21011502SChenlu.Chen@Sun.COM (P2NPHASE((uintptr_t)hdr_current_mp->b_rptr, igb->page_size)
21111502SChenlu.Chen@Sun.COM < hdr_len)) {
21211502SChenlu.Chen@Sun.COM /*
21311502SChenlu.Chen@Sun.COM * reallocate the mblk for the last header fragment,
21411502SChenlu.Chen@Sun.COM * expect it to be copied into pre-allocated
21511502SChenlu.Chen@Sun.COM * page-aligned buffer
21611502SChenlu.Chen@Sun.COM */
21711502SChenlu.Chen@Sun.COM hdr_new_mp = allocb(hdr_frag_len, NULL);
21811502SChenlu.Chen@Sun.COM if (!hdr_new_mp) {
21911502SChenlu.Chen@Sun.COM return (B_FALSE);
2209188SPaul.Guo@Sun.COM }
2219188SPaul.Guo@Sun.COM
22211502SChenlu.Chen@Sun.COM /* link the new header fragment with the other parts */
22311502SChenlu.Chen@Sun.COM bcopy(hdr_current_mp->b_rptr,
22411502SChenlu.Chen@Sun.COM hdr_new_mp->b_rptr, hdr_frag_len);
22511502SChenlu.Chen@Sun.COM hdr_new_mp->b_wptr = hdr_new_mp->b_rptr + hdr_frag_len;
22611502SChenlu.Chen@Sun.COM hdr_new_mp->b_cont = hdr_current_mp;
22711502SChenlu.Chen@Sun.COM if (hdr_previous_mp)
22811502SChenlu.Chen@Sun.COM hdr_previous_mp->b_cont = hdr_new_mp;
22911502SChenlu.Chen@Sun.COM else
23011502SChenlu.Chen@Sun.COM mp = hdr_new_mp;
23111502SChenlu.Chen@Sun.COM hdr_current_mp->b_rptr += hdr_frag_len;
2329188SPaul.Guo@Sun.COM }
23311502SChenlu.Chen@Sun.COM adjust_threshold:
23411502SChenlu.Chen@Sun.COM /*
23511502SChenlu.Chen@Sun.COM * adjust the bcopy threshhold to guarantee
23611502SChenlu.Chen@Sun.COM * the header to use bcopy way
23711502SChenlu.Chen@Sun.COM */
23811502SChenlu.Chen@Sun.COM if (copy_thresh < hdr_len)
23911502SChenlu.Chen@Sun.COM copy_thresh = hdr_len;
2409188SPaul.Guo@Sun.COM }
2419188SPaul.Guo@Sun.COM
2429188SPaul.Guo@Sun.COM /*
2435779Sxy150489 * The pending_list is a linked list that is used to save
2445779Sxy150489 * the tx control blocks that have packet data processed
2455779Sxy150489 * but have not put the data to the tx descriptor ring.
2465779Sxy150489 * It is used to reduce the lock contention of the tx_lock.
2475779Sxy150489 */
2485779Sxy150489 LINK_LIST_INIT(&pending_list);
2495779Sxy150489 desc_num = 0;
2505779Sxy150489 desc_total = 0;
2515779Sxy150489
2525779Sxy150489 current_mp = mp;
2539188SPaul.Guo@Sun.COM current_len = MBLKL(current_mp);
2545779Sxy150489 /*
2555779Sxy150489 * Decide which method to use for the first fragment
2565779Sxy150489 */
2579188SPaul.Guo@Sun.COM current_flag = (current_len <= copy_thresh) ?
2585779Sxy150489 USE_COPY : USE_DMA;
2595779Sxy150489 /*
2605779Sxy150489 * If the mblk includes several contiguous small fragments,
2615779Sxy150489 * they may be copied into one buffer. This flag is used to
2625779Sxy150489 * indicate whether there are pending fragments that need to
2635779Sxy150489 * be copied to the current tx buffer.
2645779Sxy150489 *
2655779Sxy150489 * If this flag is B_TRUE, it indicates that a new tx control
2665779Sxy150489 * block is needed to process the next fragment using either
2675779Sxy150489 * copy or DMA binding.
2685779Sxy150489 *
2695779Sxy150489 * Otherwise, it indicates that the next fragment will be
2705779Sxy150489 * copied to the current tx buffer that is maintained by the
2715779Sxy150489 * current tx control block. No new tx control block is needed.
2725779Sxy150489 */
2735779Sxy150489 copy_done = B_TRUE;
2745779Sxy150489 while (current_mp) {
2755779Sxy150489 next_mp = current_mp->b_cont;
2765779Sxy150489 eop = (next_mp == NULL); /* Last fragment of the packet? */
2779188SPaul.Guo@Sun.COM next_len = eop ? 0: MBLKL(next_mp);
2785779Sxy150489
2795779Sxy150489 /*
2805779Sxy150489 * When the current fragment is an empty fragment, if
2815779Sxy150489 * the next fragment will still be copied to the current
2825779Sxy150489 * tx buffer, we cannot skip this fragment here. Because
2835779Sxy150489 * the copy processing is pending for completion. We have
2845779Sxy150489 * to process this empty fragment in the tx_copy routine.
2855779Sxy150489 *
2865779Sxy150489 * If the copy processing is completed or a DMA binding
2875779Sxy150489 * processing is just completed, we can just skip this
2885779Sxy150489 * empty fragment.
2895779Sxy150489 */
2905779Sxy150489 if ((current_len == 0) && (copy_done)) {
2915779Sxy150489 current_mp = next_mp;
2925779Sxy150489 current_len = next_len;
2939188SPaul.Guo@Sun.COM current_flag = (current_len <= copy_thresh) ?
2945779Sxy150489 USE_COPY : USE_DMA;
2955779Sxy150489 continue;
2965779Sxy150489 }
2975779Sxy150489
2985779Sxy150489 if (copy_done) {
2995779Sxy150489 /*
3005779Sxy150489 * Get a new tx control block from the free list
3015779Sxy150489 */
3025779Sxy150489 tcb = igb_get_free_list(tx_ring);
3035779Sxy150489
3045779Sxy150489 if (tcb == NULL) {
3055779Sxy150489 IGB_DEBUG_STAT(tx_ring->stat_fail_no_tcb);
3065779Sxy150489 goto tx_failure;
3075779Sxy150489 }
3085779Sxy150489
3095779Sxy150489 /*
3105779Sxy150489 * Push the tx control block to the pending list
3115779Sxy150489 * to avoid using lock too early
3125779Sxy150489 */
3135779Sxy150489 LIST_PUSH_TAIL(&pending_list, &tcb->link);
3145779Sxy150489 }
3155779Sxy150489
3165779Sxy150489 if (current_flag == USE_COPY) {
3175779Sxy150489 /*
3185779Sxy150489 * Check whether to use bcopy or DMA binding to process
3195779Sxy150489 * the next fragment, and if using bcopy, whether we
3205779Sxy150489 * need to continue copying the next fragment into the
3215779Sxy150489 * current tx buffer.
3225779Sxy150489 */
3235779Sxy150489 ASSERT((tcb->tx_buf.len + current_len) <=
3245779Sxy150489 tcb->tx_buf.size);
3255779Sxy150489
3265779Sxy150489 if (eop) {
3275779Sxy150489 /*
3285779Sxy150489 * This is the last fragment of the packet, so
3295779Sxy150489 * the copy processing will be completed with
3305779Sxy150489 * this fragment.
3315779Sxy150489 */
3325779Sxy150489 next_flag = USE_NONE;
3335779Sxy150489 copy_done = B_TRUE;
3345779Sxy150489 } else if ((tcb->tx_buf.len + current_len + next_len) >
3355779Sxy150489 tcb->tx_buf.size) {
3365779Sxy150489 /*
3375779Sxy150489 * If the next fragment is too large to be
3385779Sxy150489 * copied to the current tx buffer, we need
3395779Sxy150489 * to complete the current copy processing.
3405779Sxy150489 */
3419188SPaul.Guo@Sun.COM next_flag = (next_len > copy_thresh) ?
3425779Sxy150489 USE_DMA: USE_COPY;
3435779Sxy150489 copy_done = B_TRUE;
3449188SPaul.Guo@Sun.COM } else if (next_len > copy_thresh) {
3455779Sxy150489 /*
3465779Sxy150489 * The next fragment needs to be processed with
3475779Sxy150489 * DMA binding. So the copy prcessing will be
3485779Sxy150489 * completed with the current fragment.
3495779Sxy150489 */
3505779Sxy150489 next_flag = USE_DMA;
3515779Sxy150489 copy_done = B_TRUE;
3525779Sxy150489 } else {
3535779Sxy150489 /*
3545779Sxy150489 * Continue to copy the next fragment to the
3555779Sxy150489 * current tx buffer.
3565779Sxy150489 */
3575779Sxy150489 next_flag = USE_COPY;
3585779Sxy150489 copy_done = B_FALSE;
3595779Sxy150489 }
3605779Sxy150489
3615779Sxy150489 desc_num = igb_tx_copy(tx_ring, tcb, current_mp,
3627072Sxy150489 current_len, copy_done);
3635779Sxy150489 } else {
3645779Sxy150489 /*
3655779Sxy150489 * Check whether to use bcopy or DMA binding to process
3665779Sxy150489 * the next fragment.
3675779Sxy150489 */
3689188SPaul.Guo@Sun.COM next_flag = (next_len > copy_thresh) ?
3695779Sxy150489 USE_DMA: USE_COPY;
3705779Sxy150489 ASSERT(copy_done == B_TRUE);
3715779Sxy150489
3725779Sxy150489 desc_num = igb_tx_bind(tx_ring, tcb, current_mp,
3735779Sxy150489 current_len);
3745779Sxy150489 }
3755779Sxy150489
3765779Sxy150489 if (desc_num > 0)
3775779Sxy150489 desc_total += desc_num;
3785779Sxy150489 else if (desc_num < 0)
3795779Sxy150489 goto tx_failure;
3805779Sxy150489
3815779Sxy150489 current_mp = next_mp;
3825779Sxy150489 current_len = next_len;
3835779Sxy150489 current_flag = next_flag;
3845779Sxy150489 }
3855779Sxy150489
3865779Sxy150489 /*
3875779Sxy150489 * Attach the mblk to the last tx control block
3885779Sxy150489 */
3895779Sxy150489 ASSERT(tcb);
3905779Sxy150489 ASSERT(tcb->mp == NULL);
3915779Sxy150489 tcb->mp = mp;
3925779Sxy150489
3935779Sxy150489 /*
3945779Sxy150489 * Before fill the tx descriptor ring with the data, we need to
3955779Sxy150489 * ensure there are adequate free descriptors for transmit
3965779Sxy150489 * (including one context descriptor).
397*12980SGuoqing.Zhu@Sun.COM * Do not use up all the tx descriptors.
398*12980SGuoqing.Zhu@Sun.COM * Otherwise tx recycle will fail and cause false hang.
3995779Sxy150489 */
400*12980SGuoqing.Zhu@Sun.COM if (tx_ring->tbd_free <= (desc_total + 1)) {
4015779Sxy150489 tx_ring->tx_recycle(tx_ring);
4025779Sxy150489 }
4035779Sxy150489
4045779Sxy150489 mutex_enter(&tx_ring->tx_lock);
4055779Sxy150489
4065779Sxy150489 /*
4075779Sxy150489 * If the number of free tx descriptors is not enough for transmit
4085779Sxy150489 * then return failure.
4095779Sxy150489 *
4105779Sxy150489 * Note: we must put this check under the mutex protection to
4115779Sxy150489 * ensure the correctness when multiple threads access it in
4125779Sxy150489 * parallel.
4135779Sxy150489 */
414*12980SGuoqing.Zhu@Sun.COM if (tx_ring->tbd_free <= (desc_total + 1)) {
4155779Sxy150489 IGB_DEBUG_STAT(tx_ring->stat_fail_no_tbd);
4165779Sxy150489 mutex_exit(&tx_ring->tx_lock);
4175779Sxy150489 goto tx_failure;
4185779Sxy150489 }
4195779Sxy150489
4209188SPaul.Guo@Sun.COM desc_num = igb_tx_fill_ring(tx_ring, &pending_list, ctx, mbsize);
4215779Sxy150489
4225779Sxy150489 ASSERT((desc_num == desc_total) || (desc_num == (desc_total + 1)));
4235779Sxy150489
42411878SVenu.Iyer@Sun.COM /* Update per-ring tx statistics */
42511878SVenu.Iyer@Sun.COM tx_ring->tx_pkts++;
42611878SVenu.Iyer@Sun.COM tx_ring->tx_bytes += mbsize;
42711878SVenu.Iyer@Sun.COM
4285779Sxy150489 mutex_exit(&tx_ring->tx_lock);
4295779Sxy150489
4305779Sxy150489 return (B_TRUE);
4315779Sxy150489
4325779Sxy150489 tx_failure:
4335779Sxy150489 /*
43411502SChenlu.Chen@Sun.COM * If new mblk has been allocted for the last header
43511502SChenlu.Chen@Sun.COM * fragment of a LSO packet, we should restore the
43611502SChenlu.Chen@Sun.COM * modified mp.
43711502SChenlu.Chen@Sun.COM */
43811502SChenlu.Chen@Sun.COM if (hdr_new_mp) {
43911502SChenlu.Chen@Sun.COM hdr_new_mp->b_cont = NULL;
44011502SChenlu.Chen@Sun.COM freeb(hdr_new_mp);
44111502SChenlu.Chen@Sun.COM hdr_current_mp->b_rptr -= hdr_frag_len;
44211502SChenlu.Chen@Sun.COM if (hdr_previous_mp)
44311502SChenlu.Chen@Sun.COM hdr_previous_mp->b_cont = hdr_current_mp;
44411502SChenlu.Chen@Sun.COM else
44511502SChenlu.Chen@Sun.COM mp = hdr_current_mp;
44611502SChenlu.Chen@Sun.COM }
44711502SChenlu.Chen@Sun.COM
44811502SChenlu.Chen@Sun.COM /*
4495779Sxy150489 * Discard the mblk and free the used resources
4505779Sxy150489 */
4515779Sxy150489 tcb = (tx_control_block_t *)LIST_GET_HEAD(&pending_list);
4525779Sxy150489 while (tcb) {
4535779Sxy150489 tcb->mp = NULL;
4545779Sxy150489
4555779Sxy150489 igb_free_tcb(tcb);
4565779Sxy150489
4575779Sxy150489 tcb = (tx_control_block_t *)
4585779Sxy150489 LIST_GET_NEXT(&pending_list, &tcb->link);
4595779Sxy150489 }
4605779Sxy150489
4615779Sxy150489 /*
4625779Sxy150489 * Return the tx control blocks in the pending list to the free list.
4635779Sxy150489 */
4645779Sxy150489 igb_put_free_list(tx_ring, &pending_list);
4655779Sxy150489
4665779Sxy150489 /* Transmit failed, do not drop the mblk, rechedule the transmit */
4675779Sxy150489 tx_ring->reschedule = B_TRUE;
4685779Sxy150489
4695779Sxy150489 return (B_FALSE);
4705779Sxy150489 }
4715779Sxy150489
4725779Sxy150489 /*
4735779Sxy150489 * igb_tx_copy
4745779Sxy150489 *
4755779Sxy150489 * Copy the mblk fragment to the pre-allocated tx buffer
4765779Sxy150489 */
4775779Sxy150489 static int
igb_tx_copy(igb_tx_ring_t * tx_ring,tx_control_block_t * tcb,mblk_t * mp,uint32_t len,boolean_t copy_done)4785779Sxy150489 igb_tx_copy(igb_tx_ring_t *tx_ring, tx_control_block_t *tcb, mblk_t *mp,
4797072Sxy150489 uint32_t len, boolean_t copy_done)
4805779Sxy150489 {
4815779Sxy150489 dma_buffer_t *tx_buf;
4825779Sxy150489 uint32_t desc_num;
4835779Sxy150489 _NOTE(ARGUNUSED(tx_ring));
4845779Sxy150489
4855779Sxy150489 tx_buf = &tcb->tx_buf;
4865779Sxy150489
4875779Sxy150489 /*
4885779Sxy150489 * Copy the packet data of the mblk fragment into the
4895779Sxy150489 * pre-allocated tx buffer, which is maintained by the
4905779Sxy150489 * tx control block.
4915779Sxy150489 *
4925779Sxy150489 * Several mblk fragments can be copied into one tx buffer.
4935779Sxy150489 * The destination address of the current copied fragment in
4945779Sxy150489 * the tx buffer is next to the end of the previous copied
4955779Sxy150489 * fragment.
4965779Sxy150489 */
4975779Sxy150489 if (len > 0) {
4985779Sxy150489 bcopy(mp->b_rptr, tx_buf->address + tx_buf->len, len);
4995779Sxy150489
5005779Sxy150489 tx_buf->len += len;
5015779Sxy150489 tcb->frag_num++;
5025779Sxy150489 }
5035779Sxy150489
5045779Sxy150489 desc_num = 0;
5055779Sxy150489
5065779Sxy150489 /*
5075779Sxy150489 * If it is the last fragment copied to the current tx buffer,
5085779Sxy150489 * in other words, if there's no remaining fragment or the remaining
5095779Sxy150489 * fragment requires a new tx control block to process, we need to
5105779Sxy150489 * complete the current copy processing by syncing up the current
5115779Sxy150489 * DMA buffer and saving the descriptor data.
5125779Sxy150489 */
5135779Sxy150489 if (copy_done) {
5145779Sxy150489 /*
5155779Sxy150489 * Sync the DMA buffer of the packet data
5165779Sxy150489 */
5175779Sxy150489 DMA_SYNC(tx_buf, DDI_DMA_SYNC_FORDEV);
5185779Sxy150489
5195779Sxy150489 tcb->tx_type = USE_COPY;
5205779Sxy150489
5215779Sxy150489 /*
5225779Sxy150489 * Save the address and length to the private data structure
5235779Sxy150489 * of the tx control block, which will be used to fill the
5245779Sxy150489 * tx descriptor ring after all the fragments are processed.
5255779Sxy150489 */
5265779Sxy150489 igb_save_desc(tcb, tx_buf->dma_address, tx_buf->len);
5275779Sxy150489 desc_num++;
5285779Sxy150489 }
5295779Sxy150489
5305779Sxy150489 return (desc_num);
5315779Sxy150489 }
5325779Sxy150489
5335779Sxy150489 /*
5345779Sxy150489 * igb_tx_bind
5355779Sxy150489 *
5365779Sxy150489 * Bind the mblk fragment with DMA
5375779Sxy150489 */
5385779Sxy150489 static int
igb_tx_bind(igb_tx_ring_t * tx_ring,tx_control_block_t * tcb,mblk_t * mp,uint32_t len)5395779Sxy150489 igb_tx_bind(igb_tx_ring_t *tx_ring, tx_control_block_t *tcb, mblk_t *mp,
5405779Sxy150489 uint32_t len)
5415779Sxy150489 {
5425779Sxy150489 int status, i;
5435779Sxy150489 ddi_dma_cookie_t dma_cookie;
5445779Sxy150489 uint_t ncookies;
5455779Sxy150489 int desc_num;
5465779Sxy150489
5475779Sxy150489 /*
5485779Sxy150489 * Use DMA binding to process the mblk fragment
5495779Sxy150489 */
5505779Sxy150489 status = ddi_dma_addr_bind_handle(tcb->tx_dma_handle, NULL,
5515779Sxy150489 (caddr_t)mp->b_rptr, len,
5525779Sxy150489 DDI_DMA_WRITE | DDI_DMA_STREAMING, DDI_DMA_DONTWAIT,
5535779Sxy150489 0, &dma_cookie, &ncookies);
5545779Sxy150489
5555779Sxy150489 if (status != DDI_DMA_MAPPED) {
5565779Sxy150489 IGB_DEBUG_STAT(tx_ring->stat_fail_dma_bind);
5575779Sxy150489 return (-1);
5585779Sxy150489 }
5595779Sxy150489
5605779Sxy150489 tcb->frag_num++;
5615779Sxy150489 tcb->tx_type = USE_DMA;
5625779Sxy150489 /*
5635779Sxy150489 * Each fragment can span several cookies. One cookie will have
5645779Sxy150489 * one tx descriptor to transmit.
5655779Sxy150489 */
5665779Sxy150489 desc_num = 0;
5675779Sxy150489 for (i = ncookies; i > 0; i--) {
5685779Sxy150489 /*
5695779Sxy150489 * Save the address and length to the private data structure
5705779Sxy150489 * of the tx control block, which will be used to fill the
5715779Sxy150489 * tx descriptor ring after all the fragments are processed.
5725779Sxy150489 */
5735779Sxy150489 igb_save_desc(tcb,
5745779Sxy150489 dma_cookie.dmac_laddress,
5755779Sxy150489 dma_cookie.dmac_size);
5765779Sxy150489
5775779Sxy150489 desc_num++;
5785779Sxy150489
5795779Sxy150489 if (i > 1)
5805779Sxy150489 ddi_dma_nextcookie(tcb->tx_dma_handle, &dma_cookie);
5815779Sxy150489 }
5825779Sxy150489
5835779Sxy150489 return (desc_num);
5845779Sxy150489 }
5855779Sxy150489
5865779Sxy150489 /*
5879188SPaul.Guo@Sun.COM * igb_get_tx_context
5885779Sxy150489 *
5899188SPaul.Guo@Sun.COM * Get the tx context information from the mblk
5905779Sxy150489 */
5919188SPaul.Guo@Sun.COM static int
igb_get_tx_context(mblk_t * mp,tx_context_t * ctx)5929188SPaul.Guo@Sun.COM igb_get_tx_context(mblk_t *mp, tx_context_t *ctx)
5935779Sxy150489 {
5945779Sxy150489 uint32_t start;
5955779Sxy150489 uint32_t flags;
5969188SPaul.Guo@Sun.COM uint32_t lso_flag;
5979188SPaul.Guo@Sun.COM uint32_t mss;
5985779Sxy150489 uint32_t len;
5995779Sxy150489 uint32_t size;
6005779Sxy150489 uint32_t offset;
6015779Sxy150489 unsigned char *pos;
6025779Sxy150489 ushort_t etype;
6035779Sxy150489 uint32_t mac_hdr_len;
6045779Sxy150489 uint32_t l4_proto;
6059188SPaul.Guo@Sun.COM uint32_t l4_hdr_len;
6065779Sxy150489
6075779Sxy150489 ASSERT(mp != NULL);
6085779Sxy150489
60911878SVenu.Iyer@Sun.COM mac_hcksum_get(mp, &start, NULL, NULL, NULL, &flags);
6109188SPaul.Guo@Sun.COM bzero(ctx, sizeof (tx_context_t));
6115779Sxy150489
6129188SPaul.Guo@Sun.COM ctx->hcksum_flags = flags;
6135779Sxy150489
6145779Sxy150489 if (flags == 0)
6159188SPaul.Guo@Sun.COM return (TX_CXT_SUCCESS);
6169188SPaul.Guo@Sun.COM
61711878SVenu.Iyer@Sun.COM mac_lso_get(mp, &mss, &lso_flag);
6189188SPaul.Guo@Sun.COM ctx->mss = mss;
6199188SPaul.Guo@Sun.COM ctx->lso_flag = (lso_flag == HW_LSO);
6209188SPaul.Guo@Sun.COM
6219188SPaul.Guo@Sun.COM /*
6229188SPaul.Guo@Sun.COM * LSO relies on tx h/w checksum, so here the packet will be
6239188SPaul.Guo@Sun.COM * dropped if the h/w checksum flags are not set.
6249188SPaul.Guo@Sun.COM */
6259188SPaul.Guo@Sun.COM if (ctx->lso_flag) {
6269188SPaul.Guo@Sun.COM if (!((ctx->hcksum_flags & HCK_PARTIALCKSUM) &&
6279188SPaul.Guo@Sun.COM (ctx->hcksum_flags & HCK_IPV4_HDRCKSUM))) {
6289188SPaul.Guo@Sun.COM IGB_DEBUGLOG_0(NULL, "igb_tx: h/w "
6299188SPaul.Guo@Sun.COM "checksum flags are not set for LSO");
6309188SPaul.Guo@Sun.COM return (TX_CXT_E_LSO_CSUM);
6319188SPaul.Guo@Sun.COM }
6329188SPaul.Guo@Sun.COM }
6335779Sxy150489
6345779Sxy150489 etype = 0;
6355779Sxy150489 mac_hdr_len = 0;
6365779Sxy150489 l4_proto = 0;
6375779Sxy150489
6385779Sxy150489 /*
6395779Sxy150489 * Firstly get the position of the ether_type/ether_tpid.
6405779Sxy150489 * Here we don't assume the ether (VLAN) header is fully included
6415779Sxy150489 * in one mblk fragment, so we go thourgh the fragments to parse
6425779Sxy150489 * the ether type.
6435779Sxy150489 */
6449188SPaul.Guo@Sun.COM size = len = MBLKL(mp);
6455779Sxy150489 offset = offsetof(struct ether_header, ether_type);
6465779Sxy150489 while (size <= offset) {
6475779Sxy150489 mp = mp->b_cont;
6485779Sxy150489 ASSERT(mp != NULL);
6499188SPaul.Guo@Sun.COM len = MBLKL(mp);
6505779Sxy150489 size += len;
6515779Sxy150489 }
6525779Sxy150489 pos = mp->b_rptr + offset + len - size;
6535779Sxy150489
6545779Sxy150489 etype = ntohs(*(ushort_t *)(uintptr_t)pos);
6555779Sxy150489 if (etype == ETHERTYPE_VLAN) {
6565779Sxy150489 /*
6575779Sxy150489 * Get the position of the ether_type in VLAN header
6585779Sxy150489 */
6595779Sxy150489 offset = offsetof(struct ether_vlan_header, ether_type);
6605779Sxy150489 while (size <= offset) {
6615779Sxy150489 mp = mp->b_cont;
6625779Sxy150489 ASSERT(mp != NULL);
6639188SPaul.Guo@Sun.COM len = MBLKL(mp);
6645779Sxy150489 size += len;
6655779Sxy150489 }
6665779Sxy150489 pos = mp->b_rptr + offset + len - size;
6675779Sxy150489
6685779Sxy150489 etype = ntohs(*(ushort_t *)(uintptr_t)pos);
6695779Sxy150489 mac_hdr_len = sizeof (struct ether_vlan_header);
6705779Sxy150489 } else {
6715779Sxy150489 mac_hdr_len = sizeof (struct ether_header);
6725779Sxy150489 }
6735779Sxy150489
6745779Sxy150489 /*
6759188SPaul.Guo@Sun.COM * Here we assume the IP(V6) header is fully included in one
6769188SPaul.Guo@Sun.COM * mblk fragment.
6775779Sxy150489 */
6785779Sxy150489 switch (etype) {
6795779Sxy150489 case ETHERTYPE_IP:
6809188SPaul.Guo@Sun.COM offset = mac_hdr_len;
6815779Sxy150489 while (size <= offset) {
6825779Sxy150489 mp = mp->b_cont;
6835779Sxy150489 ASSERT(mp != NULL);
6849188SPaul.Guo@Sun.COM len = MBLKL(mp);
6855779Sxy150489 size += len;
6865779Sxy150489 }
6875779Sxy150489 pos = mp->b_rptr + offset + len - size;
6885779Sxy150489
6899188SPaul.Guo@Sun.COM if (ctx->lso_flag) {
6909188SPaul.Guo@Sun.COM *((uint16_t *)(uintptr_t)(pos + offsetof(ipha_t,
6919188SPaul.Guo@Sun.COM ipha_length))) = 0;
6929188SPaul.Guo@Sun.COM
6939188SPaul.Guo@Sun.COM /*
6949188SPaul.Guo@Sun.COM * To utilize igb LSO, here need to fill
6959188SPaul.Guo@Sun.COM * the tcp checksum field of the packet with the
6969188SPaul.Guo@Sun.COM * following pseudo-header checksum:
6979188SPaul.Guo@Sun.COM * (ip_source_addr, ip_destination_addr, l4_proto)
6989188SPaul.Guo@Sun.COM * and also need to fill the ip header checksum
6999188SPaul.Guo@Sun.COM * with zero. Currently the tcp/ip stack has done
7009188SPaul.Guo@Sun.COM * these.
7019188SPaul.Guo@Sun.COM */
7029188SPaul.Guo@Sun.COM }
7039188SPaul.Guo@Sun.COM
7049188SPaul.Guo@Sun.COM l4_proto = *(uint8_t *)(pos + offsetof(ipha_t, ipha_protocol));
7055779Sxy150489 break;
7065779Sxy150489 case ETHERTYPE_IPV6:
7075779Sxy150489 offset = offsetof(ip6_t, ip6_nxt) + mac_hdr_len;
7085779Sxy150489 while (size <= offset) {
7095779Sxy150489 mp = mp->b_cont;
7105779Sxy150489 ASSERT(mp != NULL);
7119188SPaul.Guo@Sun.COM len = MBLKL(mp);
7125779Sxy150489 size += len;
7135779Sxy150489 }
7145779Sxy150489 pos = mp->b_rptr + offset + len - size;
7155779Sxy150489
7165779Sxy150489 l4_proto = *(uint8_t *)pos;
7175779Sxy150489 break;
7185779Sxy150489 default:
7195779Sxy150489 /* Unrecoverable error */
7209188SPaul.Guo@Sun.COM IGB_DEBUGLOG_0(NULL, "Ethernet type field error with "
7219188SPaul.Guo@Sun.COM "tx hcksum flag set");
7229188SPaul.Guo@Sun.COM return (TX_CXT_E_ETHER_TYPE);
7235779Sxy150489 }
7245779Sxy150489
7259188SPaul.Guo@Sun.COM if (ctx->lso_flag) {
7269188SPaul.Guo@Sun.COM offset = mac_hdr_len + start;
7279188SPaul.Guo@Sun.COM while (size <= offset) {
7289188SPaul.Guo@Sun.COM mp = mp->b_cont;
7299188SPaul.Guo@Sun.COM ASSERT(mp != NULL);
7309188SPaul.Guo@Sun.COM len = MBLKL(mp);
7319188SPaul.Guo@Sun.COM size += len;
7329188SPaul.Guo@Sun.COM }
7339188SPaul.Guo@Sun.COM pos = mp->b_rptr + offset + len - size;
7349188SPaul.Guo@Sun.COM
7359188SPaul.Guo@Sun.COM l4_hdr_len = TCP_HDR_LENGTH((tcph_t *)pos);
7369188SPaul.Guo@Sun.COM } else {
7379188SPaul.Guo@Sun.COM /*
7389188SPaul.Guo@Sun.COM * l4 header length is only required for LSO
7399188SPaul.Guo@Sun.COM */
7409188SPaul.Guo@Sun.COM l4_hdr_len = 0;
7419188SPaul.Guo@Sun.COM }
7429188SPaul.Guo@Sun.COM
7439188SPaul.Guo@Sun.COM ctx->mac_hdr_len = mac_hdr_len;
7449188SPaul.Guo@Sun.COM ctx->ip_hdr_len = start;
7459188SPaul.Guo@Sun.COM ctx->l4_proto = l4_proto;
7469188SPaul.Guo@Sun.COM ctx->l4_hdr_len = l4_hdr_len;
7479188SPaul.Guo@Sun.COM
7489188SPaul.Guo@Sun.COM return (TX_CXT_SUCCESS);
7495779Sxy150489 }
7505779Sxy150489
7515779Sxy150489 /*
7529188SPaul.Guo@Sun.COM * igb_check_tx_context
7535779Sxy150489 *
7545779Sxy150489 * Check if a new context descriptor is needed
7555779Sxy150489 */
7565779Sxy150489 static boolean_t
igb_check_tx_context(igb_tx_ring_t * tx_ring,tx_context_t * ctx)7579188SPaul.Guo@Sun.COM igb_check_tx_context(igb_tx_ring_t *tx_ring, tx_context_t *ctx)
7585779Sxy150489 {
7599188SPaul.Guo@Sun.COM tx_context_t *last;
7605779Sxy150489
7619188SPaul.Guo@Sun.COM if (ctx == NULL)
7625779Sxy150489 return (B_FALSE);
7635779Sxy150489
7645779Sxy150489 /*
7659188SPaul.Guo@Sun.COM * Compare the context data retrieved from the mblk and the
7669188SPaul.Guo@Sun.COM * stored context data of the last context descriptor. The data
7675779Sxy150489 * need to be checked are:
7685779Sxy150489 * hcksum_flags
7695779Sxy150489 * l4_proto
7709188SPaul.Guo@Sun.COM * mss (only check for LSO)
7719188SPaul.Guo@Sun.COM * l4_hdr_len (only check for LSO)
7729188SPaul.Guo@Sun.COM * ip_hdr_len
7735779Sxy150489 * mac_hdr_len
7745779Sxy150489 * Either one of the above data is changed, a new context descriptor
7755779Sxy150489 * will be needed.
7765779Sxy150489 */
7779188SPaul.Guo@Sun.COM last = &tx_ring->tx_context;
7785779Sxy150489
7799188SPaul.Guo@Sun.COM if (ctx->hcksum_flags != 0) {
7809188SPaul.Guo@Sun.COM if ((ctx->hcksum_flags != last->hcksum_flags) ||
7819188SPaul.Guo@Sun.COM (ctx->l4_proto != last->l4_proto) ||
7829188SPaul.Guo@Sun.COM (ctx->lso_flag && ((ctx->mss != last->mss) ||
7839188SPaul.Guo@Sun.COM (ctx->l4_hdr_len != last->l4_hdr_len))) ||
7849188SPaul.Guo@Sun.COM (ctx->ip_hdr_len != last->ip_hdr_len) ||
7859188SPaul.Guo@Sun.COM (ctx->mac_hdr_len != last->mac_hdr_len)) {
7865779Sxy150489 return (B_TRUE);
7875779Sxy150489 }
7885779Sxy150489 }
7895779Sxy150489
7905779Sxy150489 return (B_FALSE);
7915779Sxy150489 }
7925779Sxy150489
7935779Sxy150489 /*
7949188SPaul.Guo@Sun.COM * igb_fill_tx_context
7955779Sxy150489 *
7965779Sxy150489 * Fill the context descriptor with hardware checksum informations
7975779Sxy150489 */
7985779Sxy150489 static void
igb_fill_tx_context(struct e1000_adv_tx_context_desc * ctx_tbd,tx_context_t * ctx,uint32_t ring_index)7999188SPaul.Guo@Sun.COM igb_fill_tx_context(struct e1000_adv_tx_context_desc *ctx_tbd,
8009188SPaul.Guo@Sun.COM tx_context_t *ctx, uint32_t ring_index)
8015779Sxy150489 {
8025779Sxy150489 /*
8035779Sxy150489 * Fill the context descriptor with the checksum
8045779Sxy150489 * context information we've got
8055779Sxy150489 */
8069188SPaul.Guo@Sun.COM ctx_tbd->vlan_macip_lens = ctx->ip_hdr_len;
8079188SPaul.Guo@Sun.COM ctx_tbd->vlan_macip_lens |= ctx->mac_hdr_len <<
8085779Sxy150489 E1000_ADVTXD_MACLEN_SHIFT;
8095779Sxy150489
8105779Sxy150489 ctx_tbd->type_tucmd_mlhl =
8115779Sxy150489 E1000_ADVTXD_DCMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
8125779Sxy150489
8139188SPaul.Guo@Sun.COM if (ctx->hcksum_flags & HCK_IPV4_HDRCKSUM)
8145779Sxy150489 ctx_tbd->type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_IPV4;
8155779Sxy150489
8169188SPaul.Guo@Sun.COM if (ctx->hcksum_flags & HCK_PARTIALCKSUM) {
8179188SPaul.Guo@Sun.COM switch (ctx->l4_proto) {
8185779Sxy150489 case IPPROTO_TCP:
8195779Sxy150489 ctx_tbd->type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_TCP;
8205779Sxy150489 break;
8215779Sxy150489 case IPPROTO_UDP:
8225779Sxy150489 /*
8235779Sxy150489 * We don't have to explicitly set:
8245779Sxy150489 * ctx_tbd->type_tucmd_mlhl |=
8255779Sxy150489 * E1000_ADVTXD_TUCMD_L4T_UDP;
8265779Sxy150489 * Because E1000_ADVTXD_TUCMD_L4T_UDP == 0b
8275779Sxy150489 */
8285779Sxy150489 break;
8295779Sxy150489 default:
8305779Sxy150489 /* Unrecoverable error */
8315779Sxy150489 IGB_DEBUGLOG_0(NULL, "L4 type error with tx hcksum");
8325779Sxy150489 break;
8335779Sxy150489 }
8345779Sxy150489 }
8355779Sxy150489
8365779Sxy150489 ctx_tbd->seqnum_seed = 0;
8378275SEric Cheng ctx_tbd->mss_l4len_idx = ring_index << 4;
8389188SPaul.Guo@Sun.COM if (ctx->lso_flag) {
8399188SPaul.Guo@Sun.COM ctx_tbd->mss_l4len_idx |=
8409188SPaul.Guo@Sun.COM (ctx->l4_hdr_len << E1000_ADVTXD_L4LEN_SHIFT) |
8419188SPaul.Guo@Sun.COM (ctx->mss << E1000_ADVTXD_MSS_SHIFT);
8429188SPaul.Guo@Sun.COM }
8435779Sxy150489 }
8445779Sxy150489
8455779Sxy150489 /*
8465779Sxy150489 * igb_tx_fill_ring
8475779Sxy150489 *
8485779Sxy150489 * Fill the tx descriptor ring with the data
8495779Sxy150489 */
8505779Sxy150489 static int
igb_tx_fill_ring(igb_tx_ring_t * tx_ring,link_list_t * pending_list,tx_context_t * ctx,size_t mbsize)8515779Sxy150489 igb_tx_fill_ring(igb_tx_ring_t *tx_ring, link_list_t *pending_list,
8529188SPaul.Guo@Sun.COM tx_context_t *ctx, size_t mbsize)
8535779Sxy150489 {
8545779Sxy150489 struct e1000_hw *hw = &tx_ring->igb->hw;
8555779Sxy150489 boolean_t load_context;
8565779Sxy150489 uint32_t index, tcb_index, desc_num;
8575779Sxy150489 union e1000_adv_tx_desc *tbd, *first_tbd;
8585779Sxy150489 tx_control_block_t *tcb, *first_tcb;
8595779Sxy150489 uint32_t hcksum_flags;
8605779Sxy150489 int i;
8616624Sgl147354 igb_t *igb = tx_ring->igb;
8625779Sxy150489
8635779Sxy150489 ASSERT(mutex_owned(&tx_ring->tx_lock));
8645779Sxy150489
8655779Sxy150489 tbd = NULL;
8665779Sxy150489 first_tbd = NULL;
8675779Sxy150489 first_tcb = NULL;
8685779Sxy150489 desc_num = 0;
8695779Sxy150489 hcksum_flags = 0;
8705779Sxy150489 load_context = B_FALSE;
8715779Sxy150489
8725779Sxy150489 /*
8735779Sxy150489 * Get the index of the first tx descriptor that will be filled,
8745779Sxy150489 * and the index of the first work list item that will be attached
8755779Sxy150489 * with the first used tx control block in the pending list.
8765779Sxy150489 * Note: the two indexes are the same.
8775779Sxy150489 */
8785779Sxy150489 index = tx_ring->tbd_tail;
8795779Sxy150489 tcb_index = tx_ring->tbd_tail;
8805779Sxy150489
8819188SPaul.Guo@Sun.COM if (ctx != NULL) {
8829188SPaul.Guo@Sun.COM hcksum_flags = ctx->hcksum_flags;
8835779Sxy150489
8845779Sxy150489 /*
8855779Sxy150489 * Check if a new context descriptor is needed for this packet
8865779Sxy150489 */
8879188SPaul.Guo@Sun.COM load_context = igb_check_tx_context(tx_ring, ctx);
8885779Sxy150489 if (load_context) {
8895779Sxy150489 tbd = &tx_ring->tbd_ring[index];
8905779Sxy150489
8915779Sxy150489 /*
8925779Sxy150489 * Fill the context descriptor with the
8935779Sxy150489 * hardware checksum offload informations.
8945779Sxy150489 */
8959188SPaul.Guo@Sun.COM igb_fill_tx_context(
8969188SPaul.Guo@Sun.COM (struct e1000_adv_tx_context_desc *)tbd,
8979188SPaul.Guo@Sun.COM ctx, tx_ring->index);
8985779Sxy150489
8995779Sxy150489 index = NEXT_INDEX(index, 1, tx_ring->ring_size);
9005779Sxy150489 desc_num++;
9015779Sxy150489
9025779Sxy150489 /*
9035779Sxy150489 * Store the checksum context data if
9045779Sxy150489 * a new context descriptor is added
9055779Sxy150489 */
9069188SPaul.Guo@Sun.COM tx_ring->tx_context = *ctx;
9075779Sxy150489 }
9085779Sxy150489 }
9095779Sxy150489
9105779Sxy150489 first_tbd = &tx_ring->tbd_ring[index];
9115779Sxy150489
9125779Sxy150489 /*
9135779Sxy150489 * Fill tx data descriptors with the data saved in the pending list.
9145779Sxy150489 * The tx control blocks in the pending list are added to the work list
9155779Sxy150489 * at the same time.
9165779Sxy150489 *
9175779Sxy150489 * The work list is strictly 1:1 corresponding to the descriptor ring.
9185779Sxy150489 * One item of the work list corresponds to one tx descriptor. Because
9195779Sxy150489 * one tx control block can span multiple tx descriptors, the tx
9205779Sxy150489 * control block will be added to the first work list item that
9215779Sxy150489 * corresponds to the first tx descriptor generated from that tx
9225779Sxy150489 * control block.
9235779Sxy150489 */
9245779Sxy150489 tcb = (tx_control_block_t *)LIST_POP_HEAD(pending_list);
925*12980SGuoqing.Zhu@Sun.COM first_tcb = tcb;
9265779Sxy150489 while (tcb != NULL) {
9275779Sxy150489
9285779Sxy150489 for (i = 0; i < tcb->desc_num; i++) {
9295779Sxy150489 tbd = &tx_ring->tbd_ring[index];
9305779Sxy150489
9315779Sxy150489 tbd->read.buffer_addr = tcb->desc[i].address;
9325779Sxy150489 tbd->read.cmd_type_len = tcb->desc[i].length;
9335779Sxy150489
9345779Sxy150489 tbd->read.cmd_type_len |= E1000_ADVTXD_DCMD_RS |
9358571SChenlu.Chen@Sun.COM E1000_ADVTXD_DCMD_DEXT | E1000_ADVTXD_DTYP_DATA |
9368571SChenlu.Chen@Sun.COM E1000_ADVTXD_DCMD_IFCS;
9375779Sxy150489
9385779Sxy150489 tbd->read.olinfo_status = 0;
9395779Sxy150489
9405779Sxy150489 index = NEXT_INDEX(index, 1, tx_ring->ring_size);
9415779Sxy150489 desc_num++;
9425779Sxy150489 }
9435779Sxy150489
9445779Sxy150489 /*
9455779Sxy150489 * Add the tx control block to the work list
9465779Sxy150489 */
9475779Sxy150489 ASSERT(tx_ring->work_list[tcb_index] == NULL);
9485779Sxy150489 tx_ring->work_list[tcb_index] = tcb;
9495779Sxy150489
9505779Sxy150489 tcb_index = index;
9515779Sxy150489 tcb = (tx_control_block_t *)LIST_POP_HEAD(pending_list);
9525779Sxy150489 }
9535779Sxy150489
954*12980SGuoqing.Zhu@Sun.COM if (load_context) {
955*12980SGuoqing.Zhu@Sun.COM /*
956*12980SGuoqing.Zhu@Sun.COM * Count the checksum context descriptor for
957*12980SGuoqing.Zhu@Sun.COM * the first tx control block.
958*12980SGuoqing.Zhu@Sun.COM */
959*12980SGuoqing.Zhu@Sun.COM first_tcb->desc_num++;
960*12980SGuoqing.Zhu@Sun.COM }
961*12980SGuoqing.Zhu@Sun.COM first_tcb->last_index = PREV_INDEX(index, 1, tx_ring->ring_size);
962*12980SGuoqing.Zhu@Sun.COM
9635779Sxy150489 /*
9645779Sxy150489 * The Insert Ethernet CRC (IFCS) bit and the checksum fields are only
9655779Sxy150489 * valid in the first descriptor of the packet.
9669188SPaul.Guo@Sun.COM * 82576 also requires the payload length setting even without LSO
9675779Sxy150489 */
9685779Sxy150489 ASSERT(first_tbd != NULL);
9695779Sxy150489 first_tbd->read.cmd_type_len |= E1000_ADVTXD_DCMD_IFCS;
9709188SPaul.Guo@Sun.COM if (ctx != NULL && ctx->lso_flag) {
9719188SPaul.Guo@Sun.COM first_tbd->read.cmd_type_len |= E1000_ADVTXD_DCMD_TSE;
9729188SPaul.Guo@Sun.COM first_tbd->read.olinfo_status |=
9739188SPaul.Guo@Sun.COM (mbsize - ctx->mac_hdr_len - ctx->ip_hdr_len
9749188SPaul.Guo@Sun.COM - ctx->l4_hdr_len) << E1000_ADVTXD_PAYLEN_SHIFT;
9759188SPaul.Guo@Sun.COM } else {
97611155SJason.Xu@Sun.COM if (hw->mac.type >= e1000_82576) {
9779188SPaul.Guo@Sun.COM first_tbd->read.olinfo_status |=
9789188SPaul.Guo@Sun.COM (mbsize << E1000_ADVTXD_PAYLEN_SHIFT);
9799188SPaul.Guo@Sun.COM }
9808571SChenlu.Chen@Sun.COM }
9815779Sxy150489
9825779Sxy150489 /* Set hardware checksum bits */
9835779Sxy150489 if (hcksum_flags != 0) {
9845779Sxy150489 if (hcksum_flags & HCK_IPV4_HDRCKSUM)
9855779Sxy150489 first_tbd->read.olinfo_status |=
9865779Sxy150489 E1000_TXD_POPTS_IXSM << 8;
9875779Sxy150489 if (hcksum_flags & HCK_PARTIALCKSUM)
9885779Sxy150489 first_tbd->read.olinfo_status |=
9895779Sxy150489 E1000_TXD_POPTS_TXSM << 8;
9908275SEric Cheng first_tbd->read.olinfo_status |= tx_ring->index << 4;
9915779Sxy150489 }
9925779Sxy150489
9935779Sxy150489 /*
9945779Sxy150489 * The last descriptor of packet needs End Of Packet (EOP),
9955779Sxy150489 * and Report Status (RS) bits set
9965779Sxy150489 */
9975779Sxy150489 ASSERT(tbd != NULL);
9985779Sxy150489 tbd->read.cmd_type_len |=
9995779Sxy150489 E1000_ADVTXD_DCMD_EOP | E1000_ADVTXD_DCMD_RS;
10005779Sxy150489
10018275SEric Cheng IGB_DEBUG_STAT(tx_ring->stat_pkt_cnt);
10028275SEric Cheng
10035779Sxy150489 /*
10045779Sxy150489 * Sync the DMA buffer of the tx descriptor ring
10055779Sxy150489 */
10065779Sxy150489 DMA_SYNC(&tx_ring->tbd_area, DDI_DMA_SYNC_FORDEV);
10075779Sxy150489
10085779Sxy150489 /*
10095779Sxy150489 * Update the number of the free tx descriptors.
10105779Sxy150489 * The mutual exclusion between the transmission and the recycling
10115779Sxy150489 * (for the tx descriptor ring and the work list) is implemented
10125779Sxy150489 * with the atomic operation on the number of the free tx descriptors.
10135779Sxy150489 *
10145779Sxy150489 * Note: we should always decrement the counter tbd_free before
10155779Sxy150489 * advancing the hardware TDT pointer to avoid the race condition -
10165779Sxy150489 * before the counter tbd_free is decremented, the transmit of the
10175779Sxy150489 * tx descriptors has done and the counter tbd_free is increased by
10185779Sxy150489 * the tx recycling.
10195779Sxy150489 */
10205779Sxy150489 i = igb_atomic_reserve(&tx_ring->tbd_free, desc_num);
10215779Sxy150489 ASSERT(i >= 0);
10225779Sxy150489
10235779Sxy150489 tx_ring->tbd_tail = index;
10245779Sxy150489
10255779Sxy150489 /*
10265779Sxy150489 * Advance the hardware TDT pointer of the tx descriptor ring
10275779Sxy150489 */
10285779Sxy150489 E1000_WRITE_REG(hw, E1000_TDT(tx_ring->index), index);
10295779Sxy150489
10306624Sgl147354 if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
10316624Sgl147354 ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
103211367SJason.Xu@Sun.COM atomic_or_32(&igb->igb_state, IGB_ERROR);
10336624Sgl147354 }
10346624Sgl147354
10355779Sxy150489 return (desc_num);
10365779Sxy150489 }
10375779Sxy150489
10385779Sxy150489 /*
10395779Sxy150489 * igb_save_desc
10405779Sxy150489 *
10415779Sxy150489 * Save the address/length pair to the private array
10425779Sxy150489 * of the tx control block. The address/length pairs
10435779Sxy150489 * will be filled into the tx descriptor ring later.
10445779Sxy150489 */
10455779Sxy150489 static void
igb_save_desc(tx_control_block_t * tcb,uint64_t address,size_t length)10465779Sxy150489 igb_save_desc(tx_control_block_t *tcb, uint64_t address, size_t length)
10475779Sxy150489 {
10485779Sxy150489 sw_desc_t *desc;
10495779Sxy150489
10505779Sxy150489 desc = &tcb->desc[tcb->desc_num];
10515779Sxy150489 desc->address = address;
10525779Sxy150489 desc->length = length;
10535779Sxy150489
10545779Sxy150489 tcb->desc_num++;
10555779Sxy150489 }
10565779Sxy150489
10575779Sxy150489 /*
10585779Sxy150489 * igb_tx_recycle_legacy
10595779Sxy150489 *
10605779Sxy150489 * Recycle the tx descriptors and tx control blocks.
10615779Sxy150489 *
10625779Sxy150489 * The work list is traversed to check if the corresponding
10635779Sxy150489 * tx descriptors have been transmitted. If so, the resources
10645779Sxy150489 * bound to the tx control blocks will be freed, and those
10655779Sxy150489 * tx control blocks will be returned to the free list.
10665779Sxy150489 */
10675779Sxy150489 uint32_t
igb_tx_recycle_legacy(igb_tx_ring_t * tx_ring)10685779Sxy150489 igb_tx_recycle_legacy(igb_tx_ring_t *tx_ring)
10695779Sxy150489 {
1070*12980SGuoqing.Zhu@Sun.COM uint32_t index, last_index, next_index;
10715779Sxy150489 int desc_num;
10725779Sxy150489 boolean_t desc_done;
10735779Sxy150489 tx_control_block_t *tcb;
10745779Sxy150489 link_list_t pending_list;
10756624Sgl147354 igb_t *igb = tx_ring->igb;
10765779Sxy150489
10775779Sxy150489 /*
10785779Sxy150489 * The mutex_tryenter() is used to avoid unnecessary
10795779Sxy150489 * lock contention.
10805779Sxy150489 */
10815779Sxy150489 if (mutex_tryenter(&tx_ring->recycle_lock) == 0)
10825779Sxy150489 return (0);
10835779Sxy150489
10845779Sxy150489 ASSERT(tx_ring->tbd_free <= tx_ring->ring_size);
10855779Sxy150489
10865779Sxy150489 if (tx_ring->tbd_free == tx_ring->ring_size) {
10875779Sxy150489 tx_ring->recycle_fail = 0;
10885779Sxy150489 tx_ring->stall_watchdog = 0;
10895779Sxy150489 mutex_exit(&tx_ring->recycle_lock);
10905779Sxy150489 return (0);
10915779Sxy150489 }
10925779Sxy150489
10935779Sxy150489 /*
10945779Sxy150489 * Sync the DMA buffer of the tx descriptor ring
10955779Sxy150489 */
10965779Sxy150489 DMA_SYNC(&tx_ring->tbd_area, DDI_DMA_SYNC_FORKERNEL);
10975779Sxy150489
10986624Sgl147354 if (igb_check_dma_handle(
10996624Sgl147354 tx_ring->tbd_area.dma_handle) != DDI_FM_OK) {
110011557SChenlu.Chen@Sun.COM mutex_exit(&tx_ring->recycle_lock);
11016624Sgl147354 ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
110211367SJason.Xu@Sun.COM atomic_or_32(&igb->igb_state, IGB_ERROR);
110311367SJason.Xu@Sun.COM return (0);
11046624Sgl147354 }
11056624Sgl147354
11065779Sxy150489 LINK_LIST_INIT(&pending_list);
11075779Sxy150489 desc_num = 0;
11085779Sxy150489 index = tx_ring->tbd_head; /* Index of next tbd/tcb to recycle */
11095779Sxy150489
11105779Sxy150489 tcb = tx_ring->work_list[index];
11115779Sxy150489 ASSERT(tcb != NULL);
11125779Sxy150489
1113*12980SGuoqing.Zhu@Sun.COM while (tcb != NULL) {
11145779Sxy150489
11155779Sxy150489 /*
1116*12980SGuoqing.Zhu@Sun.COM * Get the last tx descriptor of this packet.
1117*12980SGuoqing.Zhu@Sun.COM * If the last tx descriptor is done, then
1118*12980SGuoqing.Zhu@Sun.COM * we can recycle all descriptors of a packet
1119*12980SGuoqing.Zhu@Sun.COM * which usually includes several tx control blocks.
1120*12980SGuoqing.Zhu@Sun.COM * For some chips, LSO descriptors can not be recycled
1121*12980SGuoqing.Zhu@Sun.COM * unless the whole packet's transmission is done.
1122*12980SGuoqing.Zhu@Sun.COM * That's why packet level recycling is used here.
11235779Sxy150489 */
1124*12980SGuoqing.Zhu@Sun.COM last_index = tcb->last_index;
1125*12980SGuoqing.Zhu@Sun.COM /*
1126*12980SGuoqing.Zhu@Sun.COM * MAX_TX_RING_SIZE is used to judge whether
1127*12980SGuoqing.Zhu@Sun.COM * the index is a valid value or not.
1128*12980SGuoqing.Zhu@Sun.COM */
1129*12980SGuoqing.Zhu@Sun.COM if (last_index == MAX_TX_RING_SIZE)
1130*12980SGuoqing.Zhu@Sun.COM break;
1131*12980SGuoqing.Zhu@Sun.COM
1132*12980SGuoqing.Zhu@Sun.COM next_index = NEXT_INDEX(last_index, 1, tx_ring->ring_size);
11335779Sxy150489
11345779Sxy150489 /*
11355779Sxy150489 * Check if the Descriptor Done bit is set
11365779Sxy150489 */
11375779Sxy150489 desc_done = tx_ring->tbd_ring[last_index].wb.status &
11385779Sxy150489 E1000_TXD_STAT_DD;
11395779Sxy150489 if (desc_done) {
1140*12980SGuoqing.Zhu@Sun.COM while (tcb != NULL) {
1141*12980SGuoqing.Zhu@Sun.COM /*
1142*12980SGuoqing.Zhu@Sun.COM * Strip off the tx control block from the work
1143*12980SGuoqing.Zhu@Sun.COM * list, and add it to the pending list.
1144*12980SGuoqing.Zhu@Sun.COM */
1145*12980SGuoqing.Zhu@Sun.COM tx_ring->work_list[index] = NULL;
1146*12980SGuoqing.Zhu@Sun.COM LIST_PUSH_TAIL(&pending_list, &tcb->link);
11475779Sxy150489
1148*12980SGuoqing.Zhu@Sun.COM /*
1149*12980SGuoqing.Zhu@Sun.COM * Count the total number of the tx descriptors
1150*12980SGuoqing.Zhu@Sun.COM * recycled.
1151*12980SGuoqing.Zhu@Sun.COM */
1152*12980SGuoqing.Zhu@Sun.COM desc_num += tcb->desc_num;
11535779Sxy150489
1154*12980SGuoqing.Zhu@Sun.COM /*
1155*12980SGuoqing.Zhu@Sun.COM * Advance the index of the tx descriptor ring
1156*12980SGuoqing.Zhu@Sun.COM */
1157*12980SGuoqing.Zhu@Sun.COM index = NEXT_INDEX(index, tcb->desc_num,
1158*12980SGuoqing.Zhu@Sun.COM tx_ring->ring_size);
11595779Sxy150489
1160*12980SGuoqing.Zhu@Sun.COM tcb = tx_ring->work_list[index];
1161*12980SGuoqing.Zhu@Sun.COM if (index == next_index)
1162*12980SGuoqing.Zhu@Sun.COM break;
1163*12980SGuoqing.Zhu@Sun.COM }
1164*12980SGuoqing.Zhu@Sun.COM } else {
1165*12980SGuoqing.Zhu@Sun.COM break;
11665779Sxy150489 }
11675779Sxy150489 }
11685779Sxy150489
11695779Sxy150489 /*
11705779Sxy150489 * If no tx descriptors are recycled, no need to do more processing
11715779Sxy150489 */
11725779Sxy150489 if (desc_num == 0) {
11735779Sxy150489 tx_ring->recycle_fail++;
11745779Sxy150489 mutex_exit(&tx_ring->recycle_lock);
11755779Sxy150489 return (0);
11765779Sxy150489 }
11775779Sxy150489
11785779Sxy150489 tx_ring->recycle_fail = 0;
11795779Sxy150489 tx_ring->stall_watchdog = 0;
11805779Sxy150489
11815779Sxy150489 /*
11825779Sxy150489 * Update the head index of the tx descriptor ring
11835779Sxy150489 */
11845779Sxy150489 tx_ring->tbd_head = index;
11855779Sxy150489
11865779Sxy150489 /*
11875779Sxy150489 * Update the number of the free tx descriptors with atomic operations
11885779Sxy150489 */
11895779Sxy150489 atomic_add_32(&tx_ring->tbd_free, desc_num);
11905779Sxy150489
11915779Sxy150489 mutex_exit(&tx_ring->recycle_lock);
11925779Sxy150489
11935779Sxy150489 /*
11945779Sxy150489 * Free the resources used by the tx control blocks
11955779Sxy150489 * in the pending list
11965779Sxy150489 */
11975779Sxy150489 tcb = (tx_control_block_t *)LIST_GET_HEAD(&pending_list);
11985779Sxy150489 while (tcb != NULL) {
11995779Sxy150489 /*
12005779Sxy150489 * Release the resources occupied by the tx control block
12015779Sxy150489 */
12025779Sxy150489 igb_free_tcb(tcb);
12035779Sxy150489
12045779Sxy150489 tcb = (tx_control_block_t *)
12055779Sxy150489 LIST_GET_NEXT(&pending_list, &tcb->link);
12065779Sxy150489 }
12075779Sxy150489
12085779Sxy150489 /*
12095779Sxy150489 * Add the tx control blocks in the pending list to the free list.
12105779Sxy150489 */
12115779Sxy150489 igb_put_free_list(tx_ring, &pending_list);
12125779Sxy150489
12135779Sxy150489 return (desc_num);
12145779Sxy150489 }
12155779Sxy150489
12165779Sxy150489 /*
12175779Sxy150489 * igb_tx_recycle_head_wb
12185779Sxy150489 *
12195779Sxy150489 * Check the head write-back, and recycle all the transmitted
12205779Sxy150489 * tx descriptors and tx control blocks.
12215779Sxy150489 */
12225779Sxy150489 uint32_t
igb_tx_recycle_head_wb(igb_tx_ring_t * tx_ring)12235779Sxy150489 igb_tx_recycle_head_wb(igb_tx_ring_t *tx_ring)
12245779Sxy150489 {
12255779Sxy150489 uint32_t index;
12265779Sxy150489 uint32_t head_wb;
12275779Sxy150489 int desc_num;
12285779Sxy150489 tx_control_block_t *tcb;
12295779Sxy150489 link_list_t pending_list;
12306624Sgl147354 igb_t *igb = tx_ring->igb;
12315779Sxy150489
12325779Sxy150489 /*
12335779Sxy150489 * The mutex_tryenter() is used to avoid unnecessary
12345779Sxy150489 * lock contention.
12355779Sxy150489 */
12365779Sxy150489 if (mutex_tryenter(&tx_ring->recycle_lock) == 0)
12375779Sxy150489 return (0);
12385779Sxy150489
12395779Sxy150489 ASSERT(tx_ring->tbd_free <= tx_ring->ring_size);
12405779Sxy150489
12415779Sxy150489 if (tx_ring->tbd_free == tx_ring->ring_size) {
12425779Sxy150489 tx_ring->recycle_fail = 0;
12435779Sxy150489 tx_ring->stall_watchdog = 0;
12445779Sxy150489 mutex_exit(&tx_ring->recycle_lock);
12455779Sxy150489 return (0);
12465779Sxy150489 }
12475779Sxy150489
12485779Sxy150489 /*
12495779Sxy150489 * Sync the DMA buffer of the tx descriptor ring
12505779Sxy150489 *
12515779Sxy150489 * Note: For head write-back mode, the tx descriptors will not
12525779Sxy150489 * be written back, but the head write-back value is stored at
12535779Sxy150489 * the last extra tbd at the end of the DMA area, we still need
12545779Sxy150489 * to sync the head write-back value for kernel.
12555779Sxy150489 *
12565779Sxy150489 * DMA_SYNC(&tx_ring->tbd_area, DDI_DMA_SYNC_FORKERNEL);
12575779Sxy150489 */
12585779Sxy150489 (void) ddi_dma_sync(tx_ring->tbd_area.dma_handle,
12595779Sxy150489 sizeof (union e1000_adv_tx_desc) * tx_ring->ring_size,
12605779Sxy150489 sizeof (uint32_t),
12615779Sxy150489 DDI_DMA_SYNC_FORKERNEL);
12625779Sxy150489
12636624Sgl147354 if (igb_check_dma_handle(
12646624Sgl147354 tx_ring->tbd_area.dma_handle) != DDI_FM_OK) {
126511557SChenlu.Chen@Sun.COM mutex_exit(&tx_ring->recycle_lock);
12666624Sgl147354 ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
126711367SJason.Xu@Sun.COM atomic_or_32(&igb->igb_state, IGB_ERROR);
126811367SJason.Xu@Sun.COM return (0);
12696624Sgl147354 }
12706624Sgl147354
12715779Sxy150489 LINK_LIST_INIT(&pending_list);
12725779Sxy150489 desc_num = 0;
12735779Sxy150489 index = tx_ring->tbd_head; /* Next index to clean */
12745779Sxy150489
12755779Sxy150489 /*
12765779Sxy150489 * Get the value of head write-back
12775779Sxy150489 */
12785779Sxy150489 head_wb = *tx_ring->tbd_head_wb;
12795779Sxy150489 while (index != head_wb) {
12805779Sxy150489 tcb = tx_ring->work_list[index];
12815779Sxy150489 ASSERT(tcb != NULL);
12825779Sxy150489
12835779Sxy150489 if (OFFSET(index, head_wb, tx_ring->ring_size) <
12845779Sxy150489 tcb->desc_num) {
12855779Sxy150489 /*
12865779Sxy150489 * The current tx control block is not
12875779Sxy150489 * completely transmitted, stop recycling
12885779Sxy150489 */
12895779Sxy150489 break;
12905779Sxy150489 }
12915779Sxy150489
12925779Sxy150489 /*
12935779Sxy150489 * Strip off the tx control block from the work list,
12945779Sxy150489 * and add it to the pending list.
12955779Sxy150489 */
12965779Sxy150489 tx_ring->work_list[index] = NULL;
12975779Sxy150489 LIST_PUSH_TAIL(&pending_list, &tcb->link);
12985779Sxy150489
12995779Sxy150489 /*
13005779Sxy150489 * Advance the index of the tx descriptor ring
13015779Sxy150489 */
13025779Sxy150489 index = NEXT_INDEX(index, tcb->desc_num, tx_ring->ring_size);
13035779Sxy150489
13045779Sxy150489 /*
13055779Sxy150489 * Count the total number of the tx descriptors recycled
13065779Sxy150489 */
13075779Sxy150489 desc_num += tcb->desc_num;
13085779Sxy150489 }
13095779Sxy150489
13105779Sxy150489 /*
13115779Sxy150489 * If no tx descriptors are recycled, no need to do more processing
13125779Sxy150489 */
13135779Sxy150489 if (desc_num == 0) {
13145779Sxy150489 tx_ring->recycle_fail++;
13155779Sxy150489 mutex_exit(&tx_ring->recycle_lock);
13165779Sxy150489 return (0);
13175779Sxy150489 }
13185779Sxy150489
13195779Sxy150489 tx_ring->recycle_fail = 0;
13205779Sxy150489 tx_ring->stall_watchdog = 0;
13215779Sxy150489
13225779Sxy150489 /*
13235779Sxy150489 * Update the head index of the tx descriptor ring
13245779Sxy150489 */
13255779Sxy150489 tx_ring->tbd_head = index;
13265779Sxy150489
13275779Sxy150489 /*
13285779Sxy150489 * Update the number of the free tx descriptors with atomic operations
13295779Sxy150489 */
13305779Sxy150489 atomic_add_32(&tx_ring->tbd_free, desc_num);
13315779Sxy150489
13325779Sxy150489 mutex_exit(&tx_ring->recycle_lock);
13335779Sxy150489
13345779Sxy150489 /*
13355779Sxy150489 * Free the resources used by the tx control blocks
13365779Sxy150489 * in the pending list
13375779Sxy150489 */
13385779Sxy150489 tcb = (tx_control_block_t *)LIST_GET_HEAD(&pending_list);
13395779Sxy150489 while (tcb) {
13405779Sxy150489 /*
13415779Sxy150489 * Release the resources occupied by the tx control block
13425779Sxy150489 */
13435779Sxy150489 igb_free_tcb(tcb);
13445779Sxy150489
13455779Sxy150489 tcb = (tx_control_block_t *)
13465779Sxy150489 LIST_GET_NEXT(&pending_list, &tcb->link);
13475779Sxy150489 }
13485779Sxy150489
13495779Sxy150489 /*
13505779Sxy150489 * Add the tx control blocks in the pending list to the free list.
13515779Sxy150489 */
13525779Sxy150489 igb_put_free_list(tx_ring, &pending_list);
13535779Sxy150489
13545779Sxy150489 return (desc_num);
13555779Sxy150489 }
13565779Sxy150489
13575779Sxy150489 /*
13585779Sxy150489 * igb_free_tcb - free up the tx control block
13595779Sxy150489 *
13605779Sxy150489 * Free the resources of the tx control block, including
13615779Sxy150489 * unbind the previously bound DMA handle, and reset other
13625779Sxy150489 * control fields.
13635779Sxy150489 */
13645779Sxy150489 void
igb_free_tcb(tx_control_block_t * tcb)13655779Sxy150489 igb_free_tcb(tx_control_block_t *tcb)
13665779Sxy150489 {
13675779Sxy150489 switch (tcb->tx_type) {
13685779Sxy150489 case USE_COPY:
13695779Sxy150489 /*
13705779Sxy150489 * Reset the buffer length that is used for copy
13715779Sxy150489 */
13725779Sxy150489 tcb->tx_buf.len = 0;
13735779Sxy150489 break;
13745779Sxy150489 case USE_DMA:
13755779Sxy150489 /*
13765779Sxy150489 * Release the DMA resource that is used for
13775779Sxy150489 * DMA binding.
13785779Sxy150489 */
13795779Sxy150489 (void) ddi_dma_unbind_handle(tcb->tx_dma_handle);
13805779Sxy150489 break;
13815779Sxy150489 default:
13825779Sxy150489 break;
13835779Sxy150489 }
13845779Sxy150489
13855779Sxy150489 /*
13865779Sxy150489 * Free the mblk
13875779Sxy150489 */
13885779Sxy150489 if (tcb->mp != NULL) {
13895779Sxy150489 freemsg(tcb->mp);
13905779Sxy150489 tcb->mp = NULL;
13915779Sxy150489 }
13925779Sxy150489
13935779Sxy150489 tcb->tx_type = USE_NONE;
1394*12980SGuoqing.Zhu@Sun.COM tcb->last_index = MAX_TX_RING_SIZE;
13955779Sxy150489 tcb->frag_num = 0;
13965779Sxy150489 tcb->desc_num = 0;
13975779Sxy150489 }
13985779Sxy150489
13995779Sxy150489 /*
14005779Sxy150489 * igb_get_free_list - Get a free tx control block from the free list
14015779Sxy150489 *
14025779Sxy150489 * The atomic operation on the number of the available tx control block
14035779Sxy150489 * in the free list is used to keep this routine mutual exclusive with
14045779Sxy150489 * the routine igb_put_check_list.
14055779Sxy150489 */
14065779Sxy150489 static tx_control_block_t *
igb_get_free_list(igb_tx_ring_t * tx_ring)14075779Sxy150489 igb_get_free_list(igb_tx_ring_t *tx_ring)
14085779Sxy150489 {
14095779Sxy150489 tx_control_block_t *tcb;
14105779Sxy150489
14115779Sxy150489 /*
14125779Sxy150489 * Check and update the number of the free tx control block
14135779Sxy150489 * in the free list.
14145779Sxy150489 */
14155779Sxy150489 if (igb_atomic_reserve(&tx_ring->tcb_free, 1) < 0)
14165779Sxy150489 return (NULL);
14175779Sxy150489
14185779Sxy150489 mutex_enter(&tx_ring->tcb_head_lock);
14195779Sxy150489
14205779Sxy150489 tcb = tx_ring->free_list[tx_ring->tcb_head];
14215779Sxy150489 ASSERT(tcb != NULL);
14225779Sxy150489 tx_ring->free_list[tx_ring->tcb_head] = NULL;
14235779Sxy150489 tx_ring->tcb_head = NEXT_INDEX(tx_ring->tcb_head, 1,
14245779Sxy150489 tx_ring->free_list_size);
14255779Sxy150489
14265779Sxy150489 mutex_exit(&tx_ring->tcb_head_lock);
14275779Sxy150489
14285779Sxy150489 return (tcb);
14295779Sxy150489 }
14305779Sxy150489
14315779Sxy150489 /*
14325779Sxy150489 * igb_put_free_list
14335779Sxy150489 *
14345779Sxy150489 * Put a list of used tx control blocks back to the free list
14355779Sxy150489 *
14365779Sxy150489 * A mutex is used here to ensure the serialization. The mutual exclusion
14375779Sxy150489 * between igb_get_free_list and igb_put_free_list is implemented with
14385779Sxy150489 * the atomic operation on the counter tcb_free.
14395779Sxy150489 */
14405779Sxy150489 void
igb_put_free_list(igb_tx_ring_t * tx_ring,link_list_t * pending_list)14415779Sxy150489 igb_put_free_list(igb_tx_ring_t *tx_ring, link_list_t *pending_list)
14425779Sxy150489 {
14435779Sxy150489 uint32_t index;
14445779Sxy150489 int tcb_num;
14455779Sxy150489 tx_control_block_t *tcb;
14465779Sxy150489
14475779Sxy150489 mutex_enter(&tx_ring->tcb_tail_lock);
14485779Sxy150489
14495779Sxy150489 index = tx_ring->tcb_tail;
14505779Sxy150489
14515779Sxy150489 tcb_num = 0;
14525779Sxy150489 tcb = (tx_control_block_t *)LIST_POP_HEAD(pending_list);
14535779Sxy150489 while (tcb != NULL) {
14545779Sxy150489 ASSERT(tx_ring->free_list[index] == NULL);
14555779Sxy150489 tx_ring->free_list[index] = tcb;
14565779Sxy150489
14575779Sxy150489 tcb_num++;
14585779Sxy150489
14595779Sxy150489 index = NEXT_INDEX(index, 1, tx_ring->free_list_size);
14605779Sxy150489
14615779Sxy150489 tcb = (tx_control_block_t *)LIST_POP_HEAD(pending_list);
14625779Sxy150489 }
14635779Sxy150489
14645779Sxy150489 tx_ring->tcb_tail = index;
14655779Sxy150489
14665779Sxy150489 /*
14675779Sxy150489 * Update the number of the free tx control block
14685779Sxy150489 * in the free list. This operation must be placed
14695779Sxy150489 * under the protection of the lock.
14705779Sxy150489 */
14715779Sxy150489 atomic_add_32(&tx_ring->tcb_free, tcb_num);
14725779Sxy150489
14735779Sxy150489 mutex_exit(&tx_ring->tcb_tail_lock);
14745779Sxy150489 }
1475