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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*1333Scth  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * **********************************************************************
310Sstevel@tonic-gate  * Extension module for PCI nexus drivers to support PCI Hot Plug feature.
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * DESCRIPTION:
340Sstevel@tonic-gate  *    This module basically implements "devctl" and Attachment Point device
350Sstevel@tonic-gate  *    nodes for hot plug operations. The cb_ops functions needed for access
360Sstevel@tonic-gate  *    to these device nodes are also implemented. For hotplug operations
370Sstevel@tonic-gate  *    on Attachment Points it interacts with the hotplug services (HPS)
380Sstevel@tonic-gate  *    framework. A pci nexus driver would simply call pcihp_init() in its
390Sstevel@tonic-gate  *    attach() function and pcihp_uninit() call in its detach() function.
400Sstevel@tonic-gate  * **********************************************************************
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <sys/conf.h>
440Sstevel@tonic-gate #include <sys/kmem.h>
450Sstevel@tonic-gate #include <sys/debug.h>
460Sstevel@tonic-gate #include <sys/modctl.h>
470Sstevel@tonic-gate #include <sys/autoconf.h>
480Sstevel@tonic-gate #include <sys/ddi.h>
490Sstevel@tonic-gate #include <sys/sunddi.h>
500Sstevel@tonic-gate #include <sys/sunndi.h>
510Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
520Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
530Sstevel@tonic-gate #include <sys/ddipropdefs.h>
540Sstevel@tonic-gate #include <sys/open.h>
550Sstevel@tonic-gate #include <sys/file.h>
560Sstevel@tonic-gate #include <sys/stat.h>
570Sstevel@tonic-gate #include <sys/pci.h>
580Sstevel@tonic-gate #include <sys/pci_impl.h>
590Sstevel@tonic-gate #include <sys/devctl.h>
600Sstevel@tonic-gate #include <sys/hotplug/hpcsvc.h>
610Sstevel@tonic-gate #include <sys/hotplug/pci/pcicfg.h>
620Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h>
630Sstevel@tonic-gate #include <sys/sysevent.h>
640Sstevel@tonic-gate #include <sys/sysevent/eventdefs.h>
650Sstevel@tonic-gate #include <sys/sysevent/dr.h>
660Sstevel@tonic-gate #include <sys/fs/dv_node.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate  * NOTE:
700Sstevel@tonic-gate  * This module depends on PCI Configurator module (misc/pcicfg),
710Sstevel@tonic-gate  * Hot Plug Services framework module (misc/hpcsvc) and Bus Resource
720Sstevel@tonic-gate  * Allocator module (misc/busra).
730Sstevel@tonic-gate  */
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate  * ************************************************************************
770Sstevel@tonic-gate  * *** Implementation specific data structures/definitions.		***
780Sstevel@tonic-gate  * ************************************************************************
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /* soft state */
820Sstevel@tonic-gate typedef enum { PCIHP_SOFT_STATE_CLOSED, PCIHP_SOFT_STATE_OPEN,
830Sstevel@tonic-gate 		PCIHP_SOFT_STATE_OPEN_EXCL } pcihp_soft_state_t;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #define	PCI_MAX_DEVS	32	/* max. number of devices on a pci bus */
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /* the following correspond to sysevent defined subclasses */
880Sstevel@tonic-gate #define	PCIHP_DR_AP_STATE_CHANGE	0
890Sstevel@tonic-gate #define	PCIHP_DR_REQ			1
900Sstevel@tonic-gate 
910Sstevel@tonic-gate /*  pcihp_get_soft_state() command argument */
920Sstevel@tonic-gate #define	PCIHP_DR_NOOP			0
930Sstevel@tonic-gate #define	PCIHP_DR_BUS_CONFIGURE		1
940Sstevel@tonic-gate #define	PCIHP_DR_BUS_UNCONFIGURE	2
950Sstevel@tonic-gate #define	PCIHP_DR_SLOT_ENTER		4
960Sstevel@tonic-gate #define	PCIHP_DR_SLOT_EXIT		8
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*  hot plug bus state */
990Sstevel@tonic-gate enum { PCIHP_BUS_INITIALIZING, PCIHP_BUS_UNCONFIGURED,
1000Sstevel@tonic-gate 		PCIHP_BUS_CONFIGURED };
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate  * Soft state structure associated with each hot plug pci bus instance.
1040Sstevel@tonic-gate  */
1050Sstevel@tonic-gate typedef struct pcihp {
1060Sstevel@tonic-gate 	struct pcihp		*nextp;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	/* devinfo pointer to the pci bus node */
1090Sstevel@tonic-gate 	dev_info_t		*dip;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	/* soft state flags: PCIHP_SOFT_STATE_* */
1120Sstevel@tonic-gate 	pcihp_soft_state_t	soft_state;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	/* global mutex to serialize exclusive access to the bus */
1150Sstevel@tonic-gate 	kmutex_t		mutex;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	/* slot information structure */
1180Sstevel@tonic-gate 	struct pcihp_slotinfo {
1190Sstevel@tonic-gate 		hpc_slot_t	slot_hdl;	/* HPS slot handle */
1200Sstevel@tonic-gate 		ap_rstate_t	rstate;		/* state of Receptacle */
1210Sstevel@tonic-gate 		ap_ostate_t	ostate;		/* state of the Occupant */
1220Sstevel@tonic-gate 		ap_condition_t	condition;	/* condition of the occupant */
1230Sstevel@tonic-gate 		time32_t	last_change;	/* XXX needed? */
1240Sstevel@tonic-gate 		uint32_t	event_mask;	/* last event mask registered */
1250Sstevel@tonic-gate 		char		*name;		/* slot logical name */
1260Sstevel@tonic-gate 		uint_t		slot_flags;
1270Sstevel@tonic-gate 		uint16_t	slot_type;	/* slot type: pci or cpci */
1280Sstevel@tonic-gate 		uint16_t	slot_capabilities; /* 64bit, etc. */
1290Sstevel@tonic-gate 		int		hs_csr_location; /* Location of HS_CSR */
1300Sstevel@tonic-gate 		kmutex_t	slot_mutex;	/* mutex to serialize hotplug */
1310Sstevel@tonic-gate 						/* operations on the slot */
1320Sstevel@tonic-gate 	} slotinfo[PCI_MAX_DEVS];
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	/* misc. bus attributes */
1350Sstevel@tonic-gate 	uint_t			bus_flags;
1360Sstevel@tonic-gate 	uint_t			bus_state;
1370Sstevel@tonic-gate 	uint_t			slots_active;
1380Sstevel@tonic-gate } pcihp_t;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate /*
1410Sstevel@tonic-gate  * Bit definitions for slot_flags field:
1420Sstevel@tonic-gate  *
1430Sstevel@tonic-gate  *	PCIHP_SLOT_AUTO_CFG_EN	This flags is set if nexus can do auto
1440Sstevel@tonic-gate  *				configuration of hot plugged card on this slot
1450Sstevel@tonic-gate  *				if the hardware reports the hot plug events.
1460Sstevel@tonic-gate  *
1470Sstevel@tonic-gate  *	PCIHP_SLOT_DISABLED	Slot is disabled for hotplug operations.
1480Sstevel@tonic-gate  *
1490Sstevel@tonic-gate  *	PCIHP_SLOT_NOT_HEALTHY	HEALTHY# signal is not OK on this slot.
1500Sstevel@tonic-gate  */
1510Sstevel@tonic-gate #define	PCIHP_SLOT_AUTO_CFG_EN		0x1
1520Sstevel@tonic-gate #define	PCIHP_SLOT_DISABLED		0x2
1530Sstevel@tonic-gate #define	PCIHP_SLOT_NOT_HEALTHY		0x4
1540Sstevel@tonic-gate #define	PCIHP_SLOT_DEV_NON_HOTPLUG	0x8
1550Sstevel@tonic-gate #define	PCIHP_SLOT_ENUM_INS_PENDING	0x10
1560Sstevel@tonic-gate #define	PCIHP_SLOT_ENUM_EXT_PENDING	0x20
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate /*
1590Sstevel@tonic-gate  * Bit definitions for bus_flags field:
1600Sstevel@tonic-gate  *
1610Sstevel@tonic-gate  *	PCIHP_BUS_66MHZ	Bus is running at 66Mhz.
1620Sstevel@tonic-gate  */
1630Sstevel@tonic-gate #define	PCIHP_BUS_66MHZ		0x1
1640Sstevel@tonic-gate #define	PCIHP_BUS_ENUM_RADIAL	0x2
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate #define	PCIHP_DEVICES_STR		"/devices"
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate  * control structure for tree walk during configure/unconfigure operation.
1700Sstevel@tonic-gate  */
1710Sstevel@tonic-gate struct pcihp_config_ctrl {
1720Sstevel@tonic-gate 	int	pci_dev;	/* PCI device number for the slot */
1730Sstevel@tonic-gate 	uint_t	flags;		/* control flags (see below) */
1740Sstevel@tonic-gate 	int	op;		/* operation: PCIHP_ONLINE or PCIHP_OFFLINE */
1750Sstevel@tonic-gate 	int	rv;		/* return error code */
1760Sstevel@tonic-gate 	dev_info_t *dip;	/* dip at which the (first) error occurred */
1770Sstevel@tonic-gate 	hpc_occupant_info_t *occupant;
1780Sstevel@tonic-gate };
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /*
1810Sstevel@tonic-gate  * control flags for configure/unconfigure operations on the tree.
1820Sstevel@tonic-gate  *
1830Sstevel@tonic-gate  * PCIHP_CFG_CONTINUE	continue the operation ignoring errors
1840Sstevel@tonic-gate  */
1850Sstevel@tonic-gate #define	PCIHP_CFG_CONTINUE	0x1
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate #define	PCIHP_ONLINE	1
1880Sstevel@tonic-gate #define	PCIHP_OFFLINE	0
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate /* Leaf ops (hotplug controls for target devices) */
1920Sstevel@tonic-gate static int pcihp_open(dev_t *, int, int, cred_t *);
1930Sstevel@tonic-gate static int pcihp_close(dev_t, int, int, cred_t *);
1940Sstevel@tonic-gate static int pcihp_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate #ifdef DEBUG
1970Sstevel@tonic-gate static int pcihp_debug = 0;
1980Sstevel@tonic-gate #define	PCIHP_DEBUG(args)	if (pcihp_debug >= 1) cmn_err args
1990Sstevel@tonic-gate #define	PCIHP_DEBUG2(args)	if (pcihp_debug >= 2) cmn_err args
2000Sstevel@tonic-gate #else
2010Sstevel@tonic-gate #define	PCIHP_DEBUG(args)
2020Sstevel@tonic-gate #define	PCIHP_DEBUG2(args)
2030Sstevel@tonic-gate #endif
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate /*
2060Sstevel@tonic-gate  * We process ENUM# event one device at a time ie. as soon as we detect
2070Sstevel@tonic-gate  * that a device has the right ENUM# conditions, we return. If the following
2080Sstevel@tonic-gate  * variable is set to non-zero, we scan all the devices on the bus
2090Sstevel@tonic-gate  * for ENUM# conditions.
2100Sstevel@tonic-gate  */
2110Sstevel@tonic-gate static int pcihp_enum_scan_all = 0;
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate  * If HSC driver cannot determine the board type (for example: it may not
2140Sstevel@tonic-gate  * be possible to differentiate between a Basic Hotswap, Non Hotswap or
2150Sstevel@tonic-gate  * Non-friendly Full hotswap board), the default board type is assigned
2160Sstevel@tonic-gate  * to be as defined by the following variable.
2170Sstevel@tonic-gate  */
2180Sstevel@tonic-gate static int pcihp_cpci_board_type = HPC_BOARD_CPCI_NON_HS;
2190Sstevel@tonic-gate static int pcihp_cpci_led_blink = 30;
2200Sstevel@tonic-gate /*
2210Sstevel@tonic-gate  * It was noted that the blue LED when written on/off would cause INS/EXT
2220Sstevel@tonic-gate  * bit to be set causing an extra interrupt. Although the cPCI specifications
2230Sstevel@tonic-gate  * does not imply this, this behavior is seen with some FHS silicons.
2240Sstevel@tonic-gate  * Also, handling the INS/EXT bit would control the LED being On/Off.
2250Sstevel@tonic-gate  * Until the behavior is confirmed, this flag could be used to enable or
2260Sstevel@tonic-gate  * disable handling the LED.
2270Sstevel@tonic-gate  * 0 means the silicons handles the LED behavior via the INS/EXT bit.
2280Sstevel@tonic-gate  * 1 means the software must explicitly do the LED behavior.
2290Sstevel@tonic-gate  */
2300Sstevel@tonic-gate static int pcihp_cpci_blue_led = 1;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate /* static functions */
2330Sstevel@tonic-gate static pcihp_t *pcihp_create_soft_state(dev_info_t *dip);
2340Sstevel@tonic-gate static void pcihp_destroy_soft_state(dev_info_t *dip);
2350Sstevel@tonic-gate static pcihp_t *pcihp_get_soft_state(dev_info_t *dip, int cmd, int *rv);
2360Sstevel@tonic-gate static int pcihp_configure_ap(pcihp_t *pcihp_p, int pci_dev);
2370Sstevel@tonic-gate static int pcihp_unconfigure_ap(pcihp_t *pcihp_p, int pci_dev);
2380Sstevel@tonic-gate static int pcihp_new_slot_state(dev_info_t *, hpc_slot_t,
2390Sstevel@tonic-gate 	hpc_slot_info_t *, int);
2400Sstevel@tonic-gate static int pcihp_configure(dev_info_t *, void *);
2410Sstevel@tonic-gate static int pcihp_event_handler(caddr_t, uint_t);
2420Sstevel@tonic-gate static dev_info_t *pcihp_devi_find(dev_info_t *dip, uint_t dev, uint_t func);
2430Sstevel@tonic-gate static int pcihp_match_dev(dev_info_t *dip, void *hdl);
2440Sstevel@tonic-gate static int pcihp_get_hs_csr(struct pcihp_slotinfo *, ddi_acc_handle_t,
2450Sstevel@tonic-gate 	uint8_t *);
2460Sstevel@tonic-gate static void pcihp_set_hs_csr(struct pcihp_slotinfo *, ddi_acc_handle_t,
2470Sstevel@tonic-gate 	uint8_t *);
2480Sstevel@tonic-gate static int pcihp_get_hs_csr_location(ddi_acc_handle_t);
2490Sstevel@tonic-gate static int pcihp_handle_enum(pcihp_t *, int, int, int);
2500Sstevel@tonic-gate static void pcihp_hs_csr_op(pcihp_t *, int, int);
2510Sstevel@tonic-gate static int pcihp_enum_slot(pcihp_t *, struct pcihp_slotinfo *, int, int, int);
2520Sstevel@tonic-gate static int pcihp_handle_enum_extraction(pcihp_t *, int, int, int);
2530Sstevel@tonic-gate static int pcihp_handle_enum_insertion(pcihp_t *, int, int, int);
2540Sstevel@tonic-gate static int pcihp_add_dummy_reg_property(dev_info_t *, uint_t, uint_t, uint_t);
2550Sstevel@tonic-gate static int pcihp_config_setup(dev_info_t **, ddi_acc_handle_t *,
2560Sstevel@tonic-gate 			dev_info_t **, int, pcihp_t *);
2570Sstevel@tonic-gate static void pcihp_config_teardown(ddi_acc_handle_t *,
2580Sstevel@tonic-gate 			dev_info_t **, int, pcihp_t *);
2590Sstevel@tonic-gate static int pcihp_get_board_type(struct pcihp_slotinfo *);
2600Sstevel@tonic-gate /* sysevent function */
2610Sstevel@tonic-gate static void pcihp_gen_sysevent(char *, int, int, dev_info_t *, int);
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate extern int pcicfg_configure(dev_info_t *, uint_t);
2640Sstevel@tonic-gate extern int pcicfg_unconfigure(dev_info_t *, uint_t);
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate static int pcihp_list_occupants(dev_info_t *, void *);
2670Sstevel@tonic-gate static int pcihp_indirect_map(dev_info_t *dip);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate #if 0
2700Sstevel@tonic-gate static void pcihp_probe_slot_state(dev_info_t *, int, hpc_slot_state_t *);
2710Sstevel@tonic-gate #endif
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate int pcihp_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
2740Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp);
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate struct cb_ops pcihp_cb_ops = {
2770Sstevel@tonic-gate 	pcihp_open,			/* open */
2780Sstevel@tonic-gate 	pcihp_close,			/* close */
2790Sstevel@tonic-gate 	nodev,				/* strategy */
2800Sstevel@tonic-gate 	nodev,				/* print */
2810Sstevel@tonic-gate 	nodev,				/* dump */
2820Sstevel@tonic-gate 	nodev,				/* read */
2830Sstevel@tonic-gate 	nodev,				/* write */
2840Sstevel@tonic-gate 	pcihp_ioctl,			/* ioctl */
2850Sstevel@tonic-gate 	nodev,				/* devmap */
2860Sstevel@tonic-gate 	nodev,				/* mmap */
2870Sstevel@tonic-gate 	nodev,				/* segmap */
2880Sstevel@tonic-gate 	nochpoll,			/* poll */
2890Sstevel@tonic-gate 	pcihp_prop_op,			/* cb_prop_op */
2900Sstevel@tonic-gate 	NULL,				/* streamtab */
2910Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
2920Sstevel@tonic-gate 	CB_REV,				/* rev */
2930Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
2940Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
2950Sstevel@tonic-gate };
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate /*
2980Sstevel@tonic-gate  * local data
2990Sstevel@tonic-gate  */
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate int pcihp_autocfg_enabled = 1; /* auto config is enabled by default */
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate static kmutex_t pcihp_mutex; /* mutex to protect the following data */
3040Sstevel@tonic-gate static pcihp_t *pcihp_head = NULL;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate static kmutex_t pcihp_open_mutex; /* mutex to protect open/close/uninit */
3070Sstevel@tonic-gate static int	pci_devlink_flags = 0;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate /*
3100Sstevel@tonic-gate  * Module linkage information for the kernel.
3110Sstevel@tonic-gate  */
3120Sstevel@tonic-gate extern struct mod_ops mod_miscops;
3130Sstevel@tonic-gate static struct modlmisc modlmisc = {
3140Sstevel@tonic-gate 	&mod_miscops,
3150Sstevel@tonic-gate 	"PCI nexus hotplug support v%I%",
3160Sstevel@tonic-gate };
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate static struct modlinkage modlinkage = {
3190Sstevel@tonic-gate 	MODREV_1,
3200Sstevel@tonic-gate 	&modlmisc,
3210Sstevel@tonic-gate 	NULL
3220Sstevel@tonic-gate };
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate int
3250Sstevel@tonic-gate _init(void)
3260Sstevel@tonic-gate {
3270Sstevel@tonic-gate 	int error;
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	mutex_init(&pcihp_mutex, NULL, MUTEX_DRIVER, NULL);
3300Sstevel@tonic-gate 	mutex_init(&pcihp_open_mutex, NULL, MUTEX_DRIVER, NULL);
3310Sstevel@tonic-gate 	if ((error = mod_install(&modlinkage)) != 0) {
3320Sstevel@tonic-gate 		mutex_destroy(&pcihp_open_mutex);
3330Sstevel@tonic-gate 		mutex_destroy(&pcihp_mutex);
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	return (error);
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate int
3400Sstevel@tonic-gate _fini(void)
3410Sstevel@tonic-gate {
3420Sstevel@tonic-gate 	return (EBUSY);
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate int
3460Sstevel@tonic-gate _info(struct modinfo *modinfop)
3470Sstevel@tonic-gate {
3480Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate static	pcihp_t *
3520Sstevel@tonic-gate pcihp_create_soft_state(
3530Sstevel@tonic-gate 	dev_info_t *dip)
3540Sstevel@tonic-gate {
3550Sstevel@tonic-gate 	pcihp_t	*pcihp_p;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	pcihp_p = kmem_zalloc(sizeof (struct pcihp), KM_SLEEP);
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	pcihp_p->dip = dip;
3600Sstevel@tonic-gate 	mutex_init(&pcihp_p->mutex, NULL, MUTEX_DRIVER, NULL);
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	mutex_enter(&pcihp_mutex);
3630Sstevel@tonic-gate 	pcihp_p->nextp = pcihp_head;
3640Sstevel@tonic-gate 	pcihp_head = pcihp_p;
3650Sstevel@tonic-gate 	pcihp_p->bus_state = PCIHP_BUS_INITIALIZING;
3660Sstevel@tonic-gate 	pcihp_p->slots_active = 0;
3670Sstevel@tonic-gate 	mutex_exit(&pcihp_mutex);
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	return (pcihp_p);
3700Sstevel@tonic-gate }
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate static	void
3730Sstevel@tonic-gate pcihp_destroy_soft_state(
3740Sstevel@tonic-gate 	dev_info_t *dip)
3750Sstevel@tonic-gate {
3760Sstevel@tonic-gate 	pcihp_t	*p;
3770Sstevel@tonic-gate 	pcihp_t	**pp;
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	mutex_enter(&pcihp_mutex);
3800Sstevel@tonic-gate 	pp = &pcihp_head;
3810Sstevel@tonic-gate 	while ((p = *pp) != NULL) {
3820Sstevel@tonic-gate 		if (p->dip == dip) {
3830Sstevel@tonic-gate 			*pp = p->nextp;
3840Sstevel@tonic-gate 			kmem_free(p, sizeof (struct pcihp));
3850Sstevel@tonic-gate 			break;
3860Sstevel@tonic-gate 		}
3870Sstevel@tonic-gate 		pp = &(p->nextp);
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 	mutex_exit(&pcihp_mutex);
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate /*
3930Sstevel@tonic-gate  * This function should be imported by client nexus drivers as their
3940Sstevel@tonic-gate  * devo_getinfo() entry point.
3950Sstevel@tonic-gate  */
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate /* ARGSUSED */
3980Sstevel@tonic-gate int
3990Sstevel@tonic-gate pcihp_info(
4000Sstevel@tonic-gate 	dev_info_t	*dip,
4010Sstevel@tonic-gate 	ddi_info_cmd_t	cmd,
4020Sstevel@tonic-gate 	void		*arg,
4030Sstevel@tonic-gate 	void		**result)
4040Sstevel@tonic-gate {
4050Sstevel@tonic-gate 	pcihp_t		*pcihp_p;
4060Sstevel@tonic-gate 	major_t		major;
4070Sstevel@tonic-gate 	minor_t		minor;
4080Sstevel@tonic-gate 	int		instance;
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	major = getmajor((dev_t)arg);
4110Sstevel@tonic-gate 	minor = getminor((dev_t)arg);
4120Sstevel@tonic-gate 	instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	switch (cmd) {
4150Sstevel@tonic-gate 	default:
4160Sstevel@tonic-gate 		return (DDI_FAILURE);
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
4190Sstevel@tonic-gate 		*result = (void *)(intptr_t)instance;
4200Sstevel@tonic-gate 		return (DDI_SUCCESS);
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
4230Sstevel@tonic-gate 		mutex_enter(&pcihp_mutex);
4240Sstevel@tonic-gate 		pcihp_p = pcihp_head;
4250Sstevel@tonic-gate 		while (pcihp_p != NULL) {
4260Sstevel@tonic-gate 			if (ddi_name_to_major(ddi_get_name(pcihp_p->dip)) ==
4270Sstevel@tonic-gate 			    major && ddi_get_instance(pcihp_p->dip) ==
4280Sstevel@tonic-gate 			    instance) {
4290Sstevel@tonic-gate 				*result = (void *)pcihp_p->dip;
4300Sstevel@tonic-gate 				mutex_exit(&pcihp_mutex);
4310Sstevel@tonic-gate 				return (DDI_SUCCESS);
4320Sstevel@tonic-gate 			}
4330Sstevel@tonic-gate 			pcihp_p = pcihp_p->nextp;
4340Sstevel@tonic-gate 		}
4350Sstevel@tonic-gate 		mutex_exit(&pcihp_mutex);
4360Sstevel@tonic-gate 		return (DDI_FAILURE);
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate }
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate /*
4410Sstevel@tonic-gate  * This function retrieves the hot plug soft state and performs the
4420Sstevel@tonic-gate  * following primitive commands while the soft state is locked:
4430Sstevel@tonic-gate  * mark the bus unconfigured, increment slot activity, decrement
4440Sstevel@tonic-gate  * slot activity and noop.
4450Sstevel@tonic-gate  */
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate /* ARGSUSED */
4480Sstevel@tonic-gate static	pcihp_t *
4490Sstevel@tonic-gate pcihp_get_soft_state(
4500Sstevel@tonic-gate 	dev_info_t	*dip, int cmd, int *rv)
4510Sstevel@tonic-gate {
4520Sstevel@tonic-gate 	pcihp_t		*pcihp_p;
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	*rv = PCIHP_SUCCESS;
4550Sstevel@tonic-gate 	mutex_enter(&pcihp_mutex);
4560Sstevel@tonic-gate 	pcihp_p = pcihp_head;
4570Sstevel@tonic-gate 	while (pcihp_p != NULL) {
4580Sstevel@tonic-gate 		if (pcihp_p->dip == dip) {
4590Sstevel@tonic-gate 			switch (cmd) {
4600Sstevel@tonic-gate 			case PCIHP_DR_BUS_UNCONFIGURE:
4610Sstevel@tonic-gate 				if (pcihp_p->slots_active == 0)
4620Sstevel@tonic-gate 					pcihp_p->bus_state =
4630Sstevel@tonic-gate 					    PCIHP_BUS_UNCONFIGURED;
4640Sstevel@tonic-gate 				else
4650Sstevel@tonic-gate 					*rv = PCIHP_FAILURE;
4660Sstevel@tonic-gate 				break;
4670Sstevel@tonic-gate 			case PCIHP_DR_SLOT_ENTER:
4680Sstevel@tonic-gate 				if (pcihp_p->bus_state ==
4690Sstevel@tonic-gate 				    PCIHP_BUS_UNCONFIGURED)
4700Sstevel@tonic-gate 					*rv = PCIHP_FAILURE;
4710Sstevel@tonic-gate 				else
4720Sstevel@tonic-gate 					pcihp_p->slots_active++;
4730Sstevel@tonic-gate 				break;
4740Sstevel@tonic-gate 			case PCIHP_DR_SLOT_EXIT:
4750Sstevel@tonic-gate 				ASSERT(pcihp_p->slots_active > 0);
4760Sstevel@tonic-gate 				if (pcihp_p->slots_active == 0)
4770Sstevel@tonic-gate 					cmn_err(CE_PANIC,
4780Sstevel@tonic-gate 					    "pcihp (%s%d): mismatched slot"
4790Sstevel@tonic-gate 					    " activity",
4800Sstevel@tonic-gate 					    ddi_driver_name(dip),
4810Sstevel@tonic-gate 					    ddi_get_instance(dip));
4820Sstevel@tonic-gate 				else
4830Sstevel@tonic-gate 					pcihp_p->slots_active--;
4840Sstevel@tonic-gate 				break;
4850Sstevel@tonic-gate 			case PCIHP_DR_NOOP:
4860Sstevel@tonic-gate 				break;
4870Sstevel@tonic-gate 			default:
4880Sstevel@tonic-gate 				*rv = PCIHP_FAILURE;
4890Sstevel@tonic-gate 				break;
4900Sstevel@tonic-gate 			}
4910Sstevel@tonic-gate 			mutex_exit(&pcihp_mutex);
4920Sstevel@tonic-gate 			return (pcihp_p);
4930Sstevel@tonic-gate 		}
4940Sstevel@tonic-gate 		pcihp_p = pcihp_p->nextp;
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate 	mutex_exit(&pcihp_mutex);
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	return (NULL);
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate /* ARGSUSED3 */
5020Sstevel@tonic-gate static int
5030Sstevel@tonic-gate pcihp_open(dev_t *devp, int flags, int otyp, cred_t *credp)
5040Sstevel@tonic-gate {
5050Sstevel@tonic-gate 	dev_info_t *self;
5060Sstevel@tonic-gate 	pcihp_t *pcihp_p;
5070Sstevel@tonic-gate 	minor_t	minor;
5080Sstevel@tonic-gate 	int pci_dev;
5090Sstevel@tonic-gate 	int rv;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	/*
5120Sstevel@tonic-gate 	 * Make sure the open is for the right file type.
5130Sstevel@tonic-gate 	 */
5140Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
5150Sstevel@tonic-gate 		return (EINVAL);
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	mutex_enter(&pcihp_open_mutex);
5180Sstevel@tonic-gate 	/*
5190Sstevel@tonic-gate 	 * Get the soft state structure.
5200Sstevel@tonic-gate 	 */
5210Sstevel@tonic-gate 	if (pcihp_info(NULL, DDI_INFO_DEVT2DEVINFO, (void *)*devp,
5220Sstevel@tonic-gate 	    (void **)&self) != DDI_SUCCESS) {
5230Sstevel@tonic-gate 		mutex_exit(&pcihp_open_mutex);
5240Sstevel@tonic-gate 		return (ENXIO);
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	pcihp_p = pcihp_get_soft_state(self, PCIHP_DR_NOOP, &rv);
5280Sstevel@tonic-gate 	ASSERT(pcihp_p != NULL);
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	mutex_enter(&pcihp_p->mutex);
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	/*
5330Sstevel@tonic-gate 	 * If the pci_dev is valid then the minor device is an
5340Sstevel@tonic-gate 	 * AP. Otherwise it is ":devctl" minor device.
5350Sstevel@tonic-gate 	 */
5360Sstevel@tonic-gate 	minor = getminor(*devp);
5370Sstevel@tonic-gate 	pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(minor);
5380Sstevel@tonic-gate 	if (pci_dev < PCI_MAX_DEVS) {
5390Sstevel@tonic-gate 		struct pcihp_slotinfo *slotinfop;
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 		slotinfop = &pcihp_p->slotinfo[pci_dev];
5420Sstevel@tonic-gate 		if (slotinfop->slot_hdl == NULL) {
5430Sstevel@tonic-gate 			mutex_exit(&pcihp_p->mutex);
5440Sstevel@tonic-gate 			mutex_exit(&pcihp_open_mutex);
5450Sstevel@tonic-gate 			return (ENXIO);
5460Sstevel@tonic-gate 		}
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	/*
5500Sstevel@tonic-gate 	 * Handle the open by tracking the device state.
5510Sstevel@tonic-gate 	 *
5520Sstevel@tonic-gate 	 * Note: Needs review w.r.t exclusive access to AP or the bus.
5530Sstevel@tonic-gate 	 * Currently in the pci plug-in we don't use EXCL open at all
5540Sstevel@tonic-gate 	 * so the code below implements EXCL access on the bus.
5550Sstevel@tonic-gate 	 */
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	/* enforce exclusive access to the bus */
5580Sstevel@tonic-gate 	if ((pcihp_p->soft_state == PCIHP_SOFT_STATE_OPEN_EXCL) ||
5590Sstevel@tonic-gate 	    ((flags & FEXCL) &&
5600Sstevel@tonic-gate 	    (pcihp_p->soft_state != PCIHP_SOFT_STATE_CLOSED))) {
5610Sstevel@tonic-gate 		mutex_exit(&pcihp_p->mutex);
5620Sstevel@tonic-gate 		mutex_exit(&pcihp_open_mutex);
5630Sstevel@tonic-gate 		return (EBUSY);
5640Sstevel@tonic-gate 	}
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	if (flags & FEXCL)
5670Sstevel@tonic-gate 		pcihp_p->soft_state = PCIHP_SOFT_STATE_OPEN_EXCL;
5680Sstevel@tonic-gate 	else
5690Sstevel@tonic-gate 		pcihp_p->soft_state = PCIHP_SOFT_STATE_OPEN;
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	mutex_exit(&pcihp_p->mutex);
5720Sstevel@tonic-gate 	mutex_exit(&pcihp_open_mutex);
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	return (0);
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate /* ARGSUSED */
5780Sstevel@tonic-gate static int
5790Sstevel@tonic-gate pcihp_close(dev_t dev, int flags, int otyp, cred_t *credp)
5800Sstevel@tonic-gate {
5810Sstevel@tonic-gate 	dev_info_t *self;
5820Sstevel@tonic-gate 	pcihp_t *pcihp_p;
5830Sstevel@tonic-gate 	int rv;
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
5860Sstevel@tonic-gate 		return (EINVAL);
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	mutex_enter(&pcihp_open_mutex);
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	if (pcihp_info(NULL, DDI_INFO_DEVT2DEVINFO, (void *)dev,
5910Sstevel@tonic-gate 	    (void **)&self) != DDI_SUCCESS) {
5920Sstevel@tonic-gate 		mutex_exit(&pcihp_open_mutex);
5930Sstevel@tonic-gate 		return (ENXIO);
5940Sstevel@tonic-gate 	}
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	pcihp_p = pcihp_get_soft_state(self, PCIHP_DR_NOOP, &rv);
5970Sstevel@tonic-gate 	ASSERT(pcihp_p != NULL);
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	mutex_enter(&pcihp_p->mutex);
6000Sstevel@tonic-gate 	pcihp_p->soft_state = PCIHP_SOFT_STATE_CLOSED;
6010Sstevel@tonic-gate 	mutex_exit(&pcihp_p->mutex);
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	mutex_exit(&pcihp_open_mutex);
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	return (0);
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate static int
6090Sstevel@tonic-gate pcihp_list_occupants(dev_info_t *dip, void *hdl)
6100Sstevel@tonic-gate {
6110Sstevel@tonic-gate 	int pci_dev;
6120Sstevel@tonic-gate 	struct pcihp_config_ctrl *ctrl = (struct pcihp_config_ctrl *)hdl;
6130Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
6140Sstevel@tonic-gate 	int length;
6150Sstevel@tonic-gate 	major_t major;
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	/*
6180Sstevel@tonic-gate 	 * Get the PCI device number information from the devinfo
6190Sstevel@tonic-gate 	 * node. Since the node may not have the address field
6200Sstevel@tonic-gate 	 * setup (this is done in the DDI_INITCHILD of the parent)
6210Sstevel@tonic-gate 	 * we look up the 'reg' property to decode that information.
6220Sstevel@tonic-gate 	 */
6230Sstevel@tonic-gate 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
6240Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "reg", (int **)&pci_rp,
6250Sstevel@tonic-gate 	    (uint_t *)&length) != DDI_PROP_SUCCESS) {
6260Sstevel@tonic-gate 		ctrl->rv = DDI_FAILURE;
6270Sstevel@tonic-gate 		ctrl->dip = dip;
6280Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
6290Sstevel@tonic-gate 	}
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	/* get the pci device id information */
6320Sstevel@tonic-gate 	pci_dev = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	/*
6350Sstevel@tonic-gate 	 * free the memory allocated by ddi_prop_lookup_int_array
6360Sstevel@tonic-gate 	 */
6370Sstevel@tonic-gate 	ddi_prop_free(pci_rp);
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	/*
6400Sstevel@tonic-gate 	 * Match the node for the device number of the slot.
6410Sstevel@tonic-gate 	 */
6420Sstevel@tonic-gate 	if (pci_dev == ctrl->pci_dev) { /* node is a match */
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 		major = ddi_driver_major(dip);
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 		/*
6470Sstevel@tonic-gate 		 * If the node is not yet attached, then don't list it
6480Sstevel@tonic-gate 		 * as an occupant. This is valid, since nothing can be
6490Sstevel@tonic-gate 		 * consuming it until it is attached, and cfgadm will
6500Sstevel@tonic-gate 		 * ask for the property explicitly which will cause it
6510Sstevel@tonic-gate 		 * to be re-freshed right before checking with rcm.
6520Sstevel@tonic-gate 		 */
653*1333Scth 		if ((major == -1) || !i_ddi_devi_attached(dip))
6540Sstevel@tonic-gate 			return (DDI_WALK_PRUNECHILD);
6550Sstevel@tonic-gate 
656261Scth 		/*
657261Scth 		 * If we have used all our occupants then print mesage
658261Scth 		 * and terminate walk.
659261Scth 		 */
660261Scth 		if (ctrl->occupant->i >= HPC_MAX_OCCUPANTS) {
661261Scth 			cmn_err(CE_WARN,
662261Scth 			    "pcihp (%s%d): unable to list all occupants",
663261Scth 			    ddi_driver_name(ddi_get_parent(dip)),
664261Scth 			    ddi_get_instance(ddi_get_parent(dip)));
665261Scth 			return (DDI_WALK_TERMINATE);
666261Scth 		}
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 		/*
6690Sstevel@tonic-gate 		 * No need to hold the dip as ddi_walk_devs
6700Sstevel@tonic-gate 		 * has already arranged that for us.
6710Sstevel@tonic-gate 		 */
672261Scth 		ctrl->occupant->id[ctrl->occupant->i] =
673261Scth 		    kmem_alloc(sizeof (char[MAXPATHLEN]), KM_SLEEP);
6740Sstevel@tonic-gate 		(void) ddi_pathname(dip,
6750Sstevel@tonic-gate 		    (char *)ctrl->occupant->id[ctrl->occupant->i]);
6760Sstevel@tonic-gate 		ctrl->occupant->i++;
6770Sstevel@tonic-gate 	}
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	/*
6800Sstevel@tonic-gate 	 * continue the walk to the next sibling to look for a match
6810Sstevel@tonic-gate 	 * or to find other nodes if this card is a multi-function card.
6820Sstevel@tonic-gate 	 */
6830Sstevel@tonic-gate 	return (DDI_WALK_PRUNECHILD);
6840Sstevel@tonic-gate }
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate static void
6870Sstevel@tonic-gate pcihp_create_occupant_props_nolock(dev_info_t *self, dev_t dev, int pci_dev)
6880Sstevel@tonic-gate {
6890Sstevel@tonic-gate 	struct pcihp_config_ctrl ctrl;
6900Sstevel@tonic-gate 	hpc_occupant_info_t *occupant;
6910Sstevel@tonic-gate 	int i;
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	occupant = kmem_alloc(sizeof (hpc_occupant_info_t), KM_SLEEP);
6940Sstevel@tonic-gate 	occupant->i = 0;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	ctrl.flags = 0;
6970Sstevel@tonic-gate 	ctrl.dip = NULL;
6980Sstevel@tonic-gate 	ctrl.rv = NDI_SUCCESS;
6990Sstevel@tonic-gate 	ctrl.pci_dev = pci_dev;
7000Sstevel@tonic-gate 	ctrl.op = 55; /* should define DRYRUN */
7010Sstevel@tonic-gate 	ctrl.occupant = occupant;
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	ddi_walk_devs(ddi_get_child(self), pcihp_list_occupants,
7040Sstevel@tonic-gate 	    (void *)&ctrl);
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	if (occupant->i == 0) {
7070Sstevel@tonic-gate 		/* no occupants right now, need to create stub property */
7080Sstevel@tonic-gate 		char *c[] = { "" };
7090Sstevel@tonic-gate 		(void) ddi_prop_update_string_array(dev, self, "pci-occupant",
7100Sstevel@tonic-gate 		    c, 1);
7110Sstevel@tonic-gate 	} else {
7120Sstevel@tonic-gate 		(void) ddi_prop_update_string_array(dev, self, "pci-occupant",
7130Sstevel@tonic-gate 		    occupant->id, occupant->i);
7140Sstevel@tonic-gate 	}
7150Sstevel@tonic-gate 	for (i = 0; i < occupant->i; i++) {
7160Sstevel@tonic-gate 		kmem_free(occupant->id[i], sizeof (char[MAXPATHLEN]));
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	kmem_free(occupant, sizeof (hpc_occupant_info_t));
7200Sstevel@tonic-gate }
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate static void
7230Sstevel@tonic-gate pcihp_create_occupant_props(dev_info_t *self, dev_t dev, int pci_dev)
7240Sstevel@tonic-gate {
7250Sstevel@tonic-gate 	int circular;
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	ndi_devi_enter(self, &circular);
7280Sstevel@tonic-gate 	pcihp_create_occupant_props_nolock(self, dev, pci_dev);
7290Sstevel@tonic-gate 	ndi_devi_exit(self, circular);
7300Sstevel@tonic-gate }
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate static void
7330Sstevel@tonic-gate pcihp_delete_occupant_props(dev_info_t *dip, dev_t dev)
7340Sstevel@tonic-gate {
7350Sstevel@tonic-gate 	if (ddi_prop_remove(dev, dip, "pci-occupant")
7360Sstevel@tonic-gate 	    != DDI_PROP_SUCCESS)
7370Sstevel@tonic-gate 		return; /* add error handling */
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate }
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate /*
7420Sstevel@tonic-gate  * pcihp_ioctl: devctl hotplug controls
7430Sstevel@tonic-gate  */
7440Sstevel@tonic-gate /* ARGSUSED */
7450Sstevel@tonic-gate static int
7460Sstevel@tonic-gate pcihp_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
7470Sstevel@tonic-gate 	int *rvalp)
7480Sstevel@tonic-gate {
7490Sstevel@tonic-gate 	pcihp_t *pcihp_p;
7500Sstevel@tonic-gate 	dev_info_t *self;
7510Sstevel@tonic-gate 	struct devctl_iocdata *dcp;
7520Sstevel@tonic-gate 	uint_t bus_state;
7530Sstevel@tonic-gate 	int rv = 0;
7540Sstevel@tonic-gate 	int pci_dev;
7550Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
7560Sstevel@tonic-gate 	hpc_slot_state_t rstate;
7570Sstevel@tonic-gate 	devctl_ap_state_t ap_state;
7580Sstevel@tonic-gate 	struct hpc_control_data hpc_ctrldata;
7590Sstevel@tonic-gate 	struct hpc_led_info led_info;
7600Sstevel@tonic-gate 	time_t time;
7610Sstevel@tonic-gate 	int state_locking;
7620Sstevel@tonic-gate 	int state_unlocking;
7630Sstevel@tonic-gate 	int rval;
7640Sstevel@tonic-gate 	char *pathname = NULL;
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	/*
7670Sstevel@tonic-gate 	 * read devctl ioctl data before soft state retrieval
7680Sstevel@tonic-gate 	 */
7690Sstevel@tonic-gate 	if ((cmd != DEVCTL_AP_CONTROL) &&
7700Sstevel@tonic-gate 	    ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
7710Sstevel@tonic-gate 		return (EFAULT);
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	if (pcihp_info(NULL, DDI_INFO_DEVT2DEVINFO, (void *)dev,
7740Sstevel@tonic-gate 	    (void **)&self) != DDI_SUCCESS) {
7750Sstevel@tonic-gate 		if (cmd != DEVCTL_AP_CONTROL)
7760Sstevel@tonic-gate 			ndi_dc_freehdl(dcp);
7770Sstevel@tonic-gate 		return (ENXIO);
7780Sstevel@tonic-gate 	}
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	switch (cmd) {
7810Sstevel@tonic-gate 	case DEVCTL_AP_INSERT:
7820Sstevel@tonic-gate 	case DEVCTL_AP_REMOVE:
7830Sstevel@tonic-gate 	case DEVCTL_AP_CONNECT:
7840Sstevel@tonic-gate 	case DEVCTL_AP_DISCONNECT:
7850Sstevel@tonic-gate 	case DEVCTL_AP_CONFIGURE:
7860Sstevel@tonic-gate 	case DEVCTL_AP_UNCONFIGURE:
7870Sstevel@tonic-gate 	case DEVCTL_AP_GETSTATE:
7880Sstevel@tonic-gate 	case DEVCTL_AP_CONTROL:
7890Sstevel@tonic-gate 		state_locking = PCIHP_DR_SLOT_ENTER;
7900Sstevel@tonic-gate 		state_unlocking = PCIHP_DR_SLOT_EXIT;
7910Sstevel@tonic-gate 		break;
7920Sstevel@tonic-gate 	default:
7930Sstevel@tonic-gate 		state_locking = PCIHP_DR_NOOP;
7940Sstevel@tonic-gate 		state_unlocking = PCIHP_DR_NOOP;
7950Sstevel@tonic-gate 		break;
7960Sstevel@tonic-gate 	}
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	pcihp_p = pcihp_get_soft_state(self, state_locking, &rval);
7990Sstevel@tonic-gate 	ASSERT(pcihp_p != NULL);
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	if (rval == PCIHP_FAILURE) {
8020Sstevel@tonic-gate 		(void) ddi_pathname(pcihp_p->dip, pathname);
8030Sstevel@tonic-gate 		PCIHP_DEBUG((CE_WARN, "Hot Plug bus %s instance is unconfigured"
8040Sstevel@tonic-gate 		    " while slot activity is requested\n", pathname));
8050Sstevel@tonic-gate 		if (cmd != DEVCTL_AP_CONTROL)
8060Sstevel@tonic-gate 			ndi_dc_freehdl(dcp);
8070Sstevel@tonic-gate 		return (EBUSY);
8080Sstevel@tonic-gate 	}
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	/*
8110Sstevel@tonic-gate 	 * For attachment points the lower 8 bits of the minor number is the
8120Sstevel@tonic-gate 	 * PCI device number.
8130Sstevel@tonic-gate 	 */
8140Sstevel@tonic-gate 	pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(getminor(dev));
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	/*
8170Sstevel@tonic-gate 	 * We can use the generic implementation for these ioctls
8180Sstevel@tonic-gate 	 */
8190Sstevel@tonic-gate 	switch (cmd) {
8200Sstevel@tonic-gate 	case DEVCTL_DEVICE_GETSTATE:
8210Sstevel@tonic-gate 	case DEVCTL_DEVICE_ONLINE:
8220Sstevel@tonic-gate 	case DEVCTL_DEVICE_OFFLINE:
8230Sstevel@tonic-gate 	case DEVCTL_BUS_GETSTATE:
8240Sstevel@tonic-gate 		rv = ndi_devctl_ioctl(self, cmd, arg, mode, 0);
8250Sstevel@tonic-gate 		ndi_dc_freehdl(dcp);
8260Sstevel@tonic-gate 		return (rv);
8270Sstevel@tonic-gate 	default:
8280Sstevel@tonic-gate 		break;
8290Sstevel@tonic-gate 	}
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	switch (cmd) {
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	case DEVCTL_DEVICE_RESET:
8340Sstevel@tonic-gate 		rv = ENOTSUP;
8350Sstevel@tonic-gate 		break;
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	case DEVCTL_BUS_QUIESCE:
8380Sstevel@tonic-gate 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
8390Sstevel@tonic-gate 			if (bus_state == BUS_QUIESCED)
8400Sstevel@tonic-gate 				break;
8410Sstevel@tonic-gate 		(void) ndi_set_bus_state(self, BUS_QUIESCED);
8420Sstevel@tonic-gate 		break;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	case DEVCTL_BUS_UNQUIESCE:
8450Sstevel@tonic-gate 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
8460Sstevel@tonic-gate 			if (bus_state == BUS_ACTIVE)
8470Sstevel@tonic-gate 				break;
8480Sstevel@tonic-gate 		(void) ndi_set_bus_state(self, BUS_ACTIVE);
8490Sstevel@tonic-gate 		break;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	case DEVCTL_BUS_RESET:
8520Sstevel@tonic-gate 		rv = ENOTSUP;
8530Sstevel@tonic-gate 		break;
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	case DEVCTL_BUS_RESETALL:
8560Sstevel@tonic-gate 		rv = ENOTSUP;
8570Sstevel@tonic-gate 		break;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	case DEVCTL_AP_CONNECT:
8600Sstevel@tonic-gate 	case DEVCTL_AP_DISCONNECT:
8610Sstevel@tonic-gate 		/*
8620Sstevel@tonic-gate 		 * CONNECT(DISCONNECT) the hot plug slot to(from) the bus.
8630Sstevel@tonic-gate 		 *
8640Sstevel@tonic-gate 		 * For cPCI slots this operation is a nop so the HPC
8650Sstevel@tonic-gate 		 * driver may return success if it is a valid operation.
8660Sstevel@tonic-gate 		 */
8670Sstevel@tonic-gate 	case DEVCTL_AP_INSERT:
8680Sstevel@tonic-gate 	case DEVCTL_AP_REMOVE:
8690Sstevel@tonic-gate 		/*
8700Sstevel@tonic-gate 		 * Prepare the slot for INSERT/REMOVE operation.
8710Sstevel@tonic-gate 		 */
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 		/*
8740Sstevel@tonic-gate 		 * check for valid request:
8750Sstevel@tonic-gate 		 *	1. It is a hotplug slot.
8760Sstevel@tonic-gate 		 *	2. The slot has no occupant that is in
8770Sstevel@tonic-gate 		 *	   the 'configured' state.
8780Sstevel@tonic-gate 		 */
8790Sstevel@tonic-gate 		if (pci_dev >= PCI_MAX_DEVS) {
8800Sstevel@tonic-gate 			rv = ENXIO;
8810Sstevel@tonic-gate 			break;
8820Sstevel@tonic-gate 		}
8830Sstevel@tonic-gate 		slotinfop = &pcihp_p->slotinfo[pci_dev];
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 		mutex_enter(&slotinfop->slot_mutex);
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 		if ((slotinfop->slot_hdl == NULL) ||
8880Sstevel@tonic-gate 		    (slotinfop->slot_flags & PCIHP_SLOT_DISABLED)) {
8890Sstevel@tonic-gate 			rv = ENXIO;
8900Sstevel@tonic-gate 			mutex_exit(&slotinfop->slot_mutex);
8910Sstevel@tonic-gate 			break;
8920Sstevel@tonic-gate 		}
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 		/* the slot occupant must be in the UNCONFIGURED state */
8950Sstevel@tonic-gate 		if (slotinfop->ostate != AP_OSTATE_UNCONFIGURED) {
8960Sstevel@tonic-gate 			rv = EINVAL;
8970Sstevel@tonic-gate 			mutex_exit(&slotinfop->slot_mutex);
8980Sstevel@tonic-gate 			break;
8990Sstevel@tonic-gate 		}
9000Sstevel@tonic-gate 		/*
9010Sstevel@tonic-gate 		 * Call the HPC driver to perform the operation on the slot.
9020Sstevel@tonic-gate 		 */
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 		switch (cmd) {
9050Sstevel@tonic-gate 		case DEVCTL_AP_INSERT:
9060Sstevel@tonic-gate 			rv = hpc_nexus_insert(slotinfop->slot_hdl, NULL, 0);
9070Sstevel@tonic-gate 			break;
9080Sstevel@tonic-gate 		case DEVCTL_AP_REMOVE:
9090Sstevel@tonic-gate 			rv = hpc_nexus_remove(slotinfop->slot_hdl, NULL, 0);
9100Sstevel@tonic-gate 			break;
9110Sstevel@tonic-gate 		case DEVCTL_AP_CONNECT:
9120Sstevel@tonic-gate 			rv = hpc_nexus_connect(slotinfop->slot_hdl, NULL, 0);
9130Sstevel@tonic-gate 			if (rv == HPC_SUCCESS) {
9140Sstevel@tonic-gate 				slotinfop->rstate = AP_RSTATE_CONNECTED;
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 				if (drv_getparm(TIME, (void *)&time) !=
9170Sstevel@tonic-gate 				    DDI_SUCCESS)
9180Sstevel@tonic-gate 					slotinfop->last_change = (time_t)-1;
9190Sstevel@tonic-gate 				else
9200Sstevel@tonic-gate 					slotinfop->last_change = (time32_t)time;
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 				slotinfop = &pcihp_p->slotinfo[pci_dev];
9230Sstevel@tonic-gate 				pcihp_gen_sysevent(slotinfop->name,
9240Sstevel@tonic-gate 						PCIHP_DR_AP_STATE_CHANGE,
9250Sstevel@tonic-gate 						SE_NO_HINT, pcihp_p->dip,
9260Sstevel@tonic-gate 						KM_SLEEP);
9270Sstevel@tonic-gate 			}
9280Sstevel@tonic-gate 			break;
9290Sstevel@tonic-gate 		case DEVCTL_AP_DISCONNECT:
9300Sstevel@tonic-gate 			rv = hpc_nexus_disconnect(slotinfop->slot_hdl, NULL, 0);
9310Sstevel@tonic-gate 			if (rv == HPC_SUCCESS) {
9320Sstevel@tonic-gate 				slotinfop->rstate = AP_RSTATE_DISCONNECTED;
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 				if (drv_getparm(TIME, (void *)&time) !=
9350Sstevel@tonic-gate 				    DDI_SUCCESS)
9360Sstevel@tonic-gate 					slotinfop->last_change = (time_t)-1;
9370Sstevel@tonic-gate 				else
9380Sstevel@tonic-gate 					slotinfop->last_change = (time32_t)time;
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 				slotinfop = &pcihp_p->slotinfo[pci_dev];
9410Sstevel@tonic-gate 				pcihp_gen_sysevent(slotinfop->name,
9420Sstevel@tonic-gate 						PCIHP_DR_AP_STATE_CHANGE,
9430Sstevel@tonic-gate 						SE_NO_HINT, pcihp_p->dip,
9440Sstevel@tonic-gate 						KM_SLEEP);
9450Sstevel@tonic-gate 			}
9460Sstevel@tonic-gate 			break;
9470Sstevel@tonic-gate 		}
9480Sstevel@tonic-gate 		mutex_exit(&slotinfop->slot_mutex);
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 		switch (rv) {
9510Sstevel@tonic-gate 		case HPC_ERR_INVALID:
9520Sstevel@tonic-gate 			rv = ENXIO;
9530Sstevel@tonic-gate 			break;
9540Sstevel@tonic-gate 		case HPC_ERR_NOTSUPPORTED:
9550Sstevel@tonic-gate 			rv = ENOTSUP;
9560Sstevel@tonic-gate 			break;
9570Sstevel@tonic-gate 		case HPC_ERR_FAILED:
9580Sstevel@tonic-gate 			rv = EIO;
9590Sstevel@tonic-gate 			break;
9600Sstevel@tonic-gate 		}
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 		break;
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	case DEVCTL_AP_CONFIGURE:
9650Sstevel@tonic-gate 		/*
9660Sstevel@tonic-gate 		 * **************************************
9670Sstevel@tonic-gate 		 * CONFIGURE the occupant in the slot.
9680Sstevel@tonic-gate 		 * **************************************
9690Sstevel@tonic-gate 		 */
970881Sjohnny 		slotinfop = &pcihp_p->slotinfo[pci_dev];
971881Sjohnny 
972881Sjohnny 		mutex_enter(&slotinfop->slot_mutex);
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 		rv = pcihp_configure_ap(pcihp_p, pci_dev);
9750Sstevel@tonic-gate 		if (rv == HPC_SUCCESS) {
9760Sstevel@tonic-gate 			pcihp_gen_sysevent(slotinfop->name,
9770Sstevel@tonic-gate 			    PCIHP_DR_AP_STATE_CHANGE,
9780Sstevel@tonic-gate 			    SE_NO_HINT, pcihp_p->dip, KM_SLEEP);
9790Sstevel@tonic-gate 			pcihp_create_occupant_props(self, dev, pci_dev);
9800Sstevel@tonic-gate 		}
981881Sjohnny 		mutex_exit(&slotinfop->slot_mutex);
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 		break;
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	case DEVCTL_AP_UNCONFIGURE:
9860Sstevel@tonic-gate 		/*
9870Sstevel@tonic-gate 		 * **************************************
9880Sstevel@tonic-gate 		 * UNCONFIGURE the occupant in the slot.
9890Sstevel@tonic-gate 		 * **************************************
9900Sstevel@tonic-gate 		 */
991881Sjohnny 		slotinfop = &pcihp_p->slotinfo[pci_dev];
992881Sjohnny 
993881Sjohnny 		mutex_enter(&slotinfop->slot_mutex);
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 		rv = pcihp_unconfigure_ap(pcihp_p, pci_dev);
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 		if (rv == HPC_SUCCESS) {
9980Sstevel@tonic-gate 			pcihp_gen_sysevent(slotinfop->name,
9990Sstevel@tonic-gate 			    PCIHP_DR_AP_STATE_CHANGE,
10000Sstevel@tonic-gate 			    SE_NO_HINT, pcihp_p->dip, KM_SLEEP);
10010Sstevel@tonic-gate 			pcihp_delete_occupant_props(pcihp_p->dip, dev);
10020Sstevel@tonic-gate 		}
1003881Sjohnny 		mutex_exit(&slotinfop->slot_mutex);
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 		break;
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 	case DEVCTL_AP_GETSTATE:
10080Sstevel@tonic-gate 	    {
10090Sstevel@tonic-gate 		int mutex_held;
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 		/*
10120Sstevel@tonic-gate 		 * return the state of Attachment Point.
10130Sstevel@tonic-gate 		 *
10140Sstevel@tonic-gate 		 * If the occupant is in UNCONFIGURED state then
10150Sstevel@tonic-gate 		 * we should get the receptacle state from the
10160Sstevel@tonic-gate 		 * HPC driver because the receptacle state
10170Sstevel@tonic-gate 		 * maintained in the nexus may not be accurate.
10180Sstevel@tonic-gate 		 */
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 		/*
10210Sstevel@tonic-gate 		 * check for valid request:
10220Sstevel@tonic-gate 		 *	1. It is a hotplug slot.
10230Sstevel@tonic-gate 		 */
10240Sstevel@tonic-gate 		slotinfop = &pcihp_p->slotinfo[pci_dev];
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 		/* try to acquire the slot mutex */
10270Sstevel@tonic-gate 		mutex_held = mutex_tryenter(&slotinfop->slot_mutex);
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 		if (pci_dev >= PCI_MAX_DEVS || slotinfop->slot_hdl == NULL) {
10300Sstevel@tonic-gate 			rv = ENXIO;
10310Sstevel@tonic-gate 			if (mutex_held) {
10320Sstevel@tonic-gate 				mutex_exit(&slotinfop->slot_mutex);
10330Sstevel@tonic-gate 			}
10340Sstevel@tonic-gate 			break;
10350Sstevel@tonic-gate 		}
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 		if (slotinfop->ostate == AP_OSTATE_UNCONFIGURED) {
10380Sstevel@tonic-gate 		    if (hpc_nexus_control(slotinfop->slot_hdl,
10390Sstevel@tonic-gate 			HPC_CTRL_GET_SLOT_STATE, (caddr_t)&rstate) != 0) {
10400Sstevel@tonic-gate 			rv = EIO;
10410Sstevel@tonic-gate 			if (mutex_held)
10420Sstevel@tonic-gate 			    mutex_exit(&slotinfop->slot_mutex);
10430Sstevel@tonic-gate 			break;
10440Sstevel@tonic-gate 		    }
10450Sstevel@tonic-gate 		    slotinfop->rstate = (ap_rstate_t)rstate;
10460Sstevel@tonic-gate 		}
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 		ap_state.ap_rstate = slotinfop->rstate;
10490Sstevel@tonic-gate 		ap_state.ap_ostate = slotinfop->ostate;
10500Sstevel@tonic-gate 		ap_state.ap_condition = slotinfop->condition;
10510Sstevel@tonic-gate 		ap_state.ap_last_change = slotinfop->last_change;
10520Sstevel@tonic-gate 		ap_state.ap_error_code = 0; /* XXX */
10530Sstevel@tonic-gate 		if (mutex_held)
10540Sstevel@tonic-gate 			ap_state.ap_in_transition = 0; /* AP is not busy */
10550Sstevel@tonic-gate 		else
10560Sstevel@tonic-gate 			ap_state.ap_in_transition = 1; /* AP is busy */
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 		if (mutex_held)
10590Sstevel@tonic-gate 			mutex_exit(&slotinfop->slot_mutex);
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 		/* copy the return-AP-state information to the user space */
10620Sstevel@tonic-gate 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS)
10630Sstevel@tonic-gate 			rv = EFAULT;
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 		break;
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	    }
10680Sstevel@tonic-gate 	case DEVCTL_AP_CONTROL:
10690Sstevel@tonic-gate 		/*
10700Sstevel@tonic-gate 		 * HPC control functions:
10710Sstevel@tonic-gate 		 *	HPC_CTRL_ENABLE_SLOT/HPC_CTRL_DISABLE_SLOT
10720Sstevel@tonic-gate 		 *		Changes the state of the slot and preserves
10730Sstevel@tonic-gate 		 *		the state across the reboot.
10740Sstevel@tonic-gate 		 *	HPC_CTRL_ENABLE_AUTOCFG/HPC_CTRL_DISABLE_AUTOCFG
10750Sstevel@tonic-gate 		 *		Enables or disables the auto configuration
10760Sstevel@tonic-gate 		 *		of hot plugged occupant if the hardware
10770Sstevel@tonic-gate 		 *		supports notification of the hot plug
10780Sstevel@tonic-gate 		 *		events.
10790Sstevel@tonic-gate 		 *	HPC_CTRL_GET_LED_STATE/HPC_CTRL_SET_LED_STATE
10800Sstevel@tonic-gate 		 *		Controls the state of an LED.
10810Sstevel@tonic-gate 		 *	HPC_CTRL_GET_SLOT_INFO
10820Sstevel@tonic-gate 		 *		Get slot information data structure
10830Sstevel@tonic-gate 		 *		(hpc_slot_info_t).
10840Sstevel@tonic-gate 		 *	HPC_CTRL_GET_BOARD_TYPE
10850Sstevel@tonic-gate 		 *		Get board type information (hpc_board_type_t).
10860Sstevel@tonic-gate 		 *	HPC_CTRL_GET_CARD_INFO
10870Sstevel@tonic-gate 		 *		Get card information (hpc_card_info_t).
10880Sstevel@tonic-gate 		 *
10890Sstevel@tonic-gate 		 * These control functions are used by the cfgadm plug-in
10900Sstevel@tonic-gate 		 * to implement "-x" and "-v" options.
10910Sstevel@tonic-gate 		 */
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 		/* copy user ioctl data first */
10940Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
10950Sstevel@tonic-gate 		if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
10960Sstevel@tonic-gate 			struct hpc_control32_data hpc_ctrldata32;
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 			if (copyin((void *)arg, (void *)&hpc_ctrldata32,
10990Sstevel@tonic-gate 				sizeof (struct hpc_control32_data)) != 0) {
11000Sstevel@tonic-gate 				rv = EFAULT;
11010Sstevel@tonic-gate 				break;
11020Sstevel@tonic-gate 			}
11030Sstevel@tonic-gate 			hpc_ctrldata.cmd = hpc_ctrldata32.cmd;
11040Sstevel@tonic-gate 			hpc_ctrldata.data =
11050Sstevel@tonic-gate 				(void *)(intptr_t)hpc_ctrldata32.data;
11060Sstevel@tonic-gate 		}
11070Sstevel@tonic-gate #else
11080Sstevel@tonic-gate 		if (copyin((void *)arg, (void *)&hpc_ctrldata,
11090Sstevel@tonic-gate 			sizeof (struct hpc_control_data)) != 0) {
11100Sstevel@tonic-gate 			rv = EFAULT;
11110Sstevel@tonic-gate 			break;
11120Sstevel@tonic-gate 		}
11130Sstevel@tonic-gate #endif
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 		/*
11160Sstevel@tonic-gate 		 * check for valid request:
11170Sstevel@tonic-gate 		 *	1. It is a hotplug slot.
11180Sstevel@tonic-gate 		 */
11190Sstevel@tonic-gate 		slotinfop = &pcihp_p->slotinfo[pci_dev];
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 		mutex_enter(&slotinfop->slot_mutex);
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 		if (pci_dev >= PCI_MAX_DEVS || slotinfop->slot_hdl == NULL) {
11240Sstevel@tonic-gate 			rv = ENXIO;
11250Sstevel@tonic-gate 			mutex_exit(&slotinfop->slot_mutex);
11260Sstevel@tonic-gate 			break;
11270Sstevel@tonic-gate 		}
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 		switch (hpc_ctrldata.cmd) {
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 		case HPC_CTRL_GET_LED_STATE:
11320Sstevel@tonic-gate 			/* copy the led info from the user space */
11330Sstevel@tonic-gate 			if (copyin(hpc_ctrldata.data, (void *)&led_info,
11340Sstevel@tonic-gate 					sizeof (hpc_led_info_t)) != 0) {
11350Sstevel@tonic-gate 				rv = EFAULT;
11360Sstevel@tonic-gate 				break;
11370Sstevel@tonic-gate 			}
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 			/* get the state of LED information */
11400Sstevel@tonic-gate 			if (hpc_nexus_control(slotinfop->slot_hdl,
11410Sstevel@tonic-gate 				HPC_CTRL_GET_LED_STATE,
11420Sstevel@tonic-gate 				(caddr_t)&led_info) != 0) {
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 				if (rv != ENOTSUP)
11450Sstevel@tonic-gate 					rv = EIO;
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 				break;
11480Sstevel@tonic-gate 			}
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 			/* copy the led info to the user space */
11510Sstevel@tonic-gate 			if (copyout((void *)&led_info,
11520Sstevel@tonic-gate 			    hpc_ctrldata.data,
11530Sstevel@tonic-gate 			    sizeof (hpc_led_info_t)) != 0) {
11540Sstevel@tonic-gate 			    rv = EFAULT;
11550Sstevel@tonic-gate 			    break;
11560Sstevel@tonic-gate 			}
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 			break;
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 		case HPC_CTRL_SET_LED_STATE:
11610Sstevel@tonic-gate 			/* copy the led info from the user space */
11620Sstevel@tonic-gate 			if (copyin(hpc_ctrldata.data, (void *)&led_info,
11630Sstevel@tonic-gate 			    sizeof (hpc_led_info_t)) != 0) {
11640Sstevel@tonic-gate 			    rv = EFAULT;
11650Sstevel@tonic-gate 			    break;
11660Sstevel@tonic-gate 			}
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 			/* set the state of an LED */
11690Sstevel@tonic-gate 			rv = hpc_nexus_control(slotinfop->slot_hdl,
11700Sstevel@tonic-gate 			    HPC_CTRL_SET_LED_STATE, (caddr_t)&led_info);
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 			/*
11730Sstevel@tonic-gate 			 * If the Hotswap Controller does not support
11740Sstevel@tonic-gate 			 * LED management (as you would find typically
11750Sstevel@tonic-gate 			 * in the cPCI industry), then we handle the
11760Sstevel@tonic-gate 			 * blue LED on/off/blink operations, just in
11770Sstevel@tonic-gate 			 * case it helps slot identification.
11780Sstevel@tonic-gate 			 */
11790Sstevel@tonic-gate 			if ((rv == HPC_ERR_NOTSUPPORTED) &&
11800Sstevel@tonic-gate 				(slotinfop->slot_type & HPC_SLOT_TYPE_CPCI)) {
11810Sstevel@tonic-gate 				if (led_info.led != HPC_ATTN_LED)
11820Sstevel@tonic-gate 					break;
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 				switch (led_info.state) {
11850Sstevel@tonic-gate 					case HPC_LED_OFF:
11860Sstevel@tonic-gate 						pcihp_hs_csr_op(pcihp_p,
11870Sstevel@tonic-gate 						pci_dev,
11880Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_OFF);
11890Sstevel@tonic-gate 						rv = 0;
11900Sstevel@tonic-gate 						break;
11910Sstevel@tonic-gate 					case HPC_LED_ON:
11920Sstevel@tonic-gate 						/*
11930Sstevel@tonic-gate 						 * Please note that leaving
11940Sstevel@tonic-gate 						 * LED ON could be dangerous
11950Sstevel@tonic-gate 						 * as it means it is Ok to
11960Sstevel@tonic-gate 						 * remove the board, which
11970Sstevel@tonic-gate 						 * is not what we want to
11980Sstevel@tonic-gate 						 * convey. So it is upto the
11990Sstevel@tonic-gate 						 * user to take care of this
12000Sstevel@tonic-gate 						 * situation and usage.
12010Sstevel@tonic-gate 						 *
12020Sstevel@tonic-gate 						 * Normally, a Blink command
12030Sstevel@tonic-gate 						 * is more appropriate for
12040Sstevel@tonic-gate 						 * identifying a board.
12050Sstevel@tonic-gate 						 */
12060Sstevel@tonic-gate 						pcihp_hs_csr_op(pcihp_p,
12070Sstevel@tonic-gate 						pci_dev,
12080Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_ON);
12090Sstevel@tonic-gate 						rv = 0;
12100Sstevel@tonic-gate 						break;
12110Sstevel@tonic-gate 					case HPC_LED_BLINK:
12120Sstevel@tonic-gate 					{
12130Sstevel@tonic-gate 						int bl;
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 						for (bl = 0; bl < 2; bl++) {
12160Sstevel@tonic-gate 						pcihp_hs_csr_op(pcihp_p,
12170Sstevel@tonic-gate 						pci_dev,
12180Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_ON);
12190Sstevel@tonic-gate 						delay(pcihp_cpci_led_blink);
12200Sstevel@tonic-gate 						pcihp_hs_csr_op(pcihp_p,
12210Sstevel@tonic-gate 						pci_dev,
12220Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_OFF);
12230Sstevel@tonic-gate 						delay(pcihp_cpci_led_blink);
12240Sstevel@tonic-gate 						}
12250Sstevel@tonic-gate 						rv = 0;
12260Sstevel@tonic-gate 						break;
12270Sstevel@tonic-gate 					}
12280Sstevel@tonic-gate 					default:
12290Sstevel@tonic-gate 						break;
12300Sstevel@tonic-gate 				}
12310Sstevel@tonic-gate 			}
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 			if (rv == HPC_ERR_FAILED)
12340Sstevel@tonic-gate 				rv = EIO;
12350Sstevel@tonic-gate 			break;
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 		case HPC_CTRL_ENABLE_SLOT:
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 			/*
12400Sstevel@tonic-gate 			 * If slot already enabled, do not send a duplicate
12410Sstevel@tonic-gate 			 * control message to the HPC driver.
12420Sstevel@tonic-gate 			 */
12430Sstevel@tonic-gate 			if ((slotinfop->slot_flags & PCIHP_SLOT_DISABLED) == 0)
12440Sstevel@tonic-gate 				break;
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 			/* tell the HPC driver also */
12470Sstevel@tonic-gate 			if (hpc_nexus_control(slotinfop->slot_hdl,
12480Sstevel@tonic-gate 					HPC_CTRL_ENABLE_SLOT, NULL)
12490Sstevel@tonic-gate 							!= HPC_SUCCESS) {
12500Sstevel@tonic-gate 				rv = EIO;
12510Sstevel@tonic-gate 				break;
12520Sstevel@tonic-gate 			}
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 			/*
12550Sstevel@tonic-gate 			 * Enable the slot for hotplug operations.
12560Sstevel@tonic-gate 			 */
12570Sstevel@tonic-gate 			slotinfop->slot_flags &= ~PCIHP_SLOT_DISABLED;
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 			slotinfop->condition = AP_COND_UNKNOWN;
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 			/* XXX need to preserve this state across reboot? */
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 			break;
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 		case HPC_CTRL_DISABLE_SLOT:
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 			/* Do not disable if occupant configured */
12680Sstevel@tonic-gate 			if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {
12690Sstevel@tonic-gate 				rv = EAGAIN;
12700Sstevel@tonic-gate 				break;
12710Sstevel@tonic-gate 			}
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 			/* tell the HPC driver also */
12740Sstevel@tonic-gate 			if (hpc_nexus_control(slotinfop->slot_hdl,
12750Sstevel@tonic-gate 					HPC_CTRL_DISABLE_SLOT, NULL)
12760Sstevel@tonic-gate 							!= HPC_SUCCESS) {
12770Sstevel@tonic-gate 				rv = EIO;
12780Sstevel@tonic-gate 				break;
12790Sstevel@tonic-gate 			}
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 			/*
12820Sstevel@tonic-gate 			 * Disable the slot for hotplug operations.
12830Sstevel@tonic-gate 			 */
12840Sstevel@tonic-gate 			slotinfop->slot_flags |= PCIHP_SLOT_DISABLED;
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 			slotinfop->condition = AP_COND_UNUSABLE;
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 			/* XXX need to preserve this state across reboot? */
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 			break;
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 		case HPC_CTRL_ENABLE_AUTOCFG:
12930Sstevel@tonic-gate 			/*
12940Sstevel@tonic-gate 			 * Enable auto configuration on this slot.
12950Sstevel@tonic-gate 			 */
12960Sstevel@tonic-gate 			slotinfop->slot_flags |= PCIHP_SLOT_AUTO_CFG_EN;
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 			/* tell the HPC driver also */
12990Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
13000Sstevel@tonic-gate 				HPC_CTRL_ENABLE_AUTOCFG, NULL);
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI)
13030Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
13040Sstevel@tonic-gate 					HPC_EVENT_ENABLE_ENUM);
13050Sstevel@tonic-gate 			break;
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 		case HPC_CTRL_DISABLE_AUTOCFG:
13080Sstevel@tonic-gate 			/*
13090Sstevel@tonic-gate 			 * Disable auto configuration on this slot.
13100Sstevel@tonic-gate 			 */
13110Sstevel@tonic-gate 			slotinfop->slot_flags &= ~PCIHP_SLOT_AUTO_CFG_EN;
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 			/* tell the HPC driver also */
13140Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
13150Sstevel@tonic-gate 			    HPC_CTRL_DISABLE_AUTOCFG, NULL);
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI)
13180Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
13190Sstevel@tonic-gate 					HPC_EVENT_DISABLE_ENUM);
13200Sstevel@tonic-gate 			break;
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 		case HPC_CTRL_GET_BOARD_TYPE:
13230Sstevel@tonic-gate 		    {
13240Sstevel@tonic-gate 			hpc_board_type_t board_type;
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 			/*
13270Sstevel@tonic-gate 			 * Get board type data structure, hpc_board_type_t.
13280Sstevel@tonic-gate 			 */
13290Sstevel@tonic-gate 			board_type = pcihp_get_board_type(slotinfop);
13300Sstevel@tonic-gate 			if (board_type == -1) {
13310Sstevel@tonic-gate 			    rv = ENXIO;
13320Sstevel@tonic-gate 			    break;
13330Sstevel@tonic-gate 			}
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 			/* copy the board type info to the user space */
13360Sstevel@tonic-gate 			if (copyout((void *)&board_type, hpc_ctrldata.data,
13370Sstevel@tonic-gate 			    sizeof (hpc_board_type_t)) != 0) {
13380Sstevel@tonic-gate 			    rv = ENXIO;
13390Sstevel@tonic-gate 			    break;
13400Sstevel@tonic-gate 			}
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 			break;
13430Sstevel@tonic-gate 		    }
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 		case HPC_CTRL_GET_SLOT_INFO:
13460Sstevel@tonic-gate 		    {
13470Sstevel@tonic-gate 			hpc_slot_info_t slot_info;
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 			/*
13500Sstevel@tonic-gate 			 * Get slot information structure, hpc_slot_info_t.
13510Sstevel@tonic-gate 			 */
13520Sstevel@tonic-gate 			slot_info.version = HPC_SLOT_INFO_VERSION;
13530Sstevel@tonic-gate 			slot_info.slot_type = slotinfop->slot_type;
13540Sstevel@tonic-gate 			slot_info.pci_slot_capabilities =
13550Sstevel@tonic-gate 					slotinfop->slot_capabilities;
13560Sstevel@tonic-gate 			slot_info.pci_dev_num = (uint16_t)pci_dev;
13570Sstevel@tonic-gate 			(void) strcpy(slot_info.pci_slot_name, slotinfop->name);
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 			/* copy the slot info structure to the user space */
13600Sstevel@tonic-gate 			if (copyout((void *)&slot_info, hpc_ctrldata.data,
13610Sstevel@tonic-gate 			    sizeof (hpc_slot_info_t)) != 0) {
13620Sstevel@tonic-gate 			    rv = EFAULT;
13630Sstevel@tonic-gate 			    break;
13640Sstevel@tonic-gate 			}
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 			break;
13670Sstevel@tonic-gate 		    }
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 		case HPC_CTRL_GET_CARD_INFO:
13700Sstevel@tonic-gate 		    {
13710Sstevel@tonic-gate 			hpc_card_info_t card_info;
13720Sstevel@tonic-gate 			ddi_acc_handle_t handle;
13730Sstevel@tonic-gate 			dev_info_t *cdip;
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 			/*
13760Sstevel@tonic-gate 			 * Get card information structure, hpc_card_info_t.
13770Sstevel@tonic-gate 			 */
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate 			/* verify that the card is configured */
13800Sstevel@tonic-gate 			if ((slotinfop->ostate != AP_OSTATE_CONFIGURED) ||
13810Sstevel@tonic-gate 			    ((cdip = pcihp_devi_find(self, pci_dev,
13820Sstevel@tonic-gate 							0)) == NULL)) {
13830Sstevel@tonic-gate 			    /* either the card is not present or */
13840Sstevel@tonic-gate 			    /* it is not configured.		 */
13850Sstevel@tonic-gate 			    rv = ENXIO;
13860Sstevel@tonic-gate 			    break;
13870Sstevel@tonic-gate 			}
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate 			/*
13900Sstevel@tonic-gate 			 * If declared failed, don't allow Config operations.
13910Sstevel@tonic-gate 			 * Otherwise, if good or failing, it is assumed Ok
13920Sstevel@tonic-gate 			 * to get config data.
13930Sstevel@tonic-gate 			 */
13940Sstevel@tonic-gate 			if (slotinfop->condition == AP_COND_FAILED) {
13950Sstevel@tonic-gate 				rv = EIO;
13960Sstevel@tonic-gate 				break;
13970Sstevel@tonic-gate 			}
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 			/* get the information from the PCI config header */
14000Sstevel@tonic-gate 			/* for the function 0.				  */
14010Sstevel@tonic-gate 			if (pci_config_setup(cdip, &handle) != DDI_SUCCESS) {
14020Sstevel@tonic-gate 				rv = EIO;
14030Sstevel@tonic-gate 				break;
14040Sstevel@tonic-gate 			}
14050Sstevel@tonic-gate 			card_info.prog_class = pci_config_get8(handle,
14060Sstevel@tonic-gate 						PCI_CONF_PROGCLASS);
14070Sstevel@tonic-gate 			card_info.base_class = pci_config_get8(handle,
14080Sstevel@tonic-gate 						PCI_CONF_BASCLASS);
14090Sstevel@tonic-gate 			card_info.sub_class = pci_config_get8(handle,
14100Sstevel@tonic-gate 						PCI_CONF_SUBCLASS);
14110Sstevel@tonic-gate 			card_info.header_type = pci_config_get8(handle,
14120Sstevel@tonic-gate 						PCI_CONF_HEADER);
14130Sstevel@tonic-gate 			pci_config_teardown(&handle);
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate 			/* copy the card info structure to the user space */
14160Sstevel@tonic-gate 			if (copyout((void *)&card_info, hpc_ctrldata.data,
14170Sstevel@tonic-gate 			    sizeof (hpc_card_info_t)) != 0) {
14180Sstevel@tonic-gate 			    rv = EFAULT;
14190Sstevel@tonic-gate 			    break;
14200Sstevel@tonic-gate 			}
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate 			break;
14230Sstevel@tonic-gate 		    }
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 		default:
14260Sstevel@tonic-gate 			rv = EINVAL;
14270Sstevel@tonic-gate 			break;
14280Sstevel@tonic-gate 		}
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 		mutex_exit(&slotinfop->slot_mutex);
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 		break;
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 	default:
14350Sstevel@tonic-gate 		rv = ENOTTY;
14360Sstevel@tonic-gate 	}
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 	if (cmd != DEVCTL_AP_CONTROL)
14390Sstevel@tonic-gate 		ndi_dc_freehdl(dcp);
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate 	(void) pcihp_get_soft_state(self, state_unlocking, &rval);
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 	return (rv);
14440Sstevel@tonic-gate }
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate /*
14470Sstevel@tonic-gate  * **************************************
14480Sstevel@tonic-gate  * CONFIGURE the occupant in the slot.
14490Sstevel@tonic-gate  * **************************************
14500Sstevel@tonic-gate  */
14510Sstevel@tonic-gate static int
14520Sstevel@tonic-gate pcihp_configure_ap(pcihp_t *pcihp_p, int pci_dev)
14530Sstevel@tonic-gate {
14540Sstevel@tonic-gate 	dev_info_t *self = pcihp_p->dip;
14550Sstevel@tonic-gate 	int rv = HPC_SUCCESS;
14560Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
14570Sstevel@tonic-gate 	hpc_slot_state_t rstate;
14580Sstevel@tonic-gate 	struct pcihp_config_ctrl ctrl;
14590Sstevel@tonic-gate 	int circular_count;
14600Sstevel@tonic-gate 	time_t time;
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 	/*
14630Sstevel@tonic-gate 	 * check for valid request:
14640Sstevel@tonic-gate 	 *	1. It is a hotplug slot.
14650Sstevel@tonic-gate 	 *	2. The receptacle is in the CONNECTED state.
14660Sstevel@tonic-gate 	 */
14670Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
14680Sstevel@tonic-gate 
1469881Sjohnny 
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 	if ((pci_dev >= PCI_MAX_DEVS) || (slotinfop->slot_hdl == NULL) ||
14720Sstevel@tonic-gate 			(slotinfop->slot_flags & PCIHP_SLOT_DISABLED)) {
1473881Sjohnny 
14740Sstevel@tonic-gate 		return (ENXIO);
14750Sstevel@tonic-gate 	}
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate 	/*
14780Sstevel@tonic-gate 	 * If the occupant is already in (partially?) configured
14790Sstevel@tonic-gate 	 * state then call the ndi_devi_online() on the device
14800Sstevel@tonic-gate 	 * subtree(s) for this attachment point.
14810Sstevel@tonic-gate 	 */
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate 	if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {
14840Sstevel@tonic-gate 		ctrl.flags = PCIHP_CFG_CONTINUE;
14850Sstevel@tonic-gate 		ctrl.rv = NDI_SUCCESS;
14860Sstevel@tonic-gate 		ctrl.dip = NULL;
14870Sstevel@tonic-gate 		ctrl.pci_dev = pci_dev;
14880Sstevel@tonic-gate 		ctrl.op = PCIHP_ONLINE;
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 		ndi_devi_enter(self, &circular_count);
14910Sstevel@tonic-gate 		ddi_walk_devs(ddi_get_child(self), pcihp_configure,
14920Sstevel@tonic-gate 			(void *)&ctrl);
14930Sstevel@tonic-gate 		ndi_devi_exit(self, circular_count);
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 		if (ctrl.rv != NDI_SUCCESS) {
14960Sstevel@tonic-gate 			/*
14970Sstevel@tonic-gate 			 * one or more of the devices are not
14980Sstevel@tonic-gate 			 * onlined. How is this to be reported?
14990Sstevel@tonic-gate 			 */
15000Sstevel@tonic-gate 			cmn_err(CE_WARN,
15010Sstevel@tonic-gate 				"pcihp (%s%d): failed to attach one or"
15020Sstevel@tonic-gate 				" more drivers for the card in the slot %s",
15030Sstevel@tonic-gate 				ddi_driver_name(self), ddi_get_instance(self),
15040Sstevel@tonic-gate 				slotinfop->name);
15050Sstevel@tonic-gate 			/* rv = EFAULT; */
15060Sstevel@tonic-gate 		}
15070Sstevel@tonic-gate 		/* tell HPC driver that the occupant is configured */
15080Sstevel@tonic-gate 		(void) hpc_nexus_control(slotinfop->slot_hdl,
15090Sstevel@tonic-gate 			HPC_CTRL_DEV_CONFIGURED, NULL);
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 		if (drv_getparm(TIME, (void *)&time) != DDI_SUCCESS)
15120Sstevel@tonic-gate 			slotinfop->last_change = (time_t)-1;
15130Sstevel@tonic-gate 		else
15140Sstevel@tonic-gate 			slotinfop->last_change = (time32_t)time;
15150Sstevel@tonic-gate 
1516881Sjohnny 
15170Sstevel@tonic-gate 		return (rv);
15180Sstevel@tonic-gate 	}
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	/*
15210Sstevel@tonic-gate 	 * Occupant is in the UNCONFIGURED state.
15220Sstevel@tonic-gate 	 */
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 	/* Check if the receptacle is in the CONNECTED state. */
15250Sstevel@tonic-gate 	if (hpc_nexus_control(slotinfop->slot_hdl,
15260Sstevel@tonic-gate 			HPC_CTRL_GET_SLOT_STATE, (caddr_t)&rstate) != 0) {
1527881Sjohnny 
15280Sstevel@tonic-gate 		return (ENXIO);
15290Sstevel@tonic-gate 	}
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate 	if (rstate == HPC_SLOT_EMPTY) {
15320Sstevel@tonic-gate 		/* error. slot is empty */
1533881Sjohnny 
15340Sstevel@tonic-gate 		return (ENXIO);
15350Sstevel@tonic-gate 	}
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 	if (rstate != HPC_SLOT_CONNECTED) {
15380Sstevel@tonic-gate 		/* error. either the slot is empty or connect failed */
1539881Sjohnny 
15400Sstevel@tonic-gate 		return (ENXIO);
15410Sstevel@tonic-gate 	}
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 	slotinfop->rstate = AP_RSTATE_CONNECTED; /* record rstate */
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 	/* Turn INS and LED off, and start configuration. */
15460Sstevel@tonic-gate 	if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
15470Sstevel@tonic-gate 		pcihp_hs_csr_op(pcihp_p, pci_dev,
15480Sstevel@tonic-gate 				HPC_EVENT_SLOT_CONFIGURE);
15490Sstevel@tonic-gate 		if (pcihp_cpci_blue_led)
15500Sstevel@tonic-gate 			pcihp_hs_csr_op(pcihp_p, pci_dev,
15510Sstevel@tonic-gate 				HPC_EVENT_SLOT_BLUE_LED_OFF);
15520Sstevel@tonic-gate 		slotinfop->slot_flags &= ~PCIHP_SLOT_ENUM_INS_PENDING;
15530Sstevel@tonic-gate 	}
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 	(void) hpc_nexus_control(slotinfop->slot_hdl,
15560Sstevel@tonic-gate 		HPC_CTRL_DEV_CONFIG_START, NULL);
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	/*
15590Sstevel@tonic-gate 	 * Call the configurator to configure the card.
15600Sstevel@tonic-gate 	 */
15610Sstevel@tonic-gate 	if (pcicfg_configure(self, pci_dev) != PCICFG_SUCCESS) {
15620Sstevel@tonic-gate 		if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
15630Sstevel@tonic-gate 			if (pcihp_cpci_blue_led)
15640Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
15650Sstevel@tonic-gate 					HPC_EVENT_SLOT_BLUE_LED_ON);
15660Sstevel@tonic-gate 			pcihp_hs_csr_op(pcihp_p, pci_dev,
15670Sstevel@tonic-gate 				HPC_EVENT_SLOT_UNCONFIGURE);
15680Sstevel@tonic-gate 		}
15690Sstevel@tonic-gate 		/* tell HPC driver occupant configure Error */
15700Sstevel@tonic-gate 		(void) hpc_nexus_control(slotinfop->slot_hdl,
15710Sstevel@tonic-gate 			HPC_CTRL_DEV_CONFIG_FAILURE, NULL);
1572881Sjohnny 
15730Sstevel@tonic-gate 		return (EIO);
15740Sstevel@tonic-gate 	}
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 	/* record the occupant state as CONFIGURED */
15770Sstevel@tonic-gate 	slotinfop->ostate = AP_OSTATE_CONFIGURED;
15780Sstevel@tonic-gate 	slotinfop->condition = AP_COND_OK;
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate 	/* now, online all the devices in the AP */
15810Sstevel@tonic-gate 	ctrl.flags = PCIHP_CFG_CONTINUE;
15820Sstevel@tonic-gate 	ctrl.rv = NDI_SUCCESS;
15830Sstevel@tonic-gate 	ctrl.dip = NULL;
15840Sstevel@tonic-gate 	ctrl.pci_dev = pci_dev;
15850Sstevel@tonic-gate 	ctrl.op = PCIHP_ONLINE;
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 	ndi_devi_enter(self, &circular_count);
15880Sstevel@tonic-gate 	ddi_walk_devs(ddi_get_child(self), pcihp_configure,
15890Sstevel@tonic-gate 		(void *)&ctrl);
15900Sstevel@tonic-gate 	ndi_devi_exit(self, circular_count);
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 	if (ctrl.rv != NDI_SUCCESS) {
15930Sstevel@tonic-gate 		/*
15940Sstevel@tonic-gate 		 * one or more of the devices are not
15950Sstevel@tonic-gate 		 * ONLINE'd. How is this to be
15960Sstevel@tonic-gate 		 * reported?
15970Sstevel@tonic-gate 		 */
15980Sstevel@tonic-gate 		cmn_err(CE_WARN,
15990Sstevel@tonic-gate 			"pcihp (%s%d): failed to attach one or"
16000Sstevel@tonic-gate 			" more drivers for the card in"
16010Sstevel@tonic-gate 			" the slot %s",
16020Sstevel@tonic-gate 			ddi_driver_name(pcihp_p->dip),
16030Sstevel@tonic-gate 			ddi_get_instance(pcihp_p->dip),
16040Sstevel@tonic-gate 			slotinfop->name);
16050Sstevel@tonic-gate 		/* rv = EFAULT; */
16060Sstevel@tonic-gate 	}
16070Sstevel@tonic-gate 	/* store HS_CSR location.  No events, jut a read operation. */
16080Sstevel@tonic-gate 	if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI)
16090Sstevel@tonic-gate 		pcihp_hs_csr_op(pcihp_p, pci_dev, -1);
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	/* tell HPC driver that the occupant is configured */
16120Sstevel@tonic-gate 	(void) hpc_nexus_control(slotinfop->slot_hdl,
16130Sstevel@tonic-gate 		HPC_CTRL_DEV_CONFIGURED, NULL);
16140Sstevel@tonic-gate 
1615881Sjohnny 
16160Sstevel@tonic-gate 	return (rv);
16170Sstevel@tonic-gate }
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate /*
16200Sstevel@tonic-gate  * **************************************
16210Sstevel@tonic-gate  * UNCONFIGURE the occupant in the slot.
16220Sstevel@tonic-gate  * **************************************
16230Sstevel@tonic-gate  */
16240Sstevel@tonic-gate static int
16250Sstevel@tonic-gate pcihp_unconfigure_ap(pcihp_t *pcihp_p, int pci_dev)
16260Sstevel@tonic-gate {
16270Sstevel@tonic-gate 	dev_info_t *self = pcihp_p->dip;
16280Sstevel@tonic-gate 	int rv = HPC_SUCCESS;
16290Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
16300Sstevel@tonic-gate 	struct pcihp_config_ctrl ctrl;
16310Sstevel@tonic-gate 	int circular_count;
16320Sstevel@tonic-gate 	time_t time;
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 	/*
16350Sstevel@tonic-gate 	 * check for valid request:
16360Sstevel@tonic-gate 	 *	1. It is a hotplug slot.
16370Sstevel@tonic-gate 	 *	2. The occupant is in the CONFIGURED state.
16380Sstevel@tonic-gate 	 */
16390Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
16400Sstevel@tonic-gate 
1641881Sjohnny 
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 	if ((pci_dev >= PCI_MAX_DEVS) || (slotinfop->slot_hdl == NULL) ||
16440Sstevel@tonic-gate 			(slotinfop->slot_flags & PCIHP_SLOT_DISABLED)) {
1645881Sjohnny 
16460Sstevel@tonic-gate 		return (ENXIO);
16470Sstevel@tonic-gate 	}
16480Sstevel@tonic-gate 	/*
16490Sstevel@tonic-gate 	 * The following may not need to be there, as we should
16500Sstevel@tonic-gate 	 * support unconfiguring of boards and free resources
16510Sstevel@tonic-gate 	 * even when the board is not hotswappable. But this is
16520Sstevel@tonic-gate 	 * the only way, we may be able to tell the system
16530Sstevel@tonic-gate 	 * administrator that it is not a hotswap board since
16540Sstevel@tonic-gate 	 * disconnect operation is never called.
16550Sstevel@tonic-gate 	 * This way we help the system administrator from not
16560Sstevel@tonic-gate 	 * accidentally removing a non hotswap board and
16570Sstevel@tonic-gate 	 * possibly destroying it. May be this behavior can
16580Sstevel@tonic-gate 	 * be a default, and can be enabled or disabled via
16590Sstevel@tonic-gate 	 * a global flag.
16600Sstevel@tonic-gate 	 */
16610Sstevel@tonic-gate 	if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
16620Sstevel@tonic-gate 		if (slotinfop->slot_flags &
16630Sstevel@tonic-gate 				PCIHP_SLOT_DEV_NON_HOTPLUG) {
16640Sstevel@tonic-gate 			/* Operation unsupported if no HS board/slot */
1665881Sjohnny 
16660Sstevel@tonic-gate 			return (ENOTSUP);
16670Sstevel@tonic-gate 		}
16680Sstevel@tonic-gate 	}
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate 	/*
16710Sstevel@tonic-gate 	 * If the occupant is in the CONFIGURED state then
16720Sstevel@tonic-gate 	 * call the configurator to unconfigure the slot.
16730Sstevel@tonic-gate 	 */
16740Sstevel@tonic-gate 	if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate 		/*
16770Sstevel@tonic-gate 		 * since potential state change is imminent mask
16780Sstevel@tonic-gate 		 * enum events to prevent the slot from being re-configured
16790Sstevel@tonic-gate 		 */
16800Sstevel@tonic-gate 		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_DISABLE_ENUM);
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 		/*
16830Sstevel@tonic-gate 		 * Detach all the drivers for the devices in the
16840Sstevel@tonic-gate 		 * slot. Call pcihp_configure() to do this.
16850Sstevel@tonic-gate 		 */
16860Sstevel@tonic-gate 		ctrl.flags = 0;
16870Sstevel@tonic-gate 		ctrl.rv = NDI_SUCCESS;
16880Sstevel@tonic-gate 		ctrl.dip = NULL;
16890Sstevel@tonic-gate 		ctrl.pci_dev = pci_dev;
16900Sstevel@tonic-gate 		ctrl.op = PCIHP_OFFLINE;
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate 		(void) devfs_clean(self, NULL, DV_CLEAN_FORCE);
16930Sstevel@tonic-gate 		ndi_devi_enter(self, &circular_count);
16940Sstevel@tonic-gate 		ddi_walk_devs(ddi_get_child(self), pcihp_configure,
16950Sstevel@tonic-gate 			(void *)&ctrl);
16960Sstevel@tonic-gate 		ndi_devi_exit(self, circular_count);
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate 		if (ctrl.rv != NDI_SUCCESS) {
16990Sstevel@tonic-gate 			/*
17000Sstevel@tonic-gate 			 * Failed to detach one or more drivers
17010Sstevel@tonic-gate 			 * Restore the state of drivers which
17020Sstevel@tonic-gate 			 * are offlined during this operation.
17030Sstevel@tonic-gate 			 */
17040Sstevel@tonic-gate 			ctrl.flags = 0;
17050Sstevel@tonic-gate 			ctrl.rv = NDI_SUCCESS;
17060Sstevel@tonic-gate 			ctrl.dip = NULL;
17070Sstevel@tonic-gate 			ctrl.pci_dev = pci_dev;
17080Sstevel@tonic-gate 			ctrl.op = PCIHP_ONLINE;
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate 			ndi_devi_enter(self, &circular_count);
17110Sstevel@tonic-gate 			ddi_walk_devs(ddi_get_child(self),
17120Sstevel@tonic-gate 				pcihp_configure, (void *)&ctrl);
17130Sstevel@tonic-gate 			ndi_devi_exit(self, circular_count);
17140Sstevel@tonic-gate 
17150Sstevel@tonic-gate 			/* tell HPC driver that the occupant is Busy */
17160Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
17170Sstevel@tonic-gate 				HPC_CTRL_DEV_UNCONFIG_FAILURE, NULL);
17180Sstevel@tonic-gate 
17190Sstevel@tonic-gate 			rv = EBUSY;
17200Sstevel@tonic-gate 		} else {
17210Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
17220Sstevel@tonic-gate 				HPC_CTRL_DEV_UNCONFIG_START, NULL);
17230Sstevel@tonic-gate 
17240Sstevel@tonic-gate 			if (pcicfg_unconfigure(self,
17250Sstevel@tonic-gate 					pci_dev) == PCICFG_SUCCESS) {
17260Sstevel@tonic-gate 			/*
17270Sstevel@tonic-gate 			 * Now that resources are freed,
17280Sstevel@tonic-gate 			 * clear EXT and Turn LED ON.
17290Sstevel@tonic-gate 			 */
17300Sstevel@tonic-gate 			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
17310Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
17320Sstevel@tonic-gate 					HPC_EVENT_SLOT_UNCONFIGURE);
17330Sstevel@tonic-gate 				if (pcihp_cpci_blue_led)
17340Sstevel@tonic-gate 					pcihp_hs_csr_op(pcihp_p, pci_dev,
17350Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_ON);
17360Sstevel@tonic-gate 				slotinfop->hs_csr_location = 0;
17370Sstevel@tonic-gate 				slotinfop->slot_flags &=
17380Sstevel@tonic-gate 					~(PCIHP_SLOT_DEV_NON_HOTPLUG|
17390Sstevel@tonic-gate 						PCIHP_SLOT_ENUM_EXT_PENDING);
17400Sstevel@tonic-gate 			}
17410Sstevel@tonic-gate 				slotinfop->ostate =
17420Sstevel@tonic-gate 						AP_OSTATE_UNCONFIGURED;
17430Sstevel@tonic-gate 				slotinfop->condition = AP_COND_UNKNOWN;
17440Sstevel@tonic-gate 				/*
17450Sstevel@tonic-gate 				 * send the notification of state change
17460Sstevel@tonic-gate 				 * to the HPC driver.
17470Sstevel@tonic-gate 				 */
17480Sstevel@tonic-gate 				(void) hpc_nexus_control(
17490Sstevel@tonic-gate 					slotinfop->slot_hdl,
17500Sstevel@tonic-gate 					HPC_CTRL_DEV_UNCONFIGURED,
17510Sstevel@tonic-gate 					NULL);
17520Sstevel@tonic-gate 			} else {
17530Sstevel@tonic-gate 				/* tell HPC driver occupant unconfigure Error */
17540Sstevel@tonic-gate 				(void) hpc_nexus_control(slotinfop->slot_hdl,
17550Sstevel@tonic-gate 					HPC_CTRL_DEV_UNCONFIG_FAILURE, NULL);
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate 				rv = EIO;
17580Sstevel@tonic-gate 			}
17590Sstevel@tonic-gate 		}
17600Sstevel@tonic-gate 	}
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	if (drv_getparm(TIME, (void *)&time) != DDI_SUCCESS)
17630Sstevel@tonic-gate 		slotinfop->last_change = (time_t)-1;
17640Sstevel@tonic-gate 	else
17650Sstevel@tonic-gate 		slotinfop->last_change = (time32_t)time;
17660Sstevel@tonic-gate 
1767881Sjohnny 
17680Sstevel@tonic-gate 
17690Sstevel@tonic-gate 	/* unmask enum events again */
17700Sstevel@tonic-gate 	if ((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) == 0) {
17710Sstevel@tonic-gate 		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_ENABLE_ENUM);
17720Sstevel@tonic-gate 	}
17730Sstevel@tonic-gate 
17740Sstevel@tonic-gate 	return (rv);
17750Sstevel@tonic-gate }
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate /*
17780Sstevel@tonic-gate  * Accessor function to return pointer to the pci hotplug
17790Sstevel@tonic-gate  * cb_ops structure.
17800Sstevel@tonic-gate  */
17810Sstevel@tonic-gate struct cb_ops *
17820Sstevel@tonic-gate pcihp_get_cb_ops()
17830Sstevel@tonic-gate {
17840Sstevel@tonic-gate 	return (&pcihp_cb_ops);
17850Sstevel@tonic-gate }
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate /*
17880Sstevel@tonic-gate  * Setup function to initialize hot plug feature. Returns DDI_SUCCESS
17890Sstevel@tonic-gate  * for successful initialization, otherwise it returns DDI_FAILURE.
17900Sstevel@tonic-gate  *
17910Sstevel@tonic-gate  * It is assumed that this this function is called from the attach()
17920Sstevel@tonic-gate  * entry point of the PCI nexus driver.
17930Sstevel@tonic-gate  */
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate int
17960Sstevel@tonic-gate pcihp_init(dev_info_t *dip)
17970Sstevel@tonic-gate {
17980Sstevel@tonic-gate 	pcihp_t *pcihp_p;
17990Sstevel@tonic-gate 	int i;
18000Sstevel@tonic-gate 	caddr_t enum_data;
18010Sstevel@tonic-gate 	int enum_size;
18020Sstevel@tonic-gate 	int rv;
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate 	mutex_enter(&pcihp_open_mutex);
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate 	/*
18070Sstevel@tonic-gate 	 * Make sure that it is not already initialized.
18080Sstevel@tonic-gate 	 */
18090Sstevel@tonic-gate 	if (pcihp_get_soft_state(dip, PCIHP_DR_NOOP, &rv) != NULL) {
18100Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: pcihp instance already initialized!",
18110Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
18120Sstevel@tonic-gate 		goto cleanup;
18130Sstevel@tonic-gate 	}
18140Sstevel@tonic-gate 
18150Sstevel@tonic-gate 	/*
18160Sstevel@tonic-gate 	 * Initialize soft state structure for the bus instance.
18170Sstevel@tonic-gate 	 */
18180Sstevel@tonic-gate 	if ((pcihp_p = pcihp_create_soft_state(dip)) == NULL) {
18190Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: can't allocate pcihp structure",
18200Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
18210Sstevel@tonic-gate 		goto cleanup;
18220Sstevel@tonic-gate 	}
18230Sstevel@tonic-gate 
18240Sstevel@tonic-gate 	pcihp_p->soft_state = PCIHP_SOFT_STATE_CLOSED;
18250Sstevel@tonic-gate 	/* XXX if bus is running at 66Mhz then set PCI_BUS_66MHZ bit */
18260Sstevel@tonic-gate 	pcihp_p->bus_flags = 0;	/* XXX FIX IT */
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate 	/*
18290Sstevel@tonic-gate 	 * If a platform wishes to implement Radial ENUM# routing
18300Sstevel@tonic-gate 	 * a property "enum-impl" must be presented to us with a
18310Sstevel@tonic-gate 	 * string value "radial".
18320Sstevel@tonic-gate 	 * This helps us not go for polling operation (default)
18330Sstevel@tonic-gate 	 * during a ENUM# event.
18340Sstevel@tonic-gate 	 */
18350Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, 0, "enum-impl",
18360Sstevel@tonic-gate 	    (caddr_t)&enum_data, &enum_size) == DDI_PROP_SUCCESS) {
18370Sstevel@tonic-gate 		if (strcmp(enum_data, "radial") == 0) {
18380Sstevel@tonic-gate 			pcihp_p->bus_flags |= PCIHP_BUS_ENUM_RADIAL;
18390Sstevel@tonic-gate 		}
18400Sstevel@tonic-gate 		kmem_free(enum_data, enum_size);
18410Sstevel@tonic-gate 	}
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate 	for (i = 0; i < PCI_MAX_DEVS; i++) {
18440Sstevel@tonic-gate 		/* initialize slot mutex */
18450Sstevel@tonic-gate 		mutex_init(&pcihp_p->slotinfo[i].slot_mutex, NULL,
18460Sstevel@tonic-gate 						MUTEX_DRIVER, NULL);
18470Sstevel@tonic-gate 	}
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate 	/*
18500Sstevel@tonic-gate 	 *  register the bus instance with the HPS framework.
18510Sstevel@tonic-gate 	 */
18520Sstevel@tonic-gate 	if (hpc_nexus_register_bus(dip, pcihp_new_slot_state, 0) != 0) {
18530Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: failed to register the bus with HPS",
18540Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
18550Sstevel@tonic-gate 		goto cleanup1;
18560Sstevel@tonic-gate 	}
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate 	/*
18590Sstevel@tonic-gate 	 * Create the "devctl" minor for hot plug support. The minor
18600Sstevel@tonic-gate 	 * number for "devctl" node is in the same format as the AP
18610Sstevel@tonic-gate 	 * minor nodes.
18620Sstevel@tonic-gate 	 */
18630Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
18640Sstevel@tonic-gate 	    PCIHP_AP_MINOR_NUM(ddi_get_instance(dip), PCIHP_DEVCTL_MINOR),
18650Sstevel@tonic-gate 	    DDI_NT_NEXUS, 0) != DDI_SUCCESS)
18660Sstevel@tonic-gate 		goto cleanup2;
18670Sstevel@tonic-gate 
18680Sstevel@tonic-gate 	/*
18690Sstevel@tonic-gate 	 * Setup resource maps for this bus node. (Note: This can
18700Sstevel@tonic-gate 	 * be done from the attach(9E) of the nexus itself.)
18710Sstevel@tonic-gate 	 */
18720Sstevel@tonic-gate 	(void) pci_resource_setup(dip);
18730Sstevel@tonic-gate 
18740Sstevel@tonic-gate 	pcihp_p->bus_state = PCIHP_BUS_CONFIGURED;
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate 	mutex_exit(&pcihp_open_mutex);
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 	return (DDI_SUCCESS);
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate cleanup2:
18810Sstevel@tonic-gate 	(void) hpc_nexus_unregister_bus(dip);
18820Sstevel@tonic-gate cleanup1:
18830Sstevel@tonic-gate 	for (i = 0; i < PCI_MAX_DEVS; i++)
18840Sstevel@tonic-gate 		mutex_destroy(&pcihp_p->slotinfo[i].slot_mutex);
18850Sstevel@tonic-gate 	pcihp_destroy_soft_state(dip);
18860Sstevel@tonic-gate cleanup:
18870Sstevel@tonic-gate 	mutex_exit(&pcihp_open_mutex);
18880Sstevel@tonic-gate 	return (DDI_FAILURE);
18890Sstevel@tonic-gate }
18900Sstevel@tonic-gate 
18910Sstevel@tonic-gate /*
18920Sstevel@tonic-gate  * pcihp_uninit()
18930Sstevel@tonic-gate  *
18940Sstevel@tonic-gate  * The bus instance is going away, cleanup any data associated with
18950Sstevel@tonic-gate  * the management of hot plug slots. It is assumed that this function
18960Sstevel@tonic-gate  * is called from detach() routine of the PCI nexus driver. Also,
18970Sstevel@tonic-gate  * it is assumed that no devices on the bus are in the configured state.
18980Sstevel@tonic-gate  */
18990Sstevel@tonic-gate int
19000Sstevel@tonic-gate pcihp_uninit(dev_info_t *dip)
19010Sstevel@tonic-gate {
19020Sstevel@tonic-gate 	pcihp_t *pcihp_p;
19030Sstevel@tonic-gate 	int i, j;
19040Sstevel@tonic-gate 	int rv;
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate 	mutex_enter(&pcihp_open_mutex);
19070Sstevel@tonic-gate 	/* get a pointer to the soft state structure */
19080Sstevel@tonic-gate 	pcihp_p = pcihp_get_soft_state(dip, PCIHP_DR_BUS_UNCONFIGURE, &rv);
19090Sstevel@tonic-gate 	ASSERT(pcihp_p != NULL);
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate 	/* slot mutexes should prevent any configure/unconfigure access */
19120Sstevel@tonic-gate 	for (i = 0; i < PCI_MAX_DEVS; i++) {
19130Sstevel@tonic-gate 		if (!mutex_tryenter(&pcihp_p->slotinfo[i].slot_mutex)) {
19140Sstevel@tonic-gate 			for (j = 0; j < i; j++) {
19150Sstevel@tonic-gate 				mutex_exit(&pcihp_p->slotinfo[j].slot_mutex);
19160Sstevel@tonic-gate 			}
19170Sstevel@tonic-gate 			mutex_exit(&pcihp_open_mutex);
19180Sstevel@tonic-gate 			return (DDI_FAILURE);
19190Sstevel@tonic-gate 		}
19200Sstevel@tonic-gate 	}
19210Sstevel@tonic-gate 
19220Sstevel@tonic-gate 	if ((pcihp_p->soft_state != PCIHP_SOFT_STATE_CLOSED) ||
19230Sstevel@tonic-gate 	    (rv == PCIHP_FAILURE)) {
19240Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: pcihp instance is busy",
19250Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
19260Sstevel@tonic-gate 		for (i = 0; i < PCI_MAX_DEVS; i++) {
19270Sstevel@tonic-gate 			mutex_exit(&pcihp_p->slotinfo[i].slot_mutex);
19280Sstevel@tonic-gate 		}
19290Sstevel@tonic-gate 		mutex_exit(&pcihp_open_mutex);
19300Sstevel@tonic-gate 		return (DDI_FAILURE);
19310Sstevel@tonic-gate 	}
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 	/*
19340Sstevel@tonic-gate 	 * Unregister the bus with the HPS.
19350Sstevel@tonic-gate 	 *
19360Sstevel@tonic-gate 	 * (Note: It is assumed that the HPS framework uninstalls
19370Sstevel@tonic-gate 	 *  event handlers for all the hot plug slots on this bus.)
19380Sstevel@tonic-gate 	 */
19390Sstevel@tonic-gate 	(void) hpc_nexus_unregister_bus(dip);
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate 	/* Free up any kmem_alloc'd memory for slot info table. */
19420Sstevel@tonic-gate 	for (i = 0; i < PCI_MAX_DEVS; i++) {
19430Sstevel@tonic-gate 		/* free up slot name strings */
19440Sstevel@tonic-gate 		if (pcihp_p->slotinfo[i].name != NULL)
19450Sstevel@tonic-gate 			kmem_free(pcihp_p->slotinfo[i].name,
19460Sstevel@tonic-gate 				strlen(pcihp_p->slotinfo[i].name) + 1);
19470Sstevel@tonic-gate 	}
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate 	/* destroy slot mutexes */
19500Sstevel@tonic-gate 	for (i = 0; i < PCI_MAX_DEVS; i++)
19510Sstevel@tonic-gate 		mutex_destroy(&pcihp_p->slotinfo[i].slot_mutex);
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 	/* free up the soft state structure */
19560Sstevel@tonic-gate 	pcihp_destroy_soft_state(dip);
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 	/*
19590Sstevel@tonic-gate 	 * Destroy resource maps for this bus node. (Note: This can
19600Sstevel@tonic-gate 	 * be done from the detach(9E) of the nexus itself.)
19610Sstevel@tonic-gate 	 */
19620Sstevel@tonic-gate 	(void) pci_resource_destroy(dip);
19630Sstevel@tonic-gate 
19640Sstevel@tonic-gate 	mutex_exit(&pcihp_open_mutex);
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate 	return (DDI_SUCCESS);
19670Sstevel@tonic-gate }
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate /*
19700Sstevel@tonic-gate  * pcihp_new_slot_state()
19710Sstevel@tonic-gate  *
19720Sstevel@tonic-gate  * This function is called by the HPS when it finds a hot plug
19730Sstevel@tonic-gate  * slot is added or being removed from the hot plug framework.
19740Sstevel@tonic-gate  * It returns 0 for success and HPC_ERR_FAILED for errors.
19750Sstevel@tonic-gate  */
19760Sstevel@tonic-gate static int
19770Sstevel@tonic-gate pcihp_new_slot_state(dev_info_t *dip, hpc_slot_t hdl,
19780Sstevel@tonic-gate 	hpc_slot_info_t *slot_info, int slot_state)
19790Sstevel@tonic-gate {
19800Sstevel@tonic-gate 	pcihp_t *pcihp_p;
19810Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
19820Sstevel@tonic-gate 	int pci_dev;
19830Sstevel@tonic-gate 	minor_t ap_minor;
19840Sstevel@tonic-gate 	major_t ap_major;
19850Sstevel@tonic-gate 	int rv = 0;
19860Sstevel@tonic-gate 	time_t time;
19870Sstevel@tonic-gate 	int auto_enable = 1;
19880Sstevel@tonic-gate 	int rval;
19890Sstevel@tonic-gate 
19900Sstevel@tonic-gate 	/* get a pointer to the soft state structure */
19910Sstevel@tonic-gate 	pcihp_p = pcihp_get_soft_state(dip, PCIHP_DR_SLOT_ENTER, &rval);
19920Sstevel@tonic-gate 	ASSERT(pcihp_p != NULL);
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 	if (rval == PCIHP_FAILURE) {
19950Sstevel@tonic-gate 		PCIHP_DEBUG((CE_WARN, "pcihp instance is unconfigured"
19960Sstevel@tonic-gate 		    " while slot activity is requested\n"));
19970Sstevel@tonic-gate 		return (HPC_ERR_FAILED);
19980Sstevel@tonic-gate 	}
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate 	pci_dev = slot_info->pci_dev_num;
20010Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 	mutex_enter(&slotinfop->slot_mutex);
20040Sstevel@tonic-gate 
20050Sstevel@tonic-gate 	switch (slot_state) {
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate 	case HPC_SLOT_ONLINE:
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate 		/*
20100Sstevel@tonic-gate 		 * Make sure the slot is not already ONLINE (paranoia?).
20110Sstevel@tonic-gate 		 * (Note: Should this be simply an ASSERTION?)
20120Sstevel@tonic-gate 		 */
20130Sstevel@tonic-gate 		if (slotinfop->slot_hdl != NULL) {
20140Sstevel@tonic-gate 		    PCIHP_DEBUG((CE_WARN,
20150Sstevel@tonic-gate 			"pcihp (%s%d): pci slot (dev %x) already ONLINE!!",
20160Sstevel@tonic-gate 			ddi_driver_name(dip), ddi_get_instance(dip), pci_dev));
20170Sstevel@tonic-gate 			rv = HPC_ERR_FAILED;
20180Sstevel@tonic-gate 			break;
20190Sstevel@tonic-gate 		}
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate 		/*
20220Sstevel@tonic-gate 		 * Add the hot plug slot to the bus.
20230Sstevel@tonic-gate 		 */
20240Sstevel@tonic-gate 
20250Sstevel@tonic-gate 		/* create the AP minor node */
20260Sstevel@tonic-gate 		ap_minor = PCIHP_AP_MINOR_NUM(ddi_get_instance(dip), pci_dev);
20270Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, slot_info->pci_slot_name,
20280Sstevel@tonic-gate 			S_IFCHR, ap_minor,
20290Sstevel@tonic-gate 			DDI_NT_PCI_ATTACHMENT_POINT, 0) == DDI_FAILURE) {
20300Sstevel@tonic-gate 		    cmn_err(CE_WARN,
20310Sstevel@tonic-gate 			"pcihp (%s%d): ddi_create_minor_node failed"
20320Sstevel@tonic-gate 			" for pci dev %x", ddi_driver_name(dip),
20330Sstevel@tonic-gate 			ddi_get_instance(dip), pci_dev);
20340Sstevel@tonic-gate 		    rv = HPC_ERR_FAILED;
20350Sstevel@tonic-gate 		    break;
20360Sstevel@tonic-gate 		}
20370Sstevel@tonic-gate 
20380Sstevel@tonic-gate 		/* save the slot handle */
20390Sstevel@tonic-gate 		slotinfop->slot_hdl = hdl;
20400Sstevel@tonic-gate 
20410Sstevel@tonic-gate 		/* setup event handler for all hardware events on the slot */
20420Sstevel@tonic-gate 		ap_major = ddi_name_to_major(ddi_get_name(dip));
20430Sstevel@tonic-gate 		if (hpc_install_event_handler(hdl, -1, pcihp_event_handler,
20440Sstevel@tonic-gate 			(caddr_t)makedevice(ap_major, ap_minor)) != 0) {
20450Sstevel@tonic-gate 		    cmn_err(CE_WARN,
20460Sstevel@tonic-gate 			"pcihp (%s%d): install event handler failed"
20470Sstevel@tonic-gate 			" for pci dev %x", ddi_driver_name(dip),
20480Sstevel@tonic-gate 			ddi_get_instance(dip), pci_dev);
20490Sstevel@tonic-gate 		    rv = HPC_ERR_FAILED;
20500Sstevel@tonic-gate 		    break;
20510Sstevel@tonic-gate 		}
20520Sstevel@tonic-gate 		slotinfop->event_mask = (uint32_t)0xFFFFFFFF;
20530Sstevel@tonic-gate 
20540Sstevel@tonic-gate 		pcihp_create_occupant_props(dip, makedevice(ap_major,
20550Sstevel@tonic-gate 		    ap_minor), pci_dev);
20560Sstevel@tonic-gate 
20570Sstevel@tonic-gate 		/* set default auto configuration enabled flag for this slot */
20580Sstevel@tonic-gate 		slotinfop->slot_flags = pcihp_autocfg_enabled;
20590Sstevel@tonic-gate 
20600Sstevel@tonic-gate 		/* copy the slot information */
20610Sstevel@tonic-gate 		slotinfop->name =
20620Sstevel@tonic-gate 		    kmem_alloc(strlen(slot_info->pci_slot_name) + 1, KM_SLEEP);
20630Sstevel@tonic-gate 		(void) strcpy(slotinfop->name, slot_info->pci_slot_name);
20640Sstevel@tonic-gate 		slotinfop->slot_type = slot_info->slot_type;
20650Sstevel@tonic-gate 		slotinfop->hs_csr_location = 0;
20660Sstevel@tonic-gate 		slotinfop->slot_capabilities = slot_info->pci_slot_capabilities;
20670Sstevel@tonic-gate 		if (slot_info->slot_flags & HPC_SLOT_NO_AUTO_ENABLE)
20680Sstevel@tonic-gate 			auto_enable = 0;
20690Sstevel@tonic-gate 
20700Sstevel@tonic-gate 		if (slot_info->slot_flags & HPC_SLOT_CREATE_DEVLINK) {
20710Sstevel@tonic-gate 			pci_devlink_flags |= (1 << pci_dev);
20720Sstevel@tonic-gate 			(void) ddi_prop_update_int(DDI_DEV_T_NONE,
20730Sstevel@tonic-gate 						dip,
20740Sstevel@tonic-gate 						"ap-names",
20750Sstevel@tonic-gate 						pci_devlink_flags);
20760Sstevel@tonic-gate 		}
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE,
20790Sstevel@tonic-gate 		    "pcihp (%s%d): pci slot (dev %x) ONLINE\n",
20800Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip), pci_dev));
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate 		/*
20830Sstevel@tonic-gate 		 * The slot may have an occupant that was configured
20840Sstevel@tonic-gate 		 * at boot time. If we find a devinfo node in the tree
20850Sstevel@tonic-gate 		 * for this slot (i.e pci device number) then we
20860Sstevel@tonic-gate 		 * record the occupant state as CONFIGURED.
20870Sstevel@tonic-gate 		 */
20880Sstevel@tonic-gate 		if (pcihp_devi_find(dip, pci_dev, 0) != NULL) {
20890Sstevel@tonic-gate 			/* we have a configured occupant */
20900Sstevel@tonic-gate 			slotinfop->ostate = AP_OSTATE_CONFIGURED;
20910Sstevel@tonic-gate 			slotinfop->rstate = AP_RSTATE_CONNECTED;
20920Sstevel@tonic-gate 			slotinfop->condition = AP_COND_OK;
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate 			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
20950Sstevel@tonic-gate 				/* this will set slot flags too. */
20960Sstevel@tonic-gate 				(void) pcihp_get_board_type(slotinfop);
20970Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
20980Sstevel@tonic-gate 					HPC_EVENT_SLOT_CONFIGURE);
20990Sstevel@tonic-gate 				if (pcihp_cpci_blue_led)
21000Sstevel@tonic-gate 					pcihp_hs_csr_op(pcihp_p, pci_dev,
21010Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_OFF);
21020Sstevel@tonic-gate 				/* ENUM# enabled by default for cPCI devices */
21030Sstevel@tonic-gate 				slotinfop->slot_flags |= PCIHP_SLOT_AUTO_CFG_EN;
21040Sstevel@tonic-gate 				slotinfop->slot_flags &=
21050Sstevel@tonic-gate 					~PCIHP_SLOT_ENUM_INS_PENDING;
21060Sstevel@tonic-gate 			}
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate 			/* tell HPC driver that the occupant is configured */
21090Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
21100Sstevel@tonic-gate 				HPC_CTRL_DEV_CONFIGURED, NULL);
21110Sstevel@tonic-gate 		} else {
21120Sstevel@tonic-gate 			struct pcihp_config_ctrl ctrl;
21130Sstevel@tonic-gate 			int circular_count;
21140Sstevel@tonic-gate 
21150Sstevel@tonic-gate 			slotinfop->ostate = AP_OSTATE_UNCONFIGURED;
21160Sstevel@tonic-gate 			slotinfop->rstate = AP_RSTATE_EMPTY;
21170Sstevel@tonic-gate 			slotinfop->condition = AP_COND_UNKNOWN;
21180Sstevel@tonic-gate 
21190Sstevel@tonic-gate 			if (!auto_enable) {	/* no further action */
21200Sstevel@tonic-gate 				break;
21210Sstevel@tonic-gate 			}
21220Sstevel@tonic-gate 
21230Sstevel@tonic-gate 			/*
21240Sstevel@tonic-gate 			 * We enable power to the slot and try to
21250Sstevel@tonic-gate 			 * configure if there is any card present.
21260Sstevel@tonic-gate 			 *
21270Sstevel@tonic-gate 			 * Note: This case is possible if the BIOS or
21280Sstevel@tonic-gate 			 * firmware doesn't enable the slots during
21290Sstevel@tonic-gate 			 * soft reboot.
21300Sstevel@tonic-gate 			 */
21310Sstevel@tonic-gate 			if (hpc_nexus_connect(slotinfop->slot_hdl,
21320Sstevel@tonic-gate 				NULL, 0) != HPC_SUCCESS)
21330Sstevel@tonic-gate 				break;
21340Sstevel@tonic-gate 
21350Sstevel@tonic-gate 			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
21360Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
21370Sstevel@tonic-gate 					HPC_EVENT_SLOT_CONFIGURE);
21380Sstevel@tonic-gate 				if (pcihp_cpci_blue_led)
21390Sstevel@tonic-gate 					pcihp_hs_csr_op(pcihp_p, pci_dev,
21400Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_OFF);
21410Sstevel@tonic-gate 				slotinfop->slot_flags |= PCIHP_SLOT_AUTO_CFG_EN;
21420Sstevel@tonic-gate 				slotinfop->slot_flags &=
21430Sstevel@tonic-gate 					~PCIHP_SLOT_ENUM_INS_PENDING;
21440Sstevel@tonic-gate 			}
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
21470Sstevel@tonic-gate 				HPC_CTRL_DEV_CONFIG_START, NULL);
21480Sstevel@tonic-gate 
21490Sstevel@tonic-gate 			/*
21500Sstevel@tonic-gate 			 * Call the configurator to configure the card.
21510Sstevel@tonic-gate 			 */
21520Sstevel@tonic-gate 			if (pcicfg_configure(dip, pci_dev) != PCICFG_SUCCESS) {
21530Sstevel@tonic-gate 				if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
21540Sstevel@tonic-gate 					if (pcihp_cpci_blue_led)
21550Sstevel@tonic-gate 						pcihp_hs_csr_op(pcihp_p,
21560Sstevel@tonic-gate 						pci_dev,
21570Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_ON);
21580Sstevel@tonic-gate 					pcihp_hs_csr_op(pcihp_p, pci_dev,
21590Sstevel@tonic-gate 						HPC_EVENT_SLOT_UNCONFIGURE);
21600Sstevel@tonic-gate 				}
21610Sstevel@tonic-gate 
21620Sstevel@tonic-gate 				/* tell HPC driver occupant configure Error */
21630Sstevel@tonic-gate 				(void) hpc_nexus_control(slotinfop->slot_hdl,
21640Sstevel@tonic-gate 					HPC_CTRL_DEV_CONFIG_FAILURE, NULL);
21650Sstevel@tonic-gate 
21660Sstevel@tonic-gate 				/*
21670Sstevel@tonic-gate 				 * call HPC driver to turn off the power for
21680Sstevel@tonic-gate 				 * the slot.
21690Sstevel@tonic-gate 				 */
21700Sstevel@tonic-gate 				(void) hpc_nexus_disconnect(slotinfop->slot_hdl,
21710Sstevel@tonic-gate 							NULL, 0);
21720Sstevel@tonic-gate 			} else {
21730Sstevel@tonic-gate 			    /* record the occupant state as CONFIGURED */
21740Sstevel@tonic-gate 			    slotinfop->ostate = AP_OSTATE_CONFIGURED;
21750Sstevel@tonic-gate 			    slotinfop->rstate = AP_RSTATE_CONNECTED;
21760Sstevel@tonic-gate 			    slotinfop->condition = AP_COND_OK;
21770Sstevel@tonic-gate 
21780Sstevel@tonic-gate 			    /* now, online all the devices in the AP */
21790Sstevel@tonic-gate 			    ctrl.flags = PCIHP_CFG_CONTINUE;
21800Sstevel@tonic-gate 			    ctrl.rv = NDI_SUCCESS;
21810Sstevel@tonic-gate 			    ctrl.dip = NULL;
21820Sstevel@tonic-gate 			    ctrl.pci_dev = pci_dev;
21830Sstevel@tonic-gate 			    ctrl.op = PCIHP_ONLINE;
21840Sstevel@tonic-gate 				/*
21850Sstevel@tonic-gate 				 * the following sets slot_flags and
21860Sstevel@tonic-gate 				 * hs_csr_location too.
21870Sstevel@tonic-gate 				 */
21880Sstevel@tonic-gate 				(void) pcihp_get_board_type(slotinfop);
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate 			    ndi_devi_enter(dip, &circular_count);
21910Sstevel@tonic-gate 			    ddi_walk_devs(ddi_get_child(dip), pcihp_configure,
21920Sstevel@tonic-gate 				(void *)&ctrl);
21930Sstevel@tonic-gate 			    ndi_devi_exit(dip, circular_count);
21940Sstevel@tonic-gate 
21950Sstevel@tonic-gate 			    if (ctrl.rv != NDI_SUCCESS) {
21960Sstevel@tonic-gate 				/*
21970Sstevel@tonic-gate 				 * one or more of the devices are not
21980Sstevel@tonic-gate 				 * ONLINE'd. How is this to be
21990Sstevel@tonic-gate 				 * reported?
22000Sstevel@tonic-gate 				 */
22010Sstevel@tonic-gate 				cmn_err(CE_WARN,
22020Sstevel@tonic-gate 					"pcihp (%s%d): failed to attach one or"
22030Sstevel@tonic-gate 					" more drivers for the card in"
22040Sstevel@tonic-gate 					" the slot %s",
22050Sstevel@tonic-gate 					ddi_driver_name(dip),
22060Sstevel@tonic-gate 					ddi_get_instance(dip),
22070Sstevel@tonic-gate 					slotinfop->name);
22080Sstevel@tonic-gate 			    }
22090Sstevel@tonic-gate 
22100Sstevel@tonic-gate 			    /* tell HPC driver about the configured occupant */
22110Sstevel@tonic-gate 			    (void) hpc_nexus_control(slotinfop->slot_hdl,
22120Sstevel@tonic-gate 				HPC_CTRL_DEV_CONFIGURED, NULL);
22130Sstevel@tonic-gate 			}
22140Sstevel@tonic-gate 		}
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate 		break;
22170Sstevel@tonic-gate 
22180Sstevel@tonic-gate 	case HPC_SLOT_OFFLINE:
22190Sstevel@tonic-gate 		/*
22200Sstevel@tonic-gate 		 * A hot plug slot is being removed from the bus.
22210Sstevel@tonic-gate 		 * Make sure there is no occupant configured on the
22220Sstevel@tonic-gate 		 * slot before removing the AP minor node.
22230Sstevel@tonic-gate 		 */
22240Sstevel@tonic-gate 		if (slotinfop->ostate != AP_OSTATE_UNCONFIGURED) {
22250Sstevel@tonic-gate 		    cmn_err(CE_WARN, "pcihp (%s%d): Card is still in configured"
22260Sstevel@tonic-gate 			" state for pci dev %x",
22270Sstevel@tonic-gate 			ddi_driver_name(dip), ddi_get_instance(dip), pci_dev);
22280Sstevel@tonic-gate 		    rv = HPC_ERR_FAILED;
22290Sstevel@tonic-gate 		    break;
22300Sstevel@tonic-gate 		}
22310Sstevel@tonic-gate 
22320Sstevel@tonic-gate 		/*
22330Sstevel@tonic-gate 		 * If the AP device is in open state then return
22340Sstevel@tonic-gate 		 * error.
22350Sstevel@tonic-gate 		 */
22360Sstevel@tonic-gate 		if (pcihp_p->soft_state != PCIHP_SOFT_STATE_CLOSED) {
22370Sstevel@tonic-gate 		    rv = HPC_ERR_FAILED;
22380Sstevel@tonic-gate 		    break;
22390Sstevel@tonic-gate 		}
22400Sstevel@tonic-gate 		if (slot_info->slot_flags & HPC_SLOT_CREATE_DEVLINK) {
22410Sstevel@tonic-gate 			pci_devlink_flags &= ~(1 << pci_dev);
22420Sstevel@tonic-gate 			(void) ddi_prop_update_int(DDI_DEV_T_NONE,
22430Sstevel@tonic-gate 					dip,
22440Sstevel@tonic-gate 					"ap-names",
22450Sstevel@tonic-gate 					pci_devlink_flags);
22460Sstevel@tonic-gate 		}
22470Sstevel@tonic-gate 
22480Sstevel@tonic-gate 		/* remove the minor node */
22490Sstevel@tonic-gate 		ddi_remove_minor_node(dip, slotinfop->name);
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate 		/* free up the memory for the name string */
22520Sstevel@tonic-gate 		kmem_free(slotinfop->name, strlen(slotinfop->name) + 1);
22530Sstevel@tonic-gate 
22540Sstevel@tonic-gate 		/* update the slot info data */
22550Sstevel@tonic-gate 		slotinfop->name = NULL;
22560Sstevel@tonic-gate 		slotinfop->slot_hdl = NULL;
22570Sstevel@tonic-gate 
22580Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE,
22590Sstevel@tonic-gate 		    "pcihp (%s%d): pci slot (dev %x) OFFLINE\n",
22600Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip),
22610Sstevel@tonic-gate 		    slot_info->pci_dev_num));
22620Sstevel@tonic-gate 
22630Sstevel@tonic-gate 		break;
22640Sstevel@tonic-gate 	default:
22650Sstevel@tonic-gate 		cmn_err(CE_WARN,
22660Sstevel@tonic-gate 		    "pcihp_new_slot_state: unknown slot_state %d", slot_state);
22670Sstevel@tonic-gate 		rv = HPC_ERR_FAILED;
22680Sstevel@tonic-gate 	}
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate 	if (rv == 0) {
22710Sstevel@tonic-gate 		if (drv_getparm(TIME, (void *)&time) != DDI_SUCCESS)
22720Sstevel@tonic-gate 			slotinfop->last_change = (time_t)-1;
22730Sstevel@tonic-gate 		else
22740Sstevel@tonic-gate 			slotinfop->last_change = (time32_t)time;
22750Sstevel@tonic-gate 	}
22760Sstevel@tonic-gate 
22770Sstevel@tonic-gate 	mutex_exit(&slotinfop->slot_mutex);
22780Sstevel@tonic-gate 
22790Sstevel@tonic-gate 	(void) pcihp_get_soft_state(dip, PCIHP_DR_SLOT_EXIT, &rval);
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate 	return (rv);
22820Sstevel@tonic-gate }
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate /*
22850Sstevel@tonic-gate  * Event handler. It is assumed that this function is called from
22860Sstevel@tonic-gate  * a kernel context only.
22870Sstevel@tonic-gate  *
22880Sstevel@tonic-gate  * Parameters:
22890Sstevel@tonic-gate  *	slot_arg	AP minor number.
22900Sstevel@tonic-gate  *	event_mask	Event that occurred.
22910Sstevel@tonic-gate  */
22920Sstevel@tonic-gate 
22930Sstevel@tonic-gate static int
22940Sstevel@tonic-gate pcihp_event_handler(caddr_t slot_arg, uint_t event_mask)
22950Sstevel@tonic-gate {
22960Sstevel@tonic-gate 	dev_t ap_dev = (dev_t)slot_arg;
22970Sstevel@tonic-gate 	dev_info_t *self;
22980Sstevel@tonic-gate 	pcihp_t *pcihp_p;
22990Sstevel@tonic-gate 	int pci_dev;
23000Sstevel@tonic-gate 	int rv = HPC_EVENT_CLAIMED;
23010Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
23020Sstevel@tonic-gate 	struct pcihp_config_ctrl ctrl;
23030Sstevel@tonic-gate 	int circular_count;
23040Sstevel@tonic-gate 	int rval;
2305881Sjohnny 	int hint;
2306881Sjohnny 	hpc_slot_state_t rstate;
23070Sstevel@tonic-gate 
23080Sstevel@tonic-gate 	/*
23090Sstevel@tonic-gate 	 * Get the soft state structure.
23100Sstevel@tonic-gate 	 */
23110Sstevel@tonic-gate 	if (pcihp_info(NULL, DDI_INFO_DEVT2DEVINFO, (void *)ap_dev,
23120Sstevel@tonic-gate 	    (void **)&self) != DDI_SUCCESS)
23130Sstevel@tonic-gate 		return (ENXIO);
23140Sstevel@tonic-gate 
23150Sstevel@tonic-gate 	pcihp_p = pcihp_get_soft_state(self, PCIHP_DR_SLOT_ENTER, &rval);
23160Sstevel@tonic-gate 	ASSERT(pcihp_p != NULL);
23170Sstevel@tonic-gate 
23180Sstevel@tonic-gate 	if (rval == PCIHP_FAILURE) {
23190Sstevel@tonic-gate 		PCIHP_DEBUG((CE_WARN, "pcihp instance is unconfigured"
23200Sstevel@tonic-gate 		    " while slot activity is requested\n"));
23210Sstevel@tonic-gate 		return (-1);
23220Sstevel@tonic-gate 	}
23230Sstevel@tonic-gate 
23240Sstevel@tonic-gate 	/* get the PCI device number for the slot */
23250Sstevel@tonic-gate 	pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(getminor(ap_dev));
23260Sstevel@tonic-gate 
23270Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
23280Sstevel@tonic-gate 
23290Sstevel@tonic-gate 	/*
23300Sstevel@tonic-gate 	 * All the events that may be handled in interrupt context should be
23310Sstevel@tonic-gate 	 * free of any mutex usage.
23320Sstevel@tonic-gate 	 */
23330Sstevel@tonic-gate 	switch (event_mask) {
23340Sstevel@tonic-gate 
23350Sstevel@tonic-gate 	case HPC_EVENT_CLEAR_ENUM:
23360Sstevel@tonic-gate 		/*
23370Sstevel@tonic-gate 		 * Check and clear ENUM# interrupt status. This may be
23380Sstevel@tonic-gate 		 * called by the Hotswap controller driver when it is
23390Sstevel@tonic-gate 		 * operating in a full hotswap system where the
23400Sstevel@tonic-gate 		 * platform may not have control on globally disabling ENUM#.
23410Sstevel@tonic-gate 		 * In such cases, the intent is to clear interrupt and
23420Sstevel@tonic-gate 		 * process the interrupt in non-interrupt context.
23430Sstevel@tonic-gate 		 * This is the first part of the ENUM# event processing.
23440Sstevel@tonic-gate 		 */
23450Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): ENUM# is generated"
23460Sstevel@tonic-gate 		    " on the bus (for slot %s ?)",
23470Sstevel@tonic-gate 		    ddi_driver_name(pcihp_p->dip),
23480Sstevel@tonic-gate 		    ddi_get_instance(pcihp_p->dip), slotinfop->name));
23490Sstevel@tonic-gate 
23500Sstevel@tonic-gate 		/* this is the only event coming through in interrupt context */
23510Sstevel@tonic-gate 		rv = pcihp_handle_enum(pcihp_p, pci_dev, PCIHP_CLEAR_ENUM,
23520Sstevel@tonic-gate 		    KM_NOSLEEP);
23530Sstevel@tonic-gate 
23540Sstevel@tonic-gate 		(void) pcihp_get_soft_state(self, PCIHP_DR_SLOT_EXIT, &rval);
23550Sstevel@tonic-gate 
23560Sstevel@tonic-gate 		return (rv);
23570Sstevel@tonic-gate 	default:
23580Sstevel@tonic-gate 		break;
23590Sstevel@tonic-gate 	}
23600Sstevel@tonic-gate 
23610Sstevel@tonic-gate 	mutex_enter(&slotinfop->slot_mutex);
23620Sstevel@tonic-gate 
2363881Sjohnny 	if (hpc_nexus_control(slotinfop->slot_hdl,
2364881Sjohnny 	    HPC_CTRL_GET_SLOT_STATE, (caddr_t)&rstate) != 0)
2365881Sjohnny 		rv = HPC_ERR_FAILED;
2366881Sjohnny 
2367881Sjohnny 	slotinfop->rstate = (ap_rstate_t)rstate;
2368881Sjohnny 
23690Sstevel@tonic-gate 	switch (event_mask) {
23700Sstevel@tonic-gate 
23710Sstevel@tonic-gate 	case HPC_EVENT_SLOT_INSERTION:
23720Sstevel@tonic-gate 		/*
23730Sstevel@tonic-gate 		 * A card is inserted in the slot. Just report this
23740Sstevel@tonic-gate 		 * event and return.
23750Sstevel@tonic-gate 		 */
23760Sstevel@tonic-gate 		cmn_err(CE_NOTE, "pcihp (%s%d): card is inserted"
23770Sstevel@tonic-gate 			" in the slot %s (pci dev %x)",
23780Sstevel@tonic-gate 			ddi_driver_name(pcihp_p->dip),
23790Sstevel@tonic-gate 			ddi_get_instance(pcihp_p->dip),
23800Sstevel@tonic-gate 			slotinfop->name, pci_dev);
23810Sstevel@tonic-gate 
23820Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
23830Sstevel@tonic-gate 
23840Sstevel@tonic-gate 		break;
23850Sstevel@tonic-gate 
23860Sstevel@tonic-gate 	case HPC_EVENT_SLOT_CONFIGURE:
23870Sstevel@tonic-gate 		/*
23880Sstevel@tonic-gate 		 * Configure the occupant that is just inserted in the slot.
23890Sstevel@tonic-gate 		 * The receptacle may or may not be in the connected state. If
23900Sstevel@tonic-gate 		 * the receptacle is not connected and the auto configuration
23910Sstevel@tonic-gate 		 * is enabled on this slot then connect the slot. If auto
23920Sstevel@tonic-gate 		 * configuration is enabled then configure the card.
23930Sstevel@tonic-gate 		 */
23940Sstevel@tonic-gate 		if ((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) == 0) {
23950Sstevel@tonic-gate 			/*
23960Sstevel@tonic-gate 			 * auto configuration is disabled. Tell someone
23970Sstevel@tonic-gate 			 * like RCM about this hotplug event?
23980Sstevel@tonic-gate 			 */
23990Sstevel@tonic-gate 			cmn_err(CE_NOTE, "pcihp (%s%d): SLOT_CONFIGURE event"
24000Sstevel@tonic-gate 				" occurred for pci dev %x (slot %s),"
24010Sstevel@tonic-gate 				" Slot disabled for auto-configuration.",
24020Sstevel@tonic-gate 				ddi_driver_name(pcihp_p->dip),
24030Sstevel@tonic-gate 				ddi_get_instance(pcihp_p->dip), pci_dev,
24040Sstevel@tonic-gate 				slotinfop->name);
24050Sstevel@tonic-gate 
24060Sstevel@tonic-gate 			/* +++ HOOK for RCM to report this hotplug event? +++ */
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 			break;
24090Sstevel@tonic-gate 		}
24100Sstevel@tonic-gate 
24110Sstevel@tonic-gate 		if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {
24120Sstevel@tonic-gate 			cmn_err(CE_WARN, "pcihp (%s%d): SLOT_CONFIGURE event"
24130Sstevel@tonic-gate 				" re-occurred for pci dev %x (slot %s),",
24140Sstevel@tonic-gate 				ddi_driver_name(pcihp_p->dip),
24150Sstevel@tonic-gate 				ddi_get_instance(pcihp_p->dip), pci_dev,
24160Sstevel@tonic-gate 				slotinfop->name);
24170Sstevel@tonic-gate 			mutex_exit(&slotinfop->slot_mutex);
24180Sstevel@tonic-gate 
24190Sstevel@tonic-gate 			(void) pcihp_get_soft_state(self, PCIHP_DR_SLOT_EXIT,
24200Sstevel@tonic-gate 			    &rval);
24210Sstevel@tonic-gate 
24220Sstevel@tonic-gate 			return (EAGAIN);
24230Sstevel@tonic-gate 		}
24240Sstevel@tonic-gate 
24250Sstevel@tonic-gate 		/*
24260Sstevel@tonic-gate 		 * Auto configuration is enabled. First, make sure the
24270Sstevel@tonic-gate 		 * receptacle is in the CONNECTED state.
24280Sstevel@tonic-gate 		 */
24290Sstevel@tonic-gate 		if ((rv = hpc_nexus_connect(slotinfop->slot_hdl,
24300Sstevel@tonic-gate 		    NULL, 0)) == HPC_SUCCESS) {
24310Sstevel@tonic-gate 		    slotinfop->rstate = AP_RSTATE_CONNECTED; /* record rstate */
24320Sstevel@tonic-gate 		}
24330Sstevel@tonic-gate 
24340Sstevel@tonic-gate 		/* Clear INS and Turn LED Off and start configuring. */
24350Sstevel@tonic-gate 		if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
24360Sstevel@tonic-gate 			pcihp_hs_csr_op(pcihp_p, pci_dev,
24370Sstevel@tonic-gate 				HPC_EVENT_SLOT_CONFIGURE);
24380Sstevel@tonic-gate 			if (pcihp_cpci_blue_led)
24390Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
24400Sstevel@tonic-gate 					HPC_EVENT_SLOT_BLUE_LED_OFF);
24410Sstevel@tonic-gate 		}
24420Sstevel@tonic-gate 
24430Sstevel@tonic-gate 		(void) hpc_nexus_control(slotinfop->slot_hdl,
24440Sstevel@tonic-gate 			HPC_CTRL_DEV_CONFIG_START, NULL);
24450Sstevel@tonic-gate 
24460Sstevel@tonic-gate 		/*
24470Sstevel@tonic-gate 		 * Call the configurator to configure the card.
24480Sstevel@tonic-gate 		 */
24490Sstevel@tonic-gate 		if (pcicfg_configure(pcihp_p->dip, pci_dev) != PCICFG_SUCCESS) {
24500Sstevel@tonic-gate 			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
24510Sstevel@tonic-gate 				if (pcihp_cpci_blue_led)
24520Sstevel@tonic-gate 					pcihp_hs_csr_op(pcihp_p, pci_dev,
24530Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_ON);
24540Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
24550Sstevel@tonic-gate 					HPC_EVENT_SLOT_UNCONFIGURE);
24560Sstevel@tonic-gate 			}
24570Sstevel@tonic-gate 			/* failed to configure the card */
24580Sstevel@tonic-gate 			cmn_err(CE_WARN, "pcihp (%s%d): failed to configure"
24590Sstevel@tonic-gate 				" the card in the slot %s",
24600Sstevel@tonic-gate 				ddi_driver_name(pcihp_p->dip),
24610Sstevel@tonic-gate 				ddi_get_instance(pcihp_p->dip),
24620Sstevel@tonic-gate 				slotinfop->name);
24630Sstevel@tonic-gate 			/* failed to configure; disconnect the slot */
24640Sstevel@tonic-gate 			if (hpc_nexus_disconnect(slotinfop->slot_hdl,
24650Sstevel@tonic-gate 			    NULL, 0) == HPC_SUCCESS) {
24660Sstevel@tonic-gate 			    slotinfop->rstate = AP_RSTATE_DISCONNECTED;
24670Sstevel@tonic-gate 			}
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate 			/* tell HPC driver occupant configure Error */
24700Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
24710Sstevel@tonic-gate 				HPC_CTRL_DEV_CONFIG_FAILURE, NULL);
24720Sstevel@tonic-gate 		} else {
24730Sstevel@tonic-gate 			/* record the occupant state as CONFIGURED */
24740Sstevel@tonic-gate 			slotinfop->ostate = AP_OSTATE_CONFIGURED;
24750Sstevel@tonic-gate 			slotinfop->condition = AP_COND_OK;
24760Sstevel@tonic-gate 
24770Sstevel@tonic-gate 			/* now, online all the devices in the AP */
24780Sstevel@tonic-gate 			ctrl.flags = PCIHP_CFG_CONTINUE;
24790Sstevel@tonic-gate 			ctrl.rv = NDI_SUCCESS;
24800Sstevel@tonic-gate 			ctrl.dip = NULL;
24810Sstevel@tonic-gate 			ctrl.pci_dev = pci_dev;
24820Sstevel@tonic-gate 			ctrl.op = PCIHP_ONLINE;
24830Sstevel@tonic-gate 				(void) pcihp_get_board_type(slotinfop);
24840Sstevel@tonic-gate 
24850Sstevel@tonic-gate 			ndi_devi_enter(pcihp_p->dip, &circular_count);
24860Sstevel@tonic-gate 			ddi_walk_devs(ddi_get_child(pcihp_p->dip),
24870Sstevel@tonic-gate 				pcihp_configure, (void *)&ctrl);
24880Sstevel@tonic-gate 			ndi_devi_exit(pcihp_p->dip, circular_count);
24890Sstevel@tonic-gate 
24900Sstevel@tonic-gate 			if (ctrl.rv != NDI_SUCCESS) {
24910Sstevel@tonic-gate 				/*
24920Sstevel@tonic-gate 				 * one or more of the devices are not
24930Sstevel@tonic-gate 				 * ONLINE'd. How is this to be
24940Sstevel@tonic-gate 				 * reported?
24950Sstevel@tonic-gate 				 */
24960Sstevel@tonic-gate 				cmn_err(CE_WARN,
24970Sstevel@tonic-gate 					"pcihp (%s%d): failed to attach one or"
24980Sstevel@tonic-gate 					" more drivers for the card in"
24990Sstevel@tonic-gate 					" the slot %s",
25000Sstevel@tonic-gate 					ddi_driver_name(pcihp_p->dip),
25010Sstevel@tonic-gate 					ddi_get_instance(pcihp_p->dip),
25020Sstevel@tonic-gate 					slotinfop->name);
25030Sstevel@tonic-gate 			}
25040Sstevel@tonic-gate 
25050Sstevel@tonic-gate 			/* tell HPC driver that the occupant is configured */
25060Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
25070Sstevel@tonic-gate 				HPC_CTRL_DEV_CONFIGURED, NULL);
25080Sstevel@tonic-gate 
25090Sstevel@tonic-gate 			cmn_err(CE_NOTE, "pcihp (%s%d): card is CONFIGURED"
25100Sstevel@tonic-gate 				" in the slot %s (pci dev %x)",
25110Sstevel@tonic-gate 				ddi_driver_name(pcihp_p->dip),
25120Sstevel@tonic-gate 				ddi_get_instance(pcihp_p->dip),
25130Sstevel@tonic-gate 				slotinfop->name, pci_dev);
25140Sstevel@tonic-gate 		}
25150Sstevel@tonic-gate 
25160Sstevel@tonic-gate 		break;
25170Sstevel@tonic-gate 
25180Sstevel@tonic-gate 	case HPC_EVENT_SLOT_UNCONFIGURE:
25190Sstevel@tonic-gate 		/*
25200Sstevel@tonic-gate 		 * Unconfigure the occupant in this slot.
25210Sstevel@tonic-gate 		 */
25220Sstevel@tonic-gate 		if ((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) == 0) {
25230Sstevel@tonic-gate 			/*
25240Sstevel@tonic-gate 			 * auto configuration is disabled. Tell someone
25250Sstevel@tonic-gate 			 * like RCM about this hotplug event?
25260Sstevel@tonic-gate 			 */
25270Sstevel@tonic-gate 			cmn_err(CE_NOTE, "pcihp (%s%d): SLOT_UNCONFIGURE event"
25280Sstevel@tonic-gate 				" for pci dev %x (slot %s) ignored,"
25290Sstevel@tonic-gate 				" Slot disabled for auto-configuration.",
25300Sstevel@tonic-gate 				ddi_driver_name(pcihp_p->dip),
25310Sstevel@tonic-gate 				ddi_get_instance(pcihp_p->dip), pci_dev,
25320Sstevel@tonic-gate 				slotinfop->name);
25330Sstevel@tonic-gate 
25340Sstevel@tonic-gate 			/* +++ HOOK for RCM to report this hotplug event? +++ */
25350Sstevel@tonic-gate 
25360Sstevel@tonic-gate 			break;
25370Sstevel@tonic-gate 		}
25380Sstevel@tonic-gate 
25390Sstevel@tonic-gate 		if (slotinfop->ostate == AP_OSTATE_UNCONFIGURED) {
25400Sstevel@tonic-gate 			cmn_err(CE_WARN, "pcihp (%s%d): SLOT_UNCONFIGURE "
25410Sstevel@tonic-gate 				"event re-occurred for pci dev %x (slot %s),",
25420Sstevel@tonic-gate 				ddi_driver_name(pcihp_p->dip),
25430Sstevel@tonic-gate 				ddi_get_instance(pcihp_p->dip), pci_dev,
25440Sstevel@tonic-gate 				slotinfop->name);
25450Sstevel@tonic-gate 			mutex_exit(&slotinfop->slot_mutex);
25460Sstevel@tonic-gate 
25470Sstevel@tonic-gate 			(void) pcihp_get_soft_state(self, PCIHP_DR_SLOT_EXIT,
25480Sstevel@tonic-gate 			    &rval);
25490Sstevel@tonic-gate 
25500Sstevel@tonic-gate 			return (EAGAIN);
25510Sstevel@tonic-gate 		}
25520Sstevel@tonic-gate 		/*
25530Sstevel@tonic-gate 		 * If the occupant is in the CONFIGURED state then
25540Sstevel@tonic-gate 		 * call the configurator to unconfigure the slot.
25550Sstevel@tonic-gate 		 */
25560Sstevel@tonic-gate 		if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {
25570Sstevel@tonic-gate 			/*
25580Sstevel@tonic-gate 			 * Detach all the drivers for the devices in the
25590Sstevel@tonic-gate 			 * slot. Call pcihp_configure() to offline the
25600Sstevel@tonic-gate 			 * devices.
25610Sstevel@tonic-gate 			 */
25620Sstevel@tonic-gate 			ctrl.flags = 0;
25630Sstevel@tonic-gate 			ctrl.rv = NDI_SUCCESS;
25640Sstevel@tonic-gate 			ctrl.dip = NULL;
25650Sstevel@tonic-gate 			ctrl.pci_dev = pci_dev;
25660Sstevel@tonic-gate 			ctrl.op = PCIHP_OFFLINE;
25670Sstevel@tonic-gate 
25680Sstevel@tonic-gate 			(void) devfs_clean(pcihp_p->dip, NULL, DV_CLEAN_FORCE);
25690Sstevel@tonic-gate 			ndi_devi_enter(pcihp_p->dip, &circular_count);
25700Sstevel@tonic-gate 			ddi_walk_devs(ddi_get_child(pcihp_p->dip),
25710Sstevel@tonic-gate 				pcihp_configure, (void *)&ctrl);
25720Sstevel@tonic-gate 			ndi_devi_exit(pcihp_p->dip, circular_count);
25730Sstevel@tonic-gate 
25740Sstevel@tonic-gate 			if (ctrl.rv != NDI_SUCCESS) {
25750Sstevel@tonic-gate 				/*
25760Sstevel@tonic-gate 				 * Failed to detach one or more drivers.
25770Sstevel@tonic-gate 				 * Restore the status for the drivers
25780Sstevel@tonic-gate 				 * which are offlined during this step.
25790Sstevel@tonic-gate 				 */
25800Sstevel@tonic-gate 				ctrl.flags = PCIHP_CFG_CONTINUE;
25810Sstevel@tonic-gate 				ctrl.rv = NDI_SUCCESS;
25820Sstevel@tonic-gate 				ctrl.dip = NULL;
25830Sstevel@tonic-gate 				ctrl.pci_dev = pci_dev;
25840Sstevel@tonic-gate 				ctrl.op = PCIHP_ONLINE;
25850Sstevel@tonic-gate 
25860Sstevel@tonic-gate 				ndi_devi_enter(pcihp_p->dip, &circular_count);
25870Sstevel@tonic-gate 				ddi_walk_devs(ddi_get_child(pcihp_p->dip),
25880Sstevel@tonic-gate 					pcihp_configure, (void *)&ctrl);
25890Sstevel@tonic-gate 				ndi_devi_exit(pcihp_p->dip, circular_count);
25900Sstevel@tonic-gate 				rv = HPC_ERR_FAILED;
25910Sstevel@tonic-gate 			} else {
25920Sstevel@tonic-gate 				(void) hpc_nexus_control(slotinfop->slot_hdl,
25930Sstevel@tonic-gate 					HPC_CTRL_DEV_UNCONFIG_START, NULL);
25940Sstevel@tonic-gate 
25950Sstevel@tonic-gate 				if (pcicfg_unconfigure(pcihp_p->dip,
25960Sstevel@tonic-gate 						pci_dev) == PCICFG_SUCCESS) {
25970Sstevel@tonic-gate 
25980Sstevel@tonic-gate 				/* Resources freed. Turn LED on. Clear EXT. */
25990Sstevel@tonic-gate 				if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
26000Sstevel@tonic-gate 					if (pcihp_cpci_blue_led)
26010Sstevel@tonic-gate 						pcihp_hs_csr_op(pcihp_p,
26020Sstevel@tonic-gate 						pci_dev,
26030Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_ON);
26040Sstevel@tonic-gate 					pcihp_hs_csr_op(pcihp_p, pci_dev,
26050Sstevel@tonic-gate 						HPC_EVENT_SLOT_UNCONFIGURE);
26060Sstevel@tonic-gate 					slotinfop->hs_csr_location = 0;
26070Sstevel@tonic-gate 					slotinfop->slot_flags &=
26080Sstevel@tonic-gate 						~PCIHP_SLOT_DEV_NON_HOTPLUG;
26090Sstevel@tonic-gate 				}
26100Sstevel@tonic-gate 					slotinfop->ostate =
26110Sstevel@tonic-gate 						AP_OSTATE_UNCONFIGURED;
26120Sstevel@tonic-gate 					slotinfop->condition = AP_COND_UNKNOWN;
26130Sstevel@tonic-gate 					/*
26140Sstevel@tonic-gate 					 * send the notification of state change
26150Sstevel@tonic-gate 					 * to the HPC driver.
26160Sstevel@tonic-gate 					 */
26170Sstevel@tonic-gate 					(void) hpc_nexus_control(
26180Sstevel@tonic-gate 						slotinfop->slot_hdl,
26190Sstevel@tonic-gate 						HPC_CTRL_DEV_UNCONFIGURED,
26200Sstevel@tonic-gate 						NULL);
26210Sstevel@tonic-gate 					/* disconnect the slot */
26220Sstevel@tonic-gate 					if (hpc_nexus_disconnect(
26230Sstevel@tonic-gate 						slotinfop->slot_hdl,
26240Sstevel@tonic-gate 						NULL, 0) == HPC_SUCCESS) {
26250Sstevel@tonic-gate 							slotinfop->rstate =
26260Sstevel@tonic-gate 							AP_RSTATE_DISCONNECTED;
26270Sstevel@tonic-gate 					}
26280Sstevel@tonic-gate 
26290Sstevel@tonic-gate 					cmn_err(CE_NOTE,
26300Sstevel@tonic-gate 					"pcihp (%s%d): card is UNCONFIGURED"
26310Sstevel@tonic-gate 						" in the slot %s (pci dev %x)",
26320Sstevel@tonic-gate 						ddi_driver_name(pcihp_p->dip),
26330Sstevel@tonic-gate 						ddi_get_instance(pcihp_p->dip),
26340Sstevel@tonic-gate 						slotinfop->name, pci_dev);
26350Sstevel@tonic-gate 				} else {
26360Sstevel@tonic-gate 					/* tell HPC driver occupant is Busy */
26370Sstevel@tonic-gate 					(void) hpc_nexus_control(
26380Sstevel@tonic-gate 						slotinfop->slot_hdl,
26390Sstevel@tonic-gate 						HPC_CTRL_DEV_UNCONFIG_FAILURE,
26400Sstevel@tonic-gate 						NULL);
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate 					rv = HPC_ERR_FAILED;
26430Sstevel@tonic-gate 				}
26440Sstevel@tonic-gate 			}
26450Sstevel@tonic-gate 		}
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
26480Sstevel@tonic-gate 
26490Sstevel@tonic-gate 		break;
26500Sstevel@tonic-gate 
26510Sstevel@tonic-gate 	case HPC_EVENT_SLOT_REMOVAL:
26520Sstevel@tonic-gate 		/*
26530Sstevel@tonic-gate 		 * Card is removed from the slot. The card must have been
26540Sstevel@tonic-gate 		 * unconfigured before this event.
26550Sstevel@tonic-gate 		 */
26560Sstevel@tonic-gate 		if (slotinfop->ostate != AP_OSTATE_UNCONFIGURED) {
2657881Sjohnny 			slotinfop->condition = AP_COND_FAILED;
2658881Sjohnny 			cmn_err(CE_WARN, "pcihp (%s%d): card is removed"
2659881Sjohnny 			    " from the slot %s",
26600Sstevel@tonic-gate 			    ddi_driver_name(pcihp_p->dip),
26610Sstevel@tonic-gate 			    ddi_get_instance(pcihp_p->dip),
26620Sstevel@tonic-gate 			    slotinfop->name);
26630Sstevel@tonic-gate 		}
2664881Sjohnny 		else
2665881Sjohnny 			cmn_err(CE_NOTE, "pcihp (%s%d): card is removed"
2666881Sjohnny 			    " from the slot %s",
2667881Sjohnny 			    ddi_driver_name(pcihp_p->dip),
2668881Sjohnny 			    ddi_get_instance(pcihp_p->dip),
2669881Sjohnny 			    slotinfop->name);
2670881Sjohnny 
2671881Sjohnny 		slotinfop->rstate = AP_RSTATE_EMPTY;
26720Sstevel@tonic-gate 
26730Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
26740Sstevel@tonic-gate 
26750Sstevel@tonic-gate 		break;
26760Sstevel@tonic-gate 
26770Sstevel@tonic-gate 	case HPC_EVENT_SLOT_POWER_ON:
26780Sstevel@tonic-gate 		/*
26790Sstevel@tonic-gate 		 * Slot is connected to the bus. i.e the card is powered
26800Sstevel@tonic-gate 		 * on. Are there any error conditions to be checked?
26810Sstevel@tonic-gate 		 */
26820Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): card is powered"
26830Sstevel@tonic-gate 			" on in the slot %s",
26840Sstevel@tonic-gate 			ddi_driver_name(pcihp_p->dip),
26850Sstevel@tonic-gate 			ddi_get_instance(pcihp_p->dip),
26860Sstevel@tonic-gate 			slotinfop->name));
26870Sstevel@tonic-gate 
26880Sstevel@tonic-gate 		slotinfop->rstate = AP_RSTATE_CONNECTED; /* record rstate */
26890Sstevel@tonic-gate 
26900Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
26910Sstevel@tonic-gate 
26920Sstevel@tonic-gate 		break;
26930Sstevel@tonic-gate 
26940Sstevel@tonic-gate 	case HPC_EVENT_SLOT_POWER_OFF:
26950Sstevel@tonic-gate 		/*
26960Sstevel@tonic-gate 		 * Slot is disconnected from the bus. i.e the card is powered
26970Sstevel@tonic-gate 		 * off. Are there any error conditions to be checked?
26980Sstevel@tonic-gate 		 */
26990Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): card is powered"
27000Sstevel@tonic-gate 			" off in the slot %s",
27010Sstevel@tonic-gate 			ddi_driver_name(pcihp_p->dip),
27020Sstevel@tonic-gate 			ddi_get_instance(pcihp_p->dip),
27030Sstevel@tonic-gate 			slotinfop->name));
27040Sstevel@tonic-gate 
27050Sstevel@tonic-gate 		slotinfop->rstate = AP_RSTATE_DISCONNECTED; /* record rstate */
27060Sstevel@tonic-gate 
27070Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
27080Sstevel@tonic-gate 
27090Sstevel@tonic-gate 		break;
27100Sstevel@tonic-gate 
27110Sstevel@tonic-gate 	case HPC_EVENT_SLOT_LATCH_SHUT:
27120Sstevel@tonic-gate 		/*
27130Sstevel@tonic-gate 		 * Latch on the slot is closed.
27140Sstevel@tonic-gate 		 */
27150Sstevel@tonic-gate 		cmn_err(CE_NOTE, "pcihp (%s%d): latch is shut"
27160Sstevel@tonic-gate 			" for the slot %s",
27170Sstevel@tonic-gate 			ddi_driver_name(pcihp_p->dip),
27180Sstevel@tonic-gate 			ddi_get_instance(pcihp_p->dip),
27190Sstevel@tonic-gate 			slotinfop->name);
27200Sstevel@tonic-gate 
27210Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
27220Sstevel@tonic-gate 
27230Sstevel@tonic-gate 		break;
27240Sstevel@tonic-gate 
27250Sstevel@tonic-gate 	case HPC_EVENT_SLOT_LATCH_OPEN:
27260Sstevel@tonic-gate 		/*
27270Sstevel@tonic-gate 		 * Latch on the slot is open.
27280Sstevel@tonic-gate 		 */
27290Sstevel@tonic-gate 		cmn_err(CE_NOTE, "pcihp (%s%d): latch is open"
27300Sstevel@tonic-gate 			" for the slot %s",
27310Sstevel@tonic-gate 			ddi_driver_name(pcihp_p->dip),
27320Sstevel@tonic-gate 			ddi_get_instance(pcihp_p->dip),
27330Sstevel@tonic-gate 			slotinfop->name);
27340Sstevel@tonic-gate 
27350Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
27360Sstevel@tonic-gate 
27370Sstevel@tonic-gate 		break;
27380Sstevel@tonic-gate 
27390Sstevel@tonic-gate 	case HPC_EVENT_PROCESS_ENUM:
27400Sstevel@tonic-gate 		/*
27410Sstevel@tonic-gate 		 * HSC knows the device number of the slot where the
27420Sstevel@tonic-gate 		 * ENUM# was triggered.
27430Sstevel@tonic-gate 		 * Now finish the necessary actions to be taken on that
27440Sstevel@tonic-gate 		 * slot. Please note that the interrupt is already cleared.
27450Sstevel@tonic-gate 		 * This is the second(last) part of the ENUM# event processing.
27460Sstevel@tonic-gate 		 */
27470Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): processing ENUM#"
27480Sstevel@tonic-gate 			" for slot %s",
27490Sstevel@tonic-gate 			ddi_driver_name(pcihp_p->dip),
27500Sstevel@tonic-gate 			ddi_get_instance(pcihp_p->dip),
27510Sstevel@tonic-gate 			slotinfop->name));
27520Sstevel@tonic-gate 
27530Sstevel@tonic-gate 		mutex_exit(&slotinfop->slot_mutex);
27540Sstevel@tonic-gate 		rv = pcihp_enum_slot(pcihp_p, slotinfop, pci_dev,
27550Sstevel@tonic-gate 			PCIHP_HANDLE_ENUM, KM_SLEEP);
27560Sstevel@tonic-gate 		mutex_enter(&slotinfop->slot_mutex);
27570Sstevel@tonic-gate 
27580Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
27590Sstevel@tonic-gate 
27600Sstevel@tonic-gate 		break;
27610Sstevel@tonic-gate 
27620Sstevel@tonic-gate 	case HPC_EVENT_BUS_ENUM:
27630Sstevel@tonic-gate 		/*
27640Sstevel@tonic-gate 		 * Same as HPC_EVENT_SLOT_ENUM as defined the PSARC doc.
27650Sstevel@tonic-gate 		 * This term is used for better clarity of its usage.
27660Sstevel@tonic-gate 		 *
27670Sstevel@tonic-gate 		 * ENUM signal occurred on the bus. It may be from this
27680Sstevel@tonic-gate 		 * slot or any other hotplug slot on the bus.
27690Sstevel@tonic-gate 		 *
27700Sstevel@tonic-gate 		 * It is NOT recommended that the hotswap controller uses
27710Sstevel@tonic-gate 		 * event without queuing as NDI and other DDI calls may not
27720Sstevel@tonic-gate 		 * necessarily be invokable in interrupt context.
27730Sstevel@tonic-gate 		 * Hence the hotswap controller driver should use the
27740Sstevel@tonic-gate 		 * CLEAR_ENUM event which returns the slot device number
27750Sstevel@tonic-gate 		 * and then call HPC_EVENT_PROCESS_ENUM event with queuing.
27760Sstevel@tonic-gate 		 *
27770Sstevel@tonic-gate 		 * This can be used when the hotswap controller is
27780Sstevel@tonic-gate 		 * implementing a polled event mechanism to do the
27790Sstevel@tonic-gate 		 * necessary actions in a single call.
27800Sstevel@tonic-gate 		 */
27810Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): ENUM# is generated"
27820Sstevel@tonic-gate 		    " on the bus (for slot %s ?)",
27830Sstevel@tonic-gate 		    ddi_driver_name(pcihp_p->dip),
27840Sstevel@tonic-gate 		    ddi_get_instance(pcihp_p->dip),
27850Sstevel@tonic-gate 		    slotinfop->name));
27860Sstevel@tonic-gate 
27870Sstevel@tonic-gate 		mutex_exit(&slotinfop->slot_mutex);
27880Sstevel@tonic-gate 		rv = pcihp_handle_enum(pcihp_p, pci_dev, PCIHP_HANDLE_ENUM,
27890Sstevel@tonic-gate 			KM_SLEEP);
27900Sstevel@tonic-gate 		mutex_enter(&slotinfop->slot_mutex);
27910Sstevel@tonic-gate 
27920Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
27930Sstevel@tonic-gate 
27940Sstevel@tonic-gate 		break;
27950Sstevel@tonic-gate 
27960Sstevel@tonic-gate 	case HPC_EVENT_SLOT_BLUE_LED_ON:
27970Sstevel@tonic-gate 
27980Sstevel@tonic-gate 		/*
27990Sstevel@tonic-gate 		 * Request to turn Hot Swap Blue LED on.
28000Sstevel@tonic-gate 		 */
28010Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): Request To Turn On Blue "
28020Sstevel@tonic-gate 		    "LED on the bus (for slot %s ?)",
28030Sstevel@tonic-gate 		    ddi_driver_name(pcihp_p->dip),
28040Sstevel@tonic-gate 		    ddi_get_instance(pcihp_p->dip),
28050Sstevel@tonic-gate 		    slotinfop->name));
28060Sstevel@tonic-gate 
28070Sstevel@tonic-gate 		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_SLOT_BLUE_LED_ON);
28080Sstevel@tonic-gate 		break;
28090Sstevel@tonic-gate 
28100Sstevel@tonic-gate 	case HPC_EVENT_DISABLE_ENUM:
28110Sstevel@tonic-gate 		/*
28120Sstevel@tonic-gate 		 * Disable ENUM# which disables auto configuration on this slot
28130Sstevel@tonic-gate 		 */
28140Sstevel@tonic-gate 		if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
28150Sstevel@tonic-gate 			pcihp_hs_csr_op(pcihp_p, pci_dev,
28160Sstevel@tonic-gate 				HPC_EVENT_DISABLE_ENUM);
28170Sstevel@tonic-gate 			slotinfop->slot_flags &= ~PCIHP_SLOT_AUTO_CFG_EN;
28180Sstevel@tonic-gate 		}
28190Sstevel@tonic-gate 		break;
28200Sstevel@tonic-gate 
28210Sstevel@tonic-gate 	case HPC_EVENT_ENABLE_ENUM:
28220Sstevel@tonic-gate 		/*
28230Sstevel@tonic-gate 		 * Enable ENUM# which enables auto configuration on this slot.
28240Sstevel@tonic-gate 		 */
28250Sstevel@tonic-gate 		if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
28260Sstevel@tonic-gate 			pcihp_hs_csr_op(pcihp_p, pci_dev,
28270Sstevel@tonic-gate 				HPC_EVENT_ENABLE_ENUM);
28280Sstevel@tonic-gate 			slotinfop->slot_flags |= PCIHP_SLOT_AUTO_CFG_EN;
28290Sstevel@tonic-gate 		}
28300Sstevel@tonic-gate 		break;
28310Sstevel@tonic-gate 
28320Sstevel@tonic-gate 	case HPC_EVENT_SLOT_BLUE_LED_OFF:
28330Sstevel@tonic-gate 
28340Sstevel@tonic-gate 		/*
28350Sstevel@tonic-gate 		 * Request to turn Hot Swap Blue LED off.
28360Sstevel@tonic-gate 		 */
28370Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): Request To Turn Off Blue "
28380Sstevel@tonic-gate 		    "LED on the bus (for slot %s ?)",
28390Sstevel@tonic-gate 		    ddi_driver_name(pcihp_p->dip),
28400Sstevel@tonic-gate 		    ddi_get_instance(pcihp_p->dip),
28410Sstevel@tonic-gate 		    slotinfop->name));
28420Sstevel@tonic-gate 
28430Sstevel@tonic-gate 		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_SLOT_BLUE_LED_OFF);
28440Sstevel@tonic-gate 
28450Sstevel@tonic-gate 		break;
28460Sstevel@tonic-gate 
28470Sstevel@tonic-gate 	case HPC_EVENT_SLOT_NOT_HEALTHY:
28480Sstevel@tonic-gate 		/*
28490Sstevel@tonic-gate 		 * HEALTHY# signal on this slot is not OK.
28500Sstevel@tonic-gate 		 */
28510Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): HEALTHY# signal is not OK"
28520Sstevel@tonic-gate 			" for this slot %s",
28530Sstevel@tonic-gate 			ddi_driver_name(pcihp_p->dip),
28540Sstevel@tonic-gate 			ddi_get_instance(pcihp_p->dip),
28550Sstevel@tonic-gate 			slotinfop->name));
28560Sstevel@tonic-gate 
28570Sstevel@tonic-gate 		/* record the state in slot_flags field */
28580Sstevel@tonic-gate 		slotinfop->slot_flags |= PCIHP_SLOT_NOT_HEALTHY;
28590Sstevel@tonic-gate 		slotinfop->condition = AP_COND_FAILED;
28600Sstevel@tonic-gate 
28610Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
28620Sstevel@tonic-gate 
28630Sstevel@tonic-gate 		break;
28640Sstevel@tonic-gate 
28650Sstevel@tonic-gate 	case HPC_EVENT_SLOT_HEALTHY_OK:
28660Sstevel@tonic-gate 		/*
28670Sstevel@tonic-gate 		 * HEALTHY# signal on this slot is OK now.
28680Sstevel@tonic-gate 		 */
28690Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): HEALTHY# signal is OK now"
28700Sstevel@tonic-gate 			" for this slot %s",
28710Sstevel@tonic-gate 			ddi_driver_name(pcihp_p->dip),
28720Sstevel@tonic-gate 			ddi_get_instance(pcihp_p->dip),
28730Sstevel@tonic-gate 			slotinfop->name));
28740Sstevel@tonic-gate 
28750Sstevel@tonic-gate 		/* update the state in slot_flags field */
28760Sstevel@tonic-gate 		slotinfop->slot_flags &= ~PCIHP_SLOT_NOT_HEALTHY;
28770Sstevel@tonic-gate 		slotinfop->condition = AP_COND_OK;
28780Sstevel@tonic-gate 
28790Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
28800Sstevel@tonic-gate 
28810Sstevel@tonic-gate 		break;
28820Sstevel@tonic-gate 
2883881Sjohnny 	case HPC_EVENT_SLOT_ATTN:
2884881Sjohnny 		/*
2885881Sjohnny 		 * Attention button is pressed.
2886881Sjohnny 		 */
2887881Sjohnny 		if (((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) == 0) ||
2888881Sjohnny 		    (slotinfop->slot_flags & PCIHP_SLOT_DISABLED)) {
2889881Sjohnny 			/*
2890881Sjohnny 			 * either auto-conifiguration or the slot is disabled,
2891881Sjohnny 			 * ignore this event.
2892881Sjohnny 			 */
2893881Sjohnny 			break;
2894881Sjohnny 		}
2895881Sjohnny 
2896881Sjohnny 		if (slotinfop->ostate == AP_OSTATE_UNCONFIGURED)
2897881Sjohnny 			hint = SE_INCOMING_RES;
2898881Sjohnny 		else
2899881Sjohnny 			hint = SE_OUTGOING_RES;
2900881Sjohnny 
2901881Sjohnny 		if (ddi_getprop(DDI_DEV_T_ANY, pcihp_p->dip, DDI_PROP_DONTPASS,
2902881Sjohnny 		    "inkernel-autoconfig", 0) == 0) {
2903881Sjohnny 			pcihp_gen_sysevent(slotinfop->name, PCIHP_DR_REQ, hint,
2904881Sjohnny 			    pcihp_p->dip, KM_SLEEP);
2905881Sjohnny 			break;
2906881Sjohnny 		}
2907881Sjohnny 
2908881Sjohnny 		if ((slotinfop->ostate == AP_OSTATE_UNCONFIGURED) &&
2909881Sjohnny 		    (slotinfop->rstate != AP_RSTATE_EMPTY) &&
2910881Sjohnny 		    (slotinfop->condition != AP_COND_FAILED)) {
2911881Sjohnny 			if (slotinfop->rstate == AP_RSTATE_DISCONNECTED) {
2912881Sjohnny 				rv = hpc_nexus_connect(slotinfop->slot_hdl,
2913881Sjohnny 				    NULL, 0);
2914881Sjohnny 				if (rv == HPC_SUCCESS)
2915881Sjohnny 					slotinfop->rstate = AP_RSTATE_CONNECTED;
2916881Sjohnny 				else
2917881Sjohnny 					break;
2918881Sjohnny 			}
2919881Sjohnny 
2920881Sjohnny 			rv = pcihp_configure_ap(pcihp_p, pci_dev);
2921881Sjohnny 
2922881Sjohnny 		} else if ((slotinfop->ostate == AP_OSTATE_CONFIGURED) &&
2923881Sjohnny 		    (slotinfop->rstate == AP_RSTATE_CONNECTED) &&
2924881Sjohnny 		    (slotinfop->condition != AP_COND_FAILED)) {
2925881Sjohnny 			rv = pcihp_unconfigure_ap(pcihp_p, pci_dev);
2926881Sjohnny 
2927881Sjohnny 			if (rv != HPC_SUCCESS)
2928881Sjohnny 				break;
2929881Sjohnny 
2930881Sjohnny 			rv = hpc_nexus_disconnect(slotinfop->slot_hdl,
2931881Sjohnny 			    NULL, 0);
2932881Sjohnny 			if (rv == HPC_SUCCESS)
2933881Sjohnny 				slotinfop->rstate = AP_RSTATE_DISCONNECTED;
2934881Sjohnny 		}
2935881Sjohnny 
2936881Sjohnny 		break;
2937881Sjohnny 
2938881Sjohnny 	case HPC_EVENT_SLOT_POWER_FAULT:
2939881Sjohnny 		/*
2940881Sjohnny 		 * Power fault is detected.
2941881Sjohnny 		 */
2942881Sjohnny 		cmn_err(CE_NOTE, "pcihp (%s%d): power-fault"
2943881Sjohnny 		    " for this slot %s",
2944881Sjohnny 		    ddi_driver_name(pcihp_p->dip),
2945881Sjohnny 		    ddi_get_instance(pcihp_p->dip),
2946881Sjohnny 		    slotinfop->name);
2947881Sjohnny 
2948881Sjohnny 		slotinfop->condition = AP_COND_FAILED;
2949881Sjohnny 
2950881Sjohnny 		pcihp_gen_sysevent(slotinfop->name, PCIHP_DR_AP_STATE_CHANGE,
2951881Sjohnny 		    SE_NO_HINT, pcihp_p->dip, KM_SLEEP);
2952881Sjohnny 
2953881Sjohnny 		break;
2954881Sjohnny 
29550Sstevel@tonic-gate 	default:
29560Sstevel@tonic-gate 		cmn_err(CE_NOTE, "pcihp (%s%d): unknown event %x"
29570Sstevel@tonic-gate 			" for this slot %s",
29580Sstevel@tonic-gate 			ddi_driver_name(pcihp_p->dip),
29590Sstevel@tonic-gate 			ddi_get_instance(pcihp_p->dip), event_mask,
29600Sstevel@tonic-gate 			slotinfop->name);
29610Sstevel@tonic-gate 
29620Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
29630Sstevel@tonic-gate 
29640Sstevel@tonic-gate 		break;
29650Sstevel@tonic-gate 	}
29660Sstevel@tonic-gate 
29670Sstevel@tonic-gate 	mutex_exit(&slotinfop->slot_mutex);
29680Sstevel@tonic-gate 
29690Sstevel@tonic-gate 	(void) pcihp_get_soft_state(self, PCIHP_DR_SLOT_EXIT, &rval);
29700Sstevel@tonic-gate 
29710Sstevel@tonic-gate 	return (rv);
29720Sstevel@tonic-gate }
29730Sstevel@tonic-gate 
29740Sstevel@tonic-gate /*
29750Sstevel@tonic-gate  * This function is called to online or offline the devices for an
29760Sstevel@tonic-gate  * attachment point. If the PCI device number of the node matches
29770Sstevel@tonic-gate  * with the device number of the specified hot plug slot then
29780Sstevel@tonic-gate  * the operation is performed.
29790Sstevel@tonic-gate  */
29800Sstevel@tonic-gate static int
29810Sstevel@tonic-gate pcihp_configure(dev_info_t *dip, void *hdl)
29820Sstevel@tonic-gate {
29830Sstevel@tonic-gate 	int pci_dev;
29840Sstevel@tonic-gate 	struct pcihp_config_ctrl *ctrl = (struct pcihp_config_ctrl *)hdl;
29850Sstevel@tonic-gate 	int rv;
29860Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
29870Sstevel@tonic-gate 	int length;
29880Sstevel@tonic-gate 
29890Sstevel@tonic-gate 	/*
29900Sstevel@tonic-gate 	 * Get the PCI device number information from the devinfo
29910Sstevel@tonic-gate 	 * node. Since the node may not have the address field
29920Sstevel@tonic-gate 	 * setup (this is done in the DDI_INITCHILD of the parent)
29930Sstevel@tonic-gate 	 * we look up the 'reg' property to decode that information.
29940Sstevel@tonic-gate 	 */
29950Sstevel@tonic-gate 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
29960Sstevel@tonic-gate 		DDI_PROP_DONTPASS, "reg", (int **)&pci_rp,
29970Sstevel@tonic-gate 		(uint_t *)&length) != DDI_PROP_SUCCESS) {
29980Sstevel@tonic-gate 		ctrl->rv = DDI_FAILURE;
29990Sstevel@tonic-gate 		ctrl->dip = dip;
30000Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
30010Sstevel@tonic-gate 	}
30020Sstevel@tonic-gate 
30030Sstevel@tonic-gate 	/* get the pci device id information */
30040Sstevel@tonic-gate 	pci_dev = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
30050Sstevel@tonic-gate 
30060Sstevel@tonic-gate 	/*
30070Sstevel@tonic-gate 	 * free the memory allocated by ddi_prop_lookup_int_array
30080Sstevel@tonic-gate 	 */
30090Sstevel@tonic-gate 	ddi_prop_free(pci_rp);
30100Sstevel@tonic-gate 
30110Sstevel@tonic-gate 	/*
30120Sstevel@tonic-gate 	 * Match the node for the device number of the slot.
30130Sstevel@tonic-gate 	 */
30140Sstevel@tonic-gate 	if (pci_dev == ctrl->pci_dev) {	/* node is a match */
30150Sstevel@tonic-gate 		if (ctrl->op == PCIHP_ONLINE) {
30160Sstevel@tonic-gate 			/* it is CONFIGURE operation */
30170Sstevel@tonic-gate 			rv = ndi_devi_online(dip, NDI_ONLINE_ATTACH|NDI_CONFIG);
30180Sstevel@tonic-gate 		} else {
30190Sstevel@tonic-gate 			/*
30200Sstevel@tonic-gate 			 * it is UNCONFIGURE operation.
30210Sstevel@tonic-gate 			 */
30220Sstevel@tonic-gate 			rv = ndi_devi_offline(dip, NDI_UNCONFIG);
30230Sstevel@tonic-gate 		}
30240Sstevel@tonic-gate 		if (rv != NDI_SUCCESS) {
30250Sstevel@tonic-gate 			/* failed to attach/detach the driver(s) */
30260Sstevel@tonic-gate 			ctrl->rv = rv;
30270Sstevel@tonic-gate 			ctrl->dip = dip;
30280Sstevel@tonic-gate 			/* terminate the search if specified */
30290Sstevel@tonic-gate 			if (!(ctrl->flags & PCIHP_CFG_CONTINUE))
30300Sstevel@tonic-gate 				return (DDI_WALK_TERMINATE);
30310Sstevel@tonic-gate 		}
30320Sstevel@tonic-gate 	}
30330Sstevel@tonic-gate 
30340Sstevel@tonic-gate 	/*
30350Sstevel@tonic-gate 	 * continue the walk to the next sibling to look for a match
30360Sstevel@tonic-gate 	 * or to find other nodes if this card is a multi-function card.
30370Sstevel@tonic-gate 	 */
30380Sstevel@tonic-gate 	return (DDI_WALK_PRUNECHILD);
30390Sstevel@tonic-gate }
30400Sstevel@tonic-gate 
30410Sstevel@tonic-gate /* control structure used to find a device in the devinfo tree */
30420Sstevel@tonic-gate struct pcihp_find_ctrl {
30430Sstevel@tonic-gate 	uint_t		device;
30440Sstevel@tonic-gate 	uint_t		function;
30450Sstevel@tonic-gate 	dev_info_t	*dip;
30460Sstevel@tonic-gate };
30470Sstevel@tonic-gate 
30480Sstevel@tonic-gate static dev_info_t *
30490Sstevel@tonic-gate pcihp_devi_find(dev_info_t *dip, uint_t device, uint_t function)
30500Sstevel@tonic-gate {
30510Sstevel@tonic-gate 	struct pcihp_find_ctrl ctrl;
30520Sstevel@tonic-gate 	int circular_count;
30530Sstevel@tonic-gate 
30540Sstevel@tonic-gate 	ctrl.device = device;
30550Sstevel@tonic-gate 	ctrl.function = function;
30560Sstevel@tonic-gate 	ctrl.dip = NULL;
30570Sstevel@tonic-gate 
30580Sstevel@tonic-gate 	ndi_devi_enter(dip, &circular_count);
30590Sstevel@tonic-gate 	ddi_walk_devs(ddi_get_child(dip), pcihp_match_dev, (void *)&ctrl);
30600Sstevel@tonic-gate 	ndi_devi_exit(dip, circular_count);
30610Sstevel@tonic-gate 
30620Sstevel@tonic-gate 	return (ctrl.dip);
30630Sstevel@tonic-gate }
30640Sstevel@tonic-gate 
30650Sstevel@tonic-gate static int
30660Sstevel@tonic-gate pcihp_match_dev(dev_info_t *dip, void *hdl)
30670Sstevel@tonic-gate {
30680Sstevel@tonic-gate 	struct pcihp_find_ctrl *ctrl = (struct pcihp_find_ctrl *)hdl;
30690Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
30700Sstevel@tonic-gate 	int length;
30710Sstevel@tonic-gate 	int pci_dev;
30720Sstevel@tonic-gate 	int pci_func;
30730Sstevel@tonic-gate 
30740Sstevel@tonic-gate 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
30750Sstevel@tonic-gate 		DDI_PROP_DONTPASS, "reg", (int **)&pci_rp,
30760Sstevel@tonic-gate 		(uint_t *)&length) != DDI_PROP_SUCCESS) {
30770Sstevel@tonic-gate 		ctrl->dip = NULL;
30780Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
30790Sstevel@tonic-gate 	}
30800Sstevel@tonic-gate 
30810Sstevel@tonic-gate 	/* get the PCI device address info */
30820Sstevel@tonic-gate 	pci_dev = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
30830Sstevel@tonic-gate 	pci_func = PCI_REG_FUNC_G(pci_rp->pci_phys_hi);
30840Sstevel@tonic-gate 
30850Sstevel@tonic-gate 	/*
30860Sstevel@tonic-gate 	 * free the memory allocated by ddi_prop_lookup_int_array
30870Sstevel@tonic-gate 	 */
30880Sstevel@tonic-gate 	ddi_prop_free(pci_rp);
30890Sstevel@tonic-gate 
30900Sstevel@tonic-gate 
30910Sstevel@tonic-gate 	if ((pci_dev == ctrl->device) && (pci_func == ctrl->function)) {
30920Sstevel@tonic-gate 		/* found the match for the specified device address */
30930Sstevel@tonic-gate 		ctrl->dip = dip;
30940Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
30950Sstevel@tonic-gate 	}
30960Sstevel@tonic-gate 
30970Sstevel@tonic-gate 	/*
30980Sstevel@tonic-gate 	 * continue the walk to the next sibling to look for a match.
30990Sstevel@tonic-gate 	 */
31000Sstevel@tonic-gate 	return (DDI_WALK_PRUNECHILD);
31010Sstevel@tonic-gate }
31020Sstevel@tonic-gate 
31030Sstevel@tonic-gate #if 0
31040Sstevel@tonic-gate /*
31050Sstevel@tonic-gate  * Probe the configuration space of the slot to determine the receptacle
31060Sstevel@tonic-gate  * state. There may not be any devinfo tree created for this slot.
31070Sstevel@tonic-gate  */
31080Sstevel@tonic-gate static void
31090Sstevel@tonic-gate pcihp_probe_slot_state(dev_info_t *dip, int dev, hpc_slot_state_t *rstatep)
31100Sstevel@tonic-gate {
31110Sstevel@tonic-gate 	/* XXX FIX IT */
31120Sstevel@tonic-gate }
31130Sstevel@tonic-gate #endif
31140Sstevel@tonic-gate 
31150Sstevel@tonic-gate /*
31160Sstevel@tonic-gate  * This routine is called when a ENUM# assertion is detected for a bus.
31170Sstevel@tonic-gate  * Since ENUM# may be bussed, the slot that asserted ENUM# may not be known.
31180Sstevel@tonic-gate  * The HPC Driver passes the handle of a slot that is its best guess.
31190Sstevel@tonic-gate  * If the best guess slot is the one that asserted ENUM#, the proper handling
31200Sstevel@tonic-gate  * will be done.  If its not, all possible slots will be locked at until
31210Sstevel@tonic-gate  * one that is asserting ENUM is found.
31220Sstevel@tonic-gate  * Also, indicate to the HSC to turn on ENUM# after it is serviced,
31230Sstevel@tonic-gate  * incase if it was disabled by the HSC due to the nature of asynchronous
31240Sstevel@tonic-gate  * delivery of interrupt by the framework.
31250Sstevel@tonic-gate  *
31260Sstevel@tonic-gate  * opcode has the following meanings.
31270Sstevel@tonic-gate  * PCIHP_CLEAR_ENUM = just clear interrupt and return the PCI device no. if
31280Sstevel@tonic-gate  *			success, else return -1.
31290Sstevel@tonic-gate  * PCIHP_HANDLE_ENUM = clear interrupt and handle interrupt also.
31300Sstevel@tonic-gate  *
31310Sstevel@tonic-gate  */
31320Sstevel@tonic-gate static int
31330Sstevel@tonic-gate pcihp_handle_enum(pcihp_t *pcihp_p, int favorite_pci_dev, int opcode,
31340Sstevel@tonic-gate 	int kmflag)
31350Sstevel@tonic-gate {
31360Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
31370Sstevel@tonic-gate 	int pci_dev, rc, event_serviced = 0;
31380Sstevel@tonic-gate 
31390Sstevel@tonic-gate 	/*
31400Sstevel@tonic-gate 	 * Handle ENUM# condition for the "favorite" slot first.
31410Sstevel@tonic-gate 	 */
31420Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[favorite_pci_dev];
31430Sstevel@tonic-gate 	if (slotinfop) {
31440Sstevel@tonic-gate 		/*
31450Sstevel@tonic-gate 		 * First try the "favorite" pci device.  This is the device
31460Sstevel@tonic-gate 		 * associated with the handle passed by the HPC Driver.
31470Sstevel@tonic-gate 		 */
31480Sstevel@tonic-gate 		rc = pcihp_enum_slot(pcihp_p, slotinfop, favorite_pci_dev,
31490Sstevel@tonic-gate 			opcode, kmflag);
31500Sstevel@tonic-gate 		if (rc != HPC_EVENT_UNCLAIMED) {	/* indicates success */
31510Sstevel@tonic-gate 			event_serviced = 1;
31520Sstevel@tonic-gate 			/* This MUST be a non-DEBUG feature. */
31530Sstevel@tonic-gate 			if (! pcihp_enum_scan_all) {
31540Sstevel@tonic-gate 				return (rc);
31550Sstevel@tonic-gate 			}
31560Sstevel@tonic-gate 		}
31570Sstevel@tonic-gate 	}
31580Sstevel@tonic-gate 
31590Sstevel@tonic-gate 	/*
31600Sstevel@tonic-gate 	 * If ENUM# is implemented as a radial signal, then there is no
31610Sstevel@tonic-gate 	 * need to further poll the slots.
31620Sstevel@tonic-gate 	 */
31630Sstevel@tonic-gate 	if (pcihp_p->bus_flags & PCIHP_BUS_ENUM_RADIAL)
31640Sstevel@tonic-gate 		goto enum_service_check;
31650Sstevel@tonic-gate 
31660Sstevel@tonic-gate 	/*
31670Sstevel@tonic-gate 	 * If the "favorite" pci device didn't assert ENUM#, then
31680Sstevel@tonic-gate 	 * try the rest.  Once we find and handle a device that asserted
31690Sstevel@tonic-gate 	 * ENUM#, then we will terminate the walk by returning unless
31700Sstevel@tonic-gate 	 * scan-all flag is set.
31710Sstevel@tonic-gate 	 */
31720Sstevel@tonic-gate 	for (pci_dev = 0; pci_dev < PCI_MAX_DEVS; pci_dev++) {
31730Sstevel@tonic-gate 		if (pci_dev != favorite_pci_dev) {
31740Sstevel@tonic-gate 			slotinfop = &pcihp_p->slotinfo[pci_dev];
31750Sstevel@tonic-gate 			if (slotinfop == NULL) {
31760Sstevel@tonic-gate 				continue;
31770Sstevel@tonic-gate 			}
31780Sstevel@tonic-gate 			/* Only CPCI devices support ENUM# generation. */
31790Sstevel@tonic-gate 			if (!(slotinfop->slot_type & HPC_SLOT_TYPE_CPCI))
31800Sstevel@tonic-gate 				continue;
31810Sstevel@tonic-gate 			rc = pcihp_enum_slot(pcihp_p, slotinfop, pci_dev,
31820Sstevel@tonic-gate 				opcode, kmflag);
31830Sstevel@tonic-gate 			if (rc != HPC_EVENT_UNCLAIMED) {
31840Sstevel@tonic-gate 				event_serviced = 1;
31850Sstevel@tonic-gate 				/* This MUST be a non-DEBUG feature. */
31860Sstevel@tonic-gate 				if (! pcihp_enum_scan_all)
31870Sstevel@tonic-gate 					break;
31880Sstevel@tonic-gate 			}
31890Sstevel@tonic-gate 		}
31900Sstevel@tonic-gate 	}
31910Sstevel@tonic-gate 
31920Sstevel@tonic-gate enum_service_check:
31930Sstevel@tonic-gate 	if (event_serviced) {
31940Sstevel@tonic-gate 		return (rc);
31950Sstevel@tonic-gate 	}
31960Sstevel@tonic-gate 
31970Sstevel@tonic-gate 	/* No ENUM# event found, Return */
31980Sstevel@tonic-gate 	return (HPC_EVENT_UNCLAIMED);
31990Sstevel@tonic-gate }
32000Sstevel@tonic-gate 
32010Sstevel@tonic-gate /*
32020Sstevel@tonic-gate  * This routine attempts to handle a possible ENUM# assertion case for a
32030Sstevel@tonic-gate  * specified slot.  This only works for adapters that implement Hot Swap
32040Sstevel@tonic-gate  * Friendly Silicon.  If the slot's HS_CSR is read and it specifies ENUM#
32050Sstevel@tonic-gate  * has been asserted, either the insertion or removal handlers will be
32060Sstevel@tonic-gate  * called.
32070Sstevel@tonic-gate  */
32080Sstevel@tonic-gate static int
32090Sstevel@tonic-gate pcihp_enum_slot(pcihp_t *pcihp_p, struct pcihp_slotinfo *slotinfop, int pci_dev,
32100Sstevel@tonic-gate 		int opcode, int kmflag)
32110Sstevel@tonic-gate {
32120Sstevel@tonic-gate 	ddi_acc_handle_t handle;
32130Sstevel@tonic-gate 	dev_info_t *dip, *new_child = NULL;
32140Sstevel@tonic-gate 	int result, rv = -1;
32150Sstevel@tonic-gate 	uint8_t hs_csr;
32160Sstevel@tonic-gate 
32170Sstevel@tonic-gate 	if (pcihp_config_setup(&dip, &handle, &new_child, pci_dev,
32180Sstevel@tonic-gate 				pcihp_p) != DDI_SUCCESS) {
32190Sstevel@tonic-gate 		return (HPC_EVENT_UNCLAIMED);
32200Sstevel@tonic-gate 	}
32210Sstevel@tonic-gate 
32220Sstevel@tonic-gate 	/*
32230Sstevel@tonic-gate 	 * Read the device's HS_CSR.
32240Sstevel@tonic-gate 	 */
32250Sstevel@tonic-gate 	result = pcihp_get_hs_csr(slotinfop, handle, (uint8_t *)&hs_csr);
32260Sstevel@tonic-gate 	PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): hs_csr = %x, flags = %x",
32270Sstevel@tonic-gate 		ddi_driver_name(pcihp_p->dip), ddi_get_instance(pcihp_p->dip),
32280Sstevel@tonic-gate 		hs_csr, slotinfop->slot_flags));
32290Sstevel@tonic-gate 	/*
32300Sstevel@tonic-gate 	 * we teardown our device map here, because in case of an
32310Sstevel@tonic-gate 	 * extraction event, our nodes would be freed and a teardown
32320Sstevel@tonic-gate 	 * will cause problems.
32330Sstevel@tonic-gate 	 */
32340Sstevel@tonic-gate 	pcihp_config_teardown(&handle, &new_child, pci_dev, pcihp_p);
32350Sstevel@tonic-gate 
32360Sstevel@tonic-gate 	if (result == PCIHP_SUCCESS) {
32370Sstevel@tonic-gate 
32380Sstevel@tonic-gate 		/* If ENUM# is masked, then it is not us. Some other device */
32390Sstevel@tonic-gate 		if ((hs_csr & HS_CSR_EIM) && (opcode == PCIHP_CLEAR_ENUM))
32400Sstevel@tonic-gate 			return (HPC_EVENT_UNCLAIMED);
32410Sstevel@tonic-gate 		/*
32420Sstevel@tonic-gate 		 * This device supports Full Hot Swap and implements
32430Sstevel@tonic-gate 		 * the Hot Swap Control and Status Register.
32440Sstevel@tonic-gate 		 */
32450Sstevel@tonic-gate 		if ((hs_csr & HS_CSR_INS) ||
32460Sstevel@tonic-gate 			(slotinfop->slot_flags & PCIHP_SLOT_ENUM_INS_PENDING)) {
32470Sstevel@tonic-gate 			/* handle insertion ENUM */
32480Sstevel@tonic-gate 			PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): "
32490Sstevel@tonic-gate 			    "Handle Insertion ENUM (INS) "
32500Sstevel@tonic-gate 			    "on the bus (for slot %s ?)",
32510Sstevel@tonic-gate 			    ddi_driver_name(pcihp_p->dip),
32520Sstevel@tonic-gate 			    ddi_get_instance(pcihp_p->dip),
32530Sstevel@tonic-gate 			    slotinfop->name));
32540Sstevel@tonic-gate 
32550Sstevel@tonic-gate 			/*
32560Sstevel@tonic-gate 			 * generate sysevent
32570Sstevel@tonic-gate 			 */
32580Sstevel@tonic-gate 
32590Sstevel@tonic-gate 			if (opcode == PCIHP_CLEAR_ENUM)
32600Sstevel@tonic-gate 				pcihp_gen_sysevent(slotinfop->name,
32610Sstevel@tonic-gate 					PCIHP_DR_REQ,
32620Sstevel@tonic-gate 					SE_INCOMING_RES, pcihp_p->dip,
32630Sstevel@tonic-gate 					kmflag);
32640Sstevel@tonic-gate 
32650Sstevel@tonic-gate 			rv = pcihp_handle_enum_insertion(pcihp_p, pci_dev,
32660Sstevel@tonic-gate 				opcode, kmflag);
32670Sstevel@tonic-gate 
32680Sstevel@tonic-gate 		} else if ((hs_csr & HS_CSR_EXT) || (slotinfop->slot_flags &
32690Sstevel@tonic-gate 					PCIHP_SLOT_ENUM_EXT_PENDING)) {
32700Sstevel@tonic-gate 			/* handle extraction ENUM */
32710Sstevel@tonic-gate 			PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): "
32720Sstevel@tonic-gate 			    "Handle Extraction ENUM (EXT) "
32730Sstevel@tonic-gate 			    "on the bus (for slot %s ?)",
32740Sstevel@tonic-gate 			    ddi_driver_name(pcihp_p->dip),
32750Sstevel@tonic-gate 			    ddi_get_instance(pcihp_p->dip),
32760Sstevel@tonic-gate 			    slotinfop->name));
32770Sstevel@tonic-gate 
32780Sstevel@tonic-gate 			/*
32790Sstevel@tonic-gate 			 * generate sysevent
32800Sstevel@tonic-gate 			 */
32810Sstevel@tonic-gate 
32820Sstevel@tonic-gate 			if (opcode == PCIHP_CLEAR_ENUM)
32830Sstevel@tonic-gate 				pcihp_gen_sysevent(slotinfop->name,
32840Sstevel@tonic-gate 					PCIHP_DR_REQ,
32850Sstevel@tonic-gate 					SE_OUTGOING_RES,
32860Sstevel@tonic-gate 					pcihp_p->dip,
32870Sstevel@tonic-gate 					kmflag);
32880Sstevel@tonic-gate 
32890Sstevel@tonic-gate 			rv = pcihp_handle_enum_extraction(pcihp_p, pci_dev,
32900Sstevel@tonic-gate 				opcode, kmflag);
32910Sstevel@tonic-gate 		}
32920Sstevel@tonic-gate 		if (opcode == PCIHP_CLEAR_ENUM) {
32930Sstevel@tonic-gate 			if (rv == PCIHP_SUCCESS)
32940Sstevel@tonic-gate 				rv = pci_dev;
32950Sstevel@tonic-gate 			else
32960Sstevel@tonic-gate 				rv = HPC_EVENT_UNCLAIMED;
32970Sstevel@tonic-gate 		}
32980Sstevel@tonic-gate 	}
32990Sstevel@tonic-gate 
33000Sstevel@tonic-gate 	return (rv);
33010Sstevel@tonic-gate }
33020Sstevel@tonic-gate 
33030Sstevel@tonic-gate /*
33040Sstevel@tonic-gate  * This routine is called when a ENUM# caused by lifting the lever
33050Sstevel@tonic-gate  * is detected.  If the occupant is configured, it will be unconfigured.
33060Sstevel@tonic-gate  * If the occupant is already unconfigured or is successfully unconfigured,
33070Sstevel@tonic-gate  * the blue LED on the adapter is illuminated which means its OK to remove.
33080Sstevel@tonic-gate  * Please note that the lock must be released before invoking the
33090Sstevel@tonic-gate  * generic AP unconfigure function.
33100Sstevel@tonic-gate  */
33110Sstevel@tonic-gate static int
33120Sstevel@tonic-gate pcihp_handle_enum_extraction(pcihp_t *pcihp_p, int pci_dev, int opcode,
33130Sstevel@tonic-gate 	int kmflag)
33140Sstevel@tonic-gate {
33150Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
33160Sstevel@tonic-gate 	int rv = PCIHP_FAILURE;
33170Sstevel@tonic-gate 
33180Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
33190Sstevel@tonic-gate 
33200Sstevel@tonic-gate 	/*
33210Sstevel@tonic-gate 	 * It was observed that, clearing the EXT bit turned the LED ON.
33220Sstevel@tonic-gate 	 * This is a BIG problem in case if the unconfigure operation
33230Sstevel@tonic-gate 	 * failed because the board was busy.
33240Sstevel@tonic-gate 	 * In order to avoid this confusing situation (LED ON but the board
33250Sstevel@tonic-gate 	 * is not unconfigured), we instead decided not to clear EXT but
33260Sstevel@tonic-gate 	 * disable further ENUM# from this slot. Disabling ENUM# clears
33270Sstevel@tonic-gate 	 * the interrupt.
33280Sstevel@tonic-gate 	 * Finally before returning we clear the interrupt and enable
33290Sstevel@tonic-gate 	 * ENUM# back again from this slot.
33300Sstevel@tonic-gate 	 */
33310Sstevel@tonic-gate 	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_DISABLE_ENUM);
33320Sstevel@tonic-gate 	if (opcode == PCIHP_CLEAR_ENUM) {
33330Sstevel@tonic-gate 		slotinfop->slot_flags |= PCIHP_SLOT_ENUM_EXT_PENDING;
33340Sstevel@tonic-gate 		return (PCIHP_SUCCESS);
33350Sstevel@tonic-gate 	}
33360Sstevel@tonic-gate 
3337881Sjohnny 	mutex_enter(&slotinfop->slot_mutex);
33380Sstevel@tonic-gate 	rv = pcihp_unconfigure_ap(pcihp_p, pci_dev);
3339881Sjohnny 	mutex_exit(&slotinfop->slot_mutex);
33400Sstevel@tonic-gate 	if (rv != HPC_SUCCESS && rv != EBUSY) {
33410Sstevel@tonic-gate 		cmn_err(CE_NOTE, "%s%d: PCI device %x Failed on Unconfigure",
33420Sstevel@tonic-gate 		    ddi_driver_name(pcihp_p->dip),
33430Sstevel@tonic-gate 		    ddi_get_instance(pcihp_p->dip), pci_dev);
33440Sstevel@tonic-gate 	}
33450Sstevel@tonic-gate 	if (rv == EBUSY)
33460Sstevel@tonic-gate 		cmn_err(CE_NOTE, "%s%d: PCI device %x Busy",
33470Sstevel@tonic-gate 		    ddi_driver_name(pcihp_p->dip),
33480Sstevel@tonic-gate 		    ddi_get_instance(pcihp_p->dip), pci_dev);
33490Sstevel@tonic-gate 	if (rv) {
33500Sstevel@tonic-gate 		if (pcihp_cpci_blue_led)
33510Sstevel@tonic-gate 			pcihp_hs_csr_op(pcihp_p, pci_dev,
33520Sstevel@tonic-gate 					HPC_EVENT_SLOT_BLUE_LED_OFF);
33530Sstevel@tonic-gate 	}
33540Sstevel@tonic-gate 	/*
33550Sstevel@tonic-gate 	 * we must clear interrupt in case the unconfigure didn't do it
33560Sstevel@tonic-gate 	 * due to a duplicate interrupt. Extraction is success.
33570Sstevel@tonic-gate 	 */
33580Sstevel@tonic-gate 	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_SLOT_UNCONFIGURE);
33590Sstevel@tonic-gate 
33600Sstevel@tonic-gate 	if (!rv) {
33610Sstevel@tonic-gate 		/*
33620Sstevel@tonic-gate 		 * Sys Event Notification.
33630Sstevel@tonic-gate 		 */
33640Sstevel@tonic-gate 		pcihp_gen_sysevent(slotinfop->name, PCIHP_DR_AP_STATE_CHANGE,
33650Sstevel@tonic-gate 			SE_HINT_REMOVE, pcihp_p->dip, kmflag);
33660Sstevel@tonic-gate 	}
33670Sstevel@tonic-gate 
33680Sstevel@tonic-gate 	/*
33690Sstevel@tonic-gate 	 * Enable interrupts back from this board.
33700Sstevel@tonic-gate 	 * This could potentially be problematic in case if the user is
33710Sstevel@tonic-gate 	 * quick enough to extract the board.
33720Sstevel@tonic-gate 	 * But we must do it just in case if the switch is closed again.
33730Sstevel@tonic-gate 	 */
33740Sstevel@tonic-gate 	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_ENABLE_ENUM);
33750Sstevel@tonic-gate 	slotinfop->slot_flags &= ~PCIHP_SLOT_ENUM_EXT_PENDING;
33760Sstevel@tonic-gate 	return (rv);
33770Sstevel@tonic-gate }
33780Sstevel@tonic-gate 
33790Sstevel@tonic-gate /*
33800Sstevel@tonic-gate  * This routine is called when a ENUM# caused by when an adapter insertion
33810Sstevel@tonic-gate  * is detected.  If the occupant is successfully configured (i.e. PCI resources
33820Sstevel@tonic-gate  * successfully assigned, the blue LED is left off, otherwise if configuration
33830Sstevel@tonic-gate  * is not successful, the blue LED is illuminated.
33840Sstevel@tonic-gate  * Please note that the lock must be released before invoking the
33850Sstevel@tonic-gate  * generic AP configure function.
33860Sstevel@tonic-gate  */
33870Sstevel@tonic-gate static int
33880Sstevel@tonic-gate pcihp_handle_enum_insertion(pcihp_t *pcihp_p, int pci_dev, int opcode,
33890Sstevel@tonic-gate 	int kmflag)
33900Sstevel@tonic-gate {
33910Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
33920Sstevel@tonic-gate 	int rv = PCIHP_FAILURE;
33930Sstevel@tonic-gate 	minor_t ap_minor;
33940Sstevel@tonic-gate 	major_t ap_major;
33950Sstevel@tonic-gate 
33960Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
33970Sstevel@tonic-gate 	slotinfop->hs_csr_location = 0;
33980Sstevel@tonic-gate 	/* we clear the interrupt here. This is a must here. */
33990Sstevel@tonic-gate 	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_SLOT_CONFIGURE);
34000Sstevel@tonic-gate 	/*
34010Sstevel@tonic-gate 	 * disable further interrupt from this board till it is
34020Sstevel@tonic-gate 	 * configured.
34030Sstevel@tonic-gate 	 */
34040Sstevel@tonic-gate 	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_DISABLE_ENUM);
34050Sstevel@tonic-gate 	if (opcode == PCIHP_CLEAR_ENUM) {
34060Sstevel@tonic-gate 		slotinfop->slot_flags |= PCIHP_SLOT_ENUM_INS_PENDING;
34070Sstevel@tonic-gate 		return (PCIHP_SUCCESS);
34080Sstevel@tonic-gate 	}
34090Sstevel@tonic-gate 
34100Sstevel@tonic-gate 	if ((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) ==
34110Sstevel@tonic-gate 					PCIHP_SLOT_AUTO_CFG_EN) {
3412881Sjohnny 		mutex_enter(&slotinfop->slot_mutex);
34130Sstevel@tonic-gate 		rv = pcihp_configure_ap(pcihp_p, pci_dev);
3414881Sjohnny 		mutex_exit(&slotinfop->slot_mutex);
34150Sstevel@tonic-gate 		if (rv != HPC_SUCCESS) {	/* configure failed */
34160Sstevel@tonic-gate 			cmn_err(CE_NOTE, "%s%d: PCI device %x Failed on"
34170Sstevel@tonic-gate 				" Configure", ddi_driver_name(pcihp_p->dip),
34180Sstevel@tonic-gate 				ddi_get_instance(pcihp_p->dip), pci_dev);
34190Sstevel@tonic-gate 			if (pcihp_cpci_blue_led)
34200Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
34210Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_ON);
34220Sstevel@tonic-gate 		}
34230Sstevel@tonic-gate 
34240Sstevel@tonic-gate 		/* pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_CLEAR_ENUM); */
34250Sstevel@tonic-gate 		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_ENABLE_ENUM);
34260Sstevel@tonic-gate 
34270Sstevel@tonic-gate 		if (!rv) {
34280Sstevel@tonic-gate 			ap_major = ddi_driver_major(pcihp_p->dip);
34290Sstevel@tonic-gate 			ap_minor = PCIHP_AP_MINOR_NUM(
34300Sstevel@tonic-gate 			    ddi_get_instance(pcihp_p->dip), pci_dev);
34310Sstevel@tonic-gate 			pcihp_create_occupant_props(pcihp_p->dip,
34320Sstevel@tonic-gate 			    makedevice(ap_major, ap_minor), pci_dev);
34330Sstevel@tonic-gate 
34340Sstevel@tonic-gate 			/*
34350Sstevel@tonic-gate 			 * Sys Event Notification.
34360Sstevel@tonic-gate 			 */
34370Sstevel@tonic-gate 			pcihp_gen_sysevent(slotinfop->name,
34380Sstevel@tonic-gate 				PCIHP_DR_AP_STATE_CHANGE,
34390Sstevel@tonic-gate 				SE_HINT_INSERT, pcihp_p->dip, kmflag);
34400Sstevel@tonic-gate 		}
34410Sstevel@tonic-gate 
34420Sstevel@tonic-gate 	} else
34430Sstevel@tonic-gate 		rv = PCIHP_SUCCESS;
34440Sstevel@tonic-gate 	slotinfop->slot_flags &= ~PCIHP_SLOT_ENUM_INS_PENDING;
34450Sstevel@tonic-gate 	return (rv);
34460Sstevel@tonic-gate }
34470Sstevel@tonic-gate 
34480Sstevel@tonic-gate /*
34490Sstevel@tonic-gate  * Read the Hot Swap Control and Status Register (HS_CSR) and
34500Sstevel@tonic-gate  * place the result in the location pointed to be hs_csr.
34510Sstevel@tonic-gate  */
34520Sstevel@tonic-gate static int
34530Sstevel@tonic-gate pcihp_get_hs_csr(struct pcihp_slotinfo *slotinfop,
34540Sstevel@tonic-gate     ddi_acc_handle_t config_handle, uint8_t *hs_csr)
34550Sstevel@tonic-gate {
34560Sstevel@tonic-gate 	if (slotinfop->hs_csr_location == -1)
34570Sstevel@tonic-gate 		return (PCIHP_FAILURE);
34580Sstevel@tonic-gate 
34590Sstevel@tonic-gate 	if (slotinfop->hs_csr_location == 0) {
34600Sstevel@tonic-gate 		slotinfop->hs_csr_location =
34610Sstevel@tonic-gate 			pcihp_get_hs_csr_location(config_handle);
34620Sstevel@tonic-gate 
34630Sstevel@tonic-gate 		if (slotinfop->hs_csr_location == -1)
34640Sstevel@tonic-gate 			return (PCIHP_FAILURE);
34650Sstevel@tonic-gate 	}
34660Sstevel@tonic-gate 	*hs_csr = pci_config_get8(config_handle, slotinfop->hs_csr_location);
34670Sstevel@tonic-gate 	return (PCIHP_SUCCESS);
34680Sstevel@tonic-gate }
34690Sstevel@tonic-gate 
34700Sstevel@tonic-gate /*
34710Sstevel@tonic-gate  * Write the Hot Swap Control and Status Register (HS_CSR) with
34720Sstevel@tonic-gate  * the value being pointed at by hs_csr.
34730Sstevel@tonic-gate  */
34740Sstevel@tonic-gate static void
34750Sstevel@tonic-gate pcihp_set_hs_csr(struct pcihp_slotinfo *slotinfop,
34760Sstevel@tonic-gate     ddi_acc_handle_t config_handle, uint8_t *hs_csr)
34770Sstevel@tonic-gate {
34780Sstevel@tonic-gate 	if (slotinfop->hs_csr_location == -1)
34790Sstevel@tonic-gate 		return;
34800Sstevel@tonic-gate 	if (slotinfop->hs_csr_location == 0) {
34810Sstevel@tonic-gate 		slotinfop->hs_csr_location =
34820Sstevel@tonic-gate 			pcihp_get_hs_csr_location(config_handle);
34830Sstevel@tonic-gate 		if (slotinfop->hs_csr_location == -1)
34840Sstevel@tonic-gate 			return;
34850Sstevel@tonic-gate 	}
34860Sstevel@tonic-gate 	pci_config_put8(config_handle, slotinfop->hs_csr_location, *hs_csr);
34870Sstevel@tonic-gate 	PCIHP_DEBUG((CE_NOTE, "hs_csr wrote %x, read %x", *hs_csr,
34880Sstevel@tonic-gate 		pci_config_get8(config_handle, slotinfop->hs_csr_location)));
34890Sstevel@tonic-gate }
34900Sstevel@tonic-gate 
34910Sstevel@tonic-gate static int
34920Sstevel@tonic-gate pcihp_get_hs_csr_location(ddi_acc_handle_t config_handle)
34930Sstevel@tonic-gate {
34940Sstevel@tonic-gate 	uint8_t	cap_id;
34950Sstevel@tonic-gate 	uint_t	cap_id_loc;
34960Sstevel@tonic-gate 	uint16_t	status;
34970Sstevel@tonic-gate 	int location = -1;
34980Sstevel@tonic-gate #define	PCI_STAT_ECP_SUPP	0x10
34990Sstevel@tonic-gate 
35000Sstevel@tonic-gate 	/*
35010Sstevel@tonic-gate 	 * Need to check the Status register for ECP support first.
35020Sstevel@tonic-gate 	 * Also please note that for type 1 devices, the
35030Sstevel@tonic-gate 	 * offset could change. Should support type 1 next.
35040Sstevel@tonic-gate 	 */
35050Sstevel@tonic-gate 	status = pci_config_get16(config_handle, PCI_CONF_STAT);
35060Sstevel@tonic-gate 	if (!(status & PCI_STAT_ECP_SUPP)) {
35070Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "No Ext Capabilities for device\n"));
35080Sstevel@tonic-gate 		return (-1);
35090Sstevel@tonic-gate 	}
35100Sstevel@tonic-gate 	cap_id_loc = pci_config_get8(config_handle, PCI_CONF_EXTCAP);
35110Sstevel@tonic-gate 
35120Sstevel@tonic-gate 	/*
35130Sstevel@tonic-gate 	 * Walk the list of capabilities, but don't walk past the end
35140Sstevel@tonic-gate 	 * of the Configuration Space Header.
35150Sstevel@tonic-gate 	 */
35160Sstevel@tonic-gate 	while ((cap_id_loc) && (cap_id_loc < PCI_CONF_HDR_SIZE)) {
35170Sstevel@tonic-gate 
35180Sstevel@tonic-gate 		cap_id = pci_config_get8(config_handle, cap_id_loc);
35190Sstevel@tonic-gate 
35200Sstevel@tonic-gate 		if (cap_id == CPCI_HOTSWAP_CAPID) {
35210Sstevel@tonic-gate 			location = cap_id_loc + PCI_ECP_HS_CSR;
35220Sstevel@tonic-gate 			break;
35230Sstevel@tonic-gate 		}
35240Sstevel@tonic-gate 		cap_id_loc = pci_config_get8(config_handle,
35250Sstevel@tonic-gate 		    cap_id_loc + 1);
35260Sstevel@tonic-gate 	}
35270Sstevel@tonic-gate 	return (location);
35280Sstevel@tonic-gate }
35290Sstevel@tonic-gate 
35300Sstevel@tonic-gate static int
35310Sstevel@tonic-gate pcihp_add_dummy_reg_property(dev_info_t *dip,
35320Sstevel@tonic-gate     uint_t bus, uint_t device, uint_t func)
35330Sstevel@tonic-gate {
35340Sstevel@tonic-gate 	pci_regspec_t dummy_reg;
35350Sstevel@tonic-gate 
35360Sstevel@tonic-gate 	bzero(&dummy_reg, sizeof (dummy_reg));
35370Sstevel@tonic-gate 
35380Sstevel@tonic-gate 	dummy_reg.pci_phys_hi = PCIHP_MAKE_REG_HIGH(bus, device, func, 0);
35390Sstevel@tonic-gate 
35400Sstevel@tonic-gate 	return (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
35410Sstevel@tonic-gate 	    "reg", (int *)&dummy_reg, sizeof (pci_regspec_t)/sizeof (int)));
35420Sstevel@tonic-gate }
35430Sstevel@tonic-gate 
35440Sstevel@tonic-gate static void
35450Sstevel@tonic-gate pcihp_hs_csr_op(pcihp_t *pcihp_p, int pci_dev, int event)
35460Sstevel@tonic-gate {
35470Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
35480Sstevel@tonic-gate 	ddi_acc_handle_t config_handle;
35490Sstevel@tonic-gate 	dev_info_t *dip, *new_child = NULL;
35500Sstevel@tonic-gate 	uint8_t hs_csr;
35510Sstevel@tonic-gate 	int result;
35520Sstevel@tonic-gate 
35530Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
35540Sstevel@tonic-gate 
35550Sstevel@tonic-gate 	if (pcihp_config_setup(&dip, &config_handle, &new_child, pci_dev,
35560Sstevel@tonic-gate 				pcihp_p) != DDI_SUCCESS) {
35570Sstevel@tonic-gate 		return;
35580Sstevel@tonic-gate 	}
35590Sstevel@tonic-gate 
35600Sstevel@tonic-gate 	result = pcihp_get_hs_csr(slotinfop, config_handle, (uint8_t *)&hs_csr);
35610Sstevel@tonic-gate 	if ((result != PCIHP_SUCCESS) || (event == -1)) {
35620Sstevel@tonic-gate 		pcihp_config_teardown(&config_handle, &new_child, pci_dev,
35630Sstevel@tonic-gate 							pcihp_p);
35640Sstevel@tonic-gate 		return;
35650Sstevel@tonic-gate 	}
35660Sstevel@tonic-gate 
35670Sstevel@tonic-gate 	hs_csr &= 0xf;
35680Sstevel@tonic-gate 	switch (event) {
35690Sstevel@tonic-gate 		case HPC_EVENT_SLOT_BLUE_LED_ON:
35700Sstevel@tonic-gate 			hs_csr |= HS_CSR_LOO;
35710Sstevel@tonic-gate 			break;
35720Sstevel@tonic-gate 		case HPC_EVENT_SLOT_BLUE_LED_OFF:
35730Sstevel@tonic-gate 			hs_csr &= ~HS_CSR_LOO;
35740Sstevel@tonic-gate 			break;
35750Sstevel@tonic-gate 		case HPC_EVENT_SLOT_CONFIGURE:
35760Sstevel@tonic-gate 			hs_csr |= HS_CSR_INS;	/* clear INS */
35770Sstevel@tonic-gate 			break;
35780Sstevel@tonic-gate 		case HPC_EVENT_CLEAR_ENUM:
35790Sstevel@tonic-gate 			hs_csr |= (HS_CSR_INS | HS_CSR_EXT);
35800Sstevel@tonic-gate 			break;
35810Sstevel@tonic-gate 		case HPC_EVENT_SLOT_UNCONFIGURE:
35820Sstevel@tonic-gate 			hs_csr |= HS_CSR_EXT;	/* clear EXT */
35830Sstevel@tonic-gate 			break;
35840Sstevel@tonic-gate 		case HPC_EVENT_ENABLE_ENUM:
35850Sstevel@tonic-gate 			hs_csr &= ~HS_CSR_EIM;
35860Sstevel@tonic-gate 			break;
35870Sstevel@tonic-gate 		case HPC_EVENT_DISABLE_ENUM:
35880Sstevel@tonic-gate 			hs_csr |= HS_CSR_EIM;
35890Sstevel@tonic-gate 			break;
35900Sstevel@tonic-gate 		case HPC_EVENT_SLOT_NOT_HEALTHY:
35910Sstevel@tonic-gate 		case HPC_EVENT_SLOT_HEALTHY_OK:
35920Sstevel@tonic-gate 		default:
35930Sstevel@tonic-gate 			break;
35940Sstevel@tonic-gate 	}
35950Sstevel@tonic-gate 	pcihp_set_hs_csr(slotinfop, config_handle, (uint8_t *)&hs_csr);
35960Sstevel@tonic-gate 	pcihp_config_teardown(&config_handle, &new_child, pci_dev, pcihp_p);
35970Sstevel@tonic-gate }
35980Sstevel@tonic-gate 
35990Sstevel@tonic-gate static int
36000Sstevel@tonic-gate pcihp_config_setup(dev_info_t **dip, ddi_acc_handle_t *handle,
36010Sstevel@tonic-gate 			dev_info_t **new_child, int pci_dev, pcihp_t *pcihp_p)
36020Sstevel@tonic-gate {
36030Sstevel@tonic-gate 	dev_info_t *pdip = pcihp_p->dip;
36040Sstevel@tonic-gate 	int bus, len, rc = DDI_SUCCESS;
36050Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
36060Sstevel@tonic-gate 	hpc_slot_state_t rstate;
36070Sstevel@tonic-gate 	ddi_acc_hdl_t *hp;
36080Sstevel@tonic-gate 	struct bus_range {
36090Sstevel@tonic-gate 		uint32_t lo;
36100Sstevel@tonic-gate 		uint32_t hi;
36110Sstevel@tonic-gate 	} pci_bus_range;
36120Sstevel@tonic-gate 
36130Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
36140Sstevel@tonic-gate 
36150Sstevel@tonic-gate 	/*
36160Sstevel@tonic-gate 	 * If declared failed, don't allow Config operations.
36170Sstevel@tonic-gate 	 * Otherwise, if good or failing, it is assumed Ok
36180Sstevel@tonic-gate 	 * to get config data.
36190Sstevel@tonic-gate 	 */
36200Sstevel@tonic-gate 	if (slotinfop->condition == AP_COND_FAILED) {
36210Sstevel@tonic-gate 		return (PCIHP_FAILURE);
36220Sstevel@tonic-gate 	}
36230Sstevel@tonic-gate 	/*
36240Sstevel@tonic-gate 	 * check to see if there is a hardware present first.
36250Sstevel@tonic-gate 	 * If no hardware present, no need to probe this slot.
36260Sstevel@tonic-gate 	 * We can do this first probably as a first step towards
36270Sstevel@tonic-gate 	 * safeguarding from accidental removal (we don't support it!).
36280Sstevel@tonic-gate 	 */
36290Sstevel@tonic-gate 	if (hpc_nexus_control(slotinfop->slot_hdl,
36300Sstevel@tonic-gate 			HPC_CTRL_GET_SLOT_STATE, (caddr_t)&rstate) != 0) {
36310Sstevel@tonic-gate 		return (DDI_FAILURE);
36320Sstevel@tonic-gate 	}
36330Sstevel@tonic-gate 
36340Sstevel@tonic-gate 	if (rstate != HPC_SLOT_CONNECTED) {
36350Sstevel@tonic-gate 		/* error. slot must be connected */
36360Sstevel@tonic-gate 		return (DDI_FAILURE);
36370Sstevel@tonic-gate 	}
36380Sstevel@tonic-gate 	*new_child = NULL;
36390Sstevel@tonic-gate 
36400Sstevel@tonic-gate 	/*
36410Sstevel@tonic-gate 	 * If there is no dip then we need to see if an
36420Sstevel@tonic-gate 	 * adapter has just been hot plugged.
36430Sstevel@tonic-gate 	 */
36440Sstevel@tonic-gate 	len = sizeof (struct bus_range);
3645506Scth 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, pdip,
36460Sstevel@tonic-gate 	    0, "bus-range",
36470Sstevel@tonic-gate 	    (caddr_t)&pci_bus_range, &len) != DDI_SUCCESS) {
36480Sstevel@tonic-gate 
36490Sstevel@tonic-gate 		return (PCIHP_FAILURE);
36500Sstevel@tonic-gate 	}
36510Sstevel@tonic-gate 
36520Sstevel@tonic-gate 	/* primary bus number of this bus node */
36530Sstevel@tonic-gate 	bus = pci_bus_range.lo;
36540Sstevel@tonic-gate 
36550Sstevel@tonic-gate 	if (ndi_devi_alloc(pdip, DEVI_PSEUDO_NEXNAME,
3656789Sahrens 	    (pnode_t)DEVI_SID_NODEID, dip) != NDI_SUCCESS) {
36570Sstevel@tonic-gate 
36580Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "Failed to alloc probe node\n"));
36590Sstevel@tonic-gate 		return (PCIHP_FAILURE);
36600Sstevel@tonic-gate 	}
36610Sstevel@tonic-gate 
36620Sstevel@tonic-gate 	if (pcihp_add_dummy_reg_property(*dip, bus,
36630Sstevel@tonic-gate 	    pci_dev, 0) != DDI_SUCCESS) {
36640Sstevel@tonic-gate 
36650Sstevel@tonic-gate 		(void) ndi_devi_free(*dip);
36660Sstevel@tonic-gate 		return (PCIHP_FAILURE);
36670Sstevel@tonic-gate 	}
36680Sstevel@tonic-gate 
36690Sstevel@tonic-gate 	/*
36700Sstevel@tonic-gate 	 * Probe for a device. Possibly a non (c)PCI board could be sitting
36710Sstevel@tonic-gate 	 * here which would never respond to PCI config cycles - in which
36720Sstevel@tonic-gate 	 * case we return. Eventually a configure operation would fail.
36730Sstevel@tonic-gate 	 */
36740Sstevel@tonic-gate 	if (pci_config_setup(*dip, handle) != DDI_SUCCESS) {
36750Sstevel@tonic-gate 		cmn_err(CE_WARN, "Cannot set config space map for"
36760Sstevel@tonic-gate 		    " pci device number %d", pci_dev);
36770Sstevel@tonic-gate 		(void) ndi_devi_free(*dip);
36780Sstevel@tonic-gate 		return (PCIHP_FAILURE);
36790Sstevel@tonic-gate 	}
36800Sstevel@tonic-gate 
36810Sstevel@tonic-gate 	/*
36820Sstevel@tonic-gate 	 * See if there is any PCI HW at this location
36830Sstevel@tonic-gate 	 * by reading the Vendor ID.  If it returns with 0xffff
36840Sstevel@tonic-gate 	 * then there is no hardware at this location.
36850Sstevel@tonic-gate 	 */
3686881Sjohnny 	if (pcihp_indirect_map(*dip) == DDI_SUCCESS) {
36870Sstevel@tonic-gate 		if (pci_config_get16(*handle, 0) == 0xffff) {
36880Sstevel@tonic-gate 			pci_config_teardown(handle);
36890Sstevel@tonic-gate 			(void) ndi_devi_free(*dip);
36900Sstevel@tonic-gate 			return (PCIHP_FAILURE);
36910Sstevel@tonic-gate 		}
36920Sstevel@tonic-gate 	} else {
36930Sstevel@tonic-gate 		/* Check if mapping is OK */
36940Sstevel@tonic-gate 		hp = impl_acc_hdl_get(*handle);
36950Sstevel@tonic-gate 
36960Sstevel@tonic-gate 		if (ddi_peek16(*dip, (int16_t *)(hp->ah_addr),
36970Sstevel@tonic-gate 			(int16_t *)0) != DDI_SUCCESS) {
36980Sstevel@tonic-gate #ifdef DEBUG
36990Sstevel@tonic-gate 			cmn_err(CE_WARN, "Cannot Map PCI config space for "
37000Sstevel@tonic-gate 			    "device number %d", pci_dev);
37010Sstevel@tonic-gate #endif
37020Sstevel@tonic-gate 			pci_config_teardown(handle);
37030Sstevel@tonic-gate 			(void) ndi_devi_free(*dip);
37040Sstevel@tonic-gate 			return (PCIHP_FAILURE);
37050Sstevel@tonic-gate 		}
37060Sstevel@tonic-gate 	}
37070Sstevel@tonic-gate 
37080Sstevel@tonic-gate 	*new_child = *dip;
37090Sstevel@tonic-gate 	return (rc);
37100Sstevel@tonic-gate 
37110Sstevel@tonic-gate }
37120Sstevel@tonic-gate 
37130Sstevel@tonic-gate static void
37140Sstevel@tonic-gate pcihp_config_teardown(ddi_acc_handle_t *handle,
37150Sstevel@tonic-gate 			dev_info_t **new_child, int pci_dev, pcihp_t *pcihp_p)
37160Sstevel@tonic-gate {
37170Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop = &pcihp_p->slotinfo[pci_dev];
37180Sstevel@tonic-gate 
37190Sstevel@tonic-gate 	pci_config_teardown(handle);
37200Sstevel@tonic-gate 	if (*new_child) {
37210Sstevel@tonic-gate 		(void) ndi_devi_free(*new_child);
37220Sstevel@tonic-gate 		/*
37230Sstevel@tonic-gate 		 * If occupant not configured, reset HS_CSR location
37240Sstevel@tonic-gate 		 * so that we reprobe. This covers cases where
37250Sstevel@tonic-gate 		 * the receptacle had a status change without a
37260Sstevel@tonic-gate 		 * notification to the framework.
37270Sstevel@tonic-gate 		 */
37280Sstevel@tonic-gate 		if (slotinfop->ostate != AP_OSTATE_CONFIGURED)
37290Sstevel@tonic-gate 			slotinfop->hs_csr_location = 0;
37300Sstevel@tonic-gate 	}
37310Sstevel@tonic-gate }
37320Sstevel@tonic-gate 
37330Sstevel@tonic-gate static int
37340Sstevel@tonic-gate pcihp_get_board_type(struct pcihp_slotinfo *slotinfop)
37350Sstevel@tonic-gate {
37360Sstevel@tonic-gate 	hpc_board_type_t board_type;
37370Sstevel@tonic-gate 
37380Sstevel@tonic-gate 	/*
37390Sstevel@tonic-gate 	 * Get board type data structure, hpc_board_type_t.
37400Sstevel@tonic-gate 	 */
37410Sstevel@tonic-gate 	if (hpc_nexus_control(slotinfop->slot_hdl, HPC_CTRL_GET_BOARD_TYPE,
37420Sstevel@tonic-gate 				(caddr_t)&board_type) != 0) {
37430Sstevel@tonic-gate 
37440Sstevel@tonic-gate 		cmn_err(CE_WARN, "Cannot Get Board Type..");
37450Sstevel@tonic-gate 		return (-1);
37460Sstevel@tonic-gate 	}
37470Sstevel@tonic-gate 
37480Sstevel@tonic-gate 	/*
37490Sstevel@tonic-gate 	 * We expect the Hotswap Controller to tell us if the board is
37500Sstevel@tonic-gate 	 * a hotswap board or not, as it probably cannot differentiate
37510Sstevel@tonic-gate 	 * between a basic hotswap board, a non hotswap board and a
37520Sstevel@tonic-gate 	 * hotswap nonfriendly board.
37530Sstevel@tonic-gate 	 * So here is the logic to differentiate between the various
37540Sstevel@tonic-gate 	 * types of cPCI boards.
37550Sstevel@tonic-gate 	 * In case if the HSC returns board type as unknown, we assign
37560Sstevel@tonic-gate 	 * the default board type as defined by a configurable variable
37570Sstevel@tonic-gate 	 * for a BHS, nonfriendly FHS and non HS board.
37580Sstevel@tonic-gate 	 */
37590Sstevel@tonic-gate 	if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
37600Sstevel@tonic-gate 		if (slotinfop->hs_csr_location > 0)
37610Sstevel@tonic-gate 			board_type = HPC_BOARD_CPCI_FULL_HS;
37620Sstevel@tonic-gate 		else {
37630Sstevel@tonic-gate 			if (board_type == HPC_BOARD_CPCI_HS) {
37640Sstevel@tonic-gate 				if (slotinfop->hs_csr_location
37650Sstevel@tonic-gate 					== -1)
37660Sstevel@tonic-gate 					board_type =
37670Sstevel@tonic-gate 					HPC_BOARD_CPCI_BASIC_HS;
37680Sstevel@tonic-gate 			}
37690Sstevel@tonic-gate 			if (board_type == HPC_BOARD_UNKNOWN) {
37700Sstevel@tonic-gate 				if (slotinfop->hs_csr_location
37710Sstevel@tonic-gate 					== -1) {
37720Sstevel@tonic-gate 					board_type =
37730Sstevel@tonic-gate 					pcihp_cpci_board_type;
37740Sstevel@tonic-gate 				} else if
37750Sstevel@tonic-gate 				(slotinfop->hs_csr_location
37760Sstevel@tonic-gate 					!= 0) {
37770Sstevel@tonic-gate 					board_type =
37780Sstevel@tonic-gate 					HPC_BOARD_CPCI_FULL_HS;
37790Sstevel@tonic-gate 				}
37800Sstevel@tonic-gate 			}
37810Sstevel@tonic-gate 		}
37820Sstevel@tonic-gate 		/*
37830Sstevel@tonic-gate 		 * If board type is a non hotswap board, then we must
37840Sstevel@tonic-gate 		 * deny a unconfigure operation. So set this flag.
37850Sstevel@tonic-gate 		 * Strictly speaking, there is no reason not to disallow
37860Sstevel@tonic-gate 		 * a unconfigure operation on nonhotswap boards. But this
37870Sstevel@tonic-gate 		 * is the only way we can prevent a user from accidentally
37880Sstevel@tonic-gate 		 * removing the board and damaging it.
37890Sstevel@tonic-gate 		 */
37900Sstevel@tonic-gate 		if (board_type == HPC_BOARD_CPCI_NON_HS)
37910Sstevel@tonic-gate 			slotinfop->slot_flags |=
37920Sstevel@tonic-gate 				PCIHP_SLOT_DEV_NON_HOTPLUG;
37930Sstevel@tonic-gate 		else
37940Sstevel@tonic-gate 			slotinfop->slot_flags &=
37950Sstevel@tonic-gate 				~PCIHP_SLOT_DEV_NON_HOTPLUG;
37960Sstevel@tonic-gate 	}
37970Sstevel@tonic-gate 	return (board_type);
37980Sstevel@tonic-gate }
37990Sstevel@tonic-gate 
38000Sstevel@tonic-gate 
38010Sstevel@tonic-gate /*
38020Sstevel@tonic-gate  * Generate the System Event with a possible hint.
38030Sstevel@tonic-gate  */
38040Sstevel@tonic-gate static void
38050Sstevel@tonic-gate pcihp_gen_sysevent(char *slot_name, int event_sub_class, int hint,
38060Sstevel@tonic-gate 				dev_info_t *self, int kmflag)
38070Sstevel@tonic-gate {
38080Sstevel@tonic-gate 
38090Sstevel@tonic-gate 	int err;
38100Sstevel@tonic-gate 	char *ev_subclass = NULL;
38110Sstevel@tonic-gate 	sysevent_id_t eid;
38120Sstevel@tonic-gate 	nvlist_t *ev_attr_list = NULL;
38130Sstevel@tonic-gate 	char attach_pnt[MAXPATHLEN];
38140Sstevel@tonic-gate 
38150Sstevel@tonic-gate 	/*
38160Sstevel@tonic-gate 	 * Minor device name (AP) will be bus path
38170Sstevel@tonic-gate 	 * concatenated with slot name
38180Sstevel@tonic-gate 	 */
38190Sstevel@tonic-gate 
38200Sstevel@tonic-gate 	(void) strcpy(attach_pnt, PCIHP_DEVICES_STR);
38210Sstevel@tonic-gate 	(void) ddi_pathname(self, attach_pnt + strlen(PCIHP_DEVICES_STR));
38220Sstevel@tonic-gate 	(void) strcat(attach_pnt, ":");
38230Sstevel@tonic-gate 	(void) strcat(attach_pnt, slot_name);
38240Sstevel@tonic-gate 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, kmflag);
38250Sstevel@tonic-gate 	if (err != 0) {
38260Sstevel@tonic-gate 		cmn_err(CE_WARN,
38270Sstevel@tonic-gate 		    "%s%d: Failed to allocate memory "
38280Sstevel@tonic-gate 		    "for event attributes%s", ddi_driver_name(self),
38290Sstevel@tonic-gate 		    ddi_get_instance(self), ESC_DR_AP_STATE_CHANGE);
38300Sstevel@tonic-gate 		return;
38310Sstevel@tonic-gate 	}
38320Sstevel@tonic-gate 
38330Sstevel@tonic-gate 	switch (event_sub_class) {
38340Sstevel@tonic-gate 
38350Sstevel@tonic-gate 	/* event sub class: ESC_DR_AP_STATE_CHANGE */
38360Sstevel@tonic-gate 	case PCIHP_DR_AP_STATE_CHANGE:
38370Sstevel@tonic-gate 
38380Sstevel@tonic-gate 		ev_subclass = ESC_DR_AP_STATE_CHANGE;
38390Sstevel@tonic-gate 
38400Sstevel@tonic-gate 		switch (hint) {
38410Sstevel@tonic-gate 
38420Sstevel@tonic-gate 		case SE_NO_HINT:	/* fall through */
38430Sstevel@tonic-gate 		case SE_HINT_INSERT:	/* fall through */
38440Sstevel@tonic-gate 		case SE_HINT_REMOVE:
38450Sstevel@tonic-gate 
38460Sstevel@tonic-gate 
38470Sstevel@tonic-gate 			err = nvlist_add_string(ev_attr_list, DR_HINT,
38480Sstevel@tonic-gate 			    SE_HINT2STR(hint));
38490Sstevel@tonic-gate 
38500Sstevel@tonic-gate 			if (err != 0) {
38510Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s%d: Failed to add attr [%s]"
38520Sstevel@tonic-gate 					" for %s event", ddi_driver_name(self),
38530Sstevel@tonic-gate 					ddi_get_instance(self),
38540Sstevel@tonic-gate 					DR_HINT, ESC_DR_AP_STATE_CHANGE);
38550Sstevel@tonic-gate 				nvlist_free(ev_attr_list);
38560Sstevel@tonic-gate 				return;
38570Sstevel@tonic-gate 			}
38580Sstevel@tonic-gate 			break;
38590Sstevel@tonic-gate 
38600Sstevel@tonic-gate 		default:
38610Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: Unknown hint on sysevent",
38620Sstevel@tonic-gate 				ddi_driver_name(self), ddi_get_instance(self));
38630Sstevel@tonic-gate 			nvlist_free(ev_attr_list);
38640Sstevel@tonic-gate 			return;
38650Sstevel@tonic-gate 		}
38660Sstevel@tonic-gate 
38670Sstevel@tonic-gate 		break;
38680Sstevel@tonic-gate 
38690Sstevel@tonic-gate 	/* event sub class: ESC_DR_REQ */
38700Sstevel@tonic-gate 	case PCIHP_DR_REQ:
38710Sstevel@tonic-gate 
38720Sstevel@tonic-gate 		ev_subclass = ESC_DR_REQ;
38730Sstevel@tonic-gate 
38740Sstevel@tonic-gate 		switch (hint) {
38750Sstevel@tonic-gate 
38760Sstevel@tonic-gate 		case SE_INVESTIGATE_RES:	/* fall through */
38770Sstevel@tonic-gate 		case SE_INCOMING_RES:	/* fall through */
38780Sstevel@tonic-gate 		case SE_OUTGOING_RES:	/* fall through */
38790Sstevel@tonic-gate 
38800Sstevel@tonic-gate 			err = nvlist_add_string(ev_attr_list, DR_REQ_TYPE,
38810Sstevel@tonic-gate 				SE_REQ2STR(hint));
38820Sstevel@tonic-gate 
38830Sstevel@tonic-gate 			if (err != 0) {
38840Sstevel@tonic-gate 				cmn_err(CE_WARN,
38850Sstevel@tonic-gate 					"%s%d: Failed to add attr [%s] "
38860Sstevel@tonic-gate 					"for %s event", ddi_driver_name(self),
38870Sstevel@tonic-gate 					ddi_get_instance(self),
38880Sstevel@tonic-gate 					DR_REQ_TYPE, ESC_DR_REQ);
38890Sstevel@tonic-gate 				nvlist_free(ev_attr_list);
38900Sstevel@tonic-gate 				return;
38910Sstevel@tonic-gate 			}
38920Sstevel@tonic-gate 			break;
38930Sstevel@tonic-gate 
38940Sstevel@tonic-gate 		default:
38950Sstevel@tonic-gate 			cmn_err(CE_WARN,
38960Sstevel@tonic-gate 				"%s%d:  Unknown hint on sysevent",
38970Sstevel@tonic-gate 				ddi_driver_name(self), ddi_get_instance(self));
38980Sstevel@tonic-gate 			nvlist_free(ev_attr_list);
38990Sstevel@tonic-gate 			return;
39000Sstevel@tonic-gate 		}
39010Sstevel@tonic-gate 
39020Sstevel@tonic-gate 		break;
39030Sstevel@tonic-gate 
39040Sstevel@tonic-gate 	default:
39050Sstevel@tonic-gate 		cmn_err(CE_WARN,
39060Sstevel@tonic-gate 			"%s%d:  Unknown Event subclass", ddi_driver_name(self),
39070Sstevel@tonic-gate 			ddi_get_instance(self));
39080Sstevel@tonic-gate 		nvlist_free(ev_attr_list);
39090Sstevel@tonic-gate 		return;
39100Sstevel@tonic-gate 	}
39110Sstevel@tonic-gate 
39120Sstevel@tonic-gate 	/*
39130Sstevel@tonic-gate 	 * Add attachment point as attribute (common attribute)
39140Sstevel@tonic-gate 	 */
39150Sstevel@tonic-gate 
39160Sstevel@tonic-gate 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, attach_pnt);
39170Sstevel@tonic-gate 
39180Sstevel@tonic-gate 	if (err != 0) {
39190Sstevel@tonic-gate 		cmn_err(CE_WARN,
39200Sstevel@tonic-gate 			"%s%d: Failed to add attr [%s] for %s event",
39210Sstevel@tonic-gate 			ddi_driver_name(self), ddi_get_instance(self),
39220Sstevel@tonic-gate 			DR_AP_ID, EC_DR);
39230Sstevel@tonic-gate 		nvlist_free(ev_attr_list);
39240Sstevel@tonic-gate 		return;
39250Sstevel@tonic-gate 	}
39260Sstevel@tonic-gate 
39270Sstevel@tonic-gate 
39280Sstevel@tonic-gate 	/*
39290Sstevel@tonic-gate 	 * Log this event with sysevent framework.
39300Sstevel@tonic-gate 	 */
39310Sstevel@tonic-gate 
39320Sstevel@tonic-gate 	err = ddi_log_sysevent(self, DDI_VENDOR_SUNW, EC_DR,
39330Sstevel@tonic-gate 	    ev_subclass, ev_attr_list, &eid,
39340Sstevel@tonic-gate 	    ((kmflag == KM_SLEEP) ? DDI_SLEEP : DDI_NOSLEEP));
39350Sstevel@tonic-gate 	if (err != 0) {
39360Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: Failed to log %s event",
39370Sstevel@tonic-gate 			ddi_driver_name(self), ddi_get_instance(self), EC_DR);
39380Sstevel@tonic-gate 	}
39390Sstevel@tonic-gate 
39400Sstevel@tonic-gate 	nvlist_free(ev_attr_list);
39410Sstevel@tonic-gate }
39420Sstevel@tonic-gate 
39430Sstevel@tonic-gate int
39440Sstevel@tonic-gate pcihp_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
39450Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp)
39460Sstevel@tonic-gate {
39470Sstevel@tonic-gate 	int pci_dev;
39480Sstevel@tonic-gate 
39490Sstevel@tonic-gate 	if (dev == DDI_DEV_T_ANY)
39500Sstevel@tonic-gate 		goto skip;
39510Sstevel@tonic-gate 
39520Sstevel@tonic-gate 	if (strcmp(name, "pci-occupant") == 0) {
39530Sstevel@tonic-gate 		pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(getminor(dev));
39540Sstevel@tonic-gate 		pcihp_create_occupant_props_nolock(dip, dev, pci_dev);
39550Sstevel@tonic-gate 	}
39560Sstevel@tonic-gate 	/* other cases... */
39570Sstevel@tonic-gate skip:
39580Sstevel@tonic-gate 	return (ddi_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp));
39590Sstevel@tonic-gate }
39600Sstevel@tonic-gate 
39610Sstevel@tonic-gate /*
39620Sstevel@tonic-gate  * this function is called only for SPARC platforms, where we may have
39630Sstevel@tonic-gate  * a mix n' match of direct vs indirectly mapped configuration space.
39640Sstevel@tonic-gate  * On x86, this function should always return success since the configuration
39650Sstevel@tonic-gate  * space is always indirect mapped.
39660Sstevel@tonic-gate  */
39670Sstevel@tonic-gate /*ARGSUSED*/
39680Sstevel@tonic-gate static int
39690Sstevel@tonic-gate pcihp_indirect_map(dev_info_t *dip)
39700Sstevel@tonic-gate {
39710Sstevel@tonic-gate #if defined(__sparc)
39720Sstevel@tonic-gate 	int rc = DDI_FAILURE;
39730Sstevel@tonic-gate 
39740Sstevel@tonic-gate 	if (ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(dip), 0,
3975881Sjohnny 			PCI_DEV_CONF_MAP_PROP, DDI_FAILURE) != DDI_FAILURE)
39760Sstevel@tonic-gate 		rc = DDI_SUCCESS;
39770Sstevel@tonic-gate 	else
39780Sstevel@tonic-gate 		if (ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(dip),
3979881Sjohnny 				0, PCI_BUS_CONF_MAP_PROP,
39800Sstevel@tonic-gate 				DDI_FAILURE) != DDI_FAILURE)
39810Sstevel@tonic-gate 			rc = DDI_SUCCESS;
39820Sstevel@tonic-gate 	return (rc);
39830Sstevel@tonic-gate #else
39840Sstevel@tonic-gate 	return (DDI_SUCCESS);
39850Sstevel@tonic-gate #endif
39860Sstevel@tonic-gate }
3987