xref: /onnv-gate/usr/src/uts/common/io/bge/bge_send.c (revision 3534:ca275a2f676c)
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 /*
23*3534Szh199473  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
292675Szh199473 #include "bge_impl.h"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * The transmit-side code uses an allocation process which is similar
340Sstevel@tonic-gate  * to some theme park roller-coaster rides, where riders sit in cars
350Sstevel@tonic-gate  * that can go individually, but work better in a train.
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  * 1)	RESERVE a place - this doesn't refer to any specific car or
380Sstevel@tonic-gate  *	seat, just that you will get a ride.  The attempt to RESERVE a
390Sstevel@tonic-gate  *	place can fail if all spaces in all cars are already committed.
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * 2)	Prepare yourself; this may take an arbitrary (but not unbounded)
420Sstevel@tonic-gate  *	time, and you can back out at this stage, in which case you must
430Sstevel@tonic-gate  *	give up (RENOUNCE) your place.
440Sstevel@tonic-gate  *
450Sstevel@tonic-gate  * 3)	CLAIM your space - a specific car (the next sequentially
460Sstevel@tonic-gate  *	numbered one) is allocated at this stage, and is guaranteed
470Sstevel@tonic-gate  *	to be part of the next train to depart.  Once you've done
480Sstevel@tonic-gate  *	this, you can't back out, nor wait for any external event
490Sstevel@tonic-gate  *	or resource.
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  * 4)	Occupy your car - when all CLAIMED cars are OCCUPIED, they
520Sstevel@tonic-gate  *	all depart together as a single train!
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  * 5)	At the end of the ride, you climb out of the car and RENOUNCE
550Sstevel@tonic-gate  *	your right to it, so that it can be recycled for another rider.
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  * For each rider, these have to occur in this order, but the riders
580Sstevel@tonic-gate  * don't have to stay in the same order at each stage.  In particular,
590Sstevel@tonic-gate  * they may overtake each other between RESERVING a place and CLAIMING
600Sstevel@tonic-gate  * it, or between CLAIMING and OCCUPYING a space.
610Sstevel@tonic-gate  *
620Sstevel@tonic-gate  * Once a car is CLAIMED, the train currently being assembled can't go
630Sstevel@tonic-gate  * without that car (this guarantees that the cars in a single train
640Sstevel@tonic-gate  * make up a consecutively-numbered set).  Therefore, when any train
650Sstevel@tonic-gate  * leaves, we know there can't be any riders in transit between CLAIMING
660Sstevel@tonic-gate  * and OCCUPYING their cars.  There can be some who have RESERVED but
670Sstevel@tonic-gate  * not yet CLAIMED their places.  That's OK, though, because they'll go
680Sstevel@tonic-gate  * into the next train.
690Sstevel@tonic-gate  */
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #define	BGE_DBG		BGE_DBG_SEND	/* debug flag for this code	*/
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * ========== Send-side recycle routines ==========
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * Recycle all the completed buffers in the specified send ring up to
790Sstevel@tonic-gate  * (but not including) the consumer index in the status block.
800Sstevel@tonic-gate  *
810Sstevel@tonic-gate  * This function must advance (srp->tc_next) AND adjust (srp->tx_free)
820Sstevel@tonic-gate  * to account for the packets it has recycled.
830Sstevel@tonic-gate  *
840Sstevel@tonic-gate  * This is a trivial version that just does that and nothing more, but
850Sstevel@tonic-gate  * it suffices while there's only one method for sending messages (by
860Sstevel@tonic-gate  * copying) and that method doesn't need any special per-buffer action
870Sstevel@tonic-gate  * for recycling.
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate static void bge_recycle_ring(bge_t *bgep, send_ring_t *srp);
900Sstevel@tonic-gate #pragma	inline(bge_recycle_ring)
910Sstevel@tonic-gate 
920Sstevel@tonic-gate static void
930Sstevel@tonic-gate bge_recycle_ring(bge_t *bgep, send_ring_t *srp)
940Sstevel@tonic-gate {
953334Sgs150176 	sw_sbd_t *ssbdp;
963334Sgs150176 	bge_queue_item_t *buf_item;
973334Sgs150176 	bge_queue_item_t *buf_item_head;
983334Sgs150176 	bge_queue_item_t *buf_item_tail;
993334Sgs150176 	bge_queue_t *txbuf_queue;
1000Sstevel@tonic-gate 	uint64_t slot;
1010Sstevel@tonic-gate 	uint64_t n;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	ASSERT(mutex_owned(srp->tc_lock));
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	/*
1060Sstevel@tonic-gate 	 * We're about to release one or more places :-)
1070Sstevel@tonic-gate 	 * These ASSERTions check that our invariants still hold:
1080Sstevel@tonic-gate 	 *	there must always be at least one free place
1090Sstevel@tonic-gate 	 *	at this point, there must be at least one place NOT free
1100Sstevel@tonic-gate 	 *	we're not about to free more places than were claimed!
1110Sstevel@tonic-gate 	 */
1120Sstevel@tonic-gate 	ASSERT(srp->tx_free > 0);
1133334Sgs150176 	ASSERT(srp->tx_free < srp->desc.nslots);
1140Sstevel@tonic-gate 
1153334Sgs150176 	buf_item_head = buf_item_tail = NULL;
1163334Sgs150176 	for (n = 0, slot = srp->tc_next; slot != *srp->cons_index_p;
1173334Sgs150176 	    slot = NEXT(slot, srp->desc.nslots)) {
1183334Sgs150176 		ssbdp = &srp->sw_sbds[slot];
1193334Sgs150176 		ASSERT(ssbdp->pbuf != NULL);
1203334Sgs150176 		buf_item = ssbdp->pbuf;
1213334Sgs150176 		if (buf_item_head == NULL)
1223334Sgs150176 			buf_item_head = buf_item_tail = buf_item;
1233334Sgs150176 		else {
1243334Sgs150176 			buf_item_tail->next = buf_item;
1253334Sgs150176 			buf_item_tail = buf_item;
1263334Sgs150176 		}
1273334Sgs150176 		ssbdp->pbuf = NULL;
1283334Sgs150176 		n++;
1293334Sgs150176 	}
1303334Sgs150176 	if (n == 0)
1313334Sgs150176 		return;
1323334Sgs150176 
1333334Sgs150176 	/*
1343334Sgs150176 	 * Update recycle index and free tx BD number
1353334Sgs150176 	 */
1360Sstevel@tonic-gate 	srp->tc_next = slot;
1373334Sgs150176 	ASSERT(srp->tx_free + n <= srp->desc.nslots);
1380Sstevel@tonic-gate 	bge_atomic_renounce(&srp->tx_free, n);
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	/*
1410Sstevel@tonic-gate 	 * Reset the watchdog count: to 0 if all buffers are
1420Sstevel@tonic-gate 	 * now free, or to 1 if some are still outstanding.
1430Sstevel@tonic-gate 	 * Note: non-synchonised access here means we may get
1440Sstevel@tonic-gate 	 * the "wrong" answer, but only in a harmless fashion
1450Sstevel@tonic-gate 	 * (i.e. we deactivate the watchdog because all buffers
1460Sstevel@tonic-gate 	 * are apparently free, even though another thread may
1470Sstevel@tonic-gate 	 * have claimed one before we leave here; in this case
1480Sstevel@tonic-gate 	 * the watchdog will restart on the next send() call).
1490Sstevel@tonic-gate 	 */
1500Sstevel@tonic-gate 	bgep->watchdog = srp->tx_free == srp->desc.nslots ? 0 : 1;
1513334Sgs150176 
1523334Sgs150176 	/*
1533334Sgs150176 	 * Return tx buffers to buffer push queue
1543334Sgs150176 	 */
1553334Sgs150176 	txbuf_queue = srp->txbuf_push_queue;
1563334Sgs150176 	mutex_enter(txbuf_queue->lock);
1573334Sgs150176 	buf_item_tail->next = txbuf_queue->head;
1583334Sgs150176 	txbuf_queue->head = buf_item_head;
1593334Sgs150176 	txbuf_queue->count += n;
1603334Sgs150176 	mutex_exit(txbuf_queue->lock);
1613334Sgs150176 
1623334Sgs150176 	/*
1633334Sgs150176 	 * Check if we need exchange the tx buffer push and pop queue
1643334Sgs150176 	 */
1653334Sgs150176 	if ((srp->txbuf_pop_queue->count < srp->tx_buffers_low) &&
1663334Sgs150176 	    (srp->txbuf_pop_queue->count < txbuf_queue->count)) {
1673334Sgs150176 		srp->txbuf_push_queue = srp->txbuf_pop_queue;
1683334Sgs150176 		srp->txbuf_pop_queue = txbuf_queue;
1693334Sgs150176 	}
1703334Sgs150176 
171*3534Szh199473 	if (srp->tx_flow != 0 || bgep->tx_resched_needed)
1723334Sgs150176 		ddi_trigger_softintr(bgep->drain_id);
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate /*
1760Sstevel@tonic-gate  * Recycle all returned slots in all rings.
1770Sstevel@tonic-gate  *
1780Sstevel@tonic-gate  * To give priority to low-numbered rings, whenever we have recycled any
1790Sstevel@tonic-gate  * slots in any ring except 0, we restart scanning again from ring 0.
1800Sstevel@tonic-gate  * Thus, for example, if rings 0, 3, and 10 are carrying traffic, the
1810Sstevel@tonic-gate  * pattern of recycles might go 0, 3, 10, 3, 0, 10, 0:
1820Sstevel@tonic-gate  *
1830Sstevel@tonic-gate  *	0	found some - recycle them
1840Sstevel@tonic-gate  *	1..2					none found
1850Sstevel@tonic-gate  *	3	found some - recycle them	and restart scan
1860Sstevel@tonic-gate  *	0..9					none found
1870Sstevel@tonic-gate  *	10	found some - recycle them	and restart scan
1880Sstevel@tonic-gate  *	0..2					none found
1890Sstevel@tonic-gate  *	3	found some more - recycle them	and restart scan
1900Sstevel@tonic-gate  *	0	found some more - recycle them
1910Sstevel@tonic-gate  *	0..9					none found
1920Sstevel@tonic-gate  *	10	found some more - recycle them	and restart scan
1930Sstevel@tonic-gate  *	0	found some more - recycle them
1940Sstevel@tonic-gate  *	1..15					none found
1950Sstevel@tonic-gate  *
1960Sstevel@tonic-gate  * The routine returns only when a complete scan has been performed
1970Sstevel@tonic-gate  * without finding any slots to recycle.
1980Sstevel@tonic-gate  *
1990Sstevel@tonic-gate  * Note: the expression (BGE_SEND_RINGS_USED > 1) yields a compile-time
2000Sstevel@tonic-gate  * constant and allows the compiler to optimise away the outer do-loop
2010Sstevel@tonic-gate  * if only one send ring is being used.
2020Sstevel@tonic-gate  */
2030Sstevel@tonic-gate void bge_recycle(bge_t *bgep, bge_status_t *bsp);
2040Sstevel@tonic-gate #pragma	no_inline(bge_recycle)
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate void
2070Sstevel@tonic-gate bge_recycle(bge_t *bgep, bge_status_t *bsp)
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate 	send_ring_t *srp;
2100Sstevel@tonic-gate 	uint64_t ring;
2110Sstevel@tonic-gate 	uint64_t tx_rings = bgep->chipid.tx_rings;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate restart:
2140Sstevel@tonic-gate 	ring = 0;
2150Sstevel@tonic-gate 	srp = &bgep->send[ring];
2160Sstevel@tonic-gate 	do {
2170Sstevel@tonic-gate 		/*
2180Sstevel@tonic-gate 		 * For each ring, (srp->cons_index_p) points to the
2190Sstevel@tonic-gate 		 * proper index within the status block (which has
2200Sstevel@tonic-gate 		 * already been sync'd by the caller).
2210Sstevel@tonic-gate 		 */
2220Sstevel@tonic-gate 		ASSERT(srp->cons_index_p == SEND_INDEX_P(bsp, ring));
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 		if (*srp->cons_index_p == srp->tc_next)
2250Sstevel@tonic-gate 			continue;		/* no slots to recycle	*/
2263334Sgs150176 		if (mutex_tryenter(srp->tc_lock) == 0)
2273334Sgs150176 			continue;		/* already in process	*/
2280Sstevel@tonic-gate 		bge_recycle_ring(bgep, srp);
2290Sstevel@tonic-gate 		mutex_exit(srp->tc_lock);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 		/*
2320Sstevel@tonic-gate 		 * Restart from ring 0, if we're not on ring 0 already.
2330Sstevel@tonic-gate 		 * As H/W selects send BDs totally based on priority and
2340Sstevel@tonic-gate 		 * available BDs on the higher priority ring are always
2350Sstevel@tonic-gate 		 * selected first, driver should keep consistence with H/W
2360Sstevel@tonic-gate 		 * and gives lower-numbered ring with higher priority.
2370Sstevel@tonic-gate 		 */
2380Sstevel@tonic-gate 		if (tx_rings > 1 && ring > 0)
2390Sstevel@tonic-gate 			goto restart;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 		/*
2420Sstevel@tonic-gate 		 * Loop over all rings (if there *are* multiple rings)
2430Sstevel@tonic-gate 		 */
2440Sstevel@tonic-gate 	} while (++srp, ++ring < tx_rings);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate  * ========== Send-side transmit routines ==========
2500Sstevel@tonic-gate  */
2512135Szh199473 #define	TCP_CKSUM_OFFSET	16
2522135Szh199473 #define	UDP_CKSUM_OFFSET	6
2532135Szh199473 
2542135Szh199473 static void
2552135Szh199473 bge_pseudo_cksum(uint8_t *buf)
2562135Szh199473 {
2572135Szh199473 	uint32_t cksum;
2582135Szh199473 	uint16_t iphl;
2592135Szh199473 	uint16_t proto;
2602135Szh199473 
2612135Szh199473 	/*
2622135Szh199473 	 * Point it to the ip header.
2632135Szh199473 	 */
2642135Szh199473 	buf += sizeof (struct ether_header);
2652135Szh199473 
2662135Szh199473 	/*
2672135Szh199473 	 * Calculate the pseudo-header checksum.
2682135Szh199473 	 */
2692135Szh199473 	iphl = 4 * (buf[0] & 0xF);
2702135Szh199473 	cksum = (((uint16_t)buf[2])<<8) + buf[3] - iphl;
2712135Szh199473 	cksum += proto = buf[9];
2722135Szh199473 	cksum += (((uint16_t)buf[12])<<8) + buf[13];
2732135Szh199473 	cksum += (((uint16_t)buf[14])<<8) + buf[15];
2742135Szh199473 	cksum += (((uint16_t)buf[16])<<8) + buf[17];
2752135Szh199473 	cksum += (((uint16_t)buf[18])<<8) + buf[19];
2762135Szh199473 	cksum = (cksum>>16) + (cksum & 0xFFFF);
2772135Szh199473 	cksum = (cksum>>16) + (cksum & 0xFFFF);
2782135Szh199473 
2792135Szh199473 	/*
2802135Szh199473 	 * Point it to the TCP/UDP header, and
2812135Szh199473 	 * update the checksum field.
2822135Szh199473 	 */
2832135Szh199473 	buf += iphl + ((proto == IPPROTO_TCP) ?
2842135Szh199473 		TCP_CKSUM_OFFSET : UDP_CKSUM_OFFSET);
2852135Szh199473 
2862135Szh199473 	*(uint16_t *)buf = htons((uint16_t)cksum);
2872135Szh199473 }
2882135Szh199473 
2893334Sgs150176 static bge_queue_item_t *
2903334Sgs150176 bge_get_txbuf(bge_t *bgep, send_ring_t *srp)
2913334Sgs150176 {
2923334Sgs150176 	bge_queue_item_t *txbuf_item;
2933334Sgs150176 	bge_queue_t *txbuf_queue;
2940Sstevel@tonic-gate 
2953334Sgs150176 	txbuf_queue = srp->txbuf_pop_queue;
2963334Sgs150176 	mutex_enter(txbuf_queue->lock);
2973334Sgs150176 	if (txbuf_queue->count == 0) {
2983334Sgs150176 		mutex_exit(txbuf_queue->lock);
2993334Sgs150176 		txbuf_queue = srp->txbuf_push_queue;
3003334Sgs150176 		mutex_enter(txbuf_queue->lock);
3013334Sgs150176 		if (txbuf_queue->count == 0) {
3023334Sgs150176 			mutex_exit(txbuf_queue->lock);
3033334Sgs150176 			/* Try to allocate more tx buffers */
3043334Sgs150176 			if (srp->tx_array < srp->tx_array_max) {
3053334Sgs150176 				mutex_enter(srp->tx_lock);
3063334Sgs150176 				txbuf_item = bge_alloc_txbuf_array(bgep, srp);
3073334Sgs150176 				mutex_exit(srp->tx_lock);
3083334Sgs150176 			} else
3093334Sgs150176 				txbuf_item = NULL;
3103334Sgs150176 			return (txbuf_item);
3113334Sgs150176 		}
3123334Sgs150176 	}
3133334Sgs150176 	txbuf_item = txbuf_queue->head;
3143334Sgs150176 	txbuf_queue->head = (bge_queue_item_t *)txbuf_item->next;
3153334Sgs150176 	txbuf_queue->count--;
3163334Sgs150176 	mutex_exit(txbuf_queue->lock);
3173334Sgs150176 	txbuf_item->next = NULL;
3183334Sgs150176 
3193334Sgs150176 	return (txbuf_item);
3203334Sgs150176 }
3213334Sgs150176 
3223334Sgs150176 static void bge_send_fill_txbd(send_ring_t *srp, send_pkt_t *pktp);
3233334Sgs150176 #pragma	inline(bge_send_fill_txbd)
3243334Sgs150176 
3253334Sgs150176 static void
3263334Sgs150176 bge_send_fill_txbd(send_ring_t *srp, send_pkt_t *pktp)
3270Sstevel@tonic-gate {
3280Sstevel@tonic-gate 	bge_sbd_t *hw_sbd_p;
3290Sstevel@tonic-gate 	sw_sbd_t *ssbdp;
3303334Sgs150176 	bge_queue_item_t *txbuf_item;
3313334Sgs150176 	sw_txbuf_t *txbuf;
3320Sstevel@tonic-gate 	uint64_t slot;
3330Sstevel@tonic-gate 
3343334Sgs150176 	ASSERT(mutex_owned(srp->tx_lock));
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	/*
3373334Sgs150176 	 * Go straight to claiming our already-reserved places
3383334Sgs150176 	 * on the train!
3390Sstevel@tonic-gate 	 */
3403334Sgs150176 	ASSERT(pktp->txbuf_item != NULL);
3413334Sgs150176 	txbuf_item = pktp->txbuf_item;
3423334Sgs150176 	txbuf = txbuf_item->item;
3433334Sgs150176 	slot = srp->tx_next;
3440Sstevel@tonic-gate 	ssbdp = &srp->sw_sbds[slot];
3453334Sgs150176 	hw_sbd_p = DMA_VPTR(ssbdp->desc);
3463334Sgs150176 	hw_sbd_p->flags = 0;
3473334Sgs150176 	ASSERT(txbuf->copy_len != 0);
3483334Sgs150176 	(void) ddi_dma_sync(txbuf->buf.dma_hdl,  0,
3493334Sgs150176 	    txbuf->copy_len, DDI_DMA_SYNC_FORDEV);
3503334Sgs150176 	ASSERT(ssbdp->pbuf == NULL);
3513334Sgs150176 	ssbdp->pbuf = txbuf_item;
3523334Sgs150176 	srp->tx_next = NEXT(slot, srp->desc.nslots);
3533334Sgs150176 	pktp->txbuf_item = NULL;
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	/*
3563334Sgs150176 	 * Setting hardware send buffer descriptor
3570Sstevel@tonic-gate 	 */
3583334Sgs150176 	hw_sbd_p->host_buf_addr = txbuf->buf.cookie.dmac_laddress;
3593334Sgs150176 	hw_sbd_p->len = txbuf->copy_len;
3603334Sgs150176 	if (pktp->vlan_tci != 0) {
3613334Sgs150176 		hw_sbd_p->vlan_tci = pktp->vlan_tci;
362*3534Szh199473 		hw_sbd_p->host_buf_addr += VLAN_TAGSZ;
3633334Sgs150176 		hw_sbd_p->flags |= SBD_FLAG_VLAN_TAG;
3643334Sgs150176 	}
3653334Sgs150176 	if (pktp->pflags & HCK_IPV4_HDRCKSUM)
3663334Sgs150176 		hw_sbd_p->flags |= SBD_FLAG_IP_CKSUM;
3673334Sgs150176 	if (pktp->pflags & HCK_FULLCKSUM)
3683334Sgs150176 		hw_sbd_p->flags |= SBD_FLAG_TCP_UDP_CKSUM;
3693334Sgs150176 	hw_sbd_p->flags |= SBD_FLAG_PACKET_END;
3703334Sgs150176 }
3713334Sgs150176 
3723334Sgs150176 /*
3733334Sgs150176  * Send a message by copying it into a preallocated (and premapped) buffer
3743334Sgs150176  */
375*3534Szh199473 static void bge_send_copy(bge_t *bgep, sw_txbuf_t *txbuf, mblk_t *mp);
3763334Sgs150176 #pragma	inline(bge_send_copy)
3773334Sgs150176 
3783334Sgs150176 static void
379*3534Szh199473 bge_send_copy(bge_t *bgep, sw_txbuf_t *txbuf, mblk_t *mp)
3803334Sgs150176 {
3813334Sgs150176 	mblk_t *bp;
3823334Sgs150176 	uint32_t mblen;
3833334Sgs150176 	char *pbuf;
3843334Sgs150176 
3853334Sgs150176 	txbuf->copy_len = 0;
3863334Sgs150176 	pbuf = DMA_VPTR(txbuf->buf);
387*3534Szh199473 	for (bp = mp; bp != NULL; bp = bp->b_cont) {
3883334Sgs150176 		if ((mblen = MBLKL(bp)) == 0)
3893334Sgs150176 			continue;
390*3534Szh199473 		ASSERT(txbuf->copy_len + mblen <=
391*3534Szh199473 		    bgep->chipid.snd_buff_size);
392*3534Szh199473 		bcopy(bp->b_rptr, pbuf, mblen);
393*3534Szh199473 		pbuf += mblen;
394*3534Szh199473 		txbuf->copy_len += mblen;
3950Sstevel@tonic-gate 	}
3963334Sgs150176 }
3973334Sgs150176 
3983334Sgs150176 /*
3993334Sgs150176  * Fill the Tx buffer descriptors and trigger the h/w transmission
4003334Sgs150176  */
4013334Sgs150176 static void
4023334Sgs150176 bge_send_serial(bge_t *bgep, send_ring_t *srp)
4033334Sgs150176 {
4043334Sgs150176 	send_pkt_t *pktp;
4053334Sgs150176 	uint64_t txfill_next;
4063334Sgs150176 	uint32_t count;
4073334Sgs150176 	uint32_t tx_next;
4083334Sgs150176 	sw_sbd_t *ssbdp;
4093334Sgs150176 	bge_status_t *bsp;
4103334Sgs150176 
4113334Sgs150176 	/*
4123334Sgs150176 	 * Try to hold the tx lock:
4133334Sgs150176 	 *	If we are in an interrupt context, use mutex_enter() to
4143334Sgs150176 	 *	ensure quick response for tx in interrupt context;
4153334Sgs150176 	 *	Otherwise, use mutex_tryenter() to serialize this h/w tx
4163334Sgs150176 	 *	BD filling and transmission triggering task.
4173334Sgs150176 	 */
4183334Sgs150176 	if (servicing_interrupt() != 0)
4193334Sgs150176 		mutex_enter(srp->tx_lock);
4203334Sgs150176 	else if (mutex_tryenter(srp->tx_lock) == 0)
4213334Sgs150176 		return;		/* already in process	*/
4223334Sgs150176 
4233334Sgs150176 	bsp = DMA_VPTR(bgep->status_block);
4243334Sgs150176 	txfill_next = srp->txfill_next;
425*3534Szh199473 start_tx:
4263334Sgs150176 	tx_next = srp->tx_next;
4273334Sgs150176 	ssbdp = &srp->sw_sbds[tx_next];
4283334Sgs150176 	for (count = 0; count < bgep->param_drain_max; ++count) {
4293334Sgs150176 		pktp = &srp->pktp[txfill_next];
4303334Sgs150176 		if (!pktp->tx_ready) {
4313334Sgs150176 			if (count == 0)
4323334Sgs150176 				srp->tx_block++;
4333334Sgs150176 			break;
4343334Sgs150176 		}
4353334Sgs150176 
4363334Sgs150176 		/*
4373334Sgs150176 		 * If there are no enough BDs: try to recycle more
4383334Sgs150176 		 */
4393334Sgs150176 		if (srp->tx_free <= 1)
4403334Sgs150176 			bge_recycle(bgep, bsp);
4413334Sgs150176 
4423334Sgs150176 		/*
4433334Sgs150176 		 * Reserved required BDs: 1 is enough
4443334Sgs150176 		 */
4453334Sgs150176 		if (!bge_atomic_reserve(&srp->tx_free, 1)) {
4463334Sgs150176 			srp->tx_nobd++;
4473334Sgs150176 			break;
4483334Sgs150176 		}
4493334Sgs150176 
4503334Sgs150176 		/*
4513334Sgs150176 		 * Filling the tx BD
4523334Sgs150176 		 */
4533334Sgs150176 		bge_send_fill_txbd(srp, pktp);
4543334Sgs150176 		txfill_next = NEXT(txfill_next, BGE_SEND_BUF_MAX);
4553334Sgs150176 		pktp->tx_ready = B_FALSE;
4563334Sgs150176 	}
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	/*
4593334Sgs150176 	 * Trigger h/w to start transmission.
4600Sstevel@tonic-gate 	 */
4613334Sgs150176 	if (count != 0) {
4623334Sgs150176 		bge_atomic_sub64(&srp->tx_flow, count);
4633334Sgs150176 		if (tx_next + count > srp->desc.nslots) {
4643334Sgs150176 			(void) ddi_dma_sync(ssbdp->desc.dma_hdl,  0,
4653334Sgs150176 			    (srp->desc.nslots - tx_next) * sizeof (bge_sbd_t),
4663334Sgs150176 			    DDI_DMA_SYNC_FORDEV);
4673334Sgs150176 			count -= srp->desc.nslots - tx_next;
4683334Sgs150176 			ssbdp = &srp->sw_sbds[0];
4693334Sgs150176 		}
4703334Sgs150176 		(void) ddi_dma_sync(ssbdp->desc.dma_hdl,  0,
4713334Sgs150176 		    count*sizeof (bge_sbd_t), DDI_DMA_SYNC_FORDEV);
4723334Sgs150176 		bge_mbx_put(bgep, srp->chip_mbx_reg, srp->tx_next);
4733334Sgs150176 		srp->txfill_next = txfill_next;
4743334Sgs150176 		bgep->watchdog++;
475*3534Szh199473 		if (srp->tx_flow != 0 && srp->tx_free > 1)
476*3534Szh199473 			goto start_tx;
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
4793334Sgs150176 	mutex_exit(srp->tx_lock);
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate static boolean_t
4830Sstevel@tonic-gate bge_send(bge_t *bgep, mblk_t *mp)
4840Sstevel@tonic-gate {
4853334Sgs150176 	uint_t ring = 0;	/* use ring 0 */
4860Sstevel@tonic-gate 	send_ring_t *srp;
4870Sstevel@tonic-gate 	struct ether_vlan_header *ehp;
4883334Sgs150176 	bge_queue_item_t *txbuf_item;
4893334Sgs150176 	sw_txbuf_t *txbuf;
4903334Sgs150176 	send_pkt_t *pktp;
4913334Sgs150176 	uint64_t pkt_slot;
4923334Sgs150176 	uint16_t vlan_tci;
4933334Sgs150176 	uint32_t pflags;
494*3534Szh199473 	char *pbuf;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	ASSERT(mp->b_next == NULL);
4973334Sgs150176 	srp = &bgep->send[ring];
4983334Sgs150176 
4993334Sgs150176 	/*
5003334Sgs150176 	 * Get a s/w tx buffer first
5013334Sgs150176 	 */
5023334Sgs150176 	txbuf_item = bge_get_txbuf(bgep, srp);
5033334Sgs150176 	if (txbuf_item == NULL) {
5043334Sgs150176 		/* no tx buffer available */
5053334Sgs150176 		srp->tx_nobuf++;
5063334Sgs150176 		bgep->tx_resched_needed = B_TRUE;
5073334Sgs150176 		bge_send_serial(bgep, srp);
5083334Sgs150176 		return (B_FALSE);
5093334Sgs150176 	}
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	/*
5123334Sgs150176 	 * Copy all mp fragments to the pkt buffer
5130Sstevel@tonic-gate 	 */
5143334Sgs150176 	txbuf = txbuf_item->item;
515*3534Szh199473 	bge_send_copy(bgep, txbuf, mp);
516*3534Szh199473 
517*3534Szh199473 	/*
518*3534Szh199473 	 * Determine if the packet is VLAN tagged.
519*3534Szh199473 	 */
520*3534Szh199473 	ASSERT(txbuf->copy_len >= sizeof (struct ether_header));
521*3534Szh199473 	pbuf = DMA_VPTR(txbuf->buf);
522*3534Szh199473 
523*3534Szh199473 	ehp = (struct ether_vlan_header *)pbuf;
524*3534Szh199473 	if (ehp->ether_tpid == htons(ETHERTYPE_VLAN)) {
525*3534Szh199473 		/* Strip the vlan tag */
526*3534Szh199473 		vlan_tci = ntohs(ehp->ether_tci);
527*3534Szh199473 		pbuf = memmove(pbuf + VLAN_TAGSZ, pbuf, 2 * ETHERADDRL);
528*3534Szh199473 		txbuf->copy_len -= VLAN_TAGSZ;
529*3534Szh199473 	} else
530*3534Szh199473 		vlan_tci = 0;
5313334Sgs150176 
5323334Sgs150176 	/*
5333334Sgs150176 	 * Retrieve checksum offloading info.
5343334Sgs150176 	 */
5353334Sgs150176 	hcksum_retrieve(mp, NULL, NULL, NULL, NULL, NULL, NULL, &pflags);
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	/*
5383334Sgs150176 	 * Calculate pseudo checksum if needed.
5390Sstevel@tonic-gate 	 */
5403334Sgs150176 	if ((pflags & HCK_FULLCKSUM) &&
5413334Sgs150176 	    (bgep->chipid.flags & CHIP_FLAG_PARTIAL_CSUM))
542*3534Szh199473 		bge_pseudo_cksum((uint8_t *)pbuf);
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	/*
5453334Sgs150176 	 * Packet buffer is ready to send: get and fill pkt info
5460Sstevel@tonic-gate 	 */
5473334Sgs150176 	pkt_slot = bge_atomic_next(&srp->txpkt_next, BGE_SEND_BUF_MAX);
5483334Sgs150176 	pktp = &srp->pktp[pkt_slot];
5493334Sgs150176 	ASSERT(pktp->txbuf_item == NULL);
5503334Sgs150176 	pktp->txbuf_item = txbuf_item;
5513334Sgs150176 	pktp->vlan_tci = vlan_tci;
5523334Sgs150176 	pktp->pflags = pflags;
5533334Sgs150176 	atomic_inc_64(&srp->tx_flow);
5543334Sgs150176 	ASSERT(pktp->tx_ready == B_FALSE);
5553334Sgs150176 	pktp->tx_ready = B_TRUE;
5560Sstevel@tonic-gate 
5573334Sgs150176 	/*
5583334Sgs150176 	 * Filling the h/w bd and trigger the h/w to start transmission
5593334Sgs150176 	 */
5603334Sgs150176 	bge_send_serial(bgep, srp);
5613334Sgs150176 
5623334Sgs150176 	/*
5633334Sgs150176 	 * We've copied the contents, the message can be freed right away
5643334Sgs150176 	 */
5653334Sgs150176 	freemsg(mp);
5663334Sgs150176 
5670Sstevel@tonic-gate 	return (B_TRUE);
5680Sstevel@tonic-gate }
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate uint_t
5713334Sgs150176 bge_send_drain(caddr_t arg)
5720Sstevel@tonic-gate {
5733334Sgs150176 	uint_t ring = 0;	/* use ring 0 */
5740Sstevel@tonic-gate 	bge_t *bgep;
5753334Sgs150176 	send_ring_t *srp;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	bgep = (bge_t *)arg;
5783334Sgs150176 	BGE_TRACE(("bge_send_drain($%p)", (void *)bgep));
5790Sstevel@tonic-gate 
5803334Sgs150176 	srp = &bgep->send[ring];
5813334Sgs150176 	bge_send_serial(bgep, srp);
5820Sstevel@tonic-gate 
5833334Sgs150176 	if (bgep->tx_resched_needed &&
5843334Sgs150176 	    (srp->tx_flow < srp->tx_buffers_low) &&
5853334Sgs150176 	    (bgep->bge_mac_state == BGE_MAC_STARTED)) {
5862311Sseb 		mac_tx_update(bgep->mh);
5873334Sgs150176 		bgep->tx_resched_needed = B_FALSE;
5883334Sgs150176 		bgep->tx_resched++;
5890Sstevel@tonic-gate 	}
5900Sstevel@tonic-gate 
5911200Sly149593 	return (DDI_INTR_CLAIMED);
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate /*
5950Sstevel@tonic-gate  * bge_m_tx() - send a chain of packets
5960Sstevel@tonic-gate  */
5970Sstevel@tonic-gate mblk_t *
5980Sstevel@tonic-gate bge_m_tx(void *arg, mblk_t *mp)
5990Sstevel@tonic-gate {
6000Sstevel@tonic-gate 	bge_t *bgep = arg;		/* private device info	*/
6010Sstevel@tonic-gate 	mblk_t *next;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	BGE_TRACE(("bge_m_tx($%p, $%p)", arg, (void *)mp));
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	ASSERT(mp != NULL);
6060Sstevel@tonic-gate 	ASSERT(bgep->bge_mac_state == BGE_MAC_STARTED);
6070Sstevel@tonic-gate 
6083170Sml149210 	rw_enter(bgep->errlock, RW_READER);
6090Sstevel@tonic-gate 	if (bgep->bge_chip_state != BGE_CHIP_RUNNING) {
6100Sstevel@tonic-gate 		BGE_DEBUG(("bge_m_tx: chip not running"));
6113170Sml149210 		freemsgchain(mp);
6123170Sml149210 		mp = NULL;
6130Sstevel@tonic-gate 	}
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	while (mp != NULL) {
6160Sstevel@tonic-gate 		next = mp->b_next;
6170Sstevel@tonic-gate 		mp->b_next = NULL;
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 		if (!bge_send(bgep, mp)) {
6200Sstevel@tonic-gate 			mp->b_next = next;
6210Sstevel@tonic-gate 			break;
6220Sstevel@tonic-gate 		}
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 		mp = next;
6250Sstevel@tonic-gate 	}
626152Sly149593 	rw_exit(bgep->errlock);
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	return (mp);
6290Sstevel@tonic-gate }
630