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