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