xref: /onnv-gate/usr/src/uts/common/io/bge/bge_send.c (revision 3334:f7ad363e66bb)
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 /*
231200Sly149593  * Copyright 2006 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 {
95*3334Sgs150176 	sw_sbd_t *ssbdp;
96*3334Sgs150176 	bge_queue_item_t *buf_item;
97*3334Sgs150176 	bge_queue_item_t *buf_item_head;
98*3334Sgs150176 	bge_queue_item_t *buf_item_tail;
99*3334Sgs150176 	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);
113*3334Sgs150176 	ASSERT(srp->tx_free < srp->desc.nslots);
1140Sstevel@tonic-gate 
115*3334Sgs150176 	buf_item_head = buf_item_tail = NULL;
116*3334Sgs150176 	for (n = 0, slot = srp->tc_next; slot != *srp->cons_index_p;
117*3334Sgs150176 	    slot = NEXT(slot, srp->desc.nslots)) {
118*3334Sgs150176 		ssbdp = &srp->sw_sbds[slot];
119*3334Sgs150176 		ASSERT(ssbdp->pbuf != NULL);
120*3334Sgs150176 		buf_item = ssbdp->pbuf;
121*3334Sgs150176 		if (buf_item_head == NULL)
122*3334Sgs150176 			buf_item_head = buf_item_tail = buf_item;
123*3334Sgs150176 		else {
124*3334Sgs150176 			buf_item_tail->next = buf_item;
125*3334Sgs150176 			buf_item_tail = buf_item;
126*3334Sgs150176 		}
127*3334Sgs150176 		ssbdp->pbuf = NULL;
128*3334Sgs150176 		n++;
129*3334Sgs150176 	}
130*3334Sgs150176 	if (n == 0)
131*3334Sgs150176 		return;
132*3334Sgs150176 
133*3334Sgs150176 	/*
134*3334Sgs150176 	 * Update recycle index and free tx BD number
135*3334Sgs150176 	 */
1360Sstevel@tonic-gate 	srp->tc_next = slot;
137*3334Sgs150176 	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;
151*3334Sgs150176 
152*3334Sgs150176 	/*
153*3334Sgs150176 	 * Return tx buffers to buffer push queue
154*3334Sgs150176 	 */
155*3334Sgs150176 	txbuf_queue = srp->txbuf_push_queue;
156*3334Sgs150176 	mutex_enter(txbuf_queue->lock);
157*3334Sgs150176 	buf_item_tail->next = txbuf_queue->head;
158*3334Sgs150176 	txbuf_queue->head = buf_item_head;
159*3334Sgs150176 	txbuf_queue->count += n;
160*3334Sgs150176 	mutex_exit(txbuf_queue->lock);
161*3334Sgs150176 
162*3334Sgs150176 	/*
163*3334Sgs150176 	 * Check if we need exchange the tx buffer push and pop queue
164*3334Sgs150176 	 */
165*3334Sgs150176 	if ((srp->txbuf_pop_queue->count < srp->tx_buffers_low) &&
166*3334Sgs150176 	    (srp->txbuf_pop_queue->count < txbuf_queue->count)) {
167*3334Sgs150176 		srp->txbuf_push_queue = srp->txbuf_pop_queue;
168*3334Sgs150176 		srp->txbuf_pop_queue = txbuf_queue;
169*3334Sgs150176 	}
170*3334Sgs150176 
171*3334Sgs150176 	if (bgep->tx_resched_needed)
172*3334Sgs150176 		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	*/
226*3334Sgs150176 		if (mutex_tryenter(srp->tc_lock) == 0)
227*3334Sgs150176 			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 
289*3334Sgs150176 static bge_queue_item_t *
290*3334Sgs150176 bge_get_txbuf(bge_t *bgep, send_ring_t *srp)
291*3334Sgs150176 {
292*3334Sgs150176 	bge_queue_item_t *txbuf_item;
293*3334Sgs150176 	bge_queue_t *txbuf_queue;
2940Sstevel@tonic-gate 
295*3334Sgs150176 	txbuf_queue = srp->txbuf_pop_queue;
296*3334Sgs150176 	mutex_enter(txbuf_queue->lock);
297*3334Sgs150176 	if (txbuf_queue->count == 0) {
298*3334Sgs150176 		mutex_exit(txbuf_queue->lock);
299*3334Sgs150176 		txbuf_queue = srp->txbuf_push_queue;
300*3334Sgs150176 		mutex_enter(txbuf_queue->lock);
301*3334Sgs150176 		if (txbuf_queue->count == 0) {
302*3334Sgs150176 			mutex_exit(txbuf_queue->lock);
303*3334Sgs150176 			/* Try to allocate more tx buffers */
304*3334Sgs150176 			if (srp->tx_array < srp->tx_array_max) {
305*3334Sgs150176 				mutex_enter(srp->tx_lock);
306*3334Sgs150176 				txbuf_item = bge_alloc_txbuf_array(bgep, srp);
307*3334Sgs150176 				mutex_exit(srp->tx_lock);
308*3334Sgs150176 			} else
309*3334Sgs150176 				txbuf_item = NULL;
310*3334Sgs150176 			return (txbuf_item);
311*3334Sgs150176 		}
312*3334Sgs150176 	}
313*3334Sgs150176 	txbuf_item = txbuf_queue->head;
314*3334Sgs150176 	txbuf_queue->head = (bge_queue_item_t *)txbuf_item->next;
315*3334Sgs150176 	txbuf_queue->count--;
316*3334Sgs150176 	mutex_exit(txbuf_queue->lock);
317*3334Sgs150176 	txbuf_item->next = NULL;
318*3334Sgs150176 
319*3334Sgs150176 	return (txbuf_item);
320*3334Sgs150176 }
321*3334Sgs150176 
322*3334Sgs150176 static void bge_send_fill_txbd(send_ring_t *srp, send_pkt_t *pktp);
323*3334Sgs150176 #pragma	inline(bge_send_fill_txbd)
324*3334Sgs150176 
325*3334Sgs150176 static void
326*3334Sgs150176 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;
330*3334Sgs150176 	bge_queue_item_t *txbuf_item;
331*3334Sgs150176 	sw_txbuf_t *txbuf;
3320Sstevel@tonic-gate 	uint64_t slot;
3330Sstevel@tonic-gate 
334*3334Sgs150176 	ASSERT(mutex_owned(srp->tx_lock));
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	/*
337*3334Sgs150176 	 * Go straight to claiming our already-reserved places
338*3334Sgs150176 	 * on the train!
3390Sstevel@tonic-gate 	 */
340*3334Sgs150176 	ASSERT(pktp->txbuf_item != NULL);
341*3334Sgs150176 	txbuf_item = pktp->txbuf_item;
342*3334Sgs150176 	txbuf = txbuf_item->item;
343*3334Sgs150176 	slot = srp->tx_next;
3440Sstevel@tonic-gate 	ssbdp = &srp->sw_sbds[slot];
345*3334Sgs150176 	hw_sbd_p = DMA_VPTR(ssbdp->desc);
346*3334Sgs150176 	hw_sbd_p->flags = 0;
347*3334Sgs150176 	ASSERT(txbuf->copy_len != 0);
348*3334Sgs150176 	(void) ddi_dma_sync(txbuf->buf.dma_hdl,  0,
349*3334Sgs150176 	    txbuf->copy_len, DDI_DMA_SYNC_FORDEV);
350*3334Sgs150176 	ASSERT(ssbdp->pbuf == NULL);
351*3334Sgs150176 	ssbdp->pbuf = txbuf_item;
352*3334Sgs150176 	srp->tx_next = NEXT(slot, srp->desc.nslots);
353*3334Sgs150176 	pktp->txbuf_item = NULL;
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	/*
356*3334Sgs150176 	 * Setting hardware send buffer descriptor
3570Sstevel@tonic-gate 	 */
358*3334Sgs150176 	hw_sbd_p->host_buf_addr = txbuf->buf.cookie.dmac_laddress;
359*3334Sgs150176 	hw_sbd_p->len = txbuf->copy_len;
360*3334Sgs150176 	if (pktp->vlan_tci != 0) {
361*3334Sgs150176 		hw_sbd_p->vlan_tci = pktp->vlan_tci;
362*3334Sgs150176 		hw_sbd_p->flags |= SBD_FLAG_VLAN_TAG;
363*3334Sgs150176 	}
364*3334Sgs150176 	if (pktp->pflags & HCK_IPV4_HDRCKSUM)
365*3334Sgs150176 		hw_sbd_p->flags |= SBD_FLAG_IP_CKSUM;
366*3334Sgs150176 	if (pktp->pflags & HCK_FULLCKSUM)
367*3334Sgs150176 		hw_sbd_p->flags |= SBD_FLAG_TCP_UDP_CKSUM;
368*3334Sgs150176 	hw_sbd_p->flags |= SBD_FLAG_PACKET_END;
369*3334Sgs150176 }
370*3334Sgs150176 
371*3334Sgs150176 /*
372*3334Sgs150176  * Send a message by copying it into a preallocated (and premapped) buffer
373*3334Sgs150176  */
374*3334Sgs150176 static void bge_send_copy(bge_t *bgep, sw_txbuf_t *txbuf, mblk_t *mp,
375*3334Sgs150176     uint16_t tci);
376*3334Sgs150176 #pragma	inline(bge_send_copy)
377*3334Sgs150176 
378*3334Sgs150176 static void
379*3334Sgs150176 bge_send_copy(bge_t *bgep, sw_txbuf_t *txbuf, mblk_t *mp, uint16_t tci)
380*3334Sgs150176 {
381*3334Sgs150176 	mblk_t *bp;
382*3334Sgs150176 	uint32_t mblen;
383*3334Sgs150176 	char *pbuf;
384*3334Sgs150176 
385*3334Sgs150176 	txbuf->copy_len = 0;
386*3334Sgs150176 	pbuf = DMA_VPTR(txbuf->buf);
3872135Szh199473 	bp = mp;
3882135Szh199473 	if (tci != 0) {
389*3334Sgs150176 		mblen = MBLKL(bp);
3902135Szh199473 		ASSERT(mblen >= 2 * ETHERADDRL + VLAN_TAGSZ);
391*3334Sgs150176 		bcopy(bp->b_rptr, pbuf, 2 * ETHERADDRL);
392*3334Sgs150176 		pbuf += 2 * ETHERADDRL;
393*3334Sgs150176 		txbuf->copy_len += 2 * ETHERADDRL;
394*3334Sgs150176 		mblen -= 2 * ETHERADDRL + VLAN_TAGSZ;
395*3334Sgs150176 		if ((txbuf->copy_len += mblen) <= bgep->chipid.ethmax_size) {
396*3334Sgs150176 			bcopy(bp->b_wptr - mblen, pbuf, mblen);
397*3334Sgs150176 			pbuf += mblen;
3982135Szh199473 		}
3992135Szh199473 		bp = bp->b_cont;
4002135Szh199473 	}
4012135Szh199473 	for (; bp != NULL; bp = bp->b_cont) {
402*3334Sgs150176 		if ((mblen = MBLKL(bp)) == 0)
403*3334Sgs150176 			continue;
404*3334Sgs150176 		if ((txbuf->copy_len += mblen) <= bgep->chipid.ethmax_size) {
405*3334Sgs150176 			bcopy(bp->b_rptr, pbuf, mblen);
406*3334Sgs150176 			pbuf += mblen;
4070Sstevel@tonic-gate 		}
4080Sstevel@tonic-gate 	}
409*3334Sgs150176 }
410*3334Sgs150176 
411*3334Sgs150176 /*
412*3334Sgs150176  * Fill the Tx buffer descriptors and trigger the h/w transmission
413*3334Sgs150176  */
414*3334Sgs150176 static void
415*3334Sgs150176 bge_send_serial(bge_t *bgep, send_ring_t *srp)
416*3334Sgs150176 {
417*3334Sgs150176 	send_pkt_t *pktp;
418*3334Sgs150176 	uint64_t txfill_next;
419*3334Sgs150176 	uint32_t count;
420*3334Sgs150176 	uint32_t tx_next;
421*3334Sgs150176 	sw_sbd_t *ssbdp;
422*3334Sgs150176 	bge_status_t *bsp;
423*3334Sgs150176 
424*3334Sgs150176 	/*
425*3334Sgs150176 	 * Try to hold the tx lock:
426*3334Sgs150176 	 *	If we are in an interrupt context, use mutex_enter() to
427*3334Sgs150176 	 *	ensure quick response for tx in interrupt context;
428*3334Sgs150176 	 *	Otherwise, use mutex_tryenter() to serialize this h/w tx
429*3334Sgs150176 	 *	BD filling and transmission triggering task.
430*3334Sgs150176 	 */
431*3334Sgs150176 	if (servicing_interrupt() != 0)
432*3334Sgs150176 		mutex_enter(srp->tx_lock);
433*3334Sgs150176 	else if (mutex_tryenter(srp->tx_lock) == 0)
434*3334Sgs150176 		return;		/* already in process	*/
435*3334Sgs150176 
436*3334Sgs150176 	bsp = DMA_VPTR(bgep->status_block);
437*3334Sgs150176 	txfill_next = srp->txfill_next;
438*3334Sgs150176 	tx_next = srp->tx_next;
439*3334Sgs150176 	ssbdp = &srp->sw_sbds[tx_next];
440*3334Sgs150176 	for (count = 0; count < bgep->param_drain_max; ++count) {
441*3334Sgs150176 		pktp = &srp->pktp[txfill_next];
442*3334Sgs150176 		if (!pktp->tx_ready) {
443*3334Sgs150176 			if (count == 0)
444*3334Sgs150176 				srp->tx_block++;
445*3334Sgs150176 			break;
446*3334Sgs150176 		}
447*3334Sgs150176 
448*3334Sgs150176 		/*
449*3334Sgs150176 		 * If there are no enough BDs: try to recycle more
450*3334Sgs150176 		 */
451*3334Sgs150176 		if (srp->tx_free <= 1)
452*3334Sgs150176 			bge_recycle(bgep, bsp);
453*3334Sgs150176 
454*3334Sgs150176 		/*
455*3334Sgs150176 		 * Reserved required BDs: 1 is enough
456*3334Sgs150176 		 */
457*3334Sgs150176 		if (!bge_atomic_reserve(&srp->tx_free, 1)) {
458*3334Sgs150176 			srp->tx_nobd++;
459*3334Sgs150176 			break;
460*3334Sgs150176 		}
461*3334Sgs150176 
462*3334Sgs150176 		/*
463*3334Sgs150176 		 * Filling the tx BD
464*3334Sgs150176 		 */
465*3334Sgs150176 		bge_send_fill_txbd(srp, pktp);
466*3334Sgs150176 		txfill_next = NEXT(txfill_next, BGE_SEND_BUF_MAX);
467*3334Sgs150176 		pktp->tx_ready = B_FALSE;
468*3334Sgs150176 	}
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	/*
471*3334Sgs150176 	 * Trigger h/w to start transmission.
4720Sstevel@tonic-gate 	 */
473*3334Sgs150176 	if (count != 0) {
474*3334Sgs150176 		bge_atomic_sub64(&srp->tx_flow, count);
475*3334Sgs150176 		if (tx_next + count > srp->desc.nslots) {
476*3334Sgs150176 			(void) ddi_dma_sync(ssbdp->desc.dma_hdl,  0,
477*3334Sgs150176 			    (srp->desc.nslots - tx_next) * sizeof (bge_sbd_t),
478*3334Sgs150176 			    DDI_DMA_SYNC_FORDEV);
479*3334Sgs150176 			count -= srp->desc.nslots - tx_next;
480*3334Sgs150176 			ssbdp = &srp->sw_sbds[0];
481*3334Sgs150176 		}
482*3334Sgs150176 		(void) ddi_dma_sync(ssbdp->desc.dma_hdl,  0,
483*3334Sgs150176 		    count*sizeof (bge_sbd_t), DDI_DMA_SYNC_FORDEV);
484*3334Sgs150176 		bge_mbx_put(bgep, srp->chip_mbx_reg, srp->tx_next);
485*3334Sgs150176 		srp->txfill_next = txfill_next;
486*3334Sgs150176 		bgep->watchdog++;
4870Sstevel@tonic-gate 	}
4880Sstevel@tonic-gate 
489*3334Sgs150176 	mutex_exit(srp->tx_lock);
4900Sstevel@tonic-gate }
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate static boolean_t
4930Sstevel@tonic-gate bge_send(bge_t *bgep, mblk_t *mp)
4940Sstevel@tonic-gate {
495*3334Sgs150176 	uint_t ring = 0;	/* use ring 0 */
4960Sstevel@tonic-gate 	send_ring_t *srp;
4970Sstevel@tonic-gate 	struct ether_vlan_header *ehp;
498*3334Sgs150176 	bge_queue_item_t *txbuf_item;
499*3334Sgs150176 	sw_txbuf_t *txbuf;
500*3334Sgs150176 	send_pkt_t *pktp;
501*3334Sgs150176 	uint64_t pkt_slot;
502*3334Sgs150176 	uint16_t vlan_tci;
503*3334Sgs150176 	uint32_t pflags;
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	ASSERT(mp->b_next == NULL);
506*3334Sgs150176 	srp = &bgep->send[ring];
507*3334Sgs150176 
508*3334Sgs150176 	/*
509*3334Sgs150176 	 * Get a s/w tx buffer first
510*3334Sgs150176 	 */
511*3334Sgs150176 	txbuf_item = bge_get_txbuf(bgep, srp);
512*3334Sgs150176 	if (txbuf_item == NULL) {
513*3334Sgs150176 		/* no tx buffer available */
514*3334Sgs150176 		srp->tx_nobuf++;
515*3334Sgs150176 		bgep->tx_resched_needed = B_TRUE;
516*3334Sgs150176 		bge_send_serial(bgep, srp);
517*3334Sgs150176 		return (B_FALSE);
518*3334Sgs150176 	}
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	/*
5210Sstevel@tonic-gate 	 * Determine if the packet is VLAN tagged.
5220Sstevel@tonic-gate 	 */
5230Sstevel@tonic-gate 	ASSERT(MBLKL(mp) >= sizeof (struct ether_header));
5240Sstevel@tonic-gate 	ehp = (struct ether_vlan_header *)mp->b_rptr;
525*3334Sgs150176 	if (ehp->ether_tpid == htons(ETHERTYPE_VLAN))
526*3334Sgs150176 		vlan_tci  = ntohs(ehp->ether_tci);
527*3334Sgs150176 	else
528*3334Sgs150176 		vlan_tci = 0;
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	/*
531*3334Sgs150176 	 * Copy all mp fragments to the pkt buffer
5320Sstevel@tonic-gate 	 */
533*3334Sgs150176 	txbuf = txbuf_item->item;
534*3334Sgs150176 	bge_send_copy(bgep, txbuf, mp, vlan_tci);
535*3334Sgs150176 	ASSERT(txbuf->copy_len <= bgep->chipid.ethmax_size);
536*3334Sgs150176 
537*3334Sgs150176 	/*
538*3334Sgs150176 	 * Retrieve checksum offloading info.
539*3334Sgs150176 	 */
540*3334Sgs150176 	hcksum_retrieve(mp, NULL, NULL, NULL, NULL, NULL, NULL, &pflags);
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	/*
543*3334Sgs150176 	 * Calculate pseudo checksum if needed.
5440Sstevel@tonic-gate 	 */
545*3334Sgs150176 	if ((pflags & HCK_FULLCKSUM) &&
546*3334Sgs150176 	    (bgep->chipid.flags & CHIP_FLAG_PARTIAL_CSUM))
547*3334Sgs150176 		bge_pseudo_cksum((uint8_t *)DMA_VPTR(txbuf->buf));
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	/*
550*3334Sgs150176 	 * Packet buffer is ready to send: get and fill pkt info
5510Sstevel@tonic-gate 	 */
552*3334Sgs150176 	pkt_slot = bge_atomic_next(&srp->txpkt_next, BGE_SEND_BUF_MAX);
553*3334Sgs150176 	pktp = &srp->pktp[pkt_slot];
554*3334Sgs150176 	ASSERT(pktp->txbuf_item == NULL);
555*3334Sgs150176 	pktp->txbuf_item = txbuf_item;
556*3334Sgs150176 	pktp->vlan_tci = vlan_tci;
557*3334Sgs150176 	pktp->pflags = pflags;
558*3334Sgs150176 	atomic_inc_64(&srp->tx_flow);
559*3334Sgs150176 	ASSERT(pktp->tx_ready == B_FALSE);
560*3334Sgs150176 	pktp->tx_ready = B_TRUE;
5610Sstevel@tonic-gate 
562*3334Sgs150176 	/*
563*3334Sgs150176 	 * Filling the h/w bd and trigger the h/w to start transmission
564*3334Sgs150176 	 */
565*3334Sgs150176 	bge_send_serial(bgep, srp);
566*3334Sgs150176 
567*3334Sgs150176 	/*
568*3334Sgs150176 	 * We've copied the contents, the message can be freed right away
569*3334Sgs150176 	 */
570*3334Sgs150176 	freemsg(mp);
571*3334Sgs150176 
5720Sstevel@tonic-gate 	return (B_TRUE);
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate uint_t
576*3334Sgs150176 bge_send_drain(caddr_t arg)
5770Sstevel@tonic-gate {
578*3334Sgs150176 	uint_t ring = 0;	/* use ring 0 */
5790Sstevel@tonic-gate 	bge_t *bgep;
580*3334Sgs150176 	send_ring_t *srp;
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	bgep = (bge_t *)arg;
583*3334Sgs150176 	BGE_TRACE(("bge_send_drain($%p)", (void *)bgep));
5840Sstevel@tonic-gate 
585*3334Sgs150176 	srp = &bgep->send[ring];
586*3334Sgs150176 	bge_send_serial(bgep, srp);
5870Sstevel@tonic-gate 
588*3334Sgs150176 	if (bgep->tx_resched_needed &&
589*3334Sgs150176 	    (srp->tx_flow < srp->tx_buffers_low) &&
590*3334Sgs150176 	    (bgep->bge_mac_state == BGE_MAC_STARTED)) {
5912311Sseb 		mac_tx_update(bgep->mh);
592*3334Sgs150176 		bgep->tx_resched_needed = B_FALSE;
593*3334Sgs150176 		bgep->tx_resched++;
5940Sstevel@tonic-gate 	}
5950Sstevel@tonic-gate 
5961200Sly149593 	return (DDI_INTR_CLAIMED);
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate /*
6000Sstevel@tonic-gate  * bge_m_tx() - send a chain of packets
6010Sstevel@tonic-gate  */
6020Sstevel@tonic-gate mblk_t *
6030Sstevel@tonic-gate bge_m_tx(void *arg, mblk_t *mp)
6040Sstevel@tonic-gate {
6050Sstevel@tonic-gate 	bge_t *bgep = arg;		/* private device info	*/
6060Sstevel@tonic-gate 	mblk_t *next;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	BGE_TRACE(("bge_m_tx($%p, $%p)", arg, (void *)mp));
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	ASSERT(mp != NULL);
6110Sstevel@tonic-gate 	ASSERT(bgep->bge_mac_state == BGE_MAC_STARTED);
6120Sstevel@tonic-gate 
6133170Sml149210 	rw_enter(bgep->errlock, RW_READER);
6140Sstevel@tonic-gate 	if (bgep->bge_chip_state != BGE_CHIP_RUNNING) {
6150Sstevel@tonic-gate 		BGE_DEBUG(("bge_m_tx: chip not running"));
6163170Sml149210 		freemsgchain(mp);
6173170Sml149210 		mp = NULL;
6180Sstevel@tonic-gate 	}
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	while (mp != NULL) {
6210Sstevel@tonic-gate 		next = mp->b_next;
6220Sstevel@tonic-gate 		mp->b_next = NULL;
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 		if (!bge_send(bgep, mp)) {
6250Sstevel@tonic-gate 			mp->b_next = next;
6260Sstevel@tonic-gate 			break;
6270Sstevel@tonic-gate 		}
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 		mp = next;
6300Sstevel@tonic-gate 	}
631152Sly149593 	rw_exit(bgep->errlock);
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	return (mp);
6340Sstevel@tonic-gate }
635