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 /* 237099Syt223700 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 272675Szh199473 #include "bge_impl.h" 280Sstevel@tonic-gate 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* 310Sstevel@tonic-gate * The transmit-side code uses an allocation process which is similar 320Sstevel@tonic-gate * to some theme park roller-coaster rides, where riders sit in cars 330Sstevel@tonic-gate * that can go individually, but work better in a train. 340Sstevel@tonic-gate * 350Sstevel@tonic-gate * 1) RESERVE a place - this doesn't refer to any specific car or 360Sstevel@tonic-gate * seat, just that you will get a ride. The attempt to RESERVE a 370Sstevel@tonic-gate * place can fail if all spaces in all cars are already committed. 380Sstevel@tonic-gate * 390Sstevel@tonic-gate * 2) Prepare yourself; this may take an arbitrary (but not unbounded) 400Sstevel@tonic-gate * time, and you can back out at this stage, in which case you must 410Sstevel@tonic-gate * give up (RENOUNCE) your place. 420Sstevel@tonic-gate * 430Sstevel@tonic-gate * 3) CLAIM your space - a specific car (the next sequentially 440Sstevel@tonic-gate * numbered one) is allocated at this stage, and is guaranteed 450Sstevel@tonic-gate * to be part of the next train to depart. Once you've done 460Sstevel@tonic-gate * this, you can't back out, nor wait for any external event 470Sstevel@tonic-gate * or resource. 480Sstevel@tonic-gate * 490Sstevel@tonic-gate * 4) Occupy your car - when all CLAIMED cars are OCCUPIED, they 500Sstevel@tonic-gate * all depart together as a single train! 510Sstevel@tonic-gate * 520Sstevel@tonic-gate * 5) At the end of the ride, you climb out of the car and RENOUNCE 530Sstevel@tonic-gate * your right to it, so that it can be recycled for another rider. 540Sstevel@tonic-gate * 550Sstevel@tonic-gate * For each rider, these have to occur in this order, but the riders 560Sstevel@tonic-gate * don't have to stay in the same order at each stage. In particular, 570Sstevel@tonic-gate * they may overtake each other between RESERVING a place and CLAIMING 580Sstevel@tonic-gate * it, or between CLAIMING and OCCUPYING a space. 590Sstevel@tonic-gate * 600Sstevel@tonic-gate * Once a car is CLAIMED, the train currently being assembled can't go 610Sstevel@tonic-gate * without that car (this guarantees that the cars in a single train 620Sstevel@tonic-gate * make up a consecutively-numbered set). Therefore, when any train 630Sstevel@tonic-gate * leaves, we know there can't be any riders in transit between CLAIMING 640Sstevel@tonic-gate * and OCCUPYING their cars. There can be some who have RESERVED but 650Sstevel@tonic-gate * not yet CLAIMED their places. That's OK, though, because they'll go 660Sstevel@tonic-gate * into the next train. 670Sstevel@tonic-gate */ 680Sstevel@tonic-gate 690Sstevel@tonic-gate #define BGE_DBG BGE_DBG_SEND /* debug flag for this code */ 700Sstevel@tonic-gate 710Sstevel@tonic-gate /* 720Sstevel@tonic-gate * ========== Send-side recycle routines ========== 730Sstevel@tonic-gate */ 740Sstevel@tonic-gate 750Sstevel@tonic-gate /* 760Sstevel@tonic-gate * Recycle all the completed buffers in the specified send ring up to 770Sstevel@tonic-gate * (but not including) the consumer index in the status block. 780Sstevel@tonic-gate * 790Sstevel@tonic-gate * This function must advance (srp->tc_next) AND adjust (srp->tx_free) 800Sstevel@tonic-gate * to account for the packets it has recycled. 810Sstevel@tonic-gate * 820Sstevel@tonic-gate * This is a trivial version that just does that and nothing more, but 830Sstevel@tonic-gate * it suffices while there's only one method for sending messages (by 840Sstevel@tonic-gate * copying) and that method doesn't need any special per-buffer action 850Sstevel@tonic-gate * for recycling. 860Sstevel@tonic-gate */ 870Sstevel@tonic-gate static void bge_recycle_ring(bge_t *bgep, send_ring_t *srp); 880Sstevel@tonic-gate #pragma inline(bge_recycle_ring) 890Sstevel@tonic-gate 900Sstevel@tonic-gate static void 910Sstevel@tonic-gate bge_recycle_ring(bge_t *bgep, send_ring_t *srp) 920Sstevel@tonic-gate { 933334Sgs150176 sw_sbd_t *ssbdp; 943334Sgs150176 bge_queue_item_t *buf_item; 953334Sgs150176 bge_queue_item_t *buf_item_head; 963334Sgs150176 bge_queue_item_t *buf_item_tail; 973334Sgs150176 bge_queue_t *txbuf_queue; 980Sstevel@tonic-gate uint64_t slot; 990Sstevel@tonic-gate uint64_t n; 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate ASSERT(mutex_owned(srp->tc_lock)); 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate /* 1040Sstevel@tonic-gate * We're about to release one or more places :-) 1050Sstevel@tonic-gate * These ASSERTions check that our invariants still hold: 1060Sstevel@tonic-gate * there must always be at least one free place 1070Sstevel@tonic-gate * at this point, there must be at least one place NOT free 1080Sstevel@tonic-gate * we're not about to free more places than were claimed! 1090Sstevel@tonic-gate */ 1100Sstevel@tonic-gate ASSERT(srp->tx_free > 0); 1113334Sgs150176 ASSERT(srp->tx_free < srp->desc.nslots); 1120Sstevel@tonic-gate 1133334Sgs150176 buf_item_head = buf_item_tail = NULL; 1143334Sgs150176 for (n = 0, slot = srp->tc_next; slot != *srp->cons_index_p; 1153334Sgs150176 slot = NEXT(slot, srp->desc.nslots)) { 1163334Sgs150176 ssbdp = &srp->sw_sbds[slot]; 1173334Sgs150176 ASSERT(ssbdp->pbuf != NULL); 1183334Sgs150176 buf_item = ssbdp->pbuf; 1193334Sgs150176 if (buf_item_head == NULL) 1203334Sgs150176 buf_item_head = buf_item_tail = buf_item; 1213334Sgs150176 else { 1223334Sgs150176 buf_item_tail->next = buf_item; 1233334Sgs150176 buf_item_tail = buf_item; 1243334Sgs150176 } 1253334Sgs150176 ssbdp->pbuf = NULL; 1263334Sgs150176 n++; 1273334Sgs150176 } 1283334Sgs150176 if (n == 0) 1293334Sgs150176 return; 1303334Sgs150176 1313334Sgs150176 /* 1323334Sgs150176 * Update recycle index and free tx BD number 1333334Sgs150176 */ 1340Sstevel@tonic-gate srp->tc_next = slot; 1353334Sgs150176 ASSERT(srp->tx_free + n <= srp->desc.nslots); 1360Sstevel@tonic-gate bge_atomic_renounce(&srp->tx_free, n); 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate /* 1390Sstevel@tonic-gate * Reset the watchdog count: to 0 if all buffers are 1400Sstevel@tonic-gate * now free, or to 1 if some are still outstanding. 1410Sstevel@tonic-gate * Note: non-synchonised access here means we may get 1420Sstevel@tonic-gate * the "wrong" answer, but only in a harmless fashion 1430Sstevel@tonic-gate * (i.e. we deactivate the watchdog because all buffers 1440Sstevel@tonic-gate * are apparently free, even though another thread may 1450Sstevel@tonic-gate * have claimed one before we leave here; in this case 1460Sstevel@tonic-gate * the watchdog will restart on the next send() call). 1470Sstevel@tonic-gate */ 1480Sstevel@tonic-gate bgep->watchdog = srp->tx_free == srp->desc.nslots ? 0 : 1; 1493334Sgs150176 1503334Sgs150176 /* 1513334Sgs150176 * Return tx buffers to buffer push queue 1523334Sgs150176 */ 1533334Sgs150176 txbuf_queue = srp->txbuf_push_queue; 1543334Sgs150176 mutex_enter(txbuf_queue->lock); 1553334Sgs150176 buf_item_tail->next = txbuf_queue->head; 1563334Sgs150176 txbuf_queue->head = buf_item_head; 1573334Sgs150176 txbuf_queue->count += n; 1583334Sgs150176 mutex_exit(txbuf_queue->lock); 1593334Sgs150176 1603334Sgs150176 /* 1613334Sgs150176 * Check if we need exchange the tx buffer push and pop queue 1623334Sgs150176 */ 1633334Sgs150176 if ((srp->txbuf_pop_queue->count < srp->tx_buffers_low) && 1643334Sgs150176 (srp->txbuf_pop_queue->count < txbuf_queue->count)) { 1653334Sgs150176 srp->txbuf_push_queue = srp->txbuf_pop_queue; 1663334Sgs150176 srp->txbuf_pop_queue = txbuf_queue; 1673334Sgs150176 } 1683334Sgs150176 1693534Szh199473 if (srp->tx_flow != 0 || bgep->tx_resched_needed) 1703334Sgs150176 ddi_trigger_softintr(bgep->drain_id); 1710Sstevel@tonic-gate } 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate /* 1740Sstevel@tonic-gate * Recycle all returned slots in all rings. 1750Sstevel@tonic-gate * 1760Sstevel@tonic-gate * To give priority to low-numbered rings, whenever we have recycled any 1770Sstevel@tonic-gate * slots in any ring except 0, we restart scanning again from ring 0. 1780Sstevel@tonic-gate * Thus, for example, if rings 0, 3, and 10 are carrying traffic, the 1790Sstevel@tonic-gate * pattern of recycles might go 0, 3, 10, 3, 0, 10, 0: 1800Sstevel@tonic-gate * 1810Sstevel@tonic-gate * 0 found some - recycle them 1820Sstevel@tonic-gate * 1..2 none found 1830Sstevel@tonic-gate * 3 found some - recycle them and restart scan 1840Sstevel@tonic-gate * 0..9 none found 1850Sstevel@tonic-gate * 10 found some - recycle them and restart scan 1860Sstevel@tonic-gate * 0..2 none found 1870Sstevel@tonic-gate * 3 found some more - recycle them and restart scan 1880Sstevel@tonic-gate * 0 found some more - recycle them 1890Sstevel@tonic-gate * 0..9 none found 1900Sstevel@tonic-gate * 10 found some more - recycle them and restart scan 1910Sstevel@tonic-gate * 0 found some more - recycle them 1920Sstevel@tonic-gate * 1..15 none found 1930Sstevel@tonic-gate * 1940Sstevel@tonic-gate * The routine returns only when a complete scan has been performed 1950Sstevel@tonic-gate * without finding any slots to recycle. 1960Sstevel@tonic-gate * 1970Sstevel@tonic-gate * Note: the expression (BGE_SEND_RINGS_USED > 1) yields a compile-time 1980Sstevel@tonic-gate * constant and allows the compiler to optimise away the outer do-loop 1990Sstevel@tonic-gate * if only one send ring is being used. 2000Sstevel@tonic-gate */ 2010Sstevel@tonic-gate void bge_recycle(bge_t *bgep, bge_status_t *bsp); 2020Sstevel@tonic-gate #pragma no_inline(bge_recycle) 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate void 2050Sstevel@tonic-gate bge_recycle(bge_t *bgep, bge_status_t *bsp) 2060Sstevel@tonic-gate { 2070Sstevel@tonic-gate send_ring_t *srp; 2080Sstevel@tonic-gate uint64_t ring; 2090Sstevel@tonic-gate uint64_t tx_rings = bgep->chipid.tx_rings; 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate restart: 2120Sstevel@tonic-gate ring = 0; 2130Sstevel@tonic-gate srp = &bgep->send[ring]; 2140Sstevel@tonic-gate do { 2150Sstevel@tonic-gate /* 2160Sstevel@tonic-gate * For each ring, (srp->cons_index_p) points to the 2170Sstevel@tonic-gate * proper index within the status block (which has 2180Sstevel@tonic-gate * already been sync'd by the caller). 2190Sstevel@tonic-gate */ 2200Sstevel@tonic-gate ASSERT(srp->cons_index_p == SEND_INDEX_P(bsp, ring)); 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate if (*srp->cons_index_p == srp->tc_next) 2230Sstevel@tonic-gate continue; /* no slots to recycle */ 2243334Sgs150176 if (mutex_tryenter(srp->tc_lock) == 0) 2253334Sgs150176 continue; /* already in process */ 2260Sstevel@tonic-gate bge_recycle_ring(bgep, srp); 2270Sstevel@tonic-gate mutex_exit(srp->tc_lock); 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate /* 2300Sstevel@tonic-gate * Restart from ring 0, if we're not on ring 0 already. 2310Sstevel@tonic-gate * As H/W selects send BDs totally based on priority and 2320Sstevel@tonic-gate * available BDs on the higher priority ring are always 2330Sstevel@tonic-gate * selected first, driver should keep consistence with H/W 2340Sstevel@tonic-gate * and gives lower-numbered ring with higher priority. 2350Sstevel@tonic-gate */ 2360Sstevel@tonic-gate if (tx_rings > 1 && ring > 0) 2370Sstevel@tonic-gate goto restart; 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate /* 2400Sstevel@tonic-gate * Loop over all rings (if there *are* multiple rings) 2410Sstevel@tonic-gate */ 2420Sstevel@tonic-gate } while (++srp, ++ring < tx_rings); 2430Sstevel@tonic-gate } 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate /* 2470Sstevel@tonic-gate * ========== Send-side transmit routines ========== 2480Sstevel@tonic-gate */ 2492135Szh199473 #define TCP_CKSUM_OFFSET 16 2502135Szh199473 #define UDP_CKSUM_OFFSET 6 2512135Szh199473 2522135Szh199473 static void 2532135Szh199473 bge_pseudo_cksum(uint8_t *buf) 2542135Szh199473 { 2552135Szh199473 uint32_t cksum; 2562135Szh199473 uint16_t iphl; 2572135Szh199473 uint16_t proto; 2582135Szh199473 2592135Szh199473 /* 2602135Szh199473 * Point it to the ip header. 2612135Szh199473 */ 2622135Szh199473 buf += sizeof (struct ether_header); 2632135Szh199473 2642135Szh199473 /* 2652135Szh199473 * Calculate the pseudo-header checksum. 2662135Szh199473 */ 2672135Szh199473 iphl = 4 * (buf[0] & 0xF); 2682135Szh199473 cksum = (((uint16_t)buf[2])<<8) + buf[3] - iphl; 2692135Szh199473 cksum += proto = buf[9]; 2702135Szh199473 cksum += (((uint16_t)buf[12])<<8) + buf[13]; 2712135Szh199473 cksum += (((uint16_t)buf[14])<<8) + buf[15]; 2722135Szh199473 cksum += (((uint16_t)buf[16])<<8) + buf[17]; 2732135Szh199473 cksum += (((uint16_t)buf[18])<<8) + buf[19]; 2742135Szh199473 cksum = (cksum>>16) + (cksum & 0xFFFF); 2752135Szh199473 cksum = (cksum>>16) + (cksum & 0xFFFF); 2762135Szh199473 2772135Szh199473 /* 2782135Szh199473 * Point it to the TCP/UDP header, and 2792135Szh199473 * update the checksum field. 2802135Szh199473 */ 2812135Szh199473 buf += iphl + ((proto == IPPROTO_TCP) ? 2827099Syt223700 TCP_CKSUM_OFFSET : UDP_CKSUM_OFFSET); 2832135Szh199473 2847099Syt223700 /* 2857099Syt223700 * A real possibility that pointer cast is a problem. 2867099Syt223700 * Should be fixed when we know the code better. 2877099Syt223700 * E_BAD_PTR_CAST_ALIGN is added to make it temporarily clean. 2887099Syt223700 */ 2892135Szh199473 *(uint16_t *)buf = htons((uint16_t)cksum); 2902135Szh199473 } 2912135Szh199473 2923334Sgs150176 static bge_queue_item_t * 2933334Sgs150176 bge_get_txbuf(bge_t *bgep, send_ring_t *srp) 2943334Sgs150176 { 2953334Sgs150176 bge_queue_item_t *txbuf_item; 2963334Sgs150176 bge_queue_t *txbuf_queue; 2970Sstevel@tonic-gate 2983334Sgs150176 txbuf_queue = srp->txbuf_pop_queue; 2993334Sgs150176 mutex_enter(txbuf_queue->lock); 3003334Sgs150176 if (txbuf_queue->count == 0) { 3013334Sgs150176 mutex_exit(txbuf_queue->lock); 3023334Sgs150176 txbuf_queue = srp->txbuf_push_queue; 3033334Sgs150176 mutex_enter(txbuf_queue->lock); 3043334Sgs150176 if (txbuf_queue->count == 0) { 3053334Sgs150176 mutex_exit(txbuf_queue->lock); 3063334Sgs150176 /* Try to allocate more tx buffers */ 3073334Sgs150176 if (srp->tx_array < srp->tx_array_max) { 3083334Sgs150176 mutex_enter(srp->tx_lock); 3093334Sgs150176 txbuf_item = bge_alloc_txbuf_array(bgep, srp); 3103334Sgs150176 mutex_exit(srp->tx_lock); 3113334Sgs150176 } else 3123334Sgs150176 txbuf_item = NULL; 3133334Sgs150176 return (txbuf_item); 3143334Sgs150176 } 3153334Sgs150176 } 3163334Sgs150176 txbuf_item = txbuf_queue->head; 3173334Sgs150176 txbuf_queue->head = (bge_queue_item_t *)txbuf_item->next; 3183334Sgs150176 txbuf_queue->count--; 3193334Sgs150176 mutex_exit(txbuf_queue->lock); 3203334Sgs150176 txbuf_item->next = NULL; 3213334Sgs150176 3223334Sgs150176 return (txbuf_item); 3233334Sgs150176 } 3243334Sgs150176 3253334Sgs150176 static void bge_send_fill_txbd(send_ring_t *srp, send_pkt_t *pktp); 3263334Sgs150176 #pragma inline(bge_send_fill_txbd) 3273334Sgs150176 3283334Sgs150176 static void 3293334Sgs150176 bge_send_fill_txbd(send_ring_t *srp, send_pkt_t *pktp) 3300Sstevel@tonic-gate { 3310Sstevel@tonic-gate bge_sbd_t *hw_sbd_p; 3320Sstevel@tonic-gate sw_sbd_t *ssbdp; 3333334Sgs150176 bge_queue_item_t *txbuf_item; 3343334Sgs150176 sw_txbuf_t *txbuf; 3350Sstevel@tonic-gate uint64_t slot; 3360Sstevel@tonic-gate 3373334Sgs150176 ASSERT(mutex_owned(srp->tx_lock)); 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate /* 3403334Sgs150176 * Go straight to claiming our already-reserved places 3413334Sgs150176 * on the train! 3420Sstevel@tonic-gate */ 3433334Sgs150176 ASSERT(pktp->txbuf_item != NULL); 3443334Sgs150176 txbuf_item = pktp->txbuf_item; 3453334Sgs150176 txbuf = txbuf_item->item; 3463334Sgs150176 slot = srp->tx_next; 3470Sstevel@tonic-gate ssbdp = &srp->sw_sbds[slot]; 3483334Sgs150176 hw_sbd_p = DMA_VPTR(ssbdp->desc); 3493334Sgs150176 hw_sbd_p->flags = 0; 3503334Sgs150176 ASSERT(txbuf->copy_len != 0); 3513334Sgs150176 (void) ddi_dma_sync(txbuf->buf.dma_hdl, 0, 3523334Sgs150176 txbuf->copy_len, DDI_DMA_SYNC_FORDEV); 3533334Sgs150176 ASSERT(ssbdp->pbuf == NULL); 3543334Sgs150176 ssbdp->pbuf = txbuf_item; 3553334Sgs150176 srp->tx_next = NEXT(slot, srp->desc.nslots); 3563334Sgs150176 pktp->txbuf_item = NULL; 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate /* 3593334Sgs150176 * Setting hardware send buffer descriptor 3600Sstevel@tonic-gate */ 3613334Sgs150176 hw_sbd_p->host_buf_addr = txbuf->buf.cookie.dmac_laddress; 3623334Sgs150176 hw_sbd_p->len = txbuf->copy_len; 3633334Sgs150176 if (pktp->vlan_tci != 0) { 3643334Sgs150176 hw_sbd_p->vlan_tci = pktp->vlan_tci; 3653534Szh199473 hw_sbd_p->host_buf_addr += VLAN_TAGSZ; 3663334Sgs150176 hw_sbd_p->flags |= SBD_FLAG_VLAN_TAG; 3673334Sgs150176 } 3683334Sgs150176 if (pktp->pflags & HCK_IPV4_HDRCKSUM) 3693334Sgs150176 hw_sbd_p->flags |= SBD_FLAG_IP_CKSUM; 3703334Sgs150176 if (pktp->pflags & HCK_FULLCKSUM) 3713334Sgs150176 hw_sbd_p->flags |= SBD_FLAG_TCP_UDP_CKSUM; 3723334Sgs150176 hw_sbd_p->flags |= SBD_FLAG_PACKET_END; 3733334Sgs150176 } 3743334Sgs150176 3753334Sgs150176 /* 3763334Sgs150176 * Send a message by copying it into a preallocated (and premapped) buffer 3773334Sgs150176 */ 3783534Szh199473 static void bge_send_copy(bge_t *bgep, sw_txbuf_t *txbuf, mblk_t *mp); 3793334Sgs150176 #pragma inline(bge_send_copy) 3803334Sgs150176 3813334Sgs150176 static void 3823534Szh199473 bge_send_copy(bge_t *bgep, sw_txbuf_t *txbuf, mblk_t *mp) 3833334Sgs150176 { 3843334Sgs150176 mblk_t *bp; 3853334Sgs150176 uint32_t mblen; 3863334Sgs150176 char *pbuf; 3873334Sgs150176 3883334Sgs150176 txbuf->copy_len = 0; 3893334Sgs150176 pbuf = DMA_VPTR(txbuf->buf); 3903534Szh199473 for (bp = mp; bp != NULL; bp = bp->b_cont) { 3913334Sgs150176 if ((mblen = MBLKL(bp)) == 0) 3923334Sgs150176 continue; 3933534Szh199473 ASSERT(txbuf->copy_len + mblen <= 3943534Szh199473 bgep->chipid.snd_buff_size); 3953534Szh199473 bcopy(bp->b_rptr, pbuf, mblen); 3963534Szh199473 pbuf += mblen; 3973534Szh199473 txbuf->copy_len += mblen; 3980Sstevel@tonic-gate } 3993334Sgs150176 } 4003334Sgs150176 4013334Sgs150176 /* 4023334Sgs150176 * Fill the Tx buffer descriptors and trigger the h/w transmission 4033334Sgs150176 */ 4043334Sgs150176 static void 4053334Sgs150176 bge_send_serial(bge_t *bgep, send_ring_t *srp) 4063334Sgs150176 { 4073334Sgs150176 send_pkt_t *pktp; 4083334Sgs150176 uint64_t txfill_next; 4093334Sgs150176 uint32_t count; 4103334Sgs150176 uint32_t tx_next; 4113334Sgs150176 sw_sbd_t *ssbdp; 4123334Sgs150176 bge_status_t *bsp; 4133334Sgs150176 4143334Sgs150176 /* 4153334Sgs150176 * Try to hold the tx lock: 4163334Sgs150176 * If we are in an interrupt context, use mutex_enter() to 4173334Sgs150176 * ensure quick response for tx in interrupt context; 4183334Sgs150176 * Otherwise, use mutex_tryenter() to serialize this h/w tx 4193334Sgs150176 * BD filling and transmission triggering task. 4203334Sgs150176 */ 4213334Sgs150176 if (servicing_interrupt() != 0) 4223334Sgs150176 mutex_enter(srp->tx_lock); 4233334Sgs150176 else if (mutex_tryenter(srp->tx_lock) == 0) 4243334Sgs150176 return; /* already in process */ 4253334Sgs150176 4263334Sgs150176 bsp = DMA_VPTR(bgep->status_block); 4273334Sgs150176 txfill_next = srp->txfill_next; 4283534Szh199473 start_tx: 4293334Sgs150176 tx_next = srp->tx_next; 4303334Sgs150176 ssbdp = &srp->sw_sbds[tx_next]; 4313334Sgs150176 for (count = 0; count < bgep->param_drain_max; ++count) { 4323334Sgs150176 pktp = &srp->pktp[txfill_next]; 4333334Sgs150176 if (!pktp->tx_ready) { 4343334Sgs150176 if (count == 0) 4353334Sgs150176 srp->tx_block++; 4363334Sgs150176 break; 4373334Sgs150176 } 4383334Sgs150176 4393334Sgs150176 /* 4403334Sgs150176 * If there are no enough BDs: try to recycle more 4413334Sgs150176 */ 4423334Sgs150176 if (srp->tx_free <= 1) 4433334Sgs150176 bge_recycle(bgep, bsp); 4443334Sgs150176 4453334Sgs150176 /* 4463334Sgs150176 * Reserved required BDs: 1 is enough 4473334Sgs150176 */ 4483334Sgs150176 if (!bge_atomic_reserve(&srp->tx_free, 1)) { 4493334Sgs150176 srp->tx_nobd++; 4503334Sgs150176 break; 4513334Sgs150176 } 4523334Sgs150176 4533334Sgs150176 /* 4543334Sgs150176 * Filling the tx BD 4553334Sgs150176 */ 4563334Sgs150176 bge_send_fill_txbd(srp, pktp); 4573334Sgs150176 txfill_next = NEXT(txfill_next, BGE_SEND_BUF_MAX); 4583334Sgs150176 pktp->tx_ready = B_FALSE; 4593334Sgs150176 } 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate /* 4623334Sgs150176 * Trigger h/w to start transmission. 4630Sstevel@tonic-gate */ 4643334Sgs150176 if (count != 0) { 4653334Sgs150176 bge_atomic_sub64(&srp->tx_flow, count); 4663334Sgs150176 if (tx_next + count > srp->desc.nslots) { 4673334Sgs150176 (void) ddi_dma_sync(ssbdp->desc.dma_hdl, 0, 4683334Sgs150176 (srp->desc.nslots - tx_next) * sizeof (bge_sbd_t), 4693334Sgs150176 DDI_DMA_SYNC_FORDEV); 4703334Sgs150176 count -= srp->desc.nslots - tx_next; 4713334Sgs150176 ssbdp = &srp->sw_sbds[0]; 4723334Sgs150176 } 4733334Sgs150176 (void) ddi_dma_sync(ssbdp->desc.dma_hdl, 0, 4743334Sgs150176 count*sizeof (bge_sbd_t), DDI_DMA_SYNC_FORDEV); 4753334Sgs150176 bge_mbx_put(bgep, srp->chip_mbx_reg, srp->tx_next); 4763334Sgs150176 srp->txfill_next = txfill_next; 4773334Sgs150176 bgep->watchdog++; 4783534Szh199473 if (srp->tx_flow != 0 && srp->tx_free > 1) 4793534Szh199473 goto start_tx; 4800Sstevel@tonic-gate } 4810Sstevel@tonic-gate 4823334Sgs150176 mutex_exit(srp->tx_lock); 4830Sstevel@tonic-gate } 4840Sstevel@tonic-gate 485*8275SEric Cheng mblk_t * 486*8275SEric Cheng bge_ring_tx(void *arg, mblk_t *mp) 4870Sstevel@tonic-gate { 488*8275SEric Cheng send_ring_t *srp = arg; 489*8275SEric Cheng bge_t *bgep = srp->bgep; 4900Sstevel@tonic-gate struct ether_vlan_header *ehp; 4913334Sgs150176 bge_queue_item_t *txbuf_item; 4923334Sgs150176 sw_txbuf_t *txbuf; 4933334Sgs150176 send_pkt_t *pktp; 4943334Sgs150176 uint64_t pkt_slot; 4953334Sgs150176 uint16_t vlan_tci; 4963334Sgs150176 uint32_t pflags; 4973534Szh199473 char *pbuf; 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate ASSERT(mp->b_next == NULL); 5003334Sgs150176 5013334Sgs150176 /* 5023334Sgs150176 * Get a s/w tx buffer first 5033334Sgs150176 */ 5043334Sgs150176 txbuf_item = bge_get_txbuf(bgep, srp); 5053334Sgs150176 if (txbuf_item == NULL) { 5063334Sgs150176 /* no tx buffer available */ 5073334Sgs150176 srp->tx_nobuf++; 5083334Sgs150176 bgep->tx_resched_needed = B_TRUE; 5093334Sgs150176 bge_send_serial(bgep, srp); 510*8275SEric Cheng return (mp); 5113334Sgs150176 } 5120Sstevel@tonic-gate 5130Sstevel@tonic-gate /* 5143334Sgs150176 * Copy all mp fragments to the pkt buffer 5150Sstevel@tonic-gate */ 5163334Sgs150176 txbuf = txbuf_item->item; 5173534Szh199473 bge_send_copy(bgep, txbuf, mp); 5183534Szh199473 5193534Szh199473 /* 5203534Szh199473 * Determine if the packet is VLAN tagged. 5213534Szh199473 */ 5223534Szh199473 ASSERT(txbuf->copy_len >= sizeof (struct ether_header)); 5233534Szh199473 pbuf = DMA_VPTR(txbuf->buf); 5243534Szh199473 5257099Syt223700 ehp = (void *)pbuf; 5263534Szh199473 if (ehp->ether_tpid == htons(ETHERTYPE_VLAN)) { 5273534Szh199473 /* Strip the vlan tag */ 5283534Szh199473 vlan_tci = ntohs(ehp->ether_tci); 5293534Szh199473 pbuf = memmove(pbuf + VLAN_TAGSZ, pbuf, 2 * ETHERADDRL); 5303534Szh199473 txbuf->copy_len -= VLAN_TAGSZ; 5313534Szh199473 } else 5323534Szh199473 vlan_tci = 0; 5333334Sgs150176 5343334Sgs150176 /* 5353334Sgs150176 * Retrieve checksum offloading info. 5363334Sgs150176 */ 5373334Sgs150176 hcksum_retrieve(mp, NULL, NULL, NULL, NULL, NULL, NULL, &pflags); 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate /* 5403334Sgs150176 * Calculate pseudo checksum if needed. 5410Sstevel@tonic-gate */ 5423334Sgs150176 if ((pflags & HCK_FULLCKSUM) && 5433334Sgs150176 (bgep->chipid.flags & CHIP_FLAG_PARTIAL_CSUM)) 5443534Szh199473 bge_pseudo_cksum((uint8_t *)pbuf); 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate /* 5473334Sgs150176 * Packet buffer is ready to send: get and fill pkt info 5480Sstevel@tonic-gate */ 5493334Sgs150176 pkt_slot = bge_atomic_next(&srp->txpkt_next, BGE_SEND_BUF_MAX); 5503334Sgs150176 pktp = &srp->pktp[pkt_slot]; 5513334Sgs150176 ASSERT(pktp->txbuf_item == NULL); 5523334Sgs150176 pktp->txbuf_item = txbuf_item; 5533334Sgs150176 pktp->vlan_tci = vlan_tci; 5543334Sgs150176 pktp->pflags = pflags; 5553334Sgs150176 atomic_inc_64(&srp->tx_flow); 5563334Sgs150176 ASSERT(pktp->tx_ready == B_FALSE); 5573334Sgs150176 pktp->tx_ready = B_TRUE; 5580Sstevel@tonic-gate 5593334Sgs150176 /* 5603334Sgs150176 * Filling the h/w bd and trigger the h/w to start transmission 5613334Sgs150176 */ 5623334Sgs150176 bge_send_serial(bgep, srp); 5633334Sgs150176 564*8275SEric Cheng srp->pushed_bytes += MBLKL(mp); 565*8275SEric Cheng 5663334Sgs150176 /* 5673334Sgs150176 * We've copied the contents, the message can be freed right away 5683334Sgs150176 */ 5693334Sgs150176 freemsg(mp); 570*8275SEric Cheng return (NULL); 571*8275SEric Cheng } 5723334Sgs150176 573*8275SEric Cheng static mblk_t * 574*8275SEric Cheng bge_send(bge_t *bgep, mblk_t *mp) 575*8275SEric Cheng { 576*8275SEric Cheng send_ring_t *ring; 577*8275SEric Cheng 578*8275SEric Cheng ring = &bgep->send[0]; /* ring 0 */ 579*8275SEric Cheng 580*8275SEric Cheng return (bge_ring_tx(ring, mp)); 5810Sstevel@tonic-gate } 5820Sstevel@tonic-gate 5830Sstevel@tonic-gate uint_t 5843334Sgs150176 bge_send_drain(caddr_t arg) 5850Sstevel@tonic-gate { 5863334Sgs150176 uint_t ring = 0; /* use ring 0 */ 5870Sstevel@tonic-gate bge_t *bgep; 5883334Sgs150176 send_ring_t *srp; 5890Sstevel@tonic-gate 5907099Syt223700 bgep = (void *)arg; 5913334Sgs150176 BGE_TRACE(("bge_send_drain($%p)", (void *)bgep)); 5920Sstevel@tonic-gate 5933334Sgs150176 srp = &bgep->send[ring]; 5943334Sgs150176 bge_send_serial(bgep, srp); 5950Sstevel@tonic-gate 5963334Sgs150176 if (bgep->tx_resched_needed && 5973334Sgs150176 (srp->tx_flow < srp->tx_buffers_low) && 5983334Sgs150176 (bgep->bge_mac_state == BGE_MAC_STARTED)) { 5992311Sseb mac_tx_update(bgep->mh); 6003334Sgs150176 bgep->tx_resched_needed = B_FALSE; 6013334Sgs150176 bgep->tx_resched++; 6020Sstevel@tonic-gate } 6030Sstevel@tonic-gate 6041200Sly149593 return (DDI_INTR_CLAIMED); 6050Sstevel@tonic-gate } 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate /* 6080Sstevel@tonic-gate * bge_m_tx() - send a chain of packets 6090Sstevel@tonic-gate */ 6100Sstevel@tonic-gate mblk_t * 6110Sstevel@tonic-gate bge_m_tx(void *arg, mblk_t *mp) 6120Sstevel@tonic-gate { 6130Sstevel@tonic-gate bge_t *bgep = arg; /* private device info */ 6140Sstevel@tonic-gate mblk_t *next; 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate BGE_TRACE(("bge_m_tx($%p, $%p)", arg, (void *)mp)); 6170Sstevel@tonic-gate 6180Sstevel@tonic-gate ASSERT(mp != NULL); 6190Sstevel@tonic-gate ASSERT(bgep->bge_mac_state == BGE_MAC_STARTED); 6200Sstevel@tonic-gate 6213170Sml149210 rw_enter(bgep->errlock, RW_READER); 6220Sstevel@tonic-gate if (bgep->bge_chip_state != BGE_CHIP_RUNNING) { 6230Sstevel@tonic-gate BGE_DEBUG(("bge_m_tx: chip not running")); 6243170Sml149210 freemsgchain(mp); 6253170Sml149210 mp = NULL; 6260Sstevel@tonic-gate } 6270Sstevel@tonic-gate 6280Sstevel@tonic-gate while (mp != NULL) { 6290Sstevel@tonic-gate next = mp->b_next; 6300Sstevel@tonic-gate mp->b_next = NULL; 6310Sstevel@tonic-gate 632*8275SEric Cheng if ((mp = bge_send(bgep, mp)) != NULL) { 6330Sstevel@tonic-gate mp->b_next = next; 6340Sstevel@tonic-gate break; 6350Sstevel@tonic-gate } 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate mp = next; 6380Sstevel@tonic-gate } 639152Sly149593 rw_exit(bgep->errlock); 6400Sstevel@tonic-gate 6410Sstevel@tonic-gate return (mp); 6420Sstevel@tonic-gate } 643