2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#include "cfga_scsi.h"
2N/A
2N/A#define MAX_FORMAT 80
2N/A
2N/Astatic scfga_ret_t scsi_rcm_info_table(rcm_info_t *, char **);
2N/Astatic scfga_ret_t scsi_rcm_init(uint_t, char **, rcm_handle_t **);
2N/A
2N/A/*
2N/A * scsi_rcm_offline()
2N/A *
2N/A * Offline SCSI resource consumers.
2N/A */
2N/Ascfga_ret_t
2N/Ascsi_rcm_offline(char **rsrclist, char **errstring, cfga_flags_t flags)
2N/A{
2N/A int rret;
2N/A uint_t rflags = 0;
2N/A rcm_info_t *rinfo = NULL;
2N/A scfga_ret_t ret = SCFGA_OK;
2N/A rcm_handle_t *rcm_handle;
2N/A
2N/A if (rsrclist == NULL)
2N/A return (ret);
2N/A
2N/A if ((ret = scsi_rcm_init(0, errstring, &rcm_handle))
2N/A != SCFGA_OK)
2N/A return (ret);
2N/A
2N/A if (flags & CFGA_FLAG_FORCE)
2N/A rflags = RCM_FORCE;
2N/A
2N/A if ((rret = rcm_request_offline_list(rcm_handle, rsrclist, rflags,
2N/A &rinfo)) != RCM_SUCCESS) {
2N/A if ((flags & FLAG_CLIENT_DEV) == FLAG_CLIENT_DEV) {
2N/A cfga_err(errstring, 0, ERRARG_RCM_CLIENT_OFFLINE, 0);
2N/A } else {
2N/A cfga_err(errstring, 0, ERRARG_RCM_OFFLINE, 0);
2N/A }
2N/A if (rinfo) {
2N/A (void) scsi_rcm_info_table(rinfo, errstring);
2N/A rcm_free_info(rinfo);
2N/A }
2N/A if (rret == RCM_FAILURE)
2N/A (void) rcm_notify_online_list(rcm_handle, rsrclist,
2N/A rflags & ~RCM_FORCE, NULL);
2N/A ret = SCFGA_BUSY;
2N/A }
2N/A (void) rcm_free_handle(rcm_handle);
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * scsi_rcm_online()
2N/A *
2N/A * Online SCSI resource consumers that were previously offlined.
2N/A */
2N/A/*ARGSUSED2*/
2N/Ascfga_ret_t
2N/Ascsi_rcm_online(char **rsrclist, char **errstring, cfga_flags_t flags)
2N/A{
2N/A rcm_info_t *rinfo = NULL;
2N/A scfga_ret_t ret = SCFGA_OK;
2N/A rcm_handle_t *rcm_handle;
2N/A
2N/A if (rsrclist == NULL)
2N/A return (ret);
2N/A
2N/A if ((ret = scsi_rcm_init(0, errstring, &rcm_handle))
2N/A != SCFGA_OK)
2N/A return (ret);
2N/A
2N/A if (rcm_notify_online_list(rcm_handle, rsrclist, 0, &rinfo)
2N/A != RCM_SUCCESS) {
2N/A cfga_err(errstring, 0, ERRARG_RCM_ONLINE, 0);
2N/A if (rinfo != NULL) {
2N/A (void) scsi_rcm_info_table(rinfo, errstring);
2N/A rcm_free_info(rinfo);
2N/A }
2N/A ret = SCFGA_BUSY;
2N/A }
2N/A (void) rcm_free_handle(rcm_handle);
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * scsi_rcm_remove()
2N/A *
2N/A * Remove SCSI resource consumers after their kernel removal.
2N/A */
2N/A/*ARGSUSED2*/
2N/Ascfga_ret_t
2N/Ascsi_rcm_remove(char **rsrclist, char **errstring, cfga_flags_t flags)
2N/A{
2N/A rcm_info_t *rinfo = NULL;
2N/A scfga_ret_t ret = SCFGA_OK;
2N/A rcm_handle_t *rcm_handle;
2N/A
2N/A if (rsrclist == NULL)
2N/A return (ret);
2N/A
2N/A if ((ret = scsi_rcm_init(0, errstring, &rcm_handle))
2N/A != SCFGA_OK)
2N/A return (ret);
2N/A
2N/A if (rcm_notify_remove_list(rcm_handle, rsrclist, 0, &rinfo)
2N/A != RCM_SUCCESS) {
2N/A cfga_err(errstring, 0, ERRARG_RCM_REMOVE, 0);
2N/A if (rinfo) {
2N/A (void) scsi_rcm_info_table(rinfo, errstring);
2N/A rcm_free_info(rinfo);
2N/A }
2N/A ret = SCFGA_BUSY;
2N/A }
2N/A
2N/A (void) rcm_free_handle(rcm_handle);
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * scsi_rcm_suspend()
2N/A *
2N/A * Suspend SCSI resource consumers before a bus quiesce.
2N/A */
2N/Ascfga_ret_t
2N/Ascsi_rcm_suspend(char **rsrclist, char **errstring, cfga_flags_t flags,
2N/A int pflag)
2N/A{
2N/A int rret;
2N/A uint_t rflags = 0;
2N/A rcm_info_t *rinfo = NULL;
2N/A scfga_ret_t ret = SCFGA_OK;
2N/A rcm_handle_t *rcm_handle;
2N/A timespec_t zerotime = { 0, 0 };
2N/A
2N/A if (rsrclist == NULL)
2N/A return (ret);
2N/A
2N/A pflag = pflag ? RCM_NOPID : 0;
2N/A if ((ret = scsi_rcm_init(pflag, errstring, &rcm_handle))
2N/A != SCFGA_OK)
2N/A return (ret);
2N/A
2N/A if (flags & CFGA_FLAG_FORCE)
2N/A rflags = RCM_FORCE;
2N/A
2N/A /*
2N/A * attempt a suspension on a list of resources
2N/A */
2N/A if ((rret = rcm_request_suspend_list(rcm_handle, rsrclist, rflags,
2N/A &zerotime, &rinfo)) != RCM_SUCCESS) {
2N/A cfga_err(errstring, 0, ERRARG_RCM_SUSPEND, 0);
2N/A if (rinfo) {
2N/A (void) scsi_rcm_info_table(rinfo, errstring);
2N/A rcm_free_info(rinfo);
2N/A }
2N/A if (rret == RCM_FAILURE)
2N/A (void) rcm_notify_resume_list(rcm_handle, rsrclist,
2N/A (rflags & (~RCM_FORCE)), NULL);
2N/A ret = SCFGA_BUSY;
2N/A }
2N/A (void) rcm_free_handle(rcm_handle);
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * scsi_rcm_resume()
2N/A *
2N/A * Resume SCSI resource consumers after a bus has been unquiesced.
2N/A */
2N/A/*ARGSUSED2*/
2N/Ascfga_ret_t
2N/Ascsi_rcm_resume(char **rsrclist, char **errstring, cfga_flags_t flags,
2N/A int pflag)
2N/A{
2N/A rcm_info_t *rinfo = NULL;
2N/A scfga_ret_t ret = SCFGA_OK;
2N/A rcm_handle_t *rcm_handle;
2N/A
2N/A if (rsrclist == NULL)
2N/A return (ret);
2N/A
2N/A pflag = pflag ? RCM_NOPID : 0;
2N/A if ((ret = scsi_rcm_init(pflag, errstring, &rcm_handle))
2N/A != SCFGA_OK)
2N/A return (ret);
2N/A
2N/A /*
2N/A * resume the resource list.
2N/A */
2N/A if (rcm_notify_resume_list(rcm_handle, rsrclist, 0, &rinfo)
2N/A != RCM_SUCCESS) {
2N/A cfga_err(errstring, 0, ERRARG_RCM_RESUME, 0);
2N/A if (rinfo != NULL) {
2N/A (void) scsi_rcm_info_table(rinfo, errstring);
2N/A rcm_free_info(rinfo);
2N/A }
2N/A ret = SCFGA_BUSY;
2N/A }
2N/A (void) rcm_free_handle(rcm_handle);
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * scsi_rcm_init()
2N/A *
2N/A * Contains common initialization code for entering a scsi_rcm_xx()
2N/A * routine.
2N/A */
2N/Astatic scfga_ret_t
2N/Ascsi_rcm_init(uint_t rcm_flag, char **errstring, rcm_handle_t **hdlp)
2N/A{
2N/A /* Get a handle for the RCM operations */
2N/A if (rcm_alloc_handle(NULL, rcm_flag, NULL, hdlp) != RCM_SUCCESS) {
2N/A cfga_err(errstring, 0, ERR_RCM_HANDLE, 0);
2N/A return (SCFGA_LIB_ERR);
2N/A }
2N/A
2N/A return (SCFGA_OK);
2N/A}
2N/A
2N/A/*
2N/A * scsi_rcm_info_table
2N/A *
2N/A * Takes an opaque rcm_info_t pointer and a character pointer, and appends
2N/A * the rcm_info_t data in the form of a table to the given character pointer.
2N/A */
2N/Astatic scfga_ret_t
2N/Ascsi_rcm_info_table(rcm_info_t *rinfo, char **table)
2N/A{
2N/A int i;
2N/A size_t w;
2N/A size_t width = 0;
2N/A size_t w_rsrc = 0;
2N/A size_t w_info = 0;
2N/A size_t table_size = 0;
2N/A uint_t tuples = 0;
2N/A rcm_info_tuple_t *tuple = NULL;
2N/A char *rsrc;
2N/A char *info;
2N/A char *newtable;
2N/A static char format[MAX_FORMAT];
2N/A const char *infostr;
2N/A
2N/A /* Protect against invalid arguments */
2N/A if (rinfo == NULL || table == NULL)
2N/A return (SCFGA_ERR);
2N/A
2N/A /* Set localized table header strings */
2N/A rsrc = dgettext(TEXT_DOMAIN, "Resource");
2N/A info = dgettext(TEXT_DOMAIN, "Information");
2N/A
2N/A /* A first pass, to size up the RCM information */
2N/A while (tuple = rcm_info_next(rinfo, tuple)) {
2N/A if ((infostr = rcm_info_info(tuple)) != NULL) {
2N/A tuples++;
2N/A if ((w = strlen(rcm_info_rsrc(tuple))) > w_rsrc)
2N/A w_rsrc = w;
2N/A if ((w = strlen(infostr)) > w_info)
2N/A w_info = w;
2N/A }
2N/A }
2N/A
2N/A /* If nothing was sized up above, stop early */
2N/A if (tuples == 0)
2N/A return (SCFGA_OK);
2N/A
2N/A /* Adjust column widths for column headings */
2N/A if ((w = strlen(rsrc)) > w_rsrc)
2N/A w_rsrc = w;
2N/A else if ((w_rsrc - w) % 2)
2N/A w_rsrc++;
2N/A if ((w = strlen(info)) > w_info)
2N/A w_info = w;
2N/A else if ((w_info - w) % 2)
2N/A w_info++;
2N/A
2N/A /*
2N/A * Compute the total line width of each line,
2N/A * accounting for intercolumn spacing.
2N/A */
2N/A width = w_info + w_rsrc + 4;
2N/A
2N/A /* Allocate space for the table */
2N/A table_size = (2 + tuples) * (width + 1) + 2;
2N/A if (*table == NULL) {
2N/A /* zero fill for the strcat() call below */
2N/A *table = calloc(table_size, sizeof (char));
2N/A if (*table == NULL)
2N/A return (SCFGA_ERR);
2N/A } else {
2N/A newtable = realloc(*table, strlen(*table) + table_size);
2N/A if (newtable == NULL)
2N/A return (SCFGA_ERR);
2N/A else
2N/A *table = newtable;
2N/A }
2N/A
2N/A /* Place a table header into the string */
2N/A
2N/A /* The resource header */
2N/A (void) strcat(*table, "\n");
2N/A w = strlen(rsrc);
2N/A for (i = 0; i < ((w_rsrc - w) / 2); i++)
2N/A (void) strcat(*table, " ");
2N/A (void) strcat(*table, rsrc);
2N/A for (i = 0; i < ((w_rsrc - w) / 2); i++)
2N/A (void) strcat(*table, " ");
2N/A
2N/A /* The information header */
2N/A (void) strcat(*table, " ");
2N/A w = strlen(info);
2N/A for (i = 0; i < ((w_info - w) / 2); i++)
2N/A (void) strcat(*table, " ");
2N/A (void) strcat(*table, info);
2N/A for (i = 0; i < ((w_info - w) / 2); i++)
2N/A (void) strcat(*table, " ");
2N/A
2N/A /* Underline the headers */
2N/A (void) strcat(*table, "\n");
2N/A for (i = 0; i < w_rsrc; i++)
2N/A (void) strcat(*table, "-");
2N/A (void) strcat(*table, " ");
2N/A for (i = 0; i < w_info; i++)
2N/A (void) strcat(*table, "-");
2N/A
2N/A /* Construct the format string */
2N/A (void) snprintf(format, MAX_FORMAT, "%%-%ds %%-%ds",
2N/A (int)w_rsrc, (int)w_info);
2N/A
2N/A /* Add the tuples to the table string */
2N/A tuple = NULL;
2N/A while ((tuple = rcm_info_next(rinfo, tuple)) != NULL) {
2N/A if ((infostr = rcm_info_info(tuple)) != NULL) {
2N/A (void) strcat(*table, "\n");
2N/A (void) sprintf(&((*table)[strlen(*table)]),
2N/A format, rcm_info_rsrc(tuple),
2N/A infostr);
2N/A }
2N/A }
2N/A
2N/A return (SCFGA_OK);
2N/A}