xref: /onnv-gate/usr/src/uts/common/io/nxge/nxge_main.c (revision 9047:a54fa242f4f8)
13859Sml29623 /*
23859Sml29623  * CDDL HEADER START
33859Sml29623  *
43859Sml29623  * The contents of this file are subject to the terms of the
53859Sml29623  * Common Development and Distribution License (the "License").
63859Sml29623  * You may not use this file except in compliance with the License.
73859Sml29623  *
83859Sml29623  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93859Sml29623  * or http://www.opensolaris.org/os/licensing.
103859Sml29623  * See the License for the specific language governing permissions
113859Sml29623  * and limitations under the License.
123859Sml29623  *
133859Sml29623  * When distributing Covered Code, include this CDDL HEADER in each
143859Sml29623  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153859Sml29623  * If applicable, add the following below this CDDL HEADER, with the
163859Sml29623  * fields enclosed by brackets "[]" replaced with your own identifying
173859Sml29623  * information: Portions Copyright [yyyy] [name of copyright owner]
183859Sml29623  *
193859Sml29623  * CDDL HEADER END
203859Sml29623  */
213859Sml29623 /*
228661SSantwona.Behera@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
233859Sml29623  * Use is subject to license terms.
243859Sml29623  */
253859Sml29623 
263859Sml29623 /*
273859Sml29623  * SunOs MT STREAMS NIU/Neptune 10Gb Ethernet Device Driver.
283859Sml29623  */
293859Sml29623 #include	<sys/nxge/nxge_impl.h>
306495Sspeer #include	<sys/nxge/nxge_hio.h>
316495Sspeer #include	<sys/nxge/nxge_rxdma.h>
323859Sml29623 #include	<sys/pcie.h>
333859Sml29623 
343859Sml29623 uint32_t 	nxge_use_partition = 0;		/* debug partition flag */
353859Sml29623 uint32_t 	nxge_dma_obp_props_only = 1;	/* use obp published props */
363859Sml29623 uint32_t 	nxge_use_rdc_intr = 1;		/* debug to assign rdc intr */
373859Sml29623 /*
385013Sml29623  * PSARC/2007/453 MSI-X interrupt limit override
393859Sml29623  */
405013Sml29623 uint32_t	nxge_msi_enable = 2;
413859Sml29623 
426611Sml29623 /*
436705Sml29623  * Software workaround for a Neptune (PCI-E)
446705Sml29623  * hardware interrupt bug which the hardware
456705Sml29623  * may generate spurious interrupts after the
466705Sml29623  * device interrupt handler was removed. If this flag
476705Sml29623  * is enabled, the driver will reset the
486705Sml29623  * hardware when devices are being detached.
496705Sml29623  */
506705Sml29623 uint32_t	nxge_peu_reset_enable = 0;
516705Sml29623 
526705Sml29623 /*
536611Sml29623  * Software workaround for the hardware
546611Sml29623  * checksum bugs that affect packet transmission
556611Sml29623  * and receive:
566611Sml29623  *
576611Sml29623  * Usage of nxge_cksum_offload:
586611Sml29623  *
596611Sml29623  *  (1) nxge_cksum_offload = 0 (default):
606611Sml29623  *	- transmits packets:
616611Sml29623  *	  TCP: uses the hardware checksum feature.
626611Sml29623  *	  UDP: driver will compute the software checksum
636611Sml29623  *	       based on the partial checksum computed
646611Sml29623  *	       by the IP layer.
656611Sml29623  *	- receives packets
666611Sml29623  *	  TCP: marks packets checksum flags based on hardware result.
676611Sml29623  *	  UDP: will not mark checksum flags.
686611Sml29623  *
696611Sml29623  *  (2) nxge_cksum_offload = 1:
706611Sml29623  *	- transmit packets:
716611Sml29623  *	  TCP/UDP: uses the hardware checksum feature.
726611Sml29623  *	- receives packets
736611Sml29623  *	  TCP/UDP: marks packet checksum flags based on hardware result.
746611Sml29623  *
756611Sml29623  *  (3) nxge_cksum_offload = 2:
766611Sml29623  *	- The driver will not register its checksum capability.
776611Sml29623  *	  Checksum for both TCP and UDP will be computed
786611Sml29623  *	  by the stack.
796611Sml29623  *	- The software LSO is not allowed in this case.
806611Sml29623  *
816611Sml29623  *  (4) nxge_cksum_offload > 2:
826611Sml29623  *	- Will be treated as it is set to 2
836611Sml29623  *	  (stack will compute the checksum).
846611Sml29623  *
856611Sml29623  *  (5) If the hardware bug is fixed, this workaround
866611Sml29623  *	needs to be updated accordingly to reflect
876611Sml29623  *	the new hardware revision.
886611Sml29623  */
896611Sml29623 uint32_t	nxge_cksum_offload = 0;
906495Sspeer 
913859Sml29623 /*
923859Sml29623  * Globals: tunable parameters (/etc/system or adb)
933859Sml29623  *
943859Sml29623  */
953859Sml29623 uint32_t 	nxge_rbr_size = NXGE_RBR_RBB_DEFAULT;
963859Sml29623 uint32_t 	nxge_rbr_spare_size = 0;
973859Sml29623 uint32_t 	nxge_rcr_size = NXGE_RCR_DEFAULT;
983859Sml29623 uint32_t 	nxge_tx_ring_size = NXGE_TX_RING_DEFAULT;
994193Sspeer boolean_t 	nxge_no_msg = B_TRUE;		/* control message display */
1003859Sml29623 uint32_t 	nxge_no_link_notify = 0;	/* control DL_NOTIFY */
1013859Sml29623 uint32_t 	nxge_bcopy_thresh = TX_BCOPY_MAX;
1023859Sml29623 uint32_t 	nxge_dvma_thresh = TX_FASTDVMA_MIN;
1033859Sml29623 uint32_t 	nxge_dma_stream_thresh = TX_STREAM_MIN;
1043859Sml29623 uint32_t	nxge_jumbo_mtu	= TX_JUMBO_MTU;
1053859Sml29623 boolean_t	nxge_jumbo_enable = B_FALSE;
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);
2623859Sml29623 
2633859Sml29623 /*
2643859Sml29623  * The next declarations are for the GLDv3 interface.
2653859Sml29623  */
2663859Sml29623 static int nxge_m_start(void *);
2673859Sml29623 static void nxge_m_stop(void *);
2683859Sml29623 static int nxge_m_multicst(void *, boolean_t, const uint8_t *);
2693859Sml29623 static int nxge_m_promisc(void *, boolean_t);
2703859Sml29623 static void nxge_m_ioctl(void *, queue_t *, mblk_t *);
2713859Sml29623 static nxge_status_t nxge_mac_register(p_nxge_t);
2728275SEric Cheng static int nxge_altmac_set(p_nxge_t nxgep, uint8_t *mac_addr,
2738275SEric Cheng 	int slot, int rdctbl, boolean_t usetbl);
2748275SEric Cheng void nxge_mmac_kstat_update(p_nxge_t nxgep, int slot,
2753859Sml29623 	boolean_t factory);
2768275SEric Cheng #if defined(sun4v)
2778275SEric Cheng extern mblk_t *nxge_m_tx(void *arg, mblk_t *mp);
2788275SEric Cheng #endif
2798275SEric Cheng 
2808275SEric Cheng static void nxge_m_getfactaddr(void *, uint_t, uint8_t *);
2816439Sml29623 static	boolean_t nxge_m_getcapab(void *, mac_capab_t, void *);
2826439Sml29623 static int nxge_m_setprop(void *, const char *, mac_prop_id_t,
2836439Sml29623     uint_t, const void *);
2846439Sml29623 static int nxge_m_getprop(void *, const char *, mac_prop_id_t,
2858118SVasumathi.Sundaram@Sun.COM     uint_t, uint_t, void *, uint_t *);
2866439Sml29623 static int nxge_set_priv_prop(nxge_t *, const char *, uint_t,
2876439Sml29623     const void *);
2886512Ssowmini static int nxge_get_priv_prop(nxge_t *, const char *, uint_t, uint_t,
2898118SVasumathi.Sundaram@Sun.COM     void *, uint_t *);
2906512Ssowmini static int nxge_get_def_val(nxge_t *, mac_prop_id_t, uint_t, void *);
2918275SEric Cheng static void nxge_fill_ring(void *, mac_ring_type_t, const int, const int,
2928275SEric Cheng     mac_ring_info_t *, mac_ring_handle_t);
2938275SEric Cheng static void nxge_group_add_ring(mac_group_driver_t, mac_ring_driver_t,
2948275SEric Cheng     mac_ring_type_t);
2958275SEric Cheng static void nxge_group_rem_ring(mac_group_driver_t, mac_ring_driver_t,
2968275SEric Cheng     mac_ring_type_t);
2976512Ssowmini 
2986705Sml29623 static void nxge_niu_peu_reset(p_nxge_t nxgep);
2997126Sml29623 static void nxge_set_pci_replay_timeout(nxge_t *);
3006512Ssowmini 
3016512Ssowmini mac_priv_prop_t nxge_priv_props[] = {
3026512Ssowmini 	{"_adv_10gfdx_cap", MAC_PROP_PERM_RW},
3036512Ssowmini 	{"_adv_pause_cap", MAC_PROP_PERM_RW},
3046512Ssowmini 	{"_function_number", MAC_PROP_PERM_READ},
3056512Ssowmini 	{"_fw_version", MAC_PROP_PERM_READ},
3066512Ssowmini 	{"_port_mode", MAC_PROP_PERM_READ},
3076512Ssowmini 	{"_hot_swap_phy", MAC_PROP_PERM_READ},
3086512Ssowmini 	{"_accept_jumbo", MAC_PROP_PERM_RW},
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 
3863859Sml29623 void			*nxge_hw_list = NULL;
3873859Sml29623 nxge_os_mutex_t 	nxge_common_lock;
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;
5287529SSriharsha.Basavapatna@Sun.COM 	p_nxge_param_t	param_arr;
5293859Sml29623 
5303859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_attach"));
5313859Sml29623 
5323859Sml29623 	/*
5333859Sml29623 	 * Get the device instance since we'll need to setup
5343859Sml29623 	 * or retrieve a soft state for this instance.
5353859Sml29623 	 */
5363859Sml29623 	instance = ddi_get_instance(dip);
5373859Sml29623 
5383859Sml29623 	switch (cmd) {
5393859Sml29623 	case DDI_ATTACH:
5403859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_ATTACH"));
5413859Sml29623 		break;
5423859Sml29623 
5433859Sml29623 	case DDI_RESUME:
5443859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_RESUME"));
5453859Sml29623 		nxgep = (p_nxge_t)ddi_get_soft_state(nxge_list, instance);
5463859Sml29623 		if (nxgep == NULL) {
5473859Sml29623 			status = DDI_FAILURE;
5483859Sml29623 			break;
5493859Sml29623 		}
5503859Sml29623 		if (nxgep->dip != dip) {
5513859Sml29623 			status = DDI_FAILURE;
5523859Sml29623 			break;
5533859Sml29623 		}
5543859Sml29623 		if (nxgep->suspended == DDI_PM_SUSPEND) {
5553859Sml29623 			status = ddi_dev_is_needed(nxgep->dip, 0, 1);
5563859Sml29623 		} else {
5574185Sspeer 			status = nxge_resume(nxgep);
5583859Sml29623 		}
5593859Sml29623 		goto nxge_attach_exit;
5603859Sml29623 
5613859Sml29623 	case DDI_PM_RESUME:
5623859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_PM_RESUME"));
5633859Sml29623 		nxgep = (p_nxge_t)ddi_get_soft_state(nxge_list, instance);
5643859Sml29623 		if (nxgep == NULL) {
5653859Sml29623 			status = DDI_FAILURE;
5663859Sml29623 			break;
5673859Sml29623 		}
5683859Sml29623 		if (nxgep->dip != dip) {
5693859Sml29623 			status = DDI_FAILURE;
5703859Sml29623 			break;
5713859Sml29623 		}
5724185Sspeer 		status = nxge_resume(nxgep);
5733859Sml29623 		goto nxge_attach_exit;
5743859Sml29623 
5753859Sml29623 	default:
5763859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing unknown"));
5773859Sml29623 		status = DDI_FAILURE;
5783859Sml29623 		goto nxge_attach_exit;
5793859Sml29623 	}
5803859Sml29623 
5813859Sml29623 
5823859Sml29623 	if (ddi_soft_state_zalloc(nxge_list, instance) == DDI_FAILURE) {
5833859Sml29623 		status = DDI_FAILURE;
5843859Sml29623 		goto nxge_attach_exit;
5853859Sml29623 	}
5863859Sml29623 
5873859Sml29623 	nxgep = ddi_get_soft_state(nxge_list, instance);
5883859Sml29623 	if (nxgep == NULL) {
5894977Sraghus 		status = NXGE_ERROR;
5904977Sraghus 		goto nxge_attach_fail2;
5913859Sml29623 	}
5923859Sml29623 
5934693Stm144005 	nxgep->nxge_magic = NXGE_MAGIC;
5944693Stm144005 
5953859Sml29623 	nxgep->drv_state = 0;
5963859Sml29623 	nxgep->dip = dip;
5973859Sml29623 	nxgep->instance = instance;
5983859Sml29623 	nxgep->p_dip = ddi_get_parent(dip);
5993859Sml29623 	nxgep->nxge_debug_level = nxge_debug_level;
6003859Sml29623 	npi_debug_level = nxge_debug_level;
6013859Sml29623 
6026495Sspeer 	/* Are we a guest running in a Hybrid I/O environment? */
6036495Sspeer 	nxge_get_environs(nxgep);
6043859Sml29623 
6053859Sml29623 	status = nxge_map_regs(nxgep);
6066495Sspeer 
6073859Sml29623 	if (status != NXGE_OK) {
6083859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "nxge_map_regs failed"));
6094977Sraghus 		goto nxge_attach_fail3;
6103859Sml29623 	}
6113859Sml29623 
6126495Sspeer 	nxge_fm_init(nxgep, &nxge_dev_reg_acc_attr,
6136495Sspeer 	    &nxge_dev_desc_dma_acc_attr,
6146495Sspeer 	    &nxge_rx_dma_attr);
6156495Sspeer 
6166495Sspeer 	/* Create & initialize the per-Neptune data structure */
6176495Sspeer 	/* (even if we're a guest). */
6183859Sml29623 	status = nxge_init_common_dev(nxgep);
6193859Sml29623 	if (status != NXGE_OK) {
6203859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
6216512Ssowmini 		    "nxge_init_common_dev failed"));
6224977Sraghus 		goto nxge_attach_fail4;
6233859Sml29623 	}
6243859Sml29623 
6257126Sml29623 	/*
6267126Sml29623 	 * Software workaround: set the replay timer.
6277126Sml29623 	 */
6287126Sml29623 	if (nxgep->niu_type != N2_NIU) {
6297126Sml29623 		nxge_set_pci_replay_timeout(nxgep);
6307126Sml29623 	}
6318275SEric Cheng #if defined(sun4v)
6328275SEric Cheng 	if (isLDOMguest(nxgep)) {
6338275SEric Cheng 		nxge_m_callbacks.mc_tx = nxge_m_tx;
6348275SEric Cheng 	}
6358275SEric Cheng #endif
6367126Sml29623 
6376495Sspeer #if defined(sun4v)
6386495Sspeer 	/* This is required by nxge_hio_init(), which follows. */
6396495Sspeer 	if ((status = nxge_hsvc_register(nxgep)) != DDI_SUCCESS)
6407587SMichael.Speer@Sun.COM 		goto nxge_attach_fail4;
6416495Sspeer #endif
6426495Sspeer 
6436495Sspeer 	if ((status = nxge_hio_init(nxgep)) != NXGE_OK) {
6446495Sspeer 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
6456512Ssowmini 		    "nxge_hio_init failed"));
6466495Sspeer 		goto nxge_attach_fail4;
6476495Sspeer 	}
6486495Sspeer 
6494732Sdavemq 	if (nxgep->niu_type == NEPTUNE_2_10GF) {
6504732Sdavemq 		if (nxgep->function_num > 1) {
6516028Ssbehera 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "Unsupported"
6524732Sdavemq 			    " function %d. Only functions 0 and 1 are "
6534732Sdavemq 			    "supported for this card.", nxgep->function_num));
6544732Sdavemq 			status = NXGE_ERROR;
6554977Sraghus 			goto nxge_attach_fail4;
6564732Sdavemq 		}
6574732Sdavemq 	}
6584732Sdavemq 
6596495Sspeer 	if (isLDOMguest(nxgep)) {
6606495Sspeer 		/*
6616495Sspeer 		 * Use the function number here.
6626495Sspeer 		 */
6636495Sspeer 		nxgep->mac.portnum = nxgep->function_num;
6646495Sspeer 		nxgep->mac.porttype = PORT_TYPE_LOGICAL;
6656495Sspeer 
6666495Sspeer 		/* XXX We'll set the MAC address counts to 1 for now. */
6676495Sspeer 		mmac_info = &nxgep->nxge_mmac_info;
6686495Sspeer 		mmac_info->num_mmac = 1;
6696495Sspeer 		mmac_info->naddrfree = 1;
6703859Sml29623 	} else {
6716495Sspeer 		portn = NXGE_GET_PORT_NUM(nxgep->function_num);
6726495Sspeer 		nxgep->mac.portnum = portn;
6736495Sspeer 		if ((portn == 0) || (portn == 1))
6746495Sspeer 			nxgep->mac.porttype = PORT_TYPE_XMAC;
6756495Sspeer 		else
6766495Sspeer 			nxgep->mac.porttype = PORT_TYPE_BMAC;
6776495Sspeer 		/*
6786495Sspeer 		 * Neptune has 4 ports, the first 2 ports use XMAC (10G MAC)
6796495Sspeer 		 * internally, the rest 2 ports use BMAC (1G "Big" MAC).
6806495Sspeer 		 * The two types of MACs have different characterizations.
6816495Sspeer 		 */
6826495Sspeer 		mmac_info = &nxgep->nxge_mmac_info;
6836495Sspeer 		if (nxgep->function_num < 2) {
6846495Sspeer 			mmac_info->num_mmac = XMAC_MAX_ALT_ADDR_ENTRY;
6856495Sspeer 			mmac_info->naddrfree = XMAC_MAX_ALT_ADDR_ENTRY;
6866495Sspeer 		} else {
6876495Sspeer 			mmac_info->num_mmac = BMAC_MAX_ALT_ADDR_ENTRY;
6886495Sspeer 			mmac_info->naddrfree = BMAC_MAX_ALT_ADDR_ENTRY;
6896495Sspeer 		}
6903859Sml29623 	}
6913859Sml29623 	/*
6923859Sml29623 	 * Setup the Ndd parameters for the this instance.
6933859Sml29623 	 */
6943859Sml29623 	nxge_init_param(nxgep);
6953859Sml29623 
6963859Sml29623 	/*
6973859Sml29623 	 * Setup Register Tracing Buffer.
6983859Sml29623 	 */
6993859Sml29623 	npi_rtrace_buf_init((rtrace_t *)&npi_rtracebuf);
7003859Sml29623 
7013859Sml29623 	/* init stats ptr */
7023859Sml29623 	nxge_init_statsp(nxgep);
7034185Sspeer 
7044977Sraghus 	/*
7056495Sspeer 	 * Copy the vpd info from eeprom to a local data
7066495Sspeer 	 * structure, and then check its validity.
7074977Sraghus 	 */
7086495Sspeer 	if (!isLDOMguest(nxgep)) {
7096495Sspeer 		int *regp;
7106495Sspeer 		uint_t reglen;
7116495Sspeer 		int rv;
7126495Sspeer 
7136495Sspeer 		nxge_vpd_info_get(nxgep);
7146495Sspeer 
7156495Sspeer 		/* Find the NIU config handle. */
7166495Sspeer 		rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
7176495Sspeer 		    ddi_get_parent(nxgep->dip), DDI_PROP_DONTPASS,
7186495Sspeer 		    "reg", &regp, &reglen);
7196495Sspeer 
7206495Sspeer 		if (rv != DDI_PROP_SUCCESS) {
7216495Sspeer 			goto nxge_attach_fail5;
7226495Sspeer 		}
7236495Sspeer 		/*
7246495Sspeer 		 * The address_hi, that is the first int, in the reg
7256495Sspeer 		 * property consists of config handle, but need to remove
7266495Sspeer 		 * the bits 28-31 which are OBP specific info.
7276495Sspeer 		 */
7286495Sspeer 		nxgep->niu_cfg_hdl = (*regp) & 0xFFFFFFF;
7296495Sspeer 		ddi_prop_free(regp);
7306495Sspeer 	}
7316495Sspeer 
7326495Sspeer 	if (isLDOMguest(nxgep)) {
7336495Sspeer 		uchar_t *prop_val;
7346495Sspeer 		uint_t prop_len;
7357529SSriharsha.Basavapatna@Sun.COM 		uint32_t max_frame_size;
7366495Sspeer 
7376495Sspeer 		extern void nxge_get_logical_props(p_nxge_t);
7386495Sspeer 
7396495Sspeer 		nxgep->statsp->mac_stats.xcvr_inuse = LOGICAL_XCVR;
7406495Sspeer 		nxgep->mac.portmode = PORT_LOGICAL;
7416495Sspeer 		(void) ddi_prop_update_string(DDI_DEV_T_NONE, nxgep->dip,
7426495Sspeer 		    "phy-type", "virtual transceiver");
7436495Sspeer 
7446495Sspeer 		nxgep->nports = 1;
7456495Sspeer 		nxgep->board_ver = 0;	/* XXX What? */
7466495Sspeer 
7476495Sspeer 		/*
7486495Sspeer 		 * local-mac-address property gives us info on which
7496495Sspeer 		 * specific MAC address the Hybrid resource is associated
7506495Sspeer 		 * with.
7516495Sspeer 		 */
7526495Sspeer 		if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, nxgep->dip, 0,
7536495Sspeer 		    "local-mac-address", &prop_val,
7546495Sspeer 		    &prop_len) != DDI_PROP_SUCCESS) {
7556495Sspeer 			goto nxge_attach_fail5;
7566495Sspeer 		}
7576495Sspeer 		if (prop_len !=  ETHERADDRL) {
7586495Sspeer 			ddi_prop_free(prop_val);
7596495Sspeer 			goto nxge_attach_fail5;
7606495Sspeer 		}
7616495Sspeer 		ether_copy(prop_val, nxgep->hio_mac_addr);
7626495Sspeer 		ddi_prop_free(prop_val);
7636495Sspeer 		nxge_get_logical_props(nxgep);
7646495Sspeer 
7657529SSriharsha.Basavapatna@Sun.COM 		/*
7667529SSriharsha.Basavapatna@Sun.COM 		 * Enable Jumbo property based on the "max-frame-size"
7677529SSriharsha.Basavapatna@Sun.COM 		 * property value.
7687529SSriharsha.Basavapatna@Sun.COM 		 */
7697529SSriharsha.Basavapatna@Sun.COM 		max_frame_size = ddi_prop_get_int(DDI_DEV_T_ANY,
7707529SSriharsha.Basavapatna@Sun.COM 		    nxgep->dip, DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
7717529SSriharsha.Basavapatna@Sun.COM 		    "max-frame-size", NXGE_MTU_DEFAULT_MAX);
7727529SSriharsha.Basavapatna@Sun.COM 		if ((max_frame_size > NXGE_MTU_DEFAULT_MAX) &&
7737529SSriharsha.Basavapatna@Sun.COM 		    (max_frame_size <= TX_JUMBO_MTU)) {
7747529SSriharsha.Basavapatna@Sun.COM 			param_arr = nxgep->param_arr;
7757529SSriharsha.Basavapatna@Sun.COM 
7767529SSriharsha.Basavapatna@Sun.COM 			param_arr[param_accept_jumbo].value = 1;
7777529SSriharsha.Basavapatna@Sun.COM 			nxgep->mac.is_jumbo = B_TRUE;
7787529SSriharsha.Basavapatna@Sun.COM 			nxgep->mac.maxframesize = (uint16_t)max_frame_size;
7797529SSriharsha.Basavapatna@Sun.COM 			nxgep->mac.default_mtu = nxgep->mac.maxframesize -
7807529SSriharsha.Basavapatna@Sun.COM 			    NXGE_EHEADER_VLAN_CRC;
7817529SSriharsha.Basavapatna@Sun.COM 		}
7826495Sspeer 	} else {
7836495Sspeer 		status = nxge_xcvr_find(nxgep);
7846495Sspeer 
7856495Sspeer 		if (status != NXGE_OK) {
7866495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "nxge_attach: "
7876512Ssowmini 			    " Couldn't determine card type"
7886512Ssowmini 			    " .... exit "));
7896495Sspeer 			goto nxge_attach_fail5;
7906495Sspeer 		}
7916495Sspeer 
7926495Sspeer 		status = nxge_get_config_properties(nxgep);
7936495Sspeer 
7946495Sspeer 		if (status != NXGE_OK) {
7956495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
7966512Ssowmini 			    "get_hw create failed"));
7976495Sspeer 			goto nxge_attach_fail;
7986495Sspeer 		}
7993859Sml29623 	}
8003859Sml29623 
8013859Sml29623 	/*
8023859Sml29623 	 * Setup the Kstats for the driver.
8033859Sml29623 	 */
8043859Sml29623 	nxge_setup_kstats(nxgep);
8053859Sml29623 
8066495Sspeer 	if (!isLDOMguest(nxgep))
8076495Sspeer 		nxge_setup_param(nxgep);
8083859Sml29623 
8093859Sml29623 	status = nxge_setup_system_dma_pages(nxgep);
8103859Sml29623 	if (status != NXGE_OK) {
8113859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "set dma page failed"));
8123859Sml29623 		goto nxge_attach_fail;
8133859Sml29623 	}
8143859Sml29623 
8153859Sml29623 	nxge_hw_id_init(nxgep);
8166495Sspeer 
8176495Sspeer 	if (!isLDOMguest(nxgep))
8186495Sspeer 		nxge_hw_init_niu_common(nxgep);
8193859Sml29623 
8203859Sml29623 	status = nxge_setup_mutexes(nxgep);
8213859Sml29623 	if (status != NXGE_OK) {
8223859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "set mutex failed"));
8233859Sml29623 		goto nxge_attach_fail;
8243859Sml29623 	}
8253859Sml29623 
8266495Sspeer #if defined(sun4v)
8276495Sspeer 	if (isLDOMguest(nxgep)) {
8286495Sspeer 		/* Find our VR & channel sets. */
8296495Sspeer 		status = nxge_hio_vr_add(nxgep);
8307812SMichael.Speer@Sun.COM 		if (status != NXGE_OK) {
8317812SMichael.Speer@Sun.COM 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
8327812SMichael.Speer@Sun.COM 			    "nxge_hio_vr_add failed"));
8337812SMichael.Speer@Sun.COM 			(void) hsvc_unregister(&nxgep->niu_hsvc);
8347812SMichael.Speer@Sun.COM 			nxgep->niu_hsvc_available = B_FALSE;
8357812SMichael.Speer@Sun.COM 		}
8366495Sspeer 		goto nxge_attach_exit;
8376495Sspeer 	}
8386495Sspeer #endif
8396495Sspeer 
8403859Sml29623 	status = nxge_setup_dev(nxgep);
8413859Sml29623 	if (status != DDI_SUCCESS) {
8423859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "set dev failed"));
8433859Sml29623 		goto nxge_attach_fail;
8443859Sml29623 	}
8453859Sml29623 
8463859Sml29623 	status = nxge_add_intrs(nxgep);
8473859Sml29623 	if (status != DDI_SUCCESS) {
8483859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "add_intr failed"));
8493859Sml29623 		goto nxge_attach_fail;
8503859Sml29623 	}
8517812SMichael.Speer@Sun.COM 
8526835Syc148097 	/* If a guest, register with vio_net instead. */
8534977Sraghus 	if ((status = nxge_mac_register(nxgep)) != NXGE_OK) {
8543859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
8556495Sspeer 		    "unable to register to mac layer (%d)", status));
8563859Sml29623 		goto nxge_attach_fail;
8573859Sml29623 	}
8583859Sml29623 
8593859Sml29623 	mac_link_update(nxgep->mach, LINK_STATE_UNKNOWN);
8603859Sml29623 
8616495Sspeer 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
8626495Sspeer 	    "registered to mac (instance %d)", instance));
8633859Sml29623 
8646835Syc148097 	/* nxge_link_monitor calls xcvr.check_link recursively */
8653859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_START);
8663859Sml29623 
8673859Sml29623 	goto nxge_attach_exit;
8683859Sml29623 
8693859Sml29623 nxge_attach_fail:
8703859Sml29623 	nxge_unattach(nxgep);
8714977Sraghus 	goto nxge_attach_fail1;
8724977Sraghus 
8734977Sraghus nxge_attach_fail5:
8744977Sraghus 	/*
8754977Sraghus 	 * Tear down the ndd parameters setup.
8764977Sraghus 	 */
8774977Sraghus 	nxge_destroy_param(nxgep);
8784977Sraghus 
8794977Sraghus 	/*
8804977Sraghus 	 * Tear down the kstat setup.
8814977Sraghus 	 */
8824977Sraghus 	nxge_destroy_kstats(nxgep);
8834977Sraghus 
8844977Sraghus nxge_attach_fail4:
8854977Sraghus 	if (nxgep->nxge_hw_p) {
8864977Sraghus 		nxge_uninit_common_dev(nxgep);
8874977Sraghus 		nxgep->nxge_hw_p = NULL;
8884977Sraghus 	}
8894977Sraghus 
8904977Sraghus nxge_attach_fail3:
8914977Sraghus 	/*
8924977Sraghus 	 * Unmap the register setup.
8934977Sraghus 	 */
8944977Sraghus 	nxge_unmap_regs(nxgep);
8954977Sraghus 
8964977Sraghus 	nxge_fm_fini(nxgep);
8974977Sraghus 
8984977Sraghus nxge_attach_fail2:
8994977Sraghus 	ddi_soft_state_free(nxge_list, nxgep->instance);
9004977Sraghus 
9014977Sraghus nxge_attach_fail1:
9024185Sspeer 	if (status != NXGE_OK)
9034185Sspeer 		status = (NXGE_ERROR | NXGE_DDI_FAILED);
9043859Sml29623 	nxgep = NULL;
9053859Sml29623 
9063859Sml29623 nxge_attach_exit:
9073859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_attach status = 0x%08x",
9086512Ssowmini 	    status));
9093859Sml29623 
9103859Sml29623 	return (status);
9113859Sml29623 }
9123859Sml29623 
9133859Sml29623 static int
9143859Sml29623 nxge_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
9153859Sml29623 {
9163859Sml29623 	int 		status = DDI_SUCCESS;
9173859Sml29623 	int 		instance;
9183859Sml29623 	p_nxge_t 	nxgep = NULL;
9193859Sml29623 
9203859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_detach"));
9213859Sml29623 	instance = ddi_get_instance(dip);
9223859Sml29623 	nxgep = ddi_get_soft_state(nxge_list, instance);
9233859Sml29623 	if (nxgep == NULL) {
9243859Sml29623 		status = DDI_FAILURE;
9253859Sml29623 		goto nxge_detach_exit;
9263859Sml29623 	}
9273859Sml29623 
9283859Sml29623 	switch (cmd) {
9293859Sml29623 	case DDI_DETACH:
9303859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_DETACH"));
9313859Sml29623 		break;
9323859Sml29623 
9333859Sml29623 	case DDI_PM_SUSPEND:
9343859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_PM_SUSPEND"));
9353859Sml29623 		nxgep->suspended = DDI_PM_SUSPEND;
9363859Sml29623 		nxge_suspend(nxgep);
9373859Sml29623 		break;
9383859Sml29623 
9393859Sml29623 	case DDI_SUSPEND:
9403859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "doing DDI_SUSPEND"));
9413859Sml29623 		if (nxgep->suspended != DDI_PM_SUSPEND) {
9423859Sml29623 			nxgep->suspended = DDI_SUSPEND;
9433859Sml29623 			nxge_suspend(nxgep);
9443859Sml29623 		}
9453859Sml29623 		break;
9463859Sml29623 
9473859Sml29623 	default:
9483859Sml29623 		status = DDI_FAILURE;
9493859Sml29623 	}
9503859Sml29623 
9513859Sml29623 	if (cmd != DDI_DETACH)
9523859Sml29623 		goto nxge_detach_exit;
9533859Sml29623 
9543859Sml29623 	/*
9553859Sml29623 	 * Stop the xcvr polling.
9563859Sml29623 	 */
9573859Sml29623 	nxgep->suspended = cmd;
9583859Sml29623 
9593859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_STOP);
9603859Sml29623 
9616495Sspeer 	if (isLDOMguest(nxgep)) {
9627466SMisaki.Kataoka@Sun.COM 		if (nxgep->nxge_mac_state == NXGE_MAC_STARTED)
9637466SMisaki.Kataoka@Sun.COM 			nxge_m_stop((void *)nxgep);
9646495Sspeer 		nxge_hio_unregister(nxgep);
9656495Sspeer 	} else if (nxgep->mach && (status = mac_unregister(nxgep->mach)) != 0) {
9663859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
9676512Ssowmini 		    "<== nxge_detach status = 0x%08X", status));
9683859Sml29623 		return (DDI_FAILURE);
9693859Sml29623 	}
9703859Sml29623 
9713859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
9726512Ssowmini 	    "<== nxge_detach (mac_unregister) status = 0x%08X", status));
9733859Sml29623 
9743859Sml29623 	nxge_unattach(nxgep);
9753859Sml29623 	nxgep = NULL;
9763859Sml29623 
9773859Sml29623 nxge_detach_exit:
9783859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_detach status = 0x%08X",
9796512Ssowmini 	    status));
9803859Sml29623 
9813859Sml29623 	return (status);
9823859Sml29623 }
9833859Sml29623 
9843859Sml29623 static void
9853859Sml29623 nxge_unattach(p_nxge_t nxgep)
9863859Sml29623 {
9873859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_unattach"));
9883859Sml29623 
9893859Sml29623 	if (nxgep == NULL || nxgep->dev_regs == NULL) {
9903859Sml29623 		return;
9913859Sml29623 	}
9923859Sml29623 
9934693Stm144005 	nxgep->nxge_magic = 0;
9944693Stm144005 
9955780Ssbehera 	if (nxgep->nxge_timerid) {
9965780Ssbehera 		nxge_stop_timer(nxgep, nxgep->nxge_timerid);
9975780Ssbehera 		nxgep->nxge_timerid = 0;
9985780Ssbehera 	}
9995780Ssbehera 
10006705Sml29623 	/*
10016705Sml29623 	 * If this flag is set, it will affect the Neptune
10026705Sml29623 	 * only.
10036705Sml29623 	 */
10046705Sml29623 	if ((nxgep->niu_type != N2_NIU) && nxge_peu_reset_enable) {
10056705Sml29623 		nxge_niu_peu_reset(nxgep);
10066705Sml29623 	}
10076705Sml29623 
10086495Sspeer #if	defined(sun4v)
10096495Sspeer 	if (isLDOMguest(nxgep)) {
10106498Sspeer 		(void) nxge_hio_vr_release(nxgep);
10116495Sspeer 	}
10126495Sspeer #endif
10136495Sspeer 
10143859Sml29623 	if (nxgep->nxge_hw_p) {
10153859Sml29623 		nxge_uninit_common_dev(nxgep);
10163859Sml29623 		nxgep->nxge_hw_p = NULL;
10173859Sml29623 	}
10183859Sml29623 
10193859Sml29623 #if	defined(sun4v)
10203859Sml29623 	if (nxgep->niu_type == N2_NIU && nxgep->niu_hsvc_available == B_TRUE) {
10213859Sml29623 		(void) hsvc_unregister(&nxgep->niu_hsvc);
10223859Sml29623 		nxgep->niu_hsvc_available = B_FALSE;
10233859Sml29623 	}
10243859Sml29623 #endif
10253859Sml29623 	/*
10263859Sml29623 	 * Stop any further interrupts.
10273859Sml29623 	 */
10283859Sml29623 	nxge_remove_intrs(nxgep);
10293859Sml29623 
10303859Sml29623 	/*
10313859Sml29623 	 * Stop the device and free resources.
10323859Sml29623 	 */
10336495Sspeer 	if (!isLDOMguest(nxgep)) {
10346495Sspeer 		nxge_destroy_dev(nxgep);
10356495Sspeer 	}
10363859Sml29623 
10373859Sml29623 	/*
10383859Sml29623 	 * Tear down the ndd parameters setup.
10393859Sml29623 	 */
10403859Sml29623 	nxge_destroy_param(nxgep);
10413859Sml29623 
10423859Sml29623 	/*
10433859Sml29623 	 * Tear down the kstat setup.
10443859Sml29623 	 */
10453859Sml29623 	nxge_destroy_kstats(nxgep);
10463859Sml29623 
10473859Sml29623 	/*
10483859Sml29623 	 * Destroy all mutexes.
10493859Sml29623 	 */
10503859Sml29623 	nxge_destroy_mutexes(nxgep);
10513859Sml29623 
10523859Sml29623 	/*
10533859Sml29623 	 * Remove the list of ndd parameters which
10543859Sml29623 	 * were setup during attach.
10553859Sml29623 	 */
10563859Sml29623 	if (nxgep->dip) {
10573859Sml29623 		NXGE_DEBUG_MSG((nxgep, OBP_CTL,
10586512Ssowmini 		    " nxge_unattach: remove all properties"));
10593859Sml29623 
10603859Sml29623 		(void) ddi_prop_remove_all(nxgep->dip);
10613859Sml29623 	}
10623859Sml29623 
10633859Sml29623 #if NXGE_PROPERTY
10643859Sml29623 	nxge_remove_hard_properties(nxgep);
10653859Sml29623 #endif
10663859Sml29623 
10673859Sml29623 	/*
10683859Sml29623 	 * Unmap the register setup.
10693859Sml29623 	 */
10703859Sml29623 	nxge_unmap_regs(nxgep);
10713859Sml29623 
10723859Sml29623 	nxge_fm_fini(nxgep);
10733859Sml29623 
10743859Sml29623 	ddi_soft_state_free(nxge_list, nxgep->instance);
10753859Sml29623 
10763859Sml29623 	NXGE_DEBUG_MSG((NULL, DDI_CTL, "<== nxge_unattach"));
10773859Sml29623 }
10783859Sml29623 
10796495Sspeer #if defined(sun4v)
10806495Sspeer int
10817587SMichael.Speer@Sun.COM nxge_hsvc_register(nxge_t *nxgep)
10826495Sspeer {
10836495Sspeer 	nxge_status_t status;
10846495Sspeer 
10856495Sspeer 	if (nxgep->niu_type == N2_NIU) {
10866495Sspeer 		nxgep->niu_hsvc_available = B_FALSE;
10876495Sspeer 		bcopy(&niu_hsvc, &nxgep->niu_hsvc, sizeof (hsvc_info_t));
10886495Sspeer 		if ((status = hsvc_register(&nxgep->niu_hsvc,
10896495Sspeer 		    &nxgep->niu_min_ver)) != 0) {
10906495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
10916495Sspeer 			    "nxge_attach: %s: cannot negotiate "
10926495Sspeer 			    "hypervisor services revision %d group: 0x%lx "
10936495Sspeer 			    "major: 0x%lx minor: 0x%lx errno: %d",
10946495Sspeer 			    niu_hsvc.hsvc_modname, niu_hsvc.hsvc_rev,
10956495Sspeer 			    niu_hsvc.hsvc_group, niu_hsvc.hsvc_major,
10966495Sspeer 			    niu_hsvc.hsvc_minor, status));
10976495Sspeer 			return (DDI_FAILURE);
10986495Sspeer 		}
10996495Sspeer 		nxgep->niu_hsvc_available = B_TRUE;
11006495Sspeer 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11016512Ssowmini 		    "NIU Hypervisor service enabled"));
11026495Sspeer 	}
11036495Sspeer 
11046495Sspeer 	return (DDI_SUCCESS);
11056495Sspeer }
11066495Sspeer #endif
11076495Sspeer 
11083859Sml29623 static char n2_siu_name[] = "niu";
11093859Sml29623 
11103859Sml29623 static nxge_status_t
11113859Sml29623 nxge_map_regs(p_nxge_t nxgep)
11123859Sml29623 {
11133859Sml29623 	int		ddi_status = DDI_SUCCESS;
11143859Sml29623 	p_dev_regs_t 	dev_regs;
11153859Sml29623 	char		buf[MAXPATHLEN + 1];
11163859Sml29623 	char 		*devname;
11173859Sml29623 #ifdef	NXGE_DEBUG
11183859Sml29623 	char 		*sysname;
11193859Sml29623 #endif
11203859Sml29623 	off_t		regsize;
11213859Sml29623 	nxge_status_t	status = NXGE_OK;
11223859Sml29623 #if !defined(_BIG_ENDIAN)
11233859Sml29623 	off_t pci_offset;
11243859Sml29623 	uint16_t pcie_devctl;
11253859Sml29623 #endif
11263859Sml29623 
11276495Sspeer 	if (isLDOMguest(nxgep)) {
11286495Sspeer 		return (nxge_guest_regs_map(nxgep));
11296495Sspeer 	}
11306495Sspeer 
11313859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_map_regs"));
11323859Sml29623 	nxgep->dev_regs = NULL;
11333859Sml29623 	dev_regs = KMEM_ZALLOC(sizeof (dev_regs_t), KM_SLEEP);
11343859Sml29623 	dev_regs->nxge_regh = NULL;
11353859Sml29623 	dev_regs->nxge_pciregh = NULL;
11363859Sml29623 	dev_regs->nxge_msix_regh = NULL;
11373859Sml29623 	dev_regs->nxge_vir_regh = NULL;
11383859Sml29623 	dev_regs->nxge_vir2_regh = NULL;
11394732Sdavemq 	nxgep->niu_type = NIU_TYPE_NONE;
11403859Sml29623 
11413859Sml29623 	devname = ddi_pathname(nxgep->dip, buf);
11423859Sml29623 	ASSERT(strlen(devname) > 0);
11433859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11446512Ssowmini 	    "nxge_map_regs: pathname devname %s", devname));
11453859Sml29623 
11466835Syc148097 	/*
11476835Syc148097 	 * The driver is running on a N2-NIU system if devname is something
11486835Syc148097 	 * like "/niu@80/network@0"
11496835Syc148097 	 */
11503859Sml29623 	if (strstr(devname, n2_siu_name)) {
11513859Sml29623 		/* N2/NIU */
11523859Sml29623 		nxgep->niu_type = N2_NIU;
11533859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11546512Ssowmini 		    "nxge_map_regs: N2/NIU devname %s", devname));
11553859Sml29623 		/* get function number */
11563859Sml29623 		nxgep->function_num =
11576512Ssowmini 		    (devname[strlen(devname) -1] == '1' ? 1 : 0);
11583859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11596512Ssowmini 		    "nxge_map_regs: N2/NIU function number %d",
11606512Ssowmini 		    nxgep->function_num));
11613859Sml29623 	} else {
11623859Sml29623 		int		*prop_val;
11633859Sml29623 		uint_t 		prop_len;
11643859Sml29623 		uint8_t 	func_num;
11653859Sml29623 
11663859Sml29623 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, nxgep->dip,
11676512Ssowmini 		    0, "reg",
11686512Ssowmini 		    &prop_val, &prop_len) != DDI_PROP_SUCCESS) {
11693859Sml29623 			NXGE_DEBUG_MSG((nxgep, VPD_CTL,
11706512Ssowmini 			    "Reg property not found"));
11713859Sml29623 			ddi_status = DDI_FAILURE;
11723859Sml29623 			goto nxge_map_regs_fail0;
11733859Sml29623 
11743859Sml29623 		} else {
11753859Sml29623 			func_num = (prop_val[0] >> 8) & 0x7;
11763859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11776512Ssowmini 			    "Reg property found: fun # %d",
11786512Ssowmini 			    func_num));
11793859Sml29623 			nxgep->function_num = func_num;
11806495Sspeer 			if (isLDOMguest(nxgep)) {
11816495Sspeer 				nxgep->function_num /= 2;
11826495Sspeer 				return (NXGE_OK);
11836495Sspeer 			}
11843859Sml29623 			ddi_prop_free(prop_val);
11853859Sml29623 		}
11863859Sml29623 	}
11873859Sml29623 
11883859Sml29623 	switch (nxgep->niu_type) {
11893859Sml29623 	default:
11903859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 0, &regsize);
11913859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
11926512Ssowmini 		    "nxge_map_regs: pci config size 0x%x", regsize));
11933859Sml29623 
11943859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 0,
11956512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_pciregp), 0, 0,
11966512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_pciregh);
11973859Sml29623 		if (ddi_status != DDI_SUCCESS) {
11983859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
11996512Ssowmini 			    "ddi_map_regs, nxge bus config regs failed"));
12003859Sml29623 			goto nxge_map_regs_fail0;
12013859Sml29623 		}
12023859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12036512Ssowmini 		    "nxge_map_reg: PCI config addr 0x%0llx "
12046512Ssowmini 		    " handle 0x%0llx", dev_regs->nxge_pciregp,
12056512Ssowmini 		    dev_regs->nxge_pciregh));
12063859Sml29623 			/*
12073859Sml29623 			 * IMP IMP
12083859Sml29623 			 * workaround  for bit swapping bug in HW
12093859Sml29623 			 * which ends up in no-snoop = yes
12103859Sml29623 			 * resulting, in DMA not synched properly
12113859Sml29623 			 */
12123859Sml29623 #if !defined(_BIG_ENDIAN)
12133859Sml29623 		/* workarounds for x86 systems */
12143859Sml29623 		pci_offset = 0x80 + PCIE_DEVCTL;
12153859Sml29623 		pcie_devctl = 0x0;
12163859Sml29623 		pcie_devctl &= PCIE_DEVCTL_ENABLE_NO_SNOOP;
12173859Sml29623 		pcie_devctl |= PCIE_DEVCTL_RO_EN;
12183859Sml29623 		pci_config_put16(dev_regs->nxge_pciregh, pci_offset,
12196512Ssowmini 		    pcie_devctl);
12203859Sml29623 #endif
12213859Sml29623 
12223859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 1, &regsize);
12233859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12246512Ssowmini 		    "nxge_map_regs: pio size 0x%x", regsize));
12253859Sml29623 		/* set up the device mapped register */
12263859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 1,
12276512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_regp), 0, 0,
12286512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_regh);
12293859Sml29623 		if (ddi_status != DDI_SUCCESS) {
12303859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
12316512Ssowmini 			    "ddi_map_regs for Neptune global reg failed"));
12323859Sml29623 			goto nxge_map_regs_fail1;
12333859Sml29623 		}
12343859Sml29623 
12353859Sml29623 		/* set up the msi/msi-x mapped register */
12363859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 2, &regsize);
12373859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12386512Ssowmini 		    "nxge_map_regs: msix size 0x%x", regsize));
12393859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 2,
12406512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_msix_regp), 0, 0,
12416512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_msix_regh);
12423859Sml29623 		if (ddi_status != DDI_SUCCESS) {
12433859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
12446512Ssowmini 			    "ddi_map_regs for msi reg failed"));
12453859Sml29623 			goto nxge_map_regs_fail2;
12463859Sml29623 		}
12473859Sml29623 
12483859Sml29623 		/* set up the vio region mapped register */
12493859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 3, &regsize);
12503859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12516512Ssowmini 		    "nxge_map_regs: vio size 0x%x", regsize));
12523859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 3,
12536512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_vir_regp), 0, 0,
12546512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_vir_regh);
12553859Sml29623 
12563859Sml29623 		if (ddi_status != DDI_SUCCESS) {
12573859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
12586512Ssowmini 			    "ddi_map_regs for nxge vio reg failed"));
12593859Sml29623 			goto nxge_map_regs_fail3;
12603859Sml29623 		}
12613859Sml29623 		nxgep->dev_regs = dev_regs;
12623859Sml29623 
12633859Sml29623 		NPI_PCI_ACC_HANDLE_SET(nxgep, dev_regs->nxge_pciregh);
12643859Sml29623 		NPI_PCI_ADD_HANDLE_SET(nxgep,
12656512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_pciregp);
12663859Sml29623 		NPI_MSI_ACC_HANDLE_SET(nxgep, dev_regs->nxge_msix_regh);
12673859Sml29623 		NPI_MSI_ADD_HANDLE_SET(nxgep,
12686512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_msix_regp);
12693859Sml29623 
12703859Sml29623 		NPI_ACC_HANDLE_SET(nxgep, dev_regs->nxge_regh);
12713859Sml29623 		NPI_ADD_HANDLE_SET(nxgep, (npi_reg_ptr_t)dev_regs->nxge_regp);
12723859Sml29623 
12733859Sml29623 		NPI_REG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_regh);
12743859Sml29623 		NPI_REG_ADD_HANDLE_SET(nxgep,
12756512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_regp);
12763859Sml29623 
12773859Sml29623 		NPI_VREG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_vir_regh);
12783859Sml29623 		NPI_VREG_ADD_HANDLE_SET(nxgep,
12796512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_vir_regp);
12803859Sml29623 
12813859Sml29623 		break;
12823859Sml29623 
12833859Sml29623 	case N2_NIU:
12843859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL, "ddi_map_regs, NIU"));
12853859Sml29623 		/*
12863859Sml29623 		 * Set up the device mapped register (FWARC 2006/556)
12873859Sml29623 		 * (changed back to 1: reg starts at 1!)
12883859Sml29623 		 */
12893859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 1, &regsize);
12903859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
12916512Ssowmini 		    "nxge_map_regs: dev size 0x%x", regsize));
12923859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 1,
12936512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_regp), 0, 0,
12946512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_regh);
12953859Sml29623 
12963859Sml29623 		if (ddi_status != DDI_SUCCESS) {
12973859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
12986512Ssowmini 			    "ddi_map_regs for N2/NIU, global reg failed "));
12993859Sml29623 			goto nxge_map_regs_fail1;
13003859Sml29623 		}
13013859Sml29623 
13026495Sspeer 		/* set up the first vio region mapped register */
13033859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 2, &regsize);
13043859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
13056512Ssowmini 		    "nxge_map_regs: vio (1) size 0x%x", regsize));
13063859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 2,
13076512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_vir_regp), 0, 0,
13086512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_vir_regh);
13093859Sml29623 
13103859Sml29623 		if (ddi_status != DDI_SUCCESS) {
13113859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
13126512Ssowmini 			    "ddi_map_regs for nxge vio reg failed"));
13133859Sml29623 			goto nxge_map_regs_fail2;
13143859Sml29623 		}
13156495Sspeer 		/* set up the second vio region mapped register */
13163859Sml29623 		(void) ddi_dev_regsize(nxgep->dip, 3, &regsize);
13173859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
13186512Ssowmini 		    "nxge_map_regs: vio (3) size 0x%x", regsize));
13193859Sml29623 		ddi_status = ddi_regs_map_setup(nxgep->dip, 3,
13206512Ssowmini 		    (caddr_t *)&(dev_regs->nxge_vir2_regp), 0, 0,
13216512Ssowmini 		    &nxge_dev_reg_acc_attr, &dev_regs->nxge_vir2_regh);
13223859Sml29623 
13233859Sml29623 		if (ddi_status != DDI_SUCCESS) {
13243859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
13256512Ssowmini 			    "ddi_map_regs for nxge vio2 reg failed"));
13263859Sml29623 			goto nxge_map_regs_fail3;
13273859Sml29623 		}
13283859Sml29623 		nxgep->dev_regs = dev_regs;
13293859Sml29623 
13303859Sml29623 		NPI_ACC_HANDLE_SET(nxgep, dev_regs->nxge_regh);
13313859Sml29623 		NPI_ADD_HANDLE_SET(nxgep, (npi_reg_ptr_t)dev_regs->nxge_regp);
13323859Sml29623 
13333859Sml29623 		NPI_REG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_regh);
13343859Sml29623 		NPI_REG_ADD_HANDLE_SET(nxgep,
13356512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_regp);
13363859Sml29623 
13373859Sml29623 		NPI_VREG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_vir_regh);
13383859Sml29623 		NPI_VREG_ADD_HANDLE_SET(nxgep,
13396512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_vir_regp);
13403859Sml29623 
13413859Sml29623 		NPI_V2REG_ACC_HANDLE_SET(nxgep, dev_regs->nxge_vir2_regh);
13423859Sml29623 		NPI_V2REG_ADD_HANDLE_SET(nxgep,
13436512Ssowmini 		    (npi_reg_ptr_t)dev_regs->nxge_vir2_regp);
13443859Sml29623 
13453859Sml29623 		break;
13463859Sml29623 	}
13473859Sml29623 
13483859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "nxge_map_reg: hardware addr 0x%0llx "
13496512Ssowmini 	    " handle 0x%0llx", dev_regs->nxge_regp, dev_regs->nxge_regh));
13503859Sml29623 
13513859Sml29623 	goto nxge_map_regs_exit;
13523859Sml29623 nxge_map_regs_fail3:
13533859Sml29623 	if (dev_regs->nxge_msix_regh) {
13543859Sml29623 		ddi_regs_map_free(&dev_regs->nxge_msix_regh);
13553859Sml29623 	}
13563859Sml29623 	if (dev_regs->nxge_vir_regh) {
13573859Sml29623 		ddi_regs_map_free(&dev_regs->nxge_regh);
13583859Sml29623 	}
13593859Sml29623 nxge_map_regs_fail2:
13603859Sml29623 	if (dev_regs->nxge_regh) {
13613859Sml29623 		ddi_regs_map_free(&dev_regs->nxge_regh);
13623859Sml29623 	}
13633859Sml29623 nxge_map_regs_fail1:
13643859Sml29623 	if (dev_regs->nxge_pciregh) {
13653859Sml29623 		ddi_regs_map_free(&dev_regs->nxge_pciregh);
13663859Sml29623 	}
13673859Sml29623 nxge_map_regs_fail0:
13683859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "Freeing register set memory"));
13693859Sml29623 	kmem_free(dev_regs, sizeof (dev_regs_t));
13703859Sml29623 
13713859Sml29623 nxge_map_regs_exit:
13723859Sml29623 	if (ddi_status != DDI_SUCCESS)
13733859Sml29623 		status |= (NXGE_ERROR | NXGE_DDI_FAILED);
13743859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_map_regs"));
13753859Sml29623 	return (status);
13763859Sml29623 }
13773859Sml29623 
13783859Sml29623 static void
13793859Sml29623 nxge_unmap_regs(p_nxge_t nxgep)
13803859Sml29623 {
13813859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_unmap_regs"));
13826495Sspeer 
13836495Sspeer 	if (isLDOMguest(nxgep)) {
13846495Sspeer 		nxge_guest_regs_map_free(nxgep);
13856495Sspeer 		return;
13866495Sspeer 	}
13876495Sspeer 
13883859Sml29623 	if (nxgep->dev_regs) {
13893859Sml29623 		if (nxgep->dev_regs->nxge_pciregh) {
13903859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
13916512Ssowmini 			    "==> nxge_unmap_regs: bus"));
13923859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_pciregh);
13933859Sml29623 			nxgep->dev_regs->nxge_pciregh = NULL;
13943859Sml29623 		}
13953859Sml29623 		if (nxgep->dev_regs->nxge_regh) {
13963859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
13976512Ssowmini 			    "==> nxge_unmap_regs: device registers"));
13983859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_regh);
13993859Sml29623 			nxgep->dev_regs->nxge_regh = NULL;
14003859Sml29623 		}
14013859Sml29623 		if (nxgep->dev_regs->nxge_msix_regh) {
14023859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
14036512Ssowmini 			    "==> nxge_unmap_regs: device interrupts"));
14043859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_msix_regh);
14053859Sml29623 			nxgep->dev_regs->nxge_msix_regh = NULL;
14063859Sml29623 		}
14073859Sml29623 		if (nxgep->dev_regs->nxge_vir_regh) {
14083859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
14096512Ssowmini 			    "==> nxge_unmap_regs: vio region"));
14103859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_vir_regh);
14113859Sml29623 			nxgep->dev_regs->nxge_vir_regh = NULL;
14123859Sml29623 		}
14133859Sml29623 		if (nxgep->dev_regs->nxge_vir2_regh) {
14143859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
14156512Ssowmini 			    "==> nxge_unmap_regs: vio2 region"));
14163859Sml29623 			ddi_regs_map_free(&nxgep->dev_regs->nxge_vir2_regh);
14173859Sml29623 			nxgep->dev_regs->nxge_vir2_regh = NULL;
14183859Sml29623 		}
14193859Sml29623 
14203859Sml29623 		kmem_free(nxgep->dev_regs, sizeof (dev_regs_t));
14213859Sml29623 		nxgep->dev_regs = NULL;
14223859Sml29623 	}
14233859Sml29623 
14243859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_unmap_regs"));
14253859Sml29623 }
14263859Sml29623 
14273859Sml29623 static nxge_status_t
14283859Sml29623 nxge_setup_mutexes(p_nxge_t nxgep)
14293859Sml29623 {
14303859Sml29623 	int ddi_status = DDI_SUCCESS;
14313859Sml29623 	nxge_status_t status = NXGE_OK;
14323859Sml29623 	nxge_classify_t *classify_ptr;
14333859Sml29623 	int partition;
14343859Sml29623 
14353859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_setup_mutexes"));
14363859Sml29623 
14373859Sml29623 	/*
14383859Sml29623 	 * Get the interrupt cookie so the mutexes can be
14393859Sml29623 	 * Initialized.
14403859Sml29623 	 */
14416495Sspeer 	if (isLDOMguest(nxgep)) {
14426495Sspeer 		nxgep->interrupt_cookie = 0;
14436495Sspeer 	} else {
14446495Sspeer 		ddi_status = ddi_get_iblock_cookie(nxgep->dip, 0,
14456495Sspeer 		    &nxgep->interrupt_cookie);
14466495Sspeer 
14476495Sspeer 		if (ddi_status != DDI_SUCCESS) {
14486495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
14496495Sspeer 			    "<== nxge_setup_mutexes: failed 0x%x",
14506495Sspeer 			    ddi_status));
14516495Sspeer 			goto nxge_setup_mutexes_exit;
14526495Sspeer 		}
14533859Sml29623 	}
14543859Sml29623 
14554693Stm144005 	cv_init(&nxgep->poll_cv, NULL, CV_DRIVER, NULL);
14564693Stm144005 	MUTEX_INIT(&nxgep->poll_lock, NULL,
14574693Stm144005 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14584693Stm144005 
14593859Sml29623 	/*
14604693Stm144005 	 * Initialize mutexes for this device.
14613859Sml29623 	 */
14623859Sml29623 	MUTEX_INIT(nxgep->genlock, NULL,
14636512Ssowmini 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14643859Sml29623 	MUTEX_INIT(&nxgep->ouraddr_lock, NULL,
14656512Ssowmini 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14663859Sml29623 	MUTEX_INIT(&nxgep->mif_lock, NULL,
14676512Ssowmini 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14686495Sspeer 	MUTEX_INIT(&nxgep->group_lock, NULL,
14696495Sspeer 	    MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14703859Sml29623 	RW_INIT(&nxgep->filter_lock, NULL,
14716512Ssowmini 	    RW_DRIVER, (void *)nxgep->interrupt_cookie);
14723859Sml29623 
14733859Sml29623 	classify_ptr = &nxgep->classifier;
14743859Sml29623 		/*
14753859Sml29623 		 * FFLP Mutexes are never used in interrupt context
14763859Sml29623 		 * as fflp operation can take very long time to
14773859Sml29623 		 * complete and hence not suitable to invoke from interrupt
14783859Sml29623 		 * handlers.
14793859Sml29623 		 */
14803859Sml29623 	MUTEX_INIT(&classify_ptr->tcam_lock, NULL,
14814732Sdavemq 	    NXGE_MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14824977Sraghus 	if (NXGE_IS_VALID_NEPTUNE_TYPE(nxgep)) {
14833859Sml29623 		MUTEX_INIT(&classify_ptr->fcram_lock, NULL,
14844732Sdavemq 		    NXGE_MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14853859Sml29623 		for (partition = 0; partition < MAX_PARTITION; partition++) {
14863859Sml29623 			MUTEX_INIT(&classify_ptr->hash_lock[partition], NULL,
14873859Sml29623 			    NXGE_MUTEX_DRIVER, (void *)nxgep->interrupt_cookie);
14883859Sml29623 		}
14893859Sml29623 	}
14903859Sml29623 
14913859Sml29623 nxge_setup_mutexes_exit:
14923859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
14934732Sdavemq 	    "<== nxge_setup_mutexes status = %x", status));
14943859Sml29623 
14953859Sml29623 	if (ddi_status != DDI_SUCCESS)
14963859Sml29623 		status |= (NXGE_ERROR | NXGE_DDI_FAILED);
14973859Sml29623 
14983859Sml29623 	return (status);
14993859Sml29623 }
15003859Sml29623 
15013859Sml29623 static void
15023859Sml29623 nxge_destroy_mutexes(p_nxge_t nxgep)
15033859Sml29623 {
15043859Sml29623 	int partition;
15053859Sml29623 	nxge_classify_t *classify_ptr;
15063859Sml29623 
15073859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_destroy_mutexes"));
15083859Sml29623 	RW_DESTROY(&nxgep->filter_lock);
15096495Sspeer 	MUTEX_DESTROY(&nxgep->group_lock);
15103859Sml29623 	MUTEX_DESTROY(&nxgep->mif_lock);
15113859Sml29623 	MUTEX_DESTROY(&nxgep->ouraddr_lock);
15123859Sml29623 	MUTEX_DESTROY(nxgep->genlock);
15133859Sml29623 
15143859Sml29623 	classify_ptr = &nxgep->classifier;
15153859Sml29623 	MUTEX_DESTROY(&classify_ptr->tcam_lock);
15163859Sml29623 
15174693Stm144005 	/* Destroy all polling resources. */
15184693Stm144005 	MUTEX_DESTROY(&nxgep->poll_lock);
15194693Stm144005 	cv_destroy(&nxgep->poll_cv);
15204693Stm144005 
15214693Stm144005 	/* free data structures, based on HW type */
15224977Sraghus 	if (NXGE_IS_VALID_NEPTUNE_TYPE(nxgep)) {
15233859Sml29623 		MUTEX_DESTROY(&classify_ptr->fcram_lock);
15243859Sml29623 		for (partition = 0; partition < MAX_PARTITION; partition++) {
15253859Sml29623 			MUTEX_DESTROY(&classify_ptr->hash_lock[partition]);
15263859Sml29623 		}
15273859Sml29623 	}
15283859Sml29623 
15293859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_destroy_mutexes"));
15303859Sml29623 }
15313859Sml29623 
15323859Sml29623 nxge_status_t
15333859Sml29623 nxge_init(p_nxge_t nxgep)
15343859Sml29623 {
15356495Sspeer 	nxge_status_t status = NXGE_OK;
15363859Sml29623 
15373859Sml29623 	NXGE_DEBUG_MSG((nxgep, STR_CTL, "==> nxge_init"));
15383859Sml29623 
15393859Sml29623 	if (nxgep->drv_state & STATE_HW_INITIALIZED) {
15403859Sml29623 		return (status);
15413859Sml29623 	}
15423859Sml29623 
15433859Sml29623 	/*
15443859Sml29623 	 * Allocate system memory for the receive/transmit buffer blocks
15453859Sml29623 	 * and receive/transmit descriptor rings.
15463859Sml29623 	 */
15473859Sml29623 	status = nxge_alloc_mem_pool(nxgep);
15483859Sml29623 	if (status != NXGE_OK) {
15493859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "alloc mem failed\n"));
15503859Sml29623 		goto nxge_init_fail1;
15513859Sml29623 	}
15523859Sml29623 
15536495Sspeer 	if (!isLDOMguest(nxgep)) {
15546495Sspeer 		/*
15556495Sspeer 		 * Initialize and enable the TXC registers.
15566495Sspeer 		 * (Globally enable the Tx controller,
15576495Sspeer 		 *  enable the port, configure the dma channel bitmap,
15586495Sspeer 		 *  configure the max burst size).
15596495Sspeer 		 */
15606495Sspeer 		status = nxge_txc_init(nxgep);
15616495Sspeer 		if (status != NXGE_OK) {
15626495Sspeer 			NXGE_ERROR_MSG((nxgep,
15636495Sspeer 			    NXGE_ERR_CTL, "init txc failed\n"));
15646495Sspeer 			goto nxge_init_fail2;
15656495Sspeer 		}
15663859Sml29623 	}
15673859Sml29623 
15683859Sml29623 	/*
15693859Sml29623 	 * Initialize and enable TXDMA channels.
15703859Sml29623 	 */
15713859Sml29623 	status = nxge_init_txdma_channels(nxgep);
15723859Sml29623 	if (status != NXGE_OK) {
15733859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init txdma failed\n"));
15743859Sml29623 		goto nxge_init_fail3;
15753859Sml29623 	}
15763859Sml29623 
15773859Sml29623 	/*
15783859Sml29623 	 * Initialize and enable RXDMA channels.
15793859Sml29623 	 */
15803859Sml29623 	status = nxge_init_rxdma_channels(nxgep);
15813859Sml29623 	if (status != NXGE_OK) {
15823859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init rxdma failed\n"));
15833859Sml29623 		goto nxge_init_fail4;
15843859Sml29623 	}
15853859Sml29623 
15863859Sml29623 	/*
15876495Sspeer 	 * The guest domain is now done.
15886495Sspeer 	 */
15896495Sspeer 	if (isLDOMguest(nxgep)) {
15906495Sspeer 		nxgep->drv_state |= STATE_HW_INITIALIZED;
15916495Sspeer 		goto nxge_init_exit;
15926495Sspeer 	}
15936495Sspeer 
15946495Sspeer 	/*
15953859Sml29623 	 * Initialize TCAM and FCRAM (Neptune).
15963859Sml29623 	 */
15973859Sml29623 	status = nxge_classify_init(nxgep);
15983859Sml29623 	if (status != NXGE_OK) {
15993859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init classify failed\n"));
16003859Sml29623 		goto nxge_init_fail5;
16013859Sml29623 	}
16023859Sml29623 
16033859Sml29623 	/*
16043859Sml29623 	 * Initialize ZCP
16053859Sml29623 	 */
16063859Sml29623 	status = nxge_zcp_init(nxgep);
16073859Sml29623 	if (status != NXGE_OK) {
16083859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init ZCP failed\n"));
16093859Sml29623 		goto nxge_init_fail5;
16103859Sml29623 	}
16113859Sml29623 
16123859Sml29623 	/*
16133859Sml29623 	 * Initialize IPP.
16143859Sml29623 	 */
16153859Sml29623 	status = nxge_ipp_init(nxgep);
16163859Sml29623 	if (status != NXGE_OK) {
16173859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init IPP failed\n"));
16183859Sml29623 		goto nxge_init_fail5;
16193859Sml29623 	}
16203859Sml29623 
16213859Sml29623 	/*
16223859Sml29623 	 * Initialize the MAC block.
16233859Sml29623 	 */
16243859Sml29623 	status = nxge_mac_init(nxgep);
16253859Sml29623 	if (status != NXGE_OK) {
16263859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "init MAC failed\n"));
16273859Sml29623 		goto nxge_init_fail5;
16283859Sml29623 	}
16293859Sml29623 
16306495Sspeer 	nxge_intrs_enable(nxgep); /* XXX What changes do I need to make here? */
16313859Sml29623 
16323859Sml29623 	/*
16333859Sml29623 	 * Enable hardware interrupts.
16343859Sml29623 	 */
16353859Sml29623 	nxge_intr_hw_enable(nxgep);
16363859Sml29623 	nxgep->drv_state |= STATE_HW_INITIALIZED;
16373859Sml29623 
16383859Sml29623 	goto nxge_init_exit;
16393859Sml29623 
16403859Sml29623 nxge_init_fail5:
16413859Sml29623 	nxge_uninit_rxdma_channels(nxgep);
16423859Sml29623 nxge_init_fail4:
16433859Sml29623 	nxge_uninit_txdma_channels(nxgep);
16443859Sml29623 nxge_init_fail3:
16456495Sspeer 	if (!isLDOMguest(nxgep)) {
16466495Sspeer 		(void) nxge_txc_uninit(nxgep);
16476495Sspeer 	}
16483859Sml29623 nxge_init_fail2:
16493859Sml29623 	nxge_free_mem_pool(nxgep);
16503859Sml29623 nxge_init_fail1:
16513859Sml29623 	NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
16526512Ssowmini 	    "<== nxge_init status (failed) = 0x%08x", status));
16533859Sml29623 	return (status);
16543859Sml29623 
16553859Sml29623 nxge_init_exit:
16563859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_init status = 0x%08x",
16576512Ssowmini 	    status));
16583859Sml29623 	return (status);
16593859Sml29623 }
16603859Sml29623 
16613859Sml29623 
16623859Sml29623 timeout_id_t
16633859Sml29623 nxge_start_timer(p_nxge_t nxgep, fptrv_t func, int msec)
16643859Sml29623 {
16656512Ssowmini 	if ((nxgep->suspended == 0) || (nxgep->suspended == DDI_RESUME)) {
16663859Sml29623 		return (timeout(func, (caddr_t)nxgep,
16676512Ssowmini 		    drv_usectohz(1000 * msec)));
16683859Sml29623 	}
16693859Sml29623 	return (NULL);
16703859Sml29623 }
16713859Sml29623 
16723859Sml29623 /*ARGSUSED*/
16733859Sml29623 void
16743859Sml29623 nxge_stop_timer(p_nxge_t nxgep, timeout_id_t timerid)
16753859Sml29623 {
16763859Sml29623 	if (timerid) {
16773859Sml29623 		(void) untimeout(timerid);
16783859Sml29623 	}
16793859Sml29623 }
16803859Sml29623 
16813859Sml29623 void
16823859Sml29623 nxge_uninit(p_nxge_t nxgep)
16833859Sml29623 {
16843859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_uninit"));
16853859Sml29623 
16863859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
16873859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
16886512Ssowmini 		    "==> nxge_uninit: not initialized"));
16893859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
16906512Ssowmini 		    "<== nxge_uninit"));
16913859Sml29623 		return;
16923859Sml29623 	}
16933859Sml29623 
16943859Sml29623 	/* stop timer */
16953859Sml29623 	if (nxgep->nxge_timerid) {
16963859Sml29623 		nxge_stop_timer(nxgep, nxgep->nxge_timerid);
16973859Sml29623 		nxgep->nxge_timerid = 0;
16983859Sml29623 	}
16993859Sml29623 
17003859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_STOP);
17013859Sml29623 	(void) nxge_intr_hw_disable(nxgep);
17023859Sml29623 
17033859Sml29623 	/*
17043859Sml29623 	 * Reset the receive MAC side.
17053859Sml29623 	 */
17063859Sml29623 	(void) nxge_rx_mac_disable(nxgep);
17073859Sml29623 
17083859Sml29623 	/* Disable and soft reset the IPP */
17096495Sspeer 	if (!isLDOMguest(nxgep))
17106495Sspeer 		(void) nxge_ipp_disable(nxgep);
17113859Sml29623 
17123859Sml29623 	/* Free classification resources */
17133859Sml29623 	(void) nxge_classify_uninit(nxgep);
17143859Sml29623 
17153859Sml29623 	/*
17163859Sml29623 	 * Reset the transmit/receive DMA side.
17173859Sml29623 	 */
17183859Sml29623 	(void) nxge_txdma_hw_mode(nxgep, NXGE_DMA_STOP);
17193859Sml29623 	(void) nxge_rxdma_hw_mode(nxgep, NXGE_DMA_STOP);
17203859Sml29623 
17213859Sml29623 	nxge_uninit_txdma_channels(nxgep);
17223859Sml29623 	nxge_uninit_rxdma_channels(nxgep);
17233859Sml29623 
17243859Sml29623 	/*
17253859Sml29623 	 * Reset the transmit MAC side.
17263859Sml29623 	 */
17273859Sml29623 	(void) nxge_tx_mac_disable(nxgep);
17283859Sml29623 
17293859Sml29623 	nxge_free_mem_pool(nxgep);
17303859Sml29623 
17316705Sml29623 	/*
17326705Sml29623 	 * Start the timer if the reset flag is not set.
17336705Sml29623 	 * If this reset flag is set, the link monitor
17346705Sml29623 	 * will not be started in order to stop furthur bus
17356705Sml29623 	 * activities coming from this interface.
17366705Sml29623 	 * The driver will start the monitor function
17376705Sml29623 	 * if the interface was initialized again later.
17386705Sml29623 	 */
17396705Sml29623 	if (!nxge_peu_reset_enable) {
17406705Sml29623 		(void) nxge_link_monitor(nxgep, LINK_MONITOR_START);
17416705Sml29623 	}
17423859Sml29623 
17433859Sml29623 	nxgep->drv_state &= ~STATE_HW_INITIALIZED;
17443859Sml29623 
17453859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_uninit: "
17466512Ssowmini 	    "nxge_mblks_pending %d", nxge_mblks_pending));
17473859Sml29623 }
17483859Sml29623 
17493859Sml29623 void
17503859Sml29623 nxge_get64(p_nxge_t nxgep, p_mblk_t mp)
17513859Sml29623 {
17523859Sml29623 	uint64_t	reg;
17533859Sml29623 	uint64_t	regdata;
17543859Sml29623 	int		i, retry;
17553859Sml29623 
17563859Sml29623 	bcopy((char *)mp->b_rptr, (char *)&reg, sizeof (uint64_t));
17573859Sml29623 	regdata = 0;
17583859Sml29623 	retry = 1;
17593859Sml29623 
17603859Sml29623 	for (i = 0; i < retry; i++) {
17613859Sml29623 		NXGE_REG_RD64(nxgep->npi_handle, reg, &regdata);
17623859Sml29623 	}
17633859Sml29623 	bcopy((char *)&regdata, (char *)mp->b_rptr, sizeof (uint64_t));
17643859Sml29623 }
17653859Sml29623 
17663859Sml29623 void
17673859Sml29623 nxge_put64(p_nxge_t nxgep, p_mblk_t mp)
17683859Sml29623 {
17693859Sml29623 	uint64_t	reg;
17703859Sml29623 	uint64_t	buf[2];
17713859Sml29623 
17723859Sml29623 	bcopy((char *)mp->b_rptr, (char *)&buf[0], 2 * sizeof (uint64_t));
17733859Sml29623 	reg = buf[0];
17743859Sml29623 
17753859Sml29623 	NXGE_NPI_PIO_WRITE64(nxgep->npi_handle, reg, buf[1]);
17763859Sml29623 }
17773859Sml29623 
17783859Sml29623 
17793859Sml29623 nxge_os_mutex_t nxgedebuglock;
17803859Sml29623 int nxge_debug_init = 0;
17813859Sml29623 
17823859Sml29623 /*ARGSUSED*/
17833859Sml29623 /*VARARGS*/
17843859Sml29623 void
17853859Sml29623 nxge_debug_msg(p_nxge_t nxgep, uint64_t level, char *fmt, ...)
17863859Sml29623 {
17873859Sml29623 	char msg_buffer[1048];
17883859Sml29623 	char prefix_buffer[32];
17893859Sml29623 	int instance;
17903859Sml29623 	uint64_t debug_level;
17913859Sml29623 	int cmn_level = CE_CONT;
17923859Sml29623 	va_list ap;
17933859Sml29623 
17946495Sspeer 	if (nxgep && nxgep->nxge_debug_level != nxge_debug_level) {
17956495Sspeer 		/* In case a developer has changed nxge_debug_level. */
17966495Sspeer 		if (nxgep->nxge_debug_level != nxge_debug_level)
17976495Sspeer 			nxgep->nxge_debug_level = nxge_debug_level;
17986495Sspeer 	}
17996495Sspeer 
18003859Sml29623 	debug_level = (nxgep == NULL) ? nxge_debug_level :
18016512Ssowmini 	    nxgep->nxge_debug_level;
18023859Sml29623 
18033859Sml29623 	if ((level & debug_level) ||
18046512Ssowmini 	    (level == NXGE_NOTE) ||
18056512Ssowmini 	    (level == NXGE_ERR_CTL)) {
18063859Sml29623 		/* do the msg processing */
18073859Sml29623 		if (nxge_debug_init == 0) {
18083859Sml29623 			MUTEX_INIT(&nxgedebuglock, NULL, MUTEX_DRIVER, NULL);
18093859Sml29623 			nxge_debug_init = 1;
18103859Sml29623 		}
18113859Sml29623 
18123859Sml29623 		MUTEX_ENTER(&nxgedebuglock);
18133859Sml29623 
18143859Sml29623 		if ((level & NXGE_NOTE)) {
18153859Sml29623 			cmn_level = CE_NOTE;
18163859Sml29623 		}
18173859Sml29623 
18183859Sml29623 		if (level & NXGE_ERR_CTL) {
18193859Sml29623 			cmn_level = CE_WARN;
18203859Sml29623 		}
18213859Sml29623 
18223859Sml29623 		va_start(ap, fmt);
18233859Sml29623 		(void) vsprintf(msg_buffer, fmt, ap);
18243859Sml29623 		va_end(ap);
18253859Sml29623 		if (nxgep == NULL) {
18263859Sml29623 			instance = -1;
18273859Sml29623 			(void) sprintf(prefix_buffer, "%s :", "nxge");
18283859Sml29623 		} else {
18293859Sml29623 			instance = nxgep->instance;
18303859Sml29623 			(void) sprintf(prefix_buffer,
18316512Ssowmini 			    "%s%d :", "nxge", instance);
18323859Sml29623 		}
18333859Sml29623 
18343859Sml29623 		MUTEX_EXIT(&nxgedebuglock);
18353859Sml29623 		cmn_err(cmn_level, "!%s %s\n",
18366512Ssowmini 		    prefix_buffer, msg_buffer);
18373859Sml29623 
18383859Sml29623 	}
18393859Sml29623 }
18403859Sml29623 
18413859Sml29623 char *
18423859Sml29623 nxge_dump_packet(char *addr, int size)
18433859Sml29623 {
18443859Sml29623 	uchar_t *ap = (uchar_t *)addr;
18453859Sml29623 	int i;
18463859Sml29623 	static char etherbuf[1024];
18473859Sml29623 	char *cp = etherbuf;
18483859Sml29623 	char digits[] = "0123456789abcdef";
18493859Sml29623 
18503859Sml29623 	if (!size)
18513859Sml29623 		size = 60;
18523859Sml29623 
18533859Sml29623 	if (size > MAX_DUMP_SZ) {
18543859Sml29623 		/* Dump the leading bytes */
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 		for (i = 0; i < 20; i++)
18623859Sml29623 			*cp++ = '.';
18633859Sml29623 		/* Dump the last MAX_DUMP_SZ/2 bytes */
18643859Sml29623 		ap = (uchar_t *)(addr + (size - MAX_DUMP_SZ/2));
18653859Sml29623 		for (i = 0; i < MAX_DUMP_SZ/2; i++) {
18663859Sml29623 			if (*ap > 0x0f)
18673859Sml29623 				*cp++ = digits[*ap >> 4];
18683859Sml29623 			*cp++ = digits[*ap++ & 0xf];
18693859Sml29623 			*cp++ = ':';
18703859Sml29623 		}
18713859Sml29623 	} else {
18723859Sml29623 		for (i = 0; i < size; i++) {
18733859Sml29623 			if (*ap > 0x0f)
18743859Sml29623 				*cp++ = digits[*ap >> 4];
18753859Sml29623 			*cp++ = digits[*ap++ & 0xf];
18763859Sml29623 			*cp++ = ':';
18773859Sml29623 		}
18783859Sml29623 	}
18793859Sml29623 	*--cp = 0;
18803859Sml29623 	return (etherbuf);
18813859Sml29623 }
18823859Sml29623 
18833859Sml29623 #ifdef	NXGE_DEBUG
18843859Sml29623 static void
18853859Sml29623 nxge_test_map_regs(p_nxge_t nxgep)
18863859Sml29623 {
18873859Sml29623 	ddi_acc_handle_t cfg_handle;
18883859Sml29623 	p_pci_cfg_t	cfg_ptr;
18893859Sml29623 	ddi_acc_handle_t dev_handle;
18903859Sml29623 	char		*dev_ptr;
18913859Sml29623 	ddi_acc_handle_t pci_config_handle;
18923859Sml29623 	uint32_t	regval;
18933859Sml29623 	int		i;
18943859Sml29623 
18953859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_test_map_regs"));
18963859Sml29623 
18973859Sml29623 	dev_handle = nxgep->dev_regs->nxge_regh;
18983859Sml29623 	dev_ptr = (char *)nxgep->dev_regs->nxge_regp;
18993859Sml29623 
19004977Sraghus 	if (NXGE_IS_VALID_NEPTUNE_TYPE(nxgep)) {
19013859Sml29623 		cfg_handle = nxgep->dev_regs->nxge_pciregh;
19023859Sml29623 		cfg_ptr = (void *)nxgep->dev_regs->nxge_pciregp;
19033859Sml29623 
19043859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19054732Sdavemq 		    "Neptune PCI regp cfg_ptr 0x%llx", (char *)cfg_ptr));
19063859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19074732Sdavemq 		    "Neptune PCI cfg_ptr vendor id ptr 0x%llx",
19084732Sdavemq 		    &cfg_ptr->vendorid));
19093859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19104732Sdavemq 		    "\tvendorid 0x%x devid 0x%x",
19114732Sdavemq 		    NXGE_PIO_READ16(cfg_handle, &cfg_ptr->vendorid, 0),
19124732Sdavemq 		    NXGE_PIO_READ16(cfg_handle, &cfg_ptr->devid,    0)));
19133859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19144732Sdavemq 		    "PCI BAR: base 0x%x base14 0x%x base 18 0x%x "
19154732Sdavemq 		    "bar1c 0x%x",
19164732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base,   0),
19174732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base14, 0),
19184732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base18, 0),
19194732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base1c, 0)));
19203859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19214732Sdavemq 		    "\nNeptune PCI BAR: base20 0x%x base24 0x%x "
19224732Sdavemq 		    "base 28 0x%x bar2c 0x%x\n",
19234732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base20, 0),
19244732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base24, 0),
19254732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base28, 0),
19264732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base2c, 0)));
19273859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19284732Sdavemq 		    "\nNeptune PCI BAR: base30 0x%x\n",
19294732Sdavemq 		    NXGE_PIO_READ32(cfg_handle, &cfg_ptr->base30, 0)));
19303859Sml29623 
19313859Sml29623 		cfg_handle = nxgep->dev_regs->nxge_pciregh;
19323859Sml29623 		cfg_ptr = (void *)nxgep->dev_regs->nxge_pciregp;
19333859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19344732Sdavemq 		    "first  0x%llx second 0x%llx third 0x%llx "
19354732Sdavemq 		    "last 0x%llx ",
19364732Sdavemq 		    NXGE_PIO_READ64(dev_handle,
19374732Sdavemq 		    (uint64_t *)(dev_ptr + 0),  0),
19384732Sdavemq 		    NXGE_PIO_READ64(dev_handle,
19394732Sdavemq 		    (uint64_t *)(dev_ptr + 8),  0),
19404732Sdavemq 		    NXGE_PIO_READ64(dev_handle,
19414732Sdavemq 		    (uint64_t *)(dev_ptr + 16), 0),
19424732Sdavemq 		    NXGE_PIO_READ64(cfg_handle,
19434732Sdavemq 		    (uint64_t *)(dev_ptr + 24), 0)));
19443859Sml29623 	}
19453859Sml29623 }
19463859Sml29623 
19473859Sml29623 #endif
19483859Sml29623 
19493859Sml29623 static void
19503859Sml29623 nxge_suspend(p_nxge_t nxgep)
19513859Sml29623 {
19523859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_suspend"));
19533859Sml29623 
19543859Sml29623 	nxge_intrs_disable(nxgep);
19553859Sml29623 	nxge_destroy_dev(nxgep);
19563859Sml29623 
19573859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_suspend"));
19583859Sml29623 }
19593859Sml29623 
19603859Sml29623 static nxge_status_t
19613859Sml29623 nxge_resume(p_nxge_t nxgep)
19623859Sml29623 {
19633859Sml29623 	nxge_status_t status = NXGE_OK;
19643859Sml29623 
19653859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_resume"));
19664587Sjoycey 
19673859Sml29623 	nxgep->suspended = DDI_RESUME;
19684587Sjoycey 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_START);
19694587Sjoycey 	(void) nxge_rxdma_hw_mode(nxgep, NXGE_DMA_START);
19704587Sjoycey 	(void) nxge_txdma_hw_mode(nxgep, NXGE_DMA_START);
19714587Sjoycey 	(void) nxge_rx_mac_enable(nxgep);
19724587Sjoycey 	(void) nxge_tx_mac_enable(nxgep);
19734587Sjoycey 	nxge_intrs_enable(nxgep);
19743859Sml29623 	nxgep->suspended = 0;
19753859Sml29623 
19763859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
19776512Ssowmini 	    "<== nxge_resume status = 0x%x", status));
19783859Sml29623 	return (status);
19793859Sml29623 }
19803859Sml29623 
19813859Sml29623 static nxge_status_t
19823859Sml29623 nxge_setup_dev(p_nxge_t nxgep)
19833859Sml29623 {
19843859Sml29623 	nxge_status_t	status = NXGE_OK;
19853859Sml29623 
19863859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_setup_dev port %d",
19874732Sdavemq 	    nxgep->mac.portnum));
19883859Sml29623 
19893859Sml29623 	status = nxge_link_init(nxgep);
19903859Sml29623 
19913859Sml29623 	if (fm_check_acc_handle(nxgep->dev_regs->nxge_regh) != DDI_FM_OK) {
19923859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
19936512Ssowmini 		    "port%d Bad register acc handle", nxgep->mac.portnum));
19943859Sml29623 		status = NXGE_ERROR;
19953859Sml29623 	}
19963859Sml29623 
19973859Sml29623 	if (status != NXGE_OK) {
19983859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
19996512Ssowmini 		    " nxge_setup_dev status "
20006512Ssowmini 		    "(xcvr init 0x%08x)", status));
20013859Sml29623 		goto nxge_setup_dev_exit;
20023859Sml29623 	}
20033859Sml29623 
20043859Sml29623 nxge_setup_dev_exit:
20053859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
20066512Ssowmini 	    "<== nxge_setup_dev port %d status = 0x%08x",
20076512Ssowmini 	    nxgep->mac.portnum, status));
20083859Sml29623 
20093859Sml29623 	return (status);
20103859Sml29623 }
20113859Sml29623 
20123859Sml29623 static void
20133859Sml29623 nxge_destroy_dev(p_nxge_t nxgep)
20143859Sml29623 {
20153859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_destroy_dev"));
20163859Sml29623 
20173859Sml29623 	(void) nxge_link_monitor(nxgep, LINK_MONITOR_STOP);
20183859Sml29623 
20193859Sml29623 	(void) nxge_hw_stop(nxgep);
20203859Sml29623 
20213859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_destroy_dev"));
20223859Sml29623 }
20233859Sml29623 
20243859Sml29623 static nxge_status_t
20253859Sml29623 nxge_setup_system_dma_pages(p_nxge_t nxgep)
20263859Sml29623 {
20273859Sml29623 	int 			ddi_status = DDI_SUCCESS;
20283859Sml29623 	uint_t 			count;
20293859Sml29623 	ddi_dma_cookie_t 	cookie;
20303859Sml29623 	uint_t 			iommu_pagesize;
20313859Sml29623 	nxge_status_t		status = NXGE_OK;
20323859Sml29623 
20336495Sspeer 	NXGE_ERROR_MSG((nxgep, DDI_CTL, "==> nxge_setup_system_dma_pages"));
20343859Sml29623 	nxgep->sys_page_sz = ddi_ptob(nxgep->dip, (ulong_t)1);
20353859Sml29623 	if (nxgep->niu_type != N2_NIU) {
20363859Sml29623 		iommu_pagesize = dvma_pagesize(nxgep->dip);
20373859Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
20386512Ssowmini 		    " nxge_setup_system_dma_pages: page %d (ddi_ptob %d) "
20396512Ssowmini 		    " default_block_size %d iommu_pagesize %d",
20406512Ssowmini 		    nxgep->sys_page_sz,
20416512Ssowmini 		    ddi_ptob(nxgep->dip, (ulong_t)1),
20426512Ssowmini 		    nxgep->rx_default_block_size,
20436512Ssowmini 		    iommu_pagesize));
20443859Sml29623 
20453859Sml29623 		if (iommu_pagesize != 0) {
20463859Sml29623 			if (nxgep->sys_page_sz == iommu_pagesize) {
20473859Sml29623 				if (iommu_pagesize > 0x4000)
20483859Sml29623 					nxgep->sys_page_sz = 0x4000;
20493859Sml29623 			} else {
20503859Sml29623 				if (nxgep->sys_page_sz > iommu_pagesize)
20513859Sml29623 					nxgep->sys_page_sz = iommu_pagesize;
20523859Sml29623 			}
20533859Sml29623 		}
20543859Sml29623 	}
20553859Sml29623 	nxgep->sys_page_mask = ~(nxgep->sys_page_sz - 1);
20563859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
20576512Ssowmini 	    "==> nxge_setup_system_dma_pages: page %d (ddi_ptob %d) "
20586512Ssowmini 	    "default_block_size %d page mask %d",
20596512Ssowmini 	    nxgep->sys_page_sz,
20606512Ssowmini 	    ddi_ptob(nxgep->dip, (ulong_t)1),
20616512Ssowmini 	    nxgep->rx_default_block_size,
20626512Ssowmini 	    nxgep->sys_page_mask));
20633859Sml29623 
20643859Sml29623 
20653859Sml29623 	switch (nxgep->sys_page_sz) {
20663859Sml29623 	default:
20673859Sml29623 		nxgep->sys_page_sz = 0x1000;
20683859Sml29623 		nxgep->sys_page_mask = ~(nxgep->sys_page_sz - 1);
20693859Sml29623 		nxgep->rx_default_block_size = 0x1000;
20703859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_4K;
20713859Sml29623 		break;
20723859Sml29623 	case 0x1000:
20733859Sml29623 		nxgep->rx_default_block_size = 0x1000;
20743859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_4K;
20753859Sml29623 		break;
20763859Sml29623 	case 0x2000:
20773859Sml29623 		nxgep->rx_default_block_size = 0x2000;
20783859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_8K;
20793859Sml29623 		break;
20803859Sml29623 	case 0x4000:
20813859Sml29623 		nxgep->rx_default_block_size = 0x4000;
20823859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_16K;
20833859Sml29623 		break;
20843859Sml29623 	case 0x8000:
20853859Sml29623 		nxgep->rx_default_block_size = 0x8000;
20863859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_32K;
20873859Sml29623 		break;
20883859Sml29623 	}
20893859Sml29623 
20903859Sml29623 #ifndef USE_RX_BIG_BUF
20913859Sml29623 	nxge_rx_dma_attr.dma_attr_align = nxgep->sys_page_sz;
20923859Sml29623 #else
20933859Sml29623 		nxgep->rx_default_block_size = 0x2000;
20943859Sml29623 		nxgep->rx_bksize_code = RBR_BKSIZE_8K;
20953859Sml29623 #endif
20963859Sml29623 	/*
20973859Sml29623 	 * Get the system DMA burst size.
20983859Sml29623 	 */
20993859Sml29623 	ddi_status = ddi_dma_alloc_handle(nxgep->dip, &nxge_tx_dma_attr,
21006512Ssowmini 	    DDI_DMA_DONTWAIT, 0,
21016512Ssowmini 	    &nxgep->dmasparehandle);
21023859Sml29623 	if (ddi_status != DDI_SUCCESS) {
21033859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
21046512Ssowmini 		    "ddi_dma_alloc_handle: failed "
21056512Ssowmini 		    " status 0x%x", ddi_status));
21063859Sml29623 		goto nxge_get_soft_properties_exit;
21073859Sml29623 	}
21083859Sml29623 
21093859Sml29623 	ddi_status = ddi_dma_addr_bind_handle(nxgep->dmasparehandle, NULL,
21106512Ssowmini 	    (caddr_t)nxgep->dmasparehandle,
21116512Ssowmini 	    sizeof (nxgep->dmasparehandle),
21126512Ssowmini 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
21136512Ssowmini 	    DDI_DMA_DONTWAIT, 0,
21146512Ssowmini 	    &cookie, &count);
21153859Sml29623 	if (ddi_status != DDI_DMA_MAPPED) {
21163859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
21176512Ssowmini 		    "Binding spare handle to find system"
21186512Ssowmini 		    " burstsize failed."));
21193859Sml29623 		ddi_status = DDI_FAILURE;
21203859Sml29623 		goto nxge_get_soft_properties_fail1;
21213859Sml29623 	}
21223859Sml29623 
21233859Sml29623 	nxgep->sys_burst_sz = ddi_dma_burstsizes(nxgep->dmasparehandle);
21243859Sml29623 	(void) ddi_dma_unbind_handle(nxgep->dmasparehandle);
21253859Sml29623 
21263859Sml29623 nxge_get_soft_properties_fail1:
21273859Sml29623 	ddi_dma_free_handle(&nxgep->dmasparehandle);
21283859Sml29623 
21293859Sml29623 nxge_get_soft_properties_exit:
21303859Sml29623 
21313859Sml29623 	if (ddi_status != DDI_SUCCESS)
21323859Sml29623 		status |= (NXGE_ERROR | NXGE_DDI_FAILED);
21333859Sml29623 
21343859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
21356512Ssowmini 	    "<== nxge_setup_system_dma_pages status = 0x%08x", status));
21363859Sml29623 	return (status);
21373859Sml29623 }
21383859Sml29623 
21393859Sml29623 static nxge_status_t
21403859Sml29623 nxge_alloc_mem_pool(p_nxge_t nxgep)
21413859Sml29623 {
21423859Sml29623 	nxge_status_t	status = NXGE_OK;
21433859Sml29623 
21443859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_alloc_mem_pool"));
21453859Sml29623 
21463859Sml29623 	status = nxge_alloc_rx_mem_pool(nxgep);
21473859Sml29623 	if (status != NXGE_OK) {
21483859Sml29623 		return (NXGE_ERROR);
21493859Sml29623 	}
21503859Sml29623 
21513859Sml29623 	status = nxge_alloc_tx_mem_pool(nxgep);
21523859Sml29623 	if (status != NXGE_OK) {
21533859Sml29623 		nxge_free_rx_mem_pool(nxgep);
21543859Sml29623 		return (NXGE_ERROR);
21553859Sml29623 	}
21563859Sml29623 
21573859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_alloc_mem_pool"));
21583859Sml29623 	return (NXGE_OK);
21593859Sml29623 }
21603859Sml29623 
21613859Sml29623 static void
21623859Sml29623 nxge_free_mem_pool(p_nxge_t nxgep)
21633859Sml29623 {
21643859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "==> nxge_free_mem_pool"));
21653859Sml29623 
21663859Sml29623 	nxge_free_rx_mem_pool(nxgep);
21673859Sml29623 	nxge_free_tx_mem_pool(nxgep);
21683859Sml29623 
21693859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "<== nxge_free_mem_pool"));
21703859Sml29623 }
21713859Sml29623 
21726495Sspeer nxge_status_t
21733859Sml29623 nxge_alloc_rx_mem_pool(p_nxge_t nxgep)
21743859Sml29623 {
21756495Sspeer 	uint32_t		rdc_max;
21763859Sml29623 	p_nxge_dma_pt_cfg_t	p_all_cfgp;
21773859Sml29623 	p_nxge_hw_pt_cfg_t	p_cfgp;
21783859Sml29623 	p_nxge_dma_pool_t	dma_poolp;
21793859Sml29623 	p_nxge_dma_common_t	*dma_buf_p;
21803859Sml29623 	p_nxge_dma_pool_t	dma_cntl_poolp;
21813859Sml29623 	p_nxge_dma_common_t	*dma_cntl_p;
21823859Sml29623 	uint32_t 		*num_chunks; /* per dma */
21833859Sml29623 	nxge_status_t		status = NXGE_OK;
21843859Sml29623 
21853859Sml29623 	uint32_t		nxge_port_rbr_size;
21863859Sml29623 	uint32_t		nxge_port_rbr_spare_size;
21873859Sml29623 	uint32_t		nxge_port_rcr_size;
21886495Sspeer 	uint32_t		rx_cntl_alloc_size;
21893859Sml29623 
21903859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rx_mem_pool"));
21913859Sml29623 
21923859Sml29623 	p_all_cfgp = (p_nxge_dma_pt_cfg_t)&nxgep->pt_config;
21933859Sml29623 	p_cfgp = (p_nxge_hw_pt_cfg_t)&p_all_cfgp->hw_config;
21946495Sspeer 	rdc_max = NXGE_MAX_RDCS;
21953859Sml29623 
21963859Sml29623 	/*
21976495Sspeer 	 * Allocate memory for the common DMA data structures.
21983859Sml29623 	 */
21993859Sml29623 	dma_poolp = (p_nxge_dma_pool_t)KMEM_ZALLOC(sizeof (nxge_dma_pool_t),
22006512Ssowmini 	    KM_SLEEP);
22013859Sml29623 	dma_buf_p = (p_nxge_dma_common_t *)KMEM_ZALLOC(
22026512Ssowmini 	    sizeof (p_nxge_dma_common_t) * rdc_max, KM_SLEEP);
22033859Sml29623 
22043859Sml29623 	dma_cntl_poolp = (p_nxge_dma_pool_t)
22056512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_pool_t), KM_SLEEP);
22063859Sml29623 	dma_cntl_p = (p_nxge_dma_common_t *)KMEM_ZALLOC(
22076512Ssowmini 	    sizeof (p_nxge_dma_common_t) * rdc_max, KM_SLEEP);
22083859Sml29623 
22093859Sml29623 	num_chunks = (uint32_t *)KMEM_ZALLOC(
22106512Ssowmini 	    sizeof (uint32_t) * rdc_max, KM_SLEEP);
22113859Sml29623 
22123859Sml29623 	/*
22136495Sspeer 	 * Assume that each DMA channel will be configured with
22146495Sspeer 	 * the default block size.
22156495Sspeer 	 * rbr block counts are modulo the batch count (16).
22163859Sml29623 	 */
22173859Sml29623 	nxge_port_rbr_size = p_all_cfgp->rbr_size;
22183859Sml29623 	nxge_port_rcr_size = p_all_cfgp->rcr_size;
22193859Sml29623 
22203859Sml29623 	if (!nxge_port_rbr_size) {
22213859Sml29623 		nxge_port_rbr_size = NXGE_RBR_RBB_DEFAULT;
22223859Sml29623 	}
22233859Sml29623 	if (nxge_port_rbr_size % NXGE_RXDMA_POST_BATCH) {
22243859Sml29623 		nxge_port_rbr_size = (NXGE_RXDMA_POST_BATCH *
22256512Ssowmini 		    (nxge_port_rbr_size / NXGE_RXDMA_POST_BATCH + 1));
22263859Sml29623 	}
22273859Sml29623 
22283859Sml29623 	p_all_cfgp->rbr_size = nxge_port_rbr_size;
22293859Sml29623 	nxge_port_rbr_spare_size = nxge_rbr_spare_size;
22303859Sml29623 
22313859Sml29623 	if (nxge_port_rbr_spare_size % NXGE_RXDMA_POST_BATCH) {
22323859Sml29623 		nxge_port_rbr_spare_size = (NXGE_RXDMA_POST_BATCH *
22336512Ssowmini 		    (nxge_port_rbr_spare_size / NXGE_RXDMA_POST_BATCH + 1));
22343859Sml29623 	}
22355770Sml29623 	if (nxge_port_rbr_size > RBR_DEFAULT_MAX_BLKS) {
22365770Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM_CTL,
22375770Sml29623 		    "nxge_alloc_rx_mem_pool: RBR size too high %d, "
22385770Sml29623 		    "set to default %d",
22395770Sml29623 		    nxge_port_rbr_size, RBR_DEFAULT_MAX_BLKS));
22405770Sml29623 		nxge_port_rbr_size = RBR_DEFAULT_MAX_BLKS;
22415770Sml29623 	}
22425770Sml29623 	if (nxge_port_rcr_size > RCR_DEFAULT_MAX) {
22435770Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM_CTL,
22445770Sml29623 		    "nxge_alloc_rx_mem_pool: RCR too high %d, "
22455770Sml29623 		    "set to default %d",
22465770Sml29623 		    nxge_port_rcr_size, RCR_DEFAULT_MAX));
22475770Sml29623 		nxge_port_rcr_size = RCR_DEFAULT_MAX;
22485770Sml29623 	}
22493859Sml29623 
22503859Sml29623 	/*
22513859Sml29623 	 * N2/NIU has limitation on the descriptor sizes (contiguous
22523859Sml29623 	 * memory allocation on data buffers to 4M (contig_mem_alloc)
22533859Sml29623 	 * and little endian for control buffers (must use the ddi/dki mem alloc
22543859Sml29623 	 * function).
22553859Sml29623 	 */
22563859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
22573859Sml29623 	if (nxgep->niu_type == N2_NIU) {
22583859Sml29623 		nxge_port_rbr_spare_size = 0;
22593859Sml29623 		if ((nxge_port_rbr_size > NXGE_NIU_CONTIG_RBR_MAX) ||
22606512Ssowmini 		    (!ISP2(nxge_port_rbr_size))) {
22613859Sml29623 			nxge_port_rbr_size = NXGE_NIU_CONTIG_RBR_MAX;
22623859Sml29623 		}
22633859Sml29623 		if ((nxge_port_rcr_size > NXGE_NIU_CONTIG_RCR_MAX) ||
22646512Ssowmini 		    (!ISP2(nxge_port_rcr_size))) {
22653859Sml29623 			nxge_port_rcr_size = NXGE_NIU_CONTIG_RCR_MAX;
22663859Sml29623 		}
22673859Sml29623 	}
22683859Sml29623 #endif
22693859Sml29623 
22703859Sml29623 	/*
22713859Sml29623 	 * Addresses of receive block ring, receive completion ring and the
22723859Sml29623 	 * mailbox must be all cache-aligned (64 bytes).
22733859Sml29623 	 */
22743859Sml29623 	rx_cntl_alloc_size = nxge_port_rbr_size + nxge_port_rbr_spare_size;
22753859Sml29623 	rx_cntl_alloc_size *= (sizeof (rx_desc_t));
22763859Sml29623 	rx_cntl_alloc_size += (sizeof (rcr_entry_t) * nxge_port_rcr_size);
22773859Sml29623 	rx_cntl_alloc_size += sizeof (rxdma_mailbox_t);
22783859Sml29623 
22793859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_alloc_rx_mem_pool: "
22806512Ssowmini 	    "nxge_port_rbr_size = %d nxge_port_rbr_spare_size = %d "
22816512Ssowmini 	    "nxge_port_rcr_size = %d "
22826512Ssowmini 	    "rx_cntl_alloc_size = %d",
22836512Ssowmini 	    nxge_port_rbr_size, nxge_port_rbr_spare_size,
22846512Ssowmini 	    nxge_port_rcr_size,
22856512Ssowmini 	    rx_cntl_alloc_size));
22863859Sml29623 
22873859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
22883859Sml29623 	if (nxgep->niu_type == N2_NIU) {
22896495Sspeer 		uint32_t rx_buf_alloc_size = (nxgep->rx_default_block_size *
22906495Sspeer 		    (nxge_port_rbr_size + nxge_port_rbr_spare_size));
22916495Sspeer 
22923859Sml29623 		if (!ISP2(rx_buf_alloc_size)) {
22933859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
22946512Ssowmini 			    "==> nxge_alloc_rx_mem_pool: "
22956512Ssowmini 			    " must be power of 2"));
22963859Sml29623 			status |= (NXGE_ERROR | NXGE_DDI_FAILED);
22973859Sml29623 			goto nxge_alloc_rx_mem_pool_exit;
22983859Sml29623 		}
22993859Sml29623 
23003859Sml29623 		if (rx_buf_alloc_size > (1 << 22)) {
23013859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
23026512Ssowmini 			    "==> nxge_alloc_rx_mem_pool: "
23036512Ssowmini 			    " limit size to 4M"));
23043859Sml29623 			status |= (NXGE_ERROR | NXGE_DDI_FAILED);
23053859Sml29623 			goto nxge_alloc_rx_mem_pool_exit;
23063859Sml29623 		}
23073859Sml29623 
23083859Sml29623 		if (rx_cntl_alloc_size < 0x2000) {
23093859Sml29623 			rx_cntl_alloc_size = 0x2000;
23103859Sml29623 		}
23113859Sml29623 	}
23123859Sml29623 #endif
23133859Sml29623 	nxgep->nxge_port_rbr_size = nxge_port_rbr_size;
23143859Sml29623 	nxgep->nxge_port_rcr_size = nxge_port_rcr_size;
23156495Sspeer 	nxgep->nxge_port_rbr_spare_size = nxge_port_rbr_spare_size;
23166495Sspeer 	nxgep->nxge_port_rx_cntl_alloc_size = rx_cntl_alloc_size;
23176495Sspeer 
23186495Sspeer 	dma_poolp->ndmas = p_cfgp->max_rdcs;
23193859Sml29623 	dma_poolp->num_chunks = num_chunks;
23203859Sml29623 	dma_poolp->buf_allocated = B_TRUE;
23213859Sml29623 	nxgep->rx_buf_pool_p = dma_poolp;
23223859Sml29623 	dma_poolp->dma_buf_pool_p = dma_buf_p;
23233859Sml29623 
23246495Sspeer 	dma_cntl_poolp->ndmas = p_cfgp->max_rdcs;
23253859Sml29623 	dma_cntl_poolp->buf_allocated = B_TRUE;
23263859Sml29623 	nxgep->rx_cntl_pool_p = dma_cntl_poolp;
23273859Sml29623 	dma_cntl_poolp->dma_buf_pool_p = dma_cntl_p;
23283859Sml29623 
23296495Sspeer 	/* Allocate the receive rings, too. */
23306495Sspeer 	nxgep->rx_rbr_rings =
23316512Ssowmini 	    KMEM_ZALLOC(sizeof (rx_rbr_rings_t), KM_SLEEP);
23326495Sspeer 	nxgep->rx_rbr_rings->rbr_rings =
23336512Ssowmini 	    KMEM_ZALLOC(sizeof (p_rx_rbr_ring_t) * rdc_max, KM_SLEEP);
23346495Sspeer 	nxgep->rx_rcr_rings =
23356512Ssowmini 	    KMEM_ZALLOC(sizeof (rx_rcr_rings_t), KM_SLEEP);
23366495Sspeer 	nxgep->rx_rcr_rings->rcr_rings =
23376512Ssowmini 	    KMEM_ZALLOC(sizeof (p_rx_rcr_ring_t) * rdc_max, KM_SLEEP);
23386495Sspeer 	nxgep->rx_mbox_areas_p =
23396512Ssowmini 	    KMEM_ZALLOC(sizeof (rx_mbox_areas_t), KM_SLEEP);
23406495Sspeer 	nxgep->rx_mbox_areas_p->rxmbox_areas =
23416512Ssowmini 	    KMEM_ZALLOC(sizeof (p_rx_mbox_t) * rdc_max, KM_SLEEP);
23426495Sspeer 
23436495Sspeer 	nxgep->rx_rbr_rings->ndmas = nxgep->rx_rcr_rings->ndmas =
23446495Sspeer 	    p_cfgp->max_rdcs;
23456495Sspeer 
23463859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
23476512Ssowmini 	    "<== nxge_alloc_rx_mem_pool:status 0x%08x", status));
23483859Sml29623 
23493859Sml29623 nxge_alloc_rx_mem_pool_exit:
23506495Sspeer 	return (status);
23516495Sspeer }
23526495Sspeer 
23536495Sspeer /*
23546495Sspeer  * nxge_alloc_rxb
23556495Sspeer  *
23566495Sspeer  *	Allocate buffers for an RDC.
23576495Sspeer  *
23586495Sspeer  * Arguments:
23596495Sspeer  * 	nxgep
23606495Sspeer  * 	channel	The channel to map into our kernel space.
23616495Sspeer  *
23626495Sspeer  * Notes:
23636495Sspeer  *
23646495Sspeer  * NPI function calls:
23656495Sspeer  *
23666495Sspeer  * NXGE function calls:
23676495Sspeer  *
23686495Sspeer  * Registers accessed:
23696495Sspeer  *
23706495Sspeer  * Context:
23716495Sspeer  *
23726495Sspeer  * Taking apart:
23736495Sspeer  *
23746495Sspeer  * Open questions:
23756495Sspeer  *
23766495Sspeer  */
23776495Sspeer nxge_status_t
23786495Sspeer nxge_alloc_rxb(
23796495Sspeer 	p_nxge_t nxgep,
23806495Sspeer 	int channel)
23816495Sspeer {
23826495Sspeer 	size_t			rx_buf_alloc_size;
23836495Sspeer 	nxge_status_t		status = NXGE_OK;
23846495Sspeer 
23856495Sspeer 	nxge_dma_common_t	**data;
23866495Sspeer 	nxge_dma_common_t	**control;
23876495Sspeer 	uint32_t 		*num_chunks;
23886495Sspeer 
23896495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rbb"));
23906495Sspeer 
23916495Sspeer 	/*
23926495Sspeer 	 * Allocate memory for the receive buffers and descriptor rings.
23936495Sspeer 	 * Replace these allocation functions with the interface functions
23946495Sspeer 	 * provided by the partition manager if/when they are available.
23956495Sspeer 	 */
23966495Sspeer 
23976495Sspeer 	/*
23986495Sspeer 	 * Allocate memory for the receive buffer blocks.
23996495Sspeer 	 */
24006495Sspeer 	rx_buf_alloc_size = (nxgep->rx_default_block_size *
24016512Ssowmini 	    (nxgep->nxge_port_rbr_size + nxgep->nxge_port_rbr_spare_size));
24026495Sspeer 
24036495Sspeer 	data = &nxgep->rx_buf_pool_p->dma_buf_pool_p[channel];
24046495Sspeer 	num_chunks = &nxgep->rx_buf_pool_p->num_chunks[channel];
24056495Sspeer 
24066495Sspeer 	if ((status = nxge_alloc_rx_buf_dma(
24076495Sspeer 	    nxgep, channel, data, rx_buf_alloc_size,
24086495Sspeer 	    nxgep->rx_default_block_size, num_chunks)) != NXGE_OK) {
24096495Sspeer 		return (status);
24106495Sspeer 	}
24116495Sspeer 
24126495Sspeer 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "<== nxge_alloc_rxb(): "
24136495Sspeer 	    "dma %d dma_buf_p %llx &dma_buf_p %llx", channel, *data, data));
24146495Sspeer 
24156495Sspeer 	/*
24166495Sspeer 	 * Allocate memory for descriptor rings and mailbox.
24176495Sspeer 	 */
24186495Sspeer 	control = &nxgep->rx_cntl_pool_p->dma_buf_pool_p[channel];
24196495Sspeer 
24206495Sspeer 	if ((status = nxge_alloc_rx_cntl_dma(
24216495Sspeer 	    nxgep, channel, control, nxgep->nxge_port_rx_cntl_alloc_size))
24226495Sspeer 	    != NXGE_OK) {
24236495Sspeer 		nxge_free_rx_cntl_dma(nxgep, *control);
24246495Sspeer 		(*data)->buf_alloc_state |= BUF_ALLOCATED_WAIT_FREE;
24256495Sspeer 		nxge_free_rx_buf_dma(nxgep, *data, *num_chunks);
24266495Sspeer 		return (status);
24276495Sspeer 	}
24286495Sspeer 
24293859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
24306495Sspeer 	    "<== nxge_alloc_rx_mem_pool:status 0x%08x", status));
24313859Sml29623 
24323859Sml29623 	return (status);
24333859Sml29623 }
24343859Sml29623 
24356495Sspeer void
24366495Sspeer nxge_free_rxb(
24376495Sspeer 	p_nxge_t nxgep,
24386495Sspeer 	int channel)
24396495Sspeer {
24406495Sspeer 	nxge_dma_common_t	*data;
24416495Sspeer 	nxge_dma_common_t	*control;
24426495Sspeer 	uint32_t 		num_chunks;
24436495Sspeer 
24446495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rbb"));
24456495Sspeer 
24466495Sspeer 	data = nxgep->rx_buf_pool_p->dma_buf_pool_p[channel];
24476495Sspeer 	num_chunks = nxgep->rx_buf_pool_p->num_chunks[channel];
24486495Sspeer 	nxge_free_rx_buf_dma(nxgep, data, num_chunks);
24496495Sspeer 
24506495Sspeer 	nxgep->rx_buf_pool_p->dma_buf_pool_p[channel] = 0;
24516495Sspeer 	nxgep->rx_buf_pool_p->num_chunks[channel] = 0;
24526495Sspeer 
24536495Sspeer 	control = nxgep->rx_cntl_pool_p->dma_buf_pool_p[channel];
24546495Sspeer 	nxge_free_rx_cntl_dma(nxgep, control);
24556495Sspeer 
24566495Sspeer 	nxgep->rx_cntl_pool_p->dma_buf_pool_p[channel] = 0;
24576495Sspeer 
24586495Sspeer 	KMEM_FREE(data, sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK);
24596495Sspeer 	KMEM_FREE(control, sizeof (nxge_dma_common_t));
24606495Sspeer 
24616495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_alloc_rbb"));
24626495Sspeer }
24636495Sspeer 
24643859Sml29623 static void
24653859Sml29623 nxge_free_rx_mem_pool(p_nxge_t nxgep)
24663859Sml29623 {
24676495Sspeer 	int rdc_max = NXGE_MAX_RDCS;
24683859Sml29623 
24693859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_free_rx_mem_pool"));
24703859Sml29623 
24716495Sspeer 	if (!nxgep->rx_buf_pool_p || !nxgep->rx_buf_pool_p->buf_allocated) {
24723859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
24736512Ssowmini 		    "<== nxge_free_rx_mem_pool "
24746512Ssowmini 		    "(null rx buf pool or buf not allocated"));
24753859Sml29623 		return;
24763859Sml29623 	}
24776495Sspeer 	if (!nxgep->rx_cntl_pool_p || !nxgep->rx_cntl_pool_p->buf_allocated) {
24783859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
24796512Ssowmini 		    "<== nxge_free_rx_mem_pool "
24806512Ssowmini 		    "(null rx cntl buf pool or cntl buf not allocated"));
24813859Sml29623 		return;
24823859Sml29623 	}
24833859Sml29623 
24846495Sspeer 	KMEM_FREE(nxgep->rx_cntl_pool_p->dma_buf_pool_p,
24856495Sspeer 	    sizeof (p_nxge_dma_common_t) * rdc_max);
24866495Sspeer 	KMEM_FREE(nxgep->rx_cntl_pool_p, sizeof (nxge_dma_pool_t));
24876495Sspeer 
24886495Sspeer 	KMEM_FREE(nxgep->rx_buf_pool_p->num_chunks,
24896495Sspeer 	    sizeof (uint32_t) * rdc_max);
24906495Sspeer 	KMEM_FREE(nxgep->rx_buf_pool_p->dma_buf_pool_p,
24916495Sspeer 	    sizeof (p_nxge_dma_common_t) * rdc_max);
24926495Sspeer 	KMEM_FREE(nxgep->rx_buf_pool_p, sizeof (nxge_dma_pool_t));
24936495Sspeer 
24946495Sspeer 	nxgep->rx_buf_pool_p = 0;
24956495Sspeer 	nxgep->rx_cntl_pool_p = 0;
24966495Sspeer 
24976495Sspeer 	KMEM_FREE(nxgep->rx_rbr_rings->rbr_rings,
24986495Sspeer 	    sizeof (p_rx_rbr_ring_t) * rdc_max);
24996495Sspeer 	KMEM_FREE(nxgep->rx_rbr_rings, sizeof (rx_rbr_rings_t));
25006495Sspeer 	KMEM_FREE(nxgep->rx_rcr_rings->rcr_rings,
25016495Sspeer 	    sizeof (p_rx_rcr_ring_t) * rdc_max);
25026495Sspeer 	KMEM_FREE(nxgep->rx_rcr_rings, sizeof (rx_rcr_rings_t));
25036495Sspeer 	KMEM_FREE(nxgep->rx_mbox_areas_p->rxmbox_areas,
25046495Sspeer 	    sizeof (p_rx_mbox_t) * rdc_max);
25056495Sspeer 	KMEM_FREE(nxgep->rx_mbox_areas_p, sizeof (rx_mbox_areas_t));
25066495Sspeer 
25076495Sspeer 	nxgep->rx_rbr_rings = 0;
25086495Sspeer 	nxgep->rx_rcr_rings = 0;
25096495Sspeer 	nxgep->rx_mbox_areas_p = 0;
25103859Sml29623 
25113859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "<== nxge_free_rx_mem_pool"));
25123859Sml29623 }
25133859Sml29623 
25143859Sml29623 
25153859Sml29623 static nxge_status_t
25163859Sml29623 nxge_alloc_rx_buf_dma(p_nxge_t nxgep, uint16_t dma_channel,
25173859Sml29623 	p_nxge_dma_common_t *dmap,
25183859Sml29623 	size_t alloc_size, size_t block_size, uint32_t *num_chunks)
25193859Sml29623 {
25203859Sml29623 	p_nxge_dma_common_t 	rx_dmap;
25213859Sml29623 	nxge_status_t		status = NXGE_OK;
25223859Sml29623 	size_t			total_alloc_size;
25233859Sml29623 	size_t			allocated = 0;
25243859Sml29623 	int			i, size_index, array_size;
25256495Sspeer 	boolean_t		use_kmem_alloc = B_FALSE;
25263859Sml29623 
25273859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rx_buf_dma"));
25283859Sml29623 
25293859Sml29623 	rx_dmap = (p_nxge_dma_common_t)
25306512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK,
25316512Ssowmini 	    KM_SLEEP);
25323859Sml29623 
25333859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
25346512Ssowmini 	    " alloc_rx_buf_dma rdc %d asize %x bsize %x bbuf %llx ",
25356512Ssowmini 	    dma_channel, alloc_size, block_size, dmap));
25363859Sml29623 
25373859Sml29623 	total_alloc_size = alloc_size;
25383859Sml29623 
25393859Sml29623 #if defined(RX_USE_RECLAIM_POST)
25403859Sml29623 	total_alloc_size = alloc_size + alloc_size/4;
25413859Sml29623 #endif
25423859Sml29623 
25433859Sml29623 	i = 0;
25443859Sml29623 	size_index = 0;
25453859Sml29623 	array_size =  sizeof (alloc_sizes)/sizeof (size_t);
25468661SSantwona.Behera@Sun.COM 	while ((size_index < array_size) &&
25478661SSantwona.Behera@Sun.COM 	    (alloc_sizes[size_index] < alloc_size))
25486512Ssowmini 		size_index++;
25493859Sml29623 	if (size_index >= array_size) {
25503859Sml29623 		size_index = array_size - 1;
25513859Sml29623 	}
25523859Sml29623 
25536495Sspeer 	/* For Neptune, use kmem_alloc if the kmem flag is set. */
25546495Sspeer 	if (nxgep->niu_type != N2_NIU && nxge_use_kmem_alloc) {
25556495Sspeer 		use_kmem_alloc = B_TRUE;
25566495Sspeer #if defined(__i386) || defined(__amd64)
25576495Sspeer 		size_index = 0;
25586495Sspeer #endif
25596495Sspeer 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
25606495Sspeer 		    "==> nxge_alloc_rx_buf_dma: "
25616495Sspeer 		    "Neptune use kmem_alloc() - size_index %d",
25626495Sspeer 		    size_index));
25636495Sspeer 	}
25646495Sspeer 
25653859Sml29623 	while ((allocated < total_alloc_size) &&
25666512Ssowmini 	    (size_index >= 0) && (i < NXGE_DMA_BLOCK)) {
25673859Sml29623 		rx_dmap[i].dma_chunk_index = i;
25683859Sml29623 		rx_dmap[i].block_size = block_size;
25693859Sml29623 		rx_dmap[i].alength = alloc_sizes[size_index];
25703859Sml29623 		rx_dmap[i].orig_alength = rx_dmap[i].alength;
25713859Sml29623 		rx_dmap[i].nblocks = alloc_sizes[size_index] / block_size;
25723859Sml29623 		rx_dmap[i].dma_channel = dma_channel;
25733859Sml29623 		rx_dmap[i].contig_alloc_type = B_FALSE;
25746495Sspeer 		rx_dmap[i].kmem_alloc_type = B_FALSE;
25756495Sspeer 		rx_dmap[i].buf_alloc_type = DDI_MEM_ALLOC;
25763859Sml29623 
25773859Sml29623 		/*
25783859Sml29623 		 * N2/NIU: data buffers must be contiguous as the driver
25793859Sml29623 		 *	   needs to call Hypervisor api to set up
25803859Sml29623 		 *	   logical pages.
25813859Sml29623 		 */
25823859Sml29623 		if ((nxgep->niu_type == N2_NIU) && (NXGE_DMA_BLOCK == 1)) {
25833859Sml29623 			rx_dmap[i].contig_alloc_type = B_TRUE;
25846495Sspeer 			rx_dmap[i].buf_alloc_type = CONTIG_MEM_ALLOC;
25856495Sspeer 		} else if (use_kmem_alloc) {
25866495Sspeer 			/* For Neptune, use kmem_alloc */
25876495Sspeer 			NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
25886495Sspeer 			    "==> nxge_alloc_rx_buf_dma: "
25896495Sspeer 			    "Neptune use kmem_alloc()"));
25906495Sspeer 			rx_dmap[i].kmem_alloc_type = B_TRUE;
25916495Sspeer 			rx_dmap[i].buf_alloc_type = KMEM_ALLOC;
25923859Sml29623 		}
25933859Sml29623 
25943859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
25956512Ssowmini 		    "alloc_rx_buf_dma rdc %d chunk %d bufp %llx size %x "
25966512Ssowmini 		    "i %d nblocks %d alength %d",
25976512Ssowmini 		    dma_channel, i, &rx_dmap[i], block_size,
25986512Ssowmini 		    i, rx_dmap[i].nblocks,
25996512Ssowmini 		    rx_dmap[i].alength));
26003859Sml29623 		status = nxge_dma_mem_alloc(nxgep, nxge_force_dma,
26016512Ssowmini 		    &nxge_rx_dma_attr,
26026512Ssowmini 		    rx_dmap[i].alength,
26036512Ssowmini 		    &nxge_dev_buf_dma_acc_attr,
26046512Ssowmini 		    DDI_DMA_READ | DDI_DMA_STREAMING,
26056512Ssowmini 		    (p_nxge_dma_common_t)(&rx_dmap[i]));
26063859Sml29623 		if (status != NXGE_OK) {
26073859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
26086495Sspeer 			    "nxge_alloc_rx_buf_dma: Alloc Failed: "
26096495Sspeer 			    "dma %d size_index %d size requested %d",
26106495Sspeer 			    dma_channel,
26116495Sspeer 			    size_index,
26126495Sspeer 			    rx_dmap[i].alength));
26133859Sml29623 			size_index--;
26143859Sml29623 		} else {
26156495Sspeer 			rx_dmap[i].buf_alloc_state = BUF_ALLOCATED;
26166495Sspeer 			NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26176495Sspeer 			    " nxge_alloc_rx_buf_dma DONE  alloc mem: "
26186495Sspeer 			    "dma %d dma_buf_p $%p kaddrp $%p alength %d "
26196495Sspeer 			    "buf_alloc_state %d alloc_type %d",
26206495Sspeer 			    dma_channel,
26216495Sspeer 			    &rx_dmap[i],
26226495Sspeer 			    rx_dmap[i].kaddrp,
26236495Sspeer 			    rx_dmap[i].alength,
26246495Sspeer 			    rx_dmap[i].buf_alloc_state,
26256495Sspeer 			    rx_dmap[i].buf_alloc_type));
26266495Sspeer 			NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26276495Sspeer 			    " alloc_rx_buf_dma allocated rdc %d "
26286495Sspeer 			    "chunk %d size %x dvma %x bufp %llx kaddrp $%p",
26296495Sspeer 			    dma_channel, i, rx_dmap[i].alength,
26306495Sspeer 			    rx_dmap[i].ioaddr_pp, &rx_dmap[i],
26316495Sspeer 			    rx_dmap[i].kaddrp));
26323859Sml29623 			i++;
26333859Sml29623 			allocated += alloc_sizes[size_index];
26343859Sml29623 		}
26353859Sml29623 	}
26363859Sml29623 
26373859Sml29623 	if (allocated < total_alloc_size) {
26385770Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
26396495Sspeer 		    "==> nxge_alloc_rx_buf_dma: not enough for channel %d "
26405770Sml29623 		    "allocated 0x%x requested 0x%x",
26415770Sml29623 		    dma_channel,
26425770Sml29623 		    allocated, total_alloc_size));
26435770Sml29623 		status = NXGE_ERROR;
26443859Sml29623 		goto nxge_alloc_rx_mem_fail1;
26453859Sml29623 	}
26463859Sml29623 
26475770Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26486495Sspeer 	    "==> nxge_alloc_rx_buf_dma: Allocated for channel %d "
26495770Sml29623 	    "allocated 0x%x requested 0x%x",
26505770Sml29623 	    dma_channel,
26515770Sml29623 	    allocated, total_alloc_size));
26525770Sml29623 
26533859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
26546512Ssowmini 	    " alloc_rx_buf_dma rdc %d allocated %d chunks",
26556512Ssowmini 	    dma_channel, i));
26563859Sml29623 	*num_chunks = i;
26573859Sml29623 	*dmap = rx_dmap;
26583859Sml29623 
26593859Sml29623 	goto nxge_alloc_rx_mem_exit;
26603859Sml29623 
26613859Sml29623 nxge_alloc_rx_mem_fail1:
26623859Sml29623 	KMEM_FREE(rx_dmap, sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK);
26633859Sml29623 
26643859Sml29623 nxge_alloc_rx_mem_exit:
26653859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
26666512Ssowmini 	    "<== nxge_alloc_rx_buf_dma status 0x%08x", status));
26673859Sml29623 
26683859Sml29623 	return (status);
26693859Sml29623 }
26703859Sml29623 
26713859Sml29623 /*ARGSUSED*/
26723859Sml29623 static void
26733859Sml29623 nxge_free_rx_buf_dma(p_nxge_t nxgep, p_nxge_dma_common_t dmap,
26743859Sml29623     uint32_t num_chunks)
26753859Sml29623 {
26763859Sml29623 	int		i;
26773859Sml29623 
26783859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26796512Ssowmini 	    "==> nxge_free_rx_buf_dma: # of chunks %d", num_chunks));
26803859Sml29623 
26816495Sspeer 	if (dmap == 0)
26826495Sspeer 		return;
26836495Sspeer 
26843859Sml29623 	for (i = 0; i < num_chunks; i++) {
26853859Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
26866512Ssowmini 		    "==> nxge_free_rx_buf_dma: chunk %d dmap 0x%llx",
26876512Ssowmini 		    i, dmap));
26886495Sspeer 		nxge_dma_free_rx_data_buf(dmap++);
26893859Sml29623 	}
26903859Sml29623 
26913859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_free_rx_buf_dma"));
26923859Sml29623 }
26933859Sml29623 
26943859Sml29623 /*ARGSUSED*/
26953859Sml29623 static nxge_status_t
26963859Sml29623 nxge_alloc_rx_cntl_dma(p_nxge_t nxgep, uint16_t dma_channel,
26973859Sml29623     p_nxge_dma_common_t *dmap, size_t size)
26983859Sml29623 {
26993859Sml29623 	p_nxge_dma_common_t 	rx_dmap;
27003859Sml29623 	nxge_status_t		status = NXGE_OK;
27013859Sml29623 
27023859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_rx_cntl_dma"));
27033859Sml29623 
27043859Sml29623 	rx_dmap = (p_nxge_dma_common_t)
27056512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_common_t), KM_SLEEP);
27063859Sml29623 
27073859Sml29623 	rx_dmap->contig_alloc_type = B_FALSE;
27086495Sspeer 	rx_dmap->kmem_alloc_type = B_FALSE;
27093859Sml29623 
27103859Sml29623 	status = nxge_dma_mem_alloc(nxgep, nxge_force_dma,
27116512Ssowmini 	    &nxge_desc_dma_attr,
27126512Ssowmini 	    size,
27136512Ssowmini 	    &nxge_dev_desc_dma_acc_attr,
27146512Ssowmini 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
27156512Ssowmini 	    rx_dmap);
27163859Sml29623 	if (status != NXGE_OK) {
27173859Sml29623 		goto nxge_alloc_rx_cntl_dma_fail1;
27183859Sml29623 	}
27193859Sml29623 
27203859Sml29623 	*dmap = rx_dmap;
27213859Sml29623 	goto nxge_alloc_rx_cntl_dma_exit;
27223859Sml29623 
27233859Sml29623 nxge_alloc_rx_cntl_dma_fail1:
27243859Sml29623 	KMEM_FREE(rx_dmap, sizeof (nxge_dma_common_t));
27253859Sml29623 
27263859Sml29623 nxge_alloc_rx_cntl_dma_exit:
27273859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
27286512Ssowmini 	    "<== nxge_alloc_rx_cntl_dma status 0x%08x", status));
27293859Sml29623 
27303859Sml29623 	return (status);
27313859Sml29623 }
27323859Sml29623 
27333859Sml29623 /*ARGSUSED*/
27343859Sml29623 static void
27353859Sml29623 nxge_free_rx_cntl_dma(p_nxge_t nxgep, p_nxge_dma_common_t dmap)
27363859Sml29623 {
27373859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_free_rx_cntl_dma"));
27383859Sml29623 
27396495Sspeer 	if (dmap == 0)
27406495Sspeer 		return;
27416495Sspeer 
27423859Sml29623 	nxge_dma_mem_free(dmap);
27433859Sml29623 
27443859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_free_rx_cntl_dma"));
27453859Sml29623 }
27463859Sml29623 
27476495Sspeer typedef struct {
27486495Sspeer 	size_t	tx_size;
27496495Sspeer 	size_t	cr_size;
27506495Sspeer 	size_t	threshhold;
27516495Sspeer } nxge_tdc_sizes_t;
27526495Sspeer 
27536495Sspeer static
27546495Sspeer nxge_status_t
27556495Sspeer nxge_tdc_sizes(
27566495Sspeer 	nxge_t *nxgep,
27576495Sspeer 	nxge_tdc_sizes_t *sizes)
27586495Sspeer {
27596495Sspeer 	uint32_t threshhold;	/* The bcopy() threshhold */
27606495Sspeer 	size_t tx_size;		/* Transmit buffer size */
27616495Sspeer 	size_t cr_size;		/* Completion ring size */
27626495Sspeer 
27636495Sspeer 	/*
27646495Sspeer 	 * Assume that each DMA channel will be configured with the
27656495Sspeer 	 * default transmit buffer size for copying transmit data.
27666495Sspeer 	 * (If a packet is bigger than this, it will not be copied.)
27676495Sspeer 	 */
27686495Sspeer 	if (nxgep->niu_type == N2_NIU) {
27696495Sspeer 		threshhold = TX_BCOPY_SIZE;
27706495Sspeer 	} else {
27716495Sspeer 		threshhold = nxge_bcopy_thresh;
27726495Sspeer 	}
27736495Sspeer 	tx_size = nxge_tx_ring_size * threshhold;
27746495Sspeer 
27756495Sspeer 	cr_size = nxge_tx_ring_size * sizeof (tx_desc_t);
27766495Sspeer 	cr_size += sizeof (txdma_mailbox_t);
27776495Sspeer 
27786495Sspeer #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
27796495Sspeer 	if (nxgep->niu_type == N2_NIU) {
27806495Sspeer 		if (!ISP2(tx_size)) {
27816495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
27826512Ssowmini 			    "==> nxge_tdc_sizes: Tx size"
27836512Ssowmini 			    " must be power of 2"));
27846495Sspeer 			return (NXGE_ERROR);
27856495Sspeer 		}
27866495Sspeer 
27876495Sspeer 		if (tx_size > (1 << 22)) {
27886495Sspeer 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
27896512Ssowmini 			    "==> nxge_tdc_sizes: Tx size"
27906512Ssowmini 			    " limited to 4M"));
27916495Sspeer 			return (NXGE_ERROR);
27926495Sspeer 		}
27936495Sspeer 
27946495Sspeer 		if (cr_size < 0x2000)
27956495Sspeer 			cr_size = 0x2000;
27966495Sspeer 	}
27976495Sspeer #endif
27986495Sspeer 
27996495Sspeer 	sizes->threshhold = threshhold;
28006495Sspeer 	sizes->tx_size = tx_size;
28016495Sspeer 	sizes->cr_size = cr_size;
28026495Sspeer 
28036495Sspeer 	return (NXGE_OK);
28046495Sspeer }
28056495Sspeer /*
28066495Sspeer  * nxge_alloc_txb
28076495Sspeer  *
28086495Sspeer  *	Allocate buffers for an TDC.
28096495Sspeer  *
28106495Sspeer  * Arguments:
28116495Sspeer  * 	nxgep
28126495Sspeer  * 	channel	The channel to map into our kernel space.
28136495Sspeer  *
28146495Sspeer  * Notes:
28156495Sspeer  *
28166495Sspeer  * NPI function calls:
28176495Sspeer  *
28186495Sspeer  * NXGE function calls:
28196495Sspeer  *
28206495Sspeer  * Registers accessed:
28216495Sspeer  *
28226495Sspeer  * Context:
28236495Sspeer  *
28246495Sspeer  * Taking apart:
28256495Sspeer  *
28266495Sspeer  * Open questions:
28276495Sspeer  *
28286495Sspeer  */
28296495Sspeer nxge_status_t
28306495Sspeer nxge_alloc_txb(
28316495Sspeer 	p_nxge_t nxgep,
28326495Sspeer 	int channel)
28336495Sspeer {
28346495Sspeer 	nxge_dma_common_t	**dma_buf_p;
28356495Sspeer 	nxge_dma_common_t	**dma_cntl_p;
28366495Sspeer 	uint32_t 		*num_chunks;
28376495Sspeer 	nxge_status_t		status = NXGE_OK;
28386495Sspeer 
28396495Sspeer 	nxge_tdc_sizes_t	sizes;
28406495Sspeer 
28416495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_tbb"));
28426495Sspeer 
28436495Sspeer 	if (nxge_tdc_sizes(nxgep, &sizes) != NXGE_OK)
28446495Sspeer 		return (NXGE_ERROR);
28456495Sspeer 
28466495Sspeer 	/*
28476495Sspeer 	 * Allocate memory for transmit buffers and descriptor rings.
28486495Sspeer 	 * Replace these allocation functions with the interface functions
28496495Sspeer 	 * provided by the partition manager Real Soon Now.
28506495Sspeer 	 */
28516495Sspeer 	dma_buf_p = &nxgep->tx_buf_pool_p->dma_buf_pool_p[channel];
28526495Sspeer 	num_chunks = &nxgep->tx_buf_pool_p->num_chunks[channel];
28536495Sspeer 
28546495Sspeer 	dma_cntl_p = &nxgep->tx_cntl_pool_p->dma_buf_pool_p[channel];
28556495Sspeer 
28566495Sspeer 	/*
28576495Sspeer 	 * Allocate memory for transmit buffers and descriptor rings.
28586495Sspeer 	 * Replace allocation functions with interface functions provided
28596495Sspeer 	 * by the partition manager when it is available.
28606495Sspeer 	 *
28616495Sspeer 	 * Allocate memory for the transmit buffer pool.
28626495Sspeer 	 */
28636495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
28646512Ssowmini 	    "sizes: tx: %ld, cr:%ld, th:%ld",
28656512Ssowmini 	    sizes.tx_size, sizes.cr_size, sizes.threshhold));
28666495Sspeer 
28676495Sspeer 	*num_chunks = 0;
28686495Sspeer 	status = nxge_alloc_tx_buf_dma(nxgep, channel, dma_buf_p,
28696495Sspeer 	    sizes.tx_size, sizes.threshhold, num_chunks);
28706495Sspeer 	if (status != NXGE_OK) {
28716495Sspeer 		cmn_err(CE_NOTE, "nxge_alloc_tx_buf_dma failed!");
28726495Sspeer 		return (status);
28736495Sspeer 	}
28746495Sspeer 
28756495Sspeer 	/*
28766495Sspeer 	 * Allocate memory for descriptor rings and mailbox.
28776495Sspeer 	 */
28786495Sspeer 	status = nxge_alloc_tx_cntl_dma(nxgep, channel, dma_cntl_p,
28796495Sspeer 	    sizes.cr_size);
28806495Sspeer 	if (status != NXGE_OK) {
28816495Sspeer 		nxge_free_tx_buf_dma(nxgep, *dma_buf_p, *num_chunks);
28826495Sspeer 		cmn_err(CE_NOTE, "nxge_alloc_tx_cntl_dma failed!");
28836495Sspeer 		return (status);
28846495Sspeer 	}
28856495Sspeer 
28866495Sspeer 	return (NXGE_OK);
28876495Sspeer }
28886495Sspeer 
28896495Sspeer void
28906495Sspeer nxge_free_txb(
28916495Sspeer 	p_nxge_t nxgep,
28926495Sspeer 	int channel)
28936495Sspeer {
28946495Sspeer 	nxge_dma_common_t	*data;
28956495Sspeer 	nxge_dma_common_t	*control;
28966495Sspeer 	uint32_t 		num_chunks;
28976495Sspeer 
28986495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_free_txb"));
28996495Sspeer 
29006495Sspeer 	data = nxgep->tx_buf_pool_p->dma_buf_pool_p[channel];
29016495Sspeer 	num_chunks = nxgep->tx_buf_pool_p->num_chunks[channel];
29026495Sspeer 	nxge_free_tx_buf_dma(nxgep, data, num_chunks);
29036495Sspeer 
29046495Sspeer 	nxgep->tx_buf_pool_p->dma_buf_pool_p[channel] = 0;
29056495Sspeer 	nxgep->tx_buf_pool_p->num_chunks[channel] = 0;
29066495Sspeer 
29076495Sspeer 	control = nxgep->tx_cntl_pool_p->dma_buf_pool_p[channel];
29086495Sspeer 	nxge_free_tx_cntl_dma(nxgep, control);
29096495Sspeer 
29106495Sspeer 	nxgep->tx_cntl_pool_p->dma_buf_pool_p[channel] = 0;
29116495Sspeer 
29126495Sspeer 	KMEM_FREE(data, sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK);
29136495Sspeer 	KMEM_FREE(control, sizeof (nxge_dma_common_t));
29146495Sspeer 
29156495Sspeer 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_free_txb"));
29166495Sspeer }
29176495Sspeer 
29186495Sspeer /*
29196495Sspeer  * nxge_alloc_tx_mem_pool
29206495Sspeer  *
29216495Sspeer  *	This function allocates all of the per-port TDC control data structures.
29226495Sspeer  *	The per-channel (TDC) data structures are allocated when needed.
29236495Sspeer  *
29246495Sspeer  * Arguments:
29256495Sspeer  * 	nxgep
29266495Sspeer  *
29276495Sspeer  * Notes:
29286495Sspeer  *
29296495Sspeer  * Context:
29306495Sspeer  *	Any domain
29316495Sspeer  */
29326495Sspeer nxge_status_t
29333859Sml29623 nxge_alloc_tx_mem_pool(p_nxge_t nxgep)
29343859Sml29623 {
29356495Sspeer 	nxge_hw_pt_cfg_t	*p_cfgp;
29366495Sspeer 	nxge_dma_pool_t		*dma_poolp;
29376495Sspeer 	nxge_dma_common_t	**dma_buf_p;
29386495Sspeer 	nxge_dma_pool_t		*dma_cntl_poolp;
29396495Sspeer 	nxge_dma_common_t	**dma_cntl_p;
29403859Sml29623 	uint32_t		*num_chunks; /* per dma */
29416495Sspeer 	int			tdc_max;
29423859Sml29623 
29433859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "==> nxge_alloc_tx_mem_pool"));
29443859Sml29623 
29456495Sspeer 	p_cfgp = &nxgep->pt_config.hw_config;
29466495Sspeer 	tdc_max = NXGE_MAX_TDCS;
29476495Sspeer 
29483859Sml29623 	/*
29493859Sml29623 	 * Allocate memory for each transmit DMA channel.
29503859Sml29623 	 */
29513859Sml29623 	dma_poolp = (p_nxge_dma_pool_t)KMEM_ZALLOC(sizeof (nxge_dma_pool_t),
29526512Ssowmini 	    KM_SLEEP);
29533859Sml29623 	dma_buf_p = (p_nxge_dma_common_t *)KMEM_ZALLOC(
29546512Ssowmini 	    sizeof (p_nxge_dma_common_t) * tdc_max, KM_SLEEP);
29553859Sml29623 
29563859Sml29623 	dma_cntl_poolp = (p_nxge_dma_pool_t)
29576512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_pool_t), KM_SLEEP);
29583859Sml29623 	dma_cntl_p = (p_nxge_dma_common_t *)KMEM_ZALLOC(
29596512Ssowmini 	    sizeof (p_nxge_dma_common_t) * tdc_max, KM_SLEEP);
29603859Sml29623 
29615770Sml29623 	if (nxge_tx_ring_size > TDC_DEFAULT_MAX) {
29625770Sml29623 		NXGE_DEBUG_MSG((nxgep, MEM_CTL,
29635770Sml29623 		    "nxge_alloc_tx_mem_pool: TDC too high %d, "
29645770Sml29623 		    "set to default %d",
29655770Sml29623 		    nxge_tx_ring_size, TDC_DEFAULT_MAX));
29665770Sml29623 		nxge_tx_ring_size = TDC_DEFAULT_MAX;
29675770Sml29623 	}
29685770Sml29623 
29693859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
29703859Sml29623 	/*
29713859Sml29623 	 * N2/NIU has limitation on the descriptor sizes (contiguous
29723859Sml29623 	 * memory allocation on data buffers to 4M (contig_mem_alloc)
29733859Sml29623 	 * and little endian for control buffers (must use the ddi/dki mem alloc
29743859Sml29623 	 * function). The transmit ring is limited to 8K (includes the
29753859Sml29623 	 * mailbox).
29763859Sml29623 	 */
29773859Sml29623 	if (nxgep->niu_type == N2_NIU) {
29783859Sml29623 		if ((nxge_tx_ring_size > NXGE_NIU_CONTIG_TX_MAX) ||
29796512Ssowmini 		    (!ISP2(nxge_tx_ring_size))) {
29803859Sml29623 			nxge_tx_ring_size = NXGE_NIU_CONTIG_TX_MAX;
29813859Sml29623 		}
29823859Sml29623 	}
29833859Sml29623 #endif
29843859Sml29623 
29853859Sml29623 	nxgep->nxge_port_tx_ring_size = nxge_tx_ring_size;
29863859Sml29623 
29873859Sml29623 	num_chunks = (uint32_t *)KMEM_ZALLOC(
29886512Ssowmini 	    sizeof (uint32_t) * tdc_max, KM_SLEEP);
29896495Sspeer 
29906495Sspeer 	dma_poolp->ndmas = p_cfgp->tdc.owned;
29913859Sml29623 	dma_poolp->num_chunks = num_chunks;
29923859Sml29623 	dma_poolp->dma_buf_pool_p = dma_buf_p;
29933859Sml29623 	nxgep->tx_buf_pool_p = dma_poolp;
29943859Sml29623 
29956495Sspeer 	dma_poolp->buf_allocated = B_TRUE;
29966495Sspeer 
29976495Sspeer 	dma_cntl_poolp->ndmas = p_cfgp->tdc.owned;
29983859Sml29623 	dma_cntl_poolp->dma_buf_pool_p = dma_cntl_p;
29993859Sml29623 	nxgep->tx_cntl_pool_p = dma_cntl_poolp;
30003859Sml29623 
30016495Sspeer 	dma_cntl_poolp->buf_allocated = B_TRUE;
30026495Sspeer 
30036495Sspeer 	nxgep->tx_rings =
30046495Sspeer 	    KMEM_ZALLOC(sizeof (tx_rings_t), KM_SLEEP);
30056495Sspeer 	nxgep->tx_rings->rings =
30066495Sspeer 	    KMEM_ZALLOC(sizeof (p_tx_ring_t) * tdc_max, KM_SLEEP);
30076495Sspeer 	nxgep->tx_mbox_areas_p =
30086495Sspeer 	    KMEM_ZALLOC(sizeof (tx_mbox_areas_t), KM_SLEEP);
30096495Sspeer 	nxgep->tx_mbox_areas_p->txmbox_areas_p =
30106495Sspeer 	    KMEM_ZALLOC(sizeof (p_tx_mbox_t) * tdc_max, KM_SLEEP);
30116495Sspeer 
30126495Sspeer 	nxgep->tx_rings->ndmas = p_cfgp->tdc.owned;
30136495Sspeer 
30143859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL,
30156512Ssowmini 	    "==> nxge_alloc_tx_mem_pool: ndmas %d poolp->ndmas %d",
30166512Ssowmini 	    tdc_max, dma_poolp->ndmas));
30176495Sspeer 
30186495Sspeer 	return (NXGE_OK);
30193859Sml29623 }
30203859Sml29623 
30216495Sspeer nxge_status_t
30223859Sml29623 nxge_alloc_tx_buf_dma(p_nxge_t nxgep, uint16_t dma_channel,
30233859Sml29623     p_nxge_dma_common_t *dmap, size_t alloc_size,
30243859Sml29623     size_t block_size, uint32_t *num_chunks)
30253859Sml29623 {
30263859Sml29623 	p_nxge_dma_common_t 	tx_dmap;
30273859Sml29623 	nxge_status_t		status = NXGE_OK;
30283859Sml29623 	size_t			total_alloc_size;
30293859Sml29623 	size_t			allocated = 0;
30303859Sml29623 	int			i, size_index, array_size;
30313859Sml29623 
30323859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_tx_buf_dma"));
30333859Sml29623 
30343859Sml29623 	tx_dmap = (p_nxge_dma_common_t)
30356512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK,
30366512Ssowmini 	    KM_SLEEP);
30373859Sml29623 
30383859Sml29623 	total_alloc_size = alloc_size;
30393859Sml29623 	i = 0;
30403859Sml29623 	size_index = 0;
30413859Sml29623 	array_size =  sizeof (alloc_sizes) /  sizeof (size_t);
30428661SSantwona.Behera@Sun.COM 	while ((size_index < array_size) &&
30438661SSantwona.Behera@Sun.COM 	    (alloc_sizes[size_index] < alloc_size))
30443859Sml29623 		size_index++;
30453859Sml29623 	if (size_index >= array_size) {
30463859Sml29623 		size_index = array_size - 1;
30473859Sml29623 	}
30483859Sml29623 
30493859Sml29623 	while ((allocated < total_alloc_size) &&
30506512Ssowmini 	    (size_index >= 0) && (i < NXGE_DMA_BLOCK)) {
30513859Sml29623 
30523859Sml29623 		tx_dmap[i].dma_chunk_index = i;
30533859Sml29623 		tx_dmap[i].block_size = block_size;
30543859Sml29623 		tx_dmap[i].alength = alloc_sizes[size_index];
30553859Sml29623 		tx_dmap[i].orig_alength = tx_dmap[i].alength;
30563859Sml29623 		tx_dmap[i].nblocks = alloc_sizes[size_index] / block_size;
30573859Sml29623 		tx_dmap[i].dma_channel = dma_channel;
30583859Sml29623 		tx_dmap[i].contig_alloc_type = B_FALSE;
30596495Sspeer 		tx_dmap[i].kmem_alloc_type = B_FALSE;
30603859Sml29623 
30613859Sml29623 		/*
30623859Sml29623 		 * N2/NIU: data buffers must be contiguous as the driver
30633859Sml29623 		 *	   needs to call Hypervisor api to set up
30643859Sml29623 		 *	   logical pages.
30653859Sml29623 		 */
30663859Sml29623 		if ((nxgep->niu_type == N2_NIU) && (NXGE_DMA_BLOCK == 1)) {
30673859Sml29623 			tx_dmap[i].contig_alloc_type = B_TRUE;
30683859Sml29623 		}
30693859Sml29623 
30703859Sml29623 		status = nxge_dma_mem_alloc(nxgep, nxge_force_dma,
30716512Ssowmini 		    &nxge_tx_dma_attr,
30726512Ssowmini 		    tx_dmap[i].alength,
30736512Ssowmini 		    &nxge_dev_buf_dma_acc_attr,
30746512Ssowmini 		    DDI_DMA_WRITE | DDI_DMA_STREAMING,
30756512Ssowmini 		    (p_nxge_dma_common_t)(&tx_dmap[i]));
30763859Sml29623 		if (status != NXGE_OK) {
30773859Sml29623 			size_index--;
30783859Sml29623 		} else {
30793859Sml29623 			i++;
30803859Sml29623 			allocated += alloc_sizes[size_index];
30813859Sml29623 		}
30823859Sml29623 	}
30833859Sml29623 
30843859Sml29623 	if (allocated < total_alloc_size) {
30855770Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
30865770Sml29623 		    "==> nxge_alloc_tx_buf_dma: not enough channel %d: "
30875770Sml29623 		    "allocated 0x%x requested 0x%x",
30885770Sml29623 		    dma_channel,
30895770Sml29623 		    allocated, total_alloc_size));
30905770Sml29623 		status = NXGE_ERROR;
30913859Sml29623 		goto nxge_alloc_tx_mem_fail1;
30923859Sml29623 	}
30933859Sml29623 
30945770Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
30955770Sml29623 	    "==> nxge_alloc_tx_buf_dma: Allocated for channel %d: "
30965770Sml29623 	    "allocated 0x%x requested 0x%x",
30975770Sml29623 	    dma_channel,
30985770Sml29623 	    allocated, total_alloc_size));
30995770Sml29623 
31003859Sml29623 	*num_chunks = i;
31013859Sml29623 	*dmap = tx_dmap;
31023859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
31036512Ssowmini 	    "==> nxge_alloc_tx_buf_dma dmap 0x%016llx num chunks %d",
31046512Ssowmini 	    *dmap, i));
31053859Sml29623 	goto nxge_alloc_tx_mem_exit;
31063859Sml29623 
31073859Sml29623 nxge_alloc_tx_mem_fail1:
31083859Sml29623 	KMEM_FREE(tx_dmap, sizeof (nxge_dma_common_t) * NXGE_DMA_BLOCK);
31093859Sml29623 
31103859Sml29623 nxge_alloc_tx_mem_exit:
31113859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
31126512Ssowmini 	    "<== nxge_alloc_tx_buf_dma status 0x%08x", status));
31133859Sml29623 
31143859Sml29623 	return (status);
31153859Sml29623 }
31163859Sml29623 
31173859Sml29623 /*ARGSUSED*/
31183859Sml29623 static void
31193859Sml29623 nxge_free_tx_buf_dma(p_nxge_t nxgep, p_nxge_dma_common_t dmap,
31203859Sml29623     uint32_t num_chunks)
31213859Sml29623 {
31223859Sml29623 	int		i;
31233859Sml29623 
31243859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "==> nxge_free_tx_buf_dma"));
31253859Sml29623 
31266495Sspeer 	if (dmap == 0)
31276495Sspeer 		return;
31286495Sspeer 
31293859Sml29623 	for (i = 0; i < num_chunks; i++) {
31303859Sml29623 		nxge_dma_mem_free(dmap++);
31313859Sml29623 	}
31323859Sml29623 
31333859Sml29623 	NXGE_DEBUG_MSG((nxgep, MEM_CTL, "<== nxge_free_tx_buf_dma"));
31343859Sml29623 }
31353859Sml29623 
31363859Sml29623 /*ARGSUSED*/
31376495Sspeer nxge_status_t
31383859Sml29623 nxge_alloc_tx_cntl_dma(p_nxge_t nxgep, uint16_t dma_channel,
31393859Sml29623     p_nxge_dma_common_t *dmap, size_t size)
31403859Sml29623 {
31413859Sml29623 	p_nxge_dma_common_t 	tx_dmap;
31423859Sml29623 	nxge_status_t		status = NXGE_OK;
31433859Sml29623 
31443859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_alloc_tx_cntl_dma"));
31453859Sml29623 	tx_dmap = (p_nxge_dma_common_t)
31466512Ssowmini 	    KMEM_ZALLOC(sizeof (nxge_dma_common_t), KM_SLEEP);
31473859Sml29623 
31483859Sml29623 	tx_dmap->contig_alloc_type = B_FALSE;
31496495Sspeer 	tx_dmap->kmem_alloc_type = B_FALSE;
31503859Sml29623 
31513859Sml29623 	status = nxge_dma_mem_alloc(nxgep, nxge_force_dma,
31526512Ssowmini 	    &nxge_desc_dma_attr,
31536512Ssowmini 	    size,
31546512Ssowmini 	    &nxge_dev_desc_dma_acc_attr,
31556512Ssowmini 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
31566512Ssowmini 	    tx_dmap);
31573859Sml29623 	if (status != NXGE_OK) {
31583859Sml29623 		goto nxge_alloc_tx_cntl_dma_fail1;
31593859Sml29623 	}
31603859Sml29623 
31613859Sml29623 	*dmap = tx_dmap;
31623859Sml29623 	goto nxge_alloc_tx_cntl_dma_exit;
31633859Sml29623 
31643859Sml29623 nxge_alloc_tx_cntl_dma_fail1:
31653859Sml29623 	KMEM_FREE(tx_dmap, sizeof (nxge_dma_common_t));
31663859Sml29623 
31673859Sml29623 nxge_alloc_tx_cntl_dma_exit:
31683859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL,
31696512Ssowmini 	    "<== nxge_alloc_tx_cntl_dma status 0x%08x", status));
31703859Sml29623 
31713859Sml29623 	return (status);
31723859Sml29623 }
31733859Sml29623 
31743859Sml29623 /*ARGSUSED*/
31753859Sml29623 static void
31763859Sml29623 nxge_free_tx_cntl_dma(p_nxge_t nxgep, p_nxge_dma_common_t dmap)
31773859Sml29623 {
31783859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "==> nxge_free_tx_cntl_dma"));
31793859Sml29623 
31806495Sspeer 	if (dmap == 0)
31816495Sspeer 		return;
31826495Sspeer 
31833859Sml29623 	nxge_dma_mem_free(dmap);
31843859Sml29623 
31853859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_free_tx_cntl_dma"));
31863859Sml29623 }
31873859Sml29623 
31886495Sspeer /*
31896495Sspeer  * nxge_free_tx_mem_pool
31906495Sspeer  *
31916495Sspeer  *	This function frees all of the per-port TDC control data structures.
31926495Sspeer  *	The per-channel (TDC) data structures are freed when the channel
31936495Sspeer  *	is stopped.
31946495Sspeer  *
31956495Sspeer  * Arguments:
31966495Sspeer  * 	nxgep
31976495Sspeer  *
31986495Sspeer  * Notes:
31996495Sspeer  *
32006495Sspeer  * Context:
32016495Sspeer  *	Any domain
32026495Sspeer  */
32033859Sml29623 static void
32043859Sml29623 nxge_free_tx_mem_pool(p_nxge_t nxgep)
32053859Sml29623 {
32066495Sspeer 	int tdc_max = NXGE_MAX_TDCS;
32076495Sspeer 
32086495Sspeer 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "==> nxge_free_tx_mem_pool"));
32096495Sspeer 
32106495Sspeer 	if (!nxgep->tx_buf_pool_p || !nxgep->tx_buf_pool_p->buf_allocated) {
32116495Sspeer 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
32126512Ssowmini 		    "<== nxge_free_tx_mem_pool "
32136512Ssowmini 		    "(null tx buf pool or buf not allocated"));
32143859Sml29623 		return;
32153859Sml29623 	}
32166495Sspeer 	if (!nxgep->tx_cntl_pool_p || !nxgep->tx_cntl_pool_p->buf_allocated) {
32176495Sspeer 		NXGE_DEBUG_MSG((nxgep, MEM2_CTL,
32186512Ssowmini 		    "<== nxge_free_tx_mem_pool "
32196512Ssowmini 		    "(null tx cntl buf pool or cntl buf not allocated"));
32203859Sml29623 		return;
32213859Sml29623 	}
32223859Sml29623 
32236495Sspeer 	/* 1. Free the mailboxes. */
32246495Sspeer 	KMEM_FREE(nxgep->tx_mbox_areas_p->txmbox_areas_p,
32256495Sspeer 	    sizeof (p_tx_mbox_t) * tdc_max);
32266495Sspeer 	KMEM_FREE(nxgep->tx_mbox_areas_p, sizeof (tx_mbox_areas_t));
32276495Sspeer 
32286495Sspeer 	nxgep->tx_mbox_areas_p = 0;
32296495Sspeer 
32306495Sspeer 	/* 2. Free the transmit ring arrays. */
32316495Sspeer 	KMEM_FREE(nxgep->tx_rings->rings,
32326495Sspeer 	    sizeof (p_tx_ring_t) * tdc_max);
32336495Sspeer 	KMEM_FREE(nxgep->tx_rings, sizeof (tx_rings_t));
32346495Sspeer 
32356495Sspeer 	nxgep->tx_rings = 0;
32366495Sspeer 
32376495Sspeer 	/* 3. Free the completion ring data structures. */
32386495Sspeer 	KMEM_FREE(nxgep->tx_cntl_pool_p->dma_buf_pool_p,
32396495Sspeer 	    sizeof (p_nxge_dma_common_t) * tdc_max);
32406495Sspeer 	KMEM_FREE(nxgep->tx_cntl_pool_p, sizeof (nxge_dma_pool_t));
32416495Sspeer 
32426495Sspeer 	nxgep->tx_cntl_pool_p = 0;
32436495Sspeer 
32446495Sspeer 	/* 4. Free the data ring data structures. */
32456495Sspeer 	KMEM_FREE(nxgep->tx_buf_pool_p->num_chunks,
32466495Sspeer 	    sizeof (uint32_t) * tdc_max);
32476495Sspeer 	KMEM_FREE(nxgep->tx_buf_pool_p->dma_buf_pool_p,
32486495Sspeer 	    sizeof (p_nxge_dma_common_t) * tdc_max);
32496495Sspeer 	KMEM_FREE(nxgep->tx_buf_pool_p, sizeof (nxge_dma_pool_t));
32506495Sspeer 
32516495Sspeer 	nxgep->tx_buf_pool_p = 0;
32526495Sspeer 
32536495Sspeer 	NXGE_DEBUG_MSG((nxgep, MEM2_CTL, "<== nxge_free_tx_mem_pool"));
32543859Sml29623 }
32553859Sml29623 
32563859Sml29623 /*ARGSUSED*/
32573859Sml29623 static nxge_status_t
32583859Sml29623 nxge_dma_mem_alloc(p_nxge_t nxgep, dma_method_t method,
32593859Sml29623 	struct ddi_dma_attr *dma_attrp,
32603859Sml29623 	size_t length, ddi_device_acc_attr_t *acc_attr_p, uint_t xfer_flags,
32613859Sml29623 	p_nxge_dma_common_t dma_p)
32623859Sml29623 {
32633859Sml29623 	caddr_t 		kaddrp;
32643859Sml29623 	int			ddi_status = DDI_SUCCESS;
32653859Sml29623 	boolean_t		contig_alloc_type;
32666495Sspeer 	boolean_t		kmem_alloc_type;
32673859Sml29623 
32683859Sml29623 	contig_alloc_type = dma_p->contig_alloc_type;
32693859Sml29623 
32703859Sml29623 	if (contig_alloc_type && (nxgep->niu_type != N2_NIU)) {
32713859Sml29623 		/*
32723859Sml29623 		 * contig_alloc_type for contiguous memory only allowed
32733859Sml29623 		 * for N2/NIU.
32743859Sml29623 		 */
32753859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
32766512Ssowmini 		    "nxge_dma_mem_alloc: alloc type not allowed (%d)",
32776512Ssowmini 		    dma_p->contig_alloc_type));
32783859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
32793859Sml29623 	}
32803859Sml29623 
32813859Sml29623 	dma_p->dma_handle = NULL;
32823859Sml29623 	dma_p->acc_handle = NULL;
32833859Sml29623 	dma_p->kaddrp = dma_p->last_kaddrp = NULL;
32843859Sml29623 	dma_p->first_ioaddr_pp = dma_p->last_ioaddr_pp = NULL;
32853859Sml29623 	ddi_status = ddi_dma_alloc_handle(nxgep->dip, dma_attrp,
32866512Ssowmini 	    DDI_DMA_DONTWAIT, NULL, &dma_p->dma_handle);
32873859Sml29623 	if (ddi_status != DDI_SUCCESS) {
32883859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
32896512Ssowmini 		    "nxge_dma_mem_alloc:ddi_dma_alloc_handle failed."));
32903859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
32913859Sml29623 	}
32923859Sml29623 
32936495Sspeer 	kmem_alloc_type = dma_p->kmem_alloc_type;
32946495Sspeer 
32953859Sml29623 	switch (contig_alloc_type) {
32963859Sml29623 	case B_FALSE:
32976495Sspeer 		switch (kmem_alloc_type) {
32986495Sspeer 		case B_FALSE:
32996495Sspeer 			ddi_status = ddi_dma_mem_alloc(dma_p->dma_handle,
33006512Ssowmini 			    length,
33016512Ssowmini 			    acc_attr_p,
33026512Ssowmini 			    xfer_flags,
33036512Ssowmini 			    DDI_DMA_DONTWAIT, 0, &kaddrp, &dma_p->alength,
33046512Ssowmini 			    &dma_p->acc_handle);
33056495Sspeer 			if (ddi_status != DDI_SUCCESS) {
33066495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33076495Sspeer 				    "nxge_dma_mem_alloc: "
33086495Sspeer 				    "ddi_dma_mem_alloc failed"));
33096495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
33106495Sspeer 				dma_p->dma_handle = NULL;
33116495Sspeer 				return (NXGE_ERROR | NXGE_DDI_FAILED);
33126495Sspeer 			}
33136495Sspeer 			if (dma_p->alength < length) {
33146495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33156495Sspeer 				    "nxge_dma_mem_alloc:di_dma_mem_alloc "
33166495Sspeer 				    "< length."));
33176495Sspeer 				ddi_dma_mem_free(&dma_p->acc_handle);
33186495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
33196495Sspeer 				dma_p->acc_handle = NULL;
33206495Sspeer 				dma_p->dma_handle = NULL;
33216495Sspeer 				return (NXGE_ERROR);
33226495Sspeer 			}
33236495Sspeer 
33246495Sspeer 			ddi_status = ddi_dma_addr_bind_handle(dma_p->dma_handle,
33256495Sspeer 			    NULL,
33266495Sspeer 			    kaddrp, dma_p->alength, xfer_flags,
33276495Sspeer 			    DDI_DMA_DONTWAIT,
33286495Sspeer 			    0, &dma_p->dma_cookie, &dma_p->ncookies);
33296495Sspeer 			if (ddi_status != DDI_DMA_MAPPED) {
33306495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33316495Sspeer 				    "nxge_dma_mem_alloc: ddi_dma_addr_bind "
33326495Sspeer 				    "failed "
33336495Sspeer 				    "(staus 0x%x ncookies %d.)", ddi_status,
33346495Sspeer 				    dma_p->ncookies));
33356495Sspeer 				if (dma_p->acc_handle) {
33366495Sspeer 					ddi_dma_mem_free(&dma_p->acc_handle);
33376495Sspeer 					dma_p->acc_handle = NULL;
33386495Sspeer 				}
33396495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
33406495Sspeer 				dma_p->dma_handle = NULL;
33416495Sspeer 				return (NXGE_ERROR | NXGE_DDI_FAILED);
33426495Sspeer 			}
33436495Sspeer 
33446495Sspeer 			if (dma_p->ncookies != 1) {
33456495Sspeer 				NXGE_DEBUG_MSG((nxgep, DMA_CTL,
33466495Sspeer 				    "nxge_dma_mem_alloc:ddi_dma_addr_bind "
33476495Sspeer 				    "> 1 cookie"
33486495Sspeer 				    "(staus 0x%x ncookies %d.)", ddi_status,
33496495Sspeer 				    dma_p->ncookies));
33507812SMichael.Speer@Sun.COM 				(void) ddi_dma_unbind_handle(dma_p->dma_handle);
33516495Sspeer 				if (dma_p->acc_handle) {
33526495Sspeer 					ddi_dma_mem_free(&dma_p->acc_handle);
33536495Sspeer 					dma_p->acc_handle = NULL;
33546495Sspeer 				}
33556495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
33566495Sspeer 				dma_p->dma_handle = NULL;
33577812SMichael.Speer@Sun.COM 				dma_p->acc_handle = NULL;
33586495Sspeer 				return (NXGE_ERROR);
33596495Sspeer 			}
33606495Sspeer 			break;
33616495Sspeer 
33626495Sspeer 		case B_TRUE:
33636495Sspeer 			kaddrp = KMEM_ALLOC(length, KM_NOSLEEP);
33646495Sspeer 			if (kaddrp == NULL) {
33656495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33666495Sspeer 				    "nxge_dma_mem_alloc:ddi_dma_mem_alloc "
33676495Sspeer 				    "kmem alloc failed"));
33686495Sspeer 				return (NXGE_ERROR);
33696495Sspeer 			}
33706495Sspeer 
33716495Sspeer 			dma_p->alength = length;
33726495Sspeer 			ddi_status = ddi_dma_addr_bind_handle(dma_p->dma_handle,
33736495Sspeer 			    NULL, kaddrp, dma_p->alength, xfer_flags,
33746495Sspeer 			    DDI_DMA_DONTWAIT, 0,
33756495Sspeer 			    &dma_p->dma_cookie, &dma_p->ncookies);
33766495Sspeer 			if (ddi_status != DDI_DMA_MAPPED) {
33776495Sspeer 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
33786495Sspeer 				    "nxge_dma_mem_alloc:ddi_dma_addr_bind: "
33796495Sspeer 				    "(kmem_alloc) failed kaddrp $%p length %d "
33806495Sspeer 				    "(staus 0x%x (%d) ncookies %d.)",
33816495Sspeer 				    kaddrp, length,
33826495Sspeer 				    ddi_status, ddi_status, dma_p->ncookies));
33836495Sspeer 				KMEM_FREE(kaddrp, length);
33846495Sspeer 				dma_p->acc_handle = NULL;
33856495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
33866495Sspeer 				dma_p->dma_handle = NULL;
33876495Sspeer 				dma_p->kaddrp = NULL;
33886495Sspeer 				return (NXGE_ERROR | NXGE_DDI_FAILED);
33896495Sspeer 			}
33906495Sspeer 
33916495Sspeer 			if (dma_p->ncookies != 1) {
33926495Sspeer 				NXGE_DEBUG_MSG((nxgep, DMA_CTL,
33936495Sspeer 				    "nxge_dma_mem_alloc:ddi_dma_addr_bind "
33946495Sspeer 				    "(kmem_alloc) > 1 cookie"
33956495Sspeer 				    "(staus 0x%x ncookies %d.)", ddi_status,
33966512Ssowmini 				    dma_p->ncookies));
33977812SMichael.Speer@Sun.COM 				(void) ddi_dma_unbind_handle(dma_p->dma_handle);
33986495Sspeer 				KMEM_FREE(kaddrp, length);
33996495Sspeer 				ddi_dma_free_handle(&dma_p->dma_handle);
34006495Sspeer 				dma_p->dma_handle = NULL;
34017812SMichael.Speer@Sun.COM 				dma_p->acc_handle = NULL;
34026495Sspeer 				dma_p->kaddrp = NULL;
34036495Sspeer 				return (NXGE_ERROR);
34043859Sml29623 			}
34056495Sspeer 
34066495Sspeer 			dma_p->kaddrp = kaddrp;
34076495Sspeer 
34086495Sspeer 			NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
34096512Ssowmini 			    "nxge_dma_mem_alloc: kmem_alloc dmap $%p "
34106512Ssowmini 			    "kaddr $%p alength %d",
34116512Ssowmini 			    dma_p,
34126512Ssowmini 			    kaddrp,
34136512Ssowmini 			    dma_p->alength));
34146495Sspeer 			break;
34153859Sml29623 		}
34163859Sml29623 		break;
34173859Sml29623 
34183859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
34193859Sml29623 	case B_TRUE:
34203859Sml29623 		kaddrp = (caddr_t)contig_mem_alloc(length);
34213859Sml29623 		if (kaddrp == NULL) {
34223859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
34236512Ssowmini 			    "nxge_dma_mem_alloc:contig_mem_alloc failed."));
34243859Sml29623 			ddi_dma_free_handle(&dma_p->dma_handle);
34253859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
34263859Sml29623 		}
34273859Sml29623 
34283859Sml29623 		dma_p->alength = length;
34293859Sml29623 		ddi_status = ddi_dma_addr_bind_handle(dma_p->dma_handle, NULL,
34306512Ssowmini 		    kaddrp, dma_p->alength, xfer_flags, DDI_DMA_DONTWAIT, 0,
34316512Ssowmini 		    &dma_p->dma_cookie, &dma_p->ncookies);
34323859Sml29623 		if (ddi_status != DDI_DMA_MAPPED) {
34333859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
34346512Ssowmini 			    "nxge_dma_mem_alloc:di_dma_addr_bind failed "
34356512Ssowmini 			    "(status 0x%x ncookies %d.)", ddi_status,
34366512Ssowmini 			    dma_p->ncookies));
34373859Sml29623 
34383859Sml29623 			NXGE_DEBUG_MSG((nxgep, DMA_CTL,
34396512Ssowmini 			    "==> nxge_dma_mem_alloc: (not mapped)"
34406512Ssowmini 			    "length %lu (0x%x) "
34416512Ssowmini 			    "free contig kaddrp $%p "
34426512Ssowmini 			    "va_to_pa $%p",
34436512Ssowmini 			    length, length,
34446512Ssowmini 			    kaddrp,
34456512Ssowmini 			    va_to_pa(kaddrp)));
34463859Sml29623 
34473859Sml29623 
34483859Sml29623 			contig_mem_free((void *)kaddrp, length);
34493859Sml29623 			ddi_dma_free_handle(&dma_p->dma_handle);
34503859Sml29623 
34513859Sml29623 			dma_p->dma_handle = NULL;
34523859Sml29623 			dma_p->acc_handle = NULL;
34533859Sml29623 			dma_p->alength = NULL;
34543859Sml29623 			dma_p->kaddrp = NULL;
34553859Sml29623 
34563859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
34573859Sml29623 		}
34583859Sml29623 
34593859Sml29623 		if (dma_p->ncookies != 1 ||
34606512Ssowmini 		    (dma_p->dma_cookie.dmac_laddress == NULL)) {
34613859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
34626512Ssowmini 			    "nxge_dma_mem_alloc:di_dma_addr_bind > 1 "
34636512Ssowmini 			    "cookie or "
34646512Ssowmini 			    "dmac_laddress is NULL $%p size %d "
34656512Ssowmini 			    " (status 0x%x ncookies %d.)",
34666512Ssowmini 			    ddi_status,
34676512Ssowmini 			    dma_p->dma_cookie.dmac_laddress,
34686512Ssowmini 			    dma_p->dma_cookie.dmac_size,
34696512Ssowmini 			    dma_p->ncookies));
34703859Sml29623 
34713859Sml29623 			contig_mem_free((void *)kaddrp, length);
34724185Sspeer 			(void) ddi_dma_unbind_handle(dma_p->dma_handle);
34733859Sml29623 			ddi_dma_free_handle(&dma_p->dma_handle);
34743859Sml29623 
34753859Sml29623 			dma_p->alength = 0;
34763859Sml29623 			dma_p->dma_handle = NULL;
34773859Sml29623 			dma_p->acc_handle = NULL;
34783859Sml29623 			dma_p->kaddrp = NULL;
34793859Sml29623 
34803859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
34813859Sml29623 		}
34823859Sml29623 		break;
34833859Sml29623 
34843859Sml29623 #else
34853859Sml29623 	case B_TRUE:
34863859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
34876512Ssowmini 		    "nxge_dma_mem_alloc: invalid alloc type for !sun4v"));
34883859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
34893859Sml29623 #endif
34903859Sml29623 	}
34913859Sml29623 
34923859Sml29623 	dma_p->kaddrp = kaddrp;
34933859Sml29623 	dma_p->last_kaddrp = (unsigned char *)kaddrp +
34946512Ssowmini 	    dma_p->alength - RXBUF_64B_ALIGNED;
34955125Sjoycey #if defined(__i386)
34965125Sjoycey 	dma_p->ioaddr_pp =
34976512Ssowmini 	    (unsigned char *)(uint32_t)dma_p->dma_cookie.dmac_laddress;
34985125Sjoycey #else
34993859Sml29623 	dma_p->ioaddr_pp = (unsigned char *)dma_p->dma_cookie.dmac_laddress;
35005125Sjoycey #endif
35013859Sml29623 	dma_p->last_ioaddr_pp =
35025125Sjoycey #if defined(__i386)
35036512Ssowmini 	    (unsigned char *)(uint32_t)dma_p->dma_cookie.dmac_laddress +
35045125Sjoycey #else
35056512Ssowmini 	    (unsigned char *)dma_p->dma_cookie.dmac_laddress +
35065125Sjoycey #endif
35076512Ssowmini 	    dma_p->alength - RXBUF_64B_ALIGNED;
35083859Sml29623 
35093859Sml29623 	NPI_DMA_ACC_HANDLE_SET(dma_p, dma_p->acc_handle);
35103859Sml29623 
35113859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
35123859Sml29623 	dma_p->orig_ioaddr_pp =
35136512Ssowmini 	    (unsigned char *)dma_p->dma_cookie.dmac_laddress;
35143859Sml29623 	dma_p->orig_alength = length;
35153859Sml29623 	dma_p->orig_kaddrp = kaddrp;
35163859Sml29623 	dma_p->orig_vatopa = (uint64_t)va_to_pa(kaddrp);
35173859Sml29623 #endif
35183859Sml29623 
35193859Sml29623 	NXGE_DEBUG_MSG((nxgep, DMA_CTL, "<== nxge_dma_mem_alloc: "
35206512Ssowmini 	    "dma buffer allocated: dma_p $%p "
35216512Ssowmini 	    "return dmac_ladress from cookie $%p cookie dmac_size %d "
35226512Ssowmini 	    "dma_p->ioaddr_p $%p "
35236512Ssowmini 	    "dma_p->orig_ioaddr_p $%p "
35246512Ssowmini 	    "orig_vatopa $%p "
35256512Ssowmini 	    "alength %d (0x%x) "
35266512Ssowmini 	    "kaddrp $%p "
35276512Ssowmini 	    "length %d (0x%x)",
35286512Ssowmini 	    dma_p,
35296512Ssowmini 	    dma_p->dma_cookie.dmac_laddress, dma_p->dma_cookie.dmac_size,
35306512Ssowmini 	    dma_p->ioaddr_pp,
35316512Ssowmini 	    dma_p->orig_ioaddr_pp,
35326512Ssowmini 	    dma_p->orig_vatopa,
35336512Ssowmini 	    dma_p->alength, dma_p->alength,
35346512Ssowmini 	    kaddrp,
35356512Ssowmini 	    length, length));
35363859Sml29623 
35373859Sml29623 	return (NXGE_OK);
35383859Sml29623 }
35393859Sml29623 
35403859Sml29623 static void
35413859Sml29623 nxge_dma_mem_free(p_nxge_dma_common_t dma_p)
35423859Sml29623 {
35433859Sml29623 	if (dma_p->dma_handle != NULL) {
35443859Sml29623 		if (dma_p->ncookies) {
35453859Sml29623 			(void) ddi_dma_unbind_handle(dma_p->dma_handle);
35463859Sml29623 			dma_p->ncookies = 0;
35473859Sml29623 		}
35483859Sml29623 		ddi_dma_free_handle(&dma_p->dma_handle);
35493859Sml29623 		dma_p->dma_handle = NULL;
35503859Sml29623 	}
35513859Sml29623 
35523859Sml29623 	if (dma_p->acc_handle != NULL) {
35533859Sml29623 		ddi_dma_mem_free(&dma_p->acc_handle);
35543859Sml29623 		dma_p->acc_handle = NULL;
35553859Sml29623 		NPI_DMA_ACC_HANDLE_SET(dma_p, NULL);
35563859Sml29623 	}
35573859Sml29623 
35583859Sml29623 #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
35593859Sml29623 	if (dma_p->contig_alloc_type &&
35606512Ssowmini 	    dma_p->orig_kaddrp && dma_p->orig_alength) {
35613859Sml29623 		NXGE_DEBUG_MSG((NULL, DMA_CTL, "nxge_dma_mem_free: "
35626512Ssowmini 		    "kaddrp $%p (orig_kaddrp $%p)"
35636512Ssowmini 		    "mem type %d ",
35646512Ssowmini 		    "orig_alength %d "
35656512Ssowmini 		    "alength 0x%x (%d)",
35666512Ssowmini 		    dma_p->kaddrp,
35676512Ssowmini 		    dma_p->orig_kaddrp,
35686512Ssowmini 		    dma_p->contig_alloc_type,
35696512Ssowmini 		    dma_p->orig_alength,
35706512Ssowmini 		    dma_p->alength, dma_p->alength));
35713859Sml29623 
35723859Sml29623 		contig_mem_free(dma_p->orig_kaddrp, dma_p->orig_alength);
35733859Sml29623 		dma_p->orig_alength = NULL;
35743859Sml29623 		dma_p->orig_kaddrp = NULL;
35753859Sml29623 		dma_p->contig_alloc_type = B_FALSE;
35763859Sml29623 	}
35773859Sml29623 #endif
35783859Sml29623 	dma_p->kaddrp = NULL;
35793859Sml29623 	dma_p->alength = NULL;
35803859Sml29623 }
35813859Sml29623 
35826495Sspeer static void
35836495Sspeer nxge_dma_free_rx_data_buf(p_nxge_dma_common_t dma_p)
35846495Sspeer {
35856495Sspeer 	uint64_t kaddr;
35866495Sspeer 	uint32_t buf_size;
35876495Sspeer 
35886495Sspeer 	NXGE_DEBUG_MSG((NULL, DMA_CTL, "==> nxge_dma_free_rx_data_buf"));
35896495Sspeer 
35906495Sspeer 	if (dma_p->dma_handle != NULL) {
35916495Sspeer 		if (dma_p->ncookies) {
35926495Sspeer 			(void) ddi_dma_unbind_handle(dma_p->dma_handle);
35936495Sspeer 			dma_p->ncookies = 0;
35946495Sspeer 		}
35956495Sspeer 		ddi_dma_free_handle(&dma_p->dma_handle);
35966495Sspeer 		dma_p->dma_handle = NULL;
35976495Sspeer 	}
35986495Sspeer 
35996495Sspeer 	if (dma_p->acc_handle != NULL) {
36006495Sspeer 		ddi_dma_mem_free(&dma_p->acc_handle);
36016495Sspeer 		dma_p->acc_handle = NULL;
36026495Sspeer 		NPI_DMA_ACC_HANDLE_SET(dma_p, NULL);
36036495Sspeer 	}
36046495Sspeer 
36056495Sspeer 	NXGE_DEBUG_MSG((NULL, DMA_CTL,
36066495Sspeer 	    "==> nxge_dma_free_rx_data_buf: dmap $%p buf_alloc_state %d",
36076495Sspeer 	    dma_p,
36086495Sspeer 	    dma_p->buf_alloc_state));
36096495Sspeer 
36106495Sspeer 	if (!(dma_p->buf_alloc_state & BUF_ALLOCATED_WAIT_FREE)) {
36116495Sspeer 		NXGE_DEBUG_MSG((NULL, DMA_CTL,
36126495Sspeer 		    "<== nxge_dma_free_rx_data_buf: "
36136495Sspeer 		    "outstanding data buffers"));
36146495Sspeer 		return;
36156495Sspeer 	}
36166495Sspeer 
36176495Sspeer #if	defined(sun4v) && defined(NIU_LP_WORKAROUND)
36186495Sspeer 	if (dma_p->contig_alloc_type &&
36196512Ssowmini 	    dma_p->orig_kaddrp && dma_p->orig_alength) {
36206495Sspeer 		NXGE_DEBUG_MSG((NULL, DMA_CTL, "nxge_dma_free_rx_data_buf: "
36216495Sspeer 		    "kaddrp $%p (orig_kaddrp $%p)"
36226495Sspeer 		    "mem type %d ",
36236495Sspeer 		    "orig_alength %d "
36246495Sspeer 		    "alength 0x%x (%d)",
36256495Sspeer 		    dma_p->kaddrp,
36266495Sspeer 		    dma_p->orig_kaddrp,
36276495Sspeer 		    dma_p->contig_alloc_type,
36286495Sspeer 		    dma_p->orig_alength,
36296495Sspeer 		    dma_p->alength, dma_p->alength));
36306495Sspeer 
36316495Sspeer 		kaddr = (uint64_t)dma_p->orig_kaddrp;
36326495Sspeer 		buf_size = dma_p->orig_alength;
36336495Sspeer 		nxge_free_buf(CONTIG_MEM_ALLOC, kaddr, buf_size);
36346495Sspeer 		dma_p->orig_alength = NULL;
36356495Sspeer 		dma_p->orig_kaddrp = NULL;
36366495Sspeer 		dma_p->contig_alloc_type = B_FALSE;
36376495Sspeer 		dma_p->kaddrp = NULL;
36386495Sspeer 		dma_p->alength = NULL;
36396495Sspeer 		return;
36406495Sspeer 	}
36416495Sspeer #endif
36426495Sspeer 
36436495Sspeer 	if (dma_p->kmem_alloc_type) {
36446495Sspeer 		NXGE_DEBUG_MSG((NULL, DMA_CTL,
36456495Sspeer 		    "nxge_dma_free_rx_data_buf: free kmem "
36466512Ssowmini 		    "kaddrp $%p (orig_kaddrp $%p)"
36476512Ssowmini 		    "alloc type %d "
36486512Ssowmini 		    "orig_alength %d "
36496512Ssowmini 		    "alength 0x%x (%d)",
36506512Ssowmini 		    dma_p->kaddrp,
36516512Ssowmini 		    dma_p->orig_kaddrp,
36526512Ssowmini 		    dma_p->kmem_alloc_type,
36536512Ssowmini 		    dma_p->orig_alength,
36546512Ssowmini 		    dma_p->alength, dma_p->alength));
36556495Sspeer #if defined(__i386)
36566495Sspeer 		kaddr = (uint64_t)(uint32_t)dma_p->kaddrp;
36576495Sspeer #else
36586495Sspeer 		kaddr = (uint64_t)dma_p->kaddrp;
36596495Sspeer #endif
36606495Sspeer 		buf_size = dma_p->orig_alength;
36616495Sspeer 		NXGE_DEBUG_MSG((NULL, DMA_CTL,
36626495Sspeer 		    "nxge_dma_free_rx_data_buf: free dmap $%p "
36636495Sspeer 		    "kaddr $%p buf_size %d",
36646495Sspeer 		    dma_p,
36656495Sspeer 		    kaddr, buf_size));
36666495Sspeer 		nxge_free_buf(KMEM_ALLOC, kaddr, buf_size);
36676495Sspeer 		dma_p->alength = 0;
36686495Sspeer 		dma_p->orig_alength = 0;
36696495Sspeer 		dma_p->kaddrp = NULL;
36706495Sspeer 		dma_p->kmem_alloc_type = B_FALSE;
36716495Sspeer 	}
36726495Sspeer 
36736495Sspeer 	NXGE_DEBUG_MSG((NULL, DMA_CTL, "<== nxge_dma_free_rx_data_buf"));
36746495Sspeer }
36756495Sspeer 
36763859Sml29623 /*
36773859Sml29623  *	nxge_m_start() -- start transmitting and receiving.
36783859Sml29623  *
36793859Sml29623  *	This function is called by the MAC layer when the first
36803859Sml29623  *	stream is open to prepare the hardware ready for sending
36813859Sml29623  *	and transmitting packets.
36823859Sml29623  */
36833859Sml29623 static int
36843859Sml29623 nxge_m_start(void *arg)
36853859Sml29623 {
36863859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
36873859Sml29623 
36883859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_start"));
36893859Sml29623 
36906705Sml29623 	if (nxge_peu_reset_enable && !nxgep->nxge_link_poll_timerid) {
36916705Sml29623 		(void) nxge_link_monitor(nxgep, LINK_MONITOR_START);
36926705Sml29623 	}
36936705Sml29623 
36943859Sml29623 	MUTEX_ENTER(nxgep->genlock);
36953859Sml29623 	if (nxge_init(nxgep) != NXGE_OK) {
36963859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
36976512Ssowmini 		    "<== nxge_m_start: initialization failed"));
36983859Sml29623 		MUTEX_EXIT(nxgep->genlock);
36993859Sml29623 		return (EIO);
37003859Sml29623 	}
37013859Sml29623 
37023859Sml29623 	if (nxgep->nxge_mac_state == NXGE_MAC_STARTED)
37033859Sml29623 		goto nxge_m_start_exit;
37043859Sml29623 	/*
37053859Sml29623 	 * Start timer to check the system error and tx hangs
37063859Sml29623 	 */
37076495Sspeer 	if (!isLDOMguest(nxgep))
37086495Sspeer 		nxgep->nxge_timerid = nxge_start_timer(nxgep,
37096495Sspeer 		    nxge_check_hw_state, NXGE_CHECK_TIMER);
37106495Sspeer #if	defined(sun4v)
37116495Sspeer 	else
37126495Sspeer 		nxge_hio_start_timer(nxgep);
37136495Sspeer #endif
37143859Sml29623 
37153859Sml29623 	nxgep->link_notify = B_TRUE;
37163859Sml29623 
37173859Sml29623 	nxgep->nxge_mac_state = NXGE_MAC_STARTED;
37183859Sml29623 
37193859Sml29623 nxge_m_start_exit:
37203859Sml29623 	MUTEX_EXIT(nxgep->genlock);
37213859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_start"));
37223859Sml29623 	return (0);
37233859Sml29623 }
37243859Sml29623 
37258275SEric Cheng 
37268275SEric Cheng static boolean_t
37278275SEric Cheng nxge_check_groups_stopped(p_nxge_t nxgep)
37288275SEric Cheng {
37298275SEric Cheng 	int	i;
37308275SEric Cheng 
37318275SEric Cheng 	for (i = 0; i < NXGE_MAX_RDC_GROUPS; i++) {
37328275SEric Cheng 		if (nxgep->rx_hio_groups[i].started)
37338275SEric Cheng 			return (B_FALSE);
37348275SEric Cheng 	}
37358275SEric Cheng 
37368275SEric Cheng 	return (B_TRUE);
37378275SEric Cheng }
37388275SEric Cheng 
37393859Sml29623 /*
37403859Sml29623  *	nxge_m_stop(): stop transmitting and receiving.
37413859Sml29623  */
37423859Sml29623 static void
37433859Sml29623 nxge_m_stop(void *arg)
37443859Sml29623 {
37453859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
37468275SEric Cheng 	boolean_t	groups_stopped;
37473859Sml29623 
37483859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_stop"));
37493859Sml29623 
37508275SEric Cheng 	groups_stopped = nxge_check_groups_stopped(nxgep);
37518275SEric Cheng #ifdef later
37528275SEric Cheng 	ASSERT(groups_stopped == B_FALSE);
37538275SEric Cheng #endif
37548275SEric Cheng 
37558275SEric Cheng 	if (!groups_stopped) {
37568275SEric Cheng 		cmn_err(CE_WARN, "nxge(%d): groups are not stopped!\n",
37578275SEric Cheng 		    nxgep->instance);
37588275SEric Cheng 		return;
37598275SEric Cheng 	}
37608275SEric Cheng 
37617466SMisaki.Kataoka@Sun.COM 	MUTEX_ENTER(nxgep->genlock);
37627466SMisaki.Kataoka@Sun.COM 	nxgep->nxge_mac_state = NXGE_MAC_STOPPING;
37637466SMisaki.Kataoka@Sun.COM 
37643859Sml29623 	if (nxgep->nxge_timerid) {
37653859Sml29623 		nxge_stop_timer(nxgep, nxgep->nxge_timerid);
37663859Sml29623 		nxgep->nxge_timerid = 0;
37673859Sml29623 	}
37683859Sml29623 
37693859Sml29623 	nxge_uninit(nxgep);
37703859Sml29623 
37713859Sml29623 	nxgep->nxge_mac_state = NXGE_MAC_STOPPED;
37723859Sml29623 
37733859Sml29623 	MUTEX_EXIT(nxgep->genlock);
37743859Sml29623 
37753859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_stop"));
37763859Sml29623 }
37773859Sml29623 
37783859Sml29623 static int
37793859Sml29623 nxge_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
37803859Sml29623 {
37813859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
37823859Sml29623 	struct 		ether_addr addrp;
37833859Sml29623 
37843859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL,
37856512Ssowmini 	    "==> nxge_m_multicst: add %d", add));
37863859Sml29623 
37873859Sml29623 	bcopy(mca, (uint8_t *)&addrp, ETHERADDRL);
37883859Sml29623 	if (add) {
37893859Sml29623 		if (nxge_add_mcast_addr(nxgep, &addrp)) {
37903859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
37916512Ssowmini 			    "<== nxge_m_multicst: add multicast failed"));
37923859Sml29623 			return (EINVAL);
37933859Sml29623 		}
37943859Sml29623 	} else {
37953859Sml29623 		if (nxge_del_mcast_addr(nxgep, &addrp)) {
37963859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
37976512Ssowmini 			    "<== nxge_m_multicst: del multicast failed"));
37983859Sml29623 			return (EINVAL);
37993859Sml29623 		}
38003859Sml29623 	}
38013859Sml29623 
38023859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL, "<== nxge_m_multicst"));
38033859Sml29623 
38043859Sml29623 	return (0);
38053859Sml29623 }
38063859Sml29623 
38073859Sml29623 static int
38083859Sml29623 nxge_m_promisc(void *arg, boolean_t on)
38093859Sml29623 {
38103859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
38113859Sml29623 
38123859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL,
38136512Ssowmini 	    "==> nxge_m_promisc: on %d", on));
38143859Sml29623 
38153859Sml29623 	if (nxge_set_promisc(nxgep, on)) {
38163859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
38176512Ssowmini 		    "<== nxge_m_promisc: set promisc failed"));
38183859Sml29623 		return (EINVAL);
38193859Sml29623 	}
38203859Sml29623 
38213859Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL,
38226512Ssowmini 	    "<== nxge_m_promisc: on %d", on));
38233859Sml29623 
38243859Sml29623 	return (0);
38253859Sml29623 }
38263859Sml29623 
38273859Sml29623 static void
38283859Sml29623 nxge_m_ioctl(void *arg,  queue_t *wq, mblk_t *mp)
38293859Sml29623 {
38303859Sml29623 	p_nxge_t 	nxgep = (p_nxge_t)arg;
38314185Sspeer 	struct 		iocblk *iocp;
38323859Sml29623 	boolean_t 	need_privilege;
38333859Sml29623 	int 		err;
38343859Sml29623 	int 		cmd;
38353859Sml29623 
38363859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_ioctl"));
38373859Sml29623 
38383859Sml29623 	iocp = (struct iocblk *)mp->b_rptr;
38393859Sml29623 	iocp->ioc_error = 0;
38403859Sml29623 	need_privilege = B_TRUE;
38413859Sml29623 	cmd = iocp->ioc_cmd;
38423859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_ioctl: cmd 0x%08x", cmd));
38433859Sml29623 	switch (cmd) {
38443859Sml29623 	default:
38453859Sml29623 		miocnak(wq, mp, 0, EINVAL);
38463859Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_ioctl: invalid"));
38473859Sml29623 		return;
38483859Sml29623 
38493859Sml29623 	case LB_GET_INFO_SIZE:
38503859Sml29623 	case LB_GET_INFO:
38513859Sml29623 	case LB_GET_MODE:
38523859Sml29623 		need_privilege = B_FALSE;
38533859Sml29623 		break;
38543859Sml29623 	case LB_SET_MODE:
38553859Sml29623 		break;
38563859Sml29623 
38573859Sml29623 
38583859Sml29623 	case NXGE_GET_MII:
38593859Sml29623 	case NXGE_PUT_MII:
38603859Sml29623 	case NXGE_GET64:
38613859Sml29623 	case NXGE_PUT64:
38623859Sml29623 	case NXGE_GET_TX_RING_SZ:
38633859Sml29623 	case NXGE_GET_TX_DESC:
38643859Sml29623 	case NXGE_TX_SIDE_RESET:
38653859Sml29623 	case NXGE_RX_SIDE_RESET:
38663859Sml29623 	case NXGE_GLOBAL_RESET:
38673859Sml29623 	case NXGE_RESET_MAC:
38683859Sml29623 	case NXGE_TX_REGS_DUMP:
38693859Sml29623 	case NXGE_RX_REGS_DUMP:
38703859Sml29623 	case NXGE_INT_REGS_DUMP:
38713859Sml29623 	case NXGE_VIR_INT_REGS_DUMP:
38723859Sml29623 	case NXGE_PUT_TCAM:
38733859Sml29623 	case NXGE_GET_TCAM:
38743859Sml29623 	case NXGE_RTRACE:
38753859Sml29623 	case NXGE_RDUMP:
38763859Sml29623 
38773859Sml29623 		need_privilege = B_FALSE;
38783859Sml29623 		break;
38793859Sml29623 	case NXGE_INJECT_ERR:
38803859Sml29623 		cmn_err(CE_NOTE, "!nxge_m_ioctl: Inject error\n");
38813859Sml29623 		nxge_err_inject(nxgep, wq, mp);
38823859Sml29623 		break;
38833859Sml29623 	}
38843859Sml29623 
38853859Sml29623 	if (need_privilege) {
38864185Sspeer 		err = secpolicy_net_config(iocp->ioc_cr, B_FALSE);
38873859Sml29623 		if (err != 0) {
38883859Sml29623 			miocnak(wq, mp, 0, err);
38893859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
38906512Ssowmini 			    "<== nxge_m_ioctl: no priv"));
38913859Sml29623 			return;
38923859Sml29623 		}
38933859Sml29623 	}
38943859Sml29623 
38953859Sml29623 	switch (cmd) {
38963859Sml29623 
38973859Sml29623 	case LB_GET_MODE:
38983859Sml29623 	case LB_SET_MODE:
38993859Sml29623 	case LB_GET_INFO_SIZE:
39003859Sml29623 	case LB_GET_INFO:
39013859Sml29623 		nxge_loopback_ioctl(nxgep, wq, mp, iocp);
39023859Sml29623 		break;
39033859Sml29623 
39043859Sml29623 	case NXGE_GET_MII:
39053859Sml29623 	case NXGE_PUT_MII:
39063859Sml29623 	case NXGE_PUT_TCAM:
39073859Sml29623 	case NXGE_GET_TCAM:
39083859Sml29623 	case NXGE_GET64:
39093859Sml29623 	case NXGE_PUT64:
39103859Sml29623 	case NXGE_GET_TX_RING_SZ:
39113859Sml29623 	case NXGE_GET_TX_DESC:
39123859Sml29623 	case NXGE_TX_SIDE_RESET:
39133859Sml29623 	case NXGE_RX_SIDE_RESET:
39143859Sml29623 	case NXGE_GLOBAL_RESET:
39153859Sml29623 	case NXGE_RESET_MAC:
39163859Sml29623 	case NXGE_TX_REGS_DUMP:
39173859Sml29623 	case NXGE_RX_REGS_DUMP:
39183859Sml29623 	case NXGE_INT_REGS_DUMP:
39193859Sml29623 	case NXGE_VIR_INT_REGS_DUMP:
39203859Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
39216512Ssowmini 		    "==> nxge_m_ioctl: cmd 0x%x", cmd));
39223859Sml29623 		nxge_hw_ioctl(nxgep, wq, mp, iocp);
39233859Sml29623 		break;
39243859Sml29623 	}
39253859Sml29623 
39263859Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_ioctl"));
39273859Sml29623 }
39283859Sml29623 
39293859Sml29623 extern void nxge_rx_hw_blank(void *arg, time_t ticks, uint_t count);
39303859Sml29623 
39316495Sspeer void
39328275SEric Cheng nxge_mmac_kstat_update(p_nxge_t nxgep, int slot, boolean_t factory)
39333859Sml29623 {
39343859Sml29623 	p_nxge_mmac_stats_t mmac_stats;
39353859Sml29623 	int i;
39363859Sml29623 	nxge_mmac_t *mmac_info;
39373859Sml29623 
39383859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
39393859Sml29623 
39403859Sml29623 	mmac_stats = &nxgep->statsp->mmac_stats;
39413859Sml29623 	mmac_stats->mmac_max_cnt = mmac_info->num_mmac;
39423859Sml29623 	mmac_stats->mmac_avail_cnt = mmac_info->naddrfree;
39433859Sml29623 
39443859Sml29623 	for (i = 0; i < ETHERADDRL; i++) {
39453859Sml29623 		if (factory) {
39463859Sml29623 			mmac_stats->mmac_avail_pool[slot-1].ether_addr_octet[i]
39476512Ssowmini 			    = mmac_info->factory_mac_pool[slot][
39486512Ssowmini 			    (ETHERADDRL-1) - i];
39493859Sml29623 		} else {
39503859Sml29623 			mmac_stats->mmac_avail_pool[slot-1].ether_addr_octet[i]
39516512Ssowmini 			    = mmac_info->mac_pool[slot].addr[
39526512Ssowmini 			    (ETHERADDRL - 1) - i];
39533859Sml29623 		}
39543859Sml29623 	}
39553859Sml29623 }
39563859Sml29623 
39573859Sml29623 /*
39583859Sml29623  * nxge_altmac_set() -- Set an alternate MAC address
39593859Sml29623  */
39608275SEric Cheng static int
39618275SEric Cheng nxge_altmac_set(p_nxge_t nxgep, uint8_t *maddr, int slot,
39628275SEric Cheng 	int rdctbl, boolean_t usetbl)
39633859Sml29623 {
39643859Sml29623 	uint8_t addrn;
39653859Sml29623 	uint8_t portn;
39663859Sml29623 	npi_mac_addr_t altmac;
39674484Sspeer 	hostinfo_t mac_rdc;
39684484Sspeer 	p_nxge_class_pt_cfg_t clscfgp;
39693859Sml29623 
39708275SEric Cheng 
39713859Sml29623 	altmac.w2 = ((uint16_t)maddr[0] << 8) | ((uint16_t)maddr[1] & 0x0ff);
39723859Sml29623 	altmac.w1 = ((uint16_t)maddr[2] << 8) | ((uint16_t)maddr[3] & 0x0ff);
39733859Sml29623 	altmac.w0 = ((uint16_t)maddr[4] << 8) | ((uint16_t)maddr[5] & 0x0ff);
39743859Sml29623 
39753859Sml29623 	portn = nxgep->mac.portnum;
39763859Sml29623 	addrn = (uint8_t)slot - 1;
39773859Sml29623 
39788275SEric Cheng 	if (npi_mac_altaddr_entry(nxgep->npi_handle, OP_SET,
39798275SEric Cheng 	    nxgep->function_num, addrn, &altmac) != NPI_SUCCESS)
39803859Sml29623 		return (EIO);
39814484Sspeer 
39824484Sspeer 	/*
39834484Sspeer 	 * Set the rdc table number for the host info entry
39844484Sspeer 	 * for this mac address slot.
39854484Sspeer 	 */
39864484Sspeer 	clscfgp = (p_nxge_class_pt_cfg_t)&nxgep->class_config;
39874484Sspeer 	mac_rdc.value = 0;
39888275SEric Cheng 	if (usetbl)
39898275SEric Cheng 		mac_rdc.bits.w0.rdc_tbl_num = rdctbl;
39908275SEric Cheng 	else
39918275SEric Cheng 		mac_rdc.bits.w0.rdc_tbl_num =
39928275SEric Cheng 		    clscfgp->mac_host_info[addrn].rdctbl;
39934484Sspeer 	mac_rdc.bits.w0.mac_pref = clscfgp->mac_host_info[addrn].mpr_npr;
39944484Sspeer 
39954484Sspeer 	if (npi_mac_hostinfo_entry(nxgep->npi_handle, OP_SET,
39964484Sspeer 	    nxgep->function_num, addrn, &mac_rdc) != NPI_SUCCESS) {
39974484Sspeer 		return (EIO);
39984484Sspeer 	}
39994484Sspeer 
40003859Sml29623 	/*
40013859Sml29623 	 * Enable comparison with the alternate MAC address.
40023859Sml29623 	 * While the first alternate addr is enabled by bit 1 of register
40033859Sml29623 	 * BMAC_ALTAD_CMPEN, it is enabled by bit 0 of register
40043859Sml29623 	 * XMAC_ADDR_CMPEN, so slot needs to be converted to addrn
40053859Sml29623 	 * accordingly before calling npi_mac_altaddr_entry.
40063859Sml29623 	 */
40073859Sml29623 	if (portn == XMAC_PORT_0 || portn == XMAC_PORT_1)
40083859Sml29623 		addrn = (uint8_t)slot - 1;
40093859Sml29623 	else
40103859Sml29623 		addrn = (uint8_t)slot;
40113859Sml29623 
40128275SEric Cheng 	if (npi_mac_altaddr_enable(nxgep->npi_handle,
40138275SEric Cheng 	    nxgep->function_num, addrn) != NPI_SUCCESS) {
40143859Sml29623 		return (EIO);
40158275SEric Cheng 	}
40168275SEric Cheng 
40173859Sml29623 	return (0);
40183859Sml29623 }
40193859Sml29623 
40203859Sml29623 /*
40218275SEric Cheng  * nxeg_m_mmac_add_g() - find an unused address slot, set the address
40223859Sml29623  * value to the one specified, enable the port to start filtering on
40233859Sml29623  * the new MAC address.  Returns 0 on success.
40243859Sml29623  */
40256495Sspeer int
40268275SEric Cheng nxge_m_mmac_add_g(void *arg, const uint8_t *maddr, int rdctbl,
40278275SEric Cheng 	boolean_t usetbl)
40283859Sml29623 {
40293859Sml29623 	p_nxge_t nxgep = arg;
40308275SEric Cheng 	int slot;
40313859Sml29623 	nxge_mmac_t *mmac_info;
40323859Sml29623 	int err;
40333859Sml29623 	nxge_status_t status;
40343859Sml29623 
40353859Sml29623 	mutex_enter(nxgep->genlock);
40363859Sml29623 
40373859Sml29623 	/*
40383859Sml29623 	 * Make sure that nxge is initialized, if _start() has
40393859Sml29623 	 * not been called.
40403859Sml29623 	 */
40413859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
40423859Sml29623 		status = nxge_init(nxgep);
40433859Sml29623 		if (status != NXGE_OK) {
40443859Sml29623 			mutex_exit(nxgep->genlock);
40453859Sml29623 			return (ENXIO);
40463859Sml29623 		}
40473859Sml29623 	}
40483859Sml29623 
40493859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
40503859Sml29623 	if (mmac_info->naddrfree == 0) {
40513859Sml29623 		mutex_exit(nxgep->genlock);
40523859Sml29623 		return (ENOSPC);
40533859Sml29623 	}
40548275SEric Cheng 
40553859Sml29623 	/*
40563859Sml29623 	 * 	Search for the first available slot. Because naddrfree
40573859Sml29623 	 * is not zero, we are guaranteed to find one.
40583859Sml29623 	 *	Each of the first two ports of Neptune has 16 alternate
40596495Sspeer 	 * MAC slots but only the first 7 (of 15) slots have assigned factory
40603859Sml29623 	 * MAC addresses. We first search among the slots without bundled
40613859Sml29623 	 * factory MACs. If we fail to find one in that range, then we
40623859Sml29623 	 * search the slots with bundled factory MACs.  A factory MAC
40633859Sml29623 	 * will be wasted while the slot is used with a user MAC address.
40643859Sml29623 	 * But the slot could be used by factory MAC again after calling
40653859Sml29623 	 * nxge_m_mmac_remove and nxge_m_mmac_reserve.
40663859Sml29623 	 */
40678275SEric Cheng 	for (slot = 0; slot <= mmac_info->num_mmac; slot++) {
40688275SEric Cheng 		if (!(mmac_info->mac_pool[slot].flags & MMAC_SLOT_USED))
40698275SEric Cheng 			break;
40708275SEric Cheng 	}
40718275SEric Cheng 
40723859Sml29623 	ASSERT(slot <= mmac_info->num_mmac);
40738047SMichael.Speer@Sun.COM 
40748275SEric Cheng 	if ((err = nxge_altmac_set(nxgep, (uint8_t *)maddr, slot, rdctbl,
40758275SEric Cheng 	    usetbl)) != 0) {
40763859Sml29623 		mutex_exit(nxgep->genlock);
40773859Sml29623 		return (err);
40783859Sml29623 	}
40798047SMichael.Speer@Sun.COM 
40808275SEric Cheng 	bcopy(maddr, mmac_info->mac_pool[slot].addr, ETHERADDRL);
40813859Sml29623 	mmac_info->mac_pool[slot].flags |= MMAC_SLOT_USED;
40823859Sml29623 	mmac_info->mac_pool[slot].flags &= ~MMAC_VENDOR_ADDR;
40833859Sml29623 	mmac_info->naddrfree--;
40843859Sml29623 	nxge_mmac_kstat_update(nxgep, slot, B_FALSE);
40853859Sml29623 
40863859Sml29623 	mutex_exit(nxgep->genlock);
40873859Sml29623 	return (0);
40883859Sml29623 }
40893859Sml29623 
40903859Sml29623 /*
40913859Sml29623  * Remove the specified mac address and update the HW not to filter
40923859Sml29623  * the mac address anymore.
40933859Sml29623  */
40946495Sspeer int
40958275SEric Cheng nxge_m_mmac_remove(void *arg, int slot)
40963859Sml29623 {
40973859Sml29623 	p_nxge_t nxgep = arg;
40983859Sml29623 	nxge_mmac_t *mmac_info;
40993859Sml29623 	uint8_t addrn;
41003859Sml29623 	uint8_t portn;
41013859Sml29623 	int err = 0;
41023859Sml29623 	nxge_status_t status;
41033859Sml29623 
41043859Sml29623 	mutex_enter(nxgep->genlock);
41053859Sml29623 
41063859Sml29623 	/*
41073859Sml29623 	 * Make sure that nxge is initialized, if _start() has
41083859Sml29623 	 * not been called.
41093859Sml29623 	 */
41103859Sml29623 	if (!(nxgep->drv_state & STATE_HW_INITIALIZED)) {
41113859Sml29623 		status = nxge_init(nxgep);
41123859Sml29623 		if (status != NXGE_OK) {
41133859Sml29623 			mutex_exit(nxgep->genlock);
41143859Sml29623 			return (ENXIO);
41153859Sml29623 		}
41163859Sml29623 	}
41173859Sml29623 
41183859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
41193859Sml29623 	if (slot < 1 || slot > mmac_info->num_mmac) {
41203859Sml29623 		mutex_exit(nxgep->genlock);
41213859Sml29623 		return (EINVAL);
41223859Sml29623 	}
41233859Sml29623 
41243859Sml29623 	portn = nxgep->mac.portnum;
41253859Sml29623 	if (portn == XMAC_PORT_0 || portn == XMAC_PORT_1)
41263859Sml29623 		addrn = (uint8_t)slot - 1;
41273859Sml29623 	else
41283859Sml29623 		addrn = (uint8_t)slot;
41293859Sml29623 
41303859Sml29623 	if (mmac_info->mac_pool[slot].flags & MMAC_SLOT_USED) {
41313859Sml29623 		if (npi_mac_altaddr_disable(nxgep->npi_handle, portn, addrn)
41326512Ssowmini 		    == NPI_SUCCESS) {
41333859Sml29623 			mmac_info->naddrfree++;
41343859Sml29623 			mmac_info->mac_pool[slot].flags &= ~MMAC_SLOT_USED;
41353859Sml29623 			/*
41363859Sml29623 			 * Regardless if the MAC we just stopped filtering
41373859Sml29623 			 * is a user addr or a facory addr, we must set
41383859Sml29623 			 * the MMAC_VENDOR_ADDR flag if this slot has an
41393859Sml29623 			 * associated factory MAC to indicate that a factory
41403859Sml29623 			 * MAC is available.
41413859Sml29623 			 */
41423859Sml29623 			if (slot <= mmac_info->num_factory_mmac) {
41433859Sml29623 				mmac_info->mac_pool[slot].flags
41446512Ssowmini 				    |= MMAC_VENDOR_ADDR;
41453859Sml29623 			}
41463859Sml29623 			/*
41473859Sml29623 			 * Clear mac_pool[slot].addr so that kstat shows 0
41483859Sml29623 			 * alternate MAC address if the slot is not used.
41493859Sml29623 			 * (But nxge_m_mmac_get returns the factory MAC even
41503859Sml29623 			 * when the slot is not used!)
41513859Sml29623 			 */
41523859Sml29623 			bzero(mmac_info->mac_pool[slot].addr, ETHERADDRL);
41533859Sml29623 			nxge_mmac_kstat_update(nxgep, slot, B_FALSE);
41543859Sml29623 		} else {
41553859Sml29623 			err = EIO;
41563859Sml29623 		}
41573859Sml29623 	} else {
41583859Sml29623 		err = EINVAL;
41593859Sml29623 	}
41603859Sml29623 
41613859Sml29623 	mutex_exit(nxgep->genlock);
41623859Sml29623 	return (err);
41633859Sml29623 }
41643859Sml29623 
41653859Sml29623 /*
41668275SEric Cheng  * The callback to query all the factory addresses. naddr must be the same as
41678275SEric Cheng  * the number of factory addresses (returned by MAC_CAPAB_MULTIFACTADDR), and
41688275SEric Cheng  * mcm_addr is the space allocated for keep all the addresses, whose size is
41698275SEric Cheng  * naddr * MAXMACADDRLEN.
41703859Sml29623  */
41718275SEric Cheng static void
41728275SEric Cheng nxge_m_getfactaddr(void *arg, uint_t naddr, uint8_t *addr)
41733859Sml29623 {
41748275SEric Cheng 	nxge_t		*nxgep = arg;
41758275SEric Cheng 	nxge_mmac_t	*mmac_info;
41768275SEric Cheng 	int		i;
41773859Sml29623 
41783859Sml29623 	mutex_enter(nxgep->genlock);
41793859Sml29623 
41803859Sml29623 	mmac_info = &nxgep->nxge_mmac_info;
41818275SEric Cheng 	ASSERT(naddr == mmac_info->num_factory_mmac);
41828275SEric Cheng 
41838275SEric Cheng 	for (i = 0; i < naddr; i++) {
41848275SEric Cheng 		bcopy(mmac_info->factory_mac_pool[i + 1],
41858275SEric Cheng 		    addr + i * MAXMACADDRLEN, ETHERADDRL);
41868275SEric Cheng 	}
41878275SEric Cheng 
41883859Sml29623 	mutex_exit(nxgep->genlock);
41893859Sml29623 }
41903859Sml29623 
41913859Sml29623 
41923859Sml29623 static boolean_t
41933859Sml29623 nxge_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
41943859Sml29623 {
41953859Sml29623 	nxge_t *nxgep = arg;
41963859Sml29623 	uint32_t *txflags = cap_data;
41973859Sml29623 
41983859Sml29623 	switch (cap) {
41993859Sml29623 	case MAC_CAPAB_HCKSUM:
42006495Sspeer 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
42016611Sml29623 		    "==> nxge_m_getcapab: checksum %d", nxge_cksum_offload));
42026611Sml29623 		if (nxge_cksum_offload <= 1) {
42036495Sspeer 			*txflags = HCKSUM_INET_PARTIAL;
42046495Sspeer 		}
42053859Sml29623 		break;
42066495Sspeer 
42078275SEric Cheng 	case MAC_CAPAB_MULTIFACTADDR: {
42088275SEric Cheng 		mac_capab_multifactaddr_t	*mfacp = cap_data;
42098275SEric Cheng 
42103859Sml29623 		mutex_enter(nxgep->genlock);
42118275SEric Cheng 		mfacp->mcm_naddr = nxgep->nxge_mmac_info.num_factory_mmac;
42128275SEric Cheng 		mfacp->mcm_getaddr = nxge_m_getfactaddr;
42133859Sml29623 		mutex_exit(nxgep->genlock);
42143859Sml29623 		break;
42158275SEric Cheng 	}
42166495Sspeer 
42175770Sml29623 	case MAC_CAPAB_LSO: {
42185770Sml29623 		mac_capab_lso_t *cap_lso = cap_data;
42195770Sml29623 
42206003Sml29623 		if (nxgep->soft_lso_enable) {
42216611Sml29623 			if (nxge_cksum_offload <= 1) {
42226611Sml29623 				cap_lso->lso_flags = LSO_TX_BASIC_TCP_IPV4;
42236611Sml29623 				if (nxge_lso_max > NXGE_LSO_MAXLEN) {
42246611Sml29623 					nxge_lso_max = NXGE_LSO_MAXLEN;
42256611Sml29623 				}
42266611Sml29623 				cap_lso->lso_basic_tcp_ipv4.lso_max =
42276611Sml29623 				    nxge_lso_max;
42285770Sml29623 			}
42295770Sml29623 			break;
42305770Sml29623 		} else {
42315770Sml29623 			return (B_FALSE);
42325770Sml29623 		}
42335770Sml29623 	}
42345770Sml29623 
42358275SEric Cheng 	case MAC_CAPAB_RINGS: {
42368275SEric Cheng 		mac_capab_rings_t	*cap_rings = cap_data;
42378275SEric Cheng 		p_nxge_hw_pt_cfg_t	p_cfgp = &nxgep->pt_config.hw_config;
42388275SEric Cheng 
42398275SEric Cheng 		mutex_enter(nxgep->genlock);
42408275SEric Cheng 		if (cap_rings->mr_type == MAC_RING_TYPE_RX) {
42418275SEric Cheng 			cap_rings->mr_group_type = MAC_GROUP_TYPE_DYNAMIC;
42428275SEric Cheng 			cap_rings->mr_rnum = p_cfgp->max_rdcs;
42438275SEric Cheng 			cap_rings->mr_rget = nxge_fill_ring;
42448275SEric Cheng 			cap_rings->mr_gnum = p_cfgp->max_rdc_grpids;
42458275SEric Cheng 			cap_rings->mr_gget = nxge_hio_group_get;
42468275SEric Cheng 			cap_rings->mr_gaddring = nxge_group_add_ring;
42478275SEric Cheng 			cap_rings->mr_gremring = nxge_group_rem_ring;
42488275SEric Cheng 
42498275SEric Cheng 			NXGE_DEBUG_MSG((nxgep, RX_CTL,
42508275SEric Cheng 			    "==> nxge_m_getcapab: rx nrings[%d] ngroups[%d]",
42518275SEric Cheng 			    p_cfgp->max_rdcs, p_cfgp->max_rdc_grpids));
42528275SEric Cheng 		} else {
42538275SEric Cheng 			cap_rings->mr_group_type = MAC_GROUP_TYPE_DYNAMIC;
42548275SEric Cheng 			cap_rings->mr_rnum = p_cfgp->tdc.count;
42558275SEric Cheng 			cap_rings->mr_rget = nxge_fill_ring;
42568275SEric Cheng 			if (isLDOMservice(nxgep)) {
42578275SEric Cheng 				/* share capable */
42588275SEric Cheng 				/* Do not report the default ring: hence -1 */
42598275SEric Cheng 				cap_rings->mr_gnum =
42608275SEric Cheng 				    NXGE_MAX_TDC_GROUPS / nxgep->nports - 1;
42618275SEric Cheng 			} else {
42628275SEric Cheng 				cap_rings->mr_gnum = 0;
42638275SEric Cheng 			}
42648275SEric Cheng 
42658275SEric Cheng 			cap_rings->mr_gget = nxge_hio_group_get;
42668275SEric Cheng 			cap_rings->mr_gaddring = nxge_group_add_ring;
42678275SEric Cheng 			cap_rings->mr_gremring = nxge_group_rem_ring;
42688275SEric Cheng 
42698275SEric Cheng 			NXGE_DEBUG_MSG((nxgep, TX_CTL,
42708275SEric Cheng 			    "==> nxge_m_getcapab: tx rings # of rings %d",
42718275SEric Cheng 			    p_cfgp->tdc.count));
42728275SEric Cheng 		}
42738275SEric Cheng 		mutex_exit(nxgep->genlock);
42748275SEric Cheng 		break;
42758275SEric Cheng 	}
42768275SEric Cheng 
42776495Sspeer #if defined(sun4v)
42786495Sspeer 	case MAC_CAPAB_SHARES: {
42796495Sspeer 		mac_capab_share_t *mshares = (mac_capab_share_t *)cap_data;
42806495Sspeer 
42816495Sspeer 		/*
42826495Sspeer 		 * Only the service domain driver responds to
42836495Sspeer 		 * this capability request.
42846495Sspeer 		 */
42858275SEric Cheng 		mutex_enter(nxgep->genlock);
42866495Sspeer 		if (isLDOMservice(nxgep)) {
42876495Sspeer 			mshares->ms_snum = 3;
42886495Sspeer 			mshares->ms_handle = (void *)nxgep;
42896495Sspeer 			mshares->ms_salloc = nxge_hio_share_alloc;
42906495Sspeer 			mshares->ms_sfree = nxge_hio_share_free;
42918275SEric Cheng 			mshares->ms_sadd = nxge_hio_share_add_group;
42928275SEric Cheng 			mshares->ms_sremove = nxge_hio_share_rem_group;
42936495Sspeer 			mshares->ms_squery = nxge_hio_share_query;
42948275SEric Cheng 			mshares->ms_sbind = nxge_hio_share_bind;
42958275SEric Cheng 			mshares->ms_sunbind = nxge_hio_share_unbind;
42968275SEric Cheng 			mutex_exit(nxgep->genlock);
42978275SEric Cheng 		} else {
42988275SEric Cheng 			mutex_exit(nxgep->genlock);
42996495Sspeer 			return (B_FALSE);
43008275SEric Cheng 		}
43016495Sspeer 		break;
43026495Sspeer 	}
43036495Sspeer #endif
43043859Sml29623 	default:
43053859Sml29623 		return (B_FALSE);
43063859Sml29623 	}
43073859Sml29623 	return (B_TRUE);
43083859Sml29623 }
43093859Sml29623 
43106439Sml29623 static boolean_t
43116439Sml29623 nxge_param_locked(mac_prop_id_t pr_num)
43126439Sml29623 {
43136439Sml29623 	/*
43146439Sml29623 	 * All adv_* parameters are locked (read-only) while
43156439Sml29623 	 * the device is in any sort of loopback mode ...
43166439Sml29623 	 */
43176439Sml29623 	switch (pr_num) {
43186789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
43196789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
43206789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
43216789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
43226789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
43236789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
43246789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
43256789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
43266789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
43276789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
43286789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
43296789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
43306789Sam223141 		case MAC_PROP_AUTONEG:
43316789Sam223141 		case MAC_PROP_FLOWCTRL:
43326439Sml29623 			return (B_TRUE);
43336439Sml29623 	}
43346439Sml29623 	return (B_FALSE);
43356439Sml29623 }
43366439Sml29623 
43376439Sml29623 /*
43386439Sml29623  * callback functions for set/get of properties
43396439Sml29623  */
43406439Sml29623 static int
43416439Sml29623 nxge_m_setprop(void *barg, const char *pr_name, mac_prop_id_t pr_num,
43426439Sml29623     uint_t pr_valsize, const void *pr_val)
43436439Sml29623 {
43446439Sml29623 	nxge_t		*nxgep = barg;
43456439Sml29623 	p_nxge_param_t	param_arr;
43466439Sml29623 	p_nxge_stats_t	statsp;
43476439Sml29623 	int		err = 0;
43486439Sml29623 	uint8_t		val;
43496439Sml29623 	uint32_t	cur_mtu, new_mtu, old_framesize;
43506439Sml29623 	link_flowctrl_t	fl;
43516439Sml29623 
43526439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "==> nxge_m_setprop"));
43536439Sml29623 	param_arr = nxgep->param_arr;
43546439Sml29623 	statsp = nxgep->statsp;
43556439Sml29623 	mutex_enter(nxgep->genlock);
43566439Sml29623 	if (statsp->port_stats.lb_mode != nxge_lb_normal &&
43576439Sml29623 	    nxge_param_locked(pr_num)) {
43586439Sml29623 		/*
43596439Sml29623 		 * All adv_* parameters are locked (read-only)
43606439Sml29623 		 * while the device is in any sort of loopback mode.
43616439Sml29623 		 */
43626439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
43636439Sml29623 		    "==> nxge_m_setprop: loopback mode: read only"));
43646439Sml29623 		mutex_exit(nxgep->genlock);
43656439Sml29623 		return (EBUSY);
43666439Sml29623 	}
43676439Sml29623 
43686439Sml29623 	val = *(uint8_t *)pr_val;
43696439Sml29623 	switch (pr_num) {
43706789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
43716439Sml29623 			nxgep->param_en_1000fdx = val;
43726439Sml29623 			param_arr[param_anar_1000fdx].value = val;
43736439Sml29623 
43746439Sml29623 			goto reprogram;
43756439Sml29623 
43766789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
43776439Sml29623 			nxgep->param_en_100fdx = val;
43786439Sml29623 			param_arr[param_anar_100fdx].value = val;
43796439Sml29623 
43806439Sml29623 			goto reprogram;
43816439Sml29623 
43826789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
43836439Sml29623 			nxgep->param_en_10fdx = val;
43846439Sml29623 			param_arr[param_anar_10fdx].value = val;
43856439Sml29623 
43866439Sml29623 			goto reprogram;
43876439Sml29623 
43886789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
43896789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
43906789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
43916789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
43926789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
43936789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
43946789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
43956789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
43966789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
43976789Sam223141 		case MAC_PROP_STATUS:
43986789Sam223141 		case MAC_PROP_SPEED:
43996789Sam223141 		case MAC_PROP_DUPLEX:
44006439Sml29623 			err = EINVAL; /* cannot set read-only properties */
44016439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
44026439Sml29623 			    "==> nxge_m_setprop:  read only property %d",
44036439Sml29623 			    pr_num));
44046439Sml29623 			break;
44056439Sml29623 
44066789Sam223141 		case MAC_PROP_AUTONEG:
44076439Sml29623 			param_arr[param_autoneg].value = val;
44086439Sml29623 
44096439Sml29623 			goto reprogram;
44106439Sml29623 
44116789Sam223141 		case MAC_PROP_MTU:
44126439Sml29623 			cur_mtu = nxgep->mac.default_mtu;
44136439Sml29623 			bcopy(pr_val, &new_mtu, sizeof (new_mtu));
44146439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
44156439Sml29623 			    "==> nxge_m_setprop: set MTU: %d is_jumbo %d",
44166439Sml29623 			    new_mtu, nxgep->mac.is_jumbo));
44176439Sml29623 
44186439Sml29623 			if (new_mtu == cur_mtu) {
44196439Sml29623 				err = 0;
44206439Sml29623 				break;
44216439Sml29623 			}
44228118SVasumathi.Sundaram@Sun.COM 			if (nxgep->nxge_mac_state == NXGE_MAC_STARTED) {
44238118SVasumathi.Sundaram@Sun.COM 				err = EBUSY;
44248118SVasumathi.Sundaram@Sun.COM 				break;
44258118SVasumathi.Sundaram@Sun.COM 			}
44266439Sml29623 			if (new_mtu < NXGE_DEFAULT_MTU ||
44276439Sml29623 			    new_mtu > NXGE_MAXIMUM_MTU) {
44286439Sml29623 				err = EINVAL;
44296439Sml29623 				break;
44306439Sml29623 			}
44316439Sml29623 
44326439Sml29623 			if ((new_mtu > NXGE_DEFAULT_MTU) &&
44336439Sml29623 			    !nxgep->mac.is_jumbo) {
44346439Sml29623 				err = EINVAL;
44356439Sml29623 				break;
44366439Sml29623 			}
44376439Sml29623 
44386439Sml29623 			old_framesize = (uint32_t)nxgep->mac.maxframesize;
44396439Sml29623 			nxgep->mac.maxframesize = (uint16_t)
44406439Sml29623 			    (new_mtu + NXGE_EHEADER_VLAN_CRC);
44416439Sml29623 			if (nxge_mac_set_framesize(nxgep)) {
44426444Sml29623 				nxgep->mac.maxframesize =
44436444Sml29623 				    (uint16_t)old_framesize;
44446439Sml29623 				err = EINVAL;
44456439Sml29623 				break;
44466439Sml29623 			}
44476439Sml29623 
44486439Sml29623 			err = mac_maxsdu_update(nxgep->mach, new_mtu);
44496439Sml29623 			if (err) {
44506444Sml29623 				nxgep->mac.maxframesize =
44516444Sml29623 				    (uint16_t)old_framesize;
44526439Sml29623 				err = EINVAL;
44536439Sml29623 				break;
44546439Sml29623 			}
44556439Sml29623 
44566439Sml29623 			nxgep->mac.default_mtu = new_mtu;
44576439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
44586439Sml29623 			    "==> nxge_m_setprop: set MTU: %d maxframe %d",
44596439Sml29623 			    new_mtu, nxgep->mac.maxframesize));
44606439Sml29623 			break;
44616439Sml29623 
44626789Sam223141 		case MAC_PROP_FLOWCTRL:
44636439Sml29623 			bcopy(pr_val, &fl, sizeof (fl));
44646439Sml29623 			switch (fl) {
44656439Sml29623 			default:
44666439Sml29623 				err = EINVAL;
44676439Sml29623 				break;
44686439Sml29623 
44696439Sml29623 			case LINK_FLOWCTRL_NONE:
44706439Sml29623 				param_arr[param_anar_pause].value = 0;
44716439Sml29623 				break;
44726439Sml29623 
44736439Sml29623 			case LINK_FLOWCTRL_RX:
44746439Sml29623 				param_arr[param_anar_pause].value = 1;
44756439Sml29623 				break;
44766439Sml29623 
44776439Sml29623 			case LINK_FLOWCTRL_TX:
44786439Sml29623 			case LINK_FLOWCTRL_BI:
44796439Sml29623 				err = EINVAL;
44806439Sml29623 				break;
44816439Sml29623 			}
44826439Sml29623 
44836439Sml29623 reprogram:
44846439Sml29623 			if (err == 0) {
44856439Sml29623 				if (!nxge_param_link_update(nxgep)) {
44866439Sml29623 					err = EINVAL;
44876439Sml29623 				}
44886439Sml29623 			}
44896439Sml29623 			break;
44906789Sam223141 		case MAC_PROP_PRIVATE:
44916439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
44926439Sml29623 			    "==> nxge_m_setprop: private property"));
44936439Sml29623 			err = nxge_set_priv_prop(nxgep, pr_name, pr_valsize,
44946439Sml29623 			    pr_val);
44956439Sml29623 			break;
44966512Ssowmini 
44976512Ssowmini 		default:
44986512Ssowmini 			err = ENOTSUP;
44996512Ssowmini 			break;
45006439Sml29623 	}
45016439Sml29623 
45026439Sml29623 	mutex_exit(nxgep->genlock);
45036439Sml29623 
45046439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
45056439Sml29623 	    "<== nxge_m_setprop (return %d)", err));
45066439Sml29623 	return (err);
45076439Sml29623 }
45086439Sml29623 
45096439Sml29623 static int
45106439Sml29623 nxge_m_getprop(void *barg, const char *pr_name, mac_prop_id_t pr_num,
45118118SVasumathi.Sundaram@Sun.COM     uint_t pr_flags, uint_t pr_valsize, void *pr_val, uint_t *perm)
45126439Sml29623 {
45136439Sml29623 	nxge_t 		*nxgep = barg;
45146439Sml29623 	p_nxge_param_t	param_arr = nxgep->param_arr;
45156439Sml29623 	p_nxge_stats_t	statsp = nxgep->statsp;
45166439Sml29623 	int		err = 0;
45176439Sml29623 	link_flowctrl_t	fl;
45186439Sml29623 	uint64_t	tmp = 0;
45196512Ssowmini 	link_state_t	ls;
45206789Sam223141 	boolean_t	is_default = (pr_flags & MAC_PROP_DEFAULT);
45216439Sml29623 
45226439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
45236439Sml29623 	    "==> nxge_m_getprop: pr_num %d", pr_num));
45246512Ssowmini 
45256512Ssowmini 	if (pr_valsize == 0)
45266512Ssowmini 		return (EINVAL);
45276512Ssowmini 
45288118SVasumathi.Sundaram@Sun.COM 	*perm = MAC_PROP_PERM_RW;
45298118SVasumathi.Sundaram@Sun.COM 
45306789Sam223141 	if ((is_default) && (pr_num != MAC_PROP_PRIVATE)) {
45316512Ssowmini 		err = nxge_get_def_val(nxgep, pr_num, pr_valsize, pr_val);
45326512Ssowmini 		return (err);
45336512Ssowmini 	}
45346512Ssowmini 
45356439Sml29623 	bzero(pr_val, pr_valsize);
45366439Sml29623 	switch (pr_num) {
45376789Sam223141 		case MAC_PROP_DUPLEX:
45388118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
45396439Sml29623 			*(uint8_t *)pr_val = statsp->mac_stats.link_duplex;
45406439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
45416439Sml29623 			    "==> nxge_m_getprop: duplex mode %d",
45426439Sml29623 			    *(uint8_t *)pr_val));
45436439Sml29623 			break;
45446439Sml29623 
45456789Sam223141 		case MAC_PROP_SPEED:
45466439Sml29623 			if (pr_valsize < sizeof (uint64_t))
45476439Sml29623 				return (EINVAL);
45488118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
45496439Sml29623 			tmp = statsp->mac_stats.link_speed * 1000000ull;
45506439Sml29623 			bcopy(&tmp, pr_val, sizeof (tmp));
45516439Sml29623 			break;
45526439Sml29623 
45536789Sam223141 		case MAC_PROP_STATUS:
45546512Ssowmini 			if (pr_valsize < sizeof (link_state_t))
45556439Sml29623 				return (EINVAL);
45568118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
45576512Ssowmini 			if (!statsp->mac_stats.link_up)
45586512Ssowmini 				ls = LINK_STATE_DOWN;
45596512Ssowmini 			else
45606512Ssowmini 				ls = LINK_STATE_UP;
45616512Ssowmini 			bcopy(&ls, pr_val, sizeof (ls));
45626439Sml29623 			break;
45636439Sml29623 
45646789Sam223141 		case MAC_PROP_AUTONEG:
45656439Sml29623 			*(uint8_t *)pr_val =
45666439Sml29623 			    param_arr[param_autoneg].value;
45676439Sml29623 			break;
45686439Sml29623 
45696789Sam223141 		case MAC_PROP_FLOWCTRL:
45706439Sml29623 			if (pr_valsize < sizeof (link_flowctrl_t))
45716439Sml29623 				return (EINVAL);
45726439Sml29623 
45736439Sml29623 			fl = LINK_FLOWCTRL_NONE;
45746439Sml29623 			if (param_arr[param_anar_pause].value) {
45756439Sml29623 				fl = LINK_FLOWCTRL_RX;
45766439Sml29623 			}
45776439Sml29623 			bcopy(&fl, pr_val, sizeof (fl));
45786439Sml29623 			break;
45796439Sml29623 
45806789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
45818118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
45826439Sml29623 			*(uint8_t *)pr_val =
45836439Sml29623 			    param_arr[param_anar_1000fdx].value;
45846439Sml29623 			break;
45856439Sml29623 
45866789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
45876439Sml29623 			*(uint8_t *)pr_val = nxgep->param_en_1000fdx;
45886439Sml29623 			break;
45896439Sml29623 
45906789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
45918118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
45926439Sml29623 			*(uint8_t *)pr_val =
45936439Sml29623 			    param_arr[param_anar_100fdx].value;
45946439Sml29623 			break;
45956439Sml29623 
45966789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
45976439Sml29623 			*(uint8_t *)pr_val = nxgep->param_en_100fdx;
45986439Sml29623 			break;
45996439Sml29623 
46006789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
46018118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
46026439Sml29623 			*(uint8_t *)pr_val =
46036439Sml29623 			    param_arr[param_anar_10fdx].value;
46046439Sml29623 			break;
46056439Sml29623 
46066789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
46076439Sml29623 			*(uint8_t *)pr_val = nxgep->param_en_10fdx;
46086439Sml29623 			break;
46096439Sml29623 
46106789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
46116789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
46126789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
46136789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
46146789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
46156789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
46166512Ssowmini 			err = ENOTSUP;
46176512Ssowmini 			break;
46186512Ssowmini 
46196789Sam223141 		case MAC_PROP_PRIVATE:
46206512Ssowmini 			err = nxge_get_priv_prop(nxgep, pr_name, pr_flags,
46218118SVasumathi.Sundaram@Sun.COM 			    pr_valsize, pr_val, perm);
46226512Ssowmini 			break;
46236512Ssowmini 		default:
46246439Sml29623 			err = EINVAL;
46256439Sml29623 			break;
46266439Sml29623 	}
46276439Sml29623 
46286439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL, "<== nxge_m_getprop"));
46296439Sml29623 
46306439Sml29623 	return (err);
46316439Sml29623 }
46326439Sml29623 
46336439Sml29623 /* ARGSUSED */
46346439Sml29623 static int
46356439Sml29623 nxge_set_priv_prop(p_nxge_t nxgep, const char *pr_name, uint_t pr_valsize,
46366439Sml29623     const void *pr_val)
46376439Sml29623 {
46386439Sml29623 	p_nxge_param_t	param_arr = nxgep->param_arr;
46396439Sml29623 	int		err = 0;
46406439Sml29623 	long		result;
46416439Sml29623 
46426439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
46436439Sml29623 	    "==> nxge_set_priv_prop: name %s", pr_name));
46446439Sml29623 
46456439Sml29623 	if (strcmp(pr_name, "_accept_jumbo") == 0) {
46466439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
46476439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
46486439Sml29623 		    "<== nxge_set_priv_prop: name %s "
46496439Sml29623 		    "pr_val %s result %d "
46506439Sml29623 		    "param %d is_jumbo %d",
46516439Sml29623 		    pr_name, pr_val, result,
46526439Sml29623 		    param_arr[param_accept_jumbo].value,
46536439Sml29623 		    nxgep->mac.is_jumbo));
46546439Sml29623 
46556439Sml29623 		if (result > 1 || result < 0) {
46566439Sml29623 			err = EINVAL;
46576439Sml29623 		} else {
46586439Sml29623 			if (nxgep->mac.is_jumbo ==
46596439Sml29623 			    (uint32_t)result) {
46606439Sml29623 				NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
46616439Sml29623 				    "no change (%d %d)",
46626439Sml29623 				    nxgep->mac.is_jumbo,
46636439Sml29623 				    result));
46646439Sml29623 				return (0);
46656439Sml29623 			}
46666439Sml29623 		}
46676439Sml29623 
46686439Sml29623 		param_arr[param_accept_jumbo].value = result;
46696439Sml29623 		nxgep->mac.is_jumbo = B_FALSE;
46706439Sml29623 		if (result) {
46716439Sml29623 			nxgep->mac.is_jumbo = B_TRUE;
46726439Sml29623 		}
46736439Sml29623 
46746439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
46756439Sml29623 		    "<== nxge_set_priv_prop: name %s (value %d) is_jumbo %d",
46766439Sml29623 		    pr_name, result, nxgep->mac.is_jumbo));
46776439Sml29623 
46786439Sml29623 		return (err);
46796439Sml29623 	}
46806439Sml29623 
46816439Sml29623 	/* Blanking */
46826439Sml29623 	if (strcmp(pr_name, "_rxdma_intr_time") == 0) {
46836439Sml29623 		err = nxge_param_rx_intr_time(nxgep, NULL, NULL,
46846439Sml29623 		    (char *)pr_val,
46856439Sml29623 		    (caddr_t)&param_arr[param_rxdma_intr_time]);
46866439Sml29623 		if (err) {
46876439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
46886439Sml29623 			    "<== nxge_set_priv_prop: "
46896439Sml29623 			    "unable to set (%s)", pr_name));
46906439Sml29623 			err = EINVAL;
46916439Sml29623 		} else {
46926439Sml29623 			err = 0;
46936439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
46946439Sml29623 			    "<== nxge_set_priv_prop: "
46956439Sml29623 			    "set (%s)", pr_name));
46966439Sml29623 		}
46976439Sml29623 
46986439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
46996439Sml29623 		    "<== nxge_set_priv_prop: name %s (value %d)",
47006439Sml29623 		    pr_name, result));
47016439Sml29623 
47026439Sml29623 		return (err);
47036439Sml29623 	}
47046439Sml29623 
47056439Sml29623 	if (strcmp(pr_name, "_rxdma_intr_pkts") == 0) {
47066439Sml29623 		err = nxge_param_rx_intr_pkts(nxgep, NULL, NULL,
47076439Sml29623 		    (char *)pr_val,
47086439Sml29623 		    (caddr_t)&param_arr[param_rxdma_intr_pkts]);
47096439Sml29623 		if (err) {
47106439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47116439Sml29623 			    "<== nxge_set_priv_prop: "
47126439Sml29623 			    "unable to set (%s)", pr_name));
47136439Sml29623 			err = EINVAL;
47146439Sml29623 		} else {
47156439Sml29623 			err = 0;
47166439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47176439Sml29623 			    "<== nxge_set_priv_prop: "
47186439Sml29623 			    "set (%s)", pr_name));
47196439Sml29623 		}
47206439Sml29623 
47216439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47226439Sml29623 		    "<== nxge_set_priv_prop: name %s (value %d)",
47236439Sml29623 		    pr_name, result));
47246439Sml29623 
47256439Sml29623 		return (err);
47266439Sml29623 	}
47276439Sml29623 
47286439Sml29623 	/* Classification */
47296439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_tcp") == 0) {
47306439Sml29623 		if (pr_val == NULL) {
47316439Sml29623 			err = EINVAL;
47326439Sml29623 			return (err);
47336439Sml29623 		}
47346439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
47356439Sml29623 
47366439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
47376439Sml29623 		    NULL, (char *)pr_val,
47386439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_tcp]);
47396439Sml29623 
47406439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47416439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
47426439Sml29623 		    pr_name, result));
47436439Sml29623 
47446439Sml29623 		return (err);
47456439Sml29623 	}
47466439Sml29623 
47476439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_udp") == 0) {
47486439Sml29623 		if (pr_val == NULL) {
47496439Sml29623 			err = EINVAL;
47506439Sml29623 			return (err);
47516439Sml29623 		}
47526439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
47536439Sml29623 
47546439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
47556439Sml29623 		    NULL, (char *)pr_val,
47566439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_udp]);
47576439Sml29623 
47586439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47596439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
47606439Sml29623 		    pr_name, result));
47616439Sml29623 
47626439Sml29623 		return (err);
47636439Sml29623 	}
47646439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_ah") == 0) {
47656439Sml29623 		if (pr_val == NULL) {
47666439Sml29623 			err = EINVAL;
47676439Sml29623 			return (err);
47686439Sml29623 		}
47696439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
47706439Sml29623 
47716439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
47726439Sml29623 		    NULL, (char *)pr_val,
47736439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_ah]);
47746439Sml29623 
47756439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47766439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
47776439Sml29623 		    pr_name, result));
47786439Sml29623 
47796439Sml29623 		return (err);
47806439Sml29623 	}
47816439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_sctp") == 0) {
47826439Sml29623 		if (pr_val == NULL) {
47836439Sml29623 			err = EINVAL;
47846439Sml29623 			return (err);
47856439Sml29623 		}
47866439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
47876439Sml29623 
47886439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
47896439Sml29623 		    NULL, (char *)pr_val,
47906439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_sctp]);
47916439Sml29623 
47926439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
47936439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
47946439Sml29623 		    pr_name, result));
47956439Sml29623 
47966439Sml29623 		return (err);
47976439Sml29623 	}
47986439Sml29623 
47996439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_tcp") == 0) {
48006439Sml29623 		if (pr_val == NULL) {
48016439Sml29623 			err = EINVAL;
48026439Sml29623 			return (err);
48036439Sml29623 		}
48046439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
48056439Sml29623 
48066439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
48076439Sml29623 		    NULL, (char *)pr_val,
48086439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_tcp]);
48096439Sml29623 
48106439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48116439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
48126439Sml29623 		    pr_name, result));
48136439Sml29623 
48146439Sml29623 		return (err);
48156439Sml29623 	}
48166439Sml29623 
48176439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_udp") == 0) {
48186439Sml29623 		if (pr_val == NULL) {
48196439Sml29623 			err = EINVAL;
48206439Sml29623 			return (err);
48216439Sml29623 		}
48226439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
48236439Sml29623 
48246439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
48256439Sml29623 		    NULL, (char *)pr_val,
48266439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_udp]);
48276439Sml29623 
48286439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48296439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
48306439Sml29623 		    pr_name, result));
48316439Sml29623 
48326439Sml29623 		return (err);
48336439Sml29623 	}
48346439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_ah") == 0) {
48356439Sml29623 		if (pr_val == NULL) {
48366439Sml29623 			err = EINVAL;
48376439Sml29623 			return (err);
48386439Sml29623 		}
48396439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
48406439Sml29623 
48416439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
48426439Sml29623 		    NULL, (char *)pr_val,
48436439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_ah]);
48446439Sml29623 
48456439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48466439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
48476439Sml29623 		    pr_name, result));
48486439Sml29623 
48496439Sml29623 		return (err);
48506439Sml29623 	}
48516439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_sctp") == 0) {
48526439Sml29623 		if (pr_val == NULL) {
48536439Sml29623 			err = EINVAL;
48546439Sml29623 			return (err);
48556439Sml29623 		}
48566439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
48576439Sml29623 
48586439Sml29623 		err = nxge_param_set_ip_opt(nxgep, NULL,
48596439Sml29623 		    NULL, (char *)pr_val,
48606439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_sctp]);
48616439Sml29623 
48626439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48636439Sml29623 		    "<== nxge_set_priv_prop: name %s (value 0x%x)",
48646439Sml29623 		    pr_name, result));
48656439Sml29623 
48666439Sml29623 		return (err);
48676439Sml29623 	}
48686439Sml29623 
48696439Sml29623 	if (strcmp(pr_name, "_soft_lso_enable") == 0) {
48706439Sml29623 		if (pr_val == NULL) {
48716439Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48726439Sml29623 			    "==> nxge_set_priv_prop: name %s (null)", pr_name));
48736439Sml29623 			err = EINVAL;
48746439Sml29623 			return (err);
48756439Sml29623 		}
48766439Sml29623 
48776439Sml29623 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
48786439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48796439Sml29623 		    "<== nxge_set_priv_prop: name %s "
48806439Sml29623 		    "(lso %d pr_val %s value %d)",
48816439Sml29623 		    pr_name, nxgep->soft_lso_enable, pr_val, result));
48826439Sml29623 
48836439Sml29623 		if (result > 1 || result < 0) {
48846439Sml29623 			err = EINVAL;
48856439Sml29623 		} else {
48866439Sml29623 			if (nxgep->soft_lso_enable == (uint32_t)result) {
48876439Sml29623 				NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48886439Sml29623 				    "no change (%d %d)",
48896439Sml29623 				    nxgep->soft_lso_enable, result));
48906439Sml29623 				return (0);
48916439Sml29623 			}
48926439Sml29623 		}
48936439Sml29623 
48946439Sml29623 		nxgep->soft_lso_enable = (int)result;
48956439Sml29623 
48966439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
48976439Sml29623 		    "<== nxge_set_priv_prop: name %s (value %d)",
48986439Sml29623 		    pr_name, result));
48996439Sml29623 
49006439Sml29623 		return (err);
49016439Sml29623 	}
49026835Syc148097 	/*
49036835Syc148097 	 * Commands like "ndd -set /dev/nxge0 adv_10gfdx_cap 1" cause the
49046835Syc148097 	 * following code to be executed.
49056835Syc148097 	 */
49066512Ssowmini 	if (strcmp(pr_name, "_adv_10gfdx_cap") == 0) {
49076512Ssowmini 		err = nxge_param_set_mac(nxgep, NULL, NULL, (char *)pr_val,
49086512Ssowmini 		    (caddr_t)&param_arr[param_anar_10gfdx]);
49096512Ssowmini 		return (err);
49106512Ssowmini 	}
49116512Ssowmini 	if (strcmp(pr_name, "_adv_pause_cap") == 0) {
49126512Ssowmini 		err = nxge_param_set_mac(nxgep, NULL, NULL, (char *)pr_val,
49136512Ssowmini 		    (caddr_t)&param_arr[param_anar_pause]);
49146512Ssowmini 		return (err);
49156512Ssowmini 	}
49166439Sml29623 
49176439Sml29623 	return (EINVAL);
49186439Sml29623 }
49196439Sml29623 
49206439Sml29623 static int
49216512Ssowmini nxge_get_priv_prop(p_nxge_t nxgep, const char *pr_name, uint_t pr_flags,
49228118SVasumathi.Sundaram@Sun.COM     uint_t pr_valsize, void *pr_val, uint_t *perm)
49236439Sml29623 {
49246439Sml29623 	p_nxge_param_t	param_arr = nxgep->param_arr;
49256439Sml29623 	char		valstr[MAXNAMELEN];
49266439Sml29623 	int		err = EINVAL;
49276439Sml29623 	uint_t		strsize;
49286789Sam223141 	boolean_t	is_default = (pr_flags & MAC_PROP_DEFAULT);
49296439Sml29623 
49306439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49316439Sml29623 	    "==> nxge_get_priv_prop: property %s", pr_name));
49326439Sml29623 
49336439Sml29623 	/* function number */
49346439Sml29623 	if (strcmp(pr_name, "_function_number") == 0) {
49356512Ssowmini 		if (is_default)
49366512Ssowmini 			return (ENOTSUP);
49378118SVasumathi.Sundaram@Sun.COM 		*perm = MAC_PROP_PERM_READ;
49386512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%d",
49396512Ssowmini 		    nxgep->function_num);
49406439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49416439Sml29623 		    "==> nxge_get_priv_prop: name %s "
49426439Sml29623 		    "(value %d valstr %s)",
49436439Sml29623 		    pr_name, nxgep->function_num, valstr));
49446439Sml29623 
49456439Sml29623 		err = 0;
49466439Sml29623 		goto done;
49476439Sml29623 	}
49486439Sml29623 
49496439Sml29623 	/* Neptune firmware version */
49506439Sml29623 	if (strcmp(pr_name, "_fw_version") == 0) {
49516512Ssowmini 		if (is_default)
49526512Ssowmini 			return (ENOTSUP);
49538118SVasumathi.Sundaram@Sun.COM 		*perm = MAC_PROP_PERM_READ;
49546512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%s",
49556512Ssowmini 		    nxgep->vpd_info.ver);
49566439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
49576439Sml29623 		    "==> nxge_get_priv_prop: name %s "
49586439Sml29623 		    "(value %d valstr %s)",
49596439Sml29623 		    pr_name, nxgep->vpd_info.ver, valstr));
49606439Sml29623 
49616439Sml29623 		err = 0;
49626439Sml29623 		goto done;
49636439Sml29623 	}
49646439Sml29623 
49656439Sml29623 	/* port PHY mode */
49666439Sml29623 	if (strcmp(pr_name, "_port_mode") == 0) {
49676512Ssowmini 		if (is_default)
49686512Ssowmini 			return (ENOTSUP);
49698118SVasumathi.Sundaram@Sun.COM 		*perm = MAC_PROP_PERM_READ;
49706439Sml29623 		switch (nxgep->mac.portmode) {
49716439Sml29623 		case PORT_1G_COPPER:
49726512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "1G copper %s",
49736439Sml29623 			    nxgep->hot_swappable_phy ?
49746439Sml29623 			    "[Hot Swappable]" : "");
49756439Sml29623 			break;
49766439Sml29623 		case PORT_1G_FIBER:
49776512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "1G fiber %s",
49786439Sml29623 			    nxgep->hot_swappable_phy ?
49796439Sml29623 			    "[hot swappable]" : "");
49806439Sml29623 			break;
49816439Sml29623 		case PORT_10G_COPPER:
49826512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
49836512Ssowmini 			    "10G copper %s",
49846439Sml29623 			    nxgep->hot_swappable_phy ?
49856439Sml29623 			    "[hot swappable]" : "");
49866439Sml29623 			break;
49876439Sml29623 		case PORT_10G_FIBER:
49886512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "10G fiber %s",
49896439Sml29623 			    nxgep->hot_swappable_phy ?
49906439Sml29623 			    "[hot swappable]" : "");
49916439Sml29623 			break;
49926439Sml29623 		case PORT_10G_SERDES:
49936512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
49946512Ssowmini 			    "10G serdes %s", nxgep->hot_swappable_phy ?
49956439Sml29623 			    "[hot swappable]" : "");
49966439Sml29623 			break;
49976439Sml29623 		case PORT_1G_SERDES:
49986512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "1G serdes %s",
49996439Sml29623 			    nxgep->hot_swappable_phy ?
50006439Sml29623 			    "[hot swappable]" : "");
50016439Sml29623 			break;
50026835Syc148097 		case PORT_1G_TN1010:
50036835Syc148097 			(void) snprintf(valstr, sizeof (valstr),
50046835Syc148097 			    "1G TN1010 copper %s", nxgep->hot_swappable_phy ?
50056835Syc148097 			    "[hot swappable]" : "");
50066835Syc148097 			break;
50076835Syc148097 		case PORT_10G_TN1010:
50086835Syc148097 			(void) snprintf(valstr, sizeof (valstr),
50096835Syc148097 			    "10G TN1010 copper %s", nxgep->hot_swappable_phy ?
50106835Syc148097 			    "[hot swappable]" : "");
50116835Syc148097 			break;
50126439Sml29623 		case PORT_1G_RGMII_FIBER:
50136512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
50146512Ssowmini 			    "1G rgmii fiber %s", nxgep->hot_swappable_phy ?
50156439Sml29623 			    "[hot swappable]" : "");
50166439Sml29623 			break;
50176439Sml29623 		case PORT_HSP_MODE:
50186512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
50196444Sml29623 			    "phy not present[hot swappable]");
50206439Sml29623 			break;
50216439Sml29623 		default:
50226512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "unknown %s",
50236439Sml29623 			    nxgep->hot_swappable_phy ?
50246439Sml29623 			    "[hot swappable]" : "");
50256439Sml29623 			break;
50266439Sml29623 		}
50276439Sml29623 
50286439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50296439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %s)",
50306439Sml29623 		    pr_name, valstr));
50316439Sml29623 
50326439Sml29623 		err = 0;
50336439Sml29623 		goto done;
50346439Sml29623 	}
50356439Sml29623 
50366439Sml29623 	/* Hot swappable PHY */
50376439Sml29623 	if (strcmp(pr_name, "_hot_swap_phy") == 0) {
50386512Ssowmini 		if (is_default)
50396512Ssowmini 			return (ENOTSUP);
50408118SVasumathi.Sundaram@Sun.COM 		*perm = MAC_PROP_PERM_READ;
50416512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%s",
50426439Sml29623 		    nxgep->hot_swappable_phy ?
50436439Sml29623 		    "yes" : "no");
50446439Sml29623 
50456439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50466439Sml29623 		    "==> nxge_get_priv_prop: name %s "
50476439Sml29623 		    "(value %d valstr %s)",
50486439Sml29623 		    pr_name, nxgep->hot_swappable_phy, valstr));
50496439Sml29623 
50506439Sml29623 		err = 0;
50516439Sml29623 		goto done;
50526439Sml29623 	}
50536439Sml29623 
50546439Sml29623 
50556439Sml29623 	/* accept jumbo */
50566439Sml29623 	if (strcmp(pr_name, "_accept_jumbo") == 0) {
50576512Ssowmini 		if (is_default)
50586512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),  "%d", 0);
50596512Ssowmini 		else
50606512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
50616512Ssowmini 			    "%d", nxgep->mac.is_jumbo);
50626439Sml29623 		err = 0;
50636439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50646439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %d (%d, %d))",
50656439Sml29623 		    pr_name,
50666439Sml29623 		    (uint32_t)param_arr[param_accept_jumbo].value,
50676439Sml29623 		    nxgep->mac.is_jumbo,
50686439Sml29623 		    nxge_jumbo_enable));
50696439Sml29623 
50706439Sml29623 		goto done;
50716439Sml29623 	}
50726439Sml29623 
50736439Sml29623 	/* Receive Interrupt Blanking Parameters */
50746439Sml29623 	if (strcmp(pr_name, "_rxdma_intr_time") == 0) {
50756512Ssowmini 		err = 0;
50766512Ssowmini 		if (is_default) {
50776512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
50786512Ssowmini 			    "%d", RXDMA_RCR_TO_DEFAULT);
50796512Ssowmini 			goto done;
50806512Ssowmini 		}
50816512Ssowmini 
50826512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%d",
50836512Ssowmini 		    nxgep->intr_timeout);
50846439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
50856439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %d)",
50866439Sml29623 		    pr_name,
50876439Sml29623 		    (uint32_t)nxgep->intr_timeout));
50886439Sml29623 		goto done;
50896439Sml29623 	}
50906439Sml29623 
50916439Sml29623 	if (strcmp(pr_name, "_rxdma_intr_pkts") == 0) {
50926512Ssowmini 		err = 0;
50936512Ssowmini 		if (is_default) {
50946512Ssowmini 			(void) snprintf(valstr, sizeof (valstr),
50956512Ssowmini 			    "%d", RXDMA_RCR_PTHRES_DEFAULT);
50966512Ssowmini 			goto done;
50976512Ssowmini 		}
50986512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%d",
50996512Ssowmini 		    nxgep->intr_threshold);
51006439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51016439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %d)",
51026439Sml29623 		    pr_name, (uint32_t)nxgep->intr_threshold));
51036439Sml29623 
51046439Sml29623 		goto done;
51056439Sml29623 	}
51066439Sml29623 
51076439Sml29623 	/* Classification and Load Distribution Configuration */
51086439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_tcp") == 0) {
51096512Ssowmini 		if (is_default) {
51106512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
51116512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
51126512Ssowmini 			err = 0;
51136512Ssowmini 			goto done;
51146512Ssowmini 		}
51156439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
51166439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_tcp]);
51176439Sml29623 
51186512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
51196439Sml29623 		    (int)param_arr[param_class_opt_ipv4_tcp].value);
51206439Sml29623 
51216439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51226439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
51236439Sml29623 		goto done;
51246439Sml29623 	}
51256439Sml29623 
51266439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_udp") == 0) {
51276512Ssowmini 		if (is_default) {
51286512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
51296512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
51306512Ssowmini 			err = 0;
51316512Ssowmini 			goto done;
51326512Ssowmini 		}
51336439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
51346439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_udp]);
51356439Sml29623 
51366512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
51376439Sml29623 		    (int)param_arr[param_class_opt_ipv4_udp].value);
51386439Sml29623 
51396439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51406439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
51416439Sml29623 		goto done;
51426439Sml29623 	}
51436439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_ah") == 0) {
51446512Ssowmini 		if (is_default) {
51456512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
51466512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
51476512Ssowmini 			err = 0;
51486512Ssowmini 			goto done;
51496512Ssowmini 		}
51506439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
51516439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_ah]);
51526439Sml29623 
51536512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
51546439Sml29623 		    (int)param_arr[param_class_opt_ipv4_ah].value);
51556439Sml29623 
51566439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51576439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
51586439Sml29623 		goto done;
51596439Sml29623 	}
51606439Sml29623 
51616439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv4_sctp") == 0) {
51626512Ssowmini 		if (is_default) {
51636512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
51646512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
51656512Ssowmini 			err = 0;
51666512Ssowmini 			goto done;
51676512Ssowmini 		}
51686439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
51696439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv4_sctp]);
51706439Sml29623 
51716512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
51726439Sml29623 		    (int)param_arr[param_class_opt_ipv4_sctp].value);
51736439Sml29623 
51746439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51756439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
51766439Sml29623 		goto done;
51776439Sml29623 	}
51786439Sml29623 
51796439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_tcp") == 0) {
51806512Ssowmini 		if (is_default) {
51816512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
51826512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
51836512Ssowmini 			err = 0;
51846512Ssowmini 			goto done;
51856512Ssowmini 		}
51866439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
51876439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_tcp]);
51886439Sml29623 
51896512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
51906439Sml29623 		    (int)param_arr[param_class_opt_ipv6_tcp].value);
51916439Sml29623 
51926439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
51936439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
51946439Sml29623 		goto done;
51956439Sml29623 	}
51966439Sml29623 
51976439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_udp") == 0) {
51986512Ssowmini 		if (is_default) {
51996512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
52006512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
52016512Ssowmini 			err = 0;
52026512Ssowmini 			goto done;
52036512Ssowmini 		}
52046439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
52056439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_udp]);
52066439Sml29623 
52076512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
52086439Sml29623 		    (int)param_arr[param_class_opt_ipv6_udp].value);
52096439Sml29623 
52106439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
52116439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
52126439Sml29623 		goto done;
52136439Sml29623 	}
52146439Sml29623 
52156439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_ah") == 0) {
52166512Ssowmini 		if (is_default) {
52176512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
52186512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
52196512Ssowmini 			err = 0;
52206512Ssowmini 			goto done;
52216512Ssowmini 		}
52226439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
52236439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_ah]);
52246439Sml29623 
52256512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
52266439Sml29623 		    (int)param_arr[param_class_opt_ipv6_ah].value);
52276439Sml29623 
52286439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
52296439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
52306439Sml29623 		goto done;
52316439Sml29623 	}
52326439Sml29623 
52336439Sml29623 	if (strcmp(pr_name, "_class_opt_ipv6_sctp") == 0) {
52346512Ssowmini 		if (is_default) {
52356512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%x",
52366512Ssowmini 			    NXGE_CLASS_FLOW_GEN_SERVER);
52376512Ssowmini 			err = 0;
52386512Ssowmini 			goto done;
52396512Ssowmini 		}
52406439Sml29623 		err = nxge_dld_get_ip_opt(nxgep,
52416439Sml29623 		    (caddr_t)&param_arr[param_class_opt_ipv6_sctp]);
52426439Sml29623 
52436512Ssowmini 		(void) snprintf(valstr, sizeof (valstr), "%x",
52446439Sml29623 		    (int)param_arr[param_class_opt_ipv6_sctp].value);
52456439Sml29623 
52466439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
52476439Sml29623 		    "==> nxge_get_priv_prop: %s", valstr));
52486439Sml29623 		goto done;
52496439Sml29623 	}
52506439Sml29623 
52516439Sml29623 	/* Software LSO */
52526439Sml29623 	if (strcmp(pr_name, "_soft_lso_enable") == 0) {
52536512Ssowmini 		if (is_default) {
52546512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 0);
52556512Ssowmini 			err = 0;
52566512Ssowmini 			goto done;
52576512Ssowmini 		}
52586512Ssowmini 		(void) snprintf(valstr, sizeof (valstr),
52596512Ssowmini 		    "%d", nxgep->soft_lso_enable);
52606439Sml29623 		err = 0;
52616439Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
52626439Sml29623 		    "==> nxge_get_priv_prop: name %s (value %d)",
52636439Sml29623 		    pr_name, nxgep->soft_lso_enable));
52646439Sml29623 
52656439Sml29623 		goto done;
52666439Sml29623 	}
52676512Ssowmini 	if (strcmp(pr_name, "_adv_10gfdx_cap") == 0) {
52686512Ssowmini 		err = 0;
52696512Ssowmini 		if (is_default ||
52706512Ssowmini 		    nxgep->param_arr[param_anar_10gfdx].value != 0) {
52716512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 1);
52726512Ssowmini 			goto done;
52736512Ssowmini 		} else {
52746512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 0);
52756512Ssowmini 			goto done;
52766512Ssowmini 		}
52776512Ssowmini 	}
52786512Ssowmini 	if (strcmp(pr_name, "_adv_pause_cap") == 0) {
52796512Ssowmini 		err = 0;
52806512Ssowmini 		if (is_default ||
52816512Ssowmini 		    nxgep->param_arr[param_anar_pause].value != 0) {
52826512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 1);
52836512Ssowmini 			goto done;
52846512Ssowmini 		} else {
52856512Ssowmini 			(void) snprintf(valstr, sizeof (valstr), "%d", 0);
52866512Ssowmini 			goto done;
52876512Ssowmini 		}
52886512Ssowmini 	}
52896439Sml29623 
52906439Sml29623 done:
52916439Sml29623 	if (err == 0) {
52926439Sml29623 		strsize = (uint_t)strlen(valstr);
52936439Sml29623 		if (pr_valsize < strsize) {
52946439Sml29623 			err = ENOBUFS;
52956439Sml29623 		} else {
52966439Sml29623 			(void) strlcpy(pr_val, valstr, pr_valsize);
52976439Sml29623 		}
52986439Sml29623 	}
52996439Sml29623 
53006439Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_CTL,
53016439Sml29623 	    "<== nxge_get_priv_prop: return %d", err));
53026439Sml29623 	return (err);
53036439Sml29623 }
53046439Sml29623 
53053859Sml29623 /*
53063859Sml29623  * Module loading and removing entry points.
53073859Sml29623  */
53083859Sml29623 
53096705Sml29623 DDI_DEFINE_STREAM_OPS(nxge_dev_ops, nulldev, nulldev, nxge_attach, nxge_detach,
53107656SSherry.Moore@Sun.COM     nodev, NULL, D_MP, NULL, nxge_quiesce);
53113859Sml29623 
53124977Sraghus #define	NXGE_DESC_VER		"Sun NIU 10Gb Ethernet"
53133859Sml29623 
53143859Sml29623 /*
53153859Sml29623  * Module linkage information for the kernel.
53163859Sml29623  */
53173859Sml29623 static struct modldrv 	nxge_modldrv = {
53183859Sml29623 	&mod_driverops,
53193859Sml29623 	NXGE_DESC_VER,
53203859Sml29623 	&nxge_dev_ops
53213859Sml29623 };
53223859Sml29623 
53233859Sml29623 static struct modlinkage modlinkage = {
53243859Sml29623 	MODREV_1, (void *) &nxge_modldrv, NULL
53253859Sml29623 };
53263859Sml29623 
53273859Sml29623 int
53283859Sml29623 _init(void)
53293859Sml29623 {
53303859Sml29623 	int		status;
53313859Sml29623 
53323859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "==> _init"));
53333859Sml29623 	mac_init_ops(&nxge_dev_ops, "nxge");
53343859Sml29623 	status = ddi_soft_state_init(&nxge_list, sizeof (nxge_t), 0);
53353859Sml29623 	if (status != 0) {
53363859Sml29623 		NXGE_ERROR_MSG((NULL, NXGE_ERR_CTL,
53376512Ssowmini 		    "failed to init device soft state"));
53383859Sml29623 		goto _init_exit;
53393859Sml29623 	}
53403859Sml29623 	status = mod_install(&modlinkage);
53413859Sml29623 	if (status != 0) {
53423859Sml29623 		ddi_soft_state_fini(&nxge_list);
53433859Sml29623 		NXGE_ERROR_MSG((NULL, NXGE_ERR_CTL, "Mod install failed"));
53443859Sml29623 		goto _init_exit;
53453859Sml29623 	}
53463859Sml29623 
53473859Sml29623 	MUTEX_INIT(&nxge_common_lock, NULL, MUTEX_DRIVER, NULL);
53483859Sml29623 
53493859Sml29623 _init_exit:
53503859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "_init status = 0x%X", status));
53513859Sml29623 
53523859Sml29623 	return (status);
53533859Sml29623 }
53543859Sml29623 
53553859Sml29623 int
53563859Sml29623 _fini(void)
53573859Sml29623 {
53583859Sml29623 	int		status;
53593859Sml29623 
53603859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "==> _fini"));
53613859Sml29623 
53623859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "==> _fini: mod_remove"));
53633859Sml29623 
53643859Sml29623 	if (nxge_mblks_pending)
53653859Sml29623 		return (EBUSY);
53663859Sml29623 
53673859Sml29623 	status = mod_remove(&modlinkage);
53683859Sml29623 	if (status != DDI_SUCCESS) {
53693859Sml29623 		NXGE_DEBUG_MSG((NULL, MOD_CTL,
53706512Ssowmini 		    "Module removal failed 0x%08x",
53716512Ssowmini 		    status));
53723859Sml29623 		goto _fini_exit;
53733859Sml29623 	}
53743859Sml29623 
53753859Sml29623 	mac_fini_ops(&nxge_dev_ops);
53763859Sml29623 
53773859Sml29623 	ddi_soft_state_fini(&nxge_list);
53783859Sml29623 
53793859Sml29623 	MUTEX_DESTROY(&nxge_common_lock);
53803859Sml29623 _fini_exit:
53813859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "_fini status = 0x%08x", status));
53823859Sml29623 
53833859Sml29623 	return (status);
53843859Sml29623 }
53853859Sml29623 
53863859Sml29623 int
53873859Sml29623 _info(struct modinfo *modinfop)
53883859Sml29623 {
53893859Sml29623 	int		status;
53903859Sml29623 
53913859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, "==> _info"));
53923859Sml29623 	status = mod_info(&modlinkage, modinfop);
53933859Sml29623 	NXGE_DEBUG_MSG((NULL, MOD_CTL, " _info status = 0x%X", status));
53943859Sml29623 
53953859Sml29623 	return (status);
53963859Sml29623 }
53973859Sml29623 
53983859Sml29623 /*ARGSUSED*/
53998275SEric Cheng static int
54008275SEric Cheng nxge_tx_ring_start(mac_ring_driver_t rdriver, uint64_t mr_gen_num)
54018275SEric Cheng {
54028275SEric Cheng 	p_nxge_ring_handle_t	rhp = (p_nxge_ring_handle_t)rdriver;
54038275SEric Cheng 	p_nxge_t		nxgep = rhp->nxgep;
54048275SEric Cheng 	uint32_t		channel;
54058275SEric Cheng 	p_tx_ring_t		ring;
54068275SEric Cheng 
54078275SEric Cheng 	channel = nxgep->pt_config.hw_config.tdc.start + rhp->index;
54088275SEric Cheng 	ring = nxgep->tx_rings->rings[channel];
54098275SEric Cheng 
54108275SEric Cheng 	MUTEX_ENTER(&ring->lock);
54118275SEric Cheng 	ring->tx_ring_handle = rhp->ring_handle;
54128275SEric Cheng 	MUTEX_EXIT(&ring->lock);
54138275SEric Cheng 
54148275SEric Cheng 	return (0);
54158275SEric Cheng }
54168275SEric Cheng 
54178275SEric Cheng static void
54188275SEric Cheng nxge_tx_ring_stop(mac_ring_driver_t rdriver)
54198275SEric Cheng {
54208275SEric Cheng 	p_nxge_ring_handle_t	rhp = (p_nxge_ring_handle_t)rdriver;
54218275SEric Cheng 	p_nxge_t		nxgep = rhp->nxgep;
54228275SEric Cheng 	uint32_t		channel;
54238275SEric Cheng 	p_tx_ring_t		ring;
54248275SEric Cheng 
54258275SEric Cheng 	channel = nxgep->pt_config.hw_config.tdc.start + rhp->index;
54268275SEric Cheng 	ring = nxgep->tx_rings->rings[channel];
54278275SEric Cheng 
54288275SEric Cheng 	MUTEX_ENTER(&ring->lock);
54298275SEric Cheng 	ring->tx_ring_handle = (mac_ring_handle_t)NULL;
54308275SEric Cheng 	MUTEX_EXIT(&ring->lock);
54318275SEric Cheng }
54328275SEric Cheng 
54338275SEric Cheng static int
54348275SEric Cheng nxge_rx_ring_start(mac_ring_driver_t rdriver, uint64_t mr_gen_num)
54358275SEric Cheng {
54368275SEric Cheng 	p_nxge_ring_handle_t	rhp = (p_nxge_ring_handle_t)rdriver;
54378275SEric Cheng 	p_nxge_t		nxgep = rhp->nxgep;
54388275SEric Cheng 	uint32_t		channel;
54398275SEric Cheng 	p_rx_rcr_ring_t		ring;
54408275SEric Cheng 	int			i;
54418275SEric Cheng 
54428275SEric Cheng 	channel = nxgep->pt_config.hw_config.start_rdc + rhp->index;
54438275SEric Cheng 	ring =  nxgep->rx_rcr_rings->rcr_rings[channel];
54448275SEric Cheng 
54458275SEric Cheng 	MUTEX_ENTER(&ring->lock);
54468275SEric Cheng 
54478275SEric Cheng 	if (nxgep->rx_channel_started[channel] == B_TRUE) {
54488275SEric Cheng 		MUTEX_EXIT(&ring->lock);
54498275SEric Cheng 		return (0);
54508275SEric Cheng 	}
54518275SEric Cheng 
54528275SEric Cheng 	/* set rcr_ring */
54538275SEric Cheng 	for (i = 0; i < nxgep->ldgvp->maxldvs; i++) {
54548275SEric Cheng 		if ((nxgep->ldgvp->ldvp[i].is_rxdma == 1) &&
54558275SEric Cheng 		    (nxgep->ldgvp->ldvp[i].channel == channel)) {
54568275SEric Cheng 			ring->ldvp = &nxgep->ldgvp->ldvp[i];
54578275SEric Cheng 			ring->ldgp = nxgep->ldgvp->ldvp[i].ldgp;
54588275SEric Cheng 		}
54598275SEric Cheng 	}
54608275SEric Cheng 
54618275SEric Cheng 	nxgep->rx_channel_started[channel] = B_TRUE;
54628275SEric Cheng 	ring->rcr_mac_handle = rhp->ring_handle;
54638275SEric Cheng 	ring->rcr_gen_num = mr_gen_num;
54648275SEric Cheng 	MUTEX_EXIT(&ring->lock);
54658275SEric Cheng 
54668275SEric Cheng 	return (0);
54678275SEric Cheng }
54688275SEric Cheng 
54698275SEric Cheng static void
54708275SEric Cheng nxge_rx_ring_stop(mac_ring_driver_t rdriver)
54718275SEric Cheng {
54728275SEric Cheng 	p_nxge_ring_handle_t	rhp = (p_nxge_ring_handle_t)rdriver;
54738275SEric Cheng 	p_nxge_t		nxgep = rhp->nxgep;
54748275SEric Cheng 	uint32_t		channel;
54758275SEric Cheng 	p_rx_rcr_ring_t		ring;
54768275SEric Cheng 
54778275SEric Cheng 	channel = nxgep->pt_config.hw_config.start_rdc + rhp->index;
54788275SEric Cheng 	ring =  nxgep->rx_rcr_rings->rcr_rings[channel];
54798275SEric Cheng 
54808275SEric Cheng 	MUTEX_ENTER(&ring->lock);
54818275SEric Cheng 	nxgep->rx_channel_started[channel] = B_FALSE;
54828275SEric Cheng 	ring->rcr_mac_handle = NULL;
54838275SEric Cheng 	MUTEX_EXIT(&ring->lock);
54848275SEric Cheng }
54858275SEric Cheng 
54868275SEric Cheng /*
54878275SEric Cheng  * Callback funtion for MAC layer to register all rings.
54888275SEric Cheng  */
54898275SEric Cheng static void
54908275SEric Cheng nxge_fill_ring(void *arg, mac_ring_type_t rtype, const int rg_index,
54918275SEric Cheng     const int index, mac_ring_info_t *infop, mac_ring_handle_t rh)
54928275SEric Cheng {
54938275SEric Cheng 	p_nxge_t		nxgep = (p_nxge_t)arg;
54948275SEric Cheng 	p_nxge_hw_pt_cfg_t	p_cfgp = &nxgep->pt_config.hw_config;
54958275SEric Cheng 
54968275SEric Cheng 	NXGE_DEBUG_MSG((nxgep, TX_CTL,
54978275SEric Cheng 	    "==> nxge_fill_ring 0x%x index %d", rtype, index));
54988275SEric Cheng 
54998275SEric Cheng 	switch (rtype) {
55008275SEric Cheng 	case MAC_RING_TYPE_TX: {
55018275SEric Cheng 		p_nxge_ring_handle_t	rhandlep;
55028275SEric Cheng 
55038275SEric Cheng 		NXGE_DEBUG_MSG((nxgep, TX_CTL,
55048275SEric Cheng 		    "==> nxge_fill_ring (TX) 0x%x index %d ntdcs %d",
55058275SEric Cheng 		    rtype, index, p_cfgp->tdc.count));
55068275SEric Cheng 
55078275SEric Cheng 		ASSERT((index >= 0) && (index < p_cfgp->tdc.count));
55088275SEric Cheng 		rhandlep = &nxgep->tx_ring_handles[index];
55098275SEric Cheng 		rhandlep->nxgep = nxgep;
55108275SEric Cheng 		rhandlep->index = index;
55118275SEric Cheng 		rhandlep->ring_handle = rh;
55128275SEric Cheng 
55138275SEric Cheng 		infop->mri_driver = (mac_ring_driver_t)rhandlep;
55148275SEric Cheng 		infop->mri_start = nxge_tx_ring_start;
55158275SEric Cheng 		infop->mri_stop = nxge_tx_ring_stop;
55168275SEric Cheng 		infop->mri_tx = nxge_tx_ring_send;
55178275SEric Cheng 
55188275SEric Cheng 		break;
55198275SEric Cheng 	}
55208275SEric Cheng 	case MAC_RING_TYPE_RX: {
55218275SEric Cheng 		p_nxge_ring_handle_t	rhandlep;
55228275SEric Cheng 		int			nxge_rindex;
55238275SEric Cheng 		mac_intr_t		nxge_mac_intr;
55248275SEric Cheng 
55258275SEric Cheng 		NXGE_DEBUG_MSG((nxgep, RX_CTL,
55268275SEric Cheng 		    "==> nxge_fill_ring (RX) 0x%x index %d nrdcs %d",
55278275SEric Cheng 		    rtype, index, p_cfgp->max_rdcs));
55288275SEric Cheng 
55298275SEric Cheng 		/*
55308275SEric Cheng 		 * 'index' is the ring index within the group.
55318275SEric Cheng 		 * Find the ring index in the nxge instance.
55328275SEric Cheng 		 */
55338275SEric Cheng 		nxge_rindex = nxge_get_rxring_index(nxgep, rg_index, index);
55348275SEric Cheng 
55358275SEric Cheng 		ASSERT((nxge_rindex >= 0) && (nxge_rindex < p_cfgp->max_rdcs));
55368275SEric Cheng 		rhandlep = &nxgep->rx_ring_handles[nxge_rindex];
55378275SEric Cheng 		rhandlep->nxgep = nxgep;
55388275SEric Cheng 		rhandlep->index = nxge_rindex;
55398275SEric Cheng 		rhandlep->ring_handle = rh;
55408275SEric Cheng 
55418275SEric Cheng 		/*
55428275SEric Cheng 		 * Entrypoint to enable interrupt (disable poll) and
55438275SEric Cheng 		 * disable interrupt (enable poll).
55448275SEric Cheng 		 */
55458275SEric Cheng 		nxge_mac_intr.mi_handle = (mac_intr_handle_t)rhandlep;
55468275SEric Cheng 		nxge_mac_intr.mi_enable = (mac_intr_enable_t)nxge_disable_poll;
55478275SEric Cheng 		nxge_mac_intr.mi_disable = (mac_intr_disable_t)nxge_enable_poll;
55488275SEric Cheng 		infop->mri_driver = (mac_ring_driver_t)rhandlep;
55498275SEric Cheng 		infop->mri_start = nxge_rx_ring_start;
55508275SEric Cheng 		infop->mri_stop = nxge_rx_ring_stop;
55518275SEric Cheng 		infop->mri_intr = nxge_mac_intr; /* ??? */
55528275SEric Cheng 		infop->mri_poll = nxge_rx_poll;
55538275SEric Cheng 
55548275SEric Cheng 		break;
55558275SEric Cheng 	}
55568275SEric Cheng 	default:
55578275SEric Cheng 		break;
55588275SEric Cheng 	}
55598275SEric Cheng 
55608275SEric Cheng 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_fill_ring 0x%x",
55618275SEric Cheng 	    rtype));
55628275SEric Cheng }
55638275SEric Cheng 
55648275SEric Cheng static void
55658275SEric Cheng nxge_group_add_ring(mac_group_driver_t gh, mac_ring_driver_t rh,
55668275SEric Cheng     mac_ring_type_t type)
55678275SEric Cheng {
55688275SEric Cheng 	nxge_ring_group_t	*rgroup = (nxge_ring_group_t *)gh;
55698275SEric Cheng 	nxge_ring_handle_t	*rhandle = (nxge_ring_handle_t *)rh;
55708275SEric Cheng 	nxge_t			*nxge;
55718275SEric Cheng 	nxge_grp_t		*grp;
55728275SEric Cheng 	nxge_rdc_grp_t		*rdc_grp;
55738275SEric Cheng 	uint16_t		channel;	/* device-wise ring id */
55748275SEric Cheng 	int			dev_gindex;
55758275SEric Cheng 	int			rv;
55768275SEric Cheng 
55778275SEric Cheng 	nxge = rgroup->nxgep;
55788275SEric Cheng 
55798275SEric Cheng 	switch (type) {
55808275SEric Cheng 	case MAC_RING_TYPE_TX:
55818275SEric Cheng 		/*
55828275SEric Cheng 		 * nxge_grp_dc_add takes a channel number which is a
55838275SEric Cheng 		 * "devise" ring ID.
55848275SEric Cheng 		 */
55858275SEric Cheng 		channel = nxge->pt_config.hw_config.tdc.start + rhandle->index;
55868275SEric Cheng 
55878275SEric Cheng 		/*
55888275SEric Cheng 		 * Remove the ring from the default group
55898275SEric Cheng 		 */
55908275SEric Cheng 		if (rgroup->gindex != 0) {
55918275SEric Cheng 			(void) nxge_grp_dc_remove(nxge, VP_BOUND_TX, channel);
55928275SEric Cheng 		}
55938275SEric Cheng 
55948275SEric Cheng 		/*
55958275SEric Cheng 		 * nxge->tx_set.group[] is an array of groups indexed by
55968275SEric Cheng 		 * a "port" group ID.
55978275SEric Cheng 		 */
55988275SEric Cheng 		grp = nxge->tx_set.group[rgroup->gindex];
55998275SEric Cheng 		rv = nxge_grp_dc_add(nxge, grp, VP_BOUND_TX, channel);
56008275SEric Cheng 		if (rv != 0) {
56018275SEric Cheng 			NXGE_ERROR_MSG((nxge, NXGE_ERR_CTL,
56028275SEric Cheng 			    "nxge_group_add_ring: nxge_grp_dc_add failed"));
56038275SEric Cheng 		}
56048275SEric Cheng 		break;
56058275SEric Cheng 
56068275SEric Cheng 	case MAC_RING_TYPE_RX:
56078275SEric Cheng 		/*
56088275SEric Cheng 		 * nxge->rx_set.group[] is an array of groups indexed by
56098275SEric Cheng 		 * a "port" group ID.
56108275SEric Cheng 		 */
56118275SEric Cheng 		grp = nxge->rx_set.group[rgroup->gindex];
56128275SEric Cheng 
56138275SEric Cheng 		dev_gindex = nxge->pt_config.hw_config.def_mac_rxdma_grpid +
56148275SEric Cheng 		    rgroup->gindex;
56158275SEric Cheng 		rdc_grp = &nxge->pt_config.rdc_grps[dev_gindex];
56168275SEric Cheng 
56178275SEric Cheng 		/*
56188275SEric Cheng 		 * nxge_grp_dc_add takes a channel number which is a
56198275SEric Cheng 		 * "devise" ring ID.
56208275SEric Cheng 		 */
56218275SEric Cheng 		channel = nxge->pt_config.hw_config.start_rdc + rhandle->index;
56228275SEric Cheng 		rv = nxge_grp_dc_add(nxge, grp, VP_BOUND_RX, channel);
56238275SEric Cheng 		if (rv != 0) {
56248275SEric Cheng 			NXGE_ERROR_MSG((nxge, NXGE_ERR_CTL,
56258275SEric Cheng 			    "nxge_group_add_ring: nxge_grp_dc_add failed"));
56268275SEric Cheng 		}
56278275SEric Cheng 
56288275SEric Cheng 		rdc_grp->map |= (1 << channel);
56298275SEric Cheng 		rdc_grp->max_rdcs++;
56308275SEric Cheng 
5631*9047SMichael.Speer@Sun.COM 		(void) nxge_init_fzc_rdc_tbl(nxge, rdc_grp, rgroup->rdctbl);
56328275SEric Cheng 		break;
56338275SEric Cheng 	}
56348275SEric Cheng }
56358275SEric Cheng 
56368275SEric Cheng static void
56378275SEric Cheng nxge_group_rem_ring(mac_group_driver_t gh, mac_ring_driver_t rh,
56388275SEric Cheng     mac_ring_type_t type)
56398275SEric Cheng {
56408275SEric Cheng 	nxge_ring_group_t	*rgroup = (nxge_ring_group_t *)gh;
56418275SEric Cheng 	nxge_ring_handle_t	*rhandle = (nxge_ring_handle_t *)rh;
56428275SEric Cheng 	nxge_t			*nxge;
56438275SEric Cheng 	uint16_t		channel;	/* device-wise ring id */
56448275SEric Cheng 	nxge_rdc_grp_t		*rdc_grp;
56458275SEric Cheng 	int			dev_gindex;
56468275SEric Cheng 
56478275SEric Cheng 	nxge = rgroup->nxgep;
56488275SEric Cheng 
56498275SEric Cheng 	switch (type) {
56508275SEric Cheng 	case MAC_RING_TYPE_TX:
56518275SEric Cheng 		dev_gindex = nxge->pt_config.hw_config.def_mac_txdma_grpid +
56528275SEric Cheng 		    rgroup->gindex;
56538275SEric Cheng 		channel = nxge->pt_config.hw_config.tdc.start + rhandle->index;
56548275SEric Cheng 		nxge_grp_dc_remove(nxge, VP_BOUND_TX, channel);
56558275SEric Cheng 
56568275SEric Cheng 		/*
56578275SEric Cheng 		 * Add the ring back to the default group
56588275SEric Cheng 		 */
56598275SEric Cheng 		if (rgroup->gindex != 0) {
56608275SEric Cheng 			nxge_grp_t *grp;
56618275SEric Cheng 			grp = nxge->tx_set.group[0];
56628275SEric Cheng 			(void) nxge_grp_dc_add(nxge, grp, VP_BOUND_TX, channel);
56638275SEric Cheng 		}
56648275SEric Cheng 		break;
56658275SEric Cheng 
56668275SEric Cheng 	case MAC_RING_TYPE_RX:
56678275SEric Cheng 		dev_gindex = nxge->pt_config.hw_config.def_mac_rxdma_grpid +
56688275SEric Cheng 		    rgroup->gindex;
56698275SEric Cheng 		rdc_grp = &nxge->pt_config.rdc_grps[dev_gindex];
56708275SEric Cheng 		channel = rdc_grp->start_rdc + rhandle->index;
56718275SEric Cheng 		nxge_grp_dc_remove(nxge, VP_BOUND_RX, channel);
56728275SEric Cheng 
56738275SEric Cheng 		rdc_grp->map &= ~(1 << channel);
56748275SEric Cheng 		rdc_grp->max_rdcs--;
56758275SEric Cheng 
5676*9047SMichael.Speer@Sun.COM 		(void) nxge_init_fzc_rdc_tbl(nxge, rdc_grp, rgroup->rdctbl);
56778275SEric Cheng 		break;
56788275SEric Cheng 	}
56798275SEric Cheng }
56808275SEric Cheng 
56818275SEric Cheng 
56828275SEric Cheng /*ARGSUSED*/
56833859Sml29623 static nxge_status_t
56843859Sml29623 nxge_add_intrs(p_nxge_t nxgep)
56853859Sml29623 {
56863859Sml29623 
56873859Sml29623 	int		intr_types;
56883859Sml29623 	int		type = 0;
56893859Sml29623 	int		ddi_status = DDI_SUCCESS;
56903859Sml29623 	nxge_status_t	status = NXGE_OK;
56913859Sml29623 
56923859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs"));
56933859Sml29623 
56943859Sml29623 	nxgep->nxge_intr_type.intr_registered = B_FALSE;
56953859Sml29623 	nxgep->nxge_intr_type.intr_enabled = B_FALSE;
56963859Sml29623 	nxgep->nxge_intr_type.msi_intx_cnt = 0;
56973859Sml29623 	nxgep->nxge_intr_type.intr_added = 0;
56983859Sml29623 	nxgep->nxge_intr_type.niu_msi_enable = B_FALSE;
56993859Sml29623 	nxgep->nxge_intr_type.intr_type = 0;
57003859Sml29623 
57013859Sml29623 	if (nxgep->niu_type == N2_NIU) {
57023859Sml29623 		nxgep->nxge_intr_type.niu_msi_enable = B_TRUE;
57033859Sml29623 	} else if (nxge_msi_enable) {
57043859Sml29623 		nxgep->nxge_intr_type.niu_msi_enable = B_TRUE;
57053859Sml29623 	}
57063859Sml29623 
57073859Sml29623 	/* Get the supported interrupt types */
57083859Sml29623 	if ((ddi_status = ddi_intr_get_supported_types(nxgep->dip, &intr_types))
57096512Ssowmini 	    != DDI_SUCCESS) {
57103859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "<== nxge_add_intrs: "
57116512Ssowmini 		    "ddi_intr_get_supported_types failed: status 0x%08x",
57126512Ssowmini 		    ddi_status));
57133859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
57143859Sml29623 	}
57153859Sml29623 	nxgep->nxge_intr_type.intr_types = intr_types;
57163859Sml29623 
57173859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
57186512Ssowmini 	    "ddi_intr_get_supported_types: 0x%08x", intr_types));
57193859Sml29623 
57203859Sml29623 	/*
57213859Sml29623 	 * Solaris MSIX is not supported yet. use MSI for now.
57223859Sml29623 	 * nxge_msi_enable (1):
57233859Sml29623 	 *	1 - MSI		2 - MSI-X	others - FIXED
57243859Sml29623 	 */
57253859Sml29623 	switch (nxge_msi_enable) {
57263859Sml29623 	default:
57273859Sml29623 		type = DDI_INTR_TYPE_FIXED;
57283859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs: "
57296512Ssowmini 		    "use fixed (intx emulation) type %08x",
57306512Ssowmini 		    type));
57313859Sml29623 		break;
57323859Sml29623 
57333859Sml29623 	case 2:
57343859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs: "
57356512Ssowmini 		    "ddi_intr_get_supported_types: 0x%08x", intr_types));
57363859Sml29623 		if (intr_types & DDI_INTR_TYPE_MSIX) {
57373859Sml29623 			type = DDI_INTR_TYPE_MSIX;
57383859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
57396512Ssowmini 			    "ddi_intr_get_supported_types: MSIX 0x%08x",
57406512Ssowmini 			    type));
57413859Sml29623 		} else if (intr_types & DDI_INTR_TYPE_MSI) {
57423859Sml29623 			type = DDI_INTR_TYPE_MSI;
57433859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
57446512Ssowmini 			    "ddi_intr_get_supported_types: MSI 0x%08x",
57456512Ssowmini 			    type));
57463859Sml29623 		} else if (intr_types & DDI_INTR_TYPE_FIXED) {
57473859Sml29623 			type = DDI_INTR_TYPE_FIXED;
57483859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs: "
57496512Ssowmini 			    "ddi_intr_get_supported_types: MSXED0x%08x",
57506512Ssowmini 			    type));
57513859Sml29623 		}
57523859Sml29623 		break;
57533859Sml29623 
57543859Sml29623 	case 1:
57553859Sml29623 		if (intr_types & DDI_INTR_TYPE_MSI) {
57563859Sml29623 			type = DDI_INTR_TYPE_MSI;
57573859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs: "
57586512Ssowmini 			    "ddi_intr_get_supported_types: MSI 0x%08x",
57596512Ssowmini 			    type));
57603859Sml29623 		} else if (intr_types & DDI_INTR_TYPE_MSIX) {
57613859Sml29623 			type = DDI_INTR_TYPE_MSIX;
57623859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
57636512Ssowmini 			    "ddi_intr_get_supported_types: MSIX 0x%08x",
57646512Ssowmini 			    type));
57653859Sml29623 		} else if (intr_types & DDI_INTR_TYPE_FIXED) {
57663859Sml29623 			type = DDI_INTR_TYPE_FIXED;
57673859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
57686512Ssowmini 			    "ddi_intr_get_supported_types: MSXED0x%08x",
57696512Ssowmini 			    type));
57703859Sml29623 		}
57713859Sml29623 	}
57723859Sml29623 
57733859Sml29623 	nxgep->nxge_intr_type.intr_type = type;
57743859Sml29623 	if ((type == DDI_INTR_TYPE_MSIX || type == DDI_INTR_TYPE_MSI ||
57756512Ssowmini 	    type == DDI_INTR_TYPE_FIXED) &&
57766512Ssowmini 	    nxgep->nxge_intr_type.niu_msi_enable) {
57773859Sml29623 		if ((status = nxge_add_intrs_adv(nxgep)) != DDI_SUCCESS) {
57783859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
57796512Ssowmini 			    " nxge_add_intrs: "
57806512Ssowmini 			    " nxge_add_intrs_adv failed: status 0x%08x",
57816512Ssowmini 			    status));
57823859Sml29623 			return (status);
57833859Sml29623 		} else {
57843859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs: "
57856512Ssowmini 			    "interrupts registered : type %d", type));
57863859Sml29623 			nxgep->nxge_intr_type.intr_registered = B_TRUE;
57873859Sml29623 
57883859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
57896512Ssowmini 			    "\nAdded advanced nxge add_intr_adv "
57906512Ssowmini 			    "intr type 0x%x\n", type));
57913859Sml29623 
57923859Sml29623 			return (status);
57933859Sml29623 		}
57943859Sml29623 	}
57953859Sml29623 
57963859Sml29623 	if (!nxgep->nxge_intr_type.intr_registered) {
57973859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "==> nxge_add_intrs: "
57986512Ssowmini 		    "failed to register interrupts"));
57993859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
58003859Sml29623 	}
58013859Sml29623 
58023859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_add_intrs"));
58033859Sml29623 	return (status);
58043859Sml29623 }
58053859Sml29623 
58063859Sml29623 static nxge_status_t
58073859Sml29623 nxge_add_intrs_adv(p_nxge_t nxgep)
58083859Sml29623 {
58093859Sml29623 	int		intr_type;
58103859Sml29623 	p_nxge_intr_t	intrp;
58113859Sml29623 
58123859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs_adv"));
58133859Sml29623 
58143859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
58153859Sml29623 	intr_type = intrp->intr_type;
58163859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_add_intrs_adv: type 0x%x",
58176512Ssowmini 	    intr_type));
58183859Sml29623 
58193859Sml29623 	switch (intr_type) {
58203859Sml29623 	case DDI_INTR_TYPE_MSI: /* 0x2 */
58213859Sml29623 	case DDI_INTR_TYPE_MSIX: /* 0x4 */
58223859Sml29623 		return (nxge_add_intrs_adv_type(nxgep, intr_type));
58233859Sml29623 
58243859Sml29623 	case DDI_INTR_TYPE_FIXED: /* 0x1 */
58253859Sml29623 		return (nxge_add_intrs_adv_type_fix(nxgep, intr_type));
58263859Sml29623 
58273859Sml29623 	default:
58283859Sml29623 		return (NXGE_ERROR);
58293859Sml29623 	}
58303859Sml29623 }
58313859Sml29623 
58323859Sml29623 
58333859Sml29623 /*ARGSUSED*/
58343859Sml29623 static nxge_status_t
58353859Sml29623 nxge_add_intrs_adv_type(p_nxge_t nxgep, uint32_t int_type)
58363859Sml29623 {
58373859Sml29623 	dev_info_t		*dip = nxgep->dip;
58383859Sml29623 	p_nxge_ldg_t		ldgp;
58393859Sml29623 	p_nxge_intr_t		intrp;
58403859Sml29623 	uint_t			*inthandler;
58413859Sml29623 	void			*arg1, *arg2;
58423859Sml29623 	int			behavior;
58435013Sml29623 	int			nintrs, navail, nrequest;
58443859Sml29623 	int			nactual, nrequired;
58453859Sml29623 	int			inum = 0;
58463859Sml29623 	int			x, y;
58473859Sml29623 	int			ddi_status = DDI_SUCCESS;
58483859Sml29623 	nxge_status_t		status = NXGE_OK;
58493859Sml29623 
58503859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs_adv_type"));
58513859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
58523859Sml29623 	intrp->start_inum = 0;
58533859Sml29623 
58543859Sml29623 	ddi_status = ddi_intr_get_nintrs(dip, int_type, &nintrs);
58553859Sml29623 	if ((ddi_status != DDI_SUCCESS) || (nintrs == 0)) {
58563859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
58576512Ssowmini 		    "ddi_intr_get_nintrs() failed, status: 0x%x%, "
58586512Ssowmini 		    "nintrs: %d", ddi_status, nintrs));
58593859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
58603859Sml29623 	}
58613859Sml29623 
58623859Sml29623 	ddi_status = ddi_intr_get_navail(dip, int_type, &navail);
58633859Sml29623 	if ((ddi_status != DDI_SUCCESS) || (navail == 0)) {
58643859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
58656512Ssowmini 		    "ddi_intr_get_navail() failed, status: 0x%x%, "
58666512Ssowmini 		    "nintrs: %d", ddi_status, navail));
58673859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
58683859Sml29623 	}
58693859Sml29623 
58703859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL,
58716512Ssowmini 	    "ddi_intr_get_navail() returned: nintrs %d, navail %d",
58726512Ssowmini 	    nintrs, navail));
58733859Sml29623 
58745013Sml29623 	/* PSARC/2007/453 MSI-X interrupt limit override */
58755013Sml29623 	if (int_type == DDI_INTR_TYPE_MSIX) {
58765013Sml29623 		nrequest = nxge_create_msi_property(nxgep);
58775013Sml29623 		if (nrequest < navail) {
58785013Sml29623 			navail = nrequest;
58795013Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
58805013Sml29623 			    "nxge_add_intrs_adv_type: nintrs %d "
58815013Sml29623 			    "navail %d (nrequest %d)",
58825013Sml29623 			    nintrs, navail, nrequest));
58835013Sml29623 		}
58845013Sml29623 	}
58855013Sml29623 
58863859Sml29623 	if (int_type == DDI_INTR_TYPE_MSI && !ISP2(navail)) {
58873859Sml29623 		/* MSI must be power of 2 */
58883859Sml29623 		if ((navail & 16) == 16) {
58893859Sml29623 			navail = 16;
58903859Sml29623 		} else if ((navail & 8) == 8) {
58913859Sml29623 			navail = 8;
58923859Sml29623 		} else if ((navail & 4) == 4) {
58933859Sml29623 			navail = 4;
58943859Sml29623 		} else if ((navail & 2) == 2) {
58953859Sml29623 			navail = 2;
58963859Sml29623 		} else {
58973859Sml29623 			navail = 1;
58983859Sml29623 		}
58993859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
59006512Ssowmini 		    "ddi_intr_get_navail(): (msi power of 2) nintrs %d, "
59016512Ssowmini 		    "navail %d", nintrs, navail));
59023859Sml29623 	}
59033859Sml29623 
59043859Sml29623 	behavior = ((int_type == DDI_INTR_TYPE_FIXED) ? DDI_INTR_ALLOC_STRICT :
59056512Ssowmini 	    DDI_INTR_ALLOC_NORMAL);
59063859Sml29623 	intrp->intr_size = navail * sizeof (ddi_intr_handle_t);
59073859Sml29623 	intrp->htable = kmem_alloc(intrp->intr_size, KM_SLEEP);
59083859Sml29623 	ddi_status = ddi_intr_alloc(dip, intrp->htable, int_type, inum,
59096512Ssowmini 	    navail, &nactual, behavior);
59103859Sml29623 	if (ddi_status != DDI_SUCCESS || nactual == 0) {
59113859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
59126512Ssowmini 		    " ddi_intr_alloc() failed: %d",
59136512Ssowmini 		    ddi_status));
59143859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
59153859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
59163859Sml29623 	}
59173859Sml29623 
59183859Sml29623 	if ((ddi_status = ddi_intr_get_pri(intrp->htable[0],
59196512Ssowmini 	    (uint_t *)&intrp->pri)) != DDI_SUCCESS) {
59203859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
59216512Ssowmini 		    " ddi_intr_get_pri() failed: %d",
59226512Ssowmini 		    ddi_status));
59233859Sml29623 		/* Free already allocated interrupts */
59243859Sml29623 		for (y = 0; y < nactual; y++) {
59253859Sml29623 			(void) ddi_intr_free(intrp->htable[y]);
59263859Sml29623 		}
59273859Sml29623 
59283859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
59293859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
59303859Sml29623 	}
59313859Sml29623 
59323859Sml29623 	nrequired = 0;
59333859Sml29623 	switch (nxgep->niu_type) {
59343859Sml29623 	default:
59353859Sml29623 		status = nxge_ldgv_init(nxgep, &nactual, &nrequired);
59363859Sml29623 		break;
59373859Sml29623 
59383859Sml29623 	case N2_NIU:
59393859Sml29623 		status = nxge_ldgv_init_n2(nxgep, &nactual, &nrequired);
59403859Sml29623 		break;
59413859Sml29623 	}
59423859Sml29623 
59433859Sml29623 	if (status != NXGE_OK) {
59443859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
59456512Ssowmini 		    "nxge_add_intrs_adv_typ:nxge_ldgv_init "
59466512Ssowmini 		    "failed: 0x%x", status));
59473859Sml29623 		/* Free already allocated interrupts */
59483859Sml29623 		for (y = 0; y < nactual; y++) {
59493859Sml29623 			(void) ddi_intr_free(intrp->htable[y]);
59503859Sml29623 		}
59513859Sml29623 
59523859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
59533859Sml29623 		return (status);
59543859Sml29623 	}
59553859Sml29623 
59563859Sml29623 	ldgp = nxgep->ldgvp->ldgp;
59573859Sml29623 	for (x = 0; x < nrequired; x++, ldgp++) {
59583859Sml29623 		ldgp->vector = (uint8_t)x;
59593859Sml29623 		ldgp->intdata = SID_DATA(ldgp->func, x);
59603859Sml29623 		arg1 = ldgp->ldvp;
59613859Sml29623 		arg2 = nxgep;
59623859Sml29623 		if (ldgp->nldvs == 1) {
59633859Sml29623 			inthandler = (uint_t *)ldgp->ldvp->ldv_intr_handler;
59643859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
59656512Ssowmini 			    "nxge_add_intrs_adv_type: "
59666512Ssowmini 			    "arg1 0x%x arg2 0x%x: "
59676512Ssowmini 			    "1-1 int handler (entry %d intdata 0x%x)\n",
59686512Ssowmini 			    arg1, arg2,
59696512Ssowmini 			    x, ldgp->intdata));
59703859Sml29623 		} else if (ldgp->nldvs > 1) {
59713859Sml29623 			inthandler = (uint_t *)ldgp->sys_intr_handler;
59723859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
59736512Ssowmini 			    "nxge_add_intrs_adv_type: "
59746512Ssowmini 			    "arg1 0x%x arg2 0x%x: "
59756512Ssowmini 			    "nldevs %d int handler "
59766512Ssowmini 			    "(entry %d intdata 0x%x)\n",
59776512Ssowmini 			    arg1, arg2,
59786512Ssowmini 			    ldgp->nldvs, x, ldgp->intdata));
59793859Sml29623 		}
59803859Sml29623 
59813859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
59826512Ssowmini 		    "==> nxge_add_intrs_adv_type: ddi_add_intr(inum) #%d "
59836512Ssowmini 		    "htable 0x%llx", x, intrp->htable[x]));
59843859Sml29623 
59853859Sml29623 		if ((ddi_status = ddi_intr_add_handler(intrp->htable[x],
59866512Ssowmini 		    (ddi_intr_handler_t *)inthandler, arg1, arg2))
59876512Ssowmini 		    != DDI_SUCCESS) {
59883859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
59896512Ssowmini 			    "==> nxge_add_intrs_adv_type: failed #%d "
59906512Ssowmini 			    "status 0x%x", x, ddi_status));
59913859Sml29623 			for (y = 0; y < intrp->intr_added; y++) {
59923859Sml29623 				(void) ddi_intr_remove_handler(
59936512Ssowmini 				    intrp->htable[y]);
59943859Sml29623 			}
59953859Sml29623 			/* Free already allocated intr */
59963859Sml29623 			for (y = 0; y < nactual; y++) {
59973859Sml29623 				(void) ddi_intr_free(intrp->htable[y]);
59983859Sml29623 			}
59993859Sml29623 			kmem_free(intrp->htable, intrp->intr_size);
60003859Sml29623 
60013859Sml29623 			(void) nxge_ldgv_uninit(nxgep);
60023859Sml29623 
60033859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
60043859Sml29623 		}
60053859Sml29623 		intrp->intr_added++;
60063859Sml29623 	}
60073859Sml29623 
60083859Sml29623 	intrp->msi_intx_cnt = nactual;
60093859Sml29623 
60103859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
60116512Ssowmini 	    "Requested: %d, Allowed: %d msi_intx_cnt %d intr_added %d",
60126512Ssowmini 	    navail, nactual,
60136512Ssowmini 	    intrp->msi_intx_cnt,
60146512Ssowmini 	    intrp->intr_added));
60153859Sml29623 
60163859Sml29623 	(void) ddi_intr_get_cap(intrp->htable[0], &intrp->intr_cap);
60173859Sml29623 
60183859Sml29623 	(void) nxge_intr_ldgv_init(nxgep);
60193859Sml29623 
60203859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_add_intrs_adv_type"));
60213859Sml29623 
60223859Sml29623 	return (status);
60233859Sml29623 }
60243859Sml29623 
60253859Sml29623 /*ARGSUSED*/
60263859Sml29623 static nxge_status_t
60273859Sml29623 nxge_add_intrs_adv_type_fix(p_nxge_t nxgep, uint32_t int_type)
60283859Sml29623 {
60293859Sml29623 	dev_info_t		*dip = nxgep->dip;
60303859Sml29623 	p_nxge_ldg_t		ldgp;
60313859Sml29623 	p_nxge_intr_t		intrp;
60323859Sml29623 	uint_t			*inthandler;
60333859Sml29623 	void			*arg1, *arg2;
60343859Sml29623 	int			behavior;
60353859Sml29623 	int			nintrs, navail;
60363859Sml29623 	int			nactual, nrequired;
60373859Sml29623 	int			inum = 0;
60383859Sml29623 	int			x, y;
60393859Sml29623 	int			ddi_status = DDI_SUCCESS;
60403859Sml29623 	nxge_status_t		status = NXGE_OK;
60413859Sml29623 
60423859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_add_intrs_adv_type_fix"));
60433859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
60443859Sml29623 	intrp->start_inum = 0;
60453859Sml29623 
60463859Sml29623 	ddi_status = ddi_intr_get_nintrs(dip, int_type, &nintrs);
60473859Sml29623 	if ((ddi_status != DDI_SUCCESS) || (nintrs == 0)) {
60483859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
60496512Ssowmini 		    "ddi_intr_get_nintrs() failed, status: 0x%x%, "
60506512Ssowmini 		    "nintrs: %d", status, nintrs));
60513859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
60523859Sml29623 	}
60533859Sml29623 
60543859Sml29623 	ddi_status = ddi_intr_get_navail(dip, int_type, &navail);
60553859Sml29623 	if ((ddi_status != DDI_SUCCESS) || (navail == 0)) {
60563859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
60576512Ssowmini 		    "ddi_intr_get_navail() failed, status: 0x%x%, "
60586512Ssowmini 		    "nintrs: %d", ddi_status, navail));
60593859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
60603859Sml29623 	}
60613859Sml29623 
60623859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL,
60636512Ssowmini 	    "ddi_intr_get_navail() returned: nintrs %d, naavail %d",
60646512Ssowmini 	    nintrs, navail));
60653859Sml29623 
60663859Sml29623 	behavior = ((int_type == DDI_INTR_TYPE_FIXED) ? DDI_INTR_ALLOC_STRICT :
60676512Ssowmini 	    DDI_INTR_ALLOC_NORMAL);
60683859Sml29623 	intrp->intr_size = navail * sizeof (ddi_intr_handle_t);
60693859Sml29623 	intrp->htable = kmem_alloc(intrp->intr_size, KM_SLEEP);
60703859Sml29623 	ddi_status = ddi_intr_alloc(dip, intrp->htable, int_type, inum,
60716512Ssowmini 	    navail, &nactual, behavior);
60723859Sml29623 	if (ddi_status != DDI_SUCCESS || nactual == 0) {
60733859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
60746512Ssowmini 		    " ddi_intr_alloc() failed: %d",
60756512Ssowmini 		    ddi_status));
60763859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
60773859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
60783859Sml29623 	}
60793859Sml29623 
60803859Sml29623 	if ((ddi_status = ddi_intr_get_pri(intrp->htable[0],
60816512Ssowmini 	    (uint_t *)&intrp->pri)) != DDI_SUCCESS) {
60823859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
60836512Ssowmini 		    " ddi_intr_get_pri() failed: %d",
60846512Ssowmini 		    ddi_status));
60853859Sml29623 		/* Free already allocated interrupts */
60863859Sml29623 		for (y = 0; y < nactual; y++) {
60873859Sml29623 			(void) ddi_intr_free(intrp->htable[y]);
60883859Sml29623 		}
60893859Sml29623 
60903859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
60913859Sml29623 		return (NXGE_ERROR | NXGE_DDI_FAILED);
60923859Sml29623 	}
60933859Sml29623 
60943859Sml29623 	nrequired = 0;
60953859Sml29623 	switch (nxgep->niu_type) {
60963859Sml29623 	default:
60973859Sml29623 		status = nxge_ldgv_init(nxgep, &nactual, &nrequired);
60983859Sml29623 		break;
60993859Sml29623 
61003859Sml29623 	case N2_NIU:
61013859Sml29623 		status = nxge_ldgv_init_n2(nxgep, &nactual, &nrequired);
61023859Sml29623 		break;
61033859Sml29623 	}
61043859Sml29623 
61053859Sml29623 	if (status != NXGE_OK) {
61063859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
61076512Ssowmini 		    "nxge_add_intrs_adv_type_fix:nxge_ldgv_init "
61086512Ssowmini 		    "failed: 0x%x", status));
61093859Sml29623 		/* Free already allocated interrupts */
61103859Sml29623 		for (y = 0; y < nactual; y++) {
61113859Sml29623 			(void) ddi_intr_free(intrp->htable[y]);
61123859Sml29623 		}
61133859Sml29623 
61143859Sml29623 		kmem_free(intrp->htable, intrp->intr_size);
61153859Sml29623 		return (status);
61163859Sml29623 	}
61173859Sml29623 
61183859Sml29623 	ldgp = nxgep->ldgvp->ldgp;
61193859Sml29623 	for (x = 0; x < nrequired; x++, ldgp++) {
61203859Sml29623 		ldgp->vector = (uint8_t)x;
61213859Sml29623 		if (nxgep->niu_type != N2_NIU) {
61223859Sml29623 			ldgp->intdata = SID_DATA(ldgp->func, x);
61233859Sml29623 		}
61243859Sml29623 
61253859Sml29623 		arg1 = ldgp->ldvp;
61263859Sml29623 		arg2 = nxgep;
61273859Sml29623 		if (ldgp->nldvs == 1) {
61283859Sml29623 			inthandler = (uint_t *)ldgp->ldvp->ldv_intr_handler;
61293859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
61306512Ssowmini 			    "nxge_add_intrs_adv_type_fix: "
61316512Ssowmini 			    "1-1 int handler(%d) ldg %d ldv %d "
61326512Ssowmini 			    "arg1 $%p arg2 $%p\n",
61336512Ssowmini 			    x, ldgp->ldg, ldgp->ldvp->ldv,
61346512Ssowmini 			    arg1, arg2));
61353859Sml29623 		} else if (ldgp->nldvs > 1) {
61363859Sml29623 			inthandler = (uint_t *)ldgp->sys_intr_handler;
61373859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL,
61386512Ssowmini 			    "nxge_add_intrs_adv_type_fix: "
61396512Ssowmini 			    "shared ldv %d int handler(%d) ldv %d ldg %d"
61406512Ssowmini 			    "arg1 0x%016llx arg2 0x%016llx\n",
61416512Ssowmini 			    x, ldgp->nldvs, ldgp->ldg, ldgp->ldvp->ldv,
61426512Ssowmini 			    arg1, arg2));
61433859Sml29623 		}
61443859Sml29623 
61453859Sml29623 		if ((ddi_status = ddi_intr_add_handler(intrp->htable[x],
61466512Ssowmini 		    (ddi_intr_handler_t *)inthandler, arg1, arg2))
61476512Ssowmini 		    != DDI_SUCCESS) {
61483859Sml29623 			NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
61496512Ssowmini 			    "==> nxge_add_intrs_adv_type_fix: failed #%d "
61506512Ssowmini 			    "status 0x%x", x, ddi_status));
61513859Sml29623 			for (y = 0; y < intrp->intr_added; y++) {
61523859Sml29623 				(void) ddi_intr_remove_handler(
61536512Ssowmini 				    intrp->htable[y]);
61543859Sml29623 			}
61553859Sml29623 			for (y = 0; y < nactual; y++) {
61563859Sml29623 				(void) ddi_intr_free(intrp->htable[y]);
61573859Sml29623 			}
61583859Sml29623 			/* Free already allocated intr */
61593859Sml29623 			kmem_free(intrp->htable, intrp->intr_size);
61603859Sml29623 
61613859Sml29623 			(void) nxge_ldgv_uninit(nxgep);
61623859Sml29623 
61633859Sml29623 			return (NXGE_ERROR | NXGE_DDI_FAILED);
61643859Sml29623 		}
61653859Sml29623 		intrp->intr_added++;
61663859Sml29623 	}
61673859Sml29623 
61683859Sml29623 	intrp->msi_intx_cnt = nactual;
61693859Sml29623 
61703859Sml29623 	(void) ddi_intr_get_cap(intrp->htable[0], &intrp->intr_cap);
61713859Sml29623 
61723859Sml29623 	status = nxge_intr_ldgv_init(nxgep);
61733859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_add_intrs_adv_type_fix"));
61743859Sml29623 
61753859Sml29623 	return (status);
61763859Sml29623 }
61773859Sml29623 
61783859Sml29623 static void
61793859Sml29623 nxge_remove_intrs(p_nxge_t nxgep)
61803859Sml29623 {
61813859Sml29623 	int		i, inum;
61823859Sml29623 	p_nxge_intr_t	intrp;
61833859Sml29623 
61843859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_remove_intrs"));
61853859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
61863859Sml29623 	if (!intrp->intr_registered) {
61873859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
61886512Ssowmini 		    "<== nxge_remove_intrs: interrupts not registered"));
61893859Sml29623 		return;
61903859Sml29623 	}
61913859Sml29623 
61923859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_remove_intrs:advanced"));
61933859Sml29623 
61943859Sml29623 	if (intrp->intr_cap & DDI_INTR_FLAG_BLOCK) {
61953859Sml29623 		(void) ddi_intr_block_disable(intrp->htable,
61966512Ssowmini 		    intrp->intr_added);
61973859Sml29623 	} else {
61983859Sml29623 		for (i = 0; i < intrp->intr_added; i++) {
61993859Sml29623 			(void) ddi_intr_disable(intrp->htable[i]);
62003859Sml29623 		}
62013859Sml29623 	}
62023859Sml29623 
62033859Sml29623 	for (inum = 0; inum < intrp->intr_added; inum++) {
62043859Sml29623 		if (intrp->htable[inum]) {
62053859Sml29623 			(void) ddi_intr_remove_handler(intrp->htable[inum]);
62063859Sml29623 		}
62073859Sml29623 	}
62083859Sml29623 
62093859Sml29623 	for (inum = 0; inum < intrp->msi_intx_cnt; inum++) {
62103859Sml29623 		if (intrp->htable[inum]) {
62113859Sml29623 			NXGE_DEBUG_MSG((nxgep, DDI_CTL,
62126512Ssowmini 			    "nxge_remove_intrs: ddi_intr_free inum %d "
62136512Ssowmini 			    "msi_intx_cnt %d intr_added %d",
62146512Ssowmini 			    inum,
62156512Ssowmini 			    intrp->msi_intx_cnt,
62166512Ssowmini 			    intrp->intr_added));
62173859Sml29623 
62183859Sml29623 			(void) ddi_intr_free(intrp->htable[inum]);
62193859Sml29623 		}
62203859Sml29623 	}
62213859Sml29623 
62223859Sml29623 	kmem_free(intrp->htable, intrp->intr_size);
62233859Sml29623 	intrp->intr_registered = B_FALSE;
62243859Sml29623 	intrp->intr_enabled = B_FALSE;
62253859Sml29623 	intrp->msi_intx_cnt = 0;
62263859Sml29623 	intrp->intr_added = 0;
62273859Sml29623 
62283859Sml29623 	(void) nxge_ldgv_uninit(nxgep);
62293859Sml29623 
62305013Sml29623 	(void) ddi_prop_remove(DDI_DEV_T_NONE, nxgep->dip,
62315013Sml29623 	    "#msix-request");
62325013Sml29623 
62333859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_remove_intrs"));
62343859Sml29623 }
62353859Sml29623 
62363859Sml29623 /*ARGSUSED*/
62373859Sml29623 static void
62383859Sml29623 nxge_intrs_enable(p_nxge_t nxgep)
62393859Sml29623 {
62403859Sml29623 	p_nxge_intr_t	intrp;
62413859Sml29623 	int		i;
62423859Sml29623 	int		status;
62433859Sml29623 
62443859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_intrs_enable"));
62453859Sml29623 
62463859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
62473859Sml29623 
62483859Sml29623 	if (!intrp->intr_registered) {
62493859Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL, "<== nxge_intrs_enable: "
62506512Ssowmini 		    "interrupts are not registered"));
62513859Sml29623 		return;
62523859Sml29623 	}
62533859Sml29623 
62543859Sml29623 	if (intrp->intr_enabled) {
62553859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL,
62566512Ssowmini 		    "<== nxge_intrs_enable: already enabled"));
62573859Sml29623 		return;
62583859Sml29623 	}
62593859Sml29623 
62603859Sml29623 	if (intrp->intr_cap & DDI_INTR_FLAG_BLOCK) {
62613859Sml29623 		status = ddi_intr_block_enable(intrp->htable,
62626512Ssowmini 		    intrp->intr_added);
62633859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_intrs_enable "
62646512Ssowmini 		    "block enable - status 0x%x total inums #%d\n",
62656512Ssowmini 		    status, intrp->intr_added));
62663859Sml29623 	} else {
62673859Sml29623 		for (i = 0; i < intrp->intr_added; i++) {
62683859Sml29623 			status = ddi_intr_enable(intrp->htable[i]);
62693859Sml29623 			NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_intrs_enable "
62706512Ssowmini 			    "ddi_intr_enable:enable - status 0x%x "
62716512Ssowmini 			    "total inums %d enable inum #%d\n",
62726512Ssowmini 			    status, intrp->intr_added, i));
62733859Sml29623 			if (status == DDI_SUCCESS) {
62743859Sml29623 				intrp->intr_enabled = B_TRUE;
62753859Sml29623 			}
62763859Sml29623 		}
62773859Sml29623 	}
62783859Sml29623 
62793859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_intrs_enable"));
62803859Sml29623 }
62813859Sml29623 
62823859Sml29623 /*ARGSUSED*/
62833859Sml29623 static void
62843859Sml29623 nxge_intrs_disable(p_nxge_t nxgep)
62853859Sml29623 {
62863859Sml29623 	p_nxge_intr_t	intrp;
62873859Sml29623 	int		i;
62883859Sml29623 
62893859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "==> nxge_intrs_disable"));
62903859Sml29623 
62913859Sml29623 	intrp = (p_nxge_intr_t)&nxgep->nxge_intr_type;
62923859Sml29623 
62933859Sml29623 	if (!intrp->intr_registered) {
62943859Sml29623 		NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_intrs_disable: "
62956512Ssowmini 		    "interrupts are not registered"));
62963859Sml29623 		return;
62973859Sml29623 	}
62983859Sml29623 
62993859Sml29623 	if (intrp->intr_cap & DDI_INTR_FLAG_BLOCK) {
63003859Sml29623 		(void) ddi_intr_block_disable(intrp->htable,
63016512Ssowmini 		    intrp->intr_added);
63023859Sml29623 	} else {
63033859Sml29623 		for (i = 0; i < intrp->intr_added; i++) {
63043859Sml29623 			(void) ddi_intr_disable(intrp->htable[i]);
63053859Sml29623 		}
63063859Sml29623 	}
63073859Sml29623 
63083859Sml29623 	intrp->intr_enabled = B_FALSE;
63093859Sml29623 	NXGE_DEBUG_MSG((nxgep, INT_CTL, "<== nxge_intrs_disable"));
63103859Sml29623 }
63113859Sml29623 
63123859Sml29623 static nxge_status_t
63133859Sml29623 nxge_mac_register(p_nxge_t nxgep)
63143859Sml29623 {
63153859Sml29623 	mac_register_t *macp;
63163859Sml29623 	int		status;
63173859Sml29623 
63183859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_mac_register"));
63193859Sml29623 
63203859Sml29623 	if ((macp = mac_alloc(MAC_VERSION)) == NULL)
63213859Sml29623 		return (NXGE_ERROR);
63223859Sml29623 
63233859Sml29623 	macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
63243859Sml29623 	macp->m_driver = nxgep;
63253859Sml29623 	macp->m_dip = nxgep->dip;
63263859Sml29623 	macp->m_src_addr = nxgep->ouraddr.ether_addr_octet;
63273859Sml29623 	macp->m_callbacks = &nxge_m_callbacks;
63283859Sml29623 	macp->m_min_sdu = 0;
63296439Sml29623 	nxgep->mac.default_mtu = nxgep->mac.maxframesize -
63306439Sml29623 	    NXGE_EHEADER_VLAN_CRC;
63316439Sml29623 	macp->m_max_sdu = nxgep->mac.default_mtu;
63325895Syz147064 	macp->m_margin = VLAN_TAGSZ;
63336512Ssowmini 	macp->m_priv_props = nxge_priv_props;
63346512Ssowmini 	macp->m_priv_prop_count = NXGE_MAX_PRIV_PROPS;
63358275SEric Cheng 	macp->m_v12n = MAC_VIRT_HIO | MAC_VIRT_LEVEL1 | MAC_VIRT_SERIALIZE;
63363859Sml29623 
63376439Sml29623 	NXGE_DEBUG_MSG((nxgep, MAC_CTL,
63386439Sml29623 	    "==> nxge_mac_register: instance %d "
63396439Sml29623 	    "max_sdu %d margin %d maxframe %d (header %d)",
63406439Sml29623 	    nxgep->instance,
63416439Sml29623 	    macp->m_max_sdu, macp->m_margin,
63426439Sml29623 	    nxgep->mac.maxframesize,
63436439Sml29623 	    NXGE_EHEADER_VLAN_CRC));
63446439Sml29623 
63453859Sml29623 	status = mac_register(macp, &nxgep->mach);
63463859Sml29623 	mac_free(macp);
63473859Sml29623 
63483859Sml29623 	if (status != 0) {
63493859Sml29623 		cmn_err(CE_WARN,
63506512Ssowmini 		    "!nxge_mac_register failed (status %d instance %d)",
63516512Ssowmini 		    status, nxgep->instance);
63523859Sml29623 		return (NXGE_ERROR);
63533859Sml29623 	}
63543859Sml29623 
63553859Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_mac_register success "
63566512Ssowmini 	    "(instance %d)", nxgep->instance));
63573859Sml29623 
63583859Sml29623 	return (NXGE_OK);
63593859Sml29623 }
63603859Sml29623 
63613859Sml29623 void
63623859Sml29623 nxge_err_inject(p_nxge_t nxgep, queue_t *wq, mblk_t *mp)
63633859Sml29623 {
63643859Sml29623 	ssize_t		size;
63653859Sml29623 	mblk_t		*nmp;
63663859Sml29623 	uint8_t		blk_id;
63673859Sml29623 	uint8_t		chan;
63683859Sml29623 	uint32_t	err_id;
63693859Sml29623 	err_inject_t	*eip;
63703859Sml29623 
63713859Sml29623 	NXGE_DEBUG_MSG((nxgep, STR_CTL, "==> nxge_err_inject"));
63723859Sml29623 
63733859Sml29623 	size = 1024;
63743859Sml29623 	nmp = mp->b_cont;
63753859Sml29623 	eip = (err_inject_t *)nmp->b_rptr;
63763859Sml29623 	blk_id = eip->blk_id;
63773859Sml29623 	err_id = eip->err_id;
63783859Sml29623 	chan = eip->chan;
63793859Sml29623 	cmn_err(CE_NOTE, "!blk_id = 0x%x\n", blk_id);
63803859Sml29623 	cmn_err(CE_NOTE, "!err_id = 0x%x\n", err_id);
63813859Sml29623 	cmn_err(CE_NOTE, "!chan = 0x%x\n", chan);
63823859Sml29623 	switch (blk_id) {
63833859Sml29623 	case MAC_BLK_ID:
63843859Sml29623 		break;
63853859Sml29623 	case TXMAC_BLK_ID:
63863859Sml29623 		break;
63873859Sml29623 	case RXMAC_BLK_ID:
63883859Sml29623 		break;
63893859Sml29623 	case MIF_BLK_ID:
63903859Sml29623 		break;
63913859Sml29623 	case IPP_BLK_ID:
63923859Sml29623 		nxge_ipp_inject_err(nxgep, err_id);
63933859Sml29623 		break;
63943859Sml29623 	case TXC_BLK_ID:
63953859Sml29623 		nxge_txc_inject_err(nxgep, err_id);
63963859Sml29623 		break;
63973859Sml29623 	case TXDMA_BLK_ID:
63983859Sml29623 		nxge_txdma_inject_err(nxgep, err_id, chan);
63993859Sml29623 		break;
64003859Sml29623 	case RXDMA_BLK_ID:
64013859Sml29623 		nxge_rxdma_inject_err(nxgep, err_id, chan);
64023859Sml29623 		break;
64033859Sml29623 	case ZCP_BLK_ID:
64043859Sml29623 		nxge_zcp_inject_err(nxgep, err_id);
64053859Sml29623 		break;
64063859Sml29623 	case ESPC_BLK_ID:
64073859Sml29623 		break;
64083859Sml29623 	case FFLP_BLK_ID:
64093859Sml29623 		break;
64103859Sml29623 	case PHY_BLK_ID:
64113859Sml29623 		break;
64123859Sml29623 	case ETHER_SERDES_BLK_ID:
64133859Sml29623 		break;
64143859Sml29623 	case PCIE_SERDES_BLK_ID:
64153859Sml29623 		break;
64163859Sml29623 	case VIR_BLK_ID:
64173859Sml29623 		break;
64183859Sml29623 	}
64193859Sml29623 
64203859Sml29623 	nmp->b_wptr = nmp->b_rptr + size;
64213859Sml29623 	NXGE_DEBUG_MSG((nxgep, STR_CTL, "<== nxge_err_inject"));
64223859Sml29623 
64233859Sml29623 	miocack(wq, mp, (int)size, 0);
64243859Sml29623 }
64253859Sml29623 
64263859Sml29623 static int
64273859Sml29623 nxge_init_common_dev(p_nxge_t nxgep)
64283859Sml29623 {
64293859Sml29623 	p_nxge_hw_list_t	hw_p;
64303859Sml29623 	dev_info_t 		*p_dip;
64313859Sml29623 
64323859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "==> nxge_init_common_device"));
64333859Sml29623 
64343859Sml29623 	p_dip = nxgep->p_dip;
64353859Sml29623 	MUTEX_ENTER(&nxge_common_lock);
64363859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL,
64376512Ssowmini 	    "==> nxge_init_common_dev:func # %d",
64386512Ssowmini 	    nxgep->function_num));
64393859Sml29623 	/*
64403859Sml29623 	 * Loop through existing per neptune hardware list.
64413859Sml29623 	 */
64423859Sml29623 	for (hw_p = nxge_hw_list; hw_p; hw_p = hw_p->next) {
64433859Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
64446512Ssowmini 		    "==> nxge_init_common_device:func # %d "
64456512Ssowmini 		    "hw_p $%p parent dip $%p",
64466512Ssowmini 		    nxgep->function_num,
64476512Ssowmini 		    hw_p,
64486512Ssowmini 		    p_dip));
64493859Sml29623 		if (hw_p->parent_devp == p_dip) {
64503859Sml29623 			nxgep->nxge_hw_p = hw_p;
64513859Sml29623 			hw_p->ndevs++;
64523859Sml29623 			hw_p->nxge_p[nxgep->function_num] = nxgep;
64533859Sml29623 			NXGE_DEBUG_MSG((nxgep, MOD_CTL,
64546512Ssowmini 			    "==> nxge_init_common_device:func # %d "
64556512Ssowmini 			    "hw_p $%p parent dip $%p "
64566512Ssowmini 			    "ndevs %d (found)",
64576512Ssowmini 			    nxgep->function_num,
64586512Ssowmini 			    hw_p,
64596512Ssowmini 			    p_dip,
64606512Ssowmini 			    hw_p->ndevs));
64613859Sml29623 			break;
64623859Sml29623 		}
64633859Sml29623 	}
64643859Sml29623 
64653859Sml29623 	if (hw_p == NULL) {
64667801SSantwona.Behera@Sun.COM 
64677801SSantwona.Behera@Sun.COM 		char **prop_val;
64687801SSantwona.Behera@Sun.COM 		uint_t prop_len;
64697801SSantwona.Behera@Sun.COM 		int i;
64707801SSantwona.Behera@Sun.COM 
64713859Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
64726512Ssowmini 		    "==> nxge_init_common_device:func # %d "
64736512Ssowmini 		    "parent dip $%p (new)",
64746512Ssowmini 		    nxgep->function_num,
64756512Ssowmini 		    p_dip));
64763859Sml29623 		hw_p = kmem_zalloc(sizeof (nxge_hw_list_t), KM_SLEEP);
64773859Sml29623 		hw_p->parent_devp = p_dip;
64783859Sml29623 		hw_p->magic = NXGE_NEPTUNE_MAGIC;
64793859Sml29623 		nxgep->nxge_hw_p = hw_p;
64803859Sml29623 		hw_p->ndevs++;
64813859Sml29623 		hw_p->nxge_p[nxgep->function_num] = nxgep;
64823859Sml29623 		hw_p->next = nxge_hw_list;
64834732Sdavemq 		if (nxgep->niu_type == N2_NIU) {
64844732Sdavemq 			hw_p->niu_type = N2_NIU;
64854732Sdavemq 			hw_p->platform_type = P_NEPTUNE_NIU;
64864732Sdavemq 		} else {
64874732Sdavemq 			hw_p->niu_type = NIU_TYPE_NONE;
64884977Sraghus 			hw_p->platform_type = P_NEPTUNE_NONE;
64894732Sdavemq 		}
64903859Sml29623 
64913859Sml29623 		MUTEX_INIT(&hw_p->nxge_cfg_lock, NULL, MUTEX_DRIVER, NULL);
64923859Sml29623 		MUTEX_INIT(&hw_p->nxge_tcam_lock, NULL, MUTEX_DRIVER, NULL);
64933859Sml29623 		MUTEX_INIT(&hw_p->nxge_vlan_lock, NULL, MUTEX_DRIVER, NULL);
64943859Sml29623 		MUTEX_INIT(&hw_p->nxge_mdio_lock, NULL, MUTEX_DRIVER, NULL);
64953859Sml29623 
64963859Sml29623 		nxge_hw_list = hw_p;
64974732Sdavemq 
64987801SSantwona.Behera@Sun.COM 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, nxgep->dip, 0,
64997801SSantwona.Behera@Sun.COM 		    "compatible", &prop_val, &prop_len) == DDI_PROP_SUCCESS) {
65007801SSantwona.Behera@Sun.COM 			for (i = 0; i < prop_len; i++) {
65017801SSantwona.Behera@Sun.COM 				if ((strcmp((caddr_t)prop_val[i],
65027801SSantwona.Behera@Sun.COM 				    NXGE_ROCK_COMPATIBLE) == 0)) {
65037801SSantwona.Behera@Sun.COM 					hw_p->platform_type = P_NEPTUNE_ROCK;
65047801SSantwona.Behera@Sun.COM 					NXGE_DEBUG_MSG((nxgep, MOD_CTL,
65057801SSantwona.Behera@Sun.COM 					    "ROCK hw_p->platform_type %d",
65067801SSantwona.Behera@Sun.COM 					    hw_p->platform_type));
65077801SSantwona.Behera@Sun.COM 					break;
65087801SSantwona.Behera@Sun.COM 				}
65097801SSantwona.Behera@Sun.COM 				NXGE_DEBUG_MSG((nxgep, MOD_CTL,
65107801SSantwona.Behera@Sun.COM 				    "nxge_init_common_dev: read compatible"
65117801SSantwona.Behera@Sun.COM 				    " property[%d] val[%s]",
65127801SSantwona.Behera@Sun.COM 				    i, (caddr_t)prop_val[i]));
65137801SSantwona.Behera@Sun.COM 			}
65147801SSantwona.Behera@Sun.COM 		}
65157801SSantwona.Behera@Sun.COM 
65167801SSantwona.Behera@Sun.COM 		ddi_prop_free(prop_val);
65177801SSantwona.Behera@Sun.COM 
65184732Sdavemq 		(void) nxge_scan_ports_phy(nxgep, nxge_hw_list);
65193859Sml29623 	}
65203859Sml29623 
65213859Sml29623 	MUTEX_EXIT(&nxge_common_lock);
65224732Sdavemq 
65234977Sraghus 	nxgep->platform_type = hw_p->platform_type;
65247801SSantwona.Behera@Sun.COM 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "nxgep->platform_type %d",
65257801SSantwona.Behera@Sun.COM 	    nxgep->platform_type));
65264732Sdavemq 	if (nxgep->niu_type != N2_NIU) {
65274732Sdavemq 		nxgep->niu_type = hw_p->niu_type;
65284732Sdavemq 	}
65294732Sdavemq 
65303859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL,
65316512Ssowmini 	    "==> nxge_init_common_device (nxge_hw_list) $%p",
65326512Ssowmini 	    nxge_hw_list));
65333859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "<== nxge_init_common_device"));
65343859Sml29623 
65353859Sml29623 	return (NXGE_OK);
65363859Sml29623 }
65373859Sml29623 
65383859Sml29623 static void
65393859Sml29623 nxge_uninit_common_dev(p_nxge_t nxgep)
65403859Sml29623 {
65413859Sml29623 	p_nxge_hw_list_t	hw_p, h_hw_p;
65426801Sspeer 	p_nxge_dma_pt_cfg_t	p_dma_cfgp;
65436801Sspeer 	p_nxge_hw_pt_cfg_t	p_cfgp;
65443859Sml29623 	dev_info_t 		*p_dip;
65453859Sml29623 
65463859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "==> nxge_uninit_common_device"));
65473859Sml29623 	if (nxgep->nxge_hw_p == NULL) {
65483859Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
65496512Ssowmini 		    "<== nxge_uninit_common_device (no common)"));
65503859Sml29623 		return;
65513859Sml29623 	}
65523859Sml29623 
65533859Sml29623 	MUTEX_ENTER(&nxge_common_lock);
65543859Sml29623 	h_hw_p = nxge_hw_list;
65553859Sml29623 	for (hw_p = nxge_hw_list; hw_p; hw_p = hw_p->next) {
65563859Sml29623 		p_dip = hw_p->parent_devp;
65573859Sml29623 		if (nxgep->nxge_hw_p == hw_p &&
65586512Ssowmini 		    p_dip == nxgep->p_dip &&
65596512Ssowmini 		    nxgep->nxge_hw_p->magic == NXGE_NEPTUNE_MAGIC &&
65606512Ssowmini 		    hw_p->magic == NXGE_NEPTUNE_MAGIC) {
65613859Sml29623 
65623859Sml29623 			NXGE_DEBUG_MSG((nxgep, MOD_CTL,
65636512Ssowmini 			    "==> nxge_uninit_common_device:func # %d "
65646512Ssowmini 			    "hw_p $%p parent dip $%p "
65656512Ssowmini 			    "ndevs %d (found)",
65666512Ssowmini 			    nxgep->function_num,
65676512Ssowmini 			    hw_p,
65686512Ssowmini 			    p_dip,
65696512Ssowmini 			    hw_p->ndevs));
65703859Sml29623 
65716801Sspeer 			/*
65726801Sspeer 			 * Release the RDC table, a shared resoruce
65736801Sspeer 			 * of the nxge hardware.  The RDC table was
65746801Sspeer 			 * assigned to this instance of nxge in
65756801Sspeer 			 * nxge_use_cfg_dma_config().
65766801Sspeer 			 */
65777587SMichael.Speer@Sun.COM 			if (!isLDOMguest(nxgep)) {
65787587SMichael.Speer@Sun.COM 				p_dma_cfgp =
65797587SMichael.Speer@Sun.COM 				    (p_nxge_dma_pt_cfg_t)&nxgep->pt_config;
65807587SMichael.Speer@Sun.COM 				p_cfgp =
65817587SMichael.Speer@Sun.COM 				    (p_nxge_hw_pt_cfg_t)&p_dma_cfgp->hw_config;
65827587SMichael.Speer@Sun.COM 				(void) nxge_fzc_rdc_tbl_unbind(nxgep,
65837587SMichael.Speer@Sun.COM 				    p_cfgp->def_mac_rxdma_grpid);
65847766SMichael.Speer@Sun.COM 
65857766SMichael.Speer@Sun.COM 				/* Cleanup any outstanding groups.  */
65867766SMichael.Speer@Sun.COM 				nxge_grp_cleanup(nxgep);
65877587SMichael.Speer@Sun.COM 			}
65886801Sspeer 
65893859Sml29623 			if (hw_p->ndevs) {
65903859Sml29623 				hw_p->ndevs--;
65913859Sml29623 			}
65923859Sml29623 			hw_p->nxge_p[nxgep->function_num] = NULL;
65933859Sml29623 			if (!hw_p->ndevs) {
65943859Sml29623 				MUTEX_DESTROY(&hw_p->nxge_vlan_lock);
65953859Sml29623 				MUTEX_DESTROY(&hw_p->nxge_tcam_lock);
65963859Sml29623 				MUTEX_DESTROY(&hw_p->nxge_cfg_lock);
65973859Sml29623 				MUTEX_DESTROY(&hw_p->nxge_mdio_lock);
65983859Sml29623 				NXGE_DEBUG_MSG((nxgep, MOD_CTL,
65996512Ssowmini 				    "==> nxge_uninit_common_device: "
66006512Ssowmini 				    "func # %d "
66016512Ssowmini 				    "hw_p $%p parent dip $%p "
66026512Ssowmini 				    "ndevs %d (last)",
66036512Ssowmini 				    nxgep->function_num,
66046512Ssowmini 				    hw_p,
66056512Ssowmini 				    p_dip,
66066512Ssowmini 				    hw_p->ndevs));
66073859Sml29623 
66086495Sspeer 				nxge_hio_uninit(nxgep);
66096495Sspeer 
66103859Sml29623 				if (hw_p == nxge_hw_list) {
66113859Sml29623 					NXGE_DEBUG_MSG((nxgep, MOD_CTL,
66126512Ssowmini 					    "==> nxge_uninit_common_device:"
66136512Ssowmini 					    "remove head func # %d "
66146512Ssowmini 					    "hw_p $%p parent dip $%p "
66156512Ssowmini 					    "ndevs %d (head)",
66166512Ssowmini 					    nxgep->function_num,
66176512Ssowmini 					    hw_p,
66186512Ssowmini 					    p_dip,
66196512Ssowmini 					    hw_p->ndevs));
66203859Sml29623 					nxge_hw_list = hw_p->next;
66213859Sml29623 				} else {
66223859Sml29623 					NXGE_DEBUG_MSG((nxgep, MOD_CTL,
66236512Ssowmini 					    "==> nxge_uninit_common_device:"
66246512Ssowmini 					    "remove middle func # %d "
66256512Ssowmini 					    "hw_p $%p parent dip $%p "
66266512Ssowmini 					    "ndevs %d (middle)",
66276512Ssowmini 					    nxgep->function_num,
66286512Ssowmini 					    hw_p,
66296512Ssowmini 					    p_dip,
66306512Ssowmini 					    hw_p->ndevs));
66313859Sml29623 					h_hw_p->next = hw_p->next;
66323859Sml29623 				}
66333859Sml29623 
66346495Sspeer 				nxgep->nxge_hw_p = NULL;
66353859Sml29623 				KMEM_FREE(hw_p, sizeof (nxge_hw_list_t));
66363859Sml29623 			}
66373859Sml29623 			break;
66383859Sml29623 		} else {
66393859Sml29623 			h_hw_p = hw_p;
66403859Sml29623 		}
66413859Sml29623 	}
66423859Sml29623 
66433859Sml29623 	MUTEX_EXIT(&nxge_common_lock);
66443859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL,
66456512Ssowmini 	    "==> nxge_uninit_common_device (nxge_hw_list) $%p",
66466512Ssowmini 	    nxge_hw_list));
66473859Sml29623 
66483859Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "<= nxge_uninit_common_device"));
66493859Sml29623 }
66504732Sdavemq 
66514732Sdavemq /*
66524977Sraghus  * Determines the number of ports from the niu_type or the platform type.
66534732Sdavemq  * Returns the number of ports, or returns zero on failure.
66544732Sdavemq  */
66554732Sdavemq 
66564732Sdavemq int
66574977Sraghus nxge_get_nports(p_nxge_t nxgep)
66584732Sdavemq {
66594732Sdavemq 	int	nports = 0;
66604732Sdavemq 
66614977Sraghus 	switch (nxgep->niu_type) {
66624732Sdavemq 	case N2_NIU:
66634732Sdavemq 	case NEPTUNE_2_10GF:
66644732Sdavemq 		nports = 2;
66654732Sdavemq 		break;
66664732Sdavemq 	case NEPTUNE_4_1GC:
66674732Sdavemq 	case NEPTUNE_2_10GF_2_1GC:
66684732Sdavemq 	case NEPTUNE_1_10GF_3_1GC:
66694732Sdavemq 	case NEPTUNE_1_1GC_1_10GF_2_1GC:
66706261Sjoycey 	case NEPTUNE_2_10GF_2_1GRF:
66714732Sdavemq 		nports = 4;
66724732Sdavemq 		break;
66734732Sdavemq 	default:
66744977Sraghus 		switch (nxgep->platform_type) {
66754977Sraghus 		case P_NEPTUNE_NIU:
66764977Sraghus 		case P_NEPTUNE_ATLAS_2PORT:
66774977Sraghus 			nports = 2;
66784977Sraghus 			break;
66794977Sraghus 		case P_NEPTUNE_ATLAS_4PORT:
66804977Sraghus 		case P_NEPTUNE_MARAMBA_P0:
66814977Sraghus 		case P_NEPTUNE_MARAMBA_P1:
66827801SSantwona.Behera@Sun.COM 		case P_NEPTUNE_ROCK:
66835196Ssbehera 		case P_NEPTUNE_ALONSO:
66844977Sraghus 			nports = 4;
66854977Sraghus 			break;
66864977Sraghus 		default:
66874977Sraghus 			break;
66884977Sraghus 		}
66894732Sdavemq 		break;
66904732Sdavemq 	}
66914732Sdavemq 
66924732Sdavemq 	return (nports);
66934732Sdavemq }
66945013Sml29623 
66955013Sml29623 /*
66965013Sml29623  * The following two functions are to support
66975013Sml29623  * PSARC/2007/453 MSI-X interrupt limit override.
66985013Sml29623  */
66995013Sml29623 static int
67005013Sml29623 nxge_create_msi_property(p_nxge_t nxgep)
67015013Sml29623 {
67025013Sml29623 	int	nmsi;
67035013Sml29623 	extern	int ncpus;
67045013Sml29623 
67055013Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "==>nxge_create_msi_property"));
67065013Sml29623 
67075013Sml29623 	switch (nxgep->mac.portmode) {
67085013Sml29623 	case PORT_10G_COPPER:
67095013Sml29623 	case PORT_10G_FIBER:
67106835Syc148097 	case PORT_10G_TN1010:
67115013Sml29623 		(void) ddi_prop_create(DDI_DEV_T_NONE, nxgep->dip,
67125013Sml29623 		    DDI_PROP_CANSLEEP, "#msix-request", NULL, 0);
67135013Sml29623 		/*
67145013Sml29623 		 * The maximum MSI-X requested will be 8.
67158455Stc99174@train 		 * If the # of CPUs is less than 8, we will request
67168455Stc99174@train 		 * # MSI-X based on the # of CPUs (default).
67175013Sml29623 		 */
67188455Stc99174@train 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
67198455Stc99174@train 		    "==>nxge_create_msi_property (10G): nxge_msix_10g_intrs %d",
67208455Stc99174@train 		    nxge_msix_10g_intrs));
67218455Stc99174@train 		if ((nxge_msix_10g_intrs == 0) ||
67228455Stc99174@train 		    (nxge_msix_10g_intrs > NXGE_MSIX_MAX_ALLOWED)) {
67235013Sml29623 			nmsi = NXGE_MSIX_REQUEST_10G;
67248455Stc99174@train 			NXGE_DEBUG_MSG((nxgep, MOD_CTL,
67258455Stc99174@train 			    "==>nxge_create_msi_property (10G): reset to 8"));
67265013Sml29623 		} else {
67278455Stc99174@train 			nmsi = nxge_msix_10g_intrs;
67288455Stc99174@train 		}
67298455Stc99174@train 
67308455Stc99174@train 		/*
67318455Stc99174@train 		 * If # of interrupts requested is 8 (default),
67328455Stc99174@train 		 * the checking of the number of cpus will be
67338455Stc99174@train 		 * be maintained.
67348455Stc99174@train 		 */
67358455Stc99174@train 		if ((nmsi == NXGE_MSIX_REQUEST_10G) &&
67368455Stc99174@train 		    (ncpus < nmsi)) {
67378455Stc99174@train 			NXGE_DEBUG_MSG((nxgep, MOD_CTL,
67388455Stc99174@train 			    "==>nxge_create_msi_property (10G): reset to 8"));
67395013Sml29623 			nmsi = ncpus;
67405013Sml29623 		}
67415013Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
67425013Sml29623 		    "==>nxge_create_msi_property(10G): exists 0x%x (nmsi %d)",
67435013Sml29623 		    ddi_prop_exists(DDI_DEV_T_NONE, nxgep->dip,
67445013Sml29623 		    DDI_PROP_CANSLEEP, "#msix-request"), nmsi));
67455013Sml29623 		break;
67465013Sml29623 
67475013Sml29623 	default:
67488455Stc99174@train 		(void) ddi_prop_create(DDI_DEV_T_NONE, nxgep->dip,
67498455Stc99174@train 		    DDI_PROP_CANSLEEP, "#msix-request", NULL, 0);
67508455Stc99174@train 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
67518455Stc99174@train 		    "==>nxge_create_msi_property (1G): nxge_msix_1g_intrs %d",
67528455Stc99174@train 		    nxge_msix_1g_intrs));
67538455Stc99174@train 		if ((nxge_msix_1g_intrs == 0) ||
67548455Stc99174@train 		    (nxge_msix_1g_intrs > NXGE_MSIX_MAX_ALLOWED)) {
67558455Stc99174@train 			nmsi = NXGE_MSIX_REQUEST_1G;
67568455Stc99174@train 			NXGE_DEBUG_MSG((nxgep, MOD_CTL,
67578455Stc99174@train 			    "==>nxge_create_msi_property (1G): reset to 2"));
67588455Stc99174@train 		} else {
67598455Stc99174@train 			nmsi = nxge_msix_1g_intrs;
67608455Stc99174@train 		}
67615013Sml29623 		NXGE_DEBUG_MSG((nxgep, MOD_CTL,
67625013Sml29623 		    "==>nxge_create_msi_property(1G): exists 0x%x (nmsi %d)",
67635013Sml29623 		    ddi_prop_exists(DDI_DEV_T_NONE, nxgep->dip,
67645013Sml29623 		    DDI_PROP_CANSLEEP, "#msix-request"), nmsi));
67655013Sml29623 		break;
67665013Sml29623 	}
67675013Sml29623 
67685013Sml29623 	NXGE_DEBUG_MSG((nxgep, MOD_CTL, "<==nxge_create_msi_property"));
67695013Sml29623 	return (nmsi);
67705013Sml29623 }
67716512Ssowmini 
67726512Ssowmini /* ARGSUSED */
67736512Ssowmini static int
67746512Ssowmini nxge_get_def_val(nxge_t *nxgep, mac_prop_id_t pr_num, uint_t pr_valsize,
67756512Ssowmini     void *pr_val)
67766512Ssowmini {
67776512Ssowmini 	int err = 0;
67786512Ssowmini 	link_flowctrl_t fl;
67796512Ssowmini 
67806512Ssowmini 	switch (pr_num) {
67816789Sam223141 	case MAC_PROP_AUTONEG:
67826512Ssowmini 		*(uint8_t *)pr_val = 1;
67836512Ssowmini 		break;
67846789Sam223141 	case MAC_PROP_FLOWCTRL:
67856512Ssowmini 		if (pr_valsize < sizeof (link_flowctrl_t))
67866512Ssowmini 			return (EINVAL);
67876512Ssowmini 		fl = LINK_FLOWCTRL_RX;
67886512Ssowmini 		bcopy(&fl, pr_val, sizeof (fl));
67896512Ssowmini 		break;
67906789Sam223141 	case MAC_PROP_ADV_1000FDX_CAP:
67916789Sam223141 	case MAC_PROP_EN_1000FDX_CAP:
67926512Ssowmini 		*(uint8_t *)pr_val = 1;
67936512Ssowmini 		break;
67946789Sam223141 	case MAC_PROP_ADV_100FDX_CAP:
67956789Sam223141 	case MAC_PROP_EN_100FDX_CAP:
67966512Ssowmini 		*(uint8_t *)pr_val = 1;
67976512Ssowmini 		break;
67986512Ssowmini 	default:
67996512Ssowmini 		err = ENOTSUP;
68006512Ssowmini 		break;
68016512Ssowmini 	}
68026512Ssowmini 	return (err);
68036512Ssowmini }
68046705Sml29623 
68056705Sml29623 
68066705Sml29623 /*
68076705Sml29623  * The following is a software around for the Neptune hardware's
68086705Sml29623  * interrupt bugs; The Neptune hardware may generate spurious interrupts when
68096705Sml29623  * an interrupr handler is removed.
68106705Sml29623  */
68116705Sml29623 #define	NXGE_PCI_PORT_LOGIC_OFFSET	0x98
68126705Sml29623 #define	NXGE_PIM_RESET			(1ULL << 29)
68136705Sml29623 #define	NXGE_GLU_RESET			(1ULL << 30)
68146705Sml29623 #define	NXGE_NIU_RESET			(1ULL << 31)
68156705Sml29623 #define	NXGE_PCI_RESET_ALL		(NXGE_PIM_RESET |	\
68166705Sml29623 					NXGE_GLU_RESET |	\
68176705Sml29623 					NXGE_NIU_RESET)
68186705Sml29623 
68196705Sml29623 #define	NXGE_WAIT_QUITE_TIME		200000
68206705Sml29623 #define	NXGE_WAIT_QUITE_RETRY		40
68216705Sml29623 #define	NXGE_PCI_RESET_WAIT		1000000 /* one second */
68226705Sml29623 
68236705Sml29623 static void
68246705Sml29623 nxge_niu_peu_reset(p_nxge_t nxgep)
68256705Sml29623 {
68266705Sml29623 	uint32_t	rvalue;
68276705Sml29623 	p_nxge_hw_list_t hw_p;
68286705Sml29623 	p_nxge_t	fnxgep;
68296705Sml29623 	int		i, j;
68306705Sml29623 
68316705Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL, "==> nxge_niu_peu_reset"));
68326705Sml29623 	if ((hw_p = nxgep->nxge_hw_p) == NULL) {
68336705Sml29623 		NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
68346705Sml29623 		    "==> nxge_niu_peu_reset: NULL hardware pointer"));
68356705Sml29623 		return;
68366705Sml29623 	}
68376705Sml29623 
68386705Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
68396705Sml29623 	    "==> nxge_niu_peu_reset: flags 0x%x link timer id %d timer id %d",
68406705Sml29623 	    hw_p->flags, nxgep->nxge_link_poll_timerid,
68416705Sml29623 	    nxgep->nxge_timerid));
68426705Sml29623 
68436705Sml29623 	MUTEX_ENTER(&hw_p->nxge_cfg_lock);
68446705Sml29623 	/*
68456705Sml29623 	 * Make sure other instances from the same hardware
68466705Sml29623 	 * stop sending PIO and in quiescent state.
68476705Sml29623 	 */
68486705Sml29623 	for (i = 0; i < NXGE_MAX_PORTS; i++) {
68496705Sml29623 		fnxgep = hw_p->nxge_p[i];
68506705Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
68516705Sml29623 		    "==> nxge_niu_peu_reset: checking entry %d "
68526705Sml29623 		    "nxgep $%p", i, fnxgep));
68536705Sml29623 #ifdef	NXGE_DEBUG
68546705Sml29623 		if (fnxgep) {
68556705Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
68566705Sml29623 			    "==> nxge_niu_peu_reset: entry %d (function %d) "
68576705Sml29623 			    "link timer id %d hw timer id %d",
68586705Sml29623 			    i, fnxgep->function_num,
68596705Sml29623 			    fnxgep->nxge_link_poll_timerid,
68606705Sml29623 			    fnxgep->nxge_timerid));
68616705Sml29623 		}
68626705Sml29623 #endif
68636705Sml29623 		if (fnxgep && fnxgep != nxgep &&
68646705Sml29623 		    (fnxgep->nxge_timerid || fnxgep->nxge_link_poll_timerid)) {
68656705Sml29623 			NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
68666705Sml29623 			    "==> nxge_niu_peu_reset: checking $%p "
68676705Sml29623 			    "(function %d) timer ids",
68686705Sml29623 			    fnxgep, fnxgep->function_num));
68696705Sml29623 			for (j = 0; j < NXGE_WAIT_QUITE_RETRY; j++) {
68706705Sml29623 				NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
68716705Sml29623 				    "==> nxge_niu_peu_reset: waiting"));
68726705Sml29623 				NXGE_DELAY(NXGE_WAIT_QUITE_TIME);
68736705Sml29623 				if (!fnxgep->nxge_timerid &&
68746705Sml29623 				    !fnxgep->nxge_link_poll_timerid) {
68756705Sml29623 					break;
68766705Sml29623 				}
68776705Sml29623 			}
68786705Sml29623 			NXGE_DELAY(NXGE_WAIT_QUITE_TIME);
68796705Sml29623 			if (fnxgep->nxge_timerid ||
68806705Sml29623 			    fnxgep->nxge_link_poll_timerid) {
68816705Sml29623 				MUTEX_EXIT(&hw_p->nxge_cfg_lock);
68826705Sml29623 				NXGE_ERROR_MSG((nxgep, NXGE_ERR_CTL,
68836705Sml29623 				    "<== nxge_niu_peu_reset: cannot reset "
68846705Sml29623 				    "hardware (devices are still in use)"));
68856705Sml29623 				return;
68866705Sml29623 			}
68876705Sml29623 		}
68886705Sml29623 	}
68896705Sml29623 
68906705Sml29623 	if ((hw_p->flags & COMMON_RESET_NIU_PCI) != COMMON_RESET_NIU_PCI) {
68916705Sml29623 		hw_p->flags |= COMMON_RESET_NIU_PCI;
68926705Sml29623 		rvalue = pci_config_get32(nxgep->dev_regs->nxge_pciregh,
68936705Sml29623 		    NXGE_PCI_PORT_LOGIC_OFFSET);
68946705Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
68956705Sml29623 		    "nxge_niu_peu_reset: read offset 0x%x (%d) "
68966705Sml29623 		    "(data 0x%x)",
68976705Sml29623 		    NXGE_PCI_PORT_LOGIC_OFFSET,
68986705Sml29623 		    NXGE_PCI_PORT_LOGIC_OFFSET,
68996705Sml29623 		    rvalue));
69006705Sml29623 
69016705Sml29623 		rvalue |= NXGE_PCI_RESET_ALL;
69026705Sml29623 		pci_config_put32(nxgep->dev_regs->nxge_pciregh,
69036705Sml29623 		    NXGE_PCI_PORT_LOGIC_OFFSET, rvalue);
69046705Sml29623 		NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL,
69056705Sml29623 		    "nxge_niu_peu_reset: RESETTING NIU: write NIU reset 0x%x",
69066705Sml29623 		    rvalue));
69076705Sml29623 
69086705Sml29623 		NXGE_DELAY(NXGE_PCI_RESET_WAIT);
69096705Sml29623 	}
69106705Sml29623 
69116705Sml29623 	MUTEX_EXIT(&hw_p->nxge_cfg_lock);
69126705Sml29623 	NXGE_DEBUG_MSG((nxgep, NXGE_ERR_CTL, "<== nxge_niu_peu_reset"));
69136705Sml29623 }
69147126Sml29623 
69157126Sml29623 static void
69167126Sml29623 nxge_set_pci_replay_timeout(p_nxge_t nxgep)
69177126Sml29623 {
69188275SEric Cheng 	p_dev_regs_t	dev_regs;
69197126Sml29623 	uint32_t	value;
69207126Sml29623 
69217126Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "==> nxge_set_pci_replay_timeout"));
69227126Sml29623 
69237126Sml29623 	if (!nxge_set_replay_timer) {
69247126Sml29623 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
69257126Sml29623 		    "==> nxge_set_pci_replay_timeout: will not change "
69267126Sml29623 		    "the timeout"));
69277126Sml29623 		return;
69287126Sml29623 	}
69297126Sml29623 
69307126Sml29623 	dev_regs = nxgep->dev_regs;
69317126Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
69327126Sml29623 	    "==> nxge_set_pci_replay_timeout: dev_regs 0x%p pcireg 0x%p",
69337126Sml29623 	    dev_regs, dev_regs->nxge_pciregh));
69347126Sml29623 
69357126Sml29623 	if (dev_regs == NULL || (dev_regs->nxge_pciregh == NULL)) {
69367145Syc148097 		NXGE_DEBUG_MSG((nxgep, DDI_CTL,
69377126Sml29623 		    "==> nxge_set_pci_replay_timeout: NULL dev_regs $%p or "
69387126Sml29623 		    "no PCI handle",
69397126Sml29623 		    dev_regs));
69407126Sml29623 		return;
69417126Sml29623 	}
69427126Sml29623 	value = (pci_config_get32(dev_regs->nxge_pciregh,
69437126Sml29623 	    PCI_REPLAY_TIMEOUT_CFG_OFFSET) |
69447126Sml29623 	    (nxge_replay_timeout << PCI_REPLAY_TIMEOUT_SHIFT));
69457126Sml29623 
69467126Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
69477126Sml29623 	    "nxge_set_pci_replay_timeout: replay timeout value before set 0x%x "
69487126Sml29623 	    "(timeout value to set 0x%x at offset 0x%x) value 0x%x",
69497126Sml29623 	    pci_config_get32(dev_regs->nxge_pciregh,
69507126Sml29623 	    PCI_REPLAY_TIMEOUT_CFG_OFFSET), nxge_replay_timeout,
69517126Sml29623 	    PCI_REPLAY_TIMEOUT_CFG_OFFSET, value));
69527126Sml29623 
69537126Sml29623 	pci_config_put32(dev_regs->nxge_pciregh, PCI_REPLAY_TIMEOUT_CFG_OFFSET,
69547126Sml29623 	    value);
69557126Sml29623 
69567126Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL,
69577126Sml29623 	    "nxge_set_pci_replay_timeout: replay timeout value after set 0x%x",
69587126Sml29623 	    pci_config_get32(dev_regs->nxge_pciregh,
69597126Sml29623 	    PCI_REPLAY_TIMEOUT_CFG_OFFSET)));
69607126Sml29623 
69617126Sml29623 	NXGE_DEBUG_MSG((nxgep, DDI_CTL, "<== nxge_set_pci_replay_timeout"));
69627126Sml29623 }
69637656SSherry.Moore@Sun.COM 
69647656SSherry.Moore@Sun.COM /*
69657656SSherry.Moore@Sun.COM  * quiesce(9E) entry point.
69667656SSherry.Moore@Sun.COM  *
69677656SSherry.Moore@Sun.COM  * This function is called when the system is single-threaded at high
69687656SSherry.Moore@Sun.COM  * PIL with preemption disabled. Therefore, this function must not be
69697656SSherry.Moore@Sun.COM  * blocked.
69707656SSherry.Moore@Sun.COM  *
69717656SSherry.Moore@Sun.COM  * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
69727656SSherry.Moore@Sun.COM  * DDI_FAILURE indicates an error condition and should almost never happen.
69737656SSherry.Moore@Sun.COM  */
69747656SSherry.Moore@Sun.COM static int
69757656SSherry.Moore@Sun.COM nxge_quiesce(dev_info_t *dip)
69767656SSherry.Moore@Sun.COM {
69777656SSherry.Moore@Sun.COM 	int instance = ddi_get_instance(dip);
69787656SSherry.Moore@Sun.COM 	p_nxge_t nxgep = (p_nxge_t)ddi_get_soft_state(nxge_list, instance);
69797656SSherry.Moore@Sun.COM 
69807656SSherry.Moore@Sun.COM 	if (nxgep == NULL)
69817656SSherry.Moore@Sun.COM 		return (DDI_FAILURE);
69827656SSherry.Moore@Sun.COM 
69837656SSherry.Moore@Sun.COM 	/* Turn off debugging */
69847656SSherry.Moore@Sun.COM 	nxge_debug_level = NO_DEBUG;
69857656SSherry.Moore@Sun.COM 	nxgep->nxge_debug_level = NO_DEBUG;
69867656SSherry.Moore@Sun.COM 	npi_debug_level = NO_DEBUG;
69877656SSherry.Moore@Sun.COM 
69887656SSherry.Moore@Sun.COM 	/*
69897656SSherry.Moore@Sun.COM 	 * Stop link monitor only when linkchkmod is interrupt based
69907656SSherry.Moore@Sun.COM 	 */
69917656SSherry.Moore@Sun.COM 	if (nxgep->mac.linkchkmode == LINKCHK_INTR) {
69927656SSherry.Moore@Sun.COM 		(void) nxge_link_monitor(nxgep, LINK_MONITOR_STOP);
69937656SSherry.Moore@Sun.COM 	}
69947656SSherry.Moore@Sun.COM 
69957656SSherry.Moore@Sun.COM 	(void) nxge_intr_hw_disable(nxgep);
69967656SSherry.Moore@Sun.COM 
69977656SSherry.Moore@Sun.COM 	/*
69987656SSherry.Moore@Sun.COM 	 * Reset the receive MAC side.
69997656SSherry.Moore@Sun.COM 	 */
70007656SSherry.Moore@Sun.COM 	(void) nxge_rx_mac_disable(nxgep);
70017656SSherry.Moore@Sun.COM 
70027656SSherry.Moore@Sun.COM 	/* Disable and soft reset the IPP */
70037656SSherry.Moore@Sun.COM 	if (!isLDOMguest(nxgep))
70047656SSherry.Moore@Sun.COM 		(void) nxge_ipp_disable(nxgep);
70057656SSherry.Moore@Sun.COM 
70067656SSherry.Moore@Sun.COM 	/*
70077656SSherry.Moore@Sun.COM 	 * Reset the transmit/receive DMA side.
70087656SSherry.Moore@Sun.COM 	 */
70097656SSherry.Moore@Sun.COM 	(void) nxge_txdma_hw_mode(nxgep, NXGE_DMA_STOP);
70107656SSherry.Moore@Sun.COM 	(void) nxge_rxdma_hw_mode(nxgep, NXGE_DMA_STOP);
70117656SSherry.Moore@Sun.COM 
70127656SSherry.Moore@Sun.COM 	/*
70137656SSherry.Moore@Sun.COM 	 * Reset the transmit MAC side.
70147656SSherry.Moore@Sun.COM 	 */
70157656SSherry.Moore@Sun.COM 	(void) nxge_tx_mac_disable(nxgep);
70167656SSherry.Moore@Sun.COM 
70177656SSherry.Moore@Sun.COM 	return (DDI_SUCCESS);
70187656SSherry.Moore@Sun.COM }
7019