17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54c06356bSdh142964  * Common Development and Distribution License (the "License").
64c06356bSdh142964  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
224c06356bSdh142964  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include "cfga_scsi.h"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * This file contains the entry points to the plug-in as defined in the
30*bbf21555SRichard Lowe  * config_admin(3CFGADM) man page.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * Set the version number
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate int cfga_version = CFGA_HSL_V2;
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * For debugging - higher values increase verbosity
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate int _scfga_debug = 0;
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #pragma init(_cfgadm_scsi_init)
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static void
_cfgadm_scsi_init()467c478bd9Sstevel@tonic-gate _cfgadm_scsi_init()
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate 	char *tstr;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	if (tstr = getenv("SCFGA_DEBUG")) {
517c478bd9Sstevel@tonic-gate 		_scfga_debug = atoi(tstr);
527c478bd9Sstevel@tonic-gate 	}
537c478bd9Sstevel@tonic-gate }
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*ARGSUSED*/
567c478bd9Sstevel@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)577c478bd9Sstevel@tonic-gate cfga_change_state(
587c478bd9Sstevel@tonic-gate 	cfga_cmd_t state_change_cmd,
597c478bd9Sstevel@tonic-gate 	const char *ap_id,
607c478bd9Sstevel@tonic-gate 	const char *options,
617c478bd9Sstevel@tonic-gate 	struct cfga_confirm *confp,
627c478bd9Sstevel@tonic-gate 	struct cfga_msg *msgp,
637c478bd9Sstevel@tonic-gate 	char **errstring,
647c478bd9Sstevel@tonic-gate 	cfga_flags_t flags)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	apid_t apidt = {NULL};
677c478bd9Sstevel@tonic-gate 	scfga_ret_t ret;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	if (errstring != NULL) {
707c478bd9Sstevel@tonic-gate 		*errstring = NULL;
717c478bd9Sstevel@tonic-gate 	}
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	/*
747c478bd9Sstevel@tonic-gate 	 * All sub-commands which can change state of device require
757c478bd9Sstevel@tonic-gate 	 * root privileges.
767c478bd9Sstevel@tonic-gate 	 */
777c478bd9Sstevel@tonic-gate 	if (geteuid() != 0) {
787c478bd9Sstevel@tonic-gate 		return (CFGA_PRIV);
797c478bd9Sstevel@tonic-gate 	}
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	if (options != NULL && strcmp(options, OPT_DISABLE_RCM) != 0) {
827c478bd9Sstevel@tonic-gate 		cfga_err(errstring, 0, ERRARG_OPT_INVAL, options, 0);
837c478bd9Sstevel@tonic-gate 		return (CFGA_ERROR);
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	if ((ret = apidt_create(ap_id, &apidt, errstring)) != SCFGA_OK) {
877c478bd9Sstevel@tonic-gate 		return (err_cvt(ret));
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	if (options != NULL)
917c478bd9Sstevel@tonic-gate 		apidt.flags |= FLAG_DISABLE_RCM;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	/* A dynamic component indicates a device, else it is the bus */
947c478bd9Sstevel@tonic-gate 	if (apidt.dyncomp != NULL) {
957c478bd9Sstevel@tonic-gate 		ret = dev_change_state(state_change_cmd, &apidt, flags,
967c478bd9Sstevel@tonic-gate 		    errstring);
977c478bd9Sstevel@tonic-gate 	} else {
987c478bd9Sstevel@tonic-gate 		ret = bus_change_state(state_change_cmd, &apidt, confp, flags,
997c478bd9Sstevel@tonic-gate 		    errstring);
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	apidt_free(&apidt);
1037c478bd9Sstevel@tonic-gate 	return (err_cvt(ret));
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1077c478bd9Sstevel@tonic-gate cfga_err_t
cfga_private_func(const char * func,const char * ap_id,const char * options,struct cfga_confirm * confp,struct cfga_msg * msgp,char ** errstring,cfga_flags_t flags)1087c478bd9Sstevel@tonic-gate cfga_private_func(
1097c478bd9Sstevel@tonic-gate 	const char *func,
1107c478bd9Sstevel@tonic-gate 	const char *ap_id,
1117c478bd9Sstevel@tonic-gate 	const char *options,
1127c478bd9Sstevel@tonic-gate 	struct cfga_confirm *confp,
1137c478bd9Sstevel@tonic-gate 	struct cfga_msg *msgp,
1147c478bd9Sstevel@tonic-gate 	char **errstring,
1157c478bd9Sstevel@tonic-gate 	cfga_flags_t flags)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	apid_t apidt = {NULL};
1187c478bd9Sstevel@tonic-gate 	prompt_t args = {NULL};
1197c478bd9Sstevel@tonic-gate 	scfga_ret_t ret;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (errstring != NULL)
1227c478bd9Sstevel@tonic-gate 		*errstring = NULL;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	if (geteuid() != 0) {
1257c478bd9Sstevel@tonic-gate 		return (CFGA_PRIV);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	if (func == NULL) {
1297c478bd9Sstevel@tonic-gate 		return (CFGA_ERROR);
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	if (options != NULL && strcmp(options, OPT_DISABLE_RCM) != 0) {
1337c478bd9Sstevel@tonic-gate 		cfga_err(errstring, 0, ERRARG_OPT_INVAL, options, 0);
1347c478bd9Sstevel@tonic-gate 		return (CFGA_ERROR);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	if ((ret = apidt_create(ap_id, &apidt, errstring)) != SCFGA_OK) {
1387c478bd9Sstevel@tonic-gate 		return (err_cvt(ret));
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 
1414c06356bSdh142964 	if (apidt.dyntype == PATH_APID) {
1424c06356bSdh142964 		return (CFGA_OPNOTSUPP);
1434c06356bSdh142964 	}
1444c06356bSdh142964 
1457c478bd9Sstevel@tonic-gate 	if (options != NULL)
1467c478bd9Sstevel@tonic-gate 		apidt.flags |= FLAG_DISABLE_RCM;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	args.confp = confp;
1497c478bd9Sstevel@tonic-gate 	args.msgp = msgp;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	/*
1527c478bd9Sstevel@tonic-gate 	 * Process command
1537c478bd9Sstevel@tonic-gate 	 */
1547c478bd9Sstevel@tonic-gate 	ret = invoke_cmd(func, &apidt, &args, flags, errstring);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	apidt_free(&apidt);
1577c478bd9Sstevel@tonic-gate 	return (err_cvt(ret));
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1617c478bd9Sstevel@tonic-gate cfga_err_t
cfga_test(const char * ap_id,const char * options,struct cfga_msg * msgp,char ** errstring,cfga_flags_t flags)1627c478bd9Sstevel@tonic-gate cfga_test(
1637c478bd9Sstevel@tonic-gate 	const char *ap_id,
1647c478bd9Sstevel@tonic-gate 	const char *options,
1657c478bd9Sstevel@tonic-gate 	struct cfga_msg *msgp,
1667c478bd9Sstevel@tonic-gate 	char **errstring,
1677c478bd9Sstevel@tonic-gate 	cfga_flags_t flags)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	if (errstring != NULL) {
1707c478bd9Sstevel@tonic-gate 		*errstring = NULL;
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (geteuid() != 0) {
1747c478bd9Sstevel@tonic-gate 		return (CFGA_PRIV);
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	return (CFGA_OPNOTSUPP);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1817c478bd9Sstevel@tonic-gate cfga_err_t
cfga_list_ext(const char * ap_id,cfga_list_data_t ** ap_id_list,int * nlistp,const char * options,const char * listopts,char ** errstring,cfga_flags_t flags)1827c478bd9Sstevel@tonic-gate cfga_list_ext(
1837c478bd9Sstevel@tonic-gate 	const char *ap_id,
1847c478bd9Sstevel@tonic-gate 	cfga_list_data_t **ap_id_list,
1857c478bd9Sstevel@tonic-gate 	int *nlistp,
1867c478bd9Sstevel@tonic-gate 	const char *options,
1877c478bd9Sstevel@tonic-gate 	const char *listopts,
1887c478bd9Sstevel@tonic-gate 	char **errstring,
1897c478bd9Sstevel@tonic-gate 	cfga_flags_t flags)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	int hba, expand, nelem;
1927c478bd9Sstevel@tonic-gate 	ldata_list_t *llp = NULL;
1937c478bd9Sstevel@tonic-gate 	apid_t apidt = {NULL};
1947c478bd9Sstevel@tonic-gate 	scfga_cmd_t cmd;
1957c478bd9Sstevel@tonic-gate 	scfga_ret_t ret;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (errstring != NULL) {
1987c478bd9Sstevel@tonic-gate 		*errstring = NULL;
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (ap_id_list == NULL || nlistp == NULL) {
2027c478bd9Sstevel@tonic-gate 		return (CFGA_ERROR);
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	*ap_id_list = NULL;
2067c478bd9Sstevel@tonic-gate 	*nlistp = 0;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * There is no RCM involvement in "list" operations.
2107c478bd9Sstevel@tonic-gate 	 * The only supported option is OPT_USE_DIFORCE.
2117c478bd9Sstevel@tonic-gate 	 */
2127c478bd9Sstevel@tonic-gate 	if (options != NULL && strcmp(options, OPT_USE_DIFORCE) != 0) {
2137c478bd9Sstevel@tonic-gate 		cfga_err(errstring, 0, ERRARG_OPT_INVAL, options, 0);
2147c478bd9Sstevel@tonic-gate 		return (CFGA_ERROR);
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	hba = 0;
2187c478bd9Sstevel@tonic-gate 	if (GET_DYN(ap_id) == NULL) {
2197c478bd9Sstevel@tonic-gate 		hba = 1;
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	expand = 0;
2237c478bd9Sstevel@tonic-gate 	if ((flags & CFGA_FLAG_LIST_ALL) == CFGA_FLAG_LIST_ALL) {
2247c478bd9Sstevel@tonic-gate 		expand = 1;
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	/*
2287c478bd9Sstevel@tonic-gate 	 * We expand published attachment points but not
2297c478bd9Sstevel@tonic-gate 	 * dynamic attachment points
2307c478bd9Sstevel@tonic-gate 	 */
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	if (!hba) { /* Stat a single device - no expansion for devices */
2337c478bd9Sstevel@tonic-gate 		cmd = SCFGA_STAT_DEV;
2347c478bd9Sstevel@tonic-gate 	} else if (!expand) { /* Stat only the HBA */
2357c478bd9Sstevel@tonic-gate 		cmd = SCFGA_STAT_BUS;
2367c478bd9Sstevel@tonic-gate 	} else { /* Expand HBA attachment point */
2377c478bd9Sstevel@tonic-gate 		cmd = SCFGA_STAT_ALL;
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	if ((ret = apidt_create(ap_id, &apidt, errstring)) != SCFGA_OK) {
2417c478bd9Sstevel@tonic-gate 		return (err_cvt(ret));
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	/*
2457c478bd9Sstevel@tonic-gate 	 * Currently only 1 option supported
2467c478bd9Sstevel@tonic-gate 	 */
2477c478bd9Sstevel@tonic-gate 	if (options)
2487c478bd9Sstevel@tonic-gate 		apidt.flags |= FLAG_USE_DIFORCE;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	llp = NULL;
2517c478bd9Sstevel@tonic-gate 	nelem = 0;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	ret = do_list(&apidt, cmd, &llp, &nelem, errstring);
2547c478bd9Sstevel@tonic-gate 	if (ret != SCFGA_OK) {
2557c478bd9Sstevel@tonic-gate 		goto out;
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	assert(llp != NULL);
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	if (list_ext_postprocess(&llp, nelem, ap_id_list, nlistp,
2617c478bd9Sstevel@tonic-gate 	    errstring) != SCFGA_OK) {
2627c478bd9Sstevel@tonic-gate 		assert(*ap_id_list == NULL && *nlistp == 0);
2637c478bd9Sstevel@tonic-gate 		ret = SCFGA_LIB_ERR;
2647c478bd9Sstevel@tonic-gate 	} else {
2657c478bd9Sstevel@tonic-gate 		assert(*ap_id_list != NULL && *nlistp == nelem);
2667c478bd9Sstevel@tonic-gate 		ret = SCFGA_OK;
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	/* FALLTHROUGH */
2707c478bd9Sstevel@tonic-gate out:
2717c478bd9Sstevel@tonic-gate 	list_free(&llp);
2727c478bd9Sstevel@tonic-gate 	apidt_free(&apidt);
2737c478bd9Sstevel@tonic-gate 	return (err_cvt(ret));
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2787c478bd9Sstevel@tonic-gate cfga_err_t
cfga_help(struct cfga_msg * msgp,const char * options,cfga_flags_t flags)2797c478bd9Sstevel@tonic-gate cfga_help(struct cfga_msg *msgp, const char *options, cfga_flags_t flags)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate 	cfga_msg(msgp, MSG_HELP_HDR, MSG_HELP_USAGE, 0);
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	return (CFGA_OK);
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate /*
2877c478bd9Sstevel@tonic-gate  * cfga_ap_id_cmp -- use default_ap_id_cmp() in libcfgadm
2887c478bd9Sstevel@tonic-gate  */
289