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