2009-08-13 23:18:45 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
2009-08-13 23:18:45 +00:00
|
|
|
* Copyright (c) 2008, 2009 Yahoo!, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The names of the authors may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior written
|
|
|
|
* permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <err.h>
|
2012-06-19 06:18:37 +00:00
|
|
|
#include <fcntl.h>
|
2009-08-13 23:18:45 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "mfiutil.h"
|
|
|
|
|
|
|
|
static char *
|
|
|
|
adapter_time(time_t now, uint32_t at_now, uint32_t at)
|
|
|
|
{
|
|
|
|
time_t t;
|
|
|
|
|
|
|
|
t = (now - at_now) + at;
|
|
|
|
return (ctime(&t));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mfi_get_time(int fd, uint32_t *at)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (mfi_dcmd_command(fd, MFI_DCMD_TIME_SECS_GET, at, sizeof(*at), NULL,
|
|
|
|
0, NULL) < 0) {
|
|
|
|
warn("Couldn't fetch adapter time");
|
|
|
|
at = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
patrol_get_props(int fd, struct mfi_pr_properties *prop)
|
|
|
|
{
|
2010-10-26 19:11:09 +00:00
|
|
|
int error;
|
2009-08-13 23:18:45 +00:00
|
|
|
|
|
|
|
if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_PROPERTIES, prop,
|
|
|
|
sizeof(*prop), NULL, 0, NULL) < 0) {
|
2010-10-26 19:11:09 +00:00
|
|
|
error = errno;
|
2009-08-13 23:18:45 +00:00
|
|
|
warn("Failed to get patrol read properties");
|
2010-10-26 19:11:09 +00:00
|
|
|
return (error);
|
2009-08-13 23:18:45 +00:00
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-06-19 06:18:42 +00:00
|
|
|
show_patrol(int ac __unused, char **av __unused)
|
2009-08-13 23:18:45 +00:00
|
|
|
{
|
|
|
|
struct mfi_pr_properties prop;
|
|
|
|
struct mfi_pr_status status;
|
|
|
|
struct mfi_pd_list *list;
|
|
|
|
struct mfi_pd_info info;
|
2011-06-20 21:28:50 +00:00
|
|
|
char label[24];
|
2009-08-13 23:18:45 +00:00
|
|
|
time_t now;
|
|
|
|
uint32_t at;
|
2010-10-26 19:11:09 +00:00
|
|
|
int error, fd;
|
2009-08-13 23:18:45 +00:00
|
|
|
u_int i;
|
|
|
|
|
2012-06-19 06:18:37 +00:00
|
|
|
fd = mfi_open(mfi_unit, O_RDWR);
|
2009-08-13 23:18:45 +00:00
|
|
|
if (fd < 0) {
|
2010-10-26 19:11:09 +00:00
|
|
|
error = errno;
|
2009-08-13 23:18:45 +00:00
|
|
|
warn("mfi_open");
|
2010-10-26 19:11:09 +00:00
|
|
|
return (error);
|
2009-08-13 23:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
time(&now);
|
|
|
|
mfi_get_time(fd, &at);
|
2010-10-26 19:11:09 +00:00
|
|
|
error = patrol_get_props(fd, &prop);
|
2011-06-09 19:52:28 +00:00
|
|
|
if (error) {
|
|
|
|
close(fd);
|
2010-10-26 19:11:09 +00:00
|
|
|
return (error);
|
2011-06-09 19:52:28 +00:00
|
|
|
}
|
2009-08-13 23:18:45 +00:00
|
|
|
printf("Operation Mode: ");
|
|
|
|
switch (prop.op_mode) {
|
|
|
|
case MFI_PR_OPMODE_AUTO:
|
|
|
|
printf("auto\n");
|
|
|
|
break;
|
|
|
|
case MFI_PR_OPMODE_MANUAL:
|
|
|
|
printf("manual\n");
|
|
|
|
break;
|
|
|
|
case MFI_PR_OPMODE_DISABLED:
|
|
|
|
printf("disabled\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("??? (%02x)\n", prop.op_mode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (prop.op_mode == MFI_PR_OPMODE_AUTO) {
|
|
|
|
if (at != 0 && prop.next_exec)
|
|
|
|
printf(" Next Run Starts: %s", adapter_time(now, at,
|
|
|
|
prop.next_exec));
|
|
|
|
if (prop.exec_freq == 0xffffffff)
|
|
|
|
printf(" Runs Execute Continuously\n");
|
|
|
|
else if (prop.exec_freq != 0)
|
|
|
|
printf(" Runs Start Every %u seconds\n",
|
|
|
|
prop.exec_freq);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_STATUS, &status,
|
|
|
|
sizeof(status), NULL, 0, NULL) < 0) {
|
2010-10-26 19:11:09 +00:00
|
|
|
error = errno;
|
2009-08-13 23:18:45 +00:00
|
|
|
warn("Failed to get patrol read properties");
|
2011-06-09 19:52:28 +00:00
|
|
|
close(fd);
|
2010-10-26 19:11:09 +00:00
|
|
|
return (error);
|
2009-08-13 23:18:45 +00:00
|
|
|
}
|
|
|
|
printf("Runs Completed: %u\n", status.num_iteration);
|
|
|
|
printf("Current State: ");
|
|
|
|
switch (status.state) {
|
|
|
|
case MFI_PR_STATE_STOPPED:
|
|
|
|
printf("stopped\n");
|
|
|
|
break;
|
|
|
|
case MFI_PR_STATE_READY:
|
|
|
|
printf("ready\n");
|
|
|
|
break;
|
|
|
|
case MFI_PR_STATE_ACTIVE:
|
|
|
|
printf("active\n");
|
|
|
|
break;
|
|
|
|
case MFI_PR_STATE_ABORTED:
|
|
|
|
printf("aborted\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("??? (%02x)\n", status.state);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (status.state == MFI_PR_STATE_ACTIVE) {
|
|
|
|
if (mfi_pd_get_list(fd, &list, NULL) < 0) {
|
2010-10-26 19:11:09 +00:00
|
|
|
error = errno;
|
2009-08-13 23:18:45 +00:00
|
|
|
warn("Failed to get drive list");
|
2011-06-09 19:52:28 +00:00
|
|
|
close(fd);
|
2010-10-26 19:11:09 +00:00
|
|
|
return (error);
|
2009-08-13 23:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < list->count; i++) {
|
|
|
|
if (list->addr[i].scsi_dev_type != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (mfi_pd_get_info(fd, list->addr[i].device_id, &info,
|
|
|
|
NULL) < 0) {
|
2010-10-26 19:11:09 +00:00
|
|
|
error = errno;
|
2009-08-13 23:18:45 +00:00
|
|
|
warn("Failed to fetch info for drive %u",
|
|
|
|
list->addr[i].device_id);
|
2011-06-09 19:52:28 +00:00
|
|
|
free(list);
|
|
|
|
close(fd);
|
2010-10-26 19:11:09 +00:00
|
|
|
return (error);
|
2009-08-13 23:18:45 +00:00
|
|
|
}
|
|
|
|
if (info.prog_info.active & MFI_PD_PROGRESS_PATROL) {
|
2011-06-20 21:28:50 +00:00
|
|
|
snprintf(label, sizeof(label), " Drive %s",
|
|
|
|
mfi_drive_name(NULL,
|
|
|
|
list->addr[i].device_id,
|
|
|
|
MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
|
2009-08-13 23:18:45 +00:00
|
|
|
mfi_display_progress(label,
|
|
|
|
&info.prog_info.patrol);
|
|
|
|
}
|
|
|
|
}
|
2011-06-09 19:52:28 +00:00
|
|
|
free(list);
|
2009-08-13 23:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
MFI_COMMAND(show, patrol, show_patrol);
|
|
|
|
|
|
|
|
static int
|
2012-06-19 06:18:42 +00:00
|
|
|
start_patrol(int ac __unused, char **av __unused)
|
2009-08-13 23:18:45 +00:00
|
|
|
{
|
2010-10-26 19:11:09 +00:00
|
|
|
int error, fd;
|
2009-08-13 23:18:45 +00:00
|
|
|
|
2012-06-19 06:18:37 +00:00
|
|
|
fd = mfi_open(mfi_unit, O_RDWR);
|
2009-08-13 23:18:45 +00:00
|
|
|
if (fd < 0) {
|
2010-10-26 19:11:09 +00:00
|
|
|
error = errno;
|
2009-08-13 23:18:45 +00:00
|
|
|
warn("mfi_open");
|
2010-10-26 19:11:09 +00:00
|
|
|
return (error);
|
2009-08-13 23:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mfi_dcmd_command(fd, MFI_DCMD_PR_START, NULL, 0, NULL, 0, NULL) <
|
|
|
|
0) {
|
2010-10-26 19:11:09 +00:00
|
|
|
error = errno;
|
2009-08-13 23:18:45 +00:00
|
|
|
warn("Failed to start patrol read");
|
2011-06-09 19:52:28 +00:00
|
|
|
close(fd);
|
2010-10-26 19:11:09 +00:00
|
|
|
return (error);
|
2009-08-13 23:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
MFI_COMMAND(start, patrol, start_patrol);
|
|
|
|
|
|
|
|
static int
|
2012-06-19 06:18:42 +00:00
|
|
|
stop_patrol(int ac __unused, char **av __unused)
|
2009-08-13 23:18:45 +00:00
|
|
|
{
|
2010-10-26 19:11:09 +00:00
|
|
|
int error, fd;
|
2009-08-13 23:18:45 +00:00
|
|
|
|
2012-06-19 06:18:37 +00:00
|
|
|
fd = mfi_open(mfi_unit, O_RDWR);
|
2009-08-13 23:18:45 +00:00
|
|
|
if (fd < 0) {
|
2010-10-26 19:11:09 +00:00
|
|
|
error = errno;
|
2009-08-13 23:18:45 +00:00
|
|
|
warn("mfi_open");
|
2010-10-26 19:11:09 +00:00
|
|
|
return (error);
|
2009-08-13 23:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mfi_dcmd_command(fd, MFI_DCMD_PR_STOP, NULL, 0, NULL, 0, NULL) <
|
|
|
|
0) {
|
2010-10-26 19:11:09 +00:00
|
|
|
error = errno;
|
2009-08-13 23:18:45 +00:00
|
|
|
warn("Failed to stop patrol read");
|
2011-06-09 19:52:28 +00:00
|
|
|
close(fd);
|
2010-10-26 19:11:09 +00:00
|
|
|
return (error);
|
2009-08-13 23:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
MFI_COMMAND(stop, patrol, stop_patrol);
|
|
|
|
|
|
|
|
static int
|
|
|
|
patrol_config(int ac, char **av)
|
|
|
|
{
|
|
|
|
struct mfi_pr_properties prop;
|
|
|
|
long val;
|
|
|
|
time_t now;
|
2010-10-26 19:11:09 +00:00
|
|
|
int error, fd;
|
2009-08-13 23:18:45 +00:00
|
|
|
uint32_t at, next_exec, exec_freq;
|
|
|
|
char *cp;
|
|
|
|
uint8_t op_mode;
|
|
|
|
|
|
|
|
exec_freq = 0; /* GCC too stupid */
|
|
|
|
next_exec = 0;
|
|
|
|
if (ac < 2) {
|
|
|
|
warnx("patrol: command required");
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
if (strcasecmp(av[1], "auto") == 0) {
|
|
|
|
op_mode = MFI_PR_OPMODE_AUTO;
|
|
|
|
if (ac > 2) {
|
2011-04-27 14:58:06 +00:00
|
|
|
if (strcasecmp(av[2], "continuously") == 0)
|
2009-08-13 23:18:45 +00:00
|
|
|
exec_freq = 0xffffffff;
|
|
|
|
else {
|
|
|
|
val = strtol(av[2], &cp, 0);
|
|
|
|
if (*cp != '\0') {
|
|
|
|
warnx("patrol: Invalid interval %s",
|
|
|
|
av[2]);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
exec_freq = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ac > 3) {
|
|
|
|
val = strtol(av[3], &cp, 0);
|
|
|
|
if (*cp != '\0' || val < 0) {
|
|
|
|
warnx("patrol: Invalid start time %s", av[3]);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
next_exec = val;
|
|
|
|
}
|
|
|
|
} else if (strcasecmp(av[1], "manual") == 0)
|
|
|
|
op_mode = MFI_PR_OPMODE_MANUAL;
|
|
|
|
else if (strcasecmp(av[1], "disable") == 0)
|
|
|
|
op_mode = MFI_PR_OPMODE_DISABLED;
|
|
|
|
else {
|
|
|
|
warnx("patrol: Invalid command %s", av[1]);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
2012-06-19 06:18:37 +00:00
|
|
|
fd = mfi_open(mfi_unit, O_RDWR);
|
2009-08-13 23:18:45 +00:00
|
|
|
if (fd < 0) {
|
2010-10-26 19:11:09 +00:00
|
|
|
error = errno;
|
2009-08-13 23:18:45 +00:00
|
|
|
warn("mfi_open");
|
2010-10-26 19:11:09 +00:00
|
|
|
return (error);
|
2009-08-13 23:18:45 +00:00
|
|
|
}
|
|
|
|
|
2010-10-26 19:11:09 +00:00
|
|
|
error = patrol_get_props(fd, &prop);
|
2011-06-09 19:52:28 +00:00
|
|
|
if (error) {
|
|
|
|
close(fd);
|
2010-10-26 19:11:09 +00:00
|
|
|
return (error);
|
2011-06-09 19:52:28 +00:00
|
|
|
}
|
2009-08-13 23:18:45 +00:00
|
|
|
prop.op_mode = op_mode;
|
|
|
|
if (op_mode == MFI_PR_OPMODE_AUTO) {
|
|
|
|
if (ac > 2)
|
|
|
|
prop.exec_freq = exec_freq;
|
|
|
|
if (ac > 3) {
|
|
|
|
time(&now);
|
|
|
|
mfi_get_time(fd, &at);
|
2011-06-09 19:52:28 +00:00
|
|
|
if (at == 0) {
|
|
|
|
close(fd);
|
2009-08-13 23:18:45 +00:00
|
|
|
return (ENXIO);
|
2011-06-09 19:52:28 +00:00
|
|
|
}
|
2009-08-13 23:18:45 +00:00
|
|
|
prop.next_exec = at + next_exec;
|
|
|
|
printf("Starting next patrol read at %s",
|
|
|
|
adapter_time(now, at, prop.next_exec));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mfi_dcmd_command(fd, MFI_DCMD_PR_SET_PROPERTIES, &prop,
|
|
|
|
sizeof(prop), NULL, 0, NULL) < 0) {
|
2010-10-26 19:11:09 +00:00
|
|
|
error = errno;
|
2009-08-13 23:18:45 +00:00
|
|
|
warn("Failed to set patrol read properties");
|
2011-06-09 19:52:28 +00:00
|
|
|
close(fd);
|
2010-10-26 19:11:09 +00:00
|
|
|
return (error);
|
2009-08-13 23:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
MFI_COMMAND(top, patrol, patrol_config);
|