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