16621Sbt150084 /*
26621Sbt150084 * CDDL HEADER START
36621Sbt150084 *
46621Sbt150084 * The contents of this file are subject to the terms of the
56621Sbt150084 * Common Development and Distribution License (the "License").
66621Sbt150084 * You may not use this file except in compliance with the License.
76621Sbt150084 *
88275SEric Cheng * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
98275SEric Cheng * or http://www.opensolaris.org/os/licensing.
106621Sbt150084 * See the License for the specific language governing permissions
116621Sbt150084 * and limitations under the License.
126621Sbt150084 *
138275SEric Cheng * When distributing Covered Code, include this CDDL HEADER in each
148275SEric Cheng * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156621Sbt150084 * If applicable, add the following below this CDDL HEADER, with the
166621Sbt150084 * fields enclosed by brackets "[]" replaced with your own identifying
176621Sbt150084 * information: Portions Copyright [yyyy] [name of copyright owner]
186621Sbt150084 *
196621Sbt150084 * CDDL HEADER END
206621Sbt150084 */
216621Sbt150084
226621Sbt150084 /*
23*13006SChenlu.Chen@Sun.COM * Copyright(c) 2007-2010 Intel Corporation. All rights reserved.
24*13006SChenlu.Chen@Sun.COM */
25*13006SChenlu.Chen@Sun.COM
26*13006SChenlu.Chen@Sun.COM /*
27*13006SChenlu.Chen@Sun.COM * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
288275SEric Cheng */
296621Sbt150084
306621Sbt150084 #include "ixgbe_sw.h"
316621Sbt150084
326621Sbt150084 static int ixgbe_tx_copy(ixgbe_tx_ring_t *, tx_control_block_t *, mblk_t *,
337167Sgg161487 uint32_t, boolean_t);
346621Sbt150084 static int ixgbe_tx_bind(ixgbe_tx_ring_t *, tx_control_block_t *, mblk_t *,
356621Sbt150084 uint32_t);
366621Sbt150084 static int ixgbe_tx_fill_ring(ixgbe_tx_ring_t *, link_list_t *,
377167Sgg161487 ixgbe_tx_context_t *, size_t);
386621Sbt150084 static void ixgbe_save_desc(tx_control_block_t *, uint64_t, size_t);
396621Sbt150084 static tx_control_block_t *ixgbe_get_free_list(ixgbe_tx_ring_t *);
406621Sbt150084
417167Sgg161487 static int ixgbe_get_context(mblk_t *, ixgbe_tx_context_t *);
427167Sgg161487 static boolean_t ixgbe_check_context(ixgbe_tx_ring_t *,
437167Sgg161487 ixgbe_tx_context_t *);
447167Sgg161487 static void ixgbe_fill_context(struct ixgbe_adv_tx_context_desc *,
459353SSamuel.Tu@Sun.COM ixgbe_tx_context_t *);
466621Sbt150084
476621Sbt150084 #ifndef IXGBE_DEBUG
486621Sbt150084 #pragma inline(ixgbe_save_desc)
497167Sgg161487 #pragma inline(ixgbe_get_context)
507167Sgg161487 #pragma inline(ixgbe_check_context)
517167Sgg161487 #pragma inline(ixgbe_fill_context)
526621Sbt150084 #endif
536621Sbt150084
546621Sbt150084 /*
558275SEric Cheng * ixgbe_ring_tx
566621Sbt150084 *
578275SEric Cheng * To transmit one mblk through one specified ring.
586621Sbt150084 *
596621Sbt150084 * One mblk can consist of several fragments, each fragment
606621Sbt150084 * will be processed with different methods based on the size.
616621Sbt150084 * For the fragments with size less than the bcopy threshold,
626621Sbt150084 * they will be processed by using bcopy; otherwise, they will
636621Sbt150084 * be processed by using DMA binding.
646621Sbt150084 *
656621Sbt150084 * To process the mblk, a tx control block is got from the
666621Sbt150084 * free list. One tx control block contains one tx buffer, which
676621Sbt150084 * is used to copy mblk fragments' data; and one tx DMA handle,
686621Sbt150084 * which is used to bind a mblk fragment with DMA resource.
696621Sbt150084 *
706621Sbt150084 * Several small mblk fragments can be copied into one tx control
716621Sbt150084 * block's buffer, and then the buffer will be transmitted with
726621Sbt150084 * one tx descriptor.
736621Sbt150084 *
746621Sbt150084 * A large fragment only binds with one tx control block's DMA
756621Sbt150084 * handle, and it can span several tx descriptors for transmitting.
766621Sbt150084 *
776621Sbt150084 * So to transmit a packet (mblk), several tx control blocks can
786621Sbt150084 * be used. After the processing, those tx control blocks will
796621Sbt150084 * be put to the work list.
806621Sbt150084 */
818275SEric Cheng mblk_t *
ixgbe_ring_tx(void * arg,mblk_t * mp)828275SEric Cheng ixgbe_ring_tx(void *arg, mblk_t *mp)
836621Sbt150084 {
848275SEric Cheng ixgbe_tx_ring_t *tx_ring = (ixgbe_tx_ring_t *)arg;
856621Sbt150084 ixgbe_t *ixgbe = tx_ring->ixgbe;
866621Sbt150084 tx_type_t current_flag, next_flag;
876621Sbt150084 uint32_t current_len, next_len;
886621Sbt150084 uint32_t desc_total;
896621Sbt150084 size_t mbsize;
906621Sbt150084 int desc_num;
916621Sbt150084 boolean_t copy_done, eop;
9210305SPaul.Guo@Sun.COM mblk_t *current_mp, *next_mp, *nmp, *pull_mp = NULL;
936621Sbt150084 tx_control_block_t *tcb;
947167Sgg161487 ixgbe_tx_context_t tx_context, *ctx;
956621Sbt150084 link_list_t pending_list;
968275SEric Cheng uint32_t len, hdr_frag_len, hdr_len;
978275SEric Cheng uint32_t copy_thresh;
9811150SZhen.W@Sun.COM mblk_t *hdr_new_mp = NULL;
9911150SZhen.W@Sun.COM mblk_t *hdr_pre_mp = NULL;
10011150SZhen.W@Sun.COM mblk_t *hdr_nmp = NULL;
1018275SEric Cheng
1028275SEric Cheng ASSERT(mp->b_next == NULL);
1038275SEric Cheng
10411233SPaul.Guo@Sun.COM if ((ixgbe->ixgbe_state & IXGBE_SUSPENDED) ||
10511233SPaul.Guo@Sun.COM (ixgbe->ixgbe_state & IXGBE_ERROR) ||
106*13006SChenlu.Chen@Sun.COM (ixgbe->ixgbe_state & IXGBE_OVERTEMP) ||
10711233SPaul.Guo@Sun.COM !(ixgbe->ixgbe_state & IXGBE_STARTED)) {
10811233SPaul.Guo@Sun.COM return (mp);
10911233SPaul.Guo@Sun.COM }
11011233SPaul.Guo@Sun.COM
11110376SChenlu.Chen@Sun.COM copy_thresh = ixgbe->tx_copy_thresh;
1126621Sbt150084
1136621Sbt150084 /* Get the mblk size */
1146621Sbt150084 mbsize = 0;
1156621Sbt150084 for (nmp = mp; nmp != NULL; nmp = nmp->b_cont) {
1168275SEric Cheng mbsize += MBLKL(nmp);
1176621Sbt150084 }
1186621Sbt150084
1197167Sgg161487 if (ixgbe->tx_hcksum_enable) {
1207167Sgg161487 /*
1217167Sgg161487 * Retrieve checksum context information from the mblk
1227167Sgg161487 * that will be used to decide whether/how to fill the
1237167Sgg161487 * context descriptor.
1247167Sgg161487 */
1257167Sgg161487 ctx = &tx_context;
1267167Sgg161487 if (ixgbe_get_context(mp, ctx) < 0) {
1277167Sgg161487 freemsg(mp);
1288275SEric Cheng return (NULL);
1297167Sgg161487 }
1307167Sgg161487
1317167Sgg161487 /*
1327167Sgg161487 * If the mblk size exceeds the max size ixgbe could
1338275SEric Cheng * process, then discard this mblk, and return NULL.
1347167Sgg161487 */
1359353SSamuel.Tu@Sun.COM if ((ctx->lso_flag &&
1369353SSamuel.Tu@Sun.COM ((mbsize - ctx->mac_hdr_len) > IXGBE_LSO_MAXLEN)) ||
1379353SSamuel.Tu@Sun.COM (!ctx->lso_flag &&
1387167Sgg161487 (mbsize > (ixgbe->max_frame_size - ETHERFCSL)))) {
1397167Sgg161487 freemsg(mp);
1407167Sgg161487 IXGBE_DEBUGLOG_0(ixgbe, "ixgbe_tx: packet oversize");
1418275SEric Cheng return (NULL);
1427167Sgg161487 }
1437167Sgg161487 } else {
1447167Sgg161487 ctx = NULL;
1456621Sbt150084 }
1466621Sbt150084
1476621Sbt150084 /*
1486621Sbt150084 * Check and recycle tx descriptors.
1496621Sbt150084 * The recycle threshold here should be selected carefully
1506621Sbt150084 */
15110376SChenlu.Chen@Sun.COM if (tx_ring->tbd_free < ixgbe->tx_recycle_thresh) {
1526621Sbt150084 tx_ring->tx_recycle(tx_ring);
1539353SSamuel.Tu@Sun.COM }
1546621Sbt150084
1556621Sbt150084 /*
1566621Sbt150084 * After the recycling, if the tbd_free is less than the
1578275SEric Cheng * overload_threshold, assert overload, return mp;
1586621Sbt150084 * and we need to re-schedule the tx again.
1596621Sbt150084 */
16010376SChenlu.Chen@Sun.COM if (tx_ring->tbd_free < ixgbe->tx_overload_thresh) {
1616621Sbt150084 tx_ring->reschedule = B_TRUE;
1626621Sbt150084 IXGBE_DEBUG_STAT(tx_ring->stat_overload);
1638275SEric Cheng return (mp);
1646621Sbt150084 }
1656621Sbt150084
1666621Sbt150084 /*
1676621Sbt150084 * The pending_list is a linked list that is used to save
1686621Sbt150084 * the tx control blocks that have packet data processed
1696621Sbt150084 * but have not put the data to the tx descriptor ring.
1706621Sbt150084 * It is used to reduce the lock contention of the tx_lock.
1716621Sbt150084 */
1726621Sbt150084 LINK_LIST_INIT(&pending_list);
1736621Sbt150084 desc_num = 0;
1746621Sbt150084 desc_total = 0;
1756621Sbt150084
1768275SEric Cheng /*
1778275SEric Cheng * The software should guarantee LSO packet header(MAC+IP+TCP)
1788275SEric Cheng * to be within one descriptor. Here we reallocate and refill the
1798275SEric Cheng * the header if it's physical memory non-contiguous.
1808275SEric Cheng */
1818275SEric Cheng if ((ctx != NULL) && ctx->lso_flag) {
1828275SEric Cheng /* find the last fragment of the header */
1838275SEric Cheng len = MBLKL(mp);
1848275SEric Cheng ASSERT(len > 0);
18511150SZhen.W@Sun.COM hdr_nmp = mp;
1868275SEric Cheng hdr_len = ctx->ip_hdr_len + ctx->mac_hdr_len + ctx->l4_hdr_len;
1878275SEric Cheng while (len < hdr_len) {
18811150SZhen.W@Sun.COM hdr_pre_mp = hdr_nmp;
18911150SZhen.W@Sun.COM hdr_nmp = hdr_nmp->b_cont;
19011150SZhen.W@Sun.COM len += MBLKL(hdr_nmp);
1918275SEric Cheng }
1928275SEric Cheng /*
1938275SEric Cheng * If the header and the payload are in different mblks,
1948275SEric Cheng * we simply force the header to be copied into pre-allocated
1958275SEric Cheng * page-aligned buffer.
1968275SEric Cheng */
1978275SEric Cheng if (len == hdr_len)
1988275SEric Cheng goto adjust_threshold;
1998275SEric Cheng
20011150SZhen.W@Sun.COM hdr_frag_len = hdr_len - (len - MBLKL(hdr_nmp));
2018275SEric Cheng /*
2028275SEric Cheng * There are two cases we need to reallocate a mblk for the
2038275SEric Cheng * last header fragment:
2048275SEric Cheng * 1. the header is in multiple mblks and the last fragment
2058275SEric Cheng * share the same mblk with the payload
2068275SEric Cheng * 2. the header is in a single mblk shared with the payload
2078275SEric Cheng * and the header is physical memory non-contiguous
2088275SEric Cheng */
20911150SZhen.W@Sun.COM if ((hdr_nmp != mp) ||
21011150SZhen.W@Sun.COM (P2NPHASE((uintptr_t)hdr_nmp->b_rptr, ixgbe->sys_page_size)
2119353SSamuel.Tu@Sun.COM < hdr_len)) {
2128275SEric Cheng IXGBE_DEBUG_STAT(tx_ring->stat_lso_header_fail);
2138275SEric Cheng /*
2148275SEric Cheng * reallocate the mblk for the last header fragment,
2158275SEric Cheng * expect to bcopy into pre-allocated page-aligned
2168275SEric Cheng * buffer
2178275SEric Cheng */
21811150SZhen.W@Sun.COM hdr_new_mp = allocb(hdr_frag_len, NULL);
21911150SZhen.W@Sun.COM if (!hdr_new_mp)
2209353SSamuel.Tu@Sun.COM return (mp);
22111150SZhen.W@Sun.COM bcopy(hdr_nmp->b_rptr, hdr_new_mp->b_rptr,
22211150SZhen.W@Sun.COM hdr_frag_len);
2238275SEric Cheng /* link the new header fragment with the other parts */
22411150SZhen.W@Sun.COM hdr_new_mp->b_wptr = hdr_new_mp->b_rptr + hdr_frag_len;
22511150SZhen.W@Sun.COM hdr_new_mp->b_cont = hdr_nmp;
22611150SZhen.W@Sun.COM if (hdr_pre_mp)
22711150SZhen.W@Sun.COM hdr_pre_mp->b_cont = hdr_new_mp;
22811150SZhen.W@Sun.COM else
22911150SZhen.W@Sun.COM mp = hdr_new_mp;
23011150SZhen.W@Sun.COM hdr_nmp->b_rptr += hdr_frag_len;
2318275SEric Cheng }
2328275SEric Cheng adjust_threshold:
2338275SEric Cheng /*
2348275SEric Cheng * adjust the bcopy threshhold to guarantee
2358275SEric Cheng * the header to use bcopy way
2368275SEric Cheng */
2378275SEric Cheng if (copy_thresh < hdr_len)
2388275SEric Cheng copy_thresh = hdr_len;
2398275SEric Cheng }
2408275SEric Cheng
2416621Sbt150084 current_mp = mp;
2428275SEric Cheng current_len = MBLKL(current_mp);
2436621Sbt150084 /*
2446621Sbt150084 * Decide which method to use for the first fragment
2456621Sbt150084 */
2468275SEric Cheng current_flag = (current_len <= copy_thresh) ?
2476621Sbt150084 USE_COPY : USE_DMA;
2486621Sbt150084 /*
2496621Sbt150084 * If the mblk includes several contiguous small fragments,
2506621Sbt150084 * they may be copied into one buffer. This flag is used to
2516621Sbt150084 * indicate whether there are pending fragments that need to
2526621Sbt150084 * be copied to the current tx buffer.
2536621Sbt150084 *
2546621Sbt150084 * If this flag is B_TRUE, it indicates that a new tx control
2556621Sbt150084 * block is needed to process the next fragment using either
2566621Sbt150084 * copy or DMA binding.
2576621Sbt150084 *
2586621Sbt150084 * Otherwise, it indicates that the next fragment will be
2596621Sbt150084 * copied to the current tx buffer that is maintained by the
2606621Sbt150084 * current tx control block. No new tx control block is needed.
2616621Sbt150084 */
2626621Sbt150084 copy_done = B_TRUE;
2636621Sbt150084 while (current_mp) {
2646621Sbt150084 next_mp = current_mp->b_cont;
2656621Sbt150084 eop = (next_mp == NULL); /* Last fragment of the packet? */
2668275SEric Cheng next_len = eop ? 0: MBLKL(next_mp);
2676621Sbt150084
2686621Sbt150084 /*
2696621Sbt150084 * When the current fragment is an empty fragment, if
2706621Sbt150084 * the next fragment will still be copied to the current
2716621Sbt150084 * tx buffer, we cannot skip this fragment here. Because
2726621Sbt150084 * the copy processing is pending for completion. We have
2736621Sbt150084 * to process this empty fragment in the tx_copy routine.
2746621Sbt150084 *
2756621Sbt150084 * If the copy processing is completed or a DMA binding
2766621Sbt150084 * processing is just completed, we can just skip this
2776621Sbt150084 * empty fragment.
2786621Sbt150084 */
2796621Sbt150084 if ((current_len == 0) && (copy_done)) {
2806621Sbt150084 current_mp = next_mp;
2816621Sbt150084 current_len = next_len;
2828275SEric Cheng current_flag = (current_len <= copy_thresh) ?
2836621Sbt150084 USE_COPY : USE_DMA;
2846621Sbt150084 continue;
2856621Sbt150084 }
2866621Sbt150084
2876621Sbt150084 if (copy_done) {
2886621Sbt150084 /*
2896621Sbt150084 * Get a new tx control block from the free list
2906621Sbt150084 */
2916621Sbt150084 tcb = ixgbe_get_free_list(tx_ring);
2926621Sbt150084
2936621Sbt150084 if (tcb == NULL) {
2946621Sbt150084 IXGBE_DEBUG_STAT(tx_ring->stat_fail_no_tcb);
2956621Sbt150084 goto tx_failure;
2966621Sbt150084 }
2976621Sbt150084
2986621Sbt150084 /*
2996621Sbt150084 * Push the tx control block to the pending list
3006621Sbt150084 * to avoid using lock too early
3016621Sbt150084 */
3026621Sbt150084 LIST_PUSH_TAIL(&pending_list, &tcb->link);
3036621Sbt150084 }
3046621Sbt150084
3056621Sbt150084 if (current_flag == USE_COPY) {
3066621Sbt150084 /*
3076621Sbt150084 * Check whether to use bcopy or DMA binding to process
3086621Sbt150084 * the next fragment, and if using bcopy, whether we
3096621Sbt150084 * need to continue copying the next fragment into the
3106621Sbt150084 * current tx buffer.
3116621Sbt150084 */
3126621Sbt150084 ASSERT((tcb->tx_buf.len + current_len) <=
3136621Sbt150084 tcb->tx_buf.size);
3146621Sbt150084
3156621Sbt150084 if (eop) {
3166621Sbt150084 /*
3176621Sbt150084 * This is the last fragment of the packet, so
3186621Sbt150084 * the copy processing will be completed with
3196621Sbt150084 * this fragment.
3206621Sbt150084 */
3216621Sbt150084 next_flag = USE_NONE;
3226621Sbt150084 copy_done = B_TRUE;
3236621Sbt150084 } else if ((tcb->tx_buf.len + current_len + next_len) >
3246621Sbt150084 tcb->tx_buf.size) {
3256621Sbt150084 /*
3266621Sbt150084 * If the next fragment is too large to be
3276621Sbt150084 * copied to the current tx buffer, we need
3286621Sbt150084 * to complete the current copy processing.
3296621Sbt150084 */
3308275SEric Cheng next_flag = (next_len > copy_thresh) ?
3316621Sbt150084 USE_DMA: USE_COPY;
3326621Sbt150084 copy_done = B_TRUE;
3338275SEric Cheng } else if (next_len > copy_thresh) {
3346621Sbt150084 /*
3356621Sbt150084 * The next fragment needs to be processed with
3366621Sbt150084 * DMA binding. So the copy prcessing will be
3376621Sbt150084 * completed with the current fragment.
3386621Sbt150084 */
3396621Sbt150084 next_flag = USE_DMA;
3406621Sbt150084 copy_done = B_TRUE;
3416621Sbt150084 } else {
3426621Sbt150084 /*
3436621Sbt150084 * Continue to copy the next fragment to the
3446621Sbt150084 * current tx buffer.
3456621Sbt150084 */
3466621Sbt150084 next_flag = USE_COPY;
3476621Sbt150084 copy_done = B_FALSE;
3486621Sbt150084 }
3496621Sbt150084
3506621Sbt150084 desc_num = ixgbe_tx_copy(tx_ring, tcb, current_mp,
3517167Sgg161487 current_len, copy_done);
3526621Sbt150084 } else {
3536621Sbt150084 /*
3546621Sbt150084 * Check whether to use bcopy or DMA binding to process
3556621Sbt150084 * the next fragment.
3566621Sbt150084 */
3578275SEric Cheng next_flag = (next_len > copy_thresh) ?
3586621Sbt150084 USE_DMA: USE_COPY;
3596621Sbt150084 ASSERT(copy_done == B_TRUE);
3606621Sbt150084
3616621Sbt150084 desc_num = ixgbe_tx_bind(tx_ring, tcb, current_mp,
3626621Sbt150084 current_len);
3636621Sbt150084 }
3646621Sbt150084
3656621Sbt150084 if (desc_num > 0)
3666621Sbt150084 desc_total += desc_num;
3676621Sbt150084 else if (desc_num < 0)
3686621Sbt150084 goto tx_failure;
3696621Sbt150084
3706621Sbt150084 current_mp = next_mp;
3716621Sbt150084 current_len = next_len;
3726621Sbt150084 current_flag = next_flag;
3736621Sbt150084 }
3746621Sbt150084
3756621Sbt150084 /*
3766621Sbt150084 * Attach the mblk to the last tx control block
3776621Sbt150084 */
3786621Sbt150084 ASSERT(tcb);
3796621Sbt150084 ASSERT(tcb->mp == NULL);
3806621Sbt150084 tcb->mp = mp;
3816621Sbt150084
3826621Sbt150084 /*
3839681SPaul.Guo@Sun.COM * 82598/82599 chipset has a limitation that no more than 32 tx
3849681SPaul.Guo@Sun.COM * descriptors can be transmited out at one time.
3859681SPaul.Guo@Sun.COM *
3869681SPaul.Guo@Sun.COM * Here is a workaround for it: pull up the mblk then send it
3879681SPaul.Guo@Sun.COM * out with bind way. By doing so, no more than MAX_COOKIE (18)
3889681SPaul.Guo@Sun.COM * descriptors is needed.
3899681SPaul.Guo@Sun.COM */
3909681SPaul.Guo@Sun.COM if (desc_total + 1 > IXGBE_TX_DESC_LIMIT) {
3919681SPaul.Guo@Sun.COM IXGBE_DEBUG_STAT(tx_ring->stat_break_tbd_limit);
3929681SPaul.Guo@Sun.COM
3939681SPaul.Guo@Sun.COM /*
3949681SPaul.Guo@Sun.COM * Discard the mblk and free the used resources
3959681SPaul.Guo@Sun.COM */
3969681SPaul.Guo@Sun.COM tcb = (tx_control_block_t *)LIST_GET_HEAD(&pending_list);
3979681SPaul.Guo@Sun.COM while (tcb) {
3989681SPaul.Guo@Sun.COM tcb->mp = NULL;
3999681SPaul.Guo@Sun.COM ixgbe_free_tcb(tcb);
4009681SPaul.Guo@Sun.COM tcb = (tx_control_block_t *)
4019681SPaul.Guo@Sun.COM LIST_GET_NEXT(&pending_list, &tcb->link);
4029681SPaul.Guo@Sun.COM }
4039681SPaul.Guo@Sun.COM
4049681SPaul.Guo@Sun.COM /*
4059681SPaul.Guo@Sun.COM * Return the tx control blocks in the pending list to
4069681SPaul.Guo@Sun.COM * the free list.
4079681SPaul.Guo@Sun.COM */
4089681SPaul.Guo@Sun.COM ixgbe_put_free_list(tx_ring, &pending_list);
4099681SPaul.Guo@Sun.COM
4109681SPaul.Guo@Sun.COM /*
4119681SPaul.Guo@Sun.COM * pull up the mblk and send it out with bind way
4129681SPaul.Guo@Sun.COM */
41310305SPaul.Guo@Sun.COM if ((pull_mp = msgpullup(mp, -1)) == NULL) {
41410305SPaul.Guo@Sun.COM tx_ring->reschedule = B_TRUE;
41511150SZhen.W@Sun.COM
41611150SZhen.W@Sun.COM /*
41711150SZhen.W@Sun.COM * If new mblk has been allocted for the last header
41811150SZhen.W@Sun.COM * fragment of a LSO packet, we should restore the
41911150SZhen.W@Sun.COM * modified mp.
42011150SZhen.W@Sun.COM */
42111150SZhen.W@Sun.COM if (hdr_new_mp) {
42211150SZhen.W@Sun.COM hdr_new_mp->b_cont = NULL;
42311150SZhen.W@Sun.COM freeb(hdr_new_mp);
42411150SZhen.W@Sun.COM hdr_nmp->b_rptr -= hdr_frag_len;
42511150SZhen.W@Sun.COM if (hdr_pre_mp)
42611150SZhen.W@Sun.COM hdr_pre_mp->b_cont = hdr_nmp;
42711150SZhen.W@Sun.COM else
42811150SZhen.W@Sun.COM mp = hdr_nmp;
42911150SZhen.W@Sun.COM }
43010305SPaul.Guo@Sun.COM return (mp);
4319681SPaul.Guo@Sun.COM }
4329681SPaul.Guo@Sun.COM
4339681SPaul.Guo@Sun.COM LINK_LIST_INIT(&pending_list);
43410305SPaul.Guo@Sun.COM desc_total = 0;
43510305SPaul.Guo@Sun.COM
43610305SPaul.Guo@Sun.COM /*
43710305SPaul.Guo@Sun.COM * if the packet is a LSO packet, we simply
43810305SPaul.Guo@Sun.COM * transmit the header in one descriptor using the copy way
43910305SPaul.Guo@Sun.COM */
44010305SPaul.Guo@Sun.COM if ((ctx != NULL) && ctx->lso_flag) {
44110305SPaul.Guo@Sun.COM hdr_len = ctx->ip_hdr_len + ctx->mac_hdr_len +
44210305SPaul.Guo@Sun.COM ctx->l4_hdr_len;
44310305SPaul.Guo@Sun.COM
44410305SPaul.Guo@Sun.COM tcb = ixgbe_get_free_list(tx_ring);
44510305SPaul.Guo@Sun.COM if (tcb == NULL) {
44610305SPaul.Guo@Sun.COM IXGBE_DEBUG_STAT(tx_ring->stat_fail_no_tcb);
44710305SPaul.Guo@Sun.COM goto tx_failure;
44810305SPaul.Guo@Sun.COM }
44910305SPaul.Guo@Sun.COM desc_num = ixgbe_tx_copy(tx_ring, tcb, pull_mp,
45010305SPaul.Guo@Sun.COM hdr_len, B_TRUE);
45110305SPaul.Guo@Sun.COM LIST_PUSH_TAIL(&pending_list, &tcb->link);
45210305SPaul.Guo@Sun.COM desc_total += desc_num;
45310305SPaul.Guo@Sun.COM
45410305SPaul.Guo@Sun.COM pull_mp->b_rptr += hdr_len;
45510305SPaul.Guo@Sun.COM }
45610305SPaul.Guo@Sun.COM
4579681SPaul.Guo@Sun.COM tcb = ixgbe_get_free_list(tx_ring);
4589681SPaul.Guo@Sun.COM if (tcb == NULL) {
4599681SPaul.Guo@Sun.COM IXGBE_DEBUG_STAT(tx_ring->stat_fail_no_tcb);
46010305SPaul.Guo@Sun.COM goto tx_failure;
46110305SPaul.Guo@Sun.COM }
46210305SPaul.Guo@Sun.COM if ((ctx != NULL) && ctx->lso_flag) {
46310305SPaul.Guo@Sun.COM desc_num = ixgbe_tx_bind(tx_ring, tcb, pull_mp,
46410305SPaul.Guo@Sun.COM mbsize - hdr_len);
46510305SPaul.Guo@Sun.COM } else {
46610305SPaul.Guo@Sun.COM desc_num = ixgbe_tx_bind(tx_ring, tcb, pull_mp,
46710305SPaul.Guo@Sun.COM mbsize);
46810305SPaul.Guo@Sun.COM }
46910305SPaul.Guo@Sun.COM if (desc_num < 0) {
47010305SPaul.Guo@Sun.COM goto tx_failure;
4719681SPaul.Guo@Sun.COM }
4729681SPaul.Guo@Sun.COM LIST_PUSH_TAIL(&pending_list, &tcb->link);
4739681SPaul.Guo@Sun.COM
47410305SPaul.Guo@Sun.COM desc_total += desc_num;
47510305SPaul.Guo@Sun.COM tcb->mp = pull_mp;
4769681SPaul.Guo@Sun.COM }
4779681SPaul.Guo@Sun.COM
4789681SPaul.Guo@Sun.COM /*
4796621Sbt150084 * Before fill the tx descriptor ring with the data, we need to
4806621Sbt150084 * ensure there are adequate free descriptors for transmit
4816621Sbt150084 * (including one context descriptor).
482*13006SChenlu.Chen@Sun.COM * Do not use up all the tx descriptors.
483*13006SChenlu.Chen@Sun.COM * Otherwise tx recycle will fail and cause false hang.
4846621Sbt150084 */
485*13006SChenlu.Chen@Sun.COM if (tx_ring->tbd_free <= (desc_total + 1)) {
4866621Sbt150084 tx_ring->tx_recycle(tx_ring);
4876621Sbt150084 }
4886621Sbt150084
4896621Sbt150084 mutex_enter(&tx_ring->tx_lock);
4906621Sbt150084 /*
4916621Sbt150084 * If the number of free tx descriptors is not enough for transmit
4928275SEric Cheng * then return mp.
4936621Sbt150084 *
4946621Sbt150084 * Note: we must put this check under the mutex protection to
4956621Sbt150084 * ensure the correctness when multiple threads access it in
4966621Sbt150084 * parallel.
4976621Sbt150084 */
498*13006SChenlu.Chen@Sun.COM if (tx_ring->tbd_free <= (desc_total + 1)) {
4996621Sbt150084 IXGBE_DEBUG_STAT(tx_ring->stat_fail_no_tbd);
5006621Sbt150084 mutex_exit(&tx_ring->tx_lock);
5016621Sbt150084 goto tx_failure;
5026621Sbt150084 }
5036621Sbt150084
5047167Sgg161487 desc_num = ixgbe_tx_fill_ring(tx_ring, &pending_list, ctx,
5057167Sgg161487 mbsize);
5066621Sbt150084
5076621Sbt150084 ASSERT((desc_num == desc_total) || (desc_num == (desc_total + 1)));
5086621Sbt150084
50911878SVenu.Iyer@Sun.COM tx_ring->stat_obytes += mbsize;
51011878SVenu.Iyer@Sun.COM tx_ring->stat_opackets ++;
51111878SVenu.Iyer@Sun.COM
5126621Sbt150084 mutex_exit(&tx_ring->tx_lock);
5136621Sbt150084
51410305SPaul.Guo@Sun.COM /*
51510305SPaul.Guo@Sun.COM * now that the transmission succeeds, need to free the original
51610305SPaul.Guo@Sun.COM * mp if we used the pulling up mblk for transmission.
51710305SPaul.Guo@Sun.COM */
51810305SPaul.Guo@Sun.COM if (pull_mp) {
51910305SPaul.Guo@Sun.COM freemsg(mp);
52010305SPaul.Guo@Sun.COM }
52110305SPaul.Guo@Sun.COM
5228275SEric Cheng return (NULL);
5236621Sbt150084
5246621Sbt150084 tx_failure:
5256621Sbt150084 /*
52610305SPaul.Guo@Sun.COM * If transmission fails, need to free the pulling up mblk.
52710305SPaul.Guo@Sun.COM */
52810305SPaul.Guo@Sun.COM if (pull_mp) {
52910305SPaul.Guo@Sun.COM freemsg(pull_mp);
53010305SPaul.Guo@Sun.COM }
53110305SPaul.Guo@Sun.COM
53210305SPaul.Guo@Sun.COM /*
53311150SZhen.W@Sun.COM * If new mblk has been allocted for the last header
53411150SZhen.W@Sun.COM * fragment of a LSO packet, we should restore the
53511150SZhen.W@Sun.COM * modified mp.
53611150SZhen.W@Sun.COM */
53711150SZhen.W@Sun.COM if (hdr_new_mp) {
53811150SZhen.W@Sun.COM hdr_new_mp->b_cont = NULL;
53911150SZhen.W@Sun.COM freeb(hdr_new_mp);
54011150SZhen.W@Sun.COM hdr_nmp->b_rptr -= hdr_frag_len;
54111150SZhen.W@Sun.COM if (hdr_pre_mp)
54211150SZhen.W@Sun.COM hdr_pre_mp->b_cont = hdr_nmp;
54311150SZhen.W@Sun.COM else
54411150SZhen.W@Sun.COM mp = hdr_nmp;
54511150SZhen.W@Sun.COM }
54611150SZhen.W@Sun.COM /*
5476621Sbt150084 * Discard the mblk and free the used resources
5486621Sbt150084 */
5496621Sbt150084 tcb = (tx_control_block_t *)LIST_GET_HEAD(&pending_list);
5506621Sbt150084 while (tcb) {
5516621Sbt150084 tcb->mp = NULL;
5526621Sbt150084
5536621Sbt150084 ixgbe_free_tcb(tcb);
5546621Sbt150084
5556621Sbt150084 tcb = (tx_control_block_t *)
5566621Sbt150084 LIST_GET_NEXT(&pending_list, &tcb->link);
5576621Sbt150084 }
5586621Sbt150084
5596621Sbt150084 /*
5606621Sbt150084 * Return the tx control blocks in the pending list to the free list.
5616621Sbt150084 */
5626621Sbt150084 ixgbe_put_free_list(tx_ring, &pending_list);
5636621Sbt150084
5646621Sbt150084 /* Transmit failed, do not drop the mblk, rechedule the transmit */
5656621Sbt150084 tx_ring->reschedule = B_TRUE;
5666621Sbt150084
5678275SEric Cheng return (mp);
5686621Sbt150084 }
5696621Sbt150084
5706621Sbt150084 /*
5716621Sbt150084 * ixgbe_tx_copy
5726621Sbt150084 *
5736621Sbt150084 * Copy the mblk fragment to the pre-allocated tx buffer
5746621Sbt150084 */
5756621Sbt150084 static int
ixgbe_tx_copy(ixgbe_tx_ring_t * tx_ring,tx_control_block_t * tcb,mblk_t * mp,uint32_t len,boolean_t copy_done)5766621Sbt150084 ixgbe_tx_copy(ixgbe_tx_ring_t *tx_ring, tx_control_block_t *tcb, mblk_t *mp,
5777167Sgg161487 uint32_t len, boolean_t copy_done)
5786621Sbt150084 {
5796621Sbt150084 dma_buffer_t *tx_buf;
5806621Sbt150084 uint32_t desc_num;
5816621Sbt150084 _NOTE(ARGUNUSED(tx_ring));
5826621Sbt150084
5836621Sbt150084 tx_buf = &tcb->tx_buf;
5846621Sbt150084
5856621Sbt150084 /*
5866621Sbt150084 * Copy the packet data of the mblk fragment into the
5876621Sbt150084 * pre-allocated tx buffer, which is maintained by the
5886621Sbt150084 * tx control block.
5896621Sbt150084 *
5906621Sbt150084 * Several mblk fragments can be copied into one tx buffer.
5916621Sbt150084 * The destination address of the current copied fragment in
5926621Sbt150084 * the tx buffer is next to the end of the previous copied
5936621Sbt150084 * fragment.
5946621Sbt150084 */
5956621Sbt150084 if (len > 0) {
5966621Sbt150084 bcopy(mp->b_rptr, tx_buf->address + tx_buf->len, len);
5976621Sbt150084
5986621Sbt150084 tx_buf->len += len;
5996621Sbt150084 tcb->frag_num++;
6006621Sbt150084 }
6016621Sbt150084
6026621Sbt150084 desc_num = 0;
6036621Sbt150084
6046621Sbt150084 /*
6056621Sbt150084 * If it is the last fragment copied to the current tx buffer,
6066621Sbt150084 * in other words, if there's no remaining fragment or the remaining
6076621Sbt150084 * fragment requires a new tx control block to process, we need to
6086621Sbt150084 * complete the current copy processing by syncing up the current
6096621Sbt150084 * DMA buffer and saving the descriptor data.
6106621Sbt150084 */
6116621Sbt150084 if (copy_done) {
6126621Sbt150084 /*
6136621Sbt150084 * Sync the DMA buffer of the packet data
6146621Sbt150084 */
6156621Sbt150084 DMA_SYNC(tx_buf, DDI_DMA_SYNC_FORDEV);
6166621Sbt150084
6176621Sbt150084 tcb->tx_type = USE_COPY;
6186621Sbt150084
6196621Sbt150084 /*
6206621Sbt150084 * Save the address and length to the private data structure
6216621Sbt150084 * of the tx control block, which will be used to fill the
6226621Sbt150084 * tx descriptor ring after all the fragments are processed.
6236621Sbt150084 */
6246621Sbt150084 ixgbe_save_desc(tcb, tx_buf->dma_address, tx_buf->len);
6256621Sbt150084 desc_num++;
6266621Sbt150084 }
6276621Sbt150084
6286621Sbt150084 return (desc_num);
6296621Sbt150084 }
6306621Sbt150084
6316621Sbt150084 /*
6326621Sbt150084 * ixgbe_tx_bind
6336621Sbt150084 *
6346621Sbt150084 * Bind the mblk fragment with DMA
6356621Sbt150084 */
6366621Sbt150084 static int
ixgbe_tx_bind(ixgbe_tx_ring_t * tx_ring,tx_control_block_t * tcb,mblk_t * mp,uint32_t len)6376621Sbt150084 ixgbe_tx_bind(ixgbe_tx_ring_t *tx_ring, tx_control_block_t *tcb, mblk_t *mp,
6386621Sbt150084 uint32_t len)
6396621Sbt150084 {
6406621Sbt150084 int status, i;
6416621Sbt150084 ddi_dma_cookie_t dma_cookie;
6426621Sbt150084 uint_t ncookies;
6436621Sbt150084 int desc_num;
6446621Sbt150084
6456621Sbt150084 /*
6466621Sbt150084 * Use DMA binding to process the mblk fragment
6476621Sbt150084 */
6486621Sbt150084 status = ddi_dma_addr_bind_handle(tcb->tx_dma_handle, NULL,
6496621Sbt150084 (caddr_t)mp->b_rptr, len,
6506621Sbt150084 DDI_DMA_WRITE | DDI_DMA_STREAMING, DDI_DMA_DONTWAIT,
6516621Sbt150084 0, &dma_cookie, &ncookies);
6526621Sbt150084
6536621Sbt150084 if (status != DDI_DMA_MAPPED) {
6546621Sbt150084 IXGBE_DEBUG_STAT(tx_ring->stat_fail_dma_bind);
6556621Sbt150084 return (-1);
6566621Sbt150084 }
6576621Sbt150084
6586621Sbt150084 tcb->frag_num++;
6596621Sbt150084 tcb->tx_type = USE_DMA;
6606621Sbt150084 /*
6616621Sbt150084 * Each fragment can span several cookies. One cookie will have
6626621Sbt150084 * one tx descriptor to transmit.
6636621Sbt150084 */
6646621Sbt150084 desc_num = 0;
6656621Sbt150084 for (i = ncookies; i > 0; i--) {
6666621Sbt150084 /*
6676621Sbt150084 * Save the address and length to the private data structure
6686621Sbt150084 * of the tx control block, which will be used to fill the
6696621Sbt150084 * tx descriptor ring after all the fragments are processed.
6706621Sbt150084 */
6716621Sbt150084 ixgbe_save_desc(tcb,
6726621Sbt150084 dma_cookie.dmac_laddress,
6736621Sbt150084 dma_cookie.dmac_size);
6746621Sbt150084
6756621Sbt150084 desc_num++;
6766621Sbt150084
6776621Sbt150084 if (i > 1)
6786621Sbt150084 ddi_dma_nextcookie(tcb->tx_dma_handle, &dma_cookie);
6796621Sbt150084 }
6806621Sbt150084
6816621Sbt150084 return (desc_num);
6826621Sbt150084 }
6836621Sbt150084
6846621Sbt150084 /*
6857167Sgg161487 * ixgbe_get_context
6866621Sbt150084 *
6877167Sgg161487 * Get the context information from the mblk
6886621Sbt150084 */
6897167Sgg161487 static int
ixgbe_get_context(mblk_t * mp,ixgbe_tx_context_t * ctx)6907167Sgg161487 ixgbe_get_context(mblk_t *mp, ixgbe_tx_context_t *ctx)
6916621Sbt150084 {
6926621Sbt150084 uint32_t start;
6938275SEric Cheng uint32_t hckflags;
6948275SEric Cheng uint32_t lsoflags;
6958275SEric Cheng uint32_t mss;
6966621Sbt150084 uint32_t len;
6976621Sbt150084 uint32_t size;
6986621Sbt150084 uint32_t offset;
6996621Sbt150084 unsigned char *pos;
7006621Sbt150084 ushort_t etype;
7016621Sbt150084 uint32_t mac_hdr_len;
7026621Sbt150084 uint32_t l4_proto;
7037167Sgg161487 uint32_t l4_hdr_len;
7046621Sbt150084
7056621Sbt150084 ASSERT(mp != NULL);
7066621Sbt150084
70711878SVenu.Iyer@Sun.COM mac_hcksum_get(mp, &start, NULL, NULL, NULL, &hckflags);
7087167Sgg161487 bzero(ctx, sizeof (ixgbe_tx_context_t));
7096621Sbt150084
7109353SSamuel.Tu@Sun.COM if (hckflags == 0) {
7117167Sgg161487 return (0);
7129353SSamuel.Tu@Sun.COM }
7139353SSamuel.Tu@Sun.COM
7148275SEric Cheng ctx->hcksum_flags = hckflags;
7157167Sgg161487
71611878SVenu.Iyer@Sun.COM mac_lso_get(mp, &mss, &lsoflags);
7178275SEric Cheng ctx->mss = mss;
7188275SEric Cheng ctx->lso_flag = (lsoflags == HW_LSO);
7197167Sgg161487
7207167Sgg161487 /*
7217167Sgg161487 * LSO relies on tx h/w checksum, so here will drop the package
7227167Sgg161487 * if h/w checksum flag is not declared.
7237167Sgg161487 */
7247167Sgg161487 if (ctx->lso_flag) {
7257167Sgg161487 if (!((ctx->hcksum_flags & HCK_PARTIALCKSUM) &&
7267167Sgg161487 (ctx->hcksum_flags & HCK_IPV4_HDRCKSUM))) {
7277167Sgg161487 IXGBE_DEBUGLOG_0(NULL, "ixgbe_tx: h/w "
7287167Sgg161487 "checksum flags are not specified when doing LSO");
7297167Sgg161487 return (-1);
7307167Sgg161487 }
7317167Sgg161487 }
7326621Sbt150084
7336621Sbt150084 etype = 0;
7346621Sbt150084 mac_hdr_len = 0;
7356621Sbt150084 l4_proto = 0;
7366621Sbt150084
7376621Sbt150084 /*
7386621Sbt150084 * Firstly get the position of the ether_type/ether_tpid.
7396621Sbt150084 * Here we don't assume the ether (VLAN) header is fully included
7406621Sbt150084 * in one mblk fragment, so we go thourgh the fragments to parse
7416621Sbt150084 * the ether type.
7426621Sbt150084 */
7438275SEric Cheng size = len = MBLKL(mp);
7446621Sbt150084 offset = offsetof(struct ether_header, ether_type);
7456621Sbt150084 while (size <= offset) {
7466621Sbt150084 mp = mp->b_cont;
7476621Sbt150084 ASSERT(mp != NULL);
7488275SEric Cheng len = MBLKL(mp);
7496621Sbt150084 size += len;
7506621Sbt150084 }
7516621Sbt150084 pos = mp->b_rptr + offset + len - size;
7526621Sbt150084
7536621Sbt150084 etype = ntohs(*(ushort_t *)(uintptr_t)pos);
7546621Sbt150084 if (etype == ETHERTYPE_VLAN) {
7556621Sbt150084 /*
7566621Sbt150084 * Get the position of the ether_type in VLAN header
7576621Sbt150084 */
7586621Sbt150084 offset = offsetof(struct ether_vlan_header, ether_type);
7596621Sbt150084 while (size <= offset) {
7606621Sbt150084 mp = mp->b_cont;
7616621Sbt150084 ASSERT(mp != NULL);
7628275SEric Cheng len = MBLKL(mp);
7636621Sbt150084 size += len;
7646621Sbt150084 }
7656621Sbt150084 pos = mp->b_rptr + offset + len - size;
7666621Sbt150084
7676621Sbt150084 etype = ntohs(*(ushort_t *)(uintptr_t)pos);
7686621Sbt150084 mac_hdr_len = sizeof (struct ether_vlan_header);
7696621Sbt150084 } else {
7706621Sbt150084 mac_hdr_len = sizeof (struct ether_header);
7716621Sbt150084 }
7726621Sbt150084
7736621Sbt150084 /*
7748275SEric Cheng * Here we don't assume the IP(V6) header is fully included in
7757167Sgg161487 * one mblk fragment.
7766621Sbt150084 */
7776621Sbt150084 switch (etype) {
7786621Sbt150084 case ETHERTYPE_IP:
7798275SEric Cheng if (ctx->lso_flag) {
7808275SEric Cheng offset = offsetof(ipha_t, ipha_length) + mac_hdr_len;
7818275SEric Cheng while (size <= offset) {
7828275SEric Cheng mp = mp->b_cont;
7838275SEric Cheng ASSERT(mp != NULL);
7848275SEric Cheng len = MBLKL(mp);
7858275SEric Cheng size += len;
7868275SEric Cheng }
7878275SEric Cheng pos = mp->b_rptr + offset + len - size;
7888275SEric Cheng *((uint16_t *)(uintptr_t)(pos)) = 0;
7896621Sbt150084
7908275SEric Cheng offset = offsetof(ipha_t, ipha_hdr_checksum) +
7918275SEric Cheng mac_hdr_len;
7928275SEric Cheng while (size <= offset) {
7938275SEric Cheng mp = mp->b_cont;
7948275SEric Cheng ASSERT(mp != NULL);
7958275SEric Cheng len = MBLKL(mp);
7968275SEric Cheng size += len;
7978275SEric Cheng }
7988275SEric Cheng pos = mp->b_rptr + offset + len - size;
7998275SEric Cheng *((uint16_t *)(uintptr_t)(pos)) = 0;
8007167Sgg161487
8017167Sgg161487 /*
8027167Sgg161487 * To perform ixgbe LSO, here also need to fill
8037167Sgg161487 * the tcp checksum field of the packet with the
8047167Sgg161487 * following pseudo-header checksum:
8057167Sgg161487 * (ip_source_addr, ip_destination_addr, l4_proto)
8067167Sgg161487 * Currently the tcp/ip stack has done it.
8077167Sgg161487 */
8087167Sgg161487 }
8097167Sgg161487
8108275SEric Cheng offset = offsetof(ipha_t, ipha_protocol) + mac_hdr_len;
8118275SEric Cheng while (size <= offset) {
8128275SEric Cheng mp = mp->b_cont;
8138275SEric Cheng ASSERT(mp != NULL);
8148275SEric Cheng len = MBLKL(mp);
8158275SEric Cheng size += len;
8168275SEric Cheng }
8178275SEric Cheng pos = mp->b_rptr + offset + len - size;
8188275SEric Cheng
8198275SEric Cheng l4_proto = *(uint8_t *)pos;
8206621Sbt150084 break;
8216621Sbt150084 case ETHERTYPE_IPV6:
8226621Sbt150084 offset = offsetof(ip6_t, ip6_nxt) + mac_hdr_len;
8236621Sbt150084 while (size <= offset) {
8246621Sbt150084 mp = mp->b_cont;
8256621Sbt150084 ASSERT(mp != NULL);
8268275SEric Cheng len = MBLKL(mp);
8276621Sbt150084 size += len;
8286621Sbt150084 }
8296621Sbt150084 pos = mp->b_rptr + offset + len - size;
8306621Sbt150084
8316621Sbt150084 l4_proto = *(uint8_t *)pos;
8326621Sbt150084 break;
8336621Sbt150084 default:
8346621Sbt150084 /* Unrecoverable error */
8356621Sbt150084 IXGBE_DEBUGLOG_0(NULL, "Ether type error with tx hcksum");
8367167Sgg161487 return (-2);
8376621Sbt150084 }
8386621Sbt150084
8397167Sgg161487 if (ctx->lso_flag) {
8407167Sgg161487 offset = mac_hdr_len + start;
8417167Sgg161487 while (size <= offset) {
8427167Sgg161487 mp = mp->b_cont;
8437167Sgg161487 ASSERT(mp != NULL);
8448275SEric Cheng len = MBLKL(mp);
8457167Sgg161487 size += len;
8467167Sgg161487 }
8477167Sgg161487 pos = mp->b_rptr + offset + len - size;
8487167Sgg161487
8497167Sgg161487 l4_hdr_len = TCP_HDR_LENGTH((tcph_t *)pos);
8507167Sgg161487 } else {
8517167Sgg161487 /*
8527167Sgg161487 * l4 header length is only required for LSO
8537167Sgg161487 */
8547167Sgg161487 l4_hdr_len = 0;
8557167Sgg161487 }
8567167Sgg161487
8577167Sgg161487 ctx->mac_hdr_len = mac_hdr_len;
8587167Sgg161487 ctx->ip_hdr_len = start;
8597167Sgg161487 ctx->l4_proto = l4_proto;
8607167Sgg161487 ctx->l4_hdr_len = l4_hdr_len;
8617167Sgg161487
8627167Sgg161487 return (0);
8636621Sbt150084 }
8646621Sbt150084
8656621Sbt150084 /*
8667167Sgg161487 * ixgbe_check_context
8676621Sbt150084 *
8686621Sbt150084 * Check if a new context descriptor is needed
8696621Sbt150084 */
8706621Sbt150084 static boolean_t
ixgbe_check_context(ixgbe_tx_ring_t * tx_ring,ixgbe_tx_context_t * ctx)8717167Sgg161487 ixgbe_check_context(ixgbe_tx_ring_t *tx_ring, ixgbe_tx_context_t *ctx)
8726621Sbt150084 {
8737167Sgg161487 ixgbe_tx_context_t *last;
8746621Sbt150084
8757167Sgg161487 if (ctx == NULL)
8766621Sbt150084 return (B_FALSE);
8776621Sbt150084
8786621Sbt150084 /*
8798275SEric Cheng * Compare the context data retrieved from the mblk and the
8808275SEric Cheng * stored data of the last context descriptor. The data need
8818275SEric Cheng * to be checked are:
8826621Sbt150084 * hcksum_flags
8836621Sbt150084 * l4_proto
8846621Sbt150084 * mac_hdr_len
8856621Sbt150084 * ip_hdr_len
8868275SEric Cheng * lso_flag
8877167Sgg161487 * mss (only checked for LSO)
8887167Sgg161487 * l4_hr_len (only checked for LSO)
8896621Sbt150084 * Either one of the above data is changed, a new context descriptor
8906621Sbt150084 * will be needed.
8916621Sbt150084 */
8927167Sgg161487 last = &tx_ring->tx_context;
8936621Sbt150084
8948275SEric Cheng if ((ctx->hcksum_flags != last->hcksum_flags) ||
8958275SEric Cheng (ctx->l4_proto != last->l4_proto) ||
8968275SEric Cheng (ctx->mac_hdr_len != last->mac_hdr_len) ||
8978275SEric Cheng (ctx->ip_hdr_len != last->ip_hdr_len) ||
8988275SEric Cheng (ctx->lso_flag != last->lso_flag) ||
8998275SEric Cheng (ctx->lso_flag && ((ctx->mss != last->mss) ||
9008275SEric Cheng (ctx->l4_hdr_len != last->l4_hdr_len)))) {
9018275SEric Cheng return (B_TRUE);
9026621Sbt150084 }
9036621Sbt150084
9046621Sbt150084 return (B_FALSE);
9056621Sbt150084 }
9066621Sbt150084
9076621Sbt150084 /*
9087167Sgg161487 * ixgbe_fill_context
9096621Sbt150084 *
9106621Sbt150084 * Fill the context descriptor with hardware checksum informations
9116621Sbt150084 */
9126621Sbt150084 static void
ixgbe_fill_context(struct ixgbe_adv_tx_context_desc * ctx_tbd,ixgbe_tx_context_t * ctx)9137167Sgg161487 ixgbe_fill_context(struct ixgbe_adv_tx_context_desc *ctx_tbd,
9149353SSamuel.Tu@Sun.COM ixgbe_tx_context_t *ctx)
9156621Sbt150084 {
9166621Sbt150084 /*
9176621Sbt150084 * Fill the context descriptor with the checksum
9188275SEric Cheng * context information we've got.
9196621Sbt150084 */
9207167Sgg161487 ctx_tbd->vlan_macip_lens = ctx->ip_hdr_len;
9217167Sgg161487 ctx_tbd->vlan_macip_lens |= ctx->mac_hdr_len <<
9226621Sbt150084 IXGBE_ADVTXD_MACLEN_SHIFT;
9236621Sbt150084
9246621Sbt150084 ctx_tbd->type_tucmd_mlhl =
9256621Sbt150084 IXGBE_ADVTXD_DCMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
9266621Sbt150084
9277167Sgg161487 if (ctx->hcksum_flags & HCK_IPV4_HDRCKSUM)
9286621Sbt150084 ctx_tbd->type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
9296621Sbt150084
9307167Sgg161487 if (ctx->hcksum_flags & HCK_PARTIALCKSUM) {
9317167Sgg161487 switch (ctx->l4_proto) {
9326621Sbt150084 case IPPROTO_TCP:
9336621Sbt150084 ctx_tbd->type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
9346621Sbt150084 break;
9356621Sbt150084 case IPPROTO_UDP:
9366621Sbt150084 /*
9376621Sbt150084 * We don't have to explicitly set:
9386621Sbt150084 * ctx_tbd->type_tucmd_mlhl |=
9396621Sbt150084 * IXGBE_ADVTXD_TUCMD_L4T_UDP;
9406621Sbt150084 * Because IXGBE_ADVTXD_TUCMD_L4T_UDP == 0b
9416621Sbt150084 */
9426621Sbt150084 break;
9436621Sbt150084 default:
9446621Sbt150084 /* Unrecoverable error */
9456621Sbt150084 IXGBE_DEBUGLOG_0(NULL, "L4 type error with tx hcksum");
9466621Sbt150084 break;
9476621Sbt150084 }
9486621Sbt150084 }
9496621Sbt150084
9506621Sbt150084 ctx_tbd->seqnum_seed = 0;
9518275SEric Cheng
9527167Sgg161487 if (ctx->lso_flag) {
9539353SSamuel.Tu@Sun.COM ctx_tbd->mss_l4len_idx =
9547167Sgg161487 (ctx->l4_hdr_len << IXGBE_ADVTXD_L4LEN_SHIFT) |
9557167Sgg161487 (ctx->mss << IXGBE_ADVTXD_MSS_SHIFT);
9569353SSamuel.Tu@Sun.COM } else {
9579353SSamuel.Tu@Sun.COM ctx_tbd->mss_l4len_idx = 0;
9587167Sgg161487 }
9596621Sbt150084 }
9606621Sbt150084
9616621Sbt150084 /*
9626621Sbt150084 * ixgbe_tx_fill_ring
9636621Sbt150084 *
9646621Sbt150084 * Fill the tx descriptor ring with the data
9656621Sbt150084 */
9666621Sbt150084 static int
ixgbe_tx_fill_ring(ixgbe_tx_ring_t * tx_ring,link_list_t * pending_list,ixgbe_tx_context_t * ctx,size_t mbsize)9676621Sbt150084 ixgbe_tx_fill_ring(ixgbe_tx_ring_t *tx_ring, link_list_t *pending_list,
9687167Sgg161487 ixgbe_tx_context_t *ctx, size_t mbsize)
9696621Sbt150084 {
9706621Sbt150084 struct ixgbe_hw *hw = &tx_ring->ixgbe->hw;
9716621Sbt150084 boolean_t load_context;
9726621Sbt150084 uint32_t index, tcb_index, desc_num;
9736621Sbt150084 union ixgbe_adv_tx_desc *tbd, *first_tbd;
9746621Sbt150084 tx_control_block_t *tcb, *first_tcb;
9756621Sbt150084 uint32_t hcksum_flags;
9766621Sbt150084 int i;
9776621Sbt150084
9786621Sbt150084 ASSERT(mutex_owned(&tx_ring->tx_lock));
9796621Sbt150084
9806621Sbt150084 tbd = NULL;
9816621Sbt150084 first_tbd = NULL;
9826621Sbt150084 first_tcb = NULL;
9836621Sbt150084 desc_num = 0;
9846621Sbt150084 hcksum_flags = 0;
9856621Sbt150084 load_context = B_FALSE;
9866621Sbt150084
9876621Sbt150084 /*
9886621Sbt150084 * Get the index of the first tx descriptor that will be filled,
9896621Sbt150084 * and the index of the first work list item that will be attached
9906621Sbt150084 * with the first used tx control block in the pending list.
9916621Sbt150084 * Note: the two indexes are the same.
9926621Sbt150084 */
9936621Sbt150084 index = tx_ring->tbd_tail;
9946621Sbt150084 tcb_index = tx_ring->tbd_tail;
9956621Sbt150084
9967167Sgg161487 if (ctx != NULL) {
9977167Sgg161487 hcksum_flags = ctx->hcksum_flags;
9986621Sbt150084
9996621Sbt150084 /*
10006621Sbt150084 * Check if a new context descriptor is needed for this packet
10016621Sbt150084 */
10027167Sgg161487 load_context = ixgbe_check_context(tx_ring, ctx);
10037167Sgg161487
10046621Sbt150084 if (load_context) {
10056621Sbt150084 tbd = &tx_ring->tbd_ring[index];
10066621Sbt150084
10076621Sbt150084 /*
10086621Sbt150084 * Fill the context descriptor with the
10096621Sbt150084 * hardware checksum offload informations.
10106621Sbt150084 */
10117167Sgg161487 ixgbe_fill_context(
10129353SSamuel.Tu@Sun.COM (struct ixgbe_adv_tx_context_desc *)tbd, ctx);
10136621Sbt150084
10146621Sbt150084 index = NEXT_INDEX(index, 1, tx_ring->ring_size);
10156621Sbt150084 desc_num++;
10166621Sbt150084
10176621Sbt150084 /*
10186621Sbt150084 * Store the checksum context data if
10196621Sbt150084 * a new context descriptor is added
10206621Sbt150084 */
10217167Sgg161487 tx_ring->tx_context = *ctx;
10226621Sbt150084 }
10236621Sbt150084 }
10246621Sbt150084
10256621Sbt150084 first_tbd = &tx_ring->tbd_ring[index];
10266621Sbt150084
10276621Sbt150084 /*
10286621Sbt150084 * Fill tx data descriptors with the data saved in the pending list.
10296621Sbt150084 * The tx control blocks in the pending list are added to the work list
10306621Sbt150084 * at the same time.
10316621Sbt150084 *
10326621Sbt150084 * The work list is strictly 1:1 corresponding to the descriptor ring.
10336621Sbt150084 * One item of the work list corresponds to one tx descriptor. Because
10346621Sbt150084 * one tx control block can span multiple tx descriptors, the tx
10356621Sbt150084 * control block will be added to the first work list item that
10366621Sbt150084 * corresponds to the first tx descriptor generated from that tx
10376621Sbt150084 * control block.
10386621Sbt150084 */
10396621Sbt150084 tcb = (tx_control_block_t *)LIST_POP_HEAD(pending_list);
10409681SPaul.Guo@Sun.COM first_tcb = tcb;
10416621Sbt150084 while (tcb != NULL) {
10426621Sbt150084
10436621Sbt150084 for (i = 0; i < tcb->desc_num; i++) {
10446621Sbt150084 tbd = &tx_ring->tbd_ring[index];
10456621Sbt150084
10466621Sbt150084 tbd->read.buffer_addr = tcb->desc[i].address;
10476621Sbt150084 tbd->read.cmd_type_len = tcb->desc[i].length;
10486621Sbt150084
10499681SPaul.Guo@Sun.COM tbd->read.cmd_type_len |= IXGBE_ADVTXD_DCMD_DEXT
10509681SPaul.Guo@Sun.COM | IXGBE_ADVTXD_DTYP_DATA;
10516621Sbt150084
10526621Sbt150084 tbd->read.olinfo_status = 0;
10536621Sbt150084
10546621Sbt150084 index = NEXT_INDEX(index, 1, tx_ring->ring_size);
10556621Sbt150084 desc_num++;
10566621Sbt150084 }
10576621Sbt150084
10586621Sbt150084 /*
10596621Sbt150084 * Add the tx control block to the work list
10606621Sbt150084 */
10616621Sbt150084 ASSERT(tx_ring->work_list[tcb_index] == NULL);
10626621Sbt150084 tx_ring->work_list[tcb_index] = tcb;
10636621Sbt150084
10646621Sbt150084 tcb_index = index;
10656621Sbt150084 tcb = (tx_control_block_t *)LIST_POP_HEAD(pending_list);
10666621Sbt150084 }
10676621Sbt150084
10689681SPaul.Guo@Sun.COM if (load_context) {
10699681SPaul.Guo@Sun.COM /*
10709681SPaul.Guo@Sun.COM * Count the context descriptor for
10719681SPaul.Guo@Sun.COM * the first tx control block.
10729681SPaul.Guo@Sun.COM */
10739681SPaul.Guo@Sun.COM first_tcb->desc_num++;
10749681SPaul.Guo@Sun.COM }
10759681SPaul.Guo@Sun.COM first_tcb->last_index = PREV_INDEX(index, 1, tx_ring->ring_size);
10769681SPaul.Guo@Sun.COM
10776621Sbt150084 /*
10786621Sbt150084 * The Insert Ethernet CRC (IFCS) bit and the checksum fields are only
10796621Sbt150084 * valid in the first descriptor of the packet.
10809353SSamuel.Tu@Sun.COM * Setting paylen in every first_tbd for all parts.
10819353SSamuel.Tu@Sun.COM * 82599 requires the packet length in paylen field with or without
10829353SSamuel.Tu@Sun.COM * LSO and 82598 will ignore it in non-LSO mode.
10836621Sbt150084 */
10846621Sbt150084 ASSERT(first_tbd != NULL);
10856621Sbt150084 first_tbd->read.cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS;
10868275SEric Cheng
10879353SSamuel.Tu@Sun.COM switch (hw->mac.type) {
1088*13006SChenlu.Chen@Sun.COM case ixgbe_mac_82598EB:
1089*13006SChenlu.Chen@Sun.COM if (ctx != NULL && ctx->lso_flag) {
1090*13006SChenlu.Chen@Sun.COM first_tbd->read.cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
1091*13006SChenlu.Chen@Sun.COM first_tbd->read.olinfo_status |=
1092*13006SChenlu.Chen@Sun.COM (mbsize - ctx->mac_hdr_len - ctx->ip_hdr_len
1093*13006SChenlu.Chen@Sun.COM - ctx->l4_hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT;
1094*13006SChenlu.Chen@Sun.COM }
1095*13006SChenlu.Chen@Sun.COM break;
1096*13006SChenlu.Chen@Sun.COM
10979353SSamuel.Tu@Sun.COM case ixgbe_mac_82599EB:
10989353SSamuel.Tu@Sun.COM if (ctx != NULL && ctx->lso_flag) {
10999353SSamuel.Tu@Sun.COM first_tbd->read.cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
11009353SSamuel.Tu@Sun.COM first_tbd->read.olinfo_status |=
11019353SSamuel.Tu@Sun.COM (mbsize - ctx->mac_hdr_len - ctx->ip_hdr_len
11029353SSamuel.Tu@Sun.COM - ctx->l4_hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT;
11039353SSamuel.Tu@Sun.COM } else {
11049353SSamuel.Tu@Sun.COM first_tbd->read.olinfo_status |=
11059353SSamuel.Tu@Sun.COM (mbsize << IXGBE_ADVTXD_PAYLEN_SHIFT);
11069353SSamuel.Tu@Sun.COM }
11079353SSamuel.Tu@Sun.COM break;
1108*13006SChenlu.Chen@Sun.COM
11099353SSamuel.Tu@Sun.COM default:
11109353SSamuel.Tu@Sun.COM break;
11117167Sgg161487 }
11127167Sgg161487
11136621Sbt150084 /* Set hardware checksum bits */
11146621Sbt150084 if (hcksum_flags != 0) {
11156621Sbt150084 if (hcksum_flags & HCK_IPV4_HDRCKSUM)
11166621Sbt150084 first_tbd->read.olinfo_status |=
11177167Sgg161487 IXGBE_ADVTXD_POPTS_IXSM;
11186621Sbt150084 if (hcksum_flags & HCK_PARTIALCKSUM)
11196621Sbt150084 first_tbd->read.olinfo_status |=
11207167Sgg161487 IXGBE_ADVTXD_POPTS_TXSM;
11216621Sbt150084 }
11226621Sbt150084
11236621Sbt150084 /*
11246621Sbt150084 * The last descriptor of packet needs End Of Packet (EOP),
11256621Sbt150084 * and Report Status (RS) bits set
11266621Sbt150084 */
11276621Sbt150084 ASSERT(tbd != NULL);
11286621Sbt150084 tbd->read.cmd_type_len |=
11296621Sbt150084 IXGBE_ADVTXD_DCMD_EOP | IXGBE_ADVTXD_DCMD_RS;
11306621Sbt150084
11316621Sbt150084 /*
11326621Sbt150084 * Sync the DMA buffer of the tx descriptor ring
11336621Sbt150084 */
11346621Sbt150084 DMA_SYNC(&tx_ring->tbd_area, DDI_DMA_SYNC_FORDEV);
11356621Sbt150084
11366621Sbt150084 /*
11376621Sbt150084 * Update the number of the free tx descriptors.
11386621Sbt150084 * The mutual exclusion between the transmission and the recycling
11396621Sbt150084 * (for the tx descriptor ring and the work list) is implemented
11406621Sbt150084 * with the atomic operation on the number of the free tx descriptors.
11416621Sbt150084 *
11426621Sbt150084 * Note: we should always decrement the counter tbd_free before
11436621Sbt150084 * advancing the hardware TDT pointer to avoid the race condition -
11446621Sbt150084 * before the counter tbd_free is decremented, the transmit of the
11456621Sbt150084 * tx descriptors has done and the counter tbd_free is increased by
11466621Sbt150084 * the tx recycling.
11476621Sbt150084 */
11486621Sbt150084 i = ixgbe_atomic_reserve(&tx_ring->tbd_free, desc_num);
11496621Sbt150084 ASSERT(i >= 0);
11506621Sbt150084
11516621Sbt150084 tx_ring->tbd_tail = index;
11526621Sbt150084
11536621Sbt150084 /*
11546621Sbt150084 * Advance the hardware TDT pointer of the tx descriptor ring
11556621Sbt150084 */
11566621Sbt150084 IXGBE_WRITE_REG(hw, IXGBE_TDT(tx_ring->index), index);
11576621Sbt150084
11586621Sbt150084 if (ixgbe_check_acc_handle(tx_ring->ixgbe->osdep.reg_handle) !=
11596621Sbt150084 DDI_FM_OK) {
11606621Sbt150084 ddi_fm_service_impact(tx_ring->ixgbe->dip,
11616621Sbt150084 DDI_SERVICE_DEGRADED);
116211233SPaul.Guo@Sun.COM atomic_or_32(&tx_ring->ixgbe->ixgbe_state, IXGBE_ERROR);
11636621Sbt150084 }
11646621Sbt150084
11656621Sbt150084 return (desc_num);
11666621Sbt150084 }
11676621Sbt150084
11686621Sbt150084 /*
11696621Sbt150084 * ixgbe_save_desc
11706621Sbt150084 *
11716621Sbt150084 * Save the address/length pair to the private array
11726621Sbt150084 * of the tx control block. The address/length pairs
11736621Sbt150084 * will be filled into the tx descriptor ring later.
11746621Sbt150084 */
11756621Sbt150084 static void
ixgbe_save_desc(tx_control_block_t * tcb,uint64_t address,size_t length)11766621Sbt150084 ixgbe_save_desc(tx_control_block_t *tcb, uint64_t address, size_t length)
11776621Sbt150084 {
11786621Sbt150084 sw_desc_t *desc;
11796621Sbt150084
11806621Sbt150084 desc = &tcb->desc[tcb->desc_num];
11816621Sbt150084 desc->address = address;
11826621Sbt150084 desc->length = length;
11836621Sbt150084
11846621Sbt150084 tcb->desc_num++;
11856621Sbt150084 }
11866621Sbt150084
11876621Sbt150084 /*
11886621Sbt150084 * ixgbe_tx_recycle_legacy
11896621Sbt150084 *
11906621Sbt150084 * Recycle the tx descriptors and tx control blocks.
11916621Sbt150084 *
11926621Sbt150084 * The work list is traversed to check if the corresponding
11936621Sbt150084 * tx descriptors have been transmitted. If so, the resources
11946621Sbt150084 * bound to the tx control blocks will be freed, and those
11956621Sbt150084 * tx control blocks will be returned to the free list.
11966621Sbt150084 */
11976621Sbt150084 uint32_t
ixgbe_tx_recycle_legacy(ixgbe_tx_ring_t * tx_ring)11986621Sbt150084 ixgbe_tx_recycle_legacy(ixgbe_tx_ring_t *tx_ring)
11996621Sbt150084 {
12009681SPaul.Guo@Sun.COM uint32_t index, last_index, prev_index;
12016621Sbt150084 int desc_num;
12026621Sbt150084 boolean_t desc_done;
12036621Sbt150084 tx_control_block_t *tcb;
12046621Sbt150084 link_list_t pending_list;
120510376SChenlu.Chen@Sun.COM ixgbe_t *ixgbe = tx_ring->ixgbe;
12066621Sbt150084
12078275SEric Cheng mutex_enter(&tx_ring->recycle_lock);
12086621Sbt150084
12096621Sbt150084 ASSERT(tx_ring->tbd_free <= tx_ring->ring_size);
12106621Sbt150084
12116621Sbt150084 if (tx_ring->tbd_free == tx_ring->ring_size) {
12126621Sbt150084 tx_ring->recycle_fail = 0;
12136621Sbt150084 tx_ring->stall_watchdog = 0;
12148275SEric Cheng if (tx_ring->reschedule) {
12158275SEric Cheng tx_ring->reschedule = B_FALSE;
121610376SChenlu.Chen@Sun.COM mac_tx_ring_update(ixgbe->mac_hdl,
12178275SEric Cheng tx_ring->ring_handle);
12188275SEric Cheng }
12196621Sbt150084 mutex_exit(&tx_ring->recycle_lock);
12206621Sbt150084 return (0);
12216621Sbt150084 }
12226621Sbt150084
12236621Sbt150084 /*
12246621Sbt150084 * Sync the DMA buffer of the tx descriptor ring
12256621Sbt150084 */
12266621Sbt150084 DMA_SYNC(&tx_ring->tbd_area, DDI_DMA_SYNC_FORKERNEL);
12276621Sbt150084
12286621Sbt150084 if (ixgbe_check_dma_handle(tx_ring->tbd_area.dma_handle) != DDI_FM_OK) {
122912003SPaul.Guo@Sun.COM mutex_exit(&tx_ring->recycle_lock);
123010376SChenlu.Chen@Sun.COM ddi_fm_service_impact(ixgbe->dip, DDI_SERVICE_DEGRADED);
123111233SPaul.Guo@Sun.COM atomic_or_32(&ixgbe->ixgbe_state, IXGBE_ERROR);
123211233SPaul.Guo@Sun.COM return (0);
12336621Sbt150084 }
12346621Sbt150084
12356621Sbt150084 LINK_LIST_INIT(&pending_list);
12366621Sbt150084 desc_num = 0;
12376621Sbt150084 index = tx_ring->tbd_head; /* Index of next tbd/tcb to recycle */
12386621Sbt150084
12396621Sbt150084 tcb = tx_ring->work_list[index];
12406621Sbt150084 ASSERT(tcb != NULL);
12416621Sbt150084
12429681SPaul.Guo@Sun.COM while (tcb != NULL) {
12436621Sbt150084 /*
12449681SPaul.Guo@Sun.COM * Get the last tx descriptor of this packet.
12459681SPaul.Guo@Sun.COM * If the last tx descriptor is done, then
12469681SPaul.Guo@Sun.COM * we can recycle all descriptors of a packet
12479681SPaul.Guo@Sun.COM * which usually includes several tx control blocks.
12489681SPaul.Guo@Sun.COM * For 82599, LSO descriptors can not be recycled
12499681SPaul.Guo@Sun.COM * unless the whole packet's transmission is done.
12509681SPaul.Guo@Sun.COM * That's why packet level recycling is used here.
12519681SPaul.Guo@Sun.COM * For 82598, there's not such limit.
12526621Sbt150084 */
12539681SPaul.Guo@Sun.COM last_index = tcb->last_index;
12549681SPaul.Guo@Sun.COM /*
12559681SPaul.Guo@Sun.COM * MAX_TX_RING_SIZE is used to judge whether
12569681SPaul.Guo@Sun.COM * the index is a valid value or not.
12579681SPaul.Guo@Sun.COM */
12589681SPaul.Guo@Sun.COM if (last_index == MAX_TX_RING_SIZE)
12599681SPaul.Guo@Sun.COM break;
12606621Sbt150084
12616621Sbt150084 /*
12626621Sbt150084 * Check if the Descriptor Done bit is set
12636621Sbt150084 */
12646621Sbt150084 desc_done = tx_ring->tbd_ring[last_index].wb.status &
12656621Sbt150084 IXGBE_TXD_STAT_DD;
12666621Sbt150084 if (desc_done) {
12676621Sbt150084 /*
12689681SPaul.Guo@Sun.COM * recycle all descriptors of the packet
12696621Sbt150084 */
12709681SPaul.Guo@Sun.COM while (tcb != NULL) {
12719681SPaul.Guo@Sun.COM /*
12729681SPaul.Guo@Sun.COM * Strip off the tx control block from
12739681SPaul.Guo@Sun.COM * the work list, and add it to the
12749681SPaul.Guo@Sun.COM * pending list.
12759681SPaul.Guo@Sun.COM */
12769681SPaul.Guo@Sun.COM tx_ring->work_list[index] = NULL;
12779681SPaul.Guo@Sun.COM LIST_PUSH_TAIL(&pending_list, &tcb->link);
12786621Sbt150084
12799681SPaul.Guo@Sun.COM /*
12809681SPaul.Guo@Sun.COM * Count the total number of the tx
12819681SPaul.Guo@Sun.COM * descriptors recycled
12829681SPaul.Guo@Sun.COM */
12839681SPaul.Guo@Sun.COM desc_num += tcb->desc_num;
12849681SPaul.Guo@Sun.COM
12859681SPaul.Guo@Sun.COM index = NEXT_INDEX(index, tcb->desc_num,
12869681SPaul.Guo@Sun.COM tx_ring->ring_size);
12876621Sbt150084
12889681SPaul.Guo@Sun.COM tcb = tx_ring->work_list[index];
12899681SPaul.Guo@Sun.COM
12909681SPaul.Guo@Sun.COM prev_index = PREV_INDEX(index, 1,
12919681SPaul.Guo@Sun.COM tx_ring->ring_size);
12929681SPaul.Guo@Sun.COM if (prev_index == last_index)
12939681SPaul.Guo@Sun.COM break;
12949681SPaul.Guo@Sun.COM }
12959681SPaul.Guo@Sun.COM } else {
12969681SPaul.Guo@Sun.COM break;
12976621Sbt150084 }
12986621Sbt150084 }
12996621Sbt150084
13006621Sbt150084 /*
13016621Sbt150084 * If no tx descriptors are recycled, no need to do more processing
13026621Sbt150084 */
13036621Sbt150084 if (desc_num == 0) {
13046621Sbt150084 tx_ring->recycle_fail++;
13056621Sbt150084 mutex_exit(&tx_ring->recycle_lock);
13066621Sbt150084 return (0);
13076621Sbt150084 }
13086621Sbt150084
13096621Sbt150084 tx_ring->recycle_fail = 0;
13106621Sbt150084 tx_ring->stall_watchdog = 0;
13116621Sbt150084
13126621Sbt150084 /*
13136621Sbt150084 * Update the head index of the tx descriptor ring
13146621Sbt150084 */
13156621Sbt150084 tx_ring->tbd_head = index;
13166621Sbt150084
13176621Sbt150084 /*
13186621Sbt150084 * Update the number of the free tx descriptors with atomic operations
13196621Sbt150084 */
13206621Sbt150084 atomic_add_32(&tx_ring->tbd_free, desc_num);
13216621Sbt150084
132210376SChenlu.Chen@Sun.COM if ((tx_ring->tbd_free >= ixgbe->tx_resched_thresh) &&
13238275SEric Cheng (tx_ring->reschedule)) {
13248275SEric Cheng tx_ring->reschedule = B_FALSE;
132510376SChenlu.Chen@Sun.COM mac_tx_ring_update(ixgbe->mac_hdl,
13268275SEric Cheng tx_ring->ring_handle);
13278275SEric Cheng }
13286621Sbt150084 mutex_exit(&tx_ring->recycle_lock);
13296621Sbt150084
13306621Sbt150084 /*
13316621Sbt150084 * Free the resources used by the tx control blocks
13326621Sbt150084 * in the pending list
13336621Sbt150084 */
13346621Sbt150084 tcb = (tx_control_block_t *)LIST_GET_HEAD(&pending_list);
13356621Sbt150084 while (tcb != NULL) {
13366621Sbt150084 /*
13376621Sbt150084 * Release the resources occupied by the tx control block
13386621Sbt150084 */
13396621Sbt150084 ixgbe_free_tcb(tcb);
13406621Sbt150084
13416621Sbt150084 tcb = (tx_control_block_t *)
13426621Sbt150084 LIST_GET_NEXT(&pending_list, &tcb->link);
13436621Sbt150084 }
13446621Sbt150084
13456621Sbt150084 /*
13466621Sbt150084 * Add the tx control blocks in the pending list to the free list.
13476621Sbt150084 */
13486621Sbt150084 ixgbe_put_free_list(tx_ring, &pending_list);
13496621Sbt150084
13506621Sbt150084 return (desc_num);
13516621Sbt150084 }
13526621Sbt150084
13536621Sbt150084 /*
13546621Sbt150084 * ixgbe_tx_recycle_head_wb
13556621Sbt150084 *
13566621Sbt150084 * Check the head write-back, and recycle all the transmitted
13576621Sbt150084 * tx descriptors and tx control blocks.
13586621Sbt150084 */
13596621Sbt150084 uint32_t
ixgbe_tx_recycle_head_wb(ixgbe_tx_ring_t * tx_ring)13606621Sbt150084 ixgbe_tx_recycle_head_wb(ixgbe_tx_ring_t *tx_ring)
13616621Sbt150084 {
13626621Sbt150084 uint32_t index;
13636621Sbt150084 uint32_t head_wb;
13646621Sbt150084 int desc_num;
13656621Sbt150084 tx_control_block_t *tcb;
13666621Sbt150084 link_list_t pending_list;
136710376SChenlu.Chen@Sun.COM ixgbe_t *ixgbe = tx_ring->ixgbe;
13686621Sbt150084
13698275SEric Cheng mutex_enter(&tx_ring->recycle_lock);
13706621Sbt150084
13716621Sbt150084 ASSERT(tx_ring->tbd_free <= tx_ring->ring_size);
13726621Sbt150084
13736621Sbt150084 if (tx_ring->tbd_free == tx_ring->ring_size) {
13746621Sbt150084 tx_ring->recycle_fail = 0;
13756621Sbt150084 tx_ring->stall_watchdog = 0;
13768275SEric Cheng if (tx_ring->reschedule) {
13778275SEric Cheng tx_ring->reschedule = B_FALSE;
137810376SChenlu.Chen@Sun.COM mac_tx_ring_update(ixgbe->mac_hdl,
13798275SEric Cheng tx_ring->ring_handle);
13808275SEric Cheng }
13816621Sbt150084 mutex_exit(&tx_ring->recycle_lock);
13826621Sbt150084 return (0);
13836621Sbt150084 }
13846621Sbt150084
13856621Sbt150084 /*
13866621Sbt150084 * Sync the DMA buffer of the tx descriptor ring
13876621Sbt150084 *
13886621Sbt150084 * Note: For head write-back mode, the tx descriptors will not
13896621Sbt150084 * be written back, but the head write-back value is stored at
13906621Sbt150084 * the last extra tbd at the end of the DMA area, we still need
13916621Sbt150084 * to sync the head write-back value for kernel.
13926621Sbt150084 *
13936621Sbt150084 * DMA_SYNC(&tx_ring->tbd_area, DDI_DMA_SYNC_FORKERNEL);
13946621Sbt150084 */
13956621Sbt150084 (void) ddi_dma_sync(tx_ring->tbd_area.dma_handle,
13966621Sbt150084 sizeof (union ixgbe_adv_tx_desc) * tx_ring->ring_size,
13976621Sbt150084 sizeof (uint32_t),
13986621Sbt150084 DDI_DMA_SYNC_FORKERNEL);
13996621Sbt150084
14006621Sbt150084 if (ixgbe_check_dma_handle(tx_ring->tbd_area.dma_handle) != DDI_FM_OK) {
140112003SPaul.Guo@Sun.COM mutex_exit(&tx_ring->recycle_lock);
140210376SChenlu.Chen@Sun.COM ddi_fm_service_impact(ixgbe->dip,
14036621Sbt150084 DDI_SERVICE_DEGRADED);
140411233SPaul.Guo@Sun.COM atomic_or_32(&ixgbe->ixgbe_state, IXGBE_ERROR);
140511233SPaul.Guo@Sun.COM return (0);
14066621Sbt150084 }
14076621Sbt150084
14086621Sbt150084 LINK_LIST_INIT(&pending_list);
14096621Sbt150084 desc_num = 0;
14106621Sbt150084 index = tx_ring->tbd_head; /* Next index to clean */
14116621Sbt150084
14126621Sbt150084 /*
14136621Sbt150084 * Get the value of head write-back
14146621Sbt150084 */
14156621Sbt150084 head_wb = *tx_ring->tbd_head_wb;
14166621Sbt150084 while (index != head_wb) {
14176621Sbt150084 tcb = tx_ring->work_list[index];
14186621Sbt150084 ASSERT(tcb != NULL);
14196621Sbt150084
14206621Sbt150084 if (OFFSET(index, head_wb, tx_ring->ring_size) <
14216621Sbt150084 tcb->desc_num) {
14226621Sbt150084 /*
14236621Sbt150084 * The current tx control block is not
14246621Sbt150084 * completely transmitted, stop recycling
14256621Sbt150084 */
14266621Sbt150084 break;
14276621Sbt150084 }
14286621Sbt150084
14296621Sbt150084 /*
14306621Sbt150084 * Strip off the tx control block from the work list,
14316621Sbt150084 * and add it to the pending list.
14326621Sbt150084 */
14336621Sbt150084 tx_ring->work_list[index] = NULL;
14346621Sbt150084 LIST_PUSH_TAIL(&pending_list, &tcb->link);
14356621Sbt150084
14366621Sbt150084 /*
14376621Sbt150084 * Advance the index of the tx descriptor ring
14386621Sbt150084 */
14396621Sbt150084 index = NEXT_INDEX(index, tcb->desc_num, tx_ring->ring_size);
14406621Sbt150084
14416621Sbt150084 /*
14426621Sbt150084 * Count the total number of the tx descriptors recycled
14436621Sbt150084 */
14446621Sbt150084 desc_num += tcb->desc_num;
14456621Sbt150084 }
14466621Sbt150084
14476621Sbt150084 /*
14486621Sbt150084 * If no tx descriptors are recycled, no need to do more processing
14496621Sbt150084 */
14506621Sbt150084 if (desc_num == 0) {
14516621Sbt150084 tx_ring->recycle_fail++;
14526621Sbt150084 mutex_exit(&tx_ring->recycle_lock);
14536621Sbt150084 return (0);
14546621Sbt150084 }
14556621Sbt150084
14566621Sbt150084 tx_ring->recycle_fail = 0;
14576621Sbt150084 tx_ring->stall_watchdog = 0;
14586621Sbt150084
14596621Sbt150084 /*
14606621Sbt150084 * Update the head index of the tx descriptor ring
14616621Sbt150084 */
14626621Sbt150084 tx_ring->tbd_head = index;
14636621Sbt150084
14646621Sbt150084 /*
14656621Sbt150084 * Update the number of the free tx descriptors with atomic operations
14666621Sbt150084 */
14676621Sbt150084 atomic_add_32(&tx_ring->tbd_free, desc_num);
14686621Sbt150084
146910376SChenlu.Chen@Sun.COM if ((tx_ring->tbd_free >= ixgbe->tx_resched_thresh) &&
14708275SEric Cheng (tx_ring->reschedule)) {
14718275SEric Cheng tx_ring->reschedule = B_FALSE;
147210376SChenlu.Chen@Sun.COM mac_tx_ring_update(ixgbe->mac_hdl,
14738275SEric Cheng tx_ring->ring_handle);
14748275SEric Cheng }
14756621Sbt150084 mutex_exit(&tx_ring->recycle_lock);
14766621Sbt150084
14776621Sbt150084 /*
14786621Sbt150084 * Free the resources used by the tx control blocks
14796621Sbt150084 * in the pending list
14806621Sbt150084 */
14816621Sbt150084 tcb = (tx_control_block_t *)LIST_GET_HEAD(&pending_list);
14826621Sbt150084 while (tcb) {
14836621Sbt150084 /*
14846621Sbt150084 * Release the resources occupied by the tx control block
14856621Sbt150084 */
14866621Sbt150084 ixgbe_free_tcb(tcb);
14876621Sbt150084
14886621Sbt150084 tcb = (tx_control_block_t *)
14896621Sbt150084 LIST_GET_NEXT(&pending_list, &tcb->link);
14906621Sbt150084 }
14916621Sbt150084
14926621Sbt150084 /*
14936621Sbt150084 * Add the tx control blocks in the pending list to the free list.
14946621Sbt150084 */
14956621Sbt150084 ixgbe_put_free_list(tx_ring, &pending_list);
14966621Sbt150084
14976621Sbt150084 return (desc_num);
14986621Sbt150084 }
14996621Sbt150084
15006621Sbt150084 /*
15016621Sbt150084 * ixgbe_free_tcb - free up the tx control block
15026621Sbt150084 *
15036621Sbt150084 * Free the resources of the tx control block, including
15046621Sbt150084 * unbind the previously bound DMA handle, and reset other
15056621Sbt150084 * control fields.
15066621Sbt150084 */
15076621Sbt150084 void
ixgbe_free_tcb(tx_control_block_t * tcb)15086621Sbt150084 ixgbe_free_tcb(tx_control_block_t *tcb)
15096621Sbt150084 {
15106621Sbt150084 switch (tcb->tx_type) {
15116621Sbt150084 case USE_COPY:
15126621Sbt150084 /*
15136621Sbt150084 * Reset the buffer length that is used for copy
15146621Sbt150084 */
15156621Sbt150084 tcb->tx_buf.len = 0;
15166621Sbt150084 break;
15176621Sbt150084 case USE_DMA:
15186621Sbt150084 /*
15196621Sbt150084 * Release the DMA resource that is used for
15206621Sbt150084 * DMA binding.
15216621Sbt150084 */
15226621Sbt150084 (void) ddi_dma_unbind_handle(tcb->tx_dma_handle);
15236621Sbt150084 break;
15246621Sbt150084 default:
15256621Sbt150084 break;
15266621Sbt150084 }
15276621Sbt150084
15286621Sbt150084 /*
15296621Sbt150084 * Free the mblk
15306621Sbt150084 */
15316621Sbt150084 if (tcb->mp != NULL) {
15326621Sbt150084 freemsg(tcb->mp);
15336621Sbt150084 tcb->mp = NULL;
15346621Sbt150084 }
15356621Sbt150084
15366621Sbt150084 tcb->tx_type = USE_NONE;
15379681SPaul.Guo@Sun.COM tcb->last_index = MAX_TX_RING_SIZE;
15386621Sbt150084 tcb->frag_num = 0;
15396621Sbt150084 tcb->desc_num = 0;
15406621Sbt150084 }
15416621Sbt150084
15426621Sbt150084 /*
15436621Sbt150084 * ixgbe_get_free_list - Get a free tx control block from the free list
15446621Sbt150084 *
15456621Sbt150084 * The atomic operation on the number of the available tx control block
15466621Sbt150084 * in the free list is used to keep this routine mutual exclusive with
15476621Sbt150084 * the routine ixgbe_put_check_list.
15486621Sbt150084 */
15496621Sbt150084 static tx_control_block_t *
ixgbe_get_free_list(ixgbe_tx_ring_t * tx_ring)15506621Sbt150084 ixgbe_get_free_list(ixgbe_tx_ring_t *tx_ring)
15516621Sbt150084 {
15526621Sbt150084 tx_control_block_t *tcb;
15536621Sbt150084
15546621Sbt150084 /*
15556621Sbt150084 * Check and update the number of the free tx control block
15566621Sbt150084 * in the free list.
15576621Sbt150084 */
15586621Sbt150084 if (ixgbe_atomic_reserve(&tx_ring->tcb_free, 1) < 0)
15596621Sbt150084 return (NULL);
15606621Sbt150084
15616621Sbt150084 mutex_enter(&tx_ring->tcb_head_lock);
15626621Sbt150084
15636621Sbt150084 tcb = tx_ring->free_list[tx_ring->tcb_head];
15646621Sbt150084 ASSERT(tcb != NULL);
15656621Sbt150084 tx_ring->free_list[tx_ring->tcb_head] = NULL;
15666621Sbt150084 tx_ring->tcb_head = NEXT_INDEX(tx_ring->tcb_head, 1,
15676621Sbt150084 tx_ring->free_list_size);
15686621Sbt150084
15696621Sbt150084 mutex_exit(&tx_ring->tcb_head_lock);
15706621Sbt150084
15716621Sbt150084 return (tcb);
15726621Sbt150084 }
15736621Sbt150084
15746621Sbt150084 /*
15756621Sbt150084 * ixgbe_put_free_list
15766621Sbt150084 *
15776621Sbt150084 * Put a list of used tx control blocks back to the free list
15786621Sbt150084 *
15796621Sbt150084 * A mutex is used here to ensure the serialization. The mutual exclusion
15806621Sbt150084 * between ixgbe_get_free_list and ixgbe_put_free_list is implemented with
15816621Sbt150084 * the atomic operation on the counter tcb_free.
15826621Sbt150084 */
15836621Sbt150084 void
ixgbe_put_free_list(ixgbe_tx_ring_t * tx_ring,link_list_t * pending_list)15846621Sbt150084 ixgbe_put_free_list(ixgbe_tx_ring_t *tx_ring, link_list_t *pending_list)
15856621Sbt150084 {
15866621Sbt150084 uint32_t index;
15876621Sbt150084 int tcb_num;
15886621Sbt150084 tx_control_block_t *tcb;
15896621Sbt150084
15906621Sbt150084 mutex_enter(&tx_ring->tcb_tail_lock);
15916621Sbt150084
15926621Sbt150084 index = tx_ring->tcb_tail;
15936621Sbt150084
15946621Sbt150084 tcb_num = 0;
15956621Sbt150084 tcb = (tx_control_block_t *)LIST_POP_HEAD(pending_list);
15966621Sbt150084 while (tcb != NULL) {
15976621Sbt150084 ASSERT(tx_ring->free_list[index] == NULL);
15986621Sbt150084 tx_ring->free_list[index] = tcb;
15996621Sbt150084
16006621Sbt150084 tcb_num++;
16016621Sbt150084
16026621Sbt150084 index = NEXT_INDEX(index, 1, tx_ring->free_list_size);
16036621Sbt150084
16046621Sbt150084 tcb = (tx_control_block_t *)LIST_POP_HEAD(pending_list);
16056621Sbt150084 }
16066621Sbt150084
16076621Sbt150084 tx_ring->tcb_tail = index;
16086621Sbt150084
16096621Sbt150084 /*
16106621Sbt150084 * Update the number of the free tx control block
16116621Sbt150084 * in the free list. This operation must be placed
16126621Sbt150084 * under the protection of the lock.
16136621Sbt150084 */
16146621Sbt150084 atomic_add_32(&tx_ring->tcb_free, tcb_num);
16156621Sbt150084
16166621Sbt150084 mutex_exit(&tx_ring->tcb_tail_lock);
16176621Sbt150084 }
1618