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
5*10696SDavid.Hollister@Sun.COM * Common Development and Distribution License (the "License").
6*10696SDavid.Hollister@Sun.COM * 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 */
210Sstevel@tonic-gate /*
22*10696SDavid.Hollister@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include "cfga_scsi.h"
270Sstevel@tonic-gate
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * This file contains the entry points to the plug-in as defined in the
300Sstevel@tonic-gate * config_admin(3X) man page.
310Sstevel@tonic-gate */
320Sstevel@tonic-gate
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate * Set the version number
350Sstevel@tonic-gate */
360Sstevel@tonic-gate int cfga_version = CFGA_HSL_V2;
370Sstevel@tonic-gate
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate * For debugging - higher values increase verbosity
400Sstevel@tonic-gate */
410Sstevel@tonic-gate int _scfga_debug = 0;
420Sstevel@tonic-gate
430Sstevel@tonic-gate #pragma init(_cfgadm_scsi_init)
440Sstevel@tonic-gate
450Sstevel@tonic-gate static void
_cfgadm_scsi_init()460Sstevel@tonic-gate _cfgadm_scsi_init()
470Sstevel@tonic-gate {
480Sstevel@tonic-gate char *tstr;
490Sstevel@tonic-gate
500Sstevel@tonic-gate if (tstr = getenv("SCFGA_DEBUG")) {
510Sstevel@tonic-gate _scfga_debug = atoi(tstr);
520Sstevel@tonic-gate }
530Sstevel@tonic-gate }
540Sstevel@tonic-gate
550Sstevel@tonic-gate /*ARGSUSED*/
560Sstevel@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)570Sstevel@tonic-gate cfga_change_state(
580Sstevel@tonic-gate cfga_cmd_t state_change_cmd,
590Sstevel@tonic-gate const char *ap_id,
600Sstevel@tonic-gate const char *options,
610Sstevel@tonic-gate struct cfga_confirm *confp,
620Sstevel@tonic-gate struct cfga_msg *msgp,
630Sstevel@tonic-gate char **errstring,
640Sstevel@tonic-gate cfga_flags_t flags)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate apid_t apidt = {NULL};
670Sstevel@tonic-gate scfga_ret_t ret;
680Sstevel@tonic-gate
690Sstevel@tonic-gate if (errstring != NULL) {
700Sstevel@tonic-gate *errstring = NULL;
710Sstevel@tonic-gate }
720Sstevel@tonic-gate
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate * All sub-commands which can change state of device require
750Sstevel@tonic-gate * root privileges.
760Sstevel@tonic-gate */
770Sstevel@tonic-gate if (geteuid() != 0) {
780Sstevel@tonic-gate return (CFGA_PRIV);
790Sstevel@tonic-gate }
800Sstevel@tonic-gate
810Sstevel@tonic-gate if (options != NULL && strcmp(options, OPT_DISABLE_RCM) != 0) {
820Sstevel@tonic-gate cfga_err(errstring, 0, ERRARG_OPT_INVAL, options, 0);
830Sstevel@tonic-gate return (CFGA_ERROR);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
860Sstevel@tonic-gate if ((ret = apidt_create(ap_id, &apidt, errstring)) != SCFGA_OK) {
870Sstevel@tonic-gate return (err_cvt(ret));
880Sstevel@tonic-gate }
890Sstevel@tonic-gate
900Sstevel@tonic-gate if (options != NULL)
910Sstevel@tonic-gate apidt.flags |= FLAG_DISABLE_RCM;
920Sstevel@tonic-gate
930Sstevel@tonic-gate /* A dynamic component indicates a device, else it is the bus */
940Sstevel@tonic-gate if (apidt.dyncomp != NULL) {
950Sstevel@tonic-gate ret = dev_change_state(state_change_cmd, &apidt, flags,
960Sstevel@tonic-gate errstring);
970Sstevel@tonic-gate } else {
980Sstevel@tonic-gate ret = bus_change_state(state_change_cmd, &apidt, confp, flags,
990Sstevel@tonic-gate errstring);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate apidt_free(&apidt);
1030Sstevel@tonic-gate return (err_cvt(ret));
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate /*ARGSUSED*/
1070Sstevel@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)1080Sstevel@tonic-gate cfga_private_func(
1090Sstevel@tonic-gate const char *func,
1100Sstevel@tonic-gate const char *ap_id,
1110Sstevel@tonic-gate const char *options,
1120Sstevel@tonic-gate struct cfga_confirm *confp,
1130Sstevel@tonic-gate struct cfga_msg *msgp,
1140Sstevel@tonic-gate char **errstring,
1150Sstevel@tonic-gate cfga_flags_t flags)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate apid_t apidt = {NULL};
1180Sstevel@tonic-gate prompt_t args = {NULL};
1190Sstevel@tonic-gate scfga_ret_t ret;
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate if (errstring != NULL)
1220Sstevel@tonic-gate *errstring = NULL;
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate if (geteuid() != 0) {
1250Sstevel@tonic-gate return (CFGA_PRIV);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate if (func == NULL) {
1290Sstevel@tonic-gate return (CFGA_ERROR);
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate if (options != NULL && strcmp(options, OPT_DISABLE_RCM) != 0) {
1330Sstevel@tonic-gate cfga_err(errstring, 0, ERRARG_OPT_INVAL, options, 0);
1340Sstevel@tonic-gate return (CFGA_ERROR);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate if ((ret = apidt_create(ap_id, &apidt, errstring)) != SCFGA_OK) {
1380Sstevel@tonic-gate return (err_cvt(ret));
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate
141*10696SDavid.Hollister@Sun.COM if (apidt.dyntype == PATH_APID) {
142*10696SDavid.Hollister@Sun.COM return (CFGA_OPNOTSUPP);
143*10696SDavid.Hollister@Sun.COM }
144*10696SDavid.Hollister@Sun.COM
1450Sstevel@tonic-gate if (options != NULL)
1460Sstevel@tonic-gate apidt.flags |= FLAG_DISABLE_RCM;
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate args.confp = confp;
1490Sstevel@tonic-gate args.msgp = msgp;
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate /*
1520Sstevel@tonic-gate * Process command
1530Sstevel@tonic-gate */
1540Sstevel@tonic-gate ret = invoke_cmd(func, &apidt, &args, flags, errstring);
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate apidt_free(&apidt);
1570Sstevel@tonic-gate return (err_cvt(ret));
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate /*ARGSUSED*/
1610Sstevel@tonic-gate cfga_err_t
cfga_test(const char * ap_id,const char * options,struct cfga_msg * msgp,char ** errstring,cfga_flags_t flags)1620Sstevel@tonic-gate cfga_test(
1630Sstevel@tonic-gate const char *ap_id,
1640Sstevel@tonic-gate const char *options,
1650Sstevel@tonic-gate struct cfga_msg *msgp,
1660Sstevel@tonic-gate char **errstring,
1670Sstevel@tonic-gate cfga_flags_t flags)
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate if (errstring != NULL) {
1700Sstevel@tonic-gate *errstring = NULL;
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate if (geteuid() != 0) {
1740Sstevel@tonic-gate return (CFGA_PRIV);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate return (CFGA_OPNOTSUPP);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate /*ARGSUSED*/
1810Sstevel@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)1820Sstevel@tonic-gate cfga_list_ext(
1830Sstevel@tonic-gate const char *ap_id,
1840Sstevel@tonic-gate cfga_list_data_t **ap_id_list,
1850Sstevel@tonic-gate int *nlistp,
1860Sstevel@tonic-gate const char *options,
1870Sstevel@tonic-gate const char *listopts,
1880Sstevel@tonic-gate char **errstring,
1890Sstevel@tonic-gate cfga_flags_t flags)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate int hba, expand, nelem;
1920Sstevel@tonic-gate ldata_list_t *llp = NULL;
1930Sstevel@tonic-gate apid_t apidt = {NULL};
1940Sstevel@tonic-gate scfga_cmd_t cmd;
1950Sstevel@tonic-gate scfga_ret_t ret;
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate if (errstring != NULL) {
1980Sstevel@tonic-gate *errstring = NULL;
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate if (ap_id_list == NULL || nlistp == NULL) {
2020Sstevel@tonic-gate return (CFGA_ERROR);
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate *ap_id_list = NULL;
2060Sstevel@tonic-gate *nlistp = 0;
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate /*
2090Sstevel@tonic-gate * There is no RCM involvement in "list" operations.
2100Sstevel@tonic-gate * The only supported option is OPT_USE_DIFORCE.
2110Sstevel@tonic-gate */
2120Sstevel@tonic-gate if (options != NULL && strcmp(options, OPT_USE_DIFORCE) != 0) {
2130Sstevel@tonic-gate cfga_err(errstring, 0, ERRARG_OPT_INVAL, options, 0);
2140Sstevel@tonic-gate return (CFGA_ERROR);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate hba = 0;
2180Sstevel@tonic-gate if (GET_DYN(ap_id) == NULL) {
2190Sstevel@tonic-gate hba = 1;
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate expand = 0;
2230Sstevel@tonic-gate if ((flags & CFGA_FLAG_LIST_ALL) == CFGA_FLAG_LIST_ALL) {
2240Sstevel@tonic-gate expand = 1;
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate /*
2280Sstevel@tonic-gate * We expand published attachment points but not
2290Sstevel@tonic-gate * dynamic attachment points
2300Sstevel@tonic-gate */
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate if (!hba) { /* Stat a single device - no expansion for devices */
2330Sstevel@tonic-gate cmd = SCFGA_STAT_DEV;
2340Sstevel@tonic-gate } else if (!expand) { /* Stat only the HBA */
2350Sstevel@tonic-gate cmd = SCFGA_STAT_BUS;
2360Sstevel@tonic-gate } else { /* Expand HBA attachment point */
2370Sstevel@tonic-gate cmd = SCFGA_STAT_ALL;
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate if ((ret = apidt_create(ap_id, &apidt, errstring)) != SCFGA_OK) {
2410Sstevel@tonic-gate return (err_cvt(ret));
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate /*
2450Sstevel@tonic-gate * Currently only 1 option supported
2460Sstevel@tonic-gate */
2470Sstevel@tonic-gate if (options)
2480Sstevel@tonic-gate apidt.flags |= FLAG_USE_DIFORCE;
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate llp = NULL;
2510Sstevel@tonic-gate nelem = 0;
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate ret = do_list(&apidt, cmd, &llp, &nelem, errstring);
2540Sstevel@tonic-gate if (ret != SCFGA_OK) {
2550Sstevel@tonic-gate goto out;
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate assert(llp != NULL);
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate if (list_ext_postprocess(&llp, nelem, ap_id_list, nlistp,
2610Sstevel@tonic-gate errstring) != SCFGA_OK) {
2620Sstevel@tonic-gate assert(*ap_id_list == NULL && *nlistp == 0);
2630Sstevel@tonic-gate ret = SCFGA_LIB_ERR;
2640Sstevel@tonic-gate } else {
2650Sstevel@tonic-gate assert(*ap_id_list != NULL && *nlistp == nelem);
2660Sstevel@tonic-gate ret = SCFGA_OK;
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate /* FALLTHROUGH */
2700Sstevel@tonic-gate out:
2710Sstevel@tonic-gate list_free(&llp);
2720Sstevel@tonic-gate apidt_free(&apidt);
2730Sstevel@tonic-gate return (err_cvt(ret));
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate /*ARGSUSED*/
2780Sstevel@tonic-gate cfga_err_t
cfga_help(struct cfga_msg * msgp,const char * options,cfga_flags_t flags)2790Sstevel@tonic-gate cfga_help(struct cfga_msg *msgp, const char *options, cfga_flags_t flags)
2800Sstevel@tonic-gate {
2810Sstevel@tonic-gate cfga_msg(msgp, MSG_HELP_HDR, MSG_HELP_USAGE, 0);
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate return (CFGA_OK);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate /*
2870Sstevel@tonic-gate * cfga_ap_id_cmp -- use default_ap_id_cmp() in libcfgadm
2880Sstevel@tonic-gate */
289