xref: /freebsd/sys/geom/raid/md_nvidia.c (revision 636076752a85906086731f6e07be9c40b8ff5e77)
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 nvidia_raid_conf *meta;
58889b17223SAlexander Motin 	struct g_raid_disk *disk;
58989b17223SAlexander Motin 	struct task *task;
59089b17223SAlexander Motin 	int update, na;
59189b17223SAlexander Motin 
59289b17223SAlexander Motin 	md = sc->sc_md;
59389b17223SAlexander Motin 	mdi = (struct g_raid_md_nvidia_object *)md;
59489b17223SAlexander Motin 	meta = mdi->mdio_meta;
59589b17223SAlexander Motin 	update = 0;
59689b17223SAlexander Motin 	do {
59789b17223SAlexander Motin 		/* Make sure we miss anything. */
59889b17223SAlexander Motin 		na = g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE);
59989b17223SAlexander Motin 		if (na == mdi->mdio_total_disks)
60089b17223SAlexander Motin 			break;
60189b17223SAlexander Motin 
60289b17223SAlexander Motin 		G_RAID_DEBUG1(1, md->mdo_softc,
60389b17223SAlexander Motin 		    "Array is not complete (%d of %d), "
60489b17223SAlexander Motin 		    "trying to refill.", na, mdi->mdio_total_disks);
60589b17223SAlexander Motin 
60689b17223SAlexander Motin 		/* Try to get use some of STALE disks. */
60789b17223SAlexander Motin 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
60889b17223SAlexander Motin 			if (disk->d_state == G_RAID_DISK_S_STALE) {
60989b17223SAlexander Motin 				update += g_raid_md_nvidia_start_disk(disk);
61089b17223SAlexander Motin 				if (disk->d_state == G_RAID_DISK_S_ACTIVE)
61189b17223SAlexander Motin 					break;
61289b17223SAlexander Motin 			}
61389b17223SAlexander Motin 		}
61489b17223SAlexander Motin 		if (disk != NULL)
61589b17223SAlexander Motin 			continue;
61689b17223SAlexander Motin 
61789b17223SAlexander Motin 		/* Try to get use some of SPARE disks. */
61889b17223SAlexander Motin 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
61989b17223SAlexander Motin 			if (disk->d_state == G_RAID_DISK_S_SPARE) {
62089b17223SAlexander Motin 				update += g_raid_md_nvidia_start_disk(disk);
62189b17223SAlexander Motin 				if (disk->d_state == G_RAID_DISK_S_ACTIVE)
62289b17223SAlexander Motin 					break;
62389b17223SAlexander Motin 			}
62489b17223SAlexander Motin 		}
62589b17223SAlexander Motin 	} while (disk != NULL);
62689b17223SAlexander Motin 
62789b17223SAlexander Motin 	/* Write new metadata if we changed something. */
62889b17223SAlexander Motin 	if (update) {
62989b17223SAlexander Motin 		g_raid_md_write_nvidia(md, NULL, NULL, NULL);
63089b17223SAlexander Motin 		meta = mdi->mdio_meta;
63189b17223SAlexander Motin 	}
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;
68689b17223SAlexander Motin 		size = vol->v_mediasize / (mdi->mdio_total_disks - 1);
68789b17223SAlexander Motin 	} else if (meta->type == NVIDIA_T_RAID5_SYM) {
68889b17223SAlexander Motin 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID5;
68989b17223SAlexander Motin //		vol->v_raid_level_qualifier = 0x03;
69089b17223SAlexander Motin 		size = vol->v_mediasize / (mdi->mdio_total_disks - 1);
69189b17223SAlexander Motin 	} else {
69289b17223SAlexander Motin 		vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
69389b17223SAlexander Motin 		size = 0;
69489b17223SAlexander Motin 	}
69589b17223SAlexander Motin 	vol->v_strip_size = meta->strip_sectors * 512; //ZZZ
69689b17223SAlexander Motin 	vol->v_disks_count = mdi->mdio_total_disks;
69789b17223SAlexander Motin 	vol->v_sectorsize = 512; //ZZZ
69889b17223SAlexander Motin 	for (j = 0; j < vol->v_disks_count; j++) {
69989b17223SAlexander Motin 		sd = &vol->v_subdisks[j];
70089b17223SAlexander Motin 		sd->sd_offset = 0;
70189b17223SAlexander Motin 		sd->sd_size = size;
70289b17223SAlexander Motin 	}
70389b17223SAlexander Motin 	g_raid_start_volume(vol);
70489b17223SAlexander Motin 
70589b17223SAlexander Motin 	/* Create disk placeholders to store data for later writing. */
70689b17223SAlexander Motin 	for (disk_pos = 0; disk_pos < mdi->mdio_total_disks; disk_pos++) {
70789b17223SAlexander Motin 		pd = malloc(sizeof(*pd), M_MD_NVIDIA, M_WAITOK | M_ZERO);
70889b17223SAlexander Motin 		pd->pd_disk_pos = disk_pos;
70989b17223SAlexander Motin 		disk = g_raid_create_disk(sc);
71089b17223SAlexander Motin 		disk->d_md_data = (void *)pd;
71189b17223SAlexander Motin 		disk->d_state = G_RAID_DISK_S_OFFLINE;
71289b17223SAlexander Motin 		sd = &vol->v_subdisks[disk_pos];
71389b17223SAlexander Motin 		sd->sd_disk = disk;
71489b17223SAlexander Motin 		TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
71589b17223SAlexander Motin 	}
71689b17223SAlexander Motin 
71789b17223SAlexander Motin 	/* Make all disks found till the moment take their places. */
71889b17223SAlexander Motin 	do {
71989b17223SAlexander Motin 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
72089b17223SAlexander Motin 			if (disk->d_state == G_RAID_DISK_S_NONE) {
72189b17223SAlexander Motin 				g_raid_md_nvidia_start_disk(disk);
72289b17223SAlexander Motin 				break;
72389b17223SAlexander Motin 			}
72489b17223SAlexander Motin 		}
72589b17223SAlexander Motin 	} while (disk != NULL);
72689b17223SAlexander Motin 
72789b17223SAlexander Motin 	mdi->mdio_started = 1;
72889b17223SAlexander Motin 	G_RAID_DEBUG1(0, sc, "Array started.");
72989b17223SAlexander Motin 	g_raid_md_write_nvidia(md, NULL, NULL, NULL);
73089b17223SAlexander Motin 
73189b17223SAlexander Motin 	/* Pickup any STALE/SPARE disks to refill array if needed. */
73289b17223SAlexander Motin 	g_raid_md_nvidia_refill(sc);
73389b17223SAlexander Motin 
73489b17223SAlexander Motin 	g_raid_event_send(vol, G_RAID_VOLUME_E_START, G_RAID_EVENT_VOLUME);
73589b17223SAlexander Motin 
73689b17223SAlexander Motin 	callout_stop(&mdi->mdio_start_co);
73789b17223SAlexander Motin 	G_RAID_DEBUG1(1, sc, "root_mount_rel %p", mdi->mdio_rootmount);
73889b17223SAlexander Motin 	root_mount_rel(mdi->mdio_rootmount);
73989b17223SAlexander Motin 	mdi->mdio_rootmount = NULL;
74089b17223SAlexander Motin }
74189b17223SAlexander Motin 
74289b17223SAlexander Motin static void
74389b17223SAlexander Motin g_raid_md_nvidia_new_disk(struct g_raid_disk *disk)
74489b17223SAlexander Motin {
74589b17223SAlexander Motin 	struct g_raid_softc *sc;
74689b17223SAlexander Motin 	struct g_raid_md_object *md;
74789b17223SAlexander Motin 	struct g_raid_md_nvidia_object *mdi;
74889b17223SAlexander Motin 	struct nvidia_raid_conf *pdmeta;
74989b17223SAlexander Motin 	struct g_raid_md_nvidia_perdisk *pd;
75089b17223SAlexander Motin 
75189b17223SAlexander Motin 	sc = disk->d_softc;
75289b17223SAlexander Motin 	md = sc->sc_md;
75389b17223SAlexander Motin 	mdi = (struct g_raid_md_nvidia_object *)md;
75489b17223SAlexander Motin 	pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data;
75589b17223SAlexander Motin 	pdmeta = pd->pd_meta;
75689b17223SAlexander Motin 
75789b17223SAlexander Motin 	if (mdi->mdio_started) {
75889b17223SAlexander Motin 		if (g_raid_md_nvidia_start_disk(disk))
75989b17223SAlexander Motin 			g_raid_md_write_nvidia(md, NULL, NULL, NULL);
76089b17223SAlexander Motin 	} else {
76189b17223SAlexander Motin 		if (mdi->mdio_meta == NULL ||
76289b17223SAlexander Motin 		    mdi->mdio_meta->disk_number >= mdi->mdio_meta->total_disks) {
76389b17223SAlexander Motin 			G_RAID_DEBUG1(1, sc, "Newer disk");
76489b17223SAlexander Motin 			if (mdi->mdio_meta != NULL)
76589b17223SAlexander Motin 				free(mdi->mdio_meta, M_MD_NVIDIA);
76689b17223SAlexander Motin 			mdi->mdio_meta = nvidia_meta_copy(pdmeta);
76789b17223SAlexander Motin 			mdi->mdio_total_disks = pdmeta->total_disks;
76889b17223SAlexander Motin 			mdi->mdio_disks_present = 1;
76989b17223SAlexander Motin 		} else if (pdmeta->disk_number < mdi->mdio_meta->total_disks) {
77089b17223SAlexander Motin 			mdi->mdio_disks_present++;
77189b17223SAlexander Motin 			G_RAID_DEBUG1(1, sc, "Matching disk (%d of %d up)",
77289b17223SAlexander Motin 			    mdi->mdio_disks_present,
77389b17223SAlexander Motin 			    mdi->mdio_total_disks);
77489b17223SAlexander Motin 		} else
77589b17223SAlexander Motin 			G_RAID_DEBUG1(1, sc, "Spare disk");
77689b17223SAlexander Motin 
77789b17223SAlexander Motin 		/* If we collected all needed disks - start array. */
77889b17223SAlexander Motin 		if (mdi->mdio_disks_present == mdi->mdio_total_disks)
77989b17223SAlexander Motin 			g_raid_md_nvidia_start(sc);
78089b17223SAlexander Motin 	}
78189b17223SAlexander Motin }
78289b17223SAlexander Motin 
78389b17223SAlexander Motin static void
78489b17223SAlexander Motin g_raid_nvidia_go(void *arg)
78589b17223SAlexander Motin {
78689b17223SAlexander Motin 	struct g_raid_softc *sc;
78789b17223SAlexander Motin 	struct g_raid_md_object *md;
78889b17223SAlexander Motin 	struct g_raid_md_nvidia_object *mdi;
78989b17223SAlexander Motin 
79089b17223SAlexander Motin 	sc = arg;
79189b17223SAlexander Motin 	md = sc->sc_md;
79289b17223SAlexander Motin 	mdi = (struct g_raid_md_nvidia_object *)md;
79389b17223SAlexander Motin 	if (!mdi->mdio_started) {
79489b17223SAlexander Motin 		G_RAID_DEBUG1(0, sc, "Force array start due to timeout.");
79589b17223SAlexander Motin 		g_raid_event_send(sc, G_RAID_NODE_E_START, 0);
79689b17223SAlexander Motin 	}
79789b17223SAlexander Motin }
79889b17223SAlexander Motin 
79989b17223SAlexander Motin static int
80089b17223SAlexander Motin g_raid_md_create_nvidia(struct g_raid_md_object *md, struct g_class *mp,
80189b17223SAlexander Motin     struct g_geom **gp)
80289b17223SAlexander Motin {
80389b17223SAlexander Motin 	struct g_raid_softc *sc;
80489b17223SAlexander Motin 	struct g_raid_md_nvidia_object *mdi;
80589b17223SAlexander Motin 	char name[32];
80689b17223SAlexander Motin 
80789b17223SAlexander Motin 	mdi = (struct g_raid_md_nvidia_object *)md;
80889b17223SAlexander Motin 	arc4rand(&mdi->mdio_volume_id, 16, 0);
80989b17223SAlexander Motin 	snprintf(name, sizeof(name), "NVIDIA-%d",
81089b17223SAlexander Motin 	    atomic_fetchadd_int(&NVIDIANodeID, 1));
81189b17223SAlexander Motin 	sc = g_raid_create_node(mp, name, md);
81289b17223SAlexander Motin 	if (sc == NULL)
81389b17223SAlexander Motin 		return (G_RAID_MD_TASTE_FAIL);
81489b17223SAlexander Motin 	md->mdo_softc = sc;
81589b17223SAlexander Motin 	*gp = sc->sc_geom;
81689b17223SAlexander Motin 	return (G_RAID_MD_TASTE_NEW);
81789b17223SAlexander Motin }
81889b17223SAlexander Motin 
81989b17223SAlexander Motin static int
82089b17223SAlexander Motin g_raid_md_taste_nvidia(struct g_raid_md_object *md, struct g_class *mp,
82189b17223SAlexander Motin                               struct g_consumer *cp, struct g_geom **gp)
82289b17223SAlexander Motin {
82389b17223SAlexander Motin 	struct g_consumer *rcp;
82489b17223SAlexander Motin 	struct g_provider *pp;
82589b17223SAlexander Motin 	struct g_raid_md_nvidia_object *mdi, *mdi1;
82689b17223SAlexander Motin 	struct g_raid_softc *sc;
82789b17223SAlexander Motin 	struct g_raid_disk *disk;
82889b17223SAlexander Motin 	struct nvidia_raid_conf *meta;
82989b17223SAlexander Motin 	struct g_raid_md_nvidia_perdisk *pd;
83089b17223SAlexander Motin 	struct g_geom *geom;
83189b17223SAlexander Motin 	int error, disk_pos, result, spare, len;
83289b17223SAlexander Motin 	char name[32];
83389b17223SAlexander Motin 	uint16_t vendor;
83489b17223SAlexander Motin 
83589b17223SAlexander Motin 	G_RAID_DEBUG(1, "Tasting NVIDIA on %s", cp->provider->name);
83689b17223SAlexander Motin 	mdi = (struct g_raid_md_nvidia_object *)md;
83789b17223SAlexander Motin 	pp = cp->provider;
83889b17223SAlexander Motin 
83989b17223SAlexander Motin 	/* Read metadata from device. */
84089b17223SAlexander Motin 	meta = NULL;
84189b17223SAlexander Motin 	spare = 0;
84289b17223SAlexander Motin 	vendor = 0xffff;
84389b17223SAlexander Motin 	disk_pos = 0;
84489b17223SAlexander Motin 	if (g_access(cp, 1, 0, 0) != 0)
84589b17223SAlexander Motin 		return (G_RAID_MD_TASTE_FAIL);
84689b17223SAlexander Motin 	g_topology_unlock();
84789b17223SAlexander Motin 	len = 2;
84889b17223SAlexander Motin 	if (pp->geom->rank == 1)
84989b17223SAlexander Motin 		g_io_getattr("GEOM::hba_vendor", cp, &len, &vendor);
85089b17223SAlexander Motin 	meta = nvidia_meta_read(cp);
85189b17223SAlexander Motin 	g_topology_lock();
85289b17223SAlexander Motin 	g_access(cp, -1, 0, 0);
85389b17223SAlexander Motin 	if (meta == NULL) {
85489b17223SAlexander Motin 		if (g_raid_aggressive_spare) {
85589b17223SAlexander Motin 			if (vendor == 0x10de) {
85689b17223SAlexander Motin 				G_RAID_DEBUG(1,
85789b17223SAlexander Motin 				    "No NVIDIA metadata, forcing spare.");
85889b17223SAlexander Motin 				spare = 2;
85989b17223SAlexander Motin 				goto search;
86089b17223SAlexander Motin 			} else {
86189b17223SAlexander Motin 				G_RAID_DEBUG(1,
86289b17223SAlexander Motin 				    "NVIDIA vendor mismatch 0x%04x != 0x10de",
86389b17223SAlexander Motin 				    vendor);
86489b17223SAlexander Motin 			}
86589b17223SAlexander Motin 		}
86689b17223SAlexander Motin 		return (G_RAID_MD_TASTE_FAIL);
86789b17223SAlexander Motin 	}
86889b17223SAlexander Motin 
86989b17223SAlexander Motin 	/* Check this disk position in obtained metadata. */
87089b17223SAlexander Motin 	disk_pos = meta->disk_number;
87189b17223SAlexander Motin 	if (disk_pos == -1) {
87289b17223SAlexander Motin 		G_RAID_DEBUG(1, "NVIDIA disk position not found");
87389b17223SAlexander Motin 		goto fail1;
87489b17223SAlexander Motin 	}
87589b17223SAlexander Motin 
87689b17223SAlexander Motin 	/* Metadata valid. Print it. */
87789b17223SAlexander Motin 	g_raid_md_nvidia_print(meta);
87889b17223SAlexander Motin 	G_RAID_DEBUG(1, "NVIDIA disk position %d", disk_pos);
87989b17223SAlexander Motin 	spare = 0;//(meta->type == NVIDIA_T_SPARE) ? 1 : 0;
88089b17223SAlexander Motin 
88189b17223SAlexander Motin search:
88289b17223SAlexander Motin 	/* Search for matching node. */
88389b17223SAlexander Motin 	sc = NULL;
88489b17223SAlexander Motin 	mdi1 = NULL;
88589b17223SAlexander Motin 	LIST_FOREACH(geom, &mp->geom, geom) {
88689b17223SAlexander Motin 		sc = geom->softc;
88789b17223SAlexander Motin 		if (sc == NULL)
88889b17223SAlexander Motin 			continue;
88989b17223SAlexander Motin 		if (sc->sc_stopping != 0)
89089b17223SAlexander Motin 			continue;
89189b17223SAlexander Motin 		if (sc->sc_md->mdo_class != md->mdo_class)
89289b17223SAlexander Motin 			continue;
89389b17223SAlexander Motin 		mdi1 = (struct g_raid_md_nvidia_object *)sc->sc_md;
89489b17223SAlexander Motin 		if (spare) {
89589b17223SAlexander Motin 			if (mdi1->mdio_incomplete)
89689b17223SAlexander Motin 				break;
89789b17223SAlexander Motin 		} else {
89889b17223SAlexander Motin 			if (memcmp(&mdi1->mdio_volume_id,
89989b17223SAlexander Motin 			     &meta->volume_id, 16) == 0)
90089b17223SAlexander Motin 				break;
90189b17223SAlexander Motin 		}
90289b17223SAlexander Motin 	}
90389b17223SAlexander Motin 
90489b17223SAlexander Motin 	/* Found matching node. */
90589b17223SAlexander Motin 	if (geom != NULL) {
90689b17223SAlexander Motin 		G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name);
90789b17223SAlexander Motin 		result = G_RAID_MD_TASTE_EXISTING;
90889b17223SAlexander Motin 
90989b17223SAlexander Motin 	} else if (spare) { /* Not found needy node -- left for later. */
91089b17223SAlexander Motin 		G_RAID_DEBUG(1, "Spare is not needed at this time");
91189b17223SAlexander Motin 		goto fail1;
91289b17223SAlexander Motin 
91389b17223SAlexander Motin 	} else { /* Not found matching node -- create one. */
91489b17223SAlexander Motin 		result = G_RAID_MD_TASTE_NEW;
91589b17223SAlexander Motin 		memcpy(&mdi->mdio_volume_id, &meta->volume_id, 16);
91689b17223SAlexander Motin 		snprintf(name, sizeof(name), "NVIDIA-%d",
91789b17223SAlexander Motin 		    atomic_fetchadd_int(&NVIDIANodeID, 1));
91889b17223SAlexander Motin 		sc = g_raid_create_node(mp, name, md);
91989b17223SAlexander Motin 		md->mdo_softc = sc;
92089b17223SAlexander Motin 		geom = sc->sc_geom;
92189b17223SAlexander Motin 		callout_init(&mdi->mdio_start_co, 1);
92289b17223SAlexander Motin 		callout_reset(&mdi->mdio_start_co, g_raid_start_timeout * hz,
92389b17223SAlexander Motin 		    g_raid_nvidia_go, sc);
92489b17223SAlexander Motin 		mdi->mdio_rootmount = root_mount_hold("GRAID-NVIDIA");
92589b17223SAlexander Motin 		G_RAID_DEBUG1(1, sc, "root_mount_hold %p", mdi->mdio_rootmount);
92689b17223SAlexander Motin 	}
92789b17223SAlexander Motin 
92889b17223SAlexander Motin 	rcp = g_new_consumer(geom);
92989b17223SAlexander Motin 	g_attach(rcp, pp);
93089b17223SAlexander Motin 	if (g_access(rcp, 1, 1, 1) != 0)
93189b17223SAlexander Motin 		; //goto fail1;
93289b17223SAlexander Motin 
93389b17223SAlexander Motin 	g_topology_unlock();
93489b17223SAlexander Motin 	sx_xlock(&sc->sc_lock);
93589b17223SAlexander Motin 
93689b17223SAlexander Motin 	pd = malloc(sizeof(*pd), M_MD_NVIDIA, M_WAITOK | M_ZERO);
93789b17223SAlexander Motin 	pd->pd_meta = meta;
93889b17223SAlexander Motin 	if (spare == 2) {
93989b17223SAlexander Motin 		pd->pd_disk_pos = -3;
94089b17223SAlexander Motin 	} else {
94189b17223SAlexander Motin 		pd->pd_disk_pos = -1;
94289b17223SAlexander Motin 	}
94389b17223SAlexander Motin 	pd->pd_disk_size = pp->mediasize;
94489b17223SAlexander Motin 	disk = g_raid_create_disk(sc);
94589b17223SAlexander Motin 	disk->d_md_data = (void *)pd;
94689b17223SAlexander Motin 	disk->d_consumer = rcp;
94789b17223SAlexander Motin 	rcp->private = disk;
94889b17223SAlexander Motin 
94989b17223SAlexander Motin 	/* Read kernel dumping information. */
95089b17223SAlexander Motin 	disk->d_kd.offset = 0;
95189b17223SAlexander Motin 	disk->d_kd.length = OFF_MAX;
95289b17223SAlexander Motin 	len = sizeof(disk->d_kd);
95389b17223SAlexander Motin 	error = g_io_getattr("GEOM::kerneldump", rcp, &len, &disk->d_kd);
95489b17223SAlexander Motin 	if (disk->d_kd.di.dumper == NULL)
95589b17223SAlexander Motin 		G_RAID_DEBUG1(2, sc, "Dumping not supported by %s: %d.",
95689b17223SAlexander Motin 		    rcp->provider->name, error);
95789b17223SAlexander Motin 
95889b17223SAlexander Motin 	g_raid_md_nvidia_new_disk(disk);
95989b17223SAlexander Motin 
96089b17223SAlexander Motin 	sx_xunlock(&sc->sc_lock);
96189b17223SAlexander Motin 	g_topology_lock();
96289b17223SAlexander Motin 	*gp = geom;
96389b17223SAlexander Motin 	return (result);
96489b17223SAlexander Motin fail1:
96589b17223SAlexander Motin 	free(meta, M_MD_NVIDIA);
96689b17223SAlexander Motin 	return (G_RAID_MD_TASTE_FAIL);
96789b17223SAlexander Motin }
96889b17223SAlexander Motin 
96989b17223SAlexander Motin static int
97089b17223SAlexander Motin g_raid_md_event_nvidia(struct g_raid_md_object *md,
97189b17223SAlexander Motin     struct g_raid_disk *disk, u_int event)
97289b17223SAlexander Motin {
97389b17223SAlexander Motin 	struct g_raid_softc *sc;
97489b17223SAlexander Motin 	struct g_raid_subdisk *sd;
97589b17223SAlexander Motin 	struct g_raid_md_nvidia_object *mdi;
97689b17223SAlexander Motin 	struct g_raid_md_nvidia_perdisk *pd;
97789b17223SAlexander Motin 
97889b17223SAlexander Motin 	sc = md->mdo_softc;
97989b17223SAlexander Motin 	mdi = (struct g_raid_md_nvidia_object *)md;
98089b17223SAlexander Motin 	if (disk == NULL) {
98189b17223SAlexander Motin 		switch (event) {
98289b17223SAlexander Motin 		case G_RAID_NODE_E_START:
98389b17223SAlexander Motin 			if (!mdi->mdio_started) {
98489b17223SAlexander Motin 				/* Bump volume ID to drop missing disks. */
98589b17223SAlexander Motin 				arc4rand(&mdi->mdio_volume_id, 16, 0);
98689b17223SAlexander Motin 				g_raid_md_nvidia_start(sc);
98789b17223SAlexander Motin 			}
98889b17223SAlexander Motin 			return (0);
98989b17223SAlexander Motin 		}
99089b17223SAlexander Motin 		return (-1);
99189b17223SAlexander Motin 	}
99289b17223SAlexander Motin 	pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data;
99389b17223SAlexander Motin 	switch (event) {
99489b17223SAlexander Motin 	case G_RAID_DISK_E_DISCONNECTED:
99589b17223SAlexander Motin 		/* If disk was assigned, just update statuses. */
99689b17223SAlexander Motin 		if (pd->pd_disk_pos >= 0) {
99789b17223SAlexander Motin 			g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
99889b17223SAlexander Motin 			if (disk->d_consumer) {
99989b17223SAlexander Motin 				g_raid_kill_consumer(sc, disk->d_consumer);
100089b17223SAlexander Motin 				disk->d_consumer = NULL;
100189b17223SAlexander Motin 			}
100289b17223SAlexander Motin 			TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
100389b17223SAlexander Motin 				g_raid_change_subdisk_state(sd,
100489b17223SAlexander Motin 				    G_RAID_SUBDISK_S_NONE);
100589b17223SAlexander Motin 				g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
100689b17223SAlexander Motin 				    G_RAID_EVENT_SUBDISK);
100789b17223SAlexander Motin 			}
100889b17223SAlexander Motin 		} else {
100989b17223SAlexander Motin 			/* Otherwise -- delete. */
101089b17223SAlexander Motin 			g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
101189b17223SAlexander Motin 			g_raid_destroy_disk(disk);
101289b17223SAlexander Motin 		}
101389b17223SAlexander Motin 
101489b17223SAlexander Motin 		if (mdi->mdio_started) {
101589b17223SAlexander Motin 			/* Bump volume ID to prevent disk resurrection. */
101689b17223SAlexander Motin 			if (pd->pd_disk_pos >= 0)
101789b17223SAlexander Motin 				arc4rand(&mdi->mdio_volume_id, 16, 0);
101889b17223SAlexander Motin 
101989b17223SAlexander Motin 			/* Write updated metadata to all disks. */
102089b17223SAlexander Motin 			g_raid_md_write_nvidia(md, NULL, NULL, NULL);
102189b17223SAlexander Motin 		}
102289b17223SAlexander Motin 
102389b17223SAlexander Motin 		/* Check if anything left except placeholders. */
102489b17223SAlexander Motin 		if (g_raid_ndisks(sc, -1) ==
102589b17223SAlexander Motin 		    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
102689b17223SAlexander Motin 			g_raid_destroy_node(sc, 0);
102789b17223SAlexander Motin 		else
102889b17223SAlexander Motin 			g_raid_md_nvidia_refill(sc);
102989b17223SAlexander Motin 		return (0);
103089b17223SAlexander Motin 	}
103189b17223SAlexander Motin 	return (-2);
103289b17223SAlexander Motin }
103389b17223SAlexander Motin 
103489b17223SAlexander Motin static int
103589b17223SAlexander Motin g_raid_md_ctl_nvidia(struct g_raid_md_object *md,
103689b17223SAlexander Motin     struct gctl_req *req)
103789b17223SAlexander Motin {
103889b17223SAlexander Motin 	struct g_raid_softc *sc;
103989b17223SAlexander Motin 	struct g_raid_volume *vol;
104089b17223SAlexander Motin 	struct g_raid_subdisk *sd;
104189b17223SAlexander Motin 	struct g_raid_disk *disk;
104289b17223SAlexander Motin 	struct g_raid_md_nvidia_object *mdi;
104389b17223SAlexander Motin 	struct g_raid_md_nvidia_perdisk *pd;
104489b17223SAlexander Motin 	struct g_consumer *cp;
104589b17223SAlexander Motin 	struct g_provider *pp;
104689b17223SAlexander Motin 	char arg[16];
104789b17223SAlexander Motin 	const char *verb, *volname, *levelname, *diskname;
104889b17223SAlexander Motin 	int *nargs, *force;
104989b17223SAlexander Motin 	off_t size, sectorsize, strip;
105089b17223SAlexander Motin 	intmax_t *sizearg, *striparg;
105189b17223SAlexander Motin 	int numdisks, i, len, level, qual, update;
105289b17223SAlexander Motin 	int error;
105389b17223SAlexander Motin 
105489b17223SAlexander Motin 	sc = md->mdo_softc;
105589b17223SAlexander Motin 	mdi = (struct g_raid_md_nvidia_object *)md;
105689b17223SAlexander Motin 	verb = gctl_get_param(req, "verb", NULL);
105789b17223SAlexander Motin 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
105889b17223SAlexander Motin 	error = 0;
105989b17223SAlexander Motin 	if (strcmp(verb, "label") == 0) {
106089b17223SAlexander Motin 
106189b17223SAlexander Motin 		if (*nargs < 4) {
106289b17223SAlexander Motin 			gctl_error(req, "Invalid number of arguments.");
106389b17223SAlexander Motin 			return (-1);
106489b17223SAlexander Motin 		}
106589b17223SAlexander Motin 		volname = gctl_get_asciiparam(req, "arg1");
106689b17223SAlexander Motin 		if (volname == NULL) {
106789b17223SAlexander Motin 			gctl_error(req, "No volume name.");
106889b17223SAlexander Motin 			return (-2);
106989b17223SAlexander Motin 		}
107089b17223SAlexander Motin 		levelname = gctl_get_asciiparam(req, "arg2");
107189b17223SAlexander Motin 		if (levelname == NULL) {
107289b17223SAlexander Motin 			gctl_error(req, "No RAID level.");
107389b17223SAlexander Motin 			return (-3);
107489b17223SAlexander Motin 		}
107589b17223SAlexander Motin 		if (g_raid_volume_str2level(levelname, &level, &qual)) {
107689b17223SAlexander Motin 			gctl_error(req, "Unknown RAID level '%s'.", levelname);
107789b17223SAlexander Motin 			return (-4);
107889b17223SAlexander Motin 		}
107989b17223SAlexander Motin 		numdisks = *nargs - 3;
108089b17223SAlexander Motin 		force = gctl_get_paraml(req, "force", sizeof(*force));
108189b17223SAlexander Motin 		if (!g_raid_md_nvidia_supported(level, qual, numdisks,
108289b17223SAlexander Motin 		    force ? *force : 0)) {
108389b17223SAlexander Motin 			gctl_error(req, "Unsupported RAID level "
108489b17223SAlexander Motin 			    "(0x%02x/0x%02x), or number of disks (%d).",
108589b17223SAlexander Motin 			    level, qual, numdisks);
108689b17223SAlexander Motin 			return (-5);
108789b17223SAlexander Motin 		}
108889b17223SAlexander Motin 
108989b17223SAlexander Motin 		/* Search for disks, connect them and probe. */
109089b17223SAlexander Motin 		size = 0x7fffffffffffffffllu;
109189b17223SAlexander Motin 		sectorsize = 0;
109289b17223SAlexander Motin 		for (i = 0; i < numdisks; i++) {
109389b17223SAlexander Motin 			snprintf(arg, sizeof(arg), "arg%d", i + 3);
109489b17223SAlexander Motin 			diskname = gctl_get_asciiparam(req, arg);
109589b17223SAlexander Motin 			if (diskname == NULL) {
109689b17223SAlexander Motin 				gctl_error(req, "No disk name (%s).", arg);
109789b17223SAlexander Motin 				error = -6;
109889b17223SAlexander Motin 				break;
109989b17223SAlexander Motin 			}
110089b17223SAlexander Motin 			if (strcmp(diskname, "NONE") == 0) {
110189b17223SAlexander Motin 				cp = NULL;
110289b17223SAlexander Motin 				pp = NULL;
110389b17223SAlexander Motin 			} else {
110489b17223SAlexander Motin 				g_topology_lock();
110589b17223SAlexander Motin 				cp = g_raid_open_consumer(sc, diskname);
110689b17223SAlexander Motin 				if (cp == NULL) {
110789b17223SAlexander Motin 					gctl_error(req, "Can't open '%s'.",
110889b17223SAlexander Motin 					    diskname);
110989b17223SAlexander Motin 					g_topology_unlock();
111089b17223SAlexander Motin 					error = -7;
111189b17223SAlexander Motin 					break;
111289b17223SAlexander Motin 				}
111389b17223SAlexander Motin 				pp = cp->provider;
111489b17223SAlexander Motin 			}
111589b17223SAlexander Motin 			pd = malloc(sizeof(*pd), M_MD_NVIDIA, M_WAITOK | M_ZERO);
111689b17223SAlexander Motin 			pd->pd_disk_pos = i;
111789b17223SAlexander Motin 			disk = g_raid_create_disk(sc);
111889b17223SAlexander Motin 			disk->d_md_data = (void *)pd;
111989b17223SAlexander Motin 			disk->d_consumer = cp;
112089b17223SAlexander Motin 			if (cp == NULL)
112189b17223SAlexander Motin 				continue;
112289b17223SAlexander Motin 			cp->private = disk;
112389b17223SAlexander Motin 			g_topology_unlock();
112489b17223SAlexander Motin 
112589b17223SAlexander Motin 			/* Read kernel dumping information. */
112689b17223SAlexander Motin 			disk->d_kd.offset = 0;
112789b17223SAlexander Motin 			disk->d_kd.length = OFF_MAX;
112889b17223SAlexander Motin 			len = sizeof(disk->d_kd);
112989b17223SAlexander Motin 			g_io_getattr("GEOM::kerneldump", cp, &len, &disk->d_kd);
113089b17223SAlexander Motin 			if (disk->d_kd.di.dumper == NULL)
113189b17223SAlexander Motin 				G_RAID_DEBUG1(2, sc,
113289b17223SAlexander Motin 				    "Dumping not supported by %s.",
113389b17223SAlexander Motin 				    cp->provider->name);
113489b17223SAlexander Motin 
113589b17223SAlexander Motin 			pd->pd_disk_size = pp->mediasize;
113689b17223SAlexander Motin 			if (size > pp->mediasize)
113789b17223SAlexander Motin 				size = pp->mediasize;
113889b17223SAlexander Motin 			if (sectorsize < pp->sectorsize)
113989b17223SAlexander Motin 				sectorsize = pp->sectorsize;
114089b17223SAlexander Motin 		}
114189b17223SAlexander Motin 		if (error != 0)
114289b17223SAlexander Motin 			return (error);
114389b17223SAlexander Motin 
114489b17223SAlexander Motin 		/* Reserve space for metadata. */
114589b17223SAlexander Motin 		size -= 2 * sectorsize;
114689b17223SAlexander Motin 
114789b17223SAlexander Motin 		/* Handle size argument. */
114889b17223SAlexander Motin 		len = sizeof(*sizearg);
114989b17223SAlexander Motin 		sizearg = gctl_get_param(req, "size", &len);
115089b17223SAlexander Motin 		if (sizearg != NULL && len == sizeof(*sizearg) &&
115189b17223SAlexander Motin 		    *sizearg > 0) {
115289b17223SAlexander Motin 			if (*sizearg > size) {
115389b17223SAlexander Motin 				gctl_error(req, "Size too big %lld > %lld.",
115489b17223SAlexander Motin 				    (long long)*sizearg, (long long)size);
115589b17223SAlexander Motin 				return (-9);
115689b17223SAlexander Motin 			}
115789b17223SAlexander Motin 			size = *sizearg;
115889b17223SAlexander Motin 		}
115989b17223SAlexander Motin 
116089b17223SAlexander Motin 		/* Handle strip argument. */
116189b17223SAlexander Motin 		strip = 131072;
116289b17223SAlexander Motin 		len = sizeof(*striparg);
116389b17223SAlexander Motin 		striparg = gctl_get_param(req, "strip", &len);
116489b17223SAlexander Motin 		if (striparg != NULL && len == sizeof(*striparg) &&
116589b17223SAlexander Motin 		    *striparg > 0) {
116689b17223SAlexander Motin 			if (*striparg < sectorsize) {
116789b17223SAlexander Motin 				gctl_error(req, "Strip size too small.");
116889b17223SAlexander Motin 				return (-10);
116989b17223SAlexander Motin 			}
117089b17223SAlexander Motin 			if (*striparg % sectorsize != 0) {
117189b17223SAlexander Motin 				gctl_error(req, "Incorrect strip size.");
117289b17223SAlexander Motin 				return (-11);
117389b17223SAlexander Motin 			}
117489b17223SAlexander Motin 			if (strip > 65535 * sectorsize) {
117589b17223SAlexander Motin 				gctl_error(req, "Strip size too big.");
117689b17223SAlexander Motin 				return (-12);
117789b17223SAlexander Motin 			}
117889b17223SAlexander Motin 			strip = *striparg;
117989b17223SAlexander Motin 		}
118089b17223SAlexander Motin 
118189b17223SAlexander Motin 		/* Round size down to strip or sector. */
118289b17223SAlexander Motin 		if (level == G_RAID_VOLUME_RL_RAID1)
118389b17223SAlexander Motin 			size -= (size % sectorsize);
118489b17223SAlexander Motin 		else if (level == G_RAID_VOLUME_RL_RAID1E &&
118589b17223SAlexander Motin 		    (numdisks & 1) != 0)
118689b17223SAlexander Motin 			size -= (size % (2 * strip));
118789b17223SAlexander Motin 		else
118889b17223SAlexander Motin 			size -= (size % strip);
118989b17223SAlexander Motin 		if (size <= 0) {
119089b17223SAlexander Motin 			gctl_error(req, "Size too small.");
119189b17223SAlexander Motin 			return (-13);
119289b17223SAlexander Motin 		}
119389b17223SAlexander Motin 		if (size > 0xffffffffffffllu * sectorsize) {
119489b17223SAlexander Motin 			gctl_error(req, "Size too big.");
119589b17223SAlexander Motin 			return (-14);
119689b17223SAlexander Motin 		}
119789b17223SAlexander Motin 
119889b17223SAlexander Motin 		/* We have all we need, create things: volume, ... */
119989b17223SAlexander Motin 		mdi->mdio_total_disks = numdisks;
120089b17223SAlexander Motin 		mdi->mdio_started = 1;
120189b17223SAlexander Motin 		vol = g_raid_create_volume(sc, volname, -1);
120289b17223SAlexander Motin 		vol->v_md_data = (void *)(intptr_t)0;
120389b17223SAlexander Motin 		vol->v_raid_level = level;
120489b17223SAlexander Motin 		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
120589b17223SAlexander Motin 		vol->v_strip_size = strip;
120689b17223SAlexander Motin 		vol->v_disks_count = numdisks;
120789b17223SAlexander Motin 		if (level == G_RAID_VOLUME_RL_RAID0 ||
120889b17223SAlexander Motin 		    level == G_RAID_VOLUME_RL_CONCAT ||
120989b17223SAlexander Motin 		    level == G_RAID_VOLUME_RL_SINGLE)
121089b17223SAlexander Motin 			vol->v_mediasize = size * numdisks;
121189b17223SAlexander Motin 		else if (level == G_RAID_VOLUME_RL_RAID1)
121289b17223SAlexander Motin 			vol->v_mediasize = size;
121389b17223SAlexander Motin 		else if (level == G_RAID_VOLUME_RL_RAID5)
121489b17223SAlexander Motin 			vol->v_mediasize = size * (numdisks - 1);
121589b17223SAlexander Motin 		else { /* RAID1E */
121689b17223SAlexander Motin 			vol->v_mediasize = ((size * numdisks) / strip / 2) *
121789b17223SAlexander Motin 			    strip;
121889b17223SAlexander Motin 		}
121989b17223SAlexander Motin 		vol->v_sectorsize = sectorsize;
122089b17223SAlexander Motin 		g_raid_start_volume(vol);
122189b17223SAlexander Motin 
122289b17223SAlexander Motin 		/* , and subdisks. */
122389b17223SAlexander Motin 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
122489b17223SAlexander Motin 			pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data;
122589b17223SAlexander Motin 			sd = &vol->v_subdisks[pd->pd_disk_pos];
122689b17223SAlexander Motin 			sd->sd_disk = disk;
122789b17223SAlexander Motin 			sd->sd_offset = 0;
122889b17223SAlexander Motin 			sd->sd_size = size;
122989b17223SAlexander Motin 			TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
123089b17223SAlexander Motin 			if (sd->sd_disk->d_consumer != NULL) {
123189b17223SAlexander Motin 				g_raid_change_disk_state(disk,
123289b17223SAlexander Motin 				    G_RAID_DISK_S_ACTIVE);
123389b17223SAlexander Motin 				g_raid_change_subdisk_state(sd,
123489b17223SAlexander Motin 				    G_RAID_SUBDISK_S_ACTIVE);
123589b17223SAlexander Motin 				g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
123689b17223SAlexander Motin 				    G_RAID_EVENT_SUBDISK);
123789b17223SAlexander Motin 			} else {
123889b17223SAlexander Motin 				g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
123989b17223SAlexander Motin 			}
124089b17223SAlexander Motin 		}
124189b17223SAlexander Motin 
124289b17223SAlexander Motin 		/* Write metadata based on created entities. */
124389b17223SAlexander Motin 		G_RAID_DEBUG1(0, sc, "Array started.");
124489b17223SAlexander Motin 		g_raid_md_write_nvidia(md, NULL, NULL, NULL);
124589b17223SAlexander Motin 
124689b17223SAlexander Motin 		/* Pickup any STALE/SPARE disks to refill array if needed. */
124789b17223SAlexander Motin 		g_raid_md_nvidia_refill(sc);
124889b17223SAlexander Motin 
124989b17223SAlexander Motin 		g_raid_event_send(vol, G_RAID_VOLUME_E_START,
125089b17223SAlexander Motin 		    G_RAID_EVENT_VOLUME);
125189b17223SAlexander Motin 		return (0);
125289b17223SAlexander Motin 	}
125389b17223SAlexander Motin 	if (strcmp(verb, "delete") == 0) {
125489b17223SAlexander Motin 
125589b17223SAlexander Motin 		/* Check if some volume is still open. */
125689b17223SAlexander Motin 		force = gctl_get_paraml(req, "force", sizeof(*force));
125789b17223SAlexander Motin 		if (force != NULL && *force == 0 &&
125889b17223SAlexander Motin 		    g_raid_nopens(sc) != 0) {
125989b17223SAlexander Motin 			gctl_error(req, "Some volume is still open.");
126089b17223SAlexander Motin 			return (-4);
126189b17223SAlexander Motin 		}
126289b17223SAlexander Motin 
126389b17223SAlexander Motin 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
126489b17223SAlexander Motin 			if (disk->d_consumer)
126589b17223SAlexander Motin 				nvidia_meta_erase(disk->d_consumer);
126689b17223SAlexander Motin 		}
126789b17223SAlexander Motin 		g_raid_destroy_node(sc, 0);
126889b17223SAlexander Motin 		return (0);
126989b17223SAlexander Motin 	}
127089b17223SAlexander Motin 	if (strcmp(verb, "remove") == 0 ||
127189b17223SAlexander Motin 	    strcmp(verb, "fail") == 0) {
127289b17223SAlexander Motin 		if (*nargs < 2) {
127389b17223SAlexander Motin 			gctl_error(req, "Invalid number of arguments.");
127489b17223SAlexander Motin 			return (-1);
127589b17223SAlexander Motin 		}
127689b17223SAlexander Motin 		for (i = 1; i < *nargs; i++) {
127789b17223SAlexander Motin 			snprintf(arg, sizeof(arg), "arg%d", i);
127889b17223SAlexander Motin 			diskname = gctl_get_asciiparam(req, arg);
127989b17223SAlexander Motin 			if (diskname == NULL) {
128089b17223SAlexander Motin 				gctl_error(req, "No disk name (%s).", arg);
128189b17223SAlexander Motin 				error = -2;
128289b17223SAlexander Motin 				break;
128389b17223SAlexander Motin 			}
128489b17223SAlexander Motin 			if (strncmp(diskname, "/dev/", 5) == 0)
128589b17223SAlexander Motin 				diskname += 5;
128689b17223SAlexander Motin 
128789b17223SAlexander Motin 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
128889b17223SAlexander Motin 				if (disk->d_consumer != NULL &&
128989b17223SAlexander Motin 				    disk->d_consumer->provider != NULL &&
129089b17223SAlexander Motin 				    strcmp(disk->d_consumer->provider->name,
129189b17223SAlexander Motin 				     diskname) == 0)
129289b17223SAlexander Motin 					break;
129389b17223SAlexander Motin 			}
129489b17223SAlexander Motin 			if (disk == NULL) {
129589b17223SAlexander Motin 				gctl_error(req, "Disk '%s' not found.",
129689b17223SAlexander Motin 				    diskname);
129789b17223SAlexander Motin 				error = -3;
129889b17223SAlexander Motin 				break;
129989b17223SAlexander Motin 			}
130089b17223SAlexander Motin 
130189b17223SAlexander Motin 			if (strcmp(verb, "fail") == 0) {
130289b17223SAlexander Motin 				g_raid_md_fail_disk_nvidia(md, NULL, disk);
130389b17223SAlexander Motin 				continue;
130489b17223SAlexander Motin 			}
130589b17223SAlexander Motin 
130689b17223SAlexander Motin 			pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data;
130789b17223SAlexander Motin 
130889b17223SAlexander Motin 			/* Erase metadata on deleting disk. */
130989b17223SAlexander Motin 			nvidia_meta_erase(disk->d_consumer);
131089b17223SAlexander Motin 
131189b17223SAlexander Motin 			/* If disk was assigned, just update statuses. */
131289b17223SAlexander Motin 			if (pd->pd_disk_pos >= 0) {
131389b17223SAlexander Motin 				g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
131489b17223SAlexander Motin 				g_raid_kill_consumer(sc, disk->d_consumer);
131589b17223SAlexander Motin 				disk->d_consumer = NULL;
131689b17223SAlexander Motin 				TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
131789b17223SAlexander Motin 					g_raid_change_subdisk_state(sd,
131889b17223SAlexander Motin 					    G_RAID_SUBDISK_S_NONE);
131989b17223SAlexander Motin 					g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
132089b17223SAlexander Motin 					    G_RAID_EVENT_SUBDISK);
132189b17223SAlexander Motin 				}
132289b17223SAlexander Motin 			} else {
132389b17223SAlexander Motin 				/* Otherwise -- delete. */
132489b17223SAlexander Motin 				g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
132589b17223SAlexander Motin 				g_raid_destroy_disk(disk);
132689b17223SAlexander Motin 			}
132789b17223SAlexander Motin 		}
132889b17223SAlexander Motin 
132989b17223SAlexander Motin 		/* Write updated metadata to remaining disks. */
133089b17223SAlexander Motin 		g_raid_md_write_nvidia(md, NULL, NULL, NULL);
133189b17223SAlexander Motin 
133289b17223SAlexander Motin 		/* Check if anything left except placeholders. */
133389b17223SAlexander Motin 		if (g_raid_ndisks(sc, -1) ==
133489b17223SAlexander Motin 		    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
133589b17223SAlexander Motin 			g_raid_destroy_node(sc, 0);
133689b17223SAlexander Motin 		else
133789b17223SAlexander Motin 			g_raid_md_nvidia_refill(sc);
133889b17223SAlexander Motin 		return (error);
133989b17223SAlexander Motin 	}
134089b17223SAlexander Motin 	if (strcmp(verb, "insert") == 0) {
134189b17223SAlexander Motin 		if (*nargs < 2) {
134289b17223SAlexander Motin 			gctl_error(req, "Invalid number of arguments.");
134389b17223SAlexander Motin 			return (-1);
134489b17223SAlexander Motin 		}
134589b17223SAlexander Motin 		update = 0;
134689b17223SAlexander Motin 		for (i = 1; i < *nargs; i++) {
134789b17223SAlexander Motin 			/* Get disk name. */
134889b17223SAlexander Motin 			snprintf(arg, sizeof(arg), "arg%d", i);
134989b17223SAlexander Motin 			diskname = gctl_get_asciiparam(req, arg);
135089b17223SAlexander Motin 			if (diskname == NULL) {
135189b17223SAlexander Motin 				gctl_error(req, "No disk name (%s).", arg);
135289b17223SAlexander Motin 				error = -3;
135389b17223SAlexander Motin 				break;
135489b17223SAlexander Motin 			}
135589b17223SAlexander Motin 
135689b17223SAlexander Motin 			/* Try to find provider with specified name. */
135789b17223SAlexander Motin 			g_topology_lock();
135889b17223SAlexander Motin 			cp = g_raid_open_consumer(sc, diskname);
135989b17223SAlexander Motin 			if (cp == NULL) {
136089b17223SAlexander Motin 				gctl_error(req, "Can't open disk '%s'.",
136189b17223SAlexander Motin 				    diskname);
136289b17223SAlexander Motin 				g_topology_unlock();
136389b17223SAlexander Motin 				error = -4;
136489b17223SAlexander Motin 				break;
136589b17223SAlexander Motin 			}
136689b17223SAlexander Motin 			pp = cp->provider;
136789b17223SAlexander Motin 
136889b17223SAlexander Motin 			pd = malloc(sizeof(*pd), M_MD_NVIDIA, M_WAITOK | M_ZERO);
136989b17223SAlexander Motin 			pd->pd_disk_pos = -3;
137089b17223SAlexander Motin 			pd->pd_disk_size = pp->mediasize;
137189b17223SAlexander Motin 
137289b17223SAlexander Motin 			disk = g_raid_create_disk(sc);
137389b17223SAlexander Motin 			disk->d_consumer = cp;
137489b17223SAlexander Motin 			disk->d_consumer->private = disk;
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));
1436*63607675SAlexander 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;
146489b17223SAlexander Motin //	else if (vol->v_raid_level_qualifier == 0)
146589b17223SAlexander 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_object *mdi;
153289b17223SAlexander Motin 	struct g_raid_md_nvidia_perdisk *pd;
153389b17223SAlexander Motin 	struct g_raid_subdisk *sd;
153489b17223SAlexander Motin 
153589b17223SAlexander Motin 	sc = md->mdo_softc;
153689b17223SAlexander Motin 	mdi = (struct g_raid_md_nvidia_object *)md;
153789b17223SAlexander Motin 	pd = (struct g_raid_md_nvidia_perdisk *)tdisk->d_md_data;
153889b17223SAlexander Motin 
153989b17223SAlexander Motin 	/* We can't fail disk that is not a part of array now. */
154089b17223SAlexander Motin 	if (pd->pd_disk_pos < 0)
154189b17223SAlexander Motin 		return (-1);
154289b17223SAlexander Motin 
154389b17223SAlexander Motin 	/* Erase metadata to prevent disks's later resurrection. */
154489b17223SAlexander Motin 	if (tdisk->d_consumer)
154589b17223SAlexander Motin 		nvidia_meta_erase(tdisk->d_consumer);
154689b17223SAlexander Motin 
154789b17223SAlexander Motin 	/* Change states. */
154889b17223SAlexander Motin 	g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED);
154989b17223SAlexander Motin 	TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) {
155089b17223SAlexander Motin 		g_raid_change_subdisk_state(sd,
155189b17223SAlexander Motin 		    G_RAID_SUBDISK_S_FAILED);
155289b17223SAlexander Motin 		g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED,
155389b17223SAlexander Motin 		    G_RAID_EVENT_SUBDISK);
155489b17223SAlexander Motin 	}
155589b17223SAlexander Motin 
155689b17223SAlexander Motin 	/* Write updated metadata to remaining disks. */
155789b17223SAlexander Motin 	g_raid_md_write_nvidia(md, NULL, NULL, tdisk);
155889b17223SAlexander Motin 
155989b17223SAlexander Motin 	/* Check if anything left except placeholders. */
156089b17223SAlexander Motin 	if (g_raid_ndisks(sc, -1) ==
156189b17223SAlexander Motin 	    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
156289b17223SAlexander Motin 		g_raid_destroy_node(sc, 0);
156389b17223SAlexander Motin 	else
156489b17223SAlexander Motin 		g_raid_md_nvidia_refill(sc);
156589b17223SAlexander Motin 	return (0);
156689b17223SAlexander Motin }
156789b17223SAlexander Motin 
156889b17223SAlexander Motin static int
156989b17223SAlexander Motin g_raid_md_free_disk_nvidia(struct g_raid_md_object *md,
157089b17223SAlexander Motin     struct g_raid_disk *disk)
157189b17223SAlexander Motin {
157289b17223SAlexander Motin 	struct g_raid_md_nvidia_perdisk *pd;
157389b17223SAlexander Motin 
157489b17223SAlexander Motin 	pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data;
157589b17223SAlexander Motin 	if (pd->pd_meta != NULL) {
157689b17223SAlexander Motin 		free(pd->pd_meta, M_MD_NVIDIA);
157789b17223SAlexander Motin 		pd->pd_meta = NULL;
157889b17223SAlexander Motin 	}
157989b17223SAlexander Motin 	free(pd, M_MD_NVIDIA);
158089b17223SAlexander Motin 	disk->d_md_data = NULL;
158189b17223SAlexander Motin 	return (0);
158289b17223SAlexander Motin }
158389b17223SAlexander Motin 
158489b17223SAlexander Motin static int
158589b17223SAlexander Motin g_raid_md_free_nvidia(struct g_raid_md_object *md)
158689b17223SAlexander Motin {
158789b17223SAlexander Motin 	struct g_raid_md_nvidia_object *mdi;
158889b17223SAlexander Motin 
158989b17223SAlexander Motin 	mdi = (struct g_raid_md_nvidia_object *)md;
159089b17223SAlexander Motin 	if (!mdi->mdio_started) {
159189b17223SAlexander Motin 		mdi->mdio_started = 0;
159289b17223SAlexander Motin 		callout_stop(&mdi->mdio_start_co);
159389b17223SAlexander Motin 		G_RAID_DEBUG1(1, md->mdo_softc,
159489b17223SAlexander Motin 		    "root_mount_rel %p", mdi->mdio_rootmount);
159589b17223SAlexander Motin 		root_mount_rel(mdi->mdio_rootmount);
159689b17223SAlexander Motin 		mdi->mdio_rootmount = NULL;
159789b17223SAlexander Motin 	}
159889b17223SAlexander Motin 	if (mdi->mdio_meta != NULL) {
159989b17223SAlexander Motin 		free(mdi->mdio_meta, M_MD_NVIDIA);
160089b17223SAlexander Motin 		mdi->mdio_meta = NULL;
160189b17223SAlexander Motin 	}
160289b17223SAlexander Motin 	return (0);
160389b17223SAlexander Motin }
160489b17223SAlexander Motin 
160589b17223SAlexander Motin G_RAID_MD_DECLARE(g_raid_md_nvidia);
1606