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