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 #define MAX_FORMAT 80
290Sstevel@tonic-gate
300Sstevel@tonic-gate static scfga_ret_t scsi_rcm_info_table(rcm_info_t *, char **);
310Sstevel@tonic-gate static scfga_ret_t scsi_rcm_init(uint_t, char **, rcm_handle_t **);
320Sstevel@tonic-gate
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate * scsi_rcm_offline()
350Sstevel@tonic-gate *
360Sstevel@tonic-gate * Offline SCSI resource consumers.
370Sstevel@tonic-gate */
380Sstevel@tonic-gate scfga_ret_t
scsi_rcm_offline(char ** rsrclist,char ** errstring,cfga_flags_t flags)390Sstevel@tonic-gate scsi_rcm_offline(char **rsrclist, char **errstring, cfga_flags_t flags)
400Sstevel@tonic-gate {
410Sstevel@tonic-gate int rret;
420Sstevel@tonic-gate uint_t rflags = 0;
430Sstevel@tonic-gate rcm_info_t *rinfo = NULL;
440Sstevel@tonic-gate scfga_ret_t ret = SCFGA_OK;
450Sstevel@tonic-gate rcm_handle_t *rcm_handle;
460Sstevel@tonic-gate
470Sstevel@tonic-gate if (rsrclist == NULL)
480Sstevel@tonic-gate return (ret);
490Sstevel@tonic-gate
500Sstevel@tonic-gate if ((ret = scsi_rcm_init(0, errstring, &rcm_handle))
510Sstevel@tonic-gate != SCFGA_OK)
520Sstevel@tonic-gate return (ret);
530Sstevel@tonic-gate
540Sstevel@tonic-gate if (flags & CFGA_FLAG_FORCE)
550Sstevel@tonic-gate rflags = RCM_FORCE;
560Sstevel@tonic-gate
570Sstevel@tonic-gate if ((rret = rcm_request_offline_list(rcm_handle, rsrclist, rflags,
580Sstevel@tonic-gate &rinfo)) != RCM_SUCCESS) {
59*10696SDavid.Hollister@Sun.COM if ((flags & FLAG_CLIENT_DEV) == FLAG_CLIENT_DEV) {
60*10696SDavid.Hollister@Sun.COM cfga_err(errstring, 0, ERRARG_RCM_CLIENT_OFFLINE, 0);
61*10696SDavid.Hollister@Sun.COM } else {
62*10696SDavid.Hollister@Sun.COM cfga_err(errstring, 0, ERRARG_RCM_OFFLINE, 0);
63*10696SDavid.Hollister@Sun.COM }
640Sstevel@tonic-gate if (rinfo) {
650Sstevel@tonic-gate (void) scsi_rcm_info_table(rinfo, errstring);
660Sstevel@tonic-gate rcm_free_info(rinfo);
670Sstevel@tonic-gate }
680Sstevel@tonic-gate if (rret == RCM_FAILURE)
690Sstevel@tonic-gate (void) rcm_notify_online_list(rcm_handle, rsrclist,
700Sstevel@tonic-gate rflags & ~RCM_FORCE, NULL);
710Sstevel@tonic-gate ret = SCFGA_BUSY;
720Sstevel@tonic-gate }
73*10696SDavid.Hollister@Sun.COM (void) rcm_free_handle(rcm_handle);
740Sstevel@tonic-gate return (ret);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate * scsi_rcm_online()
790Sstevel@tonic-gate *
800Sstevel@tonic-gate * Online SCSI resource consumers that were previously offlined.
810Sstevel@tonic-gate */
820Sstevel@tonic-gate /*ARGSUSED2*/
830Sstevel@tonic-gate scfga_ret_t
scsi_rcm_online(char ** rsrclist,char ** errstring,cfga_flags_t flags)840Sstevel@tonic-gate scsi_rcm_online(char **rsrclist, char **errstring, cfga_flags_t flags)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate rcm_info_t *rinfo = NULL;
870Sstevel@tonic-gate scfga_ret_t ret = SCFGA_OK;
880Sstevel@tonic-gate rcm_handle_t *rcm_handle;
890Sstevel@tonic-gate
900Sstevel@tonic-gate if (rsrclist == NULL)
910Sstevel@tonic-gate return (ret);
920Sstevel@tonic-gate
930Sstevel@tonic-gate if ((ret = scsi_rcm_init(0, errstring, &rcm_handle))
940Sstevel@tonic-gate != SCFGA_OK)
950Sstevel@tonic-gate return (ret);
960Sstevel@tonic-gate
970Sstevel@tonic-gate if (rcm_notify_online_list(rcm_handle, rsrclist, 0, &rinfo)
980Sstevel@tonic-gate != RCM_SUCCESS) {
990Sstevel@tonic-gate cfga_err(errstring, 0, ERRARG_RCM_ONLINE, 0);
1000Sstevel@tonic-gate if (rinfo != NULL) {
1010Sstevel@tonic-gate (void) scsi_rcm_info_table(rinfo, errstring);
1020Sstevel@tonic-gate rcm_free_info(rinfo);
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate ret = SCFGA_BUSY;
1050Sstevel@tonic-gate }
106*10696SDavid.Hollister@Sun.COM (void) rcm_free_handle(rcm_handle);
1070Sstevel@tonic-gate return (ret);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate /*
1110Sstevel@tonic-gate * scsi_rcm_remove()
1120Sstevel@tonic-gate *
1130Sstevel@tonic-gate * Remove SCSI resource consumers after their kernel removal.
1140Sstevel@tonic-gate */
1150Sstevel@tonic-gate /*ARGSUSED2*/
1160Sstevel@tonic-gate scfga_ret_t
scsi_rcm_remove(char ** rsrclist,char ** errstring,cfga_flags_t flags)1170Sstevel@tonic-gate scsi_rcm_remove(char **rsrclist, char **errstring, cfga_flags_t flags)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate rcm_info_t *rinfo = NULL;
1200Sstevel@tonic-gate scfga_ret_t ret = SCFGA_OK;
1210Sstevel@tonic-gate rcm_handle_t *rcm_handle;
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate if (rsrclist == NULL)
1240Sstevel@tonic-gate return (ret);
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate if ((ret = scsi_rcm_init(0, errstring, &rcm_handle))
1270Sstevel@tonic-gate != SCFGA_OK)
1280Sstevel@tonic-gate return (ret);
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate if (rcm_notify_remove_list(rcm_handle, rsrclist, 0, &rinfo)
1310Sstevel@tonic-gate != RCM_SUCCESS) {
1320Sstevel@tonic-gate cfga_err(errstring, 0, ERRARG_RCM_REMOVE, 0);
1330Sstevel@tonic-gate if (rinfo) {
1340Sstevel@tonic-gate (void) scsi_rcm_info_table(rinfo, errstring);
1350Sstevel@tonic-gate rcm_free_info(rinfo);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate ret = SCFGA_BUSY;
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate
140*10696SDavid.Hollister@Sun.COM (void) rcm_free_handle(rcm_handle);
1410Sstevel@tonic-gate return (ret);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate * scsi_rcm_suspend()
1460Sstevel@tonic-gate *
1470Sstevel@tonic-gate * Suspend SCSI resource consumers before a bus quiesce.
1480Sstevel@tonic-gate */
1490Sstevel@tonic-gate scfga_ret_t
scsi_rcm_suspend(char ** rsrclist,char ** errstring,cfga_flags_t flags,int pflag)1500Sstevel@tonic-gate scsi_rcm_suspend(char **rsrclist, char **errstring, cfga_flags_t flags,
1510Sstevel@tonic-gate int pflag)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate int rret;
1540Sstevel@tonic-gate uint_t rflags = 0;
1550Sstevel@tonic-gate rcm_info_t *rinfo = NULL;
1560Sstevel@tonic-gate scfga_ret_t ret = SCFGA_OK;
1570Sstevel@tonic-gate rcm_handle_t *rcm_handle;
1580Sstevel@tonic-gate timespec_t zerotime = { 0, 0 };
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate if (rsrclist == NULL)
1610Sstevel@tonic-gate return (ret);
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate pflag = pflag ? RCM_NOPID : 0;
1640Sstevel@tonic-gate if ((ret = scsi_rcm_init(pflag, errstring, &rcm_handle))
1650Sstevel@tonic-gate != SCFGA_OK)
1660Sstevel@tonic-gate return (ret);
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate if (flags & CFGA_FLAG_FORCE)
1690Sstevel@tonic-gate rflags = RCM_FORCE;
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate * attempt a suspension on a list of resources
1730Sstevel@tonic-gate */
1740Sstevel@tonic-gate if ((rret = rcm_request_suspend_list(rcm_handle, rsrclist, rflags,
1750Sstevel@tonic-gate &zerotime, &rinfo)) != RCM_SUCCESS) {
1760Sstevel@tonic-gate cfga_err(errstring, 0, ERRARG_RCM_SUSPEND, 0);
1770Sstevel@tonic-gate if (rinfo) {
1780Sstevel@tonic-gate (void) scsi_rcm_info_table(rinfo, errstring);
1790Sstevel@tonic-gate rcm_free_info(rinfo);
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate if (rret == RCM_FAILURE)
1820Sstevel@tonic-gate (void) rcm_notify_resume_list(rcm_handle, rsrclist,
1830Sstevel@tonic-gate (rflags & (~RCM_FORCE)), NULL);
1840Sstevel@tonic-gate ret = SCFGA_BUSY;
1850Sstevel@tonic-gate }
186*10696SDavid.Hollister@Sun.COM (void) rcm_free_handle(rcm_handle);
1870Sstevel@tonic-gate return (ret);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate /*
1910Sstevel@tonic-gate * scsi_rcm_resume()
1920Sstevel@tonic-gate *
1930Sstevel@tonic-gate * Resume SCSI resource consumers after a bus has been unquiesced.
1940Sstevel@tonic-gate */
1950Sstevel@tonic-gate /*ARGSUSED2*/
1960Sstevel@tonic-gate scfga_ret_t
scsi_rcm_resume(char ** rsrclist,char ** errstring,cfga_flags_t flags,int pflag)1970Sstevel@tonic-gate scsi_rcm_resume(char **rsrclist, char **errstring, cfga_flags_t flags,
1980Sstevel@tonic-gate int pflag)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate rcm_info_t *rinfo = NULL;
2010Sstevel@tonic-gate scfga_ret_t ret = SCFGA_OK;
2020Sstevel@tonic-gate rcm_handle_t *rcm_handle;
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate if (rsrclist == NULL)
2050Sstevel@tonic-gate return (ret);
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate pflag = pflag ? RCM_NOPID : 0;
2080Sstevel@tonic-gate if ((ret = scsi_rcm_init(pflag, errstring, &rcm_handle))
2090Sstevel@tonic-gate != SCFGA_OK)
2100Sstevel@tonic-gate return (ret);
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate * resume the resource list.
2140Sstevel@tonic-gate */
2150Sstevel@tonic-gate if (rcm_notify_resume_list(rcm_handle, rsrclist, 0, &rinfo)
2160Sstevel@tonic-gate != RCM_SUCCESS) {
2170Sstevel@tonic-gate cfga_err(errstring, 0, ERRARG_RCM_RESUME, 0);
2180Sstevel@tonic-gate if (rinfo != NULL) {
2190Sstevel@tonic-gate (void) scsi_rcm_info_table(rinfo, errstring);
2200Sstevel@tonic-gate rcm_free_info(rinfo);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate ret = SCFGA_BUSY;
2230Sstevel@tonic-gate }
224*10696SDavid.Hollister@Sun.COM (void) rcm_free_handle(rcm_handle);
2250Sstevel@tonic-gate return (ret);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate /*
2290Sstevel@tonic-gate * scsi_rcm_init()
2300Sstevel@tonic-gate *
2310Sstevel@tonic-gate * Contains common initialization code for entering a scsi_rcm_xx()
2320Sstevel@tonic-gate * routine.
2330Sstevel@tonic-gate */
2340Sstevel@tonic-gate static scfga_ret_t
scsi_rcm_init(uint_t rcm_flag,char ** errstring,rcm_handle_t ** hdlp)2350Sstevel@tonic-gate scsi_rcm_init(uint_t rcm_flag, char **errstring, rcm_handle_t **hdlp)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate /* Get a handle for the RCM operations */
2380Sstevel@tonic-gate if (rcm_alloc_handle(NULL, rcm_flag, NULL, hdlp) != RCM_SUCCESS) {
2390Sstevel@tonic-gate cfga_err(errstring, 0, ERR_RCM_HANDLE, 0);
2400Sstevel@tonic-gate return (SCFGA_LIB_ERR);
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate return (SCFGA_OK);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate * scsi_rcm_info_table
2480Sstevel@tonic-gate *
2490Sstevel@tonic-gate * Takes an opaque rcm_info_t pointer and a character pointer, and appends
2500Sstevel@tonic-gate * the rcm_info_t data in the form of a table to the given character pointer.
2510Sstevel@tonic-gate */
2520Sstevel@tonic-gate static scfga_ret_t
scsi_rcm_info_table(rcm_info_t * rinfo,char ** table)2530Sstevel@tonic-gate scsi_rcm_info_table(rcm_info_t *rinfo, char **table)
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate int i;
2560Sstevel@tonic-gate size_t w;
2570Sstevel@tonic-gate size_t width = 0;
2580Sstevel@tonic-gate size_t w_rsrc = 0;
2590Sstevel@tonic-gate size_t w_info = 0;
2600Sstevel@tonic-gate size_t table_size = 0;
2610Sstevel@tonic-gate uint_t tuples = 0;
2620Sstevel@tonic-gate rcm_info_tuple_t *tuple = NULL;
2630Sstevel@tonic-gate char *rsrc;
2640Sstevel@tonic-gate char *info;
2650Sstevel@tonic-gate char *newtable;
2660Sstevel@tonic-gate static char format[MAX_FORMAT];
2670Sstevel@tonic-gate const char *infostr;
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate /* Protect against invalid arguments */
2700Sstevel@tonic-gate if (rinfo == NULL || table == NULL)
2710Sstevel@tonic-gate return (SCFGA_ERR);
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate /* Set localized table header strings */
2740Sstevel@tonic-gate rsrc = dgettext(TEXT_DOMAIN, "Resource");
2750Sstevel@tonic-gate info = dgettext(TEXT_DOMAIN, "Information");
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate /* A first pass, to size up the RCM information */
2780Sstevel@tonic-gate while (tuple = rcm_info_next(rinfo, tuple)) {
2790Sstevel@tonic-gate if ((infostr = rcm_info_info(tuple)) != NULL) {
2800Sstevel@tonic-gate tuples++;
2810Sstevel@tonic-gate if ((w = strlen(rcm_info_rsrc(tuple))) > w_rsrc)
2820Sstevel@tonic-gate w_rsrc = w;
2830Sstevel@tonic-gate if ((w = strlen(infostr)) > w_info)
2840Sstevel@tonic-gate w_info = w;
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate /* If nothing was sized up above, stop early */
2890Sstevel@tonic-gate if (tuples == 0)
2900Sstevel@tonic-gate return (SCFGA_OK);
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate /* Adjust column widths for column headings */
2930Sstevel@tonic-gate if ((w = strlen(rsrc)) > w_rsrc)
2940Sstevel@tonic-gate w_rsrc = w;
2950Sstevel@tonic-gate else if ((w_rsrc - w) % 2)
2960Sstevel@tonic-gate w_rsrc++;
2970Sstevel@tonic-gate if ((w = strlen(info)) > w_info)
2980Sstevel@tonic-gate w_info = w;
2990Sstevel@tonic-gate else if ((w_info - w) % 2)
3000Sstevel@tonic-gate w_info++;
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate /*
3030Sstevel@tonic-gate * Compute the total line width of each line,
3040Sstevel@tonic-gate * accounting for intercolumn spacing.
3050Sstevel@tonic-gate */
3060Sstevel@tonic-gate width = w_info + w_rsrc + 4;
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate /* Allocate space for the table */
3090Sstevel@tonic-gate table_size = (2 + tuples) * (width + 1) + 2;
3100Sstevel@tonic-gate if (*table == NULL) {
3110Sstevel@tonic-gate /* zero fill for the strcat() call below */
3120Sstevel@tonic-gate *table = calloc(table_size, sizeof (char));
3130Sstevel@tonic-gate if (*table == NULL)
3140Sstevel@tonic-gate return (SCFGA_ERR);
3150Sstevel@tonic-gate } else {
3160Sstevel@tonic-gate newtable = realloc(*table, strlen(*table) + table_size);
3170Sstevel@tonic-gate if (newtable == NULL)
3180Sstevel@tonic-gate return (SCFGA_ERR);
3190Sstevel@tonic-gate else
3200Sstevel@tonic-gate *table = newtable;
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate /* Place a table header into the string */
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate /* The resource header */
3260Sstevel@tonic-gate (void) strcat(*table, "\n");
3270Sstevel@tonic-gate w = strlen(rsrc);
3280Sstevel@tonic-gate for (i = 0; i < ((w_rsrc - w) / 2); i++)
3290Sstevel@tonic-gate (void) strcat(*table, " ");
3300Sstevel@tonic-gate (void) strcat(*table, rsrc);
3310Sstevel@tonic-gate for (i = 0; i < ((w_rsrc - w) / 2); i++)
3320Sstevel@tonic-gate (void) strcat(*table, " ");
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate /* The information header */
3350Sstevel@tonic-gate (void) strcat(*table, " ");
3360Sstevel@tonic-gate w = strlen(info);
3370Sstevel@tonic-gate for (i = 0; i < ((w_info - w) / 2); i++)
3380Sstevel@tonic-gate (void) strcat(*table, " ");
3390Sstevel@tonic-gate (void) strcat(*table, info);
3400Sstevel@tonic-gate for (i = 0; i < ((w_info - w) / 2); i++)
3410Sstevel@tonic-gate (void) strcat(*table, " ");
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate /* Underline the headers */
3440Sstevel@tonic-gate (void) strcat(*table, "\n");
3450Sstevel@tonic-gate for (i = 0; i < w_rsrc; i++)
3460Sstevel@tonic-gate (void) strcat(*table, "-");
3470Sstevel@tonic-gate (void) strcat(*table, " ");
3480Sstevel@tonic-gate for (i = 0; i < w_info; i++)
3490Sstevel@tonic-gate (void) strcat(*table, "-");
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate /* Construct the format string */
3520Sstevel@tonic-gate (void) snprintf(format, MAX_FORMAT, "%%-%ds %%-%ds",
3530Sstevel@tonic-gate (int)w_rsrc, (int)w_info);
3540Sstevel@tonic-gate
3550Sstevel@tonic-gate /* Add the tuples to the table string */
3560Sstevel@tonic-gate tuple = NULL;
3570Sstevel@tonic-gate while ((tuple = rcm_info_next(rinfo, tuple)) != NULL) {
3580Sstevel@tonic-gate if ((infostr = rcm_info_info(tuple)) != NULL) {
3590Sstevel@tonic-gate (void) strcat(*table, "\n");
3600Sstevel@tonic-gate (void) sprintf(&((*table)[strlen(*table)]),
3610Sstevel@tonic-gate format, rcm_info_rsrc(tuple),
3620Sstevel@tonic-gate infostr);
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate return (SCFGA_OK);
3670Sstevel@tonic-gate }
368