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