1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * **********************************************************************
31*0Sstevel@tonic-gate  * Extension module for PCI nexus drivers to support PCI Hot Plug feature.
32*0Sstevel@tonic-gate  *
33*0Sstevel@tonic-gate  * DESCRIPTION:
34*0Sstevel@tonic-gate  *    This module basically implements "devctl" and Attachment Point device
35*0Sstevel@tonic-gate  *    nodes for hot plug operations. The cb_ops functions needed for access
36*0Sstevel@tonic-gate  *    to these device nodes are also implemented. For hotplug operations
37*0Sstevel@tonic-gate  *    on Attachment Points it interacts with the hotplug services (HPS)
38*0Sstevel@tonic-gate  *    framework. A pci nexus driver would simply call pcihp_init() in its
39*0Sstevel@tonic-gate  *    attach() function and pcihp_uninit() call in its detach() function.
40*0Sstevel@tonic-gate  * **********************************************************************
41*0Sstevel@tonic-gate  */
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #include <sys/conf.h>
44*0Sstevel@tonic-gate #include <sys/kmem.h>
45*0Sstevel@tonic-gate #include <sys/debug.h>
46*0Sstevel@tonic-gate #include <sys/modctl.h>
47*0Sstevel@tonic-gate #include <sys/autoconf.h>
48*0Sstevel@tonic-gate #include <sys/ddi.h>
49*0Sstevel@tonic-gate #include <sys/sunddi.h>
50*0Sstevel@tonic-gate #include <sys/sunndi.h>
51*0Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
52*0Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
53*0Sstevel@tonic-gate #include <sys/ddipropdefs.h>
54*0Sstevel@tonic-gate #include <sys/open.h>
55*0Sstevel@tonic-gate #include <sys/file.h>
56*0Sstevel@tonic-gate #include <sys/stat.h>
57*0Sstevel@tonic-gate #include <sys/pci.h>
58*0Sstevel@tonic-gate #include <sys/pci_impl.h>
59*0Sstevel@tonic-gate #include <sys/devctl.h>
60*0Sstevel@tonic-gate #include <sys/hotplug/hpcsvc.h>
61*0Sstevel@tonic-gate #include <sys/hotplug/pci/pcicfg.h>
62*0Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h>
63*0Sstevel@tonic-gate #include <sys/sysevent.h>
64*0Sstevel@tonic-gate #include <sys/sysevent/eventdefs.h>
65*0Sstevel@tonic-gate #include <sys/sysevent/dr.h>
66*0Sstevel@tonic-gate #include <sys/fs/dv_node.h>
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate  * NOTE:
70*0Sstevel@tonic-gate  * This module depends on PCI Configurator module (misc/pcicfg),
71*0Sstevel@tonic-gate  * Hot Plug Services framework module (misc/hpcsvc) and Bus Resource
72*0Sstevel@tonic-gate  * Allocator module (misc/busra).
73*0Sstevel@tonic-gate  */
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate /*
76*0Sstevel@tonic-gate  * ************************************************************************
77*0Sstevel@tonic-gate  * *** Implementation specific data structures/definitions.		***
78*0Sstevel@tonic-gate  * ************************************************************************
79*0Sstevel@tonic-gate  */
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate /* soft state */
82*0Sstevel@tonic-gate typedef enum { PCIHP_SOFT_STATE_CLOSED, PCIHP_SOFT_STATE_OPEN,
83*0Sstevel@tonic-gate 		PCIHP_SOFT_STATE_OPEN_EXCL } pcihp_soft_state_t;
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate #define	PCI_MAX_DEVS	32	/* max. number of devices on a pci bus */
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate /* the following correspond to sysevent defined subclasses */
88*0Sstevel@tonic-gate #define	PCIHP_DR_AP_STATE_CHANGE	0
89*0Sstevel@tonic-gate #define	PCIHP_DR_REQ			1
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate /*  pcihp_get_soft_state() command argument */
92*0Sstevel@tonic-gate #define	PCIHP_DR_NOOP			0
93*0Sstevel@tonic-gate #define	PCIHP_DR_BUS_CONFIGURE		1
94*0Sstevel@tonic-gate #define	PCIHP_DR_BUS_UNCONFIGURE	2
95*0Sstevel@tonic-gate #define	PCIHP_DR_SLOT_ENTER		4
96*0Sstevel@tonic-gate #define	PCIHP_DR_SLOT_EXIT		8
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate /*  hot plug bus state */
99*0Sstevel@tonic-gate enum { PCIHP_BUS_INITIALIZING, PCIHP_BUS_UNCONFIGURED,
100*0Sstevel@tonic-gate 		PCIHP_BUS_CONFIGURED };
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate /*
103*0Sstevel@tonic-gate  * Soft state structure associated with each hot plug pci bus instance.
104*0Sstevel@tonic-gate  */
105*0Sstevel@tonic-gate typedef struct pcihp {
106*0Sstevel@tonic-gate 	struct pcihp		*nextp;
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	/* devinfo pointer to the pci bus node */
109*0Sstevel@tonic-gate 	dev_info_t		*dip;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	/* soft state flags: PCIHP_SOFT_STATE_* */
112*0Sstevel@tonic-gate 	pcihp_soft_state_t	soft_state;
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	/* global mutex to serialize exclusive access to the bus */
115*0Sstevel@tonic-gate 	kmutex_t		mutex;
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	/* slot information structure */
118*0Sstevel@tonic-gate 	struct pcihp_slotinfo {
119*0Sstevel@tonic-gate 		hpc_slot_t	slot_hdl;	/* HPS slot handle */
120*0Sstevel@tonic-gate 		ap_rstate_t	rstate;		/* state of Receptacle */
121*0Sstevel@tonic-gate 		ap_ostate_t	ostate;		/* state of the Occupant */
122*0Sstevel@tonic-gate 		ap_condition_t	condition;	/* condition of the occupant */
123*0Sstevel@tonic-gate 		time32_t	last_change;	/* XXX needed? */
124*0Sstevel@tonic-gate 		uint32_t	event_mask;	/* last event mask registered */
125*0Sstevel@tonic-gate 		char		*name;		/* slot logical name */
126*0Sstevel@tonic-gate 		uint_t		slot_flags;
127*0Sstevel@tonic-gate 		uint16_t	slot_type;	/* slot type: pci or cpci */
128*0Sstevel@tonic-gate 		uint16_t	slot_capabilities; /* 64bit, etc. */
129*0Sstevel@tonic-gate 		int		hs_csr_location; /* Location of HS_CSR */
130*0Sstevel@tonic-gate 		kmutex_t	slot_mutex;	/* mutex to serialize hotplug */
131*0Sstevel@tonic-gate 						/* operations on the slot */
132*0Sstevel@tonic-gate 	} slotinfo[PCI_MAX_DEVS];
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	/* misc. bus attributes */
135*0Sstevel@tonic-gate 	uint_t			bus_flags;
136*0Sstevel@tonic-gate 	uint_t			bus_state;
137*0Sstevel@tonic-gate 	uint_t			slots_active;
138*0Sstevel@tonic-gate } pcihp_t;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate /*
141*0Sstevel@tonic-gate  * Bit definitions for slot_flags field:
142*0Sstevel@tonic-gate  *
143*0Sstevel@tonic-gate  *	PCIHP_SLOT_AUTO_CFG_EN	This flags is set if nexus can do auto
144*0Sstevel@tonic-gate  *				configuration of hot plugged card on this slot
145*0Sstevel@tonic-gate  *				if the hardware reports the hot plug events.
146*0Sstevel@tonic-gate  *
147*0Sstevel@tonic-gate  *	PCIHP_SLOT_DISABLED	Slot is disabled for hotplug operations.
148*0Sstevel@tonic-gate  *
149*0Sstevel@tonic-gate  *	PCIHP_SLOT_NOT_HEALTHY	HEALTHY# signal is not OK on this slot.
150*0Sstevel@tonic-gate  */
151*0Sstevel@tonic-gate #define	PCIHP_SLOT_AUTO_CFG_EN		0x1
152*0Sstevel@tonic-gate #define	PCIHP_SLOT_DISABLED		0x2
153*0Sstevel@tonic-gate #define	PCIHP_SLOT_NOT_HEALTHY		0x4
154*0Sstevel@tonic-gate #define	PCIHP_SLOT_DEV_NON_HOTPLUG	0x8
155*0Sstevel@tonic-gate #define	PCIHP_SLOT_ENUM_INS_PENDING	0x10
156*0Sstevel@tonic-gate #define	PCIHP_SLOT_ENUM_EXT_PENDING	0x20
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate /*
159*0Sstevel@tonic-gate  * Bit definitions for bus_flags field:
160*0Sstevel@tonic-gate  *
161*0Sstevel@tonic-gate  *	PCIHP_BUS_66MHZ	Bus is running at 66Mhz.
162*0Sstevel@tonic-gate  */
163*0Sstevel@tonic-gate #define	PCIHP_BUS_66MHZ		0x1
164*0Sstevel@tonic-gate #define	PCIHP_BUS_ENUM_RADIAL	0x2
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate #define	PCIHP_DEVICES_STR		"/devices"
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate /*
169*0Sstevel@tonic-gate  * control structure for tree walk during configure/unconfigure operation.
170*0Sstevel@tonic-gate  */
171*0Sstevel@tonic-gate struct pcihp_config_ctrl {
172*0Sstevel@tonic-gate 	int	pci_dev;	/* PCI device number for the slot */
173*0Sstevel@tonic-gate 	uint_t	flags;		/* control flags (see below) */
174*0Sstevel@tonic-gate 	int	op;		/* operation: PCIHP_ONLINE or PCIHP_OFFLINE */
175*0Sstevel@tonic-gate 	int	rv;		/* return error code */
176*0Sstevel@tonic-gate 	dev_info_t *dip;	/* dip at which the (first) error occurred */
177*0Sstevel@tonic-gate 	hpc_occupant_info_t *occupant;
178*0Sstevel@tonic-gate };
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate /*
181*0Sstevel@tonic-gate  * control flags for configure/unconfigure operations on the tree.
182*0Sstevel@tonic-gate  *
183*0Sstevel@tonic-gate  * PCIHP_CFG_CONTINUE	continue the operation ignoring errors
184*0Sstevel@tonic-gate  */
185*0Sstevel@tonic-gate #define	PCIHP_CFG_CONTINUE	0x1
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate #define	PCIHP_ONLINE	1
188*0Sstevel@tonic-gate #define	PCIHP_OFFLINE	0
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate /* Leaf ops (hotplug controls for target devices) */
192*0Sstevel@tonic-gate static int pcihp_open(dev_t *, int, int, cred_t *);
193*0Sstevel@tonic-gate static int pcihp_close(dev_t, int, int, cred_t *);
194*0Sstevel@tonic-gate static int pcihp_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate #ifdef DEBUG
197*0Sstevel@tonic-gate static int pcihp_debug = 0;
198*0Sstevel@tonic-gate #define	PCIHP_DEBUG(args)	if (pcihp_debug >= 1) cmn_err args
199*0Sstevel@tonic-gate #define	PCIHP_DEBUG2(args)	if (pcihp_debug >= 2) cmn_err args
200*0Sstevel@tonic-gate #else
201*0Sstevel@tonic-gate #define	PCIHP_DEBUG(args)
202*0Sstevel@tonic-gate #define	PCIHP_DEBUG2(args)
203*0Sstevel@tonic-gate #endif
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate /*
206*0Sstevel@tonic-gate  * We process ENUM# event one device at a time ie. as soon as we detect
207*0Sstevel@tonic-gate  * that a device has the right ENUM# conditions, we return. If the following
208*0Sstevel@tonic-gate  * variable is set to non-zero, we scan all the devices on the bus
209*0Sstevel@tonic-gate  * for ENUM# conditions.
210*0Sstevel@tonic-gate  */
211*0Sstevel@tonic-gate static int pcihp_enum_scan_all = 0;
212*0Sstevel@tonic-gate /*
213*0Sstevel@tonic-gate  * If HSC driver cannot determine the board type (for example: it may not
214*0Sstevel@tonic-gate  * be possible to differentiate between a Basic Hotswap, Non Hotswap or
215*0Sstevel@tonic-gate  * Non-friendly Full hotswap board), the default board type is assigned
216*0Sstevel@tonic-gate  * to be as defined by the following variable.
217*0Sstevel@tonic-gate  */
218*0Sstevel@tonic-gate static int pcihp_cpci_board_type = HPC_BOARD_CPCI_NON_HS;
219*0Sstevel@tonic-gate static int pcihp_cpci_led_blink = 30;
220*0Sstevel@tonic-gate /*
221*0Sstevel@tonic-gate  * It was noted that the blue LED when written on/off would cause INS/EXT
222*0Sstevel@tonic-gate  * bit to be set causing an extra interrupt. Although the cPCI specifications
223*0Sstevel@tonic-gate  * does not imply this, this behavior is seen with some FHS silicons.
224*0Sstevel@tonic-gate  * Also, handling the INS/EXT bit would control the LED being On/Off.
225*0Sstevel@tonic-gate  * Until the behavior is confirmed, this flag could be used to enable or
226*0Sstevel@tonic-gate  * disable handling the LED.
227*0Sstevel@tonic-gate  * 0 means the silicons handles the LED behavior via the INS/EXT bit.
228*0Sstevel@tonic-gate  * 1 means the software must explicitly do the LED behavior.
229*0Sstevel@tonic-gate  */
230*0Sstevel@tonic-gate static int pcihp_cpci_blue_led = 1;
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate /* static functions */
233*0Sstevel@tonic-gate static pcihp_t *pcihp_create_soft_state(dev_info_t *dip);
234*0Sstevel@tonic-gate static void pcihp_destroy_soft_state(dev_info_t *dip);
235*0Sstevel@tonic-gate static pcihp_t *pcihp_get_soft_state(dev_info_t *dip, int cmd, int *rv);
236*0Sstevel@tonic-gate static int pcihp_configure_ap(pcihp_t *pcihp_p, int pci_dev);
237*0Sstevel@tonic-gate static int pcihp_unconfigure_ap(pcihp_t *pcihp_p, int pci_dev);
238*0Sstevel@tonic-gate static int pcihp_new_slot_state(dev_info_t *, hpc_slot_t,
239*0Sstevel@tonic-gate 	hpc_slot_info_t *, int);
240*0Sstevel@tonic-gate static int pcihp_configure(dev_info_t *, void *);
241*0Sstevel@tonic-gate static int pcihp_event_handler(caddr_t, uint_t);
242*0Sstevel@tonic-gate static dev_info_t *pcihp_devi_find(dev_info_t *dip, uint_t dev, uint_t func);
243*0Sstevel@tonic-gate static int pcihp_match_dev(dev_info_t *dip, void *hdl);
244*0Sstevel@tonic-gate static int pcihp_get_hs_csr(struct pcihp_slotinfo *, ddi_acc_handle_t,
245*0Sstevel@tonic-gate 	uint8_t *);
246*0Sstevel@tonic-gate static void pcihp_set_hs_csr(struct pcihp_slotinfo *, ddi_acc_handle_t,
247*0Sstevel@tonic-gate 	uint8_t *);
248*0Sstevel@tonic-gate static int pcihp_get_hs_csr_location(ddi_acc_handle_t);
249*0Sstevel@tonic-gate static int pcihp_handle_enum(pcihp_t *, int, int, int);
250*0Sstevel@tonic-gate static void pcihp_hs_csr_op(pcihp_t *, int, int);
251*0Sstevel@tonic-gate static int pcihp_enum_slot(pcihp_t *, struct pcihp_slotinfo *, int, int, int);
252*0Sstevel@tonic-gate static int pcihp_handle_enum_extraction(pcihp_t *, int, int, int);
253*0Sstevel@tonic-gate static int pcihp_handle_enum_insertion(pcihp_t *, int, int, int);
254*0Sstevel@tonic-gate static int pcihp_add_dummy_reg_property(dev_info_t *, uint_t, uint_t, uint_t);
255*0Sstevel@tonic-gate static int pcihp_config_setup(dev_info_t **, ddi_acc_handle_t *,
256*0Sstevel@tonic-gate 			dev_info_t **, int, pcihp_t *);
257*0Sstevel@tonic-gate static void pcihp_config_teardown(ddi_acc_handle_t *,
258*0Sstevel@tonic-gate 			dev_info_t **, int, pcihp_t *);
259*0Sstevel@tonic-gate static int pcihp_get_board_type(struct pcihp_slotinfo *);
260*0Sstevel@tonic-gate /* sysevent function */
261*0Sstevel@tonic-gate static void pcihp_gen_sysevent(char *, int, int, dev_info_t *, int);
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate extern int pcicfg_configure(dev_info_t *, uint_t);
264*0Sstevel@tonic-gate extern int pcicfg_unconfigure(dev_info_t *, uint_t);
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate static int pcihp_list_occupants(dev_info_t *, void *);
267*0Sstevel@tonic-gate static int pcihp_indirect_map(dev_info_t *dip);
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate #if 0
270*0Sstevel@tonic-gate static void pcihp_probe_slot_state(dev_info_t *, int, hpc_slot_state_t *);
271*0Sstevel@tonic-gate #endif
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate int pcihp_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
274*0Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp);
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate struct cb_ops pcihp_cb_ops = {
277*0Sstevel@tonic-gate 	pcihp_open,			/* open */
278*0Sstevel@tonic-gate 	pcihp_close,			/* close */
279*0Sstevel@tonic-gate 	nodev,				/* strategy */
280*0Sstevel@tonic-gate 	nodev,				/* print */
281*0Sstevel@tonic-gate 	nodev,				/* dump */
282*0Sstevel@tonic-gate 	nodev,				/* read */
283*0Sstevel@tonic-gate 	nodev,				/* write */
284*0Sstevel@tonic-gate 	pcihp_ioctl,			/* ioctl */
285*0Sstevel@tonic-gate 	nodev,				/* devmap */
286*0Sstevel@tonic-gate 	nodev,				/* mmap */
287*0Sstevel@tonic-gate 	nodev,				/* segmap */
288*0Sstevel@tonic-gate 	nochpoll,			/* poll */
289*0Sstevel@tonic-gate 	pcihp_prop_op,			/* cb_prop_op */
290*0Sstevel@tonic-gate 	NULL,				/* streamtab */
291*0Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
292*0Sstevel@tonic-gate 	CB_REV,				/* rev */
293*0Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
294*0Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
295*0Sstevel@tonic-gate };
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate /*
298*0Sstevel@tonic-gate  * local data
299*0Sstevel@tonic-gate  */
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate int pcihp_autocfg_enabled = 1; /* auto config is enabled by default */
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate static kmutex_t pcihp_mutex; /* mutex to protect the following data */
304*0Sstevel@tonic-gate static pcihp_t *pcihp_head = NULL;
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate static kmutex_t pcihp_open_mutex; /* mutex to protect open/close/uninit */
307*0Sstevel@tonic-gate static int	pci_devlink_flags = 0;
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate /*
310*0Sstevel@tonic-gate  * Module linkage information for the kernel.
311*0Sstevel@tonic-gate  */
312*0Sstevel@tonic-gate extern struct mod_ops mod_miscops;
313*0Sstevel@tonic-gate static struct modlmisc modlmisc = {
314*0Sstevel@tonic-gate 	&mod_miscops,
315*0Sstevel@tonic-gate 	"PCI nexus hotplug support v%I%",
316*0Sstevel@tonic-gate };
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
319*0Sstevel@tonic-gate 	MODREV_1,
320*0Sstevel@tonic-gate 	&modlmisc,
321*0Sstevel@tonic-gate 	NULL
322*0Sstevel@tonic-gate };
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate int
325*0Sstevel@tonic-gate _init(void)
326*0Sstevel@tonic-gate {
327*0Sstevel@tonic-gate 	int error;
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate 	mutex_init(&pcihp_mutex, NULL, MUTEX_DRIVER, NULL);
330*0Sstevel@tonic-gate 	mutex_init(&pcihp_open_mutex, NULL, MUTEX_DRIVER, NULL);
331*0Sstevel@tonic-gate 	if ((error = mod_install(&modlinkage)) != 0) {
332*0Sstevel@tonic-gate 		mutex_destroy(&pcihp_open_mutex);
333*0Sstevel@tonic-gate 		mutex_destroy(&pcihp_mutex);
334*0Sstevel@tonic-gate 	}
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 	return (error);
337*0Sstevel@tonic-gate }
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate int
340*0Sstevel@tonic-gate _fini(void)
341*0Sstevel@tonic-gate {
342*0Sstevel@tonic-gate 	return (EBUSY);
343*0Sstevel@tonic-gate }
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate int
346*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
347*0Sstevel@tonic-gate {
348*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
349*0Sstevel@tonic-gate }
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate static	pcihp_t *
352*0Sstevel@tonic-gate pcihp_create_soft_state(
353*0Sstevel@tonic-gate 	dev_info_t *dip)
354*0Sstevel@tonic-gate {
355*0Sstevel@tonic-gate 	pcihp_t	*pcihp_p;
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	pcihp_p = kmem_zalloc(sizeof (struct pcihp), KM_SLEEP);
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 	pcihp_p->dip = dip;
360*0Sstevel@tonic-gate 	mutex_init(&pcihp_p->mutex, NULL, MUTEX_DRIVER, NULL);
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate 	mutex_enter(&pcihp_mutex);
363*0Sstevel@tonic-gate 	pcihp_p->nextp = pcihp_head;
364*0Sstevel@tonic-gate 	pcihp_head = pcihp_p;
365*0Sstevel@tonic-gate 	pcihp_p->bus_state = PCIHP_BUS_INITIALIZING;
366*0Sstevel@tonic-gate 	pcihp_p->slots_active = 0;
367*0Sstevel@tonic-gate 	mutex_exit(&pcihp_mutex);
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 	return (pcihp_p);
370*0Sstevel@tonic-gate }
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate static	void
373*0Sstevel@tonic-gate pcihp_destroy_soft_state(
374*0Sstevel@tonic-gate 	dev_info_t *dip)
375*0Sstevel@tonic-gate {
376*0Sstevel@tonic-gate 	pcihp_t	*p;
377*0Sstevel@tonic-gate 	pcihp_t	**pp;
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 	mutex_enter(&pcihp_mutex);
380*0Sstevel@tonic-gate 	pp = &pcihp_head;
381*0Sstevel@tonic-gate 	while ((p = *pp) != NULL) {
382*0Sstevel@tonic-gate 		if (p->dip == dip) {
383*0Sstevel@tonic-gate 			*pp = p->nextp;
384*0Sstevel@tonic-gate 			kmem_free(p, sizeof (struct pcihp));
385*0Sstevel@tonic-gate 			break;
386*0Sstevel@tonic-gate 		}
387*0Sstevel@tonic-gate 		pp = &(p->nextp);
388*0Sstevel@tonic-gate 	}
389*0Sstevel@tonic-gate 	mutex_exit(&pcihp_mutex);
390*0Sstevel@tonic-gate }
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate /*
393*0Sstevel@tonic-gate  * This function should be imported by client nexus drivers as their
394*0Sstevel@tonic-gate  * devo_getinfo() entry point.
395*0Sstevel@tonic-gate  */
396*0Sstevel@tonic-gate 
397*0Sstevel@tonic-gate /* ARGSUSED */
398*0Sstevel@tonic-gate int
399*0Sstevel@tonic-gate pcihp_info(
400*0Sstevel@tonic-gate 	dev_info_t	*dip,
401*0Sstevel@tonic-gate 	ddi_info_cmd_t	cmd,
402*0Sstevel@tonic-gate 	void		*arg,
403*0Sstevel@tonic-gate 	void		**result)
404*0Sstevel@tonic-gate {
405*0Sstevel@tonic-gate 	pcihp_t		*pcihp_p;
406*0Sstevel@tonic-gate 	major_t		major;
407*0Sstevel@tonic-gate 	minor_t		minor;
408*0Sstevel@tonic-gate 	int		instance;
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate 	major = getmajor((dev_t)arg);
411*0Sstevel@tonic-gate 	minor = getminor((dev_t)arg);
412*0Sstevel@tonic-gate 	instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 	switch (cmd) {
415*0Sstevel@tonic-gate 	default:
416*0Sstevel@tonic-gate 		return (DDI_FAILURE);
417*0Sstevel@tonic-gate 
418*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
419*0Sstevel@tonic-gate 		*result = (void *)(intptr_t)instance;
420*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
421*0Sstevel@tonic-gate 
422*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
423*0Sstevel@tonic-gate 		mutex_enter(&pcihp_mutex);
424*0Sstevel@tonic-gate 		pcihp_p = pcihp_head;
425*0Sstevel@tonic-gate 		while (pcihp_p != NULL) {
426*0Sstevel@tonic-gate 			if (ddi_name_to_major(ddi_get_name(pcihp_p->dip)) ==
427*0Sstevel@tonic-gate 			    major && ddi_get_instance(pcihp_p->dip) ==
428*0Sstevel@tonic-gate 			    instance) {
429*0Sstevel@tonic-gate 				*result = (void *)pcihp_p->dip;
430*0Sstevel@tonic-gate 				mutex_exit(&pcihp_mutex);
431*0Sstevel@tonic-gate 				return (DDI_SUCCESS);
432*0Sstevel@tonic-gate 			}
433*0Sstevel@tonic-gate 			pcihp_p = pcihp_p->nextp;
434*0Sstevel@tonic-gate 		}
435*0Sstevel@tonic-gate 		mutex_exit(&pcihp_mutex);
436*0Sstevel@tonic-gate 		return (DDI_FAILURE);
437*0Sstevel@tonic-gate 	}
438*0Sstevel@tonic-gate }
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate /*
441*0Sstevel@tonic-gate  * This function retrieves the hot plug soft state and performs the
442*0Sstevel@tonic-gate  * following primitive commands while the soft state is locked:
443*0Sstevel@tonic-gate  * mark the bus unconfigured, increment slot activity, decrement
444*0Sstevel@tonic-gate  * slot activity and noop.
445*0Sstevel@tonic-gate  */
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate /* ARGSUSED */
448*0Sstevel@tonic-gate static	pcihp_t *
449*0Sstevel@tonic-gate pcihp_get_soft_state(
450*0Sstevel@tonic-gate 	dev_info_t	*dip, int cmd, int *rv)
451*0Sstevel@tonic-gate {
452*0Sstevel@tonic-gate 	pcihp_t		*pcihp_p;
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate 	*rv = PCIHP_SUCCESS;
455*0Sstevel@tonic-gate 	mutex_enter(&pcihp_mutex);
456*0Sstevel@tonic-gate 	pcihp_p = pcihp_head;
457*0Sstevel@tonic-gate 	while (pcihp_p != NULL) {
458*0Sstevel@tonic-gate 		if (pcihp_p->dip == dip) {
459*0Sstevel@tonic-gate 			switch (cmd) {
460*0Sstevel@tonic-gate 			case PCIHP_DR_BUS_UNCONFIGURE:
461*0Sstevel@tonic-gate 				if (pcihp_p->slots_active == 0)
462*0Sstevel@tonic-gate 					pcihp_p->bus_state =
463*0Sstevel@tonic-gate 					    PCIHP_BUS_UNCONFIGURED;
464*0Sstevel@tonic-gate 				else
465*0Sstevel@tonic-gate 					*rv = PCIHP_FAILURE;
466*0Sstevel@tonic-gate 				break;
467*0Sstevel@tonic-gate 			case PCIHP_DR_SLOT_ENTER:
468*0Sstevel@tonic-gate 				if (pcihp_p->bus_state ==
469*0Sstevel@tonic-gate 				    PCIHP_BUS_UNCONFIGURED)
470*0Sstevel@tonic-gate 					*rv = PCIHP_FAILURE;
471*0Sstevel@tonic-gate 				else
472*0Sstevel@tonic-gate 					pcihp_p->slots_active++;
473*0Sstevel@tonic-gate 				break;
474*0Sstevel@tonic-gate 			case PCIHP_DR_SLOT_EXIT:
475*0Sstevel@tonic-gate 				ASSERT(pcihp_p->slots_active > 0);
476*0Sstevel@tonic-gate 				if (pcihp_p->slots_active == 0)
477*0Sstevel@tonic-gate 					cmn_err(CE_PANIC,
478*0Sstevel@tonic-gate 					    "pcihp (%s%d): mismatched slot"
479*0Sstevel@tonic-gate 					    " activity",
480*0Sstevel@tonic-gate 					    ddi_driver_name(dip),
481*0Sstevel@tonic-gate 					    ddi_get_instance(dip));
482*0Sstevel@tonic-gate 				else
483*0Sstevel@tonic-gate 					pcihp_p->slots_active--;
484*0Sstevel@tonic-gate 				break;
485*0Sstevel@tonic-gate 			case PCIHP_DR_NOOP:
486*0Sstevel@tonic-gate 				break;
487*0Sstevel@tonic-gate 			default:
488*0Sstevel@tonic-gate 				*rv = PCIHP_FAILURE;
489*0Sstevel@tonic-gate 				break;
490*0Sstevel@tonic-gate 			}
491*0Sstevel@tonic-gate 			mutex_exit(&pcihp_mutex);
492*0Sstevel@tonic-gate 			return (pcihp_p);
493*0Sstevel@tonic-gate 		}
494*0Sstevel@tonic-gate 		pcihp_p = pcihp_p->nextp;
495*0Sstevel@tonic-gate 	}
496*0Sstevel@tonic-gate 	mutex_exit(&pcihp_mutex);
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate 	return (NULL);
499*0Sstevel@tonic-gate }
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate /* ARGSUSED3 */
502*0Sstevel@tonic-gate static int
503*0Sstevel@tonic-gate pcihp_open(dev_t *devp, int flags, int otyp, cred_t *credp)
504*0Sstevel@tonic-gate {
505*0Sstevel@tonic-gate 	dev_info_t *self;
506*0Sstevel@tonic-gate 	pcihp_t *pcihp_p;
507*0Sstevel@tonic-gate 	minor_t	minor;
508*0Sstevel@tonic-gate 	int pci_dev;
509*0Sstevel@tonic-gate 	int rv;
510*0Sstevel@tonic-gate 
511*0Sstevel@tonic-gate 	/*
512*0Sstevel@tonic-gate 	 * Make sure the open is for the right file type.
513*0Sstevel@tonic-gate 	 */
514*0Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
515*0Sstevel@tonic-gate 		return (EINVAL);
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate 	mutex_enter(&pcihp_open_mutex);
518*0Sstevel@tonic-gate 	/*
519*0Sstevel@tonic-gate 	 * Get the soft state structure.
520*0Sstevel@tonic-gate 	 */
521*0Sstevel@tonic-gate 	if (pcihp_info(NULL, DDI_INFO_DEVT2DEVINFO, (void *)*devp,
522*0Sstevel@tonic-gate 	    (void **)&self) != DDI_SUCCESS) {
523*0Sstevel@tonic-gate 		mutex_exit(&pcihp_open_mutex);
524*0Sstevel@tonic-gate 		return (ENXIO);
525*0Sstevel@tonic-gate 	}
526*0Sstevel@tonic-gate 
527*0Sstevel@tonic-gate 	pcihp_p = pcihp_get_soft_state(self, PCIHP_DR_NOOP, &rv);
528*0Sstevel@tonic-gate 	ASSERT(pcihp_p != NULL);
529*0Sstevel@tonic-gate 
530*0Sstevel@tonic-gate 	mutex_enter(&pcihp_p->mutex);
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate 	/*
533*0Sstevel@tonic-gate 	 * If the pci_dev is valid then the minor device is an
534*0Sstevel@tonic-gate 	 * AP. Otherwise it is ":devctl" minor device.
535*0Sstevel@tonic-gate 	 */
536*0Sstevel@tonic-gate 	minor = getminor(*devp);
537*0Sstevel@tonic-gate 	pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(minor);
538*0Sstevel@tonic-gate 	if (pci_dev < PCI_MAX_DEVS) {
539*0Sstevel@tonic-gate 		struct pcihp_slotinfo *slotinfop;
540*0Sstevel@tonic-gate 
541*0Sstevel@tonic-gate 		slotinfop = &pcihp_p->slotinfo[pci_dev];
542*0Sstevel@tonic-gate 		if (slotinfop->slot_hdl == NULL) {
543*0Sstevel@tonic-gate 			mutex_exit(&pcihp_p->mutex);
544*0Sstevel@tonic-gate 			mutex_exit(&pcihp_open_mutex);
545*0Sstevel@tonic-gate 			return (ENXIO);
546*0Sstevel@tonic-gate 		}
547*0Sstevel@tonic-gate 	}
548*0Sstevel@tonic-gate 
549*0Sstevel@tonic-gate 	/*
550*0Sstevel@tonic-gate 	 * Handle the open by tracking the device state.
551*0Sstevel@tonic-gate 	 *
552*0Sstevel@tonic-gate 	 * Note: Needs review w.r.t exclusive access to AP or the bus.
553*0Sstevel@tonic-gate 	 * Currently in the pci plug-in we don't use EXCL open at all
554*0Sstevel@tonic-gate 	 * so the code below implements EXCL access on the bus.
555*0Sstevel@tonic-gate 	 */
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate 	/* enforce exclusive access to the bus */
558*0Sstevel@tonic-gate 	if ((pcihp_p->soft_state == PCIHP_SOFT_STATE_OPEN_EXCL) ||
559*0Sstevel@tonic-gate 	    ((flags & FEXCL) &&
560*0Sstevel@tonic-gate 	    (pcihp_p->soft_state != PCIHP_SOFT_STATE_CLOSED))) {
561*0Sstevel@tonic-gate 		mutex_exit(&pcihp_p->mutex);
562*0Sstevel@tonic-gate 		mutex_exit(&pcihp_open_mutex);
563*0Sstevel@tonic-gate 		return (EBUSY);
564*0Sstevel@tonic-gate 	}
565*0Sstevel@tonic-gate 
566*0Sstevel@tonic-gate 	if (flags & FEXCL)
567*0Sstevel@tonic-gate 		pcihp_p->soft_state = PCIHP_SOFT_STATE_OPEN_EXCL;
568*0Sstevel@tonic-gate 	else
569*0Sstevel@tonic-gate 		pcihp_p->soft_state = PCIHP_SOFT_STATE_OPEN;
570*0Sstevel@tonic-gate 
571*0Sstevel@tonic-gate 	mutex_exit(&pcihp_p->mutex);
572*0Sstevel@tonic-gate 	mutex_exit(&pcihp_open_mutex);
573*0Sstevel@tonic-gate 
574*0Sstevel@tonic-gate 	return (0);
575*0Sstevel@tonic-gate }
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate /* ARGSUSED */
578*0Sstevel@tonic-gate static int
579*0Sstevel@tonic-gate pcihp_close(dev_t dev, int flags, int otyp, cred_t *credp)
580*0Sstevel@tonic-gate {
581*0Sstevel@tonic-gate 	dev_info_t *self;
582*0Sstevel@tonic-gate 	pcihp_t *pcihp_p;
583*0Sstevel@tonic-gate 	int rv;
584*0Sstevel@tonic-gate 
585*0Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
586*0Sstevel@tonic-gate 		return (EINVAL);
587*0Sstevel@tonic-gate 
588*0Sstevel@tonic-gate 	mutex_enter(&pcihp_open_mutex);
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate 	if (pcihp_info(NULL, DDI_INFO_DEVT2DEVINFO, (void *)dev,
591*0Sstevel@tonic-gate 	    (void **)&self) != DDI_SUCCESS) {
592*0Sstevel@tonic-gate 		mutex_exit(&pcihp_open_mutex);
593*0Sstevel@tonic-gate 		return (ENXIO);
594*0Sstevel@tonic-gate 	}
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 	pcihp_p = pcihp_get_soft_state(self, PCIHP_DR_NOOP, &rv);
597*0Sstevel@tonic-gate 	ASSERT(pcihp_p != NULL);
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 	mutex_enter(&pcihp_p->mutex);
600*0Sstevel@tonic-gate 	pcihp_p->soft_state = PCIHP_SOFT_STATE_CLOSED;
601*0Sstevel@tonic-gate 	mutex_exit(&pcihp_p->mutex);
602*0Sstevel@tonic-gate 
603*0Sstevel@tonic-gate 	mutex_exit(&pcihp_open_mutex);
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate 	return (0);
606*0Sstevel@tonic-gate }
607*0Sstevel@tonic-gate 
608*0Sstevel@tonic-gate static int
609*0Sstevel@tonic-gate pcihp_list_occupants(dev_info_t *dip, void *hdl)
610*0Sstevel@tonic-gate {
611*0Sstevel@tonic-gate 	int pci_dev;
612*0Sstevel@tonic-gate 	struct pcihp_config_ctrl *ctrl = (struct pcihp_config_ctrl *)hdl;
613*0Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
614*0Sstevel@tonic-gate 	int length;
615*0Sstevel@tonic-gate 	major_t major;
616*0Sstevel@tonic-gate 
617*0Sstevel@tonic-gate 	/*
618*0Sstevel@tonic-gate 	 * Get the PCI device number information from the devinfo
619*0Sstevel@tonic-gate 	 * node. Since the node may not have the address field
620*0Sstevel@tonic-gate 	 * setup (this is done in the DDI_INITCHILD of the parent)
621*0Sstevel@tonic-gate 	 * we look up the 'reg' property to decode that information.
622*0Sstevel@tonic-gate 	 */
623*0Sstevel@tonic-gate 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
624*0Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "reg", (int **)&pci_rp,
625*0Sstevel@tonic-gate 	    (uint_t *)&length) != DDI_PROP_SUCCESS) {
626*0Sstevel@tonic-gate 		ctrl->rv = DDI_FAILURE;
627*0Sstevel@tonic-gate 		ctrl->dip = dip;
628*0Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
629*0Sstevel@tonic-gate 	}
630*0Sstevel@tonic-gate 
631*0Sstevel@tonic-gate 	/* get the pci device id information */
632*0Sstevel@tonic-gate 	pci_dev = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
633*0Sstevel@tonic-gate 
634*0Sstevel@tonic-gate 	/*
635*0Sstevel@tonic-gate 	 * free the memory allocated by ddi_prop_lookup_int_array
636*0Sstevel@tonic-gate 	 */
637*0Sstevel@tonic-gate 	ddi_prop_free(pci_rp);
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate 	/*
640*0Sstevel@tonic-gate 	 * Match the node for the device number of the slot.
641*0Sstevel@tonic-gate 	 */
642*0Sstevel@tonic-gate 	if (pci_dev == ctrl->pci_dev) { /* node is a match */
643*0Sstevel@tonic-gate 
644*0Sstevel@tonic-gate 		major = ddi_driver_major(dip);
645*0Sstevel@tonic-gate 
646*0Sstevel@tonic-gate 		/*
647*0Sstevel@tonic-gate 		 * If the node is not yet attached, then don't list it
648*0Sstevel@tonic-gate 		 * as an occupant. This is valid, since nothing can be
649*0Sstevel@tonic-gate 		 * consuming it until it is attached, and cfgadm will
650*0Sstevel@tonic-gate 		 * ask for the property explicitly which will cause it
651*0Sstevel@tonic-gate 		 * to be re-freshed right before checking with rcm.
652*0Sstevel@tonic-gate 		 */
653*0Sstevel@tonic-gate 		if ((major == -1) || (i_ddi_node_state(dip) < DS_ATTACHED))
654*0Sstevel@tonic-gate 			return (DDI_WALK_PRUNECHILD);
655*0Sstevel@tonic-gate 
656*0Sstevel@tonic-gate 		ctrl->occupant->id[ctrl->occupant->i] =
657*0Sstevel@tonic-gate 		    kmem_alloc(sizeof (char[MAXPATHLEN]), KM_SLEEP);
658*0Sstevel@tonic-gate 
659*0Sstevel@tonic-gate 		/*
660*0Sstevel@tonic-gate 		 * No need to hold the dip as ddi_walk_devs
661*0Sstevel@tonic-gate 		 * has already arranged that for us.
662*0Sstevel@tonic-gate 		 */
663*0Sstevel@tonic-gate 		(void) ddi_pathname(dip,
664*0Sstevel@tonic-gate 		    (char *)ctrl->occupant->id[ctrl->occupant->i]);
665*0Sstevel@tonic-gate 		ctrl->occupant->i++;
666*0Sstevel@tonic-gate 	}
667*0Sstevel@tonic-gate 
668*0Sstevel@tonic-gate 	/*
669*0Sstevel@tonic-gate 	 * continue the walk to the next sibling to look for a match
670*0Sstevel@tonic-gate 	 * or to find other nodes if this card is a multi-function card.
671*0Sstevel@tonic-gate 	 */
672*0Sstevel@tonic-gate 	return (DDI_WALK_PRUNECHILD);
673*0Sstevel@tonic-gate }
674*0Sstevel@tonic-gate 
675*0Sstevel@tonic-gate static void
676*0Sstevel@tonic-gate pcihp_create_occupant_props_nolock(dev_info_t *self, dev_t dev, int pci_dev)
677*0Sstevel@tonic-gate {
678*0Sstevel@tonic-gate 	struct pcihp_config_ctrl ctrl;
679*0Sstevel@tonic-gate 	hpc_occupant_info_t *occupant;
680*0Sstevel@tonic-gate 	int i;
681*0Sstevel@tonic-gate 
682*0Sstevel@tonic-gate 	occupant = kmem_alloc(sizeof (hpc_occupant_info_t), KM_SLEEP);
683*0Sstevel@tonic-gate 	occupant->i = 0;
684*0Sstevel@tonic-gate 
685*0Sstevel@tonic-gate 	ctrl.flags = 0;
686*0Sstevel@tonic-gate 	ctrl.dip = NULL;
687*0Sstevel@tonic-gate 	ctrl.rv = NDI_SUCCESS;
688*0Sstevel@tonic-gate 	ctrl.pci_dev = pci_dev;
689*0Sstevel@tonic-gate 	ctrl.op = 55; /* should define DRYRUN */
690*0Sstevel@tonic-gate 	ctrl.occupant = occupant;
691*0Sstevel@tonic-gate 
692*0Sstevel@tonic-gate 	ddi_walk_devs(ddi_get_child(self), pcihp_list_occupants,
693*0Sstevel@tonic-gate 	    (void *)&ctrl);
694*0Sstevel@tonic-gate 
695*0Sstevel@tonic-gate 	if (occupant->i == 0) {
696*0Sstevel@tonic-gate 		/* no occupants right now, need to create stub property */
697*0Sstevel@tonic-gate 		char *c[] = { "" };
698*0Sstevel@tonic-gate 		(void) ddi_prop_update_string_array(dev, self, "pci-occupant",
699*0Sstevel@tonic-gate 		    c, 1);
700*0Sstevel@tonic-gate 	} else {
701*0Sstevel@tonic-gate 		(void) ddi_prop_update_string_array(dev, self, "pci-occupant",
702*0Sstevel@tonic-gate 		    occupant->id, occupant->i);
703*0Sstevel@tonic-gate 	}
704*0Sstevel@tonic-gate 	for (i = 0; i < occupant->i; i++) {
705*0Sstevel@tonic-gate 		kmem_free(occupant->id[i], sizeof (char[MAXPATHLEN]));
706*0Sstevel@tonic-gate 	}
707*0Sstevel@tonic-gate 
708*0Sstevel@tonic-gate 	kmem_free(occupant, sizeof (hpc_occupant_info_t));
709*0Sstevel@tonic-gate }
710*0Sstevel@tonic-gate 
711*0Sstevel@tonic-gate static void
712*0Sstevel@tonic-gate pcihp_create_occupant_props(dev_info_t *self, dev_t dev, int pci_dev)
713*0Sstevel@tonic-gate {
714*0Sstevel@tonic-gate 	int circular;
715*0Sstevel@tonic-gate 
716*0Sstevel@tonic-gate 	ndi_devi_enter(self, &circular);
717*0Sstevel@tonic-gate 	pcihp_create_occupant_props_nolock(self, dev, pci_dev);
718*0Sstevel@tonic-gate 	ndi_devi_exit(self, circular);
719*0Sstevel@tonic-gate }
720*0Sstevel@tonic-gate 
721*0Sstevel@tonic-gate static void
722*0Sstevel@tonic-gate pcihp_delete_occupant_props(dev_info_t *dip, dev_t dev)
723*0Sstevel@tonic-gate {
724*0Sstevel@tonic-gate 	if (ddi_prop_remove(dev, dip, "pci-occupant")
725*0Sstevel@tonic-gate 	    != DDI_PROP_SUCCESS)
726*0Sstevel@tonic-gate 		return; /* add error handling */
727*0Sstevel@tonic-gate 
728*0Sstevel@tonic-gate }
729*0Sstevel@tonic-gate 
730*0Sstevel@tonic-gate /*
731*0Sstevel@tonic-gate  * pcihp_ioctl: devctl hotplug controls
732*0Sstevel@tonic-gate  */
733*0Sstevel@tonic-gate /* ARGSUSED */
734*0Sstevel@tonic-gate static int
735*0Sstevel@tonic-gate pcihp_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
736*0Sstevel@tonic-gate 	int *rvalp)
737*0Sstevel@tonic-gate {
738*0Sstevel@tonic-gate 	pcihp_t *pcihp_p;
739*0Sstevel@tonic-gate 	dev_info_t *self;
740*0Sstevel@tonic-gate 	struct devctl_iocdata *dcp;
741*0Sstevel@tonic-gate 	uint_t bus_state;
742*0Sstevel@tonic-gate 	int rv = 0;
743*0Sstevel@tonic-gate 	int pci_dev;
744*0Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
745*0Sstevel@tonic-gate 	hpc_slot_state_t rstate;
746*0Sstevel@tonic-gate 	devctl_ap_state_t ap_state;
747*0Sstevel@tonic-gate 	struct hpc_control_data hpc_ctrldata;
748*0Sstevel@tonic-gate 	struct hpc_led_info led_info;
749*0Sstevel@tonic-gate 	time_t time;
750*0Sstevel@tonic-gate 	int state_locking;
751*0Sstevel@tonic-gate 	int state_unlocking;
752*0Sstevel@tonic-gate 	int rval;
753*0Sstevel@tonic-gate 	char *pathname = NULL;
754*0Sstevel@tonic-gate 
755*0Sstevel@tonic-gate 	/*
756*0Sstevel@tonic-gate 	 * read devctl ioctl data before soft state retrieval
757*0Sstevel@tonic-gate 	 */
758*0Sstevel@tonic-gate 	if ((cmd != DEVCTL_AP_CONTROL) &&
759*0Sstevel@tonic-gate 	    ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
760*0Sstevel@tonic-gate 		return (EFAULT);
761*0Sstevel@tonic-gate 
762*0Sstevel@tonic-gate 	if (pcihp_info(NULL, DDI_INFO_DEVT2DEVINFO, (void *)dev,
763*0Sstevel@tonic-gate 	    (void **)&self) != DDI_SUCCESS) {
764*0Sstevel@tonic-gate 		if (cmd != DEVCTL_AP_CONTROL)
765*0Sstevel@tonic-gate 			ndi_dc_freehdl(dcp);
766*0Sstevel@tonic-gate 		return (ENXIO);
767*0Sstevel@tonic-gate 	}
768*0Sstevel@tonic-gate 
769*0Sstevel@tonic-gate 	switch (cmd) {
770*0Sstevel@tonic-gate 	case DEVCTL_AP_INSERT:
771*0Sstevel@tonic-gate 	case DEVCTL_AP_REMOVE:
772*0Sstevel@tonic-gate 	case DEVCTL_AP_CONNECT:
773*0Sstevel@tonic-gate 	case DEVCTL_AP_DISCONNECT:
774*0Sstevel@tonic-gate 	case DEVCTL_AP_CONFIGURE:
775*0Sstevel@tonic-gate 	case DEVCTL_AP_UNCONFIGURE:
776*0Sstevel@tonic-gate 	case DEVCTL_AP_GETSTATE:
777*0Sstevel@tonic-gate 	case DEVCTL_AP_CONTROL:
778*0Sstevel@tonic-gate 		state_locking = PCIHP_DR_SLOT_ENTER;
779*0Sstevel@tonic-gate 		state_unlocking = PCIHP_DR_SLOT_EXIT;
780*0Sstevel@tonic-gate 		break;
781*0Sstevel@tonic-gate 	default:
782*0Sstevel@tonic-gate 		state_locking = PCIHP_DR_NOOP;
783*0Sstevel@tonic-gate 		state_unlocking = PCIHP_DR_NOOP;
784*0Sstevel@tonic-gate 		break;
785*0Sstevel@tonic-gate 	}
786*0Sstevel@tonic-gate 
787*0Sstevel@tonic-gate 	pcihp_p = pcihp_get_soft_state(self, state_locking, &rval);
788*0Sstevel@tonic-gate 	ASSERT(pcihp_p != NULL);
789*0Sstevel@tonic-gate 
790*0Sstevel@tonic-gate 	if (rval == PCIHP_FAILURE) {
791*0Sstevel@tonic-gate 		(void) ddi_pathname(pcihp_p->dip, pathname);
792*0Sstevel@tonic-gate 		PCIHP_DEBUG((CE_WARN, "Hot Plug bus %s instance is unconfigured"
793*0Sstevel@tonic-gate 		    " while slot activity is requested\n", pathname));
794*0Sstevel@tonic-gate 		if (cmd != DEVCTL_AP_CONTROL)
795*0Sstevel@tonic-gate 			ndi_dc_freehdl(dcp);
796*0Sstevel@tonic-gate 		return (EBUSY);
797*0Sstevel@tonic-gate 	}
798*0Sstevel@tonic-gate 
799*0Sstevel@tonic-gate 	/*
800*0Sstevel@tonic-gate 	 * For attachment points the lower 8 bits of the minor number is the
801*0Sstevel@tonic-gate 	 * PCI device number.
802*0Sstevel@tonic-gate 	 */
803*0Sstevel@tonic-gate 	pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(getminor(dev));
804*0Sstevel@tonic-gate 
805*0Sstevel@tonic-gate 	/*
806*0Sstevel@tonic-gate 	 * We can use the generic implementation for these ioctls
807*0Sstevel@tonic-gate 	 */
808*0Sstevel@tonic-gate 	switch (cmd) {
809*0Sstevel@tonic-gate 	case DEVCTL_DEVICE_GETSTATE:
810*0Sstevel@tonic-gate 	case DEVCTL_DEVICE_ONLINE:
811*0Sstevel@tonic-gate 	case DEVCTL_DEVICE_OFFLINE:
812*0Sstevel@tonic-gate 	case DEVCTL_BUS_GETSTATE:
813*0Sstevel@tonic-gate 		rv = ndi_devctl_ioctl(self, cmd, arg, mode, 0);
814*0Sstevel@tonic-gate 		ndi_dc_freehdl(dcp);
815*0Sstevel@tonic-gate 		return (rv);
816*0Sstevel@tonic-gate 	default:
817*0Sstevel@tonic-gate 		break;
818*0Sstevel@tonic-gate 	}
819*0Sstevel@tonic-gate 
820*0Sstevel@tonic-gate 	switch (cmd) {
821*0Sstevel@tonic-gate 
822*0Sstevel@tonic-gate 	case DEVCTL_DEVICE_RESET:
823*0Sstevel@tonic-gate 		rv = ENOTSUP;
824*0Sstevel@tonic-gate 		break;
825*0Sstevel@tonic-gate 
826*0Sstevel@tonic-gate 	case DEVCTL_BUS_QUIESCE:
827*0Sstevel@tonic-gate 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
828*0Sstevel@tonic-gate 			if (bus_state == BUS_QUIESCED)
829*0Sstevel@tonic-gate 				break;
830*0Sstevel@tonic-gate 		(void) ndi_set_bus_state(self, BUS_QUIESCED);
831*0Sstevel@tonic-gate 		break;
832*0Sstevel@tonic-gate 
833*0Sstevel@tonic-gate 	case DEVCTL_BUS_UNQUIESCE:
834*0Sstevel@tonic-gate 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
835*0Sstevel@tonic-gate 			if (bus_state == BUS_ACTIVE)
836*0Sstevel@tonic-gate 				break;
837*0Sstevel@tonic-gate 		(void) ndi_set_bus_state(self, BUS_ACTIVE);
838*0Sstevel@tonic-gate 		break;
839*0Sstevel@tonic-gate 
840*0Sstevel@tonic-gate 	case DEVCTL_BUS_RESET:
841*0Sstevel@tonic-gate 		rv = ENOTSUP;
842*0Sstevel@tonic-gate 		break;
843*0Sstevel@tonic-gate 
844*0Sstevel@tonic-gate 	case DEVCTL_BUS_RESETALL:
845*0Sstevel@tonic-gate 		rv = ENOTSUP;
846*0Sstevel@tonic-gate 		break;
847*0Sstevel@tonic-gate 
848*0Sstevel@tonic-gate 	case DEVCTL_AP_CONNECT:
849*0Sstevel@tonic-gate 	case DEVCTL_AP_DISCONNECT:
850*0Sstevel@tonic-gate 		/*
851*0Sstevel@tonic-gate 		 * CONNECT(DISCONNECT) the hot plug slot to(from) the bus.
852*0Sstevel@tonic-gate 		 *
853*0Sstevel@tonic-gate 		 * For cPCI slots this operation is a nop so the HPC
854*0Sstevel@tonic-gate 		 * driver may return success if it is a valid operation.
855*0Sstevel@tonic-gate 		 */
856*0Sstevel@tonic-gate 	case DEVCTL_AP_INSERT:
857*0Sstevel@tonic-gate 	case DEVCTL_AP_REMOVE:
858*0Sstevel@tonic-gate 		/*
859*0Sstevel@tonic-gate 		 * Prepare the slot for INSERT/REMOVE operation.
860*0Sstevel@tonic-gate 		 */
861*0Sstevel@tonic-gate 
862*0Sstevel@tonic-gate 		/*
863*0Sstevel@tonic-gate 		 * check for valid request:
864*0Sstevel@tonic-gate 		 *	1. It is a hotplug slot.
865*0Sstevel@tonic-gate 		 *	2. The slot has no occupant that is in
866*0Sstevel@tonic-gate 		 *	   the 'configured' state.
867*0Sstevel@tonic-gate 		 */
868*0Sstevel@tonic-gate 		if (pci_dev >= PCI_MAX_DEVS) {
869*0Sstevel@tonic-gate 			rv = ENXIO;
870*0Sstevel@tonic-gate 			break;
871*0Sstevel@tonic-gate 		}
872*0Sstevel@tonic-gate 		slotinfop = &pcihp_p->slotinfo[pci_dev];
873*0Sstevel@tonic-gate 
874*0Sstevel@tonic-gate 		mutex_enter(&slotinfop->slot_mutex);
875*0Sstevel@tonic-gate 
876*0Sstevel@tonic-gate 		if ((slotinfop->slot_hdl == NULL) ||
877*0Sstevel@tonic-gate 		    (slotinfop->slot_flags & PCIHP_SLOT_DISABLED)) {
878*0Sstevel@tonic-gate 			rv = ENXIO;
879*0Sstevel@tonic-gate 			mutex_exit(&slotinfop->slot_mutex);
880*0Sstevel@tonic-gate 			break;
881*0Sstevel@tonic-gate 		}
882*0Sstevel@tonic-gate 
883*0Sstevel@tonic-gate 		/* the slot occupant must be in the UNCONFIGURED state */
884*0Sstevel@tonic-gate 		if (slotinfop->ostate != AP_OSTATE_UNCONFIGURED) {
885*0Sstevel@tonic-gate 			rv = EINVAL;
886*0Sstevel@tonic-gate 			mutex_exit(&slotinfop->slot_mutex);
887*0Sstevel@tonic-gate 			break;
888*0Sstevel@tonic-gate 		}
889*0Sstevel@tonic-gate 		/*
890*0Sstevel@tonic-gate 		 * Call the HPC driver to perform the operation on the slot.
891*0Sstevel@tonic-gate 		 */
892*0Sstevel@tonic-gate 
893*0Sstevel@tonic-gate 		switch (cmd) {
894*0Sstevel@tonic-gate 		case DEVCTL_AP_INSERT:
895*0Sstevel@tonic-gate 			rv = hpc_nexus_insert(slotinfop->slot_hdl, NULL, 0);
896*0Sstevel@tonic-gate 			break;
897*0Sstevel@tonic-gate 		case DEVCTL_AP_REMOVE:
898*0Sstevel@tonic-gate 			rv = hpc_nexus_remove(slotinfop->slot_hdl, NULL, 0);
899*0Sstevel@tonic-gate 			break;
900*0Sstevel@tonic-gate 		case DEVCTL_AP_CONNECT:
901*0Sstevel@tonic-gate 			rv = hpc_nexus_connect(slotinfop->slot_hdl, NULL, 0);
902*0Sstevel@tonic-gate 			if (rv == HPC_SUCCESS) {
903*0Sstevel@tonic-gate 				slotinfop->rstate = AP_RSTATE_CONNECTED;
904*0Sstevel@tonic-gate 
905*0Sstevel@tonic-gate 				if (drv_getparm(TIME, (void *)&time) !=
906*0Sstevel@tonic-gate 				    DDI_SUCCESS)
907*0Sstevel@tonic-gate 					slotinfop->last_change = (time_t)-1;
908*0Sstevel@tonic-gate 				else
909*0Sstevel@tonic-gate 					slotinfop->last_change = (time32_t)time;
910*0Sstevel@tonic-gate 
911*0Sstevel@tonic-gate 				slotinfop = &pcihp_p->slotinfo[pci_dev];
912*0Sstevel@tonic-gate 				pcihp_gen_sysevent(slotinfop->name,
913*0Sstevel@tonic-gate 						PCIHP_DR_AP_STATE_CHANGE,
914*0Sstevel@tonic-gate 						SE_NO_HINT, pcihp_p->dip,
915*0Sstevel@tonic-gate 						KM_SLEEP);
916*0Sstevel@tonic-gate 			}
917*0Sstevel@tonic-gate 			break;
918*0Sstevel@tonic-gate 		case DEVCTL_AP_DISCONNECT:
919*0Sstevel@tonic-gate 			rv = hpc_nexus_disconnect(slotinfop->slot_hdl, NULL, 0);
920*0Sstevel@tonic-gate 			if (rv == HPC_SUCCESS) {
921*0Sstevel@tonic-gate 				slotinfop->rstate = AP_RSTATE_DISCONNECTED;
922*0Sstevel@tonic-gate 
923*0Sstevel@tonic-gate 				if (drv_getparm(TIME, (void *)&time) !=
924*0Sstevel@tonic-gate 				    DDI_SUCCESS)
925*0Sstevel@tonic-gate 					slotinfop->last_change = (time_t)-1;
926*0Sstevel@tonic-gate 				else
927*0Sstevel@tonic-gate 					slotinfop->last_change = (time32_t)time;
928*0Sstevel@tonic-gate 
929*0Sstevel@tonic-gate 				slotinfop = &pcihp_p->slotinfo[pci_dev];
930*0Sstevel@tonic-gate 				pcihp_gen_sysevent(slotinfop->name,
931*0Sstevel@tonic-gate 						PCIHP_DR_AP_STATE_CHANGE,
932*0Sstevel@tonic-gate 						SE_NO_HINT, pcihp_p->dip,
933*0Sstevel@tonic-gate 						KM_SLEEP);
934*0Sstevel@tonic-gate 			}
935*0Sstevel@tonic-gate 			break;
936*0Sstevel@tonic-gate 		}
937*0Sstevel@tonic-gate 		mutex_exit(&slotinfop->slot_mutex);
938*0Sstevel@tonic-gate 
939*0Sstevel@tonic-gate 		switch (rv) {
940*0Sstevel@tonic-gate 		case HPC_ERR_INVALID:
941*0Sstevel@tonic-gate 			rv = ENXIO;
942*0Sstevel@tonic-gate 			break;
943*0Sstevel@tonic-gate 		case HPC_ERR_NOTSUPPORTED:
944*0Sstevel@tonic-gate 			rv = ENOTSUP;
945*0Sstevel@tonic-gate 			break;
946*0Sstevel@tonic-gate 		case HPC_ERR_FAILED:
947*0Sstevel@tonic-gate 			rv = EIO;
948*0Sstevel@tonic-gate 			break;
949*0Sstevel@tonic-gate 		}
950*0Sstevel@tonic-gate 
951*0Sstevel@tonic-gate 		break;
952*0Sstevel@tonic-gate 
953*0Sstevel@tonic-gate 	case DEVCTL_AP_CONFIGURE:
954*0Sstevel@tonic-gate 		/*
955*0Sstevel@tonic-gate 		 * **************************************
956*0Sstevel@tonic-gate 		 * CONFIGURE the occupant in the slot.
957*0Sstevel@tonic-gate 		 * **************************************
958*0Sstevel@tonic-gate 		 */
959*0Sstevel@tonic-gate 
960*0Sstevel@tonic-gate 		rv = pcihp_configure_ap(pcihp_p, pci_dev);
961*0Sstevel@tonic-gate 		if (rv == HPC_SUCCESS) {
962*0Sstevel@tonic-gate 			slotinfop = &pcihp_p->slotinfo[pci_dev];
963*0Sstevel@tonic-gate 			pcihp_gen_sysevent(slotinfop->name,
964*0Sstevel@tonic-gate 			    PCIHP_DR_AP_STATE_CHANGE,
965*0Sstevel@tonic-gate 			    SE_NO_HINT, pcihp_p->dip, KM_SLEEP);
966*0Sstevel@tonic-gate 			pcihp_create_occupant_props(self, dev, pci_dev);
967*0Sstevel@tonic-gate 		}
968*0Sstevel@tonic-gate 
969*0Sstevel@tonic-gate 		break;
970*0Sstevel@tonic-gate 
971*0Sstevel@tonic-gate 	case DEVCTL_AP_UNCONFIGURE:
972*0Sstevel@tonic-gate 		/*
973*0Sstevel@tonic-gate 		 * **************************************
974*0Sstevel@tonic-gate 		 * UNCONFIGURE the occupant in the slot.
975*0Sstevel@tonic-gate 		 * **************************************
976*0Sstevel@tonic-gate 		 */
977*0Sstevel@tonic-gate 
978*0Sstevel@tonic-gate 		rv = pcihp_unconfigure_ap(pcihp_p, pci_dev);
979*0Sstevel@tonic-gate 
980*0Sstevel@tonic-gate 		if (rv == HPC_SUCCESS) {
981*0Sstevel@tonic-gate 
982*0Sstevel@tonic-gate 			slotinfop = &pcihp_p->slotinfo[pci_dev];
983*0Sstevel@tonic-gate 			pcihp_gen_sysevent(slotinfop->name,
984*0Sstevel@tonic-gate 			    PCIHP_DR_AP_STATE_CHANGE,
985*0Sstevel@tonic-gate 			    SE_NO_HINT, pcihp_p->dip, KM_SLEEP);
986*0Sstevel@tonic-gate 			pcihp_delete_occupant_props(pcihp_p->dip, dev);
987*0Sstevel@tonic-gate 		}
988*0Sstevel@tonic-gate 
989*0Sstevel@tonic-gate 		break;
990*0Sstevel@tonic-gate 
991*0Sstevel@tonic-gate 	case DEVCTL_AP_GETSTATE:
992*0Sstevel@tonic-gate 	    {
993*0Sstevel@tonic-gate 		int mutex_held;
994*0Sstevel@tonic-gate 
995*0Sstevel@tonic-gate 		/*
996*0Sstevel@tonic-gate 		 * return the state of Attachment Point.
997*0Sstevel@tonic-gate 		 *
998*0Sstevel@tonic-gate 		 * If the occupant is in UNCONFIGURED state then
999*0Sstevel@tonic-gate 		 * we should get the receptacle state from the
1000*0Sstevel@tonic-gate 		 * HPC driver because the receptacle state
1001*0Sstevel@tonic-gate 		 * maintained in the nexus may not be accurate.
1002*0Sstevel@tonic-gate 		 */
1003*0Sstevel@tonic-gate 
1004*0Sstevel@tonic-gate 		/*
1005*0Sstevel@tonic-gate 		 * check for valid request:
1006*0Sstevel@tonic-gate 		 *	1. It is a hotplug slot.
1007*0Sstevel@tonic-gate 		 */
1008*0Sstevel@tonic-gate 		slotinfop = &pcihp_p->slotinfo[pci_dev];
1009*0Sstevel@tonic-gate 
1010*0Sstevel@tonic-gate 		/* try to acquire the slot mutex */
1011*0Sstevel@tonic-gate 		mutex_held = mutex_tryenter(&slotinfop->slot_mutex);
1012*0Sstevel@tonic-gate 
1013*0Sstevel@tonic-gate 		if (pci_dev >= PCI_MAX_DEVS || slotinfop->slot_hdl == NULL) {
1014*0Sstevel@tonic-gate 			rv = ENXIO;
1015*0Sstevel@tonic-gate 			if (mutex_held) {
1016*0Sstevel@tonic-gate 				mutex_exit(&slotinfop->slot_mutex);
1017*0Sstevel@tonic-gate 			}
1018*0Sstevel@tonic-gate 			break;
1019*0Sstevel@tonic-gate 		}
1020*0Sstevel@tonic-gate 
1021*0Sstevel@tonic-gate 		if (slotinfop->ostate == AP_OSTATE_UNCONFIGURED) {
1022*0Sstevel@tonic-gate 		    if (hpc_nexus_control(slotinfop->slot_hdl,
1023*0Sstevel@tonic-gate 			HPC_CTRL_GET_SLOT_STATE, (caddr_t)&rstate) != 0) {
1024*0Sstevel@tonic-gate 			rv = EIO;
1025*0Sstevel@tonic-gate 			if (mutex_held)
1026*0Sstevel@tonic-gate 			    mutex_exit(&slotinfop->slot_mutex);
1027*0Sstevel@tonic-gate 			break;
1028*0Sstevel@tonic-gate 		    }
1029*0Sstevel@tonic-gate 		    slotinfop->rstate = (ap_rstate_t)rstate;
1030*0Sstevel@tonic-gate 		}
1031*0Sstevel@tonic-gate 
1032*0Sstevel@tonic-gate 		ap_state.ap_rstate = slotinfop->rstate;
1033*0Sstevel@tonic-gate 		ap_state.ap_ostate = slotinfop->ostate;
1034*0Sstevel@tonic-gate 		ap_state.ap_condition = slotinfop->condition;
1035*0Sstevel@tonic-gate 		ap_state.ap_last_change = slotinfop->last_change;
1036*0Sstevel@tonic-gate 		ap_state.ap_error_code = 0; /* XXX */
1037*0Sstevel@tonic-gate 		if (mutex_held)
1038*0Sstevel@tonic-gate 			ap_state.ap_in_transition = 0; /* AP is not busy */
1039*0Sstevel@tonic-gate 		else
1040*0Sstevel@tonic-gate 			ap_state.ap_in_transition = 1; /* AP is busy */
1041*0Sstevel@tonic-gate 
1042*0Sstevel@tonic-gate 		if (mutex_held)
1043*0Sstevel@tonic-gate 			mutex_exit(&slotinfop->slot_mutex);
1044*0Sstevel@tonic-gate 
1045*0Sstevel@tonic-gate 		/* copy the return-AP-state information to the user space */
1046*0Sstevel@tonic-gate 		if (ndi_dc_return_ap_state(&ap_state, dcp) != NDI_SUCCESS)
1047*0Sstevel@tonic-gate 			rv = EFAULT;
1048*0Sstevel@tonic-gate 
1049*0Sstevel@tonic-gate 		break;
1050*0Sstevel@tonic-gate 
1051*0Sstevel@tonic-gate 	    }
1052*0Sstevel@tonic-gate 	case DEVCTL_AP_CONTROL:
1053*0Sstevel@tonic-gate 		/*
1054*0Sstevel@tonic-gate 		 * HPC control functions:
1055*0Sstevel@tonic-gate 		 *	HPC_CTRL_ENABLE_SLOT/HPC_CTRL_DISABLE_SLOT
1056*0Sstevel@tonic-gate 		 *		Changes the state of the slot and preserves
1057*0Sstevel@tonic-gate 		 *		the state across the reboot.
1058*0Sstevel@tonic-gate 		 *	HPC_CTRL_ENABLE_AUTOCFG/HPC_CTRL_DISABLE_AUTOCFG
1059*0Sstevel@tonic-gate 		 *		Enables or disables the auto configuration
1060*0Sstevel@tonic-gate 		 *		of hot plugged occupant if the hardware
1061*0Sstevel@tonic-gate 		 *		supports notification of the hot plug
1062*0Sstevel@tonic-gate 		 *		events.
1063*0Sstevel@tonic-gate 		 *	HPC_CTRL_GET_LED_STATE/HPC_CTRL_SET_LED_STATE
1064*0Sstevel@tonic-gate 		 *		Controls the state of an LED.
1065*0Sstevel@tonic-gate 		 *	HPC_CTRL_GET_SLOT_INFO
1066*0Sstevel@tonic-gate 		 *		Get slot information data structure
1067*0Sstevel@tonic-gate 		 *		(hpc_slot_info_t).
1068*0Sstevel@tonic-gate 		 *	HPC_CTRL_GET_BOARD_TYPE
1069*0Sstevel@tonic-gate 		 *		Get board type information (hpc_board_type_t).
1070*0Sstevel@tonic-gate 		 *	HPC_CTRL_GET_CARD_INFO
1071*0Sstevel@tonic-gate 		 *		Get card information (hpc_card_info_t).
1072*0Sstevel@tonic-gate 		 *
1073*0Sstevel@tonic-gate 		 * These control functions are used by the cfgadm plug-in
1074*0Sstevel@tonic-gate 		 * to implement "-x" and "-v" options.
1075*0Sstevel@tonic-gate 		 */
1076*0Sstevel@tonic-gate 
1077*0Sstevel@tonic-gate 		/* copy user ioctl data first */
1078*0Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
1079*0Sstevel@tonic-gate 		if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
1080*0Sstevel@tonic-gate 			struct hpc_control32_data hpc_ctrldata32;
1081*0Sstevel@tonic-gate 
1082*0Sstevel@tonic-gate 			if (copyin((void *)arg, (void *)&hpc_ctrldata32,
1083*0Sstevel@tonic-gate 				sizeof (struct hpc_control32_data)) != 0) {
1084*0Sstevel@tonic-gate 				rv = EFAULT;
1085*0Sstevel@tonic-gate 				break;
1086*0Sstevel@tonic-gate 			}
1087*0Sstevel@tonic-gate 			hpc_ctrldata.cmd = hpc_ctrldata32.cmd;
1088*0Sstevel@tonic-gate 			hpc_ctrldata.data =
1089*0Sstevel@tonic-gate 				(void *)(intptr_t)hpc_ctrldata32.data;
1090*0Sstevel@tonic-gate 		}
1091*0Sstevel@tonic-gate #else
1092*0Sstevel@tonic-gate 		if (copyin((void *)arg, (void *)&hpc_ctrldata,
1093*0Sstevel@tonic-gate 			sizeof (struct hpc_control_data)) != 0) {
1094*0Sstevel@tonic-gate 			rv = EFAULT;
1095*0Sstevel@tonic-gate 			break;
1096*0Sstevel@tonic-gate 		}
1097*0Sstevel@tonic-gate #endif
1098*0Sstevel@tonic-gate 
1099*0Sstevel@tonic-gate 		/*
1100*0Sstevel@tonic-gate 		 * check for valid request:
1101*0Sstevel@tonic-gate 		 *	1. It is a hotplug slot.
1102*0Sstevel@tonic-gate 		 */
1103*0Sstevel@tonic-gate 		slotinfop = &pcihp_p->slotinfo[pci_dev];
1104*0Sstevel@tonic-gate 
1105*0Sstevel@tonic-gate 		mutex_enter(&slotinfop->slot_mutex);
1106*0Sstevel@tonic-gate 
1107*0Sstevel@tonic-gate 		if (pci_dev >= PCI_MAX_DEVS || slotinfop->slot_hdl == NULL) {
1108*0Sstevel@tonic-gate 			rv = ENXIO;
1109*0Sstevel@tonic-gate 			mutex_exit(&slotinfop->slot_mutex);
1110*0Sstevel@tonic-gate 			break;
1111*0Sstevel@tonic-gate 		}
1112*0Sstevel@tonic-gate 
1113*0Sstevel@tonic-gate 		switch (hpc_ctrldata.cmd) {
1114*0Sstevel@tonic-gate 
1115*0Sstevel@tonic-gate 		case HPC_CTRL_GET_LED_STATE:
1116*0Sstevel@tonic-gate 			/* copy the led info from the user space */
1117*0Sstevel@tonic-gate 			if (copyin(hpc_ctrldata.data, (void *)&led_info,
1118*0Sstevel@tonic-gate 					sizeof (hpc_led_info_t)) != 0) {
1119*0Sstevel@tonic-gate 				rv = EFAULT;
1120*0Sstevel@tonic-gate 				break;
1121*0Sstevel@tonic-gate 			}
1122*0Sstevel@tonic-gate 
1123*0Sstevel@tonic-gate 			/* get the state of LED information */
1124*0Sstevel@tonic-gate 			if (hpc_nexus_control(slotinfop->slot_hdl,
1125*0Sstevel@tonic-gate 				HPC_CTRL_GET_LED_STATE,
1126*0Sstevel@tonic-gate 				(caddr_t)&led_info) != 0) {
1127*0Sstevel@tonic-gate 
1128*0Sstevel@tonic-gate 				if (rv != ENOTSUP)
1129*0Sstevel@tonic-gate 					rv = EIO;
1130*0Sstevel@tonic-gate 
1131*0Sstevel@tonic-gate 				break;
1132*0Sstevel@tonic-gate 			}
1133*0Sstevel@tonic-gate 
1134*0Sstevel@tonic-gate 			/* copy the led info to the user space */
1135*0Sstevel@tonic-gate 			if (copyout((void *)&led_info,
1136*0Sstevel@tonic-gate 			    hpc_ctrldata.data,
1137*0Sstevel@tonic-gate 			    sizeof (hpc_led_info_t)) != 0) {
1138*0Sstevel@tonic-gate 			    rv = EFAULT;
1139*0Sstevel@tonic-gate 			    break;
1140*0Sstevel@tonic-gate 			}
1141*0Sstevel@tonic-gate 
1142*0Sstevel@tonic-gate 			break;
1143*0Sstevel@tonic-gate 
1144*0Sstevel@tonic-gate 		case HPC_CTRL_SET_LED_STATE:
1145*0Sstevel@tonic-gate 			/* copy the led info from the user space */
1146*0Sstevel@tonic-gate 			if (copyin(hpc_ctrldata.data, (void *)&led_info,
1147*0Sstevel@tonic-gate 			    sizeof (hpc_led_info_t)) != 0) {
1148*0Sstevel@tonic-gate 			    rv = EFAULT;
1149*0Sstevel@tonic-gate 			    break;
1150*0Sstevel@tonic-gate 			}
1151*0Sstevel@tonic-gate 
1152*0Sstevel@tonic-gate 			/* set the state of an LED */
1153*0Sstevel@tonic-gate 			rv = hpc_nexus_control(slotinfop->slot_hdl,
1154*0Sstevel@tonic-gate 			    HPC_CTRL_SET_LED_STATE, (caddr_t)&led_info);
1155*0Sstevel@tonic-gate 
1156*0Sstevel@tonic-gate 			/*
1157*0Sstevel@tonic-gate 			 * If the Hotswap Controller does not support
1158*0Sstevel@tonic-gate 			 * LED management (as you would find typically
1159*0Sstevel@tonic-gate 			 * in the cPCI industry), then we handle the
1160*0Sstevel@tonic-gate 			 * blue LED on/off/blink operations, just in
1161*0Sstevel@tonic-gate 			 * case it helps slot identification.
1162*0Sstevel@tonic-gate 			 */
1163*0Sstevel@tonic-gate 			if ((rv == HPC_ERR_NOTSUPPORTED) &&
1164*0Sstevel@tonic-gate 				(slotinfop->slot_type & HPC_SLOT_TYPE_CPCI)) {
1165*0Sstevel@tonic-gate 				if (led_info.led != HPC_ATTN_LED)
1166*0Sstevel@tonic-gate 					break;
1167*0Sstevel@tonic-gate 
1168*0Sstevel@tonic-gate 				switch (led_info.state) {
1169*0Sstevel@tonic-gate 					case HPC_LED_OFF:
1170*0Sstevel@tonic-gate 						pcihp_hs_csr_op(pcihp_p,
1171*0Sstevel@tonic-gate 						pci_dev,
1172*0Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_OFF);
1173*0Sstevel@tonic-gate 						rv = 0;
1174*0Sstevel@tonic-gate 						break;
1175*0Sstevel@tonic-gate 					case HPC_LED_ON:
1176*0Sstevel@tonic-gate 						/*
1177*0Sstevel@tonic-gate 						 * Please note that leaving
1178*0Sstevel@tonic-gate 						 * LED ON could be dangerous
1179*0Sstevel@tonic-gate 						 * as it means it is Ok to
1180*0Sstevel@tonic-gate 						 * remove the board, which
1181*0Sstevel@tonic-gate 						 * is not what we want to
1182*0Sstevel@tonic-gate 						 * convey. So it is upto the
1183*0Sstevel@tonic-gate 						 * user to take care of this
1184*0Sstevel@tonic-gate 						 * situation and usage.
1185*0Sstevel@tonic-gate 						 *
1186*0Sstevel@tonic-gate 						 * Normally, a Blink command
1187*0Sstevel@tonic-gate 						 * is more appropriate for
1188*0Sstevel@tonic-gate 						 * identifying a board.
1189*0Sstevel@tonic-gate 						 */
1190*0Sstevel@tonic-gate 						pcihp_hs_csr_op(pcihp_p,
1191*0Sstevel@tonic-gate 						pci_dev,
1192*0Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_ON);
1193*0Sstevel@tonic-gate 						rv = 0;
1194*0Sstevel@tonic-gate 						break;
1195*0Sstevel@tonic-gate 					case HPC_LED_BLINK:
1196*0Sstevel@tonic-gate 					{
1197*0Sstevel@tonic-gate 						int bl;
1198*0Sstevel@tonic-gate 
1199*0Sstevel@tonic-gate 						for (bl = 0; bl < 2; bl++) {
1200*0Sstevel@tonic-gate 						pcihp_hs_csr_op(pcihp_p,
1201*0Sstevel@tonic-gate 						pci_dev,
1202*0Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_ON);
1203*0Sstevel@tonic-gate 						delay(pcihp_cpci_led_blink);
1204*0Sstevel@tonic-gate 						pcihp_hs_csr_op(pcihp_p,
1205*0Sstevel@tonic-gate 						pci_dev,
1206*0Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_OFF);
1207*0Sstevel@tonic-gate 						delay(pcihp_cpci_led_blink);
1208*0Sstevel@tonic-gate 						}
1209*0Sstevel@tonic-gate 						rv = 0;
1210*0Sstevel@tonic-gate 						break;
1211*0Sstevel@tonic-gate 					}
1212*0Sstevel@tonic-gate 					default:
1213*0Sstevel@tonic-gate 						break;
1214*0Sstevel@tonic-gate 				}
1215*0Sstevel@tonic-gate 			}
1216*0Sstevel@tonic-gate 
1217*0Sstevel@tonic-gate 			if (rv == HPC_ERR_FAILED)
1218*0Sstevel@tonic-gate 				rv = EIO;
1219*0Sstevel@tonic-gate 			break;
1220*0Sstevel@tonic-gate 
1221*0Sstevel@tonic-gate 		case HPC_CTRL_ENABLE_SLOT:
1222*0Sstevel@tonic-gate 
1223*0Sstevel@tonic-gate 			/*
1224*0Sstevel@tonic-gate 			 * If slot already enabled, do not send a duplicate
1225*0Sstevel@tonic-gate 			 * control message to the HPC driver.
1226*0Sstevel@tonic-gate 			 */
1227*0Sstevel@tonic-gate 			if ((slotinfop->slot_flags & PCIHP_SLOT_DISABLED) == 0)
1228*0Sstevel@tonic-gate 				break;
1229*0Sstevel@tonic-gate 
1230*0Sstevel@tonic-gate 			/* tell the HPC driver also */
1231*0Sstevel@tonic-gate 			if (hpc_nexus_control(slotinfop->slot_hdl,
1232*0Sstevel@tonic-gate 					HPC_CTRL_ENABLE_SLOT, NULL)
1233*0Sstevel@tonic-gate 							!= HPC_SUCCESS) {
1234*0Sstevel@tonic-gate 				rv = EIO;
1235*0Sstevel@tonic-gate 				break;
1236*0Sstevel@tonic-gate 			}
1237*0Sstevel@tonic-gate 
1238*0Sstevel@tonic-gate 			/*
1239*0Sstevel@tonic-gate 			 * Enable the slot for hotplug operations.
1240*0Sstevel@tonic-gate 			 */
1241*0Sstevel@tonic-gate 			slotinfop->slot_flags &= ~PCIHP_SLOT_DISABLED;
1242*0Sstevel@tonic-gate 
1243*0Sstevel@tonic-gate 			slotinfop->condition = AP_COND_UNKNOWN;
1244*0Sstevel@tonic-gate 
1245*0Sstevel@tonic-gate 			/* XXX need to preserve this state across reboot? */
1246*0Sstevel@tonic-gate 
1247*0Sstevel@tonic-gate 			break;
1248*0Sstevel@tonic-gate 
1249*0Sstevel@tonic-gate 		case HPC_CTRL_DISABLE_SLOT:
1250*0Sstevel@tonic-gate 
1251*0Sstevel@tonic-gate 			/* Do not disable if occupant configured */
1252*0Sstevel@tonic-gate 			if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {
1253*0Sstevel@tonic-gate 				rv = EAGAIN;
1254*0Sstevel@tonic-gate 				break;
1255*0Sstevel@tonic-gate 			}
1256*0Sstevel@tonic-gate 
1257*0Sstevel@tonic-gate 			/* tell the HPC driver also */
1258*0Sstevel@tonic-gate 			if (hpc_nexus_control(slotinfop->slot_hdl,
1259*0Sstevel@tonic-gate 					HPC_CTRL_DISABLE_SLOT, NULL)
1260*0Sstevel@tonic-gate 							!= HPC_SUCCESS) {
1261*0Sstevel@tonic-gate 				rv = EIO;
1262*0Sstevel@tonic-gate 				break;
1263*0Sstevel@tonic-gate 			}
1264*0Sstevel@tonic-gate 
1265*0Sstevel@tonic-gate 			/*
1266*0Sstevel@tonic-gate 			 * Disable the slot for hotplug operations.
1267*0Sstevel@tonic-gate 			 */
1268*0Sstevel@tonic-gate 			slotinfop->slot_flags |= PCIHP_SLOT_DISABLED;
1269*0Sstevel@tonic-gate 
1270*0Sstevel@tonic-gate 			slotinfop->condition = AP_COND_UNUSABLE;
1271*0Sstevel@tonic-gate 
1272*0Sstevel@tonic-gate 			/* XXX need to preserve this state across reboot? */
1273*0Sstevel@tonic-gate 
1274*0Sstevel@tonic-gate 			break;
1275*0Sstevel@tonic-gate 
1276*0Sstevel@tonic-gate 		case HPC_CTRL_ENABLE_AUTOCFG:
1277*0Sstevel@tonic-gate 			/*
1278*0Sstevel@tonic-gate 			 * Enable auto configuration on this slot.
1279*0Sstevel@tonic-gate 			 */
1280*0Sstevel@tonic-gate 			slotinfop->slot_flags |= PCIHP_SLOT_AUTO_CFG_EN;
1281*0Sstevel@tonic-gate 
1282*0Sstevel@tonic-gate 			/* tell the HPC driver also */
1283*0Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
1284*0Sstevel@tonic-gate 				HPC_CTRL_ENABLE_AUTOCFG, NULL);
1285*0Sstevel@tonic-gate 
1286*0Sstevel@tonic-gate 			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI)
1287*0Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
1288*0Sstevel@tonic-gate 					HPC_EVENT_ENABLE_ENUM);
1289*0Sstevel@tonic-gate 			break;
1290*0Sstevel@tonic-gate 
1291*0Sstevel@tonic-gate 		case HPC_CTRL_DISABLE_AUTOCFG:
1292*0Sstevel@tonic-gate 			/*
1293*0Sstevel@tonic-gate 			 * Disable auto configuration on this slot.
1294*0Sstevel@tonic-gate 			 */
1295*0Sstevel@tonic-gate 			slotinfop->slot_flags &= ~PCIHP_SLOT_AUTO_CFG_EN;
1296*0Sstevel@tonic-gate 
1297*0Sstevel@tonic-gate 			/* tell the HPC driver also */
1298*0Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
1299*0Sstevel@tonic-gate 			    HPC_CTRL_DISABLE_AUTOCFG, NULL);
1300*0Sstevel@tonic-gate 
1301*0Sstevel@tonic-gate 			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI)
1302*0Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
1303*0Sstevel@tonic-gate 					HPC_EVENT_DISABLE_ENUM);
1304*0Sstevel@tonic-gate 			break;
1305*0Sstevel@tonic-gate 
1306*0Sstevel@tonic-gate 		case HPC_CTRL_GET_BOARD_TYPE:
1307*0Sstevel@tonic-gate 		    {
1308*0Sstevel@tonic-gate 			hpc_board_type_t board_type;
1309*0Sstevel@tonic-gate 
1310*0Sstevel@tonic-gate 			/*
1311*0Sstevel@tonic-gate 			 * Get board type data structure, hpc_board_type_t.
1312*0Sstevel@tonic-gate 			 */
1313*0Sstevel@tonic-gate 			board_type = pcihp_get_board_type(slotinfop);
1314*0Sstevel@tonic-gate 			if (board_type == -1) {
1315*0Sstevel@tonic-gate 			    rv = ENXIO;
1316*0Sstevel@tonic-gate 			    break;
1317*0Sstevel@tonic-gate 			}
1318*0Sstevel@tonic-gate 
1319*0Sstevel@tonic-gate 			/* copy the board type info to the user space */
1320*0Sstevel@tonic-gate 			if (copyout((void *)&board_type, hpc_ctrldata.data,
1321*0Sstevel@tonic-gate 			    sizeof (hpc_board_type_t)) != 0) {
1322*0Sstevel@tonic-gate 			    rv = ENXIO;
1323*0Sstevel@tonic-gate 			    break;
1324*0Sstevel@tonic-gate 			}
1325*0Sstevel@tonic-gate 
1326*0Sstevel@tonic-gate 			break;
1327*0Sstevel@tonic-gate 		    }
1328*0Sstevel@tonic-gate 
1329*0Sstevel@tonic-gate 		case HPC_CTRL_GET_SLOT_INFO:
1330*0Sstevel@tonic-gate 		    {
1331*0Sstevel@tonic-gate 			hpc_slot_info_t slot_info;
1332*0Sstevel@tonic-gate 
1333*0Sstevel@tonic-gate 			/*
1334*0Sstevel@tonic-gate 			 * Get slot information structure, hpc_slot_info_t.
1335*0Sstevel@tonic-gate 			 */
1336*0Sstevel@tonic-gate 			slot_info.version = HPC_SLOT_INFO_VERSION;
1337*0Sstevel@tonic-gate 			slot_info.slot_type = slotinfop->slot_type;
1338*0Sstevel@tonic-gate 			slot_info.pci_slot_capabilities =
1339*0Sstevel@tonic-gate 					slotinfop->slot_capabilities;
1340*0Sstevel@tonic-gate 			slot_info.pci_dev_num = (uint16_t)pci_dev;
1341*0Sstevel@tonic-gate 			(void) strcpy(slot_info.pci_slot_name, slotinfop->name);
1342*0Sstevel@tonic-gate 
1343*0Sstevel@tonic-gate 			/* copy the slot info structure to the user space */
1344*0Sstevel@tonic-gate 			if (copyout((void *)&slot_info, hpc_ctrldata.data,
1345*0Sstevel@tonic-gate 			    sizeof (hpc_slot_info_t)) != 0) {
1346*0Sstevel@tonic-gate 			    rv = EFAULT;
1347*0Sstevel@tonic-gate 			    break;
1348*0Sstevel@tonic-gate 			}
1349*0Sstevel@tonic-gate 
1350*0Sstevel@tonic-gate 			break;
1351*0Sstevel@tonic-gate 		    }
1352*0Sstevel@tonic-gate 
1353*0Sstevel@tonic-gate 		case HPC_CTRL_GET_CARD_INFO:
1354*0Sstevel@tonic-gate 		    {
1355*0Sstevel@tonic-gate 			hpc_card_info_t card_info;
1356*0Sstevel@tonic-gate 			ddi_acc_handle_t handle;
1357*0Sstevel@tonic-gate 			dev_info_t *cdip;
1358*0Sstevel@tonic-gate 
1359*0Sstevel@tonic-gate 			/*
1360*0Sstevel@tonic-gate 			 * Get card information structure, hpc_card_info_t.
1361*0Sstevel@tonic-gate 			 */
1362*0Sstevel@tonic-gate 
1363*0Sstevel@tonic-gate 			/* verify that the card is configured */
1364*0Sstevel@tonic-gate 			if ((slotinfop->ostate != AP_OSTATE_CONFIGURED) ||
1365*0Sstevel@tonic-gate 			    ((cdip = pcihp_devi_find(self, pci_dev,
1366*0Sstevel@tonic-gate 							0)) == NULL)) {
1367*0Sstevel@tonic-gate 			    /* either the card is not present or */
1368*0Sstevel@tonic-gate 			    /* it is not configured.		 */
1369*0Sstevel@tonic-gate 			    rv = ENXIO;
1370*0Sstevel@tonic-gate 			    break;
1371*0Sstevel@tonic-gate 			}
1372*0Sstevel@tonic-gate 
1373*0Sstevel@tonic-gate 			/*
1374*0Sstevel@tonic-gate 			 * If declared failed, don't allow Config operations.
1375*0Sstevel@tonic-gate 			 * Otherwise, if good or failing, it is assumed Ok
1376*0Sstevel@tonic-gate 			 * to get config data.
1377*0Sstevel@tonic-gate 			 */
1378*0Sstevel@tonic-gate 			if (slotinfop->condition == AP_COND_FAILED) {
1379*0Sstevel@tonic-gate 				rv = EIO;
1380*0Sstevel@tonic-gate 				break;
1381*0Sstevel@tonic-gate 			}
1382*0Sstevel@tonic-gate 
1383*0Sstevel@tonic-gate 			/* get the information from the PCI config header */
1384*0Sstevel@tonic-gate 			/* for the function 0.				  */
1385*0Sstevel@tonic-gate 			if (pci_config_setup(cdip, &handle) != DDI_SUCCESS) {
1386*0Sstevel@tonic-gate 				rv = EIO;
1387*0Sstevel@tonic-gate 				break;
1388*0Sstevel@tonic-gate 			}
1389*0Sstevel@tonic-gate 			card_info.prog_class = pci_config_get8(handle,
1390*0Sstevel@tonic-gate 						PCI_CONF_PROGCLASS);
1391*0Sstevel@tonic-gate 			card_info.base_class = pci_config_get8(handle,
1392*0Sstevel@tonic-gate 						PCI_CONF_BASCLASS);
1393*0Sstevel@tonic-gate 			card_info.sub_class = pci_config_get8(handle,
1394*0Sstevel@tonic-gate 						PCI_CONF_SUBCLASS);
1395*0Sstevel@tonic-gate 			card_info.header_type = pci_config_get8(handle,
1396*0Sstevel@tonic-gate 						PCI_CONF_HEADER);
1397*0Sstevel@tonic-gate 			pci_config_teardown(&handle);
1398*0Sstevel@tonic-gate 
1399*0Sstevel@tonic-gate 			/* copy the card info structure to the user space */
1400*0Sstevel@tonic-gate 			if (copyout((void *)&card_info, hpc_ctrldata.data,
1401*0Sstevel@tonic-gate 			    sizeof (hpc_card_info_t)) != 0) {
1402*0Sstevel@tonic-gate 			    rv = EFAULT;
1403*0Sstevel@tonic-gate 			    break;
1404*0Sstevel@tonic-gate 			}
1405*0Sstevel@tonic-gate 
1406*0Sstevel@tonic-gate 			break;
1407*0Sstevel@tonic-gate 		    }
1408*0Sstevel@tonic-gate 
1409*0Sstevel@tonic-gate 		default:
1410*0Sstevel@tonic-gate 			rv = EINVAL;
1411*0Sstevel@tonic-gate 			break;
1412*0Sstevel@tonic-gate 		}
1413*0Sstevel@tonic-gate 
1414*0Sstevel@tonic-gate 		mutex_exit(&slotinfop->slot_mutex);
1415*0Sstevel@tonic-gate 
1416*0Sstevel@tonic-gate 		break;
1417*0Sstevel@tonic-gate 
1418*0Sstevel@tonic-gate 	default:
1419*0Sstevel@tonic-gate 		rv = ENOTTY;
1420*0Sstevel@tonic-gate 	}
1421*0Sstevel@tonic-gate 
1422*0Sstevel@tonic-gate 	if (cmd != DEVCTL_AP_CONTROL)
1423*0Sstevel@tonic-gate 		ndi_dc_freehdl(dcp);
1424*0Sstevel@tonic-gate 
1425*0Sstevel@tonic-gate 	(void) pcihp_get_soft_state(self, state_unlocking, &rval);
1426*0Sstevel@tonic-gate 
1427*0Sstevel@tonic-gate 	return (rv);
1428*0Sstevel@tonic-gate }
1429*0Sstevel@tonic-gate 
1430*0Sstevel@tonic-gate /*
1431*0Sstevel@tonic-gate  * **************************************
1432*0Sstevel@tonic-gate  * CONFIGURE the occupant in the slot.
1433*0Sstevel@tonic-gate  * **************************************
1434*0Sstevel@tonic-gate  */
1435*0Sstevel@tonic-gate static int
1436*0Sstevel@tonic-gate pcihp_configure_ap(pcihp_t *pcihp_p, int pci_dev)
1437*0Sstevel@tonic-gate {
1438*0Sstevel@tonic-gate 	dev_info_t *self = pcihp_p->dip;
1439*0Sstevel@tonic-gate 	int rv = HPC_SUCCESS;
1440*0Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
1441*0Sstevel@tonic-gate 	hpc_slot_state_t rstate;
1442*0Sstevel@tonic-gate 	struct pcihp_config_ctrl ctrl;
1443*0Sstevel@tonic-gate 	int circular_count;
1444*0Sstevel@tonic-gate 	time_t time;
1445*0Sstevel@tonic-gate 
1446*0Sstevel@tonic-gate 	/*
1447*0Sstevel@tonic-gate 	 * check for valid request:
1448*0Sstevel@tonic-gate 	 *	1. It is a hotplug slot.
1449*0Sstevel@tonic-gate 	 *	2. The receptacle is in the CONNECTED state.
1450*0Sstevel@tonic-gate 	 */
1451*0Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
1452*0Sstevel@tonic-gate 
1453*0Sstevel@tonic-gate 	mutex_enter(&slotinfop->slot_mutex);
1454*0Sstevel@tonic-gate 
1455*0Sstevel@tonic-gate 	if ((pci_dev >= PCI_MAX_DEVS) || (slotinfop->slot_hdl == NULL) ||
1456*0Sstevel@tonic-gate 			(slotinfop->slot_flags & PCIHP_SLOT_DISABLED)) {
1457*0Sstevel@tonic-gate 		mutex_exit(&slotinfop->slot_mutex);
1458*0Sstevel@tonic-gate 		return (ENXIO);
1459*0Sstevel@tonic-gate 	}
1460*0Sstevel@tonic-gate 
1461*0Sstevel@tonic-gate 	/*
1462*0Sstevel@tonic-gate 	 * If the occupant is already in (partially?) configured
1463*0Sstevel@tonic-gate 	 * state then call the ndi_devi_online() on the device
1464*0Sstevel@tonic-gate 	 * subtree(s) for this attachment point.
1465*0Sstevel@tonic-gate 	 */
1466*0Sstevel@tonic-gate 
1467*0Sstevel@tonic-gate 	if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {
1468*0Sstevel@tonic-gate 		ctrl.flags = PCIHP_CFG_CONTINUE;
1469*0Sstevel@tonic-gate 		ctrl.rv = NDI_SUCCESS;
1470*0Sstevel@tonic-gate 		ctrl.dip = NULL;
1471*0Sstevel@tonic-gate 		ctrl.pci_dev = pci_dev;
1472*0Sstevel@tonic-gate 		ctrl.op = PCIHP_ONLINE;
1473*0Sstevel@tonic-gate 
1474*0Sstevel@tonic-gate 		ndi_devi_enter(self, &circular_count);
1475*0Sstevel@tonic-gate 		ddi_walk_devs(ddi_get_child(self), pcihp_configure,
1476*0Sstevel@tonic-gate 			(void *)&ctrl);
1477*0Sstevel@tonic-gate 		ndi_devi_exit(self, circular_count);
1478*0Sstevel@tonic-gate 
1479*0Sstevel@tonic-gate 		if (ctrl.rv != NDI_SUCCESS) {
1480*0Sstevel@tonic-gate 			/*
1481*0Sstevel@tonic-gate 			 * one or more of the devices are not
1482*0Sstevel@tonic-gate 			 * onlined. How is this to be reported?
1483*0Sstevel@tonic-gate 			 */
1484*0Sstevel@tonic-gate 			cmn_err(CE_WARN,
1485*0Sstevel@tonic-gate 				"pcihp (%s%d): failed to attach one or"
1486*0Sstevel@tonic-gate 				" more drivers for the card in the slot %s",
1487*0Sstevel@tonic-gate 				ddi_driver_name(self), ddi_get_instance(self),
1488*0Sstevel@tonic-gate 				slotinfop->name);
1489*0Sstevel@tonic-gate 			/* rv = EFAULT; */
1490*0Sstevel@tonic-gate 		}
1491*0Sstevel@tonic-gate 		/* tell HPC driver that the occupant is configured */
1492*0Sstevel@tonic-gate 		(void) hpc_nexus_control(slotinfop->slot_hdl,
1493*0Sstevel@tonic-gate 			HPC_CTRL_DEV_CONFIGURED, NULL);
1494*0Sstevel@tonic-gate 
1495*0Sstevel@tonic-gate 		if (drv_getparm(TIME, (void *)&time) != DDI_SUCCESS)
1496*0Sstevel@tonic-gate 			slotinfop->last_change = (time_t)-1;
1497*0Sstevel@tonic-gate 		else
1498*0Sstevel@tonic-gate 			slotinfop->last_change = (time32_t)time;
1499*0Sstevel@tonic-gate 
1500*0Sstevel@tonic-gate 		mutex_exit(&slotinfop->slot_mutex);
1501*0Sstevel@tonic-gate 		return (rv);
1502*0Sstevel@tonic-gate 	}
1503*0Sstevel@tonic-gate 
1504*0Sstevel@tonic-gate 	/*
1505*0Sstevel@tonic-gate 	 * Occupant is in the UNCONFIGURED state.
1506*0Sstevel@tonic-gate 	 */
1507*0Sstevel@tonic-gate 
1508*0Sstevel@tonic-gate 	/* Check if the receptacle is in the CONNECTED state. */
1509*0Sstevel@tonic-gate 	if (hpc_nexus_control(slotinfop->slot_hdl,
1510*0Sstevel@tonic-gate 			HPC_CTRL_GET_SLOT_STATE, (caddr_t)&rstate) != 0) {
1511*0Sstevel@tonic-gate 		mutex_exit(&slotinfop->slot_mutex);
1512*0Sstevel@tonic-gate 		return (ENXIO);
1513*0Sstevel@tonic-gate 	}
1514*0Sstevel@tonic-gate 
1515*0Sstevel@tonic-gate 	if (rstate == HPC_SLOT_EMPTY) {
1516*0Sstevel@tonic-gate 		/* error. slot is empty */
1517*0Sstevel@tonic-gate 		mutex_exit(&slotinfop->slot_mutex);
1518*0Sstevel@tonic-gate 		return (ENXIO);
1519*0Sstevel@tonic-gate 	}
1520*0Sstevel@tonic-gate 
1521*0Sstevel@tonic-gate 	if (rstate != HPC_SLOT_CONNECTED) {
1522*0Sstevel@tonic-gate 		/* error. either the slot is empty or connect failed */
1523*0Sstevel@tonic-gate 		mutex_exit(&slotinfop->slot_mutex);
1524*0Sstevel@tonic-gate 		return (ENXIO);
1525*0Sstevel@tonic-gate 	}
1526*0Sstevel@tonic-gate 
1527*0Sstevel@tonic-gate 	slotinfop->rstate = AP_RSTATE_CONNECTED; /* record rstate */
1528*0Sstevel@tonic-gate 
1529*0Sstevel@tonic-gate 	/* Turn INS and LED off, and start configuration. */
1530*0Sstevel@tonic-gate 	if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
1531*0Sstevel@tonic-gate 		pcihp_hs_csr_op(pcihp_p, pci_dev,
1532*0Sstevel@tonic-gate 				HPC_EVENT_SLOT_CONFIGURE);
1533*0Sstevel@tonic-gate 		if (pcihp_cpci_blue_led)
1534*0Sstevel@tonic-gate 			pcihp_hs_csr_op(pcihp_p, pci_dev,
1535*0Sstevel@tonic-gate 				HPC_EVENT_SLOT_BLUE_LED_OFF);
1536*0Sstevel@tonic-gate 		slotinfop->slot_flags &= ~PCIHP_SLOT_ENUM_INS_PENDING;
1537*0Sstevel@tonic-gate 	}
1538*0Sstevel@tonic-gate 
1539*0Sstevel@tonic-gate 	(void) hpc_nexus_control(slotinfop->slot_hdl,
1540*0Sstevel@tonic-gate 		HPC_CTRL_DEV_CONFIG_START, NULL);
1541*0Sstevel@tonic-gate 
1542*0Sstevel@tonic-gate 	/*
1543*0Sstevel@tonic-gate 	 * Call the configurator to configure the card.
1544*0Sstevel@tonic-gate 	 */
1545*0Sstevel@tonic-gate 	if (pcicfg_configure(self, pci_dev) != PCICFG_SUCCESS) {
1546*0Sstevel@tonic-gate 		if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
1547*0Sstevel@tonic-gate 			if (pcihp_cpci_blue_led)
1548*0Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
1549*0Sstevel@tonic-gate 					HPC_EVENT_SLOT_BLUE_LED_ON);
1550*0Sstevel@tonic-gate 			pcihp_hs_csr_op(pcihp_p, pci_dev,
1551*0Sstevel@tonic-gate 				HPC_EVENT_SLOT_UNCONFIGURE);
1552*0Sstevel@tonic-gate 		}
1553*0Sstevel@tonic-gate 		/* tell HPC driver occupant configure Error */
1554*0Sstevel@tonic-gate 		(void) hpc_nexus_control(slotinfop->slot_hdl,
1555*0Sstevel@tonic-gate 			HPC_CTRL_DEV_CONFIG_FAILURE, NULL);
1556*0Sstevel@tonic-gate 		mutex_exit(&slotinfop->slot_mutex);
1557*0Sstevel@tonic-gate 		return (EIO);
1558*0Sstevel@tonic-gate 	}
1559*0Sstevel@tonic-gate 
1560*0Sstevel@tonic-gate 	/* record the occupant state as CONFIGURED */
1561*0Sstevel@tonic-gate 	slotinfop->ostate = AP_OSTATE_CONFIGURED;
1562*0Sstevel@tonic-gate 	slotinfop->condition = AP_COND_OK;
1563*0Sstevel@tonic-gate 
1564*0Sstevel@tonic-gate 	/* now, online all the devices in the AP */
1565*0Sstevel@tonic-gate 	ctrl.flags = PCIHP_CFG_CONTINUE;
1566*0Sstevel@tonic-gate 	ctrl.rv = NDI_SUCCESS;
1567*0Sstevel@tonic-gate 	ctrl.dip = NULL;
1568*0Sstevel@tonic-gate 	ctrl.pci_dev = pci_dev;
1569*0Sstevel@tonic-gate 	ctrl.op = PCIHP_ONLINE;
1570*0Sstevel@tonic-gate 
1571*0Sstevel@tonic-gate 	ndi_devi_enter(self, &circular_count);
1572*0Sstevel@tonic-gate 	ddi_walk_devs(ddi_get_child(self), pcihp_configure,
1573*0Sstevel@tonic-gate 		(void *)&ctrl);
1574*0Sstevel@tonic-gate 	ndi_devi_exit(self, circular_count);
1575*0Sstevel@tonic-gate 
1576*0Sstevel@tonic-gate 	if (ctrl.rv != NDI_SUCCESS) {
1577*0Sstevel@tonic-gate 		/*
1578*0Sstevel@tonic-gate 		 * one or more of the devices are not
1579*0Sstevel@tonic-gate 		 * ONLINE'd. How is this to be
1580*0Sstevel@tonic-gate 		 * reported?
1581*0Sstevel@tonic-gate 		 */
1582*0Sstevel@tonic-gate 		cmn_err(CE_WARN,
1583*0Sstevel@tonic-gate 			"pcihp (%s%d): failed to attach one or"
1584*0Sstevel@tonic-gate 			" more drivers for the card in"
1585*0Sstevel@tonic-gate 			" the slot %s",
1586*0Sstevel@tonic-gate 			ddi_driver_name(pcihp_p->dip),
1587*0Sstevel@tonic-gate 			ddi_get_instance(pcihp_p->dip),
1588*0Sstevel@tonic-gate 			slotinfop->name);
1589*0Sstevel@tonic-gate 		/* rv = EFAULT; */
1590*0Sstevel@tonic-gate 	}
1591*0Sstevel@tonic-gate 	/* store HS_CSR location.  No events, jut a read operation. */
1592*0Sstevel@tonic-gate 	if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI)
1593*0Sstevel@tonic-gate 		pcihp_hs_csr_op(pcihp_p, pci_dev, -1);
1594*0Sstevel@tonic-gate 
1595*0Sstevel@tonic-gate 	/* tell HPC driver that the occupant is configured */
1596*0Sstevel@tonic-gate 	(void) hpc_nexus_control(slotinfop->slot_hdl,
1597*0Sstevel@tonic-gate 		HPC_CTRL_DEV_CONFIGURED, NULL);
1598*0Sstevel@tonic-gate 
1599*0Sstevel@tonic-gate 	mutex_exit(&slotinfop->slot_mutex);
1600*0Sstevel@tonic-gate 	return (rv);
1601*0Sstevel@tonic-gate }
1602*0Sstevel@tonic-gate 
1603*0Sstevel@tonic-gate /*
1604*0Sstevel@tonic-gate  * **************************************
1605*0Sstevel@tonic-gate  * UNCONFIGURE the occupant in the slot.
1606*0Sstevel@tonic-gate  * **************************************
1607*0Sstevel@tonic-gate  */
1608*0Sstevel@tonic-gate static int
1609*0Sstevel@tonic-gate pcihp_unconfigure_ap(pcihp_t *pcihp_p, int pci_dev)
1610*0Sstevel@tonic-gate {
1611*0Sstevel@tonic-gate 	dev_info_t *self = pcihp_p->dip;
1612*0Sstevel@tonic-gate 	int rv = HPC_SUCCESS;
1613*0Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
1614*0Sstevel@tonic-gate 	struct pcihp_config_ctrl ctrl;
1615*0Sstevel@tonic-gate 	int circular_count;
1616*0Sstevel@tonic-gate 	time_t time;
1617*0Sstevel@tonic-gate 
1618*0Sstevel@tonic-gate 	/*
1619*0Sstevel@tonic-gate 	 * check for valid request:
1620*0Sstevel@tonic-gate 	 *	1. It is a hotplug slot.
1621*0Sstevel@tonic-gate 	 *	2. The occupant is in the CONFIGURED state.
1622*0Sstevel@tonic-gate 	 */
1623*0Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
1624*0Sstevel@tonic-gate 
1625*0Sstevel@tonic-gate 	mutex_enter(&slotinfop->slot_mutex);
1626*0Sstevel@tonic-gate 
1627*0Sstevel@tonic-gate 	if ((pci_dev >= PCI_MAX_DEVS) || (slotinfop->slot_hdl == NULL) ||
1628*0Sstevel@tonic-gate 			(slotinfop->slot_flags & PCIHP_SLOT_DISABLED)) {
1629*0Sstevel@tonic-gate 		mutex_exit(&slotinfop->slot_mutex);
1630*0Sstevel@tonic-gate 		return (ENXIO);
1631*0Sstevel@tonic-gate 	}
1632*0Sstevel@tonic-gate 	/*
1633*0Sstevel@tonic-gate 	 * The following may not need to be there, as we should
1634*0Sstevel@tonic-gate 	 * support unconfiguring of boards and free resources
1635*0Sstevel@tonic-gate 	 * even when the board is not hotswappable. But this is
1636*0Sstevel@tonic-gate 	 * the only way, we may be able to tell the system
1637*0Sstevel@tonic-gate 	 * administrator that it is not a hotswap board since
1638*0Sstevel@tonic-gate 	 * disconnect operation is never called.
1639*0Sstevel@tonic-gate 	 * This way we help the system administrator from not
1640*0Sstevel@tonic-gate 	 * accidentally removing a non hotswap board and
1641*0Sstevel@tonic-gate 	 * possibly destroying it. May be this behavior can
1642*0Sstevel@tonic-gate 	 * be a default, and can be enabled or disabled via
1643*0Sstevel@tonic-gate 	 * a global flag.
1644*0Sstevel@tonic-gate 	 */
1645*0Sstevel@tonic-gate 	if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
1646*0Sstevel@tonic-gate 		if (slotinfop->slot_flags &
1647*0Sstevel@tonic-gate 				PCIHP_SLOT_DEV_NON_HOTPLUG) {
1648*0Sstevel@tonic-gate 			/* Operation unsupported if no HS board/slot */
1649*0Sstevel@tonic-gate 			mutex_exit(&slotinfop->slot_mutex);
1650*0Sstevel@tonic-gate 			return (ENOTSUP);
1651*0Sstevel@tonic-gate 		}
1652*0Sstevel@tonic-gate 	}
1653*0Sstevel@tonic-gate 
1654*0Sstevel@tonic-gate 	/*
1655*0Sstevel@tonic-gate 	 * If the occupant is in the CONFIGURED state then
1656*0Sstevel@tonic-gate 	 * call the configurator to unconfigure the slot.
1657*0Sstevel@tonic-gate 	 */
1658*0Sstevel@tonic-gate 	if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {
1659*0Sstevel@tonic-gate 
1660*0Sstevel@tonic-gate 		/*
1661*0Sstevel@tonic-gate 		 * since potential state change is imminent mask
1662*0Sstevel@tonic-gate 		 * enum events to prevent the slot from being re-configured
1663*0Sstevel@tonic-gate 		 */
1664*0Sstevel@tonic-gate 		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_DISABLE_ENUM);
1665*0Sstevel@tonic-gate 
1666*0Sstevel@tonic-gate 		/*
1667*0Sstevel@tonic-gate 		 * Detach all the drivers for the devices in the
1668*0Sstevel@tonic-gate 		 * slot. Call pcihp_configure() to do this.
1669*0Sstevel@tonic-gate 		 */
1670*0Sstevel@tonic-gate 		ctrl.flags = 0;
1671*0Sstevel@tonic-gate 		ctrl.rv = NDI_SUCCESS;
1672*0Sstevel@tonic-gate 		ctrl.dip = NULL;
1673*0Sstevel@tonic-gate 		ctrl.pci_dev = pci_dev;
1674*0Sstevel@tonic-gate 		ctrl.op = PCIHP_OFFLINE;
1675*0Sstevel@tonic-gate 
1676*0Sstevel@tonic-gate 		(void) devfs_clean(self, NULL, DV_CLEAN_FORCE);
1677*0Sstevel@tonic-gate 		ndi_devi_enter(self, &circular_count);
1678*0Sstevel@tonic-gate 		ddi_walk_devs(ddi_get_child(self), pcihp_configure,
1679*0Sstevel@tonic-gate 			(void *)&ctrl);
1680*0Sstevel@tonic-gate 		ndi_devi_exit(self, circular_count);
1681*0Sstevel@tonic-gate 
1682*0Sstevel@tonic-gate 		if (ctrl.rv != NDI_SUCCESS) {
1683*0Sstevel@tonic-gate 			/*
1684*0Sstevel@tonic-gate 			 * Failed to detach one or more drivers
1685*0Sstevel@tonic-gate 			 * Restore the state of drivers which
1686*0Sstevel@tonic-gate 			 * are offlined during this operation.
1687*0Sstevel@tonic-gate 			 */
1688*0Sstevel@tonic-gate 			ctrl.flags = 0;
1689*0Sstevel@tonic-gate 			ctrl.rv = NDI_SUCCESS;
1690*0Sstevel@tonic-gate 			ctrl.dip = NULL;
1691*0Sstevel@tonic-gate 			ctrl.pci_dev = pci_dev;
1692*0Sstevel@tonic-gate 			ctrl.op = PCIHP_ONLINE;
1693*0Sstevel@tonic-gate 
1694*0Sstevel@tonic-gate 			ndi_devi_enter(self, &circular_count);
1695*0Sstevel@tonic-gate 			ddi_walk_devs(ddi_get_child(self),
1696*0Sstevel@tonic-gate 				pcihp_configure, (void *)&ctrl);
1697*0Sstevel@tonic-gate 			ndi_devi_exit(self, circular_count);
1698*0Sstevel@tonic-gate 
1699*0Sstevel@tonic-gate 			/* tell HPC driver that the occupant is Busy */
1700*0Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
1701*0Sstevel@tonic-gate 				HPC_CTRL_DEV_UNCONFIG_FAILURE, NULL);
1702*0Sstevel@tonic-gate 
1703*0Sstevel@tonic-gate 			rv = EBUSY;
1704*0Sstevel@tonic-gate 		} else {
1705*0Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
1706*0Sstevel@tonic-gate 				HPC_CTRL_DEV_UNCONFIG_START, NULL);
1707*0Sstevel@tonic-gate 
1708*0Sstevel@tonic-gate 			if (pcicfg_unconfigure(self,
1709*0Sstevel@tonic-gate 					pci_dev) == PCICFG_SUCCESS) {
1710*0Sstevel@tonic-gate 			/*
1711*0Sstevel@tonic-gate 			 * Now that resources are freed,
1712*0Sstevel@tonic-gate 			 * clear EXT and Turn LED ON.
1713*0Sstevel@tonic-gate 			 */
1714*0Sstevel@tonic-gate 			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
1715*0Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
1716*0Sstevel@tonic-gate 					HPC_EVENT_SLOT_UNCONFIGURE);
1717*0Sstevel@tonic-gate 				if (pcihp_cpci_blue_led)
1718*0Sstevel@tonic-gate 					pcihp_hs_csr_op(pcihp_p, pci_dev,
1719*0Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_ON);
1720*0Sstevel@tonic-gate 				slotinfop->hs_csr_location = 0;
1721*0Sstevel@tonic-gate 				slotinfop->slot_flags &=
1722*0Sstevel@tonic-gate 					~(PCIHP_SLOT_DEV_NON_HOTPLUG|
1723*0Sstevel@tonic-gate 						PCIHP_SLOT_ENUM_EXT_PENDING);
1724*0Sstevel@tonic-gate 			}
1725*0Sstevel@tonic-gate 				slotinfop->ostate =
1726*0Sstevel@tonic-gate 						AP_OSTATE_UNCONFIGURED;
1727*0Sstevel@tonic-gate 				slotinfop->condition = AP_COND_UNKNOWN;
1728*0Sstevel@tonic-gate 				/*
1729*0Sstevel@tonic-gate 				 * send the notification of state change
1730*0Sstevel@tonic-gate 				 * to the HPC driver.
1731*0Sstevel@tonic-gate 				 */
1732*0Sstevel@tonic-gate 				(void) hpc_nexus_control(
1733*0Sstevel@tonic-gate 					slotinfop->slot_hdl,
1734*0Sstevel@tonic-gate 					HPC_CTRL_DEV_UNCONFIGURED,
1735*0Sstevel@tonic-gate 					NULL);
1736*0Sstevel@tonic-gate 			} else {
1737*0Sstevel@tonic-gate 				/* tell HPC driver occupant unconfigure Error */
1738*0Sstevel@tonic-gate 				(void) hpc_nexus_control(slotinfop->slot_hdl,
1739*0Sstevel@tonic-gate 					HPC_CTRL_DEV_UNCONFIG_FAILURE, NULL);
1740*0Sstevel@tonic-gate 
1741*0Sstevel@tonic-gate 				rv = EIO;
1742*0Sstevel@tonic-gate 			}
1743*0Sstevel@tonic-gate 		}
1744*0Sstevel@tonic-gate 	}
1745*0Sstevel@tonic-gate 
1746*0Sstevel@tonic-gate 	if (drv_getparm(TIME, (void *)&time) != DDI_SUCCESS)
1747*0Sstevel@tonic-gate 		slotinfop->last_change = (time_t)-1;
1748*0Sstevel@tonic-gate 	else
1749*0Sstevel@tonic-gate 		slotinfop->last_change = (time32_t)time;
1750*0Sstevel@tonic-gate 
1751*0Sstevel@tonic-gate 	mutex_exit(&slotinfop->slot_mutex);
1752*0Sstevel@tonic-gate 
1753*0Sstevel@tonic-gate 	/* unmask enum events again */
1754*0Sstevel@tonic-gate 	if ((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) == 0) {
1755*0Sstevel@tonic-gate 		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_ENABLE_ENUM);
1756*0Sstevel@tonic-gate 	}
1757*0Sstevel@tonic-gate 
1758*0Sstevel@tonic-gate 	return (rv);
1759*0Sstevel@tonic-gate }
1760*0Sstevel@tonic-gate 
1761*0Sstevel@tonic-gate /*
1762*0Sstevel@tonic-gate  * Accessor function to return pointer to the pci hotplug
1763*0Sstevel@tonic-gate  * cb_ops structure.
1764*0Sstevel@tonic-gate  */
1765*0Sstevel@tonic-gate struct cb_ops *
1766*0Sstevel@tonic-gate pcihp_get_cb_ops()
1767*0Sstevel@tonic-gate {
1768*0Sstevel@tonic-gate 	return (&pcihp_cb_ops);
1769*0Sstevel@tonic-gate }
1770*0Sstevel@tonic-gate 
1771*0Sstevel@tonic-gate /*
1772*0Sstevel@tonic-gate  * Setup function to initialize hot plug feature. Returns DDI_SUCCESS
1773*0Sstevel@tonic-gate  * for successful initialization, otherwise it returns DDI_FAILURE.
1774*0Sstevel@tonic-gate  *
1775*0Sstevel@tonic-gate  * It is assumed that this this function is called from the attach()
1776*0Sstevel@tonic-gate  * entry point of the PCI nexus driver.
1777*0Sstevel@tonic-gate  */
1778*0Sstevel@tonic-gate 
1779*0Sstevel@tonic-gate int
1780*0Sstevel@tonic-gate pcihp_init(dev_info_t *dip)
1781*0Sstevel@tonic-gate {
1782*0Sstevel@tonic-gate 	pcihp_t *pcihp_p;
1783*0Sstevel@tonic-gate 	int i;
1784*0Sstevel@tonic-gate 	caddr_t enum_data;
1785*0Sstevel@tonic-gate 	int enum_size;
1786*0Sstevel@tonic-gate 	int rv;
1787*0Sstevel@tonic-gate 
1788*0Sstevel@tonic-gate 	mutex_enter(&pcihp_open_mutex);
1789*0Sstevel@tonic-gate 
1790*0Sstevel@tonic-gate 	/*
1791*0Sstevel@tonic-gate 	 * Make sure that it is not already initialized.
1792*0Sstevel@tonic-gate 	 */
1793*0Sstevel@tonic-gate 	if (pcihp_get_soft_state(dip, PCIHP_DR_NOOP, &rv) != NULL) {
1794*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: pcihp instance already initialized!",
1795*0Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
1796*0Sstevel@tonic-gate 		goto cleanup;
1797*0Sstevel@tonic-gate 	}
1798*0Sstevel@tonic-gate 
1799*0Sstevel@tonic-gate 	/*
1800*0Sstevel@tonic-gate 	 * Initialize soft state structure for the bus instance.
1801*0Sstevel@tonic-gate 	 */
1802*0Sstevel@tonic-gate 	if ((pcihp_p = pcihp_create_soft_state(dip)) == NULL) {
1803*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: can't allocate pcihp structure",
1804*0Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
1805*0Sstevel@tonic-gate 		goto cleanup;
1806*0Sstevel@tonic-gate 	}
1807*0Sstevel@tonic-gate 
1808*0Sstevel@tonic-gate 	pcihp_p->soft_state = PCIHP_SOFT_STATE_CLOSED;
1809*0Sstevel@tonic-gate 	/* XXX if bus is running at 66Mhz then set PCI_BUS_66MHZ bit */
1810*0Sstevel@tonic-gate 	pcihp_p->bus_flags = 0;	/* XXX FIX IT */
1811*0Sstevel@tonic-gate 
1812*0Sstevel@tonic-gate 	/*
1813*0Sstevel@tonic-gate 	 * If a platform wishes to implement Radial ENUM# routing
1814*0Sstevel@tonic-gate 	 * a property "enum-impl" must be presented to us with a
1815*0Sstevel@tonic-gate 	 * string value "radial".
1816*0Sstevel@tonic-gate 	 * This helps us not go for polling operation (default)
1817*0Sstevel@tonic-gate 	 * during a ENUM# event.
1818*0Sstevel@tonic-gate 	 */
1819*0Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, 0, "enum-impl",
1820*0Sstevel@tonic-gate 	    (caddr_t)&enum_data, &enum_size) == DDI_PROP_SUCCESS) {
1821*0Sstevel@tonic-gate 		if (strcmp(enum_data, "radial") == 0) {
1822*0Sstevel@tonic-gate 			pcihp_p->bus_flags |= PCIHP_BUS_ENUM_RADIAL;
1823*0Sstevel@tonic-gate 		}
1824*0Sstevel@tonic-gate 		kmem_free(enum_data, enum_size);
1825*0Sstevel@tonic-gate 	}
1826*0Sstevel@tonic-gate 
1827*0Sstevel@tonic-gate 	for (i = 0; i < PCI_MAX_DEVS; i++) {
1828*0Sstevel@tonic-gate 		/* initialize slot mutex */
1829*0Sstevel@tonic-gate 		mutex_init(&pcihp_p->slotinfo[i].slot_mutex, NULL,
1830*0Sstevel@tonic-gate 						MUTEX_DRIVER, NULL);
1831*0Sstevel@tonic-gate 	}
1832*0Sstevel@tonic-gate 
1833*0Sstevel@tonic-gate 	/*
1834*0Sstevel@tonic-gate 	 *  register the bus instance with the HPS framework.
1835*0Sstevel@tonic-gate 	 */
1836*0Sstevel@tonic-gate 	if (hpc_nexus_register_bus(dip, pcihp_new_slot_state, 0) != 0) {
1837*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: failed to register the bus with HPS",
1838*0Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
1839*0Sstevel@tonic-gate 		goto cleanup1;
1840*0Sstevel@tonic-gate 	}
1841*0Sstevel@tonic-gate 
1842*0Sstevel@tonic-gate 	/*
1843*0Sstevel@tonic-gate 	 * Create the "devctl" minor for hot plug support. The minor
1844*0Sstevel@tonic-gate 	 * number for "devctl" node is in the same format as the AP
1845*0Sstevel@tonic-gate 	 * minor nodes.
1846*0Sstevel@tonic-gate 	 */
1847*0Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
1848*0Sstevel@tonic-gate 	    PCIHP_AP_MINOR_NUM(ddi_get_instance(dip), PCIHP_DEVCTL_MINOR),
1849*0Sstevel@tonic-gate 	    DDI_NT_NEXUS, 0) != DDI_SUCCESS)
1850*0Sstevel@tonic-gate 		goto cleanup2;
1851*0Sstevel@tonic-gate 
1852*0Sstevel@tonic-gate 	/*
1853*0Sstevel@tonic-gate 	 * Setup resource maps for this bus node. (Note: This can
1854*0Sstevel@tonic-gate 	 * be done from the attach(9E) of the nexus itself.)
1855*0Sstevel@tonic-gate 	 */
1856*0Sstevel@tonic-gate 	(void) pci_resource_setup(dip);
1857*0Sstevel@tonic-gate 
1858*0Sstevel@tonic-gate 	pcihp_p->bus_state = PCIHP_BUS_CONFIGURED;
1859*0Sstevel@tonic-gate 
1860*0Sstevel@tonic-gate 	mutex_exit(&pcihp_open_mutex);
1861*0Sstevel@tonic-gate 
1862*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
1863*0Sstevel@tonic-gate 
1864*0Sstevel@tonic-gate cleanup2:
1865*0Sstevel@tonic-gate 	(void) hpc_nexus_unregister_bus(dip);
1866*0Sstevel@tonic-gate cleanup1:
1867*0Sstevel@tonic-gate 	for (i = 0; i < PCI_MAX_DEVS; i++)
1868*0Sstevel@tonic-gate 		mutex_destroy(&pcihp_p->slotinfo[i].slot_mutex);
1869*0Sstevel@tonic-gate 	pcihp_destroy_soft_state(dip);
1870*0Sstevel@tonic-gate cleanup:
1871*0Sstevel@tonic-gate 	mutex_exit(&pcihp_open_mutex);
1872*0Sstevel@tonic-gate 	return (DDI_FAILURE);
1873*0Sstevel@tonic-gate }
1874*0Sstevel@tonic-gate 
1875*0Sstevel@tonic-gate /*
1876*0Sstevel@tonic-gate  * pcihp_uninit()
1877*0Sstevel@tonic-gate  *
1878*0Sstevel@tonic-gate  * The bus instance is going away, cleanup any data associated with
1879*0Sstevel@tonic-gate  * the management of hot plug slots. It is assumed that this function
1880*0Sstevel@tonic-gate  * is called from detach() routine of the PCI nexus driver. Also,
1881*0Sstevel@tonic-gate  * it is assumed that no devices on the bus are in the configured state.
1882*0Sstevel@tonic-gate  */
1883*0Sstevel@tonic-gate int
1884*0Sstevel@tonic-gate pcihp_uninit(dev_info_t *dip)
1885*0Sstevel@tonic-gate {
1886*0Sstevel@tonic-gate 	pcihp_t *pcihp_p;
1887*0Sstevel@tonic-gate 	int i, j;
1888*0Sstevel@tonic-gate 	int rv;
1889*0Sstevel@tonic-gate 
1890*0Sstevel@tonic-gate 	mutex_enter(&pcihp_open_mutex);
1891*0Sstevel@tonic-gate 	/* get a pointer to the soft state structure */
1892*0Sstevel@tonic-gate 	pcihp_p = pcihp_get_soft_state(dip, PCIHP_DR_BUS_UNCONFIGURE, &rv);
1893*0Sstevel@tonic-gate 	ASSERT(pcihp_p != NULL);
1894*0Sstevel@tonic-gate 
1895*0Sstevel@tonic-gate 	/* slot mutexes should prevent any configure/unconfigure access */
1896*0Sstevel@tonic-gate 	for (i = 0; i < PCI_MAX_DEVS; i++) {
1897*0Sstevel@tonic-gate 		if (!mutex_tryenter(&pcihp_p->slotinfo[i].slot_mutex)) {
1898*0Sstevel@tonic-gate 			for (j = 0; j < i; j++) {
1899*0Sstevel@tonic-gate 				mutex_exit(&pcihp_p->slotinfo[j].slot_mutex);
1900*0Sstevel@tonic-gate 			}
1901*0Sstevel@tonic-gate 			mutex_exit(&pcihp_open_mutex);
1902*0Sstevel@tonic-gate 			return (DDI_FAILURE);
1903*0Sstevel@tonic-gate 		}
1904*0Sstevel@tonic-gate 	}
1905*0Sstevel@tonic-gate 
1906*0Sstevel@tonic-gate 	if ((pcihp_p->soft_state != PCIHP_SOFT_STATE_CLOSED) ||
1907*0Sstevel@tonic-gate 	    (rv == PCIHP_FAILURE)) {
1908*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: pcihp instance is busy",
1909*0Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
1910*0Sstevel@tonic-gate 		for (i = 0; i < PCI_MAX_DEVS; i++) {
1911*0Sstevel@tonic-gate 			mutex_exit(&pcihp_p->slotinfo[i].slot_mutex);
1912*0Sstevel@tonic-gate 		}
1913*0Sstevel@tonic-gate 		mutex_exit(&pcihp_open_mutex);
1914*0Sstevel@tonic-gate 		return (DDI_FAILURE);
1915*0Sstevel@tonic-gate 	}
1916*0Sstevel@tonic-gate 
1917*0Sstevel@tonic-gate 	/*
1918*0Sstevel@tonic-gate 	 * Unregister the bus with the HPS.
1919*0Sstevel@tonic-gate 	 *
1920*0Sstevel@tonic-gate 	 * (Note: It is assumed that the HPS framework uninstalls
1921*0Sstevel@tonic-gate 	 *  event handlers for all the hot plug slots on this bus.)
1922*0Sstevel@tonic-gate 	 */
1923*0Sstevel@tonic-gate 	(void) hpc_nexus_unregister_bus(dip);
1924*0Sstevel@tonic-gate 
1925*0Sstevel@tonic-gate 	/* Free up any kmem_alloc'd memory for slot info table. */
1926*0Sstevel@tonic-gate 	for (i = 0; i < PCI_MAX_DEVS; i++) {
1927*0Sstevel@tonic-gate 		/* free up slot name strings */
1928*0Sstevel@tonic-gate 		if (pcihp_p->slotinfo[i].name != NULL)
1929*0Sstevel@tonic-gate 			kmem_free(pcihp_p->slotinfo[i].name,
1930*0Sstevel@tonic-gate 				strlen(pcihp_p->slotinfo[i].name) + 1);
1931*0Sstevel@tonic-gate 	}
1932*0Sstevel@tonic-gate 
1933*0Sstevel@tonic-gate 	/* destroy slot mutexes */
1934*0Sstevel@tonic-gate 	for (i = 0; i < PCI_MAX_DEVS; i++)
1935*0Sstevel@tonic-gate 		mutex_destroy(&pcihp_p->slotinfo[i].slot_mutex);
1936*0Sstevel@tonic-gate 
1937*0Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
1938*0Sstevel@tonic-gate 
1939*0Sstevel@tonic-gate 	/* free up the soft state structure */
1940*0Sstevel@tonic-gate 	pcihp_destroy_soft_state(dip);
1941*0Sstevel@tonic-gate 
1942*0Sstevel@tonic-gate 	/*
1943*0Sstevel@tonic-gate 	 * Destroy resource maps for this bus node. (Note: This can
1944*0Sstevel@tonic-gate 	 * be done from the detach(9E) of the nexus itself.)
1945*0Sstevel@tonic-gate 	 */
1946*0Sstevel@tonic-gate 	(void) pci_resource_destroy(dip);
1947*0Sstevel@tonic-gate 
1948*0Sstevel@tonic-gate 	mutex_exit(&pcihp_open_mutex);
1949*0Sstevel@tonic-gate 
1950*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
1951*0Sstevel@tonic-gate }
1952*0Sstevel@tonic-gate 
1953*0Sstevel@tonic-gate /*
1954*0Sstevel@tonic-gate  * pcihp_new_slot_state()
1955*0Sstevel@tonic-gate  *
1956*0Sstevel@tonic-gate  * This function is called by the HPS when it finds a hot plug
1957*0Sstevel@tonic-gate  * slot is added or being removed from the hot plug framework.
1958*0Sstevel@tonic-gate  * It returns 0 for success and HPC_ERR_FAILED for errors.
1959*0Sstevel@tonic-gate  */
1960*0Sstevel@tonic-gate static int
1961*0Sstevel@tonic-gate pcihp_new_slot_state(dev_info_t *dip, hpc_slot_t hdl,
1962*0Sstevel@tonic-gate 	hpc_slot_info_t *slot_info, int slot_state)
1963*0Sstevel@tonic-gate {
1964*0Sstevel@tonic-gate 	pcihp_t *pcihp_p;
1965*0Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
1966*0Sstevel@tonic-gate 	int pci_dev;
1967*0Sstevel@tonic-gate 	minor_t ap_minor;
1968*0Sstevel@tonic-gate 	major_t ap_major;
1969*0Sstevel@tonic-gate 	int rv = 0;
1970*0Sstevel@tonic-gate 	time_t time;
1971*0Sstevel@tonic-gate 	int auto_enable = 1;
1972*0Sstevel@tonic-gate 	int rval;
1973*0Sstevel@tonic-gate 
1974*0Sstevel@tonic-gate 	/* get a pointer to the soft state structure */
1975*0Sstevel@tonic-gate 	pcihp_p = pcihp_get_soft_state(dip, PCIHP_DR_SLOT_ENTER, &rval);
1976*0Sstevel@tonic-gate 	ASSERT(pcihp_p != NULL);
1977*0Sstevel@tonic-gate 
1978*0Sstevel@tonic-gate 	if (rval == PCIHP_FAILURE) {
1979*0Sstevel@tonic-gate 		PCIHP_DEBUG((CE_WARN, "pcihp instance is unconfigured"
1980*0Sstevel@tonic-gate 		    " while slot activity is requested\n"));
1981*0Sstevel@tonic-gate 		return (HPC_ERR_FAILED);
1982*0Sstevel@tonic-gate 	}
1983*0Sstevel@tonic-gate 
1984*0Sstevel@tonic-gate 	pci_dev = slot_info->pci_dev_num;
1985*0Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
1986*0Sstevel@tonic-gate 
1987*0Sstevel@tonic-gate 	mutex_enter(&slotinfop->slot_mutex);
1988*0Sstevel@tonic-gate 
1989*0Sstevel@tonic-gate 	switch (slot_state) {
1990*0Sstevel@tonic-gate 
1991*0Sstevel@tonic-gate 	case HPC_SLOT_ONLINE:
1992*0Sstevel@tonic-gate 
1993*0Sstevel@tonic-gate 		/*
1994*0Sstevel@tonic-gate 		 * Make sure the slot is not already ONLINE (paranoia?).
1995*0Sstevel@tonic-gate 		 * (Note: Should this be simply an ASSERTION?)
1996*0Sstevel@tonic-gate 		 */
1997*0Sstevel@tonic-gate 		if (slotinfop->slot_hdl != NULL) {
1998*0Sstevel@tonic-gate 		    PCIHP_DEBUG((CE_WARN,
1999*0Sstevel@tonic-gate 			"pcihp (%s%d): pci slot (dev %x) already ONLINE!!",
2000*0Sstevel@tonic-gate 			ddi_driver_name(dip), ddi_get_instance(dip), pci_dev));
2001*0Sstevel@tonic-gate 			rv = HPC_ERR_FAILED;
2002*0Sstevel@tonic-gate 			break;
2003*0Sstevel@tonic-gate 		}
2004*0Sstevel@tonic-gate 
2005*0Sstevel@tonic-gate 		/*
2006*0Sstevel@tonic-gate 		 * Add the hot plug slot to the bus.
2007*0Sstevel@tonic-gate 		 */
2008*0Sstevel@tonic-gate 
2009*0Sstevel@tonic-gate 		/* create the AP minor node */
2010*0Sstevel@tonic-gate 		ap_minor = PCIHP_AP_MINOR_NUM(ddi_get_instance(dip), pci_dev);
2011*0Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, slot_info->pci_slot_name,
2012*0Sstevel@tonic-gate 			S_IFCHR, ap_minor,
2013*0Sstevel@tonic-gate 			DDI_NT_PCI_ATTACHMENT_POINT, 0) == DDI_FAILURE) {
2014*0Sstevel@tonic-gate 		    cmn_err(CE_WARN,
2015*0Sstevel@tonic-gate 			"pcihp (%s%d): ddi_create_minor_node failed"
2016*0Sstevel@tonic-gate 			" for pci dev %x", ddi_driver_name(dip),
2017*0Sstevel@tonic-gate 			ddi_get_instance(dip), pci_dev);
2018*0Sstevel@tonic-gate 		    rv = HPC_ERR_FAILED;
2019*0Sstevel@tonic-gate 		    break;
2020*0Sstevel@tonic-gate 		}
2021*0Sstevel@tonic-gate 
2022*0Sstevel@tonic-gate 		/* save the slot handle */
2023*0Sstevel@tonic-gate 		slotinfop->slot_hdl = hdl;
2024*0Sstevel@tonic-gate 
2025*0Sstevel@tonic-gate 		/* setup event handler for all hardware events on the slot */
2026*0Sstevel@tonic-gate 		ap_major = ddi_name_to_major(ddi_get_name(dip));
2027*0Sstevel@tonic-gate 		if (hpc_install_event_handler(hdl, -1, pcihp_event_handler,
2028*0Sstevel@tonic-gate 			(caddr_t)makedevice(ap_major, ap_minor)) != 0) {
2029*0Sstevel@tonic-gate 		    cmn_err(CE_WARN,
2030*0Sstevel@tonic-gate 			"pcihp (%s%d): install event handler failed"
2031*0Sstevel@tonic-gate 			" for pci dev %x", ddi_driver_name(dip),
2032*0Sstevel@tonic-gate 			ddi_get_instance(dip), pci_dev);
2033*0Sstevel@tonic-gate 		    rv = HPC_ERR_FAILED;
2034*0Sstevel@tonic-gate 		    break;
2035*0Sstevel@tonic-gate 		}
2036*0Sstevel@tonic-gate 		slotinfop->event_mask = (uint32_t)0xFFFFFFFF;
2037*0Sstevel@tonic-gate 
2038*0Sstevel@tonic-gate 		pcihp_create_occupant_props(dip, makedevice(ap_major,
2039*0Sstevel@tonic-gate 		    ap_minor), pci_dev);
2040*0Sstevel@tonic-gate 
2041*0Sstevel@tonic-gate 		/* set default auto configuration enabled flag for this slot */
2042*0Sstevel@tonic-gate 		slotinfop->slot_flags = pcihp_autocfg_enabled;
2043*0Sstevel@tonic-gate 
2044*0Sstevel@tonic-gate 		/* copy the slot information */
2045*0Sstevel@tonic-gate 		slotinfop->name =
2046*0Sstevel@tonic-gate 		    kmem_alloc(strlen(slot_info->pci_slot_name) + 1, KM_SLEEP);
2047*0Sstevel@tonic-gate 		(void) strcpy(slotinfop->name, slot_info->pci_slot_name);
2048*0Sstevel@tonic-gate 		slotinfop->slot_type = slot_info->slot_type;
2049*0Sstevel@tonic-gate 		slotinfop->hs_csr_location = 0;
2050*0Sstevel@tonic-gate 		slotinfop->slot_capabilities = slot_info->pci_slot_capabilities;
2051*0Sstevel@tonic-gate 		if (slot_info->slot_flags & HPC_SLOT_NO_AUTO_ENABLE)
2052*0Sstevel@tonic-gate 			auto_enable = 0;
2053*0Sstevel@tonic-gate 
2054*0Sstevel@tonic-gate 		if (slot_info->slot_flags & HPC_SLOT_CREATE_DEVLINK) {
2055*0Sstevel@tonic-gate 			pci_devlink_flags |= (1 << pci_dev);
2056*0Sstevel@tonic-gate 			(void) ddi_prop_update_int(DDI_DEV_T_NONE,
2057*0Sstevel@tonic-gate 						dip,
2058*0Sstevel@tonic-gate 						"ap-names",
2059*0Sstevel@tonic-gate 						pci_devlink_flags);
2060*0Sstevel@tonic-gate 		}
2061*0Sstevel@tonic-gate 
2062*0Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE,
2063*0Sstevel@tonic-gate 		    "pcihp (%s%d): pci slot (dev %x) ONLINE\n",
2064*0Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip), pci_dev));
2065*0Sstevel@tonic-gate 
2066*0Sstevel@tonic-gate 		/*
2067*0Sstevel@tonic-gate 		 * The slot may have an occupant that was configured
2068*0Sstevel@tonic-gate 		 * at boot time. If we find a devinfo node in the tree
2069*0Sstevel@tonic-gate 		 * for this slot (i.e pci device number) then we
2070*0Sstevel@tonic-gate 		 * record the occupant state as CONFIGURED.
2071*0Sstevel@tonic-gate 		 */
2072*0Sstevel@tonic-gate 		if (pcihp_devi_find(dip, pci_dev, 0) != NULL) {
2073*0Sstevel@tonic-gate 			/* we have a configured occupant */
2074*0Sstevel@tonic-gate 			slotinfop->ostate = AP_OSTATE_CONFIGURED;
2075*0Sstevel@tonic-gate 			slotinfop->rstate = AP_RSTATE_CONNECTED;
2076*0Sstevel@tonic-gate 			slotinfop->condition = AP_COND_OK;
2077*0Sstevel@tonic-gate 
2078*0Sstevel@tonic-gate 			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
2079*0Sstevel@tonic-gate 				/* this will set slot flags too. */
2080*0Sstevel@tonic-gate 				(void) pcihp_get_board_type(slotinfop);
2081*0Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
2082*0Sstevel@tonic-gate 					HPC_EVENT_SLOT_CONFIGURE);
2083*0Sstevel@tonic-gate 				if (pcihp_cpci_blue_led)
2084*0Sstevel@tonic-gate 					pcihp_hs_csr_op(pcihp_p, pci_dev,
2085*0Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_OFF);
2086*0Sstevel@tonic-gate 				/* ENUM# enabled by default for cPCI devices */
2087*0Sstevel@tonic-gate 				slotinfop->slot_flags |= PCIHP_SLOT_AUTO_CFG_EN;
2088*0Sstevel@tonic-gate 				slotinfop->slot_flags &=
2089*0Sstevel@tonic-gate 					~PCIHP_SLOT_ENUM_INS_PENDING;
2090*0Sstevel@tonic-gate 			}
2091*0Sstevel@tonic-gate 
2092*0Sstevel@tonic-gate 			/* tell HPC driver that the occupant is configured */
2093*0Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
2094*0Sstevel@tonic-gate 				HPC_CTRL_DEV_CONFIGURED, NULL);
2095*0Sstevel@tonic-gate 		} else {
2096*0Sstevel@tonic-gate 			struct pcihp_config_ctrl ctrl;
2097*0Sstevel@tonic-gate 			int circular_count;
2098*0Sstevel@tonic-gate 
2099*0Sstevel@tonic-gate 			slotinfop->ostate = AP_OSTATE_UNCONFIGURED;
2100*0Sstevel@tonic-gate 			slotinfop->rstate = AP_RSTATE_EMPTY;
2101*0Sstevel@tonic-gate 			slotinfop->condition = AP_COND_UNKNOWN;
2102*0Sstevel@tonic-gate 
2103*0Sstevel@tonic-gate 			if (!auto_enable) {	/* no further action */
2104*0Sstevel@tonic-gate 				break;
2105*0Sstevel@tonic-gate 			}
2106*0Sstevel@tonic-gate 
2107*0Sstevel@tonic-gate 			/*
2108*0Sstevel@tonic-gate 			 * We enable power to the slot and try to
2109*0Sstevel@tonic-gate 			 * configure if there is any card present.
2110*0Sstevel@tonic-gate 			 *
2111*0Sstevel@tonic-gate 			 * Note: This case is possible if the BIOS or
2112*0Sstevel@tonic-gate 			 * firmware doesn't enable the slots during
2113*0Sstevel@tonic-gate 			 * soft reboot.
2114*0Sstevel@tonic-gate 			 */
2115*0Sstevel@tonic-gate 			if (hpc_nexus_connect(slotinfop->slot_hdl,
2116*0Sstevel@tonic-gate 				NULL, 0) != HPC_SUCCESS)
2117*0Sstevel@tonic-gate 				break;
2118*0Sstevel@tonic-gate 
2119*0Sstevel@tonic-gate 			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
2120*0Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
2121*0Sstevel@tonic-gate 					HPC_EVENT_SLOT_CONFIGURE);
2122*0Sstevel@tonic-gate 				if (pcihp_cpci_blue_led)
2123*0Sstevel@tonic-gate 					pcihp_hs_csr_op(pcihp_p, pci_dev,
2124*0Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_OFF);
2125*0Sstevel@tonic-gate 				slotinfop->slot_flags |= PCIHP_SLOT_AUTO_CFG_EN;
2126*0Sstevel@tonic-gate 				slotinfop->slot_flags &=
2127*0Sstevel@tonic-gate 					~PCIHP_SLOT_ENUM_INS_PENDING;
2128*0Sstevel@tonic-gate 			}
2129*0Sstevel@tonic-gate 
2130*0Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
2131*0Sstevel@tonic-gate 				HPC_CTRL_DEV_CONFIG_START, NULL);
2132*0Sstevel@tonic-gate 
2133*0Sstevel@tonic-gate 			/*
2134*0Sstevel@tonic-gate 			 * Call the configurator to configure the card.
2135*0Sstevel@tonic-gate 			 */
2136*0Sstevel@tonic-gate 			if (pcicfg_configure(dip, pci_dev) != PCICFG_SUCCESS) {
2137*0Sstevel@tonic-gate 				if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
2138*0Sstevel@tonic-gate 					if (pcihp_cpci_blue_led)
2139*0Sstevel@tonic-gate 						pcihp_hs_csr_op(pcihp_p,
2140*0Sstevel@tonic-gate 						pci_dev,
2141*0Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_ON);
2142*0Sstevel@tonic-gate 					pcihp_hs_csr_op(pcihp_p, pci_dev,
2143*0Sstevel@tonic-gate 						HPC_EVENT_SLOT_UNCONFIGURE);
2144*0Sstevel@tonic-gate 				}
2145*0Sstevel@tonic-gate 
2146*0Sstevel@tonic-gate 				/* tell HPC driver occupant configure Error */
2147*0Sstevel@tonic-gate 				(void) hpc_nexus_control(slotinfop->slot_hdl,
2148*0Sstevel@tonic-gate 					HPC_CTRL_DEV_CONFIG_FAILURE, NULL);
2149*0Sstevel@tonic-gate 
2150*0Sstevel@tonic-gate 				/*
2151*0Sstevel@tonic-gate 				 * call HPC driver to turn off the power for
2152*0Sstevel@tonic-gate 				 * the slot.
2153*0Sstevel@tonic-gate 				 */
2154*0Sstevel@tonic-gate 				(void) hpc_nexus_disconnect(slotinfop->slot_hdl,
2155*0Sstevel@tonic-gate 							NULL, 0);
2156*0Sstevel@tonic-gate 			} else {
2157*0Sstevel@tonic-gate 			    /* record the occupant state as CONFIGURED */
2158*0Sstevel@tonic-gate 			    slotinfop->ostate = AP_OSTATE_CONFIGURED;
2159*0Sstevel@tonic-gate 			    slotinfop->rstate = AP_RSTATE_CONNECTED;
2160*0Sstevel@tonic-gate 			    slotinfop->condition = AP_COND_OK;
2161*0Sstevel@tonic-gate 
2162*0Sstevel@tonic-gate 			    /* now, online all the devices in the AP */
2163*0Sstevel@tonic-gate 			    ctrl.flags = PCIHP_CFG_CONTINUE;
2164*0Sstevel@tonic-gate 			    ctrl.rv = NDI_SUCCESS;
2165*0Sstevel@tonic-gate 			    ctrl.dip = NULL;
2166*0Sstevel@tonic-gate 			    ctrl.pci_dev = pci_dev;
2167*0Sstevel@tonic-gate 			    ctrl.op = PCIHP_ONLINE;
2168*0Sstevel@tonic-gate 				/*
2169*0Sstevel@tonic-gate 				 * the following sets slot_flags and
2170*0Sstevel@tonic-gate 				 * hs_csr_location too.
2171*0Sstevel@tonic-gate 				 */
2172*0Sstevel@tonic-gate 				(void) pcihp_get_board_type(slotinfop);
2173*0Sstevel@tonic-gate 
2174*0Sstevel@tonic-gate 			    ndi_devi_enter(dip, &circular_count);
2175*0Sstevel@tonic-gate 			    ddi_walk_devs(ddi_get_child(dip), pcihp_configure,
2176*0Sstevel@tonic-gate 				(void *)&ctrl);
2177*0Sstevel@tonic-gate 			    ndi_devi_exit(dip, circular_count);
2178*0Sstevel@tonic-gate 
2179*0Sstevel@tonic-gate 			    if (ctrl.rv != NDI_SUCCESS) {
2180*0Sstevel@tonic-gate 				/*
2181*0Sstevel@tonic-gate 				 * one or more of the devices are not
2182*0Sstevel@tonic-gate 				 * ONLINE'd. How is this to be
2183*0Sstevel@tonic-gate 				 * reported?
2184*0Sstevel@tonic-gate 				 */
2185*0Sstevel@tonic-gate 				cmn_err(CE_WARN,
2186*0Sstevel@tonic-gate 					"pcihp (%s%d): failed to attach one or"
2187*0Sstevel@tonic-gate 					" more drivers for the card in"
2188*0Sstevel@tonic-gate 					" the slot %s",
2189*0Sstevel@tonic-gate 					ddi_driver_name(dip),
2190*0Sstevel@tonic-gate 					ddi_get_instance(dip),
2191*0Sstevel@tonic-gate 					slotinfop->name);
2192*0Sstevel@tonic-gate 			    }
2193*0Sstevel@tonic-gate 
2194*0Sstevel@tonic-gate 			    /* tell HPC driver about the configured occupant */
2195*0Sstevel@tonic-gate 			    (void) hpc_nexus_control(slotinfop->slot_hdl,
2196*0Sstevel@tonic-gate 				HPC_CTRL_DEV_CONFIGURED, NULL);
2197*0Sstevel@tonic-gate 			}
2198*0Sstevel@tonic-gate 		}
2199*0Sstevel@tonic-gate 
2200*0Sstevel@tonic-gate 		break;
2201*0Sstevel@tonic-gate 
2202*0Sstevel@tonic-gate 	case HPC_SLOT_OFFLINE:
2203*0Sstevel@tonic-gate 		/*
2204*0Sstevel@tonic-gate 		 * A hot plug slot is being removed from the bus.
2205*0Sstevel@tonic-gate 		 * Make sure there is no occupant configured on the
2206*0Sstevel@tonic-gate 		 * slot before removing the AP minor node.
2207*0Sstevel@tonic-gate 		 */
2208*0Sstevel@tonic-gate 		if (slotinfop->ostate != AP_OSTATE_UNCONFIGURED) {
2209*0Sstevel@tonic-gate 		    cmn_err(CE_WARN, "pcihp (%s%d): Card is still in configured"
2210*0Sstevel@tonic-gate 			" state for pci dev %x",
2211*0Sstevel@tonic-gate 			ddi_driver_name(dip), ddi_get_instance(dip), pci_dev);
2212*0Sstevel@tonic-gate 		    rv = HPC_ERR_FAILED;
2213*0Sstevel@tonic-gate 		    break;
2214*0Sstevel@tonic-gate 		}
2215*0Sstevel@tonic-gate 
2216*0Sstevel@tonic-gate 		/*
2217*0Sstevel@tonic-gate 		 * If the AP device is in open state then return
2218*0Sstevel@tonic-gate 		 * error.
2219*0Sstevel@tonic-gate 		 */
2220*0Sstevel@tonic-gate 		if (pcihp_p->soft_state != PCIHP_SOFT_STATE_CLOSED) {
2221*0Sstevel@tonic-gate 		    rv = HPC_ERR_FAILED;
2222*0Sstevel@tonic-gate 		    break;
2223*0Sstevel@tonic-gate 		}
2224*0Sstevel@tonic-gate 		if (slot_info->slot_flags & HPC_SLOT_CREATE_DEVLINK) {
2225*0Sstevel@tonic-gate 			pci_devlink_flags &= ~(1 << pci_dev);
2226*0Sstevel@tonic-gate 			(void) ddi_prop_update_int(DDI_DEV_T_NONE,
2227*0Sstevel@tonic-gate 					dip,
2228*0Sstevel@tonic-gate 					"ap-names",
2229*0Sstevel@tonic-gate 					pci_devlink_flags);
2230*0Sstevel@tonic-gate 		}
2231*0Sstevel@tonic-gate 
2232*0Sstevel@tonic-gate 		/* remove the minor node */
2233*0Sstevel@tonic-gate 		ddi_remove_minor_node(dip, slotinfop->name);
2234*0Sstevel@tonic-gate 
2235*0Sstevel@tonic-gate 		/* free up the memory for the name string */
2236*0Sstevel@tonic-gate 		kmem_free(slotinfop->name, strlen(slotinfop->name) + 1);
2237*0Sstevel@tonic-gate 
2238*0Sstevel@tonic-gate 		/* update the slot info data */
2239*0Sstevel@tonic-gate 		slotinfop->name = NULL;
2240*0Sstevel@tonic-gate 		slotinfop->slot_hdl = NULL;
2241*0Sstevel@tonic-gate 
2242*0Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE,
2243*0Sstevel@tonic-gate 		    "pcihp (%s%d): pci slot (dev %x) OFFLINE\n",
2244*0Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip),
2245*0Sstevel@tonic-gate 		    slot_info->pci_dev_num));
2246*0Sstevel@tonic-gate 
2247*0Sstevel@tonic-gate 		break;
2248*0Sstevel@tonic-gate 	default:
2249*0Sstevel@tonic-gate 		cmn_err(CE_WARN,
2250*0Sstevel@tonic-gate 		    "pcihp_new_slot_state: unknown slot_state %d", slot_state);
2251*0Sstevel@tonic-gate 		rv = HPC_ERR_FAILED;
2252*0Sstevel@tonic-gate 	}
2253*0Sstevel@tonic-gate 
2254*0Sstevel@tonic-gate 	if (rv == 0) {
2255*0Sstevel@tonic-gate 		if (drv_getparm(TIME, (void *)&time) != DDI_SUCCESS)
2256*0Sstevel@tonic-gate 			slotinfop->last_change = (time_t)-1;
2257*0Sstevel@tonic-gate 		else
2258*0Sstevel@tonic-gate 			slotinfop->last_change = (time32_t)time;
2259*0Sstevel@tonic-gate 	}
2260*0Sstevel@tonic-gate 
2261*0Sstevel@tonic-gate 	mutex_exit(&slotinfop->slot_mutex);
2262*0Sstevel@tonic-gate 
2263*0Sstevel@tonic-gate 	(void) pcihp_get_soft_state(dip, PCIHP_DR_SLOT_EXIT, &rval);
2264*0Sstevel@tonic-gate 
2265*0Sstevel@tonic-gate 	return (rv);
2266*0Sstevel@tonic-gate }
2267*0Sstevel@tonic-gate 
2268*0Sstevel@tonic-gate /*
2269*0Sstevel@tonic-gate  * Event handler. It is assumed that this function is called from
2270*0Sstevel@tonic-gate  * a kernel context only.
2271*0Sstevel@tonic-gate  *
2272*0Sstevel@tonic-gate  * Parameters:
2273*0Sstevel@tonic-gate  *	slot_arg	AP minor number.
2274*0Sstevel@tonic-gate  *	event_mask	Event that occurred.
2275*0Sstevel@tonic-gate  */
2276*0Sstevel@tonic-gate 
2277*0Sstevel@tonic-gate static int
2278*0Sstevel@tonic-gate pcihp_event_handler(caddr_t slot_arg, uint_t event_mask)
2279*0Sstevel@tonic-gate {
2280*0Sstevel@tonic-gate 	dev_t ap_dev = (dev_t)slot_arg;
2281*0Sstevel@tonic-gate 	dev_info_t *self;
2282*0Sstevel@tonic-gate 	pcihp_t *pcihp_p;
2283*0Sstevel@tonic-gate 	int pci_dev;
2284*0Sstevel@tonic-gate 	int rv = HPC_EVENT_CLAIMED;
2285*0Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
2286*0Sstevel@tonic-gate 	struct pcihp_config_ctrl ctrl;
2287*0Sstevel@tonic-gate 	int circular_count;
2288*0Sstevel@tonic-gate 	int rval;
2289*0Sstevel@tonic-gate 
2290*0Sstevel@tonic-gate 	/*
2291*0Sstevel@tonic-gate 	 * Get the soft state structure.
2292*0Sstevel@tonic-gate 	 */
2293*0Sstevel@tonic-gate 	if (pcihp_info(NULL, DDI_INFO_DEVT2DEVINFO, (void *)ap_dev,
2294*0Sstevel@tonic-gate 	    (void **)&self) != DDI_SUCCESS)
2295*0Sstevel@tonic-gate 		return (ENXIO);
2296*0Sstevel@tonic-gate 
2297*0Sstevel@tonic-gate 	pcihp_p = pcihp_get_soft_state(self, PCIHP_DR_SLOT_ENTER, &rval);
2298*0Sstevel@tonic-gate 	ASSERT(pcihp_p != NULL);
2299*0Sstevel@tonic-gate 
2300*0Sstevel@tonic-gate 	if (rval == PCIHP_FAILURE) {
2301*0Sstevel@tonic-gate 		PCIHP_DEBUG((CE_WARN, "pcihp instance is unconfigured"
2302*0Sstevel@tonic-gate 		    " while slot activity is requested\n"));
2303*0Sstevel@tonic-gate 		return (-1);
2304*0Sstevel@tonic-gate 	}
2305*0Sstevel@tonic-gate 
2306*0Sstevel@tonic-gate 	/* get the PCI device number for the slot */
2307*0Sstevel@tonic-gate 	pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(getminor(ap_dev));
2308*0Sstevel@tonic-gate 
2309*0Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
2310*0Sstevel@tonic-gate 
2311*0Sstevel@tonic-gate 	/*
2312*0Sstevel@tonic-gate 	 * All the events that may be handled in interrupt context should be
2313*0Sstevel@tonic-gate 	 * free of any mutex usage.
2314*0Sstevel@tonic-gate 	 */
2315*0Sstevel@tonic-gate 	switch (event_mask) {
2316*0Sstevel@tonic-gate 
2317*0Sstevel@tonic-gate 	case HPC_EVENT_CLEAR_ENUM:
2318*0Sstevel@tonic-gate 		/*
2319*0Sstevel@tonic-gate 		 * Check and clear ENUM# interrupt status. This may be
2320*0Sstevel@tonic-gate 		 * called by the Hotswap controller driver when it is
2321*0Sstevel@tonic-gate 		 * operating in a full hotswap system where the
2322*0Sstevel@tonic-gate 		 * platform may not have control on globally disabling ENUM#.
2323*0Sstevel@tonic-gate 		 * In such cases, the intent is to clear interrupt and
2324*0Sstevel@tonic-gate 		 * process the interrupt in non-interrupt context.
2325*0Sstevel@tonic-gate 		 * This is the first part of the ENUM# event processing.
2326*0Sstevel@tonic-gate 		 */
2327*0Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): ENUM# is generated"
2328*0Sstevel@tonic-gate 		    " on the bus (for slot %s ?)",
2329*0Sstevel@tonic-gate 		    ddi_driver_name(pcihp_p->dip),
2330*0Sstevel@tonic-gate 		    ddi_get_instance(pcihp_p->dip), slotinfop->name));
2331*0Sstevel@tonic-gate 
2332*0Sstevel@tonic-gate 		/* this is the only event coming through in interrupt context */
2333*0Sstevel@tonic-gate 		rv = pcihp_handle_enum(pcihp_p, pci_dev, PCIHP_CLEAR_ENUM,
2334*0Sstevel@tonic-gate 		    KM_NOSLEEP);
2335*0Sstevel@tonic-gate 
2336*0Sstevel@tonic-gate 		(void) pcihp_get_soft_state(self, PCIHP_DR_SLOT_EXIT, &rval);
2337*0Sstevel@tonic-gate 
2338*0Sstevel@tonic-gate 		return (rv);
2339*0Sstevel@tonic-gate 	default:
2340*0Sstevel@tonic-gate 		break;
2341*0Sstevel@tonic-gate 	}
2342*0Sstevel@tonic-gate 
2343*0Sstevel@tonic-gate 	mutex_enter(&slotinfop->slot_mutex);
2344*0Sstevel@tonic-gate 
2345*0Sstevel@tonic-gate 	switch (event_mask) {
2346*0Sstevel@tonic-gate 
2347*0Sstevel@tonic-gate 	case HPC_EVENT_SLOT_INSERTION:
2348*0Sstevel@tonic-gate 		/*
2349*0Sstevel@tonic-gate 		 * A card is inserted in the slot. Just report this
2350*0Sstevel@tonic-gate 		 * event and return.
2351*0Sstevel@tonic-gate 		 */
2352*0Sstevel@tonic-gate 		cmn_err(CE_NOTE, "pcihp (%s%d): card is inserted"
2353*0Sstevel@tonic-gate 			" in the slot %s (pci dev %x)",
2354*0Sstevel@tonic-gate 			ddi_driver_name(pcihp_p->dip),
2355*0Sstevel@tonic-gate 			ddi_get_instance(pcihp_p->dip),
2356*0Sstevel@tonic-gate 			slotinfop->name, pci_dev);
2357*0Sstevel@tonic-gate 
2358*0Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2359*0Sstevel@tonic-gate 
2360*0Sstevel@tonic-gate 		break;
2361*0Sstevel@tonic-gate 
2362*0Sstevel@tonic-gate 	case HPC_EVENT_SLOT_CONFIGURE:
2363*0Sstevel@tonic-gate 		/*
2364*0Sstevel@tonic-gate 		 * Configure the occupant that is just inserted in the slot.
2365*0Sstevel@tonic-gate 		 * The receptacle may or may not be in the connected state. If
2366*0Sstevel@tonic-gate 		 * the receptacle is not connected and the auto configuration
2367*0Sstevel@tonic-gate 		 * is enabled on this slot then connect the slot. If auto
2368*0Sstevel@tonic-gate 		 * configuration is enabled then configure the card.
2369*0Sstevel@tonic-gate 		 */
2370*0Sstevel@tonic-gate 		if ((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) == 0) {
2371*0Sstevel@tonic-gate 			/*
2372*0Sstevel@tonic-gate 			 * auto configuration is disabled. Tell someone
2373*0Sstevel@tonic-gate 			 * like RCM about this hotplug event?
2374*0Sstevel@tonic-gate 			 */
2375*0Sstevel@tonic-gate 			cmn_err(CE_NOTE, "pcihp (%s%d): SLOT_CONFIGURE event"
2376*0Sstevel@tonic-gate 				" occurred for pci dev %x (slot %s),"
2377*0Sstevel@tonic-gate 				" Slot disabled for auto-configuration.",
2378*0Sstevel@tonic-gate 				ddi_driver_name(pcihp_p->dip),
2379*0Sstevel@tonic-gate 				ddi_get_instance(pcihp_p->dip), pci_dev,
2380*0Sstevel@tonic-gate 				slotinfop->name);
2381*0Sstevel@tonic-gate 
2382*0Sstevel@tonic-gate 			/* +++ HOOK for RCM to report this hotplug event? +++ */
2383*0Sstevel@tonic-gate 
2384*0Sstevel@tonic-gate 			break;
2385*0Sstevel@tonic-gate 		}
2386*0Sstevel@tonic-gate 
2387*0Sstevel@tonic-gate 		if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {
2388*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "pcihp (%s%d): SLOT_CONFIGURE event"
2389*0Sstevel@tonic-gate 				" re-occurred for pci dev %x (slot %s),",
2390*0Sstevel@tonic-gate 				ddi_driver_name(pcihp_p->dip),
2391*0Sstevel@tonic-gate 				ddi_get_instance(pcihp_p->dip), pci_dev,
2392*0Sstevel@tonic-gate 				slotinfop->name);
2393*0Sstevel@tonic-gate 			mutex_exit(&slotinfop->slot_mutex);
2394*0Sstevel@tonic-gate 
2395*0Sstevel@tonic-gate 			(void) pcihp_get_soft_state(self, PCIHP_DR_SLOT_EXIT,
2396*0Sstevel@tonic-gate 			    &rval);
2397*0Sstevel@tonic-gate 
2398*0Sstevel@tonic-gate 			return (EAGAIN);
2399*0Sstevel@tonic-gate 		}
2400*0Sstevel@tonic-gate 
2401*0Sstevel@tonic-gate 		/*
2402*0Sstevel@tonic-gate 		 * Auto configuration is enabled. First, make sure the
2403*0Sstevel@tonic-gate 		 * receptacle is in the CONNECTED state.
2404*0Sstevel@tonic-gate 		 */
2405*0Sstevel@tonic-gate 		if ((rv = hpc_nexus_connect(slotinfop->slot_hdl,
2406*0Sstevel@tonic-gate 		    NULL, 0)) == HPC_SUCCESS) {
2407*0Sstevel@tonic-gate 		    slotinfop->rstate = AP_RSTATE_CONNECTED; /* record rstate */
2408*0Sstevel@tonic-gate 		}
2409*0Sstevel@tonic-gate 
2410*0Sstevel@tonic-gate 		/* Clear INS and Turn LED Off and start configuring. */
2411*0Sstevel@tonic-gate 		if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
2412*0Sstevel@tonic-gate 			pcihp_hs_csr_op(pcihp_p, pci_dev,
2413*0Sstevel@tonic-gate 				HPC_EVENT_SLOT_CONFIGURE);
2414*0Sstevel@tonic-gate 			if (pcihp_cpci_blue_led)
2415*0Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
2416*0Sstevel@tonic-gate 					HPC_EVENT_SLOT_BLUE_LED_OFF);
2417*0Sstevel@tonic-gate 		}
2418*0Sstevel@tonic-gate 
2419*0Sstevel@tonic-gate 		(void) hpc_nexus_control(slotinfop->slot_hdl,
2420*0Sstevel@tonic-gate 			HPC_CTRL_DEV_CONFIG_START, NULL);
2421*0Sstevel@tonic-gate 
2422*0Sstevel@tonic-gate 		/*
2423*0Sstevel@tonic-gate 		 * Call the configurator to configure the card.
2424*0Sstevel@tonic-gate 		 */
2425*0Sstevel@tonic-gate 		if (pcicfg_configure(pcihp_p->dip, pci_dev) != PCICFG_SUCCESS) {
2426*0Sstevel@tonic-gate 			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
2427*0Sstevel@tonic-gate 				if (pcihp_cpci_blue_led)
2428*0Sstevel@tonic-gate 					pcihp_hs_csr_op(pcihp_p, pci_dev,
2429*0Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_ON);
2430*0Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
2431*0Sstevel@tonic-gate 					HPC_EVENT_SLOT_UNCONFIGURE);
2432*0Sstevel@tonic-gate 			}
2433*0Sstevel@tonic-gate 			/* failed to configure the card */
2434*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "pcihp (%s%d): failed to configure"
2435*0Sstevel@tonic-gate 				" the card in the slot %s",
2436*0Sstevel@tonic-gate 				ddi_driver_name(pcihp_p->dip),
2437*0Sstevel@tonic-gate 				ddi_get_instance(pcihp_p->dip),
2438*0Sstevel@tonic-gate 				slotinfop->name);
2439*0Sstevel@tonic-gate 			/* failed to configure; disconnect the slot */
2440*0Sstevel@tonic-gate 			if (hpc_nexus_disconnect(slotinfop->slot_hdl,
2441*0Sstevel@tonic-gate 			    NULL, 0) == HPC_SUCCESS) {
2442*0Sstevel@tonic-gate 			    slotinfop->rstate = AP_RSTATE_DISCONNECTED;
2443*0Sstevel@tonic-gate 			}
2444*0Sstevel@tonic-gate 
2445*0Sstevel@tonic-gate 			/* tell HPC driver occupant configure Error */
2446*0Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
2447*0Sstevel@tonic-gate 				HPC_CTRL_DEV_CONFIG_FAILURE, NULL);
2448*0Sstevel@tonic-gate 		} else {
2449*0Sstevel@tonic-gate 			/* record the occupant state as CONFIGURED */
2450*0Sstevel@tonic-gate 			slotinfop->ostate = AP_OSTATE_CONFIGURED;
2451*0Sstevel@tonic-gate 			slotinfop->condition = AP_COND_OK;
2452*0Sstevel@tonic-gate 
2453*0Sstevel@tonic-gate 			/* now, online all the devices in the AP */
2454*0Sstevel@tonic-gate 			ctrl.flags = PCIHP_CFG_CONTINUE;
2455*0Sstevel@tonic-gate 			ctrl.rv = NDI_SUCCESS;
2456*0Sstevel@tonic-gate 			ctrl.dip = NULL;
2457*0Sstevel@tonic-gate 			ctrl.pci_dev = pci_dev;
2458*0Sstevel@tonic-gate 			ctrl.op = PCIHP_ONLINE;
2459*0Sstevel@tonic-gate 				(void) pcihp_get_board_type(slotinfop);
2460*0Sstevel@tonic-gate 
2461*0Sstevel@tonic-gate 			ndi_devi_enter(pcihp_p->dip, &circular_count);
2462*0Sstevel@tonic-gate 			ddi_walk_devs(ddi_get_child(pcihp_p->dip),
2463*0Sstevel@tonic-gate 				pcihp_configure, (void *)&ctrl);
2464*0Sstevel@tonic-gate 			ndi_devi_exit(pcihp_p->dip, circular_count);
2465*0Sstevel@tonic-gate 
2466*0Sstevel@tonic-gate 			if (ctrl.rv != NDI_SUCCESS) {
2467*0Sstevel@tonic-gate 				/*
2468*0Sstevel@tonic-gate 				 * one or more of the devices are not
2469*0Sstevel@tonic-gate 				 * ONLINE'd. How is this to be
2470*0Sstevel@tonic-gate 				 * reported?
2471*0Sstevel@tonic-gate 				 */
2472*0Sstevel@tonic-gate 				cmn_err(CE_WARN,
2473*0Sstevel@tonic-gate 					"pcihp (%s%d): failed to attach one or"
2474*0Sstevel@tonic-gate 					" more drivers for the card in"
2475*0Sstevel@tonic-gate 					" the slot %s",
2476*0Sstevel@tonic-gate 					ddi_driver_name(pcihp_p->dip),
2477*0Sstevel@tonic-gate 					ddi_get_instance(pcihp_p->dip),
2478*0Sstevel@tonic-gate 					slotinfop->name);
2479*0Sstevel@tonic-gate 			}
2480*0Sstevel@tonic-gate 
2481*0Sstevel@tonic-gate 			/* tell HPC driver that the occupant is configured */
2482*0Sstevel@tonic-gate 			(void) hpc_nexus_control(slotinfop->slot_hdl,
2483*0Sstevel@tonic-gate 				HPC_CTRL_DEV_CONFIGURED, NULL);
2484*0Sstevel@tonic-gate 
2485*0Sstevel@tonic-gate 			cmn_err(CE_NOTE, "pcihp (%s%d): card is CONFIGURED"
2486*0Sstevel@tonic-gate 				" in the slot %s (pci dev %x)",
2487*0Sstevel@tonic-gate 				ddi_driver_name(pcihp_p->dip),
2488*0Sstevel@tonic-gate 				ddi_get_instance(pcihp_p->dip),
2489*0Sstevel@tonic-gate 				slotinfop->name, pci_dev);
2490*0Sstevel@tonic-gate 		}
2491*0Sstevel@tonic-gate 
2492*0Sstevel@tonic-gate 		break;
2493*0Sstevel@tonic-gate 
2494*0Sstevel@tonic-gate 	case HPC_EVENT_SLOT_UNCONFIGURE:
2495*0Sstevel@tonic-gate 		/*
2496*0Sstevel@tonic-gate 		 * Unconfigure the occupant in this slot.
2497*0Sstevel@tonic-gate 		 */
2498*0Sstevel@tonic-gate 		if ((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) == 0) {
2499*0Sstevel@tonic-gate 			/*
2500*0Sstevel@tonic-gate 			 * auto configuration is disabled. Tell someone
2501*0Sstevel@tonic-gate 			 * like RCM about this hotplug event?
2502*0Sstevel@tonic-gate 			 */
2503*0Sstevel@tonic-gate 			cmn_err(CE_NOTE, "pcihp (%s%d): SLOT_UNCONFIGURE event"
2504*0Sstevel@tonic-gate 				" for pci dev %x (slot %s) ignored,"
2505*0Sstevel@tonic-gate 				" Slot disabled for auto-configuration.",
2506*0Sstevel@tonic-gate 				ddi_driver_name(pcihp_p->dip),
2507*0Sstevel@tonic-gate 				ddi_get_instance(pcihp_p->dip), pci_dev,
2508*0Sstevel@tonic-gate 				slotinfop->name);
2509*0Sstevel@tonic-gate 
2510*0Sstevel@tonic-gate 			/* +++ HOOK for RCM to report this hotplug event? +++ */
2511*0Sstevel@tonic-gate 
2512*0Sstevel@tonic-gate 			break;
2513*0Sstevel@tonic-gate 		}
2514*0Sstevel@tonic-gate 
2515*0Sstevel@tonic-gate 		if (slotinfop->ostate == AP_OSTATE_UNCONFIGURED) {
2516*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "pcihp (%s%d): SLOT_UNCONFIGURE "
2517*0Sstevel@tonic-gate 				"event re-occurred for pci dev %x (slot %s),",
2518*0Sstevel@tonic-gate 				ddi_driver_name(pcihp_p->dip),
2519*0Sstevel@tonic-gate 				ddi_get_instance(pcihp_p->dip), pci_dev,
2520*0Sstevel@tonic-gate 				slotinfop->name);
2521*0Sstevel@tonic-gate 			mutex_exit(&slotinfop->slot_mutex);
2522*0Sstevel@tonic-gate 
2523*0Sstevel@tonic-gate 			(void) pcihp_get_soft_state(self, PCIHP_DR_SLOT_EXIT,
2524*0Sstevel@tonic-gate 			    &rval);
2525*0Sstevel@tonic-gate 
2526*0Sstevel@tonic-gate 			return (EAGAIN);
2527*0Sstevel@tonic-gate 		}
2528*0Sstevel@tonic-gate 		/*
2529*0Sstevel@tonic-gate 		 * If the occupant is in the CONFIGURED state then
2530*0Sstevel@tonic-gate 		 * call the configurator to unconfigure the slot.
2531*0Sstevel@tonic-gate 		 */
2532*0Sstevel@tonic-gate 		if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {
2533*0Sstevel@tonic-gate 			/*
2534*0Sstevel@tonic-gate 			 * Detach all the drivers for the devices in the
2535*0Sstevel@tonic-gate 			 * slot. Call pcihp_configure() to offline the
2536*0Sstevel@tonic-gate 			 * devices.
2537*0Sstevel@tonic-gate 			 */
2538*0Sstevel@tonic-gate 			ctrl.flags = 0;
2539*0Sstevel@tonic-gate 			ctrl.rv = NDI_SUCCESS;
2540*0Sstevel@tonic-gate 			ctrl.dip = NULL;
2541*0Sstevel@tonic-gate 			ctrl.pci_dev = pci_dev;
2542*0Sstevel@tonic-gate 			ctrl.op = PCIHP_OFFLINE;
2543*0Sstevel@tonic-gate 
2544*0Sstevel@tonic-gate 			(void) devfs_clean(pcihp_p->dip, NULL, DV_CLEAN_FORCE);
2545*0Sstevel@tonic-gate 			ndi_devi_enter(pcihp_p->dip, &circular_count);
2546*0Sstevel@tonic-gate 			ddi_walk_devs(ddi_get_child(pcihp_p->dip),
2547*0Sstevel@tonic-gate 				pcihp_configure, (void *)&ctrl);
2548*0Sstevel@tonic-gate 			ndi_devi_exit(pcihp_p->dip, circular_count);
2549*0Sstevel@tonic-gate 
2550*0Sstevel@tonic-gate 			if (ctrl.rv != NDI_SUCCESS) {
2551*0Sstevel@tonic-gate 				/*
2552*0Sstevel@tonic-gate 				 * Failed to detach one or more drivers.
2553*0Sstevel@tonic-gate 				 * Restore the status for the drivers
2554*0Sstevel@tonic-gate 				 * which are offlined during this step.
2555*0Sstevel@tonic-gate 				 */
2556*0Sstevel@tonic-gate 				ctrl.flags = PCIHP_CFG_CONTINUE;
2557*0Sstevel@tonic-gate 				ctrl.rv = NDI_SUCCESS;
2558*0Sstevel@tonic-gate 				ctrl.dip = NULL;
2559*0Sstevel@tonic-gate 				ctrl.pci_dev = pci_dev;
2560*0Sstevel@tonic-gate 				ctrl.op = PCIHP_ONLINE;
2561*0Sstevel@tonic-gate 
2562*0Sstevel@tonic-gate 				ndi_devi_enter(pcihp_p->dip, &circular_count);
2563*0Sstevel@tonic-gate 				ddi_walk_devs(ddi_get_child(pcihp_p->dip),
2564*0Sstevel@tonic-gate 					pcihp_configure, (void *)&ctrl);
2565*0Sstevel@tonic-gate 				ndi_devi_exit(pcihp_p->dip, circular_count);
2566*0Sstevel@tonic-gate 				rv = HPC_ERR_FAILED;
2567*0Sstevel@tonic-gate 			} else {
2568*0Sstevel@tonic-gate 				(void) hpc_nexus_control(slotinfop->slot_hdl,
2569*0Sstevel@tonic-gate 					HPC_CTRL_DEV_UNCONFIG_START, NULL);
2570*0Sstevel@tonic-gate 
2571*0Sstevel@tonic-gate 				if (pcicfg_unconfigure(pcihp_p->dip,
2572*0Sstevel@tonic-gate 						pci_dev) == PCICFG_SUCCESS) {
2573*0Sstevel@tonic-gate 
2574*0Sstevel@tonic-gate 				/* Resources freed. Turn LED on. Clear EXT. */
2575*0Sstevel@tonic-gate 				if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
2576*0Sstevel@tonic-gate 					if (pcihp_cpci_blue_led)
2577*0Sstevel@tonic-gate 						pcihp_hs_csr_op(pcihp_p,
2578*0Sstevel@tonic-gate 						pci_dev,
2579*0Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_ON);
2580*0Sstevel@tonic-gate 					pcihp_hs_csr_op(pcihp_p, pci_dev,
2581*0Sstevel@tonic-gate 						HPC_EVENT_SLOT_UNCONFIGURE);
2582*0Sstevel@tonic-gate 					slotinfop->hs_csr_location = 0;
2583*0Sstevel@tonic-gate 					slotinfop->slot_flags &=
2584*0Sstevel@tonic-gate 						~PCIHP_SLOT_DEV_NON_HOTPLUG;
2585*0Sstevel@tonic-gate 				}
2586*0Sstevel@tonic-gate 					slotinfop->ostate =
2587*0Sstevel@tonic-gate 						AP_OSTATE_UNCONFIGURED;
2588*0Sstevel@tonic-gate 					slotinfop->condition = AP_COND_UNKNOWN;
2589*0Sstevel@tonic-gate 					/*
2590*0Sstevel@tonic-gate 					 * send the notification of state change
2591*0Sstevel@tonic-gate 					 * to the HPC driver.
2592*0Sstevel@tonic-gate 					 */
2593*0Sstevel@tonic-gate 					(void) hpc_nexus_control(
2594*0Sstevel@tonic-gate 						slotinfop->slot_hdl,
2595*0Sstevel@tonic-gate 						HPC_CTRL_DEV_UNCONFIGURED,
2596*0Sstevel@tonic-gate 						NULL);
2597*0Sstevel@tonic-gate 					/* disconnect the slot */
2598*0Sstevel@tonic-gate 					if (hpc_nexus_disconnect(
2599*0Sstevel@tonic-gate 						slotinfop->slot_hdl,
2600*0Sstevel@tonic-gate 						NULL, 0) == HPC_SUCCESS) {
2601*0Sstevel@tonic-gate 							slotinfop->rstate =
2602*0Sstevel@tonic-gate 							AP_RSTATE_DISCONNECTED;
2603*0Sstevel@tonic-gate 					}
2604*0Sstevel@tonic-gate 
2605*0Sstevel@tonic-gate 					cmn_err(CE_NOTE,
2606*0Sstevel@tonic-gate 					"pcihp (%s%d): card is UNCONFIGURED"
2607*0Sstevel@tonic-gate 						" in the slot %s (pci dev %x)",
2608*0Sstevel@tonic-gate 						ddi_driver_name(pcihp_p->dip),
2609*0Sstevel@tonic-gate 						ddi_get_instance(pcihp_p->dip),
2610*0Sstevel@tonic-gate 						slotinfop->name, pci_dev);
2611*0Sstevel@tonic-gate 				} else {
2612*0Sstevel@tonic-gate 					/* tell HPC driver occupant is Busy */
2613*0Sstevel@tonic-gate 					(void) hpc_nexus_control(
2614*0Sstevel@tonic-gate 						slotinfop->slot_hdl,
2615*0Sstevel@tonic-gate 						HPC_CTRL_DEV_UNCONFIG_FAILURE,
2616*0Sstevel@tonic-gate 						NULL);
2617*0Sstevel@tonic-gate 
2618*0Sstevel@tonic-gate 					rv = HPC_ERR_FAILED;
2619*0Sstevel@tonic-gate 				}
2620*0Sstevel@tonic-gate 			}
2621*0Sstevel@tonic-gate 		}
2622*0Sstevel@tonic-gate 
2623*0Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2624*0Sstevel@tonic-gate 
2625*0Sstevel@tonic-gate 		break;
2626*0Sstevel@tonic-gate 
2627*0Sstevel@tonic-gate 	case HPC_EVENT_SLOT_REMOVAL:
2628*0Sstevel@tonic-gate 		/*
2629*0Sstevel@tonic-gate 		 * Card is removed from the slot. The card must have been
2630*0Sstevel@tonic-gate 		 * unconfigured before this event.
2631*0Sstevel@tonic-gate 		 */
2632*0Sstevel@tonic-gate 		if (slotinfop->ostate != AP_OSTATE_UNCONFIGURED) {
2633*0Sstevel@tonic-gate 			panic("pcihp (%s%d): card is removed from"
2634*0Sstevel@tonic-gate 			    " the slot %s before doing unconfigure!!",
2635*0Sstevel@tonic-gate 			    ddi_driver_name(pcihp_p->dip),
2636*0Sstevel@tonic-gate 			    ddi_get_instance(pcihp_p->dip),
2637*0Sstevel@tonic-gate 			    slotinfop->name);
2638*0Sstevel@tonic-gate 			/*NOTREACHED*/
2639*0Sstevel@tonic-gate 		}
2640*0Sstevel@tonic-gate 
2641*0Sstevel@tonic-gate 		cmn_err(CE_NOTE, "pcihp (%s%d): card is removed"
2642*0Sstevel@tonic-gate 		    " from the slot %s",
2643*0Sstevel@tonic-gate 		    ddi_driver_name(pcihp_p->dip),
2644*0Sstevel@tonic-gate 		    ddi_get_instance(pcihp_p->dip),
2645*0Sstevel@tonic-gate 		    slotinfop->name);
2646*0Sstevel@tonic-gate 
2647*0Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2648*0Sstevel@tonic-gate 
2649*0Sstevel@tonic-gate 		break;
2650*0Sstevel@tonic-gate 
2651*0Sstevel@tonic-gate 	case HPC_EVENT_SLOT_POWER_ON:
2652*0Sstevel@tonic-gate 		/*
2653*0Sstevel@tonic-gate 		 * Slot is connected to the bus. i.e the card is powered
2654*0Sstevel@tonic-gate 		 * on. Are there any error conditions to be checked?
2655*0Sstevel@tonic-gate 		 */
2656*0Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): card is powered"
2657*0Sstevel@tonic-gate 			" on in the slot %s",
2658*0Sstevel@tonic-gate 			ddi_driver_name(pcihp_p->dip),
2659*0Sstevel@tonic-gate 			ddi_get_instance(pcihp_p->dip),
2660*0Sstevel@tonic-gate 			slotinfop->name));
2661*0Sstevel@tonic-gate 
2662*0Sstevel@tonic-gate 		slotinfop->rstate = AP_RSTATE_CONNECTED; /* record rstate */
2663*0Sstevel@tonic-gate 
2664*0Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2665*0Sstevel@tonic-gate 
2666*0Sstevel@tonic-gate 		break;
2667*0Sstevel@tonic-gate 
2668*0Sstevel@tonic-gate 	case HPC_EVENT_SLOT_POWER_OFF:
2669*0Sstevel@tonic-gate 		/*
2670*0Sstevel@tonic-gate 		 * Slot is disconnected from the bus. i.e the card is powered
2671*0Sstevel@tonic-gate 		 * off. Are there any error conditions to be checked?
2672*0Sstevel@tonic-gate 		 */
2673*0Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): card is powered"
2674*0Sstevel@tonic-gate 			" off in the slot %s",
2675*0Sstevel@tonic-gate 			ddi_driver_name(pcihp_p->dip),
2676*0Sstevel@tonic-gate 			ddi_get_instance(pcihp_p->dip),
2677*0Sstevel@tonic-gate 			slotinfop->name));
2678*0Sstevel@tonic-gate 
2679*0Sstevel@tonic-gate 		slotinfop->rstate = AP_RSTATE_DISCONNECTED; /* record rstate */
2680*0Sstevel@tonic-gate 
2681*0Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2682*0Sstevel@tonic-gate 
2683*0Sstevel@tonic-gate 		break;
2684*0Sstevel@tonic-gate 
2685*0Sstevel@tonic-gate 	case HPC_EVENT_SLOT_LATCH_SHUT:
2686*0Sstevel@tonic-gate 		/*
2687*0Sstevel@tonic-gate 		 * Latch on the slot is closed.
2688*0Sstevel@tonic-gate 		 */
2689*0Sstevel@tonic-gate 		cmn_err(CE_NOTE, "pcihp (%s%d): latch is shut"
2690*0Sstevel@tonic-gate 			" for the slot %s",
2691*0Sstevel@tonic-gate 			ddi_driver_name(pcihp_p->dip),
2692*0Sstevel@tonic-gate 			ddi_get_instance(pcihp_p->dip),
2693*0Sstevel@tonic-gate 			slotinfop->name);
2694*0Sstevel@tonic-gate 
2695*0Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2696*0Sstevel@tonic-gate 
2697*0Sstevel@tonic-gate 		break;
2698*0Sstevel@tonic-gate 
2699*0Sstevel@tonic-gate 	case HPC_EVENT_SLOT_LATCH_OPEN:
2700*0Sstevel@tonic-gate 		/*
2701*0Sstevel@tonic-gate 		 * Latch on the slot is open.
2702*0Sstevel@tonic-gate 		 */
2703*0Sstevel@tonic-gate 		cmn_err(CE_NOTE, "pcihp (%s%d): latch is open"
2704*0Sstevel@tonic-gate 			" for the slot %s",
2705*0Sstevel@tonic-gate 			ddi_driver_name(pcihp_p->dip),
2706*0Sstevel@tonic-gate 			ddi_get_instance(pcihp_p->dip),
2707*0Sstevel@tonic-gate 			slotinfop->name);
2708*0Sstevel@tonic-gate 
2709*0Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2710*0Sstevel@tonic-gate 
2711*0Sstevel@tonic-gate 		break;
2712*0Sstevel@tonic-gate 
2713*0Sstevel@tonic-gate 	case HPC_EVENT_PROCESS_ENUM:
2714*0Sstevel@tonic-gate 		/*
2715*0Sstevel@tonic-gate 		 * HSC knows the device number of the slot where the
2716*0Sstevel@tonic-gate 		 * ENUM# was triggered.
2717*0Sstevel@tonic-gate 		 * Now finish the necessary actions to be taken on that
2718*0Sstevel@tonic-gate 		 * slot. Please note that the interrupt is already cleared.
2719*0Sstevel@tonic-gate 		 * This is the second(last) part of the ENUM# event processing.
2720*0Sstevel@tonic-gate 		 */
2721*0Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): processing ENUM#"
2722*0Sstevel@tonic-gate 			" for slot %s",
2723*0Sstevel@tonic-gate 			ddi_driver_name(pcihp_p->dip),
2724*0Sstevel@tonic-gate 			ddi_get_instance(pcihp_p->dip),
2725*0Sstevel@tonic-gate 			slotinfop->name));
2726*0Sstevel@tonic-gate 
2727*0Sstevel@tonic-gate 		mutex_exit(&slotinfop->slot_mutex);
2728*0Sstevel@tonic-gate 		rv = pcihp_enum_slot(pcihp_p, slotinfop, pci_dev,
2729*0Sstevel@tonic-gate 			PCIHP_HANDLE_ENUM, KM_SLEEP);
2730*0Sstevel@tonic-gate 		mutex_enter(&slotinfop->slot_mutex);
2731*0Sstevel@tonic-gate 
2732*0Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2733*0Sstevel@tonic-gate 
2734*0Sstevel@tonic-gate 		break;
2735*0Sstevel@tonic-gate 
2736*0Sstevel@tonic-gate 	case HPC_EVENT_BUS_ENUM:
2737*0Sstevel@tonic-gate 		/*
2738*0Sstevel@tonic-gate 		 * Same as HPC_EVENT_SLOT_ENUM as defined the PSARC doc.
2739*0Sstevel@tonic-gate 		 * This term is used for better clarity of its usage.
2740*0Sstevel@tonic-gate 		 *
2741*0Sstevel@tonic-gate 		 * ENUM signal occurred on the bus. It may be from this
2742*0Sstevel@tonic-gate 		 * slot or any other hotplug slot on the bus.
2743*0Sstevel@tonic-gate 		 *
2744*0Sstevel@tonic-gate 		 * It is NOT recommended that the hotswap controller uses
2745*0Sstevel@tonic-gate 		 * event without queuing as NDI and other DDI calls may not
2746*0Sstevel@tonic-gate 		 * necessarily be invokable in interrupt context.
2747*0Sstevel@tonic-gate 		 * Hence the hotswap controller driver should use the
2748*0Sstevel@tonic-gate 		 * CLEAR_ENUM event which returns the slot device number
2749*0Sstevel@tonic-gate 		 * and then call HPC_EVENT_PROCESS_ENUM event with queuing.
2750*0Sstevel@tonic-gate 		 *
2751*0Sstevel@tonic-gate 		 * This can be used when the hotswap controller is
2752*0Sstevel@tonic-gate 		 * implementing a polled event mechanism to do the
2753*0Sstevel@tonic-gate 		 * necessary actions in a single call.
2754*0Sstevel@tonic-gate 		 */
2755*0Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): ENUM# is generated"
2756*0Sstevel@tonic-gate 		    " on the bus (for slot %s ?)",
2757*0Sstevel@tonic-gate 		    ddi_driver_name(pcihp_p->dip),
2758*0Sstevel@tonic-gate 		    ddi_get_instance(pcihp_p->dip),
2759*0Sstevel@tonic-gate 		    slotinfop->name));
2760*0Sstevel@tonic-gate 
2761*0Sstevel@tonic-gate 		mutex_exit(&slotinfop->slot_mutex);
2762*0Sstevel@tonic-gate 		rv = pcihp_handle_enum(pcihp_p, pci_dev, PCIHP_HANDLE_ENUM,
2763*0Sstevel@tonic-gate 			KM_SLEEP);
2764*0Sstevel@tonic-gate 		mutex_enter(&slotinfop->slot_mutex);
2765*0Sstevel@tonic-gate 
2766*0Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2767*0Sstevel@tonic-gate 
2768*0Sstevel@tonic-gate 		break;
2769*0Sstevel@tonic-gate 
2770*0Sstevel@tonic-gate 	case HPC_EVENT_SLOT_BLUE_LED_ON:
2771*0Sstevel@tonic-gate 
2772*0Sstevel@tonic-gate 		/*
2773*0Sstevel@tonic-gate 		 * Request to turn Hot Swap Blue LED on.
2774*0Sstevel@tonic-gate 		 */
2775*0Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): Request To Turn On Blue "
2776*0Sstevel@tonic-gate 		    "LED on the bus (for slot %s ?)",
2777*0Sstevel@tonic-gate 		    ddi_driver_name(pcihp_p->dip),
2778*0Sstevel@tonic-gate 		    ddi_get_instance(pcihp_p->dip),
2779*0Sstevel@tonic-gate 		    slotinfop->name));
2780*0Sstevel@tonic-gate 
2781*0Sstevel@tonic-gate 		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_SLOT_BLUE_LED_ON);
2782*0Sstevel@tonic-gate 		break;
2783*0Sstevel@tonic-gate 
2784*0Sstevel@tonic-gate 	case HPC_EVENT_DISABLE_ENUM:
2785*0Sstevel@tonic-gate 		/*
2786*0Sstevel@tonic-gate 		 * Disable ENUM# which disables auto configuration on this slot
2787*0Sstevel@tonic-gate 		 */
2788*0Sstevel@tonic-gate 		if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
2789*0Sstevel@tonic-gate 			pcihp_hs_csr_op(pcihp_p, pci_dev,
2790*0Sstevel@tonic-gate 				HPC_EVENT_DISABLE_ENUM);
2791*0Sstevel@tonic-gate 			slotinfop->slot_flags &= ~PCIHP_SLOT_AUTO_CFG_EN;
2792*0Sstevel@tonic-gate 		}
2793*0Sstevel@tonic-gate 		break;
2794*0Sstevel@tonic-gate 
2795*0Sstevel@tonic-gate 	case HPC_EVENT_ENABLE_ENUM:
2796*0Sstevel@tonic-gate 		/*
2797*0Sstevel@tonic-gate 		 * Enable ENUM# which enables auto configuration on this slot.
2798*0Sstevel@tonic-gate 		 */
2799*0Sstevel@tonic-gate 		if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
2800*0Sstevel@tonic-gate 			pcihp_hs_csr_op(pcihp_p, pci_dev,
2801*0Sstevel@tonic-gate 				HPC_EVENT_ENABLE_ENUM);
2802*0Sstevel@tonic-gate 			slotinfop->slot_flags |= PCIHP_SLOT_AUTO_CFG_EN;
2803*0Sstevel@tonic-gate 		}
2804*0Sstevel@tonic-gate 		break;
2805*0Sstevel@tonic-gate 
2806*0Sstevel@tonic-gate 	case HPC_EVENT_SLOT_BLUE_LED_OFF:
2807*0Sstevel@tonic-gate 
2808*0Sstevel@tonic-gate 		/*
2809*0Sstevel@tonic-gate 		 * Request to turn Hot Swap Blue LED off.
2810*0Sstevel@tonic-gate 		 */
2811*0Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): Request To Turn Off Blue "
2812*0Sstevel@tonic-gate 		    "LED on the bus (for slot %s ?)",
2813*0Sstevel@tonic-gate 		    ddi_driver_name(pcihp_p->dip),
2814*0Sstevel@tonic-gate 		    ddi_get_instance(pcihp_p->dip),
2815*0Sstevel@tonic-gate 		    slotinfop->name));
2816*0Sstevel@tonic-gate 
2817*0Sstevel@tonic-gate 		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_SLOT_BLUE_LED_OFF);
2818*0Sstevel@tonic-gate 
2819*0Sstevel@tonic-gate 		break;
2820*0Sstevel@tonic-gate 
2821*0Sstevel@tonic-gate 	case HPC_EVENT_SLOT_NOT_HEALTHY:
2822*0Sstevel@tonic-gate 		/*
2823*0Sstevel@tonic-gate 		 * HEALTHY# signal on this slot is not OK.
2824*0Sstevel@tonic-gate 		 */
2825*0Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): HEALTHY# signal is not OK"
2826*0Sstevel@tonic-gate 			" for this slot %s",
2827*0Sstevel@tonic-gate 			ddi_driver_name(pcihp_p->dip),
2828*0Sstevel@tonic-gate 			ddi_get_instance(pcihp_p->dip),
2829*0Sstevel@tonic-gate 			slotinfop->name));
2830*0Sstevel@tonic-gate 
2831*0Sstevel@tonic-gate 		/* record the state in slot_flags field */
2832*0Sstevel@tonic-gate 		slotinfop->slot_flags |= PCIHP_SLOT_NOT_HEALTHY;
2833*0Sstevel@tonic-gate 		slotinfop->condition = AP_COND_FAILED;
2834*0Sstevel@tonic-gate 
2835*0Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2836*0Sstevel@tonic-gate 
2837*0Sstevel@tonic-gate 		break;
2838*0Sstevel@tonic-gate 
2839*0Sstevel@tonic-gate 	case HPC_EVENT_SLOT_HEALTHY_OK:
2840*0Sstevel@tonic-gate 		/*
2841*0Sstevel@tonic-gate 		 * HEALTHY# signal on this slot is OK now.
2842*0Sstevel@tonic-gate 		 */
2843*0Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): HEALTHY# signal is OK now"
2844*0Sstevel@tonic-gate 			" for this slot %s",
2845*0Sstevel@tonic-gate 			ddi_driver_name(pcihp_p->dip),
2846*0Sstevel@tonic-gate 			ddi_get_instance(pcihp_p->dip),
2847*0Sstevel@tonic-gate 			slotinfop->name));
2848*0Sstevel@tonic-gate 
2849*0Sstevel@tonic-gate 		/* update the state in slot_flags field */
2850*0Sstevel@tonic-gate 		slotinfop->slot_flags &= ~PCIHP_SLOT_NOT_HEALTHY;
2851*0Sstevel@tonic-gate 		slotinfop->condition = AP_COND_OK;
2852*0Sstevel@tonic-gate 
2853*0Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2854*0Sstevel@tonic-gate 
2855*0Sstevel@tonic-gate 		break;
2856*0Sstevel@tonic-gate 
2857*0Sstevel@tonic-gate 	default:
2858*0Sstevel@tonic-gate 		cmn_err(CE_NOTE, "pcihp (%s%d): unknown event %x"
2859*0Sstevel@tonic-gate 			" for this slot %s",
2860*0Sstevel@tonic-gate 			ddi_driver_name(pcihp_p->dip),
2861*0Sstevel@tonic-gate 			ddi_get_instance(pcihp_p->dip), event_mask,
2862*0Sstevel@tonic-gate 			slotinfop->name);
2863*0Sstevel@tonic-gate 
2864*0Sstevel@tonic-gate 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2865*0Sstevel@tonic-gate 
2866*0Sstevel@tonic-gate 		break;
2867*0Sstevel@tonic-gate 	}
2868*0Sstevel@tonic-gate 
2869*0Sstevel@tonic-gate 	mutex_exit(&slotinfop->slot_mutex);
2870*0Sstevel@tonic-gate 
2871*0Sstevel@tonic-gate 	(void) pcihp_get_soft_state(self, PCIHP_DR_SLOT_EXIT, &rval);
2872*0Sstevel@tonic-gate 
2873*0Sstevel@tonic-gate 	return (rv);
2874*0Sstevel@tonic-gate }
2875*0Sstevel@tonic-gate 
2876*0Sstevel@tonic-gate /*
2877*0Sstevel@tonic-gate  * This function is called to online or offline the devices for an
2878*0Sstevel@tonic-gate  * attachment point. If the PCI device number of the node matches
2879*0Sstevel@tonic-gate  * with the device number of the specified hot plug slot then
2880*0Sstevel@tonic-gate  * the operation is performed.
2881*0Sstevel@tonic-gate  */
2882*0Sstevel@tonic-gate static int
2883*0Sstevel@tonic-gate pcihp_configure(dev_info_t *dip, void *hdl)
2884*0Sstevel@tonic-gate {
2885*0Sstevel@tonic-gate 	int pci_dev;
2886*0Sstevel@tonic-gate 	struct pcihp_config_ctrl *ctrl = (struct pcihp_config_ctrl *)hdl;
2887*0Sstevel@tonic-gate 	int rv;
2888*0Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
2889*0Sstevel@tonic-gate 	int length;
2890*0Sstevel@tonic-gate 
2891*0Sstevel@tonic-gate 	/*
2892*0Sstevel@tonic-gate 	 * Get the PCI device number information from the devinfo
2893*0Sstevel@tonic-gate 	 * node. Since the node may not have the address field
2894*0Sstevel@tonic-gate 	 * setup (this is done in the DDI_INITCHILD of the parent)
2895*0Sstevel@tonic-gate 	 * we look up the 'reg' property to decode that information.
2896*0Sstevel@tonic-gate 	 */
2897*0Sstevel@tonic-gate 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
2898*0Sstevel@tonic-gate 		DDI_PROP_DONTPASS, "reg", (int **)&pci_rp,
2899*0Sstevel@tonic-gate 		(uint_t *)&length) != DDI_PROP_SUCCESS) {
2900*0Sstevel@tonic-gate 		ctrl->rv = DDI_FAILURE;
2901*0Sstevel@tonic-gate 		ctrl->dip = dip;
2902*0Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
2903*0Sstevel@tonic-gate 	}
2904*0Sstevel@tonic-gate 
2905*0Sstevel@tonic-gate 	/* get the pci device id information */
2906*0Sstevel@tonic-gate 	pci_dev = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
2907*0Sstevel@tonic-gate 
2908*0Sstevel@tonic-gate 	/*
2909*0Sstevel@tonic-gate 	 * free the memory allocated by ddi_prop_lookup_int_array
2910*0Sstevel@tonic-gate 	 */
2911*0Sstevel@tonic-gate 	ddi_prop_free(pci_rp);
2912*0Sstevel@tonic-gate 
2913*0Sstevel@tonic-gate 	/*
2914*0Sstevel@tonic-gate 	 * Match the node for the device number of the slot.
2915*0Sstevel@tonic-gate 	 */
2916*0Sstevel@tonic-gate 	if (pci_dev == ctrl->pci_dev) {	/* node is a match */
2917*0Sstevel@tonic-gate 		if (ctrl->op == PCIHP_ONLINE) {
2918*0Sstevel@tonic-gate 			/* it is CONFIGURE operation */
2919*0Sstevel@tonic-gate 			rv = ndi_devi_online(dip, NDI_ONLINE_ATTACH|NDI_CONFIG);
2920*0Sstevel@tonic-gate 		} else {
2921*0Sstevel@tonic-gate 			/*
2922*0Sstevel@tonic-gate 			 * it is UNCONFIGURE operation.
2923*0Sstevel@tonic-gate 			 */
2924*0Sstevel@tonic-gate 			rv = ndi_devi_offline(dip, NDI_UNCONFIG);
2925*0Sstevel@tonic-gate 		}
2926*0Sstevel@tonic-gate 		if (rv != NDI_SUCCESS) {
2927*0Sstevel@tonic-gate 			/* failed to attach/detach the driver(s) */
2928*0Sstevel@tonic-gate 			ctrl->rv = rv;
2929*0Sstevel@tonic-gate 			ctrl->dip = dip;
2930*0Sstevel@tonic-gate 			/* terminate the search if specified */
2931*0Sstevel@tonic-gate 			if (!(ctrl->flags & PCIHP_CFG_CONTINUE))
2932*0Sstevel@tonic-gate 				return (DDI_WALK_TERMINATE);
2933*0Sstevel@tonic-gate 		}
2934*0Sstevel@tonic-gate 	}
2935*0Sstevel@tonic-gate 
2936*0Sstevel@tonic-gate 	/*
2937*0Sstevel@tonic-gate 	 * continue the walk to the next sibling to look for a match
2938*0Sstevel@tonic-gate 	 * or to find other nodes if this card is a multi-function card.
2939*0Sstevel@tonic-gate 	 */
2940*0Sstevel@tonic-gate 	return (DDI_WALK_PRUNECHILD);
2941*0Sstevel@tonic-gate }
2942*0Sstevel@tonic-gate 
2943*0Sstevel@tonic-gate /* control structure used to find a device in the devinfo tree */
2944*0Sstevel@tonic-gate struct pcihp_find_ctrl {
2945*0Sstevel@tonic-gate 	uint_t		device;
2946*0Sstevel@tonic-gate 	uint_t		function;
2947*0Sstevel@tonic-gate 	dev_info_t	*dip;
2948*0Sstevel@tonic-gate };
2949*0Sstevel@tonic-gate 
2950*0Sstevel@tonic-gate static dev_info_t *
2951*0Sstevel@tonic-gate pcihp_devi_find(dev_info_t *dip, uint_t device, uint_t function)
2952*0Sstevel@tonic-gate {
2953*0Sstevel@tonic-gate 	struct pcihp_find_ctrl ctrl;
2954*0Sstevel@tonic-gate 	int circular_count;
2955*0Sstevel@tonic-gate 
2956*0Sstevel@tonic-gate 	ctrl.device = device;
2957*0Sstevel@tonic-gate 	ctrl.function = function;
2958*0Sstevel@tonic-gate 	ctrl.dip = NULL;
2959*0Sstevel@tonic-gate 
2960*0Sstevel@tonic-gate 	ndi_devi_enter(dip, &circular_count);
2961*0Sstevel@tonic-gate 	ddi_walk_devs(ddi_get_child(dip), pcihp_match_dev, (void *)&ctrl);
2962*0Sstevel@tonic-gate 	ndi_devi_exit(dip, circular_count);
2963*0Sstevel@tonic-gate 
2964*0Sstevel@tonic-gate 	return (ctrl.dip);
2965*0Sstevel@tonic-gate }
2966*0Sstevel@tonic-gate 
2967*0Sstevel@tonic-gate static int
2968*0Sstevel@tonic-gate pcihp_match_dev(dev_info_t *dip, void *hdl)
2969*0Sstevel@tonic-gate {
2970*0Sstevel@tonic-gate 	struct pcihp_find_ctrl *ctrl = (struct pcihp_find_ctrl *)hdl;
2971*0Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
2972*0Sstevel@tonic-gate 	int length;
2973*0Sstevel@tonic-gate 	int pci_dev;
2974*0Sstevel@tonic-gate 	int pci_func;
2975*0Sstevel@tonic-gate 
2976*0Sstevel@tonic-gate 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
2977*0Sstevel@tonic-gate 		DDI_PROP_DONTPASS, "reg", (int **)&pci_rp,
2978*0Sstevel@tonic-gate 		(uint_t *)&length) != DDI_PROP_SUCCESS) {
2979*0Sstevel@tonic-gate 		ctrl->dip = NULL;
2980*0Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
2981*0Sstevel@tonic-gate 	}
2982*0Sstevel@tonic-gate 
2983*0Sstevel@tonic-gate 	/* get the PCI device address info */
2984*0Sstevel@tonic-gate 	pci_dev = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
2985*0Sstevel@tonic-gate 	pci_func = PCI_REG_FUNC_G(pci_rp->pci_phys_hi);
2986*0Sstevel@tonic-gate 
2987*0Sstevel@tonic-gate 	/*
2988*0Sstevel@tonic-gate 	 * free the memory allocated by ddi_prop_lookup_int_array
2989*0Sstevel@tonic-gate 	 */
2990*0Sstevel@tonic-gate 	ddi_prop_free(pci_rp);
2991*0Sstevel@tonic-gate 
2992*0Sstevel@tonic-gate 
2993*0Sstevel@tonic-gate 	if ((pci_dev == ctrl->device) && (pci_func == ctrl->function)) {
2994*0Sstevel@tonic-gate 		/* found the match for the specified device address */
2995*0Sstevel@tonic-gate 		ctrl->dip = dip;
2996*0Sstevel@tonic-gate 		return (DDI_WALK_TERMINATE);
2997*0Sstevel@tonic-gate 	}
2998*0Sstevel@tonic-gate 
2999*0Sstevel@tonic-gate 	/*
3000*0Sstevel@tonic-gate 	 * continue the walk to the next sibling to look for a match.
3001*0Sstevel@tonic-gate 	 */
3002*0Sstevel@tonic-gate 	return (DDI_WALK_PRUNECHILD);
3003*0Sstevel@tonic-gate }
3004*0Sstevel@tonic-gate 
3005*0Sstevel@tonic-gate #if 0
3006*0Sstevel@tonic-gate /*
3007*0Sstevel@tonic-gate  * Probe the configuration space of the slot to determine the receptacle
3008*0Sstevel@tonic-gate  * state. There may not be any devinfo tree created for this slot.
3009*0Sstevel@tonic-gate  */
3010*0Sstevel@tonic-gate static void
3011*0Sstevel@tonic-gate pcihp_probe_slot_state(dev_info_t *dip, int dev, hpc_slot_state_t *rstatep)
3012*0Sstevel@tonic-gate {
3013*0Sstevel@tonic-gate 	/* XXX FIX IT */
3014*0Sstevel@tonic-gate }
3015*0Sstevel@tonic-gate #endif
3016*0Sstevel@tonic-gate 
3017*0Sstevel@tonic-gate /*
3018*0Sstevel@tonic-gate  * This routine is called when a ENUM# assertion is detected for a bus.
3019*0Sstevel@tonic-gate  * Since ENUM# may be bussed, the slot that asserted ENUM# may not be known.
3020*0Sstevel@tonic-gate  * The HPC Driver passes the handle of a slot that is its best guess.
3021*0Sstevel@tonic-gate  * If the best guess slot is the one that asserted ENUM#, the proper handling
3022*0Sstevel@tonic-gate  * will be done.  If its not, all possible slots will be locked at until
3023*0Sstevel@tonic-gate  * one that is asserting ENUM is found.
3024*0Sstevel@tonic-gate  * Also, indicate to the HSC to turn on ENUM# after it is serviced,
3025*0Sstevel@tonic-gate  * incase if it was disabled by the HSC due to the nature of asynchronous
3026*0Sstevel@tonic-gate  * delivery of interrupt by the framework.
3027*0Sstevel@tonic-gate  *
3028*0Sstevel@tonic-gate  * opcode has the following meanings.
3029*0Sstevel@tonic-gate  * PCIHP_CLEAR_ENUM = just clear interrupt and return the PCI device no. if
3030*0Sstevel@tonic-gate  *			success, else return -1.
3031*0Sstevel@tonic-gate  * PCIHP_HANDLE_ENUM = clear interrupt and handle interrupt also.
3032*0Sstevel@tonic-gate  *
3033*0Sstevel@tonic-gate  */
3034*0Sstevel@tonic-gate static int
3035*0Sstevel@tonic-gate pcihp_handle_enum(pcihp_t *pcihp_p, int favorite_pci_dev, int opcode,
3036*0Sstevel@tonic-gate 	int kmflag)
3037*0Sstevel@tonic-gate {
3038*0Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
3039*0Sstevel@tonic-gate 	int pci_dev, rc, event_serviced = 0;
3040*0Sstevel@tonic-gate 
3041*0Sstevel@tonic-gate 	/*
3042*0Sstevel@tonic-gate 	 * Handle ENUM# condition for the "favorite" slot first.
3043*0Sstevel@tonic-gate 	 */
3044*0Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[favorite_pci_dev];
3045*0Sstevel@tonic-gate 	if (slotinfop) {
3046*0Sstevel@tonic-gate 		/*
3047*0Sstevel@tonic-gate 		 * First try the "favorite" pci device.  This is the device
3048*0Sstevel@tonic-gate 		 * associated with the handle passed by the HPC Driver.
3049*0Sstevel@tonic-gate 		 */
3050*0Sstevel@tonic-gate 		rc = pcihp_enum_slot(pcihp_p, slotinfop, favorite_pci_dev,
3051*0Sstevel@tonic-gate 			opcode, kmflag);
3052*0Sstevel@tonic-gate 		if (rc != HPC_EVENT_UNCLAIMED) {	/* indicates success */
3053*0Sstevel@tonic-gate 			event_serviced = 1;
3054*0Sstevel@tonic-gate 			/* This MUST be a non-DEBUG feature. */
3055*0Sstevel@tonic-gate 			if (! pcihp_enum_scan_all) {
3056*0Sstevel@tonic-gate 				return (rc);
3057*0Sstevel@tonic-gate 			}
3058*0Sstevel@tonic-gate 		}
3059*0Sstevel@tonic-gate 	}
3060*0Sstevel@tonic-gate 
3061*0Sstevel@tonic-gate 	/*
3062*0Sstevel@tonic-gate 	 * If ENUM# is implemented as a radial signal, then there is no
3063*0Sstevel@tonic-gate 	 * need to further poll the slots.
3064*0Sstevel@tonic-gate 	 */
3065*0Sstevel@tonic-gate 	if (pcihp_p->bus_flags & PCIHP_BUS_ENUM_RADIAL)
3066*0Sstevel@tonic-gate 		goto enum_service_check;
3067*0Sstevel@tonic-gate 
3068*0Sstevel@tonic-gate 	/*
3069*0Sstevel@tonic-gate 	 * If the "favorite" pci device didn't assert ENUM#, then
3070*0Sstevel@tonic-gate 	 * try the rest.  Once we find and handle a device that asserted
3071*0Sstevel@tonic-gate 	 * ENUM#, then we will terminate the walk by returning unless
3072*0Sstevel@tonic-gate 	 * scan-all flag is set.
3073*0Sstevel@tonic-gate 	 */
3074*0Sstevel@tonic-gate 	for (pci_dev = 0; pci_dev < PCI_MAX_DEVS; pci_dev++) {
3075*0Sstevel@tonic-gate 		if (pci_dev != favorite_pci_dev) {
3076*0Sstevel@tonic-gate 			slotinfop = &pcihp_p->slotinfo[pci_dev];
3077*0Sstevel@tonic-gate 			if (slotinfop == NULL) {
3078*0Sstevel@tonic-gate 				continue;
3079*0Sstevel@tonic-gate 			}
3080*0Sstevel@tonic-gate 			/* Only CPCI devices support ENUM# generation. */
3081*0Sstevel@tonic-gate 			if (!(slotinfop->slot_type & HPC_SLOT_TYPE_CPCI))
3082*0Sstevel@tonic-gate 				continue;
3083*0Sstevel@tonic-gate 			rc = pcihp_enum_slot(pcihp_p, slotinfop, pci_dev,
3084*0Sstevel@tonic-gate 				opcode, kmflag);
3085*0Sstevel@tonic-gate 			if (rc != HPC_EVENT_UNCLAIMED) {
3086*0Sstevel@tonic-gate 				event_serviced = 1;
3087*0Sstevel@tonic-gate 				/* This MUST be a non-DEBUG feature. */
3088*0Sstevel@tonic-gate 				if (! pcihp_enum_scan_all)
3089*0Sstevel@tonic-gate 					break;
3090*0Sstevel@tonic-gate 			}
3091*0Sstevel@tonic-gate 		}
3092*0Sstevel@tonic-gate 	}
3093*0Sstevel@tonic-gate 
3094*0Sstevel@tonic-gate enum_service_check:
3095*0Sstevel@tonic-gate 	if (event_serviced) {
3096*0Sstevel@tonic-gate 		return (rc);
3097*0Sstevel@tonic-gate 	}
3098*0Sstevel@tonic-gate 
3099*0Sstevel@tonic-gate 	/* No ENUM# event found, Return */
3100*0Sstevel@tonic-gate 	return (HPC_EVENT_UNCLAIMED);
3101*0Sstevel@tonic-gate }
3102*0Sstevel@tonic-gate 
3103*0Sstevel@tonic-gate /*
3104*0Sstevel@tonic-gate  * This routine attempts to handle a possible ENUM# assertion case for a
3105*0Sstevel@tonic-gate  * specified slot.  This only works for adapters that implement Hot Swap
3106*0Sstevel@tonic-gate  * Friendly Silicon.  If the slot's HS_CSR is read and it specifies ENUM#
3107*0Sstevel@tonic-gate  * has been asserted, either the insertion or removal handlers will be
3108*0Sstevel@tonic-gate  * called.
3109*0Sstevel@tonic-gate  */
3110*0Sstevel@tonic-gate static int
3111*0Sstevel@tonic-gate pcihp_enum_slot(pcihp_t *pcihp_p, struct pcihp_slotinfo *slotinfop, int pci_dev,
3112*0Sstevel@tonic-gate 		int opcode, int kmflag)
3113*0Sstevel@tonic-gate {
3114*0Sstevel@tonic-gate 	ddi_acc_handle_t handle;
3115*0Sstevel@tonic-gate 	dev_info_t *dip, *new_child = NULL;
3116*0Sstevel@tonic-gate 	int result, rv = -1;
3117*0Sstevel@tonic-gate 	uint8_t hs_csr;
3118*0Sstevel@tonic-gate 
3119*0Sstevel@tonic-gate 	if (pcihp_config_setup(&dip, &handle, &new_child, pci_dev,
3120*0Sstevel@tonic-gate 				pcihp_p) != DDI_SUCCESS) {
3121*0Sstevel@tonic-gate 		return (HPC_EVENT_UNCLAIMED);
3122*0Sstevel@tonic-gate 	}
3123*0Sstevel@tonic-gate 
3124*0Sstevel@tonic-gate 	/*
3125*0Sstevel@tonic-gate 	 * Read the device's HS_CSR.
3126*0Sstevel@tonic-gate 	 */
3127*0Sstevel@tonic-gate 	result = pcihp_get_hs_csr(slotinfop, handle, (uint8_t *)&hs_csr);
3128*0Sstevel@tonic-gate 	PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): hs_csr = %x, flags = %x",
3129*0Sstevel@tonic-gate 		ddi_driver_name(pcihp_p->dip), ddi_get_instance(pcihp_p->dip),
3130*0Sstevel@tonic-gate 		hs_csr, slotinfop->slot_flags));
3131*0Sstevel@tonic-gate 	/*
3132*0Sstevel@tonic-gate 	 * we teardown our device map here, because in case of an
3133*0Sstevel@tonic-gate 	 * extraction event, our nodes would be freed and a teardown
3134*0Sstevel@tonic-gate 	 * will cause problems.
3135*0Sstevel@tonic-gate 	 */
3136*0Sstevel@tonic-gate 	pcihp_config_teardown(&handle, &new_child, pci_dev, pcihp_p);
3137*0Sstevel@tonic-gate 
3138*0Sstevel@tonic-gate 	if (result == PCIHP_SUCCESS) {
3139*0Sstevel@tonic-gate 
3140*0Sstevel@tonic-gate 		/* If ENUM# is masked, then it is not us. Some other device */
3141*0Sstevel@tonic-gate 		if ((hs_csr & HS_CSR_EIM) && (opcode == PCIHP_CLEAR_ENUM))
3142*0Sstevel@tonic-gate 			return (HPC_EVENT_UNCLAIMED);
3143*0Sstevel@tonic-gate 		/*
3144*0Sstevel@tonic-gate 		 * This device supports Full Hot Swap and implements
3145*0Sstevel@tonic-gate 		 * the Hot Swap Control and Status Register.
3146*0Sstevel@tonic-gate 		 */
3147*0Sstevel@tonic-gate 		if ((hs_csr & HS_CSR_INS) ||
3148*0Sstevel@tonic-gate 			(slotinfop->slot_flags & PCIHP_SLOT_ENUM_INS_PENDING)) {
3149*0Sstevel@tonic-gate 			/* handle insertion ENUM */
3150*0Sstevel@tonic-gate 			PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): "
3151*0Sstevel@tonic-gate 			    "Handle Insertion ENUM (INS) "
3152*0Sstevel@tonic-gate 			    "on the bus (for slot %s ?)",
3153*0Sstevel@tonic-gate 			    ddi_driver_name(pcihp_p->dip),
3154*0Sstevel@tonic-gate 			    ddi_get_instance(pcihp_p->dip),
3155*0Sstevel@tonic-gate 			    slotinfop->name));
3156*0Sstevel@tonic-gate 
3157*0Sstevel@tonic-gate 			/*
3158*0Sstevel@tonic-gate 			 * generate sysevent
3159*0Sstevel@tonic-gate 			 */
3160*0Sstevel@tonic-gate 
3161*0Sstevel@tonic-gate 			if (opcode == PCIHP_CLEAR_ENUM)
3162*0Sstevel@tonic-gate 				pcihp_gen_sysevent(slotinfop->name,
3163*0Sstevel@tonic-gate 					PCIHP_DR_REQ,
3164*0Sstevel@tonic-gate 					SE_INCOMING_RES, pcihp_p->dip,
3165*0Sstevel@tonic-gate 					kmflag);
3166*0Sstevel@tonic-gate 
3167*0Sstevel@tonic-gate 			rv = pcihp_handle_enum_insertion(pcihp_p, pci_dev,
3168*0Sstevel@tonic-gate 				opcode, kmflag);
3169*0Sstevel@tonic-gate 
3170*0Sstevel@tonic-gate 		} else if ((hs_csr & HS_CSR_EXT) || (slotinfop->slot_flags &
3171*0Sstevel@tonic-gate 					PCIHP_SLOT_ENUM_EXT_PENDING)) {
3172*0Sstevel@tonic-gate 			/* handle extraction ENUM */
3173*0Sstevel@tonic-gate 			PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): "
3174*0Sstevel@tonic-gate 			    "Handle Extraction ENUM (EXT) "
3175*0Sstevel@tonic-gate 			    "on the bus (for slot %s ?)",
3176*0Sstevel@tonic-gate 			    ddi_driver_name(pcihp_p->dip),
3177*0Sstevel@tonic-gate 			    ddi_get_instance(pcihp_p->dip),
3178*0Sstevel@tonic-gate 			    slotinfop->name));
3179*0Sstevel@tonic-gate 
3180*0Sstevel@tonic-gate 			/*
3181*0Sstevel@tonic-gate 			 * generate sysevent
3182*0Sstevel@tonic-gate 			 */
3183*0Sstevel@tonic-gate 
3184*0Sstevel@tonic-gate 			if (opcode == PCIHP_CLEAR_ENUM)
3185*0Sstevel@tonic-gate 				pcihp_gen_sysevent(slotinfop->name,
3186*0Sstevel@tonic-gate 					PCIHP_DR_REQ,
3187*0Sstevel@tonic-gate 					SE_OUTGOING_RES,
3188*0Sstevel@tonic-gate 					pcihp_p->dip,
3189*0Sstevel@tonic-gate 					kmflag);
3190*0Sstevel@tonic-gate 
3191*0Sstevel@tonic-gate 			rv = pcihp_handle_enum_extraction(pcihp_p, pci_dev,
3192*0Sstevel@tonic-gate 				opcode, kmflag);
3193*0Sstevel@tonic-gate 		}
3194*0Sstevel@tonic-gate 		if (opcode == PCIHP_CLEAR_ENUM) {
3195*0Sstevel@tonic-gate 			if (rv == PCIHP_SUCCESS)
3196*0Sstevel@tonic-gate 				rv = pci_dev;
3197*0Sstevel@tonic-gate 			else
3198*0Sstevel@tonic-gate 				rv = HPC_EVENT_UNCLAIMED;
3199*0Sstevel@tonic-gate 		}
3200*0Sstevel@tonic-gate 	}
3201*0Sstevel@tonic-gate 
3202*0Sstevel@tonic-gate 	return (rv);
3203*0Sstevel@tonic-gate }
3204*0Sstevel@tonic-gate 
3205*0Sstevel@tonic-gate /*
3206*0Sstevel@tonic-gate  * This routine is called when a ENUM# caused by lifting the lever
3207*0Sstevel@tonic-gate  * is detected.  If the occupant is configured, it will be unconfigured.
3208*0Sstevel@tonic-gate  * If the occupant is already unconfigured or is successfully unconfigured,
3209*0Sstevel@tonic-gate  * the blue LED on the adapter is illuminated which means its OK to remove.
3210*0Sstevel@tonic-gate  * Please note that the lock must be released before invoking the
3211*0Sstevel@tonic-gate  * generic AP unconfigure function.
3212*0Sstevel@tonic-gate  */
3213*0Sstevel@tonic-gate static int
3214*0Sstevel@tonic-gate pcihp_handle_enum_extraction(pcihp_t *pcihp_p, int pci_dev, int opcode,
3215*0Sstevel@tonic-gate 	int kmflag)
3216*0Sstevel@tonic-gate {
3217*0Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
3218*0Sstevel@tonic-gate 	int rv = PCIHP_FAILURE;
3219*0Sstevel@tonic-gate 
3220*0Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
3221*0Sstevel@tonic-gate 
3222*0Sstevel@tonic-gate 	/*
3223*0Sstevel@tonic-gate 	 * It was observed that, clearing the EXT bit turned the LED ON.
3224*0Sstevel@tonic-gate 	 * This is a BIG problem in case if the unconfigure operation
3225*0Sstevel@tonic-gate 	 * failed because the board was busy.
3226*0Sstevel@tonic-gate 	 * In order to avoid this confusing situation (LED ON but the board
3227*0Sstevel@tonic-gate 	 * is not unconfigured), we instead decided not to clear EXT but
3228*0Sstevel@tonic-gate 	 * disable further ENUM# from this slot. Disabling ENUM# clears
3229*0Sstevel@tonic-gate 	 * the interrupt.
3230*0Sstevel@tonic-gate 	 * Finally before returning we clear the interrupt and enable
3231*0Sstevel@tonic-gate 	 * ENUM# back again from this slot.
3232*0Sstevel@tonic-gate 	 */
3233*0Sstevel@tonic-gate 	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_DISABLE_ENUM);
3234*0Sstevel@tonic-gate 	if (opcode == PCIHP_CLEAR_ENUM) {
3235*0Sstevel@tonic-gate 		slotinfop->slot_flags |= PCIHP_SLOT_ENUM_EXT_PENDING;
3236*0Sstevel@tonic-gate 		return (PCIHP_SUCCESS);
3237*0Sstevel@tonic-gate 	}
3238*0Sstevel@tonic-gate 
3239*0Sstevel@tonic-gate 	rv = pcihp_unconfigure_ap(pcihp_p, pci_dev);
3240*0Sstevel@tonic-gate 	if (rv != HPC_SUCCESS && rv != EBUSY) {
3241*0Sstevel@tonic-gate 		cmn_err(CE_NOTE, "%s%d: PCI device %x Failed on Unconfigure",
3242*0Sstevel@tonic-gate 		    ddi_driver_name(pcihp_p->dip),
3243*0Sstevel@tonic-gate 		    ddi_get_instance(pcihp_p->dip), pci_dev);
3244*0Sstevel@tonic-gate 	}
3245*0Sstevel@tonic-gate 	if (rv == EBUSY)
3246*0Sstevel@tonic-gate 		cmn_err(CE_NOTE, "%s%d: PCI device %x Busy",
3247*0Sstevel@tonic-gate 		    ddi_driver_name(pcihp_p->dip),
3248*0Sstevel@tonic-gate 		    ddi_get_instance(pcihp_p->dip), pci_dev);
3249*0Sstevel@tonic-gate 	if (rv) {
3250*0Sstevel@tonic-gate 		if (pcihp_cpci_blue_led)
3251*0Sstevel@tonic-gate 			pcihp_hs_csr_op(pcihp_p, pci_dev,
3252*0Sstevel@tonic-gate 					HPC_EVENT_SLOT_BLUE_LED_OFF);
3253*0Sstevel@tonic-gate 	}
3254*0Sstevel@tonic-gate 	/*
3255*0Sstevel@tonic-gate 	 * we must clear interrupt in case the unconfigure didn't do it
3256*0Sstevel@tonic-gate 	 * due to a duplicate interrupt. Extraction is success.
3257*0Sstevel@tonic-gate 	 */
3258*0Sstevel@tonic-gate 	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_SLOT_UNCONFIGURE);
3259*0Sstevel@tonic-gate 
3260*0Sstevel@tonic-gate 	if (!rv) {
3261*0Sstevel@tonic-gate 		/*
3262*0Sstevel@tonic-gate 		 * Sys Event Notification.
3263*0Sstevel@tonic-gate 		 */
3264*0Sstevel@tonic-gate 		pcihp_gen_sysevent(slotinfop->name, PCIHP_DR_AP_STATE_CHANGE,
3265*0Sstevel@tonic-gate 			SE_HINT_REMOVE, pcihp_p->dip, kmflag);
3266*0Sstevel@tonic-gate 	}
3267*0Sstevel@tonic-gate 
3268*0Sstevel@tonic-gate 	/*
3269*0Sstevel@tonic-gate 	 * Enable interrupts back from this board.
3270*0Sstevel@tonic-gate 	 * This could potentially be problematic in case if the user is
3271*0Sstevel@tonic-gate 	 * quick enough to extract the board.
3272*0Sstevel@tonic-gate 	 * But we must do it just in case if the switch is closed again.
3273*0Sstevel@tonic-gate 	 */
3274*0Sstevel@tonic-gate 	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_ENABLE_ENUM);
3275*0Sstevel@tonic-gate 	slotinfop->slot_flags &= ~PCIHP_SLOT_ENUM_EXT_PENDING;
3276*0Sstevel@tonic-gate 	return (rv);
3277*0Sstevel@tonic-gate }
3278*0Sstevel@tonic-gate 
3279*0Sstevel@tonic-gate /*
3280*0Sstevel@tonic-gate  * This routine is called when a ENUM# caused by when an adapter insertion
3281*0Sstevel@tonic-gate  * is detected.  If the occupant is successfully configured (i.e. PCI resources
3282*0Sstevel@tonic-gate  * successfully assigned, the blue LED is left off, otherwise if configuration
3283*0Sstevel@tonic-gate  * is not successful, the blue LED is illuminated.
3284*0Sstevel@tonic-gate  * Please note that the lock must be released before invoking the
3285*0Sstevel@tonic-gate  * generic AP configure function.
3286*0Sstevel@tonic-gate  */
3287*0Sstevel@tonic-gate static int
3288*0Sstevel@tonic-gate pcihp_handle_enum_insertion(pcihp_t *pcihp_p, int pci_dev, int opcode,
3289*0Sstevel@tonic-gate 	int kmflag)
3290*0Sstevel@tonic-gate {
3291*0Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
3292*0Sstevel@tonic-gate 	int rv = PCIHP_FAILURE;
3293*0Sstevel@tonic-gate 	minor_t ap_minor;
3294*0Sstevel@tonic-gate 	major_t ap_major;
3295*0Sstevel@tonic-gate 
3296*0Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
3297*0Sstevel@tonic-gate 	slotinfop->hs_csr_location = 0;
3298*0Sstevel@tonic-gate 	/* we clear the interrupt here. This is a must here. */
3299*0Sstevel@tonic-gate 	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_SLOT_CONFIGURE);
3300*0Sstevel@tonic-gate 	/*
3301*0Sstevel@tonic-gate 	 * disable further interrupt from this board till it is
3302*0Sstevel@tonic-gate 	 * configured.
3303*0Sstevel@tonic-gate 	 */
3304*0Sstevel@tonic-gate 	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_DISABLE_ENUM);
3305*0Sstevel@tonic-gate 	if (opcode == PCIHP_CLEAR_ENUM) {
3306*0Sstevel@tonic-gate 		slotinfop->slot_flags |= PCIHP_SLOT_ENUM_INS_PENDING;
3307*0Sstevel@tonic-gate 		return (PCIHP_SUCCESS);
3308*0Sstevel@tonic-gate 	}
3309*0Sstevel@tonic-gate 
3310*0Sstevel@tonic-gate 	if ((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) ==
3311*0Sstevel@tonic-gate 					PCIHP_SLOT_AUTO_CFG_EN) {
3312*0Sstevel@tonic-gate 		rv = pcihp_configure_ap(pcihp_p, pci_dev);
3313*0Sstevel@tonic-gate 		if (rv != HPC_SUCCESS) {	/* configure failed */
3314*0Sstevel@tonic-gate 			cmn_err(CE_NOTE, "%s%d: PCI device %x Failed on"
3315*0Sstevel@tonic-gate 				" Configure", ddi_driver_name(pcihp_p->dip),
3316*0Sstevel@tonic-gate 				ddi_get_instance(pcihp_p->dip), pci_dev);
3317*0Sstevel@tonic-gate 			if (pcihp_cpci_blue_led)
3318*0Sstevel@tonic-gate 				pcihp_hs_csr_op(pcihp_p, pci_dev,
3319*0Sstevel@tonic-gate 						HPC_EVENT_SLOT_BLUE_LED_ON);
3320*0Sstevel@tonic-gate 		}
3321*0Sstevel@tonic-gate 
3322*0Sstevel@tonic-gate 		/* pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_CLEAR_ENUM); */
3323*0Sstevel@tonic-gate 		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_ENABLE_ENUM);
3324*0Sstevel@tonic-gate 
3325*0Sstevel@tonic-gate 		if (!rv) {
3326*0Sstevel@tonic-gate 			ap_major = ddi_driver_major(pcihp_p->dip);
3327*0Sstevel@tonic-gate 			ap_minor = PCIHP_AP_MINOR_NUM(
3328*0Sstevel@tonic-gate 			    ddi_get_instance(pcihp_p->dip), pci_dev);
3329*0Sstevel@tonic-gate 			pcihp_create_occupant_props(pcihp_p->dip,
3330*0Sstevel@tonic-gate 			    makedevice(ap_major, ap_minor), pci_dev);
3331*0Sstevel@tonic-gate 
3332*0Sstevel@tonic-gate 			/*
3333*0Sstevel@tonic-gate 			 * Sys Event Notification.
3334*0Sstevel@tonic-gate 			 */
3335*0Sstevel@tonic-gate 			pcihp_gen_sysevent(slotinfop->name,
3336*0Sstevel@tonic-gate 				PCIHP_DR_AP_STATE_CHANGE,
3337*0Sstevel@tonic-gate 				SE_HINT_INSERT, pcihp_p->dip, kmflag);
3338*0Sstevel@tonic-gate 		}
3339*0Sstevel@tonic-gate 
3340*0Sstevel@tonic-gate 	} else
3341*0Sstevel@tonic-gate 		rv = PCIHP_SUCCESS;
3342*0Sstevel@tonic-gate 	slotinfop->slot_flags &= ~PCIHP_SLOT_ENUM_INS_PENDING;
3343*0Sstevel@tonic-gate 	return (rv);
3344*0Sstevel@tonic-gate }
3345*0Sstevel@tonic-gate 
3346*0Sstevel@tonic-gate /*
3347*0Sstevel@tonic-gate  * Read the Hot Swap Control and Status Register (HS_CSR) and
3348*0Sstevel@tonic-gate  * place the result in the location pointed to be hs_csr.
3349*0Sstevel@tonic-gate  */
3350*0Sstevel@tonic-gate static int
3351*0Sstevel@tonic-gate pcihp_get_hs_csr(struct pcihp_slotinfo *slotinfop,
3352*0Sstevel@tonic-gate     ddi_acc_handle_t config_handle, uint8_t *hs_csr)
3353*0Sstevel@tonic-gate {
3354*0Sstevel@tonic-gate 	if (slotinfop->hs_csr_location == -1)
3355*0Sstevel@tonic-gate 		return (PCIHP_FAILURE);
3356*0Sstevel@tonic-gate 
3357*0Sstevel@tonic-gate 	if (slotinfop->hs_csr_location == 0) {
3358*0Sstevel@tonic-gate 		slotinfop->hs_csr_location =
3359*0Sstevel@tonic-gate 			pcihp_get_hs_csr_location(config_handle);
3360*0Sstevel@tonic-gate 
3361*0Sstevel@tonic-gate 		if (slotinfop->hs_csr_location == -1)
3362*0Sstevel@tonic-gate 			return (PCIHP_FAILURE);
3363*0Sstevel@tonic-gate 	}
3364*0Sstevel@tonic-gate 	*hs_csr = pci_config_get8(config_handle, slotinfop->hs_csr_location);
3365*0Sstevel@tonic-gate 	return (PCIHP_SUCCESS);
3366*0Sstevel@tonic-gate }
3367*0Sstevel@tonic-gate 
3368*0Sstevel@tonic-gate /*
3369*0Sstevel@tonic-gate  * Write the Hot Swap Control and Status Register (HS_CSR) with
3370*0Sstevel@tonic-gate  * the value being pointed at by hs_csr.
3371*0Sstevel@tonic-gate  */
3372*0Sstevel@tonic-gate static void
3373*0Sstevel@tonic-gate pcihp_set_hs_csr(struct pcihp_slotinfo *slotinfop,
3374*0Sstevel@tonic-gate     ddi_acc_handle_t config_handle, uint8_t *hs_csr)
3375*0Sstevel@tonic-gate {
3376*0Sstevel@tonic-gate 	if (slotinfop->hs_csr_location == -1)
3377*0Sstevel@tonic-gate 		return;
3378*0Sstevel@tonic-gate 	if (slotinfop->hs_csr_location == 0) {
3379*0Sstevel@tonic-gate 		slotinfop->hs_csr_location =
3380*0Sstevel@tonic-gate 			pcihp_get_hs_csr_location(config_handle);
3381*0Sstevel@tonic-gate 		if (slotinfop->hs_csr_location == -1)
3382*0Sstevel@tonic-gate 			return;
3383*0Sstevel@tonic-gate 	}
3384*0Sstevel@tonic-gate 	pci_config_put8(config_handle, slotinfop->hs_csr_location, *hs_csr);
3385*0Sstevel@tonic-gate 	PCIHP_DEBUG((CE_NOTE, "hs_csr wrote %x, read %x", *hs_csr,
3386*0Sstevel@tonic-gate 		pci_config_get8(config_handle, slotinfop->hs_csr_location)));
3387*0Sstevel@tonic-gate }
3388*0Sstevel@tonic-gate 
3389*0Sstevel@tonic-gate static int
3390*0Sstevel@tonic-gate pcihp_get_hs_csr_location(ddi_acc_handle_t config_handle)
3391*0Sstevel@tonic-gate {
3392*0Sstevel@tonic-gate 	uint8_t	cap_id;
3393*0Sstevel@tonic-gate 	uint_t	cap_id_loc;
3394*0Sstevel@tonic-gate 	uint16_t	status;
3395*0Sstevel@tonic-gate 	int location = -1;
3396*0Sstevel@tonic-gate #define	PCI_STAT_ECP_SUPP	0x10
3397*0Sstevel@tonic-gate 
3398*0Sstevel@tonic-gate 	/*
3399*0Sstevel@tonic-gate 	 * Need to check the Status register for ECP support first.
3400*0Sstevel@tonic-gate 	 * Also please note that for type 1 devices, the
3401*0Sstevel@tonic-gate 	 * offset could change. Should support type 1 next.
3402*0Sstevel@tonic-gate 	 */
3403*0Sstevel@tonic-gate 	status = pci_config_get16(config_handle, PCI_CONF_STAT);
3404*0Sstevel@tonic-gate 	if (!(status & PCI_STAT_ECP_SUPP)) {
3405*0Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "No Ext Capabilities for device\n"));
3406*0Sstevel@tonic-gate 		return (-1);
3407*0Sstevel@tonic-gate 	}
3408*0Sstevel@tonic-gate 	cap_id_loc = pci_config_get8(config_handle, PCI_CONF_EXTCAP);
3409*0Sstevel@tonic-gate 
3410*0Sstevel@tonic-gate 	/*
3411*0Sstevel@tonic-gate 	 * Walk the list of capabilities, but don't walk past the end
3412*0Sstevel@tonic-gate 	 * of the Configuration Space Header.
3413*0Sstevel@tonic-gate 	 */
3414*0Sstevel@tonic-gate 	while ((cap_id_loc) && (cap_id_loc < PCI_CONF_HDR_SIZE)) {
3415*0Sstevel@tonic-gate 
3416*0Sstevel@tonic-gate 		cap_id = pci_config_get8(config_handle, cap_id_loc);
3417*0Sstevel@tonic-gate 
3418*0Sstevel@tonic-gate 		if (cap_id == CPCI_HOTSWAP_CAPID) {
3419*0Sstevel@tonic-gate 			location = cap_id_loc + PCI_ECP_HS_CSR;
3420*0Sstevel@tonic-gate 			break;
3421*0Sstevel@tonic-gate 		}
3422*0Sstevel@tonic-gate 		cap_id_loc = pci_config_get8(config_handle,
3423*0Sstevel@tonic-gate 		    cap_id_loc + 1);
3424*0Sstevel@tonic-gate 	}
3425*0Sstevel@tonic-gate 	return (location);
3426*0Sstevel@tonic-gate }
3427*0Sstevel@tonic-gate 
3428*0Sstevel@tonic-gate static int
3429*0Sstevel@tonic-gate pcihp_add_dummy_reg_property(dev_info_t *dip,
3430*0Sstevel@tonic-gate     uint_t bus, uint_t device, uint_t func)
3431*0Sstevel@tonic-gate {
3432*0Sstevel@tonic-gate 	pci_regspec_t dummy_reg;
3433*0Sstevel@tonic-gate 
3434*0Sstevel@tonic-gate 	bzero(&dummy_reg, sizeof (dummy_reg));
3435*0Sstevel@tonic-gate 
3436*0Sstevel@tonic-gate 	dummy_reg.pci_phys_hi = PCIHP_MAKE_REG_HIGH(bus, device, func, 0);
3437*0Sstevel@tonic-gate 
3438*0Sstevel@tonic-gate 	return (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
3439*0Sstevel@tonic-gate 	    "reg", (int *)&dummy_reg, sizeof (pci_regspec_t)/sizeof (int)));
3440*0Sstevel@tonic-gate }
3441*0Sstevel@tonic-gate 
3442*0Sstevel@tonic-gate static void
3443*0Sstevel@tonic-gate pcihp_hs_csr_op(pcihp_t *pcihp_p, int pci_dev, int event)
3444*0Sstevel@tonic-gate {
3445*0Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
3446*0Sstevel@tonic-gate 	ddi_acc_handle_t config_handle;
3447*0Sstevel@tonic-gate 	dev_info_t *dip, *new_child = NULL;
3448*0Sstevel@tonic-gate 	uint8_t hs_csr;
3449*0Sstevel@tonic-gate 	int result;
3450*0Sstevel@tonic-gate 
3451*0Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
3452*0Sstevel@tonic-gate 
3453*0Sstevel@tonic-gate 	if (pcihp_config_setup(&dip, &config_handle, &new_child, pci_dev,
3454*0Sstevel@tonic-gate 				pcihp_p) != DDI_SUCCESS) {
3455*0Sstevel@tonic-gate 		return;
3456*0Sstevel@tonic-gate 	}
3457*0Sstevel@tonic-gate 
3458*0Sstevel@tonic-gate 	result = pcihp_get_hs_csr(slotinfop, config_handle, (uint8_t *)&hs_csr);
3459*0Sstevel@tonic-gate 	if ((result != PCIHP_SUCCESS) || (event == -1)) {
3460*0Sstevel@tonic-gate 		pcihp_config_teardown(&config_handle, &new_child, pci_dev,
3461*0Sstevel@tonic-gate 							pcihp_p);
3462*0Sstevel@tonic-gate 		return;
3463*0Sstevel@tonic-gate 	}
3464*0Sstevel@tonic-gate 
3465*0Sstevel@tonic-gate 	hs_csr &= 0xf;
3466*0Sstevel@tonic-gate 	switch (event) {
3467*0Sstevel@tonic-gate 		case HPC_EVENT_SLOT_BLUE_LED_ON:
3468*0Sstevel@tonic-gate 			hs_csr |= HS_CSR_LOO;
3469*0Sstevel@tonic-gate 			break;
3470*0Sstevel@tonic-gate 		case HPC_EVENT_SLOT_BLUE_LED_OFF:
3471*0Sstevel@tonic-gate 			hs_csr &= ~HS_CSR_LOO;
3472*0Sstevel@tonic-gate 			break;
3473*0Sstevel@tonic-gate 		case HPC_EVENT_SLOT_CONFIGURE:
3474*0Sstevel@tonic-gate 			hs_csr |= HS_CSR_INS;	/* clear INS */
3475*0Sstevel@tonic-gate 			break;
3476*0Sstevel@tonic-gate 		case HPC_EVENT_CLEAR_ENUM:
3477*0Sstevel@tonic-gate 			hs_csr |= (HS_CSR_INS | HS_CSR_EXT);
3478*0Sstevel@tonic-gate 			break;
3479*0Sstevel@tonic-gate 		case HPC_EVENT_SLOT_UNCONFIGURE:
3480*0Sstevel@tonic-gate 			hs_csr |= HS_CSR_EXT;	/* clear EXT */
3481*0Sstevel@tonic-gate 			break;
3482*0Sstevel@tonic-gate 		case HPC_EVENT_ENABLE_ENUM:
3483*0Sstevel@tonic-gate 			hs_csr &= ~HS_CSR_EIM;
3484*0Sstevel@tonic-gate 			break;
3485*0Sstevel@tonic-gate 		case HPC_EVENT_DISABLE_ENUM:
3486*0Sstevel@tonic-gate 			hs_csr |= HS_CSR_EIM;
3487*0Sstevel@tonic-gate 			break;
3488*0Sstevel@tonic-gate 		case HPC_EVENT_SLOT_NOT_HEALTHY:
3489*0Sstevel@tonic-gate 		case HPC_EVENT_SLOT_HEALTHY_OK:
3490*0Sstevel@tonic-gate 		default:
3491*0Sstevel@tonic-gate 			break;
3492*0Sstevel@tonic-gate 	}
3493*0Sstevel@tonic-gate 	pcihp_set_hs_csr(slotinfop, config_handle, (uint8_t *)&hs_csr);
3494*0Sstevel@tonic-gate 	pcihp_config_teardown(&config_handle, &new_child, pci_dev, pcihp_p);
3495*0Sstevel@tonic-gate }
3496*0Sstevel@tonic-gate 
3497*0Sstevel@tonic-gate static int
3498*0Sstevel@tonic-gate pcihp_config_setup(dev_info_t **dip, ddi_acc_handle_t *handle,
3499*0Sstevel@tonic-gate 			dev_info_t **new_child, int pci_dev, pcihp_t *pcihp_p)
3500*0Sstevel@tonic-gate {
3501*0Sstevel@tonic-gate 	dev_info_t *pdip = pcihp_p->dip;
3502*0Sstevel@tonic-gate 	int bus, len, rc = DDI_SUCCESS;
3503*0Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop;
3504*0Sstevel@tonic-gate 	hpc_slot_state_t rstate;
3505*0Sstevel@tonic-gate 	ddi_acc_hdl_t *hp;
3506*0Sstevel@tonic-gate 	struct bus_range {
3507*0Sstevel@tonic-gate 		uint32_t lo;
3508*0Sstevel@tonic-gate 		uint32_t hi;
3509*0Sstevel@tonic-gate 	} pci_bus_range;
3510*0Sstevel@tonic-gate 	int flags = 0;
3511*0Sstevel@tonic-gate 
3512*0Sstevel@tonic-gate 	slotinfop = &pcihp_p->slotinfo[pci_dev];
3513*0Sstevel@tonic-gate 
3514*0Sstevel@tonic-gate 	/*
3515*0Sstevel@tonic-gate 	 * If declared failed, don't allow Config operations.
3516*0Sstevel@tonic-gate 	 * Otherwise, if good or failing, it is assumed Ok
3517*0Sstevel@tonic-gate 	 * to get config data.
3518*0Sstevel@tonic-gate 	 */
3519*0Sstevel@tonic-gate 	if (slotinfop->condition == AP_COND_FAILED) {
3520*0Sstevel@tonic-gate 		return (PCIHP_FAILURE);
3521*0Sstevel@tonic-gate 	}
3522*0Sstevel@tonic-gate 	/*
3523*0Sstevel@tonic-gate 	 * check to see if there is a hardware present first.
3524*0Sstevel@tonic-gate 	 * If no hardware present, no need to probe this slot.
3525*0Sstevel@tonic-gate 	 * We can do this first probably as a first step towards
3526*0Sstevel@tonic-gate 	 * safeguarding from accidental removal (we don't support it!).
3527*0Sstevel@tonic-gate 	 */
3528*0Sstevel@tonic-gate 	if (hpc_nexus_control(slotinfop->slot_hdl,
3529*0Sstevel@tonic-gate 			HPC_CTRL_GET_SLOT_STATE, (caddr_t)&rstate) != 0) {
3530*0Sstevel@tonic-gate 		return (DDI_FAILURE);
3531*0Sstevel@tonic-gate 	}
3532*0Sstevel@tonic-gate 
3533*0Sstevel@tonic-gate 	if (rstate != HPC_SLOT_CONNECTED) {
3534*0Sstevel@tonic-gate 		/* error. slot must be connected */
3535*0Sstevel@tonic-gate 		return (DDI_FAILURE);
3536*0Sstevel@tonic-gate 	}
3537*0Sstevel@tonic-gate 	*new_child = NULL;
3538*0Sstevel@tonic-gate 
3539*0Sstevel@tonic-gate 	/*
3540*0Sstevel@tonic-gate 	 * If there is no dip then we need to see if an
3541*0Sstevel@tonic-gate 	 * adapter has just been hot plugged.
3542*0Sstevel@tonic-gate 	 */
3543*0Sstevel@tonic-gate 	len = sizeof (struct bus_range);
3544*0Sstevel@tonic-gate 	if (ddi_getlongprop_buf(DDI_DEV_T_NONE, pdip,
3545*0Sstevel@tonic-gate 	    0, "bus-range",
3546*0Sstevel@tonic-gate 	    (caddr_t)&pci_bus_range, &len) != DDI_SUCCESS) {
3547*0Sstevel@tonic-gate 
3548*0Sstevel@tonic-gate 		return (PCIHP_FAILURE);
3549*0Sstevel@tonic-gate 	}
3550*0Sstevel@tonic-gate 
3551*0Sstevel@tonic-gate 	/* primary bus number of this bus node */
3552*0Sstevel@tonic-gate 	bus = pci_bus_range.lo;
3553*0Sstevel@tonic-gate 
3554*0Sstevel@tonic-gate 	if (ndi_devi_alloc(pdip, DEVI_PSEUDO_NEXNAME,
3555*0Sstevel@tonic-gate 	    (dnode_t)DEVI_SID_NODEID, dip) != NDI_SUCCESS) {
3556*0Sstevel@tonic-gate 
3557*0Sstevel@tonic-gate 		PCIHP_DEBUG((CE_NOTE, "Failed to alloc probe node\n"));
3558*0Sstevel@tonic-gate 		return (PCIHP_FAILURE);
3559*0Sstevel@tonic-gate 	}
3560*0Sstevel@tonic-gate 
3561*0Sstevel@tonic-gate 	if (pcihp_add_dummy_reg_property(*dip, bus,
3562*0Sstevel@tonic-gate 	    pci_dev, 0) != DDI_SUCCESS) {
3563*0Sstevel@tonic-gate 
3564*0Sstevel@tonic-gate 		(void) ndi_devi_free(*dip);
3565*0Sstevel@tonic-gate 		return (PCIHP_FAILURE);
3566*0Sstevel@tonic-gate 	}
3567*0Sstevel@tonic-gate 
3568*0Sstevel@tonic-gate 	/*
3569*0Sstevel@tonic-gate 	 * Probe for a device. Possibly a non (c)PCI board could be sitting
3570*0Sstevel@tonic-gate 	 * here which would never respond to PCI config cycles - in which
3571*0Sstevel@tonic-gate 	 * case we return. Eventually a configure operation would fail.
3572*0Sstevel@tonic-gate 	 */
3573*0Sstevel@tonic-gate 	if (pci_config_setup(*dip, handle) != DDI_SUCCESS) {
3574*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "Cannot set config space map for"
3575*0Sstevel@tonic-gate 		    " pci device number %d", pci_dev);
3576*0Sstevel@tonic-gate 		(void) ndi_devi_free(*dip);
3577*0Sstevel@tonic-gate 		return (PCIHP_FAILURE);
3578*0Sstevel@tonic-gate 	}
3579*0Sstevel@tonic-gate 
3580*0Sstevel@tonic-gate 	if (pcihp_indirect_map(*dip) == DDI_SUCCESS)
3581*0Sstevel@tonic-gate 		flags |= PCICFG_CONF_INDIRECT_MAP;
3582*0Sstevel@tonic-gate 
3583*0Sstevel@tonic-gate 	/*
3584*0Sstevel@tonic-gate 	 * See if there is any PCI HW at this location
3585*0Sstevel@tonic-gate 	 * by reading the Vendor ID.  If it returns with 0xffff
3586*0Sstevel@tonic-gate 	 * then there is no hardware at this location.
3587*0Sstevel@tonic-gate 	 */
3588*0Sstevel@tonic-gate 	if (flags & PCICFG_CONF_INDIRECT_MAP) {
3589*0Sstevel@tonic-gate 
3590*0Sstevel@tonic-gate 		if (pci_config_get16(*handle, 0) == 0xffff) {
3591*0Sstevel@tonic-gate 			pci_config_teardown(handle);
3592*0Sstevel@tonic-gate 			(void) ndi_devi_free(*dip);
3593*0Sstevel@tonic-gate 			return (PCIHP_FAILURE);
3594*0Sstevel@tonic-gate 		}
3595*0Sstevel@tonic-gate 	} else {
3596*0Sstevel@tonic-gate 		/* Check if mapping is OK */
3597*0Sstevel@tonic-gate 		hp = impl_acc_hdl_get(*handle);
3598*0Sstevel@tonic-gate 
3599*0Sstevel@tonic-gate 		if (ddi_peek16(*dip, (int16_t *)(hp->ah_addr),
3600*0Sstevel@tonic-gate 			(int16_t *)0) != DDI_SUCCESS) {
3601*0Sstevel@tonic-gate #ifdef DEBUG
3602*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "Cannot Map PCI config space for "
3603*0Sstevel@tonic-gate 			    "device number %d", pci_dev);
3604*0Sstevel@tonic-gate #endif
3605*0Sstevel@tonic-gate 			pci_config_teardown(handle);
3606*0Sstevel@tonic-gate 			(void) ndi_devi_free(*dip);
3607*0Sstevel@tonic-gate 			return (PCIHP_FAILURE);
3608*0Sstevel@tonic-gate 		}
3609*0Sstevel@tonic-gate 	}
3610*0Sstevel@tonic-gate 
3611*0Sstevel@tonic-gate 	*new_child = *dip;
3612*0Sstevel@tonic-gate 	return (rc);
3613*0Sstevel@tonic-gate 
3614*0Sstevel@tonic-gate }
3615*0Sstevel@tonic-gate 
3616*0Sstevel@tonic-gate static void
3617*0Sstevel@tonic-gate pcihp_config_teardown(ddi_acc_handle_t *handle,
3618*0Sstevel@tonic-gate 			dev_info_t **new_child, int pci_dev, pcihp_t *pcihp_p)
3619*0Sstevel@tonic-gate {
3620*0Sstevel@tonic-gate 	struct pcihp_slotinfo *slotinfop = &pcihp_p->slotinfo[pci_dev];
3621*0Sstevel@tonic-gate 
3622*0Sstevel@tonic-gate 	pci_config_teardown(handle);
3623*0Sstevel@tonic-gate 	if (*new_child) {
3624*0Sstevel@tonic-gate 		(void) ndi_devi_free(*new_child);
3625*0Sstevel@tonic-gate 		/*
3626*0Sstevel@tonic-gate 		 * If occupant not configured, reset HS_CSR location
3627*0Sstevel@tonic-gate 		 * so that we reprobe. This covers cases where
3628*0Sstevel@tonic-gate 		 * the receptacle had a status change without a
3629*0Sstevel@tonic-gate 		 * notification to the framework.
3630*0Sstevel@tonic-gate 		 */
3631*0Sstevel@tonic-gate 		if (slotinfop->ostate != AP_OSTATE_CONFIGURED)
3632*0Sstevel@tonic-gate 			slotinfop->hs_csr_location = 0;
3633*0Sstevel@tonic-gate 	}
3634*0Sstevel@tonic-gate }
3635*0Sstevel@tonic-gate 
3636*0Sstevel@tonic-gate static int
3637*0Sstevel@tonic-gate pcihp_get_board_type(struct pcihp_slotinfo *slotinfop)
3638*0Sstevel@tonic-gate {
3639*0Sstevel@tonic-gate 	hpc_board_type_t board_type;
3640*0Sstevel@tonic-gate 
3641*0Sstevel@tonic-gate 	/*
3642*0Sstevel@tonic-gate 	 * Get board type data structure, hpc_board_type_t.
3643*0Sstevel@tonic-gate 	 */
3644*0Sstevel@tonic-gate 	if (hpc_nexus_control(slotinfop->slot_hdl, HPC_CTRL_GET_BOARD_TYPE,
3645*0Sstevel@tonic-gate 				(caddr_t)&board_type) != 0) {
3646*0Sstevel@tonic-gate 
3647*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "Cannot Get Board Type..");
3648*0Sstevel@tonic-gate 		return (-1);
3649*0Sstevel@tonic-gate 	}
3650*0Sstevel@tonic-gate 
3651*0Sstevel@tonic-gate 	/*
3652*0Sstevel@tonic-gate 	 * We expect the Hotswap Controller to tell us if the board is
3653*0Sstevel@tonic-gate 	 * a hotswap board or not, as it probably cannot differentiate
3654*0Sstevel@tonic-gate 	 * between a basic hotswap board, a non hotswap board and a
3655*0Sstevel@tonic-gate 	 * hotswap nonfriendly board.
3656*0Sstevel@tonic-gate 	 * So here is the logic to differentiate between the various
3657*0Sstevel@tonic-gate 	 * types of cPCI boards.
3658*0Sstevel@tonic-gate 	 * In case if the HSC returns board type as unknown, we assign
3659*0Sstevel@tonic-gate 	 * the default board type as defined by a configurable variable
3660*0Sstevel@tonic-gate 	 * for a BHS, nonfriendly FHS and non HS board.
3661*0Sstevel@tonic-gate 	 */
3662*0Sstevel@tonic-gate 	if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
3663*0Sstevel@tonic-gate 		if (slotinfop->hs_csr_location > 0)
3664*0Sstevel@tonic-gate 			board_type = HPC_BOARD_CPCI_FULL_HS;
3665*0Sstevel@tonic-gate 		else {
3666*0Sstevel@tonic-gate 			if (board_type == HPC_BOARD_CPCI_HS) {
3667*0Sstevel@tonic-gate 				if (slotinfop->hs_csr_location
3668*0Sstevel@tonic-gate 					== -1)
3669*0Sstevel@tonic-gate 					board_type =
3670*0Sstevel@tonic-gate 					HPC_BOARD_CPCI_BASIC_HS;
3671*0Sstevel@tonic-gate 			}
3672*0Sstevel@tonic-gate 			if (board_type == HPC_BOARD_UNKNOWN) {
3673*0Sstevel@tonic-gate 				if (slotinfop->hs_csr_location
3674*0Sstevel@tonic-gate 					== -1) {
3675*0Sstevel@tonic-gate 					board_type =
3676*0Sstevel@tonic-gate 					pcihp_cpci_board_type;
3677*0Sstevel@tonic-gate 				} else if
3678*0Sstevel@tonic-gate 				(slotinfop->hs_csr_location
3679*0Sstevel@tonic-gate 					!= 0) {
3680*0Sstevel@tonic-gate 					board_type =
3681*0Sstevel@tonic-gate 					HPC_BOARD_CPCI_FULL_HS;
3682*0Sstevel@tonic-gate 				}
3683*0Sstevel@tonic-gate 			}
3684*0Sstevel@tonic-gate 		}
3685*0Sstevel@tonic-gate 		/*
3686*0Sstevel@tonic-gate 		 * If board type is a non hotswap board, then we must
3687*0Sstevel@tonic-gate 		 * deny a unconfigure operation. So set this flag.
3688*0Sstevel@tonic-gate 		 * Strictly speaking, there is no reason not to disallow
3689*0Sstevel@tonic-gate 		 * a unconfigure operation on nonhotswap boards. But this
3690*0Sstevel@tonic-gate 		 * is the only way we can prevent a user from accidentally
3691*0Sstevel@tonic-gate 		 * removing the board and damaging it.
3692*0Sstevel@tonic-gate 		 */
3693*0Sstevel@tonic-gate 		if (board_type == HPC_BOARD_CPCI_NON_HS)
3694*0Sstevel@tonic-gate 			slotinfop->slot_flags |=
3695*0Sstevel@tonic-gate 				PCIHP_SLOT_DEV_NON_HOTPLUG;
3696*0Sstevel@tonic-gate 		else
3697*0Sstevel@tonic-gate 			slotinfop->slot_flags &=
3698*0Sstevel@tonic-gate 				~PCIHP_SLOT_DEV_NON_HOTPLUG;
3699*0Sstevel@tonic-gate 	}
3700*0Sstevel@tonic-gate 	return (board_type);
3701*0Sstevel@tonic-gate }
3702*0Sstevel@tonic-gate 
3703*0Sstevel@tonic-gate 
3704*0Sstevel@tonic-gate /*
3705*0Sstevel@tonic-gate  * Generate the System Event with a possible hint.
3706*0Sstevel@tonic-gate  */
3707*0Sstevel@tonic-gate static void
3708*0Sstevel@tonic-gate pcihp_gen_sysevent(char *slot_name, int event_sub_class, int hint,
3709*0Sstevel@tonic-gate 				dev_info_t *self, int kmflag)
3710*0Sstevel@tonic-gate {
3711*0Sstevel@tonic-gate 
3712*0Sstevel@tonic-gate 	int err;
3713*0Sstevel@tonic-gate 	char *ev_subclass = NULL;
3714*0Sstevel@tonic-gate 	sysevent_id_t eid;
3715*0Sstevel@tonic-gate 	nvlist_t *ev_attr_list = NULL;
3716*0Sstevel@tonic-gate 	char attach_pnt[MAXPATHLEN];
3717*0Sstevel@tonic-gate 
3718*0Sstevel@tonic-gate 	/*
3719*0Sstevel@tonic-gate 	 * Minor device name (AP) will be bus path
3720*0Sstevel@tonic-gate 	 * concatenated with slot name
3721*0Sstevel@tonic-gate 	 */
3722*0Sstevel@tonic-gate 
3723*0Sstevel@tonic-gate 	(void) strcpy(attach_pnt, PCIHP_DEVICES_STR);
3724*0Sstevel@tonic-gate 	(void) ddi_pathname(self, attach_pnt + strlen(PCIHP_DEVICES_STR));
3725*0Sstevel@tonic-gate 	(void) strcat(attach_pnt, ":");
3726*0Sstevel@tonic-gate 	(void) strcat(attach_pnt, slot_name);
3727*0Sstevel@tonic-gate 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, kmflag);
3728*0Sstevel@tonic-gate 	if (err != 0) {
3729*0Sstevel@tonic-gate 		cmn_err(CE_WARN,
3730*0Sstevel@tonic-gate 		    "%s%d: Failed to allocate memory "
3731*0Sstevel@tonic-gate 		    "for event attributes%s", ddi_driver_name(self),
3732*0Sstevel@tonic-gate 		    ddi_get_instance(self), ESC_DR_AP_STATE_CHANGE);
3733*0Sstevel@tonic-gate 		return;
3734*0Sstevel@tonic-gate 	}
3735*0Sstevel@tonic-gate 
3736*0Sstevel@tonic-gate 	switch (event_sub_class) {
3737*0Sstevel@tonic-gate 
3738*0Sstevel@tonic-gate 	/* event sub class: ESC_DR_AP_STATE_CHANGE */
3739*0Sstevel@tonic-gate 	case PCIHP_DR_AP_STATE_CHANGE:
3740*0Sstevel@tonic-gate 
3741*0Sstevel@tonic-gate 		ev_subclass = ESC_DR_AP_STATE_CHANGE;
3742*0Sstevel@tonic-gate 
3743*0Sstevel@tonic-gate 		switch (hint) {
3744*0Sstevel@tonic-gate 
3745*0Sstevel@tonic-gate 		case SE_NO_HINT:	/* fall through */
3746*0Sstevel@tonic-gate 		case SE_HINT_INSERT:	/* fall through */
3747*0Sstevel@tonic-gate 		case SE_HINT_REMOVE:
3748*0Sstevel@tonic-gate 
3749*0Sstevel@tonic-gate 
3750*0Sstevel@tonic-gate 			err = nvlist_add_string(ev_attr_list, DR_HINT,
3751*0Sstevel@tonic-gate 			    SE_HINT2STR(hint));
3752*0Sstevel@tonic-gate 
3753*0Sstevel@tonic-gate 			if (err != 0) {
3754*0Sstevel@tonic-gate 				cmn_err(CE_WARN, "%s%d: Failed to add attr [%s]"
3755*0Sstevel@tonic-gate 					" for %s event", ddi_driver_name(self),
3756*0Sstevel@tonic-gate 					ddi_get_instance(self),
3757*0Sstevel@tonic-gate 					DR_HINT, ESC_DR_AP_STATE_CHANGE);
3758*0Sstevel@tonic-gate 				nvlist_free(ev_attr_list);
3759*0Sstevel@tonic-gate 				return;
3760*0Sstevel@tonic-gate 			}
3761*0Sstevel@tonic-gate 			break;
3762*0Sstevel@tonic-gate 
3763*0Sstevel@tonic-gate 		default:
3764*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: Unknown hint on sysevent",
3765*0Sstevel@tonic-gate 				ddi_driver_name(self), ddi_get_instance(self));
3766*0Sstevel@tonic-gate 			nvlist_free(ev_attr_list);
3767*0Sstevel@tonic-gate 			return;
3768*0Sstevel@tonic-gate 		}
3769*0Sstevel@tonic-gate 
3770*0Sstevel@tonic-gate 		break;
3771*0Sstevel@tonic-gate 
3772*0Sstevel@tonic-gate 	/* event sub class: ESC_DR_REQ */
3773*0Sstevel@tonic-gate 	case PCIHP_DR_REQ:
3774*0Sstevel@tonic-gate 
3775*0Sstevel@tonic-gate 		ev_subclass = ESC_DR_REQ;
3776*0Sstevel@tonic-gate 
3777*0Sstevel@tonic-gate 		switch (hint) {
3778*0Sstevel@tonic-gate 
3779*0Sstevel@tonic-gate 		case SE_INVESTIGATE_RES:	/* fall through */
3780*0Sstevel@tonic-gate 		case SE_INCOMING_RES:	/* fall through */
3781*0Sstevel@tonic-gate 		case SE_OUTGOING_RES:	/* fall through */
3782*0Sstevel@tonic-gate 
3783*0Sstevel@tonic-gate 			err = nvlist_add_string(ev_attr_list, DR_REQ_TYPE,
3784*0Sstevel@tonic-gate 				SE_REQ2STR(hint));
3785*0Sstevel@tonic-gate 
3786*0Sstevel@tonic-gate 			if (err != 0) {
3787*0Sstevel@tonic-gate 				cmn_err(CE_WARN,
3788*0Sstevel@tonic-gate 					"%s%d: Failed to add attr [%s] "
3789*0Sstevel@tonic-gate 					"for %s event", ddi_driver_name(self),
3790*0Sstevel@tonic-gate 					ddi_get_instance(self),
3791*0Sstevel@tonic-gate 					DR_REQ_TYPE, ESC_DR_REQ);
3792*0Sstevel@tonic-gate 				nvlist_free(ev_attr_list);
3793*0Sstevel@tonic-gate 				return;
3794*0Sstevel@tonic-gate 			}
3795*0Sstevel@tonic-gate 			break;
3796*0Sstevel@tonic-gate 
3797*0Sstevel@tonic-gate 		default:
3798*0Sstevel@tonic-gate 			cmn_err(CE_WARN,
3799*0Sstevel@tonic-gate 				"%s%d:  Unknown hint on sysevent",
3800*0Sstevel@tonic-gate 				ddi_driver_name(self), ddi_get_instance(self));
3801*0Sstevel@tonic-gate 			nvlist_free(ev_attr_list);
3802*0Sstevel@tonic-gate 			return;
3803*0Sstevel@tonic-gate 		}
3804*0Sstevel@tonic-gate 
3805*0Sstevel@tonic-gate 		break;
3806*0Sstevel@tonic-gate 
3807*0Sstevel@tonic-gate 	default:
3808*0Sstevel@tonic-gate 		cmn_err(CE_WARN,
3809*0Sstevel@tonic-gate 			"%s%d:  Unknown Event subclass", ddi_driver_name(self),
3810*0Sstevel@tonic-gate 			ddi_get_instance(self));
3811*0Sstevel@tonic-gate 		nvlist_free(ev_attr_list);
3812*0Sstevel@tonic-gate 		return;
3813*0Sstevel@tonic-gate 	}
3814*0Sstevel@tonic-gate 
3815*0Sstevel@tonic-gate 	/*
3816*0Sstevel@tonic-gate 	 * Add attachment point as attribute (common attribute)
3817*0Sstevel@tonic-gate 	 */
3818*0Sstevel@tonic-gate 
3819*0Sstevel@tonic-gate 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, attach_pnt);
3820*0Sstevel@tonic-gate 
3821*0Sstevel@tonic-gate 	if (err != 0) {
3822*0Sstevel@tonic-gate 		cmn_err(CE_WARN,
3823*0Sstevel@tonic-gate 			"%s%d: Failed to add attr [%s] for %s event",
3824*0Sstevel@tonic-gate 			ddi_driver_name(self), ddi_get_instance(self),
3825*0Sstevel@tonic-gate 			DR_AP_ID, EC_DR);
3826*0Sstevel@tonic-gate 		nvlist_free(ev_attr_list);
3827*0Sstevel@tonic-gate 		return;
3828*0Sstevel@tonic-gate 	}
3829*0Sstevel@tonic-gate 
3830*0Sstevel@tonic-gate 
3831*0Sstevel@tonic-gate 	/*
3832*0Sstevel@tonic-gate 	 * Log this event with sysevent framework.
3833*0Sstevel@tonic-gate 	 */
3834*0Sstevel@tonic-gate 
3835*0Sstevel@tonic-gate 	err = ddi_log_sysevent(self, DDI_VENDOR_SUNW, EC_DR,
3836*0Sstevel@tonic-gate 	    ev_subclass, ev_attr_list, &eid,
3837*0Sstevel@tonic-gate 	    ((kmflag == KM_SLEEP) ? DDI_SLEEP : DDI_NOSLEEP));
3838*0Sstevel@tonic-gate 	if (err != 0) {
3839*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: Failed to log %s event",
3840*0Sstevel@tonic-gate 			ddi_driver_name(self), ddi_get_instance(self), EC_DR);
3841*0Sstevel@tonic-gate 	}
3842*0Sstevel@tonic-gate 
3843*0Sstevel@tonic-gate 	nvlist_free(ev_attr_list);
3844*0Sstevel@tonic-gate }
3845*0Sstevel@tonic-gate 
3846*0Sstevel@tonic-gate int
3847*0Sstevel@tonic-gate pcihp_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
3848*0Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp)
3849*0Sstevel@tonic-gate {
3850*0Sstevel@tonic-gate 	int pci_dev;
3851*0Sstevel@tonic-gate 
3852*0Sstevel@tonic-gate 	if (dev == DDI_DEV_T_ANY)
3853*0Sstevel@tonic-gate 		goto skip;
3854*0Sstevel@tonic-gate 
3855*0Sstevel@tonic-gate 	if (strcmp(name, "pci-occupant") == 0) {
3856*0Sstevel@tonic-gate 		pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(getminor(dev));
3857*0Sstevel@tonic-gate 		pcihp_create_occupant_props_nolock(dip, dev, pci_dev);
3858*0Sstevel@tonic-gate 	}
3859*0Sstevel@tonic-gate 	/* other cases... */
3860*0Sstevel@tonic-gate skip:
3861*0Sstevel@tonic-gate 	return (ddi_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp));
3862*0Sstevel@tonic-gate }
3863*0Sstevel@tonic-gate 
3864*0Sstevel@tonic-gate /*
3865*0Sstevel@tonic-gate  * this function is called only for SPARC platforms, where we may have
3866*0Sstevel@tonic-gate  * a mix n' match of direct vs indirectly mapped configuration space.
3867*0Sstevel@tonic-gate  * On x86, this function should always return success since the configuration
3868*0Sstevel@tonic-gate  * space is always indirect mapped.
3869*0Sstevel@tonic-gate  */
3870*0Sstevel@tonic-gate /*ARGSUSED*/
3871*0Sstevel@tonic-gate static int
3872*0Sstevel@tonic-gate pcihp_indirect_map(dev_info_t *dip)
3873*0Sstevel@tonic-gate {
3874*0Sstevel@tonic-gate #if defined(__sparc)
3875*0Sstevel@tonic-gate 	int rc = DDI_FAILURE;
3876*0Sstevel@tonic-gate 
3877*0Sstevel@tonic-gate 	if (ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(dip), 0,
3878*0Sstevel@tonic-gate 			PCICFG_DEV_CONF_MAP_PROP, DDI_FAILURE) != DDI_FAILURE)
3879*0Sstevel@tonic-gate 		rc = DDI_SUCCESS;
3880*0Sstevel@tonic-gate 	else
3881*0Sstevel@tonic-gate 		if (ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(dip),
3882*0Sstevel@tonic-gate 				0, PCICFG_BUS_CONF_MAP_PROP,
3883*0Sstevel@tonic-gate 				DDI_FAILURE) != DDI_FAILURE)
3884*0Sstevel@tonic-gate 			rc = DDI_SUCCESS;
3885*0Sstevel@tonic-gate 	return (rc);
3886*0Sstevel@tonic-gate #else
3887*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
3888*0Sstevel@tonic-gate #endif
3889*0Sstevel@tonic-gate }
3890