2 * Copyright (c) 2008, 2009 Yahoo!, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The names of the authors may not be used to endorse or promote
14 * products derived from this software without specific prior written
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: src/usr.sbin/mfiutil/mfi_patrol.c,v 1.5 2011/06/20 21:28:50 bz Exp $
32 #include <sys/types.h>
33 #include <sys/errno.h>
43 adapter_time(time_t now, uint32_t at_now, uint32_t at)
47 t = (now - at_now) + at;
52 mfi_get_time(int fd, uint32_t *at)
55 if (mfi_dcmd_command(fd, MFI_DCMD_TIME_SECS_GET, at, sizeof(*at), NULL,
57 warn("Couldn't fetch adapter time");
63 patrol_get_props(int fd, struct mfi_pr_properties *prop)
67 if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_PROPERTIES, prop,
68 sizeof(*prop), NULL, 0, NULL) < 0) {
70 warn("Failed to get patrol read properties");
77 show_patrol(__unused int ac, __unused char **av)
79 struct mfi_pr_properties prop;
80 struct mfi_pr_status status;
81 struct mfi_pd_list *list;
82 struct mfi_pd_info info;
89 fd = mfi_open(mfi_unit);
97 mfi_get_time(fd, &at);
98 error = patrol_get_props(fd, &prop);
103 printf("Operation Mode: ");
104 switch (prop.op_mode) {
105 case MFI_PR_OPMODE_AUTO:
108 case MFI_PR_OPMODE_MANUAL:
111 case MFI_PR_OPMODE_DISABLED:
112 printf("disabled\n");
115 printf("??? (%02x)\n", prop.op_mode);
118 if (prop.op_mode == MFI_PR_OPMODE_AUTO) {
119 if (at != 0 && prop.next_exec)
120 printf(" Next Run Starts: %s", adapter_time(now, at,
122 if (prop.exec_freq == 0xffffffff)
123 printf(" Runs Execute Continuously\n");
124 else if (prop.exec_freq != 0)
125 printf(" Runs Start Every %u seconds\n",
129 if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_STATUS, &status,
130 sizeof(status), NULL, 0, NULL) < 0) {
132 warn("Failed to get patrol read properties");
136 printf("Runs Completed: %u\n", status.num_iteration);
137 printf("Current State: ");
138 switch (status.state) {
139 case MFI_PR_STATE_STOPPED:
142 case MFI_PR_STATE_READY:
145 case MFI_PR_STATE_ACTIVE:
148 case MFI_PR_STATE_ABORTED:
152 printf("??? (%02x)\n", status.state);
155 if (status.state == MFI_PR_STATE_ACTIVE) {
156 if (mfi_pd_get_list(fd, &list, NULL) < 0) {
158 warn("Failed to get drive list");
163 for (i = 0; i < list->count; i++) {
164 if (list->addr[i].scsi_dev_type != 0)
167 if (mfi_pd_get_info(fd, list->addr[i].device_id, &info,
170 warn("Failed to fetch info for drive %u",
171 list->addr[i].device_id);
176 if (info.prog_info.active & MFI_PD_PROGRESS_PATROL) {
177 snprintf(label, sizeof(label), " Drive %s",
179 list->addr[i].device_id,
180 MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
181 mfi_display_progress(label,
182 &info.prog_info.patrol);
192 MFI_COMMAND(show, patrol, show_patrol);
195 start_patrol(__unused int ac, __unused char **av)
199 fd = mfi_open(mfi_unit);
206 if (mfi_dcmd_command(fd, MFI_DCMD_PR_START, NULL, 0, NULL, 0, NULL) <
209 warn("Failed to start patrol read");
218 MFI_COMMAND(start, patrol, start_patrol);
221 stop_patrol(__unused int ac, __unused char **av)
225 fd = mfi_open(mfi_unit);
232 if (mfi_dcmd_command(fd, MFI_DCMD_PR_STOP, NULL, 0, NULL, 0, NULL) <
235 warn("Failed to stop patrol read");
244 MFI_COMMAND(stop, patrol, stop_patrol);
247 patrol_config(int ac, char **av)
249 struct mfi_pr_properties prop;
253 uint32_t at, next_exec, exec_freq;
257 exec_freq = 0; /* GCC too stupid */
260 warnx("patrol: command required");
263 if (strcasecmp(av[1], "auto") == 0) {
264 op_mode = MFI_PR_OPMODE_AUTO;
266 if (strcasecmp(av[2], "continuously") == 0)
267 exec_freq = 0xffffffff;
269 val = strtol(av[2], &cp, 0);
271 warnx("patrol: Invalid interval %s",
279 val = strtol(av[3], &cp, 0);
280 if (*cp != '\0' || val < 0) {
281 warnx("patrol: Invalid start time %s", av[3]);
286 } else if (strcasecmp(av[1], "manual") == 0)
287 op_mode = MFI_PR_OPMODE_MANUAL;
288 else if (strcasecmp(av[1], "disable") == 0)
289 op_mode = MFI_PR_OPMODE_DISABLED;
291 warnx("patrol: Invalid command %s", av[1]);
295 fd = mfi_open(mfi_unit);
302 error = patrol_get_props(fd, &prop);
307 prop.op_mode = op_mode;
308 if (op_mode == MFI_PR_OPMODE_AUTO) {
310 prop.exec_freq = exec_freq;
313 mfi_get_time(fd, &at);
318 prop.next_exec = at + next_exec;
319 printf("Starting next patrol read at %s",
320 adapter_time(now, at, prop.next_exec));
323 if (mfi_dcmd_command(fd, MFI_DCMD_PR_SET_PROPERTIES, &prop,
324 sizeof(prop), NULL, 0, NULL) < 0) {
326 warn("Failed to set patrol read properties");
335 MFI_COMMAND(top, patrol, patrol_config);