xref: /illumos-gate/usr/src/lib/cfgadm_plugins/shp/common/shp.c (revision dfc115332c94a2f62058ac7f2bce7631fbd20b3d)
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  * Copyright (c) 2016 by Delphix. All rights reserved.
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 the 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 	uint_t		hpflags = 0;
623 	hp_node_t	node;
624 	hp_node_t	results = NULL;
625 
626 	if ((rv = check_options(options)) != CFGA_OK) {
627 		return (rv);
628 	}
629 
630 	if (errstring != NULL)
631 		*errstring = NULL;
632 
633 	rv = CFGA_OK;
634 	DBG(1, ("cfga_change_state:(%s)\n", ap_id));
635 
636 	rv = physpath2node(ap_id, errstring, &node);
637 	if (rv != CFGA_OK)
638 		return (rv);
639 
640 	/*
641 	 * Check for the FORCE flag.  It is only used
642 	 * for DISCONNECT or UNCONFIGURE state changes.
643 	 */
644 	if (flags & CFGA_FLAG_FORCE)
645 		hpflags |= HPFORCE;
646 
647 	state = hp_state(node);
648 
649 	/*
650 	 * Which state should we drive to ?
651 	 */
652 	if ((state_change_cmd != CFGA_CMD_LOAD) &&
653 	    (state_change_cmd != CFGA_CMD_UNLOAD)) {
654 		if (cfga_target_state(state_change_cmd,
655 		    &new_state) != CFGA_OK) {
656 			hp_fini(node);
657 			return (CFGA_ERROR);
658 		}
659 	}
660 
661 	DBG(1, ("cfga_change_state: state is %d\n", state));
662 	switch (state_change_cmd) {
663 	case CFGA_CMD_CONNECT:
664 		DBG(1, ("connect\n"));
665 		if (state == DDI_HP_CN_STATE_EMPTY) {
666 			cfga_err(errstring, ERR_AP_ERR, 0);
667 			rv = CFGA_INVAL;
668 		} else if (state == DDI_HP_CN_STATE_PRESENT) {
669 			/* Connect the slot */
670 			if (hp_set_state(node, 0, new_state, &results) != 0) {
671 				rv = CFGA_ERROR;
672 				cfga_err(errstring, CMD_SLOT_CONNECT, 0);
673 			}
674 		}
675 		break;
676 
677 	case CFGA_CMD_DISCONNECT:
678 		DBG(1, ("disconnect\n"));
679 		if (state == DDI_HP_CN_STATE_EMPTY) {
680 			cfga_err(errstring, ERR_AP_ERR, 0);
681 			rv = CFGA_INVAL;
682 		} else if (state > DDI_HP_CN_STATE_PRESENT) {
683 			/* Disconnect the slot */
684 			rv = hp_set_state(node, hpflags, new_state, &results);
685 			if (rv != 0) {
686 				if (rv == EBUSY)
687 					rv = CFGA_BUSY;
688 				else
689 					rv = CFGA_ERROR;
690 
691 				if (results) {
692 					pci_rcm_info_table(results, errstring);
693 					hp_fini(results);
694 				} else {
695 					cfga_err(errstring,
696 					    CMD_SLOT_DISCONNECT, 0);
697 				}
698 			}
699 		}
700 		break;
701 
702 	case CFGA_CMD_CONFIGURE:
703 		/*
704 		 * for multi-func device we allow multiple
705 		 * configure on the same slot because one
706 		 * func can be configured and other one won't
707 		 */
708 		DBG(1, ("configure\n"));
709 		if (state == DDI_HP_CN_STATE_EMPTY) {
710 			cfga_err(errstring, ERR_AP_ERR, 0);
711 			rv = CFGA_INVAL;
712 		} else if (hp_set_state(node, 0, new_state, &results) != 0) {
713 			rv = CFGA_ERROR;
714 			cfga_err(errstring, CMD_SLOT_CONFIGURE, 0);
715 		}
716 		break;
717 
718 	case CFGA_CMD_UNCONFIGURE:
719 		DBG(1, ("unconfigure\n"));
720 		if (state == DDI_HP_CN_STATE_EMPTY) {
721 			cfga_err(errstring, ERR_AP_ERR, 0);
722 			rv = CFGA_INVAL;
723 		} else if (state >= DDI_HP_CN_STATE_ENABLED) {
724 			rv = hp_set_state(node, hpflags, new_state, &results);
725 			if (rv != 0) {
726 				if (rv == EBUSY)
727 					rv = CFGA_BUSY;
728 				else
729 					rv = CFGA_ERROR;
730 
731 				if (results) {
732 					pci_rcm_info_table(results, errstring);
733 					hp_fini(results);
734 				} else {
735 					cfga_err(errstring,
736 					    CMD_SLOT_UNCONFIGURE, 0);
737 				}
738 			}
739 		}
740 		DBG(1, ("unconfigure rv:(%i)\n", rv));
741 		break;
742 
743 	case CFGA_CMD_LOAD:
744 		/* do nothing, just produce error msg as is */
745 		if (state < DDI_HP_CN_STATE_POWERED) {
746 			rv = CFGA_ERROR;
747 			cfga_err(errstring, CMD_SLOT_INSERT, 0);
748 		} else {
749 			cfga_err(errstring, ERR_AP_ERR, 0);
750 			rv = CFGA_INVAL;
751 		}
752 		break;
753 
754 	case CFGA_CMD_UNLOAD:
755 		/* do nothing, just produce error msg as is */
756 		if (state < DDI_HP_CN_STATE_POWERED) {
757 			rv = CFGA_ERROR;
758 			cfga_err(errstring, CMD_SLOT_REMOVE, 0);
759 		} else {
760 			cfga_err(errstring, ERR_AP_ERR, 0);
761 			rv = CFGA_INVAL;
762 		}
763 		break;
764 
765 	default:
766 		rv = CFGA_OPNOTSUPP;
767 		break;
768 	}
769 
770 	hp_fini(node);
771 	return (rv);
772 }
773 
774 char *
775 get_val_from_result(char *result)
776 {
777 	char *tmp;
778 
779 	tmp = strchr(result, '=');
780 	if (tmp == NULL)
781 		return (NULL);
782 
783 	tmp++;
784 	return (tmp);
785 }
786 
787 static cfga_err_t
788 prt_led_mode(const char *ap_id, int repeat, char **errstring,
789     struct cfga_msg *msgp)
790 {
791 	pciehpc_led_t led;
792 	hp_node_t node;
793 	char *buff;
794 	char *buf;
795 	char *cp, line[MAXLINE];
796 	char *tmp;
797 	char *format;
798 	char *result;
799 	int i, n, rv;
800 	int len = MAXLINE;
801 
802 	pciehpc_led_t states[] = {
803 		PCIEHPC_POWER_LED,
804 		PCIEHPC_FAULT_LED,
805 		PCIEHPC_ATTN_LED,
806 		PCIEHPC_ACTIVE_LED
807 	};
808 
809 	DBG(1, ("prt_led_mod function\n"));
810 	if (!repeat)
811 		cfga_msg(msgp, "Ap_Id\t\t\tLed");
812 
813 	rv = physpath2node(ap_id, errstring, &node);
814 	if (rv != CFGA_OK)
815 		return (rv);
816 
817 	if ((buff = malloc(MAXPATHLEN)) == NULL) {
818 		hp_fini(node);
819 		cfga_err(errstring, "malloc ", 0);
820 		return (CFGA_ERROR);
821 	}
822 
823 	(void) memset(buff, 0, MAXPATHLEN);
824 
825 	if (fix_ap_name(buff, ap_id, hp_name(node),
826 	    errstring) != CFGA_OK) {
827 		hp_fini(node);
828 		free(buff);
829 		return (CFGA_ERROR);
830 	}
831 
832 	cp = line;
833 	(void) snprintf(cp, len, "%s\t\t", buff);
834 	len -= strlen(cp);
835 	cp += strlen(cp);
836 
837 	free(buff);
838 
839 	n = sizeof (states)/sizeof (pciehpc_led_t);
840 	for (i = 0; i < n; i++) {
841 		led = states[i];
842 
843 		format = (i == n - 1) ? "%s=%s" : "%s=%s,";
844 		if (hp_get_private(node, led_strs2[led], &result) != 0) {
845 			(void) snprintf(cp, len, format,
846 			    led_strs[led], cfga_strs[UNKNOWN]);
847 			len -= strlen(cp);
848 			cp += strlen(cp);
849 			DBG(1, ("%s:%s\n", led_strs[led], cfga_strs[UNKNOWN]));
850 		} else {
851 			/*
852 			 * hp_get_private() will return back things like
853 			 * "led_fault=off", transform it to cfgadm desired
854 			 * format.
855 			 */
856 			tmp = get_val_from_result(result);
857 			if (tmp == NULL) {
858 				free(result);
859 				hp_fini(node);
860 				return (CFGA_ERROR);
861 			}
862 
863 			(void) snprintf(cp, len, format,
864 			    led_strs[led], tmp);
865 			len -= strlen(cp);
866 			cp += strlen(cp);
867 			DBG(1, ("%s:%s\n", led_strs[led], tmp));
868 			free(result);
869 		}
870 	}
871 
872 	cfga_msg(msgp, line);	/* print the message */
873 
874 	hp_fini(node);
875 
876 	return (CFGA_OK);
877 }
878 
879 /*ARGSUSED*/
880 cfga_err_t
881 cfga_private_func(const char *function, const char *ap_id,
882     const char *options, struct cfga_confirm *confp,
883     struct cfga_msg *msgp, char **errstring, cfga_flags_t flags)
884 {
885 	char *str;
886 	int   len, fd, i = 0, repeat = 0;
887 	char buf[MAXNAMELEN];
888 	char ptr;
889 	cfga_err_t rv;
890 	char *led, *mode;
891 	hp_node_t node;
892 	char *result;
893 
894 	DBG(1, ("cfgadm_private_func: ap_id:%s\n", ap_id));
895 	DBG(2, ("  options: %s\n", (options == NULL)?"null":options));
896 	DBG(2, ("  confp: %x\n", confp));
897 	DBG(2, ("  cfga_msg: %x\n", cfga_msg));
898 	DBG(2, ("  flag: %d\n", flags));
899 
900 	if ((rv = check_options(options)) != CFGA_OK) {
901 		return (rv);
902 	}
903 
904 	if (private_check == confp)
905 		repeat = 1;
906 	else
907 		private_check = (void*)confp;
908 
909 	for (i = 0, str = func_strs[i], len = strlen(str);
910 	    func_strs[i] != NULL; i++) {
911 		str = func_strs[i];
912 		len = strlen(str);
913 		if (strncmp(function, str, len) == 0)
914 			break;
915 	}
916 
917 	switch (i) {
918 		case ENABLE_SLOT:
919 		case DISABLE_SLOT:
920 			/* pass through */
921 		case ENABLE_AUTOCNF:
922 		case DISABLE_AUTOCNF:
923 			/* no action needed */
924 			return (CFGA_OK);
925 			break;
926 		case LED:
927 			/* set mode */
928 			ptr = function[len++];
929 			if (ptr == '=') {
930 				str = (char *)function;
931 				for (str = (str+len++), i = 0; *str != ',';
932 				    i++, str++) {
933 					if (i == (MAXNAMELEN - 1))
934 						break;
935 
936 					buf[i] = *str;
937 					DBG_F(2, (stdout, "%c\n", buf[i]));
938 				}
939 				buf[i] = '\0'; str++;
940 				DBG(2, ("buf = %s\n", buf));
941 
942 				/* ACTIVE=3,ATTN=2,POWER=1,FAULT=0 */
943 				if (strcmp(buf, led_strs[POWER]) == 0)
944 					led = PCIEHPC_PROP_LED_POWER;
945 				else if (strcmp(buf, led_strs[FAULT]) == 0)
946 					led = PCIEHPC_PROP_LED_FAULT;
947 				else if (strcmp(buf, led_strs[ATTN]) == 0)
948 					led = PCIEHPC_PROP_LED_ATTN;
949 				else if (strcmp(buf, led_strs[ACTIVE]) == 0)
950 					led = PCIEHPC_PROP_LED_ACTIVE;
951 				else return (CFGA_INVAL);
952 
953 				len = strlen(func_strs[MODE]);
954 				if ((strncmp(str, func_strs[MODE], len) == 0) &&
955 				    (*(str+(len)) == '=')) {
956 					for (str = (str+(++len)), i = 0;
957 					    *str != NULL; i++, str++) {
958 						buf[i] = *str;
959 					}
960 				}
961 				buf[i] = '\0';
962 				DBG(2, ("buf_mode= %s\n", buf));
963 
964 				/* ON = 1, OFF = 0 */
965 				if (strcmp(buf, mode_strs[ON]) == 0)
966 					mode = PCIEHPC_PROP_VALUE_ON;
967 				else if (strcmp(buf, mode_strs[OFF]) == 0)
968 					mode = PCIEHPC_PROP_VALUE_OFF;
969 				else if (strcmp(buf, mode_strs[BLINK]) == 0)
970 					mode = PCIEHPC_PROP_VALUE_BLINK;
971 				else return (CFGA_INVAL);
972 
973 				/* sendin  */
974 				memset(buf, 0, sizeof (buf));
975 				snprintf(buf, sizeof (buf), "%s=%s",
976 				    led, mode);
977 				buf[MAXNAMELEN - 1] = '\0';
978 
979 				break;
980 			} else if (ptr == '\0') {
981 				/* print mode */
982 				DBG(1, ("Print mode\n"));
983 				return (prt_led_mode(ap_id, repeat, errstring,
984 				    msgp));
985 			}
986 		default:
987 			DBG(1, ("default\n"));
988 			errno = EINVAL;
989 			return (CFGA_INVAL);
990 	}
991 
992 	rv = physpath2node(ap_id, errstring, &node);
993 	if (rv != CFGA_OK)
994 		return (rv);
995 
996 	if (hp_set_private(node, buf, &result) != 0) {
997 		hp_fini(node);
998 		return (CFGA_ERROR);
999 	}
1000 
1001 	hp_fini(node);
1002 	return (CFGA_OK);
1003 }
1004 
1005 /*ARGSUSED*/
1006 cfga_err_t cfga_test(const char *ap_id, const char *options,
1007     struct cfga_msg *msgp, char **errstring, cfga_flags_t flags)
1008 {
1009 	cfga_err_t rv;
1010 	if (errstring != NULL)
1011 		*errstring = NULL;
1012 
1013 	if ((rv = check_options(options)) != CFGA_OK) {
1014 		return (rv);
1015 	}
1016 
1017 	DBG(1, ("cfga_test:(%s)\n", ap_id));
1018 	/* will need to implement pci CTRL command */
1019 	return (CFGA_NOTSUPP);
1020 }
1021 
1022 /*
1023  * The slot-names property describes the external labeling of add-in slots.
1024  * This property is an encoded array, an integer followed by a list of
1025  * strings. The return value from di_prop_lookup_ints for slot-names is -1.
1026  * The expected return value should be the number of elements.
1027  * Di_prop_decode_common does not decode encoded data from software,
1028  * such as the solaris device tree, unlike from the prom.
1029  * Di_prop_decode_common takes the size of the encoded data and mods
1030  * it with the size of int. The size of the encoded data for slot-names is 9
1031  * and the size of int is 4, yielding a non zero result. A value of -1 is used
1032  * to indicate that the number of elements can not be determined.
1033  * Di_prop_decode_common can be modified to decode encoded data from the solaris
1034  * device tree.
1035  */
1036 static int
1037 fixup_slotname(int rval, int *intp, struct searcharg *slotarg)
1038 {
1039 	if ((slotarg->slt_name_src == PROM_SLT_NAME) && (rval == -1)) {
1040 		return (DI_WALK_TERMINATE);
1041 	} else {
1042 		int i;
1043 		char *tmptr = (char *)(intp+1);
1044 		DBG(1, ("slot-bitmask: %x \n", *intp));
1045 
1046 		rval = (rval -1) * 4;
1047 
1048 		for (i = 0; i <= slotarg->minor; i++) {
1049 			DBG(2, ("curr slot-name: %s \n", tmptr));
1050 
1051 			if (i >= MAXDEVS)
1052 				return (DI_WALK_TERMINATE);
1053 
1054 			if ((*intp >> i) & 1) {
1055 				/* assign tmptr */
1056 				DBG(2, ("slot-name: %s \n", tmptr));
1057 				if (i == slotarg->minor)
1058 					(void) strcpy(slotarg->slotnames[i],
1059 					    tmptr);
1060 				/* wind tmptr to next \0 */
1061 				while (*tmptr != '\0') {
1062 					tmptr++;
1063 				}
1064 				tmptr++;
1065 			} else {
1066 				/* point at unknown string */
1067 				if (i == slotarg->minor)
1068 					(void) strcpy(slotarg->slotnames[i],
1069 					    "unknown");
1070 			}
1071 		}
1072 	}
1073 	return (DI_WALK_TERMINATE);
1074 }
1075 
1076 static int
1077 find_slotname(di_node_t din, di_minor_t dim, void *arg)
1078 {
1079 	struct searcharg *slotarg = (struct searcharg *)arg;
1080 	di_prom_handle_t ph = (di_prom_handle_t)slotarg->promp;
1081 	di_prom_prop_t	prom_prop;
1082 	di_prop_t	solaris_prop;
1083 	int *intp, rval;
1084 	char *devname;
1085 	char fulldevname[MAXNAMELEN];
1086 
1087 	slotarg->minor = dim->dev_minor % 256;
1088 
1089 	DBG(2, ("minor number:(%i)\n", slotarg->minor));
1090 	DBG(2, ("hot plug slots found so far:(%i)\n", 0));
1091 
1092 	if ((devname = di_devfs_path(din)) != NULL) {
1093 		(void) snprintf(fulldevname, MAXNAMELEN,
1094 		    "/devices%s:%s", devname, di_minor_name(dim));
1095 		di_devfs_path_free(devname);
1096 	}
1097 
1098 	if (strcmp(fulldevname, slotarg->devpath) == 0) {
1099 
1100 		/*
1101 		 * Check the Solaris device tree first
1102 		 * in the case of a DR operation
1103 		 */
1104 		solaris_prop = di_prop_hw_next(din, DI_PROP_NIL);
1105 		while (solaris_prop != DI_PROP_NIL) {
1106 			if (strcmp("slot-names", di_prop_name(solaris_prop))
1107 			    == 0) {
1108 				rval = di_prop_lookup_ints(DDI_DEV_T_ANY,
1109 				    din, di_prop_name(solaris_prop), &intp);
1110 				slotarg->slt_name_src = SOLARIS_SLT_NAME;
1111 
1112 				return (fixup_slotname(rval, intp, slotarg));
1113 			}
1114 			solaris_prop = di_prop_hw_next(din, solaris_prop);
1115 		}
1116 
1117 		/*
1118 		 * Check the prom device tree which is populated at boot.
1119 		 * If this fails, give up and set the slot name to null.
1120 		 */
1121 		prom_prop = di_prom_prop_next(ph, din, DI_PROM_PROP_NIL);
1122 		while (prom_prop != DI_PROM_PROP_NIL) {
1123 			if (strcmp("slot-names", di_prom_prop_name(prom_prop))
1124 			    == 0) {
1125 				rval = di_prom_prop_lookup_ints(ph,
1126 				    din, di_prom_prop_name(prom_prop), &intp);
1127 				slotarg->slt_name_src = PROM_SLT_NAME;
1128 
1129 				return (fixup_slotname(rval, intp, slotarg));
1130 			}
1131 			prom_prop = di_prom_prop_next(ph, din, prom_prop);
1132 		}
1133 		*slotarg->slotnames[slotarg->minor] = '\0';
1134 		return (DI_WALK_TERMINATE);
1135 	} else
1136 		return (DI_WALK_CONTINUE);
1137 }
1138 
1139 static int
1140 find_physical_slot_names(const char *devcomp, struct searcharg *slotarg)
1141 {
1142 	di_node_t root_node;
1143 
1144 	DBG(1, ("find_physical_slot_names\n"));
1145 
1146 	if ((root_node = di_init("/", DINFOCPYALL|DINFOPATH))
1147 	    == DI_NODE_NIL) {
1148 		DBG(1, ("di_init() failed\n"));
1149 		return (-1);
1150 	}
1151 
1152 	slotarg->devpath = (char *)devcomp;
1153 
1154 	if ((slotarg->promp = di_prom_init()) == DI_PROM_HANDLE_NIL) {
1155 		DBG(1, ("di_prom_init() failed\n"));
1156 		di_fini(root_node);
1157 		return (-1);
1158 	}
1159 
1160 	(void) di_walk_minor(root_node, "ddi_ctl:attachment_point:pci",
1161 	    0, (void *)slotarg, find_slotname);
1162 
1163 	di_prom_fini(slotarg->promp);
1164 	di_fini(root_node);
1165 	if (slotarg->slotnames[0] != NULL)
1166 		return (0);
1167 	else
1168 		return (-1);
1169 }
1170 
1171 static void
1172 get_type(const char *boardtype, const char *cardtype, char *buf)
1173 {
1174 /* for type string assembly in get_type() */
1175 #define	TPCT(s)	(void) strlcat(buf, (s), CFGA_TYPE_LEN)
1176 
1177 	int i;
1178 
1179 	if (strcmp(cardtype, "unknown") == 0) {
1180 		TPCT("unknown");
1181 		return;
1182 	}
1183 
1184 	TPCT(cardtype);
1185 	TPCT("/");
1186 
1187 	if (strcmp(boardtype, PCIEHPC_PROP_VALUE_PCIHOTPLUG) == 0)
1188 		TPCT(board_strs[PCIEHPC_BOARD_PCI_HOTPLUG]);
1189 	else
1190 		TPCT(board_strs[PCIEHPC_BOARD_UNKNOWN]);
1191 }
1192 
1193 /*
1194  * call-back function for di_devlink_walk
1195  * if the link lives in /dev/cfg copy its name
1196  */
1197 static int
1198 found_devlink(di_devlink_t link, void *ap_log_id)
1199 {
1200 	if (strncmp("/dev/cfg/", di_devlink_path(link), 9) == 0) {
1201 		/* copy everything but /dev/cfg/ */
1202 		(void) strcpy((char *)ap_log_id, di_devlink_path(link) + 9);
1203 		DBG(1, ("found_devlink: %s\n", (char *)ap_log_id));
1204 		return (DI_WALK_TERMINATE);
1205 	}
1206 	return (DI_WALK_CONTINUE);
1207 }
1208 
1209 /*
1210  * Walk throught the cached /dev link tree looking for links to the ap
1211  * if none are found return an error
1212  */
1213 static cfga_err_t
1214 check_devlinks(char *ap_log_id, const char *ap_id)
1215 {
1216 	di_devlink_handle_t hdl;
1217 
1218 	DBG(1, ("check_devlinks: %s\n", ap_id));
1219 
1220 	hdl = di_devlink_init(NULL, 0);
1221 
1222 	if (strncmp("/devices/", ap_id, 9) == 0) {
1223 		/* ap_id is a valid minor_path with /devices prepended */
1224 		(void) di_devlink_walk(hdl, NULL, ap_id + 8, DI_PRIMARY_LINK,
1225 		    (void *)ap_log_id, found_devlink);
1226 	} else {
1227 		DBG(1, ("check_devlinks: invalid ap_id: %s\n", ap_id));
1228 		return (CFGA_ERROR);
1229 	}
1230 
1231 	(void) di_devlink_fini(&hdl);
1232 
1233 	if (ap_log_id[0] != '\0')
1234 		return (CFGA_OK);
1235 	else
1236 		return (CFGA_ERROR);
1237 }
1238 
1239 /*
1240  * most of this is needed to compensate for
1241  * differences between various platforms
1242  */
1243 static cfga_err_t
1244 fix_ap_name(char *ap_log_id, const char *ap_id, char *slot_name,
1245     char **errstring)
1246 {
1247 	char *buf;
1248 	char *tmp;
1249 	char *ptr;
1250 
1251 	di_node_t ap_node;
1252 
1253 	ap_log_id[0] = '\0';
1254 
1255 	if (check_devlinks(ap_log_id, ap_id) == CFGA_OK)
1256 		return (CFGA_OK);
1257 
1258 	DBG(1, ("fix_ap_name: %s\n", ap_id));
1259 
1260 	if ((buf = malloc(strlen(ap_id) + 1)) == NULL) {
1261 		DBG(1, ("malloc failed\n"));
1262 		return (CFGA_ERROR);
1263 	}
1264 	(void) strcpy(buf, ap_id);
1265 	tmp = buf + sizeof ("/devices") - 1;
1266 
1267 	ptr = strchr(tmp, ':');
1268 	ptr[0] = '\0';
1269 
1270 	DBG(1, ("fix_ap_name: %s\n", tmp));
1271 
1272 	ap_node = di_init(tmp, DINFOMINOR);
1273 	if (ap_node == DI_NODE_NIL) {
1274 		cfga_err(errstring, "di_init ", 0);
1275 		DBG(1, ("fix_ap_name: failed to snapshot node\n"));
1276 		return (CFGA_ERROR);
1277 	}
1278 
1279 	(void) snprintf(ap_log_id, strlen(ap_id) + 1, "%s%i:%s",
1280 	    di_driver_name(ap_node), di_instance(ap_node), slot_name);
1281 
1282 	DBG(1, ("fix_ap_name: %s\n", ap_log_id));
1283 
1284 	di_fini(ap_node);
1285 
1286 	free(buf);
1287 	return (CFGA_OK);
1288 }
1289 
1290 
1291 static int
1292 findlink_cb(di_devlink_t devlink, void *arg)
1293 {
1294 	(*(char **)arg) = strdup(di_devlink_path(devlink));
1295 
1296 	return (DI_WALK_TERMINATE);
1297 }
1298 
1299 /*
1300  * returns an allocated string containing the full path to the devlink for
1301  * <ap_phys_id> in the devlink database; we expect only one devlink per
1302  * <ap_phys_id> so we return the first encountered
1303  */
1304 static char *
1305 findlink(char *ap_phys_id)
1306 {
1307 	di_devlink_handle_t hdl;
1308 	char *path = NULL;
1309 
1310 	hdl = di_devlink_init(NULL, 0);
1311 
1312 	if (strncmp("/devices/", ap_phys_id, 9) == 0)
1313 		ap_phys_id += 8;
1314 
1315 	(void) di_devlink_walk(hdl, "^cfg/.+$", ap_phys_id, DI_PRIMARY_LINK,
1316 	    (void *)&path, findlink_cb);
1317 
1318 	(void) di_devlink_fini(&hdl);
1319 	return (path);
1320 }
1321 
1322 
1323 /*
1324  * returns CFGA_OK if it can succesfully retrieve the devlink info associated
1325  * with devlink for <ap_phys_id> which will be returned through <ap_info>
1326  */
1327 cfga_err_t
1328 get_dli(char *dlpath, char *ap_info, int ap_info_sz)
1329 {
1330 	int fd;
1331 
1332 	fd = di_dli_openr(dlpath);
1333 	if (fd < 0)
1334 		return (CFGA_ERROR);
1335 
1336 	(void) read(fd, ap_info, ap_info_sz);
1337 	ap_info[ap_info_sz - 1] = '\0';
1338 
1339 	di_dli_close(fd);
1340 	return (CFGA_OK);
1341 }
1342 
1343 static cfga_err_t
1344 cfga_get_condition(hp_node_t node, ap_condition_t *cond)
1345 {
1346 	char *condition;
1347 
1348 	/* "condition" bus specific commands */
1349 	if (hp_get_private(node, PCIEHPC_PROP_SLOT_CONDITION,
1350 	    &condition) != 0) {
1351 		*cond = AP_COND_UNKNOWN;
1352 		return (CFGA_ERROR);
1353 	}
1354 
1355 	condition = get_val_from_result(condition);
1356 
1357 	if (strcmp(condition, PCIEHPC_PROP_COND_OK) == 0)
1358 		*cond = AP_COND_OK;
1359 	else if (strcmp(condition, PCIEHPC_PROP_COND_FAILING) == 0)
1360 		*cond = AP_COND_FAILING;
1361 	else if (strcmp(condition, PCIEHPC_PROP_COND_FAILED) == 0)
1362 		*cond = AP_COND_FAILED;
1363 	else if (strcmp(condition, PCIEHPC_PROP_COND_UNUSABLE) == 0)
1364 		*cond = AP_COND_UNUSABLE;
1365 	else if (strcmp(condition, PCIEHPC_PROP_COND_UNKNOWN) == 0)
1366 		*cond = AP_COND_UNKNOWN;
1367 	else
1368 		return (CFGA_ERROR);
1369 
1370 	return (CFGA_OK);
1371 }
1372 
1373 /*ARGSUSED*/
1374 cfga_err_t
1375 cfga_list_ext(const char *ap_id, cfga_list_data_t **cs,
1376     int *nlist, const char *options, const char *listopts, char **errstring,
1377     cfga_flags_t flags)
1378 {
1379 	char			*boardtype;
1380 	char			*cardtype;
1381 	char			*tmpb = NULL, *tmpc = NULL;
1382 	struct	searcharg	slotname_arg;
1383 	int			fd;
1384 	int			rv = CFGA_OK;
1385 	char			*dlpath = NULL;
1386 	hp_node_t		node;
1387 	ap_rstate_t		rs;
1388 	ap_ostate_t		os;
1389 	ap_condition_t		cond;
1390 
1391 	if ((rv = check_options(options)) != CFGA_OK) {
1392 		return (rv);
1393 	}
1394 
1395 	if (errstring != NULL)
1396 		*errstring = NULL;
1397 
1398 	DBG(1, ("cfga_list_ext:(%s)\n", ap_id));
1399 
1400 	if (cs == NULL || nlist == NULL) {
1401 		rv = CFGA_ERROR;
1402 		return (rv);
1403 	}
1404 
1405 	*nlist = 1;
1406 
1407 	if ((*cs = malloc(sizeof (cfga_list_data_t))) == NULL) {
1408 		cfga_err(errstring, "malloc ", 0);
1409 		DBG(1, ("malloc failed\n"));
1410 		rv = CFGA_ERROR;
1411 		return (rv);
1412 	}
1413 	(void) memset(*cs, 0, sizeof (cfga_list_data_t));
1414 
1415 	rv = physpath2node(ap_id, errstring, &node);
1416 	if (rv != CFGA_OK) {
1417 		DBG(1, ("physpath2node failed\n"));
1418 		return (rv);
1419 	}
1420 
1421 	if (cfga_get_state(node, &rs, &os) != CFGA_OK) {
1422 		DBG(1, ("cfga_get_state failed\n"));
1423 		hp_fini(node);
1424 		return (CFGA_ERROR);
1425 	}
1426 
1427 	switch (rs) {
1428 		case AP_RSTATE_EMPTY:
1429 			(*cs)->ap_r_state = CFGA_STAT_EMPTY;
1430 			DBG(2, ("ap_rstate = CFGA_STAT_EMPTY\n"));
1431 			break;
1432 		case AP_RSTATE_DISCONNECTED:
1433 			(*cs)->ap_r_state = CFGA_STAT_DISCONNECTED;
1434 			DBG(2, ("ap_rstate = CFGA_STAT_DISCONNECTED\n"));
1435 			break;
1436 		case AP_RSTATE_CONNECTED:
1437 			(*cs)->ap_r_state = CFGA_STAT_CONNECTED;
1438 			DBG(2, ("ap_rstate = CFGA_STAT_CONNECTED\n"));
1439 			break;
1440 	default:
1441 		cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
1442 		rv = CFGA_ERROR;
1443 		hp_fini(node);
1444 		return (rv);
1445 	}
1446 
1447 	switch (os) {
1448 		case AP_OSTATE_CONFIGURED:
1449 			(*cs)->ap_o_state = CFGA_STAT_CONFIGURED;
1450 			DBG(2, ("ap_ostate = CFGA_STAT_CONFIGURED\n"));
1451 			break;
1452 		case AP_OSTATE_UNCONFIGURED:
1453 			(*cs)->ap_o_state = CFGA_STAT_UNCONFIGURED;
1454 			DBG(2, ("ap_ostate = CFGA_STAT_UNCONFIGURED\n"));
1455 			break;
1456 	default:
1457 		cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
1458 		rv = CFGA_ERROR;
1459 		hp_fini(node);
1460 		return (rv);
1461 	}
1462 
1463 	(void) cfga_get_condition(node, &cond);
1464 
1465 	switch (cond) {
1466 		case AP_COND_OK:
1467 			(*cs)->ap_cond = CFGA_COND_OK;
1468 			DBG(2, ("ap_cond = CFGA_COND_OK\n"));
1469 			break;
1470 		case AP_COND_FAILING:
1471 			(*cs)->ap_cond = CFGA_COND_FAILING;
1472 			DBG(2, ("ap_cond = CFGA_COND_FAILING\n"));
1473 			break;
1474 		case AP_COND_FAILED:
1475 			(*cs)->ap_cond = CFGA_COND_FAILED;
1476 			DBG(2, ("ap_cond = CFGA_COND_FAILED\n"));
1477 			break;
1478 		case AP_COND_UNUSABLE:
1479 			(*cs)->ap_cond = CFGA_COND_UNUSABLE;
1480 			DBG(2, ("ap_cond = CFGA_COND_UNUSABLE\n"));
1481 			break;
1482 		case AP_COND_UNKNOWN:
1483 			(*cs)->ap_cond = CFGA_COND_UNKNOWN;
1484 			DBG(2, ("ap_cond = CFGA_COND_UNKNOW\n"));
1485 			break;
1486 	default:
1487 		cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
1488 		rv = CFGA_ERROR;
1489 		hp_fini(node);
1490 		return (rv);
1491 	}
1492 	/*
1493 	 * We're not busy since the entrance into the kernel has been
1494 	 * sync'ed via libhotplug.
1495 	 */
1496 	(*cs)->ap_busy = 0;
1497 
1498 	/* last change */
1499 	(*cs)->ap_status_time = hp_last_change(node);
1500 
1501 	/* board type */
1502 	if (hp_get_private(node, PCIEHPC_PROP_BOARD_TYPE, &tmpb) != 0)
1503 		boardtype = PCIEHPC_PROP_VALUE_UNKNOWN;
1504 	else
1505 		boardtype = get_val_from_result(tmpb);
1506 
1507 	/* card type */
1508 	if (hp_get_private(node, PCIEHPC_PROP_CARD_TYPE, &tmpc) != 0)
1509 		cardtype = PCIEHPC_PROP_VALUE_UNKNOWN;
1510 	else
1511 		cardtype = get_val_from_result(tmpc);
1512 
1513 	/* logical ap_id */
1514 	rv = fix_ap_name((*cs)->ap_log_id, ap_id,
1515 	    hp_name(node), errstring);
1516 	DBG(1, ("logical id: %s\n", (*cs)->ap_log_id));
1517 	/* physical ap_id */
1518 	(void) strcpy((*cs)->ap_phys_id, ap_id);    /* physical path of AP */
1519 
1520 	/* information */
1521 	dlpath = findlink((*cs)->ap_phys_id);
1522 	if (dlpath != NULL) {
1523 		if (get_dli(dlpath, (*cs)->ap_info,
1524 		    sizeof ((*cs)->ap_info)) != CFGA_OK)
1525 			(*cs)->ap_info[0] = '\0';
1526 		free(dlpath);
1527 	}
1528 
1529 	if ((*cs)->ap_log_id[0] == '\0')
1530 		(void) strcpy((*cs)->ap_log_id, hp_name(node));
1531 
1532 	if ((*cs)->ap_info[0] == '\0') {
1533 		/* slot_names of bus node  */
1534 		if (find_physical_slot_names(ap_id, &slotname_arg) != -1)
1535 			(void) strcpy((*cs)->ap_info,
1536 			    slotname_arg.slotnames[slotname_arg.minor]);
1537 	}
1538 
1539 	/* class_code/subclass/boardtype */
1540 	get_type(boardtype, cardtype, (*cs)->ap_type);
1541 
1542 	DBG(1, ("cfga_list_ext return success\n"));
1543 	rv = CFGA_OK;
1544 
1545 	free(tmpb);
1546 	free(tmpc);
1547 	hp_fini(node);
1548 	return (rv);
1549 }
1550 
1551 /*
1552  * This routine prints a single line of help message
1553  */
1554 static void
1555 cfga_msg(struct cfga_msg *msgp, const char *str)
1556 {
1557 	DBG(2, ("<%s>", str));
1558 
1559 	if (msgp == NULL || msgp->message_routine == NULL)
1560 		return;
1561 
1562 	(*msgp->message_routine)(msgp->appdata_ptr, str);
1563 	(*msgp->message_routine)(msgp->appdata_ptr, "\n");
1564 }
1565 
1566 static cfga_err_t
1567 check_options(const char *options)
1568 {
1569 	struct cfga_msg *msgp = NULL;
1570 
1571 	if (options) {
1572 		cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_UNKNOWN]));
1573 		cfga_msg(msgp, options);
1574 		return (CFGA_INVAL);
1575 	}
1576 	return (CFGA_OK);
1577 }
1578 
1579 /*ARGSUSED*/
1580 cfga_err_t
1581 cfga_help(struct cfga_msg *msgp, const char *options, cfga_flags_t flags)
1582 {
1583 	if (options) {
1584 		cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_UNKNOWN]));
1585 		cfga_msg(msgp, options);
1586 	}
1587 	DBG(1, ("cfga_help\n"));
1588 
1589 	cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_HEADER]));
1590 	cfga_msg(msgp, cfga_strs[HELP_CONFIG]);
1591 	cfga_msg(msgp, cfga_strs[HELP_ENABLE_SLOT]);
1592 	cfga_msg(msgp, cfga_strs[HELP_DISABLE_SLOT]);
1593 	cfga_msg(msgp, cfga_strs[HELP_ENABLE_AUTOCONF]);
1594 	cfga_msg(msgp, cfga_strs[HELP_DISABLE_AUTOCONF]);
1595 	cfga_msg(msgp, cfga_strs[HELP_LED_CNTRL]);
1596 	return (CFGA_OK);
1597 }
1598 
1599 /*
1600  * cfga_err() accepts a variable number of message IDs and constructs
1601  * a corresponding error string which is returned via the errstring argument.
1602  * cfga_err() calls gettext() to internationalize proper messages.
1603  */
1604 static void
1605 cfga_err(char **errstring, ...)
1606 {
1607 	int a;
1608 	int i;
1609 	int n;
1610 	int len;
1611 	int flen;
1612 	char *p;
1613 	char *q;
1614 	char *s[32];
1615 	char *failed;
1616 	va_list ap;
1617 
1618 	/*
1619 	 * If errstring is null it means user is not interested in getting
1620 	 * error status. So we don't do all the work
1621 	 */
1622 	if (errstring == NULL) {
1623 		return;
1624 	}
1625 	va_start(ap, errstring);
1626 
1627 	failed = dgettext(TEXT_DOMAIN, cfga_strs[FAILED]);
1628 	flen = strlen(failed);
1629 
1630 	for (n = len = 0; (a = va_arg(ap, int)) != 0; n++) {
1631 		switch (a) {
1632 		case CMD_GETSTAT:
1633 		case CMD_LIST:
1634 		case CMD_SLOT_CONNECT:
1635 		case CMD_SLOT_DISCONNECT:
1636 		case CMD_SLOT_CONFIGURE:
1637 		case CMD_SLOT_UNCONFIGURE:
1638 			p =  cfga_errstrs(a);
1639 			len += (strlen(p) + flen);
1640 			s[n] = p;
1641 			s[++n] = cfga_strs[FAILED];
1642 
1643 			DBG(2, ("<%s>", p));
1644 			DBG(2, (cfga_strs[FAILED]));
1645 			break;
1646 
1647 		case ERR_CMD_INVAL:
1648 		case ERR_AP_INVAL:
1649 		case ERR_OPT_INVAL:
1650 		case ERR_AP_ERR:
1651 			switch (a) {
1652 			case ERR_CMD_INVAL:
1653 				p = dgettext(TEXT_DOMAIN,
1654 				    cfga_errstrs[ERR_CMD_INVAL]);
1655 				break;
1656 			case ERR_AP_INVAL:
1657 				p = dgettext(TEXT_DOMAIN,
1658 				    cfga_errstrs[ERR_AP_INVAL]);
1659 				break;
1660 			case ERR_OPT_INVAL:
1661 				p = dgettext(TEXT_DOMAIN,
1662 				    cfga_errstrs[ERR_OPT_INVAL]);
1663 				break;
1664 			case ERR_AP_ERR:
1665 				p = dgettext(TEXT_DOMAIN,
1666 				    cfga_errstrs[ERR_AP_ERR]);
1667 				break;
1668 			}
1669 
1670 			if ((q = va_arg(ap, char *)) != NULL) {
1671 				len += (strlen(p) + strlen(q));
1672 				s[n] = p;
1673 				s[++n] = q;
1674 				DBG(2, ("<%s>", p));
1675 				DBG(2, ("<%s>", q));
1676 				break;
1677 			} else {
1678 				len += strlen(p);
1679 				s[n] = p;
1680 
1681 			}
1682 			DBG(2, ("<%s>", p));
1683 			break;
1684 
1685 		default:
1686 			n--;
1687 			break;
1688 		}
1689 	}
1690 
1691 	DBG(2, ("\n"));
1692 	va_end(ap);
1693 
1694 	if ((p = calloc(len + 1, 1)) == NULL)
1695 		return;
1696 
1697 	for (i = 0; i < n; i++) {
1698 		(void) strlcat(p, s[i], len + 1);
1699 		DBG(2, ("i:%d, %s\n", i, s[i]));
1700 	}
1701 
1702 	*errstring = p;
1703 	DBG(2, ("%s\n", *errstring));
1704 }
1705 
1706 /*
1707  * cfga_ap_id_cmp -- use default_ap_id_cmp() in libcfgadm
1708  */
1709