xref: /illumos-gate/usr/src/lib/cfgadm_plugins/shp/common/shp.c (revision d7c57852794a9ac50d4b6a9c4161cb10919658aa)
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 	char *tmpc;
1348 	cfga_err_t ret = CFGA_OK;
1349 
1350 	/* "condition" bus specific commands */
1351 	if (hp_get_private(node, PCIEHPC_PROP_SLOT_CONDITION,
1352 	    &tmpc) != 0) {
1353 		*cond = AP_COND_UNKNOWN;
1354 		return (CFGA_ERROR);
1355 	}
1356 
1357 	condition = get_val_from_result(tmpc);
1358 
1359 	if (strcmp(condition, PCIEHPC_PROP_COND_OK) == 0)
1360 		*cond = AP_COND_OK;
1361 	else if (strcmp(condition, PCIEHPC_PROP_COND_FAILING) == 0)
1362 		*cond = AP_COND_FAILING;
1363 	else if (strcmp(condition, PCIEHPC_PROP_COND_FAILED) == 0)
1364 		*cond = AP_COND_FAILED;
1365 	else if (strcmp(condition, PCIEHPC_PROP_COND_UNUSABLE) == 0)
1366 		*cond = AP_COND_UNUSABLE;
1367 	else if (strcmp(condition, PCIEHPC_PROP_COND_UNKNOWN) == 0)
1368 		*cond = AP_COND_UNKNOWN;
1369 	else
1370 		ret = CFGA_ERROR;
1371 
1372 	free(tmpc);
1373 	return (ret);
1374 }
1375 
1376 /*ARGSUSED*/
1377 cfga_err_t
1378 cfga_list_ext(const char *ap_id, cfga_list_data_t **cs,
1379     int *nlist, const char *options, const char *listopts, char **errstring,
1380     cfga_flags_t flags)
1381 {
1382 	char			*boardtype;
1383 	char			*cardtype;
1384 	char			*tmpb = NULL, *tmpc = NULL;
1385 	struct	searcharg	slotname_arg;
1386 	int			fd;
1387 	int			rv = CFGA_OK;
1388 	char			*dlpath = NULL;
1389 	hp_node_t		node;
1390 	ap_rstate_t		rs;
1391 	ap_ostate_t		os;
1392 	ap_condition_t		cond;
1393 
1394 	if ((rv = check_options(options)) != CFGA_OK) {
1395 		return (rv);
1396 	}
1397 
1398 	if (errstring != NULL)
1399 		*errstring = NULL;
1400 
1401 	DBG(1, ("cfga_list_ext:(%s)\n", ap_id));
1402 
1403 	if (cs == NULL || nlist == NULL) {
1404 		rv = CFGA_ERROR;
1405 		return (rv);
1406 	}
1407 
1408 	*nlist = 1;
1409 
1410 	if ((*cs = malloc(sizeof (cfga_list_data_t))) == NULL) {
1411 		cfga_err(errstring, "malloc ", 0);
1412 		DBG(1, ("malloc failed\n"));
1413 		rv = CFGA_ERROR;
1414 		return (rv);
1415 	}
1416 	(void) memset(*cs, 0, sizeof (cfga_list_data_t));
1417 
1418 	rv = physpath2node(ap_id, errstring, &node);
1419 	if (rv != CFGA_OK) {
1420 		DBG(1, ("physpath2node failed\n"));
1421 		return (rv);
1422 	}
1423 
1424 	if (cfga_get_state(node, &rs, &os) != CFGA_OK) {
1425 		DBG(1, ("cfga_get_state failed\n"));
1426 		hp_fini(node);
1427 		return (CFGA_ERROR);
1428 	}
1429 
1430 	switch (rs) {
1431 		case AP_RSTATE_EMPTY:
1432 			(*cs)->ap_r_state = CFGA_STAT_EMPTY;
1433 			DBG(2, ("ap_rstate = CFGA_STAT_EMPTY\n"));
1434 			break;
1435 		case AP_RSTATE_DISCONNECTED:
1436 			(*cs)->ap_r_state = CFGA_STAT_DISCONNECTED;
1437 			DBG(2, ("ap_rstate = CFGA_STAT_DISCONNECTED\n"));
1438 			break;
1439 		case AP_RSTATE_CONNECTED:
1440 			(*cs)->ap_r_state = CFGA_STAT_CONNECTED;
1441 			DBG(2, ("ap_rstate = CFGA_STAT_CONNECTED\n"));
1442 			break;
1443 	default:
1444 		cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
1445 		rv = CFGA_ERROR;
1446 		hp_fini(node);
1447 		return (rv);
1448 	}
1449 
1450 	switch (os) {
1451 		case AP_OSTATE_CONFIGURED:
1452 			(*cs)->ap_o_state = CFGA_STAT_CONFIGURED;
1453 			DBG(2, ("ap_ostate = CFGA_STAT_CONFIGURED\n"));
1454 			break;
1455 		case AP_OSTATE_UNCONFIGURED:
1456 			(*cs)->ap_o_state = CFGA_STAT_UNCONFIGURED;
1457 			DBG(2, ("ap_ostate = CFGA_STAT_UNCONFIGURED\n"));
1458 			break;
1459 	default:
1460 		cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
1461 		rv = CFGA_ERROR;
1462 		hp_fini(node);
1463 		return (rv);
1464 	}
1465 
1466 	(void) cfga_get_condition(node, &cond);
1467 
1468 	switch (cond) {
1469 		case AP_COND_OK:
1470 			(*cs)->ap_cond = CFGA_COND_OK;
1471 			DBG(2, ("ap_cond = CFGA_COND_OK\n"));
1472 			break;
1473 		case AP_COND_FAILING:
1474 			(*cs)->ap_cond = CFGA_COND_FAILING;
1475 			DBG(2, ("ap_cond = CFGA_COND_FAILING\n"));
1476 			break;
1477 		case AP_COND_FAILED:
1478 			(*cs)->ap_cond = CFGA_COND_FAILED;
1479 			DBG(2, ("ap_cond = CFGA_COND_FAILED\n"));
1480 			break;
1481 		case AP_COND_UNUSABLE:
1482 			(*cs)->ap_cond = CFGA_COND_UNUSABLE;
1483 			DBG(2, ("ap_cond = CFGA_COND_UNUSABLE\n"));
1484 			break;
1485 		case AP_COND_UNKNOWN:
1486 			(*cs)->ap_cond = CFGA_COND_UNKNOWN;
1487 			DBG(2, ("ap_cond = CFGA_COND_UNKNOW\n"));
1488 			break;
1489 	default:
1490 		cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
1491 		rv = CFGA_ERROR;
1492 		hp_fini(node);
1493 		return (rv);
1494 	}
1495 	/*
1496 	 * We're not busy since the entrance into the kernel has been
1497 	 * sync'ed via libhotplug.
1498 	 */
1499 	(*cs)->ap_busy = 0;
1500 
1501 	/* last change */
1502 	(*cs)->ap_status_time = hp_last_change(node);
1503 
1504 	/* board type */
1505 	if (hp_get_private(node, PCIEHPC_PROP_BOARD_TYPE, &tmpb) != 0)
1506 		boardtype = PCIEHPC_PROP_VALUE_UNKNOWN;
1507 	else
1508 		boardtype = get_val_from_result(tmpb);
1509 
1510 	/* card type */
1511 	if (hp_get_private(node, PCIEHPC_PROP_CARD_TYPE, &tmpc) != 0)
1512 		cardtype = PCIEHPC_PROP_VALUE_UNKNOWN;
1513 	else
1514 		cardtype = get_val_from_result(tmpc);
1515 
1516 	/* logical ap_id */
1517 	rv = fix_ap_name((*cs)->ap_log_id, ap_id,
1518 	    hp_name(node), errstring);
1519 	DBG(1, ("logical id: %s\n", (*cs)->ap_log_id));
1520 	/* physical ap_id */
1521 	(void) strcpy((*cs)->ap_phys_id, ap_id);    /* physical path of AP */
1522 
1523 	/* information */
1524 	dlpath = findlink((*cs)->ap_phys_id);
1525 	if (dlpath != NULL) {
1526 		if (get_dli(dlpath, (*cs)->ap_info,
1527 		    sizeof ((*cs)->ap_info)) != CFGA_OK)
1528 			(*cs)->ap_info[0] = '\0';
1529 		free(dlpath);
1530 	}
1531 
1532 	if ((*cs)->ap_log_id[0] == '\0')
1533 		(void) strcpy((*cs)->ap_log_id, hp_name(node));
1534 
1535 	if ((*cs)->ap_info[0] == '\0') {
1536 		/* slot_names of bus node  */
1537 		if (find_physical_slot_names(ap_id, &slotname_arg) != -1)
1538 			(void) strcpy((*cs)->ap_info,
1539 			    slotname_arg.slotnames[slotname_arg.minor]);
1540 	}
1541 
1542 	/* class_code/subclass/boardtype */
1543 	get_type(boardtype, cardtype, (*cs)->ap_type);
1544 
1545 	DBG(1, ("cfga_list_ext return success\n"));
1546 	rv = CFGA_OK;
1547 
1548 	free(tmpb);
1549 	free(tmpc);
1550 	hp_fini(node);
1551 	return (rv);
1552 }
1553 
1554 /*
1555  * This routine prints a single line of help message
1556  */
1557 static void
1558 cfga_msg(struct cfga_msg *msgp, const char *str)
1559 {
1560 	DBG(2, ("<%s>", str));
1561 
1562 	if (msgp == NULL || msgp->message_routine == NULL)
1563 		return;
1564 
1565 	(*msgp->message_routine)(msgp->appdata_ptr, str);
1566 	(*msgp->message_routine)(msgp->appdata_ptr, "\n");
1567 }
1568 
1569 static cfga_err_t
1570 check_options(const char *options)
1571 {
1572 	struct cfga_msg *msgp = NULL;
1573 
1574 	if (options) {
1575 		cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_UNKNOWN]));
1576 		cfga_msg(msgp, options);
1577 		return (CFGA_INVAL);
1578 	}
1579 	return (CFGA_OK);
1580 }
1581 
1582 /*ARGSUSED*/
1583 cfga_err_t
1584 cfga_help(struct cfga_msg *msgp, const char *options, cfga_flags_t flags)
1585 {
1586 	if (options) {
1587 		cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_UNKNOWN]));
1588 		cfga_msg(msgp, options);
1589 	}
1590 	DBG(1, ("cfga_help\n"));
1591 
1592 	cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_HEADER]));
1593 	cfga_msg(msgp, cfga_strs[HELP_CONFIG]);
1594 	cfga_msg(msgp, cfga_strs[HELP_ENABLE_SLOT]);
1595 	cfga_msg(msgp, cfga_strs[HELP_DISABLE_SLOT]);
1596 	cfga_msg(msgp, cfga_strs[HELP_ENABLE_AUTOCONF]);
1597 	cfga_msg(msgp, cfga_strs[HELP_DISABLE_AUTOCONF]);
1598 	cfga_msg(msgp, cfga_strs[HELP_LED_CNTRL]);
1599 	return (CFGA_OK);
1600 }
1601 
1602 /*
1603  * cfga_err() accepts a variable number of message IDs and constructs
1604  * a corresponding error string which is returned via the errstring argument.
1605  * cfga_err() calls gettext() to internationalize proper messages.
1606  */
1607 static void
1608 cfga_err(char **errstring, ...)
1609 {
1610 	int a;
1611 	int i;
1612 	int n;
1613 	int len;
1614 	int flen;
1615 	char *p;
1616 	char *q;
1617 	char *s[32];
1618 	char *failed;
1619 	va_list ap;
1620 
1621 	/*
1622 	 * If errstring is null it means user is not interested in getting
1623 	 * error status. So we don't do all the work
1624 	 */
1625 	if (errstring == NULL) {
1626 		return;
1627 	}
1628 	va_start(ap, errstring);
1629 
1630 	failed = dgettext(TEXT_DOMAIN, cfga_strs[FAILED]);
1631 	flen = strlen(failed);
1632 
1633 	for (n = len = 0; (a = va_arg(ap, int)) != 0; n++) {
1634 		switch (a) {
1635 		case CMD_GETSTAT:
1636 		case CMD_LIST:
1637 		case CMD_SLOT_CONNECT:
1638 		case CMD_SLOT_DISCONNECT:
1639 		case CMD_SLOT_CONFIGURE:
1640 		case CMD_SLOT_UNCONFIGURE:
1641 			p =  cfga_errstrs(a);
1642 			len += (strlen(p) + flen);
1643 			s[n] = p;
1644 			s[++n] = cfga_strs[FAILED];
1645 
1646 			DBG(2, ("<%s>", p));
1647 			DBG(2, (cfga_strs[FAILED]));
1648 			break;
1649 
1650 		case ERR_CMD_INVAL:
1651 		case ERR_AP_INVAL:
1652 		case ERR_OPT_INVAL:
1653 		case ERR_AP_ERR:
1654 			switch (a) {
1655 			case ERR_CMD_INVAL:
1656 				p = dgettext(TEXT_DOMAIN,
1657 				    cfga_errstrs[ERR_CMD_INVAL]);
1658 				break;
1659 			case ERR_AP_INVAL:
1660 				p = dgettext(TEXT_DOMAIN,
1661 				    cfga_errstrs[ERR_AP_INVAL]);
1662 				break;
1663 			case ERR_OPT_INVAL:
1664 				p = dgettext(TEXT_DOMAIN,
1665 				    cfga_errstrs[ERR_OPT_INVAL]);
1666 				break;
1667 			case ERR_AP_ERR:
1668 				p = dgettext(TEXT_DOMAIN,
1669 				    cfga_errstrs[ERR_AP_ERR]);
1670 				break;
1671 			}
1672 
1673 			if ((q = va_arg(ap, char *)) != NULL) {
1674 				len += (strlen(p) + strlen(q));
1675 				s[n] = p;
1676 				s[++n] = q;
1677 				DBG(2, ("<%s>", p));
1678 				DBG(2, ("<%s>", q));
1679 				break;
1680 			} else {
1681 				len += strlen(p);
1682 				s[n] = p;
1683 
1684 			}
1685 			DBG(2, ("<%s>", p));
1686 			break;
1687 
1688 		default:
1689 			n--;
1690 			break;
1691 		}
1692 	}
1693 
1694 	DBG(2, ("\n"));
1695 	va_end(ap);
1696 
1697 	if ((p = calloc(len + 1, 1)) == NULL)
1698 		return;
1699 
1700 	for (i = 0; i < n; i++) {
1701 		(void) strlcat(p, s[i], len + 1);
1702 		DBG(2, ("i:%d, %s\n", i, s[i]));
1703 	}
1704 
1705 	*errstring = p;
1706 	DBG(2, ("%s\n", *errstring));
1707 }
1708 
1709 /*
1710  * cfga_ap_id_cmp -- use default_ap_id_cmp() in libcfgadm
1711  */
1712