mfi_patrol.c revision 222899
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: head/usr.sbin/mfiutil/mfi_patrol.c 222899 2011-06-09 19:52:28Z bz $
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
42static char *
43adapter_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
51static void
52mfi_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 = 0;
59	}
60}
61
62static int
63patrol_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
76static int
77show_patrol(int ac, 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[16];
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 %u",
178				    list->addr[i].device_id);
179				mfi_display_progress(label,
180				    &info.prog_info.patrol);
181			}
182		}
183		free(list);
184	}
185
186	close(fd);
187
188	return (0);
189}
190MFI_COMMAND(show, patrol, show_patrol);
191
192static int
193start_patrol(int ac, char **av)
194{
195	int error, fd;
196
197	fd = mfi_open(mfi_unit);
198	if (fd < 0) {
199		error = errno;
200		warn("mfi_open");
201		return (error);
202	}
203
204	if (mfi_dcmd_command(fd, MFI_DCMD_PR_START, NULL, 0, NULL, 0, NULL) <
205	    0) {
206		error = errno;
207		warn("Failed to start patrol read");
208		close(fd);
209		return (error);
210	}
211
212	close(fd);
213
214	return (0);
215}
216MFI_COMMAND(start, patrol, start_patrol);
217
218static int
219stop_patrol(int ac, char **av)
220{
221	int error, fd;
222
223	fd = mfi_open(mfi_unit);
224	if (fd < 0) {
225		error = errno;
226		warn("mfi_open");
227		return (error);
228	}
229
230	if (mfi_dcmd_command(fd, MFI_DCMD_PR_STOP, NULL, 0, NULL, 0, NULL) <
231	    0) {
232		error = errno;
233		warn("Failed to stop patrol read");
234		close(fd);
235		return (error);
236	}
237
238	close(fd);
239
240	return (0);
241}
242MFI_COMMAND(stop, patrol, stop_patrol);
243
244static int
245patrol_config(int ac, char **av)
246{
247	struct mfi_pr_properties prop;
248	long val;
249	time_t now;
250	int error, fd;
251	uint32_t at, next_exec, exec_freq;
252	char *cp;
253	uint8_t op_mode;
254
255	exec_freq = 0;	/* GCC too stupid */
256	next_exec = 0;
257	if (ac < 2) {
258		warnx("patrol: command required");
259		return (EINVAL);
260	}
261	if (strcasecmp(av[1], "auto") == 0) {
262		op_mode = MFI_PR_OPMODE_AUTO;
263		if (ac > 2) {
264			if (strcasecmp(av[2], "continuously") == 0)
265				exec_freq = 0xffffffff;
266			else {
267				val = strtol(av[2], &cp, 0);
268				if (*cp != '\0') {
269					warnx("patrol: Invalid interval %s",
270					    av[2]);
271					return (EINVAL);
272				}
273				exec_freq = val;
274			}
275		}
276		if (ac > 3) {
277			val = strtol(av[3], &cp, 0);
278			if (*cp != '\0' || val < 0) {
279				warnx("patrol: Invalid start time %s", av[3]);
280				return (EINVAL);
281			}
282			next_exec = val;
283		}
284	} else if (strcasecmp(av[1], "manual") == 0)
285		op_mode = MFI_PR_OPMODE_MANUAL;
286	else if (strcasecmp(av[1], "disable") == 0)
287		op_mode = MFI_PR_OPMODE_DISABLED;
288	else {
289		warnx("patrol: Invalid command %s", av[1]);
290		return (EINVAL);
291	}
292
293	fd = mfi_open(mfi_unit);
294	if (fd < 0) {
295		error = errno;
296		warn("mfi_open");
297		return (error);
298	}
299
300	error = patrol_get_props(fd, &prop);
301	if (error) {
302		close(fd);
303		return (error);
304	}
305	prop.op_mode = op_mode;
306	if (op_mode == MFI_PR_OPMODE_AUTO) {
307		if (ac > 2)
308			prop.exec_freq = exec_freq;
309		if (ac > 3) {
310			time(&now);
311			mfi_get_time(fd, &at);
312			if (at == 0) {
313				close(fd);
314				return (ENXIO);
315			}
316			prop.next_exec = at + next_exec;
317			printf("Starting next patrol read at %s",
318			    adapter_time(now, at, prop.next_exec));
319		}
320	}
321	if (mfi_dcmd_command(fd, MFI_DCMD_PR_SET_PROPERTIES, &prop,
322	    sizeof(prop), NULL, 0, NULL) < 0) {
323		error = errno;
324		warn("Failed to set patrol read properties");
325		close(fd);
326		return (error);
327	}
328
329	close(fd);
330
331	return (0);
332}
333MFI_COMMAND(top, patrol, patrol_config);
334