xref: /onnv-gate/usr/src/uts/common/io/nxge/nxge_main.c (revision 6909:409b8d628a42)
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 /*
225770Sml29623  * 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 /*
293859Sml29623  * SunOs MT STREAMS NIU/Neptune 10Gb Ethernet Device Driver.
303859Sml29623  */
313859Sml29623 #include	<sys/nxge/nxge_impl.h>
326495Sspeer #include	<sys/nxge/nxge_hio.h>
336495Sspeer #include	<sys/nxge/nxge_rxdma.h>
343859Sml29623 #include	<sys/pcie.h>
353859Sml29623 
363859Sml29623 uint32_t 	nxge_use_partition = 0;		/* debug partition flag */
373859Sml29623 uint32_t 	nxge_dma_obp_props_only = 1;	/* use obp published props */
383859Sml29623 uint32_t 	nxge_use_rdc_intr = 1;		/* debug to assign rdc intr */
393859Sml29623 /*
405013Sml29623  * PSARC/2007/453 MSI-X interrupt limit override
415013Sml29623  * (This PSARC case is limited to MSI-X vectors
425013Sml29623  *  and SPARC platforms only).
433859Sml29623  */
445013Sml29623 #if defined(_BIG_ENDIAN)
455013Sml29623 uint32_t	nxge_msi_enable = 2;
465013Sml29623 #else
475013Sml29623 uint32_t	nxge_msi_enable = 1;
485013Sml29623 #endif
493859Sml29623 
506611Sml29623 /*
516705Sml29623  * Software workaround for a Neptune (PCI-E)
526705Sml29623  * hardware interrupt bug which the hardware
536705Sml29623  * may generate spurious interrupts after the
546705Sml29623  * device interrupt handler was removed. If this flag
556705Sml29623  * is enabled, the driver will reset the
566705Sml29623  * hardware when devices are being detached.
576705Sml29623  */
586705Sml29623 uint32_t	nxge_peu_reset_enable = 0;
596705Sml29623 
606705Sml29623 /*
616611Sml29623  * Software workaround for the hardware
626611Sml29623  * checksum bugs that affect packet transmission
636611Sml29623  * and receive:
646611Sml29623  *
656611Sml29623  * Usage of nxge_cksum_offload:
666611Sml29623  *
676611Sml29623  *  (1) nxge_cksum_offload = 0 (default):
686611Sml29623  *	- transmits packets:
696611Sml29623  *	  TCP: uses the hardware checksum feature.
706611Sml29623  *	  UDP: driver will compute the software checksum
716611Sml29623  *	       based on the partial checksum computed
726611Sml29623  *	       by the IP layer.
736611Sml29623  *	- receives packets
746611Sml29623  *	  TCP: marks packets checksum flags based on hardware result.
756611Sml29623  *	  UDP: will not mark checksum flags.
766611Sml29623  *
776611Sml29623  *  (2) nxge_cksum_offload = 1:
786611Sml29623  *	- transmit packets:
796611Sml29623  *	  TCP/UDP: uses the hardware checksum feature.
806611Sml29623  *	- receives packets
816611Sml29623  *	  TCP/UDP: marks packet checksum flags based on hardware result.
826611Sml29623  *
836611Sml29623  *  (3) nxge_cksum_offload = 2:
846611Sml29623  *	- The driver will not register its checksum capability.
856611Sml29623  *	  Checksum for both TCP and UDP will be computed
866611Sml29623  *	  by the stack.
876611Sml29623  *	- The software LSO is not allowed in this case.
886611Sml29623  *
896611Sml29623  *  (4) nxge_cksum_offload > 2:
906611Sml29623  *	- Will be treated as it is set to 2
916611Sml29623  *	  (stack will compute the checksum).
926611Sml29623  *
936611Sml29623  *  (5) If the hardware bug is fixed, this workaround
946611Sml29623  *	needs to be updated accordingly to reflect
956611Sml29623  *	the new hardware revision.
966611Sml29623  */
976611Sml29623 uint32_t	nxge_cksum_offload = 0;
986495Sspeer 
993859Sml29623 /*
1003859Sml29623  * Globals: tunable parameters (/etc/system or adb)
1013859Sml29623  *
1023859Sml29623  */
1033859Sml29623 uint32_t 	nxge_rbr_size = NXGE_RBR_RBB_DEFAULT;
1043859Sml29623 uint32_t 	nxge_rbr_spare_size = 0;
1053859Sml29623 uint32_t 	nxge_rcr_size = NXGE_RCR_DEFAULT;
1063859Sml29623 uint32_t 	nxge_tx_ring_size = NXGE_TX_RING_DEFAULT;
1074193Sspeer boolean_t 	nxge_no_msg = B_TRUE;		/* control message display */
1083859Sml29623 uint32_t 	nxge_no_link_notify = 0;	/* control DL_NOTIFY */
1093859Sml29623 uint32_t 	nxge_bcopy_thresh = TX_BCOPY_MAX;
1103859Sml29623 uint32_t 	nxge_dvma_thresh = TX_FASTDVMA_MIN;
1113859Sml29623 uint32_t 	nxge_dma_stream_thresh = TX_STREAM_MIN;
1123859Sml29623 uint32_t	nxge_jumbo_mtu	= TX_JUMBO_MTU;
1133859Sml29623 boolean_t	nxge_jumbo_enable = B_FALSE;
1143859Sml29623 uint16_t	nxge_rcr_timeout = NXGE_RDC_RCR_TIMEOUT;
1153859Sml29623 uint16_t	nxge_rcr_threshold = NXGE_RDC_RCR_THRESHOLD;
1163952Sml29623 nxge_tx_mode_t	nxge_tx_scheme = NXGE_USE_SERIAL;
1173859Sml29623 
1185770Sml29623 /* MAX LSO size */
1195770Sml29623 #define		NXGE_LSO_MAXLEN	65535
1205770Sml29623 uint32_t	nxge_lso_max = NXGE_LSO_MAXLEN;
1215770Sml29623 
1223859Sml29623 /*
1233859Sml29623  * Debugging flags:
1243859Sml29623  *		nxge_no_tx_lb : transmit load balancing
1253859Sml29623  *		nxge_tx_lb_policy: 0 - TCP port (default)
1263859Sml29623  *				   3 - DEST MAC
1273859Sml29623  */
1283859Sml29623 uint32_t 	nxge_no_tx_lb = 0;
1293859Sml29623 uint32_t 	nxge_tx_lb_policy = NXGE_TX_LB_TCPUDP;
1303859Sml29623 
1313859Sml29623 /*
1323859Sml29623  * Add tunable to reduce the amount of time spent in the
1333859Sml29623  * ISR doing Rx Processing.
1343859Sml29623  */
1353859Sml29623 uint32_t nxge_max_rx_pkts = 1024;
1363859Sml29623 
1373859Sml29623 /*
1383859Sml29623  * Tunables to manage the receive buffer blocks.
1393859Sml29623  *
1403859Sml29623  * nxge_rx_threshold_hi: copy all buffers.
1413859Sml29623  * nxge_rx_bcopy_size_type: receive buffer block size type.
1423859Sml29623  * nxge_rx_threshold_lo: copy only up to tunable block size type.
1433859Sml29623  */
1443859Sml29623 nxge_rxbuf_threshold_t nxge_rx_threshold_hi = NXGE_RX_COPY_6;
1453859Sml29623 nxge_rxbuf_type_t nxge_rx_buf_size_type = RCR_PKTBUFSZ_0;
1463859Sml29623 nxge_rxbuf_threshold_t nxge_rx_threshold_lo = NXGE_RX_COPY_3;
1473859Sml29623 
1486495Sspeer /* Use kmem_alloc() to allocate data buffers. */
149*6909Sml29623 #if defined(_BIG_ENDIAN)
1506498Sspeer uint32_t	nxge_use_kmem_alloc = 1;
1516495Sspeer #else
1526498Sspeer uint32_t	nxge_use_kmem_alloc = 0;
1536495Sspeer #endif
1546495Sspeer 
1553859Sml29623 rtrace_t npi_rtracebuf;
1563859Sml29623 
1573859Sml29623 #if	defined(sun4v)
1583859Sml29623 /*
1593859Sml29623  * Hypervisor N2/NIU services information.
1603859Sml29623  */
1613859Sml29623 static hsvc_info_t niu_hsvc = {
1623859Sml29623 	HSVC_REV_1, NULL, HSVC_GROUP_NIU, NIU_MAJOR_VER,
1633859Sml29623 	NIU_MINOR_VER, "nxge"
1643859Sml29623 };
1656495Sspeer 
1666495Sspeer static int nxge_hsvc_register(p_nxge_t);
1673859Sml29623 #endif
1683859Sml29623 
1693859Sml29623 /*
1703859Sml29623  * Function Prototypes
1713859Sml29623  */
1723859Sml29623 static int nxge_attach(dev_info_t *, ddi_attach_cmd_t);
1733859Sml29623 static int nxge_detach(dev_info_t *, ddi_detach_cmd_t);
1743859Sml29623 static void nxge_unattach(p_nxge_t);
1753859Sml29623 
1763859Sml29623 #if NXGE_PROPERTY
1773859Sml29623 static void nxge_remove_hard_properties(p_nxge_t);
1783859Sml29623 #endif
1793859Sml29623 
1806495Sspeer /*
1816495Sspeer  * These two functions are required by nxge_hio.c
1826495Sspeer  */
1836495Sspeer extern int nxge_m_mmac_add(void *arg, mac_multi_addr_t *maddr);
1846495Sspeer extern int nxge_m_mmac_remove(void *arg, mac_addr_slot_t slot);
1856495Sspeer 
1863859Sml29623 static nxge_status_t nxge_setup_system_dma_pages(p_nxge_t);
1873859Sml29623 
1883859Sml29623 static nxge_status_t nxge_setup_mutexes(p_nxge_t);
1893859Sml29623 static void nxge_destroy_mutexes(p_nxge_t);
1903859Sml29623 
1913859Sml29623 static nxge_status_t nxge_map_regs(p_nxge_t nxgep);
1923859Sml29623 static void nxge_unmap_regs(p_nxge_t nxgep);
1933859Sml29623 #ifdef	NXGE_DEBUG
1943859Sml29623 static void nxge_test_map_regs(p_nxge_t nxgep);
1953859Sml29623 #endif
1963859Sml29623 
1973859Sml29623 static nxge_status_t nxge_add_intrs(p_nxge_t nxgep);
1983859Sml29623 static nxge_status_t nxge_add_soft_intrs(p_nxge_t nxgep);
1993859Sml29623 static void nxge_remove_intrs(p_nxge_t nxgep);
2003859Sml29623 static void nxge_remove_soft_intrs(p_nxge_t nxgep);
2013859Sml29623 
2023859Sml29623 static nxge_status_t nxge_add_intrs_adv(p_nxge_t nxgep);
2033859Sml29623 static nxge_status_t nxge_add_intrs_adv_type(p_nxge_t, uint32_t);
2043859Sml29623 static nxge_status_t nxge_add_intrs_adv_type_fix(p_nxge_t, uint32_t);
2053859Sml29623 static void nxge_intrs_enable(p_nxge_t nxgep);
2063859Sml29623 static void nxge_intrs_disable(p_nxge_t nxgep);
2073859Sml29623 
2083859Sml29623 static void nxge_suspend(p_nxge_t);
2093859Sml29623 static nxge_status_t nxge_resume(p_nxge_t);
2103859Sml29623 
2113859Sml29623 static nxge_status_t nxge_setup_dev(p_nxge_t);
2123859Sml29623 static void nxge_destroy_dev(p_nxge_t);
2133859Sml29623 
2143859Sml29623 static nxge_status_t nxge_alloc_mem_pool(p_nxge_t);
2153859Sml29623 static void nxge_free_mem_pool(p_nxge_t);
2163859Sml29623 
2176495Sspeer nxge_status_t nxge_alloc_rx_mem_pool(p_nxge_t);
2183859Sml29623 static void nxge_free_rx_mem_pool(p_nxge_t);
2193859Sml29623 
2206495Sspeer nxge_status_t nxge_alloc_tx_mem_pool(p_nxge_t);
2213859Sml29623 static void nxge_free_tx_mem_pool(p_nxge_t);
2223859Sml29623 
2233859Sml29623 static nxge_status_t nxge_dma_mem_alloc(p_nxge_t, dma_method_t,
2243859Sml29623 	struct ddi_dma_attr *,
2253859Sml29623 	size_t, ddi_device_acc_attr_t *, uint_t,
2263859Sml29623 	p_nxge_dma_common_t);
2273859Sml29623 
2283859Sml29623 static void nxge_dma_mem_free(p_nxge_dma_common_t);
2296495Sspeer static void nxge_dma_free_rx_data_buf(p_nxge_dma_common_t);
2303859Sml29623 
2313859Sml29623 static nxge_status_t nxge_alloc_rx_buf_dma(p_nxge_t, uint16_t,
2323859Sml29623 	p_nxge_dma_common_t *, size_t, size_t, uint32_t *);
2333859Sml29623 static void nxge_free_rx_buf_dma(p_nxge_t, p_nxge_dma_common_t, uint32_t);
2343859Sml29623 
2353859Sml29623 static nxge_status_t nxge_alloc_rx_cntl_dma(p_nxge_t, uint16_t,
2363859Sml29623 	p_nxge_dma_common_t *, size_t);
2373859Sml29623 static void nxge_free_rx_cntl_dma(p_nxge_t, p_nxge_dma_common_t);
2383859Sml29623 
2396495Sspeer extern nxge_status_t nxge_alloc_tx_buf_dma(p_nxge_t, uint16_t,
2403859Sml29623 	p_nxge_dma_common_t *, size_t, size_t, uint32_t *);
2413859Sml29623 static void nxge_free_tx_buf_dma(p_nxge_t, p_nxge_dma_common_t, uint32_t);
2423859Sml29623 
2436495Sspeer extern nxge_status_t nxge_alloc_tx_cntl_dma(p_nxge_t, uint16_t,
2443859Sml29623 	p_nxge_dma_common_t *,
2453859Sml29623 	size_t);
2463859Sml29623 static void nxge_free_tx_cntl_dma(p_nxge_t, p_nxge_dma_common_t);
2473859Sml29623 
2483859Sml29623 static int nxge_init_common_dev(p_nxge_t);
2493859Sml29623 static void nxge_uninit_common_dev(p_nxge_t);
2506512Ssowmini extern int nxge_param_set_mac(p_nxge_t, queue_t *, mblk_t *,
2516512Ssowmini     char *, caddr_t);
2523859Sml29623 
2533859Sml29623 /*
2543859Sml29623  * The next declarations are for the GLDv3 interface.
2553859Sml29623  */
2563859Sml29623 static int nxge_m_start(void *);
2573859Sml29623 static void nxge_m_stop(void *);
2583859Sml29623 static int nxge_m_unicst(void *, const uint8_t *);
2593859Sml29623 static int nxge_m_multicst(void *, boolean_t, const uint8_t *);
2603859Sml29623 static int nxge_m_promisc(void *, boolean_t);
2613859Sml29623 static void nxge_m_ioctl(void *, queue_t *, mblk_t *);
2623859Sml29623 static void nxge_m_resources(void *);
2633859Sml29623 mblk_t *nxge_m_tx(void *arg, mblk_t *);
2643859Sml29623 static nxge_status_t nxge_mac_register(p_nxge_t);
2653859Sml29623 static int nxge_altmac_set(p_nxge_t nxgep, uint8_t *mac_addr,
2663859Sml29623 	mac_addr_slot_t slot);
2676495Sspeer void nxge_mmac_kstat_update(p_nxge_t nxgep, mac_addr_slot_t slot,
2683859Sml29623 	boolean_t factory);
2693859Sml29623 static int nxge_m_mmac_reserve(void *arg, mac_multi_addr_t *maddr);
2703859Sml29623 static int nxge_m_mmac_modify(void *arg, mac_multi_addr_t *maddr);
2713859Sml29623 static int nxge_m_mmac_get(void *arg, mac_multi_addr_t *maddr);
2726439Sml29623 static	boolean_t nxge_m_getcapab(void *, mac_capab_t, void *);
2736439Sml29623 static int nxge_m_setprop(void *, const char *, mac_prop_id_t,
2746439Sml29623     uint_t, const void *);
2756439Sml29623 static int nxge_m_getprop(void *, const char *, mac_prop_id_t,
2766512Ssowmini     uint_t, uint_t, void *);
2776439Sml29623 static int nxge_set_priv_prop(nxge_t *, const char *, uint_t,
2786439Sml29623     const void *);
2796512Ssowmini static int nxge_get_priv_prop(nxge_t *, const char *, uint_t, uint_t,
2806439Sml29623     void *);
2816512Ssowmini static int nxge_get_def_val(nxge_t *, mac_prop_id_t, uint_t, void *);
2826512Ssowmini 
2836705Sml29623 static void nxge_niu_peu_reset(p_nxge_t nxgep);
2846512Ssowmini 
2856512Ssowmini mac_priv_prop_t nxge_priv_props[] = {
2866512Ssowmini 	{"_adv_10gfdx_cap", MAC_PROP_PERM_RW},
2876512Ssowmini 	{"_adv_pause_cap", MAC_PROP_PERM_RW},
2886512Ssowmini 	{"_function_number", MAC_PROP_PERM_READ},
2896512Ssowmini 	{"_fw_version", MAC_PROP_PERM_READ},
2906512Ssowmini 	{"_port_mode", MAC_PROP_PERM_READ},
2916512Ssowmini 	{"_hot_swap_phy", MAC_PROP_PERM_READ},
2926512Ssowmini 	{"_accept_jumbo", MAC_PROP_PERM_RW},
2936512Ssowmini 	{"_rxdma_intr_time", MAC_PROP_PERM_RW},
2946512Ssowmini 	{"_rxdma_intr_pkts", MAC_PROP_PERM_RW},
2956512Ssowmini 	{"_class_opt_ipv4_tcp", MAC_PROP_PERM_RW},
2966512Ssowmini 	{"_class_opt_ipv4_udp", MAC_PROP_PERM_RW},
2976512Ssowmini 	{"_class_opt_ipv4_ah", MAC_PROP_PERM_RW},
2986512Ssowmini 	{"_class_opt_ipv4_sctp", MAC_PROP_PERM_RW},
2996512Ssowmini 	{"_class_opt_ipv6_tcp", MAC_PROP_PERM_RW},
3006512Ssowmini 	{"_class_opt_ipv6_udp", MAC_PROP_PERM_RW},
3016512Ssowmini 	{"_class_opt_ipv6_ah", MAC_PROP_PERM_RW},
3026512Ssowmini 	{"_class_opt_ipv6_sctp", MAC_PROP_PERM_RW},
3036512Ssowmini 	{"_soft_lso_enable", MAC_PROP_PERM_RW}
3046512Ssowmini };
3056512Ssowmini 
3066512Ssowmini #define	NXGE_MAX_PRIV_PROPS	\
3076512Ssowmini 	(sizeof (nxge_priv_props)/sizeof (mac_priv_prop_t))
3086439Sml29623 
3096439Sml29623 #define	NXGE_M_CALLBACK_FLAGS\
3106439Sml29623 	(MC_RESOURCES | MC_IOCTL | MC_GETCAPAB | MC_SETPROP | MC_GETPROP)
3116439Sml29623 
3123859Sml29623 
3133859Sml29623 #define	NXGE_NEPTUNE_MAGIC	0x4E584745UL
3143859Sml29623 #define	MAX_DUMP_SZ 256
3153859Sml29623 
3166439Sml29623 #define	NXGE_M_CALLBACK_FLAGS	\
3176439Sml29623 	(MC_RESOURCES | MC_IOCTL | MC_GETCAPAB | MC_SETPROP | MC_GETPROP)
3186439Sml29623 
3196495Sspeer mac_callbacks_t nxge_m_callbacks = {
3203859Sml29623 	NXGE_M_CALLBACK_FLAGS,
3213859Sml29623 	nxge_m_stat,
3223859Sml29623 	nxge_m_start,
3233859Sml29623 	nxge_m_stop,
3243859Sml29623 	nxge_m_promisc,
3253859Sml29623 	nxge_m_multicst,
3263859Sml29623 	nxge_m_unicst,
3273859Sml29623 	nxge_m_tx,
3283859Sml29623 	nxge_m_resources,
3293859Sml29623 	nxge_m_ioctl,
3306439Sml29623 	nxge_m_getcapab,
3316439Sml29623 	NULL,
3326439Sml29623 	NULL,
3336439Sml29623 	nxge_m_setprop,
3346439Sml29623 	nxge_m_getprop
3353859Sml29623 };
3363859Sml29623 
3373859Sml29623 void
3383859Sml29623 nxge_err_inject(p_nxge_t, queue_t *, mblk_t *);
3393859Sml29623 
3405013Sml29623 /* PSARC/2007/453 MSI-X interrupt limit override. */
3415013Sml29623 #define	NXGE_MSIX_REQUEST_10G	8
3425013Sml29623 #define	NXGE_MSIX_REQUEST_1G	2
3435013Sml29623 static int nxge_create_msi_property(p_nxge_t);
3445013Sml29623 
3453859Sml29623 /*
3463859Sml29623  * These global variables control the message
3473859Sml29623  * output.
3483859Sml29623  */
3493859Sml29623 out_dbgmsg_t nxge_dbgmsg_out = DBG_CONSOLE | STR_LOG;
3506495Sspeer uint64_t nxge_debug_level;
3513859Sml29623 
3523859Sml29623 /*
3533859Sml29623  * This list contains the instance structures for the Neptune
3543859Sml29623  * devices present in the system. The lock exists to guarantee
3553859Sml29623  * mutually exclusive access to the list.
3563859Sml29623  */
3573859Sml29623 void 			*nxge_list = NULL;
3583859Sml29623 
3593859Sml29623 void			*nxge_hw_list = NULL;
3603859Sml29623 nxge_os_mutex_t 	nxge_common_lock;
3613859Sml29623 
3623859Sml29623 extern uint64_t 	npi_debug_level;
3633859Sml29623 
3643859Sml29623 extern nxge_status_t	nxge_ldgv_init(p_nxge_t, int *, int *);
3653859Sml29623 extern nxge_status_t	nxge_ldgv_init_n2(p_nxge_t, int *, int *);
3663859Sml29623 extern nxge_status_t	nxge_ldgv_uninit(p_nxge_t);
3673859Sml29623 extern nxge_status_t	nxge_intr_ldgv_init(p_nxge_t);
3683859Sml29623 extern void		nxge_fm_init(p_nxge_t,
3693859Sml29623 					ddi_device_acc_attr_t *,
3703859Sml29623 					ddi_device_acc_attr_t *,
3713859Sml29623 					ddi_dma_attr_t *);
3723859Sml29623 extern void		nxge_fm_fini(p_nxge_t);
3733859Sml29623 extern npi_status_t	npi_mac_altaddr_disable(npi_handle_t, uint8_t, uint8_t);
3743859Sml29623 
3753859Sml29623 /*
3763859Sml29623  * Count used to maintain the number of buffers being used
3773859Sml29623  * by Neptune instances and loaned up to the upper layers.
3783859Sml29623  */
3793859Sml29623 uint32_t nxge_mblks_pending = 0;
3803859Sml29623 
3813859Sml29623 /*
3823859Sml29623  * Device register access attributes for PIO.
3833859Sml29623  */
3843859Sml29623 static ddi_device_acc_attr_t nxge_dev_reg_acc_attr = {
3853859Sml29623 	DDI_DEVICE_ATTR_V0,
3863859Sml29623 	DDI_STRUCTURE_LE_ACC,
3873859Sml29623 	DDI_STRICTORDER_ACC,
3883859Sml29623 };
3893859Sml29623 
3903859Sml29623 /*
3913859Sml29623  * Device descriptor access attributes for DMA.
3923859Sml29623  */
3933859Sml29623 static ddi_device_acc_attr_t nxge_dev_desc_dma_acc_attr = {
3943859Sml29623 	DDI_DEVICE_ATTR_V0,
3953859Sml29623 	DDI_STRUCTURE_LE_ACC,
3963859Sml29623 	DDI_STRICTORDER_ACC
3973859Sml29623 };
3983859Sml29623 
3993859Sml29623 /*
4003859Sml29623  * Device buffer access attributes for DMA.
4013859Sml29623  */
4023859Sml29623 static ddi_device_acc_attr_t nxge_dev_buf_dma_acc_attr = {
4033859Sml29623 	DDI_DEVICE_ATTR_V0,
4043859Sml29623 	DDI_STRUCTURE_BE_ACC,
4053859Sml29623 	DDI_STRICTORDER_ACC
4063859Sml29623 };
4073859Sml29623 
4083859Sml29623 ddi_dma_attr_t nxge_desc_dma_attr = {
4093859Sml29623 	DMA_ATTR_V0,		/* version number. */
4103859Sml29623 	0,			/* low address */
4113859Sml29623 	0xffffffffffffffff,	/* high address */
4123859Sml29623 	0xffffffffffffffff,	/* address counter max */
4133859Sml29623 #ifndef NIU_PA_WORKAROUND
4143859Sml29623 	0x100000,		/* alignment */
4153859Sml29623 #else
4163859Sml29623 	0x2000,
4173859Sml29623 #endif
4183859Sml29623 	0xfc00fc,		/* dlim_burstsizes */
4193859Sml29623 	0x1,			/* minimum transfer size */
4203859Sml29623 	0xffffffffffffffff,	/* maximum transfer size */
4213859Sml29623 	0xffffffffffffffff,	/* maximum segment size */
4223859Sml29623 	1,			/* scatter/gather list length */
4233859Sml29623 	(unsigned int) 1,	/* granularity */
4243859Sml29623 	0			/* attribute flags */
4253859Sml29623 };
4263859Sml29623 
4273859Sml29623 ddi_dma_attr_t nxge_tx_dma_attr = {
4283859Sml29623 	DMA_ATTR_V0,		/* version number. */
4293859Sml29623 	0,			/* low address */
4303859Sml29623 	0xffffffffffffffff,	/* high address */
4313859Sml29623 	0xffffffffffffffff,	/* address counter max */
4323859Sml29623 #if defined(_BIG_ENDIAN)
4333859Sml29623 	0x2000,			/* alignment */
4343859Sml29623 #else
4353859Sml29623 	0x1000,			/* alignment */
4363859Sml29623 #endif
4373859Sml29623 	0xfc00fc,		/* dlim_burstsizes */
4383859Sml29623 	0x1,			/* minimum transfer size */
4393859Sml29623 	0xffffffffffffffff,	/* maximum transfer size */
4403859Sml29623 	0xffffffffffffffff,	/* maximum segment size */
4413859Sml29623 	5,			/* scatter/gather list length */
4423859Sml29623 	(unsigned int) 1,	/* granularity */
4433859Sml29623 	0			/* attribute flags */
4443859Sml29623 };
4453859Sml29623 
4463859Sml29623 ddi_dma_attr_t nxge_rx_dma_attr = {
4473859Sml29623 	DMA_ATTR_V0,		/* version number. */
4483859Sml29623 	0,			/* low address */
4493859Sml29623 	0xffffffffffffffff,	/* high address */
4503859Sml29623 	0xffffffffffffffff,	/* address counter max */
4513859Sml29623 	0x2000,			/* alignment */
4523859Sml29623 	0xfc00fc,		/* dlim_burstsizes */
4533859Sml29623 	0x1,			/* minimum transfer size */
4543859Sml29623 	0xffffffffffffffff,	/* maximum transfer size */
4553859Sml29623 	0xffffffffffffffff,	/* maximum segment size */
4563859Sml29623 	1,			/* scatter/gather list length */
4573859Sml29623 	(unsigned int) 1,	/* granularity */
4584781Ssbehera 	DDI_DMA_RELAXED_ORDERING /* attribute flags */
4593859Sml29623 };
4603859Sml29623 
4613859Sml29623 ddi_dma_lim_t nxge_dma_limits = {
4623859Sml29623 	(uint_t)0,		/* dlim_addr_lo */
4633859Sml29623 	(uint_t)0xffffffff,	/* dlim_addr_hi */
4643859Sml29623 	(uint_t)0xffffffff,	/* dlim_cntr_max */
4653859Sml29623 	(uint_t)0xfc00fc,	/* dlim_burstsizes for 32 and 64 bit xfers */
4663859Sml29623 	0x1,			/* dlim_minxfer */
4673859Sml29623 	1024			/* dlim_speed */
4683859Sml29623 };
4693859Sml29623 
4703859Sml29623 dma_method_t nxge_force_dma = DVMA;
4713859Sml29623 
4723859Sml29623 /*
4733859Sml29623  * dma chunk sizes.
4743859Sml29623  *
4753859Sml29623  * Try to allocate the largest possible size
4763859Sml29623  * so that fewer number of dma chunks would be managed
4773859Sml29623  */
4783859Sml29623 #ifdef NIU_PA_WORKAROUND
4793859Sml29623 size_t alloc_sizes [] = {0x2000};
4803859Sml29623 #else
4813859Sml29623 size_t alloc_sizes [] = {0x1000, 0x2000, 0x4000, 0x8000,
4823859Sml29623 		0x10000, 0x20000, 0x40000, 0x80000,
4835770Sml29623 		0x100000, 0x200000, 0x400000, 0x800000,
4845770Sml29623 		0x1000000, 0x2000000, 0x4000000};
4853859Sml29623 #endif
4863859Sml29623 
4873859Sml29623 /*
4883859Sml29623  * Translate "dev_t" to a pointer to the associated "dev_info_t".
4893859Sml29623  */
4903859Sml29623 
4916495Sspeer extern void nxge_get_environs(nxge_t *);
4926495Sspeer 
4933859Sml29623 static int
4943859Sml29623 nxge_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4953859Sml29623 {
4963859Sml29623 	p_nxge_t	nxgep = NULL;
4973859Sml29623 	int		instance;
4983859Sml29623 	int		status = DDI_SUCCESS;
4993859Sml29623 	uint8_t		portn;
5003859Sml29623 	nxge_mmac_t	*mmac_info;
5013859Sml29623 
5023859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_attach"));
5033859Sml29623 
5043859Sml29623 	/*
5053859Sml29623 	 * Get the device instance since we'll need to setup
5063859Sml29623 	 * or retrieve a soft state for this instance.
5073859Sml29623 	 */
5083859Sml29623 	instance = ddi_get_instance(dip);
5093859Sml29623 
5103859Sml29623 	switch (cmd) {
5113859Sml29623 	case DDI_ATTACH:
5123859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_ATTACH"));
5133859Sml29623 		break;
5143859Sml29623 
5153859Sml29623 	case DDI_RESUME:
5163859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_RESUME"));
5173859Sml29623 		nxgep = (p_nxge_t)ddi_get_soft_state(nxge_list, instance);
5183859Sml29623 		if (nxgep == NULL) {
5193859Sml29623 			status = DDI_FAILURE;
5203859Sml29623 			break;
5213859Sml29623 		}
5223859Sml29623 		if (nxgep->dip != dip) {
5233859Sml29623 			status = DDI_FAILURE;
5243859Sml29623 			break;
5253859Sml29623 		}
5263859Sml29623 		if (nxgep->suspended == DDI_PM_SUSPEND) {
5273859Sml29623 			status = ddi_dev_is_needed(nxgep->dip, 0, 1);
5283859Sml29623 		} else {
5294185Sspeer 			status = nxge_resume(nxgep);
5303859Sml29623 		}
5313859Sml29623 		goto nxge_attach_exit;
5323859Sml29623 
5333859Sml29623 	case DDI_PM_RESUME:
5343859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_PM_RESUME"));
5353859Sml29623 		nxgep = (p_nxge_t)ddi_get_soft_state(nxge_list, instance);
5363859Sml29623 		if (nxgep == NULL) {
5373859Sml29623 			status = DDI_FAILURE;
5383859Sml29623 			break;
5393859Sml29623 		}
5403859Sml29623 		if (nxgep->dip != dip) {
5413859Sml29623 			status = DDI_FAILURE;
5423859Sml29623 			break;
5433859Sml29623 		}
5444185Sspeer 		status = nxge_resume(nxgep);
5453859Sml29623 		goto nxge_attach_exit;
5463859Sml29623 
5473859Sml29623 	default:
5483859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing unknown"));
5493859Sml29623 		status = DDI_FAILURE;
5503859Sml29623 		goto nxge_attach_exit;
5513859Sml29623 	}
5523859Sml29623 
5533859Sml29623 
5543859Sml29623 	if (ddi_soft_state_zalloc(nxge_list, instance) == DDI_FAILURE) {
5553859Sml29623 		status = DDI_FAILURE;
5563859Sml29623 		goto nxge_attach_exit;
5573859Sml29623 	}
5583859Sml29623 
5593859Sml29623 	nxgep = ddi_get_soft_state(nxge_list, instance);
5603859Sml29623 	if (nxgep == NULL) {
5614977Sraghus 		status = NXGE_ERROR;
5624977Sraghus 		goto nxge_attach_fail2;
5633859Sml29623 	}
5643859Sml29623 
5654693Stm144005 	nxgep->nxge_magic = NXGE_MAGIC;
5664693Stm144005 
5673859Sml29623 	nxgep->drv_state = 0;
5683859Sml29623 	nxgep->dip = dip;
5693859Sml29623 	nxgep->instance = instance;
5703859Sml29623 	nxgep->p_dip = ddi_get_parent(dip);
5713859Sml29623 	nxgep->nxge_debug_level = nxge_debug_level;
5723859Sml29623 	npi_debug_level = nxge_debug_level;
5733859Sml29623 
5746495Sspeer 	/* Are we a guest running in a Hybrid I/O environment? */
5756495Sspeer 	nxge_get_environs(nxgep);
5763859Sml29623 
5773859Sml29623 	status = nxge_map_regs(nxgep);
5786495Sspeer 
5793859Sml29623 	if (status != NXGE_OK) {
5803859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "nxge_map_regs failed"));
5814977Sraghus 		goto nxge_attach_fail3;
5823859Sml29623 	}
5833859Sml29623 
5846495Sspeer 	nxge_fm_init(nxgep, &nxge_dev_reg_acc_attr,
5856495Sspeer 	    &nxge_dev_desc_dma_acc_attr,
5866495Sspeer 	    &nxge_rx_dma_attr);
5876495Sspeer 
5886495Sspeer 	/* Create & initialize the per-Neptune data structure */
5896495Sspeer 	/* (even if we're a guest). */
5903859Sml29623 	status = nxge_init_common_dev(nxgep);
5913859Sml29623 	if (status != NXGE_OK) {
5923859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
5936512Ssowmini 		    "nxge_init_common_dev failed"));
5944977Sraghus 		goto nxge_attach_fail4;
5953859Sml29623 	}
5963859Sml29623 
5976495Sspeer #if defined(sun4v)
5986495Sspeer 	/* This is required by nxge_hio_init(), which follows. */
5996495Sspeer 	if ((status = nxge_hsvc_register(nxgep)) != DDI_SUCCESS)
6006495Sspeer 		goto nxge_attach_fail;
6016495Sspeer #endif
6026495Sspeer 
6036495Sspeer 	if ((status = nxge_hio_init(nxgep)) != NXGE_OK) {
6046495Sspeer 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
6056512Ssowmini 		    "nxge_hio_init failed"));
6066495Sspeer 		goto nxge_attach_fail4;
6076495Sspeer 	}
6086495Sspeer 
6094732Sdavemq 	if (nxgep->niu_type == NEPTUNE_2_10GF) {
6104732Sdavemq 		if (nxgep->function_num > 1) {
6116028Ssbehera 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "Unsupported"
6124732Sdavemq 			    " function %d. Only functions 0 and 1 are "
6134732Sdavemq 			    "supported for this card.", nxgep->function_num));
6144732Sdavemq 			status = NXGE_ERROR;
6154977Sraghus 			goto nxge_attach_fail4;
6164732Sdavemq 		}
6174732Sdavemq 	}
6184732Sdavemq 
6196495Sspeer 	if (isLDOMguest(nxgep)) {
6206495Sspeer 		/*
6216495Sspeer 		 * Use the function number here.
6226495Sspeer 		 */
6236495Sspeer 		nxgep->mac.portnum = nxgep->function_num;
6246495Sspeer 		nxgep->mac.porttype = PORT_TYPE_LOGICAL;
6256495Sspeer 
6266495Sspeer 		/* XXX We'll set the MAC address counts to 1 for now. */
6276495Sspeer 		mmac_info = &nxgep->nxge_mmac_info;
6286495Sspeer 		mmac_info->num_mmac = 1;
6296495Sspeer 		mmac_info->naddrfree = 1;
6303859Sml29623 	} else {
6316495Sspeer 		portn = NXGE_GET_PORT_NUM(nxgep->function_num);
6326495Sspeer 		nxgep->mac.portnum = portn;
6336495Sspeer 		if ((portn == 0) || (portn == 1))
6346495Sspeer 			nxgep->mac.porttype = PORT_TYPE_XMAC;
6356495Sspeer 		else
6366495Sspeer 			nxgep->mac.porttype = PORT_TYPE_BMAC;
6376495Sspeer 		/*
6386495Sspeer 		 * Neptune has 4 ports, the first 2 ports use XMAC (10G MAC)
6396495Sspeer 		 * internally, the rest 2 ports use BMAC (1G "Big" MAC).
6406495Sspeer 		 * The two types of MACs have different characterizations.
6416495Sspeer 		 */
6426495Sspeer 		mmac_info = &nxgep->nxge_mmac_info;
6436495Sspeer 		if (nxgep->function_num < 2) {
6446495Sspeer 			mmac_info->num_mmac = XMAC_MAX_ALT_ADDR_ENTRY;
6456495Sspeer 			mmac_info->naddrfree = XMAC_MAX_ALT_ADDR_ENTRY;
6466495Sspeer 		} else {
6476495Sspeer 			mmac_info->num_mmac = BMAC_MAX_ALT_ADDR_ENTRY;
6486495Sspeer 			mmac_info->naddrfree = BMAC_MAX_ALT_ADDR_ENTRY;
6496495Sspeer 		}
6503859Sml29623 	}
6513859Sml29623 	/*
6523859Sml29623 	 * Setup the Ndd parameters for the this instance.
6533859Sml29623 	 */
6543859Sml29623 	nxge_init_param(nxgep);
6553859Sml29623 
6563859Sml29623 	/*
6573859Sml29623 	 * Setup Register Tracing Buffer.
6583859Sml29623 	 */
6593859Sml29623 	npi_rtrace_buf_init((rtrace_t *)&npi_rtracebuf);
6603859Sml29623 
6613859Sml29623 	/* init stats ptr */
6623859Sml29623 	nxge_init_statsp(nxgep);
6634185Sspeer 
6644977Sraghus 	/*
6656495Sspeer 	 * Copy the vpd info from eeprom to a local data
6666495Sspeer 	 * structure, and then check its validity.
6674977Sraghus 	 */
6686495Sspeer 	if (!isLDOMguest(nxgep)) {
6696495Sspeer 		int *regp;
6706495Sspeer 		uint_t reglen;
6716495Sspeer 		int rv;
6726495Sspeer 
6736495Sspeer 		nxge_vpd_info_get(nxgep);
6746495Sspeer 
6756495Sspeer 		/* Find the NIU config handle. */
6766495Sspeer 		rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
6776495Sspeer 		    ddi_get_parent(nxgep->dip), DDI_PROP_DONTPASS,
6786495Sspeer 		    "reg", &regp, &reglen);
6796495Sspeer 
6806495Sspeer 		if (rv != DDI_PROP_SUCCESS) {
6816495Sspeer 			goto nxge_attach_fail5;
6826495Sspeer 		}
6836495Sspeer 		/*
6846495Sspeer 		 * The address_hi, that is the first int, in the reg
6856495Sspeer 		 * property consists of config handle, but need to remove
6866495Sspeer 		 * the bits 28-31 which are OBP specific info.
6876495Sspeer 		 */
6886495Sspeer 		nxgep->niu_cfg_hdl = (*regp) & 0xFFFFFFF;
6896495Sspeer 		ddi_prop_free(regp);
6906495Sspeer 	}
6916495Sspeer 
6926495Sspeer 	if (isLDOMguest(nxgep)) {
6936495Sspeer 		uchar_t *prop_val;
6946495Sspeer 		uint_t prop_len;
6956495Sspeer 
6966495Sspeer 		extern void nxge_get_logical_props(p_nxge_t);
6976495Sspeer 
6986495Sspeer 		nxgep->statsp->mac_stats.xcvr_inuse = LOGICAL_XCVR;
6996495Sspeer 		nxgep->mac.portmode = PORT_LOGICAL;
7006495Sspeer 		(void) ddi_prop_update_string(DDI_DEV_T_NONE, nxgep->dip,
7016495Sspeer 		    "phy-type", "virtual transceiver");
7026495Sspeer 
7036495Sspeer 		nxgep->nports = 1;
7046495Sspeer 		nxgep->board_ver = 0;	/* XXX What? */
7056495Sspeer 
7066495Sspeer 		/*
7076495Sspeer 		 * local-mac-address property gives us info on which
7086495Sspeer 		 * specific MAC address the Hybrid resource is associated
7096495Sspeer 		 * with.
7106495Sspeer 		 */
7116495Sspeer 		if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, nxgep->dip, 0,
7126495Sspeer 		    "local-mac-address", &prop_val,
7136495Sspeer 		    &prop_len) != DDI_PROP_SUCCESS) {
7146495Sspeer 			goto nxge_attach_fail5;
7156495Sspeer 		}
7166495Sspeer 		if (prop_len !=  ETHERADDRL) {
7176495Sspeer 			ddi_prop_free(prop_val);
7186495Sspeer 			goto nxge_attach_fail5;
7196495Sspeer 		}
7206495Sspeer 		ether_copy(prop_val, nxgep->hio_mac_addr);
7216495Sspeer 		ddi_prop_free(prop_val);
7226495Sspeer 		nxge_get_logical_props(nxgep);
7236495Sspeer 
7246495Sspeer 	} else {
7256495Sspeer 		status = nxge_xcvr_find(nxgep);
7266495Sspeer 
7276495Sspeer 		if (status != NXGE_OK) {
7286495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "nxge_attach: "
7296512Ssowmini 			    " Couldn't determine card type"
7306512Ssowmini 			    " .... exit "));
7316495Sspeer 			goto nxge_attach_fail5;
7326495Sspeer 		}
7336495Sspeer 
7346495Sspeer 		status = nxge_get_config_properties(nxgep);
7356495Sspeer 
7366495Sspeer 		if (status != NXGE_OK) {
7376495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
7386512Ssowmini 			    "get_hw create failed"));
7396495Sspeer 			goto nxge_attach_fail;
7406495Sspeer 		}
7413859Sml29623 	}
7423859Sml29623 
7433859Sml29623 	/*
7443859Sml29623 	 * Setup the Kstats for the driver.
7453859Sml29623 	 */
7463859Sml29623 	nxge_setup_kstats(nxgep);
7473859Sml29623 
7486495Sspeer 	if (!isLDOMguest(nxgep))
7496495Sspeer 		nxge_setup_param(nxgep);
7503859Sml29623 
7513859Sml29623 	status = nxge_setup_system_dma_pages(nxgep);
7523859Sml29623 	if (status != NXGE_OK) {
7533859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "set dma page failed"));
7543859Sml29623 		goto nxge_attach_fail;
7553859Sml29623 	}
7563859Sml29623 
7573859Sml29623 	nxge_hw_id_init(nxgep);
7586495Sspeer 
7596495Sspeer 	if (!isLDOMguest(nxgep))
7606495Sspeer 		nxge_hw_init_niu_common(nxgep);
7613859Sml29623 
7623859Sml29623 	status = nxge_setup_mutexes(nxgep);
7633859Sml29623 	if (status != NXGE_OK) {
7643859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "set mutex failed"));
7653859Sml29623 		goto nxge_attach_fail;
7663859Sml29623 	}
7673859Sml29623 
7686495Sspeer #if defined(sun4v)
7696495Sspeer 	if (isLDOMguest(nxgep)) {
7706495Sspeer 		/* Find our VR & channel sets. */
7716495Sspeer 		status = nxge_hio_vr_add(nxgep);
7726495Sspeer 		goto nxge_attach_exit;
7736495Sspeer 	}
7746495Sspeer #endif
7756495Sspeer 
7763859Sml29623 	status = nxge_setup_dev(nxgep);
7773859Sml29623 	if (status != DDI_SUCCESS) {
7783859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "set dev failed"));
7793859Sml29623 		goto nxge_attach_fail;
7803859Sml29623 	}
7813859Sml29623 
7823859Sml29623 	status = nxge_add_intrs(nxgep);
7833859Sml29623 	if (status != DDI_SUCCESS) {
7843859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "add_intr failed"));
7853859Sml29623 		goto nxge_attach_fail;
7863859Sml29623 	}
7873859Sml29623 	status = nxge_add_soft_intrs(nxgep);
7883859Sml29623 	if (status != DDI_SUCCESS) {
7896495Sspeer 		NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
7906495Sspeer 		    "add_soft_intr failed"));
7913859Sml29623 		goto nxge_attach_fail;
7923859Sml29623 	}
7933859Sml29623 
7943859Sml29623 	/*
7953859Sml29623 	 * Enable interrupts.
7963859Sml29623 	 */
7973859Sml29623 	nxge_intrs_enable(nxgep);
7983859Sml29623 
7996835Syc148097 	/* If a guest, register with vio_net instead. */
8004977Sraghus 	if ((status = nxge_mac_register(nxgep)) != NXGE_OK) {
8013859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
8026495Sspeer 		    "unable to register to mac layer (%d)", status));
8033859Sml29623 		goto nxge_attach_fail;
8043859Sml29623 	}
8053859Sml29623 
8063859Sml29623 	mac_link_update(nxgep->mach, LINK_STATE_UNKNOWN);
8073859Sml29623 
8086495Sspeer 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
8096495Sspeer 	    "registered to mac (instance %d)", instance));
8103859Sml29623 
8116835Syc148097 	/* nxge_link_monitor calls xcvr.check_link recursively */
8123859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_START);
8133859Sml29623 
8143859Sml29623 	goto nxge_attach_exit;
8153859Sml29623 
8163859Sml29623 nxge_attach_fail:
8173859Sml29623 	nxge_unattach(nxgep);
8184977Sraghus 	goto nxge_attach_fail1;
8194977Sraghus 
8204977Sraghus nxge_attach_fail5:
8214977Sraghus 	/*
8224977Sraghus 	 * Tear down the ndd parameters setup.
8234977Sraghus 	 */
8244977Sraghus 	nxge_destroy_param(nxgep);
8254977Sraghus 
8264977Sraghus 	/*
8274977Sraghus 	 * Tear down the kstat setup.
8284977Sraghus 	 */
8294977Sraghus 	nxge_destroy_kstats(nxgep);
8304977Sraghus 
8314977Sraghus nxge_attach_fail4:
8324977Sraghus 	if (nxgep->nxge_hw_p) {
8334977Sraghus 		nxge_uninit_common_dev(nxgep);
8344977Sraghus 		nxgep->nxge_hw_p = NULL;
8354977Sraghus 	}
8364977Sraghus 
8374977Sraghus nxge_attach_fail3:
8384977Sraghus 	/*
8394977Sraghus 	 * Unmap the register setup.
8404977Sraghus 	 */
8414977Sraghus 	nxge_unmap_regs(nxgep);
8424977Sraghus 
8434977Sraghus 	nxge_fm_fini(nxgep);
8444977Sraghus 
8454977Sraghus nxge_attach_fail2:
8464977Sraghus 	ddi_soft_state_free(nxge_list, nxgep->instance);
8474977Sraghus 
8484977Sraghus nxge_attach_fail1:
8494185Sspeer 	if (status != NXGE_OK)
8504185Sspeer 		status = (NXGE_ERROR | NXGE_DDI_FAILED);
8513859Sml29623 	nxgep = NULL;
8523859Sml29623 
8533859Sml29623 nxge_attach_exit:
8543859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_attach status = 0x%08x",
8556512Ssowmini 	    status));
8563859Sml29623 
8573859Sml29623 	return (status);
8583859Sml29623 }
8593859Sml29623 
8603859Sml29623 static int
8613859Sml29623 nxge_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
8623859Sml29623 {
8633859Sml29623 	int 		status = DDI_SUCCESS;
8643859Sml29623 	int 		instance;
8653859Sml29623 	p_nxge_t 	nxgep = NULL;
8663859Sml29623 
8673859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_detach"));
8683859Sml29623 	instance = ddi_get_instance(dip);
8693859Sml29623 	nxgep = ddi_get_soft_state(nxge_list, instance);
8703859Sml29623 	if (nxgep == NULL) {
8713859Sml29623 		status = DDI_FAILURE;
8723859Sml29623 		goto nxge_detach_exit;
8733859Sml29623 	}
8743859Sml29623 
8753859Sml29623 	switch (cmd) {
8763859Sml29623 	case DDI_DETACH:
8773859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_DETACH"));
8783859Sml29623 		break;
8793859Sml29623 
8803859Sml29623 	case DDI_PM_SUSPEND:
8813859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_PM_SUSPEND"));
8823859Sml29623 		nxgep->suspended = DDI_PM_SUSPEND;
8833859Sml29623 		nxge_suspend(nxgep);
8843859Sml29623 		break;
8853859Sml29623 
8863859Sml29623 	case DDI_SUSPEND:
8873859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_SUSPEND"));
8883859Sml29623 		if (nxgep->suspended != DDI_PM_SUSPEND) {
8893859Sml29623 			nxgep->suspended = DDI_SUSPEND;
8903859Sml29623 			nxge_suspend(nxgep);
8913859Sml29623 		}
8923859Sml29623 		break;
8933859Sml29623 
8943859Sml29623 	default:
8953859Sml29623 		status = DDI_FAILURE;
8963859Sml29623 	}
8973859Sml29623 
8983859Sml29623 	if (cmd != DDI_DETACH)
8993859Sml29623 		goto nxge_detach_exit;
9003859Sml29623 
9013859Sml29623 	/*
9023859Sml29623 	 * Stop the xcvr polling.
9033859Sml29623 	 */
9043859Sml29623 	nxgep->suspended = cmd;
9053859Sml29623 
9063859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_STOP);
9073859Sml29623 
9086495Sspeer 	if (isLDOMguest(nxgep)) {
9096495Sspeer 		nxge_hio_unregister(nxgep);
9106495Sspeer 	} else if (nxgep->mach && (status = mac_unregister(nxgep->mach)) != 0) {
9113859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
9126512Ssowmini 		    "<== nxge_detach status = 0x%08X", status));
9133859Sml29623 		return (DDI_FAILURE);
9143859Sml29623 	}
9153859Sml29623 
9163859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
9176512Ssowmini 	    "<== nxge_detach (mac_unregister) status = 0x%08X", status));
9183859Sml29623 
9193859Sml29623 	nxge_unattach(nxgep);
9203859Sml29623 	nxgep = NULL;
9213859Sml29623 
9223859Sml29623 nxge_detach_exit:
9233859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_detach status = 0x%08X",
9246512Ssowmini 	    status));
9253859Sml29623 
9263859Sml29623 	return (status);
9273859Sml29623 }
9283859Sml29623 
9293859Sml29623 static void
9303859Sml29623 nxge_unattach(p_nxge_t nxgep)
9313859Sml29623 {
9323859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_unattach"));
9333859Sml29623 
9343859Sml29623 	if (nxgep == NULL || nxgep->dev_regs == NULL) {
9353859Sml29623 		return;
9363859Sml29623 	}
9373859Sml29623 
9384693Stm144005 	nxgep->nxge_magic = 0;
9394693Stm144005 
9405780Ssbehera 	if (nxgep->nxge_timerid) {
9415780Ssbehera 		nxge_stop_timer(nxgep, nxgep->nxge_timerid);
9425780Ssbehera 		nxgep->nxge_timerid = 0;
9435780Ssbehera 	}
9445780Ssbehera 
9456705Sml29623 	/*
9466705Sml29623 	 * If this flag is set, it will affect the Neptune
9476705Sml29623 	 * only.
9486705Sml29623 	 */
9496705Sml29623 	if ((nxgep->niu_type != N2_NIU) && nxge_peu_reset_enable) {
9506705Sml29623 		nxge_niu_peu_reset(nxgep);
9516705Sml29623 	}
9526705Sml29623 
9536495Sspeer #if	defined(sun4v)
9546495Sspeer 	if (isLDOMguest(nxgep)) {
9556498Sspeer 		(void) nxge_hio_vr_release(nxgep);
9566495Sspeer 	}
9576495Sspeer #endif
9586495Sspeer 
9593859Sml29623 	if (nxgep->nxge_hw_p) {
9603859Sml29623 		nxge_uninit_common_dev(nxgep);
9613859Sml29623 		nxgep->nxge_hw_p = NULL;
9623859Sml29623 	}
9633859Sml29623 
9643859Sml29623 #if	defined(sun4v)
9653859Sml29623 	if (nxgep->niu_type == N2_NIU && nxgep->niu_hsvc_available == B_TRUE) {
9663859Sml29623 		(void) hsvc_unregister(&nxgep->niu_hsvc);
9673859Sml29623 		nxgep->niu_hsvc_available = B_FALSE;
9683859Sml29623 	}
9693859Sml29623 #endif
9703859Sml29623 	/*
9713859Sml29623 	 * Stop any further interrupts.
9723859Sml29623 	 */
9733859Sml29623 	nxge_remove_intrs(nxgep);
9743859Sml29623 
9753859Sml29623 	/* remove soft interrups */
9763859Sml29623 	nxge_remove_soft_intrs(nxgep);
9773859Sml29623 
9783859Sml29623 	/*
9793859Sml29623 	 * Stop the device and free resources.
9803859Sml29623 	 */
9816495Sspeer 	if (!isLDOMguest(nxgep)) {
9826495Sspeer 		nxge_destroy_dev(nxgep);
9836495Sspeer 	}
9843859Sml29623 
9853859Sml29623 	/*
9863859Sml29623 	 * Tear down the ndd parameters setup.
9873859Sml29623 	 */
9883859Sml29623 	nxge_destroy_param(nxgep);
9893859Sml29623 
9903859Sml29623 	/*
9913859Sml29623 	 * Tear down the kstat setup.
9923859Sml29623 	 */
9933859Sml29623 	nxge_destroy_kstats(nxgep);
9943859Sml29623 
9953859Sml29623 	/*
9963859Sml29623 	 * Destroy all mutexes.
9973859Sml29623 	 */
9983859Sml29623 	nxge_destroy_mutexes(nxgep);
9993859Sml29623 
10003859Sml29623 	/*
10013859Sml29623 	 * Remove the list of ndd parameters which
10023859Sml29623 	 * were setup during attach.
10033859Sml29623 	 */
10043859Sml29623 	if (nxgep->dip) {
10053859Sml29623 		NXGE_DEBUG_MSG((nxgep, OBP_CTL,
10066512Ssowmini 		    " nxge_unattach: remove all properties"));
10073859Sml29623 
10083859Sml29623 		(void) ddi_prop_remove_all(nxgep->dip);
10093859Sml29623 	}
10103859Sml29623 
10113859Sml29623 #if NXGE_PROPERTY
10123859Sml29623 	nxge_remove_hard_properties(nxgep);
10133859Sml29623 #endif
10143859Sml29623 
10153859Sml29623 	/*
10163859Sml29623 	 * Unmap the register setup.
10173859Sml29623 	 */
10183859Sml29623 	nxge_unmap_regs(nxgep);
10193859Sml29623 
10203859Sml29623 	nxge_fm_fini(nxgep);
10213859Sml29623 
10223859Sml29623 	ddi_soft_state_free(nxge_list, nxgep->instance);
10233859Sml29623 
10243859Sml29623 	NXGE_DEBUG_MSG((NULL, DDI_CTL, "<== nxge_unattach"));
10253859Sml29623 }
10263859Sml29623 
10276495Sspeer #if defined(sun4v)
10286495Sspeer int
10296495Sspeer nxge_hsvc_register(
10306495Sspeer 	nxge_t *nxgep)
10316495Sspeer {
10326495Sspeer 	nxge_status_t status;
10336495Sspeer 
10346495Sspeer 	if (nxgep->niu_type == N2_NIU) {
10356495Sspeer 		nxgep->niu_hsvc_available = B_FALSE;
10366495Sspeer 		bcopy(&niu_hsvc, &nxgep->niu_hsvc, sizeof (hsvc_info_t));
10376495Sspeer 		if ((status = hsvc_register(&nxgep->niu_hsvc,
10386495Sspeer 		    &nxgep->niu_min_ver)) != 0) {
10396495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
10406495Sspeer 			    "nxge_attach: %s: cannot negotiate "
10416495Sspeer 			    "hypervisor services revision %d group: 0x%lx "
10426495Sspeer 			    "major: 0x%lx minor: 0x%lx errno: %d",
10436495Sspeer 			    niu_hsvc.hsvc_modname, niu_hsvc.hsvc_rev,
10446495Sspeer 			    niu_hsvc.hsvc_group, niu_hsvc.hsvc_major,
10456495Sspeer 			    niu_hsvc.hsvc_minor, status));
10466495Sspeer 			return (DDI_FAILURE);
10476495Sspeer 		}
10486495Sspeer 		nxgep->niu_hsvc_available = B_TRUE;
10496495Sspeer 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
10506512Ssowmini 		    "NIU Hypervisor service enabled"));
10516495Sspeer 	}
10526495Sspeer 
10536495Sspeer 	return (DDI_SUCCESS);
10546495Sspeer }
10556495Sspeer #endif
10566495Sspeer 
10573859Sml29623 static char n2_siu_name[] = "niu";
10583859Sml29623 
10593859Sml29623 static nxge_status_t
10603859Sml29623 nxge_map_regs(p_nxge_t nxgep)
10613859Sml29623 {
10623859Sml29623 	int		ddi_status = DDI_SUCCESS;
10633859Sml29623 	p_dev_regs_t 	dev_regs;
10643859Sml29623 	char		buf[MAXPATHLEN + 1];
10653859Sml29623 	char 		*devname;
10663859Sml29623 #ifdef	NXGE_DEBUG
10673859Sml29623 	char 		*sysname;
10683859Sml29623 #endif
10693859Sml29623 	off_t		regsize;
10703859Sml29623 	nxge_status_t	status = NXGE_OK;
10713859Sml29623 #if !defined(_BIG_ENDIAN)
10723859Sml29623 	off_t pci_offset;
10733859Sml29623 	uint16_t pcie_devctl;
10743859Sml29623 #endif
10753859Sml29623 
10766495Sspeer 	if (isLDOMguest(nxgep)) {
10776495Sspeer 		return (nxge_guest_regs_map(nxgep));
10786495Sspeer 	}
10796495Sspeer 
10803859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_map_regs"));
10813859Sml29623 	nxgep->dev_regs = NULL;
10823859Sml29623 	dev_regs = KMEM_ZALLOC(sizeof (dev_regs_t), KM_SLEEP);
10833859Sml29623 	dev_regs->nxge_regh = NULL;
10843859Sml29623 	dev_regs->nxge_pciregh = NULL;
10853859Sml29623 	dev_regs->nxge_msix_regh = NULL;
10863859Sml29623 	dev_regs->nxge_vir_regh = NULL;
10873859Sml29623 	dev_regs->nxge_vir2_regh = NULL;
10884732Sdavemq 	nxgep->niu_type = NIU_TYPE_NONE;
10893859Sml29623 
10903859Sml29623 	devname = ddi_pathname(nxgep->dip, buf);
10913859Sml29623 	ASSERT(strlen(devname) > 0);
10923859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
10936512Ssowmini 	    "nxge_map_regs: pathname devname %s", devname));
10943859Sml29623 
10956835Syc148097 	/*
10966835Syc148097 	 * The driver is running on a N2-NIU system if devname is something
10976835Syc148097 	 * like "/niu@80/network@0"
10986835Syc148097 	 */
10993859Sml29623 	if (strstr(devname, n2_siu_name)) {
11003859Sml29623 		/* N2/NIU */
11013859Sml29623 		nxgep->niu_type = N2_NIU;
11023859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11036512Ssowmini 		    "nxge_map_regs: N2/NIU devname %s", devname));
11043859Sml29623 		/* get function number */
11053859Sml29623 		nxgep->function_num =
11066512Ssowmini 		    (devname[strlen(devname) -1] == '1' ? 1 : 0);
11073859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11086512Ssowmini 		    "nxge_map_regs: N2/NIU function number %d",
11096512Ssowmini 		    nxgep->function_num));
11103859Sml29623 	} else {
11113859Sml29623 		int		*prop_val;
11123859Sml29623 		uint_t 		prop_len;
11133859Sml29623 		uint8_t 	func_num;
11143859Sml29623 
11153859Sml29623 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, nxgep->dip,
11166512Ssowmini 		    0, "reg",
11176512Ssowmini 		    &prop_val, &prop_len) != DDI_PROP_SUCCESS) {
11183859Sml29623 			NXGE_DEBUG_MSG((nxgep, VPD_CTL,
11196512Ssowmini 			    "Reg property not found"));
11203859Sml29623 			ddi_status = DDI_FAILURE;
11213859Sml29623 			goto nxge_map_regs_fail0;
11223859Sml29623 
11233859Sml29623 		} else {
11243859Sml29623 			func_num = (prop_val[0] >> 8) & 0x7;
11253859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11266512Ssowmini 			    "Reg property found: fun # %d",
11276512Ssowmini 			    func_num));
11283859Sml29623 			nxgep->function_num = func_num;
11296495Sspeer 			if (isLDOMguest(nxgep)) {
11306495Sspeer 				nxgep->function_num /= 2;
11316495Sspeer 				return (NXGE_OK);
11326495Sspeer 			}
11333859Sml29623 			ddi_prop_free(prop_val);
11343859Sml29623 		}
11353859Sml29623 	}
11363859Sml29623 
11373859Sml29623 	switch (nxgep->niu_type) {
11383859Sml29623 	default:
11393859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 0, &regsize);
11403859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11416512Ssowmini 		    "nxge_map_regs: pci config size 0x%x", regsize));
11423859Sml29623 
11433859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 0,
11446512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_pciregp), 0, 0,
11456512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_pciregh);
11463859Sml29623 		if (ddi_status != DDI_SUCCESS) {
11473859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
11486512Ssowmini 			    "ddi_map_regs, nxge bus config regs failed"));
11493859Sml29623 			goto nxge_map_regs_fail0;
11503859Sml29623 		}
11513859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11526512Ssowmini 		    "nxge_map_reg: PCI config addr 0x%0llx "
11536512Ssowmini 		    " handle 0x%0llx", dev_regs->nxge_pciregp,
11546512Ssowmini 		    dev_regs->nxge_pciregh));
11553859Sml29623 			/*
11563859Sml29623 			 * IMP IMP
11573859Sml29623 			 * workaround  for bit swapping bug in HW
11583859Sml29623 			 * which ends up in no-snoop = yes
11593859Sml29623 			 * resulting, in DMA not synched properly
11603859Sml29623 			 */
11613859Sml29623 #if !defined(_BIG_ENDIAN)
11623859Sml29623 		/* workarounds for x86 systems */
11633859Sml29623 		pci_offset = 0x80 + PCIE_DEVCTL;
11643859Sml29623 		pcie_devctl = 0x0;
11653859Sml29623 		pcie_devctl &= PCIE_DEVCTL_ENABLE_NO_SNOOP;
11663859Sml29623 		pcie_devctl |= PCIE_DEVCTL_RO_EN;
11673859Sml29623 		pci_config_put16(dev_regs->nxge_pciregh, pci_offset,
11686512Ssowmini 		    pcie_devctl);
11693859Sml29623 #endif
11703859Sml29623 
11713859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 1, &regsize);
11723859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11736512Ssowmini 		    "nxge_map_regs: pio size 0x%x", regsize));
11743859Sml29623 		/* set up the device mapped register */
11753859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 1,
11766512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_regp), 0, 0,
11776512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_regh);
11783859Sml29623 		if (ddi_status != DDI_SUCCESS) {
11793859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
11806512Ssowmini 			    "ddi_map_regs for Neptune global reg failed"));
11813859Sml29623 			goto nxge_map_regs_fail1;
11823859Sml29623 		}
11833859Sml29623 
11843859Sml29623 		/* set up the msi/msi-x mapped register */
11853859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 2, &regsize);
11863859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11876512Ssowmini 		    "nxge_map_regs: msix size 0x%x", regsize));
11883859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 2,
11896512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_msix_regp), 0, 0,
11906512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_msix_regh);
11913859Sml29623 		if (ddi_status != DDI_SUCCESS) {
11923859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
11936512Ssowmini 			    "ddi_map_regs for msi reg failed"));
11943859Sml29623 			goto nxge_map_regs_fail2;
11953859Sml29623 		}
11963859Sml29623 
11973859Sml29623 		/* set up the vio region mapped register */
11983859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 3, &regsize);
11993859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12006512Ssowmini 		    "nxge_map_regs: vio size 0x%x", regsize));
12013859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 3,
12026512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_vir_regp), 0, 0,
12036512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_vir_regh);
12043859Sml29623 
12053859Sml29623 		if (ddi_status != DDI_SUCCESS) {
12063859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
12076512Ssowmini 			    "ddi_map_regs for nxge vio reg failed"));
12083859Sml29623 			goto nxge_map_regs_fail3;
12093859Sml29623 		}
12103859Sml29623 		nxgep->dev_regs = dev_regs;
12113859Sml29623 
12123859Sml29623 		NPI_PCI_ACC_HANDLE_SET(nxgep, dev_regs->nxge_pciregh);
12133859Sml29623 		NPI_PCI_ADD_HANDLE_SET(nxgep,
12146512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_pciregp);
12153859Sml29623 		NPI_MSI_ACC_HANDLE_SET(nxgep, dev_regs->nxge_msix_regh);
12163859Sml29623 		NPI_MSI_ADD_HANDLE_SET(nxgep,
12176512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_msix_regp);
12183859Sml29623 
12193859Sml29623 		NPI_ACC_HANDLE_SET(nxgep, dev_regs->nxge_regh);
12203859Sml29623 		NPI_ADD_HANDLE_SET(nxgep, (npi_reg_ptr_t)dev_regs->nxge_regp);
12213859Sml29623 
12223859Sml29623 		NPI_REG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_regh);
12233859Sml29623 		NPI_REG_ADD_HANDLE_SET(nxgep,
12246512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_regp);
12253859Sml29623 
12263859Sml29623 		NPI_VREG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_vir_regh);
12273859Sml29623 		NPI_VREG_ADD_HANDLE_SET(nxgep,
12286512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_vir_regp);
12293859Sml29623 
12303859Sml29623 		break;
12313859Sml29623 
12323859Sml29623 	case N2_NIU:
12333859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "ddi_map_regs, NIU"));
12343859Sml29623 		/*
12353859Sml29623 		 * Set up the device mapped register (FWARC 2006/556)
12363859Sml29623 		 * (changed back to 1: reg starts at 1!)
12373859Sml29623 		 */
12383859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 1, &regsize);
12393859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12406512Ssowmini 		    "nxge_map_regs: dev size 0x%x", regsize));
12413859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 1,
12426512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_regp), 0, 0,
12436512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_regh);
12443859Sml29623 
12453859Sml29623 		if (ddi_status != DDI_SUCCESS) {
12463859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
12476512Ssowmini 			    "ddi_map_regs for N2/NIU, global reg failed "));
12483859Sml29623 			goto nxge_map_regs_fail1;
12493859Sml29623 		}
12503859Sml29623 
12516495Sspeer 		/* set up the first vio region mapped register */
12523859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 2, &regsize);
12533859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12546512Ssowmini 		    "nxge_map_regs: vio (1) size 0x%x", regsize));
12553859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 2,
12566512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_vir_regp), 0, 0,
12576512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_vir_regh);
12583859Sml29623 
12593859Sml29623 		if (ddi_status != DDI_SUCCESS) {
12603859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
12616512Ssowmini 			    "ddi_map_regs for nxge vio reg failed"));
12623859Sml29623 			goto nxge_map_regs_fail2;
12633859Sml29623 		}
12646495Sspeer 		/* set up the second vio region mapped register */
12653859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 3, &regsize);
12663859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12676512Ssowmini 		    "nxge_map_regs: vio (3) size 0x%x", regsize));
12683859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 3,
12696512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_vir2_regp), 0, 0,
12706512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_vir2_regh);
12713859Sml29623 
12723859Sml29623 		if (ddi_status != DDI_SUCCESS) {
12733859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
12746512Ssowmini 			    "ddi_map_regs for nxge vio2 reg failed"));
12753859Sml29623 			goto nxge_map_regs_fail3;
12763859Sml29623 		}
12773859Sml29623 		nxgep->dev_regs = dev_regs;
12783859Sml29623 
12793859Sml29623 		NPI_ACC_HANDLE_SET(nxgep, dev_regs->nxge_regh);
12803859Sml29623 		NPI_ADD_HANDLE_SET(nxgep, (npi_reg_ptr_t)dev_regs->nxge_regp);
12813859Sml29623 
12823859Sml29623 		NPI_REG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_regh);
12833859Sml29623 		NPI_REG_ADD_HANDLE_SET(nxgep,
12846512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_regp);
12853859Sml29623 
12863859Sml29623 		NPI_VREG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_vir_regh);
12873859Sml29623 		NPI_VREG_ADD_HANDLE_SET(nxgep,
12886512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_vir_regp);
12893859Sml29623 
12903859Sml29623 		NPI_V2REG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_vir2_regh);
12913859Sml29623 		NPI_V2REG_ADD_HANDLE_SET(nxgep,
12926512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_vir2_regp);
12933859Sml29623 
12943859Sml29623 		break;
12953859Sml29623 	}
12963859Sml29623 
12973859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "nxge_map_reg: hardware addr 0x%0llx "
12986512Ssowmini 	    " handle 0x%0llx", dev_regs->nxge_regp, dev_regs->nxge_regh));
12993859Sml29623 
13003859Sml29623 	goto nxge_map_regs_exit;
13013859Sml29623 nxge_map_regs_fail3:
13023859Sml29623 	if (dev_regs->nxge_msix_regh) {
13033859Sml29623 		ddi_regs_map_free(&dev_regs->nxge_msix_regh);
13043859Sml29623 	}
13053859Sml29623 	if (dev_regs->nxge_vir_regh) {
13063859Sml29623 		ddi_regs_map_free(&dev_regs->nxge_regh);
13073859Sml29623 	}
13083859Sml29623 nxge_map_regs_fail2:
13093859Sml29623 	if (dev_regs->nxge_regh) {
13103859Sml29623 		ddi_regs_map_free(&dev_regs->nxge_regh);
13113859Sml29623 	}
13123859Sml29623 nxge_map_regs_fail1:
13133859Sml29623 	if (dev_regs->nxge_pciregh) {
13143859Sml29623 		ddi_regs_map_free(&dev_regs->nxge_pciregh);
13153859Sml29623 	}
13163859Sml29623 nxge_map_regs_fail0:
13173859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "Freeing register set memory"));
13183859Sml29623 	kmem_free(dev_regs, sizeof (dev_regs_t));
13193859Sml29623 
13203859Sml29623 nxge_map_regs_exit:
13213859Sml29623 	if (ddi_status != DDI_SUCCESS)
13223859Sml29623 		status |= (NXGE_ERROR | NXGE_DDI_FAILED);
13233859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_map_regs"));
13243859Sml29623 	return (status);
13253859Sml29623 }
13263859Sml29623 
13273859Sml29623 static void
13283859Sml29623 nxge_unmap_regs(p_nxge_t nxgep)
13293859Sml29623 {
13303859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_unmap_regs"));
13316495Sspeer 
13326495Sspeer 	if (isLDOMguest(nxgep)) {
13336495Sspeer 		nxge_guest_regs_map_free(nxgep);
13346495Sspeer 		return;
13356495Sspeer 	}
13366495Sspeer 
13373859Sml29623 	if (nxgep->dev_regs) {
13383859Sml29623 		if (nxgep->dev_regs->nxge_pciregh) {
13393859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
13406512Ssowmini 			    "==> nxge_unmap_regs: bus"));
13413859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_pciregh);
13423859Sml29623 			nxgep->dev_regs->nxge_pciregh = NULL;
13433859Sml29623 		}
13443859Sml29623 		if (nxgep->dev_regs->nxge_regh) {
13453859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
13466512Ssowmini 			    "==> nxge_unmap_regs: device registers"));
13473859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_regh);
13483859Sml29623 			nxgep->dev_regs->nxge_regh = NULL;
13493859Sml29623 		}
13503859Sml29623 		if (nxgep->dev_regs->nxge_msix_regh) {
13513859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
13526512Ssowmini 			    "==> nxge_unmap_regs: device interrupts"));
13533859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_msix_regh);
13543859Sml29623 			nxgep->dev_regs->nxge_msix_regh = NULL;
13553859Sml29623 		}
13563859Sml29623 		if (nxgep->dev_regs->nxge_vir_regh) {
13573859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
13586512Ssowmini 			    "==> nxge_unmap_regs: vio region"));
13593859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_vir_regh);
13603859Sml29623 			nxgep->dev_regs->nxge_vir_regh = NULL;
13613859Sml29623 		}
13623859Sml29623 		if (nxgep->dev_regs->nxge_vir2_regh) {
13633859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
13646512Ssowmini 			    "==> nxge_unmap_regs: vio2 region"));
13653859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_vir2_regh);
13663859Sml29623 			nxgep->dev_regs->nxge_vir2_regh = NULL;
13673859Sml29623 		}
13683859Sml29623 
13693859Sml29623 		kmem_free(nxgep->dev_regs, sizeof (dev_regs_t));
13703859Sml29623 		nxgep->dev_regs = NULL;
13713859Sml29623 	}
13723859Sml29623 
13733859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_unmap_regs"));
13743859Sml29623 }
13753859Sml29623 
13763859Sml29623 static nxge_status_t
13773859Sml29623 nxge_setup_mutexes(p_nxge_t nxgep)
13783859Sml29623 {
13793859Sml29623 	int ddi_status = DDI_SUCCESS;
13803859Sml29623 	nxge_status_t status = NXGE_OK;
13813859Sml29623 	nxge_classify_t *classify_ptr;
13823859Sml29623 	int partition;
13833859Sml29623 
13843859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_setup_mutexes"));
13853859Sml29623 
13863859Sml29623 	/*
13873859Sml29623 	 * Get the interrupt cookie so the mutexes can be
13883859Sml29623 	 * Initialized.
13893859Sml29623 	 */
13906495Sspeer 	if (isLDOMguest(nxgep)) {
13916495Sspeer 		nxgep->interrupt_cookie = 0;
13926495Sspeer 	} else {
13936495Sspeer 		ddi_status = ddi_get_iblock_cookie(nxgep->dip, 0,
13946495Sspeer 		    &nxgep->interrupt_cookie);
13956495Sspeer 
13966495Sspeer 		if (ddi_status != DDI_SUCCESS) {
13976495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
13986495Sspeer 			    "<== nxge_setup_mutexes: failed 0x%x",
13996495Sspeer 			    ddi_status));
14006495Sspeer 			goto nxge_setup_mutexes_exit;
14016495Sspeer 		}
14023859Sml29623 	}
14033859Sml29623 
14044693Stm144005 	cv_init(&nxgep->poll_cv, NULL, CV_DRIVER, NULL);
14054693Stm144005 	MUTEX_INIT(&nxgep->poll_lock, NULL,
14064693Stm144005 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14074693Stm144005 
14083859Sml29623 	/*
14094693Stm144005 	 * Initialize mutexes for this device.
14103859Sml29623 	 */
14113859Sml29623 	MUTEX_INIT(nxgep->genlock, NULL,
14126512Ssowmini 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14133859Sml29623 	MUTEX_INIT(&nxgep->ouraddr_lock, NULL,
14146512Ssowmini 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14153859Sml29623 	MUTEX_INIT(&nxgep->mif_lock, NULL,
14166512Ssowmini 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14176495Sspeer 	MUTEX_INIT(&nxgep->group_lock, NULL,
14186495Sspeer 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14193859Sml29623 	RW_INIT(&nxgep->filter_lock, NULL,
14206512Ssowmini 	    RW_DRIVER, (void *)nxgep->interrupt_cookie);
14213859Sml29623 
14223859Sml29623 	classify_ptr = &nxgep->classifier;
14233859Sml29623 		/*
14243859Sml29623 		 * FFLP Mutexes are never used in interrupt context
14253859Sml29623 		 * as fflp operation can take very long time to
14263859Sml29623 		 * complete and hence not suitable to invoke from interrupt
14273859Sml29623 		 * handlers.
14283859Sml29623 		 */
14293859Sml29623 	MUTEX_INIT(&classify_ptr->tcam_lock, NULL,
14304732Sdavemq 	    NXGE_MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14314977Sraghus 	if (NXGE_IS_VALID_NEPTUNE_TYPE(nxgep)) {
14323859Sml29623 		MUTEX_INIT(&classify_ptr->fcram_lock, NULL,
14334732Sdavemq 		    NXGE_MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14343859Sml29623 		for (partition = 0; partition < MAX_PARTITION; partition++) {
14353859Sml29623 			MUTEX_INIT(&classify_ptr->hash_lock[partition], NULL,
14363859Sml29623 			    NXGE_MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14373859Sml29623 		}
14383859Sml29623 	}
14393859Sml29623 
14403859Sml29623 nxge_setup_mutexes_exit:
14413859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
14424732Sdavemq 	    "<== nxge_setup_mutexes status = %x", status));
14433859Sml29623 
14443859Sml29623 	if (ddi_status != DDI_SUCCESS)
14453859Sml29623 		status |= (NXGE_ERROR | NXGE_DDI_FAILED);
14463859Sml29623 
14473859Sml29623 	return (status);
14483859Sml29623 }
14493859Sml29623 
14503859Sml29623 static void
14513859Sml29623 nxge_destroy_mutexes(p_nxge_t nxgep)
14523859Sml29623 {
14533859Sml29623 	int partition;
14543859Sml29623 	nxge_classify_t *classify_ptr;
14553859Sml29623 
14563859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_destroy_mutexes"));
14573859Sml29623 	RW_DESTROY(&nxgep->filter_lock);
14586495Sspeer 	MUTEX_DESTROY(&nxgep->group_lock);
14593859Sml29623 	MUTEX_DESTROY(&nxgep->mif_lock);
14603859Sml29623 	MUTEX_DESTROY(&nxgep->ouraddr_lock);
14613859Sml29623 	MUTEX_DESTROY(nxgep->genlock);
14623859Sml29623 
14633859Sml29623 	classify_ptr = &nxgep->classifier;
14643859Sml29623 	MUTEX_DESTROY(&classify_ptr->tcam_lock);
14653859Sml29623 
14664693Stm144005 	/* Destroy all polling resources. */
14674693Stm144005 	MUTEX_DESTROY(&nxgep->poll_lock);
14684693Stm144005 	cv_destroy(&nxgep->poll_cv);
14694693Stm144005 
14704693Stm144005 	/* free data structures, based on HW type */
14714977Sraghus 	if (NXGE_IS_VALID_NEPTUNE_TYPE(nxgep)) {
14723859Sml29623 		MUTEX_DESTROY(&classify_ptr->fcram_lock);
14733859Sml29623 		for (partition = 0; partition < MAX_PARTITION; partition++) {
14743859Sml29623 			MUTEX_DESTROY(&classify_ptr->hash_lock[partition]);
14753859Sml29623 		}
14763859Sml29623 	}
14773859Sml29623 
14783859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_destroy_mutexes"));
14793859Sml29623 }
14803859Sml29623 
14813859Sml29623 nxge_status_t
14823859Sml29623 nxge_init(p_nxge_t nxgep)
14833859Sml29623 {
14846495Sspeer 	nxge_status_t status = NXGE_OK;
14853859Sml29623 
14863859Sml29623 	NXGE_DEBUG_MSG((nxgep, STR_CTL, "==> nxge_init"));
14873859Sml29623 
14883859Sml29623 	if (nxgep->drv_state & STATE_HW_INITIALIZED) {
14893859Sml29623 		return (status);
14903859Sml29623 	}
14913859Sml29623 
14923859Sml29623 	/*
14933859Sml29623 	 * Allocate system memory for the receive/transmit buffer blocks
14943859Sml29623 	 * and receive/transmit descriptor rings.
14953859Sml29623 	 */
14963859Sml29623 	status = nxge_alloc_mem_pool(nxgep);
14973859Sml29623 	if (status != NXGE_OK) {
14983859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "alloc mem failed\n"));
14993859Sml29623 		goto nxge_init_fail1;
15003859Sml29623 	}
15013859Sml29623 
15026495Sspeer 	if (!isLDOMguest(nxgep)) {
15036495Sspeer 		/*
15046495Sspeer 		 * Initialize and enable the TXC registers.
15056495Sspeer 		 * (Globally enable the Tx controller,
15066495Sspeer 		 *  enable the port, configure the dma channel bitmap,
15076495Sspeer 		 *  configure the max burst size).
15086495Sspeer 		 */
15096495Sspeer 		status = nxge_txc_init(nxgep);
15106495Sspeer 		if (status != NXGE_OK) {
15116495Sspeer 			NXGE_ERROR_MSG((nxgep,
15126495Sspeer 			    NXGE_ERR_CTL, "init txc failed\n"));
15136495Sspeer 			goto nxge_init_fail2;
15146495Sspeer 		}
15153859Sml29623 	}
15163859Sml29623 
15173859Sml29623 	/*
15183859Sml29623 	 * Initialize and enable TXDMA channels.
15193859Sml29623 	 */
15203859Sml29623 	status = nxge_init_txdma_channels(nxgep);
15213859Sml29623 	if (status != NXGE_OK) {
15223859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init txdma failed\n"));
15233859Sml29623 		goto nxge_init_fail3;
15243859Sml29623 	}
15253859Sml29623 
15263859Sml29623 	/*
15273859Sml29623 	 * Initialize and enable RXDMA channels.
15283859Sml29623 	 */
15293859Sml29623 	status = nxge_init_rxdma_channels(nxgep);
15303859Sml29623 	if (status != NXGE_OK) {
15313859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init rxdma failed\n"));
15323859Sml29623 		goto nxge_init_fail4;
15333859Sml29623 	}
15343859Sml29623 
15353859Sml29623 	/*
15366495Sspeer 	 * The guest domain is now done.
15376495Sspeer 	 */
15386495Sspeer 	if (isLDOMguest(nxgep)) {
15396495Sspeer 		nxgep->drv_state |= STATE_HW_INITIALIZED;
15406495Sspeer 		goto nxge_init_exit;
15416495Sspeer 	}
15426495Sspeer 
15436495Sspeer 	/*
15443859Sml29623 	 * Initialize TCAM and FCRAM (Neptune).
15453859Sml29623 	 */
15463859Sml29623 	status = nxge_classify_init(nxgep);
15473859Sml29623 	if (status != NXGE_OK) {
15483859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init classify failed\n"));
15493859Sml29623 		goto nxge_init_fail5;
15503859Sml29623 	}
15513859Sml29623 
15523859Sml29623 	/*
15533859Sml29623 	 * Initialize ZCP
15543859Sml29623 	 */
15553859Sml29623 	status = nxge_zcp_init(nxgep);
15563859Sml29623 	if (status != NXGE_OK) {
15573859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init ZCP failed\n"));
15583859Sml29623 		goto nxge_init_fail5;
15593859Sml29623 	}
15603859Sml29623 
15613859Sml29623 	/*
15623859Sml29623 	 * Initialize IPP.
15633859Sml29623 	 */
15643859Sml29623 	status = nxge_ipp_init(nxgep);
15653859Sml29623 	if (status != NXGE_OK) {
15663859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init IPP failed\n"));
15673859Sml29623 		goto nxge_init_fail5;
15683859Sml29623 	}
15693859Sml29623 
15703859Sml29623 	/*
15713859Sml29623 	 * Initialize the MAC block.
15723859Sml29623 	 */
15733859Sml29623 	status = nxge_mac_init(nxgep);
15743859Sml29623 	if (status != NXGE_OK) {
15753859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init MAC failed\n"));
15763859Sml29623 		goto nxge_init_fail5;
15773859Sml29623 	}
15783859Sml29623 
15796495Sspeer 	nxge_intrs_enable(nxgep); /* XXX What changes do I need to make here? */
15803859Sml29623 
15813859Sml29623 	/*
15823859Sml29623 	 * Enable hardware interrupts.
15833859Sml29623 	 */
15843859Sml29623 	nxge_intr_hw_enable(nxgep);
15853859Sml29623 	nxgep->drv_state |= STATE_HW_INITIALIZED;
15863859Sml29623 
15873859Sml29623 	goto nxge_init_exit;
15883859Sml29623 
15893859Sml29623 nxge_init_fail5:
15903859Sml29623 	nxge_uninit_rxdma_channels(nxgep);
15913859Sml29623 nxge_init_fail4:
15923859Sml29623 	nxge_uninit_txdma_channels(nxgep);
15933859Sml29623 nxge_init_fail3:
15946495Sspeer 	if (!isLDOMguest(nxgep)) {
15956495Sspeer 		(void) nxge_txc_uninit(nxgep);
15966495Sspeer 	}
15973859Sml29623 nxge_init_fail2:
15983859Sml29623 	nxge_free_mem_pool(nxgep);
15993859Sml29623 nxge_init_fail1:
16003859Sml29623 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
16016512Ssowmini 	    "<== nxge_init status (failed) = 0x%08x", status));
16023859Sml29623 	return (status);
16033859Sml29623 
16043859Sml29623 nxge_init_exit:
16053859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_init status = 0x%08x",
16066512Ssowmini 	    status));
16073859Sml29623 	return (status);
16083859Sml29623 }
16093859Sml29623 
16103859Sml29623 
16113859Sml29623 timeout_id_t
16123859Sml29623 nxge_start_timer(p_nxge_t nxgep, fptrv_t func, int msec)
16133859Sml29623 {
16146512Ssowmini 	if ((nxgep->suspended == 0) || (nxgep->suspended == DDI_RESUME)) {
16153859Sml29623 		return (timeout(func, (caddr_t)nxgep,
16166512Ssowmini 		    drv_usectohz(1000 * msec)));
16173859Sml29623 	}
16183859Sml29623 	return (NULL);
16193859Sml29623 }
16203859Sml29623 
16213859Sml29623 /*ARGSUSED*/
16223859Sml29623 void
16233859Sml29623 nxge_stop_timer(p_nxge_t nxgep, timeout_id_t timerid)
16243859Sml29623 {
16253859Sml29623 	if (timerid) {
16263859Sml29623 		(void) untimeout(timerid);
16273859Sml29623 	}
16283859Sml29623 }
16293859Sml29623 
16303859Sml29623 void
16313859Sml29623 nxge_uninit(p_nxge_t nxgep)
16323859Sml29623 {
16333859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_uninit"));
16343859Sml29623 
16353859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
16363859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
16376512Ssowmini 		    "==> nxge_uninit: not initialized"));
16383859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
16396512Ssowmini 		    "<== nxge_uninit"));
16403859Sml29623 		return;
16413859Sml29623 	}
16423859Sml29623 
16433859Sml29623 	/* stop timer */
16443859Sml29623 	if (nxgep->nxge_timerid) {
16453859Sml29623 		nxge_stop_timer(nxgep, nxgep->nxge_timerid);
16463859Sml29623 		nxgep->nxge_timerid = 0;
16473859Sml29623 	}
16483859Sml29623 
16493859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_STOP);
16503859Sml29623 	(void) nxge_intr_hw_disable(nxgep);
16513859Sml29623 
16523859Sml29623 	/*
16533859Sml29623 	 * Reset the receive MAC side.
16543859Sml29623 	 */
16553859Sml29623 	(void) nxge_rx_mac_disable(nxgep);
16563859Sml29623 
16573859Sml29623 	/* Disable and soft reset the IPP */
16586495Sspeer 	if (!isLDOMguest(nxgep))
16596495Sspeer 		(void) nxge_ipp_disable(nxgep);
16603859Sml29623 
16613859Sml29623 	/* Free classification resources */
16623859Sml29623 	(void) nxge_classify_uninit(nxgep);
16633859Sml29623 
16643859Sml29623 	/*
16653859Sml29623 	 * Reset the transmit/receive DMA side.
16663859Sml29623 	 */
16673859Sml29623 	(void) nxge_txdma_hw_mode(nxgep, NXGE_DMA_STOP);
16683859Sml29623 	(void) nxge_rxdma_hw_mode(nxgep, NXGE_DMA_STOP);
16693859Sml29623 
16703859Sml29623 	nxge_uninit_txdma_channels(nxgep);
16713859Sml29623 	nxge_uninit_rxdma_channels(nxgep);
16723859Sml29623 
16733859Sml29623 	/*
16743859Sml29623 	 * Reset the transmit MAC side.
16753859Sml29623 	 */
16763859Sml29623 	(void) nxge_tx_mac_disable(nxgep);
16773859Sml29623 
16783859Sml29623 	nxge_free_mem_pool(nxgep);
16793859Sml29623 
16806705Sml29623 	/*
16816705Sml29623 	 * Start the timer if the reset flag is not set.
16826705Sml29623 	 * If this reset flag is set, the link monitor
16836705Sml29623 	 * will not be started in order to stop furthur bus
16846705Sml29623 	 * activities coming from this interface.
16856705Sml29623 	 * The driver will start the monitor function
16866705Sml29623 	 * if the interface was initialized again later.
16876705Sml29623 	 */
16886705Sml29623 	if (!nxge_peu_reset_enable) {
16896705Sml29623 		(void) nxge_link_monitor(nxgep, LINK_MONITOR_START);
16906705Sml29623 	}
16913859Sml29623 
16923859Sml29623 	nxgep->drv_state &= ~STATE_HW_INITIALIZED;
16933859Sml29623 
16943859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_uninit: "
16956512Ssowmini 	    "nxge_mblks_pending %d", nxge_mblks_pending));
16963859Sml29623 }
16973859Sml29623 
16983859Sml29623 void
16993859Sml29623 nxge_get64(p_nxge_t nxgep, p_mblk_t mp)
17003859Sml29623 {
17015125Sjoycey #if defined(__i386)
17025125Sjoycey 	size_t		reg;
17035125Sjoycey #else
17043859Sml29623 	uint64_t	reg;
17055125Sjoycey #endif
17063859Sml29623 	uint64_t	regdata;
17073859Sml29623 	int		i, retry;
17083859Sml29623 
17093859Sml29623 	bcopy((char *)mp->b_rptr, (char *)&reg, sizeof (uint64_t));
17103859Sml29623 	regdata = 0;
17113859Sml29623 	retry = 1;
17123859Sml29623 
17133859Sml29623 	for (i = 0; i < retry; i++) {
17143859Sml29623 		NXGE_REG_RD64(nxgep->npi_handle, reg, &regdata);
17153859Sml29623 	}
17163859Sml29623 	bcopy((char *)&regdata, (char *)mp->b_rptr, sizeof (uint64_t));
17173859Sml29623 }
17183859Sml29623 
17193859Sml29623 void
17203859Sml29623 nxge_put64(p_nxge_t nxgep, p_mblk_t mp)
17213859Sml29623 {
17225125Sjoycey #if defined(__i386)
17235125Sjoycey 	size_t		reg;
17245125Sjoycey #else
17253859Sml29623 	uint64_t	reg;
17265125Sjoycey #endif
17273859Sml29623 	uint64_t	buf[2];
17283859Sml29623 
17293859Sml29623 	bcopy((char *)mp->b_rptr, (char *)&buf[0], 2 * sizeof (uint64_t));
17305133Sjoycey #if defined(__i386)
17315133Sjoycey 	reg = (size_t)buf[0];
17325133Sjoycey #else
17333859Sml29623 	reg = buf[0];
17345133Sjoycey #endif
17353859Sml29623 
17363859Sml29623 	NXGE_NPI_PIO_WRITE64(nxgep->npi_handle, reg, buf[1]);
17373859Sml29623 }
17383859Sml29623 
17393859Sml29623 
17403859Sml29623 nxge_os_mutex_t nxgedebuglock;
17413859Sml29623 int nxge_debug_init = 0;
17423859Sml29623 
17433859Sml29623 /*ARGSUSED*/
17443859Sml29623 /*VARARGS*/
17453859Sml29623 void
17463859Sml29623 nxge_debug_msg(p_nxge_t nxgep, uint64_t level, char *fmt, ...)
17473859Sml29623 {
17483859Sml29623 	char msg_buffer[1048];
17493859Sml29623 	char prefix_buffer[32];
17503859Sml29623 	int instance;
17513859Sml29623 	uint64_t debug_level;
17523859Sml29623 	int cmn_level = CE_CONT;
17533859Sml29623 	va_list ap;
17543859Sml29623 
17556495Sspeer 	if (nxgep && nxgep->nxge_debug_level != nxge_debug_level) {
17566495Sspeer 		/* In case a developer has changed nxge_debug_level. */
17576495Sspeer 		if (nxgep->nxge_debug_level != nxge_debug_level)
17586495Sspeer 			nxgep->nxge_debug_level = nxge_debug_level;
17596495Sspeer 	}
17606495Sspeer 
17613859Sml29623 	debug_level = (nxgep == NULL) ? nxge_debug_level :
17626512Ssowmini 	    nxgep->nxge_debug_level;
17633859Sml29623 
17643859Sml29623 	if ((level & debug_level) ||
17656512Ssowmini 	    (level == NXGE_NOTE) ||
17666512Ssowmini 	    (level == NXGE_ERR_CTL)) {
17673859Sml29623 		/* do the msg processing */
17683859Sml29623 		if (nxge_debug_init == 0) {
17693859Sml29623 			MUTEX_INIT(&nxgedebuglock, NULL, MUTEX_DRIVER, NULL);
17703859Sml29623 			nxge_debug_init = 1;
17713859Sml29623 		}
17723859Sml29623 
17733859Sml29623 		MUTEX_ENTER(&nxgedebuglock);
17743859Sml29623 
17753859Sml29623 		if ((level & NXGE_NOTE)) {
17763859Sml29623 			cmn_level = CE_NOTE;
17773859Sml29623 		}
17783859Sml29623 
17793859Sml29623 		if (level & NXGE_ERR_CTL) {
17803859Sml29623 			cmn_level = CE_WARN;
17813859Sml29623 		}
17823859Sml29623 
17833859Sml29623 		va_start(ap, fmt);
17843859Sml29623 		(void) vsprintf(msg_buffer, fmt, ap);
17853859Sml29623 		va_end(ap);
17863859Sml29623 		if (nxgep == NULL) {
17873859Sml29623 			instance = -1;
17883859Sml29623 			(void) sprintf(prefix_buffer, "%s :", "nxge");
17893859Sml29623 		} else {
17903859Sml29623 			instance = nxgep->instance;
17913859Sml29623 			(void) sprintf(prefix_buffer,
17926512Ssowmini 			    "%s%d :", "nxge", instance);
17933859Sml29623 		}
17943859Sml29623 
17953859Sml29623 		MUTEX_EXIT(&nxgedebuglock);
17963859Sml29623 		cmn_err(cmn_level, "!%s %s\n",
17976512Ssowmini 		    prefix_buffer, msg_buffer);
17983859Sml29623 
17993859Sml29623 	}
18003859Sml29623 }
18013859Sml29623 
18023859Sml29623 char *
18033859Sml29623 nxge_dump_packet(char *addr, int size)
18043859Sml29623 {
18053859Sml29623 	uchar_t *ap = (uchar_t *)addr;
18063859Sml29623 	int i;
18073859Sml29623 	static char etherbuf[1024];
18083859Sml29623 	char *cp = etherbuf;
18093859Sml29623 	char digits[] = "0123456789abcdef";
18103859Sml29623 
18113859Sml29623 	if (!size)
18123859Sml29623 		size = 60;
18133859Sml29623 
18143859Sml29623 	if (size > MAX_DUMP_SZ) {
18153859Sml29623 		/* Dump the leading bytes */
18163859Sml29623 		for (i = 0; i < MAX_DUMP_SZ/2; i++) {
18173859Sml29623 			if (*ap > 0x0f)
18183859Sml29623 				*cp++ = digits[*ap >> 4];
18193859Sml29623 			*cp++ = digits[*ap++ & 0xf];
18203859Sml29623 			*cp++ = ':';
18213859Sml29623 		}
18223859Sml29623 		for (i = 0; i < 20; i++)
18233859Sml29623 			*cp++ = '.';
18243859Sml29623 		/* Dump the last MAX_DUMP_SZ/2 bytes */
18253859Sml29623 		ap = (uchar_t *)(addr + (size - MAX_DUMP_SZ/2));
18263859Sml29623 		for (i = 0; i < MAX_DUMP_SZ/2; i++) {
18273859Sml29623 			if (*ap > 0x0f)
18283859Sml29623 				*cp++ = digits[*ap >> 4];
18293859Sml29623 			*cp++ = digits[*ap++ & 0xf];
18303859Sml29623 			*cp++ = ':';
18313859Sml29623 		}
18323859Sml29623 	} else {
18333859Sml29623 		for (i = 0; i < size; i++) {
18343859Sml29623 			if (*ap > 0x0f)
18353859Sml29623 				*cp++ = digits[*ap >> 4];
18363859Sml29623 			*cp++ = digits[*ap++ & 0xf];
18373859Sml29623 			*cp++ = ':';
18383859Sml29623 		}
18393859Sml29623 	}
18403859Sml29623 	*--cp = 0;
18413859Sml29623 	return (etherbuf);
18423859Sml29623 }
18433859Sml29623 
18443859Sml29623 #ifdef	NXGE_DEBUG
18453859Sml29623 static void
18463859Sml29623 nxge_test_map_regs(p_nxge_t nxgep)
18473859Sml29623 {
18483859Sml29623 	ddi_acc_handle_t cfg_handle;
18493859Sml29623 	p_pci_cfg_t	cfg_ptr;
18503859Sml29623 	ddi_acc_handle_t dev_handle;
18513859Sml29623 	char		*dev_ptr;
18523859Sml29623 	ddi_acc_handle_t pci_config_handle;
18533859Sml29623 	uint32_t	regval;
18543859Sml29623 	int		i;
18553859Sml29623 
18563859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_test_map_regs"));
18573859Sml29623 
18583859Sml29623 	dev_handle = nxgep->dev_regs->nxge_regh;
18593859Sml29623 	dev_ptr = (char *)nxgep->dev_regs->nxge_regp;
18603859Sml29623 
18614977Sraghus 	if (NXGE_IS_VALID_NEPTUNE_TYPE(nxgep)) {
18623859Sml29623 		cfg_handle = nxgep->dev_regs->nxge_pciregh;
18633859Sml29623 		cfg_ptr = (void *)nxgep->dev_regs->nxge_pciregp;
18643859Sml29623 
18653859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
18664732Sdavemq 		    "Neptune PCI regp cfg_ptr 0x%llx", (char *)cfg_ptr));
18673859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
18684732Sdavemq 		    "Neptune PCI cfg_ptr vendor id ptr 0x%llx",
18694732Sdavemq 		    &cfg_ptr->vendorid));
18703859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
18714732Sdavemq 		    "\tvendorid 0x%x devid 0x%x",
18724732Sdavemq 		    NXGE_PIO_READ16(cfg_handle, &cfg_ptr->vendorid, 0),
18734732Sdavemq 		    NXGE_PIO_READ16(cfg_handle, &cfg_ptr->devid,    0)));
18743859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
18754732Sdavemq 		    "PCI BAR: base 0x%x base14 0x%x base 18 0x%x "
18764732Sdavemq 		    "bar1c 0x%x",
18774732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base,   0),
18784732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base14, 0),
18794732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base18, 0),
18804732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base1c, 0)));
18813859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
18824732Sdavemq 		    "\nNeptune PCI BAR: base20 0x%x base24 0x%x "
18834732Sdavemq 		    "base 28 0x%x bar2c 0x%x\n",
18844732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base20, 0),
18854732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base24, 0),
18864732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base28, 0),
18874732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base2c, 0)));
18883859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
18894732Sdavemq 		    "\nNeptune PCI BAR: base30 0x%x\n",
18904732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base30, 0)));
18913859Sml29623 
18923859Sml29623 		cfg_handle = nxgep->dev_regs->nxge_pciregh;
18933859Sml29623 		cfg_ptr = (void *)nxgep->dev_regs->nxge_pciregp;
18943859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
18954732Sdavemq 		    "first  0x%llx second 0x%llx third 0x%llx "
18964732Sdavemq 		    "last 0x%llx ",
18974732Sdavemq 		    NXGE_PIO_READ64(dev_handle,
18984732Sdavemq 		    (uint64_t *)(dev_ptr + 0),  0),
18994732Sdavemq 		    NXGE_PIO_READ64(dev_handle,
19004732Sdavemq 		    (uint64_t *)(dev_ptr + 8),  0),
19014732Sdavemq 		    NXGE_PIO_READ64(dev_handle,
19024732Sdavemq 		    (uint64_t *)(dev_ptr + 16), 0),
19034732Sdavemq 		    NXGE_PIO_READ64(cfg_handle,
19044732Sdavemq 		    (uint64_t *)(dev_ptr + 24), 0)));
19053859Sml29623 	}
19063859Sml29623 }
19073859Sml29623 
19083859Sml29623 #endif
19093859Sml29623 
19103859Sml29623 static void
19113859Sml29623 nxge_suspend(p_nxge_t nxgep)
19123859Sml29623 {
19133859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_suspend"));
19143859Sml29623 
19153859Sml29623 	nxge_intrs_disable(nxgep);
19163859Sml29623 	nxge_destroy_dev(nxgep);
19173859Sml29623 
19183859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_suspend"));
19193859Sml29623 }
19203859Sml29623 
19213859Sml29623 static nxge_status_t
19223859Sml29623 nxge_resume(p_nxge_t nxgep)
19233859Sml29623 {
19243859Sml29623 	nxge_status_t status = NXGE_OK;
19253859Sml29623 
19263859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_resume"));
19274587Sjoycey 
19283859Sml29623 	nxgep->suspended = DDI_RESUME;
19294587Sjoycey 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_START);
19304587Sjoycey 	(void) nxge_rxdma_hw_mode(nxgep, NXGE_DMA_START);
19314587Sjoycey 	(void) nxge_txdma_hw_mode(nxgep, NXGE_DMA_START);
19324587Sjoycey 	(void) nxge_rx_mac_enable(nxgep);
19334587Sjoycey 	(void) nxge_tx_mac_enable(nxgep);
19344587Sjoycey 	nxge_intrs_enable(nxgep);
19353859Sml29623 	nxgep->suspended = 0;
19363859Sml29623 
19373859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19386512Ssowmini 	    "<== nxge_resume status = 0x%x", status));
19393859Sml29623 	return (status);
19403859Sml29623 }
19413859Sml29623 
19423859Sml29623 static nxge_status_t
19433859Sml29623 nxge_setup_dev(p_nxge_t nxgep)
19443859Sml29623 {
19453859Sml29623 	nxge_status_t	status = NXGE_OK;
19463859Sml29623 
19473859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_setup_dev port %d",
19484732Sdavemq 	    nxgep->mac.portnum));
19493859Sml29623 
19503859Sml29623 	status = nxge_link_init(nxgep);
19513859Sml29623 
19523859Sml29623 	if (fm_check_acc_handle(nxgep->dev_regs->nxge_regh) != DDI_FM_OK) {
19533859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
19546512Ssowmini 		    "port%d Bad register acc handle", nxgep->mac.portnum));
19553859Sml29623 		status = NXGE_ERROR;
19563859Sml29623 	}
19573859Sml29623 
19583859Sml29623 	if (status != NXGE_OK) {
19593859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
19606512Ssowmini 		    " nxge_setup_dev status "
19616512Ssowmini 		    "(xcvr init 0x%08x)", status));
19623859Sml29623 		goto nxge_setup_dev_exit;
19633859Sml29623 	}
19643859Sml29623 
19653859Sml29623 nxge_setup_dev_exit:
19663859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19676512Ssowmini 	    "<== nxge_setup_dev port %d status = 0x%08x",
19686512Ssowmini 	    nxgep->mac.portnum, status));
19693859Sml29623 
19703859Sml29623 	return (status);
19713859Sml29623 }
19723859Sml29623 
19733859Sml29623 static void
19743859Sml29623 nxge_destroy_dev(p_nxge_t nxgep)
19753859Sml29623 {
19763859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_destroy_dev"));
19773859Sml29623 
19783859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_STOP);
19793859Sml29623 
19803859Sml29623 	(void) nxge_hw_stop(nxgep);
19813859Sml29623 
19823859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_destroy_dev"));
19833859Sml29623 }
19843859Sml29623 
19853859Sml29623 static nxge_status_t
19863859Sml29623 nxge_setup_system_dma_pages(p_nxge_t nxgep)
19873859Sml29623 {
19883859Sml29623 	int 			ddi_status = DDI_SUCCESS;
19893859Sml29623 	uint_t 			count;
19903859Sml29623 	ddi_dma_cookie_t 	cookie;
19913859Sml29623 	uint_t 			iommu_pagesize;
19923859Sml29623 	nxge_status_t		status = NXGE_OK;
19933859Sml29623 
19946495Sspeer 	NXGE_ERROR_MSG((nxgep, DDI_CTL, "==> nxge_setup_system_dma_pages"));
19953859Sml29623 	nxgep->sys_page_sz = ddi_ptob(nxgep->dip, (ulong_t)1);
19963859Sml29623 	if (nxgep->niu_type != N2_NIU) {
19973859Sml29623 		iommu_pagesize = dvma_pagesize(nxgep->dip);
19983859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19996512Ssowmini 		    " nxge_setup_system_dma_pages: page %d (ddi_ptob %d) "
20006512Ssowmini 		    " default_block_size %d iommu_pagesize %d",
20016512Ssowmini 		    nxgep->sys_page_sz,
20026512Ssowmini 		    ddi_ptob(nxgep->dip, (ulong_t)1),
20036512Ssowmini 		    nxgep->rx_default_block_size,
20046512Ssowmini 		    iommu_pagesize));
20053859Sml29623 
20063859Sml29623 		if (iommu_pagesize != 0) {
20073859Sml29623 			if (nxgep->sys_page_sz == iommu_pagesize) {
20083859Sml29623 				if (iommu_pagesize > 0x4000)
20093859Sml29623 					nxgep->sys_page_sz = 0x4000;
20103859Sml29623 			} else {
20113859Sml29623 				if (nxgep->sys_page_sz > iommu_pagesize)
20123859Sml29623 					nxgep->sys_page_sz = iommu_pagesize;
20133859Sml29623 			}
20143859Sml29623 		}
20153859Sml29623 	}
20163859Sml29623 	nxgep->sys_page_mask = ~(nxgep->sys_page_sz - 1);
20173859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
20186512Ssowmini 	    "==> nxge_setup_system_dma_pages: page %d (ddi_ptob %d) "
20196512Ssowmini 	    "default_block_size %d page mask %d",
20206512Ssowmini 	    nxgep->sys_page_sz,
20216512Ssowmini 	    ddi_ptob(nxgep->dip, (ulong_t)1),
20226512Ssowmini 	    nxgep->rx_default_block_size,
20236512Ssowmini 	    nxgep->sys_page_mask));
20243859Sml29623 
20253859Sml29623 
20263859Sml29623 	switch (nxgep->sys_page_sz) {
20273859Sml29623 	default:
20283859Sml29623 		nxgep->sys_page_sz = 0x1000;
20293859Sml29623 		nxgep->sys_page_mask = ~(nxgep->sys_page_sz - 1);
20303859Sml29623 		nxgep->rx_default_block_size = 0x1000;
20313859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_4K;
20323859Sml29623 		break;
20333859Sml29623 	case 0x1000:
20343859Sml29623 		nxgep->rx_default_block_size = 0x1000;
20353859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_4K;
20363859Sml29623 		break;
20373859Sml29623 	case 0x2000:
20383859Sml29623 		nxgep->rx_default_block_size = 0x2000;
20393859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_8K;
20403859Sml29623 		break;
20413859Sml29623 	case 0x4000:
20423859Sml29623 		nxgep->rx_default_block_size = 0x4000;
20433859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_16K;
20443859Sml29623 		break;
20453859Sml29623 	case 0x8000:
20463859Sml29623 		nxgep->rx_default_block_size = 0x8000;
20473859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_32K;
20483859Sml29623 		break;
20493859Sml29623 	}
20503859Sml29623 
20513859Sml29623 #ifndef USE_RX_BIG_BUF
20523859Sml29623 	nxge_rx_dma_attr.dma_attr_align = nxgep->sys_page_sz;
20533859Sml29623 #else
20543859Sml29623 		nxgep->rx_default_block_size = 0x2000;
20553859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_8K;
20563859Sml29623 #endif
20573859Sml29623 	/*
20583859Sml29623 	 * Get the system DMA burst size.
20593859Sml29623 	 */
20603859Sml29623 	ddi_status = ddi_dma_alloc_handle(nxgep->dip, &nxge_tx_dma_attr,
20616512Ssowmini 	    DDI_DMA_DONTWAIT, 0,
20626512Ssowmini 	    &nxgep->dmasparehandle);
20633859Sml29623 	if (ddi_status != DDI_SUCCESS) {
20643859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
20656512Ssowmini 		    "ddi_dma_alloc_handle: failed "
20666512Ssowmini 		    " status 0x%x", ddi_status));
20673859Sml29623 		goto nxge_get_soft_properties_exit;
20683859Sml29623 	}
20693859Sml29623 
20703859Sml29623 	ddi_status = ddi_dma_addr_bind_handle(nxgep->dmasparehandle, NULL,
20716512Ssowmini 	    (caddr_t)nxgep->dmasparehandle,
20726512Ssowmini 	    sizeof (nxgep->dmasparehandle),
20736512Ssowmini 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
20746512Ssowmini 	    DDI_DMA_DONTWAIT, 0,
20756512Ssowmini 	    &cookie, &count);
20763859Sml29623 	if (ddi_status != DDI_DMA_MAPPED) {
20773859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
20786512Ssowmini 		    "Binding spare handle to find system"
20796512Ssowmini 		    " burstsize failed."));
20803859Sml29623 		ddi_status = DDI_FAILURE;
20813859Sml29623 		goto nxge_get_soft_properties_fail1;
20823859Sml29623 	}
20833859Sml29623 
20843859Sml29623 	nxgep->sys_burst_sz = ddi_dma_burstsizes(nxgep->dmasparehandle);
20853859Sml29623 	(void) ddi_dma_unbind_handle(nxgep->dmasparehandle);
20863859Sml29623 
20873859Sml29623 nxge_get_soft_properties_fail1:
20883859Sml29623 	ddi_dma_free_handle(&nxgep->dmasparehandle);
20893859Sml29623 
20903859Sml29623 nxge_get_soft_properties_exit:
20913859Sml29623 
20923859Sml29623 	if (ddi_status != DDI_SUCCESS)
20933859Sml29623 		status |= (NXGE_ERROR | NXGE_DDI_FAILED);
20943859Sml29623 
20953859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
20966512Ssowmini 	    "<== nxge_setup_system_dma_pages status = 0x%08x", status));
20973859Sml29623 	return (status);
20983859Sml29623 }
20993859Sml29623 
21003859Sml29623 static nxge_status_t
21013859Sml29623 nxge_alloc_mem_pool(p_nxge_t nxgep)
21023859Sml29623 {
21033859Sml29623 	nxge_status_t	status = NXGE_OK;
21043859Sml29623 
21053859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_alloc_mem_pool"));
21063859Sml29623 
21073859Sml29623 	status = nxge_alloc_rx_mem_pool(nxgep);
21083859Sml29623 	if (status != NXGE_OK) {
21093859Sml29623 		return (NXGE_ERROR);
21103859Sml29623 	}
21113859Sml29623 
21123859Sml29623 	status = nxge_alloc_tx_mem_pool(nxgep);
21133859Sml29623 	if (status != NXGE_OK) {
21143859Sml29623 		nxge_free_rx_mem_pool(nxgep);
21153859Sml29623 		return (NXGE_ERROR);
21163859Sml29623 	}
21173859Sml29623 
21183859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_alloc_mem_pool"));
21193859Sml29623 	return (NXGE_OK);
21203859Sml29623 }
21213859Sml29623 
21223859Sml29623 static void
21233859Sml29623 nxge_free_mem_pool(p_nxge_t nxgep)
21243859Sml29623 {
21253859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "==> nxge_free_mem_pool"));
21263859Sml29623 
21273859Sml29623 	nxge_free_rx_mem_pool(nxgep);
21283859Sml29623 	nxge_free_tx_mem_pool(nxgep);
21293859Sml29623 
21303859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "<== nxge_free_mem_pool"));
21313859Sml29623 }
21323859Sml29623 
21336495Sspeer nxge_status_t
21343859Sml29623 nxge_alloc_rx_mem_pool(p_nxge_t nxgep)
21353859Sml29623 {
21366495Sspeer 	uint32_t		rdc_max;
21373859Sml29623 	p_nxge_dma_pt_cfg_t	p_all_cfgp;
21383859Sml29623 	p_nxge_hw_pt_cfg_t	p_cfgp;
21393859Sml29623 	p_nxge_dma_pool_t	dma_poolp;
21403859Sml29623 	p_nxge_dma_common_t	*dma_buf_p;
21413859Sml29623 	p_nxge_dma_pool_t	dma_cntl_poolp;
21423859Sml29623 	p_nxge_dma_common_t	*dma_cntl_p;
21433859Sml29623 	uint32_t 		*num_chunks; /* per dma */
21443859Sml29623 	nxge_status_t		status = NXGE_OK;
21453859Sml29623 
21463859Sml29623 	uint32_t		nxge_port_rbr_size;
21473859Sml29623 	uint32_t		nxge_port_rbr_spare_size;
21483859Sml29623 	uint32_t		nxge_port_rcr_size;
21496495Sspeer 	uint32_t		rx_cntl_alloc_size;
21503859Sml29623 
21513859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rx_mem_pool"));
21523859Sml29623 
21533859Sml29623 	p_all_cfgp = (p_nxge_dma_pt_cfg_t)&nxgep->pt_config;
21543859Sml29623 	p_cfgp = (p_nxge_hw_pt_cfg_t)&p_all_cfgp->hw_config;
21556495Sspeer 	rdc_max = NXGE_MAX_RDCS;
21563859Sml29623 
21573859Sml29623 	/*
21586495Sspeer 	 * Allocate memory for the common DMA data structures.
21593859Sml29623 	 */
21603859Sml29623 	dma_poolp = (p_nxge_dma_pool_t)KMEM_ZALLOC(sizeof (nxge_dma_pool_t),
21616512Ssowmini 	    KM_SLEEP);
21623859Sml29623 	dma_buf_p = (p_nxge_dma_common_t *)KMEM_ZALLOC(
21636512Ssowmini 	    sizeof (p_nxge_dma_common_t) * rdc_max, KM_SLEEP);
21643859Sml29623 
21653859Sml29623 	dma_cntl_poolp = (p_nxge_dma_pool_t)
21666512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_pool_t), KM_SLEEP);
21673859Sml29623 	dma_cntl_p = (p_nxge_dma_common_t *)KMEM_ZALLOC(
21686512Ssowmini 	    sizeof (p_nxge_dma_common_t) * rdc_max, KM_SLEEP);
21693859Sml29623 
21703859Sml29623 	num_chunks = (uint32_t *)KMEM_ZALLOC(
21716512Ssowmini 	    sizeof (uint32_t) * rdc_max, KM_SLEEP);
21723859Sml29623 
21733859Sml29623 	/*
21746495Sspeer 	 * Assume that each DMA channel will be configured with
21756495Sspeer 	 * the default block size.
21766495Sspeer 	 * rbr block counts are modulo the batch count (16).
21773859Sml29623 	 */
21783859Sml29623 	nxge_port_rbr_size = p_all_cfgp->rbr_size;
21793859Sml29623 	nxge_port_rcr_size = p_all_cfgp->rcr_size;
21803859Sml29623 
21813859Sml29623 	if (!nxge_port_rbr_size) {
21823859Sml29623 		nxge_port_rbr_size = NXGE_RBR_RBB_DEFAULT;
21833859Sml29623 	}
21843859Sml29623 	if (nxge_port_rbr_size % NXGE_RXDMA_POST_BATCH) {
21853859Sml29623 		nxge_port_rbr_size = (NXGE_RXDMA_POST_BATCH *
21866512Ssowmini 		    (nxge_port_rbr_size / NXGE_RXDMA_POST_BATCH + 1));
21873859Sml29623 	}
21883859Sml29623 
21893859Sml29623 	p_all_cfgp->rbr_size = nxge_port_rbr_size;
21903859Sml29623 	nxge_port_rbr_spare_size = nxge_rbr_spare_size;
21913859Sml29623 
21923859Sml29623 	if (nxge_port_rbr_spare_size % NXGE_RXDMA_POST_BATCH) {
21933859Sml29623 		nxge_port_rbr_spare_size = (NXGE_RXDMA_POST_BATCH *
21946512Ssowmini 		    (nxge_port_rbr_spare_size / NXGE_RXDMA_POST_BATCH + 1));
21953859Sml29623 	}
21965770Sml29623 	if (nxge_port_rbr_size > RBR_DEFAULT_MAX_BLKS) {
21975770Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM_CTL,
21985770Sml29623 		    "nxge_alloc_rx_mem_pool: RBR size too high %d, "
21995770Sml29623 		    "set to default %d",
22005770Sml29623 		    nxge_port_rbr_size, RBR_DEFAULT_MAX_BLKS));
22015770Sml29623 		nxge_port_rbr_size = RBR_DEFAULT_MAX_BLKS;
22025770Sml29623 	}
22035770Sml29623 	if (nxge_port_rcr_size > RCR_DEFAULT_MAX) {
22045770Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM_CTL,
22055770Sml29623 		    "nxge_alloc_rx_mem_pool: RCR too high %d, "
22065770Sml29623 		    "set to default %d",
22075770Sml29623 		    nxge_port_rcr_size, RCR_DEFAULT_MAX));
22085770Sml29623 		nxge_port_rcr_size = RCR_DEFAULT_MAX;
22095770Sml29623 	}
22103859Sml29623 
22113859Sml29623 	/*
22123859Sml29623 	 * N2/NIU has limitation on the descriptor sizes (contiguous
22133859Sml29623 	 * memory allocation on data buffers to 4M (contig_mem_alloc)
22143859Sml29623 	 * and little endian for control buffers (must use the ddi/dki mem alloc
22153859Sml29623 	 * function).
22163859Sml29623 	 */
22173859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
22183859Sml29623 	if (nxgep->niu_type == N2_NIU) {
22193859Sml29623 		nxge_port_rbr_spare_size = 0;
22203859Sml29623 		if ((nxge_port_rbr_size > NXGE_NIU_CONTIG_RBR_MAX) ||
22216512Ssowmini 		    (!ISP2(nxge_port_rbr_size))) {
22223859Sml29623 			nxge_port_rbr_size = NXGE_NIU_CONTIG_RBR_MAX;
22233859Sml29623 		}
22243859Sml29623 		if ((nxge_port_rcr_size > NXGE_NIU_CONTIG_RCR_MAX) ||
22256512Ssowmini 		    (!ISP2(nxge_port_rcr_size))) {
22263859Sml29623 			nxge_port_rcr_size = NXGE_NIU_CONTIG_RCR_MAX;
22273859Sml29623 		}
22283859Sml29623 	}
22293859Sml29623 #endif
22303859Sml29623 
22313859Sml29623 	/*
22323859Sml29623 	 * Addresses of receive block ring, receive completion ring and the
22333859Sml29623 	 * mailbox must be all cache-aligned (64 bytes).
22343859Sml29623 	 */
22353859Sml29623 	rx_cntl_alloc_size = nxge_port_rbr_size + nxge_port_rbr_spare_size;
22363859Sml29623 	rx_cntl_alloc_size *= (sizeof (rx_desc_t));
22373859Sml29623 	rx_cntl_alloc_size += (sizeof (rcr_entry_t) * nxge_port_rcr_size);
22383859Sml29623 	rx_cntl_alloc_size += sizeof (rxdma_mailbox_t);
22393859Sml29623 
22403859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_alloc_rx_mem_pool: "
22416512Ssowmini 	    "nxge_port_rbr_size = %d nxge_port_rbr_spare_size = %d "
22426512Ssowmini 	    "nxge_port_rcr_size = %d "
22436512Ssowmini 	    "rx_cntl_alloc_size = %d",
22446512Ssowmini 	    nxge_port_rbr_size, nxge_port_rbr_spare_size,
22456512Ssowmini 	    nxge_port_rcr_size,
22466512Ssowmini 	    rx_cntl_alloc_size));
22473859Sml29623 
22483859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
22493859Sml29623 	if (nxgep->niu_type == N2_NIU) {
22506495Sspeer 		uint32_t rx_buf_alloc_size = (nxgep->rx_default_block_size *
22516495Sspeer 		    (nxge_port_rbr_size + nxge_port_rbr_spare_size));
22526495Sspeer 
22533859Sml29623 		if (!ISP2(rx_buf_alloc_size)) {
22543859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
22556512Ssowmini 			    "==> nxge_alloc_rx_mem_pool: "
22566512Ssowmini 			    " must be power of 2"));
22573859Sml29623 			status |= (NXGE_ERROR | NXGE_DDI_FAILED);
22583859Sml29623 			goto nxge_alloc_rx_mem_pool_exit;
22593859Sml29623 		}
22603859Sml29623 
22613859Sml29623 		if (rx_buf_alloc_size > (1 << 22)) {
22623859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
22636512Ssowmini 			    "==> nxge_alloc_rx_mem_pool: "
22646512Ssowmini 			    " limit size to 4M"));
22653859Sml29623 			status |= (NXGE_ERROR | NXGE_DDI_FAILED);
22663859Sml29623 			goto nxge_alloc_rx_mem_pool_exit;
22673859Sml29623 		}
22683859Sml29623 
22693859Sml29623 		if (rx_cntl_alloc_size < 0x2000) {
22703859Sml29623 			rx_cntl_alloc_size = 0x2000;
22713859Sml29623 		}
22723859Sml29623 	}
22733859Sml29623 #endif
22743859Sml29623 	nxgep->nxge_port_rbr_size = nxge_port_rbr_size;
22753859Sml29623 	nxgep->nxge_port_rcr_size = nxge_port_rcr_size;
22766495Sspeer 	nxgep->nxge_port_rbr_spare_size = nxge_port_rbr_spare_size;
22776495Sspeer 	nxgep->nxge_port_rx_cntl_alloc_size = rx_cntl_alloc_size;
22786495Sspeer 
22796495Sspeer 	dma_poolp->ndmas = p_cfgp->max_rdcs;
22803859Sml29623 	dma_poolp->num_chunks = num_chunks;
22813859Sml29623 	dma_poolp->buf_allocated = B_TRUE;
22823859Sml29623 	nxgep->rx_buf_pool_p = dma_poolp;
22833859Sml29623 	dma_poolp->dma_buf_pool_p = dma_buf_p;
22843859Sml29623 
22856495Sspeer 	dma_cntl_poolp->ndmas = p_cfgp->max_rdcs;
22863859Sml29623 	dma_cntl_poolp->buf_allocated = B_TRUE;
22873859Sml29623 	nxgep->rx_cntl_pool_p = dma_cntl_poolp;
22883859Sml29623 	dma_cntl_poolp->dma_buf_pool_p = dma_cntl_p;
22893859Sml29623 
22906495Sspeer 	/* Allocate the receive rings, too. */
22916495Sspeer 	nxgep->rx_rbr_rings =
22926512Ssowmini 	    KMEM_ZALLOC(sizeof (rx_rbr_rings_t), KM_SLEEP);
22936495Sspeer 	nxgep->rx_rbr_rings->rbr_rings =
22946512Ssowmini 	    KMEM_ZALLOC(sizeof (p_rx_rbr_ring_t) * rdc_max, KM_SLEEP);
22956495Sspeer 	nxgep->rx_rcr_rings =
22966512Ssowmini 	    KMEM_ZALLOC(sizeof (rx_rcr_rings_t), KM_SLEEP);
22976495Sspeer 	nxgep->rx_rcr_rings->rcr_rings =
22986512Ssowmini 	    KMEM_ZALLOC(sizeof (p_rx_rcr_ring_t) * rdc_max, KM_SLEEP);
22996495Sspeer 	nxgep->rx_mbox_areas_p =
23006512Ssowmini 	    KMEM_ZALLOC(sizeof (rx_mbox_areas_t), KM_SLEEP);
23016495Sspeer 	nxgep->rx_mbox_areas_p->rxmbox_areas =
23026512Ssowmini 	    KMEM_ZALLOC(sizeof (p_rx_mbox_t) * rdc_max, KM_SLEEP);
23036495Sspeer 
23046495Sspeer 	nxgep->rx_rbr_rings->ndmas = nxgep->rx_rcr_rings->ndmas =
23056495Sspeer 	    p_cfgp->max_rdcs;
23066495Sspeer 
23073859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
23086512Ssowmini 	    "<== nxge_alloc_rx_mem_pool:status 0x%08x", status));
23093859Sml29623 
23103859Sml29623 nxge_alloc_rx_mem_pool_exit:
23116495Sspeer 	return (status);
23126495Sspeer }
23136495Sspeer 
23146495Sspeer /*
23156495Sspeer  * nxge_alloc_rxb
23166495Sspeer  *
23176495Sspeer  *	Allocate buffers for an RDC.
23186495Sspeer  *
23196495Sspeer  * Arguments:
23206495Sspeer  * 	nxgep
23216495Sspeer  * 	channel	The channel to map into our kernel space.
23226495Sspeer  *
23236495Sspeer  * Notes:
23246495Sspeer  *
23256495Sspeer  * NPI function calls:
23266495Sspeer  *
23276495Sspeer  * NXGE function calls:
23286495Sspeer  *
23296495Sspeer  * Registers accessed:
23306495Sspeer  *
23316495Sspeer  * Context:
23326495Sspeer  *
23336495Sspeer  * Taking apart:
23346495Sspeer  *
23356495Sspeer  * Open questions:
23366495Sspeer  *
23376495Sspeer  */
23386495Sspeer nxge_status_t
23396495Sspeer nxge_alloc_rxb(
23406495Sspeer 	p_nxge_t nxgep,
23416495Sspeer 	int channel)
23426495Sspeer {
23436495Sspeer 	size_t			rx_buf_alloc_size;
23446495Sspeer 	nxge_status_t		status = NXGE_OK;
23456495Sspeer 
23466495Sspeer 	nxge_dma_common_t	**data;
23476495Sspeer 	nxge_dma_common_t	**control;
23486495Sspeer 	uint32_t 		*num_chunks;
23496495Sspeer 
23506495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rbb"));
23516495Sspeer 
23526495Sspeer 	/*
23536495Sspeer 	 * Allocate memory for the receive buffers and descriptor rings.
23546495Sspeer 	 * Replace these allocation functions with the interface functions
23556495Sspeer 	 * provided by the partition manager if/when they are available.
23566495Sspeer 	 */
23576495Sspeer 
23586495Sspeer 	/*
23596495Sspeer 	 * Allocate memory for the receive buffer blocks.
23606495Sspeer 	 */
23616495Sspeer 	rx_buf_alloc_size = (nxgep->rx_default_block_size *
23626512Ssowmini 	    (nxgep->nxge_port_rbr_size + nxgep->nxge_port_rbr_spare_size));
23636495Sspeer 
23646495Sspeer 	data = &nxgep->rx_buf_pool_p->dma_buf_pool_p[channel];
23656495Sspeer 	num_chunks = &nxgep->rx_buf_pool_p->num_chunks[channel];
23666495Sspeer 
23676495Sspeer 	if ((status = nxge_alloc_rx_buf_dma(
23686495Sspeer 	    nxgep, channel, data, rx_buf_alloc_size,
23696495Sspeer 	    nxgep->rx_default_block_size, num_chunks)) != NXGE_OK) {
23706495Sspeer 		return (status);
23716495Sspeer 	}
23726495Sspeer 
23736495Sspeer 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "<== nxge_alloc_rxb(): "
23746495Sspeer 	    "dma %d dma_buf_p %llx &dma_buf_p %llx", channel, *data, data));
23756495Sspeer 
23766495Sspeer 	/*
23776495Sspeer 	 * Allocate memory for descriptor rings and mailbox.
23786495Sspeer 	 */
23796495Sspeer 	control = &nxgep->rx_cntl_pool_p->dma_buf_pool_p[channel];
23806495Sspeer 
23816495Sspeer 	if ((status = nxge_alloc_rx_cntl_dma(
23826495Sspeer 	    nxgep, channel, control, nxgep->nxge_port_rx_cntl_alloc_size))
23836495Sspeer 	    != NXGE_OK) {
23846495Sspeer 		nxge_free_rx_cntl_dma(nxgep, *control);
23856495Sspeer 		(*data)->buf_alloc_state |= BUF_ALLOCATED_WAIT_FREE;
23866495Sspeer 		nxge_free_rx_buf_dma(nxgep, *data, *num_chunks);
23876495Sspeer 		return (status);
23886495Sspeer 	}
23896495Sspeer 
23903859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
23916495Sspeer 	    "<== nxge_alloc_rx_mem_pool:status 0x%08x", status));
23923859Sml29623 
23933859Sml29623 	return (status);
23943859Sml29623 }
23953859Sml29623 
23966495Sspeer void
23976495Sspeer nxge_free_rxb(
23986495Sspeer 	p_nxge_t nxgep,
23996495Sspeer 	int channel)
24006495Sspeer {
24016495Sspeer 	nxge_dma_common_t	*data;
24026495Sspeer 	nxge_dma_common_t	*control;
24036495Sspeer 	uint32_t 		num_chunks;
24046495Sspeer 
24056495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rbb"));
24066495Sspeer 
24076495Sspeer 	data = nxgep->rx_buf_pool_p->dma_buf_pool_p[channel];
24086495Sspeer 	num_chunks = nxgep->rx_buf_pool_p->num_chunks[channel];
24096495Sspeer 	nxge_free_rx_buf_dma(nxgep, data, num_chunks);
24106495Sspeer 
24116495Sspeer 	nxgep->rx_buf_pool_p->dma_buf_pool_p[channel] = 0;
24126495Sspeer 	nxgep->rx_buf_pool_p->num_chunks[channel] = 0;
24136495Sspeer 
24146495Sspeer 	control = nxgep->rx_cntl_pool_p->dma_buf_pool_p[channel];
24156495Sspeer 	nxge_free_rx_cntl_dma(nxgep, control);
24166495Sspeer 
24176495Sspeer 	nxgep->rx_cntl_pool_p->dma_buf_pool_p[channel] = 0;
24186495Sspeer 
24196495Sspeer 	KMEM_FREE(data, sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK);
24206495Sspeer 	KMEM_FREE(control, sizeof (nxge_dma_common_t));
24216495Sspeer 
24226495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_alloc_rbb"));
24236495Sspeer }
24246495Sspeer 
24253859Sml29623 static void
24263859Sml29623 nxge_free_rx_mem_pool(p_nxge_t nxgep)
24273859Sml29623 {
24286495Sspeer 	int rdc_max = NXGE_MAX_RDCS;
24293859Sml29623 
24303859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_free_rx_mem_pool"));
24313859Sml29623 
24326495Sspeer 	if (!nxgep->rx_buf_pool_p || !nxgep->rx_buf_pool_p->buf_allocated) {
24333859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
24346512Ssowmini 		    "<== nxge_free_rx_mem_pool "
24356512Ssowmini 		    "(null rx buf pool or buf not allocated"));
24363859Sml29623 		return;
24373859Sml29623 	}
24386495Sspeer 	if (!nxgep->rx_cntl_pool_p || !nxgep->rx_cntl_pool_p->buf_allocated) {
24393859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
24406512Ssowmini 		    "<== nxge_free_rx_mem_pool "
24416512Ssowmini 		    "(null rx cntl buf pool or cntl buf not allocated"));
24423859Sml29623 		return;
24433859Sml29623 	}
24443859Sml29623 
24456495Sspeer 	KMEM_FREE(nxgep->rx_cntl_pool_p->dma_buf_pool_p,
24466495Sspeer 	    sizeof (p_nxge_dma_common_t) * rdc_max);
24476495Sspeer 	KMEM_FREE(nxgep->rx_cntl_pool_p, sizeof (nxge_dma_pool_t));
24486495Sspeer 
24496495Sspeer 	KMEM_FREE(nxgep->rx_buf_pool_p->num_chunks,
24506495Sspeer 	    sizeof (uint32_t) * rdc_max);
24516495Sspeer 	KMEM_FREE(nxgep->rx_buf_pool_p->dma_buf_pool_p,
24526495Sspeer 	    sizeof (p_nxge_dma_common_t) * rdc_max);
24536495Sspeer 	KMEM_FREE(nxgep->rx_buf_pool_p, sizeof (nxge_dma_pool_t));
24546495Sspeer 
24556495Sspeer 	nxgep->rx_buf_pool_p = 0;
24566495Sspeer 	nxgep->rx_cntl_pool_p = 0;
24576495Sspeer 
24586495Sspeer 	KMEM_FREE(nxgep->rx_rbr_rings->rbr_rings,
24596495Sspeer 	    sizeof (p_rx_rbr_ring_t) * rdc_max);
24606495Sspeer 	KMEM_FREE(nxgep->rx_rbr_rings, sizeof (rx_rbr_rings_t));
24616495Sspeer 	KMEM_FREE(nxgep->rx_rcr_rings->rcr_rings,
24626495Sspeer 	    sizeof (p_rx_rcr_ring_t) * rdc_max);
24636495Sspeer 	KMEM_FREE(nxgep->rx_rcr_rings, sizeof (rx_rcr_rings_t));
24646495Sspeer 	KMEM_FREE(nxgep->rx_mbox_areas_p->rxmbox_areas,
24656495Sspeer 	    sizeof (p_rx_mbox_t) * rdc_max);
24666495Sspeer 	KMEM_FREE(nxgep->rx_mbox_areas_p, sizeof (rx_mbox_areas_t));
24676495Sspeer 
24686495Sspeer 	nxgep->rx_rbr_rings = 0;
24696495Sspeer 	nxgep->rx_rcr_rings = 0;
24706495Sspeer 	nxgep->rx_mbox_areas_p = 0;
24713859Sml29623 
24723859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "<== nxge_free_rx_mem_pool"));
24733859Sml29623 }
24743859Sml29623 
24753859Sml29623 
24763859Sml29623 static nxge_status_t
24773859Sml29623 nxge_alloc_rx_buf_dma(p_nxge_t nxgep, uint16_t dma_channel,
24783859Sml29623 	p_nxge_dma_common_t *dmap,
24793859Sml29623 	size_t alloc_size, size_t block_size, uint32_t *num_chunks)
24803859Sml29623 {
24813859Sml29623 	p_nxge_dma_common_t 	rx_dmap;
24823859Sml29623 	nxge_status_t		status = NXGE_OK;
24833859Sml29623 	size_t			total_alloc_size;
24843859Sml29623 	size_t			allocated = 0;
24853859Sml29623 	int			i, size_index, array_size;
24866495Sspeer 	boolean_t		use_kmem_alloc = B_FALSE;
24873859Sml29623 
24883859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rx_buf_dma"));
24893859Sml29623 
24903859Sml29623 	rx_dmap = (p_nxge_dma_common_t)
24916512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK,
24926512Ssowmini 	    KM_SLEEP);
24933859Sml29623 
24943859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
24956512Ssowmini 	    " alloc_rx_buf_dma rdc %d asize %x bsize %x bbuf %llx ",
24966512Ssowmini 	    dma_channel, alloc_size, block_size, dmap));
24973859Sml29623 
24983859Sml29623 	total_alloc_size = alloc_size;
24993859Sml29623 
25003859Sml29623 #if defined(RX_USE_RECLAIM_POST)
25013859Sml29623 	total_alloc_size = alloc_size + alloc_size/4;
25023859Sml29623 #endif
25033859Sml29623 
25043859Sml29623 	i = 0;
25053859Sml29623 	size_index = 0;
25063859Sml29623 	array_size =  sizeof (alloc_sizes)/sizeof (size_t);
25073859Sml29623 	while ((alloc_sizes[size_index] < alloc_size) &&
25086512Ssowmini 	    (size_index < array_size))
25096512Ssowmini 		size_index++;
25103859Sml29623 	if (size_index >= array_size) {
25113859Sml29623 		size_index = array_size - 1;
25123859Sml29623 	}
25133859Sml29623 
25146495Sspeer 	/* For Neptune, use kmem_alloc if the kmem flag is set. */
25156495Sspeer 	if (nxgep->niu_type != N2_NIU && nxge_use_kmem_alloc) {
25166495Sspeer 		use_kmem_alloc = B_TRUE;
25176495Sspeer #if defined(__i386) || defined(__amd64)
25186495Sspeer 		size_index = 0;
25196495Sspeer #endif
25206495Sspeer 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
25216495Sspeer 		    "==> nxge_alloc_rx_buf_dma: "
25226495Sspeer 		    "Neptune use kmem_alloc() - size_index %d",
25236495Sspeer 		    size_index));
25246495Sspeer 	}
25256495Sspeer 
25263859Sml29623 	while ((allocated < total_alloc_size) &&
25276512Ssowmini 	    (size_index >= 0) && (i < NXGE_DMA_BLOCK)) {
25283859Sml29623 		rx_dmap[i].dma_chunk_index = i;
25293859Sml29623 		rx_dmap[i].block_size = block_size;
25303859Sml29623 		rx_dmap[i].alength = alloc_sizes[size_index];
25313859Sml29623 		rx_dmap[i].orig_alength = rx_dmap[i].alength;
25323859Sml29623 		rx_dmap[i].nblocks = alloc_sizes[size_index] / block_size;
25333859Sml29623 		rx_dmap[i].dma_channel = dma_channel;
25343859Sml29623 		rx_dmap[i].contig_alloc_type = B_FALSE;
25356495Sspeer 		rx_dmap[i].kmem_alloc_type = B_FALSE;
25366495Sspeer 		rx_dmap[i].buf_alloc_type = DDI_MEM_ALLOC;
25373859Sml29623 
25383859Sml29623 		/*
25393859Sml29623 		 * N2/NIU: data buffers must be contiguous as the driver
25403859Sml29623 		 *	   needs to call Hypervisor api to set up
25413859Sml29623 		 *	   logical pages.
25423859Sml29623 		 */
25433859Sml29623 		if ((nxgep->niu_type == N2_NIU) && (NXGE_DMA_BLOCK == 1)) {
25443859Sml29623 			rx_dmap[i].contig_alloc_type = B_TRUE;
25456495Sspeer 			rx_dmap[i].buf_alloc_type = CONTIG_MEM_ALLOC;
25466495Sspeer 		} else if (use_kmem_alloc) {
25476495Sspeer 			/* For Neptune, use kmem_alloc */
25486495Sspeer 			NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
25496495Sspeer 			    "==> nxge_alloc_rx_buf_dma: "
25506495Sspeer 			    "Neptune use kmem_alloc()"));
25516495Sspeer 			rx_dmap[i].kmem_alloc_type = B_TRUE;
25526495Sspeer 			rx_dmap[i].buf_alloc_type = KMEM_ALLOC;
25533859Sml29623 		}
25543859Sml29623 
25553859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
25566512Ssowmini 		    "alloc_rx_buf_dma rdc %d chunk %d bufp %llx size %x "
25576512Ssowmini 		    "i %d nblocks %d alength %d",
25586512Ssowmini 		    dma_channel, i, &rx_dmap[i], block_size,
25596512Ssowmini 		    i, rx_dmap[i].nblocks,
25606512Ssowmini 		    rx_dmap[i].alength));
25613859Sml29623 		status = nxge_dma_mem_alloc(nxgep, nxge_force_dma,
25626512Ssowmini 		    &nxge_rx_dma_attr,
25636512Ssowmini 		    rx_dmap[i].alength,
25646512Ssowmini 		    &nxge_dev_buf_dma_acc_attr,
25656512Ssowmini 		    DDI_DMA_READ | DDI_DMA_STREAMING,
25666512Ssowmini 		    (p_nxge_dma_common_t)(&rx_dmap[i]));
25673859Sml29623 		if (status != NXGE_OK) {
25683859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
25696495Sspeer 			    "nxge_alloc_rx_buf_dma: Alloc Failed: "
25706495Sspeer 			    "dma %d size_index %d size requested %d",
25716495Sspeer 			    dma_channel,
25726495Sspeer 			    size_index,
25736495Sspeer 			    rx_dmap[i].alength));
25743859Sml29623 			size_index--;
25753859Sml29623 		} else {
25766495Sspeer 			rx_dmap[i].buf_alloc_state = BUF_ALLOCATED;
25776495Sspeer 			NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
25786495Sspeer 			    " nxge_alloc_rx_buf_dma DONE  alloc mem: "
25796495Sspeer 			    "dma %d dma_buf_p $%p kaddrp $%p alength %d "
25806495Sspeer 			    "buf_alloc_state %d alloc_type %d",
25816495Sspeer 			    dma_channel,
25826495Sspeer 			    &rx_dmap[i],
25836495Sspeer 			    rx_dmap[i].kaddrp,
25846495Sspeer 			    rx_dmap[i].alength,
25856495Sspeer 			    rx_dmap[i].buf_alloc_state,
25866495Sspeer 			    rx_dmap[i].buf_alloc_type));
25876495Sspeer 			NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
25886495Sspeer 			    " alloc_rx_buf_dma allocated rdc %d "
25896495Sspeer 			    "chunk %d size %x dvma %x bufp %llx kaddrp $%p",
25906495Sspeer 			    dma_channel, i, rx_dmap[i].alength,
25916495Sspeer 			    rx_dmap[i].ioaddr_pp, &rx_dmap[i],
25926495Sspeer 			    rx_dmap[i].kaddrp));
25933859Sml29623 			i++;
25943859Sml29623 			allocated += alloc_sizes[size_index];
25953859Sml29623 		}
25963859Sml29623 	}
25973859Sml29623 
25983859Sml29623 	if (allocated < total_alloc_size) {
25995770Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
26006495Sspeer 		    "==> nxge_alloc_rx_buf_dma: not enough for channel %d "
26015770Sml29623 		    "allocated 0x%x requested 0x%x",
26025770Sml29623 		    dma_channel,
26035770Sml29623 		    allocated, total_alloc_size));
26045770Sml29623 		status = NXGE_ERROR;
26053859Sml29623 		goto nxge_alloc_rx_mem_fail1;
26063859Sml29623 	}
26073859Sml29623 
26085770Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26096495Sspeer 	    "==> nxge_alloc_rx_buf_dma: Allocated for channel %d "
26105770Sml29623 	    "allocated 0x%x requested 0x%x",
26115770Sml29623 	    dma_channel,
26125770Sml29623 	    allocated, total_alloc_size));
26135770Sml29623 
26143859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
26156512Ssowmini 	    " alloc_rx_buf_dma rdc %d allocated %d chunks",
26166512Ssowmini 	    dma_channel, i));
26173859Sml29623 	*num_chunks = i;
26183859Sml29623 	*dmap = rx_dmap;
26193859Sml29623 
26203859Sml29623 	goto nxge_alloc_rx_mem_exit;
26213859Sml29623 
26223859Sml29623 nxge_alloc_rx_mem_fail1:
26233859Sml29623 	KMEM_FREE(rx_dmap, sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK);
26243859Sml29623 
26253859Sml29623 nxge_alloc_rx_mem_exit:
26263859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
26276512Ssowmini 	    "<== nxge_alloc_rx_buf_dma status 0x%08x", status));
26283859Sml29623 
26293859Sml29623 	return (status);
26303859Sml29623 }
26313859Sml29623 
26323859Sml29623 /*ARGSUSED*/
26333859Sml29623 static void
26343859Sml29623 nxge_free_rx_buf_dma(p_nxge_t nxgep, p_nxge_dma_common_t dmap,
26353859Sml29623     uint32_t num_chunks)
26363859Sml29623 {
26373859Sml29623 	int		i;
26383859Sml29623 
26393859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26406512Ssowmini 	    "==> nxge_free_rx_buf_dma: # of chunks %d", num_chunks));
26413859Sml29623 
26426495Sspeer 	if (dmap == 0)
26436495Sspeer 		return;
26446495Sspeer 
26453859Sml29623 	for (i = 0; i < num_chunks; i++) {
26463859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26476512Ssowmini 		    "==> nxge_free_rx_buf_dma: chunk %d dmap 0x%llx",
26486512Ssowmini 		    i, dmap));
26496495Sspeer 		nxge_dma_free_rx_data_buf(dmap++);
26503859Sml29623 	}
26513859Sml29623 
26523859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_free_rx_buf_dma"));
26533859Sml29623 }
26543859Sml29623 
26553859Sml29623 /*ARGSUSED*/
26563859Sml29623 static nxge_status_t
26573859Sml29623 nxge_alloc_rx_cntl_dma(p_nxge_t nxgep, uint16_t dma_channel,
26583859Sml29623     p_nxge_dma_common_t *dmap, size_t size)
26593859Sml29623 {
26603859Sml29623 	p_nxge_dma_common_t 	rx_dmap;
26613859Sml29623 	nxge_status_t		status = NXGE_OK;
26623859Sml29623 
26633859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rx_cntl_dma"));
26643859Sml29623 
26653859Sml29623 	rx_dmap = (p_nxge_dma_common_t)
26666512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_common_t), KM_SLEEP);
26673859Sml29623 
26683859Sml29623 	rx_dmap->contig_alloc_type = B_FALSE;
26696495Sspeer 	rx_dmap->kmem_alloc_type = B_FALSE;
26703859Sml29623 
26713859Sml29623 	status = nxge_dma_mem_alloc(nxgep, nxge_force_dma,
26726512Ssowmini 	    &nxge_desc_dma_attr,
26736512Ssowmini 	    size,
26746512Ssowmini 	    &nxge_dev_desc_dma_acc_attr,
26756512Ssowmini 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
26766512Ssowmini 	    rx_dmap);
26773859Sml29623 	if (status != NXGE_OK) {
26783859Sml29623 		goto nxge_alloc_rx_cntl_dma_fail1;
26793859Sml29623 	}
26803859Sml29623 
26813859Sml29623 	*dmap = rx_dmap;
26823859Sml29623 	goto nxge_alloc_rx_cntl_dma_exit;
26833859Sml29623 
26843859Sml29623 nxge_alloc_rx_cntl_dma_fail1:
26853859Sml29623 	KMEM_FREE(rx_dmap, sizeof (nxge_dma_common_t));
26863859Sml29623 
26873859Sml29623 nxge_alloc_rx_cntl_dma_exit:
26883859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
26896512Ssowmini 	    "<== nxge_alloc_rx_cntl_dma status 0x%08x", status));
26903859Sml29623 
26913859Sml29623 	return (status);
26923859Sml29623 }
26933859Sml29623 
26943859Sml29623 /*ARGSUSED*/
26953859Sml29623 static void
26963859Sml29623 nxge_free_rx_cntl_dma(p_nxge_t nxgep, p_nxge_dma_common_t dmap)
26973859Sml29623 {
26983859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_free_rx_cntl_dma"));
26993859Sml29623 
27006495Sspeer 	if (dmap == 0)
27016495Sspeer 		return;
27026495Sspeer 
27033859Sml29623 	nxge_dma_mem_free(dmap);
27043859Sml29623 
27053859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_free_rx_cntl_dma"));
27063859Sml29623 }
27073859Sml29623 
27086495Sspeer typedef struct {
27096495Sspeer 	size_t	tx_size;
27106495Sspeer 	size_t	cr_size;
27116495Sspeer 	size_t	threshhold;
27126495Sspeer } nxge_tdc_sizes_t;
27136495Sspeer 
27146495Sspeer static
27156495Sspeer nxge_status_t
27166495Sspeer nxge_tdc_sizes(
27176495Sspeer 	nxge_t *nxgep,
27186495Sspeer 	nxge_tdc_sizes_t *sizes)
27196495Sspeer {
27206495Sspeer 	uint32_t threshhold;	/* The bcopy() threshhold */
27216495Sspeer 	size_t tx_size;		/* Transmit buffer size */
27226495Sspeer 	size_t cr_size;		/* Completion ring size */
27236495Sspeer 
27246495Sspeer 	/*
27256495Sspeer 	 * Assume that each DMA channel will be configured with the
27266495Sspeer 	 * default transmit buffer size for copying transmit data.
27276495Sspeer 	 * (If a packet is bigger than this, it will not be copied.)
27286495Sspeer 	 */
27296495Sspeer 	if (nxgep->niu_type == N2_NIU) {
27306495Sspeer 		threshhold = TX_BCOPY_SIZE;
27316495Sspeer 	} else {
27326495Sspeer 		threshhold = nxge_bcopy_thresh;
27336495Sspeer 	}
27346495Sspeer 	tx_size = nxge_tx_ring_size * threshhold;
27356495Sspeer 
27366495Sspeer 	cr_size = nxge_tx_ring_size * sizeof (tx_desc_t);
27376495Sspeer 	cr_size += sizeof (txdma_mailbox_t);
27386495Sspeer 
27396495Sspeer #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
27406495Sspeer 	if (nxgep->niu_type == N2_NIU) {
27416495Sspeer 		if (!ISP2(tx_size)) {
27426495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
27436512Ssowmini 			    "==> nxge_tdc_sizes: Tx size"
27446512Ssowmini 			    " must be power of 2"));
27456495Sspeer 			return (NXGE_ERROR);
27466495Sspeer 		}
27476495Sspeer 
27486495Sspeer 		if (tx_size > (1 << 22)) {
27496495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
27506512Ssowmini 			    "==> nxge_tdc_sizes: Tx size"
27516512Ssowmini 			    " limited to 4M"));
27526495Sspeer 			return (NXGE_ERROR);
27536495Sspeer 		}
27546495Sspeer 
27556495Sspeer 		if (cr_size < 0x2000)
27566495Sspeer 			cr_size = 0x2000;
27576495Sspeer 	}
27586495Sspeer #endif
27596495Sspeer 
27606495Sspeer 	sizes->threshhold = threshhold;
27616495Sspeer 	sizes->tx_size = tx_size;
27626495Sspeer 	sizes->cr_size = cr_size;
27636495Sspeer 
27646495Sspeer 	return (NXGE_OK);
27656495Sspeer }
27666495Sspeer /*
27676495Sspeer  * nxge_alloc_txb
27686495Sspeer  *
27696495Sspeer  *	Allocate buffers for an TDC.
27706495Sspeer  *
27716495Sspeer  * Arguments:
27726495Sspeer  * 	nxgep
27736495Sspeer  * 	channel	The channel to map into our kernel space.
27746495Sspeer  *
27756495Sspeer  * Notes:
27766495Sspeer  *
27776495Sspeer  * NPI function calls:
27786495Sspeer  *
27796495Sspeer  * NXGE function calls:
27806495Sspeer  *
27816495Sspeer  * Registers accessed:
27826495Sspeer  *
27836495Sspeer  * Context:
27846495Sspeer  *
27856495Sspeer  * Taking apart:
27866495Sspeer  *
27876495Sspeer  * Open questions:
27886495Sspeer  *
27896495Sspeer  */
27906495Sspeer nxge_status_t
27916495Sspeer nxge_alloc_txb(
27926495Sspeer 	p_nxge_t nxgep,
27936495Sspeer 	int channel)
27946495Sspeer {
27956495Sspeer 	nxge_dma_common_t	**dma_buf_p;
27966495Sspeer 	nxge_dma_common_t	**dma_cntl_p;
27976495Sspeer 	uint32_t 		*num_chunks;
27986495Sspeer 	nxge_status_t		status = NXGE_OK;
27996495Sspeer 
28006495Sspeer 	nxge_tdc_sizes_t	sizes;
28016495Sspeer 
28026495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_tbb"));
28036495Sspeer 
28046495Sspeer 	if (nxge_tdc_sizes(nxgep, &sizes) != NXGE_OK)
28056495Sspeer 		return (NXGE_ERROR);
28066495Sspeer 
28076495Sspeer 	/*
28086495Sspeer 	 * Allocate memory for transmit buffers and descriptor rings.
28096495Sspeer 	 * Replace these allocation functions with the interface functions
28106495Sspeer 	 * provided by the partition manager Real Soon Now.
28116495Sspeer 	 */
28126495Sspeer 	dma_buf_p = &nxgep->tx_buf_pool_p->dma_buf_pool_p[channel];
28136495Sspeer 	num_chunks = &nxgep->tx_buf_pool_p->num_chunks[channel];
28146495Sspeer 
28156495Sspeer 	dma_cntl_p = &nxgep->tx_cntl_pool_p->dma_buf_pool_p[channel];
28166495Sspeer 
28176495Sspeer 	/*
28186495Sspeer 	 * Allocate memory for transmit buffers and descriptor rings.
28196495Sspeer 	 * Replace allocation functions with interface functions provided
28206495Sspeer 	 * by the partition manager when it is available.
28216495Sspeer 	 *
28226495Sspeer 	 * Allocate memory for the transmit buffer pool.
28236495Sspeer 	 */
28246495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
28256512Ssowmini 	    "sizes: tx: %ld, cr:%ld, th:%ld",
28266512Ssowmini 	    sizes.tx_size, sizes.cr_size, sizes.threshhold));
28276495Sspeer 
28286495Sspeer 	*num_chunks = 0;
28296495Sspeer 	status = nxge_alloc_tx_buf_dma(nxgep, channel, dma_buf_p,
28306495Sspeer 	    sizes.tx_size, sizes.threshhold, num_chunks);
28316495Sspeer 	if (status != NXGE_OK) {
28326495Sspeer 		cmn_err(CE_NOTE, "nxge_alloc_tx_buf_dma failed!");
28336495Sspeer 		return (status);
28346495Sspeer 	}
28356495Sspeer 
28366495Sspeer 	/*
28376495Sspeer 	 * Allocate memory for descriptor rings and mailbox.
28386495Sspeer 	 */
28396495Sspeer 	status = nxge_alloc_tx_cntl_dma(nxgep, channel, dma_cntl_p,
28406495Sspeer 	    sizes.cr_size);
28416495Sspeer 	if (status != NXGE_OK) {
28426495Sspeer 		nxge_free_tx_buf_dma(nxgep, *dma_buf_p, *num_chunks);
28436495Sspeer 		cmn_err(CE_NOTE, "nxge_alloc_tx_cntl_dma failed!");
28446495Sspeer 		return (status);
28456495Sspeer 	}
28466495Sspeer 
28476495Sspeer 	return (NXGE_OK);
28486495Sspeer }
28496495Sspeer 
28506495Sspeer void
28516495Sspeer nxge_free_txb(
28526495Sspeer 	p_nxge_t nxgep,
28536495Sspeer 	int channel)
28546495Sspeer {
28556495Sspeer 	nxge_dma_common_t	*data;
28566495Sspeer 	nxge_dma_common_t	*control;
28576495Sspeer 	uint32_t 		num_chunks;
28586495Sspeer 
28596495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_free_txb"));
28606495Sspeer 
28616495Sspeer 	data = nxgep->tx_buf_pool_p->dma_buf_pool_p[channel];
28626495Sspeer 	num_chunks = nxgep->tx_buf_pool_p->num_chunks[channel];
28636495Sspeer 	nxge_free_tx_buf_dma(nxgep, data, num_chunks);
28646495Sspeer 
28656495Sspeer 	nxgep->tx_buf_pool_p->dma_buf_pool_p[channel] = 0;
28666495Sspeer 	nxgep->tx_buf_pool_p->num_chunks[channel] = 0;
28676495Sspeer 
28686495Sspeer 	control = nxgep->tx_cntl_pool_p->dma_buf_pool_p[channel];
28696495Sspeer 	nxge_free_tx_cntl_dma(nxgep, control);
28706495Sspeer 
28716495Sspeer 	nxgep->tx_cntl_pool_p->dma_buf_pool_p[channel] = 0;
28726495Sspeer 
28736495Sspeer 	KMEM_FREE(data, sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK);
28746495Sspeer 	KMEM_FREE(control, sizeof (nxge_dma_common_t));
28756495Sspeer 
28766495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_free_txb"));
28776495Sspeer }
28786495Sspeer 
28796495Sspeer /*
28806495Sspeer  * nxge_alloc_tx_mem_pool
28816495Sspeer  *
28826495Sspeer  *	This function allocates all of the per-port TDC control data structures.
28836495Sspeer  *	The per-channel (TDC) data structures are allocated when needed.
28846495Sspeer  *
28856495Sspeer  * Arguments:
28866495Sspeer  * 	nxgep
28876495Sspeer  *
28886495Sspeer  * Notes:
28896495Sspeer  *
28906495Sspeer  * Context:
28916495Sspeer  *	Any domain
28926495Sspeer  */
28936495Sspeer nxge_status_t
28943859Sml29623 nxge_alloc_tx_mem_pool(p_nxge_t nxgep)
28953859Sml29623 {
28966495Sspeer 	nxge_hw_pt_cfg_t	*p_cfgp;
28976495Sspeer 	nxge_dma_pool_t		*dma_poolp;
28986495Sspeer 	nxge_dma_common_t	**dma_buf_p;
28996495Sspeer 	nxge_dma_pool_t		*dma_cntl_poolp;
29006495Sspeer 	nxge_dma_common_t	**dma_cntl_p;
29013859Sml29623 	uint32_t		*num_chunks; /* per dma */
29026495Sspeer 	int			tdc_max;
29033859Sml29623 
29043859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "==> nxge_alloc_tx_mem_pool"));
29053859Sml29623 
29066495Sspeer 	p_cfgp = &nxgep->pt_config.hw_config;
29076495Sspeer 	tdc_max = NXGE_MAX_TDCS;
29086495Sspeer 
29093859Sml29623 	/*
29103859Sml29623 	 * Allocate memory for each transmit DMA channel.
29113859Sml29623 	 */
29123859Sml29623 	dma_poolp = (p_nxge_dma_pool_t)KMEM_ZALLOC(sizeof (nxge_dma_pool_t),
29136512Ssowmini 	    KM_SLEEP);
29143859Sml29623 	dma_buf_p = (p_nxge_dma_common_t *)KMEM_ZALLOC(
29156512Ssowmini 	    sizeof (p_nxge_dma_common_t) * tdc_max, KM_SLEEP);
29163859Sml29623 
29173859Sml29623 	dma_cntl_poolp = (p_nxge_dma_pool_t)
29186512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_pool_t), KM_SLEEP);
29193859Sml29623 	dma_cntl_p = (p_nxge_dma_common_t *)KMEM_ZALLOC(
29206512Ssowmini 	    sizeof (p_nxge_dma_common_t) * tdc_max, KM_SLEEP);
29213859Sml29623 
29225770Sml29623 	if (nxge_tx_ring_size > TDC_DEFAULT_MAX) {
29235770Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM_CTL,
29245770Sml29623 		    "nxge_alloc_tx_mem_pool: TDC too high %d, "
29255770Sml29623 		    "set to default %d",
29265770Sml29623 		    nxge_tx_ring_size, TDC_DEFAULT_MAX));
29275770Sml29623 		nxge_tx_ring_size = TDC_DEFAULT_MAX;
29285770Sml29623 	}
29295770Sml29623 
29303859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
29313859Sml29623 	/*
29323859Sml29623 	 * N2/NIU has limitation on the descriptor sizes (contiguous
29333859Sml29623 	 * memory allocation on data buffers to 4M (contig_mem_alloc)
29343859Sml29623 	 * and little endian for control buffers (must use the ddi/dki mem alloc
29353859Sml29623 	 * function). The transmit ring is limited to 8K (includes the
29363859Sml29623 	 * mailbox).
29373859Sml29623 	 */
29383859Sml29623 	if (nxgep->niu_type == N2_NIU) {
29393859Sml29623 		if ((nxge_tx_ring_size > NXGE_NIU_CONTIG_TX_MAX) ||
29406512Ssowmini 		    (!ISP2(nxge_tx_ring_size))) {
29413859Sml29623 			nxge_tx_ring_size = NXGE_NIU_CONTIG_TX_MAX;
29423859Sml29623 		}
29433859Sml29623 	}
29443859Sml29623 #endif
29453859Sml29623 
29463859Sml29623 	nxgep->nxge_port_tx_ring_size = nxge_tx_ring_size;
29473859Sml29623 
29483859Sml29623 	num_chunks = (uint32_t *)KMEM_ZALLOC(
29496512Ssowmini 	    sizeof (uint32_t) * tdc_max, KM_SLEEP);
29506495Sspeer 
29516495Sspeer 	dma_poolp->ndmas = p_cfgp->tdc.owned;
29523859Sml29623 	dma_poolp->num_chunks = num_chunks;
29533859Sml29623 	dma_poolp->dma_buf_pool_p = dma_buf_p;
29543859Sml29623 	nxgep->tx_buf_pool_p = dma_poolp;
29553859Sml29623 
29566495Sspeer 	dma_poolp->buf_allocated = B_TRUE;
29576495Sspeer 
29586495Sspeer 	dma_cntl_poolp->ndmas = p_cfgp->tdc.owned;
29593859Sml29623 	dma_cntl_poolp->dma_buf_pool_p = dma_cntl_p;
29603859Sml29623 	nxgep->tx_cntl_pool_p = dma_cntl_poolp;
29613859Sml29623 
29626495Sspeer 	dma_cntl_poolp->buf_allocated = B_TRUE;
29636495Sspeer 
29646495Sspeer 	nxgep->tx_rings =
29656495Sspeer 	    KMEM_ZALLOC(sizeof (tx_rings_t), KM_SLEEP);
29666495Sspeer 	nxgep->tx_rings->rings =
29676495Sspeer 	    KMEM_ZALLOC(sizeof (p_tx_ring_t) * tdc_max, KM_SLEEP);
29686495Sspeer 	nxgep->tx_mbox_areas_p =
29696495Sspeer 	    KMEM_ZALLOC(sizeof (tx_mbox_areas_t), KM_SLEEP);
29706495Sspeer 	nxgep->tx_mbox_areas_p->txmbox_areas_p =
29716495Sspeer 	    KMEM_ZALLOC(sizeof (p_tx_mbox_t) * tdc_max, KM_SLEEP);
29726495Sspeer 
29736495Sspeer 	nxgep->tx_rings->ndmas = p_cfgp->tdc.owned;
29746495Sspeer 
29753859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL,
29766512Ssowmini 	    "==> nxge_alloc_tx_mem_pool: ndmas %d poolp->ndmas %d",
29776512Ssowmini 	    tdc_max, dma_poolp->ndmas));
29786495Sspeer 
29796495Sspeer 	return (NXGE_OK);
29803859Sml29623 }
29813859Sml29623 
29826495Sspeer nxge_status_t
29833859Sml29623 nxge_alloc_tx_buf_dma(p_nxge_t nxgep, uint16_t dma_channel,
29843859Sml29623     p_nxge_dma_common_t *dmap, size_t alloc_size,
29853859Sml29623     size_t block_size, uint32_t *num_chunks)
29863859Sml29623 {
29873859Sml29623 	p_nxge_dma_common_t 	tx_dmap;
29883859Sml29623 	nxge_status_t		status = NXGE_OK;
29893859Sml29623 	size_t			total_alloc_size;
29903859Sml29623 	size_t			allocated = 0;
29913859Sml29623 	int			i, size_index, array_size;
29923859Sml29623 
29933859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_tx_buf_dma"));
29943859Sml29623 
29953859Sml29623 	tx_dmap = (p_nxge_dma_common_t)
29966512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK,
29976512Ssowmini 	    KM_SLEEP);
29983859Sml29623 
29993859Sml29623 	total_alloc_size = alloc_size;
30003859Sml29623 	i = 0;
30013859Sml29623 	size_index = 0;
30023859Sml29623 	array_size =  sizeof (alloc_sizes) /  sizeof (size_t);
30033859Sml29623 	while ((alloc_sizes[size_index] < alloc_size) &&
30046512Ssowmini 	    (size_index < array_size))
30053859Sml29623 		size_index++;
30063859Sml29623 	if (size_index >= array_size) {
30073859Sml29623 		size_index = array_size - 1;
30083859Sml29623 	}
30093859Sml29623 
30103859Sml29623 	while ((allocated < total_alloc_size) &&
30116512Ssowmini 	    (size_index >= 0) && (i < NXGE_DMA_BLOCK)) {
30123859Sml29623 
30133859Sml29623 		tx_dmap[i].dma_chunk_index = i;
30143859Sml29623 		tx_dmap[i].block_size = block_size;
30153859Sml29623 		tx_dmap[i].alength = alloc_sizes[size_index];
30163859Sml29623 		tx_dmap[i].orig_alength = tx_dmap[i].alength;
30173859Sml29623 		tx_dmap[i].nblocks = alloc_sizes[size_index] / block_size;
30183859Sml29623 		tx_dmap[i].dma_channel = dma_channel;
30193859Sml29623 		tx_dmap[i].contig_alloc_type = B_FALSE;
30206495Sspeer 		tx_dmap[i].kmem_alloc_type = B_FALSE;
30213859Sml29623 
30223859Sml29623 		/*
30233859Sml29623 		 * N2/NIU: data buffers must be contiguous as the driver
30243859Sml29623 		 *	   needs to call Hypervisor api to set up
30253859Sml29623 		 *	   logical pages.
30263859Sml29623 		 */
30273859Sml29623 		if ((nxgep->niu_type == N2_NIU) && (NXGE_DMA_BLOCK == 1)) {
30283859Sml29623 			tx_dmap[i].contig_alloc_type = B_TRUE;
30293859Sml29623 		}
30303859Sml29623 
30313859Sml29623 		status = nxge_dma_mem_alloc(nxgep, nxge_force_dma,
30326512Ssowmini 		    &nxge_tx_dma_attr,
30336512Ssowmini 		    tx_dmap[i].alength,
30346512Ssowmini 		    &nxge_dev_buf_dma_acc_attr,
30356512Ssowmini 		    DDI_DMA_WRITE | DDI_DMA_STREAMING,
30366512Ssowmini 		    (p_nxge_dma_common_t)(&tx_dmap[i]));
30373859Sml29623 		if (status != NXGE_OK) {
30383859Sml29623 			size_index--;
30393859Sml29623 		} else {
30403859Sml29623 			i++;
30413859Sml29623 			allocated += alloc_sizes[size_index];
30423859Sml29623 		}
30433859Sml29623 	}
30443859Sml29623 
30453859Sml29623 	if (allocated < total_alloc_size) {
30465770Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
30475770Sml29623 		    "==> nxge_alloc_tx_buf_dma: not enough channel %d: "
30485770Sml29623 		    "allocated 0x%x requested 0x%x",
30495770Sml29623 		    dma_channel,
30505770Sml29623 		    allocated, total_alloc_size));
30515770Sml29623 		status = NXGE_ERROR;
30523859Sml29623 		goto nxge_alloc_tx_mem_fail1;
30533859Sml29623 	}
30543859Sml29623 
30555770Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
30565770Sml29623 	    "==> nxge_alloc_tx_buf_dma: Allocated for channel %d: "
30575770Sml29623 	    "allocated 0x%x requested 0x%x",
30585770Sml29623 	    dma_channel,
30595770Sml29623 	    allocated, total_alloc_size));
30605770Sml29623 
30613859Sml29623 	*num_chunks = i;
30623859Sml29623 	*dmap = tx_dmap;
30633859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
30646512Ssowmini 	    "==> nxge_alloc_tx_buf_dma dmap 0x%016llx num chunks %d",
30656512Ssowmini 	    *dmap, i));
30663859Sml29623 	goto nxge_alloc_tx_mem_exit;
30673859Sml29623 
30683859Sml29623 nxge_alloc_tx_mem_fail1:
30693859Sml29623 	KMEM_FREE(tx_dmap, sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK);
30703859Sml29623 
30713859Sml29623 nxge_alloc_tx_mem_exit:
30723859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
30736512Ssowmini 	    "<== nxge_alloc_tx_buf_dma status 0x%08x", status));
30743859Sml29623 
30753859Sml29623 	return (status);
30763859Sml29623 }
30773859Sml29623 
30783859Sml29623 /*ARGSUSED*/
30793859Sml29623 static void
30803859Sml29623 nxge_free_tx_buf_dma(p_nxge_t nxgep, p_nxge_dma_common_t dmap,
30813859Sml29623     uint32_t num_chunks)
30823859Sml29623 {
30833859Sml29623 	int		i;
30843859Sml29623 
30853859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "==> nxge_free_tx_buf_dma"));
30863859Sml29623 
30876495Sspeer 	if (dmap == 0)
30886495Sspeer 		return;
30896495Sspeer 
30903859Sml29623 	for (i = 0; i < num_chunks; i++) {
30913859Sml29623 		nxge_dma_mem_free(dmap++);
30923859Sml29623 	}
30933859Sml29623 
30943859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "<== nxge_free_tx_buf_dma"));
30953859Sml29623 }
30963859Sml29623 
30973859Sml29623 /*ARGSUSED*/
30986495Sspeer nxge_status_t
30993859Sml29623 nxge_alloc_tx_cntl_dma(p_nxge_t nxgep, uint16_t dma_channel,
31003859Sml29623     p_nxge_dma_common_t *dmap, size_t size)
31013859Sml29623 {
31023859Sml29623 	p_nxge_dma_common_t 	tx_dmap;
31033859Sml29623 	nxge_status_t		status = NXGE_OK;
31043859Sml29623 
31053859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_tx_cntl_dma"));
31063859Sml29623 	tx_dmap = (p_nxge_dma_common_t)
31076512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_common_t), KM_SLEEP);
31083859Sml29623 
31093859Sml29623 	tx_dmap->contig_alloc_type = B_FALSE;
31106495Sspeer 	tx_dmap->kmem_alloc_type = B_FALSE;
31113859Sml29623 
31123859Sml29623 	status = nxge_dma_mem_alloc(nxgep, nxge_force_dma,
31136512Ssowmini 	    &nxge_desc_dma_attr,
31146512Ssowmini 	    size,
31156512Ssowmini 	    &nxge_dev_desc_dma_acc_attr,
31166512Ssowmini 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
31176512Ssowmini 	    tx_dmap);
31183859Sml29623 	if (status != NXGE_OK) {
31193859Sml29623 		goto nxge_alloc_tx_cntl_dma_fail1;
31203859Sml29623 	}
31213859Sml29623 
31223859Sml29623 	*dmap = tx_dmap;
31233859Sml29623 	goto nxge_alloc_tx_cntl_dma_exit;
31243859Sml29623 
31253859Sml29623 nxge_alloc_tx_cntl_dma_fail1:
31263859Sml29623 	KMEM_FREE(tx_dmap, sizeof (nxge_dma_common_t));
31273859Sml29623 
31283859Sml29623 nxge_alloc_tx_cntl_dma_exit:
31293859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
31306512Ssowmini 	    "<== nxge_alloc_tx_cntl_dma status 0x%08x", status));
31313859Sml29623 
31323859Sml29623 	return (status);
31333859Sml29623 }
31343859Sml29623 
31353859Sml29623 /*ARGSUSED*/
31363859Sml29623 static void
31373859Sml29623 nxge_free_tx_cntl_dma(p_nxge_t nxgep, p_nxge_dma_common_t dmap)
31383859Sml29623 {
31393859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_free_tx_cntl_dma"));
31403859Sml29623 
31416495Sspeer 	if (dmap == 0)
31426495Sspeer 		return;
31436495Sspeer 
31443859Sml29623 	nxge_dma_mem_free(dmap);
31453859Sml29623 
31463859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_free_tx_cntl_dma"));
31473859Sml29623 }
31483859Sml29623 
31496495Sspeer /*
31506495Sspeer  * nxge_free_tx_mem_pool
31516495Sspeer  *
31526495Sspeer  *	This function frees all of the per-port TDC control data structures.
31536495Sspeer  *	The per-channel (TDC) data structures are freed when the channel
31546495Sspeer  *	is stopped.
31556495Sspeer  *
31566495Sspeer  * Arguments:
31576495Sspeer  * 	nxgep
31586495Sspeer  *
31596495Sspeer  * Notes:
31606495Sspeer  *
31616495Sspeer  * Context:
31626495Sspeer  *	Any domain
31636495Sspeer  */
31643859Sml29623 static void
31653859Sml29623 nxge_free_tx_mem_pool(p_nxge_t nxgep)
31663859Sml29623 {
31676495Sspeer 	int tdc_max = NXGE_MAX_TDCS;
31686495Sspeer 
31696495Sspeer 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_free_tx_mem_pool"));
31706495Sspeer 
31716495Sspeer 	if (!nxgep->tx_buf_pool_p || !nxgep->tx_buf_pool_p->buf_allocated) {
31726495Sspeer 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
31736512Ssowmini 		    "<== nxge_free_tx_mem_pool "
31746512Ssowmini 		    "(null tx buf pool or buf not allocated"));
31753859Sml29623 		return;
31763859Sml29623 	}
31776495Sspeer 	if (!nxgep->tx_cntl_pool_p || !nxgep->tx_cntl_pool_p->buf_allocated) {
31786495Sspeer 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
31796512Ssowmini 		    "<== nxge_free_tx_mem_pool "
31806512Ssowmini 		    "(null tx cntl buf pool or cntl buf not allocated"));
31813859Sml29623 		return;
31823859Sml29623 	}
31833859Sml29623 
31846495Sspeer 	/* 1. Free the mailboxes. */
31856495Sspeer 	KMEM_FREE(nxgep->tx_mbox_areas_p->txmbox_areas_p,
31866495Sspeer 	    sizeof (p_tx_mbox_t) * tdc_max);
31876495Sspeer 	KMEM_FREE(nxgep->tx_mbox_areas_p, sizeof (tx_mbox_areas_t));
31886495Sspeer 
31896495Sspeer 	nxgep->tx_mbox_areas_p = 0;
31906495Sspeer 
31916495Sspeer 	/* 2. Free the transmit ring arrays. */
31926495Sspeer 	KMEM_FREE(nxgep->tx_rings->rings,
31936495Sspeer 	    sizeof (p_tx_ring_t) * tdc_max);
31946495Sspeer 	KMEM_FREE(nxgep->tx_rings, sizeof (tx_rings_t));
31956495Sspeer 
31966495Sspeer 	nxgep->tx_rings = 0;
31976495Sspeer 
31986495Sspeer 	/* 3. Free the completion ring data structures. */
31996495Sspeer 	KMEM_FREE(nxgep->tx_cntl_pool_p->dma_buf_pool_p,
32006495Sspeer 	    sizeof (p_nxge_dma_common_t) * tdc_max);
32016495Sspeer 	KMEM_FREE(nxgep->tx_cntl_pool_p, sizeof (nxge_dma_pool_t));
32026495Sspeer 
32036495Sspeer 	nxgep->tx_cntl_pool_p = 0;
32046495Sspeer 
32056495Sspeer 	/* 4. Free the data ring data structures. */
32066495Sspeer 	KMEM_FREE(nxgep->tx_buf_pool_p->num_chunks,
32076495Sspeer 	    sizeof (uint32_t) * tdc_max);
32086495Sspeer 	KMEM_FREE(nxgep->tx_buf_pool_p->dma_buf_pool_p,
32096495Sspeer 	    sizeof (p_nxge_dma_common_t) * tdc_max);
32106495Sspeer 	KMEM_FREE(nxgep->tx_buf_pool_p, sizeof (nxge_dma_pool_t));
32116495Sspeer 
32126495Sspeer 	nxgep->tx_buf_pool_p = 0;
32136495Sspeer 
32146495Sspeer 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "<== nxge_free_tx_mem_pool"));
32153859Sml29623 }
32163859Sml29623 
32173859Sml29623 /*ARGSUSED*/
32183859Sml29623 static nxge_status_t
32193859Sml29623 nxge_dma_mem_alloc(p_nxge_t nxgep, dma_method_t method,
32203859Sml29623 	struct ddi_dma_attr *dma_attrp,
32213859Sml29623 	size_t length, ddi_device_acc_attr_t *acc_attr_p, uint_t xfer_flags,
32223859Sml29623 	p_nxge_dma_common_t dma_p)
32233859Sml29623 {
32243859Sml29623 	caddr_t 		kaddrp;
32253859Sml29623 	int			ddi_status = DDI_SUCCESS;
32263859Sml29623 	boolean_t		contig_alloc_type;
32276495Sspeer 	boolean_t		kmem_alloc_type;
32283859Sml29623 
32293859Sml29623 	contig_alloc_type = dma_p->contig_alloc_type;
32303859Sml29623 
32313859Sml29623 	if (contig_alloc_type && (nxgep->niu_type != N2_NIU)) {
32323859Sml29623 		/*
32333859Sml29623 		 * contig_alloc_type for contiguous memory only allowed
32343859Sml29623 		 * for N2/NIU.
32353859Sml29623 		 */
32363859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
32376512Ssowmini 		    "nxge_dma_mem_alloc: alloc type not allowed (%d)",
32386512Ssowmini 		    dma_p->contig_alloc_type));
32393859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
32403859Sml29623 	}
32413859Sml29623 
32423859Sml29623 	dma_p->dma_handle = NULL;
32433859Sml29623 	dma_p->acc_handle = NULL;
32443859Sml29623 	dma_p->kaddrp = dma_p->last_kaddrp = NULL;
32453859Sml29623 	dma_p->first_ioaddr_pp = dma_p->last_ioaddr_pp = NULL;
32463859Sml29623 	ddi_status = ddi_dma_alloc_handle(nxgep->dip, dma_attrp,
32476512Ssowmini 	    DDI_DMA_DONTWAIT, NULL, &dma_p->dma_handle);
32483859Sml29623 	if (ddi_status != DDI_SUCCESS) {
32493859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
32506512Ssowmini 		    "nxge_dma_mem_alloc:ddi_dma_alloc_handle failed."));
32513859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
32523859Sml29623 	}
32533859Sml29623 
32546495Sspeer 	kmem_alloc_type = dma_p->kmem_alloc_type;
32556495Sspeer 
32563859Sml29623 	switch (contig_alloc_type) {
32573859Sml29623 	case B_FALSE:
32586495Sspeer 		switch (kmem_alloc_type) {
32596495Sspeer 		case B_FALSE:
32606495Sspeer 			ddi_status = ddi_dma_mem_alloc(dma_p->dma_handle,
32616512Ssowmini 			    length,
32626512Ssowmini 			    acc_attr_p,
32636512Ssowmini 			    xfer_flags,
32646512Ssowmini 			    DDI_DMA_DONTWAIT, 0, &kaddrp, &dma_p->alength,
32656512Ssowmini 			    &dma_p->acc_handle);
32666495Sspeer 			if (ddi_status != DDI_SUCCESS) {
32676495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
32686495Sspeer 				    "nxge_dma_mem_alloc: "
32696495Sspeer 				    "ddi_dma_mem_alloc failed"));
32706495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
32716495Sspeer 				dma_p->dma_handle = NULL;
32726495Sspeer 				return (NXGE_ERROR | NXGE_DDI_FAILED);
32736495Sspeer 			}
32746495Sspeer 			if (dma_p->alength < length) {
32756495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
32766495Sspeer 				    "nxge_dma_mem_alloc:di_dma_mem_alloc "
32776495Sspeer 				    "< length."));
32786495Sspeer 				ddi_dma_mem_free(&dma_p->acc_handle);
32796495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
32806495Sspeer 				dma_p->acc_handle = NULL;
32816495Sspeer 				dma_p->dma_handle = NULL;
32826495Sspeer 				return (NXGE_ERROR);
32836495Sspeer 			}
32846495Sspeer 
32856495Sspeer 			ddi_status = ddi_dma_addr_bind_handle(dma_p->dma_handle,
32866495Sspeer 			    NULL,
32876495Sspeer 			    kaddrp, dma_p->alength, xfer_flags,
32886495Sspeer 			    DDI_DMA_DONTWAIT,
32896495Sspeer 			    0, &dma_p->dma_cookie, &dma_p->ncookies);
32906495Sspeer 			if (ddi_status != DDI_DMA_MAPPED) {
32916495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
32926495Sspeer 				    "nxge_dma_mem_alloc: ddi_dma_addr_bind "
32936495Sspeer 				    "failed "
32946495Sspeer 				    "(staus 0x%x ncookies %d.)", ddi_status,
32956495Sspeer 				    dma_p->ncookies));
32966495Sspeer 				if (dma_p->acc_handle) {
32976495Sspeer 					ddi_dma_mem_free(&dma_p->acc_handle);
32986495Sspeer 					dma_p->acc_handle = NULL;
32996495Sspeer 				}
33006495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
33016495Sspeer 				dma_p->dma_handle = NULL;
33026495Sspeer 				return (NXGE_ERROR | NXGE_DDI_FAILED);
33036495Sspeer 			}
33046495Sspeer 
33056495Sspeer 			if (dma_p->ncookies != 1) {
33066495Sspeer 				NXGE_DEBUG_MSG((nxgep, DMA_CTL,
33076495Sspeer 				    "nxge_dma_mem_alloc:ddi_dma_addr_bind "
33086495Sspeer 				    "> 1 cookie"
33096495Sspeer 				    "(staus 0x%x ncookies %d.)", ddi_status,
33106495Sspeer 				    dma_p->ncookies));
33116495Sspeer 				if (dma_p->acc_handle) {
33126495Sspeer 					ddi_dma_mem_free(&dma_p->acc_handle);
33136495Sspeer 					dma_p->acc_handle = NULL;
33146495Sspeer 				}
33156495Sspeer 				(void) ddi_dma_unbind_handle(dma_p->dma_handle);
33166495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
33176495Sspeer 				dma_p->dma_handle = NULL;
33186495Sspeer 				return (NXGE_ERROR);
33196495Sspeer 			}
33206495Sspeer 			break;
33216495Sspeer 
33226495Sspeer 		case B_TRUE:
33236495Sspeer 			kaddrp = KMEM_ALLOC(length, KM_NOSLEEP);
33246495Sspeer 			if (kaddrp == NULL) {
33256495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33266495Sspeer 				    "nxge_dma_mem_alloc:ddi_dma_mem_alloc "
33276495Sspeer 				    "kmem alloc failed"));
33286495Sspeer 				return (NXGE_ERROR);
33296495Sspeer 			}
33306495Sspeer 
33316495Sspeer 			dma_p->alength = length;
33326495Sspeer 			ddi_status = ddi_dma_addr_bind_handle(dma_p->dma_handle,
33336495Sspeer 			    NULL, kaddrp, dma_p->alength, xfer_flags,
33346495Sspeer 			    DDI_DMA_DONTWAIT, 0,
33356495Sspeer 			    &dma_p->dma_cookie, &dma_p->ncookies);
33366495Sspeer 			if (ddi_status != DDI_DMA_MAPPED) {
33376495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33386495Sspeer 				    "nxge_dma_mem_alloc:ddi_dma_addr_bind: "
33396495Sspeer 				    "(kmem_alloc) failed kaddrp $%p length %d "
33406495Sspeer 				    "(staus 0x%x (%d) ncookies %d.)",
33416495Sspeer 				    kaddrp, length,
33426495Sspeer 				    ddi_status, ddi_status, dma_p->ncookies));
33436495Sspeer 				KMEM_FREE(kaddrp, length);
33446495Sspeer 				dma_p->acc_handle = NULL;
33456495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
33466495Sspeer 				dma_p->dma_handle = NULL;
33476495Sspeer 				dma_p->kaddrp = NULL;
33486495Sspeer 				return (NXGE_ERROR | NXGE_DDI_FAILED);
33496495Sspeer 			}
33506495Sspeer 
33516495Sspeer 			if (dma_p->ncookies != 1) {
33526495Sspeer 				NXGE_DEBUG_MSG((nxgep, DMA_CTL,
33536495Sspeer 				    "nxge_dma_mem_alloc:ddi_dma_addr_bind "
33546495Sspeer 				    "(kmem_alloc) > 1 cookie"
33556495Sspeer 				    "(staus 0x%x ncookies %d.)", ddi_status,
33566512Ssowmini 				    dma_p->ncookies));
33576495Sspeer 				KMEM_FREE(kaddrp, length);
33583859Sml29623 				dma_p->acc_handle = NULL;
33596495Sspeer 				(void) ddi_dma_unbind_handle(dma_p->dma_handle);
33606495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
33616495Sspeer 				dma_p->dma_handle = NULL;
33626495Sspeer 				dma_p->kaddrp = NULL;
33636495Sspeer 				return (NXGE_ERROR);
33643859Sml29623 			}
33656495Sspeer 
33666495Sspeer 			dma_p->kaddrp = kaddrp;
33676495Sspeer 
33686495Sspeer 			NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
33696512Ssowmini 			    "nxge_dma_mem_alloc: kmem_alloc dmap $%p "
33706512Ssowmini 			    "kaddr $%p alength %d",
33716512Ssowmini 			    dma_p,
33726512Ssowmini 			    kaddrp,
33736512Ssowmini 			    dma_p->alength));
33746495Sspeer 			break;
33753859Sml29623 		}
33763859Sml29623 		break;
33773859Sml29623 
33783859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
33793859Sml29623 	case B_TRUE:
33803859Sml29623 		kaddrp = (caddr_t)contig_mem_alloc(length);
33813859Sml29623 		if (kaddrp == NULL) {
33823859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33836512Ssowmini 			    "nxge_dma_mem_alloc:contig_mem_alloc failed."));
33843859Sml29623 			ddi_dma_free_handle(&dma_p->dma_handle);
33853859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
33863859Sml29623 		}
33873859Sml29623 
33883859Sml29623 		dma_p->alength = length;
33893859Sml29623 		ddi_status = ddi_dma_addr_bind_handle(dma_p->dma_handle, NULL,
33906512Ssowmini 		    kaddrp, dma_p->alength, xfer_flags, DDI_DMA_DONTWAIT, 0,
33916512Ssowmini 		    &dma_p->dma_cookie, &dma_p->ncookies);
33923859Sml29623 		if (ddi_status != DDI_DMA_MAPPED) {
33933859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33946512Ssowmini 			    "nxge_dma_mem_alloc:di_dma_addr_bind failed "
33956512Ssowmini 			    "(status 0x%x ncookies %d.)", ddi_status,
33966512Ssowmini 			    dma_p->ncookies));
33973859Sml29623 
33983859Sml29623 			NXGE_DEBUG_MSG((nxgep, DMA_CTL,
33996512Ssowmini 			    "==> nxge_dma_mem_alloc: (not mapped)"
34006512Ssowmini 			    "length %lu (0x%x) "
34016512Ssowmini 			    "free contig kaddrp $%p "
34026512Ssowmini 			    "va_to_pa $%p",
34036512Ssowmini 			    length, length,
34046512Ssowmini 			    kaddrp,
34056512Ssowmini 			    va_to_pa(kaddrp)));
34063859Sml29623 
34073859Sml29623 
34083859Sml29623 			contig_mem_free((void *)kaddrp, length);
34093859Sml29623 			ddi_dma_free_handle(&dma_p->dma_handle);
34103859Sml29623 
34113859Sml29623 			dma_p->dma_handle = NULL;
34123859Sml29623 			dma_p->acc_handle = NULL;
34133859Sml29623 			dma_p->alength = NULL;
34143859Sml29623 			dma_p->kaddrp = NULL;
34153859Sml29623 
34163859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
34173859Sml29623 		}
34183859Sml29623 
34193859Sml29623 		if (dma_p->ncookies != 1 ||
34206512Ssowmini 		    (dma_p->dma_cookie.dmac_laddress == NULL)) {
34213859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
34226512Ssowmini 			    "nxge_dma_mem_alloc:di_dma_addr_bind > 1 "
34236512Ssowmini 			    "cookie or "
34246512Ssowmini 			    "dmac_laddress is NULL $%p size %d "
34256512Ssowmini 			    " (status 0x%x ncookies %d.)",
34266512Ssowmini 			    ddi_status,
34276512Ssowmini 			    dma_p->dma_cookie.dmac_laddress,
34286512Ssowmini 			    dma_p->dma_cookie.dmac_size,
34296512Ssowmini 			    dma_p->ncookies));
34303859Sml29623 
34313859Sml29623 			contig_mem_free((void *)kaddrp, length);
34324185Sspeer 			(void) ddi_dma_unbind_handle(dma_p->dma_handle);
34333859Sml29623 			ddi_dma_free_handle(&dma_p->dma_handle);
34343859Sml29623 
34353859Sml29623 			dma_p->alength = 0;
34363859Sml29623 			dma_p->dma_handle = NULL;
34373859Sml29623 			dma_p->acc_handle = NULL;
34383859Sml29623 			dma_p->kaddrp = NULL;
34393859Sml29623 
34403859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
34413859Sml29623 		}
34423859Sml29623 		break;
34433859Sml29623 
34443859Sml29623 #else
34453859Sml29623 	case B_TRUE:
34463859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
34476512Ssowmini 		    "nxge_dma_mem_alloc: invalid alloc type for !sun4v"));
34483859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
34493859Sml29623 #endif
34503859Sml29623 	}
34513859Sml29623 
34523859Sml29623 	dma_p->kaddrp = kaddrp;
34533859Sml29623 	dma_p->last_kaddrp = (unsigned char *)kaddrp +
34546512Ssowmini 	    dma_p->alength - RXBUF_64B_ALIGNED;
34555125Sjoycey #if defined(__i386)
34565125Sjoycey 	dma_p->ioaddr_pp =
34576512Ssowmini 	    (unsigned char *)(uint32_t)dma_p->dma_cookie.dmac_laddress;
34585125Sjoycey #else
34593859Sml29623 	dma_p->ioaddr_pp = (unsigned char *)dma_p->dma_cookie.dmac_laddress;
34605125Sjoycey #endif
34613859Sml29623 	dma_p->last_ioaddr_pp =
34625125Sjoycey #if defined(__i386)
34636512Ssowmini 	    (unsigned char *)(uint32_t)dma_p->dma_cookie.dmac_laddress +
34645125Sjoycey #else
34656512Ssowmini 	    (unsigned char *)dma_p->dma_cookie.dmac_laddress +
34665125Sjoycey #endif
34676512Ssowmini 	    dma_p->alength - RXBUF_64B_ALIGNED;
34683859Sml29623 
34693859Sml29623 	NPI_DMA_ACC_HANDLE_SET(dma_p, dma_p->acc_handle);
34703859Sml29623 
34713859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
34723859Sml29623 	dma_p->orig_ioaddr_pp =
34736512Ssowmini 	    (unsigned char *)dma_p->dma_cookie.dmac_laddress;
34743859Sml29623 	dma_p->orig_alength = length;
34753859Sml29623 	dma_p->orig_kaddrp = kaddrp;
34763859Sml29623 	dma_p->orig_vatopa = (uint64_t)va_to_pa(kaddrp);
34773859Sml29623 #endif
34783859Sml29623 
34793859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_dma_mem_alloc: "
34806512Ssowmini 	    "dma buffer allocated: dma_p $%p "
34816512Ssowmini 	    "return dmac_ladress from cookie $%p cookie dmac_size %d "
34826512Ssowmini 	    "dma_p->ioaddr_p $%p "
34836512Ssowmini 	    "dma_p->orig_ioaddr_p $%p "
34846512Ssowmini 	    "orig_vatopa $%p "
34856512Ssowmini 	    "alength %d (0x%x) "
34866512Ssowmini 	    "kaddrp $%p "
34876512Ssowmini 	    "length %d (0x%x)",
34886512Ssowmini 	    dma_p,
34896512Ssowmini 	    dma_p->dma_cookie.dmac_laddress, dma_p->dma_cookie.dmac_size,
34906512Ssowmini 	    dma_p->ioaddr_pp,
34916512Ssowmini 	    dma_p->orig_ioaddr_pp,
34926512Ssowmini 	    dma_p->orig_vatopa,
34936512Ssowmini 	    dma_p->alength, dma_p->alength,
34946512Ssowmini 	    kaddrp,
34956512Ssowmini 	    length, length));
34963859Sml29623 
34973859Sml29623 	return (NXGE_OK);
34983859Sml29623 }
34993859Sml29623 
35003859Sml29623 static void
35013859Sml29623 nxge_dma_mem_free(p_nxge_dma_common_t dma_p)
35023859Sml29623 {
35033859Sml29623 	if (dma_p->dma_handle != NULL) {
35043859Sml29623 		if (dma_p->ncookies) {
35053859Sml29623 			(void) ddi_dma_unbind_handle(dma_p->dma_handle);
35063859Sml29623 			dma_p->ncookies = 0;
35073859Sml29623 		}
35083859Sml29623 		ddi_dma_free_handle(&dma_p->dma_handle);
35093859Sml29623 		dma_p->dma_handle = NULL;
35103859Sml29623 	}
35113859Sml29623 
35123859Sml29623 	if (dma_p->acc_handle != NULL) {
35133859Sml29623 		ddi_dma_mem_free(&dma_p->acc_handle);
35143859Sml29623 		dma_p->acc_handle = NULL;
35153859Sml29623 		NPI_DMA_ACC_HANDLE_SET(dma_p, NULL);
35163859Sml29623 	}
35173859Sml29623 
35183859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
35193859Sml29623 	if (dma_p->contig_alloc_type &&
35206512Ssowmini 	    dma_p->orig_kaddrp && dma_p->orig_alength) {
35213859Sml29623 		NXGE_DEBUG_MSG((NULL, DMA_CTL, "nxge_dma_mem_free: "
35226512Ssowmini 		    "kaddrp $%p (orig_kaddrp $%p)"
35236512Ssowmini 		    "mem type %d ",
35246512Ssowmini 		    "orig_alength %d "
35256512Ssowmini 		    "alength 0x%x (%d)",
35266512Ssowmini 		    dma_p->kaddrp,
35276512Ssowmini 		    dma_p->orig_kaddrp,
35286512Ssowmini 		    dma_p->contig_alloc_type,
35296512Ssowmini 		    dma_p->orig_alength,
35306512Ssowmini 		    dma_p->alength, dma_p->alength));
35313859Sml29623 
35323859Sml29623 		contig_mem_free(dma_p->orig_kaddrp, dma_p->orig_alength);
35333859Sml29623 		dma_p->orig_alength = NULL;
35343859Sml29623 		dma_p->orig_kaddrp = NULL;
35353859Sml29623 		dma_p->contig_alloc_type = B_FALSE;
35363859Sml29623 	}
35373859Sml29623 #endif
35383859Sml29623 	dma_p->kaddrp = NULL;
35393859Sml29623 	dma_p->alength = NULL;
35403859Sml29623 }
35413859Sml29623 
35426495Sspeer static void
35436495Sspeer nxge_dma_free_rx_data_buf(p_nxge_dma_common_t dma_p)
35446495Sspeer {
35456495Sspeer 	uint64_t kaddr;
35466495Sspeer 	uint32_t buf_size;
35476495Sspeer 
35486495Sspeer 	NXGE_DEBUG_MSG((NULL, DMA_CTL, "==> nxge_dma_free_rx_data_buf"));
35496495Sspeer 
35506495Sspeer 	if (dma_p->dma_handle != NULL) {
35516495Sspeer 		if (dma_p->ncookies) {
35526495Sspeer 			(void) ddi_dma_unbind_handle(dma_p->dma_handle);
35536495Sspeer 			dma_p->ncookies = 0;
35546495Sspeer 		}
35556495Sspeer 		ddi_dma_free_handle(&dma_p->dma_handle);
35566495Sspeer 		dma_p->dma_handle = NULL;
35576495Sspeer 	}
35586495Sspeer 
35596495Sspeer 	if (dma_p->acc_handle != NULL) {
35606495Sspeer 		ddi_dma_mem_free(&dma_p->acc_handle);
35616495Sspeer 		dma_p->acc_handle = NULL;
35626495Sspeer 		NPI_DMA_ACC_HANDLE_SET(dma_p, NULL);
35636495Sspeer 	}
35646495Sspeer 
35656495Sspeer 	NXGE_DEBUG_MSG((NULL, DMA_CTL,
35666495Sspeer 	    "==> nxge_dma_free_rx_data_buf: dmap $%p buf_alloc_state %d",
35676495Sspeer 	    dma_p,
35686495Sspeer 	    dma_p->buf_alloc_state));
35696495Sspeer 
35706495Sspeer 	if (!(dma_p->buf_alloc_state & BUF_ALLOCATED_WAIT_FREE)) {
35716495Sspeer 		NXGE_DEBUG_MSG((NULL, DMA_CTL,
35726495Sspeer 		    "<== nxge_dma_free_rx_data_buf: "
35736495Sspeer 		    "outstanding data buffers"));
35746495Sspeer 		return;
35756495Sspeer 	}
35766495Sspeer 
35776495Sspeer #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
35786495Sspeer 	if (dma_p->contig_alloc_type &&
35796512Ssowmini 	    dma_p->orig_kaddrp && dma_p->orig_alength) {
35806495Sspeer 		NXGE_DEBUG_MSG((NULL, DMA_CTL, "nxge_dma_free_rx_data_buf: "
35816495Sspeer 		    "kaddrp $%p (orig_kaddrp $%p)"
35826495Sspeer 		    "mem type %d ",
35836495Sspeer 		    "orig_alength %d "
35846495Sspeer 		    "alength 0x%x (%d)",
35856495Sspeer 		    dma_p->kaddrp,
35866495Sspeer 		    dma_p->orig_kaddrp,
35876495Sspeer 		    dma_p->contig_alloc_type,
35886495Sspeer 		    dma_p->orig_alength,
35896495Sspeer 		    dma_p->alength, dma_p->alength));
35906495Sspeer 
35916495Sspeer 		kaddr = (uint64_t)dma_p->orig_kaddrp;
35926495Sspeer 		buf_size = dma_p->orig_alength;
35936495Sspeer 		nxge_free_buf(CONTIG_MEM_ALLOC, kaddr, buf_size);
35946495Sspeer 		dma_p->orig_alength = NULL;
35956495Sspeer 		dma_p->orig_kaddrp = NULL;
35966495Sspeer 		dma_p->contig_alloc_type = B_FALSE;
35976495Sspeer 		dma_p->kaddrp = NULL;
35986495Sspeer 		dma_p->alength = NULL;
35996495Sspeer 		return;
36006495Sspeer 	}
36016495Sspeer #endif
36026495Sspeer 
36036495Sspeer 	if (dma_p->kmem_alloc_type) {
36046495Sspeer 		NXGE_DEBUG_MSG((NULL, DMA_CTL,
36056495Sspeer 		    "nxge_dma_free_rx_data_buf: free kmem "
36066512Ssowmini 		    "kaddrp $%p (orig_kaddrp $%p)"
36076512Ssowmini 		    "alloc type %d "
36086512Ssowmini 		    "orig_alength %d "
36096512Ssowmini 		    "alength 0x%x (%d)",
36106512Ssowmini 		    dma_p->kaddrp,
36116512Ssowmini 		    dma_p->orig_kaddrp,
36126512Ssowmini 		    dma_p->kmem_alloc_type,
36136512Ssowmini 		    dma_p->orig_alength,
36146512Ssowmini 		    dma_p->alength, dma_p->alength));
36156495Sspeer #if defined(__i386)
36166495Sspeer 		kaddr = (uint64_t)(uint32_t)dma_p->kaddrp;
36176495Sspeer #else
36186495Sspeer 		kaddr = (uint64_t)dma_p->kaddrp;
36196495Sspeer #endif
36206495Sspeer 		buf_size = dma_p->orig_alength;
36216495Sspeer 		NXGE_DEBUG_MSG((NULL, DMA_CTL,
36226495Sspeer 		    "nxge_dma_free_rx_data_buf: free dmap $%p "
36236495Sspeer 		    "kaddr $%p buf_size %d",
36246495Sspeer 		    dma_p,
36256495Sspeer 		    kaddr, buf_size));
36266495Sspeer 		nxge_free_buf(KMEM_ALLOC, kaddr, buf_size);
36276495Sspeer 		dma_p->alength = 0;
36286495Sspeer 		dma_p->orig_alength = 0;
36296495Sspeer 		dma_p->kaddrp = NULL;
36306495Sspeer 		dma_p->kmem_alloc_type = B_FALSE;
36316495Sspeer 	}
36326495Sspeer 
36336495Sspeer 	NXGE_DEBUG_MSG((NULL, DMA_CTL, "<== nxge_dma_free_rx_data_buf"));
36346495Sspeer }
36356495Sspeer 
36363859Sml29623 /*
36373859Sml29623  *	nxge_m_start() -- start transmitting and receiving.
36383859Sml29623  *
36393859Sml29623  *	This function is called by the MAC layer when the first
36403859Sml29623  *	stream is open to prepare the hardware ready for sending
36413859Sml29623  *	and transmitting packets.
36423859Sml29623  */
36433859Sml29623 static int
36443859Sml29623 nxge_m_start(void *arg)
36453859Sml29623 {
36463859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
36473859Sml29623 
36483859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_start"));
36493859Sml29623 
36506705Sml29623 	if (nxge_peu_reset_enable && !nxgep->nxge_link_poll_timerid) {
36516705Sml29623 		(void) nxge_link_monitor(nxgep, LINK_MONITOR_START);
36526705Sml29623 	}
36536705Sml29623 
36543859Sml29623 	MUTEX_ENTER(nxgep->genlock);
36553859Sml29623 	if (nxge_init(nxgep) != NXGE_OK) {
36563859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
36576512Ssowmini 		    "<== nxge_m_start: initialization failed"));
36583859Sml29623 		MUTEX_EXIT(nxgep->genlock);
36593859Sml29623 		return (EIO);
36603859Sml29623 	}
36613859Sml29623 
36623859Sml29623 	if (nxgep->nxge_mac_state == NXGE_MAC_STARTED)
36633859Sml29623 		goto nxge_m_start_exit;
36643859Sml29623 	/*
36653859Sml29623 	 * Start timer to check the system error and tx hangs
36663859Sml29623 	 */
36676495Sspeer 	if (!isLDOMguest(nxgep))
36686495Sspeer 		nxgep->nxge_timerid = nxge_start_timer(nxgep,
36696495Sspeer 		    nxge_check_hw_state, NXGE_CHECK_TIMER);
36706495Sspeer #if	defined(sun4v)
36716495Sspeer 	else
36726495Sspeer 		nxge_hio_start_timer(nxgep);
36736495Sspeer #endif
36743859Sml29623 
36753859Sml29623 	nxgep->link_notify = B_TRUE;
36763859Sml29623 
36773859Sml29623 	nxgep->nxge_mac_state = NXGE_MAC_STARTED;
36783859Sml29623 
36793859Sml29623 nxge_m_start_exit:
36803859Sml29623 	MUTEX_EXIT(nxgep->genlock);
36813859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_start"));
36823859Sml29623 	return (0);
36833859Sml29623 }
36843859Sml29623 
36853859Sml29623 /*
36863859Sml29623  *	nxge_m_stop(): stop transmitting and receiving.
36873859Sml29623  */
36883859Sml29623 static void
36893859Sml29623 nxge_m_stop(void *arg)
36903859Sml29623 {
36913859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
36923859Sml29623 
36933859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_stop"));
36943859Sml29623 
36953859Sml29623 	if (nxgep->nxge_timerid) {
36963859Sml29623 		nxge_stop_timer(nxgep, nxgep->nxge_timerid);
36973859Sml29623 		nxgep->nxge_timerid = 0;
36983859Sml29623 	}
36993859Sml29623 
37003859Sml29623 	MUTEX_ENTER(nxgep->genlock);
37016495Sspeer 	nxgep->nxge_mac_state = NXGE_MAC_STOPPING;
37023859Sml29623 	nxge_uninit(nxgep);
37033859Sml29623 
37043859Sml29623 	nxgep->nxge_mac_state = NXGE_MAC_STOPPED;
37053859Sml29623 
37063859Sml29623 	MUTEX_EXIT(nxgep->genlock);
37073859Sml29623 
37083859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_stop"));
37093859Sml29623 }
37103859Sml29623 
37113859Sml29623 static int
37123859Sml29623 nxge_m_unicst(void *arg, const uint8_t *macaddr)
37133859Sml29623 {
37143859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
37153859Sml29623 	struct 		ether_addr addrp;
37163859Sml29623 
37173859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL, "==> nxge_m_unicst"));
37183859Sml29623 
37193859Sml29623 	bcopy(macaddr, (uint8_t *)&addrp, ETHERADDRL);
37203859Sml29623 	if (nxge_set_mac_addr(nxgep, &addrp)) {
37213859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
37226512Ssowmini 		    "<== nxge_m_unicst: set unitcast failed"));
37233859Sml29623 		return (EINVAL);
37243859Sml29623 	}
37253859Sml29623 
37263859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL, "<== nxge_m_unicst"));
37273859Sml29623 
37283859Sml29623 	return (0);
37293859Sml29623 }
37303859Sml29623 
37313859Sml29623 static int
37323859Sml29623 nxge_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
37333859Sml29623 {
37343859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
37353859Sml29623 	struct 		ether_addr addrp;
37363859Sml29623 
37373859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL,
37386512Ssowmini 	    "==> nxge_m_multicst: add %d", add));
37393859Sml29623 
37403859Sml29623 	bcopy(mca, (uint8_t *)&addrp, ETHERADDRL);
37413859Sml29623 	if (add) {
37423859Sml29623 		if (nxge_add_mcast_addr(nxgep, &addrp)) {
37433859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
37446512Ssowmini 			    "<== nxge_m_multicst: add multicast failed"));
37453859Sml29623 			return (EINVAL);
37463859Sml29623 		}
37473859Sml29623 	} else {
37483859Sml29623 		if (nxge_del_mcast_addr(nxgep, &addrp)) {
37493859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
37506512Ssowmini 			    "<== nxge_m_multicst: del multicast failed"));
37513859Sml29623 			return (EINVAL);
37523859Sml29623 		}
37533859Sml29623 	}
37543859Sml29623 
37553859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL, "<== nxge_m_multicst"));
37563859Sml29623 
37573859Sml29623 	return (0);
37583859Sml29623 }
37593859Sml29623 
37603859Sml29623 static int
37613859Sml29623 nxge_m_promisc(void *arg, boolean_t on)
37623859Sml29623 {
37633859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
37643859Sml29623 
37653859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL,
37666512Ssowmini 	    "==> nxge_m_promisc: on %d", on));
37673859Sml29623 
37683859Sml29623 	if (nxge_set_promisc(nxgep, on)) {
37693859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
37706512Ssowmini 		    "<== nxge_m_promisc: set promisc failed"));
37713859Sml29623 		return (EINVAL);
37723859Sml29623 	}
37733859Sml29623 
37743859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL,
37756512Ssowmini 	    "<== nxge_m_promisc: on %d", on));
37763859Sml29623 
37773859Sml29623 	return (0);
37783859Sml29623 }
37793859Sml29623 
37803859Sml29623 static void
37813859Sml29623 nxge_m_ioctl(void *arg,  queue_t *wq, mblk_t *mp)
37823859Sml29623 {
37833859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
37844185Sspeer 	struct 		iocblk *iocp;
37853859Sml29623 	boolean_t 	need_privilege;
37863859Sml29623 	int 		err;
37873859Sml29623 	int 		cmd;
37883859Sml29623 
37893859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_ioctl"));
37903859Sml29623 
37913859Sml29623 	iocp = (struct iocblk *)mp->b_rptr;
37923859Sml29623 	iocp->ioc_error = 0;
37933859Sml29623 	need_privilege = B_TRUE;
37943859Sml29623 	cmd = iocp->ioc_cmd;
37953859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_ioctl: cmd 0x%08x", cmd));
37963859Sml29623 	switch (cmd) {
37973859Sml29623 	default:
37983859Sml29623 		miocnak(wq, mp, 0, EINVAL);
37993859Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_ioctl: invalid"));
38003859Sml29623 		return;
38013859Sml29623 
38023859Sml29623 	case LB_GET_INFO_SIZE:
38033859Sml29623 	case LB_GET_INFO:
38043859Sml29623 	case LB_GET_MODE:
38053859Sml29623 		need_privilege = B_FALSE;
38063859Sml29623 		break;
38073859Sml29623 	case LB_SET_MODE:
38083859Sml29623 		break;
38093859Sml29623 
38103859Sml29623 
38113859Sml29623 	case NXGE_GET_MII:
38123859Sml29623 	case NXGE_PUT_MII:
38133859Sml29623 	case NXGE_GET64:
38143859Sml29623 	case NXGE_PUT64:
38153859Sml29623 	case NXGE_GET_TX_RING_SZ:
38163859Sml29623 	case NXGE_GET_TX_DESC:
38173859Sml29623 	case NXGE_TX_SIDE_RESET:
38183859Sml29623 	case NXGE_RX_SIDE_RESET:
38193859Sml29623 	case NXGE_GLOBAL_RESET:
38203859Sml29623 	case NXGE_RESET_MAC:
38213859Sml29623 	case NXGE_TX_REGS_DUMP:
38223859Sml29623 	case NXGE_RX_REGS_DUMP:
38233859Sml29623 	case NXGE_INT_REGS_DUMP:
38243859Sml29623 	case NXGE_VIR_INT_REGS_DUMP:
38253859Sml29623 	case NXGE_PUT_TCAM:
38263859Sml29623 	case NXGE_GET_TCAM:
38273859Sml29623 	case NXGE_RTRACE:
38283859Sml29623 	case NXGE_RDUMP:
38293859Sml29623 
38303859Sml29623 		need_privilege = B_FALSE;
38313859Sml29623 		break;
38323859Sml29623 	case NXGE_INJECT_ERR:
38333859Sml29623 		cmn_err(CE_NOTE, "!nxge_m_ioctl: Inject error\n");
38343859Sml29623 		nxge_err_inject(nxgep, wq, mp);
38353859Sml29623 		break;
38363859Sml29623 	}
38373859Sml29623 
38383859Sml29623 	if (need_privilege) {
38394185Sspeer 		err = secpolicy_net_config(iocp->ioc_cr, B_FALSE);
38403859Sml29623 		if (err != 0) {
38413859Sml29623 			miocnak(wq, mp, 0, err);
38423859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
38436512Ssowmini 			    "<== nxge_m_ioctl: no priv"));
38443859Sml29623 			return;
38453859Sml29623 		}
38463859Sml29623 	}
38473859Sml29623 
38483859Sml29623 	switch (cmd) {
38493859Sml29623 
38503859Sml29623 	case LB_GET_MODE:
38513859Sml29623 	case LB_SET_MODE:
38523859Sml29623 	case LB_GET_INFO_SIZE:
38533859Sml29623 	case LB_GET_INFO:
38543859Sml29623 		nxge_loopback_ioctl(nxgep, wq, mp, iocp);
38553859Sml29623 		break;
38563859Sml29623 
38573859Sml29623 	case NXGE_GET_MII:
38583859Sml29623 	case NXGE_PUT_MII:
38593859Sml29623 	case NXGE_PUT_TCAM:
38603859Sml29623 	case NXGE_GET_TCAM:
38613859Sml29623 	case NXGE_GET64:
38623859Sml29623 	case NXGE_PUT64:
38633859Sml29623 	case NXGE_GET_TX_RING_SZ:
38643859Sml29623 	case NXGE_GET_TX_DESC:
38653859Sml29623 	case NXGE_TX_SIDE_RESET:
38663859Sml29623 	case NXGE_RX_SIDE_RESET:
38673859Sml29623 	case NXGE_GLOBAL_RESET:
38683859Sml29623 	case NXGE_RESET_MAC:
38693859Sml29623 	case NXGE_TX_REGS_DUMP:
38703859Sml29623 	case NXGE_RX_REGS_DUMP:
38713859Sml29623 	case NXGE_INT_REGS_DUMP:
38723859Sml29623 	case NXGE_VIR_INT_REGS_DUMP:
38733859Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
38746512Ssowmini 		    "==> nxge_m_ioctl: cmd 0x%x", cmd));
38753859Sml29623 		nxge_hw_ioctl(nxgep, wq, mp, iocp);
38763859Sml29623 		break;
38773859Sml29623 	}
38783859Sml29623 
38793859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_ioctl"));
38803859Sml29623 }
38813859Sml29623 
38823859Sml29623 extern void nxge_rx_hw_blank(void *arg, time_t ticks, uint_t count);
38833859Sml29623 
38843859Sml29623 static void
38853859Sml29623 nxge_m_resources(void *arg)
38863859Sml29623 {
38873859Sml29623 	p_nxge_t		nxgep = arg;
38883859Sml29623 	mac_rx_fifo_t 		mrf;
38896495Sspeer 
38906495Sspeer 	nxge_grp_set_t		*set = &nxgep->rx_set;
38916495Sspeer 	uint8_t			rdc;
38926495Sspeer 
38936495Sspeer 	rx_rcr_ring_t		*ring;
38943859Sml29623 
38953859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "==> nxge_m_resources"));
38963859Sml29623 
38973859Sml29623 	MUTEX_ENTER(nxgep->genlock);
38983859Sml29623 
38996495Sspeer 	if (set->owned.map == 0) {
39006495Sspeer 		NXGE_ERROR_MSG((NULL, NXGE_ERR_CTL,
39016495Sspeer 		    "nxge_m_resources: no receive resources"));
39026495Sspeer 		goto nxge_m_resources_exit;
39036495Sspeer 	}
39046495Sspeer 
39053859Sml29623 	/*
39063859Sml29623 	 * CR 6492541 Check to see if the drv_state has been initialized,
39073859Sml29623 	 * if not * call nxge_init().
39083859Sml29623 	 */
39093859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
39106495Sspeer 		if (nxge_init(nxgep) != NXGE_OK)
39113859Sml29623 			goto nxge_m_resources_exit;
39123859Sml29623 	}
39133859Sml29623 
39143859Sml29623 	mrf.mrf_type = MAC_RX_FIFO;
39153859Sml29623 	mrf.mrf_blank = nxge_rx_hw_blank;
39163859Sml29623 	mrf.mrf_arg = (void *)nxgep;
39173859Sml29623 
39183859Sml29623 	mrf.mrf_normal_blank_time = 128;
39193859Sml29623 	mrf.mrf_normal_pkt_count = 8;
39203859Sml29623 
39213859Sml29623 	/*
39223859Sml29623 	 * Export our receive resources to the MAC layer.
39233859Sml29623 	 */
39246495Sspeer 	for (rdc = 0; rdc < NXGE_MAX_RDCS; rdc++) {
39256495Sspeer 		if ((1 << rdc) & set->owned.map) {
39266495Sspeer 			ring = nxgep->rx_rcr_rings->rcr_rings[rdc];
39276495Sspeer 			if (ring == 0) {
39286495Sspeer 				/*
39296495Sspeer 				 * This is a big deal only if we are
39306495Sspeer 				 * *not* in an LDOMs environment.
39316495Sspeer 				 */
39326495Sspeer 				if (nxgep->environs == SOLARIS_DOMAIN) {
39336495Sspeer 					cmn_err(CE_NOTE,
39346495Sspeer 					    "==> nxge_m_resources: "
39356495Sspeer 					    "ring %d == 0", rdc);
39366495Sspeer 				}
39376495Sspeer 				continue;
39386495Sspeer 			}
39396495Sspeer 			ring->rcr_mac_handle = mac_resource_add
39406495Sspeer 			    (nxgep->mach, (mac_resource_t *)&mrf);
39416495Sspeer 
39426495Sspeer 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
39436495Sspeer 			    "==> nxge_m_resources: RDC %d RCR %p MAC handle %p",
39446495Sspeer 			    rdc, ring, ring->rcr_mac_handle));
39456495Sspeer 		}
39463859Sml29623 	}
39473859Sml29623 
39483859Sml29623 nxge_m_resources_exit:
39493859Sml29623 	MUTEX_EXIT(nxgep->genlock);
39503859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "<== nxge_m_resources"));
39513859Sml29623 }
39523859Sml29623 
39536495Sspeer void
39543859Sml29623 nxge_mmac_kstat_update(p_nxge_t nxgep, mac_addr_slot_t slot, boolean_t factory)
39553859Sml29623 {
39563859Sml29623 	p_nxge_mmac_stats_t mmac_stats;
39573859Sml29623 	int i;
39583859Sml29623 	nxge_mmac_t *mmac_info;
39593859Sml29623 
39603859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
39613859Sml29623 
39623859Sml29623 	mmac_stats = &nxgep->statsp->mmac_stats;
39633859Sml29623 	mmac_stats->mmac_max_cnt = mmac_info->num_mmac;
39643859Sml29623 	mmac_stats->mmac_avail_cnt = mmac_info->naddrfree;
39653859Sml29623 
39663859Sml29623 	for (i = 0; i < ETHERADDRL; i++) {
39673859Sml29623 		if (factory) {
39683859Sml29623 			mmac_stats->mmac_avail_pool[slot-1].ether_addr_octet[i]
39696512Ssowmini 			    = mmac_info->factory_mac_pool[slot][
39706512Ssowmini 			    (ETHERADDRL-1) - i];
39713859Sml29623 		} else {
39723859Sml29623 			mmac_stats->mmac_avail_pool[slot-1].ether_addr_octet[i]
39736512Ssowmini 			    = mmac_info->mac_pool[slot].addr[
39746512Ssowmini 			    (ETHERADDRL - 1) - i];
39753859Sml29623 		}
39763859Sml29623 	}
39773859Sml29623 }
39783859Sml29623 
39793859Sml29623 /*
39803859Sml29623  * nxge_altmac_set() -- Set an alternate MAC address
39813859Sml29623  */
39823859Sml29623 static int
39833859Sml29623 nxge_altmac_set(p_nxge_t nxgep, uint8_t *maddr, mac_addr_slot_t slot)
39843859Sml29623 {
39853859Sml29623 	uint8_t addrn;
39863859Sml29623 	uint8_t portn;
39873859Sml29623 	npi_mac_addr_t altmac;
39884484Sspeer 	hostinfo_t mac_rdc;
39894484Sspeer 	p_nxge_class_pt_cfg_t clscfgp;
39903859Sml29623 
39913859Sml29623 	altmac.w2 = ((uint16_t)maddr[0] << 8) | ((uint16_t)maddr[1] & 0x0ff);
39923859Sml29623 	altmac.w1 = ((uint16_t)maddr[2] << 8) | ((uint16_t)maddr[3] & 0x0ff);
39933859Sml29623 	altmac.w0 = ((uint16_t)maddr[4] << 8) | ((uint16_t)maddr[5] & 0x0ff);
39943859Sml29623 
39953859Sml29623 	portn = nxgep->mac.portnum;
39963859Sml29623 	addrn = (uint8_t)slot - 1;
39973859Sml29623 
39983859Sml29623 	if (npi_mac_altaddr_entry(nxgep->npi_handle, OP_SET, portn,
39996512Ssowmini 	    addrn, &altmac) != NPI_SUCCESS)
40003859Sml29623 		return (EIO);
40014484Sspeer 
40024484Sspeer 	/*
40034484Sspeer 	 * Set the rdc table number for the host info entry
40044484Sspeer 	 * for this mac address slot.
40054484Sspeer 	 */
40064484Sspeer 	clscfgp = (p_nxge_class_pt_cfg_t)&nxgep->class_config;
40074484Sspeer 	mac_rdc.value = 0;
40084484Sspeer 	mac_rdc.bits.w0.rdc_tbl_num = clscfgp->mac_host_info[addrn].rdctbl;
40094484Sspeer 	mac_rdc.bits.w0.mac_pref = clscfgp->mac_host_info[addrn].mpr_npr;
40104484Sspeer 
40114484Sspeer 	if (npi_mac_hostinfo_entry(nxgep->npi_handle, OP_SET,
40124484Sspeer 	    nxgep->function_num, addrn, &mac_rdc) != NPI_SUCCESS) {
40134484Sspeer 		return (EIO);
40144484Sspeer 	}
40154484Sspeer 
40163859Sml29623 	/*
40173859Sml29623 	 * Enable comparison with the alternate MAC address.
40183859Sml29623 	 * While the first alternate addr is enabled by bit 1 of register
40193859Sml29623 	 * BMAC_ALTAD_CMPEN, it is enabled by bit 0 of register
40203859Sml29623 	 * XMAC_ADDR_CMPEN, so slot needs to be converted to addrn
40213859Sml29623 	 * accordingly before calling npi_mac_altaddr_entry.
40223859Sml29623 	 */
40233859Sml29623 	if (portn == XMAC_PORT_0 || portn == XMAC_PORT_1)
40243859Sml29623 		addrn = (uint8_t)slot - 1;
40253859Sml29623 	else
40263859Sml29623 		addrn = (uint8_t)slot;
40273859Sml29623 
40283859Sml29623 	if (npi_mac_altaddr_enable(nxgep->npi_handle, portn, addrn)
40296512Ssowmini 	    != NPI_SUCCESS)
40303859Sml29623 		return (EIO);
40313859Sml29623 
40323859Sml29623 	return (0);
40333859Sml29623 }
40343859Sml29623 
40353859Sml29623 /*
40363859Sml29623  * nxeg_m_mmac_add() - find an unused address slot, set the address
40373859Sml29623  * value to the one specified, enable the port to start filtering on
40383859Sml29623  * the new MAC address.  Returns 0 on success.
40393859Sml29623  */
40406495Sspeer int
40413859Sml29623 nxge_m_mmac_add(void *arg, mac_multi_addr_t *maddr)
40423859Sml29623 {
40433859Sml29623 	p_nxge_t nxgep = arg;
40443859Sml29623 	mac_addr_slot_t slot;
40453859Sml29623 	nxge_mmac_t *mmac_info;
40463859Sml29623 	int err;
40473859Sml29623 	nxge_status_t status;
40483859Sml29623 
40493859Sml29623 	mutex_enter(nxgep->genlock);
40503859Sml29623 
40513859Sml29623 	/*
40523859Sml29623 	 * Make sure that nxge is initialized, if _start() has
40533859Sml29623 	 * not been called.
40543859Sml29623 	 */
40553859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
40563859Sml29623 		status = nxge_init(nxgep);
40573859Sml29623 		if (status != NXGE_OK) {
40583859Sml29623 			mutex_exit(nxgep->genlock);
40593859Sml29623 			return (ENXIO);
40603859Sml29623 		}
40613859Sml29623 	}
40623859Sml29623 
40633859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
40643859Sml29623 	if (mmac_info->naddrfree == 0) {
40653859Sml29623 		mutex_exit(nxgep->genlock);
40663859Sml29623 		return (ENOSPC);
40673859Sml29623 	}
40683859Sml29623 	if (!mac_unicst_verify(nxgep->mach, maddr->mma_addr,
40696512Ssowmini 	    maddr->mma_addrlen)) {
40703859Sml29623 		mutex_exit(nxgep->genlock);
40713859Sml29623 		return (EINVAL);
40723859Sml29623 	}
40733859Sml29623 	/*
40743859Sml29623 	 * 	Search for the first available slot. Because naddrfree
40753859Sml29623 	 * is not zero, we are guaranteed to find one.
40763859Sml29623 	 * 	Slot 0 is for unique (primary) MAC. The first alternate
40773859Sml29623 	 * MAC slot is slot 1.
40783859Sml29623 	 *	Each of the first two ports of Neptune has 16 alternate
40796495Sspeer 	 * MAC slots but only the first 7 (of 15) slots have assigned factory
40803859Sml29623 	 * MAC addresses. We first search among the slots without bundled
40813859Sml29623 	 * factory MACs. If we fail to find one in that range, then we
40823859Sml29623 	 * search the slots with bundled factory MACs.  A factory MAC
40833859Sml29623 	 * will be wasted while the slot is used with a user MAC address.
40843859Sml29623 	 * But the slot could be used by factory MAC again after calling
40853859Sml29623 	 * nxge_m_mmac_remove and nxge_m_mmac_reserve.
40863859Sml29623 	 */
40873859Sml29623 	if (mmac_info->num_factory_mmac < mmac_info->num_mmac) {
40883859Sml29623 		for (slot = mmac_info->num_factory_mmac + 1;
40896512Ssowmini 		    slot <= mmac_info->num_mmac; slot++) {
40903859Sml29623 			if (!(mmac_info->mac_pool[slot].flags & MMAC_SLOT_USED))
40913859Sml29623 				break;
40923859Sml29623 		}
40933859Sml29623 		if (slot > mmac_info->num_mmac) {
40943859Sml29623 			for (slot = 1; slot <= mmac_info->num_factory_mmac;
40956512Ssowmini 			    slot++) {
40963859Sml29623 				if (!(mmac_info->mac_pool[slot].flags
40976512Ssowmini 				    & MMAC_SLOT_USED))
40983859Sml29623 					break;
40993859Sml29623 			}
41003859Sml29623 		}
41013859Sml29623 	} else {
41023859Sml29623 		for (slot = 1; slot <= mmac_info->num_mmac; slot++) {
41033859Sml29623 			if (!(mmac_info->mac_pool[slot].flags & MMAC_SLOT_USED))
41043859Sml29623 				break;
41053859Sml29623 		}
41063859Sml29623 	}
41073859Sml29623 	ASSERT(slot <= mmac_info->num_mmac);
41083859Sml29623 	if ((err = nxge_altmac_set(nxgep, maddr->mma_addr, slot)) != 0) {
41093859Sml29623 		mutex_exit(nxgep->genlock);
41103859Sml29623 		return (err);
41113859Sml29623 	}
41123859Sml29623 	bcopy(maddr->mma_addr, mmac_info->mac_pool[slot].addr, ETHERADDRL);
41133859Sml29623 	mmac_info->mac_pool[slot].flags |= MMAC_SLOT_USED;
41143859Sml29623 	mmac_info->mac_pool[slot].flags &= ~MMAC_VENDOR_ADDR;
41153859Sml29623 	mmac_info->naddrfree--;
41163859Sml29623 	nxge_mmac_kstat_update(nxgep, slot, B_FALSE);
41173859Sml29623 
41183859Sml29623 	maddr->mma_slot = slot;
41193859Sml29623 
41203859Sml29623 	mutex_exit(nxgep->genlock);
41213859Sml29623 	return (0);
41223859Sml29623 }
41233859Sml29623 
41243859Sml29623 /*
41253859Sml29623  * This function reserves an unused slot and programs the slot and the HW
41263859Sml29623  * with a factory mac address.
41273859Sml29623  */
41283859Sml29623 static int
41293859Sml29623 nxge_m_mmac_reserve(void *arg, mac_multi_addr_t *maddr)
41303859Sml29623 {
41313859Sml29623 	p_nxge_t nxgep = arg;
41323859Sml29623 	mac_addr_slot_t slot;
41333859Sml29623 	nxge_mmac_t *mmac_info;
41343859Sml29623 	int err;
41353859Sml29623 	nxge_status_t status;
41363859Sml29623 
41373859Sml29623 	mutex_enter(nxgep->genlock);
41383859Sml29623 
41393859Sml29623 	/*
41403859Sml29623 	 * Make sure that nxge is initialized, if _start() has
41413859Sml29623 	 * not been called.
41423859Sml29623 	 */
41433859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
41443859Sml29623 		status = nxge_init(nxgep);
41453859Sml29623 		if (status != NXGE_OK) {
41463859Sml29623 			mutex_exit(nxgep->genlock);
41473859Sml29623 			return (ENXIO);
41483859Sml29623 		}
41493859Sml29623 	}
41503859Sml29623 
41513859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
41523859Sml29623 	if (mmac_info->naddrfree == 0) {
41533859Sml29623 		mutex_exit(nxgep->genlock);
41543859Sml29623 		return (ENOSPC);
41553859Sml29623 	}
41563859Sml29623 
41573859Sml29623 	slot = maddr->mma_slot;
41583859Sml29623 	if (slot == -1) {  /* -1: Take the first available slot */
41593859Sml29623 		for (slot = 1; slot <= mmac_info->num_factory_mmac; slot++) {
41603859Sml29623 			if (!(mmac_info->mac_pool[slot].flags & MMAC_SLOT_USED))
41613859Sml29623 				break;
41623859Sml29623 		}
41633859Sml29623 		if (slot > mmac_info->num_factory_mmac) {
41643859Sml29623 			mutex_exit(nxgep->genlock);
41653859Sml29623 			return (ENOSPC);
41663859Sml29623 		}
41673859Sml29623 	}
41683859Sml29623 	if (slot < 1 || slot > mmac_info->num_factory_mmac) {
41693859Sml29623 		/*
41703859Sml29623 		 * Do not support factory MAC at a slot greater than
41713859Sml29623 		 * num_factory_mmac even when there are available factory
41723859Sml29623 		 * MAC addresses because the alternate MACs are bundled with
41733859Sml29623 		 * slot[1] through slot[num_factory_mmac]
41743859Sml29623 		 */
41753859Sml29623 		mutex_exit(nxgep->genlock);
41763859Sml29623 		return (EINVAL);
41773859Sml29623 	}
41783859Sml29623 	if (mmac_info->mac_pool[slot].flags & MMAC_SLOT_USED) {
41793859Sml29623 		mutex_exit(nxgep->genlock);
41803859Sml29623 		return (EBUSY);
41813859Sml29623 	}
41823859Sml29623 	/* Verify the address to be reserved */
41833859Sml29623 	if (!mac_unicst_verify(nxgep->mach,
41846512Ssowmini 	    mmac_info->factory_mac_pool[slot], ETHERADDRL)) {
41853859Sml29623 		mutex_exit(nxgep->genlock);
41863859Sml29623 		return (EINVAL);
41873859Sml29623 	}
41883859Sml29623 	if (err = nxge_altmac_set(nxgep,
41896512Ssowmini 	    mmac_info->factory_mac_pool[slot], slot)) {
41903859Sml29623 		mutex_exit(nxgep->genlock);
41913859Sml29623 		return (err);
41923859Sml29623 	}
41933859Sml29623 	bcopy(mmac_info->factory_mac_pool[slot], maddr->mma_addr, ETHERADDRL);
41943859Sml29623 	mmac_info->mac_pool[slot].flags |= MMAC_SLOT_USED | MMAC_VENDOR_ADDR;
41953859Sml29623 	mmac_info->naddrfree--;
41963859Sml29623 
41973859Sml29623 	nxge_mmac_kstat_update(nxgep, slot, B_TRUE);
41983859Sml29623 	mutex_exit(nxgep->genlock);
41993859Sml29623 
42003859Sml29623 	/* Pass info back to the caller */
42013859Sml29623 	maddr->mma_slot = slot;
42023859Sml29623 	maddr->mma_addrlen = ETHERADDRL;
42033859Sml29623 	maddr->mma_flags = MMAC_SLOT_USED | MMAC_VENDOR_ADDR;
42043859Sml29623 
42053859Sml29623 	return (0);
42063859Sml29623 }
42073859Sml29623 
42083859Sml29623 /*
42093859Sml29623  * Remove the specified mac address and update the HW not to filter
42103859Sml29623  * the mac address anymore.
42113859Sml29623  */
42126495Sspeer int
42133859Sml29623 nxge_m_mmac_remove(void *arg, mac_addr_slot_t slot)
42143859Sml29623 {
42153859Sml29623 	p_nxge_t nxgep = arg;
42163859Sml29623 	nxge_mmac_t *mmac_info;
42173859Sml29623 	uint8_t addrn;
42183859Sml29623 	uint8_t portn;
42193859Sml29623 	int err = 0;
42203859Sml29623 	nxge_status_t status;
42213859Sml29623 
42223859Sml29623 	mutex_enter(nxgep->genlock);
42233859Sml29623 
42243859Sml29623 	/*
42253859Sml29623 	 * Make sure that nxge is initialized, if _start() has
42263859Sml29623 	 * not been called.
42273859Sml29623 	 */
42283859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
42293859Sml29623 		status = nxge_init(nxgep);
42303859Sml29623 		if (status != NXGE_OK) {
42313859Sml29623 			mutex_exit(nxgep->genlock);
42323859Sml29623 			return (ENXIO);
42333859Sml29623 		}
42343859Sml29623 	}
42353859Sml29623 
42363859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
42373859Sml29623 	if (slot < 1 || slot > mmac_info->num_mmac) {
42383859Sml29623 		mutex_exit(nxgep->genlock);
42393859Sml29623 		return (EINVAL);
42403859Sml29623 	}
42413859Sml29623 
42423859Sml29623 	portn = nxgep->mac.portnum;
42433859Sml29623 	if (portn == XMAC_PORT_0 || portn == XMAC_PORT_1)
42443859Sml29623 		addrn = (uint8_t)slot - 1;
42453859Sml29623 	else
42463859Sml29623 		addrn = (uint8_t)slot;
42473859Sml29623 
42483859Sml29623 	if (mmac_info->mac_pool[slot].flags & MMAC_SLOT_USED) {
42493859Sml29623 		if (npi_mac_altaddr_disable(nxgep->npi_handle, portn, addrn)
42506512Ssowmini 		    == NPI_SUCCESS) {
42513859Sml29623 			mmac_info->naddrfree++;
42523859Sml29623 			mmac_info->mac_pool[slot].flags &= ~MMAC_SLOT_USED;
42533859Sml29623 			/*
42543859Sml29623 			 * Regardless if the MAC we just stopped filtering
42553859Sml29623 			 * is a user addr or a facory addr, we must set
42563859Sml29623 			 * the MMAC_VENDOR_ADDR flag if this slot has an
42573859Sml29623 			 * associated factory MAC to indicate that a factory
42583859Sml29623 			 * MAC is available.
42593859Sml29623 			 */
42603859Sml29623 			if (slot <= mmac_info->num_factory_mmac) {
42613859Sml29623 				mmac_info->mac_pool[slot].flags
42626512Ssowmini 				    |= MMAC_VENDOR_ADDR;
42633859Sml29623 			}
42643859Sml29623 			/*
42653859Sml29623 			 * Clear mac_pool[slot].addr so that kstat shows 0
42663859Sml29623 			 * alternate MAC address if the slot is not used.
42673859Sml29623 			 * (But nxge_m_mmac_get returns the factory MAC even
42683859Sml29623 			 * when the slot is not used!)
42693859Sml29623 			 */
42703859Sml29623 			bzero(mmac_info->mac_pool[slot].addr, ETHERADDRL);
42713859Sml29623 			nxge_mmac_kstat_update(nxgep, slot, B_FALSE);
42723859Sml29623 		} else {
42733859Sml29623 			err = EIO;
42743859Sml29623 		}
42753859Sml29623 	} else {
42763859Sml29623 		err = EINVAL;
42773859Sml29623 	}
42783859Sml29623 
42793859Sml29623 	mutex_exit(nxgep->genlock);
42803859Sml29623 	return (err);
42813859Sml29623 }
42823859Sml29623 
42833859Sml29623 /*
42843859Sml29623  * Modify a mac address added by nxge_m_mmac_add or nxge_m_mmac_reserve().
42853859Sml29623  */
42863859Sml29623 static int
42873859Sml29623 nxge_m_mmac_modify(void *arg, mac_multi_addr_t *maddr)
42883859Sml29623 {
42893859Sml29623 	p_nxge_t nxgep = arg;
42903859Sml29623 	mac_addr_slot_t slot;
42913859Sml29623 	nxge_mmac_t *mmac_info;
42923859Sml29623 	int err = 0;
42933859Sml29623 	nxge_status_t status;
42943859Sml29623 
42953859Sml29623 	if (!mac_unicst_verify(nxgep->mach, maddr->mma_addr,
42966512Ssowmini 	    maddr->mma_addrlen))
42973859Sml29623 		return (EINVAL);
42983859Sml29623 
42993859Sml29623 	slot = maddr->mma_slot;
43003859Sml29623 
43013859Sml29623 	mutex_enter(nxgep->genlock);
43023859Sml29623 
43033859Sml29623 	/*
43043859Sml29623 	 * Make sure that nxge is initialized, if _start() has
43053859Sml29623 	 * not been called.
43063859Sml29623 	 */
43073859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
43083859Sml29623 		status = nxge_init(nxgep);
43093859Sml29623 		if (status != NXGE_OK) {
43103859Sml29623 			mutex_exit(nxgep->genlock);
43113859Sml29623 			return (ENXIO);
43123859Sml29623 		}
43133859Sml29623 	}
43143859Sml29623 
43153859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
43163859Sml29623 	if (slot < 1 || slot > mmac_info->num_mmac) {
43173859Sml29623 		mutex_exit(nxgep->genlock);
43183859Sml29623 		return (EINVAL);
43193859Sml29623 	}
43203859Sml29623 	if (mmac_info->mac_pool[slot].flags & MMAC_SLOT_USED) {
43213859Sml29623 		if ((err = nxge_altmac_set(nxgep, maddr->mma_addr, slot))
43226512Ssowmini 		    != 0) {
43233859Sml29623 			bcopy(maddr->mma_addr, mmac_info->mac_pool[slot].addr,
43246512Ssowmini 			    ETHERADDRL);
43253859Sml29623 			/*
43263859Sml29623 			 * Assume that the MAC passed down from the caller
43273859Sml29623 			 * is not a factory MAC address (The user should
43283859Sml29623 			 * call mmac_remove followed by mmac_reserve if
43293859Sml29623 			 * he wants to use the factory MAC for this slot).
43303859Sml29623 			 */
43313859Sml29623 			mmac_info->mac_pool[slot].flags &= ~MMAC_VENDOR_ADDR;
43323859Sml29623 			nxge_mmac_kstat_update(nxgep, slot, B_FALSE);
43333859Sml29623 		}
43343859Sml29623 	} else {
43353859Sml29623 		err = EINVAL;
43363859Sml29623 	}
43373859Sml29623 	mutex_exit(nxgep->genlock);
43383859Sml29623 	return (err);
43393859Sml29623 }
43403859Sml29623 
43413859Sml29623 /*
43423859Sml29623  * nxge_m_mmac_get() - Get the MAC address and other information
43433859Sml29623  * related to the slot.  mma_flags should be set to 0 in the call.
43443859Sml29623  * Note: although kstat shows MAC address as zero when a slot is
43453859Sml29623  * not used, Crossbow expects nxge_m_mmac_get to copy factory MAC
43463859Sml29623  * to the caller as long as the slot is not using a user MAC address.
43473859Sml29623  * The following table shows the rules,
43483859Sml29623  *
43493859Sml29623  *				   USED    VENDOR    mma_addr
43503859Sml29623  * ------------------------------------------------------------
43513859Sml29623  * (1) Slot uses a user MAC:        yes      no     user MAC
43523859Sml29623  * (2) Slot uses a factory MAC:     yes      yes    factory MAC
43533859Sml29623  * (3) Slot is not used but is
43543859Sml29623  *     factory MAC capable:         no       yes    factory MAC
43553859Sml29623  * (4) Slot is not used and is
43563859Sml29623  *     not factory MAC capable:     no       no        0
43573859Sml29623  * ------------------------------------------------------------
43583859Sml29623  */
43593859Sml29623 static int
43603859Sml29623 nxge_m_mmac_get(void *arg, mac_multi_addr_t *maddr)
43613859Sml29623 {
43623859Sml29623 	nxge_t *nxgep = arg;
43633859Sml29623 	mac_addr_slot_t slot;
43643859Sml29623 	nxge_mmac_t *mmac_info;
43653859Sml29623 	nxge_status_t status;
43663859Sml29623 
43673859Sml29623 	slot = maddr->mma_slot;
43683859Sml29623 
43693859Sml29623 	mutex_enter(nxgep->genlock);
43703859Sml29623 
43713859Sml29623 	/*
43723859Sml29623 	 * Make sure that nxge is initialized, if _start() has
43733859Sml29623 	 * not been called.
43743859Sml29623 	 */
43753859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
43763859Sml29623 		status = nxge_init(nxgep);
43773859Sml29623 		if (status != NXGE_OK) {
43783859Sml29623 			mutex_exit(nxgep->genlock);
43793859Sml29623 			return (ENXIO);
43803859Sml29623 		}
43813859Sml29623 	}
43823859Sml29623 
43833859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
43843859Sml29623 
43853859Sml29623 	if (slot < 1 || slot > mmac_info->num_mmac) {
43863859Sml29623 		mutex_exit(nxgep->genlock);
43873859Sml29623 		return (EINVAL);
43883859Sml29623 	}
43893859Sml29623 	maddr->mma_flags = 0;
43903859Sml29623 	if (mmac_info->mac_pool[slot].flags & MMAC_SLOT_USED)
43913859Sml29623 		maddr->mma_flags |= MMAC_SLOT_USED;
43923859Sml29623 
43933859Sml29623 	if (mmac_info->mac_pool[slot].flags & MMAC_VENDOR_ADDR) {
43943859Sml29623 		maddr->mma_flags |= MMAC_VENDOR_ADDR;
43953859Sml29623 		bcopy(mmac_info->factory_mac_pool[slot],
43966512Ssowmini 		    maddr->mma_addr, ETHERADDRL);
43973859Sml29623 		maddr->mma_addrlen = ETHERADDRL;
43983859Sml29623 	} else {
43993859Sml29623 		if (maddr->mma_flags & MMAC_SLOT_USED) {
44003859Sml29623 			bcopy(mmac_info->mac_pool[slot].addr,
44016512Ssowmini 			    maddr->mma_addr, ETHERADDRL);
44023859Sml29623 			maddr->mma_addrlen = ETHERADDRL;
44033859Sml29623 		} else {
44043859Sml29623 			bzero(maddr->mma_addr, ETHERADDRL);
44053859Sml29623 			maddr->mma_addrlen = 0;
44063859Sml29623 		}
44073859Sml29623 	}
44083859Sml29623 	mutex_exit(nxgep->genlock);
44093859Sml29623 	return (0);
44103859Sml29623 }
44113859Sml29623 
44123859Sml29623 static boolean_t
44133859Sml29623 nxge_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
44143859Sml29623 {
44153859Sml29623 	nxge_t *nxgep = arg;
44163859Sml29623 	uint32_t *txflags = cap_data;
44173859Sml29623 	multiaddress_capab_t *mmacp = cap_data;
44183859Sml29623 
44193859Sml29623 	switch (cap) {
44203859Sml29623 	case MAC_CAPAB_HCKSUM:
44216495Sspeer 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
44226611Sml29623 		    "==> nxge_m_getcapab: checksum %d", nxge_cksum_offload));
44236611Sml29623 		if (nxge_cksum_offload <= 1) {
44246495Sspeer 			*txflags = HCKSUM_INET_PARTIAL;
44256495Sspeer 		}
44263859Sml29623 		break;
44276495Sspeer 
44283859Sml29623 	case MAC_CAPAB_POLL:
44293859Sml29623 		/*
44303859Sml29623 		 * There's nothing for us to fill in, simply returning
44313859Sml29623 		 * B_TRUE stating that we support polling is sufficient.
44323859Sml29623 		 */
44333859Sml29623 		break;
44343859Sml29623 
44353859Sml29623 	case MAC_CAPAB_MULTIADDRESS:
44366495Sspeer 		mmacp = (multiaddress_capab_t *)cap_data;
44373859Sml29623 		mutex_enter(nxgep->genlock);
44383859Sml29623 
44393859Sml29623 		mmacp->maddr_naddr = nxgep->nxge_mmac_info.num_mmac;
44403859Sml29623 		mmacp->maddr_naddrfree = nxgep->nxge_mmac_info.naddrfree;
44416611Sml29623 		mmacp->maddr_flag = 0; /* 0 is required by PSARC2006/265 */
44423859Sml29623 		/*
44433859Sml29623 		 * maddr_handle is driver's private data, passed back to
44443859Sml29623 		 * entry point functions as arg.
44453859Sml29623 		 */
44463859Sml29623 		mmacp->maddr_handle	= nxgep;
44473859Sml29623 		mmacp->maddr_add	= nxge_m_mmac_add;
44483859Sml29623 		mmacp->maddr_remove	= nxge_m_mmac_remove;
44493859Sml29623 		mmacp->maddr_modify	= nxge_m_mmac_modify;
44503859Sml29623 		mmacp->maddr_get	= nxge_m_mmac_get;
44513859Sml29623 		mmacp->maddr_reserve	= nxge_m_mmac_reserve;
44523859Sml29623 
44533859Sml29623 		mutex_exit(nxgep->genlock);
44543859Sml29623 		break;
44556495Sspeer 
44565770Sml29623 	case MAC_CAPAB_LSO: {
44575770Sml29623 		mac_capab_lso_t *cap_lso = cap_data;
44585770Sml29623 
44596003Sml29623 		if (nxgep->soft_lso_enable) {
44606611Sml29623 			if (nxge_cksum_offload <= 1) {
44616611Sml29623 				cap_lso->lso_flags = LSO_TX_BASIC_TCP_IPV4;
44626611Sml29623 				if (nxge_lso_max > NXGE_LSO_MAXLEN) {
44636611Sml29623 					nxge_lso_max = NXGE_LSO_MAXLEN;
44646611Sml29623 				}
44656611Sml29623 				cap_lso->lso_basic_tcp_ipv4.lso_max =
44666611Sml29623 				    nxge_lso_max;
44675770Sml29623 			}
44685770Sml29623 			break;
44695770Sml29623 		} else {
44705770Sml29623 			return (B_FALSE);
44715770Sml29623 		}
44725770Sml29623 	}
44735770Sml29623 
44746495Sspeer #if defined(sun4v)
44756495Sspeer 	case MAC_CAPAB_RINGS: {
44766495Sspeer 		mac_capab_rings_t *mrings = (mac_capab_rings_t *)cap_data;
44776495Sspeer 
44786495Sspeer 		/*
44796495Sspeer 		 * Only the service domain driver responds to
44806495Sspeer 		 * this capability request.
44816495Sspeer 		 */
44826495Sspeer 		if (isLDOMservice(nxgep)) {
44836495Sspeer 			mrings->mr_handle = (void *)nxgep;
44846495Sspeer 
44856495Sspeer 			/*
44866495Sspeer 			 * No dynamic allocation of groups and
44876495Sspeer 			 * rings at this time.  Shares dictate the
44886705Sml29623 			 * configuration.
44896495Sspeer 			 */
44906495Sspeer 			mrings->mr_gadd_ring = NULL;
44916495Sspeer 			mrings->mr_grem_ring = NULL;
44926495Sspeer 			mrings->mr_rget = NULL;
44936495Sspeer 			mrings->mr_gget = nxge_hio_group_get;
44946495Sspeer 
44956495Sspeer 			if (mrings->mr_type == MAC_RING_TYPE_RX) {
44966495Sspeer 				mrings->mr_rnum = 8; /* XXX */
44976495Sspeer 				mrings->mr_gnum = 6; /* XXX */
44986495Sspeer 			} else {
44996495Sspeer 				mrings->mr_rnum = 8; /* XXX */
45006495Sspeer 				mrings->mr_gnum = 0; /* XXX */
45016495Sspeer 			}
45026495Sspeer 		} else
45036495Sspeer 			return (B_FALSE);
45046495Sspeer 		break;
45056495Sspeer 	}
45066495Sspeer 
45076495Sspeer 	case MAC_CAPAB_SHARES: {
45086495Sspeer 		mac_capab_share_t *mshares = (mac_capab_share_t *)cap_data;
45096495Sspeer 
45106495Sspeer 		/*
45116495Sspeer 		 * Only the service domain driver responds to
45126495Sspeer 		 * this capability request.
45136495Sspeer 		 */
45146495Sspeer 		if (isLDOMservice(nxgep)) {
45156495Sspeer 			mshares->ms_snum = 3;
45166495Sspeer 			mshares->ms_handle = (void *)nxgep;
45176495Sspeer 			mshares->ms_salloc = nxge_hio_share_alloc;
45186495Sspeer 			mshares->ms_sfree = nxge_hio_share_free;
45196495Sspeer 			mshares->ms_sadd = NULL;
45206495Sspeer 			mshares->ms_sremove = NULL;
45216495Sspeer 			mshares->ms_squery = nxge_hio_share_query;
45226495Sspeer 		} else
45236495Sspeer 			return (B_FALSE);
45246495Sspeer 		break;
45256495Sspeer 	}
45266495Sspeer #endif
45273859Sml29623 	default:
45283859Sml29623 		return (B_FALSE);
45293859Sml29623 	}
45303859Sml29623 	return (B_TRUE);
45313859Sml29623 }
45323859Sml29623 
45336439Sml29623 static boolean_t
45346439Sml29623 nxge_param_locked(mac_prop_id_t pr_num)
45356439Sml29623 {
45366439Sml29623 	/*
45376439Sml29623 	 * All adv_* parameters are locked (read-only) while
45386439Sml29623 	 * the device is in any sort of loopback mode ...
45396439Sml29623 	 */
45406439Sml29623 	switch (pr_num) {
45416789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
45426789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
45436789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
45446789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
45456789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
45466789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
45476789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
45486789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
45496789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
45506789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
45516789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
45526789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
45536789Sam223141 		case MAC_PROP_AUTONEG:
45546789Sam223141 		case MAC_PROP_FLOWCTRL:
45556439Sml29623 			return (B_TRUE);
45566439Sml29623 	}
45576439Sml29623 	return (B_FALSE);
45586439Sml29623 }
45596439Sml29623 
45606439Sml29623 /*
45616439Sml29623  * callback functions for set/get of properties
45626439Sml29623  */
45636439Sml29623 static int
45646439Sml29623 nxge_m_setprop(void *barg, const char *pr_name, mac_prop_id_t pr_num,
45656439Sml29623     uint_t pr_valsize, const void *pr_val)
45666439Sml29623 {
45676439Sml29623 	nxge_t		*nxgep = barg;
45686439Sml29623 	p_nxge_param_t	param_arr;
45696439Sml29623 	p_nxge_stats_t	statsp;
45706439Sml29623 	int		err = 0;
45716439Sml29623 	uint8_t		val;
45726439Sml29623 	uint32_t	cur_mtu, new_mtu, old_framesize;
45736439Sml29623 	link_flowctrl_t	fl;
45746439Sml29623 
45756439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_setprop"));
45766439Sml29623 	param_arr = nxgep->param_arr;
45776439Sml29623 	statsp = nxgep->statsp;
45786439Sml29623 	mutex_enter(nxgep->genlock);
45796439Sml29623 	if (statsp->port_stats.lb_mode != nxge_lb_normal &&
45806439Sml29623 	    nxge_param_locked(pr_num)) {
45816439Sml29623 		/*
45826439Sml29623 		 * All adv_* parameters are locked (read-only)
45836439Sml29623 		 * while the device is in any sort of loopback mode.
45846439Sml29623 		 */
45856439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
45866439Sml29623 		    "==> nxge_m_setprop: loopback mode: read only"));
45876439Sml29623 		mutex_exit(nxgep->genlock);
45886439Sml29623 		return (EBUSY);
45896439Sml29623 	}
45906439Sml29623 
45916439Sml29623 	val = *(uint8_t *)pr_val;
45926439Sml29623 	switch (pr_num) {
45936789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
45946439Sml29623 			nxgep->param_en_1000fdx = val;
45956439Sml29623 			param_arr[param_anar_1000fdx].value = val;
45966439Sml29623 
45976439Sml29623 			goto reprogram;
45986439Sml29623 
45996789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
46006439Sml29623 			nxgep->param_en_100fdx = val;
46016439Sml29623 			param_arr[param_anar_100fdx].value = val;
46026439Sml29623 
46036439Sml29623 			goto reprogram;
46046439Sml29623 
46056789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
46066439Sml29623 			nxgep->param_en_10fdx = val;
46076439Sml29623 			param_arr[param_anar_10fdx].value = val;
46086439Sml29623 
46096439Sml29623 			goto reprogram;
46106439Sml29623 
46116789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
46126789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
46136789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
46146789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
46156789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
46166789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
46176789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
46186789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
46196789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
46206789Sam223141 		case MAC_PROP_STATUS:
46216789Sam223141 		case MAC_PROP_SPEED:
46226789Sam223141 		case MAC_PROP_DUPLEX:
46236439Sml29623 			err = EINVAL; /* cannot set read-only properties */
46246439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
46256439Sml29623 			    "==> nxge_m_setprop:  read only property %d",
46266439Sml29623 			    pr_num));
46276439Sml29623 			break;
46286439Sml29623 
46296789Sam223141 		case MAC_PROP_AUTONEG:
46306439Sml29623 			param_arr[param_autoneg].value = val;
46316439Sml29623 
46326439Sml29623 			goto reprogram;
46336439Sml29623 
46346789Sam223141 		case MAC_PROP_MTU:
46356439Sml29623 			if (nxgep->nxge_mac_state == NXGE_MAC_STARTED) {
46366439Sml29623 				err = EBUSY;
46376439Sml29623 				break;
46386439Sml29623 			}
46396439Sml29623 
46406439Sml29623 			cur_mtu = nxgep->mac.default_mtu;
46416439Sml29623 			bcopy(pr_val, &new_mtu, sizeof (new_mtu));
46426439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
46436439Sml29623 			    "==> nxge_m_setprop: set MTU: %d is_jumbo %d",
46446439Sml29623 			    new_mtu, nxgep->mac.is_jumbo));
46456439Sml29623 
46466439Sml29623 			if (new_mtu == cur_mtu) {
46476439Sml29623 				err = 0;
46486439Sml29623 				break;
46496439Sml29623 			}
46506439Sml29623 			if (new_mtu < NXGE_DEFAULT_MTU ||
46516439Sml29623 			    new_mtu > NXGE_MAXIMUM_MTU) {
46526439Sml29623 				err = EINVAL;
46536439Sml29623 				break;
46546439Sml29623 			}
46556439Sml29623 
46566439Sml29623 			if ((new_mtu > NXGE_DEFAULT_MTU) &&
46576439Sml29623 			    !nxgep->mac.is_jumbo) {
46586439Sml29623 				err = EINVAL;
46596439Sml29623 				break;
46606439Sml29623 			}
46616439Sml29623 
46626439Sml29623 			old_framesize = (uint32_t)nxgep->mac.maxframesize;
46636439Sml29623 			nxgep->mac.maxframesize = (uint16_t)
46646439Sml29623 			    (new_mtu + NXGE_EHEADER_VLAN_CRC);
46656439Sml29623 			if (nxge_mac_set_framesize(nxgep)) {
46666444Sml29623 				nxgep->mac.maxframesize =
46676444Sml29623 				    (uint16_t)old_framesize;
46686439Sml29623 				err = EINVAL;
46696439Sml29623 				break;
46706439Sml29623 			}
46716439Sml29623 
46726439Sml29623 			err = mac_maxsdu_update(nxgep->mach, new_mtu);
46736439Sml29623 			if (err) {
46746444Sml29623 				nxgep->mac.maxframesize =
46756444Sml29623 				    (uint16_t)old_framesize;
46766439Sml29623 				err = EINVAL;
46776439Sml29623 				break;
46786439Sml29623 			}
46796439Sml29623 
46806439Sml29623 			nxgep->mac.default_mtu = new_mtu;
46816439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
46826439Sml29623 			    "==> nxge_m_setprop: set MTU: %d maxframe %d",
46836439Sml29623 			    new_mtu, nxgep->mac.maxframesize));
46846439Sml29623 			break;
46856439Sml29623 
46866789Sam223141 		case MAC_PROP_FLOWCTRL:
46876439Sml29623 			bcopy(pr_val, &fl, sizeof (fl));
46886439Sml29623 			switch (fl) {
46896439Sml29623 			default:
46906439Sml29623 				err = EINVAL;
46916439Sml29623 				break;
46926439Sml29623 
46936439Sml29623 			case LINK_FLOWCTRL_NONE:
46946439Sml29623 				param_arr[param_anar_pause].value = 0;
46956439Sml29623 				break;
46966439Sml29623 
46976439Sml29623 			case LINK_FLOWCTRL_RX:
46986439Sml29623 				param_arr[param_anar_pause].value = 1;
46996439Sml29623 				break;
47006439Sml29623 
47016439Sml29623 			case LINK_FLOWCTRL_TX:
47026439Sml29623 			case LINK_FLOWCTRL_BI:
47036439Sml29623 				err = EINVAL;
47046439Sml29623 				break;
47056439Sml29623 			}
47066439Sml29623 
47076439Sml29623 reprogram:
47086439Sml29623 			if (err == 0) {
47096439Sml29623 				if (!nxge_param_link_update(nxgep)) {
47106439Sml29623 					err = EINVAL;
47116439Sml29623 				}
47126439Sml29623 			}
47136439Sml29623 			break;
47146789Sam223141 		case MAC_PROP_PRIVATE:
47156439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47166439Sml29623 			    "==> nxge_m_setprop: private property"));
47176439Sml29623 			err = nxge_set_priv_prop(nxgep, pr_name, pr_valsize,
47186439Sml29623 			    pr_val);
47196439Sml29623 			break;
47206512Ssowmini 
47216512Ssowmini 		default:
47226512Ssowmini 			err = ENOTSUP;
47236512Ssowmini 			break;
47246439Sml29623 	}
47256439Sml29623 
47266439Sml29623 	mutex_exit(nxgep->genlock);
47276439Sml29623 
47286439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47296439Sml29623 	    "<== nxge_m_setprop (return %d)", err));
47306439Sml29623 	return (err);
47316439Sml29623 }
47326439Sml29623 
47336439Sml29623 static int
47346439Sml29623 nxge_m_getprop(void *barg, const char *pr_name, mac_prop_id_t pr_num,
47356512Ssowmini     uint_t pr_flags, uint_t pr_valsize, void *pr_val)
47366439Sml29623 {
47376439Sml29623 	nxge_t 		*nxgep = barg;
47386439Sml29623 	p_nxge_param_t	param_arr = nxgep->param_arr;
47396439Sml29623 	p_nxge_stats_t	statsp = nxgep->statsp;
47406439Sml29623 	int		err = 0;
47416439Sml29623 	link_flowctrl_t	fl;
47426439Sml29623 	uint64_t	tmp = 0;
47436512Ssowmini 	link_state_t	ls;
47446789Sam223141 	boolean_t	is_default = (pr_flags & MAC_PROP_DEFAULT);
47456439Sml29623 
47466439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47476439Sml29623 	    "==> nxge_m_getprop: pr_num %d", pr_num));
47486512Ssowmini 
47496512Ssowmini 	if (pr_valsize == 0)
47506512Ssowmini 		return (EINVAL);
47516512Ssowmini 
47526789Sam223141 	if ((is_default) && (pr_num != MAC_PROP_PRIVATE)) {
47536512Ssowmini 		err = nxge_get_def_val(nxgep, pr_num, pr_valsize, pr_val);
47546512Ssowmini 		return (err);
47556512Ssowmini 	}
47566512Ssowmini 
47576439Sml29623 	bzero(pr_val, pr_valsize);
47586439Sml29623 	switch (pr_num) {
47596789Sam223141 		case MAC_PROP_DUPLEX:
47606439Sml29623 			*(uint8_t *)pr_val = statsp->mac_stats.link_duplex;
47616439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47626439Sml29623 			    "==> nxge_m_getprop: duplex mode %d",
47636439Sml29623 			    *(uint8_t *)pr_val));
47646439Sml29623 			break;
47656439Sml29623 
47666789Sam223141 		case MAC_PROP_SPEED:
47676439Sml29623 			if (pr_valsize < sizeof (uint64_t))
47686439Sml29623 				return (EINVAL);
47696439Sml29623 			tmp = statsp->mac_stats.link_speed * 1000000ull;
47706439Sml29623 			bcopy(&tmp, pr_val, sizeof (tmp));
47716439Sml29623 			break;
47726439Sml29623 
47736789Sam223141 		case MAC_PROP_STATUS:
47746512Ssowmini 			if (pr_valsize < sizeof (link_state_t))
47756439Sml29623 				return (EINVAL);
47766512Ssowmini 			if (!statsp->mac_stats.link_up)
47776512Ssowmini 				ls = LINK_STATE_DOWN;
47786512Ssowmini 			else
47796512Ssowmini 				ls = LINK_STATE_UP;
47806512Ssowmini 			bcopy(&ls, pr_val, sizeof (ls));
47816439Sml29623 			break;
47826439Sml29623 
47836789Sam223141 		case MAC_PROP_AUTONEG:
47846439Sml29623 			*(uint8_t *)pr_val =
47856439Sml29623 			    param_arr[param_autoneg].value;
47866439Sml29623 			break;
47876439Sml29623 
47886789Sam223141 		case MAC_PROP_FLOWCTRL:
47896439Sml29623 			if (pr_valsize < sizeof (link_flowctrl_t))
47906439Sml29623 				return (EINVAL);
47916439Sml29623 
47926439Sml29623 			fl = LINK_FLOWCTRL_NONE;
47936439Sml29623 			if (param_arr[param_anar_pause].value) {
47946439Sml29623 				fl = LINK_FLOWCTRL_RX;
47956439Sml29623 			}
47966439Sml29623 			bcopy(&fl, pr_val, sizeof (fl));
47976439Sml29623 			break;
47986439Sml29623 
47996789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
48006439Sml29623 			*(uint8_t *)pr_val =
48016439Sml29623 			    param_arr[param_anar_1000fdx].value;
48026439Sml29623 			break;
48036439Sml29623 
48046789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
48056439Sml29623 			*(uint8_t *)pr_val = nxgep->param_en_1000fdx;
48066439Sml29623 			break;
48076439Sml29623 
48086789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
48096439Sml29623 			*(uint8_t *)pr_val =
48106439Sml29623 			    param_arr[param_anar_100fdx].value;
48116439Sml29623 			break;
48126439Sml29623 
48136789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
48146439Sml29623 			*(uint8_t *)pr_val = nxgep->param_en_100fdx;
48156439Sml29623 			break;
48166439Sml29623 
48176789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
48186439Sml29623 			*(uint8_t *)pr_val =
48196439Sml29623 			    param_arr[param_anar_10fdx].value;
48206439Sml29623 			break;
48216439Sml29623 
48226789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
48236439Sml29623 			*(uint8_t *)pr_val = nxgep->param_en_10fdx;
48246439Sml29623 			break;
48256439Sml29623 
48266789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
48276789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
48286789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
48296789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
48306789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
48316789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
48326512Ssowmini 			err = ENOTSUP;
48336512Ssowmini 			break;
48346512Ssowmini 
48356789Sam223141 		case MAC_PROP_PRIVATE:
48366512Ssowmini 			err = nxge_get_priv_prop(nxgep, pr_name, pr_flags,
48376512Ssowmini 			    pr_valsize, pr_val);
48386512Ssowmini 			break;
48396512Ssowmini 		default:
48406439Sml29623 			err = EINVAL;
48416439Sml29623 			break;
48426439Sml29623 	}
48436439Sml29623 
48446439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_getprop"));
48456439Sml29623 
48466439Sml29623 	return (err);
48476439Sml29623 }
48486439Sml29623 
48496439Sml29623 /* ARGSUSED */
48506439Sml29623 static int
48516439Sml29623 nxge_set_priv_prop(p_nxge_t nxgep, const char *pr_name, uint_t pr_valsize,
48526439Sml29623     const void *pr_val)
48536439Sml29623 {
48546439Sml29623 	p_nxge_param_t	param_arr = nxgep->param_arr;
48556439Sml29623 	int		err = 0;
48566439Sml29623 	long		result;
48576439Sml29623 
48586439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48596439Sml29623 	    "==> nxge_set_priv_prop: name %s", pr_name));
48606439Sml29623 
48616439Sml29623 	if (strcmp(pr_name, "_accept_jumbo") == 0) {
48626439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
48636439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48646439Sml29623 		    "<== nxge_set_priv_prop: name %s "
48656439Sml29623 		    "pr_val %s result %d "
48666439Sml29623 		    "param %d is_jumbo %d",
48676439Sml29623 		    pr_name, pr_val, result,
48686439Sml29623 		    param_arr[param_accept_jumbo].value,
48696439Sml29623 		    nxgep->mac.is_jumbo));
48706439Sml29623 
48716439Sml29623 		if (result > 1 || result < 0) {
48726439Sml29623 			err = EINVAL;
48736439Sml29623 		} else {
48746439Sml29623 			if (nxgep->mac.is_jumbo ==
48756439Sml29623 			    (uint32_t)result) {
48766439Sml29623 				NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48776439Sml29623 				    "no change (%d %d)",
48786439Sml29623 				    nxgep->mac.is_jumbo,
48796439Sml29623 				    result));
48806439Sml29623 				return (0);
48816439Sml29623 			}
48826439Sml29623 		}
48836439Sml29623 
48846439Sml29623 		param_arr[param_accept_jumbo].value = result;
48856439Sml29623 		nxgep->mac.is_jumbo = B_FALSE;
48866439Sml29623 		if (result) {
48876439Sml29623 			nxgep->mac.is_jumbo = B_TRUE;
48886439Sml29623 		}
48896439Sml29623 
48906439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48916439Sml29623 		    "<== nxge_set_priv_prop: name %s (value %d) is_jumbo %d",
48926439Sml29623 		    pr_name, result, nxgep->mac.is_jumbo));
48936439Sml29623 
48946439Sml29623 		return (err);
48956439Sml29623 	}
48966439Sml29623 
48976439Sml29623 	/* Blanking */
48986439Sml29623 	if (strcmp(pr_name, "_rxdma_intr_time") == 0) {
48996439Sml29623 		err = nxge_param_rx_intr_time(nxgep, NULL, NULL,
49006439Sml29623 		    (char *)pr_val,
49016439Sml29623 		    (caddr_t)&param_arr[param_rxdma_intr_time]);
49026439Sml29623 		if (err) {
49036439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49046439Sml29623 			    "<== nxge_set_priv_prop: "
49056439Sml29623 			    "unable to set (%s)", pr_name));
49066439Sml29623 			err = EINVAL;
49076439Sml29623 		} else {
49086439Sml29623 			err = 0;
49096439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49106439Sml29623 			    "<== nxge_set_priv_prop: "
49116439Sml29623 			    "set (%s)", pr_name));
49126439Sml29623 		}
49136439Sml29623 
49146439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49156439Sml29623 		    "<== nxge_set_priv_prop: name %s (value %d)",
49166439Sml29623 		    pr_name, result));
49176439Sml29623 
49186439Sml29623 		return (err);
49196439Sml29623 	}
49206439Sml29623 
49216439Sml29623 	if (strcmp(pr_name, "_rxdma_intr_pkts") == 0) {
49226439Sml29623 		err = nxge_param_rx_intr_pkts(nxgep, NULL, NULL,
49236439Sml29623 		    (char *)pr_val,
49246439Sml29623 		    (caddr_t)&param_arr[param_rxdma_intr_pkts]);
49256439Sml29623 		if (err) {
49266439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49276439Sml29623 			    "<== nxge_set_priv_prop: "
49286439Sml29623 			    "unable to set (%s)", pr_name));
49296439Sml29623 			err = EINVAL;
49306439Sml29623 		} else {
49316439Sml29623 			err = 0;
49326439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49336439Sml29623 			    "<== nxge_set_priv_prop: "
49346439Sml29623 			    "set (%s)", pr_name));
49356439Sml29623 		}
49366439Sml29623 
49376439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49386439Sml29623 		    "<== nxge_set_priv_prop: name %s (value %d)",
49396439Sml29623 		    pr_name, result));
49406439Sml29623 
49416439Sml29623 		return (err);
49426439Sml29623 	}
49436439Sml29623 
49446439Sml29623 	/* Classification */
49456439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_tcp") == 0) {
49466439Sml29623 		if (pr_val == NULL) {
49476439Sml29623 			err = EINVAL;
49486439Sml29623 			return (err);
49496439Sml29623 		}
49506439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
49516439Sml29623 
49526439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
49536439Sml29623 		    NULL, (char *)pr_val,
49546439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_tcp]);
49556439Sml29623 
49566439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49576439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
49586439Sml29623 		    pr_name, result));
49596439Sml29623 
49606439Sml29623 		return (err);
49616439Sml29623 	}
49626439Sml29623 
49636439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_udp") == 0) {
49646439Sml29623 		if (pr_val == NULL) {
49656439Sml29623 			err = EINVAL;
49666439Sml29623 			return (err);
49676439Sml29623 		}
49686439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
49696439Sml29623 
49706439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
49716439Sml29623 		    NULL, (char *)pr_val,
49726439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_udp]);
49736439Sml29623 
49746439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49756439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
49766439Sml29623 		    pr_name, result));
49776439Sml29623 
49786439Sml29623 		return (err);
49796439Sml29623 	}
49806439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_ah") == 0) {
49816439Sml29623 		if (pr_val == NULL) {
49826439Sml29623 			err = EINVAL;
49836439Sml29623 			return (err);
49846439Sml29623 		}
49856439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
49866439Sml29623 
49876439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
49886439Sml29623 		    NULL, (char *)pr_val,
49896439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_ah]);
49906439Sml29623 
49916439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49926439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
49936439Sml29623 		    pr_name, result));
49946439Sml29623 
49956439Sml29623 		return (err);
49966439Sml29623 	}
49976439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_sctp") == 0) {
49986439Sml29623 		if (pr_val == NULL) {
49996439Sml29623 			err = EINVAL;
50006439Sml29623 			return (err);
50016439Sml29623 		}
50026439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
50036439Sml29623 
50046439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
50056439Sml29623 		    NULL, (char *)pr_val,
50066439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_sctp]);
50076439Sml29623 
50086439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50096439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
50106439Sml29623 		    pr_name, result));
50116439Sml29623 
50126439Sml29623 		return (err);
50136439Sml29623 	}
50146439Sml29623 
50156439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_tcp") == 0) {
50166439Sml29623 		if (pr_val == NULL) {
50176439Sml29623 			err = EINVAL;
50186439Sml29623 			return (err);
50196439Sml29623 		}
50206439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
50216439Sml29623 
50226439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
50236439Sml29623 		    NULL, (char *)pr_val,
50246439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_tcp]);
50256439Sml29623 
50266439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50276439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
50286439Sml29623 		    pr_name, result));
50296439Sml29623 
50306439Sml29623 		return (err);
50316439Sml29623 	}
50326439Sml29623 
50336439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_udp") == 0) {
50346439Sml29623 		if (pr_val == NULL) {
50356439Sml29623 			err = EINVAL;
50366439Sml29623 			return (err);
50376439Sml29623 		}
50386439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
50396439Sml29623 
50406439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
50416439Sml29623 		    NULL, (char *)pr_val,
50426439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_udp]);
50436439Sml29623 
50446439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50456439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
50466439Sml29623 		    pr_name, result));
50476439Sml29623 
50486439Sml29623 		return (err);
50496439Sml29623 	}
50506439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_ah") == 0) {
50516439Sml29623 		if (pr_val == NULL) {
50526439Sml29623 			err = EINVAL;
50536439Sml29623 			return (err);
50546439Sml29623 		}
50556439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
50566439Sml29623 
50576439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
50586439Sml29623 		    NULL, (char *)pr_val,
50596439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_ah]);
50606439Sml29623 
50616439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50626439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
50636439Sml29623 		    pr_name, result));
50646439Sml29623 
50656439Sml29623 		return (err);
50666439Sml29623 	}
50676439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_sctp") == 0) {
50686439Sml29623 		if (pr_val == NULL) {
50696439Sml29623 			err = EINVAL;
50706439Sml29623 			return (err);
50716439Sml29623 		}
50726439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
50736439Sml29623 
50746439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
50756439Sml29623 		    NULL, (char *)pr_val,
50766439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_sctp]);
50776439Sml29623 
50786439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50796439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
50806439Sml29623 		    pr_name, result));
50816439Sml29623 
50826439Sml29623 		return (err);
50836439Sml29623 	}
50846439Sml29623 
50856439Sml29623 	if (strcmp(pr_name, "_soft_lso_enable") == 0) {
50866439Sml29623 		if (nxgep->nxge_mac_state == NXGE_MAC_STARTED) {
50876439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50886439Sml29623 			    "==> nxge_set_priv_prop: name %s (busy)", pr_name));
50896439Sml29623 			err = EBUSY;
50906439Sml29623 			return (err);
50916439Sml29623 		}
50926439Sml29623 		if (pr_val == NULL) {
50936439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50946439Sml29623 			    "==> nxge_set_priv_prop: name %s (null)", pr_name));
50956439Sml29623 			err = EINVAL;
50966439Sml29623 			return (err);
50976439Sml29623 		}
50986439Sml29623 
50996439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
51006439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51016439Sml29623 		    "<== nxge_set_priv_prop: name %s "
51026439Sml29623 		    "(lso %d pr_val %s value %d)",
51036439Sml29623 		    pr_name, nxgep->soft_lso_enable, pr_val, result));
51046439Sml29623 
51056439Sml29623 		if (result > 1 || result < 0) {
51066439Sml29623 			err = EINVAL;
51076439Sml29623 		} else {
51086439Sml29623 			if (nxgep->soft_lso_enable == (uint32_t)result) {
51096439Sml29623 				NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51106439Sml29623 				    "no change (%d %d)",
51116439Sml29623 				    nxgep->soft_lso_enable, result));
51126439Sml29623 				return (0);
51136439Sml29623 			}
51146439Sml29623 		}
51156439Sml29623 
51166439Sml29623 		nxgep->soft_lso_enable = (int)result;
51176439Sml29623 
51186439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51196439Sml29623 		    "<== nxge_set_priv_prop: name %s (value %d)",
51206439Sml29623 		    pr_name, result));
51216439Sml29623 
51226439Sml29623 		return (err);
51236439Sml29623 	}
51246835Syc148097 	/*
51256835Syc148097 	 * Commands like "ndd -set /dev/nxge0 adv_10gfdx_cap 1" cause the
51266835Syc148097 	 * following code to be executed.
51276835Syc148097 	 */
51286512Ssowmini 	if (strcmp(pr_name, "_adv_10gfdx_cap") == 0) {
51296512Ssowmini 		err = nxge_param_set_mac(nxgep, NULL, NULL, (char *)pr_val,
51306512Ssowmini 		    (caddr_t)&param_arr[param_anar_10gfdx]);
51316512Ssowmini 		return (err);
51326512Ssowmini 	}
51336512Ssowmini 	if (strcmp(pr_name, "_adv_pause_cap") == 0) {
51346512Ssowmini 		err = nxge_param_set_mac(nxgep, NULL, NULL, (char *)pr_val,
51356512Ssowmini 		    (caddr_t)&param_arr[param_anar_pause]);
51366512Ssowmini 		return (err);
51376512Ssowmini 	}
51386439Sml29623 
51396439Sml29623 	return (EINVAL);
51406439Sml29623 }
51416439Sml29623 
51426439Sml29623 static int
51436512Ssowmini nxge_get_priv_prop(p_nxge_t nxgep, const char *pr_name, uint_t pr_flags,
51446512Ssowmini     uint_t pr_valsize, void *pr_val)
51456439Sml29623 {
51466439Sml29623 	p_nxge_param_t	param_arr = nxgep->param_arr;
51476439Sml29623 	char		valstr[MAXNAMELEN];
51486439Sml29623 	int		err = EINVAL;
51496439Sml29623 	uint_t		strsize;
51506789Sam223141 	boolean_t	is_default = (pr_flags & MAC_PROP_DEFAULT);
51516439Sml29623 
51526439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51536439Sml29623 	    "==> nxge_get_priv_prop: property %s", pr_name));
51546439Sml29623 
51556439Sml29623 	/* function number */
51566439Sml29623 	if (strcmp(pr_name, "_function_number") == 0) {
51576512Ssowmini 		if (is_default)
51586512Ssowmini 			return (ENOTSUP);
51596512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%d",
51606512Ssowmini 		    nxgep->function_num);
51616439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51626439Sml29623 		    "==> nxge_get_priv_prop: name %s "
51636439Sml29623 		    "(value %d valstr %s)",
51646439Sml29623 		    pr_name, nxgep->function_num, valstr));
51656439Sml29623 
51666439Sml29623 		err = 0;
51676439Sml29623 		goto done;
51686439Sml29623 	}
51696439Sml29623 
51706439Sml29623 	/* Neptune firmware version */
51716439Sml29623 	if (strcmp(pr_name, "_fw_version") == 0) {
51726512Ssowmini 		if (is_default)
51736512Ssowmini 			return (ENOTSUP);
51746512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%s",
51756512Ssowmini 		    nxgep->vpd_info.ver);
51766439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51776439Sml29623 		    "==> nxge_get_priv_prop: name %s "
51786439Sml29623 		    "(value %d valstr %s)",
51796439Sml29623 		    pr_name, nxgep->vpd_info.ver, valstr));
51806439Sml29623 
51816439Sml29623 		err = 0;
51826439Sml29623 		goto done;
51836439Sml29623 	}
51846439Sml29623 
51856439Sml29623 	/* port PHY mode */
51866439Sml29623 	if (strcmp(pr_name, "_port_mode") == 0) {
51876512Ssowmini 		if (is_default)
51886512Ssowmini 			return (ENOTSUP);
51896439Sml29623 		switch (nxgep->mac.portmode) {
51906439Sml29623 		case PORT_1G_COPPER:
51916512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "1G copper %s",
51926439Sml29623 			    nxgep->hot_swappable_phy ?
51936439Sml29623 			    "[Hot Swappable]" : "");
51946439Sml29623 			break;
51956439Sml29623 		case PORT_1G_FIBER:
51966512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "1G fiber %s",
51976439Sml29623 			    nxgep->hot_swappable_phy ?
51986439Sml29623 			    "[hot swappable]" : "");
51996439Sml29623 			break;
52006439Sml29623 		case PORT_10G_COPPER:
52016512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
52026512Ssowmini 			    "10G copper %s",
52036439Sml29623 			    nxgep->hot_swappable_phy ?
52046439Sml29623 			    "[hot swappable]" : "");
52056439Sml29623 			break;
52066439Sml29623 		case PORT_10G_FIBER:
52076512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "10G fiber %s",
52086439Sml29623 			    nxgep->hot_swappable_phy ?
52096439Sml29623 			    "[hot swappable]" : "");
52106439Sml29623 			break;
52116439Sml29623 		case PORT_10G_SERDES:
52126512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
52136512Ssowmini 			    "10G serdes %s", nxgep->hot_swappable_phy ?
52146439Sml29623 			    "[hot swappable]" : "");
52156439Sml29623 			break;
52166439Sml29623 		case PORT_1G_SERDES:
52176512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "1G serdes %s",
52186439Sml29623 			    nxgep->hot_swappable_phy ?
52196439Sml29623 			    "[hot swappable]" : "");
52206439Sml29623 			break;
52216835Syc148097 		case PORT_1G_TN1010:
52226835Syc148097 			(void) snprintf(valstr, sizeof (valstr),
52236835Syc148097 			    "1G TN1010 copper %s", nxgep->hot_swappable_phy ?
52246835Syc148097 			    "[hot swappable]" : "");
52256835Syc148097 			break;
52266835Syc148097 		case PORT_10G_TN1010:
52276835Syc148097 			(void) snprintf(valstr, sizeof (valstr),
52286835Syc148097 			    "10G TN1010 copper %s", nxgep->hot_swappable_phy ?
52296835Syc148097 			    "[hot swappable]" : "");
52306835Syc148097 			break;
52316439Sml29623 		case PORT_1G_RGMII_FIBER:
52326512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
52336512Ssowmini 			    "1G rgmii fiber %s", nxgep->hot_swappable_phy ?
52346439Sml29623 			    "[hot swappable]" : "");
52356439Sml29623 			break;
52366439Sml29623 		case PORT_HSP_MODE:
52376512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
52386444Sml29623 			    "phy not present[hot swappable]");
52396439Sml29623 			break;
52406439Sml29623 		default:
52416512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "unknown %s",
52426439Sml29623 			    nxgep->hot_swappable_phy ?
52436439Sml29623 			    "[hot swappable]" : "");
52446439Sml29623 			break;
52456439Sml29623 		}
52466439Sml29623 
52476439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
52486439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %s)",
52496439Sml29623 		    pr_name, valstr));
52506439Sml29623 
52516439Sml29623 		err = 0;
52526439Sml29623 		goto done;
52536439Sml29623 	}
52546439Sml29623 
52556439Sml29623 	/* Hot swappable PHY */
52566439Sml29623 	if (strcmp(pr_name, "_hot_swap_phy") == 0) {
52576512Ssowmini 		if (is_default)
52586512Ssowmini 			return (ENOTSUP);
52596512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%s",
52606439Sml29623 		    nxgep->hot_swappable_phy ?
52616439Sml29623 		    "yes" : "no");
52626439Sml29623 
52636439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
52646439Sml29623 		    "==> nxge_get_priv_prop: name %s "
52656439Sml29623 		    "(value %d valstr %s)",
52666439Sml29623 		    pr_name, nxgep->hot_swappable_phy, valstr));
52676439Sml29623 
52686439Sml29623 		err = 0;
52696439Sml29623 		goto done;
52706439Sml29623 	}
52716439Sml29623 
52726439Sml29623 
52736439Sml29623 	/* accept jumbo */
52746439Sml29623 	if (strcmp(pr_name, "_accept_jumbo") == 0) {
52756512Ssowmini 		if (is_default)
52766512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),  "%d", 0);
52776512Ssowmini 		else
52786512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
52796512Ssowmini 			    "%d", nxgep->mac.is_jumbo);
52806439Sml29623 		err = 0;
52816439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
52826439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %d (%d, %d))",
52836439Sml29623 		    pr_name,
52846439Sml29623 		    (uint32_t)param_arr[param_accept_jumbo].value,
52856439Sml29623 		    nxgep->mac.is_jumbo,
52866439Sml29623 		    nxge_jumbo_enable));
52876439Sml29623 
52886439Sml29623 		goto done;
52896439Sml29623 	}
52906439Sml29623 
52916439Sml29623 	/* Receive Interrupt Blanking Parameters */
52926439Sml29623 	if (strcmp(pr_name, "_rxdma_intr_time") == 0) {
52936512Ssowmini 		err = 0;
52946512Ssowmini 		if (is_default) {
52956512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
52966512Ssowmini 			    "%d", RXDMA_RCR_TO_DEFAULT);
52976512Ssowmini 			goto done;
52986512Ssowmini 		}
52996512Ssowmini 
53006512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%d",
53016512Ssowmini 		    nxgep->intr_timeout);
53026439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53036439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %d)",
53046439Sml29623 		    pr_name,
53056439Sml29623 		    (uint32_t)nxgep->intr_timeout));
53066439Sml29623 		goto done;
53076439Sml29623 	}
53086439Sml29623 
53096439Sml29623 	if (strcmp(pr_name, "_rxdma_intr_pkts") == 0) {
53106512Ssowmini 		err = 0;
53116512Ssowmini 		if (is_default) {
53126512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
53136512Ssowmini 			    "%d", RXDMA_RCR_PTHRES_DEFAULT);
53146512Ssowmini 			goto done;
53156512Ssowmini 		}
53166512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%d",
53176512Ssowmini 		    nxgep->intr_threshold);
53186439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53196439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %d)",
53206439Sml29623 		    pr_name, (uint32_t)nxgep->intr_threshold));
53216439Sml29623 
53226439Sml29623 		goto done;
53236439Sml29623 	}
53246439Sml29623 
53256439Sml29623 	/* Classification and Load Distribution Configuration */
53266439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_tcp") == 0) {
53276512Ssowmini 		if (is_default) {
53286512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
53296512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
53306512Ssowmini 			err = 0;
53316512Ssowmini 			goto done;
53326512Ssowmini 		}
53336439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
53346439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_tcp]);
53356439Sml29623 
53366512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
53376439Sml29623 		    (int)param_arr[param_class_opt_ipv4_tcp].value);
53386439Sml29623 
53396439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53406439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
53416439Sml29623 		goto done;
53426439Sml29623 	}
53436439Sml29623 
53446439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_udp") == 0) {
53456512Ssowmini 		if (is_default) {
53466512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
53476512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
53486512Ssowmini 			err = 0;
53496512Ssowmini 			goto done;
53506512Ssowmini 		}
53516439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
53526439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_udp]);
53536439Sml29623 
53546512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
53556439Sml29623 		    (int)param_arr[param_class_opt_ipv4_udp].value);
53566439Sml29623 
53576439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53586439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
53596439Sml29623 		goto done;
53606439Sml29623 	}
53616439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_ah") == 0) {
53626512Ssowmini 		if (is_default) {
53636512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
53646512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
53656512Ssowmini 			err = 0;
53666512Ssowmini 			goto done;
53676512Ssowmini 		}
53686439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
53696439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_ah]);
53706439Sml29623 
53716512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
53726439Sml29623 		    (int)param_arr[param_class_opt_ipv4_ah].value);
53736439Sml29623 
53746439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53756439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
53766439Sml29623 		goto done;
53776439Sml29623 	}
53786439Sml29623 
53796439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_sctp") == 0) {
53806512Ssowmini 		if (is_default) {
53816512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
53826512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
53836512Ssowmini 			err = 0;
53846512Ssowmini 			goto done;
53856512Ssowmini 		}
53866439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
53876439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_sctp]);
53886439Sml29623 
53896512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
53906439Sml29623 		    (int)param_arr[param_class_opt_ipv4_sctp].value);
53916439Sml29623 
53926439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53936439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
53946439Sml29623 		goto done;
53956439Sml29623 	}
53966439Sml29623 
53976439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_tcp") == 0) {
53986512Ssowmini 		if (is_default) {
53996512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
54006512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
54016512Ssowmini 			err = 0;
54026512Ssowmini 			goto done;
54036512Ssowmini 		}
54046439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
54056439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_tcp]);
54066439Sml29623 
54076512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
54086439Sml29623 		    (int)param_arr[param_class_opt_ipv6_tcp].value);
54096439Sml29623 
54106439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
54116439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
54126439Sml29623 		goto done;
54136439Sml29623 	}
54146439Sml29623 
54156439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_udp") == 0) {
54166512Ssowmini 		if (is_default) {
54176512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
54186512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
54196512Ssowmini 			err = 0;
54206512Ssowmini 			goto done;
54216512Ssowmini 		}
54226439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
54236439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_udp]);
54246439Sml29623 
54256512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
54266439Sml29623 		    (int)param_arr[param_class_opt_ipv6_udp].value);
54276439Sml29623 
54286439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
54296439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
54306439Sml29623 		goto done;
54316439Sml29623 	}
54326439Sml29623 
54336439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_ah") == 0) {
54346512Ssowmini 		if (is_default) {
54356512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
54366512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
54376512Ssowmini 			err = 0;
54386512Ssowmini 			goto done;
54396512Ssowmini 		}
54406439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
54416439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_ah]);
54426439Sml29623 
54436512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
54446439Sml29623 		    (int)param_arr[param_class_opt_ipv6_ah].value);
54456439Sml29623 
54466439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
54476439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
54486439Sml29623 		goto done;
54496439Sml29623 	}
54506439Sml29623 
54516439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_sctp") == 0) {
54526512Ssowmini 		if (is_default) {
54536512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
54546512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
54556512Ssowmini 			err = 0;
54566512Ssowmini 			goto done;
54576512Ssowmini 		}
54586439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
54596439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_sctp]);
54606439Sml29623 
54616512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
54626439Sml29623 		    (int)param_arr[param_class_opt_ipv6_sctp].value);
54636439Sml29623 
54646439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
54656439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
54666439Sml29623 		goto done;
54676439Sml29623 	}
54686439Sml29623 
54696439Sml29623 	/* Software LSO */
54706439Sml29623 	if (strcmp(pr_name, "_soft_lso_enable") == 0) {
54716512Ssowmini 		if (is_default) {
54726512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 0);
54736512Ssowmini 			err = 0;
54746512Ssowmini 			goto done;
54756512Ssowmini 		}
54766512Ssowmini 		(void) snprintf(valstr, sizeof (valstr),
54776512Ssowmini 		    "%d", nxgep->soft_lso_enable);
54786439Sml29623 		err = 0;
54796439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
54806439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %d)",
54816439Sml29623 		    pr_name, nxgep->soft_lso_enable));
54826439Sml29623 
54836439Sml29623 		goto done;
54846439Sml29623 	}
54856512Ssowmini 	if (strcmp(pr_name, "_adv_10gfdx_cap") == 0) {
54866512Ssowmini 		err = 0;
54876512Ssowmini 		if (is_default ||
54886512Ssowmini 		    nxgep->param_arr[param_anar_10gfdx].value != 0) {
54896512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 1);
54906512Ssowmini 			goto done;
54916512Ssowmini 		} else {
54926512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 0);
54936512Ssowmini 			goto done;
54946512Ssowmini 		}
54956512Ssowmini 	}
54966512Ssowmini 	if (strcmp(pr_name, "_adv_pause_cap") == 0) {
54976512Ssowmini 		err = 0;
54986512Ssowmini 		if (is_default ||
54996512Ssowmini 		    nxgep->param_arr[param_anar_pause].value != 0) {
55006512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 1);
55016512Ssowmini 			goto done;
55026512Ssowmini 		} else {
55036512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 0);
55046512Ssowmini 			goto done;
55056512Ssowmini 		}
55066512Ssowmini 	}
55076439Sml29623 
55086439Sml29623 done:
55096439Sml29623 	if (err == 0) {
55106439Sml29623 		strsize = (uint_t)strlen(valstr);
55116439Sml29623 		if (pr_valsize < strsize) {
55126439Sml29623 			err = ENOBUFS;
55136439Sml29623 		} else {
55146439Sml29623 			(void) strlcpy(pr_val, valstr, pr_valsize);
55156439Sml29623 		}
55166439Sml29623 	}
55176439Sml29623 
55186439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
55196439Sml29623 	    "<== nxge_get_priv_prop: return %d", err));
55206439Sml29623 	return (err);
55216439Sml29623 }
55226439Sml29623 
55233859Sml29623 /*
55243859Sml29623  * Module loading and removing entry points.
55253859Sml29623  */
55263859Sml29623 
55276705Sml29623 DDI_DEFINE_STREAM_OPS(nxge_dev_ops, nulldev, nulldev, nxge_attach, nxge_detach,
55286705Sml29623     nodev, NULL, D_MP, NULL);
55293859Sml29623 
55304977Sraghus #define	NXGE_DESC_VER		"Sun NIU 10Gb Ethernet"
55313859Sml29623 
55323859Sml29623 /*
55333859Sml29623  * Module linkage information for the kernel.
55343859Sml29623  */
55353859Sml29623 static struct modldrv 	nxge_modldrv = {
55363859Sml29623 	&mod_driverops,
55373859Sml29623 	NXGE_DESC_VER,
55383859Sml29623 	&nxge_dev_ops
55393859Sml29623 };
55403859Sml29623 
55413859Sml29623 static struct modlinkage modlinkage = {
55423859Sml29623 	MODREV_1, (void *) &nxge_modldrv, NULL
55433859Sml29623 };
55443859Sml29623 
55453859Sml29623 int
55463859Sml29623 _init(void)
55473859Sml29623 {
55483859Sml29623 	int		status;
55493859Sml29623 
55503859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "==> _init"));
55513859Sml29623 	mac_init_ops(&nxge_dev_ops, "nxge");
55523859Sml29623 	status = ddi_soft_state_init(&nxge_list, sizeof (nxge_t), 0);
55533859Sml29623 	if (status != 0) {
55543859Sml29623 		NXGE_ERROR_MSG((NULL, NXGE_ERR_CTL,
55556512Ssowmini 		    "failed to init device soft state"));
55563859Sml29623 		goto _init_exit;
55573859Sml29623 	}
55583859Sml29623 	status = mod_install(&modlinkage);
55593859Sml29623 	if (status != 0) {
55603859Sml29623 		ddi_soft_state_fini(&nxge_list);
55613859Sml29623 		NXGE_ERROR_MSG((NULL, NXGE_ERR_CTL, "Mod install failed"));
55623859Sml29623 		goto _init_exit;
55633859Sml29623 	}
55643859Sml29623 
55653859Sml29623 	MUTEX_INIT(&nxge_common_lock, NULL, MUTEX_DRIVER, NULL);
55663859Sml29623 
55673859Sml29623 _init_exit:
55683859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "_init status = 0x%X", status));
55693859Sml29623 
55703859Sml29623 	return (status);
55713859Sml29623 }
55723859Sml29623 
55733859Sml29623 int
55743859Sml29623 _fini(void)
55753859Sml29623 {
55763859Sml29623 	int		status;
55773859Sml29623 
55783859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "==> _fini"));
55793859Sml29623 
55803859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "==> _fini: mod_remove"));
55813859Sml29623 
55823859Sml29623 	if (nxge_mblks_pending)
55833859Sml29623 		return (EBUSY);
55843859Sml29623 
55853859Sml29623 	status = mod_remove(&modlinkage);
55863859Sml29623 	if (status != DDI_SUCCESS) {
55873859Sml29623 		NXGE_DEBUG_MSG((NULL, MOD_CTL,
55886512Ssowmini 		    "Module removal failed 0x%08x",
55896512Ssowmini 		    status));
55903859Sml29623 		goto _fini_exit;
55913859Sml29623 	}
55923859Sml29623 
55933859Sml29623 	mac_fini_ops(&nxge_dev_ops);
55943859Sml29623 
55953859Sml29623 	ddi_soft_state_fini(&nxge_list);
55963859Sml29623 
55973859Sml29623 	MUTEX_DESTROY(&nxge_common_lock);
55983859Sml29623 _fini_exit:
55993859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "_fini status = 0x%08x", status));
56003859Sml29623 
56013859Sml29623 	return (status);
56023859Sml29623 }
56033859Sml29623 
56043859Sml29623 int
56053859Sml29623 _info(struct modinfo *modinfop)
56063859Sml29623 {
56073859Sml29623 	int		status;
56083859Sml29623 
56093859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "==> _info"));
56103859Sml29623 	status = mod_info(&modlinkage, modinfop);
56113859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, " _info status = 0x%X", status));
56123859Sml29623 
56133859Sml29623 	return (status);
56143859Sml29623 }
56153859Sml29623 
56163859Sml29623 /*ARGSUSED*/
56173859Sml29623 static nxge_status_t
56183859Sml29623 nxge_add_intrs(p_nxge_t nxgep)
56193859Sml29623 {
56203859Sml29623 
56213859Sml29623 	int		intr_types;
56223859Sml29623 	int		type = 0;
56233859Sml29623 	int		ddi_status = DDI_SUCCESS;
56243859Sml29623 	nxge_status_t	status = NXGE_OK;
56253859Sml29623 
56263859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs"));
56273859Sml29623 
56283859Sml29623 	nxgep->nxge_intr_type.intr_registered = B_FALSE;
56293859Sml29623 	nxgep->nxge_intr_type.intr_enabled = B_FALSE;
56303859Sml29623 	nxgep->nxge_intr_type.msi_intx_cnt = 0;
56313859Sml29623 	nxgep->nxge_intr_type.intr_added = 0;
56323859Sml29623 	nxgep->nxge_intr_type.niu_msi_enable = B_FALSE;
56333859Sml29623 	nxgep->nxge_intr_type.intr_type = 0;
56343859Sml29623 
56353859Sml29623 	if (nxgep->niu_type == N2_NIU) {
56363859Sml29623 		nxgep->nxge_intr_type.niu_msi_enable = B_TRUE;
56373859Sml29623 	} else if (nxge_msi_enable) {
56383859Sml29623 		nxgep->nxge_intr_type.niu_msi_enable = B_TRUE;
56393859Sml29623 	}
56403859Sml29623 
56413859Sml29623 	/* Get the supported interrupt types */
56423859Sml29623 	if ((ddi_status = ddi_intr_get_supported_types(nxgep->dip, &intr_types))
56436512Ssowmini 	    != DDI_SUCCESS) {
56443859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "<== nxge_add_intrs: "
56456512Ssowmini 		    "ddi_intr_get_supported_types failed: status 0x%08x",
56466512Ssowmini 		    ddi_status));
56473859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
56483859Sml29623 	}
56493859Sml29623 	nxgep->nxge_intr_type.intr_types = intr_types;
56503859Sml29623 
56513859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
56526512Ssowmini 	    "ddi_intr_get_supported_types: 0x%08x", intr_types));
56533859Sml29623 
56543859Sml29623 	/*
56553859Sml29623 	 * Solaris MSIX is not supported yet. use MSI for now.
56563859Sml29623 	 * nxge_msi_enable (1):
56573859Sml29623 	 *	1 - MSI		2 - MSI-X	others - FIXED
56583859Sml29623 	 */
56593859Sml29623 	switch (nxge_msi_enable) {
56603859Sml29623 	default:
56613859Sml29623 		type = DDI_INTR_TYPE_FIXED;
56623859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs: "
56636512Ssowmini 		    "use fixed (intx emulation) type %08x",
56646512Ssowmini 		    type));
56653859Sml29623 		break;
56663859Sml29623 
56673859Sml29623 	case 2:
56683859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs: "
56696512Ssowmini 		    "ddi_intr_get_supported_types: 0x%08x", intr_types));
56703859Sml29623 		if (intr_types & DDI_INTR_TYPE_MSIX) {
56713859Sml29623 			type = DDI_INTR_TYPE_MSIX;
56723859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
56736512Ssowmini 			    "ddi_intr_get_supported_types: MSIX 0x%08x",
56746512Ssowmini 			    type));
56753859Sml29623 		} else if (intr_types & DDI_INTR_TYPE_MSI) {
56763859Sml29623 			type = DDI_INTR_TYPE_MSI;
56773859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
56786512Ssowmini 			    "ddi_intr_get_supported_types: MSI 0x%08x",
56796512Ssowmini 			    type));
56803859Sml29623 		} else if (intr_types & DDI_INTR_TYPE_FIXED) {
56813859Sml29623 			type = DDI_INTR_TYPE_FIXED;
56823859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs: "
56836512Ssowmini 			    "ddi_intr_get_supported_types: MSXED0x%08x",
56846512Ssowmini 			    type));
56853859Sml29623 		}
56863859Sml29623 		break;
56873859Sml29623 
56883859Sml29623 	case 1:
56893859Sml29623 		if (intr_types & DDI_INTR_TYPE_MSI) {
56903859Sml29623 			type = DDI_INTR_TYPE_MSI;
56913859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs: "
56926512Ssowmini 			    "ddi_intr_get_supported_types: MSI 0x%08x",
56936512Ssowmini 			    type));
56943859Sml29623 		} else if (intr_types & DDI_INTR_TYPE_MSIX) {
56953859Sml29623 			type = DDI_INTR_TYPE_MSIX;
56963859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
56976512Ssowmini 			    "ddi_intr_get_supported_types: MSIX 0x%08x",
56986512Ssowmini 			    type));
56993859Sml29623 		} else if (intr_types & DDI_INTR_TYPE_FIXED) {
57003859Sml29623 			type = DDI_INTR_TYPE_FIXED;
57013859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
57026512Ssowmini 			    "ddi_intr_get_supported_types: MSXED0x%08x",
57036512Ssowmini 			    type));
57043859Sml29623 		}
57053859Sml29623 	}
57063859Sml29623 
57073859Sml29623 	nxgep->nxge_intr_type.intr_type = type;
57083859Sml29623 	if ((type == DDI_INTR_TYPE_MSIX || type == DDI_INTR_TYPE_MSI ||
57096512Ssowmini 	    type == DDI_INTR_TYPE_FIXED) &&
57106512Ssowmini 	    nxgep->nxge_intr_type.niu_msi_enable) {
57113859Sml29623 		if ((status = nxge_add_intrs_adv(nxgep)) != DDI_SUCCESS) {
57123859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
57136512Ssowmini 			    " nxge_add_intrs: "
57146512Ssowmini 			    " nxge_add_intrs_adv failed: status 0x%08x",
57156512Ssowmini 			    status));
57163859Sml29623 			return (status);
57173859Sml29623 		} else {
57183859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
57196512Ssowmini 			    "interrupts registered : type %d", type));
57203859Sml29623 			nxgep->nxge_intr_type.intr_registered = B_TRUE;
57213859Sml29623 
57223859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
57236512Ssowmini 			    "\nAdded advanced nxge add_intr_adv "
57246512Ssowmini 			    "intr type 0x%x\n", type));
57253859Sml29623 
57263859Sml29623 			return (status);
57273859Sml29623 		}
57283859Sml29623 	}
57293859Sml29623 
57303859Sml29623 	if (!nxgep->nxge_intr_type.intr_registered) {
57313859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "==> nxge_add_intrs: "
57326512Ssowmini 		    "failed to register interrupts"));
57333859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
57343859Sml29623 	}
57353859Sml29623 
57363859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_add_intrs"));
57373859Sml29623 	return (status);
57383859Sml29623 }
57393859Sml29623 
57403859Sml29623 /*ARGSUSED*/
57413859Sml29623 static nxge_status_t
57423859Sml29623 nxge_add_soft_intrs(p_nxge_t nxgep)
57433859Sml29623 {
57443859Sml29623 
57453859Sml29623 	int		ddi_status = DDI_SUCCESS;
57463859Sml29623 	nxge_status_t	status = NXGE_OK;
57473859Sml29623 
57483859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_soft_intrs"));
57493859Sml29623 
57503859Sml29623 	nxgep->resched_id = NULL;
57513859Sml29623 	nxgep->resched_running = B_FALSE;
57523859Sml29623 	ddi_status = ddi_add_softintr(nxgep->dip, DDI_SOFTINT_LOW,
57536512Ssowmini 	    &nxgep->resched_id,
57546512Ssowmini 	    NULL, NULL, nxge_reschedule, (caddr_t)nxgep);
57553859Sml29623 	if (ddi_status != DDI_SUCCESS) {
57563859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "<== nxge_add_soft_intrs: "
57576512Ssowmini 		    "ddi_add_softintrs failed: status 0x%08x",
57586512Ssowmini 		    ddi_status));
57593859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
57603859Sml29623 	}
57613859Sml29623 
57623859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_ddi_add_soft_intrs"));
57633859Sml29623 
57643859Sml29623 	return (status);
57653859Sml29623 }
57663859Sml29623 
57673859Sml29623 static nxge_status_t
57683859Sml29623 nxge_add_intrs_adv(p_nxge_t nxgep)
57693859Sml29623 {
57703859Sml29623 	int		intr_type;
57713859Sml29623 	p_nxge_intr_t	intrp;
57723859Sml29623 
57733859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs_adv"));
57743859Sml29623 
57753859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
57763859Sml29623 	intr_type = intrp->intr_type;
57773859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs_adv: type 0x%x",
57786512Ssowmini 	    intr_type));
57793859Sml29623 
57803859Sml29623 	switch (intr_type) {
57813859Sml29623 	case DDI_INTR_TYPE_MSI: /* 0x2 */
57823859Sml29623 	case DDI_INTR_TYPE_MSIX: /* 0x4 */
57833859Sml29623 		return (nxge_add_intrs_adv_type(nxgep, intr_type));
57843859Sml29623 
57853859Sml29623 	case DDI_INTR_TYPE_FIXED: /* 0x1 */
57863859Sml29623 		return (nxge_add_intrs_adv_type_fix(nxgep, intr_type));
57873859Sml29623 
57883859Sml29623 	default:
57893859Sml29623 		return (NXGE_ERROR);
57903859Sml29623 	}
57913859Sml29623 }
57923859Sml29623 
57933859Sml29623 
57943859Sml29623 /*ARGSUSED*/
57953859Sml29623 static nxge_status_t
57963859Sml29623 nxge_add_intrs_adv_type(p_nxge_t nxgep, uint32_t int_type)
57973859Sml29623 {
57983859Sml29623 	dev_info_t		*dip = nxgep->dip;
57993859Sml29623 	p_nxge_ldg_t		ldgp;
58003859Sml29623 	p_nxge_intr_t		intrp;
58013859Sml29623 	uint_t			*inthandler;
58023859Sml29623 	void			*arg1, *arg2;
58033859Sml29623 	int			behavior;
58045013Sml29623 	int			nintrs, navail, nrequest;
58053859Sml29623 	int			nactual, nrequired;
58063859Sml29623 	int			inum = 0;
58073859Sml29623 	int			x, y;
58083859Sml29623 	int			ddi_status = DDI_SUCCESS;
58093859Sml29623 	nxge_status_t		status = NXGE_OK;
58103859Sml29623 
58113859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs_adv_type"));
58123859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
58133859Sml29623 	intrp->start_inum = 0;
58143859Sml29623 
58153859Sml29623 	ddi_status = ddi_intr_get_nintrs(dip, int_type, &nintrs);
58163859Sml29623 	if ((ddi_status != DDI_SUCCESS) || (nintrs == 0)) {
58173859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
58186512Ssowmini 		    "ddi_intr_get_nintrs() failed, status: 0x%x%, "
58196512Ssowmini 		    "nintrs: %d", ddi_status, nintrs));
58203859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
58213859Sml29623 	}
58223859Sml29623 
58233859Sml29623 	ddi_status = ddi_intr_get_navail(dip, int_type, &navail);
58243859Sml29623 	if ((ddi_status != DDI_SUCCESS) || (navail == 0)) {
58253859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
58266512Ssowmini 		    "ddi_intr_get_navail() failed, status: 0x%x%, "
58276512Ssowmini 		    "nintrs: %d", ddi_status, navail));
58283859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
58293859Sml29623 	}
58303859Sml29623 
58313859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL,
58326512Ssowmini 	    "ddi_intr_get_navail() returned: nintrs %d, navail %d",
58336512Ssowmini 	    nintrs, navail));
58343859Sml29623 
58355013Sml29623 	/* PSARC/2007/453 MSI-X interrupt limit override */
58365013Sml29623 	if (int_type == DDI_INTR_TYPE_MSIX) {
58375013Sml29623 		nrequest = nxge_create_msi_property(nxgep);
58385013Sml29623 		if (nrequest < navail) {
58395013Sml29623 			navail = nrequest;
58405013Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
58415013Sml29623 			    "nxge_add_intrs_adv_type: nintrs %d "
58425013Sml29623 			    "navail %d (nrequest %d)",
58435013Sml29623 			    nintrs, navail, nrequest));
58445013Sml29623 		}
58455013Sml29623 	}
58465013Sml29623 
58473859Sml29623 	if (int_type == DDI_INTR_TYPE_MSI && !ISP2(navail)) {
58483859Sml29623 		/* MSI must be power of 2 */
58493859Sml29623 		if ((navail & 16) == 16) {
58503859Sml29623 			navail = 16;
58513859Sml29623 		} else if ((navail & 8) == 8) {
58523859Sml29623 			navail = 8;
58533859Sml29623 		} else if ((navail & 4) == 4) {
58543859Sml29623 			navail = 4;
58553859Sml29623 		} else if ((navail & 2) == 2) {
58563859Sml29623 			navail = 2;
58573859Sml29623 		} else {
58583859Sml29623 			navail = 1;
58593859Sml29623 		}
58603859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
58616512Ssowmini 		    "ddi_intr_get_navail(): (msi power of 2) nintrs %d, "
58626512Ssowmini 		    "navail %d", nintrs, navail));
58633859Sml29623 	}
58643859Sml29623 
58653859Sml29623 	behavior = ((int_type == DDI_INTR_TYPE_FIXED) ? DDI_INTR_ALLOC_STRICT :
58666512Ssowmini 	    DDI_INTR_ALLOC_NORMAL);
58673859Sml29623 	intrp->intr_size = navail * sizeof (ddi_intr_handle_t);
58683859Sml29623 	intrp->htable = kmem_alloc(intrp->intr_size, KM_SLEEP);
58693859Sml29623 	ddi_status = ddi_intr_alloc(dip, intrp->htable, int_type, inum,
58706512Ssowmini 	    navail, &nactual, behavior);
58713859Sml29623 	if (ddi_status != DDI_SUCCESS || nactual == 0) {
58723859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
58736512Ssowmini 		    " ddi_intr_alloc() failed: %d",
58746512Ssowmini 		    ddi_status));
58753859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
58763859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
58773859Sml29623 	}
58783859Sml29623 
58793859Sml29623 	if ((ddi_status = ddi_intr_get_pri(intrp->htable[0],
58806512Ssowmini 	    (uint_t *)&intrp->pri)) != DDI_SUCCESS) {
58813859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
58826512Ssowmini 		    " ddi_intr_get_pri() failed: %d",
58836512Ssowmini 		    ddi_status));
58843859Sml29623 		/* Free already allocated interrupts */
58853859Sml29623 		for (y = 0; y < nactual; y++) {
58863859Sml29623 			(void) ddi_intr_free(intrp->htable[y]);
58873859Sml29623 		}
58883859Sml29623 
58893859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
58903859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
58913859Sml29623 	}
58923859Sml29623 
58933859Sml29623 	nrequired = 0;
58943859Sml29623 	switch (nxgep->niu_type) {
58953859Sml29623 	default:
58963859Sml29623 		status = nxge_ldgv_init(nxgep, &nactual, &nrequired);
58973859Sml29623 		break;
58983859Sml29623 
58993859Sml29623 	case N2_NIU:
59003859Sml29623 		status = nxge_ldgv_init_n2(nxgep, &nactual, &nrequired);
59013859Sml29623 		break;
59023859Sml29623 	}
59033859Sml29623 
59043859Sml29623 	if (status != NXGE_OK) {
59053859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
59066512Ssowmini 		    "nxge_add_intrs_adv_typ:nxge_ldgv_init "
59076512Ssowmini 		    "failed: 0x%x", status));
59083859Sml29623 		/* Free already allocated interrupts */
59093859Sml29623 		for (y = 0; y < nactual; y++) {
59103859Sml29623 			(void) ddi_intr_free(intrp->htable[y]);
59113859Sml29623 		}
59123859Sml29623 
59133859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
59143859Sml29623 		return (status);
59153859Sml29623 	}
59163859Sml29623 
59173859Sml29623 	ldgp = nxgep->ldgvp->ldgp;
59183859Sml29623 	for (x = 0; x < nrequired; x++, ldgp++) {
59193859Sml29623 		ldgp->vector = (uint8_t)x;
59203859Sml29623 		ldgp->intdata = SID_DATA(ldgp->func, x);
59213859Sml29623 		arg1 = ldgp->ldvp;
59223859Sml29623 		arg2 = nxgep;
59233859Sml29623 		if (ldgp->nldvs == 1) {
59243859Sml29623 			inthandler = (uint_t *)ldgp->ldvp->ldv_intr_handler;
59253859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
59266512Ssowmini 			    "nxge_add_intrs_adv_type: "
59276512Ssowmini 			    "arg1 0x%x arg2 0x%x: "
59286512Ssowmini 			    "1-1 int handler (entry %d intdata 0x%x)\n",
59296512Ssowmini 			    arg1, arg2,
59306512Ssowmini 			    x, ldgp->intdata));
59313859Sml29623 		} else if (ldgp->nldvs > 1) {
59323859Sml29623 			inthandler = (uint_t *)ldgp->sys_intr_handler;
59333859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
59346512Ssowmini 			    "nxge_add_intrs_adv_type: "
59356512Ssowmini 			    "arg1 0x%x arg2 0x%x: "
59366512Ssowmini 			    "nldevs %d int handler "
59376512Ssowmini 			    "(entry %d intdata 0x%x)\n",
59386512Ssowmini 			    arg1, arg2,
59396512Ssowmini 			    ldgp->nldvs, x, ldgp->intdata));
59403859Sml29623 		}
59413859Sml29623 
59423859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
59436512Ssowmini 		    "==> nxge_add_intrs_adv_type: ddi_add_intr(inum) #%d "
59446512Ssowmini 		    "htable 0x%llx", x, intrp->htable[x]));
59453859Sml29623 
59463859Sml29623 		if ((ddi_status = ddi_intr_add_handler(intrp->htable[x],
59476512Ssowmini 		    (ddi_intr_handler_t *)inthandler, arg1, arg2))
59486512Ssowmini 		    != DDI_SUCCESS) {
59493859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
59506512Ssowmini 			    "==> nxge_add_intrs_adv_type: failed #%d "
59516512Ssowmini 			    "status 0x%x", x, ddi_status));
59523859Sml29623 			for (y = 0; y < intrp->intr_added; y++) {
59533859Sml29623 				(void) ddi_intr_remove_handler(
59546512Ssowmini 				    intrp->htable[y]);
59553859Sml29623 			}
59563859Sml29623 			/* Free already allocated intr */
59573859Sml29623 			for (y = 0; y < nactual; y++) {
59583859Sml29623 				(void) ddi_intr_free(intrp->htable[y]);
59593859Sml29623 			}
59603859Sml29623 			kmem_free(intrp->htable, intrp->intr_size);
59613859Sml29623 
59623859Sml29623 			(void) nxge_ldgv_uninit(nxgep);
59633859Sml29623 
59643859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
59653859Sml29623 		}
59663859Sml29623 		intrp->intr_added++;
59673859Sml29623 	}
59683859Sml29623 
59693859Sml29623 	intrp->msi_intx_cnt = nactual;
59703859Sml29623 
59713859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
59726512Ssowmini 	    "Requested: %d, Allowed: %d msi_intx_cnt %d intr_added %d",
59736512Ssowmini 	    navail, nactual,
59746512Ssowmini 	    intrp->msi_intx_cnt,
59756512Ssowmini 	    intrp->intr_added));
59763859Sml29623 
59773859Sml29623 	(void) ddi_intr_get_cap(intrp->htable[0], &intrp->intr_cap);
59783859Sml29623 
59793859Sml29623 	(void) nxge_intr_ldgv_init(nxgep);
59803859Sml29623 
59813859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_add_intrs_adv_type"));
59823859Sml29623 
59833859Sml29623 	return (status);
59843859Sml29623 }
59853859Sml29623 
59863859Sml29623 /*ARGSUSED*/
59873859Sml29623 static nxge_status_t
59883859Sml29623 nxge_add_intrs_adv_type_fix(p_nxge_t nxgep, uint32_t int_type)
59893859Sml29623 {
59903859Sml29623 	dev_info_t		*dip = nxgep->dip;
59913859Sml29623 	p_nxge_ldg_t		ldgp;
59923859Sml29623 	p_nxge_intr_t		intrp;
59933859Sml29623 	uint_t			*inthandler;
59943859Sml29623 	void			*arg1, *arg2;
59953859Sml29623 	int			behavior;
59963859Sml29623 	int			nintrs, navail;
59973859Sml29623 	int			nactual, nrequired;
59983859Sml29623 	int			inum = 0;
59993859Sml29623 	int			x, y;
60003859Sml29623 	int			ddi_status = DDI_SUCCESS;
60013859Sml29623 	nxge_status_t		status = NXGE_OK;
60023859Sml29623 
60033859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs_adv_type_fix"));
60043859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
60053859Sml29623 	intrp->start_inum = 0;
60063859Sml29623 
60073859Sml29623 	ddi_status = ddi_intr_get_nintrs(dip, int_type, &nintrs);
60083859Sml29623 	if ((ddi_status != DDI_SUCCESS) || (nintrs == 0)) {
60093859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
60106512Ssowmini 		    "ddi_intr_get_nintrs() failed, status: 0x%x%, "
60116512Ssowmini 		    "nintrs: %d", status, nintrs));
60123859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
60133859Sml29623 	}
60143859Sml29623 
60153859Sml29623 	ddi_status = ddi_intr_get_navail(dip, int_type, &navail);
60163859Sml29623 	if ((ddi_status != DDI_SUCCESS) || (navail == 0)) {
60173859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
60186512Ssowmini 		    "ddi_intr_get_navail() failed, status: 0x%x%, "
60196512Ssowmini 		    "nintrs: %d", ddi_status, navail));
60203859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
60213859Sml29623 	}
60223859Sml29623 
60233859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL,
60246512Ssowmini 	    "ddi_intr_get_navail() returned: nintrs %d, naavail %d",
60256512Ssowmini 	    nintrs, navail));
60263859Sml29623 
60273859Sml29623 	behavior = ((int_type == DDI_INTR_TYPE_FIXED) ? DDI_INTR_ALLOC_STRICT :
60286512Ssowmini 	    DDI_INTR_ALLOC_NORMAL);
60293859Sml29623 	intrp->intr_size = navail * sizeof (ddi_intr_handle_t);
60303859Sml29623 	intrp->htable = kmem_alloc(intrp->intr_size, KM_SLEEP);
60313859Sml29623 	ddi_status = ddi_intr_alloc(dip, intrp->htable, int_type, inum,
60326512Ssowmini 	    navail, &nactual, behavior);
60333859Sml29623 	if (ddi_status != DDI_SUCCESS || nactual == 0) {
60343859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
60356512Ssowmini 		    " ddi_intr_alloc() failed: %d",
60366512Ssowmini 		    ddi_status));
60373859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
60383859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
60393859Sml29623 	}
60403859Sml29623 
60413859Sml29623 	if ((ddi_status = ddi_intr_get_pri(intrp->htable[0],
60426512Ssowmini 	    (uint_t *)&intrp->pri)) != DDI_SUCCESS) {
60433859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
60446512Ssowmini 		    " ddi_intr_get_pri() failed: %d",
60456512Ssowmini 		    ddi_status));
60463859Sml29623 		/* Free already allocated interrupts */
60473859Sml29623 		for (y = 0; y < nactual; y++) {
60483859Sml29623 			(void) ddi_intr_free(intrp->htable[y]);
60493859Sml29623 		}
60503859Sml29623 
60513859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
60523859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
60533859Sml29623 	}
60543859Sml29623 
60553859Sml29623 	nrequired = 0;
60563859Sml29623 	switch (nxgep->niu_type) {
60573859Sml29623 	default:
60583859Sml29623 		status = nxge_ldgv_init(nxgep, &nactual, &nrequired);
60593859Sml29623 		break;
60603859Sml29623 
60613859Sml29623 	case N2_NIU:
60623859Sml29623 		status = nxge_ldgv_init_n2(nxgep, &nactual, &nrequired);
60633859Sml29623 		break;
60643859Sml29623 	}
60653859Sml29623 
60663859Sml29623 	if (status != NXGE_OK) {
60673859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
60686512Ssowmini 		    "nxge_add_intrs_adv_type_fix:nxge_ldgv_init "
60696512Ssowmini 		    "failed: 0x%x", status));
60703859Sml29623 		/* Free already allocated interrupts */
60713859Sml29623 		for (y = 0; y < nactual; y++) {
60723859Sml29623 			(void) ddi_intr_free(intrp->htable[y]);
60733859Sml29623 		}
60743859Sml29623 
60753859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
60763859Sml29623 		return (status);
60773859Sml29623 	}
60783859Sml29623 
60793859Sml29623 	ldgp = nxgep->ldgvp->ldgp;
60803859Sml29623 	for (x = 0; x < nrequired; x++, ldgp++) {
60813859Sml29623 		ldgp->vector = (uint8_t)x;
60823859Sml29623 		if (nxgep->niu_type != N2_NIU) {
60833859Sml29623 			ldgp->intdata = SID_DATA(ldgp->func, x);
60843859Sml29623 		}
60853859Sml29623 
60863859Sml29623 		arg1 = ldgp->ldvp;
60873859Sml29623 		arg2 = nxgep;
60883859Sml29623 		if (ldgp->nldvs == 1) {
60893859Sml29623 			inthandler = (uint_t *)ldgp->ldvp->ldv_intr_handler;
60903859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
60916512Ssowmini 			    "nxge_add_intrs_adv_type_fix: "
60926512Ssowmini 			    "1-1 int handler(%d) ldg %d ldv %d "
60936512Ssowmini 			    "arg1 $%p arg2 $%p\n",
60946512Ssowmini 			    x, ldgp->ldg, ldgp->ldvp->ldv,
60956512Ssowmini 			    arg1, arg2));
60963859Sml29623 		} else if (ldgp->nldvs > 1) {
60973859Sml29623 			inthandler = (uint_t *)ldgp->sys_intr_handler;
60983859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
60996512Ssowmini 			    "nxge_add_intrs_adv_type_fix: "
61006512Ssowmini 			    "shared ldv %d int handler(%d) ldv %d ldg %d"
61016512Ssowmini 			    "arg1 0x%016llx arg2 0x%016llx\n",
61026512Ssowmini 			    x, ldgp->nldvs, ldgp->ldg, ldgp->ldvp->ldv,
61036512Ssowmini 			    arg1, arg2));
61043859Sml29623 		}
61053859Sml29623 
61063859Sml29623 		if ((ddi_status = ddi_intr_add_handler(intrp->htable[x],
61076512Ssowmini 		    (ddi_intr_handler_t *)inthandler, arg1, arg2))
61086512Ssowmini 		    != DDI_SUCCESS) {
61093859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
61106512Ssowmini 			    "==> nxge_add_intrs_adv_type_fix: failed #%d "
61116512Ssowmini 			    "status 0x%x", x, ddi_status));
61123859Sml29623 			for (y = 0; y < intrp->intr_added; y++) {
61133859Sml29623 				(void) ddi_intr_remove_handler(
61146512Ssowmini 				    intrp->htable[y]);
61153859Sml29623 			}
61163859Sml29623 			for (y = 0; y < nactual; y++) {
61173859Sml29623 				(void) ddi_intr_free(intrp->htable[y]);
61183859Sml29623 			}
61193859Sml29623 			/* Free already allocated intr */
61203859Sml29623 			kmem_free(intrp->htable, intrp->intr_size);
61213859Sml29623 
61223859Sml29623 			(void) nxge_ldgv_uninit(nxgep);
61233859Sml29623 
61243859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
61253859Sml29623 		}
61263859Sml29623 		intrp->intr_added++;
61273859Sml29623 	}
61283859Sml29623 
61293859Sml29623 	intrp->msi_intx_cnt = nactual;
61303859Sml29623 
61313859Sml29623 	(void) ddi_intr_get_cap(intrp->htable[0], &intrp->intr_cap);
61323859Sml29623 
61333859Sml29623 	status = nxge_intr_ldgv_init(nxgep);
61343859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_add_intrs_adv_type_fix"));
61353859Sml29623 
61363859Sml29623 	return (status);
61373859Sml29623 }
61383859Sml29623 
61393859Sml29623 static void
61403859Sml29623 nxge_remove_intrs(p_nxge_t nxgep)
61413859Sml29623 {
61423859Sml29623 	int		i, inum;
61433859Sml29623 	p_nxge_intr_t	intrp;
61443859Sml29623 
61453859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_remove_intrs"));
61463859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
61473859Sml29623 	if (!intrp->intr_registered) {
61483859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
61496512Ssowmini 		    "<== nxge_remove_intrs: interrupts not registered"));
61503859Sml29623 		return;
61513859Sml29623 	}
61523859Sml29623 
61533859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_remove_intrs:advanced"));
61543859Sml29623 
61553859Sml29623 	if (intrp->intr_cap & DDI_INTR_FLAG_BLOCK) {
61563859Sml29623 		(void) ddi_intr_block_disable(intrp->htable,
61576512Ssowmini 		    intrp->intr_added);
61583859Sml29623 	} else {
61593859Sml29623 		for (i = 0; i < intrp->intr_added; i++) {
61603859Sml29623 			(void) ddi_intr_disable(intrp->htable[i]);
61613859Sml29623 		}
61623859Sml29623 	}
61633859Sml29623 
61643859Sml29623 	for (inum = 0; inum < intrp->intr_added; inum++) {
61653859Sml29623 		if (intrp->htable[inum]) {
61663859Sml29623 			(void) ddi_intr_remove_handler(intrp->htable[inum]);
61673859Sml29623 		}
61683859Sml29623 	}
61693859Sml29623 
61703859Sml29623 	for (inum = 0; inum < intrp->msi_intx_cnt; inum++) {
61713859Sml29623 		if (intrp->htable[inum]) {
61723859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
61736512Ssowmini 			    "nxge_remove_intrs: ddi_intr_free inum %d "
61746512Ssowmini 			    "msi_intx_cnt %d intr_added %d",
61756512Ssowmini 			    inum,
61766512Ssowmini 			    intrp->msi_intx_cnt,
61776512Ssowmini 			    intrp->intr_added));
61783859Sml29623 
61793859Sml29623 			(void) ddi_intr_free(intrp->htable[inum]);
61803859Sml29623 		}
61813859Sml29623 	}
61823859Sml29623 
61833859Sml29623 	kmem_free(intrp->htable, intrp->intr_size);
61843859Sml29623 	intrp->intr_registered = B_FALSE;
61853859Sml29623 	intrp->intr_enabled = B_FALSE;
61863859Sml29623 	intrp->msi_intx_cnt = 0;
61873859Sml29623 	intrp->intr_added = 0;
61883859Sml29623 
61893859Sml29623 	(void) nxge_ldgv_uninit(nxgep);
61903859Sml29623 
61915013Sml29623 	(void) ddi_prop_remove(DDI_DEV_T_NONE, nxgep->dip,
61925013Sml29623 	    "#msix-request");
61935013Sml29623 
61943859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_remove_intrs"));
61953859Sml29623 }
61963859Sml29623 
61973859Sml29623 /*ARGSUSED*/
61983859Sml29623 static void
61993859Sml29623 nxge_remove_soft_intrs(p_nxge_t nxgep)
62003859Sml29623 {
62013859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_remove_soft_intrs"));
62023859Sml29623 	if (nxgep->resched_id) {
62033859Sml29623 		ddi_remove_softintr(nxgep->resched_id);
62043859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
62056512Ssowmini 		    "==> nxge_remove_soft_intrs: removed"));
62063859Sml29623 		nxgep->resched_id = NULL;
62073859Sml29623 	}
62083859Sml29623 
62093859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_remove_soft_intrs"));
62103859Sml29623 }
62113859Sml29623 
62123859Sml29623 /*ARGSUSED*/
62133859Sml29623 static void
62143859Sml29623 nxge_intrs_enable(p_nxge_t nxgep)
62153859Sml29623 {
62163859Sml29623 	p_nxge_intr_t	intrp;
62173859Sml29623 	int		i;
62183859Sml29623 	int		status;
62193859Sml29623 
62203859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_intrs_enable"));
62213859Sml29623 
62223859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
62233859Sml29623 
62243859Sml29623 	if (!intrp->intr_registered) {
62253859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "<== nxge_intrs_enable: "
62266512Ssowmini 		    "interrupts are not registered"));
62273859Sml29623 		return;
62283859Sml29623 	}
62293859Sml29623 
62303859Sml29623 	if (intrp->intr_enabled) {
62313859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
62326512Ssowmini 		    "<== nxge_intrs_enable: already enabled"));
62333859Sml29623 		return;
62343859Sml29623 	}
62353859Sml29623 
62363859Sml29623 	if (intrp->intr_cap & DDI_INTR_FLAG_BLOCK) {
62373859Sml29623 		status = ddi_intr_block_enable(intrp->htable,
62386512Ssowmini 		    intrp->intr_added);
62393859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_intrs_enable "
62406512Ssowmini 		    "block enable - status 0x%x total inums #%d\n",
62416512Ssowmini 		    status, intrp->intr_added));
62423859Sml29623 	} else {
62433859Sml29623 		for (i = 0; i < intrp->intr_added; i++) {
62443859Sml29623 			status = ddi_intr_enable(intrp->htable[i]);
62453859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_intrs_enable "
62466512Ssowmini 			    "ddi_intr_enable:enable - status 0x%x "
62476512Ssowmini 			    "total inums %d enable inum #%d\n",
62486512Ssowmini 			    status, intrp->intr_added, i));
62493859Sml29623 			if (status == DDI_SUCCESS) {
62503859Sml29623 				intrp->intr_enabled = B_TRUE;
62513859Sml29623 			}
62523859Sml29623 		}
62533859Sml29623 	}
62543859Sml29623 
62553859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_intrs_enable"));
62563859Sml29623 }
62573859Sml29623 
62583859Sml29623 /*ARGSUSED*/
62593859Sml29623 static void
62603859Sml29623 nxge_intrs_disable(p_nxge_t nxgep)
62613859Sml29623 {
62623859Sml29623 	p_nxge_intr_t	intrp;
62633859Sml29623 	int		i;
62643859Sml29623 
62653859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_intrs_disable"));
62663859Sml29623 
62673859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
62683859Sml29623 
62693859Sml29623 	if (!intrp->intr_registered) {
62703859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_intrs_disable: "
62716512Ssowmini 		    "interrupts are not registered"));
62723859Sml29623 		return;
62733859Sml29623 	}
62743859Sml29623 
62753859Sml29623 	if (intrp->intr_cap & DDI_INTR_FLAG_BLOCK) {
62763859Sml29623 		(void) ddi_intr_block_disable(intrp->htable,
62776512Ssowmini 		    intrp->intr_added);
62783859Sml29623 	} else {
62793859Sml29623 		for (i = 0; i < intrp->intr_added; i++) {
62803859Sml29623 			(void) ddi_intr_disable(intrp->htable[i]);
62813859Sml29623 		}
62823859Sml29623 	}
62833859Sml29623 
62843859Sml29623 	intrp->intr_enabled = B_FALSE;
62853859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_intrs_disable"));
62863859Sml29623 }
62873859Sml29623 
62883859Sml29623 static nxge_status_t
62893859Sml29623 nxge_mac_register(p_nxge_t nxgep)
62903859Sml29623 {
62913859Sml29623 	mac_register_t *macp;
62923859Sml29623 	int		status;
62933859Sml29623 
62943859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_mac_register"));
62953859Sml29623 
62963859Sml29623 	if ((macp = mac_alloc(MAC_VERSION)) == NULL)
62973859Sml29623 		return (NXGE_ERROR);
62983859Sml29623 
62993859Sml29623 	macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
63003859Sml29623 	macp->m_driver = nxgep;
63013859Sml29623 	macp->m_dip = nxgep->dip;
63023859Sml29623 	macp->m_src_addr = nxgep->ouraddr.ether_addr_octet;
63033859Sml29623 	macp->m_callbacks = &nxge_m_callbacks;
63043859Sml29623 	macp->m_min_sdu = 0;
63056439Sml29623 	nxgep->mac.default_mtu = nxgep->mac.maxframesize -
63066439Sml29623 	    NXGE_EHEADER_VLAN_CRC;
63076439Sml29623 	macp->m_max_sdu = nxgep->mac.default_mtu;
63085895Syz147064 	macp->m_margin = VLAN_TAGSZ;
63096512Ssowmini 	macp->m_priv_props = nxge_priv_props;
63106512Ssowmini 	macp->m_priv_prop_count = NXGE_MAX_PRIV_PROPS;
63113859Sml29623 
63126439Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL,
63136439Sml29623 	    "==> nxge_mac_register: instance %d "
63146439Sml29623 	    "max_sdu %d margin %d maxframe %d (header %d)",
63156439Sml29623 	    nxgep->instance,
63166439Sml29623 	    macp->m_max_sdu, macp->m_margin,
63176439Sml29623 	    nxgep->mac.maxframesize,
63186439Sml29623 	    NXGE_EHEADER_VLAN_CRC));
63196439Sml29623 
63203859Sml29623 	status = mac_register(macp, &nxgep->mach);
63213859Sml29623 	mac_free(macp);
63223859Sml29623 
63233859Sml29623 	if (status != 0) {
63243859Sml29623 		cmn_err(CE_WARN,
63256512Ssowmini 		    "!nxge_mac_register failed (status %d instance %d)",
63266512Ssowmini 		    status, nxgep->instance);
63273859Sml29623 		return (NXGE_ERROR);
63283859Sml29623 	}
63293859Sml29623 
63303859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_mac_register success "
63316512Ssowmini 	    "(instance %d)", nxgep->instance));
63323859Sml29623 
63333859Sml29623 	return (NXGE_OK);
63343859Sml29623 }
63353859Sml29623 
63363859Sml29623 void
63373859Sml29623 nxge_err_inject(p_nxge_t nxgep, queue_t *wq, mblk_t *mp)
63383859Sml29623 {
63393859Sml29623 	ssize_t		size;
63403859Sml29623 	mblk_t		*nmp;
63413859Sml29623 	uint8_t		blk_id;
63423859Sml29623 	uint8_t		chan;
63433859Sml29623 	uint32_t	err_id;
63443859Sml29623 	err_inject_t	*eip;
63453859Sml29623 
63463859Sml29623 	NXGE_DEBUG_MSG((nxgep, STR_CTL, "==> nxge_err_inject"));
63473859Sml29623 
63483859Sml29623 	size = 1024;
63493859Sml29623 	nmp = mp->b_cont;
63503859Sml29623 	eip = (err_inject_t *)nmp->b_rptr;
63513859Sml29623 	blk_id = eip->blk_id;
63523859Sml29623 	err_id = eip->err_id;
63533859Sml29623 	chan = eip->chan;
63543859Sml29623 	cmn_err(CE_NOTE, "!blk_id = 0x%x\n", blk_id);
63553859Sml29623 	cmn_err(CE_NOTE, "!err_id = 0x%x\n", err_id);
63563859Sml29623 	cmn_err(CE_NOTE, "!chan = 0x%x\n", chan);
63573859Sml29623 	switch (blk_id) {
63583859Sml29623 	case MAC_BLK_ID:
63593859Sml29623 		break;
63603859Sml29623 	case TXMAC_BLK_ID:
63613859Sml29623 		break;
63623859Sml29623 	case RXMAC_BLK_ID:
63633859Sml29623 		break;
63643859Sml29623 	case MIF_BLK_ID:
63653859Sml29623 		break;
63663859Sml29623 	case IPP_BLK_ID:
63673859Sml29623 		nxge_ipp_inject_err(nxgep, err_id);
63683859Sml29623 		break;
63693859Sml29623 	case TXC_BLK_ID:
63703859Sml29623 		nxge_txc_inject_err(nxgep, err_id);
63713859Sml29623 		break;
63723859Sml29623 	case TXDMA_BLK_ID:
63733859Sml29623 		nxge_txdma_inject_err(nxgep, err_id, chan);
63743859Sml29623 		break;
63753859Sml29623 	case RXDMA_BLK_ID:
63763859Sml29623 		nxge_rxdma_inject_err(nxgep, err_id, chan);
63773859Sml29623 		break;
63783859Sml29623 	case ZCP_BLK_ID:
63793859Sml29623 		nxge_zcp_inject_err(nxgep, err_id);
63803859Sml29623 		break;
63813859Sml29623 	case ESPC_BLK_ID:
63823859Sml29623 		break;
63833859Sml29623 	case FFLP_BLK_ID:
63843859Sml29623 		break;
63853859Sml29623 	case PHY_BLK_ID:
63863859Sml29623 		break;
63873859Sml29623 	case ETHER_SERDES_BLK_ID:
63883859Sml29623 		break;
63893859Sml29623 	case PCIE_SERDES_BLK_ID:
63903859Sml29623 		break;
63913859Sml29623 	case VIR_BLK_ID:
63923859Sml29623 		break;
63933859Sml29623 	}
63943859Sml29623 
63953859Sml29623 	nmp->b_wptr = nmp->b_rptr + size;
63963859Sml29623 	NXGE_DEBUG_MSG((nxgep, STR_CTL, "<== nxge_err_inject"));
63973859Sml29623 
63983859Sml29623 	miocack(wq, mp, (int)size, 0);
63993859Sml29623 }
64003859Sml29623 
64013859Sml29623 static int
64023859Sml29623 nxge_init_common_dev(p_nxge_t nxgep)
64033859Sml29623 {
64043859Sml29623 	p_nxge_hw_list_t	hw_p;
64053859Sml29623 	dev_info_t 		*p_dip;
64063859Sml29623 
64073859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "==> nxge_init_common_device"));
64083859Sml29623 
64093859Sml29623 	p_dip = nxgep->p_dip;
64103859Sml29623 	MUTEX_ENTER(&nxge_common_lock);
64113859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL,
64126512Ssowmini 	    "==> nxge_init_common_dev:func # %d",
64136512Ssowmini 	    nxgep->function_num));
64143859Sml29623 	/*
64153859Sml29623 	 * Loop through existing per neptune hardware list.
64163859Sml29623 	 */
64173859Sml29623 	for (hw_p = nxge_hw_list; hw_p; hw_p = hw_p->next) {
64183859Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
64196512Ssowmini 		    "==> nxge_init_common_device:func # %d "
64206512Ssowmini 		    "hw_p $%p parent dip $%p",
64216512Ssowmini 		    nxgep->function_num,
64226512Ssowmini 		    hw_p,
64236512Ssowmini 		    p_dip));
64243859Sml29623 		if (hw_p->parent_devp == p_dip) {
64253859Sml29623 			nxgep->nxge_hw_p = hw_p;
64263859Sml29623 			hw_p->ndevs++;
64273859Sml29623 			hw_p->nxge_p[nxgep->function_num] = nxgep;
64283859Sml29623 			NXGE_DEBUG_MSG((nxgep, MOD_CTL,
64296512Ssowmini 			    "==> nxge_init_common_device:func # %d "
64306512Ssowmini 			    "hw_p $%p parent dip $%p "
64316512Ssowmini 			    "ndevs %d (found)",
64326512Ssowmini 			    nxgep->function_num,
64336512Ssowmini 			    hw_p,
64346512Ssowmini 			    p_dip,
64356512Ssowmini 			    hw_p->ndevs));
64363859Sml29623 			break;
64373859Sml29623 		}
64383859Sml29623 	}
64393859Sml29623 
64403859Sml29623 	if (hw_p == NULL) {
64413859Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
64426512Ssowmini 		    "==> nxge_init_common_device:func # %d "
64436512Ssowmini 		    "parent dip $%p (new)",
64446512Ssowmini 		    nxgep->function_num,
64456512Ssowmini 		    p_dip));
64463859Sml29623 		hw_p = kmem_zalloc(sizeof (nxge_hw_list_t), KM_SLEEP);
64473859Sml29623 		hw_p->parent_devp = p_dip;
64483859Sml29623 		hw_p->magic = NXGE_NEPTUNE_MAGIC;
64493859Sml29623 		nxgep->nxge_hw_p = hw_p;
64503859Sml29623 		hw_p->ndevs++;
64513859Sml29623 		hw_p->nxge_p[nxgep->function_num] = nxgep;
64523859Sml29623 		hw_p->next = nxge_hw_list;
64534732Sdavemq 		if (nxgep->niu_type == N2_NIU) {
64544732Sdavemq 			hw_p->niu_type = N2_NIU;
64554732Sdavemq 			hw_p->platform_type = P_NEPTUNE_NIU;
64564732Sdavemq 		} else {
64574732Sdavemq 			hw_p->niu_type = NIU_TYPE_NONE;
64584977Sraghus 			hw_p->platform_type = P_NEPTUNE_NONE;
64594732Sdavemq 		}
64603859Sml29623 
64613859Sml29623 		MUTEX_INIT(&hw_p->nxge_cfg_lock, NULL, MUTEX_DRIVER, NULL);
64623859Sml29623 		MUTEX_INIT(&hw_p->nxge_tcam_lock, NULL, MUTEX_DRIVER, NULL);
64633859Sml29623 		MUTEX_INIT(&hw_p->nxge_vlan_lock, NULL, MUTEX_DRIVER, NULL);
64643859Sml29623 		MUTEX_INIT(&hw_p->nxge_mdio_lock, NULL, MUTEX_DRIVER, NULL);
64653859Sml29623 
64663859Sml29623 		nxge_hw_list = hw_p;
64674732Sdavemq 
64684732Sdavemq 		(void) nxge_scan_ports_phy(nxgep, nxge_hw_list);
64693859Sml29623 	}
64703859Sml29623 
64713859Sml29623 	MUTEX_EXIT(&nxge_common_lock);
64724732Sdavemq 
64734977Sraghus 	nxgep->platform_type = hw_p->platform_type;
64744732Sdavemq 	if (nxgep->niu_type != N2_NIU) {
64754732Sdavemq 		nxgep->niu_type = hw_p->niu_type;
64764732Sdavemq 	}
64774732Sdavemq 
64783859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL,
64796512Ssowmini 	    "==> nxge_init_common_device (nxge_hw_list) $%p",
64806512Ssowmini 	    nxge_hw_list));
64813859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "<== nxge_init_common_device"));
64823859Sml29623 
64833859Sml29623 	return (NXGE_OK);
64843859Sml29623 }
64853859Sml29623 
64863859Sml29623 static void
64873859Sml29623 nxge_uninit_common_dev(p_nxge_t nxgep)
64883859Sml29623 {
64893859Sml29623 	p_nxge_hw_list_t	hw_p, h_hw_p;
64906801Sspeer 	p_nxge_dma_pt_cfg_t	p_dma_cfgp;
64916801Sspeer 	p_nxge_hw_pt_cfg_t	p_cfgp;
64923859Sml29623 	dev_info_t 		*p_dip;
64933859Sml29623 
64943859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "==> nxge_uninit_common_device"));
64953859Sml29623 	if (nxgep->nxge_hw_p == NULL) {
64963859Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
64976512Ssowmini 		    "<== nxge_uninit_common_device (no common)"));
64983859Sml29623 		return;
64993859Sml29623 	}
65003859Sml29623 
65013859Sml29623 	MUTEX_ENTER(&nxge_common_lock);
65023859Sml29623 	h_hw_p = nxge_hw_list;
65033859Sml29623 	for (hw_p = nxge_hw_list; hw_p; hw_p = hw_p->next) {
65043859Sml29623 		p_dip = hw_p->parent_devp;
65053859Sml29623 		if (nxgep->nxge_hw_p == hw_p &&
65066512Ssowmini 		    p_dip == nxgep->p_dip &&
65076512Ssowmini 		    nxgep->nxge_hw_p->magic == NXGE_NEPTUNE_MAGIC &&
65086512Ssowmini 		    hw_p->magic == NXGE_NEPTUNE_MAGIC) {
65093859Sml29623 
65103859Sml29623 			NXGE_DEBUG_MSG((nxgep, MOD_CTL,
65116512Ssowmini 			    "==> nxge_uninit_common_device:func # %d "
65126512Ssowmini 			    "hw_p $%p parent dip $%p "
65136512Ssowmini 			    "ndevs %d (found)",
65146512Ssowmini 			    nxgep->function_num,
65156512Ssowmini 			    hw_p,
65166512Ssowmini 			    p_dip,
65176512Ssowmini 			    hw_p->ndevs));
65183859Sml29623 
65196801Sspeer 			/*
65206801Sspeer 			 * Release the RDC table, a shared resoruce
65216801Sspeer 			 * of the nxge hardware.  The RDC table was
65226801Sspeer 			 * assigned to this instance of nxge in
65236801Sspeer 			 * nxge_use_cfg_dma_config().
65246801Sspeer 			 */
65256801Sspeer 			p_dma_cfgp = (p_nxge_dma_pt_cfg_t)&nxgep->pt_config;
65266801Sspeer 			p_cfgp = (p_nxge_hw_pt_cfg_t)&p_dma_cfgp->hw_config;
65276801Sspeer 			(void) nxge_fzc_rdc_tbl_unbind(nxgep,
65286837Syc148097 			    p_cfgp->def_mac_rxdma_grpid);
65296801Sspeer 
65303859Sml29623 			if (hw_p->ndevs) {
65313859Sml29623 				hw_p->ndevs--;
65323859Sml29623 			}
65333859Sml29623 			hw_p->nxge_p[nxgep->function_num] = NULL;
65343859Sml29623 			if (!hw_p->ndevs) {
65353859Sml29623 				MUTEX_DESTROY(&hw_p->nxge_vlan_lock);
65363859Sml29623 				MUTEX_DESTROY(&hw_p->nxge_tcam_lock);
65373859Sml29623 				MUTEX_DESTROY(&hw_p->nxge_cfg_lock);
65383859Sml29623 				MUTEX_DESTROY(&hw_p->nxge_mdio_lock);
65393859Sml29623 				NXGE_DEBUG_MSG((nxgep, MOD_CTL,
65406512Ssowmini 				    "==> nxge_uninit_common_device: "
65416512Ssowmini 				    "func # %d "
65426512Ssowmini 				    "hw_p $%p parent dip $%p "
65436512Ssowmini 				    "ndevs %d (last)",
65446512Ssowmini 				    nxgep->function_num,
65456512Ssowmini 				    hw_p,
65466512Ssowmini 				    p_dip,
65476512Ssowmini 				    hw_p->ndevs));
65483859Sml29623 
65496495Sspeer 				nxge_hio_uninit(nxgep);
65506495Sspeer 
65513859Sml29623 				if (hw_p == nxge_hw_list) {
65523859Sml29623 					NXGE_DEBUG_MSG((nxgep, MOD_CTL,
65536512Ssowmini 					    "==> nxge_uninit_common_device:"
65546512Ssowmini 					    "remove head func # %d "
65556512Ssowmini 					    "hw_p $%p parent dip $%p "
65566512Ssowmini 					    "ndevs %d (head)",
65576512Ssowmini 					    nxgep->function_num,
65586512Ssowmini 					    hw_p,
65596512Ssowmini 					    p_dip,
65606512Ssowmini 					    hw_p->ndevs));
65613859Sml29623 					nxge_hw_list = hw_p->next;
65623859Sml29623 				} else {
65633859Sml29623 					NXGE_DEBUG_MSG((nxgep, MOD_CTL,
65646512Ssowmini 					    "==> nxge_uninit_common_device:"
65656512Ssowmini 					    "remove middle func # %d "
65666512Ssowmini 					    "hw_p $%p parent dip $%p "
65676512Ssowmini 					    "ndevs %d (middle)",
65686512Ssowmini 					    nxgep->function_num,
65696512Ssowmini 					    hw_p,
65706512Ssowmini 					    p_dip,
65716512Ssowmini 					    hw_p->ndevs));
65723859Sml29623 					h_hw_p->next = hw_p->next;
65733859Sml29623 				}
65743859Sml29623 
65756495Sspeer 				nxgep->nxge_hw_p = NULL;
65763859Sml29623 				KMEM_FREE(hw_p, sizeof (nxge_hw_list_t));
65773859Sml29623 			}
65783859Sml29623 			break;
65793859Sml29623 		} else {
65803859Sml29623 			h_hw_p = hw_p;
65813859Sml29623 		}
65823859Sml29623 	}
65833859Sml29623 
65843859Sml29623 	MUTEX_EXIT(&nxge_common_lock);
65853859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL,
65866512Ssowmini 	    "==> nxge_uninit_common_device (nxge_hw_list) $%p",
65876512Ssowmini 	    nxge_hw_list));
65883859Sml29623 
65893859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "<= nxge_uninit_common_device"));
65903859Sml29623 }
65914732Sdavemq 
65924732Sdavemq /*
65934977Sraghus  * Determines the number of ports from the niu_type or the platform type.
65944732Sdavemq  * Returns the number of ports, or returns zero on failure.
65954732Sdavemq  */
65964732Sdavemq 
65974732Sdavemq int
65984977Sraghus nxge_get_nports(p_nxge_t nxgep)
65994732Sdavemq {
66004732Sdavemq 	int	nports = 0;
66014732Sdavemq 
66024977Sraghus 	switch (nxgep->niu_type) {
66034732Sdavemq 	case N2_NIU:
66044732Sdavemq 	case NEPTUNE_2_10GF:
66054732Sdavemq 		nports = 2;
66064732Sdavemq 		break;
66074732Sdavemq 	case NEPTUNE_4_1GC:
66084732Sdavemq 	case NEPTUNE_2_10GF_2_1GC:
66094732Sdavemq 	case NEPTUNE_1_10GF_3_1GC:
66104732Sdavemq 	case NEPTUNE_1_1GC_1_10GF_2_1GC:
66116261Sjoycey 	case NEPTUNE_2_10GF_2_1GRF:
66124732Sdavemq 		nports = 4;
66134732Sdavemq 		break;
66144732Sdavemq 	default:
66154977Sraghus 		switch (nxgep->platform_type) {
66164977Sraghus 		case P_NEPTUNE_NIU:
66174977Sraghus 		case P_NEPTUNE_ATLAS_2PORT:
66184977Sraghus 			nports = 2;
66194977Sraghus 			break;
66204977Sraghus 		case P_NEPTUNE_ATLAS_4PORT:
66214977Sraghus 		case P_NEPTUNE_MARAMBA_P0:
66224977Sraghus 		case P_NEPTUNE_MARAMBA_P1:
66235196Ssbehera 		case P_NEPTUNE_ALONSO:
66244977Sraghus 			nports = 4;
66254977Sraghus 			break;
66264977Sraghus 		default:
66274977Sraghus 			break;
66284977Sraghus 		}
66294732Sdavemq 		break;
66304732Sdavemq 	}
66314732Sdavemq 
66324732Sdavemq 	return (nports);
66334732Sdavemq }
66345013Sml29623 
66355013Sml29623 /*
66365013Sml29623  * The following two functions are to support
66375013Sml29623  * PSARC/2007/453 MSI-X interrupt limit override.
66385013Sml29623  */
66395013Sml29623 static int
66405013Sml29623 nxge_create_msi_property(p_nxge_t nxgep)
66415013Sml29623 {
66425013Sml29623 	int	nmsi;
66435013Sml29623 	extern	int ncpus;
66445013Sml29623 
66455013Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "==>nxge_create_msi_property"));
66465013Sml29623 
66475013Sml29623 	switch (nxgep->mac.portmode) {
66485013Sml29623 	case PORT_10G_COPPER:
66495013Sml29623 	case PORT_10G_FIBER:
66506835Syc148097 	case PORT_10G_TN1010:
66515013Sml29623 		(void) ddi_prop_create(DDI_DEV_T_NONE, nxgep->dip,
66525013Sml29623 		    DDI_PROP_CANSLEEP, "#msix-request", NULL, 0);
66535013Sml29623 		/*
66545013Sml29623 		 * The maximum MSI-X requested will be 8.
66555013Sml29623 		 * If the # of CPUs is less than 8, we will reqeust
66565013Sml29623 		 * # MSI-X based on the # of CPUs.
66575013Sml29623 		 */
66585013Sml29623 		if (ncpus >= NXGE_MSIX_REQUEST_10G) {
66595013Sml29623 			nmsi = NXGE_MSIX_REQUEST_10G;
66605013Sml29623 		} else {
66615013Sml29623 			nmsi = ncpus;
66625013Sml29623 		}
66635013Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
66645013Sml29623 		    "==>nxge_create_msi_property(10G): exists 0x%x (nmsi %d)",
66655013Sml29623 		    ddi_prop_exists(DDI_DEV_T_NONE, nxgep->dip,
66665013Sml29623 		    DDI_PROP_CANSLEEP, "#msix-request"), nmsi));
66675013Sml29623 		break;
66685013Sml29623 
66695013Sml29623 	default:
66705013Sml29623 		nmsi = NXGE_MSIX_REQUEST_1G;
66715013Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
66725013Sml29623 		    "==>nxge_create_msi_property(1G): exists 0x%x (nmsi %d)",
66735013Sml29623 		    ddi_prop_exists(DDI_DEV_T_NONE, nxgep->dip,
66745013Sml29623 		    DDI_PROP_CANSLEEP, "#msix-request"), nmsi));
66755013Sml29623 		break;
66765013Sml29623 	}
66775013Sml29623 
66785013Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "<==nxge_create_msi_property"));
66795013Sml29623 	return (nmsi);
66805013Sml29623 }
66816512Ssowmini 
66826512Ssowmini /* ARGSUSED */
66836512Ssowmini static int
66846512Ssowmini nxge_get_def_val(nxge_t *nxgep, mac_prop_id_t pr_num, uint_t pr_valsize,
66856512Ssowmini     void *pr_val)
66866512Ssowmini {
66876512Ssowmini 	int err = 0;
66886512Ssowmini 	link_flowctrl_t fl;
66896512Ssowmini 
66906512Ssowmini 	switch (pr_num) {
66916789Sam223141 	case MAC_PROP_AUTONEG:
66926512Ssowmini 		*(uint8_t *)pr_val = 1;
66936512Ssowmini 		break;
66946789Sam223141 	case MAC_PROP_FLOWCTRL:
66956512Ssowmini 		if (pr_valsize < sizeof (link_flowctrl_t))
66966512Ssowmini 			return (EINVAL);
66976512Ssowmini 		fl = LINK_FLOWCTRL_RX;
66986512Ssowmini 		bcopy(&fl, pr_val, sizeof (fl));
66996512Ssowmini 		break;
67006789Sam223141 	case MAC_PROP_ADV_1000FDX_CAP:
67016789Sam223141 	case MAC_PROP_EN_1000FDX_CAP:
67026512Ssowmini 		*(uint8_t *)pr_val = 1;
67036512Ssowmini 		break;
67046789Sam223141 	case MAC_PROP_ADV_100FDX_CAP:
67056789Sam223141 	case MAC_PROP_EN_100FDX_CAP:
67066512Ssowmini 		*(uint8_t *)pr_val = 1;
67076512Ssowmini 		break;
67086512Ssowmini 	default:
67096512Ssowmini 		err = ENOTSUP;
67106512Ssowmini 		break;
67116512Ssowmini 	}
67126512Ssowmini 	return (err);
67136512Ssowmini }
67146705Sml29623 
67156705Sml29623 
67166705Sml29623 /*
67176705Sml29623  * The following is a software around for the Neptune hardware's
67186705Sml29623  * interrupt bugs; The Neptune hardware may generate spurious interrupts when
67196705Sml29623  * an interrupr handler is removed.
67206705Sml29623  */
67216705Sml29623 #define	NXGE_PCI_PORT_LOGIC_OFFSET	0x98
67226705Sml29623 #define	NXGE_PIM_RESET			(1ULL << 29)
67236705Sml29623 #define	NXGE_GLU_RESET			(1ULL << 30)
67246705Sml29623 #define	NXGE_NIU_RESET			(1ULL << 31)
67256705Sml29623 #define	NXGE_PCI_RESET_ALL		(NXGE_PIM_RESET |	\
67266705Sml29623 					NXGE_GLU_RESET |	\
67276705Sml29623 					NXGE_NIU_RESET)
67286705Sml29623 
67296705Sml29623 #define	NXGE_WAIT_QUITE_TIME		200000
67306705Sml29623 #define	NXGE_WAIT_QUITE_RETRY		40
67316705Sml29623 #define	NXGE_PCI_RESET_WAIT		1000000 /* one second */
67326705Sml29623 
67336705Sml29623 static void
67346705Sml29623 nxge_niu_peu_reset(p_nxge_t nxgep)
67356705Sml29623 {
67366705Sml29623 	uint32_t	rvalue;
67376705Sml29623 	p_nxge_hw_list_t hw_p;
67386705Sml29623 	p_nxge_t	fnxgep;
67396705Sml29623 	int		i, j;
67406705Sml29623 
67416705Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL, "==> nxge_niu_peu_reset"));
67426705Sml29623 	if ((hw_p = nxgep->nxge_hw_p) == NULL) {
67436705Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
67446705Sml29623 		    "==> nxge_niu_peu_reset: NULL hardware pointer"));
67456705Sml29623 		return;
67466705Sml29623 	}
67476705Sml29623 
67486705Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
67496705Sml29623 	    "==> nxge_niu_peu_reset: flags 0x%x link timer id %d timer id %d",
67506705Sml29623 	    hw_p->flags, nxgep->nxge_link_poll_timerid,
67516705Sml29623 	    nxgep->nxge_timerid));
67526705Sml29623 
67536705Sml29623 	MUTEX_ENTER(&hw_p->nxge_cfg_lock);
67546705Sml29623 	/*
67556705Sml29623 	 * Make sure other instances from the same hardware
67566705Sml29623 	 * stop sending PIO and in quiescent state.
67576705Sml29623 	 */
67586705Sml29623 	for (i = 0; i < NXGE_MAX_PORTS; i++) {
67596705Sml29623 		fnxgep = hw_p->nxge_p[i];
67606705Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
67616705Sml29623 		    "==> nxge_niu_peu_reset: checking entry %d "
67626705Sml29623 		    "nxgep $%p", i, fnxgep));
67636705Sml29623 #ifdef	NXGE_DEBUG
67646705Sml29623 		if (fnxgep) {
67656705Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
67666705Sml29623 			    "==> nxge_niu_peu_reset: entry %d (function %d) "
67676705Sml29623 			    "link timer id %d hw timer id %d",
67686705Sml29623 			    i, fnxgep->function_num,
67696705Sml29623 			    fnxgep->nxge_link_poll_timerid,
67706705Sml29623 			    fnxgep->nxge_timerid));
67716705Sml29623 		}
67726705Sml29623 #endif
67736705Sml29623 		if (fnxgep && fnxgep != nxgep &&
67746705Sml29623 		    (fnxgep->nxge_timerid || fnxgep->nxge_link_poll_timerid)) {
67756705Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
67766705Sml29623 			    "==> nxge_niu_peu_reset: checking $%p "
67776705Sml29623 			    "(function %d) timer ids",
67786705Sml29623 			    fnxgep, fnxgep->function_num));
67796705Sml29623 			for (j = 0; j < NXGE_WAIT_QUITE_RETRY; j++) {
67806705Sml29623 				NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
67816705Sml29623 				    "==> nxge_niu_peu_reset: waiting"));
67826705Sml29623 				NXGE_DELAY(NXGE_WAIT_QUITE_TIME);
67836705Sml29623 				if (!fnxgep->nxge_timerid &&
67846705Sml29623 				    !fnxgep->nxge_link_poll_timerid) {
67856705Sml29623 					break;
67866705Sml29623 				}
67876705Sml29623 			}
67886705Sml29623 			NXGE_DELAY(NXGE_WAIT_QUITE_TIME);
67896705Sml29623 			if (fnxgep->nxge_timerid ||
67906705Sml29623 			    fnxgep->nxge_link_poll_timerid) {
67916705Sml29623 				MUTEX_EXIT(&hw_p->nxge_cfg_lock);
67926705Sml29623 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
67936705Sml29623 				    "<== nxge_niu_peu_reset: cannot reset "
67946705Sml29623 				    "hardware (devices are still in use)"));
67956705Sml29623 				return;
67966705Sml29623 			}
67976705Sml29623 		}
67986705Sml29623 	}
67996705Sml29623 
68006705Sml29623 	if ((hw_p->flags & COMMON_RESET_NIU_PCI) != COMMON_RESET_NIU_PCI) {
68016705Sml29623 		hw_p->flags |= COMMON_RESET_NIU_PCI;
68026705Sml29623 		rvalue = pci_config_get32(nxgep->dev_regs->nxge_pciregh,
68036705Sml29623 		    NXGE_PCI_PORT_LOGIC_OFFSET);
68046705Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
68056705Sml29623 		    "nxge_niu_peu_reset: read offset 0x%x (%d) "
68066705Sml29623 		    "(data 0x%x)",
68076705Sml29623 		    NXGE_PCI_PORT_LOGIC_OFFSET,
68086705Sml29623 		    NXGE_PCI_PORT_LOGIC_OFFSET,
68096705Sml29623 		    rvalue));
68106705Sml29623 
68116705Sml29623 		rvalue |= NXGE_PCI_RESET_ALL;
68126705Sml29623 		pci_config_put32(nxgep->dev_regs->nxge_pciregh,
68136705Sml29623 		    NXGE_PCI_PORT_LOGIC_OFFSET, rvalue);
68146705Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
68156705Sml29623 		    "nxge_niu_peu_reset: RESETTING NIU: write NIU reset 0x%x",
68166705Sml29623 		    rvalue));
68176705Sml29623 
68186705Sml29623 		NXGE_DELAY(NXGE_PCI_RESET_WAIT);
68196705Sml29623 	}
68206705Sml29623 
68216705Sml29623 	MUTEX_EXIT(&hw_p->nxge_cfg_lock);
68226705Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL, "<== nxge_niu_peu_reset"));
68236705Sml29623 }
6824