2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2008, 2009 Yahoo!, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the authors may not be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/types.h>
35 #include <sys/errno.h>
46 adapter_time(time_t now, uint32_t at_now, uint32_t at)
50 t = (now - at_now) + at;
55 mfi_get_time(int fd, uint32_t *at)
58 if (mfi_dcmd_command(fd, MFI_DCMD_TIME_SECS_GET, at, sizeof(*at), NULL,
60 warn("Couldn't fetch adapter time");
66 patrol_get_props(int fd, struct mfi_pr_properties *prop)
70 if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_PROPERTIES, prop,
71 sizeof(*prop), NULL, 0, NULL) < 0) {
73 warn("Failed to get patrol read properties");
80 show_patrol(int ac __unused, char **av __unused)
82 struct mfi_pr_properties prop;
83 struct mfi_pr_status status;
84 struct mfi_pd_list *list;
85 struct mfi_pd_info info;
92 fd = mfi_open(mfi_unit, O_RDWR);
100 mfi_get_time(fd, &at);
101 error = patrol_get_props(fd, &prop);
106 printf("Operation Mode: ");
107 switch (prop.op_mode) {
108 case MFI_PR_OPMODE_AUTO:
111 case MFI_PR_OPMODE_MANUAL:
114 case MFI_PR_OPMODE_DISABLED:
115 printf("disabled\n");
118 printf("??? (%02x)\n", prop.op_mode);
121 if (prop.op_mode == MFI_PR_OPMODE_AUTO) {
122 if (at != 0 && prop.next_exec)
123 printf(" Next Run Starts: %s", adapter_time(now, at,
125 if (prop.exec_freq == 0xffffffff)
126 printf(" Runs Execute Continuously\n");
127 else if (prop.exec_freq != 0)
128 printf(" Runs Start Every %u seconds\n",
132 if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_STATUS, &status,
133 sizeof(status), NULL, 0, NULL) < 0) {
135 warn("Failed to get patrol read properties");
139 printf("Runs Completed: %u\n", status.num_iteration);
140 printf("Current State: ");
141 switch (status.state) {
142 case MFI_PR_STATE_STOPPED:
145 case MFI_PR_STATE_READY:
148 case MFI_PR_STATE_ACTIVE:
151 case MFI_PR_STATE_ABORTED:
155 printf("??? (%02x)\n", status.state);
158 if (status.state == MFI_PR_STATE_ACTIVE) {
159 if (mfi_pd_get_list(fd, &list, NULL) < 0) {
161 warn("Failed to get drive list");
166 for (i = 0; i < list->count; i++) {
167 if (list->addr[i].scsi_dev_type != 0)
170 if (mfi_pd_get_info(fd, list->addr[i].device_id, &info,
173 warn("Failed to fetch info for drive %u",
174 list->addr[i].device_id);
179 if (info.prog_info.active & MFI_PD_PROGRESS_PATROL) {
180 snprintf(label, sizeof(label), " Drive %s",
182 list->addr[i].device_id,
183 MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
184 mfi_display_progress(label,
185 &info.prog_info.patrol);
195 MFI_COMMAND(show, patrol, show_patrol);
198 start_patrol(int ac __unused, char **av __unused)
202 fd = mfi_open(mfi_unit, O_RDWR);
209 if (mfi_dcmd_command(fd, MFI_DCMD_PR_START, NULL, 0, NULL, 0, NULL) <
212 warn("Failed to start patrol read");
221 MFI_COMMAND(start, patrol, start_patrol);
224 stop_patrol(int ac __unused, char **av __unused)
228 fd = mfi_open(mfi_unit, O_RDWR);
235 if (mfi_dcmd_command(fd, MFI_DCMD_PR_STOP, NULL, 0, NULL, 0, NULL) <
238 warn("Failed to stop patrol read");
247 MFI_COMMAND(stop, patrol, stop_patrol);
250 patrol_config(int ac, char **av)
252 struct mfi_pr_properties prop;
256 uint32_t at, next_exec, exec_freq;
260 exec_freq = 0; /* GCC too stupid */
263 warnx("patrol: command required");
266 if (strcasecmp(av[1], "auto") == 0) {
267 op_mode = MFI_PR_OPMODE_AUTO;
269 if (strcasecmp(av[2], "continuously") == 0)
270 exec_freq = 0xffffffff;
272 val = strtol(av[2], &cp, 0);
274 warnx("patrol: Invalid interval %s",
282 val = strtol(av[3], &cp, 0);
283 if (*cp != '\0' || val < 0) {
284 warnx("patrol: Invalid start time %s", av[3]);
289 } else if (strcasecmp(av[1], "manual") == 0)
290 op_mode = MFI_PR_OPMODE_MANUAL;
291 else if (strcasecmp(av[1], "disable") == 0)
292 op_mode = MFI_PR_OPMODE_DISABLED;
294 warnx("patrol: Invalid command %s", av[1]);
298 fd = mfi_open(mfi_unit, O_RDWR);
305 error = patrol_get_props(fd, &prop);
310 prop.op_mode = op_mode;
311 if (op_mode == MFI_PR_OPMODE_AUTO) {
313 prop.exec_freq = exec_freq;
316 mfi_get_time(fd, &at);
321 prop.next_exec = at + next_exec;
322 printf("Starting next patrol read at %s",
323 adapter_time(now, at, prop.next_exec));
326 if (mfi_dcmd_command(fd, MFI_DCMD_PR_SET_PROPERTIES, &prop,
327 sizeof(prop), NULL, 0, NULL) < 0) {
329 warn("Failed to set patrol read properties");
338 MFI_COMMAND(top, patrol, patrol_config);