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