xref: /freebsd/sys/geom/raid/md_sii.c (revision ad1f936ab7e497926e46079de8df7407ab123213)
189b17223SAlexander Motin /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
33728855aSPedro F. Giffuni  *
489b17223SAlexander Motin  * Copyright (c) 2011 Alexander Motin <mav@FreeBSD.org>
5e26083caSAlexander Motin  * Copyright (c) 2000 - 2008 Søren Schmidt <sos@FreeBSD.org>
689b17223SAlexander Motin  * All rights reserved.
789b17223SAlexander Motin  *
889b17223SAlexander Motin  * Redistribution and use in source and binary forms, with or without
989b17223SAlexander Motin  * modification, are permitted provided that the following conditions
1089b17223SAlexander Motin  * are met:
1189b17223SAlexander Motin  * 1. Redistributions of source code must retain the above copyright
1289b17223SAlexander Motin  *    notice, this list of conditions and the following disclaimer.
1389b17223SAlexander Motin  * 2. Redistributions in binary form must reproduce the above copyright
1489b17223SAlexander Motin  *    notice, this list of conditions and the following disclaimer in the
1589b17223SAlexander Motin  *    documentation and/or other materials provided with the distribution.
1689b17223SAlexander Motin  *
1789b17223SAlexander Motin  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
1889b17223SAlexander Motin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1989b17223SAlexander Motin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2089b17223SAlexander Motin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
2189b17223SAlexander Motin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2289b17223SAlexander Motin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2389b17223SAlexander Motin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2489b17223SAlexander Motin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2589b17223SAlexander Motin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2689b17223SAlexander Motin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2789b17223SAlexander Motin  * SUCH DAMAGE.
2889b17223SAlexander Motin  */
2989b17223SAlexander Motin 
3089b17223SAlexander Motin #include <sys/param.h>
3189b17223SAlexander Motin #include <sys/bio.h>
3289b17223SAlexander Motin #include <sys/endian.h>
3389b17223SAlexander Motin #include <sys/kernel.h>
3489b17223SAlexander Motin #include <sys/kobj.h>
3589b17223SAlexander Motin #include <sys/limits.h>
3689b17223SAlexander Motin #include <sys/lock.h>
3789b17223SAlexander Motin #include <sys/malloc.h>
3889b17223SAlexander Motin #include <sys/mutex.h>
3989b17223SAlexander Motin #include <sys/systm.h>
4089b17223SAlexander Motin #include <sys/taskqueue.h>
4189b17223SAlexander Motin #include <geom/geom.h>
42ac03832eSConrad Meyer #include <geom/geom_dbg.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_SII, "md_sii_data", "GEOM_RAID SiI metadata");
4789b17223SAlexander Motin 
4889b17223SAlexander Motin struct sii_raid_conf {
4989b17223SAlexander Motin 	uint16_t	ata_params_00_53[54];
5089b17223SAlexander Motin 	uint64_t	total_sectors;		/* 54 - 57 */
5189b17223SAlexander Motin 	uint16_t	ata_params_58_81[72];
5289b17223SAlexander Motin 	uint16_t	product_id;		/* 130 */
5389b17223SAlexander Motin 	uint16_t	vendor_id;		/* 131 */
5489b17223SAlexander Motin 	uint16_t	version_minor;		/* 132 */
5589b17223SAlexander Motin 	uint16_t	version_major;		/* 133 */
5689b17223SAlexander Motin 	uint8_t		timestamp[6];		/* 134 - 136 */
5789b17223SAlexander Motin 	uint16_t	strip_sectors;		/* 137 */
5889b17223SAlexander Motin 	uint16_t	dummy_2;
5989b17223SAlexander Motin 	uint8_t		disk_number;		/* 139 */
6089b17223SAlexander Motin 	uint8_t		type;
6189b17223SAlexander Motin #define SII_T_RAID0             0x00
6289b17223SAlexander Motin #define SII_T_RAID1             0x01
6389b17223SAlexander Motin #define SII_T_RAID01            0x02
6489b17223SAlexander Motin #define SII_T_SPARE             0x03
6589b17223SAlexander Motin #define SII_T_CONCAT            0x04
6689b17223SAlexander Motin #define SII_T_RAID5             0x10
6789b17223SAlexander Motin #define SII_T_RESERVED          0xfd
6889b17223SAlexander Motin #define SII_T_JBOD              0xff
6989b17223SAlexander Motin 
7089b17223SAlexander Motin 	uint8_t		raid0_disks;		/* 140 */
7189b17223SAlexander Motin 	uint8_t		raid0_ident;
7289b17223SAlexander Motin 	uint8_t		raid1_disks;		/* 141 */
7389b17223SAlexander Motin 	uint8_t		raid1_ident;
7489b17223SAlexander Motin 	uint64_t	rebuild_lba;		/* 142 - 145 */
7589b17223SAlexander Motin 	uint32_t	generation;		/* 146 - 147 */
7689b17223SAlexander Motin 	uint8_t		disk_status;		/* 148 */
7789b17223SAlexander Motin #define SII_S_CURRENT           0x01
7889b17223SAlexander Motin #define SII_S_REBUILD           0x02
7989b17223SAlexander Motin #define SII_S_DROPPED           0x03
8089b17223SAlexander Motin #define SII_S_REMOVED           0x04
8189b17223SAlexander Motin 
8289b17223SAlexander Motin 	uint8_t		raid_status;
8389b17223SAlexander Motin #define SII_S_ONLINE            0x01
8489b17223SAlexander Motin #define SII_S_AVAILABLE         0x02
8589b17223SAlexander Motin 
8689b17223SAlexander Motin 	uint8_t		raid_location;		/* 149 */
8789b17223SAlexander Motin 	uint8_t		disk_location;
8889b17223SAlexander Motin 	uint8_t		auto_rebuild;		/* 150 */
8989b17223SAlexander Motin #define SII_R_REBUILD           0x00
9089b17223SAlexander Motin #define SII_R_NOREBUILD         0xff
9189b17223SAlexander Motin 
9289b17223SAlexander Motin 	uint8_t		dummy_3;
9389b17223SAlexander Motin 	uint8_t		name[16];		/* 151 - 158 */
9489b17223SAlexander Motin 	uint16_t	checksum;		/* 159 */
9589b17223SAlexander Motin 	uint16_t	ata_params_160_255[96];
9689b17223SAlexander Motin } __packed;
9789b17223SAlexander Motin 
9889b17223SAlexander Motin struct g_raid_md_sii_perdisk {
9989b17223SAlexander Motin 	struct sii_raid_conf	*pd_meta;
10089b17223SAlexander Motin 	int			 pd_disk_pos;
10189b17223SAlexander Motin 	off_t			 pd_disk_size;
10289b17223SAlexander Motin };
10389b17223SAlexander Motin 
10489b17223SAlexander Motin struct g_raid_md_sii_object {
10589b17223SAlexander Motin 	struct g_raid_md_object	 mdio_base;
10689b17223SAlexander Motin 	uint8_t			 mdio_timestamp[6];
10789b17223SAlexander Motin 	uint8_t			 mdio_location;
10889b17223SAlexander Motin 	uint32_t		 mdio_generation;
10989b17223SAlexander Motin 	struct sii_raid_conf	*mdio_meta;
11089b17223SAlexander Motin 	struct callout		 mdio_start_co;	/* STARTING state timer. */
11189b17223SAlexander Motin 	int			 mdio_total_disks;
11289b17223SAlexander Motin 	int			 mdio_disks_present;
11389b17223SAlexander Motin 	int			 mdio_started;
11489b17223SAlexander Motin 	int			 mdio_incomplete;
11589b17223SAlexander Motin 	struct root_hold_token	*mdio_rootmount; /* Root mount delay token. */
11689b17223SAlexander Motin };
11789b17223SAlexander Motin 
11889b17223SAlexander Motin static g_raid_md_create_t g_raid_md_create_sii;
11989b17223SAlexander Motin static g_raid_md_taste_t g_raid_md_taste_sii;
12089b17223SAlexander Motin static g_raid_md_event_t g_raid_md_event_sii;
12189b17223SAlexander Motin static g_raid_md_ctl_t g_raid_md_ctl_sii;
12289b17223SAlexander Motin static g_raid_md_write_t g_raid_md_write_sii;
12389b17223SAlexander Motin static g_raid_md_fail_disk_t g_raid_md_fail_disk_sii;
12489b17223SAlexander Motin static g_raid_md_free_disk_t g_raid_md_free_disk_sii;
12589b17223SAlexander Motin static g_raid_md_free_t g_raid_md_free_sii;
12689b17223SAlexander Motin 
12789b17223SAlexander Motin static kobj_method_t g_raid_md_sii_methods[] = {
12889b17223SAlexander Motin 	KOBJMETHOD(g_raid_md_create,	g_raid_md_create_sii),
12989b17223SAlexander Motin 	KOBJMETHOD(g_raid_md_taste,	g_raid_md_taste_sii),
13089b17223SAlexander Motin 	KOBJMETHOD(g_raid_md_event,	g_raid_md_event_sii),
13189b17223SAlexander Motin 	KOBJMETHOD(g_raid_md_ctl,	g_raid_md_ctl_sii),
13289b17223SAlexander Motin 	KOBJMETHOD(g_raid_md_write,	g_raid_md_write_sii),
13389b17223SAlexander Motin 	KOBJMETHOD(g_raid_md_fail_disk,	g_raid_md_fail_disk_sii),
13489b17223SAlexander Motin 	KOBJMETHOD(g_raid_md_free_disk,	g_raid_md_free_disk_sii),
13589b17223SAlexander Motin 	KOBJMETHOD(g_raid_md_free,	g_raid_md_free_sii),
13689b17223SAlexander Motin 	{ 0, 0 }
13789b17223SAlexander Motin };
13889b17223SAlexander Motin 
13989b17223SAlexander Motin static struct g_raid_md_class g_raid_md_sii_class = {
14089b17223SAlexander Motin 	"SiI",
14189b17223SAlexander Motin 	g_raid_md_sii_methods,
14289b17223SAlexander Motin 	sizeof(struct g_raid_md_sii_object),
143c89d2fbeSAlexander Motin 	.mdc_enable = 1,
14489b17223SAlexander Motin 	.mdc_priority = 100
14589b17223SAlexander Motin };
14689b17223SAlexander Motin 
14789b17223SAlexander Motin static void
g_raid_md_sii_print(struct sii_raid_conf * meta)14889b17223SAlexander Motin g_raid_md_sii_print(struct sii_raid_conf *meta)
14989b17223SAlexander Motin {
15089b17223SAlexander Motin 
15189b17223SAlexander Motin 	if (g_raid_debug < 1)
15289b17223SAlexander Motin 		return;
15389b17223SAlexander Motin 
15489b17223SAlexander Motin 	printf("********* ATA SiI RAID Metadata *********\n");
15589b17223SAlexander Motin 	printf("total_sectors       %llu\n",
15689b17223SAlexander Motin 	    (long long unsigned)meta->total_sectors);
15789b17223SAlexander Motin 	printf("product_id          0x%04x\n", meta->product_id);
15889b17223SAlexander Motin 	printf("vendor_id           0x%04x\n", meta->vendor_id);
15989b17223SAlexander Motin 	printf("version_minor       0x%04x\n", meta->version_minor);
16089b17223SAlexander Motin 	printf("version_major       0x%04x\n", meta->version_major);
16189b17223SAlexander Motin 	printf("timestamp           0x%02x%02x%02x%02x%02x%02x\n",
16289b17223SAlexander Motin 	    meta->timestamp[5], meta->timestamp[4], meta->timestamp[3],
16389b17223SAlexander Motin 	    meta->timestamp[2], meta->timestamp[1], meta->timestamp[0]);
16489b17223SAlexander Motin 	printf("strip_sectors       %d\n", meta->strip_sectors);
16589b17223SAlexander Motin 	printf("disk_number         %d\n", meta->disk_number);
16689b17223SAlexander Motin 	printf("type                0x%02x\n", meta->type);
16789b17223SAlexander Motin 	printf("raid0_disks         %d\n", meta->raid0_disks);
16889b17223SAlexander Motin 	printf("raid0_ident         %d\n", meta->raid0_ident);
16989b17223SAlexander Motin 	printf("raid1_disks         %d\n", meta->raid1_disks);
17089b17223SAlexander Motin 	printf("raid1_ident         %d\n", meta->raid1_ident);
17189b17223SAlexander Motin 	printf("rebuild_lba         %llu\n",
17289b17223SAlexander Motin 	    (long long unsigned)meta->rebuild_lba);
17389b17223SAlexander Motin 	printf("generation          %d\n", meta->generation);
17489b17223SAlexander Motin 	printf("disk_status         %d\n", meta->disk_status);
17589b17223SAlexander Motin 	printf("raid_status         %d\n", meta->raid_status);
17689b17223SAlexander Motin 	printf("raid_location       %d\n", meta->raid_location);
17789b17223SAlexander Motin 	printf("disk_location       %d\n", meta->disk_location);
17889b17223SAlexander Motin 	printf("auto_rebuild        %d\n", meta->auto_rebuild);
17989b17223SAlexander Motin 	printf("name                <%.16s>\n", meta->name);
18089b17223SAlexander Motin 	printf("checksum            0x%04x\n", meta->checksum);
18189b17223SAlexander Motin 	printf("=================================================\n");
18289b17223SAlexander Motin }
18389b17223SAlexander Motin 
18489b17223SAlexander Motin static struct sii_raid_conf *
sii_meta_copy(struct sii_raid_conf * meta)18589b17223SAlexander Motin sii_meta_copy(struct sii_raid_conf *meta)
18689b17223SAlexander Motin {
18789b17223SAlexander Motin 	struct sii_raid_conf *nmeta;
18889b17223SAlexander Motin 
18989b17223SAlexander Motin 	nmeta = malloc(sizeof(*meta), M_MD_SII, M_WAITOK);
19089b17223SAlexander Motin 	memcpy(nmeta, meta, sizeof(*meta));
19189b17223SAlexander Motin 	return (nmeta);
19289b17223SAlexander Motin }
19389b17223SAlexander Motin 
19489b17223SAlexander Motin static int
sii_meta_total_disks(struct sii_raid_conf * meta)19589b17223SAlexander Motin sii_meta_total_disks(struct sii_raid_conf *meta)
19689b17223SAlexander Motin {
19789b17223SAlexander Motin 
19889b17223SAlexander Motin 	switch (meta->type) {
19989b17223SAlexander Motin 	case SII_T_RAID0:
20089b17223SAlexander Motin 	case SII_T_RAID5:
20189b17223SAlexander Motin 	case SII_T_CONCAT:
20289b17223SAlexander Motin 		return (meta->raid0_disks);
20389b17223SAlexander Motin 	case SII_T_RAID1:
20489b17223SAlexander Motin 		return (meta->raid1_disks);
20589b17223SAlexander Motin 	case SII_T_RAID01:
20689b17223SAlexander Motin 		return (meta->raid0_disks * meta->raid1_disks);
20789b17223SAlexander Motin 	case SII_T_SPARE:
20889b17223SAlexander Motin 	case SII_T_JBOD:
20989b17223SAlexander Motin 		return (1);
21089b17223SAlexander Motin 	}
21189b17223SAlexander Motin 	return (0);
21289b17223SAlexander Motin }
21389b17223SAlexander Motin 
21489b17223SAlexander Motin static int
sii_meta_disk_pos(struct sii_raid_conf * meta,struct sii_raid_conf * pdmeta)21589b17223SAlexander Motin sii_meta_disk_pos(struct sii_raid_conf *meta, struct sii_raid_conf *pdmeta)
21689b17223SAlexander Motin {
21789b17223SAlexander Motin 
21889b17223SAlexander Motin 	if (pdmeta->type == SII_T_SPARE)
21989b17223SAlexander Motin 		return (-3);
22089b17223SAlexander Motin 
22189b17223SAlexander Motin 	if (memcmp(&meta->timestamp, &pdmeta->timestamp, 6) != 0)
22289b17223SAlexander Motin 		return (-1);
22389b17223SAlexander Motin 
22489b17223SAlexander Motin 	switch (pdmeta->type) {
22589b17223SAlexander Motin 	case SII_T_RAID0:
22689b17223SAlexander Motin 	case SII_T_RAID1:
22789b17223SAlexander Motin 	case SII_T_RAID5:
22889b17223SAlexander Motin 	case SII_T_CONCAT:
22989b17223SAlexander Motin 		return (pdmeta->disk_number);
23089b17223SAlexander Motin 	case SII_T_RAID01:
23189b17223SAlexander Motin 		return (pdmeta->raid1_ident * pdmeta->raid1_disks +
23289b17223SAlexander Motin 		    pdmeta->raid0_ident);
23389b17223SAlexander Motin 	case SII_T_JBOD:
23489b17223SAlexander Motin 		return (0);
23589b17223SAlexander Motin 	}
23689b17223SAlexander Motin 	return (-1);
23789b17223SAlexander Motin }
23889b17223SAlexander Motin 
23989b17223SAlexander Motin static void
sii_meta_get_name(struct sii_raid_conf * meta,char * buf)24089b17223SAlexander Motin sii_meta_get_name(struct sii_raid_conf *meta, char *buf)
24189b17223SAlexander Motin {
24289b17223SAlexander Motin 	int i;
24389b17223SAlexander Motin 
24489b17223SAlexander Motin 	strncpy(buf, meta->name, 16);
24589b17223SAlexander Motin 	buf[16] = 0;
24689b17223SAlexander Motin 	for (i = 15; i >= 0; i--) {
24789b17223SAlexander Motin 		if (buf[i] > 0x20)
24889b17223SAlexander Motin 			break;
24989b17223SAlexander Motin 		buf[i] = 0;
25089b17223SAlexander Motin 	}
25189b17223SAlexander Motin }
25289b17223SAlexander Motin 
25389b17223SAlexander Motin static void
sii_meta_put_name(struct sii_raid_conf * meta,char * buf)25489b17223SAlexander Motin sii_meta_put_name(struct sii_raid_conf *meta, char *buf)
25589b17223SAlexander Motin {
25689b17223SAlexander Motin 
25789b17223SAlexander Motin 	memset(meta->name, 0x20, 16);
25889b17223SAlexander Motin 	memcpy(meta->name, buf, MIN(strlen(buf), 16));
25989b17223SAlexander Motin }
26089b17223SAlexander Motin 
26189b17223SAlexander Motin static struct sii_raid_conf *
sii_meta_read(struct g_consumer * cp)26289b17223SAlexander Motin sii_meta_read(struct g_consumer *cp)
26389b17223SAlexander Motin {
26489b17223SAlexander Motin 	struct g_provider *pp;
26589b17223SAlexander Motin 	struct sii_raid_conf *meta;
26689b17223SAlexander Motin 	char *buf;
26789b17223SAlexander Motin 	int error, i;
26889b17223SAlexander Motin 	uint16_t checksum, *ptr;
26989b17223SAlexander Motin 
27089b17223SAlexander Motin 	pp = cp->provider;
2719e9ba9c7SMark Johnston 	if (pp->sectorsize < sizeof(*meta))
2729e9ba9c7SMark Johnston 		return (NULL);
27389b17223SAlexander Motin 	/* Read the anchor sector. */
27489b17223SAlexander Motin 	buf = g_read_data(cp,
27589b17223SAlexander Motin 	    pp->mediasize - pp->sectorsize, pp->sectorsize, &error);
27689b17223SAlexander Motin 	if (buf == NULL) {
27789b17223SAlexander Motin 		G_RAID_DEBUG(1, "Cannot read metadata from %s (error=%d).",
27889b17223SAlexander Motin 		    pp->name, error);
27989b17223SAlexander Motin 		return (NULL);
28089b17223SAlexander Motin 	}
2811e68fe9cSAlexander Motin 	meta = (struct sii_raid_conf *)buf;
28289b17223SAlexander Motin 
28389b17223SAlexander Motin 	/* Check vendor ID. */
28489b17223SAlexander Motin 	if (meta->vendor_id != 0x1095) {
28589b17223SAlexander Motin 		G_RAID_DEBUG(1, "SiI vendor ID check failed on %s (0x%04x)",
28689b17223SAlexander Motin 		    pp->name, meta->vendor_id);
2871e68fe9cSAlexander Motin 		g_free(buf);
28889b17223SAlexander Motin 		return (NULL);
28989b17223SAlexander Motin 	}
29089b17223SAlexander Motin 
29189b17223SAlexander Motin 	/* Check metadata major version. */
29289b17223SAlexander Motin 	if (meta->version_major != 2) {
29389b17223SAlexander Motin 		G_RAID_DEBUG(1, "SiI version check failed on %s (%d.%d)",
29489b17223SAlexander Motin 		    pp->name, meta->version_major, meta->version_minor);
2951e68fe9cSAlexander Motin 		g_free(buf);
29689b17223SAlexander Motin 		return (NULL);
29789b17223SAlexander Motin 	}
2981e68fe9cSAlexander Motin 	meta = malloc(sizeof(*meta), M_MD_SII, M_WAITOK);
2991e68fe9cSAlexander Motin 	memcpy(meta, buf, min(sizeof(*meta), pp->sectorsize));
3001e68fe9cSAlexander Motin 	g_free(buf);
30189b17223SAlexander Motin 
30289b17223SAlexander Motin 	/* Check metadata checksum. */
30389b17223SAlexander Motin 	for (checksum = 0, ptr = (uint16_t *)meta, i = 0; i <= 159; i++)
30489b17223SAlexander Motin 		checksum += *ptr++;
30589b17223SAlexander Motin 	if (checksum != 0) {
30689b17223SAlexander Motin 		G_RAID_DEBUG(1, "SiI checksum check failed on %s", pp->name);
30789b17223SAlexander Motin 		free(meta, M_MD_SII);
30889b17223SAlexander Motin 		return (NULL);
30989b17223SAlexander Motin 	}
31089b17223SAlexander Motin 
31189b17223SAlexander Motin 	/* Check raid type. */
31289b17223SAlexander Motin 	if (meta->type != SII_T_RAID0 && meta->type != SII_T_RAID1 &&
31389b17223SAlexander Motin 	    meta->type != SII_T_RAID01 && meta->type != SII_T_SPARE &&
31489b17223SAlexander Motin 	    meta->type != SII_T_RAID5 && meta->type != SII_T_CONCAT &&
31589b17223SAlexander Motin 	    meta->type != SII_T_JBOD) {
31689b17223SAlexander Motin 		G_RAID_DEBUG(1, "SiI unknown RAID level on %s (0x%02x)",
31789b17223SAlexander Motin 		    pp->name, meta->type);
31889b17223SAlexander Motin 		free(meta, M_MD_SII);
31989b17223SAlexander Motin 		return (NULL);
32089b17223SAlexander Motin 	}
32189b17223SAlexander Motin 
32289b17223SAlexander Motin 	return (meta);
32389b17223SAlexander Motin }
32489b17223SAlexander Motin 
32589b17223SAlexander Motin static int
sii_meta_write(struct g_consumer * cp,struct sii_raid_conf * meta)32689b17223SAlexander Motin sii_meta_write(struct g_consumer *cp, struct sii_raid_conf *meta)
32789b17223SAlexander Motin {
32889b17223SAlexander Motin 	struct g_provider *pp;
32989b17223SAlexander Motin 	char *buf;
33089b17223SAlexander Motin 	int error, i;
33189b17223SAlexander Motin 	uint16_t checksum, *ptr;
33289b17223SAlexander Motin 
33389b17223SAlexander Motin 	pp = cp->provider;
33489b17223SAlexander Motin 
33589b17223SAlexander Motin 	/* Recalculate checksum for case if metadata were changed. */
33689b17223SAlexander Motin 	meta->checksum = 0;
33789b17223SAlexander Motin 	for (checksum = 0, ptr = (uint16_t *)meta, i = 0; i < 159; i++)
33889b17223SAlexander Motin 		checksum += *ptr++;
33989b17223SAlexander Motin 	meta->checksum -= checksum;
34089b17223SAlexander Motin 
34189b17223SAlexander Motin 	/* Create and fill buffer. */
34289b17223SAlexander Motin 	buf = malloc(pp->sectorsize, M_MD_SII, M_WAITOK | M_ZERO);
34389b17223SAlexander Motin 	memcpy(buf, meta, sizeof(*meta));
34489b17223SAlexander Motin 
34589b17223SAlexander Motin 	/* Write 4 copies of metadata. */
34689b17223SAlexander Motin 	for (i = 0; i < 4; i++) {
34789b17223SAlexander Motin 		error = g_write_data(cp,
34889b17223SAlexander Motin 		    pp->mediasize - (pp->sectorsize * (1 + 0x200 * i)),
34989b17223SAlexander Motin 		    buf, pp->sectorsize);
35089b17223SAlexander Motin 		if (error != 0) {
35189b17223SAlexander Motin 			G_RAID_DEBUG(1, "Cannot write metadata to %s (error=%d).",
35289b17223SAlexander Motin 			    pp->name, error);
35389b17223SAlexander Motin 			break;
35489b17223SAlexander Motin 		}
35589b17223SAlexander Motin 	}
35689b17223SAlexander Motin 
35789b17223SAlexander Motin 	free(buf, M_MD_SII);
35889b17223SAlexander Motin 	return (error);
35989b17223SAlexander Motin }
36089b17223SAlexander Motin 
36189b17223SAlexander Motin static int
sii_meta_erase(struct g_consumer * cp)36289b17223SAlexander Motin sii_meta_erase(struct g_consumer *cp)
36389b17223SAlexander Motin {
36489b17223SAlexander Motin 	struct g_provider *pp;
36589b17223SAlexander Motin 	char *buf;
36689b17223SAlexander Motin 	int error, i;
36789b17223SAlexander Motin 
36889b17223SAlexander Motin 	pp = cp->provider;
36989b17223SAlexander Motin 	buf = malloc(pp->sectorsize, M_MD_SII, M_WAITOK | M_ZERO);
37089b17223SAlexander Motin 	/* Write 4 copies of metadata. */
37189b17223SAlexander Motin 	for (i = 0; i < 4; i++) {
37289b17223SAlexander Motin 		error = g_write_data(cp,
37389b17223SAlexander Motin 		    pp->mediasize - (pp->sectorsize * (1 + 0x200 * i)),
37489b17223SAlexander Motin 		    buf, pp->sectorsize);
37589b17223SAlexander Motin 		if (error != 0) {
37689b17223SAlexander Motin 			G_RAID_DEBUG(1, "Cannot erase metadata on %s (error=%d).",
37789b17223SAlexander Motin 			    pp->name, error);
37889b17223SAlexander Motin 		}
37989b17223SAlexander Motin 	}
38089b17223SAlexander Motin 	free(buf, M_MD_SII);
38189b17223SAlexander Motin 	return (error);
38289b17223SAlexander Motin }
38389b17223SAlexander Motin 
38489b17223SAlexander Motin static int
sii_meta_write_spare(struct g_consumer * cp)38589b17223SAlexander Motin sii_meta_write_spare(struct g_consumer *cp)
38689b17223SAlexander Motin {
38789b17223SAlexander Motin 	struct sii_raid_conf *meta;
38889b17223SAlexander Motin 	int error;
38989b17223SAlexander Motin 
39089b17223SAlexander Motin 	meta = malloc(sizeof(*meta), M_MD_SII, M_WAITOK | M_ZERO);
39189b17223SAlexander Motin 	meta->total_sectors = cp->provider->mediasize /
39289b17223SAlexander Motin 	    cp->provider->sectorsize - 0x800;
39389b17223SAlexander Motin 	meta->vendor_id = 0x1095;
39489b17223SAlexander Motin 	meta->version_minor = 0;
39589b17223SAlexander Motin 	meta->version_major = 2;
39689b17223SAlexander Motin 	meta->timestamp[0] = arc4random();
39789b17223SAlexander Motin 	meta->timestamp[1] = arc4random();
39889b17223SAlexander Motin 	meta->timestamp[2] = arc4random();
39989b17223SAlexander Motin 	meta->timestamp[3] = arc4random();
40089b17223SAlexander Motin 	meta->timestamp[4] = arc4random();
40189b17223SAlexander Motin 	meta->timestamp[5] = arc4random();
40289b17223SAlexander Motin 	meta->type = SII_T_SPARE;
40389b17223SAlexander Motin 	meta->generation = 1;
40489b17223SAlexander Motin 	meta->raid1_ident = 0xff;
40589b17223SAlexander Motin 	meta->raid_location = arc4random();
40689b17223SAlexander Motin 	error = sii_meta_write(cp, meta);
40789b17223SAlexander Motin 	free(meta, M_MD_SII);
40889b17223SAlexander Motin 	return (error);
40989b17223SAlexander Motin }
41089b17223SAlexander Motin 
41189b17223SAlexander Motin static struct g_raid_disk *
g_raid_md_sii_get_disk(struct g_raid_softc * sc,int id)41289b17223SAlexander Motin g_raid_md_sii_get_disk(struct g_raid_softc *sc, int id)
41389b17223SAlexander Motin {
41489b17223SAlexander Motin 	struct g_raid_disk	*disk;
41589b17223SAlexander Motin 	struct g_raid_md_sii_perdisk *pd;
41689b17223SAlexander Motin 
41789b17223SAlexander Motin 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
41889b17223SAlexander Motin 		pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
41989b17223SAlexander Motin 		if (pd->pd_disk_pos == id)
42089b17223SAlexander Motin 			break;
42189b17223SAlexander Motin 	}
42289b17223SAlexander Motin 	return (disk);
42389b17223SAlexander Motin }
42489b17223SAlexander Motin 
42589b17223SAlexander Motin static int
g_raid_md_sii_supported(int level,int qual,int disks,int force)42689b17223SAlexander Motin g_raid_md_sii_supported(int level, int qual, int disks, int force)
42789b17223SAlexander Motin {
42889b17223SAlexander Motin 
42989b17223SAlexander Motin 	if (disks > 8)
43089b17223SAlexander Motin 		return (0);
43189b17223SAlexander Motin 	switch (level) {
43289b17223SAlexander Motin 	case G_RAID_VOLUME_RL_RAID0:
43389b17223SAlexander Motin 		if (disks < 1)
43489b17223SAlexander Motin 			return (0);
43589b17223SAlexander Motin 		if (!force && (disks < 2 || disks > 6))
43689b17223SAlexander Motin 			return (0);
43789b17223SAlexander Motin 		break;
43889b17223SAlexander Motin 	case G_RAID_VOLUME_RL_RAID1:
43989b17223SAlexander Motin 		if (disks < 1)
44089b17223SAlexander Motin 			return (0);
44189b17223SAlexander Motin 		if (!force && (disks != 2))
44289b17223SAlexander Motin 			return (0);
44389b17223SAlexander Motin 		break;
44489b17223SAlexander Motin 	case G_RAID_VOLUME_RL_RAID1E:
44589b17223SAlexander Motin 		if (disks < 2)
44689b17223SAlexander Motin 			return (0);
44789b17223SAlexander Motin 		if (disks % 2 != 0)
44889b17223SAlexander Motin 			return (0);
44989b17223SAlexander Motin 		if (!force && (disks < 4))
45089b17223SAlexander Motin 			return (0);
45189b17223SAlexander Motin 		break;
45289b17223SAlexander Motin 	case G_RAID_VOLUME_RL_SINGLE:
45389b17223SAlexander Motin 		if (disks != 1)
45489b17223SAlexander Motin 			return (0);
45589b17223SAlexander Motin 		break;
45689b17223SAlexander Motin 	case G_RAID_VOLUME_RL_CONCAT:
45789b17223SAlexander Motin 		if (disks < 2)
45889b17223SAlexander Motin 			return (0);
45989b17223SAlexander Motin 		break;
46089b17223SAlexander Motin 	case G_RAID_VOLUME_RL_RAID5:
46189b17223SAlexander Motin 		if (disks < 3)
46289b17223SAlexander Motin 			return (0);
463fc1de960SAlexander Motin 		if (qual != G_RAID_VOLUME_RLQ_R5LS)
464fc1de960SAlexander Motin 			return (0);
46589b17223SAlexander Motin 		break;
46689b17223SAlexander Motin 	default:
46789b17223SAlexander Motin 		return (0);
46889b17223SAlexander Motin 	}
469fc1de960SAlexander Motin 	if (level != G_RAID_VOLUME_RL_RAID5 && qual != G_RAID_VOLUME_RLQ_NONE)
47089b17223SAlexander Motin 		return (0);
47189b17223SAlexander Motin 	return (1);
47289b17223SAlexander Motin }
47389b17223SAlexander Motin 
47489b17223SAlexander Motin static int
g_raid_md_sii_start_disk(struct g_raid_disk * disk)47589b17223SAlexander Motin g_raid_md_sii_start_disk(struct g_raid_disk *disk)
47689b17223SAlexander Motin {
47789b17223SAlexander Motin 	struct g_raid_softc *sc;
47889b17223SAlexander Motin 	struct g_raid_subdisk *sd, *tmpsd;
47989b17223SAlexander Motin 	struct g_raid_disk *olddisk, *tmpdisk;
48089b17223SAlexander Motin 	struct g_raid_md_object *md;
48189b17223SAlexander Motin 	struct g_raid_md_sii_object *mdi;
48289b17223SAlexander Motin 	struct g_raid_md_sii_perdisk *pd, *oldpd;
48389b17223SAlexander Motin 	struct sii_raid_conf *meta;
48489b17223SAlexander Motin 	int disk_pos, resurrection = 0;
48589b17223SAlexander Motin 
48689b17223SAlexander Motin 	sc = disk->d_softc;
48789b17223SAlexander Motin 	md = sc->sc_md;
48889b17223SAlexander Motin 	mdi = (struct g_raid_md_sii_object *)md;
48989b17223SAlexander Motin 	meta = mdi->mdio_meta;
49089b17223SAlexander Motin 	pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
49189b17223SAlexander Motin 	olddisk = NULL;
49289b17223SAlexander Motin 
49328323addSBryan Drewery 	/* Find disk position in metadata by its serial. */
49489b17223SAlexander Motin 	if (pd->pd_meta != NULL)
49589b17223SAlexander Motin 		disk_pos = sii_meta_disk_pos(meta, pd->pd_meta);
49689b17223SAlexander Motin 	else
49789b17223SAlexander Motin 		disk_pos = -3;
49889b17223SAlexander Motin 	if (disk_pos < 0) {
49989b17223SAlexander Motin 		G_RAID_DEBUG1(1, sc, "Unknown, probably new or stale disk");
50089b17223SAlexander Motin 		/* If we are in the start process, that's all for now. */
50189b17223SAlexander Motin 		if (!mdi->mdio_started)
50289b17223SAlexander Motin 			goto nofit;
50389b17223SAlexander Motin 		/*
50489b17223SAlexander Motin 		 * If we have already started - try to get use of the disk.
50589b17223SAlexander Motin 		 * Try to replace OFFLINE disks first, then FAILED.
50689b17223SAlexander Motin 		 */
50789b17223SAlexander Motin 		TAILQ_FOREACH(tmpdisk, &sc->sc_disks, d_next) {
50889b17223SAlexander Motin 			if (tmpdisk->d_state != G_RAID_DISK_S_OFFLINE &&
50989b17223SAlexander Motin 			    tmpdisk->d_state != G_RAID_DISK_S_FAILED)
51089b17223SAlexander Motin 				continue;
51189b17223SAlexander Motin 			/* Make sure this disk is big enough. */
51289b17223SAlexander Motin 			TAILQ_FOREACH(sd, &tmpdisk->d_subdisks, sd_next) {
51389b17223SAlexander Motin 				if (sd->sd_offset + sd->sd_size + 512 >
51489b17223SAlexander Motin 				    pd->pd_disk_size) {
51589b17223SAlexander Motin 					G_RAID_DEBUG1(1, sc,
51689b17223SAlexander Motin 					    "Disk too small (%ju < %ju)",
51789b17223SAlexander Motin 					    pd->pd_disk_size,
51889b17223SAlexander Motin 					    sd->sd_offset + sd->sd_size + 512);
51989b17223SAlexander Motin 					break;
52089b17223SAlexander Motin 				}
52189b17223SAlexander Motin 			}
52289b17223SAlexander Motin 			if (sd != NULL)
52389b17223SAlexander Motin 				continue;
52489b17223SAlexander Motin 			if (tmpdisk->d_state == G_RAID_DISK_S_OFFLINE) {
52589b17223SAlexander Motin 				olddisk = tmpdisk;
52689b17223SAlexander Motin 				break;
52789b17223SAlexander Motin 			} else if (olddisk == NULL)
52889b17223SAlexander Motin 				olddisk = tmpdisk;
52989b17223SAlexander Motin 		}
53089b17223SAlexander Motin 		if (olddisk == NULL) {
53189b17223SAlexander Motin nofit:
53289b17223SAlexander Motin 			if (disk_pos == -3 || pd->pd_disk_pos == -3) {
53389b17223SAlexander Motin 				g_raid_change_disk_state(disk,
53489b17223SAlexander Motin 				    G_RAID_DISK_S_SPARE);
53589b17223SAlexander Motin 				return (1);
53689b17223SAlexander Motin 			} else {
53789b17223SAlexander Motin 				g_raid_change_disk_state(disk,
53889b17223SAlexander Motin 				    G_RAID_DISK_S_STALE);
53989b17223SAlexander Motin 				return (0);
54089b17223SAlexander Motin 			}
54189b17223SAlexander Motin 		}
54289b17223SAlexander Motin 		oldpd = (struct g_raid_md_sii_perdisk *)olddisk->d_md_data;
54389b17223SAlexander Motin 		disk_pos = oldpd->pd_disk_pos;
54489b17223SAlexander Motin 		resurrection = 1;
54589b17223SAlexander Motin 	}
54689b17223SAlexander Motin 
54789b17223SAlexander Motin 	if (olddisk == NULL) {
54889b17223SAlexander Motin 		/* Find placeholder by position. */
54989b17223SAlexander Motin 		olddisk = g_raid_md_sii_get_disk(sc, disk_pos);
55089b17223SAlexander Motin 		if (olddisk == NULL)
55189b17223SAlexander Motin 			panic("No disk at position %d!", disk_pos);
55289b17223SAlexander Motin 		if (olddisk->d_state != G_RAID_DISK_S_OFFLINE) {
5536bc3fe5fSPedro F. Giffuni 			G_RAID_DEBUG1(1, sc, "More than one disk for pos %d",
55489b17223SAlexander Motin 			    disk_pos);
55589b17223SAlexander Motin 			g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE);
55689b17223SAlexander Motin 			return (0);
55789b17223SAlexander Motin 		}
55889b17223SAlexander Motin 		oldpd = (struct g_raid_md_sii_perdisk *)olddisk->d_md_data;
55989b17223SAlexander Motin 	}
56089b17223SAlexander Motin 
56189b17223SAlexander Motin 	/* Replace failed disk or placeholder with new disk. */
56289b17223SAlexander Motin 	TAILQ_FOREACH_SAFE(sd, &olddisk->d_subdisks, sd_next, tmpsd) {
56389b17223SAlexander Motin 		TAILQ_REMOVE(&olddisk->d_subdisks, sd, sd_next);
56489b17223SAlexander Motin 		TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
56589b17223SAlexander Motin 		sd->sd_disk = disk;
56689b17223SAlexander Motin 	}
56789b17223SAlexander Motin 	oldpd->pd_disk_pos = -2;
56889b17223SAlexander Motin 	pd->pd_disk_pos = disk_pos;
56989b17223SAlexander Motin 
57089b17223SAlexander Motin 	/* If it was placeholder -- destroy it. */
57189b17223SAlexander Motin 	if (olddisk->d_state == G_RAID_DISK_S_OFFLINE) {
57289b17223SAlexander Motin 		g_raid_destroy_disk(olddisk);
57389b17223SAlexander Motin 	} else {
57489b17223SAlexander Motin 		/* Otherwise, make it STALE_FAILED. */
57589b17223SAlexander Motin 		g_raid_change_disk_state(olddisk, G_RAID_DISK_S_STALE_FAILED);
57689b17223SAlexander Motin 	}
57789b17223SAlexander Motin 
57889b17223SAlexander Motin 	/* Welcome the new disk. */
57989b17223SAlexander Motin 	if (resurrection)
58089b17223SAlexander Motin 		g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
58189b17223SAlexander Motin 	else if (pd->pd_meta->disk_status == SII_S_CURRENT ||
58289b17223SAlexander Motin 	    pd->pd_meta->disk_status == SII_S_REBUILD)
58389b17223SAlexander Motin 		g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
58489b17223SAlexander Motin 	else
58589b17223SAlexander Motin 		g_raid_change_disk_state(disk, G_RAID_DISK_S_FAILED);
58689b17223SAlexander Motin 	TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
58789b17223SAlexander Motin 		/*
58889b17223SAlexander Motin 		 * Different disks may have different sizes,
58989b17223SAlexander Motin 		 * in concat mode. Update from real disk size.
59089b17223SAlexander Motin 		 */
59189b17223SAlexander Motin 		if (meta->type == SII_T_CONCAT || meta->type == SII_T_JBOD)
59289b17223SAlexander Motin 			sd->sd_size = pd->pd_disk_size - 0x800 * 512;
59389b17223SAlexander Motin 
59489b17223SAlexander Motin 		if (resurrection) {
59589b17223SAlexander Motin 			/* New or ex-spare disk. */
59689b17223SAlexander Motin 			g_raid_change_subdisk_state(sd,
59789b17223SAlexander Motin 			    G_RAID_SUBDISK_S_NEW);
59889b17223SAlexander Motin 		} else if (pd->pd_meta->disk_status == SII_S_REBUILD) {
59989b17223SAlexander Motin 			/* Rebuilding disk. */
60089b17223SAlexander Motin 			g_raid_change_subdisk_state(sd,
60189b17223SAlexander Motin 			    G_RAID_SUBDISK_S_REBUILD);
60289b17223SAlexander Motin 			if (pd->pd_meta->generation == meta->generation)
60389b17223SAlexander Motin 				sd->sd_rebuild_pos = pd->pd_meta->rebuild_lba * 512;
60489b17223SAlexander Motin 			else
60589b17223SAlexander Motin 				sd->sd_rebuild_pos = 0;
60689b17223SAlexander Motin 		} else if (pd->pd_meta->disk_status == SII_S_CURRENT) {
60789b17223SAlexander Motin 			if (pd->pd_meta->raid_status == SII_S_ONLINE ||
60889b17223SAlexander Motin 			    pd->pd_meta->generation != meta->generation) {
60989b17223SAlexander Motin 				/* Dirty or resyncing disk. */
61089b17223SAlexander Motin 				g_raid_change_subdisk_state(sd,
61189b17223SAlexander Motin 				    G_RAID_SUBDISK_S_STALE);
61289b17223SAlexander Motin 			} else {
61389b17223SAlexander Motin 				/* Up to date disk. */
61489b17223SAlexander Motin 				g_raid_change_subdisk_state(sd,
61589b17223SAlexander Motin 				    G_RAID_SUBDISK_S_ACTIVE);
61689b17223SAlexander Motin 			}
61789b17223SAlexander Motin 		} else {
61889b17223SAlexander Motin 			g_raid_change_subdisk_state(sd,
61989b17223SAlexander Motin 			    G_RAID_SUBDISK_S_FAILED);
62089b17223SAlexander Motin 		}
62189b17223SAlexander Motin 		g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
62289b17223SAlexander Motin 		    G_RAID_EVENT_SUBDISK);
62389b17223SAlexander Motin 	}
62489b17223SAlexander Motin 
62589b17223SAlexander Motin 	/* Update status of our need for spare. */
62689b17223SAlexander Motin 	if (mdi->mdio_started) {
62789b17223SAlexander Motin 		mdi->mdio_incomplete =
62889b17223SAlexander Motin 		    (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) <
62989b17223SAlexander Motin 		     mdi->mdio_total_disks);
63089b17223SAlexander Motin 	}
63189b17223SAlexander Motin 
63289b17223SAlexander Motin 	return (resurrection);
63389b17223SAlexander Motin }
63489b17223SAlexander Motin 
63589b17223SAlexander Motin static void
g_disk_md_sii_retaste(void * arg,int pending)63689b17223SAlexander Motin g_disk_md_sii_retaste(void *arg, int pending)
63789b17223SAlexander Motin {
63889b17223SAlexander Motin 
63989b17223SAlexander Motin 	G_RAID_DEBUG(1, "Array is not complete, trying to retaste.");
64089b17223SAlexander Motin 	g_retaste(&g_raid_class);
64189b17223SAlexander Motin 	free(arg, M_MD_SII);
64289b17223SAlexander Motin }
64389b17223SAlexander Motin 
64489b17223SAlexander Motin static void
g_raid_md_sii_refill(struct g_raid_softc * sc)64589b17223SAlexander Motin g_raid_md_sii_refill(struct g_raid_softc *sc)
64689b17223SAlexander Motin {
64789b17223SAlexander Motin 	struct g_raid_md_object *md;
64889b17223SAlexander Motin 	struct g_raid_md_sii_object *mdi;
64989b17223SAlexander Motin 	struct g_raid_disk *disk;
65089b17223SAlexander Motin 	struct task *task;
65189b17223SAlexander Motin 	int update, na;
65289b17223SAlexander Motin 
65389b17223SAlexander Motin 	md = sc->sc_md;
65489b17223SAlexander Motin 	mdi = (struct g_raid_md_sii_object *)md;
65589b17223SAlexander Motin 	update = 0;
65689b17223SAlexander Motin 	do {
65789b17223SAlexander Motin 		/* Make sure we miss anything. */
65889b17223SAlexander Motin 		na = g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE);
65989b17223SAlexander Motin 		if (na == mdi->mdio_total_disks)
66089b17223SAlexander Motin 			break;
66189b17223SAlexander Motin 
66289b17223SAlexander Motin 		G_RAID_DEBUG1(1, md->mdo_softc,
66389b17223SAlexander Motin 		    "Array is not complete (%d of %d), "
66489b17223SAlexander Motin 		    "trying to refill.", na, mdi->mdio_total_disks);
66589b17223SAlexander Motin 
66689b17223SAlexander Motin 		/* Try to get use some of STALE disks. */
66789b17223SAlexander Motin 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
66889b17223SAlexander Motin 			if (disk->d_state == G_RAID_DISK_S_STALE) {
66989b17223SAlexander Motin 				update += g_raid_md_sii_start_disk(disk);
67089b17223SAlexander Motin 				if (disk->d_state == G_RAID_DISK_S_ACTIVE)
67189b17223SAlexander Motin 					break;
67289b17223SAlexander Motin 			}
67389b17223SAlexander Motin 		}
67489b17223SAlexander Motin 		if (disk != NULL)
67589b17223SAlexander Motin 			continue;
67689b17223SAlexander Motin 
67789b17223SAlexander Motin 		/* Try to get use some of SPARE disks. */
67889b17223SAlexander Motin 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
67989b17223SAlexander Motin 			if (disk->d_state == G_RAID_DISK_S_SPARE) {
68089b17223SAlexander Motin 				update += g_raid_md_sii_start_disk(disk);
68189b17223SAlexander Motin 				if (disk->d_state == G_RAID_DISK_S_ACTIVE)
68289b17223SAlexander Motin 					break;
68389b17223SAlexander Motin 			}
68489b17223SAlexander Motin 		}
68589b17223SAlexander Motin 	} while (disk != NULL);
68689b17223SAlexander Motin 
68789b17223SAlexander Motin 	/* Write new metadata if we changed something. */
68814e2cd0aSAlexander Motin 	if (update)
68989b17223SAlexander Motin 		g_raid_md_write_sii(md, NULL, NULL, NULL);
69089b17223SAlexander Motin 
69189b17223SAlexander Motin 	/* Update status of our need for spare. */
69289b17223SAlexander Motin 	mdi->mdio_incomplete = (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) <
69389b17223SAlexander Motin 	    mdi->mdio_total_disks);
69489b17223SAlexander Motin 
69589b17223SAlexander Motin 	/* Request retaste hoping to find spare. */
69689b17223SAlexander Motin 	if (mdi->mdio_incomplete) {
69789b17223SAlexander Motin 		task = malloc(sizeof(struct task),
69889b17223SAlexander Motin 		    M_MD_SII, M_WAITOK | M_ZERO);
69989b17223SAlexander Motin 		TASK_INIT(task, 0, g_disk_md_sii_retaste, task);
70089b17223SAlexander Motin 		taskqueue_enqueue(taskqueue_swi, task);
70189b17223SAlexander Motin 	}
70289b17223SAlexander Motin }
70389b17223SAlexander Motin 
70489b17223SAlexander Motin static void
g_raid_md_sii_start(struct g_raid_softc * sc)70589b17223SAlexander Motin g_raid_md_sii_start(struct g_raid_softc *sc)
70689b17223SAlexander Motin {
70789b17223SAlexander Motin 	struct g_raid_md_object *md;
70889b17223SAlexander Motin 	struct g_raid_md_sii_object *mdi;
70989b17223SAlexander Motin 	struct g_raid_md_sii_perdisk *pd;
71089b17223SAlexander Motin 	struct sii_raid_conf *meta;
71189b17223SAlexander Motin 	struct g_raid_volume *vol;
71289b17223SAlexander Motin 	struct g_raid_subdisk *sd;
71389b17223SAlexander Motin 	struct g_raid_disk *disk, *best;
71489b17223SAlexander Motin 	off_t size;
71589b17223SAlexander Motin 	int j, disk_pos;
71689b17223SAlexander Motin 	uint32_t gendiff, bestgendiff;
71789b17223SAlexander Motin 	char buf[17];
71889b17223SAlexander Motin 
71989b17223SAlexander Motin 	md = sc->sc_md;
72089b17223SAlexander Motin 	mdi = (struct g_raid_md_sii_object *)md;
72189b17223SAlexander Motin 	meta = mdi->mdio_meta;
72289b17223SAlexander Motin 
72389b17223SAlexander Motin 	/* Create volumes and subdisks. */
72489b17223SAlexander Motin 	sii_meta_get_name(meta, buf);
72589b17223SAlexander Motin 	vol = g_raid_create_volume(sc, buf, -1);
72689b17223SAlexander Motin 	vol->v_mediasize = (off_t)meta->total_sectors * 512;
727fc1de960SAlexander Motin 	vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
72889b17223SAlexander Motin 	if (meta->type == SII_T_RAID0) {
72989b17223SAlexander Motin 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID0;
73089b17223SAlexander Motin 		size = vol->v_mediasize / mdi->mdio_total_disks;
73189b17223SAlexander Motin 	} else if (meta->type == SII_T_RAID1) {
73289b17223SAlexander Motin 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID1;
73389b17223SAlexander Motin 		size = vol->v_mediasize;
73489b17223SAlexander Motin 	} else if (meta->type == SII_T_RAID01) {
73589b17223SAlexander Motin 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E;
73689b17223SAlexander Motin 		size = vol->v_mediasize / (mdi->mdio_total_disks / 2);
73789b17223SAlexander Motin 	} else if (meta->type == SII_T_CONCAT) {
73889b17223SAlexander Motin 		if (mdi->mdio_total_disks == 1)
73989b17223SAlexander Motin 			vol->v_raid_level = G_RAID_VOLUME_RL_SINGLE;
74089b17223SAlexander Motin 		else
74189b17223SAlexander Motin 			vol->v_raid_level = G_RAID_VOLUME_RL_CONCAT;
74289b17223SAlexander Motin 		size = 0;
74389b17223SAlexander Motin 	} else if (meta->type == SII_T_RAID5) {
74489b17223SAlexander Motin 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID5;
745fc1de960SAlexander Motin 		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LS;
74689b17223SAlexander Motin 		size = vol->v_mediasize / (mdi->mdio_total_disks - 1);
74789b17223SAlexander Motin 	} else if (meta->type == SII_T_JBOD) {
74889b17223SAlexander Motin 		vol->v_raid_level = G_RAID_VOLUME_RL_SINGLE;
74989b17223SAlexander Motin 		size = 0;
75089b17223SAlexander Motin 	} else {
75189b17223SAlexander Motin 		vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
75289b17223SAlexander Motin 		size = 0;
75389b17223SAlexander Motin 	}
75489b17223SAlexander Motin 	vol->v_strip_size = meta->strip_sectors * 512; //ZZZ
75589b17223SAlexander Motin 	vol->v_disks_count = mdi->mdio_total_disks;
75689b17223SAlexander Motin 	vol->v_sectorsize = 512; //ZZZ
75789b17223SAlexander Motin 	for (j = 0; j < vol->v_disks_count; j++) {
75889b17223SAlexander Motin 		sd = &vol->v_subdisks[j];
75989b17223SAlexander Motin 		sd->sd_offset = 0;
76089b17223SAlexander Motin 		sd->sd_size = size;
76189b17223SAlexander Motin 	}
76289b17223SAlexander Motin 	g_raid_start_volume(vol);
76389b17223SAlexander Motin 
76489b17223SAlexander Motin 	/* Create disk placeholders to store data for later writing. */
76589b17223SAlexander Motin 	for (disk_pos = 0; disk_pos < mdi->mdio_total_disks; disk_pos++) {
76689b17223SAlexander Motin 		pd = malloc(sizeof(*pd), M_MD_SII, M_WAITOK | M_ZERO);
76789b17223SAlexander Motin 		pd->pd_disk_pos = disk_pos;
76889b17223SAlexander Motin 		disk = g_raid_create_disk(sc);
76989b17223SAlexander Motin 		disk->d_md_data = (void *)pd;
77089b17223SAlexander Motin 		disk->d_state = G_RAID_DISK_S_OFFLINE;
77189b17223SAlexander Motin 		sd = &vol->v_subdisks[disk_pos];
77289b17223SAlexander Motin 		sd->sd_disk = disk;
77389b17223SAlexander Motin 		TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
77489b17223SAlexander Motin 	}
77589b17223SAlexander Motin 
77689b17223SAlexander Motin 	/*
77789b17223SAlexander Motin 	 * Make all disks found till the moment take their places
77889b17223SAlexander Motin 	 * in order of their generation numbers.
77989b17223SAlexander Motin 	 */
78089b17223SAlexander Motin 	do {
78189b17223SAlexander Motin 		best = NULL;
78289b17223SAlexander Motin 		bestgendiff = 0xffffffff;
78389b17223SAlexander Motin 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
78489b17223SAlexander Motin 			if (disk->d_state != G_RAID_DISK_S_NONE)
78589b17223SAlexander Motin 				continue;
78689b17223SAlexander Motin 			pd = disk->d_md_data;
78789b17223SAlexander Motin 			if (pd->pd_meta == NULL)
78889b17223SAlexander Motin 				gendiff = 0xfffffffe;
78989b17223SAlexander Motin 			else
79089b17223SAlexander Motin 				gendiff = meta->generation -
79189b17223SAlexander Motin 				    pd->pd_meta->generation;
79289b17223SAlexander Motin 			if (gendiff < bestgendiff) {
79389b17223SAlexander Motin 				best = disk;
79489b17223SAlexander Motin 				bestgendiff = gendiff;
79589b17223SAlexander Motin 			}
79689b17223SAlexander Motin 		}
79789b17223SAlexander Motin 		if (best != NULL)
79889b17223SAlexander Motin 			g_raid_md_sii_start_disk(best);
79989b17223SAlexander Motin 	} while (best != NULL);
80089b17223SAlexander Motin 
80189b17223SAlexander Motin 	mdi->mdio_started = 1;
80289b17223SAlexander Motin 	G_RAID_DEBUG1(0, sc, "Array started.");
80389b17223SAlexander Motin 	g_raid_md_write_sii(md, NULL, NULL, NULL);
80489b17223SAlexander Motin 
80589b17223SAlexander Motin 	/* Pickup any STALE/SPARE disks to refill array if needed. */
80689b17223SAlexander Motin 	g_raid_md_sii_refill(sc);
80789b17223SAlexander Motin 
80889b17223SAlexander Motin 	g_raid_event_send(vol, G_RAID_VOLUME_E_START, G_RAID_EVENT_VOLUME);
80989b17223SAlexander Motin 
81089b17223SAlexander Motin 	callout_stop(&mdi->mdio_start_co);
81189b17223SAlexander Motin 	G_RAID_DEBUG1(1, sc, "root_mount_rel %p", mdi->mdio_rootmount);
81289b17223SAlexander Motin 	root_mount_rel(mdi->mdio_rootmount);
81389b17223SAlexander Motin 	mdi->mdio_rootmount = NULL;
81489b17223SAlexander Motin }
81589b17223SAlexander Motin 
81689b17223SAlexander Motin static void
g_raid_md_sii_new_disk(struct g_raid_disk * disk)81789b17223SAlexander Motin g_raid_md_sii_new_disk(struct g_raid_disk *disk)
81889b17223SAlexander Motin {
81989b17223SAlexander Motin 	struct g_raid_softc *sc;
82089b17223SAlexander Motin 	struct g_raid_md_object *md;
82189b17223SAlexander Motin 	struct g_raid_md_sii_object *mdi;
82289b17223SAlexander Motin 	struct sii_raid_conf *pdmeta;
82389b17223SAlexander Motin 	struct g_raid_md_sii_perdisk *pd;
82489b17223SAlexander Motin 
82589b17223SAlexander Motin 	sc = disk->d_softc;
82689b17223SAlexander Motin 	md = sc->sc_md;
82789b17223SAlexander Motin 	mdi = (struct g_raid_md_sii_object *)md;
82889b17223SAlexander Motin 	pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
82989b17223SAlexander Motin 	pdmeta = pd->pd_meta;
83089b17223SAlexander Motin 
83189b17223SAlexander Motin 	if (mdi->mdio_started) {
83289b17223SAlexander Motin 		if (g_raid_md_sii_start_disk(disk))
83389b17223SAlexander Motin 			g_raid_md_write_sii(md, NULL, NULL, NULL);
83489b17223SAlexander Motin 	} else {
83589b17223SAlexander Motin 		if (mdi->mdio_meta == NULL ||
83689b17223SAlexander Motin 		    ((int32_t)(pdmeta->generation - mdi->mdio_generation)) > 0) {
83789b17223SAlexander Motin 			G_RAID_DEBUG1(1, sc, "Newer disk");
83889b17223SAlexander Motin 			if (mdi->mdio_meta != NULL)
83989b17223SAlexander Motin 				free(mdi->mdio_meta, M_MD_SII);
84089b17223SAlexander Motin 			mdi->mdio_meta = sii_meta_copy(pdmeta);
84189b17223SAlexander Motin 			mdi->mdio_generation = mdi->mdio_meta->generation;
84289b17223SAlexander Motin 			mdi->mdio_total_disks = sii_meta_total_disks(pdmeta);
84389b17223SAlexander Motin 			mdi->mdio_disks_present = 1;
84489b17223SAlexander Motin 		} else if (pdmeta->generation == mdi->mdio_generation) {
84589b17223SAlexander Motin 			mdi->mdio_disks_present++;
84689b17223SAlexander Motin 			G_RAID_DEBUG1(1, sc, "Matching disk (%d of %d up)",
84789b17223SAlexander Motin 			    mdi->mdio_disks_present,
84889b17223SAlexander Motin 			    mdi->mdio_total_disks);
84989b17223SAlexander Motin 		} else {
85089b17223SAlexander Motin 			G_RAID_DEBUG1(1, sc, "Older disk");
85189b17223SAlexander Motin 		}
85289b17223SAlexander Motin 
85389b17223SAlexander Motin 		/* If we collected all needed disks - start array. */
85489b17223SAlexander Motin 		if (mdi->mdio_disks_present == mdi->mdio_total_disks)
85589b17223SAlexander Motin 			g_raid_md_sii_start(sc);
85689b17223SAlexander Motin 	}
85789b17223SAlexander Motin }
85889b17223SAlexander Motin 
85989b17223SAlexander Motin static void
g_raid_sii_go(void * arg)86089b17223SAlexander Motin g_raid_sii_go(void *arg)
86189b17223SAlexander Motin {
86289b17223SAlexander Motin 	struct g_raid_softc *sc;
86389b17223SAlexander Motin 	struct g_raid_md_object *md;
86489b17223SAlexander Motin 	struct g_raid_md_sii_object *mdi;
86589b17223SAlexander Motin 
86689b17223SAlexander Motin 	sc = arg;
86789b17223SAlexander Motin 	md = sc->sc_md;
86889b17223SAlexander Motin 	mdi = (struct g_raid_md_sii_object *)md;
86989b17223SAlexander Motin 	if (!mdi->mdio_started) {
87089b17223SAlexander Motin 		G_RAID_DEBUG1(0, sc, "Force array start due to timeout.");
87189b17223SAlexander Motin 		g_raid_event_send(sc, G_RAID_NODE_E_START, 0);
87289b17223SAlexander Motin 	}
87389b17223SAlexander Motin }
87489b17223SAlexander Motin 
87589b17223SAlexander Motin static int
g_raid_md_create_sii(struct g_raid_md_object * md,struct g_class * mp,struct g_geom ** gp)87689b17223SAlexander Motin g_raid_md_create_sii(struct g_raid_md_object *md, struct g_class *mp,
87789b17223SAlexander Motin     struct g_geom **gp)
87889b17223SAlexander Motin {
87989b17223SAlexander Motin 	struct g_raid_softc *sc;
88089b17223SAlexander Motin 	struct g_raid_md_sii_object *mdi;
88189b17223SAlexander Motin 	char name[32];
88289b17223SAlexander Motin 
88389b17223SAlexander Motin 	mdi = (struct g_raid_md_sii_object *)md;
88489b17223SAlexander Motin 	mdi->mdio_timestamp[5] = arc4random();
88589b17223SAlexander Motin 	mdi->mdio_timestamp[4] = arc4random();
88689b17223SAlexander Motin 	mdi->mdio_timestamp[3] = arc4random();
88789b17223SAlexander Motin 	mdi->mdio_timestamp[2] = arc4random();
88889b17223SAlexander Motin 	mdi->mdio_timestamp[1] = arc4random();
88989b17223SAlexander Motin 	mdi->mdio_timestamp[0] = arc4random();
89089b17223SAlexander Motin 	mdi->mdio_location = arc4random();
89189b17223SAlexander Motin 	mdi->mdio_generation = 0;
89289b17223SAlexander Motin 	snprintf(name, sizeof(name), "SiI-%02x%02x%02x%02x%02x%02x",
89389b17223SAlexander Motin 	    mdi->mdio_timestamp[5], mdi->mdio_timestamp[4],
89489b17223SAlexander Motin 	    mdi->mdio_timestamp[3], mdi->mdio_timestamp[2],
89589b17223SAlexander Motin 	    mdi->mdio_timestamp[1], mdi->mdio_timestamp[0]);
89689b17223SAlexander Motin 	sc = g_raid_create_node(mp, name, md);
89789b17223SAlexander Motin 	if (sc == NULL)
89889b17223SAlexander Motin 		return (G_RAID_MD_TASTE_FAIL);
89989b17223SAlexander Motin 	md->mdo_softc = sc;
90089b17223SAlexander Motin 	*gp = sc->sc_geom;
90189b17223SAlexander Motin 	return (G_RAID_MD_TASTE_NEW);
90289b17223SAlexander Motin }
90389b17223SAlexander Motin 
90489b17223SAlexander Motin static int
g_raid_md_taste_sii(struct g_raid_md_object * md,struct g_class * mp,struct g_consumer * cp,struct g_geom ** gp)90589b17223SAlexander Motin g_raid_md_taste_sii(struct g_raid_md_object *md, struct g_class *mp,
90689b17223SAlexander Motin                               struct g_consumer *cp, struct g_geom **gp)
90789b17223SAlexander Motin {
90889b17223SAlexander Motin 	struct g_consumer *rcp;
90989b17223SAlexander Motin 	struct g_provider *pp;
91089b17223SAlexander Motin 	struct g_raid_md_sii_object *mdi, *mdi1;
91189b17223SAlexander Motin 	struct g_raid_softc *sc;
91289b17223SAlexander Motin 	struct g_raid_disk *disk;
91389b17223SAlexander Motin 	struct sii_raid_conf *meta;
91489b17223SAlexander Motin 	struct g_raid_md_sii_perdisk *pd;
91589b17223SAlexander Motin 	struct g_geom *geom;
916609a7474SAlexander Motin 	int disk_pos, result, spare, len;
91789b17223SAlexander Motin 	char name[32];
91889b17223SAlexander Motin 	uint16_t vendor;
91989b17223SAlexander Motin 
92089b17223SAlexander Motin 	G_RAID_DEBUG(1, "Tasting SiI on %s", cp->provider->name);
92189b17223SAlexander Motin 	mdi = (struct g_raid_md_sii_object *)md;
92289b17223SAlexander Motin 	pp = cp->provider;
92389b17223SAlexander Motin 
924*ad1f936aSRose 	/* Explicitly reject providers with too small sector size */
925*ad1f936aSRose 	if (pp->sectorsize < sizeof(struct sii_raid_conf)) {
926*ad1f936aSRose 		G_RAID_DEBUG(1, "SiI sector size too small on %s: %u < %zu",
927*ad1f936aSRose 		    pp->name, pp->sectorsize, sizeof(struct sii_raid_conf));
928*ad1f936aSRose 		return (G_RAID_MD_TASTE_FAIL);
929*ad1f936aSRose 	}
930*ad1f936aSRose 
93189b17223SAlexander Motin 	/* Read metadata from device. */
93289b17223SAlexander Motin 	meta = NULL;
93389b17223SAlexander Motin 	g_topology_unlock();
9340b1b7c2cSAlexander Motin 	vendor = 0xffff;
9350b1b7c2cSAlexander Motin 	len = sizeof(vendor);
93689b17223SAlexander Motin 	if (pp->geom->rank == 1)
93789b17223SAlexander Motin 		g_io_getattr("GEOM::hba_vendor", cp, &len, &vendor);
93889b17223SAlexander Motin 	meta = sii_meta_read(cp);
93989b17223SAlexander Motin 	g_topology_lock();
94089b17223SAlexander Motin 	if (meta == NULL) {
94189b17223SAlexander Motin 		if (g_raid_aggressive_spare) {
94289b17223SAlexander Motin 			if (vendor == 0x1095) {
94389b17223SAlexander Motin 				G_RAID_DEBUG(1,
94489b17223SAlexander Motin 				    "No SiI metadata, forcing spare.");
94589b17223SAlexander Motin 				spare = 2;
94689b17223SAlexander Motin 				goto search;
94789b17223SAlexander Motin 			} else {
94889b17223SAlexander Motin 				G_RAID_DEBUG(1,
94989b17223SAlexander Motin 				    "SiI vendor mismatch 0x%04x != 0x1095",
95089b17223SAlexander Motin 				    vendor);
95189b17223SAlexander Motin 			}
95289b17223SAlexander Motin 		}
95389b17223SAlexander Motin 		return (G_RAID_MD_TASTE_FAIL);
95489b17223SAlexander Motin 	}
95589b17223SAlexander Motin 
95689b17223SAlexander Motin 	/* Check this disk position in obtained metadata. */
95789b17223SAlexander Motin 	disk_pos = sii_meta_disk_pos(meta, meta);
95889b17223SAlexander Motin 	if (disk_pos == -1) {
95989b17223SAlexander Motin 		G_RAID_DEBUG(1, "SiI disk position not found");
96089b17223SAlexander Motin 		goto fail1;
96189b17223SAlexander Motin 	}
96289b17223SAlexander Motin 
96389b17223SAlexander Motin 	/* Metadata valid. Print it. */
96489b17223SAlexander Motin 	g_raid_md_sii_print(meta);
96589b17223SAlexander Motin 	G_RAID_DEBUG(1, "SiI disk position %d", disk_pos);
96689b17223SAlexander Motin 	spare = (meta->type == SII_T_SPARE) ? 1 : 0;
96789b17223SAlexander Motin 
96889b17223SAlexander Motin search:
96989b17223SAlexander Motin 	/* Search for matching node. */
97089b17223SAlexander Motin 	sc = NULL;
97189b17223SAlexander Motin 	mdi1 = NULL;
97289b17223SAlexander Motin 	LIST_FOREACH(geom, &mp->geom, geom) {
97389b17223SAlexander Motin 		sc = geom->softc;
97489b17223SAlexander Motin 		if (sc == NULL)
97589b17223SAlexander Motin 			continue;
97689b17223SAlexander Motin 		if (sc->sc_stopping != 0)
97789b17223SAlexander Motin 			continue;
97889b17223SAlexander Motin 		if (sc->sc_md->mdo_class != md->mdo_class)
97989b17223SAlexander Motin 			continue;
98089b17223SAlexander Motin 		mdi1 = (struct g_raid_md_sii_object *)sc->sc_md;
98189b17223SAlexander Motin 		if (spare) {
98289b17223SAlexander Motin 			if (mdi1->mdio_incomplete)
98389b17223SAlexander Motin 				break;
98489b17223SAlexander Motin 		} else {
98589b17223SAlexander Motin 			if (mdi1->mdio_location == meta->raid_location &&
98689b17223SAlexander Motin 			    memcmp(&mdi1->mdio_timestamp,
98789b17223SAlexander Motin 			     &meta->timestamp, 6) == 0)
98889b17223SAlexander Motin 				break;
98989b17223SAlexander Motin 		}
99089b17223SAlexander Motin 	}
99189b17223SAlexander Motin 
99289b17223SAlexander Motin 	/* Found matching node. */
99389b17223SAlexander Motin 	if (geom != NULL) {
99489b17223SAlexander Motin 		G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name);
99589b17223SAlexander Motin 		result = G_RAID_MD_TASTE_EXISTING;
99689b17223SAlexander Motin 
99789b17223SAlexander Motin 	} else if (spare) { /* Not found needy node -- left for later. */
99889b17223SAlexander Motin 		G_RAID_DEBUG(1, "Spare is not needed at this time");
99989b17223SAlexander Motin 		goto fail1;
100089b17223SAlexander Motin 
100189b17223SAlexander Motin 	} else { /* Not found matching node -- create one. */
100289b17223SAlexander Motin 		result = G_RAID_MD_TASTE_NEW;
100389b17223SAlexander Motin 		memcpy(&mdi->mdio_timestamp, &meta->timestamp, 6);
100489b17223SAlexander Motin 		mdi->mdio_location = meta->raid_location;
100589b17223SAlexander Motin 		snprintf(name, sizeof(name), "SiI-%02x%02x%02x%02x%02x%02x",
100689b17223SAlexander Motin 		    mdi->mdio_timestamp[5], mdi->mdio_timestamp[4],
100789b17223SAlexander Motin 		    mdi->mdio_timestamp[3], mdi->mdio_timestamp[2],
100889b17223SAlexander Motin 		    mdi->mdio_timestamp[1], mdi->mdio_timestamp[0]);
100989b17223SAlexander Motin 		sc = g_raid_create_node(mp, name, md);
101089b17223SAlexander Motin 		md->mdo_softc = sc;
101189b17223SAlexander Motin 		geom = sc->sc_geom;
101289b17223SAlexander Motin 		callout_init(&mdi->mdio_start_co, 1);
101389b17223SAlexander Motin 		callout_reset(&mdi->mdio_start_co, g_raid_start_timeout * hz,
101489b17223SAlexander Motin 		    g_raid_sii_go, sc);
101589b17223SAlexander Motin 		mdi->mdio_rootmount = root_mount_hold("GRAID-SiI");
101689b17223SAlexander Motin 		G_RAID_DEBUG1(1, sc, "root_mount_hold %p", mdi->mdio_rootmount);
101789b17223SAlexander Motin 	}
101889b17223SAlexander Motin 
1019dea1e226SAlexander Motin 	/* There is no return after this point, so we close passed consumer. */
1020dea1e226SAlexander Motin 	g_access(cp, -1, 0, 0);
1021dea1e226SAlexander Motin 
102289b17223SAlexander Motin 	rcp = g_new_consumer(geom);
102340ea77a0SAlexander Motin 	rcp->flags |= G_CF_DIRECT_RECEIVE;
102489b17223SAlexander Motin 	g_attach(rcp, pp);
102589b17223SAlexander Motin 	if (g_access(rcp, 1, 1, 1) != 0)
102689b17223SAlexander Motin 		; //goto fail1;
102789b17223SAlexander Motin 
102889b17223SAlexander Motin 	g_topology_unlock();
102989b17223SAlexander Motin 	sx_xlock(&sc->sc_lock);
103089b17223SAlexander Motin 
103189b17223SAlexander Motin 	pd = malloc(sizeof(*pd), M_MD_SII, M_WAITOK | M_ZERO);
103289b17223SAlexander Motin 	pd->pd_meta = meta;
103389b17223SAlexander Motin 	if (spare == 2) {
103489b17223SAlexander Motin 		pd->pd_disk_pos = -3;
103589b17223SAlexander Motin 	} else {
103689b17223SAlexander Motin 		pd->pd_disk_pos = -1;
103789b17223SAlexander Motin 	}
103889b17223SAlexander Motin 	pd->pd_disk_size = pp->mediasize;
103989b17223SAlexander Motin 	disk = g_raid_create_disk(sc);
104089b17223SAlexander Motin 	disk->d_md_data = (void *)pd;
104189b17223SAlexander Motin 	disk->d_consumer = rcp;
104289b17223SAlexander Motin 	rcp->private = disk;
104389b17223SAlexander Motin 
1044609a7474SAlexander Motin 	g_raid_get_disk_info(disk);
104589b17223SAlexander Motin 
104689b17223SAlexander Motin 	g_raid_md_sii_new_disk(disk);
104789b17223SAlexander Motin 
104889b17223SAlexander Motin 	sx_xunlock(&sc->sc_lock);
104989b17223SAlexander Motin 	g_topology_lock();
105089b17223SAlexander Motin 	*gp = geom;
105189b17223SAlexander Motin 	return (result);
105289b17223SAlexander Motin fail1:
105389b17223SAlexander Motin 	free(meta, M_MD_SII);
105489b17223SAlexander Motin 	return (G_RAID_MD_TASTE_FAIL);
105589b17223SAlexander Motin }
105689b17223SAlexander Motin 
105789b17223SAlexander Motin static int
g_raid_md_event_sii(struct g_raid_md_object * md,struct g_raid_disk * disk,u_int event)105889b17223SAlexander Motin g_raid_md_event_sii(struct g_raid_md_object *md,
105989b17223SAlexander Motin     struct g_raid_disk *disk, u_int event)
106089b17223SAlexander Motin {
106189b17223SAlexander Motin 	struct g_raid_softc *sc;
106289b17223SAlexander Motin 	struct g_raid_subdisk *sd;
106389b17223SAlexander Motin 	struct g_raid_md_sii_object *mdi;
106489b17223SAlexander Motin 	struct g_raid_md_sii_perdisk *pd;
106589b17223SAlexander Motin 
106689b17223SAlexander Motin 	sc = md->mdo_softc;
106789b17223SAlexander Motin 	mdi = (struct g_raid_md_sii_object *)md;
106889b17223SAlexander Motin 	if (disk == NULL) {
106989b17223SAlexander Motin 		switch (event) {
107089b17223SAlexander Motin 		case G_RAID_NODE_E_START:
107189b17223SAlexander Motin 			if (!mdi->mdio_started)
107289b17223SAlexander Motin 				g_raid_md_sii_start(sc);
107389b17223SAlexander Motin 			return (0);
107489b17223SAlexander Motin 		}
107589b17223SAlexander Motin 		return (-1);
107689b17223SAlexander Motin 	}
107789b17223SAlexander Motin 	pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
107889b17223SAlexander Motin 	switch (event) {
107989b17223SAlexander Motin 	case G_RAID_DISK_E_DISCONNECTED:
108089b17223SAlexander Motin 		/* If disk was assigned, just update statuses. */
108189b17223SAlexander Motin 		if (pd->pd_disk_pos >= 0) {
108289b17223SAlexander Motin 			g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
108389b17223SAlexander Motin 			if (disk->d_consumer) {
108489b17223SAlexander Motin 				g_raid_kill_consumer(sc, disk->d_consumer);
108589b17223SAlexander Motin 				disk->d_consumer = NULL;
108689b17223SAlexander Motin 			}
108789b17223SAlexander Motin 			TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
108889b17223SAlexander Motin 				g_raid_change_subdisk_state(sd,
108989b17223SAlexander Motin 				    G_RAID_SUBDISK_S_NONE);
109089b17223SAlexander Motin 				g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
109189b17223SAlexander Motin 				    G_RAID_EVENT_SUBDISK);
109289b17223SAlexander Motin 			}
109389b17223SAlexander Motin 		} else {
109489b17223SAlexander Motin 			/* Otherwise -- delete. */
109589b17223SAlexander Motin 			g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
109689b17223SAlexander Motin 			g_raid_destroy_disk(disk);
109789b17223SAlexander Motin 		}
109889b17223SAlexander Motin 
109989b17223SAlexander Motin 		/* Write updated metadata to all disks. */
110089b17223SAlexander Motin 		g_raid_md_write_sii(md, NULL, NULL, NULL);
110189b17223SAlexander Motin 
110289b17223SAlexander Motin 		/* Check if anything left except placeholders. */
110389b17223SAlexander Motin 		if (g_raid_ndisks(sc, -1) ==
110489b17223SAlexander Motin 		    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
110589b17223SAlexander Motin 			g_raid_destroy_node(sc, 0);
110689b17223SAlexander Motin 		else
110789b17223SAlexander Motin 			g_raid_md_sii_refill(sc);
110889b17223SAlexander Motin 		return (0);
110989b17223SAlexander Motin 	}
111089b17223SAlexander Motin 	return (-2);
111189b17223SAlexander Motin }
111289b17223SAlexander Motin 
111389b17223SAlexander Motin static int
g_raid_md_ctl_sii(struct g_raid_md_object * md,struct gctl_req * req)111489b17223SAlexander Motin g_raid_md_ctl_sii(struct g_raid_md_object *md,
111589b17223SAlexander Motin     struct gctl_req *req)
111689b17223SAlexander Motin {
111789b17223SAlexander Motin 	struct g_raid_softc *sc;
111889b17223SAlexander Motin 	struct g_raid_volume *vol;
111989b17223SAlexander Motin 	struct g_raid_subdisk *sd;
112089b17223SAlexander Motin 	struct g_raid_disk *disk;
112189b17223SAlexander Motin 	struct g_raid_md_sii_object *mdi;
112289b17223SAlexander Motin 	struct g_raid_md_sii_perdisk *pd;
112389b17223SAlexander Motin 	struct g_consumer *cp;
112489b17223SAlexander Motin 	struct g_provider *pp;
112589b17223SAlexander Motin 	char arg[16];
112689b17223SAlexander Motin 	const char *verb, *volname, *levelname, *diskname;
112789b17223SAlexander Motin 	int *nargs, *force;
112889b17223SAlexander Motin 	off_t size, sectorsize, strip;
112989b17223SAlexander Motin 	intmax_t *sizearg, *striparg;
113089b17223SAlexander Motin 	int numdisks, i, len, level, qual, update;
113189b17223SAlexander Motin 	int error;
113289b17223SAlexander Motin 
113389b17223SAlexander Motin 	sc = md->mdo_softc;
113489b17223SAlexander Motin 	mdi = (struct g_raid_md_sii_object *)md;
113589b17223SAlexander Motin 	verb = gctl_get_param(req, "verb", NULL);
113689b17223SAlexander Motin 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
113789b17223SAlexander Motin 	error = 0;
113889b17223SAlexander Motin 	if (strcmp(verb, "label") == 0) {
113989b17223SAlexander Motin 		if (*nargs < 4) {
114089b17223SAlexander Motin 			gctl_error(req, "Invalid number of arguments.");
114189b17223SAlexander Motin 			return (-1);
114289b17223SAlexander Motin 		}
114389b17223SAlexander Motin 		volname = gctl_get_asciiparam(req, "arg1");
114489b17223SAlexander Motin 		if (volname == NULL) {
114589b17223SAlexander Motin 			gctl_error(req, "No volume name.");
114689b17223SAlexander Motin 			return (-2);
114789b17223SAlexander Motin 		}
114889b17223SAlexander Motin 		levelname = gctl_get_asciiparam(req, "arg2");
114989b17223SAlexander Motin 		if (levelname == NULL) {
115089b17223SAlexander Motin 			gctl_error(req, "No RAID level.");
115189b17223SAlexander Motin 			return (-3);
115289b17223SAlexander Motin 		}
1153fc1de960SAlexander Motin 		if (strcasecmp(levelname, "RAID5") == 0)
11547b2a8d78SAlexander Motin 			levelname = "RAID5-LS";
115589b17223SAlexander Motin 		if (g_raid_volume_str2level(levelname, &level, &qual)) {
115689b17223SAlexander Motin 			gctl_error(req, "Unknown RAID level '%s'.", levelname);
115789b17223SAlexander Motin 			return (-4);
115889b17223SAlexander Motin 		}
115989b17223SAlexander Motin 		numdisks = *nargs - 3;
116089b17223SAlexander Motin 		force = gctl_get_paraml(req, "force", sizeof(*force));
116189b17223SAlexander Motin 		if (!g_raid_md_sii_supported(level, qual, numdisks,
116289b17223SAlexander Motin 		    force ? *force : 0)) {
116389b17223SAlexander Motin 			gctl_error(req, "Unsupported RAID level "
116489b17223SAlexander Motin 			    "(0x%02x/0x%02x), or number of disks (%d).",
116589b17223SAlexander Motin 			    level, qual, numdisks);
116689b17223SAlexander Motin 			return (-5);
116789b17223SAlexander Motin 		}
116889b17223SAlexander Motin 
116989b17223SAlexander Motin 		/* Search for disks, connect them and probe. */
117089b17223SAlexander Motin 		size = 0x7fffffffffffffffllu;
117189b17223SAlexander Motin 		sectorsize = 0;
117289b17223SAlexander Motin 		for (i = 0; i < numdisks; i++) {
117389b17223SAlexander Motin 			snprintf(arg, sizeof(arg), "arg%d", i + 3);
117489b17223SAlexander Motin 			diskname = gctl_get_asciiparam(req, arg);
117589b17223SAlexander Motin 			if (diskname == NULL) {
117689b17223SAlexander Motin 				gctl_error(req, "No disk name (%s).", arg);
117789b17223SAlexander Motin 				error = -6;
117889b17223SAlexander Motin 				break;
117989b17223SAlexander Motin 			}
118089b17223SAlexander Motin 			if (strcmp(diskname, "NONE") == 0) {
118189b17223SAlexander Motin 				cp = NULL;
118289b17223SAlexander Motin 				pp = NULL;
118389b17223SAlexander Motin 			} else {
118489b17223SAlexander Motin 				g_topology_lock();
118589b17223SAlexander Motin 				cp = g_raid_open_consumer(sc, diskname);
118689b17223SAlexander Motin 				if (cp == NULL) {
118789b17223SAlexander Motin 					gctl_error(req, "Can't open '%s'.",
118889b17223SAlexander Motin 					    diskname);
118989b17223SAlexander Motin 					g_topology_unlock();
119089b17223SAlexander Motin 					error = -7;
119189b17223SAlexander Motin 					break;
119289b17223SAlexander Motin 				}
119389b17223SAlexander Motin 				pp = cp->provider;
119489b17223SAlexander Motin 			}
119589b17223SAlexander Motin 			pd = malloc(sizeof(*pd), M_MD_SII, M_WAITOK | M_ZERO);
119689b17223SAlexander Motin 			pd->pd_disk_pos = i;
119789b17223SAlexander Motin 			disk = g_raid_create_disk(sc);
119889b17223SAlexander Motin 			disk->d_md_data = (void *)pd;
119989b17223SAlexander Motin 			disk->d_consumer = cp;
120089b17223SAlexander Motin 			if (cp == NULL)
120189b17223SAlexander Motin 				continue;
120289b17223SAlexander Motin 			cp->private = disk;
120389b17223SAlexander Motin 			g_topology_unlock();
120489b17223SAlexander Motin 
1205609a7474SAlexander Motin 			g_raid_get_disk_info(disk);
120689b17223SAlexander Motin 
120789b17223SAlexander Motin 			pd->pd_disk_size = pp->mediasize;
120889b17223SAlexander Motin 			if (size > pp->mediasize)
120989b17223SAlexander Motin 				size = pp->mediasize;
121089b17223SAlexander Motin 			if (sectorsize < pp->sectorsize)
121189b17223SAlexander Motin 				sectorsize = pp->sectorsize;
121289b17223SAlexander Motin 		}
121389b17223SAlexander Motin 		if (error != 0)
121489b17223SAlexander Motin 			return (error);
121589b17223SAlexander Motin 
121614e2cd0aSAlexander Motin 		if (sectorsize <= 0) {
121714e2cd0aSAlexander Motin 			gctl_error(req, "Can't get sector size.");
121814e2cd0aSAlexander Motin 			return (-8);
121914e2cd0aSAlexander Motin 		}
122014e2cd0aSAlexander Motin 
122189b17223SAlexander Motin 		/* Reserve space for metadata. */
122289b17223SAlexander Motin 		size -= 0x800 * sectorsize;
122389b17223SAlexander Motin 
122489b17223SAlexander Motin 		/* Handle size argument. */
122589b17223SAlexander Motin 		len = sizeof(*sizearg);
122689b17223SAlexander Motin 		sizearg = gctl_get_param(req, "size", &len);
122789b17223SAlexander Motin 		if (sizearg != NULL && len == sizeof(*sizearg) &&
122889b17223SAlexander Motin 		    *sizearg > 0) {
122989b17223SAlexander Motin 			if (*sizearg > size) {
123089b17223SAlexander Motin 				gctl_error(req, "Size too big %lld > %lld.",
123189b17223SAlexander Motin 				    (long long)*sizearg, (long long)size);
123289b17223SAlexander Motin 				return (-9);
123389b17223SAlexander Motin 			}
123489b17223SAlexander Motin 			size = *sizearg;
123589b17223SAlexander Motin 		}
123689b17223SAlexander Motin 
123789b17223SAlexander Motin 		/* Handle strip argument. */
123889b17223SAlexander Motin 		strip = 131072;
123989b17223SAlexander Motin 		len = sizeof(*striparg);
124089b17223SAlexander Motin 		striparg = gctl_get_param(req, "strip", &len);
124189b17223SAlexander Motin 		if (striparg != NULL && len == sizeof(*striparg) &&
124289b17223SAlexander Motin 		    *striparg > 0) {
124389b17223SAlexander Motin 			if (*striparg < sectorsize) {
124489b17223SAlexander Motin 				gctl_error(req, "Strip size too small.");
124589b17223SAlexander Motin 				return (-10);
124689b17223SAlexander Motin 			}
124789b17223SAlexander Motin 			if (*striparg % sectorsize != 0) {
124889b17223SAlexander Motin 				gctl_error(req, "Incorrect strip size.");
124989b17223SAlexander Motin 				return (-11);
125089b17223SAlexander Motin 			}
125189b17223SAlexander Motin 			if (strip > 65535 * sectorsize) {
125289b17223SAlexander Motin 				gctl_error(req, "Strip size too big.");
125389b17223SAlexander Motin 				return (-12);
125489b17223SAlexander Motin 			}
125589b17223SAlexander Motin 			strip = *striparg;
125689b17223SAlexander Motin 		}
125789b17223SAlexander Motin 
125889b17223SAlexander Motin 		/* Round size down to strip or sector. */
125989b17223SAlexander Motin 		if (level == G_RAID_VOLUME_RL_RAID1)
126089b17223SAlexander Motin 			size -= (size % sectorsize);
126189b17223SAlexander Motin 		else if (level == G_RAID_VOLUME_RL_RAID1E &&
126289b17223SAlexander Motin 		    (numdisks & 1) != 0)
126389b17223SAlexander Motin 			size -= (size % (2 * strip));
126489b17223SAlexander Motin 		else
126589b17223SAlexander Motin 			size -= (size % strip);
126689b17223SAlexander Motin 		if (size <= 0) {
126789b17223SAlexander Motin 			gctl_error(req, "Size too small.");
126889b17223SAlexander Motin 			return (-13);
126989b17223SAlexander Motin 		}
127089b17223SAlexander Motin 		if (size > 0xffffffffffffllu * sectorsize) {
127189b17223SAlexander Motin 			gctl_error(req, "Size too big.");
127289b17223SAlexander Motin 			return (-14);
127389b17223SAlexander Motin 		}
127489b17223SAlexander Motin 
127589b17223SAlexander Motin 		/* We have all we need, create things: volume, ... */
127689b17223SAlexander Motin 		mdi->mdio_total_disks = numdisks;
127789b17223SAlexander Motin 		mdi->mdio_started = 1;
127889b17223SAlexander Motin 		vol = g_raid_create_volume(sc, volname, -1);
127989b17223SAlexander Motin 		vol->v_md_data = (void *)(intptr_t)0;
128089b17223SAlexander Motin 		vol->v_raid_level = level;
1281fc1de960SAlexander Motin 		vol->v_raid_level_qualifier = qual;
128289b17223SAlexander Motin 		vol->v_strip_size = strip;
128389b17223SAlexander Motin 		vol->v_disks_count = numdisks;
128489b17223SAlexander Motin 		if (level == G_RAID_VOLUME_RL_RAID0 ||
128589b17223SAlexander Motin 		    level == G_RAID_VOLUME_RL_CONCAT ||
128689b17223SAlexander Motin 		    level == G_RAID_VOLUME_RL_SINGLE)
128789b17223SAlexander Motin 			vol->v_mediasize = size * numdisks;
128889b17223SAlexander Motin 		else if (level == G_RAID_VOLUME_RL_RAID1)
128989b17223SAlexander Motin 			vol->v_mediasize = size;
129089b17223SAlexander Motin 		else if (level == G_RAID_VOLUME_RL_RAID5)
129189b17223SAlexander Motin 			vol->v_mediasize = size * (numdisks - 1);
129289b17223SAlexander Motin 		else { /* RAID1E */
129389b17223SAlexander Motin 			vol->v_mediasize = ((size * numdisks) / strip / 2) *
129489b17223SAlexander Motin 			    strip;
129589b17223SAlexander Motin 		}
129689b17223SAlexander Motin 		vol->v_sectorsize = sectorsize;
129789b17223SAlexander Motin 		g_raid_start_volume(vol);
129889b17223SAlexander Motin 
129989b17223SAlexander Motin 		/* , and subdisks. */
130089b17223SAlexander Motin 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
130189b17223SAlexander Motin 			pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
130289b17223SAlexander Motin 			sd = &vol->v_subdisks[pd->pd_disk_pos];
130389b17223SAlexander Motin 			sd->sd_disk = disk;
130489b17223SAlexander Motin 			sd->sd_offset = 0;
130589b17223SAlexander Motin 			sd->sd_size = size;
130689b17223SAlexander Motin 			TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
130789b17223SAlexander Motin 			if (sd->sd_disk->d_consumer != NULL) {
130889b17223SAlexander Motin 				g_raid_change_disk_state(disk,
130989b17223SAlexander Motin 				    G_RAID_DISK_S_ACTIVE);
131089b17223SAlexander Motin 				g_raid_change_subdisk_state(sd,
131189b17223SAlexander Motin 				    G_RAID_SUBDISK_S_ACTIVE);
131289b17223SAlexander Motin 				g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
131389b17223SAlexander Motin 				    G_RAID_EVENT_SUBDISK);
131489b17223SAlexander Motin 			} else {
131589b17223SAlexander Motin 				g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
131689b17223SAlexander Motin 			}
131789b17223SAlexander Motin 		}
131889b17223SAlexander Motin 
131989b17223SAlexander Motin 		/* Write metadata based on created entities. */
132089b17223SAlexander Motin 		G_RAID_DEBUG1(0, sc, "Array started.");
132189b17223SAlexander Motin 		g_raid_md_write_sii(md, NULL, NULL, NULL);
132289b17223SAlexander Motin 
132389b17223SAlexander Motin 		/* Pickup any STALE/SPARE disks to refill array if needed. */
132489b17223SAlexander Motin 		g_raid_md_sii_refill(sc);
132589b17223SAlexander Motin 
132689b17223SAlexander Motin 		g_raid_event_send(vol, G_RAID_VOLUME_E_START,
132789b17223SAlexander Motin 		    G_RAID_EVENT_VOLUME);
132889b17223SAlexander Motin 		return (0);
132989b17223SAlexander Motin 	}
133089b17223SAlexander Motin 	if (strcmp(verb, "delete") == 0) {
133189b17223SAlexander Motin 		/* Check if some volume is still open. */
133289b17223SAlexander Motin 		force = gctl_get_paraml(req, "force", sizeof(*force));
133389b17223SAlexander Motin 		if (force != NULL && *force == 0 &&
133489b17223SAlexander Motin 		    g_raid_nopens(sc) != 0) {
133589b17223SAlexander Motin 			gctl_error(req, "Some volume is still open.");
133689b17223SAlexander Motin 			return (-4);
133789b17223SAlexander Motin 		}
133889b17223SAlexander Motin 
133989b17223SAlexander Motin 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
134089b17223SAlexander Motin 			if (disk->d_consumer)
134189b17223SAlexander Motin 				sii_meta_erase(disk->d_consumer);
134289b17223SAlexander Motin 		}
134389b17223SAlexander Motin 		g_raid_destroy_node(sc, 0);
134489b17223SAlexander Motin 		return (0);
134589b17223SAlexander Motin 	}
134689b17223SAlexander Motin 	if (strcmp(verb, "remove") == 0 ||
134789b17223SAlexander Motin 	    strcmp(verb, "fail") == 0) {
134889b17223SAlexander Motin 		if (*nargs < 2) {
134989b17223SAlexander Motin 			gctl_error(req, "Invalid number of arguments.");
135089b17223SAlexander Motin 			return (-1);
135189b17223SAlexander Motin 		}
135289b17223SAlexander Motin 		for (i = 1; i < *nargs; i++) {
135389b17223SAlexander Motin 			snprintf(arg, sizeof(arg), "arg%d", i);
135489b17223SAlexander Motin 			diskname = gctl_get_asciiparam(req, arg);
135589b17223SAlexander Motin 			if (diskname == NULL) {
135689b17223SAlexander Motin 				gctl_error(req, "No disk name (%s).", arg);
135789b17223SAlexander Motin 				error = -2;
135889b17223SAlexander Motin 				break;
135989b17223SAlexander Motin 			}
13608510f61aSXin LI 			if (strncmp(diskname, _PATH_DEV, 5) == 0)
136189b17223SAlexander Motin 				diskname += 5;
136289b17223SAlexander Motin 
136389b17223SAlexander Motin 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
136489b17223SAlexander Motin 				if (disk->d_consumer != NULL &&
136589b17223SAlexander Motin 				    disk->d_consumer->provider != NULL &&
136689b17223SAlexander Motin 				    strcmp(disk->d_consumer->provider->name,
136789b17223SAlexander Motin 				     diskname) == 0)
136889b17223SAlexander Motin 					break;
136989b17223SAlexander Motin 			}
137089b17223SAlexander Motin 			if (disk == NULL) {
137189b17223SAlexander Motin 				gctl_error(req, "Disk '%s' not found.",
137289b17223SAlexander Motin 				    diskname);
137389b17223SAlexander Motin 				error = -3;
137489b17223SAlexander Motin 				break;
137589b17223SAlexander Motin 			}
137689b17223SAlexander Motin 
137789b17223SAlexander Motin 			if (strcmp(verb, "fail") == 0) {
137889b17223SAlexander Motin 				g_raid_md_fail_disk_sii(md, NULL, disk);
137989b17223SAlexander Motin 				continue;
138089b17223SAlexander Motin 			}
138189b17223SAlexander Motin 
138289b17223SAlexander Motin 			pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
138389b17223SAlexander Motin 
138489b17223SAlexander Motin 			/* Erase metadata on deleting disk. */
138589b17223SAlexander Motin 			sii_meta_erase(disk->d_consumer);
138689b17223SAlexander Motin 
138789b17223SAlexander Motin 			/* If disk was assigned, just update statuses. */
138889b17223SAlexander Motin 			if (pd->pd_disk_pos >= 0) {
138989b17223SAlexander Motin 				g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
139089b17223SAlexander Motin 				g_raid_kill_consumer(sc, disk->d_consumer);
139189b17223SAlexander Motin 				disk->d_consumer = NULL;
139289b17223SAlexander Motin 				TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
139389b17223SAlexander Motin 					g_raid_change_subdisk_state(sd,
139489b17223SAlexander Motin 					    G_RAID_SUBDISK_S_NONE);
139589b17223SAlexander Motin 					g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
139689b17223SAlexander Motin 					    G_RAID_EVENT_SUBDISK);
139789b17223SAlexander Motin 				}
139889b17223SAlexander Motin 			} else {
139989b17223SAlexander Motin 				/* Otherwise -- delete. */
140089b17223SAlexander Motin 				g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
140189b17223SAlexander Motin 				g_raid_destroy_disk(disk);
140289b17223SAlexander Motin 			}
140389b17223SAlexander Motin 		}
140489b17223SAlexander Motin 
140589b17223SAlexander Motin 		/* Write updated metadata to remaining disks. */
140689b17223SAlexander Motin 		g_raid_md_write_sii(md, NULL, NULL, NULL);
140789b17223SAlexander Motin 
140889b17223SAlexander Motin 		/* Check if anything left except placeholders. */
140989b17223SAlexander Motin 		if (g_raid_ndisks(sc, -1) ==
141089b17223SAlexander Motin 		    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
141189b17223SAlexander Motin 			g_raid_destroy_node(sc, 0);
141289b17223SAlexander Motin 		else
141389b17223SAlexander Motin 			g_raid_md_sii_refill(sc);
141489b17223SAlexander Motin 		return (error);
141589b17223SAlexander Motin 	}
141689b17223SAlexander Motin 	if (strcmp(verb, "insert") == 0) {
141789b17223SAlexander Motin 		if (*nargs < 2) {
141889b17223SAlexander Motin 			gctl_error(req, "Invalid number of arguments.");
141989b17223SAlexander Motin 			return (-1);
142089b17223SAlexander Motin 		}
142189b17223SAlexander Motin 		update = 0;
142289b17223SAlexander Motin 		for (i = 1; i < *nargs; i++) {
142389b17223SAlexander Motin 			/* Get disk name. */
142489b17223SAlexander Motin 			snprintf(arg, sizeof(arg), "arg%d", i);
142589b17223SAlexander Motin 			diskname = gctl_get_asciiparam(req, arg);
142689b17223SAlexander Motin 			if (diskname == NULL) {
142789b17223SAlexander Motin 				gctl_error(req, "No disk name (%s).", arg);
142889b17223SAlexander Motin 				error = -3;
142989b17223SAlexander Motin 				break;
143089b17223SAlexander Motin 			}
143189b17223SAlexander Motin 
143289b17223SAlexander Motin 			/* Try to find provider with specified name. */
143389b17223SAlexander Motin 			g_topology_lock();
143489b17223SAlexander Motin 			cp = g_raid_open_consumer(sc, diskname);
143589b17223SAlexander Motin 			if (cp == NULL) {
143689b17223SAlexander Motin 				gctl_error(req, "Can't open disk '%s'.",
143789b17223SAlexander Motin 				    diskname);
143889b17223SAlexander Motin 				g_topology_unlock();
143989b17223SAlexander Motin 				error = -4;
144089b17223SAlexander Motin 				break;
144189b17223SAlexander Motin 			}
144289b17223SAlexander Motin 			pp = cp->provider;
144389b17223SAlexander Motin 
144489b17223SAlexander Motin 			pd = malloc(sizeof(*pd), M_MD_SII, M_WAITOK | M_ZERO);
144589b17223SAlexander Motin 			pd->pd_disk_pos = -3;
144689b17223SAlexander Motin 			pd->pd_disk_size = pp->mediasize;
144789b17223SAlexander Motin 
144889b17223SAlexander Motin 			disk = g_raid_create_disk(sc);
144989b17223SAlexander Motin 			disk->d_consumer = cp;
145089b17223SAlexander Motin 			disk->d_md_data = (void *)pd;
145189b17223SAlexander Motin 			cp->private = disk;
145289b17223SAlexander Motin 			g_topology_unlock();
145389b17223SAlexander Motin 
1454609a7474SAlexander Motin 			g_raid_get_disk_info(disk);
145589b17223SAlexander Motin 
145689b17223SAlexander Motin 			/* Welcome the "new" disk. */
145789b17223SAlexander Motin 			update += g_raid_md_sii_start_disk(disk);
145889b17223SAlexander Motin 			if (disk->d_state == G_RAID_DISK_S_SPARE) {
145989b17223SAlexander Motin 				sii_meta_write_spare(cp);
146089b17223SAlexander Motin 				g_raid_destroy_disk(disk);
146189b17223SAlexander Motin 			} else if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
146289b17223SAlexander Motin 				gctl_error(req, "Disk '%s' doesn't fit.",
146389b17223SAlexander Motin 				    diskname);
146489b17223SAlexander Motin 				g_raid_destroy_disk(disk);
146589b17223SAlexander Motin 				error = -8;
146689b17223SAlexander Motin 				break;
146789b17223SAlexander Motin 			}
146889b17223SAlexander Motin 		}
146989b17223SAlexander Motin 
147089b17223SAlexander Motin 		/* Write new metadata if we changed something. */
147189b17223SAlexander Motin 		if (update)
147289b17223SAlexander Motin 			g_raid_md_write_sii(md, NULL, NULL, NULL);
147389b17223SAlexander Motin 		return (error);
147489b17223SAlexander Motin 	}
147589b17223SAlexander Motin 	gctl_error(req, "Command '%s' is not supported.", verb);
147689b17223SAlexander Motin 	return (-100);
147789b17223SAlexander Motin }
147889b17223SAlexander Motin 
147989b17223SAlexander Motin static int
g_raid_md_write_sii(struct g_raid_md_object * md,struct g_raid_volume * tvol,struct g_raid_subdisk * tsd,struct g_raid_disk * tdisk)148089b17223SAlexander Motin g_raid_md_write_sii(struct g_raid_md_object *md, struct g_raid_volume *tvol,
148189b17223SAlexander Motin     struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
148289b17223SAlexander Motin {
148389b17223SAlexander Motin 	struct g_raid_softc *sc;
148489b17223SAlexander Motin 	struct g_raid_volume *vol;
148589b17223SAlexander Motin 	struct g_raid_subdisk *sd;
148689b17223SAlexander Motin 	struct g_raid_disk *disk;
148789b17223SAlexander Motin 	struct g_raid_md_sii_object *mdi;
148889b17223SAlexander Motin 	struct g_raid_md_sii_perdisk *pd;
148989b17223SAlexander Motin 	struct sii_raid_conf *meta;
1490b99bce73SPedro F. Giffuni 	u_int i;
149189b17223SAlexander Motin 
149289b17223SAlexander Motin 	sc = md->mdo_softc;
149389b17223SAlexander Motin 	mdi = (struct g_raid_md_sii_object *)md;
149489b17223SAlexander Motin 
149589b17223SAlexander Motin 	if (sc->sc_stopping == G_RAID_DESTROY_HARD)
149689b17223SAlexander Motin 		return (0);
149789b17223SAlexander Motin 
149889b17223SAlexander Motin 	/* Bump generation. Newly written metadata may differ from previous. */
149989b17223SAlexander Motin 	mdi->mdio_generation++;
150089b17223SAlexander Motin 
150189b17223SAlexander Motin 	/* There is only one volume. */
150289b17223SAlexander Motin 	vol = TAILQ_FIRST(&sc->sc_volumes);
150389b17223SAlexander Motin 
150489b17223SAlexander Motin 	/* Fill global fields. */
150589b17223SAlexander Motin 	meta = malloc(sizeof(*meta), M_MD_SII, M_WAITOK | M_ZERO);
150689b17223SAlexander Motin 	if (mdi->mdio_meta)
150789b17223SAlexander Motin 		memcpy(meta, mdi->mdio_meta, sizeof(*meta));
150889b17223SAlexander Motin 	meta->total_sectors = vol->v_mediasize / vol->v_sectorsize;
150989b17223SAlexander Motin 	meta->vendor_id = 0x1095;
151089b17223SAlexander Motin 	meta->version_minor = 0;
151189b17223SAlexander Motin 	meta->version_major = 2;
151289b17223SAlexander Motin 	memcpy(&meta->timestamp, &mdi->mdio_timestamp, 6);
151389b17223SAlexander Motin 	meta->strip_sectors = vol->v_strip_size / vol->v_sectorsize;
151489b17223SAlexander Motin 	if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0) {
151589b17223SAlexander Motin 		meta->type = SII_T_RAID0;
151689b17223SAlexander Motin 		meta->raid0_disks = vol->v_disks_count;
151789b17223SAlexander Motin 		meta->raid1_disks = 0xff;
151889b17223SAlexander Motin 	} else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1) {
151989b17223SAlexander Motin 		meta->type = SII_T_RAID1;
152089b17223SAlexander Motin 		meta->raid0_disks = 0xff;
152189b17223SAlexander Motin 		meta->raid1_disks = vol->v_disks_count;
152289b17223SAlexander Motin 	} else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) {
152389b17223SAlexander Motin 		meta->type = SII_T_RAID01;
152489b17223SAlexander Motin 		meta->raid0_disks = vol->v_disks_count / 2;
152589b17223SAlexander Motin 		meta->raid1_disks = 2;
152689b17223SAlexander Motin 	} else if (vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT ||
152789b17223SAlexander Motin 	    vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE) {
152889b17223SAlexander Motin 		meta->type = SII_T_JBOD;
152989b17223SAlexander Motin 		meta->raid0_disks = vol->v_disks_count;
153089b17223SAlexander Motin 		meta->raid1_disks = 0xff;
153189b17223SAlexander Motin 	} else {
153289b17223SAlexander Motin 		meta->type = SII_T_RAID5;
153389b17223SAlexander Motin 		meta->raid0_disks = vol->v_disks_count;
153489b17223SAlexander Motin 		meta->raid1_disks = 0xff;
153589b17223SAlexander Motin 	}
153689b17223SAlexander Motin 	meta->generation = mdi->mdio_generation;
153789b17223SAlexander Motin 	meta->raid_status = vol->v_dirty ? SII_S_ONLINE : SII_S_AVAILABLE;
153889b17223SAlexander Motin 	for (i = 0; i < vol->v_disks_count; i++) {
153989b17223SAlexander Motin 		sd = &vol->v_subdisks[i];
154089b17223SAlexander Motin 		if (sd->sd_state == G_RAID_SUBDISK_S_STALE ||
154189b17223SAlexander Motin 		    sd->sd_state == G_RAID_SUBDISK_S_RESYNC)
154289b17223SAlexander Motin 			meta->raid_status = SII_S_ONLINE;
154389b17223SAlexander Motin 	}
154489b17223SAlexander Motin 	meta->raid_location = mdi->mdio_location;
154589b17223SAlexander Motin 	sii_meta_put_name(meta, vol->v_name);
154689b17223SAlexander Motin 
154789b17223SAlexander Motin 	/* We are done. Print meta data and store them to disks. */
154889b17223SAlexander Motin 	if (mdi->mdio_meta != NULL)
154989b17223SAlexander Motin 		free(mdi->mdio_meta, M_MD_SII);
155089b17223SAlexander Motin 	mdi->mdio_meta = meta;
155189b17223SAlexander Motin 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
155289b17223SAlexander Motin 		pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
155389b17223SAlexander Motin 		if (disk->d_state != G_RAID_DISK_S_ACTIVE)
155489b17223SAlexander Motin 			continue;
155589b17223SAlexander Motin 		if (pd->pd_meta != NULL) {
155689b17223SAlexander Motin 			free(pd->pd_meta, M_MD_SII);
155789b17223SAlexander Motin 			pd->pd_meta = NULL;
155889b17223SAlexander Motin 		}
155989b17223SAlexander Motin 		pd->pd_meta = sii_meta_copy(meta);
156089b17223SAlexander Motin 		if ((sd = TAILQ_FIRST(&disk->d_subdisks)) != NULL) {
156189b17223SAlexander Motin 			if (sd->sd_state < G_RAID_SUBDISK_S_NEW)
156289b17223SAlexander Motin 				pd->pd_meta->disk_status = SII_S_DROPPED;
156389b17223SAlexander Motin 			else if (sd->sd_state < G_RAID_SUBDISK_S_STALE) {
156489b17223SAlexander Motin 				pd->pd_meta->disk_status = SII_S_REBUILD;
156589b17223SAlexander Motin 				pd->pd_meta->rebuild_lba =
156689b17223SAlexander Motin 				    sd->sd_rebuild_pos / vol->v_sectorsize;
156789b17223SAlexander Motin 			} else
156889b17223SAlexander Motin 				pd->pd_meta->disk_status = SII_S_CURRENT;
156989b17223SAlexander Motin 			if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1) {
157089b17223SAlexander Motin 				pd->pd_meta->disk_number = sd->sd_pos;
157189b17223SAlexander Motin 				pd->pd_meta->raid0_ident = 0xff;
157289b17223SAlexander Motin 				pd->pd_meta->raid1_ident = 0;
157389b17223SAlexander Motin 			} else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E) {
157489b17223SAlexander Motin 				pd->pd_meta->disk_number = sd->sd_pos / meta->raid1_disks;
157589b17223SAlexander Motin 				pd->pd_meta->raid0_ident = sd->sd_pos % meta->raid1_disks;
157689b17223SAlexander Motin 				pd->pd_meta->raid1_ident = sd->sd_pos / meta->raid1_disks;
157789b17223SAlexander Motin 			} else {
157889b17223SAlexander Motin 				pd->pd_meta->disk_number = sd->sd_pos;
157989b17223SAlexander Motin 				pd->pd_meta->raid0_ident = 0;
158089b17223SAlexander Motin 				pd->pd_meta->raid1_ident = 0xff;
158189b17223SAlexander Motin 			}
158289b17223SAlexander Motin 		}
158389b17223SAlexander Motin 		G_RAID_DEBUG(1, "Writing SiI metadata to %s",
158489b17223SAlexander Motin 		    g_raid_get_diskname(disk));
158589b17223SAlexander Motin 		g_raid_md_sii_print(pd->pd_meta);
158689b17223SAlexander Motin 		sii_meta_write(disk->d_consumer, pd->pd_meta);
158789b17223SAlexander Motin 	}
158889b17223SAlexander Motin 	return (0);
158989b17223SAlexander Motin }
159089b17223SAlexander Motin 
159189b17223SAlexander Motin static int
g_raid_md_fail_disk_sii(struct g_raid_md_object * md,struct g_raid_subdisk * tsd,struct g_raid_disk * tdisk)159289b17223SAlexander Motin g_raid_md_fail_disk_sii(struct g_raid_md_object *md,
159389b17223SAlexander Motin     struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
159489b17223SAlexander Motin {
159589b17223SAlexander Motin 	struct g_raid_softc *sc;
159689b17223SAlexander Motin 	struct g_raid_md_sii_perdisk *pd;
159789b17223SAlexander Motin 	struct g_raid_subdisk *sd;
159889b17223SAlexander Motin 
159989b17223SAlexander Motin 	sc = md->mdo_softc;
160089b17223SAlexander Motin 	pd = (struct g_raid_md_sii_perdisk *)tdisk->d_md_data;
160189b17223SAlexander Motin 
160289b17223SAlexander Motin 	/* We can't fail disk that is not a part of array now. */
160389b17223SAlexander Motin 	if (pd->pd_disk_pos < 0)
160489b17223SAlexander Motin 		return (-1);
160589b17223SAlexander Motin 
160689b17223SAlexander Motin 	/*
160789b17223SAlexander Motin 	 * Mark disk as failed in metadata and try to write that metadata
160889b17223SAlexander Motin 	 * to the disk itself to prevent it's later resurrection as STALE.
160989b17223SAlexander Motin 	 */
161089b17223SAlexander Motin 	if (tdisk->d_consumer) {
161189b17223SAlexander Motin 		if (pd->pd_meta) {
161289b17223SAlexander Motin 			pd->pd_meta->disk_status = SII_S_REMOVED;
161389b17223SAlexander Motin 			sii_meta_write(tdisk->d_consumer, pd->pd_meta);
161489b17223SAlexander Motin 		} else
161589b17223SAlexander Motin 			sii_meta_erase(tdisk->d_consumer);
161689b17223SAlexander Motin 	}
161789b17223SAlexander Motin 
161889b17223SAlexander Motin 	/* Change states. */
161989b17223SAlexander Motin 	g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED);
162089b17223SAlexander Motin 	TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) {
162189b17223SAlexander Motin 		g_raid_change_subdisk_state(sd,
162289b17223SAlexander Motin 		    G_RAID_SUBDISK_S_FAILED);
162389b17223SAlexander Motin 		g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED,
162489b17223SAlexander Motin 		    G_RAID_EVENT_SUBDISK);
162589b17223SAlexander Motin 	}
162689b17223SAlexander Motin 
162789b17223SAlexander Motin 	/* Write updated metadata to remaining disks. */
162889b17223SAlexander Motin 	g_raid_md_write_sii(md, NULL, NULL, tdisk);
162989b17223SAlexander Motin 
163089b17223SAlexander Motin 	/* Check if anything left except placeholders. */
163189b17223SAlexander Motin 	if (g_raid_ndisks(sc, -1) ==
163289b17223SAlexander Motin 	    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
163389b17223SAlexander Motin 		g_raid_destroy_node(sc, 0);
163489b17223SAlexander Motin 	else
163589b17223SAlexander Motin 		g_raid_md_sii_refill(sc);
163689b17223SAlexander Motin 	return (0);
163789b17223SAlexander Motin }
163889b17223SAlexander Motin 
163989b17223SAlexander Motin static int
g_raid_md_free_disk_sii(struct g_raid_md_object * md,struct g_raid_disk * disk)164089b17223SAlexander Motin g_raid_md_free_disk_sii(struct g_raid_md_object *md,
164189b17223SAlexander Motin     struct g_raid_disk *disk)
164289b17223SAlexander Motin {
164389b17223SAlexander Motin 	struct g_raid_md_sii_perdisk *pd;
164489b17223SAlexander Motin 
164589b17223SAlexander Motin 	pd = (struct g_raid_md_sii_perdisk *)disk->d_md_data;
164689b17223SAlexander Motin 	if (pd->pd_meta != NULL) {
164789b17223SAlexander Motin 		free(pd->pd_meta, M_MD_SII);
164889b17223SAlexander Motin 		pd->pd_meta = NULL;
164989b17223SAlexander Motin 	}
165089b17223SAlexander Motin 	free(pd, M_MD_SII);
165189b17223SAlexander Motin 	disk->d_md_data = NULL;
165289b17223SAlexander Motin 	return (0);
165389b17223SAlexander Motin }
165489b17223SAlexander Motin 
165589b17223SAlexander Motin static int
g_raid_md_free_sii(struct g_raid_md_object * md)165689b17223SAlexander Motin g_raid_md_free_sii(struct g_raid_md_object *md)
165789b17223SAlexander Motin {
165889b17223SAlexander Motin 	struct g_raid_md_sii_object *mdi;
165989b17223SAlexander Motin 
166089b17223SAlexander Motin 	mdi = (struct g_raid_md_sii_object *)md;
166189b17223SAlexander Motin 	if (!mdi->mdio_started) {
166289b17223SAlexander Motin 		mdi->mdio_started = 0;
166389b17223SAlexander Motin 		callout_stop(&mdi->mdio_start_co);
166489b17223SAlexander Motin 		G_RAID_DEBUG1(1, md->mdo_softc,
166589b17223SAlexander Motin 		    "root_mount_rel %p", mdi->mdio_rootmount);
166689b17223SAlexander Motin 		root_mount_rel(mdi->mdio_rootmount);
166789b17223SAlexander Motin 		mdi->mdio_rootmount = NULL;
166889b17223SAlexander Motin 	}
166989b17223SAlexander Motin 	if (mdi->mdio_meta != NULL) {
167089b17223SAlexander Motin 		free(mdi->mdio_meta, M_MD_SII);
167189b17223SAlexander Motin 		mdi->mdio_meta = NULL;
167289b17223SAlexander Motin 	}
167389b17223SAlexander Motin 	return (0);
167489b17223SAlexander Motin }
167589b17223SAlexander Motin 
1676c89d2fbeSAlexander Motin G_RAID_MD_DECLARE(sii, "SiI");
1677