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