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