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