xref: /dragonfly/sys/dev/disk/nata/ata-raid.c (revision 73e0051e)
1 /*-
2  * Copyright (c) 2000 - 2006 S�ren Schmidt <sos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/ata/ata-raid.c,v 1.120 2006/04/15 10:27:41 maxim Exp $
27  * $DragonFly: src/sys/dev/disk/nata/ata-raid.c,v 1.11 2008/08/30 02:56:11 dillon Exp $
28  */
29 
30 #include "opt_ata.h"
31 
32 #include <sys/param.h>
33 #include <sys/bio.h>
34 #include <sys/buf.h>
35 #include <sys/buf2.h>
36 #include <sys/bus.h>
37 #include <sys/conf.h>
38 #include <sys/device.h>
39 #include <sys/disk.h>
40 #include <sys/endian.h>
41 #include <sys/libkern.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/nata.h>
45 #include <sys/spinlock2.h>
46 #include <sys/systm.h>
47 
48 #include <vm/pmap.h>
49 
50 #include <machine/md_var.h>
51 
52 #include <bus/pci/pcivar.h>
53 
54 #include "ata-all.h"
55 #include "ata-disk.h"
56 #include "ata-raid.h"
57 #include "ata-pci.h"
58 #include "ata_if.h"
59 
60 
61 /* device structure */
62 static	d_strategy_t	ata_raid_strategy;
63 static	d_dump_t	ata_raid_dump;
64 static struct dev_ops ar_ops = {
65 	{ "ar", 157, D_DISK },
66 	.d_open =	nullopen,
67 	.d_close =	nullclose,
68 	.d_read =	physread,
69 	.d_write =	physwrite,
70 	.d_strategy =	ata_raid_strategy,
71 	.d_dump =	ata_raid_dump,
72 };
73 
74 /* prototypes */
75 static void ata_raid_done(struct ata_request *request);
76 static void ata_raid_config_changed(struct ar_softc *rdp, int writeback);
77 static int ata_raid_status(struct ata_ioc_raid_config *config);
78 static int ata_raid_create(struct ata_ioc_raid_config *config);
79 static int ata_raid_delete(int array);
80 static int ata_raid_addspare(struct ata_ioc_raid_config *config);
81 static int ata_raid_rebuild(int array);
82 static int ata_raid_read_metadata(device_t subdisk);
83 static int ata_raid_write_metadata(struct ar_softc *rdp);
84 static int ata_raid_wipe_metadata(struct ar_softc *rdp);
85 static int ata_raid_adaptec_read_meta(device_t dev, struct ar_softc **raidp);
86 static int ata_raid_hptv2_read_meta(device_t dev, struct ar_softc **raidp);
87 static int ata_raid_hptv2_write_meta(struct ar_softc *rdp);
88 static int ata_raid_hptv3_read_meta(device_t dev, struct ar_softc **raidp);
89 static int ata_raid_intel_read_meta(device_t dev, struct ar_softc **raidp);
90 static int ata_raid_intel_write_meta(struct ar_softc *rdp);
91 static int ata_raid_ite_read_meta(device_t dev, struct ar_softc **raidp);
92 static int ata_raid_jmicron_read_meta(device_t dev, struct ar_softc **raidp);
93 static int ata_raid_jmicron_write_meta(struct ar_softc *rdp);
94 static int ata_raid_lsiv2_read_meta(device_t dev, struct ar_softc **raidp);
95 static int ata_raid_lsiv3_read_meta(device_t dev, struct ar_softc **raidp);
96 static int ata_raid_nvidia_read_meta(device_t dev, struct ar_softc **raidp);
97 static int ata_raid_promise_read_meta(device_t dev, struct ar_softc **raidp, int native);
98 static int ata_raid_promise_write_meta(struct ar_softc *rdp);
99 static int ata_raid_sii_read_meta(device_t dev, struct ar_softc **raidp);
100 static int ata_raid_sis_read_meta(device_t dev, struct ar_softc **raidp);
101 static int ata_raid_sis_write_meta(struct ar_softc *rdp);
102 static int ata_raid_via_read_meta(device_t dev, struct ar_softc **raidp);
103 static int ata_raid_via_write_meta(struct ar_softc *rdp);
104 static struct ata_request *ata_raid_init_request(struct ar_softc *rdp, struct bio *bio);
105 static int ata_raid_send_request(struct ata_request *request);
106 static int ata_raid_rw(device_t dev, u_int64_t lba, void *data, u_int bcount, int flags);
107 static char * ata_raid_format(struct ar_softc *rdp);
108 static char * ata_raid_type(struct ar_softc *rdp);
109 static char * ata_raid_flags(struct ar_softc *rdp);
110 
111 /* debugging only */
112 static void ata_raid_print_meta(struct ar_softc *meta);
113 static void ata_raid_adaptec_print_meta(struct adaptec_raid_conf *meta);
114 static void ata_raid_hptv2_print_meta(struct hptv2_raid_conf *meta);
115 static void ata_raid_hptv3_print_meta(struct hptv3_raid_conf *meta);
116 static void ata_raid_intel_print_meta(struct intel_raid_conf *meta);
117 static void ata_raid_ite_print_meta(struct ite_raid_conf *meta);
118 static void ata_raid_jmicron_print_meta(struct jmicron_raid_conf *meta);
119 static void ata_raid_lsiv2_print_meta(struct lsiv2_raid_conf *meta);
120 static void ata_raid_lsiv3_print_meta(struct lsiv3_raid_conf *meta);
121 static void ata_raid_nvidia_print_meta(struct nvidia_raid_conf *meta);
122 static void ata_raid_promise_print_meta(struct promise_raid_conf *meta);
123 static void ata_raid_sii_print_meta(struct sii_raid_conf *meta);
124 static void ata_raid_sis_print_meta(struct sis_raid_conf *meta);
125 static void ata_raid_via_print_meta(struct via_raid_conf *meta);
126 
127 /* internal vars */
128 static struct ar_softc *ata_raid_arrays[MAX_ARRAYS];
129 static MALLOC_DEFINE(M_AR, "ar_driver", "ATA PseudoRAID driver");
130 static devclass_t ata_raid_sub_devclass;
131 static int testing = 0;
132 
133 static void
134 ata_raid_attach(struct ar_softc *rdp, int writeback)
135 {
136     struct disk_info info;
137     cdev_t cdev;
138     char buffer[32];
139     int disk;
140 
141     spin_init(&rdp->lock);
142     ata_raid_config_changed(rdp, writeback);
143 
144     /* sanitize arrays total_size % (width * interleave) == 0 */
145     if (rdp->type == AR_T_RAID0 || rdp->type == AR_T_RAID01 ||
146 	rdp->type == AR_T_RAID5) {
147 	rdp->total_sectors = (rdp->total_sectors/(rdp->interleave*rdp->width))*
148 			     (rdp->interleave * rdp->width);
149 	ksprintf(buffer, " (stripe %d KB)",
150 		(rdp->interleave * DEV_BSIZE) / 1024);
151     }
152     else
153 	buffer[0] = '\0';
154     /* XXX TGEN add devstats? */
155     cdev = disk_create(rdp->lun, &rdp->disk, &ar_ops);
156     cdev->si_drv1 = rdp;
157     cdev->si_iosize_max = 128 * DEV_BSIZE;
158     rdp->cdev = cdev;
159 
160     bzero(&info, sizeof(info));
161     info.d_media_blksize = DEV_BSIZE;		/* mandatory */
162     info.d_media_blocks = rdp->total_sectors;
163 
164     info.d_secpertrack = rdp->sectors;		/* optional */
165     info.d_nheads = rdp->heads;
166     info.d_ncylinders = rdp->total_sectors/(rdp->heads*rdp->sectors);
167     info.d_secpercyl = rdp->sectors * rdp->heads;
168 
169     kprintf("ar%d: %juMB <%s %s%s> status: %s\n", rdp->lun,
170 	   rdp->total_sectors / ((1024L * 1024L) / DEV_BSIZE),
171 	   ata_raid_format(rdp), ata_raid_type(rdp),
172 	   buffer, ata_raid_flags(rdp));
173 
174     if (testing || bootverbose)
175 	kprintf("ar%d: %ju sectors [%dC/%dH/%dS] <%s> subdisks defined as:\n",
176 	       rdp->lun, rdp->total_sectors,
177 	       rdp->cylinders, rdp->heads, rdp->sectors, rdp->name);
178 
179     for (disk = 0; disk < rdp->total_disks; disk++) {
180 	kprintf("ar%d: disk%d ", rdp->lun, disk);
181 	if (rdp->disks[disk].dev) {
182 	    if (rdp->disks[disk].flags & AR_DF_PRESENT) {
183 		/* status of this disk in the array */
184 		if (rdp->disks[disk].flags & AR_DF_ONLINE)
185 		    kprintf("READY ");
186 		else if (rdp->disks[disk].flags & AR_DF_SPARE)
187 		    kprintf("SPARE ");
188 		else
189 		    kprintf("FREE  ");
190 
191 		/* what type of disk is this in the array */
192 		switch (rdp->type) {
193 		case AR_T_RAID1:
194 		case AR_T_RAID01:
195 		    if (disk < rdp->width)
196 			kprintf("(master) ");
197 		    else
198 			kprintf("(mirror) ");
199 		}
200 
201 		/* which physical disk is used */
202 		kprintf("using %s at ata%d-%s\n",
203 		       device_get_nameunit(rdp->disks[disk].dev),
204 		       device_get_unit(device_get_parent(rdp->disks[disk].dev)),
205 		       (((struct ata_device *)
206 			 device_get_softc(rdp->disks[disk].dev))->unit ==
207 			 ATA_MASTER) ? "master" : "slave");
208 	    }
209 	    else if (rdp->disks[disk].flags & AR_DF_ASSIGNED)
210 		kprintf("DOWN\n");
211 	    else
212 		kprintf("INVALID no RAID config on this subdisk\n");
213 	}
214 	else
215 	    kprintf("DOWN no device found for this subdisk\n");
216     }
217 
218     disk_setdiskinfo(&rdp->disk, &info);
219 }
220 
221 /*
222  * ATA PseudoRAID ioctl function. Note that this does not need to be adjusted
223  * to the dev_ops way, because it's just chained from the generic ata ioctl.
224  */
225 static int
226 ata_raid_ioctl(u_long cmd, caddr_t data)
227 {
228     struct ata_ioc_raid_config *config = (struct ata_ioc_raid_config *)data;
229     int *lun = (int *)data;
230     int error = EOPNOTSUPP;
231 
232     switch (cmd) {
233     case IOCATARAIDSTATUS:
234 	error = ata_raid_status(config);
235 	break;
236 
237     case IOCATARAIDCREATE:
238 	error = ata_raid_create(config);
239 	break;
240 
241     case IOCATARAIDDELETE:
242 	error = ata_raid_delete(*lun);
243 	break;
244 
245     case IOCATARAIDADDSPARE:
246 	error = ata_raid_addspare(config);
247 	break;
248 
249     case IOCATARAIDREBUILD:
250 	error = ata_raid_rebuild(*lun);
251 	break;
252     }
253     return error;
254 }
255 
256 static int
257 ata_raid_flush(struct ar_softc *rdp, struct bio *bp)
258 {
259     struct ata_request *request;
260     device_t dev;
261     int disk, error;
262 
263     error = 0;
264     bp->bio_driver_info = NULL;
265 
266     for (disk = 0; disk < rdp->total_disks; disk++) {
267 	if ((dev = rdp->disks[disk].dev) != NULL)
268 	    bp->bio_driver_info = (void *)((intptr_t)bp->bio_driver_info + 1);
269     }
270     for (disk = 0; disk < rdp->total_disks; disk++) {
271 	if ((dev = rdp->disks[disk].dev) == NULL)
272 	    continue;
273 	if (!(request = ata_raid_init_request(rdp, bp)))
274 	    return ENOMEM;
275 	request->dev = dev;
276 	request->u.ata.command = ATA_FLUSHCACHE;
277 	request->u.ata.lba = 0;
278 	request->u.ata.count = 0;
279 	request->u.ata.feature = 0;
280 	request->timeout = 1;
281 	request->retries = 0;
282 	request->flags |= ATA_R_ORDERED | ATA_R_DIRECT;
283 	ata_queue_request(request);
284     }
285     return 0;
286 }
287 
288 /*
289  * XXX TGEN there are a lot of offset -> block number conversions going on
290  * here, which is suboptimal.
291  */
292 static int
293 ata_raid_strategy(struct dev_strategy_args *ap)
294 {
295     struct ar_softc *rdp = ap->a_head.a_dev->si_drv1;
296     struct bio *bp = ap->a_bio;
297     struct buf *bbp = bp->bio_buf;
298     struct ata_request *request;
299     caddr_t data;
300     u_int64_t blkno, lba, blk = 0;
301     int count, chunk, drv, par = 0, change = 0;
302 
303     if (bbp->b_cmd == BUF_CMD_FLUSH) {
304 	int error;
305 
306 	error = ata_raid_flush(rdp, bp);
307 	if (error != 0) {
308 		bbp->b_flags |= B_ERROR;
309 		bbp->b_error = error;
310 		biodone(bp);
311 	}
312 	return(0);
313     }
314 
315     if (!(rdp->status & AR_S_READY) ||
316 	(bbp->b_cmd != BUF_CMD_READ && bbp->b_cmd != BUF_CMD_WRITE)) {
317 	bbp->b_flags |= B_ERROR;
318 	bbp->b_error = EIO;
319 	biodone(bp);
320 	return(0);
321     }
322 
323     bbp->b_resid = bbp->b_bcount;
324     for (count = howmany(bbp->b_bcount, DEV_BSIZE),
325 	 /* bio_offset is byte granularity, convert */
326 	 blkno = (u_int64_t)(bp->bio_offset >> DEV_BSHIFT),
327 	 data = bbp->b_data;
328 	 count > 0;
329 	 count -= chunk, blkno += chunk, data += (chunk * DEV_BSIZE)) {
330 
331 	switch (rdp->type) {
332 	case AR_T_RAID1:
333 	    drv = 0;
334 	    lba = blkno;
335 	    chunk = count;
336 	    break;
337 
338 	case AR_T_JBOD:
339 	case AR_T_SPAN:
340 	    drv = 0;
341 	    lba = blkno;
342 	    while (lba >= rdp->disks[drv].sectors)
343 		lba -= rdp->disks[drv++].sectors;
344 	    chunk = min(rdp->disks[drv].sectors - lba, count);
345 	    break;
346 
347 	case AR_T_RAID0:
348 	case AR_T_RAID01:
349 	    chunk = blkno % rdp->interleave;
350 	    drv = (blkno / rdp->interleave) % rdp->width;
351 	    lba = (((blkno/rdp->interleave)/rdp->width)*rdp->interleave)+chunk;
352 	    chunk = min(count, rdp->interleave - chunk);
353 	    break;
354 
355 	case AR_T_RAID5:
356 	    drv = (blkno / rdp->interleave) % (rdp->width - 1);
357 	    par = rdp->width - 1 -
358 		  (blkno / (rdp->interleave * (rdp->width - 1))) % rdp->width;
359 	    if (drv >= par)
360 		drv++;
361 	    lba = ((blkno/rdp->interleave)/(rdp->width-1))*(rdp->interleave) +
362 		  ((blkno%(rdp->interleave*(rdp->width-1)))%rdp->interleave);
363 	    chunk = min(count, rdp->interleave - (lba % rdp->interleave));
364 	    break;
365 
366 	default:
367 	    kprintf("ar%d: unknown array type in ata_raid_strategy\n", rdp->lun);
368 	    bbp->b_flags |= B_ERROR;
369 	    bbp->b_error = EIO;
370 	    biodone(bp);
371 	    return(0);
372 	}
373 
374 	/* offset on all but "first on HPTv2" */
375 	if (!(drv == 0 && rdp->format == AR_F_HPTV2_RAID))
376 	    lba += rdp->offset_sectors;
377 
378 	if (!(request = ata_raid_init_request(rdp, bp))) {
379 	    bbp->b_flags |= B_ERROR;
380 	    bbp->b_error = EIO;
381 	    biodone(bp);
382 	    return(0);
383 	}
384 	request->data = data;
385 	request->bytecount = chunk * DEV_BSIZE;
386 	request->u.ata.lba = lba;
387 	request->u.ata.count = request->bytecount / DEV_BSIZE;
388 
389 	switch (rdp->type) {
390 	case AR_T_JBOD:
391 	case AR_T_SPAN:
392 	case AR_T_RAID0:
393 	    if (((rdp->disks[drv].flags & (AR_DF_PRESENT|AR_DF_ONLINE)) ==
394 		 (AR_DF_PRESENT|AR_DF_ONLINE) && !rdp->disks[drv].dev)) {
395 		rdp->disks[drv].flags &= ~AR_DF_ONLINE;
396 		ata_raid_config_changed(rdp, 1);
397 		ata_free_request(request);
398 		bbp->b_flags |= B_ERROR;
399 		bbp->b_error = EIO;
400 		biodone(bp);
401 		return(0);
402 	    }
403 	    request->this = drv;
404 	    request->dev = rdp->disks[request->this].dev;
405 	    ata_raid_send_request(request);
406 	    break;
407 
408 	case AR_T_RAID1:
409 	case AR_T_RAID01:
410 	    if ((rdp->disks[drv].flags &
411 		 (AR_DF_PRESENT|AR_DF_ONLINE))==(AR_DF_PRESENT|AR_DF_ONLINE) &&
412 		!rdp->disks[drv].dev) {
413 		rdp->disks[drv].flags &= ~AR_DF_ONLINE;
414 		change = 1;
415 	    }
416 	    if ((rdp->disks[drv + rdp->width].flags &
417 		 (AR_DF_PRESENT|AR_DF_ONLINE))==(AR_DF_PRESENT|AR_DF_ONLINE) &&
418 		!rdp->disks[drv + rdp->width].dev) {
419 		rdp->disks[drv + rdp->width].flags &= ~AR_DF_ONLINE;
420 		change = 1;
421 	    }
422 	    if (change)
423 		ata_raid_config_changed(rdp, 1);
424 	    if (!(rdp->status & AR_S_READY)) {
425 		ata_free_request(request);
426 		bbp->b_flags |= B_ERROR;
427 		bbp->b_error = EIO;
428 		biodone(bp);
429 		return(0);
430 	    }
431 
432 	    if (rdp->status & AR_S_REBUILDING)
433 		blk = ((lba / rdp->interleave) * rdp->width) * rdp->interleave +
434 		      (rdp->interleave * (drv % rdp->width)) +
435 		      lba % rdp->interleave;;
436 
437 	    if (bbp->b_cmd == BUF_CMD_READ) {
438 		int src_online =
439 		    (rdp->disks[drv].flags & AR_DF_ONLINE);
440 		int mir_online =
441 		    (rdp->disks[drv+rdp->width].flags & AR_DF_ONLINE);
442 
443 		/* if mirror gone or close to last access on source */
444 		if (!mir_online ||
445 		    ((src_online) &&
446 		     ((u_int64_t)(bp->bio_offset >> DEV_BSHIFT)) >=
447 			(rdp->disks[drv].last_lba - AR_PROXIMITY) &&
448 		     ((u_int64_t)(bp->bio_offset >> DEV_BSHIFT)) <=
449 			(rdp->disks[drv].last_lba + AR_PROXIMITY))) {
450 		    rdp->toggle = 0;
451 		}
452 		/* if source gone or close to last access on mirror */
453 		else if (!src_online ||
454 			 ((mir_online) &&
455 			  ((u_int64_t)(bp->bio_offset >> DEV_BSHIFT)) >=
456 			  (rdp->disks[drv+rdp->width].last_lba-AR_PROXIMITY) &&
457 			  ((u_int64_t)(bp->bio_offset >> DEV_BSHIFT)) <=
458 			  (rdp->disks[drv+rdp->width].last_lba+AR_PROXIMITY))) {
459 		    drv += rdp->width;
460 		    rdp->toggle = 1;
461 		}
462 		/* not close to any previous access, toggle */
463 		else {
464 		    if (rdp->toggle)
465 			rdp->toggle = 0;
466 		    else {
467 			drv += rdp->width;
468 			rdp->toggle = 1;
469 		    }
470 		}
471 
472 		if ((rdp->status & AR_S_REBUILDING) &&
473 		    (blk <= rdp->rebuild_lba) &&
474 		    ((blk + chunk) > rdp->rebuild_lba)) {
475 		    struct ata_composite *composite;
476 		    struct ata_request *rebuild;
477 		    int this;
478 
479 		    /* figure out what part to rebuild */
480 		    if (drv < rdp->width)
481 			this = drv + rdp->width;
482 		    else
483 			this = drv - rdp->width;
484 
485 		    /* do we have a spare to rebuild on ? */
486 		    if (rdp->disks[this].flags & AR_DF_SPARE) {
487 			if ((composite = ata_alloc_composite())) {
488 			    if ((rebuild = ata_alloc_request())) {
489 				rdp->rebuild_lba = blk + chunk;
490 				bcopy(request, rebuild,
491 				      sizeof(struct ata_request));
492 				rebuild->this = this;
493 				rebuild->dev = rdp->disks[this].dev;
494 				rebuild->flags &= ~ATA_R_READ;
495 				rebuild->flags |= ATA_R_WRITE;
496 				spin_init(&composite->lock);
497 				composite->residual = request->bytecount;
498 				composite->rd_needed |= (1 << drv);
499 				composite->wr_depend |= (1 << drv);
500 				composite->wr_needed |= (1 << this);
501 				composite->request[drv] = request;
502 				composite->request[this] = rebuild;
503 				request->composite = composite;
504 				rebuild->composite = composite;
505 				ata_raid_send_request(rebuild);
506 			    }
507 			    else {
508 				ata_free_composite(composite);
509 				kprintf("DOH! ata_alloc_request failed!\n");
510 			    }
511 			}
512 			else {
513 			    kprintf("DOH! ata_alloc_composite failed!\n");
514 			}
515 		    }
516 		    else if (rdp->disks[this].flags & AR_DF_ONLINE) {
517 			/*
518 			 * if we got here we are a chunk of a RAID01 that
519 			 * does not need a rebuild, but we need to increment
520 			 * the rebuild_lba address to get the rebuild to
521 			 * move to the next chunk correctly
522 			 */
523 			rdp->rebuild_lba = blk + chunk;
524 		    }
525 		    else
526 			kprintf("DOH! we didn't find the rebuild part\n");
527 		}
528 	    }
529 	    if (bbp->b_cmd == BUF_CMD_WRITE) {
530 		if ((rdp->disks[drv+rdp->width].flags & AR_DF_ONLINE) ||
531 		    ((rdp->status & AR_S_REBUILDING) &&
532 		     (rdp->disks[drv+rdp->width].flags & AR_DF_SPARE) &&
533 		     ((blk < rdp->rebuild_lba) ||
534 		      ((blk <= rdp->rebuild_lba) &&
535 		       ((blk + chunk) > rdp->rebuild_lba))))) {
536 		    if ((rdp->disks[drv].flags & AR_DF_ONLINE) ||
537 			((rdp->status & AR_S_REBUILDING) &&
538 			 (rdp->disks[drv].flags & AR_DF_SPARE) &&
539 			 ((blk < rdp->rebuild_lba) ||
540 			  ((blk <= rdp->rebuild_lba) &&
541 			   ((blk + chunk) > rdp->rebuild_lba))))) {
542 			struct ata_request *mirror;
543 			struct ata_composite *composite;
544 			int this = drv + rdp->width;
545 
546 			if ((composite = ata_alloc_composite())) {
547 			    if ((mirror = ata_alloc_request())) {
548 				if ((blk <= rdp->rebuild_lba) &&
549 				    ((blk + chunk) > rdp->rebuild_lba))
550 				    rdp->rebuild_lba = blk + chunk;
551 				bcopy(request, mirror,
552 				      sizeof(struct ata_request));
553 				mirror->this = this;
554 				mirror->dev = rdp->disks[this].dev;
555 				spin_init(&composite->lock);
556 				composite->residual = request->bytecount;
557 				composite->wr_needed |= (1 << drv);
558 				composite->wr_needed |= (1 << this);
559 				composite->request[drv] = request;
560 				composite->request[this] = mirror;
561 				request->composite = composite;
562 				mirror->composite = composite;
563 				ata_raid_send_request(mirror);
564 				rdp->disks[this].last_lba =
565 				    (u_int64_t)(bp->bio_offset >> DEV_BSHIFT) +
566 				    chunk;
567 			    }
568 			    else {
569 				ata_free_composite(composite);
570 				kprintf("DOH! ata_alloc_request failed!\n");
571 			    }
572 			}
573 			else {
574 			    kprintf("DOH! ata_alloc_composite failed!\n");
575 			}
576 		    }
577 		    else
578 			drv += rdp->width;
579 		}
580 	    }
581 	    request->this = drv;
582 	    request->dev = rdp->disks[request->this].dev;
583 	    ata_raid_send_request(request);
584 	    rdp->disks[request->this].last_lba =
585 	       ((u_int64_t)(bp->bio_offset) >> DEV_BSHIFT) + chunk;
586 	    break;
587 
588 	case AR_T_RAID5:
589 	    if (((rdp->disks[drv].flags & (AR_DF_PRESENT|AR_DF_ONLINE)) ==
590 		 (AR_DF_PRESENT|AR_DF_ONLINE) && !rdp->disks[drv].dev)) {
591 		rdp->disks[drv].flags &= ~AR_DF_ONLINE;
592 		change = 1;
593 	    }
594 	    if (((rdp->disks[par].flags & (AR_DF_PRESENT|AR_DF_ONLINE)) ==
595 		 (AR_DF_PRESENT|AR_DF_ONLINE) && !rdp->disks[par].dev)) {
596 		rdp->disks[par].flags &= ~AR_DF_ONLINE;
597 		change = 1;
598 	    }
599 	    if (change)
600 		ata_raid_config_changed(rdp, 1);
601 	    if (!(rdp->status & AR_S_READY)) {
602 		ata_free_request(request);
603 		bbp->b_flags |= B_ERROR;
604 		bbp->b_error = EIO;
605 		biodone(bp);
606 		return(0);
607 	    }
608 	    if (rdp->status & AR_S_DEGRADED) {
609 		/* do the XOR game if possible */
610 	    }
611 	    else {
612 		request->this = drv;
613 		request->dev = rdp->disks[request->this].dev;
614 		if (bbp->b_cmd == BUF_CMD_READ) {
615 		    ata_raid_send_request(request);
616 		}
617 		if (bbp->b_cmd == BUF_CMD_WRITE) {
618 		    ata_raid_send_request(request);
619 		    /* XXX TGEN no, I don't speak Danish either */
620 		    /*
621 		     * sikre at l�s-modify-skriv til hver disk er atomarisk.
622 		     * par kopi af request
623 		     * l�se orgdata fra drv
624 		     * skriv nydata til drv
625 		     * l�se parorgdata fra par
626 		     * skriv orgdata xor parorgdata xor nydata til par
627 		     */
628 		}
629 	    }
630 	    break;
631 
632 	default:
633 	    kprintf("ar%d: unknown array type in ata_raid_strategy\n", rdp->lun);
634 	}
635     }
636 
637     return(0);
638 }
639 
640 static void
641 ata_raid_done(struct ata_request *request)
642 {
643     struct ar_softc *rdp = request->driver;
644     struct ata_composite *composite = NULL;
645     struct bio *bp = request->bio;
646     struct buf *bbp = bp->bio_buf;
647     int i, mirror, finished = 0;
648 
649     if (bbp->b_cmd == BUF_CMD_FLUSH) {
650 	if (bbp->b_error == 0)
651 		bbp->b_error = request->result;
652 	ata_free_request(request);
653 	bp->bio_driver_info = (void *)((intptr_t)bp->bio_driver_info - 1);
654 	if ((intptr_t)bp->bio_driver_info == 0) {
655 		if (bbp->b_error)
656 			bbp->b_flags |= B_ERROR;
657 		biodone(bp);
658 	}
659 	return;
660     }
661 
662     switch (rdp->type) {
663     case AR_T_JBOD:
664     case AR_T_SPAN:
665     case AR_T_RAID0:
666 	if (request->result) {
667 	    rdp->disks[request->this].flags &= ~AR_DF_ONLINE;
668 	    ata_raid_config_changed(rdp, 1);
669 	    bbp->b_error = request->result;
670 	    finished = 1;
671 	}
672 	else {
673 	    bbp->b_resid -= request->donecount;
674 	    if (!bbp->b_resid)
675 		finished = 1;
676 	}
677 	break;
678 
679     case AR_T_RAID1:
680     case AR_T_RAID01:
681 	if (request->this < rdp->width)
682 	    mirror = request->this + rdp->width;
683 	else
684 	    mirror = request->this - rdp->width;
685 	if (request->result) {
686 	    rdp->disks[request->this].flags &= ~AR_DF_ONLINE;
687 	    ata_raid_config_changed(rdp, 1);
688 	}
689 	if (rdp->status & AR_S_READY) {
690 	    u_int64_t blk = 0;
691 
692 	    if (rdp->status & AR_S_REBUILDING)
693 		blk = ((request->u.ata.lba / rdp->interleave) * rdp->width) *
694 		      rdp->interleave + (rdp->interleave *
695 		      (request->this % rdp->width)) +
696 		      request->u.ata.lba % rdp->interleave;
697 
698 	    if (bbp->b_cmd == BUF_CMD_READ) {
699 
700 		/* is this a rebuild composite */
701 		if ((composite = request->composite)) {
702 		    spin_lock_wr(&composite->lock);
703 
704 		    /* handle the read part of a rebuild composite */
705 		    if (request->flags & ATA_R_READ) {
706 
707 			/* if read failed array is now broken */
708 			if (request->result) {
709 			    rdp->disks[request->this].flags &= ~AR_DF_ONLINE;
710 			    ata_raid_config_changed(rdp, 1);
711 			    bbp->b_error = request->result;
712 			    rdp->rebuild_lba = blk;
713 			    finished = 1;
714 			}
715 
716 			/* good data, update how far we've gotten */
717 			else {
718 			    bbp->b_resid -= request->donecount;
719 			    composite->residual -= request->donecount;
720 			    if (!composite->residual) {
721 				if (composite->wr_done & (1 << mirror))
722 				    finished = 1;
723 			    }
724 			}
725 		    }
726 
727 		    /* handle the write part of a rebuild composite */
728 		    else if (request->flags & ATA_R_WRITE) {
729 			if (composite->rd_done & (1 << mirror)) {
730 			    if (request->result) {
731 				kprintf("DOH! rebuild failed\n"); /* XXX SOS */
732 				rdp->rebuild_lba = blk;
733 			    }
734 			    if (!composite->residual)
735 				finished = 1;
736 			}
737 		    }
738 		    spin_unlock_wr(&composite->lock);
739 		}
740 
741 		/* if read failed retry on the mirror */
742 		else if (request->result) {
743 		    request->dev = rdp->disks[mirror].dev;
744 		    request->flags &= ~ATA_R_TIMEOUT;
745 		    ata_raid_send_request(request);
746 		    return;
747 		}
748 
749 		/* we have good data */
750 		else {
751 		    bbp->b_resid -= request->donecount;
752 		    if (!bbp->b_resid)
753 			finished = 1;
754 		}
755 	    }
756 	    else if (bbp->b_cmd == BUF_CMD_WRITE) {
757 		/* do we have a mirror or rebuild to deal with ? */
758 		if ((composite = request->composite)) {
759 		    spin_lock_wr(&composite->lock);
760 		    if (composite->wr_done & (1 << mirror)) {
761 			if (request->result) {
762 			    if (composite->request[mirror]->result) {
763 				kprintf("DOH! all disks failed and got here\n");
764 				bbp->b_error = EIO;
765 			    }
766 			    if (rdp->status & AR_S_REBUILDING) {
767 				rdp->rebuild_lba = blk;
768 				kprintf("DOH! rebuild failed\n"); /* XXX SOS */
769 			    }
770 			    bbp->b_resid -=
771 				composite->request[mirror]->donecount;
772 			    composite->residual -=
773 				composite->request[mirror]->donecount;
774 			}
775 			else {
776 			    bbp->b_resid -= request->donecount;
777 			    composite->residual -= request->donecount;
778 			}
779 			if (!composite->residual)
780 			    finished = 1;
781 		    }
782 		    spin_unlock_wr(&composite->lock);
783 		}
784 		/* no mirror we are done */
785 		else {
786 		    bbp->b_resid -= request->donecount;
787 		    if (!bbp->b_resid)
788 			finished = 1;
789 		}
790 	    }
791 	}
792 	else {
793 	    /* XXX TGEN bbp->b_flags |= B_ERROR; */
794 	    bbp->b_error = request->result;
795 	    biodone(bp);
796 	}
797 	break;
798 
799     case AR_T_RAID5:
800 	if (request->result) {
801 	    rdp->disks[request->this].flags &= ~AR_DF_ONLINE;
802 	    ata_raid_config_changed(rdp, 1);
803 	    if (rdp->status & AR_S_READY) {
804 		if (bbp->b_cmd == BUF_CMD_READ) {
805 		    /* do the XOR game to recover data */
806 		}
807 		if (bbp->b_cmd == BUF_CMD_WRITE) {
808 		    /* if the parity failed we're OK sortof */
809 		    /* otherwise wee need to do the XOR long dance */
810 		}
811 		finished = 1;
812 	    }
813 	    else {
814 		/* XXX TGEN bbp->b_flags |= B_ERROR; */
815 		bbp->b_error = request->result;
816 		biodone(bp);
817 	    }
818 	}
819 	else {
820 	    /* did we have an XOR game going ?? */
821 	    bbp->b_resid -= request->donecount;
822 	    if (!bbp->b_resid)
823 		finished = 1;
824 	}
825 	break;
826 
827     default:
828 	kprintf("ar%d: unknown array type in ata_raid_done\n", rdp->lun);
829     }
830 
831     if (finished) {
832 	if ((rdp->status & AR_S_REBUILDING) &&
833 	    rdp->rebuild_lba >= rdp->total_sectors) {
834 	    int disk;
835 
836 	    for (disk = 0; disk < rdp->total_disks; disk++) {
837 		if ((rdp->disks[disk].flags &
838 		     (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_SPARE)) ==
839 		    (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_SPARE)) {
840 		    rdp->disks[disk].flags &= ~AR_DF_SPARE;
841 		    rdp->disks[disk].flags |= AR_DF_ONLINE;
842 		}
843 	    }
844 	    rdp->status &= ~AR_S_REBUILDING;
845 	    ata_raid_config_changed(rdp, 1);
846 	}
847 	if (!bbp->b_resid)
848 	    biodone(bp);
849     }
850 
851     if (composite) {
852 	if (finished) {
853 	    /* we are done with this composite, free all resources */
854 	    for (i = 0; i < 32; i++) {
855 		if (composite->rd_needed & (1 << i) ||
856 		    composite->wr_needed & (1 << i)) {
857 		    ata_free_request(composite->request[i]);
858 		}
859 	    }
860 	    spin_uninit(&composite->lock);
861 	    ata_free_composite(composite);
862 	}
863     }
864     else
865 	ata_free_request(request);
866 }
867 
868 static int
869 ata_raid_dump(struct dev_dump_args *ap)
870 {
871     struct ar_softc *rdp = ap->a_head.a_dev->si_drv1;
872     struct buf dbuf;
873     vm_paddr_t addr = 0;
874     long blkcnt;
875     int dumppages = MAXDUMPPGS;
876     int error = 0;
877     int i, disk;
878 
879     blkcnt = howmany(PAGE_SIZE, ap->a_secsize);
880 
881     while (ap->a_count > 0) {
882 	caddr_t va = NULL;
883 
884 	if ((ap->a_count / blkcnt) < dumppages)
885 	    dumppages = ap->a_count / blkcnt;
886 
887 	for (i = 0; i < dumppages; ++i) {
888 	    vm_paddr_t a = addr + (i * PAGE_SIZE);
889 	    if (is_physical_memory(a))
890 		va = pmap_kenter_temporary(trunc_page(a), i);
891 	    else
892 		va = pmap_kenter_temporary(trunc_page(0), i);
893 	}
894 
895 	bzero(&dbuf, sizeof(struct buf));
896 	BUF_LOCKINIT(&dbuf);
897 	BUF_LOCK(&dbuf, LK_EXCLUSIVE);
898 	initbufbio(&dbuf);
899 	/* bio_offset is byte granularity, convert block granularity a_blkno */
900 	dbuf.b_bio1.bio_offset = (off_t)(ap->a_blkno << DEV_BSHIFT);
901 	dbuf.b_bio1.bio_caller_info1.ptr = (void *)rdp;
902 	dbuf.b_bio1.bio_flags |= BIO_SYNC;
903 	dbuf.b_bio1.bio_done = biodone_sync;
904 	dbuf.b_bcount = dumppages * PAGE_SIZE;
905 	dbuf.b_data = va;
906 	dbuf.b_cmd = BUF_CMD_WRITE;
907 	dev_dstrategy(rdp->cdev, &dbuf.b_bio1);
908 	/* wait for completion, unlock the buffer, check status */
909 	if (biowait(&dbuf.b_bio1, "dumpw")) {
910 	    BUF_UNLOCK(&dbuf);
911 	    return(dbuf.b_error ? dbuf.b_error : EIO);
912 	}
913 	BUF_UNLOCK(&dbuf);
914 
915 	if (dumpstatus(addr, (off_t)ap->a_count * DEV_BSIZE) < 0)
916 	    return(EINTR);
917 
918 	ap->a_blkno += blkcnt * dumppages;
919 	ap->a_count -= blkcnt * dumppages;
920 	addr += PAGE_SIZE * dumppages;
921     }
922 
923     /* flush subdisk buffers to media */
924     for (disk = 0; disk < rdp->total_disks; disk++)
925 	if (rdp->disks[disk].dev)
926 	    error |= ata_controlcmd(rdp->disks[disk].dev, ATA_FLUSHCACHE, 0, 0,
927 				    0);
928     return (error ? EIO : 0);
929 }
930 
931 static void
932 ata_raid_config_changed(struct ar_softc *rdp, int writeback)
933 {
934     int disk, count, status;
935 
936     spin_lock_wr(&rdp->lock);
937     /* set default all working mode */
938     status = rdp->status;
939     rdp->status &= ~AR_S_DEGRADED;
940     rdp->status |= AR_S_READY;
941 
942     /* make sure all lost drives are accounted for */
943     for (disk = 0; disk < rdp->total_disks; disk++) {
944 	if (!(rdp->disks[disk].flags & AR_DF_PRESENT))
945 	    rdp->disks[disk].flags &= ~AR_DF_ONLINE;
946     }
947 
948     /* depending on RAID type figure out our health status */
949     switch (rdp->type) {
950     case AR_T_JBOD:
951     case AR_T_SPAN:
952     case AR_T_RAID0:
953 	for (disk = 0; disk < rdp->total_disks; disk++)
954 	    if (!(rdp->disks[disk].flags & AR_DF_ONLINE))
955 		rdp->status &= ~AR_S_READY;
956 	break;
957 
958     case AR_T_RAID1:
959     case AR_T_RAID01:
960 	for (disk = 0; disk < rdp->width; disk++) {
961 	    if (!(rdp->disks[disk].flags & AR_DF_ONLINE) &&
962 		!(rdp->disks[disk + rdp->width].flags & AR_DF_ONLINE)) {
963 		rdp->status &= ~AR_S_READY;
964 	    }
965 	    else if (((rdp->disks[disk].flags & AR_DF_ONLINE) &&
966 		      !(rdp->disks[disk + rdp->width].flags & AR_DF_ONLINE)) ||
967 		     (!(rdp->disks[disk].flags & AR_DF_ONLINE) &&
968 		      (rdp->disks [disk + rdp->width].flags & AR_DF_ONLINE))) {
969 		rdp->status |= AR_S_DEGRADED;
970 	    }
971 	}
972 	break;
973 
974     case AR_T_RAID5:
975 	for (count = 0, disk = 0; disk < rdp->total_disks; disk++) {
976 	    if (!(rdp->disks[disk].flags & AR_DF_ONLINE))
977 		count++;
978 	}
979 	if (count) {
980 	    if (count > 1)
981 		rdp->status &= ~AR_S_READY;
982 	    else
983 		rdp->status |= AR_S_DEGRADED;
984 	}
985 	break;
986     default:
987 	rdp->status &= ~AR_S_READY;
988     }
989 
990     /*
991      * Note that when the array breaks so comes up broken we
992      * force a write of the array config to the remaining
993      * drives so that the generation will be incremented past
994      * those of the missing or failed drives (in all cases).
995      */
996     if (rdp->status != status) {
997 	if (!(rdp->status & AR_S_READY)) {
998 	    kprintf("ar%d: FAILURE - %s array broken\n",
999 		   rdp->lun, ata_raid_type(rdp));
1000 	    writeback = 1;
1001 	}
1002 	else if (rdp->status & AR_S_DEGRADED) {
1003 	    if (rdp->type & (AR_T_RAID1 | AR_T_RAID01))
1004 		kprintf("ar%d: WARNING - mirror", rdp->lun);
1005 	    else
1006 		kprintf("ar%d: WARNING - parity", rdp->lun);
1007 	    kprintf(" protection lost. %s array in DEGRADED mode\n",
1008 		   ata_raid_type(rdp));
1009 	    writeback = 1;
1010 	}
1011     }
1012     spin_unlock_wr(&rdp->lock);
1013     if (writeback)
1014 	ata_raid_write_metadata(rdp);
1015 
1016 }
1017 
1018 static int
1019 ata_raid_status(struct ata_ioc_raid_config *config)
1020 {
1021     struct ar_softc *rdp;
1022     int i;
1023 
1024     if (!(rdp = ata_raid_arrays[config->lun]))
1025 	return ENXIO;
1026 
1027     config->type = rdp->type;
1028     config->total_disks = rdp->total_disks;
1029     for (i = 0; i < rdp->total_disks; i++ ) {
1030 	if ((rdp->disks[i].flags & AR_DF_PRESENT) && rdp->disks[i].dev)
1031 	    config->disks[i] = device_get_unit(rdp->disks[i].dev);
1032 	else
1033 	    config->disks[i] = -1;
1034     }
1035     config->interleave = rdp->interleave;
1036     config->status = rdp->status;
1037     config->progress = 100 * rdp->rebuild_lba / rdp->total_sectors;
1038     return 0;
1039 }
1040 
1041 static int
1042 ata_raid_create(struct ata_ioc_raid_config *config)
1043 {
1044     struct ar_softc *rdp;
1045     device_t subdisk;
1046     int array, disk;
1047     int ctlr = 0, disk_size = 0, total_disks = 0;
1048 
1049     for (array = 0; array < MAX_ARRAYS; array++) {
1050 	if (!ata_raid_arrays[array])
1051 	    break;
1052     }
1053     if (array >= MAX_ARRAYS)
1054 	return ENOSPC;
1055 
1056     rdp = (struct ar_softc*)kmalloc(sizeof(struct ar_softc), M_AR,
1057 	M_WAITOK | M_ZERO);
1058 
1059     for (disk = 0; disk < config->total_disks; disk++) {
1060 	if ((subdisk = devclass_get_device(ata_raid_sub_devclass,
1061 					   config->disks[disk]))) {
1062 	    struct ata_raid_subdisk *ars = device_get_softc(subdisk);
1063 
1064 	    /* is device already assigned to another array ? */
1065 	    if (ars->raid[rdp->volume]) {
1066 		config->disks[disk] = -1;
1067 		kfree(rdp, M_AR);
1068 		return EBUSY;
1069 	    }
1070 	    rdp->disks[disk].dev = device_get_parent(subdisk);
1071 
1072 	    switch (pci_get_vendor(GRANDPARENT(rdp->disks[disk].dev))) {
1073 	    case ATA_HIGHPOINT_ID:
1074 		/*
1075 		 * we need some way to decide if it should be v2 or v3
1076 		 * for now just use v2 since the v3 BIOS knows how to
1077 		 * handle that as well.
1078 		 */
1079 		ctlr = AR_F_HPTV2_RAID;
1080 		rdp->disks[disk].sectors = HPTV3_LBA(rdp->disks[disk].dev);
1081 		break;
1082 
1083 	    case ATA_INTEL_ID:
1084 		ctlr = AR_F_INTEL_RAID;
1085 		rdp->disks[disk].sectors = INTEL_LBA(rdp->disks[disk].dev);
1086 		break;
1087 
1088 	    case ATA_ITE_ID:
1089 		ctlr = AR_F_ITE_RAID;
1090 		rdp->disks[disk].sectors = ITE_LBA(rdp->disks[disk].dev);
1091 		break;
1092 
1093 	    case ATA_JMICRON_ID:
1094 		ctlr = AR_F_JMICRON_RAID;
1095 		rdp->disks[disk].sectors = JMICRON_LBA(rdp->disks[disk].dev);
1096 		break;
1097 
1098 	    case 0:     /* XXX SOS cover up for bug in our PCI code */
1099 	    case ATA_PROMISE_ID:
1100 		ctlr = AR_F_PROMISE_RAID;
1101 		rdp->disks[disk].sectors = PROMISE_LBA(rdp->disks[disk].dev);
1102 		break;
1103 
1104 	    case ATA_SIS_ID:
1105 		ctlr = AR_F_SIS_RAID;
1106 		rdp->disks[disk].sectors = SIS_LBA(rdp->disks[disk].dev);
1107 		break;
1108 
1109 	    case ATA_ATI_ID:
1110 	    case ATA_VIA_ID:
1111 		ctlr = AR_F_VIA_RAID;
1112 		rdp->disks[disk].sectors = VIA_LBA(rdp->disks[disk].dev);
1113 		break;
1114 
1115 	    default:
1116 		/* XXX SOS
1117 		 * right, so here we are, we have an ATA chip and we want
1118 		 * to create a RAID and store the metadata.
1119 		 * we need to find a way to tell what kind of metadata this
1120 		 * hardware's BIOS might be using (good ideas are welcomed)
1121 		 * for now we just use our own native FreeBSD format.
1122 		 * the only way to get support for the BIOS format is to
1123 		 * setup the RAID from there, in that case we pickup the
1124 		 * metadata format from the disks (if we support it).
1125 		 */
1126 		kprintf("WARNING!! - not able to determine metadata format\n"
1127 		       "WARNING!! - Using FreeBSD PseudoRAID metadata\n"
1128 		       "If that is not what you want, use the BIOS to "
1129 		       "create the array\n");
1130 		ctlr = AR_F_FREEBSD_RAID;
1131 		rdp->disks[disk].sectors = PROMISE_LBA(rdp->disks[disk].dev);
1132 		break;
1133 	    }
1134 
1135 	    /* we need all disks to be of the same format */
1136 	    if ((rdp->format & AR_F_FORMAT_MASK) &&
1137 		(rdp->format & AR_F_FORMAT_MASK) != (ctlr & AR_F_FORMAT_MASK)) {
1138 		kfree(rdp, M_AR);
1139 		return EXDEV;
1140 	    }
1141 	    else
1142 		rdp->format = ctlr;
1143 
1144 	    /* use the smallest disk of the lots size */
1145 	    /* gigabyte boundry ??? XXX SOS */
1146 	    if (disk_size)
1147 		disk_size = min(rdp->disks[disk].sectors, disk_size);
1148 	    else
1149 		disk_size = rdp->disks[disk].sectors;
1150 	    rdp->disks[disk].flags =
1151 		(AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_ONLINE);
1152 
1153 	    total_disks++;
1154 	}
1155 	else {
1156 	    config->disks[disk] = -1;
1157 	    kfree(rdp, M_AR);
1158 	    return ENXIO;
1159 	}
1160     }
1161 
1162     if (total_disks != config->total_disks) {
1163 	kfree(rdp, M_AR);
1164 	return ENODEV;
1165     }
1166 
1167     switch (config->type) {
1168     case AR_T_JBOD:
1169     case AR_T_SPAN:
1170     case AR_T_RAID0:
1171 	break;
1172 
1173     case AR_T_RAID1:
1174 	if (total_disks != 2) {
1175 	    kfree(rdp, M_AR);
1176 	    return EPERM;
1177 	}
1178 	break;
1179 
1180     case AR_T_RAID01:
1181 	if (total_disks % 2 != 0) {
1182 	    kfree(rdp, M_AR);
1183 	    return EPERM;
1184 	}
1185 	break;
1186 
1187     case AR_T_RAID5:
1188 	if (total_disks < 3) {
1189 	    kfree(rdp, M_AR);
1190 	    return EPERM;
1191 	}
1192 	break;
1193 
1194     default:
1195 	kfree(rdp, M_AR);
1196 	return EOPNOTSUPP;
1197     }
1198     rdp->type = config->type;
1199     rdp->lun = array;
1200     if (rdp->type == AR_T_RAID0 || rdp->type == AR_T_RAID01 ||
1201 	rdp->type == AR_T_RAID5) {
1202 	int bit = 0;
1203 
1204 	while (config->interleave >>= 1)
1205 	    bit++;
1206 	rdp->interleave = 1 << bit;
1207     }
1208     rdp->offset_sectors = 0;
1209 
1210     /* values that depend on metadata format */
1211     switch (rdp->format) {
1212     case AR_F_ADAPTEC_RAID:
1213 	rdp->interleave = min(max(32, rdp->interleave), 128); /*+*/
1214 	break;
1215 
1216     case AR_F_HPTV2_RAID:
1217 	rdp->interleave = min(max(8, rdp->interleave), 128); /*+*/
1218 	rdp->offset_sectors = HPTV2_LBA(x) + 1;
1219 	break;
1220 
1221     case AR_F_HPTV3_RAID:
1222 	rdp->interleave = min(max(32, rdp->interleave), 4096); /*+*/
1223 	break;
1224 
1225     case AR_F_INTEL_RAID:
1226 	rdp->interleave = min(max(8, rdp->interleave), 256); /*+*/
1227 	break;
1228 
1229     case AR_F_ITE_RAID:
1230 	rdp->interleave = min(max(2, rdp->interleave), 128); /*+*/
1231 	break;
1232 
1233     case AR_F_JMICRON_RAID:
1234 	rdp->interleave = min(max(8, rdp->interleave), 256); /*+*/
1235 	break;
1236 
1237     case AR_F_LSIV2_RAID:
1238 	rdp->interleave = min(max(2, rdp->interleave), 4096);
1239 	break;
1240 
1241     case AR_F_LSIV3_RAID:
1242 	rdp->interleave = min(max(2, rdp->interleave), 256);
1243 	break;
1244 
1245     case AR_F_PROMISE_RAID:
1246 	rdp->interleave = min(max(2, rdp->interleave), 2048); /*+*/
1247 	break;
1248 
1249     case AR_F_SII_RAID:
1250 	rdp->interleave = min(max(8, rdp->interleave), 256); /*+*/
1251 	break;
1252 
1253     case AR_F_SIS_RAID:
1254 	rdp->interleave = min(max(32, rdp->interleave), 512); /*+*/
1255 	break;
1256 
1257     case AR_F_VIA_RAID:
1258 	rdp->interleave = min(max(8, rdp->interleave), 128); /*+*/
1259 	break;
1260     }
1261 
1262     rdp->total_disks = total_disks;
1263     rdp->width = total_disks / (rdp->type & (AR_RAID1 | AR_T_RAID01) ? 2 : 1);
1264     rdp->total_sectors = disk_size * (rdp->width - (rdp->type == AR_RAID5));
1265     rdp->heads = 255;
1266     rdp->sectors = 63;
1267     rdp->cylinders = rdp->total_sectors / (255 * 63);
1268     rdp->rebuild_lba = 0;
1269     rdp->status |= AR_S_READY;
1270 
1271     /* we are committed to this array, grap the subdisks */
1272     for (disk = 0; disk < config->total_disks; disk++) {
1273 	if ((subdisk = devclass_get_device(ata_raid_sub_devclass,
1274 					   config->disks[disk]))) {
1275 	    struct ata_raid_subdisk *ars = device_get_softc(subdisk);
1276 
1277 	    ars->raid[rdp->volume] = rdp;
1278 	    ars->disk_number[rdp->volume] = disk;
1279 	}
1280     }
1281     ata_raid_attach(rdp, 1);
1282     ata_raid_arrays[array] = rdp;
1283     config->lun = array;
1284     return 0;
1285 }
1286 
1287 static int
1288 ata_raid_delete(int array)
1289 {
1290     struct ar_softc *rdp;
1291     device_t subdisk;
1292     int disk;
1293 
1294     if (!(rdp = ata_raid_arrays[array]))
1295 	return ENXIO;
1296 
1297     rdp->status &= ~AR_S_READY;
1298     disk_destroy(&rdp->disk);
1299 
1300     for (disk = 0; disk < rdp->total_disks; disk++) {
1301 	if ((rdp->disks[disk].flags & AR_DF_PRESENT) && rdp->disks[disk].dev) {
1302 	    if ((subdisk = devclass_get_device(ata_raid_sub_devclass,
1303 		     device_get_unit(rdp->disks[disk].dev)))) {
1304 		struct ata_raid_subdisk *ars = device_get_softc(subdisk);
1305 
1306 		if (ars->raid[rdp->volume] != rdp)           /* XXX SOS */
1307 		    device_printf(subdisk, "DOH! this disk doesn't belong\n");
1308 		if (ars->disk_number[rdp->volume] != disk)   /* XXX SOS */
1309 		    device_printf(subdisk, "DOH! this disk number is wrong\n");
1310 		ars->raid[rdp->volume] = NULL;
1311 		ars->disk_number[rdp->volume] = -1;
1312 	    }
1313 	    rdp->disks[disk].flags = 0;
1314 	}
1315     }
1316     ata_raid_wipe_metadata(rdp);
1317     ata_raid_arrays[array] = NULL;
1318     kfree(rdp, M_AR);
1319     return 0;
1320 }
1321 
1322 static int
1323 ata_raid_addspare(struct ata_ioc_raid_config *config)
1324 {
1325     struct ar_softc *rdp;
1326     device_t subdisk;
1327     int disk;
1328 
1329     if (!(rdp = ata_raid_arrays[config->lun]))
1330 	return ENXIO;
1331     if (!(rdp->status & AR_S_DEGRADED) || !(rdp->status & AR_S_READY))
1332 	return ENXIO;
1333     if (rdp->status & AR_S_REBUILDING)
1334 	return EBUSY;
1335     switch (rdp->type) {
1336     case AR_T_RAID1:
1337     case AR_T_RAID01:
1338     case AR_T_RAID5:
1339 	for (disk = 0; disk < rdp->total_disks; disk++ ) {
1340 
1341 	    if (((rdp->disks[disk].flags & (AR_DF_PRESENT | AR_DF_ONLINE)) ==
1342 		 (AR_DF_PRESENT | AR_DF_ONLINE)) && rdp->disks[disk].dev)
1343 		continue;
1344 
1345 	    if ((subdisk = devclass_get_device(ata_raid_sub_devclass,
1346 					       config->disks[0] ))) {
1347 		struct ata_raid_subdisk *ars = device_get_softc(subdisk);
1348 
1349 		if (ars->raid[rdp->volume])
1350 		    return EBUSY;
1351 
1352 		/* XXX SOS validate size etc etc */
1353 		ars->raid[rdp->volume] = rdp;
1354 		ars->disk_number[rdp->volume] = disk;
1355 		rdp->disks[disk].dev = device_get_parent(subdisk);
1356 		rdp->disks[disk].flags =
1357 		    (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_SPARE);
1358 
1359 		device_printf(rdp->disks[disk].dev,
1360 			      "inserted into ar%d disk%d as spare\n",
1361 			      rdp->lun, disk);
1362 		ata_raid_config_changed(rdp, 1);
1363 		return 0;
1364 	    }
1365 	}
1366 	return ENXIO;
1367 
1368     default:
1369 	return EPERM;
1370     }
1371 }
1372 
1373 static int
1374 ata_raid_rebuild(int array)
1375 {
1376     struct ar_softc *rdp;
1377     int disk, count;
1378 
1379     if (!(rdp = ata_raid_arrays[array]))
1380 	return ENXIO;
1381     /* XXX SOS we should lock the rdp softc here */
1382     if (!(rdp->status & AR_S_DEGRADED) || !(rdp->status & AR_S_READY))
1383 	return ENXIO;
1384     if (rdp->status & AR_S_REBUILDING)
1385 	return EBUSY;
1386 
1387     switch (rdp->type) {
1388     case AR_T_RAID1:
1389     case AR_T_RAID01:
1390     case AR_T_RAID5:
1391 	for (count = 0, disk = 0; disk < rdp->total_disks; disk++ ) {
1392 	    if (((rdp->disks[disk].flags &
1393 		  (AR_DF_PRESENT|AR_DF_ASSIGNED|AR_DF_ONLINE|AR_DF_SPARE)) ==
1394 		 (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_SPARE)) &&
1395 		rdp->disks[disk].dev) {
1396 		count++;
1397 	    }
1398 	}
1399 
1400 	if (count) {
1401 	    rdp->rebuild_lba = 0;
1402 	    rdp->status |= AR_S_REBUILDING;
1403 	    return 0;
1404 	}
1405 	return EIO;
1406 
1407     default:
1408 	return EPERM;
1409     }
1410 }
1411 
1412 static int
1413 ata_raid_read_metadata(device_t subdisk)
1414 {
1415     devclass_t pci_devclass = devclass_find("pci");
1416     devclass_t devclass=device_get_devclass(GRANDPARENT(GRANDPARENT(subdisk)));
1417 
1418     /* prioritize vendor native metadata layout if possible */
1419     if (devclass == pci_devclass) {
1420 	switch (pci_get_vendor(GRANDPARENT(device_get_parent(subdisk)))) {
1421 	case ATA_HIGHPOINT_ID:
1422 	    if (ata_raid_hptv3_read_meta(subdisk, ata_raid_arrays))
1423 		return 0;
1424 	    if (ata_raid_hptv2_read_meta(subdisk, ata_raid_arrays))
1425 		return 0;
1426 	    break;
1427 
1428 	case ATA_INTEL_ID:
1429 	    if (ata_raid_intel_read_meta(subdisk, ata_raid_arrays))
1430 		return 0;
1431 	    break;
1432 
1433 	case ATA_ITE_ID:
1434 	    if (ata_raid_ite_read_meta(subdisk, ata_raid_arrays))
1435 		return 0;
1436 	    break;
1437 
1438 	case ATA_JMICRON_ID:
1439 	    if (ata_raid_jmicron_read_meta(subdisk, ata_raid_arrays))
1440 		return 0;
1441 	    break;
1442 
1443 	case ATA_NVIDIA_ID:
1444 	    if (ata_raid_nvidia_read_meta(subdisk, ata_raid_arrays))
1445 		return 0;
1446 	    break;
1447 
1448 	case 0:         /* XXX SOS cover up for bug in our PCI code */
1449 	case ATA_PROMISE_ID:
1450 	    if (ata_raid_promise_read_meta(subdisk, ata_raid_arrays, 0))
1451 		return 0;
1452 	    break;
1453 
1454 	case ATA_ATI_ID:
1455 	case ATA_SILICON_IMAGE_ID:
1456 	    if (ata_raid_sii_read_meta(subdisk, ata_raid_arrays))
1457 		return 0;
1458 	    break;
1459 
1460 	case ATA_SIS_ID:
1461 	    if (ata_raid_sis_read_meta(subdisk, ata_raid_arrays))
1462 		return 0;
1463 	    break;
1464 
1465 	case ATA_VIA_ID:
1466 	    if (ata_raid_via_read_meta(subdisk, ata_raid_arrays))
1467 		return 0;
1468 	    break;
1469 	}
1470     }
1471 
1472     /* handle controllers that have multiple layout possibilities */
1473     /* NOTE: the order of these are not insignificant */
1474 
1475     /* Adaptec HostRAID */
1476     if (ata_raid_adaptec_read_meta(subdisk, ata_raid_arrays))
1477 	return 0;
1478 
1479     /* LSILogic v3 and v2 */
1480     if (ata_raid_lsiv3_read_meta(subdisk, ata_raid_arrays))
1481 	return 0;
1482     if (ata_raid_lsiv2_read_meta(subdisk, ata_raid_arrays))
1483 	return 0;
1484 
1485     /* if none of the above matched, try FreeBSD native format */
1486     return ata_raid_promise_read_meta(subdisk, ata_raid_arrays, 1);
1487 }
1488 
1489 static int
1490 ata_raid_write_metadata(struct ar_softc *rdp)
1491 {
1492     switch (rdp->format) {
1493     case AR_F_FREEBSD_RAID:
1494     case AR_F_PROMISE_RAID:
1495 	return ata_raid_promise_write_meta(rdp);
1496 
1497     case AR_F_HPTV3_RAID:
1498     case AR_F_HPTV2_RAID:
1499 	/*
1500 	 * always write HPT v2 metadata, the v3 BIOS knows it as well.
1501 	 * this is handy since we cannot know what version BIOS is on there
1502 	 */
1503 	return ata_raid_hptv2_write_meta(rdp);
1504 
1505     case AR_F_INTEL_RAID:
1506 	return ata_raid_intel_write_meta(rdp);
1507 
1508     case AR_F_JMICRON_RAID:
1509 	return ata_raid_jmicron_write_meta(rdp);
1510 
1511     case AR_F_SIS_RAID:
1512 	return ata_raid_sis_write_meta(rdp);
1513 
1514     case AR_F_VIA_RAID:
1515 	return ata_raid_via_write_meta(rdp);
1516 #if 0
1517     case AR_F_HPTV3_RAID:
1518 	return ata_raid_hptv3_write_meta(rdp);
1519 
1520     case AR_F_ADAPTEC_RAID:
1521 	return ata_raid_adaptec_write_meta(rdp);
1522 
1523     case AR_F_ITE_RAID:
1524 	return ata_raid_ite_write_meta(rdp);
1525 
1526     case AR_F_LSIV2_RAID:
1527 	return ata_raid_lsiv2_write_meta(rdp);
1528 
1529     case AR_F_LSIV3_RAID:
1530 	return ata_raid_lsiv3_write_meta(rdp);
1531 
1532     case AR_F_NVIDIA_RAID:
1533 	return ata_raid_nvidia_write_meta(rdp);
1534 
1535     case AR_F_SII_RAID:
1536 	return ata_raid_sii_write_meta(rdp);
1537 
1538 #endif
1539     default:
1540 	kprintf("ar%d: writing of %s metadata is NOT supported yet\n",
1541 	       rdp->lun, ata_raid_format(rdp));
1542     }
1543     return -1;
1544 }
1545 
1546 static int
1547 ata_raid_wipe_metadata(struct ar_softc *rdp)
1548 {
1549     int disk, error = 0;
1550     u_int64_t lba;
1551     u_int32_t size;
1552     u_int8_t *meta;
1553 
1554     for (disk = 0; disk < rdp->total_disks; disk++) {
1555 	if (rdp->disks[disk].dev) {
1556 	    switch (rdp->format) {
1557 	    case AR_F_ADAPTEC_RAID:
1558 		lba = ADP_LBA(rdp->disks[disk].dev);
1559 		size = sizeof(struct adaptec_raid_conf);
1560 		break;
1561 
1562 	    case AR_F_HPTV2_RAID:
1563 		lba = HPTV2_LBA(rdp->disks[disk].dev);
1564 		size = sizeof(struct hptv2_raid_conf);
1565 		break;
1566 
1567 	    case AR_F_HPTV3_RAID:
1568 		lba = HPTV3_LBA(rdp->disks[disk].dev);
1569 		size = sizeof(struct hptv3_raid_conf);
1570 		break;
1571 
1572 	    case AR_F_INTEL_RAID:
1573 		lba = INTEL_LBA(rdp->disks[disk].dev);
1574 		size = 3 * 512;         /* XXX SOS */
1575 		break;
1576 
1577 	    case AR_F_ITE_RAID:
1578 		lba = ITE_LBA(rdp->disks[disk].dev);
1579 		size = sizeof(struct ite_raid_conf);
1580 		break;
1581 
1582 	    case AR_F_JMICRON_RAID:
1583 		lba = JMICRON_LBA(rdp->disks[disk].dev);
1584 		size = sizeof(struct jmicron_raid_conf);
1585 		break;
1586 
1587 	    case AR_F_LSIV2_RAID:
1588 		lba = LSIV2_LBA(rdp->disks[disk].dev);
1589 		size = sizeof(struct lsiv2_raid_conf);
1590 		break;
1591 
1592 	    case AR_F_LSIV3_RAID:
1593 		lba = LSIV3_LBA(rdp->disks[disk].dev);
1594 		size = sizeof(struct lsiv3_raid_conf);
1595 		break;
1596 
1597 	    case AR_F_NVIDIA_RAID:
1598 		lba = NVIDIA_LBA(rdp->disks[disk].dev);
1599 		size = sizeof(struct nvidia_raid_conf);
1600 		break;
1601 
1602 	    case AR_F_FREEBSD_RAID:
1603 	    case AR_F_PROMISE_RAID:
1604 		lba = PROMISE_LBA(rdp->disks[disk].dev);
1605 		size = sizeof(struct promise_raid_conf);
1606 		break;
1607 
1608 	    case AR_F_SII_RAID:
1609 		lba = SII_LBA(rdp->disks[disk].dev);
1610 		size = sizeof(struct sii_raid_conf);
1611 		break;
1612 
1613 	    case AR_F_SIS_RAID:
1614 		lba = SIS_LBA(rdp->disks[disk].dev);
1615 		size = sizeof(struct sis_raid_conf);
1616 		break;
1617 
1618 	    case AR_F_VIA_RAID:
1619 		lba = VIA_LBA(rdp->disks[disk].dev);
1620 		size = sizeof(struct via_raid_conf);
1621 		break;
1622 
1623 	    default:
1624 		kprintf("ar%d: wiping of %s metadata is NOT supported yet\n",
1625 		       rdp->lun, ata_raid_format(rdp));
1626 		return ENXIO;
1627 	    }
1628 	    meta = kmalloc(size, M_AR, M_WAITOK | M_ZERO);
1629 	    if (ata_raid_rw(rdp->disks[disk].dev, lba, meta, size,
1630 			    ATA_R_WRITE | ATA_R_DIRECT)) {
1631 		device_printf(rdp->disks[disk].dev, "wipe metadata failed\n");
1632 		error = EIO;
1633 	    }
1634 	    kfree(meta, M_AR);
1635 	}
1636     }
1637     return error;
1638 }
1639 
1640 /* Adaptec HostRAID Metadata */
1641 static int
1642 ata_raid_adaptec_read_meta(device_t dev, struct ar_softc **raidp)
1643 {
1644     struct ata_raid_subdisk *ars = device_get_softc(dev);
1645     device_t parent = device_get_parent(dev);
1646     struct adaptec_raid_conf *meta;
1647     struct ar_softc *raid;
1648     int array, disk, retval = 0;
1649 
1650     meta = (struct adaptec_raid_conf *)
1651 	    kmalloc(sizeof(struct adaptec_raid_conf), M_AR, M_WAITOK | M_ZERO);
1652 
1653     if (ata_raid_rw(parent, ADP_LBA(parent),
1654 		    meta, sizeof(struct adaptec_raid_conf), ATA_R_READ)) {
1655 	if (testing || bootverbose)
1656 	    device_printf(parent, "Adaptec read metadata failed\n");
1657 	goto adaptec_out;
1658     }
1659 
1660     /* check if this is a Adaptec RAID struct */
1661     if (meta->magic_0 != ADP_MAGIC_0 || meta->magic_3 != ADP_MAGIC_3) {
1662 	if (testing || bootverbose)
1663 	    device_printf(parent, "Adaptec check1 failed\n");
1664 	goto adaptec_out;
1665     }
1666 
1667     if (testing || bootverbose)
1668 	ata_raid_adaptec_print_meta(meta);
1669 
1670     /* now convert Adaptec metadata into our generic form */
1671     for (array = 0; array < MAX_ARRAYS; array++) {
1672 	if (!raidp[array]) {
1673 	    raidp[array] =
1674 		(struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
1675 					  M_WAITOK | M_ZERO);
1676 	}
1677 	raid = raidp[array];
1678 	if (raid->format && (raid->format != AR_F_ADAPTEC_RAID))
1679 	    continue;
1680 
1681 	if (raid->magic_0 && raid->magic_0 != meta->configs[0].magic_0)
1682 	    continue;
1683 
1684 	if (!meta->generation || be32toh(meta->generation) > raid->generation) {
1685 	    switch (meta->configs[0].type) {
1686 	    case ADP_T_RAID0:
1687 		raid->magic_0 = meta->configs[0].magic_0;
1688 		raid->type = AR_T_RAID0;
1689 		raid->interleave = 1 << (meta->configs[0].stripe_shift >> 1);
1690 		raid->width = be16toh(meta->configs[0].total_disks);
1691 		break;
1692 
1693 	    case ADP_T_RAID1:
1694 		raid->magic_0 = meta->configs[0].magic_0;
1695 		raid->type = AR_T_RAID1;
1696 		raid->width = be16toh(meta->configs[0].total_disks) / 2;
1697 		break;
1698 
1699 	    default:
1700 		device_printf(parent, "Adaptec unknown RAID type 0x%02x\n",
1701 			      meta->configs[0].type);
1702 		kfree(raidp[array], M_AR);
1703 		raidp[array] = NULL;
1704 		goto adaptec_out;
1705 	    }
1706 
1707 	    raid->format = AR_F_ADAPTEC_RAID;
1708 	    raid->generation = be32toh(meta->generation);
1709 	    raid->total_disks = be16toh(meta->configs[0].total_disks);
1710 	    raid->total_sectors = be32toh(meta->configs[0].sectors);
1711 	    raid->heads = 255;
1712 	    raid->sectors = 63;
1713 	    raid->cylinders = raid->total_sectors / (63 * 255);
1714 	    raid->offset_sectors = 0;
1715 	    raid->rebuild_lba = 0;
1716 	    raid->lun = array;
1717 	    strncpy(raid->name, meta->configs[0].name,
1718 		    min(sizeof(raid->name), sizeof(meta->configs[0].name)));
1719 
1720 	    /* clear out any old info */
1721 	    if (raid->generation) {
1722 		for (disk = 0; disk < raid->total_disks; disk++) {
1723 		    raid->disks[disk].dev = NULL;
1724 		    raid->disks[disk].flags = 0;
1725 		}
1726 	    }
1727 	}
1728 	if (be32toh(meta->generation) >= raid->generation) {
1729 	    struct ata_device *atadev = device_get_softc(parent);
1730 	    struct ata_channel *ch = device_get_softc(GRANDPARENT(dev));
1731 	    int disk_number = (ch->unit << !(ch->flags & ATA_NO_SLAVE)) +
1732 			      ATA_DEV(atadev->unit);
1733 
1734 	    raid->disks[disk_number].dev = parent;
1735 	    raid->disks[disk_number].sectors =
1736 		be32toh(meta->configs[disk_number + 1].sectors);
1737 	    raid->disks[disk_number].flags =
1738 		(AR_DF_ONLINE | AR_DF_PRESENT | AR_DF_ASSIGNED);
1739 	    ars->raid[raid->volume] = raid;
1740 	    ars->disk_number[raid->volume] = disk_number;
1741 	    retval = 1;
1742 	}
1743 	break;
1744     }
1745 
1746 adaptec_out:
1747     kfree(meta, M_AR);
1748     return retval;
1749 }
1750 
1751 /* Highpoint V2 RocketRAID Metadata */
1752 static int
1753 ata_raid_hptv2_read_meta(device_t dev, struct ar_softc **raidp)
1754 {
1755     struct ata_raid_subdisk *ars = device_get_softc(dev);
1756     device_t parent = device_get_parent(dev);
1757     struct hptv2_raid_conf *meta;
1758     struct ar_softc *raid = NULL;
1759     int array, disk_number = 0, retval = 0;
1760 
1761     meta = (struct hptv2_raid_conf *)kmalloc(sizeof(struct hptv2_raid_conf),
1762 	M_AR, M_WAITOK | M_ZERO);
1763 
1764     if (ata_raid_rw(parent, HPTV2_LBA(parent),
1765 		    meta, sizeof(struct hptv2_raid_conf), ATA_R_READ)) {
1766 	if (testing || bootverbose)
1767 	    device_printf(parent, "HighPoint (v2) read metadata failed\n");
1768 	goto hptv2_out;
1769     }
1770 
1771     /* check if this is a HighPoint v2 RAID struct */
1772     if (meta->magic != HPTV2_MAGIC_OK && meta->magic != HPTV2_MAGIC_BAD) {
1773 	if (testing || bootverbose)
1774 	    device_printf(parent, "HighPoint (v2) check1 failed\n");
1775 	goto hptv2_out;
1776     }
1777 
1778     /* is this disk defined, or an old leftover/spare ? */
1779     if (!meta->magic_0) {
1780 	if (testing || bootverbose)
1781 	    device_printf(parent, "HighPoint (v2) check2 failed\n");
1782 	goto hptv2_out;
1783     }
1784 
1785     if (testing || bootverbose)
1786 	ata_raid_hptv2_print_meta(meta);
1787 
1788     /* now convert HighPoint (v2) metadata into our generic form */
1789     for (array = 0; array < MAX_ARRAYS; array++) {
1790 	if (!raidp[array]) {
1791 	    raidp[array] =
1792 		(struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
1793 					  M_WAITOK | M_ZERO);
1794 	}
1795 	raid = raidp[array];
1796 	if (raid->format && (raid->format != AR_F_HPTV2_RAID))
1797 	    continue;
1798 
1799 	switch (meta->type) {
1800 	case HPTV2_T_RAID0:
1801 	    if ((meta->order & (HPTV2_O_RAID0|HPTV2_O_OK)) ==
1802 		(HPTV2_O_RAID0|HPTV2_O_OK))
1803 		goto highpoint_raid1;
1804 	    if (meta->order & (HPTV2_O_RAID0 | HPTV2_O_RAID1))
1805 		goto highpoint_raid01;
1806 	    if (raid->magic_0 && raid->magic_0 != meta->magic_0)
1807 		continue;
1808 	    raid->magic_0 = meta->magic_0;
1809 	    raid->type = AR_T_RAID0;
1810 	    raid->interleave = 1 << meta->stripe_shift;
1811 	    disk_number = meta->disk_number;
1812 	    if (!(meta->order & HPTV2_O_OK))
1813 		meta->magic = 0;        /* mark bad */
1814 	    break;
1815 
1816 	case HPTV2_T_RAID1:
1817 highpoint_raid1:
1818 	    if (raid->magic_0 && raid->magic_0 != meta->magic_0)
1819 		continue;
1820 	    raid->magic_0 = meta->magic_0;
1821 	    raid->type = AR_T_RAID1;
1822 	    disk_number = (meta->disk_number > 0);
1823 	    break;
1824 
1825 	case HPTV2_T_RAID01_RAID0:
1826 highpoint_raid01:
1827 	    if (meta->order & HPTV2_O_RAID0) {
1828 		if ((raid->magic_0 && raid->magic_0 != meta->magic_0) ||
1829 		    (raid->magic_1 && raid->magic_1 != meta->magic_1))
1830 		    continue;
1831 		raid->magic_0 = meta->magic_0;
1832 		raid->magic_1 = meta->magic_1;
1833 		raid->type = AR_T_RAID01;
1834 		raid->interleave = 1 << meta->stripe_shift;
1835 		disk_number = meta->disk_number;
1836 	    }
1837 	    else {
1838 		if (raid->magic_1 && raid->magic_1 != meta->magic_1)
1839 		    continue;
1840 		raid->magic_1 = meta->magic_1;
1841 		raid->type = AR_T_RAID01;
1842 		raid->interleave = 1 << meta->stripe_shift;
1843 		disk_number = meta->disk_number + meta->array_width;
1844 		if (!(meta->order & HPTV2_O_RAID1))
1845 		    meta->magic = 0;    /* mark bad */
1846 	    }
1847 	    break;
1848 
1849 	case HPTV2_T_SPAN:
1850 	    if (raid->magic_0 && raid->magic_0 != meta->magic_0)
1851 		continue;
1852 	    raid->magic_0 = meta->magic_0;
1853 	    raid->type = AR_T_SPAN;
1854 	    disk_number = meta->disk_number;
1855 	    break;
1856 
1857 	default:
1858 	    device_printf(parent, "Highpoint (v2) unknown RAID type 0x%02x\n",
1859 			  meta->type);
1860 	    kfree(raidp[array], M_AR);
1861 	    raidp[array] = NULL;
1862 	    goto hptv2_out;
1863 	}
1864 
1865 	raid->format |= AR_F_HPTV2_RAID;
1866 	raid->disks[disk_number].dev = parent;
1867 	raid->disks[disk_number].flags = (AR_DF_PRESENT | AR_DF_ASSIGNED);
1868 	raid->lun = array;
1869 	strncpy(raid->name, meta->name_1,
1870 		min(sizeof(raid->name), sizeof(meta->name_1)));
1871 	if (meta->magic == HPTV2_MAGIC_OK) {
1872 	    raid->disks[disk_number].flags |= AR_DF_ONLINE;
1873 	    raid->width = meta->array_width;
1874 	    raid->total_sectors = meta->total_sectors;
1875 	    raid->heads = 255;
1876 	    raid->sectors = 63;
1877 	    raid->cylinders = raid->total_sectors / (63 * 255);
1878 	    raid->offset_sectors = HPTV2_LBA(parent) + 1;
1879 	    raid->rebuild_lba = meta->rebuild_lba;
1880 	    raid->disks[disk_number].sectors =
1881 		raid->total_sectors / raid->width;
1882 	}
1883 	else
1884 	    raid->disks[disk_number].flags &= ~AR_DF_ONLINE;
1885 
1886 	if ((raid->type & AR_T_RAID0) && (raid->total_disks < raid->width))
1887 	    raid->total_disks = raid->width;
1888 	if (disk_number >= raid->total_disks)
1889 	    raid->total_disks = disk_number + 1;
1890 	ars->raid[raid->volume] = raid;
1891 	ars->disk_number[raid->volume] = disk_number;
1892 	retval = 1;
1893 	break;
1894     }
1895 
1896 hptv2_out:
1897     kfree(meta, M_AR);
1898     return retval;
1899 }
1900 
1901 static int
1902 ata_raid_hptv2_write_meta(struct ar_softc *rdp)
1903 {
1904     struct hptv2_raid_conf *meta;
1905     struct timeval timestamp;
1906     int disk, error = 0;
1907 
1908     meta = (struct hptv2_raid_conf *)kmalloc(sizeof(struct hptv2_raid_conf),
1909 	M_AR, M_WAITOK | M_ZERO);
1910 
1911     microtime(&timestamp);
1912     rdp->magic_0 = timestamp.tv_sec + 2;
1913     rdp->magic_1 = timestamp.tv_sec;
1914 
1915     for (disk = 0; disk < rdp->total_disks; disk++) {
1916 	if ((rdp->disks[disk].flags & (AR_DF_PRESENT | AR_DF_ONLINE)) ==
1917 	    (AR_DF_PRESENT | AR_DF_ONLINE))
1918 	    meta->magic = HPTV2_MAGIC_OK;
1919 	if (rdp->disks[disk].flags & AR_DF_ASSIGNED) {
1920 	    meta->magic_0 = rdp->magic_0;
1921 	    if (strlen(rdp->name))
1922 		strncpy(meta->name_1, rdp->name, sizeof(meta->name_1));
1923 	    else
1924 		strcpy(meta->name_1, "FreeBSD");
1925 	}
1926 	meta->disk_number = disk;
1927 
1928 	switch (rdp->type) {
1929 	case AR_T_RAID0:
1930 	    meta->type = HPTV2_T_RAID0;
1931 	    strcpy(meta->name_2, "RAID 0");
1932 	    if (rdp->disks[disk].flags & AR_DF_ONLINE)
1933 		meta->order = HPTV2_O_OK;
1934 	    break;
1935 
1936 	case AR_T_RAID1:
1937 	    meta->type = HPTV2_T_RAID0;
1938 	    strcpy(meta->name_2, "RAID 1");
1939 	    meta->disk_number = (disk < rdp->width) ? disk : disk + 5;
1940 	    meta->order = HPTV2_O_RAID0 | HPTV2_O_OK;
1941 	    break;
1942 
1943 	case AR_T_RAID01:
1944 	    meta->type = HPTV2_T_RAID01_RAID0;
1945 	    strcpy(meta->name_2, "RAID 0+1");
1946 	    if (rdp->disks[disk].flags & AR_DF_ONLINE) {
1947 		if (disk < rdp->width) {
1948 		    meta->order = (HPTV2_O_RAID0 | HPTV2_O_RAID1);
1949 		    meta->magic_0 = rdp->magic_0 - 1;
1950 		}
1951 		else {
1952 		    meta->order = HPTV2_O_RAID1;
1953 		    meta->disk_number -= rdp->width;
1954 		}
1955 	    }
1956 	    else
1957 		meta->magic_0 = rdp->magic_0 - 1;
1958 	    meta->magic_1 = rdp->magic_1;
1959 	    break;
1960 
1961 	case AR_T_SPAN:
1962 	    meta->type = HPTV2_T_SPAN;
1963 	    strcpy(meta->name_2, "SPAN");
1964 	    break;
1965 	default:
1966 	    kfree(meta, M_AR);
1967 	    return ENODEV;
1968 	}
1969 
1970 	meta->array_width = rdp->width;
1971 	meta->stripe_shift = (rdp->width > 1) ? (ffs(rdp->interleave)-1) : 0;
1972 	meta->total_sectors = rdp->total_sectors;
1973 	meta->rebuild_lba = rdp->rebuild_lba;
1974 	if (testing || bootverbose)
1975 	    ata_raid_hptv2_print_meta(meta);
1976 	if (rdp->disks[disk].dev) {
1977 	    if (ata_raid_rw(rdp->disks[disk].dev,
1978 			    HPTV2_LBA(rdp->disks[disk].dev), meta,
1979 			    sizeof(struct promise_raid_conf),
1980 			    ATA_R_WRITE | ATA_R_DIRECT)) {
1981 		device_printf(rdp->disks[disk].dev, "write metadata failed\n");
1982 		error = EIO;
1983 	    }
1984 	}
1985     }
1986     kfree(meta, M_AR);
1987     return error;
1988 }
1989 
1990 /* Highpoint V3 RocketRAID Metadata */
1991 static int
1992 ata_raid_hptv3_read_meta(device_t dev, struct ar_softc **raidp)
1993 {
1994     struct ata_raid_subdisk *ars = device_get_softc(dev);
1995     device_t parent = device_get_parent(dev);
1996     struct hptv3_raid_conf *meta;
1997     struct ar_softc *raid = NULL;
1998     int array, disk_number, retval = 0;
1999 
2000     meta = (struct hptv3_raid_conf *)kmalloc(sizeof(struct hptv3_raid_conf),
2001 	M_AR, M_WAITOK | M_ZERO);
2002 
2003     if (ata_raid_rw(parent, HPTV3_LBA(parent),
2004 		    meta, sizeof(struct hptv3_raid_conf), ATA_R_READ)) {
2005 	if (testing || bootverbose)
2006 	    device_printf(parent, "HighPoint (v3) read metadata failed\n");
2007 	goto hptv3_out;
2008     }
2009 
2010     /* check if this is a HighPoint v3 RAID struct */
2011     if (meta->magic != HPTV3_MAGIC) {
2012 	if (testing || bootverbose)
2013 	    device_printf(parent, "HighPoint (v3) check1 failed\n");
2014 	goto hptv3_out;
2015     }
2016 
2017     /* check if there are any config_entries */
2018     if (meta->config_entries < 1) {
2019 	if (testing || bootverbose)
2020 	    device_printf(parent, "HighPoint (v3) check2 failed\n");
2021 	goto hptv3_out;
2022     }
2023 
2024     if (testing || bootverbose)
2025 	ata_raid_hptv3_print_meta(meta);
2026 
2027     /* now convert HighPoint (v3) metadata into our generic form */
2028     for (array = 0; array < MAX_ARRAYS; array++) {
2029 	if (!raidp[array]) {
2030 	    raidp[array] =
2031 		(struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
2032 					  M_WAITOK | M_ZERO);
2033 	}
2034 	raid = raidp[array];
2035 	if (raid->format && (raid->format != AR_F_HPTV3_RAID))
2036 	    continue;
2037 
2038 	if ((raid->format & AR_F_HPTV3_RAID) && raid->magic_0 != meta->magic_0)
2039 	    continue;
2040 
2041 	switch (meta->configs[0].type) {
2042 	case HPTV3_T_RAID0:
2043 	    raid->type = AR_T_RAID0;
2044 	    raid->width = meta->configs[0].total_disks;
2045 	    disk_number = meta->configs[0].disk_number;
2046 	    break;
2047 
2048 	case HPTV3_T_RAID1:
2049 	    raid->type = AR_T_RAID1;
2050 	    raid->width = meta->configs[0].total_disks / 2;
2051 	    disk_number = meta->configs[0].disk_number;
2052 	    break;
2053 
2054 	case HPTV3_T_RAID5:
2055 	    raid->type = AR_T_RAID5;
2056 	    raid->width = meta->configs[0].total_disks;
2057 	    disk_number = meta->configs[0].disk_number;
2058 	    break;
2059 
2060 	case HPTV3_T_SPAN:
2061 	    raid->type = AR_T_SPAN;
2062 	    raid->width = meta->configs[0].total_disks;
2063 	    disk_number = meta->configs[0].disk_number;
2064 	    break;
2065 
2066 	default:
2067 	    device_printf(parent, "Highpoint (v3) unknown RAID type 0x%02x\n",
2068 			  meta->configs[0].type);
2069 	    kfree(raidp[array], M_AR);
2070 	    raidp[array] = NULL;
2071 	    goto hptv3_out;
2072 	}
2073 	if (meta->config_entries == 2) {
2074 	    switch (meta->configs[1].type) {
2075 	    case HPTV3_T_RAID1:
2076 		if (raid->type == AR_T_RAID0) {
2077 		    raid->type = AR_T_RAID01;
2078 		    disk_number = meta->configs[1].disk_number +
2079 				  (meta->configs[0].disk_number << 1);
2080 		    break;
2081 		}
2082 	    default:
2083 		device_printf(parent, "Highpoint (v3) unknown level 2 0x%02x\n",
2084 			      meta->configs[1].type);
2085 		kfree(raidp[array], M_AR);
2086 		raidp[array] = NULL;
2087 		goto hptv3_out;
2088 	    }
2089 	}
2090 
2091 	raid->magic_0 = meta->magic_0;
2092 	raid->format = AR_F_HPTV3_RAID;
2093 	raid->generation = meta->timestamp;
2094 	raid->interleave = 1 << meta->configs[0].stripe_shift;
2095 	raid->total_disks = meta->configs[0].total_disks +
2096 	    meta->configs[1].total_disks;
2097 	raid->total_sectors = meta->configs[0].total_sectors +
2098 	    ((u_int64_t)meta->configs_high[0].total_sectors << 32);
2099 	raid->heads = 255;
2100 	raid->sectors = 63;
2101 	raid->cylinders = raid->total_sectors / (63 * 255);
2102 	raid->offset_sectors = 0;
2103 	raid->rebuild_lba = meta->configs[0].rebuild_lba +
2104 	    ((u_int64_t)meta->configs_high[0].rebuild_lba << 32);
2105 	raid->lun = array;
2106 	strncpy(raid->name, meta->name,
2107 		min(sizeof(raid->name), sizeof(meta->name)));
2108 	raid->disks[disk_number].sectors = raid->total_sectors /
2109 	    (raid->type == AR_T_RAID5 ? raid->width - 1 : raid->width);
2110 	raid->disks[disk_number].dev = parent;
2111 	raid->disks[disk_number].flags =
2112 	    (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_ONLINE);
2113 	ars->raid[raid->volume] = raid;
2114 	ars->disk_number[raid->volume] = disk_number;
2115 	retval = 1;
2116 	break;
2117     }
2118 
2119 hptv3_out:
2120     kfree(meta, M_AR);
2121     return retval;
2122 }
2123 
2124 /* Intel MatrixRAID Metadata */
2125 static int
2126 ata_raid_intel_read_meta(device_t dev, struct ar_softc **raidp)
2127 {
2128     struct ata_raid_subdisk *ars = device_get_softc(dev);
2129     device_t parent = device_get_parent(dev);
2130     struct intel_raid_conf *meta;
2131     struct intel_raid_mapping *map;
2132     struct ar_softc *raid = NULL;
2133     u_int32_t checksum, *ptr;
2134     int array, count, disk, volume = 1, retval = 0;
2135     char *tmp;
2136 
2137     meta = (struct intel_raid_conf *)kmalloc(1536, M_AR, M_WAITOK | M_ZERO);
2138 
2139     if (ata_raid_rw(parent, INTEL_LBA(parent), meta, 1024, ATA_R_READ)) {
2140 	if (testing || bootverbose)
2141 	    device_printf(parent, "Intel read metadata failed\n");
2142 	goto intel_out;
2143     }
2144     tmp = (char *)meta;
2145     bcopy(tmp, tmp+1024, 512);
2146     bcopy(tmp+512, tmp, 1024);
2147     bzero(tmp+1024, 512);
2148 
2149     /* check if this is a Intel RAID struct */
2150     if (strncmp(meta->intel_id, INTEL_MAGIC, strlen(INTEL_MAGIC))) {
2151 	if (testing || bootverbose)
2152 	    device_printf(parent, "Intel check1 failed\n");
2153 	goto intel_out;
2154     }
2155 
2156     for (checksum = 0, ptr = (u_int32_t *)meta, count = 0;
2157 	 count < (meta->config_size / sizeof(u_int32_t)); count++) {
2158 	checksum += *ptr++;
2159     }
2160     checksum -= meta->checksum;
2161     if (checksum != meta->checksum) {
2162 	if (testing || bootverbose)
2163 	    device_printf(parent, "Intel check2 failed\n");
2164 	goto intel_out;
2165     }
2166 
2167     if (testing || bootverbose)
2168 	ata_raid_intel_print_meta(meta);
2169 
2170     map = (struct intel_raid_mapping *)&meta->disk[meta->total_disks];
2171 
2172     /* now convert Intel metadata into our generic form */
2173     for (array = 0; array < MAX_ARRAYS; array++) {
2174 	if (!raidp[array]) {
2175 	    raidp[array] =
2176 		(struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
2177 					  M_WAITOK | M_ZERO);
2178 	}
2179 	raid = raidp[array];
2180 	if (raid->format && (raid->format != AR_F_INTEL_RAID))
2181 	    continue;
2182 
2183 	if ((raid->format & AR_F_INTEL_RAID) &&
2184 	    (raid->magic_0 != meta->config_id))
2185 	    continue;
2186 
2187 	/*
2188 	 * update our knowledge about the array config based on generation
2189 	 * NOTE: there can be multiple volumes on a disk set
2190 	 */
2191 	if (!meta->generation || meta->generation > raid->generation) {
2192 	    switch (map->type) {
2193 	    case INTEL_T_RAID0:
2194 		raid->type = AR_T_RAID0;
2195 		raid->width = map->total_disks;
2196 		break;
2197 
2198 	    case INTEL_T_RAID1:
2199 		if (map->total_disks == 4)
2200 		    raid->type = AR_T_RAID01;
2201 		else
2202 		    raid->type = AR_T_RAID1;
2203 		raid->width = map->total_disks / 2;
2204 		break;
2205 
2206 	    case INTEL_T_RAID5:
2207 		raid->type = AR_T_RAID5;
2208 		raid->width = map->total_disks;
2209 		break;
2210 
2211 	    default:
2212 		device_printf(parent, "Intel unknown RAID type 0x%02x\n",
2213 			      map->type);
2214 		kfree(raidp[array], M_AR);
2215 		raidp[array] = NULL;
2216 		goto intel_out;
2217 	    }
2218 
2219 	    switch (map->status) {
2220 	    case INTEL_S_READY:
2221 		raid->status = AR_S_READY;
2222 		break;
2223 	    case INTEL_S_DEGRADED:
2224 		raid->status |= AR_S_DEGRADED;
2225 		break;
2226 	    case INTEL_S_DISABLED:
2227 	    case INTEL_S_FAILURE:
2228 		raid->status = 0;
2229 	    }
2230 
2231 	    raid->magic_0 = meta->config_id;
2232 	    raid->format = AR_F_INTEL_RAID;
2233 	    raid->generation = meta->generation;
2234 	    raid->interleave = map->stripe_sectors;
2235 	    raid->total_disks = map->total_disks;
2236 	    raid->total_sectors = map->total_sectors;
2237 	    raid->heads = 255;
2238 	    raid->sectors = 63;
2239 	    raid->cylinders = raid->total_sectors / (63 * 255);
2240 	    raid->offset_sectors = map->offset;
2241 	    raid->rebuild_lba = 0;
2242 	    raid->lun = array;
2243 	    raid->volume = volume - 1;
2244 	    strncpy(raid->name, map->name,
2245 		    min(sizeof(raid->name), sizeof(map->name)));
2246 
2247 	    /* clear out any old info */
2248 	    for (disk = 0; disk < raid->total_disks; disk++) {
2249 		raid->disks[disk].dev = NULL;
2250 		bcopy(meta->disk[map->disk_idx[disk]].serial,
2251 		      raid->disks[disk].serial,
2252 		      sizeof(raid->disks[disk].serial));
2253 		raid->disks[disk].sectors =
2254 		    meta->disk[map->disk_idx[disk]].sectors;
2255 		raid->disks[disk].flags = 0;
2256 		if (meta->disk[map->disk_idx[disk]].flags & INTEL_F_ONLINE)
2257 		    raid->disks[disk].flags |= AR_DF_ONLINE;
2258 		if (meta->disk[map->disk_idx[disk]].flags & INTEL_F_ASSIGNED)
2259 		    raid->disks[disk].flags |= AR_DF_ASSIGNED;
2260 		if (meta->disk[map->disk_idx[disk]].flags & INTEL_F_SPARE) {
2261 		    raid->disks[disk].flags &= ~(AR_DF_ONLINE | AR_DF_ASSIGNED);
2262 		    raid->disks[disk].flags |= AR_DF_SPARE;
2263 		}
2264 		if (meta->disk[map->disk_idx[disk]].flags & INTEL_F_DOWN)
2265 		    raid->disks[disk].flags &= ~AR_DF_ONLINE;
2266 	    }
2267 	}
2268 	if (meta->generation >= raid->generation) {
2269 	    for (disk = 0; disk < raid->total_disks; disk++) {
2270 		struct ata_device *atadev = device_get_softc(parent);
2271 
2272 		if (!strncmp(raid->disks[disk].serial, atadev->param.serial,
2273 		    sizeof(raid->disks[disk].serial))) {
2274 		    raid->disks[disk].dev = parent;
2275 		    raid->disks[disk].flags |= (AR_DF_PRESENT | AR_DF_ONLINE);
2276 		    ars->raid[raid->volume] = raid;
2277 		    ars->disk_number[raid->volume] = disk;
2278 		    retval = 1;
2279 		}
2280 	    }
2281 	}
2282 	else
2283 	    goto intel_out;
2284 
2285 	if (retval) {
2286 	    if (volume < meta->total_volumes) {
2287 		map = (struct intel_raid_mapping *)
2288 		      &map->disk_idx[map->total_disks];
2289 		volume++;
2290 		retval = 0;
2291 		continue;
2292 	    }
2293 	    break;
2294 	}
2295 	else {
2296 	    kfree(raidp[array], M_AR);
2297 	    raidp[array] = NULL;
2298 	    if (volume == 2)
2299 		retval = 1;
2300 	}
2301     }
2302 
2303 intel_out:
2304     kfree(meta, M_AR);
2305     return retval;
2306 }
2307 
2308 static int
2309 ata_raid_intel_write_meta(struct ar_softc *rdp)
2310 {
2311     struct intel_raid_conf *meta;
2312     struct intel_raid_mapping *map;
2313     struct timeval timestamp;
2314     u_int32_t checksum, *ptr;
2315     int count, disk, error = 0;
2316     char *tmp;
2317 
2318     meta = (struct intel_raid_conf *)kmalloc(1536, M_AR, M_WAITOK | M_ZERO);
2319 
2320     rdp->generation++;
2321 
2322     /* Generate a new config_id if none exists */
2323     if (!rdp->magic_0) {
2324 	microtime(&timestamp);
2325 	rdp->magic_0 = timestamp.tv_sec ^ timestamp.tv_usec;
2326     }
2327 
2328     bcopy(INTEL_MAGIC, meta->intel_id, sizeof(meta->intel_id));
2329     bcopy(INTEL_VERSION_1100, meta->version, sizeof(meta->version));
2330     meta->config_id = rdp->magic_0;
2331     meta->generation = rdp->generation;
2332     meta->total_disks = rdp->total_disks;
2333     meta->total_volumes = 1;                                    /* XXX SOS */
2334     for (disk = 0; disk < rdp->total_disks; disk++) {
2335 	if (rdp->disks[disk].dev) {
2336 	    struct ata_channel *ch =
2337 		device_get_softc(device_get_parent(rdp->disks[disk].dev));
2338 	    struct ata_device *atadev =
2339 		device_get_softc(rdp->disks[disk].dev);
2340 
2341 	    bcopy(atadev->param.serial, meta->disk[disk].serial,
2342 		  sizeof(rdp->disks[disk].serial));
2343 	    meta->disk[disk].sectors = rdp->disks[disk].sectors;
2344 	    meta->disk[disk].id = (ch->unit << 16) | ATA_DEV(atadev->unit);
2345 	}
2346 	else
2347 	    meta->disk[disk].sectors = rdp->total_sectors / rdp->width;
2348 	meta->disk[disk].flags = 0;
2349 	if (rdp->disks[disk].flags & AR_DF_SPARE)
2350 	    meta->disk[disk].flags  |= INTEL_F_SPARE;
2351 	else {
2352 	    if (rdp->disks[disk].flags & AR_DF_ONLINE)
2353 		meta->disk[disk].flags |= INTEL_F_ONLINE;
2354 	    else
2355 		meta->disk[disk].flags |= INTEL_F_DOWN;
2356 	    if (rdp->disks[disk].flags & AR_DF_ASSIGNED)
2357 		meta->disk[disk].flags  |= INTEL_F_ASSIGNED;
2358 	}
2359     }
2360     map = (struct intel_raid_mapping *)&meta->disk[meta->total_disks];
2361 
2362     bcopy(rdp->name, map->name, sizeof(rdp->name));
2363     map->total_sectors = rdp->total_sectors;
2364     map->state = 12;                                            /* XXX SOS */
2365     map->offset = rdp->offset_sectors;
2366     map->stripe_count = rdp->total_sectors / (rdp->interleave*rdp->total_disks);
2367     map->stripe_sectors =  rdp->interleave;
2368     map->disk_sectors = rdp->total_sectors / rdp->width;
2369     map->status = INTEL_S_READY;                                /* XXX SOS */
2370     switch (rdp->type) {
2371     case AR_T_RAID0:
2372 	map->type = INTEL_T_RAID0;
2373 	break;
2374     case AR_T_RAID1:
2375 	map->type = INTEL_T_RAID1;
2376 	break;
2377     case AR_T_RAID01:
2378 	map->type = INTEL_T_RAID1;
2379 	break;
2380     case AR_T_RAID5:
2381 	map->type = INTEL_T_RAID5;
2382 	break;
2383     default:
2384 	kfree(meta, M_AR);
2385 	return ENODEV;
2386     }
2387     map->total_disks = rdp->total_disks;
2388     map->magic[0] = 0x02;
2389     map->magic[1] = 0xff;
2390     map->magic[2] = 0x01;
2391     for (disk = 0; disk < rdp->total_disks; disk++)
2392 	map->disk_idx[disk] = disk;
2393 
2394     meta->config_size = (char *)&map->disk_idx[disk] - (char *)meta;
2395     for (checksum = 0, ptr = (u_int32_t *)meta, count = 0;
2396 	 count < (meta->config_size / sizeof(u_int32_t)); count++) {
2397 	checksum += *ptr++;
2398     }
2399     meta->checksum = checksum;
2400 
2401     if (testing || bootverbose)
2402 	ata_raid_intel_print_meta(meta);
2403 
2404     tmp = (char *)meta;
2405     bcopy(tmp, tmp+1024, 512);
2406     bcopy(tmp+512, tmp, 1024);
2407     bzero(tmp+1024, 512);
2408 
2409     for (disk = 0; disk < rdp->total_disks; disk++) {
2410 	if (rdp->disks[disk].dev) {
2411 	    if (ata_raid_rw(rdp->disks[disk].dev,
2412 			    INTEL_LBA(rdp->disks[disk].dev),
2413 			    meta, 1024, ATA_R_WRITE | ATA_R_DIRECT)) {
2414 		device_printf(rdp->disks[disk].dev, "write metadata failed\n");
2415 		error = EIO;
2416 	    }
2417 	}
2418     }
2419     kfree(meta, M_AR);
2420     return error;
2421 }
2422 
2423 
2424 /* Integrated Technology Express Metadata */
2425 static int
2426 ata_raid_ite_read_meta(device_t dev, struct ar_softc **raidp)
2427 {
2428     struct ata_raid_subdisk *ars = device_get_softc(dev);
2429     device_t parent = device_get_parent(dev);
2430     struct ite_raid_conf *meta;
2431     struct ar_softc *raid = NULL;
2432     int array, disk_number, count, retval = 0;
2433     u_int16_t *ptr;
2434 
2435     meta = (struct ite_raid_conf *)kmalloc(sizeof(struct ite_raid_conf), M_AR,
2436 	M_WAITOK | M_ZERO);
2437 
2438     if (ata_raid_rw(parent, ITE_LBA(parent),
2439 		    meta, sizeof(struct ite_raid_conf), ATA_R_READ)) {
2440 	if (testing || bootverbose)
2441 	    device_printf(parent, "ITE read metadata failed\n");
2442 	goto ite_out;
2443     }
2444 
2445     /* check if this is a ITE RAID struct */
2446     for (ptr = (u_int16_t *)meta->ite_id, count = 0;
2447 	 count < sizeof(meta->ite_id)/sizeof(uint16_t); count++)
2448 	ptr[count] = be16toh(ptr[count]);
2449 
2450     if (strncmp(meta->ite_id, ITE_MAGIC, strlen(ITE_MAGIC))) {
2451 	if (testing || bootverbose)
2452 	    device_printf(parent, "ITE check1 failed\n");
2453 	goto ite_out;
2454     }
2455 
2456     if (testing || bootverbose)
2457 	ata_raid_ite_print_meta(meta);
2458 
2459     /* now convert ITE metadata into our generic form */
2460     for (array = 0; array < MAX_ARRAYS; array++) {
2461 	if ((raid = raidp[array])) {
2462 	    if (raid->format != AR_F_ITE_RAID)
2463 		continue;
2464 	    if (raid->magic_0 != *((u_int64_t *)meta->timestamp_0))
2465 		continue;
2466 	}
2467 
2468 	/* if we dont have a disks timestamp the RAID is invalidated */
2469 	if (*((u_int64_t *)meta->timestamp_1) == 0)
2470 	    goto ite_out;
2471 
2472 	if (!raid) {
2473 	    raidp[array] = (struct ar_softc *)kmalloc(sizeof(struct ar_softc),
2474 						     M_AR, M_WAITOK | M_ZERO);
2475 	}
2476 
2477 	switch (meta->type) {
2478 	case ITE_T_RAID0:
2479 	    raid->type = AR_T_RAID0;
2480 	    raid->width = meta->array_width;
2481 	    raid->total_disks = meta->array_width;
2482 	    disk_number = meta->disk_number;
2483 	    break;
2484 
2485 	case ITE_T_RAID1:
2486 	    raid->type = AR_T_RAID1;
2487 	    raid->width = 1;
2488 	    raid->total_disks = 2;
2489 	    disk_number = meta->disk_number;
2490 	    break;
2491 
2492 	case ITE_T_RAID01:
2493 	    raid->type = AR_T_RAID01;
2494 	    raid->width = meta->array_width;
2495 	    raid->total_disks = 4;
2496 	    disk_number = ((meta->disk_number & 0x02) >> 1) |
2497 			  ((meta->disk_number & 0x01) << 1);
2498 	    break;
2499 
2500 	case ITE_T_SPAN:
2501 	    raid->type = AR_T_SPAN;
2502 	    raid->width = 1;
2503 	    raid->total_disks = meta->array_width;
2504 	    disk_number = meta->disk_number;
2505 	    break;
2506 
2507 	default:
2508 	    device_printf(parent, "ITE unknown RAID type 0x%02x\n", meta->type);
2509 	    kfree(raidp[array], M_AR);
2510 	    raidp[array] = NULL;
2511 	    goto ite_out;
2512 	}
2513 
2514 	raid->magic_0 = *((u_int64_t *)meta->timestamp_0);
2515 	raid->format = AR_F_ITE_RAID;
2516 	raid->generation = 0;
2517 	raid->interleave = meta->stripe_sectors;
2518 	raid->total_sectors = meta->total_sectors;
2519 	raid->heads = 255;
2520 	raid->sectors = 63;
2521 	raid->cylinders = raid->total_sectors / (63 * 255);
2522 	raid->offset_sectors = 0;
2523 	raid->rebuild_lba = 0;
2524 	raid->lun = array;
2525 
2526 	raid->disks[disk_number].dev = parent;
2527 	raid->disks[disk_number].sectors = raid->total_sectors / raid->width;
2528 	raid->disks[disk_number].flags =
2529 	    (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_ONLINE);
2530 	ars->raid[raid->volume] = raid;
2531 	ars->disk_number[raid->volume] = disk_number;
2532 	retval = 1;
2533 	break;
2534     }
2535 ite_out:
2536     kfree(meta, M_AR);
2537     return retval;
2538 }
2539 
2540 /* JMicron Technology Corp Metadata */
2541 static int
2542 ata_raid_jmicron_read_meta(device_t dev, struct ar_softc **raidp)
2543 {
2544     struct ata_raid_subdisk *ars = device_get_softc(dev);
2545     device_t parent = device_get_parent(dev);
2546     struct jmicron_raid_conf *meta;
2547     struct ar_softc *raid = NULL;
2548     u_int16_t checksum, *ptr;
2549     u_int64_t disk_size;
2550     int count, array, disk, total_disks, retval = 0;
2551 
2552     meta = (struct jmicron_raid_conf *)
2553 	kmalloc(sizeof(struct jmicron_raid_conf), M_AR, M_WAITOK | M_ZERO);
2554 
2555     if (ata_raid_rw(parent, JMICRON_LBA(parent),
2556 		    meta, sizeof(struct jmicron_raid_conf), ATA_R_READ)) {
2557 	if (testing || bootverbose)
2558 	    device_printf(parent,
2559 			  "JMicron read metadata failed\n");
2560     }
2561 
2562     /* check for JMicron signature */
2563     if (strncmp(meta->signature, JMICRON_MAGIC, 2)) {
2564 	if (testing || bootverbose)
2565 	    device_printf(parent, "JMicron check1 failed\n");
2566 	goto jmicron_out;
2567     }
2568 
2569     /* calculate checksum and compare for valid */
2570     for (checksum = 0, ptr = (u_int16_t *)meta, count = 0; count < 64; count++)
2571 	checksum += *ptr++;
2572     if (checksum) {
2573 	if (testing || bootverbose)
2574 	    device_printf(parent, "JMicron check2 failed\n");
2575 	goto jmicron_out;
2576     }
2577 
2578     if (testing || bootverbose)
2579 	ata_raid_jmicron_print_meta(meta);
2580 
2581     /* now convert JMicron meta into our generic form */
2582     for (array = 0; array < MAX_ARRAYS; array++) {
2583 jmicron_next:
2584 	if (!raidp[array]) {
2585 	    raidp[array] =
2586 		(struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
2587 					  M_WAITOK | M_ZERO);
2588 	}
2589 	raid = raidp[array];
2590 	if (raid->format && (raid->format != AR_F_JMICRON_RAID))
2591 	    continue;
2592 
2593 	for (total_disks = 0, disk = 0; disk < JM_MAX_DISKS; disk++) {
2594 	    if (meta->disks[disk]) {
2595 		if (raid->format == AR_F_JMICRON_RAID) {
2596 		    if (bcmp(&meta->disks[disk],
2597 			raid->disks[disk].serial, sizeof(u_int32_t))) {
2598 			array++;
2599 			goto jmicron_next;
2600 		    }
2601 		}
2602 		else
2603 		    bcopy(&meta->disks[disk],
2604 			  raid->disks[disk].serial, sizeof(u_int32_t));
2605 		total_disks++;
2606 	    }
2607 	}
2608 	/* handle spares XXX SOS */
2609 
2610 	switch (meta->type) {
2611 	case JM_T_RAID0:
2612 	    raid->type = AR_T_RAID0;
2613 	    raid->width = total_disks;
2614 	    break;
2615 
2616 	case JM_T_RAID1:
2617 	    raid->type = AR_T_RAID1;
2618 	    raid->width = 1;
2619 	    break;
2620 
2621 	case JM_T_RAID01:
2622 	    raid->type = AR_T_RAID01;
2623 	    raid->width = total_disks / 2;
2624 	    break;
2625 
2626 	case JM_T_RAID5:
2627 	    raid->type = AR_T_RAID5;
2628 	    raid->width = total_disks;
2629 	    break;
2630 
2631 	case JM_T_JBOD:
2632 	    raid->type = AR_T_SPAN;
2633 	    raid->width = 1;
2634 	    break;
2635 
2636 	default:
2637 	    device_printf(parent,
2638 			  "JMicron unknown RAID type 0x%02x\n", meta->type);
2639 	    kfree(raidp[array], M_AR);
2640 	    raidp[array] = NULL;
2641 	    goto jmicron_out;
2642 	}
2643 	disk_size = (meta->disk_sectors_high << 16) + meta->disk_sectors_low;
2644 	raid->format = AR_F_JMICRON_RAID;
2645 	strncpy(raid->name, meta->name, sizeof(meta->name));
2646 	raid->generation = 0;
2647 	raid->interleave = 2 << meta->stripe_shift;
2648 	raid->total_disks = total_disks;
2649 	raid->total_sectors = disk_size * (raid->width-(raid->type==AR_RAID5));
2650 	raid->heads = 255;
2651 	raid->sectors = 63;
2652 	raid->cylinders = raid->total_sectors / (63 * 255);
2653 	raid->offset_sectors = meta->offset * 16;
2654 	raid->rebuild_lba = 0;
2655 	raid->lun = array;
2656 
2657 	for (disk = 0; disk < raid->total_disks; disk++) {
2658 	    if (meta->disks[disk] == meta->disk_id) {
2659 		raid->disks[disk].dev = parent;
2660 		raid->disks[disk].sectors = disk_size;
2661 		raid->disks[disk].flags =
2662 		    (AR_DF_ONLINE | AR_DF_PRESENT | AR_DF_ASSIGNED);
2663 		ars->raid[raid->volume] = raid;
2664 		ars->disk_number[raid->volume] = disk;
2665 		retval = 1;
2666 		break;
2667 	    }
2668 	}
2669 	break;
2670     }
2671 jmicron_out:
2672     kfree(meta, M_AR);
2673     return retval;
2674 }
2675 
2676 static int
2677 ata_raid_jmicron_write_meta(struct ar_softc *rdp)
2678 {
2679     struct jmicron_raid_conf *meta;
2680     u_int64_t disk_sectors;
2681     int disk, error = 0;
2682 
2683     meta = (struct jmicron_raid_conf *)
2684 	kmalloc(sizeof(struct jmicron_raid_conf), M_AR, M_WAITOK | M_ZERO);
2685 
2686     rdp->generation++;
2687     switch (rdp->type) {
2688     case AR_T_JBOD:
2689 	meta->type = JM_T_JBOD;
2690 	break;
2691 
2692     case AR_T_RAID0:
2693 	meta->type = JM_T_RAID0;
2694 	break;
2695 
2696     case AR_T_RAID1:
2697 	meta->type = JM_T_RAID1;
2698 	break;
2699 
2700     case AR_T_RAID5:
2701 	meta->type = JM_T_RAID5;
2702 	break;
2703 
2704     case AR_T_RAID01:
2705 	meta->type = JM_T_RAID01;
2706 	break;
2707 
2708     default:
2709 	kfree(meta, M_AR);
2710 	return ENODEV;
2711     }
2712     bcopy(JMICRON_MAGIC, meta->signature, sizeof(JMICRON_MAGIC));
2713     meta->version = JMICRON_VERSION;
2714     meta->offset = rdp->offset_sectors / 16;
2715     disk_sectors = rdp->total_sectors / (rdp->width - (rdp->type == AR_RAID5));
2716     meta->disk_sectors_low = disk_sectors & 0xffff;
2717     meta->disk_sectors_high = disk_sectors >> 16;
2718     strncpy(meta->name, rdp->name, sizeof(meta->name));
2719     meta->stripe_shift = ffs(rdp->interleave) - 2;
2720 
2721     for (disk = 0; disk < rdp->total_disks; disk++) {
2722 	if (rdp->disks[disk].serial[0])
2723 	    bcopy(rdp->disks[disk].serial,&meta->disks[disk],sizeof(u_int32_t));
2724 	else
2725 	    meta->disks[disk] = (u_int32_t)(uintptr_t)rdp->disks[disk].dev;
2726     }
2727 
2728     for (disk = 0; disk < rdp->total_disks; disk++) {
2729 	if (rdp->disks[disk].dev) {
2730 	    u_int16_t checksum = 0, *ptr;
2731 	    int count;
2732 
2733 	    meta->disk_id = meta->disks[disk];
2734 	    meta->checksum = 0;
2735 	    for (ptr = (u_int16_t *)meta, count = 0; count < 64; count++)
2736 		checksum += *ptr++;
2737 	    meta->checksum -= checksum;
2738 
2739 	    if (testing || bootverbose)
2740 		ata_raid_jmicron_print_meta(meta);
2741 
2742 	    if (ata_raid_rw(rdp->disks[disk].dev,
2743 			    JMICRON_LBA(rdp->disks[disk].dev),
2744 			    meta, sizeof(struct jmicron_raid_conf),
2745 			    ATA_R_WRITE | ATA_R_DIRECT)) {
2746 		device_printf(rdp->disks[disk].dev, "write metadata failed\n");
2747 		error = EIO;
2748 	    }
2749 	}
2750     }
2751     /* handle spares XXX SOS */
2752 
2753     kfree(meta, M_AR);
2754     return error;
2755 }
2756 
2757 /* LSILogic V2 MegaRAID Metadata */
2758 static int
2759 ata_raid_lsiv2_read_meta(device_t dev, struct ar_softc **raidp)
2760 {
2761     struct ata_raid_subdisk *ars = device_get_softc(dev);
2762     device_t parent = device_get_parent(dev);
2763     struct lsiv2_raid_conf *meta;
2764     struct ar_softc *raid = NULL;
2765     int array, retval = 0;
2766 
2767     meta = (struct lsiv2_raid_conf *)kmalloc(sizeof(struct lsiv2_raid_conf),
2768 	M_AR, M_WAITOK | M_ZERO);
2769 
2770     if (ata_raid_rw(parent, LSIV2_LBA(parent),
2771 		    meta, sizeof(struct lsiv2_raid_conf), ATA_R_READ)) {
2772 	if (testing || bootverbose)
2773 	    device_printf(parent, "LSI (v2) read metadata failed\n");
2774 	goto lsiv2_out;
2775     }
2776 
2777     /* check if this is a LSI RAID struct */
2778     if (strncmp(meta->lsi_id, LSIV2_MAGIC, strlen(LSIV2_MAGIC))) {
2779 	if (testing || bootverbose)
2780 	    device_printf(parent, "LSI (v2) check1 failed\n");
2781 	goto lsiv2_out;
2782     }
2783 
2784     if (testing || bootverbose)
2785 	ata_raid_lsiv2_print_meta(meta);
2786 
2787     /* now convert LSI (v2) config meta into our generic form */
2788     for (array = 0; array < MAX_ARRAYS; array++) {
2789 	int raid_entry, conf_entry;
2790 
2791 	if (!raidp[array + meta->raid_number]) {
2792 	    raidp[array + meta->raid_number] =
2793 		(struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
2794 					  M_WAITOK | M_ZERO);
2795 	}
2796 	raid = raidp[array + meta->raid_number];
2797 	if (raid->format && (raid->format != AR_F_LSIV2_RAID))
2798 	    continue;
2799 
2800 	if (raid->magic_0 &&
2801 	    ((raid->magic_0 != meta->timestamp) ||
2802 	     (raid->magic_1 != meta->raid_number)))
2803 	    continue;
2804 
2805 	array += meta->raid_number;
2806 
2807 	raid_entry = meta->raid_number;
2808 	conf_entry = (meta->configs[raid_entry].raid.config_offset >> 4) +
2809 		     meta->disk_number - 1;
2810 
2811 	switch (meta->configs[raid_entry].raid.type) {
2812 	case LSIV2_T_RAID0:
2813 	    raid->magic_0 = meta->timestamp;
2814 	    raid->magic_1 = meta->raid_number;
2815 	    raid->type = AR_T_RAID0;
2816 	    raid->interleave = meta->configs[raid_entry].raid.stripe_sectors;
2817 	    raid->width = meta->configs[raid_entry].raid.array_width;
2818 	    break;
2819 
2820 	case LSIV2_T_RAID1:
2821 	    raid->magic_0 = meta->timestamp;
2822 	    raid->magic_1 = meta->raid_number;
2823 	    raid->type = AR_T_RAID1;
2824 	    raid->width = meta->configs[raid_entry].raid.array_width;
2825 	    break;
2826 
2827 	case LSIV2_T_RAID0 | LSIV2_T_RAID1:
2828 	    raid->magic_0 = meta->timestamp;
2829 	    raid->magic_1 = meta->raid_number;
2830 	    raid->type = AR_T_RAID01;
2831 	    raid->interleave = meta->configs[raid_entry].raid.stripe_sectors;
2832 	    raid->width = meta->configs[raid_entry].raid.array_width;
2833 	    break;
2834 
2835 	default:
2836 	    device_printf(parent, "LSI v2 unknown RAID type 0x%02x\n",
2837 			  meta->configs[raid_entry].raid.type);
2838 	    kfree(raidp[array], M_AR);
2839 	    raidp[array] = NULL;
2840 	    goto lsiv2_out;
2841 	}
2842 
2843 	raid->format = AR_F_LSIV2_RAID;
2844 	raid->generation = 0;
2845 	raid->total_disks = meta->configs[raid_entry].raid.disk_count;
2846 	raid->total_sectors = meta->configs[raid_entry].raid.total_sectors;
2847 	raid->heads = 255;
2848 	raid->sectors = 63;
2849 	raid->cylinders = raid->total_sectors / (63 * 255);
2850 	raid->offset_sectors = 0;
2851 	raid->rebuild_lba = 0;
2852 	raid->lun = array;
2853 
2854 	if (meta->configs[conf_entry].disk.device != LSIV2_D_NONE) {
2855 	    raid->disks[meta->disk_number].dev = parent;
2856 	    raid->disks[meta->disk_number].sectors =
2857 		meta->configs[conf_entry].disk.disk_sectors;
2858 	    raid->disks[meta->disk_number].flags =
2859 		(AR_DF_ONLINE | AR_DF_PRESENT | AR_DF_ASSIGNED);
2860 	    ars->raid[raid->volume] = raid;
2861 	    ars->disk_number[raid->volume] = meta->disk_number;
2862 	    retval = 1;
2863 	}
2864 	else
2865 	    raid->disks[meta->disk_number].flags &= ~AR_DF_ONLINE;
2866 
2867 	break;
2868     }
2869 
2870 lsiv2_out:
2871     kfree(meta, M_AR);
2872     return retval;
2873 }
2874 
2875 /* LSILogic V3 MegaRAID Metadata */
2876 static int
2877 ata_raid_lsiv3_read_meta(device_t dev, struct ar_softc **raidp)
2878 {
2879     struct ata_raid_subdisk *ars = device_get_softc(dev);
2880     device_t parent = device_get_parent(dev);
2881     struct lsiv3_raid_conf *meta;
2882     struct ar_softc *raid = NULL;
2883     u_int8_t checksum, *ptr;
2884     int array, entry, count, disk_number, retval = 0;
2885 
2886     meta = (struct lsiv3_raid_conf *)kmalloc(sizeof(struct lsiv3_raid_conf),
2887 	M_AR, M_WAITOK | M_ZERO);
2888 
2889     if (ata_raid_rw(parent, LSIV3_LBA(parent),
2890 		    meta, sizeof(struct lsiv3_raid_conf), ATA_R_READ)) {
2891 	if (testing || bootverbose)
2892 	    device_printf(parent, "LSI (v3) read metadata failed\n");
2893 	goto lsiv3_out;
2894     }
2895 
2896     /* check if this is a LSI RAID struct */
2897     if (strncmp(meta->lsi_id, LSIV3_MAGIC, strlen(LSIV3_MAGIC))) {
2898 	if (testing || bootverbose)
2899 	    device_printf(parent, "LSI (v3) check1 failed\n");
2900 	goto lsiv3_out;
2901     }
2902 
2903     /* check if the checksum is OK */
2904     for (checksum = 0, ptr = meta->lsi_id, count = 0; count < 512; count++)
2905 	checksum += *ptr++;
2906     if (checksum) {
2907 	if (testing || bootverbose)
2908 	    device_printf(parent, "LSI (v3) check2 failed\n");
2909 	goto lsiv3_out;
2910     }
2911 
2912     if (testing || bootverbose)
2913 	ata_raid_lsiv3_print_meta(meta);
2914 
2915     /* now convert LSI (v3) config meta into our generic form */
2916     for (array = 0, entry = 0; array < MAX_ARRAYS && entry < 8;) {
2917 	if (!raidp[array]) {
2918 	    raidp[array] =
2919 		(struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
2920 					  M_WAITOK | M_ZERO);
2921 	}
2922 	raid = raidp[array];
2923 	if (raid->format && (raid->format != AR_F_LSIV3_RAID)) {
2924 	    array++;
2925 	    continue;
2926 	}
2927 
2928 	if ((raid->format == AR_F_LSIV3_RAID) &&
2929 	    (raid->magic_0 != meta->timestamp)) {
2930 	    array++;
2931 	    continue;
2932 	}
2933 
2934 	switch (meta->raid[entry].total_disks) {
2935 	case 0:
2936 	    entry++;
2937 	    continue;
2938 	case 1:
2939 	    if (meta->raid[entry].device == meta->device) {
2940 		disk_number = 0;
2941 		break;
2942 	    }
2943 	    if (raid->format)
2944 		array++;
2945 	    entry++;
2946 	    continue;
2947 	case 2:
2948 	    disk_number = (meta->device & (LSIV3_D_DEVICE|LSIV3_D_CHANNEL))?1:0;
2949 	    break;
2950 	default:
2951 	    device_printf(parent, "lsiv3 > 2 disk support untested!!\n");
2952 	    disk_number = (meta->device & LSIV3_D_DEVICE ? 1 : 0) +
2953 			  (meta->device & LSIV3_D_CHANNEL ? 2 : 0);
2954 	    break;
2955 	}
2956 
2957 	switch (meta->raid[entry].type) {
2958 	case LSIV3_T_RAID0:
2959 	    raid->type = AR_T_RAID0;
2960 	    raid->width = meta->raid[entry].total_disks;
2961 	    break;
2962 
2963 	case LSIV3_T_RAID1:
2964 	    raid->type = AR_T_RAID1;
2965 	    raid->width = meta->raid[entry].array_width;
2966 	    break;
2967 
2968 	default:
2969 	    device_printf(parent, "LSI v3 unknown RAID type 0x%02x\n",
2970 			  meta->raid[entry].type);
2971 	    kfree(raidp[array], M_AR);
2972 	    raidp[array] = NULL;
2973 	    entry++;
2974 	    continue;
2975 	}
2976 
2977 	raid->magic_0 = meta->timestamp;
2978 	raid->format = AR_F_LSIV3_RAID;
2979 	raid->generation = 0;
2980 	raid->interleave = meta->raid[entry].stripe_pages * 8;
2981 	raid->total_disks = meta->raid[entry].total_disks;
2982 	raid->total_sectors = raid->width * meta->raid[entry].sectors;
2983 	raid->heads = 255;
2984 	raid->sectors = 63;
2985 	raid->cylinders = raid->total_sectors / (63 * 255);
2986 	raid->offset_sectors = meta->raid[entry].offset;
2987 	raid->rebuild_lba = 0;
2988 	raid->lun = array;
2989 
2990 	raid->disks[disk_number].dev = parent;
2991 	raid->disks[disk_number].sectors = raid->total_sectors / raid->width;
2992 	raid->disks[disk_number].flags =
2993 	    (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_ONLINE);
2994 	ars->raid[raid->volume] = raid;
2995 	ars->disk_number[raid->volume] = disk_number;
2996 	retval = 1;
2997 	entry++;
2998 	array++;
2999     }
3000 
3001 lsiv3_out:
3002     kfree(meta, M_AR);
3003     return retval;
3004 }
3005 
3006 /* nVidia MediaShield Metadata */
3007 static int
3008 ata_raid_nvidia_read_meta(device_t dev, struct ar_softc **raidp)
3009 {
3010     struct ata_raid_subdisk *ars = device_get_softc(dev);
3011     device_t parent = device_get_parent(dev);
3012     struct nvidia_raid_conf *meta;
3013     struct ar_softc *raid = NULL;
3014     u_int32_t checksum, *ptr;
3015     int array, count, retval = 0;
3016 
3017     meta = (struct nvidia_raid_conf *)kmalloc(sizeof(struct nvidia_raid_conf),
3018 	M_AR, M_WAITOK | M_ZERO);
3019 
3020     if (ata_raid_rw(parent, NVIDIA_LBA(parent),
3021 		    meta, sizeof(struct nvidia_raid_conf), ATA_R_READ)) {
3022 	if (testing || bootverbose)
3023 	    device_printf(parent, "nVidia read metadata failed\n");
3024 	goto nvidia_out;
3025     }
3026 
3027     /* check if this is a nVidia RAID struct */
3028     if (strncmp(meta->nvidia_id, NV_MAGIC, strlen(NV_MAGIC))) {
3029 	if (testing || bootverbose)
3030 	    device_printf(parent, "nVidia check1 failed\n");
3031 	goto nvidia_out;
3032     }
3033 
3034     /* check if the checksum is OK */
3035     for (checksum = 0, ptr = (u_int32_t*)meta, count = 0;
3036 	 count < meta->config_size; count++)
3037 	checksum += *ptr++;
3038     if (checksum) {
3039 	if (testing || bootverbose)
3040 	    device_printf(parent, "nVidia check2 failed\n");
3041 	goto nvidia_out;
3042     }
3043 
3044     if (testing || bootverbose)
3045 	ata_raid_nvidia_print_meta(meta);
3046 
3047     /* now convert nVidia meta into our generic form */
3048     for (array = 0; array < MAX_ARRAYS; array++) {
3049 	if (!raidp[array]) {
3050 	    raidp[array] =
3051 		(struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
3052 					  M_WAITOK | M_ZERO);
3053 	}
3054 	raid = raidp[array];
3055 	if (raid->format && (raid->format != AR_F_NVIDIA_RAID))
3056 	    continue;
3057 
3058 	if (raid->format == AR_F_NVIDIA_RAID &&
3059 	    ((raid->magic_0 != meta->magic_1) ||
3060 	     (raid->magic_1 != meta->magic_2))) {
3061 	    continue;
3062 	}
3063 
3064 	switch (meta->type) {
3065 	case NV_T_SPAN:
3066 	    raid->type = AR_T_SPAN;
3067 	    break;
3068 
3069 	case NV_T_RAID0:
3070 	    raid->type = AR_T_RAID0;
3071 	    break;
3072 
3073 	case NV_T_RAID1:
3074 	    raid->type = AR_T_RAID1;
3075 	    break;
3076 
3077 	case NV_T_RAID5:
3078 	    raid->type = AR_T_RAID5;
3079 	    break;
3080 
3081 	case NV_T_RAID01:
3082 	    raid->type = AR_T_RAID01;
3083 	    break;
3084 
3085 	default:
3086 	    device_printf(parent, "nVidia unknown RAID type 0x%02x\n",
3087 			  meta->type);
3088 	    kfree(raidp[array], M_AR);
3089 	    raidp[array] = NULL;
3090 	    goto nvidia_out;
3091 	}
3092 	raid->magic_0 = meta->magic_1;
3093 	raid->magic_1 = meta->magic_2;
3094 	raid->format = AR_F_NVIDIA_RAID;
3095 	raid->generation = 0;
3096 	raid->interleave = meta->stripe_sectors;
3097 	raid->width = meta->array_width;
3098 	raid->total_disks = meta->total_disks;
3099 	raid->total_sectors = meta->total_sectors;
3100 	raid->heads = 255;
3101 	raid->sectors = 63;
3102 	raid->cylinders = raid->total_sectors / (63 * 255);
3103 	raid->offset_sectors = 0;
3104 	raid->rebuild_lba = meta->rebuild_lba;
3105 	raid->lun = array;
3106 	raid->status = AR_S_READY;
3107 	if (meta->status & NV_S_DEGRADED)
3108 	    raid->status |= AR_S_DEGRADED;
3109 
3110 	raid->disks[meta->disk_number].dev = parent;
3111 	raid->disks[meta->disk_number].sectors =
3112 	    raid->total_sectors / raid->width;
3113 	raid->disks[meta->disk_number].flags =
3114 	    (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_ONLINE);
3115 	ars->raid[raid->volume] = raid;
3116 	ars->disk_number[raid->volume] = meta->disk_number;
3117 	retval = 1;
3118 	break;
3119     }
3120 
3121 nvidia_out:
3122     kfree(meta, M_AR);
3123     return retval;
3124 }
3125 
3126 /* Promise FastTrak Metadata */
3127 static int
3128 ata_raid_promise_read_meta(device_t dev, struct ar_softc **raidp, int native)
3129 {
3130     struct ata_raid_subdisk *ars = device_get_softc(dev);
3131     device_t parent = device_get_parent(dev);
3132     struct promise_raid_conf *meta;
3133     struct ar_softc *raid;
3134     u_int32_t checksum, *ptr;
3135     int array, count, disk, disksum = 0, retval = 0;
3136 
3137     meta = (struct promise_raid_conf *)
3138 	kmalloc(sizeof(struct promise_raid_conf), M_AR, M_WAITOK | M_ZERO);
3139 
3140     if (ata_raid_rw(parent, PROMISE_LBA(parent),
3141 		    meta, sizeof(struct promise_raid_conf), ATA_R_READ)) {
3142 	if (testing || bootverbose)
3143 	    device_printf(parent, "%s read metadata failed\n",
3144 			  native ? "FreeBSD" : "Promise");
3145 	goto promise_out;
3146     }
3147 
3148     /* check the signature */
3149     if (native) {
3150 	if (strncmp(meta->promise_id, ATA_MAGIC, strlen(ATA_MAGIC))) {
3151 	    if (testing || bootverbose)
3152 		device_printf(parent, "FreeBSD check1 failed\n");
3153 	    goto promise_out;
3154 	}
3155     }
3156     else {
3157 	if (strncmp(meta->promise_id, PR_MAGIC, strlen(PR_MAGIC))) {
3158 	    if (testing || bootverbose)
3159 		device_printf(parent, "Promise check1 failed\n");
3160 	    goto promise_out;
3161 	}
3162     }
3163 
3164     /* check if the checksum is OK */
3165     for (checksum = 0, ptr = (u_int32_t *)meta, count = 0; count < 511; count++)
3166 	checksum += *ptr++;
3167     if (checksum != *ptr) {
3168 	if (testing || bootverbose)
3169 	    device_printf(parent, "%s check2 failed\n",
3170 			  native ? "FreeBSD" : "Promise");
3171 	goto promise_out;
3172     }
3173 
3174     /* check on disk integrity status */
3175     if (meta->raid.integrity != PR_I_VALID) {
3176 	if (testing || bootverbose)
3177 	    device_printf(parent, "%s check3 failed\n",
3178 			  native ? "FreeBSD" : "Promise");
3179 	goto promise_out;
3180     }
3181 
3182     if (testing || bootverbose)
3183 	ata_raid_promise_print_meta(meta);
3184 
3185     /* now convert Promise metadata into our generic form */
3186     for (array = 0; array < MAX_ARRAYS; array++) {
3187 	if (!raidp[array]) {
3188 	    raidp[array] =
3189 		(struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
3190 					  M_WAITOK | M_ZERO);
3191 	}
3192 	raid = raidp[array];
3193 	if (raid->format &&
3194 	    (raid->format != (native ? AR_F_FREEBSD_RAID : AR_F_PROMISE_RAID)))
3195 	    continue;
3196 
3197 	if ((raid->format == (native ? AR_F_FREEBSD_RAID : AR_F_PROMISE_RAID))&&
3198 	    !(meta->raid.magic_1 == (raid->magic_1)))
3199 	    continue;
3200 
3201 	/* update our knowledge about the array config based on generation */
3202 	if (!meta->raid.generation || meta->raid.generation > raid->generation){
3203 	    switch (meta->raid.type) {
3204 	    case PR_T_SPAN:
3205 		raid->type = AR_T_SPAN;
3206 		break;
3207 
3208 	    case PR_T_JBOD:
3209 		raid->type = AR_T_JBOD;
3210 		break;
3211 
3212 	    case PR_T_RAID0:
3213 		raid->type = AR_T_RAID0;
3214 		break;
3215 
3216 	    case PR_T_RAID1:
3217 		raid->type = AR_T_RAID1;
3218 		if (meta->raid.array_width > 1)
3219 		    raid->type = AR_T_RAID01;
3220 		break;
3221 
3222 	    case PR_T_RAID5:
3223 		raid->type = AR_T_RAID5;
3224 		break;
3225 
3226 	    default:
3227 		device_printf(parent, "%s unknown RAID type 0x%02x\n",
3228 			      native ? "FreeBSD" : "Promise", meta->raid.type);
3229 		kfree(raidp[array], M_AR);
3230 		raidp[array] = NULL;
3231 		goto promise_out;
3232 	    }
3233 	    raid->magic_1 = meta->raid.magic_1;
3234 	    raid->format = (native ? AR_F_FREEBSD_RAID : AR_F_PROMISE_RAID);
3235 	    raid->generation = meta->raid.generation;
3236 	    raid->interleave = 1 << meta->raid.stripe_shift;
3237 	    raid->width = meta->raid.array_width;
3238 	    raid->total_disks = meta->raid.total_disks;
3239 	    raid->heads = meta->raid.heads + 1;
3240 	    raid->sectors = meta->raid.sectors;
3241 	    raid->cylinders = meta->raid.cylinders + 1;
3242 	    raid->total_sectors = meta->raid.total_sectors;
3243 	    raid->offset_sectors = 0;
3244 	    raid->rebuild_lba = meta->raid.rebuild_lba;
3245 	    raid->lun = array;
3246 	    if ((meta->raid.status &
3247 		 (PR_S_VALID | PR_S_ONLINE | PR_S_INITED | PR_S_READY)) ==
3248 		(PR_S_VALID | PR_S_ONLINE | PR_S_INITED | PR_S_READY)) {
3249 		raid->status |= AR_S_READY;
3250 		if (meta->raid.status & PR_S_DEGRADED)
3251 		    raid->status |= AR_S_DEGRADED;
3252 	    }
3253 	    else
3254 		raid->status &= ~AR_S_READY;
3255 
3256 	    /* convert disk flags to our internal types */
3257 	    for (disk = 0; disk < meta->raid.total_disks; disk++) {
3258 		raid->disks[disk].dev = NULL;
3259 		raid->disks[disk].flags = 0;
3260 		*((u_int64_t *)(raid->disks[disk].serial)) =
3261 		    meta->raid.disk[disk].magic_0;
3262 		disksum += meta->raid.disk[disk].flags;
3263 		if (meta->raid.disk[disk].flags & PR_F_ONLINE)
3264 		    raid->disks[disk].flags |= AR_DF_ONLINE;
3265 		if (meta->raid.disk[disk].flags & PR_F_ASSIGNED)
3266 		    raid->disks[disk].flags |= AR_DF_ASSIGNED;
3267 		if (meta->raid.disk[disk].flags & PR_F_SPARE) {
3268 		    raid->disks[disk].flags &= ~(AR_DF_ONLINE | AR_DF_ASSIGNED);
3269 		    raid->disks[disk].flags |= AR_DF_SPARE;
3270 		}
3271 		if (meta->raid.disk[disk].flags & (PR_F_REDIR | PR_F_DOWN))
3272 		    raid->disks[disk].flags &= ~AR_DF_ONLINE;
3273 	    }
3274 	    if (!disksum) {
3275 		device_printf(parent, "%s subdisks has no flags\n",
3276 			      native ? "FreeBSD" : "Promise");
3277 		kfree(raidp[array], M_AR);
3278 		raidp[array] = NULL;
3279 		goto promise_out;
3280 	    }
3281 	}
3282 	if (meta->raid.generation >= raid->generation) {
3283 	    int disk_number = meta->raid.disk_number;
3284 
3285 	    if (raid->disks[disk_number].flags && (meta->magic_0 ==
3286 		*((u_int64_t *)(raid->disks[disk_number].serial)))) {
3287 		raid->disks[disk_number].dev = parent;
3288 		raid->disks[disk_number].flags |= AR_DF_PRESENT;
3289 		raid->disks[disk_number].sectors = meta->raid.disk_sectors;
3290 		if ((raid->disks[disk_number].flags &
3291 		    (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_ONLINE)) ==
3292 		    (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_ONLINE)) {
3293 		    ars->raid[raid->volume] = raid;
3294 		    ars->disk_number[raid->volume] = disk_number;
3295 		    retval = 1;
3296 		}
3297 	    }
3298 	}
3299 	break;
3300     }
3301 
3302 promise_out:
3303     kfree(meta, M_AR);
3304     return retval;
3305 }
3306 
3307 static int
3308 ata_raid_promise_write_meta(struct ar_softc *rdp)
3309 {
3310     struct promise_raid_conf *meta;
3311     struct timeval timestamp;
3312     u_int32_t *ckptr;
3313     int count, disk, drive, error = 0;
3314 
3315     meta = (struct promise_raid_conf *)
3316 	kmalloc(sizeof(struct promise_raid_conf), M_AR, M_WAITOK);
3317 
3318     rdp->generation++;
3319     microtime(&timestamp);
3320 
3321     for (disk = 0; disk < rdp->total_disks; disk++) {
3322 	for (count = 0; count < sizeof(struct promise_raid_conf); count++)
3323 	    *(((u_int8_t *)meta) + count) = 255 - (count % 256);
3324 	meta->dummy_0 = 0x00020000;
3325 	meta->raid.disk_number = disk;
3326 
3327 	if (rdp->disks[disk].dev) {
3328 	    struct ata_device *atadev = device_get_softc(rdp->disks[disk].dev);
3329 	    struct ata_channel *ch =
3330 		device_get_softc(device_get_parent(rdp->disks[disk].dev));
3331 
3332 	    meta->raid.channel = ch->unit;
3333 	    meta->raid.device = ATA_DEV(atadev->unit);
3334 	    meta->raid.disk_sectors = rdp->disks[disk].sectors;
3335 	    meta->raid.disk_offset = rdp->offset_sectors;
3336 	}
3337 	else {
3338 	    meta->raid.channel = 0;
3339 	    meta->raid.device = 0;
3340 	    meta->raid.disk_sectors = 0;
3341 	    meta->raid.disk_offset = 0;
3342 	}
3343 	meta->magic_0 = PR_MAGIC0(meta->raid) | timestamp.tv_sec;
3344 	meta->magic_1 = timestamp.tv_sec >> 16;
3345 	meta->magic_2 = timestamp.tv_sec;
3346 	meta->raid.integrity = PR_I_VALID;
3347 	meta->raid.magic_0 = meta->magic_0;
3348 	meta->raid.rebuild_lba = rdp->rebuild_lba;
3349 	meta->raid.generation = rdp->generation;
3350 
3351 	if (rdp->status & AR_S_READY) {
3352 	    meta->raid.flags = (PR_F_VALID | PR_F_ASSIGNED | PR_F_ONLINE);
3353 	    meta->raid.status =
3354 		(PR_S_VALID | PR_S_ONLINE | PR_S_INITED | PR_S_READY);
3355 	    if (rdp->status & AR_S_DEGRADED)
3356 		meta->raid.status |= PR_S_DEGRADED;
3357 	    else
3358 		meta->raid.status |= PR_S_FUNCTIONAL;
3359 	}
3360 	else {
3361 	    meta->raid.flags = PR_F_DOWN;
3362 	    meta->raid.status = 0;
3363 	}
3364 
3365 	switch (rdp->type) {
3366 	case AR_T_RAID0:
3367 	    meta->raid.type = PR_T_RAID0;
3368 	    break;
3369 	case AR_T_RAID1:
3370 	    meta->raid.type = PR_T_RAID1;
3371 	    break;
3372 	case AR_T_RAID01:
3373 	    meta->raid.type = PR_T_RAID1;
3374 	    break;
3375 	case AR_T_RAID5:
3376 	    meta->raid.type = PR_T_RAID5;
3377 	    break;
3378 	case AR_T_SPAN:
3379 	    meta->raid.type = PR_T_SPAN;
3380 	    break;
3381 	case AR_T_JBOD:
3382 	    meta->raid.type = PR_T_JBOD;
3383 	    break;
3384 	default:
3385 	    kfree(meta, M_AR);
3386 	    return ENODEV;
3387 	}
3388 
3389 	meta->raid.total_disks = rdp->total_disks;
3390 	meta->raid.stripe_shift = ffs(rdp->interleave) - 1;
3391 	meta->raid.array_width = rdp->width;
3392 	meta->raid.array_number = rdp->lun;
3393 	meta->raid.total_sectors = rdp->total_sectors;
3394 	meta->raid.cylinders = rdp->cylinders - 1;
3395 	meta->raid.heads = rdp->heads - 1;
3396 	meta->raid.sectors = rdp->sectors;
3397 	meta->raid.magic_1 = (u_int64_t)meta->magic_2<<16 | meta->magic_1;
3398 
3399 	bzero(&meta->raid.disk, 8 * 12);
3400 	for (drive = 0; drive < rdp->total_disks; drive++) {
3401 	    meta->raid.disk[drive].flags = 0;
3402 	    if (rdp->disks[drive].flags & AR_DF_PRESENT)
3403 		meta->raid.disk[drive].flags |= PR_F_VALID;
3404 	    if (rdp->disks[drive].flags & AR_DF_ASSIGNED)
3405 		meta->raid.disk[drive].flags |= PR_F_ASSIGNED;
3406 	    if (rdp->disks[drive].flags & AR_DF_ONLINE)
3407 		meta->raid.disk[drive].flags |= PR_F_ONLINE;
3408 	    else
3409 		if (rdp->disks[drive].flags & AR_DF_PRESENT)
3410 		    meta->raid.disk[drive].flags = (PR_F_REDIR | PR_F_DOWN);
3411 	    if (rdp->disks[drive].flags & AR_DF_SPARE)
3412 		meta->raid.disk[drive].flags |= PR_F_SPARE;
3413 	    meta->raid.disk[drive].dummy_0 = 0x0;
3414 	    if (rdp->disks[drive].dev) {
3415 		struct ata_channel *ch =
3416 		    device_get_softc(device_get_parent(rdp->disks[drive].dev));
3417 		struct ata_device *atadev =
3418 		    device_get_softc(rdp->disks[drive].dev);
3419 
3420 		meta->raid.disk[drive].channel = ch->unit;
3421 		meta->raid.disk[drive].device = ATA_DEV(atadev->unit);
3422 	    }
3423 	    meta->raid.disk[drive].magic_0 =
3424 		PR_MAGIC0(meta->raid.disk[drive]) | timestamp.tv_sec;
3425 	}
3426 
3427 	if (rdp->disks[disk].dev) {
3428 	    if ((rdp->disks[disk].flags & (AR_DF_PRESENT | AR_DF_ONLINE)) ==
3429 		(AR_DF_PRESENT | AR_DF_ONLINE)) {
3430 		if (rdp->format == AR_F_FREEBSD_RAID)
3431 		    bcopy(ATA_MAGIC, meta->promise_id, sizeof(ATA_MAGIC));
3432 		else
3433 		    bcopy(PR_MAGIC, meta->promise_id, sizeof(PR_MAGIC));
3434 	    }
3435 	    else
3436 		bzero(meta->promise_id, sizeof(meta->promise_id));
3437 	    meta->checksum = 0;
3438 	    for (ckptr = (int32_t *)meta, count = 0; count < 511; count++)
3439 		meta->checksum += *ckptr++;
3440 	    if (testing || bootverbose)
3441 		ata_raid_promise_print_meta(meta);
3442 	    if (ata_raid_rw(rdp->disks[disk].dev,
3443 			    PROMISE_LBA(rdp->disks[disk].dev),
3444 			    meta, sizeof(struct promise_raid_conf),
3445 			    ATA_R_WRITE | ATA_R_DIRECT)) {
3446 		device_printf(rdp->disks[disk].dev, "write metadata failed\n");
3447 		error = EIO;
3448 	    }
3449 	}
3450     }
3451     kfree(meta, M_AR);
3452     return error;
3453 }
3454 
3455 /* Silicon Image Medley Metadata */
3456 static int
3457 ata_raid_sii_read_meta(device_t dev, struct ar_softc **raidp)
3458 {
3459     struct ata_raid_subdisk *ars = device_get_softc(dev);
3460     device_t parent = device_get_parent(dev);
3461     struct sii_raid_conf *meta;
3462     struct ar_softc *raid = NULL;
3463     u_int16_t checksum, *ptr;
3464     int array, count, disk, retval = 0;
3465 
3466     meta = (struct sii_raid_conf *)kmalloc(sizeof(struct sii_raid_conf), M_AR,
3467 	M_WAITOK | M_ZERO);
3468 
3469     if (ata_raid_rw(parent, SII_LBA(parent),
3470 		    meta, sizeof(struct sii_raid_conf), ATA_R_READ)) {
3471 	if (testing || bootverbose)
3472 	    device_printf(parent, "Silicon Image read metadata failed\n");
3473 	goto sii_out;
3474     }
3475 
3476     /* check if this is a Silicon Image (Medley) RAID struct */
3477     for (checksum = 0, ptr = (u_int16_t *)meta, count = 0; count < 160; count++)
3478 	checksum += *ptr++;
3479     if (checksum) {
3480 	if (testing || bootverbose)
3481 	    device_printf(parent, "Silicon Image check1 failed\n");
3482 	goto sii_out;
3483     }
3484 
3485     for (checksum = 0, ptr = (u_int16_t *)meta, count = 0; count < 256; count++)
3486 	checksum += *ptr++;
3487     if (checksum != meta->checksum_1) {
3488 	if (testing || bootverbose)
3489 	    device_printf(parent, "Silicon Image check2 failed\n");
3490 	goto sii_out;
3491     }
3492 
3493     /* check verison */
3494     if (meta->version_major != 0x0002 ||
3495 	(meta->version_minor != 0x0000 && meta->version_minor != 0x0001)) {
3496 	if (testing || bootverbose)
3497 	    device_printf(parent, "Silicon Image check3 failed\n");
3498 	goto sii_out;
3499     }
3500 
3501     if (testing || bootverbose)
3502 	ata_raid_sii_print_meta(meta);
3503 
3504     /* now convert Silicon Image meta into our generic form */
3505     for (array = 0; array < MAX_ARRAYS; array++) {
3506 	if (!raidp[array]) {
3507 	    raidp[array] =
3508 		(struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
3509 					  M_WAITOK | M_ZERO);
3510 	}
3511 	raid = raidp[array];
3512 	if (raid->format && (raid->format != AR_F_SII_RAID))
3513 	    continue;
3514 
3515 	if (raid->format == AR_F_SII_RAID &&
3516 	    (raid->magic_0 != *((u_int64_t *)meta->timestamp))) {
3517 	    continue;
3518 	}
3519 
3520 	/* update our knowledge about the array config based on generation */
3521 	if (!meta->generation || meta->generation > raid->generation) {
3522 	    switch (meta->type) {
3523 	    case SII_T_RAID0:
3524 		raid->type = AR_T_RAID0;
3525 		break;
3526 
3527 	    case SII_T_RAID1:
3528 		raid->type = AR_T_RAID1;
3529 		break;
3530 
3531 	    case SII_T_RAID01:
3532 		raid->type = AR_T_RAID01;
3533 		break;
3534 
3535 	    case SII_T_SPARE:
3536 		device_printf(parent, "Silicon Image SPARE disk\n");
3537 		kfree(raidp[array], M_AR);
3538 		raidp[array] = NULL;
3539 		goto sii_out;
3540 
3541 	    default:
3542 		device_printf(parent,"Silicon Image unknown RAID type 0x%02x\n",
3543 			      meta->type);
3544 		kfree(raidp[array], M_AR);
3545 		raidp[array] = NULL;
3546 		goto sii_out;
3547 	    }
3548 	    raid->magic_0 = *((u_int64_t *)meta->timestamp);
3549 	    raid->format = AR_F_SII_RAID;
3550 	    raid->generation = meta->generation;
3551 	    raid->interleave = meta->stripe_sectors;
3552 	    raid->width = (meta->raid0_disks != 0xff) ? meta->raid0_disks : 1;
3553 	    raid->total_disks =
3554 		((meta->raid0_disks != 0xff) ? meta->raid0_disks : 0) +
3555 		((meta->raid1_disks != 0xff) ? meta->raid1_disks : 0);
3556 	    raid->total_sectors = meta->total_sectors;
3557 	    raid->heads = 255;
3558 	    raid->sectors = 63;
3559 	    raid->cylinders = raid->total_sectors / (63 * 255);
3560 	    raid->offset_sectors = 0;
3561 	    raid->rebuild_lba = meta->rebuild_lba;
3562 	    raid->lun = array;
3563 	    strncpy(raid->name, meta->name,
3564 		    min(sizeof(raid->name), sizeof(meta->name)));
3565 
3566 	    /* clear out any old info */
3567 	    if (raid->generation) {
3568 		for (disk = 0; disk < raid->total_disks; disk++) {
3569 		    raid->disks[disk].dev = NULL;
3570 		    raid->disks[disk].flags = 0;
3571 		}
3572 	    }
3573 	}
3574 	if (meta->generation >= raid->generation) {
3575 	    /* XXX SOS add check for the right physical disk by serial# */
3576 	    if (meta->status & SII_S_READY) {
3577 		int disk_number = (raid->type == AR_T_RAID01) ?
3578 		    meta->raid1_ident + (meta->raid0_ident << 1) :
3579 		    meta->disk_number;
3580 
3581 		raid->disks[disk_number].dev = parent;
3582 		raid->disks[disk_number].sectors =
3583 		    raid->total_sectors / raid->width;
3584 		raid->disks[disk_number].flags =
3585 		    (AR_DF_ONLINE | AR_DF_PRESENT | AR_DF_ASSIGNED);
3586 		ars->raid[raid->volume] = raid;
3587 		ars->disk_number[raid->volume] = disk_number;
3588 		retval = 1;
3589 	    }
3590 	}
3591 	break;
3592     }
3593 
3594 sii_out:
3595     kfree(meta, M_AR);
3596     return retval;
3597 }
3598 
3599 /* Silicon Integrated Systems Metadata */
3600 static int
3601 ata_raid_sis_read_meta(device_t dev, struct ar_softc **raidp)
3602 {
3603     struct ata_raid_subdisk *ars = device_get_softc(dev);
3604     device_t parent = device_get_parent(dev);
3605     struct sis_raid_conf *meta;
3606     struct ar_softc *raid = NULL;
3607     int array, disk_number, drive, retval = 0;
3608 
3609     meta = (struct sis_raid_conf *)kmalloc(sizeof(struct sis_raid_conf), M_AR,
3610 	M_WAITOK | M_ZERO);
3611 
3612     if (ata_raid_rw(parent, SIS_LBA(parent),
3613 		    meta, sizeof(struct sis_raid_conf), ATA_R_READ)) {
3614 	if (testing || bootverbose)
3615 	    device_printf(parent,
3616 			  "Silicon Integrated Systems read metadata failed\n");
3617     }
3618 
3619     /* check for SiS magic */
3620     if (meta->magic != SIS_MAGIC) {
3621 	if (testing || bootverbose)
3622 	    device_printf(parent,
3623 			  "Silicon Integrated Systems check1 failed\n");
3624 	goto sis_out;
3625     }
3626 
3627     if (testing || bootverbose)
3628 	ata_raid_sis_print_meta(meta);
3629 
3630     /* now convert SiS meta into our generic form */
3631     for (array = 0; array < MAX_ARRAYS; array++) {
3632 	if (!raidp[array]) {
3633 	    raidp[array] =
3634 		(struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
3635 					  M_WAITOK | M_ZERO);
3636 	}
3637 
3638 	raid = raidp[array];
3639 	if (raid->format && (raid->format != AR_F_SIS_RAID))
3640 	    continue;
3641 
3642 	if ((raid->format == AR_F_SIS_RAID) &&
3643 	    ((raid->magic_0 != meta->controller_pci_id) ||
3644 	     (raid->magic_1 != meta->timestamp))) {
3645 	    continue;
3646 	}
3647 
3648 	switch (meta->type_total_disks & SIS_T_MASK) {
3649 	case SIS_T_JBOD:
3650 	    raid->type = AR_T_JBOD;
3651 	    raid->width = (meta->type_total_disks & SIS_D_MASK);
3652 	    raid->total_sectors += SIS_LBA(parent);
3653 	    break;
3654 
3655 	case SIS_T_RAID0:
3656 	    raid->type = AR_T_RAID0;
3657 	    raid->width = (meta->type_total_disks & SIS_D_MASK);
3658 	    if (!raid->total_sectors ||
3659 		(raid->total_sectors > (raid->width * SIS_LBA(parent))))
3660 		raid->total_sectors = raid->width * SIS_LBA(parent);
3661 	    break;
3662 
3663 	case SIS_T_RAID1:
3664 	    raid->type = AR_T_RAID1;
3665 	    raid->width = 1;
3666 	    if (!raid->total_sectors || (raid->total_sectors > SIS_LBA(parent)))
3667 		raid->total_sectors = SIS_LBA(parent);
3668 	    break;
3669 
3670 	default:
3671 	    device_printf(parent, "Silicon Integrated Systems "
3672 			  "unknown RAID type 0x%08x\n", meta->magic);
3673 	    kfree(raidp[array], M_AR);
3674 	    raidp[array] = NULL;
3675 	    goto sis_out;
3676 	}
3677 	raid->magic_0 = meta->controller_pci_id;
3678 	raid->magic_1 = meta->timestamp;
3679 	raid->format = AR_F_SIS_RAID;
3680 	raid->generation = 0;
3681 	raid->interleave = meta->stripe_sectors;
3682 	raid->total_disks = (meta->type_total_disks & SIS_D_MASK);
3683 	raid->heads = 255;
3684 	raid->sectors = 63;
3685 	raid->cylinders = raid->total_sectors / (63 * 255);
3686 	raid->offset_sectors = 0;
3687 	raid->rebuild_lba = 0;
3688 	raid->lun = array;
3689 	/* XXX SOS if total_disks > 2 this doesn't float */
3690 	if (((meta->disks & SIS_D_MASTER) >> 4) == meta->disk_number)
3691 	    disk_number = 0;
3692 	else
3693 	    disk_number = 1;
3694 
3695 	for (drive = 0; drive < raid->total_disks; drive++) {
3696 	    raid->disks[drive].sectors = raid->total_sectors/raid->width;
3697 	    if (drive == disk_number) {
3698 		raid->disks[disk_number].dev = parent;
3699 		raid->disks[disk_number].flags =
3700 		    (AR_DF_ONLINE | AR_DF_PRESENT | AR_DF_ASSIGNED);
3701 		ars->raid[raid->volume] = raid;
3702 		ars->disk_number[raid->volume] = disk_number;
3703 	    }
3704 	}
3705 	retval = 1;
3706 	break;
3707     }
3708 
3709 sis_out:
3710     kfree(meta, M_AR);
3711     return retval;
3712 }
3713 
3714 static int
3715 ata_raid_sis_write_meta(struct ar_softc *rdp)
3716 {
3717     struct sis_raid_conf *meta;
3718     struct timeval timestamp;
3719     int disk, error = 0;
3720 
3721     meta = (struct sis_raid_conf *)kmalloc(sizeof(struct sis_raid_conf), M_AR,
3722 	M_WAITOK | M_ZERO);
3723 
3724     rdp->generation++;
3725     microtime(&timestamp);
3726 
3727     meta->magic = SIS_MAGIC;
3728     /* XXX SOS if total_disks > 2 this doesn't float */
3729     for (disk = 0; disk < rdp->total_disks; disk++) {
3730 	if (rdp->disks[disk].dev) {
3731 	    struct ata_channel *ch =
3732 		device_get_softc(device_get_parent(rdp->disks[disk].dev));
3733 	    struct ata_device *atadev = device_get_softc(rdp->disks[disk].dev);
3734 	    int disk_number = 1 + ATA_DEV(atadev->unit) + (ch->unit << 1);
3735 
3736 	    meta->disks |= disk_number << ((1 - disk) << 2);
3737 	}
3738     }
3739     switch (rdp->type) {
3740     case AR_T_JBOD:
3741 	meta->type_total_disks = SIS_T_JBOD;
3742 	break;
3743 
3744     case AR_T_RAID0:
3745 	meta->type_total_disks = SIS_T_RAID0;
3746 	break;
3747 
3748     case AR_T_RAID1:
3749 	meta->type_total_disks = SIS_T_RAID1;
3750 	break;
3751 
3752     default:
3753 	kfree(meta, M_AR);
3754 	return ENODEV;
3755     }
3756     meta->type_total_disks |= (rdp->total_disks & SIS_D_MASK);
3757     meta->stripe_sectors = rdp->interleave;
3758     meta->timestamp = timestamp.tv_sec;
3759 
3760     for (disk = 0; disk < rdp->total_disks; disk++) {
3761 	if (rdp->disks[disk].dev) {
3762 	    struct ata_channel *ch =
3763 		device_get_softc(device_get_parent(rdp->disks[disk].dev));
3764 	    struct ata_device *atadev = device_get_softc(rdp->disks[disk].dev);
3765 
3766 	    meta->controller_pci_id =
3767 		(pci_get_vendor(GRANDPARENT(rdp->disks[disk].dev)) << 16) |
3768 		pci_get_device(GRANDPARENT(rdp->disks[disk].dev));
3769 	    bcopy(atadev->param.model, meta->model, sizeof(meta->model));
3770 
3771 	    /* XXX SOS if total_disks > 2 this may not float */
3772 	    meta->disk_number = 1 + ATA_DEV(atadev->unit) + (ch->unit << 1);
3773 
3774 	    if (testing || bootverbose)
3775 		ata_raid_sis_print_meta(meta);
3776 
3777 	    if (ata_raid_rw(rdp->disks[disk].dev,
3778 			    SIS_LBA(rdp->disks[disk].dev),
3779 			    meta, sizeof(struct sis_raid_conf),
3780 			    ATA_R_WRITE | ATA_R_DIRECT)) {
3781 		device_printf(rdp->disks[disk].dev, "write metadata failed\n");
3782 		error = EIO;
3783 	    }
3784 	}
3785     }
3786     kfree(meta, M_AR);
3787     return error;
3788 }
3789 
3790 /* VIA Tech V-RAID Metadata */
3791 static int
3792 ata_raid_via_read_meta(device_t dev, struct ar_softc **raidp)
3793 {
3794     struct ata_raid_subdisk *ars = device_get_softc(dev);
3795     device_t parent = device_get_parent(dev);
3796     struct via_raid_conf *meta;
3797     struct ar_softc *raid = NULL;
3798     u_int8_t checksum, *ptr;
3799     int array, count, disk, retval = 0;
3800 
3801     meta = (struct via_raid_conf *)kmalloc(sizeof(struct via_raid_conf), M_AR,
3802 	M_WAITOK | M_ZERO);
3803 
3804     if (ata_raid_rw(parent, VIA_LBA(parent),
3805 		    meta, sizeof(struct via_raid_conf), ATA_R_READ)) {
3806 	if (testing || bootverbose)
3807 	    device_printf(parent, "VIA read metadata failed\n");
3808 	goto via_out;
3809     }
3810 
3811     /* check if this is a VIA RAID struct */
3812     if (meta->magic != VIA_MAGIC) {
3813 	if (testing || bootverbose)
3814 	    device_printf(parent, "VIA check1 failed\n");
3815 	goto via_out;
3816     }
3817 
3818     /* calculate checksum and compare for valid */
3819     for (checksum = 0, ptr = (u_int8_t *)meta, count = 0; count < 50; count++)
3820 	checksum += *ptr++;
3821     if (checksum != meta->checksum) {
3822 	if (testing || bootverbose)
3823 	    device_printf(parent, "VIA check2 failed\n");
3824 	goto via_out;
3825     }
3826 
3827     if (testing || bootverbose)
3828 	ata_raid_via_print_meta(meta);
3829 
3830     /* now convert VIA meta into our generic form */
3831     for (array = 0; array < MAX_ARRAYS; array++) {
3832 	if (!raidp[array]) {
3833 	    raidp[array] =
3834 		(struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
3835 					  M_WAITOK | M_ZERO);
3836 	}
3837 	raid = raidp[array];
3838 	if (raid->format && (raid->format != AR_F_VIA_RAID))
3839 	    continue;
3840 
3841 	if (raid->format == AR_F_VIA_RAID && (raid->magic_0 != meta->disks[0]))
3842 	    continue;
3843 
3844 	switch (meta->type & VIA_T_MASK) {
3845 	case VIA_T_RAID0:
3846 	    raid->type = AR_T_RAID0;
3847 	    raid->width = meta->stripe_layout & VIA_L_DISKS;
3848 	    if (!raid->total_sectors ||
3849 		(raid->total_sectors > (raid->width * meta->disk_sectors)))
3850 		raid->total_sectors = raid->width * meta->disk_sectors;
3851 	    break;
3852 
3853 	case VIA_T_RAID1:
3854 	    raid->type = AR_T_RAID1;
3855 	    raid->width = 1;
3856 	    raid->total_sectors = meta->disk_sectors;
3857 	    break;
3858 
3859 	case VIA_T_RAID01:
3860 	    raid->type = AR_T_RAID01;
3861 	    raid->width = meta->stripe_layout & VIA_L_DISKS;
3862 	    if (!raid->total_sectors ||
3863 		(raid->total_sectors > (raid->width * meta->disk_sectors)))
3864 		raid->total_sectors = raid->width * meta->disk_sectors;
3865 	    break;
3866 
3867 	case VIA_T_RAID5:
3868 	    raid->type = AR_T_RAID5;
3869 	    raid->width = meta->stripe_layout & VIA_L_DISKS;
3870 	    if (!raid->total_sectors ||
3871 		(raid->total_sectors > ((raid->width - 1)*meta->disk_sectors)))
3872 		raid->total_sectors = (raid->width - 1) * meta->disk_sectors;
3873 	    break;
3874 
3875 	case VIA_T_SPAN:
3876 	    raid->type = AR_T_SPAN;
3877 	    raid->width = 1;
3878 	    raid->total_sectors += meta->disk_sectors;
3879 	    break;
3880 
3881 	default:
3882 	    device_printf(parent,"VIA unknown RAID type 0x%02x\n", meta->type);
3883 	    kfree(raidp[array], M_AR);
3884 	    raidp[array] = NULL;
3885 	    goto via_out;
3886 	}
3887 	raid->magic_0 = meta->disks[0];
3888 	raid->format = AR_F_VIA_RAID;
3889 	raid->generation = 0;
3890 	raid->interleave =
3891 	    0x08 << ((meta->stripe_layout & VIA_L_MASK) >> VIA_L_SHIFT);
3892 	for (count = 0, disk = 0; disk < 8; disk++)
3893 	    if (meta->disks[disk])
3894 		count++;
3895 	raid->total_disks = count;
3896 	raid->heads = 255;
3897 	raid->sectors = 63;
3898 	raid->cylinders = raid->total_sectors / (63 * 255);
3899 	raid->offset_sectors = 0;
3900 	raid->rebuild_lba = 0;
3901 	raid->lun = array;
3902 
3903 	for (disk = 0; disk < raid->total_disks; disk++) {
3904 	    if (meta->disks[disk] == meta->disk_id) {
3905 		raid->disks[disk].dev = parent;
3906 		bcopy(&meta->disk_id, raid->disks[disk].serial,
3907 		      sizeof(u_int32_t));
3908 		raid->disks[disk].sectors = meta->disk_sectors;
3909 		raid->disks[disk].flags =
3910 		    (AR_DF_ONLINE | AR_DF_PRESENT | AR_DF_ASSIGNED);
3911 		ars->raid[raid->volume] = raid;
3912 		ars->disk_number[raid->volume] = disk;
3913 		retval = 1;
3914 		break;
3915 	    }
3916 	}
3917 	break;
3918     }
3919 
3920 via_out:
3921     kfree(meta, M_AR);
3922     return retval;
3923 }
3924 
3925 static int
3926 ata_raid_via_write_meta(struct ar_softc *rdp)
3927 {
3928     struct via_raid_conf *meta;
3929     int disk, error = 0;
3930 
3931     meta = (struct via_raid_conf *)kmalloc(sizeof(struct via_raid_conf), M_AR,
3932 	M_WAITOK | M_ZERO);
3933 
3934     rdp->generation++;
3935 
3936     meta->magic = VIA_MAGIC;
3937     meta->dummy_0 = 0x02;
3938     switch (rdp->type) {
3939     case AR_T_SPAN:
3940 	meta->type = VIA_T_SPAN;
3941 	meta->stripe_layout = (rdp->total_disks & VIA_L_DISKS);
3942 	break;
3943 
3944     case AR_T_RAID0:
3945 	meta->type = VIA_T_RAID0;
3946 	meta->stripe_layout = ((rdp->interleave >> 1) & VIA_L_MASK);
3947 	meta->stripe_layout |= (rdp->total_disks & VIA_L_DISKS);
3948 	break;
3949 
3950     case AR_T_RAID1:
3951 	meta->type = VIA_T_RAID1;
3952 	meta->stripe_layout = (rdp->total_disks & VIA_L_DISKS);
3953 	break;
3954 
3955     case AR_T_RAID5:
3956 	meta->type = VIA_T_RAID5;
3957 	meta->stripe_layout = ((rdp->interleave >> 1) & VIA_L_MASK);
3958 	meta->stripe_layout |= (rdp->total_disks & VIA_L_DISKS);
3959 	break;
3960 
3961     case AR_T_RAID01:
3962 	meta->type = VIA_T_RAID01;
3963 	meta->stripe_layout = ((rdp->interleave >> 1) & VIA_L_MASK);
3964 	meta->stripe_layout |= (rdp->width & VIA_L_DISKS);
3965 	break;
3966 
3967     default:
3968 	kfree(meta, M_AR);
3969 	return ENODEV;
3970     }
3971     meta->type |= VIA_T_BOOTABLE;       /* XXX SOS */
3972     meta->disk_sectors =
3973 	rdp->total_sectors / (rdp->width - (rdp->type == AR_RAID5));
3974     for (disk = 0; disk < rdp->total_disks; disk++)
3975 	meta->disks[disk] = (u_int32_t)(uintptr_t)rdp->disks[disk].dev;
3976 
3977     for (disk = 0; disk < rdp->total_disks; disk++) {
3978 	if (rdp->disks[disk].dev) {
3979 	    u_int8_t *ptr;
3980 	    int count;
3981 
3982 	    meta->disk_index = disk * sizeof(u_int32_t);
3983 	    if (rdp->type == AR_T_RAID01)
3984 		meta->disk_index = ((meta->disk_index & 0x08) << 2) |
3985 				   (meta->disk_index & ~0x08);
3986 	    meta->disk_id = meta->disks[disk];
3987 	    meta->checksum = 0;
3988 	    for (ptr = (u_int8_t *)meta, count = 0; count < 50; count++)
3989 		meta->checksum += *ptr++;
3990 
3991 	    if (testing || bootverbose)
3992 		ata_raid_via_print_meta(meta);
3993 
3994 	    if (ata_raid_rw(rdp->disks[disk].dev,
3995 			    VIA_LBA(rdp->disks[disk].dev),
3996 			    meta, sizeof(struct via_raid_conf),
3997 			    ATA_R_WRITE | ATA_R_DIRECT)) {
3998 		device_printf(rdp->disks[disk].dev, "write metadata failed\n");
3999 		error = EIO;
4000 	    }
4001 	}
4002     }
4003     kfree(meta, M_AR);
4004     return error;
4005 }
4006 
4007 static struct ata_request *
4008 ata_raid_init_request(struct ar_softc *rdp, struct bio *bio)
4009 {
4010     struct ata_request *request;
4011 
4012     if (!(request = ata_alloc_request())) {
4013 	kprintf("FAILURE - out of memory in ata_raid_init_request\n");
4014 	return NULL;
4015     }
4016     request->timeout = ATA_DEFAULT_TIMEOUT;
4017     request->retries = 2;
4018     request->callback = ata_raid_done;
4019     request->driver = rdp;
4020     request->bio = bio;
4021     switch (request->bio->bio_buf->b_cmd) {
4022     case BUF_CMD_READ:
4023 	request->flags = ATA_R_READ;
4024 	break;
4025     case BUF_CMD_WRITE:
4026 	request->flags = ATA_R_WRITE;
4027 	break;
4028     case BUF_CMD_FLUSH:
4029 	request->flags = ATA_R_CONTROL;
4030 	break;
4031     default:
4032 	kprintf("ar%d: FAILURE - unknown BUF operation\n", rdp->lun);
4033 	ata_free_request(request);
4034 #if 0
4035 	bio->bio_buf->b_flags |= B_ERROR;
4036 	bio->bio_buf->b_error = EIO;
4037 	biodone(bio);
4038 #endif /* 0 */
4039 	return(NULL);
4040     }
4041     return request;
4042 }
4043 
4044 static int
4045 ata_raid_send_request(struct ata_request *request)
4046 {
4047     struct ata_device *atadev = device_get_softc(request->dev);
4048 
4049     request->transfersize = min(request->bytecount, atadev->max_iosize);
4050     if (request->flags & ATA_R_READ) {
4051 	if (atadev->mode >= ATA_DMA) {
4052 	    request->flags |= ATA_R_DMA;
4053 	    request->u.ata.command = ATA_READ_DMA;
4054 	}
4055 	else if (atadev->max_iosize > DEV_BSIZE)
4056 	    request->u.ata.command = ATA_READ_MUL;
4057 	else
4058 	    request->u.ata.command = ATA_READ;
4059     }
4060     else if (request->flags & ATA_R_WRITE) {
4061 	if (atadev->mode >= ATA_DMA) {
4062 	    request->flags |= ATA_R_DMA;
4063 	    request->u.ata.command = ATA_WRITE_DMA;
4064 	}
4065 	else if (atadev->max_iosize > DEV_BSIZE)
4066 	    request->u.ata.command = ATA_WRITE_MUL;
4067 	else
4068 	    request->u.ata.command = ATA_WRITE;
4069     }
4070     else {
4071 	device_printf(request->dev, "FAILURE - unknown IO operation\n");
4072 	ata_free_request(request);
4073 	return EIO;
4074     }
4075     request->flags |= (ATA_R_ORDERED | ATA_R_THREAD);
4076     ata_queue_request(request);
4077     return 0;
4078 }
4079 
4080 static int
4081 ata_raid_rw(device_t dev, u_int64_t lba, void *data, u_int bcount, int flags)
4082 {
4083     struct ata_device *atadev = device_get_softc(dev);
4084     struct ata_request *request;
4085     int error;
4086 
4087     if (bcount % DEV_BSIZE) {
4088 	device_printf(dev, "FAILURE - transfers must be modulo sectorsize\n");
4089 	return ENOMEM;
4090     }
4091 
4092     if (!(request = ata_alloc_request())) {
4093 	device_printf(dev, "FAILURE - out of memory in ata_raid_rw\n");
4094 	return ENOMEM;
4095     }
4096 
4097     /* setup request */
4098     request->dev = dev;
4099     request->timeout = 10;
4100     request->retries = 0;
4101     request->data = data;
4102     request->bytecount = bcount;
4103     request->transfersize = DEV_BSIZE;
4104     request->u.ata.lba = lba;
4105     request->u.ata.count = request->bytecount / DEV_BSIZE;
4106     request->flags = flags;
4107 
4108     if (flags & ATA_R_READ) {
4109 	if (atadev->mode >= ATA_DMA) {
4110 	    request->u.ata.command = ATA_READ_DMA;
4111 	    request->flags |= ATA_R_DMA;
4112 	}
4113 	else
4114 	    request->u.ata.command = ATA_READ;
4115 	ata_queue_request(request);
4116     }
4117     else if (flags & ATA_R_WRITE) {
4118 	if (atadev->mode >= ATA_DMA) {
4119 	    request->u.ata.command = ATA_WRITE_DMA;
4120 	    request->flags |= ATA_R_DMA;
4121 	}
4122 	else
4123 	    request->u.ata.command = ATA_WRITE;
4124 	ata_queue_request(request);
4125     }
4126     else {
4127 	device_printf(dev, "FAILURE - unknown IO operation\n");
4128 	request->result = EIO;
4129     }
4130     error = request->result;
4131     ata_free_request(request);
4132     return error;
4133 }
4134 
4135 /*
4136  * module handeling
4137  */
4138 static int
4139 ata_raid_subdisk_probe(device_t dev)
4140 {
4141     device_quiet(dev);
4142     return 0;
4143 }
4144 
4145 static int
4146 ata_raid_subdisk_attach(device_t dev)
4147 {
4148     struct ata_raid_subdisk *ars = device_get_softc(dev);
4149     int volume;
4150 
4151     for (volume = 0; volume < MAX_VOLUMES; volume++) {
4152 	ars->raid[volume] = NULL;
4153 	ars->disk_number[volume] = -1;
4154     }
4155     ata_raid_read_metadata(dev);
4156     return 0;
4157 }
4158 
4159 static int
4160 ata_raid_subdisk_detach(device_t dev)
4161 {
4162     struct ata_raid_subdisk *ars = device_get_softc(dev);
4163     int volume;
4164 
4165     for (volume = 0; volume < MAX_VOLUMES; volume++) {
4166 	if (ars->raid[volume]) {
4167 	    ars->raid[volume]->disks[ars->disk_number[volume]].flags &=
4168 		~(AR_DF_PRESENT | AR_DF_ONLINE);
4169 	    ars->raid[volume]->disks[ars->disk_number[volume]].dev = NULL;
4170 	    ata_raid_config_changed(ars->raid[volume], 1);
4171 	    ars->raid[volume] = NULL;
4172 	    ars->disk_number[volume] = -1;
4173 	}
4174     }
4175     return 0;
4176 }
4177 
4178 static device_method_t ata_raid_sub_methods[] = {
4179     /* device interface */
4180     DEVMETHOD(device_probe,     ata_raid_subdisk_probe),
4181     DEVMETHOD(device_attach,    ata_raid_subdisk_attach),
4182     DEVMETHOD(device_detach,    ata_raid_subdisk_detach),
4183     { 0, 0 }
4184 };
4185 
4186 static driver_t ata_raid_sub_driver = {
4187     "subdisk",
4188     ata_raid_sub_methods,
4189     sizeof(struct ata_raid_subdisk)
4190 };
4191 
4192 DRIVER_MODULE(subdisk, ad, ata_raid_sub_driver, ata_raid_sub_devclass, NULL, NULL);
4193 
4194 static int
4195 ata_raid_module_event_handler(module_t mod, int what, void *arg)
4196 {
4197     int i;
4198 
4199     switch (what) {
4200     case MOD_LOAD:
4201 	if (testing || bootverbose)
4202 	    kprintf("ATA PseudoRAID loaded\n");
4203 #if 0
4204 	/* setup table to hold metadata for all ATA PseudoRAID arrays */
4205 	ata_raid_arrays = kmalloc(sizeof(struct ar_soft *) * MAX_ARRAYS,
4206 				M_AR, M_WAITOK | M_ZERO);
4207 #endif
4208 	/* attach found PseudoRAID arrays */
4209 	for (i = 0; i < MAX_ARRAYS; i++) {
4210 	    struct ar_softc *rdp = ata_raid_arrays[i];
4211 
4212 	    if (!rdp || !rdp->format)
4213 		continue;
4214 	    if (testing || bootverbose)
4215 		ata_raid_print_meta(rdp);
4216 	    ata_raid_attach(rdp, 0);
4217 	}
4218 	ata_raid_ioctl_func = ata_raid_ioctl;
4219 	return 0;
4220 
4221     case MOD_UNLOAD:
4222 	/* detach found PseudoRAID arrays */
4223 	for (i = 0; i < MAX_ARRAYS; i++) {
4224 	    struct ar_softc *rdp = ata_raid_arrays[i];
4225 
4226 	    if (!rdp || !rdp->status)
4227 		continue;
4228 	    disk_destroy(&rdp->disk);
4229 	}
4230 	if (testing || bootverbose)
4231 	    kprintf("ATA PseudoRAID unloaded\n");
4232 #if 0
4233 	kfree(ata_raid_arrays, M_AR);
4234 #endif
4235 	ata_raid_ioctl_func = NULL;
4236 	return 0;
4237 
4238     default:
4239 	return EOPNOTSUPP;
4240     }
4241 }
4242 
4243 static moduledata_t ata_raid_moduledata =
4244     { "ataraid", ata_raid_module_event_handler, NULL };
4245 DECLARE_MODULE(ata, ata_raid_moduledata, SI_SUB_RAID, SI_ORDER_FIRST);
4246 MODULE_VERSION(ataraid, 1);
4247 MODULE_DEPEND(ataraid, ata, 1, 1, 1);
4248 MODULE_DEPEND(ataraid, ad, 1, 1, 1);
4249 
4250 static char *
4251 ata_raid_format(struct ar_softc *rdp)
4252 {
4253     switch (rdp->format) {
4254     case AR_F_FREEBSD_RAID:     return "FreeBSD PseudoRAID";
4255     case AR_F_ADAPTEC_RAID:     return "Adaptec HostRAID";
4256     case AR_F_HPTV2_RAID:       return "HighPoint v2 RocketRAID";
4257     case AR_F_HPTV3_RAID:       return "HighPoint v3 RocketRAID";
4258     case AR_F_INTEL_RAID:       return "Intel MatrixRAID";
4259     case AR_F_ITE_RAID:         return "Integrated Technology Express";
4260     case AR_F_JMICRON_RAID:     return "JMicron Technology Corp";
4261     case AR_F_LSIV2_RAID:       return "LSILogic v2 MegaRAID";
4262     case AR_F_LSIV3_RAID:       return "LSILogic v3 MegaRAID";
4263     case AR_F_NVIDIA_RAID:      return "nVidia MediaShield";
4264     case AR_F_PROMISE_RAID:     return "Promise Fasttrak";
4265     case AR_F_SII_RAID:         return "Silicon Image Medley";
4266     case AR_F_SIS_RAID:         return "Silicon Integrated Systems";
4267     case AR_F_VIA_RAID:         return "VIA Tech V-RAID";
4268     default:                    return "UNKNOWN";
4269     }
4270 }
4271 
4272 static char *
4273 ata_raid_type(struct ar_softc *rdp)
4274 {
4275     switch (rdp->type) {
4276     case AR_T_JBOD:     return "JBOD";
4277     case AR_T_SPAN:     return "SPAN";
4278     case AR_T_RAID0:    return "RAID0";
4279     case AR_T_RAID1:    return "RAID1";
4280     case AR_T_RAID3:    return "RAID3";
4281     case AR_T_RAID4:    return "RAID4";
4282     case AR_T_RAID5:    return "RAID5";
4283     case AR_T_RAID01:   return "RAID0+1";
4284     default:            return "UNKNOWN";
4285     }
4286 }
4287 
4288 static char *
4289 ata_raid_flags(struct ar_softc *rdp)
4290 {
4291     switch (rdp->status & (AR_S_READY | AR_S_DEGRADED | AR_S_REBUILDING)) {
4292     case AR_S_READY:                                    return "READY";
4293     case AR_S_READY | AR_S_DEGRADED:                    return "DEGRADED";
4294     case AR_S_READY | AR_S_REBUILDING:
4295     case AR_S_READY | AR_S_DEGRADED | AR_S_REBUILDING:  return "REBUILDING";
4296     default:                                            return "BROKEN";
4297     }
4298 }
4299 
4300 /* debugging gunk */
4301 static void
4302 ata_raid_print_meta(struct ar_softc *raid)
4303 {
4304     int i;
4305 
4306     kprintf("********** ATA PseudoRAID ar%d Metadata **********\n", raid->lun);
4307     kprintf("=================================================\n");
4308     kprintf("format              %s\n", ata_raid_format(raid));
4309     kprintf("type                %s\n", ata_raid_type(raid));
4310     kprintf("flags               0x%02x %b\n", raid->status, raid->status,
4311 	   "\20\3REBUILDING\2DEGRADED\1READY\n");
4312     kprintf("magic_0             0x%016jx\n", raid->magic_0);
4313     kprintf("magic_1             0x%016jx\n",raid->magic_1);
4314     kprintf("generation          %u\n", raid->generation);
4315     kprintf("total_sectors       %ju\n", raid->total_sectors);
4316     kprintf("offset_sectors      %ju\n", raid->offset_sectors);
4317     kprintf("heads               %u\n", raid->heads);
4318     kprintf("sectors             %u\n", raid->sectors);
4319     kprintf("cylinders           %u\n", raid->cylinders);
4320     kprintf("width               %u\n", raid->width);
4321     kprintf("interleave          %u\n", raid->interleave);
4322     kprintf("total_disks         %u\n", raid->total_disks);
4323     for (i = 0; i < raid->total_disks; i++) {
4324 	kprintf("    disk %d:      flags = 0x%02x %b\n", i, raid->disks[i].flags,
4325 	       raid->disks[i].flags, "\20\4ONLINE\3SPARE\2ASSIGNED\1PRESENT\n");
4326 	if (raid->disks[i].dev) {
4327 	    kprintf("        ");
4328 	    device_printf(raid->disks[i].dev, " sectors %jd\n",
4329 			  raid->disks[i].sectors);
4330 	}
4331     }
4332     kprintf("=================================================\n");
4333 }
4334 
4335 static char *
4336 ata_raid_adaptec_type(int type)
4337 {
4338     static char buffer[16];
4339 
4340     switch (type) {
4341     case ADP_T_RAID0:   return "RAID0";
4342     case ADP_T_RAID1:   return "RAID1";
4343     default:            ksprintf(buffer, "UNKNOWN 0x%02x", type);
4344 			return buffer;
4345     }
4346 }
4347 
4348 static void
4349 ata_raid_adaptec_print_meta(struct adaptec_raid_conf *meta)
4350 {
4351     int i;
4352 
4353     kprintf("********* ATA Adaptec HostRAID Metadata *********\n");
4354     kprintf("magic_0             <0x%08x>\n", be32toh(meta->magic_0));
4355     kprintf("generation          0x%08x\n", be32toh(meta->generation));
4356     kprintf("dummy_0             0x%04x\n", be16toh(meta->dummy_0));
4357     kprintf("total_configs       %u\n", be16toh(meta->total_configs));
4358     kprintf("dummy_1             0x%04x\n", be16toh(meta->dummy_1));
4359     kprintf("checksum            0x%04x\n", be16toh(meta->checksum));
4360     kprintf("dummy_2             0x%08x\n", be32toh(meta->dummy_2));
4361     kprintf("dummy_3             0x%08x\n", be32toh(meta->dummy_3));
4362     kprintf("flags               0x%08x\n", be32toh(meta->flags));
4363     kprintf("timestamp           0x%08x\n", be32toh(meta->timestamp));
4364     kprintf("dummy_4             0x%08x 0x%08x 0x%08x 0x%08x\n",
4365 	   be32toh(meta->dummy_4[0]), be32toh(meta->dummy_4[1]),
4366 	   be32toh(meta->dummy_4[2]), be32toh(meta->dummy_4[3]));
4367     kprintf("dummy_5             0x%08x 0x%08x 0x%08x 0x%08x\n",
4368 	   be32toh(meta->dummy_5[0]), be32toh(meta->dummy_5[1]),
4369 	   be32toh(meta->dummy_5[2]), be32toh(meta->dummy_5[3]));
4370 
4371     for (i = 0; i < be16toh(meta->total_configs); i++) {
4372 	kprintf("    %d   total_disks  %u\n", i,
4373 	       be16toh(meta->configs[i].disk_number));
4374 	kprintf("    %d   generation   %u\n", i,
4375 	       be16toh(meta->configs[i].generation));
4376 	kprintf("    %d   magic_0      0x%08x\n", i,
4377 	       be32toh(meta->configs[i].magic_0));
4378 	kprintf("    %d   dummy_0      0x%02x\n", i, meta->configs[i].dummy_0);
4379 	kprintf("    %d   type         %s\n", i,
4380 	       ata_raid_adaptec_type(meta->configs[i].type));
4381 	kprintf("    %d   dummy_1      0x%02x\n", i, meta->configs[i].dummy_1);
4382 	kprintf("    %d   flags        %d\n", i,
4383 	       be32toh(meta->configs[i].flags));
4384 	kprintf("    %d   dummy_2      0x%02x\n", i, meta->configs[i].dummy_2);
4385 	kprintf("    %d   dummy_3      0x%02x\n", i, meta->configs[i].dummy_3);
4386 	kprintf("    %d   dummy_4      0x%02x\n", i, meta->configs[i].dummy_4);
4387 	kprintf("    %d   dummy_5      0x%02x\n", i, meta->configs[i].dummy_5);
4388 	kprintf("    %d   disk_number  %u\n", i,
4389 	       be32toh(meta->configs[i].disk_number));
4390 	kprintf("    %d   dummy_6      0x%08x\n", i,
4391 	       be32toh(meta->configs[i].dummy_6));
4392 	kprintf("    %d   sectors      %u\n", i,
4393 	       be32toh(meta->configs[i].sectors));
4394 	kprintf("    %d   stripe_shift %u\n", i,
4395 	       be16toh(meta->configs[i].stripe_shift));
4396 	kprintf("    %d   dummy_7      0x%08x\n", i,
4397 	       be32toh(meta->configs[i].dummy_7));
4398 	kprintf("    %d   dummy_8      0x%08x 0x%08x 0x%08x 0x%08x\n", i,
4399 	       be32toh(meta->configs[i].dummy_8[0]),
4400 	       be32toh(meta->configs[i].dummy_8[1]),
4401 	       be32toh(meta->configs[i].dummy_8[2]),
4402 	       be32toh(meta->configs[i].dummy_8[3]));
4403 	kprintf("    %d   name         <%s>\n", i, meta->configs[i].name);
4404     }
4405     kprintf("magic_1             <0x%08x>\n", be32toh(meta->magic_1));
4406     kprintf("magic_2             <0x%08x>\n", be32toh(meta->magic_2));
4407     kprintf("magic_3             <0x%08x>\n", be32toh(meta->magic_3));
4408     kprintf("magic_4             <0x%08x>\n", be32toh(meta->magic_4));
4409     kprintf("=================================================\n");
4410 }
4411 
4412 static char *
4413 ata_raid_hptv2_type(int type)
4414 {
4415     static char buffer[16];
4416 
4417     switch (type) {
4418     case HPTV2_T_RAID0:         return "RAID0";
4419     case HPTV2_T_RAID1:         return "RAID1";
4420     case HPTV2_T_RAID01_RAID0:  return "RAID01_RAID0";
4421     case HPTV2_T_SPAN:          return "SPAN";
4422     case HPTV2_T_RAID_3:        return "RAID3";
4423     case HPTV2_T_RAID_5:        return "RAID5";
4424     case HPTV2_T_JBOD:          return "JBOD";
4425     case HPTV2_T_RAID01_RAID1:  return "RAID01_RAID1";
4426     default:            ksprintf(buffer, "UNKNOWN 0x%02x", type);
4427 			return buffer;
4428     }
4429 }
4430 
4431 static void
4432 ata_raid_hptv2_print_meta(struct hptv2_raid_conf *meta)
4433 {
4434     int i;
4435 
4436     kprintf("****** ATA Highpoint V2 RocketRAID Metadata *****\n");
4437     kprintf("magic               0x%08x\n", meta->magic);
4438     kprintf("magic_0             0x%08x\n", meta->magic_0);
4439     kprintf("magic_1             0x%08x\n", meta->magic_1);
4440     kprintf("order               0x%08x\n", meta->order);
4441     kprintf("array_width         %u\n", meta->array_width);
4442     kprintf("stripe_shift        %u\n", meta->stripe_shift);
4443     kprintf("type                %s\n", ata_raid_hptv2_type(meta->type));
4444     kprintf("disk_number         %u\n", meta->disk_number);
4445     kprintf("total_sectors       %u\n", meta->total_sectors);
4446     kprintf("disk_mode           0x%08x\n", meta->disk_mode);
4447     kprintf("boot_mode           0x%08x\n", meta->boot_mode);
4448     kprintf("boot_disk           0x%02x\n", meta->boot_disk);
4449     kprintf("boot_protect        0x%02x\n", meta->boot_protect);
4450     kprintf("log_entries         0x%02x\n", meta->error_log_entries);
4451     kprintf("log_index           0x%02x\n", meta->error_log_index);
4452     if (meta->error_log_entries) {
4453 	kprintf("    timestamp  reason disk  status  sectors lba\n");
4454 	for (i = meta->error_log_index;
4455 	     i < meta->error_log_index + meta->error_log_entries; i++)
4456 	    kprintf("    0x%08x  0x%02x  0x%02x  0x%02x    0x%02x    0x%08x\n",
4457 		   meta->errorlog[i%32].timestamp,
4458 		   meta->errorlog[i%32].reason,
4459 		   meta->errorlog[i%32].disk, meta->errorlog[i%32].status,
4460 		   meta->errorlog[i%32].sectors, meta->errorlog[i%32].lba);
4461     }
4462     kprintf("rebuild_lba         0x%08x\n", meta->rebuild_lba);
4463     kprintf("dummy_1             0x%02x\n", meta->dummy_1);
4464     kprintf("name_1              <%.15s>\n", meta->name_1);
4465     kprintf("dummy_2             0x%02x\n", meta->dummy_2);
4466     kprintf("name_2              <%.15s>\n", meta->name_2);
4467     kprintf("=================================================\n");
4468 }
4469 
4470 static char *
4471 ata_raid_hptv3_type(int type)
4472 {
4473     static char buffer[16];
4474 
4475     switch (type) {
4476     case HPTV3_T_SPARE: return "SPARE";
4477     case HPTV3_T_JBOD:  return "JBOD";
4478     case HPTV3_T_SPAN:  return "SPAN";
4479     case HPTV3_T_RAID0: return "RAID0";
4480     case HPTV3_T_RAID1: return "RAID1";
4481     case HPTV3_T_RAID3: return "RAID3";
4482     case HPTV3_T_RAID5: return "RAID5";
4483     default:            ksprintf(buffer, "UNKNOWN 0x%02x", type);
4484 			return buffer;
4485     }
4486 }
4487 
4488 static void
4489 ata_raid_hptv3_print_meta(struct hptv3_raid_conf *meta)
4490 {
4491     int i;
4492 
4493     kprintf("****** ATA Highpoint V3 RocketRAID Metadata *****\n");
4494     kprintf("magic               0x%08x\n", meta->magic);
4495     kprintf("magic_0             0x%08x\n", meta->magic_0);
4496     kprintf("checksum_0          0x%02x\n", meta->checksum_0);
4497     kprintf("mode                0x%02x\n", meta->mode);
4498     kprintf("user_mode           0x%02x\n", meta->user_mode);
4499     kprintf("config_entries      0x%02x\n", meta->config_entries);
4500     for (i = 0; i < meta->config_entries; i++) {
4501 	kprintf("config %d:\n", i);
4502 	kprintf("    total_sectors       %ju\n",
4503 	       meta->configs[0].total_sectors +
4504 	       ((u_int64_t)meta->configs_high[0].total_sectors << 32));
4505 	kprintf("    type                %s\n",
4506 	       ata_raid_hptv3_type(meta->configs[i].type));
4507 	kprintf("    total_disks         %u\n", meta->configs[i].total_disks);
4508 	kprintf("    disk_number         %u\n", meta->configs[i].disk_number);
4509 	kprintf("    stripe_shift        %u\n", meta->configs[i].stripe_shift);
4510 	kprintf("    status              %b\n", meta->configs[i].status,
4511 	       "\20\2RAID5\1NEED_REBUILD\n");
4512 	kprintf("    critical_disks      %u\n", meta->configs[i].critical_disks);
4513 	kprintf("    rebuild_lba         %ju\n",
4514 	       meta->configs_high[0].rebuild_lba +
4515 	       ((u_int64_t)meta->configs_high[0].rebuild_lba << 32));
4516     }
4517     kprintf("name                <%.16s>\n", meta->name);
4518     kprintf("timestamp           0x%08x\n", meta->timestamp);
4519     kprintf("description         <%.16s>\n", meta->description);
4520     kprintf("creator             <%.16s>\n", meta->creator);
4521     kprintf("checksum_1          0x%02x\n", meta->checksum_1);
4522     kprintf("dummy_0             0x%02x\n", meta->dummy_0);
4523     kprintf("dummy_1             0x%02x\n", meta->dummy_1);
4524     kprintf("flags               %b\n", meta->flags,
4525 	   "\20\4RCACHE\3WCACHE\2NCQ\1TCQ\n");
4526     kprintf("=================================================\n");
4527 }
4528 
4529 static char *
4530 ata_raid_intel_type(int type)
4531 {
4532     static char buffer[16];
4533 
4534     switch (type) {
4535     case INTEL_T_RAID0: return "RAID0";
4536     case INTEL_T_RAID1: return "RAID1";
4537     case INTEL_T_RAID5: return "RAID5";
4538     default:            ksprintf(buffer, "UNKNOWN 0x%02x", type);
4539 			return buffer;
4540     }
4541 }
4542 
4543 static void
4544 ata_raid_intel_print_meta(struct intel_raid_conf *meta)
4545 {
4546     struct intel_raid_mapping *map;
4547     int i, j;
4548 
4549     kprintf("********* ATA Intel MatrixRAID Metadata *********\n");
4550     kprintf("intel_id            <%.24s>\n", meta->intel_id);
4551     kprintf("version             <%.6s>\n", meta->version);
4552     kprintf("checksum            0x%08x\n", meta->checksum);
4553     kprintf("config_size         0x%08x\n", meta->config_size);
4554     kprintf("config_id           0x%08x\n", meta->config_id);
4555     kprintf("generation          0x%08x\n", meta->generation);
4556     kprintf("total_disks         %u\n", meta->total_disks);
4557     kprintf("total_volumes       %u\n", meta->total_volumes);
4558     kprintf("DISK#   serial disk_sectors disk_id flags\n");
4559     for (i = 0; i < meta->total_disks; i++ ) {
4560 	kprintf("    %d   <%.16s> %u 0x%08x 0x%08x\n", i,
4561 	       meta->disk[i].serial, meta->disk[i].sectors,
4562 	       meta->disk[i].id, meta->disk[i].flags);
4563     }
4564     map = (struct intel_raid_mapping *)&meta->disk[meta->total_disks];
4565     for (j = 0; j < meta->total_volumes; j++) {
4566 	kprintf("name                %.16s\n", map->name);
4567 	kprintf("total_sectors       %ju\n", map->total_sectors);
4568 	kprintf("state               %u\n", map->state);
4569 	kprintf("reserved            %u\n", map->reserved);
4570 	kprintf("offset              %u\n", map->offset);
4571 	kprintf("disk_sectors        %u\n", map->disk_sectors);
4572 	kprintf("stripe_count        %u\n", map->stripe_count);
4573 	kprintf("stripe_sectors      %u\n", map->stripe_sectors);
4574 	kprintf("status              %u\n", map->status);
4575 	kprintf("type                %s\n", ata_raid_intel_type(map->type));
4576 	kprintf("total_disks         %u\n", map->total_disks);
4577 	kprintf("magic[0]            0x%02x\n", map->magic[0]);
4578 	kprintf("magic[1]            0x%02x\n", map->magic[1]);
4579 	kprintf("magic[2]            0x%02x\n", map->magic[2]);
4580 	for (i = 0; i < map->total_disks; i++ ) {
4581 	    kprintf("    disk %d at disk_idx 0x%08x\n", i, map->disk_idx[i]);
4582 	}
4583 	map = (struct intel_raid_mapping *)&map->disk_idx[map->total_disks];
4584     }
4585     kprintf("=================================================\n");
4586 }
4587 
4588 static char *
4589 ata_raid_ite_type(int type)
4590 {
4591     static char buffer[16];
4592 
4593     switch (type) {
4594     case ITE_T_RAID0:   return "RAID0";
4595     case ITE_T_RAID1:   return "RAID1";
4596     case ITE_T_RAID01:  return "RAID0+1";
4597     case ITE_T_SPAN:    return "SPAN";
4598     default:            ksprintf(buffer, "UNKNOWN 0x%02x", type);
4599 			return buffer;
4600     }
4601 }
4602 
4603 static void
4604 ata_raid_ite_print_meta(struct ite_raid_conf *meta)
4605 {
4606     kprintf("*** ATA Integrated Technology Express Metadata **\n");
4607     kprintf("ite_id              <%.40s>\n", meta->ite_id);
4608     kprintf("timestamp_0         %04x/%02x/%02x %02x:%02x:%02x.%02x\n",
4609 	   *((u_int16_t *)meta->timestamp_0), meta->timestamp_0[2],
4610 	   meta->timestamp_0[3], meta->timestamp_0[5], meta->timestamp_0[4],
4611 	   meta->timestamp_0[7], meta->timestamp_0[6]);
4612     kprintf("total_sectors       %jd\n", meta->total_sectors);
4613     kprintf("type                %s\n", ata_raid_ite_type(meta->type));
4614     kprintf("stripe_1kblocks     %u\n", meta->stripe_1kblocks);
4615     kprintf("timestamp_1         %04x/%02x/%02x %02x:%02x:%02x.%02x\n",
4616 	   *((u_int16_t *)meta->timestamp_1), meta->timestamp_1[2],
4617 	   meta->timestamp_1[3], meta->timestamp_1[5], meta->timestamp_1[4],
4618 	   meta->timestamp_1[7], meta->timestamp_1[6]);
4619     kprintf("stripe_sectors      %u\n", meta->stripe_sectors);
4620     kprintf("array_width         %u\n", meta->array_width);
4621     kprintf("disk_number         %u\n", meta->disk_number);
4622     kprintf("disk_sectors        %u\n", meta->disk_sectors);
4623     kprintf("=================================================\n");
4624 }
4625 
4626 static char *
4627 ata_raid_jmicron_type(int type)
4628 {
4629     static char buffer[16];
4630 
4631     switch (type) {
4632     case JM_T_RAID0:	return "RAID0";
4633     case JM_T_RAID1:	return "RAID1";
4634     case JM_T_RAID01:	return "RAID0+1";
4635     case JM_T_JBOD:	return "JBOD";
4636     case JM_T_RAID5:	return "RAID5";
4637     default:            ksprintf(buffer, "UNKNOWN 0x%02x", type);
4638 			return buffer;
4639     }
4640 }
4641 
4642 static void
4643 ata_raid_jmicron_print_meta(struct jmicron_raid_conf *meta)
4644 {
4645     int i;
4646 
4647     kprintf("***** ATA JMicron Technology Corp Metadata ******\n");
4648     kprintf("signature           %.2s\n", meta->signature);
4649     kprintf("version             0x%04x\n", meta->version);
4650     kprintf("checksum            0x%04x\n", meta->checksum);
4651     kprintf("disk_id             0x%08x\n", meta->disk_id);
4652     kprintf("offset              0x%08x\n", meta->offset);
4653     kprintf("disk_sectors_low    0x%08x\n", meta->disk_sectors_low);
4654     kprintf("disk_sectors_high   0x%08x\n", meta->disk_sectors_high);
4655     kprintf("name                %.16s\n", meta->name);
4656     kprintf("type                %s\n", ata_raid_jmicron_type(meta->type));
4657     kprintf("stripe_shift        %d\n", meta->stripe_shift);
4658     kprintf("flags               0x%04x\n", meta->flags);
4659     kprintf("spare:\n");
4660     for (i=0; i < 2 && meta->spare[i]; i++)
4661 	kprintf("    %d                  0x%08x\n", i, meta->spare[i]);
4662     kprintf("disks:\n");
4663     for (i=0; i < 8 && meta->disks[i]; i++)
4664 	kprintf("    %d                  0x%08x\n", i, meta->disks[i]);
4665     kprintf("=================================================\n");
4666 }
4667 
4668 static char *
4669 ata_raid_lsiv2_type(int type)
4670 {
4671     static char buffer[16];
4672 
4673     switch (type) {
4674     case LSIV2_T_RAID0: return "RAID0";
4675     case LSIV2_T_RAID1: return "RAID1";
4676     case LSIV2_T_SPARE: return "SPARE";
4677     default:            ksprintf(buffer, "UNKNOWN 0x%02x", type);
4678 			return buffer;
4679     }
4680 }
4681 
4682 static void
4683 ata_raid_lsiv2_print_meta(struct lsiv2_raid_conf *meta)
4684 {
4685     int i;
4686 
4687     kprintf("******* ATA LSILogic V2 MegaRAID Metadata *******\n");
4688     kprintf("lsi_id              <%s>\n", meta->lsi_id);
4689     kprintf("dummy_0             0x%02x\n", meta->dummy_0);
4690     kprintf("flags               0x%02x\n", meta->flags);
4691     kprintf("version             0x%04x\n", meta->version);
4692     kprintf("config_entries      0x%02x\n", meta->config_entries);
4693     kprintf("raid_count          0x%02x\n", meta->raid_count);
4694     kprintf("total_disks         0x%02x\n", meta->total_disks);
4695     kprintf("dummy_1             0x%02x\n", meta->dummy_1);
4696     kprintf("dummy_2             0x%04x\n", meta->dummy_2);
4697     for (i = 0; i < meta->config_entries; i++) {
4698 	kprintf("    type             %s\n",
4699 	       ata_raid_lsiv2_type(meta->configs[i].raid.type));
4700 	kprintf("    dummy_0          %02x\n", meta->configs[i].raid.dummy_0);
4701 	kprintf("    stripe_sectors   %u\n",
4702 	       meta->configs[i].raid.stripe_sectors);
4703 	kprintf("    array_width      %u\n",
4704 	       meta->configs[i].raid.array_width);
4705 	kprintf("    disk_count       %u\n", meta->configs[i].raid.disk_count);
4706 	kprintf("    config_offset    %u\n",
4707 	       meta->configs[i].raid.config_offset);
4708 	kprintf("    dummy_1          %u\n", meta->configs[i].raid.dummy_1);
4709 	kprintf("    flags            %02x\n", meta->configs[i].raid.flags);
4710 	kprintf("    total_sectors    %u\n",
4711 	       meta->configs[i].raid.total_sectors);
4712     }
4713     kprintf("disk_number         0x%02x\n", meta->disk_number);
4714     kprintf("raid_number         0x%02x\n", meta->raid_number);
4715     kprintf("timestamp           0x%08x\n", meta->timestamp);
4716     kprintf("=================================================\n");
4717 }
4718 
4719 static char *
4720 ata_raid_lsiv3_type(int type)
4721 {
4722     static char buffer[16];
4723 
4724     switch (type) {
4725     case LSIV3_T_RAID0: return "RAID0";
4726     case LSIV3_T_RAID1: return "RAID1";
4727     default:            ksprintf(buffer, "UNKNOWN 0x%02x", type);
4728 			return buffer;
4729     }
4730 }
4731 
4732 static void
4733 ata_raid_lsiv3_print_meta(struct lsiv3_raid_conf *meta)
4734 {
4735     int i;
4736 
4737     kprintf("******* ATA LSILogic V3 MegaRAID Metadata *******\n");
4738     kprintf("lsi_id              <%.6s>\n", meta->lsi_id);
4739     kprintf("dummy_0             0x%04x\n", meta->dummy_0);
4740     kprintf("version             0x%04x\n", meta->version);
4741     kprintf("dummy_0             0x%04x\n", meta->dummy_1);
4742     kprintf("RAID configs:\n");
4743     for (i = 0; i < 8; i++) {
4744 	if (meta->raid[i].total_disks) {
4745 	    kprintf("%02d  stripe_pages       %u\n", i,
4746 		   meta->raid[i].stripe_pages);
4747 	    kprintf("%02d  type               %s\n", i,
4748 		   ata_raid_lsiv3_type(meta->raid[i].type));
4749 	    kprintf("%02d  total_disks        %u\n", i,
4750 		   meta->raid[i].total_disks);
4751 	    kprintf("%02d  array_width        %u\n", i,
4752 		   meta->raid[i].array_width);
4753 	    kprintf("%02d  sectors            %u\n", i, meta->raid[i].sectors);
4754 	    kprintf("%02d  offset             %u\n", i, meta->raid[i].offset);
4755 	    kprintf("%02d  device             0x%02x\n", i,
4756 		   meta->raid[i].device);
4757 	}
4758     }
4759     kprintf("DISK configs:\n");
4760     for (i = 0; i < 6; i++) {
4761 	    if (meta->disk[i].disk_sectors) {
4762 	    kprintf("%02d  disk_sectors       %u\n", i,
4763 		   meta->disk[i].disk_sectors);
4764 	    kprintf("%02d  flags              0x%02x\n", i, meta->disk[i].flags);
4765 	}
4766     }
4767     kprintf("device              0x%02x\n", meta->device);
4768     kprintf("timestamp           0x%08x\n", meta->timestamp);
4769     kprintf("checksum_1          0x%02x\n", meta->checksum_1);
4770     kprintf("=================================================\n");
4771 }
4772 
4773 static char *
4774 ata_raid_nvidia_type(int type)
4775 {
4776     static char buffer[16];
4777 
4778     switch (type) {
4779     case NV_T_SPAN:     return "SPAN";
4780     case NV_T_RAID0:    return "RAID0";
4781     case NV_T_RAID1:    return "RAID1";
4782     case NV_T_RAID3:    return "RAID3";
4783     case NV_T_RAID5:    return "RAID5";
4784     case NV_T_RAID01:   return "RAID0+1";
4785     default:            ksprintf(buffer, "UNKNOWN 0x%02x", type);
4786 			return buffer;
4787     }
4788 }
4789 
4790 static void
4791 ata_raid_nvidia_print_meta(struct nvidia_raid_conf *meta)
4792 {
4793     kprintf("******** ATA nVidia MediaShield Metadata ********\n");
4794     kprintf("nvidia_id           <%.8s>\n", meta->nvidia_id);
4795     kprintf("config_size         %d\n", meta->config_size);
4796     kprintf("checksum            0x%08x\n", meta->checksum);
4797     kprintf("version             0x%04x\n", meta->version);
4798     kprintf("disk_number         %d\n", meta->disk_number);
4799     kprintf("dummy_0             0x%02x\n", meta->dummy_0);
4800     kprintf("total_sectors       %d\n", meta->total_sectors);
4801     kprintf("sectors_size        %d\n", meta->sector_size);
4802     kprintf("serial              %.16s\n", meta->serial);
4803     kprintf("revision            %.4s\n", meta->revision);
4804     kprintf("dummy_1             0x%08x\n", meta->dummy_1);
4805     kprintf("magic_0             0x%08x\n", meta->magic_0);
4806     kprintf("magic_1             0x%016jx\n", meta->magic_1);
4807     kprintf("magic_2             0x%016jx\n", meta->magic_2);
4808     kprintf("flags               0x%02x\n", meta->flags);
4809     kprintf("array_width         %d\n", meta->array_width);
4810     kprintf("total_disks         %d\n", meta->total_disks);
4811     kprintf("dummy_2             0x%02x\n", meta->dummy_2);
4812     kprintf("type                %s\n", ata_raid_nvidia_type(meta->type));
4813     kprintf("dummy_3             0x%04x\n", meta->dummy_3);
4814     kprintf("stripe_sectors      %d\n", meta->stripe_sectors);
4815     kprintf("stripe_bytes        %d\n", meta->stripe_bytes);
4816     kprintf("stripe_shift        %d\n", meta->stripe_shift);
4817     kprintf("stripe_mask         0x%08x\n", meta->stripe_mask);
4818     kprintf("stripe_sizesectors  %d\n", meta->stripe_sizesectors);
4819     kprintf("stripe_sizebytes    %d\n", meta->stripe_sizebytes);
4820     kprintf("rebuild_lba         %d\n", meta->rebuild_lba);
4821     kprintf("dummy_4             0x%08x\n", meta->dummy_4);
4822     kprintf("dummy_5             0x%08x\n", meta->dummy_5);
4823     kprintf("status              0x%08x\n", meta->status);
4824     kprintf("=================================================\n");
4825 }
4826 
4827 static char *
4828 ata_raid_promise_type(int type)
4829 {
4830     static char buffer[16];
4831 
4832     switch (type) {
4833     case PR_T_RAID0:    return "RAID0";
4834     case PR_T_RAID1:    return "RAID1";
4835     case PR_T_RAID3:    return "RAID3";
4836     case PR_T_RAID5:    return "RAID5";
4837     case PR_T_SPAN:     return "SPAN";
4838     default:            ksprintf(buffer, "UNKNOWN 0x%02x", type);
4839 			return buffer;
4840     }
4841 }
4842 
4843 static void
4844 ata_raid_promise_print_meta(struct promise_raid_conf *meta)
4845 {
4846     int i;
4847 
4848     kprintf("********* ATA Promise FastTrak Metadata *********\n");
4849     kprintf("promise_id          <%s>\n", meta->promise_id);
4850     kprintf("dummy_0             0x%08x\n", meta->dummy_0);
4851     kprintf("magic_0             0x%016jx\n", meta->magic_0);
4852     kprintf("magic_1             0x%04x\n", meta->magic_1);
4853     kprintf("magic_2             0x%08x\n", meta->magic_2);
4854     kprintf("integrity           0x%08x %b\n", meta->raid.integrity,
4855 		meta->raid.integrity, "\20\10VALID\n" );
4856     kprintf("flags               0x%02x %b\n",
4857 	   meta->raid.flags, meta->raid.flags,
4858 	   "\20\10READY\7DOWN\6REDIR\5DUPLICATE\4SPARE"
4859 	   "\3ASSIGNED\2ONLINE\1VALID\n");
4860     kprintf("disk_number         %d\n", meta->raid.disk_number);
4861     kprintf("channel             0x%02x\n", meta->raid.channel);
4862     kprintf("device              0x%02x\n", meta->raid.device);
4863     kprintf("magic_0             0x%016jx\n", meta->raid.magic_0);
4864     kprintf("disk_offset         %u\n", meta->raid.disk_offset);
4865     kprintf("disk_sectors        %u\n", meta->raid.disk_sectors);
4866     kprintf("rebuild_lba         0x%08x\n", meta->raid.rebuild_lba);
4867     kprintf("generation          0x%04x\n", meta->raid.generation);
4868     kprintf("status              0x%02x %b\n",
4869 	    meta->raid.status, meta->raid.status,
4870 	   "\20\6MARKED\5DEGRADED\4READY\3INITED\2ONLINE\1VALID\n");
4871     kprintf("type                %s\n", ata_raid_promise_type(meta->raid.type));
4872     kprintf("total_disks         %u\n", meta->raid.total_disks);
4873     kprintf("stripe_shift        %u\n", meta->raid.stripe_shift);
4874     kprintf("array_width         %u\n", meta->raid.array_width);
4875     kprintf("array_number        %u\n", meta->raid.array_number);
4876     kprintf("total_sectors       %u\n", meta->raid.total_sectors);
4877     kprintf("cylinders           %u\n", meta->raid.cylinders);
4878     kprintf("heads               %u\n", meta->raid.heads);
4879     kprintf("sectors             %u\n", meta->raid.sectors);
4880     kprintf("magic_1             0x%016jx\n", meta->raid.magic_1);
4881     kprintf("DISK#   flags dummy_0 channel device  magic_0\n");
4882     for (i = 0; i < 8; i++) {
4883 	kprintf("  %d    %b    0x%02x  0x%02x  0x%02x  ",
4884 	       i, meta->raid.disk[i].flags,
4885 	       "\20\10READY\7DOWN\6REDIR\5DUPLICATE\4SPARE"
4886 	       "\3ASSIGNED\2ONLINE\1VALID\n", meta->raid.disk[i].dummy_0,
4887 	       meta->raid.disk[i].channel, meta->raid.disk[i].device);
4888 	kprintf("0x%016jx\n", meta->raid.disk[i].magic_0);
4889     }
4890     kprintf("checksum            0x%08x\n", meta->checksum);
4891     kprintf("=================================================\n");
4892 }
4893 
4894 static char *
4895 ata_raid_sii_type(int type)
4896 {
4897     static char buffer[16];
4898 
4899     switch (type) {
4900     case SII_T_RAID0:   return "RAID0";
4901     case SII_T_RAID1:   return "RAID1";
4902     case SII_T_RAID01:  return "RAID0+1";
4903     case SII_T_SPARE:   return "SPARE";
4904     default:            ksprintf(buffer, "UNKNOWN 0x%02x", type);
4905 			return buffer;
4906     }
4907 }
4908 
4909 static void
4910 ata_raid_sii_print_meta(struct sii_raid_conf *meta)
4911 {
4912     kprintf("******* ATA Silicon Image Medley Metadata *******\n");
4913     kprintf("total_sectors       %ju\n", meta->total_sectors);
4914     kprintf("dummy_0             0x%04x\n", meta->dummy_0);
4915     kprintf("dummy_1             0x%04x\n", meta->dummy_1);
4916     kprintf("controller_pci_id   0x%08x\n", meta->controller_pci_id);
4917     kprintf("version_minor       0x%04x\n", meta->version_minor);
4918     kprintf("version_major       0x%04x\n", meta->version_major);
4919     kprintf("timestamp           20%02x/%02x/%02x %02x:%02x:%02x\n",
4920 	   meta->timestamp[5], meta->timestamp[4], meta->timestamp[3],
4921 	   meta->timestamp[2], meta->timestamp[1], meta->timestamp[0]);
4922     kprintf("stripe_sectors      %u\n", meta->stripe_sectors);
4923     kprintf("dummy_2             0x%04x\n", meta->dummy_2);
4924     kprintf("disk_number         %u\n", meta->disk_number);
4925     kprintf("type                %s\n", ata_raid_sii_type(meta->type));
4926     kprintf("raid0_disks         %u\n", meta->raid0_disks);
4927     kprintf("raid0_ident         %u\n", meta->raid0_ident);
4928     kprintf("raid1_disks         %u\n", meta->raid1_disks);
4929     kprintf("raid1_ident         %u\n", meta->raid1_ident);
4930     kprintf("rebuild_lba         %ju\n", meta->rebuild_lba);
4931     kprintf("generation          0x%08x\n", meta->generation);
4932     kprintf("status              0x%02x %b\n",
4933 	    meta->status, meta->status,
4934 	   "\20\1READY\n");
4935     kprintf("base_raid1_position %02x\n", meta->base_raid1_position);
4936     kprintf("base_raid0_position %02x\n", meta->base_raid0_position);
4937     kprintf("position            %02x\n", meta->position);
4938     kprintf("dummy_3             %04x\n", meta->dummy_3);
4939     kprintf("name                <%.16s>\n", meta->name);
4940     kprintf("checksum_0          0x%04x\n", meta->checksum_0);
4941     kprintf("checksum_1          0x%04x\n", meta->checksum_1);
4942     kprintf("=================================================\n");
4943 }
4944 
4945 static char *
4946 ata_raid_sis_type(int type)
4947 {
4948     static char buffer[16];
4949 
4950     switch (type) {
4951     case SIS_T_JBOD:    return "JBOD";
4952     case SIS_T_RAID0:   return "RAID0";
4953     case SIS_T_RAID1:   return "RAID1";
4954     default:            ksprintf(buffer, "UNKNOWN 0x%02x", type);
4955 			return buffer;
4956     }
4957 }
4958 
4959 static void
4960 ata_raid_sis_print_meta(struct sis_raid_conf *meta)
4961 {
4962     kprintf("**** ATA Silicon Integrated Systems Metadata ****\n");
4963     kprintf("magic               0x%04x\n", meta->magic);
4964     kprintf("disks               0x%02x\n", meta->disks);
4965     kprintf("type                %s\n",
4966 	   ata_raid_sis_type(meta->type_total_disks & SIS_T_MASK));
4967     kprintf("total_disks         %u\n", meta->type_total_disks & SIS_D_MASK);
4968     kprintf("dummy_0             0x%08x\n", meta->dummy_0);
4969     kprintf("controller_pci_id   0x%08x\n", meta->controller_pci_id);
4970     kprintf("stripe_sectors      %u\n", meta->stripe_sectors);
4971     kprintf("dummy_1             0x%04x\n", meta->dummy_1);
4972     kprintf("timestamp           0x%08x\n", meta->timestamp);
4973     kprintf("model               %.40s\n", meta->model);
4974     kprintf("disk_number         %u\n", meta->disk_number);
4975     kprintf("dummy_2             0x%02x 0x%02x 0x%02x\n",
4976 	   meta->dummy_2[0], meta->dummy_2[1], meta->dummy_2[2]);
4977     kprintf("=================================================\n");
4978 }
4979 
4980 static char *
4981 ata_raid_via_type(int type)
4982 {
4983     static char buffer[16];
4984 
4985     switch (type) {
4986     case VIA_T_RAID0:   return "RAID0";
4987     case VIA_T_RAID1:   return "RAID1";
4988     case VIA_T_RAID5:   return "RAID5";
4989     case VIA_T_RAID01:  return "RAID0+1";
4990     case VIA_T_SPAN:    return "SPAN";
4991     default:            ksprintf(buffer, "UNKNOWN 0x%02x", type);
4992 			return buffer;
4993     }
4994 }
4995 
4996 static void
4997 ata_raid_via_print_meta(struct via_raid_conf *meta)
4998 {
4999     int i;
5000 
5001     kprintf("*************** ATA VIA Metadata ****************\n");
5002     kprintf("magic               0x%02x\n", meta->magic);
5003     kprintf("dummy_0             0x%02x\n", meta->dummy_0);
5004     kprintf("type                %s\n",
5005 	   ata_raid_via_type(meta->type & VIA_T_MASK));
5006     kprintf("bootable            %d\n", meta->type & VIA_T_BOOTABLE);
5007     kprintf("unknown             %d\n", meta->type & VIA_T_UNKNOWN);
5008     kprintf("disk_index          0x%02x\n", meta->disk_index);
5009     kprintf("stripe_layout       0x%02x\n", meta->stripe_layout);
5010     kprintf(" stripe_disks       %d\n", meta->stripe_layout & VIA_L_DISKS);
5011     kprintf(" stripe_sectors     %d\n",
5012 	   0x08 << ((meta->stripe_layout & VIA_L_MASK) >> VIA_L_SHIFT));
5013     kprintf("disk_sectors        %ju\n", meta->disk_sectors);
5014     kprintf("disk_id             0x%08x\n", meta->disk_id);
5015     kprintf("DISK#   disk_id\n");
5016     for (i = 0; i < 8; i++) {
5017 	if (meta->disks[i])
5018 	    kprintf("  %d    0x%08x\n", i, meta->disks[i]);
5019     }
5020     kprintf("checksum            0x%02x\n", meta->checksum);
5021     kprintf("=================================================\n");
5022 }
5023