xref: /onnv-gate/usr/src/uts/common/io/bge/bge_send.c (revision 12673:e579258c2bb7)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51369Sdduvall  * Common Development and Distribution License (the "License").
61369Sdduvall  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211369Sdduvall 
220Sstevel@tonic-gate /*
2312547SYong.Tan@Sun.COM  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
262675Szh199473 #include "bge_impl.h"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * The transmit-side code uses an allocation process which is similar
310Sstevel@tonic-gate  * to some theme park roller-coaster rides, where riders sit in cars
320Sstevel@tonic-gate  * that can go individually, but work better in a train.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * 1)	RESERVE a place - this doesn't refer to any specific car or
350Sstevel@tonic-gate  *	seat, just that you will get a ride.  The attempt to RESERVE a
360Sstevel@tonic-gate  *	place can fail if all spaces in all cars are already committed.
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  * 2)	Prepare yourself; this may take an arbitrary (but not unbounded)
390Sstevel@tonic-gate  *	time, and you can back out at this stage, in which case you must
400Sstevel@tonic-gate  *	give up (RENOUNCE) your place.
410Sstevel@tonic-gate  *
420Sstevel@tonic-gate  * 3)	CLAIM your space - a specific car (the next sequentially
430Sstevel@tonic-gate  *	numbered one) is allocated at this stage, and is guaranteed
440Sstevel@tonic-gate  *	to be part of the next train to depart.  Once you've done
450Sstevel@tonic-gate  *	this, you can't back out, nor wait for any external event
460Sstevel@tonic-gate  *	or resource.
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * 4)	Occupy your car - when all CLAIMED cars are OCCUPIED, they
490Sstevel@tonic-gate  *	all depart together as a single train!
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  * 5)	At the end of the ride, you climb out of the car and RENOUNCE
520Sstevel@tonic-gate  *	your right to it, so that it can be recycled for another rider.
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  * For each rider, these have to occur in this order, but the riders
550Sstevel@tonic-gate  * don't have to stay in the same order at each stage.  In particular,
560Sstevel@tonic-gate  * they may overtake each other between RESERVING a place and CLAIMING
570Sstevel@tonic-gate  * it, or between CLAIMING and OCCUPYING a space.
580Sstevel@tonic-gate  *
590Sstevel@tonic-gate  * Once a car is CLAIMED, the train currently being assembled can't go
600Sstevel@tonic-gate  * without that car (this guarantees that the cars in a single train
610Sstevel@tonic-gate  * make up a consecutively-numbered set).  Therefore, when any train
620Sstevel@tonic-gate  * leaves, we know there can't be any riders in transit between CLAIMING
630Sstevel@tonic-gate  * and OCCUPYING their cars.  There can be some who have RESERVED but
640Sstevel@tonic-gate  * not yet CLAIMED their places.  That's OK, though, because they'll go
650Sstevel@tonic-gate  * into the next train.
660Sstevel@tonic-gate  */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #define	BGE_DBG		BGE_DBG_SEND	/* debug flag for this code	*/
690Sstevel@tonic-gate 
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate  * ========== Send-side recycle routines ==========
720Sstevel@tonic-gate  */
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate  * Recycle all the completed buffers in the specified send ring up to
760Sstevel@tonic-gate  * (but not including) the consumer index in the status block.
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  * This function must advance (srp->tc_next) AND adjust (srp->tx_free)
790Sstevel@tonic-gate  * to account for the packets it has recycled.
800Sstevel@tonic-gate  *
810Sstevel@tonic-gate  * This is a trivial version that just does that and nothing more, but
820Sstevel@tonic-gate  * it suffices while there's only one method for sending messages (by
830Sstevel@tonic-gate  * copying) and that method doesn't need any special per-buffer action
840Sstevel@tonic-gate  * for recycling.
850Sstevel@tonic-gate  */
8612547SYong.Tan@Sun.COM static boolean_t bge_recycle_ring(bge_t *bgep, send_ring_t *srp);
870Sstevel@tonic-gate #pragma	inline(bge_recycle_ring)
880Sstevel@tonic-gate 
8912547SYong.Tan@Sun.COM static boolean_t
bge_recycle_ring(bge_t * bgep,send_ring_t * srp)900Sstevel@tonic-gate bge_recycle_ring(bge_t *bgep, send_ring_t *srp)
910Sstevel@tonic-gate {
923334Sgs150176 	sw_sbd_t *ssbdp;
933334Sgs150176 	bge_queue_item_t *buf_item;
943334Sgs150176 	bge_queue_item_t *buf_item_head;
953334Sgs150176 	bge_queue_item_t *buf_item_tail;
963334Sgs150176 	bge_queue_t *txbuf_queue;
970Sstevel@tonic-gate 	uint64_t slot;
980Sstevel@tonic-gate 	uint64_t n;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	ASSERT(mutex_owned(srp->tc_lock));
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	/*
1030Sstevel@tonic-gate 	 * We're about to release one or more places :-)
1040Sstevel@tonic-gate 	 * These ASSERTions check that our invariants still hold:
1050Sstevel@tonic-gate 	 *	there must always be at least one free place
1060Sstevel@tonic-gate 	 *	at this point, there must be at least one place NOT free
1070Sstevel@tonic-gate 	 *	we're not about to free more places than were claimed!
1080Sstevel@tonic-gate 	 */
1098922SYong.Tan@Sun.COM 	ASSERT(srp->tx_free <= srp->desc.nslots);
1100Sstevel@tonic-gate 
1113334Sgs150176 	buf_item_head = buf_item_tail = NULL;
1123334Sgs150176 	for (n = 0, slot = srp->tc_next; slot != *srp->cons_index_p;
1133334Sgs150176 	    slot = NEXT(slot, srp->desc.nslots)) {
1143334Sgs150176 		ssbdp = &srp->sw_sbds[slot];
1153334Sgs150176 		ASSERT(ssbdp->pbuf != NULL);
1163334Sgs150176 		buf_item = ssbdp->pbuf;
1173334Sgs150176 		if (buf_item_head == NULL)
1183334Sgs150176 			buf_item_head = buf_item_tail = buf_item;
1193334Sgs150176 		else {
1203334Sgs150176 			buf_item_tail->next = buf_item;
1213334Sgs150176 			buf_item_tail = buf_item;
1223334Sgs150176 		}
1233334Sgs150176 		ssbdp->pbuf = NULL;
1243334Sgs150176 		n++;
1253334Sgs150176 	}
1263334Sgs150176 	if (n == 0)
12712547SYong.Tan@Sun.COM 		return (B_FALSE);
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	/*
1300Sstevel@tonic-gate 	 * Reset the watchdog count: to 0 if all buffers are
1310Sstevel@tonic-gate 	 * now free, or to 1 if some are still outstanding.
1320Sstevel@tonic-gate 	 * Note: non-synchonised access here means we may get
1330Sstevel@tonic-gate 	 * the "wrong" answer, but only in a harmless fashion
1340Sstevel@tonic-gate 	 * (i.e. we deactivate the watchdog because all buffers
1350Sstevel@tonic-gate 	 * are apparently free, even though another thread may
1360Sstevel@tonic-gate 	 * have claimed one before we leave here; in this case
1370Sstevel@tonic-gate 	 * the watchdog will restart on the next send() call).
1380Sstevel@tonic-gate 	 */
13912547SYong.Tan@Sun.COM 	bgep->watchdog = (slot == srp->tx_next) ? 0 : 1;
14012547SYong.Tan@Sun.COM 
14112547SYong.Tan@Sun.COM 	/*
14212547SYong.Tan@Sun.COM 	 * Update recycle index and free tx BD number
14312547SYong.Tan@Sun.COM 	 */
14412547SYong.Tan@Sun.COM 	srp->tc_next = slot;
14512547SYong.Tan@Sun.COM 	ASSERT(srp->tx_free + n <= srp->desc.nslots);
14612547SYong.Tan@Sun.COM 	bge_atomic_renounce(&srp->tx_free, n);
1473334Sgs150176 
1483334Sgs150176 	/*
1493334Sgs150176 	 * Return tx buffers to buffer push queue
1503334Sgs150176 	 */
1513334Sgs150176 	txbuf_queue = srp->txbuf_push_queue;
1523334Sgs150176 	mutex_enter(txbuf_queue->lock);
1533334Sgs150176 	buf_item_tail->next = txbuf_queue->head;
1543334Sgs150176 	txbuf_queue->head = buf_item_head;
1553334Sgs150176 	txbuf_queue->count += n;
1563334Sgs150176 	mutex_exit(txbuf_queue->lock);
1573334Sgs150176 
1583334Sgs150176 	/*
1593334Sgs150176 	 * Check if we need exchange the tx buffer push and pop queue
1603334Sgs150176 	 */
1613334Sgs150176 	if ((srp->txbuf_pop_queue->count < srp->tx_buffers_low) &&
1623334Sgs150176 	    (srp->txbuf_pop_queue->count < txbuf_queue->count)) {
1633334Sgs150176 		srp->txbuf_push_queue = srp->txbuf_pop_queue;
1643334Sgs150176 		srp->txbuf_pop_queue = txbuf_queue;
1653334Sgs150176 	}
1663334Sgs150176 
1673534Szh199473 	if (srp->tx_flow != 0 || bgep->tx_resched_needed)
1683334Sgs150176 		ddi_trigger_softintr(bgep->drain_id);
16912547SYong.Tan@Sun.COM 
17012547SYong.Tan@Sun.COM 	return (B_TRUE);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate /*
1740Sstevel@tonic-gate  * Recycle all returned slots in all rings.
1750Sstevel@tonic-gate  *
1760Sstevel@tonic-gate  * To give priority to low-numbered rings, whenever we have recycled any
1770Sstevel@tonic-gate  * slots in any ring except 0, we restart scanning again from ring 0.
1780Sstevel@tonic-gate  * Thus, for example, if rings 0, 3, and 10 are carrying traffic, the
1790Sstevel@tonic-gate  * pattern of recycles might go 0, 3, 10, 3, 0, 10, 0:
1800Sstevel@tonic-gate  *
1810Sstevel@tonic-gate  *	0	found some - recycle them
1820Sstevel@tonic-gate  *	1..2					none found
1830Sstevel@tonic-gate  *	3	found some - recycle them	and restart scan
1840Sstevel@tonic-gate  *	0..9					none found
1850Sstevel@tonic-gate  *	10	found some - recycle them	and restart scan
1860Sstevel@tonic-gate  *	0..2					none found
1870Sstevel@tonic-gate  *	3	found some more - recycle them	and restart scan
1880Sstevel@tonic-gate  *	0	found some more - recycle them
1890Sstevel@tonic-gate  *	0..9					none found
1900Sstevel@tonic-gate  *	10	found some more - recycle them	and restart scan
1910Sstevel@tonic-gate  *	0	found some more - recycle them
1920Sstevel@tonic-gate  *	1..15					none found
1930Sstevel@tonic-gate  *
1940Sstevel@tonic-gate  * The routine returns only when a complete scan has been performed
1950Sstevel@tonic-gate  * without finding any slots to recycle.
1960Sstevel@tonic-gate  *
1970Sstevel@tonic-gate  * Note: the expression (BGE_SEND_RINGS_USED > 1) yields a compile-time
1980Sstevel@tonic-gate  * constant and allows the compiler to optimise away the outer do-loop
1990Sstevel@tonic-gate  * if only one send ring is being used.
2000Sstevel@tonic-gate  */
20112547SYong.Tan@Sun.COM boolean_t bge_recycle(bge_t *bgep, bge_status_t *bsp);
2020Sstevel@tonic-gate #pragma	no_inline(bge_recycle)
2030Sstevel@tonic-gate 
20412547SYong.Tan@Sun.COM boolean_t
bge_recycle(bge_t * bgep,bge_status_t * bsp)2050Sstevel@tonic-gate bge_recycle(bge_t *bgep, bge_status_t *bsp)
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate 	send_ring_t *srp;
2080Sstevel@tonic-gate 	uint64_t ring;
2090Sstevel@tonic-gate 	uint64_t tx_rings = bgep->chipid.tx_rings;
21012547SYong.Tan@Sun.COM 	boolean_t tx_done = B_FALSE;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate restart:
2130Sstevel@tonic-gate 	ring = 0;
2140Sstevel@tonic-gate 	srp = &bgep->send[ring];
2150Sstevel@tonic-gate 	do {
2160Sstevel@tonic-gate 		/*
2170Sstevel@tonic-gate 		 * For each ring, (srp->cons_index_p) points to the
2180Sstevel@tonic-gate 		 * proper index within the status block (which has
2190Sstevel@tonic-gate 		 * already been sync'd by the caller).
2200Sstevel@tonic-gate 		 */
2210Sstevel@tonic-gate 		ASSERT(srp->cons_index_p == SEND_INDEX_P(bsp, ring));
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 		if (*srp->cons_index_p == srp->tc_next)
2240Sstevel@tonic-gate 			continue;		/* no slots to recycle	*/
2253334Sgs150176 		if (mutex_tryenter(srp->tc_lock) == 0)
2263334Sgs150176 			continue;		/* already in process	*/
22712547SYong.Tan@Sun.COM 		tx_done |= bge_recycle_ring(bgep, srp);
2280Sstevel@tonic-gate 		mutex_exit(srp->tc_lock);
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 		/*
2310Sstevel@tonic-gate 		 * Restart from ring 0, if we're not on ring 0 already.
2320Sstevel@tonic-gate 		 * As H/W selects send BDs totally based on priority and
2330Sstevel@tonic-gate 		 * available BDs on the higher priority ring are always
2340Sstevel@tonic-gate 		 * selected first, driver should keep consistence with H/W
2350Sstevel@tonic-gate 		 * and gives lower-numbered ring with higher priority.
2360Sstevel@tonic-gate 		 */
2370Sstevel@tonic-gate 		if (tx_rings > 1 && ring > 0)
2380Sstevel@tonic-gate 			goto restart;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 		/*
2410Sstevel@tonic-gate 		 * Loop over all rings (if there *are* multiple rings)
2420Sstevel@tonic-gate 		 */
2430Sstevel@tonic-gate 	} while (++srp, ++ring < tx_rings);
24412547SYong.Tan@Sun.COM 
24512547SYong.Tan@Sun.COM 	return (tx_done);
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate /*
2500Sstevel@tonic-gate  * ========== Send-side transmit routines ==========
2510Sstevel@tonic-gate  */
2522135Szh199473 #define	TCP_CKSUM_OFFSET	16
2532135Szh199473 #define	UDP_CKSUM_OFFSET	6
2542135Szh199473 
2552135Szh199473 static void
bge_pseudo_cksum(uint8_t * buf)2562135Szh199473 bge_pseudo_cksum(uint8_t *buf)
2572135Szh199473 {
2582135Szh199473 	uint32_t cksum;
2592135Szh199473 	uint16_t iphl;
2602135Szh199473 	uint16_t proto;
2612135Szh199473 
2622135Szh199473 	/*
2632135Szh199473 	 * Point it to the ip header.
2642135Szh199473 	 */
2652135Szh199473 	buf += sizeof (struct ether_header);
2662135Szh199473 
2672135Szh199473 	/*
2682135Szh199473 	 * Calculate the pseudo-header checksum.
2692135Szh199473 	 */
2702135Szh199473 	iphl = 4 * (buf[0] & 0xF);
2712135Szh199473 	cksum = (((uint16_t)buf[2])<<8) + buf[3] - iphl;
2722135Szh199473 	cksum += proto = buf[9];
2732135Szh199473 	cksum += (((uint16_t)buf[12])<<8) + buf[13];
2742135Szh199473 	cksum += (((uint16_t)buf[14])<<8) + buf[15];
2752135Szh199473 	cksum += (((uint16_t)buf[16])<<8) + buf[17];
2762135Szh199473 	cksum += (((uint16_t)buf[18])<<8) + buf[19];
2772135Szh199473 	cksum = (cksum>>16) + (cksum & 0xFFFF);
2782135Szh199473 	cksum = (cksum>>16) + (cksum & 0xFFFF);
2792135Szh199473 
2802135Szh199473 	/*
2812135Szh199473 	 * Point it to the TCP/UDP header, and
2822135Szh199473 	 * update the checksum field.
2832135Szh199473 	 */
2842135Szh199473 	buf += iphl + ((proto == IPPROTO_TCP) ?
2857099Syt223700 	    TCP_CKSUM_OFFSET : UDP_CKSUM_OFFSET);
2862135Szh199473 
2877099Syt223700 	/*
2887099Syt223700 	 * A real possibility that pointer cast is a problem.
2897099Syt223700 	 * Should be fixed when we know the code better.
2907099Syt223700 	 * E_BAD_PTR_CAST_ALIGN is added to make it temporarily clean.
2917099Syt223700 	 */
2922135Szh199473 	*(uint16_t *)buf = htons((uint16_t)cksum);
2932135Szh199473 }
2942135Szh199473 
2953334Sgs150176 static bge_queue_item_t *
bge_get_txbuf(bge_t * bgep,send_ring_t * srp)2963334Sgs150176 bge_get_txbuf(bge_t *bgep, send_ring_t *srp)
2973334Sgs150176 {
2983334Sgs150176 	bge_queue_item_t *txbuf_item;
2993334Sgs150176 	bge_queue_t *txbuf_queue;
3000Sstevel@tonic-gate 
3013334Sgs150176 	txbuf_queue = srp->txbuf_pop_queue;
3023334Sgs150176 	mutex_enter(txbuf_queue->lock);
3033334Sgs150176 	if (txbuf_queue->count == 0) {
3043334Sgs150176 		mutex_exit(txbuf_queue->lock);
3053334Sgs150176 		txbuf_queue = srp->txbuf_push_queue;
3063334Sgs150176 		mutex_enter(txbuf_queue->lock);
3073334Sgs150176 		if (txbuf_queue->count == 0) {
3083334Sgs150176 			mutex_exit(txbuf_queue->lock);
3093334Sgs150176 			/* Try to allocate more tx buffers */
3103334Sgs150176 			if (srp->tx_array < srp->tx_array_max) {
3113334Sgs150176 				mutex_enter(srp->tx_lock);
3123334Sgs150176 				txbuf_item = bge_alloc_txbuf_array(bgep, srp);
3133334Sgs150176 				mutex_exit(srp->tx_lock);
3143334Sgs150176 			} else
3153334Sgs150176 				txbuf_item = NULL;
3163334Sgs150176 			return (txbuf_item);
3173334Sgs150176 		}
3183334Sgs150176 	}
3193334Sgs150176 	txbuf_item = txbuf_queue->head;
3203334Sgs150176 	txbuf_queue->head = (bge_queue_item_t *)txbuf_item->next;
3213334Sgs150176 	txbuf_queue->count--;
3223334Sgs150176 	mutex_exit(txbuf_queue->lock);
3233334Sgs150176 	txbuf_item->next = NULL;
3243334Sgs150176 
3253334Sgs150176 	return (txbuf_item);
3263334Sgs150176 }
3273334Sgs150176 
3283334Sgs150176 /*
3293334Sgs150176  * Send a message by copying it into a preallocated (and premapped) buffer
3303334Sgs150176  */
3313534Szh199473 static void bge_send_copy(bge_t *bgep, sw_txbuf_t *txbuf, mblk_t *mp);
3323334Sgs150176 #pragma	inline(bge_send_copy)
3333334Sgs150176 
3343334Sgs150176 static void
bge_send_copy(bge_t * bgep,sw_txbuf_t * txbuf,mblk_t * mp)3353534Szh199473 bge_send_copy(bge_t *bgep, sw_txbuf_t *txbuf, mblk_t *mp)
3363334Sgs150176 {
3373334Sgs150176 	mblk_t *bp;
3383334Sgs150176 	uint32_t mblen;
3393334Sgs150176 	char *pbuf;
3403334Sgs150176 
3413334Sgs150176 	txbuf->copy_len = 0;
3423334Sgs150176 	pbuf = DMA_VPTR(txbuf->buf);
3433534Szh199473 	for (bp = mp; bp != NULL; bp = bp->b_cont) {
3443334Sgs150176 		if ((mblen = MBLKL(bp)) == 0)
3453334Sgs150176 			continue;
3463534Szh199473 		ASSERT(txbuf->copy_len + mblen <=
3473534Szh199473 		    bgep->chipid.snd_buff_size);
3483534Szh199473 		bcopy(bp->b_rptr, pbuf, mblen);
3493534Szh199473 		pbuf += mblen;
3503534Szh199473 		txbuf->copy_len += mblen;
3510Sstevel@tonic-gate 	}
3523334Sgs150176 }
3533334Sgs150176 
3543334Sgs150176 /*
3553334Sgs150176  * Fill the Tx buffer descriptors and trigger the h/w transmission
3563334Sgs150176  */
3573334Sgs150176 static void
bge_send_serial(bge_t * bgep,send_ring_t * srp)3583334Sgs150176 bge_send_serial(bge_t *bgep, send_ring_t *srp)
3593334Sgs150176 {
3603334Sgs150176 	send_pkt_t *pktp;
3613334Sgs150176 	uint64_t txfill_next;
3623334Sgs150176 	uint32_t count;
3633334Sgs150176 	uint32_t tx_next;
3643334Sgs150176 	sw_sbd_t *ssbdp;
3653334Sgs150176 	bge_status_t *bsp;
36612547SYong.Tan@Sun.COM 	bge_sbd_t *hw_sbd_p;
36712547SYong.Tan@Sun.COM 	bge_queue_item_t *txbuf_item;
36812547SYong.Tan@Sun.COM 	sw_txbuf_t *txbuf;
3693334Sgs150176 
3703334Sgs150176 	/*
3713334Sgs150176 	 * Try to hold the tx lock:
3723334Sgs150176 	 *	If we are in an interrupt context, use mutex_enter() to
3733334Sgs150176 	 *	ensure quick response for tx in interrupt context;
3743334Sgs150176 	 *	Otherwise, use mutex_tryenter() to serialize this h/w tx
3753334Sgs150176 	 *	BD filling and transmission triggering task.
3763334Sgs150176 	 */
3773334Sgs150176 	if (servicing_interrupt() != 0)
3783334Sgs150176 		mutex_enter(srp->tx_lock);
3793334Sgs150176 	else if (mutex_tryenter(srp->tx_lock) == 0)
3803334Sgs150176 		return;		/* already in process	*/
3813334Sgs150176 
3823334Sgs150176 	bsp = DMA_VPTR(bgep->status_block);
3833334Sgs150176 	txfill_next = srp->txfill_next;
38412547SYong.Tan@Sun.COM 	tx_next = srp->tx_next;
3853534Szh199473 start_tx:
3863334Sgs150176 	for (count = 0; count < bgep->param_drain_max; ++count) {
3873334Sgs150176 		pktp = &srp->pktp[txfill_next];
3883334Sgs150176 		if (!pktp->tx_ready) {
3893334Sgs150176 			if (count == 0)
3903334Sgs150176 				srp->tx_block++;
3913334Sgs150176 			break;
3923334Sgs150176 		}
3933334Sgs150176 
3943334Sgs150176 		/*
3953334Sgs150176 		 * If there are no enough BDs: try to recycle more
3963334Sgs150176 		 */
3973334Sgs150176 		if (srp->tx_free <= 1)
39812547SYong.Tan@Sun.COM 			(void) bge_recycle(bgep, bsp);
3993334Sgs150176 
4003334Sgs150176 		/*
4013334Sgs150176 		 * Reserved required BDs: 1 is enough
4023334Sgs150176 		 */
4033334Sgs150176 		if (!bge_atomic_reserve(&srp->tx_free, 1)) {
4043334Sgs150176 			srp->tx_nobd++;
4053334Sgs150176 			break;
4063334Sgs150176 		}
4073334Sgs150176 
4083334Sgs150176 		/*
4093334Sgs150176 		 * Filling the tx BD
4103334Sgs150176 		 */
41112547SYong.Tan@Sun.COM 
41212547SYong.Tan@Sun.COM 		/*
41312547SYong.Tan@Sun.COM 		 * Go straight to claiming our already-reserved places
41412547SYong.Tan@Sun.COM 		 * on the train!
41512547SYong.Tan@Sun.COM 		 */
41612547SYong.Tan@Sun.COM 		ASSERT(pktp->txbuf_item != NULL);
41712547SYong.Tan@Sun.COM 		txbuf_item = pktp->txbuf_item;
41812547SYong.Tan@Sun.COM 		pktp->txbuf_item = NULL;
41912547SYong.Tan@Sun.COM 		pktp->tx_ready = B_FALSE;
42012547SYong.Tan@Sun.COM 
42112547SYong.Tan@Sun.COM 		txbuf = txbuf_item->item;
42212547SYong.Tan@Sun.COM 		ASSERT(txbuf->copy_len != 0);
42312547SYong.Tan@Sun.COM 		(void) ddi_dma_sync(txbuf->buf.dma_hdl,  0,
42412547SYong.Tan@Sun.COM 		    txbuf->copy_len, DDI_DMA_SYNC_FORDEV);
42512547SYong.Tan@Sun.COM 
42612547SYong.Tan@Sun.COM 		ssbdp = &srp->sw_sbds[tx_next];
42712547SYong.Tan@Sun.COM 		ASSERT(ssbdp->pbuf == NULL);
42812547SYong.Tan@Sun.COM 		ssbdp->pbuf = txbuf_item;
42912547SYong.Tan@Sun.COM 
43012547SYong.Tan@Sun.COM 		/*
43112547SYong.Tan@Sun.COM 		 * Setting hardware send buffer descriptor
43212547SYong.Tan@Sun.COM 		 */
43312547SYong.Tan@Sun.COM 		hw_sbd_p = DMA_VPTR(ssbdp->desc);
43412547SYong.Tan@Sun.COM 		hw_sbd_p->flags = 0;
43512547SYong.Tan@Sun.COM 		hw_sbd_p->host_buf_addr = txbuf->buf.cookie.dmac_laddress;
43612547SYong.Tan@Sun.COM 		hw_sbd_p->len = txbuf->copy_len;
43712547SYong.Tan@Sun.COM 		if (pktp->vlan_tci != 0) {
43812547SYong.Tan@Sun.COM 			hw_sbd_p->vlan_tci = pktp->vlan_tci;
43912547SYong.Tan@Sun.COM 			hw_sbd_p->host_buf_addr += VLAN_TAGSZ;
44012547SYong.Tan@Sun.COM 			hw_sbd_p->flags |= SBD_FLAG_VLAN_TAG;
44112547SYong.Tan@Sun.COM 		}
44212547SYong.Tan@Sun.COM 		if (pktp->pflags & HCK_IPV4_HDRCKSUM)
44312547SYong.Tan@Sun.COM 			hw_sbd_p->flags |= SBD_FLAG_IP_CKSUM;
44412547SYong.Tan@Sun.COM 		if (pktp->pflags & HCK_FULLCKSUM)
44512547SYong.Tan@Sun.COM 			hw_sbd_p->flags |= SBD_FLAG_TCP_UDP_CKSUM;
44612547SYong.Tan@Sun.COM 		hw_sbd_p->flags |= SBD_FLAG_PACKET_END;
44712547SYong.Tan@Sun.COM 
4483334Sgs150176 		txfill_next = NEXT(txfill_next, BGE_SEND_BUF_MAX);
44912547SYong.Tan@Sun.COM 		tx_next = NEXT(tx_next, srp->desc.nslots);
4503334Sgs150176 	}
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	/*
4533334Sgs150176 	 * Trigger h/w to start transmission.
4540Sstevel@tonic-gate 	 */
4553334Sgs150176 	if (count != 0) {
4563334Sgs150176 		bge_atomic_sub64(&srp->tx_flow, count);
45712547SYong.Tan@Sun.COM 		srp->txfill_next = txfill_next;
45812547SYong.Tan@Sun.COM 
45912547SYong.Tan@Sun.COM 		if (srp->tx_next > tx_next) {
4603334Sgs150176 			(void) ddi_dma_sync(ssbdp->desc.dma_hdl,  0,
46112547SYong.Tan@Sun.COM 			    (srp->desc.nslots - srp->tx_next) *
46212547SYong.Tan@Sun.COM 			    sizeof (bge_sbd_t),
4633334Sgs150176 			    DDI_DMA_SYNC_FORDEV);
46412547SYong.Tan@Sun.COM 			count -= srp->desc.nslots - srp->tx_next;
4653334Sgs150176 			ssbdp = &srp->sw_sbds[0];
4663334Sgs150176 		}
4673334Sgs150176 		(void) ddi_dma_sync(ssbdp->desc.dma_hdl,  0,
4683334Sgs150176 		    count*sizeof (bge_sbd_t), DDI_DMA_SYNC_FORDEV);
46912547SYong.Tan@Sun.COM 		bge_mbx_put(bgep, srp->chip_mbx_reg, tx_next);
47012547SYong.Tan@Sun.COM 		srp->tx_next = tx_next;
47112547SYong.Tan@Sun.COM 		atomic_or_32(&bgep->watchdog, 1);
47212547SYong.Tan@Sun.COM 
4733534Szh199473 		if (srp->tx_flow != 0 && srp->tx_free > 1)
4743534Szh199473 			goto start_tx;
4750Sstevel@tonic-gate 	}
4760Sstevel@tonic-gate 
4773334Sgs150176 	mutex_exit(srp->tx_lock);
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate 
4808275SEric Cheng mblk_t *
bge_ring_tx(void * arg,mblk_t * mp)4818275SEric Cheng bge_ring_tx(void *arg, mblk_t *mp)
4820Sstevel@tonic-gate {
4838275SEric Cheng 	send_ring_t *srp = arg;
4848275SEric Cheng 	bge_t *bgep = srp->bgep;
4850Sstevel@tonic-gate 	struct ether_vlan_header *ehp;
4863334Sgs150176 	bge_queue_item_t *txbuf_item;
4873334Sgs150176 	sw_txbuf_t *txbuf;
4883334Sgs150176 	send_pkt_t *pktp;
4893334Sgs150176 	uint64_t pkt_slot;
4903334Sgs150176 	uint16_t vlan_tci;
4913334Sgs150176 	uint32_t pflags;
4923534Szh199473 	char *pbuf;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	ASSERT(mp->b_next == NULL);
4953334Sgs150176 
4963334Sgs150176 	/*
4973334Sgs150176 	 * Get a s/w tx buffer first
4983334Sgs150176 	 */
4993334Sgs150176 	txbuf_item = bge_get_txbuf(bgep, srp);
5003334Sgs150176 	if (txbuf_item == NULL) {
5013334Sgs150176 		/* no tx buffer available */
5023334Sgs150176 		srp->tx_nobuf++;
5033334Sgs150176 		bgep->tx_resched_needed = B_TRUE;
5043334Sgs150176 		bge_send_serial(bgep, srp);
5058275SEric Cheng 		return (mp);
5063334Sgs150176 	}
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	/*
5093334Sgs150176 	 * Copy all mp fragments to the pkt buffer
5100Sstevel@tonic-gate 	 */
5113334Sgs150176 	txbuf = txbuf_item->item;
5123534Szh199473 	bge_send_copy(bgep, txbuf, mp);
5133534Szh199473 
5143534Szh199473 	/*
5153534Szh199473 	 * Determine if the packet is VLAN tagged.
5163534Szh199473 	 */
5173534Szh199473 	ASSERT(txbuf->copy_len >= sizeof (struct ether_header));
5183534Szh199473 	pbuf = DMA_VPTR(txbuf->buf);
5193534Szh199473 
5207099Syt223700 	ehp = (void *)pbuf;
5213534Szh199473 	if (ehp->ether_tpid == htons(ETHERTYPE_VLAN)) {
5223534Szh199473 		/* Strip the vlan tag */
5233534Szh199473 		vlan_tci = ntohs(ehp->ether_tci);
5243534Szh199473 		pbuf = memmove(pbuf + VLAN_TAGSZ, pbuf, 2 * ETHERADDRL);
5253534Szh199473 		txbuf->copy_len -= VLAN_TAGSZ;
5263534Szh199473 	} else
5273534Szh199473 		vlan_tci = 0;
5283334Sgs150176 
5293334Sgs150176 	/*
5303334Sgs150176 	 * Retrieve checksum offloading info.
5313334Sgs150176 	 */
53211878SVenu.Iyer@Sun.COM 	mac_hcksum_get(mp, NULL, NULL, NULL, NULL, &pflags);
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	/*
5353334Sgs150176 	 * Calculate pseudo checksum if needed.
5360Sstevel@tonic-gate 	 */
5373334Sgs150176 	if ((pflags & HCK_FULLCKSUM) &&
5383334Sgs150176 	    (bgep->chipid.flags & CHIP_FLAG_PARTIAL_CSUM))
5393534Szh199473 		bge_pseudo_cksum((uint8_t *)pbuf);
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	/*
5423334Sgs150176 	 * Packet buffer is ready to send: get and fill pkt info
5430Sstevel@tonic-gate 	 */
5443334Sgs150176 	pkt_slot = bge_atomic_next(&srp->txpkt_next, BGE_SEND_BUF_MAX);
5453334Sgs150176 	pktp = &srp->pktp[pkt_slot];
5463334Sgs150176 	ASSERT(pktp->txbuf_item == NULL);
5473334Sgs150176 	pktp->txbuf_item = txbuf_item;
5483334Sgs150176 	pktp->vlan_tci = vlan_tci;
5493334Sgs150176 	pktp->pflags = pflags;
5503334Sgs150176 	atomic_inc_64(&srp->tx_flow);
5513334Sgs150176 	ASSERT(pktp->tx_ready == B_FALSE);
5523334Sgs150176 	pktp->tx_ready = B_TRUE;
5530Sstevel@tonic-gate 
5543334Sgs150176 	/*
5553334Sgs150176 	 * Filling the h/w bd and trigger the h/w to start transmission
5563334Sgs150176 	 */
5573334Sgs150176 	bge_send_serial(bgep, srp);
5583334Sgs150176 
5598275SEric Cheng 	srp->pushed_bytes += MBLKL(mp);
5608275SEric Cheng 
5613334Sgs150176 	/*
5623334Sgs150176 	 * We've copied the contents, the message can be freed right away
5633334Sgs150176 	 */
5643334Sgs150176 	freemsg(mp);
5658275SEric Cheng 	return (NULL);
5668275SEric Cheng }
5673334Sgs150176 
5688275SEric Cheng static mblk_t *
bge_send(bge_t * bgep,mblk_t * mp)5698275SEric Cheng bge_send(bge_t *bgep, mblk_t *mp)
5708275SEric Cheng {
5718275SEric Cheng 	send_ring_t *ring;
5728275SEric Cheng 
5738275SEric Cheng 	ring = &bgep->send[0];	/* ring 0 */
5748275SEric Cheng 
5758275SEric Cheng 	return (bge_ring_tx(ring, mp));
5760Sstevel@tonic-gate }
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate uint_t
bge_send_drain(caddr_t arg)5793334Sgs150176 bge_send_drain(caddr_t arg)
5800Sstevel@tonic-gate {
5813334Sgs150176 	uint_t ring = 0;	/* use ring 0 */
5820Sstevel@tonic-gate 	bge_t *bgep;
5833334Sgs150176 	send_ring_t *srp;
5840Sstevel@tonic-gate 
5857099Syt223700 	bgep = (void *)arg;
5863334Sgs150176 	BGE_TRACE(("bge_send_drain($%p)", (void *)bgep));
5870Sstevel@tonic-gate 
5883334Sgs150176 	srp = &bgep->send[ring];
5893334Sgs150176 	bge_send_serial(bgep, srp);
5900Sstevel@tonic-gate 
5913334Sgs150176 	if (bgep->tx_resched_needed &&
5923334Sgs150176 	    (srp->tx_flow < srp->tx_buffers_low) &&
5933334Sgs150176 	    (bgep->bge_mac_state == BGE_MAC_STARTED)) {
5942311Sseb 		mac_tx_update(bgep->mh);
5953334Sgs150176 		bgep->tx_resched_needed = B_FALSE;
5963334Sgs150176 		bgep->tx_resched++;
5970Sstevel@tonic-gate 	}
5980Sstevel@tonic-gate 
5991200Sly149593 	return (DDI_INTR_CLAIMED);
6000Sstevel@tonic-gate }
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate /*
6030Sstevel@tonic-gate  * bge_m_tx() - send a chain of packets
6040Sstevel@tonic-gate  */
6050Sstevel@tonic-gate mblk_t *
bge_m_tx(void * arg,mblk_t * mp)6060Sstevel@tonic-gate bge_m_tx(void *arg, mblk_t *mp)
6070Sstevel@tonic-gate {
6080Sstevel@tonic-gate 	bge_t *bgep = arg;		/* private device info	*/
6090Sstevel@tonic-gate 	mblk_t *next;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	BGE_TRACE(("bge_m_tx($%p, $%p)", arg, (void *)mp));
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	ASSERT(mp != NULL);
6140Sstevel@tonic-gate 	ASSERT(bgep->bge_mac_state == BGE_MAC_STARTED);
6150Sstevel@tonic-gate 
6163170Sml149210 	rw_enter(bgep->errlock, RW_READER);
617*12673SYong.Tan@Sun.COM 	if ((bgep->bge_chip_state != BGE_CHIP_RUNNING) ||
618*12673SYong.Tan@Sun.COM 	    !(bgep->param_link_up)) {
619*12673SYong.Tan@Sun.COM 		BGE_DEBUG(("bge_m_tx: chip not running or link down"));
6203170Sml149210 		freemsgchain(mp);
6213170Sml149210 		mp = NULL;
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	while (mp != NULL) {
6250Sstevel@tonic-gate 		next = mp->b_next;
6260Sstevel@tonic-gate 		mp->b_next = NULL;
6270Sstevel@tonic-gate 
6288275SEric Cheng 		if ((mp = bge_send(bgep, mp)) != NULL) {
6290Sstevel@tonic-gate 			mp->b_next = next;
6300Sstevel@tonic-gate 			break;
6310Sstevel@tonic-gate 		}
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 		mp = next;
6340Sstevel@tonic-gate 	}
635152Sly149593 	rw_exit(bgep->errlock);
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	return (mp);
6380Sstevel@tonic-gate }
639