xref: /illumos-gate/usr/src/uts/common/io/nxge/nxge_send.c (revision 1f8914d5)
144961713Sgirish /*
244961713Sgirish  * CDDL HEADER START
344961713Sgirish  *
444961713Sgirish  * The contents of this file are subject to the terms of the
544961713Sgirish  * Common Development and Distribution License (the "License").
644961713Sgirish  * You may not use this file except in compliance with the License.
744961713Sgirish  *
844961713Sgirish  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
944961713Sgirish  * or http://www.opensolaris.org/os/licensing.
1044961713Sgirish  * See the License for the specific language governing permissions
1144961713Sgirish  * and limitations under the License.
1244961713Sgirish  *
1344961713Sgirish  * When distributing Covered Code, include this CDDL HEADER in each
1444961713Sgirish  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1544961713Sgirish  * If applicable, add the following below this CDDL HEADER, with the
1644961713Sgirish  * fields enclosed by brackets "[]" replaced with your own identifying
1744961713Sgirish  * information: Portions Copyright [yyyy] [name of copyright owner]
1844961713Sgirish  *
1944961713Sgirish  * CDDL HEADER END
2044961713Sgirish  */
2144961713Sgirish /*
22a3c5bd6dSspeer  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
2344961713Sgirish  * Use is subject to license terms.
2444961713Sgirish  */
2544961713Sgirish 
2644961713Sgirish #pragma ident	"%Z%%M%	%I%	%E% SMI"
2744961713Sgirish 
28a3c5bd6dSspeer #include <sys/nxge/nxge_impl.h>
2944961713Sgirish 
3044961713Sgirish extern uint32_t		nxge_reclaim_pending;
3144961713Sgirish extern uint32_t 	nxge_bcopy_thresh;
3244961713Sgirish extern uint32_t 	nxge_dvma_thresh;
3344961713Sgirish extern uint32_t 	nxge_dma_stream_thresh;
3444961713Sgirish extern uint32_t		nxge_tx_minfree;
3544961713Sgirish extern uint32_t		nxge_tx_intr_thres;
3644961713Sgirish extern uint32_t		nxge_tx_max_gathers;
3744961713Sgirish extern uint32_t		nxge_tx_tiny_pack;
3844961713Sgirish extern uint32_t		nxge_tx_use_bcopy;
3944961713Sgirish extern uint32_t		nxge_tx_lb_policy;
4044961713Sgirish extern uint32_t		nxge_no_tx_lb;
41*1f8914d5Sml extern nxge_tx_mode_t	nxge_tx_scheme;
4244961713Sgirish 
4344961713Sgirish typedef struct _mac_tx_hint {
4444961713Sgirish 	uint16_t	sap;
4544961713Sgirish 	uint16_t	vid;
4644961713Sgirish 	void		*hash;
4744961713Sgirish } mac_tx_hint_t, *p_mac_tx_hint_t;
4844961713Sgirish 
4944961713Sgirish int nxge_tx_lb_ring_1(p_mblk_t, uint32_t, p_mac_tx_hint_t);
5044961713Sgirish 
5144961713Sgirish int
5244961713Sgirish nxge_start(p_nxge_t nxgep, p_tx_ring_t tx_ring_p, p_mblk_t mp)
5344961713Sgirish {
5444961713Sgirish 	int 			status = 0;
5544961713Sgirish 	p_tx_desc_t 		tx_desc_ring_vp;
5644961713Sgirish 	npi_handle_t		npi_desc_handle;
5744961713Sgirish 	nxge_os_dma_handle_t 	tx_desc_dma_handle;
5844961713Sgirish 	p_tx_desc_t 		tx_desc_p;
5944961713Sgirish 	p_tx_msg_t 		tx_msg_ring;
6044961713Sgirish 	p_tx_msg_t 		tx_msg_p;
6144961713Sgirish 	tx_desc_t		tx_desc, *tmp_desc_p;
6244961713Sgirish 	tx_desc_t		sop_tx_desc, *sop_tx_desc_p;
6344961713Sgirish 	p_tx_pkt_header_t	hdrp;
6444961713Sgirish 	p_tx_pkt_hdr_all_t	pkthdrp;
6544961713Sgirish 	uint8_t			npads = 0;
6644961713Sgirish 	uint64_t 		dma_ioaddr;
6744961713Sgirish 	uint32_t		dma_flags;
6844961713Sgirish 	int			last_bidx;
6944961713Sgirish 	uint8_t 		*b_rptr;
7044961713Sgirish 	caddr_t 		kaddr;
7144961713Sgirish 	uint32_t		nmblks;
7244961713Sgirish 	uint32_t		ngathers;
7344961713Sgirish 	uint32_t		clen;
7444961713Sgirish 	int 			len;
7544961713Sgirish 	uint32_t		pkt_len, pack_len, min_len;
7644961713Sgirish 	uint32_t		bcopy_thresh;
7744961713Sgirish 	int 			i, cur_index, sop_index;
7844961713Sgirish 	uint16_t		tail_index;
7944961713Sgirish 	boolean_t		tail_wrap = B_FALSE;
8044961713Sgirish 	nxge_dma_common_t	desc_area;
8144961713Sgirish 	nxge_os_dma_handle_t 	dma_handle;
8244961713Sgirish 	ddi_dma_cookie_t 	dma_cookie;
8344961713Sgirish 	npi_handle_t		npi_handle;
8444961713Sgirish 	p_mblk_t 		nmp;
8544961713Sgirish 	p_mblk_t		t_mp;
8644961713Sgirish 	uint32_t 		ncookies;
8744961713Sgirish 	boolean_t 		good_packet;
8844961713Sgirish 	boolean_t 		mark_mode = B_FALSE;
8944961713Sgirish 	p_nxge_stats_t 		statsp;
9044961713Sgirish 	p_nxge_tx_ring_stats_t tdc_stats;
9144961713Sgirish 	t_uscalar_t 		start_offset = 0;
9244961713Sgirish 	t_uscalar_t 		stuff_offset = 0;
9344961713Sgirish 	t_uscalar_t 		end_offset = 0;
9444961713Sgirish 	t_uscalar_t 		value = 0;
9544961713Sgirish 	t_uscalar_t 		cksum_flags = 0;
9644961713Sgirish 	boolean_t		cksum_on = B_FALSE;
9744961713Sgirish 	uint32_t		boff = 0;
9844961713Sgirish 	uint64_t		tot_xfer_len = 0, tmp_len = 0;
9944961713Sgirish 	boolean_t		header_set = B_FALSE;
10044961713Sgirish #ifdef NXGE_DEBUG
10144961713Sgirish 	p_tx_desc_t 		tx_desc_ring_pp;
10244961713Sgirish 	p_tx_desc_t 		tx_desc_pp;
10344961713Sgirish 	tx_desc_t		*save_desc_p;
10444961713Sgirish 	int			dump_len;
10544961713Sgirish 	int			sad_len;
10644961713Sgirish 	uint64_t		sad;
10744961713Sgirish 	int			xfer_len;
10844961713Sgirish 	uint32_t		msgsize;
10944961713Sgirish #endif
11044961713Sgirish 
11144961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL,
11244961713Sgirish 		"==> nxge_start: tx dma channel %d", tx_ring_p->tdc));
11344961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL,
11444961713Sgirish 		"==> nxge_start: Starting tdc %d desc pending %d",
11544961713Sgirish 		tx_ring_p->tdc, tx_ring_p->descs_pending));
11644961713Sgirish 
11744961713Sgirish 	statsp = nxgep->statsp;
11844961713Sgirish 
11944961713Sgirish 	if (nxgep->statsp->port_stats.lb_mode == nxge_lb_normal) {
12014ea4bb7Ssd 		if (!statsp->mac_stats.link_up) {
12144961713Sgirish 			freemsg(mp);
12244961713Sgirish 			NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_start: "
12344961713Sgirish 				"link not up or LB mode"));
12444961713Sgirish 			goto nxge_start_fail1;
12544961713Sgirish 		}
12644961713Sgirish 	}
12744961713Sgirish 
12844961713Sgirish 	hcksum_retrieve(mp, NULL, NULL, &start_offset,
12944961713Sgirish 		&stuff_offset, &end_offset, &value, &cksum_flags);
13044961713Sgirish 	if (!NXGE_IS_VLAN_PACKET(mp->b_rptr)) {
13144961713Sgirish 		start_offset += sizeof (ether_header_t);
13244961713Sgirish 		stuff_offset += sizeof (ether_header_t);
13344961713Sgirish 	} else {
13444961713Sgirish 		start_offset += sizeof (struct ether_vlan_header);
13544961713Sgirish 		stuff_offset += sizeof (struct ether_vlan_header);
13644961713Sgirish 	}
13744961713Sgirish 
13844961713Sgirish 	if (cksum_flags & HCK_PARTIALCKSUM) {
13944961713Sgirish 		NXGE_DEBUG_MSG((nxgep, TX_CTL,
14044961713Sgirish 			"==> nxge_start: cksum_flags 0x%x (partial checksum) ",
14144961713Sgirish 			cksum_flags));
14244961713Sgirish 		cksum_on = B_TRUE;
14344961713Sgirish 	}
14444961713Sgirish 
14544961713Sgirish #ifdef	NXGE_DEBUG
14644961713Sgirish 	if (tx_ring_p->descs_pending) {
14744961713Sgirish 		NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_start: "
14844961713Sgirish 			"desc pending %d ", tx_ring_p->descs_pending));
14944961713Sgirish 	}
15044961713Sgirish 
15144961713Sgirish 	dump_len = (int)(MBLKL(mp));
15244961713Sgirish 	dump_len = (dump_len > 128) ? 128: dump_len;
15344961713Sgirish 
15444961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL,
15544961713Sgirish 		"==> nxge_start: tdc %d: dumping ...: b_rptr $%p "
15644961713Sgirish 		"(Before header reserve: ORIGINAL LEN %d)",
15744961713Sgirish 		tx_ring_p->tdc,
15844961713Sgirish 		mp->b_rptr,
15944961713Sgirish 		dump_len));
16044961713Sgirish 
16144961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_start: dump packets "
16244961713Sgirish 		"(IP ORIGINAL b_rptr $%p): %s", mp->b_rptr,
16344961713Sgirish 		nxge_dump_packet((char *)mp->b_rptr, dump_len)));
16444961713Sgirish #endif
16544961713Sgirish 
16644961713Sgirish 	MUTEX_ENTER(&tx_ring_p->lock);
16744961713Sgirish 	tdc_stats = tx_ring_p->tdc_stats;
16844961713Sgirish 	mark_mode = (tx_ring_p->descs_pending &&
16944961713Sgirish 		((tx_ring_p->tx_ring_size - tx_ring_p->descs_pending)
17044961713Sgirish 		< nxge_tx_minfree));
17144961713Sgirish 
17244961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL,
17344961713Sgirish 		"TX Descriptor ring is channel %d mark mode %d",
17444961713Sgirish 		tx_ring_p->tdc, mark_mode));
17544961713Sgirish 
17644961713Sgirish 	if (!nxge_txdma_reclaim(nxgep, tx_ring_p, nxge_tx_minfree)) {
17744961713Sgirish 		NXGE_DEBUG_MSG((nxgep, TX_CTL,
17844961713Sgirish 			"TX Descriptor ring is full: channel %d",
17944961713Sgirish 			tx_ring_p->tdc));
18044961713Sgirish 		cas32((uint32_t *)&tx_ring_p->queueing, 0, 1);
18144961713Sgirish 		tdc_stats->tx_no_desc++;
18244961713Sgirish 		MUTEX_EXIT(&tx_ring_p->lock);
18344961713Sgirish 		if (nxgep->resched_needed && !nxgep->resched_running) {
18444961713Sgirish 			nxgep->resched_running = B_TRUE;
18544961713Sgirish 			ddi_trigger_softintr(nxgep->resched_id);
18644961713Sgirish 		}
18744961713Sgirish 		status = 1;
18844961713Sgirish 		goto nxge_start_fail1;
18944961713Sgirish 	}
19044961713Sgirish 
19144961713Sgirish 	nmp = mp;
19244961713Sgirish 	i = sop_index = tx_ring_p->wr_index;
19344961713Sgirish 	nmblks = 0;
19444961713Sgirish 	ngathers = 0;
19544961713Sgirish 	pkt_len = 0;
19644961713Sgirish 	pack_len = 0;
19744961713Sgirish 	clen = 0;
19844961713Sgirish 	last_bidx = -1;
19944961713Sgirish 	good_packet = B_TRUE;
20044961713Sgirish 
20144961713Sgirish 	desc_area = tx_ring_p->tdc_desc;
20244961713Sgirish 	npi_handle = desc_area.npi_handle;
20344961713Sgirish 	npi_desc_handle.regh = (nxge_os_acc_handle_t)
20444961713Sgirish 			DMA_COMMON_ACC_HANDLE(desc_area);
20544961713Sgirish 	tx_desc_ring_vp = (p_tx_desc_t)DMA_COMMON_VPTR(desc_area);
20644961713Sgirish #ifdef	NXGE_DEBUG
20744961713Sgirish 	tx_desc_ring_pp = (p_tx_desc_t)DMA_COMMON_IOADDR(desc_area);
20844961713Sgirish #endif
20944961713Sgirish 	tx_desc_dma_handle = (nxge_os_dma_handle_t)
21044961713Sgirish 			DMA_COMMON_HANDLE(desc_area);
21144961713Sgirish 	tx_msg_ring = tx_ring_p->tx_msg_ring;
21244961713Sgirish 
21344961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_start: wr_index %d i %d",
21444961713Sgirish 		sop_index, i));
21544961713Sgirish 
21644961713Sgirish #ifdef	NXGE_DEBUG
21744961713Sgirish 	msgsize = msgdsize(nmp);
21844961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL,
21944961713Sgirish 		"==> nxge_start(1): wr_index %d i %d msgdsize %d",
22044961713Sgirish 		sop_index, i, msgsize));
22144961713Sgirish #endif
22244961713Sgirish 	/*
22344961713Sgirish 	 * The first 16 bytes of the premapped buffer are reserved
22444961713Sgirish 	 * for header. No padding will be used.
22544961713Sgirish 	 */
22644961713Sgirish 	pkt_len = pack_len = boff = TX_PKT_HEADER_SIZE;
227*1f8914d5Sml 	if (nxge_tx_use_bcopy && (nxgep->niu_type != N2_NIU)) {
22844961713Sgirish 		bcopy_thresh = (nxge_bcopy_thresh - TX_PKT_HEADER_SIZE);
22944961713Sgirish 	} else {
23044961713Sgirish 		bcopy_thresh = (TX_BCOPY_SIZE - TX_PKT_HEADER_SIZE);
23144961713Sgirish 	}
23244961713Sgirish 	while (nmp) {
23344961713Sgirish 		good_packet = B_TRUE;
23444961713Sgirish 		b_rptr = nmp->b_rptr;
23544961713Sgirish 		len = MBLKL(nmp);
23644961713Sgirish 		if (len <= 0) {
23744961713Sgirish 			nmp = nmp->b_cont;
23844961713Sgirish 			continue;
23944961713Sgirish 		}
24044961713Sgirish 		nmblks++;
24144961713Sgirish 
24244961713Sgirish 		NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_start(1): nmblks %d "
24344961713Sgirish 			"len %d pkt_len %d pack_len %d",
24444961713Sgirish 			nmblks, len, pkt_len, pack_len));
24544961713Sgirish 		/*
24614ea4bb7Ssd 		 * Hardware limits the transfer length to 4K for NIU and
24714ea4bb7Ssd 		 * 4076 (TX_MAX_TRANSFER_LENGTH) for Neptune. But we just
24814ea4bb7Ssd 		 * use TX_MAX_TRANSFER_LENGTH as the limit for both.
24914ea4bb7Ssd 		 * If len is longer than the limit, then we break nmp into
25014ea4bb7Ssd 		 * two chunks: Make the first chunk equal to the limit and
25114ea4bb7Ssd 		 * the second chunk for the remaining data. If the second
25214ea4bb7Ssd 		 * chunk is still larger than the limit, then it will be
25314ea4bb7Ssd 		 * broken into two in the next pass.
25444961713Sgirish 		 */
25514ea4bb7Ssd 		if (len > TX_MAX_TRANSFER_LENGTH - TX_PKT_HEADER_SIZE) {
25644961713Sgirish 			t_mp = dupb(nmp);
25714ea4bb7Ssd 			nmp->b_wptr = nmp->b_rptr +
25814ea4bb7Ssd 				(TX_MAX_TRANSFER_LENGTH - TX_PKT_HEADER_SIZE);
25944961713Sgirish 			t_mp->b_rptr = nmp->b_wptr;
26044961713Sgirish 			t_mp->b_cont = nmp->b_cont;
26144961713Sgirish 			nmp->b_cont = t_mp;
26244961713Sgirish 			len = MBLKL(nmp);
26344961713Sgirish 		}
26444961713Sgirish 
26544961713Sgirish 		tx_desc.value = 0;
26644961713Sgirish 		tx_desc_p = &tx_desc_ring_vp[i];
26744961713Sgirish #ifdef	NXGE_DEBUG
26844961713Sgirish 		tx_desc_pp = &tx_desc_ring_pp[i];
26944961713Sgirish #endif
27044961713Sgirish 		tx_msg_p = &tx_msg_ring[i];
27144961713Sgirish 		npi_desc_handle.regp = (uint64_t)tx_desc_p;
27244961713Sgirish 		if (!header_set &&
27344961713Sgirish 			((!nxge_tx_use_bcopy && (len > TX_BCOPY_SIZE)) ||
27444961713Sgirish 				(len >= bcopy_thresh))) {
27544961713Sgirish 			header_set = B_TRUE;
27644961713Sgirish 			bcopy_thresh += TX_PKT_HEADER_SIZE;
27744961713Sgirish 			boff = 0;
27844961713Sgirish 			pack_len = 0;
27944961713Sgirish 			kaddr = (caddr_t)DMA_COMMON_VPTR(tx_msg_p->buf_dma);
28044961713Sgirish 			hdrp = (p_tx_pkt_header_t)kaddr;
28144961713Sgirish 			clen = pkt_len;
28244961713Sgirish 			dma_handle = tx_msg_p->buf_dma_handle;
28344961713Sgirish 			dma_ioaddr = DMA_COMMON_IOADDR(tx_msg_p->buf_dma);
28444961713Sgirish 			(void) ddi_dma_sync(dma_handle,
28544961713Sgirish 				i * nxge_bcopy_thresh, nxge_bcopy_thresh,
28644961713Sgirish 				DDI_DMA_SYNC_FORDEV);
28744961713Sgirish 
28844961713Sgirish 			tx_msg_p->flags.dma_type = USE_BCOPY;
28944961713Sgirish 			goto nxge_start_control_header_only;
29044961713Sgirish 		}
29144961713Sgirish 
29244961713Sgirish 		pkt_len += len;
29344961713Sgirish 		pack_len += len;
29444961713Sgirish 
29544961713Sgirish 		NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_start(3): "
29644961713Sgirish 			"desc entry %d "
29744961713Sgirish 			"DESC IOADDR $%p "
29844961713Sgirish 			"desc_vp $%p tx_desc_p $%p "
29944961713Sgirish 			"desc_pp $%p tx_desc_pp $%p "
30044961713Sgirish 			"len %d pkt_len %d pack_len %d",
30144961713Sgirish 			i,
30244961713Sgirish 			DMA_COMMON_IOADDR(desc_area),
30344961713Sgirish 			tx_desc_ring_vp, tx_desc_p,
30444961713Sgirish 			tx_desc_ring_pp, tx_desc_pp,
30544961713Sgirish 			len, pkt_len, pack_len));
30644961713Sgirish 
30744961713Sgirish 		if (len < bcopy_thresh) {
30844961713Sgirish 			NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_start(4): "
30944961713Sgirish 				"USE BCOPY: "));
31044961713Sgirish 			if (nxge_tx_tiny_pack) {
31144961713Sgirish 				uint32_t blst =
31244961713Sgirish 					TXDMA_DESC_NEXT_INDEX(i, -1,
31344961713Sgirish 						tx_ring_p->tx_wrap_mask);
31444961713Sgirish 				NXGE_DEBUG_MSG((nxgep, TX_CTL,
31544961713Sgirish 					"==> nxge_start(5): pack"));
31644961713Sgirish 				if ((pack_len <= bcopy_thresh) &&
31744961713Sgirish 					(last_bidx == blst)) {
31844961713Sgirish 					NXGE_DEBUG_MSG((nxgep, TX_CTL,
31944961713Sgirish 						"==> nxge_start: pack(6) "
32044961713Sgirish 						"(pkt_len %d pack_len %d)",
32144961713Sgirish 						pkt_len, pack_len));
32244961713Sgirish 					i = blst;
32344961713Sgirish 					tx_desc_p = &tx_desc_ring_vp[i];
32444961713Sgirish #ifdef	NXGE_DEBUG
32544961713Sgirish 					tx_desc_pp = &tx_desc_ring_pp[i];
32644961713Sgirish #endif
32744961713Sgirish 					tx_msg_p = &tx_msg_ring[i];
32844961713Sgirish 					boff = pack_len - len;
32944961713Sgirish 					ngathers--;
330a3c5bd6dSspeer 				} else if (pack_len > bcopy_thresh &&
331a3c5bd6dSspeer 					header_set) {
33244961713Sgirish 					pack_len = len;
33344961713Sgirish 					boff = 0;
33444961713Sgirish 					bcopy_thresh = nxge_bcopy_thresh;
33544961713Sgirish 					NXGE_DEBUG_MSG((nxgep, TX_CTL,
33644961713Sgirish 						"==> nxge_start(7): > max NEW "
33744961713Sgirish 						"bcopy thresh %d "
33844961713Sgirish 						"pkt_len %d pack_len %d(next)",
33944961713Sgirish 						bcopy_thresh,
34044961713Sgirish 						pkt_len, pack_len));
34144961713Sgirish 				}
34244961713Sgirish 				last_bidx = i;
34344961713Sgirish 			}
34444961713Sgirish 			kaddr = (caddr_t)DMA_COMMON_VPTR(tx_msg_p->buf_dma);
34544961713Sgirish 			if ((boff == TX_PKT_HEADER_SIZE) && (nmblks == 1)) {
34644961713Sgirish 				hdrp = (p_tx_pkt_header_t)kaddr;
34744961713Sgirish 				header_set = B_TRUE;
34844961713Sgirish 				NXGE_DEBUG_MSG((nxgep, TX_CTL,
34944961713Sgirish 					"==> nxge_start(7_x2): "
35044961713Sgirish 					"pkt_len %d pack_len %d (new hdrp $%p)",
35144961713Sgirish 					pkt_len, pack_len, hdrp));
35244961713Sgirish 			}
35344961713Sgirish 			tx_msg_p->flags.dma_type = USE_BCOPY;
35444961713Sgirish 			kaddr += boff;
35544961713Sgirish 			NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_start(8): "
35644961713Sgirish 				"USE BCOPY: before bcopy "
35744961713Sgirish 				"DESC IOADDR $%p entry %d "
35844961713Sgirish 				"bcopy packets %d "
35944961713Sgirish 				"bcopy kaddr $%p "
36044961713Sgirish 				"bcopy ioaddr (SAD) $%p "
36144961713Sgirish 				"bcopy clen %d "
36244961713Sgirish 				"bcopy boff %d",
36344961713Sgirish 				DMA_COMMON_IOADDR(desc_area), i,
36444961713Sgirish 				tdc_stats->tx_hdr_pkts,
36544961713Sgirish 				kaddr,
36644961713Sgirish 				dma_ioaddr,
36744961713Sgirish 				clen,
36844961713Sgirish 				boff));
36944961713Sgirish 			NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_start: "
37044961713Sgirish 				"1USE BCOPY: "));
37144961713Sgirish 			NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_start: "
37244961713Sgirish 				"2USE BCOPY: "));
37344961713Sgirish 			NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_start: "
37444961713Sgirish 				"last USE BCOPY: copy from b_rptr $%p "
37544961713Sgirish 				"to KADDR $%p (len %d offset %d",
37644961713Sgirish 				b_rptr, kaddr, len, boff));
37744961713Sgirish 
37844961713Sgirish 			bcopy(b_rptr, kaddr, len);
37944961713Sgirish 
38044961713Sgirish #ifdef	NXGE_DEBUG
38144961713Sgirish 			dump_len = (len > 128) ? 128: len;
38244961713Sgirish 			NXGE_DEBUG_MSG((nxgep, TX_CTL,
38344961713Sgirish 				"==> nxge_start: dump packets "
38444961713Sgirish 				"(After BCOPY len %d)"
38544961713Sgirish 				"(b_rptr $%p): %s", len, nmp->b_rptr,
38644961713Sgirish 				nxge_dump_packet((char *)nmp->b_rptr,
38744961713Sgirish 				dump_len)));
38844961713Sgirish #endif
38944961713Sgirish 
39044961713Sgirish 			dma_handle = tx_msg_p->buf_dma_handle;
39144961713Sgirish 			dma_ioaddr = DMA_COMMON_IOADDR(tx_msg_p->buf_dma);
39244961713Sgirish 			(void) ddi_dma_sync(dma_handle,
39344961713Sgirish 				i * nxge_bcopy_thresh, nxge_bcopy_thresh,
39444961713Sgirish 					DDI_DMA_SYNC_FORDEV);
39544961713Sgirish 			clen = len + boff;
39644961713Sgirish 			tdc_stats->tx_hdr_pkts++;
39744961713Sgirish 			NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_start(9): "
39844961713Sgirish 				"USE BCOPY: "
39944961713Sgirish 				"DESC IOADDR $%p entry %d "
40044961713Sgirish 				"bcopy packets %d "
40144961713Sgirish 				"bcopy kaddr $%p "
40244961713Sgirish 				"bcopy ioaddr (SAD) $%p "
40344961713Sgirish 				"bcopy clen %d "
40444961713Sgirish 				"bcopy boff %d",
40544961713Sgirish 				DMA_COMMON_IOADDR(desc_area),
40644961713Sgirish 				i,
40744961713Sgirish 				tdc_stats->tx_hdr_pkts,
40844961713Sgirish 				kaddr,
40944961713Sgirish 				dma_ioaddr,
41044961713Sgirish 				clen,
41144961713Sgirish 				boff));
41244961713Sgirish 		} else {
41344961713Sgirish 			NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_start(12): "
41444961713Sgirish 				"USE DVMA: len %d", len));
41544961713Sgirish 			tx_msg_p->flags.dma_type = USE_DMA;
41644961713Sgirish 			dma_flags = DDI_DMA_WRITE;
41744961713Sgirish 			if (len < nxge_dma_stream_thresh) {
41844961713Sgirish 				dma_flags |= DDI_DMA_CONSISTENT;
41944961713Sgirish 			} else {
42044961713Sgirish 				dma_flags |= DDI_DMA_STREAMING;
42144961713Sgirish 			}
42244961713Sgirish 
42344961713Sgirish 			dma_handle = tx_msg_p->dma_handle;
42444961713Sgirish 			status = ddi_dma_addr_bind_handle(dma_handle, NULL,
42544961713Sgirish 				(caddr_t)b_rptr, len, dma_flags,
42644961713Sgirish 				DDI_DMA_DONTWAIT, NULL,
42744961713Sgirish 				&dma_cookie, &ncookies);
42844961713Sgirish 			if (status == DDI_DMA_MAPPED) {
42944961713Sgirish 				dma_ioaddr = dma_cookie.dmac_laddress;
43044961713Sgirish 				len = (int)dma_cookie.dmac_size;
43144961713Sgirish 				clen = (uint32_t)dma_cookie.dmac_size;
43244961713Sgirish 				NXGE_DEBUG_MSG((nxgep, TX_CTL,
43344961713Sgirish 					"==> nxge_start(12_1): "
43444961713Sgirish 					"USE DVMA: len %d clen %d "
43544961713Sgirish 					"ngathers %d",
43644961713Sgirish 					len, clen,
43744961713Sgirish 					ngathers));
43844961713Sgirish 
43944961713Sgirish 				npi_desc_handle.regp = (uint64_t)tx_desc_p;
44044961713Sgirish 				while (ncookies > 1) {
44144961713Sgirish 					ngathers++;
44244961713Sgirish 					/*
44344961713Sgirish 					 * this is the fix for multiple
44444961713Sgirish 					 * cookies, which are basicaly
44544961713Sgirish 					 * a descriptor entry, we don't set
44644961713Sgirish 					 * SOP bit as well as related fields
44744961713Sgirish 					 */
44844961713Sgirish 
44944961713Sgirish 					(void) npi_txdma_desc_gather_set(
45044961713Sgirish 						npi_desc_handle,
45144961713Sgirish 						&tx_desc,
45244961713Sgirish 						(ngathers -1),
45344961713Sgirish 						mark_mode,
45444961713Sgirish 						ngathers,
45544961713Sgirish 						dma_ioaddr,
45644961713Sgirish 						clen);
45744961713Sgirish 
45844961713Sgirish 					tx_msg_p->tx_msg_size = clen;
45944961713Sgirish 					NXGE_DEBUG_MSG((nxgep, TX_CTL,
46044961713Sgirish 						"==> nxge_start:  DMA "
46144961713Sgirish 						"ncookie %d "
46244961713Sgirish 						"ngathers %d "
46344961713Sgirish 						"dma_ioaddr $%p len %d"
46444961713Sgirish 						"desc $%p descp $%p (%d)",
46544961713Sgirish 						ncookies,
46644961713Sgirish 						ngathers,
46744961713Sgirish 						dma_ioaddr, clen,
46844961713Sgirish 						*tx_desc_p, tx_desc_p, i));
46944961713Sgirish 
47044961713Sgirish 					ddi_dma_nextcookie(dma_handle,
47144961713Sgirish 							&dma_cookie);
47244961713Sgirish 					dma_ioaddr =
47344961713Sgirish 						dma_cookie.dmac_laddress;
47444961713Sgirish 
47544961713Sgirish 					len = (int)dma_cookie.dmac_size;
47644961713Sgirish 					clen = (uint32_t)dma_cookie.dmac_size;
47744961713Sgirish 					NXGE_DEBUG_MSG((nxgep, TX_CTL,
47844961713Sgirish 						"==> nxge_start(12_2): "
47944961713Sgirish 						"USE DVMA: len %d clen %d ",
48044961713Sgirish 						len, clen));
48144961713Sgirish 
48244961713Sgirish 					i = TXDMA_DESC_NEXT_INDEX(i, 1,
48344961713Sgirish 						tx_ring_p->tx_wrap_mask);
48444961713Sgirish 					tx_desc_p = &tx_desc_ring_vp[i];
48544961713Sgirish 
48644961713Sgirish 					npi_desc_handle.regp =
48744961713Sgirish 						(uint64_t)tx_desc_p;
48844961713Sgirish 					tx_msg_p = &tx_msg_ring[i];
48944961713Sgirish 					tx_msg_p->flags.dma_type = USE_NONE;
49044961713Sgirish 					tx_desc.value = 0;
49144961713Sgirish 
49244961713Sgirish 					ncookies--;
49344961713Sgirish 				}
49444961713Sgirish 				tdc_stats->tx_ddi_pkts++;
49544961713Sgirish 				NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_start:"
49644961713Sgirish 					"DMA: ddi packets %d",
49744961713Sgirish 					tdc_stats->tx_ddi_pkts));
49844961713Sgirish 			} else {
49944961713Sgirish 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
50044961713Sgirish 				    "dma mapping failed for %d "
50144961713Sgirish 				    "bytes addr $%p flags %x (%d)",
50244961713Sgirish 				    len, b_rptr, status, status));
50344961713Sgirish 				good_packet = B_FALSE;
50444961713Sgirish 				tdc_stats->tx_dma_bind_fail++;
50544961713Sgirish 				tx_msg_p->flags.dma_type = USE_NONE;
50644961713Sgirish 				goto nxge_start_fail2;
50744961713Sgirish 			}
50844961713Sgirish 		} /* ddi dvma */
50944961713Sgirish 
51044961713Sgirish 		nmp = nmp->b_cont;
51144961713Sgirish nxge_start_control_header_only:
51244961713Sgirish 		npi_desc_handle.regp = (uint64_t)tx_desc_p;
51344961713Sgirish 		ngathers++;
51444961713Sgirish 
51544961713Sgirish 		if (ngathers == 1) {
51644961713Sgirish #ifdef	NXGE_DEBUG
51744961713Sgirish 			save_desc_p = &sop_tx_desc;
51844961713Sgirish #endif
51944961713Sgirish 			sop_tx_desc_p = &sop_tx_desc;
52044961713Sgirish 			sop_tx_desc_p->value = 0;
52144961713Sgirish 			sop_tx_desc_p->bits.hdw.tr_len = clen;
52244961713Sgirish 			sop_tx_desc_p->bits.hdw.sad = dma_ioaddr >> 32;
52344961713Sgirish 			sop_tx_desc_p->bits.ldw.sad = dma_ioaddr & 0xffffffff;
52444961713Sgirish 		} else {
52544961713Sgirish #ifdef	NXGE_DEBUG
52644961713Sgirish 			save_desc_p = &tx_desc;
52744961713Sgirish #endif
52844961713Sgirish 			tmp_desc_p = &tx_desc;
52944961713Sgirish 			tmp_desc_p->value = 0;
53044961713Sgirish 			tmp_desc_p->bits.hdw.tr_len = clen;
53144961713Sgirish 			tmp_desc_p->bits.hdw.sad = dma_ioaddr >> 32;
53244961713Sgirish 			tmp_desc_p->bits.ldw.sad = dma_ioaddr & 0xffffffff;
53344961713Sgirish 
53444961713Sgirish 			tx_desc_p->value = tmp_desc_p->value;
53544961713Sgirish 		}
53644961713Sgirish 
53744961713Sgirish 		NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_start(13): "
53844961713Sgirish 			"Desc_entry %d ngathers %d "
53944961713Sgirish 			"desc_vp $%p tx_desc_p $%p "
54044961713Sgirish 			"len %d clen %d pkt_len %d pack_len %d nmblks %d "
54144961713Sgirish 			"dma_ioaddr (SAD) $%p mark %d",
54244961713Sgirish 			i, ngathers,
54344961713Sgirish 			tx_desc_ring_vp, tx_desc_p,
54444961713Sgirish 			len, clen, pkt_len, pack_len, nmblks,
54544961713Sgirish 			dma_ioaddr, mark_mode));
54644961713Sgirish 
54744961713Sgirish #ifdef NXGE_DEBUG
54844961713Sgirish 		npi_desc_handle.nxgep = nxgep;
54944961713Sgirish 		npi_desc_handle.function.function = nxgep->function_num;
55044961713Sgirish 		npi_desc_handle.function.instance = nxgep->instance;
55144961713Sgirish 		sad = (save_desc_p->value & TX_PKT_DESC_SAD_MASK);
55244961713Sgirish 		xfer_len = ((save_desc_p->value & TX_PKT_DESC_TR_LEN_MASK) >>
55344961713Sgirish 			TX_PKT_DESC_TR_LEN_SHIFT);
55444961713Sgirish 
55544961713Sgirish 
55644961713Sgirish 		NXGE_DEBUG_MSG((nxgep, TX_CTL, "\n\t: value 0x%llx\n"
55744961713Sgirish 			"\t\tsad $%p\ttr_len %d len %d\tnptrs %d\t"
55844961713Sgirish 			"mark %d sop %d\n",
55944961713Sgirish 			save_desc_p->value,
56044961713Sgirish 			sad,
56144961713Sgirish 			save_desc_p->bits.hdw.tr_len,
56244961713Sgirish 			xfer_len,
56344961713Sgirish 			save_desc_p->bits.hdw.num_ptr,
56444961713Sgirish 			save_desc_p->bits.hdw.mark,
56544961713Sgirish 			save_desc_p->bits.hdw.sop));
56644961713Sgirish 
56744961713Sgirish 		npi_txdma_dump_desc_one(npi_desc_handle, NULL, i);
56844961713Sgirish #endif
56944961713Sgirish 
57044961713Sgirish 		tx_msg_p->tx_msg_size = clen;
57144961713Sgirish 		i = TXDMA_DESC_NEXT_INDEX(i, 1, tx_ring_p->tx_wrap_mask);
57244961713Sgirish 		if (ngathers > nxge_tx_max_gathers) {
57344961713Sgirish 			good_packet = B_FALSE;
57444961713Sgirish 			hcksum_retrieve(mp, NULL, NULL, &start_offset,
57544961713Sgirish 				&stuff_offset, &end_offset, &value,
57644961713Sgirish 				&cksum_flags);
57744961713Sgirish 
57844961713Sgirish 			NXGE_DEBUG_MSG((NULL, TX_CTL,
57944961713Sgirish 				"==> nxge_start(14): pull msg - "
58044961713Sgirish 				"len %d pkt_len %d ngathers %d",
58144961713Sgirish 				len, pkt_len, ngathers));
58244961713Sgirish 			/* Pull all message blocks from b_cont */
58344961713Sgirish 			if ((msgpullup(mp, -1)) == NULL) {
58444961713Sgirish 				goto nxge_start_fail2;
58544961713Sgirish 			}
58644961713Sgirish 			goto nxge_start_fail2;
58744961713Sgirish 		}
58844961713Sgirish 	} /* while (nmp) */
58944961713Sgirish 
59044961713Sgirish 	tx_msg_p->tx_message = mp;
59144961713Sgirish 	tx_desc_p = &tx_desc_ring_vp[sop_index];
59244961713Sgirish 	npi_desc_handle.regp = (uint64_t)tx_desc_p;
59344961713Sgirish 
59444961713Sgirish 	pkthdrp = (p_tx_pkt_hdr_all_t)hdrp;
59544961713Sgirish 	pkthdrp->reserved = 0;
59644961713Sgirish 	hdrp->value = 0;
59744961713Sgirish 	(void) nxge_fill_tx_hdr(mp, B_FALSE, cksum_on,
59844961713Sgirish 		(pkt_len - TX_PKT_HEADER_SIZE), npads, pkthdrp);
59944961713Sgirish 
60044961713Sgirish 	if (pkt_len > NXGE_MTU_DEFAULT_MAX) {
60144961713Sgirish 		tdc_stats->tx_jumbo_pkts++;
60244961713Sgirish 	}
60344961713Sgirish 
60444961713Sgirish 	min_len = (nxgep->msg_min + TX_PKT_HEADER_SIZE + (npads * 2));
60544961713Sgirish 	if (pkt_len < min_len) {
60644961713Sgirish 		/* Assume we use bcopy to premapped buffers */
60744961713Sgirish 		kaddr = (caddr_t)DMA_COMMON_VPTR(tx_msg_p->buf_dma);
60844961713Sgirish 		NXGE_DEBUG_MSG((NULL, TX_CTL,
60944961713Sgirish 			"==> nxge_start(14-1): < (msg_min + 16)"
61044961713Sgirish 			"len %d pkt_len %d min_len %d bzero %d ngathers %d",
61144961713Sgirish 			len, pkt_len, min_len, (min_len - pkt_len), ngathers));
61244961713Sgirish 		bzero((kaddr + pkt_len), (min_len - pkt_len));
61344961713Sgirish 		pkt_len = tx_msg_p->tx_msg_size = min_len;
61444961713Sgirish 
61544961713Sgirish 		sop_tx_desc_p->bits.hdw.tr_len = min_len;
61644961713Sgirish 
61744961713Sgirish 		NXGE_MEM_PIO_WRITE64(npi_desc_handle, sop_tx_desc_p->value);
61844961713Sgirish 		tx_desc_p->value = sop_tx_desc_p->value;
61944961713Sgirish 
62044961713Sgirish 		NXGE_DEBUG_MSG((NULL, TX_CTL,
62144961713Sgirish 			"==> nxge_start(14-2): < msg_min - "
62244961713Sgirish 			"len %d pkt_len %d min_len %d ngathers %d",
62344961713Sgirish 			len, pkt_len, min_len, ngathers));
62444961713Sgirish 	}
62544961713Sgirish 
62644961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_start: cksum_flags 0x%x ",
62744961713Sgirish 		cksum_flags));
62844961713Sgirish 	if (cksum_flags & HCK_PARTIALCKSUM) {
62944961713Sgirish 		NXGE_DEBUG_MSG((nxgep, TX_CTL,
63044961713Sgirish 			"==> nxge_start: cksum_flags 0x%x (partial checksum) ",
63144961713Sgirish 			cksum_flags));
63244961713Sgirish 		cksum_on = B_TRUE;
63344961713Sgirish 		NXGE_DEBUG_MSG((nxgep, TX_CTL,
63444961713Sgirish 			"==> nxge_start: from IP cksum_flags 0x%x "
63544961713Sgirish 			"(partial checksum) "
63644961713Sgirish 			"start_offset %d stuff_offset %d",
63744961713Sgirish 			cksum_flags, start_offset, stuff_offset));
63844961713Sgirish 		tmp_len = (uint64_t)(start_offset >> 1);
63944961713Sgirish 		hdrp->value |= (tmp_len << TX_PKT_HEADER_L4START_SHIFT);
64044961713Sgirish 		tmp_len = (uint64_t)(stuff_offset >> 1);
64144961713Sgirish 		hdrp->value |= (tmp_len << TX_PKT_HEADER_L4STUFF_SHIFT);
64244961713Sgirish 
64344961713Sgirish 		NXGE_DEBUG_MSG((nxgep, TX_CTL,
64444961713Sgirish 			"==> nxge_start: from IP cksum_flags 0x%x "
64544961713Sgirish 			"(partial checksum) "
64644961713Sgirish 			"after SHIFT start_offset %d stuff_offset %d",
64744961713Sgirish 			cksum_flags, start_offset, stuff_offset));
64844961713Sgirish 	}
64944961713Sgirish 	{
65044961713Sgirish 		uint64_t	tmp_len;
65144961713Sgirish 
65244961713Sgirish 		/* pkt_len already includes 16 + paddings!! */
65344961713Sgirish 		/* Update the control header length */
65444961713Sgirish 		tot_xfer_len = (pkt_len - TX_PKT_HEADER_SIZE);
65544961713Sgirish 		tmp_len = hdrp->value |
65644961713Sgirish 			(tot_xfer_len << TX_PKT_HEADER_TOT_XFER_LEN_SHIFT);
65744961713Sgirish 
65844961713Sgirish 		NXGE_DEBUG_MSG((nxgep, TX_CTL,
65944961713Sgirish 			"==> nxge_start(15_x1): setting SOP "
66044961713Sgirish 			"tot_xfer_len 0x%llx (%d) pkt_len %d tmp_len "
66144961713Sgirish 			"0x%llx hdrp->value 0x%llx",
66244961713Sgirish 			tot_xfer_len, tot_xfer_len, pkt_len,
66344961713Sgirish 			tmp_len, hdrp->value));
66444961713Sgirish #if defined(_BIG_ENDIAN)
66544961713Sgirish 		hdrp->value = ddi_swap64(tmp_len);
66644961713Sgirish #else
66744961713Sgirish 		hdrp->value = tmp_len;
66844961713Sgirish #endif
66944961713Sgirish 		NXGE_DEBUG_MSG((nxgep,
67044961713Sgirish 			TX_CTL, "==> nxge_start(15_x2): setting SOP "
67144961713Sgirish 			"after SWAP: tot_xfer_len 0x%llx pkt_len %d "
67244961713Sgirish 			"tmp_len 0x%llx hdrp->value 0x%llx",
67344961713Sgirish 			tot_xfer_len, pkt_len,
67444961713Sgirish 			tmp_len, hdrp->value));
67544961713Sgirish 	}
67644961713Sgirish 
67744961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_start(15): setting SOP "
67844961713Sgirish 		"wr_index %d "
67944961713Sgirish 		"tot_xfer_len (%d) pkt_len %d npads %d",
68044961713Sgirish 		sop_index,
68144961713Sgirish 		tot_xfer_len, pkt_len,
68244961713Sgirish 		npads));
68344961713Sgirish 
68444961713Sgirish 	sop_tx_desc_p->bits.hdw.sop = 1;
68544961713Sgirish 	sop_tx_desc_p->bits.hdw.mark = mark_mode;
68644961713Sgirish 	sop_tx_desc_p->bits.hdw.num_ptr = ngathers;
68744961713Sgirish 
68844961713Sgirish 	NXGE_MEM_PIO_WRITE64(npi_desc_handle, sop_tx_desc_p->value);
68944961713Sgirish 
69044961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_start(16): set SOP done"));
69144961713Sgirish 
69244961713Sgirish #ifdef NXGE_DEBUG
69344961713Sgirish 	npi_desc_handle.nxgep = nxgep;
69444961713Sgirish 	npi_desc_handle.function.function = nxgep->function_num;
69544961713Sgirish 	npi_desc_handle.function.instance = nxgep->instance;
69644961713Sgirish 
69744961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL, "\n\t: value 0x%llx\n"
69844961713Sgirish 		"\t\tsad $%p\ttr_len %d len %d\tnptrs %d\tmark %d sop %d\n",
69944961713Sgirish 		save_desc_p->value,
70044961713Sgirish 		sad,
70144961713Sgirish 		save_desc_p->bits.hdw.tr_len,
70244961713Sgirish 		xfer_len,
70344961713Sgirish 		save_desc_p->bits.hdw.num_ptr,
70444961713Sgirish 		save_desc_p->bits.hdw.mark,
70544961713Sgirish 		save_desc_p->bits.hdw.sop));
70644961713Sgirish 	(void) npi_txdma_dump_desc_one(npi_desc_handle, NULL, sop_index);
70744961713Sgirish 
70844961713Sgirish 	dump_len = (pkt_len > 128) ? 128: pkt_len;
70944961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL,
71044961713Sgirish 		"==> nxge_start: dump packets(17) (after sop set, len "
71144961713Sgirish 		" (len/dump_len/pkt_len/tot_xfer_len) %d/%d/%d/%d):\n"
71244961713Sgirish 		"ptr $%p: %s", len, dump_len, pkt_len, tot_xfer_len,
71344961713Sgirish 		(char *)hdrp,
71444961713Sgirish 		nxge_dump_packet((char *)hdrp, dump_len)));
71544961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL,
71644961713Sgirish 		"==> nxge_start(18): TX desc sync: sop_index %d",
71744961713Sgirish 			sop_index));
71844961713Sgirish #endif
71944961713Sgirish 
72044961713Sgirish 	if ((ngathers == 1) || tx_ring_p->wr_index < i) {
72144961713Sgirish 		(void) ddi_dma_sync(tx_desc_dma_handle,
72244961713Sgirish 			sop_index * sizeof (tx_desc_t),
72344961713Sgirish 			ngathers * sizeof (tx_desc_t),
72444961713Sgirish 			DDI_DMA_SYNC_FORDEV);
72544961713Sgirish 
72644961713Sgirish 		NXGE_DEBUG_MSG((nxgep, TX_CTL, "nxge_start(19): sync 1 "
72744961713Sgirish 			"cs_off = 0x%02X cs_s_off = 0x%02X "
72844961713Sgirish 			"pkt_len %d ngathers %d sop_index %d\n",
72944961713Sgirish 			stuff_offset, start_offset,
73044961713Sgirish 			pkt_len, ngathers, sop_index));
73144961713Sgirish 	} else { /* more than one descriptor and wrap around */
73244961713Sgirish 		uint32_t nsdescs = tx_ring_p->tx_ring_size - sop_index;
73344961713Sgirish 		(void) ddi_dma_sync(tx_desc_dma_handle,
73444961713Sgirish 			sop_index * sizeof (tx_desc_t),
73544961713Sgirish 			nsdescs * sizeof (tx_desc_t),
73644961713Sgirish 			DDI_DMA_SYNC_FORDEV);
73744961713Sgirish 		NXGE_DEBUG_MSG((nxgep, TX_CTL, "nxge_start(20): sync 1 "
73844961713Sgirish 			"cs_off = 0x%02X cs_s_off = 0x%02X "
73944961713Sgirish 			"pkt_len %d ngathers %d sop_index %d\n",
74044961713Sgirish 			stuff_offset, start_offset,
74144961713Sgirish 				pkt_len, ngathers, sop_index));
74244961713Sgirish 
74344961713Sgirish 		(void) ddi_dma_sync(tx_desc_dma_handle,
74444961713Sgirish 			0,
74544961713Sgirish 			(ngathers - nsdescs) * sizeof (tx_desc_t),
74644961713Sgirish 			DDI_DMA_SYNC_FORDEV);
74744961713Sgirish 		NXGE_DEBUG_MSG((nxgep, TX_CTL, "nxge_start(21): sync 2 "
74844961713Sgirish 			"cs_off = 0x%02X cs_s_off = 0x%02X "
74944961713Sgirish 			"pkt_len %d ngathers %d sop_index %d\n",
75044961713Sgirish 			stuff_offset, start_offset,
75144961713Sgirish 			pkt_len, ngathers, sop_index));
75244961713Sgirish 	}
75344961713Sgirish 
75444961713Sgirish 	tail_index = tx_ring_p->wr_index;
75544961713Sgirish 	tail_wrap = tx_ring_p->wr_index_wrap;
75644961713Sgirish 
75744961713Sgirish 	tx_ring_p->wr_index = i;
75844961713Sgirish 	if (tx_ring_p->wr_index <= tail_index) {
75944961713Sgirish 		tx_ring_p->wr_index_wrap = ((tail_wrap == B_TRUE) ?
76044961713Sgirish 						B_FALSE : B_TRUE);
76144961713Sgirish 	}
76244961713Sgirish 
76344961713Sgirish 	tx_ring_p->descs_pending += ngathers;
76444961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_start: TX kick: "
76544961713Sgirish 		"channel %d wr_index %d wrap %d ngathers %d desc_pend %d",
76644961713Sgirish 		tx_ring_p->tdc,
76744961713Sgirish 		tx_ring_p->wr_index,
76844961713Sgirish 		tx_ring_p->wr_index_wrap,
76944961713Sgirish 		ngathers,
77044961713Sgirish 		tx_ring_p->descs_pending));
77144961713Sgirish 
77244961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_start: TX KICKING: "));
77344961713Sgirish 
77444961713Sgirish 	{
77544961713Sgirish 		tx_ring_kick_t		kick;
77644961713Sgirish 
77744961713Sgirish 		kick.value = 0;
77844961713Sgirish 		kick.bits.ldw.wrap = tx_ring_p->wr_index_wrap;
77944961713Sgirish 		kick.bits.ldw.tail = (uint16_t)tx_ring_p->wr_index;
78044961713Sgirish 
78144961713Sgirish 		/* Kick start the Transmit kick register */
78244961713Sgirish 		TXDMA_REG_WRITE64(NXGE_DEV_NPI_HANDLE(nxgep),
78344961713Sgirish 			TX_RING_KICK_REG,
78444961713Sgirish 			(uint8_t)tx_ring_p->tdc,
78544961713Sgirish 			kick.value);
78644961713Sgirish 	}
78744961713Sgirish 
78844961713Sgirish 	tdc_stats->tx_starts++;
78944961713Sgirish 
79044961713Sgirish 	MUTEX_EXIT(&tx_ring_p->lock);
79144961713Sgirish 
79244961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL, "<== nxge_start"));
79344961713Sgirish 
79444961713Sgirish 	return (status);
79544961713Sgirish 
79644961713Sgirish nxge_start_fail2:
79744961713Sgirish 	if (good_packet == B_FALSE) {
79844961713Sgirish 		cur_index = sop_index;
79944961713Sgirish 		NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_start: clean up"));
80044961713Sgirish 		for (i = 0; i < ngathers; i++) {
80144961713Sgirish 			tx_desc_p = &tx_desc_ring_vp[cur_index];
80244961713Sgirish 			npi_handle.regp = (uint64_t)tx_desc_p;
80344961713Sgirish 			tx_msg_p = &tx_msg_ring[cur_index];
80444961713Sgirish 			(void) npi_txdma_desc_set_zero(npi_handle, 1);
80544961713Sgirish 			if (tx_msg_p->flags.dma_type == USE_DVMA) {
80644961713Sgirish 				NXGE_DEBUG_MSG((nxgep, TX_CTL,
80744961713Sgirish 					"tx_desc_p = %X index = %d",
80844961713Sgirish 					tx_desc_p, tx_ring_p->rd_index));
80944961713Sgirish 				(void) dvma_unload(
81044961713Sgirish 						tx_msg_p->dvma_handle,
81144961713Sgirish 						0, -1);
81244961713Sgirish 				tx_msg_p->dvma_handle = NULL;
81344961713Sgirish 				if (tx_ring_p->dvma_wr_index ==
81444961713Sgirish 					tx_ring_p->dvma_wrap_mask)
81544961713Sgirish 					tx_ring_p->dvma_wr_index = 0;
81644961713Sgirish 				else
81744961713Sgirish 					tx_ring_p->dvma_wr_index++;
81844961713Sgirish 				tx_ring_p->dvma_pending--;
81944961713Sgirish 			} else if (tx_msg_p->flags.dma_type ==
82044961713Sgirish 					USE_DMA) {
82144961713Sgirish 				if (ddi_dma_unbind_handle(
82244961713Sgirish 					tx_msg_p->dma_handle))
82344961713Sgirish 					cmn_err(CE_WARN, "!nxge_start: "
82444961713Sgirish 						"ddi_dma_unbind_handle failed");
82544961713Sgirish 			}
82644961713Sgirish 			tx_msg_p->flags.dma_type = USE_NONE;
82744961713Sgirish 			cur_index = TXDMA_DESC_NEXT_INDEX(cur_index, 1,
82844961713Sgirish 				tx_ring_p->tx_wrap_mask);
82944961713Sgirish 
83044961713Sgirish 		}
83144961713Sgirish 
83244961713Sgirish 		nxgep->resched_needed = B_TRUE;
83344961713Sgirish 	}
83444961713Sgirish 
83544961713Sgirish 	MUTEX_EXIT(&tx_ring_p->lock);
83644961713Sgirish 
83744961713Sgirish nxge_start_fail1:
83844961713Sgirish 	/* Add FMA to check the access handle nxge_hregh */
83944961713Sgirish 
84044961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL, "<== nxge_start"));
84144961713Sgirish 
84244961713Sgirish 	return (status);
84344961713Sgirish }
84444961713Sgirish 
845*1f8914d5Sml int
846*1f8914d5Sml nxge_serial_tx(mblk_t *mp, void *arg)
847*1f8914d5Sml {
848*1f8914d5Sml 	p_tx_ring_t		tx_ring_p = (p_tx_ring_t)arg;
849*1f8914d5Sml 	p_nxge_t		nxgep = tx_ring_p->nxgep;
850*1f8914d5Sml 
851*1f8914d5Sml 	return (nxge_start(nxgep, tx_ring_p, mp));
852*1f8914d5Sml }
853*1f8914d5Sml 
85444961713Sgirish boolean_t
85544961713Sgirish nxge_send(p_nxge_t nxgep, mblk_t *mp, p_mac_tx_hint_t hp)
85644961713Sgirish {
85744961713Sgirish 	p_tx_ring_t 		*tx_rings;
85844961713Sgirish 	uint8_t			ring_index;
859*1f8914d5Sml 	p_tx_ring_t		tx_ring_p;
86044961713Sgirish 
86144961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_send"));
86244961713Sgirish 
86344961713Sgirish 	ASSERT(mp->b_next == NULL);
86444961713Sgirish 
86544961713Sgirish 	ring_index = nxge_tx_lb_ring_1(mp, nxgep->max_tdcs, hp);
86644961713Sgirish 	tx_rings = nxgep->tx_rings->rings;
86744961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_tx_msg: tx_rings $%p",
86844961713Sgirish 		tx_rings));
86944961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_tx_msg: max_tdcs %d "
87044961713Sgirish 		"ring_index %d", nxgep->max_tdcs, ring_index));
87144961713Sgirish 
872*1f8914d5Sml 	switch (nxge_tx_scheme) {
873*1f8914d5Sml 	case NXGE_USE_START:
874*1f8914d5Sml 		if (nxge_start(nxgep, tx_rings[ring_index], mp)) {
875*1f8914d5Sml 			NXGE_DEBUG_MSG((nxgep, TX_CTL, "<== nxge_send: failed "
876*1f8914d5Sml 				"ring index %d", ring_index));
877*1f8914d5Sml 			return (B_FALSE);
878*1f8914d5Sml 		}
879*1f8914d5Sml 		break;
880*1f8914d5Sml 
881*1f8914d5Sml 	case NXGE_USE_SERIAL:
882*1f8914d5Sml 	default:
883*1f8914d5Sml 		tx_ring_p = tx_rings[ring_index];
884*1f8914d5Sml 		nxge_serialize_enter(tx_ring_p->serial, mp);
885*1f8914d5Sml 		break;
88644961713Sgirish 	}
88744961713Sgirish 
88844961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL, "<== nxge_send: ring index %d",
88944961713Sgirish 		ring_index));
89044961713Sgirish 
89144961713Sgirish 	return (B_TRUE);
89244961713Sgirish }
89344961713Sgirish 
89444961713Sgirish /*
89544961713Sgirish  * nxge_m_tx() - send a chain of packets
89644961713Sgirish  */
89744961713Sgirish mblk_t *
89844961713Sgirish nxge_m_tx(void *arg, mblk_t *mp)
89944961713Sgirish {
90044961713Sgirish 	p_nxge_t 		nxgep = (p_nxge_t)arg;
90144961713Sgirish 	mblk_t 			*next;
90244961713Sgirish 	mac_tx_hint_t		hint;
90344961713Sgirish 
90444961713Sgirish 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
90544961713Sgirish 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
90644961713Sgirish 			"==> nxge_m_tx: hardware not initialized"));
90744961713Sgirish 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
90844961713Sgirish 			"<== nxge_m_tx"));
90944961713Sgirish 		return (mp);
91044961713Sgirish 	}
91144961713Sgirish 
91244961713Sgirish 	hint.hash =  NULL;
91344961713Sgirish 	hint.vid =  0;
91444961713Sgirish 	hint.sap =  0;
91544961713Sgirish 
91644961713Sgirish 	while (mp != NULL) {
91744961713Sgirish 		next = mp->b_next;
91844961713Sgirish 		mp->b_next = NULL;
91944961713Sgirish 
92044961713Sgirish 		/*
92144961713Sgirish 		 * Until Nemo tx resource works, the mac driver
92244961713Sgirish 		 * does the load balancing based on TCP port,
92344961713Sgirish 		 * or CPU. For debugging, we use a system
92444961713Sgirish 		 * configurable parameter.
92544961713Sgirish 		 */
92644961713Sgirish 		if (!nxge_send(nxgep, mp, &hint)) {
92744961713Sgirish 			mp->b_next = next;
92844961713Sgirish 			break;
92944961713Sgirish 		}
93044961713Sgirish 
93144961713Sgirish 		mp = next;
93244961713Sgirish 	}
93344961713Sgirish 
93444961713Sgirish 	return (mp);
93544961713Sgirish }
93644961713Sgirish 
93744961713Sgirish int
93844961713Sgirish nxge_tx_lb_ring_1(p_mblk_t mp, uint32_t maxtdcs, p_mac_tx_hint_t hp)
93944961713Sgirish {
94044961713Sgirish 	uint8_t 		ring_index = 0;
94144961713Sgirish 	uint8_t 		*tcp_port;
94244961713Sgirish 	p_mblk_t 		nmp;
94344961713Sgirish 	size_t 			mblk_len;
94444961713Sgirish 	size_t 			iph_len;
94544961713Sgirish 	size_t 			hdrs_size;
94644961713Sgirish 	uint8_t			hdrs_buf[sizeof (struct  ether_header) +
94744961713Sgirish 					IP_MAX_HDR_LENGTH + sizeof (uint32_t)];
94844961713Sgirish 				/*
94944961713Sgirish 				 * allocate space big enough to cover
95044961713Sgirish 				 * the max ip header length and the first
95144961713Sgirish 				 * 4 bytes of the TCP/IP header.
95244961713Sgirish 				 */
95344961713Sgirish 
95444961713Sgirish 	boolean_t		qos = B_FALSE;
95544961713Sgirish 
95644961713Sgirish 	NXGE_DEBUG_MSG((NULL, TX_CTL, "==> nxge_tx_lb_ring"));
95744961713Sgirish 
95844961713Sgirish 	if (hp->vid) {
95944961713Sgirish 		qos = B_TRUE;
96044961713Sgirish 	}
96144961713Sgirish 	switch (nxge_tx_lb_policy) {
96244961713Sgirish 	case NXGE_TX_LB_TCPUDP: /* default IPv4 TCP/UDP */
96344961713Sgirish 	default:
96444961713Sgirish 		tcp_port = mp->b_rptr;
96544961713Sgirish 		if (!nxge_no_tx_lb && !qos &&
96644961713Sgirish 			(ntohs(((p_ether_header_t)tcp_port)->ether_type)
96744961713Sgirish 				== ETHERTYPE_IP)) {
96844961713Sgirish 			nmp = mp;
96944961713Sgirish 			mblk_len = MBLKL(nmp);
97044961713Sgirish 			tcp_port = NULL;
97144961713Sgirish 			if (mblk_len > sizeof (struct ether_header) +
97244961713Sgirish 					sizeof (uint8_t)) {
97344961713Sgirish 				tcp_port = nmp->b_rptr +
97444961713Sgirish 					sizeof (struct ether_header);
97544961713Sgirish 				mblk_len -= sizeof (struct ether_header);
97644961713Sgirish 				iph_len = ((*tcp_port) & 0x0f) << 2;
97744961713Sgirish 				if (mblk_len > (iph_len + sizeof (uint32_t))) {
97844961713Sgirish 					tcp_port = nmp->b_rptr;
97944961713Sgirish 				} else {
98044961713Sgirish 					tcp_port = NULL;
98144961713Sgirish 				}
98244961713Sgirish 			}
98344961713Sgirish 			if (tcp_port == NULL) {
98444961713Sgirish 				hdrs_size = 0;
98544961713Sgirish 				((p_ether_header_t)hdrs_buf)->ether_type = 0;
98644961713Sgirish 				while ((nmp) && (hdrs_size <
98744961713Sgirish 						sizeof (hdrs_buf))) {
98844961713Sgirish 					mblk_len = MBLKL(nmp);
98944961713Sgirish 					if (mblk_len >=
99044961713Sgirish 						(sizeof (hdrs_buf) - hdrs_size))
99144961713Sgirish 						mblk_len = sizeof (hdrs_buf) -
99244961713Sgirish 							hdrs_size;
99344961713Sgirish 					bcopy(nmp->b_rptr,
99444961713Sgirish 						&hdrs_buf[hdrs_size], mblk_len);
99544961713Sgirish 					hdrs_size += mblk_len;
99644961713Sgirish 					nmp = nmp->b_cont;
99744961713Sgirish 				}
99844961713Sgirish 				tcp_port = hdrs_buf;
99944961713Sgirish 			}
100044961713Sgirish 			tcp_port += sizeof (ether_header_t);
100144961713Sgirish 			if (!(tcp_port[6] & 0x3f) && !(tcp_port[7] & 0xff)) {
100244961713Sgirish 				if ((tcp_port[9] == IPPROTO_TCP) ||
100344961713Sgirish 						(tcp_port[9] == IPPROTO_UDP)) {
100444961713Sgirish 					tcp_port += ((*tcp_port) & 0x0f) << 2;
100544961713Sgirish 					ring_index =
100644961713Sgirish 						((tcp_port[1] ^ tcp_port[3])
100744961713Sgirish 						% maxtdcs);
100844961713Sgirish 				} else {
100944961713Sgirish 					ring_index = tcp_port[19] % maxtdcs;
101044961713Sgirish 				}
101144961713Sgirish 			} else { /* fragmented packet */
101244961713Sgirish 				ring_index = tcp_port[19] % maxtdcs;
101344961713Sgirish 			}
101444961713Sgirish 		} else {
101544961713Sgirish 			ring_index = mp->b_band % maxtdcs;
101644961713Sgirish 		}
101744961713Sgirish 		break;
101844961713Sgirish 
101944961713Sgirish 	case NXGE_TX_LB_HASH:
102044961713Sgirish 		if (hp->hash) {
102144961713Sgirish 			ring_index = ((uint64_t)(hp->hash) % maxtdcs);
102244961713Sgirish 		} else {
102344961713Sgirish 			ring_index = mp->b_band % maxtdcs;
102444961713Sgirish 		}
102544961713Sgirish 		break;
102644961713Sgirish 
102744961713Sgirish 	case NXGE_TX_LB_DEST_MAC: /* Use destination MAC address */
102844961713Sgirish 		tcp_port = mp->b_rptr;
102944961713Sgirish 		ring_index = tcp_port[5] % maxtdcs;
103044961713Sgirish 		break;
103144961713Sgirish 	}
103244961713Sgirish 
103344961713Sgirish 	NXGE_DEBUG_MSG((NULL, TX_CTL, "<== nxge_tx_lb_ring"));
103444961713Sgirish 
103544961713Sgirish 	return (ring_index);
103644961713Sgirish }
103744961713Sgirish 
103844961713Sgirish uint_t
103944961713Sgirish nxge_reschedule(caddr_t arg)
104044961713Sgirish {
104144961713Sgirish 	p_nxge_t nxgep;
104244961713Sgirish 
104344961713Sgirish 	nxgep = (p_nxge_t)arg;
104444961713Sgirish 
104544961713Sgirish 	NXGE_DEBUG_MSG((nxgep, TX_CTL, "==> nxge_reschedule"));
104644961713Sgirish 
104744961713Sgirish 	if (nxgep->nxge_mac_state == NXGE_MAC_STARTED &&
104844961713Sgirish 			nxgep->resched_needed) {
104944961713Sgirish 		mac_tx_update(nxgep->mach);
105044961713Sgirish 		nxgep->resched_needed = B_FALSE;
105144961713Sgirish 		nxgep->resched_running = B_FALSE;
105244961713Sgirish 	}
105344961713Sgirish 
105444961713Sgirish 	NXGE_DEBUG_MSG((NULL, TX_CTL, "<== nxge_reschedule"));
105544961713Sgirish 	return (DDI_INTR_CLAIMED);
105644961713Sgirish }
1057