xref: /freebsd/sys/geom/raid/md_intel.c (revision d0ef721ed3dc99bddc1e48605a6921ec60322efc)
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 	u_int64_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 
265 static struct intel_raid_map *
266 intel_get_map(struct intel_raid_vol *mvol, int i)
267 {
268 	struct intel_raid_map *mmap;
269 
270 	if (i > (mvol->migr_state ? 1 : 0))
271 		return (NULL);
272 	mmap = &mvol->map[0];
273 	for (; i > 0; i--) {
274 		mmap = (struct intel_raid_map *)
275 		    &mmap->disk_idx[mmap->total_disks];
276 	}
277 	return ((struct intel_raid_map *)mmap);
278 }
279 
280 static struct intel_raid_vol *
281 intel_get_volume(struct intel_raid_conf *meta, int i)
282 {
283 	struct intel_raid_vol *mvol;
284 	struct intel_raid_map *mmap;
285 
286 	if (i > 1)
287 		return (NULL);
288 	mvol = (struct intel_raid_vol *)&meta->disk[meta->total_disks];
289 	for (; i > 0; i--) {
290 		mmap = intel_get_map(mvol, mvol->migr_state ? 1 : 0);
291 		mvol = (struct intel_raid_vol *)
292 		    &mmap->disk_idx[mmap->total_disks];
293 	}
294 	return (mvol);
295 }
296 
297 static off_t
298 intel_get_map_offset(struct intel_raid_map *mmap)
299 {
300 	off_t offset = (off_t)mmap->offset_hi << 32;
301 
302 	offset += mmap->offset;
303 	return (offset);
304 }
305 
306 static void
307 intel_set_map_offset(struct intel_raid_map *mmap, off_t offset)
308 {
309 
310 	mmap->offset = offset & 0xffffffff;
311 	mmap->offset_hi = offset >> 32;
312 }
313 
314 static off_t
315 intel_get_map_disk_sectors(struct intel_raid_map *mmap)
316 {
317 	off_t disk_sectors = (off_t)mmap->disk_sectors_hi << 32;
318 
319 	disk_sectors += mmap->disk_sectors;
320 	return (disk_sectors);
321 }
322 
323 static void
324 intel_set_map_disk_sectors(struct intel_raid_map *mmap, off_t disk_sectors)
325 {
326 
327 	mmap->disk_sectors = disk_sectors & 0xffffffff;
328 	mmap->disk_sectors_hi = disk_sectors >> 32;
329 }
330 
331 static void
332 intel_set_map_stripe_count(struct intel_raid_map *mmap, off_t stripe_count)
333 {
334 
335 	mmap->stripe_count = stripe_count & 0xffffffff;
336 	mmap->stripe_count_hi = stripe_count >> 32;
337 }
338 
339 static off_t
340 intel_get_disk_sectors(struct intel_raid_disk *disk)
341 {
342 	off_t sectors = (off_t)disk->sectors_hi << 32;
343 
344 	sectors += disk->sectors;
345 	return (sectors);
346 }
347 
348 static void
349 intel_set_disk_sectors(struct intel_raid_disk *disk, off_t sectors)
350 {
351 
352 	disk->sectors = sectors & 0xffffffff;
353 	disk->sectors_hi = sectors >> 32;
354 }
355 
356 static off_t
357 intel_get_vol_curr_migr_unit(struct intel_raid_vol *vol)
358 {
359 	off_t curr_migr_unit = (off_t)vol->curr_migr_unit_hi << 32;
360 
361 	curr_migr_unit += vol->curr_migr_unit;
362 	return (curr_migr_unit);
363 }
364 
365 static void
366 intel_set_vol_curr_migr_unit(struct intel_raid_vol *vol, off_t curr_migr_unit)
367 {
368 
369 	vol->curr_migr_unit = curr_migr_unit & 0xffffffff;
370 	vol->curr_migr_unit_hi = curr_migr_unit >> 32;
371 }
372 
373 static char *
374 intel_status2str(int status)
375 {
376 
377 	switch (status) {
378 	case INTEL_S_READY:
379 		return ("READY");
380 	case INTEL_S_UNINITIALIZED:
381 		return ("UNINITIALIZED");
382 	case INTEL_S_DEGRADED:
383 		return ("DEGRADED");
384 	case INTEL_S_FAILURE:
385 		return ("FAILURE");
386 	default:
387 		return ("UNKNOWN");
388 	}
389 }
390 
391 static char *
392 intel_type2str(int type)
393 {
394 
395 	switch (type) {
396 	case INTEL_T_RAID0:
397 		return ("RAID0");
398 	case INTEL_T_RAID1:
399 		return ("RAID1");
400 	case INTEL_T_RAID5:
401 		return ("RAID5");
402 	default:
403 		return ("UNKNOWN");
404 	}
405 }
406 
407 static char *
408 intel_cngst2str(int cng_state)
409 {
410 
411 	switch (cng_state) {
412 	case INTEL_CNGST_UPDATED:
413 		return ("UPDATED");
414 	case INTEL_CNGST_NEEDS_UPDATE:
415 		return ("NEEDS_UPDATE");
416 	case INTEL_CNGST_MASTER_MISSING:
417 		return ("MASTER_MISSING");
418 	default:
419 		return ("UNKNOWN");
420 	}
421 }
422 
423 static char *
424 intel_mt2str(int type)
425 {
426 
427 	switch (type) {
428 	case INTEL_MT_INIT:
429 		return ("INIT");
430 	case INTEL_MT_REBUILD:
431 		return ("REBUILD");
432 	case INTEL_MT_VERIFY:
433 		return ("VERIFY");
434 	case INTEL_MT_GEN_MIGR:
435 		return ("GEN_MIGR");
436 	case INTEL_MT_STATE_CHANGE:
437 		return ("STATE_CHANGE");
438 	case INTEL_MT_REPAIR:
439 		return ("REPAIR");
440 	default:
441 		return ("UNKNOWN");
442 	}
443 }
444 
445 static void
446 g_raid_md_intel_print(struct intel_raid_conf *meta)
447 {
448 	struct intel_raid_vol *mvol;
449 	struct intel_raid_map *mmap;
450 	int i, j, k;
451 
452 	if (g_raid_debug < 1)
453 		return;
454 
455 	printf("********* ATA Intel MatrixRAID Metadata *********\n");
456 	printf("intel_id            <%.24s>\n", meta->intel_id);
457 	printf("version             <%.6s>\n", meta->version);
458 	printf("checksum            0x%08x\n", meta->checksum);
459 	printf("config_size         0x%08x\n", meta->config_size);
460 	printf("config_id           0x%08x\n", meta->config_id);
461 	printf("generation          0x%08x\n", meta->generation);
462 	printf("error_log_size      %d\n", meta->error_log_size);
463 	printf("attributes          0x%b\n", meta->attributes,
464 		"\020"
465 		"\001RAID0"
466 		"\002RAID1"
467 		"\003RAID10"
468 		"\004RAID1E"
469 		"\005RAID15"
470 		"\006RAIDCNG"
471 		"\007EXT_STRIP"
472 		"\032NVM_CACHE"
473 		"\0332TB_DISK"
474 		"\034BBM"
475 		"\035NVM_CACHE"
476 		"\0362TB"
477 		"\037PM"
478 		"\040CHECKSUM");
479 	printf("total_disks         %u\n", meta->total_disks);
480 	printf("total_volumes       %u\n", meta->total_volumes);
481 	printf("error_log_pos       %u\n", meta->error_log_pos);
482 	printf("cache_size          %u\n", meta->cache_size);
483 	printf("orig_config_id      0x%08x\n", meta->orig_config_id);
484 	printf("pwr_cycle_count     %u\n", meta->pwr_cycle_count);
485 	printf("bbm_log_size        %u\n", meta->bbm_log_size);
486 	printf("Flags: S - Spare, A - Assigned, F - Failed, O - Online, D - Disabled\n");
487 	printf("DISK#   serial disk_sectors disk_sectors_hi disk_id flags owner\n");
488 	for (i = 0; i < meta->total_disks; i++ ) {
489 		printf("    %d   <%.16s> %u %u 0x%08x 0x%b %08x\n", i,
490 		    meta->disk[i].serial, meta->disk[i].sectors,
491 		    meta->disk[i].sectors_hi, meta->disk[i].id,
492 		    meta->disk[i].flags, "\20\01S\02A\03F\04O\05D",
493 		    meta->disk[i].owner_cfg_num);
494 	}
495 	for (i = 0; i < meta->total_volumes; i++) {
496 		mvol = intel_get_volume(meta, i);
497 		printf(" ****** Volume %d ******\n", i);
498 		printf(" name               %.16s\n", mvol->name);
499 		printf(" total_sectors      %ju\n", mvol->total_sectors);
500 		printf(" state              0x%b\n", mvol->state,
501 			"\020"
502 			"\001BOOTABLE"
503 			"\002BOOT_DEVICE"
504 			"\003READ_COALESCING"
505 			"\004WRITE_COALESCING"
506 			"\005LAST_SHUTDOWN_DIRTY"
507 			"\006HIDDEN_AT_BOOT"
508 			"\007CURRENTLY_HIDDEN"
509 			"\010VERIFY_AND_FIX"
510 			"\011MAP_STATE_UNINIT"
511 			"\012NO_AUTO_RECOVERY"
512 			"\013CLONE_N_GO"
513 			"\014CLONE_MAN_SYNC"
514 			"\015CNG_MASTER_DISK_NUM");
515 		printf(" reserved           %u\n", mvol->reserved);
516 		printf(" migr_priority      %u\n", mvol->migr_priority);
517 		printf(" num_sub_vols       %u\n", mvol->num_sub_vols);
518 		printf(" tid                %u\n", mvol->tid);
519 		printf(" cng_master_disk    %u\n", mvol->cng_master_disk);
520 		printf(" cache_policy       %u\n", mvol->cache_policy);
521 		printf(" cng_state          %u (%s)\n", mvol->cng_state,
522 			intel_cngst2str(mvol->cng_state));
523 		printf(" cng_sub_state      %u\n", mvol->cng_sub_state);
524 		printf(" curr_migr_unit     %u\n", mvol->curr_migr_unit);
525 		printf(" curr_migr_unit_hi  %u\n", mvol->curr_migr_unit_hi);
526 		printf(" checkpoint_id      %u\n", mvol->checkpoint_id);
527 		printf(" migr_state         %u\n", mvol->migr_state);
528 		printf(" migr_type          %u (%s)\n", mvol->migr_type,
529 			intel_mt2str(mvol->migr_type));
530 		printf(" dirty              %u\n", mvol->dirty);
531 		printf(" fs_state           %u\n", mvol->fs_state);
532 		printf(" verify_errors      %u\n", mvol->verify_errors);
533 		printf(" bad_blocks         %u\n", mvol->bad_blocks);
534 
535 		for (j = 0; j < (mvol->migr_state ? 2 : 1); j++) {
536 			printf("  *** Map %d ***\n", j);
537 			mmap = intel_get_map(mvol, j);
538 			printf("  offset            %u\n", mmap->offset);
539 			printf("  offset_hi         %u\n", mmap->offset_hi);
540 			printf("  disk_sectors      %u\n", mmap->disk_sectors);
541 			printf("  disk_sectors_hi   %u\n", mmap->disk_sectors_hi);
542 			printf("  stripe_count      %u\n", mmap->stripe_count);
543 			printf("  stripe_count_hi   %u\n", mmap->stripe_count_hi);
544 			printf("  strip_sectors     %u\n", mmap->strip_sectors);
545 			printf("  status            %u (%s)\n", mmap->status,
546 				intel_status2str(mmap->status));
547 			printf("  type              %u (%s)\n", mmap->type,
548 				intel_type2str(mmap->type));
549 			printf("  total_disks       %u\n", mmap->total_disks);
550 			printf("  total_domains     %u\n", mmap->total_domains);
551 			printf("  failed_disk_num   %u\n", mmap->failed_disk_num);
552 			printf("  ddf               %u\n", mmap->ddf);
553 			printf("  disk_idx         ");
554 			for (k = 0; k < mmap->total_disks; k++)
555 				printf(" 0x%08x", mmap->disk_idx[k]);
556 			printf("\n");
557 		}
558 	}
559 	printf("=================================================\n");
560 }
561 
562 static struct intel_raid_conf *
563 intel_meta_copy(struct intel_raid_conf *meta)
564 {
565 	struct intel_raid_conf *nmeta;
566 
567 	nmeta = malloc(meta->config_size, M_MD_INTEL, M_WAITOK);
568 	memcpy(nmeta, meta, meta->config_size);
569 	return (nmeta);
570 }
571 
572 static int
573 intel_meta_find_disk(struct intel_raid_conf *meta, char *serial)
574 {
575 	int pos;
576 
577 	for (pos = 0; pos < meta->total_disks; pos++) {
578 		if (strncmp(meta->disk[pos].serial,
579 		    serial, INTEL_SERIAL_LEN) == 0)
580 			return (pos);
581 	}
582 	return (-1);
583 }
584 
585 static struct intel_raid_conf *
586 intel_meta_read(struct g_consumer *cp)
587 {
588 	struct g_provider *pp;
589 	struct intel_raid_conf *meta;
590 	struct intel_raid_vol *mvol;
591 	struct intel_raid_map *mmap, *mmap1;
592 	char *buf;
593 	int error, i, j, k, left, size;
594 	uint32_t checksum, *ptr;
595 
596 	pp = cp->provider;
597 
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 
1716 		if (*nargs < 4) {
1717 			gctl_error(req, "Invalid number of arguments.");
1718 			return (-1);
1719 		}
1720 		volname = gctl_get_asciiparam(req, "arg1");
1721 		if (volname == NULL) {
1722 			gctl_error(req, "No volume name.");
1723 			return (-2);
1724 		}
1725 		levelname = gctl_get_asciiparam(req, "arg2");
1726 		if (levelname == NULL) {
1727 			gctl_error(req, "No RAID level.");
1728 			return (-3);
1729 		}
1730 		if (strcasecmp(levelname, "RAID5") == 0)
1731 			levelname = "RAID5-LA";
1732 		if (g_raid_volume_str2level(levelname, &level, &qual)) {
1733 			gctl_error(req, "Unknown RAID level '%s'.", levelname);
1734 			return (-4);
1735 		}
1736 		numdisks = *nargs - 3;
1737 		force = gctl_get_paraml(req, "force", sizeof(*force));
1738 		if (!g_raid_md_intel_supported(level, qual, numdisks,
1739 		    force ? *force : 0)) {
1740 			gctl_error(req, "Unsupported RAID level "
1741 			    "(0x%02x/0x%02x), or number of disks (%d).",
1742 			    level, qual, numdisks);
1743 			return (-5);
1744 		}
1745 
1746 		/* Search for disks, connect them and probe. */
1747 		size = 0x7fffffffffffffffllu;
1748 		sectorsize = 0;
1749 		for (i = 0; i < numdisks; i++) {
1750 			snprintf(arg, sizeof(arg), "arg%d", i + 3);
1751 			diskname = gctl_get_asciiparam(req, arg);
1752 			if (diskname == NULL) {
1753 				gctl_error(req, "No disk name (%s).", arg);
1754 				error = -6;
1755 				break;
1756 			}
1757 			if (strcmp(diskname, "NONE") == 0) {
1758 				cp = NULL;
1759 				pp = NULL;
1760 			} else {
1761 				g_topology_lock();
1762 				cp = g_raid_open_consumer(sc, diskname);
1763 				if (cp == NULL) {
1764 					gctl_error(req, "Can't open disk '%s'.",
1765 					    diskname);
1766 					g_topology_unlock();
1767 					error = -7;
1768 					break;
1769 				}
1770 				pp = cp->provider;
1771 			}
1772 			pd = malloc(sizeof(*pd), M_MD_INTEL, M_WAITOK | M_ZERO);
1773 			pd->pd_disk_pos = i;
1774 			disk = g_raid_create_disk(sc);
1775 			disk->d_md_data = (void *)pd;
1776 			disk->d_consumer = cp;
1777 			if (cp == NULL) {
1778 				strcpy(&pd->pd_disk_meta.serial[0], "NONE");
1779 				pd->pd_disk_meta.id = 0xffffffff;
1780 				pd->pd_disk_meta.flags = INTEL_F_ASSIGNED;
1781 				continue;
1782 			}
1783 			cp->private = disk;
1784 			g_topology_unlock();
1785 
1786 			error = g_raid_md_get_label(cp,
1787 			    &pd->pd_disk_meta.serial[0], INTEL_SERIAL_LEN);
1788 			if (error != 0) {
1789 				gctl_error(req,
1790 				    "Can't get serial for provider '%s'.",
1791 				    diskname);
1792 				error = -8;
1793 				break;
1794 			}
1795 
1796 			g_raid_get_disk_info(disk);
1797 
1798 			intel_set_disk_sectors(&pd->pd_disk_meta,
1799 			    pp->mediasize / pp->sectorsize);
1800 			if (size > pp->mediasize)
1801 				size = pp->mediasize;
1802 			if (sectorsize < pp->sectorsize)
1803 				sectorsize = pp->sectorsize;
1804 			pd->pd_disk_meta.id = 0;
1805 			pd->pd_disk_meta.flags = INTEL_F_ASSIGNED | INTEL_F_ONLINE;
1806 		}
1807 		if (error != 0)
1808 			return (error);
1809 
1810 		if (sectorsize <= 0) {
1811 			gctl_error(req, "Can't get sector size.");
1812 			return (-8);
1813 		}
1814 
1815 		/* Reserve some space for metadata. */
1816 		size -= ((4096 + sectorsize - 1) / sectorsize) * sectorsize;
1817 
1818 		/* Handle size argument. */
1819 		len = sizeof(*sizearg);
1820 		sizearg = gctl_get_param(req, "size", &len);
1821 		if (sizearg != NULL && len == sizeof(*sizearg) &&
1822 		    *sizearg > 0) {
1823 			if (*sizearg > size) {
1824 				gctl_error(req, "Size too big %lld > %lld.",
1825 				    (long long)*sizearg, (long long)size);
1826 				return (-9);
1827 			}
1828 			size = *sizearg;
1829 		}
1830 
1831 		/* Handle strip argument. */
1832 		strip = 131072;
1833 		len = sizeof(*striparg);
1834 		striparg = gctl_get_param(req, "strip", &len);
1835 		if (striparg != NULL && len == sizeof(*striparg) &&
1836 		    *striparg > 0) {
1837 			if (*striparg < sectorsize) {
1838 				gctl_error(req, "Strip size too small.");
1839 				return (-10);
1840 			}
1841 			if (*striparg % sectorsize != 0) {
1842 				gctl_error(req, "Incorrect strip size.");
1843 				return (-11);
1844 			}
1845 			if (strip > 65535 * sectorsize) {
1846 				gctl_error(req, "Strip size too big.");
1847 				return (-12);
1848 			}
1849 			strip = *striparg;
1850 		}
1851 
1852 		/* Round size down to strip or sector. */
1853 		if (level == G_RAID_VOLUME_RL_RAID1)
1854 			size -= (size % sectorsize);
1855 		else if (level == G_RAID_VOLUME_RL_RAID1E &&
1856 		    (numdisks & 1) != 0)
1857 			size -= (size % (2 * strip));
1858 		else
1859 			size -= (size % strip);
1860 		if (size <= 0) {
1861 			gctl_error(req, "Size too small.");
1862 			return (-13);
1863 		}
1864 
1865 		/* We have all we need, create things: volume, ... */
1866 		mdi->mdio_started = 1;
1867 		vol = g_raid_create_volume(sc, volname, -1);
1868 		pv = malloc(sizeof(*pv), M_MD_INTEL, M_WAITOK | M_ZERO);
1869 		pv->pv_volume_pos = 0;
1870 		vol->v_md_data = pv;
1871 		vol->v_raid_level = level;
1872 		vol->v_raid_level_qualifier = qual;
1873 		vol->v_strip_size = strip;
1874 		vol->v_disks_count = numdisks;
1875 		if (level == G_RAID_VOLUME_RL_RAID0)
1876 			vol->v_mediasize = size * numdisks;
1877 		else if (level == G_RAID_VOLUME_RL_RAID1)
1878 			vol->v_mediasize = size;
1879 		else if (level == G_RAID_VOLUME_RL_RAID5)
1880 			vol->v_mediasize = size * (numdisks - 1);
1881 		else { /* RAID1E */
1882 			vol->v_mediasize = ((size * numdisks) / strip / 2) *
1883 			    strip;
1884 		}
1885 		vol->v_sectorsize = sectorsize;
1886 		g_raid_start_volume(vol);
1887 
1888 		/* , and subdisks. */
1889 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1890 			pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
1891 			sd = &vol->v_subdisks[pd->pd_disk_pos];
1892 			sd->sd_disk = disk;
1893 			sd->sd_offset = 0;
1894 			sd->sd_size = size;
1895 			TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
1896 			if (sd->sd_disk->d_consumer != NULL) {
1897 				g_raid_change_disk_state(disk,
1898 				    G_RAID_DISK_S_ACTIVE);
1899 				if (level == G_RAID_VOLUME_RL_RAID5)
1900 					g_raid_change_subdisk_state(sd,
1901 					    G_RAID_SUBDISK_S_UNINITIALIZED);
1902 				else
1903 					g_raid_change_subdisk_state(sd,
1904 					    G_RAID_SUBDISK_S_ACTIVE);
1905 				g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
1906 				    G_RAID_EVENT_SUBDISK);
1907 			} else {
1908 				g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
1909 			}
1910 		}
1911 
1912 		/* Write metadata based on created entities. */
1913 		G_RAID_DEBUG1(0, sc, "Array started.");
1914 		g_raid_md_write_intel(md, NULL, NULL, NULL);
1915 
1916 		/* Pickup any STALE/SPARE disks to refill array if needed. */
1917 		g_raid_md_intel_refill(sc);
1918 
1919 		g_raid_event_send(vol, G_RAID_VOLUME_E_START,
1920 		    G_RAID_EVENT_VOLUME);
1921 		return (0);
1922 	}
1923 	if (strcmp(verb, "add") == 0) {
1924 
1925 		if (*nargs != 3) {
1926 			gctl_error(req, "Invalid number of arguments.");
1927 			return (-1);
1928 		}
1929 		volname = gctl_get_asciiparam(req, "arg1");
1930 		if (volname == NULL) {
1931 			gctl_error(req, "No volume name.");
1932 			return (-2);
1933 		}
1934 		levelname = gctl_get_asciiparam(req, "arg2");
1935 		if (levelname == NULL) {
1936 			gctl_error(req, "No RAID level.");
1937 			return (-3);
1938 		}
1939 		if (strcasecmp(levelname, "RAID5") == 0)
1940 			levelname = "RAID5-LA";
1941 		if (g_raid_volume_str2level(levelname, &level, &qual)) {
1942 			gctl_error(req, "Unknown RAID level '%s'.", levelname);
1943 			return (-4);
1944 		}
1945 
1946 		/* Look for existing volumes. */
1947 		i = 0;
1948 		vol1 = NULL;
1949 		TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1950 			vol1 = vol;
1951 			i++;
1952 		}
1953 		if (i > 1) {
1954 			gctl_error(req, "Maximum two volumes supported.");
1955 			return (-6);
1956 		}
1957 		if (vol1 == NULL) {
1958 			gctl_error(req, "At least one volume must exist.");
1959 			return (-7);
1960 		}
1961 
1962 		numdisks = vol1->v_disks_count;
1963 		force = gctl_get_paraml(req, "force", sizeof(*force));
1964 		if (!g_raid_md_intel_supported(level, qual, numdisks,
1965 		    force ? *force : 0)) {
1966 			gctl_error(req, "Unsupported RAID level "
1967 			    "(0x%02x/0x%02x), or number of disks (%d).",
1968 			    level, qual, numdisks);
1969 			return (-5);
1970 		}
1971 
1972 		/* Collect info about present disks. */
1973 		size = 0x7fffffffffffffffllu;
1974 		sectorsize = 512;
1975 		for (i = 0; i < numdisks; i++) {
1976 			disk = vol1->v_subdisks[i].sd_disk;
1977 			pd = (struct g_raid_md_intel_perdisk *)
1978 			    disk->d_md_data;
1979 			disk_sectors =
1980 			    intel_get_disk_sectors(&pd->pd_disk_meta);
1981 
1982 			if (disk_sectors * 512 < size)
1983 				size = disk_sectors * 512;
1984 			if (disk->d_consumer != NULL &&
1985 			    disk->d_consumer->provider != NULL &&
1986 			    disk->d_consumer->provider->sectorsize >
1987 			     sectorsize) {
1988 				sectorsize =
1989 				    disk->d_consumer->provider->sectorsize;
1990 			}
1991 		}
1992 
1993 		/* Reserve some space for metadata. */
1994 		size -= ((4096 + sectorsize - 1) / sectorsize) * sectorsize;
1995 
1996 		/* Decide insert before or after. */
1997 		sd = &vol1->v_subdisks[0];
1998 		if (sd->sd_offset >
1999 		    size - (sd->sd_offset + sd->sd_size)) {
2000 			off = 0;
2001 			size = sd->sd_offset;
2002 		} else {
2003 			off = sd->sd_offset + sd->sd_size;
2004 			size = size - (sd->sd_offset + sd->sd_size);
2005 		}
2006 
2007 		/* Handle strip argument. */
2008 		strip = 131072;
2009 		len = sizeof(*striparg);
2010 		striparg = gctl_get_param(req, "strip", &len);
2011 		if (striparg != NULL && len == sizeof(*striparg) &&
2012 		    *striparg > 0) {
2013 			if (*striparg < sectorsize) {
2014 				gctl_error(req, "Strip size too small.");
2015 				return (-10);
2016 			}
2017 			if (*striparg % sectorsize != 0) {
2018 				gctl_error(req, "Incorrect strip size.");
2019 				return (-11);
2020 			}
2021 			if (strip > 65535 * sectorsize) {
2022 				gctl_error(req, "Strip size too big.");
2023 				return (-12);
2024 			}
2025 			strip = *striparg;
2026 		}
2027 
2028 		/* Round offset up to strip. */
2029 		if (off % strip != 0) {
2030 			size -= strip - off % strip;
2031 			off += strip - off % strip;
2032 		}
2033 
2034 		/* Handle size argument. */
2035 		len = sizeof(*sizearg);
2036 		sizearg = gctl_get_param(req, "size", &len);
2037 		if (sizearg != NULL && len == sizeof(*sizearg) &&
2038 		    *sizearg > 0) {
2039 			if (*sizearg > size) {
2040 				gctl_error(req, "Size too big %lld > %lld.",
2041 				    (long long)*sizearg, (long long)size);
2042 				return (-9);
2043 			}
2044 			size = *sizearg;
2045 		}
2046 
2047 		/* Round size down to strip or sector. */
2048 		if (level == G_RAID_VOLUME_RL_RAID1)
2049 			size -= (size % sectorsize);
2050 		else
2051 			size -= (size % strip);
2052 		if (size <= 0) {
2053 			gctl_error(req, "Size too small.");
2054 			return (-13);
2055 		}
2056 		if (size > 0xffffffffllu * sectorsize) {
2057 			gctl_error(req, "Size too big.");
2058 			return (-14);
2059 		}
2060 
2061 		/* We have all we need, create things: volume, ... */
2062 		vol = g_raid_create_volume(sc, volname, -1);
2063 		pv = malloc(sizeof(*pv), M_MD_INTEL, M_WAITOK | M_ZERO);
2064 		pv->pv_volume_pos = i;
2065 		vol->v_md_data = pv;
2066 		vol->v_raid_level = level;
2067 		vol->v_raid_level_qualifier = qual;
2068 		vol->v_strip_size = strip;
2069 		vol->v_disks_count = numdisks;
2070 		if (level == G_RAID_VOLUME_RL_RAID0)
2071 			vol->v_mediasize = size * numdisks;
2072 		else if (level == G_RAID_VOLUME_RL_RAID1)
2073 			vol->v_mediasize = size;
2074 		else if (level == G_RAID_VOLUME_RL_RAID5)
2075 			vol->v_mediasize = size * (numdisks - 1);
2076 		else { /* RAID1E */
2077 			vol->v_mediasize = ((size * numdisks) / strip / 2) *
2078 			    strip;
2079 		}
2080 		vol->v_sectorsize = sectorsize;
2081 		g_raid_start_volume(vol);
2082 
2083 		/* , and subdisks. */
2084 		for (i = 0; i < numdisks; i++) {
2085 			disk = vol1->v_subdisks[i].sd_disk;
2086 			sd = &vol->v_subdisks[i];
2087 			sd->sd_disk = disk;
2088 			sd->sd_offset = off;
2089 			sd->sd_size = size;
2090 			TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
2091 			if (disk->d_state == G_RAID_DISK_S_ACTIVE) {
2092 				if (level == G_RAID_VOLUME_RL_RAID5)
2093 					g_raid_change_subdisk_state(sd,
2094 					    G_RAID_SUBDISK_S_UNINITIALIZED);
2095 				else
2096 					g_raid_change_subdisk_state(sd,
2097 					    G_RAID_SUBDISK_S_ACTIVE);
2098 				g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
2099 				    G_RAID_EVENT_SUBDISK);
2100 			}
2101 		}
2102 
2103 		/* Write metadata based on created entities. */
2104 		g_raid_md_write_intel(md, NULL, NULL, NULL);
2105 
2106 		g_raid_event_send(vol, G_RAID_VOLUME_E_START,
2107 		    G_RAID_EVENT_VOLUME);
2108 		return (0);
2109 	}
2110 	if (strcmp(verb, "delete") == 0) {
2111 
2112 		nodename = gctl_get_asciiparam(req, "arg0");
2113 		if (nodename != NULL && strcasecmp(sc->sc_name, nodename) != 0)
2114 			nodename = NULL;
2115 
2116 		/* Full node destruction. */
2117 		if (*nargs == 1 && nodename != NULL) {
2118 			/* Check if some volume is still open. */
2119 			force = gctl_get_paraml(req, "force", sizeof(*force));
2120 			if (force != NULL && *force == 0 &&
2121 			    g_raid_nopens(sc) != 0) {
2122 				gctl_error(req, "Some volume is still open.");
2123 				return (-4);
2124 			}
2125 
2126 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2127 				if (disk->d_consumer)
2128 					intel_meta_erase(disk->d_consumer);
2129 			}
2130 			g_raid_destroy_node(sc, 0);
2131 			return (0);
2132 		}
2133 
2134 		/* Destroy specified volume. If it was last - all node. */
2135 		if (*nargs > 2) {
2136 			gctl_error(req, "Invalid number of arguments.");
2137 			return (-1);
2138 		}
2139 		volname = gctl_get_asciiparam(req,
2140 		    nodename != NULL ? "arg1" : "arg0");
2141 		if (volname == NULL) {
2142 			gctl_error(req, "No volume name.");
2143 			return (-2);
2144 		}
2145 
2146 		/* Search for volume. */
2147 		TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2148 			if (strcmp(vol->v_name, volname) == 0)
2149 				break;
2150 			pp = vol->v_provider;
2151 			if (pp == NULL)
2152 				continue;
2153 			if (strcmp(pp->name, volname) == 0)
2154 				break;
2155 			if (strncmp(pp->name, "raid/", 5) == 0 &&
2156 			    strcmp(pp->name + 5, volname) == 0)
2157 				break;
2158 		}
2159 		if (vol == NULL) {
2160 			i = strtol(volname, &tmp, 10);
2161 			if (verb != volname && tmp[0] == 0) {
2162 				TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2163 					if (vol->v_global_id == i)
2164 						break;
2165 				}
2166 			}
2167 		}
2168 		if (vol == NULL) {
2169 			gctl_error(req, "Volume '%s' not found.", volname);
2170 			return (-3);
2171 		}
2172 
2173 		/* Check if volume is still open. */
2174 		force = gctl_get_paraml(req, "force", sizeof(*force));
2175 		if (force != NULL && *force == 0 &&
2176 		    vol->v_provider_open != 0) {
2177 			gctl_error(req, "Volume is still open.");
2178 			return (-4);
2179 		}
2180 
2181 		/* Destroy volume and potentially node. */
2182 		i = 0;
2183 		TAILQ_FOREACH(vol1, &sc->sc_volumes, v_next)
2184 			i++;
2185 		if (i >= 2) {
2186 			g_raid_destroy_volume(vol);
2187 			g_raid_md_write_intel(md, NULL, NULL, NULL);
2188 		} else {
2189 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2190 				if (disk->d_consumer)
2191 					intel_meta_erase(disk->d_consumer);
2192 			}
2193 			g_raid_destroy_node(sc, 0);
2194 		}
2195 		return (0);
2196 	}
2197 	if (strcmp(verb, "remove") == 0 ||
2198 	    strcmp(verb, "fail") == 0) {
2199 		if (*nargs < 2) {
2200 			gctl_error(req, "Invalid number of arguments.");
2201 			return (-1);
2202 		}
2203 		for (i = 1; i < *nargs; i++) {
2204 			snprintf(arg, sizeof(arg), "arg%d", i);
2205 			diskname = gctl_get_asciiparam(req, arg);
2206 			if (diskname == NULL) {
2207 				gctl_error(req, "No disk name (%s).", arg);
2208 				error = -2;
2209 				break;
2210 			}
2211 			if (strncmp(diskname, "/dev/", 5) == 0)
2212 				diskname += 5;
2213 
2214 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2215 				if (disk->d_consumer != NULL &&
2216 				    disk->d_consumer->provider != NULL &&
2217 				    strcmp(disk->d_consumer->provider->name,
2218 				     diskname) == 0)
2219 					break;
2220 			}
2221 			if (disk == NULL) {
2222 				gctl_error(req, "Disk '%s' not found.",
2223 				    diskname);
2224 				error = -3;
2225 				break;
2226 			}
2227 
2228 			if (strcmp(verb, "fail") == 0) {
2229 				g_raid_md_fail_disk_intel(md, NULL, disk);
2230 				continue;
2231 			}
2232 
2233 			pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
2234 
2235 			/* Erase metadata on deleting disk. */
2236 			intel_meta_erase(disk->d_consumer);
2237 
2238 			/* If disk was assigned, just update statuses. */
2239 			if (pd->pd_disk_pos >= 0) {
2240 				g_raid_change_disk_state(disk, G_RAID_DISK_S_OFFLINE);
2241 				g_raid_kill_consumer(sc, disk->d_consumer);
2242 				disk->d_consumer = NULL;
2243 				TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
2244 					g_raid_change_subdisk_state(sd,
2245 					    G_RAID_SUBDISK_S_NONE);
2246 					g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
2247 					    G_RAID_EVENT_SUBDISK);
2248 				}
2249 			} else {
2250 				/* Otherwise -- delete. */
2251 				g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
2252 				g_raid_destroy_disk(disk);
2253 			}
2254 		}
2255 
2256 		/* Write updated metadata to remaining disks. */
2257 		g_raid_md_write_intel(md, NULL, NULL, NULL);
2258 
2259 		/* Check if anything left except placeholders. */
2260 		if (g_raid_ndisks(sc, -1) ==
2261 		    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
2262 			g_raid_destroy_node(sc, 0);
2263 		else
2264 			g_raid_md_intel_refill(sc);
2265 		return (error);
2266 	}
2267 	if (strcmp(verb, "insert") == 0) {
2268 		if (*nargs < 2) {
2269 			gctl_error(req, "Invalid number of arguments.");
2270 			return (-1);
2271 		}
2272 		update = 0;
2273 		for (i = 1; i < *nargs; i++) {
2274 			/* Get disk name. */
2275 			snprintf(arg, sizeof(arg), "arg%d", i);
2276 			diskname = gctl_get_asciiparam(req, arg);
2277 			if (diskname == NULL) {
2278 				gctl_error(req, "No disk name (%s).", arg);
2279 				error = -3;
2280 				break;
2281 			}
2282 
2283 			/* Try to find provider with specified name. */
2284 			g_topology_lock();
2285 			cp = g_raid_open_consumer(sc, diskname);
2286 			if (cp == NULL) {
2287 				gctl_error(req, "Can't open disk '%s'.",
2288 				    diskname);
2289 				g_topology_unlock();
2290 				error = -4;
2291 				break;
2292 			}
2293 			pp = cp->provider;
2294 			g_topology_unlock();
2295 
2296 			/* Read disk serial. */
2297 			error = g_raid_md_get_label(cp,
2298 			    &serial[0], INTEL_SERIAL_LEN);
2299 			if (error != 0) {
2300 				gctl_error(req,
2301 				    "Can't get serial for provider '%s'.",
2302 				    diskname);
2303 				g_raid_kill_consumer(sc, cp);
2304 				error = -7;
2305 				break;
2306 			}
2307 
2308 			pd = malloc(sizeof(*pd), M_MD_INTEL, M_WAITOK | M_ZERO);
2309 			pd->pd_disk_pos = -1;
2310 
2311 			disk = g_raid_create_disk(sc);
2312 			disk->d_consumer = cp;
2313 			disk->d_md_data = (void *)pd;
2314 			cp->private = disk;
2315 
2316 			g_raid_get_disk_info(disk);
2317 
2318 			memcpy(&pd->pd_disk_meta.serial[0], &serial[0],
2319 			    INTEL_SERIAL_LEN);
2320 			intel_set_disk_sectors(&pd->pd_disk_meta,
2321 			    pp->mediasize / pp->sectorsize);
2322 			pd->pd_disk_meta.id = 0;
2323 			pd->pd_disk_meta.flags = INTEL_F_SPARE;
2324 
2325 			/* Welcome the "new" disk. */
2326 			update += g_raid_md_intel_start_disk(disk);
2327 			if (disk->d_state == G_RAID_DISK_S_SPARE) {
2328 				intel_meta_write_spare(cp, &pd->pd_disk_meta);
2329 				g_raid_destroy_disk(disk);
2330 			} else if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
2331 				gctl_error(req, "Disk '%s' doesn't fit.",
2332 				    diskname);
2333 				g_raid_destroy_disk(disk);
2334 				error = -8;
2335 				break;
2336 			}
2337 		}
2338 
2339 		/* Write new metadata if we changed something. */
2340 		if (update)
2341 			g_raid_md_write_intel(md, NULL, NULL, NULL);
2342 		return (error);
2343 	}
2344 	return (-100);
2345 }
2346 
2347 static int
2348 g_raid_md_write_intel(struct g_raid_md_object *md, struct g_raid_volume *tvol,
2349     struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
2350 {
2351 	struct g_raid_softc *sc;
2352 	struct g_raid_volume *vol;
2353 	struct g_raid_subdisk *sd;
2354 	struct g_raid_disk *disk;
2355 	struct g_raid_md_intel_object *mdi;
2356 	struct g_raid_md_intel_pervolume *pv;
2357 	struct g_raid_md_intel_perdisk *pd;
2358 	struct intel_raid_conf *meta;
2359 	struct intel_raid_vol *mvol;
2360 	struct intel_raid_map *mmap0, *mmap1;
2361 	off_t sectorsize = 512, pos;
2362 	const char *version, *cv;
2363 	int vi, sdi, numdisks, len, state, stale;
2364 
2365 	sc = md->mdo_softc;
2366 	mdi = (struct g_raid_md_intel_object *)md;
2367 
2368 	if (sc->sc_stopping == G_RAID_DESTROY_HARD)
2369 		return (0);
2370 
2371 	/* Bump generation. Newly written metadata may differ from previous. */
2372 	mdi->mdio_generation++;
2373 
2374 	/* Count number of disks. */
2375 	numdisks = 0;
2376 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2377 		pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
2378 		if (pd->pd_disk_pos < 0)
2379 			continue;
2380 		numdisks++;
2381 		if (disk->d_state == G_RAID_DISK_S_ACTIVE) {
2382 			pd->pd_disk_meta.flags =
2383 			    INTEL_F_ONLINE | INTEL_F_ASSIGNED;
2384 		} else if (disk->d_state == G_RAID_DISK_S_FAILED) {
2385 			pd->pd_disk_meta.flags = INTEL_F_FAILED |
2386 			    INTEL_F_ASSIGNED;
2387 		} else if (disk->d_state == G_RAID_DISK_S_DISABLED) {
2388 			pd->pd_disk_meta.flags = INTEL_F_FAILED |
2389 			    INTEL_F_ASSIGNED | INTEL_F_DISABLED;
2390 		} else {
2391 			if (!(pd->pd_disk_meta.flags & INTEL_F_DISABLED))
2392 				pd->pd_disk_meta.flags = INTEL_F_ASSIGNED;
2393 			if (pd->pd_disk_meta.id != 0xffffffff) {
2394 				pd->pd_disk_meta.id = 0xffffffff;
2395 				len = strlen(pd->pd_disk_meta.serial);
2396 				len = min(len, INTEL_SERIAL_LEN - 3);
2397 				strcpy(pd->pd_disk_meta.serial + len, ":0");
2398 			}
2399 		}
2400 	}
2401 
2402 	/* Fill anchor and disks. */
2403 	meta = malloc(INTEL_MAX_MD_SIZE(numdisks),
2404 	    M_MD_INTEL, M_WAITOK | M_ZERO);
2405 	memcpy(&meta->intel_id[0], INTEL_MAGIC, sizeof(INTEL_MAGIC) - 1);
2406 	meta->config_size = INTEL_MAX_MD_SIZE(numdisks);
2407 	meta->config_id = mdi->mdio_config_id;
2408 	meta->orig_config_id = mdi->mdio_orig_config_id;
2409 	meta->generation = mdi->mdio_generation;
2410 	meta->attributes = INTEL_ATTR_CHECKSUM;
2411 	meta->total_disks = numdisks;
2412 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2413 		pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
2414 		if (pd->pd_disk_pos < 0)
2415 			continue;
2416 		meta->disk[pd->pd_disk_pos] = pd->pd_disk_meta;
2417 		if (pd->pd_disk_meta.sectors_hi != 0)
2418 			meta->attributes |= INTEL_ATTR_2TB_DISK;
2419 	}
2420 
2421 	/* Fill volumes and maps. */
2422 	vi = 0;
2423 	version = INTEL_VERSION_1000;
2424 	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2425 		pv = vol->v_md_data;
2426 		if (vol->v_stopping)
2427 			continue;
2428 		mvol = intel_get_volume(meta, vi);
2429 
2430 		/* New metadata may have different volumes order. */
2431 		pv->pv_volume_pos = vi;
2432 
2433 		for (sdi = 0; sdi < vol->v_disks_count; sdi++) {
2434 			sd = &vol->v_subdisks[sdi];
2435 			if (sd->sd_disk != NULL)
2436 				break;
2437 		}
2438 		if (sdi >= vol->v_disks_count)
2439 			panic("No any filled subdisk in volume");
2440 		if (vol->v_mediasize >= 0x20000000000llu)
2441 			meta->attributes |= INTEL_ATTR_2TB;
2442 		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0)
2443 			meta->attributes |= INTEL_ATTR_RAID0;
2444 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1)
2445 			meta->attributes |= INTEL_ATTR_RAID1;
2446 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID5)
2447 			meta->attributes |= INTEL_ATTR_RAID5;
2448 		else if ((vol->v_disks_count & 1) == 0)
2449 			meta->attributes |= INTEL_ATTR_RAID10;
2450 		else
2451 			meta->attributes |= INTEL_ATTR_RAID1E;
2452 		if (pv->pv_cng)
2453 			meta->attributes |= INTEL_ATTR_RAIDCNG;
2454 		if (vol->v_strip_size > 131072)
2455 			meta->attributes |= INTEL_ATTR_EXT_STRIP;
2456 
2457 		if (pv->pv_cng)
2458 			cv = INTEL_VERSION_1206;
2459 		else if (vol->v_disks_count > 4)
2460 			cv = INTEL_VERSION_1204;
2461 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID5)
2462 			cv = INTEL_VERSION_1202;
2463 		else if (vol->v_disks_count > 2)
2464 			cv = INTEL_VERSION_1201;
2465 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1)
2466 			cv = INTEL_VERSION_1100;
2467 		else
2468 			cv = INTEL_VERSION_1000;
2469 		if (strcmp(cv, version) > 0)
2470 			version = cv;
2471 
2472 		strlcpy(&mvol->name[0], vol->v_name, sizeof(mvol->name));
2473 		mvol->total_sectors = vol->v_mediasize / sectorsize;
2474 		mvol->state = (INTEL_ST_READ_COALESCING |
2475 		    INTEL_ST_WRITE_COALESCING);
2476 		mvol->tid = vol->v_global_id + 1;
2477 		if (pv->pv_cng) {
2478 			mvol->state |= INTEL_ST_CLONE_N_GO;
2479 			if (pv->pv_cng_man_sync)
2480 				mvol->state |= INTEL_ST_CLONE_MAN_SYNC;
2481 			mvol->cng_master_disk = pv->pv_cng_master_disk;
2482 			if (vol->v_subdisks[pv->pv_cng_master_disk].sd_state ==
2483 			    G_RAID_SUBDISK_S_NONE)
2484 				mvol->cng_state = INTEL_CNGST_MASTER_MISSING;
2485 			else if (vol->v_state != G_RAID_VOLUME_S_OPTIMAL)
2486 				mvol->cng_state = INTEL_CNGST_NEEDS_UPDATE;
2487 			else
2488 				mvol->cng_state = INTEL_CNGST_UPDATED;
2489 		}
2490 
2491 		/* Check for any recovery in progress. */
2492 		state = G_RAID_SUBDISK_S_ACTIVE;
2493 		pos = 0x7fffffffffffffffllu;
2494 		stale = 0;
2495 		for (sdi = 0; sdi < vol->v_disks_count; sdi++) {
2496 			sd = &vol->v_subdisks[sdi];
2497 			if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD)
2498 				state = G_RAID_SUBDISK_S_REBUILD;
2499 			else if (sd->sd_state == G_RAID_SUBDISK_S_RESYNC &&
2500 			    state != G_RAID_SUBDISK_S_REBUILD)
2501 				state = G_RAID_SUBDISK_S_RESYNC;
2502 			else if (sd->sd_state == G_RAID_SUBDISK_S_STALE)
2503 				stale = 1;
2504 			if ((sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
2505 			    sd->sd_state == G_RAID_SUBDISK_S_RESYNC) &&
2506 			     sd->sd_rebuild_pos < pos)
2507 			        pos = sd->sd_rebuild_pos;
2508 		}
2509 		if (state == G_RAID_SUBDISK_S_REBUILD) {
2510 			mvol->migr_state = 1;
2511 			mvol->migr_type = INTEL_MT_REBUILD;
2512 		} else if (state == G_RAID_SUBDISK_S_RESYNC) {
2513 			mvol->migr_state = 1;
2514 			/* mvol->migr_type = INTEL_MT_REPAIR; */
2515 			mvol->migr_type = INTEL_MT_VERIFY;
2516 			mvol->state |= INTEL_ST_VERIFY_AND_FIX;
2517 		} else
2518 			mvol->migr_state = 0;
2519 		mvol->dirty = (vol->v_dirty || stale);
2520 
2521 		mmap0 = intel_get_map(mvol, 0);
2522 
2523 		/* Write map / common part of two maps. */
2524 		intel_set_map_offset(mmap0, sd->sd_offset / sectorsize);
2525 		intel_set_map_disk_sectors(mmap0, sd->sd_size / sectorsize);
2526 		mmap0->strip_sectors = vol->v_strip_size / sectorsize;
2527 		if (vol->v_state == G_RAID_VOLUME_S_BROKEN)
2528 			mmap0->status = INTEL_S_FAILURE;
2529 		else if (vol->v_state == G_RAID_VOLUME_S_DEGRADED)
2530 			mmap0->status = INTEL_S_DEGRADED;
2531 		else if (g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_UNINITIALIZED)
2532 		    == g_raid_nsubdisks(vol, -1))
2533 			mmap0->status = INTEL_S_UNINITIALIZED;
2534 		else
2535 			mmap0->status = INTEL_S_READY;
2536 		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID0)
2537 			mmap0->type = INTEL_T_RAID0;
2538 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 ||
2539 		    vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E)
2540 			mmap0->type = INTEL_T_RAID1;
2541 		else
2542 			mmap0->type = INTEL_T_RAID5;
2543 		mmap0->total_disks = vol->v_disks_count;
2544 		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1)
2545 			mmap0->total_domains = vol->v_disks_count;
2546 		else if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E)
2547 			mmap0->total_domains = 2;
2548 		else
2549 			mmap0->total_domains = 1;
2550 		intel_set_map_stripe_count(mmap0,
2551 		    sd->sd_size / vol->v_strip_size / mmap0->total_domains);
2552 		mmap0->failed_disk_num = 0xff;
2553 		mmap0->ddf = 1;
2554 
2555 		/* If there are two maps - copy common and update. */
2556 		if (mvol->migr_state) {
2557 			intel_set_vol_curr_migr_unit(mvol,
2558 			    pos / vol->v_strip_size / mmap0->total_domains);
2559 			mmap1 = intel_get_map(mvol, 1);
2560 			memcpy(mmap1, mmap0, sizeof(struct intel_raid_map));
2561 			mmap0->status = INTEL_S_READY;
2562 		} else
2563 			mmap1 = NULL;
2564 
2565 		/* Write disk indexes and put rebuild flags. */
2566 		for (sdi = 0; sdi < vol->v_disks_count; sdi++) {
2567 			sd = &vol->v_subdisks[sdi];
2568 			pd = (struct g_raid_md_intel_perdisk *)
2569 			    sd->sd_disk->d_md_data;
2570 			mmap0->disk_idx[sdi] = pd->pd_disk_pos;
2571 			if (mvol->migr_state)
2572 				mmap1->disk_idx[sdi] = pd->pd_disk_pos;
2573 			if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
2574 			    sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
2575 				mmap1->disk_idx[sdi] |= INTEL_DI_RBLD;
2576 			} else if (sd->sd_state != G_RAID_SUBDISK_S_ACTIVE &&
2577 			    sd->sd_state != G_RAID_SUBDISK_S_STALE &&
2578 			    sd->sd_state != G_RAID_SUBDISK_S_UNINITIALIZED) {
2579 				mmap0->disk_idx[sdi] |= INTEL_DI_RBLD;
2580 				if (mvol->migr_state)
2581 					mmap1->disk_idx[sdi] |= INTEL_DI_RBLD;
2582 			}
2583 			if ((sd->sd_state == G_RAID_SUBDISK_S_NONE ||
2584 			     sd->sd_state == G_RAID_SUBDISK_S_FAILED ||
2585 			     sd->sd_state == G_RAID_SUBDISK_S_REBUILD) &&
2586 			    mmap0->failed_disk_num == 0xff) {
2587 				mmap0->failed_disk_num = sdi;
2588 				if (mvol->migr_state)
2589 					mmap1->failed_disk_num = sdi;
2590 			}
2591 		}
2592 		vi++;
2593 	}
2594 	meta->total_volumes = vi;
2595 	if (vi > 1 || meta->attributes &
2596 	     (INTEL_ATTR_EXT_STRIP | INTEL_ATTR_2TB_DISK | INTEL_ATTR_2TB))
2597 		version = INTEL_VERSION_1300;
2598 	if (strcmp(version, INTEL_VERSION_1300) < 0)
2599 		meta->attributes &= INTEL_ATTR_CHECKSUM;
2600 	memcpy(&meta->version[0], version, sizeof(INTEL_VERSION_1000) - 1);
2601 
2602 	/* We are done. Print meta data and store them to disks. */
2603 	g_raid_md_intel_print(meta);
2604 	if (mdi->mdio_meta != NULL)
2605 		free(mdi->mdio_meta, M_MD_INTEL);
2606 	mdi->mdio_meta = meta;
2607 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2608 		pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
2609 		if (disk->d_state != G_RAID_DISK_S_ACTIVE)
2610 			continue;
2611 		if (pd->pd_meta != NULL) {
2612 			free(pd->pd_meta, M_MD_INTEL);
2613 			pd->pd_meta = NULL;
2614 		}
2615 		pd->pd_meta = intel_meta_copy(meta);
2616 		intel_meta_write(disk->d_consumer, meta);
2617 	}
2618 	return (0);
2619 }
2620 
2621 static int
2622 g_raid_md_fail_disk_intel(struct g_raid_md_object *md,
2623     struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
2624 {
2625 	struct g_raid_softc *sc;
2626 	struct g_raid_md_intel_object *mdi;
2627 	struct g_raid_md_intel_perdisk *pd;
2628 	struct g_raid_subdisk *sd;
2629 
2630 	sc = md->mdo_softc;
2631 	mdi = (struct g_raid_md_intel_object *)md;
2632 	pd = (struct g_raid_md_intel_perdisk *)tdisk->d_md_data;
2633 
2634 	/* We can't fail disk that is not a part of array now. */
2635 	if (pd->pd_disk_pos < 0)
2636 		return (-1);
2637 
2638 	/*
2639 	 * Mark disk as failed in metadata and try to write that metadata
2640 	 * to the disk itself to prevent it's later resurrection as STALE.
2641 	 */
2642 	mdi->mdio_meta->disk[pd->pd_disk_pos].flags = INTEL_F_FAILED;
2643 	pd->pd_disk_meta.flags = INTEL_F_FAILED;
2644 	g_raid_md_intel_print(mdi->mdio_meta);
2645 	if (tdisk->d_consumer)
2646 		intel_meta_write(tdisk->d_consumer, mdi->mdio_meta);
2647 
2648 	/* Change states. */
2649 	g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED);
2650 	TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) {
2651 		g_raid_change_subdisk_state(sd,
2652 		    G_RAID_SUBDISK_S_FAILED);
2653 		g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED,
2654 		    G_RAID_EVENT_SUBDISK);
2655 	}
2656 
2657 	/* Write updated metadata to remaining disks. */
2658 	g_raid_md_write_intel(md, NULL, NULL, tdisk);
2659 
2660 	/* Check if anything left except placeholders. */
2661 	if (g_raid_ndisks(sc, -1) ==
2662 	    g_raid_ndisks(sc, G_RAID_DISK_S_OFFLINE))
2663 		g_raid_destroy_node(sc, 0);
2664 	else
2665 		g_raid_md_intel_refill(sc);
2666 	return (0);
2667 }
2668 
2669 static int
2670 g_raid_md_free_disk_intel(struct g_raid_md_object *md,
2671     struct g_raid_disk *disk)
2672 {
2673 	struct g_raid_md_intel_perdisk *pd;
2674 
2675 	pd = (struct g_raid_md_intel_perdisk *)disk->d_md_data;
2676 	if (pd->pd_meta != NULL) {
2677 		free(pd->pd_meta, M_MD_INTEL);
2678 		pd->pd_meta = NULL;
2679 	}
2680 	free(pd, M_MD_INTEL);
2681 	disk->d_md_data = NULL;
2682 	return (0);
2683 }
2684 
2685 static int
2686 g_raid_md_free_volume_intel(struct g_raid_md_object *md,
2687     struct g_raid_volume *vol)
2688 {
2689 	struct g_raid_md_intel_pervolume *pv;
2690 
2691 	pv = (struct g_raid_md_intel_pervolume *)vol->v_md_data;
2692 	free(pv, M_MD_INTEL);
2693 	vol->v_md_data = NULL;
2694 	return (0);
2695 }
2696 
2697 static int
2698 g_raid_md_free_intel(struct g_raid_md_object *md)
2699 {
2700 	struct g_raid_md_intel_object *mdi;
2701 
2702 	mdi = (struct g_raid_md_intel_object *)md;
2703 	if (!mdi->mdio_started) {
2704 		mdi->mdio_started = 0;
2705 		callout_stop(&mdi->mdio_start_co);
2706 		G_RAID_DEBUG1(1, md->mdo_softc,
2707 		    "root_mount_rel %p", mdi->mdio_rootmount);
2708 		root_mount_rel(mdi->mdio_rootmount);
2709 		mdi->mdio_rootmount = NULL;
2710 	}
2711 	if (mdi->mdio_meta != NULL) {
2712 		free(mdi->mdio_meta, M_MD_INTEL);
2713 		mdi->mdio_meta = NULL;
2714 	}
2715 	return (0);
2716 }
2717 
2718 G_RAID_MD_DECLARE(intel, "Intel");
2719