189b17223SAlexander Motin /*- 289b17223SAlexander Motin * Copyright (c) 2011 Alexander Motin <mav@FreeBSD.org> 3e26083caSAlexander Motin * Copyright (c) 2000 - 2008 Søren Schmidt <sos@FreeBSD.org> 489b17223SAlexander Motin * All rights reserved. 589b17223SAlexander Motin * 689b17223SAlexander Motin * Redistribution and use in source and binary forms, with or without 789b17223SAlexander Motin * modification, are permitted provided that the following conditions 889b17223SAlexander Motin * are met: 989b17223SAlexander Motin * 1. Redistributions of source code must retain the above copyright 1089b17223SAlexander Motin * notice, this list of conditions and the following disclaimer. 1189b17223SAlexander Motin * 2. Redistributions in binary form must reproduce the above copyright 1289b17223SAlexander Motin * notice, this list of conditions and the following disclaimer in the 1389b17223SAlexander Motin * documentation and/or other materials provided with the distribution. 1489b17223SAlexander Motin * 1589b17223SAlexander Motin * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 1689b17223SAlexander Motin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1789b17223SAlexander Motin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1889b17223SAlexander Motin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 1989b17223SAlexander Motin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2089b17223SAlexander Motin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2189b17223SAlexander Motin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2289b17223SAlexander Motin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2389b17223SAlexander Motin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2489b17223SAlexander Motin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2589b17223SAlexander Motin * SUCH DAMAGE. 2689b17223SAlexander Motin */ 2789b17223SAlexander Motin 2889b17223SAlexander Motin #include <sys/cdefs.h> 2989b17223SAlexander Motin __FBSDID("$FreeBSD$"); 3089b17223SAlexander Motin 3189b17223SAlexander Motin #include <sys/param.h> 3289b17223SAlexander Motin #include <sys/bio.h> 3389b17223SAlexander Motin #include <sys/endian.h> 3489b17223SAlexander Motin #include <sys/kernel.h> 3589b17223SAlexander Motin #include <sys/kobj.h> 3689b17223SAlexander Motin #include <sys/limits.h> 3789b17223SAlexander Motin #include <sys/lock.h> 3889b17223SAlexander Motin #include <sys/malloc.h> 3989b17223SAlexander Motin #include <sys/mutex.h> 4089b17223SAlexander Motin #include <sys/systm.h> 4189b17223SAlexander Motin #include <sys/taskqueue.h> 4289b17223SAlexander Motin #include <geom/geom.h> 4389b17223SAlexander Motin #include "geom/raid/g_raid.h" 4489b17223SAlexander Motin #include "g_raid_md_if.h" 4589b17223SAlexander Motin 4689b17223SAlexander Motin static MALLOC_DEFINE(M_MD_NVIDIA, "md_nvidia_data", "GEOM_RAID NVIDIA metadata"); 4789b17223SAlexander Motin 4889b17223SAlexander Motin struct nvidia_raid_conf { 4989b17223SAlexander Motin uint8_t nvidia_id[8]; 5089b17223SAlexander Motin #define NVIDIA_MAGIC "NVIDIA " 5189b17223SAlexander Motin 5289b17223SAlexander Motin uint32_t config_size; 5389b17223SAlexander Motin uint32_t checksum; 5489b17223SAlexander Motin uint16_t version; 5589b17223SAlexander Motin uint8_t disk_number; 5689b17223SAlexander Motin uint8_t dummy_0; 5789b17223SAlexander Motin uint32_t total_sectors; 5889b17223SAlexander Motin uint32_t sector_size; 5989b17223SAlexander Motin uint8_t name[16]; 6089b17223SAlexander Motin uint8_t revision[4]; 6189b17223SAlexander Motin uint32_t disk_status; 6289b17223SAlexander Motin 6389b17223SAlexander Motin uint32_t magic_0; 6489b17223SAlexander Motin #define NVIDIA_MAGIC0 0x00640044 6589b17223SAlexander Motin 6689b17223SAlexander Motin uint64_t volume_id[2]; 6789b17223SAlexander Motin uint8_t state; 6889b17223SAlexander Motin #define NVIDIA_S_IDLE 0 6989b17223SAlexander Motin #define NVIDIA_S_INIT 2 7089b17223SAlexander Motin #define NVIDIA_S_REBUILD 3 7189b17223SAlexander Motin #define NVIDIA_S_UPGRADE 4 7289b17223SAlexander Motin #define NVIDIA_S_SYNC 5 7389b17223SAlexander Motin uint8_t array_width; 7489b17223SAlexander Motin uint8_t total_disks; 7589b17223SAlexander Motin uint8_t orig_array_width; 7689b17223SAlexander Motin uint16_t type; 7789b17223SAlexander Motin #define NVIDIA_T_RAID0 0x0080 7889b17223SAlexander Motin #define NVIDIA_T_RAID1 0x0081 7989b17223SAlexander Motin #define NVIDIA_T_RAID3 0x0083 8089b17223SAlexander Motin #define NVIDIA_T_RAID5 0x0085 /* RLQ = 00/02? */ 8189b17223SAlexander Motin #define NVIDIA_T_RAID5_SYM 0x0095 /* RLQ = 03 */ 8289b17223SAlexander Motin #define NVIDIA_T_RAID10 0x008a 8389b17223SAlexander Motin #define NVIDIA_T_RAID01 0x8180 8489b17223SAlexander Motin #define NVIDIA_T_CONCAT 0x00ff 8589b17223SAlexander Motin 8689b17223SAlexander Motin uint16_t dummy_3; 8789b17223SAlexander Motin uint32_t strip_sectors; 8889b17223SAlexander Motin uint32_t strip_bytes; 8989b17223SAlexander Motin uint32_t strip_shift; 9089b17223SAlexander Motin uint32_t strip_mask; 9189b17223SAlexander Motin uint32_t stripe_sectors; 9289b17223SAlexander Motin uint32_t stripe_bytes; 9389b17223SAlexander Motin uint32_t rebuild_lba; 9489b17223SAlexander Motin uint32_t orig_type; 9589b17223SAlexander Motin uint32_t orig_total_sectors; 9689b17223SAlexander Motin uint32_t status; 9789b17223SAlexander Motin #define NVIDIA_S_BOOTABLE 0x00000001 9889b17223SAlexander Motin #define NVIDIA_S_DEGRADED 0x00000002 9989b17223SAlexander Motin 10089b17223SAlexander Motin uint32_t filler[98]; 10189b17223SAlexander Motin } __packed; 10289b17223SAlexander Motin 10389b17223SAlexander Motin struct g_raid_md_nvidia_perdisk { 10489b17223SAlexander Motin struct nvidia_raid_conf *pd_meta; 10589b17223SAlexander Motin int pd_disk_pos; 10689b17223SAlexander Motin off_t pd_disk_size; 10789b17223SAlexander Motin }; 10889b17223SAlexander Motin 10989b17223SAlexander Motin struct g_raid_md_nvidia_object { 11089b17223SAlexander Motin struct g_raid_md_object mdio_base; 11189b17223SAlexander Motin uint64_t mdio_volume_id[2]; 11289b17223SAlexander Motin struct nvidia_raid_conf *mdio_meta; 11389b17223SAlexander Motin struct callout mdio_start_co; /* STARTING state timer. */ 11489b17223SAlexander Motin int mdio_total_disks; 11589b17223SAlexander Motin int mdio_disks_present; 11689b17223SAlexander Motin int mdio_started; 11789b17223SAlexander Motin int mdio_incomplete; 11889b17223SAlexander Motin struct root_hold_token *mdio_rootmount; /* Root mount delay token. */ 11989b17223SAlexander Motin }; 12089b17223SAlexander Motin 12189b17223SAlexander Motin static g_raid_md_create_t g_raid_md_create_nvidia; 12289b17223SAlexander Motin static g_raid_md_taste_t g_raid_md_taste_nvidia; 12389b17223SAlexander Motin static g_raid_md_event_t g_raid_md_event_nvidia; 12489b17223SAlexander Motin static g_raid_md_ctl_t g_raid_md_ctl_nvidia; 12589b17223SAlexander Motin static g_raid_md_write_t g_raid_md_write_nvidia; 12689b17223SAlexander Motin static g_raid_md_fail_disk_t g_raid_md_fail_disk_nvidia; 12789b17223SAlexander Motin static g_raid_md_free_disk_t g_raid_md_free_disk_nvidia; 12889b17223SAlexander Motin static g_raid_md_free_t g_raid_md_free_nvidia; 12989b17223SAlexander Motin 13089b17223SAlexander Motin static kobj_method_t g_raid_md_nvidia_methods[] = { 13189b17223SAlexander Motin KOBJMETHOD(g_raid_md_create, g_raid_md_create_nvidia), 13289b17223SAlexander Motin KOBJMETHOD(g_raid_md_taste, g_raid_md_taste_nvidia), 13389b17223SAlexander Motin KOBJMETHOD(g_raid_md_event, g_raid_md_event_nvidia), 13489b17223SAlexander Motin KOBJMETHOD(g_raid_md_ctl, g_raid_md_ctl_nvidia), 13589b17223SAlexander Motin KOBJMETHOD(g_raid_md_write, g_raid_md_write_nvidia), 13689b17223SAlexander Motin KOBJMETHOD(g_raid_md_fail_disk, g_raid_md_fail_disk_nvidia), 13789b17223SAlexander Motin KOBJMETHOD(g_raid_md_free_disk, g_raid_md_free_disk_nvidia), 13889b17223SAlexander Motin KOBJMETHOD(g_raid_md_free, g_raid_md_free_nvidia), 13989b17223SAlexander Motin { 0, 0 } 14089b17223SAlexander Motin }; 14189b17223SAlexander Motin 14289b17223SAlexander Motin static struct g_raid_md_class g_raid_md_nvidia_class = { 14389b17223SAlexander Motin "NVIDIA", 14489b17223SAlexander Motin g_raid_md_nvidia_methods, 14589b17223SAlexander Motin sizeof(struct g_raid_md_nvidia_object), 146c89d2fbeSAlexander Motin .mdc_enable = 1, 14789b17223SAlexander Motin .mdc_priority = 100 14889b17223SAlexander Motin }; 14989b17223SAlexander Motin 15089b17223SAlexander Motin static int NVIDIANodeID = 1; 15189b17223SAlexander Motin 15289b17223SAlexander Motin static void 15389b17223SAlexander Motin g_raid_md_nvidia_print(struct nvidia_raid_conf *meta) 15489b17223SAlexander Motin { 15589b17223SAlexander Motin 15689b17223SAlexander Motin if (g_raid_debug < 1) 15789b17223SAlexander Motin return; 15889b17223SAlexander Motin 15989b17223SAlexander Motin printf("********* ATA NVIDIA RAID Metadata *********\n"); 16089b17223SAlexander Motin printf("nvidia_id <%.8s>\n", meta->nvidia_id); 16189b17223SAlexander Motin printf("config_size %u\n", meta->config_size); 16289b17223SAlexander Motin printf("checksum 0x%08x\n", meta->checksum); 16389b17223SAlexander Motin printf("version 0x%04x\n", meta->version); 16489b17223SAlexander Motin printf("disk_number %d\n", meta->disk_number); 16589b17223SAlexander Motin printf("dummy_0 0x%02x\n", meta->dummy_0); 16689b17223SAlexander Motin printf("total_sectors %u\n", meta->total_sectors); 16789b17223SAlexander Motin printf("sector_size %u\n", meta->sector_size); 16889b17223SAlexander Motin printf("name <%.16s>\n", meta->name); 16989b17223SAlexander Motin printf("revision 0x%02x%02x%02x%02x\n", 17089b17223SAlexander Motin meta->revision[0], meta->revision[1], 17189b17223SAlexander Motin meta->revision[2], meta->revision[3]); 17289b17223SAlexander Motin printf("disk_status 0x%08x\n", meta->disk_status); 17389b17223SAlexander Motin printf("magic_0 0x%08x\n", meta->magic_0); 17489b17223SAlexander Motin printf("volume_id 0x%016jx%016jx\n", 17589b17223SAlexander Motin meta->volume_id[1], meta->volume_id[0]); 17689b17223SAlexander Motin printf("state 0x%02x\n", meta->state); 17789b17223SAlexander Motin printf("array_width %u\n", meta->array_width); 17889b17223SAlexander Motin printf("total_disks %u\n", meta->total_disks); 17989b17223SAlexander Motin printf("orig_array_width %u\n", meta->orig_array_width); 18089b17223SAlexander Motin printf("type 0x%04x\n", meta->type); 18189b17223SAlexander Motin printf("dummy_3 0x%04x\n", meta->dummy_3); 18289b17223SAlexander Motin printf("strip_sectors %u\n", meta->strip_sectors); 18389b17223SAlexander Motin printf("strip_bytes %u\n", meta->strip_bytes); 18489b17223SAlexander Motin printf("strip_shift %u\n", meta->strip_shift); 18589b17223SAlexander Motin printf("strip_mask 0x%08x\n", meta->strip_mask); 18689b17223SAlexander Motin printf("stripe_sectors %u\n", meta->stripe_sectors); 18789b17223SAlexander Motin printf("stripe_bytes %u\n", meta->stripe_bytes); 18889b17223SAlexander Motin printf("rebuild_lba %u\n", meta->rebuild_lba); 18989b17223SAlexander Motin printf("orig_type 0x%04x\n", meta->orig_type); 19089b17223SAlexander Motin printf("orig_total_sectors %u\n", meta->orig_total_sectors); 19189b17223SAlexander Motin printf("status 0x%08x\n", meta->status); 19289b17223SAlexander Motin printf("=================================================\n"); 19389b17223SAlexander Motin } 19489b17223SAlexander Motin 19589b17223SAlexander Motin static struct nvidia_raid_conf * 19689b17223SAlexander Motin nvidia_meta_copy(struct nvidia_raid_conf *meta) 19789b17223SAlexander Motin { 19889b17223SAlexander Motin struct nvidia_raid_conf *nmeta; 19989b17223SAlexander Motin 20089b17223SAlexander Motin nmeta = malloc(sizeof(*meta), M_MD_NVIDIA, M_WAITOK); 20189b17223SAlexander Motin memcpy(nmeta, meta, sizeof(*meta)); 20289b17223SAlexander Motin return (nmeta); 20389b17223SAlexander Motin } 20489b17223SAlexander Motin 20589b17223SAlexander Motin static int 20689b17223SAlexander Motin nvidia_meta_translate_disk(struct nvidia_raid_conf *meta, int md_disk_pos) 20789b17223SAlexander Motin { 20889b17223SAlexander Motin int disk_pos; 20989b17223SAlexander Motin 21089b17223SAlexander Motin if (md_disk_pos >= 0 && meta->type == NVIDIA_T_RAID01) { 21189b17223SAlexander Motin disk_pos = (md_disk_pos / meta->array_width) + 21289b17223SAlexander Motin (md_disk_pos % meta->array_width) * meta->array_width; 21389b17223SAlexander Motin } else 21489b17223SAlexander Motin disk_pos = md_disk_pos; 21589b17223SAlexander Motin return (disk_pos); 21689b17223SAlexander Motin } 21789b17223SAlexander Motin 21889b17223SAlexander Motin static void 21989b17223SAlexander Motin nvidia_meta_get_name(struct nvidia_raid_conf *meta, char *buf) 22089b17223SAlexander Motin { 22189b17223SAlexander Motin int i; 22289b17223SAlexander Motin 22389b17223SAlexander Motin strncpy(buf, meta->name, 16); 22489b17223SAlexander Motin buf[16] = 0; 22589b17223SAlexander Motin for (i = 15; i >= 0; i--) { 22689b17223SAlexander Motin if (buf[i] > 0x20) 22789b17223SAlexander Motin break; 22889b17223SAlexander Motin buf[i] = 0; 22989b17223SAlexander Motin } 23089b17223SAlexander Motin } 23189b17223SAlexander Motin 23289b17223SAlexander Motin static void 23389b17223SAlexander Motin nvidia_meta_put_name(struct nvidia_raid_conf *meta, char *buf) 23489b17223SAlexander Motin { 23589b17223SAlexander Motin 23689b17223SAlexander Motin memset(meta->name, 0x20, 16); 23789b17223SAlexander Motin memcpy(meta->name, buf, MIN(strlen(buf), 16)); 23889b17223SAlexander Motin } 23989b17223SAlexander Motin 24089b17223SAlexander Motin static struct nvidia_raid_conf * 24189b17223SAlexander Motin nvidia_meta_read(struct g_consumer *cp) 24289b17223SAlexander Motin { 24389b17223SAlexander Motin struct g_provider *pp; 24489b17223SAlexander Motin struct nvidia_raid_conf *meta; 24589b17223SAlexander Motin char *buf; 24689b17223SAlexander Motin int error, i; 24789b17223SAlexander Motin uint32_t checksum, *ptr; 24889b17223SAlexander Motin 24989b17223SAlexander Motin pp = cp->provider; 25089b17223SAlexander Motin 25189b17223SAlexander Motin /* Read the anchor sector. */ 25289b17223SAlexander Motin buf = g_read_data(cp, 25389b17223SAlexander Motin pp->mediasize - 2 * pp->sectorsize, pp->sectorsize, &error); 25489b17223SAlexander Motin if (buf == NULL) { 25589b17223SAlexander Motin G_RAID_DEBUG(1, "Cannot read metadata from %s (error=%d).", 25689b17223SAlexander Motin pp->name, error); 25789b17223SAlexander Motin return (NULL); 25889b17223SAlexander Motin } 2591e68fe9cSAlexander Motin meta = (struct nvidia_raid_conf *)buf; 26089b17223SAlexander Motin 26189b17223SAlexander Motin /* Check if this is an NVIDIA RAID struct */ 26289b17223SAlexander Motin if (strncmp(meta->nvidia_id, NVIDIA_MAGIC, strlen(NVIDIA_MAGIC))) { 26389b17223SAlexander Motin G_RAID_DEBUG(1, "NVIDIA signature check failed on %s", pp->name); 2641e68fe9cSAlexander Motin g_free(buf); 26589b17223SAlexander Motin return (NULL); 26689b17223SAlexander Motin } 26789b17223SAlexander Motin if (meta->config_size > 128 || 26889b17223SAlexander Motin meta->config_size < 30) { 26989b17223SAlexander Motin G_RAID_DEBUG(1, "NVIDIA metadata size looks wrong: %d", 27089b17223SAlexander Motin meta->config_size); 2711e68fe9cSAlexander Motin g_free(buf); 27289b17223SAlexander Motin return (NULL); 27389b17223SAlexander Motin } 2741e68fe9cSAlexander Motin meta = malloc(sizeof(*meta), M_MD_NVIDIA, M_WAITOK); 2751e68fe9cSAlexander Motin memcpy(meta, buf, min(sizeof(*meta), pp->sectorsize)); 2761e68fe9cSAlexander Motin g_free(buf); 27789b17223SAlexander Motin 27889b17223SAlexander Motin /* Check metadata checksum. */ 27989b17223SAlexander Motin for (checksum = 0, ptr = (uint32_t *)meta, 28089b17223SAlexander Motin i = 0; i < meta->config_size; i++) 28189b17223SAlexander Motin checksum += *ptr++; 28289b17223SAlexander Motin if (checksum != 0) { 28389b17223SAlexander Motin G_RAID_DEBUG(1, "NVIDIA checksum check failed on %s", pp->name); 28489b17223SAlexander Motin free(meta, M_MD_NVIDIA); 28589b17223SAlexander Motin return (NULL); 28689b17223SAlexander Motin } 28789b17223SAlexander Motin 28889b17223SAlexander Motin /* Check volume state. */ 28989b17223SAlexander Motin if (meta->state != NVIDIA_S_IDLE && meta->state != NVIDIA_S_INIT && 29089b17223SAlexander Motin meta->state != NVIDIA_S_REBUILD && meta->state != NVIDIA_S_SYNC) { 29189b17223SAlexander Motin G_RAID_DEBUG(1, "NVIDIA unknown state on %s (0x%02x)", 29289b17223SAlexander Motin pp->name, meta->state); 29389b17223SAlexander Motin free(meta, M_MD_NVIDIA); 29489b17223SAlexander Motin return (NULL); 29589b17223SAlexander Motin } 29689b17223SAlexander Motin 29789b17223SAlexander Motin /* Check raid type. */ 29889b17223SAlexander Motin if (meta->type != NVIDIA_T_RAID0 && meta->type != NVIDIA_T_RAID1 && 29989b17223SAlexander Motin meta->type != NVIDIA_T_RAID3 && meta->type != NVIDIA_T_RAID5 && 30089b17223SAlexander Motin meta->type != NVIDIA_T_RAID5_SYM && 30189b17223SAlexander Motin meta->type != NVIDIA_T_RAID01 && meta->type != NVIDIA_T_CONCAT) { 30289b17223SAlexander Motin G_RAID_DEBUG(1, "NVIDIA unknown RAID level on %s (0x%02x)", 30389b17223SAlexander Motin pp->name, meta->type); 30489b17223SAlexander Motin free(meta, M_MD_NVIDIA); 30589b17223SAlexander Motin return (NULL); 30689b17223SAlexander Motin } 30789b17223SAlexander Motin 30889b17223SAlexander Motin return (meta); 30989b17223SAlexander Motin } 31089b17223SAlexander Motin 31189b17223SAlexander Motin static int 31289b17223SAlexander Motin nvidia_meta_write(struct g_consumer *cp, struct nvidia_raid_conf *meta) 31389b17223SAlexander Motin { 31489b17223SAlexander Motin struct g_provider *pp; 31589b17223SAlexander Motin char *buf; 31689b17223SAlexander Motin int error, i; 31789b17223SAlexander Motin uint32_t checksum, *ptr; 31889b17223SAlexander Motin 31989b17223SAlexander Motin pp = cp->provider; 32089b17223SAlexander Motin 32189b17223SAlexander Motin /* Recalculate checksum for case if metadata were changed. */ 32289b17223SAlexander Motin meta->checksum = 0; 32389b17223SAlexander Motin for (checksum = 0, ptr = (uint32_t *)meta, 32489b17223SAlexander Motin i = 0; i < meta->config_size; i++) 32589b17223SAlexander Motin checksum += *ptr++; 32689b17223SAlexander Motin meta->checksum -= checksum; 32789b17223SAlexander Motin 32889b17223SAlexander Motin /* Create and fill buffer. */ 32989b17223SAlexander Motin buf = malloc(pp->sectorsize, M_MD_NVIDIA, M_WAITOK | M_ZERO); 33089b17223SAlexander Motin memcpy(buf, meta, sizeof(*meta)); 33189b17223SAlexander Motin 33289b17223SAlexander Motin /* Write metadata. */ 33389b17223SAlexander Motin error = g_write_data(cp, 33489b17223SAlexander Motin pp->mediasize - 2 * pp->sectorsize, buf, pp->sectorsize); 33589b17223SAlexander Motin if (error != 0) { 33689b17223SAlexander Motin G_RAID_DEBUG(1, "Cannot write metadata to %s (error=%d).", 33789b17223SAlexander Motin pp->name, error); 33889b17223SAlexander Motin } 33989b17223SAlexander Motin 34089b17223SAlexander Motin free(buf, M_MD_NVIDIA); 34189b17223SAlexander Motin return (error); 34289b17223SAlexander Motin } 34389b17223SAlexander Motin 34489b17223SAlexander Motin static int 34589b17223SAlexander Motin nvidia_meta_erase(struct g_consumer *cp) 34689b17223SAlexander Motin { 34789b17223SAlexander Motin struct g_provider *pp; 34889b17223SAlexander Motin char *buf; 34989b17223SAlexander Motin int error; 35089b17223SAlexander Motin 35189b17223SAlexander Motin pp = cp->provider; 35289b17223SAlexander Motin buf = malloc(pp->sectorsize, M_MD_NVIDIA, M_WAITOK | M_ZERO); 35389b17223SAlexander Motin error = g_write_data(cp, 35489b17223SAlexander Motin pp->mediasize - 2 * pp->sectorsize, buf, pp->sectorsize); 35589b17223SAlexander Motin if (error != 0) { 35689b17223SAlexander Motin G_RAID_DEBUG(1, "Cannot erase metadata on %s (error=%d).", 35789b17223SAlexander Motin pp->name, error); 35889b17223SAlexander Motin } 35989b17223SAlexander Motin free(buf, M_MD_NVIDIA); 36089b17223SAlexander Motin return (error); 36189b17223SAlexander Motin } 36289b17223SAlexander Motin 36389b17223SAlexander Motin static struct g_raid_disk * 36489b17223SAlexander Motin g_raid_md_nvidia_get_disk(struct g_raid_softc *sc, int id) 36589b17223SAlexander Motin { 36689b17223SAlexander Motin struct g_raid_disk *disk; 36789b17223SAlexander Motin struct g_raid_md_nvidia_perdisk *pd; 36889b17223SAlexander Motin 36989b17223SAlexander Motin TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 37089b17223SAlexander Motin pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data; 37189b17223SAlexander Motin if (pd->pd_disk_pos == id) 37289b17223SAlexander Motin break; 37389b17223SAlexander Motin } 37489b17223SAlexander Motin return (disk); 37589b17223SAlexander Motin } 37689b17223SAlexander Motin 37789b17223SAlexander Motin static int 37889b17223SAlexander Motin g_raid_md_nvidia_supported(int level, int qual, int disks, int force) 37989b17223SAlexander Motin { 38089b17223SAlexander Motin 38189b17223SAlexander Motin switch (level) { 38289b17223SAlexander Motin case G_RAID_VOLUME_RL_RAID0: 38389b17223SAlexander Motin if (disks < 1) 38489b17223SAlexander Motin return (0); 38589b17223SAlexander Motin if (!force && (disks < 2 || disks > 6)) 38689b17223SAlexander Motin return (0); 38789b17223SAlexander Motin break; 38889b17223SAlexander Motin case G_RAID_VOLUME_RL_RAID1: 38989b17223SAlexander Motin if (disks < 1) 39089b17223SAlexander Motin return (0); 39189b17223SAlexander Motin if (!force && (disks != 2)) 39289b17223SAlexander Motin return (0); 39389b17223SAlexander Motin break; 39489b17223SAlexander Motin case G_RAID_VOLUME_RL_RAID1E: 39589b17223SAlexander Motin if (disks < 2) 39689b17223SAlexander Motin return (0); 39789b17223SAlexander Motin if (disks % 2 != 0) 39889b17223SAlexander Motin return (0); 39989b17223SAlexander Motin if (!force && (disks < 4)) 40089b17223SAlexander Motin return (0); 40189b17223SAlexander Motin break; 40289b17223SAlexander Motin case G_RAID_VOLUME_RL_SINGLE: 40389b17223SAlexander Motin if (disks != 1) 40489b17223SAlexander Motin return (0); 40589b17223SAlexander Motin break; 40689b17223SAlexander Motin case G_RAID_VOLUME_RL_CONCAT: 40789b17223SAlexander Motin if (disks < 2) 40889b17223SAlexander Motin return (0); 40989b17223SAlexander Motin break; 41089b17223SAlexander Motin case G_RAID_VOLUME_RL_RAID5: 41189b17223SAlexander Motin if (disks < 3) 41289b17223SAlexander Motin return (0); 413fc1de960SAlexander Motin if (qual != G_RAID_VOLUME_RLQ_R5LA && 414fc1de960SAlexander Motin qual != G_RAID_VOLUME_RLQ_R5LS) 415fc1de960SAlexander Motin return (0); 41689b17223SAlexander Motin break; 41789b17223SAlexander Motin default: 41889b17223SAlexander Motin return (0); 41989b17223SAlexander Motin } 420fc1de960SAlexander Motin if (level != G_RAID_VOLUME_RL_RAID5 && qual != G_RAID_VOLUME_RLQ_NONE) 42189b17223SAlexander Motin return (0); 42289b17223SAlexander Motin return (1); 42389b17223SAlexander Motin } 42489b17223SAlexander Motin 42589b17223SAlexander Motin static int 42689b17223SAlexander Motin g_raid_md_nvidia_start_disk(struct g_raid_disk *disk) 42789b17223SAlexander Motin { 42889b17223SAlexander Motin struct g_raid_softc *sc; 42989b17223SAlexander Motin struct g_raid_subdisk *sd, *tmpsd; 43089b17223SAlexander Motin struct g_raid_disk *olddisk, *tmpdisk; 43189b17223SAlexander Motin struct g_raid_md_object *md; 43289b17223SAlexander Motin struct g_raid_md_nvidia_object *mdi; 43389b17223SAlexander Motin struct g_raid_md_nvidia_perdisk *pd, *oldpd; 43489b17223SAlexander Motin struct nvidia_raid_conf *meta; 43589b17223SAlexander Motin int disk_pos, resurrection = 0; 43689b17223SAlexander Motin 43789b17223SAlexander Motin sc = disk->d_softc; 43889b17223SAlexander Motin md = sc->sc_md; 43989b17223SAlexander Motin mdi = (struct g_raid_md_nvidia_object *)md; 44089b17223SAlexander Motin meta = mdi->mdio_meta; 44189b17223SAlexander Motin pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data; 44289b17223SAlexander Motin olddisk = NULL; 44389b17223SAlexander Motin 444*28323addSBryan Drewery /* Find disk position in metadata by its serial. */ 44589b17223SAlexander Motin if (pd->pd_meta != NULL) { 44689b17223SAlexander Motin disk_pos = pd->pd_meta->disk_number; 44789b17223SAlexander Motin if (disk_pos >= meta->total_disks || mdi->mdio_started) 44889b17223SAlexander Motin disk_pos = -3; 44989b17223SAlexander Motin } else 45089b17223SAlexander Motin disk_pos = -3; 45189b17223SAlexander Motin /* For RAID0+1 we need to translate order. */ 45289b17223SAlexander Motin disk_pos = nvidia_meta_translate_disk(meta, disk_pos); 45389b17223SAlexander Motin if (disk_pos < 0) { 45489b17223SAlexander Motin G_RAID_DEBUG1(1, sc, "Unknown, probably new or stale disk"); 45589b17223SAlexander Motin /* If we are in the start process, that's all for now. */ 45689b17223SAlexander Motin if (!mdi->mdio_started) 45789b17223SAlexander Motin goto nofit; 45889b17223SAlexander Motin /* 45989b17223SAlexander Motin * If we have already started - try to get use of the disk. 46089b17223SAlexander Motin * Try to replace OFFLINE disks first, then FAILED. 46189b17223SAlexander Motin */ 46289b17223SAlexander Motin TAILQ_FOREACH(tmpdisk, &sc->sc_disks, d_next) { 46389b17223SAlexander Motin if (tmpdisk->d_state != G_RAID_DISK_S_OFFLINE && 46489b17223SAlexander Motin tmpdisk->d_state != G_RAID_DISK_S_FAILED) 46589b17223SAlexander Motin continue; 46689b17223SAlexander Motin /* Make sure this disk is big enough. */ 46789b17223SAlexander Motin TAILQ_FOREACH(sd, &tmpdisk->d_subdisks, sd_next) { 46889b17223SAlexander Motin if (sd->sd_offset + sd->sd_size + 2 * 512 > 46989b17223SAlexander Motin pd->pd_disk_size) { 47089b17223SAlexander Motin G_RAID_DEBUG1(1, sc, 47189b17223SAlexander Motin "Disk too small (%ju < %ju)", 47289b17223SAlexander Motin pd->pd_disk_size, 47389b17223SAlexander Motin sd->sd_offset + sd->sd_size + 512); 47489b17223SAlexander Motin break; 47589b17223SAlexander Motin } 47689b17223SAlexander Motin } 47789b17223SAlexander Motin if (sd != NULL) 47889b17223SAlexander Motin continue; 47989b17223SAlexander Motin if (tmpdisk->d_state == G_RAID_DISK_S_OFFLINE) { 48089b17223SAlexander Motin olddisk = tmpdisk; 48189b17223SAlexander Motin break; 48289b17223SAlexander Motin } else if (olddisk == NULL) 48389b17223SAlexander Motin olddisk = tmpdisk; 48489b17223SAlexander Motin } 48589b17223SAlexander Motin if (olddisk == NULL) { 48689b17223SAlexander Motin nofit: 48789b17223SAlexander Motin g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE); 48889b17223SAlexander Motin return (1); 48989b17223SAlexander Motin } 49089b17223SAlexander Motin oldpd = (struct g_raid_md_nvidia_perdisk *)olddisk->d_md_data; 49189b17223SAlexander Motin disk_pos = oldpd->pd_disk_pos; 49289b17223SAlexander Motin resurrection = 1; 49389b17223SAlexander Motin } 49489b17223SAlexander Motin 49589b17223SAlexander Motin if (olddisk == NULL) { 49689b17223SAlexander Motin /* Find placeholder by position. */ 49789b17223SAlexander Motin olddisk = g_raid_md_nvidia_get_disk(sc, disk_pos); 49889b17223SAlexander Motin if (olddisk == NULL) 49989b17223SAlexander Motin panic("No disk at position %d!", disk_pos); 50089b17223SAlexander Motin if (olddisk->d_state != G_RAID_DISK_S_OFFLINE) { 5016bc3fe5fSPedro F. Giffuni G_RAID_DEBUG1(1, sc, "More than one disk for pos %d", 50289b17223SAlexander Motin disk_pos); 50389b17223SAlexander Motin g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE); 50489b17223SAlexander Motin return (0); 50589b17223SAlexander Motin } 50689b17223SAlexander Motin oldpd = (struct g_raid_md_nvidia_perdisk *)olddisk->d_md_data; 50789b17223SAlexander Motin } 50889b17223SAlexander Motin 50989b17223SAlexander Motin /* Replace failed disk or placeholder with new disk. */ 51089b17223SAlexander Motin TAILQ_FOREACH_SAFE(sd, &olddisk->d_subdisks, sd_next, tmpsd) { 51189b17223SAlexander Motin TAILQ_REMOVE(&olddisk->d_subdisks, sd, sd_next); 51289b17223SAlexander Motin TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next); 51389b17223SAlexander Motin sd->sd_disk = disk; 51489b17223SAlexander Motin } 51589b17223SAlexander Motin oldpd->pd_disk_pos = -2; 51689b17223SAlexander Motin pd->pd_disk_pos = disk_pos; 51789b17223SAlexander Motin 51889b17223SAlexander Motin /* If it was placeholder -- destroy it. */ 51989b17223SAlexander Motin if (olddisk->d_state == G_RAID_DISK_S_OFFLINE) { 52089b17223SAlexander Motin g_raid_destroy_disk(olddisk); 52189b17223SAlexander Motin } else { 52289b17223SAlexander Motin /* Otherwise, make it STALE_FAILED. */ 52389b17223SAlexander Motin g_raid_change_disk_state(olddisk, G_RAID_DISK_S_STALE_FAILED); 52489b17223SAlexander Motin } 52589b17223SAlexander Motin 52689b17223SAlexander Motin /* Welcome the new disk. */ 52789b17223SAlexander Motin if (resurrection) 52889b17223SAlexander Motin g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE); 52989b17223SAlexander Motin else// if (pd->pd_meta->disk_status == NVIDIA_S_CURRENT || 53089b17223SAlexander Motin //pd->pd_meta->disk_status == NVIDIA_S_REBUILD) 53189b17223SAlexander Motin g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE); 53289b17223SAlexander Motin // else 53389b17223SAlexander Motin // g_raid_change_disk_state(disk, G_RAID_DISK_S_FAILED); 53489b17223SAlexander Motin TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) { 53589b17223SAlexander Motin 53689b17223SAlexander Motin /* 53789b17223SAlexander Motin * Different disks may have different sizes, 53889b17223SAlexander Motin * in concat mode. Update from real disk size. 53989b17223SAlexander Motin */ 54089b17223SAlexander Motin if (meta->type == NVIDIA_T_CONCAT) 54189b17223SAlexander Motin sd->sd_size = pd->pd_disk_size - 0x800 * 512; 54289b17223SAlexander Motin 54389b17223SAlexander Motin if (resurrection) { 54489b17223SAlexander Motin /* New or ex-spare disk. */ 54589b17223SAlexander Motin g_raid_change_subdisk_state(sd, 54689b17223SAlexander Motin G_RAID_SUBDISK_S_NEW); 54789b17223SAlexander Motin } else if (meta->state == NVIDIA_S_REBUILD && 54889b17223SAlexander Motin (pd->pd_meta->disk_status & 0x100)) { 54989b17223SAlexander Motin /* Rebuilding disk. */ 55089b17223SAlexander Motin g_raid_change_subdisk_state(sd, 55189b17223SAlexander Motin G_RAID_SUBDISK_S_REBUILD); 55289b17223SAlexander Motin sd->sd_rebuild_pos = (off_t)pd->pd_meta->rebuild_lba / 55389b17223SAlexander Motin meta->array_width * pd->pd_meta->sector_size; 55489b17223SAlexander Motin } else if (meta->state == NVIDIA_S_SYNC) { 55589b17223SAlexander Motin /* Resyncing/dirty disk. */ 55689b17223SAlexander Motin g_raid_change_subdisk_state(sd, 55789b17223SAlexander Motin G_RAID_SUBDISK_S_RESYNC); 55889b17223SAlexander Motin sd->sd_rebuild_pos = (off_t)pd->pd_meta->rebuild_lba / 55989b17223SAlexander Motin meta->array_width * pd->pd_meta->sector_size; 56089b17223SAlexander Motin } else { 56189b17223SAlexander Motin /* Up to date disk. */ 56289b17223SAlexander Motin g_raid_change_subdisk_state(sd, 56389b17223SAlexander Motin G_RAID_SUBDISK_S_ACTIVE); 56489b17223SAlexander Motin } 56589b17223SAlexander Motin g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW, 56689b17223SAlexander Motin G_RAID_EVENT_SUBDISK); 56789b17223SAlexander Motin } 56889b17223SAlexander Motin 56989b17223SAlexander Motin /* Update status of our need for spare. */ 57089b17223SAlexander Motin if (mdi->mdio_started) { 57189b17223SAlexander Motin mdi->mdio_incomplete = 57289b17223SAlexander Motin (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) < 57389b17223SAlexander Motin mdi->mdio_total_disks); 57489b17223SAlexander Motin } 57589b17223SAlexander Motin 57689b17223SAlexander Motin return (resurrection); 57789b17223SAlexander Motin } 57889b17223SAlexander Motin 57989b17223SAlexander Motin static void 58089b17223SAlexander Motin g_disk_md_nvidia_retaste(void *arg, int pending) 58189b17223SAlexander Motin { 58289b17223SAlexander Motin 58389b17223SAlexander Motin G_RAID_DEBUG(1, "Array is not complete, trying to retaste."); 58489b17223SAlexander Motin g_retaste(&g_raid_class); 58589b17223SAlexander Motin free(arg, M_MD_NVIDIA); 58689b17223SAlexander Motin } 58789b17223SAlexander Motin 58889b17223SAlexander Motin static void 58989b17223SAlexander Motin g_raid_md_nvidia_refill(struct g_raid_softc *sc) 59089b17223SAlexander Motin { 59189b17223SAlexander Motin struct g_raid_md_object *md; 59289b17223SAlexander Motin struct g_raid_md_nvidia_object *mdi; 59389b17223SAlexander Motin struct g_raid_disk *disk; 59489b17223SAlexander Motin struct task *task; 59589b17223SAlexander Motin int update, na; 59689b17223SAlexander Motin 59789b17223SAlexander Motin md = sc->sc_md; 59889b17223SAlexander Motin mdi = (struct g_raid_md_nvidia_object *)md; 59989b17223SAlexander Motin update = 0; 60089b17223SAlexander Motin do { 60189b17223SAlexander Motin /* Make sure we miss anything. */ 60289b17223SAlexander Motin na = g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE); 60389b17223SAlexander Motin if (na == mdi->mdio_total_disks) 60489b17223SAlexander Motin break; 60589b17223SAlexander Motin 60689b17223SAlexander Motin G_RAID_DEBUG1(1, md->mdo_softc, 60789b17223SAlexander Motin "Array is not complete (%d of %d), " 60889b17223SAlexander Motin "trying to refill.", na, mdi->mdio_total_disks); 60989b17223SAlexander Motin 61089b17223SAlexander Motin /* Try to get use some of STALE disks. */ 61189b17223SAlexander Motin TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 61289b17223SAlexander Motin if (disk->d_state == G_RAID_DISK_S_STALE) { 61389b17223SAlexander Motin update += g_raid_md_nvidia_start_disk(disk); 61489b17223SAlexander Motin if (disk->d_state == G_RAID_DISK_S_ACTIVE) 61589b17223SAlexander Motin break; 61689b17223SAlexander Motin } 61789b17223SAlexander Motin } 61889b17223SAlexander Motin if (disk != NULL) 61989b17223SAlexander Motin continue; 62089b17223SAlexander Motin 62189b17223SAlexander Motin /* Try to get use some of SPARE disks. */ 62289b17223SAlexander Motin TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 62389b17223SAlexander Motin if (disk->d_state == G_RAID_DISK_S_SPARE) { 62489b17223SAlexander Motin update += g_raid_md_nvidia_start_disk(disk); 62589b17223SAlexander Motin if (disk->d_state == G_RAID_DISK_S_ACTIVE) 62689b17223SAlexander Motin break; 62789b17223SAlexander Motin } 62889b17223SAlexander Motin } 62989b17223SAlexander Motin } while (disk != NULL); 63089b17223SAlexander Motin 63189b17223SAlexander Motin /* Write new metadata if we changed something. */ 63214e2cd0aSAlexander Motin if (update) 63389b17223SAlexander Motin g_raid_md_write_nvidia(md, NULL, NULL, NULL); 63489b17223SAlexander Motin 63589b17223SAlexander Motin /* Update status of our need for spare. */ 63689b17223SAlexander Motin mdi->mdio_incomplete = (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) < 63789b17223SAlexander Motin mdi->mdio_total_disks); 63889b17223SAlexander Motin 63989b17223SAlexander Motin /* Request retaste hoping to find spare. */ 64089b17223SAlexander Motin if (mdi->mdio_incomplete) { 64189b17223SAlexander Motin task = malloc(sizeof(struct task), 64289b17223SAlexander Motin M_MD_NVIDIA, M_WAITOK | M_ZERO); 64389b17223SAlexander Motin TASK_INIT(task, 0, g_disk_md_nvidia_retaste, task); 64489b17223SAlexander Motin taskqueue_enqueue(taskqueue_swi, task); 64589b17223SAlexander Motin } 64689b17223SAlexander Motin } 64789b17223SAlexander Motin 64889b17223SAlexander Motin static void 64989b17223SAlexander Motin g_raid_md_nvidia_start(struct g_raid_softc *sc) 65089b17223SAlexander Motin { 65189b17223SAlexander Motin struct g_raid_md_object *md; 65289b17223SAlexander Motin struct g_raid_md_nvidia_object *mdi; 65389b17223SAlexander Motin struct g_raid_md_nvidia_perdisk *pd; 65489b17223SAlexander Motin struct nvidia_raid_conf *meta; 65589b17223SAlexander Motin struct g_raid_volume *vol; 65689b17223SAlexander Motin struct g_raid_subdisk *sd; 65789b17223SAlexander Motin struct g_raid_disk *disk; 65889b17223SAlexander Motin off_t size; 65989b17223SAlexander Motin int j, disk_pos; 66089b17223SAlexander Motin char buf[17]; 66189b17223SAlexander Motin 66289b17223SAlexander Motin md = sc->sc_md; 66389b17223SAlexander Motin mdi = (struct g_raid_md_nvidia_object *)md; 66489b17223SAlexander Motin meta = mdi->mdio_meta; 66589b17223SAlexander Motin 66689b17223SAlexander Motin /* Create volumes and subdisks. */ 66789b17223SAlexander Motin nvidia_meta_get_name(meta, buf); 66889b17223SAlexander Motin vol = g_raid_create_volume(sc, buf, -1); 66989b17223SAlexander Motin vol->v_mediasize = (off_t)meta->total_sectors * 512; 67089b17223SAlexander Motin vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE; 67189b17223SAlexander Motin if (meta->type == NVIDIA_T_RAID0) { 67289b17223SAlexander Motin vol->v_raid_level = G_RAID_VOLUME_RL_RAID0; 67389b17223SAlexander Motin size = vol->v_mediasize / mdi->mdio_total_disks; 67489b17223SAlexander Motin } else if (meta->type == NVIDIA_T_RAID1) { 67589b17223SAlexander Motin vol->v_raid_level = G_RAID_VOLUME_RL_RAID1; 67689b17223SAlexander Motin size = vol->v_mediasize; 67789b17223SAlexander Motin } else if (meta->type == NVIDIA_T_RAID01) { 67889b17223SAlexander Motin vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E; 67989b17223SAlexander Motin size = vol->v_mediasize / (mdi->mdio_total_disks / 2); 68089b17223SAlexander Motin } else if (meta->type == NVIDIA_T_CONCAT) { 68189b17223SAlexander Motin if (mdi->mdio_total_disks == 1) 68289b17223SAlexander Motin vol->v_raid_level = G_RAID_VOLUME_RL_SINGLE; 68389b17223SAlexander Motin else 68489b17223SAlexander Motin vol->v_raid_level = G_RAID_VOLUME_RL_CONCAT; 68589b17223SAlexander Motin size = 0; 68689b17223SAlexander Motin } else if (meta->type == NVIDIA_T_RAID5) { 68789b17223SAlexander Motin vol->v_raid_level = G_RAID_VOLUME_RL_RAID5; 688fc1de960SAlexander Motin vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LA; 68989b17223SAlexander Motin size = vol->v_mediasize / (mdi->mdio_total_disks - 1); 69089b17223SAlexander Motin } else if (meta->type == NVIDIA_T_RAID5_SYM) { 69189b17223SAlexander Motin vol->v_raid_level = G_RAID_VOLUME_RL_RAID5; 692fc1de960SAlexander Motin vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LS; 69389b17223SAlexander Motin size = vol->v_mediasize / (mdi->mdio_total_disks - 1); 69489b17223SAlexander Motin } else { 69589b17223SAlexander Motin vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN; 69689b17223SAlexander Motin size = 0; 69789b17223SAlexander Motin } 69889b17223SAlexander Motin vol->v_strip_size = meta->strip_sectors * 512; //ZZZ 69989b17223SAlexander Motin vol->v_disks_count = mdi->mdio_total_disks; 70089b17223SAlexander Motin vol->v_sectorsize = 512; //ZZZ 70189b17223SAlexander Motin for (j = 0; j < vol->v_disks_count; j++) { 70289b17223SAlexander Motin sd = &vol->v_subdisks[j]; 70389b17223SAlexander Motin sd->sd_offset = 0; 70489b17223SAlexander Motin sd->sd_size = size; 70589b17223SAlexander Motin } 70689b17223SAlexander Motin g_raid_start_volume(vol); 70789b17223SAlexander Motin 70889b17223SAlexander Motin /* Create disk placeholders to store data for later writing. */ 70989b17223SAlexander Motin for (disk_pos = 0; disk_pos < mdi->mdio_total_disks; disk_pos++) { 71089b17223SAlexander Motin pd = malloc(sizeof(*pd), M_MD_NVIDIA, M_WAITOK | M_ZERO); 71189b17223SAlexander Motin pd->pd_disk_pos = disk_pos; 71289b17223SAlexander Motin disk = g_raid_create_disk(sc); 71389b17223SAlexander Motin disk->d_md_data = (void *)pd; 71489b17223SAlexander Motin disk->d_state = G_RAID_DISK_S_OFFLINE; 71589b17223SAlexander Motin sd = &vol->v_subdisks[disk_pos]; 71689b17223SAlexander Motin sd->sd_disk = disk; 71789b17223SAlexander Motin TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next); 71889b17223SAlexander Motin } 71989b17223SAlexander Motin 72089b17223SAlexander Motin /* Make all disks found till the moment take their places. */ 72189b17223SAlexander Motin do { 72289b17223SAlexander Motin TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 72389b17223SAlexander Motin if (disk->d_state == G_RAID_DISK_S_NONE) { 72489b17223SAlexander Motin g_raid_md_nvidia_start_disk(disk); 72589b17223SAlexander Motin break; 72689b17223SAlexander Motin } 72789b17223SAlexander Motin } 72889b17223SAlexander Motin } while (disk != NULL); 72989b17223SAlexander Motin 73089b17223SAlexander Motin mdi->mdio_started = 1; 73189b17223SAlexander Motin G_RAID_DEBUG1(0, sc, "Array started."); 73289b17223SAlexander Motin g_raid_md_write_nvidia(md, NULL, NULL, NULL); 73389b17223SAlexander Motin 73489b17223SAlexander Motin /* Pickup any STALE/SPARE disks to refill array if needed. */ 73589b17223SAlexander Motin g_raid_md_nvidia_refill(sc); 73689b17223SAlexander Motin 73789b17223SAlexander Motin g_raid_event_send(vol, G_RAID_VOLUME_E_START, G_RAID_EVENT_VOLUME); 73889b17223SAlexander Motin 73989b17223SAlexander Motin callout_stop(&mdi->mdio_start_co); 74089b17223SAlexander Motin G_RAID_DEBUG1(1, sc, "root_mount_rel %p", mdi->mdio_rootmount); 74189b17223SAlexander Motin root_mount_rel(mdi->mdio_rootmount); 74289b17223SAlexander Motin mdi->mdio_rootmount = NULL; 74389b17223SAlexander Motin } 74489b17223SAlexander Motin 74589b17223SAlexander Motin static void 74689b17223SAlexander Motin g_raid_md_nvidia_new_disk(struct g_raid_disk *disk) 74789b17223SAlexander Motin { 74889b17223SAlexander Motin struct g_raid_softc *sc; 74989b17223SAlexander Motin struct g_raid_md_object *md; 75089b17223SAlexander Motin struct g_raid_md_nvidia_object *mdi; 75189b17223SAlexander Motin struct nvidia_raid_conf *pdmeta; 75289b17223SAlexander Motin struct g_raid_md_nvidia_perdisk *pd; 75389b17223SAlexander Motin 75489b17223SAlexander Motin sc = disk->d_softc; 75589b17223SAlexander Motin md = sc->sc_md; 75689b17223SAlexander Motin mdi = (struct g_raid_md_nvidia_object *)md; 75789b17223SAlexander Motin pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data; 75889b17223SAlexander Motin pdmeta = pd->pd_meta; 75989b17223SAlexander Motin 76089b17223SAlexander Motin if (mdi->mdio_started) { 76189b17223SAlexander Motin if (g_raid_md_nvidia_start_disk(disk)) 76289b17223SAlexander Motin g_raid_md_write_nvidia(md, NULL, NULL, NULL); 76389b17223SAlexander Motin } else { 76489b17223SAlexander Motin if (mdi->mdio_meta == NULL || 76589b17223SAlexander Motin mdi->mdio_meta->disk_number >= mdi->mdio_meta->total_disks) { 76689b17223SAlexander Motin G_RAID_DEBUG1(1, sc, "Newer disk"); 76789b17223SAlexander Motin if (mdi->mdio_meta != NULL) 76889b17223SAlexander Motin free(mdi->mdio_meta, M_MD_NVIDIA); 76989b17223SAlexander Motin mdi->mdio_meta = nvidia_meta_copy(pdmeta); 77089b17223SAlexander Motin mdi->mdio_total_disks = pdmeta->total_disks; 77189b17223SAlexander Motin mdi->mdio_disks_present = 1; 77289b17223SAlexander Motin } else if (pdmeta->disk_number < mdi->mdio_meta->total_disks) { 77389b17223SAlexander Motin mdi->mdio_disks_present++; 77489b17223SAlexander Motin G_RAID_DEBUG1(1, sc, "Matching disk (%d of %d up)", 77589b17223SAlexander Motin mdi->mdio_disks_present, 77689b17223SAlexander Motin mdi->mdio_total_disks); 77789b17223SAlexander Motin } else 77889b17223SAlexander Motin G_RAID_DEBUG1(1, sc, "Spare disk"); 77989b17223SAlexander Motin 78089b17223SAlexander Motin /* If we collected all needed disks - start array. */ 78189b17223SAlexander Motin if (mdi->mdio_disks_present == mdi->mdio_total_disks) 78289b17223SAlexander Motin g_raid_md_nvidia_start(sc); 78389b17223SAlexander Motin } 78489b17223SAlexander Motin } 78589b17223SAlexander Motin 78689b17223SAlexander Motin static void 78789b17223SAlexander Motin g_raid_nvidia_go(void *arg) 78889b17223SAlexander Motin { 78989b17223SAlexander Motin struct g_raid_softc *sc; 79089b17223SAlexander Motin struct g_raid_md_object *md; 79189b17223SAlexander Motin struct g_raid_md_nvidia_object *mdi; 79289b17223SAlexander Motin 79389b17223SAlexander Motin sc = arg; 79489b17223SAlexander Motin md = sc->sc_md; 79589b17223SAlexander Motin mdi = (struct g_raid_md_nvidia_object *)md; 79689b17223SAlexander Motin if (!mdi->mdio_started) { 79789b17223SAlexander Motin G_RAID_DEBUG1(0, sc, "Force array start due to timeout."); 79889b17223SAlexander Motin g_raid_event_send(sc, G_RAID_NODE_E_START, 0); 79989b17223SAlexander Motin } 80089b17223SAlexander Motin } 80189b17223SAlexander Motin 80289b17223SAlexander Motin static int 80389b17223SAlexander Motin g_raid_md_create_nvidia(struct g_raid_md_object *md, struct g_class *mp, 80489b17223SAlexander Motin struct g_geom **gp) 80589b17223SAlexander Motin { 80689b17223SAlexander Motin struct g_raid_softc *sc; 80789b17223SAlexander Motin struct g_raid_md_nvidia_object *mdi; 80889b17223SAlexander Motin char name[32]; 80989b17223SAlexander Motin 81089b17223SAlexander Motin mdi = (struct g_raid_md_nvidia_object *)md; 81189b17223SAlexander Motin arc4rand(&mdi->mdio_volume_id, 16, 0); 81289b17223SAlexander Motin snprintf(name, sizeof(name), "NVIDIA-%d", 81389b17223SAlexander Motin atomic_fetchadd_int(&NVIDIANodeID, 1)); 81489b17223SAlexander Motin sc = g_raid_create_node(mp, name, md); 81589b17223SAlexander Motin if (sc == NULL) 81689b17223SAlexander Motin return (G_RAID_MD_TASTE_FAIL); 81789b17223SAlexander Motin md->mdo_softc = sc; 81889b17223SAlexander Motin *gp = sc->sc_geom; 81989b17223SAlexander Motin return (G_RAID_MD_TASTE_NEW); 82089b17223SAlexander Motin } 82189b17223SAlexander Motin 82289b17223SAlexander Motin static int 82389b17223SAlexander Motin g_raid_md_taste_nvidia(struct g_raid_md_object *md, struct g_class *mp, 82489b17223SAlexander Motin struct g_consumer *cp, struct g_geom **gp) 82589b17223SAlexander Motin { 82689b17223SAlexander Motin struct g_consumer *rcp; 82789b17223SAlexander Motin struct g_provider *pp; 82889b17223SAlexander Motin struct g_raid_md_nvidia_object *mdi, *mdi1; 82989b17223SAlexander Motin struct g_raid_softc *sc; 83089b17223SAlexander Motin struct g_raid_disk *disk; 83189b17223SAlexander Motin struct nvidia_raid_conf *meta; 83289b17223SAlexander Motin struct g_raid_md_nvidia_perdisk *pd; 83389b17223SAlexander Motin struct g_geom *geom; 834609a7474SAlexander Motin int result, spare, len; 83589b17223SAlexander Motin char name[32]; 83689b17223SAlexander Motin uint16_t vendor; 83789b17223SAlexander Motin 83889b17223SAlexander Motin G_RAID_DEBUG(1, "Tasting NVIDIA on %s", cp->provider->name); 83989b17223SAlexander Motin mdi = (struct g_raid_md_nvidia_object *)md; 84089b17223SAlexander Motin pp = cp->provider; 84189b17223SAlexander Motin 84289b17223SAlexander Motin /* Read metadata from device. */ 84389b17223SAlexander Motin meta = NULL; 84489b17223SAlexander Motin g_topology_unlock(); 8450b1b7c2cSAlexander Motin vendor = 0xffff; 8460b1b7c2cSAlexander Motin len = sizeof(vendor); 84789b17223SAlexander Motin if (pp->geom->rank == 1) 84889b17223SAlexander Motin g_io_getattr("GEOM::hba_vendor", cp, &len, &vendor); 84989b17223SAlexander Motin meta = nvidia_meta_read(cp); 85089b17223SAlexander Motin g_topology_lock(); 85189b17223SAlexander Motin if (meta == NULL) { 85289b17223SAlexander Motin if (g_raid_aggressive_spare) { 85389b17223SAlexander Motin if (vendor == 0x10de) { 85489b17223SAlexander Motin G_RAID_DEBUG(1, 85589b17223SAlexander Motin "No NVIDIA metadata, forcing spare."); 85689b17223SAlexander Motin spare = 2; 85789b17223SAlexander Motin goto search; 85889b17223SAlexander Motin } else { 85989b17223SAlexander Motin G_RAID_DEBUG(1, 86089b17223SAlexander Motin "NVIDIA vendor mismatch 0x%04x != 0x10de", 86189b17223SAlexander Motin vendor); 86289b17223SAlexander Motin } 86389b17223SAlexander Motin } 86489b17223SAlexander Motin return (G_RAID_MD_TASTE_FAIL); 86589b17223SAlexander Motin } 86689b17223SAlexander Motin 86789b17223SAlexander Motin /* Metadata valid. Print it. */ 86889b17223SAlexander Motin g_raid_md_nvidia_print(meta); 86914e2cd0aSAlexander Motin G_RAID_DEBUG(1, "NVIDIA disk position %d", meta->disk_number); 87089b17223SAlexander Motin spare = 0;//(meta->type == NVIDIA_T_SPARE) ? 1 : 0; 87189b17223SAlexander Motin 87289b17223SAlexander Motin search: 87389b17223SAlexander Motin /* Search for matching node. */ 87489b17223SAlexander Motin sc = NULL; 87589b17223SAlexander Motin mdi1 = NULL; 87689b17223SAlexander Motin LIST_FOREACH(geom, &mp->geom, geom) { 87789b17223SAlexander Motin sc = geom->softc; 87889b17223SAlexander Motin if (sc == NULL) 87989b17223SAlexander Motin continue; 88089b17223SAlexander Motin if (sc->sc_stopping != 0) 88189b17223SAlexander Motin continue; 88289b17223SAlexander Motin if (sc->sc_md->mdo_class != md->mdo_class) 88389b17223SAlexander Motin continue; 88489b17223SAlexander Motin mdi1 = (struct g_raid_md_nvidia_object *)sc->sc_md; 88589b17223SAlexander Motin if (spare) { 88689b17223SAlexander Motin if (mdi1->mdio_incomplete) 88789b17223SAlexander Motin break; 88889b17223SAlexander Motin } else { 88989b17223SAlexander Motin if (memcmp(&mdi1->mdio_volume_id, 89089b17223SAlexander Motin &meta->volume_id, 16) == 0) 89189b17223SAlexander Motin break; 89289b17223SAlexander Motin } 89389b17223SAlexander Motin } 89489b17223SAlexander Motin 89589b17223SAlexander Motin /* Found matching node. */ 89689b17223SAlexander Motin if (geom != NULL) { 89789b17223SAlexander Motin G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name); 89889b17223SAlexander Motin result = G_RAID_MD_TASTE_EXISTING; 89989b17223SAlexander Motin 90089b17223SAlexander Motin } else if (spare) { /* Not found needy node -- left for later. */ 90189b17223SAlexander Motin G_RAID_DEBUG(1, "Spare is not needed at this time"); 90289b17223SAlexander Motin goto fail1; 90389b17223SAlexander Motin 90489b17223SAlexander Motin } else { /* Not found matching node -- create one. */ 90589b17223SAlexander Motin result = G_RAID_MD_TASTE_NEW; 90689b17223SAlexander Motin memcpy(&mdi->mdio_volume_id, &meta->volume_id, 16); 90789b17223SAlexander Motin snprintf(name, sizeof(name), "NVIDIA-%d", 90889b17223SAlexander Motin atomic_fetchadd_int(&NVIDIANodeID, 1)); 90989b17223SAlexander Motin sc = g_raid_create_node(mp, name, md); 91089b17223SAlexander Motin md->mdo_softc = sc; 91189b17223SAlexander Motin geom = sc->sc_geom; 91289b17223SAlexander Motin callout_init(&mdi->mdio_start_co, 1); 91389b17223SAlexander Motin callout_reset(&mdi->mdio_start_co, g_raid_start_timeout * hz, 91489b17223SAlexander Motin g_raid_nvidia_go, sc); 91589b17223SAlexander Motin mdi->mdio_rootmount = root_mount_hold("GRAID-NVIDIA"); 91689b17223SAlexander Motin G_RAID_DEBUG1(1, sc, "root_mount_hold %p", mdi->mdio_rootmount); 91789b17223SAlexander Motin } 91889b17223SAlexander Motin 919dea1e226SAlexander Motin /* There is no return after this point, so we close passed consumer. */ 920dea1e226SAlexander Motin g_access(cp, -1, 0, 0); 921dea1e226SAlexander Motin 92289b17223SAlexander Motin rcp = g_new_consumer(geom); 92340ea77a0SAlexander Motin rcp->flags |= G_CF_DIRECT_RECEIVE; 92489b17223SAlexander Motin g_attach(rcp, pp); 92589b17223SAlexander Motin if (g_access(rcp, 1, 1, 1) != 0) 92689b17223SAlexander Motin ; //goto fail1; 92789b17223SAlexander Motin 92889b17223SAlexander Motin g_topology_unlock(); 92989b17223SAlexander Motin sx_xlock(&sc->sc_lock); 93089b17223SAlexander Motin 93189b17223SAlexander Motin pd = malloc(sizeof(*pd), M_MD_NVIDIA, M_WAITOK | M_ZERO); 93289b17223SAlexander Motin pd->pd_meta = meta; 93389b17223SAlexander Motin if (spare == 2) { 93489b17223SAlexander Motin pd->pd_disk_pos = -3; 93589b17223SAlexander Motin } else { 93689b17223SAlexander Motin pd->pd_disk_pos = -1; 93789b17223SAlexander Motin } 93889b17223SAlexander Motin pd->pd_disk_size = pp->mediasize; 93989b17223SAlexander Motin disk = g_raid_create_disk(sc); 94089b17223SAlexander Motin disk->d_md_data = (void *)pd; 94189b17223SAlexander Motin disk->d_consumer = rcp; 94289b17223SAlexander Motin rcp->private = disk; 94389b17223SAlexander Motin 944609a7474SAlexander Motin g_raid_get_disk_info(disk); 94589b17223SAlexander Motin 94689b17223SAlexander Motin g_raid_md_nvidia_new_disk(disk); 94789b17223SAlexander Motin 94889b17223SAlexander Motin sx_xunlock(&sc->sc_lock); 94989b17223SAlexander Motin g_topology_lock(); 95089b17223SAlexander Motin *gp = geom; 95189b17223SAlexander Motin return (result); 95289b17223SAlexander Motin fail1: 95389b17223SAlexander Motin free(meta, M_MD_NVIDIA); 95489b17223SAlexander Motin return (G_RAID_MD_TASTE_FAIL); 95589b17223SAlexander Motin } 95689b17223SAlexander Motin 95789b17223SAlexander Motin static int 95889b17223SAlexander Motin g_raid_md_event_nvidia(struct g_raid_md_object *md, 95989b17223SAlexander Motin struct g_raid_disk *disk, u_int event) 96089b17223SAlexander Motin { 96189b17223SAlexander Motin struct g_raid_softc *sc; 96289b17223SAlexander Motin struct g_raid_subdisk *sd; 96389b17223SAlexander Motin struct g_raid_md_nvidia_object *mdi; 96489b17223SAlexander Motin struct g_raid_md_nvidia_perdisk *pd; 96589b17223SAlexander Motin 96689b17223SAlexander Motin sc = md->mdo_softc; 96789b17223SAlexander Motin mdi = (struct g_raid_md_nvidia_object *)md; 96889b17223SAlexander Motin if (disk == NULL) { 96989b17223SAlexander Motin switch (event) { 97089b17223SAlexander Motin case G_RAID_NODE_E_START: 97189b17223SAlexander Motin if (!mdi->mdio_started) { 97289b17223SAlexander Motin /* Bump volume ID to drop missing disks. */ 97389b17223SAlexander Motin arc4rand(&mdi->mdio_volume_id, 16, 0); 97489b17223SAlexander Motin g_raid_md_nvidia_start(sc); 97589b17223SAlexander Motin } 97689b17223SAlexander Motin return (0); 97789b17223SAlexander Motin } 97889b17223SAlexander Motin return (-1); 97989b17223SAlexander Motin } 98089b17223SAlexander Motin pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data; 98189b17223SAlexander Motin switch (event) { 98289b17223SAlexander Motin case G_RAID_DISK_E_DISCONNECTED: 98389b17223SAlexander Motin /* If disk was assigned, just update statuses. */ 98489b17223SAlexander Motin if (pd->pd_disk_pos >= 0) { 98589b17223SAlexander Motin g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE); 98689b17223SAlexander Motin if (disk->d_consumer) { 98789b17223SAlexander Motin g_raid_kill_consumer(sc, disk->d_consumer); 98889b17223SAlexander Motin disk->d_consumer = NULL; 98989b17223SAlexander Motin } 99089b17223SAlexander Motin TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) { 99189b17223SAlexander Motin g_raid_change_subdisk_state(sd, 99289b17223SAlexander Motin G_RAID_SUBDISK_S_NONE); 99389b17223SAlexander Motin g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED, 99489b17223SAlexander Motin G_RAID_EVENT_SUBDISK); 99589b17223SAlexander Motin } 99689b17223SAlexander Motin } else { 99789b17223SAlexander Motin /* Otherwise -- delete. */ 99889b17223SAlexander Motin g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE); 99989b17223SAlexander Motin g_raid_destroy_disk(disk); 100089b17223SAlexander Motin } 100189b17223SAlexander Motin 100289b17223SAlexander Motin if (mdi->mdio_started) { 100389b17223SAlexander Motin /* Bump volume ID to prevent disk resurrection. */ 100489b17223SAlexander Motin if (pd->pd_disk_pos >= 0) 100589b17223SAlexander Motin arc4rand(&mdi->mdio_volume_id, 16, 0); 100689b17223SAlexander Motin 100789b17223SAlexander Motin /* Write updated metadata to all disks. */ 100889b17223SAlexander Motin g_raid_md_write_nvidia(md, NULL, NULL, NULL); 100989b17223SAlexander Motin } 101089b17223SAlexander Motin 101189b17223SAlexander Motin /* Check if anything left except placeholders. */ 101289b17223SAlexander Motin if (g_raid_ndisks(sc, -1) == 101389b17223SAlexander Motin g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE)) 101489b17223SAlexander Motin g_raid_destroy_node(sc, 0); 101589b17223SAlexander Motin else 101689b17223SAlexander Motin g_raid_md_nvidia_refill(sc); 101789b17223SAlexander Motin return (0); 101889b17223SAlexander Motin } 101989b17223SAlexander Motin return (-2); 102089b17223SAlexander Motin } 102189b17223SAlexander Motin 102289b17223SAlexander Motin static int 102389b17223SAlexander Motin g_raid_md_ctl_nvidia(struct g_raid_md_object *md, 102489b17223SAlexander Motin struct gctl_req *req) 102589b17223SAlexander Motin { 102689b17223SAlexander Motin struct g_raid_softc *sc; 102789b17223SAlexander Motin struct g_raid_volume *vol; 102889b17223SAlexander Motin struct g_raid_subdisk *sd; 102989b17223SAlexander Motin struct g_raid_disk *disk; 103089b17223SAlexander Motin struct g_raid_md_nvidia_object *mdi; 103189b17223SAlexander Motin struct g_raid_md_nvidia_perdisk *pd; 103289b17223SAlexander Motin struct g_consumer *cp; 103389b17223SAlexander Motin struct g_provider *pp; 103489b17223SAlexander Motin char arg[16]; 103589b17223SAlexander Motin const char *verb, *volname, *levelname, *diskname; 103689b17223SAlexander Motin int *nargs, *force; 1037733a1f3fSAlexander Motin off_t size, sectorsize, strip, volsize; 103889b17223SAlexander Motin intmax_t *sizearg, *striparg; 103989b17223SAlexander Motin int numdisks, i, len, level, qual, update; 104089b17223SAlexander Motin int error; 104189b17223SAlexander Motin 104289b17223SAlexander Motin sc = md->mdo_softc; 104389b17223SAlexander Motin mdi = (struct g_raid_md_nvidia_object *)md; 104489b17223SAlexander Motin verb = gctl_get_param(req, "verb", NULL); 104589b17223SAlexander Motin nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 104689b17223SAlexander Motin error = 0; 104789b17223SAlexander Motin if (strcmp(verb, "label") == 0) { 104889b17223SAlexander Motin 104989b17223SAlexander Motin if (*nargs < 4) { 105089b17223SAlexander Motin gctl_error(req, "Invalid number of arguments."); 105189b17223SAlexander Motin return (-1); 105289b17223SAlexander Motin } 105389b17223SAlexander Motin volname = gctl_get_asciiparam(req, "arg1"); 105489b17223SAlexander Motin if (volname == NULL) { 105589b17223SAlexander Motin gctl_error(req, "No volume name."); 105689b17223SAlexander Motin return (-2); 105789b17223SAlexander Motin } 105889b17223SAlexander Motin levelname = gctl_get_asciiparam(req, "arg2"); 105989b17223SAlexander Motin if (levelname == NULL) { 106089b17223SAlexander Motin gctl_error(req, "No RAID level."); 106189b17223SAlexander Motin return (-3); 106289b17223SAlexander Motin } 1063fc1de960SAlexander Motin if (strcasecmp(levelname, "RAID5") == 0) 10647b2a8d78SAlexander Motin levelname = "RAID5-LS"; 106589b17223SAlexander Motin if (g_raid_volume_str2level(levelname, &level, &qual)) { 106689b17223SAlexander Motin gctl_error(req, "Unknown RAID level '%s'.", levelname); 106789b17223SAlexander Motin return (-4); 106889b17223SAlexander Motin } 106989b17223SAlexander Motin numdisks = *nargs - 3; 107089b17223SAlexander Motin force = gctl_get_paraml(req, "force", sizeof(*force)); 107189b17223SAlexander Motin if (!g_raid_md_nvidia_supported(level, qual, numdisks, 107289b17223SAlexander Motin force ? *force : 0)) { 107389b17223SAlexander Motin gctl_error(req, "Unsupported RAID level " 107489b17223SAlexander Motin "(0x%02x/0x%02x), or number of disks (%d).", 107589b17223SAlexander Motin level, qual, numdisks); 107689b17223SAlexander Motin return (-5); 107789b17223SAlexander Motin } 107889b17223SAlexander Motin 107989b17223SAlexander Motin /* Search for disks, connect them and probe. */ 108089b17223SAlexander Motin size = 0x7fffffffffffffffllu; 108189b17223SAlexander Motin sectorsize = 0; 108289b17223SAlexander Motin for (i = 0; i < numdisks; i++) { 108389b17223SAlexander Motin snprintf(arg, sizeof(arg), "arg%d", i + 3); 108489b17223SAlexander Motin diskname = gctl_get_asciiparam(req, arg); 108589b17223SAlexander Motin if (diskname == NULL) { 108689b17223SAlexander Motin gctl_error(req, "No disk name (%s).", arg); 108789b17223SAlexander Motin error = -6; 108889b17223SAlexander Motin break; 108989b17223SAlexander Motin } 109089b17223SAlexander Motin if (strcmp(diskname, "NONE") == 0) { 109189b17223SAlexander Motin cp = NULL; 109289b17223SAlexander Motin pp = NULL; 109389b17223SAlexander Motin } else { 109489b17223SAlexander Motin g_topology_lock(); 109589b17223SAlexander Motin cp = g_raid_open_consumer(sc, diskname); 109689b17223SAlexander Motin if (cp == NULL) { 109789b17223SAlexander Motin gctl_error(req, "Can't open '%s'.", 109889b17223SAlexander Motin diskname); 109989b17223SAlexander Motin g_topology_unlock(); 110089b17223SAlexander Motin error = -7; 110189b17223SAlexander Motin break; 110289b17223SAlexander Motin } 110389b17223SAlexander Motin pp = cp->provider; 110489b17223SAlexander Motin } 110589b17223SAlexander Motin pd = malloc(sizeof(*pd), M_MD_NVIDIA, M_WAITOK | M_ZERO); 110689b17223SAlexander Motin pd->pd_disk_pos = i; 110789b17223SAlexander Motin disk = g_raid_create_disk(sc); 110889b17223SAlexander Motin disk->d_md_data = (void *)pd; 110989b17223SAlexander Motin disk->d_consumer = cp; 111089b17223SAlexander Motin if (cp == NULL) 111189b17223SAlexander Motin continue; 111289b17223SAlexander Motin cp->private = disk; 111389b17223SAlexander Motin g_topology_unlock(); 111489b17223SAlexander Motin 1115609a7474SAlexander Motin g_raid_get_disk_info(disk); 111689b17223SAlexander Motin 111789b17223SAlexander Motin pd->pd_disk_size = pp->mediasize; 111889b17223SAlexander Motin if (size > pp->mediasize) 111989b17223SAlexander Motin size = pp->mediasize; 112089b17223SAlexander Motin if (sectorsize < pp->sectorsize) 112189b17223SAlexander Motin sectorsize = pp->sectorsize; 112289b17223SAlexander Motin } 112389b17223SAlexander Motin if (error != 0) 112489b17223SAlexander Motin return (error); 112589b17223SAlexander Motin 112614e2cd0aSAlexander Motin if (sectorsize <= 0) { 112714e2cd0aSAlexander Motin gctl_error(req, "Can't get sector size."); 112814e2cd0aSAlexander Motin return (-8); 112914e2cd0aSAlexander Motin } 113014e2cd0aSAlexander Motin 113189b17223SAlexander Motin /* Reserve space for metadata. */ 113289b17223SAlexander Motin size -= 2 * sectorsize; 113389b17223SAlexander Motin 113489b17223SAlexander Motin /* Handle size argument. */ 113589b17223SAlexander Motin len = sizeof(*sizearg); 113689b17223SAlexander Motin sizearg = gctl_get_param(req, "size", &len); 113789b17223SAlexander Motin if (sizearg != NULL && len == sizeof(*sizearg) && 113889b17223SAlexander Motin *sizearg > 0) { 113989b17223SAlexander Motin if (*sizearg > size) { 114089b17223SAlexander Motin gctl_error(req, "Size too big %lld > %lld.", 114189b17223SAlexander Motin (long long)*sizearg, (long long)size); 114289b17223SAlexander Motin return (-9); 114389b17223SAlexander Motin } 114489b17223SAlexander Motin size = *sizearg; 114589b17223SAlexander Motin } 114689b17223SAlexander Motin 114789b17223SAlexander Motin /* Handle strip argument. */ 114889b17223SAlexander Motin strip = 131072; 114989b17223SAlexander Motin len = sizeof(*striparg); 115089b17223SAlexander Motin striparg = gctl_get_param(req, "strip", &len); 115189b17223SAlexander Motin if (striparg != NULL && len == sizeof(*striparg) && 115289b17223SAlexander Motin *striparg > 0) { 115389b17223SAlexander Motin if (*striparg < sectorsize) { 115489b17223SAlexander Motin gctl_error(req, "Strip size too small."); 115589b17223SAlexander Motin return (-10); 115689b17223SAlexander Motin } 115789b17223SAlexander Motin if (*striparg % sectorsize != 0) { 115889b17223SAlexander Motin gctl_error(req, "Incorrect strip size."); 115989b17223SAlexander Motin return (-11); 116089b17223SAlexander Motin } 116189b17223SAlexander Motin if (strip > 65535 * sectorsize) { 116289b17223SAlexander Motin gctl_error(req, "Strip size too big."); 116389b17223SAlexander Motin return (-12); 116489b17223SAlexander Motin } 116589b17223SAlexander Motin strip = *striparg; 116689b17223SAlexander Motin } 116789b17223SAlexander Motin 116889b17223SAlexander Motin /* Round size down to strip or sector. */ 116989b17223SAlexander Motin if (level == G_RAID_VOLUME_RL_RAID1) 117089b17223SAlexander Motin size -= (size % sectorsize); 117189b17223SAlexander Motin else if (level == G_RAID_VOLUME_RL_RAID1E && 117289b17223SAlexander Motin (numdisks & 1) != 0) 117389b17223SAlexander Motin size -= (size % (2 * strip)); 117489b17223SAlexander Motin else 117589b17223SAlexander Motin size -= (size % strip); 117689b17223SAlexander Motin if (size <= 0) { 117789b17223SAlexander Motin gctl_error(req, "Size too small."); 117889b17223SAlexander Motin return (-13); 117989b17223SAlexander Motin } 1180733a1f3fSAlexander Motin 1181733a1f3fSAlexander Motin if (level == G_RAID_VOLUME_RL_RAID0 || 1182733a1f3fSAlexander Motin level == G_RAID_VOLUME_RL_CONCAT || 1183733a1f3fSAlexander Motin level == G_RAID_VOLUME_RL_SINGLE) 1184733a1f3fSAlexander Motin volsize = size * numdisks; 1185733a1f3fSAlexander Motin else if (level == G_RAID_VOLUME_RL_RAID1) 1186733a1f3fSAlexander Motin volsize = size; 1187733a1f3fSAlexander Motin else if (level == G_RAID_VOLUME_RL_RAID5) 1188733a1f3fSAlexander Motin volsize = size * (numdisks - 1); 1189733a1f3fSAlexander Motin else { /* RAID1E */ 1190733a1f3fSAlexander Motin volsize = ((size * numdisks) / strip / 2) * 1191733a1f3fSAlexander Motin strip; 1192733a1f3fSAlexander Motin } 1193733a1f3fSAlexander Motin if (volsize > 0xffffffffllu * sectorsize) { 119489b17223SAlexander Motin gctl_error(req, "Size too big."); 119589b17223SAlexander Motin return (-14); 119689b17223SAlexander Motin } 119789b17223SAlexander Motin 119889b17223SAlexander Motin /* We have all we need, create things: volume, ... */ 119989b17223SAlexander Motin mdi->mdio_total_disks = numdisks; 120089b17223SAlexander Motin mdi->mdio_started = 1; 120189b17223SAlexander Motin vol = g_raid_create_volume(sc, volname, -1); 120289b17223SAlexander Motin vol->v_md_data = (void *)(intptr_t)0; 120389b17223SAlexander Motin vol->v_raid_level = level; 1204fc1de960SAlexander Motin vol->v_raid_level_qualifier = qual; 120589b17223SAlexander Motin vol->v_strip_size = strip; 120689b17223SAlexander Motin vol->v_disks_count = numdisks; 1207733a1f3fSAlexander Motin vol->v_mediasize = volsize; 120889b17223SAlexander Motin vol->v_sectorsize = sectorsize; 120989b17223SAlexander Motin g_raid_start_volume(vol); 121089b17223SAlexander Motin 121189b17223SAlexander Motin /* , and subdisks. */ 121289b17223SAlexander Motin TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 121389b17223SAlexander Motin pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data; 121489b17223SAlexander Motin sd = &vol->v_subdisks[pd->pd_disk_pos]; 121589b17223SAlexander Motin sd->sd_disk = disk; 121689b17223SAlexander Motin sd->sd_offset = 0; 121789b17223SAlexander Motin sd->sd_size = size; 121889b17223SAlexander Motin TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next); 121989b17223SAlexander Motin if (sd->sd_disk->d_consumer != NULL) { 122089b17223SAlexander Motin g_raid_change_disk_state(disk, 122189b17223SAlexander Motin G_RAID_DISK_S_ACTIVE); 122289b17223SAlexander Motin g_raid_change_subdisk_state(sd, 122389b17223SAlexander Motin G_RAID_SUBDISK_S_ACTIVE); 122489b17223SAlexander Motin g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW, 122589b17223SAlexander Motin G_RAID_EVENT_SUBDISK); 122689b17223SAlexander Motin } else { 122789b17223SAlexander Motin g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE); 122889b17223SAlexander Motin } 122989b17223SAlexander Motin } 123089b17223SAlexander Motin 123189b17223SAlexander Motin /* Write metadata based on created entities. */ 123289b17223SAlexander Motin G_RAID_DEBUG1(0, sc, "Array started."); 123389b17223SAlexander Motin g_raid_md_write_nvidia(md, NULL, NULL, NULL); 123489b17223SAlexander Motin 123589b17223SAlexander Motin /* Pickup any STALE/SPARE disks to refill array if needed. */ 123689b17223SAlexander Motin g_raid_md_nvidia_refill(sc); 123789b17223SAlexander Motin 123889b17223SAlexander Motin g_raid_event_send(vol, G_RAID_VOLUME_E_START, 123989b17223SAlexander Motin G_RAID_EVENT_VOLUME); 124089b17223SAlexander Motin return (0); 124189b17223SAlexander Motin } 124289b17223SAlexander Motin if (strcmp(verb, "delete") == 0) { 124389b17223SAlexander Motin 124489b17223SAlexander Motin /* Check if some volume is still open. */ 124589b17223SAlexander Motin force = gctl_get_paraml(req, "force", sizeof(*force)); 124689b17223SAlexander Motin if (force != NULL && *force == 0 && 124789b17223SAlexander Motin g_raid_nopens(sc) != 0) { 124889b17223SAlexander Motin gctl_error(req, "Some volume is still open."); 124989b17223SAlexander Motin return (-4); 125089b17223SAlexander Motin } 125189b17223SAlexander Motin 125289b17223SAlexander Motin TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 125389b17223SAlexander Motin if (disk->d_consumer) 125489b17223SAlexander Motin nvidia_meta_erase(disk->d_consumer); 125589b17223SAlexander Motin } 125689b17223SAlexander Motin g_raid_destroy_node(sc, 0); 125789b17223SAlexander Motin return (0); 125889b17223SAlexander Motin } 125989b17223SAlexander Motin if (strcmp(verb, "remove") == 0 || 126089b17223SAlexander Motin strcmp(verb, "fail") == 0) { 126189b17223SAlexander Motin if (*nargs < 2) { 126289b17223SAlexander Motin gctl_error(req, "Invalid number of arguments."); 126389b17223SAlexander Motin return (-1); 126489b17223SAlexander Motin } 126589b17223SAlexander Motin for (i = 1; i < *nargs; i++) { 126689b17223SAlexander Motin snprintf(arg, sizeof(arg), "arg%d", i); 126789b17223SAlexander Motin diskname = gctl_get_asciiparam(req, arg); 126889b17223SAlexander Motin if (diskname == NULL) { 126989b17223SAlexander Motin gctl_error(req, "No disk name (%s).", arg); 127089b17223SAlexander Motin error = -2; 127189b17223SAlexander Motin break; 127289b17223SAlexander Motin } 127389b17223SAlexander Motin if (strncmp(diskname, "/dev/", 5) == 0) 127489b17223SAlexander Motin diskname += 5; 127589b17223SAlexander Motin 127689b17223SAlexander Motin TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 127789b17223SAlexander Motin if (disk->d_consumer != NULL && 127889b17223SAlexander Motin disk->d_consumer->provider != NULL && 127989b17223SAlexander Motin strcmp(disk->d_consumer->provider->name, 128089b17223SAlexander Motin diskname) == 0) 128189b17223SAlexander Motin break; 128289b17223SAlexander Motin } 128389b17223SAlexander Motin if (disk == NULL) { 128489b17223SAlexander Motin gctl_error(req, "Disk '%s' not found.", 128589b17223SAlexander Motin diskname); 128689b17223SAlexander Motin error = -3; 128789b17223SAlexander Motin break; 128889b17223SAlexander Motin } 128989b17223SAlexander Motin 129089b17223SAlexander Motin if (strcmp(verb, "fail") == 0) { 129189b17223SAlexander Motin g_raid_md_fail_disk_nvidia(md, NULL, disk); 129289b17223SAlexander Motin continue; 129389b17223SAlexander Motin } 129489b17223SAlexander Motin 129589b17223SAlexander Motin pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data; 129689b17223SAlexander Motin 129789b17223SAlexander Motin /* Erase metadata on deleting disk. */ 129889b17223SAlexander Motin nvidia_meta_erase(disk->d_consumer); 129989b17223SAlexander Motin 130089b17223SAlexander Motin /* If disk was assigned, just update statuses. */ 130189b17223SAlexander Motin if (pd->pd_disk_pos >= 0) { 130289b17223SAlexander Motin g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE); 130389b17223SAlexander Motin g_raid_kill_consumer(sc, disk->d_consumer); 130489b17223SAlexander Motin disk->d_consumer = NULL; 130589b17223SAlexander Motin TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) { 130689b17223SAlexander Motin g_raid_change_subdisk_state(sd, 130789b17223SAlexander Motin G_RAID_SUBDISK_S_NONE); 130889b17223SAlexander Motin g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED, 130989b17223SAlexander Motin G_RAID_EVENT_SUBDISK); 131089b17223SAlexander Motin } 131189b17223SAlexander Motin } else { 131289b17223SAlexander Motin /* Otherwise -- delete. */ 131389b17223SAlexander Motin g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE); 131489b17223SAlexander Motin g_raid_destroy_disk(disk); 131589b17223SAlexander Motin } 131689b17223SAlexander Motin } 131789b17223SAlexander Motin 131889b17223SAlexander Motin /* Write updated metadata to remaining disks. */ 131989b17223SAlexander Motin g_raid_md_write_nvidia(md, NULL, NULL, NULL); 132089b17223SAlexander Motin 132189b17223SAlexander Motin /* Check if anything left except placeholders. */ 132289b17223SAlexander Motin if (g_raid_ndisks(sc, -1) == 132389b17223SAlexander Motin g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE)) 132489b17223SAlexander Motin g_raid_destroy_node(sc, 0); 132589b17223SAlexander Motin else 132689b17223SAlexander Motin g_raid_md_nvidia_refill(sc); 132789b17223SAlexander Motin return (error); 132889b17223SAlexander Motin } 132989b17223SAlexander Motin if (strcmp(verb, "insert") == 0) { 133089b17223SAlexander Motin if (*nargs < 2) { 133189b17223SAlexander Motin gctl_error(req, "Invalid number of arguments."); 133289b17223SAlexander Motin return (-1); 133389b17223SAlexander Motin } 133489b17223SAlexander Motin update = 0; 133589b17223SAlexander Motin for (i = 1; i < *nargs; i++) { 133689b17223SAlexander Motin /* Get disk name. */ 133789b17223SAlexander Motin snprintf(arg, sizeof(arg), "arg%d", i); 133889b17223SAlexander Motin diskname = gctl_get_asciiparam(req, arg); 133989b17223SAlexander Motin if (diskname == NULL) { 134089b17223SAlexander Motin gctl_error(req, "No disk name (%s).", arg); 134189b17223SAlexander Motin error = -3; 134289b17223SAlexander Motin break; 134389b17223SAlexander Motin } 134489b17223SAlexander Motin 134589b17223SAlexander Motin /* Try to find provider with specified name. */ 134689b17223SAlexander Motin g_topology_lock(); 134789b17223SAlexander Motin cp = g_raid_open_consumer(sc, diskname); 134889b17223SAlexander Motin if (cp == NULL) { 134989b17223SAlexander Motin gctl_error(req, "Can't open disk '%s'.", 135089b17223SAlexander Motin diskname); 135189b17223SAlexander Motin g_topology_unlock(); 135289b17223SAlexander Motin error = -4; 135389b17223SAlexander Motin break; 135489b17223SAlexander Motin } 135589b17223SAlexander Motin pp = cp->provider; 135689b17223SAlexander Motin 135789b17223SAlexander Motin pd = malloc(sizeof(*pd), M_MD_NVIDIA, M_WAITOK | M_ZERO); 135889b17223SAlexander Motin pd->pd_disk_pos = -3; 135989b17223SAlexander Motin pd->pd_disk_size = pp->mediasize; 136089b17223SAlexander Motin 136189b17223SAlexander Motin disk = g_raid_create_disk(sc); 136289b17223SAlexander Motin disk->d_consumer = cp; 136389b17223SAlexander Motin disk->d_md_data = (void *)pd; 136489b17223SAlexander Motin cp->private = disk; 136589b17223SAlexander Motin g_topology_unlock(); 136689b17223SAlexander Motin 1367609a7474SAlexander Motin g_raid_get_disk_info(disk); 136889b17223SAlexander Motin 136989b17223SAlexander Motin /* Welcome the "new" disk. */ 137089b17223SAlexander Motin update += g_raid_md_nvidia_start_disk(disk); 137189b17223SAlexander Motin if (disk->d_state != G_RAID_DISK_S_SPARE && 137289b17223SAlexander Motin disk->d_state != G_RAID_DISK_S_ACTIVE) { 137389b17223SAlexander Motin gctl_error(req, "Disk '%s' doesn't fit.", 137489b17223SAlexander Motin diskname); 137589b17223SAlexander Motin g_raid_destroy_disk(disk); 137689b17223SAlexander Motin error = -8; 137789b17223SAlexander Motin break; 137889b17223SAlexander Motin } 137989b17223SAlexander Motin } 138089b17223SAlexander Motin 138189b17223SAlexander Motin /* Write new metadata if we changed something. */ 138289b17223SAlexander Motin if (update) 138389b17223SAlexander Motin g_raid_md_write_nvidia(md, NULL, NULL, NULL); 138489b17223SAlexander Motin return (error); 138589b17223SAlexander Motin } 138689b17223SAlexander Motin gctl_error(req, "Command '%s' is not supported.", verb); 138789b17223SAlexander Motin return (-100); 138889b17223SAlexander Motin } 138989b17223SAlexander Motin 139089b17223SAlexander Motin static int 139189b17223SAlexander Motin g_raid_md_write_nvidia(struct g_raid_md_object *md, struct g_raid_volume *tvol, 139289b17223SAlexander Motin struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk) 139389b17223SAlexander Motin { 139489b17223SAlexander Motin struct g_raid_softc *sc; 139589b17223SAlexander Motin struct g_raid_volume *vol; 139689b17223SAlexander Motin struct g_raid_subdisk *sd; 139789b17223SAlexander Motin struct g_raid_disk *disk; 139889b17223SAlexander Motin struct g_raid_md_nvidia_object *mdi; 139989b17223SAlexander Motin struct g_raid_md_nvidia_perdisk *pd; 140089b17223SAlexander Motin struct nvidia_raid_conf *meta; 140189b17223SAlexander Motin int i, spares; 140289b17223SAlexander Motin 140389b17223SAlexander Motin sc = md->mdo_softc; 140489b17223SAlexander Motin mdi = (struct g_raid_md_nvidia_object *)md; 140589b17223SAlexander Motin 140689b17223SAlexander Motin if (sc->sc_stopping == G_RAID_DESTROY_HARD) 140789b17223SAlexander Motin return (0); 140889b17223SAlexander Motin 140989b17223SAlexander Motin /* There is only one volume. */ 141089b17223SAlexander Motin vol = TAILQ_FIRST(&sc->sc_volumes); 141189b17223SAlexander Motin 141289b17223SAlexander Motin /* Fill global fields. */ 141389b17223SAlexander Motin meta = malloc(sizeof(*meta), M_MD_NVIDIA, M_WAITOK | M_ZERO); 141489b17223SAlexander Motin if (mdi->mdio_meta) 141589b17223SAlexander Motin memcpy(meta, mdi->mdio_meta, sizeof(*meta)); 141663607675SAlexander Motin memcpy(meta->nvidia_id, NVIDIA_MAGIC, sizeof(NVIDIA_MAGIC) - 1); 141789b17223SAlexander Motin meta->config_size = 30; 141889b17223SAlexander Motin meta->version = 0x0064; 141989b17223SAlexander Motin meta->total_sectors = vol->v_mediasize / vol->v_sectorsize; 142089b17223SAlexander Motin meta->sector_size = vol->v_sectorsize; 142189b17223SAlexander Motin nvidia_meta_put_name(meta, vol->v_name); 142289b17223SAlexander Motin meta->magic_0 = NVIDIA_MAGIC0; 142389b17223SAlexander Motin memcpy(&meta->volume_id, &mdi->mdio_volume_id, 16); 142489b17223SAlexander Motin meta->state = NVIDIA_S_IDLE; 142589b17223SAlexander Motin if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1) 142689b17223SAlexander Motin meta->array_width = 1; 142789b17223SAlexander Motin else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) 142889b17223SAlexander Motin meta->array_width = vol->v_disks_count / 2; 142989b17223SAlexander Motin else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID5) 143089b17223SAlexander Motin meta->array_width = vol->v_disks_count - 1; 143189b17223SAlexander Motin else 143289b17223SAlexander Motin meta->array_width = vol->v_disks_count; 143389b17223SAlexander Motin meta->total_disks = vol->v_disks_count; 143489b17223SAlexander Motin meta->orig_array_width = meta->array_width; 143589b17223SAlexander Motin if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0) 143689b17223SAlexander Motin meta->type = NVIDIA_T_RAID0; 143789b17223SAlexander Motin else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1) 143889b17223SAlexander Motin meta->type = NVIDIA_T_RAID1; 143989b17223SAlexander Motin else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) 144089b17223SAlexander Motin meta->type = NVIDIA_T_RAID01; 144189b17223SAlexander Motin else if (vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT || 144289b17223SAlexander Motin vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE) 144389b17223SAlexander Motin meta->type = NVIDIA_T_CONCAT; 1444fc1de960SAlexander Motin else if (vol->v_raid_level_qualifier == G_RAID_VOLUME_RLQ_R5LA) 1445fc1de960SAlexander Motin meta->type = NVIDIA_T_RAID5; 144689b17223SAlexander Motin else 144789b17223SAlexander Motin meta->type = NVIDIA_T_RAID5_SYM; 144889b17223SAlexander Motin meta->strip_sectors = vol->v_strip_size / vol->v_sectorsize; 144989b17223SAlexander Motin meta->strip_bytes = vol->v_strip_size; 145089b17223SAlexander Motin meta->strip_shift = ffs(meta->strip_sectors) - 1; 145189b17223SAlexander Motin meta->strip_mask = meta->strip_sectors - 1; 145289b17223SAlexander Motin meta->stripe_sectors = meta->strip_sectors * meta->orig_array_width; 145389b17223SAlexander Motin meta->stripe_bytes = meta->stripe_sectors * vol->v_sectorsize; 145489b17223SAlexander Motin meta->rebuild_lba = 0; 145589b17223SAlexander Motin meta->orig_type = meta->type; 145689b17223SAlexander Motin meta->orig_total_sectors = meta->total_sectors; 145789b17223SAlexander Motin meta->status = 0; 145889b17223SAlexander Motin 145989b17223SAlexander Motin for (i = 0; i < vol->v_disks_count; i++) { 146089b17223SAlexander Motin sd = &vol->v_subdisks[i]; 146189b17223SAlexander Motin if ((sd->sd_state == G_RAID_SUBDISK_S_STALE || 146289b17223SAlexander Motin sd->sd_state == G_RAID_SUBDISK_S_RESYNC || 146389b17223SAlexander Motin vol->v_dirty) && 146489b17223SAlexander Motin meta->state != NVIDIA_S_REBUILD) 146589b17223SAlexander Motin meta->state = NVIDIA_S_SYNC; 146689b17223SAlexander Motin else if (sd->sd_state == G_RAID_SUBDISK_S_NEW || 146789b17223SAlexander Motin sd->sd_state == G_RAID_SUBDISK_S_REBUILD) 146889b17223SAlexander Motin meta->state = NVIDIA_S_REBUILD; 146989b17223SAlexander Motin } 147089b17223SAlexander Motin 147189b17223SAlexander Motin /* We are done. Print meta data and store them to disks. */ 147289b17223SAlexander Motin if (mdi->mdio_meta != NULL) 147389b17223SAlexander Motin free(mdi->mdio_meta, M_MD_NVIDIA); 147489b17223SAlexander Motin mdi->mdio_meta = meta; 147589b17223SAlexander Motin spares = 0; 147689b17223SAlexander Motin TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 147789b17223SAlexander Motin pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data; 147889b17223SAlexander Motin if (disk->d_state != G_RAID_DISK_S_ACTIVE && 147989b17223SAlexander Motin disk->d_state != G_RAID_DISK_S_SPARE) 148089b17223SAlexander Motin continue; 148189b17223SAlexander Motin if (pd->pd_meta != NULL) { 148289b17223SAlexander Motin free(pd->pd_meta, M_MD_NVIDIA); 148389b17223SAlexander Motin pd->pd_meta = NULL; 148489b17223SAlexander Motin } 148589b17223SAlexander Motin pd->pd_meta = nvidia_meta_copy(meta); 148689b17223SAlexander Motin if ((sd = TAILQ_FIRST(&disk->d_subdisks)) != NULL) { 148789b17223SAlexander Motin /* For RAID0+1 we need to translate order. */ 148889b17223SAlexander Motin pd->pd_meta->disk_number = 148989b17223SAlexander Motin nvidia_meta_translate_disk(meta, sd->sd_pos); 149089b17223SAlexander Motin if (sd->sd_state != G_RAID_SUBDISK_S_ACTIVE) { 149189b17223SAlexander Motin pd->pd_meta->disk_status = 0x100; 149289b17223SAlexander Motin pd->pd_meta->rebuild_lba = 149389b17223SAlexander Motin sd->sd_rebuild_pos / vol->v_sectorsize * 149489b17223SAlexander Motin meta->array_width; 149589b17223SAlexander Motin } 149689b17223SAlexander Motin } else 149789b17223SAlexander Motin pd->pd_meta->disk_number = meta->total_disks + spares++; 149889b17223SAlexander Motin G_RAID_DEBUG(1, "Writing NVIDIA metadata to %s", 149989b17223SAlexander Motin g_raid_get_diskname(disk)); 150089b17223SAlexander Motin g_raid_md_nvidia_print(pd->pd_meta); 150189b17223SAlexander Motin nvidia_meta_write(disk->d_consumer, pd->pd_meta); 150289b17223SAlexander Motin } 150389b17223SAlexander Motin return (0); 150489b17223SAlexander Motin } 150589b17223SAlexander Motin 150689b17223SAlexander Motin static int 150789b17223SAlexander Motin g_raid_md_fail_disk_nvidia(struct g_raid_md_object *md, 150889b17223SAlexander Motin struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk) 150989b17223SAlexander Motin { 151089b17223SAlexander Motin struct g_raid_softc *sc; 151189b17223SAlexander Motin struct g_raid_md_nvidia_perdisk *pd; 151289b17223SAlexander Motin struct g_raid_subdisk *sd; 151389b17223SAlexander Motin 151489b17223SAlexander Motin sc = md->mdo_softc; 151589b17223SAlexander Motin pd = (struct g_raid_md_nvidia_perdisk *)tdisk->d_md_data; 151689b17223SAlexander Motin 151789b17223SAlexander Motin /* We can't fail disk that is not a part of array now. */ 151889b17223SAlexander Motin if (pd->pd_disk_pos < 0) 151989b17223SAlexander Motin return (-1); 152089b17223SAlexander Motin 152189b17223SAlexander Motin /* Erase metadata to prevent disks's later resurrection. */ 152289b17223SAlexander Motin if (tdisk->d_consumer) 152389b17223SAlexander Motin nvidia_meta_erase(tdisk->d_consumer); 152489b17223SAlexander Motin 152589b17223SAlexander Motin /* Change states. */ 152689b17223SAlexander Motin g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED); 152789b17223SAlexander Motin TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) { 152889b17223SAlexander Motin g_raid_change_subdisk_state(sd, 152989b17223SAlexander Motin G_RAID_SUBDISK_S_FAILED); 153089b17223SAlexander Motin g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED, 153189b17223SAlexander Motin G_RAID_EVENT_SUBDISK); 153289b17223SAlexander Motin } 153389b17223SAlexander Motin 153489b17223SAlexander Motin /* Write updated metadata to remaining disks. */ 153589b17223SAlexander Motin g_raid_md_write_nvidia(md, NULL, NULL, tdisk); 153689b17223SAlexander Motin 153789b17223SAlexander Motin /* Check if anything left except placeholders. */ 153889b17223SAlexander Motin if (g_raid_ndisks(sc, -1) == 153989b17223SAlexander Motin g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE)) 154089b17223SAlexander Motin g_raid_destroy_node(sc, 0); 154189b17223SAlexander Motin else 154289b17223SAlexander Motin g_raid_md_nvidia_refill(sc); 154389b17223SAlexander Motin return (0); 154489b17223SAlexander Motin } 154589b17223SAlexander Motin 154689b17223SAlexander Motin static int 154789b17223SAlexander Motin g_raid_md_free_disk_nvidia(struct g_raid_md_object *md, 154889b17223SAlexander Motin struct g_raid_disk *disk) 154989b17223SAlexander Motin { 155089b17223SAlexander Motin struct g_raid_md_nvidia_perdisk *pd; 155189b17223SAlexander Motin 155289b17223SAlexander Motin pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data; 155389b17223SAlexander Motin if (pd->pd_meta != NULL) { 155489b17223SAlexander Motin free(pd->pd_meta, M_MD_NVIDIA); 155589b17223SAlexander Motin pd->pd_meta = NULL; 155689b17223SAlexander Motin } 155789b17223SAlexander Motin free(pd, M_MD_NVIDIA); 155889b17223SAlexander Motin disk->d_md_data = NULL; 155989b17223SAlexander Motin return (0); 156089b17223SAlexander Motin } 156189b17223SAlexander Motin 156289b17223SAlexander Motin static int 156389b17223SAlexander Motin g_raid_md_free_nvidia(struct g_raid_md_object *md) 156489b17223SAlexander Motin { 156589b17223SAlexander Motin struct g_raid_md_nvidia_object *mdi; 156689b17223SAlexander Motin 156789b17223SAlexander Motin mdi = (struct g_raid_md_nvidia_object *)md; 156889b17223SAlexander Motin if (!mdi->mdio_started) { 156989b17223SAlexander Motin mdi->mdio_started = 0; 157089b17223SAlexander Motin callout_stop(&mdi->mdio_start_co); 157189b17223SAlexander Motin G_RAID_DEBUG1(1, md->mdo_softc, 157289b17223SAlexander Motin "root_mount_rel %p", mdi->mdio_rootmount); 157389b17223SAlexander Motin root_mount_rel(mdi->mdio_rootmount); 157489b17223SAlexander Motin mdi->mdio_rootmount = NULL; 157589b17223SAlexander Motin } 157689b17223SAlexander Motin if (mdi->mdio_meta != NULL) { 157789b17223SAlexander Motin free(mdi->mdio_meta, M_MD_NVIDIA); 157889b17223SAlexander Motin mdi->mdio_meta = NULL; 157989b17223SAlexander Motin } 158089b17223SAlexander Motin return (0); 158189b17223SAlexander Motin } 158289b17223SAlexander Motin 1583c89d2fbeSAlexander Motin G_RAID_MD_DECLARE(nvidia, "NVIDIA"); 1584