xref: /freebsd/usr.sbin/mfiutil/mfi_patrol.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2008, 2009 Yahoo!, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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
17  *    permission.
18  *
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
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD$
32  */
33 
34 #include <sys/types.h>
35 #include <sys/errno.h>
36 #include <err.h>
37 #include <fcntl.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <time.h>
42 #include <unistd.h>
43 #include "mfiutil.h"
44 
45 static char *
46 adapter_time(time_t now, uint32_t at_now, uint32_t at)
47 {
48 	time_t t;
49 
50 	t = (now - at_now) + at;
51 	return (ctime(&t));
52 }
53 
54 static void
55 mfi_get_time(int fd, uint32_t *at)
56 {
57 
58 	if (mfi_dcmd_command(fd, MFI_DCMD_TIME_SECS_GET, at, sizeof(*at), NULL,
59 	    0, NULL) < 0) {
60 		warn("Couldn't fetch adapter time");
61 		at = 0;
62 	}
63 }
64 
65 static int
66 patrol_get_props(int fd, struct mfi_pr_properties *prop)
67 {
68 	int error;
69 
70 	if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_PROPERTIES, prop,
71 	    sizeof(*prop), NULL, 0, NULL) < 0) {
72 		error = errno;
73 		warn("Failed to get patrol read properties");
74 		return (error);
75 	}
76 	return (0);
77 }
78 
79 static int
80 show_patrol(int ac __unused, char **av __unused)
81 {
82 	struct mfi_pr_properties prop;
83 	struct mfi_pr_status status;
84 	struct mfi_pd_list *list;
85 	struct mfi_pd_info info;
86 	char label[24];
87 	time_t now;
88 	uint32_t at;
89 	int error, fd;
90 	u_int i;
91 
92 	fd = mfi_open(mfi_unit, O_RDWR);
93 	if (fd < 0) {
94 		error = errno;
95 		warn("mfi_open");
96 		return (error);
97 	}
98 
99 	time(&now);
100 	mfi_get_time(fd, &at);
101 	error = patrol_get_props(fd, &prop);
102 	if (error) {
103 		close(fd);
104 		return (error);
105 	}
106 	printf("Operation Mode: ");
107 	switch (prop.op_mode) {
108 	case MFI_PR_OPMODE_AUTO:
109 		printf("auto\n");
110 		break;
111 	case MFI_PR_OPMODE_MANUAL:
112 		printf("manual\n");
113 		break;
114 	case MFI_PR_OPMODE_DISABLED:
115 		printf("disabled\n");
116 		break;
117 	default:
118 		printf("??? (%02x)\n", prop.op_mode);
119 		break;
120 	}
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,
124 			    prop.next_exec));
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",
129 			    prop.exec_freq);
130 	}
131 
132 	if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_STATUS, &status,
133 	    sizeof(status), NULL, 0, NULL) < 0) {
134 		error = errno;
135 		warn("Failed to get patrol read properties");
136 		close(fd);
137 		return (error);
138 	}
139 	printf("Runs Completed: %u\n", status.num_iteration);
140 	printf("Current State: ");
141 	switch (status.state) {
142 	case MFI_PR_STATE_STOPPED:
143 		printf("stopped\n");
144 		break;
145 	case MFI_PR_STATE_READY:
146 		printf("ready\n");
147 		break;
148 	case MFI_PR_STATE_ACTIVE:
149 		printf("active\n");
150 		break;
151 	case MFI_PR_STATE_ABORTED:
152 		printf("aborted\n");
153 		break;
154 	default:
155 		printf("??? (%02x)\n", status.state);
156 		break;
157 	}
158 	if (status.state == MFI_PR_STATE_ACTIVE) {
159 		if (mfi_pd_get_list(fd, &list, NULL) < 0) {
160 			error = errno;
161 			warn("Failed to get drive list");
162 			close(fd);
163 			return (error);
164 		}
165 
166 		for (i = 0; i < list->count; i++) {
167 			if (list->addr[i].scsi_dev_type != 0)
168 				continue;
169 
170 			if (mfi_pd_get_info(fd, list->addr[i].device_id, &info,
171 			    NULL) < 0) {
172 				error = errno;
173 				warn("Failed to fetch info for drive %u",
174 				    list->addr[i].device_id);
175 				free(list);
176 				close(fd);
177 				return (error);
178 			}
179 			if (info.prog_info.active & MFI_PD_PROGRESS_PATROL) {
180 				snprintf(label, sizeof(label), "    Drive %s",
181 				    mfi_drive_name(NULL,
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);
186 			}
187 		}
188 		free(list);
189 	}
190 
191 	close(fd);
192 
193 	return (0);
194 }
195 MFI_COMMAND(show, patrol, show_patrol);
196 
197 static int
198 start_patrol(int ac __unused, char **av __unused)
199 {
200 	int error, fd;
201 
202 	fd = mfi_open(mfi_unit, O_RDWR);
203 	if (fd < 0) {
204 		error = errno;
205 		warn("mfi_open");
206 		return (error);
207 	}
208 
209 	if (mfi_dcmd_command(fd, MFI_DCMD_PR_START, NULL, 0, NULL, 0, NULL) <
210 	    0) {
211 		error = errno;
212 		warn("Failed to start patrol read");
213 		close(fd);
214 		return (error);
215 	}
216 
217 	close(fd);
218 
219 	return (0);
220 }
221 MFI_COMMAND(start, patrol, start_patrol);
222 
223 static int
224 stop_patrol(int ac __unused, char **av __unused)
225 {
226 	int error, fd;
227 
228 	fd = mfi_open(mfi_unit, O_RDWR);
229 	if (fd < 0) {
230 		error = errno;
231 		warn("mfi_open");
232 		return (error);
233 	}
234 
235 	if (mfi_dcmd_command(fd, MFI_DCMD_PR_STOP, NULL, 0, NULL, 0, NULL) <
236 	    0) {
237 		error = errno;
238 		warn("Failed to stop patrol read");
239 		close(fd);
240 		return (error);
241 	}
242 
243 	close(fd);
244 
245 	return (0);
246 }
247 MFI_COMMAND(stop, patrol, stop_patrol);
248 
249 static int
250 patrol_config(int ac, char **av)
251 {
252 	struct mfi_pr_properties prop;
253 	long val;
254 	time_t now;
255 	int error, fd;
256 	uint32_t at, next_exec, exec_freq;
257 	char *cp;
258 	uint8_t op_mode;
259 
260 	exec_freq = 0;	/* GCC too stupid */
261 	next_exec = 0;
262 	if (ac < 2) {
263 		warnx("patrol: command required");
264 		return (EINVAL);
265 	}
266 	if (strcasecmp(av[1], "auto") == 0) {
267 		op_mode = MFI_PR_OPMODE_AUTO;
268 		if (ac > 2) {
269 			if (strcasecmp(av[2], "continuously") == 0)
270 				exec_freq = 0xffffffff;
271 			else {
272 				val = strtol(av[2], &cp, 0);
273 				if (*cp != '\0') {
274 					warnx("patrol: Invalid interval %s",
275 					    av[2]);
276 					return (EINVAL);
277 				}
278 				exec_freq = val;
279 			}
280 		}
281 		if (ac > 3) {
282 			val = strtol(av[3], &cp, 0);
283 			if (*cp != '\0' || val < 0) {
284 				warnx("patrol: Invalid start time %s", av[3]);
285 				return (EINVAL);
286 			}
287 			next_exec = val;
288 		}
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;
293 	else {
294 		warnx("patrol: Invalid command %s", av[1]);
295 		return (EINVAL);
296 	}
297 
298 	fd = mfi_open(mfi_unit, O_RDWR);
299 	if (fd < 0) {
300 		error = errno;
301 		warn("mfi_open");
302 		return (error);
303 	}
304 
305 	error = patrol_get_props(fd, &prop);
306 	if (error) {
307 		close(fd);
308 		return (error);
309 	}
310 	prop.op_mode = op_mode;
311 	if (op_mode == MFI_PR_OPMODE_AUTO) {
312 		if (ac > 2)
313 			prop.exec_freq = exec_freq;
314 		if (ac > 3) {
315 			time(&now);
316 			mfi_get_time(fd, &at);
317 			if (at == 0) {
318 				close(fd);
319 				return (ENXIO);
320 			}
321 			prop.next_exec = at + next_exec;
322 			printf("Starting next patrol read at %s",
323 			    adapter_time(now, at, prop.next_exec));
324 		}
325 	}
326 	if (mfi_dcmd_command(fd, MFI_DCMD_PR_SET_PROPERTIES, &prop,
327 	    sizeof(prop), NULL, 0, NULL) < 0) {
328 		error = errno;
329 		warn("Failed to set patrol read properties");
330 		close(fd);
331 		return (error);
332 	}
333 
334 	close(fd);
335 
336 	return (0);
337 }
338 MFI_COMMAND(top, patrol, patrol_config);
339