xref: /freebsd/usr.sbin/mptutil/mpt_config.c (revision d90e14fc18e8d276b9b4318b4ca0e16f015c2658)
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 <fcntl.h>
37fc58801cSScott Long #include <libutil.h>
38fc58801cSScott Long #include <paths.h>
39fc58801cSScott Long #ifdef DEBUG
40fc58801cSScott Long #include <stdint.h>
41fc58801cSScott Long #endif
42fc58801cSScott Long #include <stdio.h>
43fc58801cSScott Long #include <stdlib.h>
44fc58801cSScott Long #include <string.h>
45fc58801cSScott Long #include <unistd.h>
46fc58801cSScott Long #include "mptutil.h"
47fc58801cSScott Long 
48fc58801cSScott Long #ifdef DEBUG
49fc58801cSScott Long static void	dump_config(CONFIG_PAGE_RAID_VOL_0 *vol);
50fc58801cSScott Long #endif
51fc58801cSScott Long 
52fc58801cSScott Long static long
dehumanize(const char * value)53fc58801cSScott Long dehumanize(const char *value)
54fc58801cSScott Long {
55fc58801cSScott Long         char    *vtp;
56fc58801cSScott Long         long    iv;
57fc58801cSScott Long 
58fc58801cSScott Long         if (value == NULL)
59fc58801cSScott Long                 return (0);
60fc58801cSScott Long         iv = strtoq(value, &vtp, 0);
61fc58801cSScott Long         if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0')) {
62fc58801cSScott Long                 return (0);
63fc58801cSScott Long         }
64fc58801cSScott Long         switch (vtp[0]) {
65fc58801cSScott Long         case 't': case 'T':
66fc58801cSScott Long                 iv *= 1024;
67d68b76a9SAlan Somers                 /* FALLTHROUGH */
68fc58801cSScott Long         case 'g': case 'G':
69fc58801cSScott Long                 iv *= 1024;
70d68b76a9SAlan Somers                 /* FALLTHROUGH */
71fc58801cSScott Long         case 'm': case 'M':
72fc58801cSScott Long                 iv *= 1024;
73d68b76a9SAlan Somers                 /* FALLTHROUGH */
74fc58801cSScott Long         case 'k': case 'K':
75fc58801cSScott Long                 iv *= 1024;
76d68b76a9SAlan Somers                 /* FALLTHROUGH */
77fc58801cSScott Long         case '\0':
78fc58801cSScott Long                 break;
79fc58801cSScott Long         default:
80fc58801cSScott Long                 return (0);
81fc58801cSScott Long         }
82fc58801cSScott Long         return (iv);
83fc58801cSScott Long }
84fc58801cSScott Long 
85fc58801cSScott Long /*
86fc58801cSScott Long  * Lock the volume by opening its /dev device read/write.  This will
87fc58801cSScott Long  * only work if nothing else has it opened (including mounts).  We
88fc58801cSScott Long  * leak the fd on purpose since this application is not long-running.
89fc58801cSScott Long  */
90fc58801cSScott Long int
mpt_lock_volume(U8 VolumeBus,U8 VolumeID)91fc58801cSScott Long mpt_lock_volume(U8 VolumeBus, U8 VolumeID)
92fc58801cSScott Long {
93fc58801cSScott Long 	char path[MAXPATHLEN];
94fc58801cSScott Long 	struct mpt_query_disk qd;
95fc58801cSScott Long 	int error, vfd;
96fc58801cSScott Long 
97fc58801cSScott Long 	error = mpt_query_disk(VolumeBus, VolumeID, &qd);
98fc58801cSScott Long 	if (error == ENOENT)
99fc58801cSScott Long 		/*
100fc58801cSScott Long 		 * This means there isn't a CAM device associated with
101fc58801cSScott Long 		 * the volume, and thus it is already implicitly
102fc58801cSScott Long 		 * locked, so just return.
103fc58801cSScott Long 		 */
104fc58801cSScott Long 		return (0);
105fc58801cSScott Long 	if (error) {
106c5f2b79dSJohn Baldwin 		warnc(error, "Unable to lookup volume device name");
107c5f2b79dSJohn Baldwin 		return (error);
108fc58801cSScott Long 	}
109fc58801cSScott Long 	snprintf(path, sizeof(path), "%s%s", _PATH_DEV, qd.devname);
110fc58801cSScott Long 	vfd = open(path, O_RDWR);
111fc58801cSScott Long 	if (vfd < 0) {
112c5f2b79dSJohn Baldwin 		error = errno;
113fc58801cSScott Long 		warn("Unable to lock volume %s", qd.devname);
114c5f2b79dSJohn Baldwin 		return (error);
115fc58801cSScott Long 	}
116fc58801cSScott Long 	return (0);
117fc58801cSScott Long }
118fc58801cSScott Long 
119fc58801cSScott Long static int
mpt_lock_physdisk(struct mpt_standalone_disk * disk)120fc58801cSScott Long mpt_lock_physdisk(struct mpt_standalone_disk *disk)
121fc58801cSScott Long {
122fc58801cSScott Long 	char path[MAXPATHLEN];
123c5f2b79dSJohn Baldwin 	int dfd, error;
124fc58801cSScott Long 
125fc58801cSScott Long 	snprintf(path, sizeof(path), "%s%s", _PATH_DEV, disk->devname);
126fc58801cSScott Long 	dfd = open(path, O_RDWR);
127fc58801cSScott Long 	if (dfd < 0) {
128c5f2b79dSJohn Baldwin 		error = errno;
129fc58801cSScott Long 		warn("Unable to lock disk %s", disk->devname);
130c5f2b79dSJohn Baldwin 		return (error);
131fc58801cSScott Long 	}
132fc58801cSScott Long 	return (0);
133fc58801cSScott Long }
134fc58801cSScott Long 
135fc58801cSScott Long static int
mpt_lookup_standalone_disk(const char * name,struct mpt_standalone_disk * disks,int ndisks,int * index)136fc58801cSScott Long mpt_lookup_standalone_disk(const char *name, struct mpt_standalone_disk *disks,
137fc58801cSScott Long     int ndisks, int *index)
138fc58801cSScott Long {
139fc58801cSScott Long 	char *cp;
140fc58801cSScott Long 	long bus, id;
141fc58801cSScott Long 	int i;
142fc58801cSScott Long 
143fc58801cSScott Long 	/* Check for a raw <bus>:<id> string. */
144fc58801cSScott Long 	bus = strtol(name, &cp, 0);
145fc58801cSScott Long 	if (*cp == ':') {
146fc58801cSScott Long 		id = strtol(cp + 1, &cp, 0);
147fc58801cSScott Long 		if (*cp == '\0') {
148fc58801cSScott Long 			if (bus < 0 || bus > 0xff || id < 0 || id > 0xff) {
149c5f2b79dSJohn Baldwin 				return (EINVAL);
150fc58801cSScott Long 			}
151fc58801cSScott Long 			for (i = 0; i < ndisks; i++) {
152fc58801cSScott Long 				if (disks[i].bus == (U8)bus &&
153fc58801cSScott Long 				    disks[i].target == (U8)id) {
154fc58801cSScott Long 					*index = i;
155fc58801cSScott Long 					return (0);
156fc58801cSScott Long 				}
157fc58801cSScott Long 			}
158c5f2b79dSJohn Baldwin 			return (ENOENT);
159fc58801cSScott Long 		}
160fc58801cSScott Long 	}
161fc58801cSScott Long 
162fc58801cSScott Long 	if (name[0] == 'd' && name[1] == 'a') {
163fc58801cSScott Long 		for (i = 0; i < ndisks; i++) {
164fc58801cSScott Long 			if (strcmp(name, disks[i].devname) == 0) {
165fc58801cSScott Long 				*index = i;
166fc58801cSScott Long 				return (0);
167fc58801cSScott Long 			}
168fc58801cSScott Long 		}
169c5f2b79dSJohn Baldwin 		return (ENOENT);
170fc58801cSScott Long 	}
171fc58801cSScott Long 
172c5f2b79dSJohn Baldwin 	return (EINVAL);
173fc58801cSScott Long }
174fc58801cSScott Long 
175fc58801cSScott Long /*
176fc58801cSScott Long  * Mark a standalone disk as being a physical disk.
177fc58801cSScott Long  */
178fc58801cSScott Long static int
mpt_create_physdisk(int fd,struct mpt_standalone_disk * disk,U8 * PhysDiskNum)179fc58801cSScott Long mpt_create_physdisk(int fd, struct mpt_standalone_disk *disk, U8 *PhysDiskNum)
180fc58801cSScott Long {
181fc58801cSScott Long 	CONFIG_PAGE_HEADER header;
182fc58801cSScott Long 	CONFIG_PAGE_RAID_PHYS_DISK_0 *config_page;
183c5f2b79dSJohn Baldwin 	int error;
184fc58801cSScott Long 	U32 ActionData;
185fc58801cSScott Long 
186c5f2b79dSJohn Baldwin 	error = mpt_read_config_page_header(fd, MPI_CONFIG_PAGETYPE_RAID_PHYSDISK,
187c5f2b79dSJohn Baldwin 	    0, 0, &header, NULL);
188c5f2b79dSJohn Baldwin 	if (error)
189c5f2b79dSJohn Baldwin 		return (error);
190fc58801cSScott Long 	if (header.PageVersion > MPI_RAIDPHYSDISKPAGE0_PAGEVERSION) {
191fc58801cSScott Long 		warnx("Unsupported RAID physdisk page 0 version %d",
192fc58801cSScott Long 		    header.PageVersion);
193c5f2b79dSJohn Baldwin 		return (EOPNOTSUPP);
194fc58801cSScott Long 	}
195fc58801cSScott Long 	config_page = calloc(1, sizeof(CONFIG_PAGE_RAID_PHYS_DISK_0));
196fc58801cSScott Long 	config_page->Header.PageType = MPI_CONFIG_PAGETYPE_RAID_PHYSDISK;
197fc58801cSScott Long 	config_page->Header.PageNumber = 0;
198fc58801cSScott Long 	config_page->Header.PageLength = sizeof(CONFIG_PAGE_RAID_PHYS_DISK_0) /
199fc58801cSScott Long 	    4;
200fc58801cSScott Long 	config_page->PhysDiskIOC = 0;	/* XXX */
201fc58801cSScott Long 	config_page->PhysDiskBus = disk->bus;
202fc58801cSScott Long 	config_page->PhysDiskID = disk->target;
203fc58801cSScott Long 
204fc58801cSScott Long 	/* XXX: Enclosure info for PhysDiskSettings? */
205c5f2b79dSJohn Baldwin 	error = mpt_raid_action(fd, MPI_RAID_ACTION_CREATE_PHYSDISK, 0, 0, 0, 0,
206fc58801cSScott Long 	    config_page, sizeof(CONFIG_PAGE_RAID_PHYS_DISK_0), NULL,
207c5f2b79dSJohn Baldwin 	    &ActionData, sizeof(ActionData), NULL, NULL, 1);
208c5f2b79dSJohn Baldwin 	if (error)
209c5f2b79dSJohn Baldwin 		return (error);
210fc58801cSScott Long 	*PhysDiskNum = ActionData & 0xff;
211fc58801cSScott Long 	return (0);
212fc58801cSScott Long }
213fc58801cSScott Long 
214fc58801cSScott Long static int
mpt_delete_physdisk(int fd,U8 PhysDiskNum)215fc58801cSScott Long mpt_delete_physdisk(int fd, U8 PhysDiskNum)
216fc58801cSScott Long {
217fc58801cSScott Long 
218fc58801cSScott Long 	return (mpt_raid_action(fd, MPI_RAID_ACTION_DELETE_PHYSDISK, 0, 0,
219fc58801cSScott Long 	    PhysDiskNum, 0, NULL, 0, NULL, NULL, 0, NULL, NULL, 0));
220fc58801cSScott Long }
221fc58801cSScott Long 
222fc58801cSScott Long /*
223fc58801cSScott Long  * MPT's firmware does not have a clear command.  Instead, we
224fc58801cSScott Long  * implement it by deleting each array and disk by hand.
225fc58801cSScott Long  */
226fc58801cSScott Long static int
clear_config(int ac,char ** av)227fc58801cSScott Long clear_config(int ac, char **av)
228fc58801cSScott Long {
229fc58801cSScott Long 	CONFIG_PAGE_IOC_2 *ioc2;
230fc58801cSScott Long 	CONFIG_PAGE_IOC_2_RAID_VOL *vol;
231fc58801cSScott Long 	CONFIG_PAGE_IOC_3 *ioc3;
232fc58801cSScott Long 	IOC_3_PHYS_DISK *disk;
233fc58801cSScott Long 	CONFIG_PAGE_IOC_5 *ioc5;
234fc58801cSScott Long 	IOC_5_HOT_SPARE *spare;
235c5f2b79dSJohn Baldwin 	int ch, error, fd, i;
236fc58801cSScott Long 
237fc58801cSScott Long 	fd = mpt_open(mpt_unit);
238fc58801cSScott Long 	if (fd < 0) {
239c5f2b79dSJohn Baldwin 		error = errno;
240fc58801cSScott Long 		warn("mpt_open");
241c5f2b79dSJohn Baldwin 		return (error);
242fc58801cSScott Long 	}
243fc58801cSScott Long 
244fc58801cSScott Long 	ioc2 = mpt_read_ioc_page(fd, 2, NULL);
245fc58801cSScott Long 	if (ioc2 == NULL) {
246c5f2b79dSJohn Baldwin 		error = errno;
247fc58801cSScott Long 		warn("Failed to fetch volume list");
248d68b76a9SAlan Somers 		close(fd);
249c5f2b79dSJohn Baldwin 		return (error);
250fc58801cSScott Long 	}
251fc58801cSScott Long 
252fc58801cSScott Long 	/* Lock all the volumes first. */
253fc58801cSScott Long 	vol = ioc2->RaidVolume;
254fc58801cSScott Long 	for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) {
255fc58801cSScott Long 		if (mpt_lock_volume(vol->VolumeBus, vol->VolumeID) < 0) {
256fc58801cSScott Long 			warnx("Volume %s is busy and cannot be deleted",
257fc58801cSScott Long 			    mpt_volume_name(vol->VolumeBus, vol->VolumeID));
258d68b76a9SAlan Somers 			free(ioc2);
259d68b76a9SAlan Somers 			close(fd);
260fc58801cSScott Long 			return (EBUSY);
261fc58801cSScott Long 		}
262fc58801cSScott Long 	}
263fc58801cSScott Long 
264fc58801cSScott Long 	printf(
265fc58801cSScott Long 	    "Are you sure you wish to clear the configuration on mpt%u? [y/N] ",
266fc58801cSScott Long 	    mpt_unit);
267fc58801cSScott Long 	ch = getchar();
268fc58801cSScott Long 	if (ch != 'y' && ch != 'Y') {
269fc58801cSScott Long 		printf("\nAborting\n");
270d68b76a9SAlan Somers 		free(ioc2);
271d68b76a9SAlan Somers 		close(fd);
272fc58801cSScott Long 		return (0);
273fc58801cSScott Long 	}
274fc58801cSScott Long 
275fc58801cSScott Long 	/* Delete all the volumes. */
276fc58801cSScott Long 	vol = ioc2->RaidVolume;
277c5f2b79dSJohn Baldwin 	for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) {
278c5f2b79dSJohn Baldwin 		error = mpt_raid_action(fd, MPI_RAID_ACTION_DELETE_VOLUME,
279fc58801cSScott Long 		    vol->VolumeBus, vol->VolumeID, 0,
280fc58801cSScott Long 		    MPI_RAID_ACTION_ADATA_DEL_PHYS_DISKS |
281fc58801cSScott Long 		    MPI_RAID_ACTION_ADATA_ZERO_LBA0, NULL, 0, NULL, NULL, 0,
282c5f2b79dSJohn Baldwin 		    NULL, NULL, 0);
283c5f2b79dSJohn Baldwin 		if (error)
284c5f2b79dSJohn Baldwin 			warnc(error, "Failed to delete volume %s",
285fc58801cSScott Long 			    mpt_volume_name(vol->VolumeBus, vol->VolumeID));
286c5f2b79dSJohn Baldwin 	}
287fc58801cSScott Long 	free(ioc2);
288fc58801cSScott Long 
289fc58801cSScott Long 	/* Delete all the spares. */
290fc58801cSScott Long 	ioc5 = mpt_read_ioc_page(fd, 5, NULL);
291fc58801cSScott Long 	if (ioc5 == NULL)
292fc58801cSScott Long 		warn("Failed to fetch spare list");
293fc58801cSScott Long 	else {
294fc58801cSScott Long 		spare = ioc5->HotSpare;
295fc58801cSScott Long 		for (i = 0; i < ioc5->NumHotSpares; spare++, i++)
296fc58801cSScott Long 			if (mpt_delete_physdisk(fd, spare->PhysDiskNum) < 0)
297fc58801cSScott Long 				warn("Failed to delete physical disk %d",
298fc58801cSScott Long 				    spare->PhysDiskNum);
299fc58801cSScott Long 		free(ioc5);
300fc58801cSScott Long 	}
301fc58801cSScott Long 
302fc58801cSScott Long 	/* Delete any RAID physdisks that may be left. */
303fc58801cSScott Long 	ioc3 = mpt_read_ioc_page(fd, 3, NULL);
304fc58801cSScott Long 	if (ioc3 == NULL)
305fc58801cSScott Long 		warn("Failed to fetch drive list");
306fc58801cSScott Long 	else {
307fc58801cSScott Long 		disk = ioc3->PhysDisk;
308fc58801cSScott Long 		for (i = 0; i < ioc3->NumPhysDisks; disk++, i++)
309fc58801cSScott Long 			if (mpt_delete_physdisk(fd, disk->PhysDiskNum) < 0)
310fc58801cSScott Long 				warn("Failed to delete physical disk %d",
311fc58801cSScott Long 				    disk->PhysDiskNum);
312fc58801cSScott Long 		free(ioc3);
313fc58801cSScott Long 	}
314fc58801cSScott Long 
315fc58801cSScott Long 	printf("mpt%d: Configuration cleared\n", mpt_unit);
316*d90e14fcSEnji Cooper 	error = mpt_rescan_bus(-1, -1);
317fc58801cSScott Long 	close(fd);
318fc58801cSScott Long 
319*d90e14fcSEnji Cooper 	return (error);
320fc58801cSScott Long }
321fc58801cSScott Long MPT_COMMAND(top, clear, clear_config);
322fc58801cSScott Long 
323fc58801cSScott Long #define	RT_RAID0	0
324fc58801cSScott Long #define	RT_RAID1	1
325fc58801cSScott Long #define	RT_RAID1E	2
326fc58801cSScott Long 
327fc58801cSScott Long static struct raid_type_entry {
328fc58801cSScott Long 	const char *name;
329fc58801cSScott Long 	int	raid_type;
330fc58801cSScott Long } raid_type_table[] = {
331fc58801cSScott Long 	{ "raid0",	RT_RAID0 },
332fc58801cSScott Long 	{ "raid-0",	RT_RAID0 },
333fc58801cSScott Long 	{ "raid1",	RT_RAID1 },
334fc58801cSScott Long 	{ "raid-1",	RT_RAID1 },
335fc58801cSScott Long 	{ "mirror",	RT_RAID1 },
336fc58801cSScott Long 	{ "raid1e",	RT_RAID1E },
337fc58801cSScott Long 	{ "raid-1e",	RT_RAID1E },
338fc58801cSScott Long 	{ NULL,		0 },
339fc58801cSScott Long };
340fc58801cSScott Long 
341fc58801cSScott Long struct config_id_state {
342fc58801cSScott Long 	struct mpt_standalone_disk *sdisks;
343fc58801cSScott Long 	struct mpt_drive_list *list;
344fc58801cSScott Long 	CONFIG_PAGE_IOC_2 *ioc2;
345fc58801cSScott Long 	U8	target_id;
346fc58801cSScott Long 	int	nsdisks;
347fc58801cSScott Long };
348fc58801cSScott Long 
349fc58801cSScott Long struct drive_info {
350fc58801cSScott Long 	CONFIG_PAGE_RAID_PHYS_DISK_0 *info;
351fc58801cSScott Long 	struct mpt_standalone_disk *sdisk;
352fc58801cSScott Long };
353fc58801cSScott Long 
354fc58801cSScott Long struct volume_info {
355fc58801cSScott Long 	int	drive_count;
356fc58801cSScott Long 	struct drive_info *drives;
357fc58801cSScott Long };
358fc58801cSScott Long 
359fc58801cSScott Long /* Parse a comma-separated list of drives for a volume. */
360fc58801cSScott Long static int
parse_volume(int fd,int raid_type,struct config_id_state * state,char * volume_str,struct volume_info * info)361fc58801cSScott Long parse_volume(int fd, int raid_type, struct config_id_state *state,
362fc58801cSScott Long     char *volume_str, struct volume_info *info)
363fc58801cSScott Long {
364fc58801cSScott Long 	struct drive_info *dinfo;
365fc58801cSScott Long 	U8 PhysDiskNum;
366fc58801cSScott Long 	char *cp;
367fc58801cSScott Long 	int count, error, i;
368fc58801cSScott Long 
369fc58801cSScott Long 	cp = volume_str;
370fc58801cSScott Long 	for (count = 0; cp != NULL; count++) {
371fc58801cSScott Long 		cp = strchr(cp, ',');
372fc58801cSScott Long 		if (cp != NULL) {
373fc58801cSScott Long 			cp++;
374fc58801cSScott Long 			if (*cp == ',') {
375fc58801cSScott Long 				warnx("Invalid drive list '%s'", volume_str);
376fc58801cSScott Long 				return (EINVAL);
377fc58801cSScott Long 			}
378fc58801cSScott Long 		}
379fc58801cSScott Long 	}
380fc58801cSScott Long 
381fc58801cSScott Long 	/* Validate the number of drives for this volume. */
382fc58801cSScott Long 	switch (raid_type) {
383fc58801cSScott Long 	case RT_RAID0:
384fc58801cSScott Long 		if (count < 2) {
385fc58801cSScott Long 			warnx("RAID0 requires at least 2 drives in each "
386fc58801cSScott Long 			    "array");
387fc58801cSScott Long 			return (EINVAL);
388fc58801cSScott Long 		}
389fc58801cSScott Long 		break;
390fc58801cSScott Long 	case RT_RAID1:
391fc58801cSScott Long 		if (count != 2) {
392fc58801cSScott Long 			warnx("RAID1 requires exactly 2 drives in each "
393fc58801cSScott Long 			    "array");
394fc58801cSScott Long 			return (EINVAL);
395fc58801cSScott Long 		}
396fc58801cSScott Long 		break;
397fc58801cSScott Long 	case RT_RAID1E:
398fc58801cSScott Long 		if (count < 3) {
399fc58801cSScott Long 			warnx("RAID1E requires at least 3 drives in each "
400fc58801cSScott Long 			    "array");
401fc58801cSScott Long 			return (EINVAL);
402fc58801cSScott Long 		}
403fc58801cSScott Long 		break;
404fc58801cSScott Long 	}
405fc58801cSScott Long 
406fc58801cSScott Long 	/* Validate each drive. */
407fc58801cSScott Long 	info->drives = calloc(count, sizeof(struct drive_info));
408fc58801cSScott Long 	info->drive_count = count;
409fc58801cSScott Long 	for (dinfo = info->drives; (cp = strsep(&volume_str, ",")) != NULL;
410fc58801cSScott Long 	     dinfo++) {
411fc58801cSScott Long 		/* If this drive is already a RAID phys just fetch the info. */
412fc58801cSScott Long 		error = mpt_lookup_drive(state->list, cp, &PhysDiskNum);
413fc58801cSScott Long 		if (error == 0) {
414fc58801cSScott Long 			dinfo->info = mpt_pd_info(fd, PhysDiskNum, NULL);
415fc58801cSScott Long 			if (dinfo->info == NULL)
416fc58801cSScott Long 				return (errno);
417fc58801cSScott Long 			continue;
418fc58801cSScott Long 		}
419fc58801cSScott Long 
420fc58801cSScott Long 		/* See if it is a standalone disk. */
421fc58801cSScott Long 		if (mpt_lookup_standalone_disk(cp, state->sdisks,
422fc58801cSScott Long 		    state->nsdisks, &i) < 0) {
423c5f2b79dSJohn Baldwin 			error = errno;
424fc58801cSScott Long 			warn("Unable to lookup drive %s", cp);
425c5f2b79dSJohn Baldwin 			return (error);
426fc58801cSScott Long 		}
427fc58801cSScott Long 		dinfo->sdisk = &state->sdisks[i];
428fc58801cSScott Long 
429fc58801cSScott Long 		/* Lock the disk, we will create phys disk pages later. */
430fc58801cSScott Long 		if (mpt_lock_physdisk(dinfo->sdisk) < 0)
431fc58801cSScott Long 			return (errno);
432fc58801cSScott Long 	}
433fc58801cSScott Long 
434fc58801cSScott Long 	return (0);
435fc58801cSScott Long }
436fc58801cSScott Long 
437fc58801cSScott Long /*
438fc58801cSScott Long  * Add RAID physdisk pages for any standalone disks that a volume is
439fc58801cSScott Long  * going to use.
440fc58801cSScott Long  */
441fc58801cSScott Long static int
add_drives(int fd,struct volume_info * info,int verbose)442fc58801cSScott Long add_drives(int fd, struct volume_info *info, int verbose)
443fc58801cSScott Long {
444fc58801cSScott Long 	struct drive_info *dinfo;
445fc58801cSScott Long 	U8 PhysDiskNum;
446c5f2b79dSJohn Baldwin 	int error, i;
447fc58801cSScott Long 
448fc58801cSScott Long 	for (i = 0, dinfo = info->drives; i < info->drive_count;
449fc58801cSScott Long 	     i++, dinfo++) {
450fc58801cSScott Long 		if (dinfo->info == NULL) {
451fc58801cSScott Long 			if (mpt_create_physdisk(fd, dinfo->sdisk,
452fc58801cSScott Long 			    &PhysDiskNum) < 0) {
453c5f2b79dSJohn Baldwin 				error = errno;
454fc58801cSScott Long 				warn(
455fc58801cSScott Long 			    "Failed to create physical disk page for %s",
456fc58801cSScott Long 				    dinfo->sdisk->devname);
457c5f2b79dSJohn Baldwin 				return (error);
458fc58801cSScott Long 			}
459fc58801cSScott Long 			if (verbose)
460fc58801cSScott Long 				printf("Added drive %s with PhysDiskNum %u\n",
461fc58801cSScott Long 				    dinfo->sdisk->devname, PhysDiskNum);
462fc58801cSScott Long 
463fc58801cSScott Long 			dinfo->info = mpt_pd_info(fd, PhysDiskNum, NULL);
464fc58801cSScott Long 			if (dinfo->info == NULL)
465fc58801cSScott Long 				return (errno);
466fc58801cSScott Long 		}
467fc58801cSScott Long 	}
468fc58801cSScott Long 	return (0);
469fc58801cSScott Long }
470fc58801cSScott Long 
471fc58801cSScott Long /*
472fc58801cSScott Long  * Find the next free target ID assuming that 'target_id' is the last
473fc58801cSScott Long  * one used.  'target_id' should be 0xff for the initial test.
474fc58801cSScott Long  */
475fc58801cSScott Long static U8
find_next_volume(struct config_id_state * state)476fc58801cSScott Long find_next_volume(struct config_id_state *state)
477fc58801cSScott Long {
478fc58801cSScott Long 	CONFIG_PAGE_IOC_2_RAID_VOL *vol;
479fc58801cSScott Long 	int i;
480fc58801cSScott Long 
481fc58801cSScott Long restart:
482fc58801cSScott Long 	/* Assume the current one is used. */
483fc58801cSScott Long 	state->target_id++;
484fc58801cSScott Long 
485fc58801cSScott Long 	/* Search drives first. */
486fc58801cSScott Long 	for (i = 0; i < state->nsdisks; i++)
487fc58801cSScott Long 		if (state->sdisks[i].target == state->target_id)
488fc58801cSScott Long 			goto restart;
489fc58801cSScott Long 	for (i = 0; i < state->list->ndrives; i++)
490fc58801cSScott Long 		if (state->list->drives[i]->PhysDiskID == state->target_id)
491fc58801cSScott Long 			goto restart;
492fc58801cSScott Long 
4933df5ecacSUlrich Spörlein 	/* Search volumes second. */
494fc58801cSScott Long 	vol = state->ioc2->RaidVolume;
495fc58801cSScott Long 	for (i = 0; i < state->ioc2->NumActiveVolumes; vol++, i++)
496fc58801cSScott Long 		if (vol->VolumeID == state->target_id)
497fc58801cSScott Long 			goto restart;
498fc58801cSScott Long 
499fc58801cSScott Long 	return (state->target_id);
500fc58801cSScott Long }
501fc58801cSScott Long 
502fc58801cSScott Long /* Create a volume and populate it with drives. */
503fc58801cSScott Long static CONFIG_PAGE_RAID_VOL_0 *
build_volume(int fd,struct volume_info * info,int raid_type,long stripe_size,struct config_id_state * state,int verbose)504fc58801cSScott Long build_volume(int fd, struct volume_info *info, int raid_type, long stripe_size,
505fc58801cSScott Long     struct config_id_state *state, int verbose)
506fc58801cSScott Long {
507fc58801cSScott Long 	CONFIG_PAGE_HEADER header;
508fc58801cSScott Long 	CONFIG_PAGE_RAID_VOL_0 *vol;
509fc58801cSScott Long 	RAID_VOL0_PHYS_DISK *rdisk;
510fc58801cSScott Long 	struct drive_info *dinfo;
511fc58801cSScott Long         U32 MinLBA;
512fc58801cSScott Long 	uint64_t MaxLBA;
513fc58801cSScott Long 	size_t page_size;
514c5f2b79dSJohn Baldwin 	int error, i;
515fc58801cSScott Long 
516c5f2b79dSJohn Baldwin 	error = mpt_read_config_page_header(fd, MPI_CONFIG_PAGETYPE_RAID_VOLUME,
517c5f2b79dSJohn Baldwin 	    0, 0, &header, NULL);
518c5f2b79dSJohn Baldwin 	if (error) {
519c5f2b79dSJohn Baldwin 		errno = error;
520fc58801cSScott Long 		return (NULL);
521c5f2b79dSJohn Baldwin 	}
522fc58801cSScott Long 	if (header.PageVersion > MPI_RAIDVOLPAGE0_PAGEVERSION) {
523fc58801cSScott Long 		warnx("Unsupported RAID volume page 0 version %d",
524fc58801cSScott Long 		    header.PageVersion);
525fc58801cSScott Long 		errno = EOPNOTSUPP;
526fc58801cSScott Long 		return (NULL);
527fc58801cSScott Long 	}
528fc58801cSScott Long 	page_size = sizeof(CONFIG_PAGE_RAID_VOL_0) +
529fc58801cSScott Long 	    sizeof(RAID_VOL0_PHYS_DISK) * (info->drive_count - 1);
530fc58801cSScott Long 	vol = calloc(1, page_size);
531c5f2b79dSJohn Baldwin 	if (vol == NULL)
532c5f2b79dSJohn Baldwin 		return (NULL);
533fc58801cSScott Long 
534fc58801cSScott Long 	/* Header */
535fc58801cSScott Long 	vol->Header.PageType = MPI_CONFIG_PAGETYPE_RAID_VOLUME;
536fc58801cSScott Long 	vol->Header.PageNumber = 0;
537fc58801cSScott Long 	vol->Header.PageLength = page_size / 4;
538fc58801cSScott Long 
539fc58801cSScott Long 	/* Properties */
540fc58801cSScott Long 	vol->VolumeID = find_next_volume(state);
541fc58801cSScott Long 	vol->VolumeBus = 0;
542fc58801cSScott Long 	vol->VolumeIOC = 0;	/* XXX */
543fc58801cSScott Long 	vol->VolumeStatus.Flags = MPI_RAIDVOL0_STATUS_FLAG_ENABLED;
544fc58801cSScott Long 	vol->VolumeStatus.State = MPI_RAIDVOL0_STATUS_STATE_OPTIMAL;
545fc58801cSScott Long 	vol->VolumeSettings.Settings = MPI_RAIDVOL0_SETTING_USE_DEFAULTS;
546fc58801cSScott Long 	vol->VolumeSettings.HotSparePool = MPI_RAID_HOT_SPARE_POOL_0;
547fc58801cSScott Long 	vol->NumPhysDisks = info->drive_count;
548fc58801cSScott Long 
549fc58801cSScott Long 	/* Find the smallest drive. */
550fc58801cSScott Long 	MinLBA = info->drives[0].info->MaxLBA;
551fc58801cSScott Long 	for (i = 1; i < info->drive_count; i++)
552fc58801cSScott Long 		if (info->drives[i].info->MaxLBA < MinLBA)
553fc58801cSScott Long 			MinLBA = info->drives[i].info->MaxLBA;
554fc58801cSScott Long 
555fc58801cSScott Long 	/*
556fc58801cSScott Long 	 * Now chop off 512MB at the end to leave room for the
557fc58801cSScott Long 	 * metadata.  The controller might only use 64MB, but we just
558fc58801cSScott Long 	 * chop off the max to be simple.
559fc58801cSScott Long 	 */
560fc58801cSScott Long 	MinLBA -= (512 * 1024 * 1024) / 512;
561fc58801cSScott Long 
562fc58801cSScott Long 	switch (raid_type) {
563fc58801cSScott Long 	case RT_RAID0:
564fc58801cSScott Long 		vol->VolumeType = MPI_RAID_VOL_TYPE_IS;
565fc58801cSScott Long 		vol->StripeSize = stripe_size / 512;
566d68b76a9SAlan Somers 		MaxLBA = (uint64_t)MinLBA * info->drive_count;
567fc58801cSScott Long 		break;
568fc58801cSScott Long 	case RT_RAID1:
569fc58801cSScott Long 		vol->VolumeType = MPI_RAID_VOL_TYPE_IM;
570d68b76a9SAlan Somers 		MaxLBA = (uint64_t)MinLBA * (info->drive_count / 2);
571fc58801cSScott Long 		break;
572fc58801cSScott Long 	case RT_RAID1E:
573fc58801cSScott Long 		vol->VolumeType = MPI_RAID_VOL_TYPE_IME;
574fc58801cSScott Long 		vol->StripeSize = stripe_size / 512;
575d68b76a9SAlan Somers 		MaxLBA = (uint64_t)MinLBA * info->drive_count / 2;
576fc58801cSScott Long 		break;
577fc58801cSScott Long 	default:
578fc58801cSScott Long 		/* Pacify gcc. */
579fc58801cSScott Long 		abort();
580fc58801cSScott Long 	}
581fc58801cSScott Long 
582fc58801cSScott Long 	/*
583fc58801cSScott Long 	 * If the controller doesn't support 64-bit addressing and the
584fc58801cSScott Long 	 * new volume is larger than 2^32 blocks, warn the user and
585fc58801cSScott Long 	 * truncate the volume.
586fc58801cSScott Long 	 */
587fc58801cSScott Long 	if (MaxLBA >> 32 != 0 &&
588fc58801cSScott Long 	    !(state->ioc2->CapabilitiesFlags &
589fc58801cSScott Long 	    MPI_IOCPAGE2_CAP_FLAGS_RAID_64_BIT_ADDRESSING)) {
590fc58801cSScott Long 		warnx(
591fc58801cSScott Long 	    "Controller does not support volumes > 2TB, truncating volume.");
592fc58801cSScott Long 		MaxLBA = 0xffffffff;
593fc58801cSScott Long 	}
594fc58801cSScott Long 	vol->MaxLBA = MaxLBA;
595fc58801cSScott Long 	vol->MaxLBAHigh = MaxLBA >> 32;
596fc58801cSScott Long 
597fc58801cSScott Long 	/* Populate drives. */
598fc58801cSScott Long 	for (i = 0, dinfo = info->drives, rdisk = vol->PhysDisk;
599fc58801cSScott Long 	     i < info->drive_count; i++, dinfo++, rdisk++) {
600fc58801cSScott Long 		if (verbose)
601fc58801cSScott Long 			printf("Adding drive %u (%u:%u) to volume %u:%u\n",
602fc58801cSScott Long 			    dinfo->info->PhysDiskNum, dinfo->info->PhysDiskBus,
603fc58801cSScott Long 			    dinfo->info->PhysDiskID, vol->VolumeBus,
604fc58801cSScott Long 			    vol->VolumeID);
605fc58801cSScott Long 		if (raid_type == RT_RAID1) {
606fc58801cSScott Long 			if (i == 0)
607fc58801cSScott Long 				rdisk->PhysDiskMap =
608fc58801cSScott Long 				    MPI_RAIDVOL0_PHYSDISK_PRIMARY;
609fc58801cSScott Long 			else
610fc58801cSScott Long 				rdisk->PhysDiskMap =
611fc58801cSScott Long 				    MPI_RAIDVOL0_PHYSDISK_SECONDARY;
612fc58801cSScott Long 		} else
613fc58801cSScott Long 			rdisk->PhysDiskMap = i;
614fc58801cSScott Long 		rdisk->PhysDiskNum = dinfo->info->PhysDiskNum;
615fc58801cSScott Long 	}
616fc58801cSScott Long 
617fc58801cSScott Long 	return (vol);
618fc58801cSScott Long }
619fc58801cSScott Long 
620fc58801cSScott Long static int
create_volume(int ac,char ** av)621fc58801cSScott Long create_volume(int ac, char **av)
622fc58801cSScott Long {
623fc58801cSScott Long 	CONFIG_PAGE_RAID_VOL_0 *vol;
624fc58801cSScott Long 	struct config_id_state state;
625fc58801cSScott Long 	struct volume_info *info;
626fc58801cSScott Long 	long stripe_size;
627c5f2b79dSJohn Baldwin 	int ch, error, fd, i, quick, raid_type, verbose;
628fc58801cSScott Long #ifdef DEBUG
629fc58801cSScott Long 	int dump;
630fc58801cSScott Long #endif
631fc58801cSScott Long 
632fc58801cSScott Long 	if (ac < 2) {
633fc58801cSScott Long 		warnx("create: volume type required");
634fc58801cSScott Long 		return (EINVAL);
635fc58801cSScott Long 	}
636fc58801cSScott Long 
637fc58801cSScott Long 	fd = mpt_open(mpt_unit);
638fc58801cSScott Long 	if (fd < 0) {
639c5f2b79dSJohn Baldwin 		error = errno;
640fc58801cSScott Long 		warn("mpt_open");
641c5f2b79dSJohn Baldwin 		return (error);
642fc58801cSScott Long 	}
643fc58801cSScott Long 
644fc58801cSScott Long 	/* Lookup the RAID type first. */
645fc58801cSScott Long 	raid_type = -1;
646fc58801cSScott Long 	for (i = 0; raid_type_table[i].name != NULL; i++)
647fc58801cSScott Long 		if (strcasecmp(raid_type_table[i].name, av[1]) == 0) {
648fc58801cSScott Long 			raid_type = raid_type_table[i].raid_type;
649fc58801cSScott Long 			break;
650fc58801cSScott Long 		}
651fc58801cSScott Long 
652fc58801cSScott Long 	if (raid_type == -1) {
653fc58801cSScott Long 		warnx("Unknown or unsupported volume type %s", av[1]);
654d68b76a9SAlan Somers 		close(fd);
655fc58801cSScott Long 		return (EINVAL);
656fc58801cSScott Long 	}
657fc58801cSScott Long 
658fc58801cSScott Long 	/* Parse any options. */
659fc58801cSScott Long 	optind = 2;
660fc58801cSScott Long #ifdef DEBUG
661fc58801cSScott Long 	dump = 0;
662fc58801cSScott Long #endif
663fc58801cSScott Long 	quick = 0;
664fc58801cSScott Long 	verbose = 0;
665fc58801cSScott Long 	stripe_size = 64 * 1024;
666fc58801cSScott Long 
667fc58801cSScott Long 	while ((ch = getopt(ac, av, "dqs:v")) != -1) {
668fc58801cSScott Long 		switch (ch) {
669fc58801cSScott Long #ifdef DEBUG
670fc58801cSScott Long 		case 'd':
671fc58801cSScott Long 			dump = 1;
672fc58801cSScott Long 			break;
673fc58801cSScott Long #endif
674fc58801cSScott Long 		case 'q':
675fc58801cSScott Long 			quick = 1;
676fc58801cSScott Long 			break;
677fc58801cSScott Long 		case 's':
678fc58801cSScott Long 			stripe_size = dehumanize(optarg);
679fc58801cSScott Long 			if ((stripe_size < 512) || (!powerof2(stripe_size))) {
680fc58801cSScott Long 				warnx("Invalid stripe size %s", optarg);
681d68b76a9SAlan Somers 				close(fd);
682fc58801cSScott Long 				return (EINVAL);
683fc58801cSScott Long 			}
684fc58801cSScott Long 			break;
685fc58801cSScott Long 		case 'v':
686fc58801cSScott Long 			verbose = 1;
687fc58801cSScott Long 			break;
688fc58801cSScott Long 		case '?':
689fc58801cSScott Long 		default:
690d68b76a9SAlan Somers 			close(fd);
691fc58801cSScott Long 			return (EINVAL);
692fc58801cSScott Long 		}
693fc58801cSScott Long 	}
694fc58801cSScott Long 	ac -= optind;
695fc58801cSScott Long 	av += optind;
696fc58801cSScott Long 
697fc58801cSScott Long 	/* Fetch existing config data. */
698fc58801cSScott Long 	state.ioc2 = mpt_read_ioc_page(fd, 2, NULL);
699fc58801cSScott Long 	if (state.ioc2 == NULL) {
700c5f2b79dSJohn Baldwin 		error = errno;
701fc58801cSScott Long 		warn("Failed to read volume list");
702d68b76a9SAlan Somers 		close(fd);
703c5f2b79dSJohn Baldwin 		return (error);
704fc58801cSScott Long 	}
705fc58801cSScott Long 	state.list = mpt_pd_list(fd);
706d68b76a9SAlan Somers 	if (state.list == NULL) {
707d68b76a9SAlan Somers 		close(fd);
708fc58801cSScott Long 		return (errno);
709d68b76a9SAlan Somers 	}
710fc58801cSScott Long 	error = mpt_fetch_disks(fd, &state.nsdisks, &state.sdisks);
711fc58801cSScott Long 	if (error) {
712fc58801cSScott Long 		warn("Failed to fetch standalone disk list");
713d68b76a9SAlan Somers 		close(fd);
714fc58801cSScott Long 		return (error);
715fc58801cSScott Long 	}
716fc58801cSScott Long 	state.target_id = 0xff;
717fc58801cSScott Long 
718fc58801cSScott Long 	/* Parse the drive list. */
719fc58801cSScott Long 	if (ac != 1) {
720fc58801cSScott Long 		warnx("Exactly one drive list is required");
721d68b76a9SAlan Somers 		close(fd);
722fc58801cSScott Long 		return (EINVAL);
723fc58801cSScott Long 	}
724fc58801cSScott Long 	info = calloc(1, sizeof(*info));
725d68b76a9SAlan Somers 	if (info == NULL) {
726d68b76a9SAlan Somers 		close(fd);
727c5f2b79dSJohn Baldwin 		return (ENOMEM);
728d68b76a9SAlan Somers 	}
729fc58801cSScott Long 	error = parse_volume(fd, raid_type, &state, av[0], info);
730d68b76a9SAlan Somers 	if (error) {
731d68b76a9SAlan Somers 		free(info);
732d68b76a9SAlan Somers 		close(fd);
733fc58801cSScott Long 		return (error);
734d68b76a9SAlan Somers 	}
735fc58801cSScott Long 
736fc58801cSScott Long 	/* Create RAID physdisk pages for standalone disks. */
737fc58801cSScott Long 	error = add_drives(fd, info, verbose);
738d68b76a9SAlan Somers 	if (error) {
739d68b76a9SAlan Somers 		free(info);
740d68b76a9SAlan Somers 		close(fd);
741fc58801cSScott Long 		return (error);
742d68b76a9SAlan Somers 	}
743fc58801cSScott Long 
744fc58801cSScott Long 	/* Build the volume. */
745fc58801cSScott Long 	vol = build_volume(fd, info, raid_type, stripe_size, &state, verbose);
746d68b76a9SAlan Somers 	if (vol == NULL) {
747d68b76a9SAlan Somers 		free(info);
748d68b76a9SAlan Somers 		close(fd);
749c5f2b79dSJohn Baldwin 		return (errno);
750d68b76a9SAlan Somers 	}
751fc58801cSScott Long 
752fc58801cSScott Long #ifdef DEBUG
753fc58801cSScott Long 	if (dump) {
754fc58801cSScott Long 		dump_config(vol);
755fc58801cSScott Long 		goto skip;
756fc58801cSScott Long 	}
757fc58801cSScott Long #endif
758fc58801cSScott Long 
759fc58801cSScott Long 	/* Send the new volume to the controller. */
760c5f2b79dSJohn Baldwin 	error = mpt_raid_action(fd, MPI_RAID_ACTION_CREATE_VOLUME, vol->VolumeBus,
761fc58801cSScott Long 	    vol->VolumeID, 0, quick ? MPI_RAID_ACTION_ADATA_DO_NOT_SYNC : 0,
762c5f2b79dSJohn Baldwin 	    vol, vol->Header.PageLength * 4, NULL, NULL, 0, NULL, NULL, 1);
763c5f2b79dSJohn Baldwin 	if (error) {
764c5f2b79dSJohn Baldwin 		errno = error;
765fc58801cSScott Long 		warn("Failed to add volume");
766d68b76a9SAlan Somers 		free(info);
767d68b76a9SAlan Somers 		close(fd);
768c5f2b79dSJohn Baldwin 		return (error);
769fc58801cSScott Long 	}
770fc58801cSScott Long 
771fc58801cSScott Long #ifdef DEBUG
772fc58801cSScott Long skip:
773fc58801cSScott Long #endif
774*d90e14fcSEnji Cooper 	error = mpt_rescan_bus(vol->VolumeBus, vol->VolumeID);
775fc58801cSScott Long 
776fc58801cSScott Long 	/* Clean up. */
777fc58801cSScott Long 	free(vol);
778fc58801cSScott Long 	free(info);
779fc58801cSScott Long 	free(state.sdisks);
780fc58801cSScott Long 	mpt_free_pd_list(state.list);
781fc58801cSScott Long 	free(state.ioc2);
782fc58801cSScott Long 	close(fd);
783fc58801cSScott Long 
784*d90e14fcSEnji Cooper 	return (error);
785fc58801cSScott Long }
786fc58801cSScott Long MPT_COMMAND(top, create, create_volume);
787fc58801cSScott Long 
788fc58801cSScott Long static int
delete_volume(int ac,char ** av)789fc58801cSScott Long delete_volume(int ac, char **av)
790fc58801cSScott Long {
791fc58801cSScott Long 	U8 VolumeBus, VolumeID;
792c5f2b79dSJohn Baldwin 	int error, fd;
793fc58801cSScott Long 
794fc58801cSScott Long 	if (ac != 2) {
795fc58801cSScott Long 		warnx("delete: volume required");
796fc58801cSScott Long 		return (EINVAL);
797fc58801cSScott Long 	}
798fc58801cSScott Long 
799fc58801cSScott Long 	fd = mpt_open(mpt_unit);
800fc58801cSScott Long 	if (fd < 0) {
801c5f2b79dSJohn Baldwin 		error = errno;
802fc58801cSScott Long 		warn("mpt_open");
803c5f2b79dSJohn Baldwin 		return (error);
804fc58801cSScott Long 	}
805fc58801cSScott Long 
806c5f2b79dSJohn Baldwin 	error = mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID);
807c5f2b79dSJohn Baldwin 	if (error) {
808c5f2b79dSJohn Baldwin 		warnc(error, "Invalid volume %s", av[1]);
809d68b76a9SAlan Somers 		close(fd);
810c5f2b79dSJohn Baldwin 		return (error);
811fc58801cSScott Long 	}
812fc58801cSScott Long 
813d68b76a9SAlan Somers 	if (mpt_lock_volume(VolumeBus, VolumeID) < 0) {
814d68b76a9SAlan Somers 		close(fd);
815fc58801cSScott Long 		return (errno);
816d68b76a9SAlan Somers 	}
817fc58801cSScott Long 
818c5f2b79dSJohn Baldwin 	error = mpt_raid_action(fd, MPI_RAID_ACTION_DELETE_VOLUME, VolumeBus,
819fc58801cSScott Long 	    VolumeID, 0, MPI_RAID_ACTION_ADATA_DEL_PHYS_DISKS |
820fc58801cSScott Long 	    MPI_RAID_ACTION_ADATA_ZERO_LBA0, NULL, 0, NULL, NULL, 0, NULL,
821c5f2b79dSJohn Baldwin 	    NULL, 0);
822c5f2b79dSJohn Baldwin 	if (error) {
823c5f2b79dSJohn Baldwin 		warnc(error, "Failed to delete volume");
824d68b76a9SAlan Somers 		close(fd);
825c5f2b79dSJohn Baldwin 		return (error);
826fc58801cSScott Long 	}
827fc58801cSScott Long 
828*d90e14fcSEnji Cooper 	error = mpt_rescan_bus(-1, -1);
829fc58801cSScott Long 	close(fd);
830fc58801cSScott Long 
831*d90e14fcSEnji Cooper 	return (error);
832fc58801cSScott Long }
833fc58801cSScott Long MPT_COMMAND(top, delete, delete_volume);
834fc58801cSScott Long 
835fc58801cSScott Long static int
find_volume_spare_pool(int fd,const char * name,int * pool)836fc58801cSScott Long find_volume_spare_pool(int fd, const char *name, int *pool)
837fc58801cSScott Long {
838fc58801cSScott Long 	CONFIG_PAGE_RAID_VOL_0 *info;
839fc58801cSScott Long 	CONFIG_PAGE_IOC_2 *ioc2;
840fc58801cSScott Long 	CONFIG_PAGE_IOC_2_RAID_VOL *vol;
841fc58801cSScott Long 	U8 VolumeBus, VolumeID;
842c5f2b79dSJohn Baldwin 	int error, i, j, new_pool, pool_count[7];
843fc58801cSScott Long 
844c5f2b79dSJohn Baldwin 	error = mpt_lookup_volume(fd, name, &VolumeBus, &VolumeID);
845c5f2b79dSJohn Baldwin 	if (error) {
846c5f2b79dSJohn Baldwin 		warnc(error, "Invalid volume %s", name);
847c5f2b79dSJohn Baldwin 		return (error);
848fc58801cSScott Long 	}
849fc58801cSScott Long 
850fc58801cSScott Long 	info = mpt_vol_info(fd, VolumeBus, VolumeID, NULL);
851fc58801cSScott Long 	if (info == NULL)
852c5f2b79dSJohn Baldwin 		return (errno);
853fc58801cSScott Long 
854fc58801cSScott Long 	/*
855fc58801cSScott Long 	 * Check for an existing pool other than pool 0 (used for
856fc58801cSScott Long 	 * global spares).
857fc58801cSScott Long 	 */
858fc58801cSScott Long 	if ((info->VolumeSettings.HotSparePool & ~MPI_RAID_HOT_SPARE_POOL_0) !=
859fc58801cSScott Long 	    0) {
860fc58801cSScott Long 		*pool = 1 << (ffs(info->VolumeSettings.HotSparePool &
861fc58801cSScott Long 		    ~MPI_RAID_HOT_SPARE_POOL_0) - 1);
862d68b76a9SAlan Somers 		free(info);
863fc58801cSScott Long 		return (0);
864fc58801cSScott Long 	}
865fc58801cSScott Long 	free(info);
866fc58801cSScott Long 
867fc58801cSScott Long 	/*
868fc58801cSScott Long 	 * Try to find a free pool.  First, figure out which pools are
869fc58801cSScott Long 	 * in use.
870fc58801cSScott Long 	 */
871fc58801cSScott Long 	ioc2 = mpt_read_ioc_page(fd, 2, NULL);
872fc58801cSScott Long 	if (ioc2 == NULL) {
873c5f2b79dSJohn Baldwin 		error = errno;
874fc58801cSScott Long 		warn("Failed to fetch volume list");
875c5f2b79dSJohn Baldwin 		return (error);
876fc58801cSScott Long 	}
877fc58801cSScott Long 	bzero(pool_count, sizeof(pool_count));
878fc58801cSScott Long 	vol = ioc2->RaidVolume;
879fc58801cSScott Long 	for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) {
880fc58801cSScott Long 		info = mpt_vol_info(fd, vol->VolumeBus, vol->VolumeID, NULL);
881fc58801cSScott Long 		if (info == NULL)
882c5f2b79dSJohn Baldwin 			return (errno);
883fc58801cSScott Long 		for (j = 0; j < 7; j++)
884fc58801cSScott Long 			if (info->VolumeSettings.HotSparePool & (1 << (j + 1)))
885fc58801cSScott Long 				pool_count[j]++;
886fc58801cSScott Long 		free(info);
887fc58801cSScott Long 	}
888fc58801cSScott Long 	free(ioc2);
889fc58801cSScott Long 
890fc58801cSScott Long 	/* Find the pool with the lowest use count. */
891fc58801cSScott Long 	new_pool = 0;
892fc58801cSScott Long 	for (i = 1; i < 7; i++)
893fc58801cSScott Long 		if (pool_count[i] < pool_count[new_pool])
894fc58801cSScott Long 			new_pool = i;
895fc58801cSScott Long 	new_pool++;
896fc58801cSScott Long 
897fc58801cSScott Long 	/* Add this pool to the volume. */
898fc58801cSScott Long 	info = mpt_vol_info(fd, VolumeBus, VolumeID, NULL);
899fc58801cSScott Long 	if (info == NULL)
900c5f2b79dSJohn Baldwin 		return (error);
901fc58801cSScott Long 	info->VolumeSettings.HotSparePool |= (1 << new_pool);
902c5f2b79dSJohn Baldwin 	error = mpt_raid_action(fd, MPI_RAID_ACTION_CHANGE_VOLUME_SETTINGS,
903fc58801cSScott Long 	    VolumeBus, VolumeID, 0, *(U32 *)&info->VolumeSettings, NULL, 0,
904c5f2b79dSJohn Baldwin 	    NULL, NULL, 0, NULL, NULL, 0);
905c5f2b79dSJohn Baldwin 	if (error) {
906fc58801cSScott Long 		warnx("Failed to add spare pool %d to %s", new_pool,
907fc58801cSScott Long 		    mpt_volume_name(VolumeBus, VolumeID));
908d68b76a9SAlan Somers 		free(info);
909c5f2b79dSJohn Baldwin 		return (error);
910fc58801cSScott Long 	}
911fc58801cSScott Long 	free(info);
912fc58801cSScott Long 
913fc58801cSScott Long 	*pool = (1 << new_pool);
914fc58801cSScott Long 	return (0);
915fc58801cSScott Long }
916fc58801cSScott Long 
917fc58801cSScott Long static int
add_spare(int ac,char ** av)918fc58801cSScott Long add_spare(int ac, char **av)
919fc58801cSScott Long {
920fc58801cSScott Long 	CONFIG_PAGE_RAID_PHYS_DISK_0 *info;
921fc58801cSScott Long 	struct mpt_standalone_disk *sdisks;
922fc58801cSScott Long 	struct mpt_drive_list *list;
923fc58801cSScott Long 	U8 PhysDiskNum;
924fc58801cSScott Long 	int error, fd, i, nsdisks, pool;
925fc58801cSScott Long 
926fc58801cSScott Long 	if (ac < 2) {
927fc58801cSScott Long 		warnx("add spare: drive required");
928fc58801cSScott Long 		return (EINVAL);
929fc58801cSScott Long 	}
930fc58801cSScott Long 	if (ac > 3) {
931fc58801cSScott Long 		warnx("add spare: extra arguments");
932fc58801cSScott Long 		return (EINVAL);
933fc58801cSScott Long 	}
934fc58801cSScott Long 
935fc58801cSScott Long 	fd = mpt_open(mpt_unit);
936fc58801cSScott Long 	if (fd < 0) {
937c5f2b79dSJohn Baldwin 		error = errno;
938fc58801cSScott Long 		warn("mpt_open");
939c5f2b79dSJohn Baldwin 		return (error);
940fc58801cSScott Long 	}
941fc58801cSScott Long 
942fc58801cSScott Long 	if (ac == 3) {
943c5f2b79dSJohn Baldwin 		error = find_volume_spare_pool(fd, av[2], &pool);
944d68b76a9SAlan Somers 		if (error) {
945d68b76a9SAlan Somers 			close(fd);
946c5f2b79dSJohn Baldwin 			return (error);
947d68b76a9SAlan Somers 		}
948fc58801cSScott Long 	} else
949fc58801cSScott Long 		pool = MPI_RAID_HOT_SPARE_POOL_0;
950fc58801cSScott Long 
951fc58801cSScott Long 	list = mpt_pd_list(fd);
952fc58801cSScott Long 	if (list == NULL)
953fc58801cSScott Long 		return (errno);
954fc58801cSScott Long 
955fc58801cSScott Long 	error = mpt_lookup_drive(list, av[1], &PhysDiskNum);
956fc58801cSScott Long 	if (error) {
957fc58801cSScott Long 		error = mpt_fetch_disks(fd, &nsdisks, &sdisks);
958fc58801cSScott Long 		if (error != 0) {
959fc58801cSScott Long 			warn("Failed to fetch standalone disk list");
960d68b76a9SAlan Somers 			mpt_free_pd_list(list);
961d68b76a9SAlan Somers 			close(fd);
962fc58801cSScott Long 			return (error);
963fc58801cSScott Long 		}
964fc58801cSScott Long 
965fc58801cSScott Long 		if (mpt_lookup_standalone_disk(av[1], sdisks, nsdisks, &i) <
966fc58801cSScott Long 		    0) {
967c5f2b79dSJohn Baldwin 			error = errno;
968fc58801cSScott Long 			warn("Unable to lookup drive %s", av[1]);
969d68b76a9SAlan Somers 			mpt_free_pd_list(list);
970d68b76a9SAlan Somers 			close(fd);
971c5f2b79dSJohn Baldwin 			return (error);
972fc58801cSScott Long 		}
973fc58801cSScott Long 
974d68b76a9SAlan Somers 		if (mpt_lock_physdisk(&sdisks[i]) < 0) {
975d68b76a9SAlan Somers 			mpt_free_pd_list(list);
976d68b76a9SAlan Somers 			close(fd);
977fc58801cSScott Long 			return (errno);
978d68b76a9SAlan Somers 		}
979fc58801cSScott Long 
980fc58801cSScott Long 		if (mpt_create_physdisk(fd, &sdisks[i], &PhysDiskNum) < 0) {
981c5f2b79dSJohn Baldwin 			error = errno;
982fc58801cSScott Long 			warn("Failed to create physical disk page");
983d68b76a9SAlan Somers 			mpt_free_pd_list(list);
984d68b76a9SAlan Somers 			close(fd);
985c5f2b79dSJohn Baldwin 			return (error);
986fc58801cSScott Long 		}
987fc58801cSScott Long 		free(sdisks);
988fc58801cSScott Long 	}
989fc58801cSScott Long 	mpt_free_pd_list(list);
990fc58801cSScott Long 
991fc58801cSScott Long 	info = mpt_pd_info(fd, PhysDiskNum, NULL);
992fc58801cSScott Long 	if (info == NULL) {
993c5f2b79dSJohn Baldwin 		error = errno;
994fc58801cSScott Long 		warn("Failed to fetch drive info");
995d68b76a9SAlan Somers 		close(fd);
996c5f2b79dSJohn Baldwin 		return (error);
997fc58801cSScott Long 	}
998fc58801cSScott Long 
999fc58801cSScott Long 	info->PhysDiskSettings.HotSparePool = pool;
1000fc58801cSScott Long 	error = mpt_raid_action(fd, MPI_RAID_ACTION_CHANGE_PHYSDISK_SETTINGS, 0,
1001fc58801cSScott Long 	    0, PhysDiskNum, *(U32 *)&info->PhysDiskSettings, NULL, 0, NULL,
1002fc58801cSScott Long 	    NULL, 0, NULL, NULL, 0);
1003fc58801cSScott Long 	if (error) {
1004c5f2b79dSJohn Baldwin 		warnc(error, "Failed to assign spare");
1005d68b76a9SAlan Somers 		close(fd);
1006c5f2b79dSJohn Baldwin 		return (error);
1007fc58801cSScott Long 	}
1008fc58801cSScott Long 
1009fc58801cSScott Long 	free(info);
1010fc58801cSScott Long 	close(fd);
1011fc58801cSScott Long 
1012fc58801cSScott Long 	return (0);
1013fc58801cSScott Long }
1014fc58801cSScott Long MPT_COMMAND(top, add, add_spare);
1015fc58801cSScott Long 
1016fc58801cSScott Long static int
remove_spare(int ac,char ** av)1017fc58801cSScott Long remove_spare(int ac, char **av)
1018fc58801cSScott Long {
1019fc58801cSScott Long 	CONFIG_PAGE_RAID_PHYS_DISK_0 *info;
1020fc58801cSScott Long 	struct mpt_drive_list *list;
1021fc58801cSScott Long 	U8 PhysDiskNum;
1022fc58801cSScott Long 	int error, fd;
1023fc58801cSScott Long 
1024fc58801cSScott Long 	if (ac != 2) {
1025fc58801cSScott Long 		warnx("remove spare: drive required");
1026fc58801cSScott Long 		return (EINVAL);
1027fc58801cSScott Long 	}
1028fc58801cSScott Long 
1029fc58801cSScott Long 	fd = mpt_open(mpt_unit);
1030fc58801cSScott Long 	if (fd < 0) {
1031c5f2b79dSJohn Baldwin 		error = errno;
1032fc58801cSScott Long 		warn("mpt_open");
1033c5f2b79dSJohn Baldwin 		return (error);
1034fc58801cSScott Long 	}
1035fc58801cSScott Long 
1036fc58801cSScott Long 	list = mpt_pd_list(fd);
1037d68b76a9SAlan Somers 	if (list == NULL) {
1038d68b76a9SAlan Somers 		close(fd);
1039fc58801cSScott Long 		return (errno);
1040d68b76a9SAlan Somers 	}
1041fc58801cSScott Long 
1042fc58801cSScott Long 	error = mpt_lookup_drive(list, av[1], &PhysDiskNum);
1043fc58801cSScott Long 	if (error) {
1044fc58801cSScott Long 		warn("Failed to find drive %s", av[1]);
1045d68b76a9SAlan Somers 		close(fd);
1046fc58801cSScott Long 		return (error);
1047fc58801cSScott Long 	}
1048fc58801cSScott Long 	mpt_free_pd_list(list);
1049fc58801cSScott Long 
1050fc58801cSScott Long 
1051fc58801cSScott Long 	info = mpt_pd_info(fd, PhysDiskNum, NULL);
1052fc58801cSScott Long 	if (info == NULL) {
1053c5f2b79dSJohn Baldwin 		error = errno;
1054fc58801cSScott Long 		warn("Failed to fetch drive info");
1055d68b76a9SAlan Somers 		close(fd);
1056c5f2b79dSJohn Baldwin 		return (error);
1057fc58801cSScott Long 	}
1058fc58801cSScott Long 
1059fc58801cSScott Long 	if (info->PhysDiskSettings.HotSparePool == 0) {
1060fc58801cSScott Long 		warnx("Drive %u is not a hot spare", PhysDiskNum);
1061d68b76a9SAlan Somers 		free(info);
1062d68b76a9SAlan Somers 		close(fd);
1063fc58801cSScott Long 		return (EINVAL);
1064fc58801cSScott Long 	}
1065fc58801cSScott Long 
1066fc58801cSScott Long 	if (mpt_delete_physdisk(fd, PhysDiskNum) < 0) {
1067c5f2b79dSJohn Baldwin 		error = errno;
1068fc58801cSScott Long 		warn("Failed to delete physical disk page");
1069d68b76a9SAlan Somers 		free(info);
1070d68b76a9SAlan Somers 		close(fd);
1071c5f2b79dSJohn Baldwin 		return (error);
1072fc58801cSScott Long 	}
1073fc58801cSScott Long 
1074*d90e14fcSEnji Cooper 	error = mpt_rescan_bus(info->PhysDiskBus, info->PhysDiskID);
1075*d90e14fcSEnji Cooper 	if (error)
1076*d90e14fcSEnji Cooper 		return (error);
1077fc58801cSScott Long 	free(info);
1078fc58801cSScott Long 	close(fd);
1079fc58801cSScott Long 
1080fc58801cSScott Long 	return (0);
1081fc58801cSScott Long }
1082fc58801cSScott Long MPT_COMMAND(top, remove, remove_spare);
1083fc58801cSScott Long 
1084fc58801cSScott Long #ifdef DEBUG
1085fc58801cSScott Long MPT_TABLE(top, pd);
1086fc58801cSScott Long 
1087fc58801cSScott Long static int
pd_create(int ac,char ** av)1088fc58801cSScott Long pd_create(int ac, char **av)
1089fc58801cSScott Long {
1090fc58801cSScott Long 	struct mpt_standalone_disk *disks;
1091fc58801cSScott Long 	int error, fd, i, ndisks;
1092fc58801cSScott Long 	U8 PhysDiskNum;
1093fc58801cSScott Long 
1094fc58801cSScott Long 	if (ac != 2) {
1095fc58801cSScott Long 		warnx("pd create: drive required");
1096fc58801cSScott Long 		return (EINVAL);
1097fc58801cSScott Long 	}
1098fc58801cSScott Long 
1099fc58801cSScott Long 	fd = mpt_open(mpt_unit);
1100fc58801cSScott Long 	if (fd < 0) {
1101c5f2b79dSJohn Baldwin 		error = errno;
1102fc58801cSScott Long 		warn("mpt_open");
1103c5f2b79dSJohn Baldwin 		return (error);
1104fc58801cSScott Long 	}
1105fc58801cSScott Long 
1106fc58801cSScott Long 	error = mpt_fetch_disks(fd, &ndisks, &disks);
1107fc58801cSScott Long 	if (error != 0) {
1108fc58801cSScott Long 		warn("Failed to fetch standalone disk list");
1109fc58801cSScott Long 		return (error);
1110fc58801cSScott Long 	}
1111fc58801cSScott Long 
1112fc58801cSScott Long 	if (mpt_lookup_standalone_disk(av[1], disks, ndisks, &i) < 0) {
1113c5f2b79dSJohn Baldwin 		error = errno;
1114fc58801cSScott Long 		warn("Unable to lookup drive");
1115c5f2b79dSJohn Baldwin 		return (error);
1116fc58801cSScott Long 	}
1117fc58801cSScott Long 
1118fc58801cSScott Long 	if (mpt_lock_physdisk(&disks[i]) < 0)
1119fc58801cSScott Long 		return (errno);
1120fc58801cSScott Long 
1121fc58801cSScott Long 	if (mpt_create_physdisk(fd, &disks[i], &PhysDiskNum) < 0) {
1122c5f2b79dSJohn Baldwin 		error = errno;
1123fc58801cSScott Long 		warn("Failed to create physical disk page");
1124c5f2b79dSJohn Baldwin 		return (error);
1125fc58801cSScott Long 	}
1126fc58801cSScott Long 	free(disks);
1127fc58801cSScott Long 
1128fc58801cSScott Long 	printf("Added drive %s with PhysDiskNum %u\n", av[1], PhysDiskNum);
1129fc58801cSScott Long 
1130fc58801cSScott Long 	close(fd);
1131fc58801cSScott Long 
1132fc58801cSScott Long 	return (0);
1133fc58801cSScott Long }
1134fc58801cSScott Long MPT_COMMAND(pd, create, pd_create);
1135fc58801cSScott Long 
1136fc58801cSScott Long static int
pd_delete(int ac,char ** av)1137fc58801cSScott Long pd_delete(int ac, char **av)
1138fc58801cSScott Long {
1139fc58801cSScott Long 	CONFIG_PAGE_RAID_PHYS_DISK_0 *info;
1140fc58801cSScott Long 	struct mpt_drive_list *list;
1141c5f2b79dSJohn Baldwin 	int error, fd;
1142fc58801cSScott Long 	U8 PhysDiskNum;
1143fc58801cSScott Long 
1144fc58801cSScott Long 	if (ac != 2) {
1145fc58801cSScott Long 		warnx("pd delete: drive required");
1146fc58801cSScott Long 		return (EINVAL);
1147fc58801cSScott Long 	}
1148fc58801cSScott Long 
1149fc58801cSScott Long 	fd = mpt_open(mpt_unit);
1150fc58801cSScott Long 	if (fd < 0) {
1151c5f2b79dSJohn Baldwin 		error = errno;
1152fc58801cSScott Long 		warn("mpt_open");
1153c5f2b79dSJohn Baldwin 		return (error);
1154fc58801cSScott Long 	}
1155fc58801cSScott Long 
1156fc58801cSScott Long 	list = mpt_pd_list(fd);
1157fc58801cSScott Long 	if (list == NULL)
1158fc58801cSScott Long 		return (errno);
1159fc58801cSScott Long 
1160fc58801cSScott Long 	if (mpt_lookup_drive(list, av[1], &PhysDiskNum) < 0) {
1161c5f2b79dSJohn Baldwin 		error = errno;
1162fc58801cSScott Long 		warn("Failed to find drive %s", av[1]);
1163c5f2b79dSJohn Baldwin 		return (error);
1164fc58801cSScott Long 	}
1165fc58801cSScott Long 	mpt_free_pd_list(list);
1166fc58801cSScott Long 
1167fc58801cSScott Long 	info = mpt_pd_info(fd, PhysDiskNum, NULL);
1168fc58801cSScott Long 	if (info == NULL) {
1169c5f2b79dSJohn Baldwin 		error = errno;
1170fc58801cSScott Long 		warn("Failed to fetch drive info");
1171c5f2b79dSJohn Baldwin 		return (error);
1172fc58801cSScott Long 	}
1173fc58801cSScott Long 
1174fc58801cSScott Long 	if (mpt_delete_physdisk(fd, PhysDiskNum) < 0) {
1175c5f2b79dSJohn Baldwin 		error = errno;
1176fc58801cSScott Long 		warn("Failed to delete physical disk page");
1177c5f2b79dSJohn Baldwin 		return (error);
1178fc58801cSScott Long 	}
1179fc58801cSScott Long 
1180*d90e14fcSEnji Cooper 	error = mpt_rescan_bus(info->PhysDiskBus, info->PhysDiskID);
1181*d90e14fcSEnji Cooper 	if (error)
1182*d90e14fcSEnji Cooper 		return (error);
1183fc58801cSScott Long 	free(info);
1184fc58801cSScott Long 	close(fd);
1185fc58801cSScott Long 
1186fc58801cSScott Long 	return (0);
1187fc58801cSScott Long }
1188fc58801cSScott Long MPT_COMMAND(pd, delete, pd_delete);
1189fc58801cSScott Long 
1190fc58801cSScott Long /* Display raw data about a volume config. */
1191fc58801cSScott Long static void
dump_config(CONFIG_PAGE_RAID_VOL_0 * vol)1192fc58801cSScott Long dump_config(CONFIG_PAGE_RAID_VOL_0 *vol)
1193fc58801cSScott Long {
1194fc58801cSScott Long 	int i;
1195fc58801cSScott Long 
1196fc58801cSScott Long 	printf("Volume Configuration (Debug):\n");
1197fc58801cSScott Long 	printf(
1198fc58801cSScott Long    " Page Header: Type 0x%02x Number 0x%02x Length 0x%02x(%u) Version 0x%02x\n",
1199fc58801cSScott Long 	    vol->Header.PageType, vol->Header.PageNumber,
1200fc58801cSScott Long 	    vol->Header.PageLength, vol->Header.PageLength * 4,
1201fc58801cSScott Long 	    vol->Header.PageVersion);
1202fc58801cSScott Long 	printf("     Address: %d:%d IOC %d\n", vol->VolumeBus, vol->VolumeID,
1203fc58801cSScott Long 	    vol->VolumeIOC);
1204fc58801cSScott Long 	printf("        Type: %d (%s)\n", vol->VolumeType,
1205fc58801cSScott Long 	    mpt_raid_level(vol->VolumeType));
1206fc58801cSScott Long 	printf("      Status: %s (Flags 0x%02x)\n",
1207fc58801cSScott Long 	    mpt_volstate(vol->VolumeStatus.State), vol->VolumeStatus.Flags);
1208fc58801cSScott Long 	printf("    Settings: 0x%04x (Spare Pools 0x%02x)\n",
1209fc58801cSScott Long 	    vol->VolumeSettings.Settings, vol->VolumeSettings.HotSparePool);
1210fc58801cSScott Long 	printf("      MaxLBA: %ju\n", (uintmax_t)vol->MaxLBAHigh << 32 |
1211fc58801cSScott Long 	    vol->MaxLBA);
1212fc58801cSScott Long 	printf(" Stripe Size: %ld\n", (long)vol->StripeSize * 512);
1213fc58801cSScott Long 	printf(" %d Disks:\n", vol->NumPhysDisks);
1214fc58801cSScott Long 
1215fc58801cSScott Long 	for (i = 0; i < vol->NumPhysDisks; i++)
1216fc58801cSScott Long 		printf("    Disk %d: Num 0x%02x Map 0x%02x\n", i,
1217fc58801cSScott Long 		    vol->PhysDisk[i].PhysDiskNum, vol->PhysDisk[i].PhysDiskMap);
1218fc58801cSScott Long }
1219fc58801cSScott Long 
1220fc58801cSScott Long static int
debug_config(int ac,char ** av)1221fc58801cSScott Long debug_config(int ac, char **av)
1222fc58801cSScott Long {
1223fc58801cSScott Long 	CONFIG_PAGE_RAID_VOL_0 *vol;
1224fc58801cSScott Long 	U8 VolumeBus, VolumeID;
1225c5f2b79dSJohn Baldwin 	int error, fd;
1226fc58801cSScott Long 
1227fc58801cSScott Long 	if (ac != 2) {
1228fc58801cSScott Long 		warnx("debug: volume required");
1229fc58801cSScott Long 		return (EINVAL);
1230fc58801cSScott Long 	}
1231fc58801cSScott Long 
1232fc58801cSScott Long 	fd = mpt_open(mpt_unit);
1233fc58801cSScott Long 	if (fd < 0) {
1234c5f2b79dSJohn Baldwin 		error = errno;
1235fc58801cSScott Long 		warn("mpt_open");
1236c5f2b79dSJohn Baldwin 		return (error);
1237fc58801cSScott Long 	}
1238fc58801cSScott Long 
1239c5f2b79dSJohn Baldwin 	error = mpt_lookup_volume(fd, av[1], &VolumeBus, &VolumeID);
1240c5f2b79dSJohn Baldwin 	if (error) {
1241c5f2b79dSJohn Baldwin 		warnc(error, "Invalid volume: %s", av[1]);
1242c5f2b79dSJohn Baldwin 		return (error);
1243fc58801cSScott Long 	}
1244fc58801cSScott Long 
1245fc58801cSScott Long 	vol = mpt_vol_info(fd, VolumeBus, VolumeID, NULL);
1246fc58801cSScott Long 	if (vol == NULL) {
1247c5f2b79dSJohn Baldwin 		error = errno;
1248fc58801cSScott Long 		warn("Failed to get volume info");
1249c5f2b79dSJohn Baldwin 		return (error);
1250fc58801cSScott Long 	}
1251fc58801cSScott Long 
1252fc58801cSScott Long 	dump_config(vol);
1253fc58801cSScott Long 	free(vol);
1254fc58801cSScott Long 	close(fd);
1255fc58801cSScott Long 
1256fc58801cSScott Long 	return (0);
1257fc58801cSScott Long }
1258fc58801cSScott Long MPT_COMMAND(top, debug, debug_config);
1259fc58801cSScott Long #endif
1260