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