xref: /onnv-gate/usr/src/uts/sun4u/io/pci/simba.c (revision 7656:2621e50fdf4a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51865Sdilpreet  * Common Development and Distribution License (the "License").
61865Sdilpreet  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*7656SSherry.Moore@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  *	PCI to PCI bus bridge nexus driver
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/conf.h>
320Sstevel@tonic-gate #include <sys/kmem.h>
330Sstevel@tonic-gate #include <sys/debug.h>
340Sstevel@tonic-gate #include <sys/modctl.h>
350Sstevel@tonic-gate #include <sys/autoconf.h>
360Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
370Sstevel@tonic-gate #include <sys/ddi_subrdefs.h>
380Sstevel@tonic-gate #include <sys/ddifm.h>
390Sstevel@tonic-gate #include <sys/fm/util.h>
400Sstevel@tonic-gate #include <sys/fm/protocol.h>
410Sstevel@tonic-gate #include <sys/fm/io/pci.h>
420Sstevel@tonic-gate #include <sys/pci.h>
430Sstevel@tonic-gate #include <sys/pci/pci_nexus.h>
440Sstevel@tonic-gate #include <sys/pci/pci_regs.h>
450Sstevel@tonic-gate #include <sys/pci/pci_simba.h>
460Sstevel@tonic-gate #include <sys/ddi.h>
470Sstevel@tonic-gate #include <sys/sunddi.h>
480Sstevel@tonic-gate #include <sys/sunndi.h>
490Sstevel@tonic-gate #include <sys/promif.h>		/* prom_printf */
500Sstevel@tonic-gate #include <sys/open.h>
510Sstevel@tonic-gate #include <sys/stat.h>
520Sstevel@tonic-gate #include <sys/file.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #if defined(DEBUG) && !defined(lint)
550Sstevel@tonic-gate static uint_t simba_debug_flags = 0;
560Sstevel@tonic-gate #define	D_IDENTIFY	0x00000001
570Sstevel@tonic-gate #define	D_ATTACH	0x00000002
580Sstevel@tonic-gate #define	D_DETACH	0x00000004
590Sstevel@tonic-gate #define	D_MAP		0x00000008
600Sstevel@tonic-gate #define	D_CTLOPS	0x00000010
610Sstevel@tonic-gate #define	D_G_ISPEC	0x00000020
620Sstevel@tonic-gate #define	D_A_ISPEC	0x00000040
630Sstevel@tonic-gate #define	D_INIT_CLD	0x00400000
640Sstevel@tonic-gate #define	D_FAULT		0x00000080
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #define	DEBUG0(f, s) if ((f)& simba_debug_flags) \
670Sstevel@tonic-gate 	prom_printf("simba: " s "\n")
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #define	DEBUG1(f, s, a) if ((f)& simba_debug_flags) \
700Sstevel@tonic-gate 	prom_printf("simba: " s "\n", a)
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #define	DEBUG2(f, s, a, b) if ((f)& simba_debug_flags) \
730Sstevel@tonic-gate 	prom_printf("simba: " s "\n", a, b)
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #define	DEBUG3(f, s, a, b, c) if ((f)& simba_debug_flags) \
760Sstevel@tonic-gate 	prom_printf("simba: " s "\n", a, b, c)
770Sstevel@tonic-gate 
780Sstevel@tonic-gate #define	DEBUG4(f, s, a, b, c, d) if ((f)& simba_debug_flags) \
790Sstevel@tonic-gate 	prom_printf("simba: " s "\n", a, b, c, d)
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #define	DEBUG5(f, s, a, b, c, d, e) if ((f)& simba_debug_flags) \
820Sstevel@tonic-gate 	prom_printf("simba: " s "\n", a, b, c, d, e)
830Sstevel@tonic-gate 
840Sstevel@tonic-gate #define	DEBUG6(f, s, a, b, c, d, e, ff) if ((f)& simba_debug_flags) \
850Sstevel@tonic-gate 	prom_printf("simba: " s "\n", a, b, c, d, e, ff)
860Sstevel@tonic-gate 
870Sstevel@tonic-gate #else
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #define	DEBUG0(f, s)
900Sstevel@tonic-gate #define	DEBUG1(f, s, a)
910Sstevel@tonic-gate #define	DEBUG2(f, s, a, b)
920Sstevel@tonic-gate #define	DEBUG3(f, s, a, b, c)
930Sstevel@tonic-gate #define	DEBUG4(f, s, a, b, c, d)
940Sstevel@tonic-gate #define	DEBUG5(f, s, a, b, c, d, e)
950Sstevel@tonic-gate #define	DEBUG6(f, s, a, b, c, d, e, ff)
960Sstevel@tonic-gate 
970Sstevel@tonic-gate #endif
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  * The variable controls the default setting of the command register
1010Sstevel@tonic-gate  * for pci devices.  See simba_initchild() for details.
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate static ushort_t simba_command_default = PCI_COMM_SERR_ENABLE |
1040Sstevel@tonic-gate 					PCI_COMM_WAIT_CYC_ENAB |
1050Sstevel@tonic-gate 					PCI_COMM_PARITY_DETECT |
1060Sstevel@tonic-gate 					PCI_COMM_ME |
1070Sstevel@tonic-gate 					PCI_COMM_MAE |
1080Sstevel@tonic-gate 					PCI_COMM_IO;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate static int simba_bus_map(dev_info_t *, dev_info_t *, ddi_map_req_t *,
1110Sstevel@tonic-gate 	off_t, off_t, caddr_t *);
1120Sstevel@tonic-gate static int simba_ctlops(dev_info_t *, dev_info_t *, ddi_ctl_enum_t,
1130Sstevel@tonic-gate 	void *, void *);
1140Sstevel@tonic-gate static int simba_fm_init_child(dev_info_t *dip, dev_info_t *tdip, int cap,
1150Sstevel@tonic-gate 		ddi_iblock_cookie_t *ibc);
1160Sstevel@tonic-gate static void simba_bus_enter(dev_info_t *dip, ddi_acc_handle_t handle);
1170Sstevel@tonic-gate static void simba_bus_exit(dev_info_t *dip, ddi_acc_handle_t handle);
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate struct bus_ops simba_bus_ops = {
1200Sstevel@tonic-gate 	BUSO_REV,
1210Sstevel@tonic-gate 	simba_bus_map,
1220Sstevel@tonic-gate 	0,
1230Sstevel@tonic-gate 	0,
1240Sstevel@tonic-gate 	0,
1250Sstevel@tonic-gate 	i_ddi_map_fault,
1260Sstevel@tonic-gate 	ddi_dma_map,
1270Sstevel@tonic-gate 	ddi_dma_allochdl,
1280Sstevel@tonic-gate 	ddi_dma_freehdl,
1290Sstevel@tonic-gate 	ddi_dma_bindhdl,
1300Sstevel@tonic-gate 	ddi_dma_unbindhdl,
1310Sstevel@tonic-gate 	ddi_dma_flush,
1320Sstevel@tonic-gate 	ddi_dma_win,
1330Sstevel@tonic-gate 	ddi_dma_mctl,
1340Sstevel@tonic-gate 	simba_ctlops,
1350Sstevel@tonic-gate 	ddi_bus_prop_op,
1360Sstevel@tonic-gate 	ndi_busop_get_eventcookie,
1370Sstevel@tonic-gate 	ndi_busop_add_eventcall,
1380Sstevel@tonic-gate 	ndi_busop_remove_eventcall,
1390Sstevel@tonic-gate 	ndi_post_event,
1400Sstevel@tonic-gate 	0,
1410Sstevel@tonic-gate 	0,
1420Sstevel@tonic-gate 	0,
1430Sstevel@tonic-gate 	simba_fm_init_child,
1440Sstevel@tonic-gate 	NULL,
1450Sstevel@tonic-gate 	simba_bus_enter,
1460Sstevel@tonic-gate 	simba_bus_exit,
1470Sstevel@tonic-gate 	0,
1480Sstevel@tonic-gate 	i_ddi_intr_ops
1490Sstevel@tonic-gate };
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate static int simba_open(dev_t *devp, int flags, int otyp, cred_t *credp);
1520Sstevel@tonic-gate static int simba_close(dev_t dev, int flags, int otyp, cred_t *credp);
1530Sstevel@tonic-gate static int simba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
1540Sstevel@tonic-gate     cred_t *credp, int *rvalp);
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate static struct cb_ops simba_cb_ops = {
1570Sstevel@tonic-gate 	simba_open,			/* open */
1580Sstevel@tonic-gate 	simba_close,			/* close */
1590Sstevel@tonic-gate 	nulldev,			/* strategy */
1600Sstevel@tonic-gate 	nulldev,			/* print */
1610Sstevel@tonic-gate 	nulldev,			/* dump */
1620Sstevel@tonic-gate 	nulldev,			/* read */
1630Sstevel@tonic-gate 	nulldev,			/* write */
1640Sstevel@tonic-gate 	simba_ioctl,			/* ioctl */
1650Sstevel@tonic-gate 	nodev,				/* devmap */
1660Sstevel@tonic-gate 	nodev,				/* mmap */
1670Sstevel@tonic-gate 	nodev,				/* segmap */
1680Sstevel@tonic-gate 	nochpoll,			/* poll */
1690Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
1700Sstevel@tonic-gate 	NULL,				/* streamtab */
1710Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
1720Sstevel@tonic-gate 	CB_REV,				/* rev */
1730Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
1740Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
1750Sstevel@tonic-gate };
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate static int simba_probe(dev_info_t *);
1780Sstevel@tonic-gate static int simba_attach(dev_info_t *devi, ddi_attach_cmd_t cmd);
1790Sstevel@tonic-gate static int simba_detach(dev_info_t *devi, ddi_detach_cmd_t cmd);
1800Sstevel@tonic-gate static int simba_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
1810Sstevel@tonic-gate 	void *arg, void **result);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate struct dev_ops simba_ops = {
1840Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
1850Sstevel@tonic-gate 	0,			/* refcnt  */
1860Sstevel@tonic-gate 	simba_info,		/* info */
1870Sstevel@tonic-gate 	nulldev,		/* identify */
1880Sstevel@tonic-gate 	simba_probe,		/* probe */
1890Sstevel@tonic-gate 	simba_attach,		/* attach */
1900Sstevel@tonic-gate 	simba_detach,		/* detach */
1910Sstevel@tonic-gate 	nulldev,		/* reset */
1920Sstevel@tonic-gate 	&simba_cb_ops,		/* driver operations */
193*7656SSherry.Moore@Sun.COM 	&simba_bus_ops,		/* bus operations */
194*7656SSherry.Moore@Sun.COM 	NULL,
195*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_supported,	/* devo_quiesce */
1960Sstevel@tonic-gate };
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate /*
1990Sstevel@tonic-gate  * Module linkage information for the kernel.
2000Sstevel@tonic-gate  */
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate static struct modldrv modldrv = {
2030Sstevel@tonic-gate 	&mod_driverops, /* Type of module */
204*7656SSherry.Moore@Sun.COM 	"SIMBA PCI to PCI bridge nexus driver",
2050Sstevel@tonic-gate 	&simba_ops,	/* driver ops */
2060Sstevel@tonic-gate };
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate static struct modlinkage modlinkage = {
2090Sstevel@tonic-gate 	MODREV_1,
2100Sstevel@tonic-gate 	(void *)&modldrv,
2110Sstevel@tonic-gate 	NULL
2120Sstevel@tonic-gate };
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate  * Simba specific error state structure
2160Sstevel@tonic-gate  */
217946Smathue struct simba_errstate {
2180Sstevel@tonic-gate 	char *error;
2190Sstevel@tonic-gate 	ushort_t pci_cfg_stat;
2200Sstevel@tonic-gate 	ushort_t pci_cfg_sec_stat;
2210Sstevel@tonic-gate 	uint64_t afsr;
2220Sstevel@tonic-gate 	uint64_t afar;
2230Sstevel@tonic-gate 	int bridge_secondary;
2240Sstevel@tonic-gate };
2250Sstevel@tonic-gate 
226946Smathue struct simba_cfg_state {
2270Sstevel@tonic-gate 	dev_info_t *dip;
2280Sstevel@tonic-gate 	ushort_t command;
2290Sstevel@tonic-gate 	uchar_t cache_line_size;
2300Sstevel@tonic-gate 	uchar_t latency_timer;
2310Sstevel@tonic-gate 	uchar_t header_type;
2320Sstevel@tonic-gate 	uchar_t bus_number;
2330Sstevel@tonic-gate 	uchar_t sec_bus_number;
2340Sstevel@tonic-gate 	uchar_t sub_bus_number;
2350Sstevel@tonic-gate 	uchar_t sec_latency_timer;
2360Sstevel@tonic-gate 	ushort_t bridge_control;
2370Sstevel@tonic-gate };
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate /*
2400Sstevel@tonic-gate  * soft state pointer and structure template:
2410Sstevel@tonic-gate  */
2420Sstevel@tonic-gate static void *simba_state;
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate typedef struct {
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	dev_info_t *dip;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	/*
2490Sstevel@tonic-gate 	 * configuration register state for the bus:
2500Sstevel@tonic-gate 	 */
2510Sstevel@tonic-gate 	ddi_acc_handle_t config_handle;
2520Sstevel@tonic-gate 	uchar_t simba_cache_line_size;
2530Sstevel@tonic-gate 	uchar_t simba_latency_timer;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	/*
2560Sstevel@tonic-gate 	 * cpr support:
2570Sstevel@tonic-gate 	 */
2580Sstevel@tonic-gate 	uint_t config_state_index;
2590Sstevel@tonic-gate 	struct simba_cfg_state *simba_config_state_p;
2600Sstevel@tonic-gate 	ddi_iblock_cookie_t fm_ibc;
2610Sstevel@tonic-gate 	int fm_cap;
2620Sstevel@tonic-gate 	kmutex_t simba_mutex;
2630Sstevel@tonic-gate 	uint_t simba_soft_state;
2640Sstevel@tonic-gate #define	SIMBA_SOFT_STATE_CLOSED		0x00
2650Sstevel@tonic-gate #define	SIMBA_SOFT_STATE_OPEN		0x01
2660Sstevel@tonic-gate #define	SIMBA_SOFT_STATE_OPEN_EXCL	0x02
2670Sstevel@tonic-gate } simba_devstate_t;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate /*
2700Sstevel@tonic-gate  * The following variable enables a workaround for the following obp bug:
2710Sstevel@tonic-gate  *
2720Sstevel@tonic-gate  *	1234181 - obp should set latency timer registers in pci
2730Sstevel@tonic-gate  *		configuration header
2740Sstevel@tonic-gate  *
2750Sstevel@tonic-gate  * Until this bug gets fixed in the obp, the following workaround should
2760Sstevel@tonic-gate  * be enabled.
2770Sstevel@tonic-gate  */
2780Sstevel@tonic-gate static uint_t simba_set_latency_timer_register = 1;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate /*
2810Sstevel@tonic-gate  * The following variable enables a workaround for an obp bug to be
2820Sstevel@tonic-gate  * submitted.  A bug requesting a workaround fof this problem has
2830Sstevel@tonic-gate  * been filed:
2840Sstevel@tonic-gate  *
2850Sstevel@tonic-gate  *	1235094 - need workarounds on positron nexus drivers to set cache
2860Sstevel@tonic-gate  *		line size registers
2870Sstevel@tonic-gate  *
2880Sstevel@tonic-gate  * Until this bug gets fixed in the obp, the following workaround should
2890Sstevel@tonic-gate  * be enabled.
2900Sstevel@tonic-gate  */
2910Sstevel@tonic-gate static uint_t simba_set_cache_line_size_register = 1;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate /*
2950Sstevel@tonic-gate  * forward function declarations:
2960Sstevel@tonic-gate  */
2970Sstevel@tonic-gate static void simba_uninitchild(dev_info_t *);
2980Sstevel@tonic-gate static int simba_initchild(dev_info_t *child);
2990Sstevel@tonic-gate static void simba_save_config_regs(simba_devstate_t *simba_p);
3000Sstevel@tonic-gate static void simba_restore_config_regs(simba_devstate_t *simba_p);
3010Sstevel@tonic-gate static int simba_err_callback(dev_info_t *dip, ddi_fm_error_t *derr,
3020Sstevel@tonic-gate 		const void *impl_data);
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate int
_init(void)3050Sstevel@tonic-gate _init(void)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate 	int e;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	DEBUG0(D_ATTACH, "_init() installing module...\n");
3100Sstevel@tonic-gate 	if ((e = ddi_soft_state_init(&simba_state, sizeof (simba_devstate_t),
3110Sstevel@tonic-gate 	    1)) == 0 && (e = mod_install(&modlinkage)) != 0)
3120Sstevel@tonic-gate 		ddi_soft_state_fini(&simba_state);
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	DEBUG0(D_ATTACH, "_init() module installed\n");
3150Sstevel@tonic-gate 	return (e);
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate int
_fini(void)3190Sstevel@tonic-gate _fini(void)
3200Sstevel@tonic-gate {
3210Sstevel@tonic-gate 	int e;
3220Sstevel@tonic-gate 	DEBUG0(D_ATTACH, "_fini() removing module...\n");
3230Sstevel@tonic-gate 	if ((e = mod_remove(&modlinkage)) == 0)
3240Sstevel@tonic-gate 		ddi_soft_state_fini(&simba_state);
3250Sstevel@tonic-gate 	return (e);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate int
_info(struct modinfo * modinfop)3290Sstevel@tonic-gate _info(struct modinfo *modinfop)
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate 	DEBUG0(D_ATTACH, "_info() called.\n");
3320Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate /*ARGSUSED*/
3360Sstevel@tonic-gate static int
simba_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)3370Sstevel@tonic-gate simba_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate 	simba_devstate_t *simba_p;	/* per simba state pointer */
3400Sstevel@tonic-gate 	int instance;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	instance = getminor((dev_t)arg);
3430Sstevel@tonic-gate 	simba_p = (simba_devstate_t *)ddi_get_soft_state(simba_state,
3440Sstevel@tonic-gate 	    instance);
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	switch (infocmd) {
3470Sstevel@tonic-gate 	default:
3480Sstevel@tonic-gate 		return (DDI_FAILURE);
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
351946Smathue 		*result = (void *)(uintptr_t)instance;
3520Sstevel@tonic-gate 		return (DDI_SUCCESS);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
3550Sstevel@tonic-gate 		if (simba_p == NULL)
3560Sstevel@tonic-gate 			return (DDI_FAILURE);
3570Sstevel@tonic-gate 		*result = (void *)simba_p->dip;
3580Sstevel@tonic-gate 		return (DDI_SUCCESS);
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate }
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate /*ARGSUSED*/
3630Sstevel@tonic-gate static int
simba_probe(register dev_info_t * devi)3640Sstevel@tonic-gate simba_probe(register dev_info_t *devi)
3650Sstevel@tonic-gate {
3660Sstevel@tonic-gate 	DEBUG0(D_ATTACH, "simba_probe() called.\n");
3670Sstevel@tonic-gate 	return (DDI_PROBE_SUCCESS);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate /*ARGSUSED*/
3710Sstevel@tonic-gate static int
simba_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)3720Sstevel@tonic-gate simba_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
3730Sstevel@tonic-gate {
3740Sstevel@tonic-gate 	int instance;
3750Sstevel@tonic-gate 	simba_devstate_t *simba;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	switch (cmd) {
3780Sstevel@tonic-gate 	case DDI_ATTACH:
3790Sstevel@tonic-gate 
380946Smathue 		DEBUG1(D_ATTACH, "attach(%p) ATTACH\n", devi);
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 		/*
3830Sstevel@tonic-gate 		 * Make sure the "device_type" property exists.
3840Sstevel@tonic-gate 		 */
3850Sstevel@tonic-gate 		(void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
386*7656SSherry.Moore@Sun.COM 		    "device_type", "pci");
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 		/*
3890Sstevel@tonic-gate 		 * Allocate and get soft state structure.
3900Sstevel@tonic-gate 		 */
3910Sstevel@tonic-gate 		instance = ddi_get_instance(devi);
3920Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(simba_state, instance) != DDI_SUCCESS)
3930Sstevel@tonic-gate 			return (DDI_FAILURE);
3940Sstevel@tonic-gate 		simba = (simba_devstate_t *)ddi_get_soft_state(simba_state,
3950Sstevel@tonic-gate 		    instance);
3960Sstevel@tonic-gate 		simba->dip = devi;
3970Sstevel@tonic-gate 		mutex_init(&simba->simba_mutex, NULL, MUTEX_DRIVER, NULL);
3980Sstevel@tonic-gate 		simba->simba_soft_state = SIMBA_SOFT_STATE_CLOSED;
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 		/*
4010Sstevel@tonic-gate 		 * create minor node for devctl interfaces
4020Sstevel@tonic-gate 		 */
4030Sstevel@tonic-gate 		if (ddi_create_minor_node(devi, "devctl", S_IFCHR, instance,
4040Sstevel@tonic-gate 		    DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
4050Sstevel@tonic-gate 			mutex_destroy(&simba->simba_mutex);
4060Sstevel@tonic-gate 			ddi_soft_state_free(simba_state, instance);
4070Sstevel@tonic-gate 			return (DDI_FAILURE);
4080Sstevel@tonic-gate 		}
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 		if (pci_config_setup(devi, &simba->config_handle) !=
4110Sstevel@tonic-gate 		    DDI_SUCCESS) {
4120Sstevel@tonic-gate 			ddi_remove_minor_node(devi, "devctl");
4130Sstevel@tonic-gate 			mutex_destroy(&simba->simba_mutex);
4140Sstevel@tonic-gate 			ddi_soft_state_free(simba_state, instance);
4150Sstevel@tonic-gate 			return (DDI_FAILURE);
4160Sstevel@tonic-gate 		}
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 		/*
4190Sstevel@tonic-gate 		 * Simba cache line size is 64 bytes and hardwired.
4200Sstevel@tonic-gate 		 */
4210Sstevel@tonic-gate 		simba->simba_cache_line_size =
4220Sstevel@tonic-gate 		    pci_config_get8(simba->config_handle,
423*7656SSherry.Moore@Sun.COM 		    PCI_CONF_CACHE_LINESZ);
4240Sstevel@tonic-gate 		simba->simba_latency_timer =
4250Sstevel@tonic-gate 		    pci_config_get8(simba->config_handle,
426*7656SSherry.Moore@Sun.COM 		    PCI_CONF_LATENCY_TIMER);
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 		/* simba specific, clears up the pri/sec status registers */
4290Sstevel@tonic-gate 		pci_config_put16(simba->config_handle, 0x6, 0xffff);
4300Sstevel@tonic-gate 		pci_config_put16(simba->config_handle, 0x1e, 0xffff);
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 		DEBUG2(D_ATTACH, "simba_attach(): clsz=%x, lt=%x\n",
433*7656SSherry.Moore@Sun.COM 		    simba->simba_cache_line_size,
434*7656SSherry.Moore@Sun.COM 		    simba->simba_latency_timer);
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 		/*
4370Sstevel@tonic-gate 		 * Initialize FMA support
4380Sstevel@tonic-gate 		 */
4390Sstevel@tonic-gate 		simba->fm_cap = DDI_FM_EREPORT_CAPABLE | DDI_FM_ERRCB_CAPABLE |
440*7656SSherry.Moore@Sun.COM 		    DDI_FM_ACCCHK_CAPABLE | DDI_FM_DMACHK_CAPABLE;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 		/*
4430Sstevel@tonic-gate 		 * Call parent to get it's capablity
4440Sstevel@tonic-gate 		 */
4450Sstevel@tonic-gate 		ddi_fm_init(devi, &simba->fm_cap, &simba->fm_ibc);
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 		ASSERT((simba->fm_cap & DDI_FM_ERRCB_CAPABLE) &&
4480Sstevel@tonic-gate 		    (simba->fm_cap & DDI_FM_EREPORT_CAPABLE));
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 		pci_ereport_setup(devi);
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 		ddi_fm_handler_register(devi, simba_err_callback, simba);
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 		ddi_report_dev(devi);
4550Sstevel@tonic-gate 		DEBUG0(D_ATTACH, "attach(): ATTACH done\n");
4560Sstevel@tonic-gate 		return (DDI_SUCCESS);
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	case DDI_RESUME:
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 		/*
4610Sstevel@tonic-gate 		 * Get the soft state structure for the bridge.
4620Sstevel@tonic-gate 		 */
4630Sstevel@tonic-gate 		simba = (simba_devstate_t *)
464*7656SSherry.Moore@Sun.COM 		    ddi_get_soft_state(simba_state, ddi_get_instance(devi));
4650Sstevel@tonic-gate 		simba_restore_config_regs(simba);
4660Sstevel@tonic-gate 		return (DDI_SUCCESS);
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 	return (DDI_FAILURE);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate /*ARGSUSED*/
4720Sstevel@tonic-gate static int
simba_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)4730Sstevel@tonic-gate simba_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
4740Sstevel@tonic-gate {
4750Sstevel@tonic-gate 	simba_devstate_t *simba;
4760Sstevel@tonic-gate 	simba = (simba_devstate_t *)
477*7656SSherry.Moore@Sun.COM 	    ddi_get_soft_state(simba_state, ddi_get_instance(devi));
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	switch (cmd) {
4800Sstevel@tonic-gate 	case DDI_DETACH:
4810Sstevel@tonic-gate 		DEBUG0(D_DETACH, "detach() called\n");
4820Sstevel@tonic-gate 		ddi_fm_handler_unregister(devi);
4830Sstevel@tonic-gate 		pci_ereport_teardown(devi);
4840Sstevel@tonic-gate 		ddi_fm_fini(devi);
4850Sstevel@tonic-gate 		pci_config_teardown(&simba->config_handle);
4860Sstevel@tonic-gate 		(void) ddi_prop_remove(DDI_DEV_T_NONE, devi, "device_type");
4870Sstevel@tonic-gate 		ddi_remove_minor_node(devi, "devctl");
4880Sstevel@tonic-gate 		mutex_destroy(&simba->simba_mutex);
4890Sstevel@tonic-gate 		ddi_soft_state_free(simba_state, ddi_get_instance(devi));
4900Sstevel@tonic-gate 		return (DDI_SUCCESS);
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	case DDI_SUSPEND:
4930Sstevel@tonic-gate 		simba_save_config_regs(simba);
4940Sstevel@tonic-gate 		return (DDI_SUCCESS);
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate 	return (DDI_FAILURE);
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate /*ARGSUSED*/
5000Sstevel@tonic-gate static int
simba_bus_map(dev_info_t * dip,dev_info_t * rdip,ddi_map_req_t * mp,off_t offset,off_t len,caddr_t * vaddrp)5010Sstevel@tonic-gate simba_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
5020Sstevel@tonic-gate 	off_t offset, off_t len, caddr_t *vaddrp)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	register dev_info_t *pdip;
5050Sstevel@tonic-gate 
506946Smathue 	DEBUG3(D_MAP, "simba_bus_map(): dip=%p, rdip=%p, mp=%p", dip, rdip, mp);
507946Smathue 	DEBUG3(D_MAP, "simba_bus_map(): offset=%lx, len=%lx, vaddrp=%p",
5080Sstevel@tonic-gate 	    offset, len, vaddrp);
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	pdip = (dev_info_t *)DEVI(dip)->devi_parent;
5110Sstevel@tonic-gate 	return ((DEVI(pdip)->devi_ops->devo_bus_ops->bus_map)
5120Sstevel@tonic-gate 	    (pdip, rdip, mp, offset, len, vaddrp));
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate /*
5160Sstevel@tonic-gate  * Registered error handling callback with our parent
5170Sstevel@tonic-gate  */
5180Sstevel@tonic-gate static int
simba_err_callback(dev_info_t * dip,ddi_fm_error_t * derr,const void * impl_data)5190Sstevel@tonic-gate simba_err_callback(dev_info_t *dip, ddi_fm_error_t *derr, const void *impl_data)
5200Sstevel@tonic-gate {
5210Sstevel@tonic-gate 	simba_devstate_t *simba = (simba_devstate_t *)impl_data;
5220Sstevel@tonic-gate 	struct simba_errstate simba_err;
5230Sstevel@tonic-gate 	int ret = 0;
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	bzero(&simba_err, sizeof (struct simba_errstate));
5260Sstevel@tonic-gate 	simba_err.afsr = pci_config_get64(simba->config_handle, 0xe8);
5270Sstevel@tonic-gate 	simba_err.afar = pci_config_get64(simba->config_handle, 0xf0);
5280Sstevel@tonic-gate 	derr->fme_ena = fm_ena_generate(0, FM_ENA_FMT1);
5290Sstevel@tonic-gate 
5301865Sdilpreet 	pci_ereport_post(dip, derr, NULL);
5311865Sdilpreet 	ret = derr->fme_status;
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	DEBUG6(D_FAULT, "%s-%d: cleaning up fault bits %x %x %x.%8x\n",
5341865Sdilpreet 	    ddi_driver_name(simba->dip), ddi_get_instance(simba->dip),
5351865Sdilpreet 	    simba_err.pci_cfg_stat, simba_err.pci_cfg_sec_stat,
5361865Sdilpreet 	    (uint_t)(simba_err.afsr >> 32), (uint_t)simba_err.afsr);
5370Sstevel@tonic-gate 	pci_config_put64(simba->config_handle, 0xe8, simba_err.afsr);
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	return (ret);
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate #if defined(DEBUG) && !defined(lint)
5430Sstevel@tonic-gate static char *ops[] =
5440Sstevel@tonic-gate {
5450Sstevel@tonic-gate 	"DDI_CTLOPS_DMAPMAPC",
5460Sstevel@tonic-gate 	"DDI_CTLOPS_INITCHILD",
5470Sstevel@tonic-gate 	"DDI_CTLOPS_UNINITCHILD",
5480Sstevel@tonic-gate 	"DDI_CTLOPS_REPORTDEV",
5490Sstevel@tonic-gate 	"DDI_CTLOPS_REPORTINT",
5500Sstevel@tonic-gate 	"DDI_CTLOPS_REGSIZE",
5510Sstevel@tonic-gate 	"DDI_CTLOPS_NREGS",
552693Sgovinda 	"DDI_CTLOPS_RESERVED0",
5530Sstevel@tonic-gate 	"DDI_CTLOPS_SIDDEV",
5540Sstevel@tonic-gate 	"DDI_CTLOPS_SLAVEONLY",
5550Sstevel@tonic-gate 	"DDI_CTLOPS_AFFINITY",
5560Sstevel@tonic-gate 	"DDI_CTLOPS_IOMIN",
5570Sstevel@tonic-gate 	"DDI_CTLOPS_PTOB",
5580Sstevel@tonic-gate 	"DDI_CTLOPS_BTOP",
5590Sstevel@tonic-gate 	"DDI_CTLOPS_BTOPR",
5600Sstevel@tonic-gate 	"DDI_CTLOPS_RESERVED1",
5610Sstevel@tonic-gate 	"DDI_CTLOPS_RESERVED2",
5620Sstevel@tonic-gate 	"DDI_CTLOPS_RESERVED3",
563693Sgovinda 	"DDI_CTLOPS_RESERVED4",
564693Sgovinda 	"DDI_CTLOPS_RESERVED5",
5650Sstevel@tonic-gate 	"DDI_CTLOPS_DVMAPAGESIZE",
5660Sstevel@tonic-gate 	"DDI_CTLOPS_POWER",
5670Sstevel@tonic-gate 	"DDI_CTLOPS_ATTACH",
5680Sstevel@tonic-gate 	"DDI_CTLOPS_DETACH",
5690Sstevel@tonic-gate 	"DDI_CTLOPS_POKE",
5700Sstevel@tonic-gate 	"DDI_CTLOPS_PEEK"
5710Sstevel@tonic-gate };
5720Sstevel@tonic-gate #endif
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate /*ARGSUSED*/
5750Sstevel@tonic-gate static int
simba_ctlops(dev_info_t * dip,dev_info_t * rdip,ddi_ctl_enum_t ctlop,void * arg,void * result)5760Sstevel@tonic-gate simba_ctlops(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t ctlop,
5770Sstevel@tonic-gate 	void *arg, void *result)
5780Sstevel@tonic-gate {
5790Sstevel@tonic-gate 	int reglen;
5800Sstevel@tonic-gate 	int rn;
5810Sstevel@tonic-gate 	int totreg;
5820Sstevel@tonic-gate 	pci_regspec_t *drv_regp;
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	DEBUG6(D_CTLOPS,
585*7656SSherry.Moore@Sun.COM 	    "simba_ctlops(): dip=%p rdip=%p ctlop=%x-%s arg=%p result=%p",
586*7656SSherry.Moore@Sun.COM 	    dip, rdip, ctlop, ctlop < (sizeof (ops) / sizeof (ops[0])) ?
587*7656SSherry.Moore@Sun.COM 	    ops[ctlop] : "Unknown", arg, result);
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	switch (ctlop) {
5900Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV:
5910Sstevel@tonic-gate 		if (rdip == (dev_info_t *)0)
5920Sstevel@tonic-gate 			return (DDI_FAILURE);
5930Sstevel@tonic-gate 		cmn_err(CE_CONT, "?PCI-device: %s@%s, %s%d\n",
5940Sstevel@tonic-gate 		    ddi_node_name(rdip), ddi_get_name_addr(rdip),
5950Sstevel@tonic-gate 		    ddi_driver_name(rdip),
5960Sstevel@tonic-gate 		    ddi_get_instance(rdip));
5970Sstevel@tonic-gate 		return (DDI_SUCCESS);
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD:
6000Sstevel@tonic-gate 		return (simba_initchild((dev_info_t *)arg));
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD:
6030Sstevel@tonic-gate 		simba_uninitchild((dev_info_t *)arg);
6040Sstevel@tonic-gate 		return (DDI_SUCCESS);
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	case DDI_CTLOPS_SIDDEV:
6070Sstevel@tonic-gate 		return (DDI_SUCCESS);
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	case DDI_CTLOPS_REGSIZE:
6100Sstevel@tonic-gate 	case DDI_CTLOPS_NREGS:
6110Sstevel@tonic-gate 		if (rdip == (dev_info_t *)0)
6120Sstevel@tonic-gate 			return (DDI_FAILURE);
6130Sstevel@tonic-gate 		break;
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	default:
6160Sstevel@tonic-gate 		DEBUG0(D_CTLOPS, "simba_ctlops(): calling ddi_ctlops()");
6170Sstevel@tonic-gate 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
6180Sstevel@tonic-gate 	}
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	*(int *)result = 0;
621506Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, rdip,
622*7656SSherry.Moore@Sun.COM 	    DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP, "reg",
623*7656SSherry.Moore@Sun.COM 	    (caddr_t)&drv_regp, &reglen) != DDI_SUCCESS)
6240Sstevel@tonic-gate 		return (DDI_FAILURE);
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	totreg = reglen / sizeof (pci_regspec_t);
6270Sstevel@tonic-gate 	if (ctlop == DDI_CTLOPS_NREGS)
6280Sstevel@tonic-gate 		*(int *)result = totreg;
6290Sstevel@tonic-gate 	else if (ctlop == DDI_CTLOPS_REGSIZE) {
6300Sstevel@tonic-gate 		rn = *(int *)arg;
6310Sstevel@tonic-gate 		if (rn >= totreg) {
6320Sstevel@tonic-gate 			kmem_free(drv_regp, reglen);
6330Sstevel@tonic-gate 			return (DDI_FAILURE);
6340Sstevel@tonic-gate 		}
6350Sstevel@tonic-gate 		*(off_t *)result = drv_regp[rn].pci_size_low |
636*7656SSherry.Moore@Sun.COM 		    ((uint64_t)drv_regp[rn].pci_size_hi << 32);
6370Sstevel@tonic-gate 	}
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	kmem_free(drv_regp, reglen);
640946Smathue 	DEBUG1(D_CTLOPS, "simba_ctlops(): *result=%lx\n", *(off_t *)result);
6410Sstevel@tonic-gate 	return (DDI_SUCCESS);
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate static int
simba_name_child(dev_info_t * child,char * name,int namelen)6450Sstevel@tonic-gate simba_name_child(dev_info_t *child, char *name, int namelen)
6460Sstevel@tonic-gate {
6470Sstevel@tonic-gate 	uint_t n, slot, func;
6480Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(child) == 0) {
6510Sstevel@tonic-gate 		char **unit_addr;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 		/* name .conf nodes by "unit-address" property" */
6540Sstevel@tonic-gate 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, child,
6550Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "unit-address", &unit_addr, &n) !=
6560Sstevel@tonic-gate 		    DDI_PROP_SUCCESS) {
6570Sstevel@tonic-gate 			cmn_err(CE_WARN, "cannot name node from %s.conf",
6580Sstevel@tonic-gate 			    ddi_driver_name(child));
6590Sstevel@tonic-gate 			return (DDI_FAILURE);
6600Sstevel@tonic-gate 		}
6610Sstevel@tonic-gate 		if (n != 1 || *unit_addr == NULL || **unit_addr == 0) {
6620Sstevel@tonic-gate 			cmn_err(CE_WARN, "unit-address property in %s.conf"
6630Sstevel@tonic-gate 			    " not well-formed", ddi_driver_name(child));
6640Sstevel@tonic-gate 			ddi_prop_free(unit_addr);
6650Sstevel@tonic-gate 			return (DDI_FAILURE);
6660Sstevel@tonic-gate 		}
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%s", *unit_addr);
6690Sstevel@tonic-gate 		ddi_prop_free(unit_addr);
6700Sstevel@tonic-gate 		return (DDI_SUCCESS);
6710Sstevel@tonic-gate 	}
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	/* name hardware nodes by "reg" property */
6740Sstevel@tonic-gate 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child, 0, "reg",
6750Sstevel@tonic-gate 	    (int **)&pci_rp, &n) != DDI_SUCCESS)
6760Sstevel@tonic-gate 		return (DDI_FAILURE);
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	/* get the device identifications */
6790Sstevel@tonic-gate 	slot = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
6800Sstevel@tonic-gate 	func = PCI_REG_FUNC_G(pci_rp->pci_phys_hi);
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	if (func != 0)
6830Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%x,%x", slot, func);
6840Sstevel@tonic-gate 	else
6850Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%x", slot);
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	ddi_prop_free(pci_rp);
6880Sstevel@tonic-gate 	return (DDI_SUCCESS);
6890Sstevel@tonic-gate }
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate static int
simba_initchild(dev_info_t * child)6920Sstevel@tonic-gate simba_initchild(dev_info_t *child)
6930Sstevel@tonic-gate {
6940Sstevel@tonic-gate 	char name[MAXNAMELEN];
6950Sstevel@tonic-gate 	int i;
6960Sstevel@tonic-gate 	ddi_acc_handle_t config_handle;
6970Sstevel@tonic-gate 	ushort_t command_preserve, command;
6980Sstevel@tonic-gate 	uchar_t header_type;
6990Sstevel@tonic-gate 	uchar_t min_gnt, latency_timer;
7000Sstevel@tonic-gate 	simba_devstate_t *simba;
7010Sstevel@tonic-gate 	uint_t n;
7020Sstevel@tonic-gate 
703946Smathue 	DEBUG1(D_INIT_CLD, "simba_initchild(): child=%p\n", child);
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	/*
7060Sstevel@tonic-gate 	 * Pseudo nodes indicate a prototype node with per-instance
7070Sstevel@tonic-gate 	 * properties to be merged into the real h/w device node.
7080Sstevel@tonic-gate 	 * The interpretation of the unit-address is DD[,F]
7090Sstevel@tonic-gate 	 * where DD is the device id and F is the function.
7100Sstevel@tonic-gate 	 */
7110Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(child) == 0) {
7120Sstevel@tonic-gate 		extern int pci_allow_pseudo_children;
7130Sstevel@tonic-gate 		pci_regspec_t *pci_rp;
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 		if (ddi_getlongprop(DDI_DEV_T_ANY, child,
7160Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "reg", (caddr_t)&pci_rp, &i) ==
7170Sstevel@tonic-gate 		    DDI_SUCCESS) {
7180Sstevel@tonic-gate 			cmn_err(CE_WARN,
7190Sstevel@tonic-gate 			    "cannot merge prototype from %s.conf",
7200Sstevel@tonic-gate 			    ddi_driver_name(child));
7210Sstevel@tonic-gate 			kmem_free(pci_rp, i);
7220Sstevel@tonic-gate 			return (DDI_NOT_WELL_FORMED);
7230Sstevel@tonic-gate 		}
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 		if (simba_name_child(child, name, MAXNAMELEN) != DDI_SUCCESS)
7260Sstevel@tonic-gate 			return (DDI_NOT_WELL_FORMED);
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 		ddi_set_name_addr(child, name);
7290Sstevel@tonic-gate 		ddi_set_parent_data(child, NULL);
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 		/*
7320Sstevel@tonic-gate 		 * Try to merge the properties from this prototype
7330Sstevel@tonic-gate 		 * node into real h/w nodes.
7340Sstevel@tonic-gate 		 */
7350Sstevel@tonic-gate 		if (ndi_merge_node(child, simba_name_child) == DDI_SUCCESS) {
7360Sstevel@tonic-gate 			/*
7370Sstevel@tonic-gate 			 * Merged ok - return failure to remove the node.
7380Sstevel@tonic-gate 			 */
7390Sstevel@tonic-gate 			simba_uninitchild(child);
7400Sstevel@tonic-gate 			return (DDI_FAILURE);
7410Sstevel@tonic-gate 		}
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 		/* workaround for ddivs to run under PCI */
7440Sstevel@tonic-gate 		if (pci_allow_pseudo_children)
7450Sstevel@tonic-gate 			return (DDI_SUCCESS);
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 		/*
7480Sstevel@tonic-gate 		 * The child was not merged into a h/w node,
7490Sstevel@tonic-gate 		 * but there's not much we can do with it other
7500Sstevel@tonic-gate 		 * than return failure to cause the node to be removed.
7510Sstevel@tonic-gate 		 */
7520Sstevel@tonic-gate 		cmn_err(CE_WARN, "!%s@%s: %s.conf properties not merged",
7530Sstevel@tonic-gate 		    ddi_driver_name(child), ddi_get_name_addr(child),
7540Sstevel@tonic-gate 		    ddi_driver_name(child));
7550Sstevel@tonic-gate 		simba_uninitchild(child);
7560Sstevel@tonic-gate 		return (DDI_NOT_WELL_FORMED);
7570Sstevel@tonic-gate 	}
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	/*
7600Sstevel@tonic-gate 	 * Initialize real h/w nodes
7610Sstevel@tonic-gate 	 */
7620Sstevel@tonic-gate 	if (simba_name_child(child, name, MAXNAMELEN) != DDI_SUCCESS)
7630Sstevel@tonic-gate 		return (DDI_FAILURE);
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	ddi_set_name_addr(child, name);
7660Sstevel@tonic-gate 	ddi_set_parent_data(child, NULL);
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	if (pci_config_setup(child, &config_handle) != DDI_SUCCESS) {
7690Sstevel@tonic-gate 		simba_uninitchild(child);
7700Sstevel@tonic-gate 		return (DDI_FAILURE);
7710Sstevel@tonic-gate 	}
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	DEBUG0(D_INIT_CLD, "simba_initchild(): pci_config_setup success!\n");
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	/*
7760Sstevel@tonic-gate 	 * Determine the configuration header type.
7770Sstevel@tonic-gate 	 */
7780Sstevel@tonic-gate 	header_type = pci_config_get8(config_handle, PCI_CONF_HEADER);
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	/*
7810Sstevel@tonic-gate 	 * Support for the "command-preserve" property.
7820Sstevel@tonic-gate 	 */
7830Sstevel@tonic-gate 	command_preserve = ddi_prop_get_int(DDI_DEV_T_ANY, child,
7840Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "command-preserve", 0);
7850Sstevel@tonic-gate 	command = pci_config_get16(config_handle, PCI_CONF_COMM);
7860Sstevel@tonic-gate 	command &= (command_preserve | PCI_COMM_BACK2BACK_ENAB);
7870Sstevel@tonic-gate 	command |= (simba_command_default & ~command_preserve);
7880Sstevel@tonic-gate 	pci_config_put16(config_handle, PCI_CONF_COMM, command);
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	/* clean up all PCI child devices status register */
7910Sstevel@tonic-gate 	pci_config_put16(config_handle, PCI_CONF_STAT, 0xffff);
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	/*
7940Sstevel@tonic-gate 	 * If the device has a primary bus control register then program it
7950Sstevel@tonic-gate 	 * based on the settings in the command register.
7960Sstevel@tonic-gate 	 */
7970Sstevel@tonic-gate 	if ((header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE) {
7980Sstevel@tonic-gate 		ushort_t bcr =
7990Sstevel@tonic-gate 		    pci_config_get16(config_handle, PCI_BCNF_BCNTRL);
8000Sstevel@tonic-gate 		if (simba_command_default & PCI_COMM_PARITY_DETECT)
8010Sstevel@tonic-gate 			bcr |= PCI_BCNF_BCNTRL_PARITY_ENABLE;
8020Sstevel@tonic-gate 		if (simba_command_default & PCI_COMM_SERR_ENABLE)
8030Sstevel@tonic-gate 			bcr |= PCI_BCNF_BCNTRL_SERR_ENABLE;
8040Sstevel@tonic-gate 		bcr |= PCI_BCNF_BCNTRL_MAST_AB_MODE;
8050Sstevel@tonic-gate 		pci_config_put8(config_handle, PCI_BCNF_BCNTRL, bcr);
8060Sstevel@tonic-gate 	}
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	simba = (simba_devstate_t *)ddi_get_soft_state(simba_state,
809*7656SSherry.Moore@Sun.COM 	    ddi_get_instance(ddi_get_parent(child)));
8100Sstevel@tonic-gate 	/*
8110Sstevel@tonic-gate 	 * Initialize cache-line-size configuration register if needed.
8120Sstevel@tonic-gate 	 */
8130Sstevel@tonic-gate 	if (simba_set_cache_line_size_register &&
8140Sstevel@tonic-gate 	    ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
815*7656SSherry.Moore@Sun.COM 	    "cache-line-size", 0) == 0) {
8160Sstevel@tonic-gate 		pci_config_put8(config_handle, PCI_CONF_CACHE_LINESZ,
8170Sstevel@tonic-gate 		    simba->simba_cache_line_size);
8180Sstevel@tonic-gate 		n = pci_config_get8(config_handle, PCI_CONF_CACHE_LINESZ);
8190Sstevel@tonic-gate 		if (n != 0)
8200Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, child,
821*7656SSherry.Moore@Sun.COM 			    "cache-line-size", n);
8220Sstevel@tonic-gate 	}
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	/*
8250Sstevel@tonic-gate 	 * Initialize latency timer configuration registers if needed.
8260Sstevel@tonic-gate 	 */
8270Sstevel@tonic-gate 	if (simba_set_latency_timer_register &&
8280Sstevel@tonic-gate 	    ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
829*7656SSherry.Moore@Sun.COM 	    "latency-timer", 0) == 0) {
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 		if ((header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE) {
8320Sstevel@tonic-gate 			latency_timer = simba->simba_latency_timer;
8330Sstevel@tonic-gate 			pci_config_put8(config_handle, PCI_BCNF_LATENCY_TIMER,
8340Sstevel@tonic-gate 			    simba->simba_latency_timer);
8350Sstevel@tonic-gate 		} else {
8360Sstevel@tonic-gate 			min_gnt = pci_config_get8(config_handle,
8370Sstevel@tonic-gate 			    PCI_CONF_MIN_G);
8380Sstevel@tonic-gate 			latency_timer = min_gnt * 8;
8390Sstevel@tonic-gate 		}
8400Sstevel@tonic-gate 		pci_config_put8(config_handle, PCI_CONF_LATENCY_TIMER,
8410Sstevel@tonic-gate 		    latency_timer);
8420Sstevel@tonic-gate 		n = pci_config_get8(config_handle, PCI_CONF_LATENCY_TIMER);
8430Sstevel@tonic-gate 		if (n != 0)
8440Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, child,
8450Sstevel@tonic-gate 			    "latency-timer", n);
8460Sstevel@tonic-gate 	}
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	pci_config_teardown(&config_handle);
8490Sstevel@tonic-gate 	DEBUG0(D_INIT_CLD, "simba_initchild(): pci_config_teardown called\n");
8500Sstevel@tonic-gate 	return (DDI_SUCCESS);
8510Sstevel@tonic-gate }
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate static void
simba_uninitchild(dev_info_t * dip)8540Sstevel@tonic-gate simba_uninitchild(dev_info_t *dip)
8550Sstevel@tonic-gate {
8560Sstevel@tonic-gate 	ddi_set_name_addr(dip, NULL);
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	/*
8590Sstevel@tonic-gate 	 * Strip the node to properly convert it back to prototype form
8600Sstevel@tonic-gate 	 */
8610Sstevel@tonic-gate 	impl_rem_dev_props(dip);
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate /*
8650Sstevel@tonic-gate  * simba_save_config_regs
8660Sstevel@tonic-gate  *
8670Sstevel@tonic-gate  * This routine saves the state of the configuration registers of all
8680Sstevel@tonic-gate  * the child nodes of each PBM.
8690Sstevel@tonic-gate  *
8700Sstevel@tonic-gate  * used by: simba_detach() on suspends
8710Sstevel@tonic-gate  *
8720Sstevel@tonic-gate  * return value: none
8730Sstevel@tonic-gate  */
8740Sstevel@tonic-gate static void
simba_save_config_regs(simba_devstate_t * simba_p)8750Sstevel@tonic-gate simba_save_config_regs(simba_devstate_t *simba_p)
8760Sstevel@tonic-gate {
8770Sstevel@tonic-gate 	int i;
8780Sstevel@tonic-gate 	dev_info_t *dip;
8790Sstevel@tonic-gate 	ddi_acc_handle_t ch;
8800Sstevel@tonic-gate 	struct simba_cfg_state *statep;
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	for (i = 0, dip = ddi_get_child(simba_p->dip); dip != NULL;
883*7656SSherry.Moore@Sun.COM 	    dip = ddi_get_next_sibling(dip)) {
8841333Scth 		if (i_ddi_devi_attached(dip))
8850Sstevel@tonic-gate 			i++;
8860Sstevel@tonic-gate 	}
8870Sstevel@tonic-gate 	if (!i)
8880Sstevel@tonic-gate 		return;
8890Sstevel@tonic-gate 	simba_p->simba_config_state_p =
890*7656SSherry.Moore@Sun.COM 	    kmem_zalloc(i * sizeof (struct simba_cfg_state), KM_NOSLEEP);
8910Sstevel@tonic-gate 	if (!simba_p->simba_config_state_p) {
8920Sstevel@tonic-gate 		cmn_err(CE_WARN, "not enough memrory to save simba child\n");
8930Sstevel@tonic-gate 		return;
8940Sstevel@tonic-gate 	}
8950Sstevel@tonic-gate 	simba_p->config_state_index = i;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	for (statep = simba_p->simba_config_state_p,
898*7656SSherry.Moore@Sun.COM 	    dip = ddi_get_child(simba_p->dip);
899*7656SSherry.Moore@Sun.COM 	    dip != NULL;
900*7656SSherry.Moore@Sun.COM 	    dip = ddi_get_next_sibling(dip)) {
9010Sstevel@tonic-gate 
9021333Scth 		if (!i_ddi_devi_attached(dip)) {
9030Sstevel@tonic-gate 			DEBUG4(D_DETACH, "%s%d: skipping unattached %s%d\n",
904*7656SSherry.Moore@Sun.COM 			    ddi_driver_name(simba_p->dip),
905*7656SSherry.Moore@Sun.COM 			    ddi_get_instance(simba_p->dip),
906*7656SSherry.Moore@Sun.COM 			    ddi_driver_name(dip),
907*7656SSherry.Moore@Sun.COM 			    ddi_get_instance(dip));
9080Sstevel@tonic-gate 			continue;
9090Sstevel@tonic-gate 		}
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 		DEBUG4(D_DETACH, "%s%d: saving regs for %s%d\n",
912*7656SSherry.Moore@Sun.COM 		    ddi_driver_name(simba_p->dip),
913*7656SSherry.Moore@Sun.COM 		    ddi_get_instance(simba_p->dip),
914*7656SSherry.Moore@Sun.COM 		    ddi_driver_name(dip),
915*7656SSherry.Moore@Sun.COM 		    ddi_get_instance(dip));
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 		if (pci_config_setup(dip, &ch) != DDI_SUCCESS) {
9180Sstevel@tonic-gate 			DEBUG4(D_DETACH, "%s%d: can't config space for %s%d\n",
919*7656SSherry.Moore@Sun.COM 			    ddi_driver_name(simba_p->dip),
920*7656SSherry.Moore@Sun.COM 			    ddi_get_instance(simba_p->dip),
921*7656SSherry.Moore@Sun.COM 			    ddi_driver_name(dip),
922*7656SSherry.Moore@Sun.COM 			    ddi_get_instance(dip));
9230Sstevel@tonic-gate 			continue;
9240Sstevel@tonic-gate 		}
9250Sstevel@tonic-gate 
926946Smathue 		DEBUG3(D_DETACH, "%s%d: saving child dip=%p\n",
927*7656SSherry.Moore@Sun.COM 		    ddi_driver_name(simba_p->dip),
928*7656SSherry.Moore@Sun.COM 		    ddi_get_instance(simba_p->dip),
929*7656SSherry.Moore@Sun.COM 		    dip);
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 		statep->dip = dip;
9320Sstevel@tonic-gate 		statep->command = pci_config_get16(ch, PCI_CONF_COMM);
9330Sstevel@tonic-gate 		statep->header_type = pci_config_get8(ch, PCI_CONF_HEADER);
9340Sstevel@tonic-gate 		if ((statep->header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE)
9350Sstevel@tonic-gate 			statep->bridge_control =
9360Sstevel@tonic-gate 			    pci_config_get16(ch, PCI_BCNF_BCNTRL);
9370Sstevel@tonic-gate 		statep->cache_line_size =
938*7656SSherry.Moore@Sun.COM 		    pci_config_get8(ch, PCI_CONF_CACHE_LINESZ);
9390Sstevel@tonic-gate 		statep->latency_timer =
940*7656SSherry.Moore@Sun.COM 		    pci_config_get8(ch, PCI_CONF_LATENCY_TIMER);
9410Sstevel@tonic-gate 		if ((statep->header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE)
9420Sstevel@tonic-gate 			statep->sec_latency_timer =
943*7656SSherry.Moore@Sun.COM 			    pci_config_get8(ch, PCI_BCNF_LATENCY_TIMER);
9440Sstevel@tonic-gate 		/*
9450Sstevel@tonic-gate 		 * Simba specific.
9460Sstevel@tonic-gate 		 */
9470Sstevel@tonic-gate 		if (pci_config_get16(ch, PCI_CONF_VENID) == PCI_SIMBA_VENID &&
9480Sstevel@tonic-gate 		    pci_config_get16(ch, PCI_CONF_DEVID) == PCI_SIMBA_DEVID) {
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 			statep->bus_number =
951*7656SSherry.Moore@Sun.COM 			    pci_config_get8(ch, PCI_BCNF_PRIBUS);
9520Sstevel@tonic-gate 			statep->sec_bus_number =
953*7656SSherry.Moore@Sun.COM 			    pci_config_get8(ch, PCI_BCNF_SECBUS);
9540Sstevel@tonic-gate 			statep->sub_bus_number =
955*7656SSherry.Moore@Sun.COM 			    pci_config_get8(ch, PCI_BCNF_SUBBUS);
9560Sstevel@tonic-gate 			statep->bridge_control =
957*7656SSherry.Moore@Sun.COM 			    pci_config_get16(ch, PCI_BCNF_BCNTRL);
9580Sstevel@tonic-gate 		}
9590Sstevel@tonic-gate 		pci_config_teardown(&ch);
9600Sstevel@tonic-gate 		statep++;
9610Sstevel@tonic-gate 	}
9620Sstevel@tonic-gate }
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate /*
9660Sstevel@tonic-gate  * simba_restore_config_regs
9670Sstevel@tonic-gate  *
9680Sstevel@tonic-gate  * This routine restores the state of the configuration registers of all
9690Sstevel@tonic-gate  * the child nodes of each PBM.
9700Sstevel@tonic-gate  *
9710Sstevel@tonic-gate  * used by: simba_attach() on resume
9720Sstevel@tonic-gate  *
9730Sstevel@tonic-gate  * return value: none
9740Sstevel@tonic-gate  */
9750Sstevel@tonic-gate static void
simba_restore_config_regs(simba_devstate_t * simba_p)9760Sstevel@tonic-gate simba_restore_config_regs(simba_devstate_t *simba_p)
9770Sstevel@tonic-gate {
9780Sstevel@tonic-gate 	int i;
9790Sstevel@tonic-gate 	dev_info_t *dip;
9800Sstevel@tonic-gate 	ddi_acc_handle_t ch;
9810Sstevel@tonic-gate 	struct simba_cfg_state *statep = simba_p->simba_config_state_p;
9820Sstevel@tonic-gate 	if (!simba_p->config_state_index)
9830Sstevel@tonic-gate 		return;
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	for (i = 0; i < simba_p->config_state_index; i++, statep++) {
9860Sstevel@tonic-gate 		dip = statep->dip;
9870Sstevel@tonic-gate 		if (!dip) {
9880Sstevel@tonic-gate 			cmn_err(CE_WARN,
989*7656SSherry.Moore@Sun.COM 			    "%s%d: skipping bad dev info (%d)\n",
990*7656SSherry.Moore@Sun.COM 			    ddi_driver_name(simba_p->dip),
991*7656SSherry.Moore@Sun.COM 			    ddi_get_instance(simba_p->dip),
992*7656SSherry.Moore@Sun.COM 			    i);
9930Sstevel@tonic-gate 			continue;
9940Sstevel@tonic-gate 		}
9950Sstevel@tonic-gate 
996946Smathue 		DEBUG5(D_ATTACH, "%s%d: restoring regs for %p-%s%d\n",
997*7656SSherry.Moore@Sun.COM 		    ddi_driver_name(simba_p->dip),
998*7656SSherry.Moore@Sun.COM 		    ddi_get_instance(simba_p->dip),
999*7656SSherry.Moore@Sun.COM 		    dip,
1000*7656SSherry.Moore@Sun.COM 		    ddi_driver_name(dip),
1001*7656SSherry.Moore@Sun.COM 		    ddi_get_instance(dip));
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 		if (pci_config_setup(dip, &ch) != DDI_SUCCESS) {
10040Sstevel@tonic-gate 			DEBUG4(D_ATTACH, "%s%d: can't config space for %s%d\n",
1005*7656SSherry.Moore@Sun.COM 			    ddi_driver_name(simba_p->dip),
1006*7656SSherry.Moore@Sun.COM 			    ddi_get_instance(simba_p->dip),
1007*7656SSherry.Moore@Sun.COM 			    ddi_driver_name(dip),
1008*7656SSherry.Moore@Sun.COM 			    ddi_get_instance(dip));
10090Sstevel@tonic-gate 			continue;
10100Sstevel@tonic-gate 		}
10110Sstevel@tonic-gate 		pci_config_put16(ch, PCI_CONF_COMM, statep->command);
10120Sstevel@tonic-gate 		if ((statep->header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE)
10130Sstevel@tonic-gate 			pci_config_put16(ch, PCI_BCNF_BCNTRL,
1014*7656SSherry.Moore@Sun.COM 			    statep->bridge_control);
10150Sstevel@tonic-gate 		/*
10160Sstevel@tonic-gate 		 * Simba specific.
10170Sstevel@tonic-gate 		 */
10180Sstevel@tonic-gate 		if (pci_config_get16(ch, PCI_CONF_VENID) == PCI_SIMBA_VENID &&
10190Sstevel@tonic-gate 		    pci_config_get16(ch, PCI_CONF_DEVID) == PCI_SIMBA_DEVID) {
10200Sstevel@tonic-gate 			pci_config_put8(ch, PCI_BCNF_PRIBUS,
1021*7656SSherry.Moore@Sun.COM 			    statep->bus_number);
10220Sstevel@tonic-gate 			pci_config_put8(ch, PCI_BCNF_SECBUS,
1023*7656SSherry.Moore@Sun.COM 			    statep->sec_bus_number);
10240Sstevel@tonic-gate 			pci_config_put8(ch, PCI_BCNF_SUBBUS,
1025*7656SSherry.Moore@Sun.COM 			    statep->sub_bus_number);
10260Sstevel@tonic-gate 			pci_config_put16(ch, PCI_BCNF_BCNTRL,
1027*7656SSherry.Moore@Sun.COM 			    statep->bridge_control);
10280Sstevel@tonic-gate 		}
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 		pci_config_put8(ch, PCI_CONF_CACHE_LINESZ,
1031*7656SSherry.Moore@Sun.COM 		    statep->cache_line_size);
10320Sstevel@tonic-gate 		pci_config_put8(ch, PCI_CONF_LATENCY_TIMER,
1033*7656SSherry.Moore@Sun.COM 		    statep->latency_timer);
10340Sstevel@tonic-gate 		if ((statep->header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE)
10350Sstevel@tonic-gate 			pci_config_put8(ch, PCI_BCNF_LATENCY_TIMER,
1036*7656SSherry.Moore@Sun.COM 			    statep->sec_latency_timer);
10370Sstevel@tonic-gate 		pci_config_teardown(&ch);
10380Sstevel@tonic-gate 	}
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	kmem_free(simba_p->simba_config_state_p,
1041*7656SSherry.Moore@Sun.COM 	    simba_p->config_state_index * sizeof (struct simba_cfg_state));
10420Sstevel@tonic-gate 	simba_p->simba_config_state_p = NULL;
10430Sstevel@tonic-gate 	simba_p->config_state_index = 0;
10440Sstevel@tonic-gate }
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate /* ARGSUSED */
10470Sstevel@tonic-gate static int
simba_open(dev_t * devp,int flags,int otyp,cred_t * credp)10480Sstevel@tonic-gate simba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
10490Sstevel@tonic-gate {
10500Sstevel@tonic-gate 	simba_devstate_t *simba_p;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	/*
10530Sstevel@tonic-gate 	 * Make sure the open is for the right file type.
10540Sstevel@tonic-gate 	 */
10550Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
10560Sstevel@tonic-gate 		return (EINVAL);
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	/*
10590Sstevel@tonic-gate 	 * Get the soft state structure for the device.
10600Sstevel@tonic-gate 	 */
10610Sstevel@tonic-gate 	simba_p = (simba_devstate_t *)ddi_get_soft_state(simba_state,
10620Sstevel@tonic-gate 	    getminor(*devp));
10630Sstevel@tonic-gate 	if (simba_p == NULL)
10640Sstevel@tonic-gate 		return (ENXIO);
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	/*
10670Sstevel@tonic-gate 	 * Handle the open by tracking the device state.
10680Sstevel@tonic-gate 	 */
10690Sstevel@tonic-gate 	mutex_enter(&simba_p->simba_mutex);
10700Sstevel@tonic-gate 	if (flags & FEXCL) {
10710Sstevel@tonic-gate 		if (simba_p->simba_soft_state != SIMBA_SOFT_STATE_CLOSED) {
10720Sstevel@tonic-gate 			mutex_exit(&simba_p->simba_mutex);
10730Sstevel@tonic-gate 			return (EBUSY);
10740Sstevel@tonic-gate 		}
10750Sstevel@tonic-gate 		simba_p->simba_soft_state = SIMBA_SOFT_STATE_OPEN_EXCL;
10760Sstevel@tonic-gate 	} else {
10770Sstevel@tonic-gate 		if (simba_p->simba_soft_state == SIMBA_SOFT_STATE_OPEN_EXCL) {
10780Sstevel@tonic-gate 			mutex_exit(&simba_p->simba_mutex);
10790Sstevel@tonic-gate 			return (EBUSY);
10800Sstevel@tonic-gate 		}
10810Sstevel@tonic-gate 		simba_p->simba_soft_state = SIMBA_SOFT_STATE_OPEN;
10820Sstevel@tonic-gate 	}
10830Sstevel@tonic-gate 	mutex_exit(&simba_p->simba_mutex);
10840Sstevel@tonic-gate 	return (0);
10850Sstevel@tonic-gate }
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate /* ARGSUSED */
10890Sstevel@tonic-gate static int
simba_close(dev_t dev,int flags,int otyp,cred_t * credp)10900Sstevel@tonic-gate simba_close(dev_t dev, int flags, int otyp, cred_t *credp)
10910Sstevel@tonic-gate {
10920Sstevel@tonic-gate 	simba_devstate_t *simba_p;
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
10950Sstevel@tonic-gate 		return (EINVAL);
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	simba_p = (simba_devstate_t *)ddi_get_soft_state(simba_state,
10980Sstevel@tonic-gate 	    getminor(dev));
10990Sstevel@tonic-gate 	if (simba_p == NULL)
11000Sstevel@tonic-gate 		return (ENXIO);
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	mutex_enter(&simba_p->simba_mutex);
11030Sstevel@tonic-gate 	simba_p->simba_soft_state = SIMBA_SOFT_STATE_CLOSED;
11040Sstevel@tonic-gate 	mutex_exit(&simba_p->simba_mutex);
11050Sstevel@tonic-gate 	return (0);
11060Sstevel@tonic-gate }
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate /*
11100Sstevel@tonic-gate  * simba_ioctl: devctl hotplug controls
11110Sstevel@tonic-gate  */
11120Sstevel@tonic-gate /* ARGSUSED */
11130Sstevel@tonic-gate static int
simba_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)11140Sstevel@tonic-gate simba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
11150Sstevel@tonic-gate 	int *rvalp)
11160Sstevel@tonic-gate {
11170Sstevel@tonic-gate 	simba_devstate_t *simba_p;
11180Sstevel@tonic-gate 	dev_info_t *self;
11190Sstevel@tonic-gate 	struct devctl_iocdata *dcp;
11200Sstevel@tonic-gate 	uint_t bus_state;
11210Sstevel@tonic-gate 	int rv = 0;
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	simba_p = (simba_devstate_t *)ddi_get_soft_state(simba_state,
11240Sstevel@tonic-gate 	    getminor(dev));
11250Sstevel@tonic-gate 	if (simba_p == NULL)
11260Sstevel@tonic-gate 		return (ENXIO);
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	self = simba_p->dip;
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 	/*
11310Sstevel@tonic-gate 	 * We can use the generic implementation for these ioctls
11320Sstevel@tonic-gate 	 */
11330Sstevel@tonic-gate 	switch (cmd) {
11340Sstevel@tonic-gate 	case DEVCTL_DEVICE_GETSTATE:
11350Sstevel@tonic-gate 	case DEVCTL_DEVICE_ONLINE:
11360Sstevel@tonic-gate 	case DEVCTL_DEVICE_OFFLINE:
11370Sstevel@tonic-gate 	case DEVCTL_BUS_GETSTATE:
11380Sstevel@tonic-gate 		return (ndi_devctl_ioctl(self, cmd, arg, mode, 0));
11390Sstevel@tonic-gate 	}
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	/*
11420Sstevel@tonic-gate 	 * read devctl ioctl data
11430Sstevel@tonic-gate 	 */
11440Sstevel@tonic-gate 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
11450Sstevel@tonic-gate 		return (EFAULT);
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 	switch (cmd) {
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 	case DEVCTL_DEVICE_RESET:
11500Sstevel@tonic-gate 		rv = ENOTSUP;
11510Sstevel@tonic-gate 		break;
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	case DEVCTL_BUS_QUIESCE:
11550Sstevel@tonic-gate 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
11560Sstevel@tonic-gate 			if (bus_state == BUS_QUIESCED)
11570Sstevel@tonic-gate 				break;
11580Sstevel@tonic-gate 		(void) ndi_set_bus_state(self, BUS_QUIESCED);
11590Sstevel@tonic-gate 		break;
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	case DEVCTL_BUS_UNQUIESCE:
11620Sstevel@tonic-gate 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
11630Sstevel@tonic-gate 			if (bus_state == BUS_ACTIVE)
11640Sstevel@tonic-gate 				break;
11650Sstevel@tonic-gate 		(void) ndi_set_bus_state(self, BUS_ACTIVE);
11660Sstevel@tonic-gate 		break;
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	case DEVCTL_BUS_RESET:
11690Sstevel@tonic-gate 		rv = ENOTSUP;
11700Sstevel@tonic-gate 		break;
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 	case DEVCTL_BUS_RESETALL:
11730Sstevel@tonic-gate 		rv = ENOTSUP;
11740Sstevel@tonic-gate 		break;
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 	default:
11770Sstevel@tonic-gate 		rv = ENOTTY;
11780Sstevel@tonic-gate 	}
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	ndi_dc_freehdl(dcp);
11810Sstevel@tonic-gate 	return (rv);
11820Sstevel@tonic-gate }
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate /*
11850Sstevel@tonic-gate  * Initialize FMA resources for children devices. Called when
11860Sstevel@tonic-gate  * child calls ddi_fm_init().
11870Sstevel@tonic-gate  */
11880Sstevel@tonic-gate /*ARGSUSED*/
11890Sstevel@tonic-gate static int
simba_fm_init_child(dev_info_t * dip,dev_info_t * tdip,int cap,ddi_iblock_cookie_t * ibc)11900Sstevel@tonic-gate simba_fm_init_child(dev_info_t *dip, dev_info_t *tdip, int cap,
11910Sstevel@tonic-gate 		ddi_iblock_cookie_t *ibc)
11920Sstevel@tonic-gate {
11930Sstevel@tonic-gate 	simba_devstate_t *simba_p = ddi_get_soft_state(simba_state,
1194*7656SSherry.Moore@Sun.COM 	    ddi_get_instance(dip));
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	*ibc = simba_p->fm_ibc;
11970Sstevel@tonic-gate 	return (simba_p->fm_cap);
11980Sstevel@tonic-gate }
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate static void
simba_bus_enter(dev_info_t * dip,ddi_acc_handle_t handle)12010Sstevel@tonic-gate simba_bus_enter(dev_info_t *dip, ddi_acc_handle_t handle)
12020Sstevel@tonic-gate {
12030Sstevel@tonic-gate 	i_ndi_busop_access_enter(dip, handle);
12040Sstevel@tonic-gate }
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate /* ARGSUSED */
12070Sstevel@tonic-gate static void
simba_bus_exit(dev_info_t * dip,ddi_acc_handle_t handle)12080Sstevel@tonic-gate simba_bus_exit(dev_info_t *dip, ddi_acc_handle_t handle)
12090Sstevel@tonic-gate {
12100Sstevel@tonic-gate 	i_ndi_busop_access_exit(dip, handle);
12110Sstevel@tonic-gate }
1212