#include "bge_impl.h"
#define BGE_DBG BGE_DBG_SEND
static boolean_t
bge_recycle_ring(bge_t *bgep, send_ring_t *srp)
{
sw_sbd_t *ssbdp;
bge_queue_item_t *buf_item;
bge_queue_item_t *buf_item_head;
bge_queue_item_t *buf_item_tail;
bge_queue_t *txbuf_queue;
uint64_t slot;
uint64_t n;
ASSERT(mutex_owned(srp->tc_lock));
ASSERT(srp->tx_free <= srp->desc.nslots);
buf_item_head = buf_item_tail = NULL;
for (n = 0, slot = srp->tc_next; slot != *srp->cons_index_p;
slot = NEXT(slot, srp->desc.nslots)) {
ssbdp = &srp->sw_sbds[slot];
ASSERT(ssbdp->pbuf != NULL);
buf_item = ssbdp->pbuf;
if (buf_item_head == NULL)
buf_item_head = buf_item_tail = buf_item;
else {
buf_item_tail->next = buf_item;
buf_item_tail = buf_item;
}
ssbdp->pbuf = NULL;
n++;
}
if (n == 0)
return (B_FALSE);
bgep->watchdog = (slot == srp->tx_next) ? 0 : 1;
srp->tc_next = slot;
ASSERT(srp->tx_free + n <= srp->desc.nslots);
bge_atomic_renounce(&srp->tx_free, n);
txbuf_queue = srp->txbuf_push_queue;
mutex_enter(txbuf_queue->lock);
buf_item_tail->next = txbuf_queue->head;
txbuf_queue->head = buf_item_head;
txbuf_queue->count += n;
mutex_exit(txbuf_queue->lock);
if ((srp->txbuf_pop_queue->count < srp->tx_buffers_low) &&
(srp->txbuf_pop_queue->count < txbuf_queue->count)) {
srp->txbuf_push_queue = srp->txbuf_pop_queue;
srp->txbuf_pop_queue = txbuf_queue;
}
if (srp->tx_flow != 0 || bgep->tx_resched_needed)
ddi_trigger_softintr(bgep->drain_id);
return (B_TRUE);
}
boolean_t bge_recycle(bge_t *bgep, bge_status_t *bsp);
boolean_t
bge_recycle(bge_t *bgep, bge_status_t *bsp)
{
send_ring_t *srp;
uint64_t ring;
uint64_t tx_rings = bgep->chipid.tx_rings;
boolean_t tx_done = B_FALSE;
restart:
ring = 0;
srp = &bgep->send[ring];
do {
ASSERT(srp->cons_index_p == SEND_INDEX_P(bsp, ring));
if (*srp->cons_index_p == srp->tc_next)
continue;
if (mutex_tryenter(srp->tc_lock) == 0)
continue;
tx_done |= bge_recycle_ring(bgep, srp);
mutex_exit(srp->tc_lock);
if (tx_rings > 1 && ring > 0)
goto restart;
} while (++srp, ++ring < tx_rings);
return (tx_done);
}
#define TCP_CKSUM_OFFSET 16
#define UDP_CKSUM_OFFSET 6
static void
bge_pseudo_cksum(uint8_t *buf)
{
uint32_t cksum;
uint16_t iphl;
uint16_t proto;
buf += sizeof (struct ether_header);
iphl = 4 * (buf[0] & 0xF);
cksum = (((uint16_t)buf[2])<<8) + buf[3] - iphl;
cksum += proto = buf[9];
cksum += (((uint16_t)buf[12])<<8) + buf[13];
cksum += (((uint16_t)buf[14])<<8) + buf[15];
cksum += (((uint16_t)buf[16])<<8) + buf[17];
cksum += (((uint16_t)buf[18])<<8) + buf[19];
cksum = (cksum>>16) + (cksum & 0xFFFF);
cksum = (cksum>>16) + (cksum & 0xFFFF);
buf += iphl + ((proto == IPPROTO_TCP) ?
TCP_CKSUM_OFFSET : UDP_CKSUM_OFFSET);
*(uint16_t *)buf = htons((uint16_t)cksum);
}
static bge_queue_item_t *
bge_get_txbuf(bge_t *bgep, send_ring_t *srp)
{
bge_queue_item_t *txbuf_item;
bge_queue_t *txbuf_queue;
txbuf_queue = srp->txbuf_pop_queue;
mutex_enter(txbuf_queue->lock);
if (txbuf_queue->count == 0) {
mutex_exit(txbuf_queue->lock);
txbuf_queue = srp->txbuf_push_queue;
mutex_enter(txbuf_queue->lock);
if (txbuf_queue->count == 0) {
mutex_exit(txbuf_queue->lock);
if (srp->tx_array < srp->tx_array_max) {
mutex_enter(srp->tx_lock);
txbuf_item = bge_alloc_txbuf_array(bgep, srp);
mutex_exit(srp->tx_lock);
} else
txbuf_item = NULL;
return (txbuf_item);
}
}
txbuf_item = txbuf_queue->head;
txbuf_queue->head = (bge_queue_item_t *)txbuf_item->next;
txbuf_queue->count--;
mutex_exit(txbuf_queue->lock);
txbuf_item->next = NULL;
return (txbuf_item);
}
static void bge_send_copy(bge_t *bgep, sw_txbuf_t *txbuf, mblk_t *mp);
static void
bge_send_copy(bge_t *bgep, sw_txbuf_t *txbuf, mblk_t *mp)
{
mblk_t *bp;
uint32_t mblen;
char *pbuf;
txbuf->copy_len = 0;
pbuf = DMA_VPTR(txbuf->buf);
for (bp = mp; bp != NULL; bp = bp->b_cont) {
if ((mblen = MBLKL(bp)) == 0)
continue;
ASSERT(txbuf->copy_len + mblen <=
bgep->chipid.snd_buff_size);
bcopy(bp->b_rptr, pbuf, mblen);
pbuf += mblen;
txbuf->copy_len += mblen;
}
}
static void
bge_send_serial(bge_t *bgep, send_ring_t *srp)
{
send_pkt_t *pktp;
uint64_t txfill_next;
uint32_t count;
uint32_t tx_next;
sw_sbd_t *ssbdp;
bge_status_t *bsp;
bge_sbd_t *hw_sbd_p;
bge_queue_item_t *txbuf_item;
sw_txbuf_t *txbuf;
if (servicing_interrupt() != 0)
mutex_enter(srp->tx_lock);
else if (mutex_tryenter(srp->tx_lock) == 0)
return;
bsp = DMA_VPTR(bgep->status_block);
txfill_next = srp->txfill_next;
tx_next = srp->tx_next;
start_tx:
for (count = 0; count < bgep->param_drain_max; ++count) {
pktp = &srp->pktp[txfill_next];
if (!pktp->tx_ready) {
if (count == 0)
srp->tx_block++;
break;
}
if (srp->tx_free <= 1)
(void) bge_recycle(bgep, bsp);
if (!bge_atomic_reserve(&srp->tx_free, 1)) {
srp->tx_nobd++;
break;
}
ASSERT(pktp->txbuf_item != NULL);
txbuf_item = pktp->txbuf_item;
pktp->txbuf_item = NULL;
pktp->tx_ready = B_FALSE;
txbuf = txbuf_item->item;
ASSERT(txbuf->copy_len != 0);
(void) ddi_dma_sync(txbuf->buf.dma_hdl, 0,
txbuf->copy_len, DDI_DMA_SYNC_FORDEV);
ssbdp = &srp->sw_sbds[tx_next];
ASSERT(ssbdp->pbuf == NULL);
ssbdp->pbuf = txbuf_item;
hw_sbd_p = DMA_VPTR(ssbdp->desc);
hw_sbd_p->flags = 0;
hw_sbd_p->host_buf_addr = txbuf->buf.cookie.dmac_laddress;
hw_sbd_p->len = txbuf->copy_len;
if (pktp->vlan_tci != 0) {
hw_sbd_p->vlan_tci = pktp->vlan_tci;
hw_sbd_p->host_buf_addr += VLAN_TAGSZ;
hw_sbd_p->flags |= SBD_FLAG_VLAN_TAG;
}
if (pktp->pflags & HCK_IPV4_HDRCKSUM)
hw_sbd_p->flags |= SBD_FLAG_IP_CKSUM;
if (pktp->pflags & HCK_FULLCKSUM)
hw_sbd_p->flags |= SBD_FLAG_TCP_UDP_CKSUM;
if (!(bgep->chipid.flags & CHIP_FLAG_NO_JUMBO) &&
(DEVICE_5717_SERIES_CHIPSETS(bgep) ||
DEVICE_5725_SERIES_CHIPSETS(bgep) ||
DEVICE_57765_SERIES_CHIPSETS(bgep)) &&
(txbuf->copy_len > ETHERMAX))
hw_sbd_p->flags |= SBD_FLAG_JMB_PKT;
hw_sbd_p->flags |= SBD_FLAG_PACKET_END;
txfill_next = NEXT(txfill_next, BGE_SEND_BUF_MAX);
tx_next = NEXT(tx_next, srp->desc.nslots);
}
if (count != 0) {
bge_atomic_sub64(&srp->tx_flow, count);
srp->txfill_next = txfill_next;
if (srp->tx_next > tx_next) {
(void) ddi_dma_sync(ssbdp->desc.dma_hdl, 0,
(srp->desc.nslots - srp->tx_next) *
sizeof (bge_sbd_t),
DDI_DMA_SYNC_FORDEV);
count -= srp->desc.nslots - srp->tx_next;
ssbdp = &srp->sw_sbds[0];
}
(void) ddi_dma_sync(ssbdp->desc.dma_hdl, 0,
count*sizeof (bge_sbd_t), DDI_DMA_SYNC_FORDEV);
bge_mbx_put(bgep, srp->chip_mbx_reg, tx_next);
srp->tx_next = tx_next;
atomic_or_32(&bgep->watchdog, 1);
if (srp->tx_flow != 0 && srp->tx_free > 1)
goto start_tx;
}
mutex_exit(srp->tx_lock);
}
mblk_t *
bge_ring_tx(void *arg, mblk_t *mp)
{
send_ring_t *srp = arg;
bge_t *bgep = srp->bgep;
struct ether_vlan_header *ehp;
bge_queue_item_t *txbuf_item;
sw_txbuf_t *txbuf;
send_pkt_t *pktp;
uint64_t pkt_slot;
uint16_t vlan_tci;
uint32_t pflags;
char *pbuf;
ASSERT(mp->b_next == NULL);
txbuf_item = bge_get_txbuf(bgep, srp);
if (txbuf_item == NULL) {
srp->tx_nobuf++;
bgep->tx_resched_needed = B_TRUE;
bge_send_serial(bgep, srp);
return (mp);
}
txbuf = txbuf_item->item;
bge_send_copy(bgep, txbuf, mp);
ASSERT(txbuf->copy_len >= sizeof (struct ether_header));
pbuf = DMA_VPTR(txbuf->buf);
ehp = (void *)pbuf;
if (ehp->ether_tpid == htons(ETHERTYPE_VLAN)) {
vlan_tci = ntohs(ehp->ether_tci);
pbuf = memmove(pbuf + VLAN_TAGSZ, pbuf, 2 * ETHERADDRL);
txbuf->copy_len -= VLAN_TAGSZ;
} else
vlan_tci = 0;
mac_hcksum_get(mp, NULL, NULL, NULL, NULL, &pflags);
if ((pflags & HCK_FULLCKSUM) &&
(bgep->chipid.flags & CHIP_FLAG_PARTIAL_CSUM))
bge_pseudo_cksum((uint8_t *)pbuf);
pkt_slot = bge_atomic_next(&srp->txpkt_next, BGE_SEND_BUF_MAX);
pktp = &srp->pktp[pkt_slot];
ASSERT(pktp->txbuf_item == NULL);
pktp->txbuf_item = txbuf_item;
pktp->vlan_tci = vlan_tci;
pktp->pflags = pflags;
atomic_inc_64(&srp->tx_flow);
ASSERT(pktp->tx_ready == B_FALSE);
pktp->tx_ready = B_TRUE;
bge_send_serial(bgep, srp);
srp->pushed_bytes += MBLKL(mp);
freemsg(mp);
return (NULL);
}
static mblk_t *
bge_send(bge_t *bgep, mblk_t *mp)
{
send_ring_t *ring;
ring = &bgep->send[0];
return (bge_ring_tx(ring, mp));
}
uint_t
bge_send_drain(caddr_t arg)
{
uint_t ring = 0;
bge_t *bgep;
send_ring_t *srp;
bgep = (void *)arg;
BGE_TRACE(("bge_send_drain($%p)", (void *)bgep));
srp = &bgep->send[ring];
bge_send_serial(bgep, srp);
if (bgep->tx_resched_needed &&
(srp->tx_flow < srp->tx_buffers_low) &&
(bgep->bge_mac_state == BGE_MAC_STARTED)) {
mac_tx_update(bgep->mh);
bgep->tx_resched_needed = B_FALSE;
bgep->tx_resched++;
}
return (DDI_INTR_CLAIMED);
}
mblk_t *
bge_m_tx(void *arg, mblk_t *mp)
{
bge_t *bgep = arg;
mblk_t *next;
BGE_TRACE(("bge_m_tx($%p, $%p)", arg, (void *)mp));
ASSERT(mp != NULL);
ASSERT(bgep->bge_mac_state == BGE_MAC_STARTED);
rw_enter(bgep->errlock, RW_READER);
if ((bgep->bge_chip_state != BGE_CHIP_RUNNING) ||
!(bgep->param_link_up)) {
BGE_DEBUG(("bge_m_tx: chip not running or link down"));
freemsgchain(mp);
mp = NULL;
}
while (mp != NULL) {
next = mp->b_next;
mp->b_next = NULL;
if ((mp = bge_send(bgep, mp)) != NULL) {
mp->b_next = next;
break;
}
mp = next;
}
rw_exit(bgep->errlock);
return (mp);
}