17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5f94c6026Sjj  * Common Development and Distribution License (the "License").
6f94c6026Sjj  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2226947304SEvan Yan  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
263fe80ca4SDan Cross /*
273fe80ca4SDan Cross  * Copyright 2023 Oxide Computer Company
283fe80ca4SDan Cross  */
293fe80ca4SDan Cross 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * **********************************************************************
327c478bd9Sstevel@tonic-gate  * Extension module for PCI nexus drivers to support PCI Hot Plug feature.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * DESCRIPTION:
357c478bd9Sstevel@tonic-gate  *    This module basically implements "devctl" and Attachment Point device
367c478bd9Sstevel@tonic-gate  *    nodes for hot plug operations. The cb_ops functions needed for access
377c478bd9Sstevel@tonic-gate  *    to these device nodes are also implemented. For hotplug operations
387c478bd9Sstevel@tonic-gate  *    on Attachment Points it interacts with the hotplug services (HPS)
397c478bd9Sstevel@tonic-gate  *    framework. A pci nexus driver would simply call pcihp_init() in its
407c478bd9Sstevel@tonic-gate  *    attach() function and pcihp_uninit() call in its detach() function.
417c478bd9Sstevel@tonic-gate  * **********************************************************************
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <sys/conf.h>
457c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
467c478bd9Sstevel@tonic-gate #include <sys/debug.h>
477c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
487c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
497c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
507c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
517c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
527c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
537c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
547c478bd9Sstevel@tonic-gate #include <sys/ddipropdefs.h>
557c478bd9Sstevel@tonic-gate #include <sys/open.h>
567c478bd9Sstevel@tonic-gate #include <sys/file.h>
577c478bd9Sstevel@tonic-gate #include <sys/stat.h>
587c478bd9Sstevel@tonic-gate #include <sys/pci.h>
597c478bd9Sstevel@tonic-gate #include <sys/pci_impl.h>
607c478bd9Sstevel@tonic-gate #include <sys/devctl.h>
617c478bd9Sstevel@tonic-gate #include <sys/hotplug/hpcsvc.h>
627c478bd9Sstevel@tonic-gate #include <sys/hotplug/pci/pcicfg.h>
637c478bd9Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h>
647c478bd9Sstevel@tonic-gate #include <sys/sysevent.h>
657c478bd9Sstevel@tonic-gate #include <sys/sysevent/eventdefs.h>
667c478bd9Sstevel@tonic-gate #include <sys/sysevent/dr.h>
677c478bd9Sstevel@tonic-gate #include <sys/fs/dv_node.h>
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate  * NOTE:
717c478bd9Sstevel@tonic-gate  * This module depends on PCI Configurator module (misc/pcicfg),
727c478bd9Sstevel@tonic-gate  * Hot Plug Services framework module (misc/hpcsvc) and Bus Resource
737c478bd9Sstevel@tonic-gate  * Allocator module (misc/busra).
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * ************************************************************************
787c478bd9Sstevel@tonic-gate  * *** Implementation specific data structures/definitions.		***
797c478bd9Sstevel@tonic-gate  * ************************************************************************
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /* soft state */
837c478bd9Sstevel@tonic-gate typedef enum { PCIHP_SOFT_STATE_CLOSED, PCIHP_SOFT_STATE_OPEN,
847c478bd9Sstevel@tonic-gate 		PCIHP_SOFT_STATE_OPEN_EXCL } pcihp_soft_state_t;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #define	PCI_MAX_DEVS	32	/* max. number of devices on a pci bus */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /* the following correspond to sysevent defined subclasses */
897c478bd9Sstevel@tonic-gate #define	PCIHP_DR_AP_STATE_CHANGE	0
907c478bd9Sstevel@tonic-gate #define	PCIHP_DR_REQ			1
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*  pcihp_get_soft_state() command argument */
937c478bd9Sstevel@tonic-gate #define	PCIHP_DR_NOOP			0
947c478bd9Sstevel@tonic-gate #define	PCIHP_DR_BUS_CONFIGURE		1
957c478bd9Sstevel@tonic-gate #define	PCIHP_DR_BUS_UNCONFIGURE	2
967c478bd9Sstevel@tonic-gate #define	PCIHP_DR_SLOT_ENTER		4
977c478bd9Sstevel@tonic-gate #define	PCIHP_DR_SLOT_EXIT		8
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*  hot plug bus state */
1007c478bd9Sstevel@tonic-gate enum { PCIHP_BUS_INITIALIZING, PCIHP_BUS_UNCONFIGURED,
1017c478bd9Sstevel@tonic-gate 		PCIHP_BUS_CONFIGURED };
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  * Soft state structure associated with each hot plug pci bus instance.
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate typedef struct pcihp {
1077c478bd9Sstevel@tonic-gate 	struct pcihp		*nextp;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	/* devinfo pointer to the pci bus node */
1107c478bd9Sstevel@tonic-gate 	dev_info_t		*dip;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	/* soft state flags: PCIHP_SOFT_STATE_* */
1137c478bd9Sstevel@tonic-gate 	pcihp_soft_state_t	soft_state;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	/* global mutex to serialize exclusive access to the bus */
1167c478bd9Sstevel@tonic-gate 	kmutex_t		mutex;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	/* slot information structure */
1197c478bd9Sstevel@tonic-gate 	struct pcihp_slotinfo {
1207c478bd9Sstevel@tonic-gate 		hpc_slot_t	slot_hdl;	/* HPS slot handle */
1217c478bd9Sstevel@tonic-gate 		ap_rstate_t	rstate;		/* state of Receptacle */
1227c478bd9Sstevel@tonic-gate 		ap_ostate_t	ostate;		/* state of the Occupant */
1237c478bd9Sstevel@tonic-gate 		ap_condition_t	condition;	/* condition of the occupant */
1247c478bd9Sstevel@tonic-gate 		time32_t	last_change;	/* XXX needed? */
1257c478bd9Sstevel@tonic-gate 		uint32_t	event_mask;	/* last event mask registered */
1267c478bd9Sstevel@tonic-gate 		char		*name;		/* slot logical name */
1277c478bd9Sstevel@tonic-gate 		uint_t		slot_flags;
1287c478bd9Sstevel@tonic-gate 		uint16_t	slot_type;	/* slot type: pci or cpci */
1297c478bd9Sstevel@tonic-gate 		uint16_t	slot_capabilities; /* 64bit, etc. */
1307c478bd9Sstevel@tonic-gate 		int		hs_csr_location; /* Location of HS_CSR */
1317c478bd9Sstevel@tonic-gate 		kmutex_t	slot_mutex;	/* mutex to serialize hotplug */
1327c478bd9Sstevel@tonic-gate 						/* operations on the slot */
1337c478bd9Sstevel@tonic-gate 	} slotinfo[PCI_MAX_DEVS];
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	/* misc. bus attributes */
1367c478bd9Sstevel@tonic-gate 	uint_t			bus_flags;
1377c478bd9Sstevel@tonic-gate 	uint_t			bus_state;
1387c478bd9Sstevel@tonic-gate 	uint_t			slots_active;
1397c478bd9Sstevel@tonic-gate } pcihp_t;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * Bit definitions for slot_flags field:
1437c478bd9Sstevel@tonic-gate  *
1447c478bd9Sstevel@tonic-gate  *	PCIHP_SLOT_AUTO_CFG_EN	This flags is set if nexus can do auto
1457c478bd9Sstevel@tonic-gate  *				configuration of hot plugged card on this slot
1467c478bd9Sstevel@tonic-gate  *				if the hardware reports the hot plug events.
1477c478bd9Sstevel@tonic-gate  *
1487c478bd9Sstevel@tonic-gate  *	PCIHP_SLOT_DISABLED	Slot is disabled for hotplug operations.
1497c478bd9Sstevel@tonic-gate  *
1507c478bd9Sstevel@tonic-gate  *	PCIHP_SLOT_NOT_HEALTHY	HEALTHY# signal is not OK on this slot.
1517c478bd9Sstevel@tonic-gate  */
1527c478bd9Sstevel@tonic-gate #define	PCIHP_SLOT_AUTO_CFG_EN		0x1
1537c478bd9Sstevel@tonic-gate #define	PCIHP_SLOT_DISABLED		0x2
1547c478bd9Sstevel@tonic-gate #define	PCIHP_SLOT_NOT_HEALTHY		0x4
1557c478bd9Sstevel@tonic-gate #define	PCIHP_SLOT_DEV_NON_HOTPLUG	0x8
1567c478bd9Sstevel@tonic-gate #define	PCIHP_SLOT_ENUM_INS_PENDING	0x10
1577c478bd9Sstevel@tonic-gate #define	PCIHP_SLOT_ENUM_EXT_PENDING	0x20
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate /*
1607c478bd9Sstevel@tonic-gate  * Bit definitions for bus_flags field:
1617c478bd9Sstevel@tonic-gate  *
1627c478bd9Sstevel@tonic-gate  *	PCIHP_BUS_66MHZ	Bus is running at 66Mhz.
1637c478bd9Sstevel@tonic-gate  */
1647c478bd9Sstevel@tonic-gate #define	PCIHP_BUS_66MHZ		0x1
1657c478bd9Sstevel@tonic-gate #define	PCIHP_BUS_ENUM_RADIAL	0x2
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate #define	PCIHP_DEVICES_STR		"/devices"
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  * control structure for tree walk during configure/unconfigure operation.
1717c478bd9Sstevel@tonic-gate  */
1727c478bd9Sstevel@tonic-gate struct pcihp_config_ctrl {
1737c478bd9Sstevel@tonic-gate 	int	pci_dev;	/* PCI device number for the slot */
1747c478bd9Sstevel@tonic-gate 	uint_t	flags;		/* control flags (see below) */
1757c478bd9Sstevel@tonic-gate 	int	op;		/* operation: PCIHP_ONLINE or PCIHP_OFFLINE */
1767c478bd9Sstevel@tonic-gate 	int	rv;		/* return error code */
1777c478bd9Sstevel@tonic-gate 	dev_info_t *dip;	/* dip at which the (first) error occurred */
1787c478bd9Sstevel@tonic-gate 	hpc_occupant_info_t *occupant;
1797c478bd9Sstevel@tonic-gate };
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * control flags for configure/unconfigure operations on the tree.
1837c478bd9Sstevel@tonic-gate  *
1847c478bd9Sstevel@tonic-gate  * PCIHP_CFG_CONTINUE	continue the operation ignoring errors
1857c478bd9Sstevel@tonic-gate  */
1867c478bd9Sstevel@tonic-gate #define	PCIHP_CFG_CONTINUE	0x1
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate #define	PCIHP_ONLINE	1
1897c478bd9Sstevel@tonic-gate #define	PCIHP_OFFLINE	0
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate /* Leaf ops (hotplug controls for target devices) */
1937c478bd9Sstevel@tonic-gate static int pcihp_open(dev_t *, int, int, cred_t *);
1947c478bd9Sstevel@tonic-gate static int pcihp_close(dev_t, int, int, cred_t *);
1957c478bd9Sstevel@tonic-gate static int pcihp_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate #ifdef DEBUG
1987c478bd9Sstevel@tonic-gate static int pcihp_debug = 0;
1997c478bd9Sstevel@tonic-gate #define	PCIHP_DEBUG(args)	if (pcihp_debug >= 1) cmn_err args
2007c478bd9Sstevel@tonic-gate #define	PCIHP_DEBUG2(args)	if (pcihp_debug >= 2) cmn_err args
2017c478bd9Sstevel@tonic-gate #else
2027c478bd9Sstevel@tonic-gate #define	PCIHP_DEBUG(args)
2037c478bd9Sstevel@tonic-gate #define	PCIHP_DEBUG2(args)
2047c478bd9Sstevel@tonic-gate #endif
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * We process ENUM# event one device at a time ie. as soon as we detect
2087c478bd9Sstevel@tonic-gate  * that a device has the right ENUM# conditions, we return. If the following
2097c478bd9Sstevel@tonic-gate  * variable is set to non-zero, we scan all the devices on the bus
2107c478bd9Sstevel@tonic-gate  * for ENUM# conditions.
2117c478bd9Sstevel@tonic-gate  */
2127c478bd9Sstevel@tonic-gate static int pcihp_enum_scan_all = 0;
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate  * If HSC driver cannot determine the board type (for example: it may not
2157c478bd9Sstevel@tonic-gate  * be possible to differentiate between a Basic Hotswap, Non Hotswap or
2167c478bd9Sstevel@tonic-gate  * Non-friendly Full hotswap board), the default board type is assigned
2177c478bd9Sstevel@tonic-gate  * to be as defined by the following variable.
2187c478bd9Sstevel@tonic-gate  */
2197c478bd9Sstevel@tonic-gate static int pcihp_cpci_board_type = HPC_BOARD_CPCI_NON_HS;
2207c478bd9Sstevel@tonic-gate static int pcihp_cpci_led_blink = 30;
2217c478bd9Sstevel@tonic-gate /*
2227c478bd9Sstevel@tonic-gate  * It was noted that the blue LED when written on/off would cause INS/EXT
2237c478bd9Sstevel@tonic-gate  * bit to be set causing an extra interrupt. Although the cPCI specifications
2247c478bd9Sstevel@tonic-gate  * does not imply this, this behavior is seen with some FHS silicons.
2257c478bd9Sstevel@tonic-gate  * Also, handling the INS/EXT bit would control the LED being On/Off.
2267c478bd9Sstevel@tonic-gate  * Until the behavior is confirmed, this flag could be used to enable or
2277c478bd9Sstevel@tonic-gate  * disable handling the LED.
2287c478bd9Sstevel@tonic-gate  * 0 means the silicons handles the LED behavior via the INS/EXT bit.
2297c478bd9Sstevel@tonic-gate  * 1 means the software must explicitly do the LED behavior.
2307c478bd9Sstevel@tonic-gate  */
2317c478bd9Sstevel@tonic-gate static int pcihp_cpci_blue_led = 1;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate /* static functions */
2347c478bd9Sstevel@tonic-gate static pcihp_t *pcihp_create_soft_state(dev_info_t *dip);
2357c478bd9Sstevel@tonic-gate static void pcihp_destroy_soft_state(dev_info_t *dip);
2367c478bd9Sstevel@tonic-gate static pcihp_t *pcihp_get_soft_state(dev_info_t *dip, int cmd, int *rv);
2377c478bd9Sstevel@tonic-gate static int pcihp_configure_ap(pcihp_t *pcihp_p, int pci_dev);
2387c478bd9Sstevel@tonic-gate static int pcihp_unconfigure_ap(pcihp_t *pcihp_p, int pci_dev);
2397c478bd9Sstevel@tonic-gate static int pcihp_new_slot_state(dev_info_t *, hpc_slot_t,
2407c478bd9Sstevel@tonic-gate 	hpc_slot_info_t *, int);
2417c478bd9Sstevel@tonic-gate static int pcihp_configure(dev_info_t *, void *);
24232d69156Sscarter static bool_t pcihp_check_status(dev_info_t *);
2437c478bd9Sstevel@tonic-gate static int pcihp_event_handler(caddr_t, uint_t);
2447c478bd9Sstevel@tonic-gate static dev_info_t *pcihp_devi_find(dev_info_t *dip, uint_t dev, uint_t func);
2457c478bd9Sstevel@tonic-gate static int pcihp_match_dev(dev_info_t *dip, void *hdl);
2467c478bd9Sstevel@tonic-gate static int pcihp_get_hs_csr(struct pcihp_slotinfo *, ddi_acc_handle_t,
2477c478bd9Sstevel@tonic-gate 	uint8_t *);
2487c478bd9Sstevel@tonic-gate static void pcihp_set_hs_csr(struct pcihp_slotinfo *, ddi_acc_handle_t,
2497c478bd9Sstevel@tonic-gate 	uint8_t *);
2507c478bd9Sstevel@tonic-gate static int pcihp_get_hs_csr_location(ddi_acc_handle_t);
2517c478bd9Sstevel@tonic-gate static int pcihp_handle_enum(pcihp_t *, int, int, int);
2527c478bd9Sstevel@tonic-gate static void pcihp_hs_csr_op(pcihp_t *, int, int);
2537c478bd9Sstevel@tonic-gate static int pcihp_enum_slot(pcihp_t *, struct pcihp_slotinfo *, int, int, int);
2547c478bd9Sstevel@tonic-gate static int pcihp_handle_enum_extraction(pcihp_t *, int, int, int);
2557c478bd9Sstevel@tonic-gate static int pcihp_handle_enum_insertion(pcihp_t *, int, int, int);
2567c478bd9Sstevel@tonic-gate static int pcihp_add_dummy_reg_property(dev_info_t *, uint_t, uint_t, uint_t);
2577c478bd9Sstevel@tonic-gate static int pcihp_config_setup(dev_info_t **, ddi_acc_handle_t *,
2587c478bd9Sstevel@tonic-gate 			dev_info_t **, int, pcihp_t *);
2597c478bd9Sstevel@tonic-gate static void pcihp_config_teardown(ddi_acc_handle_t *,
2607c478bd9Sstevel@tonic-gate 			dev_info_t **, int, pcihp_t *);
2617c478bd9Sstevel@tonic-gate static int pcihp_get_board_type(struct pcihp_slotinfo *);
2627c478bd9Sstevel@tonic-gate /* sysevent function */
2637c478bd9Sstevel@tonic-gate static void pcihp_gen_sysevent(char *, int, int, dev_info_t *, int);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate static int pcihp_list_occupants(dev_info_t *, void *);
2667c478bd9Sstevel@tonic-gate static int pcihp_indirect_map(dev_info_t *dip);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate #if 0
2697c478bd9Sstevel@tonic-gate static void pcihp_probe_slot_state(dev_info_t *, int, hpc_slot_state_t *);
2707c478bd9Sstevel@tonic-gate #endif
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate int pcihp_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
2737c478bd9Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate struct cb_ops pcihp_cb_ops = {
2767c478bd9Sstevel@tonic-gate 	pcihp_open,			/* open */
2777c478bd9Sstevel@tonic-gate 	pcihp_close,			/* close */
2787c478bd9Sstevel@tonic-gate 	nodev,				/* strategy */
2797c478bd9Sstevel@tonic-gate 	nodev,				/* print */
2807c478bd9Sstevel@tonic-gate 	nodev,				/* dump */
2817c478bd9Sstevel@tonic-gate 	nodev,				/* read */
2827c478bd9Sstevel@tonic-gate 	nodev,				/* write */
2837c478bd9Sstevel@tonic-gate 	pcihp_ioctl,			/* ioctl */
2847c478bd9Sstevel@tonic-gate 	nodev,				/* devmap */
2857c478bd9Sstevel@tonic-gate 	nodev,				/* mmap */
2867c478bd9Sstevel@tonic-gate 	nodev,				/* segmap */
2877c478bd9Sstevel@tonic-gate 	nochpoll,			/* poll */
2887c478bd9Sstevel@tonic-gate 	pcihp_prop_op,			/* cb_prop_op */
2897c478bd9Sstevel@tonic-gate 	NULL,				/* streamtab */
2907c478bd9Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
2917c478bd9Sstevel@tonic-gate 	CB_REV,				/* rev */
2927c478bd9Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
2937c478bd9Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
2947c478bd9Sstevel@tonic-gate };
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate  * local data
2987c478bd9Sstevel@tonic-gate  */
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate int pcihp_autocfg_enabled = 1; /* auto config is enabled by default */
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate static kmutex_t pcihp_mutex; /* mutex to protect the following data */
3037c478bd9Sstevel@tonic-gate static pcihp_t *pcihp_head = NULL;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate static kmutex_t pcihp_open_mutex; /* mutex to protect open/close/uninit */
3067c478bd9Sstevel@tonic-gate static int	pci_devlink_flags = 0;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /*
3097c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
3107c478bd9Sstevel@tonic-gate  */
3117c478bd9Sstevel@tonic-gate extern struct mod_ops mod_miscops;
3127c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc = {
3137c478bd9Sstevel@tonic-gate 	&mod_miscops,
31499e6869fSjmcp 	"PCI nexus hotplug support",
3157c478bd9Sstevel@tonic-gate };
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
3187c478bd9Sstevel@tonic-gate 	MODREV_1,
3197c478bd9Sstevel@tonic-gate 	&modlmisc,
3207c478bd9Sstevel@tonic-gate 	NULL
3217c478bd9Sstevel@tonic-gate };
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate int
_init(void)3247c478bd9Sstevel@tonic-gate _init(void)
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate 	int error;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	mutex_init(&pcihp_mutex, NULL, MUTEX_DRIVER, NULL);
3297c478bd9Sstevel@tonic-gate 	mutex_init(&pcihp_open_mutex, NULL, MUTEX_DRIVER, NULL);
3307c478bd9Sstevel@tonic-gate 	if ((error = mod_install(&modlinkage)) != 0) {
3317c478bd9Sstevel@tonic-gate 		mutex_destroy(&pcihp_open_mutex);
3327c478bd9Sstevel@tonic-gate 		mutex_destroy(&pcihp_mutex);
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	return (error);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate int
_fini(void)3397c478bd9Sstevel@tonic-gate _fini(void)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate 	return (EBUSY);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)3457c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
3467c478bd9Sstevel@tonic-gate {
3477c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate static	pcihp_t *
pcihp_create_soft_state(dev_info_t * dip)3517c478bd9Sstevel@tonic-gate pcihp_create_soft_state(
3527c478bd9Sstevel@tonic-gate 	dev_info_t *dip)
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate 	pcihp_t	*pcihp_p;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	pcihp_p = kmem_zalloc(sizeof (struct pcihp), KM_SLEEP);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	pcihp_p->dip = dip;
3597c478bd9Sstevel@tonic-gate 	mutex_init(&pcihp_p->mutex, NULL, MUTEX_DRIVER, NULL);
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	mutex_enter(&pcihp_mutex);
3627c478bd9Sstevel@tonic-gate 	pcihp_p->nextp = pcihp_head;
3637c478bd9Sstevel@tonic-gate 	pcihp_head = pcihp_p;
3647c478bd9Sstevel@tonic-gate 	pcihp_p->bus_state = PCIHP_BUS_INITIALIZING;
3657c478bd9Sstevel@tonic-gate 	pcihp_p->slots_active = 0;
3667c478bd9Sstevel@tonic-gate 	mutex_exit(&pcihp_mutex);
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	return (pcihp_p);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate static	void
pcihp_destroy_soft_state(dev_info_t * dip)3727c478bd9Sstevel@tonic-gate pcihp_destroy_soft_state(
3737c478bd9Sstevel@tonic-gate 	dev_info_t *dip)
3747c478bd9Sstevel@tonic-gate {
3757c478bd9Sstevel@tonic-gate 	pcihp_t	*p;
3767c478bd9Sstevel@tonic-gate 	pcihp_t	**pp;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	mutex_enter(&pcihp_mutex);
3797c478bd9Sstevel@tonic-gate 	pp = &pcihp_head;
3807c478bd9Sstevel@tonic-gate 	while ((p = *pp) != NULL) {
3817c478bd9Sstevel@tonic-gate 		if (p->dip == dip) {
3827c478bd9Sstevel@tonic-gate 			*pp = p->nextp;
3837c478bd9Sstevel@tonic-gate 			kmem_free(p, sizeof (struct pcihp));
3847c478bd9Sstevel@tonic-gate 			break;
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 		pp = &(p->nextp);
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 	mutex_exit(&pcihp_mutex);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate /*
3927c478bd9Sstevel@tonic-gate  * This function should be imported by client nexus drivers as their
3937c478bd9Sstevel@tonic-gate  * devo_getinfo() entry point.
3947c478bd9Sstevel@tonic-gate  */
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate /* ARGSUSED */
3977c478bd9Sstevel@tonic-gate int
pcihp_info(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** result)3987c478bd9Sstevel@tonic-gate pcihp_info(
3997c478bd9Sstevel@tonic-gate 	dev_info_t	*dip,
4007c478bd9Sstevel@tonic-gate 	ddi_info_cmd_t	cmd,
4017c478bd9Sstevel@tonic-gate 	void		*arg,
4027c478bd9Sstevel@tonic-gate 	void		**result)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	pcihp_t		*pcihp_p;
4057c478bd9Sstevel@tonic-gate 	major_t		major;
4067c478bd9Sstevel@tonic-gate 	minor_t		minor;
4077c478bd9Sstevel@tonic-gate 	int		instance;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	major = getmajor((dev_t)arg);
4107c478bd9Sstevel@tonic-gate 	minor = getminor((dev_t)arg);
4117c478bd9Sstevel@tonic-gate 	instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	switch (cmd) {
4147c478bd9Sstevel@tonic-gate 	default:
4157c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
4187c478bd9Sstevel@tonic-gate 		*result = (void *)(intptr_t)instance;
4197c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
4227c478bd9Sstevel@tonic-gate 		mutex_enter(&pcihp_mutex);
4237c478bd9Sstevel@tonic-gate 		pcihp_p = pcihp_head;
4247c478bd9Sstevel@tonic-gate 		while (pcihp_p != NULL) {
4255c066ec2SJerry Gilliam 			if (ddi_driver_major(pcihp_p->dip) ==
4267c478bd9Sstevel@tonic-gate 			    major && ddi_get_instance(pcihp_p->dip) ==
4277c478bd9Sstevel@tonic-gate 			    instance) {
4287c478bd9Sstevel@tonic-gate 				*result = (void *)pcihp_p->dip;
4297c478bd9Sstevel@tonic-gate 				mutex_exit(&pcihp_mutex);
4307c478bd9Sstevel@tonic-gate 				return (DDI_SUCCESS);
4317c478bd9Sstevel@tonic-gate 			}
4327c478bd9Sstevel@tonic-gate 			pcihp_p = pcihp_p->nextp;
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 		mutex_exit(&pcihp_mutex);
4357c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate /*
4407c478bd9Sstevel@tonic-gate  * This function retrieves the hot plug soft state and performs the
4417c478bd9Sstevel@tonic-gate  * following primitive commands while the soft state is locked:
4427c478bd9Sstevel@tonic-gate  * mark the bus unconfigured, increment slot activity, decrement
4437c478bd9Sstevel@tonic-gate  * slot activity and noop.
4447c478bd9Sstevel@tonic-gate  */
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate /* ARGSUSED */
4477c478bd9Sstevel@tonic-gate static	pcihp_t *
pcihp_get_soft_state(dev_info_t * dip,int cmd,int * rv)4487c478bd9Sstevel@tonic-gate pcihp_get_soft_state(
4497c478bd9Sstevel@tonic-gate 	dev_info_t	*dip, int cmd, int *rv)
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate 	pcihp_t		*pcihp_p;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	*rv = PCIHP_SUCCESS;
4547c478bd9Sstevel@tonic-gate 	mutex_enter(&pcihp_mutex);
4557c478bd9Sstevel@tonic-gate 	pcihp_p = pcihp_head;
4567c478bd9Sstevel@tonic-gate 	while (pcihp_p != NULL) {
4577c478bd9Sstevel@tonic-gate 		if (pcihp_p->dip == dip) {
4587c478bd9Sstevel@tonic-gate 			switch (cmd) {
4597c478bd9Sstevel@tonic-gate 			case PCIHP_DR_BUS_UNCONFIGURE:
4607c478bd9Sstevel@tonic-gate 				if (pcihp_p->slots_active == 0)
4617c478bd9Sstevel@tonic-gate 					pcihp_p->bus_state =
4627c478bd9Sstevel@tonic-gate 					    PCIHP_BUS_UNCONFIGURED;
4637c478bd9Sstevel@tonic-gate 				else
4647c478bd9Sstevel@tonic-gate 					*rv = PCIHP_FAILURE;
4657c478bd9Sstevel@tonic-gate 				break;
4667c478bd9Sstevel@tonic-gate 			case PCIHP_DR_SLOT_ENTER:
4677c478bd9Sstevel@tonic-gate 				if (pcihp_p->bus_state ==
4687c478bd9Sstevel@tonic-gate 				    PCIHP_BUS_UNCONFIGURED)
4697c478bd9Sstevel@tonic-gate 					*rv = PCIHP_FAILURE;
4707c478bd9Sstevel@tonic-gate 				else
4717c478bd9Sstevel@tonic-gate 					pcihp_p->slots_active++;
4727c478bd9Sstevel@tonic-gate 				break;
4737c478bd9Sstevel@tonic-gate 			case PCIHP_DR_SLOT_EXIT:
4747c478bd9Sstevel@tonic-gate 				ASSERT(pcihp_p->slots_active > 0);
4757c478bd9Sstevel@tonic-gate 				if (pcihp_p->slots_active == 0)
4767c478bd9Sstevel@tonic-gate 					cmn_err(CE_PANIC,
4777c478bd9Sstevel@tonic-gate 					    "pcihp (%s%d): mismatched slot"
4787c478bd9Sstevel@tonic-gate 					    " activity",
4797c478bd9Sstevel@tonic-gate 					    ddi_driver_name(dip),
4807c478bd9Sstevel@tonic-gate 					    ddi_get_instance(dip));
4817c478bd9Sstevel@tonic-gate 				else
4827c478bd9Sstevel@tonic-gate 					pcihp_p->slots_active--;
4837c478bd9Sstevel@tonic-gate 				break;
4847c478bd9Sstevel@tonic-gate 			case PCIHP_DR_NOOP:
4857c478bd9Sstevel@tonic-gate 				break;
4867c478bd9Sstevel@tonic-gate 			default:
4877c478bd9Sstevel@tonic-gate 				*rv = PCIHP_FAILURE;
4887c478bd9Sstevel@tonic-gate 				break;
4897c478bd9Sstevel@tonic-gate 			}
4907c478bd9Sstevel@tonic-gate 			mutex_exit(&pcihp_mutex);
4917c478bd9Sstevel@tonic-gate 			return (pcihp_p);
4927c478bd9Sstevel@tonic-gate 		}
4937c478bd9Sstevel@tonic-gate 		pcihp_p = pcihp_p->nextp;
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 	mutex_exit(&pcihp_mutex);
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	return (NULL);
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate /* ARGSUSED3 */
5017c478bd9Sstevel@tonic-gate static int
pcihp_open(dev_t * devp,int flags,int otyp,cred_t * credp)5027c478bd9Sstevel@tonic-gate pcihp_open(dev_t *devp, int flags, int otyp, cred_t *credp)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate 	dev_info_t *self;
5057c478bd9Sstevel@tonic-gate 	pcihp_t *pcihp_p;
5067c478bd9Sstevel@tonic-gate 	minor_t	minor;
5077c478bd9Sstevel@tonic-gate 	int pci_dev;
5087c478bd9Sstevel@tonic-gate 	int rv;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	/*
5117c478bd9Sstevel@tonic-gate 	 * Make sure the open is for the right file type.
5127c478bd9Sstevel@tonic-gate 	 */
5137c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
5147c478bd9Sstevel@tonic-gate 		return (EINVAL);
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	mutex_enter(&pcihp_open_mutex);
5177c478bd9Sstevel@tonic-gate 	/*
5187c478bd9Sstevel@tonic-gate 	 * Get the soft state structure.
5197c478bd9Sstevel@tonic-gate 	 */
5207c478bd9Sstevel@tonic-gate 	if (pcihp_info(NULL, DDI_INFO_DEVT2DEVINFO, (void *)*devp,
5217c478bd9Sstevel@tonic-gate 	    (void **)&self) != DDI_SUCCESS) {
5227c478bd9Sstevel@tonic-gate 		mutex_exit(&pcihp_open_mutex);
5237c478bd9Sstevel@tonic-gate 		return (ENXIO);
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	pcihp_p = pcihp_get_soft_state(self, PCIHP_DR_NOOP, &rv);
5277c478bd9Sstevel@tonic-gate 	ASSERT(pcihp_p != NULL);
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	mutex_enter(&pcihp_p->mutex);
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	/*
5327c478bd9Sstevel@tonic-gate 	 * If the pci_dev is valid then the minor device is an
5337c478bd9Sstevel@tonic-gate 	 * AP. Otherwise it is ":devctl" minor device.
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 	minor = getminor(*devp);
5367c478bd9Sstevel@tonic-gate 	pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(minor);
5377c478bd9Sstevel@tonic-gate 	if (pci_dev < PCI_MAX_DEVS) {
5387c478bd9Sstevel@tonic-gate 		struct pcihp_slotinfo *slotinfop;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 		slotinfop = &pcihp_p->slotinfo[pci_dev];
5417c478bd9Sstevel@tonic-gate 		if (slotinfop->slot_hdl == NULL) {
5427c478bd9Sstevel@tonic-gate 			mutex_exit(&pcihp_p->mutex);
5437c478bd9Sstevel@tonic-gate 			mutex_exit(&pcihp_open_mutex);
5447c478bd9Sstevel@tonic-gate 			return (ENXIO);
5457c478bd9Sstevel@tonic-gate 		}
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	/*
5497c478bd9Sstevel@tonic-gate 	 * Handle the open by tracking the device state.
5507c478bd9Sstevel@tonic-gate 	 *
5517c478bd9Sstevel@tonic-gate 	 * Note: Needs review w.r.t exclusive access to AP or the bus.
5527c478bd9Sstevel@tonic-gate 	 * Currently in the pci plug-in we don't use EXCL open at all
5537c478bd9Sstevel@tonic-gate 	 * so the code below implements EXCL access on the bus.
5547c478bd9Sstevel@tonic-gate 	 */
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	/* enforce exclusive access to the bus */
5577c478bd9Sstevel@tonic-gate 	if ((pcihp_p->soft_state == PCIHP_SOFT_STATE_OPEN_EXCL) ||
5587c478bd9Sstevel@tonic-gate 	    ((flags & FEXCL) &&
5597c478bd9Sstevel@tonic-gate 	    (pcihp_p->soft_state != PCIHP_SOFT_STATE_CLOSED))) {
5607c478bd9Sstevel@tonic-gate 		mutex_exit(&pcihp_p->mutex);
5617c478bd9Sstevel@tonic-gate 		mutex_exit(&pcihp_open_mutex);
5627c478bd9Sstevel@tonic-gate 		return (EBUSY);
5637c478bd9Sstevel@tonic-gate 	}
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	if (flags & FEXCL)
5667c478bd9Sstevel@tonic-gate 		pcihp_p->soft_state = PCIHP_SOFT_STATE_OPEN_EXCL;
5677c478bd9Sstevel@tonic-gate 	else
5687c478bd9Sstevel@tonic-gate 		pcihp_p->soft_state = PCIHP_SOFT_STATE_OPEN;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	mutex_exit(&pcihp_p->mutex);
5717c478bd9Sstevel@tonic-gate 	mutex_exit(&pcihp_open_mutex);
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	return (0);
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate /* ARGSUSED */
5777c478bd9Sstevel@tonic-gate static int
pcihp_close(dev_t dev,int flags,int otyp,cred_t * credp)5787c478bd9Sstevel@tonic-gate pcihp_close(dev_t dev, int flags, int otyp, cred_t *credp)
5797c478bd9Sstevel@tonic-gate {
5807c478bd9Sstevel@tonic-gate 	dev_info_t *self;
5817c478bd9Sstevel@tonic-gate 	pcihp_t *pcihp_p;
5827c478bd9Sstevel@tonic-gate 	int rv;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
5857c478bd9Sstevel@tonic-gate 		return (EINVAL);
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	mutex_enter(&pcihp_open_mutex);
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	if (pcihp_info(NULL, DDI_INFO_DEVT2DEVINFO, (void *)dev,
5907c478bd9Sstevel@tonic-gate 	    (void **)&self) != DDI_SUCCESS) {
5917c478bd9Sstevel@tonic-gate 		mutex_exit(&pcihp_open_mutex);
5927c478bd9Sstevel@tonic-gate 		return (ENXIO);
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	pcihp_p = pcihp_get_soft_state(self, PCIHP_DR_NOOP, &rv);
5967c478bd9Sstevel@tonic-gate 	ASSERT(pcihp_p != NULL);
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	mutex_enter(&pcihp_p->mutex);
5997c478bd9Sstevel@tonic-gate 	pcihp_p->soft_state = PCIHP_SOFT_STATE_CLOSED;
6007c478bd9Sstevel@tonic-gate 	mutex_exit(&pcihp_p->mutex);
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	mutex_exit(&pcihp_open_mutex);
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	return (0);
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate static int
pcihp_list_occupants(dev_info_t * dip,void * hdl)6087c478bd9Sstevel@tonic-gate pcihp_list_occupants(dev_info_t *dip, void *hdl)
6097c478bd9Sstevel@tonic-gate {
6107c478bd9Sstevel@tonic-gate 	int pci_dev;
6117c478bd9Sstevel@tonic-gate 	struct pcihp_config_ctrl *ctrl = (struct pcihp_config_ctrl *)hdl;
6127c478bd9Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
6137c478bd9Sstevel@tonic-gate 	int length;
6147c478bd9Sstevel@tonic-gate 	major_t major;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	/*
6177c478bd9Sstevel@tonic-gate 	 * Get the PCI device number information from the devinfo
6187c478bd9Sstevel@tonic-gate 	 * node. Since the node may not have the address field
6197c478bd9Sstevel@tonic-gate 	 * setup (this is done in the DDI_INITCHILD of the parent)
6207c478bd9Sstevel@tonic-gate 	 * we look up the 'reg' property to decode that information.
6217c478bd9Sstevel@tonic-gate 	 */
6227c478bd9Sstevel@tonic-gate 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
6237c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "reg", (int **)&pci_rp,
6247c478bd9Sstevel@tonic-gate 	    (uint_t *)&length) != DDI_PROP_SUCCESS) {
6257c478bd9Sstevel@tonic-gate 		ctrl->rv = DDI_FAILURE;
6267c478bd9Sstevel@tonic-gate 		ctrl->dip = dip;
6277c478bd9Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	/* get the pci device id information */
6317c478bd9Sstevel@tonic-gate 	pci_dev = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	/*
6347c478bd9Sstevel@tonic-gate 	 * free the memory allocated by ddi_prop_lookup_int_array
6357c478bd9Sstevel@tonic-gate 	 */
6367c478bd9Sstevel@tonic-gate 	ddi_prop_free(pci_rp);
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	/*
6397c478bd9Sstevel@tonic-gate 	 * Match the node for the device number of the slot.
6407c478bd9Sstevel@tonic-gate 	 */
6417c478bd9Sstevel@tonic-gate 	if (pci_dev == ctrl->pci_dev) { /* node is a match */
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 		major = ddi_driver_major(dip);
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		/*
6467c478bd9Sstevel@tonic-gate 		 * If the node is not yet attached, then don't list it
6477c478bd9Sstevel@tonic-gate 		 * as an occupant. This is valid, since nothing can be
6487c478bd9Sstevel@tonic-gate 		 * consuming it until it is attached, and cfgadm will
6497c478bd9Sstevel@tonic-gate 		 * ask for the property explicitly which will cause it
6507c478bd9Sstevel@tonic-gate 		 * to be re-freshed right before checking with rcm.
6517c478bd9Sstevel@tonic-gate 		 */
652737d277aScth 		if ((major == -1) || !i_ddi_devi_attached(dip))
6537c478bd9Sstevel@tonic-gate 			return (DDI_WALK_PRUNECHILD);
6547c478bd9Sstevel@tonic-gate 
65510302a14Scth 		/*
65610302a14Scth 		 * If we have used all our occupants then print mesage
65710302a14Scth 		 * and terminate walk.
65810302a14Scth 		 */
65910302a14Scth 		if (ctrl->occupant->i >= HPC_MAX_OCCUPANTS) {
66010302a14Scth 			cmn_err(CE_WARN,
66110302a14Scth 			    "pcihp (%s%d): unable to list all occupants",
66210302a14Scth 			    ddi_driver_name(ddi_get_parent(dip)),
66310302a14Scth 			    ddi_get_instance(ddi_get_parent(dip)));
66410302a14Scth 			return (DDI_WALK_TERMINATE);
66510302a14Scth 		}
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 		/*
6687c478bd9Sstevel@tonic-gate 		 * No need to hold the dip as ddi_walk_devs
6697c478bd9Sstevel@tonic-gate 		 * has already arranged that for us.
6707c478bd9Sstevel@tonic-gate 		 */
67110302a14Scth 		ctrl->occupant->id[ctrl->occupant->i] =
67210302a14Scth 		    kmem_alloc(sizeof (char[MAXPATHLEN]), KM_SLEEP);
6737c478bd9Sstevel@tonic-gate 		(void) ddi_pathname(dip,
6747c478bd9Sstevel@tonic-gate 		    (char *)ctrl->occupant->id[ctrl->occupant->i]);
6757c478bd9Sstevel@tonic-gate 		ctrl->occupant->i++;
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	/*
6797c478bd9Sstevel@tonic-gate 	 * continue the walk to the next sibling to look for a match
6807c478bd9Sstevel@tonic-gate 	 * or to find other nodes if this card is a multi-function card.
6817c478bd9Sstevel@tonic-gate 	 */
6827c478bd9Sstevel@tonic-gate 	return (DDI_WALK_PRUNECHILD);
6837c478bd9Sstevel@tonic-gate }
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate static void
pcihp_create_occupant_props_nolock(dev_info_t * self,dev_t dev,int pci_dev)6867c478bd9Sstevel@tonic-gate pcihp_create_occupant_props_nolock(dev_info_t *self, dev_t dev, int pci_dev)
6877c478bd9Sstevel@tonic-gate {
6887c478bd9Sstevel@tonic-gate 	struct pcihp_config_ctrl ctrl;
6897c478bd9Sstevel@tonic-gate 	hpc_occupant_info_t *occupant;
6907c478bd9Sstevel@tonic-gate 	int i;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	occupant = kmem_alloc(sizeof (hpc_occupant_info_t), KM_SLEEP);
6937c478bd9Sstevel@tonic-gate 	occupant->i = 0;
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	ctrl.flags = 0;
6967c478bd9Sstevel@tonic-gate 	ctrl.dip = NULL;
6977c478bd9Sstevel@tonic-gate 	ctrl.rv = NDI_SUCCESS;
6987c478bd9Sstevel@tonic-gate 	ctrl.pci_dev = pci_dev;
6997c478bd9Sstevel@tonic-gate 	ctrl.op = 55; /* should define DRYRUN */
7007c478bd9Sstevel@tonic-gate 	ctrl.occupant = occupant;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	ddi_walk_devs(ddi_get_child(self), pcihp_list_occupants,
7037c478bd9Sstevel@tonic-gate 	    (void *)&ctrl);
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	if (occupant->i == 0) {
7067c478bd9Sstevel@tonic-gate 		/* no occupants right now, need to create stub property */
7077c478bd9Sstevel@tonic-gate 		char *c[] = { "" };
7087c478bd9Sstevel@tonic-gate 		(void) ddi_prop_update_string_array(dev, self, "pci-occupant",
7097c478bd9Sstevel@tonic-gate 		    c, 1);
7107c478bd9Sstevel@tonic-gate 	} else {
7117c478bd9Sstevel@tonic-gate 		(void) ddi_prop_update_string_array(dev, self, "pci-occupant",
7127c478bd9Sstevel@tonic-gate 		    occupant->id, occupant->i);
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 	for (i = 0; i < occupant->i; i++) {
7157c478bd9Sstevel@tonic-gate 		kmem_free(occupant->id[i], sizeof (char[MAXPATHLEN]));
7167c478bd9Sstevel@tonic-gate 	}
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	kmem_free(occupant, sizeof (hpc_occupant_info_t));
7197c478bd9Sstevel@tonic-gate }
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate static void
pcihp_create_occupant_props(dev_info_t * self,dev_t dev,int pci_dev)7227c478bd9Sstevel@tonic-gate pcihp_create_occupant_props(dev_info_t *self, dev_t dev, int pci_dev)
7237c478bd9Sstevel@tonic-gate {
7243fe80ca4SDan Cross 	ndi_devi_enter(self);
7257c478bd9Sstevel@tonic-gate 	pcihp_create_occupant_props_nolock(self, dev, pci_dev);
7263fe80ca4SDan Cross 	ndi_devi_exit(self);
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate static void
pcihp_delete_occupant_props(dev_info_t * dip,dev_t dev)7307c478bd9Sstevel@tonic-gate pcihp_delete_occupant_props(dev_info_t *dip, dev_t dev)
7317c478bd9Sstevel@tonic-gate {
7327c478bd9Sstevel@tonic-gate 	if (ddi_prop_remove(dev, dip, "pci-occupant")
7337c478bd9Sstevel@tonic-gate 	    != DDI_PROP_SUCCESS)
7347c478bd9Sstevel@tonic-gate 		return; /* add error handling */
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate }
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate /*
7397c478bd9Sstevel@tonic-gate  * pcihp_ioctl: devctl hotplug controls
7407c478bd9Sstevel@tonic-gate  */
7417c478bd9Sstevel@tonic-gate /* ARGSUSED */
7427c478bd9Sstevel@tonic-gate static int
pcihp_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)7437c478bd9Sstevel@tonic-gate pcihp_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
744*d5ebc493SDan Cross     int *rvalp)
7457c478bd9Sstevel@tonic-gate {
7467c478bd9Sstevel@tonic-gate 	pcihp_t *pcihp_p;
7477c478bd9Sstevel@tonic-gate 	dev_info_t *self;
7487c478bd9Sstevel@tonic-gate 	struct devctl_iocdata *dcp;
7497c478bd9Sstevel@tonic-gate 	uint_t bus_state;
7507c478bd9Sstevel@tonic-gate 	int rv = 0;
7517c478bd9Sstevel@tonic-gate 	int pci_dev;
7527c478bd9Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
7537c478bd9Sstevel@tonic-gate 	hpc_slot_state_t rstate;
7547c478bd9Sstevel@tonic-gate 	devctl_ap_state_t ap_state;
7557c478bd9Sstevel@tonic-gate 	struct hpc_control_data hpc_ctrldata;
7567c478bd9Sstevel@tonic-gate 	struct hpc_led_info led_info;
7577c478bd9Sstevel@tonic-gate 	time_t time;
7587c478bd9Sstevel@tonic-gate 	int state_locking;
7597c478bd9Sstevel@tonic-gate 	int state_unlocking;
7607c478bd9Sstevel@tonic-gate 	int rval;
7617c478bd9Sstevel@tonic-gate 	char *pathname = NULL;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	/*
7647c478bd9Sstevel@tonic-gate 	 * read devctl ioctl data before soft state retrieval
7657c478bd9Sstevel@tonic-gate 	 */
7667c478bd9Sstevel@tonic-gate 	if ((cmd != DEVCTL_AP_CONTROL) &&
7677c478bd9Sstevel@tonic-gate 	    ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
7687c478bd9Sstevel@tonic-gate 		return (EFAULT);
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	if (pcihp_info(NULL, DDI_INFO_DEVT2DEVINFO, (void *)dev,
7717c478bd9Sstevel@tonic-gate 	    (void **)&self) != DDI_SUCCESS) {
7727c478bd9Sstevel@tonic-gate 		if (cmd != DEVCTL_AP_CONTROL)
7737c478bd9Sstevel@tonic-gate 			ndi_dc_freehdl(dcp);
7747c478bd9Sstevel@tonic-gate 		return (ENXIO);
7757c478bd9Sstevel@tonic-gate 	}
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	switch (cmd) {
7787c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_INSERT:
7797c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_REMOVE:
7807c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONNECT:
7817c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_DISCONNECT:
7827c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONFIGURE:
7837c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_UNCONFIGURE:
7847c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_GETSTATE:
7857c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONTROL:
7867c478bd9Sstevel@tonic-gate 		state_locking = PCIHP_DR_SLOT_ENTER;
7877c478bd9Sstevel@tonic-gate 		state_unlocking = PCIHP_DR_SLOT_EXIT;
7887c478bd9Sstevel@tonic-gate 		break;
7897c478bd9Sstevel@tonic-gate 	default:
7907c478bd9Sstevel@tonic-gate 		state_locking = PCIHP_DR_NOOP;
7917c478bd9Sstevel@tonic-gate 		state_unlocking = PCIHP_DR_NOOP;
7927c478bd9Sstevel@tonic-gate 		break;
7937c478bd9Sstevel@tonic-gate 	}
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	pcihp_p = pcihp_get_soft_state(self, state_locking, &rval);
7967c478bd9Sstevel@tonic-gate 	ASSERT(pcihp_p != NULL);
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	if (rval == PCIHP_FAILURE) {
7997c478bd9Sstevel@tonic-gate 		(void) ddi_pathname(pcihp_p->dip, pathname);
8007c478bd9Sstevel@tonic-gate 		PCIHP_DEBUG((CE_WARN, "Hot Plug bus %s instance is unconfigured"
8017c478bd9Sstevel@tonic-gate 		    " while slot activity is requested\n", pathname));
8027c478bd9Sstevel@tonic-gate 		if (cmd != DEVCTL_AP_CONTROL)
8037c478bd9Sstevel@tonic-gate 			ndi_dc_freehdl(dcp);
8047c478bd9Sstevel@tonic-gate 		return (EBUSY);
8057c478bd9Sstevel@tonic-gate 	}
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	/*
8087c478bd9Sstevel@tonic-gate 	 * For attachment points the lower 8 bits of the minor number is the
8097c478bd9Sstevel@tonic-gate 	 * PCI device number.
8107c478bd9Sstevel@tonic-gate 	 */
8117c478bd9Sstevel@tonic-gate 	pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(getminor(dev));
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	/*
8147c478bd9Sstevel@tonic-gate 	 * We can use the generic implementation for these ioctls
8157c478bd9Sstevel@tonic-gate 	 */
8167c478bd9Sstevel@tonic-gate 	switch (cmd) {
8177c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_GETSTATE:
8187c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_ONLINE:
8197c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_OFFLINE:
8207c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_GETSTATE:
8217c478bd9Sstevel@tonic-gate 		rv = ndi_devctl_ioctl(self, cmd, arg, mode, 0);
8227c478bd9Sstevel@tonic-gate 		ndi_dc_freehdl(dcp);
8237c478bd9Sstevel@tonic-gate 		return (rv);
8247c478bd9Sstevel@tonic-gate 	default:
8257c478bd9Sstevel@tonic-gate 		break;
8267c478bd9Sstevel@tonic-gate 	}
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	switch (cmd) {
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_RESET:
8317c478bd9Sstevel@tonic-gate 		rv = ENOTSUP;
8327c478bd9Sstevel@tonic-gate 		break;
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_QUIESCE:
8357c478bd9Sstevel@tonic-gate 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
8367c478bd9Sstevel@tonic-gate 			if (bus_state == BUS_QUIESCED)
8377c478bd9Sstevel@tonic-gate 				break;
8387c478bd9Sstevel@tonic-gate 		(void) ndi_set_bus_state(self, BUS_QUIESCED);
8397c478bd9Sstevel@tonic-gate 		break;
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_UNQUIESCE:
8427c478bd9Sstevel@tonic-gate 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
8437c478bd9Sstevel@tonic-gate 			if (bus_state == BUS_ACTIVE)
8447c478bd9Sstevel@tonic-gate 				break;
8457c478bd9Sstevel@tonic-gate 		(void) ndi_set_bus_state(self, BUS_ACTIVE);
8467c478bd9Sstevel@tonic-gate 		break;
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_RESET:
8497c478bd9Sstevel@tonic-gate 		rv = ENOTSUP;
8507c478bd9Sstevel@tonic-gate 		break;
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_RESETALL:
8537c478bd9Sstevel@tonic-gate 		rv = ENOTSUP;
8547c478bd9Sstevel@tonic-gate 		break;
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONNECT:
8577c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_DISCONNECT:
8587c478bd9Sstevel@tonic-gate 		/*
8597c478bd9Sstevel@tonic-gate 		 * CONNECT(DISCONNECT) the hot plug slot to(from) the bus.
8607c478bd9Sstevel@tonic-gate 		 *
8617c478bd9Sstevel@tonic-gate 		 * For cPCI slots this operation is a nop so the HPC
8627c478bd9Sstevel@tonic-gate 		 * driver may return success if it is a valid operation.
8637c478bd9Sstevel@tonic-gate 		 */
8647c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_INSERT:
8657c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_REMOVE:
8667c478bd9Sstevel@tonic-gate 		/*
8677c478bd9Sstevel@tonic-gate 		 * Prepare the slot for INSERT/REMOVE operation.
8687c478bd9Sstevel@tonic-gate 		 */
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 		/*
8717c478bd9Sstevel@tonic-gate 		 * check for valid request:
8727c478bd9Sstevel@tonic-gate 		 *	1. It is a hotplug slot.
8737c478bd9Sstevel@tonic-gate 		 *	2. The slot has no occupant that is in
8747c478bd9Sstevel@tonic-gate 		 *	   the 'configured' state.
8757c478bd9Sstevel@tonic-gate 		 */
8767c478bd9Sstevel@tonic-gate 		if (pci_dev >= PCI_MAX_DEVS) {
8777c478bd9Sstevel@tonic-gate 			rv = ENXIO;
8787c478bd9Sstevel@tonic-gate 			break;
8797c478bd9Sstevel@tonic-gate 		}
8807c478bd9Sstevel@tonic-gate 		slotinfop = &pcihp_p->slotinfo[pci_dev];
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 		mutex_enter(&slotinfop->slot_mutex);
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 		if ((slotinfop->slot_hdl == NULL) ||
8857c478bd9Sstevel@tonic-gate 		    (slotinfop->slot_flags & PCIHP_SLOT_DISABLED)) {
8867c478bd9Sstevel@tonic-gate 			rv = ENXIO;
8877c478bd9Sstevel@tonic-gate 			mutex_exit(&slotinfop->slot_mutex);
8887c478bd9Sstevel@tonic-gate 			break;
8897c478bd9Sstevel@tonic-gate 		}
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 		/* the slot occupant must be in the UNCONFIGURED state */
8927c478bd9Sstevel@tonic-gate 		if (slotinfop->ostate != AP_OSTATE_UNCONFIGURED) {
8937c478bd9Sstevel@tonic-gate 			rv = EINVAL;
8947c478bd9Sstevel@tonic-gate 			mutex_exit(&slotinfop->slot_mutex);
8957c478bd9Sstevel@tonic-gate 			break;
8967c478bd9Sstevel@tonic-gate 		}
8977c478bd9Sstevel@tonic-gate 		/*
8987c478bd9Sstevel@tonic-gate 		 * Call the HPC driver to perform the operation on the slot.
8997c478bd9Sstevel@tonic-gate 		 */
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 		switch (cmd) {
9027c478bd9Sstevel@tonic-gate 		case DEVCTL_AP_INSERT:
9037c478bd9Sstevel@tonic-gate 			rv = hpc_nexus_insert(slotinfop->slot_hdl, NULL, 0);
9047c478bd9Sstevel@tonic-gate 			break;
9057c478bd9Sstevel@tonic-gate 		case DEVCTL_AP_REMOVE:
9067c478bd9Sstevel@tonic-gate 			rv = hpc_nexus_remove(slotinfop->slot_hdl, NULL, 0);
9077c478bd9Sstevel@tonic-gate 			break;
9087c478bd9Sstevel@tonic-gate 		case DEVCTL_AP_CONNECT:
9097c478bd9Sstevel@tonic-gate 			rv = hpc_nexus_connect(slotinfop->slot_hdl, NULL, 0);
9107c478bd9Sstevel@tonic-gate 			if (rv == HPC_SUCCESS) {
9117c478bd9Sstevel@tonic-gate 				slotinfop->rstate = AP_RSTATE_CONNECTED;
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 				if (drv_getparm(TIME, (void *)&time) !=
9147c478bd9Sstevel@tonic-gate 				    DDI_SUCCESS)
9157c478bd9Sstevel@tonic-gate 					slotinfop->last_change = (time_t)-1;
9167c478bd9Sstevel@tonic-gate 				else
9177c478bd9Sstevel@tonic-gate 					slotinfop->last_change = (time32_t)time;
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 				slotinfop = &pcihp_p->slotinfo[pci_dev];
9207c478bd9Sstevel@tonic-gate 				pcihp_gen_sysevent(slotinfop->name,
92132d69156Sscarter 				    PCIHP_DR_AP_STATE_CHANGE,
92232d69156Sscarter 				    SE_NO_HINT, pcihp_p->dip,
92332d69156Sscarter 				    KM_SLEEP);
9247c478bd9Sstevel@tonic-gate 			}
9257c478bd9Sstevel@tonic-gate 			break;
9267c478bd9Sstevel@tonic-gate 		case DEVCTL_AP_DISCONNECT:
9277c478bd9Sstevel@tonic-gate 			rv = hpc_nexus_disconnect(slotinfop->slot_hdl, NULL, 0);
9287c478bd9Sstevel@tonic-gate 			if (rv == HPC_SUCCESS) {
9297c478bd9Sstevel@tonic-gate 				slotinfop->rstate = AP_RSTATE_DISCONNECTED;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 				if (drv_getparm(TIME, (void *)&time) !=
9327c478bd9Sstevel@tonic-gate 				    DDI_SUCCESS)
9337c478bd9Sstevel@tonic-gate 					slotinfop->last_change = (time_t)-1;
9347c478bd9Sstevel@tonic-gate 				else
9357c478bd9Sstevel@tonic-gate 					slotinfop->last_change = (time32_t)time;
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 				slotinfop = &pcihp_p->slotinfo[pci_dev];
9387c478bd9Sstevel@tonic-gate 				pcihp_gen_sysevent(slotinfop->name,
93932d69156Sscarter 				    PCIHP_DR_AP_STATE_CHANGE,
94032d69156Sscarter 				    SE_NO_HINT, pcihp_p->dip,
94132d69156Sscarter 				    KM_SLEEP);
9427c478bd9Sstevel@tonic-gate 			}
9437c478bd9Sstevel@tonic-gate 			break;
9447c478bd9Sstevel@tonic-gate 		}
9457c478bd9Sstevel@tonic-gate 		mutex_exit(&slotinfop->slot_mutex);
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 		switch (rv) {
9487c478bd9Sstevel@tonic-gate 		case HPC_ERR_INVALID:
9497c478bd9Sstevel@tonic-gate 			rv = ENXIO;
9507c478bd9Sstevel@tonic-gate 			break;
9517c478bd9Sstevel@tonic-gate 		case HPC_ERR_NOTSUPPORTED:
9527c478bd9Sstevel@tonic-gate 			rv = ENOTSUP;
9537c478bd9Sstevel@tonic-gate 			break;
9547c478bd9Sstevel@tonic-gate 		case HPC_ERR_FAILED:
9557c478bd9Sstevel@tonic-gate 			rv = EIO;
9567c478bd9Sstevel@tonic-gate 			break;
9577c478bd9Sstevel@tonic-gate 		}
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 		break;
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONFIGURE:
9627c478bd9Sstevel@tonic-gate 		/*
9637c478bd9Sstevel@tonic-gate 		 * **************************************
9647c478bd9Sstevel@tonic-gate 		 * CONFIGURE the occupant in the slot.
9657c478bd9Sstevel@tonic-gate 		 * **************************************
9667c478bd9Sstevel@tonic-gate 		 */
96770025d76Sjohnny 		slotinfop = &pcihp_p->slotinfo[pci_dev];
96870025d76Sjohnny 
96970025d76Sjohnny 		mutex_enter(&slotinfop->slot_mutex);
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 		rv = pcihp_configure_ap(pcihp_p, pci_dev);
9727c478bd9Sstevel@tonic-gate 		if (rv == HPC_SUCCESS) {
9737c478bd9Sstevel@tonic-gate 			pcihp_gen_sysevent(slotinfop->name,
9747c478bd9Sstevel@tonic-gate 			    PCIHP_DR_AP_STATE_CHANGE,
9757c478bd9Sstevel@tonic-gate 			    SE_NO_HINT, pcihp_p->dip, KM_SLEEP);
9767c478bd9Sstevel@tonic-gate 			pcihp_create_occupant_props(self, dev, pci_dev);
9777c478bd9Sstevel@tonic-gate 		}
97870025d76Sjohnny 		mutex_exit(&slotinfop->slot_mutex);
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 		break;
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_UNCONFIGURE:
9837c478bd9Sstevel@tonic-gate 		/*
9847c478bd9Sstevel@tonic-gate 		 * **************************************
9857c478bd9Sstevel@tonic-gate 		 * UNCONFIGURE the occupant in the slot.
9867c478bd9Sstevel@tonic-gate 		 * **************************************
9877c478bd9Sstevel@tonic-gate 		 */
98870025d76Sjohnny 		slotinfop = &pcihp_p->slotinfo[pci_dev];
98970025d76Sjohnny 
99070025d76Sjohnny 		mutex_enter(&slotinfop->slot_mutex);
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 		rv = pcihp_unconfigure_ap(pcihp_p, pci_dev);
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 		if (rv == HPC_SUCCESS) {
9957c478bd9Sstevel@tonic-gate 			pcihp_gen_sysevent(slotinfop->name,
9967c478bd9Sstevel@tonic-gate 			    PCIHP_DR_AP_STATE_CHANGE,
9977c478bd9Sstevel@tonic-gate 			    SE_NO_HINT, pcihp_p->dip, KM_SLEEP);
9987c478bd9Sstevel@tonic-gate 			pcihp_delete_occupant_props(pcihp_p->dip, dev);
9997c478bd9Sstevel@tonic-gate 		}
100070025d76Sjohnny 		mutex_exit(&slotinfop->slot_mutex);
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 		break;
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_GETSTATE:
100532d69156Sscarter 	{
10067c478bd9Sstevel@tonic-gate 		int mutex_held;
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 		/*
10097c478bd9Sstevel@tonic-gate 		 * return the state of Attachment Point.
10107c478bd9Sstevel@tonic-gate 		 *
10117c478bd9Sstevel@tonic-gate 		 * If the occupant is in UNCONFIGURED state then
10127c478bd9Sstevel@tonic-gate 		 * we should get the receptacle state from the
10137c478bd9Sstevel@tonic-gate 		 * HPC driver because the receptacle state
10147c478bd9Sstevel@tonic-gate 		 * maintained in the nexus may not be accurate.
10157c478bd9Sstevel@tonic-gate 		 */
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 		/*
10187c478bd9Sstevel@tonic-gate 		 * check for valid request:
10197c478bd9Sstevel@tonic-gate 		 *	1. It is a hotplug slot.
10207c478bd9Sstevel@tonic-gate 		 */
10217c478bd9Sstevel@tonic-gate 		slotinfop = &pcihp_p->slotinfo[pci_dev];
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 		/* try to acquire the slot mutex */
10247c478bd9Sstevel@tonic-gate 		mutex_held = mutex_tryenter(&slotinfop->slot_mutex);
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 		if (pci_dev >= PCI_MAX_DEVS || slotinfop->slot_hdl == NULL) {
10277c478bd9Sstevel@tonic-gate 			rv = ENXIO;
10287c478bd9Sstevel@tonic-gate 			if (mutex_held) {
10297c478bd9Sstevel@tonic-gate 				mutex_exit(&slotinfop->slot_mutex);
10307c478bd9Sstevel@tonic-gate 			}
10317c478bd9Sstevel@tonic-gate 			break;
10327c478bd9Sstevel@tonic-gate 		}
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 		if (slotinfop->ostate == AP_OSTATE_UNCONFIGURED) {
103532d69156Sscarter 			if (hpc_nexus_control(slotinfop->slot_hdl,
103632d69156Sscarter 			    HPC_CTRL_GET_SLOT_STATE, (caddr_t)&rstate) != 0) {
103732d69156Sscarter 				rv = EIO;
103832d69156Sscarter 				if (mutex_held)
103932d69156Sscarter 					mutex_exit(&slotinfop->slot_mutex);
104032d69156Sscarter 				break;
104132d69156Sscarter 			}
104232d69156Sscarter 			slotinfop->rstate = (ap_rstate_t)rstate;
10437c478bd9Sstevel@tonic-gate 		}
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 		ap_state.ap_rstate = slotinfop->rstate;
10467c478bd9Sstevel@tonic-gate 		ap_state.ap_ostate = slotinfop->ostate;
10477c478bd9Sstevel@tonic-gate 		ap_state.ap_condition = slotinfop->condition;
10487c478bd9Sstevel@tonic-gate 		ap_state.ap_last_change = slotinfop->last_change;
10497c478bd9Sstevel@tonic-gate 		ap_state.ap_error_code = 0; /* XXX */
10507c478bd9Sstevel@tonic-gate 		if (mutex_held)
10517c478bd9Sstevel@tonic-gate 			ap_state.ap_in_transition = 0; /* AP is not busy */
10527c478bd9Sstevel@tonic-gate 		else
10537c478bd9Sstevel@tonic-gate 			ap_state.ap_in_transition = 1; /* AP is busy */
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 		if (mutex_held)
10567c478bd9Sstevel@tonic-gate 			mutex_exit(&slotinfop->slot_mutex);
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 		/* copy the return-AP-state information to the user space */
10597c478bd9Sstevel@tonic-gate 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS)
10607c478bd9Sstevel@tonic-gate 			rv = EFAULT;
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 		break;
10637c478bd9Sstevel@tonic-gate 
106432d69156Sscarter 	}
10657c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONTROL:
10667c478bd9Sstevel@tonic-gate 		/*
10677c478bd9Sstevel@tonic-gate 		 * HPC control functions:
10687c478bd9Sstevel@tonic-gate 		 *	HPC_CTRL_ENABLE_SLOT/HPC_CTRL_DISABLE_SLOT
10697c478bd9Sstevel@tonic-gate 		 *		Changes the state of the slot and preserves
10707c478bd9Sstevel@tonic-gate 		 *		the state across the reboot.
10717c478bd9Sstevel@tonic-gate 		 *	HPC_CTRL_ENABLE_AUTOCFG/HPC_CTRL_DISABLE_AUTOCFG
10727c478bd9Sstevel@tonic-gate 		 *		Enables or disables the auto configuration
10737c478bd9Sstevel@tonic-gate 		 *		of hot plugged occupant if the hardware
10747c478bd9Sstevel@tonic-gate 		 *		supports notification of the hot plug
10757c478bd9Sstevel@tonic-gate 		 *		events.
10767c478bd9Sstevel@tonic-gate 		 *	HPC_CTRL_GET_LED_STATE/HPC_CTRL_SET_LED_STATE
10777c478bd9Sstevel@tonic-gate 		 *		Controls the state of an LED.
10787c478bd9Sstevel@tonic-gate 		 *	HPC_CTRL_GET_SLOT_INFO
10797c478bd9Sstevel@tonic-gate 		 *		Get slot information data structure
10807c478bd9Sstevel@tonic-gate 		 *		(hpc_slot_info_t).
10817c478bd9Sstevel@tonic-gate 		 *	HPC_CTRL_GET_BOARD_TYPE
10827c478bd9Sstevel@tonic-gate 		 *		Get board type information (hpc_board_type_t).
10837c478bd9Sstevel@tonic-gate 		 *	HPC_CTRL_GET_CARD_INFO
10847c478bd9Sstevel@tonic-gate 		 *		Get card information (hpc_card_info_t).
10857c478bd9Sstevel@tonic-gate 		 *
10867c478bd9Sstevel@tonic-gate 		 * These control functions are used by the cfgadm plug-in
10877c478bd9Sstevel@tonic-gate 		 * to implement "-x" and "-v" options.
10887c478bd9Sstevel@tonic-gate 		 */
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 		/* copy user ioctl data first */
109199e6869fSjmcp 		switch (ddi_model_convert_from(mode & FMODELS)) {
109299e6869fSjmcp 		case DDI_MODEL_ILP32: {
10937c478bd9Sstevel@tonic-gate 			struct hpc_control32_data hpc_ctrldata32;
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 			if (copyin((void *)arg, (void *)&hpc_ctrldata32,
10967c478bd9Sstevel@tonic-gate 				sizeof (struct hpc_control32_data)) != 0) {
10977c478bd9Sstevel@tonic-gate 				rv = EFAULT;
10987c478bd9Sstevel@tonic-gate 				break;
10997c478bd9Sstevel@tonic-gate 			}
11007c478bd9Sstevel@tonic-gate 			hpc_ctrldata.cmd = hpc_ctrldata32.cmd;
11017c478bd9Sstevel@tonic-gate 			hpc_ctrldata.data =
110232d69156Sscarter 			    (void *)(intptr_t)hpc_ctrldata32.data;
110399e6869fSjmcp 			break;
11047c478bd9Sstevel@tonic-gate 		}
110599e6869fSjmcp 		case DDI_MODEL_NONE:
110699e6869fSjmcp 			if (copyin((void *)arg, (void *)&hpc_ctrldata,
110799e6869fSjmcp 			    sizeof (struct hpc_control_data)) != 0) {
110899e6869fSjmcp 				rv = EFAULT;
110999e6869fSjmcp 			}
111099e6869fSjmcp 			break;
111199e6869fSjmcp 		default:
11127c478bd9Sstevel@tonic-gate 			rv = EFAULT;
11137c478bd9Sstevel@tonic-gate 			break;
11147c478bd9Sstevel@tonic-gate 		}
111599e6869fSjmcp 		if (rv == EFAULT)
111699e6869fSjmcp 			break;
11177c478bd9Sstevel@tonic-gate 		/*
11187c478bd9Sstevel@tonic-gate 		 * check for valid request:
11197c478bd9Sstevel@tonic-gate 		 *	1. It is a hotplug slot.
11207c478bd9Sstevel@tonic-gate 		 */
11217c478bd9Sstevel@tonic-gate 		slotinfop = &pcihp_p->slotinfo[pci_dev];
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 		mutex_enter(&slotinfop->slot_mutex);
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 		if (pci_dev >= PCI_MAX_DEVS || slotinfop->slot_hdl == NULL) {
11267c478bd9Sstevel@tonic-gate 			rv = ENXIO;
11277c478bd9Sstevel@tonic-gate 			mutex_exit(&slotinfop->slot_mutex);
11287c478bd9Sstevel@tonic-gate 			break;
11297c478bd9Sstevel@tonic-gate 		}
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 		switch (hpc_ctrldata.cmd) {
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 		case HPC_CTRL_GET_LED_STATE:
11347c478bd9Sstevel@tonic-gate 			/* copy the led info from the user space */
11357c478bd9Sstevel@tonic-gate 			if (copyin(hpc_ctrldata.data, (void *)&led_info,
113632d69156Sscarter 			    sizeof (hpc_led_info_t)) != 0) {
11377c478bd9Sstevel@tonic-gate 				rv = EFAULT;
11387c478bd9Sstevel@tonic-gate 				break;
11397c478bd9Sstevel@tonic-gate 			}
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 			/* get the state of LED information */
11427c478bd9Sstevel@tonic-gate 			if (hpc_nexus_control(slotinfop->slot_hdl,
114332d69156Sscarter 			    HPC_CTRL_GET_LED_STATE, (caddr_t)&led_info) != 0) {
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 				if (rv != ENOTSUP)
11467c478bd9Sstevel@tonic-gate 					rv = EIO;
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 				break;
11497c478bd9Sstevel@tonic-gate 			}
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 			/* copy the led info to the user space */
115232d69156Sscarter 			if (copyout((void *)&led_info, hpc_ctrldata.data,
11537c478bd9Sstevel@tonic-gate 			    sizeof (hpc_led_info_t)) != 0) {
115432d69156Sscarter 				rv = EFAULT;
115532d69156Sscarter 				break;
11567c478bd9Sstevel@tonic-gate 			}
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 			break;
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 		case HPC_CTRL_SET_LED_STATE:
11617c478bd9Sstevel@tonic-gate 			/* copy the led info from the user space */
11627c478bd9Sstevel@tonic-gate 			if (copyin(hpc_ctrldata.data, (void *)&led_info,
11637c478bd9Sstevel@tonic-gate 			    sizeof (hpc_led_info_t)) != 0) {
116432d69156Sscarter 				rv = EFAULT;
116532d69156Sscarter 				break;
11667c478bd9Sstevel@tonic-gate 			}
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 			/* set the state of an LED */
11697c478bd9Sstevel@tonic-gate 			rv = hpc_nexus_control(slotinfop->slot_hdl,
11707c478bd9Sstevel@tonic-gate 			    HPC_CTRL_SET_LED_STATE, (caddr_t)&led_info);
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 			/*
11737c478bd9Sstevel@tonic-gate 			 * If the Hotswap Controller does not support
11747c478bd9Sstevel@tonic-gate 			 * LED management (as you would find typically
11757c478bd9Sstevel@tonic-gate 			 * in the cPCI industry), then we handle the
11767c478bd9Sstevel@tonic-gate 			 * blue LED on/off/blink operations, just in
11777c478bd9Sstevel@tonic-gate 			 * case it helps slot identification.
11787c478bd9Sstevel@tonic-gate 			 */
11797c478bd9Sstevel@tonic-gate 			if ((rv == HPC_ERR_NOTSUPPORTED) &&
118032d69156Sscarter 			    (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI)) {
11817c478bd9Sstevel@tonic-gate 				if (led_info.led != HPC_ATTN_LED)
11827c478bd9Sstevel@tonic-gate 					break;
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 				switch (led_info.state) {
118599e6869fSjmcp 				case HPC_LED_OFF:
118699e6869fSjmcp 					pcihp_hs_csr_op(pcihp_p,
118799e6869fSjmcp 					    pci_dev,
118899e6869fSjmcp 					    HPC_EVENT_SLOT_BLUE_LED_OFF);
118999e6869fSjmcp 					rv = 0;
119099e6869fSjmcp 					break;
119199e6869fSjmcp 				case HPC_LED_ON:
119299e6869fSjmcp 					/*
119399e6869fSjmcp 					 * Please note that leaving
119499e6869fSjmcp 					 * LED ON could be dangerous
119599e6869fSjmcp 					 * as it means it is Ok to
119699e6869fSjmcp 					 * remove the board, which
119799e6869fSjmcp 					 * is not what we want to
119899e6869fSjmcp 					 * convey. So it is upto the
119999e6869fSjmcp 					 * user to take care of this
120099e6869fSjmcp 					 * situation and usage.
120199e6869fSjmcp 					 *
120299e6869fSjmcp 					 * Normally, a Blink command
120399e6869fSjmcp 					 * is more appropriate for
120499e6869fSjmcp 					 * identifying a board.
120599e6869fSjmcp 					 */
120699e6869fSjmcp 					pcihp_hs_csr_op(pcihp_p,
120799e6869fSjmcp 					    pci_dev,
120899e6869fSjmcp 					    HPC_EVENT_SLOT_BLUE_LED_ON);
120999e6869fSjmcp 					rv = 0;
121099e6869fSjmcp 					break;
121199e6869fSjmcp 				case HPC_LED_BLINK:
121299e6869fSjmcp 				{
121399e6869fSjmcp 					int bl;
121499e6869fSjmcp 
121599e6869fSjmcp 					for (bl = 0; bl < 2; bl++) {
12167c478bd9Sstevel@tonic-gate 						pcihp_hs_csr_op(pcihp_p,
121732d69156Sscarter 						    pci_dev,
121832d69156Sscarter 						    HPC_EVENT_SLOT_BLUE_LED_ON);
12197c478bd9Sstevel@tonic-gate 						delay(pcihp_cpci_led_blink);
122099e6869fSjmcp 					pcihp_hs_csr_op(pcihp_p,
122199e6869fSjmcp 					    pci_dev,
122299e6869fSjmcp 					    HPC_EVENT_SLOT_BLUE_LED_OFF);
12237c478bd9Sstevel@tonic-gate 						delay(pcihp_cpci_led_blink);
12247c478bd9Sstevel@tonic-gate 					}
122599e6869fSjmcp 					rv = 0;
122699e6869fSjmcp 					break;
122799e6869fSjmcp 				}
122899e6869fSjmcp 				default:
122999e6869fSjmcp 					break;
12307c478bd9Sstevel@tonic-gate 				}
12317c478bd9Sstevel@tonic-gate 			}
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 			if (rv == HPC_ERR_FAILED)
12347c478bd9Sstevel@tonic-gate 				rv = EIO;
12357c478bd9Sstevel@tonic-gate 			break;
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 		case HPC_CTRL_ENABLE_SLOT:
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 			/*
12407c478bd9Sstevel@tonic-gate 			 * If slot already enabled, do not send a duplicate
12417c478bd9Sstevel@tonic-gate 			 * control message to the HPC driver.
12427c478bd9Sstevel@tonic-gate 			 */
12437c478bd9Sstevel@tonic-gate 			if ((slotinfop->slot_flags & PCIHP_SLOT_DISABLED) == 0)
12447c478bd9Sstevel@tonic-gate 				break;
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 			/* tell the HPC driver also */
12477c478bd9Sstevel@tonic-gate 			if (hpc_nexus_control(slotinfop->slot_hdl,
124832d69156Sscarter 			    HPC_CTRL_ENABLE_SLOT, NULL) != HPC_SUCCESS) {
12497c478bd9Sstevel@tonic-gate 				rv = EIO;
12507c478bd9Sstevel@tonic-gate 				break;
12517c478bd9Sstevel@tonic-gate 			}
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 			/*
12547c478bd9Sstevel@tonic-gate 			 * Enable the slot for hotplug operations.
12557c478bd9Sstevel@tonic-gate 			 */
12567c478bd9Sstevel@tonic-gate 			slotinfop->slot_flags &= ~PCIHP_SLOT_DISABLED;
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 			slotinfop->condition = AP_COND_UNKNOWN;
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 			/* XXX need to preserve this state across reboot? */
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 			break;
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 		case HPC_CTRL_DISABLE_SLOT:
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 			/* Do not disable if occupant configured */
12677c478bd9Sstevel@tonic-gate 			if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {
12687c478bd9Sstevel@tonic-gate 				rv = EAGAIN;
12697c478bd9Sstevel@tonic-gate 				break;
12707c478bd9Sstevel@tonic-gate 			}
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 			/* tell the HPC driver also */
12737c478bd9Sstevel@tonic-gate 			if (hpc_nexus_control(slotinfop->slot_hdl,
127432d69156Sscarter 			    HPC_CTRL_DISABLE_SLOT, NULL) != HPC_SUCCESS) {
12757c478bd9Sstevel@tonic-gate 				rv = EIO;
12767c478bd9Sstevel@tonic-gate 				break;
12777c478bd9Sstevel@tonic-gate 			}
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 			/*
12807c478bd9Sstevel@tonic-gate 			 * Disable the slot for hotplug operations.
12817c478bd9Sstevel@tonic-gate 			 */
12827c478bd9Sstevel@tonic-gate 			slotinfop->slot_flags |= PCIHP_SLOT_DISABLED;
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 			slotinfop->condition = AP_COND_UNUSABLE;
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 			/* XXX need to preserve this state across reboot? */
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 			break;
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 		case HPC_CTRL_ENABLE_AUTOCFG:
12917c478bd9Sstevel@tonic-gate 			/*
12927c478bd9Sstevel@tonic-gate 			 * Enable auto configuration on this slot.
12937c478bd9Sstevel@tonic-gate 			 */
12947c478bd9Sstevel@tonic-gate 			slotinfop->slot_flags |= PCIHP_SLOT_AUTO_CFG_EN;
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 			/* tell the HPC driver also */
12977c478bd9Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
129832d69156Sscarter 			    HPC_CTRL_ENABLE_AUTOCFG, NULL);
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI)
13017c478bd9Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
130232d69156Sscarter 				    HPC_EVENT_ENABLE_ENUM);
13037c478bd9Sstevel@tonic-gate 			break;
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 		case HPC_CTRL_DISABLE_AUTOCFG:
13067c478bd9Sstevel@tonic-gate 			/*
13077c478bd9Sstevel@tonic-gate 			 * Disable auto configuration on this slot.
13087c478bd9Sstevel@tonic-gate 			 */
13097c478bd9Sstevel@tonic-gate 			slotinfop->slot_flags &= ~PCIHP_SLOT_AUTO_CFG_EN;
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 			/* tell the HPC driver also */
13127c478bd9Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
13137c478bd9Sstevel@tonic-gate 			    HPC_CTRL_DISABLE_AUTOCFG, NULL);
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI)
13167c478bd9Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
131732d69156Sscarter 				    HPC_EVENT_DISABLE_ENUM);
13187c478bd9Sstevel@tonic-gate 			break;
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 		case HPC_CTRL_GET_BOARD_TYPE:
132132d69156Sscarter 		{
13227c478bd9Sstevel@tonic-gate 			hpc_board_type_t board_type;
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 			/*
13257c478bd9Sstevel@tonic-gate 			 * Get board type data structure, hpc_board_type_t.
13267c478bd9Sstevel@tonic-gate 			 */
13277c478bd9Sstevel@tonic-gate 			board_type = pcihp_get_board_type(slotinfop);
13287c478bd9Sstevel@tonic-gate 			if (board_type == -1) {
132932d69156Sscarter 				rv = ENXIO;
133032d69156Sscarter 				break;
13317c478bd9Sstevel@tonic-gate 			}
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 			/* copy the board type info to the user space */
13347c478bd9Sstevel@tonic-gate 			if (copyout((void *)&board_type, hpc_ctrldata.data,
13357c478bd9Sstevel@tonic-gate 			    sizeof (hpc_board_type_t)) != 0) {
133632d69156Sscarter 				rv = ENXIO;
133732d69156Sscarter 				break;
13387c478bd9Sstevel@tonic-gate 			}
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 			break;
134132d69156Sscarter 		}
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 		case HPC_CTRL_GET_SLOT_INFO:
134432d69156Sscarter 		{
13457c478bd9Sstevel@tonic-gate 			hpc_slot_info_t slot_info;
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 			/*
13487c478bd9Sstevel@tonic-gate 			 * Get slot information structure, hpc_slot_info_t.
13497c478bd9Sstevel@tonic-gate 			 */
13507c478bd9Sstevel@tonic-gate 			slot_info.version = HPC_SLOT_INFO_VERSION;
13517c478bd9Sstevel@tonic-gate 			slot_info.slot_type = slotinfop->slot_type;
13527c478bd9Sstevel@tonic-gate 			slot_info.pci_slot_capabilities =
135332d69156Sscarter 			    slotinfop->slot_capabilities;
13547c478bd9Sstevel@tonic-gate 			slot_info.pci_dev_num = (uint16_t)pci_dev;
13557c478bd9Sstevel@tonic-gate 			(void) strcpy(slot_info.pci_slot_name, slotinfop->name);
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 			/* copy the slot info structure to the user space */
13587c478bd9Sstevel@tonic-gate 			if (copyout((void *)&slot_info, hpc_ctrldata.data,
13597c478bd9Sstevel@tonic-gate 			    sizeof (hpc_slot_info_t)) != 0) {
136032d69156Sscarter 				rv = EFAULT;
136132d69156Sscarter 				break;
13627c478bd9Sstevel@tonic-gate 			}
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 			break;
136532d69156Sscarter 		}
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 		case HPC_CTRL_GET_CARD_INFO:
136832d69156Sscarter 		{
13697c478bd9Sstevel@tonic-gate 			hpc_card_info_t card_info;
13707c478bd9Sstevel@tonic-gate 			ddi_acc_handle_t handle;
13717c478bd9Sstevel@tonic-gate 			dev_info_t *cdip;
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 			/*
13747c478bd9Sstevel@tonic-gate 			 * Get card information structure, hpc_card_info_t.
13757c478bd9Sstevel@tonic-gate 			 */
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 			/* verify that the card is configured */
13787c478bd9Sstevel@tonic-gate 			if ((slotinfop->ostate != AP_OSTATE_CONFIGURED) ||
137932d69156Sscarter 			    ((cdip = pcihp_devi_find(self,
138032d69156Sscarter 			    pci_dev, 0)) == NULL)) {
138132d69156Sscarter 				/*
138232d69156Sscarter 				 * either the card is not present or
138332d69156Sscarter 				 * it is not configured.
138432d69156Sscarter 				 */
138532d69156Sscarter 				rv = ENXIO;
138632d69156Sscarter 				break;
13877c478bd9Sstevel@tonic-gate 			}
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 			/*
13907c478bd9Sstevel@tonic-gate 			 * If declared failed, don't allow Config operations.
13917c478bd9Sstevel@tonic-gate 			 * Otherwise, if good or failing, it is assumed Ok
13927c478bd9Sstevel@tonic-gate 			 * to get config data.
13937c478bd9Sstevel@tonic-gate 			 */
13947c478bd9Sstevel@tonic-gate 			if (slotinfop->condition == AP_COND_FAILED) {
13957c478bd9Sstevel@tonic-gate 				rv = EIO;
13967c478bd9Sstevel@tonic-gate 				break;
13977c478bd9Sstevel@tonic-gate 			}
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 			/* get the information from the PCI config header */
14007c478bd9Sstevel@tonic-gate 			/* for the function 0.				  */
14017c478bd9Sstevel@tonic-gate 			if (pci_config_setup(cdip, &handle) != DDI_SUCCESS) {
14027c478bd9Sstevel@tonic-gate 				rv = EIO;
14037c478bd9Sstevel@tonic-gate 				break;
14047c478bd9Sstevel@tonic-gate 			}
14057c478bd9Sstevel@tonic-gate 			card_info.prog_class = pci_config_get8(handle,
140632d69156Sscarter 			    PCI_CONF_PROGCLASS);
14077c478bd9Sstevel@tonic-gate 			card_info.base_class = pci_config_get8(handle,
140832d69156Sscarter 			    PCI_CONF_BASCLASS);
14097c478bd9Sstevel@tonic-gate 			card_info.sub_class = pci_config_get8(handle,
141032d69156Sscarter 			    PCI_CONF_SUBCLASS);
14117c478bd9Sstevel@tonic-gate 			card_info.header_type = pci_config_get8(handle,
141232d69156Sscarter 			    PCI_CONF_HEADER);
14137c478bd9Sstevel@tonic-gate 			pci_config_teardown(&handle);
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 			/* copy the card info structure to the user space */
14167c478bd9Sstevel@tonic-gate 			if (copyout((void *)&card_info, hpc_ctrldata.data,
14177c478bd9Sstevel@tonic-gate 			    sizeof (hpc_card_info_t)) != 0) {
141832d69156Sscarter 				rv = EFAULT;
141932d69156Sscarter 				break;
14207c478bd9Sstevel@tonic-gate 			}
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 			break;
142332d69156Sscarter 		}
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 		default:
14267c478bd9Sstevel@tonic-gate 			rv = EINVAL;
14277c478bd9Sstevel@tonic-gate 			break;
14287c478bd9Sstevel@tonic-gate 		}
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 		mutex_exit(&slotinfop->slot_mutex);
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 		break;
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	default:
14357c478bd9Sstevel@tonic-gate 		rv = ENOTTY;
14367c478bd9Sstevel@tonic-gate 	}
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	if (cmd != DEVCTL_AP_CONTROL)
14397c478bd9Sstevel@tonic-gate 		ndi_dc_freehdl(dcp);
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 	(void) pcihp_get_soft_state(self, state_unlocking, &rval);
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 	return (rv);
14447c478bd9Sstevel@tonic-gate }
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate /*
14477c478bd9Sstevel@tonic-gate  * **************************************
14487c478bd9Sstevel@tonic-gate  * CONFIGURE the occupant in the slot.
14497c478bd9Sstevel@tonic-gate  * **************************************
14507c478bd9Sstevel@tonic-gate  */
14517c478bd9Sstevel@tonic-gate static int
pcihp_configure_ap(pcihp_t * pcihp_p,int pci_dev)14527c478bd9Sstevel@tonic-gate pcihp_configure_ap(pcihp_t *pcihp_p, int pci_dev)
14537c478bd9Sstevel@tonic-gate {
14547c478bd9Sstevel@tonic-gate 	dev_info_t *self = pcihp_p->dip;
14557c478bd9Sstevel@tonic-gate 	int rv = HPC_SUCCESS;
14567c478bd9Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
14577c478bd9Sstevel@tonic-gate 	hpc_slot_state_t rstate;
14587c478bd9Sstevel@tonic-gate 	struct pcihp_config_ctrl ctrl;
14597c478bd9Sstevel@tonic-gate 	time_t time;
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	/*
14627c478bd9Sstevel@tonic-gate 	 * check for valid request:
14637c478bd9Sstevel@tonic-gate 	 *	1. It is a hotplug slot.
14647c478bd9Sstevel@tonic-gate 	 *	2. The receptacle is in the CONNECTED state.
14657c478bd9Sstevel@tonic-gate 	 */
14667c478bd9Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
14677c478bd9Sstevel@tonic-gate 
146870025d76Sjohnny 
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 	if ((pci_dev >= PCI_MAX_DEVS) || (slotinfop->slot_hdl == NULL) ||
147132d69156Sscarter 	    (slotinfop->slot_flags & PCIHP_SLOT_DISABLED)) {
147270025d76Sjohnny 
14737c478bd9Sstevel@tonic-gate 		return (ENXIO);
14747c478bd9Sstevel@tonic-gate 	}
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	/*
14777c478bd9Sstevel@tonic-gate 	 * If the occupant is already in (partially?) configured
14787c478bd9Sstevel@tonic-gate 	 * state then call the ndi_devi_online() on the device
14797c478bd9Sstevel@tonic-gate 	 * subtree(s) for this attachment point.
14807c478bd9Sstevel@tonic-gate 	 */
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {
14837c478bd9Sstevel@tonic-gate 		ctrl.flags = PCIHP_CFG_CONTINUE;
14847c478bd9Sstevel@tonic-gate 		ctrl.rv = NDI_SUCCESS;
14857c478bd9Sstevel@tonic-gate 		ctrl.dip = NULL;
14867c478bd9Sstevel@tonic-gate 		ctrl.pci_dev = pci_dev;
14877c478bd9Sstevel@tonic-gate 		ctrl.op = PCIHP_ONLINE;
14887c478bd9Sstevel@tonic-gate 
14893fe80ca4SDan Cross 		ndi_devi_enter(self);
14907c478bd9Sstevel@tonic-gate 		ddi_walk_devs(ddi_get_child(self), pcihp_configure,
149132d69156Sscarter 		    (void *)&ctrl);
14923fe80ca4SDan Cross 		ndi_devi_exit(self);
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 		if (ctrl.rv != NDI_SUCCESS) {
14957c478bd9Sstevel@tonic-gate 			/*
14967c478bd9Sstevel@tonic-gate 			 * one or more of the devices are not
14977c478bd9Sstevel@tonic-gate 			 * onlined. How is this to be reported?
14987c478bd9Sstevel@tonic-gate 			 */
14997c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
150032d69156Sscarter 			    "pcihp (%s%d): failed to attach one or"
150132d69156Sscarter 			    " more drivers for the card in the slot %s",
150232d69156Sscarter 			    ddi_driver_name(self), ddi_get_instance(self),
150332d69156Sscarter 			    slotinfop->name);
15047c478bd9Sstevel@tonic-gate 			/* rv = EFAULT; */
15057c478bd9Sstevel@tonic-gate 		}
15067c478bd9Sstevel@tonic-gate 		/* tell HPC driver that the occupant is configured */
15077c478bd9Sstevel@tonic-gate 		(void) hpc_nexus_control(slotinfop->slot_hdl,
150832d69156Sscarter 		    HPC_CTRL_DEV_CONFIGURED, NULL);
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 		if (drv_getparm(TIME, (void *)&time) != DDI_SUCCESS)
15117c478bd9Sstevel@tonic-gate 			slotinfop->last_change = (time_t)-1;
15127c478bd9Sstevel@tonic-gate 		else
15137c478bd9Sstevel@tonic-gate 			slotinfop->last_change = (time32_t)time;
15147c478bd9Sstevel@tonic-gate 
151570025d76Sjohnny 
15167c478bd9Sstevel@tonic-gate 		return (rv);
15177c478bd9Sstevel@tonic-gate 	}
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 	/*
15207c478bd9Sstevel@tonic-gate 	 * Occupant is in the UNCONFIGURED state.
15217c478bd9Sstevel@tonic-gate 	 */
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	/* Check if the receptacle is in the CONNECTED state. */
15247c478bd9Sstevel@tonic-gate 	if (hpc_nexus_control(slotinfop->slot_hdl,
152532d69156Sscarter 	    HPC_CTRL_GET_SLOT_STATE, (caddr_t)&rstate) != 0) {
152670025d76Sjohnny 
15277c478bd9Sstevel@tonic-gate 		return (ENXIO);
15287c478bd9Sstevel@tonic-gate 	}
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 	if (rstate == HPC_SLOT_EMPTY) {
15317c478bd9Sstevel@tonic-gate 		/* error. slot is empty */
153270025d76Sjohnny 
15337c478bd9Sstevel@tonic-gate 		return (ENXIO);
15347c478bd9Sstevel@tonic-gate 	}
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 	if (rstate != HPC_SLOT_CONNECTED) {
15377c478bd9Sstevel@tonic-gate 		/* error. either the slot is empty or connect failed */
153870025d76Sjohnny 
15397c478bd9Sstevel@tonic-gate 		return (ENXIO);
15407c478bd9Sstevel@tonic-gate 	}
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 	slotinfop->rstate = AP_RSTATE_CONNECTED; /* record rstate */
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 	/* Turn INS and LED off, and start configuration. */
15457c478bd9Sstevel@tonic-gate 	if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
154632d69156Sscarter 		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_SLOT_CONFIGURE);
15477c478bd9Sstevel@tonic-gate 		if (pcihp_cpci_blue_led)
15487c478bd9Sstevel@tonic-gate 			pcihp_hs_csr_op(pcihp_p, pci_dev,
154932d69156Sscarter 			    HPC_EVENT_SLOT_BLUE_LED_OFF);
15507c478bd9Sstevel@tonic-gate 		slotinfop->slot_flags &= ~PCIHP_SLOT_ENUM_INS_PENDING;
15517c478bd9Sstevel@tonic-gate 	}
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 	(void) hpc_nexus_control(slotinfop->slot_hdl,
155432d69156Sscarter 	    HPC_CTRL_DEV_CONFIG_START, NULL);
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 	/*
15577c478bd9Sstevel@tonic-gate 	 * Call the configurator to configure the card.
15587c478bd9Sstevel@tonic-gate 	 */
155926947304SEvan Yan 	if (pcicfg_configure(self, pci_dev, PCICFG_ALL_FUNC, 0)
156026947304SEvan Yan 	    != PCICFG_SUCCESS) {
15617c478bd9Sstevel@tonic-gate 		if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
15627c478bd9Sstevel@tonic-gate 			if (pcihp_cpci_blue_led)
15637c478bd9Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
156432d69156Sscarter 				    HPC_EVENT_SLOT_BLUE_LED_ON);
15657c478bd9Sstevel@tonic-gate 			pcihp_hs_csr_op(pcihp_p, pci_dev,
156632d69156Sscarter 			    HPC_EVENT_SLOT_UNCONFIGURE);
15677c478bd9Sstevel@tonic-gate 		}
15687c478bd9Sstevel@tonic-gate 		/* tell HPC driver occupant configure Error */
15697c478bd9Sstevel@tonic-gate 		(void) hpc_nexus_control(slotinfop->slot_hdl,
157032d69156Sscarter 		    HPC_CTRL_DEV_CONFIG_FAILURE, NULL);
157170025d76Sjohnny 
15727c478bd9Sstevel@tonic-gate 		return (EIO);
15737c478bd9Sstevel@tonic-gate 	}
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	/* record the occupant state as CONFIGURED */
15767c478bd9Sstevel@tonic-gate 	slotinfop->ostate = AP_OSTATE_CONFIGURED;
15777c478bd9Sstevel@tonic-gate 	slotinfop->condition = AP_COND_OK;
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 	/* now, online all the devices in the AP */
15807c478bd9Sstevel@tonic-gate 	ctrl.flags = PCIHP_CFG_CONTINUE;
15817c478bd9Sstevel@tonic-gate 	ctrl.rv = NDI_SUCCESS;
15827c478bd9Sstevel@tonic-gate 	ctrl.dip = NULL;
15837c478bd9Sstevel@tonic-gate 	ctrl.pci_dev = pci_dev;
15847c478bd9Sstevel@tonic-gate 	ctrl.op = PCIHP_ONLINE;
15857c478bd9Sstevel@tonic-gate 
15863fe80ca4SDan Cross 	ndi_devi_enter(self);
158732d69156Sscarter 	ddi_walk_devs(ddi_get_child(self), pcihp_configure, (void *)&ctrl);
15883fe80ca4SDan Cross 	ndi_devi_exit(self);
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	if (ctrl.rv != NDI_SUCCESS) {
15917c478bd9Sstevel@tonic-gate 		/*
15927c478bd9Sstevel@tonic-gate 		 * one or more of the devices are not
15937c478bd9Sstevel@tonic-gate 		 * ONLINE'd. How is this to be
15947c478bd9Sstevel@tonic-gate 		 * reported?
15957c478bd9Sstevel@tonic-gate 		 */
15967c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
159732d69156Sscarter 		    "pcihp (%s%d): failed to attach one or"
159832d69156Sscarter 		    " more drivers for the card in the slot %s",
159932d69156Sscarter 		    ddi_driver_name(pcihp_p->dip),
160032d69156Sscarter 		    ddi_get_instance(pcihp_p->dip),
160132d69156Sscarter 		    slotinfop->name);
16027c478bd9Sstevel@tonic-gate 		/* rv = EFAULT; */
16037c478bd9Sstevel@tonic-gate 	}
16047c478bd9Sstevel@tonic-gate 	/* store HS_CSR location.  No events, jut a read operation. */
16057c478bd9Sstevel@tonic-gate 	if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI)
16067c478bd9Sstevel@tonic-gate 		pcihp_hs_csr_op(pcihp_p, pci_dev, -1);
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 	/* tell HPC driver that the occupant is configured */
16097c478bd9Sstevel@tonic-gate 	(void) hpc_nexus_control(slotinfop->slot_hdl,
161032d69156Sscarter 	    HPC_CTRL_DEV_CONFIGURED, NULL);
16117c478bd9Sstevel@tonic-gate 
161270025d76Sjohnny 
16137c478bd9Sstevel@tonic-gate 	return (rv);
16147c478bd9Sstevel@tonic-gate }
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate /*
16177c478bd9Sstevel@tonic-gate  * **************************************
16187c478bd9Sstevel@tonic-gate  * UNCONFIGURE the occupant in the slot.
16197c478bd9Sstevel@tonic-gate  * **************************************
16207c478bd9Sstevel@tonic-gate  */
16217c478bd9Sstevel@tonic-gate static int
pcihp_unconfigure_ap(pcihp_t * pcihp_p,int pci_dev)16227c478bd9Sstevel@tonic-gate pcihp_unconfigure_ap(pcihp_t *pcihp_p, int pci_dev)
16237c478bd9Sstevel@tonic-gate {
16247c478bd9Sstevel@tonic-gate 	dev_info_t *self = pcihp_p->dip;
16257c478bd9Sstevel@tonic-gate 	int rv = HPC_SUCCESS;
16267c478bd9Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
16277c478bd9Sstevel@tonic-gate 	struct pcihp_config_ctrl ctrl;
16287c478bd9Sstevel@tonic-gate 	time_t time;
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 	/*
16317c478bd9Sstevel@tonic-gate 	 * check for valid request:
16327c478bd9Sstevel@tonic-gate 	 *	1. It is a hotplug slot.
16337c478bd9Sstevel@tonic-gate 	 *	2. The occupant is in the CONFIGURED state.
16347c478bd9Sstevel@tonic-gate 	 */
16357c478bd9Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
16367c478bd9Sstevel@tonic-gate 
163770025d76Sjohnny 
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	if ((pci_dev >= PCI_MAX_DEVS) || (slotinfop->slot_hdl == NULL) ||
164032d69156Sscarter 	    (slotinfop->slot_flags & PCIHP_SLOT_DISABLED)) {
164170025d76Sjohnny 
16427c478bd9Sstevel@tonic-gate 		return (ENXIO);
16437c478bd9Sstevel@tonic-gate 	}
16447c478bd9Sstevel@tonic-gate 	/*
16457c478bd9Sstevel@tonic-gate 	 * The following may not need to be there, as we should
16467c478bd9Sstevel@tonic-gate 	 * support unconfiguring of boards and free resources
16477c478bd9Sstevel@tonic-gate 	 * even when the board is not hotswappable. But this is
16487c478bd9Sstevel@tonic-gate 	 * the only way, we may be able to tell the system
16497c478bd9Sstevel@tonic-gate 	 * administrator that it is not a hotswap board since
16507c478bd9Sstevel@tonic-gate 	 * disconnect operation is never called.
16517c478bd9Sstevel@tonic-gate 	 * This way we help the system administrator from not
16527c478bd9Sstevel@tonic-gate 	 * accidentally removing a non hotswap board and
16537c478bd9Sstevel@tonic-gate 	 * possibly destroying it. May be this behavior can
16547c478bd9Sstevel@tonic-gate 	 * be a default, and can be enabled or disabled via
16557c478bd9Sstevel@tonic-gate 	 * a global flag.
16567c478bd9Sstevel@tonic-gate 	 */
16577c478bd9Sstevel@tonic-gate 	if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
165832d69156Sscarter 		if (slotinfop->slot_flags & PCIHP_SLOT_DEV_NON_HOTPLUG) {
16597c478bd9Sstevel@tonic-gate 			/* Operation unsupported if no HS board/slot */
16607c478bd9Sstevel@tonic-gate 			return (ENOTSUP);
16617c478bd9Sstevel@tonic-gate 		}
16627c478bd9Sstevel@tonic-gate 	}
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	/*
16657c478bd9Sstevel@tonic-gate 	 * If the occupant is in the CONFIGURED state then
16667c478bd9Sstevel@tonic-gate 	 * call the configurator to unconfigure the slot.
16677c478bd9Sstevel@tonic-gate 	 */
16687c478bd9Sstevel@tonic-gate 	if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 		/*
16717c478bd9Sstevel@tonic-gate 		 * since potential state change is imminent mask
16727c478bd9Sstevel@tonic-gate 		 * enum events to prevent the slot from being re-configured
16737c478bd9Sstevel@tonic-gate 		 */
16747c478bd9Sstevel@tonic-gate 		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_DISABLE_ENUM);
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 		/*
16777c478bd9Sstevel@tonic-gate 		 * Detach all the drivers for the devices in the
16787c478bd9Sstevel@tonic-gate 		 * slot. Call pcihp_configure() to do this.
16797c478bd9Sstevel@tonic-gate 		 */
16807c478bd9Sstevel@tonic-gate 		ctrl.flags = 0;
16817c478bd9Sstevel@tonic-gate 		ctrl.rv = NDI_SUCCESS;
16827c478bd9Sstevel@tonic-gate 		ctrl.dip = NULL;
16837c478bd9Sstevel@tonic-gate 		ctrl.pci_dev = pci_dev;
16847c478bd9Sstevel@tonic-gate 		ctrl.op = PCIHP_OFFLINE;
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 		(void) devfs_clean(self, NULL, DV_CLEAN_FORCE);
16873fe80ca4SDan Cross 		ndi_devi_enter(self);
16887c478bd9Sstevel@tonic-gate 		ddi_walk_devs(ddi_get_child(self), pcihp_configure,
168932d69156Sscarter 		    (void *)&ctrl);
16903fe80ca4SDan Cross 		ndi_devi_exit(self);
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 		if (ctrl.rv != NDI_SUCCESS) {
16937c478bd9Sstevel@tonic-gate 			/*
16947c478bd9Sstevel@tonic-gate 			 * Failed to detach one or more drivers
16957c478bd9Sstevel@tonic-gate 			 * Restore the state of drivers which
16967c478bd9Sstevel@tonic-gate 			 * are offlined during this operation.
16977c478bd9Sstevel@tonic-gate 			 */
16987c478bd9Sstevel@tonic-gate 			ctrl.flags = 0;
16997c478bd9Sstevel@tonic-gate 			ctrl.rv = NDI_SUCCESS;
17007c478bd9Sstevel@tonic-gate 			ctrl.dip = NULL;
17017c478bd9Sstevel@tonic-gate 			ctrl.pci_dev = pci_dev;
17027c478bd9Sstevel@tonic-gate 			ctrl.op = PCIHP_ONLINE;
17037c478bd9Sstevel@tonic-gate 
17043fe80ca4SDan Cross 			ndi_devi_enter(self);
17057c478bd9Sstevel@tonic-gate 			ddi_walk_devs(ddi_get_child(self),
170632d69156Sscarter 			    pcihp_configure, (void *)&ctrl);
17073fe80ca4SDan Cross 			ndi_devi_exit(self);
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 			/* tell HPC driver that the occupant is Busy */
17107c478bd9Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
171132d69156Sscarter 			    HPC_CTRL_DEV_UNCONFIG_FAILURE, NULL);
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 			rv = EBUSY;
17147c478bd9Sstevel@tonic-gate 		} else {
17157c478bd9Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
171632d69156Sscarter 			    HPC_CTRL_DEV_UNCONFIG_START, NULL);
17177c478bd9Sstevel@tonic-gate 
171826947304SEvan Yan 			if (pcicfg_unconfigure(self, pci_dev,
171926947304SEvan Yan 			    PCICFG_ALL_FUNC, 0) == PCICFG_SUCCESS) {
172032d69156Sscarter 				/*
172132d69156Sscarter 				 * Now that resources are freed,
172232d69156Sscarter 				 * clear EXT and Turn LED ON.
172332d69156Sscarter 				 */
172432d69156Sscarter 				if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
17257c478bd9Sstevel@tonic-gate 					pcihp_hs_csr_op(pcihp_p, pci_dev,
172632d69156Sscarter 					    HPC_EVENT_SLOT_UNCONFIGURE);
172732d69156Sscarter 					if (pcihp_cpci_blue_led)
172832d69156Sscarter 						pcihp_hs_csr_op(pcihp_p,
172932d69156Sscarter 						    pci_dev,
173032d69156Sscarter 						    HPC_EVENT_SLOT_BLUE_LED_ON);
173132d69156Sscarter 					slotinfop->hs_csr_location = 0;
173232d69156Sscarter 					slotinfop->slot_flags &=
173332d69156Sscarter 					    ~(PCIHP_SLOT_DEV_NON_HOTPLUG|
173432d69156Sscarter 					    PCIHP_SLOT_ENUM_EXT_PENDING);
173532d69156Sscarter 				}
173632d69156Sscarter 				slotinfop->ostate = AP_OSTATE_UNCONFIGURED;
17377c478bd9Sstevel@tonic-gate 				slotinfop->condition = AP_COND_UNKNOWN;
17387c478bd9Sstevel@tonic-gate 				/*
17397c478bd9Sstevel@tonic-gate 				 * send the notification of state change
17407c478bd9Sstevel@tonic-gate 				 * to the HPC driver.
17417c478bd9Sstevel@tonic-gate 				 */
174232d69156Sscarter 				(void) hpc_nexus_control(slotinfop->slot_hdl,
174332d69156Sscarter 				    HPC_CTRL_DEV_UNCONFIGURED,
174432d69156Sscarter 				    NULL);
17457c478bd9Sstevel@tonic-gate 			} else {
17467c478bd9Sstevel@tonic-gate 				/* tell HPC driver occupant unconfigure Error */
17477c478bd9Sstevel@tonic-gate 				(void) hpc_nexus_control(slotinfop->slot_hdl,
174832d69156Sscarter 				    HPC_CTRL_DEV_UNCONFIG_FAILURE, NULL);
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 				rv = EIO;
17517c478bd9Sstevel@tonic-gate 			}
17527c478bd9Sstevel@tonic-gate 		}
17537c478bd9Sstevel@tonic-gate 	}
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 	if (drv_getparm(TIME, (void *)&time) != DDI_SUCCESS)
17567c478bd9Sstevel@tonic-gate 		slotinfop->last_change = (time_t)-1;
17577c478bd9Sstevel@tonic-gate 	else
17587c478bd9Sstevel@tonic-gate 		slotinfop->last_change = (time32_t)time;
17597c478bd9Sstevel@tonic-gate 
176070025d76Sjohnny 
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 	/* unmask enum events again */
17637c478bd9Sstevel@tonic-gate 	if ((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) == 0) {
17647c478bd9Sstevel@tonic-gate 		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_ENABLE_ENUM);
17657c478bd9Sstevel@tonic-gate 	}
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 	return (rv);
17687c478bd9Sstevel@tonic-gate }
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate /*
17717c478bd9Sstevel@tonic-gate  * Accessor function to return pointer to the pci hotplug
17727c478bd9Sstevel@tonic-gate  * cb_ops structure.
17737c478bd9Sstevel@tonic-gate  */
17747c478bd9Sstevel@tonic-gate struct cb_ops *
pcihp_get_cb_ops()17757c478bd9Sstevel@tonic-gate pcihp_get_cb_ops()
17767c478bd9Sstevel@tonic-gate {
17777c478bd9Sstevel@tonic-gate 	return (&pcihp_cb_ops);
17787c478bd9Sstevel@tonic-gate }
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate /*
17817c478bd9Sstevel@tonic-gate  * Setup function to initialize hot plug feature. Returns DDI_SUCCESS
17827c478bd9Sstevel@tonic-gate  * for successful initialization, otherwise it returns DDI_FAILURE.
17837c478bd9Sstevel@tonic-gate  *
17847c478bd9Sstevel@tonic-gate  * It is assumed that this this function is called from the attach()
17857c478bd9Sstevel@tonic-gate  * entry point of the PCI nexus driver.
17867c478bd9Sstevel@tonic-gate  */
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate int
pcihp_init(dev_info_t * dip)17897c478bd9Sstevel@tonic-gate pcihp_init(dev_info_t *dip)
17907c478bd9Sstevel@tonic-gate {
17917c478bd9Sstevel@tonic-gate 	pcihp_t *pcihp_p;
17927c478bd9Sstevel@tonic-gate 	int i;
17937c478bd9Sstevel@tonic-gate 	caddr_t enum_data;
17947c478bd9Sstevel@tonic-gate 	int enum_size;
17957c478bd9Sstevel@tonic-gate 	int rv;
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 	mutex_enter(&pcihp_open_mutex);
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 	/*
18007c478bd9Sstevel@tonic-gate 	 * Make sure that it is not already initialized.
18017c478bd9Sstevel@tonic-gate 	 */
18027c478bd9Sstevel@tonic-gate 	if (pcihp_get_soft_state(dip, PCIHP_DR_NOOP, &rv) != NULL) {
18037c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: pcihp instance already initialized!",
18047c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
18057c478bd9Sstevel@tonic-gate 		goto cleanup;
18067c478bd9Sstevel@tonic-gate 	}
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	/*
18097c478bd9Sstevel@tonic-gate 	 * Initialize soft state structure for the bus instance.
18107c478bd9Sstevel@tonic-gate 	 */
18117c478bd9Sstevel@tonic-gate 	if ((pcihp_p = pcihp_create_soft_state(dip)) == NULL) {
18127c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: can't allocate pcihp structure",
18137c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
18147c478bd9Sstevel@tonic-gate 		goto cleanup;
18157c478bd9Sstevel@tonic-gate 	}
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	pcihp_p->soft_state = PCIHP_SOFT_STATE_CLOSED;
18187c478bd9Sstevel@tonic-gate 	/* XXX if bus is running at 66Mhz then set PCI_BUS_66MHZ bit */
18197c478bd9Sstevel@tonic-gate 	pcihp_p->bus_flags = 0;	/* XXX FIX IT */
18207c478bd9Sstevel@tonic-gate 
18217c478bd9Sstevel@tonic-gate 	/*
18227c478bd9Sstevel@tonic-gate 	 * If a platform wishes to implement Radial ENUM# routing
18237c478bd9Sstevel@tonic-gate 	 * a property "enum-impl" must be presented to us with a
18247c478bd9Sstevel@tonic-gate 	 * string value "radial".
18257c478bd9Sstevel@tonic-gate 	 * This helps us not go for polling operation (default)
18267c478bd9Sstevel@tonic-gate 	 * during a ENUM# event.
18277c478bd9Sstevel@tonic-gate 	 */
18287c478bd9Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, 0, "enum-impl",
18297c478bd9Sstevel@tonic-gate 	    (caddr_t)&enum_data, &enum_size) == DDI_PROP_SUCCESS) {
18307c478bd9Sstevel@tonic-gate 		if (strcmp(enum_data, "radial") == 0) {
18317c478bd9Sstevel@tonic-gate 			pcihp_p->bus_flags |= PCIHP_BUS_ENUM_RADIAL;
18327c478bd9Sstevel@tonic-gate 		}
18337c478bd9Sstevel@tonic-gate 		kmem_free(enum_data, enum_size);
18347c478bd9Sstevel@tonic-gate 	}
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate 	for (i = 0; i < PCI_MAX_DEVS; i++) {
18377c478bd9Sstevel@tonic-gate 		/* initialize slot mutex */
18387c478bd9Sstevel@tonic-gate 		mutex_init(&pcihp_p->slotinfo[i].slot_mutex, NULL,
183932d69156Sscarter 		    MUTEX_DRIVER, NULL);
18407c478bd9Sstevel@tonic-gate 	}
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 	/*
18437c478bd9Sstevel@tonic-gate 	 *  register the bus instance with the HPS framework.
18447c478bd9Sstevel@tonic-gate 	 */
18457c478bd9Sstevel@tonic-gate 	if (hpc_nexus_register_bus(dip, pcihp_new_slot_state, 0) != 0) {
18467c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: failed to register the bus with HPS",
18477c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
18487c478bd9Sstevel@tonic-gate 		goto cleanup1;
18497c478bd9Sstevel@tonic-gate 	}
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 	/*
18527c478bd9Sstevel@tonic-gate 	 * Create the "devctl" minor for hot plug support. The minor
18537c478bd9Sstevel@tonic-gate 	 * number for "devctl" node is in the same format as the AP
18547c478bd9Sstevel@tonic-gate 	 * minor nodes.
18557c478bd9Sstevel@tonic-gate 	 */
18567c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
18577c478bd9Sstevel@tonic-gate 	    PCIHP_AP_MINOR_NUM(ddi_get_instance(dip), PCIHP_DEVCTL_MINOR),
18587c478bd9Sstevel@tonic-gate 	    DDI_NT_NEXUS, 0) != DDI_SUCCESS)
18597c478bd9Sstevel@tonic-gate 		goto cleanup2;
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 	/*
18627c478bd9Sstevel@tonic-gate 	 * Setup resource maps for this bus node. (Note: This can
18637c478bd9Sstevel@tonic-gate 	 * be done from the attach(9E) of the nexus itself.)
18647c478bd9Sstevel@tonic-gate 	 */
18657c478bd9Sstevel@tonic-gate 	(void) pci_resource_setup(dip);
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	pcihp_p->bus_state = PCIHP_BUS_CONFIGURED;
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	mutex_exit(&pcihp_open_mutex);
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate cleanup2:
18747c478bd9Sstevel@tonic-gate 	(void) hpc_nexus_unregister_bus(dip);
18757c478bd9Sstevel@tonic-gate cleanup1:
18767c478bd9Sstevel@tonic-gate 	for (i = 0; i < PCI_MAX_DEVS; i++)
18777c478bd9Sstevel@tonic-gate 		mutex_destroy(&pcihp_p->slotinfo[i].slot_mutex);
18787c478bd9Sstevel@tonic-gate 	pcihp_destroy_soft_state(dip);
18797c478bd9Sstevel@tonic-gate cleanup:
18807c478bd9Sstevel@tonic-gate 	mutex_exit(&pcihp_open_mutex);
18817c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
18827c478bd9Sstevel@tonic-gate }
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate /*
18857c478bd9Sstevel@tonic-gate  * pcihp_uninit()
18867c478bd9Sstevel@tonic-gate  *
18877c478bd9Sstevel@tonic-gate  * The bus instance is going away, cleanup any data associated with
18887c478bd9Sstevel@tonic-gate  * the management of hot plug slots. It is assumed that this function
18897c478bd9Sstevel@tonic-gate  * is called from detach() routine of the PCI nexus driver. Also,
18907c478bd9Sstevel@tonic-gate  * it is assumed that no devices on the bus are in the configured state.
18917c478bd9Sstevel@tonic-gate  */
18927c478bd9Sstevel@tonic-gate int
pcihp_uninit(dev_info_t * dip)18937c478bd9Sstevel@tonic-gate pcihp_uninit(dev_info_t *dip)
18947c478bd9Sstevel@tonic-gate {
18957c478bd9Sstevel@tonic-gate 	pcihp_t *pcihp_p;
18967c478bd9Sstevel@tonic-gate 	int i, j;
18977c478bd9Sstevel@tonic-gate 	int rv;
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 	mutex_enter(&pcihp_open_mutex);
19007c478bd9Sstevel@tonic-gate 	/* get a pointer to the soft state structure */
19017c478bd9Sstevel@tonic-gate 	pcihp_p = pcihp_get_soft_state(dip, PCIHP_DR_BUS_UNCONFIGURE, &rv);
19027c478bd9Sstevel@tonic-gate 	ASSERT(pcihp_p != NULL);
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate 	/* slot mutexes should prevent any configure/unconfigure access */
19057c478bd9Sstevel@tonic-gate 	for (i = 0; i < PCI_MAX_DEVS; i++) {
19067c478bd9Sstevel@tonic-gate 		if (!mutex_tryenter(&pcihp_p->slotinfo[i].slot_mutex)) {
19077c478bd9Sstevel@tonic-gate 			for (j = 0; j < i; j++) {
19087c478bd9Sstevel@tonic-gate 				mutex_exit(&pcihp_p->slotinfo[j].slot_mutex);
19097c478bd9Sstevel@tonic-gate 			}
19107c478bd9Sstevel@tonic-gate 			mutex_exit(&pcihp_open_mutex);
19117c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
19127c478bd9Sstevel@tonic-gate 		}
19137c478bd9Sstevel@tonic-gate 	}
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate 	if ((pcihp_p->soft_state != PCIHP_SOFT_STATE_CLOSED) ||
19167c478bd9Sstevel@tonic-gate 	    (rv == PCIHP_FAILURE)) {
19177c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: pcihp instance is busy",
19187c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
19197c478bd9Sstevel@tonic-gate 		for (i = 0; i < PCI_MAX_DEVS; i++) {
19207c478bd9Sstevel@tonic-gate 			mutex_exit(&pcihp_p->slotinfo[i].slot_mutex);
19217c478bd9Sstevel@tonic-gate 		}
19227c478bd9Sstevel@tonic-gate 		mutex_exit(&pcihp_open_mutex);
19237c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
19247c478bd9Sstevel@tonic-gate 	}
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 	/*
19277c478bd9Sstevel@tonic-gate 	 * Unregister the bus with the HPS.
19287c478bd9Sstevel@tonic-gate 	 *
19297c478bd9Sstevel@tonic-gate 	 * (Note: It is assumed that the HPS framework uninstalls
19307c478bd9Sstevel@tonic-gate 	 *  event handlers for all the hot plug slots on this bus.)
19317c478bd9Sstevel@tonic-gate 	 */
19327c478bd9Sstevel@tonic-gate 	(void) hpc_nexus_unregister_bus(dip);
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 	/* Free up any kmem_alloc'd memory for slot info table. */
19357c478bd9Sstevel@tonic-gate 	for (i = 0; i < PCI_MAX_DEVS; i++) {
19367c478bd9Sstevel@tonic-gate 		/* free up slot name strings */
19377c478bd9Sstevel@tonic-gate 		if (pcihp_p->slotinfo[i].name != NULL)
19387c478bd9Sstevel@tonic-gate 			kmem_free(pcihp_p->slotinfo[i].name,
193932d69156Sscarter 			    strlen(pcihp_p->slotinfo[i].name) + 1);
19407c478bd9Sstevel@tonic-gate 	}
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate 	/* destroy slot mutexes */
19437c478bd9Sstevel@tonic-gate 	for (i = 0; i < PCI_MAX_DEVS; i++)
19447c478bd9Sstevel@tonic-gate 		mutex_destroy(&pcihp_p->slotinfo[i].slot_mutex);
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 	/* free up the soft state structure */
19497c478bd9Sstevel@tonic-gate 	pcihp_destroy_soft_state(dip);
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 	/*
19527c478bd9Sstevel@tonic-gate 	 * Destroy resource maps for this bus node. (Note: This can
19537c478bd9Sstevel@tonic-gate 	 * be done from the detach(9E) of the nexus itself.)
19547c478bd9Sstevel@tonic-gate 	 */
19557c478bd9Sstevel@tonic-gate 	(void) pci_resource_destroy(dip);
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 	mutex_exit(&pcihp_open_mutex);
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
19607c478bd9Sstevel@tonic-gate }
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate /*
19637c478bd9Sstevel@tonic-gate  * pcihp_new_slot_state()
19647c478bd9Sstevel@tonic-gate  *
19657c478bd9Sstevel@tonic-gate  * This function is called by the HPS when it finds a hot plug
19667c478bd9Sstevel@tonic-gate  * slot is added or being removed from the hot plug framework.
19677c478bd9Sstevel@tonic-gate  * It returns 0 for success and HPC_ERR_FAILED for errors.
19687c478bd9Sstevel@tonic-gate  */
19697c478bd9Sstevel@tonic-gate static int
pcihp_new_slot_state(dev_info_t * dip,hpc_slot_t hdl,hpc_slot_info_t * slot_info,int slot_state)19707c478bd9Sstevel@tonic-gate pcihp_new_slot_state(dev_info_t *dip, hpc_slot_t hdl,
1971*d5ebc493SDan Cross     hpc_slot_info_t *slot_info, int slot_state)
19727c478bd9Sstevel@tonic-gate {
19737c478bd9Sstevel@tonic-gate 	pcihp_t *pcihp_p;
19747c478bd9Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
19757c478bd9Sstevel@tonic-gate 	int pci_dev;
19767c478bd9Sstevel@tonic-gate 	minor_t ap_minor;
19777c478bd9Sstevel@tonic-gate 	major_t ap_major;
19787c478bd9Sstevel@tonic-gate 	int rv = 0;
19797c478bd9Sstevel@tonic-gate 	time_t time;
19807c478bd9Sstevel@tonic-gate 	int auto_enable = 1;
19817c478bd9Sstevel@tonic-gate 	int rval;
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 	/* get a pointer to the soft state structure */
19847c478bd9Sstevel@tonic-gate 	pcihp_p = pcihp_get_soft_state(dip, PCIHP_DR_SLOT_ENTER, &rval);
19857c478bd9Sstevel@tonic-gate 	ASSERT(pcihp_p != NULL);
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate 	if (rval == PCIHP_FAILURE) {
19887c478bd9Sstevel@tonic-gate 		PCIHP_DEBUG((CE_WARN, "pcihp instance is unconfigured"
19897c478bd9Sstevel@tonic-gate 		    " while slot activity is requested\n"));
19907c478bd9Sstevel@tonic-gate 		return (HPC_ERR_FAILED);
19917c478bd9Sstevel@tonic-gate 	}
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 	pci_dev = slot_info->pci_dev_num;
19947c478bd9Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 	mutex_enter(&slotinfop->slot_mutex);
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 	switch (slot_state) {
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 	case HPC_SLOT_ONLINE:
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 		/*
20037c478bd9Sstevel@tonic-gate 		 * Make sure the slot is not already ONLINE (paranoia?).
20047c478bd9Sstevel@tonic-gate 		 * (Note: Should this be simply an ASSERTION?)
20057c478bd9Sstevel@tonic-gate 		 */
20067c478bd9Sstevel@tonic-gate 		if (slotinfop->slot_hdl != NULL) {
200732d69156Sscarter 			PCIHP_DEBUG((CE_WARN,
200832d69156Sscarter 			    "pcihp (%s%d): pci slot (dev %x) already ONLINE!!",
200932d69156Sscarter 			    ddi_driver_name(dip), ddi_get_instance(dip),
201032d69156Sscarter 			    pci_dev));
20117c478bd9Sstevel@tonic-gate 			rv = HPC_ERR_FAILED;
20127c478bd9Sstevel@tonic-gate 			break;
20137c478bd9Sstevel@tonic-gate 		}
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate 		/*
20167c478bd9Sstevel@tonic-gate 		 * Add the hot plug slot to the bus.
20177c478bd9Sstevel@tonic-gate 		 */
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate 		/* create the AP minor node */
20207c478bd9Sstevel@tonic-gate 		ap_minor = PCIHP_AP_MINOR_NUM(ddi_get_instance(dip), pci_dev);
20217c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, slot_info->pci_slot_name,
202232d69156Sscarter 		    S_IFCHR, ap_minor,
202332d69156Sscarter 		    DDI_NT_PCI_ATTACHMENT_POINT, 0) == DDI_FAILURE) {
202432d69156Sscarter 			cmn_err(CE_WARN,
202532d69156Sscarter 			    "pcihp (%s%d): ddi_create_minor_node failed"
202632d69156Sscarter 			    " for pci dev %x", ddi_driver_name(dip),
202732d69156Sscarter 			    ddi_get_instance(dip), pci_dev);
202832d69156Sscarter 			rv = HPC_ERR_FAILED;
202932d69156Sscarter 			break;
20307c478bd9Sstevel@tonic-gate 		}
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 		/* save the slot handle */
20337c478bd9Sstevel@tonic-gate 		slotinfop->slot_hdl = hdl;
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 		/* setup event handler for all hardware events on the slot */
20365c066ec2SJerry Gilliam 		ap_major = ddi_driver_major(dip);
20377c478bd9Sstevel@tonic-gate 		if (hpc_install_event_handler(hdl, -1, pcihp_event_handler,
203832d69156Sscarter 		    (caddr_t)makedevice(ap_major, ap_minor)) != 0) {
203932d69156Sscarter 			cmn_err(CE_WARN,
204032d69156Sscarter 			    "pcihp (%s%d): install event handler failed"
204132d69156Sscarter 			    " for pci dev %x", ddi_driver_name(dip),
204232d69156Sscarter 			    ddi_get_instance(dip), pci_dev);
204332d69156Sscarter 			rv = HPC_ERR_FAILED;
204432d69156Sscarter 			break;
20457c478bd9Sstevel@tonic-gate 		}
20467c478bd9Sstevel@tonic-gate 		slotinfop->event_mask = (uint32_t)0xFFFFFFFF;
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 		pcihp_create_occupant_props(dip, makedevice(ap_major,
20497c478bd9Sstevel@tonic-gate 		    ap_minor), pci_dev);
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 		/* set default auto configuration enabled flag for this slot */
20527c478bd9Sstevel@tonic-gate 		slotinfop->slot_flags = pcihp_autocfg_enabled;
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate 		/* copy the slot information */
20557c478bd9Sstevel@tonic-gate 		slotinfop->name =
20567c478bd9Sstevel@tonic-gate 		    kmem_alloc(strlen(slot_info->pci_slot_name) + 1, KM_SLEEP);
20577c478bd9Sstevel@tonic-gate 		(void) strcpy(slotinfop->name, slot_info->pci_slot_name);
20587c478bd9Sstevel@tonic-gate 		slotinfop->slot_type = slot_info->slot_type;
20597c478bd9Sstevel@tonic-gate 		slotinfop->hs_csr_location = 0;
20607c478bd9Sstevel@tonic-gate 		slotinfop->slot_capabilities = slot_info->pci_slot_capabilities;
20617c478bd9Sstevel@tonic-gate 		if (slot_info->slot_flags & HPC_SLOT_NO_AUTO_ENABLE)
20627c478bd9Sstevel@tonic-gate 			auto_enable = 0;
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 		if (slot_info->slot_flags & HPC_SLOT_CREATE_DEVLINK) {
20657c478bd9Sstevel@tonic-gate 			pci_devlink_flags |= (1 << pci_dev);
20667c478bd9Sstevel@tonic-gate 			(void) ddi_prop_update_int(DDI_DEV_T_NONE,
206732d69156Sscarter 			    dip, "ap-names", pci_devlink_flags);
20687c478bd9Sstevel@tonic-gate 		}
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE,
20717c478bd9Sstevel@tonic-gate 		    "pcihp (%s%d): pci slot (dev %x) ONLINE\n",
20727c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip), pci_dev));
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 		/*
20757c478bd9Sstevel@tonic-gate 		 * The slot may have an occupant that was configured
20767c478bd9Sstevel@tonic-gate 		 * at boot time. If we find a devinfo node in the tree
20777c478bd9Sstevel@tonic-gate 		 * for this slot (i.e pci device number) then we
20787c478bd9Sstevel@tonic-gate 		 * record the occupant state as CONFIGURED.
20797c478bd9Sstevel@tonic-gate 		 */
20807c478bd9Sstevel@tonic-gate 		if (pcihp_devi_find(dip, pci_dev, 0) != NULL) {
20817c478bd9Sstevel@tonic-gate 			/* we have a configured occupant */
20827c478bd9Sstevel@tonic-gate 			slotinfop->ostate = AP_OSTATE_CONFIGURED;
20837c478bd9Sstevel@tonic-gate 			slotinfop->rstate = AP_RSTATE_CONNECTED;
20847c478bd9Sstevel@tonic-gate 			slotinfop->condition = AP_COND_OK;
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
20877c478bd9Sstevel@tonic-gate 				/* this will set slot flags too. */
20887c478bd9Sstevel@tonic-gate 				(void) pcihp_get_board_type(slotinfop);
20897c478bd9Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
209032d69156Sscarter 				    HPC_EVENT_SLOT_CONFIGURE);
20917c478bd9Sstevel@tonic-gate 				if (pcihp_cpci_blue_led)
20927c478bd9Sstevel@tonic-gate 					pcihp_hs_csr_op(pcihp_p, pci_dev,
209332d69156Sscarter 					    HPC_EVENT_SLOT_BLUE_LED_OFF);
20947c478bd9Sstevel@tonic-gate 				/* ENUM# enabled by default for cPCI devices */
20957c478bd9Sstevel@tonic-gate 				slotinfop->slot_flags |= PCIHP_SLOT_AUTO_CFG_EN;
20967c478bd9Sstevel@tonic-gate 				slotinfop->slot_flags &=
209732d69156Sscarter 				    ~PCIHP_SLOT_ENUM_INS_PENDING;
20987c478bd9Sstevel@tonic-gate 			}
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 			/* tell HPC driver that the occupant is configured */
21017c478bd9Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
210232d69156Sscarter 			    HPC_CTRL_DEV_CONFIGURED, NULL);
2103daeea309Sarutz 
2104daeea309Sarutz 			/*
2105daeea309Sarutz 			 * Tell sysevent listeners that slot has
2106daeea309Sarutz 			 * changed state.  At minimum, this is useful
2107daeea309Sarutz 			 * when a PCI-E Chassis (containing Occupants) is
2108daeea309Sarutz 			 * hotplugged.  In this case, the following will
2109daeea309Sarutz 			 * announce that the Occupant in the Receptacle
2110daeea309Sarutz 			 * in the Chassis had a state-change.
2111daeea309Sarutz 			 */
2112daeea309Sarutz 			pcihp_gen_sysevent(slotinfop->name,
2113daeea309Sarutz 			    PCIHP_DR_AP_STATE_CHANGE, SE_NO_HINT,
2114daeea309Sarutz 			    pcihp_p->dip, KM_SLEEP);
21157c478bd9Sstevel@tonic-gate 		} else {
21167c478bd9Sstevel@tonic-gate 			struct pcihp_config_ctrl ctrl;
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate 			slotinfop->ostate = AP_OSTATE_UNCONFIGURED;
21197c478bd9Sstevel@tonic-gate 			slotinfop->rstate = AP_RSTATE_EMPTY;
21207c478bd9Sstevel@tonic-gate 			slotinfop->condition = AP_COND_UNKNOWN;
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 			if (!auto_enable) {	/* no further action */
21237c478bd9Sstevel@tonic-gate 				break;
21247c478bd9Sstevel@tonic-gate 			}
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 			/*
21277c478bd9Sstevel@tonic-gate 			 * We enable power to the slot and try to
21287c478bd9Sstevel@tonic-gate 			 * configure if there is any card present.
21297c478bd9Sstevel@tonic-gate 			 *
21307c478bd9Sstevel@tonic-gate 			 * Note: This case is possible if the BIOS or
21317c478bd9Sstevel@tonic-gate 			 * firmware doesn't enable the slots during
21327c478bd9Sstevel@tonic-gate 			 * soft reboot.
21337c478bd9Sstevel@tonic-gate 			 */
21347c478bd9Sstevel@tonic-gate 			if (hpc_nexus_connect(slotinfop->slot_hdl,
213532d69156Sscarter 			    NULL, 0) != HPC_SUCCESS)
21367c478bd9Sstevel@tonic-gate 				break;
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
21397c478bd9Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
214032d69156Sscarter 				    HPC_EVENT_SLOT_CONFIGURE);
21417c478bd9Sstevel@tonic-gate 				if (pcihp_cpci_blue_led)
21427c478bd9Sstevel@tonic-gate 					pcihp_hs_csr_op(pcihp_p, pci_dev,
214332d69156Sscarter 					    HPC_EVENT_SLOT_BLUE_LED_OFF);
21447c478bd9Sstevel@tonic-gate 				slotinfop->slot_flags |= PCIHP_SLOT_AUTO_CFG_EN;
21457c478bd9Sstevel@tonic-gate 				slotinfop->slot_flags &=
214632d69156Sscarter 				    ~PCIHP_SLOT_ENUM_INS_PENDING;
21477c478bd9Sstevel@tonic-gate 			}
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
215032d69156Sscarter 			    HPC_CTRL_DEV_CONFIG_START, NULL);
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate 			/*
21537c478bd9Sstevel@tonic-gate 			 * Call the configurator to configure the card.
21547c478bd9Sstevel@tonic-gate 			 */
215526947304SEvan Yan 			if (pcicfg_configure(dip, pci_dev, PCICFG_ALL_FUNC, 0)
215626947304SEvan Yan 			    != PCICFG_SUCCESS) {
21577c478bd9Sstevel@tonic-gate 				if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
21587c478bd9Sstevel@tonic-gate 					if (pcihp_cpci_blue_led)
21597c478bd9Sstevel@tonic-gate 						pcihp_hs_csr_op(pcihp_p,
216032d69156Sscarter 						    pci_dev,
216132d69156Sscarter 						    HPC_EVENT_SLOT_BLUE_LED_ON);
21627c478bd9Sstevel@tonic-gate 					pcihp_hs_csr_op(pcihp_p, pci_dev,
216332d69156Sscarter 					    HPC_EVENT_SLOT_UNCONFIGURE);
21647c478bd9Sstevel@tonic-gate 				}
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate 				/* tell HPC driver occupant configure Error */
21677c478bd9Sstevel@tonic-gate 				(void) hpc_nexus_control(slotinfop->slot_hdl,
216832d69156Sscarter 				    HPC_CTRL_DEV_CONFIG_FAILURE, NULL);
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate 				/*
21717c478bd9Sstevel@tonic-gate 				 * call HPC driver to turn off the power for
21727c478bd9Sstevel@tonic-gate 				 * the slot.
21737c478bd9Sstevel@tonic-gate 				 */
21747c478bd9Sstevel@tonic-gate 				(void) hpc_nexus_disconnect(slotinfop->slot_hdl,
217532d69156Sscarter 				    NULL, 0);
21767c478bd9Sstevel@tonic-gate 			} else {
217732d69156Sscarter 				/* record the occupant state as CONFIGURED */
217832d69156Sscarter 				slotinfop->ostate = AP_OSTATE_CONFIGURED;
217932d69156Sscarter 				slotinfop->rstate = AP_RSTATE_CONNECTED;
218032d69156Sscarter 				slotinfop->condition = AP_COND_OK;
218132d69156Sscarter 
218232d69156Sscarter 				/* now, online all the devices in the AP */
218332d69156Sscarter 				ctrl.flags = PCIHP_CFG_CONTINUE;
218432d69156Sscarter 				ctrl.rv = NDI_SUCCESS;
218532d69156Sscarter 				ctrl.dip = NULL;
218632d69156Sscarter 				ctrl.pci_dev = pci_dev;
218732d69156Sscarter 				ctrl.op = PCIHP_ONLINE;
21887c478bd9Sstevel@tonic-gate 				/*
21897c478bd9Sstevel@tonic-gate 				 * the following sets slot_flags and
21907c478bd9Sstevel@tonic-gate 				 * hs_csr_location too.
21917c478bd9Sstevel@tonic-gate 				 */
21927c478bd9Sstevel@tonic-gate 				(void) pcihp_get_board_type(slotinfop);
21937c478bd9Sstevel@tonic-gate 
21943fe80ca4SDan Cross 				ndi_devi_enter(dip);
219532d69156Sscarter 				ddi_walk_devs(ddi_get_child(dip),
219632d69156Sscarter 				    pcihp_configure, (void *)&ctrl);
21973fe80ca4SDan Cross 				ndi_devi_exit(dip);
219832d69156Sscarter 
219932d69156Sscarter 				if (ctrl.rv != NDI_SUCCESS) {
220032d69156Sscarter 					/*
220132d69156Sscarter 					 * one or more of the devices are not
220232d69156Sscarter 					 * ONLINE'd. How is this to be
220332d69156Sscarter 					 * reported?
220432d69156Sscarter 					 */
2205daeea309Sarutz 					cmn_err(CE_WARN,
2206daeea309Sarutz 					    "pcihp (%s%d): failed to attach"
2207daeea309Sarutz 					    " one or more drivers for the"
2208daeea309Sarutz 					    " card in the slot %s",
220932d69156Sscarter 					    ddi_driver_name(dip),
221032d69156Sscarter 					    ddi_get_instance(dip),
221132d69156Sscarter 					    slotinfop->name);
221232d69156Sscarter 				}
22137c478bd9Sstevel@tonic-gate 
2214daeea309Sarutz 				/* tell HPC driver the Occupant is Configured */
221532d69156Sscarter 				(void) hpc_nexus_control(slotinfop->slot_hdl,
221632d69156Sscarter 				    HPC_CTRL_DEV_CONFIGURED, NULL);
2217daeea309Sarutz 
2218daeea309Sarutz 				/*
2219daeea309Sarutz 				 * Tell sysevent listeners that slot has
2220daeea309Sarutz 				 * changed state.  At minimum, this is useful
2221daeea309Sarutz 				 * when a PCI-E Chassis (containing Occupants)
2222daeea309Sarutz 				 * is hotplugged.  In this case, the following
2223daeea309Sarutz 				 * will announce that the Occupant in the
2224daeea309Sarutz 				 * Receptacle in the Chassis had a state-change.
2225daeea309Sarutz 				 */
2226daeea309Sarutz 				pcihp_gen_sysevent(slotinfop->name,
2227daeea309Sarutz 				    PCIHP_DR_AP_STATE_CHANGE, SE_NO_HINT,
2228daeea309Sarutz 				    pcihp_p->dip, KM_SLEEP);
22297c478bd9Sstevel@tonic-gate 			}
22307c478bd9Sstevel@tonic-gate 		}
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate 		break;
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate 	case HPC_SLOT_OFFLINE:
22357c478bd9Sstevel@tonic-gate 		/*
22367c478bd9Sstevel@tonic-gate 		 * A hot plug slot is being removed from the bus.
22377c478bd9Sstevel@tonic-gate 		 * Make sure there is no occupant configured on the
22387c478bd9Sstevel@tonic-gate 		 * slot before removing the AP minor node.
22397c478bd9Sstevel@tonic-gate 		 */
22407c478bd9Sstevel@tonic-gate 		if (slotinfop->ostate != AP_OSTATE_UNCONFIGURED) {
224132d69156Sscarter 			cmn_err(CE_WARN, "pcihp (%s%d): Card is still in "
224232d69156Sscarter 			    "configured state for pci dev %x",
224332d69156Sscarter 			    ddi_driver_name(dip), ddi_get_instance(dip),
224432d69156Sscarter 			    pci_dev);
224532d69156Sscarter 			rv = HPC_ERR_FAILED;
224632d69156Sscarter 			break;
22477c478bd9Sstevel@tonic-gate 		}
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate 		/*
22507c478bd9Sstevel@tonic-gate 		 * If the AP device is in open state then return
22517c478bd9Sstevel@tonic-gate 		 * error.
22527c478bd9Sstevel@tonic-gate 		 */
22537c478bd9Sstevel@tonic-gate 		if (pcihp_p->soft_state != PCIHP_SOFT_STATE_CLOSED) {
225432d69156Sscarter 			rv = HPC_ERR_FAILED;
225532d69156Sscarter 			break;
22567c478bd9Sstevel@tonic-gate 		}
22577c478bd9Sstevel@tonic-gate 		if (slot_info->slot_flags & HPC_SLOT_CREATE_DEVLINK) {
22587c478bd9Sstevel@tonic-gate 			pci_devlink_flags &= ~(1 << pci_dev);
225932d69156Sscarter 			(void) ddi_prop_update_int(DDI_DEV_T_NONE, dip,
226032d69156Sscarter 			    "ap-names", pci_devlink_flags);
22617c478bd9Sstevel@tonic-gate 		}
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate 		/* remove the minor node */
22647c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, slotinfop->name);
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate 		/* free up the memory for the name string */
22677c478bd9Sstevel@tonic-gate 		kmem_free(slotinfop->name, strlen(slotinfop->name) + 1);
22687c478bd9Sstevel@tonic-gate 
22697c478bd9Sstevel@tonic-gate 		/* update the slot info data */
22707c478bd9Sstevel@tonic-gate 		slotinfop->name = NULL;
22717c478bd9Sstevel@tonic-gate 		slotinfop->slot_hdl = NULL;
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE,
22747c478bd9Sstevel@tonic-gate 		    "pcihp (%s%d): pci slot (dev %x) OFFLINE\n",
22757c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip),
22767c478bd9Sstevel@tonic-gate 		    slot_info->pci_dev_num));
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 		break;
22797c478bd9Sstevel@tonic-gate 	default:
22807c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
22817c478bd9Sstevel@tonic-gate 		    "pcihp_new_slot_state: unknown slot_state %d", slot_state);
22827c478bd9Sstevel@tonic-gate 		rv = HPC_ERR_FAILED;
22837c478bd9Sstevel@tonic-gate 	}
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 	if (rv == 0) {
22867c478bd9Sstevel@tonic-gate 		if (drv_getparm(TIME, (void *)&time) != DDI_SUCCESS)
22877c478bd9Sstevel@tonic-gate 			slotinfop->last_change = (time_t)-1;
22887c478bd9Sstevel@tonic-gate 		else
22897c478bd9Sstevel@tonic-gate 			slotinfop->last_change = (time32_t)time;
22907c478bd9Sstevel@tonic-gate 	}
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 	mutex_exit(&slotinfop->slot_mutex);
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 	(void) pcihp_get_soft_state(dip, PCIHP_DR_SLOT_EXIT, &rval);
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 	return (rv);
22977c478bd9Sstevel@tonic-gate }
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate /*
23007c478bd9Sstevel@tonic-gate  * Event handler. It is assumed that this function is called from
23017c478bd9Sstevel@tonic-gate  * a kernel context only.
23027c478bd9Sstevel@tonic-gate  *
23037c478bd9Sstevel@tonic-gate  * Parameters:
23047c478bd9Sstevel@tonic-gate  *	slot_arg	AP minor number.
23057c478bd9Sstevel@tonic-gate  *	event_mask	Event that occurred.
23067c478bd9Sstevel@tonic-gate  */
23077c478bd9Sstevel@tonic-gate 
23087c478bd9Sstevel@tonic-gate static int
pcihp_event_handler(caddr_t slot_arg,uint_t event_mask)23097c478bd9Sstevel@tonic-gate pcihp_event_handler(caddr_t slot_arg, uint_t event_mask)
23107c478bd9Sstevel@tonic-gate {
23117c478bd9Sstevel@tonic-gate 	dev_t ap_dev = (dev_t)slot_arg;
23127c478bd9Sstevel@tonic-gate 	dev_info_t *self;
23137c478bd9Sstevel@tonic-gate 	pcihp_t *pcihp_p;
23147c478bd9Sstevel@tonic-gate 	int pci_dev;
23157c478bd9Sstevel@tonic-gate 	int rv = HPC_EVENT_CLAIMED;
23167c478bd9Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
23177c478bd9Sstevel@tonic-gate 	struct pcihp_config_ctrl ctrl;
23187c478bd9Sstevel@tonic-gate 	int rval;
231970025d76Sjohnny 	int hint;
232070025d76Sjohnny 	hpc_slot_state_t rstate;
23214eacc763Sjj 	struct hpc_led_info led_info;
23227c478bd9Sstevel@tonic-gate 
23237c478bd9Sstevel@tonic-gate 	/*
23247c478bd9Sstevel@tonic-gate 	 * Get the soft state structure.
23257c478bd9Sstevel@tonic-gate 	 */
23267c478bd9Sstevel@tonic-gate 	if (pcihp_info(NULL, DDI_INFO_DEVT2DEVINFO, (void *)ap_dev,
23277c478bd9Sstevel@tonic-gate 	    (void **)&self) != DDI_SUCCESS)
23287c478bd9Sstevel@tonic-gate 		return (ENXIO);
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 	pcihp_p = pcihp_get_soft_state(self, PCIHP_DR_SLOT_ENTER, &rval);
23317c478bd9Sstevel@tonic-gate 	ASSERT(pcihp_p != NULL);
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 	if (rval == PCIHP_FAILURE) {
23347c478bd9Sstevel@tonic-gate 		PCIHP_DEBUG((CE_WARN, "pcihp instance is unconfigured"
23357c478bd9Sstevel@tonic-gate 		    " while slot activity is requested\n"));
23367c478bd9Sstevel@tonic-gate 		return (-1);
23377c478bd9Sstevel@tonic-gate 	}
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 	/* get the PCI device number for the slot */
23407c478bd9Sstevel@tonic-gate 	pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(getminor(ap_dev));
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
23437c478bd9Sstevel@tonic-gate 
23447c478bd9Sstevel@tonic-gate 	/*
23457c478bd9Sstevel@tonic-gate 	 * All the events that may be handled in interrupt context should be
23467c478bd9Sstevel@tonic-gate 	 * free of any mutex usage.
23477c478bd9Sstevel@tonic-gate 	 */
23487c478bd9Sstevel@tonic-gate 	switch (event_mask) {
23497c478bd9Sstevel@tonic-gate 
23507c478bd9Sstevel@tonic-gate 	case HPC_EVENT_CLEAR_ENUM:
23517c478bd9Sstevel@tonic-gate 		/*
23527c478bd9Sstevel@tonic-gate 		 * Check and clear ENUM# interrupt status. This may be
23537c478bd9Sstevel@tonic-gate 		 * called by the Hotswap controller driver when it is
23547c478bd9Sstevel@tonic-gate 		 * operating in a full hotswap system where the
23557c478bd9Sstevel@tonic-gate 		 * platform may not have control on globally disabling ENUM#.
23567c478bd9Sstevel@tonic-gate 		 * In such cases, the intent is to clear interrupt and
23577c478bd9Sstevel@tonic-gate 		 * process the interrupt in non-interrupt context.
23587c478bd9Sstevel@tonic-gate 		 * This is the first part of the ENUM# event processing.
23597c478bd9Sstevel@tonic-gate 		 */
23607c478bd9Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): ENUM# is generated"
23617c478bd9Sstevel@tonic-gate 		    " on the bus (for slot %s ?)",
23627c478bd9Sstevel@tonic-gate 		    ddi_driver_name(pcihp_p->dip),
23637c478bd9Sstevel@tonic-gate 		    ddi_get_instance(pcihp_p->dip), slotinfop->name));
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 		/* this is the only event coming through in interrupt context */
23667c478bd9Sstevel@tonic-gate 		rv = pcihp_handle_enum(pcihp_p, pci_dev, PCIHP_CLEAR_ENUM,
23677c478bd9Sstevel@tonic-gate 		    KM_NOSLEEP);
23687c478bd9Sstevel@tonic-gate 
23697c478bd9Sstevel@tonic-gate 		(void) pcihp_get_soft_state(self, PCIHP_DR_SLOT_EXIT, &rval);
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 		return (rv);
23727c478bd9Sstevel@tonic-gate 	default:
23737c478bd9Sstevel@tonic-gate 		break;
23747c478bd9Sstevel@tonic-gate 	}
23757c478bd9Sstevel@tonic-gate 
23767c478bd9Sstevel@tonic-gate 	mutex_enter(&slotinfop->slot_mutex);
23777c478bd9Sstevel@tonic-gate 
237870025d76Sjohnny 	if (hpc_nexus_control(slotinfop->slot_hdl,
237970025d76Sjohnny 	    HPC_CTRL_GET_SLOT_STATE, (caddr_t)&rstate) != 0)
238070025d76Sjohnny 		rv = HPC_ERR_FAILED;
238170025d76Sjohnny 
238270025d76Sjohnny 	slotinfop->rstate = (ap_rstate_t)rstate;
238370025d76Sjohnny 
23847c478bd9Sstevel@tonic-gate 	switch (event_mask) {
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate 	case HPC_EVENT_SLOT_INSERTION:
23877c478bd9Sstevel@tonic-gate 		/*
23887c478bd9Sstevel@tonic-gate 		 * A card is inserted in the slot. Just report this
23897c478bd9Sstevel@tonic-gate 		 * event and return.
23907c478bd9Sstevel@tonic-gate 		 */
23917c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "pcihp (%s%d): card is inserted"
239232d69156Sscarter 		    " in the slot %s (pci dev %x)",
239332d69156Sscarter 		    ddi_driver_name(pcihp_p->dip),
239432d69156Sscarter 		    ddi_get_instance(pcihp_p->dip),
239532d69156Sscarter 		    slotinfop->name, pci_dev);
23967c478bd9Sstevel@tonic-gate 
23977c478bd9Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate 		break;
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate 	case HPC_EVENT_SLOT_CONFIGURE:
24027c478bd9Sstevel@tonic-gate 		/*
24037c478bd9Sstevel@tonic-gate 		 * Configure the occupant that is just inserted in the slot.
24047c478bd9Sstevel@tonic-gate 		 * The receptacle may or may not be in the connected state. If
24057c478bd9Sstevel@tonic-gate 		 * the receptacle is not connected and the auto configuration
24067c478bd9Sstevel@tonic-gate 		 * is enabled on this slot then connect the slot. If auto
24077c478bd9Sstevel@tonic-gate 		 * configuration is enabled then configure the card.
24087c478bd9Sstevel@tonic-gate 		 */
24097c478bd9Sstevel@tonic-gate 		if ((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) == 0) {
24107c478bd9Sstevel@tonic-gate 			/*
24117c478bd9Sstevel@tonic-gate 			 * auto configuration is disabled. Tell someone
24127c478bd9Sstevel@tonic-gate 			 * like RCM about this hotplug event?
24137c478bd9Sstevel@tonic-gate 			 */
24147c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "pcihp (%s%d): SLOT_CONFIGURE event"
241532d69156Sscarter 			    " occurred for pci dev %x (slot %s),"
241632d69156Sscarter 			    " Slot disabled for auto-configuration.",
241732d69156Sscarter 			    ddi_driver_name(pcihp_p->dip),
241832d69156Sscarter 			    ddi_get_instance(pcihp_p->dip), pci_dev,
241932d69156Sscarter 			    slotinfop->name);
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate 			/* +++ HOOK for RCM to report this hotplug event? +++ */
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate 			break;
24247c478bd9Sstevel@tonic-gate 		}
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate 		if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {
24277c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "pcihp (%s%d): SLOT_CONFIGURE event"
242832d69156Sscarter 			    " re-occurred for pci dev %x (slot %s),",
242932d69156Sscarter 			    ddi_driver_name(pcihp_p->dip),
243032d69156Sscarter 			    ddi_get_instance(pcihp_p->dip), pci_dev,
243132d69156Sscarter 			    slotinfop->name);
24327c478bd9Sstevel@tonic-gate 			mutex_exit(&slotinfop->slot_mutex);
24337c478bd9Sstevel@tonic-gate 
24347c478bd9Sstevel@tonic-gate 			(void) pcihp_get_soft_state(self, PCIHP_DR_SLOT_EXIT,
24357c478bd9Sstevel@tonic-gate 			    &rval);
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 			return (EAGAIN);
24387c478bd9Sstevel@tonic-gate 		}
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate 		/*
24417c478bd9Sstevel@tonic-gate 		 * Auto configuration is enabled. First, make sure the
24427c478bd9Sstevel@tonic-gate 		 * receptacle is in the CONNECTED state.
24437c478bd9Sstevel@tonic-gate 		 */
24447c478bd9Sstevel@tonic-gate 		if ((rv = hpc_nexus_connect(slotinfop->slot_hdl,
24457c478bd9Sstevel@tonic-gate 		    NULL, 0)) == HPC_SUCCESS) {
244632d69156Sscarter 			/* record rstate */
244732d69156Sscarter 			slotinfop->rstate = AP_RSTATE_CONNECTED;
24487c478bd9Sstevel@tonic-gate 		}
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate 		/* Clear INS and Turn LED Off and start configuring. */
24517c478bd9Sstevel@tonic-gate 		if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
24527c478bd9Sstevel@tonic-gate 			pcihp_hs_csr_op(pcihp_p, pci_dev,
245332d69156Sscarter 			    HPC_EVENT_SLOT_CONFIGURE);
24547c478bd9Sstevel@tonic-gate 			if (pcihp_cpci_blue_led)
24557c478bd9Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
245632d69156Sscarter 				    HPC_EVENT_SLOT_BLUE_LED_OFF);
24577c478bd9Sstevel@tonic-gate 		}
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate 		(void) hpc_nexus_control(slotinfop->slot_hdl,
246032d69156Sscarter 		    HPC_CTRL_DEV_CONFIG_START, NULL);
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate 		/*
24637c478bd9Sstevel@tonic-gate 		 * Call the configurator to configure the card.
24647c478bd9Sstevel@tonic-gate 		 */
246526947304SEvan Yan 		if (pcicfg_configure(pcihp_p->dip, pci_dev, PCICFG_ALL_FUNC, 0)
246626947304SEvan Yan 		    != PCICFG_SUCCESS) {
24677c478bd9Sstevel@tonic-gate 			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
24687c478bd9Sstevel@tonic-gate 				if (pcihp_cpci_blue_led)
24697c478bd9Sstevel@tonic-gate 					pcihp_hs_csr_op(pcihp_p, pci_dev,
247032d69156Sscarter 					    HPC_EVENT_SLOT_BLUE_LED_ON);
24717c478bd9Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
247232d69156Sscarter 				    HPC_EVENT_SLOT_UNCONFIGURE);
24737c478bd9Sstevel@tonic-gate 			}
24747c478bd9Sstevel@tonic-gate 			/* failed to configure the card */
24757c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "pcihp (%s%d): failed to configure"
247632d69156Sscarter 			    " the card in the slot %s",
247732d69156Sscarter 			    ddi_driver_name(pcihp_p->dip),
247832d69156Sscarter 			    ddi_get_instance(pcihp_p->dip),
247932d69156Sscarter 			    slotinfop->name);
24807c478bd9Sstevel@tonic-gate 			/* failed to configure; disconnect the slot */
24817c478bd9Sstevel@tonic-gate 			if (hpc_nexus_disconnect(slotinfop->slot_hdl,
24827c478bd9Sstevel@tonic-gate 			    NULL, 0) == HPC_SUCCESS) {
248332d69156Sscarter 				slotinfop->rstate = AP_RSTATE_DISCONNECTED;
24847c478bd9Sstevel@tonic-gate 			}
24857c478bd9Sstevel@tonic-gate 
24867c478bd9Sstevel@tonic-gate 			/* tell HPC driver occupant configure Error */
24877c478bd9Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
248832d69156Sscarter 			    HPC_CTRL_DEV_CONFIG_FAILURE, NULL);
24897c478bd9Sstevel@tonic-gate 		} else {
24907c478bd9Sstevel@tonic-gate 			/* record the occupant state as CONFIGURED */
24917c478bd9Sstevel@tonic-gate 			slotinfop->ostate = AP_OSTATE_CONFIGURED;
24927c478bd9Sstevel@tonic-gate 			slotinfop->condition = AP_COND_OK;
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate 			/* now, online all the devices in the AP */
24957c478bd9Sstevel@tonic-gate 			ctrl.flags = PCIHP_CFG_CONTINUE;
24967c478bd9Sstevel@tonic-gate 			ctrl.rv = NDI_SUCCESS;
24977c478bd9Sstevel@tonic-gate 			ctrl.dip = NULL;
24987c478bd9Sstevel@tonic-gate 			ctrl.pci_dev = pci_dev;
24997c478bd9Sstevel@tonic-gate 			ctrl.op = PCIHP_ONLINE;
2500*d5ebc493SDan Cross 			(void) pcihp_get_board_type(slotinfop);
25017c478bd9Sstevel@tonic-gate 
25023fe80ca4SDan Cross 			ndi_devi_enter(pcihp_p->dip);
25037c478bd9Sstevel@tonic-gate 			ddi_walk_devs(ddi_get_child(pcihp_p->dip),
250432d69156Sscarter 			    pcihp_configure, (void *)&ctrl);
25053fe80ca4SDan Cross 			ndi_devi_exit(pcihp_p->dip);
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 			if (ctrl.rv != NDI_SUCCESS) {
25087c478bd9Sstevel@tonic-gate 				/*
25097c478bd9Sstevel@tonic-gate 				 * one or more of the devices are not
25107c478bd9Sstevel@tonic-gate 				 * ONLINE'd. How is this to be
25117c478bd9Sstevel@tonic-gate 				 * reported?
25127c478bd9Sstevel@tonic-gate 				 */
25137c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
251432d69156Sscarter 				    "pcihp (%s%d): failed to attach one or"
251532d69156Sscarter 				    " more drivers for the card in"
251632d69156Sscarter 				    " the slot %s",
251732d69156Sscarter 				    ddi_driver_name(pcihp_p->dip),
251832d69156Sscarter 				    ddi_get_instance(pcihp_p->dip),
251932d69156Sscarter 				    slotinfop->name);
25207c478bd9Sstevel@tonic-gate 			}
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate 			/* tell HPC driver that the occupant is configured */
25237c478bd9Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
252432d69156Sscarter 			    HPC_CTRL_DEV_CONFIGURED, NULL);
25257c478bd9Sstevel@tonic-gate 
25267c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "pcihp (%s%d): card is CONFIGURED"
252732d69156Sscarter 			    " in the slot %s (pci dev %x)",
252832d69156Sscarter 			    ddi_driver_name(pcihp_p->dip),
252932d69156Sscarter 			    ddi_get_instance(pcihp_p->dip),
253032d69156Sscarter 			    slotinfop->name, pci_dev);
25317c478bd9Sstevel@tonic-gate 		}
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate 		break;
25347c478bd9Sstevel@tonic-gate 
25357c478bd9Sstevel@tonic-gate 	case HPC_EVENT_SLOT_UNCONFIGURE:
25367c478bd9Sstevel@tonic-gate 		/*
25377c478bd9Sstevel@tonic-gate 		 * Unconfigure the occupant in this slot.
25387c478bd9Sstevel@tonic-gate 		 */
25397c478bd9Sstevel@tonic-gate 		if ((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) == 0) {
25407c478bd9Sstevel@tonic-gate 			/*
25417c478bd9Sstevel@tonic-gate 			 * auto configuration is disabled. Tell someone
25427c478bd9Sstevel@tonic-gate 			 * like RCM about this hotplug event?
25437c478bd9Sstevel@tonic-gate 			 */
25447c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "pcihp (%s%d): SLOT_UNCONFIGURE event"
254532d69156Sscarter 			    " for pci dev %x (slot %s) ignored,"
254632d69156Sscarter 			    " Slot disabled for auto-configuration.",
254732d69156Sscarter 			    ddi_driver_name(pcihp_p->dip),
254832d69156Sscarter 			    ddi_get_instance(pcihp_p->dip), pci_dev,
254932d69156Sscarter 			    slotinfop->name);
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate 			/* +++ HOOK for RCM to report this hotplug event? +++ */
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate 			break;
25547c478bd9Sstevel@tonic-gate 		}
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate 		if (slotinfop->ostate == AP_OSTATE_UNCONFIGURED) {
25577c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "pcihp (%s%d): SLOT_UNCONFIGURE "
255832d69156Sscarter 			    "event re-occurred for pci dev %x (slot %s),",
255932d69156Sscarter 			    ddi_driver_name(pcihp_p->dip),
256032d69156Sscarter 			    ddi_get_instance(pcihp_p->dip), pci_dev,
256132d69156Sscarter 			    slotinfop->name);
25627c478bd9Sstevel@tonic-gate 			mutex_exit(&slotinfop->slot_mutex);
25637c478bd9Sstevel@tonic-gate 
25647c478bd9Sstevel@tonic-gate 			(void) pcihp_get_soft_state(self, PCIHP_DR_SLOT_EXIT,
25657c478bd9Sstevel@tonic-gate 			    &rval);
25667c478bd9Sstevel@tonic-gate 
25677c478bd9Sstevel@tonic-gate 			return (EAGAIN);
25687c478bd9Sstevel@tonic-gate 		}
25697c478bd9Sstevel@tonic-gate 		/*
25707c478bd9Sstevel@tonic-gate 		 * If the occupant is in the CONFIGURED state then
25717c478bd9Sstevel@tonic-gate 		 * call the configurator to unconfigure the slot.
25727c478bd9Sstevel@tonic-gate 		 */
25737c478bd9Sstevel@tonic-gate 		if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {
25747c478bd9Sstevel@tonic-gate 			/*
25757c478bd9Sstevel@tonic-gate 			 * Detach all the drivers for the devices in the
25767c478bd9Sstevel@tonic-gate 			 * slot. Call pcihp_configure() to offline the
25777c478bd9Sstevel@tonic-gate 			 * devices.
25787c478bd9Sstevel@tonic-gate 			 */
25797c478bd9Sstevel@tonic-gate 			ctrl.flags = 0;
25807c478bd9Sstevel@tonic-gate 			ctrl.rv = NDI_SUCCESS;
25817c478bd9Sstevel@tonic-gate 			ctrl.dip = NULL;
25827c478bd9Sstevel@tonic-gate 			ctrl.pci_dev = pci_dev;
25837c478bd9Sstevel@tonic-gate 			ctrl.op = PCIHP_OFFLINE;
25847c478bd9Sstevel@tonic-gate 
25857c478bd9Sstevel@tonic-gate 			(void) devfs_clean(pcihp_p->dip, NULL, DV_CLEAN_FORCE);
25863fe80ca4SDan Cross 			ndi_devi_enter(pcihp_p->dip);
25877c478bd9Sstevel@tonic-gate 			ddi_walk_devs(ddi_get_child(pcihp_p->dip),
258832d69156Sscarter 			    pcihp_configure, (void *)&ctrl);
25893fe80ca4SDan Cross 			ndi_devi_exit(pcihp_p->dip);
25907c478bd9Sstevel@tonic-gate 
25917c478bd9Sstevel@tonic-gate 			if (ctrl.rv != NDI_SUCCESS) {
25927c478bd9Sstevel@tonic-gate 				/*
25937c478bd9Sstevel@tonic-gate 				 * Failed to detach one or more drivers.
25947c478bd9Sstevel@tonic-gate 				 * Restore the status for the drivers
25957c478bd9Sstevel@tonic-gate 				 * which are offlined during this step.
25967c478bd9Sstevel@tonic-gate 				 */
25977c478bd9Sstevel@tonic-gate 				ctrl.flags = PCIHP_CFG_CONTINUE;
25987c478bd9Sstevel@tonic-gate 				ctrl.rv = NDI_SUCCESS;
25997c478bd9Sstevel@tonic-gate 				ctrl.dip = NULL;
26007c478bd9Sstevel@tonic-gate 				ctrl.pci_dev = pci_dev;
26017c478bd9Sstevel@tonic-gate 				ctrl.op = PCIHP_ONLINE;
26027c478bd9Sstevel@tonic-gate 
26033fe80ca4SDan Cross 				ndi_devi_enter(pcihp_p->dip);
26047c478bd9Sstevel@tonic-gate 				ddi_walk_devs(ddi_get_child(pcihp_p->dip),
260532d69156Sscarter 				    pcihp_configure, (void *)&ctrl);
26063fe80ca4SDan Cross 				ndi_devi_exit(pcihp_p->dip);
26077c478bd9Sstevel@tonic-gate 				rv = HPC_ERR_FAILED;
26087c478bd9Sstevel@tonic-gate 			} else {
26097c478bd9Sstevel@tonic-gate 				(void) hpc_nexus_control(slotinfop->slot_hdl,
261032d69156Sscarter 				    HPC_CTRL_DEV_UNCONFIG_START, NULL);
26117c478bd9Sstevel@tonic-gate 
261226947304SEvan Yan 				if (pcicfg_unconfigure(pcihp_p->dip, pci_dev,
261326947304SEvan Yan 				    PCICFG_ALL_FUNC, 0) == PCICFG_SUCCESS) {
26147c478bd9Sstevel@tonic-gate 
26157c478bd9Sstevel@tonic-gate 				/* Resources freed. Turn LED on. Clear EXT. */
26167c478bd9Sstevel@tonic-gate 				if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
26177c478bd9Sstevel@tonic-gate 					if (pcihp_cpci_blue_led)
26187c478bd9Sstevel@tonic-gate 						pcihp_hs_csr_op(pcihp_p,
261932d69156Sscarter 						    pci_dev,
262032d69156Sscarter 						    HPC_EVENT_SLOT_BLUE_LED_ON);
26217c478bd9Sstevel@tonic-gate 					pcihp_hs_csr_op(pcihp_p, pci_dev,
262232d69156Sscarter 					    HPC_EVENT_SLOT_UNCONFIGURE);
26237c478bd9Sstevel@tonic-gate 					slotinfop->hs_csr_location = 0;
26247c478bd9Sstevel@tonic-gate 					slotinfop->slot_flags &=
262532d69156Sscarter 					    ~PCIHP_SLOT_DEV_NON_HOTPLUG;
26267c478bd9Sstevel@tonic-gate 				}
26277c478bd9Sstevel@tonic-gate 					slotinfop->ostate =
262832d69156Sscarter 					    AP_OSTATE_UNCONFIGURED;
26297c478bd9Sstevel@tonic-gate 					slotinfop->condition = AP_COND_UNKNOWN;
26307c478bd9Sstevel@tonic-gate 					/*
26317c478bd9Sstevel@tonic-gate 					 * send the notification of state change
26327c478bd9Sstevel@tonic-gate 					 * to the HPC driver.
26337c478bd9Sstevel@tonic-gate 					 */
26347c478bd9Sstevel@tonic-gate 					(void) hpc_nexus_control(
263532d69156Sscarter 					    slotinfop->slot_hdl,
263632d69156Sscarter 					    HPC_CTRL_DEV_UNCONFIGURED, NULL);
26377c478bd9Sstevel@tonic-gate 					/* disconnect the slot */
26387c478bd9Sstevel@tonic-gate 					if (hpc_nexus_disconnect(
263932d69156Sscarter 					    slotinfop->slot_hdl,
264032d69156Sscarter 					    NULL, 0) == HPC_SUCCESS) {
264132d69156Sscarter 						slotinfop->rstate =
264232d69156Sscarter 						    AP_RSTATE_DISCONNECTED;
26437c478bd9Sstevel@tonic-gate 					}
26447c478bd9Sstevel@tonic-gate 
26457c478bd9Sstevel@tonic-gate 					cmn_err(CE_NOTE,
264632d69156Sscarter 					    "pcihp (%s%d): card is UNCONFIGURED"
264732d69156Sscarter 					    " in the slot %s (pci dev %x)",
264832d69156Sscarter 					    ddi_driver_name(pcihp_p->dip),
264932d69156Sscarter 					    ddi_get_instance(pcihp_p->dip),
265032d69156Sscarter 					    slotinfop->name, pci_dev);
26517c478bd9Sstevel@tonic-gate 				} else {
26527c478bd9Sstevel@tonic-gate 					/* tell HPC driver occupant is Busy */
26537c478bd9Sstevel@tonic-gate 					(void) hpc_nexus_control(
265432d69156Sscarter 					    slotinfop->slot_hdl,
265532d69156Sscarter 					    HPC_CTRL_DEV_UNCONFIG_FAILURE,
265632d69156Sscarter 					    NULL);
26577c478bd9Sstevel@tonic-gate 
26587c478bd9Sstevel@tonic-gate 					rv = HPC_ERR_FAILED;
26597c478bd9Sstevel@tonic-gate 				}
26607c478bd9Sstevel@tonic-gate 			}
26617c478bd9Sstevel@tonic-gate 		}
26627c478bd9Sstevel@tonic-gate 
26637c478bd9Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
26647c478bd9Sstevel@tonic-gate 
26657c478bd9Sstevel@tonic-gate 		break;
26667c478bd9Sstevel@tonic-gate 
26677c478bd9Sstevel@tonic-gate 	case HPC_EVENT_SLOT_REMOVAL:
26687c478bd9Sstevel@tonic-gate 		/*
26697c478bd9Sstevel@tonic-gate 		 * Card is removed from the slot. The card must have been
26707c478bd9Sstevel@tonic-gate 		 * unconfigured before this event.
26717c478bd9Sstevel@tonic-gate 		 */
26727c478bd9Sstevel@tonic-gate 		if (slotinfop->ostate != AP_OSTATE_UNCONFIGURED) {
267370025d76Sjohnny 			slotinfop->condition = AP_COND_FAILED;
267470025d76Sjohnny 			cmn_err(CE_WARN, "pcihp (%s%d): card is removed"
267570025d76Sjohnny 			    " from the slot %s",
26767c478bd9Sstevel@tonic-gate 			    ddi_driver_name(pcihp_p->dip),
26777c478bd9Sstevel@tonic-gate 			    ddi_get_instance(pcihp_p->dip),
26787c478bd9Sstevel@tonic-gate 			    slotinfop->name);
2679f94c6026Sjj 		} else {
2680f94c6026Sjj 			slotinfop->condition = AP_COND_UNKNOWN;
268170025d76Sjohnny 			cmn_err(CE_NOTE, "pcihp (%s%d): card is removed"
268270025d76Sjohnny 			    " from the slot %s",
268370025d76Sjohnny 			    ddi_driver_name(pcihp_p->dip),
268470025d76Sjohnny 			    ddi_get_instance(pcihp_p->dip),
268570025d76Sjohnny 			    slotinfop->name);
2686f94c6026Sjj 		}
26877c478bd9Sstevel@tonic-gate 
268870025d76Sjohnny 		slotinfop->rstate = AP_RSTATE_EMPTY;
26897c478bd9Sstevel@tonic-gate 
26907c478bd9Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
26917c478bd9Sstevel@tonic-gate 
26927c478bd9Sstevel@tonic-gate 		break;
26937c478bd9Sstevel@tonic-gate 
26947c478bd9Sstevel@tonic-gate 	case HPC_EVENT_SLOT_POWER_ON:
26957c478bd9Sstevel@tonic-gate 		/*
26967c478bd9Sstevel@tonic-gate 		 * Slot is connected to the bus. i.e the card is powered
26977c478bd9Sstevel@tonic-gate 		 * on. Are there any error conditions to be checked?
26987c478bd9Sstevel@tonic-gate 		 */
26997c478bd9Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): card is powered"
270032d69156Sscarter 		    " on in the slot %s",
270132d69156Sscarter 		    ddi_driver_name(pcihp_p->dip),
270232d69156Sscarter 		    ddi_get_instance(pcihp_p->dip),
270332d69156Sscarter 		    slotinfop->name));
27047c478bd9Sstevel@tonic-gate 
27057c478bd9Sstevel@tonic-gate 		slotinfop->rstate = AP_RSTATE_CONNECTED; /* record rstate */
27067c478bd9Sstevel@tonic-gate 
27077c478bd9Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
27087c478bd9Sstevel@tonic-gate 
27097c478bd9Sstevel@tonic-gate 		break;
27107c478bd9Sstevel@tonic-gate 
27117c478bd9Sstevel@tonic-gate 	case HPC_EVENT_SLOT_POWER_OFF:
27127c478bd9Sstevel@tonic-gate 		/*
27137c478bd9Sstevel@tonic-gate 		 * Slot is disconnected from the bus. i.e the card is powered
27147c478bd9Sstevel@tonic-gate 		 * off. Are there any error conditions to be checked?
27157c478bd9Sstevel@tonic-gate 		 */
27167c478bd9Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): card is powered"
271732d69156Sscarter 		    " off in the slot %s",
271832d69156Sscarter 		    ddi_driver_name(pcihp_p->dip),
271932d69156Sscarter 		    ddi_get_instance(pcihp_p->dip),
272032d69156Sscarter 		    slotinfop->name));
27217c478bd9Sstevel@tonic-gate 
27227c478bd9Sstevel@tonic-gate 		slotinfop->rstate = AP_RSTATE_DISCONNECTED; /* record rstate */
27237c478bd9Sstevel@tonic-gate 
27247c478bd9Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
27257c478bd9Sstevel@tonic-gate 
27267c478bd9Sstevel@tonic-gate 		break;
27277c478bd9Sstevel@tonic-gate 
27287c478bd9Sstevel@tonic-gate 	case HPC_EVENT_SLOT_LATCH_SHUT:
27297c478bd9Sstevel@tonic-gate 		/*
27307c478bd9Sstevel@tonic-gate 		 * Latch on the slot is closed.
27317c478bd9Sstevel@tonic-gate 		 */
273232d69156Sscarter 		cmn_err(CE_NOTE, "pcihp (%s%d): latch is shut for the slot %s",
273332d69156Sscarter 		    ddi_driver_name(pcihp_p->dip),
273432d69156Sscarter 		    ddi_get_instance(pcihp_p->dip),
273532d69156Sscarter 		    slotinfop->name);
27367c478bd9Sstevel@tonic-gate 
27377c478bd9Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
27387c478bd9Sstevel@tonic-gate 
27397c478bd9Sstevel@tonic-gate 		break;
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 	case HPC_EVENT_SLOT_LATCH_OPEN:
27427c478bd9Sstevel@tonic-gate 		/*
27437c478bd9Sstevel@tonic-gate 		 * Latch on the slot is open.
27447c478bd9Sstevel@tonic-gate 		 */
274532d69156Sscarter 		cmn_err(CE_NOTE, "pcihp (%s%d): latch is open for the slot %s",
274632d69156Sscarter 		    ddi_driver_name(pcihp_p->dip),
274732d69156Sscarter 		    ddi_get_instance(pcihp_p->dip),
274832d69156Sscarter 		    slotinfop->name);
27497c478bd9Sstevel@tonic-gate 
27507c478bd9Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
27517c478bd9Sstevel@tonic-gate 
27527c478bd9Sstevel@tonic-gate 		break;
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate 	case HPC_EVENT_PROCESS_ENUM:
27557c478bd9Sstevel@tonic-gate 		/*
27567c478bd9Sstevel@tonic-gate 		 * HSC knows the device number of the slot where the
27577c478bd9Sstevel@tonic-gate 		 * ENUM# was triggered.
27587c478bd9Sstevel@tonic-gate 		 * Now finish the necessary actions to be taken on that
27597c478bd9Sstevel@tonic-gate 		 * slot. Please note that the interrupt is already cleared.
27607c478bd9Sstevel@tonic-gate 		 * This is the second(last) part of the ENUM# event processing.
27617c478bd9Sstevel@tonic-gate 		 */
27627c478bd9Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): processing ENUM#"
276332d69156Sscarter 		    " for slot %s",
276432d69156Sscarter 		    ddi_driver_name(pcihp_p->dip),
276532d69156Sscarter 		    ddi_get_instance(pcihp_p->dip),
276632d69156Sscarter 		    slotinfop->name));
27677c478bd9Sstevel@tonic-gate 
27687c478bd9Sstevel@tonic-gate 		mutex_exit(&slotinfop->slot_mutex);
27697c478bd9Sstevel@tonic-gate 		rv = pcihp_enum_slot(pcihp_p, slotinfop, pci_dev,
277032d69156Sscarter 		    PCIHP_HANDLE_ENUM, KM_SLEEP);
27717c478bd9Sstevel@tonic-gate 		mutex_enter(&slotinfop->slot_mutex);
27727c478bd9Sstevel@tonic-gate 
27737c478bd9Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
27747c478bd9Sstevel@tonic-gate 
27757c478bd9Sstevel@tonic-gate 		break;
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate 	case HPC_EVENT_BUS_ENUM:
27787c478bd9Sstevel@tonic-gate 		/*
27797c478bd9Sstevel@tonic-gate 		 * Same as HPC_EVENT_SLOT_ENUM as defined the PSARC doc.
27807c478bd9Sstevel@tonic-gate 		 * This term is used for better clarity of its usage.
27817c478bd9Sstevel@tonic-gate 		 *
27827c478bd9Sstevel@tonic-gate 		 * ENUM signal occurred on the bus. It may be from this
27837c478bd9Sstevel@tonic-gate 		 * slot or any other hotplug slot on the bus.
27847c478bd9Sstevel@tonic-gate 		 *
27857c478bd9Sstevel@tonic-gate 		 * It is NOT recommended that the hotswap controller uses
27867c478bd9Sstevel@tonic-gate 		 * event without queuing as NDI and other DDI calls may not
27877c478bd9Sstevel@tonic-gate 		 * necessarily be invokable in interrupt context.
27887c478bd9Sstevel@tonic-gate 		 * Hence the hotswap controller driver should use the
27897c478bd9Sstevel@tonic-gate 		 * CLEAR_ENUM event which returns the slot device number
27907c478bd9Sstevel@tonic-gate 		 * and then call HPC_EVENT_PROCESS_ENUM event with queuing.
27917c478bd9Sstevel@tonic-gate 		 *
27927c478bd9Sstevel@tonic-gate 		 * This can be used when the hotswap controller is
27937c478bd9Sstevel@tonic-gate 		 * implementing a polled event mechanism to do the
27947c478bd9Sstevel@tonic-gate 		 * necessary actions in a single call.
27957c478bd9Sstevel@tonic-gate 		 */
27967c478bd9Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): ENUM# is generated"
27977c478bd9Sstevel@tonic-gate 		    " on the bus (for slot %s ?)",
27987c478bd9Sstevel@tonic-gate 		    ddi_driver_name(pcihp_p->dip),
27997c478bd9Sstevel@tonic-gate 		    ddi_get_instance(pcihp_p->dip),
28007c478bd9Sstevel@tonic-gate 		    slotinfop->name));
28017c478bd9Sstevel@tonic-gate 
28027c478bd9Sstevel@tonic-gate 		mutex_exit(&slotinfop->slot_mutex);
28037c478bd9Sstevel@tonic-gate 		rv = pcihp_handle_enum(pcihp_p, pci_dev, PCIHP_HANDLE_ENUM,
280432d69156Sscarter 		    KM_SLEEP);
28057c478bd9Sstevel@tonic-gate 		mutex_enter(&slotinfop->slot_mutex);
28067c478bd9Sstevel@tonic-gate 
28077c478bd9Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
28087c478bd9Sstevel@tonic-gate 
28097c478bd9Sstevel@tonic-gate 		break;
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate 	case HPC_EVENT_SLOT_BLUE_LED_ON:
28127c478bd9Sstevel@tonic-gate 
28137c478bd9Sstevel@tonic-gate 		/*
28147c478bd9Sstevel@tonic-gate 		 * Request to turn Hot Swap Blue LED on.
28157c478bd9Sstevel@tonic-gate 		 */
28167c478bd9Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): Request To Turn On Blue "
28177c478bd9Sstevel@tonic-gate 		    "LED on the bus (for slot %s ?)",
28187c478bd9Sstevel@tonic-gate 		    ddi_driver_name(pcihp_p->dip),
28197c478bd9Sstevel@tonic-gate 		    ddi_get_instance(pcihp_p->dip),
28207c478bd9Sstevel@tonic-gate 		    slotinfop->name));
28217c478bd9Sstevel@tonic-gate 
28227c478bd9Sstevel@tonic-gate 		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_SLOT_BLUE_LED_ON);
28237c478bd9Sstevel@tonic-gate 		break;
28247c478bd9Sstevel@tonic-gate 
28257c478bd9Sstevel@tonic-gate 	case HPC_EVENT_DISABLE_ENUM:
28267c478bd9Sstevel@tonic-gate 		/*
28277c478bd9Sstevel@tonic-gate 		 * Disable ENUM# which disables auto configuration on this slot
28287c478bd9Sstevel@tonic-gate 		 */
28297c478bd9Sstevel@tonic-gate 		if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
28307c478bd9Sstevel@tonic-gate 			pcihp_hs_csr_op(pcihp_p, pci_dev,
283132d69156Sscarter 			    HPC_EVENT_DISABLE_ENUM);
28327c478bd9Sstevel@tonic-gate 			slotinfop->slot_flags &= ~PCIHP_SLOT_AUTO_CFG_EN;
28337c478bd9Sstevel@tonic-gate 		}
28347c478bd9Sstevel@tonic-gate 		break;
28357c478bd9Sstevel@tonic-gate 
28367c478bd9Sstevel@tonic-gate 	case HPC_EVENT_ENABLE_ENUM:
28377c478bd9Sstevel@tonic-gate 		/*
28387c478bd9Sstevel@tonic-gate 		 * Enable ENUM# which enables auto configuration on this slot.
28397c478bd9Sstevel@tonic-gate 		 */
28407c478bd9Sstevel@tonic-gate 		if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
28417c478bd9Sstevel@tonic-gate 			pcihp_hs_csr_op(pcihp_p, pci_dev,
284232d69156Sscarter 			    HPC_EVENT_ENABLE_ENUM);
28437c478bd9Sstevel@tonic-gate 			slotinfop->slot_flags |= PCIHP_SLOT_AUTO_CFG_EN;
28447c478bd9Sstevel@tonic-gate 		}
28457c478bd9Sstevel@tonic-gate 		break;
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate 	case HPC_EVENT_SLOT_BLUE_LED_OFF:
28487c478bd9Sstevel@tonic-gate 
28497c478bd9Sstevel@tonic-gate 		/*
28507c478bd9Sstevel@tonic-gate 		 * Request to turn Hot Swap Blue LED off.
28517c478bd9Sstevel@tonic-gate 		 */
28527c478bd9Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): Request To Turn Off Blue "
28537c478bd9Sstevel@tonic-gate 		    "LED on the bus (for slot %s ?)",
28547c478bd9Sstevel@tonic-gate 		    ddi_driver_name(pcihp_p->dip),
28557c478bd9Sstevel@tonic-gate 		    ddi_get_instance(pcihp_p->dip),
28567c478bd9Sstevel@tonic-gate 		    slotinfop->name));
28577c478bd9Sstevel@tonic-gate 
28587c478bd9Sstevel@tonic-gate 		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_SLOT_BLUE_LED_OFF);
28597c478bd9Sstevel@tonic-gate 
28607c478bd9Sstevel@tonic-gate 		break;
28617c478bd9Sstevel@tonic-gate 
28627c478bd9Sstevel@tonic-gate 	case HPC_EVENT_SLOT_NOT_HEALTHY:
28637c478bd9Sstevel@tonic-gate 		/*
28647c478bd9Sstevel@tonic-gate 		 * HEALTHY# signal on this slot is not OK.
28657c478bd9Sstevel@tonic-gate 		 */
28667c478bd9Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): HEALTHY# signal is not OK"
286732d69156Sscarter 		    " for this slot %s",
286832d69156Sscarter 		    ddi_driver_name(pcihp_p->dip),
286932d69156Sscarter 		    ddi_get_instance(pcihp_p->dip),
287032d69156Sscarter 		    slotinfop->name));
28717c478bd9Sstevel@tonic-gate 
28727c478bd9Sstevel@tonic-gate 		/* record the state in slot_flags field */
28737c478bd9Sstevel@tonic-gate 		slotinfop->slot_flags |= PCIHP_SLOT_NOT_HEALTHY;
28747c478bd9Sstevel@tonic-gate 		slotinfop->condition = AP_COND_FAILED;
28757c478bd9Sstevel@tonic-gate 
28767c478bd9Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
28777c478bd9Sstevel@tonic-gate 
28787c478bd9Sstevel@tonic-gate 		break;
28797c478bd9Sstevel@tonic-gate 
28807c478bd9Sstevel@tonic-gate 	case HPC_EVENT_SLOT_HEALTHY_OK:
28817c478bd9Sstevel@tonic-gate 		/*
28827c478bd9Sstevel@tonic-gate 		 * HEALTHY# signal on this slot is OK now.
28837c478bd9Sstevel@tonic-gate 		 */
28847c478bd9Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): HEALTHY# signal is OK now"
288532d69156Sscarter 		    " for this slot %s",
288632d69156Sscarter 		    ddi_driver_name(pcihp_p->dip),
288732d69156Sscarter 		    ddi_get_instance(pcihp_p->dip),
288832d69156Sscarter 		    slotinfop->name));
28897c478bd9Sstevel@tonic-gate 
28907c478bd9Sstevel@tonic-gate 		/* update the state in slot_flags field */
28917c478bd9Sstevel@tonic-gate 		slotinfop->slot_flags &= ~PCIHP_SLOT_NOT_HEALTHY;
28927c478bd9Sstevel@tonic-gate 		slotinfop->condition = AP_COND_OK;
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
28957c478bd9Sstevel@tonic-gate 
28967c478bd9Sstevel@tonic-gate 		break;
28977c478bd9Sstevel@tonic-gate 
289870025d76Sjohnny 	case HPC_EVENT_SLOT_ATTN:
289970025d76Sjohnny 		/*
290070025d76Sjohnny 		 * Attention button is pressed.
290170025d76Sjohnny 		 */
290270025d76Sjohnny 		if (((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) == 0) ||
290370025d76Sjohnny 		    (slotinfop->slot_flags & PCIHP_SLOT_DISABLED)) {
290470025d76Sjohnny 			/*
290570025d76Sjohnny 			 * either auto-conifiguration or the slot is disabled,
290670025d76Sjohnny 			 * ignore this event.
290770025d76Sjohnny 			 */
290870025d76Sjohnny 			break;
290970025d76Sjohnny 		}
291070025d76Sjohnny 
291170025d76Sjohnny 		if (slotinfop->ostate == AP_OSTATE_UNCONFIGURED)
291270025d76Sjohnny 			hint = SE_INCOMING_RES;
291370025d76Sjohnny 		else
291470025d76Sjohnny 			hint = SE_OUTGOING_RES;
291570025d76Sjohnny 
291670025d76Sjohnny 		if (ddi_getprop(DDI_DEV_T_ANY, pcihp_p->dip, DDI_PROP_DONTPASS,
291770025d76Sjohnny 		    "inkernel-autoconfig", 0) == 0) {
291870025d76Sjohnny 			pcihp_gen_sysevent(slotinfop->name, PCIHP_DR_REQ, hint,
291970025d76Sjohnny 			    pcihp_p->dip, KM_SLEEP);
292070025d76Sjohnny 			break;
292170025d76Sjohnny 		}
292270025d76Sjohnny 
292370025d76Sjohnny 		if ((slotinfop->ostate == AP_OSTATE_UNCONFIGURED) &&
292470025d76Sjohnny 		    (slotinfop->rstate != AP_RSTATE_EMPTY) &&
292570025d76Sjohnny 		    (slotinfop->condition != AP_COND_FAILED)) {
292670025d76Sjohnny 			if (slotinfop->rstate == AP_RSTATE_DISCONNECTED) {
292770025d76Sjohnny 				rv = hpc_nexus_connect(slotinfop->slot_hdl,
292870025d76Sjohnny 				    NULL, 0);
292970025d76Sjohnny 				if (rv == HPC_SUCCESS)
293070025d76Sjohnny 					slotinfop->rstate = AP_RSTATE_CONNECTED;
293170025d76Sjohnny 				else
293270025d76Sjohnny 					break;
293370025d76Sjohnny 			}
293470025d76Sjohnny 
293570025d76Sjohnny 			rv = pcihp_configure_ap(pcihp_p, pci_dev);
293670025d76Sjohnny 
293770025d76Sjohnny 		} else if ((slotinfop->ostate == AP_OSTATE_CONFIGURED) &&
293870025d76Sjohnny 		    (slotinfop->rstate == AP_RSTATE_CONNECTED) &&
293970025d76Sjohnny 		    (slotinfop->condition != AP_COND_FAILED)) {
294070025d76Sjohnny 			rv = pcihp_unconfigure_ap(pcihp_p, pci_dev);
294170025d76Sjohnny 
294270025d76Sjohnny 			if (rv != HPC_SUCCESS)
294370025d76Sjohnny 				break;
294470025d76Sjohnny 
294570025d76Sjohnny 			rv = hpc_nexus_disconnect(slotinfop->slot_hdl,
294670025d76Sjohnny 			    NULL, 0);
294770025d76Sjohnny 			if (rv == HPC_SUCCESS)
294870025d76Sjohnny 				slotinfop->rstate = AP_RSTATE_DISCONNECTED;
294970025d76Sjohnny 		}
295070025d76Sjohnny 
295170025d76Sjohnny 		break;
295270025d76Sjohnny 
295370025d76Sjohnny 	case HPC_EVENT_SLOT_POWER_FAULT:
295470025d76Sjohnny 		/*
295570025d76Sjohnny 		 * Power fault is detected.
295670025d76Sjohnny 		 */
295770025d76Sjohnny 		cmn_err(CE_NOTE, "pcihp (%s%d): power-fault"
295870025d76Sjohnny 		    " for this slot %s",
295970025d76Sjohnny 		    ddi_driver_name(pcihp_p->dip),
296070025d76Sjohnny 		    ddi_get_instance(pcihp_p->dip),
296170025d76Sjohnny 		    slotinfop->name);
296270025d76Sjohnny 
29634eacc763Sjj 		/* turn on ATTN led */
29644eacc763Sjj 		led_info.led = HPC_ATTN_LED;
29654eacc763Sjj 		led_info.state = HPC_LED_ON;
29664eacc763Sjj 		rv = hpc_nexus_control(slotinfop->slot_hdl,
29674eacc763Sjj 		    HPC_CTRL_SET_LED_STATE, (caddr_t)&led_info);
29684eacc763Sjj 
2969f94c6026Sjj 		if (slotinfop->rstate == AP_RSTATE_CONNECTED)
2970f94c6026Sjj 			(void) hpc_nexus_disconnect(slotinfop->slot_hdl,
2971f94c6026Sjj 			    NULL, 0);
2972f94c6026Sjj 
297370025d76Sjohnny 		slotinfop->condition = AP_COND_FAILED;
297470025d76Sjohnny 
297570025d76Sjohnny 		pcihp_gen_sysevent(slotinfop->name, PCIHP_DR_AP_STATE_CHANGE,
297670025d76Sjohnny 		    SE_NO_HINT, pcihp_p->dip, KM_SLEEP);
297770025d76Sjohnny 
297870025d76Sjohnny 		break;
297970025d76Sjohnny 
29807c478bd9Sstevel@tonic-gate 	default:
29817c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "pcihp (%s%d): unknown event %x"
298232d69156Sscarter 		    " for this slot %s",
298332d69156Sscarter 		    ddi_driver_name(pcihp_p->dip),
298432d69156Sscarter 		    ddi_get_instance(pcihp_p->dip), event_mask,
298532d69156Sscarter 		    slotinfop->name);
29867c478bd9Sstevel@tonic-gate 
29877c478bd9Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate 		break;
29907c478bd9Sstevel@tonic-gate 	}
29917c478bd9Sstevel@tonic-gate 
29927c478bd9Sstevel@tonic-gate 	mutex_exit(&slotinfop->slot_mutex);
29937c478bd9Sstevel@tonic-gate 
29947c478bd9Sstevel@tonic-gate 	(void) pcihp_get_soft_state(self, PCIHP_DR_SLOT_EXIT, &rval);
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate 	return (rv);
29977c478bd9Sstevel@tonic-gate }
29987c478bd9Sstevel@tonic-gate 
29997c478bd9Sstevel@tonic-gate /*
30007c478bd9Sstevel@tonic-gate  * This function is called to online or offline the devices for an
30017c478bd9Sstevel@tonic-gate  * attachment point. If the PCI device number of the node matches
30027c478bd9Sstevel@tonic-gate  * with the device number of the specified hot plug slot then
30037c478bd9Sstevel@tonic-gate  * the operation is performed.
30047c478bd9Sstevel@tonic-gate  */
30057c478bd9Sstevel@tonic-gate static int
pcihp_configure(dev_info_t * dip,void * hdl)30067c478bd9Sstevel@tonic-gate pcihp_configure(dev_info_t *dip, void *hdl)
30077c478bd9Sstevel@tonic-gate {
30087c478bd9Sstevel@tonic-gate 	int pci_dev;
30097c478bd9Sstevel@tonic-gate 	struct pcihp_config_ctrl *ctrl = (struct pcihp_config_ctrl *)hdl;
30107c478bd9Sstevel@tonic-gate 	int rv;
30117c478bd9Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
30127c478bd9Sstevel@tonic-gate 	int length;
30137c478bd9Sstevel@tonic-gate 
30147c478bd9Sstevel@tonic-gate 	/*
30157c478bd9Sstevel@tonic-gate 	 * Get the PCI device number information from the devinfo
30167c478bd9Sstevel@tonic-gate 	 * node. Since the node may not have the address field
30177c478bd9Sstevel@tonic-gate 	 * setup (this is done in the DDI_INITCHILD of the parent)
30187c478bd9Sstevel@tonic-gate 	 * we look up the 'reg' property to decode that information.
30197c478bd9Sstevel@tonic-gate 	 */
302032d69156Sscarter 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
302132d69156Sscarter 	    "reg", (int **)&pci_rp, (uint_t *)&length) != DDI_PROP_SUCCESS) {
30227c478bd9Sstevel@tonic-gate 		ctrl->rv = DDI_FAILURE;
30237c478bd9Sstevel@tonic-gate 		ctrl->dip = dip;
30247c478bd9Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
30257c478bd9Sstevel@tonic-gate 	}
30267c478bd9Sstevel@tonic-gate 
30277c478bd9Sstevel@tonic-gate 	/* get the pci device id information */
30287c478bd9Sstevel@tonic-gate 	pci_dev = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
30297c478bd9Sstevel@tonic-gate 
30307c478bd9Sstevel@tonic-gate 	/*
30317c478bd9Sstevel@tonic-gate 	 * free the memory allocated by ddi_prop_lookup_int_array
30327c478bd9Sstevel@tonic-gate 	 */
30337c478bd9Sstevel@tonic-gate 	ddi_prop_free(pci_rp);
30347c478bd9Sstevel@tonic-gate 
30357c478bd9Sstevel@tonic-gate 	/*
30367c478bd9Sstevel@tonic-gate 	 * Match the node for the device number of the slot.
30377c478bd9Sstevel@tonic-gate 	 */
30387c478bd9Sstevel@tonic-gate 	if (pci_dev == ctrl->pci_dev) {	/* node is a match */
30397c478bd9Sstevel@tonic-gate 		if (ctrl->op == PCIHP_ONLINE) {
30407c478bd9Sstevel@tonic-gate 			/* it is CONFIGURE operation */
304132d69156Sscarter 
304232d69156Sscarter 			/* skip this device if it is disabled or faulty */
304332d69156Sscarter 			if (pcihp_check_status(dip) == B_FALSE) {
304432d69156Sscarter 				return (DDI_WALK_PRUNECHILD);
304532d69156Sscarter 			}
304632d69156Sscarter 
30477c478bd9Sstevel@tonic-gate 			rv = ndi_devi_online(dip, NDI_ONLINE_ATTACH|NDI_CONFIG);
30487c478bd9Sstevel@tonic-gate 		} else {
30497c478bd9Sstevel@tonic-gate 			/*
30507c478bd9Sstevel@tonic-gate 			 * it is UNCONFIGURE operation.
30517c478bd9Sstevel@tonic-gate 			 */
30527c478bd9Sstevel@tonic-gate 			rv = ndi_devi_offline(dip, NDI_UNCONFIG);
30537c478bd9Sstevel@tonic-gate 		}
30547c478bd9Sstevel@tonic-gate 		if (rv != NDI_SUCCESS) {
30557c478bd9Sstevel@tonic-gate 			/* failed to attach/detach the driver(s) */
30567c478bd9Sstevel@tonic-gate 			ctrl->rv = rv;
30577c478bd9Sstevel@tonic-gate 			ctrl->dip = dip;
30587c478bd9Sstevel@tonic-gate 			/* terminate the search if specified */
30597c478bd9Sstevel@tonic-gate 			if (!(ctrl->flags & PCIHP_CFG_CONTINUE))
30607c478bd9Sstevel@tonic-gate 				return (DDI_WALK_TERMINATE);
30617c478bd9Sstevel@tonic-gate 		}
30627c478bd9Sstevel@tonic-gate 	}
30637c478bd9Sstevel@tonic-gate 
30647c478bd9Sstevel@tonic-gate 	/*
30657c478bd9Sstevel@tonic-gate 	 * continue the walk to the next sibling to look for a match
30667c478bd9Sstevel@tonic-gate 	 * or to find other nodes if this card is a multi-function card.
30677c478bd9Sstevel@tonic-gate 	 */
30687c478bd9Sstevel@tonic-gate 	return (DDI_WALK_PRUNECHILD);
30697c478bd9Sstevel@tonic-gate }
30707c478bd9Sstevel@tonic-gate 
307132d69156Sscarter /*
307232d69156Sscarter  * Check the device for a 'status' property.  A conforming device
307332d69156Sscarter  * should have a status of "okay", "disabled", "fail", or "fail-xxx".
307432d69156Sscarter  *
307532d69156Sscarter  * Return FALSE for a conforming device that is disabled or faulted.
307632d69156Sscarter  * Return TRUE in every other case.
307732d69156Sscarter  */
307832d69156Sscarter static bool_t
pcihp_check_status(dev_info_t * dip)307932d69156Sscarter pcihp_check_status(dev_info_t *dip)
308032d69156Sscarter {
308132d69156Sscarter 	char *status_prop;
308232d69156Sscarter 	bool_t rv = B_TRUE;
308332d69156Sscarter 
308432d69156Sscarter 	/* try to get the 'status' property */
308532d69156Sscarter 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
308632d69156Sscarter 	    "status", &status_prop) == DDI_PROP_SUCCESS) {
308732d69156Sscarter 
308832d69156Sscarter 		/*
308932d69156Sscarter 		 * test if the status is "disabled", "fail", or
309032d69156Sscarter 		 * "fail-xxx".
309132d69156Sscarter 		 */
309232d69156Sscarter 		if (strcmp(status_prop, "disabled") == 0) {
309332d69156Sscarter 			rv = B_FALSE;
309432d69156Sscarter 			PCIHP_DEBUG((CE_NOTE,
309532d69156Sscarter 			    "pcihp (%s%d): device is in disabled state",
309632d69156Sscarter 			    ddi_driver_name(dip), ddi_get_instance(dip)));
309732d69156Sscarter 		} else if (strncmp(status_prop, "fail", 4) == 0) {
309832d69156Sscarter 			rv = B_FALSE;
309932d69156Sscarter 			cmn_err(CE_WARN,
310032d69156Sscarter 			    "pcihp (%s%d): device is in fault state (%s)",
310132d69156Sscarter 			    ddi_driver_name(dip), ddi_get_instance(dip),
310232d69156Sscarter 			    status_prop);
310332d69156Sscarter 		}
310432d69156Sscarter 
310532d69156Sscarter 		ddi_prop_free(status_prop);
310632d69156Sscarter 	}
310732d69156Sscarter 
310832d69156Sscarter 	return (rv);
310932d69156Sscarter }
311032d69156Sscarter 
31117c478bd9Sstevel@tonic-gate /* control structure used to find a device in the devinfo tree */
31127c478bd9Sstevel@tonic-gate struct pcihp_find_ctrl {
31137c478bd9Sstevel@tonic-gate 	uint_t		device;
31147c478bd9Sstevel@tonic-gate 	uint_t		function;
31157c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
31167c478bd9Sstevel@tonic-gate };
31177c478bd9Sstevel@tonic-gate 
31187c478bd9Sstevel@tonic-gate static dev_info_t *
pcihp_devi_find(dev_info_t * dip,uint_t device,uint_t function)31197c478bd9Sstevel@tonic-gate pcihp_devi_find(dev_info_t *dip, uint_t device, uint_t function)
31207c478bd9Sstevel@tonic-gate {
31217c478bd9Sstevel@tonic-gate 	struct pcihp_find_ctrl ctrl;
31227c478bd9Sstevel@tonic-gate 
31237c478bd9Sstevel@tonic-gate 	ctrl.device = device;
31247c478bd9Sstevel@tonic-gate 	ctrl.function = function;
31257c478bd9Sstevel@tonic-gate 	ctrl.dip = NULL;
31267c478bd9Sstevel@tonic-gate 
31273fe80ca4SDan Cross 	ndi_devi_enter(dip);
31287c478bd9Sstevel@tonic-gate 	ddi_walk_devs(ddi_get_child(dip), pcihp_match_dev, (void *)&ctrl);
31293fe80ca4SDan Cross 	ndi_devi_exit(dip);
31307c478bd9Sstevel@tonic-gate 
31317c478bd9Sstevel@tonic-gate 	return (ctrl.dip);
31327c478bd9Sstevel@tonic-gate }
31337c478bd9Sstevel@tonic-gate 
31347c478bd9Sstevel@tonic-gate static int
pcihp_match_dev(dev_info_t * dip,void * hdl)31357c478bd9Sstevel@tonic-gate pcihp_match_dev(dev_info_t *dip, void *hdl)
31367c478bd9Sstevel@tonic-gate {
31377c478bd9Sstevel@tonic-gate 	struct pcihp_find_ctrl *ctrl = (struct pcihp_find_ctrl *)hdl;
31387c478bd9Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
31397c478bd9Sstevel@tonic-gate 	int length;
31407c478bd9Sstevel@tonic-gate 	int pci_dev;
31417c478bd9Sstevel@tonic-gate 	int pci_func;
31427c478bd9Sstevel@tonic-gate 
314332d69156Sscarter 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
314432d69156Sscarter 	    "reg", (int **)&pci_rp, (uint_t *)&length) != DDI_PROP_SUCCESS) {
31457c478bd9Sstevel@tonic-gate 		ctrl->dip = NULL;
31467c478bd9Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
31477c478bd9Sstevel@tonic-gate 	}
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate 	/* get the PCI device address info */
31507c478bd9Sstevel@tonic-gate 	pci_dev = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
31517c478bd9Sstevel@tonic-gate 	pci_func = PCI_REG_FUNC_G(pci_rp->pci_phys_hi);
31527c478bd9Sstevel@tonic-gate 
31537c478bd9Sstevel@tonic-gate 	/*
31547c478bd9Sstevel@tonic-gate 	 * free the memory allocated by ddi_prop_lookup_int_array
31557c478bd9Sstevel@tonic-gate 	 */
31567c478bd9Sstevel@tonic-gate 	ddi_prop_free(pci_rp);
31577c478bd9Sstevel@tonic-gate 
31587c478bd9Sstevel@tonic-gate 
31597c478bd9Sstevel@tonic-gate 	if ((pci_dev == ctrl->device) && (pci_func == ctrl->function)) {
31607c478bd9Sstevel@tonic-gate 		/* found the match for the specified device address */
31617c478bd9Sstevel@tonic-gate 		ctrl->dip = dip;
31627c478bd9Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
31637c478bd9Sstevel@tonic-gate 	}
31647c478bd9Sstevel@tonic-gate 
31657c478bd9Sstevel@tonic-gate 	/*
31667c478bd9Sstevel@tonic-gate 	 * continue the walk to the next sibling to look for a match.
31677c478bd9Sstevel@tonic-gate 	 */
31687c478bd9Sstevel@tonic-gate 	return (DDI_WALK_PRUNECHILD);
31697c478bd9Sstevel@tonic-gate }
31707c478bd9Sstevel@tonic-gate 
31717c478bd9Sstevel@tonic-gate #if 0
31727c478bd9Sstevel@tonic-gate /*
31737c478bd9Sstevel@tonic-gate  * Probe the configuration space of the slot to determine the receptacle
31747c478bd9Sstevel@tonic-gate  * state. There may not be any devinfo tree created for this slot.
31757c478bd9Sstevel@tonic-gate  */
31767c478bd9Sstevel@tonic-gate static void
31777c478bd9Sstevel@tonic-gate pcihp_probe_slot_state(dev_info_t *dip, int dev, hpc_slot_state_t *rstatep)
31787c478bd9Sstevel@tonic-gate {
31797c478bd9Sstevel@tonic-gate 	/* XXX FIX IT */
31807c478bd9Sstevel@tonic-gate }
31817c478bd9Sstevel@tonic-gate #endif
31827c478bd9Sstevel@tonic-gate 
31837c478bd9Sstevel@tonic-gate /*
31847c478bd9Sstevel@tonic-gate  * This routine is called when a ENUM# assertion is detected for a bus.
31857c478bd9Sstevel@tonic-gate  * Since ENUM# may be bussed, the slot that asserted ENUM# may not be known.
31867c478bd9Sstevel@tonic-gate  * The HPC Driver passes the handle of a slot that is its best guess.
31877c478bd9Sstevel@tonic-gate  * If the best guess slot is the one that asserted ENUM#, the proper handling
31887c478bd9Sstevel@tonic-gate  * will be done.  If its not, all possible slots will be locked at until
31897c478bd9Sstevel@tonic-gate  * one that is asserting ENUM is found.
31907c478bd9Sstevel@tonic-gate  * Also, indicate to the HSC to turn on ENUM# after it is serviced,
31917c478bd9Sstevel@tonic-gate  * incase if it was disabled by the HSC due to the nature of asynchronous
31927c478bd9Sstevel@tonic-gate  * delivery of interrupt by the framework.
31937c478bd9Sstevel@tonic-gate  *
31947c478bd9Sstevel@tonic-gate  * opcode has the following meanings.
31957c478bd9Sstevel@tonic-gate  * PCIHP_CLEAR_ENUM = just clear interrupt and return the PCI device no. if
31967c478bd9Sstevel@tonic-gate  *			success, else return -1.
31977c478bd9Sstevel@tonic-gate  * PCIHP_HANDLE_ENUM = clear interrupt and handle interrupt also.
31987c478bd9Sstevel@tonic-gate  *
31997c478bd9Sstevel@tonic-gate  */
32007c478bd9Sstevel@tonic-gate static int
pcihp_handle_enum(pcihp_t * pcihp_p,int favorite_pci_dev,int opcode,int kmflag)32017c478bd9Sstevel@tonic-gate pcihp_handle_enum(pcihp_t *pcihp_p, int favorite_pci_dev, int opcode,
3202*d5ebc493SDan Cross     int kmflag)
32037c478bd9Sstevel@tonic-gate {
32047c478bd9Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
32057c478bd9Sstevel@tonic-gate 	int pci_dev, rc, event_serviced = 0;
32067c478bd9Sstevel@tonic-gate 
32077c478bd9Sstevel@tonic-gate 	/*
32087c478bd9Sstevel@tonic-gate 	 * Handle ENUM# condition for the "favorite" slot first.
32097c478bd9Sstevel@tonic-gate 	 */
32107c478bd9Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[favorite_pci_dev];
32117c478bd9Sstevel@tonic-gate 	if (slotinfop) {
32127c478bd9Sstevel@tonic-gate 		/*
32137c478bd9Sstevel@tonic-gate 		 * First try the "favorite" pci device.  This is the device
32147c478bd9Sstevel@tonic-gate 		 * associated with the handle passed by the HPC Driver.
32157c478bd9Sstevel@tonic-gate 		 */
32167c478bd9Sstevel@tonic-gate 		rc = pcihp_enum_slot(pcihp_p, slotinfop, favorite_pci_dev,
321732d69156Sscarter 		    opcode, kmflag);
32187c478bd9Sstevel@tonic-gate 		if (rc != HPC_EVENT_UNCLAIMED) {	/* indicates success */
32197c478bd9Sstevel@tonic-gate 			event_serviced = 1;
32207c478bd9Sstevel@tonic-gate 			/* This MUST be a non-DEBUG feature. */
32217c478bd9Sstevel@tonic-gate 			if (! pcihp_enum_scan_all) {
32227c478bd9Sstevel@tonic-gate 				return (rc);
32237c478bd9Sstevel@tonic-gate 			}
32247c478bd9Sstevel@tonic-gate 		}
32257c478bd9Sstevel@tonic-gate 	}
32267c478bd9Sstevel@tonic-gate 
32277c478bd9Sstevel@tonic-gate 	/*
32287c478bd9Sstevel@tonic-gate 	 * If ENUM# is implemented as a radial signal, then there is no
32297c478bd9Sstevel@tonic-gate 	 * need to further poll the slots.
32307c478bd9Sstevel@tonic-gate 	 */
32317c478bd9Sstevel@tonic-gate 	if (pcihp_p->bus_flags & PCIHP_BUS_ENUM_RADIAL)
32327c478bd9Sstevel@tonic-gate 		goto enum_service_check;
32337c478bd9Sstevel@tonic-gate 
32347c478bd9Sstevel@tonic-gate 	/*
32357c478bd9Sstevel@tonic-gate 	 * If the "favorite" pci device didn't assert ENUM#, then
32367c478bd9Sstevel@tonic-gate 	 * try the rest.  Once we find and handle a device that asserted
32377c478bd9Sstevel@tonic-gate 	 * ENUM#, then we will terminate the walk by returning unless
32387c478bd9Sstevel@tonic-gate 	 * scan-all flag is set.
32397c478bd9Sstevel@tonic-gate 	 */
32407c478bd9Sstevel@tonic-gate 	for (pci_dev = 0; pci_dev < PCI_MAX_DEVS; pci_dev++) {
32417c478bd9Sstevel@tonic-gate 		if (pci_dev != favorite_pci_dev) {
32427c478bd9Sstevel@tonic-gate 			slotinfop = &pcihp_p->slotinfo[pci_dev];
32437c478bd9Sstevel@tonic-gate 			if (slotinfop == NULL) {
32447c478bd9Sstevel@tonic-gate 				continue;
32457c478bd9Sstevel@tonic-gate 			}
32467c478bd9Sstevel@tonic-gate 			/* Only CPCI devices support ENUM# generation. */
32477c478bd9Sstevel@tonic-gate 			if (!(slotinfop->slot_type & HPC_SLOT_TYPE_CPCI))
32487c478bd9Sstevel@tonic-gate 				continue;
32497c478bd9Sstevel@tonic-gate 			rc = pcihp_enum_slot(pcihp_p, slotinfop, pci_dev,
325032d69156Sscarter 			    opcode, kmflag);
32517c478bd9Sstevel@tonic-gate 			if (rc != HPC_EVENT_UNCLAIMED) {
32527c478bd9Sstevel@tonic-gate 				event_serviced = 1;
32537c478bd9Sstevel@tonic-gate 				/* This MUST be a non-DEBUG feature. */
32547c478bd9Sstevel@tonic-gate 				if (! pcihp_enum_scan_all)
32557c478bd9Sstevel@tonic-gate 					break;
32567c478bd9Sstevel@tonic-gate 			}
32577c478bd9Sstevel@tonic-gate 		}
32587c478bd9Sstevel@tonic-gate 	}
32597c478bd9Sstevel@tonic-gate 
32607c478bd9Sstevel@tonic-gate enum_service_check:
32617c478bd9Sstevel@tonic-gate 	if (event_serviced) {
32627c478bd9Sstevel@tonic-gate 		return (rc);
32637c478bd9Sstevel@tonic-gate 	}
32647c478bd9Sstevel@tonic-gate 
32657c478bd9Sstevel@tonic-gate 	/* No ENUM# event found, Return */
32667c478bd9Sstevel@tonic-gate 	return (HPC_EVENT_UNCLAIMED);
32677c478bd9Sstevel@tonic-gate }
32687c478bd9Sstevel@tonic-gate 
32697c478bd9Sstevel@tonic-gate /*
32707c478bd9Sstevel@tonic-gate  * This routine attempts to handle a possible ENUM# assertion case for a
32717c478bd9Sstevel@tonic-gate  * specified slot.  This only works for adapters that implement Hot Swap
32727c478bd9Sstevel@tonic-gate  * Friendly Silicon.  If the slot's HS_CSR is read and it specifies ENUM#
32737c478bd9Sstevel@tonic-gate  * has been asserted, either the insertion or removal handlers will be
32747c478bd9Sstevel@tonic-gate  * called.
32757c478bd9Sstevel@tonic-gate  */
32767c478bd9Sstevel@tonic-gate static int
pcihp_enum_slot(pcihp_t * pcihp_p,struct pcihp_slotinfo * slotinfop,int pci_dev,int opcode,int kmflag)32777c478bd9Sstevel@tonic-gate pcihp_enum_slot(pcihp_t *pcihp_p, struct pcihp_slotinfo *slotinfop, int pci_dev,
3278*d5ebc493SDan Cross     int opcode, int kmflag)
32797c478bd9Sstevel@tonic-gate {
32807c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t handle;
32817c478bd9Sstevel@tonic-gate 	dev_info_t *dip, *new_child = NULL;
32827c478bd9Sstevel@tonic-gate 	int result, rv = -1;
32837c478bd9Sstevel@tonic-gate 	uint8_t hs_csr;
32847c478bd9Sstevel@tonic-gate 
32857c478bd9Sstevel@tonic-gate 	if (pcihp_config_setup(&dip, &handle, &new_child, pci_dev,
328632d69156Sscarter 	    pcihp_p) != DDI_SUCCESS) {
32877c478bd9Sstevel@tonic-gate 		return (HPC_EVENT_UNCLAIMED);
32887c478bd9Sstevel@tonic-gate 	}
32897c478bd9Sstevel@tonic-gate 
32907c478bd9Sstevel@tonic-gate 	/*
32917c478bd9Sstevel@tonic-gate 	 * Read the device's HS_CSR.
32927c478bd9Sstevel@tonic-gate 	 */
32937c478bd9Sstevel@tonic-gate 	result = pcihp_get_hs_csr(slotinfop, handle, (uint8_t *)&hs_csr);
32947c478bd9Sstevel@tonic-gate 	PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): hs_csr = %x, flags = %x",
329532d69156Sscarter 	    ddi_driver_name(pcihp_p->dip), ddi_get_instance(pcihp_p->dip),
329632d69156Sscarter 	    hs_csr, slotinfop->slot_flags));
32977c478bd9Sstevel@tonic-gate 	/*
32987c478bd9Sstevel@tonic-gate 	 * we teardown our device map here, because in case of an
32997c478bd9Sstevel@tonic-gate 	 * extraction event, our nodes would be freed and a teardown
33007c478bd9Sstevel@tonic-gate 	 * will cause problems.
33017c478bd9Sstevel@tonic-gate 	 */
33027c478bd9Sstevel@tonic-gate 	pcihp_config_teardown(&handle, &new_child, pci_dev, pcihp_p);
33037c478bd9Sstevel@tonic-gate 
33047c478bd9Sstevel@tonic-gate 	if (result == PCIHP_SUCCESS) {
33057c478bd9Sstevel@tonic-gate 
33067c478bd9Sstevel@tonic-gate 		/* If ENUM# is masked, then it is not us. Some other device */
33077c478bd9Sstevel@tonic-gate 		if ((hs_csr & HS_CSR_EIM) && (opcode == PCIHP_CLEAR_ENUM))
33087c478bd9Sstevel@tonic-gate 			return (HPC_EVENT_UNCLAIMED);
33097c478bd9Sstevel@tonic-gate 		/*
33107c478bd9Sstevel@tonic-gate 		 * This device supports Full Hot Swap and implements
33117c478bd9Sstevel@tonic-gate 		 * the Hot Swap Control and Status Register.
33127c478bd9Sstevel@tonic-gate 		 */
33137c478bd9Sstevel@tonic-gate 		if ((hs_csr & HS_CSR_INS) ||
331432d69156Sscarter 		    (slotinfop->slot_flags & PCIHP_SLOT_ENUM_INS_PENDING)) {
33157c478bd9Sstevel@tonic-gate 			/* handle insertion ENUM */
33167c478bd9Sstevel@tonic-gate 			PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): "
33177c478bd9Sstevel@tonic-gate 			    "Handle Insertion ENUM (INS) "
33187c478bd9Sstevel@tonic-gate 			    "on the bus (for slot %s ?)",
33197c478bd9Sstevel@tonic-gate 			    ddi_driver_name(pcihp_p->dip),
33207c478bd9Sstevel@tonic-gate 			    ddi_get_instance(pcihp_p->dip),
33217c478bd9Sstevel@tonic-gate 			    slotinfop->name));
33227c478bd9Sstevel@tonic-gate 
33237c478bd9Sstevel@tonic-gate 			/*
33247c478bd9Sstevel@tonic-gate 			 * generate sysevent
33257c478bd9Sstevel@tonic-gate 			 */
33267c478bd9Sstevel@tonic-gate 
33277c478bd9Sstevel@tonic-gate 			if (opcode == PCIHP_CLEAR_ENUM)
33287c478bd9Sstevel@tonic-gate 				pcihp_gen_sysevent(slotinfop->name,
332932d69156Sscarter 				    PCIHP_DR_REQ,
333032d69156Sscarter 				    SE_INCOMING_RES, pcihp_p->dip,
333132d69156Sscarter 				    kmflag);
33327c478bd9Sstevel@tonic-gate 
33337c478bd9Sstevel@tonic-gate 			rv = pcihp_handle_enum_insertion(pcihp_p, pci_dev,
333432d69156Sscarter 			    opcode, kmflag);
33357c478bd9Sstevel@tonic-gate 
333632d69156Sscarter 		} else if ((hs_csr & HS_CSR_EXT) ||
333732d69156Sscarter 		    (slotinfop->slot_flags & PCIHP_SLOT_ENUM_EXT_PENDING)) {
33387c478bd9Sstevel@tonic-gate 			/* handle extraction ENUM */
33397c478bd9Sstevel@tonic-gate 			PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): "
33407c478bd9Sstevel@tonic-gate 			    "Handle Extraction ENUM (EXT) "
33417c478bd9Sstevel@tonic-gate 			    "on the bus (for slot %s ?)",
33427c478bd9Sstevel@tonic-gate 			    ddi_driver_name(pcihp_p->dip),
33437c478bd9Sstevel@tonic-gate 			    ddi_get_instance(pcihp_p->dip),
33447c478bd9Sstevel@tonic-gate 			    slotinfop->name));
33457c478bd9Sstevel@tonic-gate 
33467c478bd9Sstevel@tonic-gate 			/*
33477c478bd9Sstevel@tonic-gate 			 * generate sysevent
33487c478bd9Sstevel@tonic-gate 			 */
33497c478bd9Sstevel@tonic-gate 
33507c478bd9Sstevel@tonic-gate 			if (opcode == PCIHP_CLEAR_ENUM)
33517c478bd9Sstevel@tonic-gate 				pcihp_gen_sysevent(slotinfop->name,
335232d69156Sscarter 				    PCIHP_DR_REQ,
335332d69156Sscarter 				    SE_OUTGOING_RES,
335432d69156Sscarter 				    pcihp_p->dip,
335532d69156Sscarter 				    kmflag);
33567c478bd9Sstevel@tonic-gate 
33577c478bd9Sstevel@tonic-gate 			rv = pcihp_handle_enum_extraction(pcihp_p, pci_dev,
335832d69156Sscarter 			    opcode, kmflag);
33597c478bd9Sstevel@tonic-gate 		}
33607c478bd9Sstevel@tonic-gate 		if (opcode == PCIHP_CLEAR_ENUM) {
33617c478bd9Sstevel@tonic-gate 			if (rv == PCIHP_SUCCESS)
33627c478bd9Sstevel@tonic-gate 				rv = pci_dev;
33637c478bd9Sstevel@tonic-gate 			else
33647c478bd9Sstevel@tonic-gate 				rv = HPC_EVENT_UNCLAIMED;
33657c478bd9Sstevel@tonic-gate 		}
33667c478bd9Sstevel@tonic-gate 	}
33677c478bd9Sstevel@tonic-gate 
33687c478bd9Sstevel@tonic-gate 	return (rv);
33697c478bd9Sstevel@tonic-gate }
33707c478bd9Sstevel@tonic-gate 
33717c478bd9Sstevel@tonic-gate /*
33727c478bd9Sstevel@tonic-gate  * This routine is called when a ENUM# caused by lifting the lever
33737c478bd9Sstevel@tonic-gate  * is detected.  If the occupant is configured, it will be unconfigured.
33747c478bd9Sstevel@tonic-gate  * If the occupant is already unconfigured or is successfully unconfigured,
33757c478bd9Sstevel@tonic-gate  * the blue LED on the adapter is illuminated which means its OK to remove.
33767c478bd9Sstevel@tonic-gate  * Please note that the lock must be released before invoking the
33777c478bd9Sstevel@tonic-gate  * generic AP unconfigure function.
33787c478bd9Sstevel@tonic-gate  */
33797c478bd9Sstevel@tonic-gate static int
pcihp_handle_enum_extraction(pcihp_t * pcihp_p,int pci_dev,int opcode,int kmflag)33807c478bd9Sstevel@tonic-gate pcihp_handle_enum_extraction(pcihp_t *pcihp_p, int pci_dev, int opcode,
3381*d5ebc493SDan Cross     int kmflag)
33827c478bd9Sstevel@tonic-gate {
33837c478bd9Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
33847c478bd9Sstevel@tonic-gate 	int rv = PCIHP_FAILURE;
33857c478bd9Sstevel@tonic-gate 
33867c478bd9Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
33877c478bd9Sstevel@tonic-gate 
33887c478bd9Sstevel@tonic-gate 	/*
33897c478bd9Sstevel@tonic-gate 	 * It was observed that, clearing the EXT bit turned the LED ON.
33907c478bd9Sstevel@tonic-gate 	 * This is a BIG problem in case if the unconfigure operation
33917c478bd9Sstevel@tonic-gate 	 * failed because the board was busy.
33927c478bd9Sstevel@tonic-gate 	 * In order to avoid this confusing situation (LED ON but the board
33937c478bd9Sstevel@tonic-gate 	 * is not unconfigured), we instead decided not to clear EXT but
33947c478bd9Sstevel@tonic-gate 	 * disable further ENUM# from this slot. Disabling ENUM# clears
33957c478bd9Sstevel@tonic-gate 	 * the interrupt.
33967c478bd9Sstevel@tonic-gate 	 * Finally before returning we clear the interrupt and enable
33977c478bd9Sstevel@tonic-gate 	 * ENUM# back again from this slot.
33987c478bd9Sstevel@tonic-gate 	 */
33997c478bd9Sstevel@tonic-gate 	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_DISABLE_ENUM);
34007c478bd9Sstevel@tonic-gate 	if (opcode == PCIHP_CLEAR_ENUM) {
34017c478bd9Sstevel@tonic-gate 		slotinfop->slot_flags |= PCIHP_SLOT_ENUM_EXT_PENDING;
34027c478bd9Sstevel@tonic-gate 		return (PCIHP_SUCCESS);
34037c478bd9Sstevel@tonic-gate 	}
34047c478bd9Sstevel@tonic-gate 
340570025d76Sjohnny 	mutex_enter(&slotinfop->slot_mutex);
34067c478bd9Sstevel@tonic-gate 	rv = pcihp_unconfigure_ap(pcihp_p, pci_dev);
340770025d76Sjohnny 	mutex_exit(&slotinfop->slot_mutex);
34087c478bd9Sstevel@tonic-gate 	if (rv != HPC_SUCCESS && rv != EBUSY) {
34097c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "%s%d: PCI device %x Failed on Unconfigure",
34107c478bd9Sstevel@tonic-gate 		    ddi_driver_name(pcihp_p->dip),
34117c478bd9Sstevel@tonic-gate 		    ddi_get_instance(pcihp_p->dip), pci_dev);
34127c478bd9Sstevel@tonic-gate 	}
34137c478bd9Sstevel@tonic-gate 	if (rv == EBUSY)
34147c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "%s%d: PCI device %x Busy",
34157c478bd9Sstevel@tonic-gate 		    ddi_driver_name(pcihp_p->dip),
34167c478bd9Sstevel@tonic-gate 		    ddi_get_instance(pcihp_p->dip), pci_dev);
34177c478bd9Sstevel@tonic-gate 	if (rv) {
34187c478bd9Sstevel@tonic-gate 		if (pcihp_cpci_blue_led)
34197c478bd9Sstevel@tonic-gate 			pcihp_hs_csr_op(pcihp_p, pci_dev,
342032d69156Sscarter 			    HPC_EVENT_SLOT_BLUE_LED_OFF);
34217c478bd9Sstevel@tonic-gate 	}
34227c478bd9Sstevel@tonic-gate 	/*
34237c478bd9Sstevel@tonic-gate 	 * we must clear interrupt in case the unconfigure didn't do it
34247c478bd9Sstevel@tonic-gate 	 * due to a duplicate interrupt. Extraction is success.
34257c478bd9Sstevel@tonic-gate 	 */
34267c478bd9Sstevel@tonic-gate 	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_SLOT_UNCONFIGURE);
34277c478bd9Sstevel@tonic-gate 
34287c478bd9Sstevel@tonic-gate 	if (!rv) {
34297c478bd9Sstevel@tonic-gate 		/*
34307c478bd9Sstevel@tonic-gate 		 * Sys Event Notification.
34317c478bd9Sstevel@tonic-gate 		 */
34327c478bd9Sstevel@tonic-gate 		pcihp_gen_sysevent(slotinfop->name, PCIHP_DR_AP_STATE_CHANGE,
343332d69156Sscarter 		    SE_HINT_REMOVE, pcihp_p->dip, kmflag);
34347c478bd9Sstevel@tonic-gate 	}
34357c478bd9Sstevel@tonic-gate 
34367c478bd9Sstevel@tonic-gate 	/*
34377c478bd9Sstevel@tonic-gate 	 * Enable interrupts back from this board.
34387c478bd9Sstevel@tonic-gate 	 * This could potentially be problematic in case if the user is
34397c478bd9Sstevel@tonic-gate 	 * quick enough to extract the board.
34407c478bd9Sstevel@tonic-gate 	 * But we must do it just in case if the switch is closed again.
34417c478bd9Sstevel@tonic-gate 	 */
34427c478bd9Sstevel@tonic-gate 	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_ENABLE_ENUM);
34437c478bd9Sstevel@tonic-gate 	slotinfop->slot_flags &= ~PCIHP_SLOT_ENUM_EXT_PENDING;
34447c478bd9Sstevel@tonic-gate 	return (rv);
34457c478bd9Sstevel@tonic-gate }
34467c478bd9Sstevel@tonic-gate 
34477c478bd9Sstevel@tonic-gate /*
34487c478bd9Sstevel@tonic-gate  * This routine is called when a ENUM# caused by when an adapter insertion
34497c478bd9Sstevel@tonic-gate  * is detected.  If the occupant is successfully configured (i.e. PCI resources
34507c478bd9Sstevel@tonic-gate  * successfully assigned, the blue LED is left off, otherwise if configuration
34517c478bd9Sstevel@tonic-gate  * is not successful, the blue LED is illuminated.
34527c478bd9Sstevel@tonic-gate  * Please note that the lock must be released before invoking the
34537c478bd9Sstevel@tonic-gate  * generic AP configure function.
34547c478bd9Sstevel@tonic-gate  */
34557c478bd9Sstevel@tonic-gate static int
pcihp_handle_enum_insertion(pcihp_t * pcihp_p,int pci_dev,int opcode,int kmflag)34567c478bd9Sstevel@tonic-gate pcihp_handle_enum_insertion(pcihp_t *pcihp_p, int pci_dev, int opcode,
3457*d5ebc493SDan Cross     int kmflag)
34587c478bd9Sstevel@tonic-gate {
34597c478bd9Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
34607c478bd9Sstevel@tonic-gate 	int rv = PCIHP_FAILURE;
34617c478bd9Sstevel@tonic-gate 	minor_t ap_minor;
34627c478bd9Sstevel@tonic-gate 	major_t ap_major;
34637c478bd9Sstevel@tonic-gate 
34647c478bd9Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
34657c478bd9Sstevel@tonic-gate 	slotinfop->hs_csr_location = 0;
34667c478bd9Sstevel@tonic-gate 	/* we clear the interrupt here. This is a must here. */
34677c478bd9Sstevel@tonic-gate 	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_SLOT_CONFIGURE);
34687c478bd9Sstevel@tonic-gate 	/*
34697c478bd9Sstevel@tonic-gate 	 * disable further interrupt from this board till it is
34707c478bd9Sstevel@tonic-gate 	 * configured.
34717c478bd9Sstevel@tonic-gate 	 */
34727c478bd9Sstevel@tonic-gate 	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_DISABLE_ENUM);
34737c478bd9Sstevel@tonic-gate 	if (opcode == PCIHP_CLEAR_ENUM) {
34747c478bd9Sstevel@tonic-gate 		slotinfop->slot_flags |= PCIHP_SLOT_ENUM_INS_PENDING;
34757c478bd9Sstevel@tonic-gate 		return (PCIHP_SUCCESS);
34767c478bd9Sstevel@tonic-gate 	}
34777c478bd9Sstevel@tonic-gate 
34787c478bd9Sstevel@tonic-gate 	if ((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) ==
347932d69156Sscarter 	    PCIHP_SLOT_AUTO_CFG_EN) {
348070025d76Sjohnny 		mutex_enter(&slotinfop->slot_mutex);
34817c478bd9Sstevel@tonic-gate 		rv = pcihp_configure_ap(pcihp_p, pci_dev);
348270025d76Sjohnny 		mutex_exit(&slotinfop->slot_mutex);
34837c478bd9Sstevel@tonic-gate 		if (rv != HPC_SUCCESS) {	/* configure failed */
34847c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "%s%d: PCI device %x Failed on"
348532d69156Sscarter 			    " Configure", ddi_driver_name(pcihp_p->dip),
348632d69156Sscarter 			    ddi_get_instance(pcihp_p->dip), pci_dev);
34877c478bd9Sstevel@tonic-gate 			if (pcihp_cpci_blue_led)
34887c478bd9Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
348932d69156Sscarter 				    HPC_EVENT_SLOT_BLUE_LED_ON);
34907c478bd9Sstevel@tonic-gate 		}
34917c478bd9Sstevel@tonic-gate 
34927c478bd9Sstevel@tonic-gate 		/* pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_CLEAR_ENUM); */
34937c478bd9Sstevel@tonic-gate 		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_ENABLE_ENUM);
34947c478bd9Sstevel@tonic-gate 
34957c478bd9Sstevel@tonic-gate 		if (!rv) {
34967c478bd9Sstevel@tonic-gate 			ap_major = ddi_driver_major(pcihp_p->dip);
34977c478bd9Sstevel@tonic-gate 			ap_minor = PCIHP_AP_MINOR_NUM(
34987c478bd9Sstevel@tonic-gate 			    ddi_get_instance(pcihp_p->dip), pci_dev);
34997c478bd9Sstevel@tonic-gate 			pcihp_create_occupant_props(pcihp_p->dip,
35007c478bd9Sstevel@tonic-gate 			    makedevice(ap_major, ap_minor), pci_dev);
35017c478bd9Sstevel@tonic-gate 
35027c478bd9Sstevel@tonic-gate 			/*
35037c478bd9Sstevel@tonic-gate 			 * Sys Event Notification.
35047c478bd9Sstevel@tonic-gate 			 */
35057c478bd9Sstevel@tonic-gate 			pcihp_gen_sysevent(slotinfop->name,
350632d69156Sscarter 			    PCIHP_DR_AP_STATE_CHANGE,
350732d69156Sscarter 			    SE_HINT_INSERT, pcihp_p->dip, kmflag);
35087c478bd9Sstevel@tonic-gate 		}
35097c478bd9Sstevel@tonic-gate 
35107c478bd9Sstevel@tonic-gate 	} else
35117c478bd9Sstevel@tonic-gate 		rv = PCIHP_SUCCESS;
35127c478bd9Sstevel@tonic-gate 	slotinfop->slot_flags &= ~PCIHP_SLOT_ENUM_INS_PENDING;
35137c478bd9Sstevel@tonic-gate 	return (rv);
35147c478bd9Sstevel@tonic-gate }
35157c478bd9Sstevel@tonic-gate 
35167c478bd9Sstevel@tonic-gate /*
35177c478bd9Sstevel@tonic-gate  * Read the Hot Swap Control and Status Register (HS_CSR) and
35187c478bd9Sstevel@tonic-gate  * place the result in the location pointed to be hs_csr.
35197c478bd9Sstevel@tonic-gate  */
35207c478bd9Sstevel@tonic-gate static int
pcihp_get_hs_csr(struct pcihp_slotinfo * slotinfop,ddi_acc_handle_t config_handle,uint8_t * hs_csr)35217c478bd9Sstevel@tonic-gate pcihp_get_hs_csr(struct pcihp_slotinfo *slotinfop,
35227c478bd9Sstevel@tonic-gate     ddi_acc_handle_t config_handle, uint8_t *hs_csr)
35237c478bd9Sstevel@tonic-gate {
35247c478bd9Sstevel@tonic-gate 	if (slotinfop->hs_csr_location == -1)
35257c478bd9Sstevel@tonic-gate 		return (PCIHP_FAILURE);
35267c478bd9Sstevel@tonic-gate 
35277c478bd9Sstevel@tonic-gate 	if (slotinfop->hs_csr_location == 0) {
35287c478bd9Sstevel@tonic-gate 		slotinfop->hs_csr_location =
352932d69156Sscarter 		    pcihp_get_hs_csr_location(config_handle);
35307c478bd9Sstevel@tonic-gate 
35317c478bd9Sstevel@tonic-gate 		if (slotinfop->hs_csr_location == -1)
35327c478bd9Sstevel@tonic-gate 			return (PCIHP_FAILURE);
35337c478bd9Sstevel@tonic-gate 	}
35347c478bd9Sstevel@tonic-gate 	*hs_csr = pci_config_get8(config_handle, slotinfop->hs_csr_location);
35357c478bd9Sstevel@tonic-gate 	return (PCIHP_SUCCESS);
35367c478bd9Sstevel@tonic-gate }
35377c478bd9Sstevel@tonic-gate 
35387c478bd9Sstevel@tonic-gate /*
35397c478bd9Sstevel@tonic-gate  * Write the Hot Swap Control and Status Register (HS_CSR) with
35407c478bd9Sstevel@tonic-gate  * the value being pointed at by hs_csr.
35417c478bd9Sstevel@tonic-gate  */
35427c478bd9Sstevel@tonic-gate static void
pcihp_set_hs_csr(struct pcihp_slotinfo * slotinfop,ddi_acc_handle_t config_handle,uint8_t * hs_csr)35437c478bd9Sstevel@tonic-gate pcihp_set_hs_csr(struct pcihp_slotinfo *slotinfop,
35447c478bd9Sstevel@tonic-gate     ddi_acc_handle_t config_handle, uint8_t *hs_csr)
35457c478bd9Sstevel@tonic-gate {
35467c478bd9Sstevel@tonic-gate 	if (slotinfop->hs_csr_location == -1)
35477c478bd9Sstevel@tonic-gate 		return;
35487c478bd9Sstevel@tonic-gate 	if (slotinfop->hs_csr_location == 0) {
35497c478bd9Sstevel@tonic-gate 		slotinfop->hs_csr_location =
355032d69156Sscarter 		    pcihp_get_hs_csr_location(config_handle);
35517c478bd9Sstevel@tonic-gate 		if (slotinfop->hs_csr_location == -1)
35527c478bd9Sstevel@tonic-gate 			return;
35537c478bd9Sstevel@tonic-gate 	}
35547c478bd9Sstevel@tonic-gate 	pci_config_put8(config_handle, slotinfop->hs_csr_location, *hs_csr);
35557c478bd9Sstevel@tonic-gate 	PCIHP_DEBUG((CE_NOTE, "hs_csr wrote %x, read %x", *hs_csr,
355632d69156Sscarter 	    pci_config_get8(config_handle, slotinfop->hs_csr_location)));
35577c478bd9Sstevel@tonic-gate }
35587c478bd9Sstevel@tonic-gate 
35597c478bd9Sstevel@tonic-gate static int
pcihp_get_hs_csr_location(ddi_acc_handle_t config_handle)35607c478bd9Sstevel@tonic-gate pcihp_get_hs_csr_location(ddi_acc_handle_t config_handle)
35617c478bd9Sstevel@tonic-gate {
35627c478bd9Sstevel@tonic-gate 	uint8_t	cap_id;
35637c478bd9Sstevel@tonic-gate 	uint_t	cap_id_loc;
35647c478bd9Sstevel@tonic-gate 	uint16_t	status;
35657c478bd9Sstevel@tonic-gate 	int location = -1;
35667c478bd9Sstevel@tonic-gate #define	PCI_STAT_ECP_SUPP	0x10
35677c478bd9Sstevel@tonic-gate 
35687c478bd9Sstevel@tonic-gate 	/*
35697c478bd9Sstevel@tonic-gate 	 * Need to check the Status register for ECP support first.
35707c478bd9Sstevel@tonic-gate 	 * Also please note that for type 1 devices, the
35717c478bd9Sstevel@tonic-gate 	 * offset could change. Should support type 1 next.
35727c478bd9Sstevel@tonic-gate 	 */
35737c478bd9Sstevel@tonic-gate 	status = pci_config_get16(config_handle, PCI_CONF_STAT);
35747c478bd9Sstevel@tonic-gate 	if (!(status & PCI_STAT_ECP_SUPP)) {
35757c478bd9Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "No Ext Capabilities for device\n"));
35767c478bd9Sstevel@tonic-gate 		return (-1);
35777c478bd9Sstevel@tonic-gate 	}
35787c478bd9Sstevel@tonic-gate 	cap_id_loc = pci_config_get8(config_handle, PCI_CONF_EXTCAP);
35797c478bd9Sstevel@tonic-gate 
35807c478bd9Sstevel@tonic-gate 	/*
35817c478bd9Sstevel@tonic-gate 	 * Walk the list of capabilities, but don't walk past the end
35827c478bd9Sstevel@tonic-gate 	 * of the Configuration Space Header.
35837c478bd9Sstevel@tonic-gate 	 */
35847c478bd9Sstevel@tonic-gate 	while ((cap_id_loc) && (cap_id_loc < PCI_CONF_HDR_SIZE)) {
35857c478bd9Sstevel@tonic-gate 
35867c478bd9Sstevel@tonic-gate 		cap_id = pci_config_get8(config_handle, cap_id_loc);
35877c478bd9Sstevel@tonic-gate 
35887c478bd9Sstevel@tonic-gate 		if (cap_id == CPCI_HOTSWAP_CAPID) {
35897c478bd9Sstevel@tonic-gate 			location = cap_id_loc + PCI_ECP_HS_CSR;
35907c478bd9Sstevel@tonic-gate 			break;
35917c478bd9Sstevel@tonic-gate 		}
35927c478bd9Sstevel@tonic-gate 		cap_id_loc = pci_config_get8(config_handle,
35937c478bd9Sstevel@tonic-gate 		    cap_id_loc + 1);
35947c478bd9Sstevel@tonic-gate 	}
35957c478bd9Sstevel@tonic-gate 	return (location);
35967c478bd9Sstevel@tonic-gate }
35977c478bd9Sstevel@tonic-gate 
35987c478bd9Sstevel@tonic-gate static int
pcihp_add_dummy_reg_property(dev_info_t * dip,uint_t bus,uint_t device,uint_t func)35997c478bd9Sstevel@tonic-gate pcihp_add_dummy_reg_property(dev_info_t *dip,
36007c478bd9Sstevel@tonic-gate     uint_t bus, uint_t device, uint_t func)
36017c478bd9Sstevel@tonic-gate {
36027c478bd9Sstevel@tonic-gate 	pci_regspec_t dummy_reg;
36037c478bd9Sstevel@tonic-gate 
36047c478bd9Sstevel@tonic-gate 	bzero(&dummy_reg, sizeof (dummy_reg));
36057c478bd9Sstevel@tonic-gate 
36067c478bd9Sstevel@tonic-gate 	dummy_reg.pci_phys_hi = PCIHP_MAKE_REG_HIGH(bus, device, func, 0);
36077c478bd9Sstevel@tonic-gate 
36087c478bd9Sstevel@tonic-gate 	return (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
36097c478bd9Sstevel@tonic-gate 	    "reg", (int *)&dummy_reg, sizeof (pci_regspec_t)/sizeof (int)));
36107c478bd9Sstevel@tonic-gate }
36117c478bd9Sstevel@tonic-gate 
36127c478bd9Sstevel@tonic-gate static void
pcihp_hs_csr_op(pcihp_t * pcihp_p,int pci_dev,int event)36137c478bd9Sstevel@tonic-gate pcihp_hs_csr_op(pcihp_t *pcihp_p, int pci_dev, int event)
36147c478bd9Sstevel@tonic-gate {
36157c478bd9Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
36167c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t config_handle;
36177c478bd9Sstevel@tonic-gate 	dev_info_t *dip, *new_child = NULL;
36187c478bd9Sstevel@tonic-gate 	uint8_t hs_csr;
36197c478bd9Sstevel@tonic-gate 	int result;
36207c478bd9Sstevel@tonic-gate 
36217c478bd9Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
36227c478bd9Sstevel@tonic-gate 
36237c478bd9Sstevel@tonic-gate 	if (pcihp_config_setup(&dip, &config_handle, &new_child, pci_dev,
362432d69156Sscarter 	    pcihp_p) != DDI_SUCCESS) {
36257c478bd9Sstevel@tonic-gate 		return;
36267c478bd9Sstevel@tonic-gate 	}
36277c478bd9Sstevel@tonic-gate 
36287c478bd9Sstevel@tonic-gate 	result = pcihp_get_hs_csr(slotinfop, config_handle, (uint8_t *)&hs_csr);
36297c478bd9Sstevel@tonic-gate 	if ((result != PCIHP_SUCCESS) || (event == -1)) {
36307c478bd9Sstevel@tonic-gate 		pcihp_config_teardown(&config_handle, &new_child, pci_dev,
363132d69156Sscarter 		    pcihp_p);
36327c478bd9Sstevel@tonic-gate 		return;
36337c478bd9Sstevel@tonic-gate 	}
36347c478bd9Sstevel@tonic-gate 
36357c478bd9Sstevel@tonic-gate 	hs_csr &= 0xf;
36367c478bd9Sstevel@tonic-gate 	switch (event) {
36377c478bd9Sstevel@tonic-gate 		case HPC_EVENT_SLOT_BLUE_LED_ON:
36387c478bd9Sstevel@tonic-gate 			hs_csr |= HS_CSR_LOO;
36397c478bd9Sstevel@tonic-gate 			break;
36407c478bd9Sstevel@tonic-gate 		case HPC_EVENT_SLOT_BLUE_LED_OFF:
36417c478bd9Sstevel@tonic-gate 			hs_csr &= ~HS_CSR_LOO;
36427c478bd9Sstevel@tonic-gate 			break;
36437c478bd9Sstevel@tonic-gate 		case HPC_EVENT_SLOT_CONFIGURE:
36447c478bd9Sstevel@tonic-gate 			hs_csr |= HS_CSR_INS;	/* clear INS */
36457c478bd9Sstevel@tonic-gate 			break;
36467c478bd9Sstevel@tonic-gate 		case HPC_EVENT_CLEAR_ENUM:
36477c478bd9Sstevel@tonic-gate 			hs_csr |= (HS_CSR_INS | HS_CSR_EXT);
36487c478bd9Sstevel@tonic-gate 			break;
36497c478bd9Sstevel@tonic-gate 		case HPC_EVENT_SLOT_UNCONFIGURE:
36507c478bd9Sstevel@tonic-gate 			hs_csr |= HS_CSR_EXT;	/* clear EXT */
36517c478bd9Sstevel@tonic-gate 			break;
36527c478bd9Sstevel@tonic-gate 		case HPC_EVENT_ENABLE_ENUM:
36537c478bd9Sstevel@tonic-gate 			hs_csr &= ~HS_CSR_EIM;
36547c478bd9Sstevel@tonic-gate 			break;
36557c478bd9Sstevel@tonic-gate 		case HPC_EVENT_DISABLE_ENUM:
36567c478bd9Sstevel@tonic-gate 			hs_csr |= HS_CSR_EIM;
36577c478bd9Sstevel@tonic-gate 			break;
36587c478bd9Sstevel@tonic-gate 		case HPC_EVENT_SLOT_NOT_HEALTHY:
36597c478bd9Sstevel@tonic-gate 		case HPC_EVENT_SLOT_HEALTHY_OK:
36607c478bd9Sstevel@tonic-gate 		default:
36617c478bd9Sstevel@tonic-gate 			break;
36627c478bd9Sstevel@tonic-gate 	}
36637c478bd9Sstevel@tonic-gate 	pcihp_set_hs_csr(slotinfop, config_handle, (uint8_t *)&hs_csr);
36647c478bd9Sstevel@tonic-gate 	pcihp_config_teardown(&config_handle, &new_child, pci_dev, pcihp_p);
36657c478bd9Sstevel@tonic-gate }
36667c478bd9Sstevel@tonic-gate 
36677c478bd9Sstevel@tonic-gate static int
pcihp_config_setup(dev_info_t ** dip,ddi_acc_handle_t * handle,dev_info_t ** new_child,int pci_dev,pcihp_t * pcihp_p)36687c478bd9Sstevel@tonic-gate pcihp_config_setup(dev_info_t **dip, ddi_acc_handle_t *handle,
3669*d5ebc493SDan Cross     dev_info_t **new_child, int pci_dev, pcihp_t *pcihp_p)
36707c478bd9Sstevel@tonic-gate {
36717c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = pcihp_p->dip;
36727c478bd9Sstevel@tonic-gate 	int bus, len, rc = DDI_SUCCESS;
36737c478bd9Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
36747c478bd9Sstevel@tonic-gate 	hpc_slot_state_t rstate;
36757c478bd9Sstevel@tonic-gate 	ddi_acc_hdl_t *hp;
367600d0963fSdilpreet 	pci_bus_range_t pci_bus_range;
36777c478bd9Sstevel@tonic-gate 
36787c478bd9Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
36797c478bd9Sstevel@tonic-gate 
36807c478bd9Sstevel@tonic-gate 	/*
36817c478bd9Sstevel@tonic-gate 	 * If declared failed, don't allow Config operations.
36827c478bd9Sstevel@tonic-gate 	 * Otherwise, if good or failing, it is assumed Ok
36837c478bd9Sstevel@tonic-gate 	 * to get config data.
36847c478bd9Sstevel@tonic-gate 	 */
36857c478bd9Sstevel@tonic-gate 	if (slotinfop->condition == AP_COND_FAILED) {
36867c478bd9Sstevel@tonic-gate 		return (PCIHP_FAILURE);
36877c478bd9Sstevel@tonic-gate 	}
36887c478bd9Sstevel@tonic-gate 	/*
36897c478bd9Sstevel@tonic-gate 	 * check to see if there is a hardware present first.
36907c478bd9Sstevel@tonic-gate 	 * If no hardware present, no need to probe this slot.
36917c478bd9Sstevel@tonic-gate 	 * We can do this first probably as a first step towards
36927c478bd9Sstevel@tonic-gate 	 * safeguarding from accidental removal (we don't support it!).
36937c478bd9Sstevel@tonic-gate 	 */
369432d69156Sscarter 	if (hpc_nexus_control(slotinfop->slot_hdl, HPC_CTRL_GET_SLOT_STATE,
369532d69156Sscarter 	    (caddr_t)&rstate) != 0) {
36967c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
36977c478bd9Sstevel@tonic-gate 	}
36987c478bd9Sstevel@tonic-gate 
36997c478bd9Sstevel@tonic-gate 	if (rstate != HPC_SLOT_CONNECTED) {
37007c478bd9Sstevel@tonic-gate 		/* error. slot must be connected */
37017c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
37027c478bd9Sstevel@tonic-gate 	}
37037c478bd9Sstevel@tonic-gate 	*new_child = NULL;
37047c478bd9Sstevel@tonic-gate 
37057c478bd9Sstevel@tonic-gate 	/*
37067c478bd9Sstevel@tonic-gate 	 * If there is no dip then we need to see if an
37077c478bd9Sstevel@tonic-gate 	 * adapter has just been hot plugged.
37087c478bd9Sstevel@tonic-gate 	 */
370900d0963fSdilpreet 	len = sizeof (pci_bus_range_t);
3710a3282898Scth 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, pdip,
37117c478bd9Sstevel@tonic-gate 	    0, "bus-range",
37127c478bd9Sstevel@tonic-gate 	    (caddr_t)&pci_bus_range, &len) != DDI_SUCCESS) {
37137c478bd9Sstevel@tonic-gate 
37147c478bd9Sstevel@tonic-gate 		return (PCIHP_FAILURE);
37157c478bd9Sstevel@tonic-gate 	}
37167c478bd9Sstevel@tonic-gate 
37177c478bd9Sstevel@tonic-gate 	/* primary bus number of this bus node */
37187c478bd9Sstevel@tonic-gate 	bus = pci_bus_range.lo;
37197c478bd9Sstevel@tonic-gate 
37207c478bd9Sstevel@tonic-gate 	if (ndi_devi_alloc(pdip, DEVI_PSEUDO_NEXNAME,
3721fa9e4066Sahrens 	    (pnode_t)DEVI_SID_NODEID, dip) != NDI_SUCCESS) {
37227c478bd9Sstevel@tonic-gate 
37237c478bd9Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "Failed to alloc probe node\n"));
37247c478bd9Sstevel@tonic-gate 		return (PCIHP_FAILURE);
37257c478bd9Sstevel@tonic-gate 	}
37267c478bd9Sstevel@tonic-gate 
37277c478bd9Sstevel@tonic-gate 	if (pcihp_add_dummy_reg_property(*dip, bus,
37287c478bd9Sstevel@tonic-gate 	    pci_dev, 0) != DDI_SUCCESS) {
37297c478bd9Sstevel@tonic-gate 
37307c478bd9Sstevel@tonic-gate 		(void) ndi_devi_free(*dip);
37317c478bd9Sstevel@tonic-gate 		return (PCIHP_FAILURE);
37327c478bd9Sstevel@tonic-gate 	}
37337c478bd9Sstevel@tonic-gate 
37347c478bd9Sstevel@tonic-gate 	/*
37357c478bd9Sstevel@tonic-gate 	 * Probe for a device. Possibly a non (c)PCI board could be sitting
37367c478bd9Sstevel@tonic-gate 	 * here which would never respond to PCI config cycles - in which
37377c478bd9Sstevel@tonic-gate 	 * case we return. Eventually a configure operation would fail.
37387c478bd9Sstevel@tonic-gate 	 */
37397c478bd9Sstevel@tonic-gate 	if (pci_config_setup(*dip, handle) != DDI_SUCCESS) {
37407c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "Cannot set config space map for"
37417c478bd9Sstevel@tonic-gate 		    " pci device number %d", pci_dev);
37427c478bd9Sstevel@tonic-gate 		(void) ndi_devi_free(*dip);
37437c478bd9Sstevel@tonic-gate 		return (PCIHP_FAILURE);
37447c478bd9Sstevel@tonic-gate 	}
37457c478bd9Sstevel@tonic-gate 
37467c478bd9Sstevel@tonic-gate 	/*
37477c478bd9Sstevel@tonic-gate 	 * See if there is any PCI HW at this location
37487c478bd9Sstevel@tonic-gate 	 * by reading the Vendor ID.  If it returns with 0xffff
37497c478bd9Sstevel@tonic-gate 	 * then there is no hardware at this location.
37507c478bd9Sstevel@tonic-gate 	 */
375170025d76Sjohnny 	if (pcihp_indirect_map(*dip) == DDI_SUCCESS) {
37527c478bd9Sstevel@tonic-gate 		if (pci_config_get16(*handle, 0) == 0xffff) {
37537c478bd9Sstevel@tonic-gate 			pci_config_teardown(handle);
37547c478bd9Sstevel@tonic-gate 			(void) ndi_devi_free(*dip);
37557c478bd9Sstevel@tonic-gate 			return (PCIHP_FAILURE);
37567c478bd9Sstevel@tonic-gate 		}
37577c478bd9Sstevel@tonic-gate 	} else {
37587c478bd9Sstevel@tonic-gate 		/* Check if mapping is OK */
37597c478bd9Sstevel@tonic-gate 		hp = impl_acc_hdl_get(*handle);
37607c478bd9Sstevel@tonic-gate 
37617c478bd9Sstevel@tonic-gate 		if (ddi_peek16(*dip, (int16_t *)(hp->ah_addr),
376232d69156Sscarter 		    (int16_t *)0) != DDI_SUCCESS) {
37637c478bd9Sstevel@tonic-gate #ifdef DEBUG
37647c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "Cannot Map PCI config space for "
37657c478bd9Sstevel@tonic-gate 			    "device number %d", pci_dev);
37667c478bd9Sstevel@tonic-gate #endif
37677c478bd9Sstevel@tonic-gate 			pci_config_teardown(handle);
37687c478bd9Sstevel@tonic-gate 			(void) ndi_devi_free(*dip);
37697c478bd9Sstevel@tonic-gate 			return (PCIHP_FAILURE);
37707c478bd9Sstevel@tonic-gate 		}
37717c478bd9Sstevel@tonic-gate 	}
37727c478bd9Sstevel@tonic-gate 
37737c478bd9Sstevel@tonic-gate 	*new_child = *dip;
37747c478bd9Sstevel@tonic-gate 	return (rc);
37757c478bd9Sstevel@tonic-gate 
37767c478bd9Sstevel@tonic-gate }
37777c478bd9Sstevel@tonic-gate 
37787c478bd9Sstevel@tonic-gate static void
pcihp_config_teardown(ddi_acc_handle_t * handle,dev_info_t ** new_child,int pci_dev,pcihp_t * pcihp_p)37797c478bd9Sstevel@tonic-gate pcihp_config_teardown(ddi_acc_handle_t *handle,
3780*d5ebc493SDan Cross     dev_info_t **new_child, int pci_dev, pcihp_t *pcihp_p)
37817c478bd9Sstevel@tonic-gate {
37827c478bd9Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop = &pcihp_p->slotinfo[pci_dev];
37837c478bd9Sstevel@tonic-gate 
37847c478bd9Sstevel@tonic-gate 	pci_config_teardown(handle);
37857c478bd9Sstevel@tonic-gate 	if (*new_child) {
37867c478bd9Sstevel@tonic-gate 		(void) ndi_devi_free(*new_child);
37877c478bd9Sstevel@tonic-gate 		/*
37887c478bd9Sstevel@tonic-gate 		 * If occupant not configured, reset HS_CSR location
37897c478bd9Sstevel@tonic-gate 		 * so that we reprobe. This covers cases where
37907c478bd9Sstevel@tonic-gate 		 * the receptacle had a status change without a
37917c478bd9Sstevel@tonic-gate 		 * notification to the framework.
37927c478bd9Sstevel@tonic-gate 		 */
37937c478bd9Sstevel@tonic-gate 		if (slotinfop->ostate != AP_OSTATE_CONFIGURED)
37947c478bd9Sstevel@tonic-gate 			slotinfop->hs_csr_location = 0;
37957c478bd9Sstevel@tonic-gate 	}
37967c478bd9Sstevel@tonic-gate }
37977c478bd9Sstevel@tonic-gate 
37987c478bd9Sstevel@tonic-gate static int
pcihp_get_board_type(struct pcihp_slotinfo * slotinfop)37997c478bd9Sstevel@tonic-gate pcihp_get_board_type(struct pcihp_slotinfo *slotinfop)
38007c478bd9Sstevel@tonic-gate {
38017c478bd9Sstevel@tonic-gate 	hpc_board_type_t board_type;
38027c478bd9Sstevel@tonic-gate 
38037c478bd9Sstevel@tonic-gate 	/*
38047c478bd9Sstevel@tonic-gate 	 * Get board type data structure, hpc_board_type_t.
38057c478bd9Sstevel@tonic-gate 	 */
38067c478bd9Sstevel@tonic-gate 	if (hpc_nexus_control(slotinfop->slot_hdl, HPC_CTRL_GET_BOARD_TYPE,
380732d69156Sscarter 	    (caddr_t)&board_type) != 0) {
38087c478bd9Sstevel@tonic-gate 
38097c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "Cannot Get Board Type..");
38107c478bd9Sstevel@tonic-gate 		return (-1);
38117c478bd9Sstevel@tonic-gate 	}
38127c478bd9Sstevel@tonic-gate 
38137c478bd9Sstevel@tonic-gate 	/*
38147c478bd9Sstevel@tonic-gate 	 * We expect the Hotswap Controller to tell us if the board is
38157c478bd9Sstevel@tonic-gate 	 * a hotswap board or not, as it probably cannot differentiate
38167c478bd9Sstevel@tonic-gate 	 * between a basic hotswap board, a non hotswap board and a
38177c478bd9Sstevel@tonic-gate 	 * hotswap nonfriendly board.
38187c478bd9Sstevel@tonic-gate 	 * So here is the logic to differentiate between the various
38197c478bd9Sstevel@tonic-gate 	 * types of cPCI boards.
38207c478bd9Sstevel@tonic-gate 	 * In case if the HSC returns board type as unknown, we assign
38217c478bd9Sstevel@tonic-gate 	 * the default board type as defined by a configurable variable
38227c478bd9Sstevel@tonic-gate 	 * for a BHS, nonfriendly FHS and non HS board.
38237c478bd9Sstevel@tonic-gate 	 */
38247c478bd9Sstevel@tonic-gate 	if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
38257c478bd9Sstevel@tonic-gate 		if (slotinfop->hs_csr_location > 0)
38267c478bd9Sstevel@tonic-gate 			board_type = HPC_BOARD_CPCI_FULL_HS;
38277c478bd9Sstevel@tonic-gate 		else {
38287c478bd9Sstevel@tonic-gate 			if (board_type == HPC_BOARD_CPCI_HS) {
382932d69156Sscarter 				if (slotinfop->hs_csr_location == -1)
383032d69156Sscarter 					board_type = HPC_BOARD_CPCI_BASIC_HS;
38317c478bd9Sstevel@tonic-gate 			}
38327c478bd9Sstevel@tonic-gate 			if (board_type == HPC_BOARD_UNKNOWN) {
383332d69156Sscarter 				if (slotinfop->hs_csr_location == -1) {
383432d69156Sscarter 					board_type = pcihp_cpci_board_type;
383532d69156Sscarter 				} else if (slotinfop->hs_csr_location != 0) {
383632d69156Sscarter 					board_type = HPC_BOARD_CPCI_FULL_HS;
38377c478bd9Sstevel@tonic-gate 				}
38387c478bd9Sstevel@tonic-gate 			}
38397c478bd9Sstevel@tonic-gate 		}
38407c478bd9Sstevel@tonic-gate 		/*
38417c478bd9Sstevel@tonic-gate 		 * If board type is a non hotswap board, then we must
38427c478bd9Sstevel@tonic-gate 		 * deny a unconfigure operation. So set this flag.
38437c478bd9Sstevel@tonic-gate 		 * Strictly speaking, there is no reason not to disallow
38447c478bd9Sstevel@tonic-gate 		 * a unconfigure operation on nonhotswap boards. But this
38457c478bd9Sstevel@tonic-gate 		 * is the only way we can prevent a user from accidentally
38467c478bd9Sstevel@tonic-gate 		 * removing the board and damaging it.
38477c478bd9Sstevel@tonic-gate 		 */
38487c478bd9Sstevel@tonic-gate 		if (board_type == HPC_BOARD_CPCI_NON_HS)
384932d69156Sscarter 			slotinfop->slot_flags |= PCIHP_SLOT_DEV_NON_HOTPLUG;
38507c478bd9Sstevel@tonic-gate 		else
385132d69156Sscarter 			slotinfop->slot_flags &= ~PCIHP_SLOT_DEV_NON_HOTPLUG;
38527c478bd9Sstevel@tonic-gate 	}
38537c478bd9Sstevel@tonic-gate 	return (board_type);
38547c478bd9Sstevel@tonic-gate }
38557c478bd9Sstevel@tonic-gate 
38567c478bd9Sstevel@tonic-gate 
38577c478bd9Sstevel@tonic-gate /*
38587c478bd9Sstevel@tonic-gate  * Generate the System Event with a possible hint.
38597c478bd9Sstevel@tonic-gate  */
38607c478bd9Sstevel@tonic-gate static void
pcihp_gen_sysevent(char * slot_name,int event_sub_class,int hint,dev_info_t * self,int kmflag)38617c478bd9Sstevel@tonic-gate pcihp_gen_sysevent(char *slot_name, int event_sub_class, int hint,
3862*d5ebc493SDan Cross     dev_info_t *self, int kmflag)
38637c478bd9Sstevel@tonic-gate {
38647c478bd9Sstevel@tonic-gate 
38657c478bd9Sstevel@tonic-gate 	int err;
38667c478bd9Sstevel@tonic-gate 	char *ev_subclass = NULL;
38677c478bd9Sstevel@tonic-gate 	sysevent_id_t eid;
38687c478bd9Sstevel@tonic-gate 	nvlist_t *ev_attr_list = NULL;
38697c478bd9Sstevel@tonic-gate 	char attach_pnt[MAXPATHLEN];
38707c478bd9Sstevel@tonic-gate 
38717c478bd9Sstevel@tonic-gate 	/*
38727c478bd9Sstevel@tonic-gate 	 * Minor device name (AP) will be bus path
38737c478bd9Sstevel@tonic-gate 	 * concatenated with slot name
38747c478bd9Sstevel@tonic-gate 	 */
38757c478bd9Sstevel@tonic-gate 
38767c478bd9Sstevel@tonic-gate 	(void) strcpy(attach_pnt, PCIHP_DEVICES_STR);
38777c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(self, attach_pnt + strlen(PCIHP_DEVICES_STR));
38787c478bd9Sstevel@tonic-gate 	(void) strcat(attach_pnt, ":");
38797c478bd9Sstevel@tonic-gate 	(void) strcat(attach_pnt, slot_name);
38807c478bd9Sstevel@tonic-gate 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, kmflag);
38817c478bd9Sstevel@tonic-gate 	if (err != 0) {
38827c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
38837c478bd9Sstevel@tonic-gate 		    "%s%d: Failed to allocate memory "
38847c478bd9Sstevel@tonic-gate 		    "for event attributes%s", ddi_driver_name(self),
38857c478bd9Sstevel@tonic-gate 		    ddi_get_instance(self), ESC_DR_AP_STATE_CHANGE);
38867c478bd9Sstevel@tonic-gate 		return;
38877c478bd9Sstevel@tonic-gate 	}
38887c478bd9Sstevel@tonic-gate 
38897c478bd9Sstevel@tonic-gate 	switch (event_sub_class) {
38907c478bd9Sstevel@tonic-gate 
38917c478bd9Sstevel@tonic-gate 	/* event sub class: ESC_DR_AP_STATE_CHANGE */
38927c478bd9Sstevel@tonic-gate 	case PCIHP_DR_AP_STATE_CHANGE:
38937c478bd9Sstevel@tonic-gate 
38947c478bd9Sstevel@tonic-gate 		ev_subclass = ESC_DR_AP_STATE_CHANGE;
38957c478bd9Sstevel@tonic-gate 
38967c478bd9Sstevel@tonic-gate 		switch (hint) {
38977c478bd9Sstevel@tonic-gate 
38987c478bd9Sstevel@tonic-gate 		case SE_NO_HINT:	/* fall through */
38997c478bd9Sstevel@tonic-gate 		case SE_HINT_INSERT:	/* fall through */
39007c478bd9Sstevel@tonic-gate 		case SE_HINT_REMOVE:
39017c478bd9Sstevel@tonic-gate 
39027c478bd9Sstevel@tonic-gate 
39037c478bd9Sstevel@tonic-gate 			err = nvlist_add_string(ev_attr_list, DR_HINT,
39047c478bd9Sstevel@tonic-gate 			    SE_HINT2STR(hint));
39057c478bd9Sstevel@tonic-gate 
39067c478bd9Sstevel@tonic-gate 			if (err != 0) {
39077c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s%d: Failed to add attr [%s]"
390832d69156Sscarter 				    " for %s event", ddi_driver_name(self),
390932d69156Sscarter 				    ddi_get_instance(self),
391032d69156Sscarter 				    DR_HINT, ESC_DR_AP_STATE_CHANGE);
39117c478bd9Sstevel@tonic-gate 				nvlist_free(ev_attr_list);
39127c478bd9Sstevel@tonic-gate 				return;
39137c478bd9Sstevel@tonic-gate 			}
39147c478bd9Sstevel@tonic-gate 			break;
39157c478bd9Sstevel@tonic-gate 
39167c478bd9Sstevel@tonic-gate 		default:
39177c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: Unknown hint on sysevent",
391832d69156Sscarter 			    ddi_driver_name(self), ddi_get_instance(self));
39197c478bd9Sstevel@tonic-gate 			nvlist_free(ev_attr_list);
39207c478bd9Sstevel@tonic-gate 			return;
39217c478bd9Sstevel@tonic-gate 		}
39227c478bd9Sstevel@tonic-gate 
39237c478bd9Sstevel@tonic-gate 		break;
39247c478bd9Sstevel@tonic-gate 
39257c478bd9Sstevel@tonic-gate 	/* event sub class: ESC_DR_REQ */
39267c478bd9Sstevel@tonic-gate 	case PCIHP_DR_REQ:
39277c478bd9Sstevel@tonic-gate 
39287c478bd9Sstevel@tonic-gate 		ev_subclass = ESC_DR_REQ;
39297c478bd9Sstevel@tonic-gate 
39307c478bd9Sstevel@tonic-gate 		switch (hint) {
39317c478bd9Sstevel@tonic-gate 
39327c478bd9Sstevel@tonic-gate 		case SE_INVESTIGATE_RES:	/* fall through */
39337c478bd9Sstevel@tonic-gate 		case SE_INCOMING_RES:	/* fall through */
39347c478bd9Sstevel@tonic-gate 		case SE_OUTGOING_RES:	/* fall through */
39357c478bd9Sstevel@tonic-gate 
39367c478bd9Sstevel@tonic-gate 			err = nvlist_add_string(ev_attr_list, DR_REQ_TYPE,
393732d69156Sscarter 			    SE_REQ2STR(hint));
39387c478bd9Sstevel@tonic-gate 
39397c478bd9Sstevel@tonic-gate 			if (err != 0) {
39407c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
394132d69156Sscarter 				    "%s%d: Failed to add attr [%s] "
394232d69156Sscarter 				    "for %s event", ddi_driver_name(self),
394332d69156Sscarter 				    ddi_get_instance(self),
394432d69156Sscarter 				    DR_REQ_TYPE, ESC_DR_REQ);
39457c478bd9Sstevel@tonic-gate 				nvlist_free(ev_attr_list);
39467c478bd9Sstevel@tonic-gate 				return;
39477c478bd9Sstevel@tonic-gate 			}
39487c478bd9Sstevel@tonic-gate 			break;
39497c478bd9Sstevel@tonic-gate 
39507c478bd9Sstevel@tonic-gate 		default:
395132d69156Sscarter 			cmn_err(CE_WARN, "%s%d:  Unknown hint on sysevent",
395232d69156Sscarter 			    ddi_driver_name(self), ddi_get_instance(self));
39537c478bd9Sstevel@tonic-gate 			nvlist_free(ev_attr_list);
39547c478bd9Sstevel@tonic-gate 			return;
39557c478bd9Sstevel@tonic-gate 		}
39567c478bd9Sstevel@tonic-gate 
39577c478bd9Sstevel@tonic-gate 		break;
39587c478bd9Sstevel@tonic-gate 
39597c478bd9Sstevel@tonic-gate 	default:
396032d69156Sscarter 		cmn_err(CE_WARN, "%s%d:  Unknown Event subclass",
396132d69156Sscarter 		    ddi_driver_name(self), ddi_get_instance(self));
39627c478bd9Sstevel@tonic-gate 		nvlist_free(ev_attr_list);
39637c478bd9Sstevel@tonic-gate 		return;
39647c478bd9Sstevel@tonic-gate 	}
39657c478bd9Sstevel@tonic-gate 
39667c478bd9Sstevel@tonic-gate 	/*
39677c478bd9Sstevel@tonic-gate 	 * Add attachment point as attribute (common attribute)
39687c478bd9Sstevel@tonic-gate 	 */
39697c478bd9Sstevel@tonic-gate 
39707c478bd9Sstevel@tonic-gate 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, attach_pnt);
39717c478bd9Sstevel@tonic-gate 
39727c478bd9Sstevel@tonic-gate 	if (err != 0) {
397332d69156Sscarter 		cmn_err(CE_WARN, "%s%d: Failed to add attr [%s] for %s event",
397432d69156Sscarter 		    ddi_driver_name(self), ddi_get_instance(self),
397532d69156Sscarter 		    DR_AP_ID, EC_DR);
39767c478bd9Sstevel@tonic-gate 		nvlist_free(ev_attr_list);
39777c478bd9Sstevel@tonic-gate 		return;
39787c478bd9Sstevel@tonic-gate 	}
39797c478bd9Sstevel@tonic-gate 
39807c478bd9Sstevel@tonic-gate 
39817c478bd9Sstevel@tonic-gate 	/*
39827c478bd9Sstevel@tonic-gate 	 * Log this event with sysevent framework.
39837c478bd9Sstevel@tonic-gate 	 */
39847c478bd9Sstevel@tonic-gate 
39857c478bd9Sstevel@tonic-gate 	err = ddi_log_sysevent(self, DDI_VENDOR_SUNW, EC_DR,
39867c478bd9Sstevel@tonic-gate 	    ev_subclass, ev_attr_list, &eid,
39877c478bd9Sstevel@tonic-gate 	    ((kmflag == KM_SLEEP) ? DDI_SLEEP : DDI_NOSLEEP));
39887c478bd9Sstevel@tonic-gate 	if (err != 0) {
39897c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: Failed to log %s event",
399032d69156Sscarter 		    ddi_driver_name(self), ddi_get_instance(self), EC_DR);
39917c478bd9Sstevel@tonic-gate 	}
39927c478bd9Sstevel@tonic-gate 
39937c478bd9Sstevel@tonic-gate 	nvlist_free(ev_attr_list);
39947c478bd9Sstevel@tonic-gate }
39957c478bd9Sstevel@tonic-gate 
39967c478bd9Sstevel@tonic-gate int
pcihp_prop_op(dev_t dev,dev_info_t * dip,ddi_prop_op_t prop_op,int flags,char * name,caddr_t valuep,int * lengthp)39977c478bd9Sstevel@tonic-gate pcihp_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
39987c478bd9Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp)
39997c478bd9Sstevel@tonic-gate {
40007c478bd9Sstevel@tonic-gate 	int pci_dev;
40017c478bd9Sstevel@tonic-gate 
40027c478bd9Sstevel@tonic-gate 	if (dev == DDI_DEV_T_ANY)
40037c478bd9Sstevel@tonic-gate 		goto skip;
40047c478bd9Sstevel@tonic-gate 
40057c478bd9Sstevel@tonic-gate 	if (strcmp(name, "pci-occupant") == 0) {
40067c478bd9Sstevel@tonic-gate 		pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(getminor(dev));
4007656d7645SChris Horne 		pcihp_create_occupant_props(dip, dev, pci_dev);
40087c478bd9Sstevel@tonic-gate 	}
40097c478bd9Sstevel@tonic-gate 	/* other cases... */
40107c478bd9Sstevel@tonic-gate skip:
40117c478bd9Sstevel@tonic-gate 	return (ddi_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp));
40127c478bd9Sstevel@tonic-gate }
40137c478bd9Sstevel@tonic-gate 
40147c478bd9Sstevel@tonic-gate /*
40157c478bd9Sstevel@tonic-gate  * this function is called only for SPARC platforms, where we may have
40167c478bd9Sstevel@tonic-gate  * a mix n' match of direct vs indirectly mapped configuration space.
40177c478bd9Sstevel@tonic-gate  * On x86, this function should always return success since the configuration
40187c478bd9Sstevel@tonic-gate  * space is always indirect mapped.
40197c478bd9Sstevel@tonic-gate  */
40207c478bd9Sstevel@tonic-gate /*ARGSUSED*/
40217c478bd9Sstevel@tonic-gate static int
pcihp_indirect_map(dev_info_t * dip)40227c478bd9Sstevel@tonic-gate pcihp_indirect_map(dev_info_t *dip)
40237c478bd9Sstevel@tonic-gate {
40247c478bd9Sstevel@tonic-gate #if defined(__sparc)
40257c478bd9Sstevel@tonic-gate 	int rc = DDI_FAILURE;
40267c478bd9Sstevel@tonic-gate 
40277c478bd9Sstevel@tonic-gate 	if (ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(dip), 0,
402832d69156Sscarter 	    PCI_DEV_CONF_MAP_PROP, DDI_FAILURE) != DDI_FAILURE)
40297c478bd9Sstevel@tonic-gate 		rc = DDI_SUCCESS;
40307c478bd9Sstevel@tonic-gate 	else
40317c478bd9Sstevel@tonic-gate 		if (ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(dip),
403232d69156Sscarter 		    0, PCI_BUS_CONF_MAP_PROP, DDI_FAILURE) != DDI_FAILURE)
40337c478bd9Sstevel@tonic-gate 			rc = DDI_SUCCESS;
40347c478bd9Sstevel@tonic-gate 	return (rc);
40357c478bd9Sstevel@tonic-gate #else
40367c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
40377c478bd9Sstevel@tonic-gate #endif
40387c478bd9Sstevel@tonic-gate }
4039