mpt_drive.c revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2008 Yahoo!, Inc.
5 * All rights reserved.
6 * Written by: John Baldwin <jhb@FreeBSD.org>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__RCSID("$FreeBSD: stable/11/usr.sbin/mptutil/mpt_drive.c 330897 2018-03-14 03:19:51Z eadler $");
35
36#include <sys/param.h>
37#include <sys/errno.h>
38#include <ctype.h>
39#include <err.h>
40#include <libutil.h>
41#include <limits.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <strings.h>
46#include <unistd.h>
47
48#include <camlib.h>
49#include <cam/scsi/scsi_all.h>
50
51#include "mptutil.h"
52
53const char *
54mpt_pdstate(CONFIG_PAGE_RAID_PHYS_DISK_0 *info)
55{
56	static char buf[16];
57
58	switch (info->PhysDiskStatus.State) {
59	case MPI_PHYSDISK0_STATUS_ONLINE:
60		if ((info->PhysDiskStatus.Flags &
61		    MPI_PHYSDISK0_STATUS_FLAG_OUT_OF_SYNC) &&
62		    info->PhysDiskSettings.HotSparePool == 0)
63			return ("REBUILD");
64		else
65			return ("ONLINE");
66	case MPI_PHYSDISK0_STATUS_MISSING:
67		return ("MISSING");
68	case MPI_PHYSDISK0_STATUS_NOT_COMPATIBLE:
69		return ("NOT COMPATIBLE");
70	case MPI_PHYSDISK0_STATUS_FAILED:
71		return ("FAILED");
72	case MPI_PHYSDISK0_STATUS_INITIALIZING:
73		return ("INITIALIZING");
74	case MPI_PHYSDISK0_STATUS_OFFLINE_REQUESTED:
75		return ("OFFLINE REQUESTED");
76	case MPI_PHYSDISK0_STATUS_FAILED_REQUESTED:
77		return ("FAILED REQUESTED");
78	case MPI_PHYSDISK0_STATUS_OTHER_OFFLINE:
79		return ("OTHER OFFLINE");
80	default:
81		sprintf(buf, "PSTATE 0x%02x", info->PhysDiskStatus.State);
82		return (buf);
83	}
84}
85
86/*
87 * There are several ways to enumerate physical disks.  Unfortunately,
88 * none of them are truly complete, so we have to build a union of all of
89 * them.  Specifically:
90 *
91 * - IOC2 : This gives us a list of volumes, and by walking the volumes we
92 *          can enumerate all of the drives attached to volumes including
93 *          online drives and failed drives.
94 * - IOC3 : This gives us a list of all online physical drives including
95 *          drives that are not part of a volume nor a spare drive.  It
96 *          does not include any failed drives.
97 * - IOC5 : This gives us a list of all spare drives including failed
98 *          spares.
99 *
100 * The specific edge cases are that 1) a failed volume member can only be
101 * found via IOC2, 2) a drive that is neither a volume member nor a spare
102 * can only be found via IOC3, and 3) a failed spare can only be found via
103 * IOC5.
104 *
105 * To handle this, walk all of the three lists and use the following
106 * routine to add each drive encountered.  It quietly succeeds if the
107 * drive is already present in the list.  It also sorts the list as it
108 * inserts new drives.
109 */
110static int
111mpt_pd_insert(int fd, struct mpt_drive_list *list, U8 PhysDiskNum)
112{
113	int i, j;
114
115	/*
116	 * First, do a simple linear search to see if we have already
117	 * seen this drive.
118	 */
119	for (i = 0; i < list->ndrives; i++) {
120		if (list->drives[i]->PhysDiskNum == PhysDiskNum)
121			return (0);
122		if (list->drives[i]->PhysDiskNum > PhysDiskNum)
123			break;
124	}
125
126	/*
127	 * 'i' is our slot for the 'new' drive.  Make room and then
128	 * read the drive info.
129	 */
130	for (j = list->ndrives - 1; j >= i; j--)
131		list->drives[j + 1] = list->drives[j];
132	list->drives[i] = mpt_pd_info(fd, PhysDiskNum, NULL);
133	if (list->drives[i] == NULL)
134		return (errno);
135	list->ndrives++;
136	return (0);
137}
138
139struct mpt_drive_list *
140mpt_pd_list(int fd)
141{
142	CONFIG_PAGE_IOC_2 *ioc2;
143	CONFIG_PAGE_IOC_2_RAID_VOL *vol;
144	CONFIG_PAGE_RAID_VOL_0 **volumes;
145	RAID_VOL0_PHYS_DISK *rdisk;
146	CONFIG_PAGE_IOC_3 *ioc3;
147	IOC_3_PHYS_DISK *disk;
148	CONFIG_PAGE_IOC_5 *ioc5;
149	IOC_5_HOT_SPARE *spare;
150	struct mpt_drive_list *list;
151	int count, error, i, j;
152
153	ioc2 = mpt_read_ioc_page(fd, 2, NULL);
154	if (ioc2 == NULL) {
155		error = errno;
156		warn("Failed to fetch volume list");
157		errno = error;
158		return (NULL);
159	}
160
161	ioc3 = mpt_read_ioc_page(fd, 3, NULL);
162	if (ioc3 == NULL) {
163		error = errno;
164		warn("Failed to fetch drive list");
165		free(ioc2);
166		errno = error;
167		return (NULL);
168	}
169
170	ioc5 = mpt_read_ioc_page(fd, 5, NULL);
171	if (ioc5 == NULL) {
172		error = errno;
173		warn("Failed to fetch spare list");
174		free(ioc3);
175		free(ioc2);
176		errno = error;
177		return (NULL);
178	}
179
180	/*
181	 * Go ahead and read the info for all the volumes.  For this
182	 * pass we figure out how many physical drives there are.
183	 */
184	volumes = malloc(sizeof(*volumes) * ioc2->NumActiveVolumes);
185	count = 0;
186	vol = ioc2->RaidVolume;
187	for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) {
188		volumes[i] = mpt_vol_info(fd, vol->VolumeBus, vol->VolumeID,
189		    NULL);
190		if (volumes[i] == NULL) {
191			error = errno;
192			warn("Failed to read volume info");
193			errno = error;
194			return (NULL);
195		}
196		count += volumes[i]->NumPhysDisks;
197	}
198	count += ioc3->NumPhysDisks;
199	count += ioc5->NumHotSpares;
200
201	/* Walk the various lists enumerating drives. */
202	list = malloc(sizeof(*list) + sizeof(CONFIG_PAGE_RAID_PHYS_DISK_0) *
203	    count);
204	list->ndrives = 0;
205
206	for (i = 0; i < ioc2->NumActiveVolumes; i++) {
207		rdisk = volumes[i]->PhysDisk;
208		for (j = 0; j < volumes[i]->NumPhysDisks; rdisk++, j++)
209			if (mpt_pd_insert(fd, list, rdisk->PhysDiskNum) < 0)
210				return (NULL);
211		free(volumes[i]);
212	}
213	free(ioc2);
214	free(volumes);
215
216	spare = ioc5->HotSpare;
217	for (i = 0; i < ioc5->NumHotSpares; spare++, i++)
218		if (mpt_pd_insert(fd, list, spare->PhysDiskNum) < 0)
219			return (NULL);
220	free(ioc5);
221
222	disk = ioc3->PhysDisk;
223	for (i = 0; i < ioc3->NumPhysDisks; disk++, i++)
224		if (mpt_pd_insert(fd, list, disk->PhysDiskNum) < 0)
225			return (NULL);
226	free(ioc3);
227
228	return (list);
229}
230
231void
232mpt_free_pd_list(struct mpt_drive_list *list)
233{
234	int i;
235
236	for (i = 0; i < list->ndrives; i++)
237		free(list->drives[i]);
238	free(list);
239}
240
241int
242mpt_lookup_drive(struct mpt_drive_list *list, const char *drive,
243    U8 *PhysDiskNum)
244{
245	long val;
246	uint8_t bus, id;
247	char *cp;
248
249	/* Look for a raw device id first. */
250	val = strtol(drive, &cp, 0);
251	if (*cp == '\0') {
252		if (val < 0 || val > 0xff)
253			goto bad;
254		*PhysDiskNum = val;
255		return (0);
256	}
257
258	/* Look for a <bus>:<id> string. */
259	if (*cp == ':') {
260		if (val < 0 || val > 0xff)
261			goto bad;
262		bus = val;
263		val = strtol(cp + 1, &cp, 0);
264		if (*cp != '\0')
265			goto bad;
266		if (val < 0 || val > 0xff)
267			goto bad;
268		id = val;
269
270		for (val = 0; val < list->ndrives; val++) {
271			if (list->drives[val]->PhysDiskBus == bus &&
272			    list->drives[val]->PhysDiskID == id) {
273				*PhysDiskNum = list->drives[val]->PhysDiskNum;
274				return (0);
275			}
276		}
277		return (ENOENT);
278	}
279
280bad:
281	return (EINVAL);
282}
283
284/* Borrowed heavily from scsi_all.c:scsi_print_inquiry(). */
285const char *
286mpt_pd_inq_string(CONFIG_PAGE_RAID_PHYS_DISK_0 *pd_info)
287{
288	RAID_PHYS_DISK0_INQUIRY_DATA *inq_data;
289	u_char vendor[9], product[17], revision[5];
290	static char inq_string[64];
291
292	inq_data = &pd_info->InquiryData;
293	cam_strvis(vendor, inq_data->VendorID, sizeof(inq_data->VendorID),
294	    sizeof(vendor));
295	cam_strvis(product, inq_data->ProductID, sizeof(inq_data->ProductID),
296	    sizeof(product));
297	cam_strvis(revision, inq_data->ProductRevLevel,
298	    sizeof(inq_data->ProductRevLevel), sizeof(revision));
299
300	/* Total hack. */
301	if (strcmp(vendor, "ATA") == 0)
302		snprintf(inq_string, sizeof(inq_string), "<%s %s> SATA",
303		    product, revision);
304	else
305		snprintf(inq_string, sizeof(inq_string), "<%s %s %s> SAS",
306		    vendor, product, revision);
307	return (inq_string);
308}
309
310/* Helper function to set a drive to a given state. */
311static int
312drive_set_state(char *drive, U8 Action, U8 State, const char *name)
313{
314	CONFIG_PAGE_RAID_PHYS_DISK_0 *info;
315	struct mpt_drive_list *list;
316	U8 PhysDiskNum;
317	int error, fd;
318
319	fd = mpt_open(mpt_unit);
320	if (fd < 0) {
321		error = errno;
322		warn("mpt_open");
323		return (error);
324	}
325
326	list = mpt_pd_list(fd);
327	if (list == NULL)
328		return (errno);
329
330	if (mpt_lookup_drive(list, drive, &PhysDiskNum) < 0) {
331		error = errno;
332		warn("Failed to find drive %s", drive);
333		return (error);
334	}
335	mpt_free_pd_list(list);
336
337	/* Get the info for this drive. */
338	info = mpt_pd_info(fd, PhysDiskNum, NULL);
339	if (info == NULL) {
340		error = errno;
341		warn("Failed to fetch info for drive %u", PhysDiskNum);
342		return (error);
343	}
344
345	/* Try to change the state. */
346	if (info->PhysDiskStatus.State == State) {
347		warnx("Drive %u is already in the desired state", PhysDiskNum);
348		return (EINVAL);
349	}
350
351	error = mpt_raid_action(fd, Action, 0, 0, PhysDiskNum, 0, NULL, 0, NULL,
352	    NULL, 0, NULL, NULL, 0);
353	if (error) {
354		warnc(error, "Failed to set drive %u to %s", PhysDiskNum, name);
355		return (error);
356	}
357
358	free(info);
359	close(fd);
360
361	return (0);
362}
363
364static int
365fail_drive(int ac, char **av)
366{
367
368	if (ac != 2) {
369		warnx("fail: %s", ac > 2 ? "extra arguments" :
370		    "drive required");
371		return (EINVAL);
372	}
373
374	return (drive_set_state(av[1], MPI_RAID_ACTION_FAIL_PHYSDISK,
375	    MPI_PHYSDISK0_STATUS_FAILED_REQUESTED, "FAILED"));
376}
377MPT_COMMAND(top, fail, fail_drive);
378
379static int
380online_drive(int ac, char **av)
381{
382
383	if (ac != 2) {
384		warnx("online: %s", ac > 2 ? "extra arguments" :
385		    "drive required");
386		return (EINVAL);
387	}
388
389	return (drive_set_state(av[1], MPI_RAID_ACTION_PHYSDISK_ONLINE,
390	    MPI_PHYSDISK0_STATUS_ONLINE, "ONLINE"));
391}
392MPT_COMMAND(top, online, online_drive);
393
394static int
395offline_drive(int ac, char **av)
396{
397
398	if (ac != 2) {
399		warnx("offline: %s", ac > 2 ? "extra arguments" :
400		    "drive required");
401		return (EINVAL);
402	}
403
404	return (drive_set_state(av[1], MPI_RAID_ACTION_PHYSDISK_OFFLINE,
405	    MPI_PHYSDISK0_STATUS_OFFLINE_REQUESTED, "OFFLINE"));
406}
407MPT_COMMAND(top, offline, offline_drive);
408