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