mfi_patrol.c revision 223345
1196200Sscottl/*-
2196200Sscottl * Copyright (c) 2008, 2009 Yahoo!, Inc.
3196200Sscottl * All rights reserved.
4196200Sscottl *
5196200Sscottl * Redistribution and use in source and binary forms, with or without
6196200Sscottl * modification, are permitted provided that the following conditions
7196200Sscottl * are met:
8196200Sscottl * 1. Redistributions of source code must retain the above copyright
9196200Sscottl *    notice, this list of conditions and the following disclaimer.
10196200Sscottl * 2. Redistributions in binary form must reproduce the above copyright
11196200Sscottl *    notice, this list of conditions and the following disclaimer in the
12196200Sscottl *    documentation and/or other materials provided with the distribution.
13196200Sscottl * 3. The names of the authors may not be used to endorse or promote
14196200Sscottl *    products derived from this software without specific prior written
15196200Sscottl *    permission.
16196200Sscottl *
17196200Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18196200Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19196200Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20196200Sscottl * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21196200Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22196200Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23196200Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24196200Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25196200Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26196200Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27196200Sscottl * SUCH DAMAGE.
28196200Sscottl *
29196200Sscottl * $FreeBSD: head/usr.sbin/mfiutil/mfi_patrol.c 223345 2011-06-20 21:28:50Z bz $
30196200Sscottl */
31196200Sscottl
32196200Sscottl#include <sys/types.h>
33196200Sscottl#include <sys/errno.h>
34196200Sscottl#include <err.h>
35196200Sscottl#include <stdio.h>
36196200Sscottl#include <stdlib.h>
37196200Sscottl#include <string.h>
38196200Sscottl#include <time.h>
39196200Sscottl#include <unistd.h>
40196200Sscottl#include "mfiutil.h"
41196200Sscottl
42196200Sscottlstatic char *
43196200Sscottladapter_time(time_t now, uint32_t at_now, uint32_t at)
44196200Sscottl{
45196200Sscottl	time_t t;
46196200Sscottl
47196200Sscottl	t = (now - at_now) + at;
48196200Sscottl	return (ctime(&t));
49196200Sscottl}
50196200Sscottl
51196200Sscottlstatic void
52196200Sscottlmfi_get_time(int fd, uint32_t *at)
53196200Sscottl{
54196200Sscottl
55196200Sscottl	if (mfi_dcmd_command(fd, MFI_DCMD_TIME_SECS_GET, at, sizeof(*at), NULL,
56196200Sscottl	    0, NULL) < 0) {
57196200Sscottl		warn("Couldn't fetch adapter time");
58196200Sscottl		at = 0;
59196200Sscottl	}
60196200Sscottl}
61196200Sscottl
62196200Sscottlstatic int
63196200Sscottlpatrol_get_props(int fd, struct mfi_pr_properties *prop)
64196200Sscottl{
65214396Sjhb	int error;
66196200Sscottl
67196200Sscottl	if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_PROPERTIES, prop,
68196200Sscottl	    sizeof(*prop), NULL, 0, NULL) < 0) {
69214396Sjhb		error = errno;
70196200Sscottl		warn("Failed to get patrol read properties");
71214396Sjhb		return (error);
72196200Sscottl	}
73196200Sscottl	return (0);
74196200Sscottl}
75196200Sscottl
76196200Sscottlstatic int
77196200Sscottlshow_patrol(int ac, char **av)
78196200Sscottl{
79196200Sscottl	struct mfi_pr_properties prop;
80196200Sscottl	struct mfi_pr_status status;
81196200Sscottl	struct mfi_pd_list *list;
82196200Sscottl	struct mfi_pd_info info;
83223345Sbz	char label[24];
84196200Sscottl	time_t now;
85196200Sscottl	uint32_t at;
86214396Sjhb	int error, fd;
87196200Sscottl	u_int i;
88196200Sscottl
89196200Sscottl	fd = mfi_open(mfi_unit);
90196200Sscottl	if (fd < 0) {
91214396Sjhb		error = errno;
92196200Sscottl		warn("mfi_open");
93214396Sjhb		return (error);
94196200Sscottl	}
95196200Sscottl
96196200Sscottl	time(&now);
97196200Sscottl	mfi_get_time(fd, &at);
98214396Sjhb	error = patrol_get_props(fd, &prop);
99222899Sbz	if (error) {
100222899Sbz		close(fd);
101214396Sjhb		return (error);
102222899Sbz	}
103196200Sscottl	printf("Operation Mode: ");
104196200Sscottl	switch (prop.op_mode) {
105196200Sscottl	case MFI_PR_OPMODE_AUTO:
106196200Sscottl		printf("auto\n");
107196200Sscottl		break;
108196200Sscottl	case MFI_PR_OPMODE_MANUAL:
109196200Sscottl		printf("manual\n");
110196200Sscottl		break;
111196200Sscottl	case MFI_PR_OPMODE_DISABLED:
112196200Sscottl		printf("disabled\n");
113196200Sscottl		break;
114196200Sscottl	default:
115196200Sscottl		printf("??? (%02x)\n", prop.op_mode);
116196200Sscottl		break;
117196200Sscottl	}
118196200Sscottl	if (prop.op_mode == MFI_PR_OPMODE_AUTO) {
119196200Sscottl		if (at != 0 && prop.next_exec)
120196200Sscottl			printf("    Next Run Starts: %s", adapter_time(now, at,
121196200Sscottl			    prop.next_exec));
122196200Sscottl		if (prop.exec_freq == 0xffffffff)
123196200Sscottl			printf("    Runs Execute Continuously\n");
124196200Sscottl		else if (prop.exec_freq != 0)
125196200Sscottl			printf("    Runs Start Every %u seconds\n",
126196200Sscottl			    prop.exec_freq);
127196200Sscottl	}
128196200Sscottl
129196200Sscottl	if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_STATUS, &status,
130196200Sscottl	    sizeof(status), NULL, 0, NULL) < 0) {
131214396Sjhb		error = errno;
132196200Sscottl		warn("Failed to get patrol read properties");
133222899Sbz		close(fd);
134214396Sjhb		return (error);
135196200Sscottl	}
136196200Sscottl	printf("Runs Completed: %u\n", status.num_iteration);
137196200Sscottl	printf("Current State: ");
138196200Sscottl	switch (status.state) {
139196200Sscottl	case MFI_PR_STATE_STOPPED:
140196200Sscottl		printf("stopped\n");
141196200Sscottl		break;
142196200Sscottl	case MFI_PR_STATE_READY:
143196200Sscottl		printf("ready\n");
144196200Sscottl		break;
145196200Sscottl	case MFI_PR_STATE_ACTIVE:
146196200Sscottl		printf("active\n");
147196200Sscottl		break;
148196200Sscottl	case MFI_PR_STATE_ABORTED:
149196200Sscottl		printf("aborted\n");
150196200Sscottl		break;
151196200Sscottl	default:
152196200Sscottl		printf("??? (%02x)\n", status.state);
153196200Sscottl		break;
154196200Sscottl	}
155196200Sscottl	if (status.state == MFI_PR_STATE_ACTIVE) {
156196200Sscottl		if (mfi_pd_get_list(fd, &list, NULL) < 0) {
157214396Sjhb			error = errno;
158196200Sscottl			warn("Failed to get drive list");
159222899Sbz			close(fd);
160214396Sjhb			return (error);
161196200Sscottl		}
162196200Sscottl
163196200Sscottl		for (i = 0; i < list->count; i++) {
164196200Sscottl			if (list->addr[i].scsi_dev_type != 0)
165196200Sscottl				continue;
166196200Sscottl
167196200Sscottl			if (mfi_pd_get_info(fd, list->addr[i].device_id, &info,
168196200Sscottl			    NULL) < 0) {
169214396Sjhb				error = errno;
170196200Sscottl				warn("Failed to fetch info for drive %u",
171196200Sscottl				    list->addr[i].device_id);
172222899Sbz				free(list);
173222899Sbz				close(fd);
174214396Sjhb				return (error);
175196200Sscottl			}
176196200Sscottl			if (info.prog_info.active & MFI_PD_PROGRESS_PATROL) {
177223345Sbz				snprintf(label, sizeof(label), "    Drive %s",
178223345Sbz				    mfi_drive_name(NULL,
179223345Sbz				    list->addr[i].device_id,
180223345Sbz				    MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
181196200Sscottl				mfi_display_progress(label,
182196200Sscottl				    &info.prog_info.patrol);
183196200Sscottl			}
184196200Sscottl		}
185222899Sbz		free(list);
186196200Sscottl	}
187196200Sscottl
188196200Sscottl	close(fd);
189196200Sscottl
190196200Sscottl	return (0);
191196200Sscottl}
192196200SscottlMFI_COMMAND(show, patrol, show_patrol);
193196200Sscottl
194196200Sscottlstatic int
195196200Sscottlstart_patrol(int ac, char **av)
196196200Sscottl{
197214396Sjhb	int error, fd;
198196200Sscottl
199196200Sscottl	fd = mfi_open(mfi_unit);
200196200Sscottl	if (fd < 0) {
201214396Sjhb		error = errno;
202196200Sscottl		warn("mfi_open");
203214396Sjhb		return (error);
204196200Sscottl	}
205196200Sscottl
206196200Sscottl	if (mfi_dcmd_command(fd, MFI_DCMD_PR_START, NULL, 0, NULL, 0, NULL) <
207196200Sscottl	    0) {
208214396Sjhb		error = errno;
209196200Sscottl		warn("Failed to start patrol read");
210222899Sbz		close(fd);
211214396Sjhb		return (error);
212196200Sscottl	}
213196200Sscottl
214196200Sscottl	close(fd);
215196200Sscottl
216196200Sscottl	return (0);
217196200Sscottl}
218196200SscottlMFI_COMMAND(start, patrol, start_patrol);
219196200Sscottl
220196200Sscottlstatic int
221196200Sscottlstop_patrol(int ac, char **av)
222196200Sscottl{
223214396Sjhb	int error, fd;
224196200Sscottl
225196200Sscottl	fd = mfi_open(mfi_unit);
226196200Sscottl	if (fd < 0) {
227214396Sjhb		error = errno;
228196200Sscottl		warn("mfi_open");
229214396Sjhb		return (error);
230196200Sscottl	}
231196200Sscottl
232196200Sscottl	if (mfi_dcmd_command(fd, MFI_DCMD_PR_STOP, NULL, 0, NULL, 0, NULL) <
233196200Sscottl	    0) {
234214396Sjhb		error = errno;
235196200Sscottl		warn("Failed to stop patrol read");
236222899Sbz		close(fd);
237214396Sjhb		return (error);
238196200Sscottl	}
239196200Sscottl
240196200Sscottl	close(fd);
241196200Sscottl
242196200Sscottl	return (0);
243196200Sscottl}
244196200SscottlMFI_COMMAND(stop, patrol, stop_patrol);
245196200Sscottl
246196200Sscottlstatic int
247196200Sscottlpatrol_config(int ac, char **av)
248196200Sscottl{
249196200Sscottl	struct mfi_pr_properties prop;
250196200Sscottl	long val;
251196200Sscottl	time_t now;
252214396Sjhb	int error, fd;
253196200Sscottl	uint32_t at, next_exec, exec_freq;
254196200Sscottl	char *cp;
255196200Sscottl	uint8_t op_mode;
256196200Sscottl
257196200Sscottl	exec_freq = 0;	/* GCC too stupid */
258196200Sscottl	next_exec = 0;
259196200Sscottl	if (ac < 2) {
260196200Sscottl		warnx("patrol: command required");
261196200Sscottl		return (EINVAL);
262196200Sscottl	}
263196200Sscottl	if (strcasecmp(av[1], "auto") == 0) {
264196200Sscottl		op_mode = MFI_PR_OPMODE_AUTO;
265196200Sscottl		if (ac > 2) {
266221119Spluknet			if (strcasecmp(av[2], "continuously") == 0)
267196200Sscottl				exec_freq = 0xffffffff;
268196200Sscottl			else {
269196200Sscottl				val = strtol(av[2], &cp, 0);
270196200Sscottl				if (*cp != '\0') {
271196200Sscottl					warnx("patrol: Invalid interval %s",
272196200Sscottl					    av[2]);
273196200Sscottl					return (EINVAL);
274196200Sscottl				}
275196200Sscottl				exec_freq = val;
276196200Sscottl			}
277196200Sscottl		}
278196200Sscottl		if (ac > 3) {
279196200Sscottl			val = strtol(av[3], &cp, 0);
280196200Sscottl			if (*cp != '\0' || val < 0) {
281196200Sscottl				warnx("patrol: Invalid start time %s", av[3]);
282196200Sscottl				return (EINVAL);
283196200Sscottl			}
284196200Sscottl			next_exec = val;
285196200Sscottl		}
286196200Sscottl	} else if (strcasecmp(av[1], "manual") == 0)
287196200Sscottl		op_mode = MFI_PR_OPMODE_MANUAL;
288196200Sscottl	else if (strcasecmp(av[1], "disable") == 0)
289196200Sscottl		op_mode = MFI_PR_OPMODE_DISABLED;
290196200Sscottl	else {
291196200Sscottl		warnx("patrol: Invalid command %s", av[1]);
292196200Sscottl		return (EINVAL);
293196200Sscottl	}
294196200Sscottl
295196200Sscottl	fd = mfi_open(mfi_unit);
296196200Sscottl	if (fd < 0) {
297214396Sjhb		error = errno;
298196200Sscottl		warn("mfi_open");
299214396Sjhb		return (error);
300196200Sscottl	}
301196200Sscottl
302214396Sjhb	error = patrol_get_props(fd, &prop);
303222899Sbz	if (error) {
304222899Sbz		close(fd);
305214396Sjhb		return (error);
306222899Sbz	}
307196200Sscottl	prop.op_mode = op_mode;
308196200Sscottl	if (op_mode == MFI_PR_OPMODE_AUTO) {
309196200Sscottl		if (ac > 2)
310196200Sscottl			prop.exec_freq = exec_freq;
311196200Sscottl		if (ac > 3) {
312196200Sscottl			time(&now);
313196200Sscottl			mfi_get_time(fd, &at);
314222899Sbz			if (at == 0) {
315222899Sbz				close(fd);
316196200Sscottl				return (ENXIO);
317222899Sbz			}
318196200Sscottl			prop.next_exec = at + next_exec;
319196200Sscottl			printf("Starting next patrol read at %s",
320196200Sscottl			    adapter_time(now, at, prop.next_exec));
321196200Sscottl		}
322196200Sscottl	}
323196200Sscottl	if (mfi_dcmd_command(fd, MFI_DCMD_PR_SET_PROPERTIES, &prop,
324196200Sscottl	    sizeof(prop), NULL, 0, NULL) < 0) {
325214396Sjhb		error = errno;
326196200Sscottl		warn("Failed to set patrol read properties");
327222899Sbz		close(fd);
328214396Sjhb		return (error);
329196200Sscottl	}
330196200Sscottl
331196200Sscottl	close(fd);
332196200Sscottl
333196200Sscottl	return (0);
334196200Sscottl}
335196200SscottlMFI_COMMAND(top, patrol, patrol_config);
336