xref: /freebsd/sys/geom/raid/md_intel.c (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
5  * Copyright (c) 2000 - 2008 Søren Schmidt <sos@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/bio.h>
35 #include <sys/endian.h>
36 #include <sys/kernel.h>
37 #include <sys/kobj.h>
38 #include <sys/limits.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/systm.h>
43 #include <sys/taskqueue.h>
44 #include <sys/disk.h>
45 #include <geom/geom.h>
46 #include <geom/geom_dbg.h>
47 #include "geom/raid/g_raid.h"
48 #include "g_raid_md_if.h"
49 
50 static MALLOC_DEFINE(M_MD_INTEL, "md_intel_data", "GEOM_RAID Intel metadata");
51 
52 struct intel_raid_map {
53 	uint32_t	offset;
54 	uint32_t	disk_sectors;
55 	uint32_t	stripe_count;
56 	uint16_t	strip_sectors;
57 	uint8_t		status;
58 #define INTEL_S_READY           0x00
59 #define INTEL_S_UNINITIALIZED   0x01
60 #define INTEL_S_DEGRADED        0x02
61 #define INTEL_S_FAILURE         0x03
62 
63 	uint8_t		type;
64 #define INTEL_T_RAID0           0x00
65 #define INTEL_T_RAID1           0x01
66 #define INTEL_T_RAID5           0x05
67 
68 	uint8_t		total_disks;
69 	uint8_t		total_domains;
70 	uint8_t		failed_disk_num;
71 	uint8_t		ddf;
72 	uint32_t	offset_hi;
73 	uint32_t	disk_sectors_hi;
74 	uint32_t	stripe_count_hi;
75 	uint32_t	filler_2[4];
76 	uint32_t	disk_idx[1];	/* total_disks entries. */
77 #define INTEL_DI_IDX	0x00ffffff
78 #define INTEL_DI_RBLD	0x01000000
79 } __packed;
80 
81 struct intel_raid_vol {
82 	uint8_t		name[16];
83 	uint64_t	total_sectors __packed;
84 	uint32_t	state;
85 #define INTEL_ST_BOOTABLE		0x00000001
86 #define INTEL_ST_BOOT_DEVICE		0x00000002
87 #define INTEL_ST_READ_COALESCING	0x00000004
88 #define INTEL_ST_WRITE_COALESCING	0x00000008
89 #define INTEL_ST_LAST_SHUTDOWN_DIRTY	0x00000010
90 #define INTEL_ST_HIDDEN_AT_BOOT		0x00000020
91 #define INTEL_ST_CURRENTLY_HIDDEN	0x00000040
92 #define INTEL_ST_VERIFY_AND_FIX		0x00000080
93 #define INTEL_ST_MAP_STATE_UNINIT	0x00000100
94 #define INTEL_ST_NO_AUTO_RECOVERY	0x00000200
95 #define INTEL_ST_CLONE_N_GO		0x00000400
96 #define INTEL_ST_CLONE_MAN_SYNC		0x00000800
97 #define INTEL_ST_CNG_MASTER_DISK_NUM	0x00001000
98 	uint32_t	reserved;
99 	uint8_t		migr_priority;
100 	uint8_t		num_sub_vols;
101 	uint8_t		tid;
102 	uint8_t		cng_master_disk;
103 	uint16_t	cache_policy;
104 	uint8_t		cng_state;
105 #define INTEL_CNGST_UPDATED		0
106 #define INTEL_CNGST_NEEDS_UPDATE	1
107 #define INTEL_CNGST_MASTER_MISSING	2
108 	uint8_t		cng_sub_state;
109 	uint32_t	filler_0[10];
110 
111 	uint32_t	curr_migr_unit;
112 	uint32_t	checkpoint_id;
113 	uint8_t		migr_state;
114 	uint8_t		migr_type;
115 #define INTEL_MT_INIT		0
116 #define INTEL_MT_REBUILD	1
117 #define INTEL_MT_VERIFY		2
118 #define INTEL_MT_GEN_MIGR	3
119 #define INTEL_MT_STATE_CHANGE	4
120 #define INTEL_MT_REPAIR		5
121 	uint8_t		dirty;
122 	uint8_t		fs_state;
123 	uint16_t	verify_errors;
124 	uint16_t	bad_blocks;
125 	uint32_t	curr_migr_unit_hi;
126 	uint32_t	filler_1[3];
127 	struct intel_raid_map map[1];	/* 2 entries if migr_state != 0. */
128 } __packed;
129 
130 struct intel_raid_disk {
131 #define INTEL_SERIAL_LEN	16
132 	uint8_t		serial[INTEL_SERIAL_LEN];
133 	uint32_t	sectors;
134 	uint32_t	id;
135 	uint32_t	flags;
136 #define INTEL_F_SPARE		0x01
137 #define INTEL_F_ASSIGNED	0x02
138 #define INTEL_F_FAILED		0x04
139 #define INTEL_F_ONLINE		0x08
140 #define INTEL_F_DISABLED	0x80
141 	uint32_t	owner_cfg_num;
142 	uint32_t	sectors_hi;
143 	uint32_t	filler[3];
144 } __packed;
145 
146 struct intel_raid_conf {
147 	uint8_t		intel_id[24];
148 #define INTEL_MAGIC             "Intel Raid ISM Cfg Sig. "
149 
150 	uint8_t		version[6];
151 #define INTEL_VERSION_1000	"1.0.00"	/* RAID0 */
152 #define INTEL_VERSION_1100	"1.1.00"	/* RAID1 */
153 #define INTEL_VERSION_1200	"1.2.00"	/* Many volumes */
154 #define INTEL_VERSION_1201	"1.2.01"	/* 3 or 4 disks */
155 #define INTEL_VERSION_1202	"1.2.02"	/* RAID5 */
156 #define INTEL_VERSION_1204	"1.2.04"	/* 5 or 6 disks */
157 #define INTEL_VERSION_1206	"1.2.06"	/* CNG */
158 #define INTEL_VERSION_1300	"1.3.00"	/* Attributes */
159 
160 	uint8_t		dummy_0[2];
161 	uint32_t	checksum;
162 	uint32_t	config_size;
163 	uint32_t	config_id;
164 	uint32_t	generation;
165 	uint32_t	error_log_size;
166 	uint32_t	attributes;
167 #define INTEL_ATTR_RAID0	0x00000001
168 #define INTEL_ATTR_RAID1	0x00000002
169 #define INTEL_ATTR_RAID10	0x00000004
170 #define INTEL_ATTR_RAID1E	0x00000008
171 #define INTEL_ATTR_RAID5	0x00000010
172 #define INTEL_ATTR_RAIDCNG	0x00000020
173 #define INTEL_ATTR_EXT_STRIP	0x00000040
174 #define INTEL_ATTR_NVM_CACHE	0x02000000
175 #define INTEL_ATTR_2TB_DISK	0x04000000
176 #define INTEL_ATTR_BBM		0x08000000
177 #define INTEL_ATTR_NVM_CACHE2	0x10000000
178 #define INTEL_ATTR_2TB		0x20000000
179 #define INTEL_ATTR_PM		0x40000000
180 #define INTEL_ATTR_CHECKSUM	0x80000000
181 
182 	uint8_t		total_disks;
183 	uint8_t		total_volumes;
184 	uint8_t		error_log_pos;
185 	uint8_t		dummy_2[1];
186 	uint32_t	cache_size;
187 	uint32_t	orig_config_id;
188 	uint32_t	pwr_cycle_count;
189 	uint32_t	bbm_log_size;
190 	uint32_t	filler_0[35];
191 	struct intel_raid_disk	disk[1];	/* total_disks entries. */
192 	/* Here goes total_volumes of struct intel_raid_vol. */
193 } __packed;
194 
195 #define INTEL_ATTR_SUPPORTED	( INTEL_ATTR_RAID0 | INTEL_ATTR_RAID1 |	\
196     INTEL_ATTR_RAID10 | INTEL_ATTR_RAID1E | INTEL_ATTR_RAID5 |		\
197     INTEL_ATTR_RAIDCNG | INTEL_ATTR_EXT_STRIP | INTEL_ATTR_2TB_DISK |	\
198     INTEL_ATTR_2TB | INTEL_ATTR_PM | INTEL_ATTR_CHECKSUM )
199 
200 #define INTEL_MAX_MD_SIZE(ndisks)				\
201     (sizeof(struct intel_raid_conf) +				\
202      sizeof(struct intel_raid_disk) * (ndisks - 1) +		\
203      sizeof(struct intel_raid_vol) * 2 +			\
204      sizeof(struct intel_raid_map) * 2 +			\
205      sizeof(uint32_t) * (ndisks - 1) * 4)
206 
207 struct g_raid_md_intel_perdisk {
208 	struct intel_raid_conf	*pd_meta;
209 	int			 pd_disk_pos;
210 	struct intel_raid_disk	 pd_disk_meta;
211 };
212 
213 struct g_raid_md_intel_pervolume {
214 	int			 pv_volume_pos;
215 	int			 pv_cng;
216 	int			 pv_cng_man_sync;
217 	int			 pv_cng_master_disk;
218 };
219 
220 struct g_raid_md_intel_object {
221 	struct g_raid_md_object	 mdio_base;
222 	uint32_t		 mdio_config_id;
223 	uint32_t		 mdio_orig_config_id;
224 	uint32_t		 mdio_generation;
225 	struct intel_raid_conf	*mdio_meta;
226 	struct callout		 mdio_start_co;	/* STARTING state timer. */
227 	int			 mdio_disks_present;
228 	int			 mdio_started;
229 	int			 mdio_incomplete;
230 	struct root_hold_token	*mdio_rootmount; /* Root mount delay token. */
231 };
232 
233 static g_raid_md_create_t g_raid_md_create_intel;
234 static g_raid_md_taste_t g_raid_md_taste_intel;
235 static g_raid_md_event_t g_raid_md_event_intel;
236 static g_raid_md_ctl_t g_raid_md_ctl_intel;
237 static g_raid_md_write_t g_raid_md_write_intel;
238 static g_raid_md_fail_disk_t g_raid_md_fail_disk_intel;
239 static g_raid_md_free_disk_t g_raid_md_free_disk_intel;
240 static g_raid_md_free_volume_t g_raid_md_free_volume_intel;
241 static g_raid_md_free_t g_raid_md_free_intel;
242 
243 static kobj_method_t g_raid_md_intel_methods[] = {
244 	KOBJMETHOD(g_raid_md_create,	g_raid_md_create_intel),
245 	KOBJMETHOD(g_raid_md_taste,	g_raid_md_taste_intel),
246 	KOBJMETHOD(g_raid_md_event,	g_raid_md_event_intel),
247 	KOBJMETHOD(g_raid_md_ctl,	g_raid_md_ctl_intel),
248 	KOBJMETHOD(g_raid_md_write,	g_raid_md_write_intel),
249 	KOBJMETHOD(g_raid_md_fail_disk,	g_raid_md_fail_disk_intel),
250 	KOBJMETHOD(g_raid_md_free_disk,	g_raid_md_free_disk_intel),
251 	KOBJMETHOD(g_raid_md_free_volume,	g_raid_md_free_volume_intel),
252 	KOBJMETHOD(g_raid_md_free,	g_raid_md_free_intel),
253 	{ 0, 0 }
254 };
255 
256 static struct g_raid_md_class g_raid_md_intel_class = {
257 	"Intel",
258 	g_raid_md_intel_methods,
259 	sizeof(struct g_raid_md_intel_object),
260 	.mdc_enable = 1,
261 	.mdc_priority = 100
262 };
263 
264 static struct intel_raid_map *
265 intel_get_map(struct intel_raid_vol *mvol, int i)
266 {
267 	struct intel_raid_map *mmap;
268 
269 	if (i > (mvol->migr_state ? 1 : 0))
270 		return (NULL);
271 	mmap = &mvol->map[0];
272 	for (; i > 0; i--) {
273 		mmap = (struct intel_raid_map *)
274 		    &mmap->disk_idx[mmap->total_disks];
275 	}
276 	return ((struct intel_raid_map *)mmap);
277 }
278 
279 static struct intel_raid_vol *
280 intel_get_volume(struct intel_raid_conf *meta, int i)
281 {
282 	struct intel_raid_vol *mvol;
283 	struct intel_raid_map *mmap;
284 
285 	if (i > 1)
286 		return (NULL);
287 	mvol = (struct intel_raid_vol *)&meta->disk[meta->total_disks];
288 	for (; i > 0; i--) {
289 		mmap = intel_get_map(mvol, mvol->migr_state ? 1 : 0);
290 		mvol = (struct intel_raid_vol *)
291 		    &mmap->disk_idx[mmap->total_disks];
292 	}
293 	return (mvol);
294 }
295 
296 static off_t
297 intel_get_map_offset(struct intel_raid_map *mmap)
298 {
299 	off_t offset = (off_t)mmap->offset_hi << 32;
300 
301 	offset += mmap->offset;
302 	return (offset);
303 }
304 
305 static void
306 intel_set_map_offset(struct intel_raid_map *mmap, off_t offset)
307 {
308 
309 	mmap->offset = offset & 0xffffffff;
310 	mmap->offset_hi = offset >> 32;
311 }
312 
313 static off_t
314 intel_get_map_disk_sectors(struct intel_raid_map *mmap)
315 {
316 	off_t disk_sectors = (off_t)mmap->disk_sectors_hi << 32;
317 
318 	disk_sectors += mmap->disk_sectors;
319 	return (disk_sectors);
320 }
321 
322 static void
323 intel_set_map_disk_sectors(struct intel_raid_map *mmap, off_t disk_sectors)
324 {
325 
326 	mmap->disk_sectors = disk_sectors & 0xffffffff;
327 	mmap->disk_sectors_hi = disk_sectors >> 32;
328 }
329 
330 static void
331 intel_set_map_stripe_count(struct intel_raid_map *mmap, off_t stripe_count)
332 {
333 
334 	mmap->stripe_count = stripe_count & 0xffffffff;
335 	mmap->stripe_count_hi = stripe_count >> 32;
336 }
337 
338 static off_t
339 intel_get_disk_sectors(struct intel_raid_disk *disk)
340 {
341 	off_t sectors = (off_t)disk->sectors_hi << 32;
342 
343 	sectors += disk->sectors;
344 	return (sectors);
345 }
346 
347 static void
348 intel_set_disk_sectors(struct intel_raid_disk *disk, off_t sectors)
349 {
350 
351 	disk->sectors = sectors & 0xffffffff;
352 	disk->sectors_hi = sectors >> 32;
353 }
354 
355 static off_t
356 intel_get_vol_curr_migr_unit(struct intel_raid_vol *vol)
357 {
358 	off_t curr_migr_unit = (off_t)vol->curr_migr_unit_hi << 32;
359 
360 	curr_migr_unit += vol->curr_migr_unit;
361 	return (curr_migr_unit);
362 }
363 
364 static void
365 intel_set_vol_curr_migr_unit(struct intel_raid_vol *vol, off_t curr_migr_unit)
366 {
367 
368 	vol->curr_migr_unit = curr_migr_unit & 0xffffffff;
369 	vol->curr_migr_unit_hi = curr_migr_unit >> 32;
370 }
371 
372 static char *
373 intel_status2str(int status)
374 {
375 
376 	switch (status) {
377 	case INTEL_S_READY:
378 		return ("READY");
379 	case INTEL_S_UNINITIALIZED:
380 		return ("UNINITIALIZED");
381 	case INTEL_S_DEGRADED:
382 		return ("DEGRADED");
383 	case INTEL_S_FAILURE:
384 		return ("FAILURE");
385 	default:
386 		return ("UNKNOWN");
387 	}
388 }
389 
390 static char *
391 intel_type2str(int type)
392 {
393 
394 	switch (type) {
395 	case INTEL_T_RAID0:
396 		return ("RAID0");
397 	case INTEL_T_RAID1:
398 		return ("RAID1");
399 	case INTEL_T_RAID5:
400 		return ("RAID5");
401 	default:
402 		return ("UNKNOWN");
403 	}
404 }
405 
406 static char *
407 intel_cngst2str(int cng_state)
408 {
409 
410 	switch (cng_state) {
411 	case INTEL_CNGST_UPDATED:
412 		return ("UPDATED");
413 	case INTEL_CNGST_NEEDS_UPDATE:
414 		return ("NEEDS_UPDATE");
415 	case INTEL_CNGST_MASTER_MISSING:
416 		return ("MASTER_MISSING");
417 	default:
418 		return ("UNKNOWN");
419 	}
420 }
421 
422 static char *
423 intel_mt2str(int type)
424 {
425 
426 	switch (type) {
427 	case INTEL_MT_INIT:
428 		return ("INIT");
429 	case INTEL_MT_REBUILD:
430 		return ("REBUILD");
431 	case INTEL_MT_VERIFY:
432 		return ("VERIFY");
433 	case INTEL_MT_GEN_MIGR:
434 		return ("GEN_MIGR");
435 	case INTEL_MT_STATE_CHANGE:
436 		return ("STATE_CHANGE");
437 	case INTEL_MT_REPAIR:
438 		return ("REPAIR");
439 	default:
440 		return ("UNKNOWN");
441 	}
442 }
443 
444 static void
445 g_raid_md_intel_print(struct intel_raid_conf *meta)
446 {
447 	struct intel_raid_vol *mvol;
448 	struct intel_raid_map *mmap;
449 	int i, j, k;
450 
451 	if (g_raid_debug < 1)
452 		return;
453 
454 	printf("********* ATA Intel MatrixRAID Metadata *********\n");
455 	printf("intel_id            <%.24s>\n", meta->intel_id);
456 	printf("version             <%.6s>\n", meta->version);
457 	printf("checksum            0x%08x\n", meta->checksum);
458 	printf("config_size         0x%08x\n", meta->config_size);
459 	printf("config_id           0x%08x\n", meta->config_id);
460 	printf("generation          0x%08x\n", meta->generation);
461 	printf("error_log_size      %d\n", meta->error_log_size);
462 	printf("attributes          0x%b\n", meta->attributes,
463 		"\020"
464 		"\001RAID0"
465 		"\002RAID1"
466 		"\003RAID10"
467 		"\004RAID1E"
468 		"\005RAID15"
469 		"\006RAIDCNG"
470 		"\007EXT_STRIP"
471 		"\032NVM_CACHE"
472 		"\0332TB_DISK"
473 		"\034BBM"
474 		"\035NVM_CACHE"
475 		"\0362TB"
476 		"\037PM"
477 		"\040CHECKSUM");
478 	printf("total_disks         %u\n", meta->total_disks);
479 	printf("total_volumes       %u\n", meta->total_volumes);
480 	printf("error_log_pos       %u\n", meta->error_log_pos);
481 	printf("cache_size          %u\n", meta->cache_size);
482 	printf("orig_config_id      0x%08x\n", meta->orig_config_id);
483 	printf("pwr_cycle_count     %u\n", meta->pwr_cycle_count);
484 	printf("bbm_log_size        %u\n", meta->bbm_log_size);
485 	printf("Flags: S - Spare, A - Assigned, F - Failed, O - Online, D - Disabled\n");
486 	printf("DISK#   serial disk_sectors disk_sectors_hi disk_id flags owner\n");
487 	for (i = 0; i < meta->total_disks; i++ ) {
488 		printf("    %d   <%.16s> %u %u 0x%08x 0x%b %08x\n", i,
489 		    meta->disk[i].serial, meta->disk[i].sectors,
490 		    meta->disk[i].sectors_hi, meta->disk[i].id,
491 		    meta->disk[i].flags, "\20\01S\02A\03F\04O\05D",
492 		    meta->disk[i].owner_cfg_num);
493 	}
494 	for (i = 0; i < meta->total_volumes; i++) {
495 		mvol = intel_get_volume(meta, i);
496 		printf(" ****** Volume %d ******\n", i);
497 		printf(" name               %.16s\n", mvol->name);
498 		printf(" total_sectors      %ju\n", mvol->total_sectors);
499 		printf(" state              0x%b\n", mvol->state,
500 			"\020"
501 			"\001BOOTABLE"
502 			"\002BOOT_DEVICE"
503 			"\003READ_COALESCING"
504 			"\004WRITE_COALESCING"
505 			"\005LAST_SHUTDOWN_DIRTY"
506 			"\006HIDDEN_AT_BOOT"
507 			"\007CURRENTLY_HIDDEN"
508 			"\010VERIFY_AND_FIX"
509 			"\011MAP_STATE_UNINIT"
510 			"\012NO_AUTO_RECOVERY"
511 			"\013CLONE_N_GO"
512 			"\014CLONE_MAN_SYNC"
513 			"\015CNG_MASTER_DISK_NUM");
514 		printf(" reserved           %u\n", mvol->reserved);
515 		printf(" migr_priority      %u\n", mvol->migr_priority);
516 		printf(" num_sub_vols       %u\n", mvol->num_sub_vols);
517 		printf(" tid                %u\n", mvol->tid);
518 		printf(" cng_master_disk    %u\n", mvol->cng_master_disk);
519 		printf(" cache_policy       %u\n", mvol->cache_policy);
520 		printf(" cng_state          %u (%s)\n", mvol->cng_state,
521 			intel_cngst2str(mvol->cng_state));
522 		printf(" cng_sub_state      %u\n", mvol->cng_sub_state);
523 		printf(" curr_migr_unit     %u\n", mvol->curr_migr_unit);
524 		printf(" curr_migr_unit_hi  %u\n", mvol->curr_migr_unit_hi);
525 		printf(" checkpoint_id      %u\n", mvol->checkpoint_id);
526 		printf(" migr_state         %u\n", mvol->migr_state);
527 		printf(" migr_type          %u (%s)\n", mvol->migr_type,
528 			intel_mt2str(mvol->migr_type));
529 		printf(" dirty              %u\n", mvol->dirty);
530 		printf(" fs_state           %u\n", mvol->fs_state);
531 		printf(" verify_errors      %u\n", mvol->verify_errors);
532 		printf(" bad_blocks         %u\n", mvol->bad_blocks);
533 
534 		for (j = 0; j < (mvol->migr_state ? 2 : 1); j++) {
535 			printf("  *** Map %d ***\n", j);
536 			mmap = intel_get_map(mvol, j);
537 			printf("  offset            %u\n", mmap->offset);
538 			printf("  offset_hi         %u\n", mmap->offset_hi);
539 			printf("  disk_sectors      %u\n", mmap->disk_sectors);
540 			printf("  disk_sectors_hi   %u\n", mmap->disk_sectors_hi);
541 			printf("  stripe_count      %u\n", mmap->stripe_count);
542 			printf("  stripe_count_hi   %u\n", mmap->stripe_count_hi);
543 			printf("  strip_sectors     %u\n", mmap->strip_sectors);
544 			printf("  status            %u (%s)\n", mmap->status,
545 				intel_status2str(mmap->status));
546 			printf("  type              %u (%s)\n", mmap->type,
547 				intel_type2str(mmap->type));
548 			printf("  total_disks       %u\n", mmap->total_disks);
549 			printf("  total_domains     %u\n", mmap->total_domains);
550 			printf("  failed_disk_num   %u\n", mmap->failed_disk_num);
551 			printf("  ddf               %u\n", mmap->ddf);
552 			printf("  disk_idx         ");
553 			for (k = 0; k < mmap->total_disks; k++)
554 				printf(" 0x%08x", mmap->disk_idx[k]);
555 			printf("\n");
556 		}
557 	}
558 	printf("=================================================\n");
559 }
560 
561 static struct intel_raid_conf *
562 intel_meta_copy(struct intel_raid_conf *meta)
563 {
564 	struct intel_raid_conf *nmeta;
565 
566 	nmeta = malloc(meta->config_size, M_MD_INTEL, M_WAITOK);
567 	memcpy(nmeta, meta, meta->config_size);
568 	return (nmeta);
569 }
570 
571 static int
572 intel_meta_find_disk(struct intel_raid_conf *meta, char *serial)
573 {
574 	int pos;
575 
576 	for (pos = 0; pos < meta->total_disks; pos++) {
577 		if (strncmp(meta->disk[pos].serial,
578 		    serial, INTEL_SERIAL_LEN) == 0)
579 			return (pos);
580 	}
581 	return (-1);
582 }
583 
584 static struct intel_raid_conf *
585 intel_meta_read(struct g_consumer *cp)
586 {
587 	struct g_provider *pp;
588 	struct intel_raid_conf *meta;
589 	struct intel_raid_vol *mvol;
590 	struct intel_raid_map *mmap, *mmap1;
591 	char *buf;
592 	int error, i, j, k, left, size;
593 	uint32_t checksum, *ptr;
594 
595 	pp = cp->provider;
596 	if (pp->sectorsize < sizeof(*meta))
597 		return (NULL);
598 	/* Read the anchor sector. */
599 	buf = g_read_data(cp,
600 	    pp->mediasize - pp->sectorsize * 2, pp->sectorsize, &error);
601 	if (buf == NULL) {
602 		G_RAID_DEBUG(1, "Cannot read metadata from %s (error=%d).",
603 		    pp->name, error);
604 		return (NULL);
605 	}
606 	meta = (struct intel_raid_conf *)buf;
607 
608 	/* Check if this is an Intel RAID struct */
609 	if (strncmp(meta->intel_id, INTEL_MAGIC, strlen(INTEL_MAGIC))) {
610 		G_RAID_DEBUG(1, "Intel signature check failed on %s", pp->name);
611 		g_free(buf);
612 		return (NULL);
613 	}
614 	if (meta->config_size > 65536 ||
615 	    meta->config_size < sizeof(struct intel_raid_conf)) {
616 		G_RAID_DEBUG(1, "Intel metadata size looks wrong: %d",
617 		    meta->config_size);
618 		g_free(buf);
619 		return (NULL);
620 	}
621 	size = meta->config_size;
622 	meta = malloc(size, M_MD_INTEL, M_WAITOK);
623 	memcpy(meta, buf, min(size, pp->sectorsize));
624 	g_free(buf);
625 
626 	/* Read all the rest, if needed. */
627 	if (meta->config_size > pp->sectorsize) {
628 		left = (meta->config_size - 1) / pp->sectorsize;
629 		buf = g_read_data(cp,
630 		    pp->mediasize - pp->sectorsize * (2 + left),
631 		    pp->sectorsize * left, &error);
632 		if (buf == NULL) {
633 			G_RAID_DEBUG(1, "Cannot read remaining metadata"
634 			    " part from %s (error=%d).",
635 			    pp->name, error);
636 			free(meta, M_MD_INTEL);
637 			return (NULL);
638 		}
639 		memcpy(((char *)meta) + pp->sectorsize, buf,
640 		    pp->sectorsize * left);
641 		g_free(buf);
642 	}
643 
644 	/* Check metadata checksum. */
645 	for (checksum = 0, ptr = (uint32_t *)meta, i = 0;
646 	    i < (meta->config_size / sizeof(uint32_t)); i++) {
647 		checksum += *ptr++;
648 	}
649 	checksum -= meta->checksum;
650 	if (checksum != meta->checksum) {
651 		G_RAID_DEBUG(1, "Intel checksum check failed on %s", pp->name);
652 		free(meta, M_MD_INTEL);
653 		return (NULL);
654 	}
655 
656 	/* Validate metadata size. */
657 	size = sizeof(struct intel_raid_conf) +
658 	    sizeof(struct intel_raid_disk) * (meta->total_disks - 1) +
659 	    sizeof(struct intel_raid_vol) * meta->total_volumes;
660 	if (size > meta->config_size) {
661 badsize:
662 		G_RAID_DEBUG(1, "Intel metadata size incorrect %d < %d",
663 		    meta->config_size, size);
664 		free(meta, M_MD_INTEL);
665 		return (NULL);
666 	}
667 	for (i = 0; i < meta->total_volumes; i++) {
668 		mvol = intel_get_volume(meta, i);
669 		mmap = intel_get_map(mvol, 0);
670 		size += 4 * (mmap->total_disks - 1);
671 		if (size > meta->config_size)
672 			goto badsize;
673 		if (mvol->migr_state) {
674 			size += sizeof(struct intel_raid_map);
675 			if (size > meta->config_size)
676 				goto badsize;
677 			mmap = intel_get_map(mvol, 1);
678 			size += 4 * (mmap->total_disks - 1);
679 			if (size > meta->config_size)
680 				goto badsize;
681 		}
682 	}
683 
684 	g_raid_md_intel_print(meta);
685 
686 	if (strncmp(meta->version, INTEL_VERSION_1300, 6) > 0) {
687 		G_RAID_DEBUG(1, "Intel unsupported version: '%.6s'",
688 		    meta->version);
689 		free(meta, M_MD_INTEL);
690 		return (NULL);
691 	}
692 
693 	if (strncmp(meta->version, INTEL_VERSION_1300, 6) >= 0 &&
694 	    (meta->attributes & ~INTEL_ATTR_SUPPORTED) != 0) {
695 		G_RAID_DEBUG(1, "Intel unsupported attributes: 0x%08x",
696 		    meta->attributes & ~INTEL_ATTR_SUPPORTED);
697 		free(meta, M_MD_INTEL);
698 		return (NULL);
699 	}
700 
701 	/* Validate disk indexes. */
702 	for (i = 0; i < meta->total_volumes; i++) {
703 		mvol = intel_get_volume(meta, i);
704 		for (j = 0; j < (mvol->migr_state ? 2 : 1); j++) {
705 			mmap = intel_get_map(mvol, j);
706 			for (k = 0; k < mmap->total_disks; k++) {
707 				if ((mmap->disk_idx[k] & INTEL_DI_IDX) >
708 				    meta->total_disks) {
709 					G_RAID_DEBUG(1, "Intel metadata disk"
710 					    " index %d too big (>%d)",
711 					    mmap->disk_idx[k] & INTEL_DI_IDX,
712 					    meta->total_disks);
713 					free(meta, M_MD_INTEL);
714 					return (NULL);
715 				}
716 			}
717 		}
718 	}
719 
720 	/* Validate migration types. */
721 	for (i = 0; i < meta->total_volumes; i++) {
722 		mvol = intel_get_volume(meta, i);
723 		/* Deny unknown migration types. */
724 		if (mvol->migr_state &&
725 		    mvol->migr_type != INTEL_MT_INIT &&
726 		    mvol->migr_type != INTEL_MT_REBUILD &&
727 		    mvol->migr_type != INTEL_MT_VERIFY &&
728 		    mvol->migr_type != INTEL_MT_GEN_MIGR &&
729 		    mvol->migr_type != INTEL_MT_REPAIR) {
730 			G_RAID_DEBUG(1, "Intel metadata has unsupported"
731 			    " migration type %d", mvol->migr_type);
732 			free(meta, M_MD_INTEL);
733 			return (NULL);
734 		}
735 		/* Deny general migrations except SINGLE->RAID1. */
736 		if (mvol->migr_state &&
737 		    mvol->migr_type == INTEL_MT_GEN_MIGR) {
738 			mmap = intel_get_map(mvol, 0);
739 			mmap1 = intel_get_map(mvol, 1);
740 			if (mmap1->total_disks != 1 ||
741 			    mmap->type != INTEL_T_RAID1 ||
742 			    mmap->total_disks != 2 ||
743 			    mmap->offset != mmap1->offset ||
744 			    mmap->disk_sectors != mmap1->disk_sectors ||
745 			    mmap->total_domains != mmap->total_disks ||
746 			    mmap->offset_hi != mmap1->offset_hi ||
747 			    mmap->disk_sectors_hi != mmap1->disk_sectors_hi ||
748 			    (mmap->disk_idx[0] != mmap1->disk_idx[0] &&
749 			     mmap->disk_idx[0] != mmap1->disk_idx[1])) {
750 				G_RAID_DEBUG(1, "Intel metadata has unsupported"
751 				    " variant of general migration");
752 				free(meta, M_MD_INTEL);
753 				return (NULL);
754 			}
755 		}
756 	}
757 
758 	return (meta);
759 }
760 
761 static int
762 intel_meta_write(struct g_consumer *cp, struct intel_raid_conf *meta)
763 {
764 	struct g_provider *pp;
765 	char *buf;
766 	int error, i, sectors;
767 	uint32_t checksum, *ptr;
768 
769 	pp = cp->provider;
770 
771 	/* Recalculate checksum for case if metadata were changed. */
772 	meta->checksum = 0;
773 	for (checksum = 0, ptr = (uint32_t *)meta, i = 0;
774 	    i < (meta->config_size / sizeof(uint32_t)); i++) {
775 		checksum += *ptr++;
776 	}
777 	meta->checksum = checksum;
778 
779 	/* Create and fill buffer. */
780 	sectors = howmany(meta->config_size, pp->sectorsize);
781 	buf = malloc(sectors * pp->sectorsize, M_MD_INTEL, M_WAITOK | M_ZERO);
782 	if (sectors > 1) {
783 		memcpy(buf, ((char *)meta) + pp->sectorsize,
784 		    (sectors - 1) * pp->sectorsize);
785 	}
786 	memcpy(buf + (sectors - 1) * pp->sectorsize, meta, pp->sectorsize);
787 
788 	error = g_write_data(cp,
789 	    pp->mediasize - pp->sectorsize * (1 + sectors),
790 	    buf, pp->sectorsize * sectors);
791 	if (error != 0) {
792 		G_RAID_DEBUG(1, "Cannot write metadata to %s (error=%d).",
793 		    pp->name, error);
794 	}
795 
796 	free(buf, M_MD_INTEL);
797 	return (error);
798 }
799 
800 static int
801 intel_meta_erase(struct g_consumer *cp)
802 {
803 	struct g_provider *pp;
804 	char *buf;
805 	int error;
806 
807 	pp = cp->provider;
808 	buf = malloc(pp->sectorsize, M_MD_INTEL, M_WAITOK | M_ZERO);
809 	error = g_write_data(cp,
810 	    pp->mediasize - 2 * pp->sectorsize,
811 	    buf, pp->sectorsize);
812 	if (error != 0) {
813 		G_RAID_DEBUG(1, "Cannot erase metadata on %s (error=%d).",
814 		    pp->name, error);
815 	}
816 	free(buf, M_MD_INTEL);
817 	return (error);
818 }
819 
820 static int
821 intel_meta_write_spare(struct g_consumer *cp, struct intel_raid_disk *d)
822 {
823 	struct intel_raid_conf *meta;
824 	int error;
825 
826 	/* Fill anchor and single disk. */
827 	meta = malloc(INTEL_MAX_MD_SIZE(1), M_MD_INTEL, M_WAITOK | M_ZERO);
828 	memcpy(&meta->intel_id[0], INTEL_MAGIC, sizeof(INTEL_MAGIC) - 1);
829 	memcpy(&meta->version[0], INTEL_VERSION_1000,
830 	    sizeof(INTEL_VERSION_1000) - 1);
831 	meta->config_size = INTEL_MAX_MD_SIZE(1);
832 	meta->config_id = meta->orig_config_id = arc4random();
833 	meta->generation = 1;
834 	meta->total_disks = 1;
835 	meta->disk[0] = *d;
836 	error = intel_meta_write(cp, meta);
837 	free(meta, M_MD_INTEL);
838 	return (error);
839 }
840 
841 static struct g_raid_disk *
842 g_raid_md_intel_get_disk(struct g_raid_softc *sc, int id)
843 {
844 	struct g_raid_disk	*disk;
845 	struct g_raid_md_intel_perdisk *pd;
846 
847 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
848 		pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
849 		if (pd->pd_disk_pos == id)
850 			break;
851 	}
852 	return (disk);
853 }
854 
855 static int
856 g_raid_md_intel_supported(int level, int qual, int disks, int force)
857 {
858 
859 	switch (level) {
860 	case G_RAID_VOLUME_RL_RAID0:
861 		if (disks < 1)
862 			return (0);
863 		if (!force && (disks < 2 || disks > 6))
864 			return (0);
865 		break;
866 	case G_RAID_VOLUME_RL_RAID1:
867 		if (disks < 1)
868 			return (0);
869 		if (!force && (disks != 2))
870 			return (0);
871 		break;
872 	case G_RAID_VOLUME_RL_RAID1E:
873 		if (disks < 2)
874 			return (0);
875 		if (!force && (disks != 4))
876 			return (0);
877 		break;
878 	case G_RAID_VOLUME_RL_RAID5:
879 		if (disks < 3)
880 			return (0);
881 		if (!force && disks > 6)
882 			return (0);
883 		if (qual != G_RAID_VOLUME_RLQ_R5LA)
884 			return (0);
885 		break;
886 	default:
887 		return (0);
888 	}
889 	if (level != G_RAID_VOLUME_RL_RAID5 && qual != G_RAID_VOLUME_RLQ_NONE)
890 		return (0);
891 	return (1);
892 }
893 
894 static struct g_raid_volume *
895 g_raid_md_intel_get_volume(struct g_raid_softc *sc, int id)
896 {
897 	struct g_raid_volume	*mvol;
898 	struct g_raid_md_intel_pervolume *pv;
899 
900 	TAILQ_FOREACH(mvol, &sc->sc_volumes, v_next) {
901 		pv = mvol->v_md_data;
902 		if (pv->pv_volume_pos == id)
903 			break;
904 	}
905 	return (mvol);
906 }
907 
908 static int
909 g_raid_md_intel_start_disk(struct g_raid_disk *disk)
910 {
911 	struct g_raid_softc *sc;
912 	struct g_raid_subdisk *sd, *tmpsd;
913 	struct g_raid_disk *olddisk, *tmpdisk;
914 	struct g_raid_md_object *md;
915 	struct g_raid_md_intel_object *mdi;
916 	struct g_raid_md_intel_pervolume *pv;
917 	struct g_raid_md_intel_perdisk *pd, *oldpd;
918 	struct intel_raid_conf *meta;
919 	struct intel_raid_vol *mvol;
920 	struct intel_raid_map *mmap0, *mmap1;
921 	int disk_pos, resurrection = 0, migr_global, i;
922 
923 	sc = disk->d_softc;
924 	md = sc->sc_md;
925 	mdi = (struct g_raid_md_intel_object *)md;
926 	meta = mdi->mdio_meta;
927 	pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
928 	olddisk = NULL;
929 
930 	/* Find disk position in metadata by its serial. */
931 	disk_pos = intel_meta_find_disk(meta, pd->pd_disk_meta.serial);
932 	if (disk_pos < 0) {
933 		G_RAID_DEBUG1(1, sc, "Unknown, probably new or stale disk");
934 		/* Failed stale disk is useless for us. */
935 		if ((pd->pd_disk_meta.flags & INTEL_F_FAILED) &&
936 		    !(pd->pd_disk_meta.flags & INTEL_F_DISABLED)) {
937 			g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE_FAILED);
938 			return (0);
939 		}
940 		/* If we are in the start process, that's all for now. */
941 		if (!mdi->mdio_started)
942 			goto nofit;
943 		/*
944 		 * If we have already started - try to get use of the disk.
945 		 * Try to replace OFFLINE disks first, then FAILED.
946 		 */
947 		TAILQ_FOREACH(tmpdisk, &sc->sc_disks, d_next) {
948 			if (tmpdisk->d_state != G_RAID_DISK_S_OFFLINE &&
949 			    tmpdisk->d_state != G_RAID_DISK_S_FAILED)
950 				continue;
951 			/* Make sure this disk is big enough. */
952 			TAILQ_FOREACH(sd, &tmpdisk->d_subdisks, sd_next) {
953 				off_t disk_sectors =
954 				    intel_get_disk_sectors(&pd->pd_disk_meta);
955 
956 				if (sd->sd_offset + sd->sd_size + 4096 >
957 				    disk_sectors * 512) {
958 					G_RAID_DEBUG1(1, sc,
959 					    "Disk too small (%llu < %llu)",
960 					    (unsigned long long)
961 					    disk_sectors * 512,
962 					    (unsigned long long)
963 					    sd->sd_offset + sd->sd_size + 4096);
964 					break;
965 				}
966 			}
967 			if (sd != NULL)
968 				continue;
969 			if (tmpdisk->d_state == G_RAID_DISK_S_OFFLINE) {
970 				olddisk = tmpdisk;
971 				break;
972 			} else if (olddisk == NULL)
973 				olddisk = tmpdisk;
974 		}
975 		if (olddisk == NULL) {
976 nofit:
977 			if (pd->pd_disk_meta.flags & INTEL_F_SPARE) {
978 				g_raid_change_disk_state(disk,
979 				    G_RAID_DISK_S_SPARE);
980 				return (1);
981 			} else {
982 				g_raid_change_disk_state(disk,
983 				    G_RAID_DISK_S_STALE);
984 				return (0);
985 			}
986 		}
987 		oldpd = (struct g_raid_md_intel_perdisk *)olddisk->d_md_data;
988 		disk_pos = oldpd->pd_disk_pos;
989 		resurrection = 1;
990 	}
991 
992 	if (olddisk == NULL) {
993 		/* Find placeholder by position. */
994 		olddisk = g_raid_md_intel_get_disk(sc, disk_pos);
995 		if (olddisk == NULL)
996 			panic("No disk at position %d!", disk_pos);
997 		if (olddisk->d_state != G_RAID_DISK_S_OFFLINE) {
998 			G_RAID_DEBUG1(1, sc, "More than one disk for pos %d",
999 			    disk_pos);
1000 			g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE);
1001 			return (0);
1002 		}
1003 		oldpd = (struct g_raid_md_intel_perdisk *)olddisk->d_md_data;
1004 	}
1005 
1006 	/* Replace failed disk or placeholder with new disk. */
1007 	TAILQ_FOREACH_SAFE(sd, &olddisk->d_subdisks, sd_next, tmpsd) {
1008 		TAILQ_REMOVE(&olddisk->d_subdisks, sd, sd_next);
1009 		TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
1010 		sd->sd_disk = disk;
1011 	}
1012 	oldpd->pd_disk_pos = -2;
1013 	pd->pd_disk_pos = disk_pos;
1014 
1015 	/* If it was placeholder -- destroy it. */
1016 	if (olddisk->d_state == G_RAID_DISK_S_OFFLINE) {
1017 		g_raid_destroy_disk(olddisk);
1018 	} else {
1019 		/* Otherwise, make it STALE_FAILED. */
1020 		g_raid_change_disk_state(olddisk, G_RAID_DISK_S_STALE_FAILED);
1021 		/* Update global metadata just in case. */
1022 		memcpy(&meta->disk[disk_pos], &pd->pd_disk_meta,
1023 		    sizeof(struct intel_raid_disk));
1024 	}
1025 
1026 	/* Welcome the new disk. */
1027 	if ((meta->disk[disk_pos].flags & INTEL_F_DISABLED) &&
1028 	    !(pd->pd_disk_meta.flags & INTEL_F_SPARE))
1029 		g_raid_change_disk_state(disk, G_RAID_DISK_S_DISABLED);
1030 	else if (resurrection)
1031 		g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
1032 	else if (meta->disk[disk_pos].flags & INTEL_F_FAILED)
1033 		g_raid_change_disk_state(disk, G_RAID_DISK_S_FAILED);
1034 	else if (meta->disk[disk_pos].flags & INTEL_F_SPARE)
1035 		g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE);
1036 	else
1037 		g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
1038 	TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
1039 		pv = sd->sd_volume->v_md_data;
1040 		mvol = intel_get_volume(meta, pv->pv_volume_pos);
1041 		mmap0 = intel_get_map(mvol, 0);
1042 		if (mvol->migr_state)
1043 			mmap1 = intel_get_map(mvol, 1);
1044 		else
1045 			mmap1 = mmap0;
1046 
1047 		migr_global = 1;
1048 		for (i = 0; i < mmap0->total_disks; i++) {
1049 			if ((mmap0->disk_idx[i] & INTEL_DI_RBLD) == 0 &&
1050 			    (mmap1->disk_idx[i] & INTEL_DI_RBLD) != 0)
1051 				migr_global = 0;
1052 		}
1053 
1054 		if ((meta->disk[disk_pos].flags & INTEL_F_DISABLED) &&
1055 		    !(pd->pd_disk_meta.flags & INTEL_F_SPARE)) {
1056 			/* Disabled disk, useless. */
1057 			g_raid_change_subdisk_state(sd,
1058 			    G_RAID_SUBDISK_S_NONE);
1059 		} else if (resurrection) {
1060 			/* Stale disk, almost same as new. */
1061 			g_raid_change_subdisk_state(sd,
1062 			    G_RAID_SUBDISK_S_NEW);
1063 		} else if (meta->disk[disk_pos].flags & INTEL_F_FAILED) {
1064 			/* Failed disk, almost useless. */
1065 			g_raid_change_subdisk_state(sd,
1066 			    G_RAID_SUBDISK_S_FAILED);
1067 		} else if (mvol->migr_state == 0) {
1068 			if (mmap0->status == INTEL_S_UNINITIALIZED &&
1069 			    (!pv->pv_cng || pv->pv_cng_master_disk != disk_pos)) {
1070 				/* Freshly created uninitialized volume. */
1071 				g_raid_change_subdisk_state(sd,
1072 				    G_RAID_SUBDISK_S_UNINITIALIZED);
1073 			} else if (mmap0->disk_idx[sd->sd_pos] & INTEL_DI_RBLD) {
1074 				/* Freshly inserted disk. */
1075 				g_raid_change_subdisk_state(sd,
1076 				    G_RAID_SUBDISK_S_NEW);
1077 			} else if (mvol->dirty && (!pv->pv_cng ||
1078 			    pv->pv_cng_master_disk != disk_pos)) {
1079 				/* Dirty volume (unclean shutdown). */
1080 				g_raid_change_subdisk_state(sd,
1081 				    G_RAID_SUBDISK_S_STALE);
1082 			} else {
1083 				/* Up to date disk. */
1084 				g_raid_change_subdisk_state(sd,
1085 				    G_RAID_SUBDISK_S_ACTIVE);
1086 			}
1087 		} else if (mvol->migr_type == INTEL_MT_INIT ||
1088 			   mvol->migr_type == INTEL_MT_REBUILD) {
1089 			if (mmap0->disk_idx[sd->sd_pos] & INTEL_DI_RBLD) {
1090 				/* Freshly inserted disk. */
1091 				g_raid_change_subdisk_state(sd,
1092 				    G_RAID_SUBDISK_S_NEW);
1093 			} else if (mmap1->disk_idx[sd->sd_pos] & INTEL_DI_RBLD) {
1094 				/* Rebuilding disk. */
1095 				g_raid_change_subdisk_state(sd,
1096 				    G_RAID_SUBDISK_S_REBUILD);
1097 				if (mvol->dirty) {
1098 					sd->sd_rebuild_pos = 0;
1099 				} else {
1100 					sd->sd_rebuild_pos =
1101 					    intel_get_vol_curr_migr_unit(mvol) *
1102 					    sd->sd_volume->v_strip_size *
1103 					    mmap0->total_domains;
1104 				}
1105 			} else if (mvol->migr_type == INTEL_MT_INIT &&
1106 			    migr_global) {
1107 				/* Freshly created uninitialized volume. */
1108 				g_raid_change_subdisk_state(sd,
1109 				    G_RAID_SUBDISK_S_UNINITIALIZED);
1110 			} else if (mvol->dirty && (!pv->pv_cng ||
1111 			    pv->pv_cng_master_disk != disk_pos)) {
1112 				/* Dirty volume (unclean shutdown). */
1113 				g_raid_change_subdisk_state(sd,
1114 				    G_RAID_SUBDISK_S_STALE);
1115 			} else {
1116 				/* Up to date disk. */
1117 				g_raid_change_subdisk_state(sd,
1118 				    G_RAID_SUBDISK_S_ACTIVE);
1119 			}
1120 		} else if (mvol->migr_type == INTEL_MT_VERIFY ||
1121 			   mvol->migr_type == INTEL_MT_REPAIR) {
1122 			if (mmap0->disk_idx[sd->sd_pos] & INTEL_DI_RBLD) {
1123 				/* Freshly inserted disk. */
1124 				g_raid_change_subdisk_state(sd,
1125 				    G_RAID_SUBDISK_S_NEW);
1126 			} else if ((mmap1->disk_idx[sd->sd_pos] & INTEL_DI_RBLD) ||
1127 			    migr_global) {
1128 				/* Resyncing disk. */
1129 				g_raid_change_subdisk_state(sd,
1130 				    G_RAID_SUBDISK_S_RESYNC);
1131 				if (mvol->dirty) {
1132 					sd->sd_rebuild_pos = 0;
1133 				} else {
1134 					sd->sd_rebuild_pos =
1135 					    intel_get_vol_curr_migr_unit(mvol) *
1136 					    sd->sd_volume->v_strip_size *
1137 					    mmap0->total_domains;
1138 				}
1139 			} else if (mvol->dirty) {
1140 				/* Dirty volume (unclean shutdown). */
1141 				g_raid_change_subdisk_state(sd,
1142 				    G_RAID_SUBDISK_S_STALE);
1143 			} else {
1144 				/* Up to date disk. */
1145 				g_raid_change_subdisk_state(sd,
1146 				    G_RAID_SUBDISK_S_ACTIVE);
1147 			}
1148 		} else if (mvol->migr_type == INTEL_MT_GEN_MIGR) {
1149 			if ((mmap1->disk_idx[0] & INTEL_DI_IDX) != disk_pos) {
1150 				/* Freshly inserted disk. */
1151 				g_raid_change_subdisk_state(sd,
1152 				    G_RAID_SUBDISK_S_NEW);
1153 			} else {
1154 				/* Up to date disk. */
1155 				g_raid_change_subdisk_state(sd,
1156 				    G_RAID_SUBDISK_S_ACTIVE);
1157 			}
1158 		}
1159 		g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
1160 		    G_RAID_EVENT_SUBDISK);
1161 	}
1162 
1163 	/* Update status of our need for spare. */
1164 	if (mdi->mdio_started) {
1165 		mdi->mdio_incomplete =
1166 		    (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) +
1167 		     g_raid_ndisks(sc, G_RAID_DISK_S_DISABLED) <
1168 		     meta->total_disks);
1169 	}
1170 
1171 	return (resurrection);
1172 }
1173 
1174 static void
1175 g_disk_md_intel_retaste(void *arg, int pending)
1176 {
1177 
1178 	G_RAID_DEBUG(1, "Array is not complete, trying to retaste.");
1179 	g_retaste(&g_raid_class);
1180 	free(arg, M_MD_INTEL);
1181 }
1182 
1183 static void
1184 g_raid_md_intel_refill(struct g_raid_softc *sc)
1185 {
1186 	struct g_raid_md_object *md;
1187 	struct g_raid_md_intel_object *mdi;
1188 	struct intel_raid_conf *meta;
1189 	struct g_raid_disk *disk;
1190 	struct task *task;
1191 	int update, na;
1192 
1193 	md = sc->sc_md;
1194 	mdi = (struct g_raid_md_intel_object *)md;
1195 	meta = mdi->mdio_meta;
1196 	update = 0;
1197 	do {
1198 		/* Make sure we miss anything. */
1199 		na = g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) +
1200 		    g_raid_ndisks(sc, G_RAID_DISK_S_DISABLED);
1201 		if (na == meta->total_disks)
1202 			break;
1203 
1204 		G_RAID_DEBUG1(1, md->mdo_softc,
1205 		    "Array is not complete (%d of %d), "
1206 		    "trying to refill.", na, meta->total_disks);
1207 
1208 		/* Try to get use some of STALE disks. */
1209 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1210 			if (disk->d_state == G_RAID_DISK_S_STALE) {
1211 				update += g_raid_md_intel_start_disk(disk);
1212 				if (disk->d_state == G_RAID_DISK_S_ACTIVE ||
1213 				    disk->d_state == G_RAID_DISK_S_DISABLED)
1214 					break;
1215 			}
1216 		}
1217 		if (disk != NULL)
1218 			continue;
1219 
1220 		/* Try to get use some of SPARE disks. */
1221 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1222 			if (disk->d_state == G_RAID_DISK_S_SPARE) {
1223 				update += g_raid_md_intel_start_disk(disk);
1224 				if (disk->d_state == G_RAID_DISK_S_ACTIVE)
1225 					break;
1226 			}
1227 		}
1228 	} while (disk != NULL);
1229 
1230 	/* Write new metadata if we changed something. */
1231 	if (update) {
1232 		g_raid_md_write_intel(md, NULL, NULL, NULL);
1233 		meta = mdi->mdio_meta;
1234 	}
1235 
1236 	/* Update status of our need for spare. */
1237 	mdi->mdio_incomplete = (g_raid_ndisks(sc, G_RAID_DISK_S_ACTIVE) +
1238 	    g_raid_ndisks(sc, G_RAID_DISK_S_DISABLED) < meta->total_disks);
1239 
1240 	/* Request retaste hoping to find spare. */
1241 	if (mdi->mdio_incomplete) {
1242 		task = malloc(sizeof(struct task),
1243 		    M_MD_INTEL, M_WAITOK | M_ZERO);
1244 		TASK_INIT(task, 0, g_disk_md_intel_retaste, task);
1245 		taskqueue_enqueue(taskqueue_swi, task);
1246 	}
1247 }
1248 
1249 static void
1250 g_raid_md_intel_start(struct g_raid_softc *sc)
1251 {
1252 	struct g_raid_md_object *md;
1253 	struct g_raid_md_intel_object *mdi;
1254 	struct g_raid_md_intel_pervolume *pv;
1255 	struct g_raid_md_intel_perdisk *pd;
1256 	struct intel_raid_conf *meta;
1257 	struct intel_raid_vol *mvol;
1258 	struct intel_raid_map *mmap;
1259 	struct g_raid_volume *vol;
1260 	struct g_raid_subdisk *sd;
1261 	struct g_raid_disk *disk;
1262 	int i, j, disk_pos;
1263 
1264 	md = sc->sc_md;
1265 	mdi = (struct g_raid_md_intel_object *)md;
1266 	meta = mdi->mdio_meta;
1267 
1268 	/* Create volumes and subdisks. */
1269 	for (i = 0; i < meta->total_volumes; i++) {
1270 		mvol = intel_get_volume(meta, i);
1271 		mmap = intel_get_map(mvol, 0);
1272 		vol = g_raid_create_volume(sc, mvol->name, mvol->tid - 1);
1273 		pv = malloc(sizeof(*pv), M_MD_INTEL, M_WAITOK | M_ZERO);
1274 		pv->pv_volume_pos = i;
1275 		pv->pv_cng = (mvol->state & INTEL_ST_CLONE_N_GO) != 0;
1276 		pv->pv_cng_man_sync = (mvol->state & INTEL_ST_CLONE_MAN_SYNC) != 0;
1277 		if (mvol->cng_master_disk < mmap->total_disks)
1278 			pv->pv_cng_master_disk = mvol->cng_master_disk;
1279 		vol->v_md_data = pv;
1280 		vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_NONE;
1281 		if (mmap->type == INTEL_T_RAID0)
1282 			vol->v_raid_level = G_RAID_VOLUME_RL_RAID0;
1283 		else if (mmap->type == INTEL_T_RAID1 &&
1284 		    mmap->total_domains >= 2 &&
1285 		    mmap->total_domains <= mmap->total_disks) {
1286 			/* Assume total_domains is correct. */
1287 			if (mmap->total_domains == mmap->total_disks)
1288 				vol->v_raid_level = G_RAID_VOLUME_RL_RAID1;
1289 			else
1290 				vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E;
1291 		} else if (mmap->type == INTEL_T_RAID1) {
1292 			/* total_domains looks wrong. */
1293 			if (mmap->total_disks <= 2)
1294 				vol->v_raid_level = G_RAID_VOLUME_RL_RAID1;
1295 			else
1296 				vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E;
1297 		} else if (mmap->type == INTEL_T_RAID5) {
1298 			vol->v_raid_level = G_RAID_VOLUME_RL_RAID5;
1299 			vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_R5LA;
1300 		} else
1301 			vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
1302 		vol->v_strip_size = (u_int)mmap->strip_sectors * 512; //ZZZ
1303 		vol->v_disks_count = mmap->total_disks;
1304 		vol->v_mediasize = (off_t)mvol->total_sectors * 512; //ZZZ
1305 		vol->v_sectorsize = 512; //ZZZ
1306 		for (j = 0; j < vol->v_disks_count; j++) {
1307 			sd = &vol->v_subdisks[j];
1308 			sd->sd_offset = intel_get_map_offset(mmap) * 512; //ZZZ
1309 			sd->sd_size = intel_get_map_disk_sectors(mmap) * 512; //ZZZ
1310 		}
1311 		g_raid_start_volume(vol);
1312 	}
1313 
1314 	/* Create disk placeholders to store data for later writing. */
1315 	for (disk_pos = 0; disk_pos < meta->total_disks; disk_pos++) {
1316 		pd = malloc(sizeof(*pd), M_MD_INTEL, M_WAITOK | M_ZERO);
1317 		pd->pd_disk_pos = disk_pos;
1318 		pd->pd_disk_meta = meta->disk[disk_pos];
1319 		disk = g_raid_create_disk(sc);
1320 		disk->d_md_data = (void *)pd;
1321 		disk->d_state = G_RAID_DISK_S_OFFLINE;
1322 		for (i = 0; i < meta->total_volumes; i++) {
1323 			mvol = intel_get_volume(meta, i);
1324 			mmap = intel_get_map(mvol, 0);
1325 			for (j = 0; j < mmap->total_disks; j++) {
1326 				if ((mmap->disk_idx[j] & INTEL_DI_IDX) == disk_pos)
1327 					break;
1328 			}
1329 			if (j == mmap->total_disks)
1330 				continue;
1331 			vol = g_raid_md_intel_get_volume(sc, i);
1332 			sd = &vol->v_subdisks[j];
1333 			sd->sd_disk = disk;
1334 			TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
1335 		}
1336 	}
1337 
1338 	/* Make all disks found till the moment take their places. */
1339 	do {
1340 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1341 			if (disk->d_state == G_RAID_DISK_S_NONE) {
1342 				g_raid_md_intel_start_disk(disk);
1343 				break;
1344 			}
1345 		}
1346 	} while (disk != NULL);
1347 
1348 	mdi->mdio_started = 1;
1349 	G_RAID_DEBUG1(0, sc, "Array started.");
1350 	g_raid_md_write_intel(md, NULL, NULL, NULL);
1351 
1352 	/* Pickup any STALE/SPARE disks to refill array if needed. */
1353 	g_raid_md_intel_refill(sc);
1354 
1355 	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1356 		g_raid_event_send(vol, G_RAID_VOLUME_E_START,
1357 		    G_RAID_EVENT_VOLUME);
1358 	}
1359 
1360 	callout_stop(&mdi->mdio_start_co);
1361 	G_RAID_DEBUG1(1, sc, "root_mount_rel %p", mdi->mdio_rootmount);
1362 	root_mount_rel(mdi->mdio_rootmount);
1363 	mdi->mdio_rootmount = NULL;
1364 }
1365 
1366 static void
1367 g_raid_md_intel_new_disk(struct g_raid_disk *disk)
1368 {
1369 	struct g_raid_softc *sc;
1370 	struct g_raid_md_object *md;
1371 	struct g_raid_md_intel_object *mdi;
1372 	struct intel_raid_conf *pdmeta;
1373 	struct g_raid_md_intel_perdisk *pd;
1374 
1375 	sc = disk->d_softc;
1376 	md = sc->sc_md;
1377 	mdi = (struct g_raid_md_intel_object *)md;
1378 	pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
1379 	pdmeta = pd->pd_meta;
1380 
1381 	if (mdi->mdio_started) {
1382 		if (g_raid_md_intel_start_disk(disk))
1383 			g_raid_md_write_intel(md, NULL, NULL, NULL);
1384 	} else {
1385 		/* If we haven't started yet - check metadata freshness. */
1386 		if (mdi->mdio_meta == NULL ||
1387 		    ((int32_t)(pdmeta->generation - mdi->mdio_generation)) > 0) {
1388 			G_RAID_DEBUG1(1, sc, "Newer disk");
1389 			if (mdi->mdio_meta != NULL)
1390 				free(mdi->mdio_meta, M_MD_INTEL);
1391 			mdi->mdio_meta = intel_meta_copy(pdmeta);
1392 			mdi->mdio_generation = mdi->mdio_meta->generation;
1393 			mdi->mdio_disks_present = 1;
1394 		} else if (pdmeta->generation == mdi->mdio_generation) {
1395 			mdi->mdio_disks_present++;
1396 			G_RAID_DEBUG1(1, sc, "Matching disk (%d of %d up)",
1397 			    mdi->mdio_disks_present,
1398 			    mdi->mdio_meta->total_disks);
1399 		} else {
1400 			G_RAID_DEBUG1(1, sc, "Older disk");
1401 		}
1402 		/* If we collected all needed disks - start array. */
1403 		if (mdi->mdio_disks_present == mdi->mdio_meta->total_disks)
1404 			g_raid_md_intel_start(sc);
1405 	}
1406 }
1407 
1408 static void
1409 g_raid_intel_go(void *arg)
1410 {
1411 	struct g_raid_softc *sc;
1412 	struct g_raid_md_object *md;
1413 	struct g_raid_md_intel_object *mdi;
1414 
1415 	sc = arg;
1416 	md = sc->sc_md;
1417 	mdi = (struct g_raid_md_intel_object *)md;
1418 	if (!mdi->mdio_started) {
1419 		G_RAID_DEBUG1(0, sc, "Force array start due to timeout.");
1420 		g_raid_event_send(sc, G_RAID_NODE_E_START, 0);
1421 	}
1422 }
1423 
1424 static int
1425 g_raid_md_create_intel(struct g_raid_md_object *md, struct g_class *mp,
1426     struct g_geom **gp)
1427 {
1428 	struct g_raid_softc *sc;
1429 	struct g_raid_md_intel_object *mdi;
1430 	char name[16];
1431 
1432 	mdi = (struct g_raid_md_intel_object *)md;
1433 	mdi->mdio_config_id = mdi->mdio_orig_config_id = arc4random();
1434 	mdi->mdio_generation = 0;
1435 	snprintf(name, sizeof(name), "Intel-%08x", mdi->mdio_config_id);
1436 	sc = g_raid_create_node(mp, name, md);
1437 	if (sc == NULL)
1438 		return (G_RAID_MD_TASTE_FAIL);
1439 	md->mdo_softc = sc;
1440 	*gp = sc->sc_geom;
1441 	return (G_RAID_MD_TASTE_NEW);
1442 }
1443 
1444 /*
1445  * Return the last N characters of the serial label.  The Linux and
1446  * ataraid(7) code always uses the last 16 characters of the label to
1447  * store into the Intel meta format.  Generalize this to N characters
1448  * since that's easy.  Labels can be up to 20 characters for SATA drives
1449  * and up 251 characters for SAS drives.  Since intel controllers don't
1450  * support SAS drives, just stick with the SATA limits for stack friendliness.
1451  */
1452 static int
1453 g_raid_md_get_label(struct g_consumer *cp, char *serial, int serlen)
1454 {
1455 	char serial_buffer[DISK_IDENT_SIZE];
1456 	int len, error;
1457 
1458 	len = sizeof(serial_buffer);
1459 	error = g_io_getattr("GEOM::ident", cp, &len, serial_buffer);
1460 	if (error != 0)
1461 		return (error);
1462 	len = strlen(serial_buffer);
1463 	if (len > serlen)
1464 		len -= serlen;
1465 	else
1466 		len = 0;
1467 	strncpy(serial, serial_buffer + len, serlen);
1468 	return (0);
1469 }
1470 
1471 static int
1472 g_raid_md_taste_intel(struct g_raid_md_object *md, struct g_class *mp,
1473                               struct g_consumer *cp, struct g_geom **gp)
1474 {
1475 	struct g_consumer *rcp;
1476 	struct g_provider *pp;
1477 	struct g_raid_md_intel_object *mdi, *mdi1;
1478 	struct g_raid_softc *sc;
1479 	struct g_raid_disk *disk;
1480 	struct intel_raid_conf *meta;
1481 	struct g_raid_md_intel_perdisk *pd;
1482 	struct g_geom *geom;
1483 	int error, disk_pos, result, spare, len;
1484 	char serial[INTEL_SERIAL_LEN];
1485 	char name[16];
1486 	uint16_t vendor;
1487 
1488 	G_RAID_DEBUG(1, "Tasting Intel on %s", cp->provider->name);
1489 	mdi = (struct g_raid_md_intel_object *)md;
1490 	pp = cp->provider;
1491 
1492 	/* Read metadata from device. */
1493 	meta = NULL;
1494 	disk_pos = 0;
1495 	g_topology_unlock();
1496 	error = g_raid_md_get_label(cp, serial, sizeof(serial));
1497 	if (error != 0) {
1498 		G_RAID_DEBUG(1, "Cannot get serial number from %s (error=%d).",
1499 		    pp->name, error);
1500 		goto fail2;
1501 	}
1502 	vendor = 0xffff;
1503 	len = sizeof(vendor);
1504 	if (pp->geom->rank == 1)
1505 		g_io_getattr("GEOM::hba_vendor", cp, &len, &vendor);
1506 	meta = intel_meta_read(cp);
1507 	g_topology_lock();
1508 	if (meta == NULL) {
1509 		if (g_raid_aggressive_spare) {
1510 			if (vendor != 0x8086) {
1511 				G_RAID_DEBUG(1,
1512 				    "Intel vendor mismatch 0x%04x != 0x8086",
1513 				    vendor);
1514 			} else {
1515 				G_RAID_DEBUG(1,
1516 				    "No Intel metadata, forcing spare.");
1517 				spare = 2;
1518 				goto search;
1519 			}
1520 		}
1521 		return (G_RAID_MD_TASTE_FAIL);
1522 	}
1523 
1524 	/* Check this disk position in obtained metadata. */
1525 	disk_pos = intel_meta_find_disk(meta, serial);
1526 	if (disk_pos < 0) {
1527 		G_RAID_DEBUG(1, "Intel serial '%s' not found", serial);
1528 		goto fail1;
1529 	}
1530 	if (intel_get_disk_sectors(&meta->disk[disk_pos]) !=
1531 	    (pp->mediasize / pp->sectorsize)) {
1532 		G_RAID_DEBUG(1, "Intel size mismatch %ju != %ju",
1533 		    intel_get_disk_sectors(&meta->disk[disk_pos]),
1534 		    (off_t)(pp->mediasize / pp->sectorsize));
1535 		goto fail1;
1536 	}
1537 
1538 	G_RAID_DEBUG(1, "Intel disk position %d", disk_pos);
1539 	spare = meta->disk[disk_pos].flags & INTEL_F_SPARE;
1540 
1541 search:
1542 	/* Search for matching node. */
1543 	sc = NULL;
1544 	mdi1 = NULL;
1545 	LIST_FOREACH(geom, &mp->geom, geom) {
1546 		sc = geom->softc;
1547 		if (sc == NULL)
1548 			continue;
1549 		if (sc->sc_stopping != 0)
1550 			continue;
1551 		if (sc->sc_md->mdo_class != md->mdo_class)
1552 			continue;
1553 		mdi1 = (struct g_raid_md_intel_object *)sc->sc_md;
1554 		if (spare) {
1555 			if (mdi1->mdio_incomplete)
1556 				break;
1557 		} else {
1558 			if (mdi1->mdio_config_id == meta->config_id)
1559 				break;
1560 		}
1561 	}
1562 
1563 	/* Found matching node. */
1564 	if (geom != NULL) {
1565 		G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name);
1566 		result = G_RAID_MD_TASTE_EXISTING;
1567 
1568 	} else if (spare) { /* Not found needy node -- left for later. */
1569 		G_RAID_DEBUG(1, "Spare is not needed at this time");
1570 		goto fail1;
1571 
1572 	} else { /* Not found matching node -- create one. */
1573 		result = G_RAID_MD_TASTE_NEW;
1574 		mdi->mdio_config_id = meta->config_id;
1575 		mdi->mdio_orig_config_id = meta->orig_config_id;
1576 		snprintf(name, sizeof(name), "Intel-%08x", meta->config_id);
1577 		sc = g_raid_create_node(mp, name, md);
1578 		md->mdo_softc = sc;
1579 		geom = sc->sc_geom;
1580 		callout_init(&mdi->mdio_start_co, 1);
1581 		callout_reset(&mdi->mdio_start_co, g_raid_start_timeout * hz,
1582 		    g_raid_intel_go, sc);
1583 		mdi->mdio_rootmount = root_mount_hold("GRAID-Intel");
1584 		G_RAID_DEBUG1(1, sc, "root_mount_hold %p", mdi->mdio_rootmount);
1585 	}
1586 
1587 	/* There is no return after this point, so we close passed consumer. */
1588 	g_access(cp, -1, 0, 0);
1589 
1590 	rcp = g_new_consumer(geom);
1591 	rcp->flags |= G_CF_DIRECT_RECEIVE;
1592 	g_attach(rcp, pp);
1593 	if (g_access(rcp, 1, 1, 1) != 0)
1594 		; //goto fail1;
1595 
1596 	g_topology_unlock();
1597 	sx_xlock(&sc->sc_lock);
1598 
1599 	pd = malloc(sizeof(*pd), M_MD_INTEL, M_WAITOK | M_ZERO);
1600 	pd->pd_meta = meta;
1601 	pd->pd_disk_pos = -1;
1602 	if (spare == 2) {
1603 		memcpy(&pd->pd_disk_meta.serial[0], serial, INTEL_SERIAL_LEN);
1604 		intel_set_disk_sectors(&pd->pd_disk_meta,
1605 		    pp->mediasize / pp->sectorsize);
1606 		pd->pd_disk_meta.id = 0;
1607 		pd->pd_disk_meta.flags = INTEL_F_SPARE;
1608 	} else {
1609 		pd->pd_disk_meta = meta->disk[disk_pos];
1610 	}
1611 	disk = g_raid_create_disk(sc);
1612 	disk->d_md_data = (void *)pd;
1613 	disk->d_consumer = rcp;
1614 	rcp->private = disk;
1615 
1616 	g_raid_get_disk_info(disk);
1617 
1618 	g_raid_md_intel_new_disk(disk);
1619 
1620 	sx_xunlock(&sc->sc_lock);
1621 	g_topology_lock();
1622 	*gp = geom;
1623 	return (result);
1624 fail2:
1625 	g_topology_lock();
1626 fail1:
1627 	free(meta, M_MD_INTEL);
1628 	return (G_RAID_MD_TASTE_FAIL);
1629 }
1630 
1631 static int
1632 g_raid_md_event_intel(struct g_raid_md_object *md,
1633     struct g_raid_disk *disk, u_int event)
1634 {
1635 	struct g_raid_softc *sc;
1636 	struct g_raid_subdisk *sd;
1637 	struct g_raid_md_intel_object *mdi;
1638 	struct g_raid_md_intel_perdisk *pd;
1639 
1640 	sc = md->mdo_softc;
1641 	mdi = (struct g_raid_md_intel_object *)md;
1642 	if (disk == NULL) {
1643 		switch (event) {
1644 		case G_RAID_NODE_E_START:
1645 			if (!mdi->mdio_started)
1646 				g_raid_md_intel_start(sc);
1647 			return (0);
1648 		}
1649 		return (-1);
1650 	}
1651 	pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
1652 	switch (event) {
1653 	case G_RAID_DISK_E_DISCONNECTED:
1654 		/* If disk was assigned, just update statuses. */
1655 		if (pd->pd_disk_pos >= 0) {
1656 			g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
1657 			if (disk->d_consumer) {
1658 				g_raid_kill_consumer(sc, disk->d_consumer);
1659 				disk->d_consumer = NULL;
1660 			}
1661 			TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
1662 				g_raid_change_subdisk_state(sd,
1663 				    G_RAID_SUBDISK_S_NONE);
1664 				g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
1665 				    G_RAID_EVENT_SUBDISK);
1666 			}
1667 		} else {
1668 			/* Otherwise -- delete. */
1669 			g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
1670 			g_raid_destroy_disk(disk);
1671 		}
1672 
1673 		/* Write updated metadata to all disks. */
1674 		g_raid_md_write_intel(md, NULL, NULL, NULL);
1675 
1676 		/* Check if anything left except placeholders. */
1677 		if (g_raid_ndisks(sc, -1) ==
1678 		    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
1679 			g_raid_destroy_node(sc, 0);
1680 		else
1681 			g_raid_md_intel_refill(sc);
1682 		return (0);
1683 	}
1684 	return (-2);
1685 }
1686 
1687 static int
1688 g_raid_md_ctl_intel(struct g_raid_md_object *md,
1689     struct gctl_req *req)
1690 {
1691 	struct g_raid_softc *sc;
1692 	struct g_raid_volume *vol, *vol1;
1693 	struct g_raid_subdisk *sd;
1694 	struct g_raid_disk *disk;
1695 	struct g_raid_md_intel_object *mdi;
1696 	struct g_raid_md_intel_pervolume *pv;
1697 	struct g_raid_md_intel_perdisk *pd;
1698 	struct g_consumer *cp;
1699 	struct g_provider *pp;
1700 	char arg[16], serial[INTEL_SERIAL_LEN];
1701 	const char *nodename, *verb, *volname, *levelname, *diskname;
1702 	char *tmp;
1703 	int *nargs, *force;
1704 	off_t off, size, sectorsize, strip, disk_sectors;
1705 	intmax_t *sizearg, *striparg;
1706 	int numdisks, i, len, level, qual, update;
1707 	int error;
1708 
1709 	sc = md->mdo_softc;
1710 	mdi = (struct g_raid_md_intel_object *)md;
1711 	verb = gctl_get_param(req, "verb", NULL);
1712 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1713 	error = 0;
1714 	if (strcmp(verb, "label") == 0) {
1715 		if (*nargs < 4) {
1716 			gctl_error(req, "Invalid number of arguments.");
1717 			return (-1);
1718 		}
1719 		volname = gctl_get_asciiparam(req, "arg1");
1720 		if (volname == NULL) {
1721 			gctl_error(req, "No volume name.");
1722 			return (-2);
1723 		}
1724 		levelname = gctl_get_asciiparam(req, "arg2");
1725 		if (levelname == NULL) {
1726 			gctl_error(req, "No RAID level.");
1727 			return (-3);
1728 		}
1729 		if (strcasecmp(levelname, "RAID5") == 0)
1730 			levelname = "RAID5-LA";
1731 		if (g_raid_volume_str2level(levelname, &level, &qual)) {
1732 			gctl_error(req, "Unknown RAID level '%s'.", levelname);
1733 			return (-4);
1734 		}
1735 		numdisks = *nargs - 3;
1736 		force = gctl_get_paraml(req, "force", sizeof(*force));
1737 		if (!g_raid_md_intel_supported(level, qual, numdisks,
1738 		    force ? *force : 0)) {
1739 			gctl_error(req, "Unsupported RAID level "
1740 			    "(0x%02x/0x%02x), or number of disks (%d).",
1741 			    level, qual, numdisks);
1742 			return (-5);
1743 		}
1744 
1745 		/* Search for disks, connect them and probe. */
1746 		size = 0x7fffffffffffffffllu;
1747 		sectorsize = 0;
1748 		for (i = 0; i < numdisks; i++) {
1749 			snprintf(arg, sizeof(arg), "arg%d", i + 3);
1750 			diskname = gctl_get_asciiparam(req, arg);
1751 			if (diskname == NULL) {
1752 				gctl_error(req, "No disk name (%s).", arg);
1753 				error = -6;
1754 				break;
1755 			}
1756 			if (strcmp(diskname, "NONE") == 0) {
1757 				cp = NULL;
1758 				pp = NULL;
1759 			} else {
1760 				g_topology_lock();
1761 				cp = g_raid_open_consumer(sc, diskname);
1762 				if (cp == NULL) {
1763 					gctl_error(req, "Can't open disk '%s'.",
1764 					    diskname);
1765 					g_topology_unlock();
1766 					error = -7;
1767 					break;
1768 				}
1769 				pp = cp->provider;
1770 			}
1771 			pd = malloc(sizeof(*pd), M_MD_INTEL, M_WAITOK | M_ZERO);
1772 			pd->pd_disk_pos = i;
1773 			disk = g_raid_create_disk(sc);
1774 			disk->d_md_data = (void *)pd;
1775 			disk->d_consumer = cp;
1776 			if (cp == NULL) {
1777 				strcpy(&pd->pd_disk_meta.serial[0], "NONE");
1778 				pd->pd_disk_meta.id = 0xffffffff;
1779 				pd->pd_disk_meta.flags = INTEL_F_ASSIGNED;
1780 				continue;
1781 			}
1782 			cp->private = disk;
1783 			g_topology_unlock();
1784 
1785 			error = g_raid_md_get_label(cp,
1786 			    &pd->pd_disk_meta.serial[0], INTEL_SERIAL_LEN);
1787 			if (error != 0) {
1788 				gctl_error(req,
1789 				    "Can't get serial for provider '%s'.",
1790 				    diskname);
1791 				error = -8;
1792 				break;
1793 			}
1794 
1795 			g_raid_get_disk_info(disk);
1796 
1797 			intel_set_disk_sectors(&pd->pd_disk_meta,
1798 			    pp->mediasize / pp->sectorsize);
1799 			if (size > pp->mediasize)
1800 				size = pp->mediasize;
1801 			if (sectorsize < pp->sectorsize)
1802 				sectorsize = pp->sectorsize;
1803 			pd->pd_disk_meta.id = 0;
1804 			pd->pd_disk_meta.flags = INTEL_F_ASSIGNED | INTEL_F_ONLINE;
1805 		}
1806 		if (error != 0)
1807 			return (error);
1808 
1809 		if (sectorsize <= 0) {
1810 			gctl_error(req, "Can't get sector size.");
1811 			return (-8);
1812 		}
1813 
1814 		/* Reserve some space for metadata. */
1815 		size -= ((4096 + sectorsize - 1) / sectorsize) * sectorsize;
1816 
1817 		/* Handle size argument. */
1818 		len = sizeof(*sizearg);
1819 		sizearg = gctl_get_param(req, "size", &len);
1820 		if (sizearg != NULL && len == sizeof(*sizearg) &&
1821 		    *sizearg > 0) {
1822 			if (*sizearg > size) {
1823 				gctl_error(req, "Size too big %lld > %lld.",
1824 				    (long long)*sizearg, (long long)size);
1825 				return (-9);
1826 			}
1827 			size = *sizearg;
1828 		}
1829 
1830 		/* Handle strip argument. */
1831 		strip = 131072;
1832 		len = sizeof(*striparg);
1833 		striparg = gctl_get_param(req, "strip", &len);
1834 		if (striparg != NULL && len == sizeof(*striparg) &&
1835 		    *striparg > 0) {
1836 			if (*striparg < sectorsize) {
1837 				gctl_error(req, "Strip size too small.");
1838 				return (-10);
1839 			}
1840 			if (*striparg % sectorsize != 0) {
1841 				gctl_error(req, "Incorrect strip size.");
1842 				return (-11);
1843 			}
1844 			if (strip > 65535 * sectorsize) {
1845 				gctl_error(req, "Strip size too big.");
1846 				return (-12);
1847 			}
1848 			strip = *striparg;
1849 		}
1850 
1851 		/* Round size down to strip or sector. */
1852 		if (level == G_RAID_VOLUME_RL_RAID1)
1853 			size -= (size % sectorsize);
1854 		else if (level == G_RAID_VOLUME_RL_RAID1E &&
1855 		    (numdisks & 1) != 0)
1856 			size -= (size % (2 * strip));
1857 		else
1858 			size -= (size % strip);
1859 		if (size <= 0) {
1860 			gctl_error(req, "Size too small.");
1861 			return (-13);
1862 		}
1863 
1864 		/* We have all we need, create things: volume, ... */
1865 		mdi->mdio_started = 1;
1866 		vol = g_raid_create_volume(sc, volname, -1);
1867 		pv = malloc(sizeof(*pv), M_MD_INTEL, M_WAITOK | M_ZERO);
1868 		pv->pv_volume_pos = 0;
1869 		vol->v_md_data = pv;
1870 		vol->v_raid_level = level;
1871 		vol->v_raid_level_qualifier = qual;
1872 		vol->v_strip_size = strip;
1873 		vol->v_disks_count = numdisks;
1874 		if (level == G_RAID_VOLUME_RL_RAID0)
1875 			vol->v_mediasize = size * numdisks;
1876 		else if (level == G_RAID_VOLUME_RL_RAID1)
1877 			vol->v_mediasize = size;
1878 		else if (level == G_RAID_VOLUME_RL_RAID5)
1879 			vol->v_mediasize = size * (numdisks - 1);
1880 		else { /* RAID1E */
1881 			vol->v_mediasize = ((size * numdisks) / strip / 2) *
1882 			    strip;
1883 		}
1884 		vol->v_sectorsize = sectorsize;
1885 		g_raid_start_volume(vol);
1886 
1887 		/* , and subdisks. */
1888 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1889 			pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
1890 			sd = &vol->v_subdisks[pd->pd_disk_pos];
1891 			sd->sd_disk = disk;
1892 			sd->sd_offset = 0;
1893 			sd->sd_size = size;
1894 			TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
1895 			if (sd->sd_disk->d_consumer != NULL) {
1896 				g_raid_change_disk_state(disk,
1897 				    G_RAID_DISK_S_ACTIVE);
1898 				if (level == G_RAID_VOLUME_RL_RAID5)
1899 					g_raid_change_subdisk_state(sd,
1900 					    G_RAID_SUBDISK_S_UNINITIALIZED);
1901 				else
1902 					g_raid_change_subdisk_state(sd,
1903 					    G_RAID_SUBDISK_S_ACTIVE);
1904 				g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
1905 				    G_RAID_EVENT_SUBDISK);
1906 			} else {
1907 				g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
1908 			}
1909 		}
1910 
1911 		/* Write metadata based on created entities. */
1912 		G_RAID_DEBUG1(0, sc, "Array started.");
1913 		g_raid_md_write_intel(md, NULL, NULL, NULL);
1914 
1915 		/* Pickup any STALE/SPARE disks to refill array if needed. */
1916 		g_raid_md_intel_refill(sc);
1917 
1918 		g_raid_event_send(vol, G_RAID_VOLUME_E_START,
1919 		    G_RAID_EVENT_VOLUME);
1920 		return (0);
1921 	}
1922 	if (strcmp(verb, "add") == 0) {
1923 		if (*nargs != 3) {
1924 			gctl_error(req, "Invalid number of arguments.");
1925 			return (-1);
1926 		}
1927 		volname = gctl_get_asciiparam(req, "arg1");
1928 		if (volname == NULL) {
1929 			gctl_error(req, "No volume name.");
1930 			return (-2);
1931 		}
1932 		levelname = gctl_get_asciiparam(req, "arg2");
1933 		if (levelname == NULL) {
1934 			gctl_error(req, "No RAID level.");
1935 			return (-3);
1936 		}
1937 		if (strcasecmp(levelname, "RAID5") == 0)
1938 			levelname = "RAID5-LA";
1939 		if (g_raid_volume_str2level(levelname, &level, &qual)) {
1940 			gctl_error(req, "Unknown RAID level '%s'.", levelname);
1941 			return (-4);
1942 		}
1943 
1944 		/* Look for existing volumes. */
1945 		i = 0;
1946 		vol1 = NULL;
1947 		TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1948 			vol1 = vol;
1949 			i++;
1950 		}
1951 		if (i > 1) {
1952 			gctl_error(req, "Maximum two volumes supported.");
1953 			return (-6);
1954 		}
1955 		if (vol1 == NULL) {
1956 			gctl_error(req, "At least one volume must exist.");
1957 			return (-7);
1958 		}
1959 
1960 		numdisks = vol1->v_disks_count;
1961 		force = gctl_get_paraml(req, "force", sizeof(*force));
1962 		if (!g_raid_md_intel_supported(level, qual, numdisks,
1963 		    force ? *force : 0)) {
1964 			gctl_error(req, "Unsupported RAID level "
1965 			    "(0x%02x/0x%02x), or number of disks (%d).",
1966 			    level, qual, numdisks);
1967 			return (-5);
1968 		}
1969 
1970 		/* Collect info about present disks. */
1971 		size = 0x7fffffffffffffffllu;
1972 		sectorsize = 512;
1973 		for (i = 0; i < numdisks; i++) {
1974 			disk = vol1->v_subdisks[i].sd_disk;
1975 			pd = (struct g_raid_md_intel_perdisk *)
1976 			    disk->d_md_data;
1977 			disk_sectors =
1978 			    intel_get_disk_sectors(&pd->pd_disk_meta);
1979 
1980 			if (disk_sectors * 512 < size)
1981 				size = disk_sectors * 512;
1982 			if (disk->d_consumer != NULL &&
1983 			    disk->d_consumer->provider != NULL &&
1984 			    disk->d_consumer->provider->sectorsize >
1985 			     sectorsize) {
1986 				sectorsize =
1987 				    disk->d_consumer->provider->sectorsize;
1988 			}
1989 		}
1990 
1991 		/* Reserve some space for metadata. */
1992 		size -= ((4096 + sectorsize - 1) / sectorsize) * sectorsize;
1993 
1994 		/* Decide insert before or after. */
1995 		sd = &vol1->v_subdisks[0];
1996 		if (sd->sd_offset >
1997 		    size - (sd->sd_offset + sd->sd_size)) {
1998 			off = 0;
1999 			size = sd->sd_offset;
2000 		} else {
2001 			off = sd->sd_offset + sd->sd_size;
2002 			size = size - (sd->sd_offset + sd->sd_size);
2003 		}
2004 
2005 		/* Handle strip argument. */
2006 		strip = 131072;
2007 		len = sizeof(*striparg);
2008 		striparg = gctl_get_param(req, "strip", &len);
2009 		if (striparg != NULL && len == sizeof(*striparg) &&
2010 		    *striparg > 0) {
2011 			if (*striparg < sectorsize) {
2012 				gctl_error(req, "Strip size too small.");
2013 				return (-10);
2014 			}
2015 			if (*striparg % sectorsize != 0) {
2016 				gctl_error(req, "Incorrect strip size.");
2017 				return (-11);
2018 			}
2019 			if (strip > 65535 * sectorsize) {
2020 				gctl_error(req, "Strip size too big.");
2021 				return (-12);
2022 			}
2023 			strip = *striparg;
2024 		}
2025 
2026 		/* Round offset up to strip. */
2027 		if (off % strip != 0) {
2028 			size -= strip - off % strip;
2029 			off += strip - off % strip;
2030 		}
2031 
2032 		/* Handle size argument. */
2033 		len = sizeof(*sizearg);
2034 		sizearg = gctl_get_param(req, "size", &len);
2035 		if (sizearg != NULL && len == sizeof(*sizearg) &&
2036 		    *sizearg > 0) {
2037 			if (*sizearg > size) {
2038 				gctl_error(req, "Size too big %lld > %lld.",
2039 				    (long long)*sizearg, (long long)size);
2040 				return (-9);
2041 			}
2042 			size = *sizearg;
2043 		}
2044 
2045 		/* Round size down to strip or sector. */
2046 		if (level == G_RAID_VOLUME_RL_RAID1)
2047 			size -= (size % sectorsize);
2048 		else
2049 			size -= (size % strip);
2050 		if (size <= 0) {
2051 			gctl_error(req, "Size too small.");
2052 			return (-13);
2053 		}
2054 		if (size > 0xffffffffllu * sectorsize) {
2055 			gctl_error(req, "Size too big.");
2056 			return (-14);
2057 		}
2058 
2059 		/* We have all we need, create things: volume, ... */
2060 		vol = g_raid_create_volume(sc, volname, -1);
2061 		pv = malloc(sizeof(*pv), M_MD_INTEL, M_WAITOK | M_ZERO);
2062 		pv->pv_volume_pos = i;
2063 		vol->v_md_data = pv;
2064 		vol->v_raid_level = level;
2065 		vol->v_raid_level_qualifier = qual;
2066 		vol->v_strip_size = strip;
2067 		vol->v_disks_count = numdisks;
2068 		if (level == G_RAID_VOLUME_RL_RAID0)
2069 			vol->v_mediasize = size * numdisks;
2070 		else if (level == G_RAID_VOLUME_RL_RAID1)
2071 			vol->v_mediasize = size;
2072 		else if (level == G_RAID_VOLUME_RL_RAID5)
2073 			vol->v_mediasize = size * (numdisks - 1);
2074 		else { /* RAID1E */
2075 			vol->v_mediasize = ((size * numdisks) / strip / 2) *
2076 			    strip;
2077 		}
2078 		vol->v_sectorsize = sectorsize;
2079 		g_raid_start_volume(vol);
2080 
2081 		/* , and subdisks. */
2082 		for (i = 0; i < numdisks; i++) {
2083 			disk = vol1->v_subdisks[i].sd_disk;
2084 			sd = &vol->v_subdisks[i];
2085 			sd->sd_disk = disk;
2086 			sd->sd_offset = off;
2087 			sd->sd_size = size;
2088 			TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
2089 			if (disk->d_state == G_RAID_DISK_S_ACTIVE) {
2090 				if (level == G_RAID_VOLUME_RL_RAID5)
2091 					g_raid_change_subdisk_state(sd,
2092 					    G_RAID_SUBDISK_S_UNINITIALIZED);
2093 				else
2094 					g_raid_change_subdisk_state(sd,
2095 					    G_RAID_SUBDISK_S_ACTIVE);
2096 				g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
2097 				    G_RAID_EVENT_SUBDISK);
2098 			}
2099 		}
2100 
2101 		/* Write metadata based on created entities. */
2102 		g_raid_md_write_intel(md, NULL, NULL, NULL);
2103 
2104 		g_raid_event_send(vol, G_RAID_VOLUME_E_START,
2105 		    G_RAID_EVENT_VOLUME);
2106 		return (0);
2107 	}
2108 	if (strcmp(verb, "delete") == 0) {
2109 		nodename = gctl_get_asciiparam(req, "arg0");
2110 		if (nodename != NULL && strcasecmp(sc->sc_name, nodename) != 0)
2111 			nodename = NULL;
2112 
2113 		/* Full node destruction. */
2114 		if (*nargs == 1 && nodename != NULL) {
2115 			/* Check if some volume is still open. */
2116 			force = gctl_get_paraml(req, "force", sizeof(*force));
2117 			if (force != NULL && *force == 0 &&
2118 			    g_raid_nopens(sc) != 0) {
2119 				gctl_error(req, "Some volume is still open.");
2120 				return (-4);
2121 			}
2122 
2123 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2124 				if (disk->d_consumer)
2125 					intel_meta_erase(disk->d_consumer);
2126 			}
2127 			g_raid_destroy_node(sc, 0);
2128 			return (0);
2129 		}
2130 
2131 		/* Destroy specified volume. If it was last - all node. */
2132 		if (*nargs > 2) {
2133 			gctl_error(req, "Invalid number of arguments.");
2134 			return (-1);
2135 		}
2136 		volname = gctl_get_asciiparam(req,
2137 		    nodename != NULL ? "arg1" : "arg0");
2138 		if (volname == NULL) {
2139 			gctl_error(req, "No volume name.");
2140 			return (-2);
2141 		}
2142 
2143 		/* Search for volume. */
2144 		TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2145 			if (strcmp(vol->v_name, volname) == 0)
2146 				break;
2147 			pp = vol->v_provider;
2148 			if (pp == NULL)
2149 				continue;
2150 			if (strcmp(pp->name, volname) == 0)
2151 				break;
2152 			if (strncmp(pp->name, "raid/", 5) == 0 &&
2153 			    strcmp(pp->name + 5, volname) == 0)
2154 				break;
2155 		}
2156 		if (vol == NULL) {
2157 			i = strtol(volname, &tmp, 10);
2158 			if (verb != volname && tmp[0] == 0) {
2159 				TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2160 					if (vol->v_global_id == i)
2161 						break;
2162 				}
2163 			}
2164 		}
2165 		if (vol == NULL) {
2166 			gctl_error(req, "Volume '%s' not found.", volname);
2167 			return (-3);
2168 		}
2169 
2170 		/* Check if volume is still open. */
2171 		force = gctl_get_paraml(req, "force", sizeof(*force));
2172 		if (force != NULL && *force == 0 &&
2173 		    vol->v_provider_open != 0) {
2174 			gctl_error(req, "Volume is still open.");
2175 			return (-4);
2176 		}
2177 
2178 		/* Destroy volume and potentially node. */
2179 		i = 0;
2180 		TAILQ_FOREACH(vol1, &sc->sc_volumes, v_next)
2181 			i++;
2182 		if (i >= 2) {
2183 			g_raid_destroy_volume(vol);
2184 			g_raid_md_write_intel(md, NULL, NULL, NULL);
2185 		} else {
2186 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2187 				if (disk->d_consumer)
2188 					intel_meta_erase(disk->d_consumer);
2189 			}
2190 			g_raid_destroy_node(sc, 0);
2191 		}
2192 		return (0);
2193 	}
2194 	if (strcmp(verb, "remove") == 0 ||
2195 	    strcmp(verb, "fail") == 0) {
2196 		if (*nargs < 2) {
2197 			gctl_error(req, "Invalid number of arguments.");
2198 			return (-1);
2199 		}
2200 		for (i = 1; i < *nargs; i++) {
2201 			snprintf(arg, sizeof(arg), "arg%d", i);
2202 			diskname = gctl_get_asciiparam(req, arg);
2203 			if (diskname == NULL) {
2204 				gctl_error(req, "No disk name (%s).", arg);
2205 				error = -2;
2206 				break;
2207 			}
2208 			if (strncmp(diskname, _PATH_DEV, 5) == 0)
2209 				diskname += 5;
2210 
2211 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2212 				if (disk->d_consumer != NULL &&
2213 				    disk->d_consumer->provider != NULL &&
2214 				    strcmp(disk->d_consumer->provider->name,
2215 				     diskname) == 0)
2216 					break;
2217 			}
2218 			if (disk == NULL) {
2219 				gctl_error(req, "Disk '%s' not found.",
2220 				    diskname);
2221 				error = -3;
2222 				break;
2223 			}
2224 
2225 			if (strcmp(verb, "fail") == 0) {
2226 				g_raid_md_fail_disk_intel(md, NULL, disk);
2227 				continue;
2228 			}
2229 
2230 			pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
2231 
2232 			/* Erase metadata on deleting disk. */
2233 			intel_meta_erase(disk->d_consumer);
2234 
2235 			/* If disk was assigned, just update statuses. */
2236 			if (pd->pd_disk_pos >= 0) {
2237 				g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
2238 				g_raid_kill_consumer(sc, disk->d_consumer);
2239 				disk->d_consumer = NULL;
2240 				TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
2241 					g_raid_change_subdisk_state(sd,
2242 					    G_RAID_SUBDISK_S_NONE);
2243 					g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
2244 					    G_RAID_EVENT_SUBDISK);
2245 				}
2246 			} else {
2247 				/* Otherwise -- delete. */
2248 				g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
2249 				g_raid_destroy_disk(disk);
2250 			}
2251 		}
2252 
2253 		/* Write updated metadata to remaining disks. */
2254 		g_raid_md_write_intel(md, NULL, NULL, NULL);
2255 
2256 		/* Check if anything left except placeholders. */
2257 		if (g_raid_ndisks(sc, -1) ==
2258 		    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
2259 			g_raid_destroy_node(sc, 0);
2260 		else
2261 			g_raid_md_intel_refill(sc);
2262 		return (error);
2263 	}
2264 	if (strcmp(verb, "insert") == 0) {
2265 		if (*nargs < 2) {
2266 			gctl_error(req, "Invalid number of arguments.");
2267 			return (-1);
2268 		}
2269 		update = 0;
2270 		for (i = 1; i < *nargs; i++) {
2271 			/* Get disk name. */
2272 			snprintf(arg, sizeof(arg), "arg%d", i);
2273 			diskname = gctl_get_asciiparam(req, arg);
2274 			if (diskname == NULL) {
2275 				gctl_error(req, "No disk name (%s).", arg);
2276 				error = -3;
2277 				break;
2278 			}
2279 
2280 			/* Try to find provider with specified name. */
2281 			g_topology_lock();
2282 			cp = g_raid_open_consumer(sc, diskname);
2283 			if (cp == NULL) {
2284 				gctl_error(req, "Can't open disk '%s'.",
2285 				    diskname);
2286 				g_topology_unlock();
2287 				error = -4;
2288 				break;
2289 			}
2290 			pp = cp->provider;
2291 			g_topology_unlock();
2292 
2293 			/* Read disk serial. */
2294 			error = g_raid_md_get_label(cp,
2295 			    &serial[0], INTEL_SERIAL_LEN);
2296 			if (error != 0) {
2297 				gctl_error(req,
2298 				    "Can't get serial for provider '%s'.",
2299 				    diskname);
2300 				g_raid_kill_consumer(sc, cp);
2301 				error = -7;
2302 				break;
2303 			}
2304 
2305 			pd = malloc(sizeof(*pd), M_MD_INTEL, M_WAITOK | M_ZERO);
2306 			pd->pd_disk_pos = -1;
2307 
2308 			disk = g_raid_create_disk(sc);
2309 			disk->d_consumer = cp;
2310 			disk->d_md_data = (void *)pd;
2311 			cp->private = disk;
2312 
2313 			g_raid_get_disk_info(disk);
2314 
2315 			memcpy(&pd->pd_disk_meta.serial[0], &serial[0],
2316 			    INTEL_SERIAL_LEN);
2317 			intel_set_disk_sectors(&pd->pd_disk_meta,
2318 			    pp->mediasize / pp->sectorsize);
2319 			pd->pd_disk_meta.id = 0;
2320 			pd->pd_disk_meta.flags = INTEL_F_SPARE;
2321 
2322 			/* Welcome the "new" disk. */
2323 			update += g_raid_md_intel_start_disk(disk);
2324 			if (disk->d_state == G_RAID_DISK_S_SPARE) {
2325 				intel_meta_write_spare(cp, &pd->pd_disk_meta);
2326 				g_raid_destroy_disk(disk);
2327 			} else if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
2328 				gctl_error(req, "Disk '%s' doesn't fit.",
2329 				    diskname);
2330 				g_raid_destroy_disk(disk);
2331 				error = -8;
2332 				break;
2333 			}
2334 		}
2335 
2336 		/* Write new metadata if we changed something. */
2337 		if (update)
2338 			g_raid_md_write_intel(md, NULL, NULL, NULL);
2339 		return (error);
2340 	}
2341 	return (-100);
2342 }
2343 
2344 static int
2345 g_raid_md_write_intel(struct g_raid_md_object *md, struct g_raid_volume *tvol,
2346     struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
2347 {
2348 	struct g_raid_softc *sc;
2349 	struct g_raid_volume *vol;
2350 	struct g_raid_subdisk *sd;
2351 	struct g_raid_disk *disk;
2352 	struct g_raid_md_intel_object *mdi;
2353 	struct g_raid_md_intel_pervolume *pv;
2354 	struct g_raid_md_intel_perdisk *pd;
2355 	struct intel_raid_conf *meta;
2356 	struct intel_raid_vol *mvol;
2357 	struct intel_raid_map *mmap0, *mmap1;
2358 	off_t sectorsize = 512, pos;
2359 	const char *version, *cv;
2360 	int vi, sdi, numdisks, len, state, stale;
2361 
2362 	sc = md->mdo_softc;
2363 	mdi = (struct g_raid_md_intel_object *)md;
2364 
2365 	if (sc->sc_stopping == G_RAID_DESTROY_HARD)
2366 		return (0);
2367 
2368 	/* Bump generation. Newly written metadata may differ from previous. */
2369 	mdi->mdio_generation++;
2370 
2371 	/* Count number of disks. */
2372 	numdisks = 0;
2373 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2374 		pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
2375 		if (pd->pd_disk_pos < 0)
2376 			continue;
2377 		numdisks++;
2378 		if (disk->d_state == G_RAID_DISK_S_ACTIVE) {
2379 			pd->pd_disk_meta.flags =
2380 			    INTEL_F_ONLINE | INTEL_F_ASSIGNED;
2381 		} else if (disk->d_state == G_RAID_DISK_S_FAILED) {
2382 			pd->pd_disk_meta.flags = INTEL_F_FAILED |
2383 			    INTEL_F_ASSIGNED;
2384 		} else if (disk->d_state == G_RAID_DISK_S_DISABLED) {
2385 			pd->pd_disk_meta.flags = INTEL_F_FAILED |
2386 			    INTEL_F_ASSIGNED | INTEL_F_DISABLED;
2387 		} else {
2388 			if (!(pd->pd_disk_meta.flags & INTEL_F_DISABLED))
2389 				pd->pd_disk_meta.flags = INTEL_F_ASSIGNED;
2390 			if (pd->pd_disk_meta.id != 0xffffffff) {
2391 				pd->pd_disk_meta.id = 0xffffffff;
2392 				len = strlen(pd->pd_disk_meta.serial);
2393 				len = min(len, INTEL_SERIAL_LEN - 3);
2394 				strcpy(pd->pd_disk_meta.serial + len, ":0");
2395 			}
2396 		}
2397 	}
2398 
2399 	/* Fill anchor and disks. */
2400 	meta = malloc(INTEL_MAX_MD_SIZE(numdisks),
2401 	    M_MD_INTEL, M_WAITOK | M_ZERO);
2402 	memcpy(&meta->intel_id[0], INTEL_MAGIC, sizeof(INTEL_MAGIC) - 1);
2403 	meta->config_size = INTEL_MAX_MD_SIZE(numdisks);
2404 	meta->config_id = mdi->mdio_config_id;
2405 	meta->orig_config_id = mdi->mdio_orig_config_id;
2406 	meta->generation = mdi->mdio_generation;
2407 	meta->attributes = INTEL_ATTR_CHECKSUM;
2408 	meta->total_disks = numdisks;
2409 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2410 		pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
2411 		if (pd->pd_disk_pos < 0)
2412 			continue;
2413 		meta->disk[pd->pd_disk_pos] = pd->pd_disk_meta;
2414 		if (pd->pd_disk_meta.sectors_hi != 0)
2415 			meta->attributes |= INTEL_ATTR_2TB_DISK;
2416 	}
2417 
2418 	/* Fill volumes and maps. */
2419 	vi = 0;
2420 	version = INTEL_VERSION_1000;
2421 	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2422 		pv = vol->v_md_data;
2423 		if (vol->v_stopping)
2424 			continue;
2425 		mvol = intel_get_volume(meta, vi);
2426 
2427 		/* New metadata may have different volumes order. */
2428 		pv->pv_volume_pos = vi;
2429 
2430 		for (sdi = 0; sdi < vol->v_disks_count; sdi++) {
2431 			sd = &vol->v_subdisks[sdi];
2432 			if (sd->sd_disk != NULL)
2433 				break;
2434 		}
2435 		if (sdi >= vol->v_disks_count)
2436 			panic("No any filled subdisk in volume");
2437 		if (vol->v_mediasize >= 0x20000000000llu)
2438 			meta->attributes |= INTEL_ATTR_2TB;
2439 		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0)
2440 			meta->attributes |= INTEL_ATTR_RAID0;
2441 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1)
2442 			meta->attributes |= INTEL_ATTR_RAID1;
2443 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID5)
2444 			meta->attributes |= INTEL_ATTR_RAID5;
2445 		else if ((vol->v_disks_count & 1) == 0)
2446 			meta->attributes |= INTEL_ATTR_RAID10;
2447 		else
2448 			meta->attributes |= INTEL_ATTR_RAID1E;
2449 		if (pv->pv_cng)
2450 			meta->attributes |= INTEL_ATTR_RAIDCNG;
2451 		if (vol->v_strip_size > 131072)
2452 			meta->attributes |= INTEL_ATTR_EXT_STRIP;
2453 
2454 		if (pv->pv_cng)
2455 			cv = INTEL_VERSION_1206;
2456 		else if (vol->v_disks_count > 4)
2457 			cv = INTEL_VERSION_1204;
2458 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID5)
2459 			cv = INTEL_VERSION_1202;
2460 		else if (vol->v_disks_count > 2)
2461 			cv = INTEL_VERSION_1201;
2462 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1)
2463 			cv = INTEL_VERSION_1100;
2464 		else
2465 			cv = INTEL_VERSION_1000;
2466 		if (strcmp(cv, version) > 0)
2467 			version = cv;
2468 
2469 		strlcpy(&mvol->name[0], vol->v_name, sizeof(mvol->name));
2470 		mvol->total_sectors = vol->v_mediasize / sectorsize;
2471 		mvol->state = (INTEL_ST_READ_COALESCING |
2472 		    INTEL_ST_WRITE_COALESCING);
2473 		mvol->tid = vol->v_global_id + 1;
2474 		if (pv->pv_cng) {
2475 			mvol->state |= INTEL_ST_CLONE_N_GO;
2476 			if (pv->pv_cng_man_sync)
2477 				mvol->state |= INTEL_ST_CLONE_MAN_SYNC;
2478 			mvol->cng_master_disk = pv->pv_cng_master_disk;
2479 			if (vol->v_subdisks[pv->pv_cng_master_disk].sd_state ==
2480 			    G_RAID_SUBDISK_S_NONE)
2481 				mvol->cng_state = INTEL_CNGST_MASTER_MISSING;
2482 			else if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL)
2483 				mvol->cng_state = INTEL_CNGST_NEEDS_UPDATE;
2484 			else
2485 				mvol->cng_state = INTEL_CNGST_UPDATED;
2486 		}
2487 
2488 		/* Check for any recovery in progress. */
2489 		state = G_RAID_SUBDISK_S_ACTIVE;
2490 		pos = 0x7fffffffffffffffllu;
2491 		stale = 0;
2492 		for (sdi = 0; sdi < vol->v_disks_count; sdi++) {
2493 			sd = &vol->v_subdisks[sdi];
2494 			if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD)
2495 				state = G_RAID_SUBDISK_S_REBUILD;
2496 			else if (sd->sd_state == G_RAID_SUBDISK_S_RESYNC &&
2497 			    state != G_RAID_SUBDISK_S_REBUILD)
2498 				state = G_RAID_SUBDISK_S_RESYNC;
2499 			else if (sd->sd_state == G_RAID_SUBDISK_S_STALE)
2500 				stale = 1;
2501 			if ((sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
2502 			    sd->sd_state == G_RAID_SUBDISK_S_RESYNC) &&
2503 			     sd->sd_rebuild_pos < pos)
2504 			        pos = sd->sd_rebuild_pos;
2505 		}
2506 		if (state == G_RAID_SUBDISK_S_REBUILD) {
2507 			mvol->migr_state = 1;
2508 			mvol->migr_type = INTEL_MT_REBUILD;
2509 		} else if (state == G_RAID_SUBDISK_S_RESYNC) {
2510 			mvol->migr_state = 1;
2511 			/* mvol->migr_type = INTEL_MT_REPAIR; */
2512 			mvol->migr_type = INTEL_MT_VERIFY;
2513 			mvol->state |= INTEL_ST_VERIFY_AND_FIX;
2514 		} else
2515 			mvol->migr_state = 0;
2516 		mvol->dirty = (vol->v_dirty || stale);
2517 
2518 		mmap0 = intel_get_map(mvol, 0);
2519 
2520 		/* Write map / common part of two maps. */
2521 		intel_set_map_offset(mmap0, sd->sd_offset / sectorsize);
2522 		intel_set_map_disk_sectors(mmap0, sd->sd_size / sectorsize);
2523 		mmap0->strip_sectors = vol->v_strip_size / sectorsize;
2524 		if (vol->v_state == G_RAID_VOLUME_S_BROKEN)
2525 			mmap0->status = INTEL_S_FAILURE;
2526 		else if (vol->v_state == G_RAID_VOLUME_S_DEGRADED)
2527 			mmap0->status = INTEL_S_DEGRADED;
2528 		else if (g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_UNINITIALIZED)
2529 		    == g_raid_nsubdisks(vol, -1))
2530 			mmap0->status = INTEL_S_UNINITIALIZED;
2531 		else
2532 			mmap0->status = INTEL_S_READY;
2533 		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0)
2534 			mmap0->type = INTEL_T_RAID0;
2535 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 ||
2536 		    vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E)
2537 			mmap0->type = INTEL_T_RAID1;
2538 		else
2539 			mmap0->type = INTEL_T_RAID5;
2540 		mmap0->total_disks = vol->v_disks_count;
2541 		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1)
2542 			mmap0->total_domains = vol->v_disks_count;
2543 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E)
2544 			mmap0->total_domains = 2;
2545 		else
2546 			mmap0->total_domains = 1;
2547 		intel_set_map_stripe_count(mmap0,
2548 		    sd->sd_size / vol->v_strip_size / mmap0->total_domains);
2549 		mmap0->failed_disk_num = 0xff;
2550 		mmap0->ddf = 1;
2551 
2552 		/* If there are two maps - copy common and update. */
2553 		if (mvol->migr_state) {
2554 			intel_set_vol_curr_migr_unit(mvol,
2555 			    pos / vol->v_strip_size / mmap0->total_domains);
2556 			mmap1 = intel_get_map(mvol, 1);
2557 			memcpy(mmap1, mmap0, sizeof(struct intel_raid_map));
2558 			mmap0->status = INTEL_S_READY;
2559 		} else
2560 			mmap1 = NULL;
2561 
2562 		/* Write disk indexes and put rebuild flags. */
2563 		for (sdi = 0; sdi < vol->v_disks_count; sdi++) {
2564 			sd = &vol->v_subdisks[sdi];
2565 			pd = (struct g_raid_md_intel_perdisk *)
2566 			    sd->sd_disk->d_md_data;
2567 			mmap0->disk_idx[sdi] = pd->pd_disk_pos;
2568 			if (mvol->migr_state)
2569 				mmap1->disk_idx[sdi] = pd->pd_disk_pos;
2570 			if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
2571 			    sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
2572 				mmap1->disk_idx[sdi] |= INTEL_DI_RBLD;
2573 			} else if (sd->sd_state != G_RAID_SUBDISK_S_ACTIVE &&
2574 			    sd->sd_state != G_RAID_SUBDISK_S_STALE &&
2575 			    sd->sd_state != G_RAID_SUBDISK_S_UNINITIALIZED) {
2576 				mmap0->disk_idx[sdi] |= INTEL_DI_RBLD;
2577 				if (mvol->migr_state)
2578 					mmap1->disk_idx[sdi] |= INTEL_DI_RBLD;
2579 			}
2580 			if ((sd->sd_state == G_RAID_SUBDISK_S_NONE ||
2581 			     sd->sd_state == G_RAID_SUBDISK_S_FAILED ||
2582 			     sd->sd_state == G_RAID_SUBDISK_S_REBUILD) &&
2583 			    mmap0->failed_disk_num == 0xff) {
2584 				mmap0->failed_disk_num = sdi;
2585 				if (mvol->migr_state)
2586 					mmap1->failed_disk_num = sdi;
2587 			}
2588 		}
2589 		vi++;
2590 	}
2591 	meta->total_volumes = vi;
2592 	if (vi > 1 || meta->attributes &
2593 	     (INTEL_ATTR_EXT_STRIP | INTEL_ATTR_2TB_DISK | INTEL_ATTR_2TB))
2594 		version = INTEL_VERSION_1300;
2595 	if (strcmp(version, INTEL_VERSION_1300) < 0)
2596 		meta->attributes &= INTEL_ATTR_CHECKSUM;
2597 	memcpy(&meta->version[0], version, sizeof(INTEL_VERSION_1000) - 1);
2598 
2599 	/* We are done. Print meta data and store them to disks. */
2600 	g_raid_md_intel_print(meta);
2601 	if (mdi->mdio_meta != NULL)
2602 		free(mdi->mdio_meta, M_MD_INTEL);
2603 	mdi->mdio_meta = meta;
2604 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2605 		pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
2606 		if (disk->d_state != G_RAID_DISK_S_ACTIVE)
2607 			continue;
2608 		if (pd->pd_meta != NULL) {
2609 			free(pd->pd_meta, M_MD_INTEL);
2610 			pd->pd_meta = NULL;
2611 		}
2612 		pd->pd_meta = intel_meta_copy(meta);
2613 		intel_meta_write(disk->d_consumer, meta);
2614 	}
2615 	return (0);
2616 }
2617 
2618 static int
2619 g_raid_md_fail_disk_intel(struct g_raid_md_object *md,
2620     struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
2621 {
2622 	struct g_raid_softc *sc;
2623 	struct g_raid_md_intel_object *mdi;
2624 	struct g_raid_md_intel_perdisk *pd;
2625 	struct g_raid_subdisk *sd;
2626 
2627 	sc = md->mdo_softc;
2628 	mdi = (struct g_raid_md_intel_object *)md;
2629 	pd = (struct g_raid_md_intel_perdisk *)tdisk->d_md_data;
2630 
2631 	/* We can't fail disk that is not a part of array now. */
2632 	if (pd->pd_disk_pos < 0)
2633 		return (-1);
2634 
2635 	/*
2636 	 * Mark disk as failed in metadata and try to write that metadata
2637 	 * to the disk itself to prevent it's later resurrection as STALE.
2638 	 */
2639 	mdi->mdio_meta->disk[pd->pd_disk_pos].flags = INTEL_F_FAILED;
2640 	pd->pd_disk_meta.flags = INTEL_F_FAILED;
2641 	g_raid_md_intel_print(mdi->mdio_meta);
2642 	if (tdisk->d_consumer)
2643 		intel_meta_write(tdisk->d_consumer, mdi->mdio_meta);
2644 
2645 	/* Change states. */
2646 	g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED);
2647 	TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) {
2648 		g_raid_change_subdisk_state(sd,
2649 		    G_RAID_SUBDISK_S_FAILED);
2650 		g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED,
2651 		    G_RAID_EVENT_SUBDISK);
2652 	}
2653 
2654 	/* Write updated metadata to remaining disks. */
2655 	g_raid_md_write_intel(md, NULL, NULL, tdisk);
2656 
2657 	/* Check if anything left except placeholders. */
2658 	if (g_raid_ndisks(sc, -1) ==
2659 	    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
2660 		g_raid_destroy_node(sc, 0);
2661 	else
2662 		g_raid_md_intel_refill(sc);
2663 	return (0);
2664 }
2665 
2666 static int
2667 g_raid_md_free_disk_intel(struct g_raid_md_object *md,
2668     struct g_raid_disk *disk)
2669 {
2670 	struct g_raid_md_intel_perdisk *pd;
2671 
2672 	pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
2673 	if (pd->pd_meta != NULL) {
2674 		free(pd->pd_meta, M_MD_INTEL);
2675 		pd->pd_meta = NULL;
2676 	}
2677 	free(pd, M_MD_INTEL);
2678 	disk->d_md_data = NULL;
2679 	return (0);
2680 }
2681 
2682 static int
2683 g_raid_md_free_volume_intel(struct g_raid_md_object *md,
2684     struct g_raid_volume *vol)
2685 {
2686 	struct g_raid_md_intel_pervolume *pv;
2687 
2688 	pv = (struct g_raid_md_intel_pervolume *)vol->v_md_data;
2689 	free(pv, M_MD_INTEL);
2690 	vol->v_md_data = NULL;
2691 	return (0);
2692 }
2693 
2694 static int
2695 g_raid_md_free_intel(struct g_raid_md_object *md)
2696 {
2697 	struct g_raid_md_intel_object *mdi;
2698 
2699 	mdi = (struct g_raid_md_intel_object *)md;
2700 	if (!mdi->mdio_started) {
2701 		mdi->mdio_started = 0;
2702 		callout_stop(&mdi->mdio_start_co);
2703 		G_RAID_DEBUG1(1, md->mdo_softc,
2704 		    "root_mount_rel %p", mdi->mdio_rootmount);
2705 		root_mount_rel(mdi->mdio_rootmount);
2706 		mdi->mdio_rootmount = NULL;
2707 	}
2708 	if (mdi->mdio_meta != NULL) {
2709 		free(mdi->mdio_meta, M_MD_INTEL);
2710 		mdi->mdio_meta = NULL;
2711 	}
2712 	return (0);
2713 }
2714 
2715 G_RAID_MD_DECLARE(intel, "Intel");
2716