xref: /onnv-gate/usr/src/uts/common/io/nxge/nxge_main.c (revision 7812:f4514b3ad5a2)
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 /*
273859Sml29623  * SunOs MT STREAMS NIU/Neptune 10Gb Ethernet Device Driver.
283859Sml29623  */
293859Sml29623 #include	<sys/nxge/nxge_impl.h>
306495Sspeer #include	<sys/nxge/nxge_hio.h>
316495Sspeer #include	<sys/nxge/nxge_rxdma.h>
323859Sml29623 #include	<sys/pcie.h>
333859Sml29623 
343859Sml29623 uint32_t 	nxge_use_partition = 0;		/* debug partition flag */
353859Sml29623 uint32_t 	nxge_dma_obp_props_only = 1;	/* use obp published props */
363859Sml29623 uint32_t 	nxge_use_rdc_intr = 1;		/* debug to assign rdc intr */
373859Sml29623 /*
385013Sml29623  * PSARC/2007/453 MSI-X interrupt limit override
395013Sml29623  * (This PSARC case is limited to MSI-X vectors
405013Sml29623  *  and SPARC platforms only).
413859Sml29623  */
425013Sml29623 #if defined(_BIG_ENDIAN)
435013Sml29623 uint32_t	nxge_msi_enable = 2;
445013Sml29623 #else
455013Sml29623 uint32_t	nxge_msi_enable = 1;
465013Sml29623 #endif
473859Sml29623 
486611Sml29623 /*
496705Sml29623  * Software workaround for a Neptune (PCI-E)
506705Sml29623  * hardware interrupt bug which the hardware
516705Sml29623  * may generate spurious interrupts after the
526705Sml29623  * device interrupt handler was removed. If this flag
536705Sml29623  * is enabled, the driver will reset the
546705Sml29623  * hardware when devices are being detached.
556705Sml29623  */
566705Sml29623 uint32_t	nxge_peu_reset_enable = 0;
576705Sml29623 
586705Sml29623 /*
596611Sml29623  * Software workaround for the hardware
606611Sml29623  * checksum bugs that affect packet transmission
616611Sml29623  * and receive:
626611Sml29623  *
636611Sml29623  * Usage of nxge_cksum_offload:
646611Sml29623  *
656611Sml29623  *  (1) nxge_cksum_offload = 0 (default):
666611Sml29623  *	- transmits packets:
676611Sml29623  *	  TCP: uses the hardware checksum feature.
686611Sml29623  *	  UDP: driver will compute the software checksum
696611Sml29623  *	       based on the partial checksum computed
706611Sml29623  *	       by the IP layer.
716611Sml29623  *	- receives packets
726611Sml29623  *	  TCP: marks packets checksum flags based on hardware result.
736611Sml29623  *	  UDP: will not mark checksum flags.
746611Sml29623  *
756611Sml29623  *  (2) nxge_cksum_offload = 1:
766611Sml29623  *	- transmit packets:
776611Sml29623  *	  TCP/UDP: uses the hardware checksum feature.
786611Sml29623  *	- receives packets
796611Sml29623  *	  TCP/UDP: marks packet checksum flags based on hardware result.
806611Sml29623  *
816611Sml29623  *  (3) nxge_cksum_offload = 2:
826611Sml29623  *	- The driver will not register its checksum capability.
836611Sml29623  *	  Checksum for both TCP and UDP will be computed
846611Sml29623  *	  by the stack.
856611Sml29623  *	- The software LSO is not allowed in this case.
866611Sml29623  *
876611Sml29623  *  (4) nxge_cksum_offload > 2:
886611Sml29623  *	- Will be treated as it is set to 2
896611Sml29623  *	  (stack will compute the checksum).
906611Sml29623  *
916611Sml29623  *  (5) If the hardware bug is fixed, this workaround
926611Sml29623  *	needs to be updated accordingly to reflect
936611Sml29623  *	the new hardware revision.
946611Sml29623  */
956611Sml29623 uint32_t	nxge_cksum_offload = 0;
966495Sspeer 
973859Sml29623 /*
983859Sml29623  * Globals: tunable parameters (/etc/system or adb)
993859Sml29623  *
1003859Sml29623  */
1013859Sml29623 uint32_t 	nxge_rbr_size = NXGE_RBR_RBB_DEFAULT;
1023859Sml29623 uint32_t 	nxge_rbr_spare_size = 0;
1033859Sml29623 uint32_t 	nxge_rcr_size = NXGE_RCR_DEFAULT;
1043859Sml29623 uint32_t 	nxge_tx_ring_size = NXGE_TX_RING_DEFAULT;
1054193Sspeer boolean_t 	nxge_no_msg = B_TRUE;		/* control message display */
1063859Sml29623 uint32_t 	nxge_no_link_notify = 0;	/* control DL_NOTIFY */
1073859Sml29623 uint32_t 	nxge_bcopy_thresh = TX_BCOPY_MAX;
1083859Sml29623 uint32_t 	nxge_dvma_thresh = TX_FASTDVMA_MIN;
1093859Sml29623 uint32_t 	nxge_dma_stream_thresh = TX_STREAM_MIN;
1103859Sml29623 uint32_t	nxge_jumbo_mtu	= TX_JUMBO_MTU;
1113859Sml29623 boolean_t	nxge_jumbo_enable = B_FALSE;
1123859Sml29623 uint16_t	nxge_rcr_timeout = NXGE_RDC_RCR_TIMEOUT;
1133859Sml29623 uint16_t	nxge_rcr_threshold = NXGE_RDC_RCR_THRESHOLD;
1143952Sml29623 nxge_tx_mode_t	nxge_tx_scheme = NXGE_USE_SERIAL;
1153859Sml29623 
1165770Sml29623 /* MAX LSO size */
1175770Sml29623 #define		NXGE_LSO_MAXLEN	65535
1185770Sml29623 uint32_t	nxge_lso_max = NXGE_LSO_MAXLEN;
1195770Sml29623 
1203859Sml29623 /*
1213859Sml29623  * Debugging flags:
1223859Sml29623  *		nxge_no_tx_lb : transmit load balancing
1233859Sml29623  *		nxge_tx_lb_policy: 0 - TCP port (default)
1243859Sml29623  *				   3 - DEST MAC
1253859Sml29623  */
1263859Sml29623 uint32_t 	nxge_no_tx_lb = 0;
1273859Sml29623 uint32_t 	nxge_tx_lb_policy = NXGE_TX_LB_TCPUDP;
1283859Sml29623 
1293859Sml29623 /*
1303859Sml29623  * Add tunable to reduce the amount of time spent in the
1313859Sml29623  * ISR doing Rx Processing.
1323859Sml29623  */
1333859Sml29623 uint32_t nxge_max_rx_pkts = 1024;
1343859Sml29623 
1353859Sml29623 /*
1363859Sml29623  * Tunables to manage the receive buffer blocks.
1373859Sml29623  *
1383859Sml29623  * nxge_rx_threshold_hi: copy all buffers.
1393859Sml29623  * nxge_rx_bcopy_size_type: receive buffer block size type.
1403859Sml29623  * nxge_rx_threshold_lo: copy only up to tunable block size type.
1413859Sml29623  */
1423859Sml29623 nxge_rxbuf_threshold_t nxge_rx_threshold_hi = NXGE_RX_COPY_6;
1433859Sml29623 nxge_rxbuf_type_t nxge_rx_buf_size_type = RCR_PKTBUFSZ_0;
1443859Sml29623 nxge_rxbuf_threshold_t nxge_rx_threshold_lo = NXGE_RX_COPY_3;
1453859Sml29623 
1466495Sspeer /* Use kmem_alloc() to allocate data buffers. */
1476909Sml29623 #if defined(_BIG_ENDIAN)
1486498Sspeer uint32_t	nxge_use_kmem_alloc = 1;
1496495Sspeer #else
1506498Sspeer uint32_t	nxge_use_kmem_alloc = 0;
1516495Sspeer #endif
1526495Sspeer 
1533859Sml29623 rtrace_t npi_rtracebuf;
1543859Sml29623 
1557126Sml29623 /*
1567126Sml29623  * The hardware sometimes fails to allow enough time for the link partner
1577126Sml29623  * to send an acknowledgement for packets that the hardware sent to it. The
1587126Sml29623  * hardware resends the packets earlier than it should be in those instances.
1597126Sml29623  * This behavior caused some switches to acknowledge the wrong packets
1607126Sml29623  * and it triggered the fatal error.
1617126Sml29623  * This software workaround is to set the replay timer to a value
1627126Sml29623  * suggested by the hardware team.
1637126Sml29623  *
1647126Sml29623  * PCI config space replay timer register:
1657126Sml29623  *     The following replay timeout value is 0xc
1667126Sml29623  *     for bit 14:18.
1677126Sml29623  */
1687126Sml29623 #define	PCI_REPLAY_TIMEOUT_CFG_OFFSET	0xb8
1697126Sml29623 #define	PCI_REPLAY_TIMEOUT_SHIFT	14
1707126Sml29623 
1717126Sml29623 uint32_t	nxge_set_replay_timer = 1;
1727126Sml29623 uint32_t	nxge_replay_timeout = 0xc;
1737126Sml29623 
1747241Sml29623 /*
1757241Sml29623  * The transmit serialization sometimes causes
1767241Sml29623  * longer sleep before calling the driver transmit
1777241Sml29623  * function as it sleeps longer than it should.
1787241Sml29623  * The performace group suggests that a time wait tunable
1797241Sml29623  * can be used to set the maximum wait time when needed
1807241Sml29623  * and the default is set to 1 tick.
1817241Sml29623  */
1827241Sml29623 uint32_t	nxge_tx_serial_maxsleep = 1;
1837241Sml29623 
1843859Sml29623 #if	defined(sun4v)
1853859Sml29623 /*
1863859Sml29623  * Hypervisor N2/NIU services information.
1873859Sml29623  */
1883859Sml29623 static hsvc_info_t niu_hsvc = {
1893859Sml29623 	HSVC_REV_1, NULL, HSVC_GROUP_NIU, NIU_MAJOR_VER,
1903859Sml29623 	NIU_MINOR_VER, "nxge"
1913859Sml29623 };
1926495Sspeer 
1936495Sspeer static int nxge_hsvc_register(p_nxge_t);
1943859Sml29623 #endif
1953859Sml29623 
1963859Sml29623 /*
1973859Sml29623  * Function Prototypes
1983859Sml29623  */
1993859Sml29623 static int nxge_attach(dev_info_t *, ddi_attach_cmd_t);
2003859Sml29623 static int nxge_detach(dev_info_t *, ddi_detach_cmd_t);
2013859Sml29623 static void nxge_unattach(p_nxge_t);
2027656SSherry.Moore@Sun.COM static int nxge_quiesce(dev_info_t *);
2033859Sml29623 
2043859Sml29623 #if NXGE_PROPERTY
2053859Sml29623 static void nxge_remove_hard_properties(p_nxge_t);
2063859Sml29623 #endif
2073859Sml29623 
2086495Sspeer /*
2096495Sspeer  * These two functions are required by nxge_hio.c
2106495Sspeer  */
2116495Sspeer extern int nxge_m_mmac_add(void *arg, mac_multi_addr_t *maddr);
2126495Sspeer extern int nxge_m_mmac_remove(void *arg, mac_addr_slot_t slot);
2137766SMichael.Speer@Sun.COM extern void nxge_grp_cleanup(p_nxge_t nxge);
2146495Sspeer 
2153859Sml29623 static nxge_status_t nxge_setup_system_dma_pages(p_nxge_t);
2163859Sml29623 
2173859Sml29623 static nxge_status_t nxge_setup_mutexes(p_nxge_t);
2183859Sml29623 static void nxge_destroy_mutexes(p_nxge_t);
2193859Sml29623 
2203859Sml29623 static nxge_status_t nxge_map_regs(p_nxge_t nxgep);
2213859Sml29623 static void nxge_unmap_regs(p_nxge_t nxgep);
2223859Sml29623 #ifdef	NXGE_DEBUG
2233859Sml29623 static void nxge_test_map_regs(p_nxge_t nxgep);
2243859Sml29623 #endif
2253859Sml29623 
2263859Sml29623 static nxge_status_t nxge_add_intrs(p_nxge_t nxgep);
2273859Sml29623 static nxge_status_t nxge_add_soft_intrs(p_nxge_t nxgep);
2283859Sml29623 static void nxge_remove_intrs(p_nxge_t nxgep);
2293859Sml29623 static void nxge_remove_soft_intrs(p_nxge_t nxgep);
2303859Sml29623 
2313859Sml29623 static nxge_status_t nxge_add_intrs_adv(p_nxge_t nxgep);
2323859Sml29623 static nxge_status_t nxge_add_intrs_adv_type(p_nxge_t, uint32_t);
2333859Sml29623 static nxge_status_t nxge_add_intrs_adv_type_fix(p_nxge_t, uint32_t);
2343859Sml29623 static void nxge_intrs_enable(p_nxge_t nxgep);
2353859Sml29623 static void nxge_intrs_disable(p_nxge_t nxgep);
2363859Sml29623 
2373859Sml29623 static void nxge_suspend(p_nxge_t);
2383859Sml29623 static nxge_status_t nxge_resume(p_nxge_t);
2393859Sml29623 
2403859Sml29623 static nxge_status_t nxge_setup_dev(p_nxge_t);
2413859Sml29623 static void nxge_destroy_dev(p_nxge_t);
2423859Sml29623 
2433859Sml29623 static nxge_status_t nxge_alloc_mem_pool(p_nxge_t);
2443859Sml29623 static void nxge_free_mem_pool(p_nxge_t);
2453859Sml29623 
2466495Sspeer nxge_status_t nxge_alloc_rx_mem_pool(p_nxge_t);
2473859Sml29623 static void nxge_free_rx_mem_pool(p_nxge_t);
2483859Sml29623 
2496495Sspeer nxge_status_t nxge_alloc_tx_mem_pool(p_nxge_t);
2503859Sml29623 static void nxge_free_tx_mem_pool(p_nxge_t);
2513859Sml29623 
2523859Sml29623 static nxge_status_t nxge_dma_mem_alloc(p_nxge_t, dma_method_t,
2533859Sml29623 	struct ddi_dma_attr *,
2543859Sml29623 	size_t, ddi_device_acc_attr_t *, uint_t,
2553859Sml29623 	p_nxge_dma_common_t);
2563859Sml29623 
2573859Sml29623 static void nxge_dma_mem_free(p_nxge_dma_common_t);
2586495Sspeer static void nxge_dma_free_rx_data_buf(p_nxge_dma_common_t);
2593859Sml29623 
2603859Sml29623 static nxge_status_t nxge_alloc_rx_buf_dma(p_nxge_t, uint16_t,
2613859Sml29623 	p_nxge_dma_common_t *, size_t, size_t, uint32_t *);
2623859Sml29623 static void nxge_free_rx_buf_dma(p_nxge_t, p_nxge_dma_common_t, uint32_t);
2633859Sml29623 
2643859Sml29623 static nxge_status_t nxge_alloc_rx_cntl_dma(p_nxge_t, uint16_t,
2653859Sml29623 	p_nxge_dma_common_t *, size_t);
2663859Sml29623 static void nxge_free_rx_cntl_dma(p_nxge_t, p_nxge_dma_common_t);
2673859Sml29623 
2686495Sspeer extern nxge_status_t nxge_alloc_tx_buf_dma(p_nxge_t, uint16_t,
2693859Sml29623 	p_nxge_dma_common_t *, size_t, size_t, uint32_t *);
2703859Sml29623 static void nxge_free_tx_buf_dma(p_nxge_t, p_nxge_dma_common_t, uint32_t);
2713859Sml29623 
2726495Sspeer extern nxge_status_t nxge_alloc_tx_cntl_dma(p_nxge_t, uint16_t,
2733859Sml29623 	p_nxge_dma_common_t *,
2743859Sml29623 	size_t);
2753859Sml29623 static void nxge_free_tx_cntl_dma(p_nxge_t, p_nxge_dma_common_t);
2763859Sml29623 
2773859Sml29623 static int nxge_init_common_dev(p_nxge_t);
2783859Sml29623 static void nxge_uninit_common_dev(p_nxge_t);
2796512Ssowmini extern int nxge_param_set_mac(p_nxge_t, queue_t *, mblk_t *,
2806512Ssowmini     char *, caddr_t);
2813859Sml29623 
2823859Sml29623 /*
2833859Sml29623  * The next declarations are for the GLDv3 interface.
2843859Sml29623  */
2853859Sml29623 static int nxge_m_start(void *);
2863859Sml29623 static void nxge_m_stop(void *);
2873859Sml29623 static int nxge_m_unicst(void *, const uint8_t *);
2883859Sml29623 static int nxge_m_multicst(void *, boolean_t, const uint8_t *);
2893859Sml29623 static int nxge_m_promisc(void *, boolean_t);
2903859Sml29623 static void nxge_m_ioctl(void *, queue_t *, mblk_t *);
2913859Sml29623 static void nxge_m_resources(void *);
2923859Sml29623 mblk_t *nxge_m_tx(void *arg, mblk_t *);
2933859Sml29623 static nxge_status_t nxge_mac_register(p_nxge_t);
2943859Sml29623 static int nxge_altmac_set(p_nxge_t nxgep, uint8_t *mac_addr,
2953859Sml29623 	mac_addr_slot_t slot);
2966495Sspeer void nxge_mmac_kstat_update(p_nxge_t nxgep, mac_addr_slot_t slot,
2973859Sml29623 	boolean_t factory);
2983859Sml29623 static int nxge_m_mmac_reserve(void *arg, mac_multi_addr_t *maddr);
2993859Sml29623 static int nxge_m_mmac_modify(void *arg, mac_multi_addr_t *maddr);
3003859Sml29623 static int nxge_m_mmac_get(void *arg, mac_multi_addr_t *maddr);
3016439Sml29623 static	boolean_t nxge_m_getcapab(void *, mac_capab_t, void *);
3026439Sml29623 static int nxge_m_setprop(void *, const char *, mac_prop_id_t,
3036439Sml29623     uint_t, const void *);
3046439Sml29623 static int nxge_m_getprop(void *, const char *, mac_prop_id_t,
3056512Ssowmini     uint_t, uint_t, void *);
3066439Sml29623 static int nxge_set_priv_prop(nxge_t *, const char *, uint_t,
3076439Sml29623     const void *);
3086512Ssowmini static int nxge_get_priv_prop(nxge_t *, const char *, uint_t, uint_t,
3096439Sml29623     void *);
3106512Ssowmini static int nxge_get_def_val(nxge_t *, mac_prop_id_t, uint_t, void *);
3116512Ssowmini 
3126705Sml29623 static void nxge_niu_peu_reset(p_nxge_t nxgep);
3137126Sml29623 static void nxge_set_pci_replay_timeout(nxge_t *);
3146512Ssowmini 
3156512Ssowmini mac_priv_prop_t nxge_priv_props[] = {
3166512Ssowmini 	{"_adv_10gfdx_cap", MAC_PROP_PERM_RW},
3176512Ssowmini 	{"_adv_pause_cap", MAC_PROP_PERM_RW},
3186512Ssowmini 	{"_function_number", MAC_PROP_PERM_READ},
3196512Ssowmini 	{"_fw_version", MAC_PROP_PERM_READ},
3206512Ssowmini 	{"_port_mode", MAC_PROP_PERM_READ},
3216512Ssowmini 	{"_hot_swap_phy", MAC_PROP_PERM_READ},
3226512Ssowmini 	{"_accept_jumbo", MAC_PROP_PERM_RW},
3236512Ssowmini 	{"_rxdma_intr_time", MAC_PROP_PERM_RW},
3246512Ssowmini 	{"_rxdma_intr_pkts", MAC_PROP_PERM_RW},
3256512Ssowmini 	{"_class_opt_ipv4_tcp", MAC_PROP_PERM_RW},
3266512Ssowmini 	{"_class_opt_ipv4_udp", MAC_PROP_PERM_RW},
3276512Ssowmini 	{"_class_opt_ipv4_ah", MAC_PROP_PERM_RW},
3286512Ssowmini 	{"_class_opt_ipv4_sctp", MAC_PROP_PERM_RW},
3296512Ssowmini 	{"_class_opt_ipv6_tcp", MAC_PROP_PERM_RW},
3306512Ssowmini 	{"_class_opt_ipv6_udp", MAC_PROP_PERM_RW},
3316512Ssowmini 	{"_class_opt_ipv6_ah", MAC_PROP_PERM_RW},
3326512Ssowmini 	{"_class_opt_ipv6_sctp", MAC_PROP_PERM_RW},
3336512Ssowmini 	{"_soft_lso_enable", MAC_PROP_PERM_RW}
3346512Ssowmini };
3356512Ssowmini 
3366512Ssowmini #define	NXGE_MAX_PRIV_PROPS	\
3376512Ssowmini 	(sizeof (nxge_priv_props)/sizeof (mac_priv_prop_t))
3386439Sml29623 
3396439Sml29623 #define	NXGE_M_CALLBACK_FLAGS\
3406439Sml29623 	(MC_RESOURCES | MC_IOCTL | MC_GETCAPAB | MC_SETPROP | MC_GETPROP)
3416439Sml29623 
3423859Sml29623 
3433859Sml29623 #define	NXGE_NEPTUNE_MAGIC	0x4E584745UL
3443859Sml29623 #define	MAX_DUMP_SZ 256
3453859Sml29623 
3466439Sml29623 #define	NXGE_M_CALLBACK_FLAGS	\
3476439Sml29623 	(MC_RESOURCES | MC_IOCTL | MC_GETCAPAB | MC_SETPROP | MC_GETPROP)
3486439Sml29623 
3496495Sspeer mac_callbacks_t nxge_m_callbacks = {
3503859Sml29623 	NXGE_M_CALLBACK_FLAGS,
3513859Sml29623 	nxge_m_stat,
3523859Sml29623 	nxge_m_start,
3533859Sml29623 	nxge_m_stop,
3543859Sml29623 	nxge_m_promisc,
3553859Sml29623 	nxge_m_multicst,
3563859Sml29623 	nxge_m_unicst,
3573859Sml29623 	nxge_m_tx,
3583859Sml29623 	nxge_m_resources,
3593859Sml29623 	nxge_m_ioctl,
3606439Sml29623 	nxge_m_getcapab,
3616439Sml29623 	NULL,
3626439Sml29623 	NULL,
3636439Sml29623 	nxge_m_setprop,
3646439Sml29623 	nxge_m_getprop
3653859Sml29623 };
3663859Sml29623 
3673859Sml29623 void
3683859Sml29623 nxge_err_inject(p_nxge_t, queue_t *, mblk_t *);
3693859Sml29623 
3705013Sml29623 /* PSARC/2007/453 MSI-X interrupt limit override. */
3715013Sml29623 #define	NXGE_MSIX_REQUEST_10G	8
3725013Sml29623 #define	NXGE_MSIX_REQUEST_1G	2
3735013Sml29623 static int nxge_create_msi_property(p_nxge_t);
3745013Sml29623 
3753859Sml29623 /*
3763859Sml29623  * These global variables control the message
3773859Sml29623  * output.
3783859Sml29623  */
3793859Sml29623 out_dbgmsg_t nxge_dbgmsg_out = DBG_CONSOLE | STR_LOG;
3806495Sspeer uint64_t nxge_debug_level;
3813859Sml29623 
3823859Sml29623 /*
3833859Sml29623  * This list contains the instance structures for the Neptune
3843859Sml29623  * devices present in the system. The lock exists to guarantee
3853859Sml29623  * mutually exclusive access to the list.
3863859Sml29623  */
3873859Sml29623 void 			*nxge_list = NULL;
3883859Sml29623 
3893859Sml29623 void			*nxge_hw_list = NULL;
3903859Sml29623 nxge_os_mutex_t 	nxge_common_lock;
3913859Sml29623 
3923859Sml29623 extern uint64_t 	npi_debug_level;
3933859Sml29623 
3943859Sml29623 extern nxge_status_t	nxge_ldgv_init(p_nxge_t, int *, int *);
3953859Sml29623 extern nxge_status_t	nxge_ldgv_init_n2(p_nxge_t, int *, int *);
3963859Sml29623 extern nxge_status_t	nxge_ldgv_uninit(p_nxge_t);
3973859Sml29623 extern nxge_status_t	nxge_intr_ldgv_init(p_nxge_t);
3983859Sml29623 extern void		nxge_fm_init(p_nxge_t,
3993859Sml29623 					ddi_device_acc_attr_t *,
4003859Sml29623 					ddi_device_acc_attr_t *,
4013859Sml29623 					ddi_dma_attr_t *);
4023859Sml29623 extern void		nxge_fm_fini(p_nxge_t);
4033859Sml29623 extern npi_status_t	npi_mac_altaddr_disable(npi_handle_t, uint8_t, uint8_t);
4043859Sml29623 
4053859Sml29623 /*
4063859Sml29623  * Count used to maintain the number of buffers being used
4073859Sml29623  * by Neptune instances and loaned up to the upper layers.
4083859Sml29623  */
4093859Sml29623 uint32_t nxge_mblks_pending = 0;
4103859Sml29623 
4113859Sml29623 /*
4123859Sml29623  * Device register access attributes for PIO.
4133859Sml29623  */
4143859Sml29623 static ddi_device_acc_attr_t nxge_dev_reg_acc_attr = {
4153859Sml29623 	DDI_DEVICE_ATTR_V0,
4163859Sml29623 	DDI_STRUCTURE_LE_ACC,
4173859Sml29623 	DDI_STRICTORDER_ACC,
4183859Sml29623 };
4193859Sml29623 
4203859Sml29623 /*
4213859Sml29623  * Device descriptor access attributes for DMA.
4223859Sml29623  */
4233859Sml29623 static ddi_device_acc_attr_t nxge_dev_desc_dma_acc_attr = {
4243859Sml29623 	DDI_DEVICE_ATTR_V0,
4253859Sml29623 	DDI_STRUCTURE_LE_ACC,
4263859Sml29623 	DDI_STRICTORDER_ACC
4273859Sml29623 };
4283859Sml29623 
4293859Sml29623 /*
4303859Sml29623  * Device buffer access attributes for DMA.
4313859Sml29623  */
4323859Sml29623 static ddi_device_acc_attr_t nxge_dev_buf_dma_acc_attr = {
4333859Sml29623 	DDI_DEVICE_ATTR_V0,
4343859Sml29623 	DDI_STRUCTURE_BE_ACC,
4353859Sml29623 	DDI_STRICTORDER_ACC
4363859Sml29623 };
4373859Sml29623 
4383859Sml29623 ddi_dma_attr_t nxge_desc_dma_attr = {
4393859Sml29623 	DMA_ATTR_V0,		/* version number. */
4403859Sml29623 	0,			/* low address */
4413859Sml29623 	0xffffffffffffffff,	/* high address */
4423859Sml29623 	0xffffffffffffffff,	/* address counter max */
4433859Sml29623 #ifndef NIU_PA_WORKAROUND
4443859Sml29623 	0x100000,		/* alignment */
4453859Sml29623 #else
4463859Sml29623 	0x2000,
4473859Sml29623 #endif
4483859Sml29623 	0xfc00fc,		/* dlim_burstsizes */
4493859Sml29623 	0x1,			/* minimum transfer size */
4503859Sml29623 	0xffffffffffffffff,	/* maximum transfer size */
4513859Sml29623 	0xffffffffffffffff,	/* maximum segment size */
4523859Sml29623 	1,			/* scatter/gather list length */
4533859Sml29623 	(unsigned int) 1,	/* granularity */
4543859Sml29623 	0			/* attribute flags */
4553859Sml29623 };
4563859Sml29623 
4573859Sml29623 ddi_dma_attr_t nxge_tx_dma_attr = {
4583859Sml29623 	DMA_ATTR_V0,		/* version number. */
4593859Sml29623 	0,			/* low address */
4603859Sml29623 	0xffffffffffffffff,	/* high address */
4613859Sml29623 	0xffffffffffffffff,	/* address counter max */
4623859Sml29623 #if defined(_BIG_ENDIAN)
4633859Sml29623 	0x2000,			/* alignment */
4643859Sml29623 #else
4653859Sml29623 	0x1000,			/* alignment */
4663859Sml29623 #endif
4673859Sml29623 	0xfc00fc,		/* dlim_burstsizes */
4683859Sml29623 	0x1,			/* minimum transfer size */
4693859Sml29623 	0xffffffffffffffff,	/* maximum transfer size */
4703859Sml29623 	0xffffffffffffffff,	/* maximum segment size */
4713859Sml29623 	5,			/* scatter/gather list length */
4723859Sml29623 	(unsigned int) 1,	/* granularity */
4733859Sml29623 	0			/* attribute flags */
4743859Sml29623 };
4753859Sml29623 
4763859Sml29623 ddi_dma_attr_t nxge_rx_dma_attr = {
4773859Sml29623 	DMA_ATTR_V0,		/* version number. */
4783859Sml29623 	0,			/* low address */
4793859Sml29623 	0xffffffffffffffff,	/* high address */
4803859Sml29623 	0xffffffffffffffff,	/* address counter max */
4813859Sml29623 	0x2000,			/* alignment */
4823859Sml29623 	0xfc00fc,		/* dlim_burstsizes */
4833859Sml29623 	0x1,			/* minimum transfer size */
4843859Sml29623 	0xffffffffffffffff,	/* maximum transfer size */
4853859Sml29623 	0xffffffffffffffff,	/* maximum segment size */
4863859Sml29623 	1,			/* scatter/gather list length */
4873859Sml29623 	(unsigned int) 1,	/* granularity */
4884781Ssbehera 	DDI_DMA_RELAXED_ORDERING /* attribute flags */
4893859Sml29623 };
4903859Sml29623 
4913859Sml29623 ddi_dma_lim_t nxge_dma_limits = {
4923859Sml29623 	(uint_t)0,		/* dlim_addr_lo */
4933859Sml29623 	(uint_t)0xffffffff,	/* dlim_addr_hi */
4943859Sml29623 	(uint_t)0xffffffff,	/* dlim_cntr_max */
4953859Sml29623 	(uint_t)0xfc00fc,	/* dlim_burstsizes for 32 and 64 bit xfers */
4963859Sml29623 	0x1,			/* dlim_minxfer */
4973859Sml29623 	1024			/* dlim_speed */
4983859Sml29623 };
4993859Sml29623 
5003859Sml29623 dma_method_t nxge_force_dma = DVMA;
5013859Sml29623 
5023859Sml29623 /*
5033859Sml29623  * dma chunk sizes.
5043859Sml29623  *
5053859Sml29623  * Try to allocate the largest possible size
5063859Sml29623  * so that fewer number of dma chunks would be managed
5073859Sml29623  */
5083859Sml29623 #ifdef NIU_PA_WORKAROUND
5093859Sml29623 size_t alloc_sizes [] = {0x2000};
5103859Sml29623 #else
5113859Sml29623 size_t alloc_sizes [] = {0x1000, 0x2000, 0x4000, 0x8000,
5123859Sml29623 		0x10000, 0x20000, 0x40000, 0x80000,
5135770Sml29623 		0x100000, 0x200000, 0x400000, 0x800000,
5145770Sml29623 		0x1000000, 0x2000000, 0x4000000};
5153859Sml29623 #endif
5163859Sml29623 
5173859Sml29623 /*
5183859Sml29623  * Translate "dev_t" to a pointer to the associated "dev_info_t".
5193859Sml29623  */
5203859Sml29623 
5216495Sspeer extern void nxge_get_environs(nxge_t *);
5226495Sspeer 
5233859Sml29623 static int
5243859Sml29623 nxge_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5253859Sml29623 {
5263859Sml29623 	p_nxge_t	nxgep = NULL;
5273859Sml29623 	int		instance;
5283859Sml29623 	int		status = DDI_SUCCESS;
5293859Sml29623 	uint8_t		portn;
5303859Sml29623 	nxge_mmac_t	*mmac_info;
5317529SSriharsha.Basavapatna@Sun.COM 	p_nxge_param_t	param_arr;
5323859Sml29623 
5333859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_attach"));
5343859Sml29623 
5353859Sml29623 	/*
5363859Sml29623 	 * Get the device instance since we'll need to setup
5373859Sml29623 	 * or retrieve a soft state for this instance.
5383859Sml29623 	 */
5393859Sml29623 	instance = ddi_get_instance(dip);
5403859Sml29623 
5413859Sml29623 	switch (cmd) {
5423859Sml29623 	case DDI_ATTACH:
5433859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_ATTACH"));
5443859Sml29623 		break;
5453859Sml29623 
5463859Sml29623 	case DDI_RESUME:
5473859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_RESUME"));
5483859Sml29623 		nxgep = (p_nxge_t)ddi_get_soft_state(nxge_list, instance);
5493859Sml29623 		if (nxgep == NULL) {
5503859Sml29623 			status = DDI_FAILURE;
5513859Sml29623 			break;
5523859Sml29623 		}
5533859Sml29623 		if (nxgep->dip != dip) {
5543859Sml29623 			status = DDI_FAILURE;
5553859Sml29623 			break;
5563859Sml29623 		}
5573859Sml29623 		if (nxgep->suspended == DDI_PM_SUSPEND) {
5583859Sml29623 			status = ddi_dev_is_needed(nxgep->dip, 0, 1);
5593859Sml29623 		} else {
5604185Sspeer 			status = nxge_resume(nxgep);
5613859Sml29623 		}
5623859Sml29623 		goto nxge_attach_exit;
5633859Sml29623 
5643859Sml29623 	case DDI_PM_RESUME:
5653859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_PM_RESUME"));
5663859Sml29623 		nxgep = (p_nxge_t)ddi_get_soft_state(nxge_list, instance);
5673859Sml29623 		if (nxgep == NULL) {
5683859Sml29623 			status = DDI_FAILURE;
5693859Sml29623 			break;
5703859Sml29623 		}
5713859Sml29623 		if (nxgep->dip != dip) {
5723859Sml29623 			status = DDI_FAILURE;
5733859Sml29623 			break;
5743859Sml29623 		}
5754185Sspeer 		status = nxge_resume(nxgep);
5763859Sml29623 		goto nxge_attach_exit;
5773859Sml29623 
5783859Sml29623 	default:
5793859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing unknown"));
5803859Sml29623 		status = DDI_FAILURE;
5813859Sml29623 		goto nxge_attach_exit;
5823859Sml29623 	}
5833859Sml29623 
5843859Sml29623 
5853859Sml29623 	if (ddi_soft_state_zalloc(nxge_list, instance) == DDI_FAILURE) {
5863859Sml29623 		status = DDI_FAILURE;
5873859Sml29623 		goto nxge_attach_exit;
5883859Sml29623 	}
5893859Sml29623 
5903859Sml29623 	nxgep = ddi_get_soft_state(nxge_list, instance);
5913859Sml29623 	if (nxgep == NULL) {
5924977Sraghus 		status = NXGE_ERROR;
5934977Sraghus 		goto nxge_attach_fail2;
5943859Sml29623 	}
5953859Sml29623 
5964693Stm144005 	nxgep->nxge_magic = NXGE_MAGIC;
5974693Stm144005 
5983859Sml29623 	nxgep->drv_state = 0;
5993859Sml29623 	nxgep->dip = dip;
6003859Sml29623 	nxgep->instance = instance;
6013859Sml29623 	nxgep->p_dip = ddi_get_parent(dip);
6023859Sml29623 	nxgep->nxge_debug_level = nxge_debug_level;
6033859Sml29623 	npi_debug_level = nxge_debug_level;
6043859Sml29623 
6056495Sspeer 	/* Are we a guest running in a Hybrid I/O environment? */
6066495Sspeer 	nxge_get_environs(nxgep);
6073859Sml29623 
6083859Sml29623 	status = nxge_map_regs(nxgep);
6096495Sspeer 
6103859Sml29623 	if (status != NXGE_OK) {
6113859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "nxge_map_regs failed"));
6124977Sraghus 		goto nxge_attach_fail3;
6133859Sml29623 	}
6143859Sml29623 
6156495Sspeer 	nxge_fm_init(nxgep, &nxge_dev_reg_acc_attr,
6166495Sspeer 	    &nxge_dev_desc_dma_acc_attr,
6176495Sspeer 	    &nxge_rx_dma_attr);
6186495Sspeer 
6196495Sspeer 	/* Create & initialize the per-Neptune data structure */
6206495Sspeer 	/* (even if we're a guest). */
6213859Sml29623 	status = nxge_init_common_dev(nxgep);
6223859Sml29623 	if (status != NXGE_OK) {
6233859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
6246512Ssowmini 		    "nxge_init_common_dev failed"));
6254977Sraghus 		goto nxge_attach_fail4;
6263859Sml29623 	}
6273859Sml29623 
6287126Sml29623 	/*
6297126Sml29623 	 * Software workaround: set the replay timer.
6307126Sml29623 	 */
6317126Sml29623 	if (nxgep->niu_type != N2_NIU) {
6327126Sml29623 		nxge_set_pci_replay_timeout(nxgep);
6337126Sml29623 	}
6347126Sml29623 
6356495Sspeer #if defined(sun4v)
6366495Sspeer 	/* This is required by nxge_hio_init(), which follows. */
6376495Sspeer 	if ((status = nxge_hsvc_register(nxgep)) != DDI_SUCCESS)
6387587SMichael.Speer@Sun.COM 		goto nxge_attach_fail4;
6396495Sspeer #endif
6406495Sspeer 
6416495Sspeer 	if ((status = nxge_hio_init(nxgep)) != NXGE_OK) {
6426495Sspeer 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
6436512Ssowmini 		    "nxge_hio_init failed"));
6446495Sspeer 		goto nxge_attach_fail4;
6456495Sspeer 	}
6466495Sspeer 
6474732Sdavemq 	if (nxgep->niu_type == NEPTUNE_2_10GF) {
6484732Sdavemq 		if (nxgep->function_num > 1) {
6496028Ssbehera 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "Unsupported"
6504732Sdavemq 			    " function %d. Only functions 0 and 1 are "
6514732Sdavemq 			    "supported for this card.", nxgep->function_num));
6524732Sdavemq 			status = NXGE_ERROR;
6534977Sraghus 			goto nxge_attach_fail4;
6544732Sdavemq 		}
6554732Sdavemq 	}
6564732Sdavemq 
6576495Sspeer 	if (isLDOMguest(nxgep)) {
6586495Sspeer 		/*
6596495Sspeer 		 * Use the function number here.
6606495Sspeer 		 */
6616495Sspeer 		nxgep->mac.portnum = nxgep->function_num;
6626495Sspeer 		nxgep->mac.porttype = PORT_TYPE_LOGICAL;
6636495Sspeer 
6646495Sspeer 		/* XXX We'll set the MAC address counts to 1 for now. */
6656495Sspeer 		mmac_info = &nxgep->nxge_mmac_info;
6666495Sspeer 		mmac_info->num_mmac = 1;
6676495Sspeer 		mmac_info->naddrfree = 1;
6683859Sml29623 	} else {
6696495Sspeer 		portn = NXGE_GET_PORT_NUM(nxgep->function_num);
6706495Sspeer 		nxgep->mac.portnum = portn;
6716495Sspeer 		if ((portn == 0) || (portn == 1))
6726495Sspeer 			nxgep->mac.porttype = PORT_TYPE_XMAC;
6736495Sspeer 		else
6746495Sspeer 			nxgep->mac.porttype = PORT_TYPE_BMAC;
6756495Sspeer 		/*
6766495Sspeer 		 * Neptune has 4 ports, the first 2 ports use XMAC (10G MAC)
6776495Sspeer 		 * internally, the rest 2 ports use BMAC (1G "Big" MAC).
6786495Sspeer 		 * The two types of MACs have different characterizations.
6796495Sspeer 		 */
6806495Sspeer 		mmac_info = &nxgep->nxge_mmac_info;
6816495Sspeer 		if (nxgep->function_num < 2) {
6826495Sspeer 			mmac_info->num_mmac = XMAC_MAX_ALT_ADDR_ENTRY;
6836495Sspeer 			mmac_info->naddrfree = XMAC_MAX_ALT_ADDR_ENTRY;
6846495Sspeer 		} else {
6856495Sspeer 			mmac_info->num_mmac = BMAC_MAX_ALT_ADDR_ENTRY;
6866495Sspeer 			mmac_info->naddrfree = BMAC_MAX_ALT_ADDR_ENTRY;
6876495Sspeer 		}
6883859Sml29623 	}
6893859Sml29623 	/*
6903859Sml29623 	 * Setup the Ndd parameters for the this instance.
6913859Sml29623 	 */
6923859Sml29623 	nxge_init_param(nxgep);
6933859Sml29623 
6943859Sml29623 	/*
6953859Sml29623 	 * Setup Register Tracing Buffer.
6963859Sml29623 	 */
6973859Sml29623 	npi_rtrace_buf_init((rtrace_t *)&npi_rtracebuf);
6983859Sml29623 
6993859Sml29623 	/* init stats ptr */
7003859Sml29623 	nxge_init_statsp(nxgep);
7014185Sspeer 
7024977Sraghus 	/*
7036495Sspeer 	 * Copy the vpd info from eeprom to a local data
7046495Sspeer 	 * structure, and then check its validity.
7054977Sraghus 	 */
7066495Sspeer 	if (!isLDOMguest(nxgep)) {
7076495Sspeer 		int *regp;
7086495Sspeer 		uint_t reglen;
7096495Sspeer 		int rv;
7106495Sspeer 
7116495Sspeer 		nxge_vpd_info_get(nxgep);
7126495Sspeer 
7136495Sspeer 		/* Find the NIU config handle. */
7146495Sspeer 		rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
7156495Sspeer 		    ddi_get_parent(nxgep->dip), DDI_PROP_DONTPASS,
7166495Sspeer 		    "reg", &regp, &reglen);
7176495Sspeer 
7186495Sspeer 		if (rv != DDI_PROP_SUCCESS) {
7196495Sspeer 			goto nxge_attach_fail5;
7206495Sspeer 		}
7216495Sspeer 		/*
7226495Sspeer 		 * The address_hi, that is the first int, in the reg
7236495Sspeer 		 * property consists of config handle, but need to remove
7246495Sspeer 		 * the bits 28-31 which are OBP specific info.
7256495Sspeer 		 */
7266495Sspeer 		nxgep->niu_cfg_hdl = (*regp) & 0xFFFFFFF;
7276495Sspeer 		ddi_prop_free(regp);
7286495Sspeer 	}
7296495Sspeer 
7306495Sspeer 	if (isLDOMguest(nxgep)) {
7316495Sspeer 		uchar_t *prop_val;
7326495Sspeer 		uint_t prop_len;
7337529SSriharsha.Basavapatna@Sun.COM 		uint32_t max_frame_size;
7346495Sspeer 
7356495Sspeer 		extern void nxge_get_logical_props(p_nxge_t);
7366495Sspeer 
7376495Sspeer 		nxgep->statsp->mac_stats.xcvr_inuse = LOGICAL_XCVR;
7386495Sspeer 		nxgep->mac.portmode = PORT_LOGICAL;
7396495Sspeer 		(void) ddi_prop_update_string(DDI_DEV_T_NONE, nxgep->dip,
7406495Sspeer 		    "phy-type", "virtual transceiver");
7416495Sspeer 
7426495Sspeer 		nxgep->nports = 1;
7436495Sspeer 		nxgep->board_ver = 0;	/* XXX What? */
7446495Sspeer 
7456495Sspeer 		/*
7466495Sspeer 		 * local-mac-address property gives us info on which
7476495Sspeer 		 * specific MAC address the Hybrid resource is associated
7486495Sspeer 		 * with.
7496495Sspeer 		 */
7506495Sspeer 		if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, nxgep->dip, 0,
7516495Sspeer 		    "local-mac-address", &prop_val,
7526495Sspeer 		    &prop_len) != DDI_PROP_SUCCESS) {
7536495Sspeer 			goto nxge_attach_fail5;
7546495Sspeer 		}
7556495Sspeer 		if (prop_len !=  ETHERADDRL) {
7566495Sspeer 			ddi_prop_free(prop_val);
7576495Sspeer 			goto nxge_attach_fail5;
7586495Sspeer 		}
7596495Sspeer 		ether_copy(prop_val, nxgep->hio_mac_addr);
7606495Sspeer 		ddi_prop_free(prop_val);
7616495Sspeer 		nxge_get_logical_props(nxgep);
7626495Sspeer 
7637529SSriharsha.Basavapatna@Sun.COM 		/*
7647529SSriharsha.Basavapatna@Sun.COM 		 * Enable Jumbo property based on the "max-frame-size"
7657529SSriharsha.Basavapatna@Sun.COM 		 * property value.
7667529SSriharsha.Basavapatna@Sun.COM 		 */
7677529SSriharsha.Basavapatna@Sun.COM 		max_frame_size = ddi_prop_get_int(DDI_DEV_T_ANY,
7687529SSriharsha.Basavapatna@Sun.COM 		    nxgep->dip, DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
7697529SSriharsha.Basavapatna@Sun.COM 		    "max-frame-size", NXGE_MTU_DEFAULT_MAX);
7707529SSriharsha.Basavapatna@Sun.COM 		if ((max_frame_size > NXGE_MTU_DEFAULT_MAX) &&
7717529SSriharsha.Basavapatna@Sun.COM 		    (max_frame_size <= TX_JUMBO_MTU)) {
7727529SSriharsha.Basavapatna@Sun.COM 			param_arr = nxgep->param_arr;
7737529SSriharsha.Basavapatna@Sun.COM 
7747529SSriharsha.Basavapatna@Sun.COM 			param_arr[param_accept_jumbo].value = 1;
7757529SSriharsha.Basavapatna@Sun.COM 			nxgep->mac.is_jumbo = B_TRUE;
7767529SSriharsha.Basavapatna@Sun.COM 			nxgep->mac.maxframesize = (uint16_t)max_frame_size;
7777529SSriharsha.Basavapatna@Sun.COM 			nxgep->mac.default_mtu = nxgep->mac.maxframesize -
7787529SSriharsha.Basavapatna@Sun.COM 			    NXGE_EHEADER_VLAN_CRC;
7797529SSriharsha.Basavapatna@Sun.COM 		}
7806495Sspeer 	} else {
7816495Sspeer 		status = nxge_xcvr_find(nxgep);
7826495Sspeer 
7836495Sspeer 		if (status != NXGE_OK) {
7846495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "nxge_attach: "
7856512Ssowmini 			    " Couldn't determine card type"
7866512Ssowmini 			    " .... exit "));
7876495Sspeer 			goto nxge_attach_fail5;
7886495Sspeer 		}
7896495Sspeer 
7906495Sspeer 		status = nxge_get_config_properties(nxgep);
7916495Sspeer 
7926495Sspeer 		if (status != NXGE_OK) {
7936495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
7946512Ssowmini 			    "get_hw create failed"));
7956495Sspeer 			goto nxge_attach_fail;
7966495Sspeer 		}
7973859Sml29623 	}
7983859Sml29623 
7993859Sml29623 	/*
8003859Sml29623 	 * Setup the Kstats for the driver.
8013859Sml29623 	 */
8023859Sml29623 	nxge_setup_kstats(nxgep);
8033859Sml29623 
8046495Sspeer 	if (!isLDOMguest(nxgep))
8056495Sspeer 		nxge_setup_param(nxgep);
8063859Sml29623 
8073859Sml29623 	status = nxge_setup_system_dma_pages(nxgep);
8083859Sml29623 	if (status != NXGE_OK) {
8093859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "set dma page failed"));
8103859Sml29623 		goto nxge_attach_fail;
8113859Sml29623 	}
8123859Sml29623 
8133859Sml29623 	nxge_hw_id_init(nxgep);
8146495Sspeer 
8156495Sspeer 	if (!isLDOMguest(nxgep))
8166495Sspeer 		nxge_hw_init_niu_common(nxgep);
8173859Sml29623 
8183859Sml29623 	status = nxge_setup_mutexes(nxgep);
8193859Sml29623 	if (status != NXGE_OK) {
8203859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "set mutex failed"));
8213859Sml29623 		goto nxge_attach_fail;
8223859Sml29623 	}
8233859Sml29623 
8246495Sspeer #if defined(sun4v)
8256495Sspeer 	if (isLDOMguest(nxgep)) {
8266495Sspeer 		/* Find our VR & channel sets. */
8276495Sspeer 		status = nxge_hio_vr_add(nxgep);
828*7812SMichael.Speer@Sun.COM 		if (status != NXGE_OK) {
829*7812SMichael.Speer@Sun.COM 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
830*7812SMichael.Speer@Sun.COM 			    "nxge_hio_vr_add failed"));
831*7812SMichael.Speer@Sun.COM 			(void) hsvc_unregister(&nxgep->niu_hsvc);
832*7812SMichael.Speer@Sun.COM 			nxgep->niu_hsvc_available = B_FALSE;
833*7812SMichael.Speer@Sun.COM 		}
8346495Sspeer 		goto nxge_attach_exit;
8356495Sspeer 	}
8366495Sspeer #endif
8376495Sspeer 
8383859Sml29623 	status = nxge_setup_dev(nxgep);
8393859Sml29623 	if (status != DDI_SUCCESS) {
8403859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "set dev failed"));
8413859Sml29623 		goto nxge_attach_fail;
8423859Sml29623 	}
8433859Sml29623 
8443859Sml29623 	status = nxge_add_intrs(nxgep);
8453859Sml29623 	if (status != DDI_SUCCESS) {
8463859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "add_intr failed"));
8473859Sml29623 		goto nxge_attach_fail;
8483859Sml29623 	}
849*7812SMichael.Speer@Sun.COM 
8503859Sml29623 	status = nxge_add_soft_intrs(nxgep);
8513859Sml29623 	if (status != DDI_SUCCESS) {
8526495Sspeer 		NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
8536495Sspeer 		    "add_soft_intr failed"));
8543859Sml29623 		goto nxge_attach_fail;
8553859Sml29623 	}
8563859Sml29623 
8576835Syc148097 	/* If a guest, register with vio_net instead. */
8584977Sraghus 	if ((status = nxge_mac_register(nxgep)) != NXGE_OK) {
8593859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
8606495Sspeer 		    "unable to register to mac layer (%d)", status));
8613859Sml29623 		goto nxge_attach_fail;
8623859Sml29623 	}
8633859Sml29623 
8643859Sml29623 	mac_link_update(nxgep->mach, LINK_STATE_UNKNOWN);
8653859Sml29623 
8666495Sspeer 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
8676495Sspeer 	    "registered to mac (instance %d)", instance));
8683859Sml29623 
8696835Syc148097 	/* nxge_link_monitor calls xcvr.check_link recursively */
8703859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_START);
8713859Sml29623 
8723859Sml29623 	goto nxge_attach_exit;
8733859Sml29623 
8743859Sml29623 nxge_attach_fail:
8753859Sml29623 	nxge_unattach(nxgep);
8764977Sraghus 	goto nxge_attach_fail1;
8774977Sraghus 
8784977Sraghus nxge_attach_fail5:
8794977Sraghus 	/*
8804977Sraghus 	 * Tear down the ndd parameters setup.
8814977Sraghus 	 */
8824977Sraghus 	nxge_destroy_param(nxgep);
8834977Sraghus 
8844977Sraghus 	/*
8854977Sraghus 	 * Tear down the kstat setup.
8864977Sraghus 	 */
8874977Sraghus 	nxge_destroy_kstats(nxgep);
8884977Sraghus 
8894977Sraghus nxge_attach_fail4:
8904977Sraghus 	if (nxgep->nxge_hw_p) {
8914977Sraghus 		nxge_uninit_common_dev(nxgep);
8924977Sraghus 		nxgep->nxge_hw_p = NULL;
8934977Sraghus 	}
8944977Sraghus 
8954977Sraghus nxge_attach_fail3:
8964977Sraghus 	/*
8974977Sraghus 	 * Unmap the register setup.
8984977Sraghus 	 */
8994977Sraghus 	nxge_unmap_regs(nxgep);
9004977Sraghus 
9014977Sraghus 	nxge_fm_fini(nxgep);
9024977Sraghus 
9034977Sraghus nxge_attach_fail2:
9044977Sraghus 	ddi_soft_state_free(nxge_list, nxgep->instance);
9054977Sraghus 
9064977Sraghus nxge_attach_fail1:
9074185Sspeer 	if (status != NXGE_OK)
9084185Sspeer 		status = (NXGE_ERROR | NXGE_DDI_FAILED);
9093859Sml29623 	nxgep = NULL;
9103859Sml29623 
9113859Sml29623 nxge_attach_exit:
9123859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_attach status = 0x%08x",
9136512Ssowmini 	    status));
9143859Sml29623 
9153859Sml29623 	return (status);
9163859Sml29623 }
9173859Sml29623 
9183859Sml29623 static int
9193859Sml29623 nxge_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
9203859Sml29623 {
9213859Sml29623 	int 		status = DDI_SUCCESS;
9223859Sml29623 	int 		instance;
9233859Sml29623 	p_nxge_t 	nxgep = NULL;
9243859Sml29623 
9253859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_detach"));
9263859Sml29623 	instance = ddi_get_instance(dip);
9273859Sml29623 	nxgep = ddi_get_soft_state(nxge_list, instance);
9283859Sml29623 	if (nxgep == NULL) {
9293859Sml29623 		status = DDI_FAILURE;
9303859Sml29623 		goto nxge_detach_exit;
9313859Sml29623 	}
9323859Sml29623 
9333859Sml29623 	switch (cmd) {
9343859Sml29623 	case DDI_DETACH:
9353859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_DETACH"));
9363859Sml29623 		break;
9373859Sml29623 
9383859Sml29623 	case DDI_PM_SUSPEND:
9393859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_PM_SUSPEND"));
9403859Sml29623 		nxgep->suspended = DDI_PM_SUSPEND;
9413859Sml29623 		nxge_suspend(nxgep);
9423859Sml29623 		break;
9433859Sml29623 
9443859Sml29623 	case DDI_SUSPEND:
9453859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_SUSPEND"));
9463859Sml29623 		if (nxgep->suspended != DDI_PM_SUSPEND) {
9473859Sml29623 			nxgep->suspended = DDI_SUSPEND;
9483859Sml29623 			nxge_suspend(nxgep);
9493859Sml29623 		}
9503859Sml29623 		break;
9513859Sml29623 
9523859Sml29623 	default:
9533859Sml29623 		status = DDI_FAILURE;
9543859Sml29623 	}
9553859Sml29623 
9563859Sml29623 	if (cmd != DDI_DETACH)
9573859Sml29623 		goto nxge_detach_exit;
9583859Sml29623 
9593859Sml29623 	/*
9603859Sml29623 	 * Stop the xcvr polling.
9613859Sml29623 	 */
9623859Sml29623 	nxgep->suspended = cmd;
9633859Sml29623 
9643859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_STOP);
9653859Sml29623 
9666495Sspeer 	if (isLDOMguest(nxgep)) {
9677466SMisaki.Kataoka@Sun.COM 		if (nxgep->nxge_mac_state == NXGE_MAC_STARTED)
9687466SMisaki.Kataoka@Sun.COM 			nxge_m_stop((void *)nxgep);
9696495Sspeer 		nxge_hio_unregister(nxgep);
9706495Sspeer 	} else if (nxgep->mach && (status = mac_unregister(nxgep->mach)) != 0) {
9713859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
9726512Ssowmini 		    "<== nxge_detach status = 0x%08X", status));
9733859Sml29623 		return (DDI_FAILURE);
9743859Sml29623 	}
9753859Sml29623 
9763859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
9776512Ssowmini 	    "<== nxge_detach (mac_unregister) status = 0x%08X", status));
9783859Sml29623 
9793859Sml29623 	nxge_unattach(nxgep);
9803859Sml29623 	nxgep = NULL;
9813859Sml29623 
9823859Sml29623 nxge_detach_exit:
9833859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_detach status = 0x%08X",
9846512Ssowmini 	    status));
9853859Sml29623 
9863859Sml29623 	return (status);
9873859Sml29623 }
9883859Sml29623 
9893859Sml29623 static void
9903859Sml29623 nxge_unattach(p_nxge_t nxgep)
9913859Sml29623 {
9923859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_unattach"));
9933859Sml29623 
9943859Sml29623 	if (nxgep == NULL || nxgep->dev_regs == NULL) {
9953859Sml29623 		return;
9963859Sml29623 	}
9973859Sml29623 
9984693Stm144005 	nxgep->nxge_magic = 0;
9994693Stm144005 
10005780Ssbehera 	if (nxgep->nxge_timerid) {
10015780Ssbehera 		nxge_stop_timer(nxgep, nxgep->nxge_timerid);
10025780Ssbehera 		nxgep->nxge_timerid = 0;
10035780Ssbehera 	}
10045780Ssbehera 
10056705Sml29623 	/*
10066705Sml29623 	 * If this flag is set, it will affect the Neptune
10076705Sml29623 	 * only.
10086705Sml29623 	 */
10096705Sml29623 	if ((nxgep->niu_type != N2_NIU) && nxge_peu_reset_enable) {
10106705Sml29623 		nxge_niu_peu_reset(nxgep);
10116705Sml29623 	}
10126705Sml29623 
10136495Sspeer #if	defined(sun4v)
10146495Sspeer 	if (isLDOMguest(nxgep)) {
10156498Sspeer 		(void) nxge_hio_vr_release(nxgep);
10166495Sspeer 	}
10176495Sspeer #endif
10186495Sspeer 
10193859Sml29623 	if (nxgep->nxge_hw_p) {
10203859Sml29623 		nxge_uninit_common_dev(nxgep);
10213859Sml29623 		nxgep->nxge_hw_p = NULL;
10223859Sml29623 	}
10233859Sml29623 
10243859Sml29623 #if	defined(sun4v)
10253859Sml29623 	if (nxgep->niu_type == N2_NIU && nxgep->niu_hsvc_available == B_TRUE) {
10263859Sml29623 		(void) hsvc_unregister(&nxgep->niu_hsvc);
10273859Sml29623 		nxgep->niu_hsvc_available = B_FALSE;
10283859Sml29623 	}
10293859Sml29623 #endif
10303859Sml29623 	/*
10313859Sml29623 	 * Stop any further interrupts.
10323859Sml29623 	 */
10333859Sml29623 	nxge_remove_intrs(nxgep);
10343859Sml29623 
10353859Sml29623 	/* remove soft interrups */
10363859Sml29623 	nxge_remove_soft_intrs(nxgep);
10373859Sml29623 
10383859Sml29623 	/*
10393859Sml29623 	 * Stop the device and free resources.
10403859Sml29623 	 */
10416495Sspeer 	if (!isLDOMguest(nxgep)) {
10426495Sspeer 		nxge_destroy_dev(nxgep);
10436495Sspeer 	}
10443859Sml29623 
10453859Sml29623 	/*
10463859Sml29623 	 * Tear down the ndd parameters setup.
10473859Sml29623 	 */
10483859Sml29623 	nxge_destroy_param(nxgep);
10493859Sml29623 
10503859Sml29623 	/*
10513859Sml29623 	 * Tear down the kstat setup.
10523859Sml29623 	 */
10533859Sml29623 	nxge_destroy_kstats(nxgep);
10543859Sml29623 
10553859Sml29623 	/*
10563859Sml29623 	 * Destroy all mutexes.
10573859Sml29623 	 */
10583859Sml29623 	nxge_destroy_mutexes(nxgep);
10593859Sml29623 
10603859Sml29623 	/*
10613859Sml29623 	 * Remove the list of ndd parameters which
10623859Sml29623 	 * were setup during attach.
10633859Sml29623 	 */
10643859Sml29623 	if (nxgep->dip) {
10653859Sml29623 		NXGE_DEBUG_MSG((nxgep, OBP_CTL,
10666512Ssowmini 		    " nxge_unattach: remove all properties"));
10673859Sml29623 
10683859Sml29623 		(void) ddi_prop_remove_all(nxgep->dip);
10693859Sml29623 	}
10703859Sml29623 
10713859Sml29623 #if NXGE_PROPERTY
10723859Sml29623 	nxge_remove_hard_properties(nxgep);
10733859Sml29623 #endif
10743859Sml29623 
10753859Sml29623 	/*
10763859Sml29623 	 * Unmap the register setup.
10773859Sml29623 	 */
10783859Sml29623 	nxge_unmap_regs(nxgep);
10793859Sml29623 
10803859Sml29623 	nxge_fm_fini(nxgep);
10813859Sml29623 
10823859Sml29623 	ddi_soft_state_free(nxge_list, nxgep->instance);
10833859Sml29623 
10843859Sml29623 	NXGE_DEBUG_MSG((NULL, DDI_CTL, "<== nxge_unattach"));
10853859Sml29623 }
10863859Sml29623 
10876495Sspeer #if defined(sun4v)
10886495Sspeer int
10897587SMichael.Speer@Sun.COM nxge_hsvc_register(nxge_t *nxgep)
10906495Sspeer {
10916495Sspeer 	nxge_status_t status;
10926495Sspeer 
10936495Sspeer 	if (nxgep->niu_type == N2_NIU) {
10946495Sspeer 		nxgep->niu_hsvc_available = B_FALSE;
10956495Sspeer 		bcopy(&niu_hsvc, &nxgep->niu_hsvc, sizeof (hsvc_info_t));
10966495Sspeer 		if ((status = hsvc_register(&nxgep->niu_hsvc,
10976495Sspeer 		    &nxgep->niu_min_ver)) != 0) {
10986495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
10996495Sspeer 			    "nxge_attach: %s: cannot negotiate "
11006495Sspeer 			    "hypervisor services revision %d group: 0x%lx "
11016495Sspeer 			    "major: 0x%lx minor: 0x%lx errno: %d",
11026495Sspeer 			    niu_hsvc.hsvc_modname, niu_hsvc.hsvc_rev,
11036495Sspeer 			    niu_hsvc.hsvc_group, niu_hsvc.hsvc_major,
11046495Sspeer 			    niu_hsvc.hsvc_minor, status));
11056495Sspeer 			return (DDI_FAILURE);
11066495Sspeer 		}
11076495Sspeer 		nxgep->niu_hsvc_available = B_TRUE;
11086495Sspeer 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11096512Ssowmini 		    "NIU Hypervisor service enabled"));
11106495Sspeer 	}
11116495Sspeer 
11126495Sspeer 	return (DDI_SUCCESS);
11136495Sspeer }
11146495Sspeer #endif
11156495Sspeer 
11163859Sml29623 static char n2_siu_name[] = "niu";
11173859Sml29623 
11183859Sml29623 static nxge_status_t
11193859Sml29623 nxge_map_regs(p_nxge_t nxgep)
11203859Sml29623 {
11213859Sml29623 	int		ddi_status = DDI_SUCCESS;
11223859Sml29623 	p_dev_regs_t 	dev_regs;
11233859Sml29623 	char		buf[MAXPATHLEN + 1];
11243859Sml29623 	char 		*devname;
11253859Sml29623 #ifdef	NXGE_DEBUG
11263859Sml29623 	char 		*sysname;
11273859Sml29623 #endif
11283859Sml29623 	off_t		regsize;
11293859Sml29623 	nxge_status_t	status = NXGE_OK;
11303859Sml29623 #if !defined(_BIG_ENDIAN)
11313859Sml29623 	off_t pci_offset;
11323859Sml29623 	uint16_t pcie_devctl;
11333859Sml29623 #endif
11343859Sml29623 
11356495Sspeer 	if (isLDOMguest(nxgep)) {
11366495Sspeer 		return (nxge_guest_regs_map(nxgep));
11376495Sspeer 	}
11386495Sspeer 
11393859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_map_regs"));
11403859Sml29623 	nxgep->dev_regs = NULL;
11413859Sml29623 	dev_regs = KMEM_ZALLOC(sizeof (dev_regs_t), KM_SLEEP);
11423859Sml29623 	dev_regs->nxge_regh = NULL;
11433859Sml29623 	dev_regs->nxge_pciregh = NULL;
11443859Sml29623 	dev_regs->nxge_msix_regh = NULL;
11453859Sml29623 	dev_regs->nxge_vir_regh = NULL;
11463859Sml29623 	dev_regs->nxge_vir2_regh = NULL;
11474732Sdavemq 	nxgep->niu_type = NIU_TYPE_NONE;
11483859Sml29623 
11493859Sml29623 	devname = ddi_pathname(nxgep->dip, buf);
11503859Sml29623 	ASSERT(strlen(devname) > 0);
11513859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11526512Ssowmini 	    "nxge_map_regs: pathname devname %s", devname));
11533859Sml29623 
11546835Syc148097 	/*
11556835Syc148097 	 * The driver is running on a N2-NIU system if devname is something
11566835Syc148097 	 * like "/niu@80/network@0"
11576835Syc148097 	 */
11583859Sml29623 	if (strstr(devname, n2_siu_name)) {
11593859Sml29623 		/* N2/NIU */
11603859Sml29623 		nxgep->niu_type = N2_NIU;
11613859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11626512Ssowmini 		    "nxge_map_regs: N2/NIU devname %s", devname));
11633859Sml29623 		/* get function number */
11643859Sml29623 		nxgep->function_num =
11656512Ssowmini 		    (devname[strlen(devname) -1] == '1' ? 1 : 0);
11663859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11676512Ssowmini 		    "nxge_map_regs: N2/NIU function number %d",
11686512Ssowmini 		    nxgep->function_num));
11693859Sml29623 	} else {
11703859Sml29623 		int		*prop_val;
11713859Sml29623 		uint_t 		prop_len;
11723859Sml29623 		uint8_t 	func_num;
11733859Sml29623 
11743859Sml29623 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, nxgep->dip,
11756512Ssowmini 		    0, "reg",
11766512Ssowmini 		    &prop_val, &prop_len) != DDI_PROP_SUCCESS) {
11773859Sml29623 			NXGE_DEBUG_MSG((nxgep, VPD_CTL,
11786512Ssowmini 			    "Reg property not found"));
11793859Sml29623 			ddi_status = DDI_FAILURE;
11803859Sml29623 			goto nxge_map_regs_fail0;
11813859Sml29623 
11823859Sml29623 		} else {
11833859Sml29623 			func_num = (prop_val[0] >> 8) & 0x7;
11843859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11856512Ssowmini 			    "Reg property found: fun # %d",
11866512Ssowmini 			    func_num));
11873859Sml29623 			nxgep->function_num = func_num;
11886495Sspeer 			if (isLDOMguest(nxgep)) {
11896495Sspeer 				nxgep->function_num /= 2;
11906495Sspeer 				return (NXGE_OK);
11916495Sspeer 			}
11923859Sml29623 			ddi_prop_free(prop_val);
11933859Sml29623 		}
11943859Sml29623 	}
11953859Sml29623 
11963859Sml29623 	switch (nxgep->niu_type) {
11973859Sml29623 	default:
11983859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 0, &regsize);
11993859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12006512Ssowmini 		    "nxge_map_regs: pci config size 0x%x", regsize));
12013859Sml29623 
12023859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 0,
12036512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_pciregp), 0, 0,
12046512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_pciregh);
12053859Sml29623 		if (ddi_status != DDI_SUCCESS) {
12063859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
12076512Ssowmini 			    "ddi_map_regs, nxge bus config regs failed"));
12083859Sml29623 			goto nxge_map_regs_fail0;
12093859Sml29623 		}
12103859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12116512Ssowmini 		    "nxge_map_reg: PCI config addr 0x%0llx "
12126512Ssowmini 		    " handle 0x%0llx", dev_regs->nxge_pciregp,
12136512Ssowmini 		    dev_regs->nxge_pciregh));
12143859Sml29623 			/*
12153859Sml29623 			 * IMP IMP
12163859Sml29623 			 * workaround  for bit swapping bug in HW
12173859Sml29623 			 * which ends up in no-snoop = yes
12183859Sml29623 			 * resulting, in DMA not synched properly
12193859Sml29623 			 */
12203859Sml29623 #if !defined(_BIG_ENDIAN)
12213859Sml29623 		/* workarounds for x86 systems */
12223859Sml29623 		pci_offset = 0x80 + PCIE_DEVCTL;
12233859Sml29623 		pcie_devctl = 0x0;
12243859Sml29623 		pcie_devctl &= PCIE_DEVCTL_ENABLE_NO_SNOOP;
12253859Sml29623 		pcie_devctl |= PCIE_DEVCTL_RO_EN;
12263859Sml29623 		pci_config_put16(dev_regs->nxge_pciregh, pci_offset,
12276512Ssowmini 		    pcie_devctl);
12283859Sml29623 #endif
12293859Sml29623 
12303859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 1, &regsize);
12313859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12326512Ssowmini 		    "nxge_map_regs: pio size 0x%x", regsize));
12333859Sml29623 		/* set up the device mapped register */
12343859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 1,
12356512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_regp), 0, 0,
12366512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_regh);
12373859Sml29623 		if (ddi_status != DDI_SUCCESS) {
12383859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
12396512Ssowmini 			    "ddi_map_regs for Neptune global reg failed"));
12403859Sml29623 			goto nxge_map_regs_fail1;
12413859Sml29623 		}
12423859Sml29623 
12433859Sml29623 		/* set up the msi/msi-x mapped register */
12443859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 2, &regsize);
12453859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12466512Ssowmini 		    "nxge_map_regs: msix size 0x%x", regsize));
12473859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 2,
12486512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_msix_regp), 0, 0,
12496512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_msix_regh);
12503859Sml29623 		if (ddi_status != DDI_SUCCESS) {
12513859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
12526512Ssowmini 			    "ddi_map_regs for msi reg failed"));
12533859Sml29623 			goto nxge_map_regs_fail2;
12543859Sml29623 		}
12553859Sml29623 
12563859Sml29623 		/* set up the vio region mapped register */
12573859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 3, &regsize);
12583859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12596512Ssowmini 		    "nxge_map_regs: vio size 0x%x", regsize));
12603859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 3,
12616512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_vir_regp), 0, 0,
12626512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_vir_regh);
12633859Sml29623 
12643859Sml29623 		if (ddi_status != DDI_SUCCESS) {
12653859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
12666512Ssowmini 			    "ddi_map_regs for nxge vio reg failed"));
12673859Sml29623 			goto nxge_map_regs_fail3;
12683859Sml29623 		}
12693859Sml29623 		nxgep->dev_regs = dev_regs;
12703859Sml29623 
12713859Sml29623 		NPI_PCI_ACC_HANDLE_SET(nxgep, dev_regs->nxge_pciregh);
12723859Sml29623 		NPI_PCI_ADD_HANDLE_SET(nxgep,
12736512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_pciregp);
12743859Sml29623 		NPI_MSI_ACC_HANDLE_SET(nxgep, dev_regs->nxge_msix_regh);
12753859Sml29623 		NPI_MSI_ADD_HANDLE_SET(nxgep,
12766512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_msix_regp);
12773859Sml29623 
12783859Sml29623 		NPI_ACC_HANDLE_SET(nxgep, dev_regs->nxge_regh);
12793859Sml29623 		NPI_ADD_HANDLE_SET(nxgep, (npi_reg_ptr_t)dev_regs->nxge_regp);
12803859Sml29623 
12813859Sml29623 		NPI_REG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_regh);
12823859Sml29623 		NPI_REG_ADD_HANDLE_SET(nxgep,
12836512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_regp);
12843859Sml29623 
12853859Sml29623 		NPI_VREG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_vir_regh);
12863859Sml29623 		NPI_VREG_ADD_HANDLE_SET(nxgep,
12876512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_vir_regp);
12883859Sml29623 
12893859Sml29623 		break;
12903859Sml29623 
12913859Sml29623 	case N2_NIU:
12923859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "ddi_map_regs, NIU"));
12933859Sml29623 		/*
12943859Sml29623 		 * Set up the device mapped register (FWARC 2006/556)
12953859Sml29623 		 * (changed back to 1: reg starts at 1!)
12963859Sml29623 		 */
12973859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 1, &regsize);
12983859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12996512Ssowmini 		    "nxge_map_regs: dev size 0x%x", regsize));
13003859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 1,
13016512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_regp), 0, 0,
13026512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_regh);
13033859Sml29623 
13043859Sml29623 		if (ddi_status != DDI_SUCCESS) {
13053859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
13066512Ssowmini 			    "ddi_map_regs for N2/NIU, global reg failed "));
13073859Sml29623 			goto nxge_map_regs_fail1;
13083859Sml29623 		}
13093859Sml29623 
13106495Sspeer 		/* set up the first vio region mapped register */
13113859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 2, &regsize);
13123859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
13136512Ssowmini 		    "nxge_map_regs: vio (1) size 0x%x", regsize));
13143859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 2,
13156512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_vir_regp), 0, 0,
13166512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_vir_regh);
13173859Sml29623 
13183859Sml29623 		if (ddi_status != DDI_SUCCESS) {
13193859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
13206512Ssowmini 			    "ddi_map_regs for nxge vio reg failed"));
13213859Sml29623 			goto nxge_map_regs_fail2;
13223859Sml29623 		}
13236495Sspeer 		/* set up the second vio region mapped register */
13243859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 3, &regsize);
13253859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
13266512Ssowmini 		    "nxge_map_regs: vio (3) size 0x%x", regsize));
13273859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 3,
13286512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_vir2_regp), 0, 0,
13296512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_vir2_regh);
13303859Sml29623 
13313859Sml29623 		if (ddi_status != DDI_SUCCESS) {
13323859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
13336512Ssowmini 			    "ddi_map_regs for nxge vio2 reg failed"));
13343859Sml29623 			goto nxge_map_regs_fail3;
13353859Sml29623 		}
13363859Sml29623 		nxgep->dev_regs = dev_regs;
13373859Sml29623 
13383859Sml29623 		NPI_ACC_HANDLE_SET(nxgep, dev_regs->nxge_regh);
13393859Sml29623 		NPI_ADD_HANDLE_SET(nxgep, (npi_reg_ptr_t)dev_regs->nxge_regp);
13403859Sml29623 
13413859Sml29623 		NPI_REG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_regh);
13423859Sml29623 		NPI_REG_ADD_HANDLE_SET(nxgep,
13436512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_regp);
13443859Sml29623 
13453859Sml29623 		NPI_VREG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_vir_regh);
13463859Sml29623 		NPI_VREG_ADD_HANDLE_SET(nxgep,
13476512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_vir_regp);
13483859Sml29623 
13493859Sml29623 		NPI_V2REG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_vir2_regh);
13503859Sml29623 		NPI_V2REG_ADD_HANDLE_SET(nxgep,
13516512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_vir2_regp);
13523859Sml29623 
13533859Sml29623 		break;
13543859Sml29623 	}
13553859Sml29623 
13563859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "nxge_map_reg: hardware addr 0x%0llx "
13576512Ssowmini 	    " handle 0x%0llx", dev_regs->nxge_regp, dev_regs->nxge_regh));
13583859Sml29623 
13593859Sml29623 	goto nxge_map_regs_exit;
13603859Sml29623 nxge_map_regs_fail3:
13613859Sml29623 	if (dev_regs->nxge_msix_regh) {
13623859Sml29623 		ddi_regs_map_free(&dev_regs->nxge_msix_regh);
13633859Sml29623 	}
13643859Sml29623 	if (dev_regs->nxge_vir_regh) {
13653859Sml29623 		ddi_regs_map_free(&dev_regs->nxge_regh);
13663859Sml29623 	}
13673859Sml29623 nxge_map_regs_fail2:
13683859Sml29623 	if (dev_regs->nxge_regh) {
13693859Sml29623 		ddi_regs_map_free(&dev_regs->nxge_regh);
13703859Sml29623 	}
13713859Sml29623 nxge_map_regs_fail1:
13723859Sml29623 	if (dev_regs->nxge_pciregh) {
13733859Sml29623 		ddi_regs_map_free(&dev_regs->nxge_pciregh);
13743859Sml29623 	}
13753859Sml29623 nxge_map_regs_fail0:
13763859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "Freeing register set memory"));
13773859Sml29623 	kmem_free(dev_regs, sizeof (dev_regs_t));
13783859Sml29623 
13793859Sml29623 nxge_map_regs_exit:
13803859Sml29623 	if (ddi_status != DDI_SUCCESS)
13813859Sml29623 		status |= (NXGE_ERROR | NXGE_DDI_FAILED);
13823859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_map_regs"));
13833859Sml29623 	return (status);
13843859Sml29623 }
13853859Sml29623 
13863859Sml29623 static void
13873859Sml29623 nxge_unmap_regs(p_nxge_t nxgep)
13883859Sml29623 {
13893859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_unmap_regs"));
13906495Sspeer 
13916495Sspeer 	if (isLDOMguest(nxgep)) {
13926495Sspeer 		nxge_guest_regs_map_free(nxgep);
13936495Sspeer 		return;
13946495Sspeer 	}
13956495Sspeer 
13963859Sml29623 	if (nxgep->dev_regs) {
13973859Sml29623 		if (nxgep->dev_regs->nxge_pciregh) {
13983859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
13996512Ssowmini 			    "==> nxge_unmap_regs: bus"));
14003859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_pciregh);
14013859Sml29623 			nxgep->dev_regs->nxge_pciregh = NULL;
14023859Sml29623 		}
14033859Sml29623 		if (nxgep->dev_regs->nxge_regh) {
14043859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
14056512Ssowmini 			    "==> nxge_unmap_regs: device registers"));
14063859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_regh);
14073859Sml29623 			nxgep->dev_regs->nxge_regh = NULL;
14083859Sml29623 		}
14093859Sml29623 		if (nxgep->dev_regs->nxge_msix_regh) {
14103859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
14116512Ssowmini 			    "==> nxge_unmap_regs: device interrupts"));
14123859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_msix_regh);
14133859Sml29623 			nxgep->dev_regs->nxge_msix_regh = NULL;
14143859Sml29623 		}
14153859Sml29623 		if (nxgep->dev_regs->nxge_vir_regh) {
14163859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
14176512Ssowmini 			    "==> nxge_unmap_regs: vio region"));
14183859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_vir_regh);
14193859Sml29623 			nxgep->dev_regs->nxge_vir_regh = NULL;
14203859Sml29623 		}
14213859Sml29623 		if (nxgep->dev_regs->nxge_vir2_regh) {
14223859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
14236512Ssowmini 			    "==> nxge_unmap_regs: vio2 region"));
14243859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_vir2_regh);
14253859Sml29623 			nxgep->dev_regs->nxge_vir2_regh = NULL;
14263859Sml29623 		}
14273859Sml29623 
14283859Sml29623 		kmem_free(nxgep->dev_regs, sizeof (dev_regs_t));
14293859Sml29623 		nxgep->dev_regs = NULL;
14303859Sml29623 	}
14313859Sml29623 
14323859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_unmap_regs"));
14333859Sml29623 }
14343859Sml29623 
14353859Sml29623 static nxge_status_t
14363859Sml29623 nxge_setup_mutexes(p_nxge_t nxgep)
14373859Sml29623 {
14383859Sml29623 	int ddi_status = DDI_SUCCESS;
14393859Sml29623 	nxge_status_t status = NXGE_OK;
14403859Sml29623 	nxge_classify_t *classify_ptr;
14413859Sml29623 	int partition;
14423859Sml29623 
14433859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_setup_mutexes"));
14443859Sml29623 
14453859Sml29623 	/*
14463859Sml29623 	 * Get the interrupt cookie so the mutexes can be
14473859Sml29623 	 * Initialized.
14483859Sml29623 	 */
14496495Sspeer 	if (isLDOMguest(nxgep)) {
14506495Sspeer 		nxgep->interrupt_cookie = 0;
14516495Sspeer 	} else {
14526495Sspeer 		ddi_status = ddi_get_iblock_cookie(nxgep->dip, 0,
14536495Sspeer 		    &nxgep->interrupt_cookie);
14546495Sspeer 
14556495Sspeer 		if (ddi_status != DDI_SUCCESS) {
14566495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
14576495Sspeer 			    "<== nxge_setup_mutexes: failed 0x%x",
14586495Sspeer 			    ddi_status));
14596495Sspeer 			goto nxge_setup_mutexes_exit;
14606495Sspeer 		}
14613859Sml29623 	}
14623859Sml29623 
14634693Stm144005 	cv_init(&nxgep->poll_cv, NULL, CV_DRIVER, NULL);
14644693Stm144005 	MUTEX_INIT(&nxgep->poll_lock, NULL,
14654693Stm144005 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14664693Stm144005 
14673859Sml29623 	/*
14684693Stm144005 	 * Initialize mutexes for this device.
14693859Sml29623 	 */
14703859Sml29623 	MUTEX_INIT(nxgep->genlock, NULL,
14716512Ssowmini 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14723859Sml29623 	MUTEX_INIT(&nxgep->ouraddr_lock, NULL,
14736512Ssowmini 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14743859Sml29623 	MUTEX_INIT(&nxgep->mif_lock, NULL,
14756512Ssowmini 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14766495Sspeer 	MUTEX_INIT(&nxgep->group_lock, NULL,
14776495Sspeer 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14783859Sml29623 	RW_INIT(&nxgep->filter_lock, NULL,
14796512Ssowmini 	    RW_DRIVER, (void *)nxgep->interrupt_cookie);
14803859Sml29623 
14813859Sml29623 	classify_ptr = &nxgep->classifier;
14823859Sml29623 		/*
14833859Sml29623 		 * FFLP Mutexes are never used in interrupt context
14843859Sml29623 		 * as fflp operation can take very long time to
14853859Sml29623 		 * complete and hence not suitable to invoke from interrupt
14863859Sml29623 		 * handlers.
14873859Sml29623 		 */
14883859Sml29623 	MUTEX_INIT(&classify_ptr->tcam_lock, NULL,
14894732Sdavemq 	    NXGE_MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14904977Sraghus 	if (NXGE_IS_VALID_NEPTUNE_TYPE(nxgep)) {
14913859Sml29623 		MUTEX_INIT(&classify_ptr->fcram_lock, NULL,
14924732Sdavemq 		    NXGE_MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14933859Sml29623 		for (partition = 0; partition < MAX_PARTITION; partition++) {
14943859Sml29623 			MUTEX_INIT(&classify_ptr->hash_lock[partition], NULL,
14953859Sml29623 			    NXGE_MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14963859Sml29623 		}
14973859Sml29623 	}
14983859Sml29623 
14993859Sml29623 nxge_setup_mutexes_exit:
15003859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
15014732Sdavemq 	    "<== nxge_setup_mutexes status = %x", status));
15023859Sml29623 
15033859Sml29623 	if (ddi_status != DDI_SUCCESS)
15043859Sml29623 		status |= (NXGE_ERROR | NXGE_DDI_FAILED);
15053859Sml29623 
15063859Sml29623 	return (status);
15073859Sml29623 }
15083859Sml29623 
15093859Sml29623 static void
15103859Sml29623 nxge_destroy_mutexes(p_nxge_t nxgep)
15113859Sml29623 {
15123859Sml29623 	int partition;
15133859Sml29623 	nxge_classify_t *classify_ptr;
15143859Sml29623 
15153859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_destroy_mutexes"));
15163859Sml29623 	RW_DESTROY(&nxgep->filter_lock);
15176495Sspeer 	MUTEX_DESTROY(&nxgep->group_lock);
15183859Sml29623 	MUTEX_DESTROY(&nxgep->mif_lock);
15193859Sml29623 	MUTEX_DESTROY(&nxgep->ouraddr_lock);
15203859Sml29623 	MUTEX_DESTROY(nxgep->genlock);
15213859Sml29623 
15223859Sml29623 	classify_ptr = &nxgep->classifier;
15233859Sml29623 	MUTEX_DESTROY(&classify_ptr->tcam_lock);
15243859Sml29623 
15254693Stm144005 	/* Destroy all polling resources. */
15264693Stm144005 	MUTEX_DESTROY(&nxgep->poll_lock);
15274693Stm144005 	cv_destroy(&nxgep->poll_cv);
15284693Stm144005 
15294693Stm144005 	/* free data structures, based on HW type */
15304977Sraghus 	if (NXGE_IS_VALID_NEPTUNE_TYPE(nxgep)) {
15313859Sml29623 		MUTEX_DESTROY(&classify_ptr->fcram_lock);
15323859Sml29623 		for (partition = 0; partition < MAX_PARTITION; partition++) {
15333859Sml29623 			MUTEX_DESTROY(&classify_ptr->hash_lock[partition]);
15343859Sml29623 		}
15353859Sml29623 	}
15363859Sml29623 
15373859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_destroy_mutexes"));
15383859Sml29623 }
15393859Sml29623 
15403859Sml29623 nxge_status_t
15413859Sml29623 nxge_init(p_nxge_t nxgep)
15423859Sml29623 {
15436495Sspeer 	nxge_status_t status = NXGE_OK;
15443859Sml29623 
15453859Sml29623 	NXGE_DEBUG_MSG((nxgep, STR_CTL, "==> nxge_init"));
15463859Sml29623 
15473859Sml29623 	if (nxgep->drv_state & STATE_HW_INITIALIZED) {
15483859Sml29623 		return (status);
15493859Sml29623 	}
15503859Sml29623 
15513859Sml29623 	/*
15523859Sml29623 	 * Allocate system memory for the receive/transmit buffer blocks
15533859Sml29623 	 * and receive/transmit descriptor rings.
15543859Sml29623 	 */
15553859Sml29623 	status = nxge_alloc_mem_pool(nxgep);
15563859Sml29623 	if (status != NXGE_OK) {
15573859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "alloc mem failed\n"));
15583859Sml29623 		goto nxge_init_fail1;
15593859Sml29623 	}
15603859Sml29623 
15616495Sspeer 	if (!isLDOMguest(nxgep)) {
15626495Sspeer 		/*
15636495Sspeer 		 * Initialize and enable the TXC registers.
15646495Sspeer 		 * (Globally enable the Tx controller,
15656495Sspeer 		 *  enable the port, configure the dma channel bitmap,
15666495Sspeer 		 *  configure the max burst size).
15676495Sspeer 		 */
15686495Sspeer 		status = nxge_txc_init(nxgep);
15696495Sspeer 		if (status != NXGE_OK) {
15706495Sspeer 			NXGE_ERROR_MSG((nxgep,
15716495Sspeer 			    NXGE_ERR_CTL, "init txc failed\n"));
15726495Sspeer 			goto nxge_init_fail2;
15736495Sspeer 		}
15743859Sml29623 	}
15753859Sml29623 
15763859Sml29623 	/*
15773859Sml29623 	 * Initialize and enable TXDMA channels.
15783859Sml29623 	 */
15793859Sml29623 	status = nxge_init_txdma_channels(nxgep);
15803859Sml29623 	if (status != NXGE_OK) {
15813859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init txdma failed\n"));
15823859Sml29623 		goto nxge_init_fail3;
15833859Sml29623 	}
15843859Sml29623 
15853859Sml29623 	/*
15863859Sml29623 	 * Initialize and enable RXDMA channels.
15873859Sml29623 	 */
15883859Sml29623 	status = nxge_init_rxdma_channels(nxgep);
15893859Sml29623 	if (status != NXGE_OK) {
15903859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init rxdma failed\n"));
15913859Sml29623 		goto nxge_init_fail4;
15923859Sml29623 	}
15933859Sml29623 
15943859Sml29623 	/*
15956495Sspeer 	 * The guest domain is now done.
15966495Sspeer 	 */
15976495Sspeer 	if (isLDOMguest(nxgep)) {
15986495Sspeer 		nxgep->drv_state |= STATE_HW_INITIALIZED;
15996495Sspeer 		goto nxge_init_exit;
16006495Sspeer 	}
16016495Sspeer 
16026495Sspeer 	/*
16033859Sml29623 	 * Initialize TCAM and FCRAM (Neptune).
16043859Sml29623 	 */
16053859Sml29623 	status = nxge_classify_init(nxgep);
16063859Sml29623 	if (status != NXGE_OK) {
16073859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init classify failed\n"));
16083859Sml29623 		goto nxge_init_fail5;
16093859Sml29623 	}
16103859Sml29623 
16113859Sml29623 	/*
16123859Sml29623 	 * Initialize ZCP
16133859Sml29623 	 */
16143859Sml29623 	status = nxge_zcp_init(nxgep);
16153859Sml29623 	if (status != NXGE_OK) {
16163859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init ZCP failed\n"));
16173859Sml29623 		goto nxge_init_fail5;
16183859Sml29623 	}
16193859Sml29623 
16203859Sml29623 	/*
16213859Sml29623 	 * Initialize IPP.
16223859Sml29623 	 */
16233859Sml29623 	status = nxge_ipp_init(nxgep);
16243859Sml29623 	if (status != NXGE_OK) {
16253859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init IPP failed\n"));
16263859Sml29623 		goto nxge_init_fail5;
16273859Sml29623 	}
16283859Sml29623 
16293859Sml29623 	/*
16303859Sml29623 	 * Initialize the MAC block.
16313859Sml29623 	 */
16323859Sml29623 	status = nxge_mac_init(nxgep);
16333859Sml29623 	if (status != NXGE_OK) {
16343859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init MAC failed\n"));
16353859Sml29623 		goto nxge_init_fail5;
16363859Sml29623 	}
16373859Sml29623 
16386495Sspeer 	nxge_intrs_enable(nxgep); /* XXX What changes do I need to make here? */
16393859Sml29623 
16403859Sml29623 	/*
16413859Sml29623 	 * Enable hardware interrupts.
16423859Sml29623 	 */
16433859Sml29623 	nxge_intr_hw_enable(nxgep);
16443859Sml29623 	nxgep->drv_state |= STATE_HW_INITIALIZED;
16453859Sml29623 
16463859Sml29623 	goto nxge_init_exit;
16473859Sml29623 
16483859Sml29623 nxge_init_fail5:
16493859Sml29623 	nxge_uninit_rxdma_channels(nxgep);
16503859Sml29623 nxge_init_fail4:
16513859Sml29623 	nxge_uninit_txdma_channels(nxgep);
16523859Sml29623 nxge_init_fail3:
16536495Sspeer 	if (!isLDOMguest(nxgep)) {
16546495Sspeer 		(void) nxge_txc_uninit(nxgep);
16556495Sspeer 	}
16563859Sml29623 nxge_init_fail2:
16573859Sml29623 	nxge_free_mem_pool(nxgep);
16583859Sml29623 nxge_init_fail1:
16593859Sml29623 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
16606512Ssowmini 	    "<== nxge_init status (failed) = 0x%08x", status));
16613859Sml29623 	return (status);
16623859Sml29623 
16633859Sml29623 nxge_init_exit:
16643859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_init status = 0x%08x",
16656512Ssowmini 	    status));
16663859Sml29623 	return (status);
16673859Sml29623 }
16683859Sml29623 
16693859Sml29623 
16703859Sml29623 timeout_id_t
16713859Sml29623 nxge_start_timer(p_nxge_t nxgep, fptrv_t func, int msec)
16723859Sml29623 {
16736512Ssowmini 	if ((nxgep->suspended == 0) || (nxgep->suspended == DDI_RESUME)) {
16743859Sml29623 		return (timeout(func, (caddr_t)nxgep,
16756512Ssowmini 		    drv_usectohz(1000 * msec)));
16763859Sml29623 	}
16773859Sml29623 	return (NULL);
16783859Sml29623 }
16793859Sml29623 
16803859Sml29623 /*ARGSUSED*/
16813859Sml29623 void
16823859Sml29623 nxge_stop_timer(p_nxge_t nxgep, timeout_id_t timerid)
16833859Sml29623 {
16843859Sml29623 	if (timerid) {
16853859Sml29623 		(void) untimeout(timerid);
16863859Sml29623 	}
16873859Sml29623 }
16883859Sml29623 
16893859Sml29623 void
16903859Sml29623 nxge_uninit(p_nxge_t nxgep)
16913859Sml29623 {
16923859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_uninit"));
16933859Sml29623 
16943859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
16953859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
16966512Ssowmini 		    "==> nxge_uninit: not initialized"));
16973859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
16986512Ssowmini 		    "<== nxge_uninit"));
16993859Sml29623 		return;
17003859Sml29623 	}
17013859Sml29623 
17023859Sml29623 	/* stop timer */
17033859Sml29623 	if (nxgep->nxge_timerid) {
17043859Sml29623 		nxge_stop_timer(nxgep, nxgep->nxge_timerid);
17053859Sml29623 		nxgep->nxge_timerid = 0;
17063859Sml29623 	}
17073859Sml29623 
17083859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_STOP);
17093859Sml29623 	(void) nxge_intr_hw_disable(nxgep);
17103859Sml29623 
17113859Sml29623 	/*
17123859Sml29623 	 * Reset the receive MAC side.
17133859Sml29623 	 */
17143859Sml29623 	(void) nxge_rx_mac_disable(nxgep);
17153859Sml29623 
17163859Sml29623 	/* Disable and soft reset the IPP */
17176495Sspeer 	if (!isLDOMguest(nxgep))
17186495Sspeer 		(void) nxge_ipp_disable(nxgep);
17193859Sml29623 
17203859Sml29623 	/* Free classification resources */
17213859Sml29623 	(void) nxge_classify_uninit(nxgep);
17223859Sml29623 
17233859Sml29623 	/*
17243859Sml29623 	 * Reset the transmit/receive DMA side.
17253859Sml29623 	 */
17263859Sml29623 	(void) nxge_txdma_hw_mode(nxgep, NXGE_DMA_STOP);
17273859Sml29623 	(void) nxge_rxdma_hw_mode(nxgep, NXGE_DMA_STOP);
17283859Sml29623 
17293859Sml29623 	nxge_uninit_txdma_channels(nxgep);
17303859Sml29623 	nxge_uninit_rxdma_channels(nxgep);
17313859Sml29623 
17323859Sml29623 	/*
17333859Sml29623 	 * Reset the transmit MAC side.
17343859Sml29623 	 */
17353859Sml29623 	(void) nxge_tx_mac_disable(nxgep);
17363859Sml29623 
17373859Sml29623 	nxge_free_mem_pool(nxgep);
17383859Sml29623 
17396705Sml29623 	/*
17406705Sml29623 	 * Start the timer if the reset flag is not set.
17416705Sml29623 	 * If this reset flag is set, the link monitor
17426705Sml29623 	 * will not be started in order to stop furthur bus
17436705Sml29623 	 * activities coming from this interface.
17446705Sml29623 	 * The driver will start the monitor function
17456705Sml29623 	 * if the interface was initialized again later.
17466705Sml29623 	 */
17476705Sml29623 	if (!nxge_peu_reset_enable) {
17486705Sml29623 		(void) nxge_link_monitor(nxgep, LINK_MONITOR_START);
17496705Sml29623 	}
17503859Sml29623 
17513859Sml29623 	nxgep->drv_state &= ~STATE_HW_INITIALIZED;
17523859Sml29623 
17533859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_uninit: "
17546512Ssowmini 	    "nxge_mblks_pending %d", nxge_mblks_pending));
17553859Sml29623 }
17563859Sml29623 
17573859Sml29623 void
17583859Sml29623 nxge_get64(p_nxge_t nxgep, p_mblk_t mp)
17593859Sml29623 {
17605125Sjoycey #if defined(__i386)
17615125Sjoycey 	size_t		reg;
17625125Sjoycey #else
17633859Sml29623 	uint64_t	reg;
17645125Sjoycey #endif
17653859Sml29623 	uint64_t	regdata;
17663859Sml29623 	int		i, retry;
17673859Sml29623 
17683859Sml29623 	bcopy((char *)mp->b_rptr, (char *)&reg, sizeof (uint64_t));
17693859Sml29623 	regdata = 0;
17703859Sml29623 	retry = 1;
17713859Sml29623 
17723859Sml29623 	for (i = 0; i < retry; i++) {
17733859Sml29623 		NXGE_REG_RD64(nxgep->npi_handle, reg, &regdata);
17743859Sml29623 	}
17753859Sml29623 	bcopy((char *)&regdata, (char *)mp->b_rptr, sizeof (uint64_t));
17763859Sml29623 }
17773859Sml29623 
17783859Sml29623 void
17793859Sml29623 nxge_put64(p_nxge_t nxgep, p_mblk_t mp)
17803859Sml29623 {
17815125Sjoycey #if defined(__i386)
17825125Sjoycey 	size_t		reg;
17835125Sjoycey #else
17843859Sml29623 	uint64_t	reg;
17855125Sjoycey #endif
17863859Sml29623 	uint64_t	buf[2];
17873859Sml29623 
17883859Sml29623 	bcopy((char *)mp->b_rptr, (char *)&buf[0], 2 * sizeof (uint64_t));
17895133Sjoycey #if defined(__i386)
17905133Sjoycey 	reg = (size_t)buf[0];
17915133Sjoycey #else
17923859Sml29623 	reg = buf[0];
17935133Sjoycey #endif
17943859Sml29623 
17953859Sml29623 	NXGE_NPI_PIO_WRITE64(nxgep->npi_handle, reg, buf[1]);
17963859Sml29623 }
17973859Sml29623 
17983859Sml29623 
17993859Sml29623 nxge_os_mutex_t nxgedebuglock;
18003859Sml29623 int nxge_debug_init = 0;
18013859Sml29623 
18023859Sml29623 /*ARGSUSED*/
18033859Sml29623 /*VARARGS*/
18043859Sml29623 void
18053859Sml29623 nxge_debug_msg(p_nxge_t nxgep, uint64_t level, char *fmt, ...)
18063859Sml29623 {
18073859Sml29623 	char msg_buffer[1048];
18083859Sml29623 	char prefix_buffer[32];
18093859Sml29623 	int instance;
18103859Sml29623 	uint64_t debug_level;
18113859Sml29623 	int cmn_level = CE_CONT;
18123859Sml29623 	va_list ap;
18133859Sml29623 
18146495Sspeer 	if (nxgep && nxgep->nxge_debug_level != nxge_debug_level) {
18156495Sspeer 		/* In case a developer has changed nxge_debug_level. */
18166495Sspeer 		if (nxgep->nxge_debug_level != nxge_debug_level)
18176495Sspeer 			nxgep->nxge_debug_level = nxge_debug_level;
18186495Sspeer 	}
18196495Sspeer 
18203859Sml29623 	debug_level = (nxgep == NULL) ? nxge_debug_level :
18216512Ssowmini 	    nxgep->nxge_debug_level;
18223859Sml29623 
18233859Sml29623 	if ((level & debug_level) ||
18246512Ssowmini 	    (level == NXGE_NOTE) ||
18256512Ssowmini 	    (level == NXGE_ERR_CTL)) {
18263859Sml29623 		/* do the msg processing */
18273859Sml29623 		if (nxge_debug_init == 0) {
18283859Sml29623 			MUTEX_INIT(&nxgedebuglock, NULL, MUTEX_DRIVER, NULL);
18293859Sml29623 			nxge_debug_init = 1;
18303859Sml29623 		}
18313859Sml29623 
18323859Sml29623 		MUTEX_ENTER(&nxgedebuglock);
18333859Sml29623 
18343859Sml29623 		if ((level & NXGE_NOTE)) {
18353859Sml29623 			cmn_level = CE_NOTE;
18363859Sml29623 		}
18373859Sml29623 
18383859Sml29623 		if (level & NXGE_ERR_CTL) {
18393859Sml29623 			cmn_level = CE_WARN;
18403859Sml29623 		}
18413859Sml29623 
18423859Sml29623 		va_start(ap, fmt);
18433859Sml29623 		(void) vsprintf(msg_buffer, fmt, ap);
18443859Sml29623 		va_end(ap);
18453859Sml29623 		if (nxgep == NULL) {
18463859Sml29623 			instance = -1;
18473859Sml29623 			(void) sprintf(prefix_buffer, "%s :", "nxge");
18483859Sml29623 		} else {
18493859Sml29623 			instance = nxgep->instance;
18503859Sml29623 			(void) sprintf(prefix_buffer,
18516512Ssowmini 			    "%s%d :", "nxge", instance);
18523859Sml29623 		}
18533859Sml29623 
18543859Sml29623 		MUTEX_EXIT(&nxgedebuglock);
18553859Sml29623 		cmn_err(cmn_level, "!%s %s\n",
18566512Ssowmini 		    prefix_buffer, msg_buffer);
18573859Sml29623 
18583859Sml29623 	}
18593859Sml29623 }
18603859Sml29623 
18613859Sml29623 char *
18623859Sml29623 nxge_dump_packet(char *addr, int size)
18633859Sml29623 {
18643859Sml29623 	uchar_t *ap = (uchar_t *)addr;
18653859Sml29623 	int i;
18663859Sml29623 	static char etherbuf[1024];
18673859Sml29623 	char *cp = etherbuf;
18683859Sml29623 	char digits[] = "0123456789abcdef";
18693859Sml29623 
18703859Sml29623 	if (!size)
18713859Sml29623 		size = 60;
18723859Sml29623 
18733859Sml29623 	if (size > MAX_DUMP_SZ) {
18743859Sml29623 		/* Dump the leading bytes */
18753859Sml29623 		for (i = 0; i < MAX_DUMP_SZ/2; i++) {
18763859Sml29623 			if (*ap > 0x0f)
18773859Sml29623 				*cp++ = digits[*ap >> 4];
18783859Sml29623 			*cp++ = digits[*ap++ & 0xf];
18793859Sml29623 			*cp++ = ':';
18803859Sml29623 		}
18813859Sml29623 		for (i = 0; i < 20; i++)
18823859Sml29623 			*cp++ = '.';
18833859Sml29623 		/* Dump the last MAX_DUMP_SZ/2 bytes */
18843859Sml29623 		ap = (uchar_t *)(addr + (size - MAX_DUMP_SZ/2));
18853859Sml29623 		for (i = 0; i < MAX_DUMP_SZ/2; i++) {
18863859Sml29623 			if (*ap > 0x0f)
18873859Sml29623 				*cp++ = digits[*ap >> 4];
18883859Sml29623 			*cp++ = digits[*ap++ & 0xf];
18893859Sml29623 			*cp++ = ':';
18903859Sml29623 		}
18913859Sml29623 	} else {
18923859Sml29623 		for (i = 0; i < size; i++) {
18933859Sml29623 			if (*ap > 0x0f)
18943859Sml29623 				*cp++ = digits[*ap >> 4];
18953859Sml29623 			*cp++ = digits[*ap++ & 0xf];
18963859Sml29623 			*cp++ = ':';
18973859Sml29623 		}
18983859Sml29623 	}
18993859Sml29623 	*--cp = 0;
19003859Sml29623 	return (etherbuf);
19013859Sml29623 }
19023859Sml29623 
19033859Sml29623 #ifdef	NXGE_DEBUG
19043859Sml29623 static void
19053859Sml29623 nxge_test_map_regs(p_nxge_t nxgep)
19063859Sml29623 {
19073859Sml29623 	ddi_acc_handle_t cfg_handle;
19083859Sml29623 	p_pci_cfg_t	cfg_ptr;
19093859Sml29623 	ddi_acc_handle_t dev_handle;
19103859Sml29623 	char		*dev_ptr;
19113859Sml29623 	ddi_acc_handle_t pci_config_handle;
19123859Sml29623 	uint32_t	regval;
19133859Sml29623 	int		i;
19143859Sml29623 
19153859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_test_map_regs"));
19163859Sml29623 
19173859Sml29623 	dev_handle = nxgep->dev_regs->nxge_regh;
19183859Sml29623 	dev_ptr = (char *)nxgep->dev_regs->nxge_regp;
19193859Sml29623 
19204977Sraghus 	if (NXGE_IS_VALID_NEPTUNE_TYPE(nxgep)) {
19213859Sml29623 		cfg_handle = nxgep->dev_regs->nxge_pciregh;
19223859Sml29623 		cfg_ptr = (void *)nxgep->dev_regs->nxge_pciregp;
19233859Sml29623 
19243859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19254732Sdavemq 		    "Neptune PCI regp cfg_ptr 0x%llx", (char *)cfg_ptr));
19263859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19274732Sdavemq 		    "Neptune PCI cfg_ptr vendor id ptr 0x%llx",
19284732Sdavemq 		    &cfg_ptr->vendorid));
19293859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19304732Sdavemq 		    "\tvendorid 0x%x devid 0x%x",
19314732Sdavemq 		    NXGE_PIO_READ16(cfg_handle, &cfg_ptr->vendorid, 0),
19324732Sdavemq 		    NXGE_PIO_READ16(cfg_handle, &cfg_ptr->devid,    0)));
19333859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19344732Sdavemq 		    "PCI BAR: base 0x%x base14 0x%x base 18 0x%x "
19354732Sdavemq 		    "bar1c 0x%x",
19364732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base,   0),
19374732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base14, 0),
19384732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base18, 0),
19394732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base1c, 0)));
19403859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19414732Sdavemq 		    "\nNeptune PCI BAR: base20 0x%x base24 0x%x "
19424732Sdavemq 		    "base 28 0x%x bar2c 0x%x\n",
19434732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base20, 0),
19444732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base24, 0),
19454732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base28, 0),
19464732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base2c, 0)));
19473859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19484732Sdavemq 		    "\nNeptune PCI BAR: base30 0x%x\n",
19494732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base30, 0)));
19503859Sml29623 
19513859Sml29623 		cfg_handle = nxgep->dev_regs->nxge_pciregh;
19523859Sml29623 		cfg_ptr = (void *)nxgep->dev_regs->nxge_pciregp;
19533859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19544732Sdavemq 		    "first  0x%llx second 0x%llx third 0x%llx "
19554732Sdavemq 		    "last 0x%llx ",
19564732Sdavemq 		    NXGE_PIO_READ64(dev_handle,
19574732Sdavemq 		    (uint64_t *)(dev_ptr + 0),  0),
19584732Sdavemq 		    NXGE_PIO_READ64(dev_handle,
19594732Sdavemq 		    (uint64_t *)(dev_ptr + 8),  0),
19604732Sdavemq 		    NXGE_PIO_READ64(dev_handle,
19614732Sdavemq 		    (uint64_t *)(dev_ptr + 16), 0),
19624732Sdavemq 		    NXGE_PIO_READ64(cfg_handle,
19634732Sdavemq 		    (uint64_t *)(dev_ptr + 24), 0)));
19643859Sml29623 	}
19653859Sml29623 }
19663859Sml29623 
19673859Sml29623 #endif
19683859Sml29623 
19693859Sml29623 static void
19703859Sml29623 nxge_suspend(p_nxge_t nxgep)
19713859Sml29623 {
19723859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_suspend"));
19733859Sml29623 
19743859Sml29623 	nxge_intrs_disable(nxgep);
19753859Sml29623 	nxge_destroy_dev(nxgep);
19763859Sml29623 
19773859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_suspend"));
19783859Sml29623 }
19793859Sml29623 
19803859Sml29623 static nxge_status_t
19813859Sml29623 nxge_resume(p_nxge_t nxgep)
19823859Sml29623 {
19833859Sml29623 	nxge_status_t status = NXGE_OK;
19843859Sml29623 
19853859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_resume"));
19864587Sjoycey 
19873859Sml29623 	nxgep->suspended = DDI_RESUME;
19884587Sjoycey 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_START);
19894587Sjoycey 	(void) nxge_rxdma_hw_mode(nxgep, NXGE_DMA_START);
19904587Sjoycey 	(void) nxge_txdma_hw_mode(nxgep, NXGE_DMA_START);
19914587Sjoycey 	(void) nxge_rx_mac_enable(nxgep);
19924587Sjoycey 	(void) nxge_tx_mac_enable(nxgep);
19934587Sjoycey 	nxge_intrs_enable(nxgep);
19943859Sml29623 	nxgep->suspended = 0;
19953859Sml29623 
19963859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19976512Ssowmini 	    "<== nxge_resume status = 0x%x", status));
19983859Sml29623 	return (status);
19993859Sml29623 }
20003859Sml29623 
20013859Sml29623 static nxge_status_t
20023859Sml29623 nxge_setup_dev(p_nxge_t nxgep)
20033859Sml29623 {
20043859Sml29623 	nxge_status_t	status = NXGE_OK;
20053859Sml29623 
20063859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_setup_dev port %d",
20074732Sdavemq 	    nxgep->mac.portnum));
20083859Sml29623 
20093859Sml29623 	status = nxge_link_init(nxgep);
20103859Sml29623 
20113859Sml29623 	if (fm_check_acc_handle(nxgep->dev_regs->nxge_regh) != DDI_FM_OK) {
20123859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
20136512Ssowmini 		    "port%d Bad register acc handle", nxgep->mac.portnum));
20143859Sml29623 		status = NXGE_ERROR;
20153859Sml29623 	}
20163859Sml29623 
20173859Sml29623 	if (status != NXGE_OK) {
20183859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
20196512Ssowmini 		    " nxge_setup_dev status "
20206512Ssowmini 		    "(xcvr init 0x%08x)", status));
20213859Sml29623 		goto nxge_setup_dev_exit;
20223859Sml29623 	}
20233859Sml29623 
20243859Sml29623 nxge_setup_dev_exit:
20253859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
20266512Ssowmini 	    "<== nxge_setup_dev port %d status = 0x%08x",
20276512Ssowmini 	    nxgep->mac.portnum, status));
20283859Sml29623 
20293859Sml29623 	return (status);
20303859Sml29623 }
20313859Sml29623 
20323859Sml29623 static void
20333859Sml29623 nxge_destroy_dev(p_nxge_t nxgep)
20343859Sml29623 {
20353859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_destroy_dev"));
20363859Sml29623 
20373859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_STOP);
20383859Sml29623 
20393859Sml29623 	(void) nxge_hw_stop(nxgep);
20403859Sml29623 
20413859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_destroy_dev"));
20423859Sml29623 }
20433859Sml29623 
20443859Sml29623 static nxge_status_t
20453859Sml29623 nxge_setup_system_dma_pages(p_nxge_t nxgep)
20463859Sml29623 {
20473859Sml29623 	int 			ddi_status = DDI_SUCCESS;
20483859Sml29623 	uint_t 			count;
20493859Sml29623 	ddi_dma_cookie_t 	cookie;
20503859Sml29623 	uint_t 			iommu_pagesize;
20513859Sml29623 	nxge_status_t		status = NXGE_OK;
20523859Sml29623 
20536495Sspeer 	NXGE_ERROR_MSG((nxgep, DDI_CTL, "==> nxge_setup_system_dma_pages"));
20543859Sml29623 	nxgep->sys_page_sz = ddi_ptob(nxgep->dip, (ulong_t)1);
20553859Sml29623 	if (nxgep->niu_type != N2_NIU) {
20563859Sml29623 		iommu_pagesize = dvma_pagesize(nxgep->dip);
20573859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
20586512Ssowmini 		    " nxge_setup_system_dma_pages: page %d (ddi_ptob %d) "
20596512Ssowmini 		    " default_block_size %d iommu_pagesize %d",
20606512Ssowmini 		    nxgep->sys_page_sz,
20616512Ssowmini 		    ddi_ptob(nxgep->dip, (ulong_t)1),
20626512Ssowmini 		    nxgep->rx_default_block_size,
20636512Ssowmini 		    iommu_pagesize));
20643859Sml29623 
20653859Sml29623 		if (iommu_pagesize != 0) {
20663859Sml29623 			if (nxgep->sys_page_sz == iommu_pagesize) {
20673859Sml29623 				if (iommu_pagesize > 0x4000)
20683859Sml29623 					nxgep->sys_page_sz = 0x4000;
20693859Sml29623 			} else {
20703859Sml29623 				if (nxgep->sys_page_sz > iommu_pagesize)
20713859Sml29623 					nxgep->sys_page_sz = iommu_pagesize;
20723859Sml29623 			}
20733859Sml29623 		}
20743859Sml29623 	}
20753859Sml29623 	nxgep->sys_page_mask = ~(nxgep->sys_page_sz - 1);
20763859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
20776512Ssowmini 	    "==> nxge_setup_system_dma_pages: page %d (ddi_ptob %d) "
20786512Ssowmini 	    "default_block_size %d page mask %d",
20796512Ssowmini 	    nxgep->sys_page_sz,
20806512Ssowmini 	    ddi_ptob(nxgep->dip, (ulong_t)1),
20816512Ssowmini 	    nxgep->rx_default_block_size,
20826512Ssowmini 	    nxgep->sys_page_mask));
20833859Sml29623 
20843859Sml29623 
20853859Sml29623 	switch (nxgep->sys_page_sz) {
20863859Sml29623 	default:
20873859Sml29623 		nxgep->sys_page_sz = 0x1000;
20883859Sml29623 		nxgep->sys_page_mask = ~(nxgep->sys_page_sz - 1);
20893859Sml29623 		nxgep->rx_default_block_size = 0x1000;
20903859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_4K;
20913859Sml29623 		break;
20923859Sml29623 	case 0x1000:
20933859Sml29623 		nxgep->rx_default_block_size = 0x1000;
20943859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_4K;
20953859Sml29623 		break;
20963859Sml29623 	case 0x2000:
20973859Sml29623 		nxgep->rx_default_block_size = 0x2000;
20983859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_8K;
20993859Sml29623 		break;
21003859Sml29623 	case 0x4000:
21013859Sml29623 		nxgep->rx_default_block_size = 0x4000;
21023859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_16K;
21033859Sml29623 		break;
21043859Sml29623 	case 0x8000:
21053859Sml29623 		nxgep->rx_default_block_size = 0x8000;
21063859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_32K;
21073859Sml29623 		break;
21083859Sml29623 	}
21093859Sml29623 
21103859Sml29623 #ifndef USE_RX_BIG_BUF
21113859Sml29623 	nxge_rx_dma_attr.dma_attr_align = nxgep->sys_page_sz;
21123859Sml29623 #else
21133859Sml29623 		nxgep->rx_default_block_size = 0x2000;
21143859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_8K;
21153859Sml29623 #endif
21163859Sml29623 	/*
21173859Sml29623 	 * Get the system DMA burst size.
21183859Sml29623 	 */
21193859Sml29623 	ddi_status = ddi_dma_alloc_handle(nxgep->dip, &nxge_tx_dma_attr,
21206512Ssowmini 	    DDI_DMA_DONTWAIT, 0,
21216512Ssowmini 	    &nxgep->dmasparehandle);
21223859Sml29623 	if (ddi_status != DDI_SUCCESS) {
21233859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
21246512Ssowmini 		    "ddi_dma_alloc_handle: failed "
21256512Ssowmini 		    " status 0x%x", ddi_status));
21263859Sml29623 		goto nxge_get_soft_properties_exit;
21273859Sml29623 	}
21283859Sml29623 
21293859Sml29623 	ddi_status = ddi_dma_addr_bind_handle(nxgep->dmasparehandle, NULL,
21306512Ssowmini 	    (caddr_t)nxgep->dmasparehandle,
21316512Ssowmini 	    sizeof (nxgep->dmasparehandle),
21326512Ssowmini 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
21336512Ssowmini 	    DDI_DMA_DONTWAIT, 0,
21346512Ssowmini 	    &cookie, &count);
21353859Sml29623 	if (ddi_status != DDI_DMA_MAPPED) {
21363859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
21376512Ssowmini 		    "Binding spare handle to find system"
21386512Ssowmini 		    " burstsize failed."));
21393859Sml29623 		ddi_status = DDI_FAILURE;
21403859Sml29623 		goto nxge_get_soft_properties_fail1;
21413859Sml29623 	}
21423859Sml29623 
21433859Sml29623 	nxgep->sys_burst_sz = ddi_dma_burstsizes(nxgep->dmasparehandle);
21443859Sml29623 	(void) ddi_dma_unbind_handle(nxgep->dmasparehandle);
21453859Sml29623 
21463859Sml29623 nxge_get_soft_properties_fail1:
21473859Sml29623 	ddi_dma_free_handle(&nxgep->dmasparehandle);
21483859Sml29623 
21493859Sml29623 nxge_get_soft_properties_exit:
21503859Sml29623 
21513859Sml29623 	if (ddi_status != DDI_SUCCESS)
21523859Sml29623 		status |= (NXGE_ERROR | NXGE_DDI_FAILED);
21533859Sml29623 
21543859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
21556512Ssowmini 	    "<== nxge_setup_system_dma_pages status = 0x%08x", status));
21563859Sml29623 	return (status);
21573859Sml29623 }
21583859Sml29623 
21593859Sml29623 static nxge_status_t
21603859Sml29623 nxge_alloc_mem_pool(p_nxge_t nxgep)
21613859Sml29623 {
21623859Sml29623 	nxge_status_t	status = NXGE_OK;
21633859Sml29623 
21643859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_alloc_mem_pool"));
21653859Sml29623 
21663859Sml29623 	status = nxge_alloc_rx_mem_pool(nxgep);
21673859Sml29623 	if (status != NXGE_OK) {
21683859Sml29623 		return (NXGE_ERROR);
21693859Sml29623 	}
21703859Sml29623 
21713859Sml29623 	status = nxge_alloc_tx_mem_pool(nxgep);
21723859Sml29623 	if (status != NXGE_OK) {
21733859Sml29623 		nxge_free_rx_mem_pool(nxgep);
21743859Sml29623 		return (NXGE_ERROR);
21753859Sml29623 	}
21763859Sml29623 
21773859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_alloc_mem_pool"));
21783859Sml29623 	return (NXGE_OK);
21793859Sml29623 }
21803859Sml29623 
21813859Sml29623 static void
21823859Sml29623 nxge_free_mem_pool(p_nxge_t nxgep)
21833859Sml29623 {
21843859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "==> nxge_free_mem_pool"));
21853859Sml29623 
21863859Sml29623 	nxge_free_rx_mem_pool(nxgep);
21873859Sml29623 	nxge_free_tx_mem_pool(nxgep);
21883859Sml29623 
21893859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "<== nxge_free_mem_pool"));
21903859Sml29623 }
21913859Sml29623 
21926495Sspeer nxge_status_t
21933859Sml29623 nxge_alloc_rx_mem_pool(p_nxge_t nxgep)
21943859Sml29623 {
21956495Sspeer 	uint32_t		rdc_max;
21963859Sml29623 	p_nxge_dma_pt_cfg_t	p_all_cfgp;
21973859Sml29623 	p_nxge_hw_pt_cfg_t	p_cfgp;
21983859Sml29623 	p_nxge_dma_pool_t	dma_poolp;
21993859Sml29623 	p_nxge_dma_common_t	*dma_buf_p;
22003859Sml29623 	p_nxge_dma_pool_t	dma_cntl_poolp;
22013859Sml29623 	p_nxge_dma_common_t	*dma_cntl_p;
22023859Sml29623 	uint32_t 		*num_chunks; /* per dma */
22033859Sml29623 	nxge_status_t		status = NXGE_OK;
22043859Sml29623 
22053859Sml29623 	uint32_t		nxge_port_rbr_size;
22063859Sml29623 	uint32_t		nxge_port_rbr_spare_size;
22073859Sml29623 	uint32_t		nxge_port_rcr_size;
22086495Sspeer 	uint32_t		rx_cntl_alloc_size;
22093859Sml29623 
22103859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rx_mem_pool"));
22113859Sml29623 
22123859Sml29623 	p_all_cfgp = (p_nxge_dma_pt_cfg_t)&nxgep->pt_config;
22133859Sml29623 	p_cfgp = (p_nxge_hw_pt_cfg_t)&p_all_cfgp->hw_config;
22146495Sspeer 	rdc_max = NXGE_MAX_RDCS;
22153859Sml29623 
22163859Sml29623 	/*
22176495Sspeer 	 * Allocate memory for the common DMA data structures.
22183859Sml29623 	 */
22193859Sml29623 	dma_poolp = (p_nxge_dma_pool_t)KMEM_ZALLOC(sizeof (nxge_dma_pool_t),
22206512Ssowmini 	    KM_SLEEP);
22213859Sml29623 	dma_buf_p = (p_nxge_dma_common_t *)KMEM_ZALLOC(
22226512Ssowmini 	    sizeof (p_nxge_dma_common_t) * rdc_max, KM_SLEEP);
22233859Sml29623 
22243859Sml29623 	dma_cntl_poolp = (p_nxge_dma_pool_t)
22256512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_pool_t), KM_SLEEP);
22263859Sml29623 	dma_cntl_p = (p_nxge_dma_common_t *)KMEM_ZALLOC(
22276512Ssowmini 	    sizeof (p_nxge_dma_common_t) * rdc_max, KM_SLEEP);
22283859Sml29623 
22293859Sml29623 	num_chunks = (uint32_t *)KMEM_ZALLOC(
22306512Ssowmini 	    sizeof (uint32_t) * rdc_max, KM_SLEEP);
22313859Sml29623 
22323859Sml29623 	/*
22336495Sspeer 	 * Assume that each DMA channel will be configured with
22346495Sspeer 	 * the default block size.
22356495Sspeer 	 * rbr block counts are modulo the batch count (16).
22363859Sml29623 	 */
22373859Sml29623 	nxge_port_rbr_size = p_all_cfgp->rbr_size;
22383859Sml29623 	nxge_port_rcr_size = p_all_cfgp->rcr_size;
22393859Sml29623 
22403859Sml29623 	if (!nxge_port_rbr_size) {
22413859Sml29623 		nxge_port_rbr_size = NXGE_RBR_RBB_DEFAULT;
22423859Sml29623 	}
22433859Sml29623 	if (nxge_port_rbr_size % NXGE_RXDMA_POST_BATCH) {
22443859Sml29623 		nxge_port_rbr_size = (NXGE_RXDMA_POST_BATCH *
22456512Ssowmini 		    (nxge_port_rbr_size / NXGE_RXDMA_POST_BATCH + 1));
22463859Sml29623 	}
22473859Sml29623 
22483859Sml29623 	p_all_cfgp->rbr_size = nxge_port_rbr_size;
22493859Sml29623 	nxge_port_rbr_spare_size = nxge_rbr_spare_size;
22503859Sml29623 
22513859Sml29623 	if (nxge_port_rbr_spare_size % NXGE_RXDMA_POST_BATCH) {
22523859Sml29623 		nxge_port_rbr_spare_size = (NXGE_RXDMA_POST_BATCH *
22536512Ssowmini 		    (nxge_port_rbr_spare_size / NXGE_RXDMA_POST_BATCH + 1));
22543859Sml29623 	}
22555770Sml29623 	if (nxge_port_rbr_size > RBR_DEFAULT_MAX_BLKS) {
22565770Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM_CTL,
22575770Sml29623 		    "nxge_alloc_rx_mem_pool: RBR size too high %d, "
22585770Sml29623 		    "set to default %d",
22595770Sml29623 		    nxge_port_rbr_size, RBR_DEFAULT_MAX_BLKS));
22605770Sml29623 		nxge_port_rbr_size = RBR_DEFAULT_MAX_BLKS;
22615770Sml29623 	}
22625770Sml29623 	if (nxge_port_rcr_size > RCR_DEFAULT_MAX) {
22635770Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM_CTL,
22645770Sml29623 		    "nxge_alloc_rx_mem_pool: RCR too high %d, "
22655770Sml29623 		    "set to default %d",
22665770Sml29623 		    nxge_port_rcr_size, RCR_DEFAULT_MAX));
22675770Sml29623 		nxge_port_rcr_size = RCR_DEFAULT_MAX;
22685770Sml29623 	}
22693859Sml29623 
22703859Sml29623 	/*
22713859Sml29623 	 * N2/NIU has limitation on the descriptor sizes (contiguous
22723859Sml29623 	 * memory allocation on data buffers to 4M (contig_mem_alloc)
22733859Sml29623 	 * and little endian for control buffers (must use the ddi/dki mem alloc
22743859Sml29623 	 * function).
22753859Sml29623 	 */
22763859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
22773859Sml29623 	if (nxgep->niu_type == N2_NIU) {
22783859Sml29623 		nxge_port_rbr_spare_size = 0;
22793859Sml29623 		if ((nxge_port_rbr_size > NXGE_NIU_CONTIG_RBR_MAX) ||
22806512Ssowmini 		    (!ISP2(nxge_port_rbr_size))) {
22813859Sml29623 			nxge_port_rbr_size = NXGE_NIU_CONTIG_RBR_MAX;
22823859Sml29623 		}
22833859Sml29623 		if ((nxge_port_rcr_size > NXGE_NIU_CONTIG_RCR_MAX) ||
22846512Ssowmini 		    (!ISP2(nxge_port_rcr_size))) {
22853859Sml29623 			nxge_port_rcr_size = NXGE_NIU_CONTIG_RCR_MAX;
22863859Sml29623 		}
22873859Sml29623 	}
22883859Sml29623 #endif
22893859Sml29623 
22903859Sml29623 	/*
22913859Sml29623 	 * Addresses of receive block ring, receive completion ring and the
22923859Sml29623 	 * mailbox must be all cache-aligned (64 bytes).
22933859Sml29623 	 */
22943859Sml29623 	rx_cntl_alloc_size = nxge_port_rbr_size + nxge_port_rbr_spare_size;
22953859Sml29623 	rx_cntl_alloc_size *= (sizeof (rx_desc_t));
22963859Sml29623 	rx_cntl_alloc_size += (sizeof (rcr_entry_t) * nxge_port_rcr_size);
22973859Sml29623 	rx_cntl_alloc_size += sizeof (rxdma_mailbox_t);
22983859Sml29623 
22993859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_alloc_rx_mem_pool: "
23006512Ssowmini 	    "nxge_port_rbr_size = %d nxge_port_rbr_spare_size = %d "
23016512Ssowmini 	    "nxge_port_rcr_size = %d "
23026512Ssowmini 	    "rx_cntl_alloc_size = %d",
23036512Ssowmini 	    nxge_port_rbr_size, nxge_port_rbr_spare_size,
23046512Ssowmini 	    nxge_port_rcr_size,
23056512Ssowmini 	    rx_cntl_alloc_size));
23063859Sml29623 
23073859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
23083859Sml29623 	if (nxgep->niu_type == N2_NIU) {
23096495Sspeer 		uint32_t rx_buf_alloc_size = (nxgep->rx_default_block_size *
23106495Sspeer 		    (nxge_port_rbr_size + nxge_port_rbr_spare_size));
23116495Sspeer 
23123859Sml29623 		if (!ISP2(rx_buf_alloc_size)) {
23133859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
23146512Ssowmini 			    "==> nxge_alloc_rx_mem_pool: "
23156512Ssowmini 			    " must be power of 2"));
23163859Sml29623 			status |= (NXGE_ERROR | NXGE_DDI_FAILED);
23173859Sml29623 			goto nxge_alloc_rx_mem_pool_exit;
23183859Sml29623 		}
23193859Sml29623 
23203859Sml29623 		if (rx_buf_alloc_size > (1 << 22)) {
23213859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
23226512Ssowmini 			    "==> nxge_alloc_rx_mem_pool: "
23236512Ssowmini 			    " limit size to 4M"));
23243859Sml29623 			status |= (NXGE_ERROR | NXGE_DDI_FAILED);
23253859Sml29623 			goto nxge_alloc_rx_mem_pool_exit;
23263859Sml29623 		}
23273859Sml29623 
23283859Sml29623 		if (rx_cntl_alloc_size < 0x2000) {
23293859Sml29623 			rx_cntl_alloc_size = 0x2000;
23303859Sml29623 		}
23313859Sml29623 	}
23323859Sml29623 #endif
23333859Sml29623 	nxgep->nxge_port_rbr_size = nxge_port_rbr_size;
23343859Sml29623 	nxgep->nxge_port_rcr_size = nxge_port_rcr_size;
23356495Sspeer 	nxgep->nxge_port_rbr_spare_size = nxge_port_rbr_spare_size;
23366495Sspeer 	nxgep->nxge_port_rx_cntl_alloc_size = rx_cntl_alloc_size;
23376495Sspeer 
23386495Sspeer 	dma_poolp->ndmas = p_cfgp->max_rdcs;
23393859Sml29623 	dma_poolp->num_chunks = num_chunks;
23403859Sml29623 	dma_poolp->buf_allocated = B_TRUE;
23413859Sml29623 	nxgep->rx_buf_pool_p = dma_poolp;
23423859Sml29623 	dma_poolp->dma_buf_pool_p = dma_buf_p;
23433859Sml29623 
23446495Sspeer 	dma_cntl_poolp->ndmas = p_cfgp->max_rdcs;
23453859Sml29623 	dma_cntl_poolp->buf_allocated = B_TRUE;
23463859Sml29623 	nxgep->rx_cntl_pool_p = dma_cntl_poolp;
23473859Sml29623 	dma_cntl_poolp->dma_buf_pool_p = dma_cntl_p;
23483859Sml29623 
23496495Sspeer 	/* Allocate the receive rings, too. */
23506495Sspeer 	nxgep->rx_rbr_rings =
23516512Ssowmini 	    KMEM_ZALLOC(sizeof (rx_rbr_rings_t), KM_SLEEP);
23526495Sspeer 	nxgep->rx_rbr_rings->rbr_rings =
23536512Ssowmini 	    KMEM_ZALLOC(sizeof (p_rx_rbr_ring_t) * rdc_max, KM_SLEEP);
23546495Sspeer 	nxgep->rx_rcr_rings =
23556512Ssowmini 	    KMEM_ZALLOC(sizeof (rx_rcr_rings_t), KM_SLEEP);
23566495Sspeer 	nxgep->rx_rcr_rings->rcr_rings =
23576512Ssowmini 	    KMEM_ZALLOC(sizeof (p_rx_rcr_ring_t) * rdc_max, KM_SLEEP);
23586495Sspeer 	nxgep->rx_mbox_areas_p =
23596512Ssowmini 	    KMEM_ZALLOC(sizeof (rx_mbox_areas_t), KM_SLEEP);
23606495Sspeer 	nxgep->rx_mbox_areas_p->rxmbox_areas =
23616512Ssowmini 	    KMEM_ZALLOC(sizeof (p_rx_mbox_t) * rdc_max, KM_SLEEP);
23626495Sspeer 
23636495Sspeer 	nxgep->rx_rbr_rings->ndmas = nxgep->rx_rcr_rings->ndmas =
23646495Sspeer 	    p_cfgp->max_rdcs;
23656495Sspeer 
23663859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
23676512Ssowmini 	    "<== nxge_alloc_rx_mem_pool:status 0x%08x", status));
23683859Sml29623 
23693859Sml29623 nxge_alloc_rx_mem_pool_exit:
23706495Sspeer 	return (status);
23716495Sspeer }
23726495Sspeer 
23736495Sspeer /*
23746495Sspeer  * nxge_alloc_rxb
23756495Sspeer  *
23766495Sspeer  *	Allocate buffers for an RDC.
23776495Sspeer  *
23786495Sspeer  * Arguments:
23796495Sspeer  * 	nxgep
23806495Sspeer  * 	channel	The channel to map into our kernel space.
23816495Sspeer  *
23826495Sspeer  * Notes:
23836495Sspeer  *
23846495Sspeer  * NPI function calls:
23856495Sspeer  *
23866495Sspeer  * NXGE function calls:
23876495Sspeer  *
23886495Sspeer  * Registers accessed:
23896495Sspeer  *
23906495Sspeer  * Context:
23916495Sspeer  *
23926495Sspeer  * Taking apart:
23936495Sspeer  *
23946495Sspeer  * Open questions:
23956495Sspeer  *
23966495Sspeer  */
23976495Sspeer nxge_status_t
23986495Sspeer nxge_alloc_rxb(
23996495Sspeer 	p_nxge_t nxgep,
24006495Sspeer 	int channel)
24016495Sspeer {
24026495Sspeer 	size_t			rx_buf_alloc_size;
24036495Sspeer 	nxge_status_t		status = NXGE_OK;
24046495Sspeer 
24056495Sspeer 	nxge_dma_common_t	**data;
24066495Sspeer 	nxge_dma_common_t	**control;
24076495Sspeer 	uint32_t 		*num_chunks;
24086495Sspeer 
24096495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rbb"));
24106495Sspeer 
24116495Sspeer 	/*
24126495Sspeer 	 * Allocate memory for the receive buffers and descriptor rings.
24136495Sspeer 	 * Replace these allocation functions with the interface functions
24146495Sspeer 	 * provided by the partition manager if/when they are available.
24156495Sspeer 	 */
24166495Sspeer 
24176495Sspeer 	/*
24186495Sspeer 	 * Allocate memory for the receive buffer blocks.
24196495Sspeer 	 */
24206495Sspeer 	rx_buf_alloc_size = (nxgep->rx_default_block_size *
24216512Ssowmini 	    (nxgep->nxge_port_rbr_size + nxgep->nxge_port_rbr_spare_size));
24226495Sspeer 
24236495Sspeer 	data = &nxgep->rx_buf_pool_p->dma_buf_pool_p[channel];
24246495Sspeer 	num_chunks = &nxgep->rx_buf_pool_p->num_chunks[channel];
24256495Sspeer 
24266495Sspeer 	if ((status = nxge_alloc_rx_buf_dma(
24276495Sspeer 	    nxgep, channel, data, rx_buf_alloc_size,
24286495Sspeer 	    nxgep->rx_default_block_size, num_chunks)) != NXGE_OK) {
24296495Sspeer 		return (status);
24306495Sspeer 	}
24316495Sspeer 
24326495Sspeer 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "<== nxge_alloc_rxb(): "
24336495Sspeer 	    "dma %d dma_buf_p %llx &dma_buf_p %llx", channel, *data, data));
24346495Sspeer 
24356495Sspeer 	/*
24366495Sspeer 	 * Allocate memory for descriptor rings and mailbox.
24376495Sspeer 	 */
24386495Sspeer 	control = &nxgep->rx_cntl_pool_p->dma_buf_pool_p[channel];
24396495Sspeer 
24406495Sspeer 	if ((status = nxge_alloc_rx_cntl_dma(
24416495Sspeer 	    nxgep, channel, control, nxgep->nxge_port_rx_cntl_alloc_size))
24426495Sspeer 	    != NXGE_OK) {
24436495Sspeer 		nxge_free_rx_cntl_dma(nxgep, *control);
24446495Sspeer 		(*data)->buf_alloc_state |= BUF_ALLOCATED_WAIT_FREE;
24456495Sspeer 		nxge_free_rx_buf_dma(nxgep, *data, *num_chunks);
24466495Sspeer 		return (status);
24476495Sspeer 	}
24486495Sspeer 
24493859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
24506495Sspeer 	    "<== nxge_alloc_rx_mem_pool:status 0x%08x", status));
24513859Sml29623 
24523859Sml29623 	return (status);
24533859Sml29623 }
24543859Sml29623 
24556495Sspeer void
24566495Sspeer nxge_free_rxb(
24576495Sspeer 	p_nxge_t nxgep,
24586495Sspeer 	int channel)
24596495Sspeer {
24606495Sspeer 	nxge_dma_common_t	*data;
24616495Sspeer 	nxge_dma_common_t	*control;
24626495Sspeer 	uint32_t 		num_chunks;
24636495Sspeer 
24646495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rbb"));
24656495Sspeer 
24666495Sspeer 	data = nxgep->rx_buf_pool_p->dma_buf_pool_p[channel];
24676495Sspeer 	num_chunks = nxgep->rx_buf_pool_p->num_chunks[channel];
24686495Sspeer 	nxge_free_rx_buf_dma(nxgep, data, num_chunks);
24696495Sspeer 
24706495Sspeer 	nxgep->rx_buf_pool_p->dma_buf_pool_p[channel] = 0;
24716495Sspeer 	nxgep->rx_buf_pool_p->num_chunks[channel] = 0;
24726495Sspeer 
24736495Sspeer 	control = nxgep->rx_cntl_pool_p->dma_buf_pool_p[channel];
24746495Sspeer 	nxge_free_rx_cntl_dma(nxgep, control);
24756495Sspeer 
24766495Sspeer 	nxgep->rx_cntl_pool_p->dma_buf_pool_p[channel] = 0;
24776495Sspeer 
24786495Sspeer 	KMEM_FREE(data, sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK);
24796495Sspeer 	KMEM_FREE(control, sizeof (nxge_dma_common_t));
24806495Sspeer 
24816495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_alloc_rbb"));
24826495Sspeer }
24836495Sspeer 
24843859Sml29623 static void
24853859Sml29623 nxge_free_rx_mem_pool(p_nxge_t nxgep)
24863859Sml29623 {
24876495Sspeer 	int rdc_max = NXGE_MAX_RDCS;
24883859Sml29623 
24893859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_free_rx_mem_pool"));
24903859Sml29623 
24916495Sspeer 	if (!nxgep->rx_buf_pool_p || !nxgep->rx_buf_pool_p->buf_allocated) {
24923859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
24936512Ssowmini 		    "<== nxge_free_rx_mem_pool "
24946512Ssowmini 		    "(null rx buf pool or buf not allocated"));
24953859Sml29623 		return;
24963859Sml29623 	}
24976495Sspeer 	if (!nxgep->rx_cntl_pool_p || !nxgep->rx_cntl_pool_p->buf_allocated) {
24983859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
24996512Ssowmini 		    "<== nxge_free_rx_mem_pool "
25006512Ssowmini 		    "(null rx cntl buf pool or cntl buf not allocated"));
25013859Sml29623 		return;
25023859Sml29623 	}
25033859Sml29623 
25046495Sspeer 	KMEM_FREE(nxgep->rx_cntl_pool_p->dma_buf_pool_p,
25056495Sspeer 	    sizeof (p_nxge_dma_common_t) * rdc_max);
25066495Sspeer 	KMEM_FREE(nxgep->rx_cntl_pool_p, sizeof (nxge_dma_pool_t));
25076495Sspeer 
25086495Sspeer 	KMEM_FREE(nxgep->rx_buf_pool_p->num_chunks,
25096495Sspeer 	    sizeof (uint32_t) * rdc_max);
25106495Sspeer 	KMEM_FREE(nxgep->rx_buf_pool_p->dma_buf_pool_p,
25116495Sspeer 	    sizeof (p_nxge_dma_common_t) * rdc_max);
25126495Sspeer 	KMEM_FREE(nxgep->rx_buf_pool_p, sizeof (nxge_dma_pool_t));
25136495Sspeer 
25146495Sspeer 	nxgep->rx_buf_pool_p = 0;
25156495Sspeer 	nxgep->rx_cntl_pool_p = 0;
25166495Sspeer 
25176495Sspeer 	KMEM_FREE(nxgep->rx_rbr_rings->rbr_rings,
25186495Sspeer 	    sizeof (p_rx_rbr_ring_t) * rdc_max);
25196495Sspeer 	KMEM_FREE(nxgep->rx_rbr_rings, sizeof (rx_rbr_rings_t));
25206495Sspeer 	KMEM_FREE(nxgep->rx_rcr_rings->rcr_rings,
25216495Sspeer 	    sizeof (p_rx_rcr_ring_t) * rdc_max);
25226495Sspeer 	KMEM_FREE(nxgep->rx_rcr_rings, sizeof (rx_rcr_rings_t));
25236495Sspeer 	KMEM_FREE(nxgep->rx_mbox_areas_p->rxmbox_areas,
25246495Sspeer 	    sizeof (p_rx_mbox_t) * rdc_max);
25256495Sspeer 	KMEM_FREE(nxgep->rx_mbox_areas_p, sizeof (rx_mbox_areas_t));
25266495Sspeer 
25276495Sspeer 	nxgep->rx_rbr_rings = 0;
25286495Sspeer 	nxgep->rx_rcr_rings = 0;
25296495Sspeer 	nxgep->rx_mbox_areas_p = 0;
25303859Sml29623 
25313859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "<== nxge_free_rx_mem_pool"));
25323859Sml29623 }
25333859Sml29623 
25343859Sml29623 
25353859Sml29623 static nxge_status_t
25363859Sml29623 nxge_alloc_rx_buf_dma(p_nxge_t nxgep, uint16_t dma_channel,
25373859Sml29623 	p_nxge_dma_common_t *dmap,
25383859Sml29623 	size_t alloc_size, size_t block_size, uint32_t *num_chunks)
25393859Sml29623 {
25403859Sml29623 	p_nxge_dma_common_t 	rx_dmap;
25413859Sml29623 	nxge_status_t		status = NXGE_OK;
25423859Sml29623 	size_t			total_alloc_size;
25433859Sml29623 	size_t			allocated = 0;
25443859Sml29623 	int			i, size_index, array_size;
25456495Sspeer 	boolean_t		use_kmem_alloc = B_FALSE;
25463859Sml29623 
25473859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rx_buf_dma"));
25483859Sml29623 
25493859Sml29623 	rx_dmap = (p_nxge_dma_common_t)
25506512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK,
25516512Ssowmini 	    KM_SLEEP);
25523859Sml29623 
25533859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
25546512Ssowmini 	    " alloc_rx_buf_dma rdc %d asize %x bsize %x bbuf %llx ",
25556512Ssowmini 	    dma_channel, alloc_size, block_size, dmap));
25563859Sml29623 
25573859Sml29623 	total_alloc_size = alloc_size;
25583859Sml29623 
25593859Sml29623 #if defined(RX_USE_RECLAIM_POST)
25603859Sml29623 	total_alloc_size = alloc_size + alloc_size/4;
25613859Sml29623 #endif
25623859Sml29623 
25633859Sml29623 	i = 0;
25643859Sml29623 	size_index = 0;
25653859Sml29623 	array_size =  sizeof (alloc_sizes)/sizeof (size_t);
25663859Sml29623 	while ((alloc_sizes[size_index] < alloc_size) &&
25676512Ssowmini 	    (size_index < array_size))
25686512Ssowmini 		size_index++;
25693859Sml29623 	if (size_index >= array_size) {
25703859Sml29623 		size_index = array_size - 1;
25713859Sml29623 	}
25723859Sml29623 
25736495Sspeer 	/* For Neptune, use kmem_alloc if the kmem flag is set. */
25746495Sspeer 	if (nxgep->niu_type != N2_NIU && nxge_use_kmem_alloc) {
25756495Sspeer 		use_kmem_alloc = B_TRUE;
25766495Sspeer #if defined(__i386) || defined(__amd64)
25776495Sspeer 		size_index = 0;
25786495Sspeer #endif
25796495Sspeer 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
25806495Sspeer 		    "==> nxge_alloc_rx_buf_dma: "
25816495Sspeer 		    "Neptune use kmem_alloc() - size_index %d",
25826495Sspeer 		    size_index));
25836495Sspeer 	}
25846495Sspeer 
25853859Sml29623 	while ((allocated < total_alloc_size) &&
25866512Ssowmini 	    (size_index >= 0) && (i < NXGE_DMA_BLOCK)) {
25873859Sml29623 		rx_dmap[i].dma_chunk_index = i;
25883859Sml29623 		rx_dmap[i].block_size = block_size;
25893859Sml29623 		rx_dmap[i].alength = alloc_sizes[size_index];
25903859Sml29623 		rx_dmap[i].orig_alength = rx_dmap[i].alength;
25913859Sml29623 		rx_dmap[i].nblocks = alloc_sizes[size_index] / block_size;
25923859Sml29623 		rx_dmap[i].dma_channel = dma_channel;
25933859Sml29623 		rx_dmap[i].contig_alloc_type = B_FALSE;
25946495Sspeer 		rx_dmap[i].kmem_alloc_type = B_FALSE;
25956495Sspeer 		rx_dmap[i].buf_alloc_type = DDI_MEM_ALLOC;
25963859Sml29623 
25973859Sml29623 		/*
25983859Sml29623 		 * N2/NIU: data buffers must be contiguous as the driver
25993859Sml29623 		 *	   needs to call Hypervisor api to set up
26003859Sml29623 		 *	   logical pages.
26013859Sml29623 		 */
26023859Sml29623 		if ((nxgep->niu_type == N2_NIU) && (NXGE_DMA_BLOCK == 1)) {
26033859Sml29623 			rx_dmap[i].contig_alloc_type = B_TRUE;
26046495Sspeer 			rx_dmap[i].buf_alloc_type = CONTIG_MEM_ALLOC;
26056495Sspeer 		} else if (use_kmem_alloc) {
26066495Sspeer 			/* For Neptune, use kmem_alloc */
26076495Sspeer 			NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26086495Sspeer 			    "==> nxge_alloc_rx_buf_dma: "
26096495Sspeer 			    "Neptune use kmem_alloc()"));
26106495Sspeer 			rx_dmap[i].kmem_alloc_type = B_TRUE;
26116495Sspeer 			rx_dmap[i].buf_alloc_type = KMEM_ALLOC;
26123859Sml29623 		}
26133859Sml29623 
26143859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26156512Ssowmini 		    "alloc_rx_buf_dma rdc %d chunk %d bufp %llx size %x "
26166512Ssowmini 		    "i %d nblocks %d alength %d",
26176512Ssowmini 		    dma_channel, i, &rx_dmap[i], block_size,
26186512Ssowmini 		    i, rx_dmap[i].nblocks,
26196512Ssowmini 		    rx_dmap[i].alength));
26203859Sml29623 		status = nxge_dma_mem_alloc(nxgep, nxge_force_dma,
26216512Ssowmini 		    &nxge_rx_dma_attr,
26226512Ssowmini 		    rx_dmap[i].alength,
26236512Ssowmini 		    &nxge_dev_buf_dma_acc_attr,
26246512Ssowmini 		    DDI_DMA_READ | DDI_DMA_STREAMING,
26256512Ssowmini 		    (p_nxge_dma_common_t)(&rx_dmap[i]));
26263859Sml29623 		if (status != NXGE_OK) {
26273859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
26286495Sspeer 			    "nxge_alloc_rx_buf_dma: Alloc Failed: "
26296495Sspeer 			    "dma %d size_index %d size requested %d",
26306495Sspeer 			    dma_channel,
26316495Sspeer 			    size_index,
26326495Sspeer 			    rx_dmap[i].alength));
26333859Sml29623 			size_index--;
26343859Sml29623 		} else {
26356495Sspeer 			rx_dmap[i].buf_alloc_state = BUF_ALLOCATED;
26366495Sspeer 			NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26376495Sspeer 			    " nxge_alloc_rx_buf_dma DONE  alloc mem: "
26386495Sspeer 			    "dma %d dma_buf_p $%p kaddrp $%p alength %d "
26396495Sspeer 			    "buf_alloc_state %d alloc_type %d",
26406495Sspeer 			    dma_channel,
26416495Sspeer 			    &rx_dmap[i],
26426495Sspeer 			    rx_dmap[i].kaddrp,
26436495Sspeer 			    rx_dmap[i].alength,
26446495Sspeer 			    rx_dmap[i].buf_alloc_state,
26456495Sspeer 			    rx_dmap[i].buf_alloc_type));
26466495Sspeer 			NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26476495Sspeer 			    " alloc_rx_buf_dma allocated rdc %d "
26486495Sspeer 			    "chunk %d size %x dvma %x bufp %llx kaddrp $%p",
26496495Sspeer 			    dma_channel, i, rx_dmap[i].alength,
26506495Sspeer 			    rx_dmap[i].ioaddr_pp, &rx_dmap[i],
26516495Sspeer 			    rx_dmap[i].kaddrp));
26523859Sml29623 			i++;
26533859Sml29623 			allocated += alloc_sizes[size_index];
26543859Sml29623 		}
26553859Sml29623 	}
26563859Sml29623 
26573859Sml29623 	if (allocated < total_alloc_size) {
26585770Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
26596495Sspeer 		    "==> nxge_alloc_rx_buf_dma: not enough for channel %d "
26605770Sml29623 		    "allocated 0x%x requested 0x%x",
26615770Sml29623 		    dma_channel,
26625770Sml29623 		    allocated, total_alloc_size));
26635770Sml29623 		status = NXGE_ERROR;
26643859Sml29623 		goto nxge_alloc_rx_mem_fail1;
26653859Sml29623 	}
26663859Sml29623 
26675770Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26686495Sspeer 	    "==> nxge_alloc_rx_buf_dma: Allocated for channel %d "
26695770Sml29623 	    "allocated 0x%x requested 0x%x",
26705770Sml29623 	    dma_channel,
26715770Sml29623 	    allocated, total_alloc_size));
26725770Sml29623 
26733859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
26746512Ssowmini 	    " alloc_rx_buf_dma rdc %d allocated %d chunks",
26756512Ssowmini 	    dma_channel, i));
26763859Sml29623 	*num_chunks = i;
26773859Sml29623 	*dmap = rx_dmap;
26783859Sml29623 
26793859Sml29623 	goto nxge_alloc_rx_mem_exit;
26803859Sml29623 
26813859Sml29623 nxge_alloc_rx_mem_fail1:
26823859Sml29623 	KMEM_FREE(rx_dmap, sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK);
26833859Sml29623 
26843859Sml29623 nxge_alloc_rx_mem_exit:
26853859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
26866512Ssowmini 	    "<== nxge_alloc_rx_buf_dma status 0x%08x", status));
26873859Sml29623 
26883859Sml29623 	return (status);
26893859Sml29623 }
26903859Sml29623 
26913859Sml29623 /*ARGSUSED*/
26923859Sml29623 static void
26933859Sml29623 nxge_free_rx_buf_dma(p_nxge_t nxgep, p_nxge_dma_common_t dmap,
26943859Sml29623     uint32_t num_chunks)
26953859Sml29623 {
26963859Sml29623 	int		i;
26973859Sml29623 
26983859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26996512Ssowmini 	    "==> nxge_free_rx_buf_dma: # of chunks %d", num_chunks));
27003859Sml29623 
27016495Sspeer 	if (dmap == 0)
27026495Sspeer 		return;
27036495Sspeer 
27043859Sml29623 	for (i = 0; i < num_chunks; i++) {
27053859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
27066512Ssowmini 		    "==> nxge_free_rx_buf_dma: chunk %d dmap 0x%llx",
27076512Ssowmini 		    i, dmap));
27086495Sspeer 		nxge_dma_free_rx_data_buf(dmap++);
27093859Sml29623 	}
27103859Sml29623 
27113859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_free_rx_buf_dma"));
27123859Sml29623 }
27133859Sml29623 
27143859Sml29623 /*ARGSUSED*/
27153859Sml29623 static nxge_status_t
27163859Sml29623 nxge_alloc_rx_cntl_dma(p_nxge_t nxgep, uint16_t dma_channel,
27173859Sml29623     p_nxge_dma_common_t *dmap, size_t size)
27183859Sml29623 {
27193859Sml29623 	p_nxge_dma_common_t 	rx_dmap;
27203859Sml29623 	nxge_status_t		status = NXGE_OK;
27213859Sml29623 
27223859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rx_cntl_dma"));
27233859Sml29623 
27243859Sml29623 	rx_dmap = (p_nxge_dma_common_t)
27256512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_common_t), KM_SLEEP);
27263859Sml29623 
27273859Sml29623 	rx_dmap->contig_alloc_type = B_FALSE;
27286495Sspeer 	rx_dmap->kmem_alloc_type = B_FALSE;
27293859Sml29623 
27303859Sml29623 	status = nxge_dma_mem_alloc(nxgep, nxge_force_dma,
27316512Ssowmini 	    &nxge_desc_dma_attr,
27326512Ssowmini 	    size,
27336512Ssowmini 	    &nxge_dev_desc_dma_acc_attr,
27346512Ssowmini 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
27356512Ssowmini 	    rx_dmap);
27363859Sml29623 	if (status != NXGE_OK) {
27373859Sml29623 		goto nxge_alloc_rx_cntl_dma_fail1;
27383859Sml29623 	}
27393859Sml29623 
27403859Sml29623 	*dmap = rx_dmap;
27413859Sml29623 	goto nxge_alloc_rx_cntl_dma_exit;
27423859Sml29623 
27433859Sml29623 nxge_alloc_rx_cntl_dma_fail1:
27443859Sml29623 	KMEM_FREE(rx_dmap, sizeof (nxge_dma_common_t));
27453859Sml29623 
27463859Sml29623 nxge_alloc_rx_cntl_dma_exit:
27473859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
27486512Ssowmini 	    "<== nxge_alloc_rx_cntl_dma status 0x%08x", status));
27493859Sml29623 
27503859Sml29623 	return (status);
27513859Sml29623 }
27523859Sml29623 
27533859Sml29623 /*ARGSUSED*/
27543859Sml29623 static void
27553859Sml29623 nxge_free_rx_cntl_dma(p_nxge_t nxgep, p_nxge_dma_common_t dmap)
27563859Sml29623 {
27573859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_free_rx_cntl_dma"));
27583859Sml29623 
27596495Sspeer 	if (dmap == 0)
27606495Sspeer 		return;
27616495Sspeer 
27623859Sml29623 	nxge_dma_mem_free(dmap);
27633859Sml29623 
27643859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_free_rx_cntl_dma"));
27653859Sml29623 }
27663859Sml29623 
27676495Sspeer typedef struct {
27686495Sspeer 	size_t	tx_size;
27696495Sspeer 	size_t	cr_size;
27706495Sspeer 	size_t	threshhold;
27716495Sspeer } nxge_tdc_sizes_t;
27726495Sspeer 
27736495Sspeer static
27746495Sspeer nxge_status_t
27756495Sspeer nxge_tdc_sizes(
27766495Sspeer 	nxge_t *nxgep,
27776495Sspeer 	nxge_tdc_sizes_t *sizes)
27786495Sspeer {
27796495Sspeer 	uint32_t threshhold;	/* The bcopy() threshhold */
27806495Sspeer 	size_t tx_size;		/* Transmit buffer size */
27816495Sspeer 	size_t cr_size;		/* Completion ring size */
27826495Sspeer 
27836495Sspeer 	/*
27846495Sspeer 	 * Assume that each DMA channel will be configured with the
27856495Sspeer 	 * default transmit buffer size for copying transmit data.
27866495Sspeer 	 * (If a packet is bigger than this, it will not be copied.)
27876495Sspeer 	 */
27886495Sspeer 	if (nxgep->niu_type == N2_NIU) {
27896495Sspeer 		threshhold = TX_BCOPY_SIZE;
27906495Sspeer 	} else {
27916495Sspeer 		threshhold = nxge_bcopy_thresh;
27926495Sspeer 	}
27936495Sspeer 	tx_size = nxge_tx_ring_size * threshhold;
27946495Sspeer 
27956495Sspeer 	cr_size = nxge_tx_ring_size * sizeof (tx_desc_t);
27966495Sspeer 	cr_size += sizeof (txdma_mailbox_t);
27976495Sspeer 
27986495Sspeer #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
27996495Sspeer 	if (nxgep->niu_type == N2_NIU) {
28006495Sspeer 		if (!ISP2(tx_size)) {
28016495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
28026512Ssowmini 			    "==> nxge_tdc_sizes: Tx size"
28036512Ssowmini 			    " must be power of 2"));
28046495Sspeer 			return (NXGE_ERROR);
28056495Sspeer 		}
28066495Sspeer 
28076495Sspeer 		if (tx_size > (1 << 22)) {
28086495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
28096512Ssowmini 			    "==> nxge_tdc_sizes: Tx size"
28106512Ssowmini 			    " limited to 4M"));
28116495Sspeer 			return (NXGE_ERROR);
28126495Sspeer 		}
28136495Sspeer 
28146495Sspeer 		if (cr_size < 0x2000)
28156495Sspeer 			cr_size = 0x2000;
28166495Sspeer 	}
28176495Sspeer #endif
28186495Sspeer 
28196495Sspeer 	sizes->threshhold = threshhold;
28206495Sspeer 	sizes->tx_size = tx_size;
28216495Sspeer 	sizes->cr_size = cr_size;
28226495Sspeer 
28236495Sspeer 	return (NXGE_OK);
28246495Sspeer }
28256495Sspeer /*
28266495Sspeer  * nxge_alloc_txb
28276495Sspeer  *
28286495Sspeer  *	Allocate buffers for an TDC.
28296495Sspeer  *
28306495Sspeer  * Arguments:
28316495Sspeer  * 	nxgep
28326495Sspeer  * 	channel	The channel to map into our kernel space.
28336495Sspeer  *
28346495Sspeer  * Notes:
28356495Sspeer  *
28366495Sspeer  * NPI function calls:
28376495Sspeer  *
28386495Sspeer  * NXGE function calls:
28396495Sspeer  *
28406495Sspeer  * Registers accessed:
28416495Sspeer  *
28426495Sspeer  * Context:
28436495Sspeer  *
28446495Sspeer  * Taking apart:
28456495Sspeer  *
28466495Sspeer  * Open questions:
28476495Sspeer  *
28486495Sspeer  */
28496495Sspeer nxge_status_t
28506495Sspeer nxge_alloc_txb(
28516495Sspeer 	p_nxge_t nxgep,
28526495Sspeer 	int channel)
28536495Sspeer {
28546495Sspeer 	nxge_dma_common_t	**dma_buf_p;
28556495Sspeer 	nxge_dma_common_t	**dma_cntl_p;
28566495Sspeer 	uint32_t 		*num_chunks;
28576495Sspeer 	nxge_status_t		status = NXGE_OK;
28586495Sspeer 
28596495Sspeer 	nxge_tdc_sizes_t	sizes;
28606495Sspeer 
28616495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_tbb"));
28626495Sspeer 
28636495Sspeer 	if (nxge_tdc_sizes(nxgep, &sizes) != NXGE_OK)
28646495Sspeer 		return (NXGE_ERROR);
28656495Sspeer 
28666495Sspeer 	/*
28676495Sspeer 	 * Allocate memory for transmit buffers and descriptor rings.
28686495Sspeer 	 * Replace these allocation functions with the interface functions
28696495Sspeer 	 * provided by the partition manager Real Soon Now.
28706495Sspeer 	 */
28716495Sspeer 	dma_buf_p = &nxgep->tx_buf_pool_p->dma_buf_pool_p[channel];
28726495Sspeer 	num_chunks = &nxgep->tx_buf_pool_p->num_chunks[channel];
28736495Sspeer 
28746495Sspeer 	dma_cntl_p = &nxgep->tx_cntl_pool_p->dma_buf_pool_p[channel];
28756495Sspeer 
28766495Sspeer 	/*
28776495Sspeer 	 * Allocate memory for transmit buffers and descriptor rings.
28786495Sspeer 	 * Replace allocation functions with interface functions provided
28796495Sspeer 	 * by the partition manager when it is available.
28806495Sspeer 	 *
28816495Sspeer 	 * Allocate memory for the transmit buffer pool.
28826495Sspeer 	 */
28836495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
28846512Ssowmini 	    "sizes: tx: %ld, cr:%ld, th:%ld",
28856512Ssowmini 	    sizes.tx_size, sizes.cr_size, sizes.threshhold));
28866495Sspeer 
28876495Sspeer 	*num_chunks = 0;
28886495Sspeer 	status = nxge_alloc_tx_buf_dma(nxgep, channel, dma_buf_p,
28896495Sspeer 	    sizes.tx_size, sizes.threshhold, num_chunks);
28906495Sspeer 	if (status != NXGE_OK) {
28916495Sspeer 		cmn_err(CE_NOTE, "nxge_alloc_tx_buf_dma failed!");
28926495Sspeer 		return (status);
28936495Sspeer 	}
28946495Sspeer 
28956495Sspeer 	/*
28966495Sspeer 	 * Allocate memory for descriptor rings and mailbox.
28976495Sspeer 	 */
28986495Sspeer 	status = nxge_alloc_tx_cntl_dma(nxgep, channel, dma_cntl_p,
28996495Sspeer 	    sizes.cr_size);
29006495Sspeer 	if (status != NXGE_OK) {
29016495Sspeer 		nxge_free_tx_buf_dma(nxgep, *dma_buf_p, *num_chunks);
29026495Sspeer 		cmn_err(CE_NOTE, "nxge_alloc_tx_cntl_dma failed!");
29036495Sspeer 		return (status);
29046495Sspeer 	}
29056495Sspeer 
29066495Sspeer 	return (NXGE_OK);
29076495Sspeer }
29086495Sspeer 
29096495Sspeer void
29106495Sspeer nxge_free_txb(
29116495Sspeer 	p_nxge_t nxgep,
29126495Sspeer 	int channel)
29136495Sspeer {
29146495Sspeer 	nxge_dma_common_t	*data;
29156495Sspeer 	nxge_dma_common_t	*control;
29166495Sspeer 	uint32_t 		num_chunks;
29176495Sspeer 
29186495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_free_txb"));
29196495Sspeer 
29206495Sspeer 	data = nxgep->tx_buf_pool_p->dma_buf_pool_p[channel];
29216495Sspeer 	num_chunks = nxgep->tx_buf_pool_p->num_chunks[channel];
29226495Sspeer 	nxge_free_tx_buf_dma(nxgep, data, num_chunks);
29236495Sspeer 
29246495Sspeer 	nxgep->tx_buf_pool_p->dma_buf_pool_p[channel] = 0;
29256495Sspeer 	nxgep->tx_buf_pool_p->num_chunks[channel] = 0;
29266495Sspeer 
29276495Sspeer 	control = nxgep->tx_cntl_pool_p->dma_buf_pool_p[channel];
29286495Sspeer 	nxge_free_tx_cntl_dma(nxgep, control);
29296495Sspeer 
29306495Sspeer 	nxgep->tx_cntl_pool_p->dma_buf_pool_p[channel] = 0;
29316495Sspeer 
29326495Sspeer 	KMEM_FREE(data, sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK);
29336495Sspeer 	KMEM_FREE(control, sizeof (nxge_dma_common_t));
29346495Sspeer 
29356495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_free_txb"));
29366495Sspeer }
29376495Sspeer 
29386495Sspeer /*
29396495Sspeer  * nxge_alloc_tx_mem_pool
29406495Sspeer  *
29416495Sspeer  *	This function allocates all of the per-port TDC control data structures.
29426495Sspeer  *	The per-channel (TDC) data structures are allocated when needed.
29436495Sspeer  *
29446495Sspeer  * Arguments:
29456495Sspeer  * 	nxgep
29466495Sspeer  *
29476495Sspeer  * Notes:
29486495Sspeer  *
29496495Sspeer  * Context:
29506495Sspeer  *	Any domain
29516495Sspeer  */
29526495Sspeer nxge_status_t
29533859Sml29623 nxge_alloc_tx_mem_pool(p_nxge_t nxgep)
29543859Sml29623 {
29556495Sspeer 	nxge_hw_pt_cfg_t	*p_cfgp;
29566495Sspeer 	nxge_dma_pool_t		*dma_poolp;
29576495Sspeer 	nxge_dma_common_t	**dma_buf_p;
29586495Sspeer 	nxge_dma_pool_t		*dma_cntl_poolp;
29596495Sspeer 	nxge_dma_common_t	**dma_cntl_p;
29603859Sml29623 	uint32_t		*num_chunks; /* per dma */
29616495Sspeer 	int			tdc_max;
29623859Sml29623 
29633859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "==> nxge_alloc_tx_mem_pool"));
29643859Sml29623 
29656495Sspeer 	p_cfgp = &nxgep->pt_config.hw_config;
29666495Sspeer 	tdc_max = NXGE_MAX_TDCS;
29676495Sspeer 
29683859Sml29623 	/*
29693859Sml29623 	 * Allocate memory for each transmit DMA channel.
29703859Sml29623 	 */
29713859Sml29623 	dma_poolp = (p_nxge_dma_pool_t)KMEM_ZALLOC(sizeof (nxge_dma_pool_t),
29726512Ssowmini 	    KM_SLEEP);
29733859Sml29623 	dma_buf_p = (p_nxge_dma_common_t *)KMEM_ZALLOC(
29746512Ssowmini 	    sizeof (p_nxge_dma_common_t) * tdc_max, KM_SLEEP);
29753859Sml29623 
29763859Sml29623 	dma_cntl_poolp = (p_nxge_dma_pool_t)
29776512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_pool_t), KM_SLEEP);
29783859Sml29623 	dma_cntl_p = (p_nxge_dma_common_t *)KMEM_ZALLOC(
29796512Ssowmini 	    sizeof (p_nxge_dma_common_t) * tdc_max, KM_SLEEP);
29803859Sml29623 
29815770Sml29623 	if (nxge_tx_ring_size > TDC_DEFAULT_MAX) {
29825770Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM_CTL,
29835770Sml29623 		    "nxge_alloc_tx_mem_pool: TDC too high %d, "
29845770Sml29623 		    "set to default %d",
29855770Sml29623 		    nxge_tx_ring_size, TDC_DEFAULT_MAX));
29865770Sml29623 		nxge_tx_ring_size = TDC_DEFAULT_MAX;
29875770Sml29623 	}
29885770Sml29623 
29893859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
29903859Sml29623 	/*
29913859Sml29623 	 * N2/NIU has limitation on the descriptor sizes (contiguous
29923859Sml29623 	 * memory allocation on data buffers to 4M (contig_mem_alloc)
29933859Sml29623 	 * and little endian for control buffers (must use the ddi/dki mem alloc
29943859Sml29623 	 * function). The transmit ring is limited to 8K (includes the
29953859Sml29623 	 * mailbox).
29963859Sml29623 	 */
29973859Sml29623 	if (nxgep->niu_type == N2_NIU) {
29983859Sml29623 		if ((nxge_tx_ring_size > NXGE_NIU_CONTIG_TX_MAX) ||
29996512Ssowmini 		    (!ISP2(nxge_tx_ring_size))) {
30003859Sml29623 			nxge_tx_ring_size = NXGE_NIU_CONTIG_TX_MAX;
30013859Sml29623 		}
30023859Sml29623 	}
30033859Sml29623 #endif
30043859Sml29623 
30053859Sml29623 	nxgep->nxge_port_tx_ring_size = nxge_tx_ring_size;
30063859Sml29623 
30073859Sml29623 	num_chunks = (uint32_t *)KMEM_ZALLOC(
30086512Ssowmini 	    sizeof (uint32_t) * tdc_max, KM_SLEEP);
30096495Sspeer 
30106495Sspeer 	dma_poolp->ndmas = p_cfgp->tdc.owned;
30113859Sml29623 	dma_poolp->num_chunks = num_chunks;
30123859Sml29623 	dma_poolp->dma_buf_pool_p = dma_buf_p;
30133859Sml29623 	nxgep->tx_buf_pool_p = dma_poolp;
30143859Sml29623 
30156495Sspeer 	dma_poolp->buf_allocated = B_TRUE;
30166495Sspeer 
30176495Sspeer 	dma_cntl_poolp->ndmas = p_cfgp->tdc.owned;
30183859Sml29623 	dma_cntl_poolp->dma_buf_pool_p = dma_cntl_p;
30193859Sml29623 	nxgep->tx_cntl_pool_p = dma_cntl_poolp;
30203859Sml29623 
30216495Sspeer 	dma_cntl_poolp->buf_allocated = B_TRUE;
30226495Sspeer 
30236495Sspeer 	nxgep->tx_rings =
30246495Sspeer 	    KMEM_ZALLOC(sizeof (tx_rings_t), KM_SLEEP);
30256495Sspeer 	nxgep->tx_rings->rings =
30266495Sspeer 	    KMEM_ZALLOC(sizeof (p_tx_ring_t) * tdc_max, KM_SLEEP);
30276495Sspeer 	nxgep->tx_mbox_areas_p =
30286495Sspeer 	    KMEM_ZALLOC(sizeof (tx_mbox_areas_t), KM_SLEEP);
30296495Sspeer 	nxgep->tx_mbox_areas_p->txmbox_areas_p =
30306495Sspeer 	    KMEM_ZALLOC(sizeof (p_tx_mbox_t) * tdc_max, KM_SLEEP);
30316495Sspeer 
30326495Sspeer 	nxgep->tx_rings->ndmas = p_cfgp->tdc.owned;
30336495Sspeer 
30343859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL,
30356512Ssowmini 	    "==> nxge_alloc_tx_mem_pool: ndmas %d poolp->ndmas %d",
30366512Ssowmini 	    tdc_max, dma_poolp->ndmas));
30376495Sspeer 
30386495Sspeer 	return (NXGE_OK);
30393859Sml29623 }
30403859Sml29623 
30416495Sspeer nxge_status_t
30423859Sml29623 nxge_alloc_tx_buf_dma(p_nxge_t nxgep, uint16_t dma_channel,
30433859Sml29623     p_nxge_dma_common_t *dmap, size_t alloc_size,
30443859Sml29623     size_t block_size, uint32_t *num_chunks)
30453859Sml29623 {
30463859Sml29623 	p_nxge_dma_common_t 	tx_dmap;
30473859Sml29623 	nxge_status_t		status = NXGE_OK;
30483859Sml29623 	size_t			total_alloc_size;
30493859Sml29623 	size_t			allocated = 0;
30503859Sml29623 	int			i, size_index, array_size;
30513859Sml29623 
30523859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_tx_buf_dma"));
30533859Sml29623 
30543859Sml29623 	tx_dmap = (p_nxge_dma_common_t)
30556512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK,
30566512Ssowmini 	    KM_SLEEP);
30573859Sml29623 
30583859Sml29623 	total_alloc_size = alloc_size;
30593859Sml29623 	i = 0;
30603859Sml29623 	size_index = 0;
30613859Sml29623 	array_size =  sizeof (alloc_sizes) /  sizeof (size_t);
30623859Sml29623 	while ((alloc_sizes[size_index] < alloc_size) &&
30636512Ssowmini 	    (size_index < array_size))
30643859Sml29623 		size_index++;
30653859Sml29623 	if (size_index >= array_size) {
30663859Sml29623 		size_index = array_size - 1;
30673859Sml29623 	}
30683859Sml29623 
30693859Sml29623 	while ((allocated < total_alloc_size) &&
30706512Ssowmini 	    (size_index >= 0) && (i < NXGE_DMA_BLOCK)) {
30713859Sml29623 
30723859Sml29623 		tx_dmap[i].dma_chunk_index = i;
30733859Sml29623 		tx_dmap[i].block_size = block_size;
30743859Sml29623 		tx_dmap[i].alength = alloc_sizes[size_index];
30753859Sml29623 		tx_dmap[i].orig_alength = tx_dmap[i].alength;
30763859Sml29623 		tx_dmap[i].nblocks = alloc_sizes[size_index] / block_size;
30773859Sml29623 		tx_dmap[i].dma_channel = dma_channel;
30783859Sml29623 		tx_dmap[i].contig_alloc_type = B_FALSE;
30796495Sspeer 		tx_dmap[i].kmem_alloc_type = B_FALSE;
30803859Sml29623 
30813859Sml29623 		/*
30823859Sml29623 		 * N2/NIU: data buffers must be contiguous as the driver
30833859Sml29623 		 *	   needs to call Hypervisor api to set up
30843859Sml29623 		 *	   logical pages.
30853859Sml29623 		 */
30863859Sml29623 		if ((nxgep->niu_type == N2_NIU) && (NXGE_DMA_BLOCK == 1)) {
30873859Sml29623 			tx_dmap[i].contig_alloc_type = B_TRUE;
30883859Sml29623 		}
30893859Sml29623 
30903859Sml29623 		status = nxge_dma_mem_alloc(nxgep, nxge_force_dma,
30916512Ssowmini 		    &nxge_tx_dma_attr,
30926512Ssowmini 		    tx_dmap[i].alength,
30936512Ssowmini 		    &nxge_dev_buf_dma_acc_attr,
30946512Ssowmini 		    DDI_DMA_WRITE | DDI_DMA_STREAMING,
30956512Ssowmini 		    (p_nxge_dma_common_t)(&tx_dmap[i]));
30963859Sml29623 		if (status != NXGE_OK) {
30973859Sml29623 			size_index--;
30983859Sml29623 		} else {
30993859Sml29623 			i++;
31003859Sml29623 			allocated += alloc_sizes[size_index];
31013859Sml29623 		}
31023859Sml29623 	}
31033859Sml29623 
31043859Sml29623 	if (allocated < total_alloc_size) {
31055770Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
31065770Sml29623 		    "==> nxge_alloc_tx_buf_dma: not enough channel %d: "
31075770Sml29623 		    "allocated 0x%x requested 0x%x",
31085770Sml29623 		    dma_channel,
31095770Sml29623 		    allocated, total_alloc_size));
31105770Sml29623 		status = NXGE_ERROR;
31113859Sml29623 		goto nxge_alloc_tx_mem_fail1;
31123859Sml29623 	}
31133859Sml29623 
31145770Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
31155770Sml29623 	    "==> nxge_alloc_tx_buf_dma: Allocated for channel %d: "
31165770Sml29623 	    "allocated 0x%x requested 0x%x",
31175770Sml29623 	    dma_channel,
31185770Sml29623 	    allocated, total_alloc_size));
31195770Sml29623 
31203859Sml29623 	*num_chunks = i;
31213859Sml29623 	*dmap = tx_dmap;
31223859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
31236512Ssowmini 	    "==> nxge_alloc_tx_buf_dma dmap 0x%016llx num chunks %d",
31246512Ssowmini 	    *dmap, i));
31253859Sml29623 	goto nxge_alloc_tx_mem_exit;
31263859Sml29623 
31273859Sml29623 nxge_alloc_tx_mem_fail1:
31283859Sml29623 	KMEM_FREE(tx_dmap, sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK);
31293859Sml29623 
31303859Sml29623 nxge_alloc_tx_mem_exit:
31313859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
31326512Ssowmini 	    "<== nxge_alloc_tx_buf_dma status 0x%08x", status));
31333859Sml29623 
31343859Sml29623 	return (status);
31353859Sml29623 }
31363859Sml29623 
31373859Sml29623 /*ARGSUSED*/
31383859Sml29623 static void
31393859Sml29623 nxge_free_tx_buf_dma(p_nxge_t nxgep, p_nxge_dma_common_t dmap,
31403859Sml29623     uint32_t num_chunks)
31413859Sml29623 {
31423859Sml29623 	int		i;
31433859Sml29623 
31443859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "==> nxge_free_tx_buf_dma"));
31453859Sml29623 
31466495Sspeer 	if (dmap == 0)
31476495Sspeer 		return;
31486495Sspeer 
31493859Sml29623 	for (i = 0; i < num_chunks; i++) {
31503859Sml29623 		nxge_dma_mem_free(dmap++);
31513859Sml29623 	}
31523859Sml29623 
31533859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "<== nxge_free_tx_buf_dma"));
31543859Sml29623 }
31553859Sml29623 
31563859Sml29623 /*ARGSUSED*/
31576495Sspeer nxge_status_t
31583859Sml29623 nxge_alloc_tx_cntl_dma(p_nxge_t nxgep, uint16_t dma_channel,
31593859Sml29623     p_nxge_dma_common_t *dmap, size_t size)
31603859Sml29623 {
31613859Sml29623 	p_nxge_dma_common_t 	tx_dmap;
31623859Sml29623 	nxge_status_t		status = NXGE_OK;
31633859Sml29623 
31643859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_tx_cntl_dma"));
31653859Sml29623 	tx_dmap = (p_nxge_dma_common_t)
31666512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_common_t), KM_SLEEP);
31673859Sml29623 
31683859Sml29623 	tx_dmap->contig_alloc_type = B_FALSE;
31696495Sspeer 	tx_dmap->kmem_alloc_type = B_FALSE;
31703859Sml29623 
31713859Sml29623 	status = nxge_dma_mem_alloc(nxgep, nxge_force_dma,
31726512Ssowmini 	    &nxge_desc_dma_attr,
31736512Ssowmini 	    size,
31746512Ssowmini 	    &nxge_dev_desc_dma_acc_attr,
31756512Ssowmini 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
31766512Ssowmini 	    tx_dmap);
31773859Sml29623 	if (status != NXGE_OK) {
31783859Sml29623 		goto nxge_alloc_tx_cntl_dma_fail1;
31793859Sml29623 	}
31803859Sml29623 
31813859Sml29623 	*dmap = tx_dmap;
31823859Sml29623 	goto nxge_alloc_tx_cntl_dma_exit;
31833859Sml29623 
31843859Sml29623 nxge_alloc_tx_cntl_dma_fail1:
31853859Sml29623 	KMEM_FREE(tx_dmap, sizeof (nxge_dma_common_t));
31863859Sml29623 
31873859Sml29623 nxge_alloc_tx_cntl_dma_exit:
31883859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
31896512Ssowmini 	    "<== nxge_alloc_tx_cntl_dma status 0x%08x", status));
31903859Sml29623 
31913859Sml29623 	return (status);
31923859Sml29623 }
31933859Sml29623 
31943859Sml29623 /*ARGSUSED*/
31953859Sml29623 static void
31963859Sml29623 nxge_free_tx_cntl_dma(p_nxge_t nxgep, p_nxge_dma_common_t dmap)
31973859Sml29623 {
31983859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_free_tx_cntl_dma"));
31993859Sml29623 
32006495Sspeer 	if (dmap == 0)
32016495Sspeer 		return;
32026495Sspeer 
32033859Sml29623 	nxge_dma_mem_free(dmap);
32043859Sml29623 
32053859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_free_tx_cntl_dma"));
32063859Sml29623 }
32073859Sml29623 
32086495Sspeer /*
32096495Sspeer  * nxge_free_tx_mem_pool
32106495Sspeer  *
32116495Sspeer  *	This function frees all of the per-port TDC control data structures.
32126495Sspeer  *	The per-channel (TDC) data structures are freed when the channel
32136495Sspeer  *	is stopped.
32146495Sspeer  *
32156495Sspeer  * Arguments:
32166495Sspeer  * 	nxgep
32176495Sspeer  *
32186495Sspeer  * Notes:
32196495Sspeer  *
32206495Sspeer  * Context:
32216495Sspeer  *	Any domain
32226495Sspeer  */
32233859Sml29623 static void
32243859Sml29623 nxge_free_tx_mem_pool(p_nxge_t nxgep)
32253859Sml29623 {
32266495Sspeer 	int tdc_max = NXGE_MAX_TDCS;
32276495Sspeer 
32286495Sspeer 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_free_tx_mem_pool"));
32296495Sspeer 
32306495Sspeer 	if (!nxgep->tx_buf_pool_p || !nxgep->tx_buf_pool_p->buf_allocated) {
32316495Sspeer 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
32326512Ssowmini 		    "<== nxge_free_tx_mem_pool "
32336512Ssowmini 		    "(null tx buf pool or buf not allocated"));
32343859Sml29623 		return;
32353859Sml29623 	}
32366495Sspeer 	if (!nxgep->tx_cntl_pool_p || !nxgep->tx_cntl_pool_p->buf_allocated) {
32376495Sspeer 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
32386512Ssowmini 		    "<== nxge_free_tx_mem_pool "
32396512Ssowmini 		    "(null tx cntl buf pool or cntl buf not allocated"));
32403859Sml29623 		return;
32413859Sml29623 	}
32423859Sml29623 
32436495Sspeer 	/* 1. Free the mailboxes. */
32446495Sspeer 	KMEM_FREE(nxgep->tx_mbox_areas_p->txmbox_areas_p,
32456495Sspeer 	    sizeof (p_tx_mbox_t) * tdc_max);
32466495Sspeer 	KMEM_FREE(nxgep->tx_mbox_areas_p, sizeof (tx_mbox_areas_t));
32476495Sspeer 
32486495Sspeer 	nxgep->tx_mbox_areas_p = 0;
32496495Sspeer 
32506495Sspeer 	/* 2. Free the transmit ring arrays. */
32516495Sspeer 	KMEM_FREE(nxgep->tx_rings->rings,
32526495Sspeer 	    sizeof (p_tx_ring_t) * tdc_max);
32536495Sspeer 	KMEM_FREE(nxgep->tx_rings, sizeof (tx_rings_t));
32546495Sspeer 
32556495Sspeer 	nxgep->tx_rings = 0;
32566495Sspeer 
32576495Sspeer 	/* 3. Free the completion ring data structures. */
32586495Sspeer 	KMEM_FREE(nxgep->tx_cntl_pool_p->dma_buf_pool_p,
32596495Sspeer 	    sizeof (p_nxge_dma_common_t) * tdc_max);
32606495Sspeer 	KMEM_FREE(nxgep->tx_cntl_pool_p, sizeof (nxge_dma_pool_t));
32616495Sspeer 
32626495Sspeer 	nxgep->tx_cntl_pool_p = 0;
32636495Sspeer 
32646495Sspeer 	/* 4. Free the data ring data structures. */
32656495Sspeer 	KMEM_FREE(nxgep->tx_buf_pool_p->num_chunks,
32666495Sspeer 	    sizeof (uint32_t) * tdc_max);
32676495Sspeer 	KMEM_FREE(nxgep->tx_buf_pool_p->dma_buf_pool_p,
32686495Sspeer 	    sizeof (p_nxge_dma_common_t) * tdc_max);
32696495Sspeer 	KMEM_FREE(nxgep->tx_buf_pool_p, sizeof (nxge_dma_pool_t));
32706495Sspeer 
32716495Sspeer 	nxgep->tx_buf_pool_p = 0;
32726495Sspeer 
32736495Sspeer 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "<== nxge_free_tx_mem_pool"));
32743859Sml29623 }
32753859Sml29623 
32763859Sml29623 /*ARGSUSED*/
32773859Sml29623 static nxge_status_t
32783859Sml29623 nxge_dma_mem_alloc(p_nxge_t nxgep, dma_method_t method,
32793859Sml29623 	struct ddi_dma_attr *dma_attrp,
32803859Sml29623 	size_t length, ddi_device_acc_attr_t *acc_attr_p, uint_t xfer_flags,
32813859Sml29623 	p_nxge_dma_common_t dma_p)
32823859Sml29623 {
32833859Sml29623 	caddr_t 		kaddrp;
32843859Sml29623 	int			ddi_status = DDI_SUCCESS;
32853859Sml29623 	boolean_t		contig_alloc_type;
32866495Sspeer 	boolean_t		kmem_alloc_type;
32873859Sml29623 
32883859Sml29623 	contig_alloc_type = dma_p->contig_alloc_type;
32893859Sml29623 
32903859Sml29623 	if (contig_alloc_type && (nxgep->niu_type != N2_NIU)) {
32913859Sml29623 		/*
32923859Sml29623 		 * contig_alloc_type for contiguous memory only allowed
32933859Sml29623 		 * for N2/NIU.
32943859Sml29623 		 */
32953859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
32966512Ssowmini 		    "nxge_dma_mem_alloc: alloc type not allowed (%d)",
32976512Ssowmini 		    dma_p->contig_alloc_type));
32983859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
32993859Sml29623 	}
33003859Sml29623 
33013859Sml29623 	dma_p->dma_handle = NULL;
33023859Sml29623 	dma_p->acc_handle = NULL;
33033859Sml29623 	dma_p->kaddrp = dma_p->last_kaddrp = NULL;
33043859Sml29623 	dma_p->first_ioaddr_pp = dma_p->last_ioaddr_pp = NULL;
33053859Sml29623 	ddi_status = ddi_dma_alloc_handle(nxgep->dip, dma_attrp,
33066512Ssowmini 	    DDI_DMA_DONTWAIT, NULL, &dma_p->dma_handle);
33073859Sml29623 	if (ddi_status != DDI_SUCCESS) {
33083859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33096512Ssowmini 		    "nxge_dma_mem_alloc:ddi_dma_alloc_handle failed."));
33103859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
33113859Sml29623 	}
33123859Sml29623 
33136495Sspeer 	kmem_alloc_type = dma_p->kmem_alloc_type;
33146495Sspeer 
33153859Sml29623 	switch (contig_alloc_type) {
33163859Sml29623 	case B_FALSE:
33176495Sspeer 		switch (kmem_alloc_type) {
33186495Sspeer 		case B_FALSE:
33196495Sspeer 			ddi_status = ddi_dma_mem_alloc(dma_p->dma_handle,
33206512Ssowmini 			    length,
33216512Ssowmini 			    acc_attr_p,
33226512Ssowmini 			    xfer_flags,
33236512Ssowmini 			    DDI_DMA_DONTWAIT, 0, &kaddrp, &dma_p->alength,
33246512Ssowmini 			    &dma_p->acc_handle);
33256495Sspeer 			if (ddi_status != DDI_SUCCESS) {
33266495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33276495Sspeer 				    "nxge_dma_mem_alloc: "
33286495Sspeer 				    "ddi_dma_mem_alloc failed"));
33296495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
33306495Sspeer 				dma_p->dma_handle = NULL;
33316495Sspeer 				return (NXGE_ERROR | NXGE_DDI_FAILED);
33326495Sspeer 			}
33336495Sspeer 			if (dma_p->alength < length) {
33346495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33356495Sspeer 				    "nxge_dma_mem_alloc:di_dma_mem_alloc "
33366495Sspeer 				    "< length."));
33376495Sspeer 				ddi_dma_mem_free(&dma_p->acc_handle);
33386495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
33396495Sspeer 				dma_p->acc_handle = NULL;
33406495Sspeer 				dma_p->dma_handle = NULL;
33416495Sspeer 				return (NXGE_ERROR);
33426495Sspeer 			}
33436495Sspeer 
33446495Sspeer 			ddi_status = ddi_dma_addr_bind_handle(dma_p->dma_handle,
33456495Sspeer 			    NULL,
33466495Sspeer 			    kaddrp, dma_p->alength, xfer_flags,
33476495Sspeer 			    DDI_DMA_DONTWAIT,
33486495Sspeer 			    0, &dma_p->dma_cookie, &dma_p->ncookies);
33496495Sspeer 			if (ddi_status != DDI_DMA_MAPPED) {
33506495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33516495Sspeer 				    "nxge_dma_mem_alloc: ddi_dma_addr_bind "
33526495Sspeer 				    "failed "
33536495Sspeer 				    "(staus 0x%x ncookies %d.)", ddi_status,
33546495Sspeer 				    dma_p->ncookies));
33556495Sspeer 				if (dma_p->acc_handle) {
33566495Sspeer 					ddi_dma_mem_free(&dma_p->acc_handle);
33576495Sspeer 					dma_p->acc_handle = NULL;
33586495Sspeer 				}
33596495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
33606495Sspeer 				dma_p->dma_handle = NULL;
33616495Sspeer 				return (NXGE_ERROR | NXGE_DDI_FAILED);
33626495Sspeer 			}
33636495Sspeer 
33646495Sspeer 			if (dma_p->ncookies != 1) {
33656495Sspeer 				NXGE_DEBUG_MSG((nxgep, DMA_CTL,
33666495Sspeer 				    "nxge_dma_mem_alloc:ddi_dma_addr_bind "
33676495Sspeer 				    "> 1 cookie"
33686495Sspeer 				    "(staus 0x%x ncookies %d.)", ddi_status,
33696495Sspeer 				    dma_p->ncookies));
3370*7812SMichael.Speer@Sun.COM 				(void) ddi_dma_unbind_handle(dma_p->dma_handle);
33716495Sspeer 				if (dma_p->acc_handle) {
33726495Sspeer 					ddi_dma_mem_free(&dma_p->acc_handle);
33736495Sspeer 					dma_p->acc_handle = NULL;
33746495Sspeer 				}
33756495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
33766495Sspeer 				dma_p->dma_handle = NULL;
3377*7812SMichael.Speer@Sun.COM 				dma_p->acc_handle = NULL;
33786495Sspeer 				return (NXGE_ERROR);
33796495Sspeer 			}
33806495Sspeer 			break;
33816495Sspeer 
33826495Sspeer 		case B_TRUE:
33836495Sspeer 			kaddrp = KMEM_ALLOC(length, KM_NOSLEEP);
33846495Sspeer 			if (kaddrp == NULL) {
33856495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33866495Sspeer 				    "nxge_dma_mem_alloc:ddi_dma_mem_alloc "
33876495Sspeer 				    "kmem alloc failed"));
33886495Sspeer 				return (NXGE_ERROR);
33896495Sspeer 			}
33906495Sspeer 
33916495Sspeer 			dma_p->alength = length;
33926495Sspeer 			ddi_status = ddi_dma_addr_bind_handle(dma_p->dma_handle,
33936495Sspeer 			    NULL, kaddrp, dma_p->alength, xfer_flags,
33946495Sspeer 			    DDI_DMA_DONTWAIT, 0,
33956495Sspeer 			    &dma_p->dma_cookie, &dma_p->ncookies);
33966495Sspeer 			if (ddi_status != DDI_DMA_MAPPED) {
33976495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33986495Sspeer 				    "nxge_dma_mem_alloc:ddi_dma_addr_bind: "
33996495Sspeer 				    "(kmem_alloc) failed kaddrp $%p length %d "
34006495Sspeer 				    "(staus 0x%x (%d) ncookies %d.)",
34016495Sspeer 				    kaddrp, length,
34026495Sspeer 				    ddi_status, ddi_status, dma_p->ncookies));
34036495Sspeer 				KMEM_FREE(kaddrp, length);
34046495Sspeer 				dma_p->acc_handle = NULL;
34056495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
34066495Sspeer 				dma_p->dma_handle = NULL;
34076495Sspeer 				dma_p->kaddrp = NULL;
34086495Sspeer 				return (NXGE_ERROR | NXGE_DDI_FAILED);
34096495Sspeer 			}
34106495Sspeer 
34116495Sspeer 			if (dma_p->ncookies != 1) {
34126495Sspeer 				NXGE_DEBUG_MSG((nxgep, DMA_CTL,
34136495Sspeer 				    "nxge_dma_mem_alloc:ddi_dma_addr_bind "
34146495Sspeer 				    "(kmem_alloc) > 1 cookie"
34156495Sspeer 				    "(staus 0x%x ncookies %d.)", ddi_status,
34166512Ssowmini 				    dma_p->ncookies));
3417*7812SMichael.Speer@Sun.COM 				(void) ddi_dma_unbind_handle(dma_p->dma_handle);
34186495Sspeer 				KMEM_FREE(kaddrp, length);
34196495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
34206495Sspeer 				dma_p->dma_handle = NULL;
3421*7812SMichael.Speer@Sun.COM 				dma_p->acc_handle = NULL;
34226495Sspeer 				dma_p->kaddrp = NULL;
34236495Sspeer 				return (NXGE_ERROR);
34243859Sml29623 			}
34256495Sspeer 
34266495Sspeer 			dma_p->kaddrp = kaddrp;
34276495Sspeer 
34286495Sspeer 			NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
34296512Ssowmini 			    "nxge_dma_mem_alloc: kmem_alloc dmap $%p "
34306512Ssowmini 			    "kaddr $%p alength %d",
34316512Ssowmini 			    dma_p,
34326512Ssowmini 			    kaddrp,
34336512Ssowmini 			    dma_p->alength));
34346495Sspeer 			break;
34353859Sml29623 		}
34363859Sml29623 		break;
34373859Sml29623 
34383859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
34393859Sml29623 	case B_TRUE:
34403859Sml29623 		kaddrp = (caddr_t)contig_mem_alloc(length);
34413859Sml29623 		if (kaddrp == NULL) {
34423859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
34436512Ssowmini 			    "nxge_dma_mem_alloc:contig_mem_alloc failed."));
34443859Sml29623 			ddi_dma_free_handle(&dma_p->dma_handle);
34453859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
34463859Sml29623 		}
34473859Sml29623 
34483859Sml29623 		dma_p->alength = length;
34493859Sml29623 		ddi_status = ddi_dma_addr_bind_handle(dma_p->dma_handle, NULL,
34506512Ssowmini 		    kaddrp, dma_p->alength, xfer_flags, DDI_DMA_DONTWAIT, 0,
34516512Ssowmini 		    &dma_p->dma_cookie, &dma_p->ncookies);
34523859Sml29623 		if (ddi_status != DDI_DMA_MAPPED) {
34533859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
34546512Ssowmini 			    "nxge_dma_mem_alloc:di_dma_addr_bind failed "
34556512Ssowmini 			    "(status 0x%x ncookies %d.)", ddi_status,
34566512Ssowmini 			    dma_p->ncookies));
34573859Sml29623 
34583859Sml29623 			NXGE_DEBUG_MSG((nxgep, DMA_CTL,
34596512Ssowmini 			    "==> nxge_dma_mem_alloc: (not mapped)"
34606512Ssowmini 			    "length %lu (0x%x) "
34616512Ssowmini 			    "free contig kaddrp $%p "
34626512Ssowmini 			    "va_to_pa $%p",
34636512Ssowmini 			    length, length,
34646512Ssowmini 			    kaddrp,
34656512Ssowmini 			    va_to_pa(kaddrp)));
34663859Sml29623 
34673859Sml29623 
34683859Sml29623 			contig_mem_free((void *)kaddrp, length);
34693859Sml29623 			ddi_dma_free_handle(&dma_p->dma_handle);
34703859Sml29623 
34713859Sml29623 			dma_p->dma_handle = NULL;
34723859Sml29623 			dma_p->acc_handle = NULL;
34733859Sml29623 			dma_p->alength = NULL;
34743859Sml29623 			dma_p->kaddrp = NULL;
34753859Sml29623 
34763859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
34773859Sml29623 		}
34783859Sml29623 
34793859Sml29623 		if (dma_p->ncookies != 1 ||
34806512Ssowmini 		    (dma_p->dma_cookie.dmac_laddress == NULL)) {
34813859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
34826512Ssowmini 			    "nxge_dma_mem_alloc:di_dma_addr_bind > 1 "
34836512Ssowmini 			    "cookie or "
34846512Ssowmini 			    "dmac_laddress is NULL $%p size %d "
34856512Ssowmini 			    " (status 0x%x ncookies %d.)",
34866512Ssowmini 			    ddi_status,
34876512Ssowmini 			    dma_p->dma_cookie.dmac_laddress,
34886512Ssowmini 			    dma_p->dma_cookie.dmac_size,
34896512Ssowmini 			    dma_p->ncookies));
34903859Sml29623 
34913859Sml29623 			contig_mem_free((void *)kaddrp, length);
34924185Sspeer 			(void) ddi_dma_unbind_handle(dma_p->dma_handle);
34933859Sml29623 			ddi_dma_free_handle(&dma_p->dma_handle);
34943859Sml29623 
34953859Sml29623 			dma_p->alength = 0;
34963859Sml29623 			dma_p->dma_handle = NULL;
34973859Sml29623 			dma_p->acc_handle = NULL;
34983859Sml29623 			dma_p->kaddrp = NULL;
34993859Sml29623 
35003859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
35013859Sml29623 		}
35023859Sml29623 		break;
35033859Sml29623 
35043859Sml29623 #else
35053859Sml29623 	case B_TRUE:
35063859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
35076512Ssowmini 		    "nxge_dma_mem_alloc: invalid alloc type for !sun4v"));
35083859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
35093859Sml29623 #endif
35103859Sml29623 	}
35113859Sml29623 
35123859Sml29623 	dma_p->kaddrp = kaddrp;
35133859Sml29623 	dma_p->last_kaddrp = (unsigned char *)kaddrp +
35146512Ssowmini 	    dma_p->alength - RXBUF_64B_ALIGNED;
35155125Sjoycey #if defined(__i386)
35165125Sjoycey 	dma_p->ioaddr_pp =
35176512Ssowmini 	    (unsigned char *)(uint32_t)dma_p->dma_cookie.dmac_laddress;
35185125Sjoycey #else
35193859Sml29623 	dma_p->ioaddr_pp = (unsigned char *)dma_p->dma_cookie.dmac_laddress;
35205125Sjoycey #endif
35213859Sml29623 	dma_p->last_ioaddr_pp =
35225125Sjoycey #if defined(__i386)
35236512Ssowmini 	    (unsigned char *)(uint32_t)dma_p->dma_cookie.dmac_laddress +
35245125Sjoycey #else
35256512Ssowmini 	    (unsigned char *)dma_p->dma_cookie.dmac_laddress +
35265125Sjoycey #endif
35276512Ssowmini 	    dma_p->alength - RXBUF_64B_ALIGNED;
35283859Sml29623 
35293859Sml29623 	NPI_DMA_ACC_HANDLE_SET(dma_p, dma_p->acc_handle);
35303859Sml29623 
35313859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
35323859Sml29623 	dma_p->orig_ioaddr_pp =
35336512Ssowmini 	    (unsigned char *)dma_p->dma_cookie.dmac_laddress;
35343859Sml29623 	dma_p->orig_alength = length;
35353859Sml29623 	dma_p->orig_kaddrp = kaddrp;
35363859Sml29623 	dma_p->orig_vatopa = (uint64_t)va_to_pa(kaddrp);
35373859Sml29623 #endif
35383859Sml29623 
35393859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_dma_mem_alloc: "
35406512Ssowmini 	    "dma buffer allocated: dma_p $%p "
35416512Ssowmini 	    "return dmac_ladress from cookie $%p cookie dmac_size %d "
35426512Ssowmini 	    "dma_p->ioaddr_p $%p "
35436512Ssowmini 	    "dma_p->orig_ioaddr_p $%p "
35446512Ssowmini 	    "orig_vatopa $%p "
35456512Ssowmini 	    "alength %d (0x%x) "
35466512Ssowmini 	    "kaddrp $%p "
35476512Ssowmini 	    "length %d (0x%x)",
35486512Ssowmini 	    dma_p,
35496512Ssowmini 	    dma_p->dma_cookie.dmac_laddress, dma_p->dma_cookie.dmac_size,
35506512Ssowmini 	    dma_p->ioaddr_pp,
35516512Ssowmini 	    dma_p->orig_ioaddr_pp,
35526512Ssowmini 	    dma_p->orig_vatopa,
35536512Ssowmini 	    dma_p->alength, dma_p->alength,
35546512Ssowmini 	    kaddrp,
35556512Ssowmini 	    length, length));
35563859Sml29623 
35573859Sml29623 	return (NXGE_OK);
35583859Sml29623 }
35593859Sml29623 
35603859Sml29623 static void
35613859Sml29623 nxge_dma_mem_free(p_nxge_dma_common_t dma_p)
35623859Sml29623 {
35633859Sml29623 	if (dma_p->dma_handle != NULL) {
35643859Sml29623 		if (dma_p->ncookies) {
35653859Sml29623 			(void) ddi_dma_unbind_handle(dma_p->dma_handle);
35663859Sml29623 			dma_p->ncookies = 0;
35673859Sml29623 		}
35683859Sml29623 		ddi_dma_free_handle(&dma_p->dma_handle);
35693859Sml29623 		dma_p->dma_handle = NULL;
35703859Sml29623 	}
35713859Sml29623 
35723859Sml29623 	if (dma_p->acc_handle != NULL) {
35733859Sml29623 		ddi_dma_mem_free(&dma_p->acc_handle);
35743859Sml29623 		dma_p->acc_handle = NULL;
35753859Sml29623 		NPI_DMA_ACC_HANDLE_SET(dma_p, NULL);
35763859Sml29623 	}
35773859Sml29623 
35783859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
35793859Sml29623 	if (dma_p->contig_alloc_type &&
35806512Ssowmini 	    dma_p->orig_kaddrp && dma_p->orig_alength) {
35813859Sml29623 		NXGE_DEBUG_MSG((NULL, DMA_CTL, "nxge_dma_mem_free: "
35826512Ssowmini 		    "kaddrp $%p (orig_kaddrp $%p)"
35836512Ssowmini 		    "mem type %d ",
35846512Ssowmini 		    "orig_alength %d "
35856512Ssowmini 		    "alength 0x%x (%d)",
35866512Ssowmini 		    dma_p->kaddrp,
35876512Ssowmini 		    dma_p->orig_kaddrp,
35886512Ssowmini 		    dma_p->contig_alloc_type,
35896512Ssowmini 		    dma_p->orig_alength,
35906512Ssowmini 		    dma_p->alength, dma_p->alength));
35913859Sml29623 
35923859Sml29623 		contig_mem_free(dma_p->orig_kaddrp, dma_p->orig_alength);
35933859Sml29623 		dma_p->orig_alength = NULL;
35943859Sml29623 		dma_p->orig_kaddrp = NULL;
35953859Sml29623 		dma_p->contig_alloc_type = B_FALSE;
35963859Sml29623 	}
35973859Sml29623 #endif
35983859Sml29623 	dma_p->kaddrp = NULL;
35993859Sml29623 	dma_p->alength = NULL;
36003859Sml29623 }
36013859Sml29623 
36026495Sspeer static void
36036495Sspeer nxge_dma_free_rx_data_buf(p_nxge_dma_common_t dma_p)
36046495Sspeer {
36056495Sspeer 	uint64_t kaddr;
36066495Sspeer 	uint32_t buf_size;
36076495Sspeer 
36086495Sspeer 	NXGE_DEBUG_MSG((NULL, DMA_CTL, "==> nxge_dma_free_rx_data_buf"));
36096495Sspeer 
36106495Sspeer 	if (dma_p->dma_handle != NULL) {
36116495Sspeer 		if (dma_p->ncookies) {
36126495Sspeer 			(void) ddi_dma_unbind_handle(dma_p->dma_handle);
36136495Sspeer 			dma_p->ncookies = 0;
36146495Sspeer 		}
36156495Sspeer 		ddi_dma_free_handle(&dma_p->dma_handle);
36166495Sspeer 		dma_p->dma_handle = NULL;
36176495Sspeer 	}
36186495Sspeer 
36196495Sspeer 	if (dma_p->acc_handle != NULL) {
36206495Sspeer 		ddi_dma_mem_free(&dma_p->acc_handle);
36216495Sspeer 		dma_p->acc_handle = NULL;
36226495Sspeer 		NPI_DMA_ACC_HANDLE_SET(dma_p, NULL);
36236495Sspeer 	}
36246495Sspeer 
36256495Sspeer 	NXGE_DEBUG_MSG((NULL, DMA_CTL,
36266495Sspeer 	    "==> nxge_dma_free_rx_data_buf: dmap $%p buf_alloc_state %d",
36276495Sspeer 	    dma_p,
36286495Sspeer 	    dma_p->buf_alloc_state));
36296495Sspeer 
36306495Sspeer 	if (!(dma_p->buf_alloc_state & BUF_ALLOCATED_WAIT_FREE)) {
36316495Sspeer 		NXGE_DEBUG_MSG((NULL, DMA_CTL,
36326495Sspeer 		    "<== nxge_dma_free_rx_data_buf: "
36336495Sspeer 		    "outstanding data buffers"));
36346495Sspeer 		return;
36356495Sspeer 	}
36366495Sspeer 
36376495Sspeer #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
36386495Sspeer 	if (dma_p->contig_alloc_type &&
36396512Ssowmini 	    dma_p->orig_kaddrp && dma_p->orig_alength) {
36406495Sspeer 		NXGE_DEBUG_MSG((NULL, DMA_CTL, "nxge_dma_free_rx_data_buf: "
36416495Sspeer 		    "kaddrp $%p (orig_kaddrp $%p)"
36426495Sspeer 		    "mem type %d ",
36436495Sspeer 		    "orig_alength %d "
36446495Sspeer 		    "alength 0x%x (%d)",
36456495Sspeer 		    dma_p->kaddrp,
36466495Sspeer 		    dma_p->orig_kaddrp,
36476495Sspeer 		    dma_p->contig_alloc_type,
36486495Sspeer 		    dma_p->orig_alength,
36496495Sspeer 		    dma_p->alength, dma_p->alength));
36506495Sspeer 
36516495Sspeer 		kaddr = (uint64_t)dma_p->orig_kaddrp;
36526495Sspeer 		buf_size = dma_p->orig_alength;
36536495Sspeer 		nxge_free_buf(CONTIG_MEM_ALLOC, kaddr, buf_size);
36546495Sspeer 		dma_p->orig_alength = NULL;
36556495Sspeer 		dma_p->orig_kaddrp = NULL;
36566495Sspeer 		dma_p->contig_alloc_type = B_FALSE;
36576495Sspeer 		dma_p->kaddrp = NULL;
36586495Sspeer 		dma_p->alength = NULL;
36596495Sspeer 		return;
36606495Sspeer 	}
36616495Sspeer #endif
36626495Sspeer 
36636495Sspeer 	if (dma_p->kmem_alloc_type) {
36646495Sspeer 		NXGE_DEBUG_MSG((NULL, DMA_CTL,
36656495Sspeer 		    "nxge_dma_free_rx_data_buf: free kmem "
36666512Ssowmini 		    "kaddrp $%p (orig_kaddrp $%p)"
36676512Ssowmini 		    "alloc type %d "
36686512Ssowmini 		    "orig_alength %d "
36696512Ssowmini 		    "alength 0x%x (%d)",
36706512Ssowmini 		    dma_p->kaddrp,
36716512Ssowmini 		    dma_p->orig_kaddrp,
36726512Ssowmini 		    dma_p->kmem_alloc_type,
36736512Ssowmini 		    dma_p->orig_alength,
36746512Ssowmini 		    dma_p->alength, dma_p->alength));
36756495Sspeer #if defined(__i386)
36766495Sspeer 		kaddr = (uint64_t)(uint32_t)dma_p->kaddrp;
36776495Sspeer #else
36786495Sspeer 		kaddr = (uint64_t)dma_p->kaddrp;
36796495Sspeer #endif
36806495Sspeer 		buf_size = dma_p->orig_alength;
36816495Sspeer 		NXGE_DEBUG_MSG((NULL, DMA_CTL,
36826495Sspeer 		    "nxge_dma_free_rx_data_buf: free dmap $%p "
36836495Sspeer 		    "kaddr $%p buf_size %d",
36846495Sspeer 		    dma_p,
36856495Sspeer 		    kaddr, buf_size));
36866495Sspeer 		nxge_free_buf(KMEM_ALLOC, kaddr, buf_size);
36876495Sspeer 		dma_p->alength = 0;
36886495Sspeer 		dma_p->orig_alength = 0;
36896495Sspeer 		dma_p->kaddrp = NULL;
36906495Sspeer 		dma_p->kmem_alloc_type = B_FALSE;
36916495Sspeer 	}
36926495Sspeer 
36936495Sspeer 	NXGE_DEBUG_MSG((NULL, DMA_CTL, "<== nxge_dma_free_rx_data_buf"));
36946495Sspeer }
36956495Sspeer 
36963859Sml29623 /*
36973859Sml29623  *	nxge_m_start() -- start transmitting and receiving.
36983859Sml29623  *
36993859Sml29623  *	This function is called by the MAC layer when the first
37003859Sml29623  *	stream is open to prepare the hardware ready for sending
37013859Sml29623  *	and transmitting packets.
37023859Sml29623  */
37033859Sml29623 static int
37043859Sml29623 nxge_m_start(void *arg)
37053859Sml29623 {
37063859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
37073859Sml29623 
37083859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_start"));
37093859Sml29623 
37106705Sml29623 	if (nxge_peu_reset_enable && !nxgep->nxge_link_poll_timerid) {
37116705Sml29623 		(void) nxge_link_monitor(nxgep, LINK_MONITOR_START);
37126705Sml29623 	}
37136705Sml29623 
37143859Sml29623 	MUTEX_ENTER(nxgep->genlock);
37153859Sml29623 	if (nxge_init(nxgep) != NXGE_OK) {
37163859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
37176512Ssowmini 		    "<== nxge_m_start: initialization failed"));
37183859Sml29623 		MUTEX_EXIT(nxgep->genlock);
37193859Sml29623 		return (EIO);
37203859Sml29623 	}
37213859Sml29623 
37223859Sml29623 	if (nxgep->nxge_mac_state == NXGE_MAC_STARTED)
37233859Sml29623 		goto nxge_m_start_exit;
37243859Sml29623 	/*
37253859Sml29623 	 * Start timer to check the system error and tx hangs
37263859Sml29623 	 */
37276495Sspeer 	if (!isLDOMguest(nxgep))
37286495Sspeer 		nxgep->nxge_timerid = nxge_start_timer(nxgep,
37296495Sspeer 		    nxge_check_hw_state, NXGE_CHECK_TIMER);
37306495Sspeer #if	defined(sun4v)
37316495Sspeer 	else
37326495Sspeer 		nxge_hio_start_timer(nxgep);
37336495Sspeer #endif
37343859Sml29623 
37353859Sml29623 	nxgep->link_notify = B_TRUE;
37363859Sml29623 
37373859Sml29623 	nxgep->nxge_mac_state = NXGE_MAC_STARTED;
37383859Sml29623 
37393859Sml29623 nxge_m_start_exit:
37403859Sml29623 	MUTEX_EXIT(nxgep->genlock);
37413859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_start"));
37423859Sml29623 	return (0);
37433859Sml29623 }
37443859Sml29623 
37453859Sml29623 /*
37463859Sml29623  *	nxge_m_stop(): stop transmitting and receiving.
37473859Sml29623  */
37483859Sml29623 static void
37493859Sml29623 nxge_m_stop(void *arg)
37503859Sml29623 {
37513859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
37523859Sml29623 
37533859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_stop"));
37543859Sml29623 
37557466SMisaki.Kataoka@Sun.COM 	MUTEX_ENTER(nxgep->genlock);
37567466SMisaki.Kataoka@Sun.COM 	nxgep->nxge_mac_state = NXGE_MAC_STOPPING;
37577466SMisaki.Kataoka@Sun.COM 
37583859Sml29623 	if (nxgep->nxge_timerid) {
37593859Sml29623 		nxge_stop_timer(nxgep, nxgep->nxge_timerid);
37603859Sml29623 		nxgep->nxge_timerid = 0;
37613859Sml29623 	}
37623859Sml29623 
37633859Sml29623 	nxge_uninit(nxgep);
37643859Sml29623 
37653859Sml29623 	nxgep->nxge_mac_state = NXGE_MAC_STOPPED;
37663859Sml29623 
37673859Sml29623 	MUTEX_EXIT(nxgep->genlock);
37683859Sml29623 
37693859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_stop"));
37703859Sml29623 }
37713859Sml29623 
37723859Sml29623 static int
37733859Sml29623 nxge_m_unicst(void *arg, const uint8_t *macaddr)
37743859Sml29623 {
37753859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
37763859Sml29623 	struct 		ether_addr addrp;
37773859Sml29623 
37783859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL, "==> nxge_m_unicst"));
37793859Sml29623 
37803859Sml29623 	bcopy(macaddr, (uint8_t *)&addrp, ETHERADDRL);
37813859Sml29623 	if (nxge_set_mac_addr(nxgep, &addrp)) {
37823859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
37836512Ssowmini 		    "<== nxge_m_unicst: set unitcast failed"));
37843859Sml29623 		return (EINVAL);
37853859Sml29623 	}
37863859Sml29623 
37873859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL, "<== nxge_m_unicst"));
37883859Sml29623 
37893859Sml29623 	return (0);
37903859Sml29623 }
37913859Sml29623 
37923859Sml29623 static int
37933859Sml29623 nxge_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
37943859Sml29623 {
37953859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
37963859Sml29623 	struct 		ether_addr addrp;
37973859Sml29623 
37983859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL,
37996512Ssowmini 	    "==> nxge_m_multicst: add %d", add));
38003859Sml29623 
38013859Sml29623 	bcopy(mca, (uint8_t *)&addrp, ETHERADDRL);
38023859Sml29623 	if (add) {
38033859Sml29623 		if (nxge_add_mcast_addr(nxgep, &addrp)) {
38043859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
38056512Ssowmini 			    "<== nxge_m_multicst: add multicast failed"));
38063859Sml29623 			return (EINVAL);
38073859Sml29623 		}
38083859Sml29623 	} else {
38093859Sml29623 		if (nxge_del_mcast_addr(nxgep, &addrp)) {
38103859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
38116512Ssowmini 			    "<== nxge_m_multicst: del multicast failed"));
38123859Sml29623 			return (EINVAL);
38133859Sml29623 		}
38143859Sml29623 	}
38153859Sml29623 
38163859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL, "<== nxge_m_multicst"));
38173859Sml29623 
38183859Sml29623 	return (0);
38193859Sml29623 }
38203859Sml29623 
38213859Sml29623 static int
38223859Sml29623 nxge_m_promisc(void *arg, boolean_t on)
38233859Sml29623 {
38243859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
38253859Sml29623 
38263859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL,
38276512Ssowmini 	    "==> nxge_m_promisc: on %d", on));
38283859Sml29623 
38293859Sml29623 	if (nxge_set_promisc(nxgep, on)) {
38303859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
38316512Ssowmini 		    "<== nxge_m_promisc: set promisc failed"));
38323859Sml29623 		return (EINVAL);
38333859Sml29623 	}
38343859Sml29623 
38353859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL,
38366512Ssowmini 	    "<== nxge_m_promisc: on %d", on));
38373859Sml29623 
38383859Sml29623 	return (0);
38393859Sml29623 }
38403859Sml29623 
38413859Sml29623 static void
38423859Sml29623 nxge_m_ioctl(void *arg,  queue_t *wq, mblk_t *mp)
38433859Sml29623 {
38443859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
38454185Sspeer 	struct 		iocblk *iocp;
38463859Sml29623 	boolean_t 	need_privilege;
38473859Sml29623 	int 		err;
38483859Sml29623 	int 		cmd;
38493859Sml29623 
38503859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_ioctl"));
38513859Sml29623 
38523859Sml29623 	iocp = (struct iocblk *)mp->b_rptr;
38533859Sml29623 	iocp->ioc_error = 0;
38543859Sml29623 	need_privilege = B_TRUE;
38553859Sml29623 	cmd = iocp->ioc_cmd;
38563859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_ioctl: cmd 0x%08x", cmd));
38573859Sml29623 	switch (cmd) {
38583859Sml29623 	default:
38593859Sml29623 		miocnak(wq, mp, 0, EINVAL);
38603859Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_ioctl: invalid"));
38613859Sml29623 		return;
38623859Sml29623 
38633859Sml29623 	case LB_GET_INFO_SIZE:
38643859Sml29623 	case LB_GET_INFO:
38653859Sml29623 	case LB_GET_MODE:
38663859Sml29623 		need_privilege = B_FALSE;
38673859Sml29623 		break;
38683859Sml29623 	case LB_SET_MODE:
38693859Sml29623 		break;
38703859Sml29623 
38713859Sml29623 
38723859Sml29623 	case NXGE_GET_MII:
38733859Sml29623 	case NXGE_PUT_MII:
38743859Sml29623 	case NXGE_GET64:
38753859Sml29623 	case NXGE_PUT64:
38763859Sml29623 	case NXGE_GET_TX_RING_SZ:
38773859Sml29623 	case NXGE_GET_TX_DESC:
38783859Sml29623 	case NXGE_TX_SIDE_RESET:
38793859Sml29623 	case NXGE_RX_SIDE_RESET:
38803859Sml29623 	case NXGE_GLOBAL_RESET:
38813859Sml29623 	case NXGE_RESET_MAC:
38823859Sml29623 	case NXGE_TX_REGS_DUMP:
38833859Sml29623 	case NXGE_RX_REGS_DUMP:
38843859Sml29623 	case NXGE_INT_REGS_DUMP:
38853859Sml29623 	case NXGE_VIR_INT_REGS_DUMP:
38863859Sml29623 	case NXGE_PUT_TCAM:
38873859Sml29623 	case NXGE_GET_TCAM:
38883859Sml29623 	case NXGE_RTRACE:
38893859Sml29623 	case NXGE_RDUMP:
38903859Sml29623 
38913859Sml29623 		need_privilege = B_FALSE;
38923859Sml29623 		break;
38933859Sml29623 	case NXGE_INJECT_ERR:
38943859Sml29623 		cmn_err(CE_NOTE, "!nxge_m_ioctl: Inject error\n");
38953859Sml29623 		nxge_err_inject(nxgep, wq, mp);
38963859Sml29623 		break;
38973859Sml29623 	}
38983859Sml29623 
38993859Sml29623 	if (need_privilege) {
39004185Sspeer 		err = secpolicy_net_config(iocp->ioc_cr, B_FALSE);
39013859Sml29623 		if (err != 0) {
39023859Sml29623 			miocnak(wq, mp, 0, err);
39033859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
39046512Ssowmini 			    "<== nxge_m_ioctl: no priv"));
39053859Sml29623 			return;
39063859Sml29623 		}
39073859Sml29623 	}
39083859Sml29623 
39093859Sml29623 	switch (cmd) {
39103859Sml29623 
39113859Sml29623 	case LB_GET_MODE:
39123859Sml29623 	case LB_SET_MODE:
39133859Sml29623 	case LB_GET_INFO_SIZE:
39143859Sml29623 	case LB_GET_INFO:
39153859Sml29623 		nxge_loopback_ioctl(nxgep, wq, mp, iocp);
39163859Sml29623 		break;
39173859Sml29623 
39183859Sml29623 	case NXGE_GET_MII:
39193859Sml29623 	case NXGE_PUT_MII:
39203859Sml29623 	case NXGE_PUT_TCAM:
39213859Sml29623 	case NXGE_GET_TCAM:
39223859Sml29623 	case NXGE_GET64:
39233859Sml29623 	case NXGE_PUT64:
39243859Sml29623 	case NXGE_GET_TX_RING_SZ:
39253859Sml29623 	case NXGE_GET_TX_DESC:
39263859Sml29623 	case NXGE_TX_SIDE_RESET:
39273859Sml29623 	case NXGE_RX_SIDE_RESET:
39283859Sml29623 	case NXGE_GLOBAL_RESET:
39293859Sml29623 	case NXGE_RESET_MAC:
39303859Sml29623 	case NXGE_TX_REGS_DUMP:
39313859Sml29623 	case NXGE_RX_REGS_DUMP:
39323859Sml29623 	case NXGE_INT_REGS_DUMP:
39333859Sml29623 	case NXGE_VIR_INT_REGS_DUMP:
39343859Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
39356512Ssowmini 		    "==> nxge_m_ioctl: cmd 0x%x", cmd));
39363859Sml29623 		nxge_hw_ioctl(nxgep, wq, mp, iocp);
39373859Sml29623 		break;
39383859Sml29623 	}
39393859Sml29623 
39403859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_ioctl"));
39413859Sml29623 }
39423859Sml29623 
39433859Sml29623 extern void nxge_rx_hw_blank(void *arg, time_t ticks, uint_t count);
39443859Sml29623 
39453859Sml29623 static void
39463859Sml29623 nxge_m_resources(void *arg)
39473859Sml29623 {
39483859Sml29623 	p_nxge_t		nxgep = arg;
39493859Sml29623 	mac_rx_fifo_t 		mrf;
39506495Sspeer 
39516495Sspeer 	nxge_grp_set_t		*set = &nxgep->rx_set;
39526495Sspeer 	uint8_t			rdc;
39536495Sspeer 
39546495Sspeer 	rx_rcr_ring_t		*ring;
39553859Sml29623 
39563859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "==> nxge_m_resources"));
39573859Sml29623 
39583859Sml29623 	MUTEX_ENTER(nxgep->genlock);
39593859Sml29623 
39606495Sspeer 	if (set->owned.map == 0) {
39616495Sspeer 		NXGE_ERROR_MSG((NULL, NXGE_ERR_CTL,
39626495Sspeer 		    "nxge_m_resources: no receive resources"));
39636495Sspeer 		goto nxge_m_resources_exit;
39646495Sspeer 	}
39656495Sspeer 
39663859Sml29623 	/*
39673859Sml29623 	 * CR 6492541 Check to see if the drv_state has been initialized,
39683859Sml29623 	 * if not * call nxge_init().
39693859Sml29623 	 */
39703859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
39716495Sspeer 		if (nxge_init(nxgep) != NXGE_OK)
39723859Sml29623 			goto nxge_m_resources_exit;
39733859Sml29623 	}
39743859Sml29623 
39753859Sml29623 	mrf.mrf_type = MAC_RX_FIFO;
39763859Sml29623 	mrf.mrf_blank = nxge_rx_hw_blank;
39773859Sml29623 	mrf.mrf_arg = (void *)nxgep;
39783859Sml29623 
39793859Sml29623 	mrf.mrf_normal_blank_time = 128;
39803859Sml29623 	mrf.mrf_normal_pkt_count = 8;
39813859Sml29623 
39823859Sml29623 	/*
39833859Sml29623 	 * Export our receive resources to the MAC layer.
39843859Sml29623 	 */
39856495Sspeer 	for (rdc = 0; rdc < NXGE_MAX_RDCS; rdc++) {
39866495Sspeer 		if ((1 << rdc) & set->owned.map) {
39876495Sspeer 			ring = nxgep->rx_rcr_rings->rcr_rings[rdc];
39886495Sspeer 			if (ring == 0) {
39896495Sspeer 				/*
39906495Sspeer 				 * This is a big deal only if we are
39916495Sspeer 				 * *not* in an LDOMs environment.
39926495Sspeer 				 */
39936495Sspeer 				if (nxgep->environs == SOLARIS_DOMAIN) {
39946495Sspeer 					cmn_err(CE_NOTE,
39956495Sspeer 					    "==> nxge_m_resources: "
39966495Sspeer 					    "ring %d == 0", rdc);
39976495Sspeer 				}
39986495Sspeer 				continue;
39996495Sspeer 			}
40006495Sspeer 			ring->rcr_mac_handle = mac_resource_add
40016495Sspeer 			    (nxgep->mach, (mac_resource_t *)&mrf);
40026495Sspeer 
40036495Sspeer 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
40046495Sspeer 			    "==> nxge_m_resources: RDC %d RCR %p MAC handle %p",
40056495Sspeer 			    rdc, ring, ring->rcr_mac_handle));
40066495Sspeer 		}
40073859Sml29623 	}
40083859Sml29623 
40093859Sml29623 nxge_m_resources_exit:
40103859Sml29623 	MUTEX_EXIT(nxgep->genlock);
40113859Sml29623 	NXGE_DEBUG_MSG((nxgep, RX_CTL, "<== nxge_m_resources"));
40123859Sml29623 }
40133859Sml29623 
40146495Sspeer void
40153859Sml29623 nxge_mmac_kstat_update(p_nxge_t nxgep, mac_addr_slot_t slot, boolean_t factory)
40163859Sml29623 {
40173859Sml29623 	p_nxge_mmac_stats_t mmac_stats;
40183859Sml29623 	int i;
40193859Sml29623 	nxge_mmac_t *mmac_info;
40203859Sml29623 
40213859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
40223859Sml29623 
40233859Sml29623 	mmac_stats = &nxgep->statsp->mmac_stats;
40243859Sml29623 	mmac_stats->mmac_max_cnt = mmac_info->num_mmac;
40253859Sml29623 	mmac_stats->mmac_avail_cnt = mmac_info->naddrfree;
40263859Sml29623 
40273859Sml29623 	for (i = 0; i < ETHERADDRL; i++) {
40283859Sml29623 		if (factory) {
40293859Sml29623 			mmac_stats->mmac_avail_pool[slot-1].ether_addr_octet[i]
40306512Ssowmini 			    = mmac_info->factory_mac_pool[slot][
40316512Ssowmini 			    (ETHERADDRL-1) - i];
40323859Sml29623 		} else {
40333859Sml29623 			mmac_stats->mmac_avail_pool[slot-1].ether_addr_octet[i]
40346512Ssowmini 			    = mmac_info->mac_pool[slot].addr[
40356512Ssowmini 			    (ETHERADDRL - 1) - i];
40363859Sml29623 		}
40373859Sml29623 	}
40383859Sml29623 }
40393859Sml29623 
40403859Sml29623 /*
40413859Sml29623  * nxge_altmac_set() -- Set an alternate MAC address
40423859Sml29623  */
40433859Sml29623 static int
40443859Sml29623 nxge_altmac_set(p_nxge_t nxgep, uint8_t *maddr, mac_addr_slot_t slot)
40453859Sml29623 {
40463859Sml29623 	uint8_t addrn;
40473859Sml29623 	uint8_t portn;
40483859Sml29623 	npi_mac_addr_t altmac;
40494484Sspeer 	hostinfo_t mac_rdc;
40504484Sspeer 	p_nxge_class_pt_cfg_t clscfgp;
40513859Sml29623 
40523859Sml29623 	altmac.w2 = ((uint16_t)maddr[0] << 8) | ((uint16_t)maddr[1] & 0x0ff);
40533859Sml29623 	altmac.w1 = ((uint16_t)maddr[2] << 8) | ((uint16_t)maddr[3] & 0x0ff);
40543859Sml29623 	altmac.w0 = ((uint16_t)maddr[4] << 8) | ((uint16_t)maddr[5] & 0x0ff);
40553859Sml29623 
40563859Sml29623 	portn = nxgep->mac.portnum;
40573859Sml29623 	addrn = (uint8_t)slot - 1;
40583859Sml29623 
40593859Sml29623 	if (npi_mac_altaddr_entry(nxgep->npi_handle, OP_SET, portn,
40606512Ssowmini 	    addrn, &altmac) != NPI_SUCCESS)
40613859Sml29623 		return (EIO);
40624484Sspeer 
40634484Sspeer 	/*
40644484Sspeer 	 * Set the rdc table number for the host info entry
40654484Sspeer 	 * for this mac address slot.
40664484Sspeer 	 */
40674484Sspeer 	clscfgp = (p_nxge_class_pt_cfg_t)&nxgep->class_config;
40684484Sspeer 	mac_rdc.value = 0;
40694484Sspeer 	mac_rdc.bits.w0.rdc_tbl_num = clscfgp->mac_host_info[addrn].rdctbl;
40704484Sspeer 	mac_rdc.bits.w0.mac_pref = clscfgp->mac_host_info[addrn].mpr_npr;
40714484Sspeer 
40724484Sspeer 	if (npi_mac_hostinfo_entry(nxgep->npi_handle, OP_SET,
40734484Sspeer 	    nxgep->function_num, addrn, &mac_rdc) != NPI_SUCCESS) {
40744484Sspeer 		return (EIO);
40754484Sspeer 	}
40764484Sspeer 
40773859Sml29623 	/*
40783859Sml29623 	 * Enable comparison with the alternate MAC address.
40793859Sml29623 	 * While the first alternate addr is enabled by bit 1 of register
40803859Sml29623 	 * BMAC_ALTAD_CMPEN, it is enabled by bit 0 of register
40813859Sml29623 	 * XMAC_ADDR_CMPEN, so slot needs to be converted to addrn
40823859Sml29623 	 * accordingly before calling npi_mac_altaddr_entry.
40833859Sml29623 	 */
40843859Sml29623 	if (portn == XMAC_PORT_0 || portn == XMAC_PORT_1)
40853859Sml29623 		addrn = (uint8_t)slot - 1;
40863859Sml29623 	else
40873859Sml29623 		addrn = (uint8_t)slot;
40883859Sml29623 
40893859Sml29623 	if (npi_mac_altaddr_enable(nxgep->npi_handle, portn, addrn)
40906512Ssowmini 	    != NPI_SUCCESS)
40913859Sml29623 		return (EIO);
40923859Sml29623 
40933859Sml29623 	return (0);
40943859Sml29623 }
40953859Sml29623 
40963859Sml29623 /*
40973859Sml29623  * nxeg_m_mmac_add() - find an unused address slot, set the address
40983859Sml29623  * value to the one specified, enable the port to start filtering on
40993859Sml29623  * the new MAC address.  Returns 0 on success.
41003859Sml29623  */
41016495Sspeer int
41023859Sml29623 nxge_m_mmac_add(void *arg, mac_multi_addr_t *maddr)
41033859Sml29623 {
41043859Sml29623 	p_nxge_t nxgep = arg;
41053859Sml29623 	mac_addr_slot_t slot;
41063859Sml29623 	nxge_mmac_t *mmac_info;
41073859Sml29623 	int err;
41083859Sml29623 	nxge_status_t status;
41093859Sml29623 
41103859Sml29623 	mutex_enter(nxgep->genlock);
41113859Sml29623 
41123859Sml29623 	/*
41133859Sml29623 	 * Make sure that nxge is initialized, if _start() has
41143859Sml29623 	 * not been called.
41153859Sml29623 	 */
41163859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
41173859Sml29623 		status = nxge_init(nxgep);
41183859Sml29623 		if (status != NXGE_OK) {
41193859Sml29623 			mutex_exit(nxgep->genlock);
41203859Sml29623 			return (ENXIO);
41213859Sml29623 		}
41223859Sml29623 	}
41233859Sml29623 
41243859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
41253859Sml29623 	if (mmac_info->naddrfree == 0) {
41263859Sml29623 		mutex_exit(nxgep->genlock);
41273859Sml29623 		return (ENOSPC);
41283859Sml29623 	}
41293859Sml29623 	if (!mac_unicst_verify(nxgep->mach, maddr->mma_addr,
41306512Ssowmini 	    maddr->mma_addrlen)) {
41313859Sml29623 		mutex_exit(nxgep->genlock);
41323859Sml29623 		return (EINVAL);
41333859Sml29623 	}
41343859Sml29623 	/*
41353859Sml29623 	 * 	Search for the first available slot. Because naddrfree
41363859Sml29623 	 * is not zero, we are guaranteed to find one.
41373859Sml29623 	 * 	Slot 0 is for unique (primary) MAC. The first alternate
41383859Sml29623 	 * MAC slot is slot 1.
41393859Sml29623 	 *	Each of the first two ports of Neptune has 16 alternate
41406495Sspeer 	 * MAC slots but only the first 7 (of 15) slots have assigned factory
41413859Sml29623 	 * MAC addresses. We first search among the slots without bundled
41423859Sml29623 	 * factory MACs. If we fail to find one in that range, then we
41433859Sml29623 	 * search the slots with bundled factory MACs.  A factory MAC
41443859Sml29623 	 * will be wasted while the slot is used with a user MAC address.
41453859Sml29623 	 * But the slot could be used by factory MAC again after calling
41463859Sml29623 	 * nxge_m_mmac_remove and nxge_m_mmac_reserve.
41473859Sml29623 	 */
41483859Sml29623 	if (mmac_info->num_factory_mmac < mmac_info->num_mmac) {
41493859Sml29623 		for (slot = mmac_info->num_factory_mmac + 1;
41506512Ssowmini 		    slot <= mmac_info->num_mmac; slot++) {
41513859Sml29623 			if (!(mmac_info->mac_pool[slot].flags & MMAC_SLOT_USED))
41523859Sml29623 				break;
41533859Sml29623 		}
41543859Sml29623 		if (slot > mmac_info->num_mmac) {
41553859Sml29623 			for (slot = 1; slot <= mmac_info->num_factory_mmac;
41566512Ssowmini 			    slot++) {
41573859Sml29623 				if (!(mmac_info->mac_pool[slot].flags
41586512Ssowmini 				    & MMAC_SLOT_USED))
41593859Sml29623 					break;
41603859Sml29623 			}
41613859Sml29623 		}
41623859Sml29623 	} else {
41633859Sml29623 		for (slot = 1; slot <= mmac_info->num_mmac; slot++) {
41643859Sml29623 			if (!(mmac_info->mac_pool[slot].flags & MMAC_SLOT_USED))
41653859Sml29623 				break;
41663859Sml29623 		}
41673859Sml29623 	}
41683859Sml29623 	ASSERT(slot <= mmac_info->num_mmac);
41693859Sml29623 	if ((err = nxge_altmac_set(nxgep, maddr->mma_addr, slot)) != 0) {
41703859Sml29623 		mutex_exit(nxgep->genlock);
41713859Sml29623 		return (err);
41723859Sml29623 	}
41733859Sml29623 	bcopy(maddr->mma_addr, mmac_info->mac_pool[slot].addr, ETHERADDRL);
41743859Sml29623 	mmac_info->mac_pool[slot].flags |= MMAC_SLOT_USED;
41753859Sml29623 	mmac_info->mac_pool[slot].flags &= ~MMAC_VENDOR_ADDR;
41763859Sml29623 	mmac_info->naddrfree--;
41773859Sml29623 	nxge_mmac_kstat_update(nxgep, slot, B_FALSE);
41783859Sml29623 
41793859Sml29623 	maddr->mma_slot = slot;
41803859Sml29623 
41813859Sml29623 	mutex_exit(nxgep->genlock);
41823859Sml29623 	return (0);
41833859Sml29623 }
41843859Sml29623 
41853859Sml29623 /*
41863859Sml29623  * This function reserves an unused slot and programs the slot and the HW
41873859Sml29623  * with a factory mac address.
41883859Sml29623  */
41893859Sml29623 static int
41903859Sml29623 nxge_m_mmac_reserve(void *arg, mac_multi_addr_t *maddr)
41913859Sml29623 {
41923859Sml29623 	p_nxge_t nxgep = arg;
41933859Sml29623 	mac_addr_slot_t slot;
41943859Sml29623 	nxge_mmac_t *mmac_info;
41953859Sml29623 	int err;
41963859Sml29623 	nxge_status_t status;
41973859Sml29623 
41983859Sml29623 	mutex_enter(nxgep->genlock);
41993859Sml29623 
42003859Sml29623 	/*
42013859Sml29623 	 * Make sure that nxge is initialized, if _start() has
42023859Sml29623 	 * not been called.
42033859Sml29623 	 */
42043859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
42053859Sml29623 		status = nxge_init(nxgep);
42063859Sml29623 		if (status != NXGE_OK) {
42073859Sml29623 			mutex_exit(nxgep->genlock);
42083859Sml29623 			return (ENXIO);
42093859Sml29623 		}
42103859Sml29623 	}
42113859Sml29623 
42123859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
42133859Sml29623 	if (mmac_info->naddrfree == 0) {
42143859Sml29623 		mutex_exit(nxgep->genlock);
42153859Sml29623 		return (ENOSPC);
42163859Sml29623 	}
42173859Sml29623 
42183859Sml29623 	slot = maddr->mma_slot;
42193859Sml29623 	if (slot == -1) {  /* -1: Take the first available slot */
42203859Sml29623 		for (slot = 1; slot <= mmac_info->num_factory_mmac; slot++) {
42213859Sml29623 			if (!(mmac_info->mac_pool[slot].flags & MMAC_SLOT_USED))
42223859Sml29623 				break;
42233859Sml29623 		}
42243859Sml29623 		if (slot > mmac_info->num_factory_mmac) {
42253859Sml29623 			mutex_exit(nxgep->genlock);
42263859Sml29623 			return (ENOSPC);
42273859Sml29623 		}
42283859Sml29623 	}
42293859Sml29623 	if (slot < 1 || slot > mmac_info->num_factory_mmac) {
42303859Sml29623 		/*
42313859Sml29623 		 * Do not support factory MAC at a slot greater than
42323859Sml29623 		 * num_factory_mmac even when there are available factory
42333859Sml29623 		 * MAC addresses because the alternate MACs are bundled with
42343859Sml29623 		 * slot[1] through slot[num_factory_mmac]
42353859Sml29623 		 */
42363859Sml29623 		mutex_exit(nxgep->genlock);
42373859Sml29623 		return (EINVAL);
42383859Sml29623 	}
42393859Sml29623 	if (mmac_info->mac_pool[slot].flags & MMAC_SLOT_USED) {
42403859Sml29623 		mutex_exit(nxgep->genlock);
42413859Sml29623 		return (EBUSY);
42423859Sml29623 	}
42433859Sml29623 	/* Verify the address to be reserved */
42443859Sml29623 	if (!mac_unicst_verify(nxgep->mach,
42456512Ssowmini 	    mmac_info->factory_mac_pool[slot], ETHERADDRL)) {
42463859Sml29623 		mutex_exit(nxgep->genlock);
42473859Sml29623 		return (EINVAL);
42483859Sml29623 	}
42493859Sml29623 	if (err = nxge_altmac_set(nxgep,
42506512Ssowmini 	    mmac_info->factory_mac_pool[slot], slot)) {
42513859Sml29623 		mutex_exit(nxgep->genlock);
42523859Sml29623 		return (err);
42533859Sml29623 	}
42543859Sml29623 	bcopy(mmac_info->factory_mac_pool[slot], maddr->mma_addr, ETHERADDRL);
42553859Sml29623 	mmac_info->mac_pool[slot].flags |= MMAC_SLOT_USED | MMAC_VENDOR_ADDR;
42563859Sml29623 	mmac_info->naddrfree--;
42573859Sml29623 
42583859Sml29623 	nxge_mmac_kstat_update(nxgep, slot, B_TRUE);
42593859Sml29623 	mutex_exit(nxgep->genlock);
42603859Sml29623 
42613859Sml29623 	/* Pass info back to the caller */
42623859Sml29623 	maddr->mma_slot = slot;
42633859Sml29623 	maddr->mma_addrlen = ETHERADDRL;
42643859Sml29623 	maddr->mma_flags = MMAC_SLOT_USED | MMAC_VENDOR_ADDR;
42653859Sml29623 
42663859Sml29623 	return (0);
42673859Sml29623 }
42683859Sml29623 
42693859Sml29623 /*
42703859Sml29623  * Remove the specified mac address and update the HW not to filter
42713859Sml29623  * the mac address anymore.
42723859Sml29623  */
42736495Sspeer int
42743859Sml29623 nxge_m_mmac_remove(void *arg, mac_addr_slot_t slot)
42753859Sml29623 {
42763859Sml29623 	p_nxge_t nxgep = arg;
42773859Sml29623 	nxge_mmac_t *mmac_info;
42783859Sml29623 	uint8_t addrn;
42793859Sml29623 	uint8_t portn;
42803859Sml29623 	int err = 0;
42813859Sml29623 	nxge_status_t status;
42823859Sml29623 
42833859Sml29623 	mutex_enter(nxgep->genlock);
42843859Sml29623 
42853859Sml29623 	/*
42863859Sml29623 	 * Make sure that nxge is initialized, if _start() has
42873859Sml29623 	 * not been called.
42883859Sml29623 	 */
42893859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
42903859Sml29623 		status = nxge_init(nxgep);
42913859Sml29623 		if (status != NXGE_OK) {
42923859Sml29623 			mutex_exit(nxgep->genlock);
42933859Sml29623 			return (ENXIO);
42943859Sml29623 		}
42953859Sml29623 	}
42963859Sml29623 
42973859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
42983859Sml29623 	if (slot < 1 || slot > mmac_info->num_mmac) {
42993859Sml29623 		mutex_exit(nxgep->genlock);
43003859Sml29623 		return (EINVAL);
43013859Sml29623 	}
43023859Sml29623 
43033859Sml29623 	portn = nxgep->mac.portnum;
43043859Sml29623 	if (portn == XMAC_PORT_0 || portn == XMAC_PORT_1)
43053859Sml29623 		addrn = (uint8_t)slot - 1;
43063859Sml29623 	else
43073859Sml29623 		addrn = (uint8_t)slot;
43083859Sml29623 
43093859Sml29623 	if (mmac_info->mac_pool[slot].flags & MMAC_SLOT_USED) {
43103859Sml29623 		if (npi_mac_altaddr_disable(nxgep->npi_handle, portn, addrn)
43116512Ssowmini 		    == NPI_SUCCESS) {
43123859Sml29623 			mmac_info->naddrfree++;
43133859Sml29623 			mmac_info->mac_pool[slot].flags &= ~MMAC_SLOT_USED;
43143859Sml29623 			/*
43153859Sml29623 			 * Regardless if the MAC we just stopped filtering
43163859Sml29623 			 * is a user addr or a facory addr, we must set
43173859Sml29623 			 * the MMAC_VENDOR_ADDR flag if this slot has an
43183859Sml29623 			 * associated factory MAC to indicate that a factory
43193859Sml29623 			 * MAC is available.
43203859Sml29623 			 */
43213859Sml29623 			if (slot <= mmac_info->num_factory_mmac) {
43223859Sml29623 				mmac_info->mac_pool[slot].flags
43236512Ssowmini 				    |= MMAC_VENDOR_ADDR;
43243859Sml29623 			}
43253859Sml29623 			/*
43263859Sml29623 			 * Clear mac_pool[slot].addr so that kstat shows 0
43273859Sml29623 			 * alternate MAC address if the slot is not used.
43283859Sml29623 			 * (But nxge_m_mmac_get returns the factory MAC even
43293859Sml29623 			 * when the slot is not used!)
43303859Sml29623 			 */
43313859Sml29623 			bzero(mmac_info->mac_pool[slot].addr, ETHERADDRL);
43323859Sml29623 			nxge_mmac_kstat_update(nxgep, slot, B_FALSE);
43333859Sml29623 		} else {
43343859Sml29623 			err = EIO;
43353859Sml29623 		}
43363859Sml29623 	} else {
43373859Sml29623 		err = EINVAL;
43383859Sml29623 	}
43393859Sml29623 
43403859Sml29623 	mutex_exit(nxgep->genlock);
43413859Sml29623 	return (err);
43423859Sml29623 }
43433859Sml29623 
43443859Sml29623 /*
43453859Sml29623  * Modify a mac address added by nxge_m_mmac_add or nxge_m_mmac_reserve().
43463859Sml29623  */
43473859Sml29623 static int
43483859Sml29623 nxge_m_mmac_modify(void *arg, mac_multi_addr_t *maddr)
43493859Sml29623 {
43503859Sml29623 	p_nxge_t nxgep = arg;
43513859Sml29623 	mac_addr_slot_t slot;
43523859Sml29623 	nxge_mmac_t *mmac_info;
43533859Sml29623 	int err = 0;
43543859Sml29623 	nxge_status_t status;
43553859Sml29623 
43563859Sml29623 	if (!mac_unicst_verify(nxgep->mach, maddr->mma_addr,
43576512Ssowmini 	    maddr->mma_addrlen))
43583859Sml29623 		return (EINVAL);
43593859Sml29623 
43603859Sml29623 	slot = maddr->mma_slot;
43613859Sml29623 
43623859Sml29623 	mutex_enter(nxgep->genlock);
43633859Sml29623 
43643859Sml29623 	/*
43653859Sml29623 	 * Make sure that nxge is initialized, if _start() has
43663859Sml29623 	 * not been called.
43673859Sml29623 	 */
43683859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
43693859Sml29623 		status = nxge_init(nxgep);
43703859Sml29623 		if (status != NXGE_OK) {
43713859Sml29623 			mutex_exit(nxgep->genlock);
43723859Sml29623 			return (ENXIO);
43733859Sml29623 		}
43743859Sml29623 	}
43753859Sml29623 
43763859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
43773859Sml29623 	if (slot < 1 || slot > mmac_info->num_mmac) {
43783859Sml29623 		mutex_exit(nxgep->genlock);
43793859Sml29623 		return (EINVAL);
43803859Sml29623 	}
43813859Sml29623 	if (mmac_info->mac_pool[slot].flags & MMAC_SLOT_USED) {
43823859Sml29623 		if ((err = nxge_altmac_set(nxgep, maddr->mma_addr, slot))
43836512Ssowmini 		    != 0) {
43843859Sml29623 			bcopy(maddr->mma_addr, mmac_info->mac_pool[slot].addr,
43856512Ssowmini 			    ETHERADDRL);
43863859Sml29623 			/*
43873859Sml29623 			 * Assume that the MAC passed down from the caller
43883859Sml29623 			 * is not a factory MAC address (The user should
43893859Sml29623 			 * call mmac_remove followed by mmac_reserve if
43903859Sml29623 			 * he wants to use the factory MAC for this slot).
43913859Sml29623 			 */
43923859Sml29623 			mmac_info->mac_pool[slot].flags &= ~MMAC_VENDOR_ADDR;
43933859Sml29623 			nxge_mmac_kstat_update(nxgep, slot, B_FALSE);
43943859Sml29623 		}
43953859Sml29623 	} else {
43963859Sml29623 		err = EINVAL;
43973859Sml29623 	}
43983859Sml29623 	mutex_exit(nxgep->genlock);
43993859Sml29623 	return (err);
44003859Sml29623 }
44013859Sml29623 
44023859Sml29623 /*
44033859Sml29623  * nxge_m_mmac_get() - Get the MAC address and other information
44043859Sml29623  * related to the slot.  mma_flags should be set to 0 in the call.
44053859Sml29623  * Note: although kstat shows MAC address as zero when a slot is
44063859Sml29623  * not used, Crossbow expects nxge_m_mmac_get to copy factory MAC
44073859Sml29623  * to the caller as long as the slot is not using a user MAC address.
44083859Sml29623  * The following table shows the rules,
44093859Sml29623  *
44103859Sml29623  *				   USED    VENDOR    mma_addr
44113859Sml29623  * ------------------------------------------------------------
44123859Sml29623  * (1) Slot uses a user MAC:        yes      no     user MAC
44133859Sml29623  * (2) Slot uses a factory MAC:     yes      yes    factory MAC
44143859Sml29623  * (3) Slot is not used but is
44153859Sml29623  *     factory MAC capable:         no       yes    factory MAC
44163859Sml29623  * (4) Slot is not used and is
44173859Sml29623  *     not factory MAC capable:     no       no        0
44183859Sml29623  * ------------------------------------------------------------
44193859Sml29623  */
44203859Sml29623 static int
44213859Sml29623 nxge_m_mmac_get(void *arg, mac_multi_addr_t *maddr)
44223859Sml29623 {
44233859Sml29623 	nxge_t *nxgep = arg;
44243859Sml29623 	mac_addr_slot_t slot;
44253859Sml29623 	nxge_mmac_t *mmac_info;
44263859Sml29623 	nxge_status_t status;
44273859Sml29623 
44283859Sml29623 	slot = maddr->mma_slot;
44293859Sml29623 
44303859Sml29623 	mutex_enter(nxgep->genlock);
44313859Sml29623 
44323859Sml29623 	/*
44333859Sml29623 	 * Make sure that nxge is initialized, if _start() has
44343859Sml29623 	 * not been called.
44353859Sml29623 	 */
44363859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
44373859Sml29623 		status = nxge_init(nxgep);
44383859Sml29623 		if (status != NXGE_OK) {
44393859Sml29623 			mutex_exit(nxgep->genlock);
44403859Sml29623 			return (ENXIO);
44413859Sml29623 		}
44423859Sml29623 	}
44433859Sml29623 
44443859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
44453859Sml29623 
44463859Sml29623 	if (slot < 1 || slot > mmac_info->num_mmac) {
44473859Sml29623 		mutex_exit(nxgep->genlock);
44483859Sml29623 		return (EINVAL);
44493859Sml29623 	}
44503859Sml29623 	maddr->mma_flags = 0;
44513859Sml29623 	if (mmac_info->mac_pool[slot].flags & MMAC_SLOT_USED)
44523859Sml29623 		maddr->mma_flags |= MMAC_SLOT_USED;
44533859Sml29623 
44543859Sml29623 	if (mmac_info->mac_pool[slot].flags & MMAC_VENDOR_ADDR) {
44553859Sml29623 		maddr->mma_flags |= MMAC_VENDOR_ADDR;
44563859Sml29623 		bcopy(mmac_info->factory_mac_pool[slot],
44576512Ssowmini 		    maddr->mma_addr, ETHERADDRL);
44583859Sml29623 		maddr->mma_addrlen = ETHERADDRL;
44593859Sml29623 	} else {
44603859Sml29623 		if (maddr->mma_flags & MMAC_SLOT_USED) {
44613859Sml29623 			bcopy(mmac_info->mac_pool[slot].addr,
44626512Ssowmini 			    maddr->mma_addr, ETHERADDRL);
44633859Sml29623 			maddr->mma_addrlen = ETHERADDRL;
44643859Sml29623 		} else {
44653859Sml29623 			bzero(maddr->mma_addr, ETHERADDRL);
44663859Sml29623 			maddr->mma_addrlen = 0;
44673859Sml29623 		}
44683859Sml29623 	}
44693859Sml29623 	mutex_exit(nxgep->genlock);
44703859Sml29623 	return (0);
44713859Sml29623 }
44723859Sml29623 
44733859Sml29623 static boolean_t
44743859Sml29623 nxge_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
44753859Sml29623 {
44763859Sml29623 	nxge_t *nxgep = arg;
44773859Sml29623 	uint32_t *txflags = cap_data;
44783859Sml29623 	multiaddress_capab_t *mmacp = cap_data;
44793859Sml29623 
44803859Sml29623 	switch (cap) {
44813859Sml29623 	case MAC_CAPAB_HCKSUM:
44826495Sspeer 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
44836611Sml29623 		    "==> nxge_m_getcapab: checksum %d", nxge_cksum_offload));
44846611Sml29623 		if (nxge_cksum_offload <= 1) {
44856495Sspeer 			*txflags = HCKSUM_INET_PARTIAL;
44866495Sspeer 		}
44873859Sml29623 		break;
44886495Sspeer 
44893859Sml29623 	case MAC_CAPAB_POLL:
44903859Sml29623 		/*
44913859Sml29623 		 * There's nothing for us to fill in, simply returning
44923859Sml29623 		 * B_TRUE stating that we support polling is sufficient.
44933859Sml29623 		 */
44943859Sml29623 		break;
44953859Sml29623 
44963859Sml29623 	case MAC_CAPAB_MULTIADDRESS:
44976495Sspeer 		mmacp = (multiaddress_capab_t *)cap_data;
44983859Sml29623 		mutex_enter(nxgep->genlock);
44993859Sml29623 
45003859Sml29623 		mmacp->maddr_naddr = nxgep->nxge_mmac_info.num_mmac;
45013859Sml29623 		mmacp->maddr_naddrfree = nxgep->nxge_mmac_info.naddrfree;
45026611Sml29623 		mmacp->maddr_flag = 0; /* 0 is required by PSARC2006/265 */
45033859Sml29623 		/*
45043859Sml29623 		 * maddr_handle is driver's private data, passed back to
45053859Sml29623 		 * entry point functions as arg.
45063859Sml29623 		 */
45073859Sml29623 		mmacp->maddr_handle	= nxgep;
45083859Sml29623 		mmacp->maddr_add	= nxge_m_mmac_add;
45093859Sml29623 		mmacp->maddr_remove	= nxge_m_mmac_remove;
45103859Sml29623 		mmacp->maddr_modify	= nxge_m_mmac_modify;
45113859Sml29623 		mmacp->maddr_get	= nxge_m_mmac_get;
45123859Sml29623 		mmacp->maddr_reserve	= nxge_m_mmac_reserve;
45133859Sml29623 
45143859Sml29623 		mutex_exit(nxgep->genlock);
45153859Sml29623 		break;
45166495Sspeer 
45175770Sml29623 	case MAC_CAPAB_LSO: {
45185770Sml29623 		mac_capab_lso_t *cap_lso = cap_data;
45195770Sml29623 
45206003Sml29623 		if (nxgep->soft_lso_enable) {
45216611Sml29623 			if (nxge_cksum_offload <= 1) {
45226611Sml29623 				cap_lso->lso_flags = LSO_TX_BASIC_TCP_IPV4;
45236611Sml29623 				if (nxge_lso_max > NXGE_LSO_MAXLEN) {
45246611Sml29623 					nxge_lso_max = NXGE_LSO_MAXLEN;
45256611Sml29623 				}
45266611Sml29623 				cap_lso->lso_basic_tcp_ipv4.lso_max =
45276611Sml29623 				    nxge_lso_max;
45285770Sml29623 			}
45295770Sml29623 			break;
45305770Sml29623 		} else {
45315770Sml29623 			return (B_FALSE);
45325770Sml29623 		}
45335770Sml29623 	}
45345770Sml29623 
45356495Sspeer #if defined(sun4v)
45366495Sspeer 	case MAC_CAPAB_RINGS: {
45376495Sspeer 		mac_capab_rings_t *mrings = (mac_capab_rings_t *)cap_data;
45386495Sspeer 
45396495Sspeer 		/*
45406495Sspeer 		 * Only the service domain driver responds to
45416495Sspeer 		 * this capability request.
45426495Sspeer 		 */
45436495Sspeer 		if (isLDOMservice(nxgep)) {
45446495Sspeer 			mrings->mr_handle = (void *)nxgep;
45456495Sspeer 
45466495Sspeer 			/*
45476495Sspeer 			 * No dynamic allocation of groups and
45486495Sspeer 			 * rings at this time.  Shares dictate the
45496705Sml29623 			 * configuration.
45506495Sspeer 			 */
45516495Sspeer 			mrings->mr_gadd_ring = NULL;
45526495Sspeer 			mrings->mr_grem_ring = NULL;
45536495Sspeer 			mrings->mr_rget = NULL;
45546495Sspeer 			mrings->mr_gget = nxge_hio_group_get;
45556495Sspeer 
45566495Sspeer 			if (mrings->mr_type == MAC_RING_TYPE_RX) {
45576495Sspeer 				mrings->mr_rnum = 8; /* XXX */
45586495Sspeer 				mrings->mr_gnum = 6; /* XXX */
45596495Sspeer 			} else {
45606495Sspeer 				mrings->mr_rnum = 8; /* XXX */
45616495Sspeer 				mrings->mr_gnum = 0; /* XXX */
45626495Sspeer 			}
45636495Sspeer 		} else
45646495Sspeer 			return (B_FALSE);
45656495Sspeer 		break;
45666495Sspeer 	}
45676495Sspeer 
45686495Sspeer 	case MAC_CAPAB_SHARES: {
45696495Sspeer 		mac_capab_share_t *mshares = (mac_capab_share_t *)cap_data;
45706495Sspeer 
45716495Sspeer 		/*
45726495Sspeer 		 * Only the service domain driver responds to
45736495Sspeer 		 * this capability request.
45746495Sspeer 		 */
45756495Sspeer 		if (isLDOMservice(nxgep)) {
45766495Sspeer 			mshares->ms_snum = 3;
45776495Sspeer 			mshares->ms_handle = (void *)nxgep;
45786495Sspeer 			mshares->ms_salloc = nxge_hio_share_alloc;
45796495Sspeer 			mshares->ms_sfree = nxge_hio_share_free;
45806495Sspeer 			mshares->ms_sadd = NULL;
45816495Sspeer 			mshares->ms_sremove = NULL;
45826495Sspeer 			mshares->ms_squery = nxge_hio_share_query;
45836495Sspeer 		} else
45846495Sspeer 			return (B_FALSE);
45856495Sspeer 		break;
45866495Sspeer 	}
45876495Sspeer #endif
45883859Sml29623 	default:
45893859Sml29623 		return (B_FALSE);
45903859Sml29623 	}
45913859Sml29623 	return (B_TRUE);
45923859Sml29623 }
45933859Sml29623 
45946439Sml29623 static boolean_t
45956439Sml29623 nxge_param_locked(mac_prop_id_t pr_num)
45966439Sml29623 {
45976439Sml29623 	/*
45986439Sml29623 	 * All adv_* parameters are locked (read-only) while
45996439Sml29623 	 * the device is in any sort of loopback mode ...
46006439Sml29623 	 */
46016439Sml29623 	switch (pr_num) {
46026789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
46036789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
46046789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
46056789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
46066789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
46076789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
46086789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
46096789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
46106789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
46116789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
46126789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
46136789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
46146789Sam223141 		case MAC_PROP_AUTONEG:
46156789Sam223141 		case MAC_PROP_FLOWCTRL:
46166439Sml29623 			return (B_TRUE);
46176439Sml29623 	}
46186439Sml29623 	return (B_FALSE);
46196439Sml29623 }
46206439Sml29623 
46216439Sml29623 /*
46226439Sml29623  * callback functions for set/get of properties
46236439Sml29623  */
46246439Sml29623 static int
46256439Sml29623 nxge_m_setprop(void *barg, const char *pr_name, mac_prop_id_t pr_num,
46266439Sml29623     uint_t pr_valsize, const void *pr_val)
46276439Sml29623 {
46286439Sml29623 	nxge_t		*nxgep = barg;
46296439Sml29623 	p_nxge_param_t	param_arr;
46306439Sml29623 	p_nxge_stats_t	statsp;
46316439Sml29623 	int		err = 0;
46326439Sml29623 	uint8_t		val;
46336439Sml29623 	uint32_t	cur_mtu, new_mtu, old_framesize;
46346439Sml29623 	link_flowctrl_t	fl;
46356439Sml29623 
46366439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_setprop"));
46376439Sml29623 	param_arr = nxgep->param_arr;
46386439Sml29623 	statsp = nxgep->statsp;
46396439Sml29623 	mutex_enter(nxgep->genlock);
46406439Sml29623 	if (statsp->port_stats.lb_mode != nxge_lb_normal &&
46416439Sml29623 	    nxge_param_locked(pr_num)) {
46426439Sml29623 		/*
46436439Sml29623 		 * All adv_* parameters are locked (read-only)
46446439Sml29623 		 * while the device is in any sort of loopback mode.
46456439Sml29623 		 */
46466439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
46476439Sml29623 		    "==> nxge_m_setprop: loopback mode: read only"));
46486439Sml29623 		mutex_exit(nxgep->genlock);
46496439Sml29623 		return (EBUSY);
46506439Sml29623 	}
46516439Sml29623 
46526439Sml29623 	val = *(uint8_t *)pr_val;
46536439Sml29623 	switch (pr_num) {
46546789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
46556439Sml29623 			nxgep->param_en_1000fdx = val;
46566439Sml29623 			param_arr[param_anar_1000fdx].value = val;
46576439Sml29623 
46586439Sml29623 			goto reprogram;
46596439Sml29623 
46606789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
46616439Sml29623 			nxgep->param_en_100fdx = val;
46626439Sml29623 			param_arr[param_anar_100fdx].value = val;
46636439Sml29623 
46646439Sml29623 			goto reprogram;
46656439Sml29623 
46666789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
46676439Sml29623 			nxgep->param_en_10fdx = val;
46686439Sml29623 			param_arr[param_anar_10fdx].value = val;
46696439Sml29623 
46706439Sml29623 			goto reprogram;
46716439Sml29623 
46726789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
46736789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
46746789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
46756789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
46766789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
46776789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
46786789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
46796789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
46806789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
46816789Sam223141 		case MAC_PROP_STATUS:
46826789Sam223141 		case MAC_PROP_SPEED:
46836789Sam223141 		case MAC_PROP_DUPLEX:
46846439Sml29623 			err = EINVAL; /* cannot set read-only properties */
46856439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
46866439Sml29623 			    "==> nxge_m_setprop:  read only property %d",
46876439Sml29623 			    pr_num));
46886439Sml29623 			break;
46896439Sml29623 
46906789Sam223141 		case MAC_PROP_AUTONEG:
46916439Sml29623 			param_arr[param_autoneg].value = val;
46926439Sml29623 
46936439Sml29623 			goto reprogram;
46946439Sml29623 
46956789Sam223141 		case MAC_PROP_MTU:
46966439Sml29623 			if (nxgep->nxge_mac_state == NXGE_MAC_STARTED) {
46976439Sml29623 				err = EBUSY;
46986439Sml29623 				break;
46996439Sml29623 			}
47006439Sml29623 
47016439Sml29623 			cur_mtu = nxgep->mac.default_mtu;
47026439Sml29623 			bcopy(pr_val, &new_mtu, sizeof (new_mtu));
47036439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47046439Sml29623 			    "==> nxge_m_setprop: set MTU: %d is_jumbo %d",
47056439Sml29623 			    new_mtu, nxgep->mac.is_jumbo));
47066439Sml29623 
47076439Sml29623 			if (new_mtu == cur_mtu) {
47086439Sml29623 				err = 0;
47096439Sml29623 				break;
47106439Sml29623 			}
47116439Sml29623 			if (new_mtu < NXGE_DEFAULT_MTU ||
47126439Sml29623 			    new_mtu > NXGE_MAXIMUM_MTU) {
47136439Sml29623 				err = EINVAL;
47146439Sml29623 				break;
47156439Sml29623 			}
47166439Sml29623 
47176439Sml29623 			if ((new_mtu > NXGE_DEFAULT_MTU) &&
47186439Sml29623 			    !nxgep->mac.is_jumbo) {
47196439Sml29623 				err = EINVAL;
47206439Sml29623 				break;
47216439Sml29623 			}
47226439Sml29623 
47236439Sml29623 			old_framesize = (uint32_t)nxgep->mac.maxframesize;
47246439Sml29623 			nxgep->mac.maxframesize = (uint16_t)
47256439Sml29623 			    (new_mtu + NXGE_EHEADER_VLAN_CRC);
47266439Sml29623 			if (nxge_mac_set_framesize(nxgep)) {
47276444Sml29623 				nxgep->mac.maxframesize =
47286444Sml29623 				    (uint16_t)old_framesize;
47296439Sml29623 				err = EINVAL;
47306439Sml29623 				break;
47316439Sml29623 			}
47326439Sml29623 
47336439Sml29623 			err = mac_maxsdu_update(nxgep->mach, new_mtu);
47346439Sml29623 			if (err) {
47356444Sml29623 				nxgep->mac.maxframesize =
47366444Sml29623 				    (uint16_t)old_framesize;
47376439Sml29623 				err = EINVAL;
47386439Sml29623 				break;
47396439Sml29623 			}
47406439Sml29623 
47416439Sml29623 			nxgep->mac.default_mtu = new_mtu;
47426439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47436439Sml29623 			    "==> nxge_m_setprop: set MTU: %d maxframe %d",
47446439Sml29623 			    new_mtu, nxgep->mac.maxframesize));
47456439Sml29623 			break;
47466439Sml29623 
47476789Sam223141 		case MAC_PROP_FLOWCTRL:
47486439Sml29623 			bcopy(pr_val, &fl, sizeof (fl));
47496439Sml29623 			switch (fl) {
47506439Sml29623 			default:
47516439Sml29623 				err = EINVAL;
47526439Sml29623 				break;
47536439Sml29623 
47546439Sml29623 			case LINK_FLOWCTRL_NONE:
47556439Sml29623 				param_arr[param_anar_pause].value = 0;
47566439Sml29623 				break;
47576439Sml29623 
47586439Sml29623 			case LINK_FLOWCTRL_RX:
47596439Sml29623 				param_arr[param_anar_pause].value = 1;
47606439Sml29623 				break;
47616439Sml29623 
47626439Sml29623 			case LINK_FLOWCTRL_TX:
47636439Sml29623 			case LINK_FLOWCTRL_BI:
47646439Sml29623 				err = EINVAL;
47656439Sml29623 				break;
47666439Sml29623 			}
47676439Sml29623 
47686439Sml29623 reprogram:
47696439Sml29623 			if (err == 0) {
47706439Sml29623 				if (!nxge_param_link_update(nxgep)) {
47716439Sml29623 					err = EINVAL;
47726439Sml29623 				}
47736439Sml29623 			}
47746439Sml29623 			break;
47756789Sam223141 		case MAC_PROP_PRIVATE:
47766439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47776439Sml29623 			    "==> nxge_m_setprop: private property"));
47786439Sml29623 			err = nxge_set_priv_prop(nxgep, pr_name, pr_valsize,
47796439Sml29623 			    pr_val);
47806439Sml29623 			break;
47816512Ssowmini 
47826512Ssowmini 		default:
47836512Ssowmini 			err = ENOTSUP;
47846512Ssowmini 			break;
47856439Sml29623 	}
47866439Sml29623 
47876439Sml29623 	mutex_exit(nxgep->genlock);
47886439Sml29623 
47896439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47906439Sml29623 	    "<== nxge_m_setprop (return %d)", err));
47916439Sml29623 	return (err);
47926439Sml29623 }
47936439Sml29623 
47946439Sml29623 static int
47956439Sml29623 nxge_m_getprop(void *barg, const char *pr_name, mac_prop_id_t pr_num,
47966512Ssowmini     uint_t pr_flags, uint_t pr_valsize, void *pr_val)
47976439Sml29623 {
47986439Sml29623 	nxge_t 		*nxgep = barg;
47996439Sml29623 	p_nxge_param_t	param_arr = nxgep->param_arr;
48006439Sml29623 	p_nxge_stats_t	statsp = nxgep->statsp;
48016439Sml29623 	int		err = 0;
48026439Sml29623 	link_flowctrl_t	fl;
48036439Sml29623 	uint64_t	tmp = 0;
48046512Ssowmini 	link_state_t	ls;
48056789Sam223141 	boolean_t	is_default = (pr_flags & MAC_PROP_DEFAULT);
48066439Sml29623 
48076439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48086439Sml29623 	    "==> nxge_m_getprop: pr_num %d", pr_num));
48096512Ssowmini 
48106512Ssowmini 	if (pr_valsize == 0)
48116512Ssowmini 		return (EINVAL);
48126512Ssowmini 
48136789Sam223141 	if ((is_default) && (pr_num != MAC_PROP_PRIVATE)) {
48146512Ssowmini 		err = nxge_get_def_val(nxgep, pr_num, pr_valsize, pr_val);
48156512Ssowmini 		return (err);
48166512Ssowmini 	}
48176512Ssowmini 
48186439Sml29623 	bzero(pr_val, pr_valsize);
48196439Sml29623 	switch (pr_num) {
48206789Sam223141 		case MAC_PROP_DUPLEX:
48216439Sml29623 			*(uint8_t *)pr_val = statsp->mac_stats.link_duplex;
48226439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48236439Sml29623 			    "==> nxge_m_getprop: duplex mode %d",
48246439Sml29623 			    *(uint8_t *)pr_val));
48256439Sml29623 			break;
48266439Sml29623 
48276789Sam223141 		case MAC_PROP_SPEED:
48286439Sml29623 			if (pr_valsize < sizeof (uint64_t))
48296439Sml29623 				return (EINVAL);
48306439Sml29623 			tmp = statsp->mac_stats.link_speed * 1000000ull;
48316439Sml29623 			bcopy(&tmp, pr_val, sizeof (tmp));
48326439Sml29623 			break;
48336439Sml29623 
48346789Sam223141 		case MAC_PROP_STATUS:
48356512Ssowmini 			if (pr_valsize < sizeof (link_state_t))
48366439Sml29623 				return (EINVAL);
48376512Ssowmini 			if (!statsp->mac_stats.link_up)
48386512Ssowmini 				ls = LINK_STATE_DOWN;
48396512Ssowmini 			else
48406512Ssowmini 				ls = LINK_STATE_UP;
48416512Ssowmini 			bcopy(&ls, pr_val, sizeof (ls));
48426439Sml29623 			break;
48436439Sml29623 
48446789Sam223141 		case MAC_PROP_AUTONEG:
48456439Sml29623 			*(uint8_t *)pr_val =
48466439Sml29623 			    param_arr[param_autoneg].value;
48476439Sml29623 			break;
48486439Sml29623 
48496789Sam223141 		case MAC_PROP_FLOWCTRL:
48506439Sml29623 			if (pr_valsize < sizeof (link_flowctrl_t))
48516439Sml29623 				return (EINVAL);
48526439Sml29623 
48536439Sml29623 			fl = LINK_FLOWCTRL_NONE;
48546439Sml29623 			if (param_arr[param_anar_pause].value) {
48556439Sml29623 				fl = LINK_FLOWCTRL_RX;
48566439Sml29623 			}
48576439Sml29623 			bcopy(&fl, pr_val, sizeof (fl));
48586439Sml29623 			break;
48596439Sml29623 
48606789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
48616439Sml29623 			*(uint8_t *)pr_val =
48626439Sml29623 			    param_arr[param_anar_1000fdx].value;
48636439Sml29623 			break;
48646439Sml29623 
48656789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
48666439Sml29623 			*(uint8_t *)pr_val = nxgep->param_en_1000fdx;
48676439Sml29623 			break;
48686439Sml29623 
48696789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
48706439Sml29623 			*(uint8_t *)pr_val =
48716439Sml29623 			    param_arr[param_anar_100fdx].value;
48726439Sml29623 			break;
48736439Sml29623 
48746789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
48756439Sml29623 			*(uint8_t *)pr_val = nxgep->param_en_100fdx;
48766439Sml29623 			break;
48776439Sml29623 
48786789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
48796439Sml29623 			*(uint8_t *)pr_val =
48806439Sml29623 			    param_arr[param_anar_10fdx].value;
48816439Sml29623 			break;
48826439Sml29623 
48836789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
48846439Sml29623 			*(uint8_t *)pr_val = nxgep->param_en_10fdx;
48856439Sml29623 			break;
48866439Sml29623 
48876789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
48886789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
48896789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
48906789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
48916789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
48926789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
48936512Ssowmini 			err = ENOTSUP;
48946512Ssowmini 			break;
48956512Ssowmini 
48966789Sam223141 		case MAC_PROP_PRIVATE:
48976512Ssowmini 			err = nxge_get_priv_prop(nxgep, pr_name, pr_flags,
48986512Ssowmini 			    pr_valsize, pr_val);
48996512Ssowmini 			break;
49006512Ssowmini 		default:
49016439Sml29623 			err = EINVAL;
49026439Sml29623 			break;
49036439Sml29623 	}
49046439Sml29623 
49056439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_getprop"));
49066439Sml29623 
49076439Sml29623 	return (err);
49086439Sml29623 }
49096439Sml29623 
49106439Sml29623 /* ARGSUSED */
49116439Sml29623 static int
49126439Sml29623 nxge_set_priv_prop(p_nxge_t nxgep, const char *pr_name, uint_t pr_valsize,
49136439Sml29623     const void *pr_val)
49146439Sml29623 {
49156439Sml29623 	p_nxge_param_t	param_arr = nxgep->param_arr;
49166439Sml29623 	int		err = 0;
49176439Sml29623 	long		result;
49186439Sml29623 
49196439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49206439Sml29623 	    "==> nxge_set_priv_prop: name %s", pr_name));
49216439Sml29623 
49226439Sml29623 	if (strcmp(pr_name, "_accept_jumbo") == 0) {
49236439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
49246439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49256439Sml29623 		    "<== nxge_set_priv_prop: name %s "
49266439Sml29623 		    "pr_val %s result %d "
49276439Sml29623 		    "param %d is_jumbo %d",
49286439Sml29623 		    pr_name, pr_val, result,
49296439Sml29623 		    param_arr[param_accept_jumbo].value,
49306439Sml29623 		    nxgep->mac.is_jumbo));
49316439Sml29623 
49326439Sml29623 		if (result > 1 || result < 0) {
49336439Sml29623 			err = EINVAL;
49346439Sml29623 		} else {
49356439Sml29623 			if (nxgep->mac.is_jumbo ==
49366439Sml29623 			    (uint32_t)result) {
49376439Sml29623 				NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49386439Sml29623 				    "no change (%d %d)",
49396439Sml29623 				    nxgep->mac.is_jumbo,
49406439Sml29623 				    result));
49416439Sml29623 				return (0);
49426439Sml29623 			}
49436439Sml29623 		}
49446439Sml29623 
49456439Sml29623 		param_arr[param_accept_jumbo].value = result;
49466439Sml29623 		nxgep->mac.is_jumbo = B_FALSE;
49476439Sml29623 		if (result) {
49486439Sml29623 			nxgep->mac.is_jumbo = B_TRUE;
49496439Sml29623 		}
49506439Sml29623 
49516439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49526439Sml29623 		    "<== nxge_set_priv_prop: name %s (value %d) is_jumbo %d",
49536439Sml29623 		    pr_name, result, nxgep->mac.is_jumbo));
49546439Sml29623 
49556439Sml29623 		return (err);
49566439Sml29623 	}
49576439Sml29623 
49586439Sml29623 	/* Blanking */
49596439Sml29623 	if (strcmp(pr_name, "_rxdma_intr_time") == 0) {
49606439Sml29623 		err = nxge_param_rx_intr_time(nxgep, NULL, NULL,
49616439Sml29623 		    (char *)pr_val,
49626439Sml29623 		    (caddr_t)&param_arr[param_rxdma_intr_time]);
49636439Sml29623 		if (err) {
49646439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49656439Sml29623 			    "<== nxge_set_priv_prop: "
49666439Sml29623 			    "unable to set (%s)", pr_name));
49676439Sml29623 			err = EINVAL;
49686439Sml29623 		} else {
49696439Sml29623 			err = 0;
49706439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49716439Sml29623 			    "<== nxge_set_priv_prop: "
49726439Sml29623 			    "set (%s)", pr_name));
49736439Sml29623 		}
49746439Sml29623 
49756439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49766439Sml29623 		    "<== nxge_set_priv_prop: name %s (value %d)",
49776439Sml29623 		    pr_name, result));
49786439Sml29623 
49796439Sml29623 		return (err);
49806439Sml29623 	}
49816439Sml29623 
49826439Sml29623 	if (strcmp(pr_name, "_rxdma_intr_pkts") == 0) {
49836439Sml29623 		err = nxge_param_rx_intr_pkts(nxgep, NULL, NULL,
49846439Sml29623 		    (char *)pr_val,
49856439Sml29623 		    (caddr_t)&param_arr[param_rxdma_intr_pkts]);
49866439Sml29623 		if (err) {
49876439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49886439Sml29623 			    "<== nxge_set_priv_prop: "
49896439Sml29623 			    "unable to set (%s)", pr_name));
49906439Sml29623 			err = EINVAL;
49916439Sml29623 		} else {
49926439Sml29623 			err = 0;
49936439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49946439Sml29623 			    "<== nxge_set_priv_prop: "
49956439Sml29623 			    "set (%s)", pr_name));
49966439Sml29623 		}
49976439Sml29623 
49986439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49996439Sml29623 		    "<== nxge_set_priv_prop: name %s (value %d)",
50006439Sml29623 		    pr_name, result));
50016439Sml29623 
50026439Sml29623 		return (err);
50036439Sml29623 	}
50046439Sml29623 
50056439Sml29623 	/* Classification */
50066439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_tcp") == 0) {
50076439Sml29623 		if (pr_val == NULL) {
50086439Sml29623 			err = EINVAL;
50096439Sml29623 			return (err);
50106439Sml29623 		}
50116439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
50126439Sml29623 
50136439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
50146439Sml29623 		    NULL, (char *)pr_val,
50156439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_tcp]);
50166439Sml29623 
50176439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50186439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
50196439Sml29623 		    pr_name, result));
50206439Sml29623 
50216439Sml29623 		return (err);
50226439Sml29623 	}
50236439Sml29623 
50246439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_udp") == 0) {
50256439Sml29623 		if (pr_val == NULL) {
50266439Sml29623 			err = EINVAL;
50276439Sml29623 			return (err);
50286439Sml29623 		}
50296439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
50306439Sml29623 
50316439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
50326439Sml29623 		    NULL, (char *)pr_val,
50336439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_udp]);
50346439Sml29623 
50356439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50366439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
50376439Sml29623 		    pr_name, result));
50386439Sml29623 
50396439Sml29623 		return (err);
50406439Sml29623 	}
50416439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_ah") == 0) {
50426439Sml29623 		if (pr_val == NULL) {
50436439Sml29623 			err = EINVAL;
50446439Sml29623 			return (err);
50456439Sml29623 		}
50466439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
50476439Sml29623 
50486439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
50496439Sml29623 		    NULL, (char *)pr_val,
50506439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_ah]);
50516439Sml29623 
50526439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50536439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
50546439Sml29623 		    pr_name, result));
50556439Sml29623 
50566439Sml29623 		return (err);
50576439Sml29623 	}
50586439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_sctp") == 0) {
50596439Sml29623 		if (pr_val == NULL) {
50606439Sml29623 			err = EINVAL;
50616439Sml29623 			return (err);
50626439Sml29623 		}
50636439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
50646439Sml29623 
50656439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
50666439Sml29623 		    NULL, (char *)pr_val,
50676439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_sctp]);
50686439Sml29623 
50696439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50706439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
50716439Sml29623 		    pr_name, result));
50726439Sml29623 
50736439Sml29623 		return (err);
50746439Sml29623 	}
50756439Sml29623 
50766439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_tcp") == 0) {
50776439Sml29623 		if (pr_val == NULL) {
50786439Sml29623 			err = EINVAL;
50796439Sml29623 			return (err);
50806439Sml29623 		}
50816439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
50826439Sml29623 
50836439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
50846439Sml29623 		    NULL, (char *)pr_val,
50856439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_tcp]);
50866439Sml29623 
50876439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50886439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
50896439Sml29623 		    pr_name, result));
50906439Sml29623 
50916439Sml29623 		return (err);
50926439Sml29623 	}
50936439Sml29623 
50946439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_udp") == 0) {
50956439Sml29623 		if (pr_val == NULL) {
50966439Sml29623 			err = EINVAL;
50976439Sml29623 			return (err);
50986439Sml29623 		}
50996439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
51006439Sml29623 
51016439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
51026439Sml29623 		    NULL, (char *)pr_val,
51036439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_udp]);
51046439Sml29623 
51056439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51066439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
51076439Sml29623 		    pr_name, result));
51086439Sml29623 
51096439Sml29623 		return (err);
51106439Sml29623 	}
51116439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_ah") == 0) {
51126439Sml29623 		if (pr_val == NULL) {
51136439Sml29623 			err = EINVAL;
51146439Sml29623 			return (err);
51156439Sml29623 		}
51166439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
51176439Sml29623 
51186439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
51196439Sml29623 		    NULL, (char *)pr_val,
51206439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_ah]);
51216439Sml29623 
51226439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51236439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
51246439Sml29623 		    pr_name, result));
51256439Sml29623 
51266439Sml29623 		return (err);
51276439Sml29623 	}
51286439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_sctp") == 0) {
51296439Sml29623 		if (pr_val == NULL) {
51306439Sml29623 			err = EINVAL;
51316439Sml29623 			return (err);
51326439Sml29623 		}
51336439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
51346439Sml29623 
51356439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
51366439Sml29623 		    NULL, (char *)pr_val,
51376439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_sctp]);
51386439Sml29623 
51396439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51406439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
51416439Sml29623 		    pr_name, result));
51426439Sml29623 
51436439Sml29623 		return (err);
51446439Sml29623 	}
51456439Sml29623 
51466439Sml29623 	if (strcmp(pr_name, "_soft_lso_enable") == 0) {
51476439Sml29623 		if (nxgep->nxge_mac_state == NXGE_MAC_STARTED) {
51486439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51496439Sml29623 			    "==> nxge_set_priv_prop: name %s (busy)", pr_name));
51506439Sml29623 			err = EBUSY;
51516439Sml29623 			return (err);
51526439Sml29623 		}
51536439Sml29623 		if (pr_val == NULL) {
51546439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51556439Sml29623 			    "==> nxge_set_priv_prop: name %s (null)", pr_name));
51566439Sml29623 			err = EINVAL;
51576439Sml29623 			return (err);
51586439Sml29623 		}
51596439Sml29623 
51606439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
51616439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51626439Sml29623 		    "<== nxge_set_priv_prop: name %s "
51636439Sml29623 		    "(lso %d pr_val %s value %d)",
51646439Sml29623 		    pr_name, nxgep->soft_lso_enable, pr_val, result));
51656439Sml29623 
51666439Sml29623 		if (result > 1 || result < 0) {
51676439Sml29623 			err = EINVAL;
51686439Sml29623 		} else {
51696439Sml29623 			if (nxgep->soft_lso_enable == (uint32_t)result) {
51706439Sml29623 				NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51716439Sml29623 				    "no change (%d %d)",
51726439Sml29623 				    nxgep->soft_lso_enable, result));
51736439Sml29623 				return (0);
51746439Sml29623 			}
51756439Sml29623 		}
51766439Sml29623 
51776439Sml29623 		nxgep->soft_lso_enable = (int)result;
51786439Sml29623 
51796439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51806439Sml29623 		    "<== nxge_set_priv_prop: name %s (value %d)",
51816439Sml29623 		    pr_name, result));
51826439Sml29623 
51836439Sml29623 		return (err);
51846439Sml29623 	}
51856835Syc148097 	/*
51866835Syc148097 	 * Commands like "ndd -set /dev/nxge0 adv_10gfdx_cap 1" cause the
51876835Syc148097 	 * following code to be executed.
51886835Syc148097 	 */
51896512Ssowmini 	if (strcmp(pr_name, "_adv_10gfdx_cap") == 0) {
51906512Ssowmini 		err = nxge_param_set_mac(nxgep, NULL, NULL, (char *)pr_val,
51916512Ssowmini 		    (caddr_t)&param_arr[param_anar_10gfdx]);
51926512Ssowmini 		return (err);
51936512Ssowmini 	}
51946512Ssowmini 	if (strcmp(pr_name, "_adv_pause_cap") == 0) {
51956512Ssowmini 		err = nxge_param_set_mac(nxgep, NULL, NULL, (char *)pr_val,
51966512Ssowmini 		    (caddr_t)&param_arr[param_anar_pause]);
51976512Ssowmini 		return (err);
51986512Ssowmini 	}
51996439Sml29623 
52006439Sml29623 	return (EINVAL);
52016439Sml29623 }
52026439Sml29623 
52036439Sml29623 static int
52046512Ssowmini nxge_get_priv_prop(p_nxge_t nxgep, const char *pr_name, uint_t pr_flags,
52056512Ssowmini     uint_t pr_valsize, void *pr_val)
52066439Sml29623 {
52076439Sml29623 	p_nxge_param_t	param_arr = nxgep->param_arr;
52086439Sml29623 	char		valstr[MAXNAMELEN];
52096439Sml29623 	int		err = EINVAL;
52106439Sml29623 	uint_t		strsize;
52116789Sam223141 	boolean_t	is_default = (pr_flags & MAC_PROP_DEFAULT);
52126439Sml29623 
52136439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
52146439Sml29623 	    "==> nxge_get_priv_prop: property %s", pr_name));
52156439Sml29623 
52166439Sml29623 	/* function number */
52176439Sml29623 	if (strcmp(pr_name, "_function_number") == 0) {
52186512Ssowmini 		if (is_default)
52196512Ssowmini 			return (ENOTSUP);
52206512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%d",
52216512Ssowmini 		    nxgep->function_num);
52226439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
52236439Sml29623 		    "==> nxge_get_priv_prop: name %s "
52246439Sml29623 		    "(value %d valstr %s)",
52256439Sml29623 		    pr_name, nxgep->function_num, valstr));
52266439Sml29623 
52276439Sml29623 		err = 0;
52286439Sml29623 		goto done;
52296439Sml29623 	}
52306439Sml29623 
52316439Sml29623 	/* Neptune firmware version */
52326439Sml29623 	if (strcmp(pr_name, "_fw_version") == 0) {
52336512Ssowmini 		if (is_default)
52346512Ssowmini 			return (ENOTSUP);
52356512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%s",
52366512Ssowmini 		    nxgep->vpd_info.ver);
52376439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
52386439Sml29623 		    "==> nxge_get_priv_prop: name %s "
52396439Sml29623 		    "(value %d valstr %s)",
52406439Sml29623 		    pr_name, nxgep->vpd_info.ver, valstr));
52416439Sml29623 
52426439Sml29623 		err = 0;
52436439Sml29623 		goto done;
52446439Sml29623 	}
52456439Sml29623 
52466439Sml29623 	/* port PHY mode */
52476439Sml29623 	if (strcmp(pr_name, "_port_mode") == 0) {
52486512Ssowmini 		if (is_default)
52496512Ssowmini 			return (ENOTSUP);
52506439Sml29623 		switch (nxgep->mac.portmode) {
52516439Sml29623 		case PORT_1G_COPPER:
52526512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "1G copper %s",
52536439Sml29623 			    nxgep->hot_swappable_phy ?
52546439Sml29623 			    "[Hot Swappable]" : "");
52556439Sml29623 			break;
52566439Sml29623 		case PORT_1G_FIBER:
52576512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "1G fiber %s",
52586439Sml29623 			    nxgep->hot_swappable_phy ?
52596439Sml29623 			    "[hot swappable]" : "");
52606439Sml29623 			break;
52616439Sml29623 		case PORT_10G_COPPER:
52626512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
52636512Ssowmini 			    "10G copper %s",
52646439Sml29623 			    nxgep->hot_swappable_phy ?
52656439Sml29623 			    "[hot swappable]" : "");
52666439Sml29623 			break;
52676439Sml29623 		case PORT_10G_FIBER:
52686512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "10G fiber %s",
52696439Sml29623 			    nxgep->hot_swappable_phy ?
52706439Sml29623 			    "[hot swappable]" : "");
52716439Sml29623 			break;
52726439Sml29623 		case PORT_10G_SERDES:
52736512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
52746512Ssowmini 			    "10G serdes %s", nxgep->hot_swappable_phy ?
52756439Sml29623 			    "[hot swappable]" : "");
52766439Sml29623 			break;
52776439Sml29623 		case PORT_1G_SERDES:
52786512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "1G serdes %s",
52796439Sml29623 			    nxgep->hot_swappable_phy ?
52806439Sml29623 			    "[hot swappable]" : "");
52816439Sml29623 			break;
52826835Syc148097 		case PORT_1G_TN1010:
52836835Syc148097 			(void) snprintf(valstr, sizeof (valstr),
52846835Syc148097 			    "1G TN1010 copper %s", nxgep->hot_swappable_phy ?
52856835Syc148097 			    "[hot swappable]" : "");
52866835Syc148097 			break;
52876835Syc148097 		case PORT_10G_TN1010:
52886835Syc148097 			(void) snprintf(valstr, sizeof (valstr),
52896835Syc148097 			    "10G TN1010 copper %s", nxgep->hot_swappable_phy ?
52906835Syc148097 			    "[hot swappable]" : "");
52916835Syc148097 			break;
52926439Sml29623 		case PORT_1G_RGMII_FIBER:
52936512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
52946512Ssowmini 			    "1G rgmii fiber %s", nxgep->hot_swappable_phy ?
52956439Sml29623 			    "[hot swappable]" : "");
52966439Sml29623 			break;
52976439Sml29623 		case PORT_HSP_MODE:
52986512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
52996444Sml29623 			    "phy not present[hot swappable]");
53006439Sml29623 			break;
53016439Sml29623 		default:
53026512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "unknown %s",
53036439Sml29623 			    nxgep->hot_swappable_phy ?
53046439Sml29623 			    "[hot swappable]" : "");
53056439Sml29623 			break;
53066439Sml29623 		}
53076439Sml29623 
53086439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53096439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %s)",
53106439Sml29623 		    pr_name, valstr));
53116439Sml29623 
53126439Sml29623 		err = 0;
53136439Sml29623 		goto done;
53146439Sml29623 	}
53156439Sml29623 
53166439Sml29623 	/* Hot swappable PHY */
53176439Sml29623 	if (strcmp(pr_name, "_hot_swap_phy") == 0) {
53186512Ssowmini 		if (is_default)
53196512Ssowmini 			return (ENOTSUP);
53206512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%s",
53216439Sml29623 		    nxgep->hot_swappable_phy ?
53226439Sml29623 		    "yes" : "no");
53236439Sml29623 
53246439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53256439Sml29623 		    "==> nxge_get_priv_prop: name %s "
53266439Sml29623 		    "(value %d valstr %s)",
53276439Sml29623 		    pr_name, nxgep->hot_swappable_phy, valstr));
53286439Sml29623 
53296439Sml29623 		err = 0;
53306439Sml29623 		goto done;
53316439Sml29623 	}
53326439Sml29623 
53336439Sml29623 
53346439Sml29623 	/* accept jumbo */
53356439Sml29623 	if (strcmp(pr_name, "_accept_jumbo") == 0) {
53366512Ssowmini 		if (is_default)
53376512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),  "%d", 0);
53386512Ssowmini 		else
53396512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
53406512Ssowmini 			    "%d", nxgep->mac.is_jumbo);
53416439Sml29623 		err = 0;
53426439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53436439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %d (%d, %d))",
53446439Sml29623 		    pr_name,
53456439Sml29623 		    (uint32_t)param_arr[param_accept_jumbo].value,
53466439Sml29623 		    nxgep->mac.is_jumbo,
53476439Sml29623 		    nxge_jumbo_enable));
53486439Sml29623 
53496439Sml29623 		goto done;
53506439Sml29623 	}
53516439Sml29623 
53526439Sml29623 	/* Receive Interrupt Blanking Parameters */
53536439Sml29623 	if (strcmp(pr_name, "_rxdma_intr_time") == 0) {
53546512Ssowmini 		err = 0;
53556512Ssowmini 		if (is_default) {
53566512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
53576512Ssowmini 			    "%d", RXDMA_RCR_TO_DEFAULT);
53586512Ssowmini 			goto done;
53596512Ssowmini 		}
53606512Ssowmini 
53616512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%d",
53626512Ssowmini 		    nxgep->intr_timeout);
53636439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53646439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %d)",
53656439Sml29623 		    pr_name,
53666439Sml29623 		    (uint32_t)nxgep->intr_timeout));
53676439Sml29623 		goto done;
53686439Sml29623 	}
53696439Sml29623 
53706439Sml29623 	if (strcmp(pr_name, "_rxdma_intr_pkts") == 0) {
53716512Ssowmini 		err = 0;
53726512Ssowmini 		if (is_default) {
53736512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
53746512Ssowmini 			    "%d", RXDMA_RCR_PTHRES_DEFAULT);
53756512Ssowmini 			goto done;
53766512Ssowmini 		}
53776512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%d",
53786512Ssowmini 		    nxgep->intr_threshold);
53796439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53806439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %d)",
53816439Sml29623 		    pr_name, (uint32_t)nxgep->intr_threshold));
53826439Sml29623 
53836439Sml29623 		goto done;
53846439Sml29623 	}
53856439Sml29623 
53866439Sml29623 	/* Classification and Load Distribution Configuration */
53876439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_tcp") == 0) {
53886512Ssowmini 		if (is_default) {
53896512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
53906512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
53916512Ssowmini 			err = 0;
53926512Ssowmini 			goto done;
53936512Ssowmini 		}
53946439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
53956439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_tcp]);
53966439Sml29623 
53976512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
53986439Sml29623 		    (int)param_arr[param_class_opt_ipv4_tcp].value);
53996439Sml29623 
54006439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
54016439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
54026439Sml29623 		goto done;
54036439Sml29623 	}
54046439Sml29623 
54056439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_udp") == 0) {
54066512Ssowmini 		if (is_default) {
54076512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
54086512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
54096512Ssowmini 			err = 0;
54106512Ssowmini 			goto done;
54116512Ssowmini 		}
54126439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
54136439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_udp]);
54146439Sml29623 
54156512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
54166439Sml29623 		    (int)param_arr[param_class_opt_ipv4_udp].value);
54176439Sml29623 
54186439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
54196439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
54206439Sml29623 		goto done;
54216439Sml29623 	}
54226439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_ah") == 0) {
54236512Ssowmini 		if (is_default) {
54246512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
54256512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
54266512Ssowmini 			err = 0;
54276512Ssowmini 			goto done;
54286512Ssowmini 		}
54296439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
54306439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_ah]);
54316439Sml29623 
54326512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
54336439Sml29623 		    (int)param_arr[param_class_opt_ipv4_ah].value);
54346439Sml29623 
54356439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
54366439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
54376439Sml29623 		goto done;
54386439Sml29623 	}
54396439Sml29623 
54406439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_sctp") == 0) {
54416512Ssowmini 		if (is_default) {
54426512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
54436512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
54446512Ssowmini 			err = 0;
54456512Ssowmini 			goto done;
54466512Ssowmini 		}
54476439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
54486439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_sctp]);
54496439Sml29623 
54506512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
54516439Sml29623 		    (int)param_arr[param_class_opt_ipv4_sctp].value);
54526439Sml29623 
54536439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
54546439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
54556439Sml29623 		goto done;
54566439Sml29623 	}
54576439Sml29623 
54586439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_tcp") == 0) {
54596512Ssowmini 		if (is_default) {
54606512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
54616512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
54626512Ssowmini 			err = 0;
54636512Ssowmini 			goto done;
54646512Ssowmini 		}
54656439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
54666439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_tcp]);
54676439Sml29623 
54686512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
54696439Sml29623 		    (int)param_arr[param_class_opt_ipv6_tcp].value);
54706439Sml29623 
54716439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
54726439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
54736439Sml29623 		goto done;
54746439Sml29623 	}
54756439Sml29623 
54766439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_udp") == 0) {
54776512Ssowmini 		if (is_default) {
54786512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
54796512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
54806512Ssowmini 			err = 0;
54816512Ssowmini 			goto done;
54826512Ssowmini 		}
54836439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
54846439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_udp]);
54856439Sml29623 
54866512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
54876439Sml29623 		    (int)param_arr[param_class_opt_ipv6_udp].value);
54886439Sml29623 
54896439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
54906439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
54916439Sml29623 		goto done;
54926439Sml29623 	}
54936439Sml29623 
54946439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_ah") == 0) {
54956512Ssowmini 		if (is_default) {
54966512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
54976512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
54986512Ssowmini 			err = 0;
54996512Ssowmini 			goto done;
55006512Ssowmini 		}
55016439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
55026439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_ah]);
55036439Sml29623 
55046512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
55056439Sml29623 		    (int)param_arr[param_class_opt_ipv6_ah].value);
55066439Sml29623 
55076439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
55086439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
55096439Sml29623 		goto done;
55106439Sml29623 	}
55116439Sml29623 
55126439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_sctp") == 0) {
55136512Ssowmini 		if (is_default) {
55146512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
55156512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
55166512Ssowmini 			err = 0;
55176512Ssowmini 			goto done;
55186512Ssowmini 		}
55196439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
55206439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_sctp]);
55216439Sml29623 
55226512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
55236439Sml29623 		    (int)param_arr[param_class_opt_ipv6_sctp].value);
55246439Sml29623 
55256439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
55266439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
55276439Sml29623 		goto done;
55286439Sml29623 	}
55296439Sml29623 
55306439Sml29623 	/* Software LSO */
55316439Sml29623 	if (strcmp(pr_name, "_soft_lso_enable") == 0) {
55326512Ssowmini 		if (is_default) {
55336512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 0);
55346512Ssowmini 			err = 0;
55356512Ssowmini 			goto done;
55366512Ssowmini 		}
55376512Ssowmini 		(void) snprintf(valstr, sizeof (valstr),
55386512Ssowmini 		    "%d", nxgep->soft_lso_enable);
55396439Sml29623 		err = 0;
55406439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
55416439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %d)",
55426439Sml29623 		    pr_name, nxgep->soft_lso_enable));
55436439Sml29623 
55446439Sml29623 		goto done;
55456439Sml29623 	}
55466512Ssowmini 	if (strcmp(pr_name, "_adv_10gfdx_cap") == 0) {
55476512Ssowmini 		err = 0;
55486512Ssowmini 		if (is_default ||
55496512Ssowmini 		    nxgep->param_arr[param_anar_10gfdx].value != 0) {
55506512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 1);
55516512Ssowmini 			goto done;
55526512Ssowmini 		} else {
55536512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 0);
55546512Ssowmini 			goto done;
55556512Ssowmini 		}
55566512Ssowmini 	}
55576512Ssowmini 	if (strcmp(pr_name, "_adv_pause_cap") == 0) {
55586512Ssowmini 		err = 0;
55596512Ssowmini 		if (is_default ||
55606512Ssowmini 		    nxgep->param_arr[param_anar_pause].value != 0) {
55616512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 1);
55626512Ssowmini 			goto done;
55636512Ssowmini 		} else {
55646512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 0);
55656512Ssowmini 			goto done;
55666512Ssowmini 		}
55676512Ssowmini 	}
55686439Sml29623 
55696439Sml29623 done:
55706439Sml29623 	if (err == 0) {
55716439Sml29623 		strsize = (uint_t)strlen(valstr);
55726439Sml29623 		if (pr_valsize < strsize) {
55736439Sml29623 			err = ENOBUFS;
55746439Sml29623 		} else {
55756439Sml29623 			(void) strlcpy(pr_val, valstr, pr_valsize);
55766439Sml29623 		}
55776439Sml29623 	}
55786439Sml29623 
55796439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
55806439Sml29623 	    "<== nxge_get_priv_prop: return %d", err));
55816439Sml29623 	return (err);
55826439Sml29623 }
55836439Sml29623 
55843859Sml29623 /*
55853859Sml29623  * Module loading and removing entry points.
55863859Sml29623  */
55873859Sml29623 
55886705Sml29623 DDI_DEFINE_STREAM_OPS(nxge_dev_ops, nulldev, nulldev, nxge_attach, nxge_detach,
55897656SSherry.Moore@Sun.COM     nodev, NULL, D_MP, NULL, nxge_quiesce);
55903859Sml29623 
55914977Sraghus #define	NXGE_DESC_VER		"Sun NIU 10Gb Ethernet"
55923859Sml29623 
55933859Sml29623 /*
55943859Sml29623  * Module linkage information for the kernel.
55953859Sml29623  */
55963859Sml29623 static struct modldrv 	nxge_modldrv = {
55973859Sml29623 	&mod_driverops,
55983859Sml29623 	NXGE_DESC_VER,
55993859Sml29623 	&nxge_dev_ops
56003859Sml29623 };
56013859Sml29623 
56023859Sml29623 static struct modlinkage modlinkage = {
56033859Sml29623 	MODREV_1, (void *) &nxge_modldrv, NULL
56043859Sml29623 };
56053859Sml29623 
56063859Sml29623 int
56073859Sml29623 _init(void)
56083859Sml29623 {
56093859Sml29623 	int		status;
56103859Sml29623 
56113859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "==> _init"));
56123859Sml29623 	mac_init_ops(&nxge_dev_ops, "nxge");
56133859Sml29623 	status = ddi_soft_state_init(&nxge_list, sizeof (nxge_t), 0);
56143859Sml29623 	if (status != 0) {
56153859Sml29623 		NXGE_ERROR_MSG((NULL, NXGE_ERR_CTL,
56166512Ssowmini 		    "failed to init device soft state"));
56173859Sml29623 		goto _init_exit;
56183859Sml29623 	}
56193859Sml29623 	status = mod_install(&modlinkage);
56203859Sml29623 	if (status != 0) {
56213859Sml29623 		ddi_soft_state_fini(&nxge_list);
56223859Sml29623 		NXGE_ERROR_MSG((NULL, NXGE_ERR_CTL, "Mod install failed"));
56233859Sml29623 		goto _init_exit;
56243859Sml29623 	}
56253859Sml29623 
56263859Sml29623 	MUTEX_INIT(&nxge_common_lock, NULL, MUTEX_DRIVER, NULL);
56273859Sml29623 
56283859Sml29623 _init_exit:
56293859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "_init status = 0x%X", status));
56303859Sml29623 
56313859Sml29623 	return (status);
56323859Sml29623 }
56333859Sml29623 
56343859Sml29623 int
56353859Sml29623 _fini(void)
56363859Sml29623 {
56373859Sml29623 	int		status;
56383859Sml29623 
56393859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "==> _fini"));
56403859Sml29623 
56413859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "==> _fini: mod_remove"));
56423859Sml29623 
56433859Sml29623 	if (nxge_mblks_pending)
56443859Sml29623 		return (EBUSY);
56453859Sml29623 
56463859Sml29623 	status = mod_remove(&modlinkage);
56473859Sml29623 	if (status != DDI_SUCCESS) {
56483859Sml29623 		NXGE_DEBUG_MSG((NULL, MOD_CTL,
56496512Ssowmini 		    "Module removal failed 0x%08x",
56506512Ssowmini 		    status));
56513859Sml29623 		goto _fini_exit;
56523859Sml29623 	}
56533859Sml29623 
56543859Sml29623 	mac_fini_ops(&nxge_dev_ops);
56553859Sml29623 
56563859Sml29623 	ddi_soft_state_fini(&nxge_list);
56573859Sml29623 
56583859Sml29623 	MUTEX_DESTROY(&nxge_common_lock);
56593859Sml29623 _fini_exit:
56603859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "_fini status = 0x%08x", status));
56613859Sml29623 
56623859Sml29623 	return (status);
56633859Sml29623 }
56643859Sml29623 
56653859Sml29623 int
56663859Sml29623 _info(struct modinfo *modinfop)
56673859Sml29623 {
56683859Sml29623 	int		status;
56693859Sml29623 
56703859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "==> _info"));
56713859Sml29623 	status = mod_info(&modlinkage, modinfop);
56723859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, " _info status = 0x%X", status));
56733859Sml29623 
56743859Sml29623 	return (status);
56753859Sml29623 }
56763859Sml29623 
56773859Sml29623 /*ARGSUSED*/
56783859Sml29623 static nxge_status_t
56793859Sml29623 nxge_add_intrs(p_nxge_t nxgep)
56803859Sml29623 {
56813859Sml29623 
56823859Sml29623 	int		intr_types;
56833859Sml29623 	int		type = 0;
56843859Sml29623 	int		ddi_status = DDI_SUCCESS;
56853859Sml29623 	nxge_status_t	status = NXGE_OK;
56863859Sml29623 
56873859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs"));
56883859Sml29623 
56893859Sml29623 	nxgep->nxge_intr_type.intr_registered = B_FALSE;
56903859Sml29623 	nxgep->nxge_intr_type.intr_enabled = B_FALSE;
56913859Sml29623 	nxgep->nxge_intr_type.msi_intx_cnt = 0;
56923859Sml29623 	nxgep->nxge_intr_type.intr_added = 0;
56933859Sml29623 	nxgep->nxge_intr_type.niu_msi_enable = B_FALSE;
56943859Sml29623 	nxgep->nxge_intr_type.intr_type = 0;
56953859Sml29623 
56963859Sml29623 	if (nxgep->niu_type == N2_NIU) {
56973859Sml29623 		nxgep->nxge_intr_type.niu_msi_enable = B_TRUE;
56983859Sml29623 	} else if (nxge_msi_enable) {
56993859Sml29623 		nxgep->nxge_intr_type.niu_msi_enable = B_TRUE;
57003859Sml29623 	}
57013859Sml29623 
57023859Sml29623 	/* Get the supported interrupt types */
57033859Sml29623 	if ((ddi_status = ddi_intr_get_supported_types(nxgep->dip, &intr_types))
57046512Ssowmini 	    != DDI_SUCCESS) {
57053859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "<== nxge_add_intrs: "
57066512Ssowmini 		    "ddi_intr_get_supported_types failed: status 0x%08x",
57076512Ssowmini 		    ddi_status));
57083859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
57093859Sml29623 	}
57103859Sml29623 	nxgep->nxge_intr_type.intr_types = intr_types;
57113859Sml29623 
57123859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
57136512Ssowmini 	    "ddi_intr_get_supported_types: 0x%08x", intr_types));
57143859Sml29623 
57153859Sml29623 	/*
57163859Sml29623 	 * Solaris MSIX is not supported yet. use MSI for now.
57173859Sml29623 	 * nxge_msi_enable (1):
57183859Sml29623 	 *	1 - MSI		2 - MSI-X	others - FIXED
57193859Sml29623 	 */
57203859Sml29623 	switch (nxge_msi_enable) {
57213859Sml29623 	default:
57223859Sml29623 		type = DDI_INTR_TYPE_FIXED;
57233859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs: "
57246512Ssowmini 		    "use fixed (intx emulation) type %08x",
57256512Ssowmini 		    type));
57263859Sml29623 		break;
57273859Sml29623 
57283859Sml29623 	case 2:
57293859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs: "
57306512Ssowmini 		    "ddi_intr_get_supported_types: 0x%08x", intr_types));
57313859Sml29623 		if (intr_types & DDI_INTR_TYPE_MSIX) {
57323859Sml29623 			type = DDI_INTR_TYPE_MSIX;
57333859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
57346512Ssowmini 			    "ddi_intr_get_supported_types: MSIX 0x%08x",
57356512Ssowmini 			    type));
57363859Sml29623 		} else if (intr_types & DDI_INTR_TYPE_MSI) {
57373859Sml29623 			type = DDI_INTR_TYPE_MSI;
57383859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
57396512Ssowmini 			    "ddi_intr_get_supported_types: MSI 0x%08x",
57406512Ssowmini 			    type));
57413859Sml29623 		} else if (intr_types & DDI_INTR_TYPE_FIXED) {
57423859Sml29623 			type = DDI_INTR_TYPE_FIXED;
57433859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs: "
57446512Ssowmini 			    "ddi_intr_get_supported_types: MSXED0x%08x",
57456512Ssowmini 			    type));
57463859Sml29623 		}
57473859Sml29623 		break;
57483859Sml29623 
57493859Sml29623 	case 1:
57503859Sml29623 		if (intr_types & DDI_INTR_TYPE_MSI) {
57513859Sml29623 			type = DDI_INTR_TYPE_MSI;
57523859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs: "
57536512Ssowmini 			    "ddi_intr_get_supported_types: MSI 0x%08x",
57546512Ssowmini 			    type));
57553859Sml29623 		} else if (intr_types & DDI_INTR_TYPE_MSIX) {
57563859Sml29623 			type = DDI_INTR_TYPE_MSIX;
57573859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
57586512Ssowmini 			    "ddi_intr_get_supported_types: MSIX 0x%08x",
57596512Ssowmini 			    type));
57603859Sml29623 		} else if (intr_types & DDI_INTR_TYPE_FIXED) {
57613859Sml29623 			type = DDI_INTR_TYPE_FIXED;
57623859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
57636512Ssowmini 			    "ddi_intr_get_supported_types: MSXED0x%08x",
57646512Ssowmini 			    type));
57653859Sml29623 		}
57663859Sml29623 	}
57673859Sml29623 
57683859Sml29623 	nxgep->nxge_intr_type.intr_type = type;
57693859Sml29623 	if ((type == DDI_INTR_TYPE_MSIX || type == DDI_INTR_TYPE_MSI ||
57706512Ssowmini 	    type == DDI_INTR_TYPE_FIXED) &&
57716512Ssowmini 	    nxgep->nxge_intr_type.niu_msi_enable) {
57723859Sml29623 		if ((status = nxge_add_intrs_adv(nxgep)) != DDI_SUCCESS) {
57733859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
57746512Ssowmini 			    " nxge_add_intrs: "
57756512Ssowmini 			    " nxge_add_intrs_adv failed: status 0x%08x",
57766512Ssowmini 			    status));
57773859Sml29623 			return (status);
57783859Sml29623 		} else {
57793859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
57806512Ssowmini 			    "interrupts registered : type %d", type));
57813859Sml29623 			nxgep->nxge_intr_type.intr_registered = B_TRUE;
57823859Sml29623 
57833859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
57846512Ssowmini 			    "\nAdded advanced nxge add_intr_adv "
57856512Ssowmini 			    "intr type 0x%x\n", type));
57863859Sml29623 
57873859Sml29623 			return (status);
57883859Sml29623 		}
57893859Sml29623 	}
57903859Sml29623 
57913859Sml29623 	if (!nxgep->nxge_intr_type.intr_registered) {
57923859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "==> nxge_add_intrs: "
57936512Ssowmini 		    "failed to register interrupts"));
57943859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
57953859Sml29623 	}
57963859Sml29623 
57973859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_add_intrs"));
57983859Sml29623 	return (status);
57993859Sml29623 }
58003859Sml29623 
58013859Sml29623 /*ARGSUSED*/
58023859Sml29623 static nxge_status_t
58033859Sml29623 nxge_add_soft_intrs(p_nxge_t nxgep)
58043859Sml29623 {
58053859Sml29623 
58063859Sml29623 	int		ddi_status = DDI_SUCCESS;
58073859Sml29623 	nxge_status_t	status = NXGE_OK;
58083859Sml29623 
58093859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_soft_intrs"));
58103859Sml29623 
58113859Sml29623 	nxgep->resched_id = NULL;
58123859Sml29623 	nxgep->resched_running = B_FALSE;
58133859Sml29623 	ddi_status = ddi_add_softintr(nxgep->dip, DDI_SOFTINT_LOW,
58146512Ssowmini 	    &nxgep->resched_id,
58156512Ssowmini 	    NULL, NULL, nxge_reschedule, (caddr_t)nxgep);
58163859Sml29623 	if (ddi_status != DDI_SUCCESS) {
58173859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "<== nxge_add_soft_intrs: "
58186512Ssowmini 		    "ddi_add_softintrs failed: status 0x%08x",
58196512Ssowmini 		    ddi_status));
58203859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
58213859Sml29623 	}
58223859Sml29623 
58233859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_ddi_add_soft_intrs"));
58243859Sml29623 
58253859Sml29623 	return (status);
58263859Sml29623 }
58273859Sml29623 
58283859Sml29623 static nxge_status_t
58293859Sml29623 nxge_add_intrs_adv(p_nxge_t nxgep)
58303859Sml29623 {
58313859Sml29623 	int		intr_type;
58323859Sml29623 	p_nxge_intr_t	intrp;
58333859Sml29623 
58343859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs_adv"));
58353859Sml29623 
58363859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
58373859Sml29623 	intr_type = intrp->intr_type;
58383859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs_adv: type 0x%x",
58396512Ssowmini 	    intr_type));
58403859Sml29623 
58413859Sml29623 	switch (intr_type) {
58423859Sml29623 	case DDI_INTR_TYPE_MSI: /* 0x2 */
58433859Sml29623 	case DDI_INTR_TYPE_MSIX: /* 0x4 */
58443859Sml29623 		return (nxge_add_intrs_adv_type(nxgep, intr_type));
58453859Sml29623 
58463859Sml29623 	case DDI_INTR_TYPE_FIXED: /* 0x1 */
58473859Sml29623 		return (nxge_add_intrs_adv_type_fix(nxgep, intr_type));
58483859Sml29623 
58493859Sml29623 	default:
58503859Sml29623 		return (NXGE_ERROR);
58513859Sml29623 	}
58523859Sml29623 }
58533859Sml29623 
58543859Sml29623 
58553859Sml29623 /*ARGSUSED*/
58563859Sml29623 static nxge_status_t
58573859Sml29623 nxge_add_intrs_adv_type(p_nxge_t nxgep, uint32_t int_type)
58583859Sml29623 {
58593859Sml29623 	dev_info_t		*dip = nxgep->dip;
58603859Sml29623 	p_nxge_ldg_t		ldgp;
58613859Sml29623 	p_nxge_intr_t		intrp;
58623859Sml29623 	uint_t			*inthandler;
58633859Sml29623 	void			*arg1, *arg2;
58643859Sml29623 	int			behavior;
58655013Sml29623 	int			nintrs, navail, nrequest;
58663859Sml29623 	int			nactual, nrequired;
58673859Sml29623 	int			inum = 0;
58683859Sml29623 	int			x, y;
58693859Sml29623 	int			ddi_status = DDI_SUCCESS;
58703859Sml29623 	nxge_status_t		status = NXGE_OK;
58713859Sml29623 
58723859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs_adv_type"));
58733859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
58743859Sml29623 	intrp->start_inum = 0;
58753859Sml29623 
58763859Sml29623 	ddi_status = ddi_intr_get_nintrs(dip, int_type, &nintrs);
58773859Sml29623 	if ((ddi_status != DDI_SUCCESS) || (nintrs == 0)) {
58783859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
58796512Ssowmini 		    "ddi_intr_get_nintrs() failed, status: 0x%x%, "
58806512Ssowmini 		    "nintrs: %d", ddi_status, nintrs));
58813859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
58823859Sml29623 	}
58833859Sml29623 
58843859Sml29623 	ddi_status = ddi_intr_get_navail(dip, int_type, &navail);
58853859Sml29623 	if ((ddi_status != DDI_SUCCESS) || (navail == 0)) {
58863859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
58876512Ssowmini 		    "ddi_intr_get_navail() failed, status: 0x%x%, "
58886512Ssowmini 		    "nintrs: %d", ddi_status, navail));
58893859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
58903859Sml29623 	}
58913859Sml29623 
58923859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL,
58936512Ssowmini 	    "ddi_intr_get_navail() returned: nintrs %d, navail %d",
58946512Ssowmini 	    nintrs, navail));
58953859Sml29623 
58965013Sml29623 	/* PSARC/2007/453 MSI-X interrupt limit override */
58975013Sml29623 	if (int_type == DDI_INTR_TYPE_MSIX) {
58985013Sml29623 		nrequest = nxge_create_msi_property(nxgep);
58995013Sml29623 		if (nrequest < navail) {
59005013Sml29623 			navail = nrequest;
59015013Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
59025013Sml29623 			    "nxge_add_intrs_adv_type: nintrs %d "
59035013Sml29623 			    "navail %d (nrequest %d)",
59045013Sml29623 			    nintrs, navail, nrequest));
59055013Sml29623 		}
59065013Sml29623 	}
59075013Sml29623 
59083859Sml29623 	if (int_type == DDI_INTR_TYPE_MSI && !ISP2(navail)) {
59093859Sml29623 		/* MSI must be power of 2 */
59103859Sml29623 		if ((navail & 16) == 16) {
59113859Sml29623 			navail = 16;
59123859Sml29623 		} else if ((navail & 8) == 8) {
59133859Sml29623 			navail = 8;
59143859Sml29623 		} else if ((navail & 4) == 4) {
59153859Sml29623 			navail = 4;
59163859Sml29623 		} else if ((navail & 2) == 2) {
59173859Sml29623 			navail = 2;
59183859Sml29623 		} else {
59193859Sml29623 			navail = 1;
59203859Sml29623 		}
59213859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
59226512Ssowmini 		    "ddi_intr_get_navail(): (msi power of 2) nintrs %d, "
59236512Ssowmini 		    "navail %d", nintrs, navail));
59243859Sml29623 	}
59253859Sml29623 
59263859Sml29623 	behavior = ((int_type == DDI_INTR_TYPE_FIXED) ? DDI_INTR_ALLOC_STRICT :
59276512Ssowmini 	    DDI_INTR_ALLOC_NORMAL);
59283859Sml29623 	intrp->intr_size = navail * sizeof (ddi_intr_handle_t);
59293859Sml29623 	intrp->htable = kmem_alloc(intrp->intr_size, KM_SLEEP);
59303859Sml29623 	ddi_status = ddi_intr_alloc(dip, intrp->htable, int_type, inum,
59316512Ssowmini 	    navail, &nactual, behavior);
59323859Sml29623 	if (ddi_status != DDI_SUCCESS || nactual == 0) {
59333859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
59346512Ssowmini 		    " ddi_intr_alloc() failed: %d",
59356512Ssowmini 		    ddi_status));
59363859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
59373859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
59383859Sml29623 	}
59393859Sml29623 
59403859Sml29623 	if ((ddi_status = ddi_intr_get_pri(intrp->htable[0],
59416512Ssowmini 	    (uint_t *)&intrp->pri)) != DDI_SUCCESS) {
59423859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
59436512Ssowmini 		    " ddi_intr_get_pri() failed: %d",
59446512Ssowmini 		    ddi_status));
59453859Sml29623 		/* Free already allocated interrupts */
59463859Sml29623 		for (y = 0; y < nactual; y++) {
59473859Sml29623 			(void) ddi_intr_free(intrp->htable[y]);
59483859Sml29623 		}
59493859Sml29623 
59503859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
59513859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
59523859Sml29623 	}
59533859Sml29623 
59543859Sml29623 	nrequired = 0;
59553859Sml29623 	switch (nxgep->niu_type) {
59563859Sml29623 	default:
59573859Sml29623 		status = nxge_ldgv_init(nxgep, &nactual, &nrequired);
59583859Sml29623 		break;
59593859Sml29623 
59603859Sml29623 	case N2_NIU:
59613859Sml29623 		status = nxge_ldgv_init_n2(nxgep, &nactual, &nrequired);
59623859Sml29623 		break;
59633859Sml29623 	}
59643859Sml29623 
59653859Sml29623 	if (status != NXGE_OK) {
59663859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
59676512Ssowmini 		    "nxge_add_intrs_adv_typ:nxge_ldgv_init "
59686512Ssowmini 		    "failed: 0x%x", status));
59693859Sml29623 		/* Free already allocated interrupts */
59703859Sml29623 		for (y = 0; y < nactual; y++) {
59713859Sml29623 			(void) ddi_intr_free(intrp->htable[y]);
59723859Sml29623 		}
59733859Sml29623 
59743859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
59753859Sml29623 		return (status);
59763859Sml29623 	}
59773859Sml29623 
59783859Sml29623 	ldgp = nxgep->ldgvp->ldgp;
59793859Sml29623 	for (x = 0; x < nrequired; x++, ldgp++) {
59803859Sml29623 		ldgp->vector = (uint8_t)x;
59813859Sml29623 		ldgp->intdata = SID_DATA(ldgp->func, x);
59823859Sml29623 		arg1 = ldgp->ldvp;
59833859Sml29623 		arg2 = nxgep;
59843859Sml29623 		if (ldgp->nldvs == 1) {
59853859Sml29623 			inthandler = (uint_t *)ldgp->ldvp->ldv_intr_handler;
59863859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
59876512Ssowmini 			    "nxge_add_intrs_adv_type: "
59886512Ssowmini 			    "arg1 0x%x arg2 0x%x: "
59896512Ssowmini 			    "1-1 int handler (entry %d intdata 0x%x)\n",
59906512Ssowmini 			    arg1, arg2,
59916512Ssowmini 			    x, ldgp->intdata));
59923859Sml29623 		} else if (ldgp->nldvs > 1) {
59933859Sml29623 			inthandler = (uint_t *)ldgp->sys_intr_handler;
59943859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
59956512Ssowmini 			    "nxge_add_intrs_adv_type: "
59966512Ssowmini 			    "arg1 0x%x arg2 0x%x: "
59976512Ssowmini 			    "nldevs %d int handler "
59986512Ssowmini 			    "(entry %d intdata 0x%x)\n",
59996512Ssowmini 			    arg1, arg2,
60006512Ssowmini 			    ldgp->nldvs, x, ldgp->intdata));
60013859Sml29623 		}
60023859Sml29623 
60033859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
60046512Ssowmini 		    "==> nxge_add_intrs_adv_type: ddi_add_intr(inum) #%d "
60056512Ssowmini 		    "htable 0x%llx", x, intrp->htable[x]));
60063859Sml29623 
60073859Sml29623 		if ((ddi_status = ddi_intr_add_handler(intrp->htable[x],
60086512Ssowmini 		    (ddi_intr_handler_t *)inthandler, arg1, arg2))
60096512Ssowmini 		    != DDI_SUCCESS) {
60103859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
60116512Ssowmini 			    "==> nxge_add_intrs_adv_type: failed #%d "
60126512Ssowmini 			    "status 0x%x", x, ddi_status));
60133859Sml29623 			for (y = 0; y < intrp->intr_added; y++) {
60143859Sml29623 				(void) ddi_intr_remove_handler(
60156512Ssowmini 				    intrp->htable[y]);
60163859Sml29623 			}
60173859Sml29623 			/* Free already allocated intr */
60183859Sml29623 			for (y = 0; y < nactual; y++) {
60193859Sml29623 				(void) ddi_intr_free(intrp->htable[y]);
60203859Sml29623 			}
60213859Sml29623 			kmem_free(intrp->htable, intrp->intr_size);
60223859Sml29623 
60233859Sml29623 			(void) nxge_ldgv_uninit(nxgep);
60243859Sml29623 
60253859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
60263859Sml29623 		}
60273859Sml29623 		intrp->intr_added++;
60283859Sml29623 	}
60293859Sml29623 
60303859Sml29623 	intrp->msi_intx_cnt = nactual;
60313859Sml29623 
60323859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
60336512Ssowmini 	    "Requested: %d, Allowed: %d msi_intx_cnt %d intr_added %d",
60346512Ssowmini 	    navail, nactual,
60356512Ssowmini 	    intrp->msi_intx_cnt,
60366512Ssowmini 	    intrp->intr_added));
60373859Sml29623 
60383859Sml29623 	(void) ddi_intr_get_cap(intrp->htable[0], &intrp->intr_cap);
60393859Sml29623 
60403859Sml29623 	(void) nxge_intr_ldgv_init(nxgep);
60413859Sml29623 
60423859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_add_intrs_adv_type"));
60433859Sml29623 
60443859Sml29623 	return (status);
60453859Sml29623 }
60463859Sml29623 
60473859Sml29623 /*ARGSUSED*/
60483859Sml29623 static nxge_status_t
60493859Sml29623 nxge_add_intrs_adv_type_fix(p_nxge_t nxgep, uint32_t int_type)
60503859Sml29623 {
60513859Sml29623 	dev_info_t		*dip = nxgep->dip;
60523859Sml29623 	p_nxge_ldg_t		ldgp;
60533859Sml29623 	p_nxge_intr_t		intrp;
60543859Sml29623 	uint_t			*inthandler;
60553859Sml29623 	void			*arg1, *arg2;
60563859Sml29623 	int			behavior;
60573859Sml29623 	int			nintrs, navail;
60583859Sml29623 	int			nactual, nrequired;
60593859Sml29623 	int			inum = 0;
60603859Sml29623 	int			x, y;
60613859Sml29623 	int			ddi_status = DDI_SUCCESS;
60623859Sml29623 	nxge_status_t		status = NXGE_OK;
60633859Sml29623 
60643859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs_adv_type_fix"));
60653859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
60663859Sml29623 	intrp->start_inum = 0;
60673859Sml29623 
60683859Sml29623 	ddi_status = ddi_intr_get_nintrs(dip, int_type, &nintrs);
60693859Sml29623 	if ((ddi_status != DDI_SUCCESS) || (nintrs == 0)) {
60703859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
60716512Ssowmini 		    "ddi_intr_get_nintrs() failed, status: 0x%x%, "
60726512Ssowmini 		    "nintrs: %d", status, nintrs));
60733859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
60743859Sml29623 	}
60753859Sml29623 
60763859Sml29623 	ddi_status = ddi_intr_get_navail(dip, int_type, &navail);
60773859Sml29623 	if ((ddi_status != DDI_SUCCESS) || (navail == 0)) {
60783859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
60796512Ssowmini 		    "ddi_intr_get_navail() failed, status: 0x%x%, "
60806512Ssowmini 		    "nintrs: %d", ddi_status, navail));
60813859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
60823859Sml29623 	}
60833859Sml29623 
60843859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL,
60856512Ssowmini 	    "ddi_intr_get_navail() returned: nintrs %d, naavail %d",
60866512Ssowmini 	    nintrs, navail));
60873859Sml29623 
60883859Sml29623 	behavior = ((int_type == DDI_INTR_TYPE_FIXED) ? DDI_INTR_ALLOC_STRICT :
60896512Ssowmini 	    DDI_INTR_ALLOC_NORMAL);
60903859Sml29623 	intrp->intr_size = navail * sizeof (ddi_intr_handle_t);
60913859Sml29623 	intrp->htable = kmem_alloc(intrp->intr_size, KM_SLEEP);
60923859Sml29623 	ddi_status = ddi_intr_alloc(dip, intrp->htable, int_type, inum,
60936512Ssowmini 	    navail, &nactual, behavior);
60943859Sml29623 	if (ddi_status != DDI_SUCCESS || nactual == 0) {
60953859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
60966512Ssowmini 		    " ddi_intr_alloc() failed: %d",
60976512Ssowmini 		    ddi_status));
60983859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
60993859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
61003859Sml29623 	}
61013859Sml29623 
61023859Sml29623 	if ((ddi_status = ddi_intr_get_pri(intrp->htable[0],
61036512Ssowmini 	    (uint_t *)&intrp->pri)) != DDI_SUCCESS) {
61043859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
61056512Ssowmini 		    " ddi_intr_get_pri() failed: %d",
61066512Ssowmini 		    ddi_status));
61073859Sml29623 		/* Free already allocated interrupts */
61083859Sml29623 		for (y = 0; y < nactual; y++) {
61093859Sml29623 			(void) ddi_intr_free(intrp->htable[y]);
61103859Sml29623 		}
61113859Sml29623 
61123859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
61133859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
61143859Sml29623 	}
61153859Sml29623 
61163859Sml29623 	nrequired = 0;
61173859Sml29623 	switch (nxgep->niu_type) {
61183859Sml29623 	default:
61193859Sml29623 		status = nxge_ldgv_init(nxgep, &nactual, &nrequired);
61203859Sml29623 		break;
61213859Sml29623 
61223859Sml29623 	case N2_NIU:
61233859Sml29623 		status = nxge_ldgv_init_n2(nxgep, &nactual, &nrequired);
61243859Sml29623 		break;
61253859Sml29623 	}
61263859Sml29623 
61273859Sml29623 	if (status != NXGE_OK) {
61283859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
61296512Ssowmini 		    "nxge_add_intrs_adv_type_fix:nxge_ldgv_init "
61306512Ssowmini 		    "failed: 0x%x", status));
61313859Sml29623 		/* Free already allocated interrupts */
61323859Sml29623 		for (y = 0; y < nactual; y++) {
61333859Sml29623 			(void) ddi_intr_free(intrp->htable[y]);
61343859Sml29623 		}
61353859Sml29623 
61363859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
61373859Sml29623 		return (status);
61383859Sml29623 	}
61393859Sml29623 
61403859Sml29623 	ldgp = nxgep->ldgvp->ldgp;
61413859Sml29623 	for (x = 0; x < nrequired; x++, ldgp++) {
61423859Sml29623 		ldgp->vector = (uint8_t)x;
61433859Sml29623 		if (nxgep->niu_type != N2_NIU) {
61443859Sml29623 			ldgp->intdata = SID_DATA(ldgp->func, x);
61453859Sml29623 		}
61463859Sml29623 
61473859Sml29623 		arg1 = ldgp->ldvp;
61483859Sml29623 		arg2 = nxgep;
61493859Sml29623 		if (ldgp->nldvs == 1) {
61503859Sml29623 			inthandler = (uint_t *)ldgp->ldvp->ldv_intr_handler;
61513859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
61526512Ssowmini 			    "nxge_add_intrs_adv_type_fix: "
61536512Ssowmini 			    "1-1 int handler(%d) ldg %d ldv %d "
61546512Ssowmini 			    "arg1 $%p arg2 $%p\n",
61556512Ssowmini 			    x, ldgp->ldg, ldgp->ldvp->ldv,
61566512Ssowmini 			    arg1, arg2));
61573859Sml29623 		} else if (ldgp->nldvs > 1) {
61583859Sml29623 			inthandler = (uint_t *)ldgp->sys_intr_handler;
61593859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
61606512Ssowmini 			    "nxge_add_intrs_adv_type_fix: "
61616512Ssowmini 			    "shared ldv %d int handler(%d) ldv %d ldg %d"
61626512Ssowmini 			    "arg1 0x%016llx arg2 0x%016llx\n",
61636512Ssowmini 			    x, ldgp->nldvs, ldgp->ldg, ldgp->ldvp->ldv,
61646512Ssowmini 			    arg1, arg2));
61653859Sml29623 		}
61663859Sml29623 
61673859Sml29623 		if ((ddi_status = ddi_intr_add_handler(intrp->htable[x],
61686512Ssowmini 		    (ddi_intr_handler_t *)inthandler, arg1, arg2))
61696512Ssowmini 		    != DDI_SUCCESS) {
61703859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
61716512Ssowmini 			    "==> nxge_add_intrs_adv_type_fix: failed #%d "
61726512Ssowmini 			    "status 0x%x", x, ddi_status));
61733859Sml29623 			for (y = 0; y < intrp->intr_added; y++) {
61743859Sml29623 				(void) ddi_intr_remove_handler(
61756512Ssowmini 				    intrp->htable[y]);
61763859Sml29623 			}
61773859Sml29623 			for (y = 0; y < nactual; y++) {
61783859Sml29623 				(void) ddi_intr_free(intrp->htable[y]);
61793859Sml29623 			}
61803859Sml29623 			/* Free already allocated intr */
61813859Sml29623 			kmem_free(intrp->htable, intrp->intr_size);
61823859Sml29623 
61833859Sml29623 			(void) nxge_ldgv_uninit(nxgep);
61843859Sml29623 
61853859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
61863859Sml29623 		}
61873859Sml29623 		intrp->intr_added++;
61883859Sml29623 	}
61893859Sml29623 
61903859Sml29623 	intrp->msi_intx_cnt = nactual;
61913859Sml29623 
61923859Sml29623 	(void) ddi_intr_get_cap(intrp->htable[0], &intrp->intr_cap);
61933859Sml29623 
61943859Sml29623 	status = nxge_intr_ldgv_init(nxgep);
61953859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_add_intrs_adv_type_fix"));
61963859Sml29623 
61973859Sml29623 	return (status);
61983859Sml29623 }
61993859Sml29623 
62003859Sml29623 static void
62013859Sml29623 nxge_remove_intrs(p_nxge_t nxgep)
62023859Sml29623 {
62033859Sml29623 	int		i, inum;
62043859Sml29623 	p_nxge_intr_t	intrp;
62053859Sml29623 
62063859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_remove_intrs"));
62073859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
62083859Sml29623 	if (!intrp->intr_registered) {
62093859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
62106512Ssowmini 		    "<== nxge_remove_intrs: interrupts not registered"));
62113859Sml29623 		return;
62123859Sml29623 	}
62133859Sml29623 
62143859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_remove_intrs:advanced"));
62153859Sml29623 
62163859Sml29623 	if (intrp->intr_cap & DDI_INTR_FLAG_BLOCK) {
62173859Sml29623 		(void) ddi_intr_block_disable(intrp->htable,
62186512Ssowmini 		    intrp->intr_added);
62193859Sml29623 	} else {
62203859Sml29623 		for (i = 0; i < intrp->intr_added; i++) {
62213859Sml29623 			(void) ddi_intr_disable(intrp->htable[i]);
62223859Sml29623 		}
62233859Sml29623 	}
62243859Sml29623 
62253859Sml29623 	for (inum = 0; inum < intrp->intr_added; inum++) {
62263859Sml29623 		if (intrp->htable[inum]) {
62273859Sml29623 			(void) ddi_intr_remove_handler(intrp->htable[inum]);
62283859Sml29623 		}
62293859Sml29623 	}
62303859Sml29623 
62313859Sml29623 	for (inum = 0; inum < intrp->msi_intx_cnt; inum++) {
62323859Sml29623 		if (intrp->htable[inum]) {
62333859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
62346512Ssowmini 			    "nxge_remove_intrs: ddi_intr_free inum %d "
62356512Ssowmini 			    "msi_intx_cnt %d intr_added %d",
62366512Ssowmini 			    inum,
62376512Ssowmini 			    intrp->msi_intx_cnt,
62386512Ssowmini 			    intrp->intr_added));
62393859Sml29623 
62403859Sml29623 			(void) ddi_intr_free(intrp->htable[inum]);
62413859Sml29623 		}
62423859Sml29623 	}
62433859Sml29623 
62443859Sml29623 	kmem_free(intrp->htable, intrp->intr_size);
62453859Sml29623 	intrp->intr_registered = B_FALSE;
62463859Sml29623 	intrp->intr_enabled = B_FALSE;
62473859Sml29623 	intrp->msi_intx_cnt = 0;
62483859Sml29623 	intrp->intr_added = 0;
62493859Sml29623 
62503859Sml29623 	(void) nxge_ldgv_uninit(nxgep);
62513859Sml29623 
62525013Sml29623 	(void) ddi_prop_remove(DDI_DEV_T_NONE, nxgep->dip,
62535013Sml29623 	    "#msix-request");
62545013Sml29623 
62553859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_remove_intrs"));
62563859Sml29623 }
62573859Sml29623 
62583859Sml29623 /*ARGSUSED*/
62593859Sml29623 static void
62603859Sml29623 nxge_remove_soft_intrs(p_nxge_t nxgep)
62613859Sml29623 {
62623859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_remove_soft_intrs"));
62633859Sml29623 	if (nxgep->resched_id) {
62643859Sml29623 		ddi_remove_softintr(nxgep->resched_id);
62653859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
62666512Ssowmini 		    "==> nxge_remove_soft_intrs: removed"));
62673859Sml29623 		nxgep->resched_id = NULL;
62683859Sml29623 	}
62693859Sml29623 
62703859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_remove_soft_intrs"));
62713859Sml29623 }
62723859Sml29623 
62733859Sml29623 /*ARGSUSED*/
62743859Sml29623 static void
62753859Sml29623 nxge_intrs_enable(p_nxge_t nxgep)
62763859Sml29623 {
62773859Sml29623 	p_nxge_intr_t	intrp;
62783859Sml29623 	int		i;
62793859Sml29623 	int		status;
62803859Sml29623 
62813859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_intrs_enable"));
62823859Sml29623 
62833859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
62843859Sml29623 
62853859Sml29623 	if (!intrp->intr_registered) {
62863859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "<== nxge_intrs_enable: "
62876512Ssowmini 		    "interrupts are not registered"));
62883859Sml29623 		return;
62893859Sml29623 	}
62903859Sml29623 
62913859Sml29623 	if (intrp->intr_enabled) {
62923859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
62936512Ssowmini 		    "<== nxge_intrs_enable: already enabled"));
62943859Sml29623 		return;
62953859Sml29623 	}
62963859Sml29623 
62973859Sml29623 	if (intrp->intr_cap & DDI_INTR_FLAG_BLOCK) {
62983859Sml29623 		status = ddi_intr_block_enable(intrp->htable,
62996512Ssowmini 		    intrp->intr_added);
63003859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_intrs_enable "
63016512Ssowmini 		    "block enable - status 0x%x total inums #%d\n",
63026512Ssowmini 		    status, intrp->intr_added));
63033859Sml29623 	} else {
63043859Sml29623 		for (i = 0; i < intrp->intr_added; i++) {
63053859Sml29623 			status = ddi_intr_enable(intrp->htable[i]);
63063859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_intrs_enable "
63076512Ssowmini 			    "ddi_intr_enable:enable - status 0x%x "
63086512Ssowmini 			    "total inums %d enable inum #%d\n",
63096512Ssowmini 			    status, intrp->intr_added, i));
63103859Sml29623 			if (status == DDI_SUCCESS) {
63113859Sml29623 				intrp->intr_enabled = B_TRUE;
63123859Sml29623 			}
63133859Sml29623 		}
63143859Sml29623 	}
63153859Sml29623 
63163859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_intrs_enable"));
63173859Sml29623 }
63183859Sml29623 
63193859Sml29623 /*ARGSUSED*/
63203859Sml29623 static void
63213859Sml29623 nxge_intrs_disable(p_nxge_t nxgep)
63223859Sml29623 {
63233859Sml29623 	p_nxge_intr_t	intrp;
63243859Sml29623 	int		i;
63253859Sml29623 
63263859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_intrs_disable"));
63273859Sml29623 
63283859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
63293859Sml29623 
63303859Sml29623 	if (!intrp->intr_registered) {
63313859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_intrs_disable: "
63326512Ssowmini 		    "interrupts are not registered"));
63333859Sml29623 		return;
63343859Sml29623 	}
63353859Sml29623 
63363859Sml29623 	if (intrp->intr_cap & DDI_INTR_FLAG_BLOCK) {
63373859Sml29623 		(void) ddi_intr_block_disable(intrp->htable,
63386512Ssowmini 		    intrp->intr_added);
63393859Sml29623 	} else {
63403859Sml29623 		for (i = 0; i < intrp->intr_added; i++) {
63413859Sml29623 			(void) ddi_intr_disable(intrp->htable[i]);
63423859Sml29623 		}
63433859Sml29623 	}
63443859Sml29623 
63453859Sml29623 	intrp->intr_enabled = B_FALSE;
63463859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_intrs_disable"));
63473859Sml29623 }
63483859Sml29623 
63493859Sml29623 static nxge_status_t
63503859Sml29623 nxge_mac_register(p_nxge_t nxgep)
63513859Sml29623 {
63523859Sml29623 	mac_register_t *macp;
63533859Sml29623 	int		status;
63543859Sml29623 
63553859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_mac_register"));
63563859Sml29623 
63573859Sml29623 	if ((macp = mac_alloc(MAC_VERSION)) == NULL)
63583859Sml29623 		return (NXGE_ERROR);
63593859Sml29623 
63603859Sml29623 	macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
63613859Sml29623 	macp->m_driver = nxgep;
63623859Sml29623 	macp->m_dip = nxgep->dip;
63633859Sml29623 	macp->m_src_addr = nxgep->ouraddr.ether_addr_octet;
63643859Sml29623 	macp->m_callbacks = &nxge_m_callbacks;
63653859Sml29623 	macp->m_min_sdu = 0;
63666439Sml29623 	nxgep->mac.default_mtu = nxgep->mac.maxframesize -
63676439Sml29623 	    NXGE_EHEADER_VLAN_CRC;
63686439Sml29623 	macp->m_max_sdu = nxgep->mac.default_mtu;
63695895Syz147064 	macp->m_margin = VLAN_TAGSZ;
63706512Ssowmini 	macp->m_priv_props = nxge_priv_props;
63716512Ssowmini 	macp->m_priv_prop_count = NXGE_MAX_PRIV_PROPS;
63723859Sml29623 
63736439Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL,
63746439Sml29623 	    "==> nxge_mac_register: instance %d "
63756439Sml29623 	    "max_sdu %d margin %d maxframe %d (header %d)",
63766439Sml29623 	    nxgep->instance,
63776439Sml29623 	    macp->m_max_sdu, macp->m_margin,
63786439Sml29623 	    nxgep->mac.maxframesize,
63796439Sml29623 	    NXGE_EHEADER_VLAN_CRC));
63806439Sml29623 
63813859Sml29623 	status = mac_register(macp, &nxgep->mach);
63823859Sml29623 	mac_free(macp);
63833859Sml29623 
63843859Sml29623 	if (status != 0) {
63853859Sml29623 		cmn_err(CE_WARN,
63866512Ssowmini 		    "!nxge_mac_register failed (status %d instance %d)",
63876512Ssowmini 		    status, nxgep->instance);
63883859Sml29623 		return (NXGE_ERROR);
63893859Sml29623 	}
63903859Sml29623 
63913859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_mac_register success "
63926512Ssowmini 	    "(instance %d)", nxgep->instance));
63933859Sml29623 
63943859Sml29623 	return (NXGE_OK);
63953859Sml29623 }
63963859Sml29623 
63973859Sml29623 void
63983859Sml29623 nxge_err_inject(p_nxge_t nxgep, queue_t *wq, mblk_t *mp)
63993859Sml29623 {
64003859Sml29623 	ssize_t		size;
64013859Sml29623 	mblk_t		*nmp;
64023859Sml29623 	uint8_t		blk_id;
64033859Sml29623 	uint8_t		chan;
64043859Sml29623 	uint32_t	err_id;
64053859Sml29623 	err_inject_t	*eip;
64063859Sml29623 
64073859Sml29623 	NXGE_DEBUG_MSG((nxgep, STR_CTL, "==> nxge_err_inject"));
64083859Sml29623 
64093859Sml29623 	size = 1024;
64103859Sml29623 	nmp = mp->b_cont;
64113859Sml29623 	eip = (err_inject_t *)nmp->b_rptr;
64123859Sml29623 	blk_id = eip->blk_id;
64133859Sml29623 	err_id = eip->err_id;
64143859Sml29623 	chan = eip->chan;
64153859Sml29623 	cmn_err(CE_NOTE, "!blk_id = 0x%x\n", blk_id);
64163859Sml29623 	cmn_err(CE_NOTE, "!err_id = 0x%x\n", err_id);
64173859Sml29623 	cmn_err(CE_NOTE, "!chan = 0x%x\n", chan);
64183859Sml29623 	switch (blk_id) {
64193859Sml29623 	case MAC_BLK_ID:
64203859Sml29623 		break;
64213859Sml29623 	case TXMAC_BLK_ID:
64223859Sml29623 		break;
64233859Sml29623 	case RXMAC_BLK_ID:
64243859Sml29623 		break;
64253859Sml29623 	case MIF_BLK_ID:
64263859Sml29623 		break;
64273859Sml29623 	case IPP_BLK_ID:
64283859Sml29623 		nxge_ipp_inject_err(nxgep, err_id);
64293859Sml29623 		break;
64303859Sml29623 	case TXC_BLK_ID:
64313859Sml29623 		nxge_txc_inject_err(nxgep, err_id);
64323859Sml29623 		break;
64333859Sml29623 	case TXDMA_BLK_ID:
64343859Sml29623 		nxge_txdma_inject_err(nxgep, err_id, chan);
64353859Sml29623 		break;
64363859Sml29623 	case RXDMA_BLK_ID:
64373859Sml29623 		nxge_rxdma_inject_err(nxgep, err_id, chan);
64383859Sml29623 		break;
64393859Sml29623 	case ZCP_BLK_ID:
64403859Sml29623 		nxge_zcp_inject_err(nxgep, err_id);
64413859Sml29623 		break;
64423859Sml29623 	case ESPC_BLK_ID:
64433859Sml29623 		break;
64443859Sml29623 	case FFLP_BLK_ID:
64453859Sml29623 		break;
64463859Sml29623 	case PHY_BLK_ID:
64473859Sml29623 		break;
64483859Sml29623 	case ETHER_SERDES_BLK_ID:
64493859Sml29623 		break;
64503859Sml29623 	case PCIE_SERDES_BLK_ID:
64513859Sml29623 		break;
64523859Sml29623 	case VIR_BLK_ID:
64533859Sml29623 		break;
64543859Sml29623 	}
64553859Sml29623 
64563859Sml29623 	nmp->b_wptr = nmp->b_rptr + size;
64573859Sml29623 	NXGE_DEBUG_MSG((nxgep, STR_CTL, "<== nxge_err_inject"));
64583859Sml29623 
64593859Sml29623 	miocack(wq, mp, (int)size, 0);
64603859Sml29623 }
64613859Sml29623 
64623859Sml29623 static int
64633859Sml29623 nxge_init_common_dev(p_nxge_t nxgep)
64643859Sml29623 {
64653859Sml29623 	p_nxge_hw_list_t	hw_p;
64663859Sml29623 	dev_info_t 		*p_dip;
64673859Sml29623 
64683859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "==> nxge_init_common_device"));
64693859Sml29623 
64703859Sml29623 	p_dip = nxgep->p_dip;
64713859Sml29623 	MUTEX_ENTER(&nxge_common_lock);
64723859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL,
64736512Ssowmini 	    "==> nxge_init_common_dev:func # %d",
64746512Ssowmini 	    nxgep->function_num));
64753859Sml29623 	/*
64763859Sml29623 	 * Loop through existing per neptune hardware list.
64773859Sml29623 	 */
64783859Sml29623 	for (hw_p = nxge_hw_list; hw_p; hw_p = hw_p->next) {
64793859Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
64806512Ssowmini 		    "==> nxge_init_common_device:func # %d "
64816512Ssowmini 		    "hw_p $%p parent dip $%p",
64826512Ssowmini 		    nxgep->function_num,
64836512Ssowmini 		    hw_p,
64846512Ssowmini 		    p_dip));
64853859Sml29623 		if (hw_p->parent_devp == p_dip) {
64863859Sml29623 			nxgep->nxge_hw_p = hw_p;
64873859Sml29623 			hw_p->ndevs++;
64883859Sml29623 			hw_p->nxge_p[nxgep->function_num] = nxgep;
64893859Sml29623 			NXGE_DEBUG_MSG((nxgep, MOD_CTL,
64906512Ssowmini 			    "==> nxge_init_common_device:func # %d "
64916512Ssowmini 			    "hw_p $%p parent dip $%p "
64926512Ssowmini 			    "ndevs %d (found)",
64936512Ssowmini 			    nxgep->function_num,
64946512Ssowmini 			    hw_p,
64956512Ssowmini 			    p_dip,
64966512Ssowmini 			    hw_p->ndevs));
64973859Sml29623 			break;
64983859Sml29623 		}
64993859Sml29623 	}
65003859Sml29623 
65013859Sml29623 	if (hw_p == NULL) {
65027801SSantwona.Behera@Sun.COM 
65037801SSantwona.Behera@Sun.COM 		char **prop_val;
65047801SSantwona.Behera@Sun.COM 		uint_t prop_len;
65057801SSantwona.Behera@Sun.COM 		int i;
65067801SSantwona.Behera@Sun.COM 
65073859Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
65086512Ssowmini 		    "==> nxge_init_common_device:func # %d "
65096512Ssowmini 		    "parent dip $%p (new)",
65106512Ssowmini 		    nxgep->function_num,
65116512Ssowmini 		    p_dip));
65123859Sml29623 		hw_p = kmem_zalloc(sizeof (nxge_hw_list_t), KM_SLEEP);
65133859Sml29623 		hw_p->parent_devp = p_dip;
65143859Sml29623 		hw_p->magic = NXGE_NEPTUNE_MAGIC;
65153859Sml29623 		nxgep->nxge_hw_p = hw_p;
65163859Sml29623 		hw_p->ndevs++;
65173859Sml29623 		hw_p->nxge_p[nxgep->function_num] = nxgep;
65183859Sml29623 		hw_p->next = nxge_hw_list;
65194732Sdavemq 		if (nxgep->niu_type == N2_NIU) {
65204732Sdavemq 			hw_p->niu_type = N2_NIU;
65214732Sdavemq 			hw_p->platform_type = P_NEPTUNE_NIU;
65224732Sdavemq 		} else {
65234732Sdavemq 			hw_p->niu_type = NIU_TYPE_NONE;
65244977Sraghus 			hw_p->platform_type = P_NEPTUNE_NONE;
65254732Sdavemq 		}
65263859Sml29623 
65273859Sml29623 		MUTEX_INIT(&hw_p->nxge_cfg_lock, NULL, MUTEX_DRIVER, NULL);
65283859Sml29623 		MUTEX_INIT(&hw_p->nxge_tcam_lock, NULL, MUTEX_DRIVER, NULL);
65293859Sml29623 		MUTEX_INIT(&hw_p->nxge_vlan_lock, NULL, MUTEX_DRIVER, NULL);
65303859Sml29623 		MUTEX_INIT(&hw_p->nxge_mdio_lock, NULL, MUTEX_DRIVER, NULL);
65313859Sml29623 
65323859Sml29623 		nxge_hw_list = hw_p;
65334732Sdavemq 
65347801SSantwona.Behera@Sun.COM 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, nxgep->dip, 0,
65357801SSantwona.Behera@Sun.COM 		    "compatible", &prop_val, &prop_len) == DDI_PROP_SUCCESS) {
65367801SSantwona.Behera@Sun.COM 			for (i = 0; i < prop_len; i++) {
65377801SSantwona.Behera@Sun.COM 				if ((strcmp((caddr_t)prop_val[i],
65387801SSantwona.Behera@Sun.COM 				    NXGE_ROCK_COMPATIBLE) == 0)) {
65397801SSantwona.Behera@Sun.COM 					hw_p->platform_type = P_NEPTUNE_ROCK;
65407801SSantwona.Behera@Sun.COM 					NXGE_DEBUG_MSG((nxgep, MOD_CTL,
65417801SSantwona.Behera@Sun.COM 					    "ROCK hw_p->platform_type %d",
65427801SSantwona.Behera@Sun.COM 					    hw_p->platform_type));
65437801SSantwona.Behera@Sun.COM 					break;
65447801SSantwona.Behera@Sun.COM 				}
65457801SSantwona.Behera@Sun.COM 				NXGE_DEBUG_MSG((nxgep, MOD_CTL,
65467801SSantwona.Behera@Sun.COM 				    "nxge_init_common_dev: read compatible"
65477801SSantwona.Behera@Sun.COM 				    " property[%d] val[%s]",
65487801SSantwona.Behera@Sun.COM 				    i, (caddr_t)prop_val[i]));
65497801SSantwona.Behera@Sun.COM 			}
65507801SSantwona.Behera@Sun.COM 		}
65517801SSantwona.Behera@Sun.COM 
65527801SSantwona.Behera@Sun.COM 		ddi_prop_free(prop_val);
65537801SSantwona.Behera@Sun.COM 
65544732Sdavemq 		(void) nxge_scan_ports_phy(nxgep, nxge_hw_list);
65553859Sml29623 	}
65563859Sml29623 
65573859Sml29623 	MUTEX_EXIT(&nxge_common_lock);
65584732Sdavemq 
65594977Sraghus 	nxgep->platform_type = hw_p->platform_type;
65607801SSantwona.Behera@Sun.COM 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "nxgep->platform_type %d",
65617801SSantwona.Behera@Sun.COM 	    nxgep->platform_type));
65624732Sdavemq 	if (nxgep->niu_type != N2_NIU) {
65634732Sdavemq 		nxgep->niu_type = hw_p->niu_type;
65644732Sdavemq 	}
65654732Sdavemq 
65663859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL,
65676512Ssowmini 	    "==> nxge_init_common_device (nxge_hw_list) $%p",
65686512Ssowmini 	    nxge_hw_list));
65693859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "<== nxge_init_common_device"));
65703859Sml29623 
65713859Sml29623 	return (NXGE_OK);
65723859Sml29623 }
65733859Sml29623 
65743859Sml29623 static void
65753859Sml29623 nxge_uninit_common_dev(p_nxge_t nxgep)
65763859Sml29623 {
65773859Sml29623 	p_nxge_hw_list_t	hw_p, h_hw_p;
65786801Sspeer 	p_nxge_dma_pt_cfg_t	p_dma_cfgp;
65796801Sspeer 	p_nxge_hw_pt_cfg_t	p_cfgp;
65803859Sml29623 	dev_info_t 		*p_dip;
65813859Sml29623 
65823859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "==> nxge_uninit_common_device"));
65833859Sml29623 	if (nxgep->nxge_hw_p == NULL) {
65843859Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
65856512Ssowmini 		    "<== nxge_uninit_common_device (no common)"));
65863859Sml29623 		return;
65873859Sml29623 	}
65883859Sml29623 
65893859Sml29623 	MUTEX_ENTER(&nxge_common_lock);
65903859Sml29623 	h_hw_p = nxge_hw_list;
65913859Sml29623 	for (hw_p = nxge_hw_list; hw_p; hw_p = hw_p->next) {
65923859Sml29623 		p_dip = hw_p->parent_devp;
65933859Sml29623 		if (nxgep->nxge_hw_p == hw_p &&
65946512Ssowmini 		    p_dip == nxgep->p_dip &&
65956512Ssowmini 		    nxgep->nxge_hw_p->magic == NXGE_NEPTUNE_MAGIC &&
65966512Ssowmini 		    hw_p->magic == NXGE_NEPTUNE_MAGIC) {
65973859Sml29623 
65983859Sml29623 			NXGE_DEBUG_MSG((nxgep, MOD_CTL,
65996512Ssowmini 			    "==> nxge_uninit_common_device:func # %d "
66006512Ssowmini 			    "hw_p $%p parent dip $%p "
66016512Ssowmini 			    "ndevs %d (found)",
66026512Ssowmini 			    nxgep->function_num,
66036512Ssowmini 			    hw_p,
66046512Ssowmini 			    p_dip,
66056512Ssowmini 			    hw_p->ndevs));
66063859Sml29623 
66076801Sspeer 			/*
66086801Sspeer 			 * Release the RDC table, a shared resoruce
66096801Sspeer 			 * of the nxge hardware.  The RDC table was
66106801Sspeer 			 * assigned to this instance of nxge in
66116801Sspeer 			 * nxge_use_cfg_dma_config().
66126801Sspeer 			 */
66137587SMichael.Speer@Sun.COM 			if (!isLDOMguest(nxgep)) {
66147587SMichael.Speer@Sun.COM 				p_dma_cfgp =
66157587SMichael.Speer@Sun.COM 				    (p_nxge_dma_pt_cfg_t)&nxgep->pt_config;
66167587SMichael.Speer@Sun.COM 				p_cfgp =
66177587SMichael.Speer@Sun.COM 				    (p_nxge_hw_pt_cfg_t)&p_dma_cfgp->hw_config;
66187587SMichael.Speer@Sun.COM 				(void) nxge_fzc_rdc_tbl_unbind(nxgep,
66197587SMichael.Speer@Sun.COM 				    p_cfgp->def_mac_rxdma_grpid);
66207766SMichael.Speer@Sun.COM 
66217766SMichael.Speer@Sun.COM 				/* Cleanup any outstanding groups.  */
66227766SMichael.Speer@Sun.COM 				nxge_grp_cleanup(nxgep);
66237587SMichael.Speer@Sun.COM 			}
66246801Sspeer 
66253859Sml29623 			if (hw_p->ndevs) {
66263859Sml29623 				hw_p->ndevs--;
66273859Sml29623 			}
66283859Sml29623 			hw_p->nxge_p[nxgep->function_num] = NULL;
66293859Sml29623 			if (!hw_p->ndevs) {
66303859Sml29623 				MUTEX_DESTROY(&hw_p->nxge_vlan_lock);
66313859Sml29623 				MUTEX_DESTROY(&hw_p->nxge_tcam_lock);
66323859Sml29623 				MUTEX_DESTROY(&hw_p->nxge_cfg_lock);
66333859Sml29623 				MUTEX_DESTROY(&hw_p->nxge_mdio_lock);
66343859Sml29623 				NXGE_DEBUG_MSG((nxgep, MOD_CTL,
66356512Ssowmini 				    "==> nxge_uninit_common_device: "
66366512Ssowmini 				    "func # %d "
66376512Ssowmini 				    "hw_p $%p parent dip $%p "
66386512Ssowmini 				    "ndevs %d (last)",
66396512Ssowmini 				    nxgep->function_num,
66406512Ssowmini 				    hw_p,
66416512Ssowmini 				    p_dip,
66426512Ssowmini 				    hw_p->ndevs));
66433859Sml29623 
66446495Sspeer 				nxge_hio_uninit(nxgep);
66456495Sspeer 
66463859Sml29623 				if (hw_p == nxge_hw_list) {
66473859Sml29623 					NXGE_DEBUG_MSG((nxgep, MOD_CTL,
66486512Ssowmini 					    "==> nxge_uninit_common_device:"
66496512Ssowmini 					    "remove head func # %d "
66506512Ssowmini 					    "hw_p $%p parent dip $%p "
66516512Ssowmini 					    "ndevs %d (head)",
66526512Ssowmini 					    nxgep->function_num,
66536512Ssowmini 					    hw_p,
66546512Ssowmini 					    p_dip,
66556512Ssowmini 					    hw_p->ndevs));
66563859Sml29623 					nxge_hw_list = hw_p->next;
66573859Sml29623 				} else {
66583859Sml29623 					NXGE_DEBUG_MSG((nxgep, MOD_CTL,
66596512Ssowmini 					    "==> nxge_uninit_common_device:"
66606512Ssowmini 					    "remove middle func # %d "
66616512Ssowmini 					    "hw_p $%p parent dip $%p "
66626512Ssowmini 					    "ndevs %d (middle)",
66636512Ssowmini 					    nxgep->function_num,
66646512Ssowmini 					    hw_p,
66656512Ssowmini 					    p_dip,
66666512Ssowmini 					    hw_p->ndevs));
66673859Sml29623 					h_hw_p->next = hw_p->next;
66683859Sml29623 				}
66693859Sml29623 
66706495Sspeer 				nxgep->nxge_hw_p = NULL;
66713859Sml29623 				KMEM_FREE(hw_p, sizeof (nxge_hw_list_t));
66723859Sml29623 			}
66733859Sml29623 			break;
66743859Sml29623 		} else {
66753859Sml29623 			h_hw_p = hw_p;
66763859Sml29623 		}
66773859Sml29623 	}
66783859Sml29623 
66793859Sml29623 	MUTEX_EXIT(&nxge_common_lock);
66803859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL,
66816512Ssowmini 	    "==> nxge_uninit_common_device (nxge_hw_list) $%p",
66826512Ssowmini 	    nxge_hw_list));
66833859Sml29623 
66843859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "<= nxge_uninit_common_device"));
66853859Sml29623 }
66864732Sdavemq 
66874732Sdavemq /*
66884977Sraghus  * Determines the number of ports from the niu_type or the platform type.
66894732Sdavemq  * Returns the number of ports, or returns zero on failure.
66904732Sdavemq  */
66914732Sdavemq 
66924732Sdavemq int
66934977Sraghus nxge_get_nports(p_nxge_t nxgep)
66944732Sdavemq {
66954732Sdavemq 	int	nports = 0;
66964732Sdavemq 
66974977Sraghus 	switch (nxgep->niu_type) {
66984732Sdavemq 	case N2_NIU:
66994732Sdavemq 	case NEPTUNE_2_10GF:
67004732Sdavemq 		nports = 2;
67014732Sdavemq 		break;
67024732Sdavemq 	case NEPTUNE_4_1GC:
67034732Sdavemq 	case NEPTUNE_2_10GF_2_1GC:
67044732Sdavemq 	case NEPTUNE_1_10GF_3_1GC:
67054732Sdavemq 	case NEPTUNE_1_1GC_1_10GF_2_1GC:
67066261Sjoycey 	case NEPTUNE_2_10GF_2_1GRF:
67074732Sdavemq 		nports = 4;
67084732Sdavemq 		break;
67094732Sdavemq 	default:
67104977Sraghus 		switch (nxgep->platform_type) {
67114977Sraghus 		case P_NEPTUNE_NIU:
67124977Sraghus 		case P_NEPTUNE_ATLAS_2PORT:
67134977Sraghus 			nports = 2;
67144977Sraghus 			break;
67154977Sraghus 		case P_NEPTUNE_ATLAS_4PORT:
67164977Sraghus 		case P_NEPTUNE_MARAMBA_P0:
67174977Sraghus 		case P_NEPTUNE_MARAMBA_P1:
67187801SSantwona.Behera@Sun.COM 		case P_NEPTUNE_ROCK:
67195196Ssbehera 		case P_NEPTUNE_ALONSO:
67204977Sraghus 			nports = 4;
67214977Sraghus 			break;
67224977Sraghus 		default:
67234977Sraghus 			break;
67244977Sraghus 		}
67254732Sdavemq 		break;
67264732Sdavemq 	}
67274732Sdavemq 
67284732Sdavemq 	return (nports);
67294732Sdavemq }
67305013Sml29623 
67315013Sml29623 /*
67325013Sml29623  * The following two functions are to support
67335013Sml29623  * PSARC/2007/453 MSI-X interrupt limit override.
67345013Sml29623  */
67355013Sml29623 static int
67365013Sml29623 nxge_create_msi_property(p_nxge_t nxgep)
67375013Sml29623 {
67385013Sml29623 	int	nmsi;
67395013Sml29623 	extern	int ncpus;
67405013Sml29623 
67415013Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "==>nxge_create_msi_property"));
67425013Sml29623 
67435013Sml29623 	switch (nxgep->mac.portmode) {
67445013Sml29623 	case PORT_10G_COPPER:
67455013Sml29623 	case PORT_10G_FIBER:
67466835Syc148097 	case PORT_10G_TN1010:
67475013Sml29623 		(void) ddi_prop_create(DDI_DEV_T_NONE, nxgep->dip,
67485013Sml29623 		    DDI_PROP_CANSLEEP, "#msix-request", NULL, 0);
67495013Sml29623 		/*
67505013Sml29623 		 * The maximum MSI-X requested will be 8.
67515013Sml29623 		 * If the # of CPUs is less than 8, we will reqeust
67525013Sml29623 		 * # MSI-X based on the # of CPUs.
67535013Sml29623 		 */
67545013Sml29623 		if (ncpus >= NXGE_MSIX_REQUEST_10G) {
67555013Sml29623 			nmsi = NXGE_MSIX_REQUEST_10G;
67565013Sml29623 		} else {
67575013Sml29623 			nmsi = ncpus;
67585013Sml29623 		}
67595013Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
67605013Sml29623 		    "==>nxge_create_msi_property(10G): exists 0x%x (nmsi %d)",
67615013Sml29623 		    ddi_prop_exists(DDI_DEV_T_NONE, nxgep->dip,
67625013Sml29623 		    DDI_PROP_CANSLEEP, "#msix-request"), nmsi));
67635013Sml29623 		break;
67645013Sml29623 
67655013Sml29623 	default:
67665013Sml29623 		nmsi = NXGE_MSIX_REQUEST_1G;
67675013Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
67685013Sml29623 		    "==>nxge_create_msi_property(1G): exists 0x%x (nmsi %d)",
67695013Sml29623 		    ddi_prop_exists(DDI_DEV_T_NONE, nxgep->dip,
67705013Sml29623 		    DDI_PROP_CANSLEEP, "#msix-request"), nmsi));
67715013Sml29623 		break;
67725013Sml29623 	}
67735013Sml29623 
67745013Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "<==nxge_create_msi_property"));
67755013Sml29623 	return (nmsi);
67765013Sml29623 }
67776512Ssowmini 
67786512Ssowmini /* ARGSUSED */
67796512Ssowmini static int
67806512Ssowmini nxge_get_def_val(nxge_t *nxgep, mac_prop_id_t pr_num, uint_t pr_valsize,
67816512Ssowmini     void *pr_val)
67826512Ssowmini {
67836512Ssowmini 	int err = 0;
67846512Ssowmini 	link_flowctrl_t fl;
67856512Ssowmini 
67866512Ssowmini 	switch (pr_num) {
67876789Sam223141 	case MAC_PROP_AUTONEG:
67886512Ssowmini 		*(uint8_t *)pr_val = 1;
67896512Ssowmini 		break;
67906789Sam223141 	case MAC_PROP_FLOWCTRL:
67916512Ssowmini 		if (pr_valsize < sizeof (link_flowctrl_t))
67926512Ssowmini 			return (EINVAL);
67936512Ssowmini 		fl = LINK_FLOWCTRL_RX;
67946512Ssowmini 		bcopy(&fl, pr_val, sizeof (fl));
67956512Ssowmini 		break;
67966789Sam223141 	case MAC_PROP_ADV_1000FDX_CAP:
67976789Sam223141 	case MAC_PROP_EN_1000FDX_CAP:
67986512Ssowmini 		*(uint8_t *)pr_val = 1;
67996512Ssowmini 		break;
68006789Sam223141 	case MAC_PROP_ADV_100FDX_CAP:
68016789Sam223141 	case MAC_PROP_EN_100FDX_CAP:
68026512Ssowmini 		*(uint8_t *)pr_val = 1;
68036512Ssowmini 		break;
68046512Ssowmini 	default:
68056512Ssowmini 		err = ENOTSUP;
68066512Ssowmini 		break;
68076512Ssowmini 	}
68086512Ssowmini 	return (err);
68096512Ssowmini }
68106705Sml29623 
68116705Sml29623 
68126705Sml29623 /*
68136705Sml29623  * The following is a software around for the Neptune hardware's
68146705Sml29623  * interrupt bugs; The Neptune hardware may generate spurious interrupts when
68156705Sml29623  * an interrupr handler is removed.
68166705Sml29623  */
68176705Sml29623 #define	NXGE_PCI_PORT_LOGIC_OFFSET	0x98
68186705Sml29623 #define	NXGE_PIM_RESET			(1ULL << 29)
68196705Sml29623 #define	NXGE_GLU_RESET			(1ULL << 30)
68206705Sml29623 #define	NXGE_NIU_RESET			(1ULL << 31)
68216705Sml29623 #define	NXGE_PCI_RESET_ALL		(NXGE_PIM_RESET |	\
68226705Sml29623 					NXGE_GLU_RESET |	\
68236705Sml29623 					NXGE_NIU_RESET)
68246705Sml29623 
68256705Sml29623 #define	NXGE_WAIT_QUITE_TIME		200000
68266705Sml29623 #define	NXGE_WAIT_QUITE_RETRY		40
68276705Sml29623 #define	NXGE_PCI_RESET_WAIT		1000000 /* one second */
68286705Sml29623 
68296705Sml29623 static void
68306705Sml29623 nxge_niu_peu_reset(p_nxge_t nxgep)
68316705Sml29623 {
68326705Sml29623 	uint32_t	rvalue;
68336705Sml29623 	p_nxge_hw_list_t hw_p;
68346705Sml29623 	p_nxge_t	fnxgep;
68356705Sml29623 	int		i, j;
68366705Sml29623 
68376705Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL, "==> nxge_niu_peu_reset"));
68386705Sml29623 	if ((hw_p = nxgep->nxge_hw_p) == NULL) {
68396705Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
68406705Sml29623 		    "==> nxge_niu_peu_reset: NULL hardware pointer"));
68416705Sml29623 		return;
68426705Sml29623 	}
68436705Sml29623 
68446705Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
68456705Sml29623 	    "==> nxge_niu_peu_reset: flags 0x%x link timer id %d timer id %d",
68466705Sml29623 	    hw_p->flags, nxgep->nxge_link_poll_timerid,
68476705Sml29623 	    nxgep->nxge_timerid));
68486705Sml29623 
68496705Sml29623 	MUTEX_ENTER(&hw_p->nxge_cfg_lock);
68506705Sml29623 	/*
68516705Sml29623 	 * Make sure other instances from the same hardware
68526705Sml29623 	 * stop sending PIO and in quiescent state.
68536705Sml29623 	 */
68546705Sml29623 	for (i = 0; i < NXGE_MAX_PORTS; i++) {
68556705Sml29623 		fnxgep = hw_p->nxge_p[i];
68566705Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
68576705Sml29623 		    "==> nxge_niu_peu_reset: checking entry %d "
68586705Sml29623 		    "nxgep $%p", i, fnxgep));
68596705Sml29623 #ifdef	NXGE_DEBUG
68606705Sml29623 		if (fnxgep) {
68616705Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
68626705Sml29623 			    "==> nxge_niu_peu_reset: entry %d (function %d) "
68636705Sml29623 			    "link timer id %d hw timer id %d",
68646705Sml29623 			    i, fnxgep->function_num,
68656705Sml29623 			    fnxgep->nxge_link_poll_timerid,
68666705Sml29623 			    fnxgep->nxge_timerid));
68676705Sml29623 		}
68686705Sml29623 #endif
68696705Sml29623 		if (fnxgep && fnxgep != nxgep &&
68706705Sml29623 		    (fnxgep->nxge_timerid || fnxgep->nxge_link_poll_timerid)) {
68716705Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
68726705Sml29623 			    "==> nxge_niu_peu_reset: checking $%p "
68736705Sml29623 			    "(function %d) timer ids",
68746705Sml29623 			    fnxgep, fnxgep->function_num));
68756705Sml29623 			for (j = 0; j < NXGE_WAIT_QUITE_RETRY; j++) {
68766705Sml29623 				NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
68776705Sml29623 				    "==> nxge_niu_peu_reset: waiting"));
68786705Sml29623 				NXGE_DELAY(NXGE_WAIT_QUITE_TIME);
68796705Sml29623 				if (!fnxgep->nxge_timerid &&
68806705Sml29623 				    !fnxgep->nxge_link_poll_timerid) {
68816705Sml29623 					break;
68826705Sml29623 				}
68836705Sml29623 			}
68846705Sml29623 			NXGE_DELAY(NXGE_WAIT_QUITE_TIME);
68856705Sml29623 			if (fnxgep->nxge_timerid ||
68866705Sml29623 			    fnxgep->nxge_link_poll_timerid) {
68876705Sml29623 				MUTEX_EXIT(&hw_p->nxge_cfg_lock);
68886705Sml29623 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
68896705Sml29623 				    "<== nxge_niu_peu_reset: cannot reset "
68906705Sml29623 				    "hardware (devices are still in use)"));
68916705Sml29623 				return;
68926705Sml29623 			}
68936705Sml29623 		}
68946705Sml29623 	}
68956705Sml29623 
68966705Sml29623 	if ((hw_p->flags & COMMON_RESET_NIU_PCI) != COMMON_RESET_NIU_PCI) {
68976705Sml29623 		hw_p->flags |= COMMON_RESET_NIU_PCI;
68986705Sml29623 		rvalue = pci_config_get32(nxgep->dev_regs->nxge_pciregh,
68996705Sml29623 		    NXGE_PCI_PORT_LOGIC_OFFSET);
69006705Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
69016705Sml29623 		    "nxge_niu_peu_reset: read offset 0x%x (%d) "
69026705Sml29623 		    "(data 0x%x)",
69036705Sml29623 		    NXGE_PCI_PORT_LOGIC_OFFSET,
69046705Sml29623 		    NXGE_PCI_PORT_LOGIC_OFFSET,
69056705Sml29623 		    rvalue));
69066705Sml29623 
69076705Sml29623 		rvalue |= NXGE_PCI_RESET_ALL;
69086705Sml29623 		pci_config_put32(nxgep->dev_regs->nxge_pciregh,
69096705Sml29623 		    NXGE_PCI_PORT_LOGIC_OFFSET, rvalue);
69106705Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
69116705Sml29623 		    "nxge_niu_peu_reset: RESETTING NIU: write NIU reset 0x%x",
69126705Sml29623 		    rvalue));
69136705Sml29623 
69146705Sml29623 		NXGE_DELAY(NXGE_PCI_RESET_WAIT);
69156705Sml29623 	}
69166705Sml29623 
69176705Sml29623 	MUTEX_EXIT(&hw_p->nxge_cfg_lock);
69186705Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL, "<== nxge_niu_peu_reset"));
69196705Sml29623 }
69207126Sml29623 
69217126Sml29623 static void
69227126Sml29623 nxge_set_pci_replay_timeout(p_nxge_t nxgep)
69237126Sml29623 {
69247126Sml29623 	p_dev_regs_t 	dev_regs;
69257126Sml29623 	uint32_t	value;
69267126Sml29623 
69277126Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_set_pci_replay_timeout"));
69287126Sml29623 
69297126Sml29623 	if (!nxge_set_replay_timer) {
69307126Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
69317126Sml29623 		    "==> nxge_set_pci_replay_timeout: will not change "
69327126Sml29623 		    "the timeout"));
69337126Sml29623 		return;
69347126Sml29623 	}
69357126Sml29623 
69367126Sml29623 	dev_regs = nxgep->dev_regs;
69377126Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
69387126Sml29623 	    "==> nxge_set_pci_replay_timeout: dev_regs 0x%p pcireg 0x%p",
69397126Sml29623 	    dev_regs, dev_regs->nxge_pciregh));
69407126Sml29623 
69417126Sml29623 	if (dev_regs == NULL || (dev_regs->nxge_pciregh == NULL)) {
69427145Syc148097 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
69437126Sml29623 		    "==> nxge_set_pci_replay_timeout: NULL dev_regs $%p or "
69447126Sml29623 		    "no PCI handle",
69457126Sml29623 		    dev_regs));
69467126Sml29623 		return;
69477126Sml29623 	}
69487126Sml29623 	value = (pci_config_get32(dev_regs->nxge_pciregh,
69497126Sml29623 	    PCI_REPLAY_TIMEOUT_CFG_OFFSET) |
69507126Sml29623 	    (nxge_replay_timeout << PCI_REPLAY_TIMEOUT_SHIFT));
69517126Sml29623 
69527126Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
69537126Sml29623 	    "nxge_set_pci_replay_timeout: replay timeout value before set 0x%x "
69547126Sml29623 	    "(timeout value to set 0x%x at offset 0x%x) value 0x%x",
69557126Sml29623 	    pci_config_get32(dev_regs->nxge_pciregh,
69567126Sml29623 	    PCI_REPLAY_TIMEOUT_CFG_OFFSET), nxge_replay_timeout,
69577126Sml29623 	    PCI_REPLAY_TIMEOUT_CFG_OFFSET, value));
69587126Sml29623 
69597126Sml29623 	pci_config_put32(dev_regs->nxge_pciregh, PCI_REPLAY_TIMEOUT_CFG_OFFSET,
69607126Sml29623 	    value);
69617126Sml29623 
69627126Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
69637126Sml29623 	    "nxge_set_pci_replay_timeout: replay timeout value after set 0x%x",
69647126Sml29623 	    pci_config_get32(dev_regs->nxge_pciregh,
69657126Sml29623 	    PCI_REPLAY_TIMEOUT_CFG_OFFSET)));
69667126Sml29623 
69677126Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_set_pci_replay_timeout"));
69687126Sml29623 }
69697656SSherry.Moore@Sun.COM 
69707656SSherry.Moore@Sun.COM /*
69717656SSherry.Moore@Sun.COM  * quiesce(9E) entry point.
69727656SSherry.Moore@Sun.COM  *
69737656SSherry.Moore@Sun.COM  * This function is called when the system is single-threaded at high
69747656SSherry.Moore@Sun.COM  * PIL with preemption disabled. Therefore, this function must not be
69757656SSherry.Moore@Sun.COM  * blocked.
69767656SSherry.Moore@Sun.COM  *
69777656SSherry.Moore@Sun.COM  * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
69787656SSherry.Moore@Sun.COM  * DDI_FAILURE indicates an error condition and should almost never happen.
69797656SSherry.Moore@Sun.COM  */
69807656SSherry.Moore@Sun.COM static int
69817656SSherry.Moore@Sun.COM nxge_quiesce(dev_info_t *dip)
69827656SSherry.Moore@Sun.COM {
69837656SSherry.Moore@Sun.COM 	int instance = ddi_get_instance(dip);
69847656SSherry.Moore@Sun.COM 	p_nxge_t nxgep = (p_nxge_t)ddi_get_soft_state(nxge_list, instance);
69857656SSherry.Moore@Sun.COM 
69867656SSherry.Moore@Sun.COM 	if (nxgep == NULL)
69877656SSherry.Moore@Sun.COM 		return (DDI_FAILURE);
69887656SSherry.Moore@Sun.COM 
69897656SSherry.Moore@Sun.COM 	/* Turn off debugging */
69907656SSherry.Moore@Sun.COM 	nxge_debug_level = NO_DEBUG;
69917656SSherry.Moore@Sun.COM 	nxgep->nxge_debug_level = NO_DEBUG;
69927656SSherry.Moore@Sun.COM 	npi_debug_level = NO_DEBUG;
69937656SSherry.Moore@Sun.COM 
69947656SSherry.Moore@Sun.COM 	/*
69957656SSherry.Moore@Sun.COM 	 * Stop link monitor only when linkchkmod is interrupt based
69967656SSherry.Moore@Sun.COM 	 */
69977656SSherry.Moore@Sun.COM 	if (nxgep->mac.linkchkmode == LINKCHK_INTR) {
69987656SSherry.Moore@Sun.COM 		(void) nxge_link_monitor(nxgep, LINK_MONITOR_STOP);
69997656SSherry.Moore@Sun.COM 	}
70007656SSherry.Moore@Sun.COM 
70017656SSherry.Moore@Sun.COM 	(void) nxge_intr_hw_disable(nxgep);
70027656SSherry.Moore@Sun.COM 
70037656SSherry.Moore@Sun.COM 	/*
70047656SSherry.Moore@Sun.COM 	 * Reset the receive MAC side.
70057656SSherry.Moore@Sun.COM 	 */
70067656SSherry.Moore@Sun.COM 	(void) nxge_rx_mac_disable(nxgep);
70077656SSherry.Moore@Sun.COM 
70087656SSherry.Moore@Sun.COM 	/* Disable and soft reset the IPP */
70097656SSherry.Moore@Sun.COM 	if (!isLDOMguest(nxgep))
70107656SSherry.Moore@Sun.COM 		(void) nxge_ipp_disable(nxgep);
70117656SSherry.Moore@Sun.COM 
70127656SSherry.Moore@Sun.COM 	/*
70137656SSherry.Moore@Sun.COM 	 * Reset the transmit/receive DMA side.
70147656SSherry.Moore@Sun.COM 	 */
70157656SSherry.Moore@Sun.COM 	(void) nxge_txdma_hw_mode(nxgep, NXGE_DMA_STOP);
70167656SSherry.Moore@Sun.COM 	(void) nxge_rxdma_hw_mode(nxgep, NXGE_DMA_STOP);
70177656SSherry.Moore@Sun.COM 
70187656SSherry.Moore@Sun.COM 	/*
70197656SSherry.Moore@Sun.COM 	 * Reset the transmit MAC side.
70207656SSherry.Moore@Sun.COM 	 */
70217656SSherry.Moore@Sun.COM 	(void) nxge_tx_mac_disable(nxgep);
70227656SSherry.Moore@Sun.COM 
70237656SSherry.Moore@Sun.COM 	return (DDI_SUCCESS);
70247656SSherry.Moore@Sun.COM }
7025