bge_send.c revision 381a2a9a387f449fab7d0c7e97c4184c26963abf
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * CDDL HEADER START
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * The contents of this file are subject to the terms of the
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Common Development and Distribution License (the "License").
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * You may not use this file except in compliance with the License.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * See the License for the specific language governing permissions
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * and limitations under the License.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * When distributing Covered Code, include this CDDL HEADER in each
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * If applicable, add the following below this CDDL HEADER, with the
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * fields enclosed by brackets "[]" replaced with your own identifying
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * information: Portions Copyright [yyyy] [name of copyright owner]
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * CDDL HEADER END
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Use is subject to license terms.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs#pragma ident "%Z%%M% %I% %E% SMI"
b83cd2c35abe58abb09c73f2ef35426f1384ad46Michael Speer * The transmit-side code uses an allocation process which is similar
b83cd2c35abe58abb09c73f2ef35426f1384ad46Michael Speer * to some theme park roller-coaster rides, where riders sit in cars
676f04004b15fc2872dfb0b7349d2d821a6eaa31Michael Speer * that can go individually, but work better in a train.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * 1) RESERVE a place - this doesn't refer to any specific car or
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * seat, just that you will get a ride. The attempt to RESERVE a
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * place can fail if all spaces in all cars are already committed.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * 2) Prepare yourself; this may take an arbitrary (but not unbounded)
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * time, and you can back out at this stage, in which case you must
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * give up (RENOUNCE) your place.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * 3) CLAIM your space - a specific car (the next sequentially
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * numbered one) is allocated at this stage, and is guaranteed
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * to be part of the next train to depart. Once you've done
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * this, you can't back out, nor wait for any external event
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * or resource.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * 4) Occupy your car - when all CLAIMED cars are OCCUPIED, they
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * all depart together as a single train!
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * 5) At the end of the ride, you climb out of the car and RENOUNCE
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * your right to it, so that it can be recycled for another rider.
b83cd2c35abe58abb09c73f2ef35426f1384ad46Michael Speer * For each rider, these have to occur in this order, but the riders
b83cd2c35abe58abb09c73f2ef35426f1384ad46Michael Speer * don't have to stay in the same order at each stage. In particular,
b83cd2c35abe58abb09c73f2ef35426f1384ad46Michael Speer * they may overtake each other between RESERVING a place and CLAIMING
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * it, or between CLAIMING and OCCUPYING a space.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Once a car is CLAIMED, the train currently being assembled can't go
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * without that car (this guarantees that the cars in a single train
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * make up a consecutively-numbered set). Therefore, when any train
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * leaves, we know there can't be any riders in transit between CLAIMING
8ad8db65d4781f61f1fd519144f555e6045100e1Michael Speer * and OCCUPYING their cars. There can be some who have RESERVED but
8ad8db65d4781f61f1fd519144f555e6045100e1Michael Speer * not yet CLAIMED their places. That's OK, though, because they'll go
8ad8db65d4781f61f1fd519144f555e6045100e1Michael Speer * into the next train.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs#define BGE_DBG BGE_DBG_SEND /* debug flag for this code */
8ad8db65d4781f61f1fd519144f555e6045100e1Michael Speer * ========== Send-side recycle routines ==========
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Recycle all the completed buffers in the specified send ring up to
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * (but not including) the consumer index in the status block.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * This function must advance (srp->tc_next) AND adjust (srp->tx_free)
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * to account for the packets it has recycled.
fd9489cef0e9b7d8a708339e560d453f230af2cfQiyan Sun - Sun Microsystems - San Diego United States * This is a trivial version that just does that and nothing more, but
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * it suffices while there's only one method for sending messages (by
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * copying) and that method doesn't need any special per-buffer action
1ed830817782694e7259ee818a2f8eee72233f1eMichael Speer * for recycling.
41307000fb6264427cd9b68419c13b879e83e7d3Qiyan Sun - Sun Microsystems - San Diego United Statesstatic void bge_recycle_ring(bge_t *bgep, send_ring_t *srp);
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs#pragma inline(bge_recycle_ring)
fe930412c257f961ae67039de3b164b83717976aqsstatic void
1c29f7e382074ff2792b7f30c9be898ead487a30Qiyan Sun - Sun Microsystems - San Diego United States if (slot < srp->tc_next)
1c29f7e382074ff2792b7f30c9be898ead487a30Qiyan Sun - Sun Microsystems - San Diego United States n += srp->desc.nslots;
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * We're about to release one or more places :-)
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * These ASSERTions check that our invariants still hold:
fe930412c257f961ae67039de3b164b83717976aqs * there must always be at least one free place
fe930412c257f961ae67039de3b164b83717976aqs * at this point, there must be at least one place NOT free
676f04004b15fc2872dfb0b7349d2d821a6eaa31Michael Speer * we're not about to free more places than were claimed!
fe930412c257f961ae67039de3b164b83717976aqs * Reset the watchdog count: to 0 if all buffers are
fe930412c257f961ae67039de3b164b83717976aqs * now free, or to 1 if some are still outstanding.
fe930412c257f961ae67039de3b164b83717976aqs * Note: non-synchonised access here means we may get
fe930412c257f961ae67039de3b164b83717976aqs * the "wrong" answer, but only in a harmless fashion
fe930412c257f961ae67039de3b164b83717976aqs * (i.e. we deactivate the watchdog because all buffers
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * are apparently free, even though another thread may
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * have claimed one before we leave here; in this case
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * the watchdog will restart on the next send() call).
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs bgep->watchdog = srp->tx_free == srp->desc.nslots ? 0 : 1;
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Recycle all returned slots in all rings.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * To give priority to low-numbered rings, whenever we have recycled any
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * slots in any ring except 0, we restart scanning again from ring 0.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Thus, for example, if rings 0, 3, and 10 are carrying traffic, the
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * pattern of recycles might go 0, 3, 10, 3, 0, 10, 0:
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * 0 found some - recycle them
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * 1..2 none found
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * 3 found some - recycle them and restart scan
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * 0..9 none found
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * 10 found some - recycle them and restart scan
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * 0..2 none found
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * 3 found some more - recycle them and restart scan
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * 0 found some more - recycle them
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * 0..9 none found
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * 10 found some more - recycle them and restart scan
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * 0 found some more - recycle them
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * 1..15 none found
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * The routine returns only when a complete scan has been performed
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * without finding any slots to recycle.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Note: the expression (BGE_SEND_RINGS_USED > 1) yields a compile-time
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * constant and allows the compiler to optimise away the outer do-loop
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * if only one send ring is being used.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * For each ring, (srp->cons_index_p) points to the
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * proper index within the status block (which has
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * already been sync'd by the caller).
fd9489cef0e9b7d8a708339e560d453f230af2cfQiyan Sun - Sun Microsystems - San Diego United States if (*srp->cons_index_p == srp->tc_next)
fd9489cef0e9b7d8a708339e560d453f230af2cfQiyan Sun - Sun Microsystems - San Diego United States continue; /* no slots to recycle */
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Restart from ring 0, if we're not on ring 0 already.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * As H/W selects send BDs totally based on priority and
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * available BDs on the higher priority ring are always
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * selected first, driver should keep consistence with H/W
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * and gives lower-numbered ring with higher priority.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Loop over all rings (if there *are* multiple rings)
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * ========== Send-side transmit routines ==========
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * CLAIM an already-reserved place on the next train
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * This is the point of no return!
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qsstatic uint64_t bge_send_claim(bge_t *bgep, send_ring_t *srp);
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs#pragma inline(bge_send_claim)
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs slot = bge_atomic_claim(&srp->tx_next, srp->desc.nslots);
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Bump the watchdog counter, thus guaranteeing that it's
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * nonzero (watchdog activated). Note that non-synchonised
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * access here means we may race with the reclaim() code
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * above, but the outcome will be harmless. At worst, the
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * counter may not get reset on a partial reclaim; but the
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * large trigger threshold makes false positives unlikely
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qsstatic void
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Point it to the ip header.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Calculate the pseudo-header checksum.
3a109ad9413b360a5bfa6fa5ddfacef5fd64fe5bQiyan Sun - Sun Microsystems - San Diego United States iphl = 4 * (buf[0] & 0xF);
fd9489cef0e9b7d8a708339e560d453f230af2cfQiyan Sun - Sun Microsystems - San Diego United States cksum = (((uint16_t)buf[2])<<8) + buf[3] - iphl;
6ffca240bba331a8b22ff3ce47de58da6a205843Michael Speer * Point it to the TCP/UDP header, and
6ffca240bba331a8b22ff3ce47de58da6a205843Michael Speer * update the checksum field.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Send a message by copying it into a preallocated (and premapped) buffer
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qsstatic enum send_status bge_send_copy(bge_t *bgep, mblk_t *mp,
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs#pragma inline(bge_send_copy)
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qsbge_send_copy(bge_t *bgep, mblk_t *mp, send_ring_t *srp, uint16_t tci)
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * IMPORTANT:
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Up to the point where it claims a place, a send_msg()
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * routine can indicate failure by returning SEND_FAIL.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Once it's claimed a place, it mustn't fail.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * In this version, there's no setup to be done here, and there's
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * nothing that can fail, so we can go straight to claiming our
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * already-reserved place on the train.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * This is the point of no return!
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Copy the data into a pre-mapped buffer, which avoids the
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * overhead (and complication) of mapping/unmapping STREAMS
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * buffers and keeping hold of them until the DMA has completed.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Because all buffers are the same size, and larger than the
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * longest single valid message, we don't have to bother about
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * splitting the message across multiple buffers either.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs if (tci != 0) {
fe930412c257f961ae67039de3b164b83717976aqs * We've reached the end of the chain; and we should have
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * collected no more than ETHERMAX bytes into our buffer.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Update the hardware send buffer descriptor; then we're done.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * The return status indicates that the message can be freed
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * right away, as we've already copied the contents ...
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs hw_sbd_p->host_buf_addr = ssbdp->pbuf.cookie.dmac_laddress;
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs if (tci != 0) {
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs hcksum_retrieve(mp, NULL, NULL, NULL, NULL, NULL, NULL, &pflags);
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Determine if the packet is VLAN tagged.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs sizeof (struct ether_vlan_header))) {
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Try to reserve a place in the chosen ring. Shouldn't try next
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * higher-numbered (lower-priority) ring, if there aren't any
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * available. Otherwise, packets with same priority may get
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * transmission starvation.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Now that we know that there is space to transmit the packet
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * strip any VLAN tag that is present.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * We've reserved a place :-)
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * These ASSERTions check that our invariants still hold:
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * there must still be at least one free place
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * there must be at least one place NOT free (ours!)
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs if ((status = bge_send_copy(bgep, mp, srp, tci)) == SEND_FAIL) {
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * The send routine failed :( So we have to renounce
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * our reservation before returning the error.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * The send routine succeeded; it will have updated the
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * h/w ring descriptor, and the <tx_next> and <tx_flow>
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * counters.
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * Because there can be multiple concurrent threads in
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * transit through this code, we only want to prod the
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * hardware once the last one is departing ...
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs if (bgep->bge_mac_state == BGE_MAC_STARTED && bgep->resched_needed) {
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs * bge_m_tx() - send a chain of packets
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs return (mp);
3dec9fcdd56adf1b4a563137b4915c8f2d83b881qs return (mp);