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