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