xref: /onnv-gate/usr/src/uts/common/io/nxge/nxge_main.c (revision 11304:3092d1e303d6)
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  */
219730SMichael.Speer@Sun.COM 
223859Sml29623 /*
238661SSantwona.Behera@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
243859Sml29623  * Use is subject to license terms.
253859Sml29623  */
263859Sml29623 
273859Sml29623 /*
283859Sml29623  * SunOs MT STREAMS NIU/Neptune 10Gb Ethernet Device Driver.
293859Sml29623  */
303859Sml29623 #include	<sys/nxge/nxge_impl.h>
316495Sspeer #include	<sys/nxge/nxge_hio.h>
326495Sspeer #include	<sys/nxge/nxge_rxdma.h>
333859Sml29623 #include	<sys/pcie.h>
343859Sml29623 
353859Sml29623 uint32_t 	nxge_use_partition = 0;		/* debug partition flag */
363859Sml29623 uint32_t 	nxge_dma_obp_props_only = 1;	/* use obp published props */
373859Sml29623 uint32_t 	nxge_use_rdc_intr = 1;		/* debug to assign rdc intr */
383859Sml29623 /*
395013Sml29623  * PSARC/2007/453 MSI-X interrupt limit override
403859Sml29623  */
415013Sml29623 uint32_t	nxge_msi_enable = 2;
423859Sml29623 
436611Sml29623 /*
446705Sml29623  * Software workaround for a Neptune (PCI-E)
456705Sml29623  * hardware interrupt bug which the hardware
466705Sml29623  * may generate spurious interrupts after the
476705Sml29623  * device interrupt handler was removed. If this flag
486705Sml29623  * is enabled, the driver will reset the
496705Sml29623  * hardware when devices are being detached.
506705Sml29623  */
516705Sml29623 uint32_t	nxge_peu_reset_enable = 0;
526705Sml29623 
536705Sml29623 /*
546611Sml29623  * Software workaround for the hardware
556611Sml29623  * checksum bugs that affect packet transmission
566611Sml29623  * and receive:
576611Sml29623  *
586611Sml29623  * Usage of nxge_cksum_offload:
596611Sml29623  *
606611Sml29623  *  (1) nxge_cksum_offload = 0 (default):
616611Sml29623  *	- transmits packets:
626611Sml29623  *	  TCP: uses the hardware checksum feature.
636611Sml29623  *	  UDP: driver will compute the software checksum
646611Sml29623  *	       based on the partial checksum computed
656611Sml29623  *	       by the IP layer.
666611Sml29623  *	- receives packets
676611Sml29623  *	  TCP: marks packets checksum flags based on hardware result.
686611Sml29623  *	  UDP: will not mark checksum flags.
696611Sml29623  *
706611Sml29623  *  (2) nxge_cksum_offload = 1:
716611Sml29623  *	- transmit packets:
726611Sml29623  *	  TCP/UDP: uses the hardware checksum feature.
736611Sml29623  *	- receives packets
746611Sml29623  *	  TCP/UDP: marks packet checksum flags based on hardware result.
756611Sml29623  *
766611Sml29623  *  (3) nxge_cksum_offload = 2:
776611Sml29623  *	- The driver will not register its checksum capability.
786611Sml29623  *	  Checksum for both TCP and UDP will be computed
796611Sml29623  *	  by the stack.
806611Sml29623  *	- The software LSO is not allowed in this case.
816611Sml29623  *
826611Sml29623  *  (4) nxge_cksum_offload > 2:
836611Sml29623  *	- Will be treated as it is set to 2
846611Sml29623  *	  (stack will compute the checksum).
856611Sml29623  *
866611Sml29623  *  (5) If the hardware bug is fixed, this workaround
876611Sml29623  *	needs to be updated accordingly to reflect
886611Sml29623  *	the new hardware revision.
896611Sml29623  */
906611Sml29623 uint32_t	nxge_cksum_offload = 0;
916495Sspeer 
923859Sml29623 /*
933859Sml29623  * Globals: tunable parameters (/etc/system or adb)
943859Sml29623  *
953859Sml29623  */
963859Sml29623 uint32_t 	nxge_rbr_size = NXGE_RBR_RBB_DEFAULT;
973859Sml29623 uint32_t 	nxge_rbr_spare_size = 0;
983859Sml29623 uint32_t 	nxge_rcr_size = NXGE_RCR_DEFAULT;
99*11304SJanie.Lu@Sun.COM uint16_t	nxge_rdc_buf_offset = SW_OFFSET_NO_OFFSET;
1003859Sml29623 uint32_t 	nxge_tx_ring_size = NXGE_TX_RING_DEFAULT;
1014193Sspeer boolean_t 	nxge_no_msg = B_TRUE;		/* control message display */
1023859Sml29623 uint32_t 	nxge_no_link_notify = 0;	/* control DL_NOTIFY */
1033859Sml29623 uint32_t 	nxge_bcopy_thresh = TX_BCOPY_MAX;
1043859Sml29623 uint32_t 	nxge_dvma_thresh = TX_FASTDVMA_MIN;
1053859Sml29623 uint32_t 	nxge_dma_stream_thresh = TX_STREAM_MIN;
1063859Sml29623 uint32_t	nxge_jumbo_mtu	= TX_JUMBO_MTU;
1073952Sml29623 nxge_tx_mode_t	nxge_tx_scheme = NXGE_USE_SERIAL;
1083859Sml29623 
1095770Sml29623 /* MAX LSO size */
1105770Sml29623 #define		NXGE_LSO_MAXLEN	65535
1115770Sml29623 uint32_t	nxge_lso_max = NXGE_LSO_MAXLEN;
1125770Sml29623 
1133859Sml29623 
1143859Sml29623 /*
1153859Sml29623  * Add tunable to reduce the amount of time spent in the
1163859Sml29623  * ISR doing Rx Processing.
1173859Sml29623  */
1183859Sml29623 uint32_t nxge_max_rx_pkts = 1024;
1193859Sml29623 
1203859Sml29623 /*
1213859Sml29623  * Tunables to manage the receive buffer blocks.
1223859Sml29623  *
1233859Sml29623  * nxge_rx_threshold_hi: copy all buffers.
1243859Sml29623  * nxge_rx_bcopy_size_type: receive buffer block size type.
1253859Sml29623  * nxge_rx_threshold_lo: copy only up to tunable block size type.
1263859Sml29623  */
1273859Sml29623 nxge_rxbuf_threshold_t nxge_rx_threshold_hi = NXGE_RX_COPY_6;
1283859Sml29623 nxge_rxbuf_type_t nxge_rx_buf_size_type = RCR_PKTBUFSZ_0;
1293859Sml29623 nxge_rxbuf_threshold_t nxge_rx_threshold_lo = NXGE_RX_COPY_3;
1303859Sml29623 
1316495Sspeer /* Use kmem_alloc() to allocate data buffers. */
13210577SMichael.Speer@Sun.COM #if defined(__sparc)
1336498Sspeer uint32_t	nxge_use_kmem_alloc = 1;
13410577SMichael.Speer@Sun.COM #elif defined(__i386)
13510577SMichael.Speer@Sun.COM uint32_t	nxge_use_kmem_alloc = 0;
1366495Sspeer #else
13710577SMichael.Speer@Sun.COM uint32_t	nxge_use_kmem_alloc = 1;
1386495Sspeer #endif
1396495Sspeer 
1403859Sml29623 rtrace_t npi_rtracebuf;
1413859Sml29623 
1427126Sml29623 /*
1437126Sml29623  * The hardware sometimes fails to allow enough time for the link partner
1447126Sml29623  * to send an acknowledgement for packets that the hardware sent to it. The
1457126Sml29623  * hardware resends the packets earlier than it should be in those instances.
1467126Sml29623  * This behavior caused some switches to acknowledge the wrong packets
1477126Sml29623  * and it triggered the fatal error.
1487126Sml29623  * This software workaround is to set the replay timer to a value
1497126Sml29623  * suggested by the hardware team.
1507126Sml29623  *
1517126Sml29623  * PCI config space replay timer register:
1527126Sml29623  *     The following replay timeout value is 0xc
1537126Sml29623  *     for bit 14:18.
1547126Sml29623  */
1557126Sml29623 #define	PCI_REPLAY_TIMEOUT_CFG_OFFSET	0xb8
1567126Sml29623 #define	PCI_REPLAY_TIMEOUT_SHIFT	14
1577126Sml29623 
1587126Sml29623 uint32_t	nxge_set_replay_timer = 1;
1597126Sml29623 uint32_t	nxge_replay_timeout = 0xc;
1607126Sml29623 
1617241Sml29623 /*
1627241Sml29623  * The transmit serialization sometimes causes
1637241Sml29623  * longer sleep before calling the driver transmit
1647241Sml29623  * function as it sleeps longer than it should.
1657241Sml29623  * The performace group suggests that a time wait tunable
1667241Sml29623  * can be used to set the maximum wait time when needed
1677241Sml29623  * and the default is set to 1 tick.
1687241Sml29623  */
1697241Sml29623 uint32_t	nxge_tx_serial_maxsleep = 1;
1707241Sml29623 
1713859Sml29623 #if	defined(sun4v)
1723859Sml29623 /*
1733859Sml29623  * Hypervisor N2/NIU services information.
1743859Sml29623  */
175*11304SJanie.Lu@Sun.COM /*
176*11304SJanie.Lu@Sun.COM  * The following is the default API supported:
177*11304SJanie.Lu@Sun.COM  * major 1 and minor 1.
178*11304SJanie.Lu@Sun.COM  *
179*11304SJanie.Lu@Sun.COM  * Please update the MAX_NIU_MAJORS,
180*11304SJanie.Lu@Sun.COM  * MAX_NIU_MINORS, and minor number supported
181*11304SJanie.Lu@Sun.COM  * when the newer Hypervior API interfaces
182*11304SJanie.Lu@Sun.COM  * are added. Also, please update nxge_hsvc_register()
183*11304SJanie.Lu@Sun.COM  * if needed.
184*11304SJanie.Lu@Sun.COM  */
1853859Sml29623 static hsvc_info_t niu_hsvc = {
1863859Sml29623 	HSVC_REV_1, NULL, HSVC_GROUP_NIU, NIU_MAJOR_VER,
1873859Sml29623 	NIU_MINOR_VER, "nxge"
1883859Sml29623 };
1896495Sspeer 
1906495Sspeer static int nxge_hsvc_register(p_nxge_t);
1913859Sml29623 #endif
1923859Sml29623 
1933859Sml29623 /*
1943859Sml29623  * Function Prototypes
1953859Sml29623  */
1963859Sml29623 static int nxge_attach(dev_info_t *, ddi_attach_cmd_t);
1973859Sml29623 static int nxge_detach(dev_info_t *, ddi_detach_cmd_t);
1983859Sml29623 static void nxge_unattach(p_nxge_t);
1997656SSherry.Moore@Sun.COM static int nxge_quiesce(dev_info_t *);
2003859Sml29623 
2013859Sml29623 #if NXGE_PROPERTY
2023859Sml29623 static void nxge_remove_hard_properties(p_nxge_t);
2033859Sml29623 #endif
2043859Sml29623 
2056495Sspeer /*
2066495Sspeer  * These two functions are required by nxge_hio.c
2076495Sspeer  */
2088275SEric Cheng extern int nxge_m_mmac_remove(void *arg, int slot);
2097766SMichael.Speer@Sun.COM extern void nxge_grp_cleanup(p_nxge_t nxge);
2106495Sspeer 
2113859Sml29623 static nxge_status_t nxge_setup_system_dma_pages(p_nxge_t);
2123859Sml29623 
2133859Sml29623 static nxge_status_t nxge_setup_mutexes(p_nxge_t);
2143859Sml29623 static void nxge_destroy_mutexes(p_nxge_t);
2153859Sml29623 
2163859Sml29623 static nxge_status_t nxge_map_regs(p_nxge_t nxgep);
2173859Sml29623 static void nxge_unmap_regs(p_nxge_t nxgep);
2183859Sml29623 #ifdef	NXGE_DEBUG
2193859Sml29623 static void nxge_test_map_regs(p_nxge_t nxgep);
2203859Sml29623 #endif
2213859Sml29623 
2223859Sml29623 static nxge_status_t nxge_add_intrs(p_nxge_t nxgep);
2233859Sml29623 static void nxge_remove_intrs(p_nxge_t nxgep);
2243859Sml29623 
2253859Sml29623 static nxge_status_t nxge_add_intrs_adv(p_nxge_t nxgep);
2263859Sml29623 static nxge_status_t nxge_add_intrs_adv_type(p_nxge_t, uint32_t);
2273859Sml29623 static nxge_status_t nxge_add_intrs_adv_type_fix(p_nxge_t, uint32_t);
2283859Sml29623 static void nxge_intrs_enable(p_nxge_t nxgep);
2293859Sml29623 static void nxge_intrs_disable(p_nxge_t nxgep);
2303859Sml29623 
2313859Sml29623 static void nxge_suspend(p_nxge_t);
2323859Sml29623 static nxge_status_t nxge_resume(p_nxge_t);
2333859Sml29623 
2343859Sml29623 static nxge_status_t nxge_setup_dev(p_nxge_t);
2353859Sml29623 static void nxge_destroy_dev(p_nxge_t);
2363859Sml29623 
2373859Sml29623 static nxge_status_t nxge_alloc_mem_pool(p_nxge_t);
2383859Sml29623 static void nxge_free_mem_pool(p_nxge_t);
2393859Sml29623 
2406495Sspeer nxge_status_t nxge_alloc_rx_mem_pool(p_nxge_t);
2413859Sml29623 static void nxge_free_rx_mem_pool(p_nxge_t);
2423859Sml29623 
2436495Sspeer nxge_status_t nxge_alloc_tx_mem_pool(p_nxge_t);
2443859Sml29623 static void nxge_free_tx_mem_pool(p_nxge_t);
2453859Sml29623 
2463859Sml29623 static nxge_status_t nxge_dma_mem_alloc(p_nxge_t, dma_method_t,
2473859Sml29623 	struct ddi_dma_attr *,
2483859Sml29623 	size_t, ddi_device_acc_attr_t *, uint_t,
2493859Sml29623 	p_nxge_dma_common_t);
2503859Sml29623 
2513859Sml29623 static void nxge_dma_mem_free(p_nxge_dma_common_t);
2526495Sspeer static void nxge_dma_free_rx_data_buf(p_nxge_dma_common_t);
2533859Sml29623 
2543859Sml29623 static nxge_status_t nxge_alloc_rx_buf_dma(p_nxge_t, uint16_t,
2553859Sml29623 	p_nxge_dma_common_t *, size_t, size_t, uint32_t *);
2563859Sml29623 static void nxge_free_rx_buf_dma(p_nxge_t, p_nxge_dma_common_t, uint32_t);
2573859Sml29623 
2583859Sml29623 static nxge_status_t nxge_alloc_rx_cntl_dma(p_nxge_t, uint16_t,
2593859Sml29623 	p_nxge_dma_common_t *, size_t);
2603859Sml29623 static void nxge_free_rx_cntl_dma(p_nxge_t, p_nxge_dma_common_t);
2613859Sml29623 
2626495Sspeer extern nxge_status_t nxge_alloc_tx_buf_dma(p_nxge_t, uint16_t,
2633859Sml29623 	p_nxge_dma_common_t *, size_t, size_t, uint32_t *);
2643859Sml29623 static void nxge_free_tx_buf_dma(p_nxge_t, p_nxge_dma_common_t, uint32_t);
2653859Sml29623 
2666495Sspeer extern nxge_status_t nxge_alloc_tx_cntl_dma(p_nxge_t, uint16_t,
2673859Sml29623 	p_nxge_dma_common_t *,
2683859Sml29623 	size_t);
2693859Sml29623 static void nxge_free_tx_cntl_dma(p_nxge_t, p_nxge_dma_common_t);
2703859Sml29623 
2713859Sml29623 static int nxge_init_common_dev(p_nxge_t);
2723859Sml29623 static void nxge_uninit_common_dev(p_nxge_t);
2736512Ssowmini extern int nxge_param_set_mac(p_nxge_t, queue_t *, mblk_t *,
2746512Ssowmini     char *, caddr_t);
2759232SMichael.Speer@Sun.COM #if defined(sun4v)
2769232SMichael.Speer@Sun.COM extern nxge_status_t nxge_hio_rdc_enable(p_nxge_t nxgep);
2779232SMichael.Speer@Sun.COM extern nxge_status_t nxge_hio_rdc_intr_arm(p_nxge_t nxge, boolean_t arm);
2789232SMichael.Speer@Sun.COM #endif
2793859Sml29623 
2803859Sml29623 /*
2813859Sml29623  * The next declarations are for the GLDv3 interface.
2823859Sml29623  */
2833859Sml29623 static int nxge_m_start(void *);
2843859Sml29623 static void nxge_m_stop(void *);
2853859Sml29623 static int nxge_m_multicst(void *, boolean_t, const uint8_t *);
2863859Sml29623 static int nxge_m_promisc(void *, boolean_t);
2873859Sml29623 static void nxge_m_ioctl(void *, queue_t *, mblk_t *);
28810309SSriharsha.Basavapatna@Sun.COM nxge_status_t nxge_mac_register(p_nxge_t);
2898275SEric Cheng static int nxge_altmac_set(p_nxge_t nxgep, uint8_t *mac_addr,
2908275SEric Cheng 	int slot, int rdctbl, boolean_t usetbl);
2918275SEric Cheng void nxge_mmac_kstat_update(p_nxge_t nxgep, int slot,
2923859Sml29623 	boolean_t factory);
2938275SEric Cheng 
2948275SEric Cheng static void nxge_m_getfactaddr(void *, uint_t, uint8_t *);
2956439Sml29623 static	boolean_t nxge_m_getcapab(void *, mac_capab_t, void *);
2966439Sml29623 static int nxge_m_setprop(void *, const char *, mac_prop_id_t,
2976439Sml29623     uint_t, const void *);
2986439Sml29623 static int nxge_m_getprop(void *, const char *, mac_prop_id_t,
2998118SVasumathi.Sundaram@Sun.COM     uint_t, uint_t, void *, uint_t *);
3006439Sml29623 static int nxge_set_priv_prop(nxge_t *, const char *, uint_t,
3016439Sml29623     const void *);
3026512Ssowmini static int nxge_get_priv_prop(nxge_t *, const char *, uint_t, uint_t,
3038118SVasumathi.Sundaram@Sun.COM     void *, uint_t *);
3046512Ssowmini static int nxge_get_def_val(nxge_t *, mac_prop_id_t, uint_t, void *);
3058275SEric Cheng static void nxge_fill_ring(void *, mac_ring_type_t, const int, const int,
3068275SEric Cheng     mac_ring_info_t *, mac_ring_handle_t);
3078275SEric Cheng static void nxge_group_add_ring(mac_group_driver_t, mac_ring_driver_t,
3088275SEric Cheng     mac_ring_type_t);
3098275SEric Cheng static void nxge_group_rem_ring(mac_group_driver_t, mac_ring_driver_t,
3108275SEric Cheng     mac_ring_type_t);
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 	{"_rxdma_intr_time", MAC_PROP_PERM_RW},
3236512Ssowmini 	{"_rxdma_intr_pkts", MAC_PROP_PERM_RW},
3246512Ssowmini 	{"_class_opt_ipv4_tcp", MAC_PROP_PERM_RW},
3256512Ssowmini 	{"_class_opt_ipv4_udp", MAC_PROP_PERM_RW},
3266512Ssowmini 	{"_class_opt_ipv4_ah", MAC_PROP_PERM_RW},
3276512Ssowmini 	{"_class_opt_ipv4_sctp", MAC_PROP_PERM_RW},
3286512Ssowmini 	{"_class_opt_ipv6_tcp", MAC_PROP_PERM_RW},
3296512Ssowmini 	{"_class_opt_ipv6_udp", MAC_PROP_PERM_RW},
3306512Ssowmini 	{"_class_opt_ipv6_ah", MAC_PROP_PERM_RW},
3316512Ssowmini 	{"_class_opt_ipv6_sctp", MAC_PROP_PERM_RW},
3326512Ssowmini 	{"_soft_lso_enable", MAC_PROP_PERM_RW}
3336512Ssowmini };
3346512Ssowmini 
3356512Ssowmini #define	NXGE_MAX_PRIV_PROPS	\
3366512Ssowmini 	(sizeof (nxge_priv_props)/sizeof (mac_priv_prop_t))
3376439Sml29623 
3383859Sml29623 #define	NXGE_NEPTUNE_MAGIC	0x4E584745UL
3393859Sml29623 #define	MAX_DUMP_SZ 256
3403859Sml29623 
3416439Sml29623 #define	NXGE_M_CALLBACK_FLAGS	\
3428275SEric Cheng 	(MC_IOCTL | MC_GETCAPAB | MC_SETPROP | MC_GETPROP)
3436439Sml29623 
3446495Sspeer mac_callbacks_t nxge_m_callbacks = {
3453859Sml29623 	NXGE_M_CALLBACK_FLAGS,
3463859Sml29623 	nxge_m_stat,
3473859Sml29623 	nxge_m_start,
3483859Sml29623 	nxge_m_stop,
3493859Sml29623 	nxge_m_promisc,
3503859Sml29623 	nxge_m_multicst,
3518275SEric Cheng 	NULL,
3528275SEric Cheng 	NULL,
3533859Sml29623 	nxge_m_ioctl,
3546439Sml29623 	nxge_m_getcapab,
3556439Sml29623 	NULL,
3566439Sml29623 	NULL,
3576439Sml29623 	nxge_m_setprop,
3586439Sml29623 	nxge_m_getprop
3593859Sml29623 };
3603859Sml29623 
3613859Sml29623 void
3623859Sml29623 nxge_err_inject(p_nxge_t, queue_t *, mblk_t *);
3633859Sml29623 
3645013Sml29623 /* PSARC/2007/453 MSI-X interrupt limit override. */
3655013Sml29623 #define	NXGE_MSIX_REQUEST_10G	8
3665013Sml29623 #define	NXGE_MSIX_REQUEST_1G	2
3675013Sml29623 static int nxge_create_msi_property(p_nxge_t);
3688455Stc99174@train /*
3698455Stc99174@train  * For applications that care about the
3708455Stc99174@train  * latency, it was requested by PAE and the
3718455Stc99174@train  * customers that the driver has tunables that
3728455Stc99174@train  * allow the user to tune it to a higher number
3738455Stc99174@train  * interrupts to spread the interrupts among
3748455Stc99174@train  * multiple channels. The DDI framework limits
3758455Stc99174@train  * the maximum number of MSI-X resources to allocate
3768455Stc99174@train  * to 8 (ddi_msix_alloc_limit). If more than 8
3778455Stc99174@train  * is set, ddi_msix_alloc_limit must be set accordingly.
3788455Stc99174@train  * The default number of MSI interrupts are set to
3798455Stc99174@train  * 8 for 10G and 2 for 1G link.
3808455Stc99174@train  */
3818455Stc99174@train #define	NXGE_MSIX_MAX_ALLOWED	32
3828455Stc99174@train uint32_t nxge_msix_10g_intrs = NXGE_MSIX_REQUEST_10G;
3838455Stc99174@train uint32_t nxge_msix_1g_intrs = NXGE_MSIX_REQUEST_1G;
3845013Sml29623 
3853859Sml29623 /*
3863859Sml29623  * These global variables control the message
3873859Sml29623  * output.
3883859Sml29623  */
3893859Sml29623 out_dbgmsg_t nxge_dbgmsg_out = DBG_CONSOLE | STR_LOG;
3906495Sspeer uint64_t nxge_debug_level;
3913859Sml29623 
3923859Sml29623 /*
3933859Sml29623  * This list contains the instance structures for the Neptune
3943859Sml29623  * devices present in the system. The lock exists to guarantee
3953859Sml29623  * mutually exclusive access to the list.
3963859Sml29623  */
3973859Sml29623 void 			*nxge_list = NULL;
3983859Sml29623 void			*nxge_hw_list = NULL;
3993859Sml29623 nxge_os_mutex_t 	nxge_common_lock;
4009935SMichael.Speer@Sun.COM nxge_os_mutex_t 	nxgedebuglock;
4013859Sml29623 
4023859Sml29623 extern uint64_t 	npi_debug_level;
4033859Sml29623 
4043859Sml29623 extern nxge_status_t	nxge_ldgv_init(p_nxge_t, int *, int *);
4053859Sml29623 extern nxge_status_t	nxge_ldgv_init_n2(p_nxge_t, int *, int *);
4063859Sml29623 extern nxge_status_t	nxge_ldgv_uninit(p_nxge_t);
4073859Sml29623 extern nxge_status_t	nxge_intr_ldgv_init(p_nxge_t);
4083859Sml29623 extern void		nxge_fm_init(p_nxge_t,
4093859Sml29623 					ddi_device_acc_attr_t *,
4103859Sml29623 					ddi_dma_attr_t *);
4113859Sml29623 extern void		nxge_fm_fini(p_nxge_t);
4123859Sml29623 extern npi_status_t	npi_mac_altaddr_disable(npi_handle_t, uint8_t, uint8_t);
4133859Sml29623 
4143859Sml29623 /*
4153859Sml29623  * Count used to maintain the number of buffers being used
4163859Sml29623  * by Neptune instances and loaned up to the upper layers.
4173859Sml29623  */
4183859Sml29623 uint32_t nxge_mblks_pending = 0;
4193859Sml29623 
4203859Sml29623 /*
4213859Sml29623  * Device register access attributes for PIO.
4223859Sml29623  */
4233859Sml29623 static ddi_device_acc_attr_t nxge_dev_reg_acc_attr = {
42411236SStephen.Hanson@Sun.COM 	DDI_DEVICE_ATTR_V1,
4253859Sml29623 	DDI_STRUCTURE_LE_ACC,
4263859Sml29623 	DDI_STRICTORDER_ACC,
42711236SStephen.Hanson@Sun.COM 	DDI_DEFAULT_ACC
4283859Sml29623 };
4293859Sml29623 
4303859Sml29623 /*
4313859Sml29623  * Device descriptor access attributes for DMA.
4323859Sml29623  */
4333859Sml29623 static ddi_device_acc_attr_t nxge_dev_desc_dma_acc_attr = {
4343859Sml29623 	DDI_DEVICE_ATTR_V0,
4353859Sml29623 	DDI_STRUCTURE_LE_ACC,
4363859Sml29623 	DDI_STRICTORDER_ACC
4373859Sml29623 };
4383859Sml29623 
4393859Sml29623 /*
4403859Sml29623  * Device buffer access attributes for DMA.
4413859Sml29623  */
4423859Sml29623 static ddi_device_acc_attr_t nxge_dev_buf_dma_acc_attr = {
4433859Sml29623 	DDI_DEVICE_ATTR_V0,
4443859Sml29623 	DDI_STRUCTURE_BE_ACC,
4453859Sml29623 	DDI_STRICTORDER_ACC
4463859Sml29623 };
4473859Sml29623 
4483859Sml29623 ddi_dma_attr_t nxge_desc_dma_attr = {
4493859Sml29623 	DMA_ATTR_V0,		/* version number. */
4503859Sml29623 	0,			/* low address */
4513859Sml29623 	0xffffffffffffffff,	/* high address */
4523859Sml29623 	0xffffffffffffffff,	/* address counter max */
4533859Sml29623 #ifndef NIU_PA_WORKAROUND
4543859Sml29623 	0x100000,		/* alignment */
4553859Sml29623 #else
4563859Sml29623 	0x2000,
4573859Sml29623 #endif
4583859Sml29623 	0xfc00fc,		/* dlim_burstsizes */
4593859Sml29623 	0x1,			/* minimum transfer size */
4603859Sml29623 	0xffffffffffffffff,	/* maximum transfer size */
4613859Sml29623 	0xffffffffffffffff,	/* maximum segment size */
4623859Sml29623 	1,			/* scatter/gather list length */
4633859Sml29623 	(unsigned int) 1,	/* granularity */
4643859Sml29623 	0			/* attribute flags */
4653859Sml29623 };
4663859Sml29623 
4673859Sml29623 ddi_dma_attr_t nxge_tx_dma_attr = {
4683859Sml29623 	DMA_ATTR_V0,		/* version number. */
4693859Sml29623 	0,			/* low address */
4703859Sml29623 	0xffffffffffffffff,	/* high address */
4713859Sml29623 	0xffffffffffffffff,	/* address counter max */
4723859Sml29623 #if defined(_BIG_ENDIAN)
4733859Sml29623 	0x2000,			/* alignment */
4743859Sml29623 #else
4753859Sml29623 	0x1000,			/* alignment */
4763859Sml29623 #endif
4773859Sml29623 	0xfc00fc,		/* dlim_burstsizes */
4783859Sml29623 	0x1,			/* minimum transfer size */
4793859Sml29623 	0xffffffffffffffff,	/* maximum transfer size */
4803859Sml29623 	0xffffffffffffffff,	/* maximum segment size */
4813859Sml29623 	5,			/* scatter/gather list length */
4823859Sml29623 	(unsigned int) 1,	/* granularity */
4833859Sml29623 	0			/* attribute flags */
4843859Sml29623 };
4853859Sml29623 
4863859Sml29623 ddi_dma_attr_t nxge_rx_dma_attr = {
4873859Sml29623 	DMA_ATTR_V0,		/* version number. */
4883859Sml29623 	0,			/* low address */
4893859Sml29623 	0xffffffffffffffff,	/* high address */
4903859Sml29623 	0xffffffffffffffff,	/* address counter max */
4913859Sml29623 	0x2000,			/* alignment */
4923859Sml29623 	0xfc00fc,		/* dlim_burstsizes */
4933859Sml29623 	0x1,			/* minimum transfer size */
4943859Sml29623 	0xffffffffffffffff,	/* maximum transfer size */
4953859Sml29623 	0xffffffffffffffff,	/* maximum segment size */
4963859Sml29623 	1,			/* scatter/gather list length */
4973859Sml29623 	(unsigned int) 1,	/* granularity */
4984781Ssbehera 	DDI_DMA_RELAXED_ORDERING /* attribute flags */
4993859Sml29623 };
5003859Sml29623 
5013859Sml29623 ddi_dma_lim_t nxge_dma_limits = {
5023859Sml29623 	(uint_t)0,		/* dlim_addr_lo */
5033859Sml29623 	(uint_t)0xffffffff,	/* dlim_addr_hi */
5043859Sml29623 	(uint_t)0xffffffff,	/* dlim_cntr_max */
5053859Sml29623 	(uint_t)0xfc00fc,	/* dlim_burstsizes for 32 and 64 bit xfers */
5063859Sml29623 	0x1,			/* dlim_minxfer */
5073859Sml29623 	1024			/* dlim_speed */
5083859Sml29623 };
5093859Sml29623 
5103859Sml29623 dma_method_t nxge_force_dma = DVMA;
5113859Sml29623 
5123859Sml29623 /*
5133859Sml29623  * dma chunk sizes.
5143859Sml29623  *
5153859Sml29623  * Try to allocate the largest possible size
5163859Sml29623  * so that fewer number of dma chunks would be managed
5173859Sml29623  */
5183859Sml29623 #ifdef NIU_PA_WORKAROUND
5193859Sml29623 size_t alloc_sizes [] = {0x2000};
5203859Sml29623 #else
5213859Sml29623 size_t alloc_sizes [] = {0x1000, 0x2000, 0x4000, 0x8000,
5223859Sml29623 		0x10000, 0x20000, 0x40000, 0x80000,
5235770Sml29623 		0x100000, 0x200000, 0x400000, 0x800000,
5245770Sml29623 		0x1000000, 0x2000000, 0x4000000};
5253859Sml29623 #endif
5263859Sml29623 
5273859Sml29623 /*
5283859Sml29623  * Translate "dev_t" to a pointer to the associated "dev_info_t".
5293859Sml29623  */
5303859Sml29623 
5316495Sspeer extern void nxge_get_environs(nxge_t *);
5326495Sspeer 
5333859Sml29623 static int
5343859Sml29623 nxge_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5353859Sml29623 {
5363859Sml29623 	p_nxge_t	nxgep = NULL;
5373859Sml29623 	int		instance;
5383859Sml29623 	int		status = DDI_SUCCESS;
5393859Sml29623 	uint8_t		portn;
5403859Sml29623 	nxge_mmac_t	*mmac_info;
5413859Sml29623 
5423859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_attach"));
5433859Sml29623 
5443859Sml29623 	/*
5453859Sml29623 	 * Get the device instance since we'll need to setup
5463859Sml29623 	 * or retrieve a soft state for this instance.
5473859Sml29623 	 */
5483859Sml29623 	instance = ddi_get_instance(dip);
5493859Sml29623 
5503859Sml29623 	switch (cmd) {
5513859Sml29623 	case DDI_ATTACH:
5523859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_ATTACH"));
5533859Sml29623 		break;
5543859Sml29623 
5553859Sml29623 	case DDI_RESUME:
5563859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_RESUME"));
5573859Sml29623 		nxgep = (p_nxge_t)ddi_get_soft_state(nxge_list, instance);
5583859Sml29623 		if (nxgep == NULL) {
5593859Sml29623 			status = DDI_FAILURE;
5603859Sml29623 			break;
5613859Sml29623 		}
5623859Sml29623 		if (nxgep->dip != dip) {
5633859Sml29623 			status = DDI_FAILURE;
5643859Sml29623 			break;
5653859Sml29623 		}
5663859Sml29623 		if (nxgep->suspended == DDI_PM_SUSPEND) {
5673859Sml29623 			status = ddi_dev_is_needed(nxgep->dip, 0, 1);
5683859Sml29623 		} else {
5694185Sspeer 			status = nxge_resume(nxgep);
5703859Sml29623 		}
5713859Sml29623 		goto nxge_attach_exit;
5723859Sml29623 
5733859Sml29623 	case DDI_PM_RESUME:
5743859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_PM_RESUME"));
5753859Sml29623 		nxgep = (p_nxge_t)ddi_get_soft_state(nxge_list, instance);
5763859Sml29623 		if (nxgep == NULL) {
5773859Sml29623 			status = DDI_FAILURE;
5783859Sml29623 			break;
5793859Sml29623 		}
5803859Sml29623 		if (nxgep->dip != dip) {
5813859Sml29623 			status = DDI_FAILURE;
5823859Sml29623 			break;
5833859Sml29623 		}
5844185Sspeer 		status = nxge_resume(nxgep);
5853859Sml29623 		goto nxge_attach_exit;
5863859Sml29623 
5873859Sml29623 	default:
5883859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing unknown"));
5893859Sml29623 		status = DDI_FAILURE;
5903859Sml29623 		goto nxge_attach_exit;
5913859Sml29623 	}
5923859Sml29623 
5933859Sml29623 
5943859Sml29623 	if (ddi_soft_state_zalloc(nxge_list, instance) == DDI_FAILURE) {
5953859Sml29623 		status = DDI_FAILURE;
5963859Sml29623 		goto nxge_attach_exit;
5973859Sml29623 	}
5983859Sml29623 
5993859Sml29623 	nxgep = ddi_get_soft_state(nxge_list, instance);
6003859Sml29623 	if (nxgep == NULL) {
6014977Sraghus 		status = NXGE_ERROR;
6024977Sraghus 		goto nxge_attach_fail2;
6033859Sml29623 	}
6043859Sml29623 
6054693Stm144005 	nxgep->nxge_magic = NXGE_MAGIC;
6064693Stm144005 
6073859Sml29623 	nxgep->drv_state = 0;
6083859Sml29623 	nxgep->dip = dip;
6093859Sml29623 	nxgep->instance = instance;
6103859Sml29623 	nxgep->p_dip = ddi_get_parent(dip);
6113859Sml29623 	nxgep->nxge_debug_level = nxge_debug_level;
6123859Sml29623 	npi_debug_level = nxge_debug_level;
6133859Sml29623 
6146495Sspeer 	/* Are we a guest running in a Hybrid I/O environment? */
6156495Sspeer 	nxge_get_environs(nxgep);
6163859Sml29623 
6173859Sml29623 	status = nxge_map_regs(nxgep);
6186495Sspeer 
6193859Sml29623 	if (status != NXGE_OK) {
6203859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "nxge_map_regs failed"));
6214977Sraghus 		goto nxge_attach_fail3;
6223859Sml29623 	}
6233859Sml29623 
62411236SStephen.Hanson@Sun.COM 	nxge_fm_init(nxgep, &nxge_dev_reg_acc_attr, &nxge_rx_dma_attr);
6256495Sspeer 
6266495Sspeer 	/* Create & initialize the per-Neptune data structure */
6276495Sspeer 	/* (even if we're a guest). */
6283859Sml29623 	status = nxge_init_common_dev(nxgep);
6293859Sml29623 	if (status != NXGE_OK) {
6303859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
6316512Ssowmini 		    "nxge_init_common_dev failed"));
6324977Sraghus 		goto nxge_attach_fail4;
6333859Sml29623 	}
6343859Sml29623 
6357126Sml29623 	/*
6367126Sml29623 	 * Software workaround: set the replay timer.
6377126Sml29623 	 */
6387126Sml29623 	if (nxgep->niu_type != N2_NIU) {
6397126Sml29623 		nxge_set_pci_replay_timeout(nxgep);
6407126Sml29623 	}
6417126Sml29623 
6426495Sspeer #if defined(sun4v)
6436495Sspeer 	/* This is required by nxge_hio_init(), which follows. */
6446495Sspeer 	if ((status = nxge_hsvc_register(nxgep)) != DDI_SUCCESS)
6457587SMichael.Speer@Sun.COM 		goto nxge_attach_fail4;
6466495Sspeer #endif
6476495Sspeer 
6486495Sspeer 	if ((status = nxge_hio_init(nxgep)) != NXGE_OK) {
6496495Sspeer 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
6506512Ssowmini 		    "nxge_hio_init failed"));
6516495Sspeer 		goto nxge_attach_fail4;
6526495Sspeer 	}
6536495Sspeer 
6544732Sdavemq 	if (nxgep->niu_type == NEPTUNE_2_10GF) {
6554732Sdavemq 		if (nxgep->function_num > 1) {
6566028Ssbehera 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "Unsupported"
6574732Sdavemq 			    " function %d. Only functions 0 and 1 are "
6584732Sdavemq 			    "supported for this card.", nxgep->function_num));
6594732Sdavemq 			status = NXGE_ERROR;
6604977Sraghus 			goto nxge_attach_fail4;
6614732Sdavemq 		}
6624732Sdavemq 	}
6634732Sdavemq 
6646495Sspeer 	if (isLDOMguest(nxgep)) {
6656495Sspeer 		/*
6666495Sspeer 		 * Use the function number here.
6676495Sspeer 		 */
6686495Sspeer 		nxgep->mac.portnum = nxgep->function_num;
6696495Sspeer 		nxgep->mac.porttype = PORT_TYPE_LOGICAL;
6706495Sspeer 
6716495Sspeer 		/* XXX We'll set the MAC address counts to 1 for now. */
6726495Sspeer 		mmac_info = &nxgep->nxge_mmac_info;
6736495Sspeer 		mmac_info->num_mmac = 1;
6746495Sspeer 		mmac_info->naddrfree = 1;
6753859Sml29623 	} else {
6766495Sspeer 		portn = NXGE_GET_PORT_NUM(nxgep->function_num);
6776495Sspeer 		nxgep->mac.portnum = portn;
6786495Sspeer 		if ((portn == 0) || (portn == 1))
6796495Sspeer 			nxgep->mac.porttype = PORT_TYPE_XMAC;
6806495Sspeer 		else
6816495Sspeer 			nxgep->mac.porttype = PORT_TYPE_BMAC;
6826495Sspeer 		/*
6836495Sspeer 		 * Neptune has 4 ports, the first 2 ports use XMAC (10G MAC)
6846495Sspeer 		 * internally, the rest 2 ports use BMAC (1G "Big" MAC).
6856495Sspeer 		 * The two types of MACs have different characterizations.
6866495Sspeer 		 */
6876495Sspeer 		mmac_info = &nxgep->nxge_mmac_info;
6886495Sspeer 		if (nxgep->function_num < 2) {
6896495Sspeer 			mmac_info->num_mmac = XMAC_MAX_ALT_ADDR_ENTRY;
6906495Sspeer 			mmac_info->naddrfree = XMAC_MAX_ALT_ADDR_ENTRY;
6916495Sspeer 		} else {
6926495Sspeer 			mmac_info->num_mmac = BMAC_MAX_ALT_ADDR_ENTRY;
6936495Sspeer 			mmac_info->naddrfree = BMAC_MAX_ALT_ADDR_ENTRY;
6946495Sspeer 		}
6953859Sml29623 	}
6963859Sml29623 	/*
6973859Sml29623 	 * Setup the Ndd parameters for the this instance.
6983859Sml29623 	 */
6993859Sml29623 	nxge_init_param(nxgep);
7003859Sml29623 
7013859Sml29623 	/*
7023859Sml29623 	 * Setup Register Tracing Buffer.
7033859Sml29623 	 */
7043859Sml29623 	npi_rtrace_buf_init((rtrace_t *)&npi_rtracebuf);
7053859Sml29623 
7063859Sml29623 	/* init stats ptr */
7073859Sml29623 	nxge_init_statsp(nxgep);
7084185Sspeer 
7094977Sraghus 	/*
7106495Sspeer 	 * Copy the vpd info from eeprom to a local data
7116495Sspeer 	 * structure, and then check its validity.
7124977Sraghus 	 */
7136495Sspeer 	if (!isLDOMguest(nxgep)) {
7146495Sspeer 		int *regp;
7156495Sspeer 		uint_t reglen;
7166495Sspeer 		int rv;
7176495Sspeer 
7186495Sspeer 		nxge_vpd_info_get(nxgep);
7196495Sspeer 
7206495Sspeer 		/* Find the NIU config handle. */
7216495Sspeer 		rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
7226495Sspeer 		    ddi_get_parent(nxgep->dip), DDI_PROP_DONTPASS,
7236495Sspeer 		    "reg", &regp, &reglen);
7246495Sspeer 
7256495Sspeer 		if (rv != DDI_PROP_SUCCESS) {
7266495Sspeer 			goto nxge_attach_fail5;
7276495Sspeer 		}
7286495Sspeer 		/*
7296495Sspeer 		 * The address_hi, that is the first int, in the reg
7306495Sspeer 		 * property consists of config handle, but need to remove
7316495Sspeer 		 * the bits 28-31 which are OBP specific info.
7326495Sspeer 		 */
7336495Sspeer 		nxgep->niu_cfg_hdl = (*regp) & 0xFFFFFFF;
7346495Sspeer 		ddi_prop_free(regp);
7356495Sspeer 	}
7366495Sspeer 
7379730SMichael.Speer@Sun.COM 	/*
7389730SMichael.Speer@Sun.COM 	 * Set the defaults for the MTU size.
7399730SMichael.Speer@Sun.COM 	 */
7409730SMichael.Speer@Sun.COM 	nxge_hw_id_init(nxgep);
7419730SMichael.Speer@Sun.COM 
7426495Sspeer 	if (isLDOMguest(nxgep)) {
7436495Sspeer 		uchar_t *prop_val;
7446495Sspeer 		uint_t prop_len;
7457529SSriharsha.Basavapatna@Sun.COM 		uint32_t max_frame_size;
7466495Sspeer 
7476495Sspeer 		extern void nxge_get_logical_props(p_nxge_t);
7486495Sspeer 
7496495Sspeer 		nxgep->statsp->mac_stats.xcvr_inuse = LOGICAL_XCVR;
7506495Sspeer 		nxgep->mac.portmode = PORT_LOGICAL;
7516495Sspeer 		(void) ddi_prop_update_string(DDI_DEV_T_NONE, nxgep->dip,
7526495Sspeer 		    "phy-type", "virtual transceiver");
7536495Sspeer 
7546495Sspeer 		nxgep->nports = 1;
7556495Sspeer 		nxgep->board_ver = 0;	/* XXX What? */
7566495Sspeer 
7576495Sspeer 		/*
7586495Sspeer 		 * local-mac-address property gives us info on which
7596495Sspeer 		 * specific MAC address the Hybrid resource is associated
7606495Sspeer 		 * with.
7616495Sspeer 		 */
7626495Sspeer 		if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, nxgep->dip, 0,
7636495Sspeer 		    "local-mac-address", &prop_val,
7646495Sspeer 		    &prop_len) != DDI_PROP_SUCCESS) {
7656495Sspeer 			goto nxge_attach_fail5;
7666495Sspeer 		}
7676495Sspeer 		if (prop_len !=  ETHERADDRL) {
7686495Sspeer 			ddi_prop_free(prop_val);
7696495Sspeer 			goto nxge_attach_fail5;
7706495Sspeer 		}
7716495Sspeer 		ether_copy(prop_val, nxgep->hio_mac_addr);
7726495Sspeer 		ddi_prop_free(prop_val);
7736495Sspeer 		nxge_get_logical_props(nxgep);
7746495Sspeer 
7757529SSriharsha.Basavapatna@Sun.COM 		/*
7767529SSriharsha.Basavapatna@Sun.COM 		 * Enable Jumbo property based on the "max-frame-size"
7777529SSriharsha.Basavapatna@Sun.COM 		 * property value.
7787529SSriharsha.Basavapatna@Sun.COM 		 */
7797529SSriharsha.Basavapatna@Sun.COM 		max_frame_size = ddi_prop_get_int(DDI_DEV_T_ANY,
7807529SSriharsha.Basavapatna@Sun.COM 		    nxgep->dip, DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
7817529SSriharsha.Basavapatna@Sun.COM 		    "max-frame-size", NXGE_MTU_DEFAULT_MAX);
7827529SSriharsha.Basavapatna@Sun.COM 		if ((max_frame_size > NXGE_MTU_DEFAULT_MAX) &&
7837529SSriharsha.Basavapatna@Sun.COM 		    (max_frame_size <= TX_JUMBO_MTU)) {
7847529SSriharsha.Basavapatna@Sun.COM 			nxgep->mac.is_jumbo = B_TRUE;
7857529SSriharsha.Basavapatna@Sun.COM 			nxgep->mac.maxframesize = (uint16_t)max_frame_size;
7867529SSriharsha.Basavapatna@Sun.COM 			nxgep->mac.default_mtu = nxgep->mac.maxframesize -
7877529SSriharsha.Basavapatna@Sun.COM 			    NXGE_EHEADER_VLAN_CRC;
7887529SSriharsha.Basavapatna@Sun.COM 		}
7896495Sspeer 	} else {
7906495Sspeer 		status = nxge_xcvr_find(nxgep);
7916495Sspeer 
7926495Sspeer 		if (status != NXGE_OK) {
7936495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "nxge_attach: "
7946512Ssowmini 			    " Couldn't determine card type"
7956512Ssowmini 			    " .... exit "));
7966495Sspeer 			goto nxge_attach_fail5;
7976495Sspeer 		}
7986495Sspeer 
7996495Sspeer 		status = nxge_get_config_properties(nxgep);
8006495Sspeer 
8016495Sspeer 		if (status != NXGE_OK) {
8026495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
8036512Ssowmini 			    "get_hw create failed"));
8046495Sspeer 			goto nxge_attach_fail;
8056495Sspeer 		}
8063859Sml29623 	}
8073859Sml29623 
8083859Sml29623 	/*
8093859Sml29623 	 * Setup the Kstats for the driver.
8103859Sml29623 	 */
8113859Sml29623 	nxge_setup_kstats(nxgep);
8123859Sml29623 
8136495Sspeer 	if (!isLDOMguest(nxgep))
8146495Sspeer 		nxge_setup_param(nxgep);
8153859Sml29623 
8163859Sml29623 	status = nxge_setup_system_dma_pages(nxgep);
8173859Sml29623 	if (status != NXGE_OK) {
8183859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "set dma page failed"));
8193859Sml29623 		goto nxge_attach_fail;
8203859Sml29623 	}
8213859Sml29623 
8226495Sspeer 
8236495Sspeer 	if (!isLDOMguest(nxgep))
8246495Sspeer 		nxge_hw_init_niu_common(nxgep);
8253859Sml29623 
8263859Sml29623 	status = nxge_setup_mutexes(nxgep);
8273859Sml29623 	if (status != NXGE_OK) {
8283859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "set mutex failed"));
8293859Sml29623 		goto nxge_attach_fail;
8303859Sml29623 	}
8313859Sml29623 
8326495Sspeer #if defined(sun4v)
8336495Sspeer 	if (isLDOMguest(nxgep)) {
8346495Sspeer 		/* Find our VR & channel sets. */
8356495Sspeer 		status = nxge_hio_vr_add(nxgep);
83610577SMichael.Speer@Sun.COM 		if (status != DDI_SUCCESS) {
83710577SMichael.Speer@Sun.COM 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
8387812SMichael.Speer@Sun.COM 			    "nxge_hio_vr_add failed"));
8397812SMichael.Speer@Sun.COM 			(void) hsvc_unregister(&nxgep->niu_hsvc);
8407812SMichael.Speer@Sun.COM 			nxgep->niu_hsvc_available = B_FALSE;
84110577SMichael.Speer@Sun.COM 			goto nxge_attach_fail;
8427812SMichael.Speer@Sun.COM 		}
8436495Sspeer 		goto nxge_attach_exit;
8446495Sspeer 	}
8456495Sspeer #endif
8466495Sspeer 
8473859Sml29623 	status = nxge_setup_dev(nxgep);
8483859Sml29623 	if (status != DDI_SUCCESS) {
8493859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "set dev failed"));
8503859Sml29623 		goto nxge_attach_fail;
8513859Sml29623 	}
8523859Sml29623 
8533859Sml29623 	status = nxge_add_intrs(nxgep);
8543859Sml29623 	if (status != DDI_SUCCESS) {
8553859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "add_intr failed"));
8563859Sml29623 		goto nxge_attach_fail;
8573859Sml29623 	}
8587812SMichael.Speer@Sun.COM 
8596835Syc148097 	/* If a guest, register with vio_net instead. */
8604977Sraghus 	if ((status = nxge_mac_register(nxgep)) != NXGE_OK) {
8613859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
8626495Sspeer 		    "unable to register to mac layer (%d)", status));
8633859Sml29623 		goto nxge_attach_fail;
8643859Sml29623 	}
8653859Sml29623 
8663859Sml29623 	mac_link_update(nxgep->mach, LINK_STATE_UNKNOWN);
8673859Sml29623 
8686495Sspeer 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
8696495Sspeer 	    "registered to mac (instance %d)", instance));
8703859Sml29623 
8716835Syc148097 	/* nxge_link_monitor calls xcvr.check_link recursively */
8723859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_START);
8733859Sml29623 
8743859Sml29623 	goto nxge_attach_exit;
8753859Sml29623 
8763859Sml29623 nxge_attach_fail:
8773859Sml29623 	nxge_unattach(nxgep);
8784977Sraghus 	goto nxge_attach_fail1;
8794977Sraghus 
8804977Sraghus nxge_attach_fail5:
8814977Sraghus 	/*
8824977Sraghus 	 * Tear down the ndd parameters setup.
8834977Sraghus 	 */
8844977Sraghus 	nxge_destroy_param(nxgep);
8854977Sraghus 
8864977Sraghus 	/*
8874977Sraghus 	 * Tear down the kstat setup.
8884977Sraghus 	 */
8894977Sraghus 	nxge_destroy_kstats(nxgep);
8904977Sraghus 
8914977Sraghus nxge_attach_fail4:
8924977Sraghus 	if (nxgep->nxge_hw_p) {
8934977Sraghus 		nxge_uninit_common_dev(nxgep);
8944977Sraghus 		nxgep->nxge_hw_p = NULL;
8954977Sraghus 	}
8964977Sraghus 
8974977Sraghus nxge_attach_fail3:
8984977Sraghus 	/*
8994977Sraghus 	 * Unmap the register setup.
9004977Sraghus 	 */
9014977Sraghus 	nxge_unmap_regs(nxgep);
9024977Sraghus 
9034977Sraghus 	nxge_fm_fini(nxgep);
9044977Sraghus 
9054977Sraghus nxge_attach_fail2:
9064977Sraghus 	ddi_soft_state_free(nxge_list, nxgep->instance);
9074977Sraghus 
9084977Sraghus nxge_attach_fail1:
9094185Sspeer 	if (status != NXGE_OK)
9104185Sspeer 		status = (NXGE_ERROR | NXGE_DDI_FAILED);
9113859Sml29623 	nxgep = NULL;
9123859Sml29623 
9133859Sml29623 nxge_attach_exit:
9143859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_attach status = 0x%08x",
9156512Ssowmini 	    status));
9163859Sml29623 
9173859Sml29623 	return (status);
9183859Sml29623 }
9193859Sml29623 
9203859Sml29623 static int
9213859Sml29623 nxge_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
9223859Sml29623 {
9233859Sml29623 	int 		status = DDI_SUCCESS;
9243859Sml29623 	int 		instance;
9253859Sml29623 	p_nxge_t 	nxgep = NULL;
9263859Sml29623 
9273859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_detach"));
9283859Sml29623 	instance = ddi_get_instance(dip);
9293859Sml29623 	nxgep = ddi_get_soft_state(nxge_list, instance);
9303859Sml29623 	if (nxgep == NULL) {
9313859Sml29623 		status = DDI_FAILURE;
9323859Sml29623 		goto nxge_detach_exit;
9333859Sml29623 	}
9343859Sml29623 
9353859Sml29623 	switch (cmd) {
9363859Sml29623 	case DDI_DETACH:
9373859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_DETACH"));
9383859Sml29623 		break;
9393859Sml29623 
9403859Sml29623 	case DDI_PM_SUSPEND:
9413859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_PM_SUSPEND"));
9423859Sml29623 		nxgep->suspended = DDI_PM_SUSPEND;
9433859Sml29623 		nxge_suspend(nxgep);
9443859Sml29623 		break;
9453859Sml29623 
9463859Sml29623 	case DDI_SUSPEND:
9473859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_SUSPEND"));
9483859Sml29623 		if (nxgep->suspended != DDI_PM_SUSPEND) {
9493859Sml29623 			nxgep->suspended = DDI_SUSPEND;
9503859Sml29623 			nxge_suspend(nxgep);
9513859Sml29623 		}
9523859Sml29623 		break;
9533859Sml29623 
9543859Sml29623 	default:
9553859Sml29623 		status = DDI_FAILURE;
9563859Sml29623 	}
9573859Sml29623 
9583859Sml29623 	if (cmd != DDI_DETACH)
9593859Sml29623 		goto nxge_detach_exit;
9603859Sml29623 
9613859Sml29623 	/*
9623859Sml29623 	 * Stop the xcvr polling.
9633859Sml29623 	 */
9643859Sml29623 	nxgep->suspended = cmd;
9653859Sml29623 
9663859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_STOP);
9673859Sml29623 
96810309SSriharsha.Basavapatna@Sun.COM 	if (nxgep->mach && (status = mac_unregister(nxgep->mach)) != 0) {
9693859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
9706512Ssowmini 		    "<== nxge_detach status = 0x%08X", status));
9713859Sml29623 		return (DDI_FAILURE);
9723859Sml29623 	}
9733859Sml29623 
9743859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
9756512Ssowmini 	    "<== nxge_detach (mac_unregister) status = 0x%08X", status));
9763859Sml29623 
9773859Sml29623 	nxge_unattach(nxgep);
9783859Sml29623 	nxgep = NULL;
9793859Sml29623 
9803859Sml29623 nxge_detach_exit:
9813859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_detach status = 0x%08X",
9826512Ssowmini 	    status));
9833859Sml29623 
9843859Sml29623 	return (status);
9853859Sml29623 }
9863859Sml29623 
9873859Sml29623 static void
9883859Sml29623 nxge_unattach(p_nxge_t nxgep)
9893859Sml29623 {
9903859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_unattach"));
9913859Sml29623 
9923859Sml29623 	if (nxgep == NULL || nxgep->dev_regs == NULL) {
9933859Sml29623 		return;
9943859Sml29623 	}
9953859Sml29623 
9964693Stm144005 	nxgep->nxge_magic = 0;
9974693Stm144005 
9985780Ssbehera 	if (nxgep->nxge_timerid) {
9995780Ssbehera 		nxge_stop_timer(nxgep, nxgep->nxge_timerid);
10005780Ssbehera 		nxgep->nxge_timerid = 0;
10015780Ssbehera 	}
10025780Ssbehera 
10036705Sml29623 	/*
10046705Sml29623 	 * If this flag is set, it will affect the Neptune
10056705Sml29623 	 * only.
10066705Sml29623 	 */
10076705Sml29623 	if ((nxgep->niu_type != N2_NIU) && nxge_peu_reset_enable) {
10086705Sml29623 		nxge_niu_peu_reset(nxgep);
10096705Sml29623 	}
10106705Sml29623 
10116495Sspeer #if	defined(sun4v)
10126495Sspeer 	if (isLDOMguest(nxgep)) {
10136498Sspeer 		(void) nxge_hio_vr_release(nxgep);
10146495Sspeer 	}
10156495Sspeer #endif
10166495Sspeer 
10173859Sml29623 	if (nxgep->nxge_hw_p) {
10183859Sml29623 		nxge_uninit_common_dev(nxgep);
10193859Sml29623 		nxgep->nxge_hw_p = NULL;
10203859Sml29623 	}
10213859Sml29623 
10223859Sml29623 #if	defined(sun4v)
10233859Sml29623 	if (nxgep->niu_type == N2_NIU && nxgep->niu_hsvc_available == B_TRUE) {
10243859Sml29623 		(void) hsvc_unregister(&nxgep->niu_hsvc);
10253859Sml29623 		nxgep->niu_hsvc_available = B_FALSE;
10263859Sml29623 	}
10273859Sml29623 #endif
10283859Sml29623 	/*
10293859Sml29623 	 * Stop any further interrupts.
10303859Sml29623 	 */
10313859Sml29623 	nxge_remove_intrs(nxgep);
10323859Sml29623 
10333859Sml29623 	/*
10343859Sml29623 	 * Stop the device and free resources.
10353859Sml29623 	 */
10366495Sspeer 	if (!isLDOMguest(nxgep)) {
10376495Sspeer 		nxge_destroy_dev(nxgep);
10386495Sspeer 	}
10393859Sml29623 
10403859Sml29623 	/*
10413859Sml29623 	 * Tear down the ndd parameters setup.
10423859Sml29623 	 */
10433859Sml29623 	nxge_destroy_param(nxgep);
10443859Sml29623 
10453859Sml29623 	/*
10463859Sml29623 	 * Tear down the kstat setup.
10473859Sml29623 	 */
10483859Sml29623 	nxge_destroy_kstats(nxgep);
10493859Sml29623 
10503859Sml29623 	/*
10513859Sml29623 	 * Destroy all mutexes.
10523859Sml29623 	 */
10533859Sml29623 	nxge_destroy_mutexes(nxgep);
10543859Sml29623 
10553859Sml29623 	/*
10563859Sml29623 	 * Remove the list of ndd parameters which
10573859Sml29623 	 * were setup during attach.
10583859Sml29623 	 */
10593859Sml29623 	if (nxgep->dip) {
10603859Sml29623 		NXGE_DEBUG_MSG((nxgep, OBP_CTL,
10616512Ssowmini 		    " nxge_unattach: remove all properties"));
10623859Sml29623 
10633859Sml29623 		(void) ddi_prop_remove_all(nxgep->dip);
10643859Sml29623 	}
10653859Sml29623 
10663859Sml29623 #if NXGE_PROPERTY
10673859Sml29623 	nxge_remove_hard_properties(nxgep);
10683859Sml29623 #endif
10693859Sml29623 
10703859Sml29623 	/*
10713859Sml29623 	 * Unmap the register setup.
10723859Sml29623 	 */
10733859Sml29623 	nxge_unmap_regs(nxgep);
10743859Sml29623 
10753859Sml29623 	nxge_fm_fini(nxgep);
10763859Sml29623 
10773859Sml29623 	ddi_soft_state_free(nxge_list, nxgep->instance);
10783859Sml29623 
10793859Sml29623 	NXGE_DEBUG_MSG((NULL, DDI_CTL, "<== nxge_unattach"));
10803859Sml29623 }
10813859Sml29623 
10826495Sspeer #if defined(sun4v)
10836495Sspeer int
10847587SMichael.Speer@Sun.COM nxge_hsvc_register(nxge_t *nxgep)
10856495Sspeer {
10866495Sspeer 	nxge_status_t status;
1087*11304SJanie.Lu@Sun.COM 	int i, j;
1088*11304SJanie.Lu@Sun.COM 
1089*11304SJanie.Lu@Sun.COM 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_hsvc_register"));
1090*11304SJanie.Lu@Sun.COM 	if (nxgep->niu_type != N2_NIU) {
1091*11304SJanie.Lu@Sun.COM 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_hsvc_register"));
1092*11304SJanie.Lu@Sun.COM 		return (DDI_SUCCESS);
1093*11304SJanie.Lu@Sun.COM 	}
1094*11304SJanie.Lu@Sun.COM 
1095*11304SJanie.Lu@Sun.COM 	/*
1096*11304SJanie.Lu@Sun.COM 	 * Currently, the NIU Hypervisor API supports two major versions:
1097*11304SJanie.Lu@Sun.COM 	 * version 1 and 2.
1098*11304SJanie.Lu@Sun.COM 	 * If Hypervisor introduces a higher major or minor version,
1099*11304SJanie.Lu@Sun.COM 	 * please update NIU_MAJOR_HI and NIU_MINOR_HI accordingly.
1100*11304SJanie.Lu@Sun.COM 	 */
1101*11304SJanie.Lu@Sun.COM 	nxgep->niu_hsvc_available = B_FALSE;
1102*11304SJanie.Lu@Sun.COM 	bcopy(&niu_hsvc, &nxgep->niu_hsvc,
1103*11304SJanie.Lu@Sun.COM 	    sizeof (hsvc_info_t));
1104*11304SJanie.Lu@Sun.COM 
1105*11304SJanie.Lu@Sun.COM 	for (i = NIU_MAJOR_HI; i > 0; i--) {
1106*11304SJanie.Lu@Sun.COM 		nxgep->niu_hsvc.hsvc_major = i;
1107*11304SJanie.Lu@Sun.COM 		for (j = NIU_MINOR_HI; j >= 0; j--) {
1108*11304SJanie.Lu@Sun.COM 			nxgep->niu_hsvc.hsvc_minor = j;
1109*11304SJanie.Lu@Sun.COM 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
1110*11304SJanie.Lu@Sun.COM 			    "nxge_hsvc_register: %s: negotiating "
1111*11304SJanie.Lu@Sun.COM 			    "hypervisor services revision %d "
1112*11304SJanie.Lu@Sun.COM 			    "group: 0x%lx major: 0x%lx "
1113*11304SJanie.Lu@Sun.COM 			    "minor: 0x%lx",
1114*11304SJanie.Lu@Sun.COM 			    nxgep->niu_hsvc.hsvc_modname,
1115*11304SJanie.Lu@Sun.COM 			    nxgep->niu_hsvc.hsvc_rev,
1116*11304SJanie.Lu@Sun.COM 			    nxgep->niu_hsvc.hsvc_group,
1117*11304SJanie.Lu@Sun.COM 			    nxgep->niu_hsvc.hsvc_major,
1118*11304SJanie.Lu@Sun.COM 			    nxgep->niu_hsvc.hsvc_minor,
1119*11304SJanie.Lu@Sun.COM 			    nxgep->niu_min_ver));
1120*11304SJanie.Lu@Sun.COM 
1121*11304SJanie.Lu@Sun.COM 			if ((status = hsvc_register(&nxgep->niu_hsvc,
1122*11304SJanie.Lu@Sun.COM 			    &nxgep->niu_min_ver)) == 0) {
1123*11304SJanie.Lu@Sun.COM 				/* Use the supported minor */
1124*11304SJanie.Lu@Sun.COM 				nxgep->niu_hsvc.hsvc_minor = nxgep->niu_min_ver;
1125*11304SJanie.Lu@Sun.COM 				NXGE_DEBUG_MSG((nxgep, DDI_CTL,
1126*11304SJanie.Lu@Sun.COM 				    "nxge_hsvc_register: %s: negotiated "
1127*11304SJanie.Lu@Sun.COM 				    "hypervisor services revision %d "
1128*11304SJanie.Lu@Sun.COM 				    "group: 0x%lx major: 0x%lx "
1129*11304SJanie.Lu@Sun.COM 				    "minor: 0x%lx (niu_min_ver 0x%lx)",
1130*11304SJanie.Lu@Sun.COM 				    nxgep->niu_hsvc.hsvc_modname,
1131*11304SJanie.Lu@Sun.COM 				    nxgep->niu_hsvc.hsvc_rev,
1132*11304SJanie.Lu@Sun.COM 				    nxgep->niu_hsvc.hsvc_group,
1133*11304SJanie.Lu@Sun.COM 				    nxgep->niu_hsvc.hsvc_major,
1134*11304SJanie.Lu@Sun.COM 				    nxgep->niu_hsvc.hsvc_minor,
1135*11304SJanie.Lu@Sun.COM 				    nxgep->niu_min_ver));
1136*11304SJanie.Lu@Sun.COM 
1137*11304SJanie.Lu@Sun.COM 				nxgep->niu_hsvc_available = B_TRUE;
1138*11304SJanie.Lu@Sun.COM 				NXGE_DEBUG_MSG((nxgep, DDI_CTL,
1139*11304SJanie.Lu@Sun.COM 				    "<== nxge_hsvc_register: "
1140*11304SJanie.Lu@Sun.COM 				    "NIU Hypervisor service enabled"));
1141*11304SJanie.Lu@Sun.COM 				return (DDI_SUCCESS);
1142*11304SJanie.Lu@Sun.COM 			}
1143*11304SJanie.Lu@Sun.COM 
1144*11304SJanie.Lu@Sun.COM 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
1145*11304SJanie.Lu@Sun.COM 			    "nxge_hsvc_register: %s: negotiated failed - "
1146*11304SJanie.Lu@Sun.COM 			    "try lower major number "
1147*11304SJanie.Lu@Sun.COM 			    "hypervisor services revision %d "
1148*11304SJanie.Lu@Sun.COM 			    "group: 0x%lx major: 0x%lx minor: 0x%lx "
1149*11304SJanie.Lu@Sun.COM 			    "errno: %d",
1150*11304SJanie.Lu@Sun.COM 			    nxgep->niu_hsvc.hsvc_modname,
1151*11304SJanie.Lu@Sun.COM 			    nxgep->niu_hsvc.hsvc_rev,
1152*11304SJanie.Lu@Sun.COM 			    nxgep->niu_hsvc.hsvc_group,
1153*11304SJanie.Lu@Sun.COM 			    nxgep->niu_hsvc.hsvc_major,
1154*11304SJanie.Lu@Sun.COM 			    nxgep->niu_hsvc.hsvc_minor, status));
11556495Sspeer 		}
1156*11304SJanie.Lu@Sun.COM 	}
1157*11304SJanie.Lu@Sun.COM 
1158*11304SJanie.Lu@Sun.COM 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
1159*11304SJanie.Lu@Sun.COM 	    "nxge_hsvc_register: %s: cannot negotiate "
1160*11304SJanie.Lu@Sun.COM 	    "hypervisor services revision %d group: 0x%lx "
1161*11304SJanie.Lu@Sun.COM 	    "major: 0x%lx minor: 0x%lx errno: %d",
1162*11304SJanie.Lu@Sun.COM 	    niu_hsvc.hsvc_modname, niu_hsvc.hsvc_rev,
1163*11304SJanie.Lu@Sun.COM 	    niu_hsvc.hsvc_group, niu_hsvc.hsvc_major,
1164*11304SJanie.Lu@Sun.COM 	    niu_hsvc.hsvc_minor, status));
1165*11304SJanie.Lu@Sun.COM 
1166*11304SJanie.Lu@Sun.COM 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
1167*11304SJanie.Lu@Sun.COM 	    "<== nxge_hsvc_register: Register to NIU Hypervisor failed"));
1168*11304SJanie.Lu@Sun.COM 
1169*11304SJanie.Lu@Sun.COM 	return (DDI_FAILURE);
11706495Sspeer }
11716495Sspeer #endif
11726495Sspeer 
11733859Sml29623 static char n2_siu_name[] = "niu";
11743859Sml29623 
11753859Sml29623 static nxge_status_t
11763859Sml29623 nxge_map_regs(p_nxge_t nxgep)
11773859Sml29623 {
11783859Sml29623 	int		ddi_status = DDI_SUCCESS;
11793859Sml29623 	p_dev_regs_t 	dev_regs;
11803859Sml29623 	char		buf[MAXPATHLEN + 1];
11813859Sml29623 	char 		*devname;
11823859Sml29623 #ifdef	NXGE_DEBUG
11833859Sml29623 	char 		*sysname;
11843859Sml29623 #endif
11853859Sml29623 	off_t		regsize;
11863859Sml29623 	nxge_status_t	status = NXGE_OK;
11873859Sml29623 #if !defined(_BIG_ENDIAN)
11883859Sml29623 	off_t pci_offset;
11893859Sml29623 	uint16_t pcie_devctl;
11903859Sml29623 #endif
11913859Sml29623 
11926495Sspeer 	if (isLDOMguest(nxgep)) {
11936495Sspeer 		return (nxge_guest_regs_map(nxgep));
11946495Sspeer 	}
11956495Sspeer 
11963859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_map_regs"));
11973859Sml29623 	nxgep->dev_regs = NULL;
11983859Sml29623 	dev_regs = KMEM_ZALLOC(sizeof (dev_regs_t), KM_SLEEP);
11993859Sml29623 	dev_regs->nxge_regh = NULL;
12003859Sml29623 	dev_regs->nxge_pciregh = NULL;
12013859Sml29623 	dev_regs->nxge_msix_regh = NULL;
12023859Sml29623 	dev_regs->nxge_vir_regh = NULL;
12033859Sml29623 	dev_regs->nxge_vir2_regh = NULL;
12044732Sdavemq 	nxgep->niu_type = NIU_TYPE_NONE;
12053859Sml29623 
12063859Sml29623 	devname = ddi_pathname(nxgep->dip, buf);
12073859Sml29623 	ASSERT(strlen(devname) > 0);
12083859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12096512Ssowmini 	    "nxge_map_regs: pathname devname %s", devname));
12103859Sml29623 
12116835Syc148097 	/*
12126835Syc148097 	 * The driver is running on a N2-NIU system if devname is something
12136835Syc148097 	 * like "/niu@80/network@0"
12146835Syc148097 	 */
12153859Sml29623 	if (strstr(devname, n2_siu_name)) {
12163859Sml29623 		/* N2/NIU */
12173859Sml29623 		nxgep->niu_type = N2_NIU;
12183859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12196512Ssowmini 		    "nxge_map_regs: N2/NIU devname %s", devname));
1220*11304SJanie.Lu@Sun.COM 		/*
1221*11304SJanie.Lu@Sun.COM 		 * Get function number:
1222*11304SJanie.Lu@Sun.COM 		 *  - N2/NIU: "/niu@80/network@0" and "/niu@80/network@1"
1223*11304SJanie.Lu@Sun.COM 		 */
12243859Sml29623 		nxgep->function_num =
12256512Ssowmini 		    (devname[strlen(devname) -1] == '1' ? 1 : 0);
12263859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12276512Ssowmini 		    "nxge_map_regs: N2/NIU function number %d",
12286512Ssowmini 		    nxgep->function_num));
12293859Sml29623 	} else {
12303859Sml29623 		int		*prop_val;
12313859Sml29623 		uint_t 		prop_len;
12323859Sml29623 		uint8_t 	func_num;
12333859Sml29623 
12343859Sml29623 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, nxgep->dip,
12356512Ssowmini 		    0, "reg",
12366512Ssowmini 		    &prop_val, &prop_len) != DDI_PROP_SUCCESS) {
12373859Sml29623 			NXGE_DEBUG_MSG((nxgep, VPD_CTL,
12386512Ssowmini 			    "Reg property not found"));
12393859Sml29623 			ddi_status = DDI_FAILURE;
12403859Sml29623 			goto nxge_map_regs_fail0;
12413859Sml29623 
12423859Sml29623 		} else {
12433859Sml29623 			func_num = (prop_val[0] >> 8) & 0x7;
12443859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12456512Ssowmini 			    "Reg property found: fun # %d",
12466512Ssowmini 			    func_num));
12473859Sml29623 			nxgep->function_num = func_num;
12486495Sspeer 			if (isLDOMguest(nxgep)) {
12496495Sspeer 				nxgep->function_num /= 2;
12506495Sspeer 				return (NXGE_OK);
12516495Sspeer 			}
12523859Sml29623 			ddi_prop_free(prop_val);
12533859Sml29623 		}
12543859Sml29623 	}
12553859Sml29623 
12563859Sml29623 	switch (nxgep->niu_type) {
12573859Sml29623 	default:
12583859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 0, &regsize);
12593859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12606512Ssowmini 		    "nxge_map_regs: pci config size 0x%x", regsize));
12613859Sml29623 
12623859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 0,
12636512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_pciregp), 0, 0,
12646512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_pciregh);
12653859Sml29623 		if (ddi_status != DDI_SUCCESS) {
12663859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
12676512Ssowmini 			    "ddi_map_regs, nxge bus config regs failed"));
12683859Sml29623 			goto nxge_map_regs_fail0;
12693859Sml29623 		}
12703859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12716512Ssowmini 		    "nxge_map_reg: PCI config addr 0x%0llx "
12726512Ssowmini 		    " handle 0x%0llx", dev_regs->nxge_pciregp,
12736512Ssowmini 		    dev_regs->nxge_pciregh));
12743859Sml29623 			/*
12753859Sml29623 			 * IMP IMP
12763859Sml29623 			 * workaround  for bit swapping bug in HW
12773859Sml29623 			 * which ends up in no-snoop = yes
12783859Sml29623 			 * resulting, in DMA not synched properly
12793859Sml29623 			 */
12803859Sml29623 #if !defined(_BIG_ENDIAN)
12813859Sml29623 		/* workarounds for x86 systems */
12823859Sml29623 		pci_offset = 0x80 + PCIE_DEVCTL;
12839730SMichael.Speer@Sun.COM 		pcie_devctl = pci_config_get16(dev_regs->nxge_pciregh,
12849730SMichael.Speer@Sun.COM 		    pci_offset);
12859730SMichael.Speer@Sun.COM 		pcie_devctl &= ~PCIE_DEVCTL_ENABLE_NO_SNOOP;
12863859Sml29623 		pcie_devctl |= PCIE_DEVCTL_RO_EN;
12873859Sml29623 		pci_config_put16(dev_regs->nxge_pciregh, pci_offset,
12886512Ssowmini 		    pcie_devctl);
12893859Sml29623 #endif
12903859Sml29623 
12913859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 1, &regsize);
12923859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12936512Ssowmini 		    "nxge_map_regs: pio size 0x%x", regsize));
12943859Sml29623 		/* set up the device mapped register */
12953859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 1,
12966512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_regp), 0, 0,
12976512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_regh);
12983859Sml29623 		if (ddi_status != DDI_SUCCESS) {
12993859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
13006512Ssowmini 			    "ddi_map_regs for Neptune global reg failed"));
13013859Sml29623 			goto nxge_map_regs_fail1;
13023859Sml29623 		}
13033859Sml29623 
13043859Sml29623 		/* set up the msi/msi-x mapped register */
13053859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 2, &regsize);
13063859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
13076512Ssowmini 		    "nxge_map_regs: msix size 0x%x", regsize));
13083859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 2,
13096512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_msix_regp), 0, 0,
13106512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_msix_regh);
13113859Sml29623 		if (ddi_status != DDI_SUCCESS) {
13123859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
13136512Ssowmini 			    "ddi_map_regs for msi reg failed"));
13143859Sml29623 			goto nxge_map_regs_fail2;
13153859Sml29623 		}
13163859Sml29623 
13173859Sml29623 		/* set up the vio region mapped register */
13183859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 3, &regsize);
13193859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
13206512Ssowmini 		    "nxge_map_regs: vio size 0x%x", regsize));
13213859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 3,
13226512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_vir_regp), 0, 0,
13236512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_vir_regh);
13243859Sml29623 
13253859Sml29623 		if (ddi_status != DDI_SUCCESS) {
13263859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
13276512Ssowmini 			    "ddi_map_regs for nxge vio reg failed"));
13283859Sml29623 			goto nxge_map_regs_fail3;
13293859Sml29623 		}
13303859Sml29623 		nxgep->dev_regs = dev_regs;
13313859Sml29623 
13323859Sml29623 		NPI_PCI_ACC_HANDLE_SET(nxgep, dev_regs->nxge_pciregh);
13333859Sml29623 		NPI_PCI_ADD_HANDLE_SET(nxgep,
13346512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_pciregp);
13353859Sml29623 		NPI_MSI_ACC_HANDLE_SET(nxgep, dev_regs->nxge_msix_regh);
13363859Sml29623 		NPI_MSI_ADD_HANDLE_SET(nxgep,
13376512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_msix_regp);
13383859Sml29623 
13393859Sml29623 		NPI_ACC_HANDLE_SET(nxgep, dev_regs->nxge_regh);
13403859Sml29623 		NPI_ADD_HANDLE_SET(nxgep, (npi_reg_ptr_t)dev_regs->nxge_regp);
13413859Sml29623 
13423859Sml29623 		NPI_REG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_regh);
13433859Sml29623 		NPI_REG_ADD_HANDLE_SET(nxgep,
13446512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_regp);
13453859Sml29623 
13463859Sml29623 		NPI_VREG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_vir_regh);
13473859Sml29623 		NPI_VREG_ADD_HANDLE_SET(nxgep,
13486512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_vir_regp);
13493859Sml29623 
13503859Sml29623 		break;
13513859Sml29623 
13523859Sml29623 	case N2_NIU:
13533859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "ddi_map_regs, NIU"));
13543859Sml29623 		/*
13553859Sml29623 		 * Set up the device mapped register (FWARC 2006/556)
13563859Sml29623 		 * (changed back to 1: reg starts at 1!)
13573859Sml29623 		 */
13583859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 1, &regsize);
13593859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
13606512Ssowmini 		    "nxge_map_regs: dev size 0x%x", regsize));
13613859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 1,
13626512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_regp), 0, 0,
13636512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_regh);
13643859Sml29623 
13653859Sml29623 		if (ddi_status != DDI_SUCCESS) {
13663859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
13676512Ssowmini 			    "ddi_map_regs for N2/NIU, global reg failed "));
13683859Sml29623 			goto nxge_map_regs_fail1;
13693859Sml29623 		}
13703859Sml29623 
13716495Sspeer 		/* set up the first vio region mapped register */
13723859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 2, &regsize);
13733859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
13746512Ssowmini 		    "nxge_map_regs: vio (1) size 0x%x", regsize));
13753859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 2,
13766512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_vir_regp), 0, 0,
13776512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_vir_regh);
13783859Sml29623 
13793859Sml29623 		if (ddi_status != DDI_SUCCESS) {
13803859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
13816512Ssowmini 			    "ddi_map_regs for nxge vio reg failed"));
13823859Sml29623 			goto nxge_map_regs_fail2;
13833859Sml29623 		}
13846495Sspeer 		/* set up the second vio region mapped register */
13853859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 3, &regsize);
13863859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
13876512Ssowmini 		    "nxge_map_regs: vio (3) size 0x%x", regsize));
13883859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 3,
13896512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_vir2_regp), 0, 0,
13906512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_vir2_regh);
13913859Sml29623 
13923859Sml29623 		if (ddi_status != DDI_SUCCESS) {
13933859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
13946512Ssowmini 			    "ddi_map_regs for nxge vio2 reg failed"));
13953859Sml29623 			goto nxge_map_regs_fail3;
13963859Sml29623 		}
13973859Sml29623 		nxgep->dev_regs = dev_regs;
13983859Sml29623 
13993859Sml29623 		NPI_ACC_HANDLE_SET(nxgep, dev_regs->nxge_regh);
14003859Sml29623 		NPI_ADD_HANDLE_SET(nxgep, (npi_reg_ptr_t)dev_regs->nxge_regp);
14013859Sml29623 
14023859Sml29623 		NPI_REG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_regh);
14033859Sml29623 		NPI_REG_ADD_HANDLE_SET(nxgep,
14046512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_regp);
14053859Sml29623 
14063859Sml29623 		NPI_VREG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_vir_regh);
14073859Sml29623 		NPI_VREG_ADD_HANDLE_SET(nxgep,
14086512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_vir_regp);
14093859Sml29623 
14103859Sml29623 		NPI_V2REG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_vir2_regh);
14113859Sml29623 		NPI_V2REG_ADD_HANDLE_SET(nxgep,
14126512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_vir2_regp);
14133859Sml29623 
14143859Sml29623 		break;
14153859Sml29623 	}
14163859Sml29623 
14173859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "nxge_map_reg: hardware addr 0x%0llx "
14186512Ssowmini 	    " handle 0x%0llx", dev_regs->nxge_regp, dev_regs->nxge_regh));
14193859Sml29623 
14203859Sml29623 	goto nxge_map_regs_exit;
14213859Sml29623 nxge_map_regs_fail3:
14223859Sml29623 	if (dev_regs->nxge_msix_regh) {
14233859Sml29623 		ddi_regs_map_free(&dev_regs->nxge_msix_regh);
14243859Sml29623 	}
14253859Sml29623 	if (dev_regs->nxge_vir_regh) {
14263859Sml29623 		ddi_regs_map_free(&dev_regs->nxge_regh);
14273859Sml29623 	}
14283859Sml29623 nxge_map_regs_fail2:
14293859Sml29623 	if (dev_regs->nxge_regh) {
14303859Sml29623 		ddi_regs_map_free(&dev_regs->nxge_regh);
14313859Sml29623 	}
14323859Sml29623 nxge_map_regs_fail1:
14333859Sml29623 	if (dev_regs->nxge_pciregh) {
14343859Sml29623 		ddi_regs_map_free(&dev_regs->nxge_pciregh);
14353859Sml29623 	}
14363859Sml29623 nxge_map_regs_fail0:
14373859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "Freeing register set memory"));
14383859Sml29623 	kmem_free(dev_regs, sizeof (dev_regs_t));
14393859Sml29623 
14403859Sml29623 nxge_map_regs_exit:
14413859Sml29623 	if (ddi_status != DDI_SUCCESS)
14423859Sml29623 		status |= (NXGE_ERROR | NXGE_DDI_FAILED);
14433859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_map_regs"));
14443859Sml29623 	return (status);
14453859Sml29623 }
14463859Sml29623 
14473859Sml29623 static void
14483859Sml29623 nxge_unmap_regs(p_nxge_t nxgep)
14493859Sml29623 {
14503859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_unmap_regs"));
14516495Sspeer 
14526495Sspeer 	if (isLDOMguest(nxgep)) {
14536495Sspeer 		nxge_guest_regs_map_free(nxgep);
14546495Sspeer 		return;
14556495Sspeer 	}
14566495Sspeer 
14573859Sml29623 	if (nxgep->dev_regs) {
14583859Sml29623 		if (nxgep->dev_regs->nxge_pciregh) {
14593859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
14606512Ssowmini 			    "==> nxge_unmap_regs: bus"));
14613859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_pciregh);
14623859Sml29623 			nxgep->dev_regs->nxge_pciregh = NULL;
14633859Sml29623 		}
14643859Sml29623 		if (nxgep->dev_regs->nxge_regh) {
14653859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
14666512Ssowmini 			    "==> nxge_unmap_regs: device registers"));
14673859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_regh);
14683859Sml29623 			nxgep->dev_regs->nxge_regh = NULL;
14693859Sml29623 		}
14703859Sml29623 		if (nxgep->dev_regs->nxge_msix_regh) {
14713859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
14726512Ssowmini 			    "==> nxge_unmap_regs: device interrupts"));
14733859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_msix_regh);
14743859Sml29623 			nxgep->dev_regs->nxge_msix_regh = NULL;
14753859Sml29623 		}
14763859Sml29623 		if (nxgep->dev_regs->nxge_vir_regh) {
14773859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
14786512Ssowmini 			    "==> nxge_unmap_regs: vio region"));
14793859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_vir_regh);
14803859Sml29623 			nxgep->dev_regs->nxge_vir_regh = NULL;
14813859Sml29623 		}
14823859Sml29623 		if (nxgep->dev_regs->nxge_vir2_regh) {
14833859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
14846512Ssowmini 			    "==> nxge_unmap_regs: vio2 region"));
14853859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_vir2_regh);
14863859Sml29623 			nxgep->dev_regs->nxge_vir2_regh = NULL;
14873859Sml29623 		}
14883859Sml29623 
14893859Sml29623 		kmem_free(nxgep->dev_regs, sizeof (dev_regs_t));
14903859Sml29623 		nxgep->dev_regs = NULL;
14913859Sml29623 	}
14923859Sml29623 
14933859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_unmap_regs"));
14943859Sml29623 }
14953859Sml29623 
14963859Sml29623 static nxge_status_t
14973859Sml29623 nxge_setup_mutexes(p_nxge_t nxgep)
14983859Sml29623 {
14993859Sml29623 	int ddi_status = DDI_SUCCESS;
15003859Sml29623 	nxge_status_t status = NXGE_OK;
15013859Sml29623 	nxge_classify_t *classify_ptr;
15023859Sml29623 	int partition;
15033859Sml29623 
15043859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_setup_mutexes"));
15053859Sml29623 
15063859Sml29623 	/*
15073859Sml29623 	 * Get the interrupt cookie so the mutexes can be
15083859Sml29623 	 * Initialized.
15093859Sml29623 	 */
15106495Sspeer 	if (isLDOMguest(nxgep)) {
15116495Sspeer 		nxgep->interrupt_cookie = 0;
15126495Sspeer 	} else {
15136495Sspeer 		ddi_status = ddi_get_iblock_cookie(nxgep->dip, 0,
15146495Sspeer 		    &nxgep->interrupt_cookie);
15156495Sspeer 
15166495Sspeer 		if (ddi_status != DDI_SUCCESS) {
15176495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
15186495Sspeer 			    "<== nxge_setup_mutexes: failed 0x%x",
15196495Sspeer 			    ddi_status));
15206495Sspeer 			goto nxge_setup_mutexes_exit;
15216495Sspeer 		}
15223859Sml29623 	}
15233859Sml29623 
15244693Stm144005 	cv_init(&nxgep->poll_cv, NULL, CV_DRIVER, NULL);
15254693Stm144005 	MUTEX_INIT(&nxgep->poll_lock, NULL,
15264693Stm144005 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
15274693Stm144005 
15283859Sml29623 	/*
15294693Stm144005 	 * Initialize mutexes for this device.
15303859Sml29623 	 */
15313859Sml29623 	MUTEX_INIT(nxgep->genlock, NULL,
15326512Ssowmini 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
15333859Sml29623 	MUTEX_INIT(&nxgep->ouraddr_lock, NULL,
15346512Ssowmini 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
15353859Sml29623 	MUTEX_INIT(&nxgep->mif_lock, NULL,
15366512Ssowmini 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
15376495Sspeer 	MUTEX_INIT(&nxgep->group_lock, NULL,
15386495Sspeer 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
15393859Sml29623 	RW_INIT(&nxgep->filter_lock, NULL,
15406512Ssowmini 	    RW_DRIVER, (void *)nxgep->interrupt_cookie);
15413859Sml29623 
15423859Sml29623 	classify_ptr = &nxgep->classifier;
15433859Sml29623 		/*
15443859Sml29623 		 * FFLP Mutexes are never used in interrupt context
15453859Sml29623 		 * as fflp operation can take very long time to
15463859Sml29623 		 * complete and hence not suitable to invoke from interrupt
15473859Sml29623 		 * handlers.
15483859Sml29623 		 */
15493859Sml29623 	MUTEX_INIT(&classify_ptr->tcam_lock, NULL,
15504732Sdavemq 	    NXGE_MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
15514977Sraghus 	if (NXGE_IS_VALID_NEPTUNE_TYPE(nxgep)) {
15523859Sml29623 		MUTEX_INIT(&classify_ptr->fcram_lock, NULL,
15534732Sdavemq 		    NXGE_MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
15543859Sml29623 		for (partition = 0; partition < MAX_PARTITION; partition++) {
15553859Sml29623 			MUTEX_INIT(&classify_ptr->hash_lock[partition], NULL,
15563859Sml29623 			    NXGE_MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
15573859Sml29623 		}
15583859Sml29623 	}
15593859Sml29623 
15603859Sml29623 nxge_setup_mutexes_exit:
15613859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
15624732Sdavemq 	    "<== nxge_setup_mutexes status = %x", status));
15633859Sml29623 
15643859Sml29623 	if (ddi_status != DDI_SUCCESS)
15653859Sml29623 		status |= (NXGE_ERROR | NXGE_DDI_FAILED);
15663859Sml29623 
15673859Sml29623 	return (status);
15683859Sml29623 }
15693859Sml29623 
15703859Sml29623 static void
15713859Sml29623 nxge_destroy_mutexes(p_nxge_t nxgep)
15723859Sml29623 {
15733859Sml29623 	int partition;
15743859Sml29623 	nxge_classify_t *classify_ptr;
15753859Sml29623 
15763859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_destroy_mutexes"));
15773859Sml29623 	RW_DESTROY(&nxgep->filter_lock);
15786495Sspeer 	MUTEX_DESTROY(&nxgep->group_lock);
15793859Sml29623 	MUTEX_DESTROY(&nxgep->mif_lock);
15803859Sml29623 	MUTEX_DESTROY(&nxgep->ouraddr_lock);
15813859Sml29623 	MUTEX_DESTROY(nxgep->genlock);
15823859Sml29623 
15833859Sml29623 	classify_ptr = &nxgep->classifier;
15843859Sml29623 	MUTEX_DESTROY(&classify_ptr->tcam_lock);
15853859Sml29623 
15864693Stm144005 	/* Destroy all polling resources. */
15874693Stm144005 	MUTEX_DESTROY(&nxgep->poll_lock);
15884693Stm144005 	cv_destroy(&nxgep->poll_cv);
15894693Stm144005 
15904693Stm144005 	/* free data structures, based on HW type */
15914977Sraghus 	if (NXGE_IS_VALID_NEPTUNE_TYPE(nxgep)) {
15923859Sml29623 		MUTEX_DESTROY(&classify_ptr->fcram_lock);
15933859Sml29623 		for (partition = 0; partition < MAX_PARTITION; partition++) {
15943859Sml29623 			MUTEX_DESTROY(&classify_ptr->hash_lock[partition]);
15953859Sml29623 		}
15963859Sml29623 	}
15973859Sml29623 
15983859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_destroy_mutexes"));
15993859Sml29623 }
16003859Sml29623 
16013859Sml29623 nxge_status_t
16023859Sml29623 nxge_init(p_nxge_t nxgep)
16033859Sml29623 {
16046495Sspeer 	nxge_status_t status = NXGE_OK;
16053859Sml29623 
16063859Sml29623 	NXGE_DEBUG_MSG((nxgep, STR_CTL, "==> nxge_init"));
16073859Sml29623 
16083859Sml29623 	if (nxgep->drv_state & STATE_HW_INITIALIZED) {
16093859Sml29623 		return (status);
16103859Sml29623 	}
16113859Sml29623 
16123859Sml29623 	/*
16133859Sml29623 	 * Allocate system memory for the receive/transmit buffer blocks
16143859Sml29623 	 * and receive/transmit descriptor rings.
16153859Sml29623 	 */
16163859Sml29623 	status = nxge_alloc_mem_pool(nxgep);
16173859Sml29623 	if (status != NXGE_OK) {
16183859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "alloc mem failed\n"));
16193859Sml29623 		goto nxge_init_fail1;
16203859Sml29623 	}
16213859Sml29623 
16226495Sspeer 	if (!isLDOMguest(nxgep)) {
16236495Sspeer 		/*
16246495Sspeer 		 * Initialize and enable the TXC registers.
16256495Sspeer 		 * (Globally enable the Tx controller,
16266495Sspeer 		 *  enable the port, configure the dma channel bitmap,
16276495Sspeer 		 *  configure the max burst size).
16286495Sspeer 		 */
16296495Sspeer 		status = nxge_txc_init(nxgep);
16306495Sspeer 		if (status != NXGE_OK) {
16316495Sspeer 			NXGE_ERROR_MSG((nxgep,
16326495Sspeer 			    NXGE_ERR_CTL, "init txc failed\n"));
16336495Sspeer 			goto nxge_init_fail2;
16346495Sspeer 		}
16353859Sml29623 	}
16363859Sml29623 
16373859Sml29623 	/*
16383859Sml29623 	 * Initialize and enable TXDMA channels.
16393859Sml29623 	 */
16403859Sml29623 	status = nxge_init_txdma_channels(nxgep);
16413859Sml29623 	if (status != NXGE_OK) {
16423859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init txdma failed\n"));
16433859Sml29623 		goto nxge_init_fail3;
16443859Sml29623 	}
16453859Sml29623 
16463859Sml29623 	/*
16473859Sml29623 	 * Initialize and enable RXDMA channels.
16483859Sml29623 	 */
16493859Sml29623 	status = nxge_init_rxdma_channels(nxgep);
16503859Sml29623 	if (status != NXGE_OK) {
16513859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init rxdma failed\n"));
16523859Sml29623 		goto nxge_init_fail4;
16533859Sml29623 	}
16543859Sml29623 
16553859Sml29623 	/*
16566495Sspeer 	 * The guest domain is now done.
16576495Sspeer 	 */
16586495Sspeer 	if (isLDOMguest(nxgep)) {
16596495Sspeer 		nxgep->drv_state |= STATE_HW_INITIALIZED;
16606495Sspeer 		goto nxge_init_exit;
16616495Sspeer 	}
16626495Sspeer 
16636495Sspeer 	/*
16643859Sml29623 	 * Initialize TCAM and FCRAM (Neptune).
16653859Sml29623 	 */
16663859Sml29623 	status = nxge_classify_init(nxgep);
16673859Sml29623 	if (status != NXGE_OK) {
16683859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init classify failed\n"));
16693859Sml29623 		goto nxge_init_fail5;
16703859Sml29623 	}
16713859Sml29623 
16723859Sml29623 	/*
16733859Sml29623 	 * Initialize ZCP
16743859Sml29623 	 */
16753859Sml29623 	status = nxge_zcp_init(nxgep);
16763859Sml29623 	if (status != NXGE_OK) {
16773859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init ZCP failed\n"));
16783859Sml29623 		goto nxge_init_fail5;
16793859Sml29623 	}
16803859Sml29623 
16813859Sml29623 	/*
16823859Sml29623 	 * Initialize IPP.
16833859Sml29623 	 */
16843859Sml29623 	status = nxge_ipp_init(nxgep);
16853859Sml29623 	if (status != NXGE_OK) {
16863859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init IPP failed\n"));
16873859Sml29623 		goto nxge_init_fail5;
16883859Sml29623 	}
16893859Sml29623 
16903859Sml29623 	/*
16913859Sml29623 	 * Initialize the MAC block.
16923859Sml29623 	 */
16933859Sml29623 	status = nxge_mac_init(nxgep);
16943859Sml29623 	if (status != NXGE_OK) {
16953859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init MAC failed\n"));
16963859Sml29623 		goto nxge_init_fail5;
16973859Sml29623 	}
16983859Sml29623 
16993859Sml29623 	/*
17009232SMichael.Speer@Sun.COM 	 * Enable the interrrupts for DDI.
17013859Sml29623 	 */
17029232SMichael.Speer@Sun.COM 	nxge_intrs_enable(nxgep);
17039232SMichael.Speer@Sun.COM 
17043859Sml29623 	nxgep->drv_state |= STATE_HW_INITIALIZED;
17053859Sml29623 
17063859Sml29623 	goto nxge_init_exit;
17073859Sml29623 
17083859Sml29623 nxge_init_fail5:
17093859Sml29623 	nxge_uninit_rxdma_channels(nxgep);
17103859Sml29623 nxge_init_fail4:
17113859Sml29623 	nxge_uninit_txdma_channels(nxgep);
17123859Sml29623 nxge_init_fail3:
17136495Sspeer 	if (!isLDOMguest(nxgep)) {
17146495Sspeer 		(void) nxge_txc_uninit(nxgep);
17156495Sspeer 	}
17163859Sml29623 nxge_init_fail2:
17173859Sml29623 	nxge_free_mem_pool(nxgep);
17183859Sml29623 nxge_init_fail1:
17193859Sml29623 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
17206512Ssowmini 	    "<== nxge_init status (failed) = 0x%08x", status));
17213859Sml29623 	return (status);
17223859Sml29623 
17233859Sml29623 nxge_init_exit:
17243859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_init status = 0x%08x",
17256512Ssowmini 	    status));
17263859Sml29623 	return (status);
17273859Sml29623 }
17283859Sml29623 
17293859Sml29623 
17303859Sml29623 timeout_id_t
17313859Sml29623 nxge_start_timer(p_nxge_t nxgep, fptrv_t func, int msec)
17323859Sml29623 {
17336512Ssowmini 	if ((nxgep->suspended == 0) || (nxgep->suspended == DDI_RESUME)) {
17343859Sml29623 		return (timeout(func, (caddr_t)nxgep,
17356512Ssowmini 		    drv_usectohz(1000 * msec)));
17363859Sml29623 	}
17373859Sml29623 	return (NULL);
17383859Sml29623 }
17393859Sml29623 
17403859Sml29623 /*ARGSUSED*/
17413859Sml29623 void
17423859Sml29623 nxge_stop_timer(p_nxge_t nxgep, timeout_id_t timerid)
17433859Sml29623 {
17443859Sml29623 	if (timerid) {
17453859Sml29623 		(void) untimeout(timerid);
17463859Sml29623 	}
17473859Sml29623 }
17483859Sml29623 
17493859Sml29623 void
17503859Sml29623 nxge_uninit(p_nxge_t nxgep)
17513859Sml29623 {
17523859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_uninit"));
17533859Sml29623 
17543859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
17553859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
17566512Ssowmini 		    "==> nxge_uninit: not initialized"));
17573859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
17586512Ssowmini 		    "<== nxge_uninit"));
17593859Sml29623 		return;
17603859Sml29623 	}
17613859Sml29623 
17629232SMichael.Speer@Sun.COM 	if (!isLDOMguest(nxgep)) {
17639232SMichael.Speer@Sun.COM 		/*
17649232SMichael.Speer@Sun.COM 		 * Reset the receive MAC side.
17659232SMichael.Speer@Sun.COM 		 */
17669232SMichael.Speer@Sun.COM 		(void) nxge_rx_mac_disable(nxgep);
17679232SMichael.Speer@Sun.COM 
17689232SMichael.Speer@Sun.COM 		/*
17699232SMichael.Speer@Sun.COM 		 * Drain the IPP.
17709232SMichael.Speer@Sun.COM 		 */
17719232SMichael.Speer@Sun.COM 		(void) nxge_ipp_drain(nxgep);
17729232SMichael.Speer@Sun.COM 	}
17739232SMichael.Speer@Sun.COM 
17743859Sml29623 	/* stop timer */
17753859Sml29623 	if (nxgep->nxge_timerid) {
17763859Sml29623 		nxge_stop_timer(nxgep, nxgep->nxge_timerid);
17773859Sml29623 		nxgep->nxge_timerid = 0;
17783859Sml29623 	}
17793859Sml29623 
17803859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_STOP);
17813859Sml29623 	(void) nxge_intr_hw_disable(nxgep);
17823859Sml29623 
17833859Sml29623 
17843859Sml29623 	/* Disable and soft reset the IPP */
17856495Sspeer 	if (!isLDOMguest(nxgep))
17866495Sspeer 		(void) nxge_ipp_disable(nxgep);
17873859Sml29623 
17883859Sml29623 	/* Free classification resources */
17893859Sml29623 	(void) nxge_classify_uninit(nxgep);
17903859Sml29623 
17913859Sml29623 	/*
17923859Sml29623 	 * Reset the transmit/receive DMA side.
17933859Sml29623 	 */
17943859Sml29623 	(void) nxge_txdma_hw_mode(nxgep, NXGE_DMA_STOP);
17953859Sml29623 	(void) nxge_rxdma_hw_mode(nxgep, NXGE_DMA_STOP);
17963859Sml29623 
17973859Sml29623 	nxge_uninit_txdma_channels(nxgep);
17983859Sml29623 	nxge_uninit_rxdma_channels(nxgep);
17993859Sml29623 
18003859Sml29623 	/*
18013859Sml29623 	 * Reset the transmit MAC side.
18023859Sml29623 	 */
18033859Sml29623 	(void) nxge_tx_mac_disable(nxgep);
18043859Sml29623 
18053859Sml29623 	nxge_free_mem_pool(nxgep);
18063859Sml29623 
18076705Sml29623 	/*
18086705Sml29623 	 * Start the timer if the reset flag is not set.
18096705Sml29623 	 * If this reset flag is set, the link monitor
18106705Sml29623 	 * will not be started in order to stop furthur bus
18116705Sml29623 	 * activities coming from this interface.
18126705Sml29623 	 * The driver will start the monitor function
18136705Sml29623 	 * if the interface was initialized again later.
18146705Sml29623 	 */
18156705Sml29623 	if (!nxge_peu_reset_enable) {
18166705Sml29623 		(void) nxge_link_monitor(nxgep, LINK_MONITOR_START);
18176705Sml29623 	}
18183859Sml29623 
18193859Sml29623 	nxgep->drv_state &= ~STATE_HW_INITIALIZED;
18203859Sml29623 
18213859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_uninit: "
18226512Ssowmini 	    "nxge_mblks_pending %d", nxge_mblks_pending));
18233859Sml29623 }
18243859Sml29623 
18253859Sml29623 void
18263859Sml29623 nxge_get64(p_nxge_t nxgep, p_mblk_t mp)
18273859Sml29623 {
18283859Sml29623 	uint64_t	reg;
18293859Sml29623 	uint64_t	regdata;
18303859Sml29623 	int		i, retry;
18313859Sml29623 
18323859Sml29623 	bcopy((char *)mp->b_rptr, (char *)&reg, sizeof (uint64_t));
18333859Sml29623 	regdata = 0;
18343859Sml29623 	retry = 1;
18353859Sml29623 
18363859Sml29623 	for (i = 0; i < retry; i++) {
18373859Sml29623 		NXGE_REG_RD64(nxgep->npi_handle, reg, &regdata);
18383859Sml29623 	}
18393859Sml29623 	bcopy((char *)&regdata, (char *)mp->b_rptr, sizeof (uint64_t));
18403859Sml29623 }
18413859Sml29623 
18423859Sml29623 void
18433859Sml29623 nxge_put64(p_nxge_t nxgep, p_mblk_t mp)
18443859Sml29623 {
18453859Sml29623 	uint64_t	reg;
18463859Sml29623 	uint64_t	buf[2];
18473859Sml29623 
18483859Sml29623 	bcopy((char *)mp->b_rptr, (char *)&buf[0], 2 * sizeof (uint64_t));
18493859Sml29623 	reg = buf[0];
18503859Sml29623 
18513859Sml29623 	NXGE_NPI_PIO_WRITE64(nxgep->npi_handle, reg, buf[1]);
18523859Sml29623 }
18533859Sml29623 
18543859Sml29623 /*ARGSUSED*/
18553859Sml29623 /*VARARGS*/
18563859Sml29623 void
18573859Sml29623 nxge_debug_msg(p_nxge_t nxgep, uint64_t level, char *fmt, ...)
18583859Sml29623 {
18593859Sml29623 	char msg_buffer[1048];
18603859Sml29623 	char prefix_buffer[32];
18613859Sml29623 	int instance;
18623859Sml29623 	uint64_t debug_level;
18633859Sml29623 	int cmn_level = CE_CONT;
18643859Sml29623 	va_list ap;
18653859Sml29623 
18666495Sspeer 	if (nxgep && nxgep->nxge_debug_level != nxge_debug_level) {
18676495Sspeer 		/* In case a developer has changed nxge_debug_level. */
18686495Sspeer 		if (nxgep->nxge_debug_level != nxge_debug_level)
18696495Sspeer 			nxgep->nxge_debug_level = nxge_debug_level;
18706495Sspeer 	}
18716495Sspeer 
18723859Sml29623 	debug_level = (nxgep == NULL) ? nxge_debug_level :
18736512Ssowmini 	    nxgep->nxge_debug_level;
18743859Sml29623 
18753859Sml29623 	if ((level & debug_level) ||
18766512Ssowmini 	    (level == NXGE_NOTE) ||
18776512Ssowmini 	    (level == NXGE_ERR_CTL)) {
18783859Sml29623 		/* do the msg processing */
18793859Sml29623 		MUTEX_ENTER(&nxgedebuglock);
18803859Sml29623 
18813859Sml29623 		if ((level & NXGE_NOTE)) {
18823859Sml29623 			cmn_level = CE_NOTE;
18833859Sml29623 		}
18843859Sml29623 
18853859Sml29623 		if (level & NXGE_ERR_CTL) {
18863859Sml29623 			cmn_level = CE_WARN;
18873859Sml29623 		}
18883859Sml29623 
18893859Sml29623 		va_start(ap, fmt);
18903859Sml29623 		(void) vsprintf(msg_buffer, fmt, ap);
18913859Sml29623 		va_end(ap);
18923859Sml29623 		if (nxgep == NULL) {
18933859Sml29623 			instance = -1;
18943859Sml29623 			(void) sprintf(prefix_buffer, "%s :", "nxge");
18953859Sml29623 		} else {
18963859Sml29623 			instance = nxgep->instance;
18973859Sml29623 			(void) sprintf(prefix_buffer,
18986512Ssowmini 			    "%s%d :", "nxge", instance);
18993859Sml29623 		}
19003859Sml29623 
19013859Sml29623 		MUTEX_EXIT(&nxgedebuglock);
19023859Sml29623 		cmn_err(cmn_level, "!%s %s\n",
19036512Ssowmini 		    prefix_buffer, msg_buffer);
19043859Sml29623 
19053859Sml29623 	}
19063859Sml29623 }
19073859Sml29623 
19083859Sml29623 char *
19093859Sml29623 nxge_dump_packet(char *addr, int size)
19103859Sml29623 {
19113859Sml29623 	uchar_t *ap = (uchar_t *)addr;
19123859Sml29623 	int i;
19133859Sml29623 	static char etherbuf[1024];
19143859Sml29623 	char *cp = etherbuf;
19153859Sml29623 	char digits[] = "0123456789abcdef";
19163859Sml29623 
19173859Sml29623 	if (!size)
19183859Sml29623 		size = 60;
19193859Sml29623 
19203859Sml29623 	if (size > MAX_DUMP_SZ) {
19213859Sml29623 		/* Dump the leading bytes */
19223859Sml29623 		for (i = 0; i < MAX_DUMP_SZ/2; i++) {
19233859Sml29623 			if (*ap > 0x0f)
19243859Sml29623 				*cp++ = digits[*ap >> 4];
19253859Sml29623 			*cp++ = digits[*ap++ & 0xf];
19263859Sml29623 			*cp++ = ':';
19273859Sml29623 		}
19283859Sml29623 		for (i = 0; i < 20; i++)
19293859Sml29623 			*cp++ = '.';
19303859Sml29623 		/* Dump the last MAX_DUMP_SZ/2 bytes */
19313859Sml29623 		ap = (uchar_t *)(addr + (size - MAX_DUMP_SZ/2));
19323859Sml29623 		for (i = 0; i < MAX_DUMP_SZ/2; i++) {
19333859Sml29623 			if (*ap > 0x0f)
19343859Sml29623 				*cp++ = digits[*ap >> 4];
19353859Sml29623 			*cp++ = digits[*ap++ & 0xf];
19363859Sml29623 			*cp++ = ':';
19373859Sml29623 		}
19383859Sml29623 	} else {
19393859Sml29623 		for (i = 0; i < size; i++) {
19403859Sml29623 			if (*ap > 0x0f)
19413859Sml29623 				*cp++ = digits[*ap >> 4];
19423859Sml29623 			*cp++ = digits[*ap++ & 0xf];
19433859Sml29623 			*cp++ = ':';
19443859Sml29623 		}
19453859Sml29623 	}
19463859Sml29623 	*--cp = 0;
19473859Sml29623 	return (etherbuf);
19483859Sml29623 }
19493859Sml29623 
19503859Sml29623 #ifdef	NXGE_DEBUG
19513859Sml29623 static void
19523859Sml29623 nxge_test_map_regs(p_nxge_t nxgep)
19533859Sml29623 {
19543859Sml29623 	ddi_acc_handle_t cfg_handle;
19553859Sml29623 	p_pci_cfg_t	cfg_ptr;
19563859Sml29623 	ddi_acc_handle_t dev_handle;
19573859Sml29623 	char		*dev_ptr;
19583859Sml29623 	ddi_acc_handle_t pci_config_handle;
19593859Sml29623 	uint32_t	regval;
19603859Sml29623 	int		i;
19613859Sml29623 
19623859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_test_map_regs"));
19633859Sml29623 
19643859Sml29623 	dev_handle = nxgep->dev_regs->nxge_regh;
19653859Sml29623 	dev_ptr = (char *)nxgep->dev_regs->nxge_regp;
19663859Sml29623 
19674977Sraghus 	if (NXGE_IS_VALID_NEPTUNE_TYPE(nxgep)) {
19683859Sml29623 		cfg_handle = nxgep->dev_regs->nxge_pciregh;
19693859Sml29623 		cfg_ptr = (void *)nxgep->dev_regs->nxge_pciregp;
19703859Sml29623 
19713859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19724732Sdavemq 		    "Neptune PCI regp cfg_ptr 0x%llx", (char *)cfg_ptr));
19733859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19744732Sdavemq 		    "Neptune PCI cfg_ptr vendor id ptr 0x%llx",
19754732Sdavemq 		    &cfg_ptr->vendorid));
19763859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19774732Sdavemq 		    "\tvendorid 0x%x devid 0x%x",
19784732Sdavemq 		    NXGE_PIO_READ16(cfg_handle, &cfg_ptr->vendorid, 0),
19794732Sdavemq 		    NXGE_PIO_READ16(cfg_handle, &cfg_ptr->devid,    0)));
19803859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19814732Sdavemq 		    "PCI BAR: base 0x%x base14 0x%x base 18 0x%x "
19824732Sdavemq 		    "bar1c 0x%x",
19834732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base,   0),
19844732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base14, 0),
19854732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base18, 0),
19864732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base1c, 0)));
19873859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19884732Sdavemq 		    "\nNeptune PCI BAR: base20 0x%x base24 0x%x "
19894732Sdavemq 		    "base 28 0x%x bar2c 0x%x\n",
19904732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base20, 0),
19914732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base24, 0),
19924732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base28, 0),
19934732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base2c, 0)));
19943859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19954732Sdavemq 		    "\nNeptune PCI BAR: base30 0x%x\n",
19964732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base30, 0)));
19973859Sml29623 
19983859Sml29623 		cfg_handle = nxgep->dev_regs->nxge_pciregh;
19993859Sml29623 		cfg_ptr = (void *)nxgep->dev_regs->nxge_pciregp;
20003859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
20014732Sdavemq 		    "first  0x%llx second 0x%llx third 0x%llx "
20024732Sdavemq 		    "last 0x%llx ",
20034732Sdavemq 		    NXGE_PIO_READ64(dev_handle,
20044732Sdavemq 		    (uint64_t *)(dev_ptr + 0),  0),
20054732Sdavemq 		    NXGE_PIO_READ64(dev_handle,
20064732Sdavemq 		    (uint64_t *)(dev_ptr + 8),  0),
20074732Sdavemq 		    NXGE_PIO_READ64(dev_handle,
20084732Sdavemq 		    (uint64_t *)(dev_ptr + 16), 0),
20094732Sdavemq 		    NXGE_PIO_READ64(cfg_handle,
20104732Sdavemq 		    (uint64_t *)(dev_ptr + 24), 0)));
20113859Sml29623 	}
20123859Sml29623 }
20133859Sml29623 
20143859Sml29623 #endif
20153859Sml29623 
20163859Sml29623 static void
20173859Sml29623 nxge_suspend(p_nxge_t nxgep)
20183859Sml29623 {
20193859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_suspend"));
20203859Sml29623 
20213859Sml29623 	nxge_intrs_disable(nxgep);
20223859Sml29623 	nxge_destroy_dev(nxgep);
20233859Sml29623 
20243859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_suspend"));
20253859Sml29623 }
20263859Sml29623 
20273859Sml29623 static nxge_status_t
20283859Sml29623 nxge_resume(p_nxge_t nxgep)
20293859Sml29623 {
20303859Sml29623 	nxge_status_t status = NXGE_OK;
20313859Sml29623 
20323859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_resume"));
20334587Sjoycey 
20343859Sml29623 	nxgep->suspended = DDI_RESUME;
20354587Sjoycey 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_START);
20364587Sjoycey 	(void) nxge_rxdma_hw_mode(nxgep, NXGE_DMA_START);
20374587Sjoycey 	(void) nxge_txdma_hw_mode(nxgep, NXGE_DMA_START);
20384587Sjoycey 	(void) nxge_rx_mac_enable(nxgep);
20394587Sjoycey 	(void) nxge_tx_mac_enable(nxgep);
20404587Sjoycey 	nxge_intrs_enable(nxgep);
20413859Sml29623 	nxgep->suspended = 0;
20423859Sml29623 
20433859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
20446512Ssowmini 	    "<== nxge_resume status = 0x%x", status));
20453859Sml29623 	return (status);
20463859Sml29623 }
20473859Sml29623 
20483859Sml29623 static nxge_status_t
20493859Sml29623 nxge_setup_dev(p_nxge_t nxgep)
20503859Sml29623 {
20513859Sml29623 	nxge_status_t	status = NXGE_OK;
20523859Sml29623 
20533859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_setup_dev port %d",
20544732Sdavemq 	    nxgep->mac.portnum));
20553859Sml29623 
20563859Sml29623 	status = nxge_link_init(nxgep);
20573859Sml29623 
20583859Sml29623 	if (fm_check_acc_handle(nxgep->dev_regs->nxge_regh) != DDI_FM_OK) {
20593859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
20606512Ssowmini 		    "port%d Bad register acc handle", nxgep->mac.portnum));
20613859Sml29623 		status = NXGE_ERROR;
20623859Sml29623 	}
20633859Sml29623 
20643859Sml29623 	if (status != NXGE_OK) {
20653859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
20666512Ssowmini 		    " nxge_setup_dev status "
20676512Ssowmini 		    "(xcvr init 0x%08x)", status));
20683859Sml29623 		goto nxge_setup_dev_exit;
20693859Sml29623 	}
20703859Sml29623 
20713859Sml29623 nxge_setup_dev_exit:
20723859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
20736512Ssowmini 	    "<== nxge_setup_dev port %d status = 0x%08x",
20746512Ssowmini 	    nxgep->mac.portnum, status));
20753859Sml29623 
20763859Sml29623 	return (status);
20773859Sml29623 }
20783859Sml29623 
20793859Sml29623 static void
20803859Sml29623 nxge_destroy_dev(p_nxge_t nxgep)
20813859Sml29623 {
20823859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_destroy_dev"));
20833859Sml29623 
20843859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_STOP);
20853859Sml29623 
20863859Sml29623 	(void) nxge_hw_stop(nxgep);
20873859Sml29623 
20883859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_destroy_dev"));
20893859Sml29623 }
20903859Sml29623 
20913859Sml29623 static nxge_status_t
20923859Sml29623 nxge_setup_system_dma_pages(p_nxge_t nxgep)
20933859Sml29623 {
20943859Sml29623 	int 			ddi_status = DDI_SUCCESS;
20953859Sml29623 	uint_t 			count;
20963859Sml29623 	ddi_dma_cookie_t 	cookie;
20973859Sml29623 	uint_t 			iommu_pagesize;
20983859Sml29623 	nxge_status_t		status = NXGE_OK;
20993859Sml29623 
21006495Sspeer 	NXGE_ERROR_MSG((nxgep, DDI_CTL, "==> nxge_setup_system_dma_pages"));
21013859Sml29623 	nxgep->sys_page_sz = ddi_ptob(nxgep->dip, (ulong_t)1);
21023859Sml29623 	if (nxgep->niu_type != N2_NIU) {
21033859Sml29623 		iommu_pagesize = dvma_pagesize(nxgep->dip);
21043859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
21056512Ssowmini 		    " nxge_setup_system_dma_pages: page %d (ddi_ptob %d) "
21066512Ssowmini 		    " default_block_size %d iommu_pagesize %d",
21076512Ssowmini 		    nxgep->sys_page_sz,
21086512Ssowmini 		    ddi_ptob(nxgep->dip, (ulong_t)1),
21096512Ssowmini 		    nxgep->rx_default_block_size,
21106512Ssowmini 		    iommu_pagesize));
21113859Sml29623 
21123859Sml29623 		if (iommu_pagesize != 0) {
21133859Sml29623 			if (nxgep->sys_page_sz == iommu_pagesize) {
21143859Sml29623 				if (iommu_pagesize > 0x4000)
21153859Sml29623 					nxgep->sys_page_sz = 0x4000;
21163859Sml29623 			} else {
21173859Sml29623 				if (nxgep->sys_page_sz > iommu_pagesize)
21183859Sml29623 					nxgep->sys_page_sz = iommu_pagesize;
21193859Sml29623 			}
21203859Sml29623 		}
21213859Sml29623 	}
21223859Sml29623 	nxgep->sys_page_mask = ~(nxgep->sys_page_sz - 1);
21233859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
21246512Ssowmini 	    "==> nxge_setup_system_dma_pages: page %d (ddi_ptob %d) "
21256512Ssowmini 	    "default_block_size %d page mask %d",
21266512Ssowmini 	    nxgep->sys_page_sz,
21276512Ssowmini 	    ddi_ptob(nxgep->dip, (ulong_t)1),
21286512Ssowmini 	    nxgep->rx_default_block_size,
21296512Ssowmini 	    nxgep->sys_page_mask));
21303859Sml29623 
21313859Sml29623 
21323859Sml29623 	switch (nxgep->sys_page_sz) {
21333859Sml29623 	default:
21343859Sml29623 		nxgep->sys_page_sz = 0x1000;
21353859Sml29623 		nxgep->sys_page_mask = ~(nxgep->sys_page_sz - 1);
21363859Sml29623 		nxgep->rx_default_block_size = 0x1000;
21373859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_4K;
21383859Sml29623 		break;
21393859Sml29623 	case 0x1000:
21403859Sml29623 		nxgep->rx_default_block_size = 0x1000;
21413859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_4K;
21423859Sml29623 		break;
21433859Sml29623 	case 0x2000:
21443859Sml29623 		nxgep->rx_default_block_size = 0x2000;
21453859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_8K;
21463859Sml29623 		break;
21473859Sml29623 	case 0x4000:
21483859Sml29623 		nxgep->rx_default_block_size = 0x4000;
21493859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_16K;
21503859Sml29623 		break;
21513859Sml29623 	case 0x8000:
21523859Sml29623 		nxgep->rx_default_block_size = 0x8000;
21533859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_32K;
21543859Sml29623 		break;
21553859Sml29623 	}
21563859Sml29623 
21573859Sml29623 #ifndef USE_RX_BIG_BUF
21583859Sml29623 	nxge_rx_dma_attr.dma_attr_align = nxgep->sys_page_sz;
21593859Sml29623 #else
21603859Sml29623 		nxgep->rx_default_block_size = 0x2000;
21613859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_8K;
21623859Sml29623 #endif
21633859Sml29623 	/*
21643859Sml29623 	 * Get the system DMA burst size.
21653859Sml29623 	 */
21663859Sml29623 	ddi_status = ddi_dma_alloc_handle(nxgep->dip, &nxge_tx_dma_attr,
21676512Ssowmini 	    DDI_DMA_DONTWAIT, 0,
21686512Ssowmini 	    &nxgep->dmasparehandle);
21693859Sml29623 	if (ddi_status != DDI_SUCCESS) {
21703859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
21716512Ssowmini 		    "ddi_dma_alloc_handle: failed "
21726512Ssowmini 		    " status 0x%x", ddi_status));
21733859Sml29623 		goto nxge_get_soft_properties_exit;
21743859Sml29623 	}
21753859Sml29623 
21763859Sml29623 	ddi_status = ddi_dma_addr_bind_handle(nxgep->dmasparehandle, NULL,
21776512Ssowmini 	    (caddr_t)nxgep->dmasparehandle,
21786512Ssowmini 	    sizeof (nxgep->dmasparehandle),
21796512Ssowmini 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
21806512Ssowmini 	    DDI_DMA_DONTWAIT, 0,
21816512Ssowmini 	    &cookie, &count);
21823859Sml29623 	if (ddi_status != DDI_DMA_MAPPED) {
21833859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
21846512Ssowmini 		    "Binding spare handle to find system"
21856512Ssowmini 		    " burstsize failed."));
21863859Sml29623 		ddi_status = DDI_FAILURE;
21873859Sml29623 		goto nxge_get_soft_properties_fail1;
21883859Sml29623 	}
21893859Sml29623 
21903859Sml29623 	nxgep->sys_burst_sz = ddi_dma_burstsizes(nxgep->dmasparehandle);
21913859Sml29623 	(void) ddi_dma_unbind_handle(nxgep->dmasparehandle);
21923859Sml29623 
21933859Sml29623 nxge_get_soft_properties_fail1:
21943859Sml29623 	ddi_dma_free_handle(&nxgep->dmasparehandle);
21953859Sml29623 
21963859Sml29623 nxge_get_soft_properties_exit:
21973859Sml29623 
21983859Sml29623 	if (ddi_status != DDI_SUCCESS)
21993859Sml29623 		status |= (NXGE_ERROR | NXGE_DDI_FAILED);
22003859Sml29623 
22013859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
22026512Ssowmini 	    "<== nxge_setup_system_dma_pages status = 0x%08x", status));
22033859Sml29623 	return (status);
22043859Sml29623 }
22053859Sml29623 
22063859Sml29623 static nxge_status_t
22073859Sml29623 nxge_alloc_mem_pool(p_nxge_t nxgep)
22083859Sml29623 {
22093859Sml29623 	nxge_status_t	status = NXGE_OK;
22103859Sml29623 
22113859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_alloc_mem_pool"));
22123859Sml29623 
22133859Sml29623 	status = nxge_alloc_rx_mem_pool(nxgep);
22143859Sml29623 	if (status != NXGE_OK) {
22153859Sml29623 		return (NXGE_ERROR);
22163859Sml29623 	}
22173859Sml29623 
22183859Sml29623 	status = nxge_alloc_tx_mem_pool(nxgep);
22193859Sml29623 	if (status != NXGE_OK) {
22203859Sml29623 		nxge_free_rx_mem_pool(nxgep);
22213859Sml29623 		return (NXGE_ERROR);
22223859Sml29623 	}
22233859Sml29623 
22243859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_alloc_mem_pool"));
22253859Sml29623 	return (NXGE_OK);
22263859Sml29623 }
22273859Sml29623 
22283859Sml29623 static void
22293859Sml29623 nxge_free_mem_pool(p_nxge_t nxgep)
22303859Sml29623 {
22313859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "==> nxge_free_mem_pool"));
22323859Sml29623 
22333859Sml29623 	nxge_free_rx_mem_pool(nxgep);
22343859Sml29623 	nxge_free_tx_mem_pool(nxgep);
22353859Sml29623 
22363859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "<== nxge_free_mem_pool"));
22373859Sml29623 }
22383859Sml29623 
22396495Sspeer nxge_status_t
22403859Sml29623 nxge_alloc_rx_mem_pool(p_nxge_t nxgep)
22413859Sml29623 {
22426495Sspeer 	uint32_t		rdc_max;
22433859Sml29623 	p_nxge_dma_pt_cfg_t	p_all_cfgp;
22443859Sml29623 	p_nxge_hw_pt_cfg_t	p_cfgp;
22453859Sml29623 	p_nxge_dma_pool_t	dma_poolp;
22463859Sml29623 	p_nxge_dma_common_t	*dma_buf_p;
22473859Sml29623 	p_nxge_dma_pool_t	dma_cntl_poolp;
22483859Sml29623 	p_nxge_dma_common_t	*dma_cntl_p;
22493859Sml29623 	uint32_t 		*num_chunks; /* per dma */
22503859Sml29623 	nxge_status_t		status = NXGE_OK;
22513859Sml29623 
22523859Sml29623 	uint32_t		nxge_port_rbr_size;
22533859Sml29623 	uint32_t		nxge_port_rbr_spare_size;
22543859Sml29623 	uint32_t		nxge_port_rcr_size;
22556495Sspeer 	uint32_t		rx_cntl_alloc_size;
22563859Sml29623 
22573859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rx_mem_pool"));
22583859Sml29623 
22593859Sml29623 	p_all_cfgp = (p_nxge_dma_pt_cfg_t)&nxgep->pt_config;
22603859Sml29623 	p_cfgp = (p_nxge_hw_pt_cfg_t)&p_all_cfgp->hw_config;
22616495Sspeer 	rdc_max = NXGE_MAX_RDCS;
22623859Sml29623 
22633859Sml29623 	/*
22646495Sspeer 	 * Allocate memory for the common DMA data structures.
22653859Sml29623 	 */
22663859Sml29623 	dma_poolp = (p_nxge_dma_pool_t)KMEM_ZALLOC(sizeof (nxge_dma_pool_t),
22676512Ssowmini 	    KM_SLEEP);
22683859Sml29623 	dma_buf_p = (p_nxge_dma_common_t *)KMEM_ZALLOC(
22696512Ssowmini 	    sizeof (p_nxge_dma_common_t) * rdc_max, KM_SLEEP);
22703859Sml29623 
22713859Sml29623 	dma_cntl_poolp = (p_nxge_dma_pool_t)
22726512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_pool_t), KM_SLEEP);
22733859Sml29623 	dma_cntl_p = (p_nxge_dma_common_t *)KMEM_ZALLOC(
22746512Ssowmini 	    sizeof (p_nxge_dma_common_t) * rdc_max, KM_SLEEP);
22753859Sml29623 
22763859Sml29623 	num_chunks = (uint32_t *)KMEM_ZALLOC(
22776512Ssowmini 	    sizeof (uint32_t) * rdc_max, KM_SLEEP);
22783859Sml29623 
22793859Sml29623 	/*
22806495Sspeer 	 * Assume that each DMA channel will be configured with
22816495Sspeer 	 * the default block size.
22826495Sspeer 	 * rbr block counts are modulo the batch count (16).
22833859Sml29623 	 */
22843859Sml29623 	nxge_port_rbr_size = p_all_cfgp->rbr_size;
22853859Sml29623 	nxge_port_rcr_size = p_all_cfgp->rcr_size;
22863859Sml29623 
22873859Sml29623 	if (!nxge_port_rbr_size) {
22883859Sml29623 		nxge_port_rbr_size = NXGE_RBR_RBB_DEFAULT;
22893859Sml29623 	}
22903859Sml29623 	if (nxge_port_rbr_size % NXGE_RXDMA_POST_BATCH) {
22913859Sml29623 		nxge_port_rbr_size = (NXGE_RXDMA_POST_BATCH *
22926512Ssowmini 		    (nxge_port_rbr_size / NXGE_RXDMA_POST_BATCH + 1));
22933859Sml29623 	}
22943859Sml29623 
22953859Sml29623 	p_all_cfgp->rbr_size = nxge_port_rbr_size;
22963859Sml29623 	nxge_port_rbr_spare_size = nxge_rbr_spare_size;
22973859Sml29623 
22983859Sml29623 	if (nxge_port_rbr_spare_size % NXGE_RXDMA_POST_BATCH) {
22993859Sml29623 		nxge_port_rbr_spare_size = (NXGE_RXDMA_POST_BATCH *
23006512Ssowmini 		    (nxge_port_rbr_spare_size / NXGE_RXDMA_POST_BATCH + 1));
23013859Sml29623 	}
23025770Sml29623 	if (nxge_port_rbr_size > RBR_DEFAULT_MAX_BLKS) {
23035770Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM_CTL,
23045770Sml29623 		    "nxge_alloc_rx_mem_pool: RBR size too high %d, "
23055770Sml29623 		    "set to default %d",
23065770Sml29623 		    nxge_port_rbr_size, RBR_DEFAULT_MAX_BLKS));
23075770Sml29623 		nxge_port_rbr_size = RBR_DEFAULT_MAX_BLKS;
23085770Sml29623 	}
23095770Sml29623 	if (nxge_port_rcr_size > RCR_DEFAULT_MAX) {
23105770Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM_CTL,
23115770Sml29623 		    "nxge_alloc_rx_mem_pool: RCR too high %d, "
23125770Sml29623 		    "set to default %d",
23135770Sml29623 		    nxge_port_rcr_size, RCR_DEFAULT_MAX));
23145770Sml29623 		nxge_port_rcr_size = RCR_DEFAULT_MAX;
23155770Sml29623 	}
23163859Sml29623 
23173859Sml29623 	/*
23183859Sml29623 	 * N2/NIU has limitation on the descriptor sizes (contiguous
23193859Sml29623 	 * memory allocation on data buffers to 4M (contig_mem_alloc)
23203859Sml29623 	 * and little endian for control buffers (must use the ddi/dki mem alloc
23213859Sml29623 	 * function).
23223859Sml29623 	 */
23233859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
23243859Sml29623 	if (nxgep->niu_type == N2_NIU) {
23253859Sml29623 		nxge_port_rbr_spare_size = 0;
23263859Sml29623 		if ((nxge_port_rbr_size > NXGE_NIU_CONTIG_RBR_MAX) ||
23276512Ssowmini 		    (!ISP2(nxge_port_rbr_size))) {
23283859Sml29623 			nxge_port_rbr_size = NXGE_NIU_CONTIG_RBR_MAX;
23293859Sml29623 		}
23303859Sml29623 		if ((nxge_port_rcr_size > NXGE_NIU_CONTIG_RCR_MAX) ||
23316512Ssowmini 		    (!ISP2(nxge_port_rcr_size))) {
23323859Sml29623 			nxge_port_rcr_size = NXGE_NIU_CONTIG_RCR_MAX;
23333859Sml29623 		}
23343859Sml29623 	}
23353859Sml29623 #endif
23363859Sml29623 
23373859Sml29623 	/*
23383859Sml29623 	 * Addresses of receive block ring, receive completion ring and the
23393859Sml29623 	 * mailbox must be all cache-aligned (64 bytes).
23403859Sml29623 	 */
23413859Sml29623 	rx_cntl_alloc_size = nxge_port_rbr_size + nxge_port_rbr_spare_size;
23423859Sml29623 	rx_cntl_alloc_size *= (sizeof (rx_desc_t));
23433859Sml29623 	rx_cntl_alloc_size += (sizeof (rcr_entry_t) * nxge_port_rcr_size);
23443859Sml29623 	rx_cntl_alloc_size += sizeof (rxdma_mailbox_t);
23453859Sml29623 
23463859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_alloc_rx_mem_pool: "
23476512Ssowmini 	    "nxge_port_rbr_size = %d nxge_port_rbr_spare_size = %d "
23486512Ssowmini 	    "nxge_port_rcr_size = %d "
23496512Ssowmini 	    "rx_cntl_alloc_size = %d",
23506512Ssowmini 	    nxge_port_rbr_size, nxge_port_rbr_spare_size,
23516512Ssowmini 	    nxge_port_rcr_size,
23526512Ssowmini 	    rx_cntl_alloc_size));
23533859Sml29623 
23543859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
23553859Sml29623 	if (nxgep->niu_type == N2_NIU) {
23566495Sspeer 		uint32_t rx_buf_alloc_size = (nxgep->rx_default_block_size *
23576495Sspeer 		    (nxge_port_rbr_size + nxge_port_rbr_spare_size));
23586495Sspeer 
23593859Sml29623 		if (!ISP2(rx_buf_alloc_size)) {
23603859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
23616512Ssowmini 			    "==> nxge_alloc_rx_mem_pool: "
23626512Ssowmini 			    " must be power of 2"));
23633859Sml29623 			status |= (NXGE_ERROR | NXGE_DDI_FAILED);
23643859Sml29623 			goto nxge_alloc_rx_mem_pool_exit;
23653859Sml29623 		}
23663859Sml29623 
23673859Sml29623 		if (rx_buf_alloc_size > (1 << 22)) {
23683859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
23696512Ssowmini 			    "==> nxge_alloc_rx_mem_pool: "
23706512Ssowmini 			    " limit size to 4M"));
23713859Sml29623 			status |= (NXGE_ERROR | NXGE_DDI_FAILED);
23723859Sml29623 			goto nxge_alloc_rx_mem_pool_exit;
23733859Sml29623 		}
23743859Sml29623 
23753859Sml29623 		if (rx_cntl_alloc_size < 0x2000) {
23763859Sml29623 			rx_cntl_alloc_size = 0x2000;
23773859Sml29623 		}
23783859Sml29623 	}
23793859Sml29623 #endif
23803859Sml29623 	nxgep->nxge_port_rbr_size = nxge_port_rbr_size;
23813859Sml29623 	nxgep->nxge_port_rcr_size = nxge_port_rcr_size;
23826495Sspeer 	nxgep->nxge_port_rbr_spare_size = nxge_port_rbr_spare_size;
23836495Sspeer 	nxgep->nxge_port_rx_cntl_alloc_size = rx_cntl_alloc_size;
23846495Sspeer 
23856495Sspeer 	dma_poolp->ndmas = p_cfgp->max_rdcs;
23863859Sml29623 	dma_poolp->num_chunks = num_chunks;
23873859Sml29623 	dma_poolp->buf_allocated = B_TRUE;
23883859Sml29623 	nxgep->rx_buf_pool_p = dma_poolp;
23893859Sml29623 	dma_poolp->dma_buf_pool_p = dma_buf_p;
23903859Sml29623 
23916495Sspeer 	dma_cntl_poolp->ndmas = p_cfgp->max_rdcs;
23923859Sml29623 	dma_cntl_poolp->buf_allocated = B_TRUE;
23933859Sml29623 	nxgep->rx_cntl_pool_p = dma_cntl_poolp;
23943859Sml29623 	dma_cntl_poolp->dma_buf_pool_p = dma_cntl_p;
23953859Sml29623 
23966495Sspeer 	/* Allocate the receive rings, too. */
23976495Sspeer 	nxgep->rx_rbr_rings =
23986512Ssowmini 	    KMEM_ZALLOC(sizeof (rx_rbr_rings_t), KM_SLEEP);
23996495Sspeer 	nxgep->rx_rbr_rings->rbr_rings =
24006512Ssowmini 	    KMEM_ZALLOC(sizeof (p_rx_rbr_ring_t) * rdc_max, KM_SLEEP);
24016495Sspeer 	nxgep->rx_rcr_rings =
24026512Ssowmini 	    KMEM_ZALLOC(sizeof (rx_rcr_rings_t), KM_SLEEP);
24036495Sspeer 	nxgep->rx_rcr_rings->rcr_rings =
24046512Ssowmini 	    KMEM_ZALLOC(sizeof (p_rx_rcr_ring_t) * rdc_max, KM_SLEEP);
24056495Sspeer 	nxgep->rx_mbox_areas_p =
24066512Ssowmini 	    KMEM_ZALLOC(sizeof (rx_mbox_areas_t), KM_SLEEP);
24076495Sspeer 	nxgep->rx_mbox_areas_p->rxmbox_areas =
24086512Ssowmini 	    KMEM_ZALLOC(sizeof (p_rx_mbox_t) * rdc_max, KM_SLEEP);
24096495Sspeer 
24106495Sspeer 	nxgep->rx_rbr_rings->ndmas = nxgep->rx_rcr_rings->ndmas =
24116495Sspeer 	    p_cfgp->max_rdcs;
24126495Sspeer 
24133859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
24146512Ssowmini 	    "<== nxge_alloc_rx_mem_pool:status 0x%08x", status));
24153859Sml29623 
24163859Sml29623 nxge_alloc_rx_mem_pool_exit:
24176495Sspeer 	return (status);
24186495Sspeer }
24196495Sspeer 
24206495Sspeer /*
24216495Sspeer  * nxge_alloc_rxb
24226495Sspeer  *
24236495Sspeer  *	Allocate buffers for an RDC.
24246495Sspeer  *
24256495Sspeer  * Arguments:
24266495Sspeer  * 	nxgep
24276495Sspeer  * 	channel	The channel to map into our kernel space.
24286495Sspeer  *
24296495Sspeer  * Notes:
24306495Sspeer  *
24316495Sspeer  * NPI function calls:
24326495Sspeer  *
24336495Sspeer  * NXGE function calls:
24346495Sspeer  *
24356495Sspeer  * Registers accessed:
24366495Sspeer  *
24376495Sspeer  * Context:
24386495Sspeer  *
24396495Sspeer  * Taking apart:
24406495Sspeer  *
24416495Sspeer  * Open questions:
24426495Sspeer  *
24436495Sspeer  */
24446495Sspeer nxge_status_t
24456495Sspeer nxge_alloc_rxb(
24466495Sspeer 	p_nxge_t nxgep,
24476495Sspeer 	int channel)
24486495Sspeer {
24496495Sspeer 	size_t			rx_buf_alloc_size;
24506495Sspeer 	nxge_status_t		status = NXGE_OK;
24516495Sspeer 
24526495Sspeer 	nxge_dma_common_t	**data;
24536495Sspeer 	nxge_dma_common_t	**control;
24546495Sspeer 	uint32_t 		*num_chunks;
24556495Sspeer 
24566495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rbb"));
24576495Sspeer 
24586495Sspeer 	/*
24596495Sspeer 	 * Allocate memory for the receive buffers and descriptor rings.
24606495Sspeer 	 * Replace these allocation functions with the interface functions
24616495Sspeer 	 * provided by the partition manager if/when they are available.
24626495Sspeer 	 */
24636495Sspeer 
24646495Sspeer 	/*
24656495Sspeer 	 * Allocate memory for the receive buffer blocks.
24666495Sspeer 	 */
24676495Sspeer 	rx_buf_alloc_size = (nxgep->rx_default_block_size *
24686512Ssowmini 	    (nxgep->nxge_port_rbr_size + nxgep->nxge_port_rbr_spare_size));
24696495Sspeer 
24706495Sspeer 	data = &nxgep->rx_buf_pool_p->dma_buf_pool_p[channel];
24716495Sspeer 	num_chunks = &nxgep->rx_buf_pool_p->num_chunks[channel];
24726495Sspeer 
24736495Sspeer 	if ((status = nxge_alloc_rx_buf_dma(
24746495Sspeer 	    nxgep, channel, data, rx_buf_alloc_size,
24756495Sspeer 	    nxgep->rx_default_block_size, num_chunks)) != NXGE_OK) {
24766495Sspeer 		return (status);
24776495Sspeer 	}
24786495Sspeer 
24796495Sspeer 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "<== nxge_alloc_rxb(): "
24806495Sspeer 	    "dma %d dma_buf_p %llx &dma_buf_p %llx", channel, *data, data));
24816495Sspeer 
24826495Sspeer 	/*
24836495Sspeer 	 * Allocate memory for descriptor rings and mailbox.
24846495Sspeer 	 */
24856495Sspeer 	control = &nxgep->rx_cntl_pool_p->dma_buf_pool_p[channel];
24866495Sspeer 
24876495Sspeer 	if ((status = nxge_alloc_rx_cntl_dma(
24886495Sspeer 	    nxgep, channel, control, nxgep->nxge_port_rx_cntl_alloc_size))
24896495Sspeer 	    != NXGE_OK) {
24906495Sspeer 		nxge_free_rx_cntl_dma(nxgep, *control);
24916495Sspeer 		(*data)->buf_alloc_state |= BUF_ALLOCATED_WAIT_FREE;
24926495Sspeer 		nxge_free_rx_buf_dma(nxgep, *data, *num_chunks);
24936495Sspeer 		return (status);
24946495Sspeer 	}
24956495Sspeer 
24963859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
24976495Sspeer 	    "<== nxge_alloc_rx_mem_pool:status 0x%08x", status));
24983859Sml29623 
24993859Sml29623 	return (status);
25003859Sml29623 }
25013859Sml29623 
25026495Sspeer void
25036495Sspeer nxge_free_rxb(
25046495Sspeer 	p_nxge_t nxgep,
25056495Sspeer 	int channel)
25066495Sspeer {
25076495Sspeer 	nxge_dma_common_t	*data;
25086495Sspeer 	nxge_dma_common_t	*control;
25096495Sspeer 	uint32_t 		num_chunks;
25106495Sspeer 
25116495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rbb"));
25126495Sspeer 
25136495Sspeer 	data = nxgep->rx_buf_pool_p->dma_buf_pool_p[channel];
25146495Sspeer 	num_chunks = nxgep->rx_buf_pool_p->num_chunks[channel];
25156495Sspeer 	nxge_free_rx_buf_dma(nxgep, data, num_chunks);
25166495Sspeer 
25176495Sspeer 	nxgep->rx_buf_pool_p->dma_buf_pool_p[channel] = 0;
25186495Sspeer 	nxgep->rx_buf_pool_p->num_chunks[channel] = 0;
25196495Sspeer 
25206495Sspeer 	control = nxgep->rx_cntl_pool_p->dma_buf_pool_p[channel];
25216495Sspeer 	nxge_free_rx_cntl_dma(nxgep, control);
25226495Sspeer 
25236495Sspeer 	nxgep->rx_cntl_pool_p->dma_buf_pool_p[channel] = 0;
25246495Sspeer 
25256495Sspeer 	KMEM_FREE(data, sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK);
25266495Sspeer 	KMEM_FREE(control, sizeof (nxge_dma_common_t));
25276495Sspeer 
25286495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_alloc_rbb"));
25296495Sspeer }
25306495Sspeer 
25313859Sml29623 static void
25323859Sml29623 nxge_free_rx_mem_pool(p_nxge_t nxgep)
25333859Sml29623 {
25346495Sspeer 	int rdc_max = NXGE_MAX_RDCS;
25353859Sml29623 
25363859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_free_rx_mem_pool"));
25373859Sml29623 
25386495Sspeer 	if (!nxgep->rx_buf_pool_p || !nxgep->rx_buf_pool_p->buf_allocated) {
25393859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
25406512Ssowmini 		    "<== nxge_free_rx_mem_pool "
25416512Ssowmini 		    "(null rx buf pool or buf not allocated"));
25423859Sml29623 		return;
25433859Sml29623 	}
25446495Sspeer 	if (!nxgep->rx_cntl_pool_p || !nxgep->rx_cntl_pool_p->buf_allocated) {
25453859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
25466512Ssowmini 		    "<== nxge_free_rx_mem_pool "
25476512Ssowmini 		    "(null rx cntl buf pool or cntl buf not allocated"));
25483859Sml29623 		return;
25493859Sml29623 	}
25503859Sml29623 
25516495Sspeer 	KMEM_FREE(nxgep->rx_cntl_pool_p->dma_buf_pool_p,
25526495Sspeer 	    sizeof (p_nxge_dma_common_t) * rdc_max);
25536495Sspeer 	KMEM_FREE(nxgep->rx_cntl_pool_p, sizeof (nxge_dma_pool_t));
25546495Sspeer 
25556495Sspeer 	KMEM_FREE(nxgep->rx_buf_pool_p->num_chunks,
25566495Sspeer 	    sizeof (uint32_t) * rdc_max);
25576495Sspeer 	KMEM_FREE(nxgep->rx_buf_pool_p->dma_buf_pool_p,
25586495Sspeer 	    sizeof (p_nxge_dma_common_t) * rdc_max);
25596495Sspeer 	KMEM_FREE(nxgep->rx_buf_pool_p, sizeof (nxge_dma_pool_t));
25606495Sspeer 
25616495Sspeer 	nxgep->rx_buf_pool_p = 0;
25626495Sspeer 	nxgep->rx_cntl_pool_p = 0;
25636495Sspeer 
25646495Sspeer 	KMEM_FREE(nxgep->rx_rbr_rings->rbr_rings,
25656495Sspeer 	    sizeof (p_rx_rbr_ring_t) * rdc_max);
25666495Sspeer 	KMEM_FREE(nxgep->rx_rbr_rings, sizeof (rx_rbr_rings_t));
25676495Sspeer 	KMEM_FREE(nxgep->rx_rcr_rings->rcr_rings,
25686495Sspeer 	    sizeof (p_rx_rcr_ring_t) * rdc_max);
25696495Sspeer 	KMEM_FREE(nxgep->rx_rcr_rings, sizeof (rx_rcr_rings_t));
25706495Sspeer 	KMEM_FREE(nxgep->rx_mbox_areas_p->rxmbox_areas,
25716495Sspeer 	    sizeof (p_rx_mbox_t) * rdc_max);
25726495Sspeer 	KMEM_FREE(nxgep->rx_mbox_areas_p, sizeof (rx_mbox_areas_t));
25736495Sspeer 
25746495Sspeer 	nxgep->rx_rbr_rings = 0;
25756495Sspeer 	nxgep->rx_rcr_rings = 0;
25766495Sspeer 	nxgep->rx_mbox_areas_p = 0;
25773859Sml29623 
25783859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "<== nxge_free_rx_mem_pool"));
25793859Sml29623 }
25803859Sml29623 
25813859Sml29623 
25823859Sml29623 static nxge_status_t
25833859Sml29623 nxge_alloc_rx_buf_dma(p_nxge_t nxgep, uint16_t dma_channel,
25843859Sml29623 	p_nxge_dma_common_t *dmap,
25853859Sml29623 	size_t alloc_size, size_t block_size, uint32_t *num_chunks)
25863859Sml29623 {
25873859Sml29623 	p_nxge_dma_common_t 	rx_dmap;
25883859Sml29623 	nxge_status_t		status = NXGE_OK;
25893859Sml29623 	size_t			total_alloc_size;
25903859Sml29623 	size_t			allocated = 0;
25913859Sml29623 	int			i, size_index, array_size;
25926495Sspeer 	boolean_t		use_kmem_alloc = B_FALSE;
25933859Sml29623 
25943859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rx_buf_dma"));
25953859Sml29623 
25963859Sml29623 	rx_dmap = (p_nxge_dma_common_t)
25976512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK,
25986512Ssowmini 	    KM_SLEEP);
25993859Sml29623 
26003859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26016512Ssowmini 	    " alloc_rx_buf_dma rdc %d asize %x bsize %x bbuf %llx ",
26026512Ssowmini 	    dma_channel, alloc_size, block_size, dmap));
26033859Sml29623 
26043859Sml29623 	total_alloc_size = alloc_size;
26053859Sml29623 
26063859Sml29623 #if defined(RX_USE_RECLAIM_POST)
26073859Sml29623 	total_alloc_size = alloc_size + alloc_size/4;
26083859Sml29623 #endif
26093859Sml29623 
26103859Sml29623 	i = 0;
26113859Sml29623 	size_index = 0;
26123859Sml29623 	array_size =  sizeof (alloc_sizes)/sizeof (size_t);
26138661SSantwona.Behera@Sun.COM 	while ((size_index < array_size) &&
26148661SSantwona.Behera@Sun.COM 	    (alloc_sizes[size_index] < alloc_size))
26156512Ssowmini 		size_index++;
26163859Sml29623 	if (size_index >= array_size) {
26173859Sml29623 		size_index = array_size - 1;
26183859Sml29623 	}
26193859Sml29623 
26206495Sspeer 	/* For Neptune, use kmem_alloc if the kmem flag is set. */
26216495Sspeer 	if (nxgep->niu_type != N2_NIU && nxge_use_kmem_alloc) {
26226495Sspeer 		use_kmem_alloc = B_TRUE;
26236495Sspeer #if defined(__i386) || defined(__amd64)
26246495Sspeer 		size_index = 0;
26256495Sspeer #endif
26266495Sspeer 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26276495Sspeer 		    "==> nxge_alloc_rx_buf_dma: "
26286495Sspeer 		    "Neptune use kmem_alloc() - size_index %d",
26296495Sspeer 		    size_index));
26306495Sspeer 	}
26316495Sspeer 
26323859Sml29623 	while ((allocated < total_alloc_size) &&
26336512Ssowmini 	    (size_index >= 0) && (i < NXGE_DMA_BLOCK)) {
26343859Sml29623 		rx_dmap[i].dma_chunk_index = i;
26353859Sml29623 		rx_dmap[i].block_size = block_size;
26363859Sml29623 		rx_dmap[i].alength = alloc_sizes[size_index];
26373859Sml29623 		rx_dmap[i].orig_alength = rx_dmap[i].alength;
26383859Sml29623 		rx_dmap[i].nblocks = alloc_sizes[size_index] / block_size;
26393859Sml29623 		rx_dmap[i].dma_channel = dma_channel;
26403859Sml29623 		rx_dmap[i].contig_alloc_type = B_FALSE;
26416495Sspeer 		rx_dmap[i].kmem_alloc_type = B_FALSE;
26426495Sspeer 		rx_dmap[i].buf_alloc_type = DDI_MEM_ALLOC;
26433859Sml29623 
26443859Sml29623 		/*
26453859Sml29623 		 * N2/NIU: data buffers must be contiguous as the driver
26463859Sml29623 		 *	   needs to call Hypervisor api to set up
26473859Sml29623 		 *	   logical pages.
26483859Sml29623 		 */
26493859Sml29623 		if ((nxgep->niu_type == N2_NIU) && (NXGE_DMA_BLOCK == 1)) {
26503859Sml29623 			rx_dmap[i].contig_alloc_type = B_TRUE;
26516495Sspeer 			rx_dmap[i].buf_alloc_type = CONTIG_MEM_ALLOC;
26526495Sspeer 		} else if (use_kmem_alloc) {
26536495Sspeer 			/* For Neptune, use kmem_alloc */
26546495Sspeer 			NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26556495Sspeer 			    "==> nxge_alloc_rx_buf_dma: "
26566495Sspeer 			    "Neptune use kmem_alloc()"));
26576495Sspeer 			rx_dmap[i].kmem_alloc_type = B_TRUE;
26586495Sspeer 			rx_dmap[i].buf_alloc_type = KMEM_ALLOC;
26593859Sml29623 		}
26603859Sml29623 
26613859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26626512Ssowmini 		    "alloc_rx_buf_dma rdc %d chunk %d bufp %llx size %x "
26636512Ssowmini 		    "i %d nblocks %d alength %d",
26646512Ssowmini 		    dma_channel, i, &rx_dmap[i], block_size,
26656512Ssowmini 		    i, rx_dmap[i].nblocks,
26666512Ssowmini 		    rx_dmap[i].alength));
26673859Sml29623 		status = nxge_dma_mem_alloc(nxgep, nxge_force_dma,
26686512Ssowmini 		    &nxge_rx_dma_attr,
26696512Ssowmini 		    rx_dmap[i].alength,
26706512Ssowmini 		    &nxge_dev_buf_dma_acc_attr,
26716512Ssowmini 		    DDI_DMA_READ | DDI_DMA_STREAMING,
26726512Ssowmini 		    (p_nxge_dma_common_t)(&rx_dmap[i]));
26733859Sml29623 		if (status != NXGE_OK) {
26743859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
26756495Sspeer 			    "nxge_alloc_rx_buf_dma: Alloc Failed: "
26766495Sspeer 			    "dma %d size_index %d size requested %d",
26776495Sspeer 			    dma_channel,
26786495Sspeer 			    size_index,
26796495Sspeer 			    rx_dmap[i].alength));
26803859Sml29623 			size_index--;
26813859Sml29623 		} else {
26826495Sspeer 			rx_dmap[i].buf_alloc_state = BUF_ALLOCATED;
26836495Sspeer 			NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26846495Sspeer 			    " nxge_alloc_rx_buf_dma DONE  alloc mem: "
26856495Sspeer 			    "dma %d dma_buf_p $%p kaddrp $%p alength %d "
26866495Sspeer 			    "buf_alloc_state %d alloc_type %d",
26876495Sspeer 			    dma_channel,
26886495Sspeer 			    &rx_dmap[i],
26896495Sspeer 			    rx_dmap[i].kaddrp,
26906495Sspeer 			    rx_dmap[i].alength,
26916495Sspeer 			    rx_dmap[i].buf_alloc_state,
26926495Sspeer 			    rx_dmap[i].buf_alloc_type));
26936495Sspeer 			NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26946495Sspeer 			    " alloc_rx_buf_dma allocated rdc %d "
26956495Sspeer 			    "chunk %d size %x dvma %x bufp %llx kaddrp $%p",
26966495Sspeer 			    dma_channel, i, rx_dmap[i].alength,
26976495Sspeer 			    rx_dmap[i].ioaddr_pp, &rx_dmap[i],
26986495Sspeer 			    rx_dmap[i].kaddrp));
26993859Sml29623 			i++;
27003859Sml29623 			allocated += alloc_sizes[size_index];
27013859Sml29623 		}
27023859Sml29623 	}
27033859Sml29623 
27043859Sml29623 	if (allocated < total_alloc_size) {
27055770Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
27066495Sspeer 		    "==> nxge_alloc_rx_buf_dma: not enough for channel %d "
27075770Sml29623 		    "allocated 0x%x requested 0x%x",
27085770Sml29623 		    dma_channel,
27095770Sml29623 		    allocated, total_alloc_size));
27105770Sml29623 		status = NXGE_ERROR;
27113859Sml29623 		goto nxge_alloc_rx_mem_fail1;
27123859Sml29623 	}
27133859Sml29623 
27145770Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
27156495Sspeer 	    "==> nxge_alloc_rx_buf_dma: Allocated for channel %d "
27165770Sml29623 	    "allocated 0x%x requested 0x%x",
27175770Sml29623 	    dma_channel,
27185770Sml29623 	    allocated, total_alloc_size));
27195770Sml29623 
27203859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
27216512Ssowmini 	    " alloc_rx_buf_dma rdc %d allocated %d chunks",
27226512Ssowmini 	    dma_channel, i));
27233859Sml29623 	*num_chunks = i;
27243859Sml29623 	*dmap = rx_dmap;
27253859Sml29623 
27263859Sml29623 	goto nxge_alloc_rx_mem_exit;
27273859Sml29623 
27283859Sml29623 nxge_alloc_rx_mem_fail1:
27293859Sml29623 	KMEM_FREE(rx_dmap, sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK);
27303859Sml29623 
27313859Sml29623 nxge_alloc_rx_mem_exit:
27323859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
27336512Ssowmini 	    "<== nxge_alloc_rx_buf_dma status 0x%08x", status));
27343859Sml29623 
27353859Sml29623 	return (status);
27363859Sml29623 }
27373859Sml29623 
27383859Sml29623 /*ARGSUSED*/
27393859Sml29623 static void
27403859Sml29623 nxge_free_rx_buf_dma(p_nxge_t nxgep, p_nxge_dma_common_t dmap,
27413859Sml29623     uint32_t num_chunks)
27423859Sml29623 {
27433859Sml29623 	int		i;
27443859Sml29623 
27453859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
27466512Ssowmini 	    "==> nxge_free_rx_buf_dma: # of chunks %d", num_chunks));
27473859Sml29623 
27486495Sspeer 	if (dmap == 0)
27496495Sspeer 		return;
27506495Sspeer 
27513859Sml29623 	for (i = 0; i < num_chunks; i++) {
27523859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
27536512Ssowmini 		    "==> nxge_free_rx_buf_dma: chunk %d dmap 0x%llx",
27546512Ssowmini 		    i, dmap));
27556495Sspeer 		nxge_dma_free_rx_data_buf(dmap++);
27563859Sml29623 	}
27573859Sml29623 
27583859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_free_rx_buf_dma"));
27593859Sml29623 }
27603859Sml29623 
27613859Sml29623 /*ARGSUSED*/
27623859Sml29623 static nxge_status_t
27633859Sml29623 nxge_alloc_rx_cntl_dma(p_nxge_t nxgep, uint16_t dma_channel,
27643859Sml29623     p_nxge_dma_common_t *dmap, size_t size)
27653859Sml29623 {
27663859Sml29623 	p_nxge_dma_common_t 	rx_dmap;
27673859Sml29623 	nxge_status_t		status = NXGE_OK;
27683859Sml29623 
27693859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rx_cntl_dma"));
27703859Sml29623 
27713859Sml29623 	rx_dmap = (p_nxge_dma_common_t)
27726512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_common_t), KM_SLEEP);
27733859Sml29623 
27743859Sml29623 	rx_dmap->contig_alloc_type = B_FALSE;
27756495Sspeer 	rx_dmap->kmem_alloc_type = B_FALSE;
27763859Sml29623 
27773859Sml29623 	status = nxge_dma_mem_alloc(nxgep, nxge_force_dma,
27786512Ssowmini 	    &nxge_desc_dma_attr,
27796512Ssowmini 	    size,
27806512Ssowmini 	    &nxge_dev_desc_dma_acc_attr,
27816512Ssowmini 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
27826512Ssowmini 	    rx_dmap);
27833859Sml29623 	if (status != NXGE_OK) {
27843859Sml29623 		goto nxge_alloc_rx_cntl_dma_fail1;
27853859Sml29623 	}
27863859Sml29623 
27873859Sml29623 	*dmap = rx_dmap;
27883859Sml29623 	goto nxge_alloc_rx_cntl_dma_exit;
27893859Sml29623 
27903859Sml29623 nxge_alloc_rx_cntl_dma_fail1:
27913859Sml29623 	KMEM_FREE(rx_dmap, sizeof (nxge_dma_common_t));
27923859Sml29623 
27933859Sml29623 nxge_alloc_rx_cntl_dma_exit:
27943859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
27956512Ssowmini 	    "<== nxge_alloc_rx_cntl_dma status 0x%08x", status));
27963859Sml29623 
27973859Sml29623 	return (status);
27983859Sml29623 }
27993859Sml29623 
28003859Sml29623 /*ARGSUSED*/
28013859Sml29623 static void
28023859Sml29623 nxge_free_rx_cntl_dma(p_nxge_t nxgep, p_nxge_dma_common_t dmap)
28033859Sml29623 {
28043859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_free_rx_cntl_dma"));
28053859Sml29623 
28066495Sspeer 	if (dmap == 0)
28076495Sspeer 		return;
28086495Sspeer 
28093859Sml29623 	nxge_dma_mem_free(dmap);
28103859Sml29623 
28113859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_free_rx_cntl_dma"));
28123859Sml29623 }
28133859Sml29623 
28146495Sspeer typedef struct {
28156495Sspeer 	size_t	tx_size;
28166495Sspeer 	size_t	cr_size;
28176495Sspeer 	size_t	threshhold;
28186495Sspeer } nxge_tdc_sizes_t;
28196495Sspeer 
28206495Sspeer static
28216495Sspeer nxge_status_t
28226495Sspeer nxge_tdc_sizes(
28236495Sspeer 	nxge_t *nxgep,
28246495Sspeer 	nxge_tdc_sizes_t *sizes)
28256495Sspeer {
28266495Sspeer 	uint32_t threshhold;	/* The bcopy() threshhold */
28276495Sspeer 	size_t tx_size;		/* Transmit buffer size */
28286495Sspeer 	size_t cr_size;		/* Completion ring size */
28296495Sspeer 
28306495Sspeer 	/*
28316495Sspeer 	 * Assume that each DMA channel will be configured with the
28326495Sspeer 	 * default transmit buffer size for copying transmit data.
28336495Sspeer 	 * (If a packet is bigger than this, it will not be copied.)
28346495Sspeer 	 */
28356495Sspeer 	if (nxgep->niu_type == N2_NIU) {
28366495Sspeer 		threshhold = TX_BCOPY_SIZE;
28376495Sspeer 	} else {
28386495Sspeer 		threshhold = nxge_bcopy_thresh;
28396495Sspeer 	}
28406495Sspeer 	tx_size = nxge_tx_ring_size * threshhold;
28416495Sspeer 
28426495Sspeer 	cr_size = nxge_tx_ring_size * sizeof (tx_desc_t);
28436495Sspeer 	cr_size += sizeof (txdma_mailbox_t);
28446495Sspeer 
28456495Sspeer #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
28466495Sspeer 	if (nxgep->niu_type == N2_NIU) {
28476495Sspeer 		if (!ISP2(tx_size)) {
28486495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
28496512Ssowmini 			    "==> nxge_tdc_sizes: Tx size"
28506512Ssowmini 			    " must be power of 2"));
28516495Sspeer 			return (NXGE_ERROR);
28526495Sspeer 		}
28536495Sspeer 
28546495Sspeer 		if (tx_size > (1 << 22)) {
28556495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
28566512Ssowmini 			    "==> nxge_tdc_sizes: Tx size"
28576512Ssowmini 			    " limited to 4M"));
28586495Sspeer 			return (NXGE_ERROR);
28596495Sspeer 		}
28606495Sspeer 
28616495Sspeer 		if (cr_size < 0x2000)
28626495Sspeer 			cr_size = 0x2000;
28636495Sspeer 	}
28646495Sspeer #endif
28656495Sspeer 
28666495Sspeer 	sizes->threshhold = threshhold;
28676495Sspeer 	sizes->tx_size = tx_size;
28686495Sspeer 	sizes->cr_size = cr_size;
28696495Sspeer 
28706495Sspeer 	return (NXGE_OK);
28716495Sspeer }
28726495Sspeer /*
28736495Sspeer  * nxge_alloc_txb
28746495Sspeer  *
28756495Sspeer  *	Allocate buffers for an TDC.
28766495Sspeer  *
28776495Sspeer  * Arguments:
28786495Sspeer  * 	nxgep
28796495Sspeer  * 	channel	The channel to map into our kernel space.
28806495Sspeer  *
28816495Sspeer  * Notes:
28826495Sspeer  *
28836495Sspeer  * NPI function calls:
28846495Sspeer  *
28856495Sspeer  * NXGE function calls:
28866495Sspeer  *
28876495Sspeer  * Registers accessed:
28886495Sspeer  *
28896495Sspeer  * Context:
28906495Sspeer  *
28916495Sspeer  * Taking apart:
28926495Sspeer  *
28936495Sspeer  * Open questions:
28946495Sspeer  *
28956495Sspeer  */
28966495Sspeer nxge_status_t
28976495Sspeer nxge_alloc_txb(
28986495Sspeer 	p_nxge_t nxgep,
28996495Sspeer 	int channel)
29006495Sspeer {
29016495Sspeer 	nxge_dma_common_t	**dma_buf_p;
29026495Sspeer 	nxge_dma_common_t	**dma_cntl_p;
29036495Sspeer 	uint32_t 		*num_chunks;
29046495Sspeer 	nxge_status_t		status = NXGE_OK;
29056495Sspeer 
29066495Sspeer 	nxge_tdc_sizes_t	sizes;
29076495Sspeer 
29086495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_tbb"));
29096495Sspeer 
29106495Sspeer 	if (nxge_tdc_sizes(nxgep, &sizes) != NXGE_OK)
29116495Sspeer 		return (NXGE_ERROR);
29126495Sspeer 
29136495Sspeer 	/*
29146495Sspeer 	 * Allocate memory for transmit buffers and descriptor rings.
29156495Sspeer 	 * Replace these allocation functions with the interface functions
29166495Sspeer 	 * provided by the partition manager Real Soon Now.
29176495Sspeer 	 */
29186495Sspeer 	dma_buf_p = &nxgep->tx_buf_pool_p->dma_buf_pool_p[channel];
29196495Sspeer 	num_chunks = &nxgep->tx_buf_pool_p->num_chunks[channel];
29206495Sspeer 
29216495Sspeer 	dma_cntl_p = &nxgep->tx_cntl_pool_p->dma_buf_pool_p[channel];
29226495Sspeer 
29236495Sspeer 	/*
29246495Sspeer 	 * Allocate memory for transmit buffers and descriptor rings.
29256495Sspeer 	 * Replace allocation functions with interface functions provided
29266495Sspeer 	 * by the partition manager when it is available.
29276495Sspeer 	 *
29286495Sspeer 	 * Allocate memory for the transmit buffer pool.
29296495Sspeer 	 */
29306495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
29316512Ssowmini 	    "sizes: tx: %ld, cr:%ld, th:%ld",
29326512Ssowmini 	    sizes.tx_size, sizes.cr_size, sizes.threshhold));
29336495Sspeer 
29346495Sspeer 	*num_chunks = 0;
29356495Sspeer 	status = nxge_alloc_tx_buf_dma(nxgep, channel, dma_buf_p,
29366495Sspeer 	    sizes.tx_size, sizes.threshhold, num_chunks);
29376495Sspeer 	if (status != NXGE_OK) {
29386495Sspeer 		cmn_err(CE_NOTE, "nxge_alloc_tx_buf_dma failed!");
29396495Sspeer 		return (status);
29406495Sspeer 	}
29416495Sspeer 
29426495Sspeer 	/*
29436495Sspeer 	 * Allocate memory for descriptor rings and mailbox.
29446495Sspeer 	 */
29456495Sspeer 	status = nxge_alloc_tx_cntl_dma(nxgep, channel, dma_cntl_p,
29466495Sspeer 	    sizes.cr_size);
29476495Sspeer 	if (status != NXGE_OK) {
29486495Sspeer 		nxge_free_tx_buf_dma(nxgep, *dma_buf_p, *num_chunks);
29496495Sspeer 		cmn_err(CE_NOTE, "nxge_alloc_tx_cntl_dma failed!");
29506495Sspeer 		return (status);
29516495Sspeer 	}
29526495Sspeer 
29536495Sspeer 	return (NXGE_OK);
29546495Sspeer }
29556495Sspeer 
29566495Sspeer void
29576495Sspeer nxge_free_txb(
29586495Sspeer 	p_nxge_t nxgep,
29596495Sspeer 	int channel)
29606495Sspeer {
29616495Sspeer 	nxge_dma_common_t	*data;
29626495Sspeer 	nxge_dma_common_t	*control;
29636495Sspeer 	uint32_t 		num_chunks;
29646495Sspeer 
29656495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_free_txb"));
29666495Sspeer 
29676495Sspeer 	data = nxgep->tx_buf_pool_p->dma_buf_pool_p[channel];
29686495Sspeer 	num_chunks = nxgep->tx_buf_pool_p->num_chunks[channel];
29696495Sspeer 	nxge_free_tx_buf_dma(nxgep, data, num_chunks);
29706495Sspeer 
29716495Sspeer 	nxgep->tx_buf_pool_p->dma_buf_pool_p[channel] = 0;
29726495Sspeer 	nxgep->tx_buf_pool_p->num_chunks[channel] = 0;
29736495Sspeer 
29746495Sspeer 	control = nxgep->tx_cntl_pool_p->dma_buf_pool_p[channel];
29756495Sspeer 	nxge_free_tx_cntl_dma(nxgep, control);
29766495Sspeer 
29776495Sspeer 	nxgep->tx_cntl_pool_p->dma_buf_pool_p[channel] = 0;
29786495Sspeer 
29796495Sspeer 	KMEM_FREE(data, sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK);
29806495Sspeer 	KMEM_FREE(control, sizeof (nxge_dma_common_t));
29816495Sspeer 
29826495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_free_txb"));
29836495Sspeer }
29846495Sspeer 
29856495Sspeer /*
29866495Sspeer  * nxge_alloc_tx_mem_pool
29876495Sspeer  *
29886495Sspeer  *	This function allocates all of the per-port TDC control data structures.
29896495Sspeer  *	The per-channel (TDC) data structures are allocated when needed.
29906495Sspeer  *
29916495Sspeer  * Arguments:
29926495Sspeer  * 	nxgep
29936495Sspeer  *
29946495Sspeer  * Notes:
29956495Sspeer  *
29966495Sspeer  * Context:
29976495Sspeer  *	Any domain
29986495Sspeer  */
29996495Sspeer nxge_status_t
30003859Sml29623 nxge_alloc_tx_mem_pool(p_nxge_t nxgep)
30013859Sml29623 {
30026495Sspeer 	nxge_hw_pt_cfg_t	*p_cfgp;
30036495Sspeer 	nxge_dma_pool_t		*dma_poolp;
30046495Sspeer 	nxge_dma_common_t	**dma_buf_p;
30056495Sspeer 	nxge_dma_pool_t		*dma_cntl_poolp;
30066495Sspeer 	nxge_dma_common_t	**dma_cntl_p;
30073859Sml29623 	uint32_t		*num_chunks; /* per dma */
30086495Sspeer 	int			tdc_max;
30093859Sml29623 
30103859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "==> nxge_alloc_tx_mem_pool"));
30113859Sml29623 
30126495Sspeer 	p_cfgp = &nxgep->pt_config.hw_config;
30136495Sspeer 	tdc_max = NXGE_MAX_TDCS;
30146495Sspeer 
30153859Sml29623 	/*
30163859Sml29623 	 * Allocate memory for each transmit DMA channel.
30173859Sml29623 	 */
30183859Sml29623 	dma_poolp = (p_nxge_dma_pool_t)KMEM_ZALLOC(sizeof (nxge_dma_pool_t),
30196512Ssowmini 	    KM_SLEEP);
30203859Sml29623 	dma_buf_p = (p_nxge_dma_common_t *)KMEM_ZALLOC(
30216512Ssowmini 	    sizeof (p_nxge_dma_common_t) * tdc_max, KM_SLEEP);
30223859Sml29623 
30233859Sml29623 	dma_cntl_poolp = (p_nxge_dma_pool_t)
30246512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_pool_t), KM_SLEEP);
30253859Sml29623 	dma_cntl_p = (p_nxge_dma_common_t *)KMEM_ZALLOC(
30266512Ssowmini 	    sizeof (p_nxge_dma_common_t) * tdc_max, KM_SLEEP);
30273859Sml29623 
30285770Sml29623 	if (nxge_tx_ring_size > TDC_DEFAULT_MAX) {
30295770Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM_CTL,
30305770Sml29623 		    "nxge_alloc_tx_mem_pool: TDC too high %d, "
30315770Sml29623 		    "set to default %d",
30325770Sml29623 		    nxge_tx_ring_size, TDC_DEFAULT_MAX));
30335770Sml29623 		nxge_tx_ring_size = TDC_DEFAULT_MAX;
30345770Sml29623 	}
30355770Sml29623 
30363859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
30373859Sml29623 	/*
30383859Sml29623 	 * N2/NIU has limitation on the descriptor sizes (contiguous
30393859Sml29623 	 * memory allocation on data buffers to 4M (contig_mem_alloc)
30403859Sml29623 	 * and little endian for control buffers (must use the ddi/dki mem alloc
30413859Sml29623 	 * function). The transmit ring is limited to 8K (includes the
30423859Sml29623 	 * mailbox).
30433859Sml29623 	 */
30443859Sml29623 	if (nxgep->niu_type == N2_NIU) {
30453859Sml29623 		if ((nxge_tx_ring_size > NXGE_NIU_CONTIG_TX_MAX) ||
30466512Ssowmini 		    (!ISP2(nxge_tx_ring_size))) {
30473859Sml29623 			nxge_tx_ring_size = NXGE_NIU_CONTIG_TX_MAX;
30483859Sml29623 		}
30493859Sml29623 	}
30503859Sml29623 #endif
30513859Sml29623 
30523859Sml29623 	nxgep->nxge_port_tx_ring_size = nxge_tx_ring_size;
30533859Sml29623 
30543859Sml29623 	num_chunks = (uint32_t *)KMEM_ZALLOC(
30556512Ssowmini 	    sizeof (uint32_t) * tdc_max, KM_SLEEP);
30566495Sspeer 
30576495Sspeer 	dma_poolp->ndmas = p_cfgp->tdc.owned;
30583859Sml29623 	dma_poolp->num_chunks = num_chunks;
30593859Sml29623 	dma_poolp->dma_buf_pool_p = dma_buf_p;
30603859Sml29623 	nxgep->tx_buf_pool_p = dma_poolp;
30613859Sml29623 
30626495Sspeer 	dma_poolp->buf_allocated = B_TRUE;
30636495Sspeer 
30646495Sspeer 	dma_cntl_poolp->ndmas = p_cfgp->tdc.owned;
30653859Sml29623 	dma_cntl_poolp->dma_buf_pool_p = dma_cntl_p;
30663859Sml29623 	nxgep->tx_cntl_pool_p = dma_cntl_poolp;
30673859Sml29623 
30686495Sspeer 	dma_cntl_poolp->buf_allocated = B_TRUE;
30696495Sspeer 
30706495Sspeer 	nxgep->tx_rings =
30716495Sspeer 	    KMEM_ZALLOC(sizeof (tx_rings_t), KM_SLEEP);
30726495Sspeer 	nxgep->tx_rings->rings =
30736495Sspeer 	    KMEM_ZALLOC(sizeof (p_tx_ring_t) * tdc_max, KM_SLEEP);
30746495Sspeer 	nxgep->tx_mbox_areas_p =
30756495Sspeer 	    KMEM_ZALLOC(sizeof (tx_mbox_areas_t), KM_SLEEP);
30766495Sspeer 	nxgep->tx_mbox_areas_p->txmbox_areas_p =
30776495Sspeer 	    KMEM_ZALLOC(sizeof (p_tx_mbox_t) * tdc_max, KM_SLEEP);
30786495Sspeer 
30796495Sspeer 	nxgep->tx_rings->ndmas = p_cfgp->tdc.owned;
30806495Sspeer 
30813859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL,
30826512Ssowmini 	    "==> nxge_alloc_tx_mem_pool: ndmas %d poolp->ndmas %d",
30836512Ssowmini 	    tdc_max, dma_poolp->ndmas));
30846495Sspeer 
30856495Sspeer 	return (NXGE_OK);
30863859Sml29623 }
30873859Sml29623 
30886495Sspeer nxge_status_t
30893859Sml29623 nxge_alloc_tx_buf_dma(p_nxge_t nxgep, uint16_t dma_channel,
30903859Sml29623     p_nxge_dma_common_t *dmap, size_t alloc_size,
30913859Sml29623     size_t block_size, uint32_t *num_chunks)
30923859Sml29623 {
30933859Sml29623 	p_nxge_dma_common_t 	tx_dmap;
30943859Sml29623 	nxge_status_t		status = NXGE_OK;
30953859Sml29623 	size_t			total_alloc_size;
30963859Sml29623 	size_t			allocated = 0;
30973859Sml29623 	int			i, size_index, array_size;
30983859Sml29623 
30993859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_tx_buf_dma"));
31003859Sml29623 
31013859Sml29623 	tx_dmap = (p_nxge_dma_common_t)
31026512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK,
31036512Ssowmini 	    KM_SLEEP);
31043859Sml29623 
31053859Sml29623 	total_alloc_size = alloc_size;
31063859Sml29623 	i = 0;
31073859Sml29623 	size_index = 0;
31083859Sml29623 	array_size =  sizeof (alloc_sizes) /  sizeof (size_t);
31098661SSantwona.Behera@Sun.COM 	while ((size_index < array_size) &&
31108661SSantwona.Behera@Sun.COM 	    (alloc_sizes[size_index] < alloc_size))
31113859Sml29623 		size_index++;
31123859Sml29623 	if (size_index >= array_size) {
31133859Sml29623 		size_index = array_size - 1;
31143859Sml29623 	}
31153859Sml29623 
31163859Sml29623 	while ((allocated < total_alloc_size) &&
31176512Ssowmini 	    (size_index >= 0) && (i < NXGE_DMA_BLOCK)) {
31183859Sml29623 
31193859Sml29623 		tx_dmap[i].dma_chunk_index = i;
31203859Sml29623 		tx_dmap[i].block_size = block_size;
31213859Sml29623 		tx_dmap[i].alength = alloc_sizes[size_index];
31223859Sml29623 		tx_dmap[i].orig_alength = tx_dmap[i].alength;
31233859Sml29623 		tx_dmap[i].nblocks = alloc_sizes[size_index] / block_size;
31243859Sml29623 		tx_dmap[i].dma_channel = dma_channel;
31253859Sml29623 		tx_dmap[i].contig_alloc_type = B_FALSE;
31266495Sspeer 		tx_dmap[i].kmem_alloc_type = B_FALSE;
31273859Sml29623 
31283859Sml29623 		/*
31293859Sml29623 		 * N2/NIU: data buffers must be contiguous as the driver
31303859Sml29623 		 *	   needs to call Hypervisor api to set up
31313859Sml29623 		 *	   logical pages.
31323859Sml29623 		 */
31333859Sml29623 		if ((nxgep->niu_type == N2_NIU) && (NXGE_DMA_BLOCK == 1)) {
31343859Sml29623 			tx_dmap[i].contig_alloc_type = B_TRUE;
31353859Sml29623 		}
31363859Sml29623 
31373859Sml29623 		status = nxge_dma_mem_alloc(nxgep, nxge_force_dma,
31386512Ssowmini 		    &nxge_tx_dma_attr,
31396512Ssowmini 		    tx_dmap[i].alength,
31406512Ssowmini 		    &nxge_dev_buf_dma_acc_attr,
31416512Ssowmini 		    DDI_DMA_WRITE | DDI_DMA_STREAMING,
31426512Ssowmini 		    (p_nxge_dma_common_t)(&tx_dmap[i]));
31433859Sml29623 		if (status != NXGE_OK) {
31443859Sml29623 			size_index--;
31453859Sml29623 		} else {
31463859Sml29623 			i++;
31473859Sml29623 			allocated += alloc_sizes[size_index];
31483859Sml29623 		}
31493859Sml29623 	}
31503859Sml29623 
31513859Sml29623 	if (allocated < total_alloc_size) {
31525770Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
31535770Sml29623 		    "==> nxge_alloc_tx_buf_dma: not enough channel %d: "
31545770Sml29623 		    "allocated 0x%x requested 0x%x",
31555770Sml29623 		    dma_channel,
31565770Sml29623 		    allocated, total_alloc_size));
31575770Sml29623 		status = NXGE_ERROR;
31583859Sml29623 		goto nxge_alloc_tx_mem_fail1;
31593859Sml29623 	}
31603859Sml29623 
31615770Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
31625770Sml29623 	    "==> nxge_alloc_tx_buf_dma: Allocated for channel %d: "
31635770Sml29623 	    "allocated 0x%x requested 0x%x",
31645770Sml29623 	    dma_channel,
31655770Sml29623 	    allocated, total_alloc_size));
31665770Sml29623 
31673859Sml29623 	*num_chunks = i;
31683859Sml29623 	*dmap = tx_dmap;
31693859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
31706512Ssowmini 	    "==> nxge_alloc_tx_buf_dma dmap 0x%016llx num chunks %d",
31716512Ssowmini 	    *dmap, i));
31723859Sml29623 	goto nxge_alloc_tx_mem_exit;
31733859Sml29623 
31743859Sml29623 nxge_alloc_tx_mem_fail1:
31753859Sml29623 	KMEM_FREE(tx_dmap, sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK);
31763859Sml29623 
31773859Sml29623 nxge_alloc_tx_mem_exit:
31783859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
31796512Ssowmini 	    "<== nxge_alloc_tx_buf_dma status 0x%08x", status));
31803859Sml29623 
31813859Sml29623 	return (status);
31823859Sml29623 }
31833859Sml29623 
31843859Sml29623 /*ARGSUSED*/
31853859Sml29623 static void
31863859Sml29623 nxge_free_tx_buf_dma(p_nxge_t nxgep, p_nxge_dma_common_t dmap,
31873859Sml29623     uint32_t num_chunks)
31883859Sml29623 {
31893859Sml29623 	int		i;
31903859Sml29623 
31913859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "==> nxge_free_tx_buf_dma"));
31923859Sml29623 
31936495Sspeer 	if (dmap == 0)
31946495Sspeer 		return;
31956495Sspeer 
31963859Sml29623 	for (i = 0; i < num_chunks; i++) {
31973859Sml29623 		nxge_dma_mem_free(dmap++);
31983859Sml29623 	}
31993859Sml29623 
32003859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "<== nxge_free_tx_buf_dma"));
32013859Sml29623 }
32023859Sml29623 
32033859Sml29623 /*ARGSUSED*/
32046495Sspeer nxge_status_t
32053859Sml29623 nxge_alloc_tx_cntl_dma(p_nxge_t nxgep, uint16_t dma_channel,
32063859Sml29623     p_nxge_dma_common_t *dmap, size_t size)
32073859Sml29623 {
32083859Sml29623 	p_nxge_dma_common_t 	tx_dmap;
32093859Sml29623 	nxge_status_t		status = NXGE_OK;
32103859Sml29623 
32113859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_tx_cntl_dma"));
32123859Sml29623 	tx_dmap = (p_nxge_dma_common_t)
32136512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_common_t), KM_SLEEP);
32143859Sml29623 
32153859Sml29623 	tx_dmap->contig_alloc_type = B_FALSE;
32166495Sspeer 	tx_dmap->kmem_alloc_type = B_FALSE;
32173859Sml29623 
32183859Sml29623 	status = nxge_dma_mem_alloc(nxgep, nxge_force_dma,
32196512Ssowmini 	    &nxge_desc_dma_attr,
32206512Ssowmini 	    size,
32216512Ssowmini 	    &nxge_dev_desc_dma_acc_attr,
32226512Ssowmini 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
32236512Ssowmini 	    tx_dmap);
32243859Sml29623 	if (status != NXGE_OK) {
32253859Sml29623 		goto nxge_alloc_tx_cntl_dma_fail1;
32263859Sml29623 	}
32273859Sml29623 
32283859Sml29623 	*dmap = tx_dmap;
32293859Sml29623 	goto nxge_alloc_tx_cntl_dma_exit;
32303859Sml29623 
32313859Sml29623 nxge_alloc_tx_cntl_dma_fail1:
32323859Sml29623 	KMEM_FREE(tx_dmap, sizeof (nxge_dma_common_t));
32333859Sml29623 
32343859Sml29623 nxge_alloc_tx_cntl_dma_exit:
32353859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
32366512Ssowmini 	    "<== nxge_alloc_tx_cntl_dma status 0x%08x", status));
32373859Sml29623 
32383859Sml29623 	return (status);
32393859Sml29623 }
32403859Sml29623 
32413859Sml29623 /*ARGSUSED*/
32423859Sml29623 static void
32433859Sml29623 nxge_free_tx_cntl_dma(p_nxge_t nxgep, p_nxge_dma_common_t dmap)
32443859Sml29623 {
32453859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_free_tx_cntl_dma"));
32463859Sml29623 
32476495Sspeer 	if (dmap == 0)
32486495Sspeer 		return;
32496495Sspeer 
32503859Sml29623 	nxge_dma_mem_free(dmap);
32513859Sml29623 
32523859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_free_tx_cntl_dma"));
32533859Sml29623 }
32543859Sml29623 
32556495Sspeer /*
32566495Sspeer  * nxge_free_tx_mem_pool
32576495Sspeer  *
32586495Sspeer  *	This function frees all of the per-port TDC control data structures.
32596495Sspeer  *	The per-channel (TDC) data structures are freed when the channel
32606495Sspeer  *	is stopped.
32616495Sspeer  *
32626495Sspeer  * Arguments:
32636495Sspeer  * 	nxgep
32646495Sspeer  *
32656495Sspeer  * Notes:
32666495Sspeer  *
32676495Sspeer  * Context:
32686495Sspeer  *	Any domain
32696495Sspeer  */
32703859Sml29623 static void
32713859Sml29623 nxge_free_tx_mem_pool(p_nxge_t nxgep)
32723859Sml29623 {
32736495Sspeer 	int tdc_max = NXGE_MAX_TDCS;
32746495Sspeer 
32756495Sspeer 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_free_tx_mem_pool"));
32766495Sspeer 
32776495Sspeer 	if (!nxgep->tx_buf_pool_p || !nxgep->tx_buf_pool_p->buf_allocated) {
32786495Sspeer 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
32796512Ssowmini 		    "<== nxge_free_tx_mem_pool "
32806512Ssowmini 		    "(null tx buf pool or buf not allocated"));
32813859Sml29623 		return;
32823859Sml29623 	}
32836495Sspeer 	if (!nxgep->tx_cntl_pool_p || !nxgep->tx_cntl_pool_p->buf_allocated) {
32846495Sspeer 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
32856512Ssowmini 		    "<== nxge_free_tx_mem_pool "
32866512Ssowmini 		    "(null tx cntl buf pool or cntl buf not allocated"));
32873859Sml29623 		return;
32883859Sml29623 	}
32893859Sml29623 
32906495Sspeer 	/* 1. Free the mailboxes. */
32916495Sspeer 	KMEM_FREE(nxgep->tx_mbox_areas_p->txmbox_areas_p,
32926495Sspeer 	    sizeof (p_tx_mbox_t) * tdc_max);
32936495Sspeer 	KMEM_FREE(nxgep->tx_mbox_areas_p, sizeof (tx_mbox_areas_t));
32946495Sspeer 
32956495Sspeer 	nxgep->tx_mbox_areas_p = 0;
32966495Sspeer 
32976495Sspeer 	/* 2. Free the transmit ring arrays. */
32986495Sspeer 	KMEM_FREE(nxgep->tx_rings->rings,
32996495Sspeer 	    sizeof (p_tx_ring_t) * tdc_max);
33006495Sspeer 	KMEM_FREE(nxgep->tx_rings, sizeof (tx_rings_t));
33016495Sspeer 
33026495Sspeer 	nxgep->tx_rings = 0;
33036495Sspeer 
33046495Sspeer 	/* 3. Free the completion ring data structures. */
33056495Sspeer 	KMEM_FREE(nxgep->tx_cntl_pool_p->dma_buf_pool_p,
33066495Sspeer 	    sizeof (p_nxge_dma_common_t) * tdc_max);
33076495Sspeer 	KMEM_FREE(nxgep->tx_cntl_pool_p, sizeof (nxge_dma_pool_t));
33086495Sspeer 
33096495Sspeer 	nxgep->tx_cntl_pool_p = 0;
33106495Sspeer 
33116495Sspeer 	/* 4. Free the data ring data structures. */
33126495Sspeer 	KMEM_FREE(nxgep->tx_buf_pool_p->num_chunks,
33136495Sspeer 	    sizeof (uint32_t) * tdc_max);
33146495Sspeer 	KMEM_FREE(nxgep->tx_buf_pool_p->dma_buf_pool_p,
33156495Sspeer 	    sizeof (p_nxge_dma_common_t) * tdc_max);
33166495Sspeer 	KMEM_FREE(nxgep->tx_buf_pool_p, sizeof (nxge_dma_pool_t));
33176495Sspeer 
33186495Sspeer 	nxgep->tx_buf_pool_p = 0;
33196495Sspeer 
33206495Sspeer 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "<== nxge_free_tx_mem_pool"));
33213859Sml29623 }
33223859Sml29623 
33233859Sml29623 /*ARGSUSED*/
33243859Sml29623 static nxge_status_t
33253859Sml29623 nxge_dma_mem_alloc(p_nxge_t nxgep, dma_method_t method,
33263859Sml29623 	struct ddi_dma_attr *dma_attrp,
33273859Sml29623 	size_t length, ddi_device_acc_attr_t *acc_attr_p, uint_t xfer_flags,
33283859Sml29623 	p_nxge_dma_common_t dma_p)
33293859Sml29623 {
33303859Sml29623 	caddr_t 		kaddrp;
33313859Sml29623 	int			ddi_status = DDI_SUCCESS;
33323859Sml29623 	boolean_t		contig_alloc_type;
33336495Sspeer 	boolean_t		kmem_alloc_type;
33343859Sml29623 
33353859Sml29623 	contig_alloc_type = dma_p->contig_alloc_type;
33363859Sml29623 
33373859Sml29623 	if (contig_alloc_type && (nxgep->niu_type != N2_NIU)) {
33383859Sml29623 		/*
33393859Sml29623 		 * contig_alloc_type for contiguous memory only allowed
33403859Sml29623 		 * for N2/NIU.
33413859Sml29623 		 */
33423859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33436512Ssowmini 		    "nxge_dma_mem_alloc: alloc type not allowed (%d)",
33446512Ssowmini 		    dma_p->contig_alloc_type));
33453859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
33463859Sml29623 	}
33473859Sml29623 
33483859Sml29623 	dma_p->dma_handle = NULL;
33493859Sml29623 	dma_p->acc_handle = NULL;
33503859Sml29623 	dma_p->kaddrp = dma_p->last_kaddrp = NULL;
33513859Sml29623 	dma_p->first_ioaddr_pp = dma_p->last_ioaddr_pp = NULL;
33523859Sml29623 	ddi_status = ddi_dma_alloc_handle(nxgep->dip, dma_attrp,
33536512Ssowmini 	    DDI_DMA_DONTWAIT, NULL, &dma_p->dma_handle);
33543859Sml29623 	if (ddi_status != DDI_SUCCESS) {
33553859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33566512Ssowmini 		    "nxge_dma_mem_alloc:ddi_dma_alloc_handle failed."));
33573859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
33583859Sml29623 	}
33593859Sml29623 
33606495Sspeer 	kmem_alloc_type = dma_p->kmem_alloc_type;
33616495Sspeer 
33623859Sml29623 	switch (contig_alloc_type) {
33633859Sml29623 	case B_FALSE:
33646495Sspeer 		switch (kmem_alloc_type) {
33656495Sspeer 		case B_FALSE:
33666495Sspeer 			ddi_status = ddi_dma_mem_alloc(dma_p->dma_handle,
33676512Ssowmini 			    length,
33686512Ssowmini 			    acc_attr_p,
33696512Ssowmini 			    xfer_flags,
33706512Ssowmini 			    DDI_DMA_DONTWAIT, 0, &kaddrp, &dma_p->alength,
33716512Ssowmini 			    &dma_p->acc_handle);
33726495Sspeer 			if (ddi_status != DDI_SUCCESS) {
33736495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33746495Sspeer 				    "nxge_dma_mem_alloc: "
33756495Sspeer 				    "ddi_dma_mem_alloc failed"));
33766495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
33776495Sspeer 				dma_p->dma_handle = NULL;
33786495Sspeer 				return (NXGE_ERROR | NXGE_DDI_FAILED);
33796495Sspeer 			}
33806495Sspeer 			if (dma_p->alength < length) {
33816495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33826495Sspeer 				    "nxge_dma_mem_alloc:di_dma_mem_alloc "
33836495Sspeer 				    "< length."));
33846495Sspeer 				ddi_dma_mem_free(&dma_p->acc_handle);
33856495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
33866495Sspeer 				dma_p->acc_handle = NULL;
33876495Sspeer 				dma_p->dma_handle = NULL;
33886495Sspeer 				return (NXGE_ERROR);
33896495Sspeer 			}
33906495Sspeer 
33916495Sspeer 			ddi_status = ddi_dma_addr_bind_handle(dma_p->dma_handle,
33926495Sspeer 			    NULL,
33936495Sspeer 			    kaddrp, dma_p->alength, xfer_flags,
33946495Sspeer 			    DDI_DMA_DONTWAIT,
33956495Sspeer 			    0, &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 				    "failed "
34006495Sspeer 				    "(staus 0x%x ncookies %d.)", ddi_status,
34016495Sspeer 				    dma_p->ncookies));
34026495Sspeer 				if (dma_p->acc_handle) {
34036495Sspeer 					ddi_dma_mem_free(&dma_p->acc_handle);
34046495Sspeer 					dma_p->acc_handle = NULL;
34056495Sspeer 				}
34066495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
34076495Sspeer 				dma_p->dma_handle = 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 				    "> 1 cookie"
34156495Sspeer 				    "(staus 0x%x ncookies %d.)", ddi_status,
34166495Sspeer 				    dma_p->ncookies));
34177812SMichael.Speer@Sun.COM 				(void) ddi_dma_unbind_handle(dma_p->dma_handle);
34186495Sspeer 				if (dma_p->acc_handle) {
34196495Sspeer 					ddi_dma_mem_free(&dma_p->acc_handle);
34206495Sspeer 					dma_p->acc_handle = NULL;
34216495Sspeer 				}
34226495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
34236495Sspeer 				dma_p->dma_handle = NULL;
34247812SMichael.Speer@Sun.COM 				dma_p->acc_handle = NULL;
34256495Sspeer 				return (NXGE_ERROR);
34266495Sspeer 			}
34276495Sspeer 			break;
34286495Sspeer 
34296495Sspeer 		case B_TRUE:
34306495Sspeer 			kaddrp = KMEM_ALLOC(length, KM_NOSLEEP);
34316495Sspeer 			if (kaddrp == NULL) {
34326495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
34336495Sspeer 				    "nxge_dma_mem_alloc:ddi_dma_mem_alloc "
34346495Sspeer 				    "kmem alloc failed"));
34356495Sspeer 				return (NXGE_ERROR);
34366495Sspeer 			}
34376495Sspeer 
34386495Sspeer 			dma_p->alength = length;
34396495Sspeer 			ddi_status = ddi_dma_addr_bind_handle(dma_p->dma_handle,
34406495Sspeer 			    NULL, kaddrp, dma_p->alength, xfer_flags,
34416495Sspeer 			    DDI_DMA_DONTWAIT, 0,
34426495Sspeer 			    &dma_p->dma_cookie, &dma_p->ncookies);
34436495Sspeer 			if (ddi_status != DDI_DMA_MAPPED) {
34446495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
34456495Sspeer 				    "nxge_dma_mem_alloc:ddi_dma_addr_bind: "
34466495Sspeer 				    "(kmem_alloc) failed kaddrp $%p length %d "
34476495Sspeer 				    "(staus 0x%x (%d) ncookies %d.)",
34486495Sspeer 				    kaddrp, length,
34496495Sspeer 				    ddi_status, ddi_status, dma_p->ncookies));
34506495Sspeer 				KMEM_FREE(kaddrp, length);
34516495Sspeer 				dma_p->acc_handle = NULL;
34526495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
34536495Sspeer 				dma_p->dma_handle = NULL;
34546495Sspeer 				dma_p->kaddrp = NULL;
34556495Sspeer 				return (NXGE_ERROR | NXGE_DDI_FAILED);
34566495Sspeer 			}
34576495Sspeer 
34586495Sspeer 			if (dma_p->ncookies != 1) {
34596495Sspeer 				NXGE_DEBUG_MSG((nxgep, DMA_CTL,
34606495Sspeer 				    "nxge_dma_mem_alloc:ddi_dma_addr_bind "
34616495Sspeer 				    "(kmem_alloc) > 1 cookie"
34626495Sspeer 				    "(staus 0x%x ncookies %d.)", ddi_status,
34636512Ssowmini 				    dma_p->ncookies));
34647812SMichael.Speer@Sun.COM 				(void) ddi_dma_unbind_handle(dma_p->dma_handle);
34656495Sspeer 				KMEM_FREE(kaddrp, length);
34666495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
34676495Sspeer 				dma_p->dma_handle = NULL;
34687812SMichael.Speer@Sun.COM 				dma_p->acc_handle = NULL;
34696495Sspeer 				dma_p->kaddrp = NULL;
34706495Sspeer 				return (NXGE_ERROR);
34713859Sml29623 			}
34726495Sspeer 
34736495Sspeer 			dma_p->kaddrp = kaddrp;
34746495Sspeer 
34756495Sspeer 			NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
34766512Ssowmini 			    "nxge_dma_mem_alloc: kmem_alloc dmap $%p "
34776512Ssowmini 			    "kaddr $%p alength %d",
34786512Ssowmini 			    dma_p,
34796512Ssowmini 			    kaddrp,
34806512Ssowmini 			    dma_p->alength));
34816495Sspeer 			break;
34823859Sml29623 		}
34833859Sml29623 		break;
34843859Sml29623 
34853859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
34863859Sml29623 	case B_TRUE:
34873859Sml29623 		kaddrp = (caddr_t)contig_mem_alloc(length);
34883859Sml29623 		if (kaddrp == NULL) {
34893859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
34906512Ssowmini 			    "nxge_dma_mem_alloc:contig_mem_alloc failed."));
34913859Sml29623 			ddi_dma_free_handle(&dma_p->dma_handle);
34923859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
34933859Sml29623 		}
34943859Sml29623 
34953859Sml29623 		dma_p->alength = length;
34963859Sml29623 		ddi_status = ddi_dma_addr_bind_handle(dma_p->dma_handle, NULL,
34976512Ssowmini 		    kaddrp, dma_p->alength, xfer_flags, DDI_DMA_DONTWAIT, 0,
34986512Ssowmini 		    &dma_p->dma_cookie, &dma_p->ncookies);
34993859Sml29623 		if (ddi_status != DDI_DMA_MAPPED) {
35003859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
35016512Ssowmini 			    "nxge_dma_mem_alloc:di_dma_addr_bind failed "
35026512Ssowmini 			    "(status 0x%x ncookies %d.)", ddi_status,
35036512Ssowmini 			    dma_p->ncookies));
35043859Sml29623 
35053859Sml29623 			NXGE_DEBUG_MSG((nxgep, DMA_CTL,
35066512Ssowmini 			    "==> nxge_dma_mem_alloc: (not mapped)"
35076512Ssowmini 			    "length %lu (0x%x) "
35086512Ssowmini 			    "free contig kaddrp $%p "
35096512Ssowmini 			    "va_to_pa $%p",
35106512Ssowmini 			    length, length,
35116512Ssowmini 			    kaddrp,
35126512Ssowmini 			    va_to_pa(kaddrp)));
35133859Sml29623 
35143859Sml29623 
35153859Sml29623 			contig_mem_free((void *)kaddrp, length);
35163859Sml29623 			ddi_dma_free_handle(&dma_p->dma_handle);
35173859Sml29623 
35183859Sml29623 			dma_p->dma_handle = NULL;
35193859Sml29623 			dma_p->acc_handle = NULL;
35203859Sml29623 			dma_p->alength = NULL;
35213859Sml29623 			dma_p->kaddrp = NULL;
35223859Sml29623 
35233859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
35243859Sml29623 		}
35253859Sml29623 
35263859Sml29623 		if (dma_p->ncookies != 1 ||
35276512Ssowmini 		    (dma_p->dma_cookie.dmac_laddress == NULL)) {
35283859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
35296512Ssowmini 			    "nxge_dma_mem_alloc:di_dma_addr_bind > 1 "
35306512Ssowmini 			    "cookie or "
35316512Ssowmini 			    "dmac_laddress is NULL $%p size %d "
35326512Ssowmini 			    " (status 0x%x ncookies %d.)",
35336512Ssowmini 			    ddi_status,
35346512Ssowmini 			    dma_p->dma_cookie.dmac_laddress,
35356512Ssowmini 			    dma_p->dma_cookie.dmac_size,
35366512Ssowmini 			    dma_p->ncookies));
35373859Sml29623 
35383859Sml29623 			contig_mem_free((void *)kaddrp, length);
35394185Sspeer 			(void) ddi_dma_unbind_handle(dma_p->dma_handle);
35403859Sml29623 			ddi_dma_free_handle(&dma_p->dma_handle);
35413859Sml29623 
35423859Sml29623 			dma_p->alength = 0;
35433859Sml29623 			dma_p->dma_handle = NULL;
35443859Sml29623 			dma_p->acc_handle = NULL;
35453859Sml29623 			dma_p->kaddrp = NULL;
35463859Sml29623 
35473859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
35483859Sml29623 		}
35493859Sml29623 		break;
35503859Sml29623 
35513859Sml29623 #else
35523859Sml29623 	case B_TRUE:
35533859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
35546512Ssowmini 		    "nxge_dma_mem_alloc: invalid alloc type for !sun4v"));
35553859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
35563859Sml29623 #endif
35573859Sml29623 	}
35583859Sml29623 
35593859Sml29623 	dma_p->kaddrp = kaddrp;
35603859Sml29623 	dma_p->last_kaddrp = (unsigned char *)kaddrp +
35616512Ssowmini 	    dma_p->alength - RXBUF_64B_ALIGNED;
35625125Sjoycey #if defined(__i386)
35635125Sjoycey 	dma_p->ioaddr_pp =
35646512Ssowmini 	    (unsigned char *)(uint32_t)dma_p->dma_cookie.dmac_laddress;
35655125Sjoycey #else
35663859Sml29623 	dma_p->ioaddr_pp = (unsigned char *)dma_p->dma_cookie.dmac_laddress;
35675125Sjoycey #endif
35683859Sml29623 	dma_p->last_ioaddr_pp =
35695125Sjoycey #if defined(__i386)
35706512Ssowmini 	    (unsigned char *)(uint32_t)dma_p->dma_cookie.dmac_laddress +
35715125Sjoycey #else
35726512Ssowmini 	    (unsigned char *)dma_p->dma_cookie.dmac_laddress +
35735125Sjoycey #endif
35746512Ssowmini 	    dma_p->alength - RXBUF_64B_ALIGNED;
35753859Sml29623 
35763859Sml29623 	NPI_DMA_ACC_HANDLE_SET(dma_p, dma_p->acc_handle);
35773859Sml29623 
35783859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
35793859Sml29623 	dma_p->orig_ioaddr_pp =
35806512Ssowmini 	    (unsigned char *)dma_p->dma_cookie.dmac_laddress;
35813859Sml29623 	dma_p->orig_alength = length;
35823859Sml29623 	dma_p->orig_kaddrp = kaddrp;
35833859Sml29623 	dma_p->orig_vatopa = (uint64_t)va_to_pa(kaddrp);
35843859Sml29623 #endif
35853859Sml29623 
35863859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_dma_mem_alloc: "
35876512Ssowmini 	    "dma buffer allocated: dma_p $%p "
35886512Ssowmini 	    "return dmac_ladress from cookie $%p cookie dmac_size %d "
35896512Ssowmini 	    "dma_p->ioaddr_p $%p "
35906512Ssowmini 	    "dma_p->orig_ioaddr_p $%p "
35916512Ssowmini 	    "orig_vatopa $%p "
35926512Ssowmini 	    "alength %d (0x%x) "
35936512Ssowmini 	    "kaddrp $%p "
35946512Ssowmini 	    "length %d (0x%x)",
35956512Ssowmini 	    dma_p,
35966512Ssowmini 	    dma_p->dma_cookie.dmac_laddress, dma_p->dma_cookie.dmac_size,
35976512Ssowmini 	    dma_p->ioaddr_pp,
35986512Ssowmini 	    dma_p->orig_ioaddr_pp,
35996512Ssowmini 	    dma_p->orig_vatopa,
36006512Ssowmini 	    dma_p->alength, dma_p->alength,
36016512Ssowmini 	    kaddrp,
36026512Ssowmini 	    length, length));
36033859Sml29623 
36043859Sml29623 	return (NXGE_OK);
36053859Sml29623 }
36063859Sml29623 
36073859Sml29623 static void
36083859Sml29623 nxge_dma_mem_free(p_nxge_dma_common_t dma_p)
36093859Sml29623 {
36103859Sml29623 	if (dma_p->dma_handle != NULL) {
36113859Sml29623 		if (dma_p->ncookies) {
36123859Sml29623 			(void) ddi_dma_unbind_handle(dma_p->dma_handle);
36133859Sml29623 			dma_p->ncookies = 0;
36143859Sml29623 		}
36153859Sml29623 		ddi_dma_free_handle(&dma_p->dma_handle);
36163859Sml29623 		dma_p->dma_handle = NULL;
36173859Sml29623 	}
36183859Sml29623 
36193859Sml29623 	if (dma_p->acc_handle != NULL) {
36203859Sml29623 		ddi_dma_mem_free(&dma_p->acc_handle);
36213859Sml29623 		dma_p->acc_handle = NULL;
36223859Sml29623 		NPI_DMA_ACC_HANDLE_SET(dma_p, NULL);
36233859Sml29623 	}
36243859Sml29623 
36253859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
36263859Sml29623 	if (dma_p->contig_alloc_type &&
36276512Ssowmini 	    dma_p->orig_kaddrp && dma_p->orig_alength) {
36283859Sml29623 		NXGE_DEBUG_MSG((NULL, DMA_CTL, "nxge_dma_mem_free: "
36296512Ssowmini 		    "kaddrp $%p (orig_kaddrp $%p)"
36306512Ssowmini 		    "mem type %d ",
36316512Ssowmini 		    "orig_alength %d "
36326512Ssowmini 		    "alength 0x%x (%d)",
36336512Ssowmini 		    dma_p->kaddrp,
36346512Ssowmini 		    dma_p->orig_kaddrp,
36356512Ssowmini 		    dma_p->contig_alloc_type,
36366512Ssowmini 		    dma_p->orig_alength,
36376512Ssowmini 		    dma_p->alength, dma_p->alength));
36383859Sml29623 
36393859Sml29623 		contig_mem_free(dma_p->orig_kaddrp, dma_p->orig_alength);
36403859Sml29623 		dma_p->orig_alength = NULL;
36413859Sml29623 		dma_p->orig_kaddrp = NULL;
36423859Sml29623 		dma_p->contig_alloc_type = B_FALSE;
36433859Sml29623 	}
36443859Sml29623 #endif
36453859Sml29623 	dma_p->kaddrp = NULL;
36463859Sml29623 	dma_p->alength = NULL;
36473859Sml29623 }
36483859Sml29623 
36496495Sspeer static void
36506495Sspeer nxge_dma_free_rx_data_buf(p_nxge_dma_common_t dma_p)
36516495Sspeer {
36526495Sspeer 	uint64_t kaddr;
36536495Sspeer 	uint32_t buf_size;
36546495Sspeer 
36556495Sspeer 	NXGE_DEBUG_MSG((NULL, DMA_CTL, "==> nxge_dma_free_rx_data_buf"));
36566495Sspeer 
36576495Sspeer 	if (dma_p->dma_handle != NULL) {
36586495Sspeer 		if (dma_p->ncookies) {
36596495Sspeer 			(void) ddi_dma_unbind_handle(dma_p->dma_handle);
36606495Sspeer 			dma_p->ncookies = 0;
36616495Sspeer 		}
36626495Sspeer 		ddi_dma_free_handle(&dma_p->dma_handle);
36636495Sspeer 		dma_p->dma_handle = NULL;
36646495Sspeer 	}
36656495Sspeer 
36666495Sspeer 	if (dma_p->acc_handle != NULL) {
36676495Sspeer 		ddi_dma_mem_free(&dma_p->acc_handle);
36686495Sspeer 		dma_p->acc_handle = NULL;
36696495Sspeer 		NPI_DMA_ACC_HANDLE_SET(dma_p, NULL);
36706495Sspeer 	}
36716495Sspeer 
36726495Sspeer 	NXGE_DEBUG_MSG((NULL, DMA_CTL,
36736495Sspeer 	    "==> nxge_dma_free_rx_data_buf: dmap $%p buf_alloc_state %d",
36746495Sspeer 	    dma_p,
36756495Sspeer 	    dma_p->buf_alloc_state));
36766495Sspeer 
36776495Sspeer 	if (!(dma_p->buf_alloc_state & BUF_ALLOCATED_WAIT_FREE)) {
36786495Sspeer 		NXGE_DEBUG_MSG((NULL, DMA_CTL,
36796495Sspeer 		    "<== nxge_dma_free_rx_data_buf: "
36806495Sspeer 		    "outstanding data buffers"));
36816495Sspeer 		return;
36826495Sspeer 	}
36836495Sspeer 
36846495Sspeer #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
36856495Sspeer 	if (dma_p->contig_alloc_type &&
36866512Ssowmini 	    dma_p->orig_kaddrp && dma_p->orig_alength) {
36876495Sspeer 		NXGE_DEBUG_MSG((NULL, DMA_CTL, "nxge_dma_free_rx_data_buf: "
36886495Sspeer 		    "kaddrp $%p (orig_kaddrp $%p)"
36896495Sspeer 		    "mem type %d ",
36906495Sspeer 		    "orig_alength %d "
36916495Sspeer 		    "alength 0x%x (%d)",
36926495Sspeer 		    dma_p->kaddrp,
36936495Sspeer 		    dma_p->orig_kaddrp,
36946495Sspeer 		    dma_p->contig_alloc_type,
36956495Sspeer 		    dma_p->orig_alength,
36966495Sspeer 		    dma_p->alength, dma_p->alength));
36976495Sspeer 
36986495Sspeer 		kaddr = (uint64_t)dma_p->orig_kaddrp;
36996495Sspeer 		buf_size = dma_p->orig_alength;
37006495Sspeer 		nxge_free_buf(CONTIG_MEM_ALLOC, kaddr, buf_size);
37016495Sspeer 		dma_p->orig_alength = NULL;
37026495Sspeer 		dma_p->orig_kaddrp = NULL;
37036495Sspeer 		dma_p->contig_alloc_type = B_FALSE;
37046495Sspeer 		dma_p->kaddrp = NULL;
37056495Sspeer 		dma_p->alength = NULL;
37066495Sspeer 		return;
37076495Sspeer 	}
37086495Sspeer #endif
37096495Sspeer 
37106495Sspeer 	if (dma_p->kmem_alloc_type) {
37116495Sspeer 		NXGE_DEBUG_MSG((NULL, DMA_CTL,
37126495Sspeer 		    "nxge_dma_free_rx_data_buf: free kmem "
37136512Ssowmini 		    "kaddrp $%p (orig_kaddrp $%p)"
37146512Ssowmini 		    "alloc type %d "
37156512Ssowmini 		    "orig_alength %d "
37166512Ssowmini 		    "alength 0x%x (%d)",
37176512Ssowmini 		    dma_p->kaddrp,
37186512Ssowmini 		    dma_p->orig_kaddrp,
37196512Ssowmini 		    dma_p->kmem_alloc_type,
37206512Ssowmini 		    dma_p->orig_alength,
37216512Ssowmini 		    dma_p->alength, dma_p->alength));
37226495Sspeer #if defined(__i386)
37236495Sspeer 		kaddr = (uint64_t)(uint32_t)dma_p->kaddrp;
37246495Sspeer #else
37256495Sspeer 		kaddr = (uint64_t)dma_p->kaddrp;
37266495Sspeer #endif
37276495Sspeer 		buf_size = dma_p->orig_alength;
37286495Sspeer 		NXGE_DEBUG_MSG((NULL, DMA_CTL,
37296495Sspeer 		    "nxge_dma_free_rx_data_buf: free dmap $%p "
37306495Sspeer 		    "kaddr $%p buf_size %d",
37316495Sspeer 		    dma_p,
37326495Sspeer 		    kaddr, buf_size));
37336495Sspeer 		nxge_free_buf(KMEM_ALLOC, kaddr, buf_size);
37346495Sspeer 		dma_p->alength = 0;
37356495Sspeer 		dma_p->orig_alength = 0;
37366495Sspeer 		dma_p->kaddrp = NULL;
37376495Sspeer 		dma_p->kmem_alloc_type = B_FALSE;
37386495Sspeer 	}
37396495Sspeer 
37406495Sspeer 	NXGE_DEBUG_MSG((NULL, DMA_CTL, "<== nxge_dma_free_rx_data_buf"));
37416495Sspeer }
37426495Sspeer 
37433859Sml29623 /*
37443859Sml29623  *	nxge_m_start() -- start transmitting and receiving.
37453859Sml29623  *
37463859Sml29623  *	This function is called by the MAC layer when the first
37473859Sml29623  *	stream is open to prepare the hardware ready for sending
37483859Sml29623  *	and transmitting packets.
37493859Sml29623  */
37503859Sml29623 static int
37513859Sml29623 nxge_m_start(void *arg)
37523859Sml29623 {
37533859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
37543859Sml29623 
37553859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_start"));
37563859Sml29623 
37579232SMichael.Speer@Sun.COM 	/*
37589232SMichael.Speer@Sun.COM 	 * Are we already started?
37599232SMichael.Speer@Sun.COM 	 */
37609232SMichael.Speer@Sun.COM 	if (nxgep->nxge_mac_state == NXGE_MAC_STARTED) {
37619232SMichael.Speer@Sun.COM 		return (0);
37629232SMichael.Speer@Sun.COM 	}
37639232SMichael.Speer@Sun.COM 
37646705Sml29623 	if (nxge_peu_reset_enable && !nxgep->nxge_link_poll_timerid) {
37656705Sml29623 		(void) nxge_link_monitor(nxgep, LINK_MONITOR_START);
37666705Sml29623 	}
37676705Sml29623 
37689232SMichael.Speer@Sun.COM 	/*
37699232SMichael.Speer@Sun.COM 	 * Make sure RX MAC is disabled while we initialize.
37709232SMichael.Speer@Sun.COM 	 */
37719232SMichael.Speer@Sun.COM 	if (!isLDOMguest(nxgep)) {
37729232SMichael.Speer@Sun.COM 		(void) nxge_rx_mac_disable(nxgep);
37739232SMichael.Speer@Sun.COM 	}
37749232SMichael.Speer@Sun.COM 
37759232SMichael.Speer@Sun.COM 	/*
37769232SMichael.Speer@Sun.COM 	 * Grab the global lock.
37779232SMichael.Speer@Sun.COM 	 */
37783859Sml29623 	MUTEX_ENTER(nxgep->genlock);
37799232SMichael.Speer@Sun.COM 
37809232SMichael.Speer@Sun.COM 	/*
37819232SMichael.Speer@Sun.COM 	 * Initialize the driver and hardware.
37829232SMichael.Speer@Sun.COM 	 */
37833859Sml29623 	if (nxge_init(nxgep) != NXGE_OK) {
37843859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
37856512Ssowmini 		    "<== nxge_m_start: initialization failed"));
37863859Sml29623 		MUTEX_EXIT(nxgep->genlock);
37873859Sml29623 		return (EIO);
37883859Sml29623 	}
37893859Sml29623 
37903859Sml29623 	/*
37913859Sml29623 	 * Start timer to check the system error and tx hangs
37923859Sml29623 	 */
37936495Sspeer 	if (!isLDOMguest(nxgep))
37946495Sspeer 		nxgep->nxge_timerid = nxge_start_timer(nxgep,
37956495Sspeer 		    nxge_check_hw_state, NXGE_CHECK_TIMER);
37969232SMichael.Speer@Sun.COM #if defined(sun4v)
37976495Sspeer 	else
37986495Sspeer 		nxge_hio_start_timer(nxgep);
37996495Sspeer #endif
38003859Sml29623 
38013859Sml29623 	nxgep->link_notify = B_TRUE;
38023859Sml29623 	nxgep->nxge_mac_state = NXGE_MAC_STARTED;
38033859Sml29623 
38049232SMichael.Speer@Sun.COM 	/*
38059232SMichael.Speer@Sun.COM 	 * Let the global lock go, since we are intialized.
38069232SMichael.Speer@Sun.COM 	 */
38073859Sml29623 	MUTEX_EXIT(nxgep->genlock);
38089232SMichael.Speer@Sun.COM 
38099232SMichael.Speer@Sun.COM 	/*
38109232SMichael.Speer@Sun.COM 	 * Let the MAC start receiving packets, now that
38119232SMichael.Speer@Sun.COM 	 * we are initialized.
38129232SMichael.Speer@Sun.COM 	 */
38139232SMichael.Speer@Sun.COM 	if (!isLDOMguest(nxgep)) {
38149232SMichael.Speer@Sun.COM 		if (nxge_rx_mac_enable(nxgep) != NXGE_OK) {
38159232SMichael.Speer@Sun.COM 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
38169232SMichael.Speer@Sun.COM 			    "<== nxge_m_start: enable of RX mac failed"));
38179232SMichael.Speer@Sun.COM 			return (EIO);
38189232SMichael.Speer@Sun.COM 		}
38199232SMichael.Speer@Sun.COM 
38209232SMichael.Speer@Sun.COM 		/*
38219232SMichael.Speer@Sun.COM 		 * Enable hardware interrupts.
38229232SMichael.Speer@Sun.COM 		 */
38239232SMichael.Speer@Sun.COM 		nxge_intr_hw_enable(nxgep);
38249232SMichael.Speer@Sun.COM 	}
38259232SMichael.Speer@Sun.COM #if defined(sun4v)
38269232SMichael.Speer@Sun.COM 	else {
38279232SMichael.Speer@Sun.COM 		/*
38289232SMichael.Speer@Sun.COM 		 * In guest domain we enable RDCs and their interrupts as
38299232SMichael.Speer@Sun.COM 		 * the last step.
38309232SMichael.Speer@Sun.COM 		 */
38319232SMichael.Speer@Sun.COM 		if (nxge_hio_rdc_enable(nxgep) != NXGE_OK) {
38329232SMichael.Speer@Sun.COM 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
38339232SMichael.Speer@Sun.COM 			    "<== nxge_m_start: enable of RDCs failed"));
38349232SMichael.Speer@Sun.COM 			return (EIO);
38359232SMichael.Speer@Sun.COM 		}
38369232SMichael.Speer@Sun.COM 
38379232SMichael.Speer@Sun.COM 		if (nxge_hio_rdc_intr_arm(nxgep, B_TRUE) != NXGE_OK) {
38389232SMichael.Speer@Sun.COM 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
38399232SMichael.Speer@Sun.COM 			    "<== nxge_m_start: intrs enable for RDCs failed"));
38409232SMichael.Speer@Sun.COM 			return (EIO);
38419232SMichael.Speer@Sun.COM 		}
38429232SMichael.Speer@Sun.COM 	}
38439232SMichael.Speer@Sun.COM #endif
38443859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_start"));
38453859Sml29623 	return (0);
38463859Sml29623 }
38473859Sml29623 
38488275SEric Cheng static boolean_t
38498275SEric Cheng nxge_check_groups_stopped(p_nxge_t nxgep)
38508275SEric Cheng {
38518275SEric Cheng 	int	i;
38528275SEric Cheng 
38538275SEric Cheng 	for (i = 0; i < NXGE_MAX_RDC_GROUPS; i++) {
38548275SEric Cheng 		if (nxgep->rx_hio_groups[i].started)
38558275SEric Cheng 			return (B_FALSE);
38568275SEric Cheng 	}
38578275SEric Cheng 
38588275SEric Cheng 	return (B_TRUE);
38598275SEric Cheng }
38608275SEric Cheng 
38613859Sml29623 /*
38623859Sml29623  *	nxge_m_stop(): stop transmitting and receiving.
38633859Sml29623  */
38643859Sml29623 static void
38653859Sml29623 nxge_m_stop(void *arg)
38663859Sml29623 {
38673859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
38688275SEric Cheng 	boolean_t	groups_stopped;
38693859Sml29623 
38703859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_stop"));
38713859Sml29623 
38729232SMichael.Speer@Sun.COM 	/*
38739232SMichael.Speer@Sun.COM 	 * Are the groups stopped?
38749232SMichael.Speer@Sun.COM 	 */
38758275SEric Cheng 	groups_stopped = nxge_check_groups_stopped(nxgep);
38769232SMichael.Speer@Sun.COM 	ASSERT(groups_stopped == B_TRUE);
38778275SEric Cheng 	if (!groups_stopped) {
38788275SEric Cheng 		cmn_err(CE_WARN, "nxge(%d): groups are not stopped!\n",
38798275SEric Cheng 		    nxgep->instance);
38808275SEric Cheng 		return;
38818275SEric Cheng 	}
38828275SEric Cheng 
38839232SMichael.Speer@Sun.COM 	if (!isLDOMguest(nxgep)) {
38849232SMichael.Speer@Sun.COM 		/*
38859232SMichael.Speer@Sun.COM 		 * Disable the RX mac.
38869232SMichael.Speer@Sun.COM 		 */
38879232SMichael.Speer@Sun.COM 		(void) nxge_rx_mac_disable(nxgep);
38889232SMichael.Speer@Sun.COM 
38899232SMichael.Speer@Sun.COM 		/*
38909232SMichael.Speer@Sun.COM 		 * Wait for the IPP to drain.
38919232SMichael.Speer@Sun.COM 		 */
38929232SMichael.Speer@Sun.COM 		(void) nxge_ipp_drain(nxgep);
38939232SMichael.Speer@Sun.COM 
38949232SMichael.Speer@Sun.COM 		/*
38959232SMichael.Speer@Sun.COM 		 * Disable hardware interrupts.
38969232SMichael.Speer@Sun.COM 		 */
38979232SMichael.Speer@Sun.COM 		nxge_intr_hw_disable(nxgep);
38989232SMichael.Speer@Sun.COM 	}
38999232SMichael.Speer@Sun.COM #if defined(sun4v)
39009232SMichael.Speer@Sun.COM 	else {
39019232SMichael.Speer@Sun.COM 		(void) nxge_hio_rdc_intr_arm(nxgep, B_FALSE);
39029232SMichael.Speer@Sun.COM 	}
39039232SMichael.Speer@Sun.COM #endif
39049232SMichael.Speer@Sun.COM 
39059232SMichael.Speer@Sun.COM 	/*
39069232SMichael.Speer@Sun.COM 	 * Grab the global lock.
39079232SMichael.Speer@Sun.COM 	 */
39087466SMisaki.Kataoka@Sun.COM 	MUTEX_ENTER(nxgep->genlock);
39099232SMichael.Speer@Sun.COM 
39107466SMisaki.Kataoka@Sun.COM 	nxgep->nxge_mac_state = NXGE_MAC_STOPPING;
39113859Sml29623 	if (nxgep->nxge_timerid) {
39123859Sml29623 		nxge_stop_timer(nxgep, nxgep->nxge_timerid);
39133859Sml29623 		nxgep->nxge_timerid = 0;
39143859Sml29623 	}
39153859Sml29623 
39169232SMichael.Speer@Sun.COM 	/*
39179232SMichael.Speer@Sun.COM 	 * Clean up.
39189232SMichael.Speer@Sun.COM 	 */
39193859Sml29623 	nxge_uninit(nxgep);
39203859Sml29623 
39213859Sml29623 	nxgep->nxge_mac_state = NXGE_MAC_STOPPED;
39223859Sml29623 
39239232SMichael.Speer@Sun.COM 	/*
39249232SMichael.Speer@Sun.COM 	 * Let go of the global lock.
39259232SMichael.Speer@Sun.COM 	 */
39263859Sml29623 	MUTEX_EXIT(nxgep->genlock);
39273859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_stop"));
39283859Sml29623 }
39293859Sml29623 
39303859Sml29623 static int
39313859Sml29623 nxge_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
39323859Sml29623 {
39333859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
39343859Sml29623 	struct 		ether_addr addrp;
39353859Sml29623 
39363859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL,
39376512Ssowmini 	    "==> nxge_m_multicst: add %d", add));
39383859Sml29623 
39393859Sml29623 	bcopy(mca, (uint8_t *)&addrp, ETHERADDRL);
39403859Sml29623 	if (add) {
39413859Sml29623 		if (nxge_add_mcast_addr(nxgep, &addrp)) {
39423859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
39436512Ssowmini 			    "<== nxge_m_multicst: add multicast failed"));
39443859Sml29623 			return (EINVAL);
39453859Sml29623 		}
39463859Sml29623 	} else {
39473859Sml29623 		if (nxge_del_mcast_addr(nxgep, &addrp)) {
39483859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
39496512Ssowmini 			    "<== nxge_m_multicst: del multicast failed"));
39503859Sml29623 			return (EINVAL);
39513859Sml29623 		}
39523859Sml29623 	}
39533859Sml29623 
39543859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL, "<== nxge_m_multicst"));
39553859Sml29623 
39563859Sml29623 	return (0);
39573859Sml29623 }
39583859Sml29623 
39593859Sml29623 static int
39603859Sml29623 nxge_m_promisc(void *arg, boolean_t on)
39613859Sml29623 {
39623859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
39633859Sml29623 
39643859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL,
39656512Ssowmini 	    "==> nxge_m_promisc: on %d", on));
39663859Sml29623 
39673859Sml29623 	if (nxge_set_promisc(nxgep, on)) {
39683859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
39696512Ssowmini 		    "<== nxge_m_promisc: set promisc failed"));
39703859Sml29623 		return (EINVAL);
39713859Sml29623 	}
39723859Sml29623 
39733859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL,
39746512Ssowmini 	    "<== nxge_m_promisc: on %d", on));
39753859Sml29623 
39763859Sml29623 	return (0);
39773859Sml29623 }
39783859Sml29623 
39793859Sml29623 static void
39803859Sml29623 nxge_m_ioctl(void *arg,  queue_t *wq, mblk_t *mp)
39813859Sml29623 {
39823859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
39834185Sspeer 	struct 		iocblk *iocp;
39843859Sml29623 	boolean_t 	need_privilege;
39853859Sml29623 	int 		err;
39863859Sml29623 	int 		cmd;
39873859Sml29623 
39883859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_ioctl"));
39893859Sml29623 
39903859Sml29623 	iocp = (struct iocblk *)mp->b_rptr;
39913859Sml29623 	iocp->ioc_error = 0;
39923859Sml29623 	need_privilege = B_TRUE;
39933859Sml29623 	cmd = iocp->ioc_cmd;
39943859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_ioctl: cmd 0x%08x", cmd));
39953859Sml29623 	switch (cmd) {
39963859Sml29623 	default:
39973859Sml29623 		miocnak(wq, mp, 0, EINVAL);
39983859Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_ioctl: invalid"));
39993859Sml29623 		return;
40003859Sml29623 
40013859Sml29623 	case LB_GET_INFO_SIZE:
40023859Sml29623 	case LB_GET_INFO:
40033859Sml29623 	case LB_GET_MODE:
40043859Sml29623 		need_privilege = B_FALSE;
40053859Sml29623 		break;
40063859Sml29623 	case LB_SET_MODE:
40073859Sml29623 		break;
40083859Sml29623 
40093859Sml29623 
40103859Sml29623 	case NXGE_GET_MII:
40113859Sml29623 	case NXGE_PUT_MII:
40123859Sml29623 	case NXGE_GET64:
40133859Sml29623 	case NXGE_PUT64:
40143859Sml29623 	case NXGE_GET_TX_RING_SZ:
40153859Sml29623 	case NXGE_GET_TX_DESC:
40163859Sml29623 	case NXGE_TX_SIDE_RESET:
40173859Sml29623 	case NXGE_RX_SIDE_RESET:
40183859Sml29623 	case NXGE_GLOBAL_RESET:
40193859Sml29623 	case NXGE_RESET_MAC:
40203859Sml29623 	case NXGE_TX_REGS_DUMP:
40213859Sml29623 	case NXGE_RX_REGS_DUMP:
40223859Sml29623 	case NXGE_INT_REGS_DUMP:
40233859Sml29623 	case NXGE_VIR_INT_REGS_DUMP:
40243859Sml29623 	case NXGE_PUT_TCAM:
40253859Sml29623 	case NXGE_GET_TCAM:
40263859Sml29623 	case NXGE_RTRACE:
40273859Sml29623 	case NXGE_RDUMP:
4028*11304SJanie.Lu@Sun.COM 	case NXGE_RX_CLASS:
4029*11304SJanie.Lu@Sun.COM 	case NXGE_RX_HASH:
40303859Sml29623 
40313859Sml29623 		need_privilege = B_FALSE;
40323859Sml29623 		break;
40333859Sml29623 	case NXGE_INJECT_ERR:
40343859Sml29623 		cmn_err(CE_NOTE, "!nxge_m_ioctl: Inject error\n");
40353859Sml29623 		nxge_err_inject(nxgep, wq, mp);
40363859Sml29623 		break;
40373859Sml29623 	}
40383859Sml29623 
40393859Sml29623 	if (need_privilege) {
40404185Sspeer 		err = secpolicy_net_config(iocp->ioc_cr, B_FALSE);
40413859Sml29623 		if (err != 0) {
40423859Sml29623 			miocnak(wq, mp, 0, err);
40433859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
40446512Ssowmini 			    "<== nxge_m_ioctl: no priv"));
40453859Sml29623 			return;
40463859Sml29623 		}
40473859Sml29623 	}
40483859Sml29623 
40493859Sml29623 	switch (cmd) {
40503859Sml29623 
40513859Sml29623 	case LB_GET_MODE:
40523859Sml29623 	case LB_SET_MODE:
40533859Sml29623 	case LB_GET_INFO_SIZE:
40543859Sml29623 	case LB_GET_INFO:
40553859Sml29623 		nxge_loopback_ioctl(nxgep, wq, mp, iocp);
40563859Sml29623 		break;
40573859Sml29623 
40583859Sml29623 	case NXGE_GET_MII:
40593859Sml29623 	case NXGE_PUT_MII:
40603859Sml29623 	case NXGE_PUT_TCAM:
40613859Sml29623 	case NXGE_GET_TCAM:
40623859Sml29623 	case NXGE_GET64:
40633859Sml29623 	case NXGE_PUT64:
40643859Sml29623 	case NXGE_GET_TX_RING_SZ:
40653859Sml29623 	case NXGE_GET_TX_DESC:
40663859Sml29623 	case NXGE_TX_SIDE_RESET:
40673859Sml29623 	case NXGE_RX_SIDE_RESET:
40683859Sml29623 	case NXGE_GLOBAL_RESET:
40693859Sml29623 	case NXGE_RESET_MAC:
40703859Sml29623 	case NXGE_TX_REGS_DUMP:
40713859Sml29623 	case NXGE_RX_REGS_DUMP:
40723859Sml29623 	case NXGE_INT_REGS_DUMP:
40733859Sml29623 	case NXGE_VIR_INT_REGS_DUMP:
40743859Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
40756512Ssowmini 		    "==> nxge_m_ioctl: cmd 0x%x", cmd));
40763859Sml29623 		nxge_hw_ioctl(nxgep, wq, mp, iocp);
40773859Sml29623 		break;
4078*11304SJanie.Lu@Sun.COM 	case NXGE_RX_CLASS:
4079*11304SJanie.Lu@Sun.COM 		if (nxge_rxclass_ioctl(nxgep, wq, mp->b_cont) < 0)
4080*11304SJanie.Lu@Sun.COM 			miocnak(wq, mp, 0, EINVAL);
4081*11304SJanie.Lu@Sun.COM 		else
4082*11304SJanie.Lu@Sun.COM 			miocack(wq, mp, sizeof (rx_class_cfg_t), 0);
4083*11304SJanie.Lu@Sun.COM 		break;
4084*11304SJanie.Lu@Sun.COM 	case NXGE_RX_HASH:
4085*11304SJanie.Lu@Sun.COM 
4086*11304SJanie.Lu@Sun.COM 		if (nxge_rxhash_ioctl(nxgep, wq, mp->b_cont) < 0)
4087*11304SJanie.Lu@Sun.COM 			miocnak(wq, mp, 0, EINVAL);
4088*11304SJanie.Lu@Sun.COM 		else
4089*11304SJanie.Lu@Sun.COM 			miocack(wq, mp, sizeof (cfg_cmd_t), 0);
4090*11304SJanie.Lu@Sun.COM 		break;
40913859Sml29623 	}
40923859Sml29623 
40933859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_ioctl"));
40943859Sml29623 }
40953859Sml29623 
40963859Sml29623 extern void nxge_rx_hw_blank(void *arg, time_t ticks, uint_t count);
40973859Sml29623 
40986495Sspeer void
40998275SEric Cheng nxge_mmac_kstat_update(p_nxge_t nxgep, int slot, boolean_t factory)
41003859Sml29623 {
41013859Sml29623 	p_nxge_mmac_stats_t mmac_stats;
41023859Sml29623 	int i;
41033859Sml29623 	nxge_mmac_t *mmac_info;
41043859Sml29623 
41053859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
41063859Sml29623 
41073859Sml29623 	mmac_stats = &nxgep->statsp->mmac_stats;
41083859Sml29623 	mmac_stats->mmac_max_cnt = mmac_info->num_mmac;
41093859Sml29623 	mmac_stats->mmac_avail_cnt = mmac_info->naddrfree;
41103859Sml29623 
41113859Sml29623 	for (i = 0; i < ETHERADDRL; i++) {
41123859Sml29623 		if (factory) {
41133859Sml29623 			mmac_stats->mmac_avail_pool[slot-1].ether_addr_octet[i]
41146512Ssowmini 			    = mmac_info->factory_mac_pool[slot][
41156512Ssowmini 			    (ETHERADDRL-1) - i];
41163859Sml29623 		} else {
41173859Sml29623 			mmac_stats->mmac_avail_pool[slot-1].ether_addr_octet[i]
41186512Ssowmini 			    = mmac_info->mac_pool[slot].addr[
41196512Ssowmini 			    (ETHERADDRL - 1) - i];
41203859Sml29623 		}
41213859Sml29623 	}
41223859Sml29623 }
41233859Sml29623 
41243859Sml29623 /*
41253859Sml29623  * nxge_altmac_set() -- Set an alternate MAC address
41263859Sml29623  */
41278275SEric Cheng static int
41288275SEric Cheng nxge_altmac_set(p_nxge_t nxgep, uint8_t *maddr, int slot,
41298275SEric Cheng 	int rdctbl, boolean_t usetbl)
41303859Sml29623 {
41313859Sml29623 	uint8_t addrn;
41323859Sml29623 	uint8_t portn;
41333859Sml29623 	npi_mac_addr_t altmac;
41344484Sspeer 	hostinfo_t mac_rdc;
41354484Sspeer 	p_nxge_class_pt_cfg_t clscfgp;
41363859Sml29623 
41378275SEric Cheng 
41383859Sml29623 	altmac.w2 = ((uint16_t)maddr[0] << 8) | ((uint16_t)maddr[1] & 0x0ff);
41393859Sml29623 	altmac.w1 = ((uint16_t)maddr[2] << 8) | ((uint16_t)maddr[3] & 0x0ff);
41403859Sml29623 	altmac.w0 = ((uint16_t)maddr[4] << 8) | ((uint16_t)maddr[5] & 0x0ff);
41413859Sml29623 
41423859Sml29623 	portn = nxgep->mac.portnum;
41433859Sml29623 	addrn = (uint8_t)slot - 1;
41443859Sml29623 
41458275SEric Cheng 	if (npi_mac_altaddr_entry(nxgep->npi_handle, OP_SET,
41468275SEric Cheng 	    nxgep->function_num, addrn, &altmac) != NPI_SUCCESS)
41473859Sml29623 		return (EIO);
41484484Sspeer 
41494484Sspeer 	/*
41504484Sspeer 	 * Set the rdc table number for the host info entry
41514484Sspeer 	 * for this mac address slot.
41524484Sspeer 	 */
41534484Sspeer 	clscfgp = (p_nxge_class_pt_cfg_t)&nxgep->class_config;
41544484Sspeer 	mac_rdc.value = 0;
41558275SEric Cheng 	if (usetbl)
41568275SEric Cheng 		mac_rdc.bits.w0.rdc_tbl_num = rdctbl;
41578275SEric Cheng 	else
41588275SEric Cheng 		mac_rdc.bits.w0.rdc_tbl_num =
41598275SEric Cheng 		    clscfgp->mac_host_info[addrn].rdctbl;
41604484Sspeer 	mac_rdc.bits.w0.mac_pref = clscfgp->mac_host_info[addrn].mpr_npr;
41614484Sspeer 
41624484Sspeer 	if (npi_mac_hostinfo_entry(nxgep->npi_handle, OP_SET,
41634484Sspeer 	    nxgep->function_num, addrn, &mac_rdc) != NPI_SUCCESS) {
41644484Sspeer 		return (EIO);
41654484Sspeer 	}
41664484Sspeer 
41673859Sml29623 	/*
41683859Sml29623 	 * Enable comparison with the alternate MAC address.
41693859Sml29623 	 * While the first alternate addr is enabled by bit 1 of register
41703859Sml29623 	 * BMAC_ALTAD_CMPEN, it is enabled by bit 0 of register
41713859Sml29623 	 * XMAC_ADDR_CMPEN, so slot needs to be converted to addrn
41723859Sml29623 	 * accordingly before calling npi_mac_altaddr_entry.
41733859Sml29623 	 */
41743859Sml29623 	if (portn == XMAC_PORT_0 || portn == XMAC_PORT_1)
41753859Sml29623 		addrn = (uint8_t)slot - 1;
41763859Sml29623 	else
41773859Sml29623 		addrn = (uint8_t)slot;
41783859Sml29623 
41798275SEric Cheng 	if (npi_mac_altaddr_enable(nxgep->npi_handle,
41808275SEric Cheng 	    nxgep->function_num, addrn) != NPI_SUCCESS) {
41813859Sml29623 		return (EIO);
41828275SEric Cheng 	}
41838275SEric Cheng 
41843859Sml29623 	return (0);
41853859Sml29623 }
41863859Sml29623 
41873859Sml29623 /*
41888275SEric Cheng  * nxeg_m_mmac_add_g() - find an unused address slot, set the address
41893859Sml29623  * value to the one specified, enable the port to start filtering on
41903859Sml29623  * the new MAC address.  Returns 0 on success.
41913859Sml29623  */
41926495Sspeer int
41938275SEric Cheng nxge_m_mmac_add_g(void *arg, const uint8_t *maddr, int rdctbl,
41948275SEric Cheng 	boolean_t usetbl)
41953859Sml29623 {
41963859Sml29623 	p_nxge_t nxgep = arg;
41978275SEric Cheng 	int slot;
41983859Sml29623 	nxge_mmac_t *mmac_info;
41993859Sml29623 	int err;
42003859Sml29623 	nxge_status_t status;
42013859Sml29623 
42023859Sml29623 	mutex_enter(nxgep->genlock);
42033859Sml29623 
42043859Sml29623 	/*
42053859Sml29623 	 * Make sure that nxge is initialized, if _start() has
42063859Sml29623 	 * not been called.
42073859Sml29623 	 */
42083859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
42093859Sml29623 		status = nxge_init(nxgep);
42103859Sml29623 		if (status != NXGE_OK) {
42113859Sml29623 			mutex_exit(nxgep->genlock);
42123859Sml29623 			return (ENXIO);
42133859Sml29623 		}
42143859Sml29623 	}
42153859Sml29623 
42163859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
42173859Sml29623 	if (mmac_info->naddrfree == 0) {
42183859Sml29623 		mutex_exit(nxgep->genlock);
42193859Sml29623 		return (ENOSPC);
42203859Sml29623 	}
42218275SEric Cheng 
42223859Sml29623 	/*
42233859Sml29623 	 * 	Search for the first available slot. Because naddrfree
42243859Sml29623 	 * is not zero, we are guaranteed to find one.
42253859Sml29623 	 *	Each of the first two ports of Neptune has 16 alternate
42266495Sspeer 	 * MAC slots but only the first 7 (of 15) slots have assigned factory
42273859Sml29623 	 * MAC addresses. We first search among the slots without bundled
42283859Sml29623 	 * factory MACs. If we fail to find one in that range, then we
42293859Sml29623 	 * search the slots with bundled factory MACs.  A factory MAC
42303859Sml29623 	 * will be wasted while the slot is used with a user MAC address.
42313859Sml29623 	 * But the slot could be used by factory MAC again after calling
42323859Sml29623 	 * nxge_m_mmac_remove and nxge_m_mmac_reserve.
42333859Sml29623 	 */
42348275SEric Cheng 	for (slot = 0; slot <= mmac_info->num_mmac; slot++) {
42358275SEric Cheng 		if (!(mmac_info->mac_pool[slot].flags & MMAC_SLOT_USED))
42368275SEric Cheng 			break;
42378275SEric Cheng 	}
42388275SEric Cheng 
42393859Sml29623 	ASSERT(slot <= mmac_info->num_mmac);
42408047SMichael.Speer@Sun.COM 
42418275SEric Cheng 	if ((err = nxge_altmac_set(nxgep, (uint8_t *)maddr, slot, rdctbl,
42428275SEric Cheng 	    usetbl)) != 0) {
42433859Sml29623 		mutex_exit(nxgep->genlock);
42443859Sml29623 		return (err);
42453859Sml29623 	}
42468047SMichael.Speer@Sun.COM 
42478275SEric Cheng 	bcopy(maddr, mmac_info->mac_pool[slot].addr, ETHERADDRL);
42483859Sml29623 	mmac_info->mac_pool[slot].flags |= MMAC_SLOT_USED;
42493859Sml29623 	mmac_info->mac_pool[slot].flags &= ~MMAC_VENDOR_ADDR;
42503859Sml29623 	mmac_info->naddrfree--;
42513859Sml29623 	nxge_mmac_kstat_update(nxgep, slot, B_FALSE);
42523859Sml29623 
42533859Sml29623 	mutex_exit(nxgep->genlock);
42543859Sml29623 	return (0);
42553859Sml29623 }
42563859Sml29623 
42573859Sml29623 /*
42583859Sml29623  * Remove the specified mac address and update the HW not to filter
42593859Sml29623  * the mac address anymore.
42603859Sml29623  */
42616495Sspeer int
42628275SEric Cheng nxge_m_mmac_remove(void *arg, int slot)
42633859Sml29623 {
42643859Sml29623 	p_nxge_t nxgep = arg;
42653859Sml29623 	nxge_mmac_t *mmac_info;
42663859Sml29623 	uint8_t addrn;
42673859Sml29623 	uint8_t portn;
42683859Sml29623 	int err = 0;
42693859Sml29623 	nxge_status_t status;
42703859Sml29623 
42713859Sml29623 	mutex_enter(nxgep->genlock);
42723859Sml29623 
42733859Sml29623 	/*
42743859Sml29623 	 * Make sure that nxge is initialized, if _start() has
42753859Sml29623 	 * not been called.
42763859Sml29623 	 */
42773859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
42783859Sml29623 		status = nxge_init(nxgep);
42793859Sml29623 		if (status != NXGE_OK) {
42803859Sml29623 			mutex_exit(nxgep->genlock);
42813859Sml29623 			return (ENXIO);
42823859Sml29623 		}
42833859Sml29623 	}
42843859Sml29623 
42853859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
42863859Sml29623 	if (slot < 1 || slot > mmac_info->num_mmac) {
42873859Sml29623 		mutex_exit(nxgep->genlock);
42883859Sml29623 		return (EINVAL);
42893859Sml29623 	}
42903859Sml29623 
42913859Sml29623 	portn = nxgep->mac.portnum;
42923859Sml29623 	if (portn == XMAC_PORT_0 || portn == XMAC_PORT_1)
42933859Sml29623 		addrn = (uint8_t)slot - 1;
42943859Sml29623 	else
42953859Sml29623 		addrn = (uint8_t)slot;
42963859Sml29623 
42973859Sml29623 	if (mmac_info->mac_pool[slot].flags & MMAC_SLOT_USED) {
42983859Sml29623 		if (npi_mac_altaddr_disable(nxgep->npi_handle, portn, addrn)
42996512Ssowmini 		    == NPI_SUCCESS) {
43003859Sml29623 			mmac_info->naddrfree++;
43013859Sml29623 			mmac_info->mac_pool[slot].flags &= ~MMAC_SLOT_USED;
43023859Sml29623 			/*
43033859Sml29623 			 * Regardless if the MAC we just stopped filtering
43043859Sml29623 			 * is a user addr or a facory addr, we must set
43053859Sml29623 			 * the MMAC_VENDOR_ADDR flag if this slot has an
43063859Sml29623 			 * associated factory MAC to indicate that a factory
43073859Sml29623 			 * MAC is available.
43083859Sml29623 			 */
43093859Sml29623 			if (slot <= mmac_info->num_factory_mmac) {
43103859Sml29623 				mmac_info->mac_pool[slot].flags
43116512Ssowmini 				    |= MMAC_VENDOR_ADDR;
43123859Sml29623 			}
43133859Sml29623 			/*
43143859Sml29623 			 * Clear mac_pool[slot].addr so that kstat shows 0
43153859Sml29623 			 * alternate MAC address if the slot is not used.
43163859Sml29623 			 * (But nxge_m_mmac_get returns the factory MAC even
43173859Sml29623 			 * when the slot is not used!)
43183859Sml29623 			 */
43193859Sml29623 			bzero(mmac_info->mac_pool[slot].addr, ETHERADDRL);
43203859Sml29623 			nxge_mmac_kstat_update(nxgep, slot, B_FALSE);
43213859Sml29623 		} else {
43223859Sml29623 			err = EIO;
43233859Sml29623 		}
43243859Sml29623 	} else {
43253859Sml29623 		err = EINVAL;
43263859Sml29623 	}
43273859Sml29623 
43283859Sml29623 	mutex_exit(nxgep->genlock);
43293859Sml29623 	return (err);
43303859Sml29623 }
43313859Sml29623 
43323859Sml29623 /*
43338275SEric Cheng  * The callback to query all the factory addresses. naddr must be the same as
43348275SEric Cheng  * the number of factory addresses (returned by MAC_CAPAB_MULTIFACTADDR), and
43358275SEric Cheng  * mcm_addr is the space allocated for keep all the addresses, whose size is
43368275SEric Cheng  * naddr * MAXMACADDRLEN.
43373859Sml29623  */
43388275SEric Cheng static void
43398275SEric Cheng nxge_m_getfactaddr(void *arg, uint_t naddr, uint8_t *addr)
43403859Sml29623 {
43418275SEric Cheng 	nxge_t		*nxgep = arg;
43428275SEric Cheng 	nxge_mmac_t	*mmac_info;
43438275SEric Cheng 	int		i;
43443859Sml29623 
43453859Sml29623 	mutex_enter(nxgep->genlock);
43463859Sml29623 
43473859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
43488275SEric Cheng 	ASSERT(naddr == mmac_info->num_factory_mmac);
43498275SEric Cheng 
43508275SEric Cheng 	for (i = 0; i < naddr; i++) {
43518275SEric Cheng 		bcopy(mmac_info->factory_mac_pool[i + 1],
43528275SEric Cheng 		    addr + i * MAXMACADDRLEN, ETHERADDRL);
43538275SEric Cheng 	}
43548275SEric Cheng 
43553859Sml29623 	mutex_exit(nxgep->genlock);
43563859Sml29623 }
43573859Sml29623 
43583859Sml29623 
43593859Sml29623 static boolean_t
43603859Sml29623 nxge_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
43613859Sml29623 {
43623859Sml29623 	nxge_t *nxgep = arg;
43633859Sml29623 	uint32_t *txflags = cap_data;
43643859Sml29623 
43653859Sml29623 	switch (cap) {
43663859Sml29623 	case MAC_CAPAB_HCKSUM:
43676495Sspeer 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
43686611Sml29623 		    "==> nxge_m_getcapab: checksum %d", nxge_cksum_offload));
43696611Sml29623 		if (nxge_cksum_offload <= 1) {
43706495Sspeer 			*txflags = HCKSUM_INET_PARTIAL;
43716495Sspeer 		}
43723859Sml29623 		break;
43736495Sspeer 
43748275SEric Cheng 	case MAC_CAPAB_MULTIFACTADDR: {
43758275SEric Cheng 		mac_capab_multifactaddr_t	*mfacp = cap_data;
43768275SEric Cheng 
437710309SSriharsha.Basavapatna@Sun.COM 		if (!isLDOMguest(nxgep)) {
437810309SSriharsha.Basavapatna@Sun.COM 			mutex_enter(nxgep->genlock);
437910309SSriharsha.Basavapatna@Sun.COM 			mfacp->mcm_naddr =
438010309SSriharsha.Basavapatna@Sun.COM 			    nxgep->nxge_mmac_info.num_factory_mmac;
438110309SSriharsha.Basavapatna@Sun.COM 			mfacp->mcm_getaddr = nxge_m_getfactaddr;
438210309SSriharsha.Basavapatna@Sun.COM 			mutex_exit(nxgep->genlock);
438310309SSriharsha.Basavapatna@Sun.COM 		}
43843859Sml29623 		break;
43858275SEric Cheng 	}
43866495Sspeer 
43875770Sml29623 	case MAC_CAPAB_LSO: {
43885770Sml29623 		mac_capab_lso_t *cap_lso = cap_data;
43895770Sml29623 
43906003Sml29623 		if (nxgep->soft_lso_enable) {
43916611Sml29623 			if (nxge_cksum_offload <= 1) {
43926611Sml29623 				cap_lso->lso_flags = LSO_TX_BASIC_TCP_IPV4;
43936611Sml29623 				if (nxge_lso_max > NXGE_LSO_MAXLEN) {
43946611Sml29623 					nxge_lso_max = NXGE_LSO_MAXLEN;
43956611Sml29623 				}
43966611Sml29623 				cap_lso->lso_basic_tcp_ipv4.lso_max =
43976611Sml29623 				    nxge_lso_max;
43985770Sml29623 			}
43995770Sml29623 			break;
44005770Sml29623 		} else {
44015770Sml29623 			return (B_FALSE);
44025770Sml29623 		}
44035770Sml29623 	}
44045770Sml29623 
44058275SEric Cheng 	case MAC_CAPAB_RINGS: {
44068275SEric Cheng 		mac_capab_rings_t	*cap_rings = cap_data;
44078275SEric Cheng 		p_nxge_hw_pt_cfg_t	p_cfgp = &nxgep->pt_config.hw_config;
44088275SEric Cheng 
44098275SEric Cheng 		mutex_enter(nxgep->genlock);
44108275SEric Cheng 		if (cap_rings->mr_type == MAC_RING_TYPE_RX) {
441110309SSriharsha.Basavapatna@Sun.COM 			if (isLDOMguest(nxgep))  {
441210309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_group_type =
441310309SSriharsha.Basavapatna@Sun.COM 				    MAC_GROUP_TYPE_STATIC;
441410309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_rnum =
441510309SSriharsha.Basavapatna@Sun.COM 				    NXGE_HIO_SHARE_MAX_CHANNELS;
441610309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_rget = nxge_fill_ring;
441710309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gnum = 1;
441810309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gget = nxge_hio_group_get;
441910309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gaddring = NULL;
442010309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gremring = NULL;
442110309SSriharsha.Basavapatna@Sun.COM 			} else {
442210309SSriharsha.Basavapatna@Sun.COM 				/*
442310309SSriharsha.Basavapatna@Sun.COM 				 * Service Domain.
442410309SSriharsha.Basavapatna@Sun.COM 				 */
442510309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_group_type =
442610309SSriharsha.Basavapatna@Sun.COM 				    MAC_GROUP_TYPE_DYNAMIC;
442710309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_rnum = p_cfgp->max_rdcs;
442810309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_rget = nxge_fill_ring;
442910309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gnum = p_cfgp->max_rdc_grpids;
443010309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gget = nxge_hio_group_get;
443110309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gaddring = nxge_group_add_ring;
443210309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gremring = nxge_group_rem_ring;
443310309SSriharsha.Basavapatna@Sun.COM 			}
44348275SEric Cheng 
44358275SEric Cheng 			NXGE_DEBUG_MSG((nxgep, RX_CTL,
44368275SEric Cheng 			    "==> nxge_m_getcapab: rx nrings[%d] ngroups[%d]",
44378275SEric Cheng 			    p_cfgp->max_rdcs, p_cfgp->max_rdc_grpids));
44388275SEric Cheng 		} else {
443910309SSriharsha.Basavapatna@Sun.COM 			/*
444010309SSriharsha.Basavapatna@Sun.COM 			 * TX Rings.
444110309SSriharsha.Basavapatna@Sun.COM 			 */
444210309SSriharsha.Basavapatna@Sun.COM 			if (isLDOMguest(nxgep)) {
444310309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_group_type =
444410309SSriharsha.Basavapatna@Sun.COM 				    MAC_GROUP_TYPE_STATIC;
444510309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_rnum =
444610309SSriharsha.Basavapatna@Sun.COM 				    NXGE_HIO_SHARE_MAX_CHANNELS;
444710309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_rget = nxge_fill_ring;
444810309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gnum = 0;
444910309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gget = NULL;
445010309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gaddring = NULL;
445110309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gremring = NULL;
445210309SSriharsha.Basavapatna@Sun.COM 			} else {
445310309SSriharsha.Basavapatna@Sun.COM 				/*
445410309SSriharsha.Basavapatna@Sun.COM 				 * Service Domain.
445510309SSriharsha.Basavapatna@Sun.COM 				 */
445610309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_group_type =
445710309SSriharsha.Basavapatna@Sun.COM 				    MAC_GROUP_TYPE_DYNAMIC;
445810309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_rnum = p_cfgp->tdc.count;
445910309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_rget = nxge_fill_ring;
446010309SSriharsha.Basavapatna@Sun.COM 
446110309SSriharsha.Basavapatna@Sun.COM 				/*
446210309SSriharsha.Basavapatna@Sun.COM 				 * Share capable.
446310309SSriharsha.Basavapatna@Sun.COM 				 *
446410309SSriharsha.Basavapatna@Sun.COM 				 * Do not report the default group: hence -1
446510309SSriharsha.Basavapatna@Sun.COM 				 */
44668275SEric Cheng 				cap_rings->mr_gnum =
44678275SEric Cheng 				    NXGE_MAX_TDC_GROUPS / nxgep->nports - 1;
446810309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gget = nxge_hio_group_get;
446910309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gaddring = nxge_group_add_ring;
447010309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gremring = nxge_group_rem_ring;
44718275SEric Cheng 			}
44728275SEric Cheng 
44738275SEric Cheng 			NXGE_DEBUG_MSG((nxgep, TX_CTL,
44748275SEric Cheng 			    "==> nxge_m_getcapab: tx rings # of rings %d",
44758275SEric Cheng 			    p_cfgp->tdc.count));
44768275SEric Cheng 		}
44778275SEric Cheng 		mutex_exit(nxgep->genlock);
44788275SEric Cheng 		break;
44798275SEric Cheng 	}
44808275SEric Cheng 
44816495Sspeer #if defined(sun4v)
44826495Sspeer 	case MAC_CAPAB_SHARES: {
44836495Sspeer 		mac_capab_share_t *mshares = (mac_capab_share_t *)cap_data;
44846495Sspeer 
44856495Sspeer 		/*
44866495Sspeer 		 * Only the service domain driver responds to
44876495Sspeer 		 * this capability request.
44886495Sspeer 		 */
44898275SEric Cheng 		mutex_enter(nxgep->genlock);
44906495Sspeer 		if (isLDOMservice(nxgep)) {
44916495Sspeer 			mshares->ms_snum = 3;
44926495Sspeer 			mshares->ms_handle = (void *)nxgep;
44936495Sspeer 			mshares->ms_salloc = nxge_hio_share_alloc;
44946495Sspeer 			mshares->ms_sfree = nxge_hio_share_free;
44958275SEric Cheng 			mshares->ms_sadd = nxge_hio_share_add_group;
44968275SEric Cheng 			mshares->ms_sremove = nxge_hio_share_rem_group;
44976495Sspeer 			mshares->ms_squery = nxge_hio_share_query;
44988275SEric Cheng 			mshares->ms_sbind = nxge_hio_share_bind;
44998275SEric Cheng 			mshares->ms_sunbind = nxge_hio_share_unbind;
45008275SEric Cheng 			mutex_exit(nxgep->genlock);
45018275SEric Cheng 		} else {
45028275SEric Cheng 			mutex_exit(nxgep->genlock);
45036495Sspeer 			return (B_FALSE);
45048275SEric Cheng 		}
45056495Sspeer 		break;
45066495Sspeer 	}
45076495Sspeer #endif
45083859Sml29623 	default:
45093859Sml29623 		return (B_FALSE);
45103859Sml29623 	}
45113859Sml29623 	return (B_TRUE);
45123859Sml29623 }
45133859Sml29623 
45146439Sml29623 static boolean_t
45156439Sml29623 nxge_param_locked(mac_prop_id_t pr_num)
45166439Sml29623 {
45176439Sml29623 	/*
45186439Sml29623 	 * All adv_* parameters are locked (read-only) while
45196439Sml29623 	 * the device is in any sort of loopback mode ...
45206439Sml29623 	 */
45216439Sml29623 	switch (pr_num) {
45226789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
45236789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
45246789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
45256789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
45266789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
45276789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
45286789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
45296789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
45306789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
45316789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
45326789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
45336789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
45346789Sam223141 		case MAC_PROP_AUTONEG:
45356789Sam223141 		case MAC_PROP_FLOWCTRL:
45366439Sml29623 			return (B_TRUE);
45376439Sml29623 	}
45386439Sml29623 	return (B_FALSE);
45396439Sml29623 }
45406439Sml29623 
45416439Sml29623 /*
45426439Sml29623  * callback functions for set/get of properties
45436439Sml29623  */
45446439Sml29623 static int
45456439Sml29623 nxge_m_setprop(void *barg, const char *pr_name, mac_prop_id_t pr_num,
45466439Sml29623     uint_t pr_valsize, const void *pr_val)
45476439Sml29623 {
45486439Sml29623 	nxge_t		*nxgep = barg;
45496439Sml29623 	p_nxge_param_t	param_arr;
45506439Sml29623 	p_nxge_stats_t	statsp;
45516439Sml29623 	int		err = 0;
45526439Sml29623 	uint8_t		val;
45536439Sml29623 	uint32_t	cur_mtu, new_mtu, old_framesize;
45546439Sml29623 	link_flowctrl_t	fl;
45556439Sml29623 
45566439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_setprop"));
45576439Sml29623 	param_arr = nxgep->param_arr;
45586439Sml29623 	statsp = nxgep->statsp;
45596439Sml29623 	mutex_enter(nxgep->genlock);
45606439Sml29623 	if (statsp->port_stats.lb_mode != nxge_lb_normal &&
45616439Sml29623 	    nxge_param_locked(pr_num)) {
45626439Sml29623 		/*
45636439Sml29623 		 * All adv_* parameters are locked (read-only)
45646439Sml29623 		 * while the device is in any sort of loopback mode.
45656439Sml29623 		 */
45666439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
45676439Sml29623 		    "==> nxge_m_setprop: loopback mode: read only"));
45686439Sml29623 		mutex_exit(nxgep->genlock);
45696439Sml29623 		return (EBUSY);
45706439Sml29623 	}
45716439Sml29623 
45726439Sml29623 	val = *(uint8_t *)pr_val;
45736439Sml29623 	switch (pr_num) {
45746789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
45756439Sml29623 			nxgep->param_en_1000fdx = val;
45766439Sml29623 			param_arr[param_anar_1000fdx].value = val;
45776439Sml29623 
45786439Sml29623 			goto reprogram;
45796439Sml29623 
45806789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
45816439Sml29623 			nxgep->param_en_100fdx = val;
45826439Sml29623 			param_arr[param_anar_100fdx].value = val;
45836439Sml29623 
45846439Sml29623 			goto reprogram;
45856439Sml29623 
45866789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
45876439Sml29623 			nxgep->param_en_10fdx = val;
45886439Sml29623 			param_arr[param_anar_10fdx].value = val;
45896439Sml29623 
45906439Sml29623 			goto reprogram;
45916439Sml29623 
45926789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
45936789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
45946789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
45956789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
45966789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
45976789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
45986789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
45996789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
46006789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
46016789Sam223141 		case MAC_PROP_STATUS:
46026789Sam223141 		case MAC_PROP_SPEED:
46036789Sam223141 		case MAC_PROP_DUPLEX:
46046439Sml29623 			err = EINVAL; /* cannot set read-only properties */
46056439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
46066439Sml29623 			    "==> nxge_m_setprop:  read only property %d",
46076439Sml29623 			    pr_num));
46086439Sml29623 			break;
46096439Sml29623 
46106789Sam223141 		case MAC_PROP_AUTONEG:
46116439Sml29623 			param_arr[param_autoneg].value = val;
46126439Sml29623 
46136439Sml29623 			goto reprogram;
46146439Sml29623 
46156789Sam223141 		case MAC_PROP_MTU:
46166439Sml29623 			cur_mtu = nxgep->mac.default_mtu;
46176439Sml29623 			bcopy(pr_val, &new_mtu, sizeof (new_mtu));
46186439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
46196439Sml29623 			    "==> nxge_m_setprop: set MTU: %d is_jumbo %d",
46206439Sml29623 			    new_mtu, nxgep->mac.is_jumbo));
46216439Sml29623 
46226439Sml29623 			if (new_mtu == cur_mtu) {
46236439Sml29623 				err = 0;
46246439Sml29623 				break;
46256439Sml29623 			}
46269730SMichael.Speer@Sun.COM 
46278118SVasumathi.Sundaram@Sun.COM 			if (nxgep->nxge_mac_state == NXGE_MAC_STARTED) {
46288118SVasumathi.Sundaram@Sun.COM 				err = EBUSY;
46298118SVasumathi.Sundaram@Sun.COM 				break;
46308118SVasumathi.Sundaram@Sun.COM 			}
46319730SMichael.Speer@Sun.COM 
46329730SMichael.Speer@Sun.COM 			if ((new_mtu < NXGE_DEFAULT_MTU) ||
46339730SMichael.Speer@Sun.COM 			    (new_mtu > NXGE_MAXIMUM_MTU)) {
46346439Sml29623 				err = EINVAL;
46356439Sml29623 				break;
46366439Sml29623 			}
46376439Sml29623 
46386439Sml29623 			old_framesize = (uint32_t)nxgep->mac.maxframesize;
46396439Sml29623 			nxgep->mac.maxframesize = (uint16_t)
46406439Sml29623 			    (new_mtu + NXGE_EHEADER_VLAN_CRC);
46416439Sml29623 			if (nxge_mac_set_framesize(nxgep)) {
46426444Sml29623 				nxgep->mac.maxframesize =
46436444Sml29623 				    (uint16_t)old_framesize;
46446439Sml29623 				err = EINVAL;
46456439Sml29623 				break;
46466439Sml29623 			}
46476439Sml29623 
46486439Sml29623 			err = mac_maxsdu_update(nxgep->mach, new_mtu);
46496439Sml29623 			if (err) {
46506444Sml29623 				nxgep->mac.maxframesize =
46516444Sml29623 				    (uint16_t)old_framesize;
46526439Sml29623 				err = EINVAL;
46536439Sml29623 				break;
46546439Sml29623 			}
46556439Sml29623 
46566439Sml29623 			nxgep->mac.default_mtu = new_mtu;
46579730SMichael.Speer@Sun.COM 			if (new_mtu > NXGE_DEFAULT_MTU)
46589730SMichael.Speer@Sun.COM 				nxgep->mac.is_jumbo = B_TRUE;
46599730SMichael.Speer@Sun.COM 			else
46609730SMichael.Speer@Sun.COM 				nxgep->mac.is_jumbo = B_FALSE;
46619730SMichael.Speer@Sun.COM 
46626439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
46636439Sml29623 			    "==> nxge_m_setprop: set MTU: %d maxframe %d",
46646439Sml29623 			    new_mtu, nxgep->mac.maxframesize));
46656439Sml29623 			break;
46666439Sml29623 
46676789Sam223141 		case MAC_PROP_FLOWCTRL:
46686439Sml29623 			bcopy(pr_val, &fl, sizeof (fl));
46696439Sml29623 			switch (fl) {
46706439Sml29623 			default:
46716439Sml29623 				err = EINVAL;
46726439Sml29623 				break;
46736439Sml29623 
46746439Sml29623 			case LINK_FLOWCTRL_NONE:
46756439Sml29623 				param_arr[param_anar_pause].value = 0;
46766439Sml29623 				break;
46776439Sml29623 
46786439Sml29623 			case LINK_FLOWCTRL_RX:
46796439Sml29623 				param_arr[param_anar_pause].value = 1;
46806439Sml29623 				break;
46816439Sml29623 
46826439Sml29623 			case LINK_FLOWCTRL_TX:
46836439Sml29623 			case LINK_FLOWCTRL_BI:
46846439Sml29623 				err = EINVAL;
46856439Sml29623 				break;
46866439Sml29623 			}
46876439Sml29623 
46886439Sml29623 reprogram:
46896439Sml29623 			if (err == 0) {
46906439Sml29623 				if (!nxge_param_link_update(nxgep)) {
46916439Sml29623 					err = EINVAL;
46926439Sml29623 				}
46936439Sml29623 			}
46946439Sml29623 			break;
46956789Sam223141 		case MAC_PROP_PRIVATE:
46966439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
46976439Sml29623 			    "==> nxge_m_setprop: private property"));
46986439Sml29623 			err = nxge_set_priv_prop(nxgep, pr_name, pr_valsize,
46996439Sml29623 			    pr_val);
47006439Sml29623 			break;
47016512Ssowmini 
47026512Ssowmini 		default:
47036512Ssowmini 			err = ENOTSUP;
47046512Ssowmini 			break;
47056439Sml29623 	}
47066439Sml29623 
47076439Sml29623 	mutex_exit(nxgep->genlock);
47086439Sml29623 
47096439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47106439Sml29623 	    "<== nxge_m_setprop (return %d)", err));
47116439Sml29623 	return (err);
47126439Sml29623 }
47136439Sml29623 
47146439Sml29623 static int
47156439Sml29623 nxge_m_getprop(void *barg, const char *pr_name, mac_prop_id_t pr_num,
47168118SVasumathi.Sundaram@Sun.COM     uint_t pr_flags, uint_t pr_valsize, void *pr_val, uint_t *perm)
47176439Sml29623 {
47186439Sml29623 	nxge_t 		*nxgep = barg;
47196439Sml29623 	p_nxge_param_t	param_arr = nxgep->param_arr;
47206439Sml29623 	p_nxge_stats_t	statsp = nxgep->statsp;
47216439Sml29623 	int		err = 0;
47226439Sml29623 	link_flowctrl_t	fl;
47236439Sml29623 	uint64_t	tmp = 0;
47246512Ssowmini 	link_state_t	ls;
47256789Sam223141 	boolean_t	is_default = (pr_flags & MAC_PROP_DEFAULT);
47266439Sml29623 
47276439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47286439Sml29623 	    "==> nxge_m_getprop: pr_num %d", pr_num));
47296512Ssowmini 
47306512Ssowmini 	if (pr_valsize == 0)
47316512Ssowmini 		return (EINVAL);
47326512Ssowmini 
47338118SVasumathi.Sundaram@Sun.COM 	*perm = MAC_PROP_PERM_RW;
47348118SVasumathi.Sundaram@Sun.COM 
47356789Sam223141 	if ((is_default) && (pr_num != MAC_PROP_PRIVATE)) {
47366512Ssowmini 		err = nxge_get_def_val(nxgep, pr_num, pr_valsize, pr_val);
47376512Ssowmini 		return (err);
47386512Ssowmini 	}
47396512Ssowmini 
47406439Sml29623 	bzero(pr_val, pr_valsize);
47416439Sml29623 	switch (pr_num) {
47426789Sam223141 		case MAC_PROP_DUPLEX:
47438118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
47446439Sml29623 			*(uint8_t *)pr_val = statsp->mac_stats.link_duplex;
47456439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47466439Sml29623 			    "==> nxge_m_getprop: duplex mode %d",
47476439Sml29623 			    *(uint8_t *)pr_val));
47486439Sml29623 			break;
47496439Sml29623 
47506789Sam223141 		case MAC_PROP_SPEED:
47516439Sml29623 			if (pr_valsize < sizeof (uint64_t))
47526439Sml29623 				return (EINVAL);
47538118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
47546439Sml29623 			tmp = statsp->mac_stats.link_speed * 1000000ull;
47556439Sml29623 			bcopy(&tmp, pr_val, sizeof (tmp));
47566439Sml29623 			break;
47576439Sml29623 
47586789Sam223141 		case MAC_PROP_STATUS:
47596512Ssowmini 			if (pr_valsize < sizeof (link_state_t))
47606439Sml29623 				return (EINVAL);
47618118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
47626512Ssowmini 			if (!statsp->mac_stats.link_up)
47636512Ssowmini 				ls = LINK_STATE_DOWN;
47646512Ssowmini 			else
47656512Ssowmini 				ls = LINK_STATE_UP;
47666512Ssowmini 			bcopy(&ls, pr_val, sizeof (ls));
47676439Sml29623 			break;
47686439Sml29623 
47696789Sam223141 		case MAC_PROP_AUTONEG:
47706439Sml29623 			*(uint8_t *)pr_val =
47716439Sml29623 			    param_arr[param_autoneg].value;
47726439Sml29623 			break;
47736439Sml29623 
47746789Sam223141 		case MAC_PROP_FLOWCTRL:
47756439Sml29623 			if (pr_valsize < sizeof (link_flowctrl_t))
47766439Sml29623 				return (EINVAL);
47776439Sml29623 
47786439Sml29623 			fl = LINK_FLOWCTRL_NONE;
47796439Sml29623 			if (param_arr[param_anar_pause].value) {
47806439Sml29623 				fl = LINK_FLOWCTRL_RX;
47816439Sml29623 			}
47826439Sml29623 			bcopy(&fl, pr_val, sizeof (fl));
47836439Sml29623 			break;
47846439Sml29623 
47856789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
47868118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
47876439Sml29623 			*(uint8_t *)pr_val =
47886439Sml29623 			    param_arr[param_anar_1000fdx].value;
47896439Sml29623 			break;
47906439Sml29623 
47916789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
47926439Sml29623 			*(uint8_t *)pr_val = nxgep->param_en_1000fdx;
47936439Sml29623 			break;
47946439Sml29623 
47956789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
47968118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
47976439Sml29623 			*(uint8_t *)pr_val =
47986439Sml29623 			    param_arr[param_anar_100fdx].value;
47996439Sml29623 			break;
48006439Sml29623 
48016789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
48026439Sml29623 			*(uint8_t *)pr_val = nxgep->param_en_100fdx;
48036439Sml29623 			break;
48046439Sml29623 
48056789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
48068118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
48076439Sml29623 			*(uint8_t *)pr_val =
48086439Sml29623 			    param_arr[param_anar_10fdx].value;
48096439Sml29623 			break;
48106439Sml29623 
48116789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
48126439Sml29623 			*(uint8_t *)pr_val = nxgep->param_en_10fdx;
48136439Sml29623 			break;
48146439Sml29623 
48156789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
48166789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
48176789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
48186789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
48196789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
48206789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
48216512Ssowmini 			err = ENOTSUP;
48226512Ssowmini 			break;
48236512Ssowmini 
48246789Sam223141 		case MAC_PROP_PRIVATE:
48256512Ssowmini 			err = nxge_get_priv_prop(nxgep, pr_name, pr_flags,
48268118SVasumathi.Sundaram@Sun.COM 			    pr_valsize, pr_val, perm);
48276512Ssowmini 			break;
48289514SGirish.Moodalbail@Sun.COM 
48299514SGirish.Moodalbail@Sun.COM 		case MAC_PROP_MTU: {
48309514SGirish.Moodalbail@Sun.COM 			mac_propval_range_t	range;
48319514SGirish.Moodalbail@Sun.COM 
48329514SGirish.Moodalbail@Sun.COM 			if (!(pr_flags & MAC_PROP_POSSIBLE))
48339514SGirish.Moodalbail@Sun.COM 				return (ENOTSUP);
48349514SGirish.Moodalbail@Sun.COM 			if (pr_valsize < sizeof (mac_propval_range_t))
48359514SGirish.Moodalbail@Sun.COM 				return (EINVAL);
48369514SGirish.Moodalbail@Sun.COM 			range.mpr_count = 1;
48379514SGirish.Moodalbail@Sun.COM 			range.mpr_type = MAC_PROPVAL_UINT32;
48389514SGirish.Moodalbail@Sun.COM 			range.range_uint32[0].mpur_min =
48399514SGirish.Moodalbail@Sun.COM 			    range.range_uint32[0].mpur_max = NXGE_DEFAULT_MTU;
484010392SMichael.Speer@Sun.COM 			range.range_uint32[0].mpur_max = NXGE_MAXIMUM_MTU;
48419514SGirish.Moodalbail@Sun.COM 			bcopy(&range, pr_val, sizeof (range));
48429514SGirish.Moodalbail@Sun.COM 			break;
48439514SGirish.Moodalbail@Sun.COM 		}
48446512Ssowmini 		default:
48456439Sml29623 			err = EINVAL;
48466439Sml29623 			break;
48476439Sml29623 	}
48486439Sml29623 
48496439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_getprop"));
48506439Sml29623 
48516439Sml29623 	return (err);
48526439Sml29623 }
48536439Sml29623 
48546439Sml29623 /* ARGSUSED */
48556439Sml29623 static int
48566439Sml29623 nxge_set_priv_prop(p_nxge_t nxgep, const char *pr_name, uint_t pr_valsize,
48576439Sml29623     const void *pr_val)
48586439Sml29623 {
48596439Sml29623 	p_nxge_param_t	param_arr = nxgep->param_arr;
48606439Sml29623 	int		err = 0;
48616439Sml29623 	long		result;
48626439Sml29623 
48636439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48646439Sml29623 	    "==> nxge_set_priv_prop: name %s", pr_name));
48656439Sml29623 
48666439Sml29623 	/* Blanking */
48676439Sml29623 	if (strcmp(pr_name, "_rxdma_intr_time") == 0) {
48686439Sml29623 		err = nxge_param_rx_intr_time(nxgep, NULL, NULL,
48696439Sml29623 		    (char *)pr_val,
48706439Sml29623 		    (caddr_t)&param_arr[param_rxdma_intr_time]);
48716439Sml29623 		if (err) {
48726439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48736439Sml29623 			    "<== nxge_set_priv_prop: "
48746439Sml29623 			    "unable to set (%s)", pr_name));
48756439Sml29623 			err = EINVAL;
48766439Sml29623 		} else {
48776439Sml29623 			err = 0;
48786439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48796439Sml29623 			    "<== nxge_set_priv_prop: "
48806439Sml29623 			    "set (%s)", pr_name));
48816439Sml29623 		}
48826439Sml29623 
48836439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48846439Sml29623 		    "<== nxge_set_priv_prop: name %s (value %d)",
48856439Sml29623 		    pr_name, result));
48866439Sml29623 
48876439Sml29623 		return (err);
48886439Sml29623 	}
48896439Sml29623 
48906439Sml29623 	if (strcmp(pr_name, "_rxdma_intr_pkts") == 0) {
48916439Sml29623 		err = nxge_param_rx_intr_pkts(nxgep, NULL, NULL,
48926439Sml29623 		    (char *)pr_val,
48936439Sml29623 		    (caddr_t)&param_arr[param_rxdma_intr_pkts]);
48946439Sml29623 		if (err) {
48956439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48966439Sml29623 			    "<== nxge_set_priv_prop: "
48976439Sml29623 			    "unable to set (%s)", pr_name));
48986439Sml29623 			err = EINVAL;
48996439Sml29623 		} else {
49006439Sml29623 			err = 0;
49016439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49026439Sml29623 			    "<== nxge_set_priv_prop: "
49036439Sml29623 			    "set (%s)", pr_name));
49046439Sml29623 		}
49056439Sml29623 
49066439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49076439Sml29623 		    "<== nxge_set_priv_prop: name %s (value %d)",
49086439Sml29623 		    pr_name, result));
49096439Sml29623 
49106439Sml29623 		return (err);
49116439Sml29623 	}
49126439Sml29623 
49136439Sml29623 	/* Classification */
49146439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_tcp") == 0) {
49156439Sml29623 		if (pr_val == NULL) {
49166439Sml29623 			err = EINVAL;
49176439Sml29623 			return (err);
49186439Sml29623 		}
49196439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
49206439Sml29623 
49216439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
49226439Sml29623 		    NULL, (char *)pr_val,
49236439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_tcp]);
49246439Sml29623 
49256439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49266439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
49276439Sml29623 		    pr_name, result));
49286439Sml29623 
49296439Sml29623 		return (err);
49306439Sml29623 	}
49316439Sml29623 
49326439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_udp") == 0) {
49336439Sml29623 		if (pr_val == NULL) {
49346439Sml29623 			err = EINVAL;
49356439Sml29623 			return (err);
49366439Sml29623 		}
49376439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
49386439Sml29623 
49396439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
49406439Sml29623 		    NULL, (char *)pr_val,
49416439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_udp]);
49426439Sml29623 
49436439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49446439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
49456439Sml29623 		    pr_name, result));
49466439Sml29623 
49476439Sml29623 		return (err);
49486439Sml29623 	}
49496439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_ah") == 0) {
49506439Sml29623 		if (pr_val == NULL) {
49516439Sml29623 			err = EINVAL;
49526439Sml29623 			return (err);
49536439Sml29623 		}
49546439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
49556439Sml29623 
49566439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
49576439Sml29623 		    NULL, (char *)pr_val,
49586439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_ah]);
49596439Sml29623 
49606439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49616439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
49626439Sml29623 		    pr_name, result));
49636439Sml29623 
49646439Sml29623 		return (err);
49656439Sml29623 	}
49666439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_sctp") == 0) {
49676439Sml29623 		if (pr_val == NULL) {
49686439Sml29623 			err = EINVAL;
49696439Sml29623 			return (err);
49706439Sml29623 		}
49716439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
49726439Sml29623 
49736439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
49746439Sml29623 		    NULL, (char *)pr_val,
49756439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_sctp]);
49766439Sml29623 
49776439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49786439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
49796439Sml29623 		    pr_name, result));
49806439Sml29623 
49816439Sml29623 		return (err);
49826439Sml29623 	}
49836439Sml29623 
49846439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_tcp") == 0) {
49856439Sml29623 		if (pr_val == NULL) {
49866439Sml29623 			err = EINVAL;
49876439Sml29623 			return (err);
49886439Sml29623 		}
49896439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
49906439Sml29623 
49916439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
49926439Sml29623 		    NULL, (char *)pr_val,
49936439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_tcp]);
49946439Sml29623 
49956439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49966439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
49976439Sml29623 		    pr_name, result));
49986439Sml29623 
49996439Sml29623 		return (err);
50006439Sml29623 	}
50016439Sml29623 
50026439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_udp") == 0) {
50036439Sml29623 		if (pr_val == NULL) {
50046439Sml29623 			err = EINVAL;
50056439Sml29623 			return (err);
50066439Sml29623 		}
50076439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
50086439Sml29623 
50096439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
50106439Sml29623 		    NULL, (char *)pr_val,
50116439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_udp]);
50126439Sml29623 
50136439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50146439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
50156439Sml29623 		    pr_name, result));
50166439Sml29623 
50176439Sml29623 		return (err);
50186439Sml29623 	}
50196439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_ah") == 0) {
50206439Sml29623 		if (pr_val == NULL) {
50216439Sml29623 			err = EINVAL;
50226439Sml29623 			return (err);
50236439Sml29623 		}
50246439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
50256439Sml29623 
50266439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
50276439Sml29623 		    NULL, (char *)pr_val,
50286439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_ah]);
50296439Sml29623 
50306439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50316439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
50326439Sml29623 		    pr_name, result));
50336439Sml29623 
50346439Sml29623 		return (err);
50356439Sml29623 	}
50366439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_sctp") == 0) {
50376439Sml29623 		if (pr_val == NULL) {
50386439Sml29623 			err = EINVAL;
50396439Sml29623 			return (err);
50406439Sml29623 		}
50416439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
50426439Sml29623 
50436439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
50446439Sml29623 		    NULL, (char *)pr_val,
50456439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_sctp]);
50466439Sml29623 
50476439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50486439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
50496439Sml29623 		    pr_name, result));
50506439Sml29623 
50516439Sml29623 		return (err);
50526439Sml29623 	}
50536439Sml29623 
50546439Sml29623 	if (strcmp(pr_name, "_soft_lso_enable") == 0) {
50556439Sml29623 		if (pr_val == NULL) {
50566439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50576439Sml29623 			    "==> nxge_set_priv_prop: name %s (null)", pr_name));
50586439Sml29623 			err = EINVAL;
50596439Sml29623 			return (err);
50606439Sml29623 		}
50616439Sml29623 
50626439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
50636439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50646439Sml29623 		    "<== nxge_set_priv_prop: name %s "
50656439Sml29623 		    "(lso %d pr_val %s value %d)",
50666439Sml29623 		    pr_name, nxgep->soft_lso_enable, pr_val, result));
50676439Sml29623 
50686439Sml29623 		if (result > 1 || result < 0) {
50696439Sml29623 			err = EINVAL;
50706439Sml29623 		} else {
50716439Sml29623 			if (nxgep->soft_lso_enable == (uint32_t)result) {
50726439Sml29623 				NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50736439Sml29623 				    "no change (%d %d)",
50746439Sml29623 				    nxgep->soft_lso_enable, result));
50756439Sml29623 				return (0);
50766439Sml29623 			}
50776439Sml29623 		}
50786439Sml29623 
50796439Sml29623 		nxgep->soft_lso_enable = (int)result;
50806439Sml29623 
50816439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50826439Sml29623 		    "<== nxge_set_priv_prop: name %s (value %d)",
50836439Sml29623 		    pr_name, result));
50846439Sml29623 
50856439Sml29623 		return (err);
50866439Sml29623 	}
50876835Syc148097 	/*
50886835Syc148097 	 * Commands like "ndd -set /dev/nxge0 adv_10gfdx_cap 1" cause the
50896835Syc148097 	 * following code to be executed.
50906835Syc148097 	 */
50916512Ssowmini 	if (strcmp(pr_name, "_adv_10gfdx_cap") == 0) {
50926512Ssowmini 		err = nxge_param_set_mac(nxgep, NULL, NULL, (char *)pr_val,
50936512Ssowmini 		    (caddr_t)&param_arr[param_anar_10gfdx]);
50946512Ssowmini 		return (err);
50956512Ssowmini 	}
50966512Ssowmini 	if (strcmp(pr_name, "_adv_pause_cap") == 0) {
50976512Ssowmini 		err = nxge_param_set_mac(nxgep, NULL, NULL, (char *)pr_val,
50986512Ssowmini 		    (caddr_t)&param_arr[param_anar_pause]);
50996512Ssowmini 		return (err);
51006512Ssowmini 	}
51016439Sml29623 
51026439Sml29623 	return (EINVAL);
51036439Sml29623 }
51046439Sml29623 
51056439Sml29623 static int
51066512Ssowmini nxge_get_priv_prop(p_nxge_t nxgep, const char *pr_name, uint_t pr_flags,
51078118SVasumathi.Sundaram@Sun.COM     uint_t pr_valsize, void *pr_val, uint_t *perm)
51086439Sml29623 {
51096439Sml29623 	p_nxge_param_t	param_arr = nxgep->param_arr;
51106439Sml29623 	char		valstr[MAXNAMELEN];
51116439Sml29623 	int		err = EINVAL;
51126439Sml29623 	uint_t		strsize;
51136789Sam223141 	boolean_t	is_default = (pr_flags & MAC_PROP_DEFAULT);
51146439Sml29623 
51156439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51166439Sml29623 	    "==> nxge_get_priv_prop: property %s", pr_name));
51176439Sml29623 
51186439Sml29623 	/* function number */
51196439Sml29623 	if (strcmp(pr_name, "_function_number") == 0) {
51206512Ssowmini 		if (is_default)
51216512Ssowmini 			return (ENOTSUP);
51228118SVasumathi.Sundaram@Sun.COM 		*perm = MAC_PROP_PERM_READ;
51236512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%d",
51246512Ssowmini 		    nxgep->function_num);
51256439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51266439Sml29623 		    "==> nxge_get_priv_prop: name %s "
51276439Sml29623 		    "(value %d valstr %s)",
51286439Sml29623 		    pr_name, nxgep->function_num, valstr));
51296439Sml29623 
51306439Sml29623 		err = 0;
51316439Sml29623 		goto done;
51326439Sml29623 	}
51336439Sml29623 
51346439Sml29623 	/* Neptune firmware version */
51356439Sml29623 	if (strcmp(pr_name, "_fw_version") == 0) {
51366512Ssowmini 		if (is_default)
51376512Ssowmini 			return (ENOTSUP);
51388118SVasumathi.Sundaram@Sun.COM 		*perm = MAC_PROP_PERM_READ;
51396512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%s",
51406512Ssowmini 		    nxgep->vpd_info.ver);
51416439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51426439Sml29623 		    "==> nxge_get_priv_prop: name %s "
51436439Sml29623 		    "(value %d valstr %s)",
51446439Sml29623 		    pr_name, nxgep->vpd_info.ver, valstr));
51456439Sml29623 
51466439Sml29623 		err = 0;
51476439Sml29623 		goto done;
51486439Sml29623 	}
51496439Sml29623 
51506439Sml29623 	/* port PHY mode */
51516439Sml29623 	if (strcmp(pr_name, "_port_mode") == 0) {
51526512Ssowmini 		if (is_default)
51536512Ssowmini 			return (ENOTSUP);
51548118SVasumathi.Sundaram@Sun.COM 		*perm = MAC_PROP_PERM_READ;
51556439Sml29623 		switch (nxgep->mac.portmode) {
51566439Sml29623 		case PORT_1G_COPPER:
51576512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "1G copper %s",
51586439Sml29623 			    nxgep->hot_swappable_phy ?
51596439Sml29623 			    "[Hot Swappable]" : "");
51606439Sml29623 			break;
51616439Sml29623 		case PORT_1G_FIBER:
51626512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "1G fiber %s",
51636439Sml29623 			    nxgep->hot_swappable_phy ?
51646439Sml29623 			    "[hot swappable]" : "");
51656439Sml29623 			break;
51666439Sml29623 		case PORT_10G_COPPER:
51676512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
51686512Ssowmini 			    "10G copper %s",
51696439Sml29623 			    nxgep->hot_swappable_phy ?
51706439Sml29623 			    "[hot swappable]" : "");
51716439Sml29623 			break;
51726439Sml29623 		case PORT_10G_FIBER:
51736512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "10G fiber %s",
51746439Sml29623 			    nxgep->hot_swappable_phy ?
51756439Sml29623 			    "[hot swappable]" : "");
51766439Sml29623 			break;
51776439Sml29623 		case PORT_10G_SERDES:
51786512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
51796512Ssowmini 			    "10G serdes %s", nxgep->hot_swappable_phy ?
51806439Sml29623 			    "[hot swappable]" : "");
51816439Sml29623 			break;
51826439Sml29623 		case PORT_1G_SERDES:
51836512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "1G serdes %s",
51846439Sml29623 			    nxgep->hot_swappable_phy ?
51856439Sml29623 			    "[hot swappable]" : "");
51866439Sml29623 			break;
51876835Syc148097 		case PORT_1G_TN1010:
51886835Syc148097 			(void) snprintf(valstr, sizeof (valstr),
51896835Syc148097 			    "1G TN1010 copper %s", nxgep->hot_swappable_phy ?
51906835Syc148097 			    "[hot swappable]" : "");
51916835Syc148097 			break;
51926835Syc148097 		case PORT_10G_TN1010:
51936835Syc148097 			(void) snprintf(valstr, sizeof (valstr),
51946835Syc148097 			    "10G TN1010 copper %s", nxgep->hot_swappable_phy ?
51956835Syc148097 			    "[hot swappable]" : "");
51966835Syc148097 			break;
51976439Sml29623 		case PORT_1G_RGMII_FIBER:
51986512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
51996512Ssowmini 			    "1G rgmii fiber %s", nxgep->hot_swappable_phy ?
52006439Sml29623 			    "[hot swappable]" : "");
52016439Sml29623 			break;
52026439Sml29623 		case PORT_HSP_MODE:
52036512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
52046444Sml29623 			    "phy not present[hot swappable]");
52056439Sml29623 			break;
52066439Sml29623 		default:
52076512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "unknown %s",
52086439Sml29623 			    nxgep->hot_swappable_phy ?
52096439Sml29623 			    "[hot swappable]" : "");
52106439Sml29623 			break;
52116439Sml29623 		}
52126439Sml29623 
52136439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
52146439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %s)",
52156439Sml29623 		    pr_name, valstr));
52166439Sml29623 
52176439Sml29623 		err = 0;
52186439Sml29623 		goto done;
52196439Sml29623 	}
52206439Sml29623 
52216439Sml29623 	/* Hot swappable PHY */
52226439Sml29623 	if (strcmp(pr_name, "_hot_swap_phy") == 0) {
52236512Ssowmini 		if (is_default)
52246512Ssowmini 			return (ENOTSUP);
52258118SVasumathi.Sundaram@Sun.COM 		*perm = MAC_PROP_PERM_READ;
52266512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%s",
52276439Sml29623 		    nxgep->hot_swappable_phy ?
52286439Sml29623 		    "yes" : "no");
52296439Sml29623 
52306439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
52316439Sml29623 		    "==> nxge_get_priv_prop: name %s "
52326439Sml29623 		    "(value %d valstr %s)",
52336439Sml29623 		    pr_name, nxgep->hot_swappable_phy, valstr));
52346439Sml29623 
52356439Sml29623 		err = 0;
52366439Sml29623 		goto done;
52376439Sml29623 	}
52386439Sml29623 
52396439Sml29623 
52406439Sml29623 	/* Receive Interrupt Blanking Parameters */
52416439Sml29623 	if (strcmp(pr_name, "_rxdma_intr_time") == 0) {
52426512Ssowmini 		err = 0;
52436512Ssowmini 		if (is_default) {
52446512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
52456512Ssowmini 			    "%d", RXDMA_RCR_TO_DEFAULT);
52466512Ssowmini 			goto done;
52476512Ssowmini 		}
52486512Ssowmini 
52496512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%d",
52506512Ssowmini 		    nxgep->intr_timeout);
52516439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
52526439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %d)",
52536439Sml29623 		    pr_name,
52546439Sml29623 		    (uint32_t)nxgep->intr_timeout));
52556439Sml29623 		goto done;
52566439Sml29623 	}
52576439Sml29623 
52586439Sml29623 	if (strcmp(pr_name, "_rxdma_intr_pkts") == 0) {
52596512Ssowmini 		err = 0;
52606512Ssowmini 		if (is_default) {
52616512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
52626512Ssowmini 			    "%d", RXDMA_RCR_PTHRES_DEFAULT);
52636512Ssowmini 			goto done;
52646512Ssowmini 		}
52656512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%d",
52666512Ssowmini 		    nxgep->intr_threshold);
52676439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
52686439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %d)",
52696439Sml29623 		    pr_name, (uint32_t)nxgep->intr_threshold));
52706439Sml29623 
52716439Sml29623 		goto done;
52726439Sml29623 	}
52736439Sml29623 
52746439Sml29623 	/* Classification and Load Distribution Configuration */
52756439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_tcp") == 0) {
52766512Ssowmini 		if (is_default) {
52776512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
52786512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
52796512Ssowmini 			err = 0;
52806512Ssowmini 			goto done;
52816512Ssowmini 		}
52826439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
52836439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_tcp]);
52846439Sml29623 
52856512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
52866439Sml29623 		    (int)param_arr[param_class_opt_ipv4_tcp].value);
52876439Sml29623 
52886439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
52896439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
52906439Sml29623 		goto done;
52916439Sml29623 	}
52926439Sml29623 
52936439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_udp") == 0) {
52946512Ssowmini 		if (is_default) {
52956512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
52966512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
52976512Ssowmini 			err = 0;
52986512Ssowmini 			goto done;
52996512Ssowmini 		}
53006439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
53016439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_udp]);
53026439Sml29623 
53036512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
53046439Sml29623 		    (int)param_arr[param_class_opt_ipv4_udp].value);
53056439Sml29623 
53066439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53076439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
53086439Sml29623 		goto done;
53096439Sml29623 	}
53106439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_ah") == 0) {
53116512Ssowmini 		if (is_default) {
53126512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
53136512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
53146512Ssowmini 			err = 0;
53156512Ssowmini 			goto done;
53166512Ssowmini 		}
53176439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
53186439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_ah]);
53196439Sml29623 
53206512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
53216439Sml29623 		    (int)param_arr[param_class_opt_ipv4_ah].value);
53226439Sml29623 
53236439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53246439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
53256439Sml29623 		goto done;
53266439Sml29623 	}
53276439Sml29623 
53286439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_sctp") == 0) {
53296512Ssowmini 		if (is_default) {
53306512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
53316512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
53326512Ssowmini 			err = 0;
53336512Ssowmini 			goto done;
53346512Ssowmini 		}
53356439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
53366439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_sctp]);
53376439Sml29623 
53386512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
53396439Sml29623 		    (int)param_arr[param_class_opt_ipv4_sctp].value);
53406439Sml29623 
53416439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53426439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
53436439Sml29623 		goto done;
53446439Sml29623 	}
53456439Sml29623 
53466439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_tcp") == 0) {
53476512Ssowmini 		if (is_default) {
53486512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
53496512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
53506512Ssowmini 			err = 0;
53516512Ssowmini 			goto done;
53526512Ssowmini 		}
53536439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
53546439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_tcp]);
53556439Sml29623 
53566512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
53576439Sml29623 		    (int)param_arr[param_class_opt_ipv6_tcp].value);
53586439Sml29623 
53596439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53606439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
53616439Sml29623 		goto done;
53626439Sml29623 	}
53636439Sml29623 
53646439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_udp") == 0) {
53656512Ssowmini 		if (is_default) {
53666512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
53676512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
53686512Ssowmini 			err = 0;
53696512Ssowmini 			goto done;
53706512Ssowmini 		}
53716439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
53726439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_udp]);
53736439Sml29623 
53746512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
53756439Sml29623 		    (int)param_arr[param_class_opt_ipv6_udp].value);
53766439Sml29623 
53776439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53786439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
53796439Sml29623 		goto done;
53806439Sml29623 	}
53816439Sml29623 
53826439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_ah") == 0) {
53836512Ssowmini 		if (is_default) {
53846512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
53856512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
53866512Ssowmini 			err = 0;
53876512Ssowmini 			goto done;
53886512Ssowmini 		}
53896439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
53906439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_ah]);
53916439Sml29623 
53926512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
53936439Sml29623 		    (int)param_arr[param_class_opt_ipv6_ah].value);
53946439Sml29623 
53956439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53966439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
53976439Sml29623 		goto done;
53986439Sml29623 	}
53996439Sml29623 
54006439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_sctp") == 0) {
54016512Ssowmini 		if (is_default) {
54026512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
54036512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
54046512Ssowmini 			err = 0;
54056512Ssowmini 			goto done;
54066512Ssowmini 		}
54076439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
54086439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_sctp]);
54096439Sml29623 
54106512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
54116439Sml29623 		    (int)param_arr[param_class_opt_ipv6_sctp].value);
54126439Sml29623 
54136439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
54146439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
54156439Sml29623 		goto done;
54166439Sml29623 	}
54176439Sml29623 
54186439Sml29623 	/* Software LSO */
54196439Sml29623 	if (strcmp(pr_name, "_soft_lso_enable") == 0) {
54206512Ssowmini 		if (is_default) {
54216512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 0);
54226512Ssowmini 			err = 0;
54236512Ssowmini 			goto done;
54246512Ssowmini 		}
54256512Ssowmini 		(void) snprintf(valstr, sizeof (valstr),
54266512Ssowmini 		    "%d", nxgep->soft_lso_enable);
54276439Sml29623 		err = 0;
54286439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
54296439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %d)",
54306439Sml29623 		    pr_name, nxgep->soft_lso_enable));
54316439Sml29623 
54326439Sml29623 		goto done;
54336439Sml29623 	}
54346512Ssowmini 	if (strcmp(pr_name, "_adv_10gfdx_cap") == 0) {
54356512Ssowmini 		err = 0;
54366512Ssowmini 		if (is_default ||
54376512Ssowmini 		    nxgep->param_arr[param_anar_10gfdx].value != 0) {
54386512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 1);
54396512Ssowmini 			goto done;
54406512Ssowmini 		} else {
54416512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 0);
54426512Ssowmini 			goto done;
54436512Ssowmini 		}
54446512Ssowmini 	}
54456512Ssowmini 	if (strcmp(pr_name, "_adv_pause_cap") == 0) {
54466512Ssowmini 		err = 0;
54476512Ssowmini 		if (is_default ||
54486512Ssowmini 		    nxgep->param_arr[param_anar_pause].value != 0) {
54496512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 1);
54506512Ssowmini 			goto done;
54516512Ssowmini 		} else {
54526512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 0);
54536512Ssowmini 			goto done;
54546512Ssowmini 		}
54556512Ssowmini 	}
54566439Sml29623 
54576439Sml29623 done:
54586439Sml29623 	if (err == 0) {
54596439Sml29623 		strsize = (uint_t)strlen(valstr);
54606439Sml29623 		if (pr_valsize < strsize) {
54616439Sml29623 			err = ENOBUFS;
54626439Sml29623 		} else {
54636439Sml29623 			(void) strlcpy(pr_val, valstr, pr_valsize);
54646439Sml29623 		}
54656439Sml29623 	}
54666439Sml29623 
54676439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
54686439Sml29623 	    "<== nxge_get_priv_prop: return %d", err));
54696439Sml29623 	return (err);
54706439Sml29623 }
54716439Sml29623 
54723859Sml29623 /*
54733859Sml29623  * Module loading and removing entry points.
54743859Sml29623  */
54753859Sml29623 
54766705Sml29623 DDI_DEFINE_STREAM_OPS(nxge_dev_ops, nulldev, nulldev, nxge_attach, nxge_detach,
54777656SSherry.Moore@Sun.COM     nodev, NULL, D_MP, NULL, nxge_quiesce);
54783859Sml29623 
54794977Sraghus #define	NXGE_DESC_VER		"Sun NIU 10Gb Ethernet"
54803859Sml29623 
54813859Sml29623 /*
54823859Sml29623  * Module linkage information for the kernel.
54833859Sml29623  */
54843859Sml29623 static struct modldrv 	nxge_modldrv = {
54853859Sml29623 	&mod_driverops,
54863859Sml29623 	NXGE_DESC_VER,
54873859Sml29623 	&nxge_dev_ops
54883859Sml29623 };
54893859Sml29623 
54903859Sml29623 static struct modlinkage modlinkage = {
54913859Sml29623 	MODREV_1, (void *) &nxge_modldrv, NULL
54923859Sml29623 };
54933859Sml29623 
54943859Sml29623 int
54953859Sml29623 _init(void)
54963859Sml29623 {
54973859Sml29623 	int		status;
54983859Sml29623 
54999935SMichael.Speer@Sun.COM 	MUTEX_INIT(&nxgedebuglock, NULL, MUTEX_DRIVER, NULL);
55009935SMichael.Speer@Sun.COM 
55013859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "==> _init"));
55029935SMichael.Speer@Sun.COM 
55033859Sml29623 	mac_init_ops(&nxge_dev_ops, "nxge");
55049935SMichael.Speer@Sun.COM 
55053859Sml29623 	status = ddi_soft_state_init(&nxge_list, sizeof (nxge_t), 0);
55063859Sml29623 	if (status != 0) {
55073859Sml29623 		NXGE_ERROR_MSG((NULL, NXGE_ERR_CTL,
55086512Ssowmini 		    "failed to init device soft state"));
55093859Sml29623 		goto _init_exit;
55103859Sml29623 	}
55119935SMichael.Speer@Sun.COM 
55123859Sml29623 	status = mod_install(&modlinkage);
55133859Sml29623 	if (status != 0) {
55143859Sml29623 		ddi_soft_state_fini(&nxge_list);
55153859Sml29623 		NXGE_ERROR_MSG((NULL, NXGE_ERR_CTL, "Mod install failed"));
55163859Sml29623 		goto _init_exit;
55173859Sml29623 	}
55183859Sml29623 
55193859Sml29623 	MUTEX_INIT(&nxge_common_lock, NULL, MUTEX_DRIVER, NULL);
55203859Sml29623 
55219935SMichael.Speer@Sun.COM 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "<== _init status = 0x%X", status));
55229935SMichael.Speer@Sun.COM 	return (status);
55239935SMichael.Speer@Sun.COM 
55243859Sml29623 _init_exit:
55259935SMichael.Speer@Sun.COM 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "<== _init status = 0x%X", status));
55269935SMichael.Speer@Sun.COM 	MUTEX_DESTROY(&nxgedebuglock);
55273859Sml29623 	return (status);
55283859Sml29623 }
55293859Sml29623 
55303859Sml29623 int
55313859Sml29623 _fini(void)
55323859Sml29623 {
55333859Sml29623 	int		status;
55343859Sml29623 
55353859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "==> _fini"));
55363859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "==> _fini: mod_remove"));
55373859Sml29623 
55383859Sml29623 	if (nxge_mblks_pending)
55393859Sml29623 		return (EBUSY);
55403859Sml29623 
55413859Sml29623 	status = mod_remove(&modlinkage);
55423859Sml29623 	if (status != DDI_SUCCESS) {
55433859Sml29623 		NXGE_DEBUG_MSG((NULL, MOD_CTL,
55446512Ssowmini 		    "Module removal failed 0x%08x",
55456512Ssowmini 		    status));
55463859Sml29623 		goto _fini_exit;
55473859Sml29623 	}
55483859Sml29623 
55493859Sml29623 	mac_fini_ops(&nxge_dev_ops);
55503859Sml29623 
55513859Sml29623 	ddi_soft_state_fini(&nxge_list);
55523859Sml29623 
55539935SMichael.Speer@Sun.COM 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "<== _fini status = 0x%08x", status));
55549935SMichael.Speer@Sun.COM 
55553859Sml29623 	MUTEX_DESTROY(&nxge_common_lock);
55569935SMichael.Speer@Sun.COM 	MUTEX_DESTROY(&nxgedebuglock);
55579935SMichael.Speer@Sun.COM 	return (status);
55589935SMichael.Speer@Sun.COM 
55593859Sml29623 _fini_exit:
55609935SMichael.Speer@Sun.COM 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "<== _fini status = 0x%08x", status));
55613859Sml29623 	return (status);
55623859Sml29623 }
55633859Sml29623 
55643859Sml29623 int
55653859Sml29623 _info(struct modinfo *modinfop)
55663859Sml29623 {
55673859Sml29623 	int		status;
55683859Sml29623 
55693859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "==> _info"));
55703859Sml29623 	status = mod_info(&modlinkage, modinfop);
55713859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, " _info status = 0x%X", status));
55723859Sml29623 
55733859Sml29623 	return (status);
55743859Sml29623 }
55753859Sml29623 
55763859Sml29623 /*ARGSUSED*/
55778275SEric Cheng static int
55788275SEric Cheng nxge_tx_ring_start(mac_ring_driver_t rdriver, uint64_t mr_gen_num)
55798275SEric Cheng {
55808275SEric Cheng 	p_nxge_ring_handle_t	rhp = (p_nxge_ring_handle_t)rdriver;
55818275SEric Cheng 	p_nxge_t		nxgep = rhp->nxgep;
55828275SEric Cheng 	uint32_t		channel;
55838275SEric Cheng 	p_tx_ring_t		ring;
55848275SEric Cheng 
55858275SEric Cheng 	channel = nxgep->pt_config.hw_config.tdc.start + rhp->index;
55868275SEric Cheng 	ring = nxgep->tx_rings->rings[channel];
55878275SEric Cheng 
55888275SEric Cheng 	MUTEX_ENTER(&ring->lock);
55898275SEric Cheng 	ring->tx_ring_handle = rhp->ring_handle;
55908275SEric Cheng 	MUTEX_EXIT(&ring->lock);
55918275SEric Cheng 
55928275SEric Cheng 	return (0);
55938275SEric Cheng }
55948275SEric Cheng 
55958275SEric Cheng static void
55968275SEric Cheng nxge_tx_ring_stop(mac_ring_driver_t rdriver)
55978275SEric Cheng {
55988275SEric Cheng 	p_nxge_ring_handle_t	rhp = (p_nxge_ring_handle_t)rdriver;
55998275SEric Cheng 	p_nxge_t		nxgep = rhp->nxgep;
56008275SEric Cheng 	uint32_t		channel;
56018275SEric Cheng 	p_tx_ring_t		ring;
56028275SEric Cheng 
56038275SEric Cheng 	channel = nxgep->pt_config.hw_config.tdc.start + rhp->index;
56048275SEric Cheng 	ring = nxgep->tx_rings->rings[channel];
56058275SEric Cheng 
56068275SEric Cheng 	MUTEX_ENTER(&ring->lock);
56078275SEric Cheng 	ring->tx_ring_handle = (mac_ring_handle_t)NULL;
56088275SEric Cheng 	MUTEX_EXIT(&ring->lock);
56098275SEric Cheng }
56108275SEric Cheng 
56118275SEric Cheng static int
56128275SEric Cheng nxge_rx_ring_start(mac_ring_driver_t rdriver, uint64_t mr_gen_num)
56138275SEric Cheng {
56148275SEric Cheng 	p_nxge_ring_handle_t	rhp = (p_nxge_ring_handle_t)rdriver;
56158275SEric Cheng 	p_nxge_t		nxgep = rhp->nxgep;
56168275SEric Cheng 	uint32_t		channel;
56178275SEric Cheng 	p_rx_rcr_ring_t		ring;
56188275SEric Cheng 	int			i;
56198275SEric Cheng 
56208275SEric Cheng 	channel = nxgep->pt_config.hw_config.start_rdc + rhp->index;
56218275SEric Cheng 	ring =  nxgep->rx_rcr_rings->rcr_rings[channel];
56228275SEric Cheng 
56238275SEric Cheng 	MUTEX_ENTER(&ring->lock);
56248275SEric Cheng 
56258275SEric Cheng 	if (nxgep->rx_channel_started[channel] == B_TRUE) {
56268275SEric Cheng 		MUTEX_EXIT(&ring->lock);
56278275SEric Cheng 		return (0);
56288275SEric Cheng 	}
56298275SEric Cheng 
56308275SEric Cheng 	/* set rcr_ring */
56318275SEric Cheng 	for (i = 0; i < nxgep->ldgvp->maxldvs; i++) {
56328275SEric Cheng 		if ((nxgep->ldgvp->ldvp[i].is_rxdma == 1) &&
56338275SEric Cheng 		    (nxgep->ldgvp->ldvp[i].channel == channel)) {
56348275SEric Cheng 			ring->ldvp = &nxgep->ldgvp->ldvp[i];
56358275SEric Cheng 			ring->ldgp = nxgep->ldgvp->ldvp[i].ldgp;
56368275SEric Cheng 		}
56378275SEric Cheng 	}
56388275SEric Cheng 
56398275SEric Cheng 	nxgep->rx_channel_started[channel] = B_TRUE;
56408275SEric Cheng 	ring->rcr_mac_handle = rhp->ring_handle;
56418275SEric Cheng 	ring->rcr_gen_num = mr_gen_num;
56428275SEric Cheng 	MUTEX_EXIT(&ring->lock);
56438275SEric Cheng 
56448275SEric Cheng 	return (0);
56458275SEric Cheng }
56468275SEric Cheng 
56478275SEric Cheng static void
56488275SEric Cheng nxge_rx_ring_stop(mac_ring_driver_t rdriver)
56498275SEric Cheng {
56508275SEric Cheng 	p_nxge_ring_handle_t	rhp = (p_nxge_ring_handle_t)rdriver;
56518275SEric Cheng 	p_nxge_t		nxgep = rhp->nxgep;
56528275SEric Cheng 	uint32_t		channel;
56538275SEric Cheng 	p_rx_rcr_ring_t		ring;
56548275SEric Cheng 
56558275SEric Cheng 	channel = nxgep->pt_config.hw_config.start_rdc + rhp->index;
56568275SEric Cheng 	ring =  nxgep->rx_rcr_rings->rcr_rings[channel];
56578275SEric Cheng 
56588275SEric Cheng 	MUTEX_ENTER(&ring->lock);
56598275SEric Cheng 	nxgep->rx_channel_started[channel] = B_FALSE;
56608275SEric Cheng 	ring->rcr_mac_handle = NULL;
56618275SEric Cheng 	MUTEX_EXIT(&ring->lock);
56628275SEric Cheng }
56638275SEric Cheng 
56648275SEric Cheng /*
56658275SEric Cheng  * Callback funtion for MAC layer to register all rings.
56668275SEric Cheng  */
56678275SEric Cheng static void
56688275SEric Cheng nxge_fill_ring(void *arg, mac_ring_type_t rtype, const int rg_index,
56698275SEric Cheng     const int index, mac_ring_info_t *infop, mac_ring_handle_t rh)
56708275SEric Cheng {
56718275SEric Cheng 	p_nxge_t		nxgep = (p_nxge_t)arg;
56728275SEric Cheng 	p_nxge_hw_pt_cfg_t	p_cfgp = &nxgep->pt_config.hw_config;
56738275SEric Cheng 
56748275SEric Cheng 	NXGE_DEBUG_MSG((nxgep, TX_CTL,
56758275SEric Cheng 	    "==> nxge_fill_ring 0x%x index %d", rtype, index));
56768275SEric Cheng 
56778275SEric Cheng 	switch (rtype) {
56788275SEric Cheng 	case MAC_RING_TYPE_TX: {
56798275SEric Cheng 		p_nxge_ring_handle_t	rhandlep;
56808275SEric Cheng 
56818275SEric Cheng 		NXGE_DEBUG_MSG((nxgep, TX_CTL,
56828275SEric Cheng 		    "==> nxge_fill_ring (TX) 0x%x index %d ntdcs %d",
56838275SEric Cheng 		    rtype, index, p_cfgp->tdc.count));
56848275SEric Cheng 
56858275SEric Cheng 		ASSERT((index >= 0) && (index < p_cfgp->tdc.count));
56868275SEric Cheng 		rhandlep = &nxgep->tx_ring_handles[index];
56878275SEric Cheng 		rhandlep->nxgep = nxgep;
56888275SEric Cheng 		rhandlep->index = index;
56898275SEric Cheng 		rhandlep->ring_handle = rh;
56908275SEric Cheng 
56918275SEric Cheng 		infop->mri_driver = (mac_ring_driver_t)rhandlep;
56928275SEric Cheng 		infop->mri_start = nxge_tx_ring_start;
56938275SEric Cheng 		infop->mri_stop = nxge_tx_ring_stop;
56948275SEric Cheng 		infop->mri_tx = nxge_tx_ring_send;
56958275SEric Cheng 
56968275SEric Cheng 		break;
56978275SEric Cheng 	}
56988275SEric Cheng 	case MAC_RING_TYPE_RX: {
56998275SEric Cheng 		p_nxge_ring_handle_t	rhandlep;
57008275SEric Cheng 		int			nxge_rindex;
57018275SEric Cheng 		mac_intr_t		nxge_mac_intr;
57028275SEric Cheng 
57038275SEric Cheng 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
57048275SEric Cheng 		    "==> nxge_fill_ring (RX) 0x%x index %d nrdcs %d",
57058275SEric Cheng 		    rtype, index, p_cfgp->max_rdcs));
57068275SEric Cheng 
57078275SEric Cheng 		/*
57088275SEric Cheng 		 * 'index' is the ring index within the group.
57098275SEric Cheng 		 * Find the ring index in the nxge instance.
57108275SEric Cheng 		 */
57118275SEric Cheng 		nxge_rindex = nxge_get_rxring_index(nxgep, rg_index, index);
57128275SEric Cheng 
57138275SEric Cheng 		ASSERT((nxge_rindex >= 0) && (nxge_rindex < p_cfgp->max_rdcs));
57148275SEric Cheng 		rhandlep = &nxgep->rx_ring_handles[nxge_rindex];
57158275SEric Cheng 		rhandlep->nxgep = nxgep;
57168275SEric Cheng 		rhandlep->index = nxge_rindex;
57178275SEric Cheng 		rhandlep->ring_handle = rh;
57188275SEric Cheng 
57198275SEric Cheng 		/*
57208275SEric Cheng 		 * Entrypoint to enable interrupt (disable poll) and
57218275SEric Cheng 		 * disable interrupt (enable poll).
57228275SEric Cheng 		 */
57238275SEric Cheng 		nxge_mac_intr.mi_handle = (mac_intr_handle_t)rhandlep;
57248275SEric Cheng 		nxge_mac_intr.mi_enable = (mac_intr_enable_t)nxge_disable_poll;
57258275SEric Cheng 		nxge_mac_intr.mi_disable = (mac_intr_disable_t)nxge_enable_poll;
57268275SEric Cheng 		infop->mri_driver = (mac_ring_driver_t)rhandlep;
57278275SEric Cheng 		infop->mri_start = nxge_rx_ring_start;
57288275SEric Cheng 		infop->mri_stop = nxge_rx_ring_stop;
57298275SEric Cheng 		infop->mri_intr = nxge_mac_intr; /* ??? */
57308275SEric Cheng 		infop->mri_poll = nxge_rx_poll;
57318275SEric Cheng 
57328275SEric Cheng 		break;
57338275SEric Cheng 	}
57348275SEric Cheng 	default:
57358275SEric Cheng 		break;
57368275SEric Cheng 	}
57378275SEric Cheng 
57388275SEric Cheng 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_fill_ring 0x%x",
57398275SEric Cheng 	    rtype));
57408275SEric Cheng }
57418275SEric Cheng 
57428275SEric Cheng static void
57438275SEric Cheng nxge_group_add_ring(mac_group_driver_t gh, mac_ring_driver_t rh,
57448275SEric Cheng     mac_ring_type_t type)
57458275SEric Cheng {
57468275SEric Cheng 	nxge_ring_group_t	*rgroup = (nxge_ring_group_t *)gh;
57478275SEric Cheng 	nxge_ring_handle_t	*rhandle = (nxge_ring_handle_t *)rh;
57488275SEric Cheng 	nxge_t			*nxge;
57498275SEric Cheng 	nxge_grp_t		*grp;
57508275SEric Cheng 	nxge_rdc_grp_t		*rdc_grp;
57518275SEric Cheng 	uint16_t		channel;	/* device-wise ring id */
57528275SEric Cheng 	int			dev_gindex;
57538275SEric Cheng 	int			rv;
57548275SEric Cheng 
57558275SEric Cheng 	nxge = rgroup->nxgep;
57568275SEric Cheng 
57578275SEric Cheng 	switch (type) {
57588275SEric Cheng 	case MAC_RING_TYPE_TX:
57598275SEric Cheng 		/*
57608275SEric Cheng 		 * nxge_grp_dc_add takes a channel number which is a
57618275SEric Cheng 		 * "devise" ring ID.
57628275SEric Cheng 		 */
57638275SEric Cheng 		channel = nxge->pt_config.hw_config.tdc.start + rhandle->index;
57648275SEric Cheng 
57658275SEric Cheng 		/*
57668275SEric Cheng 		 * Remove the ring from the default group
57678275SEric Cheng 		 */
57688275SEric Cheng 		if (rgroup->gindex != 0) {
57698275SEric Cheng 			(void) nxge_grp_dc_remove(nxge, VP_BOUND_TX, channel);
57708275SEric Cheng 		}
57718275SEric Cheng 
57728275SEric Cheng 		/*
57738275SEric Cheng 		 * nxge->tx_set.group[] is an array of groups indexed by
57748275SEric Cheng 		 * a "port" group ID.
57758275SEric Cheng 		 */
57768275SEric Cheng 		grp = nxge->tx_set.group[rgroup->gindex];
57778275SEric Cheng 		rv = nxge_grp_dc_add(nxge, grp, VP_BOUND_TX, channel);
57788275SEric Cheng 		if (rv != 0) {
57798275SEric Cheng 			NXGE_ERROR_MSG((nxge, NXGE_ERR_CTL,
57808275SEric Cheng 			    "nxge_group_add_ring: nxge_grp_dc_add failed"));
57818275SEric Cheng 		}
57828275SEric Cheng 		break;
57838275SEric Cheng 
57848275SEric Cheng 	case MAC_RING_TYPE_RX:
57858275SEric Cheng 		/*
57868275SEric Cheng 		 * nxge->rx_set.group[] is an array of groups indexed by
57878275SEric Cheng 		 * a "port" group ID.
57888275SEric Cheng 		 */
57898275SEric Cheng 		grp = nxge->rx_set.group[rgroup->gindex];
57908275SEric Cheng 
57918275SEric Cheng 		dev_gindex = nxge->pt_config.hw_config.def_mac_rxdma_grpid +
57928275SEric Cheng 		    rgroup->gindex;
57938275SEric Cheng 		rdc_grp = &nxge->pt_config.rdc_grps[dev_gindex];
57948275SEric Cheng 
57958275SEric Cheng 		/*
57968275SEric Cheng 		 * nxge_grp_dc_add takes a channel number which is a
57978275SEric Cheng 		 * "devise" ring ID.
57988275SEric Cheng 		 */
57998275SEric Cheng 		channel = nxge->pt_config.hw_config.start_rdc + rhandle->index;
58008275SEric Cheng 		rv = nxge_grp_dc_add(nxge, grp, VP_BOUND_RX, channel);
58018275SEric Cheng 		if (rv != 0) {
58028275SEric Cheng 			NXGE_ERROR_MSG((nxge, NXGE_ERR_CTL,
58038275SEric Cheng 			    "nxge_group_add_ring: nxge_grp_dc_add failed"));
58048275SEric Cheng 		}
58058275SEric Cheng 
58068275SEric Cheng 		rdc_grp->map |= (1 << channel);
58078275SEric Cheng 		rdc_grp->max_rdcs++;
58088275SEric Cheng 
58099047SMichael.Speer@Sun.COM 		(void) nxge_init_fzc_rdc_tbl(nxge, rdc_grp, rgroup->rdctbl);
58108275SEric Cheng 		break;
58118275SEric Cheng 	}
58128275SEric Cheng }
58138275SEric Cheng 
58148275SEric Cheng static void
58158275SEric Cheng nxge_group_rem_ring(mac_group_driver_t gh, mac_ring_driver_t rh,
58168275SEric Cheng     mac_ring_type_t type)
58178275SEric Cheng {
58188275SEric Cheng 	nxge_ring_group_t	*rgroup = (nxge_ring_group_t *)gh;
58198275SEric Cheng 	nxge_ring_handle_t	*rhandle = (nxge_ring_handle_t *)rh;
58208275SEric Cheng 	nxge_t			*nxge;
58218275SEric Cheng 	uint16_t		channel;	/* device-wise ring id */
58228275SEric Cheng 	nxge_rdc_grp_t		*rdc_grp;
58238275SEric Cheng 	int			dev_gindex;
58248275SEric Cheng 
58258275SEric Cheng 	nxge = rgroup->nxgep;
58268275SEric Cheng 
58278275SEric Cheng 	switch (type) {
58288275SEric Cheng 	case MAC_RING_TYPE_TX:
58298275SEric Cheng 		dev_gindex = nxge->pt_config.hw_config.def_mac_txdma_grpid +
58308275SEric Cheng 		    rgroup->gindex;
58318275SEric Cheng 		channel = nxge->pt_config.hw_config.tdc.start + rhandle->index;
58328275SEric Cheng 		nxge_grp_dc_remove(nxge, VP_BOUND_TX, channel);
58338275SEric Cheng 
58348275SEric Cheng 		/*
58358275SEric Cheng 		 * Add the ring back to the default group
58368275SEric Cheng 		 */
58378275SEric Cheng 		if (rgroup->gindex != 0) {
58388275SEric Cheng 			nxge_grp_t *grp;
58398275SEric Cheng 			grp = nxge->tx_set.group[0];
58408275SEric Cheng 			(void) nxge_grp_dc_add(nxge, grp, VP_BOUND_TX, channel);
58418275SEric Cheng 		}
58428275SEric Cheng 		break;
58438275SEric Cheng 
58448275SEric Cheng 	case MAC_RING_TYPE_RX:
58458275SEric Cheng 		dev_gindex = nxge->pt_config.hw_config.def_mac_rxdma_grpid +
58468275SEric Cheng 		    rgroup->gindex;
58478275SEric Cheng 		rdc_grp = &nxge->pt_config.rdc_grps[dev_gindex];
58488275SEric Cheng 		channel = rdc_grp->start_rdc + rhandle->index;
58498275SEric Cheng 		nxge_grp_dc_remove(nxge, VP_BOUND_RX, channel);
58508275SEric Cheng 
58518275SEric Cheng 		rdc_grp->map &= ~(1 << channel);
58528275SEric Cheng 		rdc_grp->max_rdcs--;
58538275SEric Cheng 
58549047SMichael.Speer@Sun.COM 		(void) nxge_init_fzc_rdc_tbl(nxge, rdc_grp, rgroup->rdctbl);
58558275SEric Cheng 		break;
58568275SEric Cheng 	}
58578275SEric Cheng }
58588275SEric Cheng 
58598275SEric Cheng 
58608275SEric Cheng /*ARGSUSED*/
58613859Sml29623 static nxge_status_t
58623859Sml29623 nxge_add_intrs(p_nxge_t nxgep)
58633859Sml29623 {
58643859Sml29623 
58653859Sml29623 	int		intr_types;
58663859Sml29623 	int		type = 0;
58673859Sml29623 	int		ddi_status = DDI_SUCCESS;
58683859Sml29623 	nxge_status_t	status = NXGE_OK;
58693859Sml29623 
58703859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs"));
58713859Sml29623 
58723859Sml29623 	nxgep->nxge_intr_type.intr_registered = B_FALSE;
58733859Sml29623 	nxgep->nxge_intr_type.intr_enabled = B_FALSE;
58743859Sml29623 	nxgep->nxge_intr_type.msi_intx_cnt = 0;
58753859Sml29623 	nxgep->nxge_intr_type.intr_added = 0;
58763859Sml29623 	nxgep->nxge_intr_type.niu_msi_enable = B_FALSE;
58773859Sml29623 	nxgep->nxge_intr_type.intr_type = 0;
58783859Sml29623 
58793859Sml29623 	if (nxgep->niu_type == N2_NIU) {
58803859Sml29623 		nxgep->nxge_intr_type.niu_msi_enable = B_TRUE;
58813859Sml29623 	} else if (nxge_msi_enable) {
58823859Sml29623 		nxgep->nxge_intr_type.niu_msi_enable = B_TRUE;
58833859Sml29623 	}
58843859Sml29623 
58853859Sml29623 	/* Get the supported interrupt types */
58863859Sml29623 	if ((ddi_status = ddi_intr_get_supported_types(nxgep->dip, &intr_types))
58876512Ssowmini 	    != DDI_SUCCESS) {
58883859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "<== nxge_add_intrs: "
58896512Ssowmini 		    "ddi_intr_get_supported_types failed: status 0x%08x",
58906512Ssowmini 		    ddi_status));
58913859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
58923859Sml29623 	}
58933859Sml29623 	nxgep->nxge_intr_type.intr_types = intr_types;
58943859Sml29623 
58953859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
58966512Ssowmini 	    "ddi_intr_get_supported_types: 0x%08x", intr_types));
58973859Sml29623 
58983859Sml29623 	/*
58993859Sml29623 	 * Solaris MSIX is not supported yet. use MSI for now.
59003859Sml29623 	 * nxge_msi_enable (1):
59013859Sml29623 	 *	1 - MSI		2 - MSI-X	others - FIXED
59023859Sml29623 	 */
59033859Sml29623 	switch (nxge_msi_enable) {
59043859Sml29623 	default:
59053859Sml29623 		type = DDI_INTR_TYPE_FIXED;
59063859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs: "
59076512Ssowmini 		    "use fixed (intx emulation) type %08x",
59086512Ssowmini 		    type));
59093859Sml29623 		break;
59103859Sml29623 
59113859Sml29623 	case 2:
59123859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs: "
59136512Ssowmini 		    "ddi_intr_get_supported_types: 0x%08x", intr_types));
59143859Sml29623 		if (intr_types & DDI_INTR_TYPE_MSIX) {
59153859Sml29623 			type = DDI_INTR_TYPE_MSIX;
59163859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
59176512Ssowmini 			    "ddi_intr_get_supported_types: MSIX 0x%08x",
59186512Ssowmini 			    type));
59193859Sml29623 		} else if (intr_types & DDI_INTR_TYPE_MSI) {
59203859Sml29623 			type = DDI_INTR_TYPE_MSI;
59213859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
59226512Ssowmini 			    "ddi_intr_get_supported_types: MSI 0x%08x",
59236512Ssowmini 			    type));
59243859Sml29623 		} else if (intr_types & DDI_INTR_TYPE_FIXED) {
59253859Sml29623 			type = DDI_INTR_TYPE_FIXED;
59263859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs: "
59276512Ssowmini 			    "ddi_intr_get_supported_types: MSXED0x%08x",
59286512Ssowmini 			    type));
59293859Sml29623 		}
59303859Sml29623 		break;
59313859Sml29623 
59323859Sml29623 	case 1:
59333859Sml29623 		if (intr_types & DDI_INTR_TYPE_MSI) {
59343859Sml29623 			type = DDI_INTR_TYPE_MSI;
59353859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs: "
59366512Ssowmini 			    "ddi_intr_get_supported_types: MSI 0x%08x",
59376512Ssowmini 			    type));
59383859Sml29623 		} else if (intr_types & DDI_INTR_TYPE_MSIX) {
59393859Sml29623 			type = DDI_INTR_TYPE_MSIX;
59403859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
59416512Ssowmini 			    "ddi_intr_get_supported_types: MSIX 0x%08x",
59426512Ssowmini 			    type));
59433859Sml29623 		} else if (intr_types & DDI_INTR_TYPE_FIXED) {
59443859Sml29623 			type = DDI_INTR_TYPE_FIXED;
59453859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
59466512Ssowmini 			    "ddi_intr_get_supported_types: MSXED0x%08x",
59476512Ssowmini 			    type));
59483859Sml29623 		}
59493859Sml29623 	}
59503859Sml29623 
59513859Sml29623 	nxgep->nxge_intr_type.intr_type = type;
59523859Sml29623 	if ((type == DDI_INTR_TYPE_MSIX || type == DDI_INTR_TYPE_MSI ||
59536512Ssowmini 	    type == DDI_INTR_TYPE_FIXED) &&
59546512Ssowmini 	    nxgep->nxge_intr_type.niu_msi_enable) {
59553859Sml29623 		if ((status = nxge_add_intrs_adv(nxgep)) != DDI_SUCCESS) {
59563859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
59576512Ssowmini 			    " nxge_add_intrs: "
59586512Ssowmini 			    " nxge_add_intrs_adv failed: status 0x%08x",
59596512Ssowmini 			    status));
59603859Sml29623 			return (status);
59613859Sml29623 		} else {
59623859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
59636512Ssowmini 			    "interrupts registered : type %d", type));
59643859Sml29623 			nxgep->nxge_intr_type.intr_registered = B_TRUE;
59653859Sml29623 
59663859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
59676512Ssowmini 			    "\nAdded advanced nxge add_intr_adv "
59686512Ssowmini 			    "intr type 0x%x\n", type));
59693859Sml29623 
59703859Sml29623 			return (status);
59713859Sml29623 		}
59723859Sml29623 	}
59733859Sml29623 
59743859Sml29623 	if (!nxgep->nxge_intr_type.intr_registered) {
59753859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "==> nxge_add_intrs: "
59766512Ssowmini 		    "failed to register interrupts"));
59773859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
59783859Sml29623 	}
59793859Sml29623 
59803859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_add_intrs"));
59813859Sml29623 	return (status);
59823859Sml29623 }
59833859Sml29623 
59843859Sml29623 static nxge_status_t
59853859Sml29623 nxge_add_intrs_adv(p_nxge_t nxgep)
59863859Sml29623 {
59873859Sml29623 	int		intr_type;
59883859Sml29623 	p_nxge_intr_t	intrp;
59893859Sml29623 
59903859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs_adv"));
59913859Sml29623 
59923859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
59933859Sml29623 	intr_type = intrp->intr_type;
59943859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs_adv: type 0x%x",
59956512Ssowmini 	    intr_type));
59963859Sml29623 
59973859Sml29623 	switch (intr_type) {
59983859Sml29623 	case DDI_INTR_TYPE_MSI: /* 0x2 */
59993859Sml29623 	case DDI_INTR_TYPE_MSIX: /* 0x4 */
60003859Sml29623 		return (nxge_add_intrs_adv_type(nxgep, intr_type));
60013859Sml29623 
60023859Sml29623 	case DDI_INTR_TYPE_FIXED: /* 0x1 */
60033859Sml29623 		return (nxge_add_intrs_adv_type_fix(nxgep, intr_type));
60043859Sml29623 
60053859Sml29623 	default:
60063859Sml29623 		return (NXGE_ERROR);
60073859Sml29623 	}
60083859Sml29623 }
60093859Sml29623 
60103859Sml29623 
60113859Sml29623 /*ARGSUSED*/
60123859Sml29623 static nxge_status_t
60133859Sml29623 nxge_add_intrs_adv_type(p_nxge_t nxgep, uint32_t int_type)
60143859Sml29623 {
60153859Sml29623 	dev_info_t		*dip = nxgep->dip;
60163859Sml29623 	p_nxge_ldg_t		ldgp;
60173859Sml29623 	p_nxge_intr_t		intrp;
60183859Sml29623 	uint_t			*inthandler;
60193859Sml29623 	void			*arg1, *arg2;
60203859Sml29623 	int			behavior;
60215013Sml29623 	int			nintrs, navail, nrequest;
60223859Sml29623 	int			nactual, nrequired;
60233859Sml29623 	int			inum = 0;
60243859Sml29623 	int			x, y;
60253859Sml29623 	int			ddi_status = DDI_SUCCESS;
60263859Sml29623 	nxge_status_t		status = NXGE_OK;
60273859Sml29623 
60283859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs_adv_type"));
60293859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
60303859Sml29623 	intrp->start_inum = 0;
60313859Sml29623 
60323859Sml29623 	ddi_status = ddi_intr_get_nintrs(dip, int_type, &nintrs);
60333859Sml29623 	if ((ddi_status != DDI_SUCCESS) || (nintrs == 0)) {
60343859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
60356512Ssowmini 		    "ddi_intr_get_nintrs() failed, status: 0x%x%, "
60366512Ssowmini 		    "nintrs: %d", ddi_status, nintrs));
60373859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
60383859Sml29623 	}
60393859Sml29623 
60403859Sml29623 	ddi_status = ddi_intr_get_navail(dip, int_type, &navail);
60413859Sml29623 	if ((ddi_status != DDI_SUCCESS) || (navail == 0)) {
60423859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
60436512Ssowmini 		    "ddi_intr_get_navail() failed, status: 0x%x%, "
60446512Ssowmini 		    "nintrs: %d", ddi_status, navail));
60453859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
60463859Sml29623 	}
60473859Sml29623 
60483859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL,
60496512Ssowmini 	    "ddi_intr_get_navail() returned: nintrs %d, navail %d",
60506512Ssowmini 	    nintrs, navail));
60513859Sml29623 
60525013Sml29623 	/* PSARC/2007/453 MSI-X interrupt limit override */
60535013Sml29623 	if (int_type == DDI_INTR_TYPE_MSIX) {
60545013Sml29623 		nrequest = nxge_create_msi_property(nxgep);
60555013Sml29623 		if (nrequest < navail) {
60565013Sml29623 			navail = nrequest;
60575013Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
60585013Sml29623 			    "nxge_add_intrs_adv_type: nintrs %d "
60595013Sml29623 			    "navail %d (nrequest %d)",
60605013Sml29623 			    nintrs, navail, nrequest));
60615013Sml29623 		}
60625013Sml29623 	}
60635013Sml29623 
60643859Sml29623 	if (int_type == DDI_INTR_TYPE_MSI && !ISP2(navail)) {
60653859Sml29623 		/* MSI must be power of 2 */
60663859Sml29623 		if ((navail & 16) == 16) {
60673859Sml29623 			navail = 16;
60683859Sml29623 		} else if ((navail & 8) == 8) {
60693859Sml29623 			navail = 8;
60703859Sml29623 		} else if ((navail & 4) == 4) {
60713859Sml29623 			navail = 4;
60723859Sml29623 		} else if ((navail & 2) == 2) {
60733859Sml29623 			navail = 2;
60743859Sml29623 		} else {
60753859Sml29623 			navail = 1;
60763859Sml29623 		}
60773859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
60786512Ssowmini 		    "ddi_intr_get_navail(): (msi power of 2) nintrs %d, "
60796512Ssowmini 		    "navail %d", nintrs, navail));
60803859Sml29623 	}
60813859Sml29623 
60823859Sml29623 	behavior = ((int_type == DDI_INTR_TYPE_FIXED) ? DDI_INTR_ALLOC_STRICT :
60836512Ssowmini 	    DDI_INTR_ALLOC_NORMAL);
60843859Sml29623 	intrp->intr_size = navail * sizeof (ddi_intr_handle_t);
60853859Sml29623 	intrp->htable = kmem_alloc(intrp->intr_size, KM_SLEEP);
60863859Sml29623 	ddi_status = ddi_intr_alloc(dip, intrp->htable, int_type, inum,
60876512Ssowmini 	    navail, &nactual, behavior);
60883859Sml29623 	if (ddi_status != DDI_SUCCESS || nactual == 0) {
60893859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
60906512Ssowmini 		    " ddi_intr_alloc() failed: %d",
60916512Ssowmini 		    ddi_status));
60923859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
60933859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
60943859Sml29623 	}
60953859Sml29623 
60963859Sml29623 	if ((ddi_status = ddi_intr_get_pri(intrp->htable[0],
60976512Ssowmini 	    (uint_t *)&intrp->pri)) != DDI_SUCCESS) {
60983859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
60996512Ssowmini 		    " ddi_intr_get_pri() failed: %d",
61006512Ssowmini 		    ddi_status));
61013859Sml29623 		/* Free already allocated interrupts */
61023859Sml29623 		for (y = 0; y < nactual; y++) {
61033859Sml29623 			(void) ddi_intr_free(intrp->htable[y]);
61043859Sml29623 		}
61053859Sml29623 
61063859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
61073859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
61083859Sml29623 	}
61093859Sml29623 
61103859Sml29623 	nrequired = 0;
61113859Sml29623 	switch (nxgep->niu_type) {
61123859Sml29623 	default:
61133859Sml29623 		status = nxge_ldgv_init(nxgep, &nactual, &nrequired);
61143859Sml29623 		break;
61153859Sml29623 
61163859Sml29623 	case N2_NIU:
61173859Sml29623 		status = nxge_ldgv_init_n2(nxgep, &nactual, &nrequired);
61183859Sml29623 		break;
61193859Sml29623 	}
61203859Sml29623 
61213859Sml29623 	if (status != NXGE_OK) {
61223859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
61236512Ssowmini 		    "nxge_add_intrs_adv_typ:nxge_ldgv_init "
61246512Ssowmini 		    "failed: 0x%x", status));
61253859Sml29623 		/* Free already allocated interrupts */
61263859Sml29623 		for (y = 0; y < nactual; y++) {
61273859Sml29623 			(void) ddi_intr_free(intrp->htable[y]);
61283859Sml29623 		}
61293859Sml29623 
61303859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
61313859Sml29623 		return (status);
61323859Sml29623 	}
61333859Sml29623 
61343859Sml29623 	ldgp = nxgep->ldgvp->ldgp;
61353859Sml29623 	for (x = 0; x < nrequired; x++, ldgp++) {
61363859Sml29623 		ldgp->vector = (uint8_t)x;
61373859Sml29623 		ldgp->intdata = SID_DATA(ldgp->func, x);
61383859Sml29623 		arg1 = ldgp->ldvp;
61393859Sml29623 		arg2 = nxgep;
61403859Sml29623 		if (ldgp->nldvs == 1) {
61413859Sml29623 			inthandler = (uint_t *)ldgp->ldvp->ldv_intr_handler;
61423859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
61436512Ssowmini 			    "nxge_add_intrs_adv_type: "
61446512Ssowmini 			    "arg1 0x%x arg2 0x%x: "
61456512Ssowmini 			    "1-1 int handler (entry %d intdata 0x%x)\n",
61466512Ssowmini 			    arg1, arg2,
61476512Ssowmini 			    x, ldgp->intdata));
61483859Sml29623 		} else if (ldgp->nldvs > 1) {
61493859Sml29623 			inthandler = (uint_t *)ldgp->sys_intr_handler;
61503859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
61516512Ssowmini 			    "nxge_add_intrs_adv_type: "
61526512Ssowmini 			    "arg1 0x%x arg2 0x%x: "
61536512Ssowmini 			    "nldevs %d int handler "
61546512Ssowmini 			    "(entry %d intdata 0x%x)\n",
61556512Ssowmini 			    arg1, arg2,
61566512Ssowmini 			    ldgp->nldvs, x, ldgp->intdata));
61573859Sml29623 		}
61583859Sml29623 
61593859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
61606512Ssowmini 		    "==> nxge_add_intrs_adv_type: ddi_add_intr(inum) #%d "
61616512Ssowmini 		    "htable 0x%llx", x, intrp->htable[x]));
61623859Sml29623 
61633859Sml29623 		if ((ddi_status = ddi_intr_add_handler(intrp->htable[x],
61646512Ssowmini 		    (ddi_intr_handler_t *)inthandler, arg1, arg2))
61656512Ssowmini 		    != DDI_SUCCESS) {
61663859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
61676512Ssowmini 			    "==> nxge_add_intrs_adv_type: failed #%d "
61686512Ssowmini 			    "status 0x%x", x, ddi_status));
61693859Sml29623 			for (y = 0; y < intrp->intr_added; y++) {
61703859Sml29623 				(void) ddi_intr_remove_handler(
61716512Ssowmini 				    intrp->htable[y]);
61723859Sml29623 			}
61733859Sml29623 			/* Free already allocated intr */
61743859Sml29623 			for (y = 0; y < nactual; y++) {
61753859Sml29623 				(void) ddi_intr_free(intrp->htable[y]);
61763859Sml29623 			}
61773859Sml29623 			kmem_free(intrp->htable, intrp->intr_size);
61783859Sml29623 
61793859Sml29623 			(void) nxge_ldgv_uninit(nxgep);
61803859Sml29623 
61813859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
61823859Sml29623 		}
61833859Sml29623 		intrp->intr_added++;
61843859Sml29623 	}
61853859Sml29623 
61863859Sml29623 	intrp->msi_intx_cnt = nactual;
61873859Sml29623 
61883859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
61896512Ssowmini 	    "Requested: %d, Allowed: %d msi_intx_cnt %d intr_added %d",
61906512Ssowmini 	    navail, nactual,
61916512Ssowmini 	    intrp->msi_intx_cnt,
61926512Ssowmini 	    intrp->intr_added));
61933859Sml29623 
61943859Sml29623 	(void) ddi_intr_get_cap(intrp->htable[0], &intrp->intr_cap);
61953859Sml29623 
61963859Sml29623 	(void) nxge_intr_ldgv_init(nxgep);
61973859Sml29623 
61983859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_add_intrs_adv_type"));
61993859Sml29623 
62003859Sml29623 	return (status);
62013859Sml29623 }
62023859Sml29623 
62033859Sml29623 /*ARGSUSED*/
62043859Sml29623 static nxge_status_t
62053859Sml29623 nxge_add_intrs_adv_type_fix(p_nxge_t nxgep, uint32_t int_type)
62063859Sml29623 {
62073859Sml29623 	dev_info_t		*dip = nxgep->dip;
62083859Sml29623 	p_nxge_ldg_t		ldgp;
62093859Sml29623 	p_nxge_intr_t		intrp;
62103859Sml29623 	uint_t			*inthandler;
62113859Sml29623 	void			*arg1, *arg2;
62123859Sml29623 	int			behavior;
62133859Sml29623 	int			nintrs, navail;
62143859Sml29623 	int			nactual, nrequired;
62153859Sml29623 	int			inum = 0;
62163859Sml29623 	int			x, y;
62173859Sml29623 	int			ddi_status = DDI_SUCCESS;
62183859Sml29623 	nxge_status_t		status = NXGE_OK;
62193859Sml29623 
62203859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs_adv_type_fix"));
62213859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
62223859Sml29623 	intrp->start_inum = 0;
62233859Sml29623 
62243859Sml29623 	ddi_status = ddi_intr_get_nintrs(dip, int_type, &nintrs);
62253859Sml29623 	if ((ddi_status != DDI_SUCCESS) || (nintrs == 0)) {
62263859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
62276512Ssowmini 		    "ddi_intr_get_nintrs() failed, status: 0x%x%, "
62286512Ssowmini 		    "nintrs: %d", status, nintrs));
62293859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
62303859Sml29623 	}
62313859Sml29623 
62323859Sml29623 	ddi_status = ddi_intr_get_navail(dip, int_type, &navail);
62333859Sml29623 	if ((ddi_status != DDI_SUCCESS) || (navail == 0)) {
62343859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
62356512Ssowmini 		    "ddi_intr_get_navail() failed, status: 0x%x%, "
62366512Ssowmini 		    "nintrs: %d", ddi_status, navail));
62373859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
62383859Sml29623 	}
62393859Sml29623 
62403859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL,
62416512Ssowmini 	    "ddi_intr_get_navail() returned: nintrs %d, naavail %d",
62426512Ssowmini 	    nintrs, navail));
62433859Sml29623 
62443859Sml29623 	behavior = ((int_type == DDI_INTR_TYPE_FIXED) ? DDI_INTR_ALLOC_STRICT :
62456512Ssowmini 	    DDI_INTR_ALLOC_NORMAL);
62463859Sml29623 	intrp->intr_size = navail * sizeof (ddi_intr_handle_t);
62473859Sml29623 	intrp->htable = kmem_alloc(intrp->intr_size, KM_SLEEP);
62483859Sml29623 	ddi_status = ddi_intr_alloc(dip, intrp->htable, int_type, inum,
62496512Ssowmini 	    navail, &nactual, behavior);
62503859Sml29623 	if (ddi_status != DDI_SUCCESS || nactual == 0) {
62513859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
62526512Ssowmini 		    " ddi_intr_alloc() failed: %d",
62536512Ssowmini 		    ddi_status));
62543859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
62553859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
62563859Sml29623 	}
62573859Sml29623 
62583859Sml29623 	if ((ddi_status = ddi_intr_get_pri(intrp->htable[0],
62596512Ssowmini 	    (uint_t *)&intrp->pri)) != DDI_SUCCESS) {
62603859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
62616512Ssowmini 		    " ddi_intr_get_pri() failed: %d",
62626512Ssowmini 		    ddi_status));
62633859Sml29623 		/* Free already allocated interrupts */
62643859Sml29623 		for (y = 0; y < nactual; y++) {
62653859Sml29623 			(void) ddi_intr_free(intrp->htable[y]);
62663859Sml29623 		}
62673859Sml29623 
62683859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
62693859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
62703859Sml29623 	}
62713859Sml29623 
62723859Sml29623 	nrequired = 0;
62733859Sml29623 	switch (nxgep->niu_type) {
62743859Sml29623 	default:
62753859Sml29623 		status = nxge_ldgv_init(nxgep, &nactual, &nrequired);
62763859Sml29623 		break;
62773859Sml29623 
62783859Sml29623 	case N2_NIU:
62793859Sml29623 		status = nxge_ldgv_init_n2(nxgep, &nactual, &nrequired);
62803859Sml29623 		break;
62813859Sml29623 	}
62823859Sml29623 
62833859Sml29623 	if (status != NXGE_OK) {
62843859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
62856512Ssowmini 		    "nxge_add_intrs_adv_type_fix:nxge_ldgv_init "
62866512Ssowmini 		    "failed: 0x%x", status));
62873859Sml29623 		/* Free already allocated interrupts */
62883859Sml29623 		for (y = 0; y < nactual; y++) {
62893859Sml29623 			(void) ddi_intr_free(intrp->htable[y]);
62903859Sml29623 		}
62913859Sml29623 
62923859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
62933859Sml29623 		return (status);
62943859Sml29623 	}
62953859Sml29623 
62963859Sml29623 	ldgp = nxgep->ldgvp->ldgp;
62973859Sml29623 	for (x = 0; x < nrequired; x++, ldgp++) {
62983859Sml29623 		ldgp->vector = (uint8_t)x;
62993859Sml29623 		if (nxgep->niu_type != N2_NIU) {
63003859Sml29623 			ldgp->intdata = SID_DATA(ldgp->func, x);
63013859Sml29623 		}
63023859Sml29623 
63033859Sml29623 		arg1 = ldgp->ldvp;
63043859Sml29623 		arg2 = nxgep;
63053859Sml29623 		if (ldgp->nldvs == 1) {
63063859Sml29623 			inthandler = (uint_t *)ldgp->ldvp->ldv_intr_handler;
63073859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
63086512Ssowmini 			    "nxge_add_intrs_adv_type_fix: "
63096512Ssowmini 			    "1-1 int handler(%d) ldg %d ldv %d "
63106512Ssowmini 			    "arg1 $%p arg2 $%p\n",
63116512Ssowmini 			    x, ldgp->ldg, ldgp->ldvp->ldv,
63126512Ssowmini 			    arg1, arg2));
63133859Sml29623 		} else if (ldgp->nldvs > 1) {
63143859Sml29623 			inthandler = (uint_t *)ldgp->sys_intr_handler;
63153859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
63166512Ssowmini 			    "nxge_add_intrs_adv_type_fix: "
63176512Ssowmini 			    "shared ldv %d int handler(%d) ldv %d ldg %d"
63186512Ssowmini 			    "arg1 0x%016llx arg2 0x%016llx\n",
63196512Ssowmini 			    x, ldgp->nldvs, ldgp->ldg, ldgp->ldvp->ldv,
63206512Ssowmini 			    arg1, arg2));
63213859Sml29623 		}
63223859Sml29623 
63233859Sml29623 		if ((ddi_status = ddi_intr_add_handler(intrp->htable[x],
63246512Ssowmini 		    (ddi_intr_handler_t *)inthandler, arg1, arg2))
63256512Ssowmini 		    != DDI_SUCCESS) {
63263859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
63276512Ssowmini 			    "==> nxge_add_intrs_adv_type_fix: failed #%d "
63286512Ssowmini 			    "status 0x%x", x, ddi_status));
63293859Sml29623 			for (y = 0; y < intrp->intr_added; y++) {
63303859Sml29623 				(void) ddi_intr_remove_handler(
63316512Ssowmini 				    intrp->htable[y]);
63323859Sml29623 			}
63333859Sml29623 			for (y = 0; y < nactual; y++) {
63343859Sml29623 				(void) ddi_intr_free(intrp->htable[y]);
63353859Sml29623 			}
63363859Sml29623 			/* Free already allocated intr */
63373859Sml29623 			kmem_free(intrp->htable, intrp->intr_size);
63383859Sml29623 
63393859Sml29623 			(void) nxge_ldgv_uninit(nxgep);
63403859Sml29623 
63413859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
63423859Sml29623 		}
63433859Sml29623 		intrp->intr_added++;
63443859Sml29623 	}
63453859Sml29623 
63463859Sml29623 	intrp->msi_intx_cnt = nactual;
63473859Sml29623 
63483859Sml29623 	(void) ddi_intr_get_cap(intrp->htable[0], &intrp->intr_cap);
63493859Sml29623 
63503859Sml29623 	status = nxge_intr_ldgv_init(nxgep);
63513859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_add_intrs_adv_type_fix"));
63523859Sml29623 
63533859Sml29623 	return (status);
63543859Sml29623 }
63553859Sml29623 
63563859Sml29623 static void
63573859Sml29623 nxge_remove_intrs(p_nxge_t nxgep)
63583859Sml29623 {
63593859Sml29623 	int		i, inum;
63603859Sml29623 	p_nxge_intr_t	intrp;
63613859Sml29623 
63623859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_remove_intrs"));
63633859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
63643859Sml29623 	if (!intrp->intr_registered) {
63653859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
63666512Ssowmini 		    "<== nxge_remove_intrs: interrupts not registered"));
63673859Sml29623 		return;
63683859Sml29623 	}
63693859Sml29623 
63703859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_remove_intrs:advanced"));
63713859Sml29623 
63723859Sml29623 	if (intrp->intr_cap & DDI_INTR_FLAG_BLOCK) {
63733859Sml29623 		(void) ddi_intr_block_disable(intrp->htable,
63746512Ssowmini 		    intrp->intr_added);
63753859Sml29623 	} else {
63763859Sml29623 		for (i = 0; i < intrp->intr_added; i++) {
63773859Sml29623 			(void) ddi_intr_disable(intrp->htable[i]);
63783859Sml29623 		}
63793859Sml29623 	}
63803859Sml29623 
63813859Sml29623 	for (inum = 0; inum < intrp->intr_added; inum++) {
63823859Sml29623 		if (intrp->htable[inum]) {
63833859Sml29623 			(void) ddi_intr_remove_handler(intrp->htable[inum]);
63843859Sml29623 		}
63853859Sml29623 	}
63863859Sml29623 
63873859Sml29623 	for (inum = 0; inum < intrp->msi_intx_cnt; inum++) {
63883859Sml29623 		if (intrp->htable[inum]) {
63893859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
63906512Ssowmini 			    "nxge_remove_intrs: ddi_intr_free inum %d "
63916512Ssowmini 			    "msi_intx_cnt %d intr_added %d",
63926512Ssowmini 			    inum,
63936512Ssowmini 			    intrp->msi_intx_cnt,
63946512Ssowmini 			    intrp->intr_added));
63953859Sml29623 
63963859Sml29623 			(void) ddi_intr_free(intrp->htable[inum]);
63973859Sml29623 		}
63983859Sml29623 	}
63993859Sml29623 
64003859Sml29623 	kmem_free(intrp->htable, intrp->intr_size);
64013859Sml29623 	intrp->intr_registered = B_FALSE;
64023859Sml29623 	intrp->intr_enabled = B_FALSE;
64033859Sml29623 	intrp->msi_intx_cnt = 0;
64043859Sml29623 	intrp->intr_added = 0;
64053859Sml29623 
64063859Sml29623 	(void) nxge_ldgv_uninit(nxgep);
64073859Sml29623 
64085013Sml29623 	(void) ddi_prop_remove(DDI_DEV_T_NONE, nxgep->dip,
64095013Sml29623 	    "#msix-request");
64105013Sml29623 
64113859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_remove_intrs"));
64123859Sml29623 }
64133859Sml29623 
64143859Sml29623 /*ARGSUSED*/
64153859Sml29623 static void
64163859Sml29623 nxge_intrs_enable(p_nxge_t nxgep)
64173859Sml29623 {
64183859Sml29623 	p_nxge_intr_t	intrp;
64193859Sml29623 	int		i;
64203859Sml29623 	int		status;
64213859Sml29623 
64223859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_intrs_enable"));
64233859Sml29623 
64243859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
64253859Sml29623 
64263859Sml29623 	if (!intrp->intr_registered) {
64273859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "<== nxge_intrs_enable: "
64286512Ssowmini 		    "interrupts are not registered"));
64293859Sml29623 		return;
64303859Sml29623 	}
64313859Sml29623 
64323859Sml29623 	if (intrp->intr_enabled) {
64333859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
64346512Ssowmini 		    "<== nxge_intrs_enable: already enabled"));
64353859Sml29623 		return;
64363859Sml29623 	}
64373859Sml29623 
64383859Sml29623 	if (intrp->intr_cap & DDI_INTR_FLAG_BLOCK) {
64393859Sml29623 		status = ddi_intr_block_enable(intrp->htable,
64406512Ssowmini 		    intrp->intr_added);
64413859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_intrs_enable "
64426512Ssowmini 		    "block enable - status 0x%x total inums #%d\n",
64436512Ssowmini 		    status, intrp->intr_added));
64443859Sml29623 	} else {
64453859Sml29623 		for (i = 0; i < intrp->intr_added; i++) {
64463859Sml29623 			status = ddi_intr_enable(intrp->htable[i]);
64473859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_intrs_enable "
64486512Ssowmini 			    "ddi_intr_enable:enable - status 0x%x "
64496512Ssowmini 			    "total inums %d enable inum #%d\n",
64506512Ssowmini 			    status, intrp->intr_added, i));
64513859Sml29623 			if (status == DDI_SUCCESS) {
64523859Sml29623 				intrp->intr_enabled = B_TRUE;
64533859Sml29623 			}
64543859Sml29623 		}
64553859Sml29623 	}
64563859Sml29623 
64573859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_intrs_enable"));
64583859Sml29623 }
64593859Sml29623 
64603859Sml29623 /*ARGSUSED*/
64613859Sml29623 static void
64623859Sml29623 nxge_intrs_disable(p_nxge_t nxgep)
64633859Sml29623 {
64643859Sml29623 	p_nxge_intr_t	intrp;
64653859Sml29623 	int		i;
64663859Sml29623 
64673859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_intrs_disable"));
64683859Sml29623 
64693859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
64703859Sml29623 
64713859Sml29623 	if (!intrp->intr_registered) {
64723859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_intrs_disable: "
64736512Ssowmini 		    "interrupts are not registered"));
64743859Sml29623 		return;
64753859Sml29623 	}
64763859Sml29623 
64773859Sml29623 	if (intrp->intr_cap & DDI_INTR_FLAG_BLOCK) {
64783859Sml29623 		(void) ddi_intr_block_disable(intrp->htable,
64796512Ssowmini 		    intrp->intr_added);
64803859Sml29623 	} else {
64813859Sml29623 		for (i = 0; i < intrp->intr_added; i++) {
64823859Sml29623 			(void) ddi_intr_disable(intrp->htable[i]);
64833859Sml29623 		}
64843859Sml29623 	}
64853859Sml29623 
64863859Sml29623 	intrp->intr_enabled = B_FALSE;
64873859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_intrs_disable"));
64883859Sml29623 }
64893859Sml29623 
649010309SSriharsha.Basavapatna@Sun.COM nxge_status_t
64913859Sml29623 nxge_mac_register(p_nxge_t nxgep)
64923859Sml29623 {
64933859Sml29623 	mac_register_t *macp;
64943859Sml29623 	int		status;
64953859Sml29623 
64963859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_mac_register"));
64973859Sml29623 
64983859Sml29623 	if ((macp = mac_alloc(MAC_VERSION)) == NULL)
64993859Sml29623 		return (NXGE_ERROR);
65003859Sml29623 
65013859Sml29623 	macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
65023859Sml29623 	macp->m_driver = nxgep;
65033859Sml29623 	macp->m_dip = nxgep->dip;
650410309SSriharsha.Basavapatna@Sun.COM 	if (!isLDOMguest(nxgep)) {
650510309SSriharsha.Basavapatna@Sun.COM 		macp->m_src_addr = nxgep->ouraddr.ether_addr_octet;
650610309SSriharsha.Basavapatna@Sun.COM 	} else {
650710309SSriharsha.Basavapatna@Sun.COM 		macp->m_src_addr = KMEM_ZALLOC(MAXMACADDRLEN, KM_SLEEP);
650810309SSriharsha.Basavapatna@Sun.COM 		macp->m_dst_addr = KMEM_ZALLOC(MAXMACADDRLEN, KM_SLEEP);
650910309SSriharsha.Basavapatna@Sun.COM 		(void) memset(macp->m_src_addr, 0xff, sizeof (MAXMACADDRLEN));
651010309SSriharsha.Basavapatna@Sun.COM 	}
65113859Sml29623 	macp->m_callbacks = &nxge_m_callbacks;
65123859Sml29623 	macp->m_min_sdu = 0;
65136439Sml29623 	nxgep->mac.default_mtu = nxgep->mac.maxframesize -
65146439Sml29623 	    NXGE_EHEADER_VLAN_CRC;
65156439Sml29623 	macp->m_max_sdu = nxgep->mac.default_mtu;
65165895Syz147064 	macp->m_margin = VLAN_TAGSZ;
65176512Ssowmini 	macp->m_priv_props = nxge_priv_props;
65186512Ssowmini 	macp->m_priv_prop_count = NXGE_MAX_PRIV_PROPS;
651910309SSriharsha.Basavapatna@Sun.COM 	if (isLDOMguest(nxgep)) {
652010309SSriharsha.Basavapatna@Sun.COM 		macp->m_v12n = MAC_VIRT_LEVEL1 | MAC_VIRT_SERIALIZE;
652110309SSriharsha.Basavapatna@Sun.COM 	} else {
652210309SSriharsha.Basavapatna@Sun.COM 		macp->m_v12n = MAC_VIRT_HIO | MAC_VIRT_LEVEL1 | \
652310309SSriharsha.Basavapatna@Sun.COM 		    MAC_VIRT_SERIALIZE;
652410309SSriharsha.Basavapatna@Sun.COM 	}
65253859Sml29623 
65266439Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL,
65276439Sml29623 	    "==> nxge_mac_register: instance %d "
65286439Sml29623 	    "max_sdu %d margin %d maxframe %d (header %d)",
65296439Sml29623 	    nxgep->instance,
65306439Sml29623 	    macp->m_max_sdu, macp->m_margin,
65316439Sml29623 	    nxgep->mac.maxframesize,
65326439Sml29623 	    NXGE_EHEADER_VLAN_CRC));
65336439Sml29623 
65343859Sml29623 	status = mac_register(macp, &nxgep->mach);
653510309SSriharsha.Basavapatna@Sun.COM 	if (isLDOMguest(nxgep)) {
653610309SSriharsha.Basavapatna@Sun.COM 		KMEM_FREE(macp->m_src_addr, MAXMACADDRLEN);
653710309SSriharsha.Basavapatna@Sun.COM 		KMEM_FREE(macp->m_dst_addr, MAXMACADDRLEN);
653810309SSriharsha.Basavapatna@Sun.COM 	}
65393859Sml29623 	mac_free(macp);
65403859Sml29623 
65413859Sml29623 	if (status != 0) {
65423859Sml29623 		cmn_err(CE_WARN,
65436512Ssowmini 		    "!nxge_mac_register failed (status %d instance %d)",
65446512Ssowmini 		    status, nxgep->instance);
65453859Sml29623 		return (NXGE_ERROR);
65463859Sml29623 	}
65473859Sml29623 
65483859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_mac_register success "
65496512Ssowmini 	    "(instance %d)", nxgep->instance));
65503859Sml29623 
65513859Sml29623 	return (NXGE_OK);
65523859Sml29623 }
65533859Sml29623 
65543859Sml29623 void
65553859Sml29623 nxge_err_inject(p_nxge_t nxgep, queue_t *wq, mblk_t *mp)
65563859Sml29623 {
65573859Sml29623 	ssize_t		size;
65583859Sml29623 	mblk_t		*nmp;
65593859Sml29623 	uint8_t		blk_id;
65603859Sml29623 	uint8_t		chan;
65613859Sml29623 	uint32_t	err_id;
65623859Sml29623 	err_inject_t	*eip;
65633859Sml29623 
65643859Sml29623 	NXGE_DEBUG_MSG((nxgep, STR_CTL, "==> nxge_err_inject"));
65653859Sml29623 
65663859Sml29623 	size = 1024;
65673859Sml29623 	nmp = mp->b_cont;
65683859Sml29623 	eip = (err_inject_t *)nmp->b_rptr;
65693859Sml29623 	blk_id = eip->blk_id;
65703859Sml29623 	err_id = eip->err_id;
65713859Sml29623 	chan = eip->chan;
65723859Sml29623 	cmn_err(CE_NOTE, "!blk_id = 0x%x\n", blk_id);
65733859Sml29623 	cmn_err(CE_NOTE, "!err_id = 0x%x\n", err_id);
65743859Sml29623 	cmn_err(CE_NOTE, "!chan = 0x%x\n", chan);
65753859Sml29623 	switch (blk_id) {
65763859Sml29623 	case MAC_BLK_ID:
65773859Sml29623 		break;
65783859Sml29623 	case TXMAC_BLK_ID:
65793859Sml29623 		break;
65803859Sml29623 	case RXMAC_BLK_ID:
65813859Sml29623 		break;
65823859Sml29623 	case MIF_BLK_ID:
65833859Sml29623 		break;
65843859Sml29623 	case IPP_BLK_ID:
65853859Sml29623 		nxge_ipp_inject_err(nxgep, err_id);
65863859Sml29623 		break;
65873859Sml29623 	case TXC_BLK_ID:
65883859Sml29623 		nxge_txc_inject_err(nxgep, err_id);
65893859Sml29623 		break;
65903859Sml29623 	case TXDMA_BLK_ID:
65913859Sml29623 		nxge_txdma_inject_err(nxgep, err_id, chan);
65923859Sml29623 		break;
65933859Sml29623 	case RXDMA_BLK_ID:
65943859Sml29623 		nxge_rxdma_inject_err(nxgep, err_id, chan);
65953859Sml29623 		break;
65963859Sml29623 	case ZCP_BLK_ID:
65973859Sml29623 		nxge_zcp_inject_err(nxgep, err_id);
65983859Sml29623 		break;
65993859Sml29623 	case ESPC_BLK_ID:
66003859Sml29623 		break;
66013859Sml29623 	case FFLP_BLK_ID:
66023859Sml29623 		break;
66033859Sml29623 	case PHY_BLK_ID:
66043859Sml29623 		break;
66053859Sml29623 	case ETHER_SERDES_BLK_ID:
66063859Sml29623 		break;
66073859Sml29623 	case PCIE_SERDES_BLK_ID:
66083859Sml29623 		break;
66093859Sml29623 	case VIR_BLK_ID:
66103859Sml29623 		break;
66113859Sml29623 	}
66123859Sml29623 
66133859Sml29623 	nmp->b_wptr = nmp->b_rptr + size;
66143859Sml29623 	NXGE_DEBUG_MSG((nxgep, STR_CTL, "<== nxge_err_inject"));
66153859Sml29623 
66163859Sml29623 	miocack(wq, mp, (int)size, 0);
66173859Sml29623 }
66183859Sml29623 
66193859Sml29623 static int
66203859Sml29623 nxge_init_common_dev(p_nxge_t nxgep)
66213859Sml29623 {
66223859Sml29623 	p_nxge_hw_list_t	hw_p;
66233859Sml29623 	dev_info_t 		*p_dip;
66243859Sml29623 
662510577SMichael.Speer@Sun.COM 	ASSERT(nxgep != NULL);
662610577SMichael.Speer@Sun.COM 
66273859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "==> nxge_init_common_device"));
66283859Sml29623 
66293859Sml29623 	p_dip = nxgep->p_dip;
66303859Sml29623 	MUTEX_ENTER(&nxge_common_lock);
66313859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL,
66326512Ssowmini 	    "==> nxge_init_common_dev:func # %d",
66336512Ssowmini 	    nxgep->function_num));
66343859Sml29623 	/*
66353859Sml29623 	 * Loop through existing per neptune hardware list.
66363859Sml29623 	 */
66373859Sml29623 	for (hw_p = nxge_hw_list; hw_p; hw_p = hw_p->next) {
66383859Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
66396512Ssowmini 		    "==> nxge_init_common_device:func # %d "
66406512Ssowmini 		    "hw_p $%p parent dip $%p",
66416512Ssowmini 		    nxgep->function_num,
66426512Ssowmini 		    hw_p,
66436512Ssowmini 		    p_dip));
66443859Sml29623 		if (hw_p->parent_devp == p_dip) {
66453859Sml29623 			nxgep->nxge_hw_p = hw_p;
66463859Sml29623 			hw_p->ndevs++;
66473859Sml29623 			hw_p->nxge_p[nxgep->function_num] = nxgep;
66483859Sml29623 			NXGE_DEBUG_MSG((nxgep, MOD_CTL,
66496512Ssowmini 			    "==> nxge_init_common_device:func # %d "
66506512Ssowmini 			    "hw_p $%p parent dip $%p "
66516512Ssowmini 			    "ndevs %d (found)",
66526512Ssowmini 			    nxgep->function_num,
66536512Ssowmini 			    hw_p,
66546512Ssowmini 			    p_dip,
66556512Ssowmini 			    hw_p->ndevs));
66563859Sml29623 			break;
66573859Sml29623 		}
66583859Sml29623 	}
66593859Sml29623 
66603859Sml29623 	if (hw_p == NULL) {
66617801SSantwona.Behera@Sun.COM 
66627801SSantwona.Behera@Sun.COM 		char **prop_val;
66637801SSantwona.Behera@Sun.COM 		uint_t prop_len;
66647801SSantwona.Behera@Sun.COM 		int i;
66657801SSantwona.Behera@Sun.COM 
66663859Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
66676512Ssowmini 		    "==> nxge_init_common_device:func # %d "
66686512Ssowmini 		    "parent dip $%p (new)",
66696512Ssowmini 		    nxgep->function_num,
66706512Ssowmini 		    p_dip));
66713859Sml29623 		hw_p = kmem_zalloc(sizeof (nxge_hw_list_t), KM_SLEEP);
66723859Sml29623 		hw_p->parent_devp = p_dip;
66733859Sml29623 		hw_p->magic = NXGE_NEPTUNE_MAGIC;
66743859Sml29623 		nxgep->nxge_hw_p = hw_p;
66753859Sml29623 		hw_p->ndevs++;
66763859Sml29623 		hw_p->nxge_p[nxgep->function_num] = nxgep;
66773859Sml29623 		hw_p->next = nxge_hw_list;
66784732Sdavemq 		if (nxgep->niu_type == N2_NIU) {
66794732Sdavemq 			hw_p->niu_type = N2_NIU;
66804732Sdavemq 			hw_p->platform_type = P_NEPTUNE_NIU;
6681*11304SJanie.Lu@Sun.COM 			hw_p->tcam_size = TCAM_NIU_TCAM_MAX_ENTRY;
66824732Sdavemq 		} else {
66834732Sdavemq 			hw_p->niu_type = NIU_TYPE_NONE;
66844977Sraghus 			hw_p->platform_type = P_NEPTUNE_NONE;
6685*11304SJanie.Lu@Sun.COM 			hw_p->tcam_size = TCAM_NXGE_TCAM_MAX_ENTRY;
66864732Sdavemq 		}
66873859Sml29623 
6688*11304SJanie.Lu@Sun.COM 		hw_p->tcam = KMEM_ZALLOC(sizeof (tcam_flow_spec_t) *
6689*11304SJanie.Lu@Sun.COM 		    hw_p->tcam_size, KM_SLEEP);
6690*11304SJanie.Lu@Sun.COM 
66913859Sml29623 		MUTEX_INIT(&hw_p->nxge_cfg_lock, NULL, MUTEX_DRIVER, NULL);
66923859Sml29623 		MUTEX_INIT(&hw_p->nxge_tcam_lock, NULL, MUTEX_DRIVER, NULL);
66933859Sml29623 		MUTEX_INIT(&hw_p->nxge_vlan_lock, NULL, MUTEX_DRIVER, NULL);
66943859Sml29623 		MUTEX_INIT(&hw_p->nxge_mdio_lock, NULL, MUTEX_DRIVER, NULL);
66953859Sml29623 
66963859Sml29623 		nxge_hw_list = hw_p;
66974732Sdavemq 
66987801SSantwona.Behera@Sun.COM 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, nxgep->dip, 0,
66997801SSantwona.Behera@Sun.COM 		    "compatible", &prop_val, &prop_len) == DDI_PROP_SUCCESS) {
67007801SSantwona.Behera@Sun.COM 			for (i = 0; i < prop_len; i++) {
67017801SSantwona.Behera@Sun.COM 				if ((strcmp((caddr_t)prop_val[i],
67027801SSantwona.Behera@Sun.COM 				    NXGE_ROCK_COMPATIBLE) == 0)) {
67037801SSantwona.Behera@Sun.COM 					hw_p->platform_type = P_NEPTUNE_ROCK;
67047801SSantwona.Behera@Sun.COM 					NXGE_DEBUG_MSG((nxgep, MOD_CTL,
67057801SSantwona.Behera@Sun.COM 					    "ROCK hw_p->platform_type %d",
67067801SSantwona.Behera@Sun.COM 					    hw_p->platform_type));
67077801SSantwona.Behera@Sun.COM 					break;
67087801SSantwona.Behera@Sun.COM 				}
67097801SSantwona.Behera@Sun.COM 				NXGE_DEBUG_MSG((nxgep, MOD_CTL,
67107801SSantwona.Behera@Sun.COM 				    "nxge_init_common_dev: read compatible"
67117801SSantwona.Behera@Sun.COM 				    " property[%d] val[%s]",
67127801SSantwona.Behera@Sun.COM 				    i, (caddr_t)prop_val[i]));
67137801SSantwona.Behera@Sun.COM 			}
67147801SSantwona.Behera@Sun.COM 		}
67157801SSantwona.Behera@Sun.COM 
67167801SSantwona.Behera@Sun.COM 		ddi_prop_free(prop_val);
67177801SSantwona.Behera@Sun.COM 
67184732Sdavemq 		(void) nxge_scan_ports_phy(nxgep, nxge_hw_list);
67193859Sml29623 	}
67203859Sml29623 
67213859Sml29623 	MUTEX_EXIT(&nxge_common_lock);
67224732Sdavemq 
67234977Sraghus 	nxgep->platform_type = hw_p->platform_type;
67247801SSantwona.Behera@Sun.COM 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "nxgep->platform_type %d",
67257801SSantwona.Behera@Sun.COM 	    nxgep->platform_type));
67264732Sdavemq 	if (nxgep->niu_type != N2_NIU) {
67274732Sdavemq 		nxgep->niu_type = hw_p->niu_type;
67284732Sdavemq 	}
67294732Sdavemq 
67303859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL,
67316512Ssowmini 	    "==> nxge_init_common_device (nxge_hw_list) $%p",
67326512Ssowmini 	    nxge_hw_list));
67333859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "<== nxge_init_common_device"));
67343859Sml29623 
67353859Sml29623 	return (NXGE_OK);
67363859Sml29623 }
67373859Sml29623 
67383859Sml29623 static void
67393859Sml29623 nxge_uninit_common_dev(p_nxge_t nxgep)
67403859Sml29623 {
67413859Sml29623 	p_nxge_hw_list_t	hw_p, h_hw_p;
67426801Sspeer 	p_nxge_dma_pt_cfg_t	p_dma_cfgp;
67436801Sspeer 	p_nxge_hw_pt_cfg_t	p_cfgp;
67443859Sml29623 	dev_info_t 		*p_dip;
67453859Sml29623 
674610577SMichael.Speer@Sun.COM 	ASSERT(nxgep != NULL);
674710577SMichael.Speer@Sun.COM 
67483859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "==> nxge_uninit_common_device"));
67493859Sml29623 	if (nxgep->nxge_hw_p == NULL) {
67503859Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
67516512Ssowmini 		    "<== nxge_uninit_common_device (no common)"));
67523859Sml29623 		return;
67533859Sml29623 	}
67543859Sml29623 
67553859Sml29623 	MUTEX_ENTER(&nxge_common_lock);
67563859Sml29623 	h_hw_p = nxge_hw_list;
67573859Sml29623 	for (hw_p = nxge_hw_list; hw_p; hw_p = hw_p->next) {
67583859Sml29623 		p_dip = hw_p->parent_devp;
67593859Sml29623 		if (nxgep->nxge_hw_p == hw_p &&
67606512Ssowmini 		    p_dip == nxgep->p_dip &&
67616512Ssowmini 		    nxgep->nxge_hw_p->magic == NXGE_NEPTUNE_MAGIC &&
67626512Ssowmini 		    hw_p->magic == NXGE_NEPTUNE_MAGIC) {
67633859Sml29623 
67643859Sml29623 			NXGE_DEBUG_MSG((nxgep, MOD_CTL,
67656512Ssowmini 			    "==> nxge_uninit_common_device:func # %d "
67666512Ssowmini 			    "hw_p $%p parent dip $%p "
67676512Ssowmini 			    "ndevs %d (found)",
67686512Ssowmini 			    nxgep->function_num,
67696512Ssowmini 			    hw_p,
67706512Ssowmini 			    p_dip,
67716512Ssowmini 			    hw_p->ndevs));
67723859Sml29623 
67736801Sspeer 			/*
67746801Sspeer 			 * Release the RDC table, a shared resoruce
67756801Sspeer 			 * of the nxge hardware.  The RDC table was
67766801Sspeer 			 * assigned to this instance of nxge in
67776801Sspeer 			 * nxge_use_cfg_dma_config().
67786801Sspeer 			 */
67797587SMichael.Speer@Sun.COM 			if (!isLDOMguest(nxgep)) {
67807587SMichael.Speer@Sun.COM 				p_dma_cfgp =
67817587SMichael.Speer@Sun.COM 				    (p_nxge_dma_pt_cfg_t)&nxgep->pt_config;
67827587SMichael.Speer@Sun.COM 				p_cfgp =
67837587SMichael.Speer@Sun.COM 				    (p_nxge_hw_pt_cfg_t)&p_dma_cfgp->hw_config;
67847587SMichael.Speer@Sun.COM 				(void) nxge_fzc_rdc_tbl_unbind(nxgep,
67857587SMichael.Speer@Sun.COM 				    p_cfgp->def_mac_rxdma_grpid);
67867766SMichael.Speer@Sun.COM 
67877766SMichael.Speer@Sun.COM 				/* Cleanup any outstanding groups.  */
67887766SMichael.Speer@Sun.COM 				nxge_grp_cleanup(nxgep);
67897587SMichael.Speer@Sun.COM 			}
67906801Sspeer 
67913859Sml29623 			if (hw_p->ndevs) {
67923859Sml29623 				hw_p->ndevs--;
67933859Sml29623 			}
67943859Sml29623 			hw_p->nxge_p[nxgep->function_num] = NULL;
67953859Sml29623 			if (!hw_p->ndevs) {
6796*11304SJanie.Lu@Sun.COM 				KMEM_FREE(hw_p->tcam,
6797*11304SJanie.Lu@Sun.COM 				    sizeof (tcam_flow_spec_t) *
6798*11304SJanie.Lu@Sun.COM 				    hw_p->tcam_size);
67993859Sml29623 				MUTEX_DESTROY(&hw_p->nxge_vlan_lock);
68003859Sml29623 				MUTEX_DESTROY(&hw_p->nxge_tcam_lock);
68013859Sml29623 				MUTEX_DESTROY(&hw_p->nxge_cfg_lock);
68023859Sml29623 				MUTEX_DESTROY(&hw_p->nxge_mdio_lock);
68033859Sml29623 				NXGE_DEBUG_MSG((nxgep, MOD_CTL,
68046512Ssowmini 				    "==> nxge_uninit_common_device: "
68056512Ssowmini 				    "func # %d "
68066512Ssowmini 				    "hw_p $%p parent dip $%p "
68076512Ssowmini 				    "ndevs %d (last)",
68086512Ssowmini 				    nxgep->function_num,
68096512Ssowmini 				    hw_p,
68106512Ssowmini 				    p_dip,
68116512Ssowmini 				    hw_p->ndevs));
68123859Sml29623 
68136495Sspeer 				nxge_hio_uninit(nxgep);
68146495Sspeer 
68153859Sml29623 				if (hw_p == nxge_hw_list) {
68163859Sml29623 					NXGE_DEBUG_MSG((nxgep, MOD_CTL,
68176512Ssowmini 					    "==> nxge_uninit_common_device:"
68186512Ssowmini 					    "remove head func # %d "
68196512Ssowmini 					    "hw_p $%p parent dip $%p "
68206512Ssowmini 					    "ndevs %d (head)",
68216512Ssowmini 					    nxgep->function_num,
68226512Ssowmini 					    hw_p,
68236512Ssowmini 					    p_dip,
68246512Ssowmini 					    hw_p->ndevs));
68253859Sml29623 					nxge_hw_list = hw_p->next;
68263859Sml29623 				} else {
68273859Sml29623 					NXGE_DEBUG_MSG((nxgep, MOD_CTL,
68286512Ssowmini 					    "==> nxge_uninit_common_device:"
68296512Ssowmini 					    "remove middle func # %d "
68306512Ssowmini 					    "hw_p $%p parent dip $%p "
68316512Ssowmini 					    "ndevs %d (middle)",
68326512Ssowmini 					    nxgep->function_num,
68336512Ssowmini 					    hw_p,
68346512Ssowmini 					    p_dip,
68356512Ssowmini 					    hw_p->ndevs));
68363859Sml29623 					h_hw_p->next = hw_p->next;
68373859Sml29623 				}
68383859Sml29623 
68396495Sspeer 				nxgep->nxge_hw_p = NULL;
68403859Sml29623 				KMEM_FREE(hw_p, sizeof (nxge_hw_list_t));
68413859Sml29623 			}
68423859Sml29623 			break;
68433859Sml29623 		} else {
68443859Sml29623 			h_hw_p = hw_p;
68453859Sml29623 		}
68463859Sml29623 	}
68473859Sml29623 
68483859Sml29623 	MUTEX_EXIT(&nxge_common_lock);
68493859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL,
68506512Ssowmini 	    "==> nxge_uninit_common_device (nxge_hw_list) $%p",
68516512Ssowmini 	    nxge_hw_list));
68523859Sml29623 
68533859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "<= nxge_uninit_common_device"));
68543859Sml29623 }
68554732Sdavemq 
68564732Sdavemq /*
68574977Sraghus  * Determines the number of ports from the niu_type or the platform type.
68584732Sdavemq  * Returns the number of ports, or returns zero on failure.
68594732Sdavemq  */
68604732Sdavemq 
68614732Sdavemq int
68624977Sraghus nxge_get_nports(p_nxge_t nxgep)
68634732Sdavemq {
68644732Sdavemq 	int	nports = 0;
68654732Sdavemq 
68664977Sraghus 	switch (nxgep->niu_type) {
68674732Sdavemq 	case N2_NIU:
68684732Sdavemq 	case NEPTUNE_2_10GF:
68694732Sdavemq 		nports = 2;
68704732Sdavemq 		break;
68714732Sdavemq 	case NEPTUNE_4_1GC:
68724732Sdavemq 	case NEPTUNE_2_10GF_2_1GC:
68734732Sdavemq 	case NEPTUNE_1_10GF_3_1GC:
68744732Sdavemq 	case NEPTUNE_1_1GC_1_10GF_2_1GC:
68756261Sjoycey 	case NEPTUNE_2_10GF_2_1GRF:
68764732Sdavemq 		nports = 4;
68774732Sdavemq 		break;
68784732Sdavemq 	default:
68794977Sraghus 		switch (nxgep->platform_type) {
68804977Sraghus 		case P_NEPTUNE_NIU:
68814977Sraghus 		case P_NEPTUNE_ATLAS_2PORT:
68824977Sraghus 			nports = 2;
68834977Sraghus 			break;
68844977Sraghus 		case P_NEPTUNE_ATLAS_4PORT:
68854977Sraghus 		case P_NEPTUNE_MARAMBA_P0:
68864977Sraghus 		case P_NEPTUNE_MARAMBA_P1:
68877801SSantwona.Behera@Sun.COM 		case P_NEPTUNE_ROCK:
68885196Ssbehera 		case P_NEPTUNE_ALONSO:
68894977Sraghus 			nports = 4;
68904977Sraghus 			break;
68914977Sraghus 		default:
68924977Sraghus 			break;
68934977Sraghus 		}
68944732Sdavemq 		break;
68954732Sdavemq 	}
68964732Sdavemq 
68974732Sdavemq 	return (nports);
68984732Sdavemq }
68995013Sml29623 
69005013Sml29623 /*
69015013Sml29623  * The following two functions are to support
69025013Sml29623  * PSARC/2007/453 MSI-X interrupt limit override.
69035013Sml29623  */
69045013Sml29623 static int
69055013Sml29623 nxge_create_msi_property(p_nxge_t nxgep)
69065013Sml29623 {
69075013Sml29623 	int	nmsi;
69085013Sml29623 	extern	int ncpus;
69095013Sml29623 
69105013Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "==>nxge_create_msi_property"));
69115013Sml29623 
69125013Sml29623 	switch (nxgep->mac.portmode) {
69135013Sml29623 	case PORT_10G_COPPER:
69145013Sml29623 	case PORT_10G_FIBER:
69156835Syc148097 	case PORT_10G_TN1010:
69165013Sml29623 		(void) ddi_prop_create(DDI_DEV_T_NONE, nxgep->dip,
69175013Sml29623 		    DDI_PROP_CANSLEEP, "#msix-request", NULL, 0);
69185013Sml29623 		/*
69195013Sml29623 		 * The maximum MSI-X requested will be 8.
69208455Stc99174@train 		 * If the # of CPUs is less than 8, we will request
69218455Stc99174@train 		 * # MSI-X based on the # of CPUs (default).
69225013Sml29623 		 */
69238455Stc99174@train 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
69248455Stc99174@train 		    "==>nxge_create_msi_property (10G): nxge_msix_10g_intrs %d",
69258455Stc99174@train 		    nxge_msix_10g_intrs));
69268455Stc99174@train 		if ((nxge_msix_10g_intrs == 0) ||
69278455Stc99174@train 		    (nxge_msix_10g_intrs > NXGE_MSIX_MAX_ALLOWED)) {
69285013Sml29623 			nmsi = NXGE_MSIX_REQUEST_10G;
69298455Stc99174@train 			NXGE_DEBUG_MSG((nxgep, MOD_CTL,
69308455Stc99174@train 			    "==>nxge_create_msi_property (10G): reset to 8"));
69315013Sml29623 		} else {
69328455Stc99174@train 			nmsi = nxge_msix_10g_intrs;
69338455Stc99174@train 		}
69348455Stc99174@train 
69358455Stc99174@train 		/*
69368455Stc99174@train 		 * If # of interrupts requested is 8 (default),
69378455Stc99174@train 		 * the checking of the number of cpus will be
69388455Stc99174@train 		 * be maintained.
69398455Stc99174@train 		 */
69408455Stc99174@train 		if ((nmsi == NXGE_MSIX_REQUEST_10G) &&
69418455Stc99174@train 		    (ncpus < nmsi)) {
69428455Stc99174@train 			NXGE_DEBUG_MSG((nxgep, MOD_CTL,
69438455Stc99174@train 			    "==>nxge_create_msi_property (10G): reset to 8"));
69445013Sml29623 			nmsi = ncpus;
69455013Sml29623 		}
69465013Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
69475013Sml29623 		    "==>nxge_create_msi_property(10G): exists 0x%x (nmsi %d)",
69485013Sml29623 		    ddi_prop_exists(DDI_DEV_T_NONE, nxgep->dip,
69495013Sml29623 		    DDI_PROP_CANSLEEP, "#msix-request"), nmsi));
69505013Sml29623 		break;
69515013Sml29623 
69525013Sml29623 	default:
69538455Stc99174@train 		(void) ddi_prop_create(DDI_DEV_T_NONE, nxgep->dip,
69548455Stc99174@train 		    DDI_PROP_CANSLEEP, "#msix-request", NULL, 0);
69558455Stc99174@train 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
69568455Stc99174@train 		    "==>nxge_create_msi_property (1G): nxge_msix_1g_intrs %d",
69578455Stc99174@train 		    nxge_msix_1g_intrs));
69588455Stc99174@train 		if ((nxge_msix_1g_intrs == 0) ||
69598455Stc99174@train 		    (nxge_msix_1g_intrs > NXGE_MSIX_MAX_ALLOWED)) {
69608455Stc99174@train 			nmsi = NXGE_MSIX_REQUEST_1G;
69618455Stc99174@train 			NXGE_DEBUG_MSG((nxgep, MOD_CTL,
69628455Stc99174@train 			    "==>nxge_create_msi_property (1G): reset to 2"));
69638455Stc99174@train 		} else {
69648455Stc99174@train 			nmsi = nxge_msix_1g_intrs;
69658455Stc99174@train 		}
69665013Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
69675013Sml29623 		    "==>nxge_create_msi_property(1G): exists 0x%x (nmsi %d)",
69685013Sml29623 		    ddi_prop_exists(DDI_DEV_T_NONE, nxgep->dip,
69695013Sml29623 		    DDI_PROP_CANSLEEP, "#msix-request"), nmsi));
69705013Sml29623 		break;
69715013Sml29623 	}
69725013Sml29623 
69735013Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "<==nxge_create_msi_property"));
69745013Sml29623 	return (nmsi);
69755013Sml29623 }
69766512Ssowmini 
69776512Ssowmini /* ARGSUSED */
69786512Ssowmini static int
69796512Ssowmini nxge_get_def_val(nxge_t *nxgep, mac_prop_id_t pr_num, uint_t pr_valsize,
69806512Ssowmini     void *pr_val)
69816512Ssowmini {
69826512Ssowmini 	int err = 0;
69836512Ssowmini 	link_flowctrl_t fl;
69846512Ssowmini 
69856512Ssowmini 	switch (pr_num) {
69866789Sam223141 	case MAC_PROP_AUTONEG:
69876512Ssowmini 		*(uint8_t *)pr_val = 1;
69886512Ssowmini 		break;
69896789Sam223141 	case MAC_PROP_FLOWCTRL:
69906512Ssowmini 		if (pr_valsize < sizeof (link_flowctrl_t))
69916512Ssowmini 			return (EINVAL);
69926512Ssowmini 		fl = LINK_FLOWCTRL_RX;
69936512Ssowmini 		bcopy(&fl, pr_val, sizeof (fl));
69946512Ssowmini 		break;
69956789Sam223141 	case MAC_PROP_ADV_1000FDX_CAP:
69966789Sam223141 	case MAC_PROP_EN_1000FDX_CAP:
69976512Ssowmini 		*(uint8_t *)pr_val = 1;
69986512Ssowmini 		break;
69996789Sam223141 	case MAC_PROP_ADV_100FDX_CAP:
70006789Sam223141 	case MAC_PROP_EN_100FDX_CAP:
70016512Ssowmini 		*(uint8_t *)pr_val = 1;
70026512Ssowmini 		break;
70036512Ssowmini 	default:
70046512Ssowmini 		err = ENOTSUP;
70056512Ssowmini 		break;
70066512Ssowmini 	}
70076512Ssowmini 	return (err);
70086512Ssowmini }
70096705Sml29623 
70106705Sml29623 
70116705Sml29623 /*
70126705Sml29623  * The following is a software around for the Neptune hardware's
70136705Sml29623  * interrupt bugs; The Neptune hardware may generate spurious interrupts when
70146705Sml29623  * an interrupr handler is removed.
70156705Sml29623  */
70166705Sml29623 #define	NXGE_PCI_PORT_LOGIC_OFFSET	0x98
70176705Sml29623 #define	NXGE_PIM_RESET			(1ULL << 29)
70186705Sml29623 #define	NXGE_GLU_RESET			(1ULL << 30)
70196705Sml29623 #define	NXGE_NIU_RESET			(1ULL << 31)
70206705Sml29623 #define	NXGE_PCI_RESET_ALL		(NXGE_PIM_RESET |	\
70216705Sml29623 					NXGE_GLU_RESET |	\
70226705Sml29623 					NXGE_NIU_RESET)
70236705Sml29623 
70246705Sml29623 #define	NXGE_WAIT_QUITE_TIME		200000
70256705Sml29623 #define	NXGE_WAIT_QUITE_RETRY		40
70266705Sml29623 #define	NXGE_PCI_RESET_WAIT		1000000 /* one second */
70276705Sml29623 
70286705Sml29623 static void
70296705Sml29623 nxge_niu_peu_reset(p_nxge_t nxgep)
70306705Sml29623 {
70316705Sml29623 	uint32_t	rvalue;
70326705Sml29623 	p_nxge_hw_list_t hw_p;
70336705Sml29623 	p_nxge_t	fnxgep;
70346705Sml29623 	int		i, j;
70356705Sml29623 
70366705Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL, "==> nxge_niu_peu_reset"));
70376705Sml29623 	if ((hw_p = nxgep->nxge_hw_p) == NULL) {
70386705Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
70396705Sml29623 		    "==> nxge_niu_peu_reset: NULL hardware pointer"));
70406705Sml29623 		return;
70416705Sml29623 	}
70426705Sml29623 
70436705Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
70446705Sml29623 	    "==> nxge_niu_peu_reset: flags 0x%x link timer id %d timer id %d",
70456705Sml29623 	    hw_p->flags, nxgep->nxge_link_poll_timerid,
70466705Sml29623 	    nxgep->nxge_timerid));
70476705Sml29623 
70486705Sml29623 	MUTEX_ENTER(&hw_p->nxge_cfg_lock);
70496705Sml29623 	/*
70506705Sml29623 	 * Make sure other instances from the same hardware
70516705Sml29623 	 * stop sending PIO and in quiescent state.
70526705Sml29623 	 */
70536705Sml29623 	for (i = 0; i < NXGE_MAX_PORTS; i++) {
70546705Sml29623 		fnxgep = hw_p->nxge_p[i];
70556705Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
70566705Sml29623 		    "==> nxge_niu_peu_reset: checking entry %d "
70576705Sml29623 		    "nxgep $%p", i, fnxgep));
70586705Sml29623 #ifdef	NXGE_DEBUG
70596705Sml29623 		if (fnxgep) {
70606705Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
70616705Sml29623 			    "==> nxge_niu_peu_reset: entry %d (function %d) "
70626705Sml29623 			    "link timer id %d hw timer id %d",
70636705Sml29623 			    i, fnxgep->function_num,
70646705Sml29623 			    fnxgep->nxge_link_poll_timerid,
70656705Sml29623 			    fnxgep->nxge_timerid));
70666705Sml29623 		}
70676705Sml29623 #endif
70686705Sml29623 		if (fnxgep && fnxgep != nxgep &&
70696705Sml29623 		    (fnxgep->nxge_timerid || fnxgep->nxge_link_poll_timerid)) {
70706705Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
70716705Sml29623 			    "==> nxge_niu_peu_reset: checking $%p "
70726705Sml29623 			    "(function %d) timer ids",
70736705Sml29623 			    fnxgep, fnxgep->function_num));
70746705Sml29623 			for (j = 0; j < NXGE_WAIT_QUITE_RETRY; j++) {
70756705Sml29623 				NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
70766705Sml29623 				    "==> nxge_niu_peu_reset: waiting"));
70776705Sml29623 				NXGE_DELAY(NXGE_WAIT_QUITE_TIME);
70786705Sml29623 				if (!fnxgep->nxge_timerid &&
70796705Sml29623 				    !fnxgep->nxge_link_poll_timerid) {
70806705Sml29623 					break;
70816705Sml29623 				}
70826705Sml29623 			}
70836705Sml29623 			NXGE_DELAY(NXGE_WAIT_QUITE_TIME);
70846705Sml29623 			if (fnxgep->nxge_timerid ||
70856705Sml29623 			    fnxgep->nxge_link_poll_timerid) {
70866705Sml29623 				MUTEX_EXIT(&hw_p->nxge_cfg_lock);
70876705Sml29623 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
70886705Sml29623 				    "<== nxge_niu_peu_reset: cannot reset "
70896705Sml29623 				    "hardware (devices are still in use)"));
70906705Sml29623 				return;
70916705Sml29623 			}
70926705Sml29623 		}
70936705Sml29623 	}
70946705Sml29623 
70956705Sml29623 	if ((hw_p->flags & COMMON_RESET_NIU_PCI) != COMMON_RESET_NIU_PCI) {
70966705Sml29623 		hw_p->flags |= COMMON_RESET_NIU_PCI;
70976705Sml29623 		rvalue = pci_config_get32(nxgep->dev_regs->nxge_pciregh,
70986705Sml29623 		    NXGE_PCI_PORT_LOGIC_OFFSET);
70996705Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
71006705Sml29623 		    "nxge_niu_peu_reset: read offset 0x%x (%d) "
71016705Sml29623 		    "(data 0x%x)",
71026705Sml29623 		    NXGE_PCI_PORT_LOGIC_OFFSET,
71036705Sml29623 		    NXGE_PCI_PORT_LOGIC_OFFSET,
71046705Sml29623 		    rvalue));
71056705Sml29623 
71066705Sml29623 		rvalue |= NXGE_PCI_RESET_ALL;
71076705Sml29623 		pci_config_put32(nxgep->dev_regs->nxge_pciregh,
71086705Sml29623 		    NXGE_PCI_PORT_LOGIC_OFFSET, rvalue);
71096705Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
71106705Sml29623 		    "nxge_niu_peu_reset: RESETTING NIU: write NIU reset 0x%x",
71116705Sml29623 		    rvalue));
71126705Sml29623 
71136705Sml29623 		NXGE_DELAY(NXGE_PCI_RESET_WAIT);
71146705Sml29623 	}
71156705Sml29623 
71166705Sml29623 	MUTEX_EXIT(&hw_p->nxge_cfg_lock);
71176705Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL, "<== nxge_niu_peu_reset"));
71186705Sml29623 }
71197126Sml29623 
71207126Sml29623 static void
71217126Sml29623 nxge_set_pci_replay_timeout(p_nxge_t nxgep)
71227126Sml29623 {
71238275SEric Cheng 	p_dev_regs_t	dev_regs;
71247126Sml29623 	uint32_t	value;
71257126Sml29623 
71267126Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_set_pci_replay_timeout"));
71277126Sml29623 
71287126Sml29623 	if (!nxge_set_replay_timer) {
71297126Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
71307126Sml29623 		    "==> nxge_set_pci_replay_timeout: will not change "
71317126Sml29623 		    "the timeout"));
71327126Sml29623 		return;
71337126Sml29623 	}
71347126Sml29623 
71357126Sml29623 	dev_regs = nxgep->dev_regs;
71367126Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
71377126Sml29623 	    "==> nxge_set_pci_replay_timeout: dev_regs 0x%p pcireg 0x%p",
71387126Sml29623 	    dev_regs, dev_regs->nxge_pciregh));
71397126Sml29623 
71407126Sml29623 	if (dev_regs == NULL || (dev_regs->nxge_pciregh == NULL)) {
71417145Syc148097 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
71427126Sml29623 		    "==> nxge_set_pci_replay_timeout: NULL dev_regs $%p or "
71437126Sml29623 		    "no PCI handle",
71447126Sml29623 		    dev_regs));
71457126Sml29623 		return;
71467126Sml29623 	}
71477126Sml29623 	value = (pci_config_get32(dev_regs->nxge_pciregh,
71487126Sml29623 	    PCI_REPLAY_TIMEOUT_CFG_OFFSET) |
71497126Sml29623 	    (nxge_replay_timeout << PCI_REPLAY_TIMEOUT_SHIFT));
71507126Sml29623 
71517126Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
71527126Sml29623 	    "nxge_set_pci_replay_timeout: replay timeout value before set 0x%x "
71537126Sml29623 	    "(timeout value to set 0x%x at offset 0x%x) value 0x%x",
71547126Sml29623 	    pci_config_get32(dev_regs->nxge_pciregh,
71557126Sml29623 	    PCI_REPLAY_TIMEOUT_CFG_OFFSET), nxge_replay_timeout,
71567126Sml29623 	    PCI_REPLAY_TIMEOUT_CFG_OFFSET, value));
71577126Sml29623 
71587126Sml29623 	pci_config_put32(dev_regs->nxge_pciregh, PCI_REPLAY_TIMEOUT_CFG_OFFSET,
71597126Sml29623 	    value);
71607126Sml29623 
71617126Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
71627126Sml29623 	    "nxge_set_pci_replay_timeout: replay timeout value after set 0x%x",
71637126Sml29623 	    pci_config_get32(dev_regs->nxge_pciregh,
71647126Sml29623 	    PCI_REPLAY_TIMEOUT_CFG_OFFSET)));
71657126Sml29623 
71667126Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_set_pci_replay_timeout"));
71677126Sml29623 }
71687656SSherry.Moore@Sun.COM 
71697656SSherry.Moore@Sun.COM /*
71707656SSherry.Moore@Sun.COM  * quiesce(9E) entry point.
71717656SSherry.Moore@Sun.COM  *
71727656SSherry.Moore@Sun.COM  * This function is called when the system is single-threaded at high
71737656SSherry.Moore@Sun.COM  * PIL with preemption disabled. Therefore, this function must not be
71747656SSherry.Moore@Sun.COM  * blocked.
71757656SSherry.Moore@Sun.COM  *
71767656SSherry.Moore@Sun.COM  * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
71777656SSherry.Moore@Sun.COM  * DDI_FAILURE indicates an error condition and should almost never happen.
71787656SSherry.Moore@Sun.COM  */
71797656SSherry.Moore@Sun.COM static int
71807656SSherry.Moore@Sun.COM nxge_quiesce(dev_info_t *dip)
71817656SSherry.Moore@Sun.COM {
71827656SSherry.Moore@Sun.COM 	int instance = ddi_get_instance(dip);
71837656SSherry.Moore@Sun.COM 	p_nxge_t nxgep = (p_nxge_t)ddi_get_soft_state(nxge_list, instance);
71847656SSherry.Moore@Sun.COM 
71857656SSherry.Moore@Sun.COM 	if (nxgep == NULL)
71867656SSherry.Moore@Sun.COM 		return (DDI_FAILURE);
71877656SSherry.Moore@Sun.COM 
71887656SSherry.Moore@Sun.COM 	/* Turn off debugging */
71897656SSherry.Moore@Sun.COM 	nxge_debug_level = NO_DEBUG;
71907656SSherry.Moore@Sun.COM 	nxgep->nxge_debug_level = NO_DEBUG;
71917656SSherry.Moore@Sun.COM 	npi_debug_level = NO_DEBUG;
71927656SSherry.Moore@Sun.COM 
71937656SSherry.Moore@Sun.COM 	/*
71947656SSherry.Moore@Sun.COM 	 * Stop link monitor only when linkchkmod is interrupt based
71957656SSherry.Moore@Sun.COM 	 */
71967656SSherry.Moore@Sun.COM 	if (nxgep->mac.linkchkmode == LINKCHK_INTR) {
71977656SSherry.Moore@Sun.COM 		(void) nxge_link_monitor(nxgep, LINK_MONITOR_STOP);
71987656SSherry.Moore@Sun.COM 	}
71997656SSherry.Moore@Sun.COM 
72007656SSherry.Moore@Sun.COM 	(void) nxge_intr_hw_disable(nxgep);
72017656SSherry.Moore@Sun.COM 
72027656SSherry.Moore@Sun.COM 	/*
72037656SSherry.Moore@Sun.COM 	 * Reset the receive MAC side.
72047656SSherry.Moore@Sun.COM 	 */
72057656SSherry.Moore@Sun.COM 	(void) nxge_rx_mac_disable(nxgep);
72067656SSherry.Moore@Sun.COM 
72077656SSherry.Moore@Sun.COM 	/* Disable and soft reset the IPP */
72087656SSherry.Moore@Sun.COM 	if (!isLDOMguest(nxgep))
72097656SSherry.Moore@Sun.COM 		(void) nxge_ipp_disable(nxgep);
72107656SSherry.Moore@Sun.COM 
72117656SSherry.Moore@Sun.COM 	/*
72127656SSherry.Moore@Sun.COM 	 * Reset the transmit/receive DMA side.
72137656SSherry.Moore@Sun.COM 	 */
72147656SSherry.Moore@Sun.COM 	(void) nxge_txdma_hw_mode(nxgep, NXGE_DMA_STOP);
72157656SSherry.Moore@Sun.COM 	(void) nxge_rxdma_hw_mode(nxgep, NXGE_DMA_STOP);
72167656SSherry.Moore@Sun.COM 
72177656SSherry.Moore@Sun.COM 	/*
72187656SSherry.Moore@Sun.COM 	 * Reset the transmit MAC side.
72197656SSherry.Moore@Sun.COM 	 */
72207656SSherry.Moore@Sun.COM 	(void) nxge_tx_mac_disable(nxgep);
72217656SSherry.Moore@Sun.COM 
72227656SSherry.Moore@Sun.COM 	return (DDI_SUCCESS);
72237656SSherry.Moore@Sun.COM }
7224