mfi_patrol.c revision 237589
1144564Simp/*-
21558Srgrimes * Copyright (c) 2008, 2009 Yahoo!, Inc.
31558Srgrimes * All rights reserved.
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer in the
121558Srgrimes *    documentation and/or other materials provided with the distribution.
131558Srgrimes * 3. The names of the authors may not be used to endorse or promote
141558Srgrimes *    products derived from this software without specific prior written
151558Srgrimes *    permission.
161558Srgrimes *
171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271558Srgrimes * SUCH DAMAGE.
281558Srgrimes *
291558Srgrimes * $FreeBSD: stable/9/usr.sbin/mfiutil/mfi_patrol.c 237589 2012-06-26 03:05:17Z eadler $
301558Srgrimes */
311558Srgrimes
321558Srgrimes#include <sys/types.h>
331558Srgrimes#include <sys/errno.h>
3423669Speter#include <err.h>
3596707Strhodes#include <fcntl.h>
361558Srgrimes#include <stdio.h>
371558Srgrimes#include <stdlib.h>
381558Srgrimes#include <string.h>
391558Srgrimes#include <time.h>
401558Srgrimes#include <unistd.h>
411558Srgrimes#include "mfiutil.h"
421558Srgrimes
43164911Sdwmalonestatic char *
44204111Suqsadapter_time(time_t now, uint32_t at_now, uint32_t at)
451558Srgrimes{
461558Srgrimes	time_t t;
4735852Sjkh
481558Srgrimes	t = (now - at_now) + at;
491558Srgrimes	return (ctime(&t));
501558Srgrimes}
511558Srgrimes
521558Srgrimesstatic void
531558Srgrimesmfi_get_time(int fd, uint32_t *at)
5423669Speter{
55102231Strhodes
561558Srgrimes	if (mfi_dcmd_command(fd, MFI_DCMD_TIME_SECS_GET, at, sizeof(*at), NULL,
571558Srgrimes	    0, NULL) < 0) {
581558Srgrimes		warn("Couldn't fetch adapter time");
591558Srgrimes		at = 0;
601558Srgrimes	}
611558Srgrimes}
621558Srgrimes
631558Srgrimesstatic int
64144099Simppatrol_get_props(int fd, struct mfi_pr_properties *prop)
651558Srgrimes{
661558Srgrimes	int error;
67102231Strhodes
681558Srgrimes	if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_PROPERTIES, prop,
691558Srgrimes	    sizeof(*prop), NULL, 0, NULL) < 0) {
701558Srgrimes		error = errno;
711558Srgrimes		warn("Failed to get patrol read properties");
721558Srgrimes		return (error);
731558Srgrimes	}
741558Srgrimes	return (0);
751558Srgrimes}
761558Srgrimes
771558Srgrimesstatic int
781558Srgrimesshow_patrol(int ac __unused, char **av __unused)
791558Srgrimes{
801558Srgrimes	struct mfi_pr_properties prop;
811558Srgrimes	struct mfi_pr_status status;
821558Srgrimes	struct mfi_pd_list *list;
831558Srgrimes	struct mfi_pd_info info;
841558Srgrimes	char label[24];
851558Srgrimes	time_t now;
861558Srgrimes	uint32_t at;
871558Srgrimes	int error, fd;
881558Srgrimes	u_int i;
891558Srgrimes
901558Srgrimes	fd = mfi_open(mfi_unit, O_RDWR);
911558Srgrimes	if (fd < 0) {
921558Srgrimes		error = errno;
931558Srgrimes		warn("mfi_open");
941558Srgrimes		return (error);
951558Srgrimes	}
961558Srgrimes
971558Srgrimes	time(&now);
981558Srgrimes	mfi_get_time(fd, &at);
991558Srgrimes	error = patrol_get_props(fd, &prop);
1001558Srgrimes	if (error) {
1011558Srgrimes		close(fd);
1021558Srgrimes		return (error);
1031558Srgrimes	}
1041558Srgrimes	printf("Operation Mode: ");
10598542Smckusick	switch (prop.op_mode) {
10698542Smckusick	case MFI_PR_OPMODE_AUTO:
10798542Smckusick		printf("auto\n");
10898542Smckusick		break;
10998542Smckusick	case MFI_PR_OPMODE_MANUAL:
11098542Smckusick		printf("manual\n");
11198542Smckusick		break;
11298542Smckusick	case MFI_PR_OPMODE_DISABLED:
11398542Smckusick		printf("disabled\n");
114100207Smckusick		break;
11598542Smckusick	default:
11698542Smckusick		printf("??? (%02x)\n", prop.op_mode);
117100207Smckusick		break;
118167011Smckusick	}
11998542Smckusick	if (prop.op_mode == MFI_PR_OPMODE_AUTO) {
1201558Srgrimes		if (at != 0 && prop.next_exec)
1211558Srgrimes			printf("    Next Run Starts: %s", adapter_time(now, at,
1221558Srgrimes			    prop.next_exec));
1231558Srgrimes		if (prop.exec_freq == 0xffffffff)
1241558Srgrimes			printf("    Runs Execute Continuously\n");
1251558Srgrimes		else if (prop.exec_freq != 0)
1261558Srgrimes			printf("    Runs Start Every %u seconds\n",
1271558Srgrimes			    prop.exec_freq);
1281558Srgrimes	}
1291558Srgrimes
1301558Srgrimes	if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_STATUS, &status,
1311558Srgrimes	    sizeof(status), NULL, 0, NULL) < 0) {
1321558Srgrimes		error = errno;
1331558Srgrimes		warn("Failed to get patrol read properties");
1341558Srgrimes		close(fd);
1351558Srgrimes		return (error);
1361558Srgrimes	}
1371558Srgrimes	printf("Runs Completed: %u\n", status.num_iteration);
1381558Srgrimes	printf("Current State: ");
1391558Srgrimes	switch (status.state) {
1401558Srgrimes	case MFI_PR_STATE_STOPPED:
141103949Smike		printf("stopped\n");
142103949Smike		break;
14323669Speter	case MFI_PR_STATE_READY:
144103949Smike		printf("ready\n");
145103949Smike		break;
1461558Srgrimes	case MFI_PR_STATE_ACTIVE:
1471558Srgrimes		printf("active\n");
1481558Srgrimes		break;
1491558Srgrimes	case MFI_PR_STATE_ABORTED:
1501558Srgrimes		printf("aborted\n");
1511558Srgrimes		break;
152144099Simp	default:
153144099Simp		printf("??? (%02x)\n", status.state);
154		break;
155	}
156	if (status.state == MFI_PR_STATE_ACTIVE) {
157		if (mfi_pd_get_list(fd, &list, NULL) < 0) {
158			error = errno;
159			warn("Failed to get drive list");
160			close(fd);
161			return (error);
162		}
163
164		for (i = 0; i < list->count; i++) {
165			if (list->addr[i].scsi_dev_type != 0)
166				continue;
167
168			if (mfi_pd_get_info(fd, list->addr[i].device_id, &info,
169			    NULL) < 0) {
170				error = errno;
171				warn("Failed to fetch info for drive %u",
172				    list->addr[i].device_id);
173				free(list);
174				close(fd);
175				return (error);
176			}
177			if (info.prog_info.active & MFI_PD_PROGRESS_PATROL) {
178				snprintf(label, sizeof(label), "    Drive %s",
179				    mfi_drive_name(NULL,
180				    list->addr[i].device_id,
181				    MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
182				mfi_display_progress(label,
183				    &info.prog_info.patrol);
184			}
185		}
186		free(list);
187	}
188
189	close(fd);
190
191	return (0);
192}
193MFI_COMMAND(show, patrol, show_patrol);
194
195static int
196start_patrol(int ac __unused, char **av __unused)
197{
198	int error, fd;
199
200	fd = mfi_open(mfi_unit, O_RDWR);
201	if (fd < 0) {
202		error = errno;
203		warn("mfi_open");
204		return (error);
205	}
206
207	if (mfi_dcmd_command(fd, MFI_DCMD_PR_START, NULL, 0, NULL, 0, NULL) <
208	    0) {
209		error = errno;
210		warn("Failed to start patrol read");
211		close(fd);
212		return (error);
213	}
214
215	close(fd);
216
217	return (0);
218}
219MFI_COMMAND(start, patrol, start_patrol);
220
221static int
222stop_patrol(int ac __unused, char **av __unused)
223{
224	int error, fd;
225
226	fd = mfi_open(mfi_unit, O_RDWR);
227	if (fd < 0) {
228		error = errno;
229		warn("mfi_open");
230		return (error);
231	}
232
233	if (mfi_dcmd_command(fd, MFI_DCMD_PR_STOP, NULL, 0, NULL, 0, NULL) <
234	    0) {
235		error = errno;
236		warn("Failed to stop patrol read");
237		close(fd);
238		return (error);
239	}
240
241	close(fd);
242
243	return (0);
244}
245MFI_COMMAND(stop, patrol, stop_patrol);
246
247static int
248patrol_config(int ac, char **av)
249{
250	struct mfi_pr_properties prop;
251	long val;
252	time_t now;
253	int error, fd;
254	uint32_t at, next_exec, exec_freq;
255	char *cp;
256	uint8_t op_mode;
257
258	exec_freq = 0;	/* GCC too stupid */
259	next_exec = 0;
260	if (ac < 2) {
261		warnx("patrol: command required");
262		return (EINVAL);
263	}
264	if (strcasecmp(av[1], "auto") == 0) {
265		op_mode = MFI_PR_OPMODE_AUTO;
266		if (ac > 2) {
267			if (strcasecmp(av[2], "continuously") == 0)
268				exec_freq = 0xffffffff;
269			else {
270				val = strtol(av[2], &cp, 0);
271				if (*cp != '\0') {
272					warnx("patrol: Invalid interval %s",
273					    av[2]);
274					return (EINVAL);
275				}
276				exec_freq = val;
277			}
278		}
279		if (ac > 3) {
280			val = strtol(av[3], &cp, 0);
281			if (*cp != '\0' || val < 0) {
282				warnx("patrol: Invalid start time %s", av[3]);
283				return (EINVAL);
284			}
285			next_exec = val;
286		}
287	} else if (strcasecmp(av[1], "manual") == 0)
288		op_mode = MFI_PR_OPMODE_MANUAL;
289	else if (strcasecmp(av[1], "disable") == 0)
290		op_mode = MFI_PR_OPMODE_DISABLED;
291	else {
292		warnx("patrol: Invalid command %s", av[1]);
293		return (EINVAL);
294	}
295
296	fd = mfi_open(mfi_unit, O_RDWR);
297	if (fd < 0) {
298		error = errno;
299		warn("mfi_open");
300		return (error);
301	}
302
303	error = patrol_get_props(fd, &prop);
304	if (error) {
305		close(fd);
306		return (error);
307	}
308	prop.op_mode = op_mode;
309	if (op_mode == MFI_PR_OPMODE_AUTO) {
310		if (ac > 2)
311			prop.exec_freq = exec_freq;
312		if (ac > 3) {
313			time(&now);
314			mfi_get_time(fd, &at);
315			if (at == 0) {
316				close(fd);
317				return (ENXIO);
318			}
319			prop.next_exec = at + next_exec;
320			printf("Starting next patrol read at %s",
321			    adapter_time(now, at, prop.next_exec));
322		}
323	}
324	if (mfi_dcmd_command(fd, MFI_DCMD_PR_SET_PROPERTIES, &prop,
325	    sizeof(prop), NULL, 0, NULL) < 0) {
326		error = errno;
327		warn("Failed to set patrol read properties");
328		close(fd);
329		return (error);
330	}
331
332	close(fd);
333
334	return (0);
335}
336MFI_COMMAND(top, patrol, patrol_config);
337