17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
562387023Sdduvall * Common Development and Distribution License (the "License").
662387023Sdduvall * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
2162387023Sdduvall
227c478bd9Sstevel@tonic-gate /*
23*087a28d1SDavid Gwynne * Copyright (c) 2010-2013, by Broadcom, Inc.
24*087a28d1SDavid Gwynne * All Rights Reserved.
25*087a28d1SDavid Gwynne */
26*087a28d1SDavid Gwynne
27*087a28d1SDavid Gwynne /*
28*087a28d1SDavid Gwynne * Copyright (c) 2002, 2010, Oracle and/or its affiliates.
29*087a28d1SDavid Gwynne * All rights reserved.
307c478bd9Sstevel@tonic-gate */
317c478bd9Sstevel@tonic-gate
32f724721bSzh199473 #include "bge_impl.h"
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate * The transmit-side code uses an allocation process which is similar
377c478bd9Sstevel@tonic-gate * to some theme park roller-coaster rides, where riders sit in cars
387c478bd9Sstevel@tonic-gate * that can go individually, but work better in a train.
397c478bd9Sstevel@tonic-gate *
407c478bd9Sstevel@tonic-gate * 1) RESERVE a place - this doesn't refer to any specific car or
417c478bd9Sstevel@tonic-gate * seat, just that you will get a ride. The attempt to RESERVE a
427c478bd9Sstevel@tonic-gate * place can fail if all spaces in all cars are already committed.
437c478bd9Sstevel@tonic-gate *
447c478bd9Sstevel@tonic-gate * 2) Prepare yourself; this may take an arbitrary (but not unbounded)
457c478bd9Sstevel@tonic-gate * time, and you can back out at this stage, in which case you must
467c478bd9Sstevel@tonic-gate * give up (RENOUNCE) your place.
477c478bd9Sstevel@tonic-gate *
487c478bd9Sstevel@tonic-gate * 3) CLAIM your space - a specific car (the next sequentially
497c478bd9Sstevel@tonic-gate * numbered one) is allocated at this stage, and is guaranteed
507c478bd9Sstevel@tonic-gate * to be part of the next train to depart. Once you've done
517c478bd9Sstevel@tonic-gate * this, you can't back out, nor wait for any external event
527c478bd9Sstevel@tonic-gate * or resource.
537c478bd9Sstevel@tonic-gate *
547c478bd9Sstevel@tonic-gate * 4) Occupy your car - when all CLAIMED cars are OCCUPIED, they
557c478bd9Sstevel@tonic-gate * all depart together as a single train!
567c478bd9Sstevel@tonic-gate *
577c478bd9Sstevel@tonic-gate * 5) At the end of the ride, you climb out of the car and RENOUNCE
587c478bd9Sstevel@tonic-gate * your right to it, so that it can be recycled for another rider.
597c478bd9Sstevel@tonic-gate *
607c478bd9Sstevel@tonic-gate * For each rider, these have to occur in this order, but the riders
617c478bd9Sstevel@tonic-gate * don't have to stay in the same order at each stage. In particular,
627c478bd9Sstevel@tonic-gate * they may overtake each other between RESERVING a place and CLAIMING
637c478bd9Sstevel@tonic-gate * it, or between CLAIMING and OCCUPYING a space.
647c478bd9Sstevel@tonic-gate *
657c478bd9Sstevel@tonic-gate * Once a car is CLAIMED, the train currently being assembled can't go
667c478bd9Sstevel@tonic-gate * without that car (this guarantees that the cars in a single train
677c478bd9Sstevel@tonic-gate * make up a consecutively-numbered set). Therefore, when any train
687c478bd9Sstevel@tonic-gate * leaves, we know there can't be any riders in transit between CLAIMING
697c478bd9Sstevel@tonic-gate * and OCCUPYING their cars. There can be some who have RESERVED but
707c478bd9Sstevel@tonic-gate * not yet CLAIMED their places. That's OK, though, because they'll go
717c478bd9Sstevel@tonic-gate * into the next train.
727c478bd9Sstevel@tonic-gate */
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate #define BGE_DBG BGE_DBG_SEND /* debug flag for this code */
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate * ========== Send-side recycle routines ==========
787c478bd9Sstevel@tonic-gate */
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate * Recycle all the completed buffers in the specified send ring up to
827c478bd9Sstevel@tonic-gate * (but not including) the consumer index in the status block.
837c478bd9Sstevel@tonic-gate *
847c478bd9Sstevel@tonic-gate * This function must advance (srp->tc_next) AND adjust (srp->tx_free)
857c478bd9Sstevel@tonic-gate * to account for the packets it has recycled.
867c478bd9Sstevel@tonic-gate *
877c478bd9Sstevel@tonic-gate * This is a trivial version that just does that and nothing more, but
887c478bd9Sstevel@tonic-gate * it suffices while there's only one method for sending messages (by
897c478bd9Sstevel@tonic-gate * copying) and that method doesn't need any special per-buffer action
907c478bd9Sstevel@tonic-gate * for recycling.
917c478bd9Sstevel@tonic-gate */
922adae974Syong tan - Sun Microsystems - Beijing China static boolean_t bge_recycle_ring(bge_t *bgep, send_ring_t *srp);
937c478bd9Sstevel@tonic-gate #pragma inline(bge_recycle_ring)
947c478bd9Sstevel@tonic-gate
952adae974Syong tan - Sun Microsystems - Beijing China static boolean_t
bge_recycle_ring(bge_t * bgep,send_ring_t * srp)967c478bd9Sstevel@tonic-gate bge_recycle_ring(bge_t *bgep, send_ring_t *srp)
977c478bd9Sstevel@tonic-gate {
98931dca7dSgs150176 sw_sbd_t *ssbdp;
99931dca7dSgs150176 bge_queue_item_t *buf_item;
100931dca7dSgs150176 bge_queue_item_t *buf_item_head;
101931dca7dSgs150176 bge_queue_item_t *buf_item_tail;
102931dca7dSgs150176 bge_queue_t *txbuf_queue;
1037c478bd9Sstevel@tonic-gate uint64_t slot;
1047c478bd9Sstevel@tonic-gate uint64_t n;
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate ASSERT(mutex_owned(srp->tc_lock));
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate * We're about to release one or more places :-)
1107c478bd9Sstevel@tonic-gate * These ASSERTions check that our invariants still hold:
1117c478bd9Sstevel@tonic-gate * there must always be at least one free place
1127c478bd9Sstevel@tonic-gate * at this point, there must be at least one place NOT free
1137c478bd9Sstevel@tonic-gate * we're not about to free more places than were claimed!
1147c478bd9Sstevel@tonic-gate */
11527c3238fSyong tan - Sun Microsystems - Beijing China ASSERT(srp->tx_free <= srp->desc.nslots);
1167c478bd9Sstevel@tonic-gate
117931dca7dSgs150176 buf_item_head = buf_item_tail = NULL;
118931dca7dSgs150176 for (n = 0, slot = srp->tc_next; slot != *srp->cons_index_p;
119931dca7dSgs150176 slot = NEXT(slot, srp->desc.nslots)) {
120931dca7dSgs150176 ssbdp = &srp->sw_sbds[slot];
121931dca7dSgs150176 ASSERT(ssbdp->pbuf != NULL);
122931dca7dSgs150176 buf_item = ssbdp->pbuf;
123931dca7dSgs150176 if (buf_item_head == NULL)
124931dca7dSgs150176 buf_item_head = buf_item_tail = buf_item;
125931dca7dSgs150176 else {
126931dca7dSgs150176 buf_item_tail->next = buf_item;
127931dca7dSgs150176 buf_item_tail = buf_item;
128931dca7dSgs150176 }
129931dca7dSgs150176 ssbdp->pbuf = NULL;
130931dca7dSgs150176 n++;
131931dca7dSgs150176 }
132931dca7dSgs150176 if (n == 0)
1332adae974Syong tan - Sun Microsystems - Beijing China return (B_FALSE);
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate * Reset the watchdog count: to 0 if all buffers are
1377c478bd9Sstevel@tonic-gate * now free, or to 1 if some are still outstanding.
1387c478bd9Sstevel@tonic-gate * Note: non-synchonised access here means we may get
1397c478bd9Sstevel@tonic-gate * the "wrong" answer, but only in a harmless fashion
1407c478bd9Sstevel@tonic-gate * (i.e. we deactivate the watchdog because all buffers
1417c478bd9Sstevel@tonic-gate * are apparently free, even though another thread may
1427c478bd9Sstevel@tonic-gate * have claimed one before we leave here; in this case
1437c478bd9Sstevel@tonic-gate * the watchdog will restart on the next send() call).
1447c478bd9Sstevel@tonic-gate */
1452adae974Syong tan - Sun Microsystems - Beijing China bgep->watchdog = (slot == srp->tx_next) ? 0 : 1;
1462adae974Syong tan - Sun Microsystems - Beijing China
1472adae974Syong tan - Sun Microsystems - Beijing China /*
1482adae974Syong tan - Sun Microsystems - Beijing China * Update recycle index and free tx BD number
1492adae974Syong tan - Sun Microsystems - Beijing China */
1502adae974Syong tan - Sun Microsystems - Beijing China srp->tc_next = slot;
1512adae974Syong tan - Sun Microsystems - Beijing China ASSERT(srp->tx_free + n <= srp->desc.nslots);
1522adae974Syong tan - Sun Microsystems - Beijing China bge_atomic_renounce(&srp->tx_free, n);
153931dca7dSgs150176
154931dca7dSgs150176 /*
155931dca7dSgs150176 * Return tx buffers to buffer push queue
156931dca7dSgs150176 */
157931dca7dSgs150176 txbuf_queue = srp->txbuf_push_queue;
158931dca7dSgs150176 mutex_enter(txbuf_queue->lock);
159931dca7dSgs150176 buf_item_tail->next = txbuf_queue->head;
160931dca7dSgs150176 txbuf_queue->head = buf_item_head;
161931dca7dSgs150176 txbuf_queue->count += n;
162931dca7dSgs150176 mutex_exit(txbuf_queue->lock);
163931dca7dSgs150176
164931dca7dSgs150176 /*
165931dca7dSgs150176 * Check if we need exchange the tx buffer push and pop queue
166931dca7dSgs150176 */
167931dca7dSgs150176 if ((srp->txbuf_pop_queue->count < srp->tx_buffers_low) &&
168931dca7dSgs150176 (srp->txbuf_pop_queue->count < txbuf_queue->count)) {
169931dca7dSgs150176 srp->txbuf_push_queue = srp->txbuf_pop_queue;
170931dca7dSgs150176 srp->txbuf_pop_queue = txbuf_queue;
171931dca7dSgs150176 }
172931dca7dSgs150176
173542d98abSzh199473 if (srp->tx_flow != 0 || bgep->tx_resched_needed)
174931dca7dSgs150176 ddi_trigger_softintr(bgep->drain_id);
1752adae974Syong tan - Sun Microsystems - Beijing China
1762adae974Syong tan - Sun Microsystems - Beijing China return (B_TRUE);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate * Recycle all returned slots in all rings.
1817c478bd9Sstevel@tonic-gate *
1827c478bd9Sstevel@tonic-gate * To give priority to low-numbered rings, whenever we have recycled any
1837c478bd9Sstevel@tonic-gate * slots in any ring except 0, we restart scanning again from ring 0.
1847c478bd9Sstevel@tonic-gate * Thus, for example, if rings 0, 3, and 10 are carrying traffic, the
1857c478bd9Sstevel@tonic-gate * pattern of recycles might go 0, 3, 10, 3, 0, 10, 0:
1867c478bd9Sstevel@tonic-gate *
1877c478bd9Sstevel@tonic-gate * 0 found some - recycle them
1887c478bd9Sstevel@tonic-gate * 1..2 none found
1897c478bd9Sstevel@tonic-gate * 3 found some - recycle them and restart scan
1907c478bd9Sstevel@tonic-gate * 0..9 none found
1917c478bd9Sstevel@tonic-gate * 10 found some - recycle them and restart scan
1927c478bd9Sstevel@tonic-gate * 0..2 none found
1937c478bd9Sstevel@tonic-gate * 3 found some more - recycle them and restart scan
1947c478bd9Sstevel@tonic-gate * 0 found some more - recycle them
1957c478bd9Sstevel@tonic-gate * 0..9 none found
1967c478bd9Sstevel@tonic-gate * 10 found some more - recycle them and restart scan
1977c478bd9Sstevel@tonic-gate * 0 found some more - recycle them
1987c478bd9Sstevel@tonic-gate * 1..15 none found
1997c478bd9Sstevel@tonic-gate *
2007c478bd9Sstevel@tonic-gate * The routine returns only when a complete scan has been performed
2017c478bd9Sstevel@tonic-gate * without finding any slots to recycle.
2027c478bd9Sstevel@tonic-gate *
2037c478bd9Sstevel@tonic-gate * Note: the expression (BGE_SEND_RINGS_USED > 1) yields a compile-time
2047c478bd9Sstevel@tonic-gate * constant and allows the compiler to optimise away the outer do-loop
2057c478bd9Sstevel@tonic-gate * if only one send ring is being used.
2067c478bd9Sstevel@tonic-gate */
2072adae974Syong tan - Sun Microsystems - Beijing China boolean_t bge_recycle(bge_t *bgep, bge_status_t *bsp);
2087c478bd9Sstevel@tonic-gate #pragma no_inline(bge_recycle)
2097c478bd9Sstevel@tonic-gate
2102adae974Syong tan - Sun Microsystems - Beijing China boolean_t
bge_recycle(bge_t * bgep,bge_status_t * bsp)2117c478bd9Sstevel@tonic-gate bge_recycle(bge_t *bgep, bge_status_t *bsp)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate send_ring_t *srp;
2147c478bd9Sstevel@tonic-gate uint64_t ring;
2157c478bd9Sstevel@tonic-gate uint64_t tx_rings = bgep->chipid.tx_rings;
2162adae974Syong tan - Sun Microsystems - Beijing China boolean_t tx_done = B_FALSE;
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate restart:
2197c478bd9Sstevel@tonic-gate ring = 0;
2207c478bd9Sstevel@tonic-gate srp = &bgep->send[ring];
2217c478bd9Sstevel@tonic-gate do {
2227c478bd9Sstevel@tonic-gate /*
2237c478bd9Sstevel@tonic-gate * For each ring, (srp->cons_index_p) points to the
2247c478bd9Sstevel@tonic-gate * proper index within the status block (which has
2257c478bd9Sstevel@tonic-gate * already been sync'd by the caller).
2267c478bd9Sstevel@tonic-gate */
2277c478bd9Sstevel@tonic-gate ASSERT(srp->cons_index_p == SEND_INDEX_P(bsp, ring));
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate if (*srp->cons_index_p == srp->tc_next)
2307c478bd9Sstevel@tonic-gate continue; /* no slots to recycle */
231931dca7dSgs150176 if (mutex_tryenter(srp->tc_lock) == 0)
232931dca7dSgs150176 continue; /* already in process */
2332adae974Syong tan - Sun Microsystems - Beijing China tx_done |= bge_recycle_ring(bgep, srp);
2347c478bd9Sstevel@tonic-gate mutex_exit(srp->tc_lock);
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate * Restart from ring 0, if we're not on ring 0 already.
2387c478bd9Sstevel@tonic-gate * As H/W selects send BDs totally based on priority and
2397c478bd9Sstevel@tonic-gate * available BDs on the higher priority ring are always
2407c478bd9Sstevel@tonic-gate * selected first, driver should keep consistence with H/W
2417c478bd9Sstevel@tonic-gate * and gives lower-numbered ring with higher priority.
2427c478bd9Sstevel@tonic-gate */
2437c478bd9Sstevel@tonic-gate if (tx_rings > 1 && ring > 0)
2447c478bd9Sstevel@tonic-gate goto restart;
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate /*
2477c478bd9Sstevel@tonic-gate * Loop over all rings (if there *are* multiple rings)
2487c478bd9Sstevel@tonic-gate */
2497c478bd9Sstevel@tonic-gate } while (++srp, ++ring < tx_rings);
2502adae974Syong tan - Sun Microsystems - Beijing China
2512adae974Syong tan - Sun Microsystems - Beijing China return (tx_done);
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate /*
2567c478bd9Sstevel@tonic-gate * ========== Send-side transmit routines ==========
2577c478bd9Sstevel@tonic-gate */
258256e438eSzh199473 #define TCP_CKSUM_OFFSET 16
259256e438eSzh199473 #define UDP_CKSUM_OFFSET 6
260256e438eSzh199473
261256e438eSzh199473 static void
bge_pseudo_cksum(uint8_t * buf)262256e438eSzh199473 bge_pseudo_cksum(uint8_t *buf)
263256e438eSzh199473 {
264256e438eSzh199473 uint32_t cksum;
265256e438eSzh199473 uint16_t iphl;
266256e438eSzh199473 uint16_t proto;
267256e438eSzh199473
268256e438eSzh199473 /*
269256e438eSzh199473 * Point it to the ip header.
270256e438eSzh199473 */
271256e438eSzh199473 buf += sizeof (struct ether_header);
272256e438eSzh199473
273256e438eSzh199473 /*
274256e438eSzh199473 * Calculate the pseudo-header checksum.
275256e438eSzh199473 */
276256e438eSzh199473 iphl = 4 * (buf[0] & 0xF);
277256e438eSzh199473 cksum = (((uint16_t)buf[2])<<8) + buf[3] - iphl;
278256e438eSzh199473 cksum += proto = buf[9];
279256e438eSzh199473 cksum += (((uint16_t)buf[12])<<8) + buf[13];
280256e438eSzh199473 cksum += (((uint16_t)buf[14])<<8) + buf[15];
281256e438eSzh199473 cksum += (((uint16_t)buf[16])<<8) + buf[17];
282256e438eSzh199473 cksum += (((uint16_t)buf[18])<<8) + buf[19];
283256e438eSzh199473 cksum = (cksum>>16) + (cksum & 0xFFFF);
284256e438eSzh199473 cksum = (cksum>>16) + (cksum & 0xFFFF);
285256e438eSzh199473
286256e438eSzh199473 /*
287256e438eSzh199473 * Point it to the TCP/UDP header, and
288256e438eSzh199473 * update the checksum field.
289256e438eSzh199473 */
290256e438eSzh199473 buf += iphl + ((proto == IPPROTO_TCP) ?
291256e438eSzh199473 TCP_CKSUM_OFFSET : UDP_CKSUM_OFFSET);
292256e438eSzh199473
2934a06b59fSyt223700 /*
2944a06b59fSyt223700 * A real possibility that pointer cast is a problem.
2954a06b59fSyt223700 * Should be fixed when we know the code better.
2964a06b59fSyt223700 * E_BAD_PTR_CAST_ALIGN is added to make it temporarily clean.
2974a06b59fSyt223700 */
298256e438eSzh199473 *(uint16_t *)buf = htons((uint16_t)cksum);
299256e438eSzh199473 }
300256e438eSzh199473
301931dca7dSgs150176 static bge_queue_item_t *
bge_get_txbuf(bge_t * bgep,send_ring_t * srp)302931dca7dSgs150176 bge_get_txbuf(bge_t *bgep, send_ring_t *srp)
303931dca7dSgs150176 {
304931dca7dSgs150176 bge_queue_item_t *txbuf_item;
305931dca7dSgs150176 bge_queue_t *txbuf_queue;
3067c478bd9Sstevel@tonic-gate
307931dca7dSgs150176 txbuf_queue = srp->txbuf_pop_queue;
308931dca7dSgs150176 mutex_enter(txbuf_queue->lock);
309931dca7dSgs150176 if (txbuf_queue->count == 0) {
310931dca7dSgs150176 mutex_exit(txbuf_queue->lock);
311931dca7dSgs150176 txbuf_queue = srp->txbuf_push_queue;
312931dca7dSgs150176 mutex_enter(txbuf_queue->lock);
313931dca7dSgs150176 if (txbuf_queue->count == 0) {
314931dca7dSgs150176 mutex_exit(txbuf_queue->lock);
315931dca7dSgs150176 /* Try to allocate more tx buffers */
316931dca7dSgs150176 if (srp->tx_array < srp->tx_array_max) {
317931dca7dSgs150176 mutex_enter(srp->tx_lock);
318931dca7dSgs150176 txbuf_item = bge_alloc_txbuf_array(bgep, srp);
319931dca7dSgs150176 mutex_exit(srp->tx_lock);
320931dca7dSgs150176 } else
321931dca7dSgs150176 txbuf_item = NULL;
322931dca7dSgs150176 return (txbuf_item);
323931dca7dSgs150176 }
324931dca7dSgs150176 }
325931dca7dSgs150176 txbuf_item = txbuf_queue->head;
326931dca7dSgs150176 txbuf_queue->head = (bge_queue_item_t *)txbuf_item->next;
327931dca7dSgs150176 txbuf_queue->count--;
328931dca7dSgs150176 mutex_exit(txbuf_queue->lock);
329931dca7dSgs150176 txbuf_item->next = NULL;
330931dca7dSgs150176
331931dca7dSgs150176 return (txbuf_item);
332931dca7dSgs150176 }
333931dca7dSgs150176
334931dca7dSgs150176 /*
335931dca7dSgs150176 * Send a message by copying it into a preallocated (and premapped) buffer
336931dca7dSgs150176 */
337542d98abSzh199473 static void bge_send_copy(bge_t *bgep, sw_txbuf_t *txbuf, mblk_t *mp);
338931dca7dSgs150176 #pragma inline(bge_send_copy)
339931dca7dSgs150176
340931dca7dSgs150176 static void
bge_send_copy(bge_t * bgep,sw_txbuf_t * txbuf,mblk_t * mp)341542d98abSzh199473 bge_send_copy(bge_t *bgep, sw_txbuf_t *txbuf, mblk_t *mp)
342931dca7dSgs150176 {
343931dca7dSgs150176 mblk_t *bp;
344931dca7dSgs150176 uint32_t mblen;
345931dca7dSgs150176 char *pbuf;
346931dca7dSgs150176
347931dca7dSgs150176 txbuf->copy_len = 0;
348931dca7dSgs150176 pbuf = DMA_VPTR(txbuf->buf);
349542d98abSzh199473 for (bp = mp; bp != NULL; bp = bp->b_cont) {
350931dca7dSgs150176 if ((mblen = MBLKL(bp)) == 0)
351931dca7dSgs150176 continue;
352542d98abSzh199473 ASSERT(txbuf->copy_len + mblen <=
353542d98abSzh199473 bgep->chipid.snd_buff_size);
354931dca7dSgs150176 bcopy(bp->b_rptr, pbuf, mblen);
355931dca7dSgs150176 pbuf += mblen;
356542d98abSzh199473 txbuf->copy_len += mblen;
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate
3607c478bd9Sstevel@tonic-gate /*
361931dca7dSgs150176 * Fill the Tx buffer descriptors and trigger the h/w transmission
3627c478bd9Sstevel@tonic-gate */
363931dca7dSgs150176 static void
bge_send_serial(bge_t * bgep,send_ring_t * srp)364931dca7dSgs150176 bge_send_serial(bge_t *bgep, send_ring_t *srp)
365931dca7dSgs150176 {
366931dca7dSgs150176 send_pkt_t *pktp;
367931dca7dSgs150176 uint64_t txfill_next;
368931dca7dSgs150176 uint32_t count;
369931dca7dSgs150176 uint32_t tx_next;
370931dca7dSgs150176 sw_sbd_t *ssbdp;
371931dca7dSgs150176 bge_status_t *bsp;
3722adae974Syong tan - Sun Microsystems - Beijing China bge_sbd_t *hw_sbd_p;
3732adae974Syong tan - Sun Microsystems - Beijing China bge_queue_item_t *txbuf_item;
3742adae974Syong tan - Sun Microsystems - Beijing China sw_txbuf_t *txbuf;
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate /*
377931dca7dSgs150176 * Try to hold the tx lock:
378931dca7dSgs150176 * If we are in an interrupt context, use mutex_enter() to
379931dca7dSgs150176 * ensure quick response for tx in interrupt context;
380931dca7dSgs150176 * Otherwise, use mutex_tryenter() to serialize this h/w tx
381931dca7dSgs150176 * BD filling and transmission triggering task.
3827c478bd9Sstevel@tonic-gate */
383931dca7dSgs150176 if (servicing_interrupt() != 0)
384931dca7dSgs150176 mutex_enter(srp->tx_lock);
385931dca7dSgs150176 else if (mutex_tryenter(srp->tx_lock) == 0)
386931dca7dSgs150176 return; /* already in process */
387931dca7dSgs150176
388931dca7dSgs150176 bsp = DMA_VPTR(bgep->status_block);
389931dca7dSgs150176 txfill_next = srp->txfill_next;
390931dca7dSgs150176 tx_next = srp->tx_next;
3912adae974Syong tan - Sun Microsystems - Beijing China start_tx:
392931dca7dSgs150176 for (count = 0; count < bgep->param_drain_max; ++count) {
393931dca7dSgs150176 pktp = &srp->pktp[txfill_next];
394931dca7dSgs150176 if (!pktp->tx_ready) {
395931dca7dSgs150176 if (count == 0)
396931dca7dSgs150176 srp->tx_block++;
397931dca7dSgs150176 break;
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate
400931dca7dSgs150176 /*
401931dca7dSgs150176 * If there are no enough BDs: try to recycle more
402931dca7dSgs150176 */
403931dca7dSgs150176 if (srp->tx_free <= 1)
4042adae974Syong tan - Sun Microsystems - Beijing China (void) bge_recycle(bgep, bsp);
405931dca7dSgs150176
406931dca7dSgs150176 /*
407931dca7dSgs150176 * Reserved required BDs: 1 is enough
408931dca7dSgs150176 */
409931dca7dSgs150176 if (!bge_atomic_reserve(&srp->tx_free, 1)) {
410931dca7dSgs150176 srp->tx_nobd++;
411931dca7dSgs150176 break;
412256e438eSzh199473 }
4137c478bd9Sstevel@tonic-gate
414931dca7dSgs150176 /*
415931dca7dSgs150176 * Filling the tx BD
416931dca7dSgs150176 */
4172adae974Syong tan - Sun Microsystems - Beijing China
4182adae974Syong tan - Sun Microsystems - Beijing China /*
4192adae974Syong tan - Sun Microsystems - Beijing China * Go straight to claiming our already-reserved places
4202adae974Syong tan - Sun Microsystems - Beijing China * on the train!
4212adae974Syong tan - Sun Microsystems - Beijing China */
4222adae974Syong tan - Sun Microsystems - Beijing China ASSERT(pktp->txbuf_item != NULL);
4232adae974Syong tan - Sun Microsystems - Beijing China txbuf_item = pktp->txbuf_item;
4242adae974Syong tan - Sun Microsystems - Beijing China pktp->txbuf_item = NULL;
425931dca7dSgs150176 pktp->tx_ready = B_FALSE;
4262adae974Syong tan - Sun Microsystems - Beijing China
4272adae974Syong tan - Sun Microsystems - Beijing China txbuf = txbuf_item->item;
4282adae974Syong tan - Sun Microsystems - Beijing China ASSERT(txbuf->copy_len != 0);
4292adae974Syong tan - Sun Microsystems - Beijing China (void) ddi_dma_sync(txbuf->buf.dma_hdl, 0,
4302adae974Syong tan - Sun Microsystems - Beijing China txbuf->copy_len, DDI_DMA_SYNC_FORDEV);
4312adae974Syong tan - Sun Microsystems - Beijing China
4322adae974Syong tan - Sun Microsystems - Beijing China ssbdp = &srp->sw_sbds[tx_next];
4332adae974Syong tan - Sun Microsystems - Beijing China ASSERT(ssbdp->pbuf == NULL);
4342adae974Syong tan - Sun Microsystems - Beijing China ssbdp->pbuf = txbuf_item;
4352adae974Syong tan - Sun Microsystems - Beijing China
4362adae974Syong tan - Sun Microsystems - Beijing China /*
4372adae974Syong tan - Sun Microsystems - Beijing China * Setting hardware send buffer descriptor
4382adae974Syong tan - Sun Microsystems - Beijing China */
4392adae974Syong tan - Sun Microsystems - Beijing China hw_sbd_p = DMA_VPTR(ssbdp->desc);
4402adae974Syong tan - Sun Microsystems - Beijing China hw_sbd_p->flags = 0;
4412adae974Syong tan - Sun Microsystems - Beijing China hw_sbd_p->host_buf_addr = txbuf->buf.cookie.dmac_laddress;
4422adae974Syong tan - Sun Microsystems - Beijing China hw_sbd_p->len = txbuf->copy_len;
4432adae974Syong tan - Sun Microsystems - Beijing China if (pktp->vlan_tci != 0) {
4442adae974Syong tan - Sun Microsystems - Beijing China hw_sbd_p->vlan_tci = pktp->vlan_tci;
4452adae974Syong tan - Sun Microsystems - Beijing China hw_sbd_p->host_buf_addr += VLAN_TAGSZ;
4462adae974Syong tan - Sun Microsystems - Beijing China hw_sbd_p->flags |= SBD_FLAG_VLAN_TAG;
4472adae974Syong tan - Sun Microsystems - Beijing China }
4482adae974Syong tan - Sun Microsystems - Beijing China if (pktp->pflags & HCK_IPV4_HDRCKSUM)
4492adae974Syong tan - Sun Microsystems - Beijing China hw_sbd_p->flags |= SBD_FLAG_IP_CKSUM;
4502adae974Syong tan - Sun Microsystems - Beijing China if (pktp->pflags & HCK_FULLCKSUM)
4512adae974Syong tan - Sun Microsystems - Beijing China hw_sbd_p->flags |= SBD_FLAG_TCP_UDP_CKSUM;
452*087a28d1SDavid Gwynne if (!(bgep->chipid.flags & CHIP_FLAG_NO_JUMBO) &&
453*087a28d1SDavid Gwynne (DEVICE_5717_SERIES_CHIPSETS(bgep) ||
454*087a28d1SDavid Gwynne DEVICE_5725_SERIES_CHIPSETS(bgep)) &&
455*087a28d1SDavid Gwynne (txbuf->copy_len > ETHERMAX))
456*087a28d1SDavid Gwynne hw_sbd_p->flags |= SBD_FLAG_JMB_PKT;
4572adae974Syong tan - Sun Microsystems - Beijing China hw_sbd_p->flags |= SBD_FLAG_PACKET_END;
4582adae974Syong tan - Sun Microsystems - Beijing China
4592adae974Syong tan - Sun Microsystems - Beijing China txfill_next = NEXT(txfill_next, BGE_SEND_BUF_MAX);
4602adae974Syong tan - Sun Microsystems - Beijing China tx_next = NEXT(tx_next, srp->desc.nslots);
461931dca7dSgs150176 }
462931dca7dSgs150176
463931dca7dSgs150176 /*
464931dca7dSgs150176 * Trigger h/w to start transmission.
465931dca7dSgs150176 */
466931dca7dSgs150176 if (count != 0) {
467931dca7dSgs150176 bge_atomic_sub64(&srp->tx_flow, count);
4682adae974Syong tan - Sun Microsystems - Beijing China srp->txfill_next = txfill_next;
4692adae974Syong tan - Sun Microsystems - Beijing China
4702adae974Syong tan - Sun Microsystems - Beijing China if (srp->tx_next > tx_next) {
471931dca7dSgs150176 (void) ddi_dma_sync(ssbdp->desc.dma_hdl, 0,
4722adae974Syong tan - Sun Microsystems - Beijing China (srp->desc.nslots - srp->tx_next) *
4732adae974Syong tan - Sun Microsystems - Beijing China sizeof (bge_sbd_t),
474931dca7dSgs150176 DDI_DMA_SYNC_FORDEV);
4752adae974Syong tan - Sun Microsystems - Beijing China count -= srp->desc.nslots - srp->tx_next;
476931dca7dSgs150176 ssbdp = &srp->sw_sbds[0];
477931dca7dSgs150176 }
478931dca7dSgs150176 (void) ddi_dma_sync(ssbdp->desc.dma_hdl, 0,
479931dca7dSgs150176 count*sizeof (bge_sbd_t), DDI_DMA_SYNC_FORDEV);
4802adae974Syong tan - Sun Microsystems - Beijing China bge_mbx_put(bgep, srp->chip_mbx_reg, tx_next);
4812adae974Syong tan - Sun Microsystems - Beijing China srp->tx_next = tx_next;
4822adae974Syong tan - Sun Microsystems - Beijing China atomic_or_32(&bgep->watchdog, 1);
4832adae974Syong tan - Sun Microsystems - Beijing China
484542d98abSzh199473 if (srp->tx_flow != 0 && srp->tx_free > 1)
485542d98abSzh199473 goto start_tx;
486931dca7dSgs150176 }
487931dca7dSgs150176
488931dca7dSgs150176 mutex_exit(srp->tx_lock);
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate
491da14cebeSEric Cheng mblk_t *
bge_ring_tx(void * arg,mblk_t * mp)492da14cebeSEric Cheng bge_ring_tx(void *arg, mblk_t *mp)
4937c478bd9Sstevel@tonic-gate {
494da14cebeSEric Cheng send_ring_t *srp = arg;
495da14cebeSEric Cheng bge_t *bgep = srp->bgep;
4967c478bd9Sstevel@tonic-gate struct ether_vlan_header *ehp;
497931dca7dSgs150176 bge_queue_item_t *txbuf_item;
498931dca7dSgs150176 sw_txbuf_t *txbuf;
499931dca7dSgs150176 send_pkt_t *pktp;
500931dca7dSgs150176 uint64_t pkt_slot;
501931dca7dSgs150176 uint16_t vlan_tci;
502931dca7dSgs150176 uint32_t pflags;
503542d98abSzh199473 char *pbuf;
5047c478bd9Sstevel@tonic-gate
5057c478bd9Sstevel@tonic-gate ASSERT(mp->b_next == NULL);
506931dca7dSgs150176
507931dca7dSgs150176 /*
508931dca7dSgs150176 * Get a s/w tx buffer first
509931dca7dSgs150176 */
510931dca7dSgs150176 txbuf_item = bge_get_txbuf(bgep, srp);
511931dca7dSgs150176 if (txbuf_item == NULL) {
512931dca7dSgs150176 /* no tx buffer available */
513931dca7dSgs150176 srp->tx_nobuf++;
514931dca7dSgs150176 bgep->tx_resched_needed = B_TRUE;
515931dca7dSgs150176 bge_send_serial(bgep, srp);
516da14cebeSEric Cheng return (mp);
517931dca7dSgs150176 }
5187c478bd9Sstevel@tonic-gate
5197c478bd9Sstevel@tonic-gate /*
520931dca7dSgs150176 * Copy all mp fragments to the pkt buffer
52108ee25aeSdr146992 */
522931dca7dSgs150176 txbuf = txbuf_item->item;
523542d98abSzh199473 bge_send_copy(bgep, txbuf, mp);
524542d98abSzh199473
525542d98abSzh199473 /*
526542d98abSzh199473 * Determine if the packet is VLAN tagged.
527542d98abSzh199473 */
528542d98abSzh199473 ASSERT(txbuf->copy_len >= sizeof (struct ether_header));
529542d98abSzh199473 pbuf = DMA_VPTR(txbuf->buf);
530542d98abSzh199473
5314a06b59fSyt223700 ehp = (void *)pbuf;
532542d98abSzh199473 if (ehp->ether_tpid == htons(ETHERTYPE_VLAN)) {
533542d98abSzh199473 /* Strip the vlan tag */
534542d98abSzh199473 vlan_tci = ntohs(ehp->ether_tci);
535542d98abSzh199473 pbuf = memmove(pbuf + VLAN_TAGSZ, pbuf, 2 * ETHERADDRL);
536542d98abSzh199473 txbuf->copy_len -= VLAN_TAGSZ;
537542d98abSzh199473 } else
538542d98abSzh199473 vlan_tci = 0;
5397c478bd9Sstevel@tonic-gate
5407c478bd9Sstevel@tonic-gate /*
541931dca7dSgs150176 * Retrieve checksum offloading info.
5427c478bd9Sstevel@tonic-gate */
5430dc2366fSVenugopal Iyer mac_hcksum_get(mp, NULL, NULL, NULL, NULL, &pflags);
5447c478bd9Sstevel@tonic-gate
5457c478bd9Sstevel@tonic-gate /*
546931dca7dSgs150176 * Calculate pseudo checksum if needed.
5477c478bd9Sstevel@tonic-gate */
548931dca7dSgs150176 if ((pflags & HCK_FULLCKSUM) &&
549931dca7dSgs150176 (bgep->chipid.flags & CHIP_FLAG_PARTIAL_CSUM))
550542d98abSzh199473 bge_pseudo_cksum((uint8_t *)pbuf);
5517c478bd9Sstevel@tonic-gate
5527c478bd9Sstevel@tonic-gate /*
553931dca7dSgs150176 * Packet buffer is ready to send: get and fill pkt info
5547c478bd9Sstevel@tonic-gate */
555931dca7dSgs150176 pkt_slot = bge_atomic_next(&srp->txpkt_next, BGE_SEND_BUF_MAX);
556931dca7dSgs150176 pktp = &srp->pktp[pkt_slot];
557931dca7dSgs150176 ASSERT(pktp->txbuf_item == NULL);
558931dca7dSgs150176 pktp->txbuf_item = txbuf_item;
559931dca7dSgs150176 pktp->vlan_tci = vlan_tci;
560931dca7dSgs150176 pktp->pflags = pflags;
561931dca7dSgs150176 atomic_inc_64(&srp->tx_flow);
562931dca7dSgs150176 ASSERT(pktp->tx_ready == B_FALSE);
563931dca7dSgs150176 pktp->tx_ready = B_TRUE;
5647c478bd9Sstevel@tonic-gate
565931dca7dSgs150176 /*
566931dca7dSgs150176 * Filling the h/w bd and trigger the h/w to start transmission
567931dca7dSgs150176 */
568931dca7dSgs150176 bge_send_serial(bgep, srp);
569931dca7dSgs150176
570da14cebeSEric Cheng srp->pushed_bytes += MBLKL(mp);
571da14cebeSEric Cheng
572931dca7dSgs150176 /*
573931dca7dSgs150176 * We've copied the contents, the message can be freed right away
574931dca7dSgs150176 */
5757c478bd9Sstevel@tonic-gate freemsg(mp);
576da14cebeSEric Cheng return (NULL);
577da14cebeSEric Cheng }
578931dca7dSgs150176
579da14cebeSEric Cheng static mblk_t *
bge_send(bge_t * bgep,mblk_t * mp)580da14cebeSEric Cheng bge_send(bge_t *bgep, mblk_t *mp)
581da14cebeSEric Cheng {
582da14cebeSEric Cheng send_ring_t *ring;
583da14cebeSEric Cheng
584da14cebeSEric Cheng ring = &bgep->send[0]; /* ring 0 */
585da14cebeSEric Cheng
586da14cebeSEric Cheng return (bge_ring_tx(ring, mp));
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate
5897c478bd9Sstevel@tonic-gate uint_t
bge_send_drain(caddr_t arg)590931dca7dSgs150176 bge_send_drain(caddr_t arg)
5917c478bd9Sstevel@tonic-gate {
592931dca7dSgs150176 uint_t ring = 0; /* use ring 0 */
5937c478bd9Sstevel@tonic-gate bge_t *bgep;
594931dca7dSgs150176 send_ring_t *srp;
5957c478bd9Sstevel@tonic-gate
5964a06b59fSyt223700 bgep = (void *)arg;
597931dca7dSgs150176 BGE_TRACE(("bge_send_drain($%p)", (void *)bgep));
5987c478bd9Sstevel@tonic-gate
599931dca7dSgs150176 srp = &bgep->send[ring];
600931dca7dSgs150176 bge_send_serial(bgep, srp);
6017c478bd9Sstevel@tonic-gate
602931dca7dSgs150176 if (bgep->tx_resched_needed &&
603931dca7dSgs150176 (srp->tx_flow < srp->tx_buffers_low) &&
604931dca7dSgs150176 (bgep->bge_mac_state == BGE_MAC_STARTED)) {
605ba2e4443Sseb mac_tx_update(bgep->mh);
606931dca7dSgs150176 bgep->tx_resched_needed = B_FALSE;
607931dca7dSgs150176 bgep->tx_resched++;
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate
610fbdb9ef6Sly149593 return (DDI_INTR_CLAIMED);
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate
6137c478bd9Sstevel@tonic-gate /*
6147c478bd9Sstevel@tonic-gate * bge_m_tx() - send a chain of packets
6157c478bd9Sstevel@tonic-gate */
6167c478bd9Sstevel@tonic-gate mblk_t *
bge_m_tx(void * arg,mblk_t * mp)6177c478bd9Sstevel@tonic-gate bge_m_tx(void *arg, mblk_t *mp)
6187c478bd9Sstevel@tonic-gate {
6197c478bd9Sstevel@tonic-gate bge_t *bgep = arg; /* private device info */
6207c478bd9Sstevel@tonic-gate mblk_t *next;
6217c478bd9Sstevel@tonic-gate
6227c478bd9Sstevel@tonic-gate BGE_TRACE(("bge_m_tx($%p, $%p)", arg, (void *)mp));
6237c478bd9Sstevel@tonic-gate
6247c478bd9Sstevel@tonic-gate ASSERT(mp != NULL);
6257c478bd9Sstevel@tonic-gate ASSERT(bgep->bge_mac_state == BGE_MAC_STARTED);
6267c478bd9Sstevel@tonic-gate
6277c966ec8Sml149210 rw_enter(bgep->errlock, RW_READER);
62846ffce64Syong tan - Sun Microsystems - Beijing China if ((bgep->bge_chip_state != BGE_CHIP_RUNNING) ||
62946ffce64Syong tan - Sun Microsystems - Beijing China !(bgep->param_link_up)) {
63046ffce64Syong tan - Sun Microsystems - Beijing China BGE_DEBUG(("bge_m_tx: chip not running or link down"));
6317c966ec8Sml149210 freemsgchain(mp);
6327c966ec8Sml149210 mp = NULL;
6337c478bd9Sstevel@tonic-gate }
6347c478bd9Sstevel@tonic-gate
6357c478bd9Sstevel@tonic-gate while (mp != NULL) {
6367c478bd9Sstevel@tonic-gate next = mp->b_next;
6377c478bd9Sstevel@tonic-gate mp->b_next = NULL;
6387c478bd9Sstevel@tonic-gate
639da14cebeSEric Cheng if ((mp = bge_send(bgep, mp)) != NULL) {
6407c478bd9Sstevel@tonic-gate mp->b_next = next;
6417c478bd9Sstevel@tonic-gate break;
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate
6447c478bd9Sstevel@tonic-gate mp = next;
6457c478bd9Sstevel@tonic-gate }
646e49ca41eSly149593 rw_exit(bgep->errlock);
6477c478bd9Sstevel@tonic-gate
6487c478bd9Sstevel@tonic-gate return (mp);
6497c478bd9Sstevel@tonic-gate }
650