xref: /freebsd/usr.sbin/mfiutil/mfi_bbu.c (revision 732a02b4e77866604a120a275c082bb6221bd2ff)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013 Sandvine Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/param.h>
32 #include <sys/errno.h>
33 #include <sys/stat.h>
34 #include <err.h>
35 #include <fcntl.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <time.h>
40 #include <unistd.h>
41 #include "mfiutil.h"
42 
43 /* The autolearn period is given in seconds. */
44 void
45 mfi_autolearn_period(uint32_t period, char *buf, size_t sz)
46 {
47 	unsigned int d, h;
48 	char *tmp;
49 
50 	d = period / (24 * 3600);
51 	h = (period % (24 * 3600)) / 3600;
52 
53 	tmp = buf;
54 	if (d != 0) {
55 		tmp += snprintf(buf, sz, "%u day%s", d, d == 1 ? "" : "s");
56 		sz -= tmp - buf;
57 		if (h != 0) {
58 			tmp += snprintf(tmp, sz, ", ");
59 			sz -= 2;
60 		}
61 	}
62 	if (h != 0)
63 		snprintf(tmp, sz, "%u hour%s", h, h == 1 ? "" : "s");
64 
65 	if (d == 0 && h == 0)
66 		snprintf(tmp, sz, "less than 1 hour");
67 }
68 
69 /* The time to the next relearn is given in seconds since 1/1/2000. */
70 void
71 mfi_next_learn_time(uint32_t next_learn_time, char *buf, size_t sz)
72 {
73 	time_t basetime;
74 	struct tm tm;
75 	size_t len;
76 
77 	memset(&tm, 0, sizeof(tm));
78 	tm.tm_year = 100;
79 	basetime = timegm(&tm);
80 	basetime += (time_t)next_learn_time;
81 	len = snprintf(buf, sz, "%s", ctime(&basetime));
82 	if (len > 0)
83 		/* Get rid of the newline added by ctime(3). */
84 		buf[len - 1] = '\0';
85 }
86 
87 void
88 mfi_autolearn_mode(uint8_t mode, char *buf, size_t sz)
89 {
90 
91 	switch (mode) {
92 	case 0:
93 		snprintf(buf, sz, "enabled");
94 		break;
95 	case 1:
96 		snprintf(buf, sz, "disabled");
97 		break;
98 	case 2:
99 		snprintf(buf, sz, "warn via event");
100 		break;
101 	default:
102 		snprintf(buf, sz, "mode 0x%02x", mode);
103 		break;
104 	}
105 }
106 
107 int
108 mfi_bbu_get_props(int fd, struct mfi_bbu_properties *props, uint8_t *statusp)
109 {
110 
111 	return (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_PROP, props,
112 	    sizeof(*props), NULL, 0, statusp));
113 }
114 
115 int
116 mfi_bbu_set_props(int fd, struct mfi_bbu_properties *props, uint8_t *statusp)
117 {
118 
119 	return (mfi_dcmd_command(fd, MFI_DCMD_BBU_SET_PROP, props,
120 	    sizeof(*props), NULL, 0, statusp));
121 }
122 
123 static int
124 start_bbu_learn(int ac, char **av __unused)
125 {
126 	uint8_t status;
127 	int error, fd;
128 
129 	status = MFI_STAT_OK;
130 	error = 0;
131 
132 	if (ac != 1) {
133 		warnx("start learn: unexpected arguments");
134 		return (EINVAL);
135 	}
136 
137 	fd = mfi_open(mfi_unit, O_RDWR);
138 	if (fd < 0) {
139 		error = errno;
140 		warn("mfi_open");
141 		return (error);
142 	}
143 
144 	if (mfi_dcmd_command(fd, MFI_DCMD_BBU_START_LEARN, NULL, 0, NULL, 0,
145 	    &status) < 0) {
146 		error = errno;
147 		warn("Failed to start BBU learn");
148 	} else if (status != MFI_STAT_OK) {
149 		warnx("Failed to start BBU learn: %s", mfi_status(status));
150 		error = EIO;
151 	}
152 
153 	return (error);
154 }
155 MFI_COMMAND(start, learn, start_bbu_learn);
156 
157 static int
158 update_bbu_props(int ac, char **av)
159 {
160 	struct mfi_bbu_properties props;
161 	unsigned long delay;
162 	uint8_t status;
163 	int error, fd;
164 	char *mode, *endptr;
165 
166 	status = MFI_STAT_OK;
167 	error = 0;
168 
169 	if (ac != 3) {
170 		warnx("bbu: property and value required");
171 		return (EINVAL);
172 	}
173 
174 	fd = mfi_open(mfi_unit, O_RDWR);
175 	if (fd < 0) {
176 		error = errno;
177 		warn("mfi_open");
178 		return (error);
179 	}
180 
181 	if (mfi_bbu_get_props(fd, &props, &status) < 0) {
182 		error = errno;
183 		warn("Failed to get BBU properties");
184 		goto done;
185 	} else if (status != MFI_STAT_OK) {
186 		warnx("Failed to get BBU properties: %s", mfi_status(status));
187 		error = EIO;
188 		goto done;
189 	}
190 
191 	if (strcmp(av[1], "learn-delay") == 0) {
192 		delay = strtoul(av[2], &endptr, 10);
193 		if (strlen(av[2]) == 0 || *endptr != '\0' || delay > 255) {
194 			warnx("Invalid learn delay '%s'", av[2]);
195 			error = EINVAL;
196 			goto done;
197 		}
198 
199 		props.learn_delay_interval = delay;
200 	} else if (strcmp(av[1], "autolearn-mode") == 0) {
201 		mode = av[2];
202 
203 		if (strcmp(av[2], "enable") == 0)
204 			props.auto_learn_mode = 0;
205 		else if (strcmp(av[2], "disable") == 0)
206 			props.auto_learn_mode = 1;
207 		else if (mode[0] >= '0' && mode[0] <= '2' && mode[1] == '\0')
208 			props.auto_learn_mode = mode[0] - '0';
209 		else {
210 			warnx("Invalid mode '%s'", mode);
211 			error = EINVAL;
212 			goto done;
213 		}
214 	} else if (strcmp(av[1], "bbu-mode") == 0) {
215 		if (props.bbu_mode == 0) {
216 			warnx("This BBU does not implement different modes");
217 			error = EINVAL;
218 			goto done;
219 		}
220 
221 		/* The mode must be an integer between 1 and 5. */
222 		mode = av[2];
223 		if (mode[0] < '1' || mode[0] > '5' || mode[1] != '\0') {
224 			warnx("Invalid mode '%s'", mode);
225 			error = EINVAL;
226 			goto done;
227 		}
228 
229 		props.bbu_mode = mode[0] - '0';
230 	} else {
231 		warnx("bbu: Invalid command '%s'", av[1]);
232 		error = EINVAL;
233 		goto done;
234 	}
235 
236 	if (mfi_bbu_set_props(fd, &props, &status) < 0) {
237 		error = errno;
238 		warn("Failed to set BBU properties");
239 		goto done;
240 	} else if (status != MFI_STAT_OK) {
241 		warnx("Failed to set BBU properties: %s", mfi_status(status));
242 		error = EIO;
243 		goto done;
244 	}
245 
246 done:
247 	close(fd);
248 
249 	return (error);
250 }
251 MFI_COMMAND(top, bbu, update_bbu_props);
252