mfi_patrol.c revision 222899
1196200Sscottl/*- 2196200Sscottl * Copyright (c) 2008, 2009 Yahoo!, Inc. 3196200Sscottl * All rights reserved. 4196200Sscottl * 5196200Sscottl * Redistribution and use in source and binary forms, with or without 6196200Sscottl * modification, are permitted provided that the following conditions 7196200Sscottl * are met: 8196200Sscottl * 1. Redistributions of source code must retain the above copyright 9196200Sscottl * notice, this list of conditions and the following disclaimer. 10196200Sscottl * 2. Redistributions in binary form must reproduce the above copyright 11196200Sscottl * notice, this list of conditions and the following disclaimer in the 12196200Sscottl * documentation and/or other materials provided with the distribution. 13196200Sscottl * 3. The names of the authors may not be used to endorse or promote 14196200Sscottl * products derived from this software without specific prior written 15196200Sscottl * permission. 16196200Sscottl * 17196200Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18196200Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19196200Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20196200Sscottl * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21196200Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22196200Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23196200Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24196200Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25196200Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26196200Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27196200Sscottl * SUCH DAMAGE. 28196200Sscottl * 29196200Sscottl * $FreeBSD: head/usr.sbin/mfiutil/mfi_patrol.c 222899 2011-06-09 19:52:28Z bz $ 30196200Sscottl */ 31196200Sscottl 32196200Sscottl#include <sys/types.h> 33196200Sscottl#include <sys/errno.h> 34196200Sscottl#include <err.h> 35196200Sscottl#include <stdio.h> 36196200Sscottl#include <stdlib.h> 37196200Sscottl#include <string.h> 38196200Sscottl#include <time.h> 39196200Sscottl#include <unistd.h> 40196200Sscottl#include "mfiutil.h" 41196200Sscottl 42196200Sscottlstatic char * 43196200Sscottladapter_time(time_t now, uint32_t at_now, uint32_t at) 44196200Sscottl{ 45196200Sscottl time_t t; 46196200Sscottl 47196200Sscottl t = (now - at_now) + at; 48196200Sscottl return (ctime(&t)); 49196200Sscottl} 50196200Sscottl 51196200Sscottlstatic void 52196200Sscottlmfi_get_time(int fd, uint32_t *at) 53196200Sscottl{ 54196200Sscottl 55196200Sscottl if (mfi_dcmd_command(fd, MFI_DCMD_TIME_SECS_GET, at, sizeof(*at), NULL, 56196200Sscottl 0, NULL) < 0) { 57196200Sscottl warn("Couldn't fetch adapter time"); 58196200Sscottl at = 0; 59196200Sscottl } 60196200Sscottl} 61196200Sscottl 62196200Sscottlstatic int 63196200Sscottlpatrol_get_props(int fd, struct mfi_pr_properties *prop) 64196200Sscottl{ 65214396Sjhb int error; 66196200Sscottl 67196200Sscottl if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_PROPERTIES, prop, 68196200Sscottl sizeof(*prop), NULL, 0, NULL) < 0) { 69214396Sjhb error = errno; 70196200Sscottl warn("Failed to get patrol read properties"); 71214396Sjhb return (error); 72196200Sscottl } 73196200Sscottl return (0); 74196200Sscottl} 75196200Sscottl 76196200Sscottlstatic int 77196200Sscottlshow_patrol(int ac, char **av) 78196200Sscottl{ 79196200Sscottl struct mfi_pr_properties prop; 80196200Sscottl struct mfi_pr_status status; 81196200Sscottl struct mfi_pd_list *list; 82196200Sscottl struct mfi_pd_info info; 83196200Sscottl char label[16]; 84196200Sscottl time_t now; 85196200Sscottl uint32_t at; 86214396Sjhb int error, fd; 87196200Sscottl u_int i; 88196200Sscottl 89196200Sscottl fd = mfi_open(mfi_unit); 90196200Sscottl if (fd < 0) { 91214396Sjhb error = errno; 92196200Sscottl warn("mfi_open"); 93214396Sjhb return (error); 94196200Sscottl } 95196200Sscottl 96196200Sscottl time(&now); 97196200Sscottl mfi_get_time(fd, &at); 98214396Sjhb error = patrol_get_props(fd, &prop); 99222899Sbz if (error) { 100222899Sbz close(fd); 101214396Sjhb return (error); 102222899Sbz } 103196200Sscottl printf("Operation Mode: "); 104196200Sscottl switch (prop.op_mode) { 105196200Sscottl case MFI_PR_OPMODE_AUTO: 106196200Sscottl printf("auto\n"); 107196200Sscottl break; 108196200Sscottl case MFI_PR_OPMODE_MANUAL: 109196200Sscottl printf("manual\n"); 110196200Sscottl break; 111196200Sscottl case MFI_PR_OPMODE_DISABLED: 112196200Sscottl printf("disabled\n"); 113196200Sscottl break; 114196200Sscottl default: 115196200Sscottl printf("??? (%02x)\n", prop.op_mode); 116196200Sscottl break; 117196200Sscottl } 118196200Sscottl if (prop.op_mode == MFI_PR_OPMODE_AUTO) { 119196200Sscottl if (at != 0 && prop.next_exec) 120196200Sscottl printf(" Next Run Starts: %s", adapter_time(now, at, 121196200Sscottl prop.next_exec)); 122196200Sscottl if (prop.exec_freq == 0xffffffff) 123196200Sscottl printf(" Runs Execute Continuously\n"); 124196200Sscottl else if (prop.exec_freq != 0) 125196200Sscottl printf(" Runs Start Every %u seconds\n", 126196200Sscottl prop.exec_freq); 127196200Sscottl } 128196200Sscottl 129196200Sscottl if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_STATUS, &status, 130196200Sscottl sizeof(status), NULL, 0, NULL) < 0) { 131214396Sjhb error = errno; 132196200Sscottl warn("Failed to get patrol read properties"); 133222899Sbz close(fd); 134214396Sjhb return (error); 135196200Sscottl } 136196200Sscottl printf("Runs Completed: %u\n", status.num_iteration); 137196200Sscottl printf("Current State: "); 138196200Sscottl switch (status.state) { 139196200Sscottl case MFI_PR_STATE_STOPPED: 140196200Sscottl printf("stopped\n"); 141196200Sscottl break; 142196200Sscottl case MFI_PR_STATE_READY: 143196200Sscottl printf("ready\n"); 144196200Sscottl break; 145196200Sscottl case MFI_PR_STATE_ACTIVE: 146196200Sscottl printf("active\n"); 147196200Sscottl break; 148196200Sscottl case MFI_PR_STATE_ABORTED: 149196200Sscottl printf("aborted\n"); 150196200Sscottl break; 151196200Sscottl default: 152196200Sscottl printf("??? (%02x)\n", status.state); 153196200Sscottl break; 154196200Sscottl } 155196200Sscottl if (status.state == MFI_PR_STATE_ACTIVE) { 156196200Sscottl if (mfi_pd_get_list(fd, &list, NULL) < 0) { 157214396Sjhb error = errno; 158196200Sscottl warn("Failed to get drive list"); 159222899Sbz close(fd); 160214396Sjhb return (error); 161196200Sscottl } 162196200Sscottl 163196200Sscottl for (i = 0; i < list->count; i++) { 164196200Sscottl if (list->addr[i].scsi_dev_type != 0) 165196200Sscottl continue; 166196200Sscottl 167196200Sscottl if (mfi_pd_get_info(fd, list->addr[i].device_id, &info, 168196200Sscottl NULL) < 0) { 169214396Sjhb error = errno; 170196200Sscottl warn("Failed to fetch info for drive %u", 171196200Sscottl list->addr[i].device_id); 172222899Sbz free(list); 173222899Sbz close(fd); 174214396Sjhb return (error); 175196200Sscottl } 176196200Sscottl if (info.prog_info.active & MFI_PD_PROGRESS_PATROL) { 177196200Sscottl snprintf(label, sizeof(label), " Drive %u", 178196200Sscottl list->addr[i].device_id); 179196200Sscottl mfi_display_progress(label, 180196200Sscottl &info.prog_info.patrol); 181196200Sscottl } 182196200Sscottl } 183222899Sbz free(list); 184196200Sscottl } 185196200Sscottl 186196200Sscottl close(fd); 187196200Sscottl 188196200Sscottl return (0); 189196200Sscottl} 190196200SscottlMFI_COMMAND(show, patrol, show_patrol); 191196200Sscottl 192196200Sscottlstatic int 193196200Sscottlstart_patrol(int ac, char **av) 194196200Sscottl{ 195214396Sjhb int error, fd; 196196200Sscottl 197196200Sscottl fd = mfi_open(mfi_unit); 198196200Sscottl if (fd < 0) { 199214396Sjhb error = errno; 200196200Sscottl warn("mfi_open"); 201214396Sjhb return (error); 202196200Sscottl } 203196200Sscottl 204196200Sscottl if (mfi_dcmd_command(fd, MFI_DCMD_PR_START, NULL, 0, NULL, 0, NULL) < 205196200Sscottl 0) { 206214396Sjhb error = errno; 207196200Sscottl warn("Failed to start patrol read"); 208222899Sbz close(fd); 209214396Sjhb return (error); 210196200Sscottl } 211196200Sscottl 212196200Sscottl close(fd); 213196200Sscottl 214196200Sscottl return (0); 215196200Sscottl} 216196200SscottlMFI_COMMAND(start, patrol, start_patrol); 217196200Sscottl 218196200Sscottlstatic int 219196200Sscottlstop_patrol(int ac, char **av) 220196200Sscottl{ 221214396Sjhb int error, fd; 222196200Sscottl 223196200Sscottl fd = mfi_open(mfi_unit); 224196200Sscottl if (fd < 0) { 225214396Sjhb error = errno; 226196200Sscottl warn("mfi_open"); 227214396Sjhb return (error); 228196200Sscottl } 229196200Sscottl 230196200Sscottl if (mfi_dcmd_command(fd, MFI_DCMD_PR_STOP, NULL, 0, NULL, 0, NULL) < 231196200Sscottl 0) { 232214396Sjhb error = errno; 233196200Sscottl warn("Failed to stop patrol read"); 234222899Sbz close(fd); 235214396Sjhb return (error); 236196200Sscottl } 237196200Sscottl 238196200Sscottl close(fd); 239196200Sscottl 240196200Sscottl return (0); 241196200Sscottl} 242196200SscottlMFI_COMMAND(stop, patrol, stop_patrol); 243196200Sscottl 244196200Sscottlstatic int 245196200Sscottlpatrol_config(int ac, char **av) 246196200Sscottl{ 247196200Sscottl struct mfi_pr_properties prop; 248196200Sscottl long val; 249196200Sscottl time_t now; 250214396Sjhb int error, fd; 251196200Sscottl uint32_t at, next_exec, exec_freq; 252196200Sscottl char *cp; 253196200Sscottl uint8_t op_mode; 254196200Sscottl 255196200Sscottl exec_freq = 0; /* GCC too stupid */ 256196200Sscottl next_exec = 0; 257196200Sscottl if (ac < 2) { 258196200Sscottl warnx("patrol: command required"); 259196200Sscottl return (EINVAL); 260196200Sscottl } 261196200Sscottl if (strcasecmp(av[1], "auto") == 0) { 262196200Sscottl op_mode = MFI_PR_OPMODE_AUTO; 263196200Sscottl if (ac > 2) { 264221119Spluknet if (strcasecmp(av[2], "continuously") == 0) 265196200Sscottl exec_freq = 0xffffffff; 266196200Sscottl else { 267196200Sscottl val = strtol(av[2], &cp, 0); 268196200Sscottl if (*cp != '\0') { 269196200Sscottl warnx("patrol: Invalid interval %s", 270196200Sscottl av[2]); 271196200Sscottl return (EINVAL); 272196200Sscottl } 273196200Sscottl exec_freq = val; 274196200Sscottl } 275196200Sscottl } 276196200Sscottl if (ac > 3) { 277196200Sscottl val = strtol(av[3], &cp, 0); 278196200Sscottl if (*cp != '\0' || val < 0) { 279196200Sscottl warnx("patrol: Invalid start time %s", av[3]); 280196200Sscottl return (EINVAL); 281196200Sscottl } 282196200Sscottl next_exec = val; 283196200Sscottl } 284196200Sscottl } else if (strcasecmp(av[1], "manual") == 0) 285196200Sscottl op_mode = MFI_PR_OPMODE_MANUAL; 286196200Sscottl else if (strcasecmp(av[1], "disable") == 0) 287196200Sscottl op_mode = MFI_PR_OPMODE_DISABLED; 288196200Sscottl else { 289196200Sscottl warnx("patrol: Invalid command %s", av[1]); 290196200Sscottl return (EINVAL); 291196200Sscottl } 292196200Sscottl 293196200Sscottl fd = mfi_open(mfi_unit); 294196200Sscottl if (fd < 0) { 295214396Sjhb error = errno; 296196200Sscottl warn("mfi_open"); 297214396Sjhb return (error); 298196200Sscottl } 299196200Sscottl 300214396Sjhb error = patrol_get_props(fd, &prop); 301222899Sbz if (error) { 302222899Sbz close(fd); 303214396Sjhb return (error); 304222899Sbz } 305196200Sscottl prop.op_mode = op_mode; 306196200Sscottl if (op_mode == MFI_PR_OPMODE_AUTO) { 307196200Sscottl if (ac > 2) 308196200Sscottl prop.exec_freq = exec_freq; 309196200Sscottl if (ac > 3) { 310196200Sscottl time(&now); 311196200Sscottl mfi_get_time(fd, &at); 312222899Sbz if (at == 0) { 313222899Sbz close(fd); 314196200Sscottl return (ENXIO); 315222899Sbz } 316196200Sscottl prop.next_exec = at + next_exec; 317196200Sscottl printf("Starting next patrol read at %s", 318196200Sscottl adapter_time(now, at, prop.next_exec)); 319196200Sscottl } 320196200Sscottl } 321196200Sscottl if (mfi_dcmd_command(fd, MFI_DCMD_PR_SET_PROPERTIES, &prop, 322196200Sscottl sizeof(prop), NULL, 0, NULL) < 0) { 323214396Sjhb error = errno; 324196200Sscottl warn("Failed to set patrol read properties"); 325222899Sbz close(fd); 326214396Sjhb return (error); 327196200Sscottl } 328196200Sscottl 329196200Sscottl close(fd); 330196200Sscottl 331196200Sscottl return (0); 332196200Sscottl} 333196200SscottlMFI_COMMAND(top, patrol, patrol_config); 334