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