summaryrefslogtreecommitdiffstats
path: root/usr.sbin/mfiutil/mfi_patrol.c
blob: c3e47f32b33c437be907dcf38e772c2574ea7ab7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*-
 * Copyright (c) 2008, 2009 Yahoo!, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The names of the authors may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/types.h>
#include <sys/errno.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "mfiutil.h"

static char *
adapter_time(time_t now, uint32_t at_now, uint32_t at)
{
	time_t t;

	t = (now - at_now) + at;
	return (ctime(&t));
}

static void
mfi_get_time(int fd, uint32_t *at)
{

	if (mfi_dcmd_command(fd, MFI_DCMD_TIME_SECS_GET, at, sizeof(*at), NULL,
	    0, NULL) < 0) {
		warn("Couldn't fetch adapter time");
		at = 0;
	}
}

static int
patrol_get_props(int fd, struct mfi_pr_properties *prop)
{
	int error;

	if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_PROPERTIES, prop,
	    sizeof(*prop), NULL, 0, NULL) < 0) {
		error = errno;
		warn("Failed to get patrol read properties");
		return (error);
	}
	return (0);
}

static int
show_patrol(int ac __unused, char **av __unused)
{
	struct mfi_pr_properties prop;
	struct mfi_pr_status status;
	struct mfi_pd_list *list;
	struct mfi_pd_info info;
	char label[24];
	time_t now;
	uint32_t at;
	int error, fd;
	u_int i;

	fd = mfi_open(mfi_unit, O_RDWR);
	if (fd < 0) {
		error = errno;
		warn("mfi_open");
		return (error);
	}

	time(&now);
	mfi_get_time(fd, &at);
	error = patrol_get_props(fd, &prop);
	if (error) {
		close(fd);
		return (error);
	}
	printf("Operation Mode: ");
	switch (prop.op_mode) {
	case MFI_PR_OPMODE_AUTO:
		printf("auto\n");
		break;
	case MFI_PR_OPMODE_MANUAL:
		printf("manual\n");
		break;
	case MFI_PR_OPMODE_DISABLED:
		printf("disabled\n");
		break;
	default:
		printf("??? (%02x)\n", prop.op_mode);
		break;
	}
	if (prop.op_mode == MFI_PR_OPMODE_AUTO) {
		if (at != 0 && prop.next_exec)
			printf("    Next Run Starts: %s", adapter_time(now, at,
			    prop.next_exec));
		if (prop.exec_freq == 0xffffffff)
			printf("    Runs Execute Continuously\n");
		else if (prop.exec_freq != 0)
			printf("    Runs Start Every %u seconds\n",
			    prop.exec_freq);
	}

	if (mfi_dcmd_command(fd, MFI_DCMD_PR_GET_STATUS, &status,
	    sizeof(status), NULL, 0, NULL) < 0) {
		error = errno;
		warn("Failed to get patrol read properties");
		close(fd);
		return (error);
	}
	printf("Runs Completed: %u\n", status.num_iteration);
	printf("Current State: ");
	switch (status.state) {
	case MFI_PR_STATE_STOPPED:
		printf("stopped\n");
		break;
	case MFI_PR_STATE_READY:
		printf("ready\n");
		break;
	case MFI_PR_STATE_ACTIVE:
		printf("active\n");
		break;
	case MFI_PR_STATE_ABORTED:
		printf("aborted\n");
		break;
	default:
		printf("??? (%02x)\n", status.state);
		break;
	}
	if (status.state == MFI_PR_STATE_ACTIVE) {
		if (mfi_pd_get_list(fd, &list, NULL) < 0) {
			error = errno;
			warn("Failed to get drive list");
			close(fd);
			return (error);
		}

		for (i = 0; i < list->count; i++) {
			if (list->addr[i].scsi_dev_type != 0)
				continue;

			if (mfi_pd_get_info(fd, list->addr[i].device_id, &info,
			    NULL) < 0) {
				error = errno;
				warn("Failed to fetch info for drive %u",
				    list->addr[i].device_id);
				free(list);
				close(fd);
				return (error);
			}
			if (info.prog_info.active & MFI_PD_PROGRESS_PATROL) {
				snprintf(label, sizeof(label), "    Drive %s",
				    mfi_drive_name(NULL,
				    list->addr[i].device_id,
				    MFI_DNAME_DEVICE_ID|MFI_DNAME_HONOR_OPTS));
				mfi_display_progress(label,
				    &info.prog_info.patrol);
			}
		}
		free(list);
	}

	close(fd);

	return (0);
}
MFI_COMMAND(show, patrol, show_patrol);

static int
start_patrol(int ac __unused, char **av __unused)
{
	int error, fd;

	fd = mfi_open(mfi_unit, O_RDWR);
	if (fd < 0) {
		error = errno;
		warn("mfi_open");
		return (error);
	}

	if (mfi_dcmd_command(fd, MFI_DCMD_PR_START, NULL, 0, NULL, 0, NULL) <
	    0) {
		error = errno;
		warn("Failed to start patrol read");
		close(fd);
		return (error);
	}

	close(fd);

	return (0);
}
MFI_COMMAND(start, patrol, start_patrol);

static int
stop_patrol(int ac __unused, char **av __unused)
{
	int error, fd;

	fd = mfi_open(mfi_unit, O_RDWR);
	if (fd < 0) {
		error = errno;
		warn("mfi_open");
		return (error);
	}

	if (mfi_dcmd_command(fd, MFI_DCMD_PR_STOP, NULL, 0, NULL, 0, NULL) <
	    0) {
		error = errno;
		warn("Failed to stop patrol read");
		close(fd);
		return (error);
	}

	close(fd);

	return (0);
}
MFI_COMMAND(stop, patrol, stop_patrol);

static int
patrol_config(int ac, char **av)
{
	struct mfi_pr_properties prop;
	long val;
	time_t now;
	int error, fd;
	uint32_t at, next_exec, exec_freq;
	char *cp;
	uint8_t op_mode;

	exec_freq = 0;	/* GCC too stupid */
	next_exec = 0;
	if (ac < 2) {
		warnx("patrol: command required");
		return (EINVAL);
	}
	if (strcasecmp(av[1], "auto") == 0) {
		op_mode = MFI_PR_OPMODE_AUTO;
		if (ac > 2) {
			if (strcasecmp(av[2], "continuously") == 0)
				exec_freq = 0xffffffff;
			else {
				val = strtol(av[2], &cp, 0);
				if (*cp != '\0') {
					warnx("patrol: Invalid interval %s",
					    av[2]);
					return (EINVAL);
				}
				exec_freq = val;
			}
		}
		if (ac > 3) {
			val = strtol(av[3], &cp, 0);
			if (*cp != '\0' || val < 0) {
				warnx("patrol: Invalid start time %s", av[3]);
				return (EINVAL);
			}
			next_exec = val;
		}
	} else if (strcasecmp(av[1], "manual") == 0)
		op_mode = MFI_PR_OPMODE_MANUAL;
	else if (strcasecmp(av[1], "disable") == 0)
		op_mode = MFI_PR_OPMODE_DISABLED;
	else {
		warnx("patrol: Invalid command %s", av[1]);
		return (EINVAL);
	}

	fd = mfi_open(mfi_unit, O_RDWR);
	if (fd < 0) {
		error = errno;
		warn("mfi_open");
		return (error);
	}

	error = patrol_get_props(fd, &prop);
	if (error) {
		close(fd);
		return (error);
	}
	prop.op_mode = op_mode;
	if (op_mode == MFI_PR_OPMODE_AUTO) {
		if (ac > 2)
			prop.exec_freq = exec_freq;
		if (ac > 3) {
			time(&now);
			mfi_get_time(fd, &at);
			if (at == 0) {
				close(fd);
				return (ENXIO);
			}
			prop.next_exec = at + next_exec;
			printf("Starting next patrol read at %s",
			    adapter_time(now, at, prop.next_exec));
		}
	}
	if (mfi_dcmd_command(fd, MFI_DCMD_PR_SET_PROPERTIES, &prop,
	    sizeof(prop), NULL, 0, NULL) < 0) {
		error = errno;
		warn("Failed to set patrol read properties");
		close(fd);
		return (error);
	}

	close(fd);

	return (0);
}
MFI_COMMAND(top, patrol, patrol_config);
OpenPOWER on IntegriCloud