11592Srgrimes/*- 21592Srgrimes * SPDX-License-Identifier: BSD-3-Clause 31592Srgrimes * 496196Sdes * Copyright (c) 2008 Yahoo!, Inc. 596196Sdes * All rights reserved. 61592Srgrimes * Written by: John Baldwin <jhb@FreeBSD.org> 796196Sdes * 896196Sdes * Redistribution and use in source and binary forms, with or without 996196Sdes * modification, are permitted provided that the following conditions 1096196Sdes * are met: 1196196Sdes * 1. Redistributions of source code must retain the above copyright 121592Srgrimes * notice, this list of conditions and the following disclaimer. 131592Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141592Srgrimes * notice, this list of conditions and the following disclaimer in the 151592Srgrimes * documentation and/or other materials provided with the distribution. 161592Srgrimes * 3. Neither the name of the author nor the names of any co-contributors 171592Srgrimes * may be used to endorse or promote products derived from this software 181592Srgrimes * without specific prior written permission. 191592Srgrimes * 20262435Sbrueffer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 211592Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221592Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231592Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 241592Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251592Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261592Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271592Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281592Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291592Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301592Srgrimes * SUCH DAMAGE. 311592Srgrimes */ 321592Srgrimes 331592Srgrimes#include <sys/param.h> 341592Srgrimes#include <sys/errno.h> 351592Srgrimes#include <err.h> 361592Srgrimes#include <fcntl.h> 37114624Sobrien#include <libutil.h> 381592Srgrimes#include <paths.h> 3929916Smarkm#ifdef DEBUG 401592Srgrimes#include <stdint.h> 411592Srgrimes#endif 421592Srgrimes#include <stdio.h> 431592Srgrimes#include <stdlib.h> 441592Srgrimes#include <string.h> 4529916Smarkm#include <unistd.h> 46114624Sobrien#include "mptutil.h" 4731405Scharnier 48114624Sobrien#ifdef DEBUG 49114624Sobrienstatic void dump_config(CONFIG_PAGE_RAID_VOL_0 *vol); 501592Srgrimes#endif 511592Srgrimes 521592Srgrimesstatic long 531592Srgrimesdehumanize(const char *value) 541592Srgrimes{ 551592Srgrimes char *vtp; 561592Srgrimes long iv; 571592Srgrimes 581592Srgrimes if (value == NULL) 591592Srgrimes return (0); 601592Srgrimes iv = strtoq(value, &vtp, 0); 6129916Smarkm if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0')) { 621592Srgrimes return (0); 631592Srgrimes } 641592Srgrimes switch (vtp[0]) { 651592Srgrimes case 't': case 'T': 661592Srgrimes iv *= 1024; 671592Srgrimes /* FALLTHROUGH */ 681592Srgrimes case 'g': case 'G': 691592Srgrimes iv *= 1024; 701592Srgrimes /* FALLTHROUGH */ 711592Srgrimes case 'm': case 'M': 7211486Sdg iv *= 1024; 731592Srgrimes /* FALLTHROUGH */ 741592Srgrimes case 'k': case 'K': 751592Srgrimes iv *= 1024; 7631405Scharnier /* FALLTHROUGH */ 7731405Scharnier case '\0': 7896196Sdes break; 791592Srgrimes default: 801592Srgrimes return (0); 811592Srgrimes } 821592Srgrimes return (iv); 831592Srgrimes} 8431405Scharnier 851592Srgrimes/* 8651433Smarkm * Lock the volume by opening its /dev device read/write. This will 871592Srgrimes * only work if nothing else has it opened (including mounts). We 881592Srgrimes * leak the fd on purpose since this application is not long-running. 891592Srgrimes */ 901592Srgrimesint 91141588Srumpt_lock_volume(U8 VolumeBus, U8 VolumeID) 921592Srgrimes{ 931592Srgrimes char path[MAXPATHLEN]; 941592Srgrimes struct mpt_query_disk qd; 951592Srgrimes int error, vfd; 961592Srgrimes 971592Srgrimes error = mpt_query_disk(VolumeBus, VolumeID, &qd); 981592Srgrimes if (error == ENOENT) 991592Srgrimes /* 10011486Sdg * This means there isn't a CAM device associated with 1011592Srgrimes * the volume, and thus it is already implicitly 1021592Srgrimes * locked, so just return. 1031592Srgrimes */ 10456590Sshin return (0); 10556590Sshin if (error) { 10656590Sshin warnc(error, "Unable to lookup volume device name"); 10756590Sshin return (error); 10856590Sshin } 10956590Sshin snprintf(path, sizeof(path), "%s%s", _PATH_DEV, qd.devname); 11056590Sshin vfd = open(path, O_RDWR); 11156590Sshin if (vfd < 0) { 11256590Sshin error = errno; 11356590Sshin warn("Unable to lock volume %s", qd.devname); 11456590Sshin return (error); 11556590Sshin } 11656590Sshin return (0); 11790377Simp} 11890377Simp 11990377Simpstatic int 12090377Simpmpt_lock_physdisk(struct mpt_standalone_disk *disk) 12190377Simp{ 12290377Simp char path[MAXPATHLEN]; 12390377Simp int dfd, error; 12490377Simp 12590377Simp snprintf(path, sizeof(path), "%s%s", _PATH_DEV, disk->devname); 12690377Simp dfd = open(path, O_RDWR); 1271592Srgrimes if (dfd < 0) { 12851433Smarkm error = errno; 1291592Srgrimes warn("Unable to lock disk %s", disk->devname); 13090377Simp return (error); 1311592Srgrimes } 1321592Srgrimes return (0); 13356590Sshin} 134141918Sstefanf 135141918Sstefanfstatic int 1361592Srgrimesmpt_lookup_standalone_disk(const char *name, struct mpt_standalone_disk *disks, 1371592Srgrimes int ndisks, int *index) 1381592Srgrimes{ 1391592Srgrimes char *cp; 14024349Simp long bus, id; 1411592Srgrimes int i; 14211486Sdg 14311486Sdg /* Check for a raw <bus>:<id> string. */ 14411486Sdg bus = strtol(name, &cp, 0); 1451592Srgrimes if (*cp == ':') { 1461592Srgrimes id = strtol(cp + 1, &cp, 0); 1471592Srgrimes if (*cp == '\0') { 1481592Srgrimes if (bus < 0 || bus > 0xff || id < 0 || id > 0xff) { 1491592Srgrimes return (EINVAL); 1501592Srgrimes } 1511592Srgrimes for (i = 0; i < ndisks; i++) { 1521592Srgrimes if (disks[i].bus == (U8)bus && 1531592Srgrimes disks[i].target == (U8)id) { 1541592Srgrimes *index = i; 1551592Srgrimes return (0); 1561592Srgrimes } 1571592Srgrimes } 1581592Srgrimes return (ENOENT); 1591592Srgrimes } 1601592Srgrimes } 1611592Srgrimes 1621592Srgrimes if (name[0] == 'd' && name[1] == 'a') { 1631592Srgrimes for (i = 0; i < ndisks; i++) { 1641592Srgrimes if (strcmp(name, disks[i].devname) == 0) { 1651592Srgrimes *index = i; 1661592Srgrimes return (0); 1671592Srgrimes } 1681592Srgrimes } 1691592Srgrimes return (ENOENT); 1701592Srgrimes } 17111486Sdg 17211486Sdg return (EINVAL); 17311486Sdg} 17496196Sdes 17556590Sshin/* 1761592Srgrimes * Mark a standalone disk as being a physical disk. 1771592Srgrimes */ 1781592Srgrimesstatic int 17956590Sshinmpt_create_physdisk(int fd, struct mpt_standalone_disk *disk, U8 *PhysDiskNum) 18011486Sdg{ 1811592Srgrimes CONFIG_PAGE_HEADER header; 18229916Smarkm CONFIG_PAGE_RAID_PHYS_DISK_0 *config_page; 1831592Srgrimes int error; 1841592Srgrimes U32 ActionData; 1851592Srgrimes 1861592Srgrimes error = mpt_read_config_page_header(fd, MPI_CONFIG_PAGETYPE_RAID_PHYSDISK, 1871592Srgrimes 0, 0, &header, NULL); 1881592Srgrimes if (error) 1891592Srgrimes return (error); 1901592Srgrimes if (header.PageVersion > MPI_RAIDPHYSDISKPAGE0_PAGEVERSION) { 1911592Srgrimes warnx("Unsupported RAID physdisk page 0 version %d", 1921592Srgrimes header.PageVersion); 1931592Srgrimes return (EOPNOTSUPP); 19490377Simp } 1951592Srgrimes config_page = calloc(1, sizeof(CONFIG_PAGE_RAID_PHYS_DISK_0)); 1961592Srgrimes config_page->Header.PageType = MPI_CONFIG_PAGETYPE_RAID_PHYSDISK; 1971592Srgrimes config_page->Header.PageNumber = 0; 19856590Sshin config_page->Header.PageLength = sizeof(CONFIG_PAGE_RAID_PHYS_DISK_0) / 19956590Sshin 4; 2001592Srgrimes config_page->PhysDiskIOC = 0; /* XXX */ 2011592Srgrimes config_page->PhysDiskBus = disk->bus; 2021592Srgrimes config_page->PhysDiskID = disk->target; 2031592Srgrimes 2041592Srgrimes /* XXX: Enclosure info for PhysDiskSettings? */ 2051592Srgrimes error = mpt_raid_action(fd, MPI_RAID_ACTION_CREATE_PHYSDISK, 0, 0, 0, 0, 2061592Srgrimes config_page, sizeof(CONFIG_PAGE_RAID_PHYS_DISK_0), NULL, 2071592Srgrimes &ActionData, sizeof(ActionData), NULL, NULL, 1); 2081592Srgrimes if (error) 20956590Sshin return (error); 21056590Sshin *PhysDiskNum = ActionData & 0xff; 21156590Sshin return (0); 21256590Sshin} 21356590Sshin 21424191Simpstatic int 2151592Srgrimesmpt_delete_physdisk(int fd, U8 PhysDiskNum) 2161592Srgrimes{ 21763959Sume 21856590Sshin return (mpt_raid_action(fd, MPI_RAID_ACTION_DELETE_PHYSDISK, 0, 0, 21963959Sume PhysDiskNum, 0, NULL, 0, NULL, NULL, 0, NULL, NULL, 0)); 22056590Sshin} 22156590Sshin 22256590Sshin/* 22356590Sshin * MPT's firmware does not have a clear command. Instead, we 22456590Sshin * implement it by deleting each array and disk by hand. 22556590Sshin */ 22656590Sshinstatic int 227146187Sumeclear_config(int ac, char **av) 22856590Sshin{ 2291592Srgrimes CONFIG_PAGE_IOC_2 *ioc2; 23056590Sshin CONFIG_PAGE_IOC_2_RAID_VOL *vol; 2311592Srgrimes CONFIG_PAGE_IOC_3 *ioc3; 2321592Srgrimes IOC_3_PHYS_DISK *disk; 2331592Srgrimes CONFIG_PAGE_IOC_5 *ioc5; 23456590Sshin IOC_5_HOT_SPARE *spare; 23596196Sdes int ch, error, fd, i; 23622455Simp 237141918Sstefanf fd = mpt_open(mpt_unit); 238141918Sstefanf if (fd < 0) { 2391592Srgrimes error = errno; 2401592Srgrimes warn("mpt_open"); 2411592Srgrimes return (error); 2421592Srgrimes } 2431592Srgrimes 2441592Srgrimes ioc2 = mpt_read_ioc_page(fd, 2, NULL); 2451592Srgrimes if (ioc2 == NULL) { 2461592Srgrimes error = errno; 24722455Simp warn("Failed to fetch volume list"); 24822455Simp close(fd); 24922455Simp return (error); 25022455Simp } 25122455Simp 25256590Sshin /* Lock all the volumes first. */ 25322455Simp vol = ioc2->RaidVolume; 25422455Simp for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) { 25522455Simp if (mpt_lock_volume(vol->VolumeBus, vol->VolumeID) < 0) { 25622455Simp warnx("Volume %s is busy and cannot be deleted", 25722455Simp mpt_volume_name(vol->VolumeBus, vol->VolumeID)); 25822455Simp free(ioc2); 2591592Srgrimes close(fd); 2601592Srgrimes return (EBUSY); 26156590Sshin } 2621592Srgrimes } 2631592Srgrimes 2641592Srgrimes printf( 2651592Srgrimes "Are you sure you wish to clear the configuration on mpt%u? [y/N] ", 2661592Srgrimes mpt_unit); 2671592Srgrimes ch = getchar(); 2681592Srgrimes if (ch != 'y' && ch != 'Y') { 2691592Srgrimes printf("\nAborting\n"); 2701592Srgrimes free(ioc2); 2711592Srgrimes close(fd); 2721592Srgrimes return (0); 2731592Srgrimes } 2741592Srgrimes 2751592Srgrimes /* Delete all the volumes. */ 2761592Srgrimes vol = ioc2->RaidVolume; 2771592Srgrimes for (i = 0; i < ioc2->NumActiveVolumes; vol++, i++) { 2781592Srgrimes error = mpt_raid_action(fd, MPI_RAID_ACTION_DELETE_VOLUME, 2791592Srgrimes vol->VolumeBus, vol->VolumeID, 0, 2801592Srgrimes MPI_RAID_ACTION_ADATA_DEL_PHYS_DISKS | 2811592Srgrimes MPI_RAID_ACTION_ADATA_ZERO_LBA0, NULL, 0, NULL, NULL, 0, 2821592Srgrimes NULL, NULL, 0); 28312575Snate if (error) 2842076Sguido warnc(error, "Failed to delete volume %s", 2852076Sguido mpt_volume_name(vol->VolumeBus, vol->VolumeID)); 2862076Sguido } 2872076Sguido free(ioc2); 2881592Srgrimes 2891592Srgrimes /* Delete all the spares. */ 2901592Srgrimes ioc5 = mpt_read_ioc_page(fd, 5, NULL); 2911592Srgrimes if (ioc5 == NULL) 2921592Srgrimes warn("Failed to fetch spare list"); 2931592Srgrimes else { 2941592Srgrimes spare = ioc5->HotSpare; 2951592Srgrimes for (i = 0; i < ioc5->NumHotSpares; spare++, i++) 2961592Srgrimes if (mpt_delete_physdisk(fd, spare->PhysDiskNum) < 0) 297141588Sru warn("Failed to delete physical disk %d", 2981592Srgrimes spare->PhysDiskNum); 2991592Srgrimes free(ioc5); 3001592Srgrimes } 3011592Srgrimes 3021592Srgrimes /* Delete any RAID physdisks that may be left. */ 3031592Srgrimes ioc3 = mpt_read_ioc_page(fd, 3, NULL); 3041592Srgrimes if (ioc3 == NULL) 3051592Srgrimes warn("Failed to fetch drive list"); 3061592Srgrimes else { 3071592Srgrimes disk = ioc3->PhysDisk; 3081592Srgrimes for (i = 0; i < ioc3->NumPhysDisks; disk++, i++) 3091592Srgrimes if (mpt_delete_physdisk(fd, disk->PhysDiskNum) < 0) 3101592Srgrimes warn("Failed to delete physical disk %d", 3111592Srgrimes disk->PhysDiskNum); 3121592Srgrimes free(ioc3); 3131592Srgrimes } 3141592Srgrimes 31590377Simp printf("mpt%d: Configuration cleared\n", mpt_unit); 3161592Srgrimes mpt_rescan_bus(-1, -1); 3171592Srgrimes close(fd); 3181592Srgrimes 319114624Sobrien return (0); 3201592Srgrimes} 3211592SrgrimesMPT_COMMAND(top, clear, clear_config); 3221592Srgrimes 3231592Srgrimes#define RT_RAID0 0 3241592Srgrimes#define RT_RAID1 1 3251592Srgrimes#define RT_RAID1E 2 3261592Srgrimes 3271592Srgrimesstatic struct raid_type_entry { 3281592Srgrimes const char *name; 3291592Srgrimes int raid_type; 3301592Srgrimes} raid_type_table[] = { 3311592Srgrimes { "raid0", RT_RAID0 }, 3321592Srgrimes { "raid-0", RT_RAID0 }, 3331592Srgrimes { "raid1", RT_RAID1 }, 3341592Srgrimes { "raid-1", RT_RAID1 }, 33590377Simp { "mirror", RT_RAID1 }, 3361592Srgrimes { "raid1e", RT_RAID1E }, 33769705Sru { "raid-1e", RT_RAID1E }, 33846078Simp { NULL, 0 }, 3391592Srgrimes}; 3401592Srgrimes 3411592Srgrimesstruct config_id_state { 3421592Srgrimes struct mpt_standalone_disk *sdisks; 3431592Srgrimes struct mpt_drive_list *list; 3441592Srgrimes CONFIG_PAGE_IOC_2 *ioc2; 3451592Srgrimes U8 target_id; 3461592Srgrimes int nsdisks; 3471592Srgrimes}; 3481592Srgrimes 3491592Srgrimesstruct drive_info { 3501592Srgrimes CONFIG_PAGE_RAID_PHYS_DISK_0 *info; 3511592Srgrimes struct mpt_standalone_disk *sdisk; 3521592Srgrimes}; 3531592Srgrimes 3541592Srgrimesstruct volume_info { 3551592Srgrimes int drive_count; 3561592Srgrimes struct drive_info *drives; 3571592Srgrimes}; 3581592Srgrimes 3591592Srgrimes/* Parse a comma-separated list of drives for a volume. */ 3601592Srgrimesstatic int 3611592Srgrimesparse_volume(int fd, int raid_type, struct config_id_state *state, 3621592Srgrimes char *volume_str, struct volume_info *info) 3631592Srgrimes{ 3641592Srgrimes struct drive_info *dinfo; 3651592Srgrimes U8 PhysDiskNum; 3661592Srgrimes char *cp; 3671592Srgrimes int count, error, i; 3681592Srgrimes 36946078Simp cp = volume_str; 3701592Srgrimes for (count = 0; cp != NULL; count++) { 3711592Srgrimes cp = strchr(cp, ','); 3721592Srgrimes if (cp != NULL) { 3731592Srgrimes cp++; 3741592Srgrimes if (*cp == ',') { 37546078Simp warnx("Invalid drive list '%s'", volume_str); 3761592Srgrimes return (EINVAL); 3771592Srgrimes } 3781592Srgrimes } 3791592Srgrimes } 3801592Srgrimes 3811592Srgrimes /* Validate the number of drives for this volume. */ 3821592Srgrimes switch (raid_type) { 3831592Srgrimes case RT_RAID0: 3841592Srgrimes if (count < 2) { 3851592Srgrimes warnx("RAID0 requires at least 2 drives in each " 3861592Srgrimes "array"); 3871592Srgrimes return (EINVAL); 3881592Srgrimes } 3891592Srgrimes break; 3901592Srgrimes case RT_RAID1: 3911592Srgrimes if (count != 2) { 3921592Srgrimes warnx("RAID1 requires exactly 2 drives in each " 3931592Srgrimes "array"); 3941592Srgrimes return (EINVAL); 3951592Srgrimes } 3961592Srgrimes break; 3971592Srgrimes case RT_RAID1E: 3981592Srgrimes if (count < 3) { 3991592Srgrimes warnx("RAID1E requires at least 3 drives in each " 400141588Sru "array"); 4011592Srgrimes return (EINVAL); 4021592Srgrimes } 4031592Srgrimes break; 40490377Simp } 4051592Srgrimes 4061592Srgrimes /* Validate each drive. */ 4071592Srgrimes info->drives = calloc(count, sizeof(struct drive_info)); 4081592Srgrimes info->drive_count = count; 4091592Srgrimes for (dinfo = info->drives; (cp = strsep(&volume_str, ",")) != NULL; 4101592Srgrimes dinfo++) { 4111592Srgrimes /* If this drive is already a RAID phys just fetch the info. */ 4121592Srgrimes error = mpt_lookup_drive(state->list, cp, &PhysDiskNum); 4131592Srgrimes if (error == 0) { 4141592Srgrimes dinfo->info = mpt_pd_info(fd, PhysDiskNum, NULL); 4151592Srgrimes if (dinfo->info == NULL) 4161592Srgrimes return (errno); 4171592Srgrimes continue; 4181592Srgrimes } 4191592Srgrimes 4201592Srgrimes /* See if it is a standalone disk. */ 4211592Srgrimes if (mpt_lookup_standalone_disk(cp, state->sdisks, 4221592Srgrimes state->nsdisks, &i) < 0) { 4231592Srgrimes error = errno; 4241592Srgrimes warn("Unable to lookup drive %s", cp); 4251592Srgrimes return (error); 4261592Srgrimes } 4271592Srgrimes dinfo->sdisk = &state->sdisks[i]; 4281592Srgrimes 4291592Srgrimes /* Lock the disk, we will create phys disk pages later. */ 4301592Srgrimes if (mpt_lock_physdisk(dinfo->sdisk) < 0) 4311592Srgrimes return (errno); 4321592Srgrimes } 4331592Srgrimes 4341592Srgrimes return (0); 4351592Srgrimes} 4361592Srgrimes 4371592Srgrimes/* 4381592Srgrimes * Add RAID physdisk pages for any standalone disks that a volume is 4391592Srgrimes * going to use. 4401592Srgrimes */ 4411592Srgrimesstatic int 4421592Srgrimesadd_drives(int fd, struct volume_info *info, int verbose) 4431592Srgrimes{ 4441592Srgrimes struct drive_info *dinfo; 4451592Srgrimes U8 PhysDiskNum; 446141588Sru int error, i; 4471592Srgrimes 4481592Srgrimes for (i = 0, dinfo = info->drives; i < info->drive_count; 4491592Srgrimes i++, dinfo++) { 4501592Srgrimes if (dinfo->info == NULL) { 4511592Srgrimes if (mpt_create_physdisk(fd, dinfo->sdisk, 4521592Srgrimes &PhysDiskNum) < 0) { 4531592Srgrimes error = errno; 4541592Srgrimes warn( 4551592Srgrimes "Failed to create physical disk page for %s", 456141588Sru dinfo->sdisk->devname); 4571592Srgrimes return (error); 4581592Srgrimes } 4591592Srgrimes if (verbose) 4601592Srgrimes printf("Added drive %s with PhysDiskNum %u\n", 4611592Srgrimes dinfo->sdisk->devname, PhysDiskNum); 4621592Srgrimes 4631592Srgrimes dinfo->info = mpt_pd_info(fd, PhysDiskNum, NULL); 4641592Srgrimes if (dinfo->info == NULL) 4651592Srgrimes return (errno); 4661592Srgrimes } 4671592Srgrimes } 4681592Srgrimes return (0); 4691592Srgrimes} 4701592Srgrimes 4711592Srgrimes/* 4721592Srgrimes * Find the next free target ID assuming that 'target_id' is the last 4731592Srgrimes * one used. 'target_id' should be 0xff for the initial test. 4741592Srgrimes */ 4751592Srgrimesstatic U8 476216584Scharnierfind_next_volume(struct config_id_state *state) 4771592Srgrimes{ 4781592Srgrimes CONFIG_PAGE_IOC_2_RAID_VOL *vol; 479146075Sjmallett int i; 4801592Srgrimes 4811592Srgrimesrestart: 4821592Srgrimes /* Assume the current one is used. */ 4831592Srgrimes state->target_id++; 48490377Simp 4851592Srgrimes /* Search drives first. */ 4861592Srgrimes for (i = 0; i < state->nsdisks; i++) 4871592Srgrimes if (state->sdisks[i].target == state->target_id) 4881592Srgrimes goto restart; 4891592Srgrimes for (i = 0; i < state->list->ndrives; i++) 4901592Srgrimes if (state->list->drives[i]->PhysDiskID == state->target_id) 4911592Srgrimes goto restart; 4921592Srgrimes 4931592Srgrimes /* Search volumes second. */ 4941592Srgrimes vol = state->ioc2->RaidVolume; 4951592Srgrimes for (i = 0; i < state->ioc2->NumActiveVolumes; vol++, i++) 49664238Skris if (vol->VolumeID == state->target_id) 4971592Srgrimes goto restart; 4981592Srgrimes 49964238Skris return (state->target_id); 50081991Sbrian} 50181972Sbrian 5021592Srgrimes/* Create a volume and populate it with drives. */ 5031592Srgrimesstatic CONFIG_PAGE_RAID_VOL_0 * 5041592Srgrimesbuild_volume(int fd, struct volume_info *info, int raid_type, long stripe_size, 5051592Srgrimes struct config_id_state *state, int verbose) 5061592Srgrimes{ 50790377Simp CONFIG_PAGE_HEADER header; 5081592Srgrimes CONFIG_PAGE_RAID_VOL_0 *vol; 50966755Sru RAID_VOL0_PHYS_DISK *rdisk; 5101592Srgrimes struct drive_info *dinfo; 5111592Srgrimes U32 MinLBA; 5121592Srgrimes uint64_t MaxLBA; 5131592Srgrimes size_t page_size; 5141592Srgrimes int error, i; 5151592Srgrimes 5161592Srgrimes error = mpt_read_config_page_header(fd, MPI_CONFIG_PAGETYPE_RAID_VOLUME, 5171592Srgrimes 0, 0, &header, NULL); 51856590Sshin if (error) { 51956939Sshin errno = error; 52056939Sshin return (NULL); 5211592Srgrimes } 5221592Srgrimes if (header.PageVersion > MPI_RAIDVOLPAGE0_PAGEVERSION) { 5231592Srgrimes warnx("Unsupported RAID volume page 0 version %d", 52490377Simp header.PageVersion); 5251592Srgrimes errno = EOPNOTSUPP; 5261592Srgrimes return (NULL); 5271592Srgrimes } 5281592Srgrimes page_size = sizeof(CONFIG_PAGE_RAID_VOL_0) + 52980381Ssheldonh sizeof(RAID_VOL0_PHYS_DISK) * (info->drive_count - 1); 5301592Srgrimes vol = calloc(1, page_size); 5311592Srgrimes if (vol == NULL) 5321592Srgrimes return (NULL); 5331592Srgrimes 5341592Srgrimes /* Header */ 5351592Srgrimes vol->Header.PageType = MPI_CONFIG_PAGETYPE_RAID_VOLUME; 5361592Srgrimes vol->Header.PageNumber = 0; 5371592Srgrimes vol->Header.PageLength = page_size / 4; 5381592Srgrimes 5391592Srgrimes /* Properties */ 54090377Simp vol->VolumeID = find_next_volume(state); 5411592Srgrimes vol->VolumeBus = 0; 542229403Sed vol->VolumeIOC = 0; /* XXX */ 5431592Srgrimes vol->VolumeStatus.Flags = MPI_RAIDVOL0_STATUS_FLAG_ENABLED; 544214680Sed vol->VolumeStatus.State = MPI_RAIDVOL0_STATUS_STATE_OPTIMAL; 5451592Srgrimes vol->VolumeSettings.Settings = MPI_RAIDVOL0_SETTING_USE_DEFAULTS; 546229403Sed vol->VolumeSettings.HotSparePool = MPI_RAID_HOT_SPARE_POOL_0; 5471592Srgrimes vol->NumPhysDisks = info->drive_count; 5481592Srgrimes 5491592Srgrimes /* Find the smallest drive. */ 5501592Srgrimes MinLBA = info->drives[0].info->MaxLBA; 5511592Srgrimes for (i = 1; i < info->drive_count; i++) 552229403Sed if (info->drives[i].info->MaxLBA < MinLBA) 5531592Srgrimes MinLBA = info->drives[i].info->MaxLBA; 5541592Srgrimes 5551592Srgrimes /* 5561592Srgrimes * Now chop off 512MB at the end to leave room for the 5571592Srgrimes * metadata. The controller might only use 64MB, but we just 558214680Sed * chop off the max to be simple. 559214680Sed */ 560214680Sed MinLBA -= (512 * 1024 * 1024) / 512; 561214680Sed 5621592Srgrimes switch (raid_type) { 5631592Srgrimes case RT_RAID0: 5641592Srgrimes vol->VolumeType = MPI_RAID_VOL_TYPE_IS; 5651592Srgrimes vol->StripeSize = stripe_size / 512; 5661592Srgrimes MaxLBA = (uint64_t)MinLBA * info->drive_count; 567229403Sed break; 5681592Srgrimes case RT_RAID1: 5691592Srgrimes vol->VolumeType = MPI_RAID_VOL_TYPE_IM; 5701592Srgrimes MaxLBA = (uint64_t)MinLBA * (info->drive_count / 2); 5711592Srgrimes break; 5721592Srgrimes case RT_RAID1E: 5731592Srgrimes vol->VolumeType = MPI_RAID_VOL_TYPE_IME; 5741592Srgrimes vol->StripeSize = stripe_size / 512; 5751592Srgrimes MaxLBA = (uint64_t)MinLBA * info->drive_count / 2; 5761592Srgrimes break; 5771592Srgrimes default: 5781592Srgrimes /* Pacify gcc. */ 5791592Srgrimes abort(); 5801592Srgrimes } 5811592Srgrimes 58290377Simp /* 5831592Srgrimes * If the controller doesn't support 64-bit addressing and the 58451433Smarkm * new volume is larger than 2^32 blocks, warn the user and 5851592Srgrimes * 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 620static int 621create_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 772skip: 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} 786MPT_COMMAND(top, create, create_volume); 787 788static int 789delete_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} 833MPT_COMMAND(top, delete, delete_volume); 834 835static int 836find_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 917static int 918add_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} 1014MPT_COMMAND(top, add, add_spare); 1015 1016static int 1017remove_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} 1080MPT_COMMAND(top, remove, remove_spare); 1081 1082#ifdef DEBUG 1083MPT_TABLE(top, pd); 1084 1085static int 1086pd_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} 1132MPT_COMMAND(pd, create, pd_create); 1133 1134static int 1135pd_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} 1184MPT_COMMAND(pd, delete, pd_delete); 1185 1186/* Display raw data about a volume config. */ 1187static void 1188dump_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 1216static int 1217debug_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} 1254MPT_COMMAND(top, debug, debug_config); 1255#endif 1256