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