mfi_patrol.c revision 196200
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 196200 2009-08-13 23:18:45Z scottl $
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{
65196200Sscottl
66196200Sscottl	if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_PROPERTIES, prop,
67196200Sscottl	    sizeof(*prop), NULL, 0, NULL) < 0) {
68196200Sscottl		warn("Failed to get patrol read properties");
69196200Sscottl		return (-1);
70196200Sscottl	}
71196200Sscottl	return (0);
72196200Sscottl}
73196200Sscottl
74196200Sscottlstatic int
75196200Sscottlshow_patrol(int ac, char **av)
76196200Sscottl{
77196200Sscottl	struct mfi_pr_properties prop;
78196200Sscottl	struct mfi_pr_status status;
79196200Sscottl	struct mfi_pd_list *list;
80196200Sscottl	struct mfi_pd_info info;
81196200Sscottl	char label[16];
82196200Sscottl	time_t now;
83196200Sscottl	uint32_t at;
84196200Sscottl	int fd;
85196200Sscottl	u_int i;
86196200Sscottl
87196200Sscottl	fd = mfi_open(mfi_unit);
88196200Sscottl	if (fd < 0) {
89196200Sscottl		warn("mfi_open");
90196200Sscottl		return (errno);
91196200Sscottl	}
92196200Sscottl
93196200Sscottl	time(&now);
94196200Sscottl	mfi_get_time(fd, &at);
95196200Sscottl	if (patrol_get_props(fd, &prop) < 0)
96196200Sscottl		return (errno);
97196200Sscottl	printf("Operation Mode: ");
98196200Sscottl	switch (prop.op_mode) {
99196200Sscottl	case MFI_PR_OPMODE_AUTO:
100196200Sscottl		printf("auto\n");
101196200Sscottl		break;
102196200Sscottl	case MFI_PR_OPMODE_MANUAL:
103196200Sscottl		printf("manual\n");
104196200Sscottl		break;
105196200Sscottl	case MFI_PR_OPMODE_DISABLED:
106196200Sscottl		printf("disabled\n");
107196200Sscottl		break;
108196200Sscottl	default:
109196200Sscottl		printf("??? (%02x)\n", prop.op_mode);
110196200Sscottl		break;
111196200Sscottl	}
112196200Sscottl	if (prop.op_mode == MFI_PR_OPMODE_AUTO) {
113196200Sscottl		if (at != 0 && prop.next_exec)
114196200Sscottl			printf("    Next Run Starts: %s", adapter_time(now, at,
115196200Sscottl			    prop.next_exec));
116196200Sscottl		if (prop.exec_freq == 0xffffffff)
117196200Sscottl			printf("    Runs Execute Continuously\n");
118196200Sscottl		else if (prop.exec_freq != 0)
119196200Sscottl			printf("    Runs Start Every %u seconds\n",
120196200Sscottl			    prop.exec_freq);
121196200Sscottl	}
122196200Sscottl
123196200Sscottl	if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_STATUS, &status,
124196200Sscottl	    sizeof(status), NULL, 0, NULL) < 0) {
125196200Sscottl		warn("Failed to get patrol read properties");
126196200Sscottl		return (errno);
127196200Sscottl	}
128196200Sscottl	printf("Runs Completed: %u\n", status.num_iteration);
129196200Sscottl	printf("Current State: ");
130196200Sscottl	switch (status.state) {
131196200Sscottl	case MFI_PR_STATE_STOPPED:
132196200Sscottl		printf("stopped\n");
133196200Sscottl		break;
134196200Sscottl	case MFI_PR_STATE_READY:
135196200Sscottl		printf("ready\n");
136196200Sscottl		break;
137196200Sscottl	case MFI_PR_STATE_ACTIVE:
138196200Sscottl		printf("active\n");
139196200Sscottl		break;
140196200Sscottl	case MFI_PR_STATE_ABORTED:
141196200Sscottl		printf("aborted\n");
142196200Sscottl		break;
143196200Sscottl	default:
144196200Sscottl		printf("??? (%02x)\n", status.state);
145196200Sscottl		break;
146196200Sscottl	}
147196200Sscottl	if (status.state == MFI_PR_STATE_ACTIVE) {
148196200Sscottl		if (mfi_pd_get_list(fd, &list, NULL) < 0) {
149196200Sscottl			warn("Failed to get drive list");
150196200Sscottl			return (errno);
151196200Sscottl		}
152196200Sscottl
153196200Sscottl		for (i = 0; i < list->count; i++) {
154196200Sscottl			if (list->addr[i].scsi_dev_type != 0)
155196200Sscottl				continue;
156196200Sscottl
157196200Sscottl			if (mfi_pd_get_info(fd, list->addr[i].device_id, &info,
158196200Sscottl			    NULL) < 0) {
159196200Sscottl				warn("Failed to fetch info for drive %u",
160196200Sscottl				    list->addr[i].device_id);
161196200Sscottl				return (errno);
162196200Sscottl			}
163196200Sscottl			if (info.prog_info.active & MFI_PD_PROGRESS_PATROL) {
164196200Sscottl				snprintf(label, sizeof(label), "    Drive %u",
165196200Sscottl				    list->addr[i].device_id);
166196200Sscottl				mfi_display_progress(label,
167196200Sscottl				    &info.prog_info.patrol);
168196200Sscottl			}
169196200Sscottl		}
170196200Sscottl	}
171196200Sscottl
172196200Sscottl	close(fd);
173196200Sscottl
174196200Sscottl	return (0);
175196200Sscottl}
176196200SscottlMFI_COMMAND(show, patrol, show_patrol);
177196200Sscottl
178196200Sscottlstatic int
179196200Sscottlstart_patrol(int ac, char **av)
180196200Sscottl{
181196200Sscottl	int fd;
182196200Sscottl
183196200Sscottl	fd = mfi_open(mfi_unit);
184196200Sscottl	if (fd < 0) {
185196200Sscottl		warn("mfi_open");
186196200Sscottl		return (errno);
187196200Sscottl	}
188196200Sscottl
189196200Sscottl	if (mfi_dcmd_command(fd, MFI_DCMD_PR_START, NULL, 0, NULL, 0, NULL) <
190196200Sscottl	    0) {
191196200Sscottl		warn("Failed to start patrol read");
192196200Sscottl		return (errno);
193196200Sscottl	}
194196200Sscottl
195196200Sscottl	close(fd);
196196200Sscottl
197196200Sscottl	return (0);
198196200Sscottl}
199196200SscottlMFI_COMMAND(start, patrol, start_patrol);
200196200Sscottl
201196200Sscottlstatic int
202196200Sscottlstop_patrol(int ac, char **av)
203196200Sscottl{
204196200Sscottl	int fd;
205196200Sscottl
206196200Sscottl	fd = mfi_open(mfi_unit);
207196200Sscottl	if (fd < 0) {
208196200Sscottl		warn("mfi_open");
209196200Sscottl		return (errno);
210196200Sscottl	}
211196200Sscottl
212196200Sscottl	if (mfi_dcmd_command(fd, MFI_DCMD_PR_STOP, NULL, 0, NULL, 0, NULL) <
213196200Sscottl	    0) {
214196200Sscottl		warn("Failed to stop patrol read");
215196200Sscottl		return (errno);
216196200Sscottl	}
217196200Sscottl
218196200Sscottl	close(fd);
219196200Sscottl
220196200Sscottl	return (0);
221196200Sscottl}
222196200SscottlMFI_COMMAND(stop, patrol, stop_patrol);
223196200Sscottl
224196200Sscottlstatic int
225196200Sscottlpatrol_config(int ac, char **av)
226196200Sscottl{
227196200Sscottl	struct mfi_pr_properties prop;
228196200Sscottl	long val;
229196200Sscottl	time_t now;
230196200Sscottl	uint32_t at, next_exec, exec_freq;
231196200Sscottl	char *cp;
232196200Sscottl	uint8_t op_mode;
233196200Sscottl	int fd;
234196200Sscottl
235196200Sscottl	exec_freq = 0;	/* GCC too stupid */
236196200Sscottl	next_exec = 0;
237196200Sscottl	if (ac < 2) {
238196200Sscottl		warnx("patrol: command required");
239196200Sscottl		return (EINVAL);
240196200Sscottl	}
241196200Sscottl	if (strcasecmp(av[1], "auto") == 0) {
242196200Sscottl		op_mode = MFI_PR_OPMODE_AUTO;
243196200Sscottl		if (ac > 2) {
244196200Sscottl			if (strcasecmp(av[2], "continously") == 0)
245196200Sscottl				exec_freq = 0xffffffff;
246196200Sscottl			else {
247196200Sscottl				val = strtol(av[2], &cp, 0);
248196200Sscottl				if (*cp != '\0') {
249196200Sscottl					warnx("patrol: Invalid interval %s",
250196200Sscottl					    av[2]);
251196200Sscottl					return (EINVAL);
252196200Sscottl				}
253196200Sscottl				exec_freq = val;
254196200Sscottl			}
255196200Sscottl		}
256196200Sscottl		if (ac > 3) {
257196200Sscottl			val = strtol(av[3], &cp, 0);
258196200Sscottl			if (*cp != '\0' || val < 0) {
259196200Sscottl				warnx("patrol: Invalid start time %s", av[3]);
260196200Sscottl				return (EINVAL);
261196200Sscottl			}
262196200Sscottl			next_exec = val;
263196200Sscottl		}
264196200Sscottl	} else if (strcasecmp(av[1], "manual") == 0)
265196200Sscottl		op_mode = MFI_PR_OPMODE_MANUAL;
266196200Sscottl	else if (strcasecmp(av[1], "disable") == 0)
267196200Sscottl		op_mode = MFI_PR_OPMODE_DISABLED;
268196200Sscottl	else {
269196200Sscottl		warnx("patrol: Invalid command %s", av[1]);
270196200Sscottl		return (EINVAL);
271196200Sscottl	}
272196200Sscottl
273196200Sscottl	fd = mfi_open(mfi_unit);
274196200Sscottl	if (fd < 0) {
275196200Sscottl		warn("mfi_open");
276196200Sscottl		return (errno);
277196200Sscottl	}
278196200Sscottl
279196200Sscottl	if (patrol_get_props(fd, &prop) < 0)
280196200Sscottl		return (errno);
281196200Sscottl	prop.op_mode = op_mode;
282196200Sscottl	if (op_mode == MFI_PR_OPMODE_AUTO) {
283196200Sscottl		if (ac > 2)
284196200Sscottl			prop.exec_freq = exec_freq;
285196200Sscottl		if (ac > 3) {
286196200Sscottl			time(&now);
287196200Sscottl			mfi_get_time(fd, &at);
288196200Sscottl			if (at == 0)
289196200Sscottl				return (ENXIO);
290196200Sscottl			prop.next_exec = at + next_exec;
291196200Sscottl			printf("Starting next patrol read at %s",
292196200Sscottl			    adapter_time(now, at, prop.next_exec));
293196200Sscottl		}
294196200Sscottl	}
295196200Sscottl	if (mfi_dcmd_command(fd, MFI_DCMD_PR_SET_PROPERTIES, &prop,
296196200Sscottl	    sizeof(prop), NULL, 0, NULL) < 0) {
297196200Sscottl		warn("Failed to set patrol read properties");
298196200Sscottl		return (errno);
299196200Sscottl	}
300196200Sscottl
301196200Sscottl	close(fd);
302196200Sscottl
303196200Sscottl	return (0);
304196200Sscottl}
305196200SscottlMFI_COMMAND(top, patrol, patrol_config);
306