1fc58801cSScott Long /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
4fc58801cSScott Long * Copyright (c) 2008 Yahoo!, Inc.
5fc58801cSScott Long * All rights reserved.
6fc58801cSScott Long * Written by: John Baldwin <jhb@FreeBSD.org>
7fc58801cSScott Long *
8fc58801cSScott Long * Redistribution and use in source and binary forms, with or without
9fc58801cSScott Long * modification, are permitted provided that the following conditions
10fc58801cSScott Long * are met:
11fc58801cSScott Long * 1. Redistributions of source code must retain the above copyright
12fc58801cSScott Long * notice, this list of conditions and the following disclaimer.
13fc58801cSScott Long * 2. Redistributions in binary form must reproduce the above copyright
14fc58801cSScott Long * notice, this list of conditions and the following disclaimer in the
15fc58801cSScott Long * documentation and/or other materials provided with the distribution.
16fc58801cSScott Long * 3. Neither the name of the author nor the names of any co-contributors
17fc58801cSScott Long * may be used to endorse or promote products derived from this software
18fc58801cSScott Long * without specific prior written permission.
19fc58801cSScott Long *
20fc58801cSScott Long * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21fc58801cSScott Long * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22fc58801cSScott Long * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23fc58801cSScott Long * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24fc58801cSScott Long * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25fc58801cSScott Long * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26fc58801cSScott Long * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27fc58801cSScott Long * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28fc58801cSScott Long * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29fc58801cSScott Long * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30fc58801cSScott Long * SUCH DAMAGE.
31fc58801cSScott Long */
32fc58801cSScott Long
33fc58801cSScott Long #include <sys/param.h>
34fc58801cSScott Long #include <sys/errno.h>
35fc58801cSScott Long #include <err.h>
36fc58801cSScott Long #include <libutil.h>
37fc58801cSScott Long #include <stdio.h>
38fc58801cSScott Long #include <stdlib.h>
39fc58801cSScott Long #include <string.h>
40fc58801cSScott Long #include <unistd.h>
41fc58801cSScott Long #include <ctype.h>
42fc58801cSScott Long #include "mptutil.h"
43fc58801cSScott Long
44fc58801cSScott Long MPT_TABLE(top, volume);
45fc58801cSScott Long
46fc58801cSScott Long const char *
mpt_volstate(U8 State)47fc58801cSScott Long mpt_volstate(U8 State)
48fc58801cSScott Long {
49fc58801cSScott Long static char buf[16];
50fc58801cSScott Long
51fc58801cSScott Long switch (State) {
52fc58801cSScott Long case MPI_RAIDVOL0_STATUS_STATE_OPTIMAL:
53fc58801cSScott Long return ("OPTIMAL");
54fc58801cSScott Long case MPI_RAIDVOL0_STATUS_STATE_DEGRADED:
55fc58801cSScott Long return ("DEGRADED");
56fc58801cSScott Long case MPI_RAIDVOL0_STATUS_STATE_FAILED:
57fc58801cSScott Long return ("FAILED");
58fc58801cSScott Long case MPI_RAIDVOL0_STATUS_STATE_MISSING:
59fc58801cSScott Long return ("MISSING");
60fc58801cSScott Long default:
61fc58801cSScott Long sprintf(buf, "VSTATE 0x%02x", State);
62fc58801cSScott Long return (buf);
63fc58801cSScott Long }
64fc58801cSScott Long }
65fc58801cSScott Long
66fc58801cSScott Long static int
volume_name(int ac,char ** av)67fc58801cSScott Long volume_name(int ac, char **av)
68fc58801cSScott Long {
69fc58801cSScott Long CONFIG_PAGE_RAID_VOL_1 *vnames;
70fc58801cSScott Long U8 VolumeBus, VolumeID;
71c5f2b79dSJohn Baldwin int error, fd;
72fc58801cSScott Long
73fc58801cSScott Long if (ac != 3) {
74fc58801cSScott Long warnx("name: volume and name required");
75fc58801cSScott Long return (EINVAL);
76fc58801cSScott Long }
77fc58801cSScott Long
78fc58801cSScott Long if (strlen(av[2]) >= sizeof(vnames->Name)) {
79fc58801cSScott Long warnx("name: new name is too long");
80fc58801cSScott Long return (ENOSPC);
81fc58801cSScott Long }
82fc58801cSScott Long
83fc58801cSScott Long fd = mpt_open(mpt_unit);
84fc58801cSScott Long if (fd < 0) {
85c5f2b79dSJohn Baldwin error = errno;
86fc58801cSScott Long warn("mpt_open");
87c5f2b79dSJohn Baldwin return (error);
88fc58801cSScott Long }
89fc58801cSScott Long
90c5f2b79dSJohn Baldwin error = mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID);
91c5f2b79dSJohn Baldwin if (error) {
92c5f2b79dSJohn Baldwin warnc(error, "Invalid volume: %s", av[1]);
93c5f2b79dSJohn Baldwin return (error);
94fc58801cSScott Long }
95fc58801cSScott Long
96fc58801cSScott Long vnames = mpt_vol_names(fd, VolumeBus, VolumeID, NULL);
97fc58801cSScott Long if (vnames == NULL) {
98c5f2b79dSJohn Baldwin error = errno;
99fc58801cSScott Long warn("Failed to fetch volume names");
100d68b76a9SAlan Somers close(fd);
101c5f2b79dSJohn Baldwin return (error);
102fc58801cSScott Long }
103fc58801cSScott Long
104fc58801cSScott Long if (vnames->Header.PageType != MPI_CONFIG_PAGEATTR_CHANGEABLE) {
105fc58801cSScott Long warnx("Volume name is read only");
106d68b76a9SAlan Somers free(vnames);
107d68b76a9SAlan Somers close(fd);
108fc58801cSScott Long return (EOPNOTSUPP);
109fc58801cSScott Long }
110fc58801cSScott Long printf("mpt%u changing volume %s name from \"%s\" to \"%s\"\n",
111fc58801cSScott Long mpt_unit, mpt_volume_name(VolumeBus, VolumeID), vnames->Name,
112fc58801cSScott Long av[2]);
113fc58801cSScott Long bzero(vnames->Name, sizeof(vnames->Name));
114fc58801cSScott Long strcpy(vnames->Name, av[2]);
115fc58801cSScott Long
116fc58801cSScott Long if (mpt_write_config_page(fd, vnames, NULL) < 0) {
117c5f2b79dSJohn Baldwin error = errno;
118fc58801cSScott Long warn("Failed to set volume name");
119d68b76a9SAlan Somers free(vnames);
120d68b76a9SAlan Somers close(fd);
121c5f2b79dSJohn Baldwin return (error);
122fc58801cSScott Long }
123fc58801cSScott Long
124fc58801cSScott Long free(vnames);
125fc58801cSScott Long close(fd);
126fc58801cSScott Long
127fc58801cSScott Long return (0);
128fc58801cSScott Long }
129fc58801cSScott Long MPT_COMMAND(top, name, volume_name);
130fc58801cSScott Long
131fc58801cSScott Long static int
volume_status(int ac,char ** av)132fc58801cSScott Long volume_status(int ac, char **av)
133fc58801cSScott Long {
134fc58801cSScott Long MPI_RAID_VOL_INDICATOR prog;
135fc58801cSScott Long RAID_VOL0_STATUS VolumeStatus;
136fc58801cSScott Long uint64_t total, remaining;
137fc58801cSScott Long float pct;
138fc58801cSScott Long U8 VolumeBus, VolumeID;
139c5f2b79dSJohn Baldwin int error, fd;
140fc58801cSScott Long
141fc58801cSScott Long if (ac != 2) {
142fc58801cSScott Long warnx("volume status: %s", ac > 2 ? "extra arguments" :
143fc58801cSScott Long "volume required");
144fc58801cSScott Long return (EINVAL);
145fc58801cSScott Long }
146fc58801cSScott Long
147fc58801cSScott Long fd = mpt_open(mpt_unit);
148fc58801cSScott Long if (fd < 0) {
149c5f2b79dSJohn Baldwin error = errno;
150fc58801cSScott Long warn("mpt_open");
151c5f2b79dSJohn Baldwin return (error);
152fc58801cSScott Long }
153fc58801cSScott Long
154c5f2b79dSJohn Baldwin error = mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID);
155c5f2b79dSJohn Baldwin if (error) {
156c5f2b79dSJohn Baldwin warnc(error, "Invalid volume: %s", av[1]);
157d68b76a9SAlan Somers close(fd);
158c5f2b79dSJohn Baldwin return (error);
159fc58801cSScott Long }
160fc58801cSScott Long
161c5f2b79dSJohn Baldwin error = mpt_raid_action(fd, MPI_RAID_ACTION_INDICATOR_STRUCT, VolumeBus,
162fc58801cSScott Long VolumeID, 0, 0, NULL, 0, &VolumeStatus, (U32 *)&prog, sizeof(prog),
163c5f2b79dSJohn Baldwin NULL, NULL, 0);
164c5f2b79dSJohn Baldwin if (error) {
165c5f2b79dSJohn Baldwin warnc(error, "Fetching volume status failed");
166d68b76a9SAlan Somers close(fd);
167c5f2b79dSJohn Baldwin return (error);
168fc58801cSScott Long }
169fc58801cSScott Long
170fc58801cSScott Long printf("Volume %s status:\n", mpt_volume_name(VolumeBus, VolumeID));
171fc58801cSScott Long printf(" state: %s\n", mpt_volstate(VolumeStatus.State));
172fc58801cSScott Long printf(" flags:");
173fc58801cSScott Long if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_ENABLED)
174fc58801cSScott Long printf(" ENABLED");
175fc58801cSScott Long else
176fc58801cSScott Long printf(" DISABLED");
177fc58801cSScott Long if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_QUIESCED)
178fc58801cSScott Long printf(", QUIESCED");
179fc58801cSScott Long if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
180fc58801cSScott Long printf(", REBUILDING");
181fc58801cSScott Long if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_VOLUME_INACTIVE)
182fc58801cSScott Long printf(", INACTIVE");
183fc58801cSScott Long if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_BAD_BLOCK_TABLE_FULL)
184fc58801cSScott Long printf(", BAD BLOCK TABLE FULL");
185fc58801cSScott Long printf("\n");
186fc58801cSScott Long if (VolumeStatus.Flags & MPI_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
187fc58801cSScott Long total = (uint64_t)prog.TotalBlocks.High << 32 |
188fc58801cSScott Long prog.TotalBlocks.Low;
189fc58801cSScott Long remaining = (uint64_t)prog.BlocksRemaining.High << 32 |
190fc58801cSScott Long prog.BlocksRemaining.Low;
191fc58801cSScott Long pct = (float)(total - remaining) * 100 / total;
192fc58801cSScott Long printf(" resync: %.2f%% complete\n", pct);
193fc58801cSScott Long }
194fc58801cSScott Long
195fc58801cSScott Long close(fd);
196fc58801cSScott Long return (0);
197fc58801cSScott Long }
198fc58801cSScott Long MPT_COMMAND(volume, status, volume_status);
199fc58801cSScott Long
200fc58801cSScott Long static int
volume_cache(int ac,char ** av)201fc58801cSScott Long volume_cache(int ac, char **av)
202fc58801cSScott Long {
203fc58801cSScott Long CONFIG_PAGE_RAID_VOL_0 *volume;
204fc58801cSScott Long U32 Settings, NewSettings;
205fc58801cSScott Long U8 VolumeBus, VolumeID;
206fc58801cSScott Long char *s1;
207c5f2b79dSJohn Baldwin int error, fd;
208fc58801cSScott Long
209fc58801cSScott Long if (ac != 3) {
210fc58801cSScott Long warnx("volume cache: %s", ac > 3 ? "extra arguments" :
211c5f2b79dSJohn Baldwin "missing arguments");
212fc58801cSScott Long return (EINVAL);
213fc58801cSScott Long }
214fc58801cSScott Long
215fc58801cSScott Long for (s1 = av[2]; *s1 != '\0'; s1++)
216fc58801cSScott Long *s1 = tolower(*s1);
217fc58801cSScott Long if ((strcmp(av[2], "enable")) && (strcmp(av[2], "enabled")) &&
218fc58801cSScott Long (strcmp(av[2], "disable")) && (strcmp(av[2], "disabled"))) {
219*9a49a379SEnji Cooper warnx("volume cache: invalid flag; "
220*9a49a379SEnji Cooper "must be 'enable', 'enabled', 'disable', or 'disabled'");
221fc58801cSScott Long return (EINVAL);
222fc58801cSScott Long }
223fc58801cSScott Long
224fc58801cSScott Long fd = mpt_open(mpt_unit);
225fc58801cSScott Long if (fd < 0) {
226c5f2b79dSJohn Baldwin error = errno;
227fc58801cSScott Long warn("mpt_open");
228c5f2b79dSJohn Baldwin return (error);
229fc58801cSScott Long }
230fc58801cSScott Long
231c5f2b79dSJohn Baldwin error = mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID);
232c5f2b79dSJohn Baldwin if (error) {
233c5f2b79dSJohn Baldwin warnc(error, "Invalid volume: %s", av[1]);
234d68b76a9SAlan Somers close(fd);
235c5f2b79dSJohn Baldwin return (error);
236fc58801cSScott Long }
237fc58801cSScott Long
238fc58801cSScott Long volume = mpt_vol_info(fd, VolumeBus, VolumeID, NULL);
239d68b76a9SAlan Somers if (volume == NULL) {
240d68b76a9SAlan Somers close(fd);
241c5f2b79dSJohn Baldwin return (errno);
242d68b76a9SAlan Somers }
243fc58801cSScott Long
244fc58801cSScott Long Settings = volume->VolumeSettings.Settings;
245fc58801cSScott Long
246fc58801cSScott Long NewSettings = Settings;
2475a9c7248SRose if (strncmp(av[2], "enable", strlen("enable")) == 0)
248fc58801cSScott Long NewSettings |= 0x01;
2495a9c7248SRose else if (strncmp(av[2], "disable", strlen("disable")) == 0)
250fc58801cSScott Long NewSettings &= ~0x01;
251fc58801cSScott Long
252fc58801cSScott Long if (NewSettings == Settings) {
253c5f2b79dSJohn Baldwin warnx("volume cache unchanged");
254d68b76a9SAlan Somers free(volume);
255fc58801cSScott Long close(fd);
256fc58801cSScott Long return (0);
257fc58801cSScott Long }
258fc58801cSScott Long
259fc58801cSScott Long volume->VolumeSettings.Settings = NewSettings;
260c5f2b79dSJohn Baldwin error = mpt_raid_action(fd, MPI_RAID_ACTION_CHANGE_VOLUME_SETTINGS,
261fc58801cSScott Long VolumeBus, VolumeID, 0, *(U32 *)&volume->VolumeSettings, NULL, 0,
262c5f2b79dSJohn Baldwin NULL, NULL, 0, NULL, NULL, 0);
263c5f2b79dSJohn Baldwin if (error)
264c5f2b79dSJohn Baldwin warnc(error, "volume cache change failed");
265fc58801cSScott Long
266fc58801cSScott Long close(fd);
267c5f2b79dSJohn Baldwin return (error);
268fc58801cSScott Long }
269fc58801cSScott Long MPT_COMMAND(volume, cache, volume_cache);
270