xref: /onnv-gate/usr/src/uts/common/io/nxge/nxge_main.c (revision 10392:952e1ebcd49b)
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;
993859Sml29623 uint32_t 	nxge_tx_ring_size = NXGE_TX_RING_DEFAULT;
1004193Sspeer boolean_t 	nxge_no_msg = B_TRUE;		/* control message display */
1013859Sml29623 uint32_t 	nxge_no_link_notify = 0;	/* control DL_NOTIFY */
1023859Sml29623 uint32_t 	nxge_bcopy_thresh = TX_BCOPY_MAX;
1033859Sml29623 uint32_t 	nxge_dvma_thresh = TX_FASTDVMA_MIN;
1043859Sml29623 uint32_t 	nxge_dma_stream_thresh = TX_STREAM_MIN;
1053859Sml29623 uint32_t	nxge_jumbo_mtu	= TX_JUMBO_MTU;
1063952Sml29623 nxge_tx_mode_t	nxge_tx_scheme = NXGE_USE_SERIAL;
1073859Sml29623 
1085770Sml29623 /* MAX LSO size */
1095770Sml29623 #define		NXGE_LSO_MAXLEN	65535
1105770Sml29623 uint32_t	nxge_lso_max = NXGE_LSO_MAXLEN;
1115770Sml29623 
1123859Sml29623 
1133859Sml29623 /*
1143859Sml29623  * Add tunable to reduce the amount of time spent in the
1153859Sml29623  * ISR doing Rx Processing.
1163859Sml29623  */
1173859Sml29623 uint32_t nxge_max_rx_pkts = 1024;
1183859Sml29623 
1193859Sml29623 /*
1203859Sml29623  * Tunables to manage the receive buffer blocks.
1213859Sml29623  *
1223859Sml29623  * nxge_rx_threshold_hi: copy all buffers.
1233859Sml29623  * nxge_rx_bcopy_size_type: receive buffer block size type.
1243859Sml29623  * nxge_rx_threshold_lo: copy only up to tunable block size type.
1253859Sml29623  */
1263859Sml29623 nxge_rxbuf_threshold_t nxge_rx_threshold_hi = NXGE_RX_COPY_6;
1273859Sml29623 nxge_rxbuf_type_t nxge_rx_buf_size_type = RCR_PKTBUFSZ_0;
1283859Sml29623 nxge_rxbuf_threshold_t nxge_rx_threshold_lo = NXGE_RX_COPY_3;
1293859Sml29623 
1306495Sspeer /* Use kmem_alloc() to allocate data buffers. */
1316909Sml29623 #if defined(_BIG_ENDIAN)
1326498Sspeer uint32_t	nxge_use_kmem_alloc = 1;
1336495Sspeer #else
1346498Sspeer uint32_t	nxge_use_kmem_alloc = 0;
1356495Sspeer #endif
1366495Sspeer 
1373859Sml29623 rtrace_t npi_rtracebuf;
1383859Sml29623 
1397126Sml29623 /*
1407126Sml29623  * The hardware sometimes fails to allow enough time for the link partner
1417126Sml29623  * to send an acknowledgement for packets that the hardware sent to it. The
1427126Sml29623  * hardware resends the packets earlier than it should be in those instances.
1437126Sml29623  * This behavior caused some switches to acknowledge the wrong packets
1447126Sml29623  * and it triggered the fatal error.
1457126Sml29623  * This software workaround is to set the replay timer to a value
1467126Sml29623  * suggested by the hardware team.
1477126Sml29623  *
1487126Sml29623  * PCI config space replay timer register:
1497126Sml29623  *     The following replay timeout value is 0xc
1507126Sml29623  *     for bit 14:18.
1517126Sml29623  */
1527126Sml29623 #define	PCI_REPLAY_TIMEOUT_CFG_OFFSET	0xb8
1537126Sml29623 #define	PCI_REPLAY_TIMEOUT_SHIFT	14
1547126Sml29623 
1557126Sml29623 uint32_t	nxge_set_replay_timer = 1;
1567126Sml29623 uint32_t	nxge_replay_timeout = 0xc;
1577126Sml29623 
1587241Sml29623 /*
1597241Sml29623  * The transmit serialization sometimes causes
1607241Sml29623  * longer sleep before calling the driver transmit
1617241Sml29623  * function as it sleeps longer than it should.
1627241Sml29623  * The performace group suggests that a time wait tunable
1637241Sml29623  * can be used to set the maximum wait time when needed
1647241Sml29623  * and the default is set to 1 tick.
1657241Sml29623  */
1667241Sml29623 uint32_t	nxge_tx_serial_maxsleep = 1;
1677241Sml29623 
1683859Sml29623 #if	defined(sun4v)
1693859Sml29623 /*
1703859Sml29623  * Hypervisor N2/NIU services information.
1713859Sml29623  */
1723859Sml29623 static hsvc_info_t niu_hsvc = {
1733859Sml29623 	HSVC_REV_1, NULL, HSVC_GROUP_NIU, NIU_MAJOR_VER,
1743859Sml29623 	NIU_MINOR_VER, "nxge"
1753859Sml29623 };
1766495Sspeer 
1776495Sspeer static int nxge_hsvc_register(p_nxge_t);
1783859Sml29623 #endif
1793859Sml29623 
1803859Sml29623 /*
1813859Sml29623  * Function Prototypes
1823859Sml29623  */
1833859Sml29623 static int nxge_attach(dev_info_t *, ddi_attach_cmd_t);
1843859Sml29623 static int nxge_detach(dev_info_t *, ddi_detach_cmd_t);
1853859Sml29623 static void nxge_unattach(p_nxge_t);
1867656SSherry.Moore@Sun.COM static int nxge_quiesce(dev_info_t *);
1873859Sml29623 
1883859Sml29623 #if NXGE_PROPERTY
1893859Sml29623 static void nxge_remove_hard_properties(p_nxge_t);
1903859Sml29623 #endif
1913859Sml29623 
1926495Sspeer /*
1936495Sspeer  * These two functions are required by nxge_hio.c
1946495Sspeer  */
1958275SEric Cheng extern int nxge_m_mmac_remove(void *arg, int slot);
1967766SMichael.Speer@Sun.COM extern void nxge_grp_cleanup(p_nxge_t nxge);
1976495Sspeer 
1983859Sml29623 static nxge_status_t nxge_setup_system_dma_pages(p_nxge_t);
1993859Sml29623 
2003859Sml29623 static nxge_status_t nxge_setup_mutexes(p_nxge_t);
2013859Sml29623 static void nxge_destroy_mutexes(p_nxge_t);
2023859Sml29623 
2033859Sml29623 static nxge_status_t nxge_map_regs(p_nxge_t nxgep);
2043859Sml29623 static void nxge_unmap_regs(p_nxge_t nxgep);
2053859Sml29623 #ifdef	NXGE_DEBUG
2063859Sml29623 static void nxge_test_map_regs(p_nxge_t nxgep);
2073859Sml29623 #endif
2083859Sml29623 
2093859Sml29623 static nxge_status_t nxge_add_intrs(p_nxge_t nxgep);
2103859Sml29623 static void nxge_remove_intrs(p_nxge_t nxgep);
2113859Sml29623 
2123859Sml29623 static nxge_status_t nxge_add_intrs_adv(p_nxge_t nxgep);
2133859Sml29623 static nxge_status_t nxge_add_intrs_adv_type(p_nxge_t, uint32_t);
2143859Sml29623 static nxge_status_t nxge_add_intrs_adv_type_fix(p_nxge_t, uint32_t);
2153859Sml29623 static void nxge_intrs_enable(p_nxge_t nxgep);
2163859Sml29623 static void nxge_intrs_disable(p_nxge_t nxgep);
2173859Sml29623 
2183859Sml29623 static void nxge_suspend(p_nxge_t);
2193859Sml29623 static nxge_status_t nxge_resume(p_nxge_t);
2203859Sml29623 
2213859Sml29623 static nxge_status_t nxge_setup_dev(p_nxge_t);
2223859Sml29623 static void nxge_destroy_dev(p_nxge_t);
2233859Sml29623 
2243859Sml29623 static nxge_status_t nxge_alloc_mem_pool(p_nxge_t);
2253859Sml29623 static void nxge_free_mem_pool(p_nxge_t);
2263859Sml29623 
2276495Sspeer nxge_status_t nxge_alloc_rx_mem_pool(p_nxge_t);
2283859Sml29623 static void nxge_free_rx_mem_pool(p_nxge_t);
2293859Sml29623 
2306495Sspeer nxge_status_t nxge_alloc_tx_mem_pool(p_nxge_t);
2313859Sml29623 static void nxge_free_tx_mem_pool(p_nxge_t);
2323859Sml29623 
2333859Sml29623 static nxge_status_t nxge_dma_mem_alloc(p_nxge_t, dma_method_t,
2343859Sml29623 	struct ddi_dma_attr *,
2353859Sml29623 	size_t, ddi_device_acc_attr_t *, uint_t,
2363859Sml29623 	p_nxge_dma_common_t);
2373859Sml29623 
2383859Sml29623 static void nxge_dma_mem_free(p_nxge_dma_common_t);
2396495Sspeer static void nxge_dma_free_rx_data_buf(p_nxge_dma_common_t);
2403859Sml29623 
2413859Sml29623 static nxge_status_t nxge_alloc_rx_buf_dma(p_nxge_t, uint16_t,
2423859Sml29623 	p_nxge_dma_common_t *, size_t, size_t, uint32_t *);
2433859Sml29623 static void nxge_free_rx_buf_dma(p_nxge_t, p_nxge_dma_common_t, uint32_t);
2443859Sml29623 
2453859Sml29623 static nxge_status_t nxge_alloc_rx_cntl_dma(p_nxge_t, uint16_t,
2463859Sml29623 	p_nxge_dma_common_t *, size_t);
2473859Sml29623 static void nxge_free_rx_cntl_dma(p_nxge_t, p_nxge_dma_common_t);
2483859Sml29623 
2496495Sspeer extern nxge_status_t nxge_alloc_tx_buf_dma(p_nxge_t, uint16_t,
2503859Sml29623 	p_nxge_dma_common_t *, size_t, size_t, uint32_t *);
2513859Sml29623 static void nxge_free_tx_buf_dma(p_nxge_t, p_nxge_dma_common_t, uint32_t);
2523859Sml29623 
2536495Sspeer extern nxge_status_t nxge_alloc_tx_cntl_dma(p_nxge_t, uint16_t,
2543859Sml29623 	p_nxge_dma_common_t *,
2553859Sml29623 	size_t);
2563859Sml29623 static void nxge_free_tx_cntl_dma(p_nxge_t, p_nxge_dma_common_t);
2573859Sml29623 
2583859Sml29623 static int nxge_init_common_dev(p_nxge_t);
2593859Sml29623 static void nxge_uninit_common_dev(p_nxge_t);
2606512Ssowmini extern int nxge_param_set_mac(p_nxge_t, queue_t *, mblk_t *,
2616512Ssowmini     char *, caddr_t);
2629232SMichael.Speer@Sun.COM #if defined(sun4v)
2639232SMichael.Speer@Sun.COM extern nxge_status_t nxge_hio_rdc_enable(p_nxge_t nxgep);
2649232SMichael.Speer@Sun.COM extern nxge_status_t nxge_hio_rdc_intr_arm(p_nxge_t nxge, boolean_t arm);
2659232SMichael.Speer@Sun.COM #endif
2663859Sml29623 
2673859Sml29623 /*
2683859Sml29623  * The next declarations are for the GLDv3 interface.
2693859Sml29623  */
2703859Sml29623 static int nxge_m_start(void *);
2713859Sml29623 static void nxge_m_stop(void *);
2723859Sml29623 static int nxge_m_multicst(void *, boolean_t, const uint8_t *);
2733859Sml29623 static int nxge_m_promisc(void *, boolean_t);
2743859Sml29623 static void nxge_m_ioctl(void *, queue_t *, mblk_t *);
27510309SSriharsha.Basavapatna@Sun.COM nxge_status_t nxge_mac_register(p_nxge_t);
2768275SEric Cheng static int nxge_altmac_set(p_nxge_t nxgep, uint8_t *mac_addr,
2778275SEric Cheng 	int slot, int rdctbl, boolean_t usetbl);
2788275SEric Cheng void nxge_mmac_kstat_update(p_nxge_t nxgep, int slot,
2793859Sml29623 	boolean_t factory);
2808275SEric Cheng 
2818275SEric Cheng static void nxge_m_getfactaddr(void *, uint_t, uint8_t *);
2826439Sml29623 static	boolean_t nxge_m_getcapab(void *, mac_capab_t, void *);
2836439Sml29623 static int nxge_m_setprop(void *, const char *, mac_prop_id_t,
2846439Sml29623     uint_t, const void *);
2856439Sml29623 static int nxge_m_getprop(void *, const char *, mac_prop_id_t,
2868118SVasumathi.Sundaram@Sun.COM     uint_t, uint_t, void *, uint_t *);
2876439Sml29623 static int nxge_set_priv_prop(nxge_t *, const char *, uint_t,
2886439Sml29623     const void *);
2896512Ssowmini static int nxge_get_priv_prop(nxge_t *, const char *, uint_t, uint_t,
2908118SVasumathi.Sundaram@Sun.COM     void *, uint_t *);
2916512Ssowmini static int nxge_get_def_val(nxge_t *, mac_prop_id_t, uint_t, void *);
2928275SEric Cheng static void nxge_fill_ring(void *, mac_ring_type_t, const int, const int,
2938275SEric Cheng     mac_ring_info_t *, mac_ring_handle_t);
2948275SEric Cheng static void nxge_group_add_ring(mac_group_driver_t, mac_ring_driver_t,
2958275SEric Cheng     mac_ring_type_t);
2968275SEric Cheng static void nxge_group_rem_ring(mac_group_driver_t, mac_ring_driver_t,
2978275SEric Cheng     mac_ring_type_t);
2986512Ssowmini 
2996705Sml29623 static void nxge_niu_peu_reset(p_nxge_t nxgep);
3007126Sml29623 static void nxge_set_pci_replay_timeout(nxge_t *);
3016512Ssowmini 
3026512Ssowmini mac_priv_prop_t nxge_priv_props[] = {
3036512Ssowmini 	{"_adv_10gfdx_cap", MAC_PROP_PERM_RW},
3046512Ssowmini 	{"_adv_pause_cap", MAC_PROP_PERM_RW},
3056512Ssowmini 	{"_function_number", MAC_PROP_PERM_READ},
3066512Ssowmini 	{"_fw_version", MAC_PROP_PERM_READ},
3076512Ssowmini 	{"_port_mode", MAC_PROP_PERM_READ},
3086512Ssowmini 	{"_hot_swap_phy", MAC_PROP_PERM_READ},
3096512Ssowmini 	{"_rxdma_intr_time", MAC_PROP_PERM_RW},
3106512Ssowmini 	{"_rxdma_intr_pkts", MAC_PROP_PERM_RW},
3116512Ssowmini 	{"_class_opt_ipv4_tcp", MAC_PROP_PERM_RW},
3126512Ssowmini 	{"_class_opt_ipv4_udp", MAC_PROP_PERM_RW},
3136512Ssowmini 	{"_class_opt_ipv4_ah", MAC_PROP_PERM_RW},
3146512Ssowmini 	{"_class_opt_ipv4_sctp", MAC_PROP_PERM_RW},
3156512Ssowmini 	{"_class_opt_ipv6_tcp", MAC_PROP_PERM_RW},
3166512Ssowmini 	{"_class_opt_ipv6_udp", MAC_PROP_PERM_RW},
3176512Ssowmini 	{"_class_opt_ipv6_ah", MAC_PROP_PERM_RW},
3186512Ssowmini 	{"_class_opt_ipv6_sctp", MAC_PROP_PERM_RW},
3196512Ssowmini 	{"_soft_lso_enable", MAC_PROP_PERM_RW}
3206512Ssowmini };
3216512Ssowmini 
3226512Ssowmini #define	NXGE_MAX_PRIV_PROPS	\
3236512Ssowmini 	(sizeof (nxge_priv_props)/sizeof (mac_priv_prop_t))
3246439Sml29623 
3253859Sml29623 #define	NXGE_NEPTUNE_MAGIC	0x4E584745UL
3263859Sml29623 #define	MAX_DUMP_SZ 256
3273859Sml29623 
3286439Sml29623 #define	NXGE_M_CALLBACK_FLAGS	\
3298275SEric Cheng 	(MC_IOCTL | MC_GETCAPAB | MC_SETPROP | MC_GETPROP)
3306439Sml29623 
3316495Sspeer mac_callbacks_t nxge_m_callbacks = {
3323859Sml29623 	NXGE_M_CALLBACK_FLAGS,
3333859Sml29623 	nxge_m_stat,
3343859Sml29623 	nxge_m_start,
3353859Sml29623 	nxge_m_stop,
3363859Sml29623 	nxge_m_promisc,
3373859Sml29623 	nxge_m_multicst,
3388275SEric Cheng 	NULL,
3398275SEric Cheng 	NULL,
3403859Sml29623 	nxge_m_ioctl,
3416439Sml29623 	nxge_m_getcapab,
3426439Sml29623 	NULL,
3436439Sml29623 	NULL,
3446439Sml29623 	nxge_m_setprop,
3456439Sml29623 	nxge_m_getprop
3463859Sml29623 };
3473859Sml29623 
3483859Sml29623 void
3493859Sml29623 nxge_err_inject(p_nxge_t, queue_t *, mblk_t *);
3503859Sml29623 
3515013Sml29623 /* PSARC/2007/453 MSI-X interrupt limit override. */
3525013Sml29623 #define	NXGE_MSIX_REQUEST_10G	8
3535013Sml29623 #define	NXGE_MSIX_REQUEST_1G	2
3545013Sml29623 static int nxge_create_msi_property(p_nxge_t);
3558455Stc99174@train /*
3568455Stc99174@train  * For applications that care about the
3578455Stc99174@train  * latency, it was requested by PAE and the
3588455Stc99174@train  * customers that the driver has tunables that
3598455Stc99174@train  * allow the user to tune it to a higher number
3608455Stc99174@train  * interrupts to spread the interrupts among
3618455Stc99174@train  * multiple channels. The DDI framework limits
3628455Stc99174@train  * the maximum number of MSI-X resources to allocate
3638455Stc99174@train  * to 8 (ddi_msix_alloc_limit). If more than 8
3648455Stc99174@train  * is set, ddi_msix_alloc_limit must be set accordingly.
3658455Stc99174@train  * The default number of MSI interrupts are set to
3668455Stc99174@train  * 8 for 10G and 2 for 1G link.
3678455Stc99174@train  */
3688455Stc99174@train #define	NXGE_MSIX_MAX_ALLOWED	32
3698455Stc99174@train uint32_t nxge_msix_10g_intrs = NXGE_MSIX_REQUEST_10G;
3708455Stc99174@train uint32_t nxge_msix_1g_intrs = NXGE_MSIX_REQUEST_1G;
3715013Sml29623 
3723859Sml29623 /*
3733859Sml29623  * These global variables control the message
3743859Sml29623  * output.
3753859Sml29623  */
3763859Sml29623 out_dbgmsg_t nxge_dbgmsg_out = DBG_CONSOLE | STR_LOG;
3776495Sspeer uint64_t nxge_debug_level;
3783859Sml29623 
3793859Sml29623 /*
3803859Sml29623  * This list contains the instance structures for the Neptune
3813859Sml29623  * devices present in the system. The lock exists to guarantee
3823859Sml29623  * mutually exclusive access to the list.
3833859Sml29623  */
3843859Sml29623 void 			*nxge_list = NULL;
3853859Sml29623 void			*nxge_hw_list = NULL;
3863859Sml29623 nxge_os_mutex_t 	nxge_common_lock;
3879935SMichael.Speer@Sun.COM nxge_os_mutex_t 	nxgedebuglock;
3883859Sml29623 
3893859Sml29623 extern uint64_t 	npi_debug_level;
3903859Sml29623 
3913859Sml29623 extern nxge_status_t	nxge_ldgv_init(p_nxge_t, int *, int *);
3923859Sml29623 extern nxge_status_t	nxge_ldgv_init_n2(p_nxge_t, int *, int *);
3933859Sml29623 extern nxge_status_t	nxge_ldgv_uninit(p_nxge_t);
3943859Sml29623 extern nxge_status_t	nxge_intr_ldgv_init(p_nxge_t);
3953859Sml29623 extern void		nxge_fm_init(p_nxge_t,
3963859Sml29623 					ddi_device_acc_attr_t *,
3973859Sml29623 					ddi_device_acc_attr_t *,
3983859Sml29623 					ddi_dma_attr_t *);
3993859Sml29623 extern void		nxge_fm_fini(p_nxge_t);
4003859Sml29623 extern npi_status_t	npi_mac_altaddr_disable(npi_handle_t, uint8_t, uint8_t);
4013859Sml29623 
4023859Sml29623 /*
4033859Sml29623  * Count used to maintain the number of buffers being used
4043859Sml29623  * by Neptune instances and loaned up to the upper layers.
4053859Sml29623  */
4063859Sml29623 uint32_t nxge_mblks_pending = 0;
4073859Sml29623 
4083859Sml29623 /*
4093859Sml29623  * Device register access attributes for PIO.
4103859Sml29623  */
4113859Sml29623 static ddi_device_acc_attr_t nxge_dev_reg_acc_attr = {
4123859Sml29623 	DDI_DEVICE_ATTR_V0,
4133859Sml29623 	DDI_STRUCTURE_LE_ACC,
4143859Sml29623 	DDI_STRICTORDER_ACC,
4153859Sml29623 };
4163859Sml29623 
4173859Sml29623 /*
4183859Sml29623  * Device descriptor access attributes for DMA.
4193859Sml29623  */
4203859Sml29623 static ddi_device_acc_attr_t nxge_dev_desc_dma_acc_attr = {
4213859Sml29623 	DDI_DEVICE_ATTR_V0,
4223859Sml29623 	DDI_STRUCTURE_LE_ACC,
4233859Sml29623 	DDI_STRICTORDER_ACC
4243859Sml29623 };
4253859Sml29623 
4263859Sml29623 /*
4273859Sml29623  * Device buffer access attributes for DMA.
4283859Sml29623  */
4293859Sml29623 static ddi_device_acc_attr_t nxge_dev_buf_dma_acc_attr = {
4303859Sml29623 	DDI_DEVICE_ATTR_V0,
4313859Sml29623 	DDI_STRUCTURE_BE_ACC,
4323859Sml29623 	DDI_STRICTORDER_ACC
4333859Sml29623 };
4343859Sml29623 
4353859Sml29623 ddi_dma_attr_t nxge_desc_dma_attr = {
4363859Sml29623 	DMA_ATTR_V0,		/* version number. */
4373859Sml29623 	0,			/* low address */
4383859Sml29623 	0xffffffffffffffff,	/* high address */
4393859Sml29623 	0xffffffffffffffff,	/* address counter max */
4403859Sml29623 #ifndef NIU_PA_WORKAROUND
4413859Sml29623 	0x100000,		/* alignment */
4423859Sml29623 #else
4433859Sml29623 	0x2000,
4443859Sml29623 #endif
4453859Sml29623 	0xfc00fc,		/* dlim_burstsizes */
4463859Sml29623 	0x1,			/* minimum transfer size */
4473859Sml29623 	0xffffffffffffffff,	/* maximum transfer size */
4483859Sml29623 	0xffffffffffffffff,	/* maximum segment size */
4493859Sml29623 	1,			/* scatter/gather list length */
4503859Sml29623 	(unsigned int) 1,	/* granularity */
4513859Sml29623 	0			/* attribute flags */
4523859Sml29623 };
4533859Sml29623 
4543859Sml29623 ddi_dma_attr_t nxge_tx_dma_attr = {
4553859Sml29623 	DMA_ATTR_V0,		/* version number. */
4563859Sml29623 	0,			/* low address */
4573859Sml29623 	0xffffffffffffffff,	/* high address */
4583859Sml29623 	0xffffffffffffffff,	/* address counter max */
4593859Sml29623 #if defined(_BIG_ENDIAN)
4603859Sml29623 	0x2000,			/* alignment */
4613859Sml29623 #else
4623859Sml29623 	0x1000,			/* alignment */
4633859Sml29623 #endif
4643859Sml29623 	0xfc00fc,		/* dlim_burstsizes */
4653859Sml29623 	0x1,			/* minimum transfer size */
4663859Sml29623 	0xffffffffffffffff,	/* maximum transfer size */
4673859Sml29623 	0xffffffffffffffff,	/* maximum segment size */
4683859Sml29623 	5,			/* scatter/gather list length */
4693859Sml29623 	(unsigned int) 1,	/* granularity */
4703859Sml29623 	0			/* attribute flags */
4713859Sml29623 };
4723859Sml29623 
4733859Sml29623 ddi_dma_attr_t nxge_rx_dma_attr = {
4743859Sml29623 	DMA_ATTR_V0,		/* version number. */
4753859Sml29623 	0,			/* low address */
4763859Sml29623 	0xffffffffffffffff,	/* high address */
4773859Sml29623 	0xffffffffffffffff,	/* address counter max */
4783859Sml29623 	0x2000,			/* alignment */
4793859Sml29623 	0xfc00fc,		/* dlim_burstsizes */
4803859Sml29623 	0x1,			/* minimum transfer size */
4813859Sml29623 	0xffffffffffffffff,	/* maximum transfer size */
4823859Sml29623 	0xffffffffffffffff,	/* maximum segment size */
4833859Sml29623 	1,			/* scatter/gather list length */
4843859Sml29623 	(unsigned int) 1,	/* granularity */
4854781Ssbehera 	DDI_DMA_RELAXED_ORDERING /* attribute flags */
4863859Sml29623 };
4873859Sml29623 
4883859Sml29623 ddi_dma_lim_t nxge_dma_limits = {
4893859Sml29623 	(uint_t)0,		/* dlim_addr_lo */
4903859Sml29623 	(uint_t)0xffffffff,	/* dlim_addr_hi */
4913859Sml29623 	(uint_t)0xffffffff,	/* dlim_cntr_max */
4923859Sml29623 	(uint_t)0xfc00fc,	/* dlim_burstsizes for 32 and 64 bit xfers */
4933859Sml29623 	0x1,			/* dlim_minxfer */
4943859Sml29623 	1024			/* dlim_speed */
4953859Sml29623 };
4963859Sml29623 
4973859Sml29623 dma_method_t nxge_force_dma = DVMA;
4983859Sml29623 
4993859Sml29623 /*
5003859Sml29623  * dma chunk sizes.
5013859Sml29623  *
5023859Sml29623  * Try to allocate the largest possible size
5033859Sml29623  * so that fewer number of dma chunks would be managed
5043859Sml29623  */
5053859Sml29623 #ifdef NIU_PA_WORKAROUND
5063859Sml29623 size_t alloc_sizes [] = {0x2000};
5073859Sml29623 #else
5083859Sml29623 size_t alloc_sizes [] = {0x1000, 0x2000, 0x4000, 0x8000,
5093859Sml29623 		0x10000, 0x20000, 0x40000, 0x80000,
5105770Sml29623 		0x100000, 0x200000, 0x400000, 0x800000,
5115770Sml29623 		0x1000000, 0x2000000, 0x4000000};
5123859Sml29623 #endif
5133859Sml29623 
5143859Sml29623 /*
5153859Sml29623  * Translate "dev_t" to a pointer to the associated "dev_info_t".
5163859Sml29623  */
5173859Sml29623 
5186495Sspeer extern void nxge_get_environs(nxge_t *);
5196495Sspeer 
5203859Sml29623 static int
5213859Sml29623 nxge_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5223859Sml29623 {
5233859Sml29623 	p_nxge_t	nxgep = NULL;
5243859Sml29623 	int		instance;
5253859Sml29623 	int		status = DDI_SUCCESS;
5263859Sml29623 	uint8_t		portn;
5273859Sml29623 	nxge_mmac_t	*mmac_info;
5283859Sml29623 
5293859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_attach"));
5303859Sml29623 
5313859Sml29623 	/*
5323859Sml29623 	 * Get the device instance since we'll need to setup
5333859Sml29623 	 * or retrieve a soft state for this instance.
5343859Sml29623 	 */
5353859Sml29623 	instance = ddi_get_instance(dip);
5363859Sml29623 
5373859Sml29623 	switch (cmd) {
5383859Sml29623 	case DDI_ATTACH:
5393859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_ATTACH"));
5403859Sml29623 		break;
5413859Sml29623 
5423859Sml29623 	case DDI_RESUME:
5433859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_RESUME"));
5443859Sml29623 		nxgep = (p_nxge_t)ddi_get_soft_state(nxge_list, instance);
5453859Sml29623 		if (nxgep == NULL) {
5463859Sml29623 			status = DDI_FAILURE;
5473859Sml29623 			break;
5483859Sml29623 		}
5493859Sml29623 		if (nxgep->dip != dip) {
5503859Sml29623 			status = DDI_FAILURE;
5513859Sml29623 			break;
5523859Sml29623 		}
5533859Sml29623 		if (nxgep->suspended == DDI_PM_SUSPEND) {
5543859Sml29623 			status = ddi_dev_is_needed(nxgep->dip, 0, 1);
5553859Sml29623 		} else {
5564185Sspeer 			status = nxge_resume(nxgep);
5573859Sml29623 		}
5583859Sml29623 		goto nxge_attach_exit;
5593859Sml29623 
5603859Sml29623 	case DDI_PM_RESUME:
5613859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_PM_RESUME"));
5623859Sml29623 		nxgep = (p_nxge_t)ddi_get_soft_state(nxge_list, instance);
5633859Sml29623 		if (nxgep == NULL) {
5643859Sml29623 			status = DDI_FAILURE;
5653859Sml29623 			break;
5663859Sml29623 		}
5673859Sml29623 		if (nxgep->dip != dip) {
5683859Sml29623 			status = DDI_FAILURE;
5693859Sml29623 			break;
5703859Sml29623 		}
5714185Sspeer 		status = nxge_resume(nxgep);
5723859Sml29623 		goto nxge_attach_exit;
5733859Sml29623 
5743859Sml29623 	default:
5753859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing unknown"));
5763859Sml29623 		status = DDI_FAILURE;
5773859Sml29623 		goto nxge_attach_exit;
5783859Sml29623 	}
5793859Sml29623 
5803859Sml29623 
5813859Sml29623 	if (ddi_soft_state_zalloc(nxge_list, instance) == DDI_FAILURE) {
5823859Sml29623 		status = DDI_FAILURE;
5833859Sml29623 		goto nxge_attach_exit;
5843859Sml29623 	}
5853859Sml29623 
5863859Sml29623 	nxgep = ddi_get_soft_state(nxge_list, instance);
5873859Sml29623 	if (nxgep == NULL) {
5884977Sraghus 		status = NXGE_ERROR;
5894977Sraghus 		goto nxge_attach_fail2;
5903859Sml29623 	}
5913859Sml29623 
5924693Stm144005 	nxgep->nxge_magic = NXGE_MAGIC;
5934693Stm144005 
5943859Sml29623 	nxgep->drv_state = 0;
5953859Sml29623 	nxgep->dip = dip;
5963859Sml29623 	nxgep->instance = instance;
5973859Sml29623 	nxgep->p_dip = ddi_get_parent(dip);
5983859Sml29623 	nxgep->nxge_debug_level = nxge_debug_level;
5993859Sml29623 	npi_debug_level = nxge_debug_level;
6003859Sml29623 
6016495Sspeer 	/* Are we a guest running in a Hybrid I/O environment? */
6026495Sspeer 	nxge_get_environs(nxgep);
6033859Sml29623 
6043859Sml29623 	status = nxge_map_regs(nxgep);
6056495Sspeer 
6063859Sml29623 	if (status != NXGE_OK) {
6073859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "nxge_map_regs failed"));
6084977Sraghus 		goto nxge_attach_fail3;
6093859Sml29623 	}
6103859Sml29623 
6116495Sspeer 	nxge_fm_init(nxgep, &nxge_dev_reg_acc_attr,
6126495Sspeer 	    &nxge_dev_desc_dma_acc_attr,
6136495Sspeer 	    &nxge_rx_dma_attr);
6146495Sspeer 
6156495Sspeer 	/* Create & initialize the per-Neptune data structure */
6166495Sspeer 	/* (even if we're a guest). */
6173859Sml29623 	status = nxge_init_common_dev(nxgep);
6183859Sml29623 	if (status != NXGE_OK) {
6193859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
6206512Ssowmini 		    "nxge_init_common_dev failed"));
6214977Sraghus 		goto nxge_attach_fail4;
6223859Sml29623 	}
6233859Sml29623 
6247126Sml29623 	/*
6257126Sml29623 	 * Software workaround: set the replay timer.
6267126Sml29623 	 */
6277126Sml29623 	if (nxgep->niu_type != N2_NIU) {
6287126Sml29623 		nxge_set_pci_replay_timeout(nxgep);
6297126Sml29623 	}
6307126Sml29623 
6316495Sspeer #if defined(sun4v)
6326495Sspeer 	/* This is required by nxge_hio_init(), which follows. */
6336495Sspeer 	if ((status = nxge_hsvc_register(nxgep)) != DDI_SUCCESS)
6347587SMichael.Speer@Sun.COM 		goto nxge_attach_fail4;
6356495Sspeer #endif
6366495Sspeer 
6376495Sspeer 	if ((status = nxge_hio_init(nxgep)) != NXGE_OK) {
6386495Sspeer 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
6396512Ssowmini 		    "nxge_hio_init failed"));
6406495Sspeer 		goto nxge_attach_fail4;
6416495Sspeer 	}
6426495Sspeer 
6434732Sdavemq 	if (nxgep->niu_type == NEPTUNE_2_10GF) {
6444732Sdavemq 		if (nxgep->function_num > 1) {
6456028Ssbehera 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "Unsupported"
6464732Sdavemq 			    " function %d. Only functions 0 and 1 are "
6474732Sdavemq 			    "supported for this card.", nxgep->function_num));
6484732Sdavemq 			status = NXGE_ERROR;
6494977Sraghus 			goto nxge_attach_fail4;
6504732Sdavemq 		}
6514732Sdavemq 	}
6524732Sdavemq 
6536495Sspeer 	if (isLDOMguest(nxgep)) {
6546495Sspeer 		/*
6556495Sspeer 		 * Use the function number here.
6566495Sspeer 		 */
6576495Sspeer 		nxgep->mac.portnum = nxgep->function_num;
6586495Sspeer 		nxgep->mac.porttype = PORT_TYPE_LOGICAL;
6596495Sspeer 
6606495Sspeer 		/* XXX We'll set the MAC address counts to 1 for now. */
6616495Sspeer 		mmac_info = &nxgep->nxge_mmac_info;
6626495Sspeer 		mmac_info->num_mmac = 1;
6636495Sspeer 		mmac_info->naddrfree = 1;
6643859Sml29623 	} else {
6656495Sspeer 		portn = NXGE_GET_PORT_NUM(nxgep->function_num);
6666495Sspeer 		nxgep->mac.portnum = portn;
6676495Sspeer 		if ((portn == 0) || (portn == 1))
6686495Sspeer 			nxgep->mac.porttype = PORT_TYPE_XMAC;
6696495Sspeer 		else
6706495Sspeer 			nxgep->mac.porttype = PORT_TYPE_BMAC;
6716495Sspeer 		/*
6726495Sspeer 		 * Neptune has 4 ports, the first 2 ports use XMAC (10G MAC)
6736495Sspeer 		 * internally, the rest 2 ports use BMAC (1G "Big" MAC).
6746495Sspeer 		 * The two types of MACs have different characterizations.
6756495Sspeer 		 */
6766495Sspeer 		mmac_info = &nxgep->nxge_mmac_info;
6776495Sspeer 		if (nxgep->function_num < 2) {
6786495Sspeer 			mmac_info->num_mmac = XMAC_MAX_ALT_ADDR_ENTRY;
6796495Sspeer 			mmac_info->naddrfree = XMAC_MAX_ALT_ADDR_ENTRY;
6806495Sspeer 		} else {
6816495Sspeer 			mmac_info->num_mmac = BMAC_MAX_ALT_ADDR_ENTRY;
6826495Sspeer 			mmac_info->naddrfree = BMAC_MAX_ALT_ADDR_ENTRY;
6836495Sspeer 		}
6843859Sml29623 	}
6853859Sml29623 	/*
6863859Sml29623 	 * Setup the Ndd parameters for the this instance.
6873859Sml29623 	 */
6883859Sml29623 	nxge_init_param(nxgep);
6893859Sml29623 
6903859Sml29623 	/*
6913859Sml29623 	 * Setup Register Tracing Buffer.
6923859Sml29623 	 */
6933859Sml29623 	npi_rtrace_buf_init((rtrace_t *)&npi_rtracebuf);
6943859Sml29623 
6953859Sml29623 	/* init stats ptr */
6963859Sml29623 	nxge_init_statsp(nxgep);
6974185Sspeer 
6984977Sraghus 	/*
6996495Sspeer 	 * Copy the vpd info from eeprom to a local data
7006495Sspeer 	 * structure, and then check its validity.
7014977Sraghus 	 */
7026495Sspeer 	if (!isLDOMguest(nxgep)) {
7036495Sspeer 		int *regp;
7046495Sspeer 		uint_t reglen;
7056495Sspeer 		int rv;
7066495Sspeer 
7076495Sspeer 		nxge_vpd_info_get(nxgep);
7086495Sspeer 
7096495Sspeer 		/* Find the NIU config handle. */
7106495Sspeer 		rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
7116495Sspeer 		    ddi_get_parent(nxgep->dip), DDI_PROP_DONTPASS,
7126495Sspeer 		    "reg", &regp, &reglen);
7136495Sspeer 
7146495Sspeer 		if (rv != DDI_PROP_SUCCESS) {
7156495Sspeer 			goto nxge_attach_fail5;
7166495Sspeer 		}
7176495Sspeer 		/*
7186495Sspeer 		 * The address_hi, that is the first int, in the reg
7196495Sspeer 		 * property consists of config handle, but need to remove
7206495Sspeer 		 * the bits 28-31 which are OBP specific info.
7216495Sspeer 		 */
7226495Sspeer 		nxgep->niu_cfg_hdl = (*regp) & 0xFFFFFFF;
7236495Sspeer 		ddi_prop_free(regp);
7246495Sspeer 	}
7256495Sspeer 
7269730SMichael.Speer@Sun.COM 	/*
7279730SMichael.Speer@Sun.COM 	 * Set the defaults for the MTU size.
7289730SMichael.Speer@Sun.COM 	 */
7299730SMichael.Speer@Sun.COM 	nxge_hw_id_init(nxgep);
7309730SMichael.Speer@Sun.COM 
7316495Sspeer 	if (isLDOMguest(nxgep)) {
7326495Sspeer 		uchar_t *prop_val;
7336495Sspeer 		uint_t prop_len;
7347529SSriharsha.Basavapatna@Sun.COM 		uint32_t max_frame_size;
7356495Sspeer 
7366495Sspeer 		extern void nxge_get_logical_props(p_nxge_t);
7376495Sspeer 
7386495Sspeer 		nxgep->statsp->mac_stats.xcvr_inuse = LOGICAL_XCVR;
7396495Sspeer 		nxgep->mac.portmode = PORT_LOGICAL;
7406495Sspeer 		(void) ddi_prop_update_string(DDI_DEV_T_NONE, nxgep->dip,
7416495Sspeer 		    "phy-type", "virtual transceiver");
7426495Sspeer 
7436495Sspeer 		nxgep->nports = 1;
7446495Sspeer 		nxgep->board_ver = 0;	/* XXX What? */
7456495Sspeer 
7466495Sspeer 		/*
7476495Sspeer 		 * local-mac-address property gives us info on which
7486495Sspeer 		 * specific MAC address the Hybrid resource is associated
7496495Sspeer 		 * with.
7506495Sspeer 		 */
7516495Sspeer 		if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, nxgep->dip, 0,
7526495Sspeer 		    "local-mac-address", &prop_val,
7536495Sspeer 		    &prop_len) != DDI_PROP_SUCCESS) {
7546495Sspeer 			goto nxge_attach_fail5;
7556495Sspeer 		}
7566495Sspeer 		if (prop_len !=  ETHERADDRL) {
7576495Sspeer 			ddi_prop_free(prop_val);
7586495Sspeer 			goto nxge_attach_fail5;
7596495Sspeer 		}
7606495Sspeer 		ether_copy(prop_val, nxgep->hio_mac_addr);
7616495Sspeer 		ddi_prop_free(prop_val);
7626495Sspeer 		nxge_get_logical_props(nxgep);
7636495Sspeer 
7647529SSriharsha.Basavapatna@Sun.COM 		/*
7657529SSriharsha.Basavapatna@Sun.COM 		 * Enable Jumbo property based on the "max-frame-size"
7667529SSriharsha.Basavapatna@Sun.COM 		 * property value.
7677529SSriharsha.Basavapatna@Sun.COM 		 */
7687529SSriharsha.Basavapatna@Sun.COM 		max_frame_size = ddi_prop_get_int(DDI_DEV_T_ANY,
7697529SSriharsha.Basavapatna@Sun.COM 		    nxgep->dip, DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
7707529SSriharsha.Basavapatna@Sun.COM 		    "max-frame-size", NXGE_MTU_DEFAULT_MAX);
7717529SSriharsha.Basavapatna@Sun.COM 		if ((max_frame_size > NXGE_MTU_DEFAULT_MAX) &&
7727529SSriharsha.Basavapatna@Sun.COM 		    (max_frame_size <= TX_JUMBO_MTU)) {
7737529SSriharsha.Basavapatna@Sun.COM 			nxgep->mac.is_jumbo = B_TRUE;
7747529SSriharsha.Basavapatna@Sun.COM 			nxgep->mac.maxframesize = (uint16_t)max_frame_size;
7757529SSriharsha.Basavapatna@Sun.COM 			nxgep->mac.default_mtu = nxgep->mac.maxframesize -
7767529SSriharsha.Basavapatna@Sun.COM 			    NXGE_EHEADER_VLAN_CRC;
7777529SSriharsha.Basavapatna@Sun.COM 		}
7786495Sspeer 	} else {
7796495Sspeer 		status = nxge_xcvr_find(nxgep);
7806495Sspeer 
7816495Sspeer 		if (status != NXGE_OK) {
7826495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "nxge_attach: "
7836512Ssowmini 			    " Couldn't determine card type"
7846512Ssowmini 			    " .... exit "));
7856495Sspeer 			goto nxge_attach_fail5;
7866495Sspeer 		}
7876495Sspeer 
7886495Sspeer 		status = nxge_get_config_properties(nxgep);
7896495Sspeer 
7906495Sspeer 		if (status != NXGE_OK) {
7916495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
7926512Ssowmini 			    "get_hw create failed"));
7936495Sspeer 			goto nxge_attach_fail;
7946495Sspeer 		}
7953859Sml29623 	}
7963859Sml29623 
7973859Sml29623 	/*
7983859Sml29623 	 * Setup the Kstats for the driver.
7993859Sml29623 	 */
8003859Sml29623 	nxge_setup_kstats(nxgep);
8013859Sml29623 
8026495Sspeer 	if (!isLDOMguest(nxgep))
8036495Sspeer 		nxge_setup_param(nxgep);
8043859Sml29623 
8053859Sml29623 	status = nxge_setup_system_dma_pages(nxgep);
8063859Sml29623 	if (status != NXGE_OK) {
8073859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "set dma page failed"));
8083859Sml29623 		goto nxge_attach_fail;
8093859Sml29623 	}
8103859Sml29623 
8116495Sspeer 
8126495Sspeer 	if (!isLDOMguest(nxgep))
8136495Sspeer 		nxge_hw_init_niu_common(nxgep);
8143859Sml29623 
8153859Sml29623 	status = nxge_setup_mutexes(nxgep);
8163859Sml29623 	if (status != NXGE_OK) {
8173859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "set mutex failed"));
8183859Sml29623 		goto nxge_attach_fail;
8193859Sml29623 	}
8203859Sml29623 
8216495Sspeer #if defined(sun4v)
8226495Sspeer 	if (isLDOMguest(nxgep)) {
8236495Sspeer 		/* Find our VR & channel sets. */
8246495Sspeer 		status = nxge_hio_vr_add(nxgep);
8257812SMichael.Speer@Sun.COM 		if (status != NXGE_OK) {
8267812SMichael.Speer@Sun.COM 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
8277812SMichael.Speer@Sun.COM 			    "nxge_hio_vr_add failed"));
8287812SMichael.Speer@Sun.COM 			(void) hsvc_unregister(&nxgep->niu_hsvc);
8297812SMichael.Speer@Sun.COM 			nxgep->niu_hsvc_available = B_FALSE;
8307812SMichael.Speer@Sun.COM 		}
8316495Sspeer 		goto nxge_attach_exit;
8326495Sspeer 	}
8336495Sspeer #endif
8346495Sspeer 
8353859Sml29623 	status = nxge_setup_dev(nxgep);
8363859Sml29623 	if (status != DDI_SUCCESS) {
8373859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "set dev failed"));
8383859Sml29623 		goto nxge_attach_fail;
8393859Sml29623 	}
8403859Sml29623 
8413859Sml29623 	status = nxge_add_intrs(nxgep);
8423859Sml29623 	if (status != DDI_SUCCESS) {
8433859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "add_intr failed"));
8443859Sml29623 		goto nxge_attach_fail;
8453859Sml29623 	}
8467812SMichael.Speer@Sun.COM 
8476835Syc148097 	/* If a guest, register with vio_net instead. */
8484977Sraghus 	if ((status = nxge_mac_register(nxgep)) != NXGE_OK) {
8493859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
8506495Sspeer 		    "unable to register to mac layer (%d)", status));
8513859Sml29623 		goto nxge_attach_fail;
8523859Sml29623 	}
8533859Sml29623 
8543859Sml29623 	mac_link_update(nxgep->mach, LINK_STATE_UNKNOWN);
8553859Sml29623 
8566495Sspeer 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
8576495Sspeer 	    "registered to mac (instance %d)", instance));
8583859Sml29623 
8596835Syc148097 	/* nxge_link_monitor calls xcvr.check_link recursively */
8603859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_START);
8613859Sml29623 
8623859Sml29623 	goto nxge_attach_exit;
8633859Sml29623 
8643859Sml29623 nxge_attach_fail:
8653859Sml29623 	nxge_unattach(nxgep);
8664977Sraghus 	goto nxge_attach_fail1;
8674977Sraghus 
8684977Sraghus nxge_attach_fail5:
8694977Sraghus 	/*
8704977Sraghus 	 * Tear down the ndd parameters setup.
8714977Sraghus 	 */
8724977Sraghus 	nxge_destroy_param(nxgep);
8734977Sraghus 
8744977Sraghus 	/*
8754977Sraghus 	 * Tear down the kstat setup.
8764977Sraghus 	 */
8774977Sraghus 	nxge_destroy_kstats(nxgep);
8784977Sraghus 
8794977Sraghus nxge_attach_fail4:
8804977Sraghus 	if (nxgep->nxge_hw_p) {
8814977Sraghus 		nxge_uninit_common_dev(nxgep);
8824977Sraghus 		nxgep->nxge_hw_p = NULL;
8834977Sraghus 	}
8844977Sraghus 
8854977Sraghus nxge_attach_fail3:
8864977Sraghus 	/*
8874977Sraghus 	 * Unmap the register setup.
8884977Sraghus 	 */
8894977Sraghus 	nxge_unmap_regs(nxgep);
8904977Sraghus 
8914977Sraghus 	nxge_fm_fini(nxgep);
8924977Sraghus 
8934977Sraghus nxge_attach_fail2:
8944977Sraghus 	ddi_soft_state_free(nxge_list, nxgep->instance);
8954977Sraghus 
8964977Sraghus nxge_attach_fail1:
8974185Sspeer 	if (status != NXGE_OK)
8984185Sspeer 		status = (NXGE_ERROR | NXGE_DDI_FAILED);
8993859Sml29623 	nxgep = NULL;
9003859Sml29623 
9013859Sml29623 nxge_attach_exit:
9023859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_attach status = 0x%08x",
9036512Ssowmini 	    status));
9043859Sml29623 
9053859Sml29623 	return (status);
9063859Sml29623 }
9073859Sml29623 
9083859Sml29623 static int
9093859Sml29623 nxge_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
9103859Sml29623 {
9113859Sml29623 	int 		status = DDI_SUCCESS;
9123859Sml29623 	int 		instance;
9133859Sml29623 	p_nxge_t 	nxgep = NULL;
9143859Sml29623 
9153859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_detach"));
9163859Sml29623 	instance = ddi_get_instance(dip);
9173859Sml29623 	nxgep = ddi_get_soft_state(nxge_list, instance);
9183859Sml29623 	if (nxgep == NULL) {
9193859Sml29623 		status = DDI_FAILURE;
9203859Sml29623 		goto nxge_detach_exit;
9213859Sml29623 	}
9223859Sml29623 
9233859Sml29623 	switch (cmd) {
9243859Sml29623 	case DDI_DETACH:
9253859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_DETACH"));
9263859Sml29623 		break;
9273859Sml29623 
9283859Sml29623 	case DDI_PM_SUSPEND:
9293859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_PM_SUSPEND"));
9303859Sml29623 		nxgep->suspended = DDI_PM_SUSPEND;
9313859Sml29623 		nxge_suspend(nxgep);
9323859Sml29623 		break;
9333859Sml29623 
9343859Sml29623 	case DDI_SUSPEND:
9353859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_SUSPEND"));
9363859Sml29623 		if (nxgep->suspended != DDI_PM_SUSPEND) {
9373859Sml29623 			nxgep->suspended = DDI_SUSPEND;
9383859Sml29623 			nxge_suspend(nxgep);
9393859Sml29623 		}
9403859Sml29623 		break;
9413859Sml29623 
9423859Sml29623 	default:
9433859Sml29623 		status = DDI_FAILURE;
9443859Sml29623 	}
9453859Sml29623 
9463859Sml29623 	if (cmd != DDI_DETACH)
9473859Sml29623 		goto nxge_detach_exit;
9483859Sml29623 
9493859Sml29623 	/*
9503859Sml29623 	 * Stop the xcvr polling.
9513859Sml29623 	 */
9523859Sml29623 	nxgep->suspended = cmd;
9533859Sml29623 
9543859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_STOP);
9553859Sml29623 
95610309SSriharsha.Basavapatna@Sun.COM 	if (nxgep->mach && (status = mac_unregister(nxgep->mach)) != 0) {
9573859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
9586512Ssowmini 		    "<== nxge_detach status = 0x%08X", status));
9593859Sml29623 		return (DDI_FAILURE);
9603859Sml29623 	}
9613859Sml29623 
9623859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
9636512Ssowmini 	    "<== nxge_detach (mac_unregister) status = 0x%08X", status));
9643859Sml29623 
9653859Sml29623 	nxge_unattach(nxgep);
9663859Sml29623 	nxgep = NULL;
9673859Sml29623 
9683859Sml29623 nxge_detach_exit:
9693859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_detach status = 0x%08X",
9706512Ssowmini 	    status));
9713859Sml29623 
9723859Sml29623 	return (status);
9733859Sml29623 }
9743859Sml29623 
9753859Sml29623 static void
9763859Sml29623 nxge_unattach(p_nxge_t nxgep)
9773859Sml29623 {
9783859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_unattach"));
9793859Sml29623 
9803859Sml29623 	if (nxgep == NULL || nxgep->dev_regs == NULL) {
9813859Sml29623 		return;
9823859Sml29623 	}
9833859Sml29623 
9844693Stm144005 	nxgep->nxge_magic = 0;
9854693Stm144005 
9865780Ssbehera 	if (nxgep->nxge_timerid) {
9875780Ssbehera 		nxge_stop_timer(nxgep, nxgep->nxge_timerid);
9885780Ssbehera 		nxgep->nxge_timerid = 0;
9895780Ssbehera 	}
9905780Ssbehera 
9916705Sml29623 	/*
9926705Sml29623 	 * If this flag is set, it will affect the Neptune
9936705Sml29623 	 * only.
9946705Sml29623 	 */
9956705Sml29623 	if ((nxgep->niu_type != N2_NIU) && nxge_peu_reset_enable) {
9966705Sml29623 		nxge_niu_peu_reset(nxgep);
9976705Sml29623 	}
9986705Sml29623 
9996495Sspeer #if	defined(sun4v)
10006495Sspeer 	if (isLDOMguest(nxgep)) {
10016498Sspeer 		(void) nxge_hio_vr_release(nxgep);
10026495Sspeer 	}
10036495Sspeer #endif
10046495Sspeer 
10053859Sml29623 	if (nxgep->nxge_hw_p) {
10063859Sml29623 		nxge_uninit_common_dev(nxgep);
10073859Sml29623 		nxgep->nxge_hw_p = NULL;
10083859Sml29623 	}
10093859Sml29623 
10103859Sml29623 #if	defined(sun4v)
10113859Sml29623 	if (nxgep->niu_type == N2_NIU && nxgep->niu_hsvc_available == B_TRUE) {
10123859Sml29623 		(void) hsvc_unregister(&nxgep->niu_hsvc);
10133859Sml29623 		nxgep->niu_hsvc_available = B_FALSE;
10143859Sml29623 	}
10153859Sml29623 #endif
10163859Sml29623 	/*
10173859Sml29623 	 * Stop any further interrupts.
10183859Sml29623 	 */
10193859Sml29623 	nxge_remove_intrs(nxgep);
10203859Sml29623 
10213859Sml29623 	/*
10223859Sml29623 	 * Stop the device and free resources.
10233859Sml29623 	 */
10246495Sspeer 	if (!isLDOMguest(nxgep)) {
10256495Sspeer 		nxge_destroy_dev(nxgep);
10266495Sspeer 	}
10273859Sml29623 
10283859Sml29623 	/*
10293859Sml29623 	 * Tear down the ndd parameters setup.
10303859Sml29623 	 */
10313859Sml29623 	nxge_destroy_param(nxgep);
10323859Sml29623 
10333859Sml29623 	/*
10343859Sml29623 	 * Tear down the kstat setup.
10353859Sml29623 	 */
10363859Sml29623 	nxge_destroy_kstats(nxgep);
10373859Sml29623 
10383859Sml29623 	/*
10393859Sml29623 	 * Destroy all mutexes.
10403859Sml29623 	 */
10413859Sml29623 	nxge_destroy_mutexes(nxgep);
10423859Sml29623 
10433859Sml29623 	/*
10443859Sml29623 	 * Remove the list of ndd parameters which
10453859Sml29623 	 * were setup during attach.
10463859Sml29623 	 */
10473859Sml29623 	if (nxgep->dip) {
10483859Sml29623 		NXGE_DEBUG_MSG((nxgep, OBP_CTL,
10496512Ssowmini 		    " nxge_unattach: remove all properties"));
10503859Sml29623 
10513859Sml29623 		(void) ddi_prop_remove_all(nxgep->dip);
10523859Sml29623 	}
10533859Sml29623 
10543859Sml29623 #if NXGE_PROPERTY
10553859Sml29623 	nxge_remove_hard_properties(nxgep);
10563859Sml29623 #endif
10573859Sml29623 
10583859Sml29623 	/*
10593859Sml29623 	 * Unmap the register setup.
10603859Sml29623 	 */
10613859Sml29623 	nxge_unmap_regs(nxgep);
10623859Sml29623 
10633859Sml29623 	nxge_fm_fini(nxgep);
10643859Sml29623 
10653859Sml29623 	ddi_soft_state_free(nxge_list, nxgep->instance);
10663859Sml29623 
10673859Sml29623 	NXGE_DEBUG_MSG((NULL, DDI_CTL, "<== nxge_unattach"));
10683859Sml29623 }
10693859Sml29623 
10706495Sspeer #if defined(sun4v)
10716495Sspeer int
10727587SMichael.Speer@Sun.COM nxge_hsvc_register(nxge_t *nxgep)
10736495Sspeer {
10746495Sspeer 	nxge_status_t status;
10756495Sspeer 
10766495Sspeer 	if (nxgep->niu_type == N2_NIU) {
10776495Sspeer 		nxgep->niu_hsvc_available = B_FALSE;
10786495Sspeer 		bcopy(&niu_hsvc, &nxgep->niu_hsvc, sizeof (hsvc_info_t));
10796495Sspeer 		if ((status = hsvc_register(&nxgep->niu_hsvc,
10806495Sspeer 		    &nxgep->niu_min_ver)) != 0) {
10816495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
10826495Sspeer 			    "nxge_attach: %s: cannot negotiate "
10836495Sspeer 			    "hypervisor services revision %d group: 0x%lx "
10846495Sspeer 			    "major: 0x%lx minor: 0x%lx errno: %d",
10856495Sspeer 			    niu_hsvc.hsvc_modname, niu_hsvc.hsvc_rev,
10866495Sspeer 			    niu_hsvc.hsvc_group, niu_hsvc.hsvc_major,
10876495Sspeer 			    niu_hsvc.hsvc_minor, status));
10886495Sspeer 			return (DDI_FAILURE);
10896495Sspeer 		}
10906495Sspeer 		nxgep->niu_hsvc_available = B_TRUE;
10916495Sspeer 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
10926512Ssowmini 		    "NIU Hypervisor service enabled"));
10936495Sspeer 	}
10946495Sspeer 
10956495Sspeer 	return (DDI_SUCCESS);
10966495Sspeer }
10976495Sspeer #endif
10986495Sspeer 
10993859Sml29623 static char n2_siu_name[] = "niu";
11003859Sml29623 
11013859Sml29623 static nxge_status_t
11023859Sml29623 nxge_map_regs(p_nxge_t nxgep)
11033859Sml29623 {
11043859Sml29623 	int		ddi_status = DDI_SUCCESS;
11053859Sml29623 	p_dev_regs_t 	dev_regs;
11063859Sml29623 	char		buf[MAXPATHLEN + 1];
11073859Sml29623 	char 		*devname;
11083859Sml29623 #ifdef	NXGE_DEBUG
11093859Sml29623 	char 		*sysname;
11103859Sml29623 #endif
11113859Sml29623 	off_t		regsize;
11123859Sml29623 	nxge_status_t	status = NXGE_OK;
11133859Sml29623 #if !defined(_BIG_ENDIAN)
11143859Sml29623 	off_t pci_offset;
11153859Sml29623 	uint16_t pcie_devctl;
11163859Sml29623 #endif
11173859Sml29623 
11186495Sspeer 	if (isLDOMguest(nxgep)) {
11196495Sspeer 		return (nxge_guest_regs_map(nxgep));
11206495Sspeer 	}
11216495Sspeer 
11223859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_map_regs"));
11233859Sml29623 	nxgep->dev_regs = NULL;
11243859Sml29623 	dev_regs = KMEM_ZALLOC(sizeof (dev_regs_t), KM_SLEEP);
11253859Sml29623 	dev_regs->nxge_regh = NULL;
11263859Sml29623 	dev_regs->nxge_pciregh = NULL;
11273859Sml29623 	dev_regs->nxge_msix_regh = NULL;
11283859Sml29623 	dev_regs->nxge_vir_regh = NULL;
11293859Sml29623 	dev_regs->nxge_vir2_regh = NULL;
11304732Sdavemq 	nxgep->niu_type = NIU_TYPE_NONE;
11313859Sml29623 
11323859Sml29623 	devname = ddi_pathname(nxgep->dip, buf);
11333859Sml29623 	ASSERT(strlen(devname) > 0);
11343859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11356512Ssowmini 	    "nxge_map_regs: pathname devname %s", devname));
11363859Sml29623 
11376835Syc148097 	/*
11386835Syc148097 	 * The driver is running on a N2-NIU system if devname is something
11396835Syc148097 	 * like "/niu@80/network@0"
11406835Syc148097 	 */
11413859Sml29623 	if (strstr(devname, n2_siu_name)) {
11423859Sml29623 		/* N2/NIU */
11433859Sml29623 		nxgep->niu_type = N2_NIU;
11443859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11456512Ssowmini 		    "nxge_map_regs: N2/NIU devname %s", devname));
11463859Sml29623 		/* get function number */
11473859Sml29623 		nxgep->function_num =
11486512Ssowmini 		    (devname[strlen(devname) -1] == '1' ? 1 : 0);
11493859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11506512Ssowmini 		    "nxge_map_regs: N2/NIU function number %d",
11516512Ssowmini 		    nxgep->function_num));
11523859Sml29623 	} else {
11533859Sml29623 		int		*prop_val;
11543859Sml29623 		uint_t 		prop_len;
11553859Sml29623 		uint8_t 	func_num;
11563859Sml29623 
11573859Sml29623 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, nxgep->dip,
11586512Ssowmini 		    0, "reg",
11596512Ssowmini 		    &prop_val, &prop_len) != DDI_PROP_SUCCESS) {
11603859Sml29623 			NXGE_DEBUG_MSG((nxgep, VPD_CTL,
11616512Ssowmini 			    "Reg property not found"));
11623859Sml29623 			ddi_status = DDI_FAILURE;
11633859Sml29623 			goto nxge_map_regs_fail0;
11643859Sml29623 
11653859Sml29623 		} else {
11663859Sml29623 			func_num = (prop_val[0] >> 8) & 0x7;
11673859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11686512Ssowmini 			    "Reg property found: fun # %d",
11696512Ssowmini 			    func_num));
11703859Sml29623 			nxgep->function_num = func_num;
11716495Sspeer 			if (isLDOMguest(nxgep)) {
11726495Sspeer 				nxgep->function_num /= 2;
11736495Sspeer 				return (NXGE_OK);
11746495Sspeer 			}
11753859Sml29623 			ddi_prop_free(prop_val);
11763859Sml29623 		}
11773859Sml29623 	}
11783859Sml29623 
11793859Sml29623 	switch (nxgep->niu_type) {
11803859Sml29623 	default:
11813859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 0, &regsize);
11823859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11836512Ssowmini 		    "nxge_map_regs: pci config size 0x%x", regsize));
11843859Sml29623 
11853859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 0,
11866512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_pciregp), 0, 0,
11876512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_pciregh);
11883859Sml29623 		if (ddi_status != DDI_SUCCESS) {
11893859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
11906512Ssowmini 			    "ddi_map_regs, nxge bus config regs failed"));
11913859Sml29623 			goto nxge_map_regs_fail0;
11923859Sml29623 		}
11933859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11946512Ssowmini 		    "nxge_map_reg: PCI config addr 0x%0llx "
11956512Ssowmini 		    " handle 0x%0llx", dev_regs->nxge_pciregp,
11966512Ssowmini 		    dev_regs->nxge_pciregh));
11973859Sml29623 			/*
11983859Sml29623 			 * IMP IMP
11993859Sml29623 			 * workaround  for bit swapping bug in HW
12003859Sml29623 			 * which ends up in no-snoop = yes
12013859Sml29623 			 * resulting, in DMA not synched properly
12023859Sml29623 			 */
12033859Sml29623 #if !defined(_BIG_ENDIAN)
12043859Sml29623 		/* workarounds for x86 systems */
12053859Sml29623 		pci_offset = 0x80 + PCIE_DEVCTL;
12069730SMichael.Speer@Sun.COM 		pcie_devctl = pci_config_get16(dev_regs->nxge_pciregh,
12079730SMichael.Speer@Sun.COM 		    pci_offset);
12089730SMichael.Speer@Sun.COM 		pcie_devctl &= ~PCIE_DEVCTL_ENABLE_NO_SNOOP;
12093859Sml29623 		pcie_devctl |= PCIE_DEVCTL_RO_EN;
12103859Sml29623 		pci_config_put16(dev_regs->nxge_pciregh, pci_offset,
12116512Ssowmini 		    pcie_devctl);
12123859Sml29623 #endif
12133859Sml29623 
12143859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 1, &regsize);
12153859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12166512Ssowmini 		    "nxge_map_regs: pio size 0x%x", regsize));
12173859Sml29623 		/* set up the device mapped register */
12183859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 1,
12196512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_regp), 0, 0,
12206512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_regh);
12213859Sml29623 		if (ddi_status != DDI_SUCCESS) {
12223859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
12236512Ssowmini 			    "ddi_map_regs for Neptune global reg failed"));
12243859Sml29623 			goto nxge_map_regs_fail1;
12253859Sml29623 		}
12263859Sml29623 
12273859Sml29623 		/* set up the msi/msi-x mapped register */
12283859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 2, &regsize);
12293859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12306512Ssowmini 		    "nxge_map_regs: msix size 0x%x", regsize));
12313859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 2,
12326512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_msix_regp), 0, 0,
12336512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_msix_regh);
12343859Sml29623 		if (ddi_status != DDI_SUCCESS) {
12353859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
12366512Ssowmini 			    "ddi_map_regs for msi reg failed"));
12373859Sml29623 			goto nxge_map_regs_fail2;
12383859Sml29623 		}
12393859Sml29623 
12403859Sml29623 		/* set up the vio region mapped register */
12413859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 3, &regsize);
12423859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12436512Ssowmini 		    "nxge_map_regs: vio size 0x%x", regsize));
12443859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 3,
12456512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_vir_regp), 0, 0,
12466512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_vir_regh);
12473859Sml29623 
12483859Sml29623 		if (ddi_status != DDI_SUCCESS) {
12493859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
12506512Ssowmini 			    "ddi_map_regs for nxge vio reg failed"));
12513859Sml29623 			goto nxge_map_regs_fail3;
12523859Sml29623 		}
12533859Sml29623 		nxgep->dev_regs = dev_regs;
12543859Sml29623 
12553859Sml29623 		NPI_PCI_ACC_HANDLE_SET(nxgep, dev_regs->nxge_pciregh);
12563859Sml29623 		NPI_PCI_ADD_HANDLE_SET(nxgep,
12576512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_pciregp);
12583859Sml29623 		NPI_MSI_ACC_HANDLE_SET(nxgep, dev_regs->nxge_msix_regh);
12593859Sml29623 		NPI_MSI_ADD_HANDLE_SET(nxgep,
12606512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_msix_regp);
12613859Sml29623 
12623859Sml29623 		NPI_ACC_HANDLE_SET(nxgep, dev_regs->nxge_regh);
12633859Sml29623 		NPI_ADD_HANDLE_SET(nxgep, (npi_reg_ptr_t)dev_regs->nxge_regp);
12643859Sml29623 
12653859Sml29623 		NPI_REG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_regh);
12663859Sml29623 		NPI_REG_ADD_HANDLE_SET(nxgep,
12676512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_regp);
12683859Sml29623 
12693859Sml29623 		NPI_VREG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_vir_regh);
12703859Sml29623 		NPI_VREG_ADD_HANDLE_SET(nxgep,
12716512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_vir_regp);
12723859Sml29623 
12733859Sml29623 		break;
12743859Sml29623 
12753859Sml29623 	case N2_NIU:
12763859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "ddi_map_regs, NIU"));
12773859Sml29623 		/*
12783859Sml29623 		 * Set up the device mapped register (FWARC 2006/556)
12793859Sml29623 		 * (changed back to 1: reg starts at 1!)
12803859Sml29623 		 */
12813859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 1, &regsize);
12823859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12836512Ssowmini 		    "nxge_map_regs: dev size 0x%x", regsize));
12843859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 1,
12856512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_regp), 0, 0,
12866512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_regh);
12873859Sml29623 
12883859Sml29623 		if (ddi_status != DDI_SUCCESS) {
12893859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
12906512Ssowmini 			    "ddi_map_regs for N2/NIU, global reg failed "));
12913859Sml29623 			goto nxge_map_regs_fail1;
12923859Sml29623 		}
12933859Sml29623 
12946495Sspeer 		/* set up the first vio region mapped register */
12953859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 2, &regsize);
12963859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12976512Ssowmini 		    "nxge_map_regs: vio (1) size 0x%x", regsize));
12983859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 2,
12996512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_vir_regp), 0, 0,
13006512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_vir_regh);
13013859Sml29623 
13023859Sml29623 		if (ddi_status != DDI_SUCCESS) {
13033859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
13046512Ssowmini 			    "ddi_map_regs for nxge vio reg failed"));
13053859Sml29623 			goto nxge_map_regs_fail2;
13063859Sml29623 		}
13076495Sspeer 		/* set up the second vio region mapped register */
13083859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 3, &regsize);
13093859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
13106512Ssowmini 		    "nxge_map_regs: vio (3) size 0x%x", regsize));
13113859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 3,
13126512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_vir2_regp), 0, 0,
13136512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_vir2_regh);
13143859Sml29623 
13153859Sml29623 		if (ddi_status != DDI_SUCCESS) {
13163859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
13176512Ssowmini 			    "ddi_map_regs for nxge vio2 reg failed"));
13183859Sml29623 			goto nxge_map_regs_fail3;
13193859Sml29623 		}
13203859Sml29623 		nxgep->dev_regs = dev_regs;
13213859Sml29623 
13223859Sml29623 		NPI_ACC_HANDLE_SET(nxgep, dev_regs->nxge_regh);
13233859Sml29623 		NPI_ADD_HANDLE_SET(nxgep, (npi_reg_ptr_t)dev_regs->nxge_regp);
13243859Sml29623 
13253859Sml29623 		NPI_REG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_regh);
13263859Sml29623 		NPI_REG_ADD_HANDLE_SET(nxgep,
13276512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_regp);
13283859Sml29623 
13293859Sml29623 		NPI_VREG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_vir_regh);
13303859Sml29623 		NPI_VREG_ADD_HANDLE_SET(nxgep,
13316512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_vir_regp);
13323859Sml29623 
13333859Sml29623 		NPI_V2REG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_vir2_regh);
13343859Sml29623 		NPI_V2REG_ADD_HANDLE_SET(nxgep,
13356512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_vir2_regp);
13363859Sml29623 
13373859Sml29623 		break;
13383859Sml29623 	}
13393859Sml29623 
13403859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "nxge_map_reg: hardware addr 0x%0llx "
13416512Ssowmini 	    " handle 0x%0llx", dev_regs->nxge_regp, dev_regs->nxge_regh));
13423859Sml29623 
13433859Sml29623 	goto nxge_map_regs_exit;
13443859Sml29623 nxge_map_regs_fail3:
13453859Sml29623 	if (dev_regs->nxge_msix_regh) {
13463859Sml29623 		ddi_regs_map_free(&dev_regs->nxge_msix_regh);
13473859Sml29623 	}
13483859Sml29623 	if (dev_regs->nxge_vir_regh) {
13493859Sml29623 		ddi_regs_map_free(&dev_regs->nxge_regh);
13503859Sml29623 	}
13513859Sml29623 nxge_map_regs_fail2:
13523859Sml29623 	if (dev_regs->nxge_regh) {
13533859Sml29623 		ddi_regs_map_free(&dev_regs->nxge_regh);
13543859Sml29623 	}
13553859Sml29623 nxge_map_regs_fail1:
13563859Sml29623 	if (dev_regs->nxge_pciregh) {
13573859Sml29623 		ddi_regs_map_free(&dev_regs->nxge_pciregh);
13583859Sml29623 	}
13593859Sml29623 nxge_map_regs_fail0:
13603859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "Freeing register set memory"));
13613859Sml29623 	kmem_free(dev_regs, sizeof (dev_regs_t));
13623859Sml29623 
13633859Sml29623 nxge_map_regs_exit:
13643859Sml29623 	if (ddi_status != DDI_SUCCESS)
13653859Sml29623 		status |= (NXGE_ERROR | NXGE_DDI_FAILED);
13663859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_map_regs"));
13673859Sml29623 	return (status);
13683859Sml29623 }
13693859Sml29623 
13703859Sml29623 static void
13713859Sml29623 nxge_unmap_regs(p_nxge_t nxgep)
13723859Sml29623 {
13733859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_unmap_regs"));
13746495Sspeer 
13756495Sspeer 	if (isLDOMguest(nxgep)) {
13766495Sspeer 		nxge_guest_regs_map_free(nxgep);
13776495Sspeer 		return;
13786495Sspeer 	}
13796495Sspeer 
13803859Sml29623 	if (nxgep->dev_regs) {
13813859Sml29623 		if (nxgep->dev_regs->nxge_pciregh) {
13823859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
13836512Ssowmini 			    "==> nxge_unmap_regs: bus"));
13843859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_pciregh);
13853859Sml29623 			nxgep->dev_regs->nxge_pciregh = NULL;
13863859Sml29623 		}
13873859Sml29623 		if (nxgep->dev_regs->nxge_regh) {
13883859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
13896512Ssowmini 			    "==> nxge_unmap_regs: device registers"));
13903859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_regh);
13913859Sml29623 			nxgep->dev_regs->nxge_regh = NULL;
13923859Sml29623 		}
13933859Sml29623 		if (nxgep->dev_regs->nxge_msix_regh) {
13943859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
13956512Ssowmini 			    "==> nxge_unmap_regs: device interrupts"));
13963859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_msix_regh);
13973859Sml29623 			nxgep->dev_regs->nxge_msix_regh = NULL;
13983859Sml29623 		}
13993859Sml29623 		if (nxgep->dev_regs->nxge_vir_regh) {
14003859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
14016512Ssowmini 			    "==> nxge_unmap_regs: vio region"));
14023859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_vir_regh);
14033859Sml29623 			nxgep->dev_regs->nxge_vir_regh = NULL;
14043859Sml29623 		}
14053859Sml29623 		if (nxgep->dev_regs->nxge_vir2_regh) {
14063859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
14076512Ssowmini 			    "==> nxge_unmap_regs: vio2 region"));
14083859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_vir2_regh);
14093859Sml29623 			nxgep->dev_regs->nxge_vir2_regh = NULL;
14103859Sml29623 		}
14113859Sml29623 
14123859Sml29623 		kmem_free(nxgep->dev_regs, sizeof (dev_regs_t));
14133859Sml29623 		nxgep->dev_regs = NULL;
14143859Sml29623 	}
14153859Sml29623 
14163859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_unmap_regs"));
14173859Sml29623 }
14183859Sml29623 
14193859Sml29623 static nxge_status_t
14203859Sml29623 nxge_setup_mutexes(p_nxge_t nxgep)
14213859Sml29623 {
14223859Sml29623 	int ddi_status = DDI_SUCCESS;
14233859Sml29623 	nxge_status_t status = NXGE_OK;
14243859Sml29623 	nxge_classify_t *classify_ptr;
14253859Sml29623 	int partition;
14263859Sml29623 
14273859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_setup_mutexes"));
14283859Sml29623 
14293859Sml29623 	/*
14303859Sml29623 	 * Get the interrupt cookie so the mutexes can be
14313859Sml29623 	 * Initialized.
14323859Sml29623 	 */
14336495Sspeer 	if (isLDOMguest(nxgep)) {
14346495Sspeer 		nxgep->interrupt_cookie = 0;
14356495Sspeer 	} else {
14366495Sspeer 		ddi_status = ddi_get_iblock_cookie(nxgep->dip, 0,
14376495Sspeer 		    &nxgep->interrupt_cookie);
14386495Sspeer 
14396495Sspeer 		if (ddi_status != DDI_SUCCESS) {
14406495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
14416495Sspeer 			    "<== nxge_setup_mutexes: failed 0x%x",
14426495Sspeer 			    ddi_status));
14436495Sspeer 			goto nxge_setup_mutexes_exit;
14446495Sspeer 		}
14453859Sml29623 	}
14463859Sml29623 
14474693Stm144005 	cv_init(&nxgep->poll_cv, NULL, CV_DRIVER, NULL);
14484693Stm144005 	MUTEX_INIT(&nxgep->poll_lock, NULL,
14494693Stm144005 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14504693Stm144005 
14513859Sml29623 	/*
14524693Stm144005 	 * Initialize mutexes for this device.
14533859Sml29623 	 */
14543859Sml29623 	MUTEX_INIT(nxgep->genlock, NULL,
14556512Ssowmini 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14563859Sml29623 	MUTEX_INIT(&nxgep->ouraddr_lock, NULL,
14576512Ssowmini 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14583859Sml29623 	MUTEX_INIT(&nxgep->mif_lock, NULL,
14596512Ssowmini 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14606495Sspeer 	MUTEX_INIT(&nxgep->group_lock, NULL,
14616495Sspeer 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14623859Sml29623 	RW_INIT(&nxgep->filter_lock, NULL,
14636512Ssowmini 	    RW_DRIVER, (void *)nxgep->interrupt_cookie);
14643859Sml29623 
14653859Sml29623 	classify_ptr = &nxgep->classifier;
14663859Sml29623 		/*
14673859Sml29623 		 * FFLP Mutexes are never used in interrupt context
14683859Sml29623 		 * as fflp operation can take very long time to
14693859Sml29623 		 * complete and hence not suitable to invoke from interrupt
14703859Sml29623 		 * handlers.
14713859Sml29623 		 */
14723859Sml29623 	MUTEX_INIT(&classify_ptr->tcam_lock, NULL,
14734732Sdavemq 	    NXGE_MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14744977Sraghus 	if (NXGE_IS_VALID_NEPTUNE_TYPE(nxgep)) {
14753859Sml29623 		MUTEX_INIT(&classify_ptr->fcram_lock, NULL,
14764732Sdavemq 		    NXGE_MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14773859Sml29623 		for (partition = 0; partition < MAX_PARTITION; partition++) {
14783859Sml29623 			MUTEX_INIT(&classify_ptr->hash_lock[partition], NULL,
14793859Sml29623 			    NXGE_MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14803859Sml29623 		}
14813859Sml29623 	}
14823859Sml29623 
14833859Sml29623 nxge_setup_mutexes_exit:
14843859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
14854732Sdavemq 	    "<== nxge_setup_mutexes status = %x", status));
14863859Sml29623 
14873859Sml29623 	if (ddi_status != DDI_SUCCESS)
14883859Sml29623 		status |= (NXGE_ERROR | NXGE_DDI_FAILED);
14893859Sml29623 
14903859Sml29623 	return (status);
14913859Sml29623 }
14923859Sml29623 
14933859Sml29623 static void
14943859Sml29623 nxge_destroy_mutexes(p_nxge_t nxgep)
14953859Sml29623 {
14963859Sml29623 	int partition;
14973859Sml29623 	nxge_classify_t *classify_ptr;
14983859Sml29623 
14993859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_destroy_mutexes"));
15003859Sml29623 	RW_DESTROY(&nxgep->filter_lock);
15016495Sspeer 	MUTEX_DESTROY(&nxgep->group_lock);
15023859Sml29623 	MUTEX_DESTROY(&nxgep->mif_lock);
15033859Sml29623 	MUTEX_DESTROY(&nxgep->ouraddr_lock);
15043859Sml29623 	MUTEX_DESTROY(nxgep->genlock);
15053859Sml29623 
15063859Sml29623 	classify_ptr = &nxgep->classifier;
15073859Sml29623 	MUTEX_DESTROY(&classify_ptr->tcam_lock);
15083859Sml29623 
15094693Stm144005 	/* Destroy all polling resources. */
15104693Stm144005 	MUTEX_DESTROY(&nxgep->poll_lock);
15114693Stm144005 	cv_destroy(&nxgep->poll_cv);
15124693Stm144005 
15134693Stm144005 	/* free data structures, based on HW type */
15144977Sraghus 	if (NXGE_IS_VALID_NEPTUNE_TYPE(nxgep)) {
15153859Sml29623 		MUTEX_DESTROY(&classify_ptr->fcram_lock);
15163859Sml29623 		for (partition = 0; partition < MAX_PARTITION; partition++) {
15173859Sml29623 			MUTEX_DESTROY(&classify_ptr->hash_lock[partition]);
15183859Sml29623 		}
15193859Sml29623 	}
15203859Sml29623 
15213859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_destroy_mutexes"));
15223859Sml29623 }
15233859Sml29623 
15243859Sml29623 nxge_status_t
15253859Sml29623 nxge_init(p_nxge_t nxgep)
15263859Sml29623 {
15276495Sspeer 	nxge_status_t status = NXGE_OK;
15283859Sml29623 
15293859Sml29623 	NXGE_DEBUG_MSG((nxgep, STR_CTL, "==> nxge_init"));
15303859Sml29623 
15313859Sml29623 	if (nxgep->drv_state & STATE_HW_INITIALIZED) {
15323859Sml29623 		return (status);
15333859Sml29623 	}
15343859Sml29623 
15353859Sml29623 	/*
15363859Sml29623 	 * Allocate system memory for the receive/transmit buffer blocks
15373859Sml29623 	 * and receive/transmit descriptor rings.
15383859Sml29623 	 */
15393859Sml29623 	status = nxge_alloc_mem_pool(nxgep);
15403859Sml29623 	if (status != NXGE_OK) {
15413859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "alloc mem failed\n"));
15423859Sml29623 		goto nxge_init_fail1;
15433859Sml29623 	}
15443859Sml29623 
15456495Sspeer 	if (!isLDOMguest(nxgep)) {
15466495Sspeer 		/*
15476495Sspeer 		 * Initialize and enable the TXC registers.
15486495Sspeer 		 * (Globally enable the Tx controller,
15496495Sspeer 		 *  enable the port, configure the dma channel bitmap,
15506495Sspeer 		 *  configure the max burst size).
15516495Sspeer 		 */
15526495Sspeer 		status = nxge_txc_init(nxgep);
15536495Sspeer 		if (status != NXGE_OK) {
15546495Sspeer 			NXGE_ERROR_MSG((nxgep,
15556495Sspeer 			    NXGE_ERR_CTL, "init txc failed\n"));
15566495Sspeer 			goto nxge_init_fail2;
15576495Sspeer 		}
15583859Sml29623 	}
15593859Sml29623 
15603859Sml29623 	/*
15613859Sml29623 	 * Initialize and enable TXDMA channels.
15623859Sml29623 	 */
15633859Sml29623 	status = nxge_init_txdma_channels(nxgep);
15643859Sml29623 	if (status != NXGE_OK) {
15653859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init txdma failed\n"));
15663859Sml29623 		goto nxge_init_fail3;
15673859Sml29623 	}
15683859Sml29623 
15693859Sml29623 	/*
15703859Sml29623 	 * Initialize and enable RXDMA channels.
15713859Sml29623 	 */
15723859Sml29623 	status = nxge_init_rxdma_channels(nxgep);
15733859Sml29623 	if (status != NXGE_OK) {
15743859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init rxdma failed\n"));
15753859Sml29623 		goto nxge_init_fail4;
15763859Sml29623 	}
15773859Sml29623 
15783859Sml29623 	/*
15796495Sspeer 	 * The guest domain is now done.
15806495Sspeer 	 */
15816495Sspeer 	if (isLDOMguest(nxgep)) {
15826495Sspeer 		nxgep->drv_state |= STATE_HW_INITIALIZED;
15836495Sspeer 		goto nxge_init_exit;
15846495Sspeer 	}
15856495Sspeer 
15866495Sspeer 	/*
15873859Sml29623 	 * Initialize TCAM and FCRAM (Neptune).
15883859Sml29623 	 */
15893859Sml29623 	status = nxge_classify_init(nxgep);
15903859Sml29623 	if (status != NXGE_OK) {
15913859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init classify failed\n"));
15923859Sml29623 		goto nxge_init_fail5;
15933859Sml29623 	}
15943859Sml29623 
15953859Sml29623 	/*
15963859Sml29623 	 * Initialize ZCP
15973859Sml29623 	 */
15983859Sml29623 	status = nxge_zcp_init(nxgep);
15993859Sml29623 	if (status != NXGE_OK) {
16003859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init ZCP failed\n"));
16013859Sml29623 		goto nxge_init_fail5;
16023859Sml29623 	}
16033859Sml29623 
16043859Sml29623 	/*
16053859Sml29623 	 * Initialize IPP.
16063859Sml29623 	 */
16073859Sml29623 	status = nxge_ipp_init(nxgep);
16083859Sml29623 	if (status != NXGE_OK) {
16093859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init IPP failed\n"));
16103859Sml29623 		goto nxge_init_fail5;
16113859Sml29623 	}
16123859Sml29623 
16133859Sml29623 	/*
16143859Sml29623 	 * Initialize the MAC block.
16153859Sml29623 	 */
16163859Sml29623 	status = nxge_mac_init(nxgep);
16173859Sml29623 	if (status != NXGE_OK) {
16183859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init MAC failed\n"));
16193859Sml29623 		goto nxge_init_fail5;
16203859Sml29623 	}
16213859Sml29623 
16223859Sml29623 	/*
16239232SMichael.Speer@Sun.COM 	 * Enable the interrrupts for DDI.
16243859Sml29623 	 */
16259232SMichael.Speer@Sun.COM 	nxge_intrs_enable(nxgep);
16269232SMichael.Speer@Sun.COM 
16273859Sml29623 	nxgep->drv_state |= STATE_HW_INITIALIZED;
16283859Sml29623 
16293859Sml29623 	goto nxge_init_exit;
16303859Sml29623 
16313859Sml29623 nxge_init_fail5:
16323859Sml29623 	nxge_uninit_rxdma_channels(nxgep);
16333859Sml29623 nxge_init_fail4:
16343859Sml29623 	nxge_uninit_txdma_channels(nxgep);
16353859Sml29623 nxge_init_fail3:
16366495Sspeer 	if (!isLDOMguest(nxgep)) {
16376495Sspeer 		(void) nxge_txc_uninit(nxgep);
16386495Sspeer 	}
16393859Sml29623 nxge_init_fail2:
16403859Sml29623 	nxge_free_mem_pool(nxgep);
16413859Sml29623 nxge_init_fail1:
16423859Sml29623 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
16436512Ssowmini 	    "<== nxge_init status (failed) = 0x%08x", status));
16443859Sml29623 	return (status);
16453859Sml29623 
16463859Sml29623 nxge_init_exit:
16473859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_init status = 0x%08x",
16486512Ssowmini 	    status));
16493859Sml29623 	return (status);
16503859Sml29623 }
16513859Sml29623 
16523859Sml29623 
16533859Sml29623 timeout_id_t
16543859Sml29623 nxge_start_timer(p_nxge_t nxgep, fptrv_t func, int msec)
16553859Sml29623 {
16566512Ssowmini 	if ((nxgep->suspended == 0) || (nxgep->suspended == DDI_RESUME)) {
16573859Sml29623 		return (timeout(func, (caddr_t)nxgep,
16586512Ssowmini 		    drv_usectohz(1000 * msec)));
16593859Sml29623 	}
16603859Sml29623 	return (NULL);
16613859Sml29623 }
16623859Sml29623 
16633859Sml29623 /*ARGSUSED*/
16643859Sml29623 void
16653859Sml29623 nxge_stop_timer(p_nxge_t nxgep, timeout_id_t timerid)
16663859Sml29623 {
16673859Sml29623 	if (timerid) {
16683859Sml29623 		(void) untimeout(timerid);
16693859Sml29623 	}
16703859Sml29623 }
16713859Sml29623 
16723859Sml29623 void
16733859Sml29623 nxge_uninit(p_nxge_t nxgep)
16743859Sml29623 {
16753859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_uninit"));
16763859Sml29623 
16773859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
16783859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
16796512Ssowmini 		    "==> nxge_uninit: not initialized"));
16803859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
16816512Ssowmini 		    "<== nxge_uninit"));
16823859Sml29623 		return;
16833859Sml29623 	}
16843859Sml29623 
16859232SMichael.Speer@Sun.COM 	if (!isLDOMguest(nxgep)) {
16869232SMichael.Speer@Sun.COM 		/*
16879232SMichael.Speer@Sun.COM 		 * Reset the receive MAC side.
16889232SMichael.Speer@Sun.COM 		 */
16899232SMichael.Speer@Sun.COM 		(void) nxge_rx_mac_disable(nxgep);
16909232SMichael.Speer@Sun.COM 
16919232SMichael.Speer@Sun.COM 		/*
16929232SMichael.Speer@Sun.COM 		 * Drain the IPP.
16939232SMichael.Speer@Sun.COM 		 */
16949232SMichael.Speer@Sun.COM 		(void) nxge_ipp_drain(nxgep);
16959232SMichael.Speer@Sun.COM 	}
16969232SMichael.Speer@Sun.COM 
16973859Sml29623 	/* stop timer */
16983859Sml29623 	if (nxgep->nxge_timerid) {
16993859Sml29623 		nxge_stop_timer(nxgep, nxgep->nxge_timerid);
17003859Sml29623 		nxgep->nxge_timerid = 0;
17013859Sml29623 	}
17023859Sml29623 
17033859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_STOP);
17043859Sml29623 	(void) nxge_intr_hw_disable(nxgep);
17053859Sml29623 
17063859Sml29623 
17073859Sml29623 	/* Disable and soft reset the IPP */
17086495Sspeer 	if (!isLDOMguest(nxgep))
17096495Sspeer 		(void) nxge_ipp_disable(nxgep);
17103859Sml29623 
17113859Sml29623 	/* Free classification resources */
17123859Sml29623 	(void) nxge_classify_uninit(nxgep);
17133859Sml29623 
17143859Sml29623 	/*
17153859Sml29623 	 * Reset the transmit/receive DMA side.
17163859Sml29623 	 */
17173859Sml29623 	(void) nxge_txdma_hw_mode(nxgep, NXGE_DMA_STOP);
17183859Sml29623 	(void) nxge_rxdma_hw_mode(nxgep, NXGE_DMA_STOP);
17193859Sml29623 
17203859Sml29623 	nxge_uninit_txdma_channels(nxgep);
17213859Sml29623 	nxge_uninit_rxdma_channels(nxgep);
17223859Sml29623 
17233859Sml29623 	/*
17243859Sml29623 	 * Reset the transmit MAC side.
17253859Sml29623 	 */
17263859Sml29623 	(void) nxge_tx_mac_disable(nxgep);
17273859Sml29623 
17283859Sml29623 	nxge_free_mem_pool(nxgep);
17293859Sml29623 
17306705Sml29623 	/*
17316705Sml29623 	 * Start the timer if the reset flag is not set.
17326705Sml29623 	 * If this reset flag is set, the link monitor
17336705Sml29623 	 * will not be started in order to stop furthur bus
17346705Sml29623 	 * activities coming from this interface.
17356705Sml29623 	 * The driver will start the monitor function
17366705Sml29623 	 * if the interface was initialized again later.
17376705Sml29623 	 */
17386705Sml29623 	if (!nxge_peu_reset_enable) {
17396705Sml29623 		(void) nxge_link_monitor(nxgep, LINK_MONITOR_START);
17406705Sml29623 	}
17413859Sml29623 
17423859Sml29623 	nxgep->drv_state &= ~STATE_HW_INITIALIZED;
17433859Sml29623 
17443859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_uninit: "
17456512Ssowmini 	    "nxge_mblks_pending %d", nxge_mblks_pending));
17463859Sml29623 }
17473859Sml29623 
17483859Sml29623 void
17493859Sml29623 nxge_get64(p_nxge_t nxgep, p_mblk_t mp)
17503859Sml29623 {
17513859Sml29623 	uint64_t	reg;
17523859Sml29623 	uint64_t	regdata;
17533859Sml29623 	int		i, retry;
17543859Sml29623 
17553859Sml29623 	bcopy((char *)mp->b_rptr, (char *)&reg, sizeof (uint64_t));
17563859Sml29623 	regdata = 0;
17573859Sml29623 	retry = 1;
17583859Sml29623 
17593859Sml29623 	for (i = 0; i < retry; i++) {
17603859Sml29623 		NXGE_REG_RD64(nxgep->npi_handle, reg, &regdata);
17613859Sml29623 	}
17623859Sml29623 	bcopy((char *)&regdata, (char *)mp->b_rptr, sizeof (uint64_t));
17633859Sml29623 }
17643859Sml29623 
17653859Sml29623 void
17663859Sml29623 nxge_put64(p_nxge_t nxgep, p_mblk_t mp)
17673859Sml29623 {
17683859Sml29623 	uint64_t	reg;
17693859Sml29623 	uint64_t	buf[2];
17703859Sml29623 
17713859Sml29623 	bcopy((char *)mp->b_rptr, (char *)&buf[0], 2 * sizeof (uint64_t));
17723859Sml29623 	reg = buf[0];
17733859Sml29623 
17743859Sml29623 	NXGE_NPI_PIO_WRITE64(nxgep->npi_handle, reg, buf[1]);
17753859Sml29623 }
17763859Sml29623 
17773859Sml29623 /*ARGSUSED*/
17783859Sml29623 /*VARARGS*/
17793859Sml29623 void
17803859Sml29623 nxge_debug_msg(p_nxge_t nxgep, uint64_t level, char *fmt, ...)
17813859Sml29623 {
17823859Sml29623 	char msg_buffer[1048];
17833859Sml29623 	char prefix_buffer[32];
17843859Sml29623 	int instance;
17853859Sml29623 	uint64_t debug_level;
17863859Sml29623 	int cmn_level = CE_CONT;
17873859Sml29623 	va_list ap;
17883859Sml29623 
17896495Sspeer 	if (nxgep && nxgep->nxge_debug_level != nxge_debug_level) {
17906495Sspeer 		/* In case a developer has changed nxge_debug_level. */
17916495Sspeer 		if (nxgep->nxge_debug_level != nxge_debug_level)
17926495Sspeer 			nxgep->nxge_debug_level = nxge_debug_level;
17936495Sspeer 	}
17946495Sspeer 
17953859Sml29623 	debug_level = (nxgep == NULL) ? nxge_debug_level :
17966512Ssowmini 	    nxgep->nxge_debug_level;
17973859Sml29623 
17983859Sml29623 	if ((level & debug_level) ||
17996512Ssowmini 	    (level == NXGE_NOTE) ||
18006512Ssowmini 	    (level == NXGE_ERR_CTL)) {
18013859Sml29623 		/* do the msg processing */
18023859Sml29623 		MUTEX_ENTER(&nxgedebuglock);
18033859Sml29623 
18043859Sml29623 		if ((level & NXGE_NOTE)) {
18053859Sml29623 			cmn_level = CE_NOTE;
18063859Sml29623 		}
18073859Sml29623 
18083859Sml29623 		if (level & NXGE_ERR_CTL) {
18093859Sml29623 			cmn_level = CE_WARN;
18103859Sml29623 		}
18113859Sml29623 
18123859Sml29623 		va_start(ap, fmt);
18133859Sml29623 		(void) vsprintf(msg_buffer, fmt, ap);
18143859Sml29623 		va_end(ap);
18153859Sml29623 		if (nxgep == NULL) {
18163859Sml29623 			instance = -1;
18173859Sml29623 			(void) sprintf(prefix_buffer, "%s :", "nxge");
18183859Sml29623 		} else {
18193859Sml29623 			instance = nxgep->instance;
18203859Sml29623 			(void) sprintf(prefix_buffer,
18216512Ssowmini 			    "%s%d :", "nxge", instance);
18223859Sml29623 		}
18233859Sml29623 
18243859Sml29623 		MUTEX_EXIT(&nxgedebuglock);
18253859Sml29623 		cmn_err(cmn_level, "!%s %s\n",
18266512Ssowmini 		    prefix_buffer, msg_buffer);
18273859Sml29623 
18283859Sml29623 	}
18293859Sml29623 }
18303859Sml29623 
18313859Sml29623 char *
18323859Sml29623 nxge_dump_packet(char *addr, int size)
18333859Sml29623 {
18343859Sml29623 	uchar_t *ap = (uchar_t *)addr;
18353859Sml29623 	int i;
18363859Sml29623 	static char etherbuf[1024];
18373859Sml29623 	char *cp = etherbuf;
18383859Sml29623 	char digits[] = "0123456789abcdef";
18393859Sml29623 
18403859Sml29623 	if (!size)
18413859Sml29623 		size = 60;
18423859Sml29623 
18433859Sml29623 	if (size > MAX_DUMP_SZ) {
18443859Sml29623 		/* Dump the leading bytes */
18453859Sml29623 		for (i = 0; i < MAX_DUMP_SZ/2; i++) {
18463859Sml29623 			if (*ap > 0x0f)
18473859Sml29623 				*cp++ = digits[*ap >> 4];
18483859Sml29623 			*cp++ = digits[*ap++ & 0xf];
18493859Sml29623 			*cp++ = ':';
18503859Sml29623 		}
18513859Sml29623 		for (i = 0; i < 20; i++)
18523859Sml29623 			*cp++ = '.';
18533859Sml29623 		/* Dump the last MAX_DUMP_SZ/2 bytes */
18543859Sml29623 		ap = (uchar_t *)(addr + (size - MAX_DUMP_SZ/2));
18553859Sml29623 		for (i = 0; i < MAX_DUMP_SZ/2; i++) {
18563859Sml29623 			if (*ap > 0x0f)
18573859Sml29623 				*cp++ = digits[*ap >> 4];
18583859Sml29623 			*cp++ = digits[*ap++ & 0xf];
18593859Sml29623 			*cp++ = ':';
18603859Sml29623 		}
18613859Sml29623 	} else {
18623859Sml29623 		for (i = 0; i < size; i++) {
18633859Sml29623 			if (*ap > 0x0f)
18643859Sml29623 				*cp++ = digits[*ap >> 4];
18653859Sml29623 			*cp++ = digits[*ap++ & 0xf];
18663859Sml29623 			*cp++ = ':';
18673859Sml29623 		}
18683859Sml29623 	}
18693859Sml29623 	*--cp = 0;
18703859Sml29623 	return (etherbuf);
18713859Sml29623 }
18723859Sml29623 
18733859Sml29623 #ifdef	NXGE_DEBUG
18743859Sml29623 static void
18753859Sml29623 nxge_test_map_regs(p_nxge_t nxgep)
18763859Sml29623 {
18773859Sml29623 	ddi_acc_handle_t cfg_handle;
18783859Sml29623 	p_pci_cfg_t	cfg_ptr;
18793859Sml29623 	ddi_acc_handle_t dev_handle;
18803859Sml29623 	char		*dev_ptr;
18813859Sml29623 	ddi_acc_handle_t pci_config_handle;
18823859Sml29623 	uint32_t	regval;
18833859Sml29623 	int		i;
18843859Sml29623 
18853859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_test_map_regs"));
18863859Sml29623 
18873859Sml29623 	dev_handle = nxgep->dev_regs->nxge_regh;
18883859Sml29623 	dev_ptr = (char *)nxgep->dev_regs->nxge_regp;
18893859Sml29623 
18904977Sraghus 	if (NXGE_IS_VALID_NEPTUNE_TYPE(nxgep)) {
18913859Sml29623 		cfg_handle = nxgep->dev_regs->nxge_pciregh;
18923859Sml29623 		cfg_ptr = (void *)nxgep->dev_regs->nxge_pciregp;
18933859Sml29623 
18943859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
18954732Sdavemq 		    "Neptune PCI regp cfg_ptr 0x%llx", (char *)cfg_ptr));
18963859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
18974732Sdavemq 		    "Neptune PCI cfg_ptr vendor id ptr 0x%llx",
18984732Sdavemq 		    &cfg_ptr->vendorid));
18993859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19004732Sdavemq 		    "\tvendorid 0x%x devid 0x%x",
19014732Sdavemq 		    NXGE_PIO_READ16(cfg_handle, &cfg_ptr->vendorid, 0),
19024732Sdavemq 		    NXGE_PIO_READ16(cfg_handle, &cfg_ptr->devid,    0)));
19033859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19044732Sdavemq 		    "PCI BAR: base 0x%x base14 0x%x base 18 0x%x "
19054732Sdavemq 		    "bar1c 0x%x",
19064732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base,   0),
19074732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base14, 0),
19084732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base18, 0),
19094732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base1c, 0)));
19103859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19114732Sdavemq 		    "\nNeptune PCI BAR: base20 0x%x base24 0x%x "
19124732Sdavemq 		    "base 28 0x%x bar2c 0x%x\n",
19134732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base20, 0),
19144732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base24, 0),
19154732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base28, 0),
19164732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base2c, 0)));
19173859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19184732Sdavemq 		    "\nNeptune PCI BAR: base30 0x%x\n",
19194732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base30, 0)));
19203859Sml29623 
19213859Sml29623 		cfg_handle = nxgep->dev_regs->nxge_pciregh;
19223859Sml29623 		cfg_ptr = (void *)nxgep->dev_regs->nxge_pciregp;
19233859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19244732Sdavemq 		    "first  0x%llx second 0x%llx third 0x%llx "
19254732Sdavemq 		    "last 0x%llx ",
19264732Sdavemq 		    NXGE_PIO_READ64(dev_handle,
19274732Sdavemq 		    (uint64_t *)(dev_ptr + 0),  0),
19284732Sdavemq 		    NXGE_PIO_READ64(dev_handle,
19294732Sdavemq 		    (uint64_t *)(dev_ptr + 8),  0),
19304732Sdavemq 		    NXGE_PIO_READ64(dev_handle,
19314732Sdavemq 		    (uint64_t *)(dev_ptr + 16), 0),
19324732Sdavemq 		    NXGE_PIO_READ64(cfg_handle,
19334732Sdavemq 		    (uint64_t *)(dev_ptr + 24), 0)));
19343859Sml29623 	}
19353859Sml29623 }
19363859Sml29623 
19373859Sml29623 #endif
19383859Sml29623 
19393859Sml29623 static void
19403859Sml29623 nxge_suspend(p_nxge_t nxgep)
19413859Sml29623 {
19423859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_suspend"));
19433859Sml29623 
19443859Sml29623 	nxge_intrs_disable(nxgep);
19453859Sml29623 	nxge_destroy_dev(nxgep);
19463859Sml29623 
19473859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_suspend"));
19483859Sml29623 }
19493859Sml29623 
19503859Sml29623 static nxge_status_t
19513859Sml29623 nxge_resume(p_nxge_t nxgep)
19523859Sml29623 {
19533859Sml29623 	nxge_status_t status = NXGE_OK;
19543859Sml29623 
19553859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_resume"));
19564587Sjoycey 
19573859Sml29623 	nxgep->suspended = DDI_RESUME;
19584587Sjoycey 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_START);
19594587Sjoycey 	(void) nxge_rxdma_hw_mode(nxgep, NXGE_DMA_START);
19604587Sjoycey 	(void) nxge_txdma_hw_mode(nxgep, NXGE_DMA_START);
19614587Sjoycey 	(void) nxge_rx_mac_enable(nxgep);
19624587Sjoycey 	(void) nxge_tx_mac_enable(nxgep);
19634587Sjoycey 	nxge_intrs_enable(nxgep);
19643859Sml29623 	nxgep->suspended = 0;
19653859Sml29623 
19663859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19676512Ssowmini 	    "<== nxge_resume status = 0x%x", status));
19683859Sml29623 	return (status);
19693859Sml29623 }
19703859Sml29623 
19713859Sml29623 static nxge_status_t
19723859Sml29623 nxge_setup_dev(p_nxge_t nxgep)
19733859Sml29623 {
19743859Sml29623 	nxge_status_t	status = NXGE_OK;
19753859Sml29623 
19763859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_setup_dev port %d",
19774732Sdavemq 	    nxgep->mac.portnum));
19783859Sml29623 
19793859Sml29623 	status = nxge_link_init(nxgep);
19803859Sml29623 
19813859Sml29623 	if (fm_check_acc_handle(nxgep->dev_regs->nxge_regh) != DDI_FM_OK) {
19823859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
19836512Ssowmini 		    "port%d Bad register acc handle", nxgep->mac.portnum));
19843859Sml29623 		status = NXGE_ERROR;
19853859Sml29623 	}
19863859Sml29623 
19873859Sml29623 	if (status != NXGE_OK) {
19883859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
19896512Ssowmini 		    " nxge_setup_dev status "
19906512Ssowmini 		    "(xcvr init 0x%08x)", status));
19913859Sml29623 		goto nxge_setup_dev_exit;
19923859Sml29623 	}
19933859Sml29623 
19943859Sml29623 nxge_setup_dev_exit:
19953859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19966512Ssowmini 	    "<== nxge_setup_dev port %d status = 0x%08x",
19976512Ssowmini 	    nxgep->mac.portnum, status));
19983859Sml29623 
19993859Sml29623 	return (status);
20003859Sml29623 }
20013859Sml29623 
20023859Sml29623 static void
20033859Sml29623 nxge_destroy_dev(p_nxge_t nxgep)
20043859Sml29623 {
20053859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_destroy_dev"));
20063859Sml29623 
20073859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_STOP);
20083859Sml29623 
20093859Sml29623 	(void) nxge_hw_stop(nxgep);
20103859Sml29623 
20113859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_destroy_dev"));
20123859Sml29623 }
20133859Sml29623 
20143859Sml29623 static nxge_status_t
20153859Sml29623 nxge_setup_system_dma_pages(p_nxge_t nxgep)
20163859Sml29623 {
20173859Sml29623 	int 			ddi_status = DDI_SUCCESS;
20183859Sml29623 	uint_t 			count;
20193859Sml29623 	ddi_dma_cookie_t 	cookie;
20203859Sml29623 	uint_t 			iommu_pagesize;
20213859Sml29623 	nxge_status_t		status = NXGE_OK;
20223859Sml29623 
20236495Sspeer 	NXGE_ERROR_MSG((nxgep, DDI_CTL, "==> nxge_setup_system_dma_pages"));
20243859Sml29623 	nxgep->sys_page_sz = ddi_ptob(nxgep->dip, (ulong_t)1);
20253859Sml29623 	if (nxgep->niu_type != N2_NIU) {
20263859Sml29623 		iommu_pagesize = dvma_pagesize(nxgep->dip);
20273859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
20286512Ssowmini 		    " nxge_setup_system_dma_pages: page %d (ddi_ptob %d) "
20296512Ssowmini 		    " default_block_size %d iommu_pagesize %d",
20306512Ssowmini 		    nxgep->sys_page_sz,
20316512Ssowmini 		    ddi_ptob(nxgep->dip, (ulong_t)1),
20326512Ssowmini 		    nxgep->rx_default_block_size,
20336512Ssowmini 		    iommu_pagesize));
20343859Sml29623 
20353859Sml29623 		if (iommu_pagesize != 0) {
20363859Sml29623 			if (nxgep->sys_page_sz == iommu_pagesize) {
20373859Sml29623 				if (iommu_pagesize > 0x4000)
20383859Sml29623 					nxgep->sys_page_sz = 0x4000;
20393859Sml29623 			} else {
20403859Sml29623 				if (nxgep->sys_page_sz > iommu_pagesize)
20413859Sml29623 					nxgep->sys_page_sz = iommu_pagesize;
20423859Sml29623 			}
20433859Sml29623 		}
20443859Sml29623 	}
20453859Sml29623 	nxgep->sys_page_mask = ~(nxgep->sys_page_sz - 1);
20463859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
20476512Ssowmini 	    "==> nxge_setup_system_dma_pages: page %d (ddi_ptob %d) "
20486512Ssowmini 	    "default_block_size %d page mask %d",
20496512Ssowmini 	    nxgep->sys_page_sz,
20506512Ssowmini 	    ddi_ptob(nxgep->dip, (ulong_t)1),
20516512Ssowmini 	    nxgep->rx_default_block_size,
20526512Ssowmini 	    nxgep->sys_page_mask));
20533859Sml29623 
20543859Sml29623 
20553859Sml29623 	switch (nxgep->sys_page_sz) {
20563859Sml29623 	default:
20573859Sml29623 		nxgep->sys_page_sz = 0x1000;
20583859Sml29623 		nxgep->sys_page_mask = ~(nxgep->sys_page_sz - 1);
20593859Sml29623 		nxgep->rx_default_block_size = 0x1000;
20603859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_4K;
20613859Sml29623 		break;
20623859Sml29623 	case 0x1000:
20633859Sml29623 		nxgep->rx_default_block_size = 0x1000;
20643859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_4K;
20653859Sml29623 		break;
20663859Sml29623 	case 0x2000:
20673859Sml29623 		nxgep->rx_default_block_size = 0x2000;
20683859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_8K;
20693859Sml29623 		break;
20703859Sml29623 	case 0x4000:
20713859Sml29623 		nxgep->rx_default_block_size = 0x4000;
20723859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_16K;
20733859Sml29623 		break;
20743859Sml29623 	case 0x8000:
20753859Sml29623 		nxgep->rx_default_block_size = 0x8000;
20763859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_32K;
20773859Sml29623 		break;
20783859Sml29623 	}
20793859Sml29623 
20803859Sml29623 #ifndef USE_RX_BIG_BUF
20813859Sml29623 	nxge_rx_dma_attr.dma_attr_align = nxgep->sys_page_sz;
20823859Sml29623 #else
20833859Sml29623 		nxgep->rx_default_block_size = 0x2000;
20843859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_8K;
20853859Sml29623 #endif
20863859Sml29623 	/*
20873859Sml29623 	 * Get the system DMA burst size.
20883859Sml29623 	 */
20893859Sml29623 	ddi_status = ddi_dma_alloc_handle(nxgep->dip, &nxge_tx_dma_attr,
20906512Ssowmini 	    DDI_DMA_DONTWAIT, 0,
20916512Ssowmini 	    &nxgep->dmasparehandle);
20923859Sml29623 	if (ddi_status != DDI_SUCCESS) {
20933859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
20946512Ssowmini 		    "ddi_dma_alloc_handle: failed "
20956512Ssowmini 		    " status 0x%x", ddi_status));
20963859Sml29623 		goto nxge_get_soft_properties_exit;
20973859Sml29623 	}
20983859Sml29623 
20993859Sml29623 	ddi_status = ddi_dma_addr_bind_handle(nxgep->dmasparehandle, NULL,
21006512Ssowmini 	    (caddr_t)nxgep->dmasparehandle,
21016512Ssowmini 	    sizeof (nxgep->dmasparehandle),
21026512Ssowmini 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
21036512Ssowmini 	    DDI_DMA_DONTWAIT, 0,
21046512Ssowmini 	    &cookie, &count);
21053859Sml29623 	if (ddi_status != DDI_DMA_MAPPED) {
21063859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
21076512Ssowmini 		    "Binding spare handle to find system"
21086512Ssowmini 		    " burstsize failed."));
21093859Sml29623 		ddi_status = DDI_FAILURE;
21103859Sml29623 		goto nxge_get_soft_properties_fail1;
21113859Sml29623 	}
21123859Sml29623 
21133859Sml29623 	nxgep->sys_burst_sz = ddi_dma_burstsizes(nxgep->dmasparehandle);
21143859Sml29623 	(void) ddi_dma_unbind_handle(nxgep->dmasparehandle);
21153859Sml29623 
21163859Sml29623 nxge_get_soft_properties_fail1:
21173859Sml29623 	ddi_dma_free_handle(&nxgep->dmasparehandle);
21183859Sml29623 
21193859Sml29623 nxge_get_soft_properties_exit:
21203859Sml29623 
21213859Sml29623 	if (ddi_status != DDI_SUCCESS)
21223859Sml29623 		status |= (NXGE_ERROR | NXGE_DDI_FAILED);
21233859Sml29623 
21243859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
21256512Ssowmini 	    "<== nxge_setup_system_dma_pages status = 0x%08x", status));
21263859Sml29623 	return (status);
21273859Sml29623 }
21283859Sml29623 
21293859Sml29623 static nxge_status_t
21303859Sml29623 nxge_alloc_mem_pool(p_nxge_t nxgep)
21313859Sml29623 {
21323859Sml29623 	nxge_status_t	status = NXGE_OK;
21333859Sml29623 
21343859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_alloc_mem_pool"));
21353859Sml29623 
21363859Sml29623 	status = nxge_alloc_rx_mem_pool(nxgep);
21373859Sml29623 	if (status != NXGE_OK) {
21383859Sml29623 		return (NXGE_ERROR);
21393859Sml29623 	}
21403859Sml29623 
21413859Sml29623 	status = nxge_alloc_tx_mem_pool(nxgep);
21423859Sml29623 	if (status != NXGE_OK) {
21433859Sml29623 		nxge_free_rx_mem_pool(nxgep);
21443859Sml29623 		return (NXGE_ERROR);
21453859Sml29623 	}
21463859Sml29623 
21473859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_alloc_mem_pool"));
21483859Sml29623 	return (NXGE_OK);
21493859Sml29623 }
21503859Sml29623 
21513859Sml29623 static void
21523859Sml29623 nxge_free_mem_pool(p_nxge_t nxgep)
21533859Sml29623 {
21543859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "==> nxge_free_mem_pool"));
21553859Sml29623 
21563859Sml29623 	nxge_free_rx_mem_pool(nxgep);
21573859Sml29623 	nxge_free_tx_mem_pool(nxgep);
21583859Sml29623 
21593859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "<== nxge_free_mem_pool"));
21603859Sml29623 }
21613859Sml29623 
21626495Sspeer nxge_status_t
21633859Sml29623 nxge_alloc_rx_mem_pool(p_nxge_t nxgep)
21643859Sml29623 {
21656495Sspeer 	uint32_t		rdc_max;
21663859Sml29623 	p_nxge_dma_pt_cfg_t	p_all_cfgp;
21673859Sml29623 	p_nxge_hw_pt_cfg_t	p_cfgp;
21683859Sml29623 	p_nxge_dma_pool_t	dma_poolp;
21693859Sml29623 	p_nxge_dma_common_t	*dma_buf_p;
21703859Sml29623 	p_nxge_dma_pool_t	dma_cntl_poolp;
21713859Sml29623 	p_nxge_dma_common_t	*dma_cntl_p;
21723859Sml29623 	uint32_t 		*num_chunks; /* per dma */
21733859Sml29623 	nxge_status_t		status = NXGE_OK;
21743859Sml29623 
21753859Sml29623 	uint32_t		nxge_port_rbr_size;
21763859Sml29623 	uint32_t		nxge_port_rbr_spare_size;
21773859Sml29623 	uint32_t		nxge_port_rcr_size;
21786495Sspeer 	uint32_t		rx_cntl_alloc_size;
21793859Sml29623 
21803859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rx_mem_pool"));
21813859Sml29623 
21823859Sml29623 	p_all_cfgp = (p_nxge_dma_pt_cfg_t)&nxgep->pt_config;
21833859Sml29623 	p_cfgp = (p_nxge_hw_pt_cfg_t)&p_all_cfgp->hw_config;
21846495Sspeer 	rdc_max = NXGE_MAX_RDCS;
21853859Sml29623 
21863859Sml29623 	/*
21876495Sspeer 	 * Allocate memory for the common DMA data structures.
21883859Sml29623 	 */
21893859Sml29623 	dma_poolp = (p_nxge_dma_pool_t)KMEM_ZALLOC(sizeof (nxge_dma_pool_t),
21906512Ssowmini 	    KM_SLEEP);
21913859Sml29623 	dma_buf_p = (p_nxge_dma_common_t *)KMEM_ZALLOC(
21926512Ssowmini 	    sizeof (p_nxge_dma_common_t) * rdc_max, KM_SLEEP);
21933859Sml29623 
21943859Sml29623 	dma_cntl_poolp = (p_nxge_dma_pool_t)
21956512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_pool_t), KM_SLEEP);
21963859Sml29623 	dma_cntl_p = (p_nxge_dma_common_t *)KMEM_ZALLOC(
21976512Ssowmini 	    sizeof (p_nxge_dma_common_t) * rdc_max, KM_SLEEP);
21983859Sml29623 
21993859Sml29623 	num_chunks = (uint32_t *)KMEM_ZALLOC(
22006512Ssowmini 	    sizeof (uint32_t) * rdc_max, KM_SLEEP);
22013859Sml29623 
22023859Sml29623 	/*
22036495Sspeer 	 * Assume that each DMA channel will be configured with
22046495Sspeer 	 * the default block size.
22056495Sspeer 	 * rbr block counts are modulo the batch count (16).
22063859Sml29623 	 */
22073859Sml29623 	nxge_port_rbr_size = p_all_cfgp->rbr_size;
22083859Sml29623 	nxge_port_rcr_size = p_all_cfgp->rcr_size;
22093859Sml29623 
22103859Sml29623 	if (!nxge_port_rbr_size) {
22113859Sml29623 		nxge_port_rbr_size = NXGE_RBR_RBB_DEFAULT;
22123859Sml29623 	}
22133859Sml29623 	if (nxge_port_rbr_size % NXGE_RXDMA_POST_BATCH) {
22143859Sml29623 		nxge_port_rbr_size = (NXGE_RXDMA_POST_BATCH *
22156512Ssowmini 		    (nxge_port_rbr_size / NXGE_RXDMA_POST_BATCH + 1));
22163859Sml29623 	}
22173859Sml29623 
22183859Sml29623 	p_all_cfgp->rbr_size = nxge_port_rbr_size;
22193859Sml29623 	nxge_port_rbr_spare_size = nxge_rbr_spare_size;
22203859Sml29623 
22213859Sml29623 	if (nxge_port_rbr_spare_size % NXGE_RXDMA_POST_BATCH) {
22223859Sml29623 		nxge_port_rbr_spare_size = (NXGE_RXDMA_POST_BATCH *
22236512Ssowmini 		    (nxge_port_rbr_spare_size / NXGE_RXDMA_POST_BATCH + 1));
22243859Sml29623 	}
22255770Sml29623 	if (nxge_port_rbr_size > RBR_DEFAULT_MAX_BLKS) {
22265770Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM_CTL,
22275770Sml29623 		    "nxge_alloc_rx_mem_pool: RBR size too high %d, "
22285770Sml29623 		    "set to default %d",
22295770Sml29623 		    nxge_port_rbr_size, RBR_DEFAULT_MAX_BLKS));
22305770Sml29623 		nxge_port_rbr_size = RBR_DEFAULT_MAX_BLKS;
22315770Sml29623 	}
22325770Sml29623 	if (nxge_port_rcr_size > RCR_DEFAULT_MAX) {
22335770Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM_CTL,
22345770Sml29623 		    "nxge_alloc_rx_mem_pool: RCR too high %d, "
22355770Sml29623 		    "set to default %d",
22365770Sml29623 		    nxge_port_rcr_size, RCR_DEFAULT_MAX));
22375770Sml29623 		nxge_port_rcr_size = RCR_DEFAULT_MAX;
22385770Sml29623 	}
22393859Sml29623 
22403859Sml29623 	/*
22413859Sml29623 	 * N2/NIU has limitation on the descriptor sizes (contiguous
22423859Sml29623 	 * memory allocation on data buffers to 4M (contig_mem_alloc)
22433859Sml29623 	 * and little endian for control buffers (must use the ddi/dki mem alloc
22443859Sml29623 	 * function).
22453859Sml29623 	 */
22463859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
22473859Sml29623 	if (nxgep->niu_type == N2_NIU) {
22483859Sml29623 		nxge_port_rbr_spare_size = 0;
22493859Sml29623 		if ((nxge_port_rbr_size > NXGE_NIU_CONTIG_RBR_MAX) ||
22506512Ssowmini 		    (!ISP2(nxge_port_rbr_size))) {
22513859Sml29623 			nxge_port_rbr_size = NXGE_NIU_CONTIG_RBR_MAX;
22523859Sml29623 		}
22533859Sml29623 		if ((nxge_port_rcr_size > NXGE_NIU_CONTIG_RCR_MAX) ||
22546512Ssowmini 		    (!ISP2(nxge_port_rcr_size))) {
22553859Sml29623 			nxge_port_rcr_size = NXGE_NIU_CONTIG_RCR_MAX;
22563859Sml29623 		}
22573859Sml29623 	}
22583859Sml29623 #endif
22593859Sml29623 
22603859Sml29623 	/*
22613859Sml29623 	 * Addresses of receive block ring, receive completion ring and the
22623859Sml29623 	 * mailbox must be all cache-aligned (64 bytes).
22633859Sml29623 	 */
22643859Sml29623 	rx_cntl_alloc_size = nxge_port_rbr_size + nxge_port_rbr_spare_size;
22653859Sml29623 	rx_cntl_alloc_size *= (sizeof (rx_desc_t));
22663859Sml29623 	rx_cntl_alloc_size += (sizeof (rcr_entry_t) * nxge_port_rcr_size);
22673859Sml29623 	rx_cntl_alloc_size += sizeof (rxdma_mailbox_t);
22683859Sml29623 
22693859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_alloc_rx_mem_pool: "
22706512Ssowmini 	    "nxge_port_rbr_size = %d nxge_port_rbr_spare_size = %d "
22716512Ssowmini 	    "nxge_port_rcr_size = %d "
22726512Ssowmini 	    "rx_cntl_alloc_size = %d",
22736512Ssowmini 	    nxge_port_rbr_size, nxge_port_rbr_spare_size,
22746512Ssowmini 	    nxge_port_rcr_size,
22756512Ssowmini 	    rx_cntl_alloc_size));
22763859Sml29623 
22773859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
22783859Sml29623 	if (nxgep->niu_type == N2_NIU) {
22796495Sspeer 		uint32_t rx_buf_alloc_size = (nxgep->rx_default_block_size *
22806495Sspeer 		    (nxge_port_rbr_size + nxge_port_rbr_spare_size));
22816495Sspeer 
22823859Sml29623 		if (!ISP2(rx_buf_alloc_size)) {
22833859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
22846512Ssowmini 			    "==> nxge_alloc_rx_mem_pool: "
22856512Ssowmini 			    " must be power of 2"));
22863859Sml29623 			status |= (NXGE_ERROR | NXGE_DDI_FAILED);
22873859Sml29623 			goto nxge_alloc_rx_mem_pool_exit;
22883859Sml29623 		}
22893859Sml29623 
22903859Sml29623 		if (rx_buf_alloc_size > (1 << 22)) {
22913859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
22926512Ssowmini 			    "==> nxge_alloc_rx_mem_pool: "
22936512Ssowmini 			    " limit size to 4M"));
22943859Sml29623 			status |= (NXGE_ERROR | NXGE_DDI_FAILED);
22953859Sml29623 			goto nxge_alloc_rx_mem_pool_exit;
22963859Sml29623 		}
22973859Sml29623 
22983859Sml29623 		if (rx_cntl_alloc_size < 0x2000) {
22993859Sml29623 			rx_cntl_alloc_size = 0x2000;
23003859Sml29623 		}
23013859Sml29623 	}
23023859Sml29623 #endif
23033859Sml29623 	nxgep->nxge_port_rbr_size = nxge_port_rbr_size;
23043859Sml29623 	nxgep->nxge_port_rcr_size = nxge_port_rcr_size;
23056495Sspeer 	nxgep->nxge_port_rbr_spare_size = nxge_port_rbr_spare_size;
23066495Sspeer 	nxgep->nxge_port_rx_cntl_alloc_size = rx_cntl_alloc_size;
23076495Sspeer 
23086495Sspeer 	dma_poolp->ndmas = p_cfgp->max_rdcs;
23093859Sml29623 	dma_poolp->num_chunks = num_chunks;
23103859Sml29623 	dma_poolp->buf_allocated = B_TRUE;
23113859Sml29623 	nxgep->rx_buf_pool_p = dma_poolp;
23123859Sml29623 	dma_poolp->dma_buf_pool_p = dma_buf_p;
23133859Sml29623 
23146495Sspeer 	dma_cntl_poolp->ndmas = p_cfgp->max_rdcs;
23153859Sml29623 	dma_cntl_poolp->buf_allocated = B_TRUE;
23163859Sml29623 	nxgep->rx_cntl_pool_p = dma_cntl_poolp;
23173859Sml29623 	dma_cntl_poolp->dma_buf_pool_p = dma_cntl_p;
23183859Sml29623 
23196495Sspeer 	/* Allocate the receive rings, too. */
23206495Sspeer 	nxgep->rx_rbr_rings =
23216512Ssowmini 	    KMEM_ZALLOC(sizeof (rx_rbr_rings_t), KM_SLEEP);
23226495Sspeer 	nxgep->rx_rbr_rings->rbr_rings =
23236512Ssowmini 	    KMEM_ZALLOC(sizeof (p_rx_rbr_ring_t) * rdc_max, KM_SLEEP);
23246495Sspeer 	nxgep->rx_rcr_rings =
23256512Ssowmini 	    KMEM_ZALLOC(sizeof (rx_rcr_rings_t), KM_SLEEP);
23266495Sspeer 	nxgep->rx_rcr_rings->rcr_rings =
23276512Ssowmini 	    KMEM_ZALLOC(sizeof (p_rx_rcr_ring_t) * rdc_max, KM_SLEEP);
23286495Sspeer 	nxgep->rx_mbox_areas_p =
23296512Ssowmini 	    KMEM_ZALLOC(sizeof (rx_mbox_areas_t), KM_SLEEP);
23306495Sspeer 	nxgep->rx_mbox_areas_p->rxmbox_areas =
23316512Ssowmini 	    KMEM_ZALLOC(sizeof (p_rx_mbox_t) * rdc_max, KM_SLEEP);
23326495Sspeer 
23336495Sspeer 	nxgep->rx_rbr_rings->ndmas = nxgep->rx_rcr_rings->ndmas =
23346495Sspeer 	    p_cfgp->max_rdcs;
23356495Sspeer 
23363859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
23376512Ssowmini 	    "<== nxge_alloc_rx_mem_pool:status 0x%08x", status));
23383859Sml29623 
23393859Sml29623 nxge_alloc_rx_mem_pool_exit:
23406495Sspeer 	return (status);
23416495Sspeer }
23426495Sspeer 
23436495Sspeer /*
23446495Sspeer  * nxge_alloc_rxb
23456495Sspeer  *
23466495Sspeer  *	Allocate buffers for an RDC.
23476495Sspeer  *
23486495Sspeer  * Arguments:
23496495Sspeer  * 	nxgep
23506495Sspeer  * 	channel	The channel to map into our kernel space.
23516495Sspeer  *
23526495Sspeer  * Notes:
23536495Sspeer  *
23546495Sspeer  * NPI function calls:
23556495Sspeer  *
23566495Sspeer  * NXGE function calls:
23576495Sspeer  *
23586495Sspeer  * Registers accessed:
23596495Sspeer  *
23606495Sspeer  * Context:
23616495Sspeer  *
23626495Sspeer  * Taking apart:
23636495Sspeer  *
23646495Sspeer  * Open questions:
23656495Sspeer  *
23666495Sspeer  */
23676495Sspeer nxge_status_t
23686495Sspeer nxge_alloc_rxb(
23696495Sspeer 	p_nxge_t nxgep,
23706495Sspeer 	int channel)
23716495Sspeer {
23726495Sspeer 	size_t			rx_buf_alloc_size;
23736495Sspeer 	nxge_status_t		status = NXGE_OK;
23746495Sspeer 
23756495Sspeer 	nxge_dma_common_t	**data;
23766495Sspeer 	nxge_dma_common_t	**control;
23776495Sspeer 	uint32_t 		*num_chunks;
23786495Sspeer 
23796495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rbb"));
23806495Sspeer 
23816495Sspeer 	/*
23826495Sspeer 	 * Allocate memory for the receive buffers and descriptor rings.
23836495Sspeer 	 * Replace these allocation functions with the interface functions
23846495Sspeer 	 * provided by the partition manager if/when they are available.
23856495Sspeer 	 */
23866495Sspeer 
23876495Sspeer 	/*
23886495Sspeer 	 * Allocate memory for the receive buffer blocks.
23896495Sspeer 	 */
23906495Sspeer 	rx_buf_alloc_size = (nxgep->rx_default_block_size *
23916512Ssowmini 	    (nxgep->nxge_port_rbr_size + nxgep->nxge_port_rbr_spare_size));
23926495Sspeer 
23936495Sspeer 	data = &nxgep->rx_buf_pool_p->dma_buf_pool_p[channel];
23946495Sspeer 	num_chunks = &nxgep->rx_buf_pool_p->num_chunks[channel];
23956495Sspeer 
23966495Sspeer 	if ((status = nxge_alloc_rx_buf_dma(
23976495Sspeer 	    nxgep, channel, data, rx_buf_alloc_size,
23986495Sspeer 	    nxgep->rx_default_block_size, num_chunks)) != NXGE_OK) {
23996495Sspeer 		return (status);
24006495Sspeer 	}
24016495Sspeer 
24026495Sspeer 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "<== nxge_alloc_rxb(): "
24036495Sspeer 	    "dma %d dma_buf_p %llx &dma_buf_p %llx", channel, *data, data));
24046495Sspeer 
24056495Sspeer 	/*
24066495Sspeer 	 * Allocate memory for descriptor rings and mailbox.
24076495Sspeer 	 */
24086495Sspeer 	control = &nxgep->rx_cntl_pool_p->dma_buf_pool_p[channel];
24096495Sspeer 
24106495Sspeer 	if ((status = nxge_alloc_rx_cntl_dma(
24116495Sspeer 	    nxgep, channel, control, nxgep->nxge_port_rx_cntl_alloc_size))
24126495Sspeer 	    != NXGE_OK) {
24136495Sspeer 		nxge_free_rx_cntl_dma(nxgep, *control);
24146495Sspeer 		(*data)->buf_alloc_state |= BUF_ALLOCATED_WAIT_FREE;
24156495Sspeer 		nxge_free_rx_buf_dma(nxgep, *data, *num_chunks);
24166495Sspeer 		return (status);
24176495Sspeer 	}
24186495Sspeer 
24193859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
24206495Sspeer 	    "<== nxge_alloc_rx_mem_pool:status 0x%08x", status));
24213859Sml29623 
24223859Sml29623 	return (status);
24233859Sml29623 }
24243859Sml29623 
24256495Sspeer void
24266495Sspeer nxge_free_rxb(
24276495Sspeer 	p_nxge_t nxgep,
24286495Sspeer 	int channel)
24296495Sspeer {
24306495Sspeer 	nxge_dma_common_t	*data;
24316495Sspeer 	nxge_dma_common_t	*control;
24326495Sspeer 	uint32_t 		num_chunks;
24336495Sspeer 
24346495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rbb"));
24356495Sspeer 
24366495Sspeer 	data = nxgep->rx_buf_pool_p->dma_buf_pool_p[channel];
24376495Sspeer 	num_chunks = nxgep->rx_buf_pool_p->num_chunks[channel];
24386495Sspeer 	nxge_free_rx_buf_dma(nxgep, data, num_chunks);
24396495Sspeer 
24406495Sspeer 	nxgep->rx_buf_pool_p->dma_buf_pool_p[channel] = 0;
24416495Sspeer 	nxgep->rx_buf_pool_p->num_chunks[channel] = 0;
24426495Sspeer 
24436495Sspeer 	control = nxgep->rx_cntl_pool_p->dma_buf_pool_p[channel];
24446495Sspeer 	nxge_free_rx_cntl_dma(nxgep, control);
24456495Sspeer 
24466495Sspeer 	nxgep->rx_cntl_pool_p->dma_buf_pool_p[channel] = 0;
24476495Sspeer 
24486495Sspeer 	KMEM_FREE(data, sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK);
24496495Sspeer 	KMEM_FREE(control, sizeof (nxge_dma_common_t));
24506495Sspeer 
24516495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_alloc_rbb"));
24526495Sspeer }
24536495Sspeer 
24543859Sml29623 static void
24553859Sml29623 nxge_free_rx_mem_pool(p_nxge_t nxgep)
24563859Sml29623 {
24576495Sspeer 	int rdc_max = NXGE_MAX_RDCS;
24583859Sml29623 
24593859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_free_rx_mem_pool"));
24603859Sml29623 
24616495Sspeer 	if (!nxgep->rx_buf_pool_p || !nxgep->rx_buf_pool_p->buf_allocated) {
24623859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
24636512Ssowmini 		    "<== nxge_free_rx_mem_pool "
24646512Ssowmini 		    "(null rx buf pool or buf not allocated"));
24653859Sml29623 		return;
24663859Sml29623 	}
24676495Sspeer 	if (!nxgep->rx_cntl_pool_p || !nxgep->rx_cntl_pool_p->buf_allocated) {
24683859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
24696512Ssowmini 		    "<== nxge_free_rx_mem_pool "
24706512Ssowmini 		    "(null rx cntl buf pool or cntl buf not allocated"));
24713859Sml29623 		return;
24723859Sml29623 	}
24733859Sml29623 
24746495Sspeer 	KMEM_FREE(nxgep->rx_cntl_pool_p->dma_buf_pool_p,
24756495Sspeer 	    sizeof (p_nxge_dma_common_t) * rdc_max);
24766495Sspeer 	KMEM_FREE(nxgep->rx_cntl_pool_p, sizeof (nxge_dma_pool_t));
24776495Sspeer 
24786495Sspeer 	KMEM_FREE(nxgep->rx_buf_pool_p->num_chunks,
24796495Sspeer 	    sizeof (uint32_t) * rdc_max);
24806495Sspeer 	KMEM_FREE(nxgep->rx_buf_pool_p->dma_buf_pool_p,
24816495Sspeer 	    sizeof (p_nxge_dma_common_t) * rdc_max);
24826495Sspeer 	KMEM_FREE(nxgep->rx_buf_pool_p, sizeof (nxge_dma_pool_t));
24836495Sspeer 
24846495Sspeer 	nxgep->rx_buf_pool_p = 0;
24856495Sspeer 	nxgep->rx_cntl_pool_p = 0;
24866495Sspeer 
24876495Sspeer 	KMEM_FREE(nxgep->rx_rbr_rings->rbr_rings,
24886495Sspeer 	    sizeof (p_rx_rbr_ring_t) * rdc_max);
24896495Sspeer 	KMEM_FREE(nxgep->rx_rbr_rings, sizeof (rx_rbr_rings_t));
24906495Sspeer 	KMEM_FREE(nxgep->rx_rcr_rings->rcr_rings,
24916495Sspeer 	    sizeof (p_rx_rcr_ring_t) * rdc_max);
24926495Sspeer 	KMEM_FREE(nxgep->rx_rcr_rings, sizeof (rx_rcr_rings_t));
24936495Sspeer 	KMEM_FREE(nxgep->rx_mbox_areas_p->rxmbox_areas,
24946495Sspeer 	    sizeof (p_rx_mbox_t) * rdc_max);
24956495Sspeer 	KMEM_FREE(nxgep->rx_mbox_areas_p, sizeof (rx_mbox_areas_t));
24966495Sspeer 
24976495Sspeer 	nxgep->rx_rbr_rings = 0;
24986495Sspeer 	nxgep->rx_rcr_rings = 0;
24996495Sspeer 	nxgep->rx_mbox_areas_p = 0;
25003859Sml29623 
25013859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "<== nxge_free_rx_mem_pool"));
25023859Sml29623 }
25033859Sml29623 
25043859Sml29623 
25053859Sml29623 static nxge_status_t
25063859Sml29623 nxge_alloc_rx_buf_dma(p_nxge_t nxgep, uint16_t dma_channel,
25073859Sml29623 	p_nxge_dma_common_t *dmap,
25083859Sml29623 	size_t alloc_size, size_t block_size, uint32_t *num_chunks)
25093859Sml29623 {
25103859Sml29623 	p_nxge_dma_common_t 	rx_dmap;
25113859Sml29623 	nxge_status_t		status = NXGE_OK;
25123859Sml29623 	size_t			total_alloc_size;
25133859Sml29623 	size_t			allocated = 0;
25143859Sml29623 	int			i, size_index, array_size;
25156495Sspeer 	boolean_t		use_kmem_alloc = B_FALSE;
25163859Sml29623 
25173859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rx_buf_dma"));
25183859Sml29623 
25193859Sml29623 	rx_dmap = (p_nxge_dma_common_t)
25206512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK,
25216512Ssowmini 	    KM_SLEEP);
25223859Sml29623 
25233859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
25246512Ssowmini 	    " alloc_rx_buf_dma rdc %d asize %x bsize %x bbuf %llx ",
25256512Ssowmini 	    dma_channel, alloc_size, block_size, dmap));
25263859Sml29623 
25273859Sml29623 	total_alloc_size = alloc_size;
25283859Sml29623 
25293859Sml29623 #if defined(RX_USE_RECLAIM_POST)
25303859Sml29623 	total_alloc_size = alloc_size + alloc_size/4;
25313859Sml29623 #endif
25323859Sml29623 
25333859Sml29623 	i = 0;
25343859Sml29623 	size_index = 0;
25353859Sml29623 	array_size =  sizeof (alloc_sizes)/sizeof (size_t);
25368661SSantwona.Behera@Sun.COM 	while ((size_index < array_size) &&
25378661SSantwona.Behera@Sun.COM 	    (alloc_sizes[size_index] < alloc_size))
25386512Ssowmini 		size_index++;
25393859Sml29623 	if (size_index >= array_size) {
25403859Sml29623 		size_index = array_size - 1;
25413859Sml29623 	}
25423859Sml29623 
25436495Sspeer 	/* For Neptune, use kmem_alloc if the kmem flag is set. */
25446495Sspeer 	if (nxgep->niu_type != N2_NIU && nxge_use_kmem_alloc) {
25456495Sspeer 		use_kmem_alloc = B_TRUE;
25466495Sspeer #if defined(__i386) || defined(__amd64)
25476495Sspeer 		size_index = 0;
25486495Sspeer #endif
25496495Sspeer 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
25506495Sspeer 		    "==> nxge_alloc_rx_buf_dma: "
25516495Sspeer 		    "Neptune use kmem_alloc() - size_index %d",
25526495Sspeer 		    size_index));
25536495Sspeer 	}
25546495Sspeer 
25553859Sml29623 	while ((allocated < total_alloc_size) &&
25566512Ssowmini 	    (size_index >= 0) && (i < NXGE_DMA_BLOCK)) {
25573859Sml29623 		rx_dmap[i].dma_chunk_index = i;
25583859Sml29623 		rx_dmap[i].block_size = block_size;
25593859Sml29623 		rx_dmap[i].alength = alloc_sizes[size_index];
25603859Sml29623 		rx_dmap[i].orig_alength = rx_dmap[i].alength;
25613859Sml29623 		rx_dmap[i].nblocks = alloc_sizes[size_index] / block_size;
25623859Sml29623 		rx_dmap[i].dma_channel = dma_channel;
25633859Sml29623 		rx_dmap[i].contig_alloc_type = B_FALSE;
25646495Sspeer 		rx_dmap[i].kmem_alloc_type = B_FALSE;
25656495Sspeer 		rx_dmap[i].buf_alloc_type = DDI_MEM_ALLOC;
25663859Sml29623 
25673859Sml29623 		/*
25683859Sml29623 		 * N2/NIU: data buffers must be contiguous as the driver
25693859Sml29623 		 *	   needs to call Hypervisor api to set up
25703859Sml29623 		 *	   logical pages.
25713859Sml29623 		 */
25723859Sml29623 		if ((nxgep->niu_type == N2_NIU) && (NXGE_DMA_BLOCK == 1)) {
25733859Sml29623 			rx_dmap[i].contig_alloc_type = B_TRUE;
25746495Sspeer 			rx_dmap[i].buf_alloc_type = CONTIG_MEM_ALLOC;
25756495Sspeer 		} else if (use_kmem_alloc) {
25766495Sspeer 			/* For Neptune, use kmem_alloc */
25776495Sspeer 			NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
25786495Sspeer 			    "==> nxge_alloc_rx_buf_dma: "
25796495Sspeer 			    "Neptune use kmem_alloc()"));
25806495Sspeer 			rx_dmap[i].kmem_alloc_type = B_TRUE;
25816495Sspeer 			rx_dmap[i].buf_alloc_type = KMEM_ALLOC;
25823859Sml29623 		}
25833859Sml29623 
25843859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
25856512Ssowmini 		    "alloc_rx_buf_dma rdc %d chunk %d bufp %llx size %x "
25866512Ssowmini 		    "i %d nblocks %d alength %d",
25876512Ssowmini 		    dma_channel, i, &rx_dmap[i], block_size,
25886512Ssowmini 		    i, rx_dmap[i].nblocks,
25896512Ssowmini 		    rx_dmap[i].alength));
25903859Sml29623 		status = nxge_dma_mem_alloc(nxgep, nxge_force_dma,
25916512Ssowmini 		    &nxge_rx_dma_attr,
25926512Ssowmini 		    rx_dmap[i].alength,
25936512Ssowmini 		    &nxge_dev_buf_dma_acc_attr,
25946512Ssowmini 		    DDI_DMA_READ | DDI_DMA_STREAMING,
25956512Ssowmini 		    (p_nxge_dma_common_t)(&rx_dmap[i]));
25963859Sml29623 		if (status != NXGE_OK) {
25973859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
25986495Sspeer 			    "nxge_alloc_rx_buf_dma: Alloc Failed: "
25996495Sspeer 			    "dma %d size_index %d size requested %d",
26006495Sspeer 			    dma_channel,
26016495Sspeer 			    size_index,
26026495Sspeer 			    rx_dmap[i].alength));
26033859Sml29623 			size_index--;
26043859Sml29623 		} else {
26056495Sspeer 			rx_dmap[i].buf_alloc_state = BUF_ALLOCATED;
26066495Sspeer 			NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26076495Sspeer 			    " nxge_alloc_rx_buf_dma DONE  alloc mem: "
26086495Sspeer 			    "dma %d dma_buf_p $%p kaddrp $%p alength %d "
26096495Sspeer 			    "buf_alloc_state %d alloc_type %d",
26106495Sspeer 			    dma_channel,
26116495Sspeer 			    &rx_dmap[i],
26126495Sspeer 			    rx_dmap[i].kaddrp,
26136495Sspeer 			    rx_dmap[i].alength,
26146495Sspeer 			    rx_dmap[i].buf_alloc_state,
26156495Sspeer 			    rx_dmap[i].buf_alloc_type));
26166495Sspeer 			NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26176495Sspeer 			    " alloc_rx_buf_dma allocated rdc %d "
26186495Sspeer 			    "chunk %d size %x dvma %x bufp %llx kaddrp $%p",
26196495Sspeer 			    dma_channel, i, rx_dmap[i].alength,
26206495Sspeer 			    rx_dmap[i].ioaddr_pp, &rx_dmap[i],
26216495Sspeer 			    rx_dmap[i].kaddrp));
26223859Sml29623 			i++;
26233859Sml29623 			allocated += alloc_sizes[size_index];
26243859Sml29623 		}
26253859Sml29623 	}
26263859Sml29623 
26273859Sml29623 	if (allocated < total_alloc_size) {
26285770Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
26296495Sspeer 		    "==> nxge_alloc_rx_buf_dma: not enough for channel %d "
26305770Sml29623 		    "allocated 0x%x requested 0x%x",
26315770Sml29623 		    dma_channel,
26325770Sml29623 		    allocated, total_alloc_size));
26335770Sml29623 		status = NXGE_ERROR;
26343859Sml29623 		goto nxge_alloc_rx_mem_fail1;
26353859Sml29623 	}
26363859Sml29623 
26375770Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26386495Sspeer 	    "==> nxge_alloc_rx_buf_dma: Allocated for channel %d "
26395770Sml29623 	    "allocated 0x%x requested 0x%x",
26405770Sml29623 	    dma_channel,
26415770Sml29623 	    allocated, total_alloc_size));
26425770Sml29623 
26433859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
26446512Ssowmini 	    " alloc_rx_buf_dma rdc %d allocated %d chunks",
26456512Ssowmini 	    dma_channel, i));
26463859Sml29623 	*num_chunks = i;
26473859Sml29623 	*dmap = rx_dmap;
26483859Sml29623 
26493859Sml29623 	goto nxge_alloc_rx_mem_exit;
26503859Sml29623 
26513859Sml29623 nxge_alloc_rx_mem_fail1:
26523859Sml29623 	KMEM_FREE(rx_dmap, sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK);
26533859Sml29623 
26543859Sml29623 nxge_alloc_rx_mem_exit:
26553859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
26566512Ssowmini 	    "<== nxge_alloc_rx_buf_dma status 0x%08x", status));
26573859Sml29623 
26583859Sml29623 	return (status);
26593859Sml29623 }
26603859Sml29623 
26613859Sml29623 /*ARGSUSED*/
26623859Sml29623 static void
26633859Sml29623 nxge_free_rx_buf_dma(p_nxge_t nxgep, p_nxge_dma_common_t dmap,
26643859Sml29623     uint32_t num_chunks)
26653859Sml29623 {
26663859Sml29623 	int		i;
26673859Sml29623 
26683859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26696512Ssowmini 	    "==> nxge_free_rx_buf_dma: # of chunks %d", num_chunks));
26703859Sml29623 
26716495Sspeer 	if (dmap == 0)
26726495Sspeer 		return;
26736495Sspeer 
26743859Sml29623 	for (i = 0; i < num_chunks; i++) {
26753859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26766512Ssowmini 		    "==> nxge_free_rx_buf_dma: chunk %d dmap 0x%llx",
26776512Ssowmini 		    i, dmap));
26786495Sspeer 		nxge_dma_free_rx_data_buf(dmap++);
26793859Sml29623 	}
26803859Sml29623 
26813859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_free_rx_buf_dma"));
26823859Sml29623 }
26833859Sml29623 
26843859Sml29623 /*ARGSUSED*/
26853859Sml29623 static nxge_status_t
26863859Sml29623 nxge_alloc_rx_cntl_dma(p_nxge_t nxgep, uint16_t dma_channel,
26873859Sml29623     p_nxge_dma_common_t *dmap, size_t size)
26883859Sml29623 {
26893859Sml29623 	p_nxge_dma_common_t 	rx_dmap;
26903859Sml29623 	nxge_status_t		status = NXGE_OK;
26913859Sml29623 
26923859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rx_cntl_dma"));
26933859Sml29623 
26943859Sml29623 	rx_dmap = (p_nxge_dma_common_t)
26956512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_common_t), KM_SLEEP);
26963859Sml29623 
26973859Sml29623 	rx_dmap->contig_alloc_type = B_FALSE;
26986495Sspeer 	rx_dmap->kmem_alloc_type = B_FALSE;
26993859Sml29623 
27003859Sml29623 	status = nxge_dma_mem_alloc(nxgep, nxge_force_dma,
27016512Ssowmini 	    &nxge_desc_dma_attr,
27026512Ssowmini 	    size,
27036512Ssowmini 	    &nxge_dev_desc_dma_acc_attr,
27046512Ssowmini 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
27056512Ssowmini 	    rx_dmap);
27063859Sml29623 	if (status != NXGE_OK) {
27073859Sml29623 		goto nxge_alloc_rx_cntl_dma_fail1;
27083859Sml29623 	}
27093859Sml29623 
27103859Sml29623 	*dmap = rx_dmap;
27113859Sml29623 	goto nxge_alloc_rx_cntl_dma_exit;
27123859Sml29623 
27133859Sml29623 nxge_alloc_rx_cntl_dma_fail1:
27143859Sml29623 	KMEM_FREE(rx_dmap, sizeof (nxge_dma_common_t));
27153859Sml29623 
27163859Sml29623 nxge_alloc_rx_cntl_dma_exit:
27173859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
27186512Ssowmini 	    "<== nxge_alloc_rx_cntl_dma status 0x%08x", status));
27193859Sml29623 
27203859Sml29623 	return (status);
27213859Sml29623 }
27223859Sml29623 
27233859Sml29623 /*ARGSUSED*/
27243859Sml29623 static void
27253859Sml29623 nxge_free_rx_cntl_dma(p_nxge_t nxgep, p_nxge_dma_common_t dmap)
27263859Sml29623 {
27273859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_free_rx_cntl_dma"));
27283859Sml29623 
27296495Sspeer 	if (dmap == 0)
27306495Sspeer 		return;
27316495Sspeer 
27323859Sml29623 	nxge_dma_mem_free(dmap);
27333859Sml29623 
27343859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_free_rx_cntl_dma"));
27353859Sml29623 }
27363859Sml29623 
27376495Sspeer typedef struct {
27386495Sspeer 	size_t	tx_size;
27396495Sspeer 	size_t	cr_size;
27406495Sspeer 	size_t	threshhold;
27416495Sspeer } nxge_tdc_sizes_t;
27426495Sspeer 
27436495Sspeer static
27446495Sspeer nxge_status_t
27456495Sspeer nxge_tdc_sizes(
27466495Sspeer 	nxge_t *nxgep,
27476495Sspeer 	nxge_tdc_sizes_t *sizes)
27486495Sspeer {
27496495Sspeer 	uint32_t threshhold;	/* The bcopy() threshhold */
27506495Sspeer 	size_t tx_size;		/* Transmit buffer size */
27516495Sspeer 	size_t cr_size;		/* Completion ring size */
27526495Sspeer 
27536495Sspeer 	/*
27546495Sspeer 	 * Assume that each DMA channel will be configured with the
27556495Sspeer 	 * default transmit buffer size for copying transmit data.
27566495Sspeer 	 * (If a packet is bigger than this, it will not be copied.)
27576495Sspeer 	 */
27586495Sspeer 	if (nxgep->niu_type == N2_NIU) {
27596495Sspeer 		threshhold = TX_BCOPY_SIZE;
27606495Sspeer 	} else {
27616495Sspeer 		threshhold = nxge_bcopy_thresh;
27626495Sspeer 	}
27636495Sspeer 	tx_size = nxge_tx_ring_size * threshhold;
27646495Sspeer 
27656495Sspeer 	cr_size = nxge_tx_ring_size * sizeof (tx_desc_t);
27666495Sspeer 	cr_size += sizeof (txdma_mailbox_t);
27676495Sspeer 
27686495Sspeer #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
27696495Sspeer 	if (nxgep->niu_type == N2_NIU) {
27706495Sspeer 		if (!ISP2(tx_size)) {
27716495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
27726512Ssowmini 			    "==> nxge_tdc_sizes: Tx size"
27736512Ssowmini 			    " must be power of 2"));
27746495Sspeer 			return (NXGE_ERROR);
27756495Sspeer 		}
27766495Sspeer 
27776495Sspeer 		if (tx_size > (1 << 22)) {
27786495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
27796512Ssowmini 			    "==> nxge_tdc_sizes: Tx size"
27806512Ssowmini 			    " limited to 4M"));
27816495Sspeer 			return (NXGE_ERROR);
27826495Sspeer 		}
27836495Sspeer 
27846495Sspeer 		if (cr_size < 0x2000)
27856495Sspeer 			cr_size = 0x2000;
27866495Sspeer 	}
27876495Sspeer #endif
27886495Sspeer 
27896495Sspeer 	sizes->threshhold = threshhold;
27906495Sspeer 	sizes->tx_size = tx_size;
27916495Sspeer 	sizes->cr_size = cr_size;
27926495Sspeer 
27936495Sspeer 	return (NXGE_OK);
27946495Sspeer }
27956495Sspeer /*
27966495Sspeer  * nxge_alloc_txb
27976495Sspeer  *
27986495Sspeer  *	Allocate buffers for an TDC.
27996495Sspeer  *
28006495Sspeer  * Arguments:
28016495Sspeer  * 	nxgep
28026495Sspeer  * 	channel	The channel to map into our kernel space.
28036495Sspeer  *
28046495Sspeer  * Notes:
28056495Sspeer  *
28066495Sspeer  * NPI function calls:
28076495Sspeer  *
28086495Sspeer  * NXGE function calls:
28096495Sspeer  *
28106495Sspeer  * Registers accessed:
28116495Sspeer  *
28126495Sspeer  * Context:
28136495Sspeer  *
28146495Sspeer  * Taking apart:
28156495Sspeer  *
28166495Sspeer  * Open questions:
28176495Sspeer  *
28186495Sspeer  */
28196495Sspeer nxge_status_t
28206495Sspeer nxge_alloc_txb(
28216495Sspeer 	p_nxge_t nxgep,
28226495Sspeer 	int channel)
28236495Sspeer {
28246495Sspeer 	nxge_dma_common_t	**dma_buf_p;
28256495Sspeer 	nxge_dma_common_t	**dma_cntl_p;
28266495Sspeer 	uint32_t 		*num_chunks;
28276495Sspeer 	nxge_status_t		status = NXGE_OK;
28286495Sspeer 
28296495Sspeer 	nxge_tdc_sizes_t	sizes;
28306495Sspeer 
28316495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_tbb"));
28326495Sspeer 
28336495Sspeer 	if (nxge_tdc_sizes(nxgep, &sizes) != NXGE_OK)
28346495Sspeer 		return (NXGE_ERROR);
28356495Sspeer 
28366495Sspeer 	/*
28376495Sspeer 	 * Allocate memory for transmit buffers and descriptor rings.
28386495Sspeer 	 * Replace these allocation functions with the interface functions
28396495Sspeer 	 * provided by the partition manager Real Soon Now.
28406495Sspeer 	 */
28416495Sspeer 	dma_buf_p = &nxgep->tx_buf_pool_p->dma_buf_pool_p[channel];
28426495Sspeer 	num_chunks = &nxgep->tx_buf_pool_p->num_chunks[channel];
28436495Sspeer 
28446495Sspeer 	dma_cntl_p = &nxgep->tx_cntl_pool_p->dma_buf_pool_p[channel];
28456495Sspeer 
28466495Sspeer 	/*
28476495Sspeer 	 * Allocate memory for transmit buffers and descriptor rings.
28486495Sspeer 	 * Replace allocation functions with interface functions provided
28496495Sspeer 	 * by the partition manager when it is available.
28506495Sspeer 	 *
28516495Sspeer 	 * Allocate memory for the transmit buffer pool.
28526495Sspeer 	 */
28536495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
28546512Ssowmini 	    "sizes: tx: %ld, cr:%ld, th:%ld",
28556512Ssowmini 	    sizes.tx_size, sizes.cr_size, sizes.threshhold));
28566495Sspeer 
28576495Sspeer 	*num_chunks = 0;
28586495Sspeer 	status = nxge_alloc_tx_buf_dma(nxgep, channel, dma_buf_p,
28596495Sspeer 	    sizes.tx_size, sizes.threshhold, num_chunks);
28606495Sspeer 	if (status != NXGE_OK) {
28616495Sspeer 		cmn_err(CE_NOTE, "nxge_alloc_tx_buf_dma failed!");
28626495Sspeer 		return (status);
28636495Sspeer 	}
28646495Sspeer 
28656495Sspeer 	/*
28666495Sspeer 	 * Allocate memory for descriptor rings and mailbox.
28676495Sspeer 	 */
28686495Sspeer 	status = nxge_alloc_tx_cntl_dma(nxgep, channel, dma_cntl_p,
28696495Sspeer 	    sizes.cr_size);
28706495Sspeer 	if (status != NXGE_OK) {
28716495Sspeer 		nxge_free_tx_buf_dma(nxgep, *dma_buf_p, *num_chunks);
28726495Sspeer 		cmn_err(CE_NOTE, "nxge_alloc_tx_cntl_dma failed!");
28736495Sspeer 		return (status);
28746495Sspeer 	}
28756495Sspeer 
28766495Sspeer 	return (NXGE_OK);
28776495Sspeer }
28786495Sspeer 
28796495Sspeer void
28806495Sspeer nxge_free_txb(
28816495Sspeer 	p_nxge_t nxgep,
28826495Sspeer 	int channel)
28836495Sspeer {
28846495Sspeer 	nxge_dma_common_t	*data;
28856495Sspeer 	nxge_dma_common_t	*control;
28866495Sspeer 	uint32_t 		num_chunks;
28876495Sspeer 
28886495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_free_txb"));
28896495Sspeer 
28906495Sspeer 	data = nxgep->tx_buf_pool_p->dma_buf_pool_p[channel];
28916495Sspeer 	num_chunks = nxgep->tx_buf_pool_p->num_chunks[channel];
28926495Sspeer 	nxge_free_tx_buf_dma(nxgep, data, num_chunks);
28936495Sspeer 
28946495Sspeer 	nxgep->tx_buf_pool_p->dma_buf_pool_p[channel] = 0;
28956495Sspeer 	nxgep->tx_buf_pool_p->num_chunks[channel] = 0;
28966495Sspeer 
28976495Sspeer 	control = nxgep->tx_cntl_pool_p->dma_buf_pool_p[channel];
28986495Sspeer 	nxge_free_tx_cntl_dma(nxgep, control);
28996495Sspeer 
29006495Sspeer 	nxgep->tx_cntl_pool_p->dma_buf_pool_p[channel] = 0;
29016495Sspeer 
29026495Sspeer 	KMEM_FREE(data, sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK);
29036495Sspeer 	KMEM_FREE(control, sizeof (nxge_dma_common_t));
29046495Sspeer 
29056495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_free_txb"));
29066495Sspeer }
29076495Sspeer 
29086495Sspeer /*
29096495Sspeer  * nxge_alloc_tx_mem_pool
29106495Sspeer  *
29116495Sspeer  *	This function allocates all of the per-port TDC control data structures.
29126495Sspeer  *	The per-channel (TDC) data structures are allocated when needed.
29136495Sspeer  *
29146495Sspeer  * Arguments:
29156495Sspeer  * 	nxgep
29166495Sspeer  *
29176495Sspeer  * Notes:
29186495Sspeer  *
29196495Sspeer  * Context:
29206495Sspeer  *	Any domain
29216495Sspeer  */
29226495Sspeer nxge_status_t
29233859Sml29623 nxge_alloc_tx_mem_pool(p_nxge_t nxgep)
29243859Sml29623 {
29256495Sspeer 	nxge_hw_pt_cfg_t	*p_cfgp;
29266495Sspeer 	nxge_dma_pool_t		*dma_poolp;
29276495Sspeer 	nxge_dma_common_t	**dma_buf_p;
29286495Sspeer 	nxge_dma_pool_t		*dma_cntl_poolp;
29296495Sspeer 	nxge_dma_common_t	**dma_cntl_p;
29303859Sml29623 	uint32_t		*num_chunks; /* per dma */
29316495Sspeer 	int			tdc_max;
29323859Sml29623 
29333859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "==> nxge_alloc_tx_mem_pool"));
29343859Sml29623 
29356495Sspeer 	p_cfgp = &nxgep->pt_config.hw_config;
29366495Sspeer 	tdc_max = NXGE_MAX_TDCS;
29376495Sspeer 
29383859Sml29623 	/*
29393859Sml29623 	 * Allocate memory for each transmit DMA channel.
29403859Sml29623 	 */
29413859Sml29623 	dma_poolp = (p_nxge_dma_pool_t)KMEM_ZALLOC(sizeof (nxge_dma_pool_t),
29426512Ssowmini 	    KM_SLEEP);
29433859Sml29623 	dma_buf_p = (p_nxge_dma_common_t *)KMEM_ZALLOC(
29446512Ssowmini 	    sizeof (p_nxge_dma_common_t) * tdc_max, KM_SLEEP);
29453859Sml29623 
29463859Sml29623 	dma_cntl_poolp = (p_nxge_dma_pool_t)
29476512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_pool_t), KM_SLEEP);
29483859Sml29623 	dma_cntl_p = (p_nxge_dma_common_t *)KMEM_ZALLOC(
29496512Ssowmini 	    sizeof (p_nxge_dma_common_t) * tdc_max, KM_SLEEP);
29503859Sml29623 
29515770Sml29623 	if (nxge_tx_ring_size > TDC_DEFAULT_MAX) {
29525770Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM_CTL,
29535770Sml29623 		    "nxge_alloc_tx_mem_pool: TDC too high %d, "
29545770Sml29623 		    "set to default %d",
29555770Sml29623 		    nxge_tx_ring_size, TDC_DEFAULT_MAX));
29565770Sml29623 		nxge_tx_ring_size = TDC_DEFAULT_MAX;
29575770Sml29623 	}
29585770Sml29623 
29593859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
29603859Sml29623 	/*
29613859Sml29623 	 * N2/NIU has limitation on the descriptor sizes (contiguous
29623859Sml29623 	 * memory allocation on data buffers to 4M (contig_mem_alloc)
29633859Sml29623 	 * and little endian for control buffers (must use the ddi/dki mem alloc
29643859Sml29623 	 * function). The transmit ring is limited to 8K (includes the
29653859Sml29623 	 * mailbox).
29663859Sml29623 	 */
29673859Sml29623 	if (nxgep->niu_type == N2_NIU) {
29683859Sml29623 		if ((nxge_tx_ring_size > NXGE_NIU_CONTIG_TX_MAX) ||
29696512Ssowmini 		    (!ISP2(nxge_tx_ring_size))) {
29703859Sml29623 			nxge_tx_ring_size = NXGE_NIU_CONTIG_TX_MAX;
29713859Sml29623 		}
29723859Sml29623 	}
29733859Sml29623 #endif
29743859Sml29623 
29753859Sml29623 	nxgep->nxge_port_tx_ring_size = nxge_tx_ring_size;
29763859Sml29623 
29773859Sml29623 	num_chunks = (uint32_t *)KMEM_ZALLOC(
29786512Ssowmini 	    sizeof (uint32_t) * tdc_max, KM_SLEEP);
29796495Sspeer 
29806495Sspeer 	dma_poolp->ndmas = p_cfgp->tdc.owned;
29813859Sml29623 	dma_poolp->num_chunks = num_chunks;
29823859Sml29623 	dma_poolp->dma_buf_pool_p = dma_buf_p;
29833859Sml29623 	nxgep->tx_buf_pool_p = dma_poolp;
29843859Sml29623 
29856495Sspeer 	dma_poolp->buf_allocated = B_TRUE;
29866495Sspeer 
29876495Sspeer 	dma_cntl_poolp->ndmas = p_cfgp->tdc.owned;
29883859Sml29623 	dma_cntl_poolp->dma_buf_pool_p = dma_cntl_p;
29893859Sml29623 	nxgep->tx_cntl_pool_p = dma_cntl_poolp;
29903859Sml29623 
29916495Sspeer 	dma_cntl_poolp->buf_allocated = B_TRUE;
29926495Sspeer 
29936495Sspeer 	nxgep->tx_rings =
29946495Sspeer 	    KMEM_ZALLOC(sizeof (tx_rings_t), KM_SLEEP);
29956495Sspeer 	nxgep->tx_rings->rings =
29966495Sspeer 	    KMEM_ZALLOC(sizeof (p_tx_ring_t) * tdc_max, KM_SLEEP);
29976495Sspeer 	nxgep->tx_mbox_areas_p =
29986495Sspeer 	    KMEM_ZALLOC(sizeof (tx_mbox_areas_t), KM_SLEEP);
29996495Sspeer 	nxgep->tx_mbox_areas_p->txmbox_areas_p =
30006495Sspeer 	    KMEM_ZALLOC(sizeof (p_tx_mbox_t) * tdc_max, KM_SLEEP);
30016495Sspeer 
30026495Sspeer 	nxgep->tx_rings->ndmas = p_cfgp->tdc.owned;
30036495Sspeer 
30043859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL,
30056512Ssowmini 	    "==> nxge_alloc_tx_mem_pool: ndmas %d poolp->ndmas %d",
30066512Ssowmini 	    tdc_max, dma_poolp->ndmas));
30076495Sspeer 
30086495Sspeer 	return (NXGE_OK);
30093859Sml29623 }
30103859Sml29623 
30116495Sspeer nxge_status_t
30123859Sml29623 nxge_alloc_tx_buf_dma(p_nxge_t nxgep, uint16_t dma_channel,
30133859Sml29623     p_nxge_dma_common_t *dmap, size_t alloc_size,
30143859Sml29623     size_t block_size, uint32_t *num_chunks)
30153859Sml29623 {
30163859Sml29623 	p_nxge_dma_common_t 	tx_dmap;
30173859Sml29623 	nxge_status_t		status = NXGE_OK;
30183859Sml29623 	size_t			total_alloc_size;
30193859Sml29623 	size_t			allocated = 0;
30203859Sml29623 	int			i, size_index, array_size;
30213859Sml29623 
30223859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_tx_buf_dma"));
30233859Sml29623 
30243859Sml29623 	tx_dmap = (p_nxge_dma_common_t)
30256512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK,
30266512Ssowmini 	    KM_SLEEP);
30273859Sml29623 
30283859Sml29623 	total_alloc_size = alloc_size;
30293859Sml29623 	i = 0;
30303859Sml29623 	size_index = 0;
30313859Sml29623 	array_size =  sizeof (alloc_sizes) /  sizeof (size_t);
30328661SSantwona.Behera@Sun.COM 	while ((size_index < array_size) &&
30338661SSantwona.Behera@Sun.COM 	    (alloc_sizes[size_index] < alloc_size))
30343859Sml29623 		size_index++;
30353859Sml29623 	if (size_index >= array_size) {
30363859Sml29623 		size_index = array_size - 1;
30373859Sml29623 	}
30383859Sml29623 
30393859Sml29623 	while ((allocated < total_alloc_size) &&
30406512Ssowmini 	    (size_index >= 0) && (i < NXGE_DMA_BLOCK)) {
30413859Sml29623 
30423859Sml29623 		tx_dmap[i].dma_chunk_index = i;
30433859Sml29623 		tx_dmap[i].block_size = block_size;
30443859Sml29623 		tx_dmap[i].alength = alloc_sizes[size_index];
30453859Sml29623 		tx_dmap[i].orig_alength = tx_dmap[i].alength;
30463859Sml29623 		tx_dmap[i].nblocks = alloc_sizes[size_index] / block_size;
30473859Sml29623 		tx_dmap[i].dma_channel = dma_channel;
30483859Sml29623 		tx_dmap[i].contig_alloc_type = B_FALSE;
30496495Sspeer 		tx_dmap[i].kmem_alloc_type = B_FALSE;
30503859Sml29623 
30513859Sml29623 		/*
30523859Sml29623 		 * N2/NIU: data buffers must be contiguous as the driver
30533859Sml29623 		 *	   needs to call Hypervisor api to set up
30543859Sml29623 		 *	   logical pages.
30553859Sml29623 		 */
30563859Sml29623 		if ((nxgep->niu_type == N2_NIU) && (NXGE_DMA_BLOCK == 1)) {
30573859Sml29623 			tx_dmap[i].contig_alloc_type = B_TRUE;
30583859Sml29623 		}
30593859Sml29623 
30603859Sml29623 		status = nxge_dma_mem_alloc(nxgep, nxge_force_dma,
30616512Ssowmini 		    &nxge_tx_dma_attr,
30626512Ssowmini 		    tx_dmap[i].alength,
30636512Ssowmini 		    &nxge_dev_buf_dma_acc_attr,
30646512Ssowmini 		    DDI_DMA_WRITE | DDI_DMA_STREAMING,
30656512Ssowmini 		    (p_nxge_dma_common_t)(&tx_dmap[i]));
30663859Sml29623 		if (status != NXGE_OK) {
30673859Sml29623 			size_index--;
30683859Sml29623 		} else {
30693859Sml29623 			i++;
30703859Sml29623 			allocated += alloc_sizes[size_index];
30713859Sml29623 		}
30723859Sml29623 	}
30733859Sml29623 
30743859Sml29623 	if (allocated < total_alloc_size) {
30755770Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
30765770Sml29623 		    "==> nxge_alloc_tx_buf_dma: not enough channel %d: "
30775770Sml29623 		    "allocated 0x%x requested 0x%x",
30785770Sml29623 		    dma_channel,
30795770Sml29623 		    allocated, total_alloc_size));
30805770Sml29623 		status = NXGE_ERROR;
30813859Sml29623 		goto nxge_alloc_tx_mem_fail1;
30823859Sml29623 	}
30833859Sml29623 
30845770Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
30855770Sml29623 	    "==> nxge_alloc_tx_buf_dma: Allocated for channel %d: "
30865770Sml29623 	    "allocated 0x%x requested 0x%x",
30875770Sml29623 	    dma_channel,
30885770Sml29623 	    allocated, total_alloc_size));
30895770Sml29623 
30903859Sml29623 	*num_chunks = i;
30913859Sml29623 	*dmap = tx_dmap;
30923859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
30936512Ssowmini 	    "==> nxge_alloc_tx_buf_dma dmap 0x%016llx num chunks %d",
30946512Ssowmini 	    *dmap, i));
30953859Sml29623 	goto nxge_alloc_tx_mem_exit;
30963859Sml29623 
30973859Sml29623 nxge_alloc_tx_mem_fail1:
30983859Sml29623 	KMEM_FREE(tx_dmap, sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK);
30993859Sml29623 
31003859Sml29623 nxge_alloc_tx_mem_exit:
31013859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
31026512Ssowmini 	    "<== nxge_alloc_tx_buf_dma status 0x%08x", status));
31033859Sml29623 
31043859Sml29623 	return (status);
31053859Sml29623 }
31063859Sml29623 
31073859Sml29623 /*ARGSUSED*/
31083859Sml29623 static void
31093859Sml29623 nxge_free_tx_buf_dma(p_nxge_t nxgep, p_nxge_dma_common_t dmap,
31103859Sml29623     uint32_t num_chunks)
31113859Sml29623 {
31123859Sml29623 	int		i;
31133859Sml29623 
31143859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "==> nxge_free_tx_buf_dma"));
31153859Sml29623 
31166495Sspeer 	if (dmap == 0)
31176495Sspeer 		return;
31186495Sspeer 
31193859Sml29623 	for (i = 0; i < num_chunks; i++) {
31203859Sml29623 		nxge_dma_mem_free(dmap++);
31213859Sml29623 	}
31223859Sml29623 
31233859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "<== nxge_free_tx_buf_dma"));
31243859Sml29623 }
31253859Sml29623 
31263859Sml29623 /*ARGSUSED*/
31276495Sspeer nxge_status_t
31283859Sml29623 nxge_alloc_tx_cntl_dma(p_nxge_t nxgep, uint16_t dma_channel,
31293859Sml29623     p_nxge_dma_common_t *dmap, size_t size)
31303859Sml29623 {
31313859Sml29623 	p_nxge_dma_common_t 	tx_dmap;
31323859Sml29623 	nxge_status_t		status = NXGE_OK;
31333859Sml29623 
31343859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_tx_cntl_dma"));
31353859Sml29623 	tx_dmap = (p_nxge_dma_common_t)
31366512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_common_t), KM_SLEEP);
31373859Sml29623 
31383859Sml29623 	tx_dmap->contig_alloc_type = B_FALSE;
31396495Sspeer 	tx_dmap->kmem_alloc_type = B_FALSE;
31403859Sml29623 
31413859Sml29623 	status = nxge_dma_mem_alloc(nxgep, nxge_force_dma,
31426512Ssowmini 	    &nxge_desc_dma_attr,
31436512Ssowmini 	    size,
31446512Ssowmini 	    &nxge_dev_desc_dma_acc_attr,
31456512Ssowmini 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
31466512Ssowmini 	    tx_dmap);
31473859Sml29623 	if (status != NXGE_OK) {
31483859Sml29623 		goto nxge_alloc_tx_cntl_dma_fail1;
31493859Sml29623 	}
31503859Sml29623 
31513859Sml29623 	*dmap = tx_dmap;
31523859Sml29623 	goto nxge_alloc_tx_cntl_dma_exit;
31533859Sml29623 
31543859Sml29623 nxge_alloc_tx_cntl_dma_fail1:
31553859Sml29623 	KMEM_FREE(tx_dmap, sizeof (nxge_dma_common_t));
31563859Sml29623 
31573859Sml29623 nxge_alloc_tx_cntl_dma_exit:
31583859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
31596512Ssowmini 	    "<== nxge_alloc_tx_cntl_dma status 0x%08x", status));
31603859Sml29623 
31613859Sml29623 	return (status);
31623859Sml29623 }
31633859Sml29623 
31643859Sml29623 /*ARGSUSED*/
31653859Sml29623 static void
31663859Sml29623 nxge_free_tx_cntl_dma(p_nxge_t nxgep, p_nxge_dma_common_t dmap)
31673859Sml29623 {
31683859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_free_tx_cntl_dma"));
31693859Sml29623 
31706495Sspeer 	if (dmap == 0)
31716495Sspeer 		return;
31726495Sspeer 
31733859Sml29623 	nxge_dma_mem_free(dmap);
31743859Sml29623 
31753859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_free_tx_cntl_dma"));
31763859Sml29623 }
31773859Sml29623 
31786495Sspeer /*
31796495Sspeer  * nxge_free_tx_mem_pool
31806495Sspeer  *
31816495Sspeer  *	This function frees all of the per-port TDC control data structures.
31826495Sspeer  *	The per-channel (TDC) data structures are freed when the channel
31836495Sspeer  *	is stopped.
31846495Sspeer  *
31856495Sspeer  * Arguments:
31866495Sspeer  * 	nxgep
31876495Sspeer  *
31886495Sspeer  * Notes:
31896495Sspeer  *
31906495Sspeer  * Context:
31916495Sspeer  *	Any domain
31926495Sspeer  */
31933859Sml29623 static void
31943859Sml29623 nxge_free_tx_mem_pool(p_nxge_t nxgep)
31953859Sml29623 {
31966495Sspeer 	int tdc_max = NXGE_MAX_TDCS;
31976495Sspeer 
31986495Sspeer 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_free_tx_mem_pool"));
31996495Sspeer 
32006495Sspeer 	if (!nxgep->tx_buf_pool_p || !nxgep->tx_buf_pool_p->buf_allocated) {
32016495Sspeer 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
32026512Ssowmini 		    "<== nxge_free_tx_mem_pool "
32036512Ssowmini 		    "(null tx buf pool or buf not allocated"));
32043859Sml29623 		return;
32053859Sml29623 	}
32066495Sspeer 	if (!nxgep->tx_cntl_pool_p || !nxgep->tx_cntl_pool_p->buf_allocated) {
32076495Sspeer 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
32086512Ssowmini 		    "<== nxge_free_tx_mem_pool "
32096512Ssowmini 		    "(null tx cntl buf pool or cntl buf not allocated"));
32103859Sml29623 		return;
32113859Sml29623 	}
32123859Sml29623 
32136495Sspeer 	/* 1. Free the mailboxes. */
32146495Sspeer 	KMEM_FREE(nxgep->tx_mbox_areas_p->txmbox_areas_p,
32156495Sspeer 	    sizeof (p_tx_mbox_t) * tdc_max);
32166495Sspeer 	KMEM_FREE(nxgep->tx_mbox_areas_p, sizeof (tx_mbox_areas_t));
32176495Sspeer 
32186495Sspeer 	nxgep->tx_mbox_areas_p = 0;
32196495Sspeer 
32206495Sspeer 	/* 2. Free the transmit ring arrays. */
32216495Sspeer 	KMEM_FREE(nxgep->tx_rings->rings,
32226495Sspeer 	    sizeof (p_tx_ring_t) * tdc_max);
32236495Sspeer 	KMEM_FREE(nxgep->tx_rings, sizeof (tx_rings_t));
32246495Sspeer 
32256495Sspeer 	nxgep->tx_rings = 0;
32266495Sspeer 
32276495Sspeer 	/* 3. Free the completion ring data structures. */
32286495Sspeer 	KMEM_FREE(nxgep->tx_cntl_pool_p->dma_buf_pool_p,
32296495Sspeer 	    sizeof (p_nxge_dma_common_t) * tdc_max);
32306495Sspeer 	KMEM_FREE(nxgep->tx_cntl_pool_p, sizeof (nxge_dma_pool_t));
32316495Sspeer 
32326495Sspeer 	nxgep->tx_cntl_pool_p = 0;
32336495Sspeer 
32346495Sspeer 	/* 4. Free the data ring data structures. */
32356495Sspeer 	KMEM_FREE(nxgep->tx_buf_pool_p->num_chunks,
32366495Sspeer 	    sizeof (uint32_t) * tdc_max);
32376495Sspeer 	KMEM_FREE(nxgep->tx_buf_pool_p->dma_buf_pool_p,
32386495Sspeer 	    sizeof (p_nxge_dma_common_t) * tdc_max);
32396495Sspeer 	KMEM_FREE(nxgep->tx_buf_pool_p, sizeof (nxge_dma_pool_t));
32406495Sspeer 
32416495Sspeer 	nxgep->tx_buf_pool_p = 0;
32426495Sspeer 
32436495Sspeer 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "<== nxge_free_tx_mem_pool"));
32443859Sml29623 }
32453859Sml29623 
32463859Sml29623 /*ARGSUSED*/
32473859Sml29623 static nxge_status_t
32483859Sml29623 nxge_dma_mem_alloc(p_nxge_t nxgep, dma_method_t method,
32493859Sml29623 	struct ddi_dma_attr *dma_attrp,
32503859Sml29623 	size_t length, ddi_device_acc_attr_t *acc_attr_p, uint_t xfer_flags,
32513859Sml29623 	p_nxge_dma_common_t dma_p)
32523859Sml29623 {
32533859Sml29623 	caddr_t 		kaddrp;
32543859Sml29623 	int			ddi_status = DDI_SUCCESS;
32553859Sml29623 	boolean_t		contig_alloc_type;
32566495Sspeer 	boolean_t		kmem_alloc_type;
32573859Sml29623 
32583859Sml29623 	contig_alloc_type = dma_p->contig_alloc_type;
32593859Sml29623 
32603859Sml29623 	if (contig_alloc_type && (nxgep->niu_type != N2_NIU)) {
32613859Sml29623 		/*
32623859Sml29623 		 * contig_alloc_type for contiguous memory only allowed
32633859Sml29623 		 * for N2/NIU.
32643859Sml29623 		 */
32653859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
32666512Ssowmini 		    "nxge_dma_mem_alloc: alloc type not allowed (%d)",
32676512Ssowmini 		    dma_p->contig_alloc_type));
32683859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
32693859Sml29623 	}
32703859Sml29623 
32713859Sml29623 	dma_p->dma_handle = NULL;
32723859Sml29623 	dma_p->acc_handle = NULL;
32733859Sml29623 	dma_p->kaddrp = dma_p->last_kaddrp = NULL;
32743859Sml29623 	dma_p->first_ioaddr_pp = dma_p->last_ioaddr_pp = NULL;
32753859Sml29623 	ddi_status = ddi_dma_alloc_handle(nxgep->dip, dma_attrp,
32766512Ssowmini 	    DDI_DMA_DONTWAIT, NULL, &dma_p->dma_handle);
32773859Sml29623 	if (ddi_status != DDI_SUCCESS) {
32783859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
32796512Ssowmini 		    "nxge_dma_mem_alloc:ddi_dma_alloc_handle failed."));
32803859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
32813859Sml29623 	}
32823859Sml29623 
32836495Sspeer 	kmem_alloc_type = dma_p->kmem_alloc_type;
32846495Sspeer 
32853859Sml29623 	switch (contig_alloc_type) {
32863859Sml29623 	case B_FALSE:
32876495Sspeer 		switch (kmem_alloc_type) {
32886495Sspeer 		case B_FALSE:
32896495Sspeer 			ddi_status = ddi_dma_mem_alloc(dma_p->dma_handle,
32906512Ssowmini 			    length,
32916512Ssowmini 			    acc_attr_p,
32926512Ssowmini 			    xfer_flags,
32936512Ssowmini 			    DDI_DMA_DONTWAIT, 0, &kaddrp, &dma_p->alength,
32946512Ssowmini 			    &dma_p->acc_handle);
32956495Sspeer 			if (ddi_status != DDI_SUCCESS) {
32966495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
32976495Sspeer 				    "nxge_dma_mem_alloc: "
32986495Sspeer 				    "ddi_dma_mem_alloc failed"));
32996495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
33006495Sspeer 				dma_p->dma_handle = NULL;
33016495Sspeer 				return (NXGE_ERROR | NXGE_DDI_FAILED);
33026495Sspeer 			}
33036495Sspeer 			if (dma_p->alength < length) {
33046495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33056495Sspeer 				    "nxge_dma_mem_alloc:di_dma_mem_alloc "
33066495Sspeer 				    "< length."));
33076495Sspeer 				ddi_dma_mem_free(&dma_p->acc_handle);
33086495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
33096495Sspeer 				dma_p->acc_handle = NULL;
33106495Sspeer 				dma_p->dma_handle = NULL;
33116495Sspeer 				return (NXGE_ERROR);
33126495Sspeer 			}
33136495Sspeer 
33146495Sspeer 			ddi_status = ddi_dma_addr_bind_handle(dma_p->dma_handle,
33156495Sspeer 			    NULL,
33166495Sspeer 			    kaddrp, dma_p->alength, xfer_flags,
33176495Sspeer 			    DDI_DMA_DONTWAIT,
33186495Sspeer 			    0, &dma_p->dma_cookie, &dma_p->ncookies);
33196495Sspeer 			if (ddi_status != DDI_DMA_MAPPED) {
33206495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33216495Sspeer 				    "nxge_dma_mem_alloc: ddi_dma_addr_bind "
33226495Sspeer 				    "failed "
33236495Sspeer 				    "(staus 0x%x ncookies %d.)", ddi_status,
33246495Sspeer 				    dma_p->ncookies));
33256495Sspeer 				if (dma_p->acc_handle) {
33266495Sspeer 					ddi_dma_mem_free(&dma_p->acc_handle);
33276495Sspeer 					dma_p->acc_handle = NULL;
33286495Sspeer 				}
33296495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
33306495Sspeer 				dma_p->dma_handle = NULL;
33316495Sspeer 				return (NXGE_ERROR | NXGE_DDI_FAILED);
33326495Sspeer 			}
33336495Sspeer 
33346495Sspeer 			if (dma_p->ncookies != 1) {
33356495Sspeer 				NXGE_DEBUG_MSG((nxgep, DMA_CTL,
33366495Sspeer 				    "nxge_dma_mem_alloc:ddi_dma_addr_bind "
33376495Sspeer 				    "> 1 cookie"
33386495Sspeer 				    "(staus 0x%x ncookies %d.)", ddi_status,
33396495Sspeer 				    dma_p->ncookies));
33407812SMichael.Speer@Sun.COM 				(void) ddi_dma_unbind_handle(dma_p->dma_handle);
33416495Sspeer 				if (dma_p->acc_handle) {
33426495Sspeer 					ddi_dma_mem_free(&dma_p->acc_handle);
33436495Sspeer 					dma_p->acc_handle = NULL;
33446495Sspeer 				}
33456495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
33466495Sspeer 				dma_p->dma_handle = NULL;
33477812SMichael.Speer@Sun.COM 				dma_p->acc_handle = NULL;
33486495Sspeer 				return (NXGE_ERROR);
33496495Sspeer 			}
33506495Sspeer 			break;
33516495Sspeer 
33526495Sspeer 		case B_TRUE:
33536495Sspeer 			kaddrp = KMEM_ALLOC(length, KM_NOSLEEP);
33546495Sspeer 			if (kaddrp == NULL) {
33556495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33566495Sspeer 				    "nxge_dma_mem_alloc:ddi_dma_mem_alloc "
33576495Sspeer 				    "kmem alloc failed"));
33586495Sspeer 				return (NXGE_ERROR);
33596495Sspeer 			}
33606495Sspeer 
33616495Sspeer 			dma_p->alength = length;
33626495Sspeer 			ddi_status = ddi_dma_addr_bind_handle(dma_p->dma_handle,
33636495Sspeer 			    NULL, kaddrp, dma_p->alength, xfer_flags,
33646495Sspeer 			    DDI_DMA_DONTWAIT, 0,
33656495Sspeer 			    &dma_p->dma_cookie, &dma_p->ncookies);
33666495Sspeer 			if (ddi_status != DDI_DMA_MAPPED) {
33676495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33686495Sspeer 				    "nxge_dma_mem_alloc:ddi_dma_addr_bind: "
33696495Sspeer 				    "(kmem_alloc) failed kaddrp $%p length %d "
33706495Sspeer 				    "(staus 0x%x (%d) ncookies %d.)",
33716495Sspeer 				    kaddrp, length,
33726495Sspeer 				    ddi_status, ddi_status, dma_p->ncookies));
33736495Sspeer 				KMEM_FREE(kaddrp, length);
33746495Sspeer 				dma_p->acc_handle = NULL;
33756495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
33766495Sspeer 				dma_p->dma_handle = NULL;
33776495Sspeer 				dma_p->kaddrp = NULL;
33786495Sspeer 				return (NXGE_ERROR | NXGE_DDI_FAILED);
33796495Sspeer 			}
33806495Sspeer 
33816495Sspeer 			if (dma_p->ncookies != 1) {
33826495Sspeer 				NXGE_DEBUG_MSG((nxgep, DMA_CTL,
33836495Sspeer 				    "nxge_dma_mem_alloc:ddi_dma_addr_bind "
33846495Sspeer 				    "(kmem_alloc) > 1 cookie"
33856495Sspeer 				    "(staus 0x%x ncookies %d.)", ddi_status,
33866512Ssowmini 				    dma_p->ncookies));
33877812SMichael.Speer@Sun.COM 				(void) ddi_dma_unbind_handle(dma_p->dma_handle);
33886495Sspeer 				KMEM_FREE(kaddrp, length);
33896495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
33906495Sspeer 				dma_p->dma_handle = NULL;
33917812SMichael.Speer@Sun.COM 				dma_p->acc_handle = NULL;
33926495Sspeer 				dma_p->kaddrp = NULL;
33936495Sspeer 				return (NXGE_ERROR);
33943859Sml29623 			}
33956495Sspeer 
33966495Sspeer 			dma_p->kaddrp = kaddrp;
33976495Sspeer 
33986495Sspeer 			NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
33996512Ssowmini 			    "nxge_dma_mem_alloc: kmem_alloc dmap $%p "
34006512Ssowmini 			    "kaddr $%p alength %d",
34016512Ssowmini 			    dma_p,
34026512Ssowmini 			    kaddrp,
34036512Ssowmini 			    dma_p->alength));
34046495Sspeer 			break;
34053859Sml29623 		}
34063859Sml29623 		break;
34073859Sml29623 
34083859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
34093859Sml29623 	case B_TRUE:
34103859Sml29623 		kaddrp = (caddr_t)contig_mem_alloc(length);
34113859Sml29623 		if (kaddrp == NULL) {
34123859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
34136512Ssowmini 			    "nxge_dma_mem_alloc:contig_mem_alloc failed."));
34143859Sml29623 			ddi_dma_free_handle(&dma_p->dma_handle);
34153859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
34163859Sml29623 		}
34173859Sml29623 
34183859Sml29623 		dma_p->alength = length;
34193859Sml29623 		ddi_status = ddi_dma_addr_bind_handle(dma_p->dma_handle, NULL,
34206512Ssowmini 		    kaddrp, dma_p->alength, xfer_flags, DDI_DMA_DONTWAIT, 0,
34216512Ssowmini 		    &dma_p->dma_cookie, &dma_p->ncookies);
34223859Sml29623 		if (ddi_status != DDI_DMA_MAPPED) {
34233859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
34246512Ssowmini 			    "nxge_dma_mem_alloc:di_dma_addr_bind failed "
34256512Ssowmini 			    "(status 0x%x ncookies %d.)", ddi_status,
34266512Ssowmini 			    dma_p->ncookies));
34273859Sml29623 
34283859Sml29623 			NXGE_DEBUG_MSG((nxgep, DMA_CTL,
34296512Ssowmini 			    "==> nxge_dma_mem_alloc: (not mapped)"
34306512Ssowmini 			    "length %lu (0x%x) "
34316512Ssowmini 			    "free contig kaddrp $%p "
34326512Ssowmini 			    "va_to_pa $%p",
34336512Ssowmini 			    length, length,
34346512Ssowmini 			    kaddrp,
34356512Ssowmini 			    va_to_pa(kaddrp)));
34363859Sml29623 
34373859Sml29623 
34383859Sml29623 			contig_mem_free((void *)kaddrp, length);
34393859Sml29623 			ddi_dma_free_handle(&dma_p->dma_handle);
34403859Sml29623 
34413859Sml29623 			dma_p->dma_handle = NULL;
34423859Sml29623 			dma_p->acc_handle = NULL;
34433859Sml29623 			dma_p->alength = NULL;
34443859Sml29623 			dma_p->kaddrp = NULL;
34453859Sml29623 
34463859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
34473859Sml29623 		}
34483859Sml29623 
34493859Sml29623 		if (dma_p->ncookies != 1 ||
34506512Ssowmini 		    (dma_p->dma_cookie.dmac_laddress == NULL)) {
34513859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
34526512Ssowmini 			    "nxge_dma_mem_alloc:di_dma_addr_bind > 1 "
34536512Ssowmini 			    "cookie or "
34546512Ssowmini 			    "dmac_laddress is NULL $%p size %d "
34556512Ssowmini 			    " (status 0x%x ncookies %d.)",
34566512Ssowmini 			    ddi_status,
34576512Ssowmini 			    dma_p->dma_cookie.dmac_laddress,
34586512Ssowmini 			    dma_p->dma_cookie.dmac_size,
34596512Ssowmini 			    dma_p->ncookies));
34603859Sml29623 
34613859Sml29623 			contig_mem_free((void *)kaddrp, length);
34624185Sspeer 			(void) ddi_dma_unbind_handle(dma_p->dma_handle);
34633859Sml29623 			ddi_dma_free_handle(&dma_p->dma_handle);
34643859Sml29623 
34653859Sml29623 			dma_p->alength = 0;
34663859Sml29623 			dma_p->dma_handle = NULL;
34673859Sml29623 			dma_p->acc_handle = NULL;
34683859Sml29623 			dma_p->kaddrp = NULL;
34693859Sml29623 
34703859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
34713859Sml29623 		}
34723859Sml29623 		break;
34733859Sml29623 
34743859Sml29623 #else
34753859Sml29623 	case B_TRUE:
34763859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
34776512Ssowmini 		    "nxge_dma_mem_alloc: invalid alloc type for !sun4v"));
34783859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
34793859Sml29623 #endif
34803859Sml29623 	}
34813859Sml29623 
34823859Sml29623 	dma_p->kaddrp = kaddrp;
34833859Sml29623 	dma_p->last_kaddrp = (unsigned char *)kaddrp +
34846512Ssowmini 	    dma_p->alength - RXBUF_64B_ALIGNED;
34855125Sjoycey #if defined(__i386)
34865125Sjoycey 	dma_p->ioaddr_pp =
34876512Ssowmini 	    (unsigned char *)(uint32_t)dma_p->dma_cookie.dmac_laddress;
34885125Sjoycey #else
34893859Sml29623 	dma_p->ioaddr_pp = (unsigned char *)dma_p->dma_cookie.dmac_laddress;
34905125Sjoycey #endif
34913859Sml29623 	dma_p->last_ioaddr_pp =
34925125Sjoycey #if defined(__i386)
34936512Ssowmini 	    (unsigned char *)(uint32_t)dma_p->dma_cookie.dmac_laddress +
34945125Sjoycey #else
34956512Ssowmini 	    (unsigned char *)dma_p->dma_cookie.dmac_laddress +
34965125Sjoycey #endif
34976512Ssowmini 	    dma_p->alength - RXBUF_64B_ALIGNED;
34983859Sml29623 
34993859Sml29623 	NPI_DMA_ACC_HANDLE_SET(dma_p, dma_p->acc_handle);
35003859Sml29623 
35013859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
35023859Sml29623 	dma_p->orig_ioaddr_pp =
35036512Ssowmini 	    (unsigned char *)dma_p->dma_cookie.dmac_laddress;
35043859Sml29623 	dma_p->orig_alength = length;
35053859Sml29623 	dma_p->orig_kaddrp = kaddrp;
35063859Sml29623 	dma_p->orig_vatopa = (uint64_t)va_to_pa(kaddrp);
35073859Sml29623 #endif
35083859Sml29623 
35093859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_dma_mem_alloc: "
35106512Ssowmini 	    "dma buffer allocated: dma_p $%p "
35116512Ssowmini 	    "return dmac_ladress from cookie $%p cookie dmac_size %d "
35126512Ssowmini 	    "dma_p->ioaddr_p $%p "
35136512Ssowmini 	    "dma_p->orig_ioaddr_p $%p "
35146512Ssowmini 	    "orig_vatopa $%p "
35156512Ssowmini 	    "alength %d (0x%x) "
35166512Ssowmini 	    "kaddrp $%p "
35176512Ssowmini 	    "length %d (0x%x)",
35186512Ssowmini 	    dma_p,
35196512Ssowmini 	    dma_p->dma_cookie.dmac_laddress, dma_p->dma_cookie.dmac_size,
35206512Ssowmini 	    dma_p->ioaddr_pp,
35216512Ssowmini 	    dma_p->orig_ioaddr_pp,
35226512Ssowmini 	    dma_p->orig_vatopa,
35236512Ssowmini 	    dma_p->alength, dma_p->alength,
35246512Ssowmini 	    kaddrp,
35256512Ssowmini 	    length, length));
35263859Sml29623 
35273859Sml29623 	return (NXGE_OK);
35283859Sml29623 }
35293859Sml29623 
35303859Sml29623 static void
35313859Sml29623 nxge_dma_mem_free(p_nxge_dma_common_t dma_p)
35323859Sml29623 {
35333859Sml29623 	if (dma_p->dma_handle != NULL) {
35343859Sml29623 		if (dma_p->ncookies) {
35353859Sml29623 			(void) ddi_dma_unbind_handle(dma_p->dma_handle);
35363859Sml29623 			dma_p->ncookies = 0;
35373859Sml29623 		}
35383859Sml29623 		ddi_dma_free_handle(&dma_p->dma_handle);
35393859Sml29623 		dma_p->dma_handle = NULL;
35403859Sml29623 	}
35413859Sml29623 
35423859Sml29623 	if (dma_p->acc_handle != NULL) {
35433859Sml29623 		ddi_dma_mem_free(&dma_p->acc_handle);
35443859Sml29623 		dma_p->acc_handle = NULL;
35453859Sml29623 		NPI_DMA_ACC_HANDLE_SET(dma_p, NULL);
35463859Sml29623 	}
35473859Sml29623 
35483859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
35493859Sml29623 	if (dma_p->contig_alloc_type &&
35506512Ssowmini 	    dma_p->orig_kaddrp && dma_p->orig_alength) {
35513859Sml29623 		NXGE_DEBUG_MSG((NULL, DMA_CTL, "nxge_dma_mem_free: "
35526512Ssowmini 		    "kaddrp $%p (orig_kaddrp $%p)"
35536512Ssowmini 		    "mem type %d ",
35546512Ssowmini 		    "orig_alength %d "
35556512Ssowmini 		    "alength 0x%x (%d)",
35566512Ssowmini 		    dma_p->kaddrp,
35576512Ssowmini 		    dma_p->orig_kaddrp,
35586512Ssowmini 		    dma_p->contig_alloc_type,
35596512Ssowmini 		    dma_p->orig_alength,
35606512Ssowmini 		    dma_p->alength, dma_p->alength));
35613859Sml29623 
35623859Sml29623 		contig_mem_free(dma_p->orig_kaddrp, dma_p->orig_alength);
35633859Sml29623 		dma_p->orig_alength = NULL;
35643859Sml29623 		dma_p->orig_kaddrp = NULL;
35653859Sml29623 		dma_p->contig_alloc_type = B_FALSE;
35663859Sml29623 	}
35673859Sml29623 #endif
35683859Sml29623 	dma_p->kaddrp = NULL;
35693859Sml29623 	dma_p->alength = NULL;
35703859Sml29623 }
35713859Sml29623 
35726495Sspeer static void
35736495Sspeer nxge_dma_free_rx_data_buf(p_nxge_dma_common_t dma_p)
35746495Sspeer {
35756495Sspeer 	uint64_t kaddr;
35766495Sspeer 	uint32_t buf_size;
35776495Sspeer 
35786495Sspeer 	NXGE_DEBUG_MSG((NULL, DMA_CTL, "==> nxge_dma_free_rx_data_buf"));
35796495Sspeer 
35806495Sspeer 	if (dma_p->dma_handle != NULL) {
35816495Sspeer 		if (dma_p->ncookies) {
35826495Sspeer 			(void) ddi_dma_unbind_handle(dma_p->dma_handle);
35836495Sspeer 			dma_p->ncookies = 0;
35846495Sspeer 		}
35856495Sspeer 		ddi_dma_free_handle(&dma_p->dma_handle);
35866495Sspeer 		dma_p->dma_handle = NULL;
35876495Sspeer 	}
35886495Sspeer 
35896495Sspeer 	if (dma_p->acc_handle != NULL) {
35906495Sspeer 		ddi_dma_mem_free(&dma_p->acc_handle);
35916495Sspeer 		dma_p->acc_handle = NULL;
35926495Sspeer 		NPI_DMA_ACC_HANDLE_SET(dma_p, NULL);
35936495Sspeer 	}
35946495Sspeer 
35956495Sspeer 	NXGE_DEBUG_MSG((NULL, DMA_CTL,
35966495Sspeer 	    "==> nxge_dma_free_rx_data_buf: dmap $%p buf_alloc_state %d",
35976495Sspeer 	    dma_p,
35986495Sspeer 	    dma_p->buf_alloc_state));
35996495Sspeer 
36006495Sspeer 	if (!(dma_p->buf_alloc_state & BUF_ALLOCATED_WAIT_FREE)) {
36016495Sspeer 		NXGE_DEBUG_MSG((NULL, DMA_CTL,
36026495Sspeer 		    "<== nxge_dma_free_rx_data_buf: "
36036495Sspeer 		    "outstanding data buffers"));
36046495Sspeer 		return;
36056495Sspeer 	}
36066495Sspeer 
36076495Sspeer #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
36086495Sspeer 	if (dma_p->contig_alloc_type &&
36096512Ssowmini 	    dma_p->orig_kaddrp && dma_p->orig_alength) {
36106495Sspeer 		NXGE_DEBUG_MSG((NULL, DMA_CTL, "nxge_dma_free_rx_data_buf: "
36116495Sspeer 		    "kaddrp $%p (orig_kaddrp $%p)"
36126495Sspeer 		    "mem type %d ",
36136495Sspeer 		    "orig_alength %d "
36146495Sspeer 		    "alength 0x%x (%d)",
36156495Sspeer 		    dma_p->kaddrp,
36166495Sspeer 		    dma_p->orig_kaddrp,
36176495Sspeer 		    dma_p->contig_alloc_type,
36186495Sspeer 		    dma_p->orig_alength,
36196495Sspeer 		    dma_p->alength, dma_p->alength));
36206495Sspeer 
36216495Sspeer 		kaddr = (uint64_t)dma_p->orig_kaddrp;
36226495Sspeer 		buf_size = dma_p->orig_alength;
36236495Sspeer 		nxge_free_buf(CONTIG_MEM_ALLOC, kaddr, buf_size);
36246495Sspeer 		dma_p->orig_alength = NULL;
36256495Sspeer 		dma_p->orig_kaddrp = NULL;
36266495Sspeer 		dma_p->contig_alloc_type = B_FALSE;
36276495Sspeer 		dma_p->kaddrp = NULL;
36286495Sspeer 		dma_p->alength = NULL;
36296495Sspeer 		return;
36306495Sspeer 	}
36316495Sspeer #endif
36326495Sspeer 
36336495Sspeer 	if (dma_p->kmem_alloc_type) {
36346495Sspeer 		NXGE_DEBUG_MSG((NULL, DMA_CTL,
36356495Sspeer 		    "nxge_dma_free_rx_data_buf: free kmem "
36366512Ssowmini 		    "kaddrp $%p (orig_kaddrp $%p)"
36376512Ssowmini 		    "alloc type %d "
36386512Ssowmini 		    "orig_alength %d "
36396512Ssowmini 		    "alength 0x%x (%d)",
36406512Ssowmini 		    dma_p->kaddrp,
36416512Ssowmini 		    dma_p->orig_kaddrp,
36426512Ssowmini 		    dma_p->kmem_alloc_type,
36436512Ssowmini 		    dma_p->orig_alength,
36446512Ssowmini 		    dma_p->alength, dma_p->alength));
36456495Sspeer #if defined(__i386)
36466495Sspeer 		kaddr = (uint64_t)(uint32_t)dma_p->kaddrp;
36476495Sspeer #else
36486495Sspeer 		kaddr = (uint64_t)dma_p->kaddrp;
36496495Sspeer #endif
36506495Sspeer 		buf_size = dma_p->orig_alength;
36516495Sspeer 		NXGE_DEBUG_MSG((NULL, DMA_CTL,
36526495Sspeer 		    "nxge_dma_free_rx_data_buf: free dmap $%p "
36536495Sspeer 		    "kaddr $%p buf_size %d",
36546495Sspeer 		    dma_p,
36556495Sspeer 		    kaddr, buf_size));
36566495Sspeer 		nxge_free_buf(KMEM_ALLOC, kaddr, buf_size);
36576495Sspeer 		dma_p->alength = 0;
36586495Sspeer 		dma_p->orig_alength = 0;
36596495Sspeer 		dma_p->kaddrp = NULL;
36606495Sspeer 		dma_p->kmem_alloc_type = B_FALSE;
36616495Sspeer 	}
36626495Sspeer 
36636495Sspeer 	NXGE_DEBUG_MSG((NULL, DMA_CTL, "<== nxge_dma_free_rx_data_buf"));
36646495Sspeer }
36656495Sspeer 
36663859Sml29623 /*
36673859Sml29623  *	nxge_m_start() -- start transmitting and receiving.
36683859Sml29623  *
36693859Sml29623  *	This function is called by the MAC layer when the first
36703859Sml29623  *	stream is open to prepare the hardware ready for sending
36713859Sml29623  *	and transmitting packets.
36723859Sml29623  */
36733859Sml29623 static int
36743859Sml29623 nxge_m_start(void *arg)
36753859Sml29623 {
36763859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
36773859Sml29623 
36783859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_start"));
36793859Sml29623 
36809232SMichael.Speer@Sun.COM 	/*
36819232SMichael.Speer@Sun.COM 	 * Are we already started?
36829232SMichael.Speer@Sun.COM 	 */
36839232SMichael.Speer@Sun.COM 	if (nxgep->nxge_mac_state == NXGE_MAC_STARTED) {
36849232SMichael.Speer@Sun.COM 		return (0);
36859232SMichael.Speer@Sun.COM 	}
36869232SMichael.Speer@Sun.COM 
36876705Sml29623 	if (nxge_peu_reset_enable && !nxgep->nxge_link_poll_timerid) {
36886705Sml29623 		(void) nxge_link_monitor(nxgep, LINK_MONITOR_START);
36896705Sml29623 	}
36906705Sml29623 
36919232SMichael.Speer@Sun.COM 	/*
36929232SMichael.Speer@Sun.COM 	 * Make sure RX MAC is disabled while we initialize.
36939232SMichael.Speer@Sun.COM 	 */
36949232SMichael.Speer@Sun.COM 	if (!isLDOMguest(nxgep)) {
36959232SMichael.Speer@Sun.COM 		(void) nxge_rx_mac_disable(nxgep);
36969232SMichael.Speer@Sun.COM 	}
36979232SMichael.Speer@Sun.COM 
36989232SMichael.Speer@Sun.COM 	/*
36999232SMichael.Speer@Sun.COM 	 * Grab the global lock.
37009232SMichael.Speer@Sun.COM 	 */
37013859Sml29623 	MUTEX_ENTER(nxgep->genlock);
37029232SMichael.Speer@Sun.COM 
37039232SMichael.Speer@Sun.COM 	/*
37049232SMichael.Speer@Sun.COM 	 * Initialize the driver and hardware.
37059232SMichael.Speer@Sun.COM 	 */
37063859Sml29623 	if (nxge_init(nxgep) != NXGE_OK) {
37073859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
37086512Ssowmini 		    "<== nxge_m_start: initialization failed"));
37093859Sml29623 		MUTEX_EXIT(nxgep->genlock);
37103859Sml29623 		return (EIO);
37113859Sml29623 	}
37123859Sml29623 
37133859Sml29623 	/*
37143859Sml29623 	 * Start timer to check the system error and tx hangs
37153859Sml29623 	 */
37166495Sspeer 	if (!isLDOMguest(nxgep))
37176495Sspeer 		nxgep->nxge_timerid = nxge_start_timer(nxgep,
37186495Sspeer 		    nxge_check_hw_state, NXGE_CHECK_TIMER);
37199232SMichael.Speer@Sun.COM #if defined(sun4v)
37206495Sspeer 	else
37216495Sspeer 		nxge_hio_start_timer(nxgep);
37226495Sspeer #endif
37233859Sml29623 
37243859Sml29623 	nxgep->link_notify = B_TRUE;
37253859Sml29623 	nxgep->nxge_mac_state = NXGE_MAC_STARTED;
37263859Sml29623 
37279232SMichael.Speer@Sun.COM 	/*
37289232SMichael.Speer@Sun.COM 	 * Let the global lock go, since we are intialized.
37299232SMichael.Speer@Sun.COM 	 */
37303859Sml29623 	MUTEX_EXIT(nxgep->genlock);
37319232SMichael.Speer@Sun.COM 
37329232SMichael.Speer@Sun.COM 	/*
37339232SMichael.Speer@Sun.COM 	 * Let the MAC start receiving packets, now that
37349232SMichael.Speer@Sun.COM 	 * we are initialized.
37359232SMichael.Speer@Sun.COM 	 */
37369232SMichael.Speer@Sun.COM 	if (!isLDOMguest(nxgep)) {
37379232SMichael.Speer@Sun.COM 		if (nxge_rx_mac_enable(nxgep) != NXGE_OK) {
37389232SMichael.Speer@Sun.COM 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
37399232SMichael.Speer@Sun.COM 			    "<== nxge_m_start: enable of RX mac failed"));
37409232SMichael.Speer@Sun.COM 			return (EIO);
37419232SMichael.Speer@Sun.COM 		}
37429232SMichael.Speer@Sun.COM 
37439232SMichael.Speer@Sun.COM 		/*
37449232SMichael.Speer@Sun.COM 		 * Enable hardware interrupts.
37459232SMichael.Speer@Sun.COM 		 */
37469232SMichael.Speer@Sun.COM 		nxge_intr_hw_enable(nxgep);
37479232SMichael.Speer@Sun.COM 	}
37489232SMichael.Speer@Sun.COM #if defined(sun4v)
37499232SMichael.Speer@Sun.COM 	else {
37509232SMichael.Speer@Sun.COM 		/*
37519232SMichael.Speer@Sun.COM 		 * In guest domain we enable RDCs and their interrupts as
37529232SMichael.Speer@Sun.COM 		 * the last step.
37539232SMichael.Speer@Sun.COM 		 */
37549232SMichael.Speer@Sun.COM 		if (nxge_hio_rdc_enable(nxgep) != NXGE_OK) {
37559232SMichael.Speer@Sun.COM 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
37569232SMichael.Speer@Sun.COM 			    "<== nxge_m_start: enable of RDCs failed"));
37579232SMichael.Speer@Sun.COM 			return (EIO);
37589232SMichael.Speer@Sun.COM 		}
37599232SMichael.Speer@Sun.COM 
37609232SMichael.Speer@Sun.COM 		if (nxge_hio_rdc_intr_arm(nxgep, B_TRUE) != NXGE_OK) {
37619232SMichael.Speer@Sun.COM 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
37629232SMichael.Speer@Sun.COM 			    "<== nxge_m_start: intrs enable for RDCs failed"));
37639232SMichael.Speer@Sun.COM 			return (EIO);
37649232SMichael.Speer@Sun.COM 		}
37659232SMichael.Speer@Sun.COM 	}
37669232SMichael.Speer@Sun.COM #endif
37673859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_start"));
37683859Sml29623 	return (0);
37693859Sml29623 }
37703859Sml29623 
37718275SEric Cheng static boolean_t
37728275SEric Cheng nxge_check_groups_stopped(p_nxge_t nxgep)
37738275SEric Cheng {
37748275SEric Cheng 	int	i;
37758275SEric Cheng 
37768275SEric Cheng 	for (i = 0; i < NXGE_MAX_RDC_GROUPS; i++) {
37778275SEric Cheng 		if (nxgep->rx_hio_groups[i].started)
37788275SEric Cheng 			return (B_FALSE);
37798275SEric Cheng 	}
37808275SEric Cheng 
37818275SEric Cheng 	return (B_TRUE);
37828275SEric Cheng }
37838275SEric Cheng 
37843859Sml29623 /*
37853859Sml29623  *	nxge_m_stop(): stop transmitting and receiving.
37863859Sml29623  */
37873859Sml29623 static void
37883859Sml29623 nxge_m_stop(void *arg)
37893859Sml29623 {
37903859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
37918275SEric Cheng 	boolean_t	groups_stopped;
37923859Sml29623 
37933859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_stop"));
37943859Sml29623 
37959232SMichael.Speer@Sun.COM 	/*
37969232SMichael.Speer@Sun.COM 	 * Are the groups stopped?
37979232SMichael.Speer@Sun.COM 	 */
37988275SEric Cheng 	groups_stopped = nxge_check_groups_stopped(nxgep);
37999232SMichael.Speer@Sun.COM 	ASSERT(groups_stopped == B_TRUE);
38008275SEric Cheng 	if (!groups_stopped) {
38018275SEric Cheng 		cmn_err(CE_WARN, "nxge(%d): groups are not stopped!\n",
38028275SEric Cheng 		    nxgep->instance);
38038275SEric Cheng 		return;
38048275SEric Cheng 	}
38058275SEric Cheng 
38069232SMichael.Speer@Sun.COM 	if (!isLDOMguest(nxgep)) {
38079232SMichael.Speer@Sun.COM 		/*
38089232SMichael.Speer@Sun.COM 		 * Disable the RX mac.
38099232SMichael.Speer@Sun.COM 		 */
38109232SMichael.Speer@Sun.COM 		(void) nxge_rx_mac_disable(nxgep);
38119232SMichael.Speer@Sun.COM 
38129232SMichael.Speer@Sun.COM 		/*
38139232SMichael.Speer@Sun.COM 		 * Wait for the IPP to drain.
38149232SMichael.Speer@Sun.COM 		 */
38159232SMichael.Speer@Sun.COM 		(void) nxge_ipp_drain(nxgep);
38169232SMichael.Speer@Sun.COM 
38179232SMichael.Speer@Sun.COM 		/*
38189232SMichael.Speer@Sun.COM 		 * Disable hardware interrupts.
38199232SMichael.Speer@Sun.COM 		 */
38209232SMichael.Speer@Sun.COM 		nxge_intr_hw_disable(nxgep);
38219232SMichael.Speer@Sun.COM 	}
38229232SMichael.Speer@Sun.COM #if defined(sun4v)
38239232SMichael.Speer@Sun.COM 	else {
38249232SMichael.Speer@Sun.COM 		(void) nxge_hio_rdc_intr_arm(nxgep, B_FALSE);
38259232SMichael.Speer@Sun.COM 	}
38269232SMichael.Speer@Sun.COM #endif
38279232SMichael.Speer@Sun.COM 
38289232SMichael.Speer@Sun.COM 	/*
38299232SMichael.Speer@Sun.COM 	 * Grab the global lock.
38309232SMichael.Speer@Sun.COM 	 */
38317466SMisaki.Kataoka@Sun.COM 	MUTEX_ENTER(nxgep->genlock);
38329232SMichael.Speer@Sun.COM 
38337466SMisaki.Kataoka@Sun.COM 	nxgep->nxge_mac_state = NXGE_MAC_STOPPING;
38343859Sml29623 	if (nxgep->nxge_timerid) {
38353859Sml29623 		nxge_stop_timer(nxgep, nxgep->nxge_timerid);
38363859Sml29623 		nxgep->nxge_timerid = 0;
38373859Sml29623 	}
38383859Sml29623 
38399232SMichael.Speer@Sun.COM 	/*
38409232SMichael.Speer@Sun.COM 	 * Clean up.
38419232SMichael.Speer@Sun.COM 	 */
38423859Sml29623 	nxge_uninit(nxgep);
38433859Sml29623 
38443859Sml29623 	nxgep->nxge_mac_state = NXGE_MAC_STOPPED;
38453859Sml29623 
38469232SMichael.Speer@Sun.COM 	/*
38479232SMichael.Speer@Sun.COM 	 * Let go of the global lock.
38489232SMichael.Speer@Sun.COM 	 */
38493859Sml29623 	MUTEX_EXIT(nxgep->genlock);
38503859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_stop"));
38513859Sml29623 }
38523859Sml29623 
38533859Sml29623 static int
38543859Sml29623 nxge_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
38553859Sml29623 {
38563859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
38573859Sml29623 	struct 		ether_addr addrp;
38583859Sml29623 
38593859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL,
38606512Ssowmini 	    "==> nxge_m_multicst: add %d", add));
38613859Sml29623 
38623859Sml29623 	bcopy(mca, (uint8_t *)&addrp, ETHERADDRL);
38633859Sml29623 	if (add) {
38643859Sml29623 		if (nxge_add_mcast_addr(nxgep, &addrp)) {
38653859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
38666512Ssowmini 			    "<== nxge_m_multicst: add multicast failed"));
38673859Sml29623 			return (EINVAL);
38683859Sml29623 		}
38693859Sml29623 	} else {
38703859Sml29623 		if (nxge_del_mcast_addr(nxgep, &addrp)) {
38713859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
38726512Ssowmini 			    "<== nxge_m_multicst: del multicast failed"));
38733859Sml29623 			return (EINVAL);
38743859Sml29623 		}
38753859Sml29623 	}
38763859Sml29623 
38773859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL, "<== nxge_m_multicst"));
38783859Sml29623 
38793859Sml29623 	return (0);
38803859Sml29623 }
38813859Sml29623 
38823859Sml29623 static int
38833859Sml29623 nxge_m_promisc(void *arg, boolean_t on)
38843859Sml29623 {
38853859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
38863859Sml29623 
38873859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL,
38886512Ssowmini 	    "==> nxge_m_promisc: on %d", on));
38893859Sml29623 
38903859Sml29623 	if (nxge_set_promisc(nxgep, on)) {
38913859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
38926512Ssowmini 		    "<== nxge_m_promisc: set promisc failed"));
38933859Sml29623 		return (EINVAL);
38943859Sml29623 	}
38953859Sml29623 
38963859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL,
38976512Ssowmini 	    "<== nxge_m_promisc: on %d", on));
38983859Sml29623 
38993859Sml29623 	return (0);
39003859Sml29623 }
39013859Sml29623 
39023859Sml29623 static void
39033859Sml29623 nxge_m_ioctl(void *arg,  queue_t *wq, mblk_t *mp)
39043859Sml29623 {
39053859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
39064185Sspeer 	struct 		iocblk *iocp;
39073859Sml29623 	boolean_t 	need_privilege;
39083859Sml29623 	int 		err;
39093859Sml29623 	int 		cmd;
39103859Sml29623 
39113859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_ioctl"));
39123859Sml29623 
39133859Sml29623 	iocp = (struct iocblk *)mp->b_rptr;
39143859Sml29623 	iocp->ioc_error = 0;
39153859Sml29623 	need_privilege = B_TRUE;
39163859Sml29623 	cmd = iocp->ioc_cmd;
39173859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_ioctl: cmd 0x%08x", cmd));
39183859Sml29623 	switch (cmd) {
39193859Sml29623 	default:
39203859Sml29623 		miocnak(wq, mp, 0, EINVAL);
39213859Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_ioctl: invalid"));
39223859Sml29623 		return;
39233859Sml29623 
39243859Sml29623 	case LB_GET_INFO_SIZE:
39253859Sml29623 	case LB_GET_INFO:
39263859Sml29623 	case LB_GET_MODE:
39273859Sml29623 		need_privilege = B_FALSE;
39283859Sml29623 		break;
39293859Sml29623 	case LB_SET_MODE:
39303859Sml29623 		break;
39313859Sml29623 
39323859Sml29623 
39333859Sml29623 	case NXGE_GET_MII:
39343859Sml29623 	case NXGE_PUT_MII:
39353859Sml29623 	case NXGE_GET64:
39363859Sml29623 	case NXGE_PUT64:
39373859Sml29623 	case NXGE_GET_TX_RING_SZ:
39383859Sml29623 	case NXGE_GET_TX_DESC:
39393859Sml29623 	case NXGE_TX_SIDE_RESET:
39403859Sml29623 	case NXGE_RX_SIDE_RESET:
39413859Sml29623 	case NXGE_GLOBAL_RESET:
39423859Sml29623 	case NXGE_RESET_MAC:
39433859Sml29623 	case NXGE_TX_REGS_DUMP:
39443859Sml29623 	case NXGE_RX_REGS_DUMP:
39453859Sml29623 	case NXGE_INT_REGS_DUMP:
39463859Sml29623 	case NXGE_VIR_INT_REGS_DUMP:
39473859Sml29623 	case NXGE_PUT_TCAM:
39483859Sml29623 	case NXGE_GET_TCAM:
39493859Sml29623 	case NXGE_RTRACE:
39503859Sml29623 	case NXGE_RDUMP:
39513859Sml29623 
39523859Sml29623 		need_privilege = B_FALSE;
39533859Sml29623 		break;
39543859Sml29623 	case NXGE_INJECT_ERR:
39553859Sml29623 		cmn_err(CE_NOTE, "!nxge_m_ioctl: Inject error\n");
39563859Sml29623 		nxge_err_inject(nxgep, wq, mp);
39573859Sml29623 		break;
39583859Sml29623 	}
39593859Sml29623 
39603859Sml29623 	if (need_privilege) {
39614185Sspeer 		err = secpolicy_net_config(iocp->ioc_cr, B_FALSE);
39623859Sml29623 		if (err != 0) {
39633859Sml29623 			miocnak(wq, mp, 0, err);
39643859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
39656512Ssowmini 			    "<== nxge_m_ioctl: no priv"));
39663859Sml29623 			return;
39673859Sml29623 		}
39683859Sml29623 	}
39693859Sml29623 
39703859Sml29623 	switch (cmd) {
39713859Sml29623 
39723859Sml29623 	case LB_GET_MODE:
39733859Sml29623 	case LB_SET_MODE:
39743859Sml29623 	case LB_GET_INFO_SIZE:
39753859Sml29623 	case LB_GET_INFO:
39763859Sml29623 		nxge_loopback_ioctl(nxgep, wq, mp, iocp);
39773859Sml29623 		break;
39783859Sml29623 
39793859Sml29623 	case NXGE_GET_MII:
39803859Sml29623 	case NXGE_PUT_MII:
39813859Sml29623 	case NXGE_PUT_TCAM:
39823859Sml29623 	case NXGE_GET_TCAM:
39833859Sml29623 	case NXGE_GET64:
39843859Sml29623 	case NXGE_PUT64:
39853859Sml29623 	case NXGE_GET_TX_RING_SZ:
39863859Sml29623 	case NXGE_GET_TX_DESC:
39873859Sml29623 	case NXGE_TX_SIDE_RESET:
39883859Sml29623 	case NXGE_RX_SIDE_RESET:
39893859Sml29623 	case NXGE_GLOBAL_RESET:
39903859Sml29623 	case NXGE_RESET_MAC:
39913859Sml29623 	case NXGE_TX_REGS_DUMP:
39923859Sml29623 	case NXGE_RX_REGS_DUMP:
39933859Sml29623 	case NXGE_INT_REGS_DUMP:
39943859Sml29623 	case NXGE_VIR_INT_REGS_DUMP:
39953859Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
39966512Ssowmini 		    "==> nxge_m_ioctl: cmd 0x%x", cmd));
39973859Sml29623 		nxge_hw_ioctl(nxgep, wq, mp, iocp);
39983859Sml29623 		break;
39993859Sml29623 	}
40003859Sml29623 
40013859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_ioctl"));
40023859Sml29623 }
40033859Sml29623 
40043859Sml29623 extern void nxge_rx_hw_blank(void *arg, time_t ticks, uint_t count);
40053859Sml29623 
40066495Sspeer void
40078275SEric Cheng nxge_mmac_kstat_update(p_nxge_t nxgep, int slot, boolean_t factory)
40083859Sml29623 {
40093859Sml29623 	p_nxge_mmac_stats_t mmac_stats;
40103859Sml29623 	int i;
40113859Sml29623 	nxge_mmac_t *mmac_info;
40123859Sml29623 
40133859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
40143859Sml29623 
40153859Sml29623 	mmac_stats = &nxgep->statsp->mmac_stats;
40163859Sml29623 	mmac_stats->mmac_max_cnt = mmac_info->num_mmac;
40173859Sml29623 	mmac_stats->mmac_avail_cnt = mmac_info->naddrfree;
40183859Sml29623 
40193859Sml29623 	for (i = 0; i < ETHERADDRL; i++) {
40203859Sml29623 		if (factory) {
40213859Sml29623 			mmac_stats->mmac_avail_pool[slot-1].ether_addr_octet[i]
40226512Ssowmini 			    = mmac_info->factory_mac_pool[slot][
40236512Ssowmini 			    (ETHERADDRL-1) - i];
40243859Sml29623 		} else {
40253859Sml29623 			mmac_stats->mmac_avail_pool[slot-1].ether_addr_octet[i]
40266512Ssowmini 			    = mmac_info->mac_pool[slot].addr[
40276512Ssowmini 			    (ETHERADDRL - 1) - i];
40283859Sml29623 		}
40293859Sml29623 	}
40303859Sml29623 }
40313859Sml29623 
40323859Sml29623 /*
40333859Sml29623  * nxge_altmac_set() -- Set an alternate MAC address
40343859Sml29623  */
40358275SEric Cheng static int
40368275SEric Cheng nxge_altmac_set(p_nxge_t nxgep, uint8_t *maddr, int slot,
40378275SEric Cheng 	int rdctbl, boolean_t usetbl)
40383859Sml29623 {
40393859Sml29623 	uint8_t addrn;
40403859Sml29623 	uint8_t portn;
40413859Sml29623 	npi_mac_addr_t altmac;
40424484Sspeer 	hostinfo_t mac_rdc;
40434484Sspeer 	p_nxge_class_pt_cfg_t clscfgp;
40443859Sml29623 
40458275SEric Cheng 
40463859Sml29623 	altmac.w2 = ((uint16_t)maddr[0] << 8) | ((uint16_t)maddr[1] & 0x0ff);
40473859Sml29623 	altmac.w1 = ((uint16_t)maddr[2] << 8) | ((uint16_t)maddr[3] & 0x0ff);
40483859Sml29623 	altmac.w0 = ((uint16_t)maddr[4] << 8) | ((uint16_t)maddr[5] & 0x0ff);
40493859Sml29623 
40503859Sml29623 	portn = nxgep->mac.portnum;
40513859Sml29623 	addrn = (uint8_t)slot - 1;
40523859Sml29623 
40538275SEric Cheng 	if (npi_mac_altaddr_entry(nxgep->npi_handle, OP_SET,
40548275SEric Cheng 	    nxgep->function_num, addrn, &altmac) != NPI_SUCCESS)
40553859Sml29623 		return (EIO);
40564484Sspeer 
40574484Sspeer 	/*
40584484Sspeer 	 * Set the rdc table number for the host info entry
40594484Sspeer 	 * for this mac address slot.
40604484Sspeer 	 */
40614484Sspeer 	clscfgp = (p_nxge_class_pt_cfg_t)&nxgep->class_config;
40624484Sspeer 	mac_rdc.value = 0;
40638275SEric Cheng 	if (usetbl)
40648275SEric Cheng 		mac_rdc.bits.w0.rdc_tbl_num = rdctbl;
40658275SEric Cheng 	else
40668275SEric Cheng 		mac_rdc.bits.w0.rdc_tbl_num =
40678275SEric Cheng 		    clscfgp->mac_host_info[addrn].rdctbl;
40684484Sspeer 	mac_rdc.bits.w0.mac_pref = clscfgp->mac_host_info[addrn].mpr_npr;
40694484Sspeer 
40704484Sspeer 	if (npi_mac_hostinfo_entry(nxgep->npi_handle, OP_SET,
40714484Sspeer 	    nxgep->function_num, addrn, &mac_rdc) != NPI_SUCCESS) {
40724484Sspeer 		return (EIO);
40734484Sspeer 	}
40744484Sspeer 
40753859Sml29623 	/*
40763859Sml29623 	 * Enable comparison with the alternate MAC address.
40773859Sml29623 	 * While the first alternate addr is enabled by bit 1 of register
40783859Sml29623 	 * BMAC_ALTAD_CMPEN, it is enabled by bit 0 of register
40793859Sml29623 	 * XMAC_ADDR_CMPEN, so slot needs to be converted to addrn
40803859Sml29623 	 * accordingly before calling npi_mac_altaddr_entry.
40813859Sml29623 	 */
40823859Sml29623 	if (portn == XMAC_PORT_0 || portn == XMAC_PORT_1)
40833859Sml29623 		addrn = (uint8_t)slot - 1;
40843859Sml29623 	else
40853859Sml29623 		addrn = (uint8_t)slot;
40863859Sml29623 
40878275SEric Cheng 	if (npi_mac_altaddr_enable(nxgep->npi_handle,
40888275SEric Cheng 	    nxgep->function_num, addrn) != NPI_SUCCESS) {
40893859Sml29623 		return (EIO);
40908275SEric Cheng 	}
40918275SEric Cheng 
40923859Sml29623 	return (0);
40933859Sml29623 }
40943859Sml29623 
40953859Sml29623 /*
40968275SEric Cheng  * nxeg_m_mmac_add_g() - find an unused address slot, set the address
40973859Sml29623  * value to the one specified, enable the port to start filtering on
40983859Sml29623  * the new MAC address.  Returns 0 on success.
40993859Sml29623  */
41006495Sspeer int
41018275SEric Cheng nxge_m_mmac_add_g(void *arg, const uint8_t *maddr, int rdctbl,
41028275SEric Cheng 	boolean_t usetbl)
41033859Sml29623 {
41043859Sml29623 	p_nxge_t nxgep = arg;
41058275SEric Cheng 	int slot;
41063859Sml29623 	nxge_mmac_t *mmac_info;
41073859Sml29623 	int err;
41083859Sml29623 	nxge_status_t status;
41093859Sml29623 
41103859Sml29623 	mutex_enter(nxgep->genlock);
41113859Sml29623 
41123859Sml29623 	/*
41133859Sml29623 	 * Make sure that nxge is initialized, if _start() has
41143859Sml29623 	 * not been called.
41153859Sml29623 	 */
41163859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
41173859Sml29623 		status = nxge_init(nxgep);
41183859Sml29623 		if (status != NXGE_OK) {
41193859Sml29623 			mutex_exit(nxgep->genlock);
41203859Sml29623 			return (ENXIO);
41213859Sml29623 		}
41223859Sml29623 	}
41233859Sml29623 
41243859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
41253859Sml29623 	if (mmac_info->naddrfree == 0) {
41263859Sml29623 		mutex_exit(nxgep->genlock);
41273859Sml29623 		return (ENOSPC);
41283859Sml29623 	}
41298275SEric Cheng 
41303859Sml29623 	/*
41313859Sml29623 	 * 	Search for the first available slot. Because naddrfree
41323859Sml29623 	 * is not zero, we are guaranteed to find one.
41333859Sml29623 	 *	Each of the first two ports of Neptune has 16 alternate
41346495Sspeer 	 * MAC slots but only the first 7 (of 15) slots have assigned factory
41353859Sml29623 	 * MAC addresses. We first search among the slots without bundled
41363859Sml29623 	 * factory MACs. If we fail to find one in that range, then we
41373859Sml29623 	 * search the slots with bundled factory MACs.  A factory MAC
41383859Sml29623 	 * will be wasted while the slot is used with a user MAC address.
41393859Sml29623 	 * But the slot could be used by factory MAC again after calling
41403859Sml29623 	 * nxge_m_mmac_remove and nxge_m_mmac_reserve.
41413859Sml29623 	 */
41428275SEric Cheng 	for (slot = 0; slot <= mmac_info->num_mmac; slot++) {
41438275SEric Cheng 		if (!(mmac_info->mac_pool[slot].flags & MMAC_SLOT_USED))
41448275SEric Cheng 			break;
41458275SEric Cheng 	}
41468275SEric Cheng 
41473859Sml29623 	ASSERT(slot <= mmac_info->num_mmac);
41488047SMichael.Speer@Sun.COM 
41498275SEric Cheng 	if ((err = nxge_altmac_set(nxgep, (uint8_t *)maddr, slot, rdctbl,
41508275SEric Cheng 	    usetbl)) != 0) {
41513859Sml29623 		mutex_exit(nxgep->genlock);
41523859Sml29623 		return (err);
41533859Sml29623 	}
41548047SMichael.Speer@Sun.COM 
41558275SEric Cheng 	bcopy(maddr, mmac_info->mac_pool[slot].addr, ETHERADDRL);
41563859Sml29623 	mmac_info->mac_pool[slot].flags |= MMAC_SLOT_USED;
41573859Sml29623 	mmac_info->mac_pool[slot].flags &= ~MMAC_VENDOR_ADDR;
41583859Sml29623 	mmac_info->naddrfree--;
41593859Sml29623 	nxge_mmac_kstat_update(nxgep, slot, B_FALSE);
41603859Sml29623 
41613859Sml29623 	mutex_exit(nxgep->genlock);
41623859Sml29623 	return (0);
41633859Sml29623 }
41643859Sml29623 
41653859Sml29623 /*
41663859Sml29623  * Remove the specified mac address and update the HW not to filter
41673859Sml29623  * the mac address anymore.
41683859Sml29623  */
41696495Sspeer int
41708275SEric Cheng nxge_m_mmac_remove(void *arg, int slot)
41713859Sml29623 {
41723859Sml29623 	p_nxge_t nxgep = arg;
41733859Sml29623 	nxge_mmac_t *mmac_info;
41743859Sml29623 	uint8_t addrn;
41753859Sml29623 	uint8_t portn;
41763859Sml29623 	int err = 0;
41773859Sml29623 	nxge_status_t status;
41783859Sml29623 
41793859Sml29623 	mutex_enter(nxgep->genlock);
41803859Sml29623 
41813859Sml29623 	/*
41823859Sml29623 	 * Make sure that nxge is initialized, if _start() has
41833859Sml29623 	 * not been called.
41843859Sml29623 	 */
41853859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
41863859Sml29623 		status = nxge_init(nxgep);
41873859Sml29623 		if (status != NXGE_OK) {
41883859Sml29623 			mutex_exit(nxgep->genlock);
41893859Sml29623 			return (ENXIO);
41903859Sml29623 		}
41913859Sml29623 	}
41923859Sml29623 
41933859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
41943859Sml29623 	if (slot < 1 || slot > mmac_info->num_mmac) {
41953859Sml29623 		mutex_exit(nxgep->genlock);
41963859Sml29623 		return (EINVAL);
41973859Sml29623 	}
41983859Sml29623 
41993859Sml29623 	portn = nxgep->mac.portnum;
42003859Sml29623 	if (portn == XMAC_PORT_0 || portn == XMAC_PORT_1)
42013859Sml29623 		addrn = (uint8_t)slot - 1;
42023859Sml29623 	else
42033859Sml29623 		addrn = (uint8_t)slot;
42043859Sml29623 
42053859Sml29623 	if (mmac_info->mac_pool[slot].flags & MMAC_SLOT_USED) {
42063859Sml29623 		if (npi_mac_altaddr_disable(nxgep->npi_handle, portn, addrn)
42076512Ssowmini 		    == NPI_SUCCESS) {
42083859Sml29623 			mmac_info->naddrfree++;
42093859Sml29623 			mmac_info->mac_pool[slot].flags &= ~MMAC_SLOT_USED;
42103859Sml29623 			/*
42113859Sml29623 			 * Regardless if the MAC we just stopped filtering
42123859Sml29623 			 * is a user addr or a facory addr, we must set
42133859Sml29623 			 * the MMAC_VENDOR_ADDR flag if this slot has an
42143859Sml29623 			 * associated factory MAC to indicate that a factory
42153859Sml29623 			 * MAC is available.
42163859Sml29623 			 */
42173859Sml29623 			if (slot <= mmac_info->num_factory_mmac) {
42183859Sml29623 				mmac_info->mac_pool[slot].flags
42196512Ssowmini 				    |= MMAC_VENDOR_ADDR;
42203859Sml29623 			}
42213859Sml29623 			/*
42223859Sml29623 			 * Clear mac_pool[slot].addr so that kstat shows 0
42233859Sml29623 			 * alternate MAC address if the slot is not used.
42243859Sml29623 			 * (But nxge_m_mmac_get returns the factory MAC even
42253859Sml29623 			 * when the slot is not used!)
42263859Sml29623 			 */
42273859Sml29623 			bzero(mmac_info->mac_pool[slot].addr, ETHERADDRL);
42283859Sml29623 			nxge_mmac_kstat_update(nxgep, slot, B_FALSE);
42293859Sml29623 		} else {
42303859Sml29623 			err = EIO;
42313859Sml29623 		}
42323859Sml29623 	} else {
42333859Sml29623 		err = EINVAL;
42343859Sml29623 	}
42353859Sml29623 
42363859Sml29623 	mutex_exit(nxgep->genlock);
42373859Sml29623 	return (err);
42383859Sml29623 }
42393859Sml29623 
42403859Sml29623 /*
42418275SEric Cheng  * The callback to query all the factory addresses. naddr must be the same as
42428275SEric Cheng  * the number of factory addresses (returned by MAC_CAPAB_MULTIFACTADDR), and
42438275SEric Cheng  * mcm_addr is the space allocated for keep all the addresses, whose size is
42448275SEric Cheng  * naddr * MAXMACADDRLEN.
42453859Sml29623  */
42468275SEric Cheng static void
42478275SEric Cheng nxge_m_getfactaddr(void *arg, uint_t naddr, uint8_t *addr)
42483859Sml29623 {
42498275SEric Cheng 	nxge_t		*nxgep = arg;
42508275SEric Cheng 	nxge_mmac_t	*mmac_info;
42518275SEric Cheng 	int		i;
42523859Sml29623 
42533859Sml29623 	mutex_enter(nxgep->genlock);
42543859Sml29623 
42553859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
42568275SEric Cheng 	ASSERT(naddr == mmac_info->num_factory_mmac);
42578275SEric Cheng 
42588275SEric Cheng 	for (i = 0; i < naddr; i++) {
42598275SEric Cheng 		bcopy(mmac_info->factory_mac_pool[i + 1],
42608275SEric Cheng 		    addr + i * MAXMACADDRLEN, ETHERADDRL);
42618275SEric Cheng 	}
42628275SEric Cheng 
42633859Sml29623 	mutex_exit(nxgep->genlock);
42643859Sml29623 }
42653859Sml29623 
42663859Sml29623 
42673859Sml29623 static boolean_t
42683859Sml29623 nxge_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
42693859Sml29623 {
42703859Sml29623 	nxge_t *nxgep = arg;
42713859Sml29623 	uint32_t *txflags = cap_data;
42723859Sml29623 
42733859Sml29623 	switch (cap) {
42743859Sml29623 	case MAC_CAPAB_HCKSUM:
42756495Sspeer 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
42766611Sml29623 		    "==> nxge_m_getcapab: checksum %d", nxge_cksum_offload));
42776611Sml29623 		if (nxge_cksum_offload <= 1) {
42786495Sspeer 			*txflags = HCKSUM_INET_PARTIAL;
42796495Sspeer 		}
42803859Sml29623 		break;
42816495Sspeer 
42828275SEric Cheng 	case MAC_CAPAB_MULTIFACTADDR: {
42838275SEric Cheng 		mac_capab_multifactaddr_t	*mfacp = cap_data;
42848275SEric Cheng 
428510309SSriharsha.Basavapatna@Sun.COM 		if (!isLDOMguest(nxgep)) {
428610309SSriharsha.Basavapatna@Sun.COM 			mutex_enter(nxgep->genlock);
428710309SSriharsha.Basavapatna@Sun.COM 			mfacp->mcm_naddr =
428810309SSriharsha.Basavapatna@Sun.COM 			    nxgep->nxge_mmac_info.num_factory_mmac;
428910309SSriharsha.Basavapatna@Sun.COM 			mfacp->mcm_getaddr = nxge_m_getfactaddr;
429010309SSriharsha.Basavapatna@Sun.COM 			mutex_exit(nxgep->genlock);
429110309SSriharsha.Basavapatna@Sun.COM 		}
42923859Sml29623 		break;
42938275SEric Cheng 	}
42946495Sspeer 
42955770Sml29623 	case MAC_CAPAB_LSO: {
42965770Sml29623 		mac_capab_lso_t *cap_lso = cap_data;
42975770Sml29623 
42986003Sml29623 		if (nxgep->soft_lso_enable) {
42996611Sml29623 			if (nxge_cksum_offload <= 1) {
43006611Sml29623 				cap_lso->lso_flags = LSO_TX_BASIC_TCP_IPV4;
43016611Sml29623 				if (nxge_lso_max > NXGE_LSO_MAXLEN) {
43026611Sml29623 					nxge_lso_max = NXGE_LSO_MAXLEN;
43036611Sml29623 				}
43046611Sml29623 				cap_lso->lso_basic_tcp_ipv4.lso_max =
43056611Sml29623 				    nxge_lso_max;
43065770Sml29623 			}
43075770Sml29623 			break;
43085770Sml29623 		} else {
43095770Sml29623 			return (B_FALSE);
43105770Sml29623 		}
43115770Sml29623 	}
43125770Sml29623 
43138275SEric Cheng 	case MAC_CAPAB_RINGS: {
43148275SEric Cheng 		mac_capab_rings_t	*cap_rings = cap_data;
43158275SEric Cheng 		p_nxge_hw_pt_cfg_t	p_cfgp = &nxgep->pt_config.hw_config;
43168275SEric Cheng 
43178275SEric Cheng 		mutex_enter(nxgep->genlock);
43188275SEric Cheng 		if (cap_rings->mr_type == MAC_RING_TYPE_RX) {
431910309SSriharsha.Basavapatna@Sun.COM 			if (isLDOMguest(nxgep))  {
432010309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_group_type =
432110309SSriharsha.Basavapatna@Sun.COM 				    MAC_GROUP_TYPE_STATIC;
432210309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_rnum =
432310309SSriharsha.Basavapatna@Sun.COM 				    NXGE_HIO_SHARE_MAX_CHANNELS;
432410309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_rget = nxge_fill_ring;
432510309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gnum = 1;
432610309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gget = nxge_hio_group_get;
432710309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gaddring = NULL;
432810309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gremring = NULL;
432910309SSriharsha.Basavapatna@Sun.COM 			} else {
433010309SSriharsha.Basavapatna@Sun.COM 				/*
433110309SSriharsha.Basavapatna@Sun.COM 				 * Service Domain.
433210309SSriharsha.Basavapatna@Sun.COM 				 */
433310309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_group_type =
433410309SSriharsha.Basavapatna@Sun.COM 				    MAC_GROUP_TYPE_DYNAMIC;
433510309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_rnum = p_cfgp->max_rdcs;
433610309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_rget = nxge_fill_ring;
433710309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gnum = p_cfgp->max_rdc_grpids;
433810309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gget = nxge_hio_group_get;
433910309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gaddring = nxge_group_add_ring;
434010309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gremring = nxge_group_rem_ring;
434110309SSriharsha.Basavapatna@Sun.COM 			}
43428275SEric Cheng 
43438275SEric Cheng 			NXGE_DEBUG_MSG((nxgep, RX_CTL,
43448275SEric Cheng 			    "==> nxge_m_getcapab: rx nrings[%d] ngroups[%d]",
43458275SEric Cheng 			    p_cfgp->max_rdcs, p_cfgp->max_rdc_grpids));
43468275SEric Cheng 		} else {
434710309SSriharsha.Basavapatna@Sun.COM 			/*
434810309SSriharsha.Basavapatna@Sun.COM 			 * TX Rings.
434910309SSriharsha.Basavapatna@Sun.COM 			 */
435010309SSriharsha.Basavapatna@Sun.COM 			if (isLDOMguest(nxgep)) {
435110309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_group_type =
435210309SSriharsha.Basavapatna@Sun.COM 				    MAC_GROUP_TYPE_STATIC;
435310309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_rnum =
435410309SSriharsha.Basavapatna@Sun.COM 				    NXGE_HIO_SHARE_MAX_CHANNELS;
435510309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_rget = nxge_fill_ring;
435610309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gnum = 0;
435710309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gget = NULL;
435810309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gaddring = NULL;
435910309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gremring = NULL;
436010309SSriharsha.Basavapatna@Sun.COM 			} else {
436110309SSriharsha.Basavapatna@Sun.COM 				/*
436210309SSriharsha.Basavapatna@Sun.COM 				 * Service Domain.
436310309SSriharsha.Basavapatna@Sun.COM 				 */
436410309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_group_type =
436510309SSriharsha.Basavapatna@Sun.COM 				    MAC_GROUP_TYPE_DYNAMIC;
436610309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_rnum = p_cfgp->tdc.count;
436710309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_rget = nxge_fill_ring;
436810309SSriharsha.Basavapatna@Sun.COM 
436910309SSriharsha.Basavapatna@Sun.COM 				/*
437010309SSriharsha.Basavapatna@Sun.COM 				 * Share capable.
437110309SSriharsha.Basavapatna@Sun.COM 				 *
437210309SSriharsha.Basavapatna@Sun.COM 				 * Do not report the default group: hence -1
437310309SSriharsha.Basavapatna@Sun.COM 				 */
43748275SEric Cheng 				cap_rings->mr_gnum =
43758275SEric Cheng 				    NXGE_MAX_TDC_GROUPS / nxgep->nports - 1;
437610309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gget = nxge_hio_group_get;
437710309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gaddring = nxge_group_add_ring;
437810309SSriharsha.Basavapatna@Sun.COM 				cap_rings->mr_gremring = nxge_group_rem_ring;
43798275SEric Cheng 			}
43808275SEric Cheng 
43818275SEric Cheng 			NXGE_DEBUG_MSG((nxgep, TX_CTL,
43828275SEric Cheng 			    "==> nxge_m_getcapab: tx rings # of rings %d",
43838275SEric Cheng 			    p_cfgp->tdc.count));
43848275SEric Cheng 		}
43858275SEric Cheng 		mutex_exit(nxgep->genlock);
43868275SEric Cheng 		break;
43878275SEric Cheng 	}
43888275SEric Cheng 
43896495Sspeer #if defined(sun4v)
43906495Sspeer 	case MAC_CAPAB_SHARES: {
43916495Sspeer 		mac_capab_share_t *mshares = (mac_capab_share_t *)cap_data;
43926495Sspeer 
43936495Sspeer 		/*
43946495Sspeer 		 * Only the service domain driver responds to
43956495Sspeer 		 * this capability request.
43966495Sspeer 		 */
43978275SEric Cheng 		mutex_enter(nxgep->genlock);
43986495Sspeer 		if (isLDOMservice(nxgep)) {
43996495Sspeer 			mshares->ms_snum = 3;
44006495Sspeer 			mshares->ms_handle = (void *)nxgep;
44016495Sspeer 			mshares->ms_salloc = nxge_hio_share_alloc;
44026495Sspeer 			mshares->ms_sfree = nxge_hio_share_free;
44038275SEric Cheng 			mshares->ms_sadd = nxge_hio_share_add_group;
44048275SEric Cheng 			mshares->ms_sremove = nxge_hio_share_rem_group;
44056495Sspeer 			mshares->ms_squery = nxge_hio_share_query;
44068275SEric Cheng 			mshares->ms_sbind = nxge_hio_share_bind;
44078275SEric Cheng 			mshares->ms_sunbind = nxge_hio_share_unbind;
44088275SEric Cheng 			mutex_exit(nxgep->genlock);
44098275SEric Cheng 		} else {
44108275SEric Cheng 			mutex_exit(nxgep->genlock);
44116495Sspeer 			return (B_FALSE);
44128275SEric Cheng 		}
44136495Sspeer 		break;
44146495Sspeer 	}
44156495Sspeer #endif
44163859Sml29623 	default:
44173859Sml29623 		return (B_FALSE);
44183859Sml29623 	}
44193859Sml29623 	return (B_TRUE);
44203859Sml29623 }
44213859Sml29623 
44226439Sml29623 static boolean_t
44236439Sml29623 nxge_param_locked(mac_prop_id_t pr_num)
44246439Sml29623 {
44256439Sml29623 	/*
44266439Sml29623 	 * All adv_* parameters are locked (read-only) while
44276439Sml29623 	 * the device is in any sort of loopback mode ...
44286439Sml29623 	 */
44296439Sml29623 	switch (pr_num) {
44306789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
44316789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
44326789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
44336789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
44346789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
44356789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
44366789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
44376789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
44386789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
44396789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
44406789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
44416789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
44426789Sam223141 		case MAC_PROP_AUTONEG:
44436789Sam223141 		case MAC_PROP_FLOWCTRL:
44446439Sml29623 			return (B_TRUE);
44456439Sml29623 	}
44466439Sml29623 	return (B_FALSE);
44476439Sml29623 }
44486439Sml29623 
44496439Sml29623 /*
44506439Sml29623  * callback functions for set/get of properties
44516439Sml29623  */
44526439Sml29623 static int
44536439Sml29623 nxge_m_setprop(void *barg, const char *pr_name, mac_prop_id_t pr_num,
44546439Sml29623     uint_t pr_valsize, const void *pr_val)
44556439Sml29623 {
44566439Sml29623 	nxge_t		*nxgep = barg;
44576439Sml29623 	p_nxge_param_t	param_arr;
44586439Sml29623 	p_nxge_stats_t	statsp;
44596439Sml29623 	int		err = 0;
44606439Sml29623 	uint8_t		val;
44616439Sml29623 	uint32_t	cur_mtu, new_mtu, old_framesize;
44626439Sml29623 	link_flowctrl_t	fl;
44636439Sml29623 
44646439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_setprop"));
44656439Sml29623 	param_arr = nxgep->param_arr;
44666439Sml29623 	statsp = nxgep->statsp;
44676439Sml29623 	mutex_enter(nxgep->genlock);
44686439Sml29623 	if (statsp->port_stats.lb_mode != nxge_lb_normal &&
44696439Sml29623 	    nxge_param_locked(pr_num)) {
44706439Sml29623 		/*
44716439Sml29623 		 * All adv_* parameters are locked (read-only)
44726439Sml29623 		 * while the device is in any sort of loopback mode.
44736439Sml29623 		 */
44746439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
44756439Sml29623 		    "==> nxge_m_setprop: loopback mode: read only"));
44766439Sml29623 		mutex_exit(nxgep->genlock);
44776439Sml29623 		return (EBUSY);
44786439Sml29623 	}
44796439Sml29623 
44806439Sml29623 	val = *(uint8_t *)pr_val;
44816439Sml29623 	switch (pr_num) {
44826789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
44836439Sml29623 			nxgep->param_en_1000fdx = val;
44846439Sml29623 			param_arr[param_anar_1000fdx].value = val;
44856439Sml29623 
44866439Sml29623 			goto reprogram;
44876439Sml29623 
44886789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
44896439Sml29623 			nxgep->param_en_100fdx = val;
44906439Sml29623 			param_arr[param_anar_100fdx].value = val;
44916439Sml29623 
44926439Sml29623 			goto reprogram;
44936439Sml29623 
44946789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
44956439Sml29623 			nxgep->param_en_10fdx = val;
44966439Sml29623 			param_arr[param_anar_10fdx].value = val;
44976439Sml29623 
44986439Sml29623 			goto reprogram;
44996439Sml29623 
45006789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
45016789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
45026789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
45036789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
45046789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
45056789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
45066789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
45076789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
45086789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
45096789Sam223141 		case MAC_PROP_STATUS:
45106789Sam223141 		case MAC_PROP_SPEED:
45116789Sam223141 		case MAC_PROP_DUPLEX:
45126439Sml29623 			err = EINVAL; /* cannot set read-only properties */
45136439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
45146439Sml29623 			    "==> nxge_m_setprop:  read only property %d",
45156439Sml29623 			    pr_num));
45166439Sml29623 			break;
45176439Sml29623 
45186789Sam223141 		case MAC_PROP_AUTONEG:
45196439Sml29623 			param_arr[param_autoneg].value = val;
45206439Sml29623 
45216439Sml29623 			goto reprogram;
45226439Sml29623 
45236789Sam223141 		case MAC_PROP_MTU:
45246439Sml29623 			cur_mtu = nxgep->mac.default_mtu;
45256439Sml29623 			bcopy(pr_val, &new_mtu, sizeof (new_mtu));
45266439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
45276439Sml29623 			    "==> nxge_m_setprop: set MTU: %d is_jumbo %d",
45286439Sml29623 			    new_mtu, nxgep->mac.is_jumbo));
45296439Sml29623 
45306439Sml29623 			if (new_mtu == cur_mtu) {
45316439Sml29623 				err = 0;
45326439Sml29623 				break;
45336439Sml29623 			}
45349730SMichael.Speer@Sun.COM 
45358118SVasumathi.Sundaram@Sun.COM 			if (nxgep->nxge_mac_state == NXGE_MAC_STARTED) {
45368118SVasumathi.Sundaram@Sun.COM 				err = EBUSY;
45378118SVasumathi.Sundaram@Sun.COM 				break;
45388118SVasumathi.Sundaram@Sun.COM 			}
45399730SMichael.Speer@Sun.COM 
45409730SMichael.Speer@Sun.COM 			if ((new_mtu < NXGE_DEFAULT_MTU) ||
45419730SMichael.Speer@Sun.COM 			    (new_mtu > NXGE_MAXIMUM_MTU)) {
45426439Sml29623 				err = EINVAL;
45436439Sml29623 				break;
45446439Sml29623 			}
45456439Sml29623 
45466439Sml29623 			old_framesize = (uint32_t)nxgep->mac.maxframesize;
45476439Sml29623 			nxgep->mac.maxframesize = (uint16_t)
45486439Sml29623 			    (new_mtu + NXGE_EHEADER_VLAN_CRC);
45496439Sml29623 			if (nxge_mac_set_framesize(nxgep)) {
45506444Sml29623 				nxgep->mac.maxframesize =
45516444Sml29623 				    (uint16_t)old_framesize;
45526439Sml29623 				err = EINVAL;
45536439Sml29623 				break;
45546439Sml29623 			}
45556439Sml29623 
45566439Sml29623 			err = mac_maxsdu_update(nxgep->mach, new_mtu);
45576439Sml29623 			if (err) {
45586444Sml29623 				nxgep->mac.maxframesize =
45596444Sml29623 				    (uint16_t)old_framesize;
45606439Sml29623 				err = EINVAL;
45616439Sml29623 				break;
45626439Sml29623 			}
45636439Sml29623 
45646439Sml29623 			nxgep->mac.default_mtu = new_mtu;
45659730SMichael.Speer@Sun.COM 			if (new_mtu > NXGE_DEFAULT_MTU)
45669730SMichael.Speer@Sun.COM 				nxgep->mac.is_jumbo = B_TRUE;
45679730SMichael.Speer@Sun.COM 			else
45689730SMichael.Speer@Sun.COM 				nxgep->mac.is_jumbo = B_FALSE;
45699730SMichael.Speer@Sun.COM 
45706439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
45716439Sml29623 			    "==> nxge_m_setprop: set MTU: %d maxframe %d",
45726439Sml29623 			    new_mtu, nxgep->mac.maxframesize));
45736439Sml29623 			break;
45746439Sml29623 
45756789Sam223141 		case MAC_PROP_FLOWCTRL:
45766439Sml29623 			bcopy(pr_val, &fl, sizeof (fl));
45776439Sml29623 			switch (fl) {
45786439Sml29623 			default:
45796439Sml29623 				err = EINVAL;
45806439Sml29623 				break;
45816439Sml29623 
45826439Sml29623 			case LINK_FLOWCTRL_NONE:
45836439Sml29623 				param_arr[param_anar_pause].value = 0;
45846439Sml29623 				break;
45856439Sml29623 
45866439Sml29623 			case LINK_FLOWCTRL_RX:
45876439Sml29623 				param_arr[param_anar_pause].value = 1;
45886439Sml29623 				break;
45896439Sml29623 
45906439Sml29623 			case LINK_FLOWCTRL_TX:
45916439Sml29623 			case LINK_FLOWCTRL_BI:
45926439Sml29623 				err = EINVAL;
45936439Sml29623 				break;
45946439Sml29623 			}
45956439Sml29623 
45966439Sml29623 reprogram:
45976439Sml29623 			if (err == 0) {
45986439Sml29623 				if (!nxge_param_link_update(nxgep)) {
45996439Sml29623 					err = EINVAL;
46006439Sml29623 				}
46016439Sml29623 			}
46026439Sml29623 			break;
46036789Sam223141 		case MAC_PROP_PRIVATE:
46046439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
46056439Sml29623 			    "==> nxge_m_setprop: private property"));
46066439Sml29623 			err = nxge_set_priv_prop(nxgep, pr_name, pr_valsize,
46076439Sml29623 			    pr_val);
46086439Sml29623 			break;
46096512Ssowmini 
46106512Ssowmini 		default:
46116512Ssowmini 			err = ENOTSUP;
46126512Ssowmini 			break;
46136439Sml29623 	}
46146439Sml29623 
46156439Sml29623 	mutex_exit(nxgep->genlock);
46166439Sml29623 
46176439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
46186439Sml29623 	    "<== nxge_m_setprop (return %d)", err));
46196439Sml29623 	return (err);
46206439Sml29623 }
46216439Sml29623 
46226439Sml29623 static int
46236439Sml29623 nxge_m_getprop(void *barg, const char *pr_name, mac_prop_id_t pr_num,
46248118SVasumathi.Sundaram@Sun.COM     uint_t pr_flags, uint_t pr_valsize, void *pr_val, uint_t *perm)
46256439Sml29623 {
46266439Sml29623 	nxge_t 		*nxgep = barg;
46276439Sml29623 	p_nxge_param_t	param_arr = nxgep->param_arr;
46286439Sml29623 	p_nxge_stats_t	statsp = nxgep->statsp;
46296439Sml29623 	int		err = 0;
46306439Sml29623 	link_flowctrl_t	fl;
46316439Sml29623 	uint64_t	tmp = 0;
46326512Ssowmini 	link_state_t	ls;
46336789Sam223141 	boolean_t	is_default = (pr_flags & MAC_PROP_DEFAULT);
46346439Sml29623 
46356439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
46366439Sml29623 	    "==> nxge_m_getprop: pr_num %d", pr_num));
46376512Ssowmini 
46386512Ssowmini 	if (pr_valsize == 0)
46396512Ssowmini 		return (EINVAL);
46406512Ssowmini 
46418118SVasumathi.Sundaram@Sun.COM 	*perm = MAC_PROP_PERM_RW;
46428118SVasumathi.Sundaram@Sun.COM 
46436789Sam223141 	if ((is_default) && (pr_num != MAC_PROP_PRIVATE)) {
46446512Ssowmini 		err = nxge_get_def_val(nxgep, pr_num, pr_valsize, pr_val);
46456512Ssowmini 		return (err);
46466512Ssowmini 	}
46476512Ssowmini 
46486439Sml29623 	bzero(pr_val, pr_valsize);
46496439Sml29623 	switch (pr_num) {
46506789Sam223141 		case MAC_PROP_DUPLEX:
46518118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
46526439Sml29623 			*(uint8_t *)pr_val = statsp->mac_stats.link_duplex;
46536439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
46546439Sml29623 			    "==> nxge_m_getprop: duplex mode %d",
46556439Sml29623 			    *(uint8_t *)pr_val));
46566439Sml29623 			break;
46576439Sml29623 
46586789Sam223141 		case MAC_PROP_SPEED:
46596439Sml29623 			if (pr_valsize < sizeof (uint64_t))
46606439Sml29623 				return (EINVAL);
46618118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
46626439Sml29623 			tmp = statsp->mac_stats.link_speed * 1000000ull;
46636439Sml29623 			bcopy(&tmp, pr_val, sizeof (tmp));
46646439Sml29623 			break;
46656439Sml29623 
46666789Sam223141 		case MAC_PROP_STATUS:
46676512Ssowmini 			if (pr_valsize < sizeof (link_state_t))
46686439Sml29623 				return (EINVAL);
46698118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
46706512Ssowmini 			if (!statsp->mac_stats.link_up)
46716512Ssowmini 				ls = LINK_STATE_DOWN;
46726512Ssowmini 			else
46736512Ssowmini 				ls = LINK_STATE_UP;
46746512Ssowmini 			bcopy(&ls, pr_val, sizeof (ls));
46756439Sml29623 			break;
46766439Sml29623 
46776789Sam223141 		case MAC_PROP_AUTONEG:
46786439Sml29623 			*(uint8_t *)pr_val =
46796439Sml29623 			    param_arr[param_autoneg].value;
46806439Sml29623 			break;
46816439Sml29623 
46826789Sam223141 		case MAC_PROP_FLOWCTRL:
46836439Sml29623 			if (pr_valsize < sizeof (link_flowctrl_t))
46846439Sml29623 				return (EINVAL);
46856439Sml29623 
46866439Sml29623 			fl = LINK_FLOWCTRL_NONE;
46876439Sml29623 			if (param_arr[param_anar_pause].value) {
46886439Sml29623 				fl = LINK_FLOWCTRL_RX;
46896439Sml29623 			}
46906439Sml29623 			bcopy(&fl, pr_val, sizeof (fl));
46916439Sml29623 			break;
46926439Sml29623 
46936789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
46948118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
46956439Sml29623 			*(uint8_t *)pr_val =
46966439Sml29623 			    param_arr[param_anar_1000fdx].value;
46976439Sml29623 			break;
46986439Sml29623 
46996789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
47006439Sml29623 			*(uint8_t *)pr_val = nxgep->param_en_1000fdx;
47016439Sml29623 			break;
47026439Sml29623 
47036789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
47048118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
47056439Sml29623 			*(uint8_t *)pr_val =
47066439Sml29623 			    param_arr[param_anar_100fdx].value;
47076439Sml29623 			break;
47086439Sml29623 
47096789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
47106439Sml29623 			*(uint8_t *)pr_val = nxgep->param_en_100fdx;
47116439Sml29623 			break;
47126439Sml29623 
47136789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
47148118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
47156439Sml29623 			*(uint8_t *)pr_val =
47166439Sml29623 			    param_arr[param_anar_10fdx].value;
47176439Sml29623 			break;
47186439Sml29623 
47196789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
47206439Sml29623 			*(uint8_t *)pr_val = nxgep->param_en_10fdx;
47216439Sml29623 			break;
47226439Sml29623 
47236789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
47246789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
47256789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
47266789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
47276789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
47286789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
47296512Ssowmini 			err = ENOTSUP;
47306512Ssowmini 			break;
47316512Ssowmini 
47326789Sam223141 		case MAC_PROP_PRIVATE:
47336512Ssowmini 			err = nxge_get_priv_prop(nxgep, pr_name, pr_flags,
47348118SVasumathi.Sundaram@Sun.COM 			    pr_valsize, pr_val, perm);
47356512Ssowmini 			break;
47369514SGirish.Moodalbail@Sun.COM 
47379514SGirish.Moodalbail@Sun.COM 		case MAC_PROP_MTU: {
47389514SGirish.Moodalbail@Sun.COM 			mac_propval_range_t	range;
47399514SGirish.Moodalbail@Sun.COM 
47409514SGirish.Moodalbail@Sun.COM 			if (!(pr_flags & MAC_PROP_POSSIBLE))
47419514SGirish.Moodalbail@Sun.COM 				return (ENOTSUP);
47429514SGirish.Moodalbail@Sun.COM 			if (pr_valsize < sizeof (mac_propval_range_t))
47439514SGirish.Moodalbail@Sun.COM 				return (EINVAL);
47449514SGirish.Moodalbail@Sun.COM 			range.mpr_count = 1;
47459514SGirish.Moodalbail@Sun.COM 			range.mpr_type = MAC_PROPVAL_UINT32;
47469514SGirish.Moodalbail@Sun.COM 			range.range_uint32[0].mpur_min =
47479514SGirish.Moodalbail@Sun.COM 			    range.range_uint32[0].mpur_max = NXGE_DEFAULT_MTU;
4748*10392SMichael.Speer@Sun.COM 			range.range_uint32[0].mpur_max = NXGE_MAXIMUM_MTU;
47499514SGirish.Moodalbail@Sun.COM 			bcopy(&range, pr_val, sizeof (range));
47509514SGirish.Moodalbail@Sun.COM 			break;
47519514SGirish.Moodalbail@Sun.COM 		}
47526512Ssowmini 		default:
47536439Sml29623 			err = EINVAL;
47546439Sml29623 			break;
47556439Sml29623 	}
47566439Sml29623 
47576439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_getprop"));
47586439Sml29623 
47596439Sml29623 	return (err);
47606439Sml29623 }
47616439Sml29623 
47626439Sml29623 /* ARGSUSED */
47636439Sml29623 static int
47646439Sml29623 nxge_set_priv_prop(p_nxge_t nxgep, const char *pr_name, uint_t pr_valsize,
47656439Sml29623     const void *pr_val)
47666439Sml29623 {
47676439Sml29623 	p_nxge_param_t	param_arr = nxgep->param_arr;
47686439Sml29623 	int		err = 0;
47696439Sml29623 	long		result;
47706439Sml29623 
47716439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47726439Sml29623 	    "==> nxge_set_priv_prop: name %s", pr_name));
47736439Sml29623 
47746439Sml29623 	/* Blanking */
47756439Sml29623 	if (strcmp(pr_name, "_rxdma_intr_time") == 0) {
47766439Sml29623 		err = nxge_param_rx_intr_time(nxgep, NULL, NULL,
47776439Sml29623 		    (char *)pr_val,
47786439Sml29623 		    (caddr_t)&param_arr[param_rxdma_intr_time]);
47796439Sml29623 		if (err) {
47806439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47816439Sml29623 			    "<== nxge_set_priv_prop: "
47826439Sml29623 			    "unable to set (%s)", pr_name));
47836439Sml29623 			err = EINVAL;
47846439Sml29623 		} else {
47856439Sml29623 			err = 0;
47866439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47876439Sml29623 			    "<== nxge_set_priv_prop: "
47886439Sml29623 			    "set (%s)", pr_name));
47896439Sml29623 		}
47906439Sml29623 
47916439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47926439Sml29623 		    "<== nxge_set_priv_prop: name %s (value %d)",
47936439Sml29623 		    pr_name, result));
47946439Sml29623 
47956439Sml29623 		return (err);
47966439Sml29623 	}
47976439Sml29623 
47986439Sml29623 	if (strcmp(pr_name, "_rxdma_intr_pkts") == 0) {
47996439Sml29623 		err = nxge_param_rx_intr_pkts(nxgep, NULL, NULL,
48006439Sml29623 		    (char *)pr_val,
48016439Sml29623 		    (caddr_t)&param_arr[param_rxdma_intr_pkts]);
48026439Sml29623 		if (err) {
48036439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48046439Sml29623 			    "<== nxge_set_priv_prop: "
48056439Sml29623 			    "unable to set (%s)", pr_name));
48066439Sml29623 			err = EINVAL;
48076439Sml29623 		} else {
48086439Sml29623 			err = 0;
48096439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48106439Sml29623 			    "<== nxge_set_priv_prop: "
48116439Sml29623 			    "set (%s)", pr_name));
48126439Sml29623 		}
48136439Sml29623 
48146439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48156439Sml29623 		    "<== nxge_set_priv_prop: name %s (value %d)",
48166439Sml29623 		    pr_name, result));
48176439Sml29623 
48186439Sml29623 		return (err);
48196439Sml29623 	}
48206439Sml29623 
48216439Sml29623 	/* Classification */
48226439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_tcp") == 0) {
48236439Sml29623 		if (pr_val == NULL) {
48246439Sml29623 			err = EINVAL;
48256439Sml29623 			return (err);
48266439Sml29623 		}
48276439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
48286439Sml29623 
48296439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
48306439Sml29623 		    NULL, (char *)pr_val,
48316439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_tcp]);
48326439Sml29623 
48336439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48346439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
48356439Sml29623 		    pr_name, result));
48366439Sml29623 
48376439Sml29623 		return (err);
48386439Sml29623 	}
48396439Sml29623 
48406439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_udp") == 0) {
48416439Sml29623 		if (pr_val == NULL) {
48426439Sml29623 			err = EINVAL;
48436439Sml29623 			return (err);
48446439Sml29623 		}
48456439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
48466439Sml29623 
48476439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
48486439Sml29623 		    NULL, (char *)pr_val,
48496439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_udp]);
48506439Sml29623 
48516439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48526439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
48536439Sml29623 		    pr_name, result));
48546439Sml29623 
48556439Sml29623 		return (err);
48566439Sml29623 	}
48576439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_ah") == 0) {
48586439Sml29623 		if (pr_val == NULL) {
48596439Sml29623 			err = EINVAL;
48606439Sml29623 			return (err);
48616439Sml29623 		}
48626439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
48636439Sml29623 
48646439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
48656439Sml29623 		    NULL, (char *)pr_val,
48666439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_ah]);
48676439Sml29623 
48686439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48696439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
48706439Sml29623 		    pr_name, result));
48716439Sml29623 
48726439Sml29623 		return (err);
48736439Sml29623 	}
48746439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_sctp") == 0) {
48756439Sml29623 		if (pr_val == NULL) {
48766439Sml29623 			err = EINVAL;
48776439Sml29623 			return (err);
48786439Sml29623 		}
48796439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
48806439Sml29623 
48816439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
48826439Sml29623 		    NULL, (char *)pr_val,
48836439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_sctp]);
48846439Sml29623 
48856439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48866439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
48876439Sml29623 		    pr_name, result));
48886439Sml29623 
48896439Sml29623 		return (err);
48906439Sml29623 	}
48916439Sml29623 
48926439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_tcp") == 0) {
48936439Sml29623 		if (pr_val == NULL) {
48946439Sml29623 			err = EINVAL;
48956439Sml29623 			return (err);
48966439Sml29623 		}
48976439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
48986439Sml29623 
48996439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
49006439Sml29623 		    NULL, (char *)pr_val,
49016439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_tcp]);
49026439Sml29623 
49036439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49046439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
49056439Sml29623 		    pr_name, result));
49066439Sml29623 
49076439Sml29623 		return (err);
49086439Sml29623 	}
49096439Sml29623 
49106439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_udp") == 0) {
49116439Sml29623 		if (pr_val == NULL) {
49126439Sml29623 			err = EINVAL;
49136439Sml29623 			return (err);
49146439Sml29623 		}
49156439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
49166439Sml29623 
49176439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
49186439Sml29623 		    NULL, (char *)pr_val,
49196439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_udp]);
49206439Sml29623 
49216439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49226439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
49236439Sml29623 		    pr_name, result));
49246439Sml29623 
49256439Sml29623 		return (err);
49266439Sml29623 	}
49276439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_ah") == 0) {
49286439Sml29623 		if (pr_val == NULL) {
49296439Sml29623 			err = EINVAL;
49306439Sml29623 			return (err);
49316439Sml29623 		}
49326439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
49336439Sml29623 
49346439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
49356439Sml29623 		    NULL, (char *)pr_val,
49366439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_ah]);
49376439Sml29623 
49386439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49396439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
49406439Sml29623 		    pr_name, result));
49416439Sml29623 
49426439Sml29623 		return (err);
49436439Sml29623 	}
49446439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_sctp") == 0) {
49456439Sml29623 		if (pr_val == NULL) {
49466439Sml29623 			err = EINVAL;
49476439Sml29623 			return (err);
49486439Sml29623 		}
49496439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
49506439Sml29623 
49516439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
49526439Sml29623 		    NULL, (char *)pr_val,
49536439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_sctp]);
49546439Sml29623 
49556439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49566439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
49576439Sml29623 		    pr_name, result));
49586439Sml29623 
49596439Sml29623 		return (err);
49606439Sml29623 	}
49616439Sml29623 
49626439Sml29623 	if (strcmp(pr_name, "_soft_lso_enable") == 0) {
49636439Sml29623 		if (pr_val == NULL) {
49646439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49656439Sml29623 			    "==> nxge_set_priv_prop: name %s (null)", pr_name));
49666439Sml29623 			err = EINVAL;
49676439Sml29623 			return (err);
49686439Sml29623 		}
49696439Sml29623 
49706439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
49716439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49726439Sml29623 		    "<== nxge_set_priv_prop: name %s "
49736439Sml29623 		    "(lso %d pr_val %s value %d)",
49746439Sml29623 		    pr_name, nxgep->soft_lso_enable, pr_val, result));
49756439Sml29623 
49766439Sml29623 		if (result > 1 || result < 0) {
49776439Sml29623 			err = EINVAL;
49786439Sml29623 		} else {
49796439Sml29623 			if (nxgep->soft_lso_enable == (uint32_t)result) {
49806439Sml29623 				NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49816439Sml29623 				    "no change (%d %d)",
49826439Sml29623 				    nxgep->soft_lso_enable, result));
49836439Sml29623 				return (0);
49846439Sml29623 			}
49856439Sml29623 		}
49866439Sml29623 
49876439Sml29623 		nxgep->soft_lso_enable = (int)result;
49886439Sml29623 
49896439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49906439Sml29623 		    "<== nxge_set_priv_prop: name %s (value %d)",
49916439Sml29623 		    pr_name, result));
49926439Sml29623 
49936439Sml29623 		return (err);
49946439Sml29623 	}
49956835Syc148097 	/*
49966835Syc148097 	 * Commands like "ndd -set /dev/nxge0 adv_10gfdx_cap 1" cause the
49976835Syc148097 	 * following code to be executed.
49986835Syc148097 	 */
49996512Ssowmini 	if (strcmp(pr_name, "_adv_10gfdx_cap") == 0) {
50006512Ssowmini 		err = nxge_param_set_mac(nxgep, NULL, NULL, (char *)pr_val,
50016512Ssowmini 		    (caddr_t)&param_arr[param_anar_10gfdx]);
50026512Ssowmini 		return (err);
50036512Ssowmini 	}
50046512Ssowmini 	if (strcmp(pr_name, "_adv_pause_cap") == 0) {
50056512Ssowmini 		err = nxge_param_set_mac(nxgep, NULL, NULL, (char *)pr_val,
50066512Ssowmini 		    (caddr_t)&param_arr[param_anar_pause]);
50076512Ssowmini 		return (err);
50086512Ssowmini 	}
50096439Sml29623 
50106439Sml29623 	return (EINVAL);
50116439Sml29623 }
50126439Sml29623 
50136439Sml29623 static int
50146512Ssowmini nxge_get_priv_prop(p_nxge_t nxgep, const char *pr_name, uint_t pr_flags,
50158118SVasumathi.Sundaram@Sun.COM     uint_t pr_valsize, void *pr_val, uint_t *perm)
50166439Sml29623 {
50176439Sml29623 	p_nxge_param_t	param_arr = nxgep->param_arr;
50186439Sml29623 	char		valstr[MAXNAMELEN];
50196439Sml29623 	int		err = EINVAL;
50206439Sml29623 	uint_t		strsize;
50216789Sam223141 	boolean_t	is_default = (pr_flags & MAC_PROP_DEFAULT);
50226439Sml29623 
50236439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50246439Sml29623 	    "==> nxge_get_priv_prop: property %s", pr_name));
50256439Sml29623 
50266439Sml29623 	/* function number */
50276439Sml29623 	if (strcmp(pr_name, "_function_number") == 0) {
50286512Ssowmini 		if (is_default)
50296512Ssowmini 			return (ENOTSUP);
50308118SVasumathi.Sundaram@Sun.COM 		*perm = MAC_PROP_PERM_READ;
50316512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%d",
50326512Ssowmini 		    nxgep->function_num);
50336439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50346439Sml29623 		    "==> nxge_get_priv_prop: name %s "
50356439Sml29623 		    "(value %d valstr %s)",
50366439Sml29623 		    pr_name, nxgep->function_num, valstr));
50376439Sml29623 
50386439Sml29623 		err = 0;
50396439Sml29623 		goto done;
50406439Sml29623 	}
50416439Sml29623 
50426439Sml29623 	/* Neptune firmware version */
50436439Sml29623 	if (strcmp(pr_name, "_fw_version") == 0) {
50446512Ssowmini 		if (is_default)
50456512Ssowmini 			return (ENOTSUP);
50468118SVasumathi.Sundaram@Sun.COM 		*perm = MAC_PROP_PERM_READ;
50476512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%s",
50486512Ssowmini 		    nxgep->vpd_info.ver);
50496439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50506439Sml29623 		    "==> nxge_get_priv_prop: name %s "
50516439Sml29623 		    "(value %d valstr %s)",
50526439Sml29623 		    pr_name, nxgep->vpd_info.ver, valstr));
50536439Sml29623 
50546439Sml29623 		err = 0;
50556439Sml29623 		goto done;
50566439Sml29623 	}
50576439Sml29623 
50586439Sml29623 	/* port PHY mode */
50596439Sml29623 	if (strcmp(pr_name, "_port_mode") == 0) {
50606512Ssowmini 		if (is_default)
50616512Ssowmini 			return (ENOTSUP);
50628118SVasumathi.Sundaram@Sun.COM 		*perm = MAC_PROP_PERM_READ;
50636439Sml29623 		switch (nxgep->mac.portmode) {
50646439Sml29623 		case PORT_1G_COPPER:
50656512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "1G copper %s",
50666439Sml29623 			    nxgep->hot_swappable_phy ?
50676439Sml29623 			    "[Hot Swappable]" : "");
50686439Sml29623 			break;
50696439Sml29623 		case PORT_1G_FIBER:
50706512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "1G fiber %s",
50716439Sml29623 			    nxgep->hot_swappable_phy ?
50726439Sml29623 			    "[hot swappable]" : "");
50736439Sml29623 			break;
50746439Sml29623 		case PORT_10G_COPPER:
50756512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
50766512Ssowmini 			    "10G copper %s",
50776439Sml29623 			    nxgep->hot_swappable_phy ?
50786439Sml29623 			    "[hot swappable]" : "");
50796439Sml29623 			break;
50806439Sml29623 		case PORT_10G_FIBER:
50816512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "10G fiber %s",
50826439Sml29623 			    nxgep->hot_swappable_phy ?
50836439Sml29623 			    "[hot swappable]" : "");
50846439Sml29623 			break;
50856439Sml29623 		case PORT_10G_SERDES:
50866512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
50876512Ssowmini 			    "10G serdes %s", nxgep->hot_swappable_phy ?
50886439Sml29623 			    "[hot swappable]" : "");
50896439Sml29623 			break;
50906439Sml29623 		case PORT_1G_SERDES:
50916512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "1G serdes %s",
50926439Sml29623 			    nxgep->hot_swappable_phy ?
50936439Sml29623 			    "[hot swappable]" : "");
50946439Sml29623 			break;
50956835Syc148097 		case PORT_1G_TN1010:
50966835Syc148097 			(void) snprintf(valstr, sizeof (valstr),
50976835Syc148097 			    "1G TN1010 copper %s", nxgep->hot_swappable_phy ?
50986835Syc148097 			    "[hot swappable]" : "");
50996835Syc148097 			break;
51006835Syc148097 		case PORT_10G_TN1010:
51016835Syc148097 			(void) snprintf(valstr, sizeof (valstr),
51026835Syc148097 			    "10G TN1010 copper %s", nxgep->hot_swappable_phy ?
51036835Syc148097 			    "[hot swappable]" : "");
51046835Syc148097 			break;
51056439Sml29623 		case PORT_1G_RGMII_FIBER:
51066512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
51076512Ssowmini 			    "1G rgmii fiber %s", nxgep->hot_swappable_phy ?
51086439Sml29623 			    "[hot swappable]" : "");
51096439Sml29623 			break;
51106439Sml29623 		case PORT_HSP_MODE:
51116512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
51126444Sml29623 			    "phy not present[hot swappable]");
51136439Sml29623 			break;
51146439Sml29623 		default:
51156512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "unknown %s",
51166439Sml29623 			    nxgep->hot_swappable_phy ?
51176439Sml29623 			    "[hot swappable]" : "");
51186439Sml29623 			break;
51196439Sml29623 		}
51206439Sml29623 
51216439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51226439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %s)",
51236439Sml29623 		    pr_name, valstr));
51246439Sml29623 
51256439Sml29623 		err = 0;
51266439Sml29623 		goto done;
51276439Sml29623 	}
51286439Sml29623 
51296439Sml29623 	/* Hot swappable PHY */
51306439Sml29623 	if (strcmp(pr_name, "_hot_swap_phy") == 0) {
51316512Ssowmini 		if (is_default)
51326512Ssowmini 			return (ENOTSUP);
51338118SVasumathi.Sundaram@Sun.COM 		*perm = MAC_PROP_PERM_READ;
51346512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%s",
51356439Sml29623 		    nxgep->hot_swappable_phy ?
51366439Sml29623 		    "yes" : "no");
51376439Sml29623 
51386439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51396439Sml29623 		    "==> nxge_get_priv_prop: name %s "
51406439Sml29623 		    "(value %d valstr %s)",
51416439Sml29623 		    pr_name, nxgep->hot_swappable_phy, valstr));
51426439Sml29623 
51436439Sml29623 		err = 0;
51446439Sml29623 		goto done;
51456439Sml29623 	}
51466439Sml29623 
51476439Sml29623 
51486439Sml29623 	/* Receive Interrupt Blanking Parameters */
51496439Sml29623 	if (strcmp(pr_name, "_rxdma_intr_time") == 0) {
51506512Ssowmini 		err = 0;
51516512Ssowmini 		if (is_default) {
51526512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
51536512Ssowmini 			    "%d", RXDMA_RCR_TO_DEFAULT);
51546512Ssowmini 			goto done;
51556512Ssowmini 		}
51566512Ssowmini 
51576512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%d",
51586512Ssowmini 		    nxgep->intr_timeout);
51596439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51606439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %d)",
51616439Sml29623 		    pr_name,
51626439Sml29623 		    (uint32_t)nxgep->intr_timeout));
51636439Sml29623 		goto done;
51646439Sml29623 	}
51656439Sml29623 
51666439Sml29623 	if (strcmp(pr_name, "_rxdma_intr_pkts") == 0) {
51676512Ssowmini 		err = 0;
51686512Ssowmini 		if (is_default) {
51696512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
51706512Ssowmini 			    "%d", RXDMA_RCR_PTHRES_DEFAULT);
51716512Ssowmini 			goto done;
51726512Ssowmini 		}
51736512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%d",
51746512Ssowmini 		    nxgep->intr_threshold);
51756439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51766439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %d)",
51776439Sml29623 		    pr_name, (uint32_t)nxgep->intr_threshold));
51786439Sml29623 
51796439Sml29623 		goto done;
51806439Sml29623 	}
51816439Sml29623 
51826439Sml29623 	/* Classification and Load Distribution Configuration */
51836439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_tcp") == 0) {
51846512Ssowmini 		if (is_default) {
51856512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
51866512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
51876512Ssowmini 			err = 0;
51886512Ssowmini 			goto done;
51896512Ssowmini 		}
51906439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
51916439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_tcp]);
51926439Sml29623 
51936512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
51946439Sml29623 		    (int)param_arr[param_class_opt_ipv4_tcp].value);
51956439Sml29623 
51966439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51976439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
51986439Sml29623 		goto done;
51996439Sml29623 	}
52006439Sml29623 
52016439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_udp") == 0) {
52026512Ssowmini 		if (is_default) {
52036512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
52046512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
52056512Ssowmini 			err = 0;
52066512Ssowmini 			goto done;
52076512Ssowmini 		}
52086439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
52096439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_udp]);
52106439Sml29623 
52116512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
52126439Sml29623 		    (int)param_arr[param_class_opt_ipv4_udp].value);
52136439Sml29623 
52146439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
52156439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
52166439Sml29623 		goto done;
52176439Sml29623 	}
52186439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_ah") == 0) {
52196512Ssowmini 		if (is_default) {
52206512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
52216512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
52226512Ssowmini 			err = 0;
52236512Ssowmini 			goto done;
52246512Ssowmini 		}
52256439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
52266439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_ah]);
52276439Sml29623 
52286512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
52296439Sml29623 		    (int)param_arr[param_class_opt_ipv4_ah].value);
52306439Sml29623 
52316439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
52326439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
52336439Sml29623 		goto done;
52346439Sml29623 	}
52356439Sml29623 
52366439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_sctp") == 0) {
52376512Ssowmini 		if (is_default) {
52386512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
52396512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
52406512Ssowmini 			err = 0;
52416512Ssowmini 			goto done;
52426512Ssowmini 		}
52436439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
52446439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_sctp]);
52456439Sml29623 
52466512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
52476439Sml29623 		    (int)param_arr[param_class_opt_ipv4_sctp].value);
52486439Sml29623 
52496439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
52506439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
52516439Sml29623 		goto done;
52526439Sml29623 	}
52536439Sml29623 
52546439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_tcp") == 0) {
52556512Ssowmini 		if (is_default) {
52566512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
52576512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
52586512Ssowmini 			err = 0;
52596512Ssowmini 			goto done;
52606512Ssowmini 		}
52616439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
52626439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_tcp]);
52636439Sml29623 
52646512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
52656439Sml29623 		    (int)param_arr[param_class_opt_ipv6_tcp].value);
52666439Sml29623 
52676439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
52686439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
52696439Sml29623 		goto done;
52706439Sml29623 	}
52716439Sml29623 
52726439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_udp") == 0) {
52736512Ssowmini 		if (is_default) {
52746512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
52756512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
52766512Ssowmini 			err = 0;
52776512Ssowmini 			goto done;
52786512Ssowmini 		}
52796439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
52806439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_udp]);
52816439Sml29623 
52826512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
52836439Sml29623 		    (int)param_arr[param_class_opt_ipv6_udp].value);
52846439Sml29623 
52856439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
52866439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
52876439Sml29623 		goto done;
52886439Sml29623 	}
52896439Sml29623 
52906439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_ah") == 0) {
52916512Ssowmini 		if (is_default) {
52926512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
52936512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
52946512Ssowmini 			err = 0;
52956512Ssowmini 			goto done;
52966512Ssowmini 		}
52976439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
52986439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_ah]);
52996439Sml29623 
53006512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
53016439Sml29623 		    (int)param_arr[param_class_opt_ipv6_ah].value);
53026439Sml29623 
53036439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53046439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
53056439Sml29623 		goto done;
53066439Sml29623 	}
53076439Sml29623 
53086439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_sctp") == 0) {
53096512Ssowmini 		if (is_default) {
53106512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
53116512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
53126512Ssowmini 			err = 0;
53136512Ssowmini 			goto done;
53146512Ssowmini 		}
53156439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
53166439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_sctp]);
53176439Sml29623 
53186512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
53196439Sml29623 		    (int)param_arr[param_class_opt_ipv6_sctp].value);
53206439Sml29623 
53216439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53226439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
53236439Sml29623 		goto done;
53246439Sml29623 	}
53256439Sml29623 
53266439Sml29623 	/* Software LSO */
53276439Sml29623 	if (strcmp(pr_name, "_soft_lso_enable") == 0) {
53286512Ssowmini 		if (is_default) {
53296512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 0);
53306512Ssowmini 			err = 0;
53316512Ssowmini 			goto done;
53326512Ssowmini 		}
53336512Ssowmini 		(void) snprintf(valstr, sizeof (valstr),
53346512Ssowmini 		    "%d", nxgep->soft_lso_enable);
53356439Sml29623 		err = 0;
53366439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53376439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %d)",
53386439Sml29623 		    pr_name, nxgep->soft_lso_enable));
53396439Sml29623 
53406439Sml29623 		goto done;
53416439Sml29623 	}
53426512Ssowmini 	if (strcmp(pr_name, "_adv_10gfdx_cap") == 0) {
53436512Ssowmini 		err = 0;
53446512Ssowmini 		if (is_default ||
53456512Ssowmini 		    nxgep->param_arr[param_anar_10gfdx].value != 0) {
53466512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 1);
53476512Ssowmini 			goto done;
53486512Ssowmini 		} else {
53496512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 0);
53506512Ssowmini 			goto done;
53516512Ssowmini 		}
53526512Ssowmini 	}
53536512Ssowmini 	if (strcmp(pr_name, "_adv_pause_cap") == 0) {
53546512Ssowmini 		err = 0;
53556512Ssowmini 		if (is_default ||
53566512Ssowmini 		    nxgep->param_arr[param_anar_pause].value != 0) {
53576512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 1);
53586512Ssowmini 			goto done;
53596512Ssowmini 		} else {
53606512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 0);
53616512Ssowmini 			goto done;
53626512Ssowmini 		}
53636512Ssowmini 	}
53646439Sml29623 
53656439Sml29623 done:
53666439Sml29623 	if (err == 0) {
53676439Sml29623 		strsize = (uint_t)strlen(valstr);
53686439Sml29623 		if (pr_valsize < strsize) {
53696439Sml29623 			err = ENOBUFS;
53706439Sml29623 		} else {
53716439Sml29623 			(void) strlcpy(pr_val, valstr, pr_valsize);
53726439Sml29623 		}
53736439Sml29623 	}
53746439Sml29623 
53756439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53766439Sml29623 	    "<== nxge_get_priv_prop: return %d", err));
53776439Sml29623 	return (err);
53786439Sml29623 }
53796439Sml29623 
53803859Sml29623 /*
53813859Sml29623  * Module loading and removing entry points.
53823859Sml29623  */
53833859Sml29623 
53846705Sml29623 DDI_DEFINE_STREAM_OPS(nxge_dev_ops, nulldev, nulldev, nxge_attach, nxge_detach,
53857656SSherry.Moore@Sun.COM     nodev, NULL, D_MP, NULL, nxge_quiesce);
53863859Sml29623 
53874977Sraghus #define	NXGE_DESC_VER		"Sun NIU 10Gb Ethernet"
53883859Sml29623 
53893859Sml29623 /*
53903859Sml29623  * Module linkage information for the kernel.
53913859Sml29623  */
53923859Sml29623 static struct modldrv 	nxge_modldrv = {
53933859Sml29623 	&mod_driverops,
53943859Sml29623 	NXGE_DESC_VER,
53953859Sml29623 	&nxge_dev_ops
53963859Sml29623 };
53973859Sml29623 
53983859Sml29623 static struct modlinkage modlinkage = {
53993859Sml29623 	MODREV_1, (void *) &nxge_modldrv, NULL
54003859Sml29623 };
54013859Sml29623 
54023859Sml29623 int
54033859Sml29623 _init(void)
54043859Sml29623 {
54053859Sml29623 	int		status;
54063859Sml29623 
54079935SMichael.Speer@Sun.COM 	MUTEX_INIT(&nxgedebuglock, NULL, MUTEX_DRIVER, NULL);
54089935SMichael.Speer@Sun.COM 
54093859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "==> _init"));
54109935SMichael.Speer@Sun.COM 
54113859Sml29623 	mac_init_ops(&nxge_dev_ops, "nxge");
54129935SMichael.Speer@Sun.COM 
54133859Sml29623 	status = ddi_soft_state_init(&nxge_list, sizeof (nxge_t), 0);
54143859Sml29623 	if (status != 0) {
54153859Sml29623 		NXGE_ERROR_MSG((NULL, NXGE_ERR_CTL,
54166512Ssowmini 		    "failed to init device soft state"));
54173859Sml29623 		goto _init_exit;
54183859Sml29623 	}
54199935SMichael.Speer@Sun.COM 
54203859Sml29623 	status = mod_install(&modlinkage);
54213859Sml29623 	if (status != 0) {
54223859Sml29623 		ddi_soft_state_fini(&nxge_list);
54233859Sml29623 		NXGE_ERROR_MSG((NULL, NXGE_ERR_CTL, "Mod install failed"));
54243859Sml29623 		goto _init_exit;
54253859Sml29623 	}
54263859Sml29623 
54273859Sml29623 	MUTEX_INIT(&nxge_common_lock, NULL, MUTEX_DRIVER, NULL);
54283859Sml29623 
54299935SMichael.Speer@Sun.COM 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "<== _init status = 0x%X", status));
54309935SMichael.Speer@Sun.COM 	return (status);
54319935SMichael.Speer@Sun.COM 
54323859Sml29623 _init_exit:
54339935SMichael.Speer@Sun.COM 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "<== _init status = 0x%X", status));
54349935SMichael.Speer@Sun.COM 	MUTEX_DESTROY(&nxgedebuglock);
54353859Sml29623 	return (status);
54363859Sml29623 }
54373859Sml29623 
54383859Sml29623 int
54393859Sml29623 _fini(void)
54403859Sml29623 {
54413859Sml29623 	int		status;
54423859Sml29623 
54433859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "==> _fini"));
54443859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "==> _fini: mod_remove"));
54453859Sml29623 
54463859Sml29623 	if (nxge_mblks_pending)
54473859Sml29623 		return (EBUSY);
54483859Sml29623 
54493859Sml29623 	status = mod_remove(&modlinkage);
54503859Sml29623 	if (status != DDI_SUCCESS) {
54513859Sml29623 		NXGE_DEBUG_MSG((NULL, MOD_CTL,
54526512Ssowmini 		    "Module removal failed 0x%08x",
54536512Ssowmini 		    status));
54543859Sml29623 		goto _fini_exit;
54553859Sml29623 	}
54563859Sml29623 
54573859Sml29623 	mac_fini_ops(&nxge_dev_ops);
54583859Sml29623 
54593859Sml29623 	ddi_soft_state_fini(&nxge_list);
54603859Sml29623 
54619935SMichael.Speer@Sun.COM 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "<== _fini status = 0x%08x", status));
54629935SMichael.Speer@Sun.COM 
54633859Sml29623 	MUTEX_DESTROY(&nxge_common_lock);
54649935SMichael.Speer@Sun.COM 	MUTEX_DESTROY(&nxgedebuglock);
54659935SMichael.Speer@Sun.COM 	return (status);
54669935SMichael.Speer@Sun.COM 
54673859Sml29623 _fini_exit:
54689935SMichael.Speer@Sun.COM 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "<== _fini status = 0x%08x", status));
54693859Sml29623 	return (status);
54703859Sml29623 }
54713859Sml29623 
54723859Sml29623 int
54733859Sml29623 _info(struct modinfo *modinfop)
54743859Sml29623 {
54753859Sml29623 	int		status;
54763859Sml29623 
54773859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "==> _info"));
54783859Sml29623 	status = mod_info(&modlinkage, modinfop);
54793859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, " _info status = 0x%X", status));
54803859Sml29623 
54813859Sml29623 	return (status);
54823859Sml29623 }
54833859Sml29623 
54843859Sml29623 /*ARGSUSED*/
54858275SEric Cheng static int
54868275SEric Cheng nxge_tx_ring_start(mac_ring_driver_t rdriver, uint64_t mr_gen_num)
54878275SEric Cheng {
54888275SEric Cheng 	p_nxge_ring_handle_t	rhp = (p_nxge_ring_handle_t)rdriver;
54898275SEric Cheng 	p_nxge_t		nxgep = rhp->nxgep;
54908275SEric Cheng 	uint32_t		channel;
54918275SEric Cheng 	p_tx_ring_t		ring;
54928275SEric Cheng 
54938275SEric Cheng 	channel = nxgep->pt_config.hw_config.tdc.start + rhp->index;
54948275SEric Cheng 	ring = nxgep->tx_rings->rings[channel];
54958275SEric Cheng 
54968275SEric Cheng 	MUTEX_ENTER(&ring->lock);
54978275SEric Cheng 	ring->tx_ring_handle = rhp->ring_handle;
54988275SEric Cheng 	MUTEX_EXIT(&ring->lock);
54998275SEric Cheng 
55008275SEric Cheng 	return (0);
55018275SEric Cheng }
55028275SEric Cheng 
55038275SEric Cheng static void
55048275SEric Cheng nxge_tx_ring_stop(mac_ring_driver_t rdriver)
55058275SEric Cheng {
55068275SEric Cheng 	p_nxge_ring_handle_t	rhp = (p_nxge_ring_handle_t)rdriver;
55078275SEric Cheng 	p_nxge_t		nxgep = rhp->nxgep;
55088275SEric Cheng 	uint32_t		channel;
55098275SEric Cheng 	p_tx_ring_t		ring;
55108275SEric Cheng 
55118275SEric Cheng 	channel = nxgep->pt_config.hw_config.tdc.start + rhp->index;
55128275SEric Cheng 	ring = nxgep->tx_rings->rings[channel];
55138275SEric Cheng 
55148275SEric Cheng 	MUTEX_ENTER(&ring->lock);
55158275SEric Cheng 	ring->tx_ring_handle = (mac_ring_handle_t)NULL;
55168275SEric Cheng 	MUTEX_EXIT(&ring->lock);
55178275SEric Cheng }
55188275SEric Cheng 
55198275SEric Cheng static int
55208275SEric Cheng nxge_rx_ring_start(mac_ring_driver_t rdriver, uint64_t mr_gen_num)
55218275SEric Cheng {
55228275SEric Cheng 	p_nxge_ring_handle_t	rhp = (p_nxge_ring_handle_t)rdriver;
55238275SEric Cheng 	p_nxge_t		nxgep = rhp->nxgep;
55248275SEric Cheng 	uint32_t		channel;
55258275SEric Cheng 	p_rx_rcr_ring_t		ring;
55268275SEric Cheng 	int			i;
55278275SEric Cheng 
55288275SEric Cheng 	channel = nxgep->pt_config.hw_config.start_rdc + rhp->index;
55298275SEric Cheng 	ring =  nxgep->rx_rcr_rings->rcr_rings[channel];
55308275SEric Cheng 
55318275SEric Cheng 	MUTEX_ENTER(&ring->lock);
55328275SEric Cheng 
55338275SEric Cheng 	if (nxgep->rx_channel_started[channel] == B_TRUE) {
55348275SEric Cheng 		MUTEX_EXIT(&ring->lock);
55358275SEric Cheng 		return (0);
55368275SEric Cheng 	}
55378275SEric Cheng 
55388275SEric Cheng 	/* set rcr_ring */
55398275SEric Cheng 	for (i = 0; i < nxgep->ldgvp->maxldvs; i++) {
55408275SEric Cheng 		if ((nxgep->ldgvp->ldvp[i].is_rxdma == 1) &&
55418275SEric Cheng 		    (nxgep->ldgvp->ldvp[i].channel == channel)) {
55428275SEric Cheng 			ring->ldvp = &nxgep->ldgvp->ldvp[i];
55438275SEric Cheng 			ring->ldgp = nxgep->ldgvp->ldvp[i].ldgp;
55448275SEric Cheng 		}
55458275SEric Cheng 	}
55468275SEric Cheng 
55478275SEric Cheng 	nxgep->rx_channel_started[channel] = B_TRUE;
55488275SEric Cheng 	ring->rcr_mac_handle = rhp->ring_handle;
55498275SEric Cheng 	ring->rcr_gen_num = mr_gen_num;
55508275SEric Cheng 	MUTEX_EXIT(&ring->lock);
55518275SEric Cheng 
55528275SEric Cheng 	return (0);
55538275SEric Cheng }
55548275SEric Cheng 
55558275SEric Cheng static void
55568275SEric Cheng nxge_rx_ring_stop(mac_ring_driver_t rdriver)
55578275SEric Cheng {
55588275SEric Cheng 	p_nxge_ring_handle_t	rhp = (p_nxge_ring_handle_t)rdriver;
55598275SEric Cheng 	p_nxge_t		nxgep = rhp->nxgep;
55608275SEric Cheng 	uint32_t		channel;
55618275SEric Cheng 	p_rx_rcr_ring_t		ring;
55628275SEric Cheng 
55638275SEric Cheng 	channel = nxgep->pt_config.hw_config.start_rdc + rhp->index;
55648275SEric Cheng 	ring =  nxgep->rx_rcr_rings->rcr_rings[channel];
55658275SEric Cheng 
55668275SEric Cheng 	MUTEX_ENTER(&ring->lock);
55678275SEric Cheng 	nxgep->rx_channel_started[channel] = B_FALSE;
55688275SEric Cheng 	ring->rcr_mac_handle = NULL;
55698275SEric Cheng 	MUTEX_EXIT(&ring->lock);
55708275SEric Cheng }
55718275SEric Cheng 
55728275SEric Cheng /*
55738275SEric Cheng  * Callback funtion for MAC layer to register all rings.
55748275SEric Cheng  */
55758275SEric Cheng static void
55768275SEric Cheng nxge_fill_ring(void *arg, mac_ring_type_t rtype, const int rg_index,
55778275SEric Cheng     const int index, mac_ring_info_t *infop, mac_ring_handle_t rh)
55788275SEric Cheng {
55798275SEric Cheng 	p_nxge_t		nxgep = (p_nxge_t)arg;
55808275SEric Cheng 	p_nxge_hw_pt_cfg_t	p_cfgp = &nxgep->pt_config.hw_config;
55818275SEric Cheng 
55828275SEric Cheng 	NXGE_DEBUG_MSG((nxgep, TX_CTL,
55838275SEric Cheng 	    "==> nxge_fill_ring 0x%x index %d", rtype, index));
55848275SEric Cheng 
55858275SEric Cheng 	switch (rtype) {
55868275SEric Cheng 	case MAC_RING_TYPE_TX: {
55878275SEric Cheng 		p_nxge_ring_handle_t	rhandlep;
55888275SEric Cheng 
55898275SEric Cheng 		NXGE_DEBUG_MSG((nxgep, TX_CTL,
55908275SEric Cheng 		    "==> nxge_fill_ring (TX) 0x%x index %d ntdcs %d",
55918275SEric Cheng 		    rtype, index, p_cfgp->tdc.count));
55928275SEric Cheng 
55938275SEric Cheng 		ASSERT((index >= 0) && (index < p_cfgp->tdc.count));
55948275SEric Cheng 		rhandlep = &nxgep->tx_ring_handles[index];
55958275SEric Cheng 		rhandlep->nxgep = nxgep;
55968275SEric Cheng 		rhandlep->index = index;
55978275SEric Cheng 		rhandlep->ring_handle = rh;
55988275SEric Cheng 
55998275SEric Cheng 		infop->mri_driver = (mac_ring_driver_t)rhandlep;
56008275SEric Cheng 		infop->mri_start = nxge_tx_ring_start;
56018275SEric Cheng 		infop->mri_stop = nxge_tx_ring_stop;
56028275SEric Cheng 		infop->mri_tx = nxge_tx_ring_send;
56038275SEric Cheng 
56048275SEric Cheng 		break;
56058275SEric Cheng 	}
56068275SEric Cheng 	case MAC_RING_TYPE_RX: {
56078275SEric Cheng 		p_nxge_ring_handle_t	rhandlep;
56088275SEric Cheng 		int			nxge_rindex;
56098275SEric Cheng 		mac_intr_t		nxge_mac_intr;
56108275SEric Cheng 
56118275SEric Cheng 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
56128275SEric Cheng 		    "==> nxge_fill_ring (RX) 0x%x index %d nrdcs %d",
56138275SEric Cheng 		    rtype, index, p_cfgp->max_rdcs));
56148275SEric Cheng 
56158275SEric Cheng 		/*
56168275SEric Cheng 		 * 'index' is the ring index within the group.
56178275SEric Cheng 		 * Find the ring index in the nxge instance.
56188275SEric Cheng 		 */
56198275SEric Cheng 		nxge_rindex = nxge_get_rxring_index(nxgep, rg_index, index);
56208275SEric Cheng 
56218275SEric Cheng 		ASSERT((nxge_rindex >= 0) && (nxge_rindex < p_cfgp->max_rdcs));
56228275SEric Cheng 		rhandlep = &nxgep->rx_ring_handles[nxge_rindex];
56238275SEric Cheng 		rhandlep->nxgep = nxgep;
56248275SEric Cheng 		rhandlep->index = nxge_rindex;
56258275SEric Cheng 		rhandlep->ring_handle = rh;
56268275SEric Cheng 
56278275SEric Cheng 		/*
56288275SEric Cheng 		 * Entrypoint to enable interrupt (disable poll) and
56298275SEric Cheng 		 * disable interrupt (enable poll).
56308275SEric Cheng 		 */
56318275SEric Cheng 		nxge_mac_intr.mi_handle = (mac_intr_handle_t)rhandlep;
56328275SEric Cheng 		nxge_mac_intr.mi_enable = (mac_intr_enable_t)nxge_disable_poll;
56338275SEric Cheng 		nxge_mac_intr.mi_disable = (mac_intr_disable_t)nxge_enable_poll;
56348275SEric Cheng 		infop->mri_driver = (mac_ring_driver_t)rhandlep;
56358275SEric Cheng 		infop->mri_start = nxge_rx_ring_start;
56368275SEric Cheng 		infop->mri_stop = nxge_rx_ring_stop;
56378275SEric Cheng 		infop->mri_intr = nxge_mac_intr; /* ??? */
56388275SEric Cheng 		infop->mri_poll = nxge_rx_poll;
56398275SEric Cheng 
56408275SEric Cheng 		break;
56418275SEric Cheng 	}
56428275SEric Cheng 	default:
56438275SEric Cheng 		break;
56448275SEric Cheng 	}
56458275SEric Cheng 
56468275SEric Cheng 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_fill_ring 0x%x",
56478275SEric Cheng 	    rtype));
56488275SEric Cheng }
56498275SEric Cheng 
56508275SEric Cheng static void
56518275SEric Cheng nxge_group_add_ring(mac_group_driver_t gh, mac_ring_driver_t rh,
56528275SEric Cheng     mac_ring_type_t type)
56538275SEric Cheng {
56548275SEric Cheng 	nxge_ring_group_t	*rgroup = (nxge_ring_group_t *)gh;
56558275SEric Cheng 	nxge_ring_handle_t	*rhandle = (nxge_ring_handle_t *)rh;
56568275SEric Cheng 	nxge_t			*nxge;
56578275SEric Cheng 	nxge_grp_t		*grp;
56588275SEric Cheng 	nxge_rdc_grp_t		*rdc_grp;
56598275SEric Cheng 	uint16_t		channel;	/* device-wise ring id */
56608275SEric Cheng 	int			dev_gindex;
56618275SEric Cheng 	int			rv;
56628275SEric Cheng 
56638275SEric Cheng 	nxge = rgroup->nxgep;
56648275SEric Cheng 
56658275SEric Cheng 	switch (type) {
56668275SEric Cheng 	case MAC_RING_TYPE_TX:
56678275SEric Cheng 		/*
56688275SEric Cheng 		 * nxge_grp_dc_add takes a channel number which is a
56698275SEric Cheng 		 * "devise" ring ID.
56708275SEric Cheng 		 */
56718275SEric Cheng 		channel = nxge->pt_config.hw_config.tdc.start + rhandle->index;
56728275SEric Cheng 
56738275SEric Cheng 		/*
56748275SEric Cheng 		 * Remove the ring from the default group
56758275SEric Cheng 		 */
56768275SEric Cheng 		if (rgroup->gindex != 0) {
56778275SEric Cheng 			(void) nxge_grp_dc_remove(nxge, VP_BOUND_TX, channel);
56788275SEric Cheng 		}
56798275SEric Cheng 
56808275SEric Cheng 		/*
56818275SEric Cheng 		 * nxge->tx_set.group[] is an array of groups indexed by
56828275SEric Cheng 		 * a "port" group ID.
56838275SEric Cheng 		 */
56848275SEric Cheng 		grp = nxge->tx_set.group[rgroup->gindex];
56858275SEric Cheng 		rv = nxge_grp_dc_add(nxge, grp, VP_BOUND_TX, channel);
56868275SEric Cheng 		if (rv != 0) {
56878275SEric Cheng 			NXGE_ERROR_MSG((nxge, NXGE_ERR_CTL,
56888275SEric Cheng 			    "nxge_group_add_ring: nxge_grp_dc_add failed"));
56898275SEric Cheng 		}
56908275SEric Cheng 		break;
56918275SEric Cheng 
56928275SEric Cheng 	case MAC_RING_TYPE_RX:
56938275SEric Cheng 		/*
56948275SEric Cheng 		 * nxge->rx_set.group[] is an array of groups indexed by
56958275SEric Cheng 		 * a "port" group ID.
56968275SEric Cheng 		 */
56978275SEric Cheng 		grp = nxge->rx_set.group[rgroup->gindex];
56988275SEric Cheng 
56998275SEric Cheng 		dev_gindex = nxge->pt_config.hw_config.def_mac_rxdma_grpid +
57008275SEric Cheng 		    rgroup->gindex;
57018275SEric Cheng 		rdc_grp = &nxge->pt_config.rdc_grps[dev_gindex];
57028275SEric Cheng 
57038275SEric Cheng 		/*
57048275SEric Cheng 		 * nxge_grp_dc_add takes a channel number which is a
57058275SEric Cheng 		 * "devise" ring ID.
57068275SEric Cheng 		 */
57078275SEric Cheng 		channel = nxge->pt_config.hw_config.start_rdc + rhandle->index;
57088275SEric Cheng 		rv = nxge_grp_dc_add(nxge, grp, VP_BOUND_RX, channel);
57098275SEric Cheng 		if (rv != 0) {
57108275SEric Cheng 			NXGE_ERROR_MSG((nxge, NXGE_ERR_CTL,
57118275SEric Cheng 			    "nxge_group_add_ring: nxge_grp_dc_add failed"));
57128275SEric Cheng 		}
57138275SEric Cheng 
57148275SEric Cheng 		rdc_grp->map |= (1 << channel);
57158275SEric Cheng 		rdc_grp->max_rdcs++;
57168275SEric Cheng 
57179047SMichael.Speer@Sun.COM 		(void) nxge_init_fzc_rdc_tbl(nxge, rdc_grp, rgroup->rdctbl);
57188275SEric Cheng 		break;
57198275SEric Cheng 	}
57208275SEric Cheng }
57218275SEric Cheng 
57228275SEric Cheng static void
57238275SEric Cheng nxge_group_rem_ring(mac_group_driver_t gh, mac_ring_driver_t rh,
57248275SEric Cheng     mac_ring_type_t type)
57258275SEric Cheng {
57268275SEric Cheng 	nxge_ring_group_t	*rgroup = (nxge_ring_group_t *)gh;
57278275SEric Cheng 	nxge_ring_handle_t	*rhandle = (nxge_ring_handle_t *)rh;
57288275SEric Cheng 	nxge_t			*nxge;
57298275SEric Cheng 	uint16_t		channel;	/* device-wise ring id */
57308275SEric Cheng 	nxge_rdc_grp_t		*rdc_grp;
57318275SEric Cheng 	int			dev_gindex;
57328275SEric Cheng 
57338275SEric Cheng 	nxge = rgroup->nxgep;
57348275SEric Cheng 
57358275SEric Cheng 	switch (type) {
57368275SEric Cheng 	case MAC_RING_TYPE_TX:
57378275SEric Cheng 		dev_gindex = nxge->pt_config.hw_config.def_mac_txdma_grpid +
57388275SEric Cheng 		    rgroup->gindex;
57398275SEric Cheng 		channel = nxge->pt_config.hw_config.tdc.start + rhandle->index;
57408275SEric Cheng 		nxge_grp_dc_remove(nxge, VP_BOUND_TX, channel);
57418275SEric Cheng 
57428275SEric Cheng 		/*
57438275SEric Cheng 		 * Add the ring back to the default group
57448275SEric Cheng 		 */
57458275SEric Cheng 		if (rgroup->gindex != 0) {
57468275SEric Cheng 			nxge_grp_t *grp;
57478275SEric Cheng 			grp = nxge->tx_set.group[0];
57488275SEric Cheng 			(void) nxge_grp_dc_add(nxge, grp, VP_BOUND_TX, channel);
57498275SEric Cheng 		}
57508275SEric Cheng 		break;
57518275SEric Cheng 
57528275SEric Cheng 	case MAC_RING_TYPE_RX:
57538275SEric Cheng 		dev_gindex = nxge->pt_config.hw_config.def_mac_rxdma_grpid +
57548275SEric Cheng 		    rgroup->gindex;
57558275SEric Cheng 		rdc_grp = &nxge->pt_config.rdc_grps[dev_gindex];
57568275SEric Cheng 		channel = rdc_grp->start_rdc + rhandle->index;
57578275SEric Cheng 		nxge_grp_dc_remove(nxge, VP_BOUND_RX, channel);
57588275SEric Cheng 
57598275SEric Cheng 		rdc_grp->map &= ~(1 << channel);
57608275SEric Cheng 		rdc_grp->max_rdcs--;
57618275SEric Cheng 
57629047SMichael.Speer@Sun.COM 		(void) nxge_init_fzc_rdc_tbl(nxge, rdc_grp, rgroup->rdctbl);
57638275SEric Cheng 		break;
57648275SEric Cheng 	}
57658275SEric Cheng }
57668275SEric Cheng 
57678275SEric Cheng 
57688275SEric Cheng /*ARGSUSED*/
57693859Sml29623 static nxge_status_t
57703859Sml29623 nxge_add_intrs(p_nxge_t nxgep)
57713859Sml29623 {
57723859Sml29623 
57733859Sml29623 	int		intr_types;
57743859Sml29623 	int		type = 0;
57753859Sml29623 	int		ddi_status = DDI_SUCCESS;
57763859Sml29623 	nxge_status_t	status = NXGE_OK;
57773859Sml29623 
57783859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs"));
57793859Sml29623 
57803859Sml29623 	nxgep->nxge_intr_type.intr_registered = B_FALSE;
57813859Sml29623 	nxgep->nxge_intr_type.intr_enabled = B_FALSE;
57823859Sml29623 	nxgep->nxge_intr_type.msi_intx_cnt = 0;
57833859Sml29623 	nxgep->nxge_intr_type.intr_added = 0;
57843859Sml29623 	nxgep->nxge_intr_type.niu_msi_enable = B_FALSE;
57853859Sml29623 	nxgep->nxge_intr_type.intr_type = 0;
57863859Sml29623 
57873859Sml29623 	if (nxgep->niu_type == N2_NIU) {
57883859Sml29623 		nxgep->nxge_intr_type.niu_msi_enable = B_TRUE;
57893859Sml29623 	} else if (nxge_msi_enable) {
57903859Sml29623 		nxgep->nxge_intr_type.niu_msi_enable = B_TRUE;
57913859Sml29623 	}
57923859Sml29623 
57933859Sml29623 	/* Get the supported interrupt types */
57943859Sml29623 	if ((ddi_status = ddi_intr_get_supported_types(nxgep->dip, &intr_types))
57956512Ssowmini 	    != DDI_SUCCESS) {
57963859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "<== nxge_add_intrs: "
57976512Ssowmini 		    "ddi_intr_get_supported_types failed: status 0x%08x",
57986512Ssowmini 		    ddi_status));
57993859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
58003859Sml29623 	}
58013859Sml29623 	nxgep->nxge_intr_type.intr_types = intr_types;
58023859Sml29623 
58033859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
58046512Ssowmini 	    "ddi_intr_get_supported_types: 0x%08x", intr_types));
58053859Sml29623 
58063859Sml29623 	/*
58073859Sml29623 	 * Solaris MSIX is not supported yet. use MSI for now.
58083859Sml29623 	 * nxge_msi_enable (1):
58093859Sml29623 	 *	1 - MSI		2 - MSI-X	others - FIXED
58103859Sml29623 	 */
58113859Sml29623 	switch (nxge_msi_enable) {
58123859Sml29623 	default:
58133859Sml29623 		type = DDI_INTR_TYPE_FIXED;
58143859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs: "
58156512Ssowmini 		    "use fixed (intx emulation) type %08x",
58166512Ssowmini 		    type));
58173859Sml29623 		break;
58183859Sml29623 
58193859Sml29623 	case 2:
58203859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs: "
58216512Ssowmini 		    "ddi_intr_get_supported_types: 0x%08x", intr_types));
58223859Sml29623 		if (intr_types & DDI_INTR_TYPE_MSIX) {
58233859Sml29623 			type = DDI_INTR_TYPE_MSIX;
58243859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
58256512Ssowmini 			    "ddi_intr_get_supported_types: MSIX 0x%08x",
58266512Ssowmini 			    type));
58273859Sml29623 		} else if (intr_types & DDI_INTR_TYPE_MSI) {
58283859Sml29623 			type = DDI_INTR_TYPE_MSI;
58293859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
58306512Ssowmini 			    "ddi_intr_get_supported_types: MSI 0x%08x",
58316512Ssowmini 			    type));
58323859Sml29623 		} else if (intr_types & DDI_INTR_TYPE_FIXED) {
58333859Sml29623 			type = DDI_INTR_TYPE_FIXED;
58343859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs: "
58356512Ssowmini 			    "ddi_intr_get_supported_types: MSXED0x%08x",
58366512Ssowmini 			    type));
58373859Sml29623 		}
58383859Sml29623 		break;
58393859Sml29623 
58403859Sml29623 	case 1:
58413859Sml29623 		if (intr_types & DDI_INTR_TYPE_MSI) {
58423859Sml29623 			type = DDI_INTR_TYPE_MSI;
58433859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs: "
58446512Ssowmini 			    "ddi_intr_get_supported_types: MSI 0x%08x",
58456512Ssowmini 			    type));
58463859Sml29623 		} else if (intr_types & DDI_INTR_TYPE_MSIX) {
58473859Sml29623 			type = DDI_INTR_TYPE_MSIX;
58483859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
58496512Ssowmini 			    "ddi_intr_get_supported_types: MSIX 0x%08x",
58506512Ssowmini 			    type));
58513859Sml29623 		} else if (intr_types & DDI_INTR_TYPE_FIXED) {
58523859Sml29623 			type = DDI_INTR_TYPE_FIXED;
58533859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
58546512Ssowmini 			    "ddi_intr_get_supported_types: MSXED0x%08x",
58556512Ssowmini 			    type));
58563859Sml29623 		}
58573859Sml29623 	}
58583859Sml29623 
58593859Sml29623 	nxgep->nxge_intr_type.intr_type = type;
58603859Sml29623 	if ((type == DDI_INTR_TYPE_MSIX || type == DDI_INTR_TYPE_MSI ||
58616512Ssowmini 	    type == DDI_INTR_TYPE_FIXED) &&
58626512Ssowmini 	    nxgep->nxge_intr_type.niu_msi_enable) {
58633859Sml29623 		if ((status = nxge_add_intrs_adv(nxgep)) != DDI_SUCCESS) {
58643859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
58656512Ssowmini 			    " nxge_add_intrs: "
58666512Ssowmini 			    " nxge_add_intrs_adv failed: status 0x%08x",
58676512Ssowmini 			    status));
58683859Sml29623 			return (status);
58693859Sml29623 		} else {
58703859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
58716512Ssowmini 			    "interrupts registered : type %d", type));
58723859Sml29623 			nxgep->nxge_intr_type.intr_registered = B_TRUE;
58733859Sml29623 
58743859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
58756512Ssowmini 			    "\nAdded advanced nxge add_intr_adv "
58766512Ssowmini 			    "intr type 0x%x\n", type));
58773859Sml29623 
58783859Sml29623 			return (status);
58793859Sml29623 		}
58803859Sml29623 	}
58813859Sml29623 
58823859Sml29623 	if (!nxgep->nxge_intr_type.intr_registered) {
58833859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "==> nxge_add_intrs: "
58846512Ssowmini 		    "failed to register interrupts"));
58853859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
58863859Sml29623 	}
58873859Sml29623 
58883859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_add_intrs"));
58893859Sml29623 	return (status);
58903859Sml29623 }
58913859Sml29623 
58923859Sml29623 static nxge_status_t
58933859Sml29623 nxge_add_intrs_adv(p_nxge_t nxgep)
58943859Sml29623 {
58953859Sml29623 	int		intr_type;
58963859Sml29623 	p_nxge_intr_t	intrp;
58973859Sml29623 
58983859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs_adv"));
58993859Sml29623 
59003859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
59013859Sml29623 	intr_type = intrp->intr_type;
59023859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs_adv: type 0x%x",
59036512Ssowmini 	    intr_type));
59043859Sml29623 
59053859Sml29623 	switch (intr_type) {
59063859Sml29623 	case DDI_INTR_TYPE_MSI: /* 0x2 */
59073859Sml29623 	case DDI_INTR_TYPE_MSIX: /* 0x4 */
59083859Sml29623 		return (nxge_add_intrs_adv_type(nxgep, intr_type));
59093859Sml29623 
59103859Sml29623 	case DDI_INTR_TYPE_FIXED: /* 0x1 */
59113859Sml29623 		return (nxge_add_intrs_adv_type_fix(nxgep, intr_type));
59123859Sml29623 
59133859Sml29623 	default:
59143859Sml29623 		return (NXGE_ERROR);
59153859Sml29623 	}
59163859Sml29623 }
59173859Sml29623 
59183859Sml29623 
59193859Sml29623 /*ARGSUSED*/
59203859Sml29623 static nxge_status_t
59213859Sml29623 nxge_add_intrs_adv_type(p_nxge_t nxgep, uint32_t int_type)
59223859Sml29623 {
59233859Sml29623 	dev_info_t		*dip = nxgep->dip;
59243859Sml29623 	p_nxge_ldg_t		ldgp;
59253859Sml29623 	p_nxge_intr_t		intrp;
59263859Sml29623 	uint_t			*inthandler;
59273859Sml29623 	void			*arg1, *arg2;
59283859Sml29623 	int			behavior;
59295013Sml29623 	int			nintrs, navail, nrequest;
59303859Sml29623 	int			nactual, nrequired;
59313859Sml29623 	int			inum = 0;
59323859Sml29623 	int			x, y;
59333859Sml29623 	int			ddi_status = DDI_SUCCESS;
59343859Sml29623 	nxge_status_t		status = NXGE_OK;
59353859Sml29623 
59363859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs_adv_type"));
59373859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
59383859Sml29623 	intrp->start_inum = 0;
59393859Sml29623 
59403859Sml29623 	ddi_status = ddi_intr_get_nintrs(dip, int_type, &nintrs);
59413859Sml29623 	if ((ddi_status != DDI_SUCCESS) || (nintrs == 0)) {
59423859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
59436512Ssowmini 		    "ddi_intr_get_nintrs() failed, status: 0x%x%, "
59446512Ssowmini 		    "nintrs: %d", ddi_status, nintrs));
59453859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
59463859Sml29623 	}
59473859Sml29623 
59483859Sml29623 	ddi_status = ddi_intr_get_navail(dip, int_type, &navail);
59493859Sml29623 	if ((ddi_status != DDI_SUCCESS) || (navail == 0)) {
59503859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
59516512Ssowmini 		    "ddi_intr_get_navail() failed, status: 0x%x%, "
59526512Ssowmini 		    "nintrs: %d", ddi_status, navail));
59533859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
59543859Sml29623 	}
59553859Sml29623 
59563859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL,
59576512Ssowmini 	    "ddi_intr_get_navail() returned: nintrs %d, navail %d",
59586512Ssowmini 	    nintrs, navail));
59593859Sml29623 
59605013Sml29623 	/* PSARC/2007/453 MSI-X interrupt limit override */
59615013Sml29623 	if (int_type == DDI_INTR_TYPE_MSIX) {
59625013Sml29623 		nrequest = nxge_create_msi_property(nxgep);
59635013Sml29623 		if (nrequest < navail) {
59645013Sml29623 			navail = nrequest;
59655013Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
59665013Sml29623 			    "nxge_add_intrs_adv_type: nintrs %d "
59675013Sml29623 			    "navail %d (nrequest %d)",
59685013Sml29623 			    nintrs, navail, nrequest));
59695013Sml29623 		}
59705013Sml29623 	}
59715013Sml29623 
59723859Sml29623 	if (int_type == DDI_INTR_TYPE_MSI && !ISP2(navail)) {
59733859Sml29623 		/* MSI must be power of 2 */
59743859Sml29623 		if ((navail & 16) == 16) {
59753859Sml29623 			navail = 16;
59763859Sml29623 		} else if ((navail & 8) == 8) {
59773859Sml29623 			navail = 8;
59783859Sml29623 		} else if ((navail & 4) == 4) {
59793859Sml29623 			navail = 4;
59803859Sml29623 		} else if ((navail & 2) == 2) {
59813859Sml29623 			navail = 2;
59823859Sml29623 		} else {
59833859Sml29623 			navail = 1;
59843859Sml29623 		}
59853859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
59866512Ssowmini 		    "ddi_intr_get_navail(): (msi power of 2) nintrs %d, "
59876512Ssowmini 		    "navail %d", nintrs, navail));
59883859Sml29623 	}
59893859Sml29623 
59903859Sml29623 	behavior = ((int_type == DDI_INTR_TYPE_FIXED) ? DDI_INTR_ALLOC_STRICT :
59916512Ssowmini 	    DDI_INTR_ALLOC_NORMAL);
59923859Sml29623 	intrp->intr_size = navail * sizeof (ddi_intr_handle_t);
59933859Sml29623 	intrp->htable = kmem_alloc(intrp->intr_size, KM_SLEEP);
59943859Sml29623 	ddi_status = ddi_intr_alloc(dip, intrp->htable, int_type, inum,
59956512Ssowmini 	    navail, &nactual, behavior);
59963859Sml29623 	if (ddi_status != DDI_SUCCESS || nactual == 0) {
59973859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
59986512Ssowmini 		    " ddi_intr_alloc() failed: %d",
59996512Ssowmini 		    ddi_status));
60003859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
60013859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
60023859Sml29623 	}
60033859Sml29623 
60043859Sml29623 	if ((ddi_status = ddi_intr_get_pri(intrp->htable[0],
60056512Ssowmini 	    (uint_t *)&intrp->pri)) != DDI_SUCCESS) {
60063859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
60076512Ssowmini 		    " ddi_intr_get_pri() failed: %d",
60086512Ssowmini 		    ddi_status));
60093859Sml29623 		/* Free already allocated interrupts */
60103859Sml29623 		for (y = 0; y < nactual; y++) {
60113859Sml29623 			(void) ddi_intr_free(intrp->htable[y]);
60123859Sml29623 		}
60133859Sml29623 
60143859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
60153859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
60163859Sml29623 	}
60173859Sml29623 
60183859Sml29623 	nrequired = 0;
60193859Sml29623 	switch (nxgep->niu_type) {
60203859Sml29623 	default:
60213859Sml29623 		status = nxge_ldgv_init(nxgep, &nactual, &nrequired);
60223859Sml29623 		break;
60233859Sml29623 
60243859Sml29623 	case N2_NIU:
60253859Sml29623 		status = nxge_ldgv_init_n2(nxgep, &nactual, &nrequired);
60263859Sml29623 		break;
60273859Sml29623 	}
60283859Sml29623 
60293859Sml29623 	if (status != NXGE_OK) {
60303859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
60316512Ssowmini 		    "nxge_add_intrs_adv_typ:nxge_ldgv_init "
60326512Ssowmini 		    "failed: 0x%x", status));
60333859Sml29623 		/* Free already allocated interrupts */
60343859Sml29623 		for (y = 0; y < nactual; y++) {
60353859Sml29623 			(void) ddi_intr_free(intrp->htable[y]);
60363859Sml29623 		}
60373859Sml29623 
60383859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
60393859Sml29623 		return (status);
60403859Sml29623 	}
60413859Sml29623 
60423859Sml29623 	ldgp = nxgep->ldgvp->ldgp;
60433859Sml29623 	for (x = 0; x < nrequired; x++, ldgp++) {
60443859Sml29623 		ldgp->vector = (uint8_t)x;
60453859Sml29623 		ldgp->intdata = SID_DATA(ldgp->func, x);
60463859Sml29623 		arg1 = ldgp->ldvp;
60473859Sml29623 		arg2 = nxgep;
60483859Sml29623 		if (ldgp->nldvs == 1) {
60493859Sml29623 			inthandler = (uint_t *)ldgp->ldvp->ldv_intr_handler;
60503859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
60516512Ssowmini 			    "nxge_add_intrs_adv_type: "
60526512Ssowmini 			    "arg1 0x%x arg2 0x%x: "
60536512Ssowmini 			    "1-1 int handler (entry %d intdata 0x%x)\n",
60546512Ssowmini 			    arg1, arg2,
60556512Ssowmini 			    x, ldgp->intdata));
60563859Sml29623 		} else if (ldgp->nldvs > 1) {
60573859Sml29623 			inthandler = (uint_t *)ldgp->sys_intr_handler;
60583859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
60596512Ssowmini 			    "nxge_add_intrs_adv_type: "
60606512Ssowmini 			    "arg1 0x%x arg2 0x%x: "
60616512Ssowmini 			    "nldevs %d int handler "
60626512Ssowmini 			    "(entry %d intdata 0x%x)\n",
60636512Ssowmini 			    arg1, arg2,
60646512Ssowmini 			    ldgp->nldvs, x, ldgp->intdata));
60653859Sml29623 		}
60663859Sml29623 
60673859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
60686512Ssowmini 		    "==> nxge_add_intrs_adv_type: ddi_add_intr(inum) #%d "
60696512Ssowmini 		    "htable 0x%llx", x, intrp->htable[x]));
60703859Sml29623 
60713859Sml29623 		if ((ddi_status = ddi_intr_add_handler(intrp->htable[x],
60726512Ssowmini 		    (ddi_intr_handler_t *)inthandler, arg1, arg2))
60736512Ssowmini 		    != DDI_SUCCESS) {
60743859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
60756512Ssowmini 			    "==> nxge_add_intrs_adv_type: failed #%d "
60766512Ssowmini 			    "status 0x%x", x, ddi_status));
60773859Sml29623 			for (y = 0; y < intrp->intr_added; y++) {
60783859Sml29623 				(void) ddi_intr_remove_handler(
60796512Ssowmini 				    intrp->htable[y]);
60803859Sml29623 			}
60813859Sml29623 			/* Free already allocated intr */
60823859Sml29623 			for (y = 0; y < nactual; y++) {
60833859Sml29623 				(void) ddi_intr_free(intrp->htable[y]);
60843859Sml29623 			}
60853859Sml29623 			kmem_free(intrp->htable, intrp->intr_size);
60863859Sml29623 
60873859Sml29623 			(void) nxge_ldgv_uninit(nxgep);
60883859Sml29623 
60893859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
60903859Sml29623 		}
60913859Sml29623 		intrp->intr_added++;
60923859Sml29623 	}
60933859Sml29623 
60943859Sml29623 	intrp->msi_intx_cnt = nactual;
60953859Sml29623 
60963859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
60976512Ssowmini 	    "Requested: %d, Allowed: %d msi_intx_cnt %d intr_added %d",
60986512Ssowmini 	    navail, nactual,
60996512Ssowmini 	    intrp->msi_intx_cnt,
61006512Ssowmini 	    intrp->intr_added));
61013859Sml29623 
61023859Sml29623 	(void) ddi_intr_get_cap(intrp->htable[0], &intrp->intr_cap);
61033859Sml29623 
61043859Sml29623 	(void) nxge_intr_ldgv_init(nxgep);
61053859Sml29623 
61063859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_add_intrs_adv_type"));
61073859Sml29623 
61083859Sml29623 	return (status);
61093859Sml29623 }
61103859Sml29623 
61113859Sml29623 /*ARGSUSED*/
61123859Sml29623 static nxge_status_t
61133859Sml29623 nxge_add_intrs_adv_type_fix(p_nxge_t nxgep, uint32_t int_type)
61143859Sml29623 {
61153859Sml29623 	dev_info_t		*dip = nxgep->dip;
61163859Sml29623 	p_nxge_ldg_t		ldgp;
61173859Sml29623 	p_nxge_intr_t		intrp;
61183859Sml29623 	uint_t			*inthandler;
61193859Sml29623 	void			*arg1, *arg2;
61203859Sml29623 	int			behavior;
61213859Sml29623 	int			nintrs, navail;
61223859Sml29623 	int			nactual, nrequired;
61233859Sml29623 	int			inum = 0;
61243859Sml29623 	int			x, y;
61253859Sml29623 	int			ddi_status = DDI_SUCCESS;
61263859Sml29623 	nxge_status_t		status = NXGE_OK;
61273859Sml29623 
61283859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs_adv_type_fix"));
61293859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
61303859Sml29623 	intrp->start_inum = 0;
61313859Sml29623 
61323859Sml29623 	ddi_status = ddi_intr_get_nintrs(dip, int_type, &nintrs);
61333859Sml29623 	if ((ddi_status != DDI_SUCCESS) || (nintrs == 0)) {
61343859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
61356512Ssowmini 		    "ddi_intr_get_nintrs() failed, status: 0x%x%, "
61366512Ssowmini 		    "nintrs: %d", status, nintrs));
61373859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
61383859Sml29623 	}
61393859Sml29623 
61403859Sml29623 	ddi_status = ddi_intr_get_navail(dip, int_type, &navail);
61413859Sml29623 	if ((ddi_status != DDI_SUCCESS) || (navail == 0)) {
61423859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
61436512Ssowmini 		    "ddi_intr_get_navail() failed, status: 0x%x%, "
61446512Ssowmini 		    "nintrs: %d", ddi_status, navail));
61453859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
61463859Sml29623 	}
61473859Sml29623 
61483859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL,
61496512Ssowmini 	    "ddi_intr_get_navail() returned: nintrs %d, naavail %d",
61506512Ssowmini 	    nintrs, navail));
61513859Sml29623 
61523859Sml29623 	behavior = ((int_type == DDI_INTR_TYPE_FIXED) ? DDI_INTR_ALLOC_STRICT :
61536512Ssowmini 	    DDI_INTR_ALLOC_NORMAL);
61543859Sml29623 	intrp->intr_size = navail * sizeof (ddi_intr_handle_t);
61553859Sml29623 	intrp->htable = kmem_alloc(intrp->intr_size, KM_SLEEP);
61563859Sml29623 	ddi_status = ddi_intr_alloc(dip, intrp->htable, int_type, inum,
61576512Ssowmini 	    navail, &nactual, behavior);
61583859Sml29623 	if (ddi_status != DDI_SUCCESS || nactual == 0) {
61593859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
61606512Ssowmini 		    " ddi_intr_alloc() failed: %d",
61616512Ssowmini 		    ddi_status));
61623859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
61633859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
61643859Sml29623 	}
61653859Sml29623 
61663859Sml29623 	if ((ddi_status = ddi_intr_get_pri(intrp->htable[0],
61676512Ssowmini 	    (uint_t *)&intrp->pri)) != DDI_SUCCESS) {
61683859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
61696512Ssowmini 		    " ddi_intr_get_pri() failed: %d",
61706512Ssowmini 		    ddi_status));
61713859Sml29623 		/* Free already allocated interrupts */
61723859Sml29623 		for (y = 0; y < nactual; y++) {
61733859Sml29623 			(void) ddi_intr_free(intrp->htable[y]);
61743859Sml29623 		}
61753859Sml29623 
61763859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
61773859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
61783859Sml29623 	}
61793859Sml29623 
61803859Sml29623 	nrequired = 0;
61813859Sml29623 	switch (nxgep->niu_type) {
61823859Sml29623 	default:
61833859Sml29623 		status = nxge_ldgv_init(nxgep, &nactual, &nrequired);
61843859Sml29623 		break;
61853859Sml29623 
61863859Sml29623 	case N2_NIU:
61873859Sml29623 		status = nxge_ldgv_init_n2(nxgep, &nactual, &nrequired);
61883859Sml29623 		break;
61893859Sml29623 	}
61903859Sml29623 
61913859Sml29623 	if (status != NXGE_OK) {
61923859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
61936512Ssowmini 		    "nxge_add_intrs_adv_type_fix:nxge_ldgv_init "
61946512Ssowmini 		    "failed: 0x%x", status));
61953859Sml29623 		/* Free already allocated interrupts */
61963859Sml29623 		for (y = 0; y < nactual; y++) {
61973859Sml29623 			(void) ddi_intr_free(intrp->htable[y]);
61983859Sml29623 		}
61993859Sml29623 
62003859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
62013859Sml29623 		return (status);
62023859Sml29623 	}
62033859Sml29623 
62043859Sml29623 	ldgp = nxgep->ldgvp->ldgp;
62053859Sml29623 	for (x = 0; x < nrequired; x++, ldgp++) {
62063859Sml29623 		ldgp->vector = (uint8_t)x;
62073859Sml29623 		if (nxgep->niu_type != N2_NIU) {
62083859Sml29623 			ldgp->intdata = SID_DATA(ldgp->func, x);
62093859Sml29623 		}
62103859Sml29623 
62113859Sml29623 		arg1 = ldgp->ldvp;
62123859Sml29623 		arg2 = nxgep;
62133859Sml29623 		if (ldgp->nldvs == 1) {
62143859Sml29623 			inthandler = (uint_t *)ldgp->ldvp->ldv_intr_handler;
62153859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
62166512Ssowmini 			    "nxge_add_intrs_adv_type_fix: "
62176512Ssowmini 			    "1-1 int handler(%d) ldg %d ldv %d "
62186512Ssowmini 			    "arg1 $%p arg2 $%p\n",
62196512Ssowmini 			    x, ldgp->ldg, ldgp->ldvp->ldv,
62206512Ssowmini 			    arg1, arg2));
62213859Sml29623 		} else if (ldgp->nldvs > 1) {
62223859Sml29623 			inthandler = (uint_t *)ldgp->sys_intr_handler;
62233859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
62246512Ssowmini 			    "nxge_add_intrs_adv_type_fix: "
62256512Ssowmini 			    "shared ldv %d int handler(%d) ldv %d ldg %d"
62266512Ssowmini 			    "arg1 0x%016llx arg2 0x%016llx\n",
62276512Ssowmini 			    x, ldgp->nldvs, ldgp->ldg, ldgp->ldvp->ldv,
62286512Ssowmini 			    arg1, arg2));
62293859Sml29623 		}
62303859Sml29623 
62313859Sml29623 		if ((ddi_status = ddi_intr_add_handler(intrp->htable[x],
62326512Ssowmini 		    (ddi_intr_handler_t *)inthandler, arg1, arg2))
62336512Ssowmini 		    != DDI_SUCCESS) {
62343859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
62356512Ssowmini 			    "==> nxge_add_intrs_adv_type_fix: failed #%d "
62366512Ssowmini 			    "status 0x%x", x, ddi_status));
62373859Sml29623 			for (y = 0; y < intrp->intr_added; y++) {
62383859Sml29623 				(void) ddi_intr_remove_handler(
62396512Ssowmini 				    intrp->htable[y]);
62403859Sml29623 			}
62413859Sml29623 			for (y = 0; y < nactual; y++) {
62423859Sml29623 				(void) ddi_intr_free(intrp->htable[y]);
62433859Sml29623 			}
62443859Sml29623 			/* Free already allocated intr */
62453859Sml29623 			kmem_free(intrp->htable, intrp->intr_size);
62463859Sml29623 
62473859Sml29623 			(void) nxge_ldgv_uninit(nxgep);
62483859Sml29623 
62493859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
62503859Sml29623 		}
62513859Sml29623 		intrp->intr_added++;
62523859Sml29623 	}
62533859Sml29623 
62543859Sml29623 	intrp->msi_intx_cnt = nactual;
62553859Sml29623 
62563859Sml29623 	(void) ddi_intr_get_cap(intrp->htable[0], &intrp->intr_cap);
62573859Sml29623 
62583859Sml29623 	status = nxge_intr_ldgv_init(nxgep);
62593859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_add_intrs_adv_type_fix"));
62603859Sml29623 
62613859Sml29623 	return (status);
62623859Sml29623 }
62633859Sml29623 
62643859Sml29623 static void
62653859Sml29623 nxge_remove_intrs(p_nxge_t nxgep)
62663859Sml29623 {
62673859Sml29623 	int		i, inum;
62683859Sml29623 	p_nxge_intr_t	intrp;
62693859Sml29623 
62703859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_remove_intrs"));
62713859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
62723859Sml29623 	if (!intrp->intr_registered) {
62733859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
62746512Ssowmini 		    "<== nxge_remove_intrs: interrupts not registered"));
62753859Sml29623 		return;
62763859Sml29623 	}
62773859Sml29623 
62783859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_remove_intrs:advanced"));
62793859Sml29623 
62803859Sml29623 	if (intrp->intr_cap & DDI_INTR_FLAG_BLOCK) {
62813859Sml29623 		(void) ddi_intr_block_disable(intrp->htable,
62826512Ssowmini 		    intrp->intr_added);
62833859Sml29623 	} else {
62843859Sml29623 		for (i = 0; i < intrp->intr_added; i++) {
62853859Sml29623 			(void) ddi_intr_disable(intrp->htable[i]);
62863859Sml29623 		}
62873859Sml29623 	}
62883859Sml29623 
62893859Sml29623 	for (inum = 0; inum < intrp->intr_added; inum++) {
62903859Sml29623 		if (intrp->htable[inum]) {
62913859Sml29623 			(void) ddi_intr_remove_handler(intrp->htable[inum]);
62923859Sml29623 		}
62933859Sml29623 	}
62943859Sml29623 
62953859Sml29623 	for (inum = 0; inum < intrp->msi_intx_cnt; inum++) {
62963859Sml29623 		if (intrp->htable[inum]) {
62973859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
62986512Ssowmini 			    "nxge_remove_intrs: ddi_intr_free inum %d "
62996512Ssowmini 			    "msi_intx_cnt %d intr_added %d",
63006512Ssowmini 			    inum,
63016512Ssowmini 			    intrp->msi_intx_cnt,
63026512Ssowmini 			    intrp->intr_added));
63033859Sml29623 
63043859Sml29623 			(void) ddi_intr_free(intrp->htable[inum]);
63053859Sml29623 		}
63063859Sml29623 	}
63073859Sml29623 
63083859Sml29623 	kmem_free(intrp->htable, intrp->intr_size);
63093859Sml29623 	intrp->intr_registered = B_FALSE;
63103859Sml29623 	intrp->intr_enabled = B_FALSE;
63113859Sml29623 	intrp->msi_intx_cnt = 0;
63123859Sml29623 	intrp->intr_added = 0;
63133859Sml29623 
63143859Sml29623 	(void) nxge_ldgv_uninit(nxgep);
63153859Sml29623 
63165013Sml29623 	(void) ddi_prop_remove(DDI_DEV_T_NONE, nxgep->dip,
63175013Sml29623 	    "#msix-request");
63185013Sml29623 
63193859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_remove_intrs"));
63203859Sml29623 }
63213859Sml29623 
63223859Sml29623 /*ARGSUSED*/
63233859Sml29623 static void
63243859Sml29623 nxge_intrs_enable(p_nxge_t nxgep)
63253859Sml29623 {
63263859Sml29623 	p_nxge_intr_t	intrp;
63273859Sml29623 	int		i;
63283859Sml29623 	int		status;
63293859Sml29623 
63303859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_intrs_enable"));
63313859Sml29623 
63323859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
63333859Sml29623 
63343859Sml29623 	if (!intrp->intr_registered) {
63353859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "<== nxge_intrs_enable: "
63366512Ssowmini 		    "interrupts are not registered"));
63373859Sml29623 		return;
63383859Sml29623 	}
63393859Sml29623 
63403859Sml29623 	if (intrp->intr_enabled) {
63413859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
63426512Ssowmini 		    "<== nxge_intrs_enable: already enabled"));
63433859Sml29623 		return;
63443859Sml29623 	}
63453859Sml29623 
63463859Sml29623 	if (intrp->intr_cap & DDI_INTR_FLAG_BLOCK) {
63473859Sml29623 		status = ddi_intr_block_enable(intrp->htable,
63486512Ssowmini 		    intrp->intr_added);
63493859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_intrs_enable "
63506512Ssowmini 		    "block enable - status 0x%x total inums #%d\n",
63516512Ssowmini 		    status, intrp->intr_added));
63523859Sml29623 	} else {
63533859Sml29623 		for (i = 0; i < intrp->intr_added; i++) {
63543859Sml29623 			status = ddi_intr_enable(intrp->htable[i]);
63553859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_intrs_enable "
63566512Ssowmini 			    "ddi_intr_enable:enable - status 0x%x "
63576512Ssowmini 			    "total inums %d enable inum #%d\n",
63586512Ssowmini 			    status, intrp->intr_added, i));
63593859Sml29623 			if (status == DDI_SUCCESS) {
63603859Sml29623 				intrp->intr_enabled = B_TRUE;
63613859Sml29623 			}
63623859Sml29623 		}
63633859Sml29623 	}
63643859Sml29623 
63653859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_intrs_enable"));
63663859Sml29623 }
63673859Sml29623 
63683859Sml29623 /*ARGSUSED*/
63693859Sml29623 static void
63703859Sml29623 nxge_intrs_disable(p_nxge_t nxgep)
63713859Sml29623 {
63723859Sml29623 	p_nxge_intr_t	intrp;
63733859Sml29623 	int		i;
63743859Sml29623 
63753859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_intrs_disable"));
63763859Sml29623 
63773859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
63783859Sml29623 
63793859Sml29623 	if (!intrp->intr_registered) {
63803859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_intrs_disable: "
63816512Ssowmini 		    "interrupts are not registered"));
63823859Sml29623 		return;
63833859Sml29623 	}
63843859Sml29623 
63853859Sml29623 	if (intrp->intr_cap & DDI_INTR_FLAG_BLOCK) {
63863859Sml29623 		(void) ddi_intr_block_disable(intrp->htable,
63876512Ssowmini 		    intrp->intr_added);
63883859Sml29623 	} else {
63893859Sml29623 		for (i = 0; i < intrp->intr_added; i++) {
63903859Sml29623 			(void) ddi_intr_disable(intrp->htable[i]);
63913859Sml29623 		}
63923859Sml29623 	}
63933859Sml29623 
63943859Sml29623 	intrp->intr_enabled = B_FALSE;
63953859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_intrs_disable"));
63963859Sml29623 }
63973859Sml29623 
639810309SSriharsha.Basavapatna@Sun.COM nxge_status_t
63993859Sml29623 nxge_mac_register(p_nxge_t nxgep)
64003859Sml29623 {
64013859Sml29623 	mac_register_t *macp;
64023859Sml29623 	int		status;
64033859Sml29623 
64043859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_mac_register"));
64053859Sml29623 
64063859Sml29623 	if ((macp = mac_alloc(MAC_VERSION)) == NULL)
64073859Sml29623 		return (NXGE_ERROR);
64083859Sml29623 
64093859Sml29623 	macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
64103859Sml29623 	macp->m_driver = nxgep;
64113859Sml29623 	macp->m_dip = nxgep->dip;
641210309SSriharsha.Basavapatna@Sun.COM 	if (!isLDOMguest(nxgep)) {
641310309SSriharsha.Basavapatna@Sun.COM 		macp->m_src_addr = nxgep->ouraddr.ether_addr_octet;
641410309SSriharsha.Basavapatna@Sun.COM 	} else {
641510309SSriharsha.Basavapatna@Sun.COM 		macp->m_src_addr = KMEM_ZALLOC(MAXMACADDRLEN, KM_SLEEP);
641610309SSriharsha.Basavapatna@Sun.COM 		macp->m_dst_addr = KMEM_ZALLOC(MAXMACADDRLEN, KM_SLEEP);
641710309SSriharsha.Basavapatna@Sun.COM 		(void) memset(macp->m_src_addr, 0xff, sizeof (MAXMACADDRLEN));
641810309SSriharsha.Basavapatna@Sun.COM 	}
64193859Sml29623 	macp->m_callbacks = &nxge_m_callbacks;
64203859Sml29623 	macp->m_min_sdu = 0;
64216439Sml29623 	nxgep->mac.default_mtu = nxgep->mac.maxframesize -
64226439Sml29623 	    NXGE_EHEADER_VLAN_CRC;
64236439Sml29623 	macp->m_max_sdu = nxgep->mac.default_mtu;
64245895Syz147064 	macp->m_margin = VLAN_TAGSZ;
64256512Ssowmini 	macp->m_priv_props = nxge_priv_props;
64266512Ssowmini 	macp->m_priv_prop_count = NXGE_MAX_PRIV_PROPS;
642710309SSriharsha.Basavapatna@Sun.COM 	if (isLDOMguest(nxgep)) {
642810309SSriharsha.Basavapatna@Sun.COM 		macp->m_v12n = MAC_VIRT_LEVEL1 | MAC_VIRT_SERIALIZE;
642910309SSriharsha.Basavapatna@Sun.COM 	} else {
643010309SSriharsha.Basavapatna@Sun.COM 		macp->m_v12n = MAC_VIRT_HIO | MAC_VIRT_LEVEL1 | \
643110309SSriharsha.Basavapatna@Sun.COM 		    MAC_VIRT_SERIALIZE;
643210309SSriharsha.Basavapatna@Sun.COM 	}
64333859Sml29623 
64346439Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL,
64356439Sml29623 	    "==> nxge_mac_register: instance %d "
64366439Sml29623 	    "max_sdu %d margin %d maxframe %d (header %d)",
64376439Sml29623 	    nxgep->instance,
64386439Sml29623 	    macp->m_max_sdu, macp->m_margin,
64396439Sml29623 	    nxgep->mac.maxframesize,
64406439Sml29623 	    NXGE_EHEADER_VLAN_CRC));
64416439Sml29623 
64423859Sml29623 	status = mac_register(macp, &nxgep->mach);
644310309SSriharsha.Basavapatna@Sun.COM 	if (isLDOMguest(nxgep)) {
644410309SSriharsha.Basavapatna@Sun.COM 		KMEM_FREE(macp->m_src_addr, MAXMACADDRLEN);
644510309SSriharsha.Basavapatna@Sun.COM 		KMEM_FREE(macp->m_dst_addr, MAXMACADDRLEN);
644610309SSriharsha.Basavapatna@Sun.COM 	}
64473859Sml29623 	mac_free(macp);
64483859Sml29623 
64493859Sml29623 	if (status != 0) {
64503859Sml29623 		cmn_err(CE_WARN,
64516512Ssowmini 		    "!nxge_mac_register failed (status %d instance %d)",
64526512Ssowmini 		    status, nxgep->instance);
64533859Sml29623 		return (NXGE_ERROR);
64543859Sml29623 	}
64553859Sml29623 
64563859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_mac_register success "
64576512Ssowmini 	    "(instance %d)", nxgep->instance));
64583859Sml29623 
64593859Sml29623 	return (NXGE_OK);
64603859Sml29623 }
64613859Sml29623 
64623859Sml29623 void
64633859Sml29623 nxge_err_inject(p_nxge_t nxgep, queue_t *wq, mblk_t *mp)
64643859Sml29623 {
64653859Sml29623 	ssize_t		size;
64663859Sml29623 	mblk_t		*nmp;
64673859Sml29623 	uint8_t		blk_id;
64683859Sml29623 	uint8_t		chan;
64693859Sml29623 	uint32_t	err_id;
64703859Sml29623 	err_inject_t	*eip;
64713859Sml29623 
64723859Sml29623 	NXGE_DEBUG_MSG((nxgep, STR_CTL, "==> nxge_err_inject"));
64733859Sml29623 
64743859Sml29623 	size = 1024;
64753859Sml29623 	nmp = mp->b_cont;
64763859Sml29623 	eip = (err_inject_t *)nmp->b_rptr;
64773859Sml29623 	blk_id = eip->blk_id;
64783859Sml29623 	err_id = eip->err_id;
64793859Sml29623 	chan = eip->chan;
64803859Sml29623 	cmn_err(CE_NOTE, "!blk_id = 0x%x\n", blk_id);
64813859Sml29623 	cmn_err(CE_NOTE, "!err_id = 0x%x\n", err_id);
64823859Sml29623 	cmn_err(CE_NOTE, "!chan = 0x%x\n", chan);
64833859Sml29623 	switch (blk_id) {
64843859Sml29623 	case MAC_BLK_ID:
64853859Sml29623 		break;
64863859Sml29623 	case TXMAC_BLK_ID:
64873859Sml29623 		break;
64883859Sml29623 	case RXMAC_BLK_ID:
64893859Sml29623 		break;
64903859Sml29623 	case MIF_BLK_ID:
64913859Sml29623 		break;
64923859Sml29623 	case IPP_BLK_ID:
64933859Sml29623 		nxge_ipp_inject_err(nxgep, err_id);
64943859Sml29623 		break;
64953859Sml29623 	case TXC_BLK_ID:
64963859Sml29623 		nxge_txc_inject_err(nxgep, err_id);
64973859Sml29623 		break;
64983859Sml29623 	case TXDMA_BLK_ID:
64993859Sml29623 		nxge_txdma_inject_err(nxgep, err_id, chan);
65003859Sml29623 		break;
65013859Sml29623 	case RXDMA_BLK_ID:
65023859Sml29623 		nxge_rxdma_inject_err(nxgep, err_id, chan);
65033859Sml29623 		break;
65043859Sml29623 	case ZCP_BLK_ID:
65053859Sml29623 		nxge_zcp_inject_err(nxgep, err_id);
65063859Sml29623 		break;
65073859Sml29623 	case ESPC_BLK_ID:
65083859Sml29623 		break;
65093859Sml29623 	case FFLP_BLK_ID:
65103859Sml29623 		break;
65113859Sml29623 	case PHY_BLK_ID:
65123859Sml29623 		break;
65133859Sml29623 	case ETHER_SERDES_BLK_ID:
65143859Sml29623 		break;
65153859Sml29623 	case PCIE_SERDES_BLK_ID:
65163859Sml29623 		break;
65173859Sml29623 	case VIR_BLK_ID:
65183859Sml29623 		break;
65193859Sml29623 	}
65203859Sml29623 
65213859Sml29623 	nmp->b_wptr = nmp->b_rptr + size;
65223859Sml29623 	NXGE_DEBUG_MSG((nxgep, STR_CTL, "<== nxge_err_inject"));
65233859Sml29623 
65243859Sml29623 	miocack(wq, mp, (int)size, 0);
65253859Sml29623 }
65263859Sml29623 
65273859Sml29623 static int
65283859Sml29623 nxge_init_common_dev(p_nxge_t nxgep)
65293859Sml29623 {
65303859Sml29623 	p_nxge_hw_list_t	hw_p;
65313859Sml29623 	dev_info_t 		*p_dip;
65323859Sml29623 
65333859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "==> nxge_init_common_device"));
65343859Sml29623 
65353859Sml29623 	p_dip = nxgep->p_dip;
65363859Sml29623 	MUTEX_ENTER(&nxge_common_lock);
65373859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL,
65386512Ssowmini 	    "==> nxge_init_common_dev:func # %d",
65396512Ssowmini 	    nxgep->function_num));
65403859Sml29623 	/*
65413859Sml29623 	 * Loop through existing per neptune hardware list.
65423859Sml29623 	 */
65433859Sml29623 	for (hw_p = nxge_hw_list; hw_p; hw_p = hw_p->next) {
65443859Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
65456512Ssowmini 		    "==> nxge_init_common_device:func # %d "
65466512Ssowmini 		    "hw_p $%p parent dip $%p",
65476512Ssowmini 		    nxgep->function_num,
65486512Ssowmini 		    hw_p,
65496512Ssowmini 		    p_dip));
65503859Sml29623 		if (hw_p->parent_devp == p_dip) {
65513859Sml29623 			nxgep->nxge_hw_p = hw_p;
65523859Sml29623 			hw_p->ndevs++;
65533859Sml29623 			hw_p->nxge_p[nxgep->function_num] = nxgep;
65543859Sml29623 			NXGE_DEBUG_MSG((nxgep, MOD_CTL,
65556512Ssowmini 			    "==> nxge_init_common_device:func # %d "
65566512Ssowmini 			    "hw_p $%p parent dip $%p "
65576512Ssowmini 			    "ndevs %d (found)",
65586512Ssowmini 			    nxgep->function_num,
65596512Ssowmini 			    hw_p,
65606512Ssowmini 			    p_dip,
65616512Ssowmini 			    hw_p->ndevs));
65623859Sml29623 			break;
65633859Sml29623 		}
65643859Sml29623 	}
65653859Sml29623 
65663859Sml29623 	if (hw_p == NULL) {
65677801SSantwona.Behera@Sun.COM 
65687801SSantwona.Behera@Sun.COM 		char **prop_val;
65697801SSantwona.Behera@Sun.COM 		uint_t prop_len;
65707801SSantwona.Behera@Sun.COM 		int i;
65717801SSantwona.Behera@Sun.COM 
65723859Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
65736512Ssowmini 		    "==> nxge_init_common_device:func # %d "
65746512Ssowmini 		    "parent dip $%p (new)",
65756512Ssowmini 		    nxgep->function_num,
65766512Ssowmini 		    p_dip));
65773859Sml29623 		hw_p = kmem_zalloc(sizeof (nxge_hw_list_t), KM_SLEEP);
65783859Sml29623 		hw_p->parent_devp = p_dip;
65793859Sml29623 		hw_p->magic = NXGE_NEPTUNE_MAGIC;
65803859Sml29623 		nxgep->nxge_hw_p = hw_p;
65813859Sml29623 		hw_p->ndevs++;
65823859Sml29623 		hw_p->nxge_p[nxgep->function_num] = nxgep;
65833859Sml29623 		hw_p->next = nxge_hw_list;
65844732Sdavemq 		if (nxgep->niu_type == N2_NIU) {
65854732Sdavemq 			hw_p->niu_type = N2_NIU;
65864732Sdavemq 			hw_p->platform_type = P_NEPTUNE_NIU;
65874732Sdavemq 		} else {
65884732Sdavemq 			hw_p->niu_type = NIU_TYPE_NONE;
65894977Sraghus 			hw_p->platform_type = P_NEPTUNE_NONE;
65904732Sdavemq 		}
65913859Sml29623 
65923859Sml29623 		MUTEX_INIT(&hw_p->nxge_cfg_lock, NULL, MUTEX_DRIVER, NULL);
65933859Sml29623 		MUTEX_INIT(&hw_p->nxge_tcam_lock, NULL, MUTEX_DRIVER, NULL);
65943859Sml29623 		MUTEX_INIT(&hw_p->nxge_vlan_lock, NULL, MUTEX_DRIVER, NULL);
65953859Sml29623 		MUTEX_INIT(&hw_p->nxge_mdio_lock, NULL, MUTEX_DRIVER, NULL);
65963859Sml29623 
65973859Sml29623 		nxge_hw_list = hw_p;
65984732Sdavemq 
65997801SSantwona.Behera@Sun.COM 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, nxgep->dip, 0,
66007801SSantwona.Behera@Sun.COM 		    "compatible", &prop_val, &prop_len) == DDI_PROP_SUCCESS) {
66017801SSantwona.Behera@Sun.COM 			for (i = 0; i < prop_len; i++) {
66027801SSantwona.Behera@Sun.COM 				if ((strcmp((caddr_t)prop_val[i],
66037801SSantwona.Behera@Sun.COM 				    NXGE_ROCK_COMPATIBLE) == 0)) {
66047801SSantwona.Behera@Sun.COM 					hw_p->platform_type = P_NEPTUNE_ROCK;
66057801SSantwona.Behera@Sun.COM 					NXGE_DEBUG_MSG((nxgep, MOD_CTL,
66067801SSantwona.Behera@Sun.COM 					    "ROCK hw_p->platform_type %d",
66077801SSantwona.Behera@Sun.COM 					    hw_p->platform_type));
66087801SSantwona.Behera@Sun.COM 					break;
66097801SSantwona.Behera@Sun.COM 				}
66107801SSantwona.Behera@Sun.COM 				NXGE_DEBUG_MSG((nxgep, MOD_CTL,
66117801SSantwona.Behera@Sun.COM 				    "nxge_init_common_dev: read compatible"
66127801SSantwona.Behera@Sun.COM 				    " property[%d] val[%s]",
66137801SSantwona.Behera@Sun.COM 				    i, (caddr_t)prop_val[i]));
66147801SSantwona.Behera@Sun.COM 			}
66157801SSantwona.Behera@Sun.COM 		}
66167801SSantwona.Behera@Sun.COM 
66177801SSantwona.Behera@Sun.COM 		ddi_prop_free(prop_val);
66187801SSantwona.Behera@Sun.COM 
66194732Sdavemq 		(void) nxge_scan_ports_phy(nxgep, nxge_hw_list);
66203859Sml29623 	}
66213859Sml29623 
66223859Sml29623 	MUTEX_EXIT(&nxge_common_lock);
66234732Sdavemq 
66244977Sraghus 	nxgep->platform_type = hw_p->platform_type;
66257801SSantwona.Behera@Sun.COM 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "nxgep->platform_type %d",
66267801SSantwona.Behera@Sun.COM 	    nxgep->platform_type));
66274732Sdavemq 	if (nxgep->niu_type != N2_NIU) {
66284732Sdavemq 		nxgep->niu_type = hw_p->niu_type;
66294732Sdavemq 	}
66304732Sdavemq 
66313859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL,
66326512Ssowmini 	    "==> nxge_init_common_device (nxge_hw_list) $%p",
66336512Ssowmini 	    nxge_hw_list));
66343859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "<== nxge_init_common_device"));
66353859Sml29623 
66363859Sml29623 	return (NXGE_OK);
66373859Sml29623 }
66383859Sml29623 
66393859Sml29623 static void
66403859Sml29623 nxge_uninit_common_dev(p_nxge_t nxgep)
66413859Sml29623 {
66423859Sml29623 	p_nxge_hw_list_t	hw_p, h_hw_p;
66436801Sspeer 	p_nxge_dma_pt_cfg_t	p_dma_cfgp;
66446801Sspeer 	p_nxge_hw_pt_cfg_t	p_cfgp;
66453859Sml29623 	dev_info_t 		*p_dip;
66463859Sml29623 
66473859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "==> nxge_uninit_common_device"));
66483859Sml29623 	if (nxgep->nxge_hw_p == NULL) {
66493859Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
66506512Ssowmini 		    "<== nxge_uninit_common_device (no common)"));
66513859Sml29623 		return;
66523859Sml29623 	}
66533859Sml29623 
66543859Sml29623 	MUTEX_ENTER(&nxge_common_lock);
66553859Sml29623 	h_hw_p = nxge_hw_list;
66563859Sml29623 	for (hw_p = nxge_hw_list; hw_p; hw_p = hw_p->next) {
66573859Sml29623 		p_dip = hw_p->parent_devp;
66583859Sml29623 		if (nxgep->nxge_hw_p == hw_p &&
66596512Ssowmini 		    p_dip == nxgep->p_dip &&
66606512Ssowmini 		    nxgep->nxge_hw_p->magic == NXGE_NEPTUNE_MAGIC &&
66616512Ssowmini 		    hw_p->magic == NXGE_NEPTUNE_MAGIC) {
66623859Sml29623 
66633859Sml29623 			NXGE_DEBUG_MSG((nxgep, MOD_CTL,
66646512Ssowmini 			    "==> nxge_uninit_common_device:func # %d "
66656512Ssowmini 			    "hw_p $%p parent dip $%p "
66666512Ssowmini 			    "ndevs %d (found)",
66676512Ssowmini 			    nxgep->function_num,
66686512Ssowmini 			    hw_p,
66696512Ssowmini 			    p_dip,
66706512Ssowmini 			    hw_p->ndevs));
66713859Sml29623 
66726801Sspeer 			/*
66736801Sspeer 			 * Release the RDC table, a shared resoruce
66746801Sspeer 			 * of the nxge hardware.  The RDC table was
66756801Sspeer 			 * assigned to this instance of nxge in
66766801Sspeer 			 * nxge_use_cfg_dma_config().
66776801Sspeer 			 */
66787587SMichael.Speer@Sun.COM 			if (!isLDOMguest(nxgep)) {
66797587SMichael.Speer@Sun.COM 				p_dma_cfgp =
66807587SMichael.Speer@Sun.COM 				    (p_nxge_dma_pt_cfg_t)&nxgep->pt_config;
66817587SMichael.Speer@Sun.COM 				p_cfgp =
66827587SMichael.Speer@Sun.COM 				    (p_nxge_hw_pt_cfg_t)&p_dma_cfgp->hw_config;
66837587SMichael.Speer@Sun.COM 				(void) nxge_fzc_rdc_tbl_unbind(nxgep,
66847587SMichael.Speer@Sun.COM 				    p_cfgp->def_mac_rxdma_grpid);
66857766SMichael.Speer@Sun.COM 
66867766SMichael.Speer@Sun.COM 				/* Cleanup any outstanding groups.  */
66877766SMichael.Speer@Sun.COM 				nxge_grp_cleanup(nxgep);
66887587SMichael.Speer@Sun.COM 			}
66896801Sspeer 
66903859Sml29623 			if (hw_p->ndevs) {
66913859Sml29623 				hw_p->ndevs--;
66923859Sml29623 			}
66933859Sml29623 			hw_p->nxge_p[nxgep->function_num] = NULL;
66943859Sml29623 			if (!hw_p->ndevs) {
66953859Sml29623 				MUTEX_DESTROY(&hw_p->nxge_vlan_lock);
66963859Sml29623 				MUTEX_DESTROY(&hw_p->nxge_tcam_lock);
66973859Sml29623 				MUTEX_DESTROY(&hw_p->nxge_cfg_lock);
66983859Sml29623 				MUTEX_DESTROY(&hw_p->nxge_mdio_lock);
66993859Sml29623 				NXGE_DEBUG_MSG((nxgep, MOD_CTL,
67006512Ssowmini 				    "==> nxge_uninit_common_device: "
67016512Ssowmini 				    "func # %d "
67026512Ssowmini 				    "hw_p $%p parent dip $%p "
67036512Ssowmini 				    "ndevs %d (last)",
67046512Ssowmini 				    nxgep->function_num,
67056512Ssowmini 				    hw_p,
67066512Ssowmini 				    p_dip,
67076512Ssowmini 				    hw_p->ndevs));
67083859Sml29623 
67096495Sspeer 				nxge_hio_uninit(nxgep);
67106495Sspeer 
67113859Sml29623 				if (hw_p == nxge_hw_list) {
67123859Sml29623 					NXGE_DEBUG_MSG((nxgep, MOD_CTL,
67136512Ssowmini 					    "==> nxge_uninit_common_device:"
67146512Ssowmini 					    "remove head func # %d "
67156512Ssowmini 					    "hw_p $%p parent dip $%p "
67166512Ssowmini 					    "ndevs %d (head)",
67176512Ssowmini 					    nxgep->function_num,
67186512Ssowmini 					    hw_p,
67196512Ssowmini 					    p_dip,
67206512Ssowmini 					    hw_p->ndevs));
67213859Sml29623 					nxge_hw_list = hw_p->next;
67223859Sml29623 				} else {
67233859Sml29623 					NXGE_DEBUG_MSG((nxgep, MOD_CTL,
67246512Ssowmini 					    "==> nxge_uninit_common_device:"
67256512Ssowmini 					    "remove middle func # %d "
67266512Ssowmini 					    "hw_p $%p parent dip $%p "
67276512Ssowmini 					    "ndevs %d (middle)",
67286512Ssowmini 					    nxgep->function_num,
67296512Ssowmini 					    hw_p,
67306512Ssowmini 					    p_dip,
67316512Ssowmini 					    hw_p->ndevs));
67323859Sml29623 					h_hw_p->next = hw_p->next;
67333859Sml29623 				}
67343859Sml29623 
67356495Sspeer 				nxgep->nxge_hw_p = NULL;
67363859Sml29623 				KMEM_FREE(hw_p, sizeof (nxge_hw_list_t));
67373859Sml29623 			}
67383859Sml29623 			break;
67393859Sml29623 		} else {
67403859Sml29623 			h_hw_p = hw_p;
67413859Sml29623 		}
67423859Sml29623 	}
67433859Sml29623 
67443859Sml29623 	MUTEX_EXIT(&nxge_common_lock);
67453859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL,
67466512Ssowmini 	    "==> nxge_uninit_common_device (nxge_hw_list) $%p",
67476512Ssowmini 	    nxge_hw_list));
67483859Sml29623 
67493859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "<= nxge_uninit_common_device"));
67503859Sml29623 }
67514732Sdavemq 
67524732Sdavemq /*
67534977Sraghus  * Determines the number of ports from the niu_type or the platform type.
67544732Sdavemq  * Returns the number of ports, or returns zero on failure.
67554732Sdavemq  */
67564732Sdavemq 
67574732Sdavemq int
67584977Sraghus nxge_get_nports(p_nxge_t nxgep)
67594732Sdavemq {
67604732Sdavemq 	int	nports = 0;
67614732Sdavemq 
67624977Sraghus 	switch (nxgep->niu_type) {
67634732Sdavemq 	case N2_NIU:
67644732Sdavemq 	case NEPTUNE_2_10GF:
67654732Sdavemq 		nports = 2;
67664732Sdavemq 		break;
67674732Sdavemq 	case NEPTUNE_4_1GC:
67684732Sdavemq 	case NEPTUNE_2_10GF_2_1GC:
67694732Sdavemq 	case NEPTUNE_1_10GF_3_1GC:
67704732Sdavemq 	case NEPTUNE_1_1GC_1_10GF_2_1GC:
67716261Sjoycey 	case NEPTUNE_2_10GF_2_1GRF:
67724732Sdavemq 		nports = 4;
67734732Sdavemq 		break;
67744732Sdavemq 	default:
67754977Sraghus 		switch (nxgep->platform_type) {
67764977Sraghus 		case P_NEPTUNE_NIU:
67774977Sraghus 		case P_NEPTUNE_ATLAS_2PORT:
67784977Sraghus 			nports = 2;
67794977Sraghus 			break;
67804977Sraghus 		case P_NEPTUNE_ATLAS_4PORT:
67814977Sraghus 		case P_NEPTUNE_MARAMBA_P0:
67824977Sraghus 		case P_NEPTUNE_MARAMBA_P1:
67837801SSantwona.Behera@Sun.COM 		case P_NEPTUNE_ROCK:
67845196Ssbehera 		case P_NEPTUNE_ALONSO:
67854977Sraghus 			nports = 4;
67864977Sraghus 			break;
67874977Sraghus 		default:
67884977Sraghus 			break;
67894977Sraghus 		}
67904732Sdavemq 		break;
67914732Sdavemq 	}
67924732Sdavemq 
67934732Sdavemq 	return (nports);
67944732Sdavemq }
67955013Sml29623 
67965013Sml29623 /*
67975013Sml29623  * The following two functions are to support
67985013Sml29623  * PSARC/2007/453 MSI-X interrupt limit override.
67995013Sml29623  */
68005013Sml29623 static int
68015013Sml29623 nxge_create_msi_property(p_nxge_t nxgep)
68025013Sml29623 {
68035013Sml29623 	int	nmsi;
68045013Sml29623 	extern	int ncpus;
68055013Sml29623 
68065013Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "==>nxge_create_msi_property"));
68075013Sml29623 
68085013Sml29623 	switch (nxgep->mac.portmode) {
68095013Sml29623 	case PORT_10G_COPPER:
68105013Sml29623 	case PORT_10G_FIBER:
68116835Syc148097 	case PORT_10G_TN1010:
68125013Sml29623 		(void) ddi_prop_create(DDI_DEV_T_NONE, nxgep->dip,
68135013Sml29623 		    DDI_PROP_CANSLEEP, "#msix-request", NULL, 0);
68145013Sml29623 		/*
68155013Sml29623 		 * The maximum MSI-X requested will be 8.
68168455Stc99174@train 		 * If the # of CPUs is less than 8, we will request
68178455Stc99174@train 		 * # MSI-X based on the # of CPUs (default).
68185013Sml29623 		 */
68198455Stc99174@train 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
68208455Stc99174@train 		    "==>nxge_create_msi_property (10G): nxge_msix_10g_intrs %d",
68218455Stc99174@train 		    nxge_msix_10g_intrs));
68228455Stc99174@train 		if ((nxge_msix_10g_intrs == 0) ||
68238455Stc99174@train 		    (nxge_msix_10g_intrs > NXGE_MSIX_MAX_ALLOWED)) {
68245013Sml29623 			nmsi = NXGE_MSIX_REQUEST_10G;
68258455Stc99174@train 			NXGE_DEBUG_MSG((nxgep, MOD_CTL,
68268455Stc99174@train 			    "==>nxge_create_msi_property (10G): reset to 8"));
68275013Sml29623 		} else {
68288455Stc99174@train 			nmsi = nxge_msix_10g_intrs;
68298455Stc99174@train 		}
68308455Stc99174@train 
68318455Stc99174@train 		/*
68328455Stc99174@train 		 * If # of interrupts requested is 8 (default),
68338455Stc99174@train 		 * the checking of the number of cpus will be
68348455Stc99174@train 		 * be maintained.
68358455Stc99174@train 		 */
68368455Stc99174@train 		if ((nmsi == NXGE_MSIX_REQUEST_10G) &&
68378455Stc99174@train 		    (ncpus < nmsi)) {
68388455Stc99174@train 			NXGE_DEBUG_MSG((nxgep, MOD_CTL,
68398455Stc99174@train 			    "==>nxge_create_msi_property (10G): reset to 8"));
68405013Sml29623 			nmsi = ncpus;
68415013Sml29623 		}
68425013Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
68435013Sml29623 		    "==>nxge_create_msi_property(10G): exists 0x%x (nmsi %d)",
68445013Sml29623 		    ddi_prop_exists(DDI_DEV_T_NONE, nxgep->dip,
68455013Sml29623 		    DDI_PROP_CANSLEEP, "#msix-request"), nmsi));
68465013Sml29623 		break;
68475013Sml29623 
68485013Sml29623 	default:
68498455Stc99174@train 		(void) ddi_prop_create(DDI_DEV_T_NONE, nxgep->dip,
68508455Stc99174@train 		    DDI_PROP_CANSLEEP, "#msix-request", NULL, 0);
68518455Stc99174@train 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
68528455Stc99174@train 		    "==>nxge_create_msi_property (1G): nxge_msix_1g_intrs %d",
68538455Stc99174@train 		    nxge_msix_1g_intrs));
68548455Stc99174@train 		if ((nxge_msix_1g_intrs == 0) ||
68558455Stc99174@train 		    (nxge_msix_1g_intrs > NXGE_MSIX_MAX_ALLOWED)) {
68568455Stc99174@train 			nmsi = NXGE_MSIX_REQUEST_1G;
68578455Stc99174@train 			NXGE_DEBUG_MSG((nxgep, MOD_CTL,
68588455Stc99174@train 			    "==>nxge_create_msi_property (1G): reset to 2"));
68598455Stc99174@train 		} else {
68608455Stc99174@train 			nmsi = nxge_msix_1g_intrs;
68618455Stc99174@train 		}
68625013Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
68635013Sml29623 		    "==>nxge_create_msi_property(1G): exists 0x%x (nmsi %d)",
68645013Sml29623 		    ddi_prop_exists(DDI_DEV_T_NONE, nxgep->dip,
68655013Sml29623 		    DDI_PROP_CANSLEEP, "#msix-request"), nmsi));
68665013Sml29623 		break;
68675013Sml29623 	}
68685013Sml29623 
68695013Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "<==nxge_create_msi_property"));
68705013Sml29623 	return (nmsi);
68715013Sml29623 }
68726512Ssowmini 
68736512Ssowmini /* ARGSUSED */
68746512Ssowmini static int
68756512Ssowmini nxge_get_def_val(nxge_t *nxgep, mac_prop_id_t pr_num, uint_t pr_valsize,
68766512Ssowmini     void *pr_val)
68776512Ssowmini {
68786512Ssowmini 	int err = 0;
68796512Ssowmini 	link_flowctrl_t fl;
68806512Ssowmini 
68816512Ssowmini 	switch (pr_num) {
68826789Sam223141 	case MAC_PROP_AUTONEG:
68836512Ssowmini 		*(uint8_t *)pr_val = 1;
68846512Ssowmini 		break;
68856789Sam223141 	case MAC_PROP_FLOWCTRL:
68866512Ssowmini 		if (pr_valsize < sizeof (link_flowctrl_t))
68876512Ssowmini 			return (EINVAL);
68886512Ssowmini 		fl = LINK_FLOWCTRL_RX;
68896512Ssowmini 		bcopy(&fl, pr_val, sizeof (fl));
68906512Ssowmini 		break;
68916789Sam223141 	case MAC_PROP_ADV_1000FDX_CAP:
68926789Sam223141 	case MAC_PROP_EN_1000FDX_CAP:
68936512Ssowmini 		*(uint8_t *)pr_val = 1;
68946512Ssowmini 		break;
68956789Sam223141 	case MAC_PROP_ADV_100FDX_CAP:
68966789Sam223141 	case MAC_PROP_EN_100FDX_CAP:
68976512Ssowmini 		*(uint8_t *)pr_val = 1;
68986512Ssowmini 		break;
68996512Ssowmini 	default:
69006512Ssowmini 		err = ENOTSUP;
69016512Ssowmini 		break;
69026512Ssowmini 	}
69036512Ssowmini 	return (err);
69046512Ssowmini }
69056705Sml29623 
69066705Sml29623 
69076705Sml29623 /*
69086705Sml29623  * The following is a software around for the Neptune hardware's
69096705Sml29623  * interrupt bugs; The Neptune hardware may generate spurious interrupts when
69106705Sml29623  * an interrupr handler is removed.
69116705Sml29623  */
69126705Sml29623 #define	NXGE_PCI_PORT_LOGIC_OFFSET	0x98
69136705Sml29623 #define	NXGE_PIM_RESET			(1ULL << 29)
69146705Sml29623 #define	NXGE_GLU_RESET			(1ULL << 30)
69156705Sml29623 #define	NXGE_NIU_RESET			(1ULL << 31)
69166705Sml29623 #define	NXGE_PCI_RESET_ALL		(NXGE_PIM_RESET |	\
69176705Sml29623 					NXGE_GLU_RESET |	\
69186705Sml29623 					NXGE_NIU_RESET)
69196705Sml29623 
69206705Sml29623 #define	NXGE_WAIT_QUITE_TIME		200000
69216705Sml29623 #define	NXGE_WAIT_QUITE_RETRY		40
69226705Sml29623 #define	NXGE_PCI_RESET_WAIT		1000000 /* one second */
69236705Sml29623 
69246705Sml29623 static void
69256705Sml29623 nxge_niu_peu_reset(p_nxge_t nxgep)
69266705Sml29623 {
69276705Sml29623 	uint32_t	rvalue;
69286705Sml29623 	p_nxge_hw_list_t hw_p;
69296705Sml29623 	p_nxge_t	fnxgep;
69306705Sml29623 	int		i, j;
69316705Sml29623 
69326705Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL, "==> nxge_niu_peu_reset"));
69336705Sml29623 	if ((hw_p = nxgep->nxge_hw_p) == NULL) {
69346705Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
69356705Sml29623 		    "==> nxge_niu_peu_reset: NULL hardware pointer"));
69366705Sml29623 		return;
69376705Sml29623 	}
69386705Sml29623 
69396705Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
69406705Sml29623 	    "==> nxge_niu_peu_reset: flags 0x%x link timer id %d timer id %d",
69416705Sml29623 	    hw_p->flags, nxgep->nxge_link_poll_timerid,
69426705Sml29623 	    nxgep->nxge_timerid));
69436705Sml29623 
69446705Sml29623 	MUTEX_ENTER(&hw_p->nxge_cfg_lock);
69456705Sml29623 	/*
69466705Sml29623 	 * Make sure other instances from the same hardware
69476705Sml29623 	 * stop sending PIO and in quiescent state.
69486705Sml29623 	 */
69496705Sml29623 	for (i = 0; i < NXGE_MAX_PORTS; i++) {
69506705Sml29623 		fnxgep = hw_p->nxge_p[i];
69516705Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
69526705Sml29623 		    "==> nxge_niu_peu_reset: checking entry %d "
69536705Sml29623 		    "nxgep $%p", i, fnxgep));
69546705Sml29623 #ifdef	NXGE_DEBUG
69556705Sml29623 		if (fnxgep) {
69566705Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
69576705Sml29623 			    "==> nxge_niu_peu_reset: entry %d (function %d) "
69586705Sml29623 			    "link timer id %d hw timer id %d",
69596705Sml29623 			    i, fnxgep->function_num,
69606705Sml29623 			    fnxgep->nxge_link_poll_timerid,
69616705Sml29623 			    fnxgep->nxge_timerid));
69626705Sml29623 		}
69636705Sml29623 #endif
69646705Sml29623 		if (fnxgep && fnxgep != nxgep &&
69656705Sml29623 		    (fnxgep->nxge_timerid || fnxgep->nxge_link_poll_timerid)) {
69666705Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
69676705Sml29623 			    "==> nxge_niu_peu_reset: checking $%p "
69686705Sml29623 			    "(function %d) timer ids",
69696705Sml29623 			    fnxgep, fnxgep->function_num));
69706705Sml29623 			for (j = 0; j < NXGE_WAIT_QUITE_RETRY; j++) {
69716705Sml29623 				NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
69726705Sml29623 				    "==> nxge_niu_peu_reset: waiting"));
69736705Sml29623 				NXGE_DELAY(NXGE_WAIT_QUITE_TIME);
69746705Sml29623 				if (!fnxgep->nxge_timerid &&
69756705Sml29623 				    !fnxgep->nxge_link_poll_timerid) {
69766705Sml29623 					break;
69776705Sml29623 				}
69786705Sml29623 			}
69796705Sml29623 			NXGE_DELAY(NXGE_WAIT_QUITE_TIME);
69806705Sml29623 			if (fnxgep->nxge_timerid ||
69816705Sml29623 			    fnxgep->nxge_link_poll_timerid) {
69826705Sml29623 				MUTEX_EXIT(&hw_p->nxge_cfg_lock);
69836705Sml29623 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
69846705Sml29623 				    "<== nxge_niu_peu_reset: cannot reset "
69856705Sml29623 				    "hardware (devices are still in use)"));
69866705Sml29623 				return;
69876705Sml29623 			}
69886705Sml29623 		}
69896705Sml29623 	}
69906705Sml29623 
69916705Sml29623 	if ((hw_p->flags & COMMON_RESET_NIU_PCI) != COMMON_RESET_NIU_PCI) {
69926705Sml29623 		hw_p->flags |= COMMON_RESET_NIU_PCI;
69936705Sml29623 		rvalue = pci_config_get32(nxgep->dev_regs->nxge_pciregh,
69946705Sml29623 		    NXGE_PCI_PORT_LOGIC_OFFSET);
69956705Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
69966705Sml29623 		    "nxge_niu_peu_reset: read offset 0x%x (%d) "
69976705Sml29623 		    "(data 0x%x)",
69986705Sml29623 		    NXGE_PCI_PORT_LOGIC_OFFSET,
69996705Sml29623 		    NXGE_PCI_PORT_LOGIC_OFFSET,
70006705Sml29623 		    rvalue));
70016705Sml29623 
70026705Sml29623 		rvalue |= NXGE_PCI_RESET_ALL;
70036705Sml29623 		pci_config_put32(nxgep->dev_regs->nxge_pciregh,
70046705Sml29623 		    NXGE_PCI_PORT_LOGIC_OFFSET, rvalue);
70056705Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
70066705Sml29623 		    "nxge_niu_peu_reset: RESETTING NIU: write NIU reset 0x%x",
70076705Sml29623 		    rvalue));
70086705Sml29623 
70096705Sml29623 		NXGE_DELAY(NXGE_PCI_RESET_WAIT);
70106705Sml29623 	}
70116705Sml29623 
70126705Sml29623 	MUTEX_EXIT(&hw_p->nxge_cfg_lock);
70136705Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL, "<== nxge_niu_peu_reset"));
70146705Sml29623 }
70157126Sml29623 
70167126Sml29623 static void
70177126Sml29623 nxge_set_pci_replay_timeout(p_nxge_t nxgep)
70187126Sml29623 {
70198275SEric Cheng 	p_dev_regs_t	dev_regs;
70207126Sml29623 	uint32_t	value;
70217126Sml29623 
70227126Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_set_pci_replay_timeout"));
70237126Sml29623 
70247126Sml29623 	if (!nxge_set_replay_timer) {
70257126Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
70267126Sml29623 		    "==> nxge_set_pci_replay_timeout: will not change "
70277126Sml29623 		    "the timeout"));
70287126Sml29623 		return;
70297126Sml29623 	}
70307126Sml29623 
70317126Sml29623 	dev_regs = nxgep->dev_regs;
70327126Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
70337126Sml29623 	    "==> nxge_set_pci_replay_timeout: dev_regs 0x%p pcireg 0x%p",
70347126Sml29623 	    dev_regs, dev_regs->nxge_pciregh));
70357126Sml29623 
70367126Sml29623 	if (dev_regs == NULL || (dev_regs->nxge_pciregh == NULL)) {
70377145Syc148097 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
70387126Sml29623 		    "==> nxge_set_pci_replay_timeout: NULL dev_regs $%p or "
70397126Sml29623 		    "no PCI handle",
70407126Sml29623 		    dev_regs));
70417126Sml29623 		return;
70427126Sml29623 	}
70437126Sml29623 	value = (pci_config_get32(dev_regs->nxge_pciregh,
70447126Sml29623 	    PCI_REPLAY_TIMEOUT_CFG_OFFSET) |
70457126Sml29623 	    (nxge_replay_timeout << PCI_REPLAY_TIMEOUT_SHIFT));
70467126Sml29623 
70477126Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
70487126Sml29623 	    "nxge_set_pci_replay_timeout: replay timeout value before set 0x%x "
70497126Sml29623 	    "(timeout value to set 0x%x at offset 0x%x) value 0x%x",
70507126Sml29623 	    pci_config_get32(dev_regs->nxge_pciregh,
70517126Sml29623 	    PCI_REPLAY_TIMEOUT_CFG_OFFSET), nxge_replay_timeout,
70527126Sml29623 	    PCI_REPLAY_TIMEOUT_CFG_OFFSET, value));
70537126Sml29623 
70547126Sml29623 	pci_config_put32(dev_regs->nxge_pciregh, PCI_REPLAY_TIMEOUT_CFG_OFFSET,
70557126Sml29623 	    value);
70567126Sml29623 
70577126Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
70587126Sml29623 	    "nxge_set_pci_replay_timeout: replay timeout value after set 0x%x",
70597126Sml29623 	    pci_config_get32(dev_regs->nxge_pciregh,
70607126Sml29623 	    PCI_REPLAY_TIMEOUT_CFG_OFFSET)));
70617126Sml29623 
70627126Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_set_pci_replay_timeout"));
70637126Sml29623 }
70647656SSherry.Moore@Sun.COM 
70657656SSherry.Moore@Sun.COM /*
70667656SSherry.Moore@Sun.COM  * quiesce(9E) entry point.
70677656SSherry.Moore@Sun.COM  *
70687656SSherry.Moore@Sun.COM  * This function is called when the system is single-threaded at high
70697656SSherry.Moore@Sun.COM  * PIL with preemption disabled. Therefore, this function must not be
70707656SSherry.Moore@Sun.COM  * blocked.
70717656SSherry.Moore@Sun.COM  *
70727656SSherry.Moore@Sun.COM  * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
70737656SSherry.Moore@Sun.COM  * DDI_FAILURE indicates an error condition and should almost never happen.
70747656SSherry.Moore@Sun.COM  */
70757656SSherry.Moore@Sun.COM static int
70767656SSherry.Moore@Sun.COM nxge_quiesce(dev_info_t *dip)
70777656SSherry.Moore@Sun.COM {
70787656SSherry.Moore@Sun.COM 	int instance = ddi_get_instance(dip);
70797656SSherry.Moore@Sun.COM 	p_nxge_t nxgep = (p_nxge_t)ddi_get_soft_state(nxge_list, instance);
70807656SSherry.Moore@Sun.COM 
70817656SSherry.Moore@Sun.COM 	if (nxgep == NULL)
70827656SSherry.Moore@Sun.COM 		return (DDI_FAILURE);
70837656SSherry.Moore@Sun.COM 
70847656SSherry.Moore@Sun.COM 	/* Turn off debugging */
70857656SSherry.Moore@Sun.COM 	nxge_debug_level = NO_DEBUG;
70867656SSherry.Moore@Sun.COM 	nxgep->nxge_debug_level = NO_DEBUG;
70877656SSherry.Moore@Sun.COM 	npi_debug_level = NO_DEBUG;
70887656SSherry.Moore@Sun.COM 
70897656SSherry.Moore@Sun.COM 	/*
70907656SSherry.Moore@Sun.COM 	 * Stop link monitor only when linkchkmod is interrupt based
70917656SSherry.Moore@Sun.COM 	 */
70927656SSherry.Moore@Sun.COM 	if (nxgep->mac.linkchkmode == LINKCHK_INTR) {
70937656SSherry.Moore@Sun.COM 		(void) nxge_link_monitor(nxgep, LINK_MONITOR_STOP);
70947656SSherry.Moore@Sun.COM 	}
70957656SSherry.Moore@Sun.COM 
70967656SSherry.Moore@Sun.COM 	(void) nxge_intr_hw_disable(nxgep);
70977656SSherry.Moore@Sun.COM 
70987656SSherry.Moore@Sun.COM 	/*
70997656SSherry.Moore@Sun.COM 	 * Reset the receive MAC side.
71007656SSherry.Moore@Sun.COM 	 */
71017656SSherry.Moore@Sun.COM 	(void) nxge_rx_mac_disable(nxgep);
71027656SSherry.Moore@Sun.COM 
71037656SSherry.Moore@Sun.COM 	/* Disable and soft reset the IPP */
71047656SSherry.Moore@Sun.COM 	if (!isLDOMguest(nxgep))
71057656SSherry.Moore@Sun.COM 		(void) nxge_ipp_disable(nxgep);
71067656SSherry.Moore@Sun.COM 
71077656SSherry.Moore@Sun.COM 	/*
71087656SSherry.Moore@Sun.COM 	 * Reset the transmit/receive DMA side.
71097656SSherry.Moore@Sun.COM 	 */
71107656SSherry.Moore@Sun.COM 	(void) nxge_txdma_hw_mode(nxgep, NXGE_DMA_STOP);
71117656SSherry.Moore@Sun.COM 	(void) nxge_rxdma_hw_mode(nxgep, NXGE_DMA_STOP);
71127656SSherry.Moore@Sun.COM 
71137656SSherry.Moore@Sun.COM 	/*
71147656SSherry.Moore@Sun.COM 	 * Reset the transmit MAC side.
71157656SSherry.Moore@Sun.COM 	 */
71167656SSherry.Moore@Sun.COM 	(void) nxge_tx_mac_disable(nxgep);
71177656SSherry.Moore@Sun.COM 
71187656SSherry.Moore@Sun.COM 	return (DDI_SUCCESS);
71197656SSherry.Moore@Sun.COM }
7120