xref: /titanic_41/usr/src/uts/common/io/hotplug/pcihp/pcihp.c (revision 9113a79cf228b8f7bd509b1328adf88659dfe218)
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 	struct hpc_led_info led_info;
2307 
2308 	/*
2309 	 * Get the soft state structure.
2310 	 */
2311 	if (pcihp_info(NULL, DDI_INFO_DEVT2DEVINFO, (void *)ap_dev,
2312 	    (void **)&self) != DDI_SUCCESS)
2313 		return (ENXIO);
2314 
2315 	pcihp_p = pcihp_get_soft_state(self, PCIHP_DR_SLOT_ENTER, &rval);
2316 	ASSERT(pcihp_p != NULL);
2317 
2318 	if (rval == PCIHP_FAILURE) {
2319 		PCIHP_DEBUG((CE_WARN, "pcihp instance is unconfigured"
2320 		    " while slot activity is requested\n"));
2321 		return (-1);
2322 	}
2323 
2324 	/* get the PCI device number for the slot */
2325 	pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(getminor(ap_dev));
2326 
2327 	slotinfop = &pcihp_p->slotinfo[pci_dev];
2328 
2329 	/*
2330 	 * All the events that may be handled in interrupt context should be
2331 	 * free of any mutex usage.
2332 	 */
2333 	switch (event_mask) {
2334 
2335 	case HPC_EVENT_CLEAR_ENUM:
2336 		/*
2337 		 * Check and clear ENUM# interrupt status. This may be
2338 		 * called by the Hotswap controller driver when it is
2339 		 * operating in a full hotswap system where the
2340 		 * platform may not have control on globally disabling ENUM#.
2341 		 * In such cases, the intent is to clear interrupt and
2342 		 * process the interrupt in non-interrupt context.
2343 		 * This is the first part of the ENUM# event processing.
2344 		 */
2345 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): ENUM# is generated"
2346 		    " on the bus (for slot %s ?)",
2347 		    ddi_driver_name(pcihp_p->dip),
2348 		    ddi_get_instance(pcihp_p->dip), slotinfop->name));
2349 
2350 		/* this is the only event coming through in interrupt context */
2351 		rv = pcihp_handle_enum(pcihp_p, pci_dev, PCIHP_CLEAR_ENUM,
2352 		    KM_NOSLEEP);
2353 
2354 		(void) pcihp_get_soft_state(self, PCIHP_DR_SLOT_EXIT, &rval);
2355 
2356 		return (rv);
2357 	default:
2358 		break;
2359 	}
2360 
2361 	mutex_enter(&slotinfop->slot_mutex);
2362 
2363 	if (hpc_nexus_control(slotinfop->slot_hdl,
2364 	    HPC_CTRL_GET_SLOT_STATE, (caddr_t)&rstate) != 0)
2365 		rv = HPC_ERR_FAILED;
2366 
2367 	slotinfop->rstate = (ap_rstate_t)rstate;
2368 
2369 	switch (event_mask) {
2370 
2371 	case HPC_EVENT_SLOT_INSERTION:
2372 		/*
2373 		 * A card is inserted in the slot. Just report this
2374 		 * event and return.
2375 		 */
2376 		cmn_err(CE_NOTE, "pcihp (%s%d): card is inserted"
2377 			" in the slot %s (pci dev %x)",
2378 			ddi_driver_name(pcihp_p->dip),
2379 			ddi_get_instance(pcihp_p->dip),
2380 			slotinfop->name, pci_dev);
2381 
2382 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2383 
2384 		break;
2385 
2386 	case HPC_EVENT_SLOT_CONFIGURE:
2387 		/*
2388 		 * Configure the occupant that is just inserted in the slot.
2389 		 * The receptacle may or may not be in the connected state. If
2390 		 * the receptacle is not connected and the auto configuration
2391 		 * is enabled on this slot then connect the slot. If auto
2392 		 * configuration is enabled then configure the card.
2393 		 */
2394 		if ((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) == 0) {
2395 			/*
2396 			 * auto configuration is disabled. Tell someone
2397 			 * like RCM about this hotplug event?
2398 			 */
2399 			cmn_err(CE_NOTE, "pcihp (%s%d): SLOT_CONFIGURE event"
2400 				" occurred for pci dev %x (slot %s),"
2401 				" Slot disabled for auto-configuration.",
2402 				ddi_driver_name(pcihp_p->dip),
2403 				ddi_get_instance(pcihp_p->dip), pci_dev,
2404 				slotinfop->name);
2405 
2406 			/* +++ HOOK for RCM to report this hotplug event? +++ */
2407 
2408 			break;
2409 		}
2410 
2411 		if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {
2412 			cmn_err(CE_WARN, "pcihp (%s%d): SLOT_CONFIGURE event"
2413 				" re-occurred for pci dev %x (slot %s),",
2414 				ddi_driver_name(pcihp_p->dip),
2415 				ddi_get_instance(pcihp_p->dip), pci_dev,
2416 				slotinfop->name);
2417 			mutex_exit(&slotinfop->slot_mutex);
2418 
2419 			(void) pcihp_get_soft_state(self, PCIHP_DR_SLOT_EXIT,
2420 			    &rval);
2421 
2422 			return (EAGAIN);
2423 		}
2424 
2425 		/*
2426 		 * Auto configuration is enabled. First, make sure the
2427 		 * receptacle is in the CONNECTED state.
2428 		 */
2429 		if ((rv = hpc_nexus_connect(slotinfop->slot_hdl,
2430 		    NULL, 0)) == HPC_SUCCESS) {
2431 		    slotinfop->rstate = AP_RSTATE_CONNECTED; /* record rstate */
2432 		}
2433 
2434 		/* Clear INS and Turn LED Off and start configuring. */
2435 		if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
2436 			pcihp_hs_csr_op(pcihp_p, pci_dev,
2437 				HPC_EVENT_SLOT_CONFIGURE);
2438 			if (pcihp_cpci_blue_led)
2439 				pcihp_hs_csr_op(pcihp_p, pci_dev,
2440 					HPC_EVENT_SLOT_BLUE_LED_OFF);
2441 		}
2442 
2443 		(void) hpc_nexus_control(slotinfop->slot_hdl,
2444 			HPC_CTRL_DEV_CONFIG_START, NULL);
2445 
2446 		/*
2447 		 * Call the configurator to configure the card.
2448 		 */
2449 		if (pcicfg_configure(pcihp_p->dip, pci_dev) != PCICFG_SUCCESS) {
2450 			if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
2451 				if (pcihp_cpci_blue_led)
2452 					pcihp_hs_csr_op(pcihp_p, pci_dev,
2453 						HPC_EVENT_SLOT_BLUE_LED_ON);
2454 				pcihp_hs_csr_op(pcihp_p, pci_dev,
2455 					HPC_EVENT_SLOT_UNCONFIGURE);
2456 			}
2457 			/* failed to configure the card */
2458 			cmn_err(CE_WARN, "pcihp (%s%d): failed to configure"
2459 				" the card in the slot %s",
2460 				ddi_driver_name(pcihp_p->dip),
2461 				ddi_get_instance(pcihp_p->dip),
2462 				slotinfop->name);
2463 			/* failed to configure; disconnect the slot */
2464 			if (hpc_nexus_disconnect(slotinfop->slot_hdl,
2465 			    NULL, 0) == HPC_SUCCESS) {
2466 			    slotinfop->rstate = AP_RSTATE_DISCONNECTED;
2467 			}
2468 
2469 			/* tell HPC driver occupant configure Error */
2470 			(void) hpc_nexus_control(slotinfop->slot_hdl,
2471 				HPC_CTRL_DEV_CONFIG_FAILURE, NULL);
2472 		} else {
2473 			/* record the occupant state as CONFIGURED */
2474 			slotinfop->ostate = AP_OSTATE_CONFIGURED;
2475 			slotinfop->condition = AP_COND_OK;
2476 
2477 			/* now, online all the devices in the AP */
2478 			ctrl.flags = PCIHP_CFG_CONTINUE;
2479 			ctrl.rv = NDI_SUCCESS;
2480 			ctrl.dip = NULL;
2481 			ctrl.pci_dev = pci_dev;
2482 			ctrl.op = PCIHP_ONLINE;
2483 				(void) pcihp_get_board_type(slotinfop);
2484 
2485 			ndi_devi_enter(pcihp_p->dip, &circular_count);
2486 			ddi_walk_devs(ddi_get_child(pcihp_p->dip),
2487 				pcihp_configure, (void *)&ctrl);
2488 			ndi_devi_exit(pcihp_p->dip, circular_count);
2489 
2490 			if (ctrl.rv != NDI_SUCCESS) {
2491 				/*
2492 				 * one or more of the devices are not
2493 				 * ONLINE'd. How is this to be
2494 				 * reported?
2495 				 */
2496 				cmn_err(CE_WARN,
2497 					"pcihp (%s%d): failed to attach one or"
2498 					" more drivers for the card in"
2499 					" the slot %s",
2500 					ddi_driver_name(pcihp_p->dip),
2501 					ddi_get_instance(pcihp_p->dip),
2502 					slotinfop->name);
2503 			}
2504 
2505 			/* tell HPC driver that the occupant is configured */
2506 			(void) hpc_nexus_control(slotinfop->slot_hdl,
2507 				HPC_CTRL_DEV_CONFIGURED, NULL);
2508 
2509 			cmn_err(CE_NOTE, "pcihp (%s%d): card is CONFIGURED"
2510 				" in the slot %s (pci dev %x)",
2511 				ddi_driver_name(pcihp_p->dip),
2512 				ddi_get_instance(pcihp_p->dip),
2513 				slotinfop->name, pci_dev);
2514 		}
2515 
2516 		break;
2517 
2518 	case HPC_EVENT_SLOT_UNCONFIGURE:
2519 		/*
2520 		 * Unconfigure the occupant in this slot.
2521 		 */
2522 		if ((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) == 0) {
2523 			/*
2524 			 * auto configuration is disabled. Tell someone
2525 			 * like RCM about this hotplug event?
2526 			 */
2527 			cmn_err(CE_NOTE, "pcihp (%s%d): SLOT_UNCONFIGURE event"
2528 				" for pci dev %x (slot %s) ignored,"
2529 				" Slot disabled for auto-configuration.",
2530 				ddi_driver_name(pcihp_p->dip),
2531 				ddi_get_instance(pcihp_p->dip), pci_dev,
2532 				slotinfop->name);
2533 
2534 			/* +++ HOOK for RCM to report this hotplug event? +++ */
2535 
2536 			break;
2537 		}
2538 
2539 		if (slotinfop->ostate == AP_OSTATE_UNCONFIGURED) {
2540 			cmn_err(CE_WARN, "pcihp (%s%d): SLOT_UNCONFIGURE "
2541 				"event re-occurred for pci dev %x (slot %s),",
2542 				ddi_driver_name(pcihp_p->dip),
2543 				ddi_get_instance(pcihp_p->dip), pci_dev,
2544 				slotinfop->name);
2545 			mutex_exit(&slotinfop->slot_mutex);
2546 
2547 			(void) pcihp_get_soft_state(self, PCIHP_DR_SLOT_EXIT,
2548 			    &rval);
2549 
2550 			return (EAGAIN);
2551 		}
2552 		/*
2553 		 * If the occupant is in the CONFIGURED state then
2554 		 * call the configurator to unconfigure the slot.
2555 		 */
2556 		if (slotinfop->ostate == AP_OSTATE_CONFIGURED) {
2557 			/*
2558 			 * Detach all the drivers for the devices in the
2559 			 * slot. Call pcihp_configure() to offline the
2560 			 * devices.
2561 			 */
2562 			ctrl.flags = 0;
2563 			ctrl.rv = NDI_SUCCESS;
2564 			ctrl.dip = NULL;
2565 			ctrl.pci_dev = pci_dev;
2566 			ctrl.op = PCIHP_OFFLINE;
2567 
2568 			(void) devfs_clean(pcihp_p->dip, NULL, DV_CLEAN_FORCE);
2569 			ndi_devi_enter(pcihp_p->dip, &circular_count);
2570 			ddi_walk_devs(ddi_get_child(pcihp_p->dip),
2571 				pcihp_configure, (void *)&ctrl);
2572 			ndi_devi_exit(pcihp_p->dip, circular_count);
2573 
2574 			if (ctrl.rv != NDI_SUCCESS) {
2575 				/*
2576 				 * Failed to detach one or more drivers.
2577 				 * Restore the status for the drivers
2578 				 * which are offlined during this step.
2579 				 */
2580 				ctrl.flags = PCIHP_CFG_CONTINUE;
2581 				ctrl.rv = NDI_SUCCESS;
2582 				ctrl.dip = NULL;
2583 				ctrl.pci_dev = pci_dev;
2584 				ctrl.op = PCIHP_ONLINE;
2585 
2586 				ndi_devi_enter(pcihp_p->dip, &circular_count);
2587 				ddi_walk_devs(ddi_get_child(pcihp_p->dip),
2588 					pcihp_configure, (void *)&ctrl);
2589 				ndi_devi_exit(pcihp_p->dip, circular_count);
2590 				rv = HPC_ERR_FAILED;
2591 			} else {
2592 				(void) hpc_nexus_control(slotinfop->slot_hdl,
2593 					HPC_CTRL_DEV_UNCONFIG_START, NULL);
2594 
2595 				if (pcicfg_unconfigure(pcihp_p->dip,
2596 						pci_dev) == PCICFG_SUCCESS) {
2597 
2598 				/* Resources freed. Turn LED on. Clear EXT. */
2599 				if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
2600 					if (pcihp_cpci_blue_led)
2601 						pcihp_hs_csr_op(pcihp_p,
2602 						pci_dev,
2603 						HPC_EVENT_SLOT_BLUE_LED_ON);
2604 					pcihp_hs_csr_op(pcihp_p, pci_dev,
2605 						HPC_EVENT_SLOT_UNCONFIGURE);
2606 					slotinfop->hs_csr_location = 0;
2607 					slotinfop->slot_flags &=
2608 						~PCIHP_SLOT_DEV_NON_HOTPLUG;
2609 				}
2610 					slotinfop->ostate =
2611 						AP_OSTATE_UNCONFIGURED;
2612 					slotinfop->condition = AP_COND_UNKNOWN;
2613 					/*
2614 					 * send the notification of state change
2615 					 * to the HPC driver.
2616 					 */
2617 					(void) hpc_nexus_control(
2618 						slotinfop->slot_hdl,
2619 						HPC_CTRL_DEV_UNCONFIGURED,
2620 						NULL);
2621 					/* disconnect the slot */
2622 					if (hpc_nexus_disconnect(
2623 						slotinfop->slot_hdl,
2624 						NULL, 0) == HPC_SUCCESS) {
2625 							slotinfop->rstate =
2626 							AP_RSTATE_DISCONNECTED;
2627 					}
2628 
2629 					cmn_err(CE_NOTE,
2630 					"pcihp (%s%d): card is UNCONFIGURED"
2631 						" in the slot %s (pci dev %x)",
2632 						ddi_driver_name(pcihp_p->dip),
2633 						ddi_get_instance(pcihp_p->dip),
2634 						slotinfop->name, pci_dev);
2635 				} else {
2636 					/* tell HPC driver occupant is Busy */
2637 					(void) hpc_nexus_control(
2638 						slotinfop->slot_hdl,
2639 						HPC_CTRL_DEV_UNCONFIG_FAILURE,
2640 						NULL);
2641 
2642 					rv = HPC_ERR_FAILED;
2643 				}
2644 			}
2645 		}
2646 
2647 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2648 
2649 		break;
2650 
2651 	case HPC_EVENT_SLOT_REMOVAL:
2652 		/*
2653 		 * Card is removed from the slot. The card must have been
2654 		 * unconfigured before this event.
2655 		 */
2656 		if (slotinfop->ostate != AP_OSTATE_UNCONFIGURED) {
2657 			slotinfop->condition = AP_COND_FAILED;
2658 			cmn_err(CE_WARN, "pcihp (%s%d): card is removed"
2659 			    " from the slot %s",
2660 			    ddi_driver_name(pcihp_p->dip),
2661 			    ddi_get_instance(pcihp_p->dip),
2662 			    slotinfop->name);
2663 		} else {
2664 			slotinfop->condition = AP_COND_UNKNOWN;
2665 			cmn_err(CE_NOTE, "pcihp (%s%d): card is removed"
2666 			    " from the slot %s",
2667 			    ddi_driver_name(pcihp_p->dip),
2668 			    ddi_get_instance(pcihp_p->dip),
2669 			    slotinfop->name);
2670 		}
2671 
2672 		slotinfop->rstate = AP_RSTATE_EMPTY;
2673 
2674 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2675 
2676 		break;
2677 
2678 	case HPC_EVENT_SLOT_POWER_ON:
2679 		/*
2680 		 * Slot is connected to the bus. i.e the card is powered
2681 		 * on. Are there any error conditions to be checked?
2682 		 */
2683 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): card is powered"
2684 			" on in the slot %s",
2685 			ddi_driver_name(pcihp_p->dip),
2686 			ddi_get_instance(pcihp_p->dip),
2687 			slotinfop->name));
2688 
2689 		slotinfop->rstate = AP_RSTATE_CONNECTED; /* record rstate */
2690 
2691 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2692 
2693 		break;
2694 
2695 	case HPC_EVENT_SLOT_POWER_OFF:
2696 		/*
2697 		 * Slot is disconnected from the bus. i.e the card is powered
2698 		 * off. Are there any error conditions to be checked?
2699 		 */
2700 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): card is powered"
2701 			" off in the slot %s",
2702 			ddi_driver_name(pcihp_p->dip),
2703 			ddi_get_instance(pcihp_p->dip),
2704 			slotinfop->name));
2705 
2706 		slotinfop->rstate = AP_RSTATE_DISCONNECTED; /* record rstate */
2707 
2708 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2709 
2710 		break;
2711 
2712 	case HPC_EVENT_SLOT_LATCH_SHUT:
2713 		/*
2714 		 * Latch on the slot is closed.
2715 		 */
2716 		cmn_err(CE_NOTE, "pcihp (%s%d): latch is shut"
2717 			" for the slot %s",
2718 			ddi_driver_name(pcihp_p->dip),
2719 			ddi_get_instance(pcihp_p->dip),
2720 			slotinfop->name);
2721 
2722 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2723 
2724 		break;
2725 
2726 	case HPC_EVENT_SLOT_LATCH_OPEN:
2727 		/*
2728 		 * Latch on the slot is open.
2729 		 */
2730 		cmn_err(CE_NOTE, "pcihp (%s%d): latch is open"
2731 			" for the slot %s",
2732 			ddi_driver_name(pcihp_p->dip),
2733 			ddi_get_instance(pcihp_p->dip),
2734 			slotinfop->name);
2735 
2736 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2737 
2738 		break;
2739 
2740 	case HPC_EVENT_PROCESS_ENUM:
2741 		/*
2742 		 * HSC knows the device number of the slot where the
2743 		 * ENUM# was triggered.
2744 		 * Now finish the necessary actions to be taken on that
2745 		 * slot. Please note that the interrupt is already cleared.
2746 		 * This is the second(last) part of the ENUM# event processing.
2747 		 */
2748 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): processing ENUM#"
2749 			" for slot %s",
2750 			ddi_driver_name(pcihp_p->dip),
2751 			ddi_get_instance(pcihp_p->dip),
2752 			slotinfop->name));
2753 
2754 		mutex_exit(&slotinfop->slot_mutex);
2755 		rv = pcihp_enum_slot(pcihp_p, slotinfop, pci_dev,
2756 			PCIHP_HANDLE_ENUM, KM_SLEEP);
2757 		mutex_enter(&slotinfop->slot_mutex);
2758 
2759 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2760 
2761 		break;
2762 
2763 	case HPC_EVENT_BUS_ENUM:
2764 		/*
2765 		 * Same as HPC_EVENT_SLOT_ENUM as defined the PSARC doc.
2766 		 * This term is used for better clarity of its usage.
2767 		 *
2768 		 * ENUM signal occurred on the bus. It may be from this
2769 		 * slot or any other hotplug slot on the bus.
2770 		 *
2771 		 * It is NOT recommended that the hotswap controller uses
2772 		 * event without queuing as NDI and other DDI calls may not
2773 		 * necessarily be invokable in interrupt context.
2774 		 * Hence the hotswap controller driver should use the
2775 		 * CLEAR_ENUM event which returns the slot device number
2776 		 * and then call HPC_EVENT_PROCESS_ENUM event with queuing.
2777 		 *
2778 		 * This can be used when the hotswap controller is
2779 		 * implementing a polled event mechanism to do the
2780 		 * necessary actions in a single call.
2781 		 */
2782 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): ENUM# is generated"
2783 		    " on the bus (for slot %s ?)",
2784 		    ddi_driver_name(pcihp_p->dip),
2785 		    ddi_get_instance(pcihp_p->dip),
2786 		    slotinfop->name));
2787 
2788 		mutex_exit(&slotinfop->slot_mutex);
2789 		rv = pcihp_handle_enum(pcihp_p, pci_dev, PCIHP_HANDLE_ENUM,
2790 			KM_SLEEP);
2791 		mutex_enter(&slotinfop->slot_mutex);
2792 
2793 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2794 
2795 		break;
2796 
2797 	case HPC_EVENT_SLOT_BLUE_LED_ON:
2798 
2799 		/*
2800 		 * Request to turn Hot Swap Blue LED on.
2801 		 */
2802 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): Request To Turn On Blue "
2803 		    "LED on the bus (for slot %s ?)",
2804 		    ddi_driver_name(pcihp_p->dip),
2805 		    ddi_get_instance(pcihp_p->dip),
2806 		    slotinfop->name));
2807 
2808 		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_SLOT_BLUE_LED_ON);
2809 		break;
2810 
2811 	case HPC_EVENT_DISABLE_ENUM:
2812 		/*
2813 		 * Disable ENUM# which disables auto configuration on this slot
2814 		 */
2815 		if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
2816 			pcihp_hs_csr_op(pcihp_p, pci_dev,
2817 				HPC_EVENT_DISABLE_ENUM);
2818 			slotinfop->slot_flags &= ~PCIHP_SLOT_AUTO_CFG_EN;
2819 		}
2820 		break;
2821 
2822 	case HPC_EVENT_ENABLE_ENUM:
2823 		/*
2824 		 * Enable ENUM# which enables auto configuration on this slot.
2825 		 */
2826 		if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
2827 			pcihp_hs_csr_op(pcihp_p, pci_dev,
2828 				HPC_EVENT_ENABLE_ENUM);
2829 			slotinfop->slot_flags |= PCIHP_SLOT_AUTO_CFG_EN;
2830 		}
2831 		break;
2832 
2833 	case HPC_EVENT_SLOT_BLUE_LED_OFF:
2834 
2835 		/*
2836 		 * Request to turn Hot Swap Blue LED off.
2837 		 */
2838 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): Request To Turn Off Blue "
2839 		    "LED on the bus (for slot %s ?)",
2840 		    ddi_driver_name(pcihp_p->dip),
2841 		    ddi_get_instance(pcihp_p->dip),
2842 		    slotinfop->name));
2843 
2844 		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_SLOT_BLUE_LED_OFF);
2845 
2846 		break;
2847 
2848 	case HPC_EVENT_SLOT_NOT_HEALTHY:
2849 		/*
2850 		 * HEALTHY# signal on this slot is not OK.
2851 		 */
2852 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): HEALTHY# signal is not OK"
2853 			" for this slot %s",
2854 			ddi_driver_name(pcihp_p->dip),
2855 			ddi_get_instance(pcihp_p->dip),
2856 			slotinfop->name));
2857 
2858 		/* record the state in slot_flags field */
2859 		slotinfop->slot_flags |= PCIHP_SLOT_NOT_HEALTHY;
2860 		slotinfop->condition = AP_COND_FAILED;
2861 
2862 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2863 
2864 		break;
2865 
2866 	case HPC_EVENT_SLOT_HEALTHY_OK:
2867 		/*
2868 		 * HEALTHY# signal on this slot is OK now.
2869 		 */
2870 		PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): HEALTHY# signal is OK now"
2871 			" for this slot %s",
2872 			ddi_driver_name(pcihp_p->dip),
2873 			ddi_get_instance(pcihp_p->dip),
2874 			slotinfop->name));
2875 
2876 		/* update the state in slot_flags field */
2877 		slotinfop->slot_flags &= ~PCIHP_SLOT_NOT_HEALTHY;
2878 		slotinfop->condition = AP_COND_OK;
2879 
2880 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2881 
2882 		break;
2883 
2884 	case HPC_EVENT_SLOT_ATTN:
2885 		/*
2886 		 * Attention button is pressed.
2887 		 */
2888 		if (((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) == 0) ||
2889 		    (slotinfop->slot_flags & PCIHP_SLOT_DISABLED)) {
2890 			/*
2891 			 * either auto-conifiguration or the slot is disabled,
2892 			 * ignore this event.
2893 			 */
2894 			break;
2895 		}
2896 
2897 		if (slotinfop->ostate == AP_OSTATE_UNCONFIGURED)
2898 			hint = SE_INCOMING_RES;
2899 		else
2900 			hint = SE_OUTGOING_RES;
2901 
2902 		if (ddi_getprop(DDI_DEV_T_ANY, pcihp_p->dip, DDI_PROP_DONTPASS,
2903 		    "inkernel-autoconfig", 0) == 0) {
2904 			pcihp_gen_sysevent(slotinfop->name, PCIHP_DR_REQ, hint,
2905 			    pcihp_p->dip, KM_SLEEP);
2906 			break;
2907 		}
2908 
2909 		if ((slotinfop->ostate == AP_OSTATE_UNCONFIGURED) &&
2910 		    (slotinfop->rstate != AP_RSTATE_EMPTY) &&
2911 		    (slotinfop->condition != AP_COND_FAILED)) {
2912 			if (slotinfop->rstate == AP_RSTATE_DISCONNECTED) {
2913 				rv = hpc_nexus_connect(slotinfop->slot_hdl,
2914 				    NULL, 0);
2915 				if (rv == HPC_SUCCESS)
2916 					slotinfop->rstate = AP_RSTATE_CONNECTED;
2917 				else
2918 					break;
2919 			}
2920 
2921 			rv = pcihp_configure_ap(pcihp_p, pci_dev);
2922 
2923 		} else if ((slotinfop->ostate == AP_OSTATE_CONFIGURED) &&
2924 		    (slotinfop->rstate == AP_RSTATE_CONNECTED) &&
2925 		    (slotinfop->condition != AP_COND_FAILED)) {
2926 			rv = pcihp_unconfigure_ap(pcihp_p, pci_dev);
2927 
2928 			if (rv != HPC_SUCCESS)
2929 				break;
2930 
2931 			rv = hpc_nexus_disconnect(slotinfop->slot_hdl,
2932 			    NULL, 0);
2933 			if (rv == HPC_SUCCESS)
2934 				slotinfop->rstate = AP_RSTATE_DISCONNECTED;
2935 		}
2936 
2937 		break;
2938 
2939 	case HPC_EVENT_SLOT_POWER_FAULT:
2940 		/*
2941 		 * Power fault is detected.
2942 		 */
2943 		cmn_err(CE_NOTE, "pcihp (%s%d): power-fault"
2944 		    " for this slot %s",
2945 		    ddi_driver_name(pcihp_p->dip),
2946 		    ddi_get_instance(pcihp_p->dip),
2947 		    slotinfop->name);
2948 
2949 		/* turn on ATTN led */
2950 		led_info.led = HPC_ATTN_LED;
2951 		led_info.state = HPC_LED_ON;
2952 		rv = hpc_nexus_control(slotinfop->slot_hdl,
2953 		    HPC_CTRL_SET_LED_STATE, (caddr_t)&led_info);
2954 
2955 		if (slotinfop->rstate == AP_RSTATE_CONNECTED)
2956 			(void) hpc_nexus_disconnect(slotinfop->slot_hdl,
2957 			    NULL, 0);
2958 
2959 		slotinfop->condition = AP_COND_FAILED;
2960 
2961 		pcihp_gen_sysevent(slotinfop->name, PCIHP_DR_AP_STATE_CHANGE,
2962 		    SE_NO_HINT, pcihp_p->dip, KM_SLEEP);
2963 
2964 		break;
2965 
2966 	default:
2967 		cmn_err(CE_NOTE, "pcihp (%s%d): unknown event %x"
2968 			" for this slot %s",
2969 			ddi_driver_name(pcihp_p->dip),
2970 			ddi_get_instance(pcihp_p->dip), event_mask,
2971 			slotinfop->name);
2972 
2973 		/* +++ HOOK for RCM to report this hotplug event? +++ */
2974 
2975 		break;
2976 	}
2977 
2978 	mutex_exit(&slotinfop->slot_mutex);
2979 
2980 	(void) pcihp_get_soft_state(self, PCIHP_DR_SLOT_EXIT, &rval);
2981 
2982 	return (rv);
2983 }
2984 
2985 /*
2986  * This function is called to online or offline the devices for an
2987  * attachment point. If the PCI device number of the node matches
2988  * with the device number of the specified hot plug slot then
2989  * the operation is performed.
2990  */
2991 static int
2992 pcihp_configure(dev_info_t *dip, void *hdl)
2993 {
2994 	int pci_dev;
2995 	struct pcihp_config_ctrl *ctrl = (struct pcihp_config_ctrl *)hdl;
2996 	int rv;
2997 	pci_regspec_t *pci_rp;
2998 	int length;
2999 
3000 	/*
3001 	 * Get the PCI device number information from the devinfo
3002 	 * node. Since the node may not have the address field
3003 	 * setup (this is done in the DDI_INITCHILD of the parent)
3004 	 * we look up the 'reg' property to decode that information.
3005 	 */
3006 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
3007 		DDI_PROP_DONTPASS, "reg", (int **)&pci_rp,
3008 		(uint_t *)&length) != DDI_PROP_SUCCESS) {
3009 		ctrl->rv = DDI_FAILURE;
3010 		ctrl->dip = dip;
3011 		return (DDI_WALK_TERMINATE);
3012 	}
3013 
3014 	/* get the pci device id information */
3015 	pci_dev = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
3016 
3017 	/*
3018 	 * free the memory allocated by ddi_prop_lookup_int_array
3019 	 */
3020 	ddi_prop_free(pci_rp);
3021 
3022 	/*
3023 	 * Match the node for the device number of the slot.
3024 	 */
3025 	if (pci_dev == ctrl->pci_dev) {	/* node is a match */
3026 		if (ctrl->op == PCIHP_ONLINE) {
3027 			/* it is CONFIGURE operation */
3028 			rv = ndi_devi_online(dip, NDI_ONLINE_ATTACH|NDI_CONFIG);
3029 		} else {
3030 			/*
3031 			 * it is UNCONFIGURE operation.
3032 			 */
3033 			rv = ndi_devi_offline(dip, NDI_UNCONFIG);
3034 		}
3035 		if (rv != NDI_SUCCESS) {
3036 			/* failed to attach/detach the driver(s) */
3037 			ctrl->rv = rv;
3038 			ctrl->dip = dip;
3039 			/* terminate the search if specified */
3040 			if (!(ctrl->flags & PCIHP_CFG_CONTINUE))
3041 				return (DDI_WALK_TERMINATE);
3042 		}
3043 	}
3044 
3045 	/*
3046 	 * continue the walk to the next sibling to look for a match
3047 	 * or to find other nodes if this card is a multi-function card.
3048 	 */
3049 	return (DDI_WALK_PRUNECHILD);
3050 }
3051 
3052 /* control structure used to find a device in the devinfo tree */
3053 struct pcihp_find_ctrl {
3054 	uint_t		device;
3055 	uint_t		function;
3056 	dev_info_t	*dip;
3057 };
3058 
3059 static dev_info_t *
3060 pcihp_devi_find(dev_info_t *dip, uint_t device, uint_t function)
3061 {
3062 	struct pcihp_find_ctrl ctrl;
3063 	int circular_count;
3064 
3065 	ctrl.device = device;
3066 	ctrl.function = function;
3067 	ctrl.dip = NULL;
3068 
3069 	ndi_devi_enter(dip, &circular_count);
3070 	ddi_walk_devs(ddi_get_child(dip), pcihp_match_dev, (void *)&ctrl);
3071 	ndi_devi_exit(dip, circular_count);
3072 
3073 	return (ctrl.dip);
3074 }
3075 
3076 static int
3077 pcihp_match_dev(dev_info_t *dip, void *hdl)
3078 {
3079 	struct pcihp_find_ctrl *ctrl = (struct pcihp_find_ctrl *)hdl;
3080 	pci_regspec_t *pci_rp;
3081 	int length;
3082 	int pci_dev;
3083 	int pci_func;
3084 
3085 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
3086 		DDI_PROP_DONTPASS, "reg", (int **)&pci_rp,
3087 		(uint_t *)&length) != DDI_PROP_SUCCESS) {
3088 		ctrl->dip = NULL;
3089 		return (DDI_WALK_TERMINATE);
3090 	}
3091 
3092 	/* get the PCI device address info */
3093 	pci_dev = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
3094 	pci_func = PCI_REG_FUNC_G(pci_rp->pci_phys_hi);
3095 
3096 	/*
3097 	 * free the memory allocated by ddi_prop_lookup_int_array
3098 	 */
3099 	ddi_prop_free(pci_rp);
3100 
3101 
3102 	if ((pci_dev == ctrl->device) && (pci_func == ctrl->function)) {
3103 		/* found the match for the specified device address */
3104 		ctrl->dip = dip;
3105 		return (DDI_WALK_TERMINATE);
3106 	}
3107 
3108 	/*
3109 	 * continue the walk to the next sibling to look for a match.
3110 	 */
3111 	return (DDI_WALK_PRUNECHILD);
3112 }
3113 
3114 #if 0
3115 /*
3116  * Probe the configuration space of the slot to determine the receptacle
3117  * state. There may not be any devinfo tree created for this slot.
3118  */
3119 static void
3120 pcihp_probe_slot_state(dev_info_t *dip, int dev, hpc_slot_state_t *rstatep)
3121 {
3122 	/* XXX FIX IT */
3123 }
3124 #endif
3125 
3126 /*
3127  * This routine is called when a ENUM# assertion is detected for a bus.
3128  * Since ENUM# may be bussed, the slot that asserted ENUM# may not be known.
3129  * The HPC Driver passes the handle of a slot that is its best guess.
3130  * If the best guess slot is the one that asserted ENUM#, the proper handling
3131  * will be done.  If its not, all possible slots will be locked at until
3132  * one that is asserting ENUM is found.
3133  * Also, indicate to the HSC to turn on ENUM# after it is serviced,
3134  * incase if it was disabled by the HSC due to the nature of asynchronous
3135  * delivery of interrupt by the framework.
3136  *
3137  * opcode has the following meanings.
3138  * PCIHP_CLEAR_ENUM = just clear interrupt and return the PCI device no. if
3139  *			success, else return -1.
3140  * PCIHP_HANDLE_ENUM = clear interrupt and handle interrupt also.
3141  *
3142  */
3143 static int
3144 pcihp_handle_enum(pcihp_t *pcihp_p, int favorite_pci_dev, int opcode,
3145 	int kmflag)
3146 {
3147 	struct pcihp_slotinfo *slotinfop;
3148 	int pci_dev, rc, event_serviced = 0;
3149 
3150 	/*
3151 	 * Handle ENUM# condition for the "favorite" slot first.
3152 	 */
3153 	slotinfop = &pcihp_p->slotinfo[favorite_pci_dev];
3154 	if (slotinfop) {
3155 		/*
3156 		 * First try the "favorite" pci device.  This is the device
3157 		 * associated with the handle passed by the HPC Driver.
3158 		 */
3159 		rc = pcihp_enum_slot(pcihp_p, slotinfop, favorite_pci_dev,
3160 			opcode, kmflag);
3161 		if (rc != HPC_EVENT_UNCLAIMED) {	/* indicates success */
3162 			event_serviced = 1;
3163 			/* This MUST be a non-DEBUG feature. */
3164 			if (! pcihp_enum_scan_all) {
3165 				return (rc);
3166 			}
3167 		}
3168 	}
3169 
3170 	/*
3171 	 * If ENUM# is implemented as a radial signal, then there is no
3172 	 * need to further poll the slots.
3173 	 */
3174 	if (pcihp_p->bus_flags & PCIHP_BUS_ENUM_RADIAL)
3175 		goto enum_service_check;
3176 
3177 	/*
3178 	 * If the "favorite" pci device didn't assert ENUM#, then
3179 	 * try the rest.  Once we find and handle a device that asserted
3180 	 * ENUM#, then we will terminate the walk by returning unless
3181 	 * scan-all flag is set.
3182 	 */
3183 	for (pci_dev = 0; pci_dev < PCI_MAX_DEVS; pci_dev++) {
3184 		if (pci_dev != favorite_pci_dev) {
3185 			slotinfop = &pcihp_p->slotinfo[pci_dev];
3186 			if (slotinfop == NULL) {
3187 				continue;
3188 			}
3189 			/* Only CPCI devices support ENUM# generation. */
3190 			if (!(slotinfop->slot_type & HPC_SLOT_TYPE_CPCI))
3191 				continue;
3192 			rc = pcihp_enum_slot(pcihp_p, slotinfop, pci_dev,
3193 				opcode, kmflag);
3194 			if (rc != HPC_EVENT_UNCLAIMED) {
3195 				event_serviced = 1;
3196 				/* This MUST be a non-DEBUG feature. */
3197 				if (! pcihp_enum_scan_all)
3198 					break;
3199 			}
3200 		}
3201 	}
3202 
3203 enum_service_check:
3204 	if (event_serviced) {
3205 		return (rc);
3206 	}
3207 
3208 	/* No ENUM# event found, Return */
3209 	return (HPC_EVENT_UNCLAIMED);
3210 }
3211 
3212 /*
3213  * This routine attempts to handle a possible ENUM# assertion case for a
3214  * specified slot.  This only works for adapters that implement Hot Swap
3215  * Friendly Silicon.  If the slot's HS_CSR is read and it specifies ENUM#
3216  * has been asserted, either the insertion or removal handlers will be
3217  * called.
3218  */
3219 static int
3220 pcihp_enum_slot(pcihp_t *pcihp_p, struct pcihp_slotinfo *slotinfop, int pci_dev,
3221 		int opcode, int kmflag)
3222 {
3223 	ddi_acc_handle_t handle;
3224 	dev_info_t *dip, *new_child = NULL;
3225 	int result, rv = -1;
3226 	uint8_t hs_csr;
3227 
3228 	if (pcihp_config_setup(&dip, &handle, &new_child, pci_dev,
3229 				pcihp_p) != DDI_SUCCESS) {
3230 		return (HPC_EVENT_UNCLAIMED);
3231 	}
3232 
3233 	/*
3234 	 * Read the device's HS_CSR.
3235 	 */
3236 	result = pcihp_get_hs_csr(slotinfop, handle, (uint8_t *)&hs_csr);
3237 	PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): hs_csr = %x, flags = %x",
3238 		ddi_driver_name(pcihp_p->dip), ddi_get_instance(pcihp_p->dip),
3239 		hs_csr, slotinfop->slot_flags));
3240 	/*
3241 	 * we teardown our device map here, because in case of an
3242 	 * extraction event, our nodes would be freed and a teardown
3243 	 * will cause problems.
3244 	 */
3245 	pcihp_config_teardown(&handle, &new_child, pci_dev, pcihp_p);
3246 
3247 	if (result == PCIHP_SUCCESS) {
3248 
3249 		/* If ENUM# is masked, then it is not us. Some other device */
3250 		if ((hs_csr & HS_CSR_EIM) && (opcode == PCIHP_CLEAR_ENUM))
3251 			return (HPC_EVENT_UNCLAIMED);
3252 		/*
3253 		 * This device supports Full Hot Swap and implements
3254 		 * the Hot Swap Control and Status Register.
3255 		 */
3256 		if ((hs_csr & HS_CSR_INS) ||
3257 			(slotinfop->slot_flags & PCIHP_SLOT_ENUM_INS_PENDING)) {
3258 			/* handle insertion ENUM */
3259 			PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): "
3260 			    "Handle Insertion ENUM (INS) "
3261 			    "on the bus (for slot %s ?)",
3262 			    ddi_driver_name(pcihp_p->dip),
3263 			    ddi_get_instance(pcihp_p->dip),
3264 			    slotinfop->name));
3265 
3266 			/*
3267 			 * generate sysevent
3268 			 */
3269 
3270 			if (opcode == PCIHP_CLEAR_ENUM)
3271 				pcihp_gen_sysevent(slotinfop->name,
3272 					PCIHP_DR_REQ,
3273 					SE_INCOMING_RES, pcihp_p->dip,
3274 					kmflag);
3275 
3276 			rv = pcihp_handle_enum_insertion(pcihp_p, pci_dev,
3277 				opcode, kmflag);
3278 
3279 		} else if ((hs_csr & HS_CSR_EXT) || (slotinfop->slot_flags &
3280 					PCIHP_SLOT_ENUM_EXT_PENDING)) {
3281 			/* handle extraction ENUM */
3282 			PCIHP_DEBUG((CE_NOTE, "pcihp (%s%d): "
3283 			    "Handle Extraction ENUM (EXT) "
3284 			    "on the bus (for slot %s ?)",
3285 			    ddi_driver_name(pcihp_p->dip),
3286 			    ddi_get_instance(pcihp_p->dip),
3287 			    slotinfop->name));
3288 
3289 			/*
3290 			 * generate sysevent
3291 			 */
3292 
3293 			if (opcode == PCIHP_CLEAR_ENUM)
3294 				pcihp_gen_sysevent(slotinfop->name,
3295 					PCIHP_DR_REQ,
3296 					SE_OUTGOING_RES,
3297 					pcihp_p->dip,
3298 					kmflag);
3299 
3300 			rv = pcihp_handle_enum_extraction(pcihp_p, pci_dev,
3301 				opcode, kmflag);
3302 		}
3303 		if (opcode == PCIHP_CLEAR_ENUM) {
3304 			if (rv == PCIHP_SUCCESS)
3305 				rv = pci_dev;
3306 			else
3307 				rv = HPC_EVENT_UNCLAIMED;
3308 		}
3309 	}
3310 
3311 	return (rv);
3312 }
3313 
3314 /*
3315  * This routine is called when a ENUM# caused by lifting the lever
3316  * is detected.  If the occupant is configured, it will be unconfigured.
3317  * If the occupant is already unconfigured or is successfully unconfigured,
3318  * the blue LED on the adapter is illuminated which means its OK to remove.
3319  * Please note that the lock must be released before invoking the
3320  * generic AP unconfigure function.
3321  */
3322 static int
3323 pcihp_handle_enum_extraction(pcihp_t *pcihp_p, int pci_dev, int opcode,
3324 	int kmflag)
3325 {
3326 	struct pcihp_slotinfo *slotinfop;
3327 	int rv = PCIHP_FAILURE;
3328 
3329 	slotinfop = &pcihp_p->slotinfo[pci_dev];
3330 
3331 	/*
3332 	 * It was observed that, clearing the EXT bit turned the LED ON.
3333 	 * This is a BIG problem in case if the unconfigure operation
3334 	 * failed because the board was busy.
3335 	 * In order to avoid this confusing situation (LED ON but the board
3336 	 * is not unconfigured), we instead decided not to clear EXT but
3337 	 * disable further ENUM# from this slot. Disabling ENUM# clears
3338 	 * the interrupt.
3339 	 * Finally before returning we clear the interrupt and enable
3340 	 * ENUM# back again from this slot.
3341 	 */
3342 	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_DISABLE_ENUM);
3343 	if (opcode == PCIHP_CLEAR_ENUM) {
3344 		slotinfop->slot_flags |= PCIHP_SLOT_ENUM_EXT_PENDING;
3345 		return (PCIHP_SUCCESS);
3346 	}
3347 
3348 	mutex_enter(&slotinfop->slot_mutex);
3349 	rv = pcihp_unconfigure_ap(pcihp_p, pci_dev);
3350 	mutex_exit(&slotinfop->slot_mutex);
3351 	if (rv != HPC_SUCCESS && rv != EBUSY) {
3352 		cmn_err(CE_NOTE, "%s%d: PCI device %x Failed on Unconfigure",
3353 		    ddi_driver_name(pcihp_p->dip),
3354 		    ddi_get_instance(pcihp_p->dip), pci_dev);
3355 	}
3356 	if (rv == EBUSY)
3357 		cmn_err(CE_NOTE, "%s%d: PCI device %x Busy",
3358 		    ddi_driver_name(pcihp_p->dip),
3359 		    ddi_get_instance(pcihp_p->dip), pci_dev);
3360 	if (rv) {
3361 		if (pcihp_cpci_blue_led)
3362 			pcihp_hs_csr_op(pcihp_p, pci_dev,
3363 					HPC_EVENT_SLOT_BLUE_LED_OFF);
3364 	}
3365 	/*
3366 	 * we must clear interrupt in case the unconfigure didn't do it
3367 	 * due to a duplicate interrupt. Extraction is success.
3368 	 */
3369 	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_SLOT_UNCONFIGURE);
3370 
3371 	if (!rv) {
3372 		/*
3373 		 * Sys Event Notification.
3374 		 */
3375 		pcihp_gen_sysevent(slotinfop->name, PCIHP_DR_AP_STATE_CHANGE,
3376 			SE_HINT_REMOVE, pcihp_p->dip, kmflag);
3377 	}
3378 
3379 	/*
3380 	 * Enable interrupts back from this board.
3381 	 * This could potentially be problematic in case if the user is
3382 	 * quick enough to extract the board.
3383 	 * But we must do it just in case if the switch is closed again.
3384 	 */
3385 	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_ENABLE_ENUM);
3386 	slotinfop->slot_flags &= ~PCIHP_SLOT_ENUM_EXT_PENDING;
3387 	return (rv);
3388 }
3389 
3390 /*
3391  * This routine is called when a ENUM# caused by when an adapter insertion
3392  * is detected.  If the occupant is successfully configured (i.e. PCI resources
3393  * successfully assigned, the blue LED is left off, otherwise if configuration
3394  * is not successful, the blue LED is illuminated.
3395  * Please note that the lock must be released before invoking the
3396  * generic AP configure function.
3397  */
3398 static int
3399 pcihp_handle_enum_insertion(pcihp_t *pcihp_p, int pci_dev, int opcode,
3400 	int kmflag)
3401 {
3402 	struct pcihp_slotinfo *slotinfop;
3403 	int rv = PCIHP_FAILURE;
3404 	minor_t ap_minor;
3405 	major_t ap_major;
3406 
3407 	slotinfop = &pcihp_p->slotinfo[pci_dev];
3408 	slotinfop->hs_csr_location = 0;
3409 	/* we clear the interrupt here. This is a must here. */
3410 	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_SLOT_CONFIGURE);
3411 	/*
3412 	 * disable further interrupt from this board till it is
3413 	 * configured.
3414 	 */
3415 	pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_DISABLE_ENUM);
3416 	if (opcode == PCIHP_CLEAR_ENUM) {
3417 		slotinfop->slot_flags |= PCIHP_SLOT_ENUM_INS_PENDING;
3418 		return (PCIHP_SUCCESS);
3419 	}
3420 
3421 	if ((slotinfop->slot_flags & PCIHP_SLOT_AUTO_CFG_EN) ==
3422 					PCIHP_SLOT_AUTO_CFG_EN) {
3423 		mutex_enter(&slotinfop->slot_mutex);
3424 		rv = pcihp_configure_ap(pcihp_p, pci_dev);
3425 		mutex_exit(&slotinfop->slot_mutex);
3426 		if (rv != HPC_SUCCESS) {	/* configure failed */
3427 			cmn_err(CE_NOTE, "%s%d: PCI device %x Failed on"
3428 				" Configure", ddi_driver_name(pcihp_p->dip),
3429 				ddi_get_instance(pcihp_p->dip), pci_dev);
3430 			if (pcihp_cpci_blue_led)
3431 				pcihp_hs_csr_op(pcihp_p, pci_dev,
3432 						HPC_EVENT_SLOT_BLUE_LED_ON);
3433 		}
3434 
3435 		/* pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_CLEAR_ENUM); */
3436 		pcihp_hs_csr_op(pcihp_p, pci_dev, HPC_EVENT_ENABLE_ENUM);
3437 
3438 		if (!rv) {
3439 			ap_major = ddi_driver_major(pcihp_p->dip);
3440 			ap_minor = PCIHP_AP_MINOR_NUM(
3441 			    ddi_get_instance(pcihp_p->dip), pci_dev);
3442 			pcihp_create_occupant_props(pcihp_p->dip,
3443 			    makedevice(ap_major, ap_minor), pci_dev);
3444 
3445 			/*
3446 			 * Sys Event Notification.
3447 			 */
3448 			pcihp_gen_sysevent(slotinfop->name,
3449 				PCIHP_DR_AP_STATE_CHANGE,
3450 				SE_HINT_INSERT, pcihp_p->dip, kmflag);
3451 		}
3452 
3453 	} else
3454 		rv = PCIHP_SUCCESS;
3455 	slotinfop->slot_flags &= ~PCIHP_SLOT_ENUM_INS_PENDING;
3456 	return (rv);
3457 }
3458 
3459 /*
3460  * Read the Hot Swap Control and Status Register (HS_CSR) and
3461  * place the result in the location pointed to be hs_csr.
3462  */
3463 static int
3464 pcihp_get_hs_csr(struct pcihp_slotinfo *slotinfop,
3465     ddi_acc_handle_t config_handle, uint8_t *hs_csr)
3466 {
3467 	if (slotinfop->hs_csr_location == -1)
3468 		return (PCIHP_FAILURE);
3469 
3470 	if (slotinfop->hs_csr_location == 0) {
3471 		slotinfop->hs_csr_location =
3472 			pcihp_get_hs_csr_location(config_handle);
3473 
3474 		if (slotinfop->hs_csr_location == -1)
3475 			return (PCIHP_FAILURE);
3476 	}
3477 	*hs_csr = pci_config_get8(config_handle, slotinfop->hs_csr_location);
3478 	return (PCIHP_SUCCESS);
3479 }
3480 
3481 /*
3482  * Write the Hot Swap Control and Status Register (HS_CSR) with
3483  * the value being pointed at by hs_csr.
3484  */
3485 static void
3486 pcihp_set_hs_csr(struct pcihp_slotinfo *slotinfop,
3487     ddi_acc_handle_t config_handle, uint8_t *hs_csr)
3488 {
3489 	if (slotinfop->hs_csr_location == -1)
3490 		return;
3491 	if (slotinfop->hs_csr_location == 0) {
3492 		slotinfop->hs_csr_location =
3493 			pcihp_get_hs_csr_location(config_handle);
3494 		if (slotinfop->hs_csr_location == -1)
3495 			return;
3496 	}
3497 	pci_config_put8(config_handle, slotinfop->hs_csr_location, *hs_csr);
3498 	PCIHP_DEBUG((CE_NOTE, "hs_csr wrote %x, read %x", *hs_csr,
3499 		pci_config_get8(config_handle, slotinfop->hs_csr_location)));
3500 }
3501 
3502 static int
3503 pcihp_get_hs_csr_location(ddi_acc_handle_t config_handle)
3504 {
3505 	uint8_t	cap_id;
3506 	uint_t	cap_id_loc;
3507 	uint16_t	status;
3508 	int location = -1;
3509 #define	PCI_STAT_ECP_SUPP	0x10
3510 
3511 	/*
3512 	 * Need to check the Status register for ECP support first.
3513 	 * Also please note that for type 1 devices, the
3514 	 * offset could change. Should support type 1 next.
3515 	 */
3516 	status = pci_config_get16(config_handle, PCI_CONF_STAT);
3517 	if (!(status & PCI_STAT_ECP_SUPP)) {
3518 		PCIHP_DEBUG((CE_NOTE, "No Ext Capabilities for device\n"));
3519 		return (-1);
3520 	}
3521 	cap_id_loc = pci_config_get8(config_handle, PCI_CONF_EXTCAP);
3522 
3523 	/*
3524 	 * Walk the list of capabilities, but don't walk past the end
3525 	 * of the Configuration Space Header.
3526 	 */
3527 	while ((cap_id_loc) && (cap_id_loc < PCI_CONF_HDR_SIZE)) {
3528 
3529 		cap_id = pci_config_get8(config_handle, cap_id_loc);
3530 
3531 		if (cap_id == CPCI_HOTSWAP_CAPID) {
3532 			location = cap_id_loc + PCI_ECP_HS_CSR;
3533 			break;
3534 		}
3535 		cap_id_loc = pci_config_get8(config_handle,
3536 		    cap_id_loc + 1);
3537 	}
3538 	return (location);
3539 }
3540 
3541 static int
3542 pcihp_add_dummy_reg_property(dev_info_t *dip,
3543     uint_t bus, uint_t device, uint_t func)
3544 {
3545 	pci_regspec_t dummy_reg;
3546 
3547 	bzero(&dummy_reg, sizeof (dummy_reg));
3548 
3549 	dummy_reg.pci_phys_hi = PCIHP_MAKE_REG_HIGH(bus, device, func, 0);
3550 
3551 	return (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
3552 	    "reg", (int *)&dummy_reg, sizeof (pci_regspec_t)/sizeof (int)));
3553 }
3554 
3555 static void
3556 pcihp_hs_csr_op(pcihp_t *pcihp_p, int pci_dev, int event)
3557 {
3558 	struct pcihp_slotinfo *slotinfop;
3559 	ddi_acc_handle_t config_handle;
3560 	dev_info_t *dip, *new_child = NULL;
3561 	uint8_t hs_csr;
3562 	int result;
3563 
3564 	slotinfop = &pcihp_p->slotinfo[pci_dev];
3565 
3566 	if (pcihp_config_setup(&dip, &config_handle, &new_child, pci_dev,
3567 				pcihp_p) != DDI_SUCCESS) {
3568 		return;
3569 	}
3570 
3571 	result = pcihp_get_hs_csr(slotinfop, config_handle, (uint8_t *)&hs_csr);
3572 	if ((result != PCIHP_SUCCESS) || (event == -1)) {
3573 		pcihp_config_teardown(&config_handle, &new_child, pci_dev,
3574 							pcihp_p);
3575 		return;
3576 	}
3577 
3578 	hs_csr &= 0xf;
3579 	switch (event) {
3580 		case HPC_EVENT_SLOT_BLUE_LED_ON:
3581 			hs_csr |= HS_CSR_LOO;
3582 			break;
3583 		case HPC_EVENT_SLOT_BLUE_LED_OFF:
3584 			hs_csr &= ~HS_CSR_LOO;
3585 			break;
3586 		case HPC_EVENT_SLOT_CONFIGURE:
3587 			hs_csr |= HS_CSR_INS;	/* clear INS */
3588 			break;
3589 		case HPC_EVENT_CLEAR_ENUM:
3590 			hs_csr |= (HS_CSR_INS | HS_CSR_EXT);
3591 			break;
3592 		case HPC_EVENT_SLOT_UNCONFIGURE:
3593 			hs_csr |= HS_CSR_EXT;	/* clear EXT */
3594 			break;
3595 		case HPC_EVENT_ENABLE_ENUM:
3596 			hs_csr &= ~HS_CSR_EIM;
3597 			break;
3598 		case HPC_EVENT_DISABLE_ENUM:
3599 			hs_csr |= HS_CSR_EIM;
3600 			break;
3601 		case HPC_EVENT_SLOT_NOT_HEALTHY:
3602 		case HPC_EVENT_SLOT_HEALTHY_OK:
3603 		default:
3604 			break;
3605 	}
3606 	pcihp_set_hs_csr(slotinfop, config_handle, (uint8_t *)&hs_csr);
3607 	pcihp_config_teardown(&config_handle, &new_child, pci_dev, pcihp_p);
3608 }
3609 
3610 static int
3611 pcihp_config_setup(dev_info_t **dip, ddi_acc_handle_t *handle,
3612 			dev_info_t **new_child, int pci_dev, pcihp_t *pcihp_p)
3613 {
3614 	dev_info_t *pdip = pcihp_p->dip;
3615 	int bus, len, rc = DDI_SUCCESS;
3616 	struct pcihp_slotinfo *slotinfop;
3617 	hpc_slot_state_t rstate;
3618 	ddi_acc_hdl_t *hp;
3619 	pci_bus_range_t pci_bus_range;
3620 
3621 	slotinfop = &pcihp_p->slotinfo[pci_dev];
3622 
3623 	/*
3624 	 * If declared failed, don't allow Config operations.
3625 	 * Otherwise, if good or failing, it is assumed Ok
3626 	 * to get config data.
3627 	 */
3628 	if (slotinfop->condition == AP_COND_FAILED) {
3629 		return (PCIHP_FAILURE);
3630 	}
3631 	/*
3632 	 * check to see if there is a hardware present first.
3633 	 * If no hardware present, no need to probe this slot.
3634 	 * We can do this first probably as a first step towards
3635 	 * safeguarding from accidental removal (we don't support it!).
3636 	 */
3637 	if (hpc_nexus_control(slotinfop->slot_hdl,
3638 			HPC_CTRL_GET_SLOT_STATE, (caddr_t)&rstate) != 0) {
3639 		return (DDI_FAILURE);
3640 	}
3641 
3642 	if (rstate != HPC_SLOT_CONNECTED) {
3643 		/* error. slot must be connected */
3644 		return (DDI_FAILURE);
3645 	}
3646 	*new_child = NULL;
3647 
3648 	/*
3649 	 * If there is no dip then we need to see if an
3650 	 * adapter has just been hot plugged.
3651 	 */
3652 	len = sizeof (pci_bus_range_t);
3653 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, pdip,
3654 	    0, "bus-range",
3655 	    (caddr_t)&pci_bus_range, &len) != DDI_SUCCESS) {
3656 
3657 		return (PCIHP_FAILURE);
3658 	}
3659 
3660 	/* primary bus number of this bus node */
3661 	bus = pci_bus_range.lo;
3662 
3663 	if (ndi_devi_alloc(pdip, DEVI_PSEUDO_NEXNAME,
3664 	    (pnode_t)DEVI_SID_NODEID, dip) != NDI_SUCCESS) {
3665 
3666 		PCIHP_DEBUG((CE_NOTE, "Failed to alloc probe node\n"));
3667 		return (PCIHP_FAILURE);
3668 	}
3669 
3670 	if (pcihp_add_dummy_reg_property(*dip, bus,
3671 	    pci_dev, 0) != DDI_SUCCESS) {
3672 
3673 		(void) ndi_devi_free(*dip);
3674 		return (PCIHP_FAILURE);
3675 	}
3676 
3677 	/*
3678 	 * Probe for a device. Possibly a non (c)PCI board could be sitting
3679 	 * here which would never respond to PCI config cycles - in which
3680 	 * case we return. Eventually a configure operation would fail.
3681 	 */
3682 	if (pci_config_setup(*dip, handle) != DDI_SUCCESS) {
3683 		cmn_err(CE_WARN, "Cannot set config space map for"
3684 		    " pci device number %d", pci_dev);
3685 		(void) ndi_devi_free(*dip);
3686 		return (PCIHP_FAILURE);
3687 	}
3688 
3689 	/*
3690 	 * See if there is any PCI HW at this location
3691 	 * by reading the Vendor ID.  If it returns with 0xffff
3692 	 * then there is no hardware at this location.
3693 	 */
3694 	if (pcihp_indirect_map(*dip) == DDI_SUCCESS) {
3695 		if (pci_config_get16(*handle, 0) == 0xffff) {
3696 			pci_config_teardown(handle);
3697 			(void) ndi_devi_free(*dip);
3698 			return (PCIHP_FAILURE);
3699 		}
3700 	} else {
3701 		/* Check if mapping is OK */
3702 		hp = impl_acc_hdl_get(*handle);
3703 
3704 		if (ddi_peek16(*dip, (int16_t *)(hp->ah_addr),
3705 			(int16_t *)0) != DDI_SUCCESS) {
3706 #ifdef DEBUG
3707 			cmn_err(CE_WARN, "Cannot Map PCI config space for "
3708 			    "device number %d", pci_dev);
3709 #endif
3710 			pci_config_teardown(handle);
3711 			(void) ndi_devi_free(*dip);
3712 			return (PCIHP_FAILURE);
3713 		}
3714 	}
3715 
3716 	*new_child = *dip;
3717 	return (rc);
3718 
3719 }
3720 
3721 static void
3722 pcihp_config_teardown(ddi_acc_handle_t *handle,
3723 			dev_info_t **new_child, int pci_dev, pcihp_t *pcihp_p)
3724 {
3725 	struct pcihp_slotinfo *slotinfop = &pcihp_p->slotinfo[pci_dev];
3726 
3727 	pci_config_teardown(handle);
3728 	if (*new_child) {
3729 		(void) ndi_devi_free(*new_child);
3730 		/*
3731 		 * If occupant not configured, reset HS_CSR location
3732 		 * so that we reprobe. This covers cases where
3733 		 * the receptacle had a status change without a
3734 		 * notification to the framework.
3735 		 */
3736 		if (slotinfop->ostate != AP_OSTATE_CONFIGURED)
3737 			slotinfop->hs_csr_location = 0;
3738 	}
3739 }
3740 
3741 static int
3742 pcihp_get_board_type(struct pcihp_slotinfo *slotinfop)
3743 {
3744 	hpc_board_type_t board_type;
3745 
3746 	/*
3747 	 * Get board type data structure, hpc_board_type_t.
3748 	 */
3749 	if (hpc_nexus_control(slotinfop->slot_hdl, HPC_CTRL_GET_BOARD_TYPE,
3750 				(caddr_t)&board_type) != 0) {
3751 
3752 		cmn_err(CE_WARN, "Cannot Get Board Type..");
3753 		return (-1);
3754 	}
3755 
3756 	/*
3757 	 * We expect the Hotswap Controller to tell us if the board is
3758 	 * a hotswap board or not, as it probably cannot differentiate
3759 	 * between a basic hotswap board, a non hotswap board and a
3760 	 * hotswap nonfriendly board.
3761 	 * So here is the logic to differentiate between the various
3762 	 * types of cPCI boards.
3763 	 * In case if the HSC returns board type as unknown, we assign
3764 	 * the default board type as defined by a configurable variable
3765 	 * for a BHS, nonfriendly FHS and non HS board.
3766 	 */
3767 	if (slotinfop->slot_type & HPC_SLOT_TYPE_CPCI) {
3768 		if (slotinfop->hs_csr_location > 0)
3769 			board_type = HPC_BOARD_CPCI_FULL_HS;
3770 		else {
3771 			if (board_type == HPC_BOARD_CPCI_HS) {
3772 				if (slotinfop->hs_csr_location
3773 					== -1)
3774 					board_type =
3775 					HPC_BOARD_CPCI_BASIC_HS;
3776 			}
3777 			if (board_type == HPC_BOARD_UNKNOWN) {
3778 				if (slotinfop->hs_csr_location
3779 					== -1) {
3780 					board_type =
3781 					pcihp_cpci_board_type;
3782 				} else if
3783 				(slotinfop->hs_csr_location
3784 					!= 0) {
3785 					board_type =
3786 					HPC_BOARD_CPCI_FULL_HS;
3787 				}
3788 			}
3789 		}
3790 		/*
3791 		 * If board type is a non hotswap board, then we must
3792 		 * deny a unconfigure operation. So set this flag.
3793 		 * Strictly speaking, there is no reason not to disallow
3794 		 * a unconfigure operation on nonhotswap boards. But this
3795 		 * is the only way we can prevent a user from accidentally
3796 		 * removing the board and damaging it.
3797 		 */
3798 		if (board_type == HPC_BOARD_CPCI_NON_HS)
3799 			slotinfop->slot_flags |=
3800 				PCIHP_SLOT_DEV_NON_HOTPLUG;
3801 		else
3802 			slotinfop->slot_flags &=
3803 				~PCIHP_SLOT_DEV_NON_HOTPLUG;
3804 	}
3805 	return (board_type);
3806 }
3807 
3808 
3809 /*
3810  * Generate the System Event with a possible hint.
3811  */
3812 static void
3813 pcihp_gen_sysevent(char *slot_name, int event_sub_class, int hint,
3814 				dev_info_t *self, int kmflag)
3815 {
3816 
3817 	int err;
3818 	char *ev_subclass = NULL;
3819 	sysevent_id_t eid;
3820 	nvlist_t *ev_attr_list = NULL;
3821 	char attach_pnt[MAXPATHLEN];
3822 
3823 	/*
3824 	 * Minor device name (AP) will be bus path
3825 	 * concatenated with slot name
3826 	 */
3827 
3828 	(void) strcpy(attach_pnt, PCIHP_DEVICES_STR);
3829 	(void) ddi_pathname(self, attach_pnt + strlen(PCIHP_DEVICES_STR));
3830 	(void) strcat(attach_pnt, ":");
3831 	(void) strcat(attach_pnt, slot_name);
3832 	err = nvlist_alloc(&ev_attr_list, NV_UNIQUE_NAME_TYPE, kmflag);
3833 	if (err != 0) {
3834 		cmn_err(CE_WARN,
3835 		    "%s%d: Failed to allocate memory "
3836 		    "for event attributes%s", ddi_driver_name(self),
3837 		    ddi_get_instance(self), ESC_DR_AP_STATE_CHANGE);
3838 		return;
3839 	}
3840 
3841 	switch (event_sub_class) {
3842 
3843 	/* event sub class: ESC_DR_AP_STATE_CHANGE */
3844 	case PCIHP_DR_AP_STATE_CHANGE:
3845 
3846 		ev_subclass = ESC_DR_AP_STATE_CHANGE;
3847 
3848 		switch (hint) {
3849 
3850 		case SE_NO_HINT:	/* fall through */
3851 		case SE_HINT_INSERT:	/* fall through */
3852 		case SE_HINT_REMOVE:
3853 
3854 
3855 			err = nvlist_add_string(ev_attr_list, DR_HINT,
3856 			    SE_HINT2STR(hint));
3857 
3858 			if (err != 0) {
3859 				cmn_err(CE_WARN, "%s%d: Failed to add attr [%s]"
3860 					" for %s event", ddi_driver_name(self),
3861 					ddi_get_instance(self),
3862 					DR_HINT, ESC_DR_AP_STATE_CHANGE);
3863 				nvlist_free(ev_attr_list);
3864 				return;
3865 			}
3866 			break;
3867 
3868 		default:
3869 			cmn_err(CE_WARN, "%s%d: Unknown hint on sysevent",
3870 				ddi_driver_name(self), ddi_get_instance(self));
3871 			nvlist_free(ev_attr_list);
3872 			return;
3873 		}
3874 
3875 		break;
3876 
3877 	/* event sub class: ESC_DR_REQ */
3878 	case PCIHP_DR_REQ:
3879 
3880 		ev_subclass = ESC_DR_REQ;
3881 
3882 		switch (hint) {
3883 
3884 		case SE_INVESTIGATE_RES:	/* fall through */
3885 		case SE_INCOMING_RES:	/* fall through */
3886 		case SE_OUTGOING_RES:	/* fall through */
3887 
3888 			err = nvlist_add_string(ev_attr_list, DR_REQ_TYPE,
3889 				SE_REQ2STR(hint));
3890 
3891 			if (err != 0) {
3892 				cmn_err(CE_WARN,
3893 					"%s%d: Failed to add attr [%s] "
3894 					"for %s event", ddi_driver_name(self),
3895 					ddi_get_instance(self),
3896 					DR_REQ_TYPE, ESC_DR_REQ);
3897 				nvlist_free(ev_attr_list);
3898 				return;
3899 			}
3900 			break;
3901 
3902 		default:
3903 			cmn_err(CE_WARN,
3904 				"%s%d:  Unknown hint on sysevent",
3905 				ddi_driver_name(self), ddi_get_instance(self));
3906 			nvlist_free(ev_attr_list);
3907 			return;
3908 		}
3909 
3910 		break;
3911 
3912 	default:
3913 		cmn_err(CE_WARN,
3914 			"%s%d:  Unknown Event subclass", ddi_driver_name(self),
3915 			ddi_get_instance(self));
3916 		nvlist_free(ev_attr_list);
3917 		return;
3918 	}
3919 
3920 	/*
3921 	 * Add attachment point as attribute (common attribute)
3922 	 */
3923 
3924 	err = nvlist_add_string(ev_attr_list, DR_AP_ID, attach_pnt);
3925 
3926 	if (err != 0) {
3927 		cmn_err(CE_WARN,
3928 			"%s%d: Failed to add attr [%s] for %s event",
3929 			ddi_driver_name(self), ddi_get_instance(self),
3930 			DR_AP_ID, EC_DR);
3931 		nvlist_free(ev_attr_list);
3932 		return;
3933 	}
3934 
3935 
3936 	/*
3937 	 * Log this event with sysevent framework.
3938 	 */
3939 
3940 	err = ddi_log_sysevent(self, DDI_VENDOR_SUNW, EC_DR,
3941 	    ev_subclass, ev_attr_list, &eid,
3942 	    ((kmflag == KM_SLEEP) ? DDI_SLEEP : DDI_NOSLEEP));
3943 	if (err != 0) {
3944 		cmn_err(CE_WARN, "%s%d: Failed to log %s event",
3945 			ddi_driver_name(self), ddi_get_instance(self), EC_DR);
3946 	}
3947 
3948 	nvlist_free(ev_attr_list);
3949 }
3950 
3951 int
3952 pcihp_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
3953     int flags, char *name, caddr_t valuep, int *lengthp)
3954 {
3955 	int pci_dev;
3956 
3957 	if (dev == DDI_DEV_T_ANY)
3958 		goto skip;
3959 
3960 	if (strcmp(name, "pci-occupant") == 0) {
3961 		pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(getminor(dev));
3962 		pcihp_create_occupant_props_nolock(dip, dev, pci_dev);
3963 	}
3964 	/* other cases... */
3965 skip:
3966 	return (ddi_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp));
3967 }
3968 
3969 /*
3970  * this function is called only for SPARC platforms, where we may have
3971  * a mix n' match of direct vs indirectly mapped configuration space.
3972  * On x86, this function should always return success since the configuration
3973  * space is always indirect mapped.
3974  */
3975 /*ARGSUSED*/
3976 static int
3977 pcihp_indirect_map(dev_info_t *dip)
3978 {
3979 #if defined(__sparc)
3980 	int rc = DDI_FAILURE;
3981 
3982 	if (ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(dip), 0,
3983 			PCI_DEV_CONF_MAP_PROP, DDI_FAILURE) != DDI_FAILURE)
3984 		rc = DDI_SUCCESS;
3985 	else
3986 		if (ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(dip),
3987 				0, PCI_BUS_CONF_MAP_PROP,
3988 				DDI_FAILURE) != DDI_FAILURE)
3989 			rc = DDI_SUCCESS;
3990 	return (rc);
3991 #else
3992 	return (DDI_SUCCESS);
3993 #endif
3994 }
3995