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