2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2008, 2009 Yahoo!, 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.
15 * 3. The names of the authors may not be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/ioctl.h>
36 #include <sys/sysctl.h>
49 #include <dev/mfi/mfi_ioctl.h>
51 static const char *mfi_status_codes[] = {
52 "Command completed successfully",
54 "Invalid DMCD opcode",
56 "Invalid Sequence Number",
57 "Abort isn't possible for the requested command",
58 "Application 'host' code not found",
60 "Application not initialized",
61 "Array index invalid",
62 "Array row not empty",
63 "Configuration resource conflict",
66 "Flash memory allocation failed",
67 "Flash download already in progress",
68 "Flash operation failed",
70 "Incomplete flash image",
74 "Specified application doesn't have host-resident code",
75 "Volume consistency check in progress",
76 "Volume initialization in progress",
77 "Volume LBA out of range",
78 "Maximum number of volumes are already configured",
79 "Volume is not OPTIMAL",
80 "Volume rebuild in progress",
81 "Volume reconstruction in progress",
82 "Volume RAID level is wrong for requested operation",
83 "Too many spares assigned",
84 "Scratch memory not available",
85 "Error writing MFC data to SEEPROM",
86 "Required hardware is missing",
88 "Volume drives are not within an enclosure",
89 "Drive clear in progress",
90 "Drive type mismatch (SATA vs SAS)",
91 "Patrol read disabled",
93 "SAS Config - Invalid action",
94 "SAS Config - Invalid data",
95 "SAS Config - Invalid page",
96 "SAS Config - Invalid type",
97 "SCSI command completed with error",
98 "SCSI I/O request failed",
99 "SCSI RESERVATION_CONFLICT",
100 "One or more flush operations during shutdown failed",
101 "Firmware time is not set",
102 "Wrong firmware or drive state",
104 "Peer controller rejected request",
105 "Unable to inform peer of communication changes",
106 "Volume reservation already in progress",
107 "I2C errors were detected",
108 "PCI errors occurred during XOR/DMA operation",
109 "Diagnostics failed",
110 "Unable to process command as boot messages are pending",
111 "Foreign configuration is incomplete"
115 mfi_status(u_int status_code)
117 static char buffer[16];
119 if (status_code == MFI_STAT_INVALID_STATUS)
120 return ("Invalid status");
121 if (status_code < sizeof(mfi_status_codes) / sizeof(char *))
122 return (mfi_status_codes[status_code]);
123 snprintf(buffer, sizeof(buffer), "Status: 0x%02x", status_code);
128 mfi_raid_level(uint8_t primary_level, uint8_t secondary_level)
132 switch (primary_level) {
136 if (secondary_level != 0)
145 if (secondary_level != 0)
154 if (secondary_level != 0)
163 sprintf(buf, "LVL 0x%02x", primary_level);
169 mfi_query_disk(int fd, uint8_t target_id, struct mfi_query_disk *info)
172 bzero(info, sizeof(*info));
173 info->array_id = target_id;
174 if (ioctl(fd, MFIIO_QUERY_DISK, info) < 0)
176 if (!info->present) {
184 mfi_volume_name(int fd, uint8_t target_id)
186 static struct mfi_query_disk info;
189 if (mfi_query_disk(fd, target_id, &info) < 0) {
190 snprintf(buf, sizeof(buf), "%d", target_id);
193 return (info.devname);
197 mfi_volume_busy(int fd, uint8_t target_id)
199 struct mfi_query_disk info;
201 /* Assume it isn't mounted if we can't get information. */
202 if (mfi_query_disk(fd, target_id, &info) < 0)
204 return (info.open != 0);
208 * Check if the running kernel supports changing the RAID
209 * configuration of the mfi controller.
212 mfi_reconfig_supported(void)
219 snprintf(mibname, sizeof(mibname), "dev.mfi.%d.delete_busy_volumes",
221 return (sysctlbyname(mibname, &dummy, &len, NULL, 0) == 0);
225 mfi_lookup_volume(int fd, const char *name, uint8_t *target_id)
227 struct mfi_query_disk info;
228 struct mfi_ld_list list;
233 /* If it's a valid number, treat it as a raw target ID. */
234 val = strtol(name, &cp, 0);
240 if (mfi_dcmd_command(fd, MFI_DCMD_LD_GET_LIST, &list, sizeof(list),
244 for (i = 0; i < list.ld_count; i++) {
245 if (mfi_query_disk(fd, list.ld_list[i].ld.v.target_id,
248 if (strcmp(name, info.devname) == 0) {
249 *target_id = list.ld_list[i].ld.v.target_id;
258 mfi_dcmd_command(int fd, uint32_t opcode, void *buf, size_t bufsize,
259 uint8_t *mbox, size_t mboxlen, uint8_t *statusp)
261 struct mfi_ioc_passthru ioc;
262 struct mfi_dcmd_frame *dcmd;
265 if ((mbox != NULL && (mboxlen == 0 || mboxlen > MFI_MBOX_SIZE)) ||
266 (mbox == NULL && mboxlen != 0)) {
271 bzero(&ioc, sizeof(ioc));
272 dcmd = &ioc.ioc_frame;
274 bcopy(mbox, dcmd->mbox, mboxlen);
275 dcmd->header.cmd = MFI_CMD_DCMD;
276 dcmd->header.timeout = 0;
277 dcmd->header.flags = 0;
278 dcmd->header.data_len = bufsize;
279 dcmd->opcode = opcode;
282 ioc.buf_size = bufsize;
283 r = ioctl(fd, MFIIO_PASSTHRU, &ioc);
288 *statusp = dcmd->header.cmd_status;
289 else if (dcmd->header.cmd_status != MFI_STAT_OK) {
290 warnx("Command failed: %s",
291 mfi_status(dcmd->header.cmd_status));
299 mfi_ctrl_get_info(int fd, struct mfi_ctrl_info *info, uint8_t *statusp)
302 return (mfi_dcmd_command(fd, MFI_DCMD_CTRL_GETINFO, info,
303 sizeof(struct mfi_ctrl_info), NULL, 0, statusp));
307 mfi_open(int unit, int acs)
309 char path[MAXPATHLEN];
311 snprintf(path, sizeof(path), "/dev/mfi%d", unit);
312 return (open(path, acs));
316 print_time_humanized(uint seconds)
319 if (seconds > 3600) {
320 printf("%u:", seconds / 3600);
324 printf("%02u:%02u", seconds / 60, seconds % 60);
326 printf("%us", seconds);
331 mfi_display_progress(const char *label, struct mfi_progress *prog)
335 printf("%s: %.2f%% complete after ", label,
336 (float)prog->progress * 100 / 0xffff);
337 print_time_humanized(prog->elapsed_seconds);
338 if (prog->progress != 0 && prog->elapsed_seconds > 10) {
339 printf(" finished in ");
340 seconds = (0x10000 * (uint32_t)prog->elapsed_seconds) /
341 prog->progress - prog->elapsed_seconds;
342 print_time_humanized(seconds);
348 mfi_table_handler(struct mfiutil_command **start, struct mfiutil_command **end,
351 struct mfiutil_command **cmd;
354 warnx("The %s command requires a sub-command.", av[0]);
357 for (cmd = start; cmd < end; cmd++) {
358 if (strcmp((*cmd)->name, av[1]) == 0)
359 return ((*cmd)->handler(ac - 1, av + 1));
362 warnx("%s is not a valid sub-command of %s.", av[1], av[0]);