xref: /freebsd/sys/geom/raid/md_nvidia.c (revision c243e4902be8df1e643c76b5f18b68bb77cc5268)
1 /*-
2  * Copyright (c) 2011 Alexander Motin <mav@FreeBSD.org>
3  * Copyright (c) 2000 - 2008 Søren Schmidt <sos@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/bio.h>
33 #include <sys/endian.h>
34 #include <sys/kernel.h>
35 #include <sys/kobj.h>
36 #include <sys/limits.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/systm.h>
41 #include <sys/taskqueue.h>
42 #include <geom/geom.h>
43 #include "geom/raid/g_raid.h"
44 #include "g_raid_md_if.h"
45 
46 static MALLOC_DEFINE(M_MD_NVIDIA, "md_nvidia_data", "GEOM_RAID NVIDIA metadata");
47 
48 struct nvidia_raid_conf {
49 	uint8_t		nvidia_id[8];
50 #define NVIDIA_MAGIC                "NVIDIA  "
51 
52 	uint32_t	config_size;
53 	uint32_t	checksum;
54 	uint16_t	version;
55 	uint8_t		disk_number;
56 	uint8_t		dummy_0;
57 	uint32_t	total_sectors;
58 	uint32_t	sector_size;
59 	uint8_t		name[16];
60 	uint8_t		revision[4];
61 	uint32_t	disk_status;
62 
63 	uint32_t	magic_0;
64 #define NVIDIA_MAGIC0		0x00640044
65 
66 	uint64_t	volume_id[2];
67 	uint8_t		state;
68 #define NVIDIA_S_IDLE		0
69 #define NVIDIA_S_INIT		2
70 #define NVIDIA_S_REBUILD	3
71 #define NVIDIA_S_UPGRADE	4
72 #define NVIDIA_S_SYNC		5
73 	uint8_t		array_width;
74 	uint8_t		total_disks;
75 	uint8_t		orig_array_width;
76 	uint16_t	type;
77 #define NVIDIA_T_RAID0		0x0080
78 #define NVIDIA_T_RAID1		0x0081
79 #define NVIDIA_T_RAID3		0x0083
80 #define NVIDIA_T_RAID5		0x0085	/* RLQ = 00/02? */
81 #define NVIDIA_T_RAID5_SYM	0x0095	/* RLQ = 03 */
82 #define NVIDIA_T_RAID10		0x008a
83 #define NVIDIA_T_RAID01		0x8180
84 #define NVIDIA_T_CONCAT		0x00ff
85 
86 	uint16_t	dummy_3;
87 	uint32_t	strip_sectors;
88 	uint32_t	strip_bytes;
89 	uint32_t	strip_shift;
90 	uint32_t	strip_mask;
91 	uint32_t	stripe_sectors;
92 	uint32_t	stripe_bytes;
93 	uint32_t	rebuild_lba;
94 	uint32_t	orig_type;
95 	uint32_t	orig_total_sectors;
96 	uint32_t	status;
97 #define NVIDIA_S_BOOTABLE	0x00000001
98 #define NVIDIA_S_DEGRADED	0x00000002
99 
100 	uint32_t	filler[98];
101 } __packed;
102 
103 struct g_raid_md_nvidia_perdisk {
104 	struct nvidia_raid_conf	*pd_meta;
105 	int			 pd_disk_pos;
106 	off_t			 pd_disk_size;
107 };
108 
109 struct g_raid_md_nvidia_object {
110 	struct g_raid_md_object	 mdio_base;
111 	uint64_t		 mdio_volume_id[2];
112 	struct nvidia_raid_conf	*mdio_meta;
113 	struct callout		 mdio_start_co;	/* STARTING state timer. */
114 	int			 mdio_total_disks;
115 	int			 mdio_disks_present;
116 	int			 mdio_started;
117 	int			 mdio_incomplete;
118 	struct root_hold_token	*mdio_rootmount; /* Root mount delay token. */
119 };
120 
121 static g_raid_md_create_t g_raid_md_create_nvidia;
122 static g_raid_md_taste_t g_raid_md_taste_nvidia;
123 static g_raid_md_event_t g_raid_md_event_nvidia;
124 static g_raid_md_ctl_t g_raid_md_ctl_nvidia;
125 static g_raid_md_write_t g_raid_md_write_nvidia;
126 static g_raid_md_fail_disk_t g_raid_md_fail_disk_nvidia;
127 static g_raid_md_free_disk_t g_raid_md_free_disk_nvidia;
128 static g_raid_md_free_t g_raid_md_free_nvidia;
129 
130 static kobj_method_t g_raid_md_nvidia_methods[] = {
131 	KOBJMETHOD(g_raid_md_create,	g_raid_md_create_nvidia),
132 	KOBJMETHOD(g_raid_md_taste,	g_raid_md_taste_nvidia),
133 	KOBJMETHOD(g_raid_md_event,	g_raid_md_event_nvidia),
134 	KOBJMETHOD(g_raid_md_ctl,	g_raid_md_ctl_nvidia),
135 	KOBJMETHOD(g_raid_md_write,	g_raid_md_write_nvidia),
136 	KOBJMETHOD(g_raid_md_fail_disk,	g_raid_md_fail_disk_nvidia),
137 	KOBJMETHOD(g_raid_md_free_disk,	g_raid_md_free_disk_nvidia),
138 	KOBJMETHOD(g_raid_md_free,	g_raid_md_free_nvidia),
139 	{ 0, 0 }
140 };
141 
142 static struct g_raid_md_class g_raid_md_nvidia_class = {
143 	"NVIDIA",
144 	g_raid_md_nvidia_methods,
145 	sizeof(struct g_raid_md_nvidia_object),
146 	.mdc_priority = 100
147 };
148 
149 static int NVIDIANodeID = 1;
150 
151 static void
152 g_raid_md_nvidia_print(struct nvidia_raid_conf *meta)
153 {
154 
155 	if (g_raid_debug < 1)
156 		return;
157 
158 	printf("********* ATA NVIDIA RAID Metadata *********\n");
159 	printf("nvidia_id           <%.8s>\n", meta->nvidia_id);
160 	printf("config_size         %u\n", meta->config_size);
161 	printf("checksum            0x%08x\n", meta->checksum);
162 	printf("version             0x%04x\n", meta->version);
163 	printf("disk_number         %d\n", meta->disk_number);
164 	printf("dummy_0             0x%02x\n", meta->dummy_0);
165 	printf("total_sectors       %u\n", meta->total_sectors);
166 	printf("sector_size         %u\n", meta->sector_size);
167 	printf("name                <%.16s>\n", meta->name);
168 	printf("revision            0x%02x%02x%02x%02x\n",
169 	    meta->revision[0], meta->revision[1],
170 	    meta->revision[2], meta->revision[3]);
171 	printf("disk_status         0x%08x\n", meta->disk_status);
172 	printf("magic_0             0x%08x\n", meta->magic_0);
173 	printf("volume_id           0x%016jx%016jx\n",
174 	    meta->volume_id[1], meta->volume_id[0]);
175 	printf("state               0x%02x\n", meta->state);
176 	printf("array_width         %u\n", meta->array_width);
177 	printf("total_disks         %u\n", meta->total_disks);
178 	printf("orig_array_width    %u\n", meta->orig_array_width);
179 	printf("type                0x%04x\n", meta->type);
180 	printf("dummy_3             0x%04x\n", meta->dummy_3);
181 	printf("strip_sectors       %u\n", meta->strip_sectors);
182 	printf("strip_bytes         %u\n", meta->strip_bytes);
183 	printf("strip_shift         %u\n", meta->strip_shift);
184 	printf("strip_mask          0x%08x\n", meta->strip_mask);
185 	printf("stripe_sectors      %u\n", meta->stripe_sectors);
186 	printf("stripe_bytes        %u\n", meta->stripe_bytes);
187 	printf("rebuild_lba         %u\n", meta->rebuild_lba);
188 	printf("orig_type           0x%04x\n", meta->orig_type);
189 	printf("orig_total_sectors  %u\n", meta->orig_total_sectors);
190 	printf("status              0x%08x\n", meta->status);
191 	printf("=================================================\n");
192 }
193 
194 static struct nvidia_raid_conf *
195 nvidia_meta_copy(struct nvidia_raid_conf *meta)
196 {
197 	struct nvidia_raid_conf *nmeta;
198 
199 	nmeta = malloc(sizeof(*meta), M_MD_NVIDIA, M_WAITOK);
200 	memcpy(nmeta, meta, sizeof(*meta));
201 	return (nmeta);
202 }
203 
204 static int
205 nvidia_meta_translate_disk(struct nvidia_raid_conf *meta, int md_disk_pos)
206 {
207 	int disk_pos;
208 
209 	if (md_disk_pos >= 0 && meta->type == NVIDIA_T_RAID01) {
210 		disk_pos = (md_disk_pos / meta->array_width) +
211 		    (md_disk_pos % meta->array_width) * meta->array_width;
212 	} else
213 		disk_pos = md_disk_pos;
214 	return (disk_pos);
215 }
216 
217 static void
218 nvidia_meta_get_name(struct nvidia_raid_conf *meta, char *buf)
219 {
220 	int i;
221 
222 	strncpy(buf, meta->name, 16);
223 	buf[16] = 0;
224 	for (i = 15; i >= 0; i--) {
225 		if (buf[i] > 0x20)
226 			break;
227 		buf[i] = 0;
228 	}
229 }
230 
231 static void
232 nvidia_meta_put_name(struct nvidia_raid_conf *meta, char *buf)
233 {
234 
235 	memset(meta->name, 0x20, 16);
236 	memcpy(meta->name, buf, MIN(strlen(buf), 16));
237 }
238 
239 static struct nvidia_raid_conf *
240 nvidia_meta_read(struct g_consumer *cp)
241 {
242 	struct g_provider *pp;
243 	struct nvidia_raid_conf *meta;
244 	char *buf;
245 	int error, i;
246 	uint32_t checksum, *ptr;
247 
248 	pp = cp->provider;
249 
250 	/* Read the anchor sector. */
251 	buf = g_read_data(cp,
252 	    pp->mediasize - 2 * pp->sectorsize, pp->sectorsize, &error);
253 	if (buf == NULL) {
254 		G_RAID_DEBUG(1, "Cannot read metadata from %s (error=%d).",
255 		    pp->name, error);
256 		return (NULL);
257 	}
258 	meta = malloc(sizeof(*meta), M_MD_NVIDIA, M_WAITOK);
259 	memcpy(meta, buf, min(sizeof(*meta), pp->sectorsize));
260 	g_free(buf);
261 
262 	/* Check if this is an NVIDIA RAID struct */
263 	if (strncmp(meta->nvidia_id, NVIDIA_MAGIC, strlen(NVIDIA_MAGIC))) {
264 		G_RAID_DEBUG(1, "NVIDIA signature check failed on %s", pp->name);
265 		free(meta, M_MD_NVIDIA);
266 		return (NULL);
267 	}
268 	if (meta->config_size > 128 ||
269 	    meta->config_size < 30) {
270 		G_RAID_DEBUG(1, "NVIDIA metadata size looks wrong: %d",
271 		    meta->config_size);
272 		free(meta, M_MD_NVIDIA);
273 		return (NULL);
274 	}
275 
276 	/* Check metadata checksum. */
277 	for (checksum = 0, ptr = (uint32_t *)meta,
278 	    i = 0; i < meta->config_size; i++)
279 		checksum += *ptr++;
280 	if (checksum != 0) {
281 		G_RAID_DEBUG(1, "NVIDIA checksum check failed on %s", pp->name);
282 		free(meta, M_MD_NVIDIA);
283 		return (NULL);
284 	}
285 
286 	/* Check volume state. */
287 	if (meta->state != NVIDIA_S_IDLE && meta->state != NVIDIA_S_INIT &&
288 	    meta->state != NVIDIA_S_REBUILD && meta->state != NVIDIA_S_SYNC) {
289 		G_RAID_DEBUG(1, "NVIDIA unknown state on %s (0x%02x)",
290 		    pp->name, meta->state);
291 		free(meta, M_MD_NVIDIA);
292 		return (NULL);
293 	}
294 
295 	/* Check raid type. */
296 	if (meta->type != NVIDIA_T_RAID0 && meta->type != NVIDIA_T_RAID1 &&
297 	    meta->type != NVIDIA_T_RAID3 && meta->type != NVIDIA_T_RAID5 &&
298 	    meta->type != NVIDIA_T_RAID5_SYM &&
299 	    meta->type != NVIDIA_T_RAID01 && meta->type != NVIDIA_T_CONCAT) {
300 		G_RAID_DEBUG(1, "NVIDIA unknown RAID level on %s (0x%02x)",
301 		    pp->name, meta->type);
302 		free(meta, M_MD_NVIDIA);
303 		return (NULL);
304 	}
305 
306 	return (meta);
307 }
308 
309 static int
310 nvidia_meta_write(struct g_consumer *cp, struct nvidia_raid_conf *meta)
311 {
312 	struct g_provider *pp;
313 	char *buf;
314 	int error, i;
315 	uint32_t checksum, *ptr;
316 
317 	pp = cp->provider;
318 
319 	/* Recalculate checksum for case if metadata were changed. */
320 	meta->checksum = 0;
321 	for (checksum = 0, ptr = (uint32_t *)meta,
322 	    i = 0; i < meta->config_size; i++)
323 		checksum += *ptr++;
324 	meta->checksum -= checksum;
325 
326 	/* Create and fill buffer. */
327 	buf = malloc(pp->sectorsize, M_MD_NVIDIA, M_WAITOK | M_ZERO);
328 	memcpy(buf, meta, sizeof(*meta));
329 
330 	/* Write metadata. */
331 	error = g_write_data(cp,
332 	    pp->mediasize - 2 * pp->sectorsize, buf, pp->sectorsize);
333 	if (error != 0) {
334 		G_RAID_DEBUG(1, "Cannot write metadata to %s (error=%d).",
335 		    pp->name, error);
336 	}
337 
338 	free(buf, M_MD_NVIDIA);
339 	return (error);
340 }
341 
342 static int
343 nvidia_meta_erase(struct g_consumer *cp)
344 {
345 	struct g_provider *pp;
346 	char *buf;
347 	int error;
348 
349 	pp = cp->provider;
350 	buf = malloc(pp->sectorsize, M_MD_NVIDIA, M_WAITOK | M_ZERO);
351 	error = g_write_data(cp,
352 	    pp->mediasize - 2 * pp->sectorsize, buf, pp->sectorsize);
353 	if (error != 0) {
354 		G_RAID_DEBUG(1, "Cannot erase metadata on %s (error=%d).",
355 		    pp->name, error);
356 	}
357 	free(buf, M_MD_NVIDIA);
358 	return (error);
359 }
360 
361 static struct g_raid_disk *
362 g_raid_md_nvidia_get_disk(struct g_raid_softc *sc, int id)
363 {
364 	struct g_raid_disk	*disk;
365 	struct g_raid_md_nvidia_perdisk *pd;
366 
367 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
368 		pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data;
369 		if (pd->pd_disk_pos == id)
370 			break;
371 	}
372 	return (disk);
373 }
374 
375 static int
376 g_raid_md_nvidia_supported(int level, int qual, int disks, int force)
377 {
378 
379 	switch (level) {
380 	case G_RAID_VOLUME_RL_RAID0:
381 		if (disks < 1)
382 			return (0);
383 		if (!force && (disks < 2 || disks > 6))
384 			return (0);
385 		break;
386 	case G_RAID_VOLUME_RL_RAID1:
387 		if (disks < 1)
388 			return (0);
389 		if (!force && (disks != 2))
390 			return (0);
391 		break;
392 	case G_RAID_VOLUME_RL_RAID1E:
393 		if (disks < 2)
394 			return (0);
395 		if (disks % 2 != 0)
396 			return (0);
397 		if (!force && (disks < 4))
398 			return (0);
399 		break;
400 	case G_RAID_VOLUME_RL_SINGLE:
401 		if (disks != 1)
402 			return (0);
403 		break;
404 	case G_RAID_VOLUME_RL_CONCAT:
405 		if (disks < 2)
406 			return (0);
407 		break;
408 	case G_RAID_VOLUME_RL_RAID5:
409 		if (disks < 3)
410 			return (0);
411 		if (qual != G_RAID_VOLUME_RLQ_R5LA &&
412 		    qual != G_RAID_VOLUME_RLQ_R5LS)
413 			return (0);
414 		break;
415 	default:
416 		return (0);
417 	}
418 	if (level != G_RAID_VOLUME_RL_RAID5 && qual != G_RAID_VOLUME_RLQ_NONE)
419 		return (0);
420 	return (1);
421 }
422 
423 static int
424 g_raid_md_nvidia_start_disk(struct g_raid_disk *disk)
425 {
426 	struct g_raid_softc *sc;
427 	struct g_raid_subdisk *sd, *tmpsd;
428 	struct g_raid_disk *olddisk, *tmpdisk;
429 	struct g_raid_md_object *md;
430 	struct g_raid_md_nvidia_object *mdi;
431 	struct g_raid_md_nvidia_perdisk *pd, *oldpd;
432 	struct nvidia_raid_conf *meta;
433 	int disk_pos, resurrection = 0;
434 
435 	sc = disk->d_softc;
436 	md = sc->sc_md;
437 	mdi = (struct g_raid_md_nvidia_object *)md;
438 	meta = mdi->mdio_meta;
439 	pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data;
440 	olddisk = NULL;
441 
442 	/* Find disk position in metadata by it's serial. */
443 	if (pd->pd_meta != NULL) {
444 		disk_pos = pd->pd_meta->disk_number;
445 		if (disk_pos >= meta->total_disks || mdi->mdio_started)
446 			disk_pos = -3;
447 	} else
448 		disk_pos = -3;
449 	/* For RAID0+1 we need to translate order. */
450 	disk_pos = nvidia_meta_translate_disk(meta, disk_pos);
451 	if (disk_pos < 0) {
452 		G_RAID_DEBUG1(1, sc, "Unknown, probably new or stale disk");
453 		/* If we are in the start process, that's all for now. */
454 		if (!mdi->mdio_started)
455 			goto nofit;
456 		/*
457 		 * If we have already started - try to get use of the disk.
458 		 * Try to replace OFFLINE disks first, then FAILED.
459 		 */
460 		TAILQ_FOREACH(tmpdisk, &sc->sc_disks, d_next) {
461 			if (tmpdisk->d_state != G_RAID_DISK_S_OFFLINE &&
462 			    tmpdisk->d_state != G_RAID_DISK_S_FAILED)
463 				continue;
464 			/* Make sure this disk is big enough. */
465 			TAILQ_FOREACH(sd, &tmpdisk->d_subdisks, sd_next) {
466 				if (sd->sd_offset + sd->sd_size + 2 * 512 >
467 				    pd->pd_disk_size) {
468 					G_RAID_DEBUG1(1, sc,
469 					    "Disk too small (%ju < %ju)",
470 					    pd->pd_disk_size,
471 					    sd->sd_offset + sd->sd_size + 512);
472 					break;
473 				}
474 			}
475 			if (sd != NULL)
476 				continue;
477 			if (tmpdisk->d_state == G_RAID_DISK_S_OFFLINE) {
478 				olddisk = tmpdisk;
479 				break;
480 			} else if (olddisk == NULL)
481 				olddisk = tmpdisk;
482 		}
483 		if (olddisk == NULL) {
484 nofit:
485 			g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE);
486 			return (1);
487 		}
488 		oldpd = (struct g_raid_md_nvidia_perdisk *)olddisk->d_md_data;
489 		disk_pos = oldpd->pd_disk_pos;
490 		resurrection = 1;
491 	}
492 
493 	if (olddisk == NULL) {
494 		/* Find placeholder by position. */
495 		olddisk = g_raid_md_nvidia_get_disk(sc, disk_pos);
496 		if (olddisk == NULL)
497 			panic("No disk at position %d!", disk_pos);
498 		if (olddisk->d_state != G_RAID_DISK_S_OFFLINE) {
499 			G_RAID_DEBUG1(1, sc, "More then one disk for pos %d",
500 			    disk_pos);
501 			g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE);
502 			return (0);
503 		}
504 		oldpd = (struct g_raid_md_nvidia_perdisk *)olddisk->d_md_data;
505 	}
506 
507 	/* Replace failed disk or placeholder with new disk. */
508 	TAILQ_FOREACH_SAFE(sd, &olddisk->d_subdisks, sd_next, tmpsd) {
509 		TAILQ_REMOVE(&olddisk->d_subdisks, sd, sd_next);
510 		TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
511 		sd->sd_disk = disk;
512 	}
513 	oldpd->pd_disk_pos = -2;
514 	pd->pd_disk_pos = disk_pos;
515 
516 	/* If it was placeholder -- destroy it. */
517 	if (olddisk->d_state == G_RAID_DISK_S_OFFLINE) {
518 		g_raid_destroy_disk(olddisk);
519 	} else {
520 		/* Otherwise, make it STALE_FAILED. */
521 		g_raid_change_disk_state(olddisk, G_RAID_DISK_S_STALE_FAILED);
522 	}
523 
524 	/* Welcome the new disk. */
525 	if (resurrection)
526 		g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
527 	else// if (pd->pd_meta->disk_status == NVIDIA_S_CURRENT ||
528 	    //pd->pd_meta->disk_status == NVIDIA_S_REBUILD)
529 		g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
530 //	else
531 //		g_raid_change_disk_state(disk, G_RAID_DISK_S_FAILED);
532 	TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
533 
534 		/*
535 		 * Different disks may have different sizes,
536 		 * in concat mode. Update from real disk size.
537 		 */
538 		if (meta->type == NVIDIA_T_CONCAT)
539 			sd->sd_size = pd->pd_disk_size - 0x800 * 512;
540 
541 		if (resurrection) {
542 			/* New or ex-spare disk. */
543 			g_raid_change_subdisk_state(sd,
544 			    G_RAID_SUBDISK_S_NEW);
545 		} else if (meta->state == NVIDIA_S_REBUILD &&
546 		    (pd->pd_meta->disk_status & 0x100)) {
547 			/* Rebuilding disk. */
548 			g_raid_change_subdisk_state(sd,
549 			    G_RAID_SUBDISK_S_REBUILD);
550 			sd->sd_rebuild_pos = (off_t)pd->pd_meta->rebuild_lba /
551 			    meta->array_width * pd->pd_meta->sector_size;
552 		} else if (meta->state == NVIDIA_S_SYNC) {
553 			/* Resyncing/dirty disk. */
554 			g_raid_change_subdisk_state(sd,
555 			    G_RAID_SUBDISK_S_RESYNC);
556 			sd->sd_rebuild_pos = (off_t)pd->pd_meta->rebuild_lba /
557 			    meta->array_width * pd->pd_meta->sector_size;
558 		} else {
559 			/* Up to date disk. */
560 			g_raid_change_subdisk_state(sd,
561 			    G_RAID_SUBDISK_S_ACTIVE);
562 		}
563 		g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
564 		    G_RAID_EVENT_SUBDISK);
565 	}
566 
567 	/* Update status of our need for spare. */
568 	if (mdi->mdio_started) {
569 		mdi->mdio_incomplete =
570 		    (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) <
571 		     mdi->mdio_total_disks);
572 	}
573 
574 	return (resurrection);
575 }
576 
577 static void
578 g_disk_md_nvidia_retaste(void *arg, int pending)
579 {
580 
581 	G_RAID_DEBUG(1, "Array is not complete, trying to retaste.");
582 	g_retaste(&g_raid_class);
583 	free(arg, M_MD_NVIDIA);
584 }
585 
586 static void
587 g_raid_md_nvidia_refill(struct g_raid_softc *sc)
588 {
589 	struct g_raid_md_object *md;
590 	struct g_raid_md_nvidia_object *mdi;
591 	struct g_raid_disk *disk;
592 	struct task *task;
593 	int update, na;
594 
595 	md = sc->sc_md;
596 	mdi = (struct g_raid_md_nvidia_object *)md;
597 	update = 0;
598 	do {
599 		/* Make sure we miss anything. */
600 		na = g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE);
601 		if (na == mdi->mdio_total_disks)
602 			break;
603 
604 		G_RAID_DEBUG1(1, md->mdo_softc,
605 		    "Array is not complete (%d of %d), "
606 		    "trying to refill.", na, mdi->mdio_total_disks);
607 
608 		/* Try to get use some of STALE disks. */
609 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
610 			if (disk->d_state == G_RAID_DISK_S_STALE) {
611 				update += g_raid_md_nvidia_start_disk(disk);
612 				if (disk->d_state == G_RAID_DISK_S_ACTIVE)
613 					break;
614 			}
615 		}
616 		if (disk != NULL)
617 			continue;
618 
619 		/* Try to get use some of SPARE disks. */
620 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
621 			if (disk->d_state == G_RAID_DISK_S_SPARE) {
622 				update += g_raid_md_nvidia_start_disk(disk);
623 				if (disk->d_state == G_RAID_DISK_S_ACTIVE)
624 					break;
625 			}
626 		}
627 	} while (disk != NULL);
628 
629 	/* Write new metadata if we changed something. */
630 	if (update)
631 		g_raid_md_write_nvidia(md, NULL, NULL, NULL);
632 
633 	/* Update status of our need for spare. */
634 	mdi->mdio_incomplete = (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) <
635 	    mdi->mdio_total_disks);
636 
637 	/* Request retaste hoping to find spare. */
638 	if (mdi->mdio_incomplete) {
639 		task = malloc(sizeof(struct task),
640 		    M_MD_NVIDIA, M_WAITOK | M_ZERO);
641 		TASK_INIT(task, 0, g_disk_md_nvidia_retaste, task);
642 		taskqueue_enqueue(taskqueue_swi, task);
643 	}
644 }
645 
646 static void
647 g_raid_md_nvidia_start(struct g_raid_softc *sc)
648 {
649 	struct g_raid_md_object *md;
650 	struct g_raid_md_nvidia_object *mdi;
651 	struct g_raid_md_nvidia_perdisk *pd;
652 	struct nvidia_raid_conf *meta;
653 	struct g_raid_volume *vol;
654 	struct g_raid_subdisk *sd;
655 	struct g_raid_disk *disk;
656 	off_t size;
657 	int j, disk_pos;
658 	char buf[17];
659 
660 	md = sc->sc_md;
661 	mdi = (struct g_raid_md_nvidia_object *)md;
662 	meta = mdi->mdio_meta;
663 
664 	/* Create volumes and subdisks. */
665 	nvidia_meta_get_name(meta, buf);
666 	vol = g_raid_create_volume(sc, buf, -1);
667 	vol->v_mediasize = (off_t)meta->total_sectors * 512;
668 	vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
669 	if (meta->type == NVIDIA_T_RAID0) {
670 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID0;
671 		size = vol->v_mediasize / mdi->mdio_total_disks;
672 	} else if (meta->type == NVIDIA_T_RAID1) {
673 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID1;
674 		size = vol->v_mediasize;
675 	} else if (meta->type == NVIDIA_T_RAID01) {
676 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E;
677 		size = vol->v_mediasize / (mdi->mdio_total_disks / 2);
678 	} else if (meta->type == NVIDIA_T_CONCAT) {
679 		if (mdi->mdio_total_disks == 1)
680 			vol->v_raid_level = G_RAID_VOLUME_RL_SINGLE;
681 		else
682 			vol->v_raid_level = G_RAID_VOLUME_RL_CONCAT;
683 		size = 0;
684 	} else if (meta->type == NVIDIA_T_RAID5) {
685 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID5;
686 		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LA;
687 		size = vol->v_mediasize / (mdi->mdio_total_disks - 1);
688 	} else if (meta->type == NVIDIA_T_RAID5_SYM) {
689 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID5;
690 		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LS;
691 		size = vol->v_mediasize / (mdi->mdio_total_disks - 1);
692 	} else {
693 		vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
694 		size = 0;
695 	}
696 	vol->v_strip_size = meta->strip_sectors * 512; //ZZZ
697 	vol->v_disks_count = mdi->mdio_total_disks;
698 	vol->v_sectorsize = 512; //ZZZ
699 	for (j = 0; j < vol->v_disks_count; j++) {
700 		sd = &vol->v_subdisks[j];
701 		sd->sd_offset = 0;
702 		sd->sd_size = size;
703 	}
704 	g_raid_start_volume(vol);
705 
706 	/* Create disk placeholders to store data for later writing. */
707 	for (disk_pos = 0; disk_pos < mdi->mdio_total_disks; disk_pos++) {
708 		pd = malloc(sizeof(*pd), M_MD_NVIDIA, M_WAITOK | M_ZERO);
709 		pd->pd_disk_pos = disk_pos;
710 		disk = g_raid_create_disk(sc);
711 		disk->d_md_data = (void *)pd;
712 		disk->d_state = G_RAID_DISK_S_OFFLINE;
713 		sd = &vol->v_subdisks[disk_pos];
714 		sd->sd_disk = disk;
715 		TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
716 	}
717 
718 	/* Make all disks found till the moment take their places. */
719 	do {
720 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
721 			if (disk->d_state == G_RAID_DISK_S_NONE) {
722 				g_raid_md_nvidia_start_disk(disk);
723 				break;
724 			}
725 		}
726 	} while (disk != NULL);
727 
728 	mdi->mdio_started = 1;
729 	G_RAID_DEBUG1(0, sc, "Array started.");
730 	g_raid_md_write_nvidia(md, NULL, NULL, NULL);
731 
732 	/* Pickup any STALE/SPARE disks to refill array if needed. */
733 	g_raid_md_nvidia_refill(sc);
734 
735 	g_raid_event_send(vol, G_RAID_VOLUME_E_START, G_RAID_EVENT_VOLUME);
736 
737 	callout_stop(&mdi->mdio_start_co);
738 	G_RAID_DEBUG1(1, sc, "root_mount_rel %p", mdi->mdio_rootmount);
739 	root_mount_rel(mdi->mdio_rootmount);
740 	mdi->mdio_rootmount = NULL;
741 }
742 
743 static void
744 g_raid_md_nvidia_new_disk(struct g_raid_disk *disk)
745 {
746 	struct g_raid_softc *sc;
747 	struct g_raid_md_object *md;
748 	struct g_raid_md_nvidia_object *mdi;
749 	struct nvidia_raid_conf *pdmeta;
750 	struct g_raid_md_nvidia_perdisk *pd;
751 
752 	sc = disk->d_softc;
753 	md = sc->sc_md;
754 	mdi = (struct g_raid_md_nvidia_object *)md;
755 	pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data;
756 	pdmeta = pd->pd_meta;
757 
758 	if (mdi->mdio_started) {
759 		if (g_raid_md_nvidia_start_disk(disk))
760 			g_raid_md_write_nvidia(md, NULL, NULL, NULL);
761 	} else {
762 		if (mdi->mdio_meta == NULL ||
763 		    mdi->mdio_meta->disk_number >= mdi->mdio_meta->total_disks) {
764 			G_RAID_DEBUG1(1, sc, "Newer disk");
765 			if (mdi->mdio_meta != NULL)
766 				free(mdi->mdio_meta, M_MD_NVIDIA);
767 			mdi->mdio_meta = nvidia_meta_copy(pdmeta);
768 			mdi->mdio_total_disks = pdmeta->total_disks;
769 			mdi->mdio_disks_present = 1;
770 		} else if (pdmeta->disk_number < mdi->mdio_meta->total_disks) {
771 			mdi->mdio_disks_present++;
772 			G_RAID_DEBUG1(1, sc, "Matching disk (%d of %d up)",
773 			    mdi->mdio_disks_present,
774 			    mdi->mdio_total_disks);
775 		} else
776 			G_RAID_DEBUG1(1, sc, "Spare disk");
777 
778 		/* If we collected all needed disks - start array. */
779 		if (mdi->mdio_disks_present == mdi->mdio_total_disks)
780 			g_raid_md_nvidia_start(sc);
781 	}
782 }
783 
784 static void
785 g_raid_nvidia_go(void *arg)
786 {
787 	struct g_raid_softc *sc;
788 	struct g_raid_md_object *md;
789 	struct g_raid_md_nvidia_object *mdi;
790 
791 	sc = arg;
792 	md = sc->sc_md;
793 	mdi = (struct g_raid_md_nvidia_object *)md;
794 	if (!mdi->mdio_started) {
795 		G_RAID_DEBUG1(0, sc, "Force array start due to timeout.");
796 		g_raid_event_send(sc, G_RAID_NODE_E_START, 0);
797 	}
798 }
799 
800 static int
801 g_raid_md_create_nvidia(struct g_raid_md_object *md, struct g_class *mp,
802     struct g_geom **gp)
803 {
804 	struct g_raid_softc *sc;
805 	struct g_raid_md_nvidia_object *mdi;
806 	char name[32];
807 
808 	mdi = (struct g_raid_md_nvidia_object *)md;
809 	arc4rand(&mdi->mdio_volume_id, 16, 0);
810 	snprintf(name, sizeof(name), "NVIDIA-%d",
811 	    atomic_fetchadd_int(&NVIDIANodeID, 1));
812 	sc = g_raid_create_node(mp, name, md);
813 	if (sc == NULL)
814 		return (G_RAID_MD_TASTE_FAIL);
815 	md->mdo_softc = sc;
816 	*gp = sc->sc_geom;
817 	return (G_RAID_MD_TASTE_NEW);
818 }
819 
820 static int
821 g_raid_md_taste_nvidia(struct g_raid_md_object *md, struct g_class *mp,
822                               struct g_consumer *cp, struct g_geom **gp)
823 {
824 	struct g_consumer *rcp;
825 	struct g_provider *pp;
826 	struct g_raid_md_nvidia_object *mdi, *mdi1;
827 	struct g_raid_softc *sc;
828 	struct g_raid_disk *disk;
829 	struct nvidia_raid_conf *meta;
830 	struct g_raid_md_nvidia_perdisk *pd;
831 	struct g_geom *geom;
832 	int error, result, spare, len;
833 	char name[32];
834 	uint16_t vendor;
835 
836 	G_RAID_DEBUG(1, "Tasting NVIDIA on %s", cp->provider->name);
837 	mdi = (struct g_raid_md_nvidia_object *)md;
838 	pp = cp->provider;
839 
840 	/* Read metadata from device. */
841 	meta = NULL;
842 	vendor = 0xffff;
843 	if (g_access(cp, 1, 0, 0) != 0)
844 		return (G_RAID_MD_TASTE_FAIL);
845 	g_topology_unlock();
846 	len = 2;
847 	if (pp->geom->rank == 1)
848 		g_io_getattr("GEOM::hba_vendor", cp, &len, &vendor);
849 	meta = nvidia_meta_read(cp);
850 	g_topology_lock();
851 	g_access(cp, -1, 0, 0);
852 	if (meta == NULL) {
853 		if (g_raid_aggressive_spare) {
854 			if (vendor == 0x10de) {
855 				G_RAID_DEBUG(1,
856 				    "No NVIDIA metadata, forcing spare.");
857 				spare = 2;
858 				goto search;
859 			} else {
860 				G_RAID_DEBUG(1,
861 				    "NVIDIA vendor mismatch 0x%04x != 0x10de",
862 				    vendor);
863 			}
864 		}
865 		return (G_RAID_MD_TASTE_FAIL);
866 	}
867 
868 	/* Metadata valid. Print it. */
869 	g_raid_md_nvidia_print(meta);
870 	G_RAID_DEBUG(1, "NVIDIA disk position %d", meta->disk_number);
871 	spare = 0;//(meta->type == NVIDIA_T_SPARE) ? 1 : 0;
872 
873 search:
874 	/* Search for matching node. */
875 	sc = NULL;
876 	mdi1 = NULL;
877 	LIST_FOREACH(geom, &mp->geom, geom) {
878 		sc = geom->softc;
879 		if (sc == NULL)
880 			continue;
881 		if (sc->sc_stopping != 0)
882 			continue;
883 		if (sc->sc_md->mdo_class != md->mdo_class)
884 			continue;
885 		mdi1 = (struct g_raid_md_nvidia_object *)sc->sc_md;
886 		if (spare) {
887 			if (mdi1->mdio_incomplete)
888 				break;
889 		} else {
890 			if (memcmp(&mdi1->mdio_volume_id,
891 			     &meta->volume_id, 16) == 0)
892 				break;
893 		}
894 	}
895 
896 	/* Found matching node. */
897 	if (geom != NULL) {
898 		G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name);
899 		result = G_RAID_MD_TASTE_EXISTING;
900 
901 	} else if (spare) { /* Not found needy node -- left for later. */
902 		G_RAID_DEBUG(1, "Spare is not needed at this time");
903 		goto fail1;
904 
905 	} else { /* Not found matching node -- create one. */
906 		result = G_RAID_MD_TASTE_NEW;
907 		memcpy(&mdi->mdio_volume_id, &meta->volume_id, 16);
908 		snprintf(name, sizeof(name), "NVIDIA-%d",
909 		    atomic_fetchadd_int(&NVIDIANodeID, 1));
910 		sc = g_raid_create_node(mp, name, md);
911 		md->mdo_softc = sc;
912 		geom = sc->sc_geom;
913 		callout_init(&mdi->mdio_start_co, 1);
914 		callout_reset(&mdi->mdio_start_co, g_raid_start_timeout * hz,
915 		    g_raid_nvidia_go, sc);
916 		mdi->mdio_rootmount = root_mount_hold("GRAID-NVIDIA");
917 		G_RAID_DEBUG1(1, sc, "root_mount_hold %p", mdi->mdio_rootmount);
918 	}
919 
920 	rcp = g_new_consumer(geom);
921 	g_attach(rcp, pp);
922 	if (g_access(rcp, 1, 1, 1) != 0)
923 		; //goto fail1;
924 
925 	g_topology_unlock();
926 	sx_xlock(&sc->sc_lock);
927 
928 	pd = malloc(sizeof(*pd), M_MD_NVIDIA, M_WAITOK | M_ZERO);
929 	pd->pd_meta = meta;
930 	if (spare == 2) {
931 		pd->pd_disk_pos = -3;
932 	} else {
933 		pd->pd_disk_pos = -1;
934 	}
935 	pd->pd_disk_size = pp->mediasize;
936 	disk = g_raid_create_disk(sc);
937 	disk->d_md_data = (void *)pd;
938 	disk->d_consumer = rcp;
939 	rcp->private = disk;
940 
941 	/* Read kernel dumping information. */
942 	disk->d_kd.offset = 0;
943 	disk->d_kd.length = OFF_MAX;
944 	len = sizeof(disk->d_kd);
945 	error = g_io_getattr("GEOM::kerneldump", rcp, &len, &disk->d_kd);
946 	if (disk->d_kd.di.dumper == NULL)
947 		G_RAID_DEBUG1(2, sc, "Dumping not supported by %s: %d.",
948 		    rcp->provider->name, error);
949 
950 	g_raid_md_nvidia_new_disk(disk);
951 
952 	sx_xunlock(&sc->sc_lock);
953 	g_topology_lock();
954 	*gp = geom;
955 	return (result);
956 fail1:
957 	free(meta, M_MD_NVIDIA);
958 	return (G_RAID_MD_TASTE_FAIL);
959 }
960 
961 static int
962 g_raid_md_event_nvidia(struct g_raid_md_object *md,
963     struct g_raid_disk *disk, u_int event)
964 {
965 	struct g_raid_softc *sc;
966 	struct g_raid_subdisk *sd;
967 	struct g_raid_md_nvidia_object *mdi;
968 	struct g_raid_md_nvidia_perdisk *pd;
969 
970 	sc = md->mdo_softc;
971 	mdi = (struct g_raid_md_nvidia_object *)md;
972 	if (disk == NULL) {
973 		switch (event) {
974 		case G_RAID_NODE_E_START:
975 			if (!mdi->mdio_started) {
976 				/* Bump volume ID to drop missing disks. */
977 				arc4rand(&mdi->mdio_volume_id, 16, 0);
978 				g_raid_md_nvidia_start(sc);
979 			}
980 			return (0);
981 		}
982 		return (-1);
983 	}
984 	pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data;
985 	switch (event) {
986 	case G_RAID_DISK_E_DISCONNECTED:
987 		/* If disk was assigned, just update statuses. */
988 		if (pd->pd_disk_pos >= 0) {
989 			g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
990 			if (disk->d_consumer) {
991 				g_raid_kill_consumer(sc, disk->d_consumer);
992 				disk->d_consumer = NULL;
993 			}
994 			TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
995 				g_raid_change_subdisk_state(sd,
996 				    G_RAID_SUBDISK_S_NONE);
997 				g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
998 				    G_RAID_EVENT_SUBDISK);
999 			}
1000 		} else {
1001 			/* Otherwise -- delete. */
1002 			g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
1003 			g_raid_destroy_disk(disk);
1004 		}
1005 
1006 		if (mdi->mdio_started) {
1007 			/* Bump volume ID to prevent disk resurrection. */
1008 			if (pd->pd_disk_pos >= 0)
1009 				arc4rand(&mdi->mdio_volume_id, 16, 0);
1010 
1011 			/* Write updated metadata to all disks. */
1012 			g_raid_md_write_nvidia(md, NULL, NULL, NULL);
1013 		}
1014 
1015 		/* Check if anything left except placeholders. */
1016 		if (g_raid_ndisks(sc, -1) ==
1017 		    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
1018 			g_raid_destroy_node(sc, 0);
1019 		else
1020 			g_raid_md_nvidia_refill(sc);
1021 		return (0);
1022 	}
1023 	return (-2);
1024 }
1025 
1026 static int
1027 g_raid_md_ctl_nvidia(struct g_raid_md_object *md,
1028     struct gctl_req *req)
1029 {
1030 	struct g_raid_softc *sc;
1031 	struct g_raid_volume *vol;
1032 	struct g_raid_subdisk *sd;
1033 	struct g_raid_disk *disk;
1034 	struct g_raid_md_nvidia_object *mdi;
1035 	struct g_raid_md_nvidia_perdisk *pd;
1036 	struct g_consumer *cp;
1037 	struct g_provider *pp;
1038 	char arg[16];
1039 	const char *verb, *volname, *levelname, *diskname;
1040 	int *nargs, *force;
1041 	off_t size, sectorsize, strip, volsize;
1042 	intmax_t *sizearg, *striparg;
1043 	int numdisks, i, len, level, qual, update;
1044 	int error;
1045 
1046 	sc = md->mdo_softc;
1047 	mdi = (struct g_raid_md_nvidia_object *)md;
1048 	verb = gctl_get_param(req, "verb", NULL);
1049 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1050 	error = 0;
1051 	if (strcmp(verb, "label") == 0) {
1052 
1053 		if (*nargs < 4) {
1054 			gctl_error(req, "Invalid number of arguments.");
1055 			return (-1);
1056 		}
1057 		volname = gctl_get_asciiparam(req, "arg1");
1058 		if (volname == NULL) {
1059 			gctl_error(req, "No volume name.");
1060 			return (-2);
1061 		}
1062 		levelname = gctl_get_asciiparam(req, "arg2");
1063 		if (levelname == NULL) {
1064 			gctl_error(req, "No RAID level.");
1065 			return (-3);
1066 		}
1067 		if (strcasecmp(levelname, "RAID5") == 0)
1068 			levelname = "RAID5-LS";
1069 		if (g_raid_volume_str2level(levelname, &level, &qual)) {
1070 			gctl_error(req, "Unknown RAID level '%s'.", levelname);
1071 			return (-4);
1072 		}
1073 		numdisks = *nargs - 3;
1074 		force = gctl_get_paraml(req, "force", sizeof(*force));
1075 		if (!g_raid_md_nvidia_supported(level, qual, numdisks,
1076 		    force ? *force : 0)) {
1077 			gctl_error(req, "Unsupported RAID level "
1078 			    "(0x%02x/0x%02x), or number of disks (%d).",
1079 			    level, qual, numdisks);
1080 			return (-5);
1081 		}
1082 
1083 		/* Search for disks, connect them and probe. */
1084 		size = 0x7fffffffffffffffllu;
1085 		sectorsize = 0;
1086 		for (i = 0; i < numdisks; i++) {
1087 			snprintf(arg, sizeof(arg), "arg%d", i + 3);
1088 			diskname = gctl_get_asciiparam(req, arg);
1089 			if (diskname == NULL) {
1090 				gctl_error(req, "No disk name (%s).", arg);
1091 				error = -6;
1092 				break;
1093 			}
1094 			if (strcmp(diskname, "NONE") == 0) {
1095 				cp = NULL;
1096 				pp = NULL;
1097 			} else {
1098 				g_topology_lock();
1099 				cp = g_raid_open_consumer(sc, diskname);
1100 				if (cp == NULL) {
1101 					gctl_error(req, "Can't open '%s'.",
1102 					    diskname);
1103 					g_topology_unlock();
1104 					error = -7;
1105 					break;
1106 				}
1107 				pp = cp->provider;
1108 			}
1109 			pd = malloc(sizeof(*pd), M_MD_NVIDIA, M_WAITOK | M_ZERO);
1110 			pd->pd_disk_pos = i;
1111 			disk = g_raid_create_disk(sc);
1112 			disk->d_md_data = (void *)pd;
1113 			disk->d_consumer = cp;
1114 			if (cp == NULL)
1115 				continue;
1116 			cp->private = disk;
1117 			g_topology_unlock();
1118 
1119 			/* Read kernel dumping information. */
1120 			disk->d_kd.offset = 0;
1121 			disk->d_kd.length = OFF_MAX;
1122 			len = sizeof(disk->d_kd);
1123 			g_io_getattr("GEOM::kerneldump", cp, &len, &disk->d_kd);
1124 			if (disk->d_kd.di.dumper == NULL)
1125 				G_RAID_DEBUG1(2, sc,
1126 				    "Dumping not supported by %s.",
1127 				    cp->provider->name);
1128 
1129 			pd->pd_disk_size = pp->mediasize;
1130 			if (size > pp->mediasize)
1131 				size = pp->mediasize;
1132 			if (sectorsize < pp->sectorsize)
1133 				sectorsize = pp->sectorsize;
1134 		}
1135 		if (error != 0)
1136 			return (error);
1137 
1138 		if (sectorsize <= 0) {
1139 			gctl_error(req, "Can't get sector size.");
1140 			return (-8);
1141 		}
1142 
1143 		/* Reserve space for metadata. */
1144 		size -= 2 * sectorsize;
1145 
1146 		/* Handle size argument. */
1147 		len = sizeof(*sizearg);
1148 		sizearg = gctl_get_param(req, "size", &len);
1149 		if (sizearg != NULL && len == sizeof(*sizearg) &&
1150 		    *sizearg > 0) {
1151 			if (*sizearg > size) {
1152 				gctl_error(req, "Size too big %lld > %lld.",
1153 				    (long long)*sizearg, (long long)size);
1154 				return (-9);
1155 			}
1156 			size = *sizearg;
1157 		}
1158 
1159 		/* Handle strip argument. */
1160 		strip = 131072;
1161 		len = sizeof(*striparg);
1162 		striparg = gctl_get_param(req, "strip", &len);
1163 		if (striparg != NULL && len == sizeof(*striparg) &&
1164 		    *striparg > 0) {
1165 			if (*striparg < sectorsize) {
1166 				gctl_error(req, "Strip size too small.");
1167 				return (-10);
1168 			}
1169 			if (*striparg % sectorsize != 0) {
1170 				gctl_error(req, "Incorrect strip size.");
1171 				return (-11);
1172 			}
1173 			if (strip > 65535 * sectorsize) {
1174 				gctl_error(req, "Strip size too big.");
1175 				return (-12);
1176 			}
1177 			strip = *striparg;
1178 		}
1179 
1180 		/* Round size down to strip or sector. */
1181 		if (level == G_RAID_VOLUME_RL_RAID1)
1182 			size -= (size % sectorsize);
1183 		else if (level == G_RAID_VOLUME_RL_RAID1E &&
1184 		    (numdisks & 1) != 0)
1185 			size -= (size % (2 * strip));
1186 		else
1187 			size -= (size % strip);
1188 		if (size <= 0) {
1189 			gctl_error(req, "Size too small.");
1190 			return (-13);
1191 		}
1192 
1193 		if (level == G_RAID_VOLUME_RL_RAID0 ||
1194 		    level == G_RAID_VOLUME_RL_CONCAT ||
1195 		    level == G_RAID_VOLUME_RL_SINGLE)
1196 			volsize = size * numdisks;
1197 		else if (level == G_RAID_VOLUME_RL_RAID1)
1198 			volsize = size;
1199 		else if (level == G_RAID_VOLUME_RL_RAID5)
1200 			volsize = size * (numdisks - 1);
1201 		else { /* RAID1E */
1202 			volsize = ((size * numdisks) / strip / 2) *
1203 			    strip;
1204 		}
1205 		if (volsize > 0xffffffffllu * sectorsize) {
1206 			gctl_error(req, "Size too big.");
1207 			return (-14);
1208 		}
1209 
1210 		/* We have all we need, create things: volume, ... */
1211 		mdi->mdio_total_disks = numdisks;
1212 		mdi->mdio_started = 1;
1213 		vol = g_raid_create_volume(sc, volname, -1);
1214 		vol->v_md_data = (void *)(intptr_t)0;
1215 		vol->v_raid_level = level;
1216 		vol->v_raid_level_qualifier = qual;
1217 		vol->v_strip_size = strip;
1218 		vol->v_disks_count = numdisks;
1219 		vol->v_mediasize = volsize;
1220 		vol->v_sectorsize = sectorsize;
1221 		g_raid_start_volume(vol);
1222 
1223 		/* , and subdisks. */
1224 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1225 			pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data;
1226 			sd = &vol->v_subdisks[pd->pd_disk_pos];
1227 			sd->sd_disk = disk;
1228 			sd->sd_offset = 0;
1229 			sd->sd_size = size;
1230 			TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
1231 			if (sd->sd_disk->d_consumer != NULL) {
1232 				g_raid_change_disk_state(disk,
1233 				    G_RAID_DISK_S_ACTIVE);
1234 				g_raid_change_subdisk_state(sd,
1235 				    G_RAID_SUBDISK_S_ACTIVE);
1236 				g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
1237 				    G_RAID_EVENT_SUBDISK);
1238 			} else {
1239 				g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
1240 			}
1241 		}
1242 
1243 		/* Write metadata based on created entities. */
1244 		G_RAID_DEBUG1(0, sc, "Array started.");
1245 		g_raid_md_write_nvidia(md, NULL, NULL, NULL);
1246 
1247 		/* Pickup any STALE/SPARE disks to refill array if needed. */
1248 		g_raid_md_nvidia_refill(sc);
1249 
1250 		g_raid_event_send(vol, G_RAID_VOLUME_E_START,
1251 		    G_RAID_EVENT_VOLUME);
1252 		return (0);
1253 	}
1254 	if (strcmp(verb, "delete") == 0) {
1255 
1256 		/* Check if some volume is still open. */
1257 		force = gctl_get_paraml(req, "force", sizeof(*force));
1258 		if (force != NULL && *force == 0 &&
1259 		    g_raid_nopens(sc) != 0) {
1260 			gctl_error(req, "Some volume is still open.");
1261 			return (-4);
1262 		}
1263 
1264 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1265 			if (disk->d_consumer)
1266 				nvidia_meta_erase(disk->d_consumer);
1267 		}
1268 		g_raid_destroy_node(sc, 0);
1269 		return (0);
1270 	}
1271 	if (strcmp(verb, "remove") == 0 ||
1272 	    strcmp(verb, "fail") == 0) {
1273 		if (*nargs < 2) {
1274 			gctl_error(req, "Invalid number of arguments.");
1275 			return (-1);
1276 		}
1277 		for (i = 1; i < *nargs; i++) {
1278 			snprintf(arg, sizeof(arg), "arg%d", i);
1279 			diskname = gctl_get_asciiparam(req, arg);
1280 			if (diskname == NULL) {
1281 				gctl_error(req, "No disk name (%s).", arg);
1282 				error = -2;
1283 				break;
1284 			}
1285 			if (strncmp(diskname, "/dev/", 5) == 0)
1286 				diskname += 5;
1287 
1288 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1289 				if (disk->d_consumer != NULL &&
1290 				    disk->d_consumer->provider != NULL &&
1291 				    strcmp(disk->d_consumer->provider->name,
1292 				     diskname) == 0)
1293 					break;
1294 			}
1295 			if (disk == NULL) {
1296 				gctl_error(req, "Disk '%s' not found.",
1297 				    diskname);
1298 				error = -3;
1299 				break;
1300 			}
1301 
1302 			if (strcmp(verb, "fail") == 0) {
1303 				g_raid_md_fail_disk_nvidia(md, NULL, disk);
1304 				continue;
1305 			}
1306 
1307 			pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data;
1308 
1309 			/* Erase metadata on deleting disk. */
1310 			nvidia_meta_erase(disk->d_consumer);
1311 
1312 			/* If disk was assigned, just update statuses. */
1313 			if (pd->pd_disk_pos >= 0) {
1314 				g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
1315 				g_raid_kill_consumer(sc, disk->d_consumer);
1316 				disk->d_consumer = NULL;
1317 				TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
1318 					g_raid_change_subdisk_state(sd,
1319 					    G_RAID_SUBDISK_S_NONE);
1320 					g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
1321 					    G_RAID_EVENT_SUBDISK);
1322 				}
1323 			} else {
1324 				/* Otherwise -- delete. */
1325 				g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
1326 				g_raid_destroy_disk(disk);
1327 			}
1328 		}
1329 
1330 		/* Write updated metadata to remaining disks. */
1331 		g_raid_md_write_nvidia(md, NULL, NULL, NULL);
1332 
1333 		/* Check if anything left except placeholders. */
1334 		if (g_raid_ndisks(sc, -1) ==
1335 		    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
1336 			g_raid_destroy_node(sc, 0);
1337 		else
1338 			g_raid_md_nvidia_refill(sc);
1339 		return (error);
1340 	}
1341 	if (strcmp(verb, "insert") == 0) {
1342 		if (*nargs < 2) {
1343 			gctl_error(req, "Invalid number of arguments.");
1344 			return (-1);
1345 		}
1346 		update = 0;
1347 		for (i = 1; i < *nargs; i++) {
1348 			/* Get disk name. */
1349 			snprintf(arg, sizeof(arg), "arg%d", i);
1350 			diskname = gctl_get_asciiparam(req, arg);
1351 			if (diskname == NULL) {
1352 				gctl_error(req, "No disk name (%s).", arg);
1353 				error = -3;
1354 				break;
1355 			}
1356 
1357 			/* Try to find provider with specified name. */
1358 			g_topology_lock();
1359 			cp = g_raid_open_consumer(sc, diskname);
1360 			if (cp == NULL) {
1361 				gctl_error(req, "Can't open disk '%s'.",
1362 				    diskname);
1363 				g_topology_unlock();
1364 				error = -4;
1365 				break;
1366 			}
1367 			pp = cp->provider;
1368 
1369 			pd = malloc(sizeof(*pd), M_MD_NVIDIA, M_WAITOK | M_ZERO);
1370 			pd->pd_disk_pos = -3;
1371 			pd->pd_disk_size = pp->mediasize;
1372 
1373 			disk = g_raid_create_disk(sc);
1374 			disk->d_consumer = cp;
1375 			disk->d_md_data = (void *)pd;
1376 			cp->private = disk;
1377 			g_topology_unlock();
1378 
1379 			/* Read kernel dumping information. */
1380 			disk->d_kd.offset = 0;
1381 			disk->d_kd.length = OFF_MAX;
1382 			len = sizeof(disk->d_kd);
1383 			g_io_getattr("GEOM::kerneldump", cp, &len, &disk->d_kd);
1384 			if (disk->d_kd.di.dumper == NULL)
1385 				G_RAID_DEBUG1(2, sc,
1386 				    "Dumping not supported by %s.",
1387 				    cp->provider->name);
1388 
1389 			/* Welcome the "new" disk. */
1390 			update += g_raid_md_nvidia_start_disk(disk);
1391 			if (disk->d_state != G_RAID_DISK_S_SPARE &&
1392 			    disk->d_state != G_RAID_DISK_S_ACTIVE) {
1393 				gctl_error(req, "Disk '%s' doesn't fit.",
1394 				    diskname);
1395 				g_raid_destroy_disk(disk);
1396 				error = -8;
1397 				break;
1398 			}
1399 		}
1400 
1401 		/* Write new metadata if we changed something. */
1402 		if (update)
1403 			g_raid_md_write_nvidia(md, NULL, NULL, NULL);
1404 		return (error);
1405 	}
1406 	gctl_error(req, "Command '%s' is not supported.", verb);
1407 	return (-100);
1408 }
1409 
1410 static int
1411 g_raid_md_write_nvidia(struct g_raid_md_object *md, struct g_raid_volume *tvol,
1412     struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
1413 {
1414 	struct g_raid_softc *sc;
1415 	struct g_raid_volume *vol;
1416 	struct g_raid_subdisk *sd;
1417 	struct g_raid_disk *disk;
1418 	struct g_raid_md_nvidia_object *mdi;
1419 	struct g_raid_md_nvidia_perdisk *pd;
1420 	struct nvidia_raid_conf *meta;
1421 	int i, spares;
1422 
1423 	sc = md->mdo_softc;
1424 	mdi = (struct g_raid_md_nvidia_object *)md;
1425 
1426 	if (sc->sc_stopping == G_RAID_DESTROY_HARD)
1427 		return (0);
1428 
1429 	/* There is only one volume. */
1430 	vol = TAILQ_FIRST(&sc->sc_volumes);
1431 
1432 	/* Fill global fields. */
1433 	meta = malloc(sizeof(*meta), M_MD_NVIDIA, M_WAITOK | M_ZERO);
1434 	if (mdi->mdio_meta)
1435 		memcpy(meta, mdi->mdio_meta, sizeof(*meta));
1436 	memcpy(meta->nvidia_id, NVIDIA_MAGIC, sizeof(NVIDIA_MAGIC) - 1);
1437 	meta->config_size = 30;
1438 	meta->version = 0x0064;
1439 	meta->total_sectors = vol->v_mediasize / vol->v_sectorsize;
1440 	meta->sector_size = vol->v_sectorsize;
1441 	nvidia_meta_put_name(meta, vol->v_name);
1442 	meta->magic_0 = NVIDIA_MAGIC0;
1443 	memcpy(&meta->volume_id, &mdi->mdio_volume_id, 16);
1444 	meta->state = NVIDIA_S_IDLE;
1445 	if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1)
1446 		meta->array_width = 1;
1447 	else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E)
1448 		meta->array_width = vol->v_disks_count / 2;
1449 	else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID5)
1450 		meta->array_width = vol->v_disks_count - 1;
1451 	else
1452 		meta->array_width = vol->v_disks_count;
1453 	meta->total_disks = vol->v_disks_count;
1454 	meta->orig_array_width = meta->array_width;
1455 	if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0)
1456 		meta->type = NVIDIA_T_RAID0;
1457 	else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1)
1458 		meta->type = NVIDIA_T_RAID1;
1459 	else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E)
1460 		meta->type = NVIDIA_T_RAID01;
1461 	else if (vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT ||
1462 	    vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE)
1463 		meta->type = NVIDIA_T_CONCAT;
1464 	else if (vol->v_raid_level_qualifier == G_RAID_VOLUME_RLQ_R5LA)
1465 		meta->type = NVIDIA_T_RAID5;
1466 	else
1467 		meta->type = NVIDIA_T_RAID5_SYM;
1468 	meta->strip_sectors = vol->v_strip_size / vol->v_sectorsize;
1469 	meta->strip_bytes = vol->v_strip_size;
1470 	meta->strip_shift = ffs(meta->strip_sectors) - 1;
1471 	meta->strip_mask = meta->strip_sectors - 1;
1472 	meta->stripe_sectors = meta->strip_sectors * meta->orig_array_width;
1473 	meta->stripe_bytes = meta->stripe_sectors * vol->v_sectorsize;
1474 	meta->rebuild_lba = 0;
1475 	meta->orig_type = meta->type;
1476 	meta->orig_total_sectors = meta->total_sectors;
1477 	meta->status = 0;
1478 
1479 	for (i = 0; i < vol->v_disks_count; i++) {
1480 		sd = &vol->v_subdisks[i];
1481 		if ((sd->sd_state == G_RAID_SUBDISK_S_STALE ||
1482 		     sd->sd_state == G_RAID_SUBDISK_S_RESYNC ||
1483 		     vol->v_dirty) &&
1484 		     meta->state != NVIDIA_S_REBUILD)
1485 			meta->state = NVIDIA_S_SYNC;
1486 		else if (sd->sd_state == G_RAID_SUBDISK_S_NEW ||
1487 		     sd->sd_state == G_RAID_SUBDISK_S_REBUILD)
1488 			meta->state = NVIDIA_S_REBUILD;
1489 	}
1490 
1491 	/* We are done. Print meta data and store them to disks. */
1492 	if (mdi->mdio_meta != NULL)
1493 		free(mdi->mdio_meta, M_MD_NVIDIA);
1494 	mdi->mdio_meta = meta;
1495 	spares = 0;
1496 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1497 		pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data;
1498 		if (disk->d_state != G_RAID_DISK_S_ACTIVE &&
1499 		    disk->d_state != G_RAID_DISK_S_SPARE)
1500 			continue;
1501 		if (pd->pd_meta != NULL) {
1502 			free(pd->pd_meta, M_MD_NVIDIA);
1503 			pd->pd_meta = NULL;
1504 		}
1505 		pd->pd_meta = nvidia_meta_copy(meta);
1506 		if ((sd = TAILQ_FIRST(&disk->d_subdisks)) != NULL) {
1507 			/* For RAID0+1 we need to translate order. */
1508 			pd->pd_meta->disk_number =
1509 			    nvidia_meta_translate_disk(meta, sd->sd_pos);
1510 			if (sd->sd_state != G_RAID_SUBDISK_S_ACTIVE) {
1511 				pd->pd_meta->disk_status = 0x100;
1512 				pd->pd_meta->rebuild_lba =
1513 				    sd->sd_rebuild_pos / vol->v_sectorsize *
1514 				    meta->array_width;
1515 			}
1516 		} else
1517 			pd->pd_meta->disk_number = meta->total_disks + spares++;
1518 		G_RAID_DEBUG(1, "Writing NVIDIA metadata to %s",
1519 		    g_raid_get_diskname(disk));
1520 		g_raid_md_nvidia_print(pd->pd_meta);
1521 		nvidia_meta_write(disk->d_consumer, pd->pd_meta);
1522 	}
1523 	return (0);
1524 }
1525 
1526 static int
1527 g_raid_md_fail_disk_nvidia(struct g_raid_md_object *md,
1528     struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
1529 {
1530 	struct g_raid_softc *sc;
1531 	struct g_raid_md_nvidia_perdisk *pd;
1532 	struct g_raid_subdisk *sd;
1533 
1534 	sc = md->mdo_softc;
1535 	pd = (struct g_raid_md_nvidia_perdisk *)tdisk->d_md_data;
1536 
1537 	/* We can't fail disk that is not a part of array now. */
1538 	if (pd->pd_disk_pos < 0)
1539 		return (-1);
1540 
1541 	/* Erase metadata to prevent disks's later resurrection. */
1542 	if (tdisk->d_consumer)
1543 		nvidia_meta_erase(tdisk->d_consumer);
1544 
1545 	/* Change states. */
1546 	g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED);
1547 	TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) {
1548 		g_raid_change_subdisk_state(sd,
1549 		    G_RAID_SUBDISK_S_FAILED);
1550 		g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED,
1551 		    G_RAID_EVENT_SUBDISK);
1552 	}
1553 
1554 	/* Write updated metadata to remaining disks. */
1555 	g_raid_md_write_nvidia(md, NULL, NULL, tdisk);
1556 
1557 	/* Check if anything left except placeholders. */
1558 	if (g_raid_ndisks(sc, -1) ==
1559 	    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
1560 		g_raid_destroy_node(sc, 0);
1561 	else
1562 		g_raid_md_nvidia_refill(sc);
1563 	return (0);
1564 }
1565 
1566 static int
1567 g_raid_md_free_disk_nvidia(struct g_raid_md_object *md,
1568     struct g_raid_disk *disk)
1569 {
1570 	struct g_raid_md_nvidia_perdisk *pd;
1571 
1572 	pd = (struct g_raid_md_nvidia_perdisk *)disk->d_md_data;
1573 	if (pd->pd_meta != NULL) {
1574 		free(pd->pd_meta, M_MD_NVIDIA);
1575 		pd->pd_meta = NULL;
1576 	}
1577 	free(pd, M_MD_NVIDIA);
1578 	disk->d_md_data = NULL;
1579 	return (0);
1580 }
1581 
1582 static int
1583 g_raid_md_free_nvidia(struct g_raid_md_object *md)
1584 {
1585 	struct g_raid_md_nvidia_object *mdi;
1586 
1587 	mdi = (struct g_raid_md_nvidia_object *)md;
1588 	if (!mdi->mdio_started) {
1589 		mdi->mdio_started = 0;
1590 		callout_stop(&mdi->mdio_start_co);
1591 		G_RAID_DEBUG1(1, md->mdo_softc,
1592 		    "root_mount_rel %p", mdi->mdio_rootmount);
1593 		root_mount_rel(mdi->mdio_rootmount);
1594 		mdi->mdio_rootmount = NULL;
1595 	}
1596 	if (mdi->mdio_meta != NULL) {
1597 		free(mdi->mdio_meta, M_MD_NVIDIA);
1598 		mdi->mdio_meta = NULL;
1599 	}
1600 	return (0);
1601 }
1602 
1603 G_RAID_MD_DECLARE(g_raid_md_nvidia);
1604