mfi_patrol.c revision 214396
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 214396 2010-10-26 19:11:09Z jhb $ 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); 99214396Sjhb if (error) 100214396Sjhb return (error); 101196200Sscottl printf("Operation Mode: "); 102196200Sscottl switch (prop.op_mode) { 103196200Sscottl case MFI_PR_OPMODE_AUTO: 104196200Sscottl printf("auto\n"); 105196200Sscottl break; 106196200Sscottl case MFI_PR_OPMODE_MANUAL: 107196200Sscottl printf("manual\n"); 108196200Sscottl break; 109196200Sscottl case MFI_PR_OPMODE_DISABLED: 110196200Sscottl printf("disabled\n"); 111196200Sscottl break; 112196200Sscottl default: 113196200Sscottl printf("??? (%02x)\n", prop.op_mode); 114196200Sscottl break; 115196200Sscottl } 116196200Sscottl if (prop.op_mode == MFI_PR_OPMODE_AUTO) { 117196200Sscottl if (at != 0 && prop.next_exec) 118196200Sscottl printf(" Next Run Starts: %s", adapter_time(now, at, 119196200Sscottl prop.next_exec)); 120196200Sscottl if (prop.exec_freq == 0xffffffff) 121196200Sscottl printf(" Runs Execute Continuously\n"); 122196200Sscottl else if (prop.exec_freq != 0) 123196200Sscottl printf(" Runs Start Every %u seconds\n", 124196200Sscottl prop.exec_freq); 125196200Sscottl } 126196200Sscottl 127196200Sscottl if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_STATUS, &status, 128196200Sscottl sizeof(status), NULL, 0, NULL) < 0) { 129214396Sjhb error = errno; 130196200Sscottl warn("Failed to get patrol read properties"); 131214396Sjhb return (error); 132196200Sscottl } 133196200Sscottl printf("Runs Completed: %u\n", status.num_iteration); 134196200Sscottl printf("Current State: "); 135196200Sscottl switch (status.state) { 136196200Sscottl case MFI_PR_STATE_STOPPED: 137196200Sscottl printf("stopped\n"); 138196200Sscottl break; 139196200Sscottl case MFI_PR_STATE_READY: 140196200Sscottl printf("ready\n"); 141196200Sscottl break; 142196200Sscottl case MFI_PR_STATE_ACTIVE: 143196200Sscottl printf("active\n"); 144196200Sscottl break; 145196200Sscottl case MFI_PR_STATE_ABORTED: 146196200Sscottl printf("aborted\n"); 147196200Sscottl break; 148196200Sscottl default: 149196200Sscottl printf("??? (%02x)\n", status.state); 150196200Sscottl break; 151196200Sscottl } 152196200Sscottl if (status.state == MFI_PR_STATE_ACTIVE) { 153196200Sscottl if (mfi_pd_get_list(fd, &list, NULL) < 0) { 154214396Sjhb error = errno; 155196200Sscottl warn("Failed to get drive list"); 156214396Sjhb return (error); 157196200Sscottl } 158196200Sscottl 159196200Sscottl for (i = 0; i < list->count; i++) { 160196200Sscottl if (list->addr[i].scsi_dev_type != 0) 161196200Sscottl continue; 162196200Sscottl 163196200Sscottl if (mfi_pd_get_info(fd, list->addr[i].device_id, &info, 164196200Sscottl NULL) < 0) { 165214396Sjhb error = errno; 166196200Sscottl warn("Failed to fetch info for drive %u", 167196200Sscottl list->addr[i].device_id); 168214396Sjhb return (error); 169196200Sscottl } 170196200Sscottl if (info.prog_info.active & MFI_PD_PROGRESS_PATROL) { 171196200Sscottl snprintf(label, sizeof(label), " Drive %u", 172196200Sscottl list->addr[i].device_id); 173196200Sscottl mfi_display_progress(label, 174196200Sscottl &info.prog_info.patrol); 175196200Sscottl } 176196200Sscottl } 177196200Sscottl } 178196200Sscottl 179196200Sscottl close(fd); 180196200Sscottl 181196200Sscottl return (0); 182196200Sscottl} 183196200SscottlMFI_COMMAND(show, patrol, show_patrol); 184196200Sscottl 185196200Sscottlstatic int 186196200Sscottlstart_patrol(int ac, char **av) 187196200Sscottl{ 188214396Sjhb int error, fd; 189196200Sscottl 190196200Sscottl fd = mfi_open(mfi_unit); 191196200Sscottl if (fd < 0) { 192214396Sjhb error = errno; 193196200Sscottl warn("mfi_open"); 194214396Sjhb return (error); 195196200Sscottl } 196196200Sscottl 197196200Sscottl if (mfi_dcmd_command(fd, MFI_DCMD_PR_START, NULL, 0, NULL, 0, NULL) < 198196200Sscottl 0) { 199214396Sjhb error = errno; 200196200Sscottl warn("Failed to start patrol read"); 201214396Sjhb return (error); 202196200Sscottl } 203196200Sscottl 204196200Sscottl close(fd); 205196200Sscottl 206196200Sscottl return (0); 207196200Sscottl} 208196200SscottlMFI_COMMAND(start, patrol, start_patrol); 209196200Sscottl 210196200Sscottlstatic int 211196200Sscottlstop_patrol(int ac, char **av) 212196200Sscottl{ 213214396Sjhb int error, fd; 214196200Sscottl 215196200Sscottl fd = mfi_open(mfi_unit); 216196200Sscottl if (fd < 0) { 217214396Sjhb error = errno; 218196200Sscottl warn("mfi_open"); 219214396Sjhb return (error); 220196200Sscottl } 221196200Sscottl 222196200Sscottl if (mfi_dcmd_command(fd, MFI_DCMD_PR_STOP, NULL, 0, NULL, 0, NULL) < 223196200Sscottl 0) { 224214396Sjhb error = errno; 225196200Sscottl warn("Failed to stop patrol read"); 226214396Sjhb return (error); 227196200Sscottl } 228196200Sscottl 229196200Sscottl close(fd); 230196200Sscottl 231196200Sscottl return (0); 232196200Sscottl} 233196200SscottlMFI_COMMAND(stop, patrol, stop_patrol); 234196200Sscottl 235196200Sscottlstatic int 236196200Sscottlpatrol_config(int ac, char **av) 237196200Sscottl{ 238196200Sscottl struct mfi_pr_properties prop; 239196200Sscottl long val; 240196200Sscottl time_t now; 241214396Sjhb int error, fd; 242196200Sscottl uint32_t at, next_exec, exec_freq; 243196200Sscottl char *cp; 244196200Sscottl uint8_t op_mode; 245196200Sscottl 246196200Sscottl exec_freq = 0; /* GCC too stupid */ 247196200Sscottl next_exec = 0; 248196200Sscottl if (ac < 2) { 249196200Sscottl warnx("patrol: command required"); 250196200Sscottl return (EINVAL); 251196200Sscottl } 252196200Sscottl if (strcasecmp(av[1], "auto") == 0) { 253196200Sscottl op_mode = MFI_PR_OPMODE_AUTO; 254196200Sscottl if (ac > 2) { 255196200Sscottl if (strcasecmp(av[2], "continously") == 0) 256196200Sscottl exec_freq = 0xffffffff; 257196200Sscottl else { 258196200Sscottl val = strtol(av[2], &cp, 0); 259196200Sscottl if (*cp != '\0') { 260196200Sscottl warnx("patrol: Invalid interval %s", 261196200Sscottl av[2]); 262196200Sscottl return (EINVAL); 263196200Sscottl } 264196200Sscottl exec_freq = val; 265196200Sscottl } 266196200Sscottl } 267196200Sscottl if (ac > 3) { 268196200Sscottl val = strtol(av[3], &cp, 0); 269196200Sscottl if (*cp != '\0' || val < 0) { 270196200Sscottl warnx("patrol: Invalid start time %s", av[3]); 271196200Sscottl return (EINVAL); 272196200Sscottl } 273196200Sscottl next_exec = val; 274196200Sscottl } 275196200Sscottl } else if (strcasecmp(av[1], "manual") == 0) 276196200Sscottl op_mode = MFI_PR_OPMODE_MANUAL; 277196200Sscottl else if (strcasecmp(av[1], "disable") == 0) 278196200Sscottl op_mode = MFI_PR_OPMODE_DISABLED; 279196200Sscottl else { 280196200Sscottl warnx("patrol: Invalid command %s", av[1]); 281196200Sscottl return (EINVAL); 282196200Sscottl } 283196200Sscottl 284196200Sscottl fd = mfi_open(mfi_unit); 285196200Sscottl if (fd < 0) { 286214396Sjhb error = errno; 287196200Sscottl warn("mfi_open"); 288214396Sjhb return (error); 289196200Sscottl } 290196200Sscottl 291214396Sjhb error = patrol_get_props(fd, &prop); 292214396Sjhb if (error) 293214396Sjhb return (error); 294196200Sscottl prop.op_mode = op_mode; 295196200Sscottl if (op_mode == MFI_PR_OPMODE_AUTO) { 296196200Sscottl if (ac > 2) 297196200Sscottl prop.exec_freq = exec_freq; 298196200Sscottl if (ac > 3) { 299196200Sscottl time(&now); 300196200Sscottl mfi_get_time(fd, &at); 301196200Sscottl if (at == 0) 302196200Sscottl return (ENXIO); 303196200Sscottl prop.next_exec = at + next_exec; 304196200Sscottl printf("Starting next patrol read at %s", 305196200Sscottl adapter_time(now, at, prop.next_exec)); 306196200Sscottl } 307196200Sscottl } 308196200Sscottl if (mfi_dcmd_command(fd, MFI_DCMD_PR_SET_PROPERTIES, &prop, 309196200Sscottl sizeof(prop), NULL, 0, NULL) < 0) { 310214396Sjhb error = errno; 311196200Sscottl warn("Failed to set patrol read properties"); 312214396Sjhb return (error); 313196200Sscottl } 314196200Sscottl 315196200Sscottl close(fd); 316196200Sscottl 317196200Sscottl return (0); 318196200Sscottl} 319196200SscottlMFI_COMMAND(top, patrol, patrol_config); 320