2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 Sandvine Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/param.h>
32 #include <sys/errno.h>
43 /* The autolearn period is given in seconds. */
45 mfi_autolearn_period(uint32_t period, char *buf, size_t sz)
50 d = period / (24 * 3600);
51 h = (period % (24 * 3600)) / 3600;
55 tmp += snprintf(buf, sz, "%u day%s", d, d == 1 ? "" : "s");
58 tmp += snprintf(tmp, sz, ", ");
63 snprintf(tmp, sz, "%u hour%s", h, h == 1 ? "" : "s");
66 snprintf(tmp, sz, "less than 1 hour");
69 /* The time to the next relearn is given in seconds since 1/1/2000. */
71 mfi_next_learn_time(uint32_t next_learn_time, char *buf, size_t sz)
77 memset(&tm, 0, sizeof(tm));
79 basetime = timegm(&tm);
80 basetime += (time_t)next_learn_time;
81 len = snprintf(buf, sz, "%s", ctime(&basetime));
83 /* Get rid of the newline added by ctime(3). */
88 mfi_autolearn_mode(uint8_t mode, char *buf, size_t sz)
93 snprintf(buf, sz, "enabled");
96 snprintf(buf, sz, "disabled");
99 snprintf(buf, sz, "warn via event");
102 snprintf(buf, sz, "mode 0x%02x", mode);
108 mfi_bbu_get_props(int fd, struct mfi_bbu_properties *props, uint8_t *statusp)
111 return (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_PROP, props,
112 sizeof(*props), NULL, 0, statusp));
116 mfi_bbu_set_props(int fd, struct mfi_bbu_properties *props, uint8_t *statusp)
119 return (mfi_dcmd_command(fd, MFI_DCMD_BBU_SET_PROP, props,
120 sizeof(*props), NULL, 0, statusp));
124 start_bbu_learn(int ac, char **av __unused)
129 status = MFI_STAT_OK;
133 warnx("start learn: unexpected arguments");
137 fd = mfi_open(mfi_unit, O_RDWR);
144 if (mfi_dcmd_command(fd, MFI_DCMD_BBU_START_LEARN, NULL, 0, NULL, 0,
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));
155 MFI_COMMAND(start, learn, start_bbu_learn);
158 update_bbu_props(int ac, char **av)
160 struct mfi_bbu_properties props;
166 status = MFI_STAT_OK;
170 warnx("bbu: property and value required");
174 fd = mfi_open(mfi_unit, O_RDWR);
181 if (mfi_bbu_get_props(fd, &props, &status) < 0) {
183 warn("Failed to get BBU properties");
185 } else if (status != MFI_STAT_OK) {
186 warnx("Failed to get BBU properties: %s", mfi_status(status));
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]);
199 props.learn_delay_interval = delay;
200 } else if (strcmp(av[1], "autolearn-mode") == 0) {
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';
210 warnx("Invalid mode '%s'", mode);
214 } else if (strcmp(av[1], "bbu-mode") == 0) {
215 if (props.bbu_mode == 0) {
216 warnx("This BBU does not implement different modes");
221 /* The mode must be an integer between 1 and 5. */
223 if (mode[0] < '1' || mode[0] > '5' || mode[1] != '\0') {
224 warnx("Invalid mode '%s'", mode);
229 props.bbu_mode = mode[0] - '0';
231 warnx("bbu: Invalid command '%s'", av[1]);
236 if (mfi_bbu_set_props(fd, &props, &status) < 0) {
238 warn("Failed to set BBU properties");
240 } else if (status != MFI_STAT_OK) {
241 warnx("Failed to set BBU properties: %s", mfi_status(status));
251 MFI_COMMAND(top, bbu, update_bbu_props);