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