xref: /onnv-gate/usr/src/lib/cfgadm_plugins/pci/common/cfga.c (revision 2587:93d8b810a71d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52312Skd93003  * Common Development and Distribution License (the "License").
62312Skd93003  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
2158Sanish 
220Sstevel@tonic-gate /*
232312Skd93003  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  *	Plugin Library for PCI Hot-Plug Controller
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <stddef.h>
340Sstevel@tonic-gate #include <locale.h>
350Sstevel@tonic-gate #include <ctype.h>
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <stdlib.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate #include <fcntl.h>
400Sstevel@tonic-gate #include <unistd.h>
410Sstevel@tonic-gate #include <errno.h>
420Sstevel@tonic-gate #include <locale.h>
430Sstevel@tonic-gate #include <langinfo.h>
440Sstevel@tonic-gate #include <time.h>
450Sstevel@tonic-gate #include <sys/param.h>
460Sstevel@tonic-gate #include <stdarg.h>
470Sstevel@tonic-gate #include <libdevinfo.h>
480Sstevel@tonic-gate #include <libdevice.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #define	CFGA_PLUGIN_LIB
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #include <config_admin.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #include <sys/types.h>
550Sstevel@tonic-gate #include <sys/stat.h>
560Sstevel@tonic-gate #include <sys/ioctl.h>
570Sstevel@tonic-gate #include <sys/dditypes.h>
580Sstevel@tonic-gate #include <sys/devctl.h>
590Sstevel@tonic-gate #include <sys/modctl.h>
600Sstevel@tonic-gate #include <sys/hotplug/hpctrl.h>
610Sstevel@tonic-gate #include <sys/pci.h>
620Sstevel@tonic-gate #include <libintl.h>
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #include <dirent.h>
650Sstevel@tonic-gate #include <limits.h>
660Sstevel@tonic-gate #include <sys/mkdev.h>
670Sstevel@tonic-gate #include <librcm.h>
6858Sanish #include "../../../../common/pci/pci_strings.h"
6958Sanish 
7058Sanish extern const struct pci_class_strings_s class_pci[];
7158Sanish extern int class_pci_items;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * Set the version number
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate int cfga_version = CFGA_HSL_V2;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate #ifdef	DEBUG
790Sstevel@tonic-gate #define	PCIHP_DBG	1
800Sstevel@tonic-gate #endif
810Sstevel@tonic-gate 
820Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
830Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
840Sstevel@tonic-gate #endif
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate  *	DEBUGING LEVEL
880Sstevel@tonic-gate  *
890Sstevel@tonic-gate  * 	External routines:  1 - 2
900Sstevel@tonic-gate  *	Internal routines:  3 - 4
910Sstevel@tonic-gate  */
920Sstevel@tonic-gate #ifdef	PCIHP_DBG
930Sstevel@tonic-gate int	pcihp_debug = 1;
940Sstevel@tonic-gate #define	DBG(level, args) \
950Sstevel@tonic-gate 	{ if (pcihp_debug >= (level)) printf args; }
960Sstevel@tonic-gate #define	DBG_F(level, args) \
970Sstevel@tonic-gate 	{ if (pcihp_debug >= (level)) fprintf args; }
980Sstevel@tonic-gate #else
990Sstevel@tonic-gate #define	DBG(level, args)	/* nothing */
1000Sstevel@tonic-gate #define	DBG_F(level, args)	/* nothing */
1010Sstevel@tonic-gate #endif
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate #define	CMD_ACQUIRE		0
1040Sstevel@tonic-gate #define	CMD_GETSTAT		1
1050Sstevel@tonic-gate #define	CMD_LIST		2
1060Sstevel@tonic-gate #define	CMD_SLOT_CONNECT	3
1070Sstevel@tonic-gate #define	CMD_SLOT_DISCONNECT	4
1080Sstevel@tonic-gate #define	CMD_SLOT_CONFIGURE	5
1090Sstevel@tonic-gate #define	CMD_SLOT_UNCONFIGURE	6
1100Sstevel@tonic-gate #define	CMD_SLOT_INSERT		7
1110Sstevel@tonic-gate #define	CMD_SLOT_REMOVE		8
1120Sstevel@tonic-gate #define	CMD_OPEN		9
1130Sstevel@tonic-gate #define	CMD_FSTAT		10
1140Sstevel@tonic-gate #define	ERR_CMD_INVAL		11
1150Sstevel@tonic-gate #define	ERR_AP_INVAL		12
1160Sstevel@tonic-gate #define	ERR_AP_ERR		13
1170Sstevel@tonic-gate #define	ERR_OPT_INVAL		14
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate static char *
1200Sstevel@tonic-gate cfga_errstrs[] = {
1210Sstevel@tonic-gate 	/* n */ "acquire ",
1220Sstevel@tonic-gate 	/* n */ "get-status ",
1230Sstevel@tonic-gate 	/* n */ "list ",
1240Sstevel@tonic-gate 	/* n */ "connect ",
1250Sstevel@tonic-gate 	/* n */ "disconnect ",
1260Sstevel@tonic-gate 	/* n */ "configure ",
1270Sstevel@tonic-gate 	/* n */ "unconfigure ",
1280Sstevel@tonic-gate 	/* n */ "insert ",
1290Sstevel@tonic-gate 	/* n */ "remove ",
1300Sstevel@tonic-gate 	/* n */ "open ",
1310Sstevel@tonic-gate 	/* n */ "fstat ",
1320Sstevel@tonic-gate 	/* y */ "invalid command ",
1330Sstevel@tonic-gate 	/* y */ "invalid attachment point ",
1340Sstevel@tonic-gate 	/* y */ "invalid transition ",
1350Sstevel@tonic-gate 	/* y */ "invalid option ",
1360Sstevel@tonic-gate 		NULL
1370Sstevel@tonic-gate };
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate #define	HELP_HEADER		1
1400Sstevel@tonic-gate #define	HELP_CONFIG		2
1410Sstevel@tonic-gate #define	HELP_ENABLE_SLOT	3
1420Sstevel@tonic-gate #define	HELP_DISABLE_SLOT	4
1430Sstevel@tonic-gate #define	HELP_ENABLE_AUTOCONF	5
1440Sstevel@tonic-gate #define	HELP_DISABLE_AUTOCONF	6
1450Sstevel@tonic-gate #define	HELP_LED_CNTRL		7
1460Sstevel@tonic-gate #define	HELP_UNKNOWN		8
1470Sstevel@tonic-gate #define	SUCCESS			9
1480Sstevel@tonic-gate #define	FAILED			10
1490Sstevel@tonic-gate #define	UNKNOWN			11
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate #define	MAXLINE			256
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate /* for type string assembly in get_type() */
1540Sstevel@tonic-gate #define	TPCT(s)	(void) strlcat(buf, (s), CFGA_TYPE_LEN)
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate extern int errno;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate static void cfga_err(char **errstring, ...);
1590Sstevel@tonic-gate static cfga_err_t fix_ap_name(char *ap_log_id, const char *ap_id,
1600Sstevel@tonic-gate     char *slot_name, char **errstring);
1610Sstevel@tonic-gate static void build_control_data(struct hpc_control_data *iocdata, uint_t cmd,
1620Sstevel@tonic-gate     void *retdata);
1630Sstevel@tonic-gate static cfga_err_t check_options(const char *options);
1640Sstevel@tonic-gate static void cfga_msg(struct cfga_msg *msgp, const char *str);
165*2587Spjha static char *findlink(char *ap_phys_id);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate static char *
1680Sstevel@tonic-gate cfga_strs[] = {
1690Sstevel@tonic-gate NULL,
1700Sstevel@tonic-gate "\nPCI hotplug specific commands:",
1710Sstevel@tonic-gate "\t-c [connect|disconnect|configure|unconfigure|insert|remove] "
1720Sstevel@tonic-gate "ap_id [ap_id...]",
1730Sstevel@tonic-gate "\t-x enable_slot  ap_id [ap_id...]",
1740Sstevel@tonic-gate "\t-x disable_slot ap_id [ap_id...]",
1750Sstevel@tonic-gate "\t-x enable_autoconfig  ap_id [ap_id...]",
1760Sstevel@tonic-gate "\t-x disable_autoconfig ap_id [ap_id...]",
1770Sstevel@tonic-gate "\t-x led[=[fault|power|active|attn],mode=[on|off|blink]] ap_id [ap_id...]",
1780Sstevel@tonic-gate "\tunknown command or option: ",
1790Sstevel@tonic-gate "success   ",
1800Sstevel@tonic-gate "failed   ",
1810Sstevel@tonic-gate "unknown",
1820Sstevel@tonic-gate NULL
1830Sstevel@tonic-gate };
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate #define	MAX_FORMAT 80
1860Sstevel@tonic-gate 
18758Sanish #define	ENABLE_SLOT	0
18858Sanish #define	DISABLE_SLOT	1
18958Sanish #define	ENABLE_AUTOCNF	2
19058Sanish #define	DISABLE_AUTOCNF	3
19158Sanish #define	LED		4
19258Sanish #define	MODE		5
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate /*
1950Sstevel@tonic-gate  * Board Type
1960Sstevel@tonic-gate  */
1970Sstevel@tonic-gate static char *
1980Sstevel@tonic-gate board_strs[] = {
1990Sstevel@tonic-gate 	/* n */ "???",	/* HPC_BOARD_UNKNOWN */
2000Sstevel@tonic-gate 	/* n */ "hp",	/* HPC_BOARD_PCI_HOTPLUG */
2010Sstevel@tonic-gate 	/* n */ "nhs",	/* HPC_BOARD_CPCI_NON_HS */
2020Sstevel@tonic-gate 	/* n */ "bhs",  /* HPC_BOARD_CPCI_BASIC_HS */
2030Sstevel@tonic-gate 	/* n */ "fhs",	/* HPC_BOARD_CPCI_FULL_HS */
2040Sstevel@tonic-gate 	/* n */ "hs",	/* HPC_BOARD_CPCI_HS */
2050Sstevel@tonic-gate 	/* n */ NULL
2060Sstevel@tonic-gate };
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate /*
2090Sstevel@tonic-gate  * HW functions
2100Sstevel@tonic-gate  */
2110Sstevel@tonic-gate static char *
2120Sstevel@tonic-gate func_strs[] = {
21358Sanish 	/* n */ "enable_slot",
2140Sstevel@tonic-gate 	/* n */ "disable_slot",
2150Sstevel@tonic-gate 	/* n */ "enable_autoconfig",
2160Sstevel@tonic-gate 	/* n */ "disable_autoconfig",
2170Sstevel@tonic-gate 	/* n */ "led",
2180Sstevel@tonic-gate 	/* n */ "mode",
2190Sstevel@tonic-gate 	/* n */ NULL
2200Sstevel@tonic-gate };
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate /*
2230Sstevel@tonic-gate  * LED strings
2240Sstevel@tonic-gate  */
2250Sstevel@tonic-gate static char *
2260Sstevel@tonic-gate led_strs[] = {
2270Sstevel@tonic-gate 	/* n */ "fault",	/* HPC_FAULT_LED */
2280Sstevel@tonic-gate 	/* n */ "power",	/* HPC_POWER_LED */
2290Sstevel@tonic-gate 	/* n */ "attn",		/* HPC_ATTN_LED */
2300Sstevel@tonic-gate 	/* n */ "active",	/* HPC_ACTIVE_LED */
2310Sstevel@tonic-gate 	/* n */ NULL
2320Sstevel@tonic-gate };
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate #define	FAULT	0
2350Sstevel@tonic-gate #define	POWER	1
2360Sstevel@tonic-gate #define	ATTN	2
2370Sstevel@tonic-gate #define	ACTIVE	3
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate static char *
2400Sstevel@tonic-gate mode_strs[] = {
2410Sstevel@tonic-gate 	/* n */ "off",		/* HPC_LED_OFF */
2420Sstevel@tonic-gate 	/* n */ "on",		/* HPC_LED_ON */
2430Sstevel@tonic-gate 	/* n */ "blink",	/* HPC_LED_BLINK */
2440Sstevel@tonic-gate 	/* n */	NULL
2450Sstevel@tonic-gate };
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate #define	OFF	0
2480Sstevel@tonic-gate #define	ON	1
2490Sstevel@tonic-gate #define	BLINK	2
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate #define	cfga_errstrs(i)		cfga_errstrs[(i)]
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate #define	cfga_eid(a, b)		(((a) << 8) + (b))
2540Sstevel@tonic-gate #define	MAXDEVS			32
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate typedef enum {
2570Sstevel@tonic-gate 	SOLARIS_SLT_NAME,
2580Sstevel@tonic-gate 	PROM_SLT_NAME
2590Sstevel@tonic-gate } slt_name_src_t;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate struct searcharg {
2620Sstevel@tonic-gate 	char	*devpath;
2630Sstevel@tonic-gate 	char	slotnames[MAXDEVS][MAXNAMELEN];
2640Sstevel@tonic-gate 	int	minor;
2650Sstevel@tonic-gate 	di_prom_handle_t	promp;
2660Sstevel@tonic-gate 	slt_name_src_t	slt_name_src;
2670Sstevel@tonic-gate };
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate static void *private_check;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate static int
get_occupants(const char * ap_id,hpc_occupant_info_t * occupant)2720Sstevel@tonic-gate get_occupants(const char *ap_id, hpc_occupant_info_t *occupant)
2730Sstevel@tonic-gate {
2740Sstevel@tonic-gate 	int rv;
2750Sstevel@tonic-gate 	int fd;
2760Sstevel@tonic-gate 	di_node_t ap_node;
2770Sstevel@tonic-gate 	char *prop_data;
2780Sstevel@tonic-gate 	char *tmp;
2790Sstevel@tonic-gate 	char *ptr;
2800Sstevel@tonic-gate 	struct stat statbuf;
2810Sstevel@tonic-gate 	dev_t devt;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	if ((fd = open(ap_id, O_RDWR)) == -1) {
2840Sstevel@tonic-gate 		DBG(2, ("open = ap_id%s, fd%d\n", ap_id, fd));
2850Sstevel@tonic-gate 		DBG_F(2, (stderr, "open on %s failed\n", ap_id));
2860Sstevel@tonic-gate 		return (CFGA_ERROR);
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	if (fstat(fd, &statbuf) == -1) {
2900Sstevel@tonic-gate 		DBG(1, ("stat failed: %i\n", errno));
2910Sstevel@tonic-gate 		(void) close(fd);
2920Sstevel@tonic-gate 		return (CFGA_ERROR);
2930Sstevel@tonic-gate 	}
2940Sstevel@tonic-gate 	(void) close(fd);
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	devt = statbuf.st_rdev;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	tmp = (char *)(ap_id + sizeof ("/devices") - 1);
2990Sstevel@tonic-gate 	if ((ptr = strrchr(tmp, ':')) != NULL)
3000Sstevel@tonic-gate 		*ptr = '\0';
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	ap_node = di_init(tmp, DINFOPROP | DINFOMINOR);
3030Sstevel@tonic-gate 	if (ap_node == DI_NODE_NIL) {
3040Sstevel@tonic-gate 		DBG(1, ("dead %i\n", errno));
3050Sstevel@tonic-gate 		return (CFGA_ERROR);
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate #ifdef	PCIHP_DBG
3090Sstevel@tonic-gate 	ptr = di_devfs_path(ap_node);
3100Sstevel@tonic-gate 	DBG(1, ("get_occupants: %s\n", ptr));
3110Sstevel@tonic-gate 	di_devfs_path_free(ptr);
3120Sstevel@tonic-gate #endif
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	if ((rv = di_prop_lookup_strings(devt, ap_node, "pci-occupant",
3150Sstevel@tonic-gate 	    &prop_data)) == -1) {
3160Sstevel@tonic-gate 		DBG(1, ("get_occupants: prop_lookup failed: %i\n", errno));
3170Sstevel@tonic-gate 		di_fini(ap_node);
3180Sstevel@tonic-gate 		return (CFGA_ERROR);
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	if (prop_data && (strcmp(prop_data, "") == 0)) {
3220Sstevel@tonic-gate 		di_fini(ap_node);
3230Sstevel@tonic-gate 		occupant->i = 0;
3240Sstevel@tonic-gate 		occupant->id[0] = NULL;
3250Sstevel@tonic-gate 		return (CFGA_OK);
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	DBG(1, ("get_occupants: %i devices found\n", rv));
3290Sstevel@tonic-gate 	for (occupant->i = 0; occupant->i < rv; occupant->i++) {
3300Sstevel@tonic-gate 		if (occupant->i >= (HPC_MAX_OCCUPANTS - 1)) {
3310Sstevel@tonic-gate 			occupant->i--;
3320Sstevel@tonic-gate 			break;
3330Sstevel@tonic-gate 		}
3340Sstevel@tonic-gate 		occupant->id[occupant->i] = (char *)malloc(
3350Sstevel@tonic-gate 			strlen(prop_data) + sizeof ("/devices"));
3360Sstevel@tonic-gate 		(void) snprintf(occupant->id[occupant->i], strlen(prop_data) +
3370Sstevel@tonic-gate 		    sizeof ("/devices"), "/devices%s", prop_data);
3380Sstevel@tonic-gate 		DBG(1, ("%s\n", occupant->id[occupant->i]));
3390Sstevel@tonic-gate 		prop_data += strlen(prop_data) + 1;
3400Sstevel@tonic-gate 	}
3410Sstevel@tonic-gate 	di_fini(ap_node);
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	occupant->id[occupant->i] = NULL;
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	return (CFGA_OK);
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate /*
3490Sstevel@tonic-gate  * let rcm know that the device has indeed been removed and clean
3500Sstevel@tonic-gate  * up rcm data
3510Sstevel@tonic-gate  */
3520Sstevel@tonic-gate static void
confirm_rcm(hpc_occupant_info_t * occupant,rcm_handle_t * rhandle)3530Sstevel@tonic-gate confirm_rcm(hpc_occupant_info_t *occupant, rcm_handle_t *rhandle)
3540Sstevel@tonic-gate {
3550Sstevel@tonic-gate 	DBG(1, ("confirm_rcm\n"));
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	if (occupant->i == 0) /* nothing was found to ask rcm about */
3580Sstevel@tonic-gate 		return;
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	(void) rcm_notify_remove_list(rhandle, occupant->id, 0, NULL);
3610Sstevel@tonic-gate 	(void) rcm_free_handle(rhandle);
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	for (; occupant->i >= 0; occupant->i--)
3640Sstevel@tonic-gate 		free(occupant->id[occupant->i]);
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate static void
fail_rcm(hpc_occupant_info_t * occupant,rcm_handle_t * rhandle)3680Sstevel@tonic-gate fail_rcm(hpc_occupant_info_t *occupant, rcm_handle_t *rhandle)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate 	DBG(1, ("fail_rcm\n"));
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	if (occupant->i == 0) /* nothing was found to ask rcm about */
3730Sstevel@tonic-gate 		return;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	(void) rcm_notify_online_list(rhandle, occupant->id, 0, NULL);
3760Sstevel@tonic-gate 	(void) rcm_free_handle(rhandle);
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	for (; occupant->i >= 0; occupant->i--)
3790Sstevel@tonic-gate 		free(occupant->id[occupant->i]);
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate /*
3830Sstevel@tonic-gate  * copied from scsi_rcm_info_table
3840Sstevel@tonic-gate  *
3850Sstevel@tonic-gate  *      Takes an opaque rcm_info_t pointer and a character pointer, and appends
3860Sstevel@tonic-gate  * the rcm_info_t data in the form of a table to the given character pointer.
3870Sstevel@tonic-gate  */
3880Sstevel@tonic-gate static void
pci_rcm_info_table(rcm_info_t * rinfo,char ** table)3890Sstevel@tonic-gate pci_rcm_info_table(rcm_info_t *rinfo, char **table)
3900Sstevel@tonic-gate {
3910Sstevel@tonic-gate 	int i;
3920Sstevel@tonic-gate 	size_t w;
3930Sstevel@tonic-gate 	size_t width = 0;
3940Sstevel@tonic-gate 	size_t w_rsrc = 0;
3950Sstevel@tonic-gate 	size_t w_info = 0;
3960Sstevel@tonic-gate 	size_t table_size = 0;
3970Sstevel@tonic-gate 	uint_t tuples = 0;
3980Sstevel@tonic-gate 	rcm_info_tuple_t *tuple = NULL;
3990Sstevel@tonic-gate 	char *rsrc;
4000Sstevel@tonic-gate 	char *info;
4010Sstevel@tonic-gate 	char *newtable;
4020Sstevel@tonic-gate 	static char format[MAX_FORMAT];
4030Sstevel@tonic-gate 	const char *infostr;
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	/* Protect against invalid arguments */
4060Sstevel@tonic-gate 	if (rinfo == NULL || table == NULL)
4070Sstevel@tonic-gate 		return;
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	/* Set localized table header strings */
4100Sstevel@tonic-gate 	rsrc = dgettext(TEXT_DOMAIN, "Resource");
4110Sstevel@tonic-gate 	info = dgettext(TEXT_DOMAIN, "Information");
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	/* A first pass, to size up the RCM information */
4140Sstevel@tonic-gate 	while (tuple = rcm_info_next(rinfo, tuple)) {
4150Sstevel@tonic-gate 		if ((infostr = rcm_info_info(tuple)) != NULL) {
4160Sstevel@tonic-gate 			tuples++;
4170Sstevel@tonic-gate 			if ((w = strlen(rcm_info_rsrc(tuple))) > w_rsrc)
4180Sstevel@tonic-gate 				w_rsrc = w;
4190Sstevel@tonic-gate 			if ((w = strlen(infostr)) > w_info)
4200Sstevel@tonic-gate 				w_info = w;
4210Sstevel@tonic-gate 		}
4220Sstevel@tonic-gate 	}
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	/* If nothing was sized up above, stop early */
4250Sstevel@tonic-gate 	if (tuples == 0)
4260Sstevel@tonic-gate 		return;
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	/* Adjust column widths for column headings */
4290Sstevel@tonic-gate 	if ((w = strlen(rsrc)) > w_rsrc)
4300Sstevel@tonic-gate 		w_rsrc = w;
4310Sstevel@tonic-gate 	else if ((w_rsrc - w) % 2)
4320Sstevel@tonic-gate 		w_rsrc++;
4330Sstevel@tonic-gate 	if ((w = strlen(info)) > w_info)
4340Sstevel@tonic-gate 		w_info = w;
4350Sstevel@tonic-gate 	else if ((w_info - w) % 2)
4360Sstevel@tonic-gate 		w_info++;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	/*
4390Sstevel@tonic-gate 	 * Compute the total line width of each line,
4400Sstevel@tonic-gate 	 * accounting for intercolumn spacing.
4410Sstevel@tonic-gate 	 */
4420Sstevel@tonic-gate 	width = w_info + w_rsrc + 4;
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	/* Allocate space for the table */
4450Sstevel@tonic-gate 	table_size = (2 + tuples) * (width + 1) + 2;
4460Sstevel@tonic-gate 	if (*table == NULL) {
4470Sstevel@tonic-gate 		/* zero fill for the strcat() call below */
4480Sstevel@tonic-gate 		*table = calloc(table_size, sizeof (char));
4490Sstevel@tonic-gate 		if (*table == NULL)
4500Sstevel@tonic-gate 			return;
4510Sstevel@tonic-gate 	} else {
4520Sstevel@tonic-gate 		newtable = realloc(*table, strlen(*table) + table_size);
4530Sstevel@tonic-gate 		if (newtable == NULL)
4540Sstevel@tonic-gate 			return;
4550Sstevel@tonic-gate 		else
4560Sstevel@tonic-gate 			*table = newtable;
4570Sstevel@tonic-gate 	}
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	/* Place a table header into the string */
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	/* The resource header */
4620Sstevel@tonic-gate 	(void) strcat(*table, "\n");
4630Sstevel@tonic-gate 	w = strlen(rsrc);
4640Sstevel@tonic-gate 	for (i = 0; i < ((w_rsrc - w) / 2); i++)
4650Sstevel@tonic-gate 		(void) strcat(*table, " ");
4660Sstevel@tonic-gate 	(void) strcat(*table, rsrc);
4670Sstevel@tonic-gate 	for (i = 0; i < ((w_rsrc - w) / 2); i++)
4680Sstevel@tonic-gate 		(void) strcat(*table, " ");
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	/* The information header */
4710Sstevel@tonic-gate 	(void) strcat(*table, "  ");
4720Sstevel@tonic-gate 	w = strlen(info);
4730Sstevel@tonic-gate 	for (i = 0; i < ((w_info - w) / 2); i++)
4740Sstevel@tonic-gate 		(void) strcat(*table, " ");
4750Sstevel@tonic-gate 	(void) strcat(*table, info);
4760Sstevel@tonic-gate 	for (i = 0; i < ((w_info - w) / 2); i++)
4770Sstevel@tonic-gate 		(void) strcat(*table, " ");
4780Sstevel@tonic-gate 	/* Underline the headers */
4790Sstevel@tonic-gate 	(void) strcat(*table, "\n");
4800Sstevel@tonic-gate 	for (i = 0; i < w_rsrc; i++)
4810Sstevel@tonic-gate 		(void) strcat(*table, "-");
4820Sstevel@tonic-gate 	(void) strcat(*table, "  ");
4830Sstevel@tonic-gate 	for (i = 0; i < w_info; i++)
4840Sstevel@tonic-gate 		(void) strcat(*table, "-");
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	/* Construct the format string */
4870Sstevel@tonic-gate 	(void) snprintf(format, MAX_FORMAT, "%%-%ds  %%-%ds",
4880Sstevel@tonic-gate 	    (int)w_rsrc, (int)w_info);
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	/* Add the tuples to the table string */
4910Sstevel@tonic-gate 	tuple = NULL;
4920Sstevel@tonic-gate 	while ((tuple = rcm_info_next(rinfo, tuple)) != NULL) {
4930Sstevel@tonic-gate 		if ((infostr = rcm_info_info(tuple)) != NULL) {
4940Sstevel@tonic-gate 			(void) strcat(*table, "\n");
4950Sstevel@tonic-gate 			(void) sprintf(&((*table)[strlen(*table)]),
4960Sstevel@tonic-gate 			    format, rcm_info_rsrc(tuple),
4970Sstevel@tonic-gate 			    infostr);
4980Sstevel@tonic-gate 		}
4990Sstevel@tonic-gate 	}
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate /*
5030Sstevel@tonic-gate  * Figure out what device is about to be unconfigured or disconnected
5040Sstevel@tonic-gate  * and make sure rcm is ok with it.
5050Sstevel@tonic-gate  * hangs on to a list of handles so they can then be confirmed or denied
5060Sstevel@tonic-gate  * if either getting the occupant list or talking to rcm fails
5070Sstevel@tonic-gate  * return CFGA_ERROR so that things can go on without rcm
5080Sstevel@tonic-gate  */
5090Sstevel@tonic-gate static int
check_rcm(const char * ap_id,hpc_occupant_info_t * occupant,rcm_handle_t ** rhandlep,char ** errstring,cfga_flags_t flags)5100Sstevel@tonic-gate check_rcm(const char *ap_id, hpc_occupant_info_t *occupant,
5110Sstevel@tonic-gate     rcm_handle_t **rhandlep, char **errstring, cfga_flags_t flags)
5120Sstevel@tonic-gate {
5130Sstevel@tonic-gate 	int rv;
5140Sstevel@tonic-gate 	rcm_info_t *rinfo;
5150Sstevel@tonic-gate 	rcm_handle_t *rhandle;
5160Sstevel@tonic-gate 	uint_t rcmflags;
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	if (get_occupants(ap_id, occupant) != 0) {
5190Sstevel@tonic-gate 		DBG(1, ("check_rcm: failed to get occupants\n"));
5200Sstevel@tonic-gate 		return (CFGA_ERROR);
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	if (occupant->i == 0) {
5240Sstevel@tonic-gate 		DBG(1, ("check_rcm: no drivers attaching to occupants\n"));
5250Sstevel@tonic-gate 		return (CFGA_OK);
5260Sstevel@tonic-gate 	}
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	if (rcm_alloc_handle(NULL, 0, NULL, &rhandle)
5290Sstevel@tonic-gate 	    != RCM_SUCCESS) {
5300Sstevel@tonic-gate 		DBG(1, ("check_rcm: blocked by rcm failure\n"));
5310Sstevel@tonic-gate 		return (CFGA_ERROR);
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	rcmflags = (flags & CFGA_FLAG_FORCE) ? RCM_FORCE : 0;
5350Sstevel@tonic-gate 	rv = rcm_request_offline_list(rhandle, occupant->id, rcmflags, &rinfo);
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	if (rv == RCM_FAILURE) {
5380Sstevel@tonic-gate 		DBG(1, ("check_rcm: blocked by rcm failure 2\n"));
5390Sstevel@tonic-gate 		pci_rcm_info_table(rinfo, errstring);
5400Sstevel@tonic-gate 		rcm_free_info(rinfo);
5410Sstevel@tonic-gate 		fail_rcm(occupant, rhandle);
5420Sstevel@tonic-gate 		return (CFGA_BUSY);
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 	if (rv == RCM_CONFLICT) {
5450Sstevel@tonic-gate 		DBG(1, ("check_rcm: blocked by %i\n",
5460Sstevel@tonic-gate 		    rcm_info_pid(rinfo)));
5470Sstevel@tonic-gate 		pci_rcm_info_table(rinfo, errstring);
5480Sstevel@tonic-gate 		rcm_free_info(rinfo);
5490Sstevel@tonic-gate 		(void) rcm_free_handle(rhandle);
5500Sstevel@tonic-gate 		for (; occupant->i >= 0; occupant->i--)
5510Sstevel@tonic-gate 			free(occupant->id[occupant->i]);
5520Sstevel@tonic-gate 		return (CFGA_BUSY);
5530Sstevel@tonic-gate 	}
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	rcm_free_info(rinfo);
5560Sstevel@tonic-gate 	*rhandlep = rhandle;
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	/* else */
5590Sstevel@tonic-gate 	return (CFGA_OK);
5600Sstevel@tonic-gate }
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate /*
5640Sstevel@tonic-gate  * Transitional Diagram:
5650Sstevel@tonic-gate  *
5660Sstevel@tonic-gate  *  empty		unconfigure
5670Sstevel@tonic-gate  * (remove)	^|  (physically insert card)
5680Sstevel@tonic-gate  *			|V
5690Sstevel@tonic-gate  * disconnect	configure
5700Sstevel@tonic-gate  * "-c DISCONNECT"	^|	"-c CONNECT"
5710Sstevel@tonic-gate  *				|V	"-c CONFIGURE"
5720Sstevel@tonic-gate  * connect	unconfigure	->	connect    configure
5730Sstevel@tonic-gate  *						<-
5740Sstevel@tonic-gate  *					"-c UNCONFIGURE"
5750Sstevel@tonic-gate  *
5760Sstevel@tonic-gate  */
5770Sstevel@tonic-gate /*ARGSUSED*/
5780Sstevel@tonic-gate cfga_err_t
cfga_change_state(cfga_cmd_t state_change_cmd,const char * ap_id,const char * options,struct cfga_confirm * confp,struct cfga_msg * msgp,char ** errstring,cfga_flags_t flags)5790Sstevel@tonic-gate cfga_change_state(cfga_cmd_t state_change_cmd, const char *ap_id,
5800Sstevel@tonic-gate     const char *options, struct cfga_confirm *confp,
5810Sstevel@tonic-gate     struct cfga_msg *msgp, char **errstring, cfga_flags_t flags)
5820Sstevel@tonic-gate {
5830Sstevel@tonic-gate 	int rv;
5840Sstevel@tonic-gate 	devctl_hdl_t		dcp;
5850Sstevel@tonic-gate 	devctl_ap_state_t	state;
5860Sstevel@tonic-gate 	ap_rstate_t		rs;
5870Sstevel@tonic-gate 	ap_ostate_t		os;
5880Sstevel@tonic-gate 	hpc_occupant_info_t occupants;
5890Sstevel@tonic-gate 	rcm_handle_t *rhandle;
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	if ((rv = check_options(options)) != CFGA_OK) {
5920Sstevel@tonic-gate 		return (rv);
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	if (errstring != NULL)
5960Sstevel@tonic-gate 		*errstring = NULL;
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	rv = CFGA_OK;
5990Sstevel@tonic-gate 	DBG(1, ("cfga_change_state:(%s)\n", ap_id));
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	if ((dcp = devctl_ap_acquire((char *)ap_id, 0)) == NULL) {
6020Sstevel@tonic-gate 		if (rv == EBUSY) {
6030Sstevel@tonic-gate 			cfga_err(errstring, CMD_ACQUIRE, ap_id, 0);
6040Sstevel@tonic-gate 			DBG(1, ("cfga_change_state: device is busy\n"));
6050Sstevel@tonic-gate 			rv = CFGA_BUSY;
6060Sstevel@tonic-gate 		} else
6070Sstevel@tonic-gate 			rv = CFGA_ERROR;
6080Sstevel@tonic-gate 		return (rv);
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	if (devctl_ap_getstate(dcp, NULL, &state) == -1) {
6120Sstevel@tonic-gate 		DBG(2, ("cfga_change_state: devctl ap getstate failed\n"));
6130Sstevel@tonic-gate 		cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
6140Sstevel@tonic-gate 		devctl_release((devctl_hdl_t)dcp);
6150Sstevel@tonic-gate 		if (rv == EBUSY)
6160Sstevel@tonic-gate 			rv = CFGA_BUSY;
6170Sstevel@tonic-gate 		else
6180Sstevel@tonic-gate 			rv = CFGA_ERROR;
6190Sstevel@tonic-gate 		return (rv);
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	rs = state.ap_rstate;
6230Sstevel@tonic-gate 	os = state.ap_ostate;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	DBG(1, ("cfga_change_state: rs is %d\n", state.ap_rstate));
6260Sstevel@tonic-gate 	DBG(1, ("cfga_change_state: os is %d\n", state.ap_ostate));
6270Sstevel@tonic-gate 	switch (state_change_cmd) {
6280Sstevel@tonic-gate 	case CFGA_CMD_CONNECT:
6292312Skd93003 		if ((rs == AP_RSTATE_EMPTY) ||
6302312Skd93003 		    (rs == AP_RSTATE_CONNECTED) ||
6310Sstevel@tonic-gate 		    (os == AP_OSTATE_CONFIGURED)) {
6320Sstevel@tonic-gate 			cfga_err(errstring, ERR_AP_ERR, 0);
6330Sstevel@tonic-gate 			rv = CFGA_INVAL;
6340Sstevel@tonic-gate 		} else {
6350Sstevel@tonic-gate 			/* Lets connect the slot */
6360Sstevel@tonic-gate 			if (devctl_ap_connect(dcp, NULL) == -1) {
6370Sstevel@tonic-gate 				rv = CFGA_ERROR;
6380Sstevel@tonic-gate 				cfga_err(errstring,
6390Sstevel@tonic-gate 				    CMD_SLOT_CONNECT, 0);
6400Sstevel@tonic-gate 			}
6410Sstevel@tonic-gate 		}
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 		break;
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	case CFGA_CMD_DISCONNECT:
6460Sstevel@tonic-gate 		DBG(1, ("disconnect\n"));
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 		if (os == AP_OSTATE_CONFIGURED) {
6490Sstevel@tonic-gate 			if ((rv = check_rcm(ap_id, &occupants, &rhandle,
6500Sstevel@tonic-gate 			    errstring, flags)) == CFGA_BUSY) {
6510Sstevel@tonic-gate 				break;
6520Sstevel@tonic-gate 			} else if (rv == CFGA_OK) {
6530Sstevel@tonic-gate 				if (devctl_ap_unconfigure(dcp, NULL) == -1) {
6540Sstevel@tonic-gate 					if (errno == EBUSY)
6550Sstevel@tonic-gate 						rv = CFGA_BUSY;
6560Sstevel@tonic-gate 					else
6570Sstevel@tonic-gate 						rv = CFGA_ERROR;
6580Sstevel@tonic-gate 					cfga_err(errstring,
6590Sstevel@tonic-gate 					    CMD_SLOT_DISCONNECT, 0);
6600Sstevel@tonic-gate 					fail_rcm(&occupants, rhandle);
6610Sstevel@tonic-gate 					break;
6620Sstevel@tonic-gate 				} else {
6630Sstevel@tonic-gate 					confirm_rcm(&occupants, rhandle);
6640Sstevel@tonic-gate 				}
6650Sstevel@tonic-gate 			} else { /* rv == CFGA_ERROR */
6660Sstevel@tonic-gate 				if (devctl_ap_unconfigure(dcp, NULL) == -1) {
6670Sstevel@tonic-gate 					if (errno == EBUSY)
6680Sstevel@tonic-gate 						rv = CFGA_BUSY;
6690Sstevel@tonic-gate 					else
6700Sstevel@tonic-gate 						rv = CFGA_ERROR;
6710Sstevel@tonic-gate 					break;
6720Sstevel@tonic-gate 				} else {
6730Sstevel@tonic-gate 					rv = CFGA_OK;
6740Sstevel@tonic-gate 				}
6750Sstevel@tonic-gate 			}
6760Sstevel@tonic-gate 		}
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 		if (rs == AP_RSTATE_CONNECTED) {
6790Sstevel@tonic-gate 			if (devctl_ap_disconnect(dcp, NULL) == -1) {
6800Sstevel@tonic-gate 				rv = CFGA_ERROR;
6810Sstevel@tonic-gate 				cfga_err(errstring, CMD_SLOT_DISCONNECT, 0);
6820Sstevel@tonic-gate 				break;
6830Sstevel@tonic-gate 			}
6840Sstevel@tonic-gate 		} else {
6850Sstevel@tonic-gate 			cfga_err(errstring, ERR_AP_ERR, 0);
6860Sstevel@tonic-gate 			rv = CFGA_INVAL;
6870Sstevel@tonic-gate 		}
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 		break;
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	case CFGA_CMD_CONFIGURE:
6920Sstevel@tonic-gate 		if (rs == AP_RSTATE_DISCONNECTED) {
6930Sstevel@tonic-gate 			if (devctl_ap_connect(dcp, NULL) == -1) {
6940Sstevel@tonic-gate 				rv = CFGA_ERROR;
6950Sstevel@tonic-gate 				cfga_err(errstring, CMD_SLOT_CONNECT, 0);
6960Sstevel@tonic-gate 				break;
6970Sstevel@tonic-gate 			}
6980Sstevel@tonic-gate 		}
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 		/*
7010Sstevel@tonic-gate 		 * for multi-func device we allow multiple
7020Sstevel@tonic-gate 		 * configure on the same slot because one
7030Sstevel@tonic-gate 		 * func can be configured and other one won't
7040Sstevel@tonic-gate 		 */
7050Sstevel@tonic-gate 		if (devctl_ap_configure(dcp, NULL) == -1) {
7060Sstevel@tonic-gate 			rv = CFGA_ERROR;
7070Sstevel@tonic-gate 			cfga_err(errstring, CMD_SLOT_CONFIGURE, 0);
708881Sjohnny 			if ((rs == AP_RSTATE_DISCONNECTED) &&
709881Sjohnny 					(devctl_ap_disconnect(dcp, NULL)
710881Sjohnny 								== -1)) {
7110Sstevel@tonic-gate 				rv = CFGA_ERROR;
7120Sstevel@tonic-gate 				cfga_err(errstring,
7130Sstevel@tonic-gate 				    CMD_SLOT_CONFIGURE, 0);
7140Sstevel@tonic-gate 			}
7150Sstevel@tonic-gate 			break;
7160Sstevel@tonic-gate 		}
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 		break;
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	case CFGA_CMD_UNCONFIGURE:
7210Sstevel@tonic-gate 		DBG(1, ("unconfigure\n"));
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 		if (os == AP_OSTATE_CONFIGURED) {
7240Sstevel@tonic-gate 			if ((rv = check_rcm(ap_id, &occupants, &rhandle,
7250Sstevel@tonic-gate 			    errstring, flags)) == CFGA_BUSY) {
7260Sstevel@tonic-gate 				break;
7270Sstevel@tonic-gate 			} else if (rv == CFGA_OK) {
7280Sstevel@tonic-gate 				if (devctl_ap_unconfigure(dcp, NULL) == -1) {
7290Sstevel@tonic-gate 					if (errno == EBUSY)
7300Sstevel@tonic-gate 						rv = CFGA_BUSY;
7310Sstevel@tonic-gate 					else {
7320Sstevel@tonic-gate 						if (errno == ENOTSUP)
7330Sstevel@tonic-gate 							rv = CFGA_OPNOTSUPP;
7340Sstevel@tonic-gate 						else
7350Sstevel@tonic-gate 							rv = CFGA_ERROR;
7360Sstevel@tonic-gate 					}
7370Sstevel@tonic-gate 					cfga_err(errstring,
7380Sstevel@tonic-gate 					    CMD_SLOT_UNCONFIGURE, 0);
7390Sstevel@tonic-gate 					fail_rcm(&occupants, rhandle);
7400Sstevel@tonic-gate 				} else {
7410Sstevel@tonic-gate 					confirm_rcm(&occupants, rhandle);
7420Sstevel@tonic-gate 				}
7430Sstevel@tonic-gate 			} else { /* rv == CFGA_ERROR */
7440Sstevel@tonic-gate 				if (devctl_ap_unconfigure(dcp, NULL) == -1) {
7450Sstevel@tonic-gate 					if (errno == EBUSY)
7460Sstevel@tonic-gate 						rv = CFGA_BUSY;
7470Sstevel@tonic-gate 					else {
7480Sstevel@tonic-gate 						if (errno == ENOTSUP)
7490Sstevel@tonic-gate 							rv = CFGA_OPNOTSUPP;
7500Sstevel@tonic-gate 						else
7510Sstevel@tonic-gate 							rv = CFGA_ERROR;
7520Sstevel@tonic-gate 					}
7530Sstevel@tonic-gate 					cfga_err(errstring,
7540Sstevel@tonic-gate 					    CMD_SLOT_UNCONFIGURE, 0);
7550Sstevel@tonic-gate 				} else {
7560Sstevel@tonic-gate 					rv = CFGA_OK;
7570Sstevel@tonic-gate 				}
7580Sstevel@tonic-gate 			}
7590Sstevel@tonic-gate 		} else {
7600Sstevel@tonic-gate 			cfga_err(errstring, ERR_AP_ERR, 0);
7610Sstevel@tonic-gate 			rv = CFGA_INVAL;
7620Sstevel@tonic-gate 		}
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 		DBG(1, ("uncofigure rv:(%i)\n", rv));
7650Sstevel@tonic-gate 		break;
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	case CFGA_CMD_LOAD:
7680Sstevel@tonic-gate 		if ((os == AP_OSTATE_UNCONFIGURED) &&
7690Sstevel@tonic-gate 		    (rs == AP_RSTATE_DISCONNECTED)) {
7700Sstevel@tonic-gate 			if (devctl_ap_insert(dcp, NULL) == -1) {
7710Sstevel@tonic-gate 				rv = CFGA_ERROR;
7720Sstevel@tonic-gate 				cfga_err(errstring, CMD_SLOT_INSERT, 0);
7730Sstevel@tonic-gate 			}
7740Sstevel@tonic-gate 		} else {
7750Sstevel@tonic-gate 			cfga_err(errstring, ERR_AP_ERR, 0);
7760Sstevel@tonic-gate 			rv = CFGA_INVAL;
7770Sstevel@tonic-gate 		}
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 		break;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	case CFGA_CMD_UNLOAD:
7820Sstevel@tonic-gate 		if ((os == AP_OSTATE_UNCONFIGURED) &&
7830Sstevel@tonic-gate 		    (rs == AP_RSTATE_DISCONNECTED)) {
7840Sstevel@tonic-gate 			if (devctl_ap_remove(dcp, NULL) == -1) {
7850Sstevel@tonic-gate 				rv = CFGA_ERROR;
7860Sstevel@tonic-gate 				cfga_err(errstring, CMD_SLOT_REMOVE, 0);
7870Sstevel@tonic-gate 			}
7880Sstevel@tonic-gate 		} else {
7890Sstevel@tonic-gate 				cfga_err(errstring, ERR_AP_ERR, 0);
7900Sstevel@tonic-gate 				rv = CFGA_INVAL;
7910Sstevel@tonic-gate 			}
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 		break;
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	default:
7960Sstevel@tonic-gate 		rv = CFGA_OPNOTSUPP;
7970Sstevel@tonic-gate 		break;
7980Sstevel@tonic-gate 	}
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	devctl_release((devctl_hdl_t)dcp);
8010Sstevel@tonic-gate 	return (rv);
8020Sstevel@tonic-gate }
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate /*
8050Sstevel@tonic-gate  * Building iocdatat to pass it to nexus
8060Sstevel@tonic-gate  *
8070Sstevel@tonic-gate  *	iocdata->cmd ==  HPC_CTRL_ENABLE_SLOT/HPC_CTRL_DISABLE_SLOT
8080Sstevel@tonic-gate  *			HPC_CTRL_ENABLE_AUTOCFG/HPC_CTRL_DISABLE_AUTOCFG
8090Sstevel@tonic-gate  *			HPC_CTRL_GET_LED_STATE/HPC_CTRL_SET_LED_STATE
8100Sstevel@tonic-gate  *			HPC_CTRL_GET_SLOT_STATE/HPC_CTRL_GET_SLOT_INFO
8110Sstevel@tonic-gate  *			HPC_CTRL_DEV_CONFIGURE/HPC_CTRL_DEV_UNCONFIGURE
8120Sstevel@tonic-gate  *			HPC_CTRL_GET_BOARD_TYPE
8130Sstevel@tonic-gate  *
8140Sstevel@tonic-gate  */
8150Sstevel@tonic-gate static void
build_control_data(struct hpc_control_data * iocdata,uint_t cmd,void * retdata)8160Sstevel@tonic-gate build_control_data(struct hpc_control_data *iocdata, uint_t cmd,
8170Sstevel@tonic-gate     void *retdata)
8180Sstevel@tonic-gate {
8190Sstevel@tonic-gate 	iocdata->cmd = cmd;
8200Sstevel@tonic-gate 	iocdata->data = retdata;
8210Sstevel@tonic-gate }
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate /*
8240Sstevel@tonic-gate  * building logical name from ap_id
8250Sstevel@tonic-gate  */
8260Sstevel@tonic-gate /*ARGSUSED2*/
8270Sstevel@tonic-gate static void
get_logical_name(const char * ap_id,char * buf,dev_t rdev)8280Sstevel@tonic-gate get_logical_name(const char *ap_id, char *buf, dev_t rdev)
8290Sstevel@tonic-gate {
8300Sstevel@tonic-gate 	char *bufptr, *bufptr2, *pci, *apid;
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	DBG(1, ("get_logical_name: %s\n", ap_id));
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	if ((apid = malloc(MAXPATHLEN)) == NULL) {
8350Sstevel@tonic-gate 		DBG(1, ("malloc failed\n"));
8360Sstevel@tonic-gate 		return;
8370Sstevel@tonic-gate 	}
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	(void) memset(apid, 0, MAXPATHLEN);
8400Sstevel@tonic-gate 	(void) strncpy(apid, ap_id, strlen(ap_id));
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	/* needs to look for last /, not first */
8430Sstevel@tonic-gate 	bufptr = strrchr(apid, '/');
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	bufptr2 = strrchr(apid, ':');
8460Sstevel@tonic-gate 	pci = ++bufptr;
8470Sstevel@tonic-gate 	bufptr = strchr(pci, ',');
8480Sstevel@tonic-gate 	if (bufptr != NULL) {
8490Sstevel@tonic-gate 		*bufptr = '\0';
8500Sstevel@tonic-gate 	}
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	bufptr = strchr(pci, '@');
8530Sstevel@tonic-gate 	if (bufptr != NULL) {
8540Sstevel@tonic-gate 		*bufptr = '\0';
8550Sstevel@tonic-gate 		bufptr++;
8560Sstevel@tonic-gate 	}
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	DBG(1, ("%s\n%s\n%s\n", pci, bufptr, bufptr2));
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	(void) strcat(buf, pci);
8610Sstevel@tonic-gate 	(void) strcat(buf, bufptr);
8620Sstevel@tonic-gate 	(void) strcat(buf, bufptr2);
8630Sstevel@tonic-gate 	free(apid);
8640Sstevel@tonic-gate }
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate static cfga_err_t
prt_led_mode(const char * ap_id,int repeat,char ** errstring,struct cfga_msg * msgp)8670Sstevel@tonic-gate prt_led_mode(const char *ap_id, int repeat, char **errstring,
8680Sstevel@tonic-gate     struct cfga_msg *msgp)
8690Sstevel@tonic-gate {
8700Sstevel@tonic-gate 	hpc_led_info_t	power_led_info = {HPC_POWER_LED, 0};
8710Sstevel@tonic-gate 	hpc_led_info_t	fault_led_info = {HPC_FAULT_LED, 0};
8720Sstevel@tonic-gate 	hpc_led_info_t	attn_led_info = {HPC_ATTN_LED, 0};
8730Sstevel@tonic-gate 	hpc_led_info_t	active_led_info = {HPC_ACTIVE_LED, 0};
8740Sstevel@tonic-gate 	struct hpc_control_data iocdata;
8750Sstevel@tonic-gate 	struct stat	statbuf;
8760Sstevel@tonic-gate 	char  *buff;
8770Sstevel@tonic-gate 	int	fd;
8780Sstevel@tonic-gate 	hpc_slot_info_t		slot_info;
8790Sstevel@tonic-gate 	char *cp, line[MAXLINE];
8800Sstevel@tonic-gate 	int len = MAXLINE;
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	DBG(1, ("prt_led_mod function\n"));
8830Sstevel@tonic-gate 	if (!repeat)
8840Sstevel@tonic-gate 		cfga_msg(msgp, "Ap_Id\t\t\tLed");
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	if ((fd = open(ap_id, O_RDWR)) == -1) {
8870Sstevel@tonic-gate 		DBG(2, ("open = ap_id%s, fd%d\n", ap_id, fd));
8880Sstevel@tonic-gate 		DBG_F(2, (stderr, "open on %s failed\n", ap_id));
8890Sstevel@tonic-gate 		cfga_err(errstring, CMD_OPEN,  ap_id, 0);
8900Sstevel@tonic-gate 		return (CFGA_ERROR);
8910Sstevel@tonic-gate 	}
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	if (fstat(fd, &statbuf) == -1) {
8940Sstevel@tonic-gate 		DBG(2, ("fstat = ap_id%s, fd%d\n", ap_id, fd));
8950Sstevel@tonic-gate 		DBG_F(2, (stderr, "fstat on %s failed\n", ap_id));
8960Sstevel@tonic-gate 		cfga_err(errstring, CMD_FSTAT, ap_id, 0);
8970Sstevel@tonic-gate 		return (CFGA_ERROR);
8980Sstevel@tonic-gate 	}
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	if ((buff = malloc(MAXPATHLEN)) == NULL) {
9010Sstevel@tonic-gate 		cfga_err(errstring, "malloc ", 0);
9020Sstevel@tonic-gate 		return (CFGA_ERROR);
9030Sstevel@tonic-gate 	}
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	(void) memset(buff, 0, MAXPATHLEN);
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	DBG(1, ("ioctl boardtype\n"));
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	build_control_data(&iocdata, HPC_CTRL_GET_SLOT_INFO,
9100Sstevel@tonic-gate 	    (void *)&slot_info);
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	if (ioctl(fd, DEVCTL_AP_CONTROL, &iocdata) == -1) {
9130Sstevel@tonic-gate 		get_logical_name(ap_id, slot_info.pci_slot_name, 0);
9140Sstevel@tonic-gate 		DBG(1, ("ioctl failed slotinfo: %s\n",
9150Sstevel@tonic-gate 		    slot_info.pci_slot_name));
9160Sstevel@tonic-gate 	} else {
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 		/*
9190Sstevel@tonic-gate 		 * the driver will report back things like hpc0_slot0
9200Sstevel@tonic-gate 		 * this needs to be changed to things like pci1:hpc0_slot0
9210Sstevel@tonic-gate 		 */
9220Sstevel@tonic-gate 		if (fix_ap_name(buff, ap_id, slot_info.pci_slot_name,
9230Sstevel@tonic-gate 		    errstring) != CFGA_OK) {
9240Sstevel@tonic-gate 			free(buff);
9250Sstevel@tonic-gate 			(void) close(fd);
9260Sstevel@tonic-gate 			return (CFGA_ERROR);
9270Sstevel@tonic-gate 		}
9280Sstevel@tonic-gate 		DBG(1, ("ioctl slotinfo: %s\n", buff));
9290Sstevel@tonic-gate 	}
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	cp = line;
9320Sstevel@tonic-gate 	(void) snprintf(cp, len, "%s\t\t", buff);
9330Sstevel@tonic-gate 	len -= strlen(cp);
9340Sstevel@tonic-gate 	cp += strlen(cp);
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	free(buff);
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	build_control_data(&iocdata, HPC_CTRL_GET_LED_STATE, &power_led_info);
9390Sstevel@tonic-gate 	if (ioctl(fd, DEVCTL_AP_CONTROL, &iocdata) == -1) {
9400Sstevel@tonic-gate 		(void) snprintf(cp, len, "%s=%s,",
9410Sstevel@tonic-gate 		    led_strs[power_led_info.led], cfga_strs[UNKNOWN]);
9420Sstevel@tonic-gate 		len -= strlen(cp);
9430Sstevel@tonic-gate 		cp += strlen(cp);
9440Sstevel@tonic-gate 	} else {
9450Sstevel@tonic-gate 		(void) snprintf(cp, len, "%s=%s,", led_strs[power_led_info.led],
9460Sstevel@tonic-gate 		    mode_strs[power_led_info.state]);
9470Sstevel@tonic-gate 		len -= strlen(cp);
9480Sstevel@tonic-gate 		cp += strlen(cp);
9490Sstevel@tonic-gate 	}
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	DBG(1, ("%s:%d\n", led_strs[power_led_info.led], power_led_info.state));
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	build_control_data(&iocdata, HPC_CTRL_GET_LED_STATE, &fault_led_info);
9540Sstevel@tonic-gate 	if (ioctl(fd, DEVCTL_AP_CONTROL, &iocdata) == -1) {
9550Sstevel@tonic-gate 		(void) snprintf(cp, len, "%s=%s,",
9560Sstevel@tonic-gate 		    led_strs[fault_led_info.led], cfga_strs[UNKNOWN]);
9570Sstevel@tonic-gate 		len -= strlen(cp);
9580Sstevel@tonic-gate 		cp += strlen(cp);
9590Sstevel@tonic-gate 	} else {
9600Sstevel@tonic-gate 		(void) snprintf(cp, len, "%s=%s,",
9610Sstevel@tonic-gate 		    led_strs[fault_led_info.led],
9620Sstevel@tonic-gate 		    mode_strs[fault_led_info.state]);
9630Sstevel@tonic-gate 		len -= strlen(cp);
9640Sstevel@tonic-gate 		cp += strlen(cp);
9650Sstevel@tonic-gate 	}
9660Sstevel@tonic-gate 	DBG(1, ("%s:%d\n", led_strs[fault_led_info.led], fault_led_info.state));
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	build_control_data(&iocdata, HPC_CTRL_GET_LED_STATE, &attn_led_info);
9690Sstevel@tonic-gate 	if (ioctl(fd, DEVCTL_AP_CONTROL, &iocdata) == -1) {
9700Sstevel@tonic-gate 		(void) snprintf(cp, len, "%s=%s,",
9710Sstevel@tonic-gate 		    led_strs[attn_led_info.led], cfga_strs[UNKNOWN]);
9720Sstevel@tonic-gate 		len -= strlen(cp);
9730Sstevel@tonic-gate 		cp += strlen(cp);
9740Sstevel@tonic-gate 	} else {
9750Sstevel@tonic-gate 		(void) snprintf(cp, len, "%s=%s,",
9760Sstevel@tonic-gate 		    led_strs[attn_led_info.led],
9770Sstevel@tonic-gate 		    mode_strs[attn_led_info.state]);
9780Sstevel@tonic-gate 		len -= strlen(cp);
9790Sstevel@tonic-gate 		cp += strlen(cp);
9800Sstevel@tonic-gate 	}
9810Sstevel@tonic-gate 	DBG(1, ("%s:%d\n", led_strs[attn_led_info.led], attn_led_info.state));
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	build_control_data(&iocdata, HPC_CTRL_GET_LED_STATE, &active_led_info);
9840Sstevel@tonic-gate 	if (ioctl(fd, DEVCTL_AP_CONTROL, &iocdata) == -1) {
9850Sstevel@tonic-gate 		(void) snprintf(cp, len, "%s=%s", led_strs[active_led_info.led],
9860Sstevel@tonic-gate 		    cfga_strs[UNKNOWN]);
9870Sstevel@tonic-gate 	} else {
9880Sstevel@tonic-gate 		(void) snprintf(cp, len, "%s=%s",
9890Sstevel@tonic-gate 		    led_strs[active_led_info.led],
9900Sstevel@tonic-gate 		    mode_strs[active_led_info.state]);
9910Sstevel@tonic-gate 	}
9920Sstevel@tonic-gate 	cfga_msg(msgp, line);	/* print the message */
9930Sstevel@tonic-gate 	DBG(1, ("%s:%d\n", led_strs[active_led_info.led],
9940Sstevel@tonic-gate 	    active_led_info.state));
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	(void) close(fd);
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	return (CFGA_OK);
9990Sstevel@tonic-gate }
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate /*ARGSUSED*/
10020Sstevel@tonic-gate cfga_err_t
cfga_private_func(const char * function,const char * ap_id,const char * options,struct cfga_confirm * confp,struct cfga_msg * msgp,char ** errstring,cfga_flags_t flags)10030Sstevel@tonic-gate cfga_private_func(const char *function, const char *ap_id,
10040Sstevel@tonic-gate     const char *options, struct cfga_confirm *confp,
10050Sstevel@tonic-gate     struct cfga_msg *msgp, char **errstring, cfga_flags_t flags)
10060Sstevel@tonic-gate {
10070Sstevel@tonic-gate 	char *str;
10080Sstevel@tonic-gate 	int   len, fd, i = 0, repeat = 0;
10090Sstevel@tonic-gate 	char buf[MAXNAMELEN];
10100Sstevel@tonic-gate 	char ptr;
10110Sstevel@tonic-gate 	hpc_led_info_t	led_info;
10120Sstevel@tonic-gate 	struct hpc_control_data	iocdata;
10130Sstevel@tonic-gate 	cfga_err_t rv;
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 	DBG(1, ("cfgadm_private_func: ap_id:%s\n", ap_id));
10160Sstevel@tonic-gate 	DBG(2, ("  options: %s\n", (options == NULL)?"null":options));
10170Sstevel@tonic-gate 	DBG(2, ("  confp: %x\n", confp));
10180Sstevel@tonic-gate 	DBG(2, ("  cfga_msg: %x\n", cfga_msg));
10190Sstevel@tonic-gate 	DBG(2, ("  flag: %d\n", flags));
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	if ((rv = check_options(options)) != CFGA_OK) {
10220Sstevel@tonic-gate 		return (rv);
10230Sstevel@tonic-gate 	}
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	if (private_check == confp)
10260Sstevel@tonic-gate 		repeat = 1;
10270Sstevel@tonic-gate 	else
10280Sstevel@tonic-gate 		private_check = (void*)confp;
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 	/* XXX change const 6 to func_str[i] != NULL */
10310Sstevel@tonic-gate 	for (i = 0, str = func_strs[i], len = strlen(str); i < 6; i++) {
10320Sstevel@tonic-gate 		str = func_strs[i];
10330Sstevel@tonic-gate 		len = strlen(str);
10340Sstevel@tonic-gate 		if (strncmp(function, str, len) == 0)
10350Sstevel@tonic-gate 			break;
10360Sstevel@tonic-gate 	}
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 	switch (i) {
10390Sstevel@tonic-gate 		case ENABLE_SLOT:
10400Sstevel@tonic-gate 			build_control_data(&iocdata,
10410Sstevel@tonic-gate 				HPC_CTRL_ENABLE_SLOT, 0);
10420Sstevel@tonic-gate 			break;
10430Sstevel@tonic-gate 		case DISABLE_SLOT:
10440Sstevel@tonic-gate 			build_control_data(&iocdata,
10450Sstevel@tonic-gate 				HPC_CTRL_DISABLE_SLOT, 0);
10460Sstevel@tonic-gate 			break;
10470Sstevel@tonic-gate 		case ENABLE_AUTOCNF:
10480Sstevel@tonic-gate 			build_control_data(&iocdata,
10490Sstevel@tonic-gate 				HPC_CTRL_ENABLE_AUTOCFG, 0);
10500Sstevel@tonic-gate 			break;
10510Sstevel@tonic-gate 		case DISABLE_AUTOCNF:
10520Sstevel@tonic-gate 			build_control_data(&iocdata,
10530Sstevel@tonic-gate 				HPC_CTRL_DISABLE_AUTOCFG, 0);
10540Sstevel@tonic-gate 			break;
10550Sstevel@tonic-gate 		case LED:
10560Sstevel@tonic-gate 			/* set mode */
10570Sstevel@tonic-gate 			ptr = function[len++];
10580Sstevel@tonic-gate 			if (ptr == '=') {
10590Sstevel@tonic-gate 				str = (char *)function;
10600Sstevel@tonic-gate 				for (str = (str+len++), i = 0; *str != ',';
10610Sstevel@tonic-gate 				    i++, str++) {
10620Sstevel@tonic-gate 					if (i == (MAXNAMELEN - 1))
10630Sstevel@tonic-gate 						break;
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 					buf[i] = *str;
10660Sstevel@tonic-gate 					DBG_F(2, (stdout, "%c\n", buf[i]));
10670Sstevel@tonic-gate 				}
10680Sstevel@tonic-gate 				buf[i] = '\0'; str++;
10690Sstevel@tonic-gate 				DBG(2, ("buf = %s\n", buf));
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 				/* ACTIVE=3,ATTN=2,POWER=1,FAULT=0 */
10720Sstevel@tonic-gate 				if (strcmp(buf, led_strs[POWER]) == 0)
10730Sstevel@tonic-gate 					led_info.led = HPC_POWER_LED;
10740Sstevel@tonic-gate 				else if (strcmp(buf, led_strs[FAULT]) == 0)
10750Sstevel@tonic-gate 					led_info.led = HPC_FAULT_LED;
10760Sstevel@tonic-gate 				else if (strcmp(buf, led_strs[ATTN]) == 0)
10770Sstevel@tonic-gate 					led_info.led = HPC_ATTN_LED;
10780Sstevel@tonic-gate 				else if (strcmp(buf, led_strs[ACTIVE]) == 0)
10790Sstevel@tonic-gate 					led_info.led = HPC_ACTIVE_LED;
10800Sstevel@tonic-gate 				else return (CFGA_INVAL);
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 				len = strlen(func_strs[MODE]);
10830Sstevel@tonic-gate 				if ((strncmp(str, func_strs[MODE], len) == 0) &&
10840Sstevel@tonic-gate 				    (*(str+(len)) == '=')) {
10850Sstevel@tonic-gate 				    for (str = (str+(++len)), i = 0;
10860Sstevel@tonic-gate 					*str != NULL; i++, str++) {
10870Sstevel@tonic-gate 						buf[i] = *str;
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 				    }
10900Sstevel@tonic-gate 				}
10910Sstevel@tonic-gate 				buf[i] = '\0';
10920Sstevel@tonic-gate 				DBG(2, ("buf_mode= %s\n", buf));
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 				/* ON = 1, OFF = 0 */
10950Sstevel@tonic-gate 				if (strcmp(buf, mode_strs[ON]) == 0)
10960Sstevel@tonic-gate 					led_info.state = HPC_LED_ON;
10970Sstevel@tonic-gate 				else if (strcmp(buf, mode_strs[OFF]) == 0)
10980Sstevel@tonic-gate 					led_info.state = HPC_LED_OFF;
10990Sstevel@tonic-gate 				else if (strcmp(buf, mode_strs[BLINK]) == 0)
11000Sstevel@tonic-gate 					led_info.state = HPC_LED_BLINK;
11010Sstevel@tonic-gate 				else return (CFGA_INVAL);
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 				/* sendin  */
11040Sstevel@tonic-gate 				build_control_data(&iocdata,
11050Sstevel@tonic-gate 				    HPC_CTRL_SET_LED_STATE,
11060Sstevel@tonic-gate 				    (void *)&led_info);
11070Sstevel@tonic-gate 				break;
11080Sstevel@tonic-gate 			} else if (ptr == '\0') {
11090Sstevel@tonic-gate 				/* print mode */
11100Sstevel@tonic-gate 				DBG(1, ("Print mode\n"));
11110Sstevel@tonic-gate 				return (prt_led_mode(ap_id, repeat, errstring,
11120Sstevel@tonic-gate 				    msgp));
11130Sstevel@tonic-gate 			}
11140Sstevel@tonic-gate 		default:
11150Sstevel@tonic-gate 			DBG(1, ("default\n"));
11160Sstevel@tonic-gate 			errno = EINVAL;
11170Sstevel@tonic-gate 			return (CFGA_INVAL);
11180Sstevel@tonic-gate 	}
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 	if ((fd = open(ap_id, O_RDWR)) == -1) {
11210Sstevel@tonic-gate 		DBG(1, ("open failed\n"));
11220Sstevel@tonic-gate 		return (CFGA_ERROR);
11230Sstevel@tonic-gate 	}
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	DBG(1, ("open = ap_id=%s, fd=%d\n", ap_id, fd));
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	if (ioctl(fd, DEVCTL_AP_CONTROL, &iocdata) == -1) {
11280Sstevel@tonic-gate 		DBG(1, ("ioctl failed\n"));
11290Sstevel@tonic-gate 		(void) close(fd);
11300Sstevel@tonic-gate 		return (CFGA_ERROR);
11310Sstevel@tonic-gate 	}
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	(void) close(fd);
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 	return (CFGA_OK);
11360Sstevel@tonic-gate }
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate /*ARGSUSED*/
cfga_test(const char * ap_id,const char * options,struct cfga_msg * msgp,char ** errstring,cfga_flags_t flags)11390Sstevel@tonic-gate cfga_err_t cfga_test(const char *ap_id, const char *options,
11400Sstevel@tonic-gate     struct cfga_msg *msgp, char **errstring, cfga_flags_t flags)
11410Sstevel@tonic-gate {
11420Sstevel@tonic-gate 	cfga_err_t rv;
11430Sstevel@tonic-gate 	if (errstring != NULL)
11440Sstevel@tonic-gate 		*errstring = NULL;
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	if ((rv = check_options(options)) != CFGA_OK) {
11470Sstevel@tonic-gate 		return (rv);
11480Sstevel@tonic-gate 	}
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	DBG(1, ("cfga_test:(%s)\n", ap_id));
11510Sstevel@tonic-gate 	/* will need to implement pci CTRL command */
11520Sstevel@tonic-gate 	return (CFGA_NOTSUPP);
11530Sstevel@tonic-gate }
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate static int
fixup_slotname(int rval,int * intp,struct searcharg * slotarg)11560Sstevel@tonic-gate fixup_slotname(int rval, int *intp, struct searcharg *slotarg)
11570Sstevel@tonic-gate {
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate /*
11600Sstevel@tonic-gate  * The slot-names property describes the external labeling of add-in slots.
11610Sstevel@tonic-gate  * This property is an encoded array, an integer followed by a list of
11620Sstevel@tonic-gate  * strings. The return value from di_prop_lookup_ints for slot-names is -1.
11630Sstevel@tonic-gate  * The expected return value should be the number of elements.
11640Sstevel@tonic-gate  * Di_prop_decode_common does not decode encoded data from software,
11650Sstevel@tonic-gate  * such as the solaris device tree, unlike from the prom.
11660Sstevel@tonic-gate  * Di_prop_decode_common takes the size of the encoded data and mods
11670Sstevel@tonic-gate  * it with the size of int. The size of the encoded data for slot-names is 9
11680Sstevel@tonic-gate  * and the size of int is 4, yielding a non zero result. A value of -1 is used
11690Sstevel@tonic-gate  * to indicate that the number of elements can not be determined.
11700Sstevel@tonic-gate  * Di_prop_decode_common can be modified to decode encoded data from the solaris
11710Sstevel@tonic-gate  * device tree.
11720Sstevel@tonic-gate  */
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	if ((slotarg->slt_name_src == PROM_SLT_NAME) && (rval == -1)) {
11750Sstevel@tonic-gate 		return (DI_WALK_TERMINATE);
11760Sstevel@tonic-gate 	} else {
11770Sstevel@tonic-gate 		int i;
11780Sstevel@tonic-gate 		char *tmptr = (char *)(intp+1);
11790Sstevel@tonic-gate 		DBG(1, ("slot-bitmask: %x \n", *intp));
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 		rval = (rval -1) * 4;
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate 		for (i = 0; i <= slotarg->minor; i++) {
11840Sstevel@tonic-gate 			DBG(2, ("curr slot-name: %s \n", tmptr));
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 			if (i >= MAXDEVS)
11870Sstevel@tonic-gate 				return (DI_WALK_TERMINATE);
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 			if ((*intp >> i) & 1) {
11900Sstevel@tonic-gate 				/* assign tmptr */
11910Sstevel@tonic-gate 				DBG(2, ("slot-name: %s \n", tmptr));
11920Sstevel@tonic-gate 				if (i == slotarg->minor)
11930Sstevel@tonic-gate 					(void) strcpy(slotarg->slotnames[i],
11940Sstevel@tonic-gate 					    tmptr);
11950Sstevel@tonic-gate 				/* wind tmptr to next \0 */
11960Sstevel@tonic-gate 				while (*tmptr != '\0') {
11970Sstevel@tonic-gate 					tmptr++;
11980Sstevel@tonic-gate 				}
11990Sstevel@tonic-gate 				tmptr++;
12000Sstevel@tonic-gate 			} else {
12010Sstevel@tonic-gate 				/* point at unknown string */
12020Sstevel@tonic-gate 				if (i == slotarg->minor)
12030Sstevel@tonic-gate 					(void) strcpy(slotarg->slotnames[i],
12040Sstevel@tonic-gate 					    "unknown");
12050Sstevel@tonic-gate 			}
12060Sstevel@tonic-gate 		}
12070Sstevel@tonic-gate 	}
12080Sstevel@tonic-gate 	return (DI_WALK_TERMINATE);
12090Sstevel@tonic-gate }
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate static int
find_slotname(di_node_t din,di_minor_t dim,void * arg)12120Sstevel@tonic-gate find_slotname(di_node_t din, di_minor_t dim, void *arg)
12130Sstevel@tonic-gate {
12140Sstevel@tonic-gate 	struct searcharg *slotarg = (struct searcharg *)arg;
12150Sstevel@tonic-gate 	di_prom_handle_t ph = (di_prom_handle_t)slotarg->promp;
12160Sstevel@tonic-gate 	di_prom_prop_t	prom_prop;
12170Sstevel@tonic-gate 	di_prop_t	solaris_prop;
12180Sstevel@tonic-gate 	int *intp, rval;
12190Sstevel@tonic-gate 	char *devname;
12200Sstevel@tonic-gate 	char fulldevname[MAXNAMELEN];
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 	slotarg->minor = dim->dev_minor % 256;
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 	DBG(2, ("minor number:(%i)\n", slotarg->minor));
12250Sstevel@tonic-gate 	DBG(2, ("hot plug slots found so far:(%i)\n", 0));
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 	if ((devname = di_devfs_path(din)) != NULL) {
12280Sstevel@tonic-gate 		(void) snprintf(fulldevname, MAXNAMELEN,
12290Sstevel@tonic-gate 		    "/devices%s:%s", devname, di_minor_name(dim));
12300Sstevel@tonic-gate 		di_devfs_path_free(devname);
12310Sstevel@tonic-gate 	}
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	if (strcmp(fulldevname, slotarg->devpath) == 0) {
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 		/*
12360Sstevel@tonic-gate 		 * Check the Solaris device tree first
12370Sstevel@tonic-gate 		 * in the case of a DR operation
12380Sstevel@tonic-gate 		 */
12390Sstevel@tonic-gate 		solaris_prop = di_prop_hw_next(din, DI_PROP_NIL);
12400Sstevel@tonic-gate 		while (solaris_prop != DI_PROP_NIL) {
12410Sstevel@tonic-gate 			if (strcmp("slot-names", di_prop_name(solaris_prop))
12420Sstevel@tonic-gate 			    == 0) {
12430Sstevel@tonic-gate 				rval = di_prop_lookup_ints(DDI_DEV_T_ANY,
12440Sstevel@tonic-gate 				    din, di_prop_name(solaris_prop), &intp);
12450Sstevel@tonic-gate 				slotarg->slt_name_src = SOLARIS_SLT_NAME;
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 				return (fixup_slotname(rval, intp, slotarg));
12480Sstevel@tonic-gate 			}
12490Sstevel@tonic-gate 			solaris_prop = di_prop_hw_next(din, solaris_prop);
12500Sstevel@tonic-gate 		}
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 		/*
12530Sstevel@tonic-gate 		 * Check the prom device tree which is populated at boot.
12540Sstevel@tonic-gate 		 * If this fails, give up and set the slot name to null.
12550Sstevel@tonic-gate 		 */
12560Sstevel@tonic-gate 		prom_prop = di_prom_prop_next(ph, din, DI_PROM_PROP_NIL);
12570Sstevel@tonic-gate 		while (prom_prop != DI_PROM_PROP_NIL) {
12580Sstevel@tonic-gate 			if (strcmp("slot-names", di_prom_prop_name(prom_prop))
12590Sstevel@tonic-gate 			    == 0) {
12600Sstevel@tonic-gate 				rval = di_prom_prop_lookup_ints(ph,
12610Sstevel@tonic-gate 				    din, di_prom_prop_name(prom_prop), &intp);
12620Sstevel@tonic-gate 				slotarg->slt_name_src = PROM_SLT_NAME;
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 				return (fixup_slotname(rval, intp, slotarg));
12650Sstevel@tonic-gate 			}
12660Sstevel@tonic-gate 			prom_prop = di_prom_prop_next(ph, din, prom_prop);
12670Sstevel@tonic-gate 		}
12680Sstevel@tonic-gate 		*slotarg->slotnames[slotarg->minor] = '\0';
12690Sstevel@tonic-gate 		return (DI_WALK_TERMINATE);
12700Sstevel@tonic-gate 	} else
12710Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
12720Sstevel@tonic-gate }
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate static int
find_physical_slot_names(const char * devcomp,struct searcharg * slotarg)12750Sstevel@tonic-gate find_physical_slot_names(const char *devcomp, struct searcharg *slotarg)
12760Sstevel@tonic-gate {
12770Sstevel@tonic-gate 	di_node_t root_node;
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 	DBG(1, ("find_physical_slot_names\n"));
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 	if ((root_node = di_init("/", DINFOCPYALL|DINFOPATH))
12820Sstevel@tonic-gate 		== DI_NODE_NIL) {
12830Sstevel@tonic-gate 		DBG(1, ("di_init() failed\n"));
12840Sstevel@tonic-gate 		return (NULL);
12850Sstevel@tonic-gate 	}
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 	slotarg->devpath = (char *)devcomp;
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	if ((slotarg->promp = di_prom_init()) == DI_PROM_HANDLE_NIL) {
12900Sstevel@tonic-gate 		DBG(1, ("di_prom_init() failed\n"));
12910Sstevel@tonic-gate 		di_fini(root_node);
12920Sstevel@tonic-gate 		return (NULL);
12930Sstevel@tonic-gate 	}
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 	(void) di_walk_minor(root_node, "ddi_ctl:attachment_point:pci",
12960Sstevel@tonic-gate 		0, (void *)slotarg, find_slotname);
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 	di_prom_fini(slotarg->promp);
12990Sstevel@tonic-gate 	di_fini(root_node);
13000Sstevel@tonic-gate 	if (slotarg->slotnames[0] != NULL)
13010Sstevel@tonic-gate 		return (0);
13020Sstevel@tonic-gate 	else
13030Sstevel@tonic-gate 		return (-1);
13040Sstevel@tonic-gate }
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate static void
get_type(hpc_board_type_t boardtype,hpc_card_info_t cardinfo,char * buf)13070Sstevel@tonic-gate get_type(hpc_board_type_t boardtype, hpc_card_info_t cardinfo, char *buf)
13080Sstevel@tonic-gate {
130958Sanish 	int i;
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	DBG(1, ("class: %i\n", cardinfo.base_class));
13120Sstevel@tonic-gate 	DBG(1, ("subclass: %i\n", cardinfo.sub_class));
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	if (cardinfo.base_class == PCI_CLASS_NONE) {
13150Sstevel@tonic-gate 		TPCT("unknown");
13160Sstevel@tonic-gate 		return;
13170Sstevel@tonic-gate 	}
13180Sstevel@tonic-gate 
131958Sanish 	for (i = 0; i < class_pci_items; i++) {
132058Sanish 		if ((cardinfo.base_class == class_pci[i].base_class) &&
132158Sanish 		    (cardinfo.sub_class == class_pci[i].sub_class) &&
132258Sanish 		    (cardinfo.prog_class == class_pci[i].prog_class)) {
132358Sanish 			TPCT(class_pci[i].short_desc);
132458Sanish 			break;
13250Sstevel@tonic-gate 		}
13260Sstevel@tonic-gate 	}
13270Sstevel@tonic-gate 
132858Sanish 	if (i == class_pci_items)
132958Sanish 		TPCT("unknown");
133058Sanish 
13310Sstevel@tonic-gate 	TPCT("/");
13320Sstevel@tonic-gate 	switch (boardtype) {
13330Sstevel@tonic-gate 	case HPC_BOARD_PCI_HOTPLUG:
13340Sstevel@tonic-gate 	case HPC_BOARD_CPCI_NON_HS:
13350Sstevel@tonic-gate 	case HPC_BOARD_CPCI_BASIC_HS:
13360Sstevel@tonic-gate 	case HPC_BOARD_CPCI_FULL_HS:
13370Sstevel@tonic-gate 	case HPC_BOARD_CPCI_HS:
13380Sstevel@tonic-gate 		TPCT(board_strs[boardtype]);
13390Sstevel@tonic-gate 		break;
13400Sstevel@tonic-gate 	case HPC_BOARD_UNKNOWN:
13410Sstevel@tonic-gate 	default:
13420Sstevel@tonic-gate 		TPCT(board_strs[HPC_BOARD_UNKNOWN]);
13430Sstevel@tonic-gate 	}
13440Sstevel@tonic-gate }
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate /*
13470Sstevel@tonic-gate  * call-back function for di_devlink_walk
13480Sstevel@tonic-gate  * if the link lives in /dev/cfg copy its name
13490Sstevel@tonic-gate  */
13500Sstevel@tonic-gate static int
found_devlink(di_devlink_t link,void * ap_log_id)13510Sstevel@tonic-gate found_devlink(di_devlink_t link, void *ap_log_id)
13520Sstevel@tonic-gate {
13530Sstevel@tonic-gate 	if (strncmp("/dev/cfg/", di_devlink_path(link), 9) == 0) {
13540Sstevel@tonic-gate 		/* copy everything but /dev/cfg/ */
13550Sstevel@tonic-gate 		(void) strcpy((char *)ap_log_id, di_devlink_path(link) + 9);
13560Sstevel@tonic-gate 		DBG(1, ("found_devlink: %s\n", (char *)ap_log_id));
13570Sstevel@tonic-gate 		return (DI_WALK_TERMINATE);
13580Sstevel@tonic-gate 	}
13590Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
13600Sstevel@tonic-gate }
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate /*
13630Sstevel@tonic-gate  * Walk throught the cached /dev link tree looking for links to the ap
13640Sstevel@tonic-gate  * if none are found return an error
13650Sstevel@tonic-gate  */
13660Sstevel@tonic-gate static cfga_err_t
check_devlinks(char * ap_log_id,const char * ap_id)13670Sstevel@tonic-gate check_devlinks(char *ap_log_id, const char *ap_id)
13680Sstevel@tonic-gate {
13690Sstevel@tonic-gate 	di_devlink_handle_t hdl;
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 	DBG(1, ("check_devlinks: %s\n", ap_id));
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 	hdl = di_devlink_init(NULL, 0);
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	if (strncmp("/devices/", ap_id, 9) == 0) {
13760Sstevel@tonic-gate 		/* ap_id is a valid minor_path with /devices prepended */
13770Sstevel@tonic-gate 		(void) di_devlink_walk(hdl, NULL, ap_id + 8, DI_PRIMARY_LINK,
13780Sstevel@tonic-gate 		    (void *)ap_log_id, found_devlink);
13790Sstevel@tonic-gate 	} else {
13800Sstevel@tonic-gate 		DBG(1, ("check_devlinks: invalid ap_id: %s\n", ap_id));
13810Sstevel@tonic-gate 		return (CFGA_ERROR);
13820Sstevel@tonic-gate 	}
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 	(void) di_devlink_fini(&hdl);
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 	if (ap_log_id[0] != '\0')
13870Sstevel@tonic-gate 		return (CFGA_OK);
13880Sstevel@tonic-gate 	else
13890Sstevel@tonic-gate 		return (CFGA_ERROR);
13900Sstevel@tonic-gate }
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate /*
13930Sstevel@tonic-gate  * most of this is needed to compensate for
13940Sstevel@tonic-gate  * differences between various platforms
13950Sstevel@tonic-gate  */
13960Sstevel@tonic-gate static cfga_err_t
fix_ap_name(char * ap_log_id,const char * ap_id,char * slot_name,char ** errstring)13970Sstevel@tonic-gate fix_ap_name(char *ap_log_id, const char *ap_id, char *slot_name,
13980Sstevel@tonic-gate     char **errstring)
13990Sstevel@tonic-gate {
14000Sstevel@tonic-gate 	char *buf;
14010Sstevel@tonic-gate 	char *tmp;
14020Sstevel@tonic-gate 	char *ptr;
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 	di_node_t ap_node;
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 	ap_log_id[0] = '\0';
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	if (check_devlinks(ap_log_id, ap_id) == CFGA_OK)
14090Sstevel@tonic-gate 		return (CFGA_OK);
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	DBG(1, ("fix_ap_name: %s\n", ap_id));
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	if ((buf = malloc(strlen(ap_id) + 1)) == NULL) {
14140Sstevel@tonic-gate 		DBG(1, ("malloc failed\n"));
14150Sstevel@tonic-gate 		return (CFGA_ERROR);
14160Sstevel@tonic-gate 	}
14170Sstevel@tonic-gate 	(void) strcpy(buf, ap_id);
14180Sstevel@tonic-gate 	tmp = buf + sizeof ("/devices") - 1;
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	ptr = strchr(tmp, ':');
14210Sstevel@tonic-gate 	ptr[0] = '\0';
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	DBG(1, ("fix_ap_name: %s\n", tmp));
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 	ap_node = di_init(tmp, DINFOMINOR);
14260Sstevel@tonic-gate 	if (ap_node == DI_NODE_NIL) {
14270Sstevel@tonic-gate 		cfga_err(errstring, "di_init ", 0);
14280Sstevel@tonic-gate 		DBG(1, ("fix_ap_name: failed to snapshot node\n"));
14290Sstevel@tonic-gate 		return (CFGA_ERROR);
14300Sstevel@tonic-gate 	}
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 	(void) snprintf(ap_log_id, strlen(ap_id) + 1, "%s%i:%s",
14330Sstevel@tonic-gate 	    di_driver_name(ap_node), di_instance(ap_node), slot_name);
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate 	DBG(1, ("fix_ap_name: %s\n", ap_log_id));
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate 	di_fini(ap_node);
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate 	free(buf);
14400Sstevel@tonic-gate 	return (CFGA_OK);
14410Sstevel@tonic-gate }
14420Sstevel@tonic-gate 
1443*2587Spjha 
1444*2587Spjha static int
findlink_cb(di_devlink_t devlink,void * arg)1445*2587Spjha findlink_cb(di_devlink_t devlink, void *arg)
1446*2587Spjha {
1447*2587Spjha 	(*(char **)arg) = strdup(di_devlink_path(devlink));
1448*2587Spjha 
1449*2587Spjha 	return (DI_WALK_TERMINATE);
1450*2587Spjha }
1451*2587Spjha 
1452*2587Spjha /*
1453*2587Spjha  * returns an allocated string containing the full path to the devlink for
1454*2587Spjha  * <ap_phys_id> in the devlink database; we expect only one devlink per
1455*2587Spjha  * <ap_phys_id> so we return the first encountered
1456*2587Spjha  */
1457*2587Spjha static char *
findlink(char * ap_phys_id)1458*2587Spjha findlink(char *ap_phys_id)
1459*2587Spjha {
1460*2587Spjha 	di_devlink_handle_t hdl;
1461*2587Spjha 	char *path = NULL;
1462*2587Spjha 
1463*2587Spjha 	hdl = di_devlink_init(NULL, 0);
1464*2587Spjha 
1465*2587Spjha 	if (strncmp("/devices/", ap_phys_id, 9) == 0)
1466*2587Spjha 		ap_phys_id += 8;
1467*2587Spjha 
1468*2587Spjha 	(void) di_devlink_walk(hdl, "^cfg/.+$", ap_phys_id, DI_PRIMARY_LINK,
1469*2587Spjha 	    (void *)&path, findlink_cb);
1470*2587Spjha 
1471*2587Spjha 	(void) di_devlink_fini(&hdl);
1472*2587Spjha 	return (path);
1473*2587Spjha }
1474*2587Spjha 
1475*2587Spjha 
1476*2587Spjha /*
1477*2587Spjha  * returns CFGA_OK if it can succesfully retrieve the devlink info associated
1478*2587Spjha  * with devlink for <ap_phys_id> which will be returned through <ap_info>
1479*2587Spjha  */
1480*2587Spjha cfga_err_t
get_dli(char * dlpath,char * ap_info,int ap_info_sz)1481*2587Spjha get_dli(char *dlpath, char *ap_info, int ap_info_sz)
1482*2587Spjha {
1483*2587Spjha 	int fd;
1484*2587Spjha 
1485*2587Spjha 	fd = di_dli_openr(dlpath);
1486*2587Spjha 	if (fd < 0)
1487*2587Spjha 		return (CFGA_ERROR);
1488*2587Spjha 
1489*2587Spjha 	(void) read(fd, ap_info, ap_info_sz);
1490*2587Spjha 	ap_info[ap_info_sz - 1] = '\0';
1491*2587Spjha 
1492*2587Spjha 	di_dli_close(fd);
1493*2587Spjha 	return (CFGA_OK);
1494*2587Spjha }
1495*2587Spjha 
1496*2587Spjha 
14970Sstevel@tonic-gate /*ARGSUSED*/
14980Sstevel@tonic-gate cfga_err_t
cfga_list_ext(const char * ap_id,cfga_list_data_t ** cs,int * nlist,const char * options,const char * listopts,char ** errstring,cfga_flags_t flags)14990Sstevel@tonic-gate cfga_list_ext(const char *ap_id, cfga_list_data_t **cs,
15000Sstevel@tonic-gate     int *nlist, const char *options, const char *listopts, char **errstring,
15010Sstevel@tonic-gate     cfga_flags_t flags)
15020Sstevel@tonic-gate {
15030Sstevel@tonic-gate 	devctl_hdl_t		dcp;
15040Sstevel@tonic-gate 	struct hpc_control_data	iocdata;
15050Sstevel@tonic-gate 	devctl_ap_state_t	state;
15060Sstevel@tonic-gate 	hpc_board_type_t	boardtype;
15070Sstevel@tonic-gate 	hpc_card_info_t		cardinfo;
15080Sstevel@tonic-gate 	hpc_slot_info_t		slot_info;
15090Sstevel@tonic-gate 	struct	searcharg	slotname_arg;
15100Sstevel@tonic-gate 	int			fd;
15110Sstevel@tonic-gate 	int			rv = CFGA_OK;
1512*2587Spjha 	char			*dlpath = NULL;
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	if ((rv = check_options(options)) != CFGA_OK) {
15150Sstevel@tonic-gate 		return (rv);
15160Sstevel@tonic-gate 	}
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate 	if (errstring != NULL)
15190Sstevel@tonic-gate 		*errstring = NULL;
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate 	(void) memset(&slot_info, 0, sizeof (hpc_slot_info_t));
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 	DBG(1, ("cfga_list_ext:(%s)\n", ap_id));
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 	if (cs == NULL || nlist == NULL) {
15260Sstevel@tonic-gate 		rv = CFGA_ERROR;
15270Sstevel@tonic-gate 		return (rv);
15280Sstevel@tonic-gate 	}
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 	*nlist = 1;
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	if ((*cs = malloc(sizeof (cfga_list_data_t))) == NULL) {
15330Sstevel@tonic-gate 		cfga_err(errstring, "malloc ", 0);
15340Sstevel@tonic-gate 		DBG(1, ("malloc failed\n"));
15350Sstevel@tonic-gate 		rv = CFGA_ERROR;
15360Sstevel@tonic-gate 		return (rv);
15370Sstevel@tonic-gate 	}
1538*2587Spjha 	(void) memset(*cs, 0, sizeof (cfga_list_data_t));
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 	if ((dcp = devctl_ap_acquire((char *)ap_id, 0)) == NULL) {
15410Sstevel@tonic-gate 		cfga_err(errstring, CMD_GETSTAT, 0);
15420Sstevel@tonic-gate 		DBG(2, ("cfga_list_ext::(devctl_ap_acquire())\n"));
15430Sstevel@tonic-gate 		rv = CFGA_ERROR;
15440Sstevel@tonic-gate 		return (rv);
15450Sstevel@tonic-gate 	}
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 	if (devctl_ap_getstate(dcp, NULL, &state) == -1) {
15480Sstevel@tonic-gate 		cfga_err(errstring, ERR_AP_ERR, ap_id, 0);
15490Sstevel@tonic-gate 		devctl_release((devctl_hdl_t)dcp);
15500Sstevel@tonic-gate 		DBG(2, ("cfga_list_ext::(devctl_ap_getstate())\n"));
15510Sstevel@tonic-gate 		rv = CFGA_ERROR;
15520Sstevel@tonic-gate 		return (rv);
15530Sstevel@tonic-gate 	}
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 	switch (state.ap_rstate) {
15560Sstevel@tonic-gate 		case AP_RSTATE_EMPTY:
15570Sstevel@tonic-gate 			(*cs)->ap_r_state = CFGA_STAT_EMPTY;
15580Sstevel@tonic-gate 			DBG(2, ("ap_rstate = CFGA_STAT_EMPTY\n"));
15590Sstevel@tonic-gate 			break;
15600Sstevel@tonic-gate 		case AP_RSTATE_DISCONNECTED:
15610Sstevel@tonic-gate 			(*cs)->ap_r_state = CFGA_STAT_DISCONNECTED;
15620Sstevel@tonic-gate 			DBG(2, ("ap_rstate = CFGA_STAT_DISCONNECTED\n"));
15630Sstevel@tonic-gate 			break;
15640Sstevel@tonic-gate 		case AP_RSTATE_CONNECTED:
15650Sstevel@tonic-gate 			(*cs)->ap_r_state = CFGA_STAT_CONNECTED;
15660Sstevel@tonic-gate 			DBG(2, ("ap_rstate = CFGA_STAT_CONNECTED\n"));
15670Sstevel@tonic-gate 			break;
15680Sstevel@tonic-gate 	default:
15690Sstevel@tonic-gate 		cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
15700Sstevel@tonic-gate 		rv = CFGA_ERROR;
15710Sstevel@tonic-gate 		devctl_release((devctl_hdl_t)dcp);
15720Sstevel@tonic-gate 		return (rv);
15730Sstevel@tonic-gate 	}
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 	switch (state.ap_ostate) {
15760Sstevel@tonic-gate 		case AP_OSTATE_CONFIGURED:
15770Sstevel@tonic-gate 			(*cs)->ap_o_state = CFGA_STAT_CONFIGURED;
15780Sstevel@tonic-gate 			DBG(2, ("ap_ostate = CFGA_STAT_CONFIGURED\n"));
15790Sstevel@tonic-gate 			break;
15800Sstevel@tonic-gate 		case AP_OSTATE_UNCONFIGURED:
15810Sstevel@tonic-gate 			(*cs)->ap_o_state = CFGA_STAT_UNCONFIGURED;
15820Sstevel@tonic-gate 			DBG(2, ("ap_ostate = CFGA_STAT_UNCONFIGURED\n"));
15830Sstevel@tonic-gate 			break;
15840Sstevel@tonic-gate 	default:
15850Sstevel@tonic-gate 		cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
15860Sstevel@tonic-gate 		rv = CFGA_ERROR;
15870Sstevel@tonic-gate 		devctl_release((devctl_hdl_t)dcp);
15880Sstevel@tonic-gate 		return (rv);
15890Sstevel@tonic-gate 	}
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 	switch (state.ap_condition) {
15920Sstevel@tonic-gate 		case AP_COND_OK:
15930Sstevel@tonic-gate 			(*cs)->ap_cond = CFGA_COND_OK;
15940Sstevel@tonic-gate 			DBG(2, ("ap_cond = CFGA_COND_OK\n"));
15950Sstevel@tonic-gate 			break;
15960Sstevel@tonic-gate 		case AP_COND_FAILING:
15970Sstevel@tonic-gate 			(*cs)->ap_cond = CFGA_COND_FAILING;
15980Sstevel@tonic-gate 			DBG(2, ("ap_cond = CFGA_COND_FAILING\n"));
15990Sstevel@tonic-gate 			break;
16000Sstevel@tonic-gate 		case AP_COND_FAILED:
16010Sstevel@tonic-gate 			(*cs)->ap_cond = CFGA_COND_FAILED;
16020Sstevel@tonic-gate 			DBG(2, ("ap_cond = CFGA_COND_FAILED\n"));
16030Sstevel@tonic-gate 			break;
16040Sstevel@tonic-gate 		case AP_COND_UNUSABLE:
16050Sstevel@tonic-gate 			(*cs)->ap_cond = CFGA_COND_UNUSABLE;
16060Sstevel@tonic-gate 			DBG(2, ("ap_cond = CFGA_COND_UNUSABLE\n"));
16070Sstevel@tonic-gate 			break;
16080Sstevel@tonic-gate 		case AP_COND_UNKNOWN:
16090Sstevel@tonic-gate 			(*cs)->ap_cond = CFGA_COND_UNKNOWN;
16100Sstevel@tonic-gate 			DBG(2, ("ap_cond = CFGA_COND_UNKNOW\n"));
16110Sstevel@tonic-gate 			break;
16120Sstevel@tonic-gate 	default:
16130Sstevel@tonic-gate 		cfga_err(errstring, CMD_GETSTAT, ap_id, 0);
16140Sstevel@tonic-gate 		rv = CFGA_ERROR;
16150Sstevel@tonic-gate 		devctl_release((devctl_hdl_t)dcp);
16160Sstevel@tonic-gate 		return (rv);
16170Sstevel@tonic-gate 	}
16180Sstevel@tonic-gate 	(*cs)->ap_busy = (int)state.ap_in_transition;
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 	devctl_release((devctl_hdl_t)dcp);
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate 	if ((fd = open(ap_id, O_RDWR)) == -1) {
16230Sstevel@tonic-gate 		cfga_err(errstring, ERR_AP_ERR, ap_id, 0);
16240Sstevel@tonic-gate 		(*cs)->ap_status_time = 0;
16250Sstevel@tonic-gate 		boardtype = HPC_BOARD_UNKNOWN;
16260Sstevel@tonic-gate 		cardinfo.base_class = PCI_CLASS_NONE;
16270Sstevel@tonic-gate 		get_logical_name(ap_id, slot_info.pci_slot_name, 0);
16280Sstevel@tonic-gate 		DBG(2, ("open on %s failed\n", ap_id));
16290Sstevel@tonic-gate 		goto cont;
16300Sstevel@tonic-gate 	}
16310Sstevel@tonic-gate 	DBG(1, ("open = ap_id=%s, fd=%d\n", ap_id, fd));
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 	(*cs)->ap_status_time = state.ap_last_change;
16340Sstevel@tonic-gate 
16350Sstevel@tonic-gate 	/* need board type and a way to get to hpc_slot_info */
16360Sstevel@tonic-gate 	build_control_data(&iocdata, HPC_CTRL_GET_BOARD_TYPE,
16370Sstevel@tonic-gate 	    (void *)&boardtype);
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 	if (ioctl(fd, DEVCTL_AP_CONTROL, &iocdata) == -1) {
16400Sstevel@tonic-gate 		boardtype = HPC_BOARD_UNKNOWN;
16410Sstevel@tonic-gate 	}
16420Sstevel@tonic-gate 	DBG(1, ("ioctl boardtype\n"));
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate 	build_control_data(&iocdata, HPC_CTRL_GET_SLOT_INFO,
16450Sstevel@tonic-gate 	    (void *)&slot_info);
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 	if (ioctl(fd, DEVCTL_AP_CONTROL, &iocdata) == -1) {
16480Sstevel@tonic-gate 		get_logical_name(ap_id, slot_info.pci_slot_name, 0);
16490Sstevel@tonic-gate 		DBG(1, ("ioctl failed slotinfo: %s\n",
16500Sstevel@tonic-gate 		    slot_info.pci_slot_name));
16510Sstevel@tonic-gate 	} else {
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate 		/*
16540Sstevel@tonic-gate 		 * the driver will report back things like hpc0_slot0
16550Sstevel@tonic-gate 		 * this needs to be changed to things like pci1:hpc0_slot0
16560Sstevel@tonic-gate 		 */
16570Sstevel@tonic-gate 		rv = fix_ap_name((*cs)->ap_log_id,
16580Sstevel@tonic-gate 		    ap_id, slot_info.pci_slot_name, errstring);
16590Sstevel@tonic-gate 		DBG(1, ("ioctl slotinfo: %s\n", (*cs)->ap_log_id));
16600Sstevel@tonic-gate 	}
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate 	build_control_data(&iocdata, HPC_CTRL_GET_CARD_INFO,
16630Sstevel@tonic-gate 	    (void *)&cardinfo);
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 	if (ioctl(fd, DEVCTL_AP_CONTROL, &iocdata) == -1) {
16660Sstevel@tonic-gate 		DBG(1, ("ioctl failed\n"));
16670Sstevel@tonic-gate 		cardinfo.base_class = PCI_CLASS_NONE;
16680Sstevel@tonic-gate 	}
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate 	DBG(1, ("ioctl cardinfo: %d\n", cardinfo.base_class));
16710Sstevel@tonic-gate 	DBG(1, ("ioctl subclass: %d\n", cardinfo.sub_class));
16720Sstevel@tonic-gate 	DBG(1, ("ioctl headertype: %d\n", cardinfo.header_type));
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 	(void) close(fd);
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate cont:
16770Sstevel@tonic-gate 	(void) strcpy((*cs)->ap_phys_id, ap_id);    /* physical path of AP */
1678*2587Spjha 
1679*2587Spjha 	dlpath = findlink((*cs)->ap_phys_id);
1680*2587Spjha 	if (dlpath != NULL) {
1681*2587Spjha 		if (get_dli(dlpath, (*cs)->ap_info,
1682*2587Spjha 		    sizeof ((*cs)->ap_info)) != CFGA_OK)
1683*2587Spjha 			(*cs)->ap_info[0] = '\0';
1684*2587Spjha 		free(dlpath);
1685*2587Spjha 	}
1686*2587Spjha 
16870Sstevel@tonic-gate 	if ((*cs)->ap_log_id[0] == '\0')
16880Sstevel@tonic-gate 		(void) strcpy((*cs)->ap_log_id, slot_info.pci_slot_name);
16890Sstevel@tonic-gate 
1690*2587Spjha 	if ((*cs)->ap_info[0] == '\0') {
1691*2587Spjha 		/* slot_names of bus node  */
1692*2587Spjha 		if (find_physical_slot_names(ap_id, &slotname_arg) != -1)
1693*2587Spjha 			(void) strcpy((*cs)->ap_info,
1694*2587Spjha 			    slotname_arg.slotnames[slotname_arg.minor]);
1695*2587Spjha 	}
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 	/* class_code/subclass/boardtype */
16980Sstevel@tonic-gate 	get_type(boardtype, cardinfo, (*cs)->ap_type);
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	DBG(1, ("cfga_list_ext return success\n"));
17010Sstevel@tonic-gate 	rv = CFGA_OK;
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate 	return (rv);
17040Sstevel@tonic-gate }
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate /*
17070Sstevel@tonic-gate  * This routine prints a single line of help message
17080Sstevel@tonic-gate  */
17090Sstevel@tonic-gate static void
cfga_msg(struct cfga_msg * msgp,const char * str)17100Sstevel@tonic-gate cfga_msg(struct cfga_msg *msgp, const char *str)
17110Sstevel@tonic-gate {
17120Sstevel@tonic-gate 	DBG(2, ("<%s>", str));
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 	if (msgp == NULL || msgp->message_routine == NULL)
17150Sstevel@tonic-gate 		return;
17160Sstevel@tonic-gate 
17170Sstevel@tonic-gate 	(*msgp->message_routine)(msgp->appdata_ptr, str);
17180Sstevel@tonic-gate 	(*msgp->message_routine)(msgp->appdata_ptr, "\n");
17190Sstevel@tonic-gate }
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate static cfga_err_t
check_options(const char * options)17220Sstevel@tonic-gate check_options(const char *options)
17230Sstevel@tonic-gate {
17240Sstevel@tonic-gate 	struct cfga_msg *msgp = NULL;
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 	if (options) {
17270Sstevel@tonic-gate 		cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_UNKNOWN]));
17280Sstevel@tonic-gate 		cfga_msg(msgp, options);
17290Sstevel@tonic-gate 		return (CFGA_INVAL);
17300Sstevel@tonic-gate 	}
17310Sstevel@tonic-gate 	return (CFGA_OK);
17320Sstevel@tonic-gate }
17330Sstevel@tonic-gate 
17340Sstevel@tonic-gate /*ARGSUSED*/
17350Sstevel@tonic-gate cfga_err_t
cfga_help(struct cfga_msg * msgp,const char * options,cfga_flags_t flags)17360Sstevel@tonic-gate cfga_help(struct cfga_msg *msgp, const char *options, cfga_flags_t flags)
17370Sstevel@tonic-gate {
17380Sstevel@tonic-gate 	if (options) {
17390Sstevel@tonic-gate 		cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_UNKNOWN]));
17400Sstevel@tonic-gate 		cfga_msg(msgp, options);
17410Sstevel@tonic-gate 	}
17420Sstevel@tonic-gate 	DBG(1, ("cfga_help\n"));
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 	cfga_msg(msgp, dgettext(TEXT_DOMAIN, cfga_strs[HELP_HEADER]));
17450Sstevel@tonic-gate 	cfga_msg(msgp, cfga_strs[HELP_CONFIG]);
17460Sstevel@tonic-gate 	cfga_msg(msgp, cfga_strs[HELP_ENABLE_SLOT]);
17470Sstevel@tonic-gate 	cfga_msg(msgp, cfga_strs[HELP_DISABLE_SLOT]);
17480Sstevel@tonic-gate 	cfga_msg(msgp, cfga_strs[HELP_ENABLE_AUTOCONF]);
17490Sstevel@tonic-gate 	cfga_msg(msgp, cfga_strs[HELP_DISABLE_AUTOCONF]);
17500Sstevel@tonic-gate 	cfga_msg(msgp, cfga_strs[HELP_LED_CNTRL]);
17510Sstevel@tonic-gate 	return (CFGA_OK);
17520Sstevel@tonic-gate }
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate /*
17550Sstevel@tonic-gate  * cfga_err() accepts a variable number of message IDs and constructs
17560Sstevel@tonic-gate  * a corresponding error string which is returned via the errstring argument.
17570Sstevel@tonic-gate  * cfga_err() calls gettext() to internationalize proper messages.
17580Sstevel@tonic-gate  */
17590Sstevel@tonic-gate static void
cfga_err(char ** errstring,...)17600Sstevel@tonic-gate cfga_err(char **errstring, ...)
17610Sstevel@tonic-gate {
17620Sstevel@tonic-gate 	int a;
17630Sstevel@tonic-gate 	int i;
17640Sstevel@tonic-gate 	int n;
17650Sstevel@tonic-gate 	int len;
17660Sstevel@tonic-gate 	int flen;
17670Sstevel@tonic-gate 	char *p;
17680Sstevel@tonic-gate 	char *q;
17690Sstevel@tonic-gate 	char *s[32];
17700Sstevel@tonic-gate 	char *failed;
17710Sstevel@tonic-gate 	va_list ap;
17720Sstevel@tonic-gate 
17730Sstevel@tonic-gate 	/*
17740Sstevel@tonic-gate 	 * If errstring is null it means user in not interested in getting
17750Sstevel@tonic-gate 	 * error status. So we don't do all the work
17760Sstevel@tonic-gate 	 */
17770Sstevel@tonic-gate 	if (errstring == NULL) {
17780Sstevel@tonic-gate 		return;
17790Sstevel@tonic-gate 	}
17800Sstevel@tonic-gate 	va_start(ap, errstring);
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 	failed = dgettext(TEXT_DOMAIN, cfga_strs[FAILED]);
17830Sstevel@tonic-gate 	flen = strlen(failed);
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate 	for (n = len = 0; (a = va_arg(ap, int)) != 0; n++) {
17860Sstevel@tonic-gate 		switch (a) {
17870Sstevel@tonic-gate 		case CMD_GETSTAT:
17880Sstevel@tonic-gate 		case CMD_LIST:
17890Sstevel@tonic-gate 		case CMD_SLOT_CONNECT:
17900Sstevel@tonic-gate 		case CMD_SLOT_DISCONNECT:
17910Sstevel@tonic-gate 		case CMD_SLOT_CONFIGURE:
17920Sstevel@tonic-gate 		case CMD_SLOT_UNCONFIGURE:
17930Sstevel@tonic-gate 			p =  cfga_errstrs(a);
17940Sstevel@tonic-gate 			len += (strlen(p) + flen);
17950Sstevel@tonic-gate 			s[n] = p;
17960Sstevel@tonic-gate 			s[++n] = cfga_strs[FAILED];
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 			DBG(2, ("<%s>", p));
17990Sstevel@tonic-gate 			DBG(2, (cfga_strs[FAILED]));
18000Sstevel@tonic-gate 			break;
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate 		case ERR_CMD_INVAL:
18030Sstevel@tonic-gate 		case ERR_AP_INVAL:
18040Sstevel@tonic-gate 		case ERR_OPT_INVAL:
18050Sstevel@tonic-gate 		case ERR_AP_ERR:
18060Sstevel@tonic-gate 			switch (a) {
18070Sstevel@tonic-gate 			case ERR_CMD_INVAL:
18080Sstevel@tonic-gate 				p = dgettext(TEXT_DOMAIN,
18090Sstevel@tonic-gate 				    cfga_errstrs[ERR_CMD_INVAL]);
18100Sstevel@tonic-gate 				break;
18110Sstevel@tonic-gate 			case ERR_AP_INVAL:
18120Sstevel@tonic-gate 				p = dgettext(TEXT_DOMAIN,
18130Sstevel@tonic-gate 				    cfga_errstrs[ERR_AP_INVAL]);
18140Sstevel@tonic-gate 				break;
18150Sstevel@tonic-gate 			case ERR_OPT_INVAL:
18160Sstevel@tonic-gate 				p = dgettext(TEXT_DOMAIN,
18170Sstevel@tonic-gate 				    cfga_errstrs[ERR_OPT_INVAL]);
18180Sstevel@tonic-gate 				break;
18190Sstevel@tonic-gate 			case ERR_AP_ERR:
18200Sstevel@tonic-gate 				p = dgettext(TEXT_DOMAIN,
18210Sstevel@tonic-gate 				    cfga_errstrs[ERR_AP_ERR]);
18220Sstevel@tonic-gate 				break;
18230Sstevel@tonic-gate 			}
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 			if ((q = va_arg(ap, char *)) != NULL) {
18260Sstevel@tonic-gate 				len += (strlen(p) + strlen(q));
18270Sstevel@tonic-gate 				s[n] = p;
18280Sstevel@tonic-gate 				s[++n] = q;
18290Sstevel@tonic-gate 				DBG(2, ("<%s>", p));
18300Sstevel@tonic-gate 				DBG(2, ("<%s>", q));
18310Sstevel@tonic-gate 				break;
18320Sstevel@tonic-gate 			} else {
18330Sstevel@tonic-gate 				len += strlen(p);
18340Sstevel@tonic-gate 				s[n] = p;
18350Sstevel@tonic-gate 
18360Sstevel@tonic-gate 			}
18370Sstevel@tonic-gate 			DBG(2, ("<%s>", p));
18380Sstevel@tonic-gate 			break;
18390Sstevel@tonic-gate 
18400Sstevel@tonic-gate 		default:
18410Sstevel@tonic-gate 			n--;
18420Sstevel@tonic-gate 			break;
18430Sstevel@tonic-gate 		}
18440Sstevel@tonic-gate 	}
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 	DBG(2, ("\n"));
18470Sstevel@tonic-gate 	va_end(ap);
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate 	if ((p = calloc(len + 1, 1)) == NULL)
18500Sstevel@tonic-gate 		return;
18510Sstevel@tonic-gate 
18520Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
18530Sstevel@tonic-gate 		(void) strlcat(p, s[i], len + 1);
18540Sstevel@tonic-gate 		DBG(2, ("i:%d, %s\n", i, s[i]));
18550Sstevel@tonic-gate 	}
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate 	*errstring = p;
18580Sstevel@tonic-gate #ifdef	DEBUG
18590Sstevel@tonic-gate 	printf("%s\n", *errstring);
18600Sstevel@tonic-gate 	free(*errstring);
18610Sstevel@tonic-gate #endif
18620Sstevel@tonic-gate }
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate /*
18650Sstevel@tonic-gate  * cfga_ap_id_cmp -- use default_ap_id_cmp() in libcfgadm
18660Sstevel@tonic-gate  */
1867