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