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