/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* This file contains functions implementing the scsi menu commands.
*
* These functions are intended for expert use only, and provide
* a raw access to a scsi device's mode pages. The ability to
* issue a raw format command is also provided, should a page be
* changed that requires a format.
*/
#include "global.h"
#include <stdlib.h>
#include <ctype.h>
#include "io.h"
#include "menu.h"
#include "misc.h"
#include "menu_scsi.h"
#include "ctlr_scsi.h"
#include "startup.h"
#include "checkdev.h"
#ifdef __STDC__
/*
* ANSI prototypes for local static functions
*/
static int do_mode_sense(int);
static int do_mode_sense_all(void);
static int do_mode_select(struct chg_list *);
static int do_format(void);
static void do_list(void);
static int do_inquiry(void);
static void do_apply(void);
static void do_cancel(void);
static void do_display(void);
static int parse_change_spec(char *, char *, int, struct chg_list *);
static void add_new_change_list_item(struct chg_list *);
static void free_change_list(void);
static void do_default(char *);
static void default_all_pages(void);
static int default_page(int);
#else
static int do_mode_sense();
static int do_mode_sense_all();
static int do_mode_select();
static int do_format();
static void do_list();
static int do_inquiry();
static void do_apply();
static void do_cancel();
static void do_display();
static int parse_change_spec();
static void add_new_change_list_item();
static void free_change_list();
static void do_default();
static void default_all_pages();
static int default_page();
#endif /* __STDC__ */
/*
* Menu data for the SCSI menu display
*/
static char *scsi_menu_strings[] = {
"p<n> - display a mode sense page",
"p<n> b<n> <op> [~]<n> - change a byte and issue mode select",
"b<n> <op> [~]<n> - add an operation to the mode select list",
" for the current page",
"",
" where: p<n> specifies the page with page code <n>",
" b<n> specifies byte <n> of the page",
" <op> can be one of the following operators:",
" = (set specified value)",
" |= (bitwise OR with current value)",
" &= (bitwise AND with current value)",
" <n> can be a decimal value in the range 0-255,",
" or two hexadecimal digits, in the form 0x<xx>.",
" [~] complements the specified value",
"",
"apply - apply mode select list",
"cancel - cancel mode select list",
"display - display mode select list",
"all - display all supported mode sense pages",
"default p<n> - mode select page <n> to default values",
"default all - mode select all pages to default values",
"format - format without standard mode selects",
"inquiry - display device's inquiry response",
"list - list common SCSI-2 mode pages",
"!<cmd> - execute <cmd> , then return"
};
/*
* Command types
*/
#define CMD_ALL 0
/*
* SCSI menu commands for minimum recognition
*/
{ NULL }
};
/*
* Implied page for mode select change lists
*/
static int current_page;
/*
* Manage the SCSI menu.
* Parse input and dispatch to the appropriate functions.
* The commands we accept are not simple one-word commands,
* so we cannot use the standard format menu-handling functions.
*/
int
c_scsi()
{
int i;
char **menu;
char *p;
char *p2;
int cmd;
int pageno;
/*
* Warn casual users that maybe they should not be
* using this menu.
*/
fmt_print("\n"
"Warning: these functions are intended for expert use only, for\n"
"debugging disk devices and for unusual configuration settings.\n"
"It is recommended that you do not use this menu for normal disk\n"
"configuration and formatting, unless you have explicit instructions,\n"
"or know exactly what you are doing.\n");
/*
* Initialize change list and current page to empty
*/
current_page = -1;
change_list = NULL;
/*
* Build and display the menu.
* we only use this for display purposes.
*/
for (i = 0; i < N_SCSI_STRINGS; i++) {
scsi_menu[i].menu_state = true;
}
/*
* Save the environment so a ctrl-C out of a command lands here.
*/
for (;;) {
if (help) {
help = 0;
fmt_print("\n\nSCSI MENU:\n");
}
/*
* Prompt and get next input line. We don't use the
* standard input routine, since we need a little
* more flexibility in parsing the input.
*/
fmt_print("scsi> ");
clean_token(s, nclean);
/*
* Mark the saved environment active so the user can now
* do a ctrl-C to get out of the command.
*/
useenv();
/*
* Figure out what the user chose
*/
if (i == 1) {
switch (cmd) {
case CMD_ALL:
(void) do_mode_sense_all();
break;
case CMD_FORMAT:
(void) do_format();
break;
case CMD_QUIT:
goto exit;
/*NOTREACHED*/
case CMD_HELP:
fmt_print("\n\nSCSI MENU:\n");
break;
case CMD_LIST:
do_list();
break;
case CMD_INQUIRY:
(void) do_inquiry();
break;
case CMD_APPLY:
do_apply();
break;
case CMD_CANCEL:
do_cancel();
break;
case CMD_DISPLAY:
do_display();
break;
}
} else if (s[0] == 'd') {
do_default(s);
} else if (s[0] == 'p') {
p = s + 1;
if (p2 == p) {
err_print("Syntax error: %s\n", s);
goto error;
}
for (p = p2; *p == ' '; p++)
;
if (*p == 0) {
(void) do_mode_sense(pageno);
} else if (*p == 'b') {
if (parse_change_spec(s, p, pageno,
&change_item)) {
(void) do_mode_select(&change_item);
}
}
} else if (s[0] == 'b') {
if (current_page == -1) {
err_print("\
Please display the page on which you'd like to do a mode select\n");
goto error;
}
if (parse_change_spec(s, s, current_page,
chg_item)) {
} else {
destroy_data((char *)chg_item);
}
} else if (s[0] == '!') {
help = 1;
} else if (s[0] != 0) {
err_print("Syntax error: %s\n", s);
}
/*
* Mark the saved environment inactive so ctrl-C doesn't
* work at the menu itself.
*/
unuseenv();
}
exit:
/*
* Clean up the environment stack and free the menu
*/
clearenv();
destroy_data((char *)menu);
/*
* Clean up the change list, if anything left over
*/
/*
* Make sure user is prompted with previous menu
*/
last_menu++;
return (0);
}
/*
* Do a mode sense on a particular page, and dump the data.
* Get all the various flavors: default, current, saved, changeable.
*/
static int
int pageno;
{
int result = 0;
result = 1;
} else {
HEX_ONLY);
}
result = 1;
} else {
HEX_ONLY);
}
result = 1;
} else {
HEX_ONLY);
}
result = 1;
} else {
HEX_ONLY);
}
fmt_print("\n");
return (result);
}
/*
* Dump all the pages a device supports
*/
static int
{
int result = 0;
result = 1;
}
result = 1;
}
result = 1;
}
result = 1;
}
fmt_print("\n");
return (result);
}
/*
* Get the current mode sense for a particular page, change
* a byte, and issue a mode select. Note that we can only
* change a value if the device indicates that those bits
* are changeable.
*/
static int
struct chg_list *change_item;
{
int length;
int pageno;
int flags;
int result = 0;
/*
* Get changeable mode sense
*/
err_print("Mode sense on page %x (changeable) failed\n",
pageno);
return (1);
}
/*
* Get saved mode sense. If saved fails, use current values.
*/
err_print("Mode sense on page %x (saved) failed\n",
pageno);
err_print("Mode sense on page %x (current) failed\n",
pageno);
return (1);
} else {
err_print("Using current values instead\n");
}
}
/*
* Use the intersection of the saved and changeable
*/
/*
* Try making this change to this page
*/
/*
* A change was made. Do a mode select
* We always want to set the Page Format bit.
* Set the Save Page bit if the drive indicates
* that it can save this page.
*/
flags |= MODE_SELECT_SP;
}
/*
* Failed - try not saving parameters,
* if possible.
*/
if (flags & MODE_SELECT_SP) {
flags &= ~MODE_SELECT_SP;
result = 1;
}
} else {
result = 1;
}
}
if (result) {
fmt_print("\n\
Mode select on page %x failed.\n", pageno);
} else if ((flags & MODE_SELECT_SP) == 0) {
fmt_print("\n\
Mode select on page %x ok, but unable to save change permanently.\n", pageno);
} else {
fmt_print("\n\
Mode select on page %x ok.\n", pageno);
}
} else {
err_print("\nDevice cannot support this change\n");
}
fmt_print("\n");
return (result);
}
/*
* Format a device, without any of the standard mode selects.
* Ask if we should format with the P or the P&G lists.
*/
static int
{
int status;
int deflt;
int grown_list;
fmt_print("\n");
/*
* Are there mounted partitions?
*/
err_print("Cannot format disk with mounted partitions\n\n");
return (-1);
}
/*
* Is any of the partitions being used for swapping.
*/
err_print("Cannot format disk while its partitions are \
currently being used for swapping.\n\n");
return (-1);
}
/*
* Are any being used for SVM, VxVM or live upgrade.
*/
(diskaddr_t)-1, 0, 0)) {
err_print("Cannot format disk while its partitions are "
"currently being used as described.\n");
return (-1);
}
/*
* Let the user choose between formatting with either
* the P, or the P&G lists. Note that yes is 0, no is 1.
*/
deflt = 0;
/*
* Construct the uscsi format ioctl.
* To format with the P and G list, we set the fmtData
* and cmpLst bits to zero. To format with just the
* P list, we set the fmtData bit (meaning that we will
* send down a defect list in the data phase) and the
* cmpLst bit (meaning that the list we send is the
* complete G list), and a defect list header with
* a defect list length of zero.
*/
if (!grown_list) {
/*
* No G list. Send empty defect list to replace it.
*/
}
/*
* Issue the format ioctl
*/
fmt_print("Formatting...\n");
return (status);
}
/*
* List common SCSI-2 mode pages
*/
static void
do_list()
{
fmt_print("\n\
Common SCSI-2 pages applicable to direct-access devices:\n\n");
fmt_print("Page 0x1 - Read-Write Error Recovery Page\n");
fmt_print("Page 0x2 - Disconnect-Reconnect Page\n");
fmt_print("Page 0x3 - Format Device Page\n");
fmt_print("Page 0x4 - Rigid Disk Geometry Page\n");
fmt_print("Page 0x7 - Verify Error Recovery Page\n");
fmt_print("Page 0x8 - Caching Page\n");
fmt_print("Page 0xA - Control Mode Page\n");
fmt_print("\n");
}
/*
* Labels for the various fields of the scsi_inquiry structure
*/
static char *scsi_inquiry_labels[] = {
"Vendor: ",
"Product: ",
"Revision: ",
"Removable media: ",
"Device type: ",
"ISO version: ",
"ECMA version: ",
"ANSI version: ",
"Async event notification: ",
"Terminate i/o process msg: ",
"Response data format: ",
"Additional length: ",
"Relative addressing: ",
"32 bit transfers: ",
"16 bit transfers: ",
"Synchronous transfers: ",
"Linked commands: ",
"Command queueing: ",
"Soft reset option: "
};
/*
* Dump the full inquiry as returned by the device
*/
static int
{
char **p;
err_print("\nInquiry failed\n");
return (1);
}
fmt_print("\nInquiry:\n");
/*
* The SCSI-2 spec defines "Additional length" as (n-4) bytes,
* where n is the last byte of the INQUIRY data. Thus
* there are n+1 bytes of INQUIRY data. We need to add 5 to
* inq_len in order to get all the INQUIRY data.
*/
fmt_print("\n");
p = scsi_inquiry_labels;
fmt_print("%s", *p++);
fmt_print("\n%s", *p++);
fmt_print("\n%s", *p++);
fmt_print("\n");
return (0);
}
static void
do_apply()
{
if (change_list == NULL) {
fmt_print("\nlist empty.\n");
} else {
(void) do_mode_select(change_list);
}
}
static void
{
if (change_list == NULL) {
fmt_print("\nlist empty.\n");
} else {
}
}
static void
{
if (change_list == NULL) {
fmt_print("\nlist empty.\n");
} else {
case CHG_MODE_ABS:
break;
case CHG_MODE_SET:
break;
case CHG_MODE_CLR:
fmt_print("&= ~0x%x\n",
break;
default:
impossible("do_display");
/*NOTREACHED*/
}
}
fmt_print("\n");
}
}
static int
char *full_input;
char *input;
int pageno;
{
char *p;
int tilde;
input++;
if (p == input) {
return (0);
}
err_print(" Unsupported byte offset: %d\n",
return (0);
}
;
switch (*input++) {
case '=':
break;
case '|':
if (*input++ == '=') {
}
break;
case '&':
if (*input++ == '=') {
}
break;
}
return (0);
}
;
if (*input == '~') {
tilde = 1;
;
} else {
tilde = 0;
}
if (p == input || *p != 0) {
return (0);
}
/*
* Apply complement if selected.
* Constrain to a byte value.
*/
if (tilde) {
}
return (1);
}
static void
{
if (change_list == NULL) {
} else {
;
}
}
static void
{
cp = change_list;
destroy_data((char *)cp);
}
change_list = NULL;
}
static void
char *input;
{
char *s = input;
char *p;
int n;
/*
* Reset current page indicator
*/
current_page = -1;
/*
* Skip the leading "default" command, which we
* must have, or we wouldn't have come here,
* and any white space.
*/
while (isspace(*s)) {
s++;
}
s++;
}
while (isspace(*s)) {
s++;
}
/*
* Subsequent modifier must be either "p<n>", or "all".
*/
if (*s == 'p') {
s++;
n = (int)strtol(s, &p, 0);
if (p == s || *p != 0) {
} else {
fmt_print("\n");
(void) default_page(n);
fmt_print("\n");
}
} else if (*s == 'a') {
} else {
}
}
static void
{
char *p;
int n;
int status;
/*
* Build and execute the uscsi ioctl. Note that
* we cannot simply call uscsi_mode_sense() here,
* since that function attempts to valididate the
* returned data, and the page 0x3f has a unique
* format.
*/
if (status) {
if (!option_msg) {
err_print("\nMode sense page 0x3f failed\n");
}
return;
}
fmt_print("\n");
/*
* Now parse the page 0x3f
*/
while (nbytes > 0) {
nbytes -= n;
if (nbytes < 0)
break;
goto error;
}
p += n;
}
if (nbytes < 0) {
err_print("Mode sense page 0x3f formatted incorrectly:\n");
}
fmt_print("\n");
}
static int
int pageno;
{
int length;
int flags;
int i;
int need_mode_select;
/*
* Get default mode sense
*/
err_print("Mode sense on page %x (dfault) failed\n",
pageno);
return (0);
}
/*
* Get the current mode sense.
*/
err_print("Mode sense on page %x (current) failed\n",
pageno);
return (0);
}
/*
* Get saved mode sense. If this fails, assume it is
* the same as the current.
*/
}
/*
* Determine if we need a mode select on this page.
* Just deal with the intersection of the three pages.
*/
need_mode_select = 0;
for (i = 2; i < length; i++) {
need_mode_select = 1;
}
}
if (need_mode_select == 0) {
fmt_print("Defaulting page 0x%x: ok\n",
pageno);
return (1);
}
/*
* A change was made. Do a mode select
* We always want to set the Page Format bit.
* Set the Save Page bit if the drive indicates
* that it can save this page.
*/
flags |= MODE_SELECT_SP;
}
/*
* Failed - try not saving parameters,
* if possible.
*/
if (flags & MODE_SELECT_SP) {
flags &= ~MODE_SELECT_SP;
fmt_print("Defaulting page 0x%x: failed\n",
pageno);
} else {
fmt_print("Defaulting page 0x%x: ",
pageno);
fmt_print("cannot save page permanently\n");
}
} else {
fmt_print("cannot save page permanently\n");
}
} else {
}
return (1);
}