xref: /titanic_52/usr/src/lib/cfgadm_plugins/pci/common/cfga.c (revision fa9e4066f08beec538e775443c5be79dd423fcab)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 /*
31  *	Plugin Library for PCI Hot-Plug Controller
32  */
33 
34 #include <stddef.h>
35 #include <locale.h>
36 #include <ctype.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42 #include <errno.h>
43 #include <locale.h>
44 #include <langinfo.h>
45 #include <time.h>
46 #include <sys/param.h>
47 #include <stdarg.h>
48 #include <libdevinfo.h>
49 #include <libdevice.h>
50 
51 #define	CFGA_PLUGIN_LIB
52 
53 #include <config_admin.h>
54 
55 #include <sys/types.h>
56 #include <sys/stat.h>
57 #include <sys/ioctl.h>
58 #include <sys/dditypes.h>
59 #include <sys/devctl.h>
60 #include <sys/modctl.h>
61 #include <sys/hotplug/hpctrl.h>
62 #include <sys/pci.h>
63 #include <libintl.h>
64 
65 #include <dirent.h>
66 #include <limits.h>
67 #include <sys/mkdev.h>
68 #include <librcm.h>
69 #include "../../../../common/pci/pci_strings.h"
70 
71 extern const struct pci_class_strings_s class_pci[];
72 extern int class_pci_items;
73 
74 /*
75  * Set the version number
76  */
77 int cfga_version = CFGA_HSL_V2;
78 
79 #ifdef	DEBUG
80 #define	PCIHP_DBG	1
81 #endif
82 
83 #if !defined(TEXT_DOMAIN)
84 #define	TEXT_DOMAIN	"SYS_TEST"
85 #endif
86 
87 /*
88  *	DEBUGING LEVEL
89  *
90  * 	External routines:  1 - 2
91  *	Internal routines:  3 - 4
92  */
93 #ifdef	PCIHP_DBG
94 int	pcihp_debug = 1;
95 #define	DBG(level, args) \
96 	{ if (pcihp_debug >= (level)) printf args; }
97 #define	DBG_F(level, args) \
98 	{ if (pcihp_debug >= (level)) fprintf args; }
99 #else
100 #define	DBG(level, args)	/* nothing */
101 #define	DBG_F(level, args)	/* nothing */
102 #endif
103 
104 #define	CMD_ACQUIRE		0
105 #define	CMD_GETSTAT		1
106 #define	CMD_LIST		2
107 #define	CMD_SLOT_CONNECT	3
108 #define	CMD_SLOT_DISCONNECT	4
109 #define	CMD_SLOT_CONFIGURE	5
110 #define	CMD_SLOT_UNCONFIGURE	6
111 #define	CMD_SLOT_INSERT		7
112 #define	CMD_SLOT_REMOVE		8
113 #define	CMD_OPEN		9
114 #define	CMD_FSTAT		10
115 #define	ERR_CMD_INVAL		11
116 #define	ERR_AP_INVAL		12
117 #define	ERR_AP_ERR		13
118 #define	ERR_OPT_INVAL		14
119 
120 static char *
121 cfga_errstrs[] = {
122 	/* n */ "acquire ",
123 	/* n */ "get-status ",
124 	/* n */ "list ",
125 	/* n */ "connect ",
126 	/* n */ "disconnect ",
127 	/* n */ "configure ",
128 	/* n */ "unconfigure ",
129 	/* n */ "insert ",
130 	/* n */ "remove ",
131 	/* n */ "open ",
132 	/* n */ "fstat ",
133 	/* y */ "invalid command ",
134 	/* y */ "invalid attachment point ",
135 	/* y */ "invalid transition ",
136 	/* y */ "invalid option ",
137 		NULL
138 };
139 
140 #define	HELP_HEADER		1
141 #define	HELP_CONFIG		2
142 #define	HELP_ENABLE_SLOT	3
143 #define	HELP_DISABLE_SLOT	4
144 #define	HELP_ENABLE_AUTOCONF	5
145 #define	HELP_DISABLE_AUTOCONF	6
146 #define	HELP_LED_CNTRL		7
147 #define	HELP_UNKNOWN		8
148 #define	SUCCESS			9
149 #define	FAILED			10
150 #define	UNKNOWN			11
151 
152 #define	MAXLINE			256
153 
154 /* for type string assembly in get_type() */
155 #define	TPCT(s)	(void) strlcat(buf, (s), CFGA_TYPE_LEN)
156 
157 extern int errno;
158 
159 static void cfga_err(char **errstring, ...);
160 static cfga_err_t fix_ap_name(char *ap_log_id, const char *ap_id,
161     char *slot_name, char **errstring);
162 static void build_control_data(struct hpc_control_data *iocdata, uint_t cmd,
163     void *retdata);
164 static cfga_err_t check_options(const char *options);
165 static void cfga_msg(struct cfga_msg *msgp, const char *str);
166 
167 static char *
168 cfga_strs[] = {
169 NULL,
170 "\nPCI hotplug specific commands:",
171 "\t-c [connect|disconnect|configure|unconfigure|insert|remove] "
172 "ap_id [ap_id...]",
173 "\t-x enable_slot  ap_id [ap_id...]",
174 "\t-x disable_slot ap_id [ap_id...]",
175 "\t-x enable_autoconfig  ap_id [ap_id...]",
176 "\t-x disable_autoconfig ap_id [ap_id...]",
177 "\t-x led[=[fault|power|active|attn],mode=[on|off|blink]] ap_id [ap_id...]",
178 "\tunknown command or option: ",
179 "success   ",
180 "failed   ",
181 "unknown",
182 NULL
183 };
184 
185 #define	MAX_FORMAT 80
186 
187 #define	ENABLE_SLOT	0
188 #define	DISABLE_SLOT	1
189 #define	ENABLE_AUTOCNF	2
190 #define	DISABLE_AUTOCNF	3
191 #define	LED		4
192 #define	MODE		5
193 
194 /*
195  * Board Type
196  */
197 static char *
198 board_strs[] = {
199 	/* n */ "???",	/* HPC_BOARD_UNKNOWN */
200 	/* n */ "hp",	/* HPC_BOARD_PCI_HOTPLUG */
201 	/* n */ "nhs",	/* HPC_BOARD_CPCI_NON_HS */
202 	/* n */ "bhs",  /* HPC_BOARD_CPCI_BASIC_HS */
203 	/* n */ "fhs",	/* HPC_BOARD_CPCI_FULL_HS */
204 	/* n */ "hs",	/* HPC_BOARD_CPCI_HS */
205 	/* n */ NULL
206 };
207 
208 /*
209  * HW functions
210  */
211 static char *
212 func_strs[] = {
213 	/* n */ "enable_slot",
214 	/* n */ "disable_slot",
215 	/* n */ "enable_autoconfig",
216 	/* n */ "disable_autoconfig",
217 	/* n */ "led",
218 	/* n */ "mode",
219 	/* n */ NULL
220 };
221 
222 /*
223  * LED strings
224  */
225 static char *
226 led_strs[] = {
227 	/* n */ "fault",	/* HPC_FAULT_LED */
228 	/* n */ "power",	/* HPC_POWER_LED */
229 	/* n */ "attn",		/* HPC_ATTN_LED */
230 	/* n */ "active",	/* HPC_ACTIVE_LED */
231 	/* n */ NULL
232 };
233 
234 #define	FAULT	0
235 #define	POWER	1
236 #define	ATTN	2
237 #define	ACTIVE	3
238 
239 static char *
240 mode_strs[] = {
241 	/* n */ "off",		/* HPC_LED_OFF */
242 	/* n */ "on",		/* HPC_LED_ON */
243 	/* n */ "blink",	/* HPC_LED_BLINK */
244 	/* n */	NULL
245 };
246 
247 #define	OFF	0
248 #define	ON	1
249 #define	BLINK	2
250 
251 #define	cfga_errstrs(i)		cfga_errstrs[(i)]
252 
253 #define	cfga_eid(a, b)		(((a) << 8) + (b))
254 #define	MAXDEVS			32
255 
256 typedef enum {
257 	SOLARIS_SLT_NAME,
258 	PROM_SLT_NAME
259 } slt_name_src_t;
260 
261 struct searcharg {
262 	char	*devpath;
263 	char	slotnames[MAXDEVS][MAXNAMELEN];
264 	int	minor;
265 	di_prom_handle_t	promp;
266 	slt_name_src_t	slt_name_src;
267 };
268 
269 static void *private_check;
270 
271 static int
272 get_occupants(const char *ap_id, hpc_occupant_info_t *occupant)
273 {
274 	int rv;
275 	int fd;
276 	di_node_t ap_node;
277 	char *prop_data;
278 	char *tmp;
279 	char *ptr;
280 	struct stat statbuf;
281 	dev_t devt;
282 
283 	if ((fd = open(ap_id, O_RDWR)) == -1) {
284 		DBG(2, ("open = ap_id%s, fd%d\n", ap_id, fd));
285 		DBG_F(2, (stderr, "open on %s failed\n", ap_id));
286 		return (CFGA_ERROR);
287 	}
288 
289 	if (fstat(fd, &statbuf) == -1) {
290 		DBG(1, ("stat failed: %i\n", errno));
291 		(void) close(fd);
292 		return (CFGA_ERROR);
293 	}
294 	(void) close(fd);
295 
296 	devt = statbuf.st_rdev;
297 
298 	tmp = (char *)(ap_id + sizeof ("/devices") - 1);
299 	if ((ptr = strrchr(tmp, ':')) != NULL)
300 		*ptr = '\0';
301 
302 	ap_node = di_init(tmp, DINFOPROP | DINFOMINOR);
303 	if (ap_node == DI_NODE_NIL) {
304 		DBG(1, ("dead %i\n", errno));
305 		return (CFGA_ERROR);
306 	}
307 
308 #ifdef	PCIHP_DBG
309 	ptr = di_devfs_path(ap_node);
310 	DBG(1, ("get_occupants: %s\n", ptr));
311 	di_devfs_path_free(ptr);
312 #endif
313 
314 	if ((rv = di_prop_lookup_strings(devt, ap_node, "pci-occupant",
315 	    &prop_data)) == -1) {
316 		DBG(1, ("get_occupants: prop_lookup failed: %i\n", errno));
317 		di_fini(ap_node);
318 		return (CFGA_ERROR);
319 	}
320 
321 	if (prop_data && (strcmp(prop_data, "") == 0)) {
322 		di_fini(ap_node);
323 		occupant->i = 0;
324 		occupant->id[0] = NULL;
325 		return (CFGA_OK);
326 	}
327 
328 	DBG(1, ("get_occupants: %i devices found\n", rv));
329 	for (occupant->i = 0; occupant->i < rv; occupant->i++) {
330 		if (occupant->i >= (HPC_MAX_OCCUPANTS - 1)) {
331 			occupant->i--;
332 			break;
333 		}
334 		occupant->id[occupant->i] = (char *)malloc(
335 			strlen(prop_data) + sizeof ("/devices"));
336 		(void) snprintf(occupant->id[occupant->i], strlen(prop_data) +
337 		    sizeof ("/devices"), "/devices%s", prop_data);
338 		DBG(1, ("%s\n", occupant->id[occupant->i]));
339 		prop_data += strlen(prop_data) + 1;
340 	}
341 	di_fini(ap_node);
342 
343 	occupant->id[occupant->i] = NULL;
344 
345 	return (CFGA_OK);
346 }
347 
348 /*
349  * let rcm know that the device has indeed been removed and clean
350  * up rcm data
351  */
352 static void
353 confirm_rcm(hpc_occupant_info_t *occupant, rcm_handle_t *rhandle)
354 {
355 	DBG(1, ("confirm_rcm\n"));
356 
357 	if (occupant->i == 0) /* nothing was found to ask rcm about */
358 		return;
359 
360 	(void) rcm_notify_remove_list(rhandle, occupant->id, 0, NULL);
361 	(void) rcm_free_handle(rhandle);
362 
363 	for (; occupant->i >= 0; occupant->i--)
364 		free(occupant->id[occupant->i]);
365 }
366 
367 static void
368 fail_rcm(hpc_occupant_info_t *occupant, rcm_handle_t *rhandle)
369 {
370 	DBG(1, ("fail_rcm\n"));
371 
372 	if (occupant->i == 0) /* nothing was found to ask rcm about */
373 		return;
374 
375 	(void) rcm_notify_online_list(rhandle, occupant->id, 0, NULL);
376 	(void) rcm_free_handle(rhandle);
377 
378 	for (; occupant->i >= 0; occupant->i--)
379 		free(occupant->id[occupant->i]);
380 }
381 
382 /*
383  * copied from scsi_rcm_info_table
384  *
385  *      Takes an opaque rcm_info_t pointer and a character pointer, and appends
386  * the rcm_info_t data in the form of a table to the given character pointer.
387  */
388 static void
389 pci_rcm_info_table(rcm_info_t *rinfo, char **table)
390 {
391 	int i;
392 	size_t w;
393 	size_t width = 0;
394 	size_t w_rsrc = 0;
395 	size_t w_info = 0;
396 	size_t table_size = 0;
397 	uint_t tuples = 0;
398 	rcm_info_tuple_t *tuple = NULL;
399 	char *rsrc;
400 	char *info;
401 	char *newtable;
402 	static char format[MAX_FORMAT];
403 	const char *infostr;
404 
405 	/* Protect against invalid arguments */
406 	if (rinfo == NULL || table == NULL)
407 		return;
408 
409 	/* Set localized table header strings */
410 	rsrc = dgettext(TEXT_DOMAIN, "Resource");
411 	info = dgettext(TEXT_DOMAIN, "Information");
412 
413 	/* A first pass, to size up the RCM information */
414 	while (tuple = rcm_info_next(rinfo, tuple)) {
415 		if ((infostr = rcm_info_info(tuple)) != NULL) {
416 			tuples++;
417 			if ((w = strlen(rcm_info_rsrc(tuple))) > w_rsrc)
418 				w_rsrc = w;
419 			if ((w = strlen(infostr)) > w_info)
420 				w_info = w;
421 		}
422 	}
423 
424 	/* If nothing was sized up above, stop early */
425 	if (tuples == 0)
426 		return;
427 
428 	/* Adjust column widths for column headings */
429 	if ((w = strlen(rsrc)) > w_rsrc)
430 		w_rsrc = w;
431 	else if ((w_rsrc - w) % 2)
432 		w_rsrc++;
433 	if ((w = strlen(info)) > w_info)
434 		w_info = w;
435 	else if ((w_info - w) % 2)
436 		w_info++;
437 
438 	/*
439 	 * Compute the total line width of each line,
440 	 * accounting for intercolumn spacing.
441 	 */
442 	width = w_info + w_rsrc + 4;
443 
444 	/* Allocate space for the table */
445 	table_size = (2 + tuples) * (width + 1) + 2;
446 	if (*table == NULL) {
447 		/* zero fill for the strcat() call below */
448 		*table = calloc(table_size, sizeof (char));
449 		if (*table == NULL)
450 			return;
451 	} else {
452 		newtable = realloc(*table, strlen(*table) + table_size);
453 		if (newtable == NULL)
454 			return;
455 		else
456 			*table = newtable;
457 	}
458 
459 	/* Place a table header into the string */
460 
461 	/* The resource header */
462 	(void) strcat(*table, "\n");
463 	w = strlen(rsrc);
464 	for (i = 0; i < ((w_rsrc - w) / 2); i++)
465 		(void) strcat(*table, " ");
466 	(void) strcat(*table, rsrc);
467 	for (i = 0; i < ((w_rsrc - w) / 2); i++)
468 		(void) strcat(*table, " ");
469 
470 	/* The information header */
471 	(void) strcat(*table, "  ");
472 	w = strlen(info);
473 	for (i = 0; i < ((w_info - w) / 2); i++)
474 		(void) strcat(*table, " ");
475 	(void) strcat(*table, info);
476 	for (i = 0; i < ((w_info - w) / 2); i++)
477 		(void) strcat(*table, " ");
478 	/* Underline the headers */
479 	(void) strcat(*table, "\n");
480 	for (i = 0; i < w_rsrc; i++)
481 		(void) strcat(*table, "-");
482 	(void) strcat(*table, "  ");
483 	for (i = 0; i < w_info; i++)
484 		(void) strcat(*table, "-");
485 
486 	/* Construct the format string */
487 	(void) snprintf(format, MAX_FORMAT, "%%-%ds  %%-%ds",
488 	    (int)w_rsrc, (int)w_info);
489 
490 	/* Add the tuples to the table string */
491 	tuple = NULL;
492 	while ((tuple = rcm_info_next(rinfo, tuple)) != NULL) {
493 		if ((infostr = rcm_info_info(tuple)) != NULL) {
494 			(void) strcat(*table, "\n");
495 			(void) sprintf(&((*table)[strlen(*table)]),
496 			    format, rcm_info_rsrc(tuple),
497 			    infostr);
498 		}
499 	}
500 }
501 
502 /*
503  * Figure out what device is about to be unconfigured or disconnected
504  * and make sure rcm is ok with it.
505  * hangs on to a list of handles so they can then be confirmed or denied
506  * if either getting the occupant list or talking to rcm fails
507  * return CFGA_ERROR so that things can go on without rcm
508  */
509 static int
510 check_rcm(const char *ap_id, hpc_occupant_info_t *occupant,
511     rcm_handle_t **rhandlep, char **errstring, cfga_flags_t flags)
512 {
513 	int rv;
514 	rcm_info_t *rinfo;
515 	rcm_handle_t *rhandle;
516 	uint_t rcmflags;
517 
518 	if (get_occupants(ap_id, occupant) != 0) {
519 		DBG(1, ("check_rcm: failed to get occupants\n"));
520 		return (CFGA_ERROR);
521 	}
522 
523 	if (occupant->i == 0) {
524 		DBG(1, ("check_rcm: no drivers attaching to occupants\n"));
525 		return (CFGA_OK);
526 	}
527 
528 	if (rcm_alloc_handle(NULL, 0, NULL, &rhandle)
529 	    != RCM_SUCCESS) {
530 		DBG(1, ("check_rcm: blocked by rcm failure\n"));
531 		return (CFGA_ERROR);
532 	}
533 
534 	rcmflags = (flags & CFGA_FLAG_FORCE) ? RCM_FORCE : 0;
535 	rv = rcm_request_offline_list(rhandle, occupant->id, rcmflags, &rinfo);
536 
537 	if (rv == RCM_FAILURE) {
538 		DBG(1, ("check_rcm: blocked by rcm failure 2\n"));
539 		pci_rcm_info_table(rinfo, errstring);
540 		rcm_free_info(rinfo);
541 		fail_rcm(occupant, rhandle);
542 		return (CFGA_BUSY);
543 	}
544 	if (rv == RCM_CONFLICT) {
545 		DBG(1, ("check_rcm: blocked by %i\n",
546 		    rcm_info_pid(rinfo)));
547 		pci_rcm_info_table(rinfo, errstring);
548 		rcm_free_info(rinfo);
549 		(void) rcm_free_handle(rhandle);
550 		for (; occupant->i >= 0; occupant->i--)
551 			free(occupant->id[occupant->i]);
552 		return (CFGA_BUSY);
553 	}
554 
555 	rcm_free_info(rinfo);
556 	*rhandlep = rhandle;
557 
558 	/* else */
559 	return (CFGA_OK);
560 }
561 
562 
563 /*
564  * Transitional Diagram:
565  *
566  *  empty		unconfigure
567  * (remove)	^|  (physically insert card)
568  *			|V
569  * disconnect	configure
570  * "-c DISCONNECT"	^|	"-c CONNECT"
571  *				|V	"-c CONFIGURE"
572  * connect	unconfigure	->	connect    configure
573  *						<-
574  *					"-c UNCONFIGURE"
575  *
576  */
577 /*ARGSUSED*/
578 cfga_err_t
579 cfga_change_state(cfga_cmd_t state_change_cmd, const char *ap_id,
580     const char *options, struct cfga_confirm *confp,
581     struct cfga_msg *msgp, char **errstring, cfga_flags_t flags)
582 {
583 	int rv;
584 	devctl_hdl_t		dcp;
585 	devctl_ap_state_t	state;
586 	ap_rstate_t		rs;
587 	ap_ostate_t		os;
588 	hpc_occupant_info_t occupants;
589 	rcm_handle_t *rhandle;
590 
591 	if ((rv = check_options(options)) != CFGA_OK) {
592 		return (rv);
593 	}
594 
595 	if (errstring != NULL)
596 		*errstring = NULL;
597 
598 	rv = CFGA_OK;
599 	DBG(1, ("cfga_change_state:(%s)\n", ap_id));
600 
601 	if ((dcp = devctl_ap_acquire((char *)ap_id, 0)) == NULL) {
602 		if (rv == EBUSY) {
603 			cfga_err(errstring, CMD_ACQUIRE, ap_id, 0);
604 			DBG(1, ("cfga_change_state: device is busy\n"));
605 			rv = CFGA_BUSY;
606 		} else
607 			rv = CFGA_ERROR;
608 		return (rv);
609 	}
610 
611 	if (devctl_ap_getstate(dcp, NULL, &state) == -1) {
612 		DBG(2, ("cfga_change_state: devctl ap getstate failed\n"));
613 		cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
614 		devctl_release((devctl_hdl_t)dcp);
615 		if (rv == EBUSY)
616 			rv = CFGA_BUSY;
617 		else
618 			rv = CFGA_ERROR;
619 		return (rv);
620 	}
621 
622 	rs = state.ap_rstate;
623 	os = state.ap_ostate;
624 
625 	DBG(1, ("cfga_change_state: rs is %d\n", state.ap_rstate));
626 	DBG(1, ("cfga_change_state: os is %d\n", state.ap_ostate));
627 	switch (state_change_cmd) {
628 	case CFGA_CMD_CONNECT:
629 		if ((rs == AP_RSTATE_CONNECTED) ||
630 		    (os == AP_OSTATE_CONFIGURED)) {
631 			cfga_err(errstring, ERR_AP_ERR, 0);
632 			rv = CFGA_INVAL;
633 		} else {
634 			/* Lets connect the slot */
635 			if (devctl_ap_connect(dcp, NULL) == -1) {
636 				rv = CFGA_ERROR;
637 				cfga_err(errstring,
638 				    CMD_SLOT_CONNECT, 0);
639 			}
640 		}
641 
642 		break;
643 
644 	case CFGA_CMD_DISCONNECT:
645 		DBG(1, ("disconnect\n"));
646 
647 		if (os == AP_OSTATE_CONFIGURED) {
648 			if ((rv = check_rcm(ap_id, &occupants, &rhandle,
649 			    errstring, flags)) == CFGA_BUSY) {
650 				break;
651 			} else if (rv == CFGA_OK) {
652 				if (devctl_ap_unconfigure(dcp, NULL) == -1) {
653 					if (errno == EBUSY)
654 						rv = CFGA_BUSY;
655 					else
656 						rv = CFGA_ERROR;
657 					cfga_err(errstring,
658 					    CMD_SLOT_DISCONNECT, 0);
659 					fail_rcm(&occupants, rhandle);
660 					break;
661 				} else {
662 					confirm_rcm(&occupants, rhandle);
663 				}
664 			} else { /* rv == CFGA_ERROR */
665 				if (devctl_ap_unconfigure(dcp, NULL) == -1) {
666 					if (errno == EBUSY)
667 						rv = CFGA_BUSY;
668 					else
669 						rv = CFGA_ERROR;
670 					break;
671 				} else {
672 					rv = CFGA_OK;
673 				}
674 			}
675 		}
676 
677 		if (rs == AP_RSTATE_CONNECTED) {
678 			if (devctl_ap_disconnect(dcp, NULL) == -1) {
679 				rv = CFGA_ERROR;
680 				cfga_err(errstring, CMD_SLOT_DISCONNECT, 0);
681 				break;
682 			}
683 		} else {
684 			cfga_err(errstring, ERR_AP_ERR, 0);
685 			rv = CFGA_INVAL;
686 		}
687 
688 		break;
689 
690 	case CFGA_CMD_CONFIGURE:
691 		if (rs == AP_RSTATE_DISCONNECTED) {
692 			if (devctl_ap_connect(dcp, NULL) == -1) {
693 				rv = CFGA_ERROR;
694 				cfga_err(errstring, CMD_SLOT_CONNECT, 0);
695 				break;
696 			}
697 		}
698 
699 		/*
700 		 * for multi-func device we allow multiple
701 		 * configure on the same slot because one
702 		 * func can be configured and other one won't
703 		 */
704 		if (devctl_ap_configure(dcp, NULL) == -1) {
705 			rv = CFGA_ERROR;
706 			cfga_err(errstring, CMD_SLOT_CONFIGURE, 0);
707 			if (devctl_ap_disconnect(dcp, NULL) == -1) {
708 				rv = CFGA_ERROR;
709 				cfga_err(errstring,
710 				    CMD_SLOT_CONFIGURE, 0);
711 			}
712 			break;
713 		}
714 
715 		break;
716 
717 	case CFGA_CMD_UNCONFIGURE:
718 		DBG(1, ("unconfigure\n"));
719 
720 		if (os == AP_OSTATE_CONFIGURED) {
721 			if ((rv = check_rcm(ap_id, &occupants, &rhandle,
722 			    errstring, flags)) == CFGA_BUSY) {
723 				break;
724 			} else if (rv == CFGA_OK) {
725 				if (devctl_ap_unconfigure(dcp, NULL) == -1) {
726 					if (errno == EBUSY)
727 						rv = CFGA_BUSY;
728 					else {
729 						if (errno == ENOTSUP)
730 							rv = CFGA_OPNOTSUPP;
731 						else
732 							rv = CFGA_ERROR;
733 					}
734 					cfga_err(errstring,
735 					    CMD_SLOT_UNCONFIGURE, 0);
736 					fail_rcm(&occupants, rhandle);
737 				} else {
738 					confirm_rcm(&occupants, rhandle);
739 				}
740 			} else { /* rv == CFGA_ERROR */
741 				if (devctl_ap_unconfigure(dcp, NULL) == -1) {
742 					if (errno == EBUSY)
743 						rv = CFGA_BUSY;
744 					else {
745 						if (errno == ENOTSUP)
746 							rv = CFGA_OPNOTSUPP;
747 						else
748 							rv = CFGA_ERROR;
749 					}
750 					cfga_err(errstring,
751 					    CMD_SLOT_UNCONFIGURE, 0);
752 				} else {
753 					rv = CFGA_OK;
754 				}
755 			}
756 		} else {
757 			cfga_err(errstring, ERR_AP_ERR, 0);
758 			rv = CFGA_INVAL;
759 		}
760 
761 		DBG(1, ("uncofigure rv:(%i)\n", rv));
762 		break;
763 
764 	case CFGA_CMD_LOAD:
765 		if ((os == AP_OSTATE_UNCONFIGURED) &&
766 		    (rs == AP_RSTATE_DISCONNECTED)) {
767 			if (devctl_ap_insert(dcp, NULL) == -1) {
768 				rv = CFGA_ERROR;
769 				cfga_err(errstring, CMD_SLOT_INSERT, 0);
770 			}
771 		} else {
772 			cfga_err(errstring, ERR_AP_ERR, 0);
773 			rv = CFGA_INVAL;
774 		}
775 
776 		break;
777 
778 	case CFGA_CMD_UNLOAD:
779 		if ((os == AP_OSTATE_UNCONFIGURED) &&
780 		    (rs == AP_RSTATE_DISCONNECTED)) {
781 			if (devctl_ap_remove(dcp, NULL) == -1) {
782 				rv = CFGA_ERROR;
783 				cfga_err(errstring, CMD_SLOT_REMOVE, 0);
784 			}
785 		} else {
786 				cfga_err(errstring, ERR_AP_ERR, 0);
787 				rv = CFGA_INVAL;
788 			}
789 
790 		break;
791 
792 	default:
793 		rv = CFGA_OPNOTSUPP;
794 		break;
795 	}
796 
797 	devctl_release((devctl_hdl_t)dcp);
798 	return (rv);
799 }
800 
801 /*
802  * Building iocdatat to pass it to nexus
803  *
804  *	iocdata->cmd ==  HPC_CTRL_ENABLE_SLOT/HPC_CTRL_DISABLE_SLOT
805  *			HPC_CTRL_ENABLE_AUTOCFG/HPC_CTRL_DISABLE_AUTOCFG
806  *			HPC_CTRL_GET_LED_STATE/HPC_CTRL_SET_LED_STATE
807  *			HPC_CTRL_GET_SLOT_STATE/HPC_CTRL_GET_SLOT_INFO
808  *			HPC_CTRL_DEV_CONFIGURE/HPC_CTRL_DEV_UNCONFIGURE
809  *			HPC_CTRL_GET_BOARD_TYPE
810  *
811  */
812 static void
813 build_control_data(struct hpc_control_data *iocdata, uint_t cmd,
814     void *retdata)
815 {
816 	iocdata->cmd = cmd;
817 	iocdata->data = retdata;
818 }
819 
820 /*
821  * building logical name from ap_id
822  */
823 /*ARGSUSED2*/
824 static void
825 get_logical_name(const char *ap_id, char *buf, dev_t rdev)
826 {
827 	char *bufptr, *bufptr2, *pci, *apid;
828 
829 	DBG(1, ("get_logical_name: %s\n", ap_id));
830 
831 	if ((apid = malloc(MAXPATHLEN)) == NULL) {
832 		DBG(1, ("malloc failed\n"));
833 		return;
834 	}
835 
836 	(void) memset(apid, 0, MAXPATHLEN);
837 	(void) strncpy(apid, ap_id, strlen(ap_id));
838 
839 	/* needs to look for last /, not first */
840 	bufptr = strrchr(apid, '/');
841 
842 	bufptr2 = strrchr(apid, ':');
843 	pci = ++bufptr;
844 	bufptr = strchr(pci, ',');
845 	if (bufptr != NULL) {
846 		*bufptr = '\0';
847 	}
848 
849 	bufptr = strchr(pci, '@');
850 	if (bufptr != NULL) {
851 		*bufptr = '\0';
852 		bufptr++;
853 	}
854 
855 	DBG(1, ("%s\n%s\n%s\n", pci, bufptr, bufptr2));
856 
857 	(void) strcat(buf, pci);
858 	(void) strcat(buf, bufptr);
859 	(void) strcat(buf, bufptr2);
860 	free(apid);
861 }
862 
863 static cfga_err_t
864 prt_led_mode(const char *ap_id, int repeat, char **errstring,
865     struct cfga_msg *msgp)
866 {
867 	hpc_led_info_t	power_led_info = {HPC_POWER_LED, 0};
868 	hpc_led_info_t	fault_led_info = {HPC_FAULT_LED, 0};
869 	hpc_led_info_t	attn_led_info = {HPC_ATTN_LED, 0};
870 	hpc_led_info_t	active_led_info = {HPC_ACTIVE_LED, 0};
871 	struct hpc_control_data iocdata;
872 	struct stat	statbuf;
873 	char  *buff;
874 	int	fd;
875 	hpc_slot_info_t		slot_info;
876 	char *cp, line[MAXLINE];
877 	int len = MAXLINE;
878 
879 	DBG(1, ("prt_led_mod function\n"));
880 	if (!repeat)
881 		cfga_msg(msgp, "Ap_Id\t\t\tLed");
882 
883 	if ((fd = open(ap_id, O_RDWR)) == -1) {
884 		DBG(2, ("open = ap_id%s, fd%d\n", ap_id, fd));
885 		DBG_F(2, (stderr, "open on %s failed\n", ap_id));
886 		cfga_err(errstring, CMD_OPEN,  ap_id, 0);
887 		return (CFGA_ERROR);
888 	}
889 
890 	if (fstat(fd, &statbuf) == -1) {
891 		DBG(2, ("fstat = ap_id%s, fd%d\n", ap_id, fd));
892 		DBG_F(2, (stderr, "fstat on %s failed\n", ap_id));
893 		cfga_err(errstring, CMD_FSTAT, ap_id, 0);
894 		return (CFGA_ERROR);
895 	}
896 
897 	if ((buff = malloc(MAXPATHLEN)) == NULL) {
898 		cfga_err(errstring, "malloc ", 0);
899 		return (CFGA_ERROR);
900 	}
901 
902 	(void) memset(buff, 0, MAXPATHLEN);
903 
904 	DBG(1, ("ioctl boardtype\n"));
905 
906 	build_control_data(&iocdata, HPC_CTRL_GET_SLOT_INFO,
907 	    (void *)&slot_info);
908 
909 	if (ioctl(fd, DEVCTL_AP_CONTROL, &iocdata) == -1) {
910 		get_logical_name(ap_id, slot_info.pci_slot_name, 0);
911 		DBG(1, ("ioctl failed slotinfo: %s\n",
912 		    slot_info.pci_slot_name));
913 	} else {
914 
915 		/*
916 		 * the driver will report back things like hpc0_slot0
917 		 * this needs to be changed to things like pci1:hpc0_slot0
918 		 */
919 		if (fix_ap_name(buff, ap_id, slot_info.pci_slot_name,
920 		    errstring) != CFGA_OK) {
921 			free(buff);
922 			(void) close(fd);
923 			return (CFGA_ERROR);
924 		}
925 		DBG(1, ("ioctl slotinfo: %s\n", buff));
926 	}
927 
928 	cp = line;
929 	(void) snprintf(cp, len, "%s\t\t", buff);
930 	len -= strlen(cp);
931 	cp += strlen(cp);
932 
933 	free(buff);
934 
935 	build_control_data(&iocdata, HPC_CTRL_GET_LED_STATE, &power_led_info);
936 	if (ioctl(fd, DEVCTL_AP_CONTROL, &iocdata) == -1) {
937 		(void) snprintf(cp, len, "%s=%s,",
938 		    led_strs[power_led_info.led], cfga_strs[UNKNOWN]);
939 		len -= strlen(cp);
940 		cp += strlen(cp);
941 	} else {
942 		(void) snprintf(cp, len, "%s=%s,", led_strs[power_led_info.led],
943 		    mode_strs[power_led_info.state]);
944 		len -= strlen(cp);
945 		cp += strlen(cp);
946 	}
947 
948 	DBG(1, ("%s:%d\n", led_strs[power_led_info.led], power_led_info.state));
949 
950 	build_control_data(&iocdata, HPC_CTRL_GET_LED_STATE, &fault_led_info);
951 	if (ioctl(fd, DEVCTL_AP_CONTROL, &iocdata) == -1) {
952 		(void) snprintf(cp, len, "%s=%s,",
953 		    led_strs[fault_led_info.led], cfga_strs[UNKNOWN]);
954 		len -= strlen(cp);
955 		cp += strlen(cp);
956 	} else {
957 		(void) snprintf(cp, len, "%s=%s,",
958 		    led_strs[fault_led_info.led],
959 		    mode_strs[fault_led_info.state]);
960 		len -= strlen(cp);
961 		cp += strlen(cp);
962 	}
963 	DBG(1, ("%s:%d\n", led_strs[fault_led_info.led], fault_led_info.state));
964 
965 	build_control_data(&iocdata, HPC_CTRL_GET_LED_STATE, &attn_led_info);
966 	if (ioctl(fd, DEVCTL_AP_CONTROL, &iocdata) == -1) {
967 		(void) snprintf(cp, len, "%s=%s,",
968 		    led_strs[attn_led_info.led], cfga_strs[UNKNOWN]);
969 		len -= strlen(cp);
970 		cp += strlen(cp);
971 	} else {
972 		(void) snprintf(cp, len, "%s=%s,",
973 		    led_strs[attn_led_info.led],
974 		    mode_strs[attn_led_info.state]);
975 		len -= strlen(cp);
976 		cp += strlen(cp);
977 	}
978 	DBG(1, ("%s:%d\n", led_strs[attn_led_info.led], attn_led_info.state));
979 
980 	build_control_data(&iocdata, HPC_CTRL_GET_LED_STATE, &active_led_info);
981 	if (ioctl(fd, DEVCTL_AP_CONTROL, &iocdata) == -1) {
982 		(void) snprintf(cp, len, "%s=%s", led_strs[active_led_info.led],
983 		    cfga_strs[UNKNOWN]);
984 	} else {
985 		(void) snprintf(cp, len, "%s=%s",
986 		    led_strs[active_led_info.led],
987 		    mode_strs[active_led_info.state]);
988 	}
989 	cfga_msg(msgp, line);	/* print the message */
990 	DBG(1, ("%s:%d\n", led_strs[active_led_info.led],
991 	    active_led_info.state));
992 
993 	(void) close(fd);
994 
995 	return (CFGA_OK);
996 }
997 
998 /*ARGSUSED*/
999 cfga_err_t
1000 cfga_private_func(const char *function, const char *ap_id,
1001     const char *options, struct cfga_confirm *confp,
1002     struct cfga_msg *msgp, char **errstring, cfga_flags_t flags)
1003 {
1004 	char *str;
1005 	int   len, fd, i = 0, repeat = 0;
1006 	char buf[MAXNAMELEN];
1007 	char ptr;
1008 	hpc_led_info_t	led_info;
1009 	struct hpc_control_data	iocdata;
1010 	cfga_err_t rv;
1011 
1012 	DBG(1, ("cfgadm_private_func: ap_id:%s\n", ap_id));
1013 	DBG(2, ("  options: %s\n", (options == NULL)?"null":options));
1014 	DBG(2, ("  confp: %x\n", confp));
1015 	DBG(2, ("  cfga_msg: %x\n", cfga_msg));
1016 	DBG(2, ("  flag: %d\n", flags));
1017 
1018 	if ((rv = check_options(options)) != CFGA_OK) {
1019 		return (rv);
1020 	}
1021 
1022 	if (private_check == confp)
1023 		repeat = 1;
1024 	else
1025 		private_check = (void*)confp;
1026 
1027 	/* XXX change const 6 to func_str[i] != NULL */
1028 	for (i = 0, str = func_strs[i], len = strlen(str); i < 6; i++) {
1029 		str = func_strs[i];
1030 		len = strlen(str);
1031 		if (strncmp(function, str, len) == 0)
1032 			break;
1033 	}
1034 
1035 	switch (i) {
1036 		case ENABLE_SLOT:
1037 			build_control_data(&iocdata,
1038 				HPC_CTRL_ENABLE_SLOT, 0);
1039 			break;
1040 		case DISABLE_SLOT:
1041 			build_control_data(&iocdata,
1042 				HPC_CTRL_DISABLE_SLOT, 0);
1043 			break;
1044 		case ENABLE_AUTOCNF:
1045 			build_control_data(&iocdata,
1046 				HPC_CTRL_ENABLE_AUTOCFG, 0);
1047 			break;
1048 		case DISABLE_AUTOCNF:
1049 			build_control_data(&iocdata,
1050 				HPC_CTRL_DISABLE_AUTOCFG, 0);
1051 			break;
1052 		case LED:
1053 			/* set mode */
1054 			ptr = function[len++];
1055 			if (ptr == '=') {
1056 				str = (char *)function;
1057 				for (str = (str+len++), i = 0; *str != ',';
1058 				    i++, str++) {
1059 					if (i == (MAXNAMELEN - 1))
1060 						break;
1061 
1062 					buf[i] = *str;
1063 					DBG_F(2, (stdout, "%c\n", buf[i]));
1064 				}
1065 				buf[i] = '\0'; str++;
1066 				DBG(2, ("buf = %s\n", buf));
1067 
1068 				/* ACTIVE=3,ATTN=2,POWER=1,FAULT=0 */
1069 				if (strcmp(buf, led_strs[POWER]) == 0)
1070 					led_info.led = HPC_POWER_LED;
1071 				else if (strcmp(buf, led_strs[FAULT]) == 0)
1072 					led_info.led = HPC_FAULT_LED;
1073 				else if (strcmp(buf, led_strs[ATTN]) == 0)
1074 					led_info.led = HPC_ATTN_LED;
1075 				else if (strcmp(buf, led_strs[ACTIVE]) == 0)
1076 					led_info.led = HPC_ACTIVE_LED;
1077 				else return (CFGA_INVAL);
1078 
1079 				len = strlen(func_strs[MODE]);
1080 				if ((strncmp(str, func_strs[MODE], len) == 0) &&
1081 				    (*(str+(len)) == '=')) {
1082 				    for (str = (str+(++len)), i = 0;
1083 					*str != NULL; i++, str++) {
1084 						buf[i] = *str;
1085 
1086 				    }
1087 				}
1088 				buf[i] = '\0';
1089 				DBG(2, ("buf_mode= %s\n", buf));
1090 
1091 				/* ON = 1, OFF = 0 */
1092 				if (strcmp(buf, mode_strs[ON]) == 0)
1093 					led_info.state = HPC_LED_ON;
1094 				else if (strcmp(buf, mode_strs[OFF]) == 0)
1095 					led_info.state = HPC_LED_OFF;
1096 				else if (strcmp(buf, mode_strs[BLINK]) == 0)
1097 					led_info.state = HPC_LED_BLINK;
1098 				else return (CFGA_INVAL);
1099 
1100 				/* sendin  */
1101 				build_control_data(&iocdata,
1102 				    HPC_CTRL_SET_LED_STATE,
1103 				    (void *)&led_info);
1104 				break;
1105 			} else if (ptr == '\0') {
1106 				/* print mode */
1107 				DBG(1, ("Print mode\n"));
1108 				return (prt_led_mode(ap_id, repeat, errstring,
1109 				    msgp));
1110 			}
1111 		default:
1112 			DBG(1, ("default\n"));
1113 			errno = EINVAL;
1114 			return (CFGA_INVAL);
1115 	}
1116 
1117 	if ((fd = open(ap_id, O_RDWR)) == -1) {
1118 		DBG(1, ("open failed\n"));
1119 		return (CFGA_ERROR);
1120 	}
1121 
1122 	DBG(1, ("open = ap_id=%s, fd=%d\n", ap_id, fd));
1123 
1124 	if (ioctl(fd, DEVCTL_AP_CONTROL, &iocdata) == -1) {
1125 		DBG(1, ("ioctl failed\n"));
1126 		(void) close(fd);
1127 		return (CFGA_ERROR);
1128 	}
1129 
1130 	(void) close(fd);
1131 
1132 	return (CFGA_OK);
1133 }
1134 
1135 /*ARGSUSED*/
1136 cfga_err_t cfga_test(const char *ap_id, const char *options,
1137     struct cfga_msg *msgp, char **errstring, cfga_flags_t flags)
1138 {
1139 	cfga_err_t rv;
1140 	if (errstring != NULL)
1141 		*errstring = NULL;
1142 
1143 	if ((rv = check_options(options)) != CFGA_OK) {
1144 		return (rv);
1145 	}
1146 
1147 	DBG(1, ("cfga_test:(%s)\n", ap_id));
1148 	/* will need to implement pci CTRL command */
1149 	return (CFGA_NOTSUPP);
1150 }
1151 
1152 static int
1153 fixup_slotname(int rval, int *intp, struct searcharg *slotarg)
1154 {
1155 
1156 /*
1157  * The slot-names property describes the external labeling of add-in slots.
1158  * This property is an encoded array, an integer followed by a list of
1159  * strings. The return value from di_prop_lookup_ints for slot-names is -1.
1160  * The expected return value should be the number of elements.
1161  * Di_prop_decode_common does not decode encoded data from software,
1162  * such as the solaris device tree, unlike from the prom.
1163  * Di_prop_decode_common takes the size of the encoded data and mods
1164  * it with the size of int. The size of the encoded data for slot-names is 9
1165  * and the size of int is 4, yielding a non zero result. A value of -1 is used
1166  * to indicate that the number of elements can not be determined.
1167  * Di_prop_decode_common can be modified to decode encoded data from the solaris
1168  * device tree.
1169  */
1170 
1171 	if ((slotarg->slt_name_src == PROM_SLT_NAME) && (rval == -1)) {
1172 		return (DI_WALK_TERMINATE);
1173 	} else {
1174 		int i;
1175 		char *tmptr = (char *)(intp+1);
1176 		DBG(1, ("slot-bitmask: %x \n", *intp));
1177 
1178 		rval = (rval -1) * 4;
1179 
1180 		for (i = 0; i <= slotarg->minor; i++) {
1181 			DBG(2, ("curr slot-name: %s \n", tmptr));
1182 
1183 			if (i >= MAXDEVS)
1184 				return (DI_WALK_TERMINATE);
1185 
1186 			if ((*intp >> i) & 1) {
1187 				/* assign tmptr */
1188 				DBG(2, ("slot-name: %s \n", tmptr));
1189 				if (i == slotarg->minor)
1190 					(void) strcpy(slotarg->slotnames[i],
1191 					    tmptr);
1192 				/* wind tmptr to next \0 */
1193 				while (*tmptr != '\0') {
1194 					tmptr++;
1195 				}
1196 				tmptr++;
1197 			} else {
1198 				/* point at unknown string */
1199 				if (i == slotarg->minor)
1200 					(void) strcpy(slotarg->slotnames[i],
1201 					    "unknown");
1202 			}
1203 		}
1204 	}
1205 	return (DI_WALK_TERMINATE);
1206 }
1207 
1208 static int
1209 find_slotname(di_node_t din, di_minor_t dim, void *arg)
1210 {
1211 	struct searcharg *slotarg = (struct searcharg *)arg;
1212 	di_prom_handle_t ph = (di_prom_handle_t)slotarg->promp;
1213 	di_prom_prop_t	prom_prop;
1214 	di_prop_t	solaris_prop;
1215 	int *intp, rval;
1216 	char *devname;
1217 	char fulldevname[MAXNAMELEN];
1218 
1219 	slotarg->minor = dim->dev_minor % 256;
1220 
1221 	DBG(2, ("minor number:(%i)\n", slotarg->minor));
1222 	DBG(2, ("hot plug slots found so far:(%i)\n", 0));
1223 
1224 	if ((devname = di_devfs_path(din)) != NULL) {
1225 		(void) snprintf(fulldevname, MAXNAMELEN,
1226 		    "/devices%s:%s", devname, di_minor_name(dim));
1227 		di_devfs_path_free(devname);
1228 	}
1229 
1230 	if (strcmp(fulldevname, slotarg->devpath) == 0) {
1231 
1232 		/*
1233 		 * Check the Solaris device tree first
1234 		 * in the case of a DR operation
1235 		 */
1236 		solaris_prop = di_prop_hw_next(din, DI_PROP_NIL);
1237 		while (solaris_prop != DI_PROP_NIL) {
1238 			if (strcmp("slot-names", di_prop_name(solaris_prop))
1239 			    == 0) {
1240 				rval = di_prop_lookup_ints(DDI_DEV_T_ANY,
1241 				    din, di_prop_name(solaris_prop), &intp);
1242 				slotarg->slt_name_src = SOLARIS_SLT_NAME;
1243 
1244 				return (fixup_slotname(rval, intp, slotarg));
1245 			}
1246 			solaris_prop = di_prop_hw_next(din, solaris_prop);
1247 		}
1248 
1249 		/*
1250 		 * Check the prom device tree which is populated at boot.
1251 		 * If this fails, give up and set the slot name to null.
1252 		 */
1253 		prom_prop = di_prom_prop_next(ph, din, DI_PROM_PROP_NIL);
1254 		while (prom_prop != DI_PROM_PROP_NIL) {
1255 			if (strcmp("slot-names", di_prom_prop_name(prom_prop))
1256 			    == 0) {
1257 				rval = di_prom_prop_lookup_ints(ph,
1258 				    din, di_prom_prop_name(prom_prop), &intp);
1259 				slotarg->slt_name_src = PROM_SLT_NAME;
1260 
1261 				return (fixup_slotname(rval, intp, slotarg));
1262 			}
1263 			prom_prop = di_prom_prop_next(ph, din, prom_prop);
1264 		}
1265 		*slotarg->slotnames[slotarg->minor] = '\0';
1266 		return (DI_WALK_TERMINATE);
1267 	} else
1268 		return (DI_WALK_CONTINUE);
1269 }
1270 
1271 static int
1272 find_physical_slot_names(const char *devcomp, struct searcharg *slotarg)
1273 {
1274 	di_node_t root_node;
1275 
1276 	DBG(1, ("find_physical_slot_names\n"));
1277 
1278 	if ((root_node = di_init("/", DINFOCPYALL|DINFOPATH))
1279 		== DI_NODE_NIL) {
1280 		DBG(1, ("di_init() failed\n"));
1281 		return (NULL);
1282 	}
1283 
1284 	slotarg->devpath = (char *)devcomp;
1285 
1286 	if ((slotarg->promp = di_prom_init()) == DI_PROM_HANDLE_NIL) {
1287 		DBG(1, ("di_prom_init() failed\n"));
1288 		di_fini(root_node);
1289 		return (NULL);
1290 	}
1291 
1292 	(void) di_walk_minor(root_node, "ddi_ctl:attachment_point:pci",
1293 		0, (void *)slotarg, find_slotname);
1294 
1295 	di_prom_fini(slotarg->promp);
1296 	di_fini(root_node);
1297 	if (slotarg->slotnames[0] != NULL)
1298 		return (0);
1299 	else
1300 		return (-1);
1301 }
1302 
1303 static void
1304 get_type(hpc_board_type_t boardtype, hpc_card_info_t cardinfo, char *buf)
1305 {
1306 	int i;
1307 
1308 	DBG(1, ("class: %i\n", cardinfo.base_class));
1309 	DBG(1, ("subclass: %i\n", cardinfo.sub_class));
1310 
1311 	if (cardinfo.base_class == PCI_CLASS_NONE) {
1312 		TPCT("unknown");
1313 		return;
1314 	}
1315 
1316 	for (i = 0; i < class_pci_items; i++) {
1317 		if ((cardinfo.base_class == class_pci[i].base_class) &&
1318 		    (cardinfo.sub_class == class_pci[i].sub_class) &&
1319 		    (cardinfo.prog_class == class_pci[i].prog_class)) {
1320 			TPCT(class_pci[i].short_desc);
1321 			break;
1322 		}
1323 	}
1324 
1325 	if (i == class_pci_items)
1326 		TPCT("unknown");
1327 
1328 	TPCT("/");
1329 	switch (boardtype) {
1330 	case HPC_BOARD_PCI_HOTPLUG:
1331 	case HPC_BOARD_CPCI_NON_HS:
1332 	case HPC_BOARD_CPCI_BASIC_HS:
1333 	case HPC_BOARD_CPCI_FULL_HS:
1334 	case HPC_BOARD_CPCI_HS:
1335 		TPCT(board_strs[boardtype]);
1336 		break;
1337 	case HPC_BOARD_UNKNOWN:
1338 	default:
1339 		TPCT(board_strs[HPC_BOARD_UNKNOWN]);
1340 	}
1341 }
1342 
1343 /*
1344  * call-back function for di_devlink_walk
1345  * if the link lives in /dev/cfg copy its name
1346  */
1347 static int
1348 found_devlink(di_devlink_t link, void *ap_log_id)
1349 {
1350 	if (strncmp("/dev/cfg/", di_devlink_path(link), 9) == 0) {
1351 		/* copy everything but /dev/cfg/ */
1352 		(void) strcpy((char *)ap_log_id, di_devlink_path(link) + 9);
1353 		DBG(1, ("found_devlink: %s\n", (char *)ap_log_id));
1354 		return (DI_WALK_TERMINATE);
1355 	}
1356 	return (DI_WALK_CONTINUE);
1357 }
1358 
1359 /*
1360  * Walk throught the cached /dev link tree looking for links to the ap
1361  * if none are found return an error
1362  */
1363 static cfga_err_t
1364 check_devlinks(char *ap_log_id, const char *ap_id)
1365 {
1366 	di_devlink_handle_t hdl;
1367 
1368 	DBG(1, ("check_devlinks: %s\n", ap_id));
1369 
1370 	hdl = di_devlink_init(NULL, 0);
1371 
1372 	if (strncmp("/devices/", ap_id, 9) == 0) {
1373 		/* ap_id is a valid minor_path with /devices prepended */
1374 		(void) di_devlink_walk(hdl, NULL, ap_id + 8, DI_PRIMARY_LINK,
1375 		    (void *)ap_log_id, found_devlink);
1376 	} else {
1377 		DBG(1, ("check_devlinks: invalid ap_id: %s\n", ap_id));
1378 		return (CFGA_ERROR);
1379 	}
1380 
1381 	(void) di_devlink_fini(&hdl);
1382 
1383 	if (ap_log_id[0] != '\0')
1384 		return (CFGA_OK);
1385 	else
1386 		return (CFGA_ERROR);
1387 }
1388 
1389 /*
1390  * most of this is needed to compensate for
1391  * differences between various platforms
1392  */
1393 static cfga_err_t
1394 fix_ap_name(char *ap_log_id, const char *ap_id, char *slot_name,
1395     char **errstring)
1396 {
1397 	char *buf;
1398 	char *tmp;
1399 	char *ptr;
1400 
1401 	di_node_t ap_node;
1402 
1403 	ap_log_id[0] = '\0';
1404 
1405 	if (check_devlinks(ap_log_id, ap_id) == CFGA_OK)
1406 		return (CFGA_OK);
1407 
1408 	DBG(1, ("fix_ap_name: %s\n", ap_id));
1409 
1410 	if ((buf = malloc(strlen(ap_id) + 1)) == NULL) {
1411 		DBG(1, ("malloc failed\n"));
1412 		return (CFGA_ERROR);
1413 	}
1414 	(void) strcpy(buf, ap_id);
1415 	tmp = buf + sizeof ("/devices") - 1;
1416 
1417 	ptr = strchr(tmp, ':');
1418 	ptr[0] = '\0';
1419 
1420 	DBG(1, ("fix_ap_name: %s\n", tmp));
1421 
1422 	ap_node = di_init(tmp, DINFOMINOR);
1423 	if (ap_node == DI_NODE_NIL) {
1424 		cfga_err(errstring, "di_init ", 0);
1425 		DBG(1, ("fix_ap_name: failed to snapshot node\n"));
1426 		return (CFGA_ERROR);
1427 	}
1428 
1429 	(void) snprintf(ap_log_id, strlen(ap_id) + 1, "%s%i:%s",
1430 	    di_driver_name(ap_node), di_instance(ap_node), slot_name);
1431 
1432 	DBG(1, ("fix_ap_name: %s\n", ap_log_id));
1433 
1434 	di_fini(ap_node);
1435 
1436 	free(buf);
1437 	return (CFGA_OK);
1438 }
1439 
1440 /*ARGSUSED*/
1441 cfga_err_t
1442 cfga_list_ext(const char *ap_id, cfga_list_data_t **cs,
1443     int *nlist, const char *options, const char *listopts, char **errstring,
1444     cfga_flags_t flags)
1445 {
1446 	devctl_hdl_t		dcp;
1447 	struct hpc_control_data	iocdata;
1448 	devctl_ap_state_t	state;
1449 	hpc_board_type_t	boardtype;
1450 	hpc_card_info_t		cardinfo;
1451 	hpc_slot_info_t		slot_info;
1452 	struct	searcharg	slotname_arg;
1453 	int			fd;
1454 	int			rv = CFGA_OK;
1455 
1456 	if ((rv = check_options(options)) != CFGA_OK) {
1457 		return (rv);
1458 	}
1459 
1460 	if (errstring != NULL)
1461 		*errstring = NULL;
1462 
1463 	(void) memset(&slot_info, 0, sizeof (hpc_slot_info_t));
1464 
1465 	DBG(1, ("cfga_list_ext:(%s)\n", ap_id));
1466 
1467 	if (cs == NULL || nlist == NULL) {
1468 		rv = CFGA_ERROR;
1469 		return (rv);
1470 	}
1471 
1472 	*nlist = 1;
1473 
1474 	if ((*cs = malloc(sizeof (cfga_list_data_t))) == NULL) {
1475 		cfga_err(errstring, "malloc ", 0);
1476 		DBG(1, ("malloc failed\n"));
1477 		rv = CFGA_ERROR;
1478 		return (rv);
1479 	}
1480 
1481 	if ((dcp = devctl_ap_acquire((char *)ap_id, 0)) == NULL) {
1482 		cfga_err(errstring, CMD_GETSTAT, 0);
1483 		DBG(2, ("cfga_list_ext::(devctl_ap_acquire())\n"));
1484 		rv = CFGA_ERROR;
1485 		return (rv);
1486 	}
1487 
1488 	if (devctl_ap_getstate(dcp, NULL, &state) == -1) {
1489 		cfga_err(errstring, ERR_AP_ERR, ap_id, 0);
1490 		devctl_release((devctl_hdl_t)dcp);
1491 		DBG(2, ("cfga_list_ext::(devctl_ap_getstate())\n"));
1492 		rv = CFGA_ERROR;
1493 		return (rv);
1494 	}
1495 
1496 	switch (state.ap_rstate) {
1497 		case AP_RSTATE_EMPTY:
1498 			(*cs)->ap_r_state = CFGA_STAT_EMPTY;
1499 			DBG(2, ("ap_rstate = CFGA_STAT_EMPTY\n"));
1500 			break;
1501 		case AP_RSTATE_DISCONNECTED:
1502 			(*cs)->ap_r_state = CFGA_STAT_DISCONNECTED;
1503 			DBG(2, ("ap_rstate = CFGA_STAT_DISCONNECTED\n"));
1504 			break;
1505 		case AP_RSTATE_CONNECTED:
1506 			(*cs)->ap_r_state = CFGA_STAT_CONNECTED;
1507 			DBG(2, ("ap_rstate = CFGA_STAT_CONNECTED\n"));
1508 			break;
1509 	default:
1510 		cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
1511 		rv = CFGA_ERROR;
1512 		devctl_release((devctl_hdl_t)dcp);
1513 		return (rv);
1514 	}
1515 
1516 	switch (state.ap_ostate) {
1517 		case AP_OSTATE_CONFIGURED:
1518 			(*cs)->ap_o_state = CFGA_STAT_CONFIGURED;
1519 			DBG(2, ("ap_ostate = CFGA_STAT_CONFIGURED\n"));
1520 			break;
1521 		case AP_OSTATE_UNCONFIGURED:
1522 			(*cs)->ap_o_state = CFGA_STAT_UNCONFIGURED;
1523 			DBG(2, ("ap_ostate = CFGA_STAT_UNCONFIGURED\n"));
1524 			break;
1525 	default:
1526 		cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
1527 		rv = CFGA_ERROR;
1528 		devctl_release((devctl_hdl_t)dcp);
1529 		return (rv);
1530 	}
1531 
1532 	switch (state.ap_condition) {
1533 		case AP_COND_OK:
1534 			(*cs)->ap_cond = CFGA_COND_OK;
1535 			DBG(2, ("ap_cond = CFGA_COND_OK\n"));
1536 			break;
1537 		case AP_COND_FAILING:
1538 			(*cs)->ap_cond = CFGA_COND_FAILING;
1539 			DBG(2, ("ap_cond = CFGA_COND_FAILING\n"));
1540 			break;
1541 		case AP_COND_FAILED:
1542 			(*cs)->ap_cond = CFGA_COND_FAILED;
1543 			DBG(2, ("ap_cond = CFGA_COND_FAILED\n"));
1544 			break;
1545 		case AP_COND_UNUSABLE:
1546 			(*cs)->ap_cond = CFGA_COND_UNUSABLE;
1547 			DBG(2, ("ap_cond = CFGA_COND_UNUSABLE\n"));
1548 			break;
1549 		case AP_COND_UNKNOWN:
1550 			(*cs)->ap_cond = CFGA_COND_UNKNOWN;
1551 			DBG(2, ("ap_cond = CFGA_COND_UNKNOW\n"));
1552 			break;
1553 	default:
1554 		cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
1555 		rv = CFGA_ERROR;
1556 		devctl_release((devctl_hdl_t)dcp);
1557 		return (rv);
1558 	}
1559 	(*cs)->ap_busy = (int)state.ap_in_transition;
1560 
1561 	devctl_release((devctl_hdl_t)dcp);
1562 
1563 	if ((fd = open(ap_id, O_RDWR)) == -1) {
1564 		cfga_err(errstring, ERR_AP_ERR, ap_id, 0);
1565 		(*cs)->ap_status_time = 0;
1566 		boardtype = HPC_BOARD_UNKNOWN;
1567 		cardinfo.base_class = PCI_CLASS_NONE;
1568 		get_logical_name(ap_id, slot_info.pci_slot_name, 0);
1569 		DBG(2, ("open on %s failed\n", ap_id));
1570 		goto cont;
1571 	}
1572 	DBG(1, ("open = ap_id=%s, fd=%d\n", ap_id, fd));
1573 
1574 	(*cs)->ap_status_time = state.ap_last_change;
1575 
1576 	/* need board type and a way to get to hpc_slot_info */
1577 	build_control_data(&iocdata, HPC_CTRL_GET_BOARD_TYPE,
1578 	    (void *)&boardtype);
1579 
1580 	if (ioctl(fd, DEVCTL_AP_CONTROL, &iocdata) == -1) {
1581 		boardtype = HPC_BOARD_UNKNOWN;
1582 	}
1583 	DBG(1, ("ioctl boardtype\n"));
1584 
1585 	build_control_data(&iocdata, HPC_CTRL_GET_SLOT_INFO,
1586 	    (void *)&slot_info);
1587 
1588 	if (ioctl(fd, DEVCTL_AP_CONTROL, &iocdata) == -1) {
1589 		get_logical_name(ap_id, slot_info.pci_slot_name, 0);
1590 		DBG(1, ("ioctl failed slotinfo: %s\n",
1591 		    slot_info.pci_slot_name));
1592 	} else {
1593 
1594 		/*
1595 		 * the driver will report back things like hpc0_slot0
1596 		 * this needs to be changed to things like pci1:hpc0_slot0
1597 		 */
1598 		rv = fix_ap_name((*cs)->ap_log_id,
1599 		    ap_id, slot_info.pci_slot_name, errstring);
1600 		DBG(1, ("ioctl slotinfo: %s\n", (*cs)->ap_log_id));
1601 	}
1602 
1603 	build_control_data(&iocdata, HPC_CTRL_GET_CARD_INFO,
1604 	    (void *)&cardinfo);
1605 
1606 	if (ioctl(fd, DEVCTL_AP_CONTROL, &iocdata) == -1) {
1607 		DBG(1, ("ioctl failed\n"));
1608 		cardinfo.base_class = PCI_CLASS_NONE;
1609 	}
1610 
1611 	DBG(1, ("ioctl cardinfo: %d\n", cardinfo.base_class));
1612 	DBG(1, ("ioctl subclass: %d\n", cardinfo.sub_class));
1613 	DBG(1, ("ioctl headertype: %d\n", cardinfo.header_type));
1614 
1615 	(void) close(fd);
1616 
1617 cont:
1618 	(void) strcpy((*cs)->ap_phys_id, ap_id);    /* physical path of AP */
1619 	if ((*cs)->ap_log_id[0] == '\0')
1620 		(void) strcpy((*cs)->ap_log_id, slot_info.pci_slot_name);
1621 
1622 	/* slot_names of bus node  */
1623 	if (find_physical_slot_names(ap_id, &slotname_arg) != -1)
1624 		(void) strcpy((*cs)->ap_info,
1625 		    slotname_arg.slotnames[slotname_arg.minor]);
1626 
1627 	(void) memset((*cs)->ap_type, 0, CFGA_TYPE_LEN);
1628 	/* class_code/subclass/boardtype */
1629 	get_type(boardtype, cardinfo, (*cs)->ap_type);
1630 
1631 	DBG(1, ("cfga_list_ext return success\n"));
1632 	rv = CFGA_OK;
1633 
1634 	return (rv);
1635 }
1636 
1637 /*
1638  * This routine prints a single line of help message
1639  */
1640 static void
1641 cfga_msg(struct cfga_msg *msgp, const char *str)
1642 {
1643 	DBG(2, ("<%s>", str));
1644 
1645 	if (msgp == NULL || msgp->message_routine == NULL)
1646 		return;
1647 
1648 	(*msgp->message_routine)(msgp->appdata_ptr, str);
1649 	(*msgp->message_routine)(msgp->appdata_ptr, "\n");
1650 }
1651 
1652 static cfga_err_t
1653 check_options(const char *options)
1654 {
1655 	struct cfga_msg *msgp = NULL;
1656 
1657 	if (options) {
1658 		cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_UNKNOWN]));
1659 		cfga_msg(msgp, options);
1660 		return (CFGA_INVAL);
1661 	}
1662 	return (CFGA_OK);
1663 }
1664 
1665 /*ARGSUSED*/
1666 cfga_err_t
1667 cfga_help(struct cfga_msg *msgp, const char *options, cfga_flags_t flags)
1668 {
1669 	if (options) {
1670 		cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_UNKNOWN]));
1671 		cfga_msg(msgp, options);
1672 	}
1673 	DBG(1, ("cfga_help\n"));
1674 
1675 	cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_HEADER]));
1676 	cfga_msg(msgp, cfga_strs[HELP_CONFIG]);
1677 	cfga_msg(msgp, cfga_strs[HELP_ENABLE_SLOT]);
1678 	cfga_msg(msgp, cfga_strs[HELP_DISABLE_SLOT]);
1679 	cfga_msg(msgp, cfga_strs[HELP_ENABLE_AUTOCONF]);
1680 	cfga_msg(msgp, cfga_strs[HELP_DISABLE_AUTOCONF]);
1681 	cfga_msg(msgp, cfga_strs[HELP_LED_CNTRL]);
1682 	return (CFGA_OK);
1683 }
1684 
1685 /*
1686  * cfga_err() accepts a variable number of message IDs and constructs
1687  * a corresponding error string which is returned via the errstring argument.
1688  * cfga_err() calls gettext() to internationalize proper messages.
1689  */
1690 static void
1691 cfga_err(char **errstring, ...)
1692 {
1693 	int a;
1694 	int i;
1695 	int n;
1696 	int len;
1697 	int flen;
1698 	char *p;
1699 	char *q;
1700 	char *s[32];
1701 	char *failed;
1702 	va_list ap;
1703 
1704 	/*
1705 	 * If errstring is null it means user in not interested in getting
1706 	 * error status. So we don't do all the work
1707 	 */
1708 	if (errstring == NULL) {
1709 		return;
1710 	}
1711 	va_start(ap, errstring);
1712 
1713 	failed = dgettext(TEXT_DOMAIN, cfga_strs[FAILED]);
1714 	flen = strlen(failed);
1715 
1716 	for (n = len = 0; (a = va_arg(ap, int)) != 0; n++) {
1717 		switch (a) {
1718 		case CMD_GETSTAT:
1719 		case CMD_LIST:
1720 		case CMD_SLOT_CONNECT:
1721 		case CMD_SLOT_DISCONNECT:
1722 		case CMD_SLOT_CONFIGURE:
1723 		case CMD_SLOT_UNCONFIGURE:
1724 			p =  cfga_errstrs(a);
1725 			len += (strlen(p) + flen);
1726 			s[n] = p;
1727 			s[++n] = cfga_strs[FAILED];
1728 
1729 			DBG(2, ("<%s>", p));
1730 			DBG(2, (cfga_strs[FAILED]));
1731 			break;
1732 
1733 		case ERR_CMD_INVAL:
1734 		case ERR_AP_INVAL:
1735 		case ERR_OPT_INVAL:
1736 		case ERR_AP_ERR:
1737 			switch (a) {
1738 			case ERR_CMD_INVAL:
1739 				p = dgettext(TEXT_DOMAIN,
1740 				    cfga_errstrs[ERR_CMD_INVAL]);
1741 				break;
1742 			case ERR_AP_INVAL:
1743 				p = dgettext(TEXT_DOMAIN,
1744 				    cfga_errstrs[ERR_AP_INVAL]);
1745 				break;
1746 			case ERR_OPT_INVAL:
1747 				p = dgettext(TEXT_DOMAIN,
1748 				    cfga_errstrs[ERR_OPT_INVAL]);
1749 				break;
1750 			case ERR_AP_ERR:
1751 				p = dgettext(TEXT_DOMAIN,
1752 				    cfga_errstrs[ERR_AP_ERR]);
1753 				break;
1754 			}
1755 
1756 			if ((q = va_arg(ap, char *)) != NULL) {
1757 				len += (strlen(p) + strlen(q));
1758 				s[n] = p;
1759 				s[++n] = q;
1760 				DBG(2, ("<%s>", p));
1761 				DBG(2, ("<%s>", q));
1762 				break;
1763 			} else {
1764 				len += strlen(p);
1765 				s[n] = p;
1766 
1767 			}
1768 			DBG(2, ("<%s>", p));
1769 			break;
1770 
1771 		default:
1772 			n--;
1773 			break;
1774 		}
1775 	}
1776 
1777 	DBG(2, ("\n"));
1778 	va_end(ap);
1779 
1780 	if ((p = calloc(len + 1, 1)) == NULL)
1781 		return;
1782 
1783 	for (i = 0; i < n; i++) {
1784 		(void) strlcat(p, s[i], len + 1);
1785 		DBG(2, ("i:%d, %s\n", i, s[i]));
1786 	}
1787 
1788 	*errstring = p;
1789 #ifdef	DEBUG
1790 	printf("%s\n", *errstring);
1791 	free(*errstring);
1792 #endif
1793 }
1794 
1795 /*
1796  * cfga_ap_id_cmp -- use default_ap_id_cmp() in libcfgadm
1797  */
1798