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$
30196200Sscottl */
31196200Sscottl
32196200Sscottl#include <sys/types.h>
33196200Sscottl#include <sys/errno.h>
34196200Sscottl#include <err.h>
35237589Seadler#include <fcntl.h>
36196200Sscottl#include <stdio.h>
37196200Sscottl#include <stdlib.h>
38196200Sscottl#include <string.h>
39196200Sscottl#include <time.h>
40196200Sscottl#include <unistd.h>
41196200Sscottl#include "mfiutil.h"
42196200Sscottl
43196200Sscottlstatic char *
44196200Sscottladapter_time(time_t now, uint32_t at_now, uint32_t at)
45196200Sscottl{
46196200Sscottl	time_t t;
47196200Sscottl
48196200Sscottl	t = (now - at_now) + at;
49196200Sscottl	return (ctime(&t));
50196200Sscottl}
51196200Sscottl
52196200Sscottlstatic void
53196200Sscottlmfi_get_time(int fd, uint32_t *at)
54196200Sscottl{
55196200Sscottl
56196200Sscottl	if (mfi_dcmd_command(fd, MFI_DCMD_TIME_SECS_GET, at, sizeof(*at), NULL,
57196200Sscottl	    0, NULL) < 0) {
58196200Sscottl		warn("Couldn't fetch adapter time");
59196200Sscottl		at = 0;
60196200Sscottl	}
61196200Sscottl}
62196200Sscottl
63196200Sscottlstatic int
64196200Sscottlpatrol_get_props(int fd, struct mfi_pr_properties *prop)
65196200Sscottl{
66214396Sjhb	int error;
67196200Sscottl
68196200Sscottl	if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_PROPERTIES, prop,
69196200Sscottl	    sizeof(*prop), NULL, 0, NULL) < 0) {
70214396Sjhb		error = errno;
71196200Sscottl		warn("Failed to get patrol read properties");
72214396Sjhb		return (error);
73196200Sscottl	}
74196200Sscottl	return (0);
75196200Sscottl}
76196200Sscottl
77196200Sscottlstatic int
78237589Seadlershow_patrol(int ac __unused, char **av __unused)
79196200Sscottl{
80196200Sscottl	struct mfi_pr_properties prop;
81196200Sscottl	struct mfi_pr_status status;
82196200Sscottl	struct mfi_pd_list *list;
83196200Sscottl	struct mfi_pd_info info;
84223345Sbz	char label[24];
85196200Sscottl	time_t now;
86196200Sscottl	uint32_t at;
87214396Sjhb	int error, fd;
88196200Sscottl	u_int i;
89196200Sscottl
90237589Seadler	fd = mfi_open(mfi_unit, O_RDWR);
91196200Sscottl	if (fd < 0) {
92214396Sjhb		error = errno;
93196200Sscottl		warn("mfi_open");
94214396Sjhb		return (error);
95196200Sscottl	}
96196200Sscottl
97196200Sscottl	time(&now);
98196200Sscottl	mfi_get_time(fd, &at);
99214396Sjhb	error = patrol_get_props(fd, &prop);
100222899Sbz	if (error) {
101222899Sbz		close(fd);
102214396Sjhb		return (error);
103222899Sbz	}
104196200Sscottl	printf("Operation Mode: ");
105196200Sscottl	switch (prop.op_mode) {
106196200Sscottl	case MFI_PR_OPMODE_AUTO:
107196200Sscottl		printf("auto\n");
108196200Sscottl		break;
109196200Sscottl	case MFI_PR_OPMODE_MANUAL:
110196200Sscottl		printf("manual\n");
111196200Sscottl		break;
112196200Sscottl	case MFI_PR_OPMODE_DISABLED:
113196200Sscottl		printf("disabled\n");
114196200Sscottl		break;
115196200Sscottl	default:
116196200Sscottl		printf("??? (%02x)\n", prop.op_mode);
117196200Sscottl		break;
118196200Sscottl	}
119196200Sscottl	if (prop.op_mode == MFI_PR_OPMODE_AUTO) {
120196200Sscottl		if (at != 0 && prop.next_exec)
121196200Sscottl			printf("    Next Run Starts: %s", adapter_time(now, at,
122196200Sscottl			    prop.next_exec));
123196200Sscottl		if (prop.exec_freq == 0xffffffff)
124196200Sscottl			printf("    Runs Execute Continuously\n");
125196200Sscottl		else if (prop.exec_freq != 0)
126196200Sscottl			printf("    Runs Start Every %u seconds\n",
127196200Sscottl			    prop.exec_freq);
128196200Sscottl	}
129196200Sscottl
130196200Sscottl	if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_STATUS, &status,
131196200Sscottl	    sizeof(status), NULL, 0, NULL) < 0) {
132214396Sjhb		error = errno;
133196200Sscottl		warn("Failed to get patrol read properties");
134222899Sbz		close(fd);
135214396Sjhb		return (error);
136196200Sscottl	}
137196200Sscottl	printf("Runs Completed: %u\n", status.num_iteration);
138196200Sscottl	printf("Current State: ");
139196200Sscottl	switch (status.state) {
140196200Sscottl	case MFI_PR_STATE_STOPPED:
141196200Sscottl		printf("stopped\n");
142196200Sscottl		break;
143196200Sscottl	case MFI_PR_STATE_READY:
144196200Sscottl		printf("ready\n");
145196200Sscottl		break;
146196200Sscottl	case MFI_PR_STATE_ACTIVE:
147196200Sscottl		printf("active\n");
148196200Sscottl		break;
149196200Sscottl	case MFI_PR_STATE_ABORTED:
150196200Sscottl		printf("aborted\n");
151196200Sscottl		break;
152196200Sscottl	default:
153196200Sscottl		printf("??? (%02x)\n", status.state);
154196200Sscottl		break;
155196200Sscottl	}
156196200Sscottl	if (status.state == MFI_PR_STATE_ACTIVE) {
157196200Sscottl		if (mfi_pd_get_list(fd, &list, NULL) < 0) {
158214396Sjhb			error = errno;
159196200Sscottl			warn("Failed to get drive list");
160222899Sbz			close(fd);
161214396Sjhb			return (error);
162196200Sscottl		}
163196200Sscottl
164196200Sscottl		for (i = 0; i < list->count; i++) {
165196200Sscottl			if (list->addr[i].scsi_dev_type != 0)
166196200Sscottl				continue;
167196200Sscottl
168196200Sscottl			if (mfi_pd_get_info(fd, list->addr[i].device_id, &info,
169196200Sscottl			    NULL) < 0) {
170214396Sjhb				error = errno;
171196200Sscottl				warn("Failed to fetch info for drive %u",
172196200Sscottl				    list->addr[i].device_id);
173222899Sbz				free(list);
174222899Sbz				close(fd);
175214396Sjhb				return (error);
176196200Sscottl			}
177196200Sscottl			if (info.prog_info.active & MFI_PD_PROGRESS_PATROL) {
178223345Sbz				snprintf(label, sizeof(label), "    Drive %s",
179223345Sbz				    mfi_drive_name(NULL,
180223345Sbz				    list->addr[i].device_id,
181223345Sbz				    MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
182196200Sscottl				mfi_display_progress(label,
183196200Sscottl				    &info.prog_info.patrol);
184196200Sscottl			}
185196200Sscottl		}
186222899Sbz		free(list);
187196200Sscottl	}
188196200Sscottl
189196200Sscottl	close(fd);
190196200Sscottl
191196200Sscottl	return (0);
192196200Sscottl}
193196200SscottlMFI_COMMAND(show, patrol, show_patrol);
194196200Sscottl
195196200Sscottlstatic int
196237589Seadlerstart_patrol(int ac __unused, char **av __unused)
197196200Sscottl{
198214396Sjhb	int error, fd;
199196200Sscottl
200237589Seadler	fd = mfi_open(mfi_unit, O_RDWR);
201196200Sscottl	if (fd < 0) {
202214396Sjhb		error = errno;
203196200Sscottl		warn("mfi_open");
204214396Sjhb		return (error);
205196200Sscottl	}
206196200Sscottl
207196200Sscottl	if (mfi_dcmd_command(fd, MFI_DCMD_PR_START, NULL, 0, NULL, 0, NULL) <
208196200Sscottl	    0) {
209214396Sjhb		error = errno;
210196200Sscottl		warn("Failed to start patrol read");
211222899Sbz		close(fd);
212214396Sjhb		return (error);
213196200Sscottl	}
214196200Sscottl
215196200Sscottl	close(fd);
216196200Sscottl
217196200Sscottl	return (0);
218196200Sscottl}
219196200SscottlMFI_COMMAND(start, patrol, start_patrol);
220196200Sscottl
221196200Sscottlstatic int
222237589Seadlerstop_patrol(int ac __unused, char **av __unused)
223196200Sscottl{
224214396Sjhb	int error, fd;
225196200Sscottl
226237589Seadler	fd = mfi_open(mfi_unit, O_RDWR);
227196200Sscottl	if (fd < 0) {
228214396Sjhb		error = errno;
229196200Sscottl		warn("mfi_open");
230214396Sjhb		return (error);
231196200Sscottl	}
232196200Sscottl
233196200Sscottl	if (mfi_dcmd_command(fd, MFI_DCMD_PR_STOP, NULL, 0, NULL, 0, NULL) <
234196200Sscottl	    0) {
235214396Sjhb		error = errno;
236196200Sscottl		warn("Failed to stop patrol read");
237222899Sbz		close(fd);
238214396Sjhb		return (error);
239196200Sscottl	}
240196200Sscottl
241196200Sscottl	close(fd);
242196200Sscottl
243196200Sscottl	return (0);
244196200Sscottl}
245196200SscottlMFI_COMMAND(stop, patrol, stop_patrol);
246196200Sscottl
247196200Sscottlstatic int
248196200Sscottlpatrol_config(int ac, char **av)
249196200Sscottl{
250196200Sscottl	struct mfi_pr_properties prop;
251196200Sscottl	long val;
252196200Sscottl	time_t now;
253214396Sjhb	int error, fd;
254196200Sscottl	uint32_t at, next_exec, exec_freq;
255196200Sscottl	char *cp;
256196200Sscottl	uint8_t op_mode;
257196200Sscottl
258196200Sscottl	exec_freq = 0;	/* GCC too stupid */
259196200Sscottl	next_exec = 0;
260196200Sscottl	if (ac < 2) {
261196200Sscottl		warnx("patrol: command required");
262196200Sscottl		return (EINVAL);
263196200Sscottl	}
264196200Sscottl	if (strcasecmp(av[1], "auto") == 0) {
265196200Sscottl		op_mode = MFI_PR_OPMODE_AUTO;
266196200Sscottl		if (ac > 2) {
267221119Spluknet			if (strcasecmp(av[2], "continuously") == 0)
268196200Sscottl				exec_freq = 0xffffffff;
269196200Sscottl			else {
270196200Sscottl				val = strtol(av[2], &cp, 0);
271196200Sscottl				if (*cp != '\0') {
272196200Sscottl					warnx("patrol: Invalid interval %s",
273196200Sscottl					    av[2]);
274196200Sscottl					return (EINVAL);
275196200Sscottl				}
276196200Sscottl				exec_freq = val;
277196200Sscottl			}
278196200Sscottl		}
279196200Sscottl		if (ac > 3) {
280196200Sscottl			val = strtol(av[3], &cp, 0);
281196200Sscottl			if (*cp != '\0' || val < 0) {
282196200Sscottl				warnx("patrol: Invalid start time %s", av[3]);
283196200Sscottl				return (EINVAL);
284196200Sscottl			}
285196200Sscottl			next_exec = val;
286196200Sscottl		}
287196200Sscottl	} else if (strcasecmp(av[1], "manual") == 0)
288196200Sscottl		op_mode = MFI_PR_OPMODE_MANUAL;
289196200Sscottl	else if (strcasecmp(av[1], "disable") == 0)
290196200Sscottl		op_mode = MFI_PR_OPMODE_DISABLED;
291196200Sscottl	else {
292196200Sscottl		warnx("patrol: Invalid command %s", av[1]);
293196200Sscottl		return (EINVAL);
294196200Sscottl	}
295196200Sscottl
296237589Seadler	fd = mfi_open(mfi_unit, O_RDWR);
297196200Sscottl	if (fd < 0) {
298214396Sjhb		error = errno;
299196200Sscottl		warn("mfi_open");
300214396Sjhb		return (error);
301196200Sscottl	}
302196200Sscottl
303214396Sjhb	error = patrol_get_props(fd, &prop);
304222899Sbz	if (error) {
305222899Sbz		close(fd);
306214396Sjhb		return (error);
307222899Sbz	}
308196200Sscottl	prop.op_mode = op_mode;
309196200Sscottl	if (op_mode == MFI_PR_OPMODE_AUTO) {
310196200Sscottl		if (ac > 2)
311196200Sscottl			prop.exec_freq = exec_freq;
312196200Sscottl		if (ac > 3) {
313196200Sscottl			time(&now);
314196200Sscottl			mfi_get_time(fd, &at);
315222899Sbz			if (at == 0) {
316222899Sbz				close(fd);
317196200Sscottl				return (ENXIO);
318222899Sbz			}
319196200Sscottl			prop.next_exec = at + next_exec;
320196200Sscottl			printf("Starting next patrol read at %s",
321196200Sscottl			    adapter_time(now, at, prop.next_exec));
322196200Sscottl		}
323196200Sscottl	}
324196200Sscottl	if (mfi_dcmd_command(fd, MFI_DCMD_PR_SET_PROPERTIES, &prop,
325196200Sscottl	    sizeof(prop), NULL, 0, NULL) < 0) {
326214396Sjhb		error = errno;
327196200Sscottl		warn("Failed to set patrol read properties");
328222899Sbz		close(fd);
329214396Sjhb		return (error);
330196200Sscottl	}
331196200Sscottl
332196200Sscottl	close(fd);
333196200Sscottl
334196200Sscottl	return (0);
335196200Sscottl}
336196200SscottlMFI_COMMAND(top, patrol, patrol_config);
337