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
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)
66 if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_PROPERTIES, prop,
67 sizeof(*prop), NULL, 0, NULL) < 0) {
68 warn("Failed to get patrol read properties");
75 show_patrol(int ac, char **av)
77 struct mfi_pr_properties prop;
78 struct mfi_pr_status status;
79 struct mfi_pd_list *list;
80 struct mfi_pd_info info;
87 fd = mfi_open(mfi_unit);
94 mfi_get_time(fd, &at);
95 if (patrol_get_props(fd, &prop) < 0)
97 printf("Operation Mode: ");
98 switch (prop.op_mode) {
99 case MFI_PR_OPMODE_AUTO:
102 case MFI_PR_OPMODE_MANUAL:
105 case MFI_PR_OPMODE_DISABLED:
106 printf("disabled\n");
109 printf("??? (%02x)\n", prop.op_mode);
112 if (prop.op_mode == MFI_PR_OPMODE_AUTO) {
113 if (at != 0 && prop.next_exec)
114 printf(" Next Run Starts: %s", adapter_time(now, at,
116 if (prop.exec_freq == 0xffffffff)
117 printf(" Runs Execute Continuously\n");
118 else if (prop.exec_freq != 0)
119 printf(" Runs Start Every %u seconds\n",
123 if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_STATUS, &status,
124 sizeof(status), NULL, 0, NULL) < 0) {
125 warn("Failed to get patrol read properties");
128 printf("Runs Completed: %u\n", status.num_iteration);
129 printf("Current State: ");
130 switch (status.state) {
131 case MFI_PR_STATE_STOPPED:
134 case MFI_PR_STATE_READY:
137 case MFI_PR_STATE_ACTIVE:
140 case MFI_PR_STATE_ABORTED:
144 printf("??? (%02x)\n", status.state);
147 if (status.state == MFI_PR_STATE_ACTIVE) {
148 if (mfi_pd_get_list(fd, &list, NULL) < 0) {
149 warn("Failed to get drive list");
153 for (i = 0; i < list->count; i++) {
154 if (list->addr[i].scsi_dev_type != 0)
157 if (mfi_pd_get_info(fd, list->addr[i].device_id, &info,
159 warn("Failed to fetch info for drive %u",
160 list->addr[i].device_id);
163 if (info.prog_info.active & MFI_PD_PROGRESS_PATROL) {
164 snprintf(label, sizeof(label), " Drive %u",
165 list->addr[i].device_id);
166 mfi_display_progress(label,
167 &info.prog_info.patrol);
176 MFI_COMMAND(show, patrol, show_patrol);
179 start_patrol(int ac, char **av)
183 fd = mfi_open(mfi_unit);
189 if (mfi_dcmd_command(fd, MFI_DCMD_PR_START, NULL, 0, NULL, 0, NULL) <
191 warn("Failed to start patrol read");
199 MFI_COMMAND(start, patrol, start_patrol);
202 stop_patrol(int ac, char **av)
206 fd = mfi_open(mfi_unit);
212 if (mfi_dcmd_command(fd, MFI_DCMD_PR_STOP, NULL, 0, NULL, 0, NULL) <
214 warn("Failed to stop patrol read");
222 MFI_COMMAND(stop, patrol, stop_patrol);
225 patrol_config(int ac, char **av)
227 struct mfi_pr_properties prop;
230 uint32_t at, next_exec, exec_freq;
235 exec_freq = 0; /* GCC too stupid */
238 warnx("patrol: command required");
241 if (strcasecmp(av[1], "auto") == 0) {
242 op_mode = MFI_PR_OPMODE_AUTO;
244 if (strcasecmp(av[2], "continously") == 0)
245 exec_freq = 0xffffffff;
247 val = strtol(av[2], &cp, 0);
249 warnx("patrol: Invalid interval %s",
257 val = strtol(av[3], &cp, 0);
258 if (*cp != '\0' || val < 0) {
259 warnx("patrol: Invalid start time %s", av[3]);
264 } else if (strcasecmp(av[1], "manual") == 0)
265 op_mode = MFI_PR_OPMODE_MANUAL;
266 else if (strcasecmp(av[1], "disable") == 0)
267 op_mode = MFI_PR_OPMODE_DISABLED;
269 warnx("patrol: Invalid command %s", av[1]);
273 fd = mfi_open(mfi_unit);
279 if (patrol_get_props(fd, &prop) < 0)
281 prop.op_mode = op_mode;
282 if (op_mode == MFI_PR_OPMODE_AUTO) {
284 prop.exec_freq = exec_freq;
287 mfi_get_time(fd, &at);
290 prop.next_exec = at + next_exec;
291 printf("Starting next patrol read at %s",
292 adapter_time(now, at, prop.next_exec));
295 if (mfi_dcmd_command(fd, MFI_DCMD_PR_SET_PROPERTIES, &prop,
296 sizeof(prop), NULL, 0, NULL) < 0) {
297 warn("Failed to set patrol read properties");
305 MFI_COMMAND(top, patrol, patrol_config);