xref: /onnv-gate/usr/src/uts/common/io/nxge/nxge_rxdma.c (revision 6028:72bc433f666e)
13859Sml29623 /*
23859Sml29623  * CDDL HEADER START
33859Sml29623  *
43859Sml29623  * The contents of this file are subject to the terms of the
53859Sml29623  * Common Development and Distribution License (the "License").
63859Sml29623  * You may not use this file except in compliance with the License.
73859Sml29623  *
83859Sml29623  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93859Sml29623  * or http://www.opensolaris.org/os/licensing.
103859Sml29623  * See the License for the specific language governing permissions
113859Sml29623  * and limitations under the License.
123859Sml29623  *
133859Sml29623  * When distributing Covered Code, include this CDDL HEADER in each
143859Sml29623  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153859Sml29623  * If applicable, add the following below this CDDL HEADER, with the
163859Sml29623  * fields enclosed by brackets "[]" replaced with your own identifying
173859Sml29623  * information: Portions Copyright [yyyy] [name of copyright owner]
183859Sml29623  *
193859Sml29623  * CDDL HEADER END
203859Sml29623  */
213859Sml29623 /*
225759Smisaki  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
233859Sml29623  * Use is subject to license terms.
243859Sml29623  */
253859Sml29623 
263859Sml29623 #pragma ident	"%Z%%M%	%I%	%E% SMI"
273859Sml29623 
283859Sml29623 #include <sys/nxge/nxge_impl.h>
293859Sml29623 #include <sys/nxge/nxge_rxdma.h>
303859Sml29623 
313859Sml29623 #define	NXGE_ACTUAL_RDCGRP(nxgep, rdcgrp)	\
323859Sml29623 	(rdcgrp + nxgep->pt_config.hw_config.start_rdc_grpid)
333859Sml29623 #define	NXGE_ACTUAL_RDC(nxgep, rdc)	\
343859Sml29623 	(rdc + nxgep->pt_config.hw_config.start_rdc)
353859Sml29623 
363859Sml29623 /*
373859Sml29623  * Globals: tunable parameters (/etc/system or adb)
383859Sml29623  *
393859Sml29623  */
403859Sml29623 extern uint32_t nxge_rbr_size;
413859Sml29623 extern uint32_t nxge_rcr_size;
423859Sml29623 extern uint32_t	nxge_rbr_spare_size;
433859Sml29623 
443859Sml29623 extern uint32_t nxge_mblks_pending;
453859Sml29623 
463859Sml29623 /*
473859Sml29623  * Tunable to reduce the amount of time spent in the
483859Sml29623  * ISR doing Rx Processing.
493859Sml29623  */
503859Sml29623 extern uint32_t nxge_max_rx_pkts;
513859Sml29623 boolean_t nxge_jumbo_enable;
523859Sml29623 
533859Sml29623 /*
543859Sml29623  * Tunables to manage the receive buffer blocks.
553859Sml29623  *
563859Sml29623  * nxge_rx_threshold_hi: copy all buffers.
573859Sml29623  * nxge_rx_bcopy_size_type: receive buffer block size type.
583859Sml29623  * nxge_rx_threshold_lo: copy only up to tunable block size type.
593859Sml29623  */
603859Sml29623 extern nxge_rxbuf_threshold_t nxge_rx_threshold_hi;
613859Sml29623 extern nxge_rxbuf_type_t nxge_rx_buf_size_type;
623859Sml29623 extern nxge_rxbuf_threshold_t nxge_rx_threshold_lo;
633859Sml29623 
643859Sml29623 static nxge_status_t nxge_map_rxdma(p_nxge_t);
653859Sml29623 static void nxge_unmap_rxdma(p_nxge_t);
663859Sml29623 
673859Sml29623 static nxge_status_t nxge_rxdma_hw_start_common(p_nxge_t);
683859Sml29623 static void nxge_rxdma_hw_stop_common(p_nxge_t);
693859Sml29623 
703859Sml29623 static nxge_status_t nxge_rxdma_hw_start(p_nxge_t);
713859Sml29623 static void nxge_rxdma_hw_stop(p_nxge_t);
723859Sml29623 
733859Sml29623 static nxge_status_t nxge_map_rxdma_channel(p_nxge_t, uint16_t,
743859Sml29623     p_nxge_dma_common_t *,  p_rx_rbr_ring_t *,
753859Sml29623     uint32_t,
763859Sml29623     p_nxge_dma_common_t *, p_rx_rcr_ring_t *,
773859Sml29623     p_rx_mbox_t *);
783859Sml29623 static void nxge_unmap_rxdma_channel(p_nxge_t, uint16_t,
793859Sml29623     p_rx_rbr_ring_t, p_rx_rcr_ring_t, p_rx_mbox_t);
803859Sml29623 
813859Sml29623 static nxge_status_t nxge_map_rxdma_channel_cfg_ring(p_nxge_t,
823859Sml29623     uint16_t,
833859Sml29623     p_nxge_dma_common_t *, p_rx_rbr_ring_t *,
843859Sml29623     p_rx_rcr_ring_t *, p_rx_mbox_t *);
853859Sml29623 static void nxge_unmap_rxdma_channel_cfg_ring(p_nxge_t,
863859Sml29623     p_rx_rcr_ring_t, p_rx_mbox_t);
873859Sml29623 
883859Sml29623 static nxge_status_t nxge_map_rxdma_channel_buf_ring(p_nxge_t,
893859Sml29623     uint16_t,
903859Sml29623     p_nxge_dma_common_t *,
913859Sml29623     p_rx_rbr_ring_t *, uint32_t);
923859Sml29623 static void nxge_unmap_rxdma_channel_buf_ring(p_nxge_t,
933859Sml29623     p_rx_rbr_ring_t);
943859Sml29623 
953859Sml29623 static nxge_status_t nxge_rxdma_start_channel(p_nxge_t, uint16_t,
963859Sml29623     p_rx_rbr_ring_t, p_rx_rcr_ring_t, p_rx_mbox_t);
973859Sml29623 static nxge_status_t nxge_rxdma_stop_channel(p_nxge_t, uint16_t);
983859Sml29623 
993859Sml29623 mblk_t *
1003859Sml29623 nxge_rx_pkts(p_nxge_t, uint_t, p_nxge_ldv_t,
1013859Sml29623     p_rx_rcr_ring_t *, rx_dma_ctl_stat_t);
1023859Sml29623 
1033859Sml29623 static void nxge_receive_packet(p_nxge_t,
1043859Sml29623 	p_rx_rcr_ring_t,
1053859Sml29623 	p_rcr_entry_t,
1063859Sml29623 	boolean_t *,
1073859Sml29623 	mblk_t **, mblk_t **);
1083859Sml29623 
1093859Sml29623 nxge_status_t nxge_disable_rxdma_channel(p_nxge_t, uint16_t);
1103859Sml29623 
1113859Sml29623 static p_rx_msg_t nxge_allocb(size_t, uint32_t, p_nxge_dma_common_t);
1123859Sml29623 static void nxge_freeb(p_rx_msg_t);
1133859Sml29623 static void nxge_rx_pkts_vring(p_nxge_t, uint_t,
1143859Sml29623     p_nxge_ldv_t, rx_dma_ctl_stat_t);
1153859Sml29623 static nxge_status_t nxge_rx_err_evnts(p_nxge_t, uint_t,
1163859Sml29623 				p_nxge_ldv_t, rx_dma_ctl_stat_t);
1173859Sml29623 
1183859Sml29623 static nxge_status_t nxge_rxdma_handle_port_errors(p_nxge_t,
1193859Sml29623 				uint32_t, uint32_t);
1203859Sml29623 
1213859Sml29623 static nxge_status_t nxge_rxbuf_index_info_init(p_nxge_t,
1223859Sml29623     p_rx_rbr_ring_t);
1233859Sml29623 
1243859Sml29623 
1253859Sml29623 static nxge_status_t
1263859Sml29623 nxge_rxdma_fatal_err_recover(p_nxge_t, uint16_t);
1273859Sml29623 
1283859Sml29623 nxge_status_t
1293859Sml29623 nxge_rx_port_fatal_err_recover(p_nxge_t);
1303859Sml29623 
1313859Sml29623 nxge_status_t
1323859Sml29623 nxge_init_rxdma_channels(p_nxge_t nxgep)
1333859Sml29623 {
1343859Sml29623 	nxge_status_t	status = NXGE_OK;
1353859Sml29623 
1363859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_init_rxdma_channels"));
1373859Sml29623 
1383859Sml29623 	status = nxge_map_rxdma(nxgep);
1393859Sml29623 	if (status != NXGE_OK) {
1403859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1413859Sml29623 			"<== nxge_init_rxdma: status 0x%x", status));
1423859Sml29623 		return (status);
1433859Sml29623 	}
1443859Sml29623 
1453859Sml29623 	status = nxge_rxdma_hw_start_common(nxgep);
1463859Sml29623 	if (status != NXGE_OK) {
1473859Sml29623 		nxge_unmap_rxdma(nxgep);
1483859Sml29623 	}
1493859Sml29623 
1503859Sml29623 	status = nxge_rxdma_hw_start(nxgep);
1513859Sml29623 	if (status != NXGE_OK) {
1523859Sml29623 		nxge_unmap_rxdma(nxgep);
1533859Sml29623 	}
1543859Sml29623 
1553859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
1563859Sml29623 		"<== nxge_init_rxdma_channels: status 0x%x", status));
1573859Sml29623 
1583859Sml29623 	return (status);
1593859Sml29623 }
1603859Sml29623 
1613859Sml29623 void
1623859Sml29623 nxge_uninit_rxdma_channels(p_nxge_t nxgep)
1633859Sml29623 {
1643859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_uninit_rxdma_channels"));
1653859Sml29623 
1663859Sml29623 	nxge_rxdma_hw_stop(nxgep);
1673859Sml29623 	nxge_rxdma_hw_stop_common(nxgep);
1683859Sml29623 	nxge_unmap_rxdma(nxgep);
1693859Sml29623 
1703859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
1713859Sml29623 		"<== nxge_uinit_rxdma_channels"));
1723859Sml29623 }
1733859Sml29623 
1743859Sml29623 nxge_status_t
1753859Sml29623 nxge_reset_rxdma_channel(p_nxge_t nxgep, uint16_t channel)
1763859Sml29623 {
1773859Sml29623 	npi_handle_t		handle;
1783859Sml29623 	npi_status_t		rs = NPI_SUCCESS;
1793859Sml29623 	nxge_status_t		status = NXGE_OK;
1803859Sml29623 
1813859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_reset_rxdma_channel"));
1823859Sml29623 
1833859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
1843859Sml29623 	rs = npi_rxdma_cfg_rdc_reset(handle, channel);
1853859Sml29623 
1863859Sml29623 	if (rs != NPI_SUCCESS) {
1873859Sml29623 		status = NXGE_ERROR | rs;
1883859Sml29623 	}
1893859Sml29623 
1903859Sml29623 	return (status);
1913859Sml29623 }
1923859Sml29623 
1933859Sml29623 void
1943859Sml29623 nxge_rxdma_regs_dump_channels(p_nxge_t nxgep)
1953859Sml29623 {
1963859Sml29623 	int			i, ndmas;
1973859Sml29623 	uint16_t		channel;
1983859Sml29623 	p_rx_rbr_rings_t 	rx_rbr_rings;
1993859Sml29623 	p_rx_rbr_ring_t		*rbr_rings;
2003859Sml29623 	npi_handle_t		handle;
2013859Sml29623 
2023859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "==> nxge_rxdma_regs_dump_channels"));
2033859Sml29623 
2043859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
2053859Sml29623 	(void) npi_rxdma_dump_fzc_regs(handle);
2063859Sml29623 
2073859Sml29623 	rx_rbr_rings = nxgep->rx_rbr_rings;
2083859Sml29623 	if (rx_rbr_rings == NULL) {
2093859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
2103859Sml29623 			"<== nxge_rxdma_regs_dump_channels: "
2113859Sml29623 			"NULL ring pointer"));
2123859Sml29623 		return;
2133859Sml29623 	}
2143859Sml29623 	if (rx_rbr_rings->rbr_rings == NULL) {
2153859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
2163859Sml29623 			"<== nxge_rxdma_regs_dump_channels: "
2173859Sml29623 			" NULL rbr rings pointer"));
2183859Sml29623 		return;
2193859Sml29623 	}
2203859Sml29623 
2213859Sml29623 	ndmas = rx_rbr_rings->ndmas;
2223859Sml29623 	if (!ndmas) {
2233859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
2243859Sml29623 			"<== nxge_rxdma_regs_dump_channels: no channel"));
2253859Sml29623 		return;
2263859Sml29623 	}
2273859Sml29623 
2283859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
2293859Sml29623 		"==> nxge_rxdma_regs_dump_channels (ndmas %d)", ndmas));
2303859Sml29623 
2313859Sml29623 	rbr_rings = rx_rbr_rings->rbr_rings;
2323859Sml29623 	for (i = 0; i < ndmas; i++) {
2333859Sml29623 		if (rbr_rings == NULL || rbr_rings[i] == NULL) {
2343859Sml29623 			continue;
2353859Sml29623 		}
2363859Sml29623 		channel = rbr_rings[i]->rdc;
2373859Sml29623 		(void) nxge_dump_rxdma_channel(nxgep, channel);
2383859Sml29623 	}
2393859Sml29623 
2403859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "<== nxge_rxdma_regs_dump"));
2413859Sml29623 
2423859Sml29623 }
2433859Sml29623 
2443859Sml29623 nxge_status_t
2453859Sml29623 nxge_dump_rxdma_channel(p_nxge_t nxgep, uint8_t channel)
2463859Sml29623 {
2473859Sml29623 	npi_handle_t		handle;
2483859Sml29623 	npi_status_t		rs = NPI_SUCCESS;
2493859Sml29623 	nxge_status_t		status = NXGE_OK;
2503859Sml29623 
2513859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_dump_rxdma_channel"));
2523859Sml29623 
2533859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
2543859Sml29623 	rs = npi_rxdma_dump_rdc_regs(handle, channel);
2553859Sml29623 
2563859Sml29623 	if (rs != NPI_SUCCESS) {
2573859Sml29623 		status = NXGE_ERROR | rs;
2583859Sml29623 	}
2593859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_dump_rxdma_channel"));
2603859Sml29623 	return (status);
2613859Sml29623 }
2623859Sml29623 
2633859Sml29623 nxge_status_t
2643859Sml29623 nxge_init_rxdma_channel_event_mask(p_nxge_t nxgep, uint16_t channel,
2653859Sml29623     p_rx_dma_ent_msk_t mask_p)
2663859Sml29623 {
2673859Sml29623 	npi_handle_t		handle;
2683859Sml29623 	npi_status_t		rs = NPI_SUCCESS;
2693859Sml29623 	nxge_status_t		status = NXGE_OK;
2703859Sml29623 
2713859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
2723859Sml29623 		"<== nxge_init_rxdma_channel_event_mask"));
2733859Sml29623 
2743859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
2753859Sml29623 	rs = npi_rxdma_event_mask(handle, OP_SET, channel, mask_p);
2763859Sml29623 	if (rs != NPI_SUCCESS) {
2773859Sml29623 		status = NXGE_ERROR | rs;
2783859Sml29623 	}
2793859Sml29623 
2803859Sml29623 	return (status);
2813859Sml29623 }
2823859Sml29623 
2833859Sml29623 nxge_status_t
2843859Sml29623 nxge_init_rxdma_channel_cntl_stat(p_nxge_t nxgep, uint16_t channel,
2853859Sml29623     p_rx_dma_ctl_stat_t cs_p)
2863859Sml29623 {
2873859Sml29623 	npi_handle_t		handle;
2883859Sml29623 	npi_status_t		rs = NPI_SUCCESS;
2893859Sml29623 	nxge_status_t		status = NXGE_OK;
2903859Sml29623 
2913859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
2923859Sml29623 		"<== nxge_init_rxdma_channel_cntl_stat"));
2933859Sml29623 
2943859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
2953859Sml29623 	rs = npi_rxdma_control_status(handle, OP_SET, channel, cs_p);
2963859Sml29623 
2973859Sml29623 	if (rs != NPI_SUCCESS) {
2983859Sml29623 		status = NXGE_ERROR | rs;
2993859Sml29623 	}
3003859Sml29623 
3013859Sml29623 	return (status);
3023859Sml29623 }
3033859Sml29623 
3043859Sml29623 nxge_status_t
3053859Sml29623 nxge_rxdma_cfg_rdcgrp_default_rdc(p_nxge_t nxgep, uint8_t rdcgrp,
3063859Sml29623 				    uint8_t rdc)
3073859Sml29623 {
3083859Sml29623 	npi_handle_t		handle;
3093859Sml29623 	npi_status_t		rs = NPI_SUCCESS;
3103859Sml29623 	p_nxge_dma_pt_cfg_t	p_dma_cfgp;
3113859Sml29623 	p_nxge_rdc_grp_t	rdc_grp_p;
3123859Sml29623 	uint8_t actual_rdcgrp, actual_rdc;
3133859Sml29623 
3143859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
3153859Sml29623 			    " ==> nxge_rxdma_cfg_rdcgrp_default_rdc"));
3163859Sml29623 	p_dma_cfgp = (p_nxge_dma_pt_cfg_t)&nxgep->pt_config;
3173859Sml29623 
3183859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
3193859Sml29623 
3203859Sml29623 	rdc_grp_p = &p_dma_cfgp->rdc_grps[rdcgrp];
3213859Sml29623 	rdc_grp_p->rdc[0] = rdc;
3223859Sml29623 
3233859Sml29623 	actual_rdcgrp = NXGE_ACTUAL_RDCGRP(nxgep, rdcgrp);
3243859Sml29623 	actual_rdc = NXGE_ACTUAL_RDC(nxgep, rdc);
3253859Sml29623 
3263859Sml29623 	rs = npi_rxdma_cfg_rdc_table_default_rdc(handle, actual_rdcgrp,
3273859Sml29623 							    actual_rdc);
3283859Sml29623 
3293859Sml29623 	if (rs != NPI_SUCCESS) {
3303859Sml29623 		return (NXGE_ERROR | rs);
3313859Sml29623 	}
3323859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
3333859Sml29623 			    " <== nxge_rxdma_cfg_rdcgrp_default_rdc"));
3343859Sml29623 	return (NXGE_OK);
3353859Sml29623 }
3363859Sml29623 
3373859Sml29623 nxge_status_t
3383859Sml29623 nxge_rxdma_cfg_port_default_rdc(p_nxge_t nxgep, uint8_t port, uint8_t rdc)
3393859Sml29623 {
3403859Sml29623 	npi_handle_t		handle;
3413859Sml29623 
3423859Sml29623 	uint8_t actual_rdc;
3433859Sml29623 	npi_status_t		rs = NPI_SUCCESS;
3443859Sml29623 
3453859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
3463859Sml29623 			    " ==> nxge_rxdma_cfg_port_default_rdc"));
3473859Sml29623 
3483859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
3493859Sml29623 	actual_rdc = NXGE_ACTUAL_RDC(nxgep, rdc);
3503859Sml29623 	rs = npi_rxdma_cfg_default_port_rdc(handle, port, actual_rdc);
3513859Sml29623 
3523859Sml29623 
3533859Sml29623 	if (rs != NPI_SUCCESS) {
3543859Sml29623 		return (NXGE_ERROR | rs);
3553859Sml29623 	}
3563859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
3573859Sml29623 			    " <== nxge_rxdma_cfg_port_default_rdc"));
3583859Sml29623 
3593859Sml29623 	return (NXGE_OK);
3603859Sml29623 }
3613859Sml29623 
3623859Sml29623 nxge_status_t
3633859Sml29623 nxge_rxdma_cfg_rcr_threshold(p_nxge_t nxgep, uint8_t channel,
3643859Sml29623 				    uint16_t pkts)
3653859Sml29623 {
3663859Sml29623 	npi_status_t	rs = NPI_SUCCESS;
3673859Sml29623 	npi_handle_t	handle;
3683859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
3693859Sml29623 			    " ==> nxge_rxdma_cfg_rcr_threshold"));
3703859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
3713859Sml29623 
3723859Sml29623 	rs = npi_rxdma_cfg_rdc_rcr_threshold(handle, channel, pkts);
3733859Sml29623 
3743859Sml29623 	if (rs != NPI_SUCCESS) {
3753859Sml29623 		return (NXGE_ERROR | rs);
3763859Sml29623 	}
3773859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL, " <== nxge_rxdma_cfg_rcr_threshold"));
3783859Sml29623 	return (NXGE_OK);
3793859Sml29623 }
3803859Sml29623 
3813859Sml29623 nxge_status_t
3823859Sml29623 nxge_rxdma_cfg_rcr_timeout(p_nxge_t nxgep, uint8_t channel,
3833859Sml29623 			    uint16_t tout, uint8_t enable)
3843859Sml29623 {
3853859Sml29623 	npi_status_t	rs = NPI_SUCCESS;
3863859Sml29623 	npi_handle_t	handle;
3873859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL, " ==> nxge_rxdma_cfg_rcr_timeout"));
3883859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
3893859Sml29623 	if (enable == 0) {
3903859Sml29623 		rs = npi_rxdma_cfg_rdc_rcr_timeout_disable(handle, channel);
3913859Sml29623 	} else {
3923859Sml29623 		rs = npi_rxdma_cfg_rdc_rcr_timeout(handle, channel,
3933859Sml29623 							    tout);
3943859Sml29623 	}
3953859Sml29623 
3963859Sml29623 	if (rs != NPI_SUCCESS) {
3973859Sml29623 		return (NXGE_ERROR | rs);
3983859Sml29623 	}
3993859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL, " <== nxge_rxdma_cfg_rcr_timeout"));
4003859Sml29623 	return (NXGE_OK);
4013859Sml29623 }
4023859Sml29623 
4033859Sml29623 nxge_status_t
4043859Sml29623 nxge_enable_rxdma_channel(p_nxge_t nxgep, uint16_t channel,
4053859Sml29623     p_rx_rbr_ring_t rbr_p, p_rx_rcr_ring_t rcr_p, p_rx_mbox_t mbox_p)
4063859Sml29623 {
4073859Sml29623 	npi_handle_t		handle;
4083859Sml29623 	rdc_desc_cfg_t 		rdc_desc;
4093859Sml29623 	p_rcrcfig_b_t		cfgb_p;
4103859Sml29623 	npi_status_t		rs = NPI_SUCCESS;
4113859Sml29623 
4123859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_enable_rxdma_channel"));
4133859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
4143859Sml29623 	/*
4153859Sml29623 	 * Use configuration data composed at init time.
4163859Sml29623 	 * Write to hardware the receive ring configurations.
4173859Sml29623 	 */
4183859Sml29623 	rdc_desc.mbox_enable = 1;
4193859Sml29623 	rdc_desc.mbox_addr = mbox_p->mbox_addr;
4203859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
4213859Sml29623 		"==> nxge_enable_rxdma_channel: mboxp $%p($%p)",
4223859Sml29623 		mbox_p->mbox_addr, rdc_desc.mbox_addr));
4233859Sml29623 
4243859Sml29623 	rdc_desc.rbr_len = rbr_p->rbb_max;
4253859Sml29623 	rdc_desc.rbr_addr = rbr_p->rbr_addr;
4263859Sml29623 
4273859Sml29623 	switch (nxgep->rx_bksize_code) {
4283859Sml29623 	case RBR_BKSIZE_4K:
4293859Sml29623 		rdc_desc.page_size = SIZE_4KB;
4303859Sml29623 		break;
4313859Sml29623 	case RBR_BKSIZE_8K:
4323859Sml29623 		rdc_desc.page_size = SIZE_8KB;
4333859Sml29623 		break;
4343859Sml29623 	case RBR_BKSIZE_16K:
4353859Sml29623 		rdc_desc.page_size = SIZE_16KB;
4363859Sml29623 		break;
4373859Sml29623 	case RBR_BKSIZE_32K:
4383859Sml29623 		rdc_desc.page_size = SIZE_32KB;
4393859Sml29623 		break;
4403859Sml29623 	}
4413859Sml29623 
4423859Sml29623 	rdc_desc.size0 = rbr_p->npi_pkt_buf_size0;
4433859Sml29623 	rdc_desc.valid0 = 1;
4443859Sml29623 
4453859Sml29623 	rdc_desc.size1 = rbr_p->npi_pkt_buf_size1;
4463859Sml29623 	rdc_desc.valid1 = 1;
4473859Sml29623 
4483859Sml29623 	rdc_desc.size2 = rbr_p->npi_pkt_buf_size2;
4493859Sml29623 	rdc_desc.valid2 = 1;
4503859Sml29623 
4513859Sml29623 	rdc_desc.full_hdr = rcr_p->full_hdr_flag;
4523859Sml29623 	rdc_desc.offset = rcr_p->sw_priv_hdr_len;
4533859Sml29623 
4543859Sml29623 	rdc_desc.rcr_len = rcr_p->comp_size;
4553859Sml29623 	rdc_desc.rcr_addr = rcr_p->rcr_addr;
4563859Sml29623 
4573859Sml29623 	cfgb_p = &(rcr_p->rcr_cfgb);
4583859Sml29623 	rdc_desc.rcr_threshold = cfgb_p->bits.ldw.pthres;
4593859Sml29623 	rdc_desc.rcr_timeout = cfgb_p->bits.ldw.timeout;
4603859Sml29623 	rdc_desc.rcr_timeout_enable = cfgb_p->bits.ldw.entout;
4613859Sml29623 
4623859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_enable_rxdma_channel: "
4633859Sml29623 		"rbr_len qlen %d pagesize code %d rcr_len %d",
4643859Sml29623 		rdc_desc.rbr_len, rdc_desc.page_size, rdc_desc.rcr_len));
4653859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_enable_rxdma_channel: "
4663859Sml29623 		"size 0 %d size 1 %d size 2 %d",
4673859Sml29623 		rbr_p->npi_pkt_buf_size0, rbr_p->npi_pkt_buf_size1,
4683859Sml29623 		rbr_p->npi_pkt_buf_size2));
4693859Sml29623 
4703859Sml29623 	rs = npi_rxdma_cfg_rdc_ring(handle, rbr_p->rdc, &rdc_desc);
4713859Sml29623 	if (rs != NPI_SUCCESS) {
4723859Sml29623 		return (NXGE_ERROR | rs);
4733859Sml29623 	}
4743859Sml29623 
4753859Sml29623 	/*
4763859Sml29623 	 * Enable the timeout and threshold.
4773859Sml29623 	 */
4783859Sml29623 	rs = npi_rxdma_cfg_rdc_rcr_threshold(handle, channel,
4793859Sml29623 			rdc_desc.rcr_threshold);
4803859Sml29623 	if (rs != NPI_SUCCESS) {
4813859Sml29623 		return (NXGE_ERROR | rs);
4823859Sml29623 	}
4833859Sml29623 
4843859Sml29623 	rs = npi_rxdma_cfg_rdc_rcr_timeout(handle, channel,
4853859Sml29623 			rdc_desc.rcr_timeout);
4863859Sml29623 	if (rs != NPI_SUCCESS) {
4873859Sml29623 		return (NXGE_ERROR | rs);
4883859Sml29623 	}
4893859Sml29623 
4903859Sml29623 	/* Enable the DMA */
4913859Sml29623 	rs = npi_rxdma_cfg_rdc_enable(handle, channel);
4923859Sml29623 	if (rs != NPI_SUCCESS) {
4933859Sml29623 		return (NXGE_ERROR | rs);
4943859Sml29623 	}
4953859Sml29623 
4963859Sml29623 	/* Kick the DMA engine. */
4973859Sml29623 	npi_rxdma_rdc_rbr_kick(handle, channel, rbr_p->rbb_max);
4983859Sml29623 	/* Clear the rbr empty bit */
4993859Sml29623 	(void) npi_rxdma_channel_rbr_empty_clear(handle, channel);
5003859Sml29623 
5013859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_enable_rxdma_channel"));
5023859Sml29623 
5033859Sml29623 	return (NXGE_OK);
5043859Sml29623 }
5053859Sml29623 
5063859Sml29623 nxge_status_t
5073859Sml29623 nxge_disable_rxdma_channel(p_nxge_t nxgep, uint16_t channel)
5083859Sml29623 {
5093859Sml29623 	npi_handle_t		handle;
5103859Sml29623 	npi_status_t		rs = NPI_SUCCESS;
5113859Sml29623 
5123859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_disable_rxdma_channel"));
5133859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
5143859Sml29623 
5153859Sml29623 	/* disable the DMA */
5163859Sml29623 	rs = npi_rxdma_cfg_rdc_disable(handle, channel);
5173859Sml29623 	if (rs != NPI_SUCCESS) {
5183859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
5193859Sml29623 			"<== nxge_disable_rxdma_channel:failed (0x%x)",
5203859Sml29623 			rs));
5213859Sml29623 		return (NXGE_ERROR | rs);
5223859Sml29623 	}
5233859Sml29623 
5243859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_disable_rxdma_channel"));
5253859Sml29623 	return (NXGE_OK);
5263859Sml29623 }
5273859Sml29623 
5283859Sml29623 nxge_status_t
5293859Sml29623 nxge_rxdma_channel_rcrflush(p_nxge_t nxgep, uint8_t channel)
5303859Sml29623 {
5313859Sml29623 	npi_handle_t		handle;
5323859Sml29623 	nxge_status_t		status = NXGE_OK;
5333859Sml29623 
5343859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
5353859Sml29623 		"<== nxge_init_rxdma_channel_rcrflush"));
5363859Sml29623 
5373859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
5383859Sml29623 	npi_rxdma_rdc_rcr_flush(handle, channel);
5393859Sml29623 
5403859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
5413859Sml29623 		"<== nxge_init_rxdma_channel_rcrflsh"));
5423859Sml29623 	return (status);
5433859Sml29623 
5443859Sml29623 }
5453859Sml29623 
5463859Sml29623 #define	MID_INDEX(l, r) ((r + l + 1) >> 1)
5473859Sml29623 
5483859Sml29623 #define	TO_LEFT -1
5493859Sml29623 #define	TO_RIGHT 1
5503859Sml29623 #define	BOTH_RIGHT (TO_RIGHT + TO_RIGHT)
5513859Sml29623 #define	BOTH_LEFT (TO_LEFT + TO_LEFT)
5523859Sml29623 #define	IN_MIDDLE (TO_RIGHT + TO_LEFT)
5533859Sml29623 #define	NO_HINT 0xffffffff
5543859Sml29623 
5553859Sml29623 /*ARGSUSED*/
5563859Sml29623 nxge_status_t
5573859Sml29623 nxge_rxbuf_pp_to_vp(p_nxge_t nxgep, p_rx_rbr_ring_t rbr_p,
5583859Sml29623 	uint8_t pktbufsz_type, uint64_t *pkt_buf_addr_pp,
5593859Sml29623 	uint64_t **pkt_buf_addr_p, uint32_t *bufoffset, uint32_t *msg_index)
5603859Sml29623 {
5613859Sml29623 	int			bufsize;
5623859Sml29623 	uint64_t		pktbuf_pp;
5633859Sml29623 	uint64_t 		dvma_addr;
5643859Sml29623 	rxring_info_t 		*ring_info;
5653859Sml29623 	int 			base_side, end_side;
5663859Sml29623 	int 			r_index, l_index, anchor_index;
5673859Sml29623 	int 			found, search_done;
5683859Sml29623 	uint32_t offset, chunk_size, block_size, page_size_mask;
5693859Sml29623 	uint32_t chunk_index, block_index, total_index;
5703859Sml29623 	int 			max_iterations, iteration;
5713859Sml29623 	rxbuf_index_info_t 	*bufinfo;
5723859Sml29623 
5733859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL, "==> nxge_rxbuf_pp_to_vp"));
5743859Sml29623 
5753859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
5763859Sml29623 		"==> nxge_rxbuf_pp_to_vp: buf_pp $%p btype %d",
5773859Sml29623 		pkt_buf_addr_pp,
5783859Sml29623 		pktbufsz_type));
5795125Sjoycey #if defined(__i386)
5805125Sjoycey 	pktbuf_pp = (uint64_t)(uint32_t)pkt_buf_addr_pp;
5815125Sjoycey #else
5823859Sml29623 	pktbuf_pp = (uint64_t)pkt_buf_addr_pp;
5835125Sjoycey #endif
5843859Sml29623 
5853859Sml29623 	switch (pktbufsz_type) {
5863859Sml29623 	case 0:
5873859Sml29623 		bufsize = rbr_p->pkt_buf_size0;
5883859Sml29623 		break;
5893859Sml29623 	case 1:
5903859Sml29623 		bufsize = rbr_p->pkt_buf_size1;
5913859Sml29623 		break;
5923859Sml29623 	case 2:
5933859Sml29623 		bufsize = rbr_p->pkt_buf_size2;
5943859Sml29623 		break;
5953859Sml29623 	case RCR_SINGLE_BLOCK:
5963859Sml29623 		bufsize = 0;
5973859Sml29623 		anchor_index = 0;
5983859Sml29623 		break;
5993859Sml29623 	default:
6003859Sml29623 		return (NXGE_ERROR);
6013859Sml29623 	}
6023859Sml29623 
6033859Sml29623 	if (rbr_p->num_blocks == 1) {
6043859Sml29623 		anchor_index = 0;
6053859Sml29623 		ring_info = rbr_p->ring_info;
6063859Sml29623 		bufinfo = (rxbuf_index_info_t *)ring_info->buffer;
6073859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX2_CTL,
6083859Sml29623 			"==> nxge_rxbuf_pp_to_vp: (found, 1 block) "
6093859Sml29623 			"buf_pp $%p btype %d anchor_index %d "
6103859Sml29623 			"bufinfo $%p",
6113859Sml29623 			pkt_buf_addr_pp,
6123859Sml29623 			pktbufsz_type,
6133859Sml29623 			anchor_index,
6143859Sml29623 			bufinfo));
6153859Sml29623 
6163859Sml29623 		goto found_index;
6173859Sml29623 	}
6183859Sml29623 
6193859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
6203859Sml29623 		"==> nxge_rxbuf_pp_to_vp: "
6213859Sml29623 		"buf_pp $%p btype %d  anchor_index %d",
6223859Sml29623 		pkt_buf_addr_pp,
6233859Sml29623 		pktbufsz_type,
6243859Sml29623 		anchor_index));
6253859Sml29623 
6263859Sml29623 	ring_info = rbr_p->ring_info;
6273859Sml29623 	found = B_FALSE;
6283859Sml29623 	bufinfo = (rxbuf_index_info_t *)ring_info->buffer;
6293859Sml29623 	iteration = 0;
6303859Sml29623 	max_iterations = ring_info->max_iterations;
6313859Sml29623 		/*
6323859Sml29623 		 * First check if this block has been seen
6333859Sml29623 		 * recently. This is indicated by a hint which
6343859Sml29623 		 * is initialized when the first buffer of the block
6353859Sml29623 		 * is seen. The hint is reset when the last buffer of
6363859Sml29623 		 * the block has been processed.
6373859Sml29623 		 * As three block sizes are supported, three hints
6383859Sml29623 		 * are kept. The idea behind the hints is that once
6393859Sml29623 		 * the hardware  uses a block for a buffer  of that
6403859Sml29623 		 * size, it will use it exclusively for that size
6413859Sml29623 		 * and will use it until it is exhausted. It is assumed
6423859Sml29623 		 * that there would a single block being used for the same
6433859Sml29623 		 * buffer sizes at any given time.
6443859Sml29623 		 */
6453859Sml29623 	if (ring_info->hint[pktbufsz_type] != NO_HINT) {
6463859Sml29623 		anchor_index = ring_info->hint[pktbufsz_type];
6473859Sml29623 		dvma_addr =  bufinfo[anchor_index].dvma_addr;
6483859Sml29623 		chunk_size = bufinfo[anchor_index].buf_size;
6493859Sml29623 		if ((pktbuf_pp >= dvma_addr) &&
6503859Sml29623 			(pktbuf_pp < (dvma_addr + chunk_size))) {
6513859Sml29623 			found = B_TRUE;
6523859Sml29623 				/*
6533859Sml29623 				 * check if this is the last buffer in the block
6543859Sml29623 				 * If so, then reset the hint for the size;
6553859Sml29623 				 */
6563859Sml29623 
6573859Sml29623 			if ((pktbuf_pp + bufsize) >= (dvma_addr + chunk_size))
6583859Sml29623 				ring_info->hint[pktbufsz_type] = NO_HINT;
6593859Sml29623 		}
6603859Sml29623 	}
6613859Sml29623 
6623859Sml29623 	if (found == B_FALSE) {
6633859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX2_CTL,
6643859Sml29623 			"==> nxge_rxbuf_pp_to_vp: (!found)"
6653859Sml29623 			"buf_pp $%p btype %d anchor_index %d",
6663859Sml29623 			pkt_buf_addr_pp,
6673859Sml29623 			pktbufsz_type,
6683859Sml29623 			anchor_index));
6693859Sml29623 
6703859Sml29623 			/*
6713859Sml29623 			 * This is the first buffer of the block of this
6723859Sml29623 			 * size. Need to search the whole information
6733859Sml29623 			 * array.
6743859Sml29623 			 * the search algorithm uses a binary tree search
6753859Sml29623 			 * algorithm. It assumes that the information is
6763859Sml29623 			 * already sorted with increasing order
6773859Sml29623 			 * info[0] < info[1] < info[2]  .... < info[n-1]
6783859Sml29623 			 * where n is the size of the information array
6793859Sml29623 			 */
6803859Sml29623 		r_index = rbr_p->num_blocks - 1;
6813859Sml29623 		l_index = 0;
6823859Sml29623 		search_done = B_FALSE;
6833859Sml29623 		anchor_index = MID_INDEX(r_index, l_index);
6843859Sml29623 		while (search_done == B_FALSE) {
6853859Sml29623 			if ((r_index == l_index) ||
6863859Sml29623 				(iteration >= max_iterations))
6873859Sml29623 				search_done = B_TRUE;
6883859Sml29623 			end_side = TO_RIGHT; /* to the right */
6893859Sml29623 			base_side = TO_LEFT; /* to the left */
6903859Sml29623 			/* read the DVMA address information and sort it */
6913859Sml29623 			dvma_addr =  bufinfo[anchor_index].dvma_addr;
6923859Sml29623 			chunk_size = bufinfo[anchor_index].buf_size;
6933859Sml29623 			NXGE_DEBUG_MSG((nxgep, RX2_CTL,
6943859Sml29623 				"==> nxge_rxbuf_pp_to_vp: (searching)"
6953859Sml29623 				"buf_pp $%p btype %d "
6963859Sml29623 				"anchor_index %d chunk_size %d dvmaaddr $%p",
6973859Sml29623 				pkt_buf_addr_pp,
6983859Sml29623 				pktbufsz_type,
6993859Sml29623 				anchor_index,
7003859Sml29623 				chunk_size,
7013859Sml29623 				dvma_addr));
7023859Sml29623 
7033859Sml29623 			if (pktbuf_pp >= dvma_addr)
7043859Sml29623 				base_side = TO_RIGHT; /* to the right */
7053859Sml29623 			if (pktbuf_pp < (dvma_addr + chunk_size))
7063859Sml29623 				end_side = TO_LEFT; /* to the left */
7073859Sml29623 
7083859Sml29623 			switch (base_side + end_side) {
7093859Sml29623 				case IN_MIDDLE:
7103859Sml29623 					/* found */
7113859Sml29623 					found = B_TRUE;
7123859Sml29623 					search_done = B_TRUE;
7133859Sml29623 					if ((pktbuf_pp + bufsize) <
7143859Sml29623 						(dvma_addr + chunk_size))
7153859Sml29623 						ring_info->hint[pktbufsz_type] =
7163859Sml29623 						bufinfo[anchor_index].buf_index;
7173859Sml29623 					break;
7183859Sml29623 				case BOTH_RIGHT:
7193859Sml29623 						/* not found: go to the right */
7203859Sml29623 					l_index = anchor_index + 1;
7213859Sml29623 					anchor_index =
7223859Sml29623 						MID_INDEX(r_index, l_index);
7233859Sml29623 					break;
7243859Sml29623 
7253859Sml29623 				case  BOTH_LEFT:
7263859Sml29623 						/* not found: go to the left */
7273859Sml29623 					r_index = anchor_index - 1;
7283859Sml29623 					anchor_index = MID_INDEX(r_index,
7293859Sml29623 						l_index);
7303859Sml29623 					break;
7313859Sml29623 				default: /* should not come here */
7323859Sml29623 					return (NXGE_ERROR);
7333859Sml29623 			}
7343859Sml29623 			iteration++;
7353859Sml29623 		}
7363859Sml29623 
7373859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX2_CTL,
7383859Sml29623 			"==> nxge_rxbuf_pp_to_vp: (search done)"
7393859Sml29623 			"buf_pp $%p btype %d anchor_index %d",
7403859Sml29623 			pkt_buf_addr_pp,
7413859Sml29623 			pktbufsz_type,
7423859Sml29623 			anchor_index));
7433859Sml29623 	}
7443859Sml29623 
7453859Sml29623 	if (found == B_FALSE) {
7463859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX2_CTL,
7473859Sml29623 			"==> nxge_rxbuf_pp_to_vp: (search failed)"
7483859Sml29623 			"buf_pp $%p btype %d anchor_index %d",
7493859Sml29623 			pkt_buf_addr_pp,
7503859Sml29623 			pktbufsz_type,
7513859Sml29623 			anchor_index));
7523859Sml29623 		return (NXGE_ERROR);
7533859Sml29623 	}
7543859Sml29623 
7553859Sml29623 found_index:
7563859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
7573859Sml29623 		"==> nxge_rxbuf_pp_to_vp: (FOUND1)"
7583859Sml29623 		"buf_pp $%p btype %d bufsize %d anchor_index %d",
7593859Sml29623 		pkt_buf_addr_pp,
7603859Sml29623 		pktbufsz_type,
7613859Sml29623 		bufsize,
7623859Sml29623 		anchor_index));
7633859Sml29623 
7643859Sml29623 	/* index of the first block in this chunk */
7653859Sml29623 	chunk_index = bufinfo[anchor_index].start_index;
7663859Sml29623 	dvma_addr =  bufinfo[anchor_index].dvma_addr;
7673859Sml29623 	page_size_mask = ring_info->block_size_mask;
7683859Sml29623 
7693859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
7703859Sml29623 		"==> nxge_rxbuf_pp_to_vp: (FOUND3), get chunk)"
7713859Sml29623 		"buf_pp $%p btype %d bufsize %d "
7723859Sml29623 		"anchor_index %d chunk_index %d dvma $%p",
7733859Sml29623 		pkt_buf_addr_pp,
7743859Sml29623 		pktbufsz_type,
7753859Sml29623 		bufsize,
7763859Sml29623 		anchor_index,
7773859Sml29623 		chunk_index,
7783859Sml29623 		dvma_addr));
7793859Sml29623 
7803859Sml29623 	offset = pktbuf_pp - dvma_addr; /* offset within the chunk */
7813859Sml29623 	block_size = rbr_p->block_size; /* System  block(page) size */
7823859Sml29623 
7833859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
7843859Sml29623 		"==> nxge_rxbuf_pp_to_vp: (FOUND4), get chunk)"
7853859Sml29623 		"buf_pp $%p btype %d bufsize %d "
7863859Sml29623 		"anchor_index %d chunk_index %d dvma $%p "
7873859Sml29623 		"offset %d block_size %d",
7883859Sml29623 		pkt_buf_addr_pp,
7893859Sml29623 		pktbufsz_type,
7903859Sml29623 		bufsize,
7913859Sml29623 		anchor_index,
7923859Sml29623 		chunk_index,
7933859Sml29623 		dvma_addr,
7943859Sml29623 		offset,
7953859Sml29623 		block_size));
7963859Sml29623 
7973859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL, "==> getting total index"));
7983859Sml29623 
7993859Sml29623 	block_index = (offset / block_size); /* index within chunk */
8003859Sml29623 	total_index = chunk_index + block_index;
8013859Sml29623 
8023859Sml29623 
8033859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
8043859Sml29623 		"==> nxge_rxbuf_pp_to_vp: "
8053859Sml29623 		"total_index %d dvma_addr $%p "
8063859Sml29623 		"offset %d block_size %d "
8073859Sml29623 		"block_index %d ",
8083859Sml29623 		total_index, dvma_addr,
8093859Sml29623 		offset, block_size,
8103859Sml29623 		block_index));
8115125Sjoycey #if defined(__i386)
8125125Sjoycey 	*pkt_buf_addr_p = (uint64_t *)((uint32_t)bufinfo[anchor_index].kaddr +
8135125Sjoycey 		(uint32_t)offset);
8145125Sjoycey #else
8155125Sjoycey 	*pkt_buf_addr_p = (uint64_t *)((uint64_t)bufinfo[anchor_index].kaddr +
8165125Sjoycey 		(uint64_t)offset);
8175125Sjoycey #endif
8183859Sml29623 
8193859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
8203859Sml29623 		"==> nxge_rxbuf_pp_to_vp: "
8213859Sml29623 		"total_index %d dvma_addr $%p "
8223859Sml29623 		"offset %d block_size %d "
8233859Sml29623 		"block_index %d "
8243859Sml29623 		"*pkt_buf_addr_p $%p",
8253859Sml29623 		total_index, dvma_addr,
8263859Sml29623 		offset, block_size,
8273859Sml29623 		block_index,
8283859Sml29623 		*pkt_buf_addr_p));
8293859Sml29623 
8303859Sml29623 
8313859Sml29623 	*msg_index = total_index;
8323859Sml29623 	*bufoffset =  (offset & page_size_mask);
8333859Sml29623 
8343859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
8353859Sml29623 		"==> nxge_rxbuf_pp_to_vp: get msg index: "
8363859Sml29623 		"msg_index %d bufoffset_index %d",
8373859Sml29623 		*msg_index,
8383859Sml29623 		*bufoffset));
8393859Sml29623 
8403859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL, "<== nxge_rxbuf_pp_to_vp"));
8413859Sml29623 
8423859Sml29623 	return (NXGE_OK);
8433859Sml29623 }
8443859Sml29623 
8453859Sml29623 /*
8463859Sml29623  * used by quick sort (qsort) function
8473859Sml29623  * to perform comparison
8483859Sml29623  */
8493859Sml29623 static int
8503859Sml29623 nxge_sort_compare(const void *p1, const void *p2)
8513859Sml29623 {
8523859Sml29623 
8533859Sml29623 	rxbuf_index_info_t *a, *b;
8543859Sml29623 
8553859Sml29623 	a = (rxbuf_index_info_t *)p1;
8563859Sml29623 	b = (rxbuf_index_info_t *)p2;
8573859Sml29623 
8583859Sml29623 	if (a->dvma_addr > b->dvma_addr)
8593859Sml29623 		return (1);
8603859Sml29623 	if (a->dvma_addr < b->dvma_addr)
8613859Sml29623 		return (-1);
8623859Sml29623 	return (0);
8633859Sml29623 }
8643859Sml29623 
8653859Sml29623 
8663859Sml29623 
8673859Sml29623 /*
8683859Sml29623  * grabbed this sort implementation from common/syscall/avl.c
8693859Sml29623  *
8703859Sml29623  */
8713859Sml29623 /*
8723859Sml29623  * Generic shellsort, from K&R (1st ed, p 58.), somewhat modified.
8733859Sml29623  * v = Ptr to array/vector of objs
8743859Sml29623  * n = # objs in the array
8753859Sml29623  * s = size of each obj (must be multiples of a word size)
8763859Sml29623  * f = ptr to function to compare two objs
8773859Sml29623  *	returns (-1 = less than, 0 = equal, 1 = greater than
8783859Sml29623  */
8793859Sml29623 void
8803859Sml29623 nxge_ksort(caddr_t v, int n, int s, int (*f)())
8813859Sml29623 {
8823859Sml29623 	int g, i, j, ii;
8833859Sml29623 	unsigned int *p1, *p2;
8843859Sml29623 	unsigned int tmp;
8853859Sml29623 
8863859Sml29623 	/* No work to do */
8873859Sml29623 	if (v == NULL || n <= 1)
8883859Sml29623 		return;
8893859Sml29623 	/* Sanity check on arguments */
8903859Sml29623 	ASSERT(((uintptr_t)v & 0x3) == 0 && (s & 0x3) == 0);
8913859Sml29623 	ASSERT(s > 0);
8923859Sml29623 
8933859Sml29623 	for (g = n / 2; g > 0; g /= 2) {
8943859Sml29623 		for (i = g; i < n; i++) {
8953859Sml29623 			for (j = i - g; j >= 0 &&
8963859Sml29623 				(*f)(v + j * s, v + (j + g) * s) == 1;
8973859Sml29623 					j -= g) {
8983859Sml29623 				p1 = (unsigned *)(v + j * s);
8993859Sml29623 				p2 = (unsigned *)(v + (j + g) * s);
9003859Sml29623 				for (ii = 0; ii < s / 4; ii++) {
9013859Sml29623 					tmp = *p1;
9023859Sml29623 					*p1++ = *p2;
9033859Sml29623 					*p2++ = tmp;
9043859Sml29623 				}
9053859Sml29623 			}
9063859Sml29623 		}
9073859Sml29623 	}
9083859Sml29623 }
9093859Sml29623 
9103859Sml29623 /*
9113859Sml29623  * Initialize data structures required for rxdma
9123859Sml29623  * buffer dvma->vmem address lookup
9133859Sml29623  */
9143859Sml29623 /*ARGSUSED*/
9153859Sml29623 static nxge_status_t
9163859Sml29623 nxge_rxbuf_index_info_init(p_nxge_t nxgep, p_rx_rbr_ring_t rbrp)
9173859Sml29623 {
9183859Sml29623 
9193859Sml29623 	int index;
9203859Sml29623 	rxring_info_t *ring_info;
9213859Sml29623 	int max_iteration = 0, max_index = 0;
9223859Sml29623 
9233859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_rxbuf_index_info_init"));
9243859Sml29623 
9253859Sml29623 	ring_info = rbrp->ring_info;
9263859Sml29623 	ring_info->hint[0] = NO_HINT;
9273859Sml29623 	ring_info->hint[1] = NO_HINT;
9283859Sml29623 	ring_info->hint[2] = NO_HINT;
9293859Sml29623 	max_index = rbrp->num_blocks;
9303859Sml29623 
9313859Sml29623 		/* read the DVMA address information and sort it */
9323859Sml29623 		/* do init of the information array */
9333859Sml29623 
9343859Sml29623 
9353859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA2_CTL,
9363859Sml29623 		" nxge_rxbuf_index_info_init Sort ptrs"));
9373859Sml29623 
9383859Sml29623 		/* sort the array */
9393859Sml29623 	nxge_ksort((void *)ring_info->buffer, max_index,
9403859Sml29623 		sizeof (rxbuf_index_info_t), nxge_sort_compare);
9413859Sml29623 
9423859Sml29623 
9433859Sml29623 
9443859Sml29623 	for (index = 0; index < max_index; index++) {
9453859Sml29623 		NXGE_DEBUG_MSG((nxgep, DMA2_CTL,
9463859Sml29623 			" nxge_rxbuf_index_info_init: sorted chunk %d "
9473859Sml29623 			" ioaddr $%p kaddr $%p size %x",
9483859Sml29623 			index, ring_info->buffer[index].dvma_addr,
9493859Sml29623 			ring_info->buffer[index].kaddr,
9503859Sml29623 			ring_info->buffer[index].buf_size));
9513859Sml29623 	}
9523859Sml29623 
9533859Sml29623 	max_iteration = 0;
9543859Sml29623 	while (max_index >= (1ULL << max_iteration))
9553859Sml29623 		max_iteration++;
9563859Sml29623 	ring_info->max_iterations = max_iteration + 1;
9573859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA2_CTL,
9583859Sml29623 		" nxge_rxbuf_index_info_init Find max iter %d",
9593859Sml29623 					ring_info->max_iterations));
9603859Sml29623 
9613859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_rxbuf_index_info_init"));
9623859Sml29623 	return (NXGE_OK);
9633859Sml29623 }
9643859Sml29623 
9653859Sml29623 /* ARGSUSED */
9663859Sml29623 void
9673859Sml29623 nxge_dump_rcr_entry(p_nxge_t nxgep, p_rcr_entry_t entry_p)
9683859Sml29623 {
9693859Sml29623 #ifdef	NXGE_DEBUG
9703859Sml29623 
9713859Sml29623 	uint32_t bptr;
9723859Sml29623 	uint64_t pp;
9733859Sml29623 
9743859Sml29623 	bptr = entry_p->bits.hdw.pkt_buf_addr;
9753859Sml29623 
9763859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
9773859Sml29623 		"\trcr entry $%p "
9783859Sml29623 		"\trcr entry 0x%0llx "
9793859Sml29623 		"\trcr entry 0x%08x "
9803859Sml29623 		"\trcr entry 0x%08x "
9813859Sml29623 		"\tvalue 0x%0llx\n"
9823859Sml29623 		"\tmulti = %d\n"
9833859Sml29623 		"\tpkt_type = 0x%x\n"
9843859Sml29623 		"\tzero_copy = %d\n"
9853859Sml29623 		"\tnoport = %d\n"
9863859Sml29623 		"\tpromis = %d\n"
9873859Sml29623 		"\terror = 0x%04x\n"
9883859Sml29623 		"\tdcf_err = 0x%01x\n"
9893859Sml29623 		"\tl2_len = %d\n"
9903859Sml29623 		"\tpktbufsize = %d\n"
9913859Sml29623 		"\tpkt_buf_addr = $%p\n"
9923859Sml29623 		"\tpkt_buf_addr (<< 6) = $%p\n",
9933859Sml29623 		entry_p,
9943859Sml29623 		*(int64_t *)entry_p,
9953859Sml29623 		*(int32_t *)entry_p,
9963859Sml29623 		*(int32_t *)((char *)entry_p + 32),
9973859Sml29623 		entry_p->value,
9983859Sml29623 		entry_p->bits.hdw.multi,
9993859Sml29623 		entry_p->bits.hdw.pkt_type,
10003859Sml29623 		entry_p->bits.hdw.zero_copy,
10013859Sml29623 		entry_p->bits.hdw.noport,
10023859Sml29623 		entry_p->bits.hdw.promis,
10033859Sml29623 		entry_p->bits.hdw.error,
10043859Sml29623 		entry_p->bits.hdw.dcf_err,
10053859Sml29623 		entry_p->bits.hdw.l2_len,
10063859Sml29623 		entry_p->bits.hdw.pktbufsz,
10073859Sml29623 		bptr,
10083859Sml29623 		entry_p->bits.ldw.pkt_buf_addr));
10093859Sml29623 
10103859Sml29623 	pp = (entry_p->value & RCR_PKT_BUF_ADDR_MASK) <<
10113859Sml29623 		RCR_PKT_BUF_ADDR_SHIFT;
10123859Sml29623 
10133859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "rcr pp 0x%llx l2 len %d",
10143859Sml29623 		pp, (*(int64_t *)entry_p >> 40) & 0x3fff));
10153859Sml29623 #endif
10163859Sml29623 }
10173859Sml29623 
10183859Sml29623 void
10193859Sml29623 nxge_rxdma_regs_dump(p_nxge_t nxgep, int rdc)
10203859Sml29623 {
10213859Sml29623 	npi_handle_t		handle;
10223859Sml29623 	rbr_stat_t 		rbr_stat;
10233859Sml29623 	addr44_t 		hd_addr;
10243859Sml29623 	addr44_t 		tail_addr;
10253859Sml29623 	uint16_t 		qlen;
10263859Sml29623 
10273859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
10283859Sml29623 		"==> nxge_rxdma_regs_dump: rdc channel %d", rdc));
10293859Sml29623 
10303859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
10313859Sml29623 
10323859Sml29623 	/* RBR head */
10333859Sml29623 	hd_addr.addr = 0;
10343859Sml29623 	(void) npi_rxdma_rdc_rbr_head_get(handle, rdc, &hd_addr);
10355165Syc148097 #if defined(__i386)
10363859Sml29623 	printf("nxge_rxdma_regs_dump: got hdptr $%p \n",
10375125Sjoycey 		(void *)(uint32_t)hd_addr.addr);
10385125Sjoycey #else
10395165Syc148097 	printf("nxge_rxdma_regs_dump: got hdptr $%p \n",
10403859Sml29623 		(void *)hd_addr.addr);
10415125Sjoycey #endif
10423859Sml29623 
10433859Sml29623 	/* RBR stats */
10443859Sml29623 	(void) npi_rxdma_rdc_rbr_stat_get(handle, rdc, &rbr_stat);
10453859Sml29623 	printf("nxge_rxdma_regs_dump: rbr len %d \n", rbr_stat.bits.ldw.qlen);
10463859Sml29623 
10473859Sml29623 	/* RCR tail */
10483859Sml29623 	tail_addr.addr = 0;
10493859Sml29623 	(void) npi_rxdma_rdc_rcr_tail_get(handle, rdc, &tail_addr);
10505165Syc148097 #if defined(__i386)
10513859Sml29623 	printf("nxge_rxdma_regs_dump: got tail ptr $%p \n",
10525125Sjoycey 		(void *)(uint32_t)tail_addr.addr);
10535125Sjoycey #else
10545165Syc148097 	printf("nxge_rxdma_regs_dump: got tail ptr $%p \n",
10553859Sml29623 		(void *)tail_addr.addr);
10565125Sjoycey #endif
10573859Sml29623 
10583859Sml29623 	/* RCR qlen */
10593859Sml29623 	(void) npi_rxdma_rdc_rcr_qlen_get(handle, rdc, &qlen);
10603859Sml29623 	printf("nxge_rxdma_regs_dump: rcr len %x \n", qlen);
10613859Sml29623 
10623859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
10633859Sml29623 		"<== nxge_rxdma_regs_dump: rdc rdc %d", rdc));
10643859Sml29623 }
10653859Sml29623 
10663859Sml29623 void
10673859Sml29623 nxge_rxdma_stop(p_nxge_t nxgep)
10683859Sml29623 {
10693859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "==> nxge_rxdma_stop"));
10703859Sml29623 
10713859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_STOP);
10723859Sml29623 	(void) nxge_rx_mac_disable(nxgep);
10733859Sml29623 	(void) nxge_rxdma_hw_mode(nxgep, NXGE_DMA_STOP);
10743859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "<== nxge_rxdma_stop"));
10753859Sml29623 }
10763859Sml29623 
10773859Sml29623 void
10783859Sml29623 nxge_rxdma_stop_reinit(p_nxge_t nxgep)
10793859Sml29623 {
10803859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "==> nxge_rxdma_stop_reinit"));
10813859Sml29623 
10823859Sml29623 	(void) nxge_rxdma_stop(nxgep);
10833859Sml29623 	(void) nxge_uninit_rxdma_channels(nxgep);
10843859Sml29623 	(void) nxge_init_rxdma_channels(nxgep);
10853859Sml29623 
10863859Sml29623 #ifndef	AXIS_DEBUG_LB
10873859Sml29623 	(void) nxge_xcvr_init(nxgep);
10883859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_START);
10893859Sml29623 #endif
10903859Sml29623 	(void) nxge_rx_mac_enable(nxgep);
10913859Sml29623 
10923859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "<== nxge_rxdma_stop_reinit"));
10933859Sml29623 }
10943859Sml29623 
10953859Sml29623 nxge_status_t
10963859Sml29623 nxge_rxdma_hw_mode(p_nxge_t nxgep, boolean_t enable)
10973859Sml29623 {
10983859Sml29623 	int			i, ndmas;
10993859Sml29623 	uint16_t		channel;
11003859Sml29623 	p_rx_rbr_rings_t 	rx_rbr_rings;
11013859Sml29623 	p_rx_rbr_ring_t		*rbr_rings;
11023859Sml29623 	npi_handle_t		handle;
11033859Sml29623 	npi_status_t		rs = NPI_SUCCESS;
11043859Sml29623 	nxge_status_t		status = NXGE_OK;
11053859Sml29623 
11063859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
11073859Sml29623 		"==> nxge_rxdma_hw_mode: mode %d", enable));
11083859Sml29623 
11093859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
11103859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
11113859Sml29623 			"<== nxge_rxdma_mode: not initialized"));
11123859Sml29623 		return (NXGE_ERROR);
11133859Sml29623 	}
11143859Sml29623 
11153859Sml29623 	rx_rbr_rings = nxgep->rx_rbr_rings;
11163859Sml29623 	if (rx_rbr_rings == NULL) {
11173859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
11183859Sml29623 			"<== nxge_rxdma_mode: NULL ring pointer"));
11193859Sml29623 		return (NXGE_ERROR);
11203859Sml29623 	}
11213859Sml29623 	if (rx_rbr_rings->rbr_rings == NULL) {
11223859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
11233859Sml29623 			"<== nxge_rxdma_mode: NULL rbr rings pointer"));
11243859Sml29623 		return (NXGE_ERROR);
11253859Sml29623 	}
11263859Sml29623 
11273859Sml29623 	ndmas = rx_rbr_rings->ndmas;
11283859Sml29623 	if (!ndmas) {
11293859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
11303859Sml29623 			"<== nxge_rxdma_mode: no channel"));
11313859Sml29623 		return (NXGE_ERROR);
11323859Sml29623 	}
11333859Sml29623 
11343859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
11353859Sml29623 		"==> nxge_rxdma_mode (ndmas %d)", ndmas));
11363859Sml29623 
11373859Sml29623 	rbr_rings = rx_rbr_rings->rbr_rings;
11383859Sml29623 
11393859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
11403859Sml29623 	for (i = 0; i < ndmas; i++) {
11413859Sml29623 		if (rbr_rings == NULL || rbr_rings[i] == NULL) {
11423859Sml29623 			continue;
11433859Sml29623 		}
11443859Sml29623 		channel = rbr_rings[i]->rdc;
11453859Sml29623 		if (enable) {
11463859Sml29623 			NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
11473859Sml29623 				"==> nxge_rxdma_hw_mode: channel %d (enable)",
11483859Sml29623 				channel));
11493859Sml29623 			rs = npi_rxdma_cfg_rdc_enable(handle, channel);
11503859Sml29623 		} else {
11513859Sml29623 			NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
11523859Sml29623 				"==> nxge_rxdma_hw_mode: channel %d (disable)",
11533859Sml29623 				channel));
11543859Sml29623 			rs = npi_rxdma_cfg_rdc_disable(handle, channel);
11553859Sml29623 		}
11563859Sml29623 	}
11573859Sml29623 
11583859Sml29623 	status = ((rs == NPI_SUCCESS) ? NXGE_OK : NXGE_ERROR | rs);
11593859Sml29623 
11603859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
11613859Sml29623 		"<== nxge_rxdma_hw_mode: status 0x%x", status));
11623859Sml29623 
11633859Sml29623 	return (status);
11643859Sml29623 }
11653859Sml29623 
11663859Sml29623 void
11673859Sml29623 nxge_rxdma_enable_channel(p_nxge_t nxgep, uint16_t channel)
11683859Sml29623 {
11693859Sml29623 	npi_handle_t		handle;
11703859Sml29623 
11713859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
11723859Sml29623 		"==> nxge_rxdma_enable_channel: channel %d", channel));
11733859Sml29623 
11743859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
11753859Sml29623 	(void) npi_rxdma_cfg_rdc_enable(handle, channel);
11763859Sml29623 
11773859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_rxdma_enable_channel"));
11783859Sml29623 }
11793859Sml29623 
11803859Sml29623 void
11813859Sml29623 nxge_rxdma_disable_channel(p_nxge_t nxgep, uint16_t channel)
11823859Sml29623 {
11833859Sml29623 	npi_handle_t		handle;
11843859Sml29623 
11853859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
11863859Sml29623 		"==> nxge_rxdma_disable_channel: channel %d", channel));
11873859Sml29623 
11883859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
11893859Sml29623 	(void) npi_rxdma_cfg_rdc_disable(handle, channel);
11903859Sml29623 
11913859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_rxdma_disable_channel"));
11923859Sml29623 }
11933859Sml29623 
11943859Sml29623 void
11953859Sml29623 nxge_hw_start_rx(p_nxge_t nxgep)
11963859Sml29623 {
11973859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_hw_start_rx"));
11983859Sml29623 
11993859Sml29623 	(void) nxge_rxdma_hw_mode(nxgep, NXGE_DMA_START);
12003859Sml29623 	(void) nxge_rx_mac_enable(nxgep);
12013859Sml29623 
12023859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_hw_start_rx"));
12033859Sml29623 }
12043859Sml29623 
12053859Sml29623 /*ARGSUSED*/
12063859Sml29623 void
12073859Sml29623 nxge_fixup_rxdma_rings(p_nxge_t nxgep)
12083859Sml29623 {
12093859Sml29623 	int			i, ndmas;
12103859Sml29623 	uint16_t		rdc;
12113859Sml29623 	p_rx_rbr_rings_t 	rx_rbr_rings;
12123859Sml29623 	p_rx_rbr_ring_t		*rbr_rings;
12133859Sml29623 	p_rx_rcr_rings_t 	rx_rcr_rings;
12143859Sml29623 
12153859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "==> nxge_fixup_rxdma_rings"));
12163859Sml29623 
12173859Sml29623 	rx_rbr_rings = nxgep->rx_rbr_rings;
12183859Sml29623 	if (rx_rbr_rings == NULL) {
12193859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
12203859Sml29623 			"<== nxge_fixup_rxdma_rings: NULL ring pointer"));
12213859Sml29623 		return;
12223859Sml29623 	}
12233859Sml29623 	ndmas = rx_rbr_rings->ndmas;
12243859Sml29623 	if (!ndmas) {
12253859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
12263859Sml29623 			"<== nxge_fixup_rxdma_rings: no channel"));
12273859Sml29623 		return;
12283859Sml29623 	}
12293859Sml29623 
12303859Sml29623 	rx_rcr_rings = nxgep->rx_rcr_rings;
12313859Sml29623 	if (rx_rcr_rings == NULL) {
12323859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
12333859Sml29623 			"<== nxge_fixup_rxdma_rings: NULL ring pointer"));
12343859Sml29623 		return;
12353859Sml29623 	}
12363859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
12373859Sml29623 		"==> nxge_fixup_rxdma_rings (ndmas %d)", ndmas));
12383859Sml29623 
12393859Sml29623 	nxge_rxdma_hw_stop(nxgep);
12403859Sml29623 
12413859Sml29623 	rbr_rings = rx_rbr_rings->rbr_rings;
12423859Sml29623 	for (i = 0; i < ndmas; i++) {
12433859Sml29623 		rdc = rbr_rings[i]->rdc;
12443859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
12453859Sml29623 			"==> nxge_fixup_rxdma_rings: channel %d "
12463859Sml29623 			"ring $%px", rdc, rbr_rings[i]));
12473859Sml29623 		(void) nxge_rxdma_fixup_channel(nxgep, rdc, i);
12483859Sml29623 	}
12493859Sml29623 
12503859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "<== nxge_fixup_rxdma_rings"));
12513859Sml29623 }
12523859Sml29623 
12533859Sml29623 void
12543859Sml29623 nxge_rxdma_fix_channel(p_nxge_t nxgep, uint16_t channel)
12553859Sml29623 {
12563859Sml29623 	int		i;
12573859Sml29623 
12583859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "==> nxge_rxdma_fix_channel"));
12593859Sml29623 	i = nxge_rxdma_get_ring_index(nxgep, channel);
12603859Sml29623 	if (i < 0) {
12613859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
12623859Sml29623 			"<== nxge_rxdma_fix_channel: no entry found"));
12633859Sml29623 		return;
12643859Sml29623 	}
12653859Sml29623 
12663859Sml29623 	nxge_rxdma_fixup_channel(nxgep, channel, i);
12673859Sml29623 
12683859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "<== nxge_txdma_fix_channel"));
12693859Sml29623 }
12703859Sml29623 
12713859Sml29623 void
12723859Sml29623 nxge_rxdma_fixup_channel(p_nxge_t nxgep, uint16_t channel, int entry)
12733859Sml29623 {
12743859Sml29623 	int			ndmas;
12753859Sml29623 	p_rx_rbr_rings_t 	rx_rbr_rings;
12763859Sml29623 	p_rx_rbr_ring_t		*rbr_rings;
12773859Sml29623 	p_rx_rcr_rings_t 	rx_rcr_rings;
12783859Sml29623 	p_rx_rcr_ring_t		*rcr_rings;
12793859Sml29623 	p_rx_mbox_areas_t 	rx_mbox_areas_p;
12803859Sml29623 	p_rx_mbox_t		*rx_mbox_p;
12813859Sml29623 	p_nxge_dma_pool_t	dma_buf_poolp;
12823859Sml29623 	p_nxge_dma_pool_t	dma_cntl_poolp;
12833859Sml29623 	p_rx_rbr_ring_t 	rbrp;
12843859Sml29623 	p_rx_rcr_ring_t 	rcrp;
12853859Sml29623 	p_rx_mbox_t 		mboxp;
12863859Sml29623 	p_nxge_dma_common_t 	dmap;
12873859Sml29623 	nxge_status_t		status = NXGE_OK;
12883859Sml29623 
12893859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "==> nxge_rxdma_fixup_channel"));
12903859Sml29623 
12913859Sml29623 	(void) nxge_rxdma_stop_channel(nxgep, channel);
12923859Sml29623 
12933859Sml29623 	dma_buf_poolp = nxgep->rx_buf_pool_p;
12943859Sml29623 	dma_cntl_poolp = nxgep->rx_cntl_pool_p;
12953859Sml29623 
12963859Sml29623 	if (!dma_buf_poolp->buf_allocated || !dma_cntl_poolp->buf_allocated) {
12973859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
12983859Sml29623 			"<== nxge_rxdma_fixup_channel: buf not allocated"));
12993859Sml29623 		return;
13003859Sml29623 	}
13013859Sml29623 
13023859Sml29623 	ndmas = dma_buf_poolp->ndmas;
13033859Sml29623 	if (!ndmas) {
13043859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
13053859Sml29623 			"<== nxge_rxdma_fixup_channel: no dma allocated"));
13063859Sml29623 		return;
13073859Sml29623 	}
13083859Sml29623 
13093859Sml29623 	rx_rbr_rings = nxgep->rx_rbr_rings;
13103859Sml29623 	rx_rcr_rings = nxgep->rx_rcr_rings;
13113859Sml29623 	rbr_rings = rx_rbr_rings->rbr_rings;
13123859Sml29623 	rcr_rings = rx_rcr_rings->rcr_rings;
13133859Sml29623 	rx_mbox_areas_p = nxgep->rx_mbox_areas_p;
13143859Sml29623 	rx_mbox_p = rx_mbox_areas_p->rxmbox_areas;
13153859Sml29623 
13163859Sml29623 	/* Reinitialize the receive block and completion rings */
13173859Sml29623 	rbrp = (p_rx_rbr_ring_t)rbr_rings[entry],
13183859Sml29623 	rcrp = (p_rx_rcr_ring_t)rcr_rings[entry],
13193859Sml29623 	mboxp = (p_rx_mbox_t)rx_mbox_p[entry];
13203859Sml29623 
13213859Sml29623 
13223859Sml29623 	rbrp->rbr_wr_index = (rbrp->rbb_max - 1);
13233859Sml29623 	rbrp->rbr_rd_index = 0;
13243859Sml29623 	rcrp->comp_rd_index = 0;
13253859Sml29623 	rcrp->comp_wt_index = 0;
13263859Sml29623 
13273859Sml29623 	dmap = (p_nxge_dma_common_t)&rcrp->rcr_desc;
13283859Sml29623 	bzero((caddr_t)dmap->kaddrp, dmap->alength);
13293859Sml29623 
13303859Sml29623 	status = nxge_rxdma_start_channel(nxgep, channel,
13313859Sml29623 			rbrp, rcrp, mboxp);
13323859Sml29623 	if (status != NXGE_OK) {
13333859Sml29623 		goto nxge_rxdma_fixup_channel_fail;
13343859Sml29623 	}
13353859Sml29623 	if (status != NXGE_OK) {
13363859Sml29623 		goto nxge_rxdma_fixup_channel_fail;
13373859Sml29623 	}
13383859Sml29623 
13393859Sml29623 nxge_rxdma_fixup_channel_fail:
13403859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
13413859Sml29623 		"==> nxge_rxdma_fixup_channel: failed (0x%08x)", status));
13423859Sml29623 
13433859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "<== nxge_rxdma_fixup_channel"));
13443859Sml29623 }
13453859Sml29623 
13463859Sml29623 int
13473859Sml29623 nxge_rxdma_get_ring_index(p_nxge_t nxgep, uint16_t channel)
13483859Sml29623 {
13493859Sml29623 	int			i, ndmas;
13503859Sml29623 	uint16_t		rdc;
13513859Sml29623 	p_rx_rbr_rings_t 	rx_rbr_rings;
13523859Sml29623 	p_rx_rbr_ring_t		*rbr_rings;
13533859Sml29623 
13543859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
13553859Sml29623 		"==> nxge_rxdma_get_ring_index: channel %d", channel));
13563859Sml29623 
13573859Sml29623 	rx_rbr_rings = nxgep->rx_rbr_rings;
13583859Sml29623 	if (rx_rbr_rings == NULL) {
13593859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
13603859Sml29623 			"<== nxge_rxdma_get_ring_index: NULL ring pointer"));
13613859Sml29623 		return (-1);
13623859Sml29623 	}
13633859Sml29623 	ndmas = rx_rbr_rings->ndmas;
13643859Sml29623 	if (!ndmas) {
13653859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
13663859Sml29623 			"<== nxge_rxdma_get_ring_index: no channel"));
13673859Sml29623 		return (-1);
13683859Sml29623 	}
13693859Sml29623 
13703859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
13713859Sml29623 		"==> nxge_rxdma_get_ring_index (ndmas %d)", ndmas));
13723859Sml29623 
13733859Sml29623 	rbr_rings = rx_rbr_rings->rbr_rings;
13743859Sml29623 	for (i = 0; i < ndmas; i++) {
13753859Sml29623 		rdc = rbr_rings[i]->rdc;
13763859Sml29623 		if (channel == rdc) {
13773859Sml29623 			NXGE_DEBUG_MSG((nxgep, RX_CTL,
13783859Sml29623 				"==> nxge_rxdma_get_rbr_ring: "
13793859Sml29623 				"channel %d (index %d) "
13803859Sml29623 				"ring %d", channel, i,
13813859Sml29623 				rbr_rings[i]));
13823859Sml29623 			return (i);
13833859Sml29623 		}
13843859Sml29623 	}
13853859Sml29623 
13863859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
13873859Sml29623 		"<== nxge_rxdma_get_rbr_ring_index: not found"));
13883859Sml29623 
13893859Sml29623 	return (-1);
13903859Sml29623 }
13913859Sml29623 
13923859Sml29623 p_rx_rbr_ring_t
13933859Sml29623 nxge_rxdma_get_rbr_ring(p_nxge_t nxgep, uint16_t channel)
13943859Sml29623 {
13953859Sml29623 	int			i, ndmas;
13963859Sml29623 	uint16_t		rdc;
13973859Sml29623 	p_rx_rbr_rings_t 	rx_rbr_rings;
13983859Sml29623 	p_rx_rbr_ring_t		*rbr_rings;
13993859Sml29623 
14003859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
14013859Sml29623 		"==> nxge_rxdma_get_rbr_ring: channel %d", channel));
14023859Sml29623 
14033859Sml29623 	rx_rbr_rings = nxgep->rx_rbr_rings;
14043859Sml29623 	if (rx_rbr_rings == NULL) {
14053859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
14063859Sml29623 			"<== nxge_rxdma_get_rbr_ring: NULL ring pointer"));
14073859Sml29623 		return (NULL);
14083859Sml29623 	}
14093859Sml29623 	ndmas = rx_rbr_rings->ndmas;
14103859Sml29623 	if (!ndmas) {
14113859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
14123859Sml29623 			"<== nxge_rxdma_get_rbr_ring: no channel"));
14133859Sml29623 		return (NULL);
14143859Sml29623 	}
14153859Sml29623 
14163859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
14173859Sml29623 		"==> nxge_rxdma_get_ring (ndmas %d)", ndmas));
14183859Sml29623 
14193859Sml29623 	rbr_rings = rx_rbr_rings->rbr_rings;
14203859Sml29623 	for (i = 0; i < ndmas; i++) {
14213859Sml29623 		rdc = rbr_rings[i]->rdc;
14223859Sml29623 		if (channel == rdc) {
14233859Sml29623 			NXGE_DEBUG_MSG((nxgep, RX_CTL,
14243859Sml29623 				"==> nxge_rxdma_get_rbr_ring: channel %d "
14253859Sml29623 				"ring $%p", channel, rbr_rings[i]));
14263859Sml29623 			return (rbr_rings[i]);
14273859Sml29623 		}
14283859Sml29623 	}
14293859Sml29623 
14303859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
14313859Sml29623 		"<== nxge_rxdma_get_rbr_ring: not found"));
14323859Sml29623 
14333859Sml29623 	return (NULL);
14343859Sml29623 }
14353859Sml29623 
14363859Sml29623 p_rx_rcr_ring_t
14373859Sml29623 nxge_rxdma_get_rcr_ring(p_nxge_t nxgep, uint16_t channel)
14383859Sml29623 {
14393859Sml29623 	int			i, ndmas;
14403859Sml29623 	uint16_t		rdc;
14413859Sml29623 	p_rx_rcr_rings_t 	rx_rcr_rings;
14423859Sml29623 	p_rx_rcr_ring_t		*rcr_rings;
14433859Sml29623 
14443859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
14453859Sml29623 		"==> nxge_rxdma_get_rcr_ring: channel %d", channel));
14463859Sml29623 
14473859Sml29623 	rx_rcr_rings = nxgep->rx_rcr_rings;
14483859Sml29623 	if (rx_rcr_rings == NULL) {
14493859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
14503859Sml29623 			"<== nxge_rxdma_get_rcr_ring: NULL ring pointer"));
14513859Sml29623 		return (NULL);
14523859Sml29623 	}
14533859Sml29623 	ndmas = rx_rcr_rings->ndmas;
14543859Sml29623 	if (!ndmas) {
14553859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
14563859Sml29623 			"<== nxge_rxdma_get_rcr_ring: no channel"));
14573859Sml29623 		return (NULL);
14583859Sml29623 	}
14593859Sml29623 
14603859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
14613859Sml29623 		"==> nxge_rxdma_get_rcr_ring (ndmas %d)", ndmas));
14623859Sml29623 
14633859Sml29623 	rcr_rings = rx_rcr_rings->rcr_rings;
14643859Sml29623 	for (i = 0; i < ndmas; i++) {
14653859Sml29623 		rdc = rcr_rings[i]->rdc;
14663859Sml29623 		if (channel == rdc) {
14673859Sml29623 			NXGE_DEBUG_MSG((nxgep, RX_CTL,
14683859Sml29623 				"==> nxge_rxdma_get_rcr_ring: channel %d "
14693859Sml29623 				"ring $%p", channel, rcr_rings[i]));
14703859Sml29623 			return (rcr_rings[i]);
14713859Sml29623 		}
14723859Sml29623 	}
14733859Sml29623 
14743859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
14753859Sml29623 		"<== nxge_rxdma_get_rcr_ring: not found"));
14763859Sml29623 
14773859Sml29623 	return (NULL);
14783859Sml29623 }
14793859Sml29623 
14803859Sml29623 /*
14813859Sml29623  * Static functions start here.
14823859Sml29623  */
14833859Sml29623 static p_rx_msg_t
14843859Sml29623 nxge_allocb(size_t size, uint32_t pri, p_nxge_dma_common_t dmabuf_p)
14853859Sml29623 {
14863859Sml29623 	p_rx_msg_t nxge_mp 		= NULL;
14873859Sml29623 	p_nxge_dma_common_t		dmamsg_p;
14883859Sml29623 	uchar_t 			*buffer;
14893859Sml29623 
14903859Sml29623 	nxge_mp = KMEM_ZALLOC(sizeof (rx_msg_t), KM_NOSLEEP);
14913859Sml29623 	if (nxge_mp == NULL) {
14924185Sspeer 		NXGE_ERROR_MSG((NULL, NXGE_ERR_CTL,
14933859Sml29623 			"Allocation of a rx msg failed."));
14943859Sml29623 		goto nxge_allocb_exit;
14953859Sml29623 	}
14963859Sml29623 
14973859Sml29623 	nxge_mp->use_buf_pool = B_FALSE;
14983859Sml29623 	if (dmabuf_p) {
14993859Sml29623 		nxge_mp->use_buf_pool = B_TRUE;
15003859Sml29623 		dmamsg_p = (p_nxge_dma_common_t)&nxge_mp->buf_dma;
15013859Sml29623 		*dmamsg_p = *dmabuf_p;
15023859Sml29623 		dmamsg_p->nblocks = 1;
15033859Sml29623 		dmamsg_p->block_size = size;
15043859Sml29623 		dmamsg_p->alength = size;
15053859Sml29623 		buffer = (uchar_t *)dmabuf_p->kaddrp;
15063859Sml29623 
15073859Sml29623 		dmabuf_p->kaddrp = (void *)
15083859Sml29623 				((char *)dmabuf_p->kaddrp + size);
15093859Sml29623 		dmabuf_p->ioaddr_pp = (void *)
15103859Sml29623 				((char *)dmabuf_p->ioaddr_pp + size);
15113859Sml29623 		dmabuf_p->alength -= size;
15123859Sml29623 		dmabuf_p->offset += size;
15133859Sml29623 		dmabuf_p->dma_cookie.dmac_laddress += size;
15143859Sml29623 		dmabuf_p->dma_cookie.dmac_size -= size;
15153859Sml29623 
15163859Sml29623 	} else {
15173859Sml29623 		buffer = KMEM_ALLOC(size, KM_NOSLEEP);
15183859Sml29623 		if (buffer == NULL) {
15194185Sspeer 			NXGE_ERROR_MSG((NULL, NXGE_ERR_CTL,
15203859Sml29623 				"Allocation of a receive page failed."));
15213859Sml29623 			goto nxge_allocb_fail1;
15223859Sml29623 		}
15233859Sml29623 	}
15243859Sml29623 
15253859Sml29623 	nxge_mp->rx_mblk_p = desballoc(buffer, size, pri, &nxge_mp->freeb);
15263859Sml29623 	if (nxge_mp->rx_mblk_p == NULL) {
15274185Sspeer 		NXGE_ERROR_MSG((NULL, NXGE_ERR_CTL, "desballoc failed."));
15283859Sml29623 		goto nxge_allocb_fail2;
15293859Sml29623 	}
15303859Sml29623 
15313859Sml29623 	nxge_mp->buffer = buffer;
15323859Sml29623 	nxge_mp->block_size = size;
15333859Sml29623 	nxge_mp->freeb.free_func = (void (*)())nxge_freeb;
15343859Sml29623 	nxge_mp->freeb.free_arg = (caddr_t)nxge_mp;
15353859Sml29623 	nxge_mp->ref_cnt = 1;
15363859Sml29623 	nxge_mp->free = B_TRUE;
15373859Sml29623 	nxge_mp->rx_use_bcopy = B_FALSE;
15383859Sml29623 
15393859Sml29623 	atomic_inc_32(&nxge_mblks_pending);
15403859Sml29623 
15413859Sml29623 	goto nxge_allocb_exit;
15423859Sml29623 
15433859Sml29623 nxge_allocb_fail2:
15443859Sml29623 	if (!nxge_mp->use_buf_pool) {
15453859Sml29623 		KMEM_FREE(buffer, size);
15463859Sml29623 	}
15473859Sml29623 
15483859Sml29623 nxge_allocb_fail1:
15493859Sml29623 	KMEM_FREE(nxge_mp, sizeof (rx_msg_t));
15503859Sml29623 	nxge_mp = NULL;
15513859Sml29623 
15523859Sml29623 nxge_allocb_exit:
15533859Sml29623 	return (nxge_mp);
15543859Sml29623 }
15553859Sml29623 
15563859Sml29623 p_mblk_t
15573859Sml29623 nxge_dupb(p_rx_msg_t nxge_mp, uint_t offset, size_t size)
15583859Sml29623 {
15593859Sml29623 	p_mblk_t mp;
15603859Sml29623 
15613859Sml29623 	NXGE_DEBUG_MSG((NULL, MEM_CTL, "==> nxge_dupb"));
15623859Sml29623 	NXGE_DEBUG_MSG((NULL, MEM_CTL, "nxge_mp = $%p "
15633859Sml29623 		"offset = 0x%08X "
15643859Sml29623 		"size = 0x%08X",
15653859Sml29623 		nxge_mp, offset, size));
15663859Sml29623 
15673859Sml29623 	mp = desballoc(&nxge_mp->buffer[offset], size,
15683859Sml29623 				0, &nxge_mp->freeb);
15693859Sml29623 	if (mp == NULL) {
15703859Sml29623 		NXGE_DEBUG_MSG((NULL, RX_CTL, "desballoc failed"));
15713859Sml29623 		goto nxge_dupb_exit;
15723859Sml29623 	}
15733859Sml29623 	atomic_inc_32(&nxge_mp->ref_cnt);
15743859Sml29623 
15753859Sml29623 
15763859Sml29623 nxge_dupb_exit:
15773859Sml29623 	NXGE_DEBUG_MSG((NULL, MEM_CTL, "<== nxge_dupb mp = $%p",
15783859Sml29623 		nxge_mp));
15793859Sml29623 	return (mp);
15803859Sml29623 }
15813859Sml29623 
15823859Sml29623 p_mblk_t
15833859Sml29623 nxge_dupb_bcopy(p_rx_msg_t nxge_mp, uint_t offset, size_t size)
15843859Sml29623 {
15853859Sml29623 	p_mblk_t mp;
15863859Sml29623 	uchar_t *dp;
15873859Sml29623 
15883859Sml29623 	mp = allocb(size + NXGE_RXBUF_EXTRA, 0);
15893859Sml29623 	if (mp == NULL) {
15903859Sml29623 		NXGE_DEBUG_MSG((NULL, RX_CTL, "desballoc failed"));
15913859Sml29623 		goto nxge_dupb_bcopy_exit;
15923859Sml29623 	}
15933859Sml29623 	dp = mp->b_rptr = mp->b_rptr + NXGE_RXBUF_EXTRA;
15943859Sml29623 	bcopy((void *)&nxge_mp->buffer[offset], dp, size);
15953859Sml29623 	mp->b_wptr = dp + size;
15963859Sml29623 
15973859Sml29623 nxge_dupb_bcopy_exit:
15983859Sml29623 	NXGE_DEBUG_MSG((NULL, MEM_CTL, "<== nxge_dupb mp = $%p",
15993859Sml29623 		nxge_mp));
16003859Sml29623 	return (mp);
16013859Sml29623 }
16023859Sml29623 
16033859Sml29623 void nxge_post_page(p_nxge_t nxgep, p_rx_rbr_ring_t rx_rbr_p,
16043859Sml29623 	p_rx_msg_t rx_msg_p);
16053859Sml29623 
16063859Sml29623 void
16073859Sml29623 nxge_post_page(p_nxge_t nxgep, p_rx_rbr_ring_t rx_rbr_p, p_rx_msg_t rx_msg_p)
16083859Sml29623 {
16093859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "==> nxge_post_page"));
16103859Sml29623 
16113859Sml29623 	/* Reuse this buffer */
16123859Sml29623 	rx_msg_p->free = B_FALSE;
16133859Sml29623 	rx_msg_p->cur_usage_cnt = 0;
16143859Sml29623 	rx_msg_p->max_usage_cnt = 0;
16153859Sml29623 	rx_msg_p->pkt_buf_size = 0;
16163859Sml29623 
16173859Sml29623 	if (rx_rbr_p->rbr_use_bcopy) {
16183859Sml29623 		rx_msg_p->rx_use_bcopy = B_FALSE;
16193859Sml29623 		atomic_dec_32(&rx_rbr_p->rbr_consumed);
16203859Sml29623 	}
16213859Sml29623 
16223859Sml29623 	/*
16233859Sml29623 	 * Get the rbr header pointer and its offset index.
16243859Sml29623 	 */
16253859Sml29623 	MUTEX_ENTER(&rx_rbr_p->post_lock);
16263859Sml29623 	rx_rbr_p->rbr_wr_index =  ((rx_rbr_p->rbr_wr_index + 1) &
16273859Sml29623 					    rx_rbr_p->rbr_wrap_mask);
16283859Sml29623 	rx_rbr_p->rbr_desc_vp[rx_rbr_p->rbr_wr_index] = rx_msg_p->shifted_addr;
16293859Sml29623 	MUTEX_EXIT(&rx_rbr_p->post_lock);
16305770Sml29623 	npi_rxdma_rdc_rbr_kick(NXGE_DEV_NPI_HANDLE(nxgep),
16315770Sml29623 	    rx_rbr_p->rdc, 1);
16323859Sml29623 
16333859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
16343859Sml29623 		"<== nxge_post_page (channel %d post_next_index %d)",
16353859Sml29623 		rx_rbr_p->rdc, rx_rbr_p->rbr_wr_index));
16363859Sml29623 
16373859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "<== nxge_post_page"));
16383859Sml29623 }
16393859Sml29623 
16403859Sml29623 void
16413859Sml29623 nxge_freeb(p_rx_msg_t rx_msg_p)
16423859Sml29623 {
16433859Sml29623 	size_t size;
16443859Sml29623 	uchar_t *buffer = NULL;
16453859Sml29623 	int ref_cnt;
16464874Sml29623 	boolean_t free_state = B_FALSE;
16473859Sml29623 
16485170Stm144005 	rx_rbr_ring_t *ring = rx_msg_p->rx_rbr_p;
16495170Stm144005 
16503859Sml29623 	NXGE_DEBUG_MSG((NULL, MEM2_CTL, "==> nxge_freeb"));
16513859Sml29623 	NXGE_DEBUG_MSG((NULL, MEM2_CTL,
16523859Sml29623 		"nxge_freeb:rx_msg_p = $%p (block pending %d)",
16533859Sml29623 		rx_msg_p, nxge_mblks_pending));
16543859Sml29623 
16554874Sml29623 	/*
16564874Sml29623 	 * First we need to get the free state, then
16574874Sml29623 	 * atomic decrement the reference count to prevent
16584874Sml29623 	 * the race condition with the interrupt thread that
16594874Sml29623 	 * is processing a loaned up buffer block.
16604874Sml29623 	 */
16614874Sml29623 	free_state = rx_msg_p->free;
16623859Sml29623 	ref_cnt = atomic_add_32_nv(&rx_msg_p->ref_cnt, -1);
16633859Sml29623 	if (!ref_cnt) {
16645770Sml29623 		atomic_dec_32(&nxge_mblks_pending);
16653859Sml29623 		buffer = rx_msg_p->buffer;
16663859Sml29623 		size = rx_msg_p->block_size;
16673859Sml29623 		NXGE_DEBUG_MSG((NULL, MEM2_CTL, "nxge_freeb: "
16683859Sml29623 			"will free: rx_msg_p = $%p (block pending %d)",
16694185Sspeer 			rx_msg_p, nxge_mblks_pending));
16703859Sml29623 
16713859Sml29623 		if (!rx_msg_p->use_buf_pool) {
16723859Sml29623 			KMEM_FREE(buffer, size);
16733859Sml29623 		}
16743859Sml29623 
16753859Sml29623 		KMEM_FREE(rx_msg_p, sizeof (rx_msg_t));
16765170Stm144005 
16775759Smisaki 		if (ring) {
16785759Smisaki 			/*
16795759Smisaki 			 * Decrement the receive buffer ring's reference
16805759Smisaki 			 * count, too.
16815759Smisaki 			 */
16825759Smisaki 			atomic_dec_32(&ring->rbr_ref_cnt);
16835759Smisaki 
16845759Smisaki 			/*
16855759Smisaki 			 * Free the receive buffer ring, iff
16865759Smisaki 			 * 1. all the receive buffers have been freed
16875759Smisaki 			 * 2. and we are in the proper state (that is,
16885759Smisaki 			 *    we are not UNMAPPING).
16895759Smisaki 			 */
16905759Smisaki 			if (ring->rbr_ref_cnt == 0 &&
16915759Smisaki 			    ring->rbr_state == RBR_UNMAPPED) {
16925759Smisaki 				KMEM_FREE(ring, sizeof (*ring));
16935759Smisaki 			}
16945170Stm144005 		}
16953859Sml29623 		return;
16963859Sml29623 	}
16973859Sml29623 
16983859Sml29623 	/*
16993859Sml29623 	 * Repost buffer.
17003859Sml29623 	 */
17015759Smisaki 	if (free_state && (ref_cnt == 1) && ring) {
17023859Sml29623 		NXGE_DEBUG_MSG((NULL, RX_CTL,
17033859Sml29623 		    "nxge_freeb: post page $%p:", rx_msg_p));
17045170Stm144005 		if (ring->rbr_state == RBR_POSTING)
17055170Stm144005 			nxge_post_page(rx_msg_p->nxgep, ring, rx_msg_p);
17063859Sml29623 	}
17073859Sml29623 
17083859Sml29623 	NXGE_DEBUG_MSG((NULL, MEM2_CTL, "<== nxge_freeb"));
17093859Sml29623 }
17103859Sml29623 
17113859Sml29623 uint_t
17123859Sml29623 nxge_rx_intr(void *arg1, void *arg2)
17133859Sml29623 {
17143859Sml29623 	p_nxge_ldv_t		ldvp = (p_nxge_ldv_t)arg1;
17153859Sml29623 	p_nxge_t		nxgep = (p_nxge_t)arg2;
17163859Sml29623 	p_nxge_ldg_t		ldgp;
17173859Sml29623 	uint8_t			channel;
17183859Sml29623 	npi_handle_t		handle;
17193859Sml29623 	rx_dma_ctl_stat_t	cs;
17203859Sml29623 
17213859Sml29623 #ifdef	NXGE_DEBUG
17223859Sml29623 	rxdma_cfig1_t		cfg;
17233859Sml29623 #endif
17243859Sml29623 	uint_t 			serviced = DDI_INTR_UNCLAIMED;
17253859Sml29623 
17263859Sml29623 	if (ldvp == NULL) {
17273859Sml29623 		NXGE_DEBUG_MSG((NULL, INT_CTL,
17283859Sml29623 			"<== nxge_rx_intr: arg2 $%p arg1 $%p",
17293859Sml29623 			nxgep, ldvp));
17303859Sml29623 
17313859Sml29623 		return (DDI_INTR_CLAIMED);
17323859Sml29623 	}
17333859Sml29623 
17343859Sml29623 	if (arg2 == NULL || (void *)ldvp->nxgep != arg2) {
17353859Sml29623 		nxgep = ldvp->nxgep;
17363859Sml29623 	}
17373859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
17383859Sml29623 		"==> nxge_rx_intr: arg2 $%p arg1 $%p",
17393859Sml29623 		nxgep, ldvp));
17403859Sml29623 
17413859Sml29623 	/*
17423859Sml29623 	 * This interrupt handler is for a specific
17433859Sml29623 	 * receive dma channel.
17443859Sml29623 	 */
17453859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
17463859Sml29623 	/*
17473859Sml29623 	 * Get the control and status for this channel.
17483859Sml29623 	 */
17493859Sml29623 	channel = ldvp->channel;
17503859Sml29623 	ldgp = ldvp->ldgp;
17513859Sml29623 	RXDMA_REG_READ64(handle, RX_DMA_CTL_STAT_REG, channel, &cs.value);
17523859Sml29623 
17533859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "==> nxge_rx_intr:channel %d "
17543859Sml29623 		"cs 0x%016llx rcrto 0x%x rcrthres %x",
17553859Sml29623 		channel,
17563859Sml29623 		cs.value,
17573859Sml29623 		cs.bits.hdw.rcrto,
17583859Sml29623 		cs.bits.hdw.rcrthres));
17593859Sml29623 
17603859Sml29623 	nxge_rx_pkts_vring(nxgep, ldvp->vdma_index, ldvp, cs);
17613859Sml29623 	serviced = DDI_INTR_CLAIMED;
17623859Sml29623 
17633859Sml29623 	/* error events. */
17643859Sml29623 	if (cs.value & RX_DMA_CTL_STAT_ERROR) {
17653859Sml29623 		(void) nxge_rx_err_evnts(nxgep, ldvp->vdma_index, ldvp, cs);
17663859Sml29623 	}
17673859Sml29623 
17683859Sml29623 nxge_intr_exit:
17693859Sml29623 
17703859Sml29623 
17713859Sml29623 	/*
17723859Sml29623 	 * Enable the mailbox update interrupt if we want
17733859Sml29623 	 * to use mailbox. We probably don't need to use
17743859Sml29623 	 * mailbox as it only saves us one pio read.
17753859Sml29623 	 * Also write 1 to rcrthres and rcrto to clear
17763859Sml29623 	 * these two edge triggered bits.
17773859Sml29623 	 */
17783859Sml29623 
17793859Sml29623 	cs.value &= RX_DMA_CTL_STAT_WR1C;
17803859Sml29623 	cs.bits.hdw.mex = 1;
17813859Sml29623 	RXDMA_REG_WRITE64(handle, RX_DMA_CTL_STAT_REG, channel,
17823859Sml29623 			cs.value);
17833859Sml29623 
17843859Sml29623 	/*
17853859Sml29623 	 * Rearm this logical group if this is a single device
17863859Sml29623 	 * group.
17873859Sml29623 	 */
17883859Sml29623 	if (ldgp->nldvs == 1) {
17893859Sml29623 		ldgimgm_t		mgm;
17903859Sml29623 		mgm.value = 0;
17913859Sml29623 		mgm.bits.ldw.arm = 1;
17923859Sml29623 		mgm.bits.ldw.timer = ldgp->ldg_timer;
17933859Sml29623 		NXGE_REG_WR64(handle,
17943859Sml29623 			    LDGIMGN_REG + LDSV_OFFSET(ldgp->ldg),
17953859Sml29623 			    mgm.value);
17963859Sml29623 	}
17973859Sml29623 
17983859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "<== nxge_rx_intr: serviced %d",
17993859Sml29623 		serviced));
18003859Sml29623 	return (serviced);
18013859Sml29623 }
18023859Sml29623 
18033859Sml29623 /*
18043859Sml29623  * Process the packets received in the specified logical device
18053859Sml29623  * and pass up a chain of message blocks to the upper layer.
18063859Sml29623  */
18073859Sml29623 static void
18083859Sml29623 nxge_rx_pkts_vring(p_nxge_t nxgep, uint_t vindex, p_nxge_ldv_t ldvp,
18093859Sml29623 				    rx_dma_ctl_stat_t cs)
18103859Sml29623 {
18113859Sml29623 	p_mblk_t		mp;
18123859Sml29623 	p_rx_rcr_ring_t		rcrp;
18133859Sml29623 
18143859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "==> nxge_rx_pkts_vring"));
18153859Sml29623 	if ((mp = nxge_rx_pkts(nxgep, vindex, ldvp, &rcrp, cs)) == NULL) {
18163859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
18173859Sml29623 			"<== nxge_rx_pkts_vring: no mp"));
18183859Sml29623 		return;
18193859Sml29623 	}
18203859Sml29623 
18213859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "==> nxge_rx_pkts_vring: $%p",
18223859Sml29623 		mp));
18233859Sml29623 
18243859Sml29623 #ifdef  NXGE_DEBUG
18253859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
18263859Sml29623 			"==> nxge_rx_pkts_vring:calling mac_rx "
18273859Sml29623 			"LEN %d mp $%p mp->b_cont $%p mp->b_next $%p rcrp $%p "
18283859Sml29623 			"mac_handle $%p",
18293859Sml29623 			mp->b_wptr - mp->b_rptr,
18303859Sml29623 			mp, mp->b_cont, mp->b_next,
18313859Sml29623 			rcrp, rcrp->rcr_mac_handle));
18323859Sml29623 
18333859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
18343859Sml29623 			"==> nxge_rx_pkts_vring: dump packets "
18353859Sml29623 			"(mp $%p b_rptr $%p b_wptr $%p):\n %s",
18363859Sml29623 			mp,
18373859Sml29623 			mp->b_rptr,
18383859Sml29623 			mp->b_wptr,
18393859Sml29623 			nxge_dump_packet((char *)mp->b_rptr,
18403859Sml29623 			mp->b_wptr - mp->b_rptr)));
18413859Sml29623 		if (mp->b_cont) {
18423859Sml29623 			NXGE_DEBUG_MSG((nxgep, RX_CTL,
18433859Sml29623 				"==> nxge_rx_pkts_vring: dump b_cont packets "
18443859Sml29623 				"(mp->b_cont $%p b_rptr $%p b_wptr $%p):\n %s",
18453859Sml29623 				mp->b_cont,
18463859Sml29623 				mp->b_cont->b_rptr,
18473859Sml29623 				mp->b_cont->b_wptr,
18483859Sml29623 				nxge_dump_packet((char *)mp->b_cont->b_rptr,
18493859Sml29623 				mp->b_cont->b_wptr - mp->b_cont->b_rptr)));
18503859Sml29623 		}
18513859Sml29623 		if (mp->b_next) {
18523859Sml29623 			NXGE_DEBUG_MSG((nxgep, RX_CTL,
18533859Sml29623 				"==> nxge_rx_pkts_vring: dump next packets "
18543859Sml29623 				"(b_rptr $%p): %s",
18553859Sml29623 				mp->b_next->b_rptr,
18563859Sml29623 				nxge_dump_packet((char *)mp->b_next->b_rptr,
18573859Sml29623 				mp->b_next->b_wptr - mp->b_next->b_rptr)));
18583859Sml29623 		}
18593859Sml29623 #endif
18603859Sml29623 
18613859Sml29623 	mac_rx(nxgep->mach, rcrp->rcr_mac_handle, mp);
18623859Sml29623 }
18633859Sml29623 
18643859Sml29623 
18653859Sml29623 /*
18663859Sml29623  * This routine is the main packet receive processing function.
18673859Sml29623  * It gets the packet type, error code, and buffer related
18683859Sml29623  * information from the receive completion entry.
18693859Sml29623  * How many completion entries to process is based on the number of packets
18703859Sml29623  * queued by the hardware, a hardware maintained tail pointer
18713859Sml29623  * and a configurable receive packet count.
18723859Sml29623  *
18733859Sml29623  * A chain of message blocks will be created as result of processing
18743859Sml29623  * the completion entries. This chain of message blocks will be returned and
18753859Sml29623  * a hardware control status register will be updated with the number of
18763859Sml29623  * packets were removed from the hardware queue.
18773859Sml29623  *
18783859Sml29623  */
18793859Sml29623 mblk_t *
18803859Sml29623 nxge_rx_pkts(p_nxge_t nxgep, uint_t vindex, p_nxge_ldv_t ldvp,
18813859Sml29623     p_rx_rcr_ring_t *rcrp, rx_dma_ctl_stat_t cs)
18823859Sml29623 {
18833859Sml29623 	npi_handle_t		handle;
18843859Sml29623 	uint8_t			channel;
18853859Sml29623 	p_rx_rcr_rings_t	rx_rcr_rings;
18863859Sml29623 	p_rx_rcr_ring_t		rcr_p;
18873859Sml29623 	uint32_t		comp_rd_index;
18883859Sml29623 	p_rcr_entry_t		rcr_desc_rd_head_p;
18893859Sml29623 	p_rcr_entry_t		rcr_desc_rd_head_pp;
18903859Sml29623 	p_mblk_t		nmp, mp_cont, head_mp, *tail_mp;
18913859Sml29623 	uint16_t		qlen, nrcr_read, npkt_read;
18923859Sml29623 	uint32_t qlen_hw;
18933859Sml29623 	boolean_t		multi;
18943859Sml29623 	rcrcfig_b_t rcr_cfg_b;
18953859Sml29623 #if defined(_BIG_ENDIAN)
18963859Sml29623 	npi_status_t		rs = NPI_SUCCESS;
18973859Sml29623 #endif
18983859Sml29623 
18993859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "==> nxge_rx_pkts:vindex %d "
19003859Sml29623 		"channel %d", vindex, ldvp->channel));
19013859Sml29623 
19023859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
19033859Sml29623 		return (NULL);
19043859Sml29623 	}
19053859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
19063859Sml29623 	rx_rcr_rings = nxgep->rx_rcr_rings;
19073859Sml29623 	rcr_p = rx_rcr_rings->rcr_rings[vindex];
19083859Sml29623 	channel = rcr_p->rdc;
19093859Sml29623 	if (channel != ldvp->channel) {
19103859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL, "==> nxge_rx_pkts:index %d "
19113859Sml29623 			"channel %d, and rcr channel %d not matched.",
19123859Sml29623 			vindex, ldvp->channel, channel));
19133859Sml29623 		return (NULL);
19143859Sml29623 	}
19153859Sml29623 
19163859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
19173859Sml29623 		"==> nxge_rx_pkts: START: rcr channel %d "
19183859Sml29623 		"head_p $%p head_pp $%p  index %d ",
19193859Sml29623 		channel, rcr_p->rcr_desc_rd_head_p,
19203859Sml29623 		rcr_p->rcr_desc_rd_head_pp,
19213859Sml29623 		rcr_p->comp_rd_index));
19223859Sml29623 
19233859Sml29623 
19243859Sml29623 #if !defined(_BIG_ENDIAN)
19253859Sml29623 	qlen = RXDMA_REG_READ32(handle, RCRSTAT_A_REG, channel) & 0xffff;
19263859Sml29623 #else
19273859Sml29623 	rs = npi_rxdma_rdc_rcr_qlen_get(handle, channel, &qlen);
19283859Sml29623 	if (rs != NPI_SUCCESS) {
19293859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL, "==> nxge_rx_pkts:index %d "
19303859Sml29623 		"channel %d, get qlen failed 0x%08x",
19313859Sml29623 		vindex, ldvp->channel, rs));
19323859Sml29623 		return (NULL);
19333859Sml29623 	}
19343859Sml29623 #endif
19353859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "==> nxge_rx_pkts:rcr channel %d "
19363859Sml29623 		"qlen %d", channel, qlen));
19373859Sml29623 
19383859Sml29623 
19393859Sml29623 
19403859Sml29623 	if (!qlen) {
19413859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
19423859Sml29623 			"==> nxge_rx_pkts:rcr channel %d "
19433859Sml29623 			"qlen %d (no pkts)", channel, qlen));
19443859Sml29623 
19453859Sml29623 		return (NULL);
19463859Sml29623 	}
19473859Sml29623 
19483859Sml29623 	comp_rd_index = rcr_p->comp_rd_index;
19493859Sml29623 
19503859Sml29623 	rcr_desc_rd_head_p = rcr_p->rcr_desc_rd_head_p;
19513859Sml29623 	rcr_desc_rd_head_pp = rcr_p->rcr_desc_rd_head_pp;
19523859Sml29623 	nrcr_read = npkt_read = 0;
19533859Sml29623 
19543859Sml29623 	/*
19553859Sml29623 	 * Number of packets queued
19563859Sml29623 	 * (The jumbo or multi packet will be counted as only one
19573859Sml29623 	 *  packets and it may take up more than one completion entry).
19583859Sml29623 	 */
19593859Sml29623 	qlen_hw = (qlen < nxge_max_rx_pkts) ?
19603859Sml29623 		qlen : nxge_max_rx_pkts;
19613859Sml29623 	head_mp = NULL;
19623859Sml29623 	tail_mp = &head_mp;
19633859Sml29623 	nmp = mp_cont = NULL;
19643859Sml29623 	multi = B_FALSE;
19653859Sml29623 
19663859Sml29623 	while (qlen_hw) {
19673859Sml29623 
19683859Sml29623 #ifdef NXGE_DEBUG
19693859Sml29623 		nxge_dump_rcr_entry(nxgep, rcr_desc_rd_head_p);
19703859Sml29623 #endif
19713859Sml29623 		/*
19723859Sml29623 		 * Process one completion ring entry.
19733859Sml29623 		 */
19743859Sml29623 		nxge_receive_packet(nxgep,
19753859Sml29623 			rcr_p, rcr_desc_rd_head_p, &multi, &nmp, &mp_cont);
19763859Sml29623 
19773859Sml29623 		/*
19783859Sml29623 		 * message chaining modes
19793859Sml29623 		 */
19803859Sml29623 		if (nmp) {
19813859Sml29623 			nmp->b_next = NULL;
19823859Sml29623 			if (!multi && !mp_cont) { /* frame fits a partition */
19833859Sml29623 				*tail_mp = nmp;
19843859Sml29623 				tail_mp = &nmp->b_next;
19853859Sml29623 				nmp = NULL;
19863859Sml29623 			} else if (multi && !mp_cont) { /* first segment */
19873859Sml29623 				*tail_mp = nmp;
19883859Sml29623 				tail_mp = &nmp->b_cont;
19893859Sml29623 			} else if (multi && mp_cont) {	/* mid of multi segs */
19903859Sml29623 				*tail_mp = mp_cont;
19913859Sml29623 				tail_mp = &mp_cont->b_cont;
19923859Sml29623 			} else if (!multi && mp_cont) { /* last segment */
19933859Sml29623 				*tail_mp = mp_cont;
19943859Sml29623 				tail_mp = &nmp->b_next;
19953859Sml29623 				nmp = NULL;
19963859Sml29623 			}
19973859Sml29623 		}
19983859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
19993859Sml29623 			"==> nxge_rx_pkts: loop: rcr channel %d "
20003859Sml29623 			"before updating: multi %d "
20013859Sml29623 			"nrcr_read %d "
20023859Sml29623 			"npk read %d "
20033859Sml29623 			"head_pp $%p  index %d ",
20043859Sml29623 			channel,
20053859Sml29623 			multi,
20063859Sml29623 			nrcr_read, npkt_read, rcr_desc_rd_head_pp,
20073859Sml29623 			comp_rd_index));
20083859Sml29623 
20093859Sml29623 		if (!multi) {
20103859Sml29623 			qlen_hw--;
20113859Sml29623 			npkt_read++;
20123859Sml29623 		}
20133859Sml29623 
20143859Sml29623 		/*
20153859Sml29623 		 * Update the next read entry.
20163859Sml29623 		 */
20173859Sml29623 		comp_rd_index = NEXT_ENTRY(comp_rd_index,
20183859Sml29623 					rcr_p->comp_wrap_mask);
20193859Sml29623 
20203859Sml29623 		rcr_desc_rd_head_p = NEXT_ENTRY_PTR(rcr_desc_rd_head_p,
20213859Sml29623 				rcr_p->rcr_desc_first_p,
20223859Sml29623 				rcr_p->rcr_desc_last_p);
20233859Sml29623 
20243859Sml29623 		nrcr_read++;
20253859Sml29623 
20263859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
20273859Sml29623 			"<== nxge_rx_pkts: (SAM, process one packet) "
20283859Sml29623 			"nrcr_read %d",
20293859Sml29623 			nrcr_read));
20303859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
20313859Sml29623 			"==> nxge_rx_pkts: loop: rcr channel %d "
20323859Sml29623 			"multi %d "
20333859Sml29623 			"nrcr_read %d "
20343859Sml29623 			"npk read %d "
20353859Sml29623 			"head_pp $%p  index %d ",
20363859Sml29623 			channel,
20373859Sml29623 			multi,
20383859Sml29623 			nrcr_read, npkt_read, rcr_desc_rd_head_pp,
20393859Sml29623 			comp_rd_index));
20403859Sml29623 
20413859Sml29623 	}
20423859Sml29623 
20433859Sml29623 	rcr_p->rcr_desc_rd_head_pp = rcr_desc_rd_head_pp;
20443859Sml29623 	rcr_p->comp_rd_index = comp_rd_index;
20453859Sml29623 	rcr_p->rcr_desc_rd_head_p = rcr_desc_rd_head_p;
20463859Sml29623 
20473859Sml29623 	if ((nxgep->intr_timeout != rcr_p->intr_timeout) ||
20483859Sml29623 		(nxgep->intr_threshold != rcr_p->intr_threshold)) {
20493859Sml29623 		rcr_p->intr_timeout = nxgep->intr_timeout;
20503859Sml29623 		rcr_p->intr_threshold = nxgep->intr_threshold;
20513859Sml29623 		rcr_cfg_b.value = 0x0ULL;
20523859Sml29623 		if (rcr_p->intr_timeout)
20533859Sml29623 			rcr_cfg_b.bits.ldw.entout = 1;
20543859Sml29623 		rcr_cfg_b.bits.ldw.timeout = rcr_p->intr_timeout;
20553859Sml29623 		rcr_cfg_b.bits.ldw.pthres = rcr_p->intr_threshold;
20563859Sml29623 		RXDMA_REG_WRITE64(handle, RCRCFIG_B_REG,
20573859Sml29623 				    channel, rcr_cfg_b.value);
20583859Sml29623 	}
20593859Sml29623 
20603859Sml29623 	cs.bits.ldw.pktread = npkt_read;
20613859Sml29623 	cs.bits.ldw.ptrread = nrcr_read;
20623859Sml29623 	RXDMA_REG_WRITE64(handle, RX_DMA_CTL_STAT_REG,
20633859Sml29623 			    channel, cs.value);
20643859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
20653859Sml29623 		"==> nxge_rx_pkts: EXIT: rcr channel %d "
20663859Sml29623 		"head_pp $%p  index %016llx ",
20673859Sml29623 		channel,
20683859Sml29623 		rcr_p->rcr_desc_rd_head_pp,
20693859Sml29623 		rcr_p->comp_rd_index));
20703859Sml29623 	/*
20713859Sml29623 	 * Update RCR buffer pointer read and number of packets
20723859Sml29623 	 * read.
20733859Sml29623 	 */
20743859Sml29623 
20753859Sml29623 	*rcrp = rcr_p;
20763859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "<== nxge_rx_pkts"));
20773859Sml29623 	return (head_mp);
20783859Sml29623 }
20793859Sml29623 
20803859Sml29623 void
20813859Sml29623 nxge_receive_packet(p_nxge_t nxgep,
20823859Sml29623     p_rx_rcr_ring_t rcr_p, p_rcr_entry_t rcr_desc_rd_head_p,
20833859Sml29623     boolean_t *multi_p, mblk_t **mp, mblk_t **mp_cont)
20843859Sml29623 {
20853859Sml29623 	p_mblk_t		nmp = NULL;
20863859Sml29623 	uint64_t		multi;
20873859Sml29623 	uint64_t		dcf_err;
20883859Sml29623 	uint8_t			channel;
20893859Sml29623 
20903859Sml29623 	boolean_t		first_entry = B_TRUE;
20913859Sml29623 	boolean_t		is_tcp_udp = B_FALSE;
20923859Sml29623 	boolean_t		buffer_free = B_FALSE;
20933859Sml29623 	boolean_t		error_send_up = B_FALSE;
20943859Sml29623 	uint8_t			error_type;
20953859Sml29623 	uint16_t		l2_len;
20963859Sml29623 	uint16_t		skip_len;
20973859Sml29623 	uint8_t			pktbufsz_type;
20983859Sml29623 	uint64_t		rcr_entry;
20993859Sml29623 	uint64_t		*pkt_buf_addr_pp;
21003859Sml29623 	uint64_t		*pkt_buf_addr_p;
21013859Sml29623 	uint32_t		buf_offset;
21023859Sml29623 	uint32_t		bsize;
21033859Sml29623 	uint32_t		error_disp_cnt;
21043859Sml29623 	uint32_t		msg_index;
21053859Sml29623 	p_rx_rbr_ring_t		rx_rbr_p;
21063859Sml29623 	p_rx_msg_t 		*rx_msg_ring_p;
21073859Sml29623 	p_rx_msg_t		rx_msg_p;
21083859Sml29623 	uint16_t		sw_offset_bytes = 0, hdr_size = 0;
21093859Sml29623 	nxge_status_t		status = NXGE_OK;
21103859Sml29623 	boolean_t		is_valid = B_FALSE;
21113859Sml29623 	p_nxge_rx_ring_stats_t	rdc_stats;
21123859Sml29623 	uint32_t		bytes_read;
21133859Sml29623 	uint64_t		pkt_type;
21143859Sml29623 	uint64_t		frag;
2115*6028Ssbehera 	boolean_t		pkt_too_long_err = B_FALSE;
21163859Sml29623 #ifdef	NXGE_DEBUG
21173859Sml29623 	int			dump_len;
21183859Sml29623 #endif
21193859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL, "==> nxge_receive_packet"));
21203859Sml29623 	first_entry = (*mp == NULL) ? B_TRUE : B_FALSE;
21213859Sml29623 
21223859Sml29623 	rcr_entry = *((uint64_t *)rcr_desc_rd_head_p);
21233859Sml29623 
21243859Sml29623 	multi = (rcr_entry & RCR_MULTI_MASK);
21253859Sml29623 	dcf_err = (rcr_entry & RCR_DCF_ERROR_MASK);
21263859Sml29623 	pkt_type = (rcr_entry & RCR_PKT_TYPE_MASK);
21273859Sml29623 
21283859Sml29623 	error_type = ((rcr_entry & RCR_ERROR_MASK) >> RCR_ERROR_SHIFT);
21293859Sml29623 	frag = (rcr_entry & RCR_FRAG_MASK);
21303859Sml29623 
21313859Sml29623 	l2_len = ((rcr_entry & RCR_L2_LEN_MASK) >> RCR_L2_LEN_SHIFT);
21323859Sml29623 
21333859Sml29623 	pktbufsz_type = ((rcr_entry & RCR_PKTBUFSZ_MASK) >>
21343859Sml29623 				RCR_PKTBUFSZ_SHIFT);
21355125Sjoycey #if defined(__i386)
21365125Sjoycey 	pkt_buf_addr_pp = (uint64_t *)(uint32_t)((rcr_entry &
21375125Sjoycey 			RCR_PKT_BUF_ADDR_MASK) << RCR_PKT_BUF_ADDR_SHIFT);
21385125Sjoycey #else
21393859Sml29623 	pkt_buf_addr_pp = (uint64_t *)((rcr_entry & RCR_PKT_BUF_ADDR_MASK) <<
21403859Sml29623 			RCR_PKT_BUF_ADDR_SHIFT);
21415125Sjoycey #endif
21423859Sml29623 
21433859Sml29623 	channel = rcr_p->rdc;
21443859Sml29623 
21453859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
21463859Sml29623 		"==> nxge_receive_packet: entryp $%p entry 0x%0llx "
21473859Sml29623 		"pkt_buf_addr_pp $%p l2_len %d multi 0x%llx "
21483859Sml29623 		"error_type 0x%x pkt_type 0x%x  "
21493859Sml29623 		"pktbufsz_type %d ",
21503859Sml29623 		rcr_desc_rd_head_p,
21513859Sml29623 		rcr_entry, pkt_buf_addr_pp, l2_len,
21523859Sml29623 		multi,
21533859Sml29623 		error_type,
21543859Sml29623 		pkt_type,
21553859Sml29623 		pktbufsz_type));
21563859Sml29623 
21573859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
21583859Sml29623 		"==> nxge_receive_packet: entryp $%p entry 0x%0llx "
21593859Sml29623 		"pkt_buf_addr_pp $%p l2_len %d multi 0x%llx "
21603859Sml29623 		"error_type 0x%x pkt_type 0x%x ", rcr_desc_rd_head_p,
21613859Sml29623 		rcr_entry, pkt_buf_addr_pp, l2_len,
21623859Sml29623 		multi,
21633859Sml29623 		error_type,
21643859Sml29623 		pkt_type));
21653859Sml29623 
21663859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
21673859Sml29623 		"==> (rbr) nxge_receive_packet: entry 0x%0llx "
21683859Sml29623 		"full pkt_buf_addr_pp $%p l2_len %d",
21693859Sml29623 		rcr_entry, pkt_buf_addr_pp, l2_len));
21703859Sml29623 
21713859Sml29623 	/* get the stats ptr */
21723859Sml29623 	rdc_stats = rcr_p->rdc_stats;
21733859Sml29623 
21743859Sml29623 	if (!l2_len) {
21753859Sml29623 
21763859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
21773859Sml29623 			"<== nxge_receive_packet: failed: l2 length is 0."));
21783859Sml29623 		return;
21793859Sml29623 	}
21803859Sml29623 
2181*6028Ssbehera 	/*
2182*6028Ssbehera 	 * Sofware workaround for BMAC hardware limitation that allows
2183*6028Ssbehera 	 * maxframe size of 1526, instead of 1522 for non-jumbo and 0x2406
2184*6028Ssbehera 	 * instead of 0x2400 for jumbo.
2185*6028Ssbehera 	 */
2186*6028Ssbehera 	if (l2_len > nxgep->mac.maxframesize) {
2187*6028Ssbehera 		pkt_too_long_err = B_TRUE;
2188*6028Ssbehera 	}
2189*6028Ssbehera 
21904185Sspeer 	/* Hardware sends us 4 bytes of CRC as no stripping is done.  */
21914185Sspeer 	l2_len -= ETHERFCSL;
21924185Sspeer 
21933859Sml29623 	/* shift 6 bits to get the full io address */
21945125Sjoycey #if defined(__i386)
21955125Sjoycey 	pkt_buf_addr_pp = (uint64_t *)((uint32_t)pkt_buf_addr_pp <<
21965125Sjoycey 				RCR_PKT_BUF_ADDR_SHIFT_FULL);
21975125Sjoycey #else
21983859Sml29623 	pkt_buf_addr_pp = (uint64_t *)((uint64_t)pkt_buf_addr_pp <<
21993859Sml29623 				RCR_PKT_BUF_ADDR_SHIFT_FULL);
22005125Sjoycey #endif
22013859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
22023859Sml29623 		"==> (rbr) nxge_receive_packet: entry 0x%0llx "
22033859Sml29623 		"full pkt_buf_addr_pp $%p l2_len %d",
22043859Sml29623 		rcr_entry, pkt_buf_addr_pp, l2_len));
22053859Sml29623 
22063859Sml29623 	rx_rbr_p = rcr_p->rx_rbr_p;
22073859Sml29623 	rx_msg_ring_p = rx_rbr_p->rx_msg_ring;
22083859Sml29623 
22093859Sml29623 	if (first_entry) {
22103859Sml29623 		hdr_size = (rcr_p->full_hdr_flag ? RXDMA_HDR_SIZE_FULL :
22113859Sml29623 			RXDMA_HDR_SIZE_DEFAULT);
22123859Sml29623 
22133859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
22143859Sml29623 			"==> nxge_receive_packet: first entry 0x%016llx "
22153859Sml29623 			"pkt_buf_addr_pp $%p l2_len %d hdr %d",
22163859Sml29623 			rcr_entry, pkt_buf_addr_pp, l2_len,
22173859Sml29623 			hdr_size));
22183859Sml29623 	}
22193859Sml29623 
22203859Sml29623 	MUTEX_ENTER(&rcr_p->lock);
22213859Sml29623 	MUTEX_ENTER(&rx_rbr_p->lock);
22223859Sml29623 
22233859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
22243859Sml29623 		"==> (rbr 1) nxge_receive_packet: entry 0x%0llx "
22253859Sml29623 		"full pkt_buf_addr_pp $%p l2_len %d",
22263859Sml29623 		rcr_entry, pkt_buf_addr_pp, l2_len));
22273859Sml29623 
22283859Sml29623 	/*
22293859Sml29623 	 * Packet buffer address in the completion entry points
22303859Sml29623 	 * to the starting buffer address (offset 0).
22313859Sml29623 	 * Use the starting buffer address to locate the corresponding
22323859Sml29623 	 * kernel address.
22333859Sml29623 	 */
22343859Sml29623 	status = nxge_rxbuf_pp_to_vp(nxgep, rx_rbr_p,
22353859Sml29623 			pktbufsz_type, pkt_buf_addr_pp, &pkt_buf_addr_p,
22363859Sml29623 			&buf_offset,
22373859Sml29623 			&msg_index);
22383859Sml29623 
22393859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
22403859Sml29623 		"==> (rbr 2) nxge_receive_packet: entry 0x%0llx "
22413859Sml29623 		"full pkt_buf_addr_pp $%p l2_len %d",
22423859Sml29623 		rcr_entry, pkt_buf_addr_pp, l2_len));
22433859Sml29623 
22443859Sml29623 	if (status != NXGE_OK) {
22453859Sml29623 		MUTEX_EXIT(&rx_rbr_p->lock);
22463859Sml29623 		MUTEX_EXIT(&rcr_p->lock);
22473859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
22483859Sml29623 			"<== nxge_receive_packet: found vaddr failed %d",
22493859Sml29623 				status));
22503859Sml29623 		return;
22513859Sml29623 	}
22523859Sml29623 
22533859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
22543859Sml29623 		"==> (rbr 3) nxge_receive_packet: entry 0x%0llx "
22553859Sml29623 		"full pkt_buf_addr_pp $%p l2_len %d",
22563859Sml29623 		rcr_entry, pkt_buf_addr_pp, l2_len));
22573859Sml29623 
22583859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
22593859Sml29623 		"==> (rbr 4 msgindex %d) nxge_receive_packet: entry 0x%0llx "
22603859Sml29623 		"full pkt_buf_addr_pp $%p l2_len %d",
22613859Sml29623 		msg_index, rcr_entry, pkt_buf_addr_pp, l2_len));
22623859Sml29623 
22633859Sml29623 	rx_msg_p = rx_msg_ring_p[msg_index];
22643859Sml29623 
22653859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
22663859Sml29623 		"==> (rbr 4 msgindex %d) nxge_receive_packet: entry 0x%0llx "
22673859Sml29623 		"full pkt_buf_addr_pp $%p l2_len %d",
22683859Sml29623 		msg_index, rcr_entry, pkt_buf_addr_pp, l2_len));
22693859Sml29623 
22703859Sml29623 	switch (pktbufsz_type) {
22713859Sml29623 	case RCR_PKTBUFSZ_0:
22723859Sml29623 		bsize = rx_rbr_p->pkt_buf_size0_bytes;
22733859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX2_CTL,
22743859Sml29623 			"==> nxge_receive_packet: 0 buf %d", bsize));
22753859Sml29623 		break;
22763859Sml29623 	case RCR_PKTBUFSZ_1:
22773859Sml29623 		bsize = rx_rbr_p->pkt_buf_size1_bytes;
22783859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX2_CTL,
22793859Sml29623 			"==> nxge_receive_packet: 1 buf %d", bsize));
22803859Sml29623 		break;
22813859Sml29623 	case RCR_PKTBUFSZ_2:
22823859Sml29623 		bsize = rx_rbr_p->pkt_buf_size2_bytes;
22833859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
22843859Sml29623 			"==> nxge_receive_packet: 2 buf %d", bsize));
22853859Sml29623 		break;
22863859Sml29623 	case RCR_SINGLE_BLOCK:
22873859Sml29623 		bsize = rx_msg_p->block_size;
22883859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX2_CTL,
22893859Sml29623 			"==> nxge_receive_packet: single %d", bsize));
22903859Sml29623 
22913859Sml29623 		break;
22923859Sml29623 	default:
22933859Sml29623 		MUTEX_EXIT(&rx_rbr_p->lock);
22943859Sml29623 		MUTEX_EXIT(&rcr_p->lock);
22953859Sml29623 		return;
22963859Sml29623 	}
22973859Sml29623 
22983859Sml29623 	DMA_COMMON_SYNC_OFFSET(rx_msg_p->buf_dma,
22993859Sml29623 		(buf_offset + sw_offset_bytes),
23003859Sml29623 		(hdr_size + l2_len),
23013859Sml29623 		DDI_DMA_SYNC_FORCPU);
23023859Sml29623 
23033859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
23043859Sml29623 		"==> nxge_receive_packet: after first dump:usage count"));
23053859Sml29623 
23063859Sml29623 	if (rx_msg_p->cur_usage_cnt == 0) {
23073859Sml29623 		if (rx_rbr_p->rbr_use_bcopy) {
23083859Sml29623 			atomic_inc_32(&rx_rbr_p->rbr_consumed);
23093859Sml29623 			if (rx_rbr_p->rbr_consumed <
23103859Sml29623 					rx_rbr_p->rbr_threshold_hi) {
23113859Sml29623 				if (rx_rbr_p->rbr_threshold_lo == 0 ||
23123859Sml29623 					((rx_rbr_p->rbr_consumed >=
23133859Sml29623 						rx_rbr_p->rbr_threshold_lo) &&
23143859Sml29623 						(rx_rbr_p->rbr_bufsize_type >=
23153859Sml29623 							pktbufsz_type))) {
23163859Sml29623 					rx_msg_p->rx_use_bcopy = B_TRUE;
23173859Sml29623 				}
23183859Sml29623 			} else {
23193859Sml29623 				rx_msg_p->rx_use_bcopy = B_TRUE;
23203859Sml29623 			}
23213859Sml29623 		}
23223859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX2_CTL,
23233859Sml29623 			"==> nxge_receive_packet: buf %d (new block) ",
23243859Sml29623 			bsize));
23253859Sml29623 
23263859Sml29623 		rx_msg_p->pkt_buf_size_code = pktbufsz_type;
23273859Sml29623 		rx_msg_p->pkt_buf_size = bsize;
23283859Sml29623 		rx_msg_p->cur_usage_cnt = 1;
23293859Sml29623 		if (pktbufsz_type == RCR_SINGLE_BLOCK) {
23303859Sml29623 			NXGE_DEBUG_MSG((nxgep, RX2_CTL,
23313859Sml29623 				"==> nxge_receive_packet: buf %d "
23323859Sml29623 				"(single block) ",
23333859Sml29623 				bsize));
23343859Sml29623 			/*
23353859Sml29623 			 * Buffer can be reused once the free function
23363859Sml29623 			 * is called.
23373859Sml29623 			 */
23383859Sml29623 			rx_msg_p->max_usage_cnt = 1;
23393859Sml29623 			buffer_free = B_TRUE;
23403859Sml29623 		} else {
23413859Sml29623 			rx_msg_p->max_usage_cnt = rx_msg_p->block_size/bsize;
23423859Sml29623 			if (rx_msg_p->max_usage_cnt == 1) {
23433859Sml29623 				buffer_free = B_TRUE;
23443859Sml29623 			}
23453859Sml29623 		}
23463859Sml29623 	} else {
23473859Sml29623 		rx_msg_p->cur_usage_cnt++;
23483859Sml29623 		if (rx_msg_p->cur_usage_cnt == rx_msg_p->max_usage_cnt) {
23493859Sml29623 			buffer_free = B_TRUE;
23503859Sml29623 		}
23513859Sml29623 	}
23523859Sml29623 
23533859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
23543859Sml29623 	    "msgbuf index = %d l2len %d bytes usage %d max_usage %d ",
23553859Sml29623 		msg_index, l2_len,
23563859Sml29623 		rx_msg_p->cur_usage_cnt, rx_msg_p->max_usage_cnt));
23573859Sml29623 
2358*6028Ssbehera 	if ((error_type) || (dcf_err) || (pkt_too_long_err)) {
23593859Sml29623 		rdc_stats->ierrors++;
23603859Sml29623 		if (dcf_err) {
23613859Sml29623 			rdc_stats->dcf_err++;
23623859Sml29623 #ifdef	NXGE_DEBUG
23633859Sml29623 			if (!rdc_stats->dcf_err) {
23643859Sml29623 				NXGE_DEBUG_MSG((nxgep, RX_CTL,
23653859Sml29623 				"nxge_receive_packet: channel %d dcf_err rcr"
23663859Sml29623 				" 0x%llx", channel, rcr_entry));
23673859Sml29623 			}
23683859Sml29623 #endif
23693859Sml29623 			NXGE_FM_REPORT_ERROR(nxgep, nxgep->mac.portnum, NULL,
23703859Sml29623 					NXGE_FM_EREPORT_RDMC_DCF_ERR);
2371*6028Ssbehera 		} else if (pkt_too_long_err) {
2372*6028Ssbehera 			rdc_stats->pkt_too_long_err++;
2373*6028Ssbehera 			NXGE_DEBUG_MSG((nxgep, RX_CTL, " nxge_receive_packet:"
2374*6028Ssbehera 			    " channel %d packet length [%d] > "
2375*6028Ssbehera 			    "maxframesize [%d]", channel, l2_len + ETHERFCSL,
2376*6028Ssbehera 			    nxgep->mac.maxframesize));
23773859Sml29623 		} else {
23783859Sml29623 				/* Update error stats */
23793859Sml29623 			error_disp_cnt = NXGE_ERROR_SHOW_MAX;
23803859Sml29623 			rdc_stats->errlog.compl_err_type = error_type;
23813859Sml29623 
23823859Sml29623 			switch (error_type) {
23835523Syc148097 			/*
23845523Syc148097 			 * Do not send FMA ereport for RCR_L2_ERROR and
23855523Syc148097 			 * RCR_L4_CSUM_ERROR because most likely they indicate
23865523Syc148097 			 * back pressure rather than HW failures.
23875523Syc148097 			 */
23885165Syc148097 			case RCR_L2_ERROR:
23895165Syc148097 				rdc_stats->l2_err++;
23905165Syc148097 				if (rdc_stats->l2_err <
23915165Syc148097 				    error_disp_cnt) {
23925165Syc148097 					NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
23935165Syc148097 					    " nxge_receive_packet:"
23945165Syc148097 					    " channel %d RCR L2_ERROR",
23955165Syc148097 					    channel));
23965165Syc148097 				}
23975165Syc148097 				break;
23985165Syc148097 			case RCR_L4_CSUM_ERROR:
23995165Syc148097 				error_send_up = B_TRUE;
24005165Syc148097 				rdc_stats->l4_cksum_err++;
24015165Syc148097 				if (rdc_stats->l4_cksum_err <
24025165Syc148097 				    error_disp_cnt) {
24033859Sml29623 					NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
24045165Syc148097 					    " nxge_receive_packet:"
24055165Syc148097 					    " channel %d"
24065165Syc148097 					    " RCR L4_CSUM_ERROR", channel));
24075165Syc148097 				}
24085165Syc148097 				break;
24095523Syc148097 			/*
24105523Syc148097 			 * Do not send FMA ereport for RCR_FFLP_SOFT_ERROR and
24115523Syc148097 			 * RCR_ZCP_SOFT_ERROR because they reflect the same
24125523Syc148097 			 * FFLP and ZCP errors that have been reported by
24135523Syc148097 			 * nxge_fflp.c and nxge_zcp.c.
24145523Syc148097 			 */
24155165Syc148097 			case RCR_FFLP_SOFT_ERROR:
24165165Syc148097 				error_send_up = B_TRUE;
24175165Syc148097 				rdc_stats->fflp_soft_err++;
24185165Syc148097 				if (rdc_stats->fflp_soft_err <
24195165Syc148097 				    error_disp_cnt) {
24205165Syc148097 					NXGE_ERROR_MSG((nxgep,
24215165Syc148097 					    NXGE_ERR_CTL,
24225165Syc148097 					    " nxge_receive_packet:"
24235165Syc148097 					    " channel %d"
24245165Syc148097 					    " RCR FFLP_SOFT_ERROR", channel));
24255165Syc148097 				}
24265165Syc148097 				break;
24275165Syc148097 			case RCR_ZCP_SOFT_ERROR:
24285165Syc148097 				error_send_up = B_TRUE;
24295165Syc148097 				rdc_stats->fflp_soft_err++;
24305165Syc148097 				if (rdc_stats->zcp_soft_err <
24315165Syc148097 				    error_disp_cnt)
24325165Syc148097 					NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
24335165Syc148097 					    " nxge_receive_packet: Channel %d"
24345165Syc148097 					    " RCR ZCP_SOFT_ERROR", channel));
24355165Syc148097 				break;
24365165Syc148097 			default:
24375165Syc148097 				rdc_stats->rcr_unknown_err++;
24385165Syc148097 				if (rdc_stats->rcr_unknown_err
24395165Syc148097 				    < error_disp_cnt) {
24405165Syc148097 					NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
24415165Syc148097 					    " nxge_receive_packet: Channel %d"
24425165Syc148097 					    " RCR entry 0x%llx error 0x%x",
24435165Syc148097 					    rcr_entry, channel, error_type));
24445165Syc148097 				}
24455165Syc148097 				break;
24463859Sml29623 			}
24473859Sml29623 		}
24483859Sml29623 
24493859Sml29623 		/*
24503859Sml29623 		 * Update and repost buffer block if max usage
24513859Sml29623 		 * count is reached.
24523859Sml29623 		 */
24533859Sml29623 		if (error_send_up == B_FALSE) {
24544874Sml29623 			atomic_inc_32(&rx_msg_p->ref_cnt);
24553859Sml29623 			if (buffer_free == B_TRUE) {
24563859Sml29623 				rx_msg_p->free = B_TRUE;
24573859Sml29623 			}
24583859Sml29623 
24593859Sml29623 			MUTEX_EXIT(&rx_rbr_p->lock);
24603859Sml29623 			MUTEX_EXIT(&rcr_p->lock);
24613859Sml29623 			nxge_freeb(rx_msg_p);
24623859Sml29623 			return;
24633859Sml29623 		}
24643859Sml29623 	}
24653859Sml29623 
24663859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
24673859Sml29623 		"==> nxge_receive_packet: DMA sync second "));
24683859Sml29623 
24695165Syc148097 	bytes_read = rcr_p->rcvd_pkt_bytes;
24703859Sml29623 	skip_len = sw_offset_bytes + hdr_size;
24713859Sml29623 	if (!rx_msg_p->rx_use_bcopy) {
24724874Sml29623 		/*
24734874Sml29623 		 * For loaned up buffers, the driver reference count
24744874Sml29623 		 * will be incremented first and then the free state.
24754874Sml29623 		 */
24765165Syc148097 		if ((nmp = nxge_dupb(rx_msg_p, buf_offset, bsize)) != NULL) {
24775165Syc148097 			if (first_entry) {
24785165Syc148097 				nmp->b_rptr = &nmp->b_rptr[skip_len];
24795165Syc148097 				if (l2_len < bsize - skip_len) {
24805165Syc148097 					nmp->b_wptr = &nmp->b_rptr[l2_len];
24815165Syc148097 				} else {
24825165Syc148097 					nmp->b_wptr = &nmp->b_rptr[bsize
24835165Syc148097 					    - skip_len];
24845165Syc148097 				}
24855165Syc148097 			} else {
24865165Syc148097 				if (l2_len - bytes_read < bsize) {
24875165Syc148097 					nmp->b_wptr =
24885165Syc148097 					    &nmp->b_rptr[l2_len - bytes_read];
24895165Syc148097 				} else {
24905165Syc148097 					nmp->b_wptr = &nmp->b_rptr[bsize];
24915165Syc148097 				}
24925165Syc148097 			}
24935165Syc148097 		}
24943859Sml29623 	} else {
24955165Syc148097 		if (first_entry) {
24965165Syc148097 			nmp = nxge_dupb_bcopy(rx_msg_p, buf_offset + skip_len,
24975165Syc148097 			    l2_len < bsize - skip_len ?
24985165Syc148097 			    l2_len : bsize - skip_len);
24995165Syc148097 		} else {
25005165Syc148097 			nmp = nxge_dupb_bcopy(rx_msg_p, buf_offset,
25015165Syc148097 			    l2_len - bytes_read < bsize ?
25025165Syc148097 			    l2_len - bytes_read : bsize);
25035165Syc148097 		}
25043859Sml29623 	}
25053859Sml29623 	if (nmp != NULL) {
25065165Syc148097 		if (first_entry)
25075165Syc148097 			bytes_read  = nmp->b_wptr - nmp->b_rptr;
25085165Syc148097 		else
25093859Sml29623 			bytes_read += nmp->b_wptr - nmp->b_rptr;
25105165Syc148097 
25115165Syc148097 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
25125165Syc148097 		    "==> nxge_receive_packet after dupb: "
25135165Syc148097 		    "rbr consumed %d "
25145165Syc148097 		    "pktbufsz_type %d "
25155165Syc148097 		    "nmp $%p rptr $%p wptr $%p "
25165165Syc148097 		    "buf_offset %d bzise %d l2_len %d skip_len %d",
25175165Syc148097 		    rx_rbr_p->rbr_consumed,
25185165Syc148097 		    pktbufsz_type,
25195165Syc148097 		    nmp, nmp->b_rptr, nmp->b_wptr,
25205165Syc148097 		    buf_offset, bsize, l2_len, skip_len));
25213859Sml29623 	} else {
25223859Sml29623 		cmn_err(CE_WARN, "!nxge_receive_packet: "
25233859Sml29623 			"update stats (error)");
25244977Sraghus 		atomic_inc_32(&rx_msg_p->ref_cnt);
25254977Sraghus 		if (buffer_free == B_TRUE) {
25264977Sraghus 			rx_msg_p->free = B_TRUE;
25274977Sraghus 		}
25284977Sraghus 		MUTEX_EXIT(&rx_rbr_p->lock);
25294977Sraghus 		MUTEX_EXIT(&rcr_p->lock);
25304977Sraghus 		nxge_freeb(rx_msg_p);
25314977Sraghus 		return;
25323859Sml29623 	}
25335060Syc148097 
25343859Sml29623 	if (buffer_free == B_TRUE) {
25353859Sml29623 		rx_msg_p->free = B_TRUE;
25363859Sml29623 	}
25373859Sml29623 	/*
25383859Sml29623 	 * ERROR, FRAG and PKT_TYPE are only reported
25393859Sml29623 	 * in the first entry.
25403859Sml29623 	 * If a packet is not fragmented and no error bit is set, then
25413859Sml29623 	 * L4 checksum is OK.
25423859Sml29623 	 */
25433859Sml29623 	is_valid = (nmp != NULL);
25445165Syc148097 	if (first_entry) {
25455165Syc148097 		rdc_stats->ipackets++; /* count only 1st seg for jumbo */
25465165Syc148097 		rdc_stats->ibytes += skip_len + l2_len < bsize ?
25475183Syc148097 		    l2_len : bsize;
25485165Syc148097 	} else {
25495165Syc148097 		rdc_stats->ibytes += l2_len - bytes_read < bsize ?
25505165Syc148097 		    l2_len - bytes_read : bsize;
25515165Syc148097 	}
25525165Syc148097 
25535165Syc148097 	rcr_p->rcvd_pkt_bytes = bytes_read;
25545165Syc148097 
25553859Sml29623 	MUTEX_EXIT(&rx_rbr_p->lock);
25563859Sml29623 	MUTEX_EXIT(&rcr_p->lock);
25573859Sml29623 
25583859Sml29623 	if (rx_msg_p->free && rx_msg_p->rx_use_bcopy) {
25593859Sml29623 		atomic_inc_32(&rx_msg_p->ref_cnt);
25603859Sml29623 		nxge_freeb(rx_msg_p);
25613859Sml29623 	}
25623859Sml29623 
25633859Sml29623 	if (is_valid) {
25643859Sml29623 		nmp->b_cont = NULL;
25653859Sml29623 		if (first_entry) {
25663859Sml29623 			*mp = nmp;
25673859Sml29623 			*mp_cont = NULL;
25685165Syc148097 		} else {
25693859Sml29623 			*mp_cont = nmp;
25705165Syc148097 		}
25713859Sml29623 	}
25723859Sml29623 
25733859Sml29623 	/*
25743859Sml29623 	 * Update stats and hardware checksuming.
25753859Sml29623 	 */
25763859Sml29623 	if (is_valid && !multi) {
25773859Sml29623 
25783859Sml29623 		is_tcp_udp = ((pkt_type == RCR_PKT_IS_TCP ||
25793859Sml29623 				pkt_type == RCR_PKT_IS_UDP) ?
25803859Sml29623 					B_TRUE: B_FALSE);
25813859Sml29623 
25823859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL, "==> nxge_receive_packet: "
25833859Sml29623 			"is_valid 0x%x multi 0x%llx pkt %d frag %d error %d",
25843859Sml29623 			is_valid, multi, is_tcp_udp, frag, error_type));
25853859Sml29623 
25863859Sml29623 		if (is_tcp_udp && !frag && !error_type) {
25873859Sml29623 			(void) hcksum_assoc(nmp, NULL, NULL, 0, 0, 0, 0,
25883859Sml29623 				HCK_FULLCKSUM_OK | HCK_FULLCKSUM, 0);
25893859Sml29623 			NXGE_DEBUG_MSG((nxgep, RX_CTL,
25903859Sml29623 				"==> nxge_receive_packet: Full tcp/udp cksum "
25913859Sml29623 				"is_valid 0x%x multi 0x%llx pkt %d frag %d "
25923859Sml29623 				"error %d",
25933859Sml29623 				is_valid, multi, is_tcp_udp, frag, error_type));
25943859Sml29623 		}
25953859Sml29623 	}
25963859Sml29623 
25973859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL,
25983859Sml29623 		"==> nxge_receive_packet: *mp 0x%016llx", *mp));
25993859Sml29623 
26003859Sml29623 	*multi_p = (multi == RCR_MULTI_MASK);
26013859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "<== nxge_receive_packet: "
26023859Sml29623 		"multi %d nmp 0x%016llx *mp 0x%016llx *mp_cont 0x%016llx",
26033859Sml29623 		*multi_p, nmp, *mp, *mp_cont));
26043859Sml29623 }
26053859Sml29623 
26063859Sml29623 /*ARGSUSED*/
26073859Sml29623 static nxge_status_t
26083859Sml29623 nxge_rx_err_evnts(p_nxge_t nxgep, uint_t index, p_nxge_ldv_t ldvp,
26093859Sml29623 						rx_dma_ctl_stat_t cs)
26103859Sml29623 {
26113859Sml29623 	p_nxge_rx_ring_stats_t	rdc_stats;
26123859Sml29623 	npi_handle_t		handle;
26133859Sml29623 	npi_status_t		rs;
26143859Sml29623 	boolean_t		rxchan_fatal = B_FALSE;
26153859Sml29623 	boolean_t		rxport_fatal = B_FALSE;
26163859Sml29623 	uint8_t			channel;
26173859Sml29623 	uint8_t			portn;
26183859Sml29623 	nxge_status_t		status = NXGE_OK;
26193859Sml29623 	uint32_t		error_disp_cnt = NXGE_ERROR_SHOW_MAX;
26203859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_rx_err_evnts"));
26213859Sml29623 
26223859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
26233859Sml29623 	channel = ldvp->channel;
26243859Sml29623 	portn = nxgep->mac.portnum;
26253859Sml29623 	rdc_stats = &nxgep->statsp->rdc_stats[ldvp->vdma_index];
26263859Sml29623 
26273859Sml29623 	if (cs.bits.hdw.rbr_tmout) {
26283859Sml29623 		rdc_stats->rx_rbr_tmout++;
26293859Sml29623 		NXGE_FM_REPORT_ERROR(nxgep, portn, channel,
26303859Sml29623 					NXGE_FM_EREPORT_RDMC_RBR_TMOUT);
26313859Sml29623 		rxchan_fatal = B_TRUE;
26323859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
26333859Sml29623 			"==> nxge_rx_err_evnts: rx_rbr_timeout"));
26343859Sml29623 	}
26353859Sml29623 	if (cs.bits.hdw.rsp_cnt_err) {
26363859Sml29623 		rdc_stats->rsp_cnt_err++;
26373859Sml29623 		NXGE_FM_REPORT_ERROR(nxgep, portn, channel,
26383859Sml29623 					NXGE_FM_EREPORT_RDMC_RSP_CNT_ERR);
26393859Sml29623 		rxchan_fatal = B_TRUE;
26403859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
26413859Sml29623 			"==> nxge_rx_err_evnts(channel %d): "
26423859Sml29623 			"rsp_cnt_err", channel));
26433859Sml29623 	}
26443859Sml29623 	if (cs.bits.hdw.byte_en_bus) {
26453859Sml29623 		rdc_stats->byte_en_bus++;
26463859Sml29623 		NXGE_FM_REPORT_ERROR(nxgep, portn, channel,
26473859Sml29623 					NXGE_FM_EREPORT_RDMC_BYTE_EN_BUS);
26483859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
26493859Sml29623 			"==> nxge_rx_err_evnts(channel %d): "
26503859Sml29623 			"fatal error: byte_en_bus", channel));
26513859Sml29623 		rxchan_fatal = B_TRUE;
26523859Sml29623 	}
26533859Sml29623 	if (cs.bits.hdw.rsp_dat_err) {
26543859Sml29623 		rdc_stats->rsp_dat_err++;
26553859Sml29623 		NXGE_FM_REPORT_ERROR(nxgep, portn, channel,
26563859Sml29623 					NXGE_FM_EREPORT_RDMC_RSP_DAT_ERR);
26573859Sml29623 		rxchan_fatal = B_TRUE;
26583859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
26593859Sml29623 			"==> nxge_rx_err_evnts(channel %d): "
26603859Sml29623 			"fatal error: rsp_dat_err", channel));
26613859Sml29623 	}
26623859Sml29623 	if (cs.bits.hdw.rcr_ack_err) {
26633859Sml29623 		rdc_stats->rcr_ack_err++;
26643859Sml29623 		NXGE_FM_REPORT_ERROR(nxgep, portn, channel,
26653859Sml29623 					NXGE_FM_EREPORT_RDMC_RCR_ACK_ERR);
26663859Sml29623 		rxchan_fatal = B_TRUE;
26673859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
26683859Sml29623 			"==> nxge_rx_err_evnts(channel %d): "
26693859Sml29623 			"fatal error: rcr_ack_err", channel));
26703859Sml29623 	}
26713859Sml29623 	if (cs.bits.hdw.dc_fifo_err) {
26723859Sml29623 		rdc_stats->dc_fifo_err++;
26733859Sml29623 		NXGE_FM_REPORT_ERROR(nxgep, portn, channel,
26743859Sml29623 					NXGE_FM_EREPORT_RDMC_DC_FIFO_ERR);
26753859Sml29623 		/* This is not a fatal error! */
26763859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
26773859Sml29623 			"==> nxge_rx_err_evnts(channel %d): "
26783859Sml29623 			"dc_fifo_err", channel));
26793859Sml29623 		rxport_fatal = B_TRUE;
26803859Sml29623 	}
26813859Sml29623 	if ((cs.bits.hdw.rcr_sha_par) || (cs.bits.hdw.rbr_pre_par)) {
26823859Sml29623 		if ((rs = npi_rxdma_ring_perr_stat_get(handle,
26833859Sml29623 				&rdc_stats->errlog.pre_par,
26843859Sml29623 				&rdc_stats->errlog.sha_par))
26853859Sml29623 				!= NPI_SUCCESS) {
26863859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
26873859Sml29623 				"==> nxge_rx_err_evnts(channel %d): "
26883859Sml29623 				"rcr_sha_par: get perr", channel));
26893859Sml29623 			return (NXGE_ERROR | rs);
26903859Sml29623 		}
26913859Sml29623 		if (cs.bits.hdw.rcr_sha_par) {
26923859Sml29623 			rdc_stats->rcr_sha_par++;
26933859Sml29623 			NXGE_FM_REPORT_ERROR(nxgep, portn, channel,
26943859Sml29623 					NXGE_FM_EREPORT_RDMC_RCR_SHA_PAR);
26953859Sml29623 			rxchan_fatal = B_TRUE;
26963859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
26973859Sml29623 				"==> nxge_rx_err_evnts(channel %d): "
26983859Sml29623 				"fatal error: rcr_sha_par", channel));
26993859Sml29623 		}
27003859Sml29623 		if (cs.bits.hdw.rbr_pre_par) {
27013859Sml29623 			rdc_stats->rbr_pre_par++;
27023859Sml29623 			NXGE_FM_REPORT_ERROR(nxgep, portn, channel,
27033859Sml29623 					NXGE_FM_EREPORT_RDMC_RBR_PRE_PAR);
27043859Sml29623 			rxchan_fatal = B_TRUE;
27053859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
27063859Sml29623 				"==> nxge_rx_err_evnts(channel %d): "
27073859Sml29623 				"fatal error: rbr_pre_par", channel));
27083859Sml29623 		}
27093859Sml29623 	}
27103859Sml29623 	if (cs.bits.hdw.port_drop_pkt) {
27113859Sml29623 		rdc_stats->port_drop_pkt++;
27123859Sml29623 		if (rdc_stats->port_drop_pkt < error_disp_cnt)
27133859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
27143859Sml29623 			"==> nxge_rx_err_evnts (channel %d): "
27153859Sml29623 			"port_drop_pkt", channel));
27163859Sml29623 	}
27173859Sml29623 	if (cs.bits.hdw.wred_drop) {
27183859Sml29623 		rdc_stats->wred_drop++;
27193859Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
27203859Sml29623 			"==> nxge_rx_err_evnts(channel %d): "
27213859Sml29623 		"wred_drop", channel));
27223859Sml29623 	}
27233859Sml29623 	if (cs.bits.hdw.rbr_pre_empty) {
27243859Sml29623 		rdc_stats->rbr_pre_empty++;
27253859Sml29623 		if (rdc_stats->rbr_pre_empty < error_disp_cnt)
27263859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
27273859Sml29623 			"==> nxge_rx_err_evnts(channel %d): "
27283859Sml29623 			"rbr_pre_empty", channel));
27293859Sml29623 	}
27303859Sml29623 	if (cs.bits.hdw.rcr_shadow_full) {
27313859Sml29623 		rdc_stats->rcr_shadow_full++;
27323859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
27333859Sml29623 			"==> nxge_rx_err_evnts(channel %d): "
27343859Sml29623 			"rcr_shadow_full", channel));
27353859Sml29623 	}
27363859Sml29623 	if (cs.bits.hdw.config_err) {
27373859Sml29623 		rdc_stats->config_err++;
27383859Sml29623 		NXGE_FM_REPORT_ERROR(nxgep, portn, channel,
27393859Sml29623 					NXGE_FM_EREPORT_RDMC_CONFIG_ERR);
27403859Sml29623 		rxchan_fatal = B_TRUE;
27413859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
27423859Sml29623 			"==> nxge_rx_err_evnts(channel %d): "
27433859Sml29623 			"config error", channel));
27443859Sml29623 	}
27453859Sml29623 	if (cs.bits.hdw.rcrincon) {
27463859Sml29623 		rdc_stats->rcrincon++;
27473859Sml29623 		NXGE_FM_REPORT_ERROR(nxgep, portn, channel,
27483859Sml29623 					NXGE_FM_EREPORT_RDMC_RCRINCON);
27493859Sml29623 		rxchan_fatal = B_TRUE;
27503859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
27513859Sml29623 			"==> nxge_rx_err_evnts(channel %d): "
27523859Sml29623 			"fatal error: rcrincon error", channel));
27533859Sml29623 	}
27543859Sml29623 	if (cs.bits.hdw.rcrfull) {
27553859Sml29623 		rdc_stats->rcrfull++;
27563859Sml29623 		NXGE_FM_REPORT_ERROR(nxgep, portn, channel,
27573859Sml29623 					NXGE_FM_EREPORT_RDMC_RCRFULL);
27583859Sml29623 		rxchan_fatal = B_TRUE;
27593859Sml29623 		if (rdc_stats->rcrfull < error_disp_cnt)
27603859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
27613859Sml29623 			"==> nxge_rx_err_evnts(channel %d): "
27623859Sml29623 			"fatal error: rcrfull error", channel));
27633859Sml29623 	}
27643859Sml29623 	if (cs.bits.hdw.rbr_empty) {
27653859Sml29623 		rdc_stats->rbr_empty++;
27663859Sml29623 		if (rdc_stats->rbr_empty < error_disp_cnt)
27673859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
27683859Sml29623 			"==> nxge_rx_err_evnts(channel %d): "
27693859Sml29623 			"rbr empty error", channel));
27703859Sml29623 	}
27713859Sml29623 	if (cs.bits.hdw.rbrfull) {
27723859Sml29623 		rdc_stats->rbrfull++;
27733859Sml29623 		NXGE_FM_REPORT_ERROR(nxgep, portn, channel,
27743859Sml29623 					NXGE_FM_EREPORT_RDMC_RBRFULL);
27753859Sml29623 		rxchan_fatal = B_TRUE;
27763859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
27773859Sml29623 			"==> nxge_rx_err_evnts(channel %d): "
27783859Sml29623 			"fatal error: rbr_full error", channel));
27793859Sml29623 	}
27803859Sml29623 	if (cs.bits.hdw.rbrlogpage) {
27813859Sml29623 		rdc_stats->rbrlogpage++;
27823859Sml29623 		NXGE_FM_REPORT_ERROR(nxgep, portn, channel,
27833859Sml29623 					NXGE_FM_EREPORT_RDMC_RBRLOGPAGE);
27843859Sml29623 		rxchan_fatal = B_TRUE;
27853859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
27863859Sml29623 			"==> nxge_rx_err_evnts(channel %d): "
27873859Sml29623 			"fatal error: rbr logical page error", channel));
27883859Sml29623 	}
27893859Sml29623 	if (cs.bits.hdw.cfiglogpage) {
27903859Sml29623 		rdc_stats->cfiglogpage++;
27913859Sml29623 		NXGE_FM_REPORT_ERROR(nxgep, portn, channel,
27923859Sml29623 					NXGE_FM_EREPORT_RDMC_CFIGLOGPAGE);
27933859Sml29623 		rxchan_fatal = B_TRUE;
27943859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
27953859Sml29623 			"==> nxge_rx_err_evnts(channel %d): "
27963859Sml29623 			"fatal error: cfig logical page error", channel));
27973859Sml29623 	}
27983859Sml29623 
27993859Sml29623 	if (rxport_fatal)  {
28003859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
28013859Sml29623 				" nxge_rx_err_evnts: "
28023859Sml29623 				" fatal error on Port #%d\n",
28033859Sml29623 				portn));
28043859Sml29623 		status = nxge_ipp_fatal_err_recover(nxgep);
28053859Sml29623 		if (status == NXGE_OK) {
28063859Sml29623 			FM_SERVICE_RESTORED(nxgep);
28073859Sml29623 		}
28083859Sml29623 	}
28093859Sml29623 
28103859Sml29623 	if (rxchan_fatal) {
28113859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
28123859Sml29623 				" nxge_rx_err_evnts: "
28133859Sml29623 				" fatal error on Channel #%d\n",
28143859Sml29623 				channel));
28153859Sml29623 		status = nxge_rxdma_fatal_err_recover(nxgep, channel);
28163859Sml29623 		if (status == NXGE_OK) {
28173859Sml29623 			FM_SERVICE_RESTORED(nxgep);
28183859Sml29623 		}
28193859Sml29623 	}
28203859Sml29623 
28213859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX2_CTL, "<== nxge_rx_err_evnts"));
28223859Sml29623 
28233859Sml29623 	return (status);
28243859Sml29623 }
28253859Sml29623 
28263859Sml29623 static nxge_status_t
28273859Sml29623 nxge_map_rxdma(p_nxge_t nxgep)
28283859Sml29623 {
28293859Sml29623 	int			i, ndmas;
28303859Sml29623 	uint16_t		channel;
28313859Sml29623 	p_rx_rbr_rings_t 	rx_rbr_rings;
28323859Sml29623 	p_rx_rbr_ring_t		*rbr_rings;
28333859Sml29623 	p_rx_rcr_rings_t 	rx_rcr_rings;
28343859Sml29623 	p_rx_rcr_ring_t		*rcr_rings;
28353859Sml29623 	p_rx_mbox_areas_t 	rx_mbox_areas_p;
28363859Sml29623 	p_rx_mbox_t		*rx_mbox_p;
28373859Sml29623 	p_nxge_dma_pool_t	dma_buf_poolp;
28383859Sml29623 	p_nxge_dma_pool_t	dma_cntl_poolp;
28393859Sml29623 	p_nxge_dma_common_t	*dma_buf_p;
28403859Sml29623 	p_nxge_dma_common_t	*dma_cntl_p;
28413859Sml29623 	uint32_t		*num_chunks;
28423859Sml29623 	nxge_status_t		status = NXGE_OK;
28433859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
28443859Sml29623 	p_nxge_dma_common_t	t_dma_buf_p;
28453859Sml29623 	p_nxge_dma_common_t	t_dma_cntl_p;
28463859Sml29623 #endif
28473859Sml29623 
28483859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_map_rxdma"));
28493859Sml29623 
28503859Sml29623 	dma_buf_poolp = nxgep->rx_buf_pool_p;
28513859Sml29623 	dma_cntl_poolp = nxgep->rx_cntl_pool_p;
28523859Sml29623 
28533859Sml29623 	if (!dma_buf_poolp->buf_allocated || !dma_cntl_poolp->buf_allocated) {
28543859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
28553859Sml29623 			"<== nxge_map_rxdma: buf not allocated"));
28563859Sml29623 		return (NXGE_ERROR);
28573859Sml29623 	}
28583859Sml29623 
28593859Sml29623 	ndmas = dma_buf_poolp->ndmas;
28603859Sml29623 	if (!ndmas) {
28613859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
28623859Sml29623 			"<== nxge_map_rxdma: no dma allocated"));
28633859Sml29623 		return (NXGE_ERROR);
28643859Sml29623 	}
28653859Sml29623 
28663859Sml29623 	num_chunks = dma_buf_poolp->num_chunks;
28673859Sml29623 	dma_buf_p = dma_buf_poolp->dma_buf_pool_p;
28683859Sml29623 	dma_cntl_p = dma_cntl_poolp->dma_buf_pool_p;
28693859Sml29623 
28703859Sml29623 	rx_rbr_rings = (p_rx_rbr_rings_t)
28713859Sml29623 		KMEM_ZALLOC(sizeof (rx_rbr_rings_t), KM_SLEEP);
28723859Sml29623 	rbr_rings = (p_rx_rbr_ring_t *)
28733859Sml29623 		KMEM_ZALLOC(sizeof (p_rx_rbr_ring_t) * ndmas, KM_SLEEP);
28743859Sml29623 	rx_rcr_rings = (p_rx_rcr_rings_t)
28753859Sml29623 		KMEM_ZALLOC(sizeof (rx_rcr_rings_t), KM_SLEEP);
28763859Sml29623 	rcr_rings = (p_rx_rcr_ring_t *)
28773859Sml29623 		KMEM_ZALLOC(sizeof (p_rx_rcr_ring_t) * ndmas, KM_SLEEP);
28783859Sml29623 	rx_mbox_areas_p = (p_rx_mbox_areas_t)
28793859Sml29623 		KMEM_ZALLOC(sizeof (rx_mbox_areas_t), KM_SLEEP);
28803859Sml29623 	rx_mbox_p = (p_rx_mbox_t *)
28813859Sml29623 		KMEM_ZALLOC(sizeof (p_rx_mbox_t) * ndmas, KM_SLEEP);
28823859Sml29623 
28833859Sml29623 	/*
28843859Sml29623 	 * Timeout should be set based on the system clock divider.
28853859Sml29623 	 * The following timeout value of 1 assumes that the
28863859Sml29623 	 * granularity (1000) is 3 microseconds running at 300MHz.
28873859Sml29623 	 */
28883859Sml29623 
28893859Sml29623 	nxgep->intr_threshold = RXDMA_RCR_PTHRES_DEFAULT;
28903859Sml29623 	nxgep->intr_timeout = RXDMA_RCR_TO_DEFAULT;
28913859Sml29623 
28923859Sml29623 	/*
28933859Sml29623 	 * Map descriptors from the buffer polls for each dam channel.
28943859Sml29623 	 */
28953859Sml29623 	for (i = 0; i < ndmas; i++) {
28963859Sml29623 		/*
28973859Sml29623 		 * Set up and prepare buffer blocks, descriptors
28983859Sml29623 		 * and mailbox.
28993859Sml29623 		 */
29003859Sml29623 		channel = ((p_nxge_dma_common_t)dma_buf_p[i])->dma_channel;
29013859Sml29623 		status = nxge_map_rxdma_channel(nxgep, channel,
29023859Sml29623 				(p_nxge_dma_common_t *)&dma_buf_p[i],
29033859Sml29623 				(p_rx_rbr_ring_t *)&rbr_rings[i],
29043859Sml29623 				num_chunks[i],
29053859Sml29623 				(p_nxge_dma_common_t *)&dma_cntl_p[i],
29063859Sml29623 				(p_rx_rcr_ring_t *)&rcr_rings[i],
29073859Sml29623 				(p_rx_mbox_t *)&rx_mbox_p[i]);
29083859Sml29623 		if (status != NXGE_OK) {
29093859Sml29623 			goto nxge_map_rxdma_fail1;
29103859Sml29623 		}
29113859Sml29623 		rbr_rings[i]->index = (uint16_t)i;
29123859Sml29623 		rcr_rings[i]->index = (uint16_t)i;
29133859Sml29623 		rcr_rings[i]->rdc_stats = &nxgep->statsp->rdc_stats[i];
29143859Sml29623 
29153859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
29163859Sml29623 		if (nxgep->niu_type == N2_NIU && NXGE_DMA_BLOCK == 1) {
29173859Sml29623 			rbr_rings[i]->hv_set = B_FALSE;
29183859Sml29623 			t_dma_buf_p = (p_nxge_dma_common_t)dma_buf_p[i];
29193859Sml29623 			t_dma_cntl_p =
29203859Sml29623 				(p_nxge_dma_common_t)dma_cntl_p[i];
29213859Sml29623 
29223859Sml29623 			rbr_rings[i]->hv_rx_buf_base_ioaddr_pp =
29233859Sml29623 				(uint64_t)t_dma_buf_p->orig_ioaddr_pp;
29243859Sml29623 			rbr_rings[i]->hv_rx_buf_ioaddr_size =
29253859Sml29623 				(uint64_t)t_dma_buf_p->orig_alength;
29263859Sml29623 			NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
29273859Sml29623 				"==> nxge_map_rxdma_channel: "
29283859Sml29623 				"channel %d "
29293859Sml29623 				"data buf base io $%p ($%p) "
29303859Sml29623 				"size 0x%llx (%d 0x%x)",
29313859Sml29623 				channel,
29323859Sml29623 				rbr_rings[i]->hv_rx_buf_base_ioaddr_pp,
29333859Sml29623 				t_dma_cntl_p->ioaddr_pp,
29343859Sml29623 				rbr_rings[i]->hv_rx_buf_ioaddr_size,
29353859Sml29623 				t_dma_buf_p->orig_alength,
29363859Sml29623 				t_dma_buf_p->orig_alength));
29373859Sml29623 
29383859Sml29623 			rbr_rings[i]->hv_rx_cntl_base_ioaddr_pp =
29393859Sml29623 				(uint64_t)t_dma_cntl_p->orig_ioaddr_pp;
29403859Sml29623 			rbr_rings[i]->hv_rx_cntl_ioaddr_size =
29413859Sml29623 				(uint64_t)t_dma_cntl_p->orig_alength;
29423859Sml29623 			NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
29433859Sml29623 				"==> nxge_map_rxdma_channel: "
29443859Sml29623 				"channel %d "
29453859Sml29623 				"cntl base io $%p ($%p) "
29463859Sml29623 				"size 0x%llx (%d 0x%x)",
29473859Sml29623 				channel,
29483859Sml29623 				rbr_rings[i]->hv_rx_cntl_base_ioaddr_pp,
29493859Sml29623 				t_dma_cntl_p->ioaddr_pp,
29503859Sml29623 				rbr_rings[i]->hv_rx_cntl_ioaddr_size,
29513859Sml29623 				t_dma_cntl_p->orig_alength,
29523859Sml29623 				t_dma_cntl_p->orig_alength));
29533859Sml29623 		}
29543859Sml29623 
29553859Sml29623 #endif	/* sun4v and NIU_LP_WORKAROUND */
29563859Sml29623 	}
29573859Sml29623 
29583859Sml29623 	rx_rbr_rings->ndmas = rx_rcr_rings->ndmas = ndmas;
29593859Sml29623 	rx_rbr_rings->rbr_rings = rbr_rings;
29603859Sml29623 	nxgep->rx_rbr_rings = rx_rbr_rings;
29613859Sml29623 	rx_rcr_rings->rcr_rings = rcr_rings;
29623859Sml29623 	nxgep->rx_rcr_rings = rx_rcr_rings;
29633859Sml29623 
29643859Sml29623 	rx_mbox_areas_p->rxmbox_areas = rx_mbox_p;
29653859Sml29623 	nxgep->rx_mbox_areas_p = rx_mbox_areas_p;
29663859Sml29623 
29673859Sml29623 	goto nxge_map_rxdma_exit;
29683859Sml29623 
29693859Sml29623 nxge_map_rxdma_fail1:
29703859Sml29623 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
29713859Sml29623 		"==> nxge_map_rxdma: unmap rbr,rcr "
29723859Sml29623 		"(status 0x%x channel %d i %d)",
29733859Sml29623 		status, channel, i));
29744185Sspeer 	i--;
29753859Sml29623 	for (; i >= 0; i--) {
29763859Sml29623 		channel = ((p_nxge_dma_common_t)dma_buf_p[i])->dma_channel;
29773859Sml29623 		nxge_unmap_rxdma_channel(nxgep, channel,
29783859Sml29623 			rbr_rings[i],
29793859Sml29623 			rcr_rings[i],
29803859Sml29623 			rx_mbox_p[i]);
29813859Sml29623 	}
29823859Sml29623 
29833859Sml29623 	KMEM_FREE(rbr_rings, sizeof (p_rx_rbr_ring_t) * ndmas);
29843859Sml29623 	KMEM_FREE(rx_rbr_rings, sizeof (rx_rbr_rings_t));
29853859Sml29623 	KMEM_FREE(rcr_rings, sizeof (p_rx_rcr_ring_t) * ndmas);
29863859Sml29623 	KMEM_FREE(rx_rcr_rings, sizeof (rx_rcr_rings_t));
29873859Sml29623 	KMEM_FREE(rx_mbox_p, sizeof (p_rx_mbox_t) * ndmas);
29883859Sml29623 	KMEM_FREE(rx_mbox_areas_p, sizeof (rx_mbox_areas_t));
29893859Sml29623 
29903859Sml29623 nxge_map_rxdma_exit:
29913859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
29923859Sml29623 		"<== nxge_map_rxdma: "
29933859Sml29623 		"(status 0x%x channel %d)",
29943859Sml29623 		status, channel));
29953859Sml29623 
29963859Sml29623 	return (status);
29973859Sml29623 }
29983859Sml29623 
29993859Sml29623 static void
30003859Sml29623 nxge_unmap_rxdma(p_nxge_t nxgep)
30013859Sml29623 {
30023859Sml29623 	int			i, ndmas;
30033859Sml29623 	uint16_t		channel;
30043859Sml29623 	p_rx_rbr_rings_t 	rx_rbr_rings;
30053859Sml29623 	p_rx_rbr_ring_t		*rbr_rings;
30063859Sml29623 	p_rx_rcr_rings_t 	rx_rcr_rings;
30073859Sml29623 	p_rx_rcr_ring_t		*rcr_rings;
30083859Sml29623 	p_rx_mbox_areas_t 	rx_mbox_areas_p;
30093859Sml29623 	p_rx_mbox_t		*rx_mbox_p;
30103859Sml29623 	p_nxge_dma_pool_t	dma_buf_poolp;
30113859Sml29623 	p_nxge_dma_pool_t	dma_cntl_poolp;
30123859Sml29623 	p_nxge_dma_common_t	*dma_buf_p;
30133859Sml29623 
30143859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_unmap_rxdma"));
30153859Sml29623 
30163859Sml29623 	dma_buf_poolp = nxgep->rx_buf_pool_p;
30173859Sml29623 	dma_cntl_poolp = nxgep->rx_cntl_pool_p;
30183859Sml29623 
30193859Sml29623 	if (!dma_buf_poolp->buf_allocated || !dma_cntl_poolp->buf_allocated) {
30203859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
30213859Sml29623 			"<== nxge_unmap_rxdma: NULL buf pointers"));
30223859Sml29623 		return;
30233859Sml29623 	}
30243859Sml29623 
30253859Sml29623 	rx_rbr_rings = nxgep->rx_rbr_rings;
30263859Sml29623 	rx_rcr_rings = nxgep->rx_rcr_rings;
30273859Sml29623 	if (rx_rbr_rings == NULL || rx_rcr_rings == NULL) {
30283859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
30293859Sml29623 			"<== nxge_unmap_rxdma: NULL ring pointers"));
30303859Sml29623 		return;
30313859Sml29623 	}
30323859Sml29623 	ndmas = rx_rbr_rings->ndmas;
30333859Sml29623 	if (!ndmas) {
30343859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
30353859Sml29623 			"<== nxge_unmap_rxdma: no channel"));
30363859Sml29623 		return;
30373859Sml29623 	}
30383859Sml29623 
30393859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
30403859Sml29623 		"==> nxge_unmap_rxdma (ndmas %d)", ndmas));
30413859Sml29623 	rbr_rings = rx_rbr_rings->rbr_rings;
30423859Sml29623 	rcr_rings = rx_rcr_rings->rcr_rings;
30433859Sml29623 	rx_mbox_areas_p = nxgep->rx_mbox_areas_p;
30443859Sml29623 	rx_mbox_p = rx_mbox_areas_p->rxmbox_areas;
30453859Sml29623 	dma_buf_p = dma_buf_poolp->dma_buf_pool_p;
30463859Sml29623 
30473859Sml29623 	for (i = 0; i < ndmas; i++) {
30483859Sml29623 		channel = ((p_nxge_dma_common_t)dma_buf_p[i])->dma_channel;
30493859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
30503859Sml29623 			"==> nxge_unmap_rxdma (ndmas %d) channel %d",
30513859Sml29623 				ndmas, channel));
30523859Sml29623 		(void) nxge_unmap_rxdma_channel(nxgep, channel,
30533859Sml29623 				(p_rx_rbr_ring_t)rbr_rings[i],
30543859Sml29623 				(p_rx_rcr_ring_t)rcr_rings[i],
30553859Sml29623 				(p_rx_mbox_t)rx_mbox_p[i]);
30563859Sml29623 	}
30573859Sml29623 
30583859Sml29623 	KMEM_FREE(rx_rbr_rings, sizeof (rx_rbr_rings_t));
30593859Sml29623 	KMEM_FREE(rbr_rings, sizeof (p_rx_rbr_ring_t) * ndmas);
30603859Sml29623 	KMEM_FREE(rx_rcr_rings, sizeof (rx_rcr_rings_t));
30613859Sml29623 	KMEM_FREE(rcr_rings, sizeof (p_rx_rcr_ring_t) * ndmas);
30623859Sml29623 	KMEM_FREE(rx_mbox_areas_p, sizeof (rx_mbox_areas_t));
30633859Sml29623 	KMEM_FREE(rx_mbox_p, sizeof (p_rx_mbox_t) * ndmas);
30643859Sml29623 
30653859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
30663859Sml29623 		"<== nxge_unmap_rxdma"));
30673859Sml29623 }
30683859Sml29623 
30693859Sml29623 nxge_status_t
30703859Sml29623 nxge_map_rxdma_channel(p_nxge_t nxgep, uint16_t channel,
30713859Sml29623     p_nxge_dma_common_t *dma_buf_p,  p_rx_rbr_ring_t *rbr_p,
30723859Sml29623     uint32_t num_chunks,
30733859Sml29623     p_nxge_dma_common_t *dma_cntl_p, p_rx_rcr_ring_t *rcr_p,
30743859Sml29623     p_rx_mbox_t *rx_mbox_p)
30753859Sml29623 {
30763859Sml29623 	int	status = NXGE_OK;
30773859Sml29623 
30783859Sml29623 	/*
30793859Sml29623 	 * Set up and prepare buffer blocks, descriptors
30803859Sml29623 	 * and mailbox.
30813859Sml29623 	 */
30823859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
30833859Sml29623 		"==> nxge_map_rxdma_channel (channel %d)", channel));
30843859Sml29623 	/*
30853859Sml29623 	 * Receive buffer blocks
30863859Sml29623 	 */
30873859Sml29623 	status = nxge_map_rxdma_channel_buf_ring(nxgep, channel,
30883859Sml29623 			dma_buf_p, rbr_p, num_chunks);
30893859Sml29623 	if (status != NXGE_OK) {
30903859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
30913859Sml29623 			"==> nxge_map_rxdma_channel (channel %d): "
30923859Sml29623 			"map buffer failed 0x%x", channel, status));
30933859Sml29623 		goto nxge_map_rxdma_channel_exit;
30943859Sml29623 	}
30953859Sml29623 
30963859Sml29623 	/*
30973859Sml29623 	 * Receive block ring, completion ring and mailbox.
30983859Sml29623 	 */
30993859Sml29623 	status = nxge_map_rxdma_channel_cfg_ring(nxgep, channel,
31003859Sml29623 			dma_cntl_p, rbr_p, rcr_p, rx_mbox_p);
31013859Sml29623 	if (status != NXGE_OK) {
31023859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
31033859Sml29623 			"==> nxge_map_rxdma_channel (channel %d): "
31043859Sml29623 			"map config failed 0x%x", channel, status));
31053859Sml29623 		goto nxge_map_rxdma_channel_fail2;
31063859Sml29623 	}
31073859Sml29623 
31083859Sml29623 	goto nxge_map_rxdma_channel_exit;
31093859Sml29623 
31103859Sml29623 nxge_map_rxdma_channel_fail3:
31113859Sml29623 	/* Free rbr, rcr */
31123859Sml29623 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
31133859Sml29623 		"==> nxge_map_rxdma_channel: free rbr/rcr "
31143859Sml29623 		"(status 0x%x channel %d)",
31153859Sml29623 		status, channel));
31163859Sml29623 	nxge_unmap_rxdma_channel_cfg_ring(nxgep,
31173859Sml29623 		*rcr_p, *rx_mbox_p);
31183859Sml29623 
31193859Sml29623 nxge_map_rxdma_channel_fail2:
31203859Sml29623 	/* Free buffer blocks */
31213859Sml29623 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
31223859Sml29623 		"==> nxge_map_rxdma_channel: free rx buffers"
31233859Sml29623 		"(nxgep 0x%x status 0x%x channel %d)",
31243859Sml29623 		nxgep, status, channel));
31253859Sml29623 	nxge_unmap_rxdma_channel_buf_ring(nxgep, *rbr_p);
31263859Sml29623 
31274185Sspeer 	status = NXGE_ERROR;
31284185Sspeer 
31293859Sml29623 nxge_map_rxdma_channel_exit:
31303859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
31313859Sml29623 		"<== nxge_map_rxdma_channel: "
31323859Sml29623 		"(nxgep 0x%x status 0x%x channel %d)",
31333859Sml29623 		nxgep, status, channel));
31343859Sml29623 
31353859Sml29623 	return (status);
31363859Sml29623 }
31373859Sml29623 
31383859Sml29623 /*ARGSUSED*/
31393859Sml29623 static void
31403859Sml29623 nxge_unmap_rxdma_channel(p_nxge_t nxgep, uint16_t channel,
31413859Sml29623     p_rx_rbr_ring_t rbr_p, p_rx_rcr_ring_t rcr_p, p_rx_mbox_t rx_mbox_p)
31423859Sml29623 {
31433859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
31443859Sml29623 		"==> nxge_unmap_rxdma_channel (channel %d)", channel));
31453859Sml29623 
31463859Sml29623 	/*
31473859Sml29623 	 * unmap receive block ring, completion ring and mailbox.
31483859Sml29623 	 */
31493859Sml29623 	(void) nxge_unmap_rxdma_channel_cfg_ring(nxgep,
31503859Sml29623 			rcr_p, rx_mbox_p);
31513859Sml29623 
31523859Sml29623 	/* unmap buffer blocks */
31533859Sml29623 	(void) nxge_unmap_rxdma_channel_buf_ring(nxgep, rbr_p);
31543859Sml29623 
31553859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "<== nxge_unmap_rxdma_channel"));
31563859Sml29623 }
31573859Sml29623 
31583859Sml29623 /*ARGSUSED*/
31593859Sml29623 static nxge_status_t
31603859Sml29623 nxge_map_rxdma_channel_cfg_ring(p_nxge_t nxgep, uint16_t dma_channel,
31613859Sml29623     p_nxge_dma_common_t *dma_cntl_p, p_rx_rbr_ring_t *rbr_p,
31623859Sml29623     p_rx_rcr_ring_t *rcr_p, p_rx_mbox_t *rx_mbox_p)
31633859Sml29623 {
31643859Sml29623 	p_rx_rbr_ring_t 	rbrp;
31653859Sml29623 	p_rx_rcr_ring_t 	rcrp;
31663859Sml29623 	p_rx_mbox_t 		mboxp;
31673859Sml29623 	p_nxge_dma_common_t 	cntl_dmap;
31683859Sml29623 	p_nxge_dma_common_t 	dmap;
31693859Sml29623 	p_rx_msg_t 		*rx_msg_ring;
31703859Sml29623 	p_rx_msg_t 		rx_msg_p;
31713859Sml29623 	p_rbr_cfig_a_t		rcfga_p;
31723859Sml29623 	p_rbr_cfig_b_t		rcfgb_p;
31733859Sml29623 	p_rcrcfig_a_t		cfga_p;
31743859Sml29623 	p_rcrcfig_b_t		cfgb_p;
31753859Sml29623 	p_rxdma_cfig1_t		cfig1_p;
31763859Sml29623 	p_rxdma_cfig2_t		cfig2_p;
31773859Sml29623 	p_rbr_kick_t		kick_p;
31783859Sml29623 	uint32_t		dmaaddrp;
31793859Sml29623 	uint32_t		*rbr_vaddrp;
31803859Sml29623 	uint32_t		bkaddr;
31813859Sml29623 	nxge_status_t		status = NXGE_OK;
31823859Sml29623 	int			i;
31833859Sml29623 	uint32_t 		nxge_port_rcr_size;
31843859Sml29623 
31853859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
31863859Sml29623 		"==> nxge_map_rxdma_channel_cfg_ring"));
31873859Sml29623 
31883859Sml29623 	cntl_dmap = *dma_cntl_p;
31893859Sml29623 
31903859Sml29623 	/* Map in the receive block ring */
31913859Sml29623 	rbrp = *rbr_p;
31923859Sml29623 	dmap = (p_nxge_dma_common_t)&rbrp->rbr_desc;
31933859Sml29623 	nxge_setup_dma_common(dmap, cntl_dmap, rbrp->rbb_max, 4);
31943859Sml29623 	/*
31953859Sml29623 	 * Zero out buffer block ring descriptors.
31963859Sml29623 	 */
31973859Sml29623 	bzero((caddr_t)dmap->kaddrp, dmap->alength);
31983859Sml29623 
31993859Sml29623 	rcfga_p = &(rbrp->rbr_cfga);
32003859Sml29623 	rcfgb_p = &(rbrp->rbr_cfgb);
32013859Sml29623 	kick_p = &(rbrp->rbr_kick);
32023859Sml29623 	rcfga_p->value = 0;
32033859Sml29623 	rcfgb_p->value = 0;
32043859Sml29623 	kick_p->value = 0;
32053859Sml29623 	rbrp->rbr_addr = dmap->dma_cookie.dmac_laddress;
32063859Sml29623 	rcfga_p->value = (rbrp->rbr_addr &
32073859Sml29623 				(RBR_CFIG_A_STDADDR_MASK |
32083859Sml29623 				RBR_CFIG_A_STDADDR_BASE_MASK));
32093859Sml29623 	rcfga_p->value |= ((uint64_t)rbrp->rbb_max << RBR_CFIG_A_LEN_SHIFT);
32103859Sml29623 
32113859Sml29623 	rcfgb_p->bits.ldw.bufsz0 = rbrp->pkt_buf_size0;
32123859Sml29623 	rcfgb_p->bits.ldw.vld0 = 1;
32133859Sml29623 	rcfgb_p->bits.ldw.bufsz1 = rbrp->pkt_buf_size1;
32143859Sml29623 	rcfgb_p->bits.ldw.vld1 = 1;
32153859Sml29623 	rcfgb_p->bits.ldw.bufsz2 = rbrp->pkt_buf_size2;
32163859Sml29623 	rcfgb_p->bits.ldw.vld2 = 1;
32173859Sml29623 	rcfgb_p->bits.ldw.bksize = nxgep->rx_bksize_code;
32183859Sml29623 
32193859Sml29623 	/*
32203859Sml29623 	 * For each buffer block, enter receive block address to the ring.
32213859Sml29623 	 */
32223859Sml29623 	rbr_vaddrp = (uint32_t *)dmap->kaddrp;
32233859Sml29623 	rbrp->rbr_desc_vp = (uint32_t *)dmap->kaddrp;
32243859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
32253859Sml29623 		"==> nxge_map_rxdma_channel_cfg_ring: channel %d "
32263859Sml29623 		"rbr_vaddrp $%p", dma_channel, rbr_vaddrp));
32273859Sml29623 
32283859Sml29623 	rx_msg_ring = rbrp->rx_msg_ring;
32293859Sml29623 	for (i = 0; i < rbrp->tnblocks; i++) {
32303859Sml29623 		rx_msg_p = rx_msg_ring[i];
32313859Sml29623 		rx_msg_p->nxgep = nxgep;
32323859Sml29623 		rx_msg_p->rx_rbr_p = rbrp;
32333859Sml29623 		bkaddr = (uint32_t)
32343859Sml29623 			((rx_msg_p->buf_dma.dma_cookie.dmac_laddress
32353859Sml29623 				>> RBR_BKADDR_SHIFT));
32363859Sml29623 		rx_msg_p->free = B_FALSE;
32373859Sml29623 		rx_msg_p->max_usage_cnt = 0xbaddcafe;
32383859Sml29623 
32393859Sml29623 		*rbr_vaddrp++ = bkaddr;
32403859Sml29623 	}
32413859Sml29623 
32423859Sml29623 	kick_p->bits.ldw.bkadd = rbrp->rbb_max;
32433859Sml29623 	rbrp->rbr_wr_index = (rbrp->rbb_max - 1);
32443859Sml29623 
32453859Sml29623 	rbrp->rbr_rd_index = 0;
32463859Sml29623 
32473859Sml29623 	rbrp->rbr_consumed = 0;
32483859Sml29623 	rbrp->rbr_use_bcopy = B_TRUE;
32493859Sml29623 	rbrp->rbr_bufsize_type = RCR_PKTBUFSZ_0;
32503859Sml29623 	/*
32513859Sml29623 	 * Do bcopy on packets greater than bcopy size once
32523859Sml29623 	 * the lo threshold is reached.
32533859Sml29623 	 * This lo threshold should be less than the hi threshold.
32543859Sml29623 	 *
32553859Sml29623 	 * Do bcopy on every packet once the hi threshold is reached.
32563859Sml29623 	 */
32573859Sml29623 	if (nxge_rx_threshold_lo >= nxge_rx_threshold_hi) {
32583859Sml29623 		/* default it to use hi */
32593859Sml29623 		nxge_rx_threshold_lo = nxge_rx_threshold_hi;
32603859Sml29623 	}
32613859Sml29623 
32623859Sml29623 	if (nxge_rx_buf_size_type > NXGE_RBR_TYPE2) {
32633859Sml29623 		nxge_rx_buf_size_type = NXGE_RBR_TYPE2;
32643859Sml29623 	}
32653859Sml29623 	rbrp->rbr_bufsize_type = nxge_rx_buf_size_type;
32663859Sml29623 
32673859Sml29623 	switch (nxge_rx_threshold_hi) {
32683859Sml29623 	default:
32693859Sml29623 	case	NXGE_RX_COPY_NONE:
32703859Sml29623 		/* Do not do bcopy at all */
32713859Sml29623 		rbrp->rbr_use_bcopy = B_FALSE;
32723859Sml29623 		rbrp->rbr_threshold_hi = rbrp->rbb_max;
32733859Sml29623 		break;
32743859Sml29623 
32753859Sml29623 	case NXGE_RX_COPY_1:
32763859Sml29623 	case NXGE_RX_COPY_2:
32773859Sml29623 	case NXGE_RX_COPY_3:
32783859Sml29623 	case NXGE_RX_COPY_4:
32793859Sml29623 	case NXGE_RX_COPY_5:
32803859Sml29623 	case NXGE_RX_COPY_6:
32813859Sml29623 	case NXGE_RX_COPY_7:
32823859Sml29623 		rbrp->rbr_threshold_hi =
32833859Sml29623 			rbrp->rbb_max *
32843859Sml29623 			(nxge_rx_threshold_hi)/NXGE_RX_BCOPY_SCALE;
32853859Sml29623 		break;
32863859Sml29623 
32873859Sml29623 	case NXGE_RX_COPY_ALL:
32883859Sml29623 		rbrp->rbr_threshold_hi = 0;
32893859Sml29623 		break;
32903859Sml29623 	}
32913859Sml29623 
32923859Sml29623 	switch (nxge_rx_threshold_lo) {
32933859Sml29623 	default:
32943859Sml29623 	case	NXGE_RX_COPY_NONE:
32953859Sml29623 		/* Do not do bcopy at all */
32963859Sml29623 		if (rbrp->rbr_use_bcopy) {
32973859Sml29623 			rbrp->rbr_use_bcopy = B_FALSE;
32983859Sml29623 		}
32993859Sml29623 		rbrp->rbr_threshold_lo = rbrp->rbb_max;
33003859Sml29623 		break;
33013859Sml29623 
33023859Sml29623 	case NXGE_RX_COPY_1:
33033859Sml29623 	case NXGE_RX_COPY_2:
33043859Sml29623 	case NXGE_RX_COPY_3:
33053859Sml29623 	case NXGE_RX_COPY_4:
33063859Sml29623 	case NXGE_RX_COPY_5:
33073859Sml29623 	case NXGE_RX_COPY_6:
33083859Sml29623 	case NXGE_RX_COPY_7:
33093859Sml29623 		rbrp->rbr_threshold_lo =
33103859Sml29623 			rbrp->rbb_max *
33113859Sml29623 			(nxge_rx_threshold_lo)/NXGE_RX_BCOPY_SCALE;
33123859Sml29623 		break;
33133859Sml29623 
33143859Sml29623 	case NXGE_RX_COPY_ALL:
33153859Sml29623 		rbrp->rbr_threshold_lo = 0;
33163859Sml29623 		break;
33173859Sml29623 	}
33183859Sml29623 
33193859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
33203859Sml29623 		"nxge_map_rxdma_channel_cfg_ring: channel %d "
33213859Sml29623 		"rbb_max %d "
33223859Sml29623 		"rbrp->rbr_bufsize_type %d "
33233859Sml29623 		"rbb_threshold_hi %d "
33243859Sml29623 		"rbb_threshold_lo %d",
33253859Sml29623 		dma_channel,
33263859Sml29623 		rbrp->rbb_max,
33273859Sml29623 		rbrp->rbr_bufsize_type,
33283859Sml29623 		rbrp->rbr_threshold_hi,
33293859Sml29623 		rbrp->rbr_threshold_lo));
33303859Sml29623 
33313859Sml29623 	rbrp->page_valid.value = 0;
33323859Sml29623 	rbrp->page_mask_1.value = rbrp->page_mask_2.value = 0;
33333859Sml29623 	rbrp->page_value_1.value = rbrp->page_value_2.value = 0;
33343859Sml29623 	rbrp->page_reloc_1.value = rbrp->page_reloc_2.value = 0;
33353859Sml29623 	rbrp->page_hdl.value = 0;
33363859Sml29623 
33373859Sml29623 	rbrp->page_valid.bits.ldw.page0 = 1;
33383859Sml29623 	rbrp->page_valid.bits.ldw.page1 = 1;
33393859Sml29623 
33403859Sml29623 	/* Map in the receive completion ring */
33413859Sml29623 	rcrp = (p_rx_rcr_ring_t)
33423859Sml29623 		KMEM_ZALLOC(sizeof (rx_rcr_ring_t), KM_SLEEP);
33433859Sml29623 	rcrp->rdc = dma_channel;
33443859Sml29623 
33453859Sml29623 	nxge_port_rcr_size = nxgep->nxge_port_rcr_size;
33463859Sml29623 	rcrp->comp_size = nxge_port_rcr_size;
33473859Sml29623 	rcrp->comp_wrap_mask = nxge_port_rcr_size - 1;
33483859Sml29623 
33493859Sml29623 	rcrp->max_receive_pkts = nxge_max_rx_pkts;
33503859Sml29623 
33513859Sml29623 	dmap = (p_nxge_dma_common_t)&rcrp->rcr_desc;
33523859Sml29623 	nxge_setup_dma_common(dmap, cntl_dmap, rcrp->comp_size,
33533859Sml29623 			sizeof (rcr_entry_t));
33543859Sml29623 	rcrp->comp_rd_index = 0;
33553859Sml29623 	rcrp->comp_wt_index = 0;
33563859Sml29623 	rcrp->rcr_desc_rd_head_p = rcrp->rcr_desc_first_p =
33573859Sml29623 		(p_rcr_entry_t)DMA_COMMON_VPTR(rcrp->rcr_desc);
33583859Sml29623 	rcrp->rcr_desc_rd_head_pp = rcrp->rcr_desc_first_pp =
33595125Sjoycey #if defined(__i386)
33605125Sjoycey 		(p_rcr_entry_t)(uint32_t)DMA_COMMON_IOADDR(rcrp->rcr_desc);
33615125Sjoycey #else
33623859Sml29623 		(p_rcr_entry_t)DMA_COMMON_IOADDR(rcrp->rcr_desc);
33635125Sjoycey #endif
33643859Sml29623 
33653859Sml29623 	rcrp->rcr_desc_last_p = rcrp->rcr_desc_rd_head_p +
33663859Sml29623 			(nxge_port_rcr_size - 1);
33673859Sml29623 	rcrp->rcr_desc_last_pp = rcrp->rcr_desc_rd_head_pp +
33683859Sml29623 			(nxge_port_rcr_size - 1);
33693859Sml29623 
33703859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
33713859Sml29623 		"==> nxge_map_rxdma_channel_cfg_ring: "
33723859Sml29623 		"channel %d "
33733859Sml29623 		"rbr_vaddrp $%p "
33743859Sml29623 		"rcr_desc_rd_head_p $%p "
33753859Sml29623 		"rcr_desc_rd_head_pp $%p "
33763859Sml29623 		"rcr_desc_rd_last_p $%p "
33773859Sml29623 		"rcr_desc_rd_last_pp $%p ",
33783859Sml29623 		dma_channel,
33793859Sml29623 		rbr_vaddrp,
33803859Sml29623 		rcrp->rcr_desc_rd_head_p,
33813859Sml29623 		rcrp->rcr_desc_rd_head_pp,
33823859Sml29623 		rcrp->rcr_desc_last_p,
33833859Sml29623 		rcrp->rcr_desc_last_pp));
33843859Sml29623 
33853859Sml29623 	/*
33863859Sml29623 	 * Zero out buffer block ring descriptors.
33873859Sml29623 	 */
33883859Sml29623 	bzero((caddr_t)dmap->kaddrp, dmap->alength);
33893859Sml29623 	rcrp->intr_timeout = nxgep->intr_timeout;
33903859Sml29623 	rcrp->intr_threshold = nxgep->intr_threshold;
33913859Sml29623 	rcrp->full_hdr_flag = B_FALSE;
33923859Sml29623 	rcrp->sw_priv_hdr_len = 0;
33933859Sml29623 
33943859Sml29623 	cfga_p = &(rcrp->rcr_cfga);
33953859Sml29623 	cfgb_p = &(rcrp->rcr_cfgb);
33963859Sml29623 	cfga_p->value = 0;
33973859Sml29623 	cfgb_p->value = 0;
33983859Sml29623 	rcrp->rcr_addr = dmap->dma_cookie.dmac_laddress;
33993859Sml29623 	cfga_p->value = (rcrp->rcr_addr &
34003859Sml29623 			    (RCRCFIG_A_STADDR_MASK |
34013859Sml29623 			    RCRCFIG_A_STADDR_BASE_MASK));
34023859Sml29623 
34033859Sml29623 	rcfga_p->value |= ((uint64_t)rcrp->comp_size <<
34043859Sml29623 				RCRCFIG_A_LEN_SHIF);
34053859Sml29623 
34063859Sml29623 	/*
34073859Sml29623 	 * Timeout should be set based on the system clock divider.
34083859Sml29623 	 * The following timeout value of 1 assumes that the
34093859Sml29623 	 * granularity (1000) is 3 microseconds running at 300MHz.
34103859Sml29623 	 */
34113859Sml29623 	cfgb_p->bits.ldw.pthres = rcrp->intr_threshold;
34123859Sml29623 	cfgb_p->bits.ldw.timeout = rcrp->intr_timeout;
34133859Sml29623 	cfgb_p->bits.ldw.entout = 1;
34143859Sml29623 
34153859Sml29623 	/* Map in the mailbox */
34163859Sml29623 	mboxp = (p_rx_mbox_t)
34173859Sml29623 			KMEM_ZALLOC(sizeof (rx_mbox_t), KM_SLEEP);
34183859Sml29623 	dmap = (p_nxge_dma_common_t)&mboxp->rx_mbox;
34193859Sml29623 	nxge_setup_dma_common(dmap, cntl_dmap, 1, sizeof (rxdma_mailbox_t));
34203859Sml29623 	cfig1_p = (p_rxdma_cfig1_t)&mboxp->rx_cfg1;
34213859Sml29623 	cfig2_p = (p_rxdma_cfig2_t)&mboxp->rx_cfg2;
34223859Sml29623 	cfig1_p->value = cfig2_p->value = 0;
34233859Sml29623 
34243859Sml29623 	mboxp->mbox_addr = dmap->dma_cookie.dmac_laddress;
34253859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
34263859Sml29623 		"==> nxge_map_rxdma_channel_cfg_ring: "
34273859Sml29623 		"channel %d cfg1 0x%016llx cfig2 0x%016llx cookie 0x%016llx",
34283859Sml29623 		dma_channel, cfig1_p->value, cfig2_p->value,
34293859Sml29623 		mboxp->mbox_addr));
34303859Sml29623 
34313859Sml29623 	dmaaddrp = (uint32_t)(dmap->dma_cookie.dmac_laddress >> 32
34323859Sml29623 			& 0xfff);
34333859Sml29623 	cfig1_p->bits.ldw.mbaddr_h = dmaaddrp;
34343859Sml29623 
34353859Sml29623 
34363859Sml29623 	dmaaddrp = (uint32_t)(dmap->dma_cookie.dmac_laddress & 0xffffffff);
34373859Sml29623 	dmaaddrp = (uint32_t)(dmap->dma_cookie.dmac_laddress &
34383859Sml29623 				RXDMA_CFIG2_MBADDR_L_MASK);
34393859Sml29623 
34403859Sml29623 	cfig2_p->bits.ldw.mbaddr = (dmaaddrp >> RXDMA_CFIG2_MBADDR_L_SHIFT);
34413859Sml29623 
34423859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
34433859Sml29623 		"==> nxge_map_rxdma_channel_cfg_ring: "
34443859Sml29623 		"channel %d damaddrp $%p "
34453859Sml29623 		"cfg1 0x%016llx cfig2 0x%016llx",
34463859Sml29623 		dma_channel, dmaaddrp,
34473859Sml29623 		cfig1_p->value, cfig2_p->value));
34483859Sml29623 
34493859Sml29623 	cfig2_p->bits.ldw.full_hdr = rcrp->full_hdr_flag;
34503859Sml29623 	cfig2_p->bits.ldw.offset = rcrp->sw_priv_hdr_len;
34513859Sml29623 
34523859Sml29623 	rbrp->rx_rcr_p = rcrp;
34533859Sml29623 	rcrp->rx_rbr_p = rbrp;
34543859Sml29623 	*rcr_p = rcrp;
34553859Sml29623 	*rx_mbox_p = mboxp;
34563859Sml29623 
34573859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
34583859Sml29623 		"<== nxge_map_rxdma_channel_cfg_ring status 0x%08x", status));
34593859Sml29623 
34603859Sml29623 	return (status);
34613859Sml29623 }
34623859Sml29623 
34633859Sml29623 /*ARGSUSED*/
34643859Sml29623 static void
34653859Sml29623 nxge_unmap_rxdma_channel_cfg_ring(p_nxge_t nxgep,
34663859Sml29623     p_rx_rcr_ring_t rcr_p, p_rx_mbox_t rx_mbox_p)
34673859Sml29623 {
34683859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
34693859Sml29623 		"==> nxge_unmap_rxdma_channel_cfg_ring: channel %d",
34703859Sml29623 		rcr_p->rdc));
34713859Sml29623 
34723859Sml29623 	KMEM_FREE(rcr_p, sizeof (rx_rcr_ring_t));
34733859Sml29623 	KMEM_FREE(rx_mbox_p, sizeof (rx_mbox_t));
34743859Sml29623 
34753859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
34763859Sml29623 		"<== nxge_unmap_rxdma_channel_cfg_ring"));
34773859Sml29623 }
34783859Sml29623 
34793859Sml29623 static nxge_status_t
34803859Sml29623 nxge_map_rxdma_channel_buf_ring(p_nxge_t nxgep, uint16_t channel,
34813859Sml29623     p_nxge_dma_common_t *dma_buf_p,
34823859Sml29623     p_rx_rbr_ring_t *rbr_p, uint32_t num_chunks)
34833859Sml29623 {
34843859Sml29623 	p_rx_rbr_ring_t 	rbrp;
34853859Sml29623 	p_nxge_dma_common_t 	dma_bufp, tmp_bufp;
34863859Sml29623 	p_rx_msg_t 		*rx_msg_ring;
34873859Sml29623 	p_rx_msg_t 		rx_msg_p;
34883859Sml29623 	p_mblk_t 		mblk_p;
34893859Sml29623 
34903859Sml29623 	rxring_info_t *ring_info;
34913859Sml29623 	nxge_status_t		status = NXGE_OK;
34923859Sml29623 	int			i, j, index;
34933859Sml29623 	uint32_t		size, bsize, nblocks, nmsgs;
34943859Sml29623 
34953859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
34963859Sml29623 		"==> nxge_map_rxdma_channel_buf_ring: channel %d",
34973859Sml29623 		channel));
34983859Sml29623 
34993859Sml29623 	dma_bufp = tmp_bufp = *dma_buf_p;
35003859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
35013859Sml29623 		" nxge_map_rxdma_channel_buf_ring: channel %d to map %d "
35023859Sml29623 		"chunks bufp 0x%016llx",
35033859Sml29623 		channel, num_chunks, dma_bufp));
35043859Sml29623 
35053859Sml29623 	nmsgs = 0;
35063859Sml29623 	for (i = 0; i < num_chunks; i++, tmp_bufp++) {
35073859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
35083859Sml29623 			"==> nxge_map_rxdma_channel_buf_ring: channel %d "
35093859Sml29623 			"bufp 0x%016llx nblocks %d nmsgs %d",
35103859Sml29623 			channel, tmp_bufp, tmp_bufp->nblocks, nmsgs));
35113859Sml29623 		nmsgs += tmp_bufp->nblocks;
35123859Sml29623 	}
35133859Sml29623 	if (!nmsgs) {
35144185Sspeer 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
35153859Sml29623 			"<== nxge_map_rxdma_channel_buf_ring: channel %d "
35163859Sml29623 			"no msg blocks",
35173859Sml29623 			channel));
35183859Sml29623 		status = NXGE_ERROR;
35193859Sml29623 		goto nxge_map_rxdma_channel_buf_ring_exit;
35203859Sml29623 	}
35213859Sml29623 
35225170Stm144005 	rbrp = (p_rx_rbr_ring_t)KMEM_ZALLOC(sizeof (*rbrp), KM_SLEEP);
35233859Sml29623 
35243859Sml29623 	size = nmsgs * sizeof (p_rx_msg_t);
35253859Sml29623 	rx_msg_ring = KMEM_ZALLOC(size, KM_SLEEP);
35263859Sml29623 	ring_info = (rxring_info_t *)KMEM_ZALLOC(sizeof (rxring_info_t),
35273859Sml29623 		KM_SLEEP);
35283859Sml29623 
35293859Sml29623 	MUTEX_INIT(&rbrp->lock, NULL, MUTEX_DRIVER,
35303859Sml29623 				(void *)nxgep->interrupt_cookie);
35313859Sml29623 	MUTEX_INIT(&rbrp->post_lock, NULL, MUTEX_DRIVER,
35323859Sml29623 				(void *)nxgep->interrupt_cookie);
35333859Sml29623 	rbrp->rdc = channel;
35343859Sml29623 	rbrp->num_blocks = num_chunks;
35353859Sml29623 	rbrp->tnblocks = nmsgs;
35363859Sml29623 	rbrp->rbb_max = nmsgs;
35373859Sml29623 	rbrp->rbr_max_size = nmsgs;
35383859Sml29623 	rbrp->rbr_wrap_mask = (rbrp->rbb_max - 1);
35393859Sml29623 
35403859Sml29623 	/*
35413859Sml29623 	 * Buffer sizes suggested by NIU architect.
35423859Sml29623 	 * 256, 512 and 2K.
35433859Sml29623 	 */
35443859Sml29623 
35453859Sml29623 	rbrp->pkt_buf_size0 = RBR_BUFSZ0_256B;
35463859Sml29623 	rbrp->pkt_buf_size0_bytes = RBR_BUFSZ0_256_BYTES;
35473859Sml29623 	rbrp->npi_pkt_buf_size0 = SIZE_256B;
35483859Sml29623 
35493859Sml29623 	rbrp->pkt_buf_size1 = RBR_BUFSZ1_1K;
35503859Sml29623 	rbrp->pkt_buf_size1_bytes = RBR_BUFSZ1_1K_BYTES;
35513859Sml29623 	rbrp->npi_pkt_buf_size1 = SIZE_1KB;
35523859Sml29623 
35533859Sml29623 	rbrp->block_size = nxgep->rx_default_block_size;
35543859Sml29623 
35553859Sml29623 	if (!nxge_jumbo_enable && !nxgep->param_arr[param_accept_jumbo].value) {
35563859Sml29623 		rbrp->pkt_buf_size2 = RBR_BUFSZ2_2K;
35573859Sml29623 		rbrp->pkt_buf_size2_bytes = RBR_BUFSZ2_2K_BYTES;
35583859Sml29623 		rbrp->npi_pkt_buf_size2 = SIZE_2KB;
35593859Sml29623 	} else {
35603859Sml29623 		if (rbrp->block_size >= 0x2000) {
35613859Sml29623 			rbrp->pkt_buf_size2 = RBR_BUFSZ2_8K;
35623859Sml29623 			rbrp->pkt_buf_size2_bytes = RBR_BUFSZ2_8K_BYTES;
35633859Sml29623 			rbrp->npi_pkt_buf_size2 = SIZE_8KB;
35643859Sml29623 		} else {
35653859Sml29623 			rbrp->pkt_buf_size2 = RBR_BUFSZ2_4K;
35663859Sml29623 			rbrp->pkt_buf_size2_bytes = RBR_BUFSZ2_4K_BYTES;
35673859Sml29623 			rbrp->npi_pkt_buf_size2 = SIZE_4KB;
35683859Sml29623 		}
35693859Sml29623 	}
35703859Sml29623 
35713859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
35723859Sml29623 		"==> nxge_map_rxdma_channel_buf_ring: channel %d "
35733859Sml29623 		"actual rbr max %d rbb_max %d nmsgs %d "
35743859Sml29623 		"rbrp->block_size %d default_block_size %d "
35753859Sml29623 		"(config nxge_rbr_size %d nxge_rbr_spare_size %d)",
35763859Sml29623 		channel, rbrp->rbr_max_size, rbrp->rbb_max, nmsgs,
35773859Sml29623 		rbrp->block_size, nxgep->rx_default_block_size,
35783859Sml29623 		nxge_rbr_size, nxge_rbr_spare_size));
35793859Sml29623 
35803859Sml29623 	/* Map in buffers from the buffer pool.  */
35813859Sml29623 	index = 0;
35823859Sml29623 	for (i = 0; i < rbrp->num_blocks; i++, dma_bufp++) {
35833859Sml29623 		bsize = dma_bufp->block_size;
35843859Sml29623 		nblocks = dma_bufp->nblocks;
35855125Sjoycey #if defined(__i386)
35865125Sjoycey 		ring_info->buffer[i].dvma_addr = (uint32_t)dma_bufp->ioaddr_pp;
35875125Sjoycey #else
35883859Sml29623 		ring_info->buffer[i].dvma_addr = (uint64_t)dma_bufp->ioaddr_pp;
35895125Sjoycey #endif
35903859Sml29623 		ring_info->buffer[i].buf_index = i;
35913859Sml29623 		ring_info->buffer[i].buf_size = dma_bufp->alength;
35923859Sml29623 		ring_info->buffer[i].start_index = index;
35935125Sjoycey #if defined(__i386)
35945125Sjoycey 		ring_info->buffer[i].kaddr = (uint32_t)dma_bufp->kaddrp;
35955125Sjoycey #else
35963859Sml29623 		ring_info->buffer[i].kaddr = (uint64_t)dma_bufp->kaddrp;
35975125Sjoycey #endif
35983859Sml29623 
35993859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
36003859Sml29623 			" nxge_map_rxdma_channel_buf_ring: map channel %d "
36013859Sml29623 			"chunk %d"
36023859Sml29623 			" nblocks %d chunk_size %x block_size 0x%x "
36033859Sml29623 			"dma_bufp $%p", channel, i,
36043859Sml29623 			dma_bufp->nblocks, ring_info->buffer[i].buf_size, bsize,
36053859Sml29623 			dma_bufp));
36063859Sml29623 
36073859Sml29623 		for (j = 0; j < nblocks; j++) {
36083859Sml29623 			if ((rx_msg_p = nxge_allocb(bsize, BPRI_LO,
36093859Sml29623 					dma_bufp)) == NULL) {
36104185Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
36114185Sspeer 					"allocb failed (index %d i %d j %d)",
36124185Sspeer 					index, i, j));
36134185Sspeer 				goto nxge_map_rxdma_channel_buf_ring_fail1;
36143859Sml29623 			}
36153859Sml29623 			rx_msg_ring[index] = rx_msg_p;
36163859Sml29623 			rx_msg_p->block_index = index;
36173859Sml29623 			rx_msg_p->shifted_addr = (uint32_t)
36183859Sml29623 				((rx_msg_p->buf_dma.dma_cookie.dmac_laddress >>
36193859Sml29623 					    RBR_BKADDR_SHIFT));
36203859Sml29623 
36213859Sml29623 			NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
36224185Sspeer 				"index %d j %d rx_msg_p $%p mblk %p",
36234185Sspeer 				index, j, rx_msg_p, rx_msg_p->rx_mblk_p));
36243859Sml29623 
36253859Sml29623 			mblk_p = rx_msg_p->rx_mblk_p;
36263859Sml29623 			mblk_p->b_wptr = mblk_p->b_rptr + bsize;
36275170Stm144005 
36285170Stm144005 			rbrp->rbr_ref_cnt++;
36293859Sml29623 			index++;
36303859Sml29623 			rx_msg_p->buf_dma.dma_channel = channel;
36313859Sml29623 		}
36323859Sml29623 	}
36333859Sml29623 	if (i < rbrp->num_blocks) {
36343859Sml29623 		goto nxge_map_rxdma_channel_buf_ring_fail1;
36353859Sml29623 	}
36363859Sml29623 
36373859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
36383859Sml29623 		"nxge_map_rxdma_channel_buf_ring: done buf init "
36393859Sml29623 			"channel %d msg block entries %d",
36403859Sml29623 			channel, index));
36413859Sml29623 	ring_info->block_size_mask = bsize - 1;
36423859Sml29623 	rbrp->rx_msg_ring = rx_msg_ring;
36433859Sml29623 	rbrp->dma_bufp = dma_buf_p;
36443859Sml29623 	rbrp->ring_info = ring_info;
36453859Sml29623 
36463859Sml29623 	status = nxge_rxbuf_index_info_init(nxgep, rbrp);
36473859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
36483859Sml29623 		" nxge_map_rxdma_channel_buf_ring: "
36493859Sml29623 		"channel %d done buf info init", channel));
36503859Sml29623 
36515170Stm144005 	/*
36525170Stm144005 	 * Finally, permit nxge_freeb() to call nxge_post_page().
36535170Stm144005 	 */
36545170Stm144005 	rbrp->rbr_state = RBR_POSTING;
36555170Stm144005 
36563859Sml29623 	*rbr_p = rbrp;
36573859Sml29623 	goto nxge_map_rxdma_channel_buf_ring_exit;
36583859Sml29623 
36593859Sml29623 nxge_map_rxdma_channel_buf_ring_fail1:
36603859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
36613859Sml29623 		" nxge_map_rxdma_channel_buf_ring: failed channel (0x%x)",
36623859Sml29623 		channel, status));
36633859Sml29623 
36643859Sml29623 	index--;
36653859Sml29623 	for (; index >= 0; index--) {
36663859Sml29623 		rx_msg_p = rx_msg_ring[index];
36673859Sml29623 		if (rx_msg_p != NULL) {
36683859Sml29623 			freeb(rx_msg_p->rx_mblk_p);
36693859Sml29623 			rx_msg_ring[index] = NULL;
36703859Sml29623 		}
36713859Sml29623 	}
36723859Sml29623 nxge_map_rxdma_channel_buf_ring_fail:
36733859Sml29623 	MUTEX_DESTROY(&rbrp->post_lock);
36743859Sml29623 	MUTEX_DESTROY(&rbrp->lock);
36753859Sml29623 	KMEM_FREE(ring_info, sizeof (rxring_info_t));
36763859Sml29623 	KMEM_FREE(rx_msg_ring, size);
36773859Sml29623 	KMEM_FREE(rbrp, sizeof (rx_rbr_ring_t));
36783859Sml29623 
36794185Sspeer 	status = NXGE_ERROR;
36804185Sspeer 
36813859Sml29623 nxge_map_rxdma_channel_buf_ring_exit:
36823859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
36833859Sml29623 		"<== nxge_map_rxdma_channel_buf_ring status 0x%08x", status));
36843859Sml29623 
36853859Sml29623 	return (status);
36863859Sml29623 }
36873859Sml29623 
36883859Sml29623 /*ARGSUSED*/
36893859Sml29623 static void
36903859Sml29623 nxge_unmap_rxdma_channel_buf_ring(p_nxge_t nxgep,
36913859Sml29623     p_rx_rbr_ring_t rbr_p)
36923859Sml29623 {
36933859Sml29623 	p_rx_msg_t 		*rx_msg_ring;
36943859Sml29623 	p_rx_msg_t 		rx_msg_p;
36953859Sml29623 	rxring_info_t 		*ring_info;
36963859Sml29623 	int			i;
36973859Sml29623 	uint32_t		size;
36983859Sml29623 #ifdef	NXGE_DEBUG
36993859Sml29623 	int			num_chunks;
37003859Sml29623 #endif
37013859Sml29623 
37023859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
37033859Sml29623 		"==> nxge_unmap_rxdma_channel_buf_ring"));
37043859Sml29623 	if (rbr_p == NULL) {
37053859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
37063859Sml29623 			"<== nxge_unmap_rxdma_channel_buf_ring: NULL rbrp"));
37073859Sml29623 		return;
37083859Sml29623 	}
37093859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
37103859Sml29623 		"==> nxge_unmap_rxdma_channel_buf_ring: channel %d",
37113859Sml29623 		rbr_p->rdc));
37123859Sml29623 
37133859Sml29623 	rx_msg_ring = rbr_p->rx_msg_ring;
37143859Sml29623 	ring_info = rbr_p->ring_info;
37153859Sml29623 
37163859Sml29623 	if (rx_msg_ring == NULL || ring_info == NULL) {
37173859Sml29623 			NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
37183859Sml29623 		"<== nxge_unmap_rxdma_channel_buf_ring: "
37193859Sml29623 		"rx_msg_ring $%p ring_info $%p",
37203859Sml29623 		rx_msg_p, ring_info));
37213859Sml29623 		return;
37223859Sml29623 	}
37233859Sml29623 
37243859Sml29623 #ifdef	NXGE_DEBUG
37253859Sml29623 	num_chunks = rbr_p->num_blocks;
37263859Sml29623 #endif
37273859Sml29623 	size = rbr_p->tnblocks * sizeof (p_rx_msg_t);
37283859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
37293859Sml29623 		" nxge_unmap_rxdma_channel_buf_ring: channel %d chunks %d "
37303859Sml29623 		"tnblocks %d (max %d) size ptrs %d ",
37313859Sml29623 		rbr_p->rdc, num_chunks,
37323859Sml29623 		rbr_p->tnblocks, rbr_p->rbr_max_size, size));
37333859Sml29623 
37343859Sml29623 	for (i = 0; i < rbr_p->tnblocks; i++) {
37353859Sml29623 		rx_msg_p = rx_msg_ring[i];
37363859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
37373859Sml29623 			" nxge_unmap_rxdma_channel_buf_ring: "
37383859Sml29623 			"rx_msg_p $%p",
37393859Sml29623 			rx_msg_p));
37403859Sml29623 		if (rx_msg_p != NULL) {
37413859Sml29623 			freeb(rx_msg_p->rx_mblk_p);
37423859Sml29623 			rx_msg_ring[i] = NULL;
37433859Sml29623 		}
37443859Sml29623 	}
37453859Sml29623 
37465170Stm144005 	/*
37475170Stm144005 	 * We no longer may use the mutex <post_lock>. By setting
37485170Stm144005 	 * <rbr_state> to anything but POSTING, we prevent
37495170Stm144005 	 * nxge_post_page() from accessing a dead mutex.
37505170Stm144005 	 */
37515170Stm144005 	rbr_p->rbr_state = RBR_UNMAPPING;
37523859Sml29623 	MUTEX_DESTROY(&rbr_p->post_lock);
37535170Stm144005 
37543859Sml29623 	MUTEX_DESTROY(&rbr_p->lock);
37553859Sml29623 	KMEM_FREE(ring_info, sizeof (rxring_info_t));
37563859Sml29623 	KMEM_FREE(rx_msg_ring, size);
37575170Stm144005 
37585170Stm144005 	if (rbr_p->rbr_ref_cnt == 0) {
37595170Stm144005 		/* This is the normal state of affairs. */
37605170Stm144005 		KMEM_FREE(rbr_p, sizeof (*rbr_p));
37615170Stm144005 	} else {
37625170Stm144005 		/*
37635170Stm144005 		 * Some of our buffers are still being used.
37645170Stm144005 		 * Therefore, tell nxge_freeb() this ring is
37655170Stm144005 		 * unmapped, so it may free <rbr_p> for us.
37665170Stm144005 		 */
37675170Stm144005 		rbr_p->rbr_state = RBR_UNMAPPED;
37685170Stm144005 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
37695170Stm144005 		    "unmap_rxdma_buf_ring: %d %s outstanding.",
37705170Stm144005 		    rbr_p->rbr_ref_cnt,
37715170Stm144005 		    rbr_p->rbr_ref_cnt == 1 ? "msg" : "msgs"));
37725170Stm144005 	}
37733859Sml29623 
37743859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
37753859Sml29623 		"<== nxge_unmap_rxdma_channel_buf_ring"));
37763859Sml29623 }
37773859Sml29623 
37783859Sml29623 static nxge_status_t
37793859Sml29623 nxge_rxdma_hw_start_common(p_nxge_t nxgep)
37803859Sml29623 {
37813859Sml29623 	nxge_status_t		status = NXGE_OK;
37823859Sml29623 
37833859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_rxdma_hw_start_common"));
37843859Sml29623 
37853859Sml29623 	/*
37863859Sml29623 	 * Load the sharable parameters by writing to the
37873859Sml29623 	 * function zero control registers. These FZC registers
37883859Sml29623 	 * should be initialized only once for the entire chip.
37893859Sml29623 	 */
37903859Sml29623 	(void) nxge_init_fzc_rx_common(nxgep);
37913859Sml29623 
37923859Sml29623 	/*
37933859Sml29623 	 * Initialize the RXDMA port specific FZC control configurations.
37943859Sml29623 	 * These FZC registers are pertaining to each port.
37953859Sml29623 	 */
37963859Sml29623 	(void) nxge_init_fzc_rxdma_port(nxgep);
37973859Sml29623 
37983859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_rxdma_hw_start_common"));
37993859Sml29623 
38003859Sml29623 	return (status);
38013859Sml29623 }
38023859Sml29623 
38033859Sml29623 /*ARGSUSED*/
38043859Sml29623 static void
38053859Sml29623 nxge_rxdma_hw_stop_common(p_nxge_t nxgep)
38063859Sml29623 {
38073859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_rxdma_hw_stop_common"));
38083859Sml29623 
38093859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_rxdma_hw_stop_common"));
38103859Sml29623 }
38113859Sml29623 
38123859Sml29623 static nxge_status_t
38133859Sml29623 nxge_rxdma_hw_start(p_nxge_t nxgep)
38143859Sml29623 {
38153859Sml29623 	int			i, ndmas;
38163859Sml29623 	uint16_t		channel;
38173859Sml29623 	p_rx_rbr_rings_t 	rx_rbr_rings;
38183859Sml29623 	p_rx_rbr_ring_t		*rbr_rings;
38193859Sml29623 	p_rx_rcr_rings_t 	rx_rcr_rings;
38203859Sml29623 	p_rx_rcr_ring_t		*rcr_rings;
38213859Sml29623 	p_rx_mbox_areas_t 	rx_mbox_areas_p;
38223859Sml29623 	p_rx_mbox_t		*rx_mbox_p;
38233859Sml29623 	nxge_status_t		status = NXGE_OK;
38243859Sml29623 
38253859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_rxdma_hw_start"));
38263859Sml29623 
38273859Sml29623 	rx_rbr_rings = nxgep->rx_rbr_rings;
38283859Sml29623 	rx_rcr_rings = nxgep->rx_rcr_rings;
38293859Sml29623 	if (rx_rbr_rings == NULL || rx_rcr_rings == NULL) {
38303859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
38313859Sml29623 			"<== nxge_rxdma_hw_start: NULL ring pointers"));
38323859Sml29623 		return (NXGE_ERROR);
38333859Sml29623 	}
38343859Sml29623 	ndmas = rx_rbr_rings->ndmas;
38353859Sml29623 	if (ndmas == 0) {
38363859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
38373859Sml29623 			"<== nxge_rxdma_hw_start: no dma channel allocated"));
38383859Sml29623 		return (NXGE_ERROR);
38393859Sml29623 	}
38403859Sml29623 
38413859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
38423859Sml29623 		"==> nxge_rxdma_hw_start (ndmas %d)", ndmas));
38433859Sml29623 
38443859Sml29623 	rbr_rings = rx_rbr_rings->rbr_rings;
38453859Sml29623 	rcr_rings = rx_rcr_rings->rcr_rings;
38463859Sml29623 	rx_mbox_areas_p = nxgep->rx_mbox_areas_p;
38473859Sml29623 	if (rx_mbox_areas_p) {
38483859Sml29623 		rx_mbox_p = rx_mbox_areas_p->rxmbox_areas;
38493859Sml29623 	}
38503859Sml29623 
38513859Sml29623 	for (i = 0; i < ndmas; i++) {
38523859Sml29623 		channel = rbr_rings[i]->rdc;
38533859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
38543859Sml29623 			"==> nxge_rxdma_hw_start (ndmas %d) channel %d",
38553859Sml29623 				ndmas, channel));
38563859Sml29623 		status = nxge_rxdma_start_channel(nxgep, channel,
38573859Sml29623 				(p_rx_rbr_ring_t)rbr_rings[i],
38583859Sml29623 				(p_rx_rcr_ring_t)rcr_rings[i],
38593859Sml29623 				(p_rx_mbox_t)rx_mbox_p[i]);
38603859Sml29623 		if (status != NXGE_OK) {
38613859Sml29623 			goto nxge_rxdma_hw_start_fail1;
38623859Sml29623 		}
38633859Sml29623 	}
38643859Sml29623 
38653859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_rxdma_hw_start: "
38663859Sml29623 		"rx_rbr_rings 0x%016llx rings 0x%016llx",
38673859Sml29623 		rx_rbr_rings, rx_rcr_rings));
38683859Sml29623 
38693859Sml29623 	goto nxge_rxdma_hw_start_exit;
38703859Sml29623 
38713859Sml29623 nxge_rxdma_hw_start_fail1:
38723859Sml29623 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
38733859Sml29623 		"==> nxge_rxdma_hw_start: disable "
38743859Sml29623 		"(status 0x%x channel %d i %d)", status, channel, i));
38753859Sml29623 	for (; i >= 0; i--) {
38763859Sml29623 		channel = rbr_rings[i]->rdc;
38773859Sml29623 		(void) nxge_rxdma_stop_channel(nxgep, channel);
38783859Sml29623 	}
38793859Sml29623 
38803859Sml29623 nxge_rxdma_hw_start_exit:
38813859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
38823859Sml29623 		"==> nxge_rxdma_hw_start: (status 0x%x)", status));
38833859Sml29623 
38843859Sml29623 	return (status);
38853859Sml29623 }
38863859Sml29623 
38873859Sml29623 static void
38883859Sml29623 nxge_rxdma_hw_stop(p_nxge_t nxgep)
38893859Sml29623 {
38903859Sml29623 	int			i, ndmas;
38913859Sml29623 	uint16_t		channel;
38923859Sml29623 	p_rx_rbr_rings_t 	rx_rbr_rings;
38933859Sml29623 	p_rx_rbr_ring_t		*rbr_rings;
38943859Sml29623 	p_rx_rcr_rings_t 	rx_rcr_rings;
38953859Sml29623 
38963859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_rxdma_hw_stop"));
38973859Sml29623 
38983859Sml29623 	rx_rbr_rings = nxgep->rx_rbr_rings;
38993859Sml29623 	rx_rcr_rings = nxgep->rx_rcr_rings;
39003859Sml29623 	if (rx_rbr_rings == NULL || rx_rcr_rings == NULL) {
39013859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
39023859Sml29623 			"<== nxge_rxdma_hw_stop: NULL ring pointers"));
39033859Sml29623 		return;
39043859Sml29623 	}
39053859Sml29623 	ndmas = rx_rbr_rings->ndmas;
39063859Sml29623 	if (!ndmas) {
39073859Sml29623 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
39083859Sml29623 			"<== nxge_rxdma_hw_stop: no dma channel allocated"));
39093859Sml29623 		return;
39103859Sml29623 	}
39113859Sml29623 
39123859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
39133859Sml29623 		"==> nxge_rxdma_hw_stop (ndmas %d)", ndmas));
39143859Sml29623 
39153859Sml29623 	rbr_rings = rx_rbr_rings->rbr_rings;
39163859Sml29623 
39173859Sml29623 	for (i = 0; i < ndmas; i++) {
39183859Sml29623 		channel = rbr_rings[i]->rdc;
39193859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
39203859Sml29623 			"==> nxge_rxdma_hw_stop (ndmas %d) channel %d",
39213859Sml29623 				ndmas, channel));
39223859Sml29623 		(void) nxge_rxdma_stop_channel(nxgep, channel);
39233859Sml29623 	}
39243859Sml29623 
39253859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_rxdma_hw_stop: "
39263859Sml29623 		"rx_rbr_rings 0x%016llx rings 0x%016llx",
39273859Sml29623 		rx_rbr_rings, rx_rcr_rings));
39283859Sml29623 
39293859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "<== nxge_rxdma_hw_stop"));
39303859Sml29623 }
39313859Sml29623 
39323859Sml29623 
39333859Sml29623 static nxge_status_t
39343859Sml29623 nxge_rxdma_start_channel(p_nxge_t nxgep, uint16_t channel,
39353859Sml29623     p_rx_rbr_ring_t rbr_p, p_rx_rcr_ring_t rcr_p, p_rx_mbox_t mbox_p)
39363859Sml29623 
39373859Sml29623 {
39383859Sml29623 	npi_handle_t		handle;
39393859Sml29623 	npi_status_t		rs = NPI_SUCCESS;
39403859Sml29623 	rx_dma_ctl_stat_t	cs;
39413859Sml29623 	rx_dma_ent_msk_t	ent_mask;
39423859Sml29623 	nxge_status_t		status = NXGE_OK;
39433859Sml29623 
39443859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_rxdma_start_channel"));
39453859Sml29623 
39463859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
39473859Sml29623 
39483859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "nxge_rxdma_start_channel: "
39493859Sml29623 		"npi handle addr $%p acc $%p",
39503859Sml29623 		nxgep->npi_handle.regp, nxgep->npi_handle.regh));
39513859Sml29623 
39523859Sml29623 	/* Reset RXDMA channel */
39533859Sml29623 	rs = npi_rxdma_cfg_rdc_reset(handle, channel);
39543859Sml29623 	if (rs != NPI_SUCCESS) {
39553859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
39563859Sml29623 			"==> nxge_rxdma_start_channel: "
39573859Sml29623 			"reset rxdma failed (0x%08x channel %d)",
39583859Sml29623 			status, channel));
39593859Sml29623 		return (NXGE_ERROR | rs);
39603859Sml29623 	}
39613859Sml29623 
39623859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
39633859Sml29623 		"==> nxge_rxdma_start_channel: reset done: channel %d",
39643859Sml29623 		channel));
39653859Sml29623 
39663859Sml29623 	/*
39673859Sml29623 	 * Initialize the RXDMA channel specific FZC control
39683859Sml29623 	 * configurations. These FZC registers are pertaining
39693859Sml29623 	 * to each RX channel (logical pages).
39703859Sml29623 	 */
39713859Sml29623 	status = nxge_init_fzc_rxdma_channel(nxgep,
39723859Sml29623 			channel, rbr_p, rcr_p, mbox_p);
39733859Sml29623 	if (status != NXGE_OK) {
39743859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
39753859Sml29623 			"==> nxge_rxdma_start_channel: "
39763859Sml29623 			"init fzc rxdma failed (0x%08x channel %d)",
39773859Sml29623 			status, channel));
39783859Sml29623 		return (status);
39793859Sml29623 	}
39803859Sml29623 
39813859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
39823859Sml29623 		"==> nxge_rxdma_start_channel: fzc done"));
39833859Sml29623 
39843859Sml29623 	/*
39853859Sml29623 	 * Zero out the shadow  and prefetch ram.
39863859Sml29623 	 */
39873859Sml29623 
39883859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_rxdma_start_channel: "
39893859Sml29623 		"ram done"));
39903859Sml29623 
39913859Sml29623 	/* Set up the interrupt event masks. */
39923859Sml29623 	ent_mask.value = 0;
39933859Sml29623 	ent_mask.value |= RX_DMA_ENT_MSK_RBREMPTY_MASK;
39943859Sml29623 	rs = npi_rxdma_event_mask(handle, OP_SET, channel,
39953859Sml29623 			&ent_mask);
39963859Sml29623 	if (rs != NPI_SUCCESS) {
39973859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
39983859Sml29623 			"==> nxge_rxdma_start_channel: "
39993859Sml29623 			"init rxdma event masks failed (0x%08x channel %d)",
40003859Sml29623 			status, channel));
40013859Sml29623 		return (NXGE_ERROR | rs);
40023859Sml29623 	}
40033859Sml29623 
40043859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_rxdma_start_channel: "
40053859Sml29623 		"event done: channel %d (mask 0x%016llx)",
40063859Sml29623 		channel, ent_mask.value));
40073859Sml29623 
40083859Sml29623 	/* Initialize the receive DMA control and status register */
40093859Sml29623 	cs.value = 0;
40103859Sml29623 	cs.bits.hdw.mex = 1;
40113859Sml29623 	cs.bits.hdw.rcrthres = 1;
40123859Sml29623 	cs.bits.hdw.rcrto = 1;
40133859Sml29623 	cs.bits.hdw.rbr_empty = 1;
40143859Sml29623 	status = nxge_init_rxdma_channel_cntl_stat(nxgep, channel, &cs);
40153859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_rxdma_start_channel: "
40163859Sml29623 		"channel %d rx_dma_cntl_stat 0x%0016llx", channel, cs.value));
40173859Sml29623 	if (status != NXGE_OK) {
40183859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
40193859Sml29623 			"==> nxge_rxdma_start_channel: "
40203859Sml29623 			"init rxdma control register failed (0x%08x channel %d",
40213859Sml29623 			status, channel));
40223859Sml29623 		return (status);
40233859Sml29623 	}
40243859Sml29623 
40253859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_rxdma_start_channel: "
40263859Sml29623 		"control done - channel %d cs 0x%016llx", channel, cs.value));
40273859Sml29623 
40283859Sml29623 	/*
40293859Sml29623 	 * Load RXDMA descriptors, buffers, mailbox,
40303859Sml29623 	 * initialise the receive DMA channels and
40313859Sml29623 	 * enable each DMA channel.
40323859Sml29623 	 */
40333859Sml29623 	status = nxge_enable_rxdma_channel(nxgep,
40343859Sml29623 			channel, rbr_p, rcr_p, mbox_p);
40353859Sml29623 
40363859Sml29623 	if (status != NXGE_OK) {
40373859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
40383859Sml29623 			    " nxge_rxdma_start_channel: "
40393859Sml29623 			    " init enable rxdma failed (0x%08x channel %d)",
40403859Sml29623 			    status, channel));
40413859Sml29623 		return (status);
40423859Sml29623 	}
40433859Sml29623 
40443859Sml29623 	ent_mask.value = 0;
40453859Sml29623 	ent_mask.value |= (RX_DMA_ENT_MSK_WRED_DROP_MASK |
40463859Sml29623 				RX_DMA_ENT_MSK_PTDROP_PKT_MASK);
40473859Sml29623 	rs = npi_rxdma_event_mask(handle, OP_SET, channel,
40483859Sml29623 			&ent_mask);
40493859Sml29623 	if (rs != NPI_SUCCESS) {
40503859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
40513859Sml29623 			"==> nxge_rxdma_start_channel: "
40523859Sml29623 			"init rxdma event masks failed (0x%08x channel %d)",
40533859Sml29623 			status, channel));
40543859Sml29623 		return (NXGE_ERROR | rs);
40553859Sml29623 	}
40563859Sml29623 
40573859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_rxdma_start_channel: "
40583859Sml29623 		"control done - channel %d cs 0x%016llx", channel, cs.value));
40593859Sml29623 
40603859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
40613859Sml29623 		"==> nxge_rxdma_start_channel: enable done"));
40623859Sml29623 
40633859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "<== nxge_rxdma_start_channel"));
40643859Sml29623 
40653859Sml29623 	return (NXGE_OK);
40663859Sml29623 }
40673859Sml29623 
40683859Sml29623 static nxge_status_t
40693859Sml29623 nxge_rxdma_stop_channel(p_nxge_t nxgep, uint16_t channel)
40703859Sml29623 {
40713859Sml29623 	npi_handle_t		handle;
40723859Sml29623 	npi_status_t		rs = NPI_SUCCESS;
40733859Sml29623 	rx_dma_ctl_stat_t	cs;
40743859Sml29623 	rx_dma_ent_msk_t	ent_mask;
40753859Sml29623 	nxge_status_t		status = NXGE_OK;
40763859Sml29623 
40773859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "==> nxge_rxdma_stop_channel"));
40783859Sml29623 
40793859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
40803859Sml29623 
40813859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "nxge_rxdma_stop_channel: "
40823859Sml29623 		"npi handle addr $%p acc $%p",
40833859Sml29623 		nxgep->npi_handle.regp, nxgep->npi_handle.regh));
40843859Sml29623 
40853859Sml29623 	/* Reset RXDMA channel */
40863859Sml29623 	rs = npi_rxdma_cfg_rdc_reset(handle, channel);
40873859Sml29623 	if (rs != NPI_SUCCESS) {
40883859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
40893859Sml29623 			    " nxge_rxdma_stop_channel: "
40903859Sml29623 			    " reset rxdma failed (0x%08x channel %d)",
40913859Sml29623 			    rs, channel));
40923859Sml29623 		return (NXGE_ERROR | rs);
40933859Sml29623 	}
40943859Sml29623 
40953859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
40963859Sml29623 		"==> nxge_rxdma_stop_channel: reset done"));
40973859Sml29623 
40983859Sml29623 	/* Set up the interrupt event masks. */
40993859Sml29623 	ent_mask.value = RX_DMA_ENT_MSK_ALL;
41003859Sml29623 	rs = npi_rxdma_event_mask(handle, OP_SET, channel,
41013859Sml29623 			&ent_mask);
41023859Sml29623 	if (rs != NPI_SUCCESS) {
41033859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
41043859Sml29623 			    "==> nxge_rxdma_stop_channel: "
41053859Sml29623 			    "set rxdma event masks failed (0x%08x channel %d)",
41063859Sml29623 			    rs, channel));
41073859Sml29623 		return (NXGE_ERROR | rs);
41083859Sml29623 	}
41093859Sml29623 
41103859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
41113859Sml29623 		"==> nxge_rxdma_stop_channel: event done"));
41123859Sml29623 
41133859Sml29623 	/* Initialize the receive DMA control and status register */
41143859Sml29623 	cs.value = 0;
41153859Sml29623 	status = nxge_init_rxdma_channel_cntl_stat(nxgep, channel,
41163859Sml29623 			&cs);
41173859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "==> nxge_rxdma_stop_channel: control "
41183859Sml29623 		" to default (all 0s) 0x%08x", cs.value));
41193859Sml29623 	if (status != NXGE_OK) {
41203859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
41213859Sml29623 			    " nxge_rxdma_stop_channel: init rxdma"
41223859Sml29623 			    " control register failed (0x%08x channel %d",
41233859Sml29623 			status, channel));
41243859Sml29623 		return (status);
41253859Sml29623 	}
41263859Sml29623 
41273859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL,
41283859Sml29623 		"==> nxge_rxdma_stop_channel: control done"));
41293859Sml29623 
41303859Sml29623 	/* disable dma channel */
41313859Sml29623 	status = nxge_disable_rxdma_channel(nxgep, channel);
41323859Sml29623 
41333859Sml29623 	if (status != NXGE_OK) {
41343859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
41353859Sml29623 			    " nxge_rxdma_stop_channel: "
41363859Sml29623 			    " init enable rxdma failed (0x%08x channel %d)",
41373859Sml29623 			    status, channel));
41383859Sml29623 		return (status);
41393859Sml29623 	}
41403859Sml29623 
41413859Sml29623 	NXGE_DEBUG_MSG((nxgep,
41423859Sml29623 		RX_CTL, "==> nxge_rxdma_stop_channel: disable done"));
41433859Sml29623 
41443859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "<== nxge_rxdma_stop_channel"));
41453859Sml29623 
41463859Sml29623 	return (NXGE_OK);
41473859Sml29623 }
41483859Sml29623 
41493859Sml29623 nxge_status_t
41503859Sml29623 nxge_rxdma_handle_sys_errors(p_nxge_t nxgep)
41513859Sml29623 {
41523859Sml29623 	npi_handle_t		handle;
41533859Sml29623 	p_nxge_rdc_sys_stats_t	statsp;
41543859Sml29623 	rx_ctl_dat_fifo_stat_t	stat;
41553859Sml29623 	uint32_t		zcp_err_status;
41563859Sml29623 	uint32_t		ipp_err_status;
41573859Sml29623 	nxge_status_t		status = NXGE_OK;
41583859Sml29623 	npi_status_t		rs = NPI_SUCCESS;
41593859Sml29623 	boolean_t		my_err = B_FALSE;
41603859Sml29623 
41613859Sml29623 	handle = nxgep->npi_handle;
41623859Sml29623 	statsp = (p_nxge_rdc_sys_stats_t)&nxgep->statsp->rdc_sys_stats;
41633859Sml29623 
41643859Sml29623 	rs = npi_rxdma_rxctl_fifo_error_intr_get(handle, &stat);
41653859Sml29623 
41663859Sml29623 	if (rs != NPI_SUCCESS)
41673859Sml29623 		return (NXGE_ERROR | rs);
41683859Sml29623 
41693859Sml29623 	if (stat.bits.ldw.id_mismatch) {
41703859Sml29623 		statsp->id_mismatch++;
41713859Sml29623 		NXGE_FM_REPORT_ERROR(nxgep, nxgep->mac.portnum, NULL,
41723859Sml29623 					NXGE_FM_EREPORT_RDMC_ID_MISMATCH);
41733859Sml29623 		/* Global fatal error encountered */
41743859Sml29623 	}
41753859Sml29623 
41763859Sml29623 	if ((stat.bits.ldw.zcp_eop_err) || (stat.bits.ldw.ipp_eop_err)) {
41773859Sml29623 		switch (nxgep->mac.portnum) {
41783859Sml29623 		case 0:
41793859Sml29623 			if ((stat.bits.ldw.zcp_eop_err & FIFO_EOP_PORT0) ||
41803859Sml29623 				(stat.bits.ldw.ipp_eop_err & FIFO_EOP_PORT0)) {
41813859Sml29623 				my_err = B_TRUE;
41823859Sml29623 				zcp_err_status = stat.bits.ldw.zcp_eop_err;
41833859Sml29623 				ipp_err_status = stat.bits.ldw.ipp_eop_err;
41843859Sml29623 			}
41853859Sml29623 			break;
41863859Sml29623 		case 1:
41873859Sml29623 			if ((stat.bits.ldw.zcp_eop_err & FIFO_EOP_PORT1) ||
41883859Sml29623 				(stat.bits.ldw.ipp_eop_err & FIFO_EOP_PORT1)) {
41893859Sml29623 				my_err = B_TRUE;
41903859Sml29623 				zcp_err_status = stat.bits.ldw.zcp_eop_err;
41913859Sml29623 				ipp_err_status = stat.bits.ldw.ipp_eop_err;
41923859Sml29623 			}
41933859Sml29623 			break;
41943859Sml29623 		case 2:
41953859Sml29623 			if ((stat.bits.ldw.zcp_eop_err & FIFO_EOP_PORT2) ||
41963859Sml29623 				(stat.bits.ldw.ipp_eop_err & FIFO_EOP_PORT2)) {
41973859Sml29623 				my_err = B_TRUE;
41983859Sml29623 				zcp_err_status = stat.bits.ldw.zcp_eop_err;
41993859Sml29623 				ipp_err_status = stat.bits.ldw.ipp_eop_err;
42003859Sml29623 			}
42013859Sml29623 			break;
42023859Sml29623 		case 3:
42033859Sml29623 			if ((stat.bits.ldw.zcp_eop_err & FIFO_EOP_PORT3) ||
42043859Sml29623 				(stat.bits.ldw.ipp_eop_err & FIFO_EOP_PORT3)) {
42053859Sml29623 				my_err = B_TRUE;
42063859Sml29623 				zcp_err_status = stat.bits.ldw.zcp_eop_err;
42073859Sml29623 				ipp_err_status = stat.bits.ldw.ipp_eop_err;
42083859Sml29623 			}
42093859Sml29623 			break;
42103859Sml29623 		default:
42113859Sml29623 			return (NXGE_ERROR);
42123859Sml29623 		}
42133859Sml29623 	}
42143859Sml29623 
42153859Sml29623 	if (my_err) {
42163859Sml29623 		status = nxge_rxdma_handle_port_errors(nxgep, ipp_err_status,
42173859Sml29623 							zcp_err_status);
42183859Sml29623 		if (status != NXGE_OK)
42193859Sml29623 			return (status);
42203859Sml29623 	}
42213859Sml29623 
42223859Sml29623 	return (NXGE_OK);
42233859Sml29623 }
42243859Sml29623 
42253859Sml29623 static nxge_status_t
42263859Sml29623 nxge_rxdma_handle_port_errors(p_nxge_t nxgep, uint32_t ipp_status,
42273859Sml29623 							uint32_t zcp_status)
42283859Sml29623 {
42293859Sml29623 	boolean_t		rxport_fatal = B_FALSE;
42303859Sml29623 	p_nxge_rdc_sys_stats_t	statsp;
42313859Sml29623 	nxge_status_t		status = NXGE_OK;
42323859Sml29623 	uint8_t			portn;
42333859Sml29623 
42343859Sml29623 	portn = nxgep->mac.portnum;
42353859Sml29623 	statsp = (p_nxge_rdc_sys_stats_t)&nxgep->statsp->rdc_sys_stats;
42363859Sml29623 
42373859Sml29623 	if (ipp_status & (0x1 << portn)) {
42383859Sml29623 		statsp->ipp_eop_err++;
42393859Sml29623 		NXGE_FM_REPORT_ERROR(nxgep, portn, NULL,
42403859Sml29623 					NXGE_FM_EREPORT_RDMC_IPP_EOP_ERR);
42413859Sml29623 		rxport_fatal = B_TRUE;
42423859Sml29623 	}
42433859Sml29623 
42443859Sml29623 	if (zcp_status & (0x1 << portn)) {
42453859Sml29623 		statsp->zcp_eop_err++;
42463859Sml29623 		NXGE_FM_REPORT_ERROR(nxgep, portn, NULL,
42473859Sml29623 					NXGE_FM_EREPORT_RDMC_ZCP_EOP_ERR);
42483859Sml29623 		rxport_fatal = B_TRUE;
42493859Sml29623 	}
42503859Sml29623 
42513859Sml29623 	if (rxport_fatal) {
42523859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
42533859Sml29623 			    " nxge_rxdma_handle_port_error: "
42543859Sml29623 			    " fatal error on Port #%d\n",
42553859Sml29623 				portn));
42563859Sml29623 		status = nxge_rx_port_fatal_err_recover(nxgep);
42573859Sml29623 		if (status == NXGE_OK) {
42583859Sml29623 			FM_SERVICE_RESTORED(nxgep);
42593859Sml29623 		}
42603859Sml29623 	}
42613859Sml29623 
42623859Sml29623 	return (status);
42633859Sml29623 }
42643859Sml29623 
42653859Sml29623 static nxge_status_t
42663859Sml29623 nxge_rxdma_fatal_err_recover(p_nxge_t nxgep, uint16_t channel)
42673859Sml29623 {
42683859Sml29623 	npi_handle_t		handle;
42693859Sml29623 	npi_status_t		rs = NPI_SUCCESS;
42703859Sml29623 	nxge_status_t		status = NXGE_OK;
42713859Sml29623 	p_rx_rbr_ring_t		rbrp;
42723859Sml29623 	p_rx_rcr_ring_t		rcrp;
42733859Sml29623 	p_rx_mbox_t		mboxp;
42743859Sml29623 	rx_dma_ent_msk_t	ent_mask;
42753859Sml29623 	p_nxge_dma_common_t	dmap;
42763859Sml29623 	int			ring_idx;
42773859Sml29623 	uint32_t		ref_cnt;
42783859Sml29623 	p_rx_msg_t		rx_msg_p;
42793859Sml29623 	int			i;
42803859Sml29623 	uint32_t		nxge_port_rcr_size;
42813859Sml29623 
42823859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "<== nxge_rxdma_fatal_err_recover"));
42833859Sml29623 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
42843859Sml29623 			"Recovering from RxDMAChannel#%d error...", channel));
42853859Sml29623 
42863859Sml29623 	/*
42873859Sml29623 	 * Stop the dma channel waits for the stop done.
42883859Sml29623 	 * If the stop done bit is not set, then create
42893859Sml29623 	 * an error.
42903859Sml29623 	 */
42913859Sml29623 
42923859Sml29623 	handle = NXGE_DEV_NPI_HANDLE(nxgep);
42933859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "Rx DMA stop..."));
42943859Sml29623 
42953859Sml29623 	ring_idx = nxge_rxdma_get_ring_index(nxgep, channel);
42963859Sml29623 	rbrp = (p_rx_rbr_ring_t)nxgep->rx_rbr_rings->rbr_rings[ring_idx];
42973859Sml29623 	rcrp = (p_rx_rcr_ring_t)nxgep->rx_rcr_rings->rcr_rings[ring_idx];
42983859Sml29623 
42993859Sml29623 	MUTEX_ENTER(&rcrp->lock);
43003859Sml29623 	MUTEX_ENTER(&rbrp->lock);
43013859Sml29623 	MUTEX_ENTER(&rbrp->post_lock);
43023859Sml29623 
43033859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "Disable RxDMA channel..."));
43043859Sml29623 
43053859Sml29623 	rs = npi_rxdma_cfg_rdc_disable(handle, channel);
43063859Sml29623 	if (rs != NPI_SUCCESS) {
43073859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
43083859Sml29623 			"nxge_disable_rxdma_channel:failed"));
43093859Sml29623 		goto fail;
43103859Sml29623 	}
43113859Sml29623 
43123859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "Disable RxDMA interrupt..."));
43133859Sml29623 
43143859Sml29623 	/* Disable interrupt */
43153859Sml29623 	ent_mask.value = RX_DMA_ENT_MSK_ALL;
43163859Sml29623 	rs = npi_rxdma_event_mask(handle, OP_SET, channel, &ent_mask);
43173859Sml29623 	if (rs != NPI_SUCCESS) {
43183859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
43193859Sml29623 				"nxge_rxdma_stop_channel: "
43203859Sml29623 				"set rxdma event masks failed (channel %d)",
43213859Sml29623 				channel));
43223859Sml29623 	}
43233859Sml29623 
43243859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "RxDMA channel reset..."));
43253859Sml29623 
43263859Sml29623 	/* Reset RXDMA channel */
43273859Sml29623 	rs = npi_rxdma_cfg_rdc_reset(handle, channel);
43283859Sml29623 	if (rs != NPI_SUCCESS) {
43293859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
43303859Sml29623 			"nxge_rxdma_fatal_err_recover: "
43313859Sml29623 				" reset rxdma failed (channel %d)", channel));
43323859Sml29623 		goto fail;
43333859Sml29623 	}
43343859Sml29623 
43353859Sml29623 	nxge_port_rcr_size = nxgep->nxge_port_rcr_size;
43363859Sml29623 
43373859Sml29623 	mboxp =
43383859Sml29623 	(p_rx_mbox_t)nxgep->rx_mbox_areas_p->rxmbox_areas[ring_idx];
43393859Sml29623 
43403859Sml29623 	rbrp->rbr_wr_index = (rbrp->rbb_max - 1);
43413859Sml29623 	rbrp->rbr_rd_index = 0;
43423859Sml29623 
43433859Sml29623 	rcrp->comp_rd_index = 0;
43443859Sml29623 	rcrp->comp_wt_index = 0;
43453859Sml29623 	rcrp->rcr_desc_rd_head_p = rcrp->rcr_desc_first_p =
43463859Sml29623 		(p_rcr_entry_t)DMA_COMMON_VPTR(rcrp->rcr_desc);
43473859Sml29623 	rcrp->rcr_desc_rd_head_pp = rcrp->rcr_desc_first_pp =
43485125Sjoycey #if defined(__i386)
43495125Sjoycey 		(p_rcr_entry_t)(uint32_t)DMA_COMMON_IOADDR(rcrp->rcr_desc);
43505125Sjoycey #else
43513859Sml29623 		(p_rcr_entry_t)DMA_COMMON_IOADDR(rcrp->rcr_desc);
43525125Sjoycey #endif
43533859Sml29623 
43543859Sml29623 	rcrp->rcr_desc_last_p = rcrp->rcr_desc_rd_head_p +
43553859Sml29623 		(nxge_port_rcr_size - 1);
43563859Sml29623 	rcrp->rcr_desc_last_pp = rcrp->rcr_desc_rd_head_pp +
43573859Sml29623 		(nxge_port_rcr_size - 1);
43583859Sml29623 
43593859Sml29623 	dmap = (p_nxge_dma_common_t)&rcrp->rcr_desc;
43603859Sml29623 	bzero((caddr_t)dmap->kaddrp, dmap->alength);
43613859Sml29623 
43623859Sml29623 	cmn_err(CE_NOTE, "!rbr entries = %d\n", rbrp->rbr_max_size);
43633859Sml29623 
43643859Sml29623 	for (i = 0; i < rbrp->rbr_max_size; i++) {
43653859Sml29623 		rx_msg_p = rbrp->rx_msg_ring[i];
43663859Sml29623 		ref_cnt = rx_msg_p->ref_cnt;
43673859Sml29623 		if (ref_cnt != 1) {
43683859Sml29623 			if (rx_msg_p->cur_usage_cnt !=
43693859Sml29623 					rx_msg_p->max_usage_cnt) {
43703859Sml29623 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
43713859Sml29623 						"buf[%d]: cur_usage_cnt = %d "
43723859Sml29623 						"max_usage_cnt = %d\n", i,
43733859Sml29623 						rx_msg_p->cur_usage_cnt,
43743859Sml29623 						rx_msg_p->max_usage_cnt));
43753859Sml29623 			} else {
43763859Sml29623 				/* Buffer can be re-posted */
43773859Sml29623 				rx_msg_p->free = B_TRUE;
43783859Sml29623 				rx_msg_p->cur_usage_cnt = 0;
43793859Sml29623 				rx_msg_p->max_usage_cnt = 0xbaddcafe;
43803859Sml29623 				rx_msg_p->pkt_buf_size = 0;
43813859Sml29623 			}
43823859Sml29623 		}
43833859Sml29623 	}
43843859Sml29623 
43853859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "RxDMA channel re-start..."));
43863859Sml29623 
43873859Sml29623 	status = nxge_rxdma_start_channel(nxgep, channel, rbrp, rcrp, mboxp);
43883859Sml29623 	if (status != NXGE_OK) {
43893859Sml29623 		goto fail;
43903859Sml29623 	}
43913859Sml29623 
43923859Sml29623 	MUTEX_EXIT(&rbrp->post_lock);
43933859Sml29623 	MUTEX_EXIT(&rbrp->lock);
43943859Sml29623 	MUTEX_EXIT(&rcrp->lock);
43953859Sml29623 
43963859Sml29623 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
43973859Sml29623 			"Recovery Successful, RxDMAChannel#%d Restored",
43983859Sml29623 			channel));
43993859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "==> nxge_rxdma_fatal_err_recover"));
44003859Sml29623 
44013859Sml29623 	return (NXGE_OK);
44023859Sml29623 fail:
44033859Sml29623 	MUTEX_EXIT(&rbrp->post_lock);
44043859Sml29623 	MUTEX_EXIT(&rbrp->lock);
44053859Sml29623 	MUTEX_EXIT(&rcrp->lock);
44063859Sml29623 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "Recovery failed"));
44073859Sml29623 
44083859Sml29623 	return (NXGE_ERROR | rs);
44093859Sml29623 }
44103859Sml29623 
44113859Sml29623 nxge_status_t
44123859Sml29623 nxge_rx_port_fatal_err_recover(p_nxge_t nxgep)
44133859Sml29623 {
44143859Sml29623 	nxge_status_t		status = NXGE_OK;
44153859Sml29623 	p_nxge_dma_common_t	*dma_buf_p;
44163859Sml29623 	uint16_t		channel;
44173859Sml29623 	int			ndmas;
44183859Sml29623 	int			i;
44193859Sml29623 
44203859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "<== nxge_rx_port_fatal_err_recover"));
44213859Sml29623 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
44223859Sml29623 				"Recovering from RxPort error..."));
44233859Sml29623 	/* Disable RxMAC */
44243859Sml29623 
44253859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "Disable RxMAC...\n"));
44263859Sml29623 	if (nxge_rx_mac_disable(nxgep) != NXGE_OK)
44273859Sml29623 		goto fail;
44283859Sml29623 
44293859Sml29623 	NXGE_DELAY(1000);
44303859Sml29623 
44313859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "Stop all RxDMA channels..."));
44323859Sml29623 
44333859Sml29623 	ndmas = nxgep->rx_buf_pool_p->ndmas;
44343859Sml29623 	dma_buf_p = nxgep->rx_buf_pool_p->dma_buf_pool_p;
44353859Sml29623 
44363859Sml29623 	for (i = 0; i < ndmas; i++) {
44373859Sml29623 		channel = ((p_nxge_dma_common_t)dma_buf_p[i])->dma_channel;
44383859Sml29623 		if (nxge_rxdma_fatal_err_recover(nxgep, channel) != NXGE_OK) {
44393859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
44403859Sml29623 					"Could not recover channel %d",
44413859Sml29623 					channel));
44423859Sml29623 		}
44433859Sml29623 	}
44443859Sml29623 
44453859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "Reset IPP..."));
44463859Sml29623 
44473859Sml29623 	/* Reset IPP */
44483859Sml29623 	if (nxge_ipp_reset(nxgep) != NXGE_OK) {
44493859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
44503859Sml29623 			"nxge_rx_port_fatal_err_recover: "
44513859Sml29623 			"Failed to reset IPP"));
44523859Sml29623 		goto fail;
44533859Sml29623 	}
44543859Sml29623 
44553859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "Reset RxMAC..."));
44563859Sml29623 
44573859Sml29623 	/* Reset RxMAC */
44583859Sml29623 	if (nxge_rx_mac_reset(nxgep) != NXGE_OK) {
44593859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
44603859Sml29623 			"nxge_rx_port_fatal_err_recover: "
44613859Sml29623 			"Failed to reset RxMAC"));
44623859Sml29623 		goto fail;
44633859Sml29623 	}
44643859Sml29623 
44653859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "Re-initialize IPP..."));
44663859Sml29623 
44673859Sml29623 	/* Re-Initialize IPP */
44683859Sml29623 	if (nxge_ipp_init(nxgep) != NXGE_OK) {
44693859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
44703859Sml29623 			"nxge_rx_port_fatal_err_recover: "
44713859Sml29623 			"Failed to init IPP"));
44723859Sml29623 		goto fail;
44733859Sml29623 	}
44743859Sml29623 
44753859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "Re-initialize RxMAC..."));
44763859Sml29623 
44773859Sml29623 	/* Re-Initialize RxMAC */
44783859Sml29623 	if ((status = nxge_rx_mac_init(nxgep)) != NXGE_OK) {
44793859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
44803859Sml29623 			"nxge_rx_port_fatal_err_recover: "
44813859Sml29623 			"Failed to reset RxMAC"));
44823859Sml29623 		goto fail;
44833859Sml29623 	}
44843859Sml29623 
44853859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "Re-enable RxMAC..."));
44863859Sml29623 
44873859Sml29623 	/* Re-enable RxMAC */
44883859Sml29623 	if ((status = nxge_rx_mac_enable(nxgep)) != NXGE_OK) {
44893859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
44903859Sml29623 			"nxge_rx_port_fatal_err_recover: "
44913859Sml29623 			"Failed to enable RxMAC"));
44923859Sml29623 		goto fail;
44933859Sml29623 	}
44943859Sml29623 
44953859Sml29623 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
44963859Sml29623 			"Recovery Successful, RxPort Restored"));
44973859Sml29623 
44983859Sml29623 	return (NXGE_OK);
44993859Sml29623 fail:
45003859Sml29623 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "Recovery failed"));
45013859Sml29623 	return (status);
45023859Sml29623 }
45033859Sml29623 
45043859Sml29623 void
45053859Sml29623 nxge_rxdma_inject_err(p_nxge_t nxgep, uint32_t err_id, uint8_t chan)
45063859Sml29623 {
45073859Sml29623 	rx_dma_ctl_stat_t	cs;
45083859Sml29623 	rx_ctl_dat_fifo_stat_t	cdfs;
45093859Sml29623 
45103859Sml29623 	switch (err_id) {
45113859Sml29623 	case NXGE_FM_EREPORT_RDMC_RCR_ACK_ERR:
45123859Sml29623 	case NXGE_FM_EREPORT_RDMC_DC_FIFO_ERR:
45133859Sml29623 	case NXGE_FM_EREPORT_RDMC_RCR_SHA_PAR:
45143859Sml29623 	case NXGE_FM_EREPORT_RDMC_RBR_PRE_PAR:
45153859Sml29623 	case NXGE_FM_EREPORT_RDMC_RBR_TMOUT:
45163859Sml29623 	case NXGE_FM_EREPORT_RDMC_RSP_CNT_ERR:
45173859Sml29623 	case NXGE_FM_EREPORT_RDMC_BYTE_EN_BUS:
45183859Sml29623 	case NXGE_FM_EREPORT_RDMC_RSP_DAT_ERR:
45193859Sml29623 	case NXGE_FM_EREPORT_RDMC_RCRINCON:
45203859Sml29623 	case NXGE_FM_EREPORT_RDMC_RCRFULL:
45213859Sml29623 	case NXGE_FM_EREPORT_RDMC_RBRFULL:
45223859Sml29623 	case NXGE_FM_EREPORT_RDMC_RBRLOGPAGE:
45233859Sml29623 	case NXGE_FM_EREPORT_RDMC_CFIGLOGPAGE:
45243859Sml29623 	case NXGE_FM_EREPORT_RDMC_CONFIG_ERR:
45253859Sml29623 		RXDMA_REG_READ64(nxgep->npi_handle, RX_DMA_CTL_STAT_DBG_REG,
45263859Sml29623 			chan, &cs.value);
45273859Sml29623 		if (err_id == NXGE_FM_EREPORT_RDMC_RCR_ACK_ERR)
45283859Sml29623 			cs.bits.hdw.rcr_ack_err = 1;
45293859Sml29623 		else if (err_id == NXGE_FM_EREPORT_RDMC_DC_FIFO_ERR)
45303859Sml29623 			cs.bits.hdw.dc_fifo_err = 1;
45313859Sml29623 		else if (err_id == NXGE_FM_EREPORT_RDMC_RCR_SHA_PAR)
45323859Sml29623 			cs.bits.hdw.rcr_sha_par = 1;
45333859Sml29623 		else if (err_id == NXGE_FM_EREPORT_RDMC_RBR_PRE_PAR)
45343859Sml29623 			cs.bits.hdw.rbr_pre_par = 1;
45353859Sml29623 		else if (err_id == NXGE_FM_EREPORT_RDMC_RBR_TMOUT)
45363859Sml29623 			cs.bits.hdw.rbr_tmout = 1;
45373859Sml29623 		else if (err_id == NXGE_FM_EREPORT_RDMC_RSP_CNT_ERR)
45383859Sml29623 			cs.bits.hdw.rsp_cnt_err = 1;
45393859Sml29623 		else if (err_id == NXGE_FM_EREPORT_RDMC_BYTE_EN_BUS)
45403859Sml29623 			cs.bits.hdw.byte_en_bus = 1;
45413859Sml29623 		else if (err_id == NXGE_FM_EREPORT_RDMC_RSP_DAT_ERR)
45423859Sml29623 			cs.bits.hdw.rsp_dat_err = 1;
45433859Sml29623 		else if (err_id == NXGE_FM_EREPORT_RDMC_CONFIG_ERR)
45443859Sml29623 			cs.bits.hdw.config_err = 1;
45453859Sml29623 		else if (err_id == NXGE_FM_EREPORT_RDMC_RCRINCON)
45463859Sml29623 			cs.bits.hdw.rcrincon = 1;
45473859Sml29623 		else if (err_id == NXGE_FM_EREPORT_RDMC_RCRFULL)
45483859Sml29623 			cs.bits.hdw.rcrfull = 1;
45493859Sml29623 		else if (err_id == NXGE_FM_EREPORT_RDMC_RBRFULL)
45503859Sml29623 			cs.bits.hdw.rbrfull = 1;
45513859Sml29623 		else if (err_id == NXGE_FM_EREPORT_RDMC_RBRLOGPAGE)
45523859Sml29623 			cs.bits.hdw.rbrlogpage = 1;
45533859Sml29623 		else if (err_id == NXGE_FM_EREPORT_RDMC_CFIGLOGPAGE)
45543859Sml29623 			cs.bits.hdw.cfiglogpage = 1;
45555125Sjoycey #if defined(__i386)
45565125Sjoycey 		cmn_err(CE_NOTE, "!Write 0x%llx to RX_DMA_CTL_STAT_DBG_REG\n",
45575125Sjoycey 				cs.value);
45585125Sjoycey #else
45593859Sml29623 		cmn_err(CE_NOTE, "!Write 0x%lx to RX_DMA_CTL_STAT_DBG_REG\n",
45603859Sml29623 				cs.value);
45615125Sjoycey #endif
45623859Sml29623 		RXDMA_REG_WRITE64(nxgep->npi_handle, RX_DMA_CTL_STAT_DBG_REG,
45633859Sml29623 			chan, cs.value);
45643859Sml29623 		break;
45653859Sml29623 	case NXGE_FM_EREPORT_RDMC_ID_MISMATCH:
45663859Sml29623 	case NXGE_FM_EREPORT_RDMC_ZCP_EOP_ERR:
45673859Sml29623 	case NXGE_FM_EREPORT_RDMC_IPP_EOP_ERR:
45683859Sml29623 		cdfs.value = 0;
45693859Sml29623 		if (err_id ==  NXGE_FM_EREPORT_RDMC_ID_MISMATCH)
45703859Sml29623 			cdfs.bits.ldw.id_mismatch = (1 << nxgep->mac.portnum);
45713859Sml29623 		else if (err_id == NXGE_FM_EREPORT_RDMC_ZCP_EOP_ERR)
45723859Sml29623 			cdfs.bits.ldw.zcp_eop_err = (1 << nxgep->mac.portnum);
45733859Sml29623 		else if (err_id == NXGE_FM_EREPORT_RDMC_IPP_EOP_ERR)
45743859Sml29623 			cdfs.bits.ldw.ipp_eop_err = (1 << nxgep->mac.portnum);
45755125Sjoycey #if defined(__i386)
45765125Sjoycey 		cmn_err(CE_NOTE,
45775125Sjoycey 			"!Write 0x%llx to RX_CTL_DAT_FIFO_STAT_DBG_REG\n",
45785125Sjoycey 			cdfs.value);
45795125Sjoycey #else
45803859Sml29623 		cmn_err(CE_NOTE,
45813859Sml29623 			"!Write 0x%lx to RX_CTL_DAT_FIFO_STAT_DBG_REG\n",
45823859Sml29623 			cdfs.value);
45835125Sjoycey #endif
45843859Sml29623 		RXDMA_REG_WRITE64(nxgep->npi_handle,
45853859Sml29623 			RX_CTL_DAT_FIFO_STAT_DBG_REG, chan, cdfs.value);
45863859Sml29623 		break;
45873859Sml29623 	case NXGE_FM_EREPORT_RDMC_DCF_ERR:
45883859Sml29623 		break;
45895165Syc148097 	case NXGE_FM_EREPORT_RDMC_RCR_ERR:
45903859Sml29623 		break;
45913859Sml29623 	}
45923859Sml29623 }
4593