1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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
29 #include <sys/param.h>
30 #include <sys/errno.h>
31 #include <sys/stat.h>
32 #include <err.h>
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <time.h>
38 #include <unistd.h>
39 #include "mfiutil.h"
40
41 /* The autolearn period is given in seconds. */
42 void
mfi_autolearn_period(uint32_t period,char * buf,size_t sz)43 mfi_autolearn_period(uint32_t period, char *buf, size_t sz)
44 {
45 unsigned int d, h;
46 char *tmp;
47
48 d = period / (24 * 3600);
49 h = (period % (24 * 3600)) / 3600;
50
51 tmp = buf;
52 if (d != 0) {
53 int fmt_len;
54 fmt_len = snprintf(buf, sz, "%u day%s", d, d == 1 ? "" : "s");
55 if (fmt_len < 0) {
56 *buf = 0;
57 return;
58 }
59 if ((size_t)fmt_len >= sz) {
60 return;
61 }
62 tmp += fmt_len;
63 sz -= tmp - buf;
64 if (h != 0) {
65 fmt_len = snprintf(tmp, sz, ", ");
66 if (fmt_len < 0 || (size_t)fmt_len >= sz) {
67 return;
68 }
69 tmp += fmt_len;
70 sz -= 2;
71 }
72 }
73 if (h != 0)
74 snprintf(tmp, sz, "%u hour%s", h, h == 1 ? "" : "s");
75
76 if (d == 0 && h == 0)
77 snprintf(tmp, sz, "less than 1 hour");
78 }
79
80 /* The time to the next relearn is given in seconds since 1/1/2000. */
81 void
mfi_next_learn_time(uint32_t next_learn_time,char * buf,size_t sz)82 mfi_next_learn_time(uint32_t next_learn_time, char *buf, size_t sz)
83 {
84 time_t basetime;
85 struct tm tm;
86 size_t len;
87
88 memset(&tm, 0, sizeof(tm));
89 tm.tm_year = 100;
90 basetime = timegm(&tm);
91 basetime += (time_t)next_learn_time;
92 len = snprintf(buf, sz, "%s", ctime(&basetime));
93 if (len > 0)
94 /* Get rid of the newline added by ctime(3). */
95 buf[len - 1] = '\0';
96 }
97
98 void
mfi_autolearn_mode(uint8_t mode,char * buf,size_t sz)99 mfi_autolearn_mode(uint8_t mode, char *buf, size_t sz)
100 {
101
102 switch (mode) {
103 case 0:
104 snprintf(buf, sz, "enabled");
105 break;
106 case 1:
107 snprintf(buf, sz, "disabled");
108 break;
109 case 2:
110 snprintf(buf, sz, "warn via event");
111 break;
112 default:
113 snprintf(buf, sz, "mode 0x%02x", mode);
114 break;
115 }
116 }
117
118 int
mfi_bbu_get_props(int fd,struct mfi_bbu_properties * props,uint8_t * statusp)119 mfi_bbu_get_props(int fd, struct mfi_bbu_properties *props, uint8_t *statusp)
120 {
121
122 return (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_PROP, props,
123 sizeof(*props), NULL, 0, statusp));
124 }
125
126 int
mfi_bbu_set_props(int fd,struct mfi_bbu_properties * props,uint8_t * statusp)127 mfi_bbu_set_props(int fd, struct mfi_bbu_properties *props, uint8_t *statusp)
128 {
129
130 return (mfi_dcmd_command(fd, MFI_DCMD_BBU_SET_PROP, props,
131 sizeof(*props), NULL, 0, statusp));
132 }
133
134 static int
start_bbu_learn(int ac,char ** av __unused)135 start_bbu_learn(int ac, char **av __unused)
136 {
137 uint8_t status;
138 int error, fd;
139
140 status = MFI_STAT_OK;
141 error = 0;
142
143 if (ac != 1) {
144 warnx("start learn: unexpected arguments");
145 return (EINVAL);
146 }
147
148 fd = mfi_open(mfi_device, O_RDWR);
149 if (fd < 0) {
150 error = errno;
151 warn("mfi_open");
152 return (error);
153 }
154
155 if (mfi_dcmd_command(fd, MFI_DCMD_BBU_START_LEARN, NULL, 0, NULL, 0,
156 &status) < 0) {
157 error = errno;
158 warn("Failed to start BBU learn");
159 } else if (status != MFI_STAT_OK) {
160 warnx("Failed to start BBU learn: %s", mfi_status(status));
161 error = EIO;
162 }
163
164 return (error);
165 }
166 MFI_COMMAND(start, learn, start_bbu_learn);
167
168 static int
update_bbu_props(int ac,char ** av)169 update_bbu_props(int ac, char **av)
170 {
171 struct mfi_bbu_properties props;
172 unsigned long delay;
173 uint8_t status;
174 int error, fd;
175 char *mode, *endptr;
176
177 status = MFI_STAT_OK;
178 error = 0;
179
180 if (ac != 3) {
181 warnx("bbu: property and value required");
182 return (EINVAL);
183 }
184
185 fd = mfi_open(mfi_device, O_RDWR);
186 if (fd < 0) {
187 error = errno;
188 warn("mfi_open");
189 return (error);
190 }
191
192 if (mfi_bbu_get_props(fd, &props, &status) < 0) {
193 error = errno;
194 warn("Failed to get BBU properties");
195 goto done;
196 } else if (status != MFI_STAT_OK) {
197 warnx("Failed to get BBU properties: %s", mfi_status(status));
198 error = EIO;
199 goto done;
200 }
201
202 if (strcmp(av[1], "learn-delay") == 0) {
203 delay = strtoul(av[2], &endptr, 10);
204 if (strlen(av[2]) == 0 || *endptr != '\0' || delay > 255) {
205 warnx("Invalid learn delay '%s'", av[2]);
206 error = EINVAL;
207 goto done;
208 }
209
210 props.learn_delay_interval = delay;
211 } else if (strcmp(av[1], "autolearn-mode") == 0) {
212 mode = av[2];
213
214 if (strcmp(av[2], "enable") == 0)
215 props.auto_learn_mode = 0;
216 else if (strcmp(av[2], "disable") == 0)
217 props.auto_learn_mode = 1;
218 else if (mode[0] >= '0' && mode[0] <= '2' && mode[1] == '\0')
219 props.auto_learn_mode = mode[0] - '0';
220 else {
221 warnx("Invalid mode '%s'", mode);
222 error = EINVAL;
223 goto done;
224 }
225 } else if (strcmp(av[1], "bbu-mode") == 0) {
226 if (props.bbu_mode == 0) {
227 warnx("This BBU does not implement different modes");
228 error = EINVAL;
229 goto done;
230 }
231
232 /* The mode must be an integer between 1 and 5. */
233 mode = av[2];
234 if (mode[0] < '1' || mode[0] > '5' || mode[1] != '\0') {
235 warnx("Invalid mode '%s'", mode);
236 error = EINVAL;
237 goto done;
238 }
239
240 props.bbu_mode = mode[0] - '0';
241 } else {
242 warnx("bbu: Invalid command '%s'", av[1]);
243 error = EINVAL;
244 goto done;
245 }
246
247 if (mfi_bbu_set_props(fd, &props, &status) < 0) {
248 error = errno;
249 warn("Failed to set BBU properties");
250 goto done;
251 } else if (status != MFI_STAT_OK) {
252 warnx("Failed to set BBU properties: %s", mfi_status(status));
253 error = EIO;
254 goto done;
255 }
256
257 done:
258 close(fd);
259
260 return (error);
261 }
262 MFI_COMMAND(top, bbu, update_bbu_props);
263