1c1b3d7c5SThomas E. Spanjaard /*- 29243051bSzrj * Copyright (c) 2000 - 2008 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> 3889be26e1SSascha 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> 4315bd3c73SMatthew Dillon #include <sys/lock.h> 445e8604ceSThomas E. Spanjaard #include <sys/module.h> 455e8604ceSThomas E. Spanjaard #include <sys/nata.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 60*bb15467aSzrj /* local implementation, to trigger a warning */ 61*bb15467aSzrj static inline void 62*bb15467aSzrj biofinish(struct bio *bp, struct bio *x __unused, int error) 63*bb15467aSzrj { 64*bb15467aSzrj struct buf *bbp = bp->bio_buf; 65*bb15467aSzrj 66*bb15467aSzrj bbp->b_flags |= B_ERROR; 67*bb15467aSzrj bbp->b_error = error; 68*bb15467aSzrj biodone(bp); 69*bb15467aSzrj } 70c1b3d7c5SThomas E. Spanjaard 71c1b3d7c5SThomas E. Spanjaard /* device structure */ 72c1b3d7c5SThomas E. Spanjaard static d_strategy_t ata_raid_strategy; 73c1b3d7c5SThomas E. Spanjaard static d_dump_t ata_raid_dump; 745e8604ceSThomas E. Spanjaard static struct dev_ops ar_ops = { 7547f4bca5SSascha Wildner { "ar", 0, D_DISK }, 76c1b3d7c5SThomas E. Spanjaard .d_open = nullopen, 77c1b3d7c5SThomas E. Spanjaard .d_close = nullclose, 78c1b3d7c5SThomas E. Spanjaard .d_read = physread, 79c1b3d7c5SThomas E. Spanjaard .d_write = physwrite, 80c1b3d7c5SThomas E. Spanjaard .d_strategy = ata_raid_strategy, 81c1b3d7c5SThomas E. Spanjaard .d_dump = ata_raid_dump, 82c1b3d7c5SThomas E. Spanjaard }; 83c1b3d7c5SThomas E. Spanjaard 84c1b3d7c5SThomas E. Spanjaard /* prototypes */ 85c1b3d7c5SThomas E. Spanjaard static void ata_raid_done(struct ata_request *request); 86c1b3d7c5SThomas E. Spanjaard static void ata_raid_config_changed(struct ar_softc *rdp, int writeback); 879243051bSzrj static int ata_raid_status(struct ata_ioc_raid_status *status); 88c1b3d7c5SThomas E. Spanjaard static int ata_raid_create(struct ata_ioc_raid_config *config); 89c1b3d7c5SThomas E. Spanjaard static int ata_raid_delete(int array); 90c1b3d7c5SThomas E. Spanjaard static int ata_raid_addspare(struct ata_ioc_raid_config *config); 91c1b3d7c5SThomas E. Spanjaard static int ata_raid_rebuild(int array); 92c1b3d7c5SThomas E. Spanjaard static int ata_raid_read_metadata(device_t subdisk); 93c1b3d7c5SThomas E. Spanjaard static int ata_raid_write_metadata(struct ar_softc *rdp); 94c1b3d7c5SThomas E. Spanjaard static int ata_raid_wipe_metadata(struct ar_softc *rdp); 95c1b3d7c5SThomas E. Spanjaard static int ata_raid_adaptec_read_meta(device_t dev, struct ar_softc **raidp); 96c1b3d7c5SThomas E. Spanjaard static int ata_raid_hptv2_read_meta(device_t dev, struct ar_softc **raidp); 97c1b3d7c5SThomas E. Spanjaard static int ata_raid_hptv2_write_meta(struct ar_softc *rdp); 98c1b3d7c5SThomas E. Spanjaard static int ata_raid_hptv3_read_meta(device_t dev, struct ar_softc **raidp); 99c1b3d7c5SThomas E. Spanjaard static int ata_raid_intel_read_meta(device_t dev, struct ar_softc **raidp); 100c1b3d7c5SThomas E. Spanjaard static int ata_raid_intel_write_meta(struct ar_softc *rdp); 101c1b3d7c5SThomas E. Spanjaard static int ata_raid_ite_read_meta(device_t dev, struct ar_softc **raidp); 102c1b3d7c5SThomas E. Spanjaard static int ata_raid_jmicron_read_meta(device_t dev, struct ar_softc **raidp); 103c1b3d7c5SThomas E. Spanjaard static int ata_raid_jmicron_write_meta(struct ar_softc *rdp); 104c1b3d7c5SThomas E. Spanjaard static int ata_raid_lsiv2_read_meta(device_t dev, struct ar_softc **raidp); 105c1b3d7c5SThomas E. Spanjaard static int ata_raid_lsiv3_read_meta(device_t dev, struct ar_softc **raidp); 106c1b3d7c5SThomas E. Spanjaard static int ata_raid_nvidia_read_meta(device_t dev, struct ar_softc **raidp); 107c1b3d7c5SThomas E. Spanjaard static int ata_raid_promise_read_meta(device_t dev, struct ar_softc **raidp, int native); 108c1b3d7c5SThomas E. Spanjaard static int ata_raid_promise_write_meta(struct ar_softc *rdp); 109c1b3d7c5SThomas E. Spanjaard static int ata_raid_sii_read_meta(device_t dev, struct ar_softc **raidp); 110c1b3d7c5SThomas E. Spanjaard static int ata_raid_sis_read_meta(device_t dev, struct ar_softc **raidp); 111c1b3d7c5SThomas E. Spanjaard static int ata_raid_sis_write_meta(struct ar_softc *rdp); 112c1b3d7c5SThomas E. Spanjaard static int ata_raid_via_read_meta(device_t dev, struct ar_softc **raidp); 113c1b3d7c5SThomas E. Spanjaard static int ata_raid_via_write_meta(struct ar_softc *rdp); 114c1b3d7c5SThomas E. Spanjaard static struct ata_request *ata_raid_init_request(struct ar_softc *rdp, struct bio *bio); 115c1b3d7c5SThomas E. Spanjaard static int ata_raid_send_request(struct ata_request *request); 116c1b3d7c5SThomas E. Spanjaard static int ata_raid_rw(device_t dev, u_int64_t lba, void *data, u_int bcount, int flags); 117c1b3d7c5SThomas E. Spanjaard static char * ata_raid_format(struct ar_softc *rdp); 118c1b3d7c5SThomas E. Spanjaard static char * ata_raid_type(struct ar_softc *rdp); 119c1b3d7c5SThomas E. Spanjaard static char * ata_raid_flags(struct ar_softc *rdp); 120c1b3d7c5SThomas E. Spanjaard 121c1b3d7c5SThomas E. Spanjaard /* debugging only */ 122c1b3d7c5SThomas E. Spanjaard static void ata_raid_print_meta(struct ar_softc *meta); 123c1b3d7c5SThomas E. Spanjaard static void ata_raid_adaptec_print_meta(struct adaptec_raid_conf *meta); 124c1b3d7c5SThomas E. Spanjaard static void ata_raid_hptv2_print_meta(struct hptv2_raid_conf *meta); 125c1b3d7c5SThomas E. Spanjaard static void ata_raid_hptv3_print_meta(struct hptv3_raid_conf *meta); 126c1b3d7c5SThomas E. Spanjaard static void ata_raid_intel_print_meta(struct intel_raid_conf *meta); 127c1b3d7c5SThomas E. Spanjaard static void ata_raid_ite_print_meta(struct ite_raid_conf *meta); 128c1b3d7c5SThomas E. Spanjaard static void ata_raid_jmicron_print_meta(struct jmicron_raid_conf *meta); 129c1b3d7c5SThomas E. Spanjaard static void ata_raid_lsiv2_print_meta(struct lsiv2_raid_conf *meta); 130c1b3d7c5SThomas E. Spanjaard static void ata_raid_lsiv3_print_meta(struct lsiv3_raid_conf *meta); 131c1b3d7c5SThomas E. Spanjaard static void ata_raid_nvidia_print_meta(struct nvidia_raid_conf *meta); 132c1b3d7c5SThomas E. Spanjaard static void ata_raid_promise_print_meta(struct promise_raid_conf *meta); 133c1b3d7c5SThomas E. Spanjaard static void ata_raid_sii_print_meta(struct sii_raid_conf *meta); 134c1b3d7c5SThomas E. Spanjaard static void ata_raid_sis_print_meta(struct sis_raid_conf *meta); 135c1b3d7c5SThomas E. Spanjaard static void ata_raid_via_print_meta(struct via_raid_conf *meta); 136c1b3d7c5SThomas E. Spanjaard 137c1b3d7c5SThomas E. Spanjaard /* internal vars */ 138c1b3d7c5SThomas E. Spanjaard static struct ar_softc *ata_raid_arrays[MAX_ARRAYS]; 139c1b3d7c5SThomas E. Spanjaard static MALLOC_DEFINE(M_AR, "ar_driver", "ATA PseudoRAID driver"); 140c1b3d7c5SThomas E. Spanjaard static devclass_t ata_raid_sub_devclass; 141c1b3d7c5SThomas E. Spanjaard static int testing = 0; 142c1b3d7c5SThomas E. Spanjaard 143c1b3d7c5SThomas E. Spanjaard static void 144c1b3d7c5SThomas E. Spanjaard ata_raid_attach(struct ar_softc *rdp, int writeback) 145c1b3d7c5SThomas E. Spanjaard { 146a688b15cSMatthew Dillon struct disk_info info; 1475e8604ceSThomas E. Spanjaard cdev_t cdev; 148c1b3d7c5SThomas E. Spanjaard char buffer[32]; 149c1b3d7c5SThomas E. Spanjaard int disk; 150c1b3d7c5SThomas E. Spanjaard 15115bd3c73SMatthew Dillon lockinit(&rdp->lock, "ataraidattach", 0, 0); 152c1b3d7c5SThomas E. Spanjaard ata_raid_config_changed(rdp, writeback); 153c1b3d7c5SThomas E. Spanjaard 154c1b3d7c5SThomas E. Spanjaard /* sanitize arrays total_size % (width * interleave) == 0 */ 155c1b3d7c5SThomas E. Spanjaard if (rdp->type == AR_T_RAID0 || rdp->type == AR_T_RAID01 || 156c1b3d7c5SThomas E. Spanjaard rdp->type == AR_T_RAID5) { 157c1b3d7c5SThomas E. Spanjaard rdp->total_sectors = (rdp->total_sectors/(rdp->interleave*rdp->width))* 158c1b3d7c5SThomas E. Spanjaard (rdp->interleave * rdp->width); 159f8c7a42dSMatthew Dillon ksprintf(buffer, " (stripe %d KB)", 160c1b3d7c5SThomas E. Spanjaard (rdp->interleave * DEV_BSIZE) / 1024); 161c1b3d7c5SThomas E. Spanjaard } 162c1b3d7c5SThomas E. Spanjaard else 163c1b3d7c5SThomas E. Spanjaard buffer[0] = '\0'; 16489be26e1SSascha Wildner 16589be26e1SSascha Wildner devstat_add_entry(&rdp->devstat, "ar", rdp->lun, 16689be26e1SSascha Wildner DEV_BSIZE, DEVSTAT_NO_ORDERED_TAGS, 16789be26e1SSascha Wildner DEVSTAT_TYPE_STORARRAY | DEVSTAT_TYPE_IF_OTHER, 16889be26e1SSascha Wildner DEVSTAT_PRIORITY_ARRAY); 16989be26e1SSascha Wildner 170a688b15cSMatthew Dillon cdev = disk_create(rdp->lun, &rdp->disk, &ar_ops); 1715e8604ceSThomas E. Spanjaard cdev->si_drv1 = rdp; 1725e8604ceSThomas E. Spanjaard cdev->si_iosize_max = 128 * DEV_BSIZE; 1735e8604ceSThomas E. Spanjaard rdp->cdev = cdev; 174a688b15cSMatthew Dillon 175a688b15cSMatthew Dillon bzero(&info, sizeof(info)); 176a688b15cSMatthew Dillon info.d_media_blksize = DEV_BSIZE; /* mandatory */ 177a688b15cSMatthew Dillon info.d_media_blocks = rdp->total_sectors; 178a688b15cSMatthew Dillon 179a688b15cSMatthew Dillon info.d_secpertrack = rdp->sectors; /* optional */ 180a688b15cSMatthew Dillon info.d_nheads = rdp->heads; 181a688b15cSMatthew Dillon info.d_ncylinders = rdp->total_sectors/(rdp->heads*rdp->sectors); 182a688b15cSMatthew Dillon info.d_secpercyl = rdp->sectors * rdp->heads; 183c1b3d7c5SThomas E. Spanjaard 184e3869ec7SSascha Wildner kprintf("ar%d: %juMB <%s %s%s> status: %s\n", rdp->lun, 185c1b3d7c5SThomas E. Spanjaard rdp->total_sectors / ((1024L * 1024L) / DEV_BSIZE), 186c1b3d7c5SThomas E. Spanjaard ata_raid_format(rdp), ata_raid_type(rdp), 187c1b3d7c5SThomas E. Spanjaard buffer, ata_raid_flags(rdp)); 188c1b3d7c5SThomas E. Spanjaard 189c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 190e3869ec7SSascha Wildner kprintf("ar%d: %ju sectors [%dC/%dH/%dS] <%s> subdisks defined as:\n", 191c1b3d7c5SThomas E. Spanjaard rdp->lun, rdp->total_sectors, 192c1b3d7c5SThomas E. Spanjaard rdp->cylinders, rdp->heads, rdp->sectors, rdp->name); 193c1b3d7c5SThomas E. Spanjaard 194c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < rdp->total_disks; disk++) { 195e3869ec7SSascha Wildner kprintf("ar%d: disk%d ", rdp->lun, disk); 196c1b3d7c5SThomas E. Spanjaard if (rdp->disks[disk].dev) { 197c1b3d7c5SThomas E. Spanjaard if (rdp->disks[disk].flags & AR_DF_PRESENT) { 198c1b3d7c5SThomas E. Spanjaard /* status of this disk in the array */ 199c1b3d7c5SThomas E. Spanjaard if (rdp->disks[disk].flags & AR_DF_ONLINE) 200e3869ec7SSascha Wildner kprintf("READY "); 201c1b3d7c5SThomas E. Spanjaard else if (rdp->disks[disk].flags & AR_DF_SPARE) 202e3869ec7SSascha Wildner kprintf("SPARE "); 203c1b3d7c5SThomas E. Spanjaard else 204e3869ec7SSascha Wildner kprintf("FREE "); 205c1b3d7c5SThomas E. Spanjaard 206c1b3d7c5SThomas E. Spanjaard /* what type of disk is this in the array */ 207c1b3d7c5SThomas E. Spanjaard switch (rdp->type) { 208c1b3d7c5SThomas E. Spanjaard case AR_T_RAID1: 209c1b3d7c5SThomas E. Spanjaard case AR_T_RAID01: 210c1b3d7c5SThomas E. Spanjaard if (disk < rdp->width) 211e3869ec7SSascha Wildner kprintf("(master) "); 212c1b3d7c5SThomas E. Spanjaard else 213e3869ec7SSascha Wildner kprintf("(mirror) "); 214c1b3d7c5SThomas E. Spanjaard } 215c1b3d7c5SThomas E. Spanjaard 216c1b3d7c5SThomas E. Spanjaard /* which physical disk is used */ 217e3869ec7SSascha Wildner kprintf("using %s at ata%d-%s\n", 218c1b3d7c5SThomas E. Spanjaard device_get_nameunit(rdp->disks[disk].dev), 219c1b3d7c5SThomas E. Spanjaard device_get_unit(device_get_parent(rdp->disks[disk].dev)), 220c1b3d7c5SThomas E. Spanjaard (((struct ata_device *) 221c1b3d7c5SThomas E. Spanjaard device_get_softc(rdp->disks[disk].dev))->unit == 222c1b3d7c5SThomas E. Spanjaard ATA_MASTER) ? "master" : "slave"); 223c1b3d7c5SThomas E. Spanjaard } 224c1b3d7c5SThomas E. Spanjaard else if (rdp->disks[disk].flags & AR_DF_ASSIGNED) 225e3869ec7SSascha Wildner kprintf("DOWN\n"); 226c1b3d7c5SThomas E. Spanjaard else 227e3869ec7SSascha Wildner kprintf("INVALID no RAID config on this subdisk\n"); 228c1b3d7c5SThomas E. Spanjaard } 229c1b3d7c5SThomas E. Spanjaard else 230e3869ec7SSascha Wildner kprintf("DOWN no device found for this subdisk\n"); 231c1b3d7c5SThomas E. Spanjaard } 232a688b15cSMatthew Dillon 233a688b15cSMatthew Dillon disk_setdiskinfo(&rdp->disk, &info); 234c1b3d7c5SThomas E. Spanjaard } 235c1b3d7c5SThomas E. Spanjaard 236c1b3d7c5SThomas E. Spanjaard /* 237c1b3d7c5SThomas E. Spanjaard * ATA PseudoRAID ioctl function. Note that this does not need to be adjusted 238c1b3d7c5SThomas E. Spanjaard * to the dev_ops way, because it's just chained from the generic ata ioctl. 239c1b3d7c5SThomas E. Spanjaard */ 240c1b3d7c5SThomas E. Spanjaard static int 241c1b3d7c5SThomas E. Spanjaard ata_raid_ioctl(u_long cmd, caddr_t data) 242c1b3d7c5SThomas E. Spanjaard { 2439243051bSzrj struct ata_ioc_raid_status *status = (struct ata_ioc_raid_status *)data; 244c1b3d7c5SThomas E. Spanjaard struct ata_ioc_raid_config *config = (struct ata_ioc_raid_config *)data; 245c1b3d7c5SThomas E. Spanjaard int *lun = (int *)data; 246c1b3d7c5SThomas E. Spanjaard int error = EOPNOTSUPP; 247c1b3d7c5SThomas E. Spanjaard 248c1b3d7c5SThomas E. Spanjaard switch (cmd) { 249c1b3d7c5SThomas E. Spanjaard case IOCATARAIDSTATUS: 2509243051bSzrj error = ata_raid_status(status); 251c1b3d7c5SThomas E. Spanjaard break; 252c1b3d7c5SThomas E. Spanjaard 253c1b3d7c5SThomas E. Spanjaard case IOCATARAIDCREATE: 254c1b3d7c5SThomas E. Spanjaard error = ata_raid_create(config); 255c1b3d7c5SThomas E. Spanjaard break; 256c1b3d7c5SThomas E. Spanjaard 257c1b3d7c5SThomas E. Spanjaard case IOCATARAIDDELETE: 258c1b3d7c5SThomas E. Spanjaard error = ata_raid_delete(*lun); 259c1b3d7c5SThomas E. Spanjaard break; 260c1b3d7c5SThomas E. Spanjaard 261c1b3d7c5SThomas E. Spanjaard case IOCATARAIDADDSPARE: 262c1b3d7c5SThomas E. Spanjaard error = ata_raid_addspare(config); 263c1b3d7c5SThomas E. Spanjaard break; 264c1b3d7c5SThomas E. Spanjaard 265c1b3d7c5SThomas E. Spanjaard case IOCATARAIDREBUILD: 266c1b3d7c5SThomas E. Spanjaard error = ata_raid_rebuild(*lun); 267c1b3d7c5SThomas E. Spanjaard break; 268c1b3d7c5SThomas E. Spanjaard } 269c1b3d7c5SThomas E. Spanjaard return error; 270c1b3d7c5SThomas E. Spanjaard } 271c1b3d7c5SThomas E. Spanjaard 272b106cb48SMatthew Dillon static int 273b106cb48SMatthew Dillon ata_raid_flush(struct ar_softc *rdp, struct bio *bp) 274b106cb48SMatthew Dillon { 275b106cb48SMatthew Dillon struct ata_request *request; 276b106cb48SMatthew Dillon device_t dev; 277a43d9d72SSascha Wildner int disk; 278b106cb48SMatthew Dillon 27960233e58SSascha Wildner bp->bio_driver_info = NULL; 280b106cb48SMatthew Dillon 281b106cb48SMatthew Dillon for (disk = 0; disk < rdp->total_disks; disk++) { 282b106cb48SMatthew Dillon if ((dev = rdp->disks[disk].dev) != NULL) 283b106cb48SMatthew Dillon bp->bio_driver_info = (void *)((intptr_t)bp->bio_driver_info + 1); 284b106cb48SMatthew Dillon } 285b106cb48SMatthew Dillon for (disk = 0; disk < rdp->total_disks; disk++) { 286b106cb48SMatthew Dillon if ((dev = rdp->disks[disk].dev) == NULL) 287b106cb48SMatthew Dillon continue; 288b106cb48SMatthew Dillon if (!(request = ata_raid_init_request(rdp, bp))) 289b106cb48SMatthew Dillon return ENOMEM; 290b106cb48SMatthew Dillon request->dev = dev; 291b106cb48SMatthew Dillon request->u.ata.command = ATA_FLUSHCACHE; 292b106cb48SMatthew Dillon request->u.ata.lba = 0; 293b106cb48SMatthew Dillon request->u.ata.count = 0; 294b106cb48SMatthew Dillon request->u.ata.feature = 0; 295*bb15467aSzrj request->timeout = 1; /* ATA_DEFAULT_TIMEOUT */ 296b106cb48SMatthew Dillon request->retries = 0; 297b106cb48SMatthew Dillon request->flags |= ATA_R_ORDERED | ATA_R_DIRECT; 298b106cb48SMatthew Dillon ata_queue_request(request); 299b106cb48SMatthew Dillon } 300b106cb48SMatthew Dillon return 0; 301b106cb48SMatthew Dillon } 302b106cb48SMatthew Dillon 303c1b3d7c5SThomas E. Spanjaard /* 304c1b3d7c5SThomas E. Spanjaard * XXX TGEN there are a lot of offset -> block number conversions going on 305c1b3d7c5SThomas E. Spanjaard * here, which is suboptimal. 306c1b3d7c5SThomas E. Spanjaard */ 307c1b3d7c5SThomas E. Spanjaard static int 308c1b3d7c5SThomas E. Spanjaard ata_raid_strategy(struct dev_strategy_args *ap) 309c1b3d7c5SThomas E. Spanjaard { 3105e8604ceSThomas E. Spanjaard struct ar_softc *rdp = ap->a_head.a_dev->si_drv1; 311c1b3d7c5SThomas E. Spanjaard struct bio *bp = ap->a_bio; 312c1b3d7c5SThomas E. Spanjaard struct buf *bbp = bp->bio_buf; 313c1b3d7c5SThomas E. Spanjaard struct ata_request *request; 314c1b3d7c5SThomas E. Spanjaard caddr_t data; 315c1b3d7c5SThomas E. Spanjaard u_int64_t blkno, lba, blk = 0; 316c1b3d7c5SThomas E. Spanjaard int count, chunk, drv, par = 0, change = 0; 317c1b3d7c5SThomas E. Spanjaard 318b106cb48SMatthew Dillon if (bbp->b_cmd == BUF_CMD_FLUSH) { 319b106cb48SMatthew Dillon int error; 320b106cb48SMatthew Dillon 321b106cb48SMatthew Dillon error = ata_raid_flush(rdp, bp); 322*bb15467aSzrj if (error != 0) 323*bb15467aSzrj biofinish(bp, NULL, error); 324b106cb48SMatthew Dillon return(0); 325b106cb48SMatthew Dillon } 326b106cb48SMatthew Dillon 327c1b3d7c5SThomas E. Spanjaard if (!(rdp->status & AR_S_READY) || 328c1b3d7c5SThomas E. Spanjaard (bbp->b_cmd != BUF_CMD_READ && bbp->b_cmd != BUF_CMD_WRITE)) { 329*bb15467aSzrj biofinish(bp, NULL, EIO); 3305e8604ceSThomas E. Spanjaard return(0); 331c1b3d7c5SThomas E. Spanjaard } 332c1b3d7c5SThomas E. Spanjaard 333c1b3d7c5SThomas E. Spanjaard bbp->b_resid = bbp->b_bcount; 334c1b3d7c5SThomas E. Spanjaard for (count = howmany(bbp->b_bcount, DEV_BSIZE), 335c1b3d7c5SThomas E. Spanjaard /* bio_offset is byte granularity, convert */ 336c1b3d7c5SThomas E. Spanjaard blkno = (u_int64_t)(bp->bio_offset >> DEV_BSHIFT), 337c1b3d7c5SThomas E. Spanjaard data = bbp->b_data; 338c1b3d7c5SThomas E. Spanjaard count > 0; 339c1b3d7c5SThomas E. Spanjaard count -= chunk, blkno += chunk, data += (chunk * DEV_BSIZE)) { 340c1b3d7c5SThomas E. Spanjaard 341c1b3d7c5SThomas E. Spanjaard switch (rdp->type) { 342c1b3d7c5SThomas E. Spanjaard case AR_T_RAID1: 343c1b3d7c5SThomas E. Spanjaard drv = 0; 344c1b3d7c5SThomas E. Spanjaard lba = blkno; 345c1b3d7c5SThomas E. Spanjaard chunk = count; 346c1b3d7c5SThomas E. Spanjaard break; 347c1b3d7c5SThomas E. Spanjaard 348c1b3d7c5SThomas E. Spanjaard case AR_T_JBOD: 349c1b3d7c5SThomas E. Spanjaard case AR_T_SPAN: 350c1b3d7c5SThomas E. Spanjaard drv = 0; 351c1b3d7c5SThomas E. Spanjaard lba = blkno; 352c1b3d7c5SThomas E. Spanjaard while (lba >= rdp->disks[drv].sectors) 353c1b3d7c5SThomas E. Spanjaard lba -= rdp->disks[drv++].sectors; 354c1b3d7c5SThomas E. Spanjaard chunk = min(rdp->disks[drv].sectors - lba, count); 355c1b3d7c5SThomas E. Spanjaard break; 356c1b3d7c5SThomas E. Spanjaard 357c1b3d7c5SThomas E. Spanjaard case AR_T_RAID0: 358c1b3d7c5SThomas E. Spanjaard case AR_T_RAID01: 359c1b3d7c5SThomas E. Spanjaard chunk = blkno % rdp->interleave; 360c1b3d7c5SThomas E. Spanjaard drv = (blkno / rdp->interleave) % rdp->width; 361c1b3d7c5SThomas E. Spanjaard lba = (((blkno/rdp->interleave)/rdp->width)*rdp->interleave)+chunk; 362c1b3d7c5SThomas E. Spanjaard chunk = min(count, rdp->interleave - chunk); 363c1b3d7c5SThomas E. Spanjaard break; 364c1b3d7c5SThomas E. Spanjaard 365c1b3d7c5SThomas E. Spanjaard case AR_T_RAID5: 366c1b3d7c5SThomas E. Spanjaard drv = (blkno / rdp->interleave) % (rdp->width - 1); 367c1b3d7c5SThomas E. Spanjaard par = rdp->width - 1 - 368c1b3d7c5SThomas E. Spanjaard (blkno / (rdp->interleave * (rdp->width - 1))) % rdp->width; 369c1b3d7c5SThomas E. Spanjaard if (drv >= par) 370c1b3d7c5SThomas E. Spanjaard drv++; 371c1b3d7c5SThomas E. Spanjaard lba = ((blkno/rdp->interleave)/(rdp->width-1))*(rdp->interleave) + 372c1b3d7c5SThomas E. Spanjaard ((blkno%(rdp->interleave*(rdp->width-1)))%rdp->interleave); 373c1b3d7c5SThomas E. Spanjaard chunk = min(count, rdp->interleave - (lba % rdp->interleave)); 374c1b3d7c5SThomas E. Spanjaard break; 375c1b3d7c5SThomas E. Spanjaard 376c1b3d7c5SThomas E. Spanjaard default: 377e3869ec7SSascha Wildner kprintf("ar%d: unknown array type in ata_raid_strategy\n", rdp->lun); 378*bb15467aSzrj biofinish(bp, NULL, EIO); 3795e8604ceSThomas E. Spanjaard return(0); 380c1b3d7c5SThomas E. Spanjaard } 381c1b3d7c5SThomas E. Spanjaard 382c1b3d7c5SThomas E. Spanjaard /* offset on all but "first on HPTv2" */ 383c1b3d7c5SThomas E. Spanjaard if (!(drv == 0 && rdp->format == AR_F_HPTV2_RAID)) 384c1b3d7c5SThomas E. Spanjaard lba += rdp->offset_sectors; 385c1b3d7c5SThomas E. Spanjaard 386c1b3d7c5SThomas E. Spanjaard if (!(request = ata_raid_init_request(rdp, bp))) { 387*bb15467aSzrj biofinish(bp, NULL, EIO); 3885e8604ceSThomas E. Spanjaard return(0); 389c1b3d7c5SThomas E. Spanjaard } 390c1b3d7c5SThomas E. Spanjaard request->data = data; 391c1b3d7c5SThomas E. Spanjaard request->bytecount = chunk * DEV_BSIZE; 392c1b3d7c5SThomas E. Spanjaard request->u.ata.lba = lba; 393c1b3d7c5SThomas E. Spanjaard request->u.ata.count = request->bytecount / DEV_BSIZE; 394c1b3d7c5SThomas E. Spanjaard 39589be26e1SSascha Wildner devstat_start_transaction(&rdp->devstat); 396c1b3d7c5SThomas E. Spanjaard switch (rdp->type) { 397c1b3d7c5SThomas E. Spanjaard case AR_T_JBOD: 398c1b3d7c5SThomas E. Spanjaard case AR_T_SPAN: 399c1b3d7c5SThomas E. Spanjaard case AR_T_RAID0: 400c1b3d7c5SThomas E. Spanjaard if (((rdp->disks[drv].flags & (AR_DF_PRESENT|AR_DF_ONLINE)) == 401c1b3d7c5SThomas E. Spanjaard (AR_DF_PRESENT|AR_DF_ONLINE) && !rdp->disks[drv].dev)) { 402c1b3d7c5SThomas E. Spanjaard rdp->disks[drv].flags &= ~AR_DF_ONLINE; 403c1b3d7c5SThomas E. Spanjaard ata_raid_config_changed(rdp, 1); 404c1b3d7c5SThomas E. Spanjaard ata_free_request(request); 405*bb15467aSzrj biofinish(bp, NULL, EIO); 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); 431*bb15467aSzrj biofinish(bp, NULL, EIO); 4325e8604ceSThomas E. Spanjaard return(0); 433c1b3d7c5SThomas E. Spanjaard } 434c1b3d7c5SThomas E. Spanjaard 435c1b3d7c5SThomas E. Spanjaard if (rdp->status & AR_S_REBUILDING) 436c1b3d7c5SThomas E. Spanjaard blk = ((lba / rdp->interleave) * rdp->width) * rdp->interleave + 437c1b3d7c5SThomas E. Spanjaard (rdp->interleave * (drv % rdp->width)) + 4383598cc14SSascha Wildner lba % rdp->interleave; 439c1b3d7c5SThomas E. Spanjaard 440c1b3d7c5SThomas E. Spanjaard if (bbp->b_cmd == BUF_CMD_READ) { 441c1b3d7c5SThomas E. Spanjaard int src_online = 442c1b3d7c5SThomas E. Spanjaard (rdp->disks[drv].flags & AR_DF_ONLINE); 443c1b3d7c5SThomas E. Spanjaard int mir_online = 444c1b3d7c5SThomas E. Spanjaard (rdp->disks[drv+rdp->width].flags & AR_DF_ONLINE); 445c1b3d7c5SThomas E. Spanjaard 446c1b3d7c5SThomas E. Spanjaard /* if mirror gone or close to last access on source */ 447c1b3d7c5SThomas E. Spanjaard if (!mir_online || 448c1b3d7c5SThomas E. Spanjaard ((src_online) && 449c1b3d7c5SThomas E. Spanjaard ((u_int64_t)(bp->bio_offset >> DEV_BSHIFT)) >= 450c1b3d7c5SThomas E. Spanjaard (rdp->disks[drv].last_lba - AR_PROXIMITY) && 451c1b3d7c5SThomas E. Spanjaard ((u_int64_t)(bp->bio_offset >> DEV_BSHIFT)) <= 452c1b3d7c5SThomas E. Spanjaard (rdp->disks[drv].last_lba + AR_PROXIMITY))) { 453c1b3d7c5SThomas E. Spanjaard rdp->toggle = 0; 454c1b3d7c5SThomas E. Spanjaard } 455c1b3d7c5SThomas E. Spanjaard /* if source gone or close to last access on mirror */ 456c1b3d7c5SThomas E. Spanjaard else if (!src_online || 457c1b3d7c5SThomas E. Spanjaard ((mir_online) && 458c1b3d7c5SThomas E. Spanjaard ((u_int64_t)(bp->bio_offset >> DEV_BSHIFT)) >= 459c1b3d7c5SThomas E. Spanjaard (rdp->disks[drv+rdp->width].last_lba-AR_PROXIMITY) && 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 drv += rdp->width; 463c1b3d7c5SThomas E. Spanjaard rdp->toggle = 1; 464c1b3d7c5SThomas E. Spanjaard } 465c1b3d7c5SThomas E. Spanjaard /* not close to any previous access, toggle */ 466c1b3d7c5SThomas E. Spanjaard else { 467c1b3d7c5SThomas E. Spanjaard if (rdp->toggle) 468c1b3d7c5SThomas E. Spanjaard rdp->toggle = 0; 469c1b3d7c5SThomas E. Spanjaard else { 470c1b3d7c5SThomas E. Spanjaard drv += rdp->width; 471c1b3d7c5SThomas E. Spanjaard rdp->toggle = 1; 472c1b3d7c5SThomas E. Spanjaard } 473c1b3d7c5SThomas E. Spanjaard } 474c1b3d7c5SThomas E. Spanjaard 475c1b3d7c5SThomas E. Spanjaard if ((rdp->status & AR_S_REBUILDING) && 476c1b3d7c5SThomas E. Spanjaard (blk <= rdp->rebuild_lba) && 477c1b3d7c5SThomas E. Spanjaard ((blk + chunk) > rdp->rebuild_lba)) { 478c1b3d7c5SThomas E. Spanjaard struct ata_composite *composite; 479c1b3d7c5SThomas E. Spanjaard struct ata_request *rebuild; 480c1b3d7c5SThomas E. Spanjaard int this; 481c1b3d7c5SThomas E. Spanjaard 482c1b3d7c5SThomas E. Spanjaard /* figure out what part to rebuild */ 483c1b3d7c5SThomas E. Spanjaard if (drv < rdp->width) 484c1b3d7c5SThomas E. Spanjaard this = drv + rdp->width; 485c1b3d7c5SThomas E. Spanjaard else 486c1b3d7c5SThomas E. Spanjaard this = drv - rdp->width; 487c1b3d7c5SThomas E. Spanjaard 488c1b3d7c5SThomas E. Spanjaard /* do we have a spare to rebuild on ? */ 489c1b3d7c5SThomas E. Spanjaard if (rdp->disks[this].flags & AR_DF_SPARE) { 490c1b3d7c5SThomas E. Spanjaard if ((composite = ata_alloc_composite())) { 491c1b3d7c5SThomas E. Spanjaard if ((rebuild = ata_alloc_request())) { 492c1b3d7c5SThomas E. Spanjaard rdp->rebuild_lba = blk + chunk; 493c1b3d7c5SThomas E. Spanjaard bcopy(request, rebuild, 494c1b3d7c5SThomas E. Spanjaard sizeof(struct ata_request)); 495c1b3d7c5SThomas E. Spanjaard rebuild->this = this; 496c1b3d7c5SThomas E. Spanjaard rebuild->dev = rdp->disks[this].dev; 497c1b3d7c5SThomas E. Spanjaard rebuild->flags &= ~ATA_R_READ; 498c1b3d7c5SThomas E. Spanjaard rebuild->flags |= ATA_R_WRITE; 49915bd3c73SMatthew Dillon lockinit(&composite->lock, "ardfspare", 0, 0); 500c1b3d7c5SThomas E. Spanjaard composite->residual = request->bytecount; 501c1b3d7c5SThomas E. Spanjaard composite->rd_needed |= (1 << drv); 502c1b3d7c5SThomas E. Spanjaard composite->wr_depend |= (1 << drv); 503c1b3d7c5SThomas E. Spanjaard composite->wr_needed |= (1 << this); 504c1b3d7c5SThomas E. Spanjaard composite->request[drv] = request; 505c1b3d7c5SThomas E. Spanjaard composite->request[this] = rebuild; 506c1b3d7c5SThomas E. Spanjaard request->composite = composite; 507c1b3d7c5SThomas E. Spanjaard rebuild->composite = composite; 508c1b3d7c5SThomas E. Spanjaard ata_raid_send_request(rebuild); 509c1b3d7c5SThomas E. Spanjaard } 510c1b3d7c5SThomas E. Spanjaard else { 511c1b3d7c5SThomas E. Spanjaard ata_free_composite(composite); 512e3869ec7SSascha Wildner kprintf("DOH! ata_alloc_request failed!\n"); 513c1b3d7c5SThomas E. Spanjaard } 514c1b3d7c5SThomas E. Spanjaard } 515c1b3d7c5SThomas E. Spanjaard else { 516e3869ec7SSascha Wildner kprintf("DOH! ata_alloc_composite failed!\n"); 517c1b3d7c5SThomas E. Spanjaard } 518c1b3d7c5SThomas E. Spanjaard } 519c1b3d7c5SThomas E. Spanjaard else if (rdp->disks[this].flags & AR_DF_ONLINE) { 520c1b3d7c5SThomas E. Spanjaard /* 521c1b3d7c5SThomas E. Spanjaard * if we got here we are a chunk of a RAID01 that 522c1b3d7c5SThomas E. Spanjaard * does not need a rebuild, but we need to increment 523c1b3d7c5SThomas E. Spanjaard * the rebuild_lba address to get the rebuild to 524c1b3d7c5SThomas E. Spanjaard * move to the next chunk correctly 525c1b3d7c5SThomas E. Spanjaard */ 526c1b3d7c5SThomas E. Spanjaard rdp->rebuild_lba = blk + chunk; 527c1b3d7c5SThomas E. Spanjaard } 528c1b3d7c5SThomas E. Spanjaard else 529e3869ec7SSascha Wildner kprintf("DOH! we didn't find the rebuild part\n"); 530c1b3d7c5SThomas E. Spanjaard } 531c1b3d7c5SThomas E. Spanjaard } 532c1b3d7c5SThomas E. Spanjaard if (bbp->b_cmd == BUF_CMD_WRITE) { 533c1b3d7c5SThomas E. Spanjaard if ((rdp->disks[drv+rdp->width].flags & AR_DF_ONLINE) || 534c1b3d7c5SThomas E. Spanjaard ((rdp->status & AR_S_REBUILDING) && 535c1b3d7c5SThomas E. Spanjaard (rdp->disks[drv+rdp->width].flags & AR_DF_SPARE) && 536c1b3d7c5SThomas E. Spanjaard ((blk < rdp->rebuild_lba) || 537c1b3d7c5SThomas E. Spanjaard ((blk <= rdp->rebuild_lba) && 538c1b3d7c5SThomas E. Spanjaard ((blk + chunk) > rdp->rebuild_lba))))) { 539c1b3d7c5SThomas E. Spanjaard if ((rdp->disks[drv].flags & AR_DF_ONLINE) || 540c1b3d7c5SThomas E. Spanjaard ((rdp->status & AR_S_REBUILDING) && 541c1b3d7c5SThomas E. Spanjaard (rdp->disks[drv].flags & AR_DF_SPARE) && 542c1b3d7c5SThomas E. Spanjaard ((blk < rdp->rebuild_lba) || 543c1b3d7c5SThomas E. Spanjaard ((blk <= rdp->rebuild_lba) && 544c1b3d7c5SThomas E. Spanjaard ((blk + chunk) > rdp->rebuild_lba))))) { 545c1b3d7c5SThomas E. Spanjaard struct ata_request *mirror; 546c1b3d7c5SThomas E. Spanjaard struct ata_composite *composite; 547c1b3d7c5SThomas E. Spanjaard int this = drv + rdp->width; 548c1b3d7c5SThomas E. Spanjaard 549c1b3d7c5SThomas E. Spanjaard if ((composite = ata_alloc_composite())) { 550c1b3d7c5SThomas E. Spanjaard if ((mirror = ata_alloc_request())) { 551c1b3d7c5SThomas E. Spanjaard if ((blk <= rdp->rebuild_lba) && 552c1b3d7c5SThomas E. Spanjaard ((blk + chunk) > rdp->rebuild_lba)) 553c1b3d7c5SThomas E. Spanjaard rdp->rebuild_lba = blk + chunk; 554c1b3d7c5SThomas E. Spanjaard bcopy(request, mirror, 555c1b3d7c5SThomas E. Spanjaard sizeof(struct ata_request)); 556c1b3d7c5SThomas E. Spanjaard mirror->this = this; 557c1b3d7c5SThomas E. Spanjaard mirror->dev = rdp->disks[this].dev; 55815bd3c73SMatthew Dillon lockinit(&composite->lock, "ardfonline", 0, 0); 559c1b3d7c5SThomas E. Spanjaard composite->residual = request->bytecount; 560c1b3d7c5SThomas E. Spanjaard composite->wr_needed |= (1 << drv); 561c1b3d7c5SThomas E. Spanjaard composite->wr_needed |= (1 << this); 562c1b3d7c5SThomas E. Spanjaard composite->request[drv] = request; 563c1b3d7c5SThomas E. Spanjaard composite->request[this] = mirror; 564c1b3d7c5SThomas E. Spanjaard request->composite = composite; 565c1b3d7c5SThomas E. Spanjaard mirror->composite = composite; 566c1b3d7c5SThomas E. Spanjaard ata_raid_send_request(mirror); 567c1b3d7c5SThomas E. Spanjaard rdp->disks[this].last_lba = 568c1b3d7c5SThomas E. Spanjaard (u_int64_t)(bp->bio_offset >> DEV_BSHIFT) + 569c1b3d7c5SThomas E. Spanjaard chunk; 570c1b3d7c5SThomas E. Spanjaard } 571c1b3d7c5SThomas E. Spanjaard else { 572c1b3d7c5SThomas E. Spanjaard ata_free_composite(composite); 573e3869ec7SSascha Wildner kprintf("DOH! ata_alloc_request failed!\n"); 574c1b3d7c5SThomas E. Spanjaard } 575c1b3d7c5SThomas E. Spanjaard } 576c1b3d7c5SThomas E. Spanjaard else { 577e3869ec7SSascha Wildner kprintf("DOH! ata_alloc_composite failed!\n"); 578c1b3d7c5SThomas E. Spanjaard } 579c1b3d7c5SThomas E. Spanjaard } 580c1b3d7c5SThomas E. Spanjaard else 581c1b3d7c5SThomas E. Spanjaard drv += rdp->width; 582c1b3d7c5SThomas E. Spanjaard } 583c1b3d7c5SThomas E. Spanjaard } 584c1b3d7c5SThomas E. Spanjaard request->this = drv; 585c1b3d7c5SThomas E. Spanjaard request->dev = rdp->disks[request->this].dev; 586c1b3d7c5SThomas E. Spanjaard ata_raid_send_request(request); 587d438c7c2SThomas E. Spanjaard rdp->disks[request->this].last_lba = 5881a215404SYONETANI Tomokazu ((u_int64_t)(bp->bio_offset) >> DEV_BSHIFT) + chunk; 589c1b3d7c5SThomas E. Spanjaard break; 590c1b3d7c5SThomas E. Spanjaard 591c1b3d7c5SThomas E. Spanjaard case AR_T_RAID5: 592c1b3d7c5SThomas E. Spanjaard if (((rdp->disks[drv].flags & (AR_DF_PRESENT|AR_DF_ONLINE)) == 593c1b3d7c5SThomas E. Spanjaard (AR_DF_PRESENT|AR_DF_ONLINE) && !rdp->disks[drv].dev)) { 594c1b3d7c5SThomas E. Spanjaard rdp->disks[drv].flags &= ~AR_DF_ONLINE; 595c1b3d7c5SThomas E. Spanjaard change = 1; 596c1b3d7c5SThomas E. Spanjaard } 597c1b3d7c5SThomas E. Spanjaard if (((rdp->disks[par].flags & (AR_DF_PRESENT|AR_DF_ONLINE)) == 598c1b3d7c5SThomas E. Spanjaard (AR_DF_PRESENT|AR_DF_ONLINE) && !rdp->disks[par].dev)) { 599c1b3d7c5SThomas E. Spanjaard rdp->disks[par].flags &= ~AR_DF_ONLINE; 600c1b3d7c5SThomas E. Spanjaard change = 1; 601c1b3d7c5SThomas E. Spanjaard } 602c1b3d7c5SThomas E. Spanjaard if (change) 603c1b3d7c5SThomas E. Spanjaard ata_raid_config_changed(rdp, 1); 604c1b3d7c5SThomas E. Spanjaard if (!(rdp->status & AR_S_READY)) { 605c1b3d7c5SThomas E. Spanjaard ata_free_request(request); 606*bb15467aSzrj biofinish(bp, NULL, EIO); 6075e8604ceSThomas E. Spanjaard return(0); 608c1b3d7c5SThomas E. Spanjaard } 609c1b3d7c5SThomas E. Spanjaard if (rdp->status & AR_S_DEGRADED) { 610c1b3d7c5SThomas E. Spanjaard /* do the XOR game if possible */ 611c1b3d7c5SThomas E. Spanjaard } 612c1b3d7c5SThomas E. Spanjaard else { 613c1b3d7c5SThomas E. Spanjaard request->this = drv; 614c1b3d7c5SThomas E. Spanjaard request->dev = rdp->disks[request->this].dev; 615c1b3d7c5SThomas E. Spanjaard if (bbp->b_cmd == BUF_CMD_READ) { 616c1b3d7c5SThomas E. Spanjaard ata_raid_send_request(request); 617c1b3d7c5SThomas E. Spanjaard } 618c1b3d7c5SThomas E. Spanjaard if (bbp->b_cmd == BUF_CMD_WRITE) { 619c1b3d7c5SThomas E. Spanjaard ata_raid_send_request(request); 620d438c7c2SThomas E. Spanjaard /* 621*bb15467aSzrj * ensure that read-modify-write to each disk is atomic. 622*bb15467aSzrj * couple of copies of request 623*bb15467aSzrj * read old data data from drv 624*bb15467aSzrj * write new data to drv 625*bb15467aSzrj * read smth-smth data from pairs 626*bb15467aSzrj * write old data xor smth-smth data xor data to pairs 627d438c7c2SThomas E. Spanjaard */ 628c1b3d7c5SThomas E. Spanjaard } 629c1b3d7c5SThomas E. Spanjaard } 630c1b3d7c5SThomas E. Spanjaard break; 631c1b3d7c5SThomas E. Spanjaard 632c1b3d7c5SThomas E. Spanjaard default: 633e3869ec7SSascha Wildner kprintf("ar%d: unknown array type in ata_raid_strategy\n", rdp->lun); 634c1b3d7c5SThomas E. Spanjaard } 635c1b3d7c5SThomas E. Spanjaard } 6365e8604ceSThomas E. Spanjaard 6375e8604ceSThomas E. Spanjaard return(0); 638c1b3d7c5SThomas E. Spanjaard } 639c1b3d7c5SThomas E. Spanjaard 640c1b3d7c5SThomas E. Spanjaard static void 641c1b3d7c5SThomas E. Spanjaard ata_raid_done(struct ata_request *request) 642c1b3d7c5SThomas E. Spanjaard { 643c1b3d7c5SThomas E. Spanjaard struct ar_softc *rdp = request->driver; 644c1b3d7c5SThomas E. Spanjaard struct ata_composite *composite = NULL; 645c1b3d7c5SThomas E. Spanjaard struct bio *bp = request->bio; 646c1b3d7c5SThomas E. Spanjaard struct buf *bbp = bp->bio_buf; 647c1b3d7c5SThomas E. Spanjaard int i, mirror, finished = 0; 648c1b3d7c5SThomas E. Spanjaard 649b106cb48SMatthew Dillon if (bbp->b_cmd == BUF_CMD_FLUSH) { 650b106cb48SMatthew Dillon if (bbp->b_error == 0) 651b106cb48SMatthew Dillon bbp->b_error = request->result; 652b106cb48SMatthew Dillon ata_free_request(request); 653b106cb48SMatthew Dillon bp->bio_driver_info = (void *)((intptr_t)bp->bio_driver_info - 1); 654b106cb48SMatthew Dillon if ((intptr_t)bp->bio_driver_info == 0) { 655b106cb48SMatthew Dillon if (bbp->b_error) 656b106cb48SMatthew Dillon bbp->b_flags |= B_ERROR; 657b106cb48SMatthew Dillon biodone(bp); 658b106cb48SMatthew Dillon } 659b106cb48SMatthew Dillon return; 660b106cb48SMatthew Dillon } 661b106cb48SMatthew Dillon 662c1b3d7c5SThomas E. Spanjaard switch (rdp->type) { 663c1b3d7c5SThomas E. Spanjaard case AR_T_JBOD: 664c1b3d7c5SThomas E. Spanjaard case AR_T_SPAN: 665c1b3d7c5SThomas E. Spanjaard case AR_T_RAID0: 666c1b3d7c5SThomas E. Spanjaard if (request->result) { 667c1b3d7c5SThomas E. Spanjaard rdp->disks[request->this].flags &= ~AR_DF_ONLINE; 668c1b3d7c5SThomas E. Spanjaard ata_raid_config_changed(rdp, 1); 669c1b3d7c5SThomas E. Spanjaard bbp->b_error = request->result; 670c1b3d7c5SThomas E. Spanjaard finished = 1; 671c1b3d7c5SThomas E. Spanjaard } 672c1b3d7c5SThomas E. Spanjaard else { 673c1b3d7c5SThomas E. Spanjaard bbp->b_resid -= request->donecount; 674c1b3d7c5SThomas E. Spanjaard if (!bbp->b_resid) 675c1b3d7c5SThomas E. Spanjaard finished = 1; 676c1b3d7c5SThomas E. Spanjaard } 677c1b3d7c5SThomas E. Spanjaard break; 678c1b3d7c5SThomas E. Spanjaard 679c1b3d7c5SThomas E. Spanjaard case AR_T_RAID1: 680c1b3d7c5SThomas E. Spanjaard case AR_T_RAID01: 681c1b3d7c5SThomas E. Spanjaard if (request->this < rdp->width) 682c1b3d7c5SThomas E. Spanjaard mirror = request->this + rdp->width; 683c1b3d7c5SThomas E. Spanjaard else 684c1b3d7c5SThomas E. Spanjaard mirror = request->this - rdp->width; 685c1b3d7c5SThomas E. Spanjaard if (request->result) { 686c1b3d7c5SThomas E. Spanjaard rdp->disks[request->this].flags &= ~AR_DF_ONLINE; 687c1b3d7c5SThomas E. Spanjaard ata_raid_config_changed(rdp, 1); 688c1b3d7c5SThomas E. Spanjaard } 689c1b3d7c5SThomas E. Spanjaard if (rdp->status & AR_S_READY) { 690c1b3d7c5SThomas E. Spanjaard u_int64_t blk = 0; 691c1b3d7c5SThomas E. Spanjaard 692c1b3d7c5SThomas E. Spanjaard if (rdp->status & AR_S_REBUILDING) 693c1b3d7c5SThomas E. Spanjaard blk = ((request->u.ata.lba / rdp->interleave) * rdp->width) * 694c1b3d7c5SThomas E. Spanjaard rdp->interleave + (rdp->interleave * 695c1b3d7c5SThomas E. Spanjaard (request->this % rdp->width)) + 696c1b3d7c5SThomas E. Spanjaard request->u.ata.lba % rdp->interleave; 697c1b3d7c5SThomas E. Spanjaard 698c1b3d7c5SThomas E. Spanjaard if (bbp->b_cmd == BUF_CMD_READ) { 699c1b3d7c5SThomas E. Spanjaard 700c1b3d7c5SThomas E. Spanjaard /* is this a rebuild composite */ 701c1b3d7c5SThomas E. Spanjaard if ((composite = request->composite)) { 70215bd3c73SMatthew Dillon lockmgr(&composite->lock, LK_EXCLUSIVE); 703c1b3d7c5SThomas E. Spanjaard 704c1b3d7c5SThomas E. Spanjaard /* handle the read part of a rebuild composite */ 705c1b3d7c5SThomas E. Spanjaard if (request->flags & ATA_R_READ) { 706c1b3d7c5SThomas E. Spanjaard 707c1b3d7c5SThomas E. Spanjaard /* if read failed array is now broken */ 708c1b3d7c5SThomas E. Spanjaard if (request->result) { 709c1b3d7c5SThomas E. Spanjaard rdp->disks[request->this].flags &= ~AR_DF_ONLINE; 710c1b3d7c5SThomas E. Spanjaard ata_raid_config_changed(rdp, 1); 711c1b3d7c5SThomas E. Spanjaard bbp->b_error = request->result; 712c1b3d7c5SThomas E. Spanjaard rdp->rebuild_lba = blk; 713c1b3d7c5SThomas E. Spanjaard finished = 1; 714c1b3d7c5SThomas E. Spanjaard } 715c1b3d7c5SThomas E. Spanjaard 716c1b3d7c5SThomas E. Spanjaard /* good data, update how far we've gotten */ 717c1b3d7c5SThomas E. Spanjaard else { 718c1b3d7c5SThomas E. Spanjaard bbp->b_resid -= request->donecount; 719c1b3d7c5SThomas E. Spanjaard composite->residual -= request->donecount; 720c1b3d7c5SThomas E. Spanjaard if (!composite->residual) { 721c1b3d7c5SThomas E. Spanjaard if (composite->wr_done & (1 << mirror)) 722c1b3d7c5SThomas E. Spanjaard finished = 1; 723c1b3d7c5SThomas E. Spanjaard } 724c1b3d7c5SThomas E. Spanjaard } 725c1b3d7c5SThomas E. Spanjaard } 726c1b3d7c5SThomas E. Spanjaard 727c1b3d7c5SThomas E. Spanjaard /* handle the write part of a rebuild composite */ 728c1b3d7c5SThomas E. Spanjaard else if (request->flags & ATA_R_WRITE) { 729c1b3d7c5SThomas E. Spanjaard if (composite->rd_done & (1 << mirror)) { 730c1b3d7c5SThomas E. Spanjaard if (request->result) { 731e3869ec7SSascha Wildner kprintf("DOH! rebuild failed\n"); /* XXX SOS */ 732c1b3d7c5SThomas E. Spanjaard rdp->rebuild_lba = blk; 733c1b3d7c5SThomas E. Spanjaard } 734c1b3d7c5SThomas E. Spanjaard if (!composite->residual) 735c1b3d7c5SThomas E. Spanjaard finished = 1; 736c1b3d7c5SThomas E. Spanjaard } 737c1b3d7c5SThomas E. Spanjaard } 73815bd3c73SMatthew Dillon lockmgr(&composite->lock, LK_RELEASE); 739c1b3d7c5SThomas E. Spanjaard } 740c1b3d7c5SThomas E. Spanjaard 741c1b3d7c5SThomas E. Spanjaard /* if read failed retry on the mirror */ 742c1b3d7c5SThomas E. Spanjaard else if (request->result) { 743c1b3d7c5SThomas E. Spanjaard request->dev = rdp->disks[mirror].dev; 744c1b3d7c5SThomas E. Spanjaard request->flags &= ~ATA_R_TIMEOUT; 745c1b3d7c5SThomas E. Spanjaard ata_raid_send_request(request); 746c1b3d7c5SThomas E. Spanjaard return; 747c1b3d7c5SThomas E. Spanjaard } 748c1b3d7c5SThomas E. Spanjaard 749c1b3d7c5SThomas E. Spanjaard /* we have good data */ 750c1b3d7c5SThomas E. Spanjaard else { 751c1b3d7c5SThomas E. Spanjaard bbp->b_resid -= request->donecount; 752c1b3d7c5SThomas E. Spanjaard if (!bbp->b_resid) 753c1b3d7c5SThomas E. Spanjaard finished = 1; 754c1b3d7c5SThomas E. Spanjaard } 755c1b3d7c5SThomas E. Spanjaard } 756c1b3d7c5SThomas E. Spanjaard else if (bbp->b_cmd == BUF_CMD_WRITE) { 757c1b3d7c5SThomas E. Spanjaard /* do we have a mirror or rebuild to deal with ? */ 758c1b3d7c5SThomas E. Spanjaard if ((composite = request->composite)) { 75915bd3c73SMatthew Dillon lockmgr(&composite->lock, LK_EXCLUSIVE); 760c1b3d7c5SThomas E. Spanjaard if (composite->wr_done & (1 << mirror)) { 761c1b3d7c5SThomas E. Spanjaard if (request->result) { 762c1b3d7c5SThomas E. Spanjaard if (composite->request[mirror]->result) { 763e3869ec7SSascha Wildner kprintf("DOH! all disks failed and got here\n"); 764c1b3d7c5SThomas E. Spanjaard bbp->b_error = EIO; 765c1b3d7c5SThomas E. Spanjaard } 766c1b3d7c5SThomas E. Spanjaard if (rdp->status & AR_S_REBUILDING) { 767c1b3d7c5SThomas E. Spanjaard rdp->rebuild_lba = blk; 768e3869ec7SSascha Wildner kprintf("DOH! rebuild failed\n"); /* XXX SOS */ 769c1b3d7c5SThomas E. Spanjaard } 770c1b3d7c5SThomas E. Spanjaard bbp->b_resid -= 771c1b3d7c5SThomas E. Spanjaard composite->request[mirror]->donecount; 772c1b3d7c5SThomas E. Spanjaard composite->residual -= 773c1b3d7c5SThomas E. Spanjaard composite->request[mirror]->donecount; 774c1b3d7c5SThomas E. Spanjaard } 775c1b3d7c5SThomas E. Spanjaard else { 776c1b3d7c5SThomas E. Spanjaard bbp->b_resid -= request->donecount; 777c1b3d7c5SThomas E. Spanjaard composite->residual -= request->donecount; 778c1b3d7c5SThomas E. Spanjaard } 779c1b3d7c5SThomas E. Spanjaard if (!composite->residual) 780c1b3d7c5SThomas E. Spanjaard finished = 1; 781c1b3d7c5SThomas E. Spanjaard } 78215bd3c73SMatthew Dillon lockmgr(&composite->lock, LK_RELEASE); 783c1b3d7c5SThomas E. Spanjaard } 784c1b3d7c5SThomas E. Spanjaard /* no mirror we are done */ 785c1b3d7c5SThomas E. Spanjaard else { 786c1b3d7c5SThomas E. Spanjaard bbp->b_resid -= request->donecount; 787c1b3d7c5SThomas E. Spanjaard if (!bbp->b_resid) 788c1b3d7c5SThomas E. Spanjaard finished = 1; 789c1b3d7c5SThomas E. Spanjaard } 790c1b3d7c5SThomas E. Spanjaard } 791c1b3d7c5SThomas E. Spanjaard } 792c1b3d7c5SThomas E. Spanjaard else { 793c1b3d7c5SThomas E. Spanjaard /* XXX TGEN bbp->b_flags |= B_ERROR; */ 794c1b3d7c5SThomas E. Spanjaard bbp->b_error = request->result; 795c1b3d7c5SThomas E. Spanjaard biodone(bp); 796c1b3d7c5SThomas E. Spanjaard } 797c1b3d7c5SThomas E. Spanjaard break; 798c1b3d7c5SThomas E. Spanjaard 799c1b3d7c5SThomas E. Spanjaard case AR_T_RAID5: 800c1b3d7c5SThomas E. Spanjaard if (request->result) { 801c1b3d7c5SThomas E. Spanjaard rdp->disks[request->this].flags &= ~AR_DF_ONLINE; 802c1b3d7c5SThomas E. Spanjaard ata_raid_config_changed(rdp, 1); 803c1b3d7c5SThomas E. Spanjaard if (rdp->status & AR_S_READY) { 804c1b3d7c5SThomas E. Spanjaard if (bbp->b_cmd == BUF_CMD_READ) { 805c1b3d7c5SThomas E. Spanjaard /* do the XOR game to recover data */ 806c1b3d7c5SThomas E. Spanjaard } 807c1b3d7c5SThomas E. Spanjaard if (bbp->b_cmd == BUF_CMD_WRITE) { 808c1b3d7c5SThomas E. Spanjaard /* if the parity failed we're OK sortof */ 809c1b3d7c5SThomas E. Spanjaard /* otherwise wee need to do the XOR long dance */ 810c1b3d7c5SThomas E. Spanjaard } 811c1b3d7c5SThomas E. Spanjaard finished = 1; 812c1b3d7c5SThomas E. Spanjaard } 813c1b3d7c5SThomas E. Spanjaard else { 814c1b3d7c5SThomas E. Spanjaard /* XXX TGEN bbp->b_flags |= B_ERROR; */ 815c1b3d7c5SThomas E. Spanjaard bbp->b_error = request->result; 816c1b3d7c5SThomas E. Spanjaard biodone(bp); 817c1b3d7c5SThomas E. Spanjaard } 818c1b3d7c5SThomas E. Spanjaard } 819c1b3d7c5SThomas E. Spanjaard else { 820d438c7c2SThomas E. Spanjaard /* did we have an XOR game going ?? */ 821c1b3d7c5SThomas E. Spanjaard bbp->b_resid -= request->donecount; 822c1b3d7c5SThomas E. Spanjaard if (!bbp->b_resid) 823c1b3d7c5SThomas E. Spanjaard finished = 1; 824c1b3d7c5SThomas E. Spanjaard } 825c1b3d7c5SThomas E. Spanjaard break; 826c1b3d7c5SThomas E. Spanjaard 827c1b3d7c5SThomas E. Spanjaard default: 828e3869ec7SSascha Wildner kprintf("ar%d: unknown array type in ata_raid_done\n", rdp->lun); 829c1b3d7c5SThomas E. Spanjaard } 830c1b3d7c5SThomas E. Spanjaard 831c1b3d7c5SThomas E. Spanjaard if (finished) { 832c1b3d7c5SThomas E. Spanjaard if ((rdp->status & AR_S_REBUILDING) && 833c1b3d7c5SThomas E. Spanjaard rdp->rebuild_lba >= rdp->total_sectors) { 834c1b3d7c5SThomas E. Spanjaard int disk; 835c1b3d7c5SThomas E. Spanjaard 836c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < rdp->total_disks; disk++) { 837c1b3d7c5SThomas E. Spanjaard if ((rdp->disks[disk].flags & 838c1b3d7c5SThomas E. Spanjaard (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_SPARE)) == 839c1b3d7c5SThomas E. Spanjaard (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_SPARE)) { 840c1b3d7c5SThomas E. Spanjaard rdp->disks[disk].flags &= ~AR_DF_SPARE; 841c1b3d7c5SThomas E. Spanjaard rdp->disks[disk].flags |= AR_DF_ONLINE; 842c1b3d7c5SThomas E. Spanjaard } 843c1b3d7c5SThomas E. Spanjaard } 844c1b3d7c5SThomas E. Spanjaard rdp->status &= ~AR_S_REBUILDING; 845c1b3d7c5SThomas E. Spanjaard ata_raid_config_changed(rdp, 1); 846c1b3d7c5SThomas E. Spanjaard } 84734c2e953SSascha Wildner devstat_end_transaction_buf(&rdp->devstat, bbp); 848c1b3d7c5SThomas E. Spanjaard if (!bbp->b_resid) 849c1b3d7c5SThomas E. Spanjaard biodone(bp); 850c1b3d7c5SThomas E. Spanjaard } 851c1b3d7c5SThomas E. Spanjaard 852c1b3d7c5SThomas E. Spanjaard if (composite) { 853c1b3d7c5SThomas E. Spanjaard if (finished) { 854c1b3d7c5SThomas E. Spanjaard /* we are done with this composite, free all resources */ 855c1b3d7c5SThomas E. Spanjaard for (i = 0; i < 32; i++) { 856c1b3d7c5SThomas E. Spanjaard if (composite->rd_needed & (1 << i) || 857c1b3d7c5SThomas E. Spanjaard composite->wr_needed & (1 << i)) { 858c1b3d7c5SThomas E. Spanjaard ata_free_request(composite->request[i]); 859c1b3d7c5SThomas E. Spanjaard } 860c1b3d7c5SThomas E. Spanjaard } 86115bd3c73SMatthew Dillon lockuninit(&composite->lock); 862c1b3d7c5SThomas E. Spanjaard ata_free_composite(composite); 863c1b3d7c5SThomas E. Spanjaard } 864c1b3d7c5SThomas E. Spanjaard } 865c1b3d7c5SThomas E. Spanjaard else 866c1b3d7c5SThomas E. Spanjaard ata_free_request(request); 867c1b3d7c5SThomas E. Spanjaard } 868c1b3d7c5SThomas E. Spanjaard 869c1b3d7c5SThomas E. Spanjaard static int 870c1b3d7c5SThomas E. Spanjaard ata_raid_dump(struct dev_dump_args *ap) 871c1b3d7c5SThomas E. Spanjaard { 872c1b3d7c5SThomas E. Spanjaard struct ar_softc *rdp = ap->a_head.a_dev->si_drv1; 8735e8604ceSThomas E. Spanjaard struct buf dbuf; 874c1b3d7c5SThomas E. Spanjaard int error = 0; 875b24cd69cSAlex Hornung int disk; 876c1b3d7c5SThomas E. Spanjaard 877b24cd69cSAlex Hornung if (ap->a_length == 0) { 878b24cd69cSAlex Hornung /* flush subdisk buffers to media */ 879b24cd69cSAlex Hornung for (disk = 0, error = 0; disk < rdp->total_disks; disk++) { 880b24cd69cSAlex Hornung if (rdp->disks[disk].dev) { 881b24cd69cSAlex Hornung error |= ata_controlcmd(rdp->disks[disk].dev, 882b24cd69cSAlex Hornung ATA_FLUSHCACHE, 0, 0, 0); 883b24cd69cSAlex Hornung } 884b24cd69cSAlex Hornung } 885b24cd69cSAlex Hornung return (error ? EIO : 0); 886c1b3d7c5SThomas E. Spanjaard } 887c1b3d7c5SThomas E. Spanjaard 8885e8604ceSThomas E. Spanjaard bzero(&dbuf, sizeof(struct buf)); 8895e8604ceSThomas E. Spanjaard initbufbio(&dbuf); 890b5d7061dSMatthew Dillon BUF_LOCK(&dbuf, LK_EXCLUSIVE); 891c1b3d7c5SThomas E. Spanjaard /* bio_offset is byte granularity, convert block granularity a_blkno */ 892b24cd69cSAlex Hornung dbuf.b_bio1.bio_offset = ap->a_offset; 8935e8604ceSThomas E. Spanjaard dbuf.b_bio1.bio_caller_info1.ptr = (void *)rdp; 894ae8e83e6SMatthew Dillon dbuf.b_bio1.bio_flags |= BIO_SYNC; 895ae8e83e6SMatthew Dillon dbuf.b_bio1.bio_done = biodone_sync; 896b24cd69cSAlex Hornung dbuf.b_bcount = ap->a_length; 897b24cd69cSAlex Hornung dbuf.b_data = ap->a_virtual; 8985e8604ceSThomas E. Spanjaard dbuf.b_cmd = BUF_CMD_WRITE; 8995e8604ceSThomas E. Spanjaard dev_dstrategy(rdp->cdev, &dbuf.b_bio1); 9005e8604ceSThomas E. Spanjaard /* wait for completion, unlock the buffer, check status */ 901ae8e83e6SMatthew Dillon if (biowait(&dbuf.b_bio1, "dumpw")) { 9025e8604ceSThomas E. Spanjaard BUF_UNLOCK(&dbuf); 9035e8604ceSThomas E. Spanjaard return(dbuf.b_error ? dbuf.b_error : EIO); 9045e8604ceSThomas E. Spanjaard } 9055e8604ceSThomas E. Spanjaard BUF_UNLOCK(&dbuf); 906b5d7061dSMatthew Dillon uninitbufbio(&dbuf); 907c1b3d7c5SThomas E. Spanjaard 908b24cd69cSAlex Hornung return 0; 909c1b3d7c5SThomas E. Spanjaard } 910c1b3d7c5SThomas E. Spanjaard 911c1b3d7c5SThomas E. Spanjaard static void 912c1b3d7c5SThomas E. Spanjaard ata_raid_config_changed(struct ar_softc *rdp, int writeback) 913c1b3d7c5SThomas E. Spanjaard { 914c1b3d7c5SThomas E. Spanjaard int disk, count, status; 915c1b3d7c5SThomas E. Spanjaard 91615bd3c73SMatthew Dillon lockmgr(&rdp->lock, LK_EXCLUSIVE); 9179243051bSzrj 918c1b3d7c5SThomas E. Spanjaard /* set default all working mode */ 919c1b3d7c5SThomas E. Spanjaard status = rdp->status; 920c1b3d7c5SThomas E. Spanjaard rdp->status &= ~AR_S_DEGRADED; 921c1b3d7c5SThomas E. Spanjaard rdp->status |= AR_S_READY; 922c1b3d7c5SThomas E. Spanjaard 923c1b3d7c5SThomas E. Spanjaard /* make sure all lost drives are accounted for */ 924c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < rdp->total_disks; disk++) { 925c1b3d7c5SThomas E. Spanjaard if (!(rdp->disks[disk].flags & AR_DF_PRESENT)) 926c1b3d7c5SThomas E. Spanjaard rdp->disks[disk].flags &= ~AR_DF_ONLINE; 927c1b3d7c5SThomas E. Spanjaard } 928c1b3d7c5SThomas E. Spanjaard 929c1b3d7c5SThomas E. Spanjaard /* depending on RAID type figure out our health status */ 930c1b3d7c5SThomas E. Spanjaard switch (rdp->type) { 931c1b3d7c5SThomas E. Spanjaard case AR_T_JBOD: 932c1b3d7c5SThomas E. Spanjaard case AR_T_SPAN: 933c1b3d7c5SThomas E. Spanjaard case AR_T_RAID0: 934c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < rdp->total_disks; disk++) 935c1b3d7c5SThomas E. Spanjaard if (!(rdp->disks[disk].flags & AR_DF_ONLINE)) 936c1b3d7c5SThomas E. Spanjaard rdp->status &= ~AR_S_READY; 937c1b3d7c5SThomas E. Spanjaard break; 938c1b3d7c5SThomas E. Spanjaard 939c1b3d7c5SThomas E. Spanjaard case AR_T_RAID1: 940c1b3d7c5SThomas E. Spanjaard case AR_T_RAID01: 941c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < rdp->width; disk++) { 942c1b3d7c5SThomas E. Spanjaard if (!(rdp->disks[disk].flags & AR_DF_ONLINE) && 943c1b3d7c5SThomas E. Spanjaard !(rdp->disks[disk + rdp->width].flags & AR_DF_ONLINE)) { 944c1b3d7c5SThomas E. Spanjaard rdp->status &= ~AR_S_READY; 945c1b3d7c5SThomas E. Spanjaard } 946c1b3d7c5SThomas E. Spanjaard else if (((rdp->disks[disk].flags & AR_DF_ONLINE) && 947c1b3d7c5SThomas E. Spanjaard !(rdp->disks[disk + rdp->width].flags & AR_DF_ONLINE)) || 948c1b3d7c5SThomas E. Spanjaard (!(rdp->disks[disk].flags & AR_DF_ONLINE) && 949c1b3d7c5SThomas E. Spanjaard (rdp->disks [disk + rdp->width].flags & AR_DF_ONLINE))) { 950c1b3d7c5SThomas E. Spanjaard rdp->status |= AR_S_DEGRADED; 951c1b3d7c5SThomas E. Spanjaard } 952c1b3d7c5SThomas E. Spanjaard } 953c1b3d7c5SThomas E. Spanjaard break; 954c1b3d7c5SThomas E. Spanjaard 955c1b3d7c5SThomas E. Spanjaard case AR_T_RAID5: 956c1b3d7c5SThomas E. Spanjaard for (count = 0, disk = 0; disk < rdp->total_disks; disk++) { 957c1b3d7c5SThomas E. Spanjaard if (!(rdp->disks[disk].flags & AR_DF_ONLINE)) 958c1b3d7c5SThomas E. Spanjaard count++; 959c1b3d7c5SThomas E. Spanjaard } 960c1b3d7c5SThomas E. Spanjaard if (count) { 961c1b3d7c5SThomas E. Spanjaard if (count > 1) 962c1b3d7c5SThomas E. Spanjaard rdp->status &= ~AR_S_READY; 963c1b3d7c5SThomas E. Spanjaard else 964c1b3d7c5SThomas E. Spanjaard rdp->status |= AR_S_DEGRADED; 965c1b3d7c5SThomas E. Spanjaard } 966c1b3d7c5SThomas E. Spanjaard break; 967c1b3d7c5SThomas E. Spanjaard default: 968c1b3d7c5SThomas E. Spanjaard rdp->status &= ~AR_S_READY; 969c1b3d7c5SThomas E. Spanjaard } 970c1b3d7c5SThomas E. Spanjaard 9716b7bbefaSMatthew Dillon /* 9726b7bbefaSMatthew Dillon * Note that when the array breaks so comes up broken we 9736b7bbefaSMatthew Dillon * force a write of the array config to the remaining 9746b7bbefaSMatthew Dillon * drives so that the generation will be incremented past 9756b7bbefaSMatthew Dillon * those of the missing or failed drives (in all cases). 9766b7bbefaSMatthew Dillon */ 977c1b3d7c5SThomas E. Spanjaard if (rdp->status != status) { 9789243051bSzrj 9799243051bSzrj /* raid status has changed, update metadata */ 9809243051bSzrj writeback = 1; 9819243051bSzrj 9829243051bSzrj /* announce we have trouble ahead */ 983c1b3d7c5SThomas E. Spanjaard if (!(rdp->status & AR_S_READY)) { 984e3869ec7SSascha Wildner kprintf("ar%d: FAILURE - %s array broken\n", 985c1b3d7c5SThomas E. Spanjaard rdp->lun, ata_raid_type(rdp)); 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)); 994c1b3d7c5SThomas E. Spanjaard } 995c1b3d7c5SThomas E. Spanjaard } 99615bd3c73SMatthew Dillon lockmgr(&rdp->lock, LK_RELEASE); 997c1b3d7c5SThomas E. Spanjaard if (writeback) 998c1b3d7c5SThomas E. Spanjaard ata_raid_write_metadata(rdp); 999c1b3d7c5SThomas E. Spanjaard 1000c1b3d7c5SThomas E. Spanjaard } 1001c1b3d7c5SThomas E. Spanjaard 1002c1b3d7c5SThomas E. Spanjaard static int 10039243051bSzrj ata_raid_status(struct ata_ioc_raid_status *status) 1004c1b3d7c5SThomas E. Spanjaard { 1005c1b3d7c5SThomas E. Spanjaard struct ar_softc *rdp; 1006c1b3d7c5SThomas E. Spanjaard int i; 1007c1b3d7c5SThomas E. Spanjaard 10089243051bSzrj if (!(rdp = ata_raid_arrays[status->lun])) 1009c1b3d7c5SThomas E. Spanjaard return ENXIO; 1010c1b3d7c5SThomas E. Spanjaard 10119243051bSzrj status->type = rdp->type; 10129243051bSzrj status->total_disks = rdp->total_disks; 1013c1b3d7c5SThomas E. Spanjaard for (i = 0; i < rdp->total_disks; i++ ) { 10149243051bSzrj status->disks[i].state = 0; 10159243051bSzrj if ((rdp->disks[i].flags & AR_DF_PRESENT) && rdp->disks[i].dev) { 10169243051bSzrj status->disks[i].lun = device_get_unit(rdp->disks[i].dev); 10179243051bSzrj if (rdp->disks[i].flags & AR_DF_PRESENT) 10189243051bSzrj status->disks[i].state |= AR_DISK_PRESENT; 10199243051bSzrj if (rdp->disks[i].flags & AR_DF_ONLINE) 10209243051bSzrj status->disks[i].state |= AR_DISK_ONLINE; 10219243051bSzrj if (rdp->disks[i].flags & AR_DF_SPARE) 10229243051bSzrj status->disks[i].state |= AR_DISK_SPARE; 10239243051bSzrj } else 10249243051bSzrj status->disks[i].lun = -1; 1025c1b3d7c5SThomas E. Spanjaard } 10269243051bSzrj status->interleave = rdp->interleave; 10279243051bSzrj status->status = rdp->status; 10289243051bSzrj status->progress = 100 * rdp->rebuild_lba / rdp->total_sectors; 1029c1b3d7c5SThomas E. Spanjaard return 0; 1030c1b3d7c5SThomas E. Spanjaard } 1031c1b3d7c5SThomas E. Spanjaard 1032c1b3d7c5SThomas E. Spanjaard static int 1033c1b3d7c5SThomas E. Spanjaard ata_raid_create(struct ata_ioc_raid_config *config) 1034c1b3d7c5SThomas E. Spanjaard { 1035c1b3d7c5SThomas E. Spanjaard struct ar_softc *rdp; 1036c1b3d7c5SThomas E. Spanjaard device_t subdisk; 1037c1b3d7c5SThomas E. Spanjaard int array, disk; 1038feabea0fSSascha Wildner int ctlr = 0, total_disks = 0; 1039feabea0fSSascha Wildner u_int disk_size = 0; 1040c1b3d7c5SThomas E. Spanjaard 1041c1b3d7c5SThomas E. Spanjaard for (array = 0; array < MAX_ARRAYS; array++) { 1042c1b3d7c5SThomas E. Spanjaard if (!ata_raid_arrays[array]) 1043c1b3d7c5SThomas E. Spanjaard break; 1044c1b3d7c5SThomas E. Spanjaard } 1045c1b3d7c5SThomas E. Spanjaard if (array >= MAX_ARRAYS) 1046c1b3d7c5SThomas E. Spanjaard return ENOSPC; 1047c1b3d7c5SThomas E. Spanjaard 1048978400d3SSascha Wildner rdp = (struct ar_softc*)kmalloc(sizeof(struct ar_softc), M_AR, 1049978400d3SSascha Wildner M_WAITOK | M_ZERO); 1050c1b3d7c5SThomas E. Spanjaard 1051c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < config->total_disks; disk++) { 1052c1b3d7c5SThomas E. Spanjaard if ((subdisk = devclass_get_device(ata_raid_sub_devclass, 1053c1b3d7c5SThomas E. Spanjaard config->disks[disk]))) { 1054c1b3d7c5SThomas E. Spanjaard struct ata_raid_subdisk *ars = device_get_softc(subdisk); 1055c1b3d7c5SThomas E. Spanjaard 1056c1b3d7c5SThomas E. Spanjaard /* is device already assigned to another array ? */ 1057c1b3d7c5SThomas E. Spanjaard if (ars->raid[rdp->volume]) { 1058c1b3d7c5SThomas E. Spanjaard config->disks[disk] = -1; 1059d438c7c2SThomas E. Spanjaard kfree(rdp, M_AR); 1060c1b3d7c5SThomas E. Spanjaard return EBUSY; 1061c1b3d7c5SThomas E. Spanjaard } 1062c1b3d7c5SThomas E. Spanjaard rdp->disks[disk].dev = device_get_parent(subdisk); 1063c1b3d7c5SThomas E. Spanjaard 10649243051bSzrj switch (pci_get_vendor(GRANDPARENT(rdp->disks[disk].dev))) { 1065c1b3d7c5SThomas E. Spanjaard case ATA_HIGHPOINT_ID: 1066c1b3d7c5SThomas E. Spanjaard /* 1067c1b3d7c5SThomas E. Spanjaard * we need some way to decide if it should be v2 or v3 1068c1b3d7c5SThomas E. Spanjaard * for now just use v2 since the v3 BIOS knows how to 1069c1b3d7c5SThomas E. Spanjaard * handle that as well. 1070c1b3d7c5SThomas E. Spanjaard */ 1071c1b3d7c5SThomas E. Spanjaard ctlr = AR_F_HPTV2_RAID; 1072c1b3d7c5SThomas E. Spanjaard rdp->disks[disk].sectors = HPTV3_LBA(rdp->disks[disk].dev); 1073c1b3d7c5SThomas E. Spanjaard break; 1074c1b3d7c5SThomas E. Spanjaard 1075c1b3d7c5SThomas E. Spanjaard case ATA_INTEL_ID: 1076c1b3d7c5SThomas E. Spanjaard ctlr = AR_F_INTEL_RAID; 1077c1b3d7c5SThomas E. Spanjaard rdp->disks[disk].sectors = INTEL_LBA(rdp->disks[disk].dev); 1078c1b3d7c5SThomas E. Spanjaard break; 1079c1b3d7c5SThomas E. Spanjaard 1080c1b3d7c5SThomas E. Spanjaard case ATA_ITE_ID: 1081c1b3d7c5SThomas E. Spanjaard ctlr = AR_F_ITE_RAID; 1082c1b3d7c5SThomas E. Spanjaard rdp->disks[disk].sectors = ITE_LBA(rdp->disks[disk].dev); 1083c1b3d7c5SThomas E. Spanjaard break; 1084c1b3d7c5SThomas E. Spanjaard 1085c1b3d7c5SThomas E. Spanjaard case ATA_JMICRON_ID: 1086c1b3d7c5SThomas E. Spanjaard ctlr = AR_F_JMICRON_RAID; 1087c1b3d7c5SThomas E. Spanjaard rdp->disks[disk].sectors = JMICRON_LBA(rdp->disks[disk].dev); 1088c1b3d7c5SThomas E. Spanjaard break; 1089c1b3d7c5SThomas E. Spanjaard 1090c1b3d7c5SThomas E. Spanjaard case 0: /* XXX SOS cover up for bug in our PCI code */ 1091c1b3d7c5SThomas E. Spanjaard case ATA_PROMISE_ID: 1092c1b3d7c5SThomas E. Spanjaard ctlr = AR_F_PROMISE_RAID; 1093c1b3d7c5SThomas E. Spanjaard rdp->disks[disk].sectors = PROMISE_LBA(rdp->disks[disk].dev); 1094c1b3d7c5SThomas E. Spanjaard break; 1095c1b3d7c5SThomas E. Spanjaard 1096c1b3d7c5SThomas E. Spanjaard case ATA_SIS_ID: 1097c1b3d7c5SThomas E. Spanjaard ctlr = AR_F_SIS_RAID; 1098c1b3d7c5SThomas E. Spanjaard rdp->disks[disk].sectors = SIS_LBA(rdp->disks[disk].dev); 1099c1b3d7c5SThomas E. Spanjaard break; 1100c1b3d7c5SThomas E. Spanjaard 1101c1b3d7c5SThomas E. Spanjaard case ATA_ATI_ID: 1102c1b3d7c5SThomas E. Spanjaard case ATA_VIA_ID: 1103c1b3d7c5SThomas E. Spanjaard ctlr = AR_F_VIA_RAID; 1104c1b3d7c5SThomas E. Spanjaard rdp->disks[disk].sectors = VIA_LBA(rdp->disks[disk].dev); 1105c1b3d7c5SThomas E. Spanjaard break; 1106c1b3d7c5SThomas E. Spanjaard 1107c1b3d7c5SThomas E. Spanjaard default: 1108c1b3d7c5SThomas E. Spanjaard /* XXX SOS 1109c1b3d7c5SThomas E. Spanjaard * right, so here we are, we have an ATA chip and we want 1110c1b3d7c5SThomas E. Spanjaard * to create a RAID and store the metadata. 1111c1b3d7c5SThomas E. Spanjaard * we need to find a way to tell what kind of metadata this 1112c1b3d7c5SThomas E. Spanjaard * hardware's BIOS might be using (good ideas are welcomed) 1113c1b3d7c5SThomas E. Spanjaard * for now we just use our own native FreeBSD format. 1114c1b3d7c5SThomas E. Spanjaard * the only way to get support for the BIOS format is to 1115c1b3d7c5SThomas E. Spanjaard * setup the RAID from there, in that case we pickup the 1116c1b3d7c5SThomas E. Spanjaard * metadata format from the disks (if we support it). 1117c1b3d7c5SThomas E. Spanjaard */ 1118e3869ec7SSascha Wildner kprintf("WARNING!! - not able to determine metadata format\n" 1119c1b3d7c5SThomas E. Spanjaard "WARNING!! - Using FreeBSD PseudoRAID metadata\n" 1120c1b3d7c5SThomas E. Spanjaard "If that is not what you want, use the BIOS to " 1121c1b3d7c5SThomas E. Spanjaard "create the array\n"); 1122c1b3d7c5SThomas E. Spanjaard ctlr = AR_F_FREEBSD_RAID; 1123c1b3d7c5SThomas E. Spanjaard rdp->disks[disk].sectors = PROMISE_LBA(rdp->disks[disk].dev); 1124c1b3d7c5SThomas E. Spanjaard break; 1125c1b3d7c5SThomas E. Spanjaard } 1126c1b3d7c5SThomas E. Spanjaard 1127c1b3d7c5SThomas E. Spanjaard /* we need all disks to be of the same format */ 1128c1b3d7c5SThomas E. Spanjaard if ((rdp->format & AR_F_FORMAT_MASK) && 1129c1b3d7c5SThomas E. Spanjaard (rdp->format & AR_F_FORMAT_MASK) != (ctlr & AR_F_FORMAT_MASK)) { 1130d438c7c2SThomas E. Spanjaard kfree(rdp, M_AR); 1131c1b3d7c5SThomas E. Spanjaard return EXDEV; 1132c1b3d7c5SThomas E. Spanjaard } 1133c1b3d7c5SThomas E. Spanjaard else 1134c1b3d7c5SThomas E. Spanjaard rdp->format = ctlr; 1135c1b3d7c5SThomas E. Spanjaard 1136c1b3d7c5SThomas E. Spanjaard /* use the smallest disk of the lots size */ 1137c1b3d7c5SThomas E. Spanjaard /* gigabyte boundry ??? XXX SOS */ 1138c1b3d7c5SThomas E. Spanjaard if (disk_size) 1139c1b3d7c5SThomas E. Spanjaard disk_size = min(rdp->disks[disk].sectors, disk_size); 1140c1b3d7c5SThomas E. Spanjaard else 1141c1b3d7c5SThomas E. Spanjaard disk_size = rdp->disks[disk].sectors; 1142c1b3d7c5SThomas E. Spanjaard rdp->disks[disk].flags = 1143c1b3d7c5SThomas E. Spanjaard (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_ONLINE); 1144c1b3d7c5SThomas E. Spanjaard 1145c1b3d7c5SThomas E. Spanjaard total_disks++; 1146c1b3d7c5SThomas E. Spanjaard } 1147c1b3d7c5SThomas E. Spanjaard else { 1148c1b3d7c5SThomas E. Spanjaard config->disks[disk] = -1; 1149d438c7c2SThomas E. Spanjaard kfree(rdp, M_AR); 1150c1b3d7c5SThomas E. Spanjaard return ENXIO; 1151c1b3d7c5SThomas E. Spanjaard } 1152c1b3d7c5SThomas E. Spanjaard } 1153c1b3d7c5SThomas E. Spanjaard 1154c1b3d7c5SThomas E. Spanjaard if (total_disks != config->total_disks) { 1155d438c7c2SThomas E. Spanjaard kfree(rdp, M_AR); 1156c1b3d7c5SThomas E. Spanjaard return ENODEV; 1157c1b3d7c5SThomas E. Spanjaard } 1158c1b3d7c5SThomas E. Spanjaard 1159c1b3d7c5SThomas E. Spanjaard switch (config->type) { 1160c1b3d7c5SThomas E. Spanjaard case AR_T_JBOD: 1161c1b3d7c5SThomas E. Spanjaard case AR_T_SPAN: 1162c1b3d7c5SThomas E. Spanjaard case AR_T_RAID0: 1163c1b3d7c5SThomas E. Spanjaard break; 1164c1b3d7c5SThomas E. Spanjaard 1165c1b3d7c5SThomas E. Spanjaard case AR_T_RAID1: 1166c1b3d7c5SThomas E. Spanjaard if (total_disks != 2) { 1167d438c7c2SThomas E. Spanjaard kfree(rdp, M_AR); 1168c1b3d7c5SThomas E. Spanjaard return EPERM; 1169c1b3d7c5SThomas E. Spanjaard } 1170c1b3d7c5SThomas E. Spanjaard break; 1171c1b3d7c5SThomas E. Spanjaard 1172c1b3d7c5SThomas E. Spanjaard case AR_T_RAID01: 1173c1b3d7c5SThomas E. Spanjaard if (total_disks % 2 != 0) { 1174d438c7c2SThomas E. Spanjaard kfree(rdp, M_AR); 1175c1b3d7c5SThomas E. Spanjaard return EPERM; 1176c1b3d7c5SThomas E. Spanjaard } 1177c1b3d7c5SThomas E. Spanjaard break; 1178c1b3d7c5SThomas E. Spanjaard 1179c1b3d7c5SThomas E. Spanjaard case AR_T_RAID5: 1180c1b3d7c5SThomas E. Spanjaard if (total_disks < 3) { 1181d438c7c2SThomas E. Spanjaard kfree(rdp, M_AR); 1182c1b3d7c5SThomas E. Spanjaard return EPERM; 1183c1b3d7c5SThomas E. Spanjaard } 1184c1b3d7c5SThomas E. Spanjaard break; 1185c1b3d7c5SThomas E. Spanjaard 1186c1b3d7c5SThomas E. Spanjaard default: 1187d438c7c2SThomas E. Spanjaard kfree(rdp, M_AR); 1188c1b3d7c5SThomas E. Spanjaard return EOPNOTSUPP; 1189c1b3d7c5SThomas E. Spanjaard } 1190c1b3d7c5SThomas E. Spanjaard rdp->type = config->type; 1191c1b3d7c5SThomas E. Spanjaard rdp->lun = array; 1192c1b3d7c5SThomas E. Spanjaard if (rdp->type == AR_T_RAID0 || rdp->type == AR_T_RAID01 || 1193c1b3d7c5SThomas E. Spanjaard rdp->type == AR_T_RAID5) { 1194c1b3d7c5SThomas E. Spanjaard int bit = 0; 1195c1b3d7c5SThomas E. Spanjaard 1196c1b3d7c5SThomas E. Spanjaard while (config->interleave >>= 1) 1197c1b3d7c5SThomas E. Spanjaard bit++; 1198c1b3d7c5SThomas E. Spanjaard rdp->interleave = 1 << bit; 1199c1b3d7c5SThomas E. Spanjaard } 1200c1b3d7c5SThomas E. Spanjaard rdp->offset_sectors = 0; 1201c1b3d7c5SThomas E. Spanjaard 1202c1b3d7c5SThomas E. Spanjaard /* values that depend on metadata format */ 1203c1b3d7c5SThomas E. Spanjaard switch (rdp->format) { 1204c1b3d7c5SThomas E. Spanjaard case AR_F_ADAPTEC_RAID: 1205c1b3d7c5SThomas E. Spanjaard rdp->interleave = min(max(32, rdp->interleave), 128); /*+*/ 1206c1b3d7c5SThomas E. Spanjaard break; 1207c1b3d7c5SThomas E. Spanjaard 1208c1b3d7c5SThomas E. Spanjaard case AR_F_HPTV2_RAID: 1209c1b3d7c5SThomas E. Spanjaard rdp->interleave = min(max(8, rdp->interleave), 128); /*+*/ 1210c1b3d7c5SThomas E. Spanjaard rdp->offset_sectors = HPTV2_LBA(x) + 1; 1211c1b3d7c5SThomas E. Spanjaard break; 1212c1b3d7c5SThomas E. Spanjaard 1213c1b3d7c5SThomas E. Spanjaard case AR_F_HPTV3_RAID: 1214c1b3d7c5SThomas E. Spanjaard rdp->interleave = min(max(32, rdp->interleave), 4096); /*+*/ 1215c1b3d7c5SThomas E. Spanjaard break; 1216c1b3d7c5SThomas E. Spanjaard 1217c1b3d7c5SThomas E. Spanjaard case AR_F_INTEL_RAID: 1218c1b3d7c5SThomas E. Spanjaard rdp->interleave = min(max(8, rdp->interleave), 256); /*+*/ 1219c1b3d7c5SThomas E. Spanjaard break; 1220c1b3d7c5SThomas E. Spanjaard 1221c1b3d7c5SThomas E. Spanjaard case AR_F_ITE_RAID: 1222c1b3d7c5SThomas E. Spanjaard rdp->interleave = min(max(2, rdp->interleave), 128); /*+*/ 1223c1b3d7c5SThomas E. Spanjaard break; 1224c1b3d7c5SThomas E. Spanjaard 1225c1b3d7c5SThomas E. Spanjaard case AR_F_JMICRON_RAID: 1226c1b3d7c5SThomas E. Spanjaard rdp->interleave = min(max(8, rdp->interleave), 256); /*+*/ 1227c1b3d7c5SThomas E. Spanjaard break; 1228c1b3d7c5SThomas E. Spanjaard 1229c1b3d7c5SThomas E. Spanjaard case AR_F_LSIV2_RAID: 1230c1b3d7c5SThomas E. Spanjaard rdp->interleave = min(max(2, rdp->interleave), 4096); 1231c1b3d7c5SThomas E. Spanjaard break; 1232c1b3d7c5SThomas E. Spanjaard 1233c1b3d7c5SThomas E. Spanjaard case AR_F_LSIV3_RAID: 1234c1b3d7c5SThomas E. Spanjaard rdp->interleave = min(max(2, rdp->interleave), 256); 1235c1b3d7c5SThomas E. Spanjaard break; 1236c1b3d7c5SThomas E. Spanjaard 1237c1b3d7c5SThomas E. Spanjaard case AR_F_PROMISE_RAID: 1238c1b3d7c5SThomas E. Spanjaard rdp->interleave = min(max(2, rdp->interleave), 2048); /*+*/ 1239c1b3d7c5SThomas E. Spanjaard break; 1240c1b3d7c5SThomas E. Spanjaard 1241c1b3d7c5SThomas E. Spanjaard case AR_F_SII_RAID: 1242c1b3d7c5SThomas E. Spanjaard rdp->interleave = min(max(8, rdp->interleave), 256); /*+*/ 1243c1b3d7c5SThomas E. Spanjaard break; 1244c1b3d7c5SThomas E. Spanjaard 1245c1b3d7c5SThomas E. Spanjaard case AR_F_SIS_RAID: 1246c1b3d7c5SThomas E. Spanjaard rdp->interleave = min(max(32, rdp->interleave), 512); /*+*/ 1247c1b3d7c5SThomas E. Spanjaard break; 1248c1b3d7c5SThomas E. Spanjaard 1249c1b3d7c5SThomas E. Spanjaard case AR_F_VIA_RAID: 1250c1b3d7c5SThomas E. Spanjaard rdp->interleave = min(max(8, rdp->interleave), 128); /*+*/ 1251c1b3d7c5SThomas E. Spanjaard break; 1252c1b3d7c5SThomas E. Spanjaard } 1253c1b3d7c5SThomas E. Spanjaard 1254c1b3d7c5SThomas E. Spanjaard rdp->total_disks = total_disks; 1255c1b3d7c5SThomas E. Spanjaard rdp->width = total_disks / (rdp->type & (AR_RAID1 | AR_T_RAID01) ? 2 : 1); 1256feabea0fSSascha Wildner rdp->total_sectors = 1257feabea0fSSascha Wildner (uint64_t)disk_size * (rdp->width - (rdp->type == AR_RAID5)); 1258c1b3d7c5SThomas E. Spanjaard rdp->heads = 255; 1259c1b3d7c5SThomas E. Spanjaard rdp->sectors = 63; 1260c1b3d7c5SThomas E. Spanjaard rdp->cylinders = rdp->total_sectors / (255 * 63); 1261c1b3d7c5SThomas E. Spanjaard rdp->rebuild_lba = 0; 1262c1b3d7c5SThomas E. Spanjaard rdp->status |= AR_S_READY; 1263c1b3d7c5SThomas E. Spanjaard 1264c1b3d7c5SThomas E. Spanjaard /* we are committed to this array, grap the subdisks */ 1265c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < config->total_disks; disk++) { 1266c1b3d7c5SThomas E. Spanjaard if ((subdisk = devclass_get_device(ata_raid_sub_devclass, 1267c1b3d7c5SThomas E. Spanjaard config->disks[disk]))) { 1268c1b3d7c5SThomas E. Spanjaard struct ata_raid_subdisk *ars = device_get_softc(subdisk); 1269c1b3d7c5SThomas E. Spanjaard 1270c1b3d7c5SThomas E. Spanjaard ars->raid[rdp->volume] = rdp; 1271c1b3d7c5SThomas E. Spanjaard ars->disk_number[rdp->volume] = disk; 1272c1b3d7c5SThomas E. Spanjaard } 1273c1b3d7c5SThomas E. Spanjaard } 1274c1b3d7c5SThomas E. Spanjaard ata_raid_attach(rdp, 1); 1275c1b3d7c5SThomas E. Spanjaard ata_raid_arrays[array] = rdp; 1276c1b3d7c5SThomas E. Spanjaard config->lun = array; 1277c1b3d7c5SThomas E. Spanjaard return 0; 1278c1b3d7c5SThomas E. Spanjaard } 1279c1b3d7c5SThomas E. Spanjaard 1280c1b3d7c5SThomas E. Spanjaard static int 1281c1b3d7c5SThomas E. Spanjaard ata_raid_delete(int array) 1282c1b3d7c5SThomas E. Spanjaard { 1283c1b3d7c5SThomas E. Spanjaard struct ar_softc *rdp; 1284c1b3d7c5SThomas E. Spanjaard device_t subdisk; 1285c1b3d7c5SThomas E. Spanjaard int disk; 1286c1b3d7c5SThomas E. Spanjaard 1287c1b3d7c5SThomas E. Spanjaard if (!(rdp = ata_raid_arrays[array])) 1288c1b3d7c5SThomas E. Spanjaard return ENXIO; 1289c1b3d7c5SThomas E. Spanjaard 1290c1b3d7c5SThomas E. Spanjaard rdp->status &= ~AR_S_READY; 1291d438c7c2SThomas E. Spanjaard disk_destroy(&rdp->disk); 129289be26e1SSascha Wildner devstat_remove_entry(&rdp->devstat); 1293c1b3d7c5SThomas E. Spanjaard 1294c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < rdp->total_disks; disk++) { 1295c1b3d7c5SThomas E. Spanjaard if ((rdp->disks[disk].flags & AR_DF_PRESENT) && rdp->disks[disk].dev) { 1296c1b3d7c5SThomas E. Spanjaard if ((subdisk = devclass_get_device(ata_raid_sub_devclass, 1297c1b3d7c5SThomas E. Spanjaard device_get_unit(rdp->disks[disk].dev)))) { 1298c1b3d7c5SThomas E. Spanjaard struct ata_raid_subdisk *ars = device_get_softc(subdisk); 1299c1b3d7c5SThomas E. Spanjaard 1300c1b3d7c5SThomas E. Spanjaard if (ars->raid[rdp->volume] != rdp) /* XXX SOS */ 1301c1b3d7c5SThomas E. Spanjaard device_printf(subdisk, "DOH! this disk doesn't belong\n"); 1302c1b3d7c5SThomas E. Spanjaard if (ars->disk_number[rdp->volume] != disk) /* XXX SOS */ 1303c1b3d7c5SThomas E. Spanjaard device_printf(subdisk, "DOH! this disk number is wrong\n"); 1304c1b3d7c5SThomas E. Spanjaard ars->raid[rdp->volume] = NULL; 1305c1b3d7c5SThomas E. Spanjaard ars->disk_number[rdp->volume] = -1; 1306c1b3d7c5SThomas E. Spanjaard } 1307c1b3d7c5SThomas E. Spanjaard rdp->disks[disk].flags = 0; 1308c1b3d7c5SThomas E. Spanjaard } 1309c1b3d7c5SThomas E. Spanjaard } 1310c1b3d7c5SThomas E. Spanjaard ata_raid_wipe_metadata(rdp); 1311c1b3d7c5SThomas E. Spanjaard ata_raid_arrays[array] = NULL; 1312d438c7c2SThomas E. Spanjaard kfree(rdp, M_AR); 1313c1b3d7c5SThomas E. Spanjaard return 0; 1314c1b3d7c5SThomas E. Spanjaard } 1315c1b3d7c5SThomas E. Spanjaard 1316c1b3d7c5SThomas E. Spanjaard static int 1317c1b3d7c5SThomas E. Spanjaard ata_raid_addspare(struct ata_ioc_raid_config *config) 1318c1b3d7c5SThomas E. Spanjaard { 1319c1b3d7c5SThomas E. Spanjaard struct ar_softc *rdp; 1320c1b3d7c5SThomas E. Spanjaard device_t subdisk; 1321c1b3d7c5SThomas E. Spanjaard int disk; 1322c1b3d7c5SThomas E. Spanjaard 1323c1b3d7c5SThomas E. Spanjaard if (!(rdp = ata_raid_arrays[config->lun])) 1324c1b3d7c5SThomas E. Spanjaard return ENXIO; 1325c1b3d7c5SThomas E. Spanjaard if (!(rdp->status & AR_S_DEGRADED) || !(rdp->status & AR_S_READY)) 1326c1b3d7c5SThomas E. Spanjaard return ENXIO; 1327c1b3d7c5SThomas E. Spanjaard if (rdp->status & AR_S_REBUILDING) 1328c1b3d7c5SThomas E. Spanjaard return EBUSY; 1329c1b3d7c5SThomas E. Spanjaard switch (rdp->type) { 1330c1b3d7c5SThomas E. Spanjaard case AR_T_RAID1: 1331c1b3d7c5SThomas E. Spanjaard case AR_T_RAID01: 1332c1b3d7c5SThomas E. Spanjaard case AR_T_RAID5: 1333c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < rdp->total_disks; disk++ ) { 1334c1b3d7c5SThomas E. Spanjaard 1335c1b3d7c5SThomas E. Spanjaard if (((rdp->disks[disk].flags & (AR_DF_PRESENT | AR_DF_ONLINE)) == 1336c1b3d7c5SThomas E. Spanjaard (AR_DF_PRESENT | AR_DF_ONLINE)) && rdp->disks[disk].dev) 1337c1b3d7c5SThomas E. Spanjaard continue; 1338c1b3d7c5SThomas E. Spanjaard 1339c1b3d7c5SThomas E. Spanjaard if ((subdisk = devclass_get_device(ata_raid_sub_devclass, 1340c1b3d7c5SThomas E. Spanjaard config->disks[0] ))) { 1341c1b3d7c5SThomas E. Spanjaard struct ata_raid_subdisk *ars = device_get_softc(subdisk); 1342c1b3d7c5SThomas E. Spanjaard 1343c1b3d7c5SThomas E. Spanjaard if (ars->raid[rdp->volume]) 1344c1b3d7c5SThomas E. Spanjaard return EBUSY; 1345c1b3d7c5SThomas E. Spanjaard 1346c1b3d7c5SThomas E. Spanjaard /* XXX SOS validate size etc etc */ 1347c1b3d7c5SThomas E. Spanjaard ars->raid[rdp->volume] = rdp; 1348c1b3d7c5SThomas E. Spanjaard ars->disk_number[rdp->volume] = disk; 1349c1b3d7c5SThomas E. Spanjaard rdp->disks[disk].dev = device_get_parent(subdisk); 1350c1b3d7c5SThomas E. Spanjaard rdp->disks[disk].flags = 1351c1b3d7c5SThomas E. Spanjaard (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_SPARE); 1352c1b3d7c5SThomas E. Spanjaard 1353c1b3d7c5SThomas E. Spanjaard device_printf(rdp->disks[disk].dev, 1354c1b3d7c5SThomas E. Spanjaard "inserted into ar%d disk%d as spare\n", 1355c1b3d7c5SThomas E. Spanjaard rdp->lun, disk); 1356c1b3d7c5SThomas E. Spanjaard ata_raid_config_changed(rdp, 1); 1357c1b3d7c5SThomas E. Spanjaard return 0; 1358c1b3d7c5SThomas E. Spanjaard } 1359c1b3d7c5SThomas E. Spanjaard } 1360c1b3d7c5SThomas E. Spanjaard return ENXIO; 1361c1b3d7c5SThomas E. Spanjaard 1362c1b3d7c5SThomas E. Spanjaard default: 1363c1b3d7c5SThomas E. Spanjaard return EPERM; 1364c1b3d7c5SThomas E. Spanjaard } 1365c1b3d7c5SThomas E. Spanjaard } 1366c1b3d7c5SThomas E. Spanjaard 1367c1b3d7c5SThomas E. Spanjaard static int 1368c1b3d7c5SThomas E. Spanjaard ata_raid_rebuild(int array) 1369c1b3d7c5SThomas E. Spanjaard { 1370c1b3d7c5SThomas E. Spanjaard struct ar_softc *rdp; 1371c1b3d7c5SThomas E. Spanjaard int disk, count; 1372c1b3d7c5SThomas E. Spanjaard 1373c1b3d7c5SThomas E. Spanjaard if (!(rdp = ata_raid_arrays[array])) 1374c1b3d7c5SThomas E. Spanjaard return ENXIO; 1375c1b3d7c5SThomas E. Spanjaard /* XXX SOS we should lock the rdp softc here */ 1376c1b3d7c5SThomas E. Spanjaard if (!(rdp->status & AR_S_DEGRADED) || !(rdp->status & AR_S_READY)) 1377c1b3d7c5SThomas E. Spanjaard return ENXIO; 1378c1b3d7c5SThomas E. Spanjaard if (rdp->status & AR_S_REBUILDING) 1379c1b3d7c5SThomas E. Spanjaard return EBUSY; 1380c1b3d7c5SThomas E. Spanjaard 1381c1b3d7c5SThomas E. Spanjaard switch (rdp->type) { 1382c1b3d7c5SThomas E. Spanjaard case AR_T_RAID1: 1383c1b3d7c5SThomas E. Spanjaard case AR_T_RAID01: 1384c1b3d7c5SThomas E. Spanjaard case AR_T_RAID5: 1385c1b3d7c5SThomas E. Spanjaard for (count = 0, disk = 0; disk < rdp->total_disks; disk++ ) { 1386c1b3d7c5SThomas E. Spanjaard if (((rdp->disks[disk].flags & 1387c1b3d7c5SThomas E. Spanjaard (AR_DF_PRESENT|AR_DF_ASSIGNED|AR_DF_ONLINE|AR_DF_SPARE)) == 1388c1b3d7c5SThomas E. Spanjaard (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_SPARE)) && 1389c1b3d7c5SThomas E. Spanjaard rdp->disks[disk].dev) { 1390c1b3d7c5SThomas E. Spanjaard count++; 1391c1b3d7c5SThomas E. Spanjaard } 1392c1b3d7c5SThomas E. Spanjaard } 1393c1b3d7c5SThomas E. Spanjaard 1394c1b3d7c5SThomas E. Spanjaard if (count) { 1395c1b3d7c5SThomas E. Spanjaard rdp->rebuild_lba = 0; 1396c1b3d7c5SThomas E. Spanjaard rdp->status |= AR_S_REBUILDING; 1397c1b3d7c5SThomas E. Spanjaard return 0; 1398c1b3d7c5SThomas E. Spanjaard } 1399c1b3d7c5SThomas E. Spanjaard return EIO; 1400c1b3d7c5SThomas E. Spanjaard 1401c1b3d7c5SThomas E. Spanjaard default: 1402c1b3d7c5SThomas E. Spanjaard return EPERM; 1403c1b3d7c5SThomas E. Spanjaard } 1404c1b3d7c5SThomas E. Spanjaard } 1405c1b3d7c5SThomas E. Spanjaard 1406c1b3d7c5SThomas E. Spanjaard static int 1407c1b3d7c5SThomas E. Spanjaard ata_raid_read_metadata(device_t subdisk) 1408c1b3d7c5SThomas E. Spanjaard { 1409c1b3d7c5SThomas E. Spanjaard devclass_t pci_devclass = devclass_find("pci"); 14109243051bSzrj devclass_t atapci_devclass = devclass_find("atapci"); 1411c1b3d7c5SThomas E. Spanjaard devclass_t devclass=device_get_devclass(GRANDPARENT(GRANDPARENT(subdisk))); 1412c1b3d7c5SThomas E. Spanjaard 1413c1b3d7c5SThomas E. Spanjaard /* prioritize vendor native metadata layout if possible */ 14149243051bSzrj if (devclass == pci_devclass || devclass == atapci_devclass) { 14159243051bSzrj switch (pci_get_vendor(GRANDPARENT(device_get_parent(subdisk)))) { 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(×tamp); 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++) { 224460279a3cSSascha Wildner u_int disk_idx = map->disk_idx[disk] & 0xffff; 224560279a3cSSascha Wildner 2246c1b3d7c5SThomas E. Spanjaard raid->disks[disk].dev = NULL; 224760279a3cSSascha Wildner bcopy(meta->disk[disk_idx].serial, 2248c1b3d7c5SThomas E. Spanjaard raid->disks[disk].serial, 2249c1b3d7c5SThomas E. Spanjaard sizeof(raid->disks[disk].serial)); 2250c1b3d7c5SThomas E. Spanjaard raid->disks[disk].sectors = 225160279a3cSSascha Wildner meta->disk[disk_idx].sectors; 2252c1b3d7c5SThomas E. Spanjaard raid->disks[disk].flags = 0; 225360279a3cSSascha Wildner if (meta->disk[disk_idx].flags & INTEL_F_ONLINE) 2254c1b3d7c5SThomas E. Spanjaard raid->disks[disk].flags |= AR_DF_ONLINE; 225560279a3cSSascha Wildner if (meta->disk[disk_idx].flags & INTEL_F_ASSIGNED) 2256c1b3d7c5SThomas E. Spanjaard raid->disks[disk].flags |= AR_DF_ASSIGNED; 225760279a3cSSascha Wildner if (meta->disk[disk_idx].flags & INTEL_F_SPARE) { 2258c1b3d7c5SThomas E. Spanjaard raid->disks[disk].flags &= ~(AR_DF_ONLINE | AR_DF_ASSIGNED); 2259c1b3d7c5SThomas E. Spanjaard raid->disks[disk].flags |= AR_DF_SPARE; 2260c1b3d7c5SThomas E. Spanjaard } 226160279a3cSSascha Wildner if (meta->disk[disk_idx].flags & INTEL_F_DOWN) 2262c1b3d7c5SThomas E. Spanjaard raid->disks[disk].flags &= ~AR_DF_ONLINE; 2263c1b3d7c5SThomas E. Spanjaard } 2264c1b3d7c5SThomas E. Spanjaard } 2265c1b3d7c5SThomas E. Spanjaard if (meta->generation >= raid->generation) { 2266c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < raid->total_disks; disk++) { 2267c1b3d7c5SThomas E. Spanjaard struct ata_device *atadev = device_get_softc(parent); 22689243051bSzrj int len; 2269c1b3d7c5SThomas E. Spanjaard 22709243051bSzrj for (len = 0; len < sizeof(atadev->param.serial); len++) { 22719243051bSzrj if (atadev->param.serial[len] < 0x20) 22729243051bSzrj break; 22739243051bSzrj } 22749243051bSzrj len = (len > sizeof(raid->disks[disk].serial)) ? 22759243051bSzrj len - sizeof(raid->disks[disk].serial) : 0; 22769243051bSzrj if (!strncmp(raid->disks[disk].serial, atadev->param.serial + len, 2277c1b3d7c5SThomas E. Spanjaard sizeof(raid->disks[disk].serial))) { 2278c1b3d7c5SThomas E. Spanjaard raid->disks[disk].dev = parent; 2279c1b3d7c5SThomas E. Spanjaard raid->disks[disk].flags |= (AR_DF_PRESENT | AR_DF_ONLINE); 2280c1b3d7c5SThomas E. Spanjaard ars->raid[raid->volume] = raid; 2281c1b3d7c5SThomas E. Spanjaard ars->disk_number[raid->volume] = disk; 2282c1b3d7c5SThomas E. Spanjaard retval = 1; 2283c1b3d7c5SThomas E. Spanjaard } 2284c1b3d7c5SThomas E. Spanjaard } 2285c1b3d7c5SThomas E. Spanjaard } 2286c1b3d7c5SThomas E. Spanjaard else 2287c1b3d7c5SThomas E. Spanjaard goto intel_out; 2288c1b3d7c5SThomas E. Spanjaard 2289c1b3d7c5SThomas E. Spanjaard if (retval) { 2290c1b3d7c5SThomas E. Spanjaard if (volume < meta->total_volumes) { 2291c1b3d7c5SThomas E. Spanjaard map = (struct intel_raid_mapping *) 2292c1b3d7c5SThomas E. Spanjaard &map->disk_idx[map->total_disks]; 2293c1b3d7c5SThomas E. Spanjaard volume++; 2294c1b3d7c5SThomas E. Spanjaard retval = 0; 2295c1b3d7c5SThomas E. Spanjaard continue; 2296c1b3d7c5SThomas E. Spanjaard } 2297c1b3d7c5SThomas E. Spanjaard break; 2298c1b3d7c5SThomas E. Spanjaard } 2299c1b3d7c5SThomas E. Spanjaard else { 2300d438c7c2SThomas E. Spanjaard kfree(raidp[array], M_AR); 2301c1b3d7c5SThomas E. Spanjaard raidp[array] = NULL; 2302c1b3d7c5SThomas E. Spanjaard if (volume == 2) 2303c1b3d7c5SThomas E. Spanjaard retval = 1; 2304c1b3d7c5SThomas E. Spanjaard } 2305c1b3d7c5SThomas E. Spanjaard } 2306c1b3d7c5SThomas E. Spanjaard 2307c1b3d7c5SThomas E. Spanjaard intel_out: 2308d438c7c2SThomas E. Spanjaard kfree(meta, M_AR); 2309c1b3d7c5SThomas E. Spanjaard return retval; 2310c1b3d7c5SThomas E. Spanjaard } 2311c1b3d7c5SThomas E. Spanjaard 2312c1b3d7c5SThomas E. Spanjaard static int 2313c1b3d7c5SThomas E. Spanjaard ata_raid_intel_write_meta(struct ar_softc *rdp) 2314c1b3d7c5SThomas E. Spanjaard { 2315c1b3d7c5SThomas E. Spanjaard struct intel_raid_conf *meta; 2316c1b3d7c5SThomas E. Spanjaard struct intel_raid_mapping *map; 2317c1b3d7c5SThomas E. Spanjaard struct timeval timestamp; 2318c1b3d7c5SThomas E. Spanjaard u_int32_t checksum, *ptr; 2319c1b3d7c5SThomas E. Spanjaard int count, disk, error = 0; 2320c1b3d7c5SThomas E. Spanjaard char *tmp; 2321c1b3d7c5SThomas E. Spanjaard 2322978400d3SSascha Wildner meta = (struct intel_raid_conf *)kmalloc(1536, M_AR, M_WAITOK | M_ZERO); 2323c1b3d7c5SThomas E. Spanjaard 2324c1b3d7c5SThomas E. Spanjaard rdp->generation++; 23256b7bbefaSMatthew Dillon 23266b7bbefaSMatthew Dillon /* Generate a new config_id if none exists */ 23276b7bbefaSMatthew Dillon if (!rdp->magic_0) { 2328c1b3d7c5SThomas E. Spanjaard microtime(×tamp); 23296b7bbefaSMatthew Dillon rdp->magic_0 = timestamp.tv_sec ^ timestamp.tv_usec; 23306b7bbefaSMatthew Dillon } 2331c1b3d7c5SThomas E. Spanjaard 2332c1b3d7c5SThomas E. Spanjaard bcopy(INTEL_MAGIC, meta->intel_id, sizeof(meta->intel_id)); 2333c1b3d7c5SThomas E. Spanjaard bcopy(INTEL_VERSION_1100, meta->version, sizeof(meta->version)); 23346b7bbefaSMatthew Dillon meta->config_id = rdp->magic_0; 2335c1b3d7c5SThomas E. Spanjaard meta->generation = rdp->generation; 2336c1b3d7c5SThomas E. Spanjaard meta->total_disks = rdp->total_disks; 2337c1b3d7c5SThomas E. Spanjaard meta->total_volumes = 1; /* XXX SOS */ 2338c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < rdp->total_disks; disk++) { 2339c1b3d7c5SThomas E. Spanjaard if (rdp->disks[disk].dev) { 2340c1b3d7c5SThomas E. Spanjaard struct ata_channel *ch = 2341c1b3d7c5SThomas E. Spanjaard device_get_softc(device_get_parent(rdp->disks[disk].dev)); 2342c1b3d7c5SThomas E. Spanjaard struct ata_device *atadev = 2343c1b3d7c5SThomas E. Spanjaard device_get_softc(rdp->disks[disk].dev); 23449243051bSzrj int len; 2345c1b3d7c5SThomas E. Spanjaard 23469243051bSzrj for (len = 0; len < sizeof(atadev->param.serial); len++) { 23479243051bSzrj if (atadev->param.serial[len] < 0x20) 23489243051bSzrj break; 23499243051bSzrj } 23509243051bSzrj len = (len > sizeof(rdp->disks[disk].serial)) ? 23519243051bSzrj len - sizeof(rdp->disks[disk].serial) : 0; 23529243051bSzrj bcopy(atadev->param.serial + len, meta->disk[disk].serial, 2353c1b3d7c5SThomas E. Spanjaard sizeof(rdp->disks[disk].serial)); 2354c1b3d7c5SThomas E. Spanjaard meta->disk[disk].sectors = rdp->disks[disk].sectors; 2355c1b3d7c5SThomas E. Spanjaard meta->disk[disk].id = (ch->unit << 16) | ATA_DEV(atadev->unit); 2356c1b3d7c5SThomas E. Spanjaard } 2357c1b3d7c5SThomas E. Spanjaard else 2358c1b3d7c5SThomas E. Spanjaard meta->disk[disk].sectors = rdp->total_sectors / rdp->width; 2359c1b3d7c5SThomas E. Spanjaard meta->disk[disk].flags = 0; 2360c1b3d7c5SThomas E. Spanjaard if (rdp->disks[disk].flags & AR_DF_SPARE) 2361c1b3d7c5SThomas E. Spanjaard meta->disk[disk].flags |= INTEL_F_SPARE; 2362c1b3d7c5SThomas E. Spanjaard else { 2363c1b3d7c5SThomas E. Spanjaard if (rdp->disks[disk].flags & AR_DF_ONLINE) 2364c1b3d7c5SThomas E. Spanjaard meta->disk[disk].flags |= INTEL_F_ONLINE; 2365c1b3d7c5SThomas E. Spanjaard else 2366c1b3d7c5SThomas E. Spanjaard meta->disk[disk].flags |= INTEL_F_DOWN; 2367c1b3d7c5SThomas E. Spanjaard if (rdp->disks[disk].flags & AR_DF_ASSIGNED) 2368c1b3d7c5SThomas E. Spanjaard meta->disk[disk].flags |= INTEL_F_ASSIGNED; 2369c1b3d7c5SThomas E. Spanjaard } 2370c1b3d7c5SThomas E. Spanjaard } 2371c1b3d7c5SThomas E. Spanjaard map = (struct intel_raid_mapping *)&meta->disk[meta->total_disks]; 2372c1b3d7c5SThomas E. Spanjaard 2373c1b3d7c5SThomas E. Spanjaard bcopy(rdp->name, map->name, sizeof(rdp->name)); 2374c1b3d7c5SThomas E. Spanjaard map->total_sectors = rdp->total_sectors; 2375c1b3d7c5SThomas E. Spanjaard map->state = 12; /* XXX SOS */ 2376c1b3d7c5SThomas E. Spanjaard map->offset = rdp->offset_sectors; 2377c1b3d7c5SThomas E. Spanjaard map->stripe_count = rdp->total_sectors / (rdp->interleave*rdp->total_disks); 2378c1b3d7c5SThomas E. Spanjaard map->stripe_sectors = rdp->interleave; 2379c1b3d7c5SThomas E. Spanjaard map->disk_sectors = rdp->total_sectors / rdp->width; 2380c1b3d7c5SThomas E. Spanjaard map->status = INTEL_S_READY; /* XXX SOS */ 2381c1b3d7c5SThomas E. Spanjaard switch (rdp->type) { 2382c1b3d7c5SThomas E. Spanjaard case AR_T_RAID0: 2383c1b3d7c5SThomas E. Spanjaard map->type = INTEL_T_RAID0; 2384c1b3d7c5SThomas E. Spanjaard break; 2385c1b3d7c5SThomas E. Spanjaard case AR_T_RAID1: 2386c1b3d7c5SThomas E. Spanjaard map->type = INTEL_T_RAID1; 2387c1b3d7c5SThomas E. Spanjaard break; 2388c1b3d7c5SThomas E. Spanjaard case AR_T_RAID01: 2389c1b3d7c5SThomas E. Spanjaard map->type = INTEL_T_RAID1; 2390c1b3d7c5SThomas E. Spanjaard break; 2391c1b3d7c5SThomas E. Spanjaard case AR_T_RAID5: 2392c1b3d7c5SThomas E. Spanjaard map->type = INTEL_T_RAID5; 2393c1b3d7c5SThomas E. Spanjaard break; 2394c1b3d7c5SThomas E. Spanjaard default: 2395d438c7c2SThomas E. Spanjaard kfree(meta, M_AR); 2396c1b3d7c5SThomas E. Spanjaard return ENODEV; 2397c1b3d7c5SThomas E. Spanjaard } 2398c1b3d7c5SThomas E. Spanjaard map->total_disks = rdp->total_disks; 2399c1b3d7c5SThomas E. Spanjaard map->magic[0] = 0x02; 2400c1b3d7c5SThomas E. Spanjaard map->magic[1] = 0xff; 2401c1b3d7c5SThomas E. Spanjaard map->magic[2] = 0x01; 2402c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < rdp->total_disks; disk++) 2403c1b3d7c5SThomas E. Spanjaard map->disk_idx[disk] = disk; 2404c1b3d7c5SThomas E. Spanjaard 2405c1b3d7c5SThomas E. Spanjaard meta->config_size = (char *)&map->disk_idx[disk] - (char *)meta; 2406c1b3d7c5SThomas E. Spanjaard for (checksum = 0, ptr = (u_int32_t *)meta, count = 0; 2407c1b3d7c5SThomas E. Spanjaard count < (meta->config_size / sizeof(u_int32_t)); count++) { 2408c1b3d7c5SThomas E. Spanjaard checksum += *ptr++; 2409c1b3d7c5SThomas E. Spanjaard } 2410c1b3d7c5SThomas E. Spanjaard meta->checksum = checksum; 2411c1b3d7c5SThomas E. Spanjaard 2412c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 2413c1b3d7c5SThomas E. Spanjaard ata_raid_intel_print_meta(meta); 2414c1b3d7c5SThomas E. Spanjaard 2415c1b3d7c5SThomas E. Spanjaard tmp = (char *)meta; 2416c1b3d7c5SThomas E. Spanjaard bcopy(tmp, tmp+1024, 512); 2417c1b3d7c5SThomas E. Spanjaard bcopy(tmp+512, tmp, 1024); 2418c1b3d7c5SThomas E. Spanjaard bzero(tmp+1024, 512); 2419c1b3d7c5SThomas E. Spanjaard 2420c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < rdp->total_disks; disk++) { 2421c1b3d7c5SThomas E. Spanjaard if (rdp->disks[disk].dev) { 2422c1b3d7c5SThomas E. Spanjaard if (ata_raid_rw(rdp->disks[disk].dev, 2423c1b3d7c5SThomas E. Spanjaard INTEL_LBA(rdp->disks[disk].dev), 2424c1b3d7c5SThomas E. Spanjaard meta, 1024, ATA_R_WRITE | ATA_R_DIRECT)) { 2425c1b3d7c5SThomas E. Spanjaard device_printf(rdp->disks[disk].dev, "write metadata failed\n"); 2426c1b3d7c5SThomas E. Spanjaard error = EIO; 2427c1b3d7c5SThomas E. Spanjaard } 2428c1b3d7c5SThomas E. Spanjaard } 2429c1b3d7c5SThomas E. Spanjaard } 2430d438c7c2SThomas E. Spanjaard kfree(meta, M_AR); 2431c1b3d7c5SThomas E. Spanjaard return error; 2432c1b3d7c5SThomas E. Spanjaard } 2433c1b3d7c5SThomas E. Spanjaard 2434c1b3d7c5SThomas E. Spanjaard 2435c1b3d7c5SThomas E. Spanjaard /* Integrated Technology Express Metadata */ 2436c1b3d7c5SThomas E. Spanjaard static int 2437c1b3d7c5SThomas E. Spanjaard ata_raid_ite_read_meta(device_t dev, struct ar_softc **raidp) 2438c1b3d7c5SThomas E. Spanjaard { 2439c1b3d7c5SThomas E. Spanjaard struct ata_raid_subdisk *ars = device_get_softc(dev); 2440c1b3d7c5SThomas E. Spanjaard device_t parent = device_get_parent(dev); 2441c1b3d7c5SThomas E. Spanjaard struct ite_raid_conf *meta; 2442c1b3d7c5SThomas E. Spanjaard struct ar_softc *raid = NULL; 2443c1b3d7c5SThomas E. Spanjaard int array, disk_number, count, retval = 0; 2444c1b3d7c5SThomas E. Spanjaard u_int16_t *ptr; 2445c1b3d7c5SThomas E. Spanjaard 2446978400d3SSascha Wildner meta = (struct ite_raid_conf *)kmalloc(sizeof(struct ite_raid_conf), M_AR, 2447978400d3SSascha Wildner M_WAITOK | M_ZERO); 2448c1b3d7c5SThomas E. Spanjaard 2449c1b3d7c5SThomas E. Spanjaard if (ata_raid_rw(parent, ITE_LBA(parent), 2450c1b3d7c5SThomas E. Spanjaard meta, sizeof(struct ite_raid_conf), ATA_R_READ)) { 2451c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 2452c1b3d7c5SThomas E. Spanjaard device_printf(parent, "ITE read metadata failed\n"); 2453c1b3d7c5SThomas E. Spanjaard goto ite_out; 2454c1b3d7c5SThomas E. Spanjaard } 2455c1b3d7c5SThomas E. Spanjaard 2456c1b3d7c5SThomas E. Spanjaard /* check if this is a ITE RAID struct */ 2457c1b3d7c5SThomas E. Spanjaard for (ptr = (u_int16_t *)meta->ite_id, count = 0; 2458c1b3d7c5SThomas E. Spanjaard count < sizeof(meta->ite_id)/sizeof(uint16_t); count++) 2459c1b3d7c5SThomas E. Spanjaard ptr[count] = be16toh(ptr[count]); 2460c1b3d7c5SThomas E. Spanjaard 2461c1b3d7c5SThomas E. Spanjaard if (strncmp(meta->ite_id, ITE_MAGIC, strlen(ITE_MAGIC))) { 2462c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 2463c1b3d7c5SThomas E. Spanjaard device_printf(parent, "ITE check1 failed\n"); 2464c1b3d7c5SThomas E. Spanjaard goto ite_out; 2465c1b3d7c5SThomas E. Spanjaard } 2466c1b3d7c5SThomas E. Spanjaard 2467c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 2468c1b3d7c5SThomas E. Spanjaard ata_raid_ite_print_meta(meta); 2469c1b3d7c5SThomas E. Spanjaard 2470c1b3d7c5SThomas E. Spanjaard /* now convert ITE metadata into our generic form */ 2471c1b3d7c5SThomas E. Spanjaard for (array = 0; array < MAX_ARRAYS; array++) { 2472c1b3d7c5SThomas E. Spanjaard if ((raid = raidp[array])) { 2473c1b3d7c5SThomas E. Spanjaard if (raid->format != AR_F_ITE_RAID) 2474c1b3d7c5SThomas E. Spanjaard continue; 2475c1b3d7c5SThomas E. Spanjaard if (raid->magic_0 != *((u_int64_t *)meta->timestamp_0)) 2476c1b3d7c5SThomas E. Spanjaard continue; 2477c1b3d7c5SThomas E. Spanjaard } 2478c1b3d7c5SThomas E. Spanjaard 2479c1b3d7c5SThomas E. Spanjaard /* if we dont have a disks timestamp the RAID is invalidated */ 2480c1b3d7c5SThomas E. Spanjaard if (*((u_int64_t *)meta->timestamp_1) == 0) 2481c1b3d7c5SThomas E. Spanjaard goto ite_out; 2482c1b3d7c5SThomas E. Spanjaard 2483c1b3d7c5SThomas E. Spanjaard if (!raid) { 2484d438c7c2SThomas E. Spanjaard raidp[array] = (struct ar_softc *)kmalloc(sizeof(struct ar_softc), 2485d438c7c2SThomas E. Spanjaard M_AR, M_WAITOK | M_ZERO); 2486c1b3d7c5SThomas E. Spanjaard } 2487c1b3d7c5SThomas E. Spanjaard 2488c1b3d7c5SThomas E. Spanjaard switch (meta->type) { 2489c1b3d7c5SThomas E. Spanjaard case ITE_T_RAID0: 2490c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID0; 2491c1b3d7c5SThomas E. Spanjaard raid->width = meta->array_width; 2492c1b3d7c5SThomas E. Spanjaard raid->total_disks = meta->array_width; 2493c1b3d7c5SThomas E. Spanjaard disk_number = meta->disk_number; 2494c1b3d7c5SThomas E. Spanjaard break; 2495c1b3d7c5SThomas E. Spanjaard 2496c1b3d7c5SThomas E. Spanjaard case ITE_T_RAID1: 2497c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID1; 2498c1b3d7c5SThomas E. Spanjaard raid->width = 1; 2499c1b3d7c5SThomas E. Spanjaard raid->total_disks = 2; 2500c1b3d7c5SThomas E. Spanjaard disk_number = meta->disk_number; 2501c1b3d7c5SThomas E. Spanjaard break; 2502c1b3d7c5SThomas E. Spanjaard 2503c1b3d7c5SThomas E. Spanjaard case ITE_T_RAID01: 2504c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID01; 2505c1b3d7c5SThomas E. Spanjaard raid->width = meta->array_width; 2506c1b3d7c5SThomas E. Spanjaard raid->total_disks = 4; 2507c1b3d7c5SThomas E. Spanjaard disk_number = ((meta->disk_number & 0x02) >> 1) | 2508c1b3d7c5SThomas E. Spanjaard ((meta->disk_number & 0x01) << 1); 2509c1b3d7c5SThomas E. Spanjaard break; 2510c1b3d7c5SThomas E. Spanjaard 2511c1b3d7c5SThomas E. Spanjaard case ITE_T_SPAN: 2512c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_SPAN; 2513c1b3d7c5SThomas E. Spanjaard raid->width = 1; 2514c1b3d7c5SThomas E. Spanjaard raid->total_disks = meta->array_width; 2515c1b3d7c5SThomas E. Spanjaard disk_number = meta->disk_number; 2516c1b3d7c5SThomas E. Spanjaard break; 2517c1b3d7c5SThomas E. Spanjaard 2518c1b3d7c5SThomas E. Spanjaard default: 2519c1b3d7c5SThomas E. Spanjaard device_printf(parent, "ITE unknown RAID type 0x%02x\n", meta->type); 2520d438c7c2SThomas E. Spanjaard kfree(raidp[array], M_AR); 2521c1b3d7c5SThomas E. Spanjaard raidp[array] = NULL; 2522c1b3d7c5SThomas E. Spanjaard goto ite_out; 2523c1b3d7c5SThomas E. Spanjaard } 2524c1b3d7c5SThomas E. Spanjaard 2525c1b3d7c5SThomas E. Spanjaard raid->magic_0 = *((u_int64_t *)meta->timestamp_0); 2526c1b3d7c5SThomas E. Spanjaard raid->format = AR_F_ITE_RAID; 2527c1b3d7c5SThomas E. Spanjaard raid->generation = 0; 2528c1b3d7c5SThomas E. Spanjaard raid->interleave = meta->stripe_sectors; 2529c1b3d7c5SThomas E. Spanjaard raid->total_sectors = meta->total_sectors; 2530c1b3d7c5SThomas E. Spanjaard raid->heads = 255; 2531c1b3d7c5SThomas E. Spanjaard raid->sectors = 63; 2532c1b3d7c5SThomas E. Spanjaard raid->cylinders = raid->total_sectors / (63 * 255); 2533c1b3d7c5SThomas E. Spanjaard raid->offset_sectors = 0; 2534c1b3d7c5SThomas E. Spanjaard raid->rebuild_lba = 0; 2535c1b3d7c5SThomas E. Spanjaard raid->lun = array; 2536c1b3d7c5SThomas E. Spanjaard 2537c1b3d7c5SThomas E. Spanjaard raid->disks[disk_number].dev = parent; 2538c1b3d7c5SThomas E. Spanjaard raid->disks[disk_number].sectors = raid->total_sectors / raid->width; 2539c1b3d7c5SThomas E. Spanjaard raid->disks[disk_number].flags = 2540c1b3d7c5SThomas E. Spanjaard (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_ONLINE); 2541c1b3d7c5SThomas E. Spanjaard ars->raid[raid->volume] = raid; 2542c1b3d7c5SThomas E. Spanjaard ars->disk_number[raid->volume] = disk_number; 2543c1b3d7c5SThomas E. Spanjaard retval = 1; 2544c1b3d7c5SThomas E. Spanjaard break; 2545c1b3d7c5SThomas E. Spanjaard } 2546c1b3d7c5SThomas E. Spanjaard ite_out: 2547d438c7c2SThomas E. Spanjaard kfree(meta, M_AR); 2548c1b3d7c5SThomas E. Spanjaard return retval; 2549c1b3d7c5SThomas E. Spanjaard } 2550c1b3d7c5SThomas E. Spanjaard 2551c1b3d7c5SThomas E. Spanjaard /* JMicron Technology Corp Metadata */ 2552c1b3d7c5SThomas E. Spanjaard static int 2553c1b3d7c5SThomas E. Spanjaard ata_raid_jmicron_read_meta(device_t dev, struct ar_softc **raidp) 2554c1b3d7c5SThomas E. Spanjaard { 2555c1b3d7c5SThomas E. Spanjaard struct ata_raid_subdisk *ars = device_get_softc(dev); 2556c1b3d7c5SThomas E. Spanjaard device_t parent = device_get_parent(dev); 2557c1b3d7c5SThomas E. Spanjaard struct jmicron_raid_conf *meta; 2558c1b3d7c5SThomas E. Spanjaard struct ar_softc *raid = NULL; 2559c1b3d7c5SThomas E. Spanjaard u_int16_t checksum, *ptr; 2560c1b3d7c5SThomas E. Spanjaard u_int64_t disk_size; 2561c1b3d7c5SThomas E. Spanjaard int count, array, disk, total_disks, retval = 0; 2562c1b3d7c5SThomas E. Spanjaard 2563978400d3SSascha Wildner meta = (struct jmicron_raid_conf *) 2564978400d3SSascha Wildner kmalloc(sizeof(struct jmicron_raid_conf), M_AR, M_WAITOK | M_ZERO); 2565c1b3d7c5SThomas E. Spanjaard 2566c1b3d7c5SThomas E. Spanjaard if (ata_raid_rw(parent, JMICRON_LBA(parent), 2567c1b3d7c5SThomas E. Spanjaard meta, sizeof(struct jmicron_raid_conf), ATA_R_READ)) { 2568c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 2569c1b3d7c5SThomas E. Spanjaard device_printf(parent, 2570c1b3d7c5SThomas E. Spanjaard "JMicron read metadata failed\n"); 2571c1b3d7c5SThomas E. Spanjaard } 2572c1b3d7c5SThomas E. Spanjaard 2573c1b3d7c5SThomas E. Spanjaard /* check for JMicron signature */ 2574c1b3d7c5SThomas E. Spanjaard if (strncmp(meta->signature, JMICRON_MAGIC, 2)) { 2575c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 2576c1b3d7c5SThomas E. Spanjaard device_printf(parent, "JMicron check1 failed\n"); 2577c1b3d7c5SThomas E. Spanjaard goto jmicron_out; 2578c1b3d7c5SThomas E. Spanjaard } 2579c1b3d7c5SThomas E. Spanjaard 2580c1b3d7c5SThomas E. Spanjaard /* calculate checksum and compare for valid */ 2581c1b3d7c5SThomas E. Spanjaard for (checksum = 0, ptr = (u_int16_t *)meta, count = 0; count < 64; count++) 2582c1b3d7c5SThomas E. Spanjaard checksum += *ptr++; 2583c1b3d7c5SThomas E. Spanjaard if (checksum) { 2584c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 2585c1b3d7c5SThomas E. Spanjaard device_printf(parent, "JMicron check2 failed\n"); 2586c1b3d7c5SThomas E. Spanjaard goto jmicron_out; 2587c1b3d7c5SThomas E. Spanjaard } 2588c1b3d7c5SThomas E. Spanjaard 2589c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 2590c1b3d7c5SThomas E. Spanjaard ata_raid_jmicron_print_meta(meta); 2591c1b3d7c5SThomas E. Spanjaard 2592c1b3d7c5SThomas E. Spanjaard /* now convert JMicron meta into our generic form */ 2593c1b3d7c5SThomas E. Spanjaard for (array = 0; array < MAX_ARRAYS; array++) { 2594c1b3d7c5SThomas E. Spanjaard jmicron_next: 2595c1b3d7c5SThomas E. Spanjaard if (!raidp[array]) { 2596c1b3d7c5SThomas E. Spanjaard raidp[array] = 2597d438c7c2SThomas E. Spanjaard (struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR, 2598d438c7c2SThomas E. Spanjaard M_WAITOK | M_ZERO); 2599c1b3d7c5SThomas E. Spanjaard } 2600c1b3d7c5SThomas E. Spanjaard raid = raidp[array]; 2601c1b3d7c5SThomas E. Spanjaard if (raid->format && (raid->format != AR_F_JMICRON_RAID)) 2602c1b3d7c5SThomas E. Spanjaard continue; 2603c1b3d7c5SThomas E. Spanjaard 2604c1b3d7c5SThomas E. Spanjaard for (total_disks = 0, disk = 0; disk < JM_MAX_DISKS; disk++) { 2605c1b3d7c5SThomas E. Spanjaard if (meta->disks[disk]) { 2606c1b3d7c5SThomas E. Spanjaard if (raid->format == AR_F_JMICRON_RAID) { 2607c1b3d7c5SThomas E. Spanjaard if (bcmp(&meta->disks[disk], 2608c1b3d7c5SThomas E. Spanjaard raid->disks[disk].serial, sizeof(u_int32_t))) { 2609c1b3d7c5SThomas E. Spanjaard array++; 2610c1b3d7c5SThomas E. Spanjaard goto jmicron_next; 2611c1b3d7c5SThomas E. Spanjaard } 2612c1b3d7c5SThomas E. Spanjaard } 2613c1b3d7c5SThomas E. Spanjaard else 2614c1b3d7c5SThomas E. Spanjaard bcopy(&meta->disks[disk], 2615c1b3d7c5SThomas E. Spanjaard raid->disks[disk].serial, sizeof(u_int32_t)); 2616c1b3d7c5SThomas E. Spanjaard total_disks++; 2617c1b3d7c5SThomas E. Spanjaard } 2618c1b3d7c5SThomas E. Spanjaard } 2619c1b3d7c5SThomas E. Spanjaard /* handle spares XXX SOS */ 2620c1b3d7c5SThomas E. Spanjaard 2621c1b3d7c5SThomas E. Spanjaard switch (meta->type) { 2622c1b3d7c5SThomas E. Spanjaard case JM_T_RAID0: 2623c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID0; 2624c1b3d7c5SThomas E. Spanjaard raid->width = total_disks; 2625c1b3d7c5SThomas E. Spanjaard break; 2626c1b3d7c5SThomas E. Spanjaard 2627c1b3d7c5SThomas E. Spanjaard case JM_T_RAID1: 2628c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID1; 2629c1b3d7c5SThomas E. Spanjaard raid->width = 1; 2630c1b3d7c5SThomas E. Spanjaard break; 2631c1b3d7c5SThomas E. Spanjaard 2632c1b3d7c5SThomas E. Spanjaard case JM_T_RAID01: 2633c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID01; 2634c1b3d7c5SThomas E. Spanjaard raid->width = total_disks / 2; 2635c1b3d7c5SThomas E. Spanjaard break; 2636c1b3d7c5SThomas E. Spanjaard 2637c1b3d7c5SThomas E. Spanjaard case JM_T_RAID5: 2638c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID5; 2639c1b3d7c5SThomas E. Spanjaard raid->width = total_disks; 2640c1b3d7c5SThomas E. Spanjaard break; 2641c1b3d7c5SThomas E. Spanjaard 2642c1b3d7c5SThomas E. Spanjaard case JM_T_JBOD: 2643c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_SPAN; 2644c1b3d7c5SThomas E. Spanjaard raid->width = 1; 2645c1b3d7c5SThomas E. Spanjaard break; 2646c1b3d7c5SThomas E. Spanjaard 2647c1b3d7c5SThomas E. Spanjaard default: 2648c1b3d7c5SThomas E. Spanjaard device_printf(parent, 2649c1b3d7c5SThomas E. Spanjaard "JMicron unknown RAID type 0x%02x\n", meta->type); 2650d438c7c2SThomas E. Spanjaard kfree(raidp[array], M_AR); 2651c1b3d7c5SThomas E. Spanjaard raidp[array] = NULL; 2652c1b3d7c5SThomas E. Spanjaard goto jmicron_out; 2653c1b3d7c5SThomas E. Spanjaard } 2654c1b3d7c5SThomas E. Spanjaard disk_size = (meta->disk_sectors_high << 16) + meta->disk_sectors_low; 2655c1b3d7c5SThomas E. Spanjaard raid->format = AR_F_JMICRON_RAID; 2656c1b3d7c5SThomas E. Spanjaard strncpy(raid->name, meta->name, sizeof(meta->name)); 2657c1b3d7c5SThomas E. Spanjaard raid->generation = 0; 2658c1b3d7c5SThomas E. Spanjaard raid->interleave = 2 << meta->stripe_shift; 2659c1b3d7c5SThomas E. Spanjaard raid->total_disks = total_disks; 2660c1b3d7c5SThomas E. Spanjaard raid->total_sectors = disk_size * (raid->width-(raid->type==AR_RAID5)); 2661c1b3d7c5SThomas E. Spanjaard raid->heads = 255; 2662c1b3d7c5SThomas E. Spanjaard raid->sectors = 63; 2663c1b3d7c5SThomas E. Spanjaard raid->cylinders = raid->total_sectors / (63 * 255); 2664c1b3d7c5SThomas E. Spanjaard raid->offset_sectors = meta->offset * 16; 2665c1b3d7c5SThomas E. Spanjaard raid->rebuild_lba = 0; 2666c1b3d7c5SThomas E. Spanjaard raid->lun = array; 2667c1b3d7c5SThomas E. Spanjaard 2668c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < raid->total_disks; disk++) { 2669c1b3d7c5SThomas E. Spanjaard if (meta->disks[disk] == meta->disk_id) { 2670c1b3d7c5SThomas E. Spanjaard raid->disks[disk].dev = parent; 2671c1b3d7c5SThomas E. Spanjaard raid->disks[disk].sectors = disk_size; 2672c1b3d7c5SThomas E. Spanjaard raid->disks[disk].flags = 2673c1b3d7c5SThomas E. Spanjaard (AR_DF_ONLINE | AR_DF_PRESENT | AR_DF_ASSIGNED); 2674c1b3d7c5SThomas E. Spanjaard ars->raid[raid->volume] = raid; 2675c1b3d7c5SThomas E. Spanjaard ars->disk_number[raid->volume] = disk; 2676c1b3d7c5SThomas E. Spanjaard retval = 1; 2677c1b3d7c5SThomas E. Spanjaard break; 2678c1b3d7c5SThomas E. Spanjaard } 2679c1b3d7c5SThomas E. Spanjaard } 2680c1b3d7c5SThomas E. Spanjaard break; 2681c1b3d7c5SThomas E. Spanjaard } 2682c1b3d7c5SThomas E. Spanjaard jmicron_out: 2683d438c7c2SThomas E. Spanjaard kfree(meta, M_AR); 2684c1b3d7c5SThomas E. Spanjaard return retval; 2685c1b3d7c5SThomas E. Spanjaard } 2686c1b3d7c5SThomas E. Spanjaard 2687c1b3d7c5SThomas E. Spanjaard static int 2688c1b3d7c5SThomas E. Spanjaard ata_raid_jmicron_write_meta(struct ar_softc *rdp) 2689c1b3d7c5SThomas E. Spanjaard { 2690c1b3d7c5SThomas E. Spanjaard struct jmicron_raid_conf *meta; 2691c1b3d7c5SThomas E. Spanjaard u_int64_t disk_sectors; 2692c1b3d7c5SThomas E. Spanjaard int disk, error = 0; 2693c1b3d7c5SThomas E. Spanjaard 2694978400d3SSascha Wildner meta = (struct jmicron_raid_conf *) 2695978400d3SSascha Wildner kmalloc(sizeof(struct jmicron_raid_conf), M_AR, M_WAITOK | M_ZERO); 2696c1b3d7c5SThomas E. Spanjaard 2697c1b3d7c5SThomas E. Spanjaard rdp->generation++; 2698c1b3d7c5SThomas E. Spanjaard switch (rdp->type) { 2699c1b3d7c5SThomas E. Spanjaard case AR_T_JBOD: 2700c1b3d7c5SThomas E. Spanjaard meta->type = JM_T_JBOD; 2701c1b3d7c5SThomas E. Spanjaard break; 2702c1b3d7c5SThomas E. Spanjaard 2703c1b3d7c5SThomas E. Spanjaard case AR_T_RAID0: 2704c1b3d7c5SThomas E. Spanjaard meta->type = JM_T_RAID0; 2705c1b3d7c5SThomas E. Spanjaard break; 2706c1b3d7c5SThomas E. Spanjaard 2707c1b3d7c5SThomas E. Spanjaard case AR_T_RAID1: 2708c1b3d7c5SThomas E. Spanjaard meta->type = JM_T_RAID1; 2709c1b3d7c5SThomas E. Spanjaard break; 2710c1b3d7c5SThomas E. Spanjaard 2711c1b3d7c5SThomas E. Spanjaard case AR_T_RAID5: 2712c1b3d7c5SThomas E. Spanjaard meta->type = JM_T_RAID5; 2713c1b3d7c5SThomas E. Spanjaard break; 2714c1b3d7c5SThomas E. Spanjaard 2715c1b3d7c5SThomas E. Spanjaard case AR_T_RAID01: 2716c1b3d7c5SThomas E. Spanjaard meta->type = JM_T_RAID01; 2717c1b3d7c5SThomas E. Spanjaard break; 2718c1b3d7c5SThomas E. Spanjaard 2719c1b3d7c5SThomas E. Spanjaard default: 2720d438c7c2SThomas E. Spanjaard kfree(meta, M_AR); 2721c1b3d7c5SThomas E. Spanjaard return ENODEV; 2722c1b3d7c5SThomas E. Spanjaard } 2723c1b3d7c5SThomas E. Spanjaard bcopy(JMICRON_MAGIC, meta->signature, sizeof(JMICRON_MAGIC)); 2724c1b3d7c5SThomas E. Spanjaard meta->version = JMICRON_VERSION; 2725c1b3d7c5SThomas E. Spanjaard meta->offset = rdp->offset_sectors / 16; 2726c1b3d7c5SThomas E. Spanjaard disk_sectors = rdp->total_sectors / (rdp->width - (rdp->type == AR_RAID5)); 2727c1b3d7c5SThomas E. Spanjaard meta->disk_sectors_low = disk_sectors & 0xffff; 2728c1b3d7c5SThomas E. Spanjaard meta->disk_sectors_high = disk_sectors >> 16; 2729c1b3d7c5SThomas E. Spanjaard strncpy(meta->name, rdp->name, sizeof(meta->name)); 2730c1b3d7c5SThomas E. Spanjaard meta->stripe_shift = ffs(rdp->interleave) - 2; 2731c1b3d7c5SThomas E. Spanjaard 273257e09377SMatthew Dillon for (disk = 0; disk < rdp->total_disks && disk < JM_MAX_DISKS; disk++) { 2733c1b3d7c5SThomas E. Spanjaard if (rdp->disks[disk].serial[0]) 2734c1b3d7c5SThomas E. Spanjaard bcopy(rdp->disks[disk].serial,&meta->disks[disk],sizeof(u_int32_t)); 2735c1b3d7c5SThomas E. Spanjaard else 2736c1b3d7c5SThomas E. Spanjaard meta->disks[disk] = (u_int32_t)(uintptr_t)rdp->disks[disk].dev; 2737c1b3d7c5SThomas E. Spanjaard } 2738c1b3d7c5SThomas E. Spanjaard 2739c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < rdp->total_disks; disk++) { 2740c1b3d7c5SThomas E. Spanjaard if (rdp->disks[disk].dev) { 2741c1b3d7c5SThomas E. Spanjaard u_int16_t checksum = 0, *ptr; 2742c1b3d7c5SThomas E. Spanjaard int count; 2743c1b3d7c5SThomas E. Spanjaard 2744c1b3d7c5SThomas E. Spanjaard meta->disk_id = meta->disks[disk]; 2745c1b3d7c5SThomas E. Spanjaard meta->checksum = 0; 2746c1b3d7c5SThomas E. Spanjaard for (ptr = (u_int16_t *)meta, count = 0; count < 64; count++) 2747c1b3d7c5SThomas E. Spanjaard checksum += *ptr++; 2748c1b3d7c5SThomas E. Spanjaard meta->checksum -= checksum; 2749c1b3d7c5SThomas E. Spanjaard 2750c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 2751c1b3d7c5SThomas E. Spanjaard ata_raid_jmicron_print_meta(meta); 2752c1b3d7c5SThomas E. Spanjaard 2753c1b3d7c5SThomas E. Spanjaard if (ata_raid_rw(rdp->disks[disk].dev, 2754c1b3d7c5SThomas E. Spanjaard JMICRON_LBA(rdp->disks[disk].dev), 2755c1b3d7c5SThomas E. Spanjaard meta, sizeof(struct jmicron_raid_conf), 2756c1b3d7c5SThomas E. Spanjaard ATA_R_WRITE | ATA_R_DIRECT)) { 2757c1b3d7c5SThomas E. Spanjaard device_printf(rdp->disks[disk].dev, "write metadata failed\n"); 2758c1b3d7c5SThomas E. Spanjaard error = EIO; 2759c1b3d7c5SThomas E. Spanjaard } 2760c1b3d7c5SThomas E. Spanjaard } 2761c1b3d7c5SThomas E. Spanjaard } 2762c1b3d7c5SThomas E. Spanjaard /* handle spares XXX SOS */ 2763c1b3d7c5SThomas E. Spanjaard 2764d438c7c2SThomas E. Spanjaard kfree(meta, M_AR); 2765c1b3d7c5SThomas E. Spanjaard return error; 2766c1b3d7c5SThomas E. Spanjaard } 2767c1b3d7c5SThomas E. Spanjaard 2768c1b3d7c5SThomas E. Spanjaard /* LSILogic V2 MegaRAID Metadata */ 2769c1b3d7c5SThomas E. Spanjaard static int 2770c1b3d7c5SThomas E. Spanjaard ata_raid_lsiv2_read_meta(device_t dev, struct ar_softc **raidp) 2771c1b3d7c5SThomas E. Spanjaard { 2772c1b3d7c5SThomas E. Spanjaard struct ata_raid_subdisk *ars = device_get_softc(dev); 2773c1b3d7c5SThomas E. Spanjaard device_t parent = device_get_parent(dev); 2774c1b3d7c5SThomas E. Spanjaard struct lsiv2_raid_conf *meta; 2775c1b3d7c5SThomas E. Spanjaard struct ar_softc *raid = NULL; 2776c1b3d7c5SThomas E. Spanjaard int array, retval = 0; 2777c1b3d7c5SThomas E. Spanjaard 2778978400d3SSascha Wildner meta = (struct lsiv2_raid_conf *)kmalloc(sizeof(struct lsiv2_raid_conf), 2779978400d3SSascha Wildner M_AR, M_WAITOK | M_ZERO); 2780c1b3d7c5SThomas E. Spanjaard 2781c1b3d7c5SThomas E. Spanjaard if (ata_raid_rw(parent, LSIV2_LBA(parent), 2782c1b3d7c5SThomas E. Spanjaard meta, sizeof(struct lsiv2_raid_conf), ATA_R_READ)) { 2783c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 2784c1b3d7c5SThomas E. Spanjaard device_printf(parent, "LSI (v2) read metadata failed\n"); 2785c1b3d7c5SThomas E. Spanjaard goto lsiv2_out; 2786c1b3d7c5SThomas E. Spanjaard } 2787c1b3d7c5SThomas E. Spanjaard 2788c1b3d7c5SThomas E. Spanjaard /* check if this is a LSI RAID struct */ 2789c1b3d7c5SThomas E. Spanjaard if (strncmp(meta->lsi_id, LSIV2_MAGIC, strlen(LSIV2_MAGIC))) { 2790c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 2791c1b3d7c5SThomas E. Spanjaard device_printf(parent, "LSI (v2) check1 failed\n"); 2792c1b3d7c5SThomas E. Spanjaard goto lsiv2_out; 2793c1b3d7c5SThomas E. Spanjaard } 2794c1b3d7c5SThomas E. Spanjaard 2795c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 2796c1b3d7c5SThomas E. Spanjaard ata_raid_lsiv2_print_meta(meta); 2797c1b3d7c5SThomas E. Spanjaard 2798c1b3d7c5SThomas E. Spanjaard /* now convert LSI (v2) config meta into our generic form */ 2799c1b3d7c5SThomas E. Spanjaard for (array = 0; array < MAX_ARRAYS; array++) { 2800c1b3d7c5SThomas E. Spanjaard int raid_entry, conf_entry; 2801c1b3d7c5SThomas E. Spanjaard 2802c1b3d7c5SThomas E. Spanjaard if (!raidp[array + meta->raid_number]) { 2803c1b3d7c5SThomas E. Spanjaard raidp[array + meta->raid_number] = 2804d438c7c2SThomas E. Spanjaard (struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR, 2805d438c7c2SThomas E. Spanjaard M_WAITOK | M_ZERO); 2806c1b3d7c5SThomas E. Spanjaard } 2807c1b3d7c5SThomas E. Spanjaard raid = raidp[array + meta->raid_number]; 2808c1b3d7c5SThomas E. Spanjaard if (raid->format && (raid->format != AR_F_LSIV2_RAID)) 2809c1b3d7c5SThomas E. Spanjaard continue; 2810c1b3d7c5SThomas E. Spanjaard 2811c1b3d7c5SThomas E. Spanjaard if (raid->magic_0 && 2812c1b3d7c5SThomas E. Spanjaard ((raid->magic_0 != meta->timestamp) || 2813c1b3d7c5SThomas E. Spanjaard (raid->magic_1 != meta->raid_number))) 2814c1b3d7c5SThomas E. Spanjaard continue; 2815c1b3d7c5SThomas E. Spanjaard 2816c1b3d7c5SThomas E. Spanjaard array += meta->raid_number; 2817c1b3d7c5SThomas E. Spanjaard 2818c1b3d7c5SThomas E. Spanjaard raid_entry = meta->raid_number; 2819c1b3d7c5SThomas E. Spanjaard conf_entry = (meta->configs[raid_entry].raid.config_offset >> 4) + 2820c1b3d7c5SThomas E. Spanjaard meta->disk_number - 1; 2821c1b3d7c5SThomas E. Spanjaard 2822c1b3d7c5SThomas E. Spanjaard switch (meta->configs[raid_entry].raid.type) { 2823c1b3d7c5SThomas E. Spanjaard case LSIV2_T_RAID0: 2824c1b3d7c5SThomas E. Spanjaard raid->magic_0 = meta->timestamp; 2825c1b3d7c5SThomas E. Spanjaard raid->magic_1 = meta->raid_number; 2826c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID0; 2827c1b3d7c5SThomas E. Spanjaard raid->interleave = meta->configs[raid_entry].raid.stripe_sectors; 2828c1b3d7c5SThomas E. Spanjaard raid->width = meta->configs[raid_entry].raid.array_width; 2829c1b3d7c5SThomas E. Spanjaard break; 2830c1b3d7c5SThomas E. Spanjaard 2831c1b3d7c5SThomas E. Spanjaard case LSIV2_T_RAID1: 2832c1b3d7c5SThomas E. Spanjaard raid->magic_0 = meta->timestamp; 2833c1b3d7c5SThomas E. Spanjaard raid->magic_1 = meta->raid_number; 2834c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID1; 2835c1b3d7c5SThomas E. Spanjaard raid->width = meta->configs[raid_entry].raid.array_width; 2836c1b3d7c5SThomas E. Spanjaard break; 2837c1b3d7c5SThomas E. Spanjaard 2838c1b3d7c5SThomas E. Spanjaard case LSIV2_T_RAID0 | LSIV2_T_RAID1: 2839c1b3d7c5SThomas E. Spanjaard raid->magic_0 = meta->timestamp; 2840c1b3d7c5SThomas E. Spanjaard raid->magic_1 = meta->raid_number; 2841c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID01; 2842c1b3d7c5SThomas E. Spanjaard raid->interleave = meta->configs[raid_entry].raid.stripe_sectors; 2843c1b3d7c5SThomas E. Spanjaard raid->width = meta->configs[raid_entry].raid.array_width; 2844c1b3d7c5SThomas E. Spanjaard break; 2845c1b3d7c5SThomas E. Spanjaard 2846c1b3d7c5SThomas E. Spanjaard default: 2847c1b3d7c5SThomas E. Spanjaard device_printf(parent, "LSI v2 unknown RAID type 0x%02x\n", 2848c1b3d7c5SThomas E. Spanjaard meta->configs[raid_entry].raid.type); 2849d438c7c2SThomas E. Spanjaard kfree(raidp[array], M_AR); 2850c1b3d7c5SThomas E. Spanjaard raidp[array] = NULL; 2851c1b3d7c5SThomas E. Spanjaard goto lsiv2_out; 2852c1b3d7c5SThomas E. Spanjaard } 2853c1b3d7c5SThomas E. Spanjaard 2854c1b3d7c5SThomas E. Spanjaard raid->format = AR_F_LSIV2_RAID; 2855c1b3d7c5SThomas E. Spanjaard raid->generation = 0; 2856c1b3d7c5SThomas E. Spanjaard raid->total_disks = meta->configs[raid_entry].raid.disk_count; 2857c1b3d7c5SThomas E. Spanjaard raid->total_sectors = meta->configs[raid_entry].raid.total_sectors; 2858c1b3d7c5SThomas E. Spanjaard raid->heads = 255; 2859c1b3d7c5SThomas E. Spanjaard raid->sectors = 63; 2860c1b3d7c5SThomas E. Spanjaard raid->cylinders = raid->total_sectors / (63 * 255); 2861c1b3d7c5SThomas E. Spanjaard raid->offset_sectors = 0; 2862c1b3d7c5SThomas E. Spanjaard raid->rebuild_lba = 0; 2863c1b3d7c5SThomas E. Spanjaard raid->lun = array; 2864c1b3d7c5SThomas E. Spanjaard 2865c1b3d7c5SThomas E. Spanjaard if (meta->configs[conf_entry].disk.device != LSIV2_D_NONE) { 2866c1b3d7c5SThomas E. Spanjaard raid->disks[meta->disk_number].dev = parent; 2867c1b3d7c5SThomas E. Spanjaard raid->disks[meta->disk_number].sectors = 2868c1b3d7c5SThomas E. Spanjaard meta->configs[conf_entry].disk.disk_sectors; 2869c1b3d7c5SThomas E. Spanjaard raid->disks[meta->disk_number].flags = 2870c1b3d7c5SThomas E. Spanjaard (AR_DF_ONLINE | AR_DF_PRESENT | AR_DF_ASSIGNED); 2871c1b3d7c5SThomas E. Spanjaard ars->raid[raid->volume] = raid; 2872c1b3d7c5SThomas E. Spanjaard ars->disk_number[raid->volume] = meta->disk_number; 2873c1b3d7c5SThomas E. Spanjaard retval = 1; 2874c1b3d7c5SThomas E. Spanjaard } 2875c1b3d7c5SThomas E. Spanjaard else 2876c1b3d7c5SThomas E. Spanjaard raid->disks[meta->disk_number].flags &= ~AR_DF_ONLINE; 2877c1b3d7c5SThomas E. Spanjaard 2878c1b3d7c5SThomas E. Spanjaard break; 2879c1b3d7c5SThomas E. Spanjaard } 2880c1b3d7c5SThomas E. Spanjaard 2881c1b3d7c5SThomas E. Spanjaard lsiv2_out: 2882d438c7c2SThomas E. Spanjaard kfree(meta, M_AR); 2883c1b3d7c5SThomas E. Spanjaard return retval; 2884c1b3d7c5SThomas E. Spanjaard } 2885c1b3d7c5SThomas E. Spanjaard 2886c1b3d7c5SThomas E. Spanjaard /* LSILogic V3 MegaRAID Metadata */ 2887c1b3d7c5SThomas E. Spanjaard static int 2888c1b3d7c5SThomas E. Spanjaard ata_raid_lsiv3_read_meta(device_t dev, struct ar_softc **raidp) 2889c1b3d7c5SThomas E. Spanjaard { 2890c1b3d7c5SThomas E. Spanjaard struct ata_raid_subdisk *ars = device_get_softc(dev); 2891c1b3d7c5SThomas E. Spanjaard device_t parent = device_get_parent(dev); 2892c1b3d7c5SThomas E. Spanjaard struct lsiv3_raid_conf *meta; 2893c1b3d7c5SThomas E. Spanjaard struct ar_softc *raid = NULL; 2894c1b3d7c5SThomas E. Spanjaard u_int8_t checksum, *ptr; 2895c1b3d7c5SThomas E. Spanjaard int array, entry, count, disk_number, retval = 0; 2896c1b3d7c5SThomas E. Spanjaard 2897978400d3SSascha Wildner meta = (struct lsiv3_raid_conf *)kmalloc(sizeof(struct lsiv3_raid_conf), 2898978400d3SSascha Wildner M_AR, M_WAITOK | M_ZERO); 2899c1b3d7c5SThomas E. Spanjaard 2900c1b3d7c5SThomas E. Spanjaard if (ata_raid_rw(parent, LSIV3_LBA(parent), 2901c1b3d7c5SThomas E. Spanjaard meta, sizeof(struct lsiv3_raid_conf), ATA_R_READ)) { 2902c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 2903c1b3d7c5SThomas E. Spanjaard device_printf(parent, "LSI (v3) read metadata failed\n"); 2904c1b3d7c5SThomas E. Spanjaard goto lsiv3_out; 2905c1b3d7c5SThomas E. Spanjaard } 2906c1b3d7c5SThomas E. Spanjaard 2907c1b3d7c5SThomas E. Spanjaard /* check if this is a LSI RAID struct */ 2908c1b3d7c5SThomas E. Spanjaard if (strncmp(meta->lsi_id, LSIV3_MAGIC, strlen(LSIV3_MAGIC))) { 2909c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 2910c1b3d7c5SThomas E. Spanjaard device_printf(parent, "LSI (v3) check1 failed\n"); 2911c1b3d7c5SThomas E. Spanjaard goto lsiv3_out; 2912c1b3d7c5SThomas E. Spanjaard } 2913c1b3d7c5SThomas E. Spanjaard 2914c1b3d7c5SThomas E. Spanjaard /* check if the checksum is OK */ 2915c1b3d7c5SThomas E. Spanjaard for (checksum = 0, ptr = meta->lsi_id, count = 0; count < 512; count++) 2916c1b3d7c5SThomas E. Spanjaard checksum += *ptr++; 2917c1b3d7c5SThomas E. Spanjaard if (checksum) { 2918c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 2919c1b3d7c5SThomas E. Spanjaard device_printf(parent, "LSI (v3) check2 failed\n"); 2920c1b3d7c5SThomas E. Spanjaard goto lsiv3_out; 2921c1b3d7c5SThomas E. Spanjaard } 2922c1b3d7c5SThomas E. Spanjaard 2923c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 2924c1b3d7c5SThomas E. Spanjaard ata_raid_lsiv3_print_meta(meta); 2925c1b3d7c5SThomas E. Spanjaard 2926c1b3d7c5SThomas E. Spanjaard /* now convert LSI (v3) config meta into our generic form */ 2927c1b3d7c5SThomas E. Spanjaard for (array = 0, entry = 0; array < MAX_ARRAYS && entry < 8;) { 2928c1b3d7c5SThomas E. Spanjaard if (!raidp[array]) { 2929c1b3d7c5SThomas E. Spanjaard raidp[array] = 2930d438c7c2SThomas E. Spanjaard (struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR, 2931d438c7c2SThomas E. Spanjaard M_WAITOK | M_ZERO); 2932c1b3d7c5SThomas E. Spanjaard } 2933c1b3d7c5SThomas E. Spanjaard raid = raidp[array]; 2934c1b3d7c5SThomas E. Spanjaard if (raid->format && (raid->format != AR_F_LSIV3_RAID)) { 2935c1b3d7c5SThomas E. Spanjaard array++; 2936c1b3d7c5SThomas E. Spanjaard continue; 2937c1b3d7c5SThomas E. Spanjaard } 2938c1b3d7c5SThomas E. Spanjaard 2939c1b3d7c5SThomas E. Spanjaard if ((raid->format == AR_F_LSIV3_RAID) && 2940c1b3d7c5SThomas E. Spanjaard (raid->magic_0 != meta->timestamp)) { 2941c1b3d7c5SThomas E. Spanjaard array++; 2942c1b3d7c5SThomas E. Spanjaard continue; 2943c1b3d7c5SThomas E. Spanjaard } 2944c1b3d7c5SThomas E. Spanjaard 2945c1b3d7c5SThomas E. Spanjaard switch (meta->raid[entry].total_disks) { 2946c1b3d7c5SThomas E. Spanjaard case 0: 2947c1b3d7c5SThomas E. Spanjaard entry++; 2948c1b3d7c5SThomas E. Spanjaard continue; 2949c1b3d7c5SThomas E. Spanjaard case 1: 2950c1b3d7c5SThomas E. Spanjaard if (meta->raid[entry].device == meta->device) { 2951c1b3d7c5SThomas E. Spanjaard disk_number = 0; 2952c1b3d7c5SThomas E. Spanjaard break; 2953c1b3d7c5SThomas E. Spanjaard } 2954c1b3d7c5SThomas E. Spanjaard if (raid->format) 2955c1b3d7c5SThomas E. Spanjaard array++; 2956c1b3d7c5SThomas E. Spanjaard entry++; 2957c1b3d7c5SThomas E. Spanjaard continue; 2958c1b3d7c5SThomas E. Spanjaard case 2: 2959c1b3d7c5SThomas E. Spanjaard disk_number = (meta->device & (LSIV3_D_DEVICE|LSIV3_D_CHANNEL))?1:0; 2960c1b3d7c5SThomas E. Spanjaard break; 2961c1b3d7c5SThomas E. Spanjaard default: 2962c1b3d7c5SThomas E. Spanjaard device_printf(parent, "lsiv3 > 2 disk support untested!!\n"); 2963c1b3d7c5SThomas E. Spanjaard disk_number = (meta->device & LSIV3_D_DEVICE ? 1 : 0) + 2964c1b3d7c5SThomas E. Spanjaard (meta->device & LSIV3_D_CHANNEL ? 2 : 0); 2965c1b3d7c5SThomas E. Spanjaard break; 2966c1b3d7c5SThomas E. Spanjaard } 2967c1b3d7c5SThomas E. Spanjaard 2968c1b3d7c5SThomas E. Spanjaard switch (meta->raid[entry].type) { 2969c1b3d7c5SThomas E. Spanjaard case LSIV3_T_RAID0: 2970c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID0; 2971c1b3d7c5SThomas E. Spanjaard raid->width = meta->raid[entry].total_disks; 2972c1b3d7c5SThomas E. Spanjaard break; 2973c1b3d7c5SThomas E. Spanjaard 2974c1b3d7c5SThomas E. Spanjaard case LSIV3_T_RAID1: 2975c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID1; 2976c1b3d7c5SThomas E. Spanjaard raid->width = meta->raid[entry].array_width; 2977c1b3d7c5SThomas E. Spanjaard break; 2978c1b3d7c5SThomas E. Spanjaard 2979c1b3d7c5SThomas E. Spanjaard default: 2980c1b3d7c5SThomas E. Spanjaard device_printf(parent, "LSI v3 unknown RAID type 0x%02x\n", 2981c1b3d7c5SThomas E. Spanjaard meta->raid[entry].type); 2982d438c7c2SThomas E. Spanjaard kfree(raidp[array], M_AR); 2983c1b3d7c5SThomas E. Spanjaard raidp[array] = NULL; 2984c1b3d7c5SThomas E. Spanjaard entry++; 2985c1b3d7c5SThomas E. Spanjaard continue; 2986c1b3d7c5SThomas E. Spanjaard } 2987c1b3d7c5SThomas E. Spanjaard 2988c1b3d7c5SThomas E. Spanjaard raid->magic_0 = meta->timestamp; 2989c1b3d7c5SThomas E. Spanjaard raid->format = AR_F_LSIV3_RAID; 2990c1b3d7c5SThomas E. Spanjaard raid->generation = 0; 2991c1b3d7c5SThomas E. Spanjaard raid->interleave = meta->raid[entry].stripe_pages * 8; 2992c1b3d7c5SThomas E. Spanjaard raid->total_disks = meta->raid[entry].total_disks; 2993c1b3d7c5SThomas E. Spanjaard raid->total_sectors = raid->width * meta->raid[entry].sectors; 2994c1b3d7c5SThomas E. Spanjaard raid->heads = 255; 2995c1b3d7c5SThomas E. Spanjaard raid->sectors = 63; 2996c1b3d7c5SThomas E. Spanjaard raid->cylinders = raid->total_sectors / (63 * 255); 2997c1b3d7c5SThomas E. Spanjaard raid->offset_sectors = meta->raid[entry].offset; 2998c1b3d7c5SThomas E. Spanjaard raid->rebuild_lba = 0; 2999c1b3d7c5SThomas E. Spanjaard raid->lun = array; 3000c1b3d7c5SThomas E. Spanjaard 3001c1b3d7c5SThomas E. Spanjaard raid->disks[disk_number].dev = parent; 3002c1b3d7c5SThomas E. Spanjaard raid->disks[disk_number].sectors = raid->total_sectors / raid->width; 3003c1b3d7c5SThomas E. Spanjaard raid->disks[disk_number].flags = 3004c1b3d7c5SThomas E. Spanjaard (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_ONLINE); 3005c1b3d7c5SThomas E. Spanjaard ars->raid[raid->volume] = raid; 3006c1b3d7c5SThomas E. Spanjaard ars->disk_number[raid->volume] = disk_number; 3007c1b3d7c5SThomas E. Spanjaard retval = 1; 3008c1b3d7c5SThomas E. Spanjaard entry++; 3009c1b3d7c5SThomas E. Spanjaard array++; 3010c1b3d7c5SThomas E. Spanjaard } 3011c1b3d7c5SThomas E. Spanjaard 3012c1b3d7c5SThomas E. Spanjaard lsiv3_out: 3013d438c7c2SThomas E. Spanjaard kfree(meta, M_AR); 3014c1b3d7c5SThomas E. Spanjaard return retval; 3015c1b3d7c5SThomas E. Spanjaard } 3016c1b3d7c5SThomas E. Spanjaard 3017c1b3d7c5SThomas E. Spanjaard /* nVidia MediaShield Metadata */ 3018c1b3d7c5SThomas E. Spanjaard static int 3019c1b3d7c5SThomas E. Spanjaard ata_raid_nvidia_read_meta(device_t dev, struct ar_softc **raidp) 3020c1b3d7c5SThomas E. Spanjaard { 3021c1b3d7c5SThomas E. Spanjaard struct ata_raid_subdisk *ars = device_get_softc(dev); 3022c1b3d7c5SThomas E. Spanjaard device_t parent = device_get_parent(dev); 3023c1b3d7c5SThomas E. Spanjaard struct nvidia_raid_conf *meta; 3024c1b3d7c5SThomas E. Spanjaard struct ar_softc *raid = NULL; 3025c1b3d7c5SThomas E. Spanjaard u_int32_t checksum, *ptr; 3026c1b3d7c5SThomas E. Spanjaard int array, count, retval = 0; 3027c1b3d7c5SThomas E. Spanjaard 3028978400d3SSascha Wildner meta = (struct nvidia_raid_conf *)kmalloc(sizeof(struct nvidia_raid_conf), 3029978400d3SSascha Wildner M_AR, M_WAITOK | M_ZERO); 3030c1b3d7c5SThomas E. Spanjaard 3031c1b3d7c5SThomas E. Spanjaard if (ata_raid_rw(parent, NVIDIA_LBA(parent), 3032c1b3d7c5SThomas E. Spanjaard meta, sizeof(struct nvidia_raid_conf), ATA_R_READ)) { 3033c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3034c1b3d7c5SThomas E. Spanjaard device_printf(parent, "nVidia read metadata failed\n"); 3035c1b3d7c5SThomas E. Spanjaard goto nvidia_out; 3036c1b3d7c5SThomas E. Spanjaard } 3037c1b3d7c5SThomas E. Spanjaard 3038c1b3d7c5SThomas E. Spanjaard /* check if this is a nVidia RAID struct */ 3039c1b3d7c5SThomas E. Spanjaard if (strncmp(meta->nvidia_id, NV_MAGIC, strlen(NV_MAGIC))) { 3040c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3041c1b3d7c5SThomas E. Spanjaard device_printf(parent, "nVidia check1 failed\n"); 3042c1b3d7c5SThomas E. Spanjaard goto nvidia_out; 3043c1b3d7c5SThomas E. Spanjaard } 3044c1b3d7c5SThomas E. Spanjaard 3045c1b3d7c5SThomas E. Spanjaard /* check if the checksum is OK */ 3046c1b3d7c5SThomas E. Spanjaard for (checksum = 0, ptr = (u_int32_t*)meta, count = 0; 3047c1b3d7c5SThomas E. Spanjaard count < meta->config_size; count++) 3048c1b3d7c5SThomas E. Spanjaard checksum += *ptr++; 3049c1b3d7c5SThomas E. Spanjaard if (checksum) { 3050c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3051c1b3d7c5SThomas E. Spanjaard device_printf(parent, "nVidia check2 failed\n"); 3052c1b3d7c5SThomas E. Spanjaard goto nvidia_out; 3053c1b3d7c5SThomas E. Spanjaard } 3054c1b3d7c5SThomas E. Spanjaard 3055c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3056c1b3d7c5SThomas E. Spanjaard ata_raid_nvidia_print_meta(meta); 3057c1b3d7c5SThomas E. Spanjaard 3058c1b3d7c5SThomas E. Spanjaard /* now convert nVidia meta into our generic form */ 3059c1b3d7c5SThomas E. Spanjaard for (array = 0; array < MAX_ARRAYS; array++) { 3060c1b3d7c5SThomas E. Spanjaard if (!raidp[array]) { 3061c1b3d7c5SThomas E. Spanjaard raidp[array] = 3062d438c7c2SThomas E. Spanjaard (struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR, 3063d438c7c2SThomas E. Spanjaard M_WAITOK | M_ZERO); 3064c1b3d7c5SThomas E. Spanjaard } 3065c1b3d7c5SThomas E. Spanjaard raid = raidp[array]; 3066c1b3d7c5SThomas E. Spanjaard if (raid->format && (raid->format != AR_F_NVIDIA_RAID)) 3067c1b3d7c5SThomas E. Spanjaard continue; 3068c1b3d7c5SThomas E. Spanjaard 3069c1b3d7c5SThomas E. Spanjaard if (raid->format == AR_F_NVIDIA_RAID && 3070c1b3d7c5SThomas E. Spanjaard ((raid->magic_0 != meta->magic_1) || 3071c1b3d7c5SThomas E. Spanjaard (raid->magic_1 != meta->magic_2))) { 3072c1b3d7c5SThomas E. Spanjaard continue; 3073c1b3d7c5SThomas E. Spanjaard } 3074c1b3d7c5SThomas E. Spanjaard 3075c1b3d7c5SThomas E. Spanjaard switch (meta->type) { 3076c1b3d7c5SThomas E. Spanjaard case NV_T_SPAN: 3077c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_SPAN; 3078c1b3d7c5SThomas E. Spanjaard break; 3079c1b3d7c5SThomas E. Spanjaard 3080c1b3d7c5SThomas E. Spanjaard case NV_T_RAID0: 3081c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID0; 3082c1b3d7c5SThomas E. Spanjaard break; 3083c1b3d7c5SThomas E. Spanjaard 3084c1b3d7c5SThomas E. Spanjaard case NV_T_RAID1: 3085c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID1; 3086c1b3d7c5SThomas E. Spanjaard break; 3087c1b3d7c5SThomas E. Spanjaard 3088c1b3d7c5SThomas E. Spanjaard case NV_T_RAID5: 3089c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID5; 3090c1b3d7c5SThomas E. Spanjaard break; 3091c1b3d7c5SThomas E. Spanjaard 3092c1b3d7c5SThomas E. Spanjaard case NV_T_RAID01: 3093c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID01; 3094c1b3d7c5SThomas E. Spanjaard break; 3095c1b3d7c5SThomas E. Spanjaard 3096c1b3d7c5SThomas E. Spanjaard default: 3097c1b3d7c5SThomas E. Spanjaard device_printf(parent, "nVidia unknown RAID type 0x%02x\n", 3098c1b3d7c5SThomas E. Spanjaard meta->type); 3099d438c7c2SThomas E. Spanjaard kfree(raidp[array], M_AR); 3100c1b3d7c5SThomas E. Spanjaard raidp[array] = NULL; 3101c1b3d7c5SThomas E. Spanjaard goto nvidia_out; 3102c1b3d7c5SThomas E. Spanjaard } 3103c1b3d7c5SThomas E. Spanjaard raid->magic_0 = meta->magic_1; 3104c1b3d7c5SThomas E. Spanjaard raid->magic_1 = meta->magic_2; 3105c1b3d7c5SThomas E. Spanjaard raid->format = AR_F_NVIDIA_RAID; 3106c1b3d7c5SThomas E. Spanjaard raid->generation = 0; 3107c1b3d7c5SThomas E. Spanjaard raid->interleave = meta->stripe_sectors; 3108c1b3d7c5SThomas E. Spanjaard raid->width = meta->array_width; 3109c1b3d7c5SThomas E. Spanjaard raid->total_disks = meta->total_disks; 3110c1b3d7c5SThomas E. Spanjaard raid->total_sectors = meta->total_sectors; 3111c1b3d7c5SThomas E. Spanjaard raid->heads = 255; 3112c1b3d7c5SThomas E. Spanjaard raid->sectors = 63; 3113c1b3d7c5SThomas E. Spanjaard raid->cylinders = raid->total_sectors / (63 * 255); 3114c1b3d7c5SThomas E. Spanjaard raid->offset_sectors = 0; 3115c1b3d7c5SThomas E. Spanjaard raid->rebuild_lba = meta->rebuild_lba; 3116c1b3d7c5SThomas E. Spanjaard raid->lun = array; 3117c1b3d7c5SThomas E. Spanjaard raid->status = AR_S_READY; 3118c1b3d7c5SThomas E. Spanjaard if (meta->status & NV_S_DEGRADED) 3119c1b3d7c5SThomas E. Spanjaard raid->status |= AR_S_DEGRADED; 3120c1b3d7c5SThomas E. Spanjaard 3121c1b3d7c5SThomas E. Spanjaard raid->disks[meta->disk_number].dev = parent; 3122c1b3d7c5SThomas E. Spanjaard raid->disks[meta->disk_number].sectors = 3123c1b3d7c5SThomas E. Spanjaard raid->total_sectors / raid->width; 3124c1b3d7c5SThomas E. Spanjaard raid->disks[meta->disk_number].flags = 3125c1b3d7c5SThomas E. Spanjaard (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_ONLINE); 3126c1b3d7c5SThomas E. Spanjaard ars->raid[raid->volume] = raid; 3127c1b3d7c5SThomas E. Spanjaard ars->disk_number[raid->volume] = meta->disk_number; 3128c1b3d7c5SThomas E. Spanjaard retval = 1; 3129c1b3d7c5SThomas E. Spanjaard break; 3130c1b3d7c5SThomas E. Spanjaard } 3131c1b3d7c5SThomas E. Spanjaard 3132c1b3d7c5SThomas E. Spanjaard nvidia_out: 3133d438c7c2SThomas E. Spanjaard kfree(meta, M_AR); 3134c1b3d7c5SThomas E. Spanjaard return retval; 3135c1b3d7c5SThomas E. Spanjaard } 3136c1b3d7c5SThomas E. Spanjaard 3137c1b3d7c5SThomas E. Spanjaard /* Promise FastTrak Metadata */ 3138c1b3d7c5SThomas E. Spanjaard static int 3139c1b3d7c5SThomas E. Spanjaard ata_raid_promise_read_meta(device_t dev, struct ar_softc **raidp, int native) 3140c1b3d7c5SThomas E. Spanjaard { 3141c1b3d7c5SThomas E. Spanjaard struct ata_raid_subdisk *ars = device_get_softc(dev); 3142c1b3d7c5SThomas E. Spanjaard device_t parent = device_get_parent(dev); 3143c1b3d7c5SThomas E. Spanjaard struct promise_raid_conf *meta; 3144c1b3d7c5SThomas E. Spanjaard struct ar_softc *raid; 3145c1b3d7c5SThomas E. Spanjaard u_int32_t checksum, *ptr; 3146c1b3d7c5SThomas E. Spanjaard int array, count, disk, disksum = 0, retval = 0; 3147c1b3d7c5SThomas E. Spanjaard 3148978400d3SSascha Wildner meta = (struct promise_raid_conf *) 3149978400d3SSascha Wildner kmalloc(sizeof(struct promise_raid_conf), M_AR, M_WAITOK | M_ZERO); 3150c1b3d7c5SThomas E. Spanjaard 3151c1b3d7c5SThomas E. Spanjaard if (ata_raid_rw(parent, PROMISE_LBA(parent), 3152c1b3d7c5SThomas E. Spanjaard meta, sizeof(struct promise_raid_conf), ATA_R_READ)) { 3153c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3154c1b3d7c5SThomas E. Spanjaard device_printf(parent, "%s read metadata failed\n", 3155c1b3d7c5SThomas E. Spanjaard native ? "FreeBSD" : "Promise"); 3156c1b3d7c5SThomas E. Spanjaard goto promise_out; 3157c1b3d7c5SThomas E. Spanjaard } 3158c1b3d7c5SThomas E. Spanjaard 3159c1b3d7c5SThomas E. Spanjaard /* check the signature */ 3160c1b3d7c5SThomas E. Spanjaard if (native) { 3161c1b3d7c5SThomas E. Spanjaard if (strncmp(meta->promise_id, ATA_MAGIC, strlen(ATA_MAGIC))) { 3162c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3163c1b3d7c5SThomas E. Spanjaard device_printf(parent, "FreeBSD check1 failed\n"); 3164c1b3d7c5SThomas E. Spanjaard goto promise_out; 3165c1b3d7c5SThomas E. Spanjaard } 3166c1b3d7c5SThomas E. Spanjaard } 3167c1b3d7c5SThomas E. Spanjaard else { 3168c1b3d7c5SThomas E. Spanjaard if (strncmp(meta->promise_id, PR_MAGIC, strlen(PR_MAGIC))) { 3169c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3170c1b3d7c5SThomas E. Spanjaard device_printf(parent, "Promise check1 failed\n"); 3171c1b3d7c5SThomas E. Spanjaard goto promise_out; 3172c1b3d7c5SThomas E. Spanjaard } 3173c1b3d7c5SThomas E. Spanjaard } 3174c1b3d7c5SThomas E. Spanjaard 3175c1b3d7c5SThomas E. Spanjaard /* check if the checksum is OK */ 3176c1b3d7c5SThomas E. Spanjaard for (checksum = 0, ptr = (u_int32_t *)meta, count = 0; count < 511; count++) 3177c1b3d7c5SThomas E. Spanjaard checksum += *ptr++; 3178c1b3d7c5SThomas E. Spanjaard if (checksum != *ptr) { 3179c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3180c1b3d7c5SThomas E. Spanjaard device_printf(parent, "%s check2 failed\n", 3181c1b3d7c5SThomas E. Spanjaard native ? "FreeBSD" : "Promise"); 3182c1b3d7c5SThomas E. Spanjaard goto promise_out; 3183c1b3d7c5SThomas E. Spanjaard } 3184c1b3d7c5SThomas E. Spanjaard 3185c1b3d7c5SThomas E. Spanjaard /* check on disk integrity status */ 3186c1b3d7c5SThomas E. Spanjaard if (meta->raid.integrity != PR_I_VALID) { 3187c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3188c1b3d7c5SThomas E. Spanjaard device_printf(parent, "%s check3 failed\n", 3189c1b3d7c5SThomas E. Spanjaard native ? "FreeBSD" : "Promise"); 3190c1b3d7c5SThomas E. Spanjaard goto promise_out; 3191c1b3d7c5SThomas E. Spanjaard } 3192c1b3d7c5SThomas E. Spanjaard 3193c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3194c1b3d7c5SThomas E. Spanjaard ata_raid_promise_print_meta(meta); 3195c1b3d7c5SThomas E. Spanjaard 3196c1b3d7c5SThomas E. Spanjaard /* now convert Promise metadata into our generic form */ 3197c1b3d7c5SThomas E. Spanjaard for (array = 0; array < MAX_ARRAYS; array++) { 3198c1b3d7c5SThomas E. Spanjaard if (!raidp[array]) { 3199c1b3d7c5SThomas E. Spanjaard raidp[array] = 3200d438c7c2SThomas E. Spanjaard (struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR, 3201d438c7c2SThomas E. Spanjaard M_WAITOK | M_ZERO); 3202c1b3d7c5SThomas E. Spanjaard } 3203c1b3d7c5SThomas E. Spanjaard raid = raidp[array]; 3204c1b3d7c5SThomas E. Spanjaard if (raid->format && 3205c1b3d7c5SThomas E. Spanjaard (raid->format != (native ? AR_F_FREEBSD_RAID : AR_F_PROMISE_RAID))) 3206c1b3d7c5SThomas E. Spanjaard continue; 3207c1b3d7c5SThomas E. Spanjaard 3208c1b3d7c5SThomas E. Spanjaard if ((raid->format == (native ? AR_F_FREEBSD_RAID : AR_F_PROMISE_RAID))&& 3209c1b3d7c5SThomas E. Spanjaard !(meta->raid.magic_1 == (raid->magic_1))) 3210c1b3d7c5SThomas E. Spanjaard continue; 3211c1b3d7c5SThomas E. Spanjaard 3212c1b3d7c5SThomas E. Spanjaard /* update our knowledge about the array config based on generation */ 3213c1b3d7c5SThomas E. Spanjaard if (!meta->raid.generation || meta->raid.generation > raid->generation){ 3214c1b3d7c5SThomas E. Spanjaard switch (meta->raid.type) { 3215c1b3d7c5SThomas E. Spanjaard case PR_T_SPAN: 3216c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_SPAN; 3217c1b3d7c5SThomas E. Spanjaard break; 3218c1b3d7c5SThomas E. Spanjaard 3219c1b3d7c5SThomas E. Spanjaard case PR_T_JBOD: 3220c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_JBOD; 3221c1b3d7c5SThomas E. Spanjaard break; 3222c1b3d7c5SThomas E. Spanjaard 3223c1b3d7c5SThomas E. Spanjaard case PR_T_RAID0: 3224c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID0; 3225c1b3d7c5SThomas E. Spanjaard break; 3226c1b3d7c5SThomas E. Spanjaard 3227c1b3d7c5SThomas E. Spanjaard case PR_T_RAID1: 3228c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID1; 3229c1b3d7c5SThomas E. Spanjaard if (meta->raid.array_width > 1) 3230c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID01; 3231c1b3d7c5SThomas E. Spanjaard break; 3232c1b3d7c5SThomas E. Spanjaard 3233c1b3d7c5SThomas E. Spanjaard case PR_T_RAID5: 3234c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID5; 3235c1b3d7c5SThomas E. Spanjaard break; 3236c1b3d7c5SThomas E. Spanjaard 3237c1b3d7c5SThomas E. Spanjaard default: 3238c1b3d7c5SThomas E. Spanjaard device_printf(parent, "%s unknown RAID type 0x%02x\n", 3239c1b3d7c5SThomas E. Spanjaard native ? "FreeBSD" : "Promise", meta->raid.type); 3240d438c7c2SThomas E. Spanjaard kfree(raidp[array], M_AR); 3241c1b3d7c5SThomas E. Spanjaard raidp[array] = NULL; 3242c1b3d7c5SThomas E. Spanjaard goto promise_out; 3243c1b3d7c5SThomas E. Spanjaard } 3244c1b3d7c5SThomas E. Spanjaard raid->magic_1 = meta->raid.magic_1; 3245c1b3d7c5SThomas E. Spanjaard raid->format = (native ? AR_F_FREEBSD_RAID : AR_F_PROMISE_RAID); 3246c1b3d7c5SThomas E. Spanjaard raid->generation = meta->raid.generation; 3247c1b3d7c5SThomas E. Spanjaard raid->interleave = 1 << meta->raid.stripe_shift; 3248c1b3d7c5SThomas E. Spanjaard raid->width = meta->raid.array_width; 3249c1b3d7c5SThomas E. Spanjaard raid->total_disks = meta->raid.total_disks; 3250c1b3d7c5SThomas E. Spanjaard raid->heads = meta->raid.heads + 1; 3251c1b3d7c5SThomas E. Spanjaard raid->sectors = meta->raid.sectors; 3252c1b3d7c5SThomas E. Spanjaard raid->cylinders = meta->raid.cylinders + 1; 3253c1b3d7c5SThomas E. Spanjaard raid->total_sectors = meta->raid.total_sectors; 3254c1b3d7c5SThomas E. Spanjaard raid->offset_sectors = 0; 3255c1b3d7c5SThomas E. Spanjaard raid->rebuild_lba = meta->raid.rebuild_lba; 3256c1b3d7c5SThomas E. Spanjaard raid->lun = array; 3257c1b3d7c5SThomas E. Spanjaard if ((meta->raid.status & 3258c1b3d7c5SThomas E. Spanjaard (PR_S_VALID | PR_S_ONLINE | PR_S_INITED | PR_S_READY)) == 3259c1b3d7c5SThomas E. Spanjaard (PR_S_VALID | PR_S_ONLINE | PR_S_INITED | PR_S_READY)) { 3260c1b3d7c5SThomas E. Spanjaard raid->status |= AR_S_READY; 3261c1b3d7c5SThomas E. Spanjaard if (meta->raid.status & PR_S_DEGRADED) 3262c1b3d7c5SThomas E. Spanjaard raid->status |= AR_S_DEGRADED; 3263c1b3d7c5SThomas E. Spanjaard } 3264c1b3d7c5SThomas E. Spanjaard else 3265c1b3d7c5SThomas E. Spanjaard raid->status &= ~AR_S_READY; 3266c1b3d7c5SThomas E. Spanjaard 3267c1b3d7c5SThomas E. Spanjaard /* convert disk flags to our internal types */ 3268c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < meta->raid.total_disks; disk++) { 3269c1b3d7c5SThomas E. Spanjaard raid->disks[disk].dev = NULL; 3270c1b3d7c5SThomas E. Spanjaard raid->disks[disk].flags = 0; 3271c1b3d7c5SThomas E. Spanjaard *((u_int64_t *)(raid->disks[disk].serial)) = 3272c1b3d7c5SThomas E. Spanjaard meta->raid.disk[disk].magic_0; 3273c1b3d7c5SThomas E. Spanjaard disksum += meta->raid.disk[disk].flags; 3274c1b3d7c5SThomas E. Spanjaard if (meta->raid.disk[disk].flags & PR_F_ONLINE) 3275c1b3d7c5SThomas E. Spanjaard raid->disks[disk].flags |= AR_DF_ONLINE; 3276c1b3d7c5SThomas E. Spanjaard if (meta->raid.disk[disk].flags & PR_F_ASSIGNED) 3277c1b3d7c5SThomas E. Spanjaard raid->disks[disk].flags |= AR_DF_ASSIGNED; 3278c1b3d7c5SThomas E. Spanjaard if (meta->raid.disk[disk].flags & PR_F_SPARE) { 3279c1b3d7c5SThomas E. Spanjaard raid->disks[disk].flags &= ~(AR_DF_ONLINE | AR_DF_ASSIGNED); 3280c1b3d7c5SThomas E. Spanjaard raid->disks[disk].flags |= AR_DF_SPARE; 3281c1b3d7c5SThomas E. Spanjaard } 3282c1b3d7c5SThomas E. Spanjaard if (meta->raid.disk[disk].flags & (PR_F_REDIR | PR_F_DOWN)) 3283c1b3d7c5SThomas E. Spanjaard raid->disks[disk].flags &= ~AR_DF_ONLINE; 3284c1b3d7c5SThomas E. Spanjaard } 3285c1b3d7c5SThomas E. Spanjaard if (!disksum) { 3286c1b3d7c5SThomas E. Spanjaard device_printf(parent, "%s subdisks has no flags\n", 3287c1b3d7c5SThomas E. Spanjaard native ? "FreeBSD" : "Promise"); 3288d438c7c2SThomas E. Spanjaard kfree(raidp[array], M_AR); 3289c1b3d7c5SThomas E. Spanjaard raidp[array] = NULL; 3290c1b3d7c5SThomas E. Spanjaard goto promise_out; 3291c1b3d7c5SThomas E. Spanjaard } 3292c1b3d7c5SThomas E. Spanjaard } 3293c1b3d7c5SThomas E. Spanjaard if (meta->raid.generation >= raid->generation) { 3294c1b3d7c5SThomas E. Spanjaard int disk_number = meta->raid.disk_number; 3295c1b3d7c5SThomas E. Spanjaard 3296c1b3d7c5SThomas E. Spanjaard if (raid->disks[disk_number].flags && (meta->magic_0 == 3297c1b3d7c5SThomas E. Spanjaard *((u_int64_t *)(raid->disks[disk_number].serial)))) { 3298c1b3d7c5SThomas E. Spanjaard raid->disks[disk_number].dev = parent; 3299c1b3d7c5SThomas E. Spanjaard raid->disks[disk_number].flags |= AR_DF_PRESENT; 3300c1b3d7c5SThomas E. Spanjaard raid->disks[disk_number].sectors = meta->raid.disk_sectors; 3301c1b3d7c5SThomas E. Spanjaard if ((raid->disks[disk_number].flags & 3302c1b3d7c5SThomas E. Spanjaard (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_ONLINE)) == 3303c1b3d7c5SThomas E. Spanjaard (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_ONLINE)) { 3304c1b3d7c5SThomas E. Spanjaard ars->raid[raid->volume] = raid; 3305c1b3d7c5SThomas E. Spanjaard ars->disk_number[raid->volume] = disk_number; 3306c1b3d7c5SThomas E. Spanjaard retval = 1; 3307c1b3d7c5SThomas E. Spanjaard } 3308c1b3d7c5SThomas E. Spanjaard } 3309c1b3d7c5SThomas E. Spanjaard } 3310c1b3d7c5SThomas E. Spanjaard break; 3311c1b3d7c5SThomas E. Spanjaard } 3312c1b3d7c5SThomas E. Spanjaard 3313c1b3d7c5SThomas E. Spanjaard promise_out: 3314d438c7c2SThomas E. Spanjaard kfree(meta, M_AR); 3315c1b3d7c5SThomas E. Spanjaard return retval; 3316c1b3d7c5SThomas E. Spanjaard } 3317c1b3d7c5SThomas E. Spanjaard 3318c1b3d7c5SThomas E. Spanjaard static int 3319c1b3d7c5SThomas E. Spanjaard ata_raid_promise_write_meta(struct ar_softc *rdp) 3320c1b3d7c5SThomas E. Spanjaard { 3321c1b3d7c5SThomas E. Spanjaard struct promise_raid_conf *meta; 3322c1b3d7c5SThomas E. Spanjaard struct timeval timestamp; 3323c1b3d7c5SThomas E. Spanjaard u_int32_t *ckptr; 3324c1b3d7c5SThomas E. Spanjaard int count, disk, drive, error = 0; 3325c1b3d7c5SThomas E. Spanjaard 3326978400d3SSascha Wildner meta = (struct promise_raid_conf *) 3327978400d3SSascha Wildner kmalloc(sizeof(struct promise_raid_conf), M_AR, M_WAITOK); 3328c1b3d7c5SThomas E. Spanjaard 3329c1b3d7c5SThomas E. Spanjaard rdp->generation++; 3330c1b3d7c5SThomas E. Spanjaard microtime(×tamp); 3331c1b3d7c5SThomas E. Spanjaard 3332c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < rdp->total_disks; disk++) { 3333c1b3d7c5SThomas E. Spanjaard for (count = 0; count < sizeof(struct promise_raid_conf); count++) 3334c1b3d7c5SThomas E. Spanjaard *(((u_int8_t *)meta) + count) = 255 - (count % 256); 3335c1b3d7c5SThomas E. Spanjaard meta->dummy_0 = 0x00020000; 3336c1b3d7c5SThomas E. Spanjaard meta->raid.disk_number = disk; 3337c1b3d7c5SThomas E. Spanjaard 3338c1b3d7c5SThomas E. Spanjaard if (rdp->disks[disk].dev) { 3339c1b3d7c5SThomas E. Spanjaard struct ata_device *atadev = device_get_softc(rdp->disks[disk].dev); 3340c1b3d7c5SThomas E. Spanjaard struct ata_channel *ch = 3341c1b3d7c5SThomas E. Spanjaard device_get_softc(device_get_parent(rdp->disks[disk].dev)); 3342c1b3d7c5SThomas E. Spanjaard 3343c1b3d7c5SThomas E. Spanjaard meta->raid.channel = ch->unit; 3344c1b3d7c5SThomas E. Spanjaard meta->raid.device = ATA_DEV(atadev->unit); 3345c1b3d7c5SThomas E. Spanjaard meta->raid.disk_sectors = rdp->disks[disk].sectors; 3346c1b3d7c5SThomas E. Spanjaard meta->raid.disk_offset = rdp->offset_sectors; 3347c1b3d7c5SThomas E. Spanjaard } 3348c1b3d7c5SThomas E. Spanjaard else { 3349c1b3d7c5SThomas E. Spanjaard meta->raid.channel = 0; 3350c1b3d7c5SThomas E. Spanjaard meta->raid.device = 0; 3351c1b3d7c5SThomas E. Spanjaard meta->raid.disk_sectors = 0; 3352c1b3d7c5SThomas E. Spanjaard meta->raid.disk_offset = 0; 3353c1b3d7c5SThomas E. Spanjaard } 3354c1b3d7c5SThomas E. Spanjaard meta->magic_0 = PR_MAGIC0(meta->raid) | timestamp.tv_sec; 3355c1b3d7c5SThomas E. Spanjaard meta->magic_1 = timestamp.tv_sec >> 16; 3356c1b3d7c5SThomas E. Spanjaard meta->magic_2 = timestamp.tv_sec; 3357c1b3d7c5SThomas E. Spanjaard meta->raid.integrity = PR_I_VALID; 3358c1b3d7c5SThomas E. Spanjaard meta->raid.magic_0 = meta->magic_0; 3359c1b3d7c5SThomas E. Spanjaard meta->raid.rebuild_lba = rdp->rebuild_lba; 3360c1b3d7c5SThomas E. Spanjaard meta->raid.generation = rdp->generation; 3361c1b3d7c5SThomas E. Spanjaard 3362c1b3d7c5SThomas E. Spanjaard if (rdp->status & AR_S_READY) { 3363c1b3d7c5SThomas E. Spanjaard meta->raid.flags = (PR_F_VALID | PR_F_ASSIGNED | PR_F_ONLINE); 3364c1b3d7c5SThomas E. Spanjaard meta->raid.status = 3365c1b3d7c5SThomas E. Spanjaard (PR_S_VALID | PR_S_ONLINE | PR_S_INITED | PR_S_READY); 3366c1b3d7c5SThomas E. Spanjaard if (rdp->status & AR_S_DEGRADED) 3367c1b3d7c5SThomas E. Spanjaard meta->raid.status |= PR_S_DEGRADED; 3368c1b3d7c5SThomas E. Spanjaard else 3369c1b3d7c5SThomas E. Spanjaard meta->raid.status |= PR_S_FUNCTIONAL; 3370c1b3d7c5SThomas E. Spanjaard } 3371c1b3d7c5SThomas E. Spanjaard else { 3372c1b3d7c5SThomas E. Spanjaard meta->raid.flags = PR_F_DOWN; 3373c1b3d7c5SThomas E. Spanjaard meta->raid.status = 0; 3374c1b3d7c5SThomas E. Spanjaard } 3375c1b3d7c5SThomas E. Spanjaard 3376c1b3d7c5SThomas E. Spanjaard switch (rdp->type) { 3377c1b3d7c5SThomas E. Spanjaard case AR_T_RAID0: 3378c1b3d7c5SThomas E. Spanjaard meta->raid.type = PR_T_RAID0; 3379c1b3d7c5SThomas E. Spanjaard break; 3380c1b3d7c5SThomas E. Spanjaard case AR_T_RAID1: 3381c1b3d7c5SThomas E. Spanjaard meta->raid.type = PR_T_RAID1; 3382c1b3d7c5SThomas E. Spanjaard break; 3383c1b3d7c5SThomas E. Spanjaard case AR_T_RAID01: 3384c1b3d7c5SThomas E. Spanjaard meta->raid.type = PR_T_RAID1; 3385c1b3d7c5SThomas E. Spanjaard break; 3386c1b3d7c5SThomas E. Spanjaard case AR_T_RAID5: 3387c1b3d7c5SThomas E. Spanjaard meta->raid.type = PR_T_RAID5; 3388c1b3d7c5SThomas E. Spanjaard break; 3389c1b3d7c5SThomas E. Spanjaard case AR_T_SPAN: 3390c1b3d7c5SThomas E. Spanjaard meta->raid.type = PR_T_SPAN; 3391c1b3d7c5SThomas E. Spanjaard break; 3392c1b3d7c5SThomas E. Spanjaard case AR_T_JBOD: 3393c1b3d7c5SThomas E. Spanjaard meta->raid.type = PR_T_JBOD; 3394c1b3d7c5SThomas E. Spanjaard break; 3395c1b3d7c5SThomas E. Spanjaard default: 3396d438c7c2SThomas E. Spanjaard kfree(meta, M_AR); 3397c1b3d7c5SThomas E. Spanjaard return ENODEV; 3398c1b3d7c5SThomas E. Spanjaard } 3399c1b3d7c5SThomas E. Spanjaard 3400c1b3d7c5SThomas E. Spanjaard meta->raid.total_disks = rdp->total_disks; 3401c1b3d7c5SThomas E. Spanjaard meta->raid.stripe_shift = ffs(rdp->interleave) - 1; 3402c1b3d7c5SThomas E. Spanjaard meta->raid.array_width = rdp->width; 3403c1b3d7c5SThomas E. Spanjaard meta->raid.array_number = rdp->lun; 3404c1b3d7c5SThomas E. Spanjaard meta->raid.total_sectors = rdp->total_sectors; 3405c1b3d7c5SThomas E. Spanjaard meta->raid.cylinders = rdp->cylinders - 1; 3406c1b3d7c5SThomas E. Spanjaard meta->raid.heads = rdp->heads - 1; 3407c1b3d7c5SThomas E. Spanjaard meta->raid.sectors = rdp->sectors; 3408c1b3d7c5SThomas E. Spanjaard meta->raid.magic_1 = (u_int64_t)meta->magic_2<<16 | meta->magic_1; 3409c1b3d7c5SThomas E. Spanjaard 3410c1b3d7c5SThomas E. Spanjaard bzero(&meta->raid.disk, 8 * 12); 3411c1b3d7c5SThomas E. Spanjaard for (drive = 0; drive < rdp->total_disks; drive++) { 3412c1b3d7c5SThomas E. Spanjaard meta->raid.disk[drive].flags = 0; 3413c1b3d7c5SThomas E. Spanjaard if (rdp->disks[drive].flags & AR_DF_PRESENT) 3414c1b3d7c5SThomas E. Spanjaard meta->raid.disk[drive].flags |= PR_F_VALID; 3415c1b3d7c5SThomas E. Spanjaard if (rdp->disks[drive].flags & AR_DF_ASSIGNED) 3416c1b3d7c5SThomas E. Spanjaard meta->raid.disk[drive].flags |= PR_F_ASSIGNED; 3417c1b3d7c5SThomas E. Spanjaard if (rdp->disks[drive].flags & AR_DF_ONLINE) 3418c1b3d7c5SThomas E. Spanjaard meta->raid.disk[drive].flags |= PR_F_ONLINE; 3419c1b3d7c5SThomas E. Spanjaard else 3420c1b3d7c5SThomas E. Spanjaard if (rdp->disks[drive].flags & AR_DF_PRESENT) 3421c1b3d7c5SThomas E. Spanjaard meta->raid.disk[drive].flags = (PR_F_REDIR | PR_F_DOWN); 3422c1b3d7c5SThomas E. Spanjaard if (rdp->disks[drive].flags & AR_DF_SPARE) 3423c1b3d7c5SThomas E. Spanjaard meta->raid.disk[drive].flags |= PR_F_SPARE; 3424c1b3d7c5SThomas E. Spanjaard meta->raid.disk[drive].dummy_0 = 0x0; 3425c1b3d7c5SThomas E. Spanjaard if (rdp->disks[drive].dev) { 3426c1b3d7c5SThomas E. Spanjaard struct ata_channel *ch = 3427c1b3d7c5SThomas E. Spanjaard device_get_softc(device_get_parent(rdp->disks[drive].dev)); 3428c1b3d7c5SThomas E. Spanjaard struct ata_device *atadev = 3429c1b3d7c5SThomas E. Spanjaard device_get_softc(rdp->disks[drive].dev); 3430c1b3d7c5SThomas E. Spanjaard 3431c1b3d7c5SThomas E. Spanjaard meta->raid.disk[drive].channel = ch->unit; 3432c1b3d7c5SThomas E. Spanjaard meta->raid.disk[drive].device = ATA_DEV(atadev->unit); 3433c1b3d7c5SThomas E. Spanjaard } 3434c1b3d7c5SThomas E. Spanjaard meta->raid.disk[drive].magic_0 = 3435c1b3d7c5SThomas E. Spanjaard PR_MAGIC0(meta->raid.disk[drive]) | timestamp.tv_sec; 3436c1b3d7c5SThomas E. Spanjaard } 3437c1b3d7c5SThomas E. Spanjaard 3438c1b3d7c5SThomas E. Spanjaard if (rdp->disks[disk].dev) { 3439c1b3d7c5SThomas E. Spanjaard if ((rdp->disks[disk].flags & (AR_DF_PRESENT | AR_DF_ONLINE)) == 3440c1b3d7c5SThomas E. Spanjaard (AR_DF_PRESENT | AR_DF_ONLINE)) { 3441c1b3d7c5SThomas E. Spanjaard if (rdp->format == AR_F_FREEBSD_RAID) 3442c1b3d7c5SThomas E. Spanjaard bcopy(ATA_MAGIC, meta->promise_id, sizeof(ATA_MAGIC)); 3443c1b3d7c5SThomas E. Spanjaard else 3444c1b3d7c5SThomas E. Spanjaard bcopy(PR_MAGIC, meta->promise_id, sizeof(PR_MAGIC)); 3445c1b3d7c5SThomas E. Spanjaard } 3446c1b3d7c5SThomas E. Spanjaard else 3447c1b3d7c5SThomas E. Spanjaard bzero(meta->promise_id, sizeof(meta->promise_id)); 3448c1b3d7c5SThomas E. Spanjaard meta->checksum = 0; 3449c1b3d7c5SThomas E. Spanjaard for (ckptr = (int32_t *)meta, count = 0; count < 511; count++) 3450c1b3d7c5SThomas E. Spanjaard meta->checksum += *ckptr++; 3451c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3452c1b3d7c5SThomas E. Spanjaard ata_raid_promise_print_meta(meta); 3453c1b3d7c5SThomas E. Spanjaard if (ata_raid_rw(rdp->disks[disk].dev, 3454c1b3d7c5SThomas E. Spanjaard PROMISE_LBA(rdp->disks[disk].dev), 3455c1b3d7c5SThomas E. Spanjaard meta, sizeof(struct promise_raid_conf), 3456c1b3d7c5SThomas E. Spanjaard ATA_R_WRITE | ATA_R_DIRECT)) { 3457c1b3d7c5SThomas E. Spanjaard device_printf(rdp->disks[disk].dev, "write metadata failed\n"); 3458c1b3d7c5SThomas E. Spanjaard error = EIO; 3459c1b3d7c5SThomas E. Spanjaard } 3460c1b3d7c5SThomas E. Spanjaard } 3461c1b3d7c5SThomas E. Spanjaard } 3462d438c7c2SThomas E. Spanjaard kfree(meta, M_AR); 3463c1b3d7c5SThomas E. Spanjaard return error; 3464c1b3d7c5SThomas E. Spanjaard } 3465c1b3d7c5SThomas E. Spanjaard 3466c1b3d7c5SThomas E. Spanjaard /* Silicon Image Medley Metadata */ 3467c1b3d7c5SThomas E. Spanjaard static int 3468c1b3d7c5SThomas E. Spanjaard ata_raid_sii_read_meta(device_t dev, struct ar_softc **raidp) 3469c1b3d7c5SThomas E. Spanjaard { 3470c1b3d7c5SThomas E. Spanjaard struct ata_raid_subdisk *ars = device_get_softc(dev); 3471c1b3d7c5SThomas E. Spanjaard device_t parent = device_get_parent(dev); 3472c1b3d7c5SThomas E. Spanjaard struct sii_raid_conf *meta; 3473c1b3d7c5SThomas E. Spanjaard struct ar_softc *raid = NULL; 3474c1b3d7c5SThomas E. Spanjaard u_int16_t checksum, *ptr; 3475c1b3d7c5SThomas E. Spanjaard int array, count, disk, retval = 0; 3476c1b3d7c5SThomas E. Spanjaard 3477978400d3SSascha Wildner meta = (struct sii_raid_conf *)kmalloc(sizeof(struct sii_raid_conf), M_AR, 3478978400d3SSascha Wildner M_WAITOK | M_ZERO); 3479c1b3d7c5SThomas E. Spanjaard 3480c1b3d7c5SThomas E. Spanjaard if (ata_raid_rw(parent, SII_LBA(parent), 3481c1b3d7c5SThomas E. Spanjaard meta, sizeof(struct sii_raid_conf), ATA_R_READ)) { 3482c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3483c1b3d7c5SThomas E. Spanjaard device_printf(parent, "Silicon Image read metadata failed\n"); 3484c1b3d7c5SThomas E. Spanjaard goto sii_out; 3485c1b3d7c5SThomas E. Spanjaard } 3486c1b3d7c5SThomas E. Spanjaard 3487c1b3d7c5SThomas E. Spanjaard /* check if this is a Silicon Image (Medley) RAID struct */ 3488c1b3d7c5SThomas E. Spanjaard for (checksum = 0, ptr = (u_int16_t *)meta, count = 0; count < 160; count++) 3489c1b3d7c5SThomas E. Spanjaard checksum += *ptr++; 3490c1b3d7c5SThomas E. Spanjaard if (checksum) { 3491c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3492c1b3d7c5SThomas E. Spanjaard device_printf(parent, "Silicon Image check1 failed\n"); 3493c1b3d7c5SThomas E. Spanjaard goto sii_out; 3494c1b3d7c5SThomas E. Spanjaard } 3495c1b3d7c5SThomas E. Spanjaard 3496c1b3d7c5SThomas E. Spanjaard for (checksum = 0, ptr = (u_int16_t *)meta, count = 0; count < 256; count++) 3497c1b3d7c5SThomas E. Spanjaard checksum += *ptr++; 3498c1b3d7c5SThomas E. Spanjaard if (checksum != meta->checksum_1) { 3499c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3500c1b3d7c5SThomas E. Spanjaard device_printf(parent, "Silicon Image check2 failed\n"); 3501c1b3d7c5SThomas E. Spanjaard goto sii_out; 3502c1b3d7c5SThomas E. Spanjaard } 3503c1b3d7c5SThomas E. Spanjaard 3504c1b3d7c5SThomas E. Spanjaard /* check verison */ 3505c1b3d7c5SThomas E. Spanjaard if (meta->version_major != 0x0002 || 3506c1b3d7c5SThomas E. Spanjaard (meta->version_minor != 0x0000 && meta->version_minor != 0x0001)) { 3507c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3508c1b3d7c5SThomas E. Spanjaard device_printf(parent, "Silicon Image check3 failed\n"); 3509c1b3d7c5SThomas E. Spanjaard goto sii_out; 3510c1b3d7c5SThomas E. Spanjaard } 3511c1b3d7c5SThomas E. Spanjaard 3512c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3513c1b3d7c5SThomas E. Spanjaard ata_raid_sii_print_meta(meta); 3514c1b3d7c5SThomas E. Spanjaard 3515c1b3d7c5SThomas E. Spanjaard /* now convert Silicon Image meta into our generic form */ 3516c1b3d7c5SThomas E. Spanjaard for (array = 0; array < MAX_ARRAYS; array++) { 3517c1b3d7c5SThomas E. Spanjaard if (!raidp[array]) { 3518c1b3d7c5SThomas E. Spanjaard raidp[array] = 3519d438c7c2SThomas E. Spanjaard (struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR, 3520d438c7c2SThomas E. Spanjaard M_WAITOK | M_ZERO); 3521c1b3d7c5SThomas E. Spanjaard } 3522c1b3d7c5SThomas E. Spanjaard raid = raidp[array]; 3523c1b3d7c5SThomas E. Spanjaard if (raid->format && (raid->format != AR_F_SII_RAID)) 3524c1b3d7c5SThomas E. Spanjaard continue; 3525c1b3d7c5SThomas E. Spanjaard 3526c1b3d7c5SThomas E. Spanjaard if (raid->format == AR_F_SII_RAID && 3527c1b3d7c5SThomas E. Spanjaard (raid->magic_0 != *((u_int64_t *)meta->timestamp))) { 3528c1b3d7c5SThomas E. Spanjaard continue; 3529c1b3d7c5SThomas E. Spanjaard } 3530c1b3d7c5SThomas E. Spanjaard 3531c1b3d7c5SThomas E. Spanjaard /* update our knowledge about the array config based on generation */ 3532c1b3d7c5SThomas E. Spanjaard if (!meta->generation || meta->generation > raid->generation) { 3533c1b3d7c5SThomas E. Spanjaard switch (meta->type) { 3534c1b3d7c5SThomas E. Spanjaard case SII_T_RAID0: 3535c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID0; 3536c1b3d7c5SThomas E. Spanjaard break; 3537c1b3d7c5SThomas E. Spanjaard 3538c1b3d7c5SThomas E. Spanjaard case SII_T_RAID1: 3539c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID1; 3540c1b3d7c5SThomas E. Spanjaard break; 3541c1b3d7c5SThomas E. Spanjaard 3542c1b3d7c5SThomas E. Spanjaard case SII_T_RAID01: 3543c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID01; 3544c1b3d7c5SThomas E. Spanjaard break; 3545c1b3d7c5SThomas E. Spanjaard 3546c1b3d7c5SThomas E. Spanjaard case SII_T_SPARE: 3547c1b3d7c5SThomas E. Spanjaard device_printf(parent, "Silicon Image SPARE disk\n"); 3548d438c7c2SThomas E. Spanjaard kfree(raidp[array], M_AR); 3549c1b3d7c5SThomas E. Spanjaard raidp[array] = NULL; 3550c1b3d7c5SThomas E. Spanjaard goto sii_out; 3551c1b3d7c5SThomas E. Spanjaard 3552c1b3d7c5SThomas E. Spanjaard default: 3553c1b3d7c5SThomas E. Spanjaard device_printf(parent,"Silicon Image unknown RAID type 0x%02x\n", 3554c1b3d7c5SThomas E. Spanjaard meta->type); 3555d438c7c2SThomas E. Spanjaard kfree(raidp[array], M_AR); 3556c1b3d7c5SThomas E. Spanjaard raidp[array] = NULL; 3557c1b3d7c5SThomas E. Spanjaard goto sii_out; 3558c1b3d7c5SThomas E. Spanjaard } 3559c1b3d7c5SThomas E. Spanjaard raid->magic_0 = *((u_int64_t *)meta->timestamp); 3560c1b3d7c5SThomas E. Spanjaard raid->format = AR_F_SII_RAID; 3561c1b3d7c5SThomas E. Spanjaard raid->generation = meta->generation; 3562c1b3d7c5SThomas E. Spanjaard raid->interleave = meta->stripe_sectors; 3563c1b3d7c5SThomas E. Spanjaard raid->width = (meta->raid0_disks != 0xff) ? meta->raid0_disks : 1; 3564c1b3d7c5SThomas E. Spanjaard raid->total_disks = 3565c1b3d7c5SThomas E. Spanjaard ((meta->raid0_disks != 0xff) ? meta->raid0_disks : 0) + 3566c1b3d7c5SThomas E. Spanjaard ((meta->raid1_disks != 0xff) ? meta->raid1_disks : 0); 3567c1b3d7c5SThomas E. Spanjaard raid->total_sectors = meta->total_sectors; 3568c1b3d7c5SThomas E. Spanjaard raid->heads = 255; 3569c1b3d7c5SThomas E. Spanjaard raid->sectors = 63; 3570c1b3d7c5SThomas E. Spanjaard raid->cylinders = raid->total_sectors / (63 * 255); 3571c1b3d7c5SThomas E. Spanjaard raid->offset_sectors = 0; 3572c1b3d7c5SThomas E. Spanjaard raid->rebuild_lba = meta->rebuild_lba; 3573c1b3d7c5SThomas E. Spanjaard raid->lun = array; 3574c1b3d7c5SThomas E. Spanjaard strncpy(raid->name, meta->name, 3575c1b3d7c5SThomas E. Spanjaard min(sizeof(raid->name), sizeof(meta->name))); 3576c1b3d7c5SThomas E. Spanjaard 3577c1b3d7c5SThomas E. Spanjaard /* clear out any old info */ 3578c1b3d7c5SThomas E. Spanjaard if (raid->generation) { 3579c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < raid->total_disks; disk++) { 3580c1b3d7c5SThomas E. Spanjaard raid->disks[disk].dev = NULL; 3581c1b3d7c5SThomas E. Spanjaard raid->disks[disk].flags = 0; 3582c1b3d7c5SThomas E. Spanjaard } 3583c1b3d7c5SThomas E. Spanjaard } 3584c1b3d7c5SThomas E. Spanjaard } 3585c1b3d7c5SThomas E. Spanjaard if (meta->generation >= raid->generation) { 3586c1b3d7c5SThomas E. Spanjaard /* XXX SOS add check for the right physical disk by serial# */ 3587c1b3d7c5SThomas E. Spanjaard if (meta->status & SII_S_READY) { 3588c1b3d7c5SThomas E. Spanjaard int disk_number = (raid->type == AR_T_RAID01) ? 3589c1b3d7c5SThomas E. Spanjaard meta->raid1_ident + (meta->raid0_ident << 1) : 3590c1b3d7c5SThomas E. Spanjaard meta->disk_number; 3591c1b3d7c5SThomas E. Spanjaard 3592c1b3d7c5SThomas E. Spanjaard raid->disks[disk_number].dev = parent; 3593c1b3d7c5SThomas E. Spanjaard raid->disks[disk_number].sectors = 3594c1b3d7c5SThomas E. Spanjaard raid->total_sectors / raid->width; 3595c1b3d7c5SThomas E. Spanjaard raid->disks[disk_number].flags = 3596c1b3d7c5SThomas E. Spanjaard (AR_DF_ONLINE | AR_DF_PRESENT | AR_DF_ASSIGNED); 3597c1b3d7c5SThomas E. Spanjaard ars->raid[raid->volume] = raid; 3598c1b3d7c5SThomas E. Spanjaard ars->disk_number[raid->volume] = disk_number; 3599c1b3d7c5SThomas E. Spanjaard retval = 1; 3600c1b3d7c5SThomas E. Spanjaard } 3601c1b3d7c5SThomas E. Spanjaard } 3602c1b3d7c5SThomas E. Spanjaard break; 3603c1b3d7c5SThomas E. Spanjaard } 3604c1b3d7c5SThomas E. Spanjaard 3605c1b3d7c5SThomas E. Spanjaard sii_out: 3606d438c7c2SThomas E. Spanjaard kfree(meta, M_AR); 3607c1b3d7c5SThomas E. Spanjaard return retval; 3608c1b3d7c5SThomas E. Spanjaard } 3609c1b3d7c5SThomas E. Spanjaard 3610c1b3d7c5SThomas E. Spanjaard /* Silicon Integrated Systems Metadata */ 3611c1b3d7c5SThomas E. Spanjaard static int 3612c1b3d7c5SThomas E. Spanjaard ata_raid_sis_read_meta(device_t dev, struct ar_softc **raidp) 3613c1b3d7c5SThomas E. Spanjaard { 3614c1b3d7c5SThomas E. Spanjaard struct ata_raid_subdisk *ars = device_get_softc(dev); 3615c1b3d7c5SThomas E. Spanjaard device_t parent = device_get_parent(dev); 3616c1b3d7c5SThomas E. Spanjaard struct sis_raid_conf *meta; 3617c1b3d7c5SThomas E. Spanjaard struct ar_softc *raid = NULL; 3618c1b3d7c5SThomas E. Spanjaard int array, disk_number, drive, retval = 0; 3619c1b3d7c5SThomas E. Spanjaard 3620978400d3SSascha Wildner meta = (struct sis_raid_conf *)kmalloc(sizeof(struct sis_raid_conf), M_AR, 3621978400d3SSascha Wildner M_WAITOK | M_ZERO); 3622c1b3d7c5SThomas E. Spanjaard 3623c1b3d7c5SThomas E. Spanjaard if (ata_raid_rw(parent, SIS_LBA(parent), 3624c1b3d7c5SThomas E. Spanjaard meta, sizeof(struct sis_raid_conf), ATA_R_READ)) { 3625c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3626c1b3d7c5SThomas E. Spanjaard device_printf(parent, 3627c1b3d7c5SThomas E. Spanjaard "Silicon Integrated Systems read metadata failed\n"); 3628c1b3d7c5SThomas E. Spanjaard } 3629c1b3d7c5SThomas E. Spanjaard 3630c1b3d7c5SThomas E. Spanjaard /* check for SiS magic */ 3631c1b3d7c5SThomas E. Spanjaard if (meta->magic != SIS_MAGIC) { 3632c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3633c1b3d7c5SThomas E. Spanjaard device_printf(parent, 3634c1b3d7c5SThomas E. Spanjaard "Silicon Integrated Systems check1 failed\n"); 3635c1b3d7c5SThomas E. Spanjaard goto sis_out; 3636c1b3d7c5SThomas E. Spanjaard } 3637c1b3d7c5SThomas E. Spanjaard 3638c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3639c1b3d7c5SThomas E. Spanjaard ata_raid_sis_print_meta(meta); 3640c1b3d7c5SThomas E. Spanjaard 3641c1b3d7c5SThomas E. Spanjaard /* now convert SiS meta into our generic form */ 3642c1b3d7c5SThomas E. Spanjaard for (array = 0; array < MAX_ARRAYS; array++) { 3643c1b3d7c5SThomas E. Spanjaard if (!raidp[array]) { 3644c1b3d7c5SThomas E. Spanjaard raidp[array] = 3645d438c7c2SThomas E. Spanjaard (struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR, 3646d438c7c2SThomas E. Spanjaard M_WAITOK | M_ZERO); 3647c1b3d7c5SThomas E. Spanjaard } 3648c1b3d7c5SThomas E. Spanjaard 3649c1b3d7c5SThomas E. Spanjaard raid = raidp[array]; 3650c1b3d7c5SThomas E. Spanjaard if (raid->format && (raid->format != AR_F_SIS_RAID)) 3651c1b3d7c5SThomas E. Spanjaard continue; 3652c1b3d7c5SThomas E. Spanjaard 3653c1b3d7c5SThomas E. Spanjaard if ((raid->format == AR_F_SIS_RAID) && 3654c1b3d7c5SThomas E. Spanjaard ((raid->magic_0 != meta->controller_pci_id) || 3655c1b3d7c5SThomas E. Spanjaard (raid->magic_1 != meta->timestamp))) { 3656c1b3d7c5SThomas E. Spanjaard continue; 3657c1b3d7c5SThomas E. Spanjaard } 3658c1b3d7c5SThomas E. Spanjaard 3659c1b3d7c5SThomas E. Spanjaard switch (meta->type_total_disks & SIS_T_MASK) { 3660c1b3d7c5SThomas E. Spanjaard case SIS_T_JBOD: 3661c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_JBOD; 3662c1b3d7c5SThomas E. Spanjaard raid->width = (meta->type_total_disks & SIS_D_MASK); 3663c1b3d7c5SThomas E. Spanjaard raid->total_sectors += SIS_LBA(parent); 3664c1b3d7c5SThomas E. Spanjaard break; 3665c1b3d7c5SThomas E. Spanjaard 3666c1b3d7c5SThomas E. Spanjaard case SIS_T_RAID0: 3667c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID0; 3668c1b3d7c5SThomas E. Spanjaard raid->width = (meta->type_total_disks & SIS_D_MASK); 3669c1b3d7c5SThomas E. Spanjaard if (!raid->total_sectors || 3670c1b3d7c5SThomas E. Spanjaard (raid->total_sectors > (raid->width * SIS_LBA(parent)))) 3671c1b3d7c5SThomas E. Spanjaard raid->total_sectors = raid->width * SIS_LBA(parent); 3672c1b3d7c5SThomas E. Spanjaard break; 3673c1b3d7c5SThomas E. Spanjaard 3674c1b3d7c5SThomas E. Spanjaard case SIS_T_RAID1: 3675c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID1; 3676c1b3d7c5SThomas E. Spanjaard raid->width = 1; 3677c1b3d7c5SThomas E. Spanjaard if (!raid->total_sectors || (raid->total_sectors > SIS_LBA(parent))) 3678c1b3d7c5SThomas E. Spanjaard raid->total_sectors = SIS_LBA(parent); 3679c1b3d7c5SThomas E. Spanjaard break; 3680c1b3d7c5SThomas E. Spanjaard 3681c1b3d7c5SThomas E. Spanjaard default: 3682c1b3d7c5SThomas E. Spanjaard device_printf(parent, "Silicon Integrated Systems " 3683c1b3d7c5SThomas E. Spanjaard "unknown RAID type 0x%08x\n", meta->magic); 3684d438c7c2SThomas E. Spanjaard kfree(raidp[array], M_AR); 3685c1b3d7c5SThomas E. Spanjaard raidp[array] = NULL; 3686c1b3d7c5SThomas E. Spanjaard goto sis_out; 3687c1b3d7c5SThomas E. Spanjaard } 3688c1b3d7c5SThomas E. Spanjaard raid->magic_0 = meta->controller_pci_id; 3689c1b3d7c5SThomas E. Spanjaard raid->magic_1 = meta->timestamp; 3690c1b3d7c5SThomas E. Spanjaard raid->format = AR_F_SIS_RAID; 3691c1b3d7c5SThomas E. Spanjaard raid->generation = 0; 3692c1b3d7c5SThomas E. Spanjaard raid->interleave = meta->stripe_sectors; 3693c1b3d7c5SThomas E. Spanjaard raid->total_disks = (meta->type_total_disks & SIS_D_MASK); 3694c1b3d7c5SThomas E. Spanjaard raid->heads = 255; 3695c1b3d7c5SThomas E. Spanjaard raid->sectors = 63; 3696c1b3d7c5SThomas E. Spanjaard raid->cylinders = raid->total_sectors / (63 * 255); 3697c1b3d7c5SThomas E. Spanjaard raid->offset_sectors = 0; 3698c1b3d7c5SThomas E. Spanjaard raid->rebuild_lba = 0; 3699c1b3d7c5SThomas E. Spanjaard raid->lun = array; 3700c1b3d7c5SThomas E. Spanjaard /* XXX SOS if total_disks > 2 this doesn't float */ 3701c1b3d7c5SThomas E. Spanjaard if (((meta->disks & SIS_D_MASTER) >> 4) == meta->disk_number) 3702c1b3d7c5SThomas E. Spanjaard disk_number = 0; 3703c1b3d7c5SThomas E. Spanjaard else 3704c1b3d7c5SThomas E. Spanjaard disk_number = 1; 3705c1b3d7c5SThomas E. Spanjaard 3706c1b3d7c5SThomas E. Spanjaard for (drive = 0; drive < raid->total_disks; drive++) { 3707c1b3d7c5SThomas E. Spanjaard raid->disks[drive].sectors = raid->total_sectors/raid->width; 3708c1b3d7c5SThomas E. Spanjaard if (drive == disk_number) { 3709c1b3d7c5SThomas E. Spanjaard raid->disks[disk_number].dev = parent; 3710c1b3d7c5SThomas E. Spanjaard raid->disks[disk_number].flags = 3711c1b3d7c5SThomas E. Spanjaard (AR_DF_ONLINE | AR_DF_PRESENT | AR_DF_ASSIGNED); 3712c1b3d7c5SThomas E. Spanjaard ars->raid[raid->volume] = raid; 3713c1b3d7c5SThomas E. Spanjaard ars->disk_number[raid->volume] = disk_number; 3714c1b3d7c5SThomas E. Spanjaard } 3715c1b3d7c5SThomas E. Spanjaard } 3716c1b3d7c5SThomas E. Spanjaard retval = 1; 3717c1b3d7c5SThomas E. Spanjaard break; 3718c1b3d7c5SThomas E. Spanjaard } 3719c1b3d7c5SThomas E. Spanjaard 3720c1b3d7c5SThomas E. Spanjaard sis_out: 3721d438c7c2SThomas E. Spanjaard kfree(meta, M_AR); 3722c1b3d7c5SThomas E. Spanjaard return retval; 3723c1b3d7c5SThomas E. Spanjaard } 3724c1b3d7c5SThomas E. Spanjaard 3725c1b3d7c5SThomas E. Spanjaard static int 3726c1b3d7c5SThomas E. Spanjaard ata_raid_sis_write_meta(struct ar_softc *rdp) 3727c1b3d7c5SThomas E. Spanjaard { 3728c1b3d7c5SThomas E. Spanjaard struct sis_raid_conf *meta; 3729c1b3d7c5SThomas E. Spanjaard struct timeval timestamp; 3730c1b3d7c5SThomas E. Spanjaard int disk, error = 0; 3731c1b3d7c5SThomas E. Spanjaard 3732978400d3SSascha Wildner meta = (struct sis_raid_conf *)kmalloc(sizeof(struct sis_raid_conf), M_AR, 3733978400d3SSascha Wildner M_WAITOK | M_ZERO); 3734c1b3d7c5SThomas E. Spanjaard 3735c1b3d7c5SThomas E. Spanjaard rdp->generation++; 3736c1b3d7c5SThomas E. Spanjaard microtime(×tamp); 3737c1b3d7c5SThomas E. Spanjaard 3738c1b3d7c5SThomas E. Spanjaard meta->magic = SIS_MAGIC; 3739c1b3d7c5SThomas E. Spanjaard /* XXX SOS if total_disks > 2 this doesn't float */ 3740c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < rdp->total_disks; disk++) { 3741c1b3d7c5SThomas E. Spanjaard if (rdp->disks[disk].dev) { 3742c1b3d7c5SThomas E. Spanjaard struct ata_channel *ch = 3743c1b3d7c5SThomas E. Spanjaard device_get_softc(device_get_parent(rdp->disks[disk].dev)); 3744c1b3d7c5SThomas E. Spanjaard struct ata_device *atadev = device_get_softc(rdp->disks[disk].dev); 3745c1b3d7c5SThomas E. Spanjaard int disk_number = 1 + ATA_DEV(atadev->unit) + (ch->unit << 1); 3746c1b3d7c5SThomas E. Spanjaard 3747c1b3d7c5SThomas E. Spanjaard meta->disks |= disk_number << ((1 - disk) << 2); 3748c1b3d7c5SThomas E. Spanjaard } 3749c1b3d7c5SThomas E. Spanjaard } 3750c1b3d7c5SThomas E. Spanjaard switch (rdp->type) { 3751c1b3d7c5SThomas E. Spanjaard case AR_T_JBOD: 3752c1b3d7c5SThomas E. Spanjaard meta->type_total_disks = SIS_T_JBOD; 3753c1b3d7c5SThomas E. Spanjaard break; 3754c1b3d7c5SThomas E. Spanjaard 3755c1b3d7c5SThomas E. Spanjaard case AR_T_RAID0: 3756c1b3d7c5SThomas E. Spanjaard meta->type_total_disks = SIS_T_RAID0; 3757c1b3d7c5SThomas E. Spanjaard break; 3758c1b3d7c5SThomas E. Spanjaard 3759c1b3d7c5SThomas E. Spanjaard case AR_T_RAID1: 3760c1b3d7c5SThomas E. Spanjaard meta->type_total_disks = SIS_T_RAID1; 3761c1b3d7c5SThomas E. Spanjaard break; 3762c1b3d7c5SThomas E. Spanjaard 3763c1b3d7c5SThomas E. Spanjaard default: 3764d438c7c2SThomas E. Spanjaard kfree(meta, M_AR); 3765c1b3d7c5SThomas E. Spanjaard return ENODEV; 3766c1b3d7c5SThomas E. Spanjaard } 3767c1b3d7c5SThomas E. Spanjaard meta->type_total_disks |= (rdp->total_disks & SIS_D_MASK); 3768c1b3d7c5SThomas E. Spanjaard meta->stripe_sectors = rdp->interleave; 3769c1b3d7c5SThomas E. Spanjaard meta->timestamp = timestamp.tv_sec; 3770c1b3d7c5SThomas E. Spanjaard 3771c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < rdp->total_disks; disk++) { 3772c1b3d7c5SThomas E. Spanjaard if (rdp->disks[disk].dev) { 3773c1b3d7c5SThomas E. Spanjaard struct ata_channel *ch = 3774c1b3d7c5SThomas E. Spanjaard device_get_softc(device_get_parent(rdp->disks[disk].dev)); 3775c1b3d7c5SThomas E. Spanjaard struct ata_device *atadev = device_get_softc(rdp->disks[disk].dev); 3776c1b3d7c5SThomas E. Spanjaard 3777c1b3d7c5SThomas E. Spanjaard meta->controller_pci_id = 3778c1b3d7c5SThomas E. Spanjaard (pci_get_vendor(GRANDPARENT(rdp->disks[disk].dev)) << 16) | 3779c1b3d7c5SThomas E. Spanjaard pci_get_device(GRANDPARENT(rdp->disks[disk].dev)); 3780c1b3d7c5SThomas E. Spanjaard bcopy(atadev->param.model, meta->model, sizeof(meta->model)); 3781c1b3d7c5SThomas E. Spanjaard 3782c1b3d7c5SThomas E. Spanjaard /* XXX SOS if total_disks > 2 this may not float */ 3783c1b3d7c5SThomas E. Spanjaard meta->disk_number = 1 + ATA_DEV(atadev->unit) + (ch->unit << 1); 3784c1b3d7c5SThomas E. Spanjaard 3785c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3786c1b3d7c5SThomas E. Spanjaard ata_raid_sis_print_meta(meta); 3787c1b3d7c5SThomas E. Spanjaard 3788c1b3d7c5SThomas E. Spanjaard if (ata_raid_rw(rdp->disks[disk].dev, 3789c1b3d7c5SThomas E. Spanjaard SIS_LBA(rdp->disks[disk].dev), 3790c1b3d7c5SThomas E. Spanjaard meta, sizeof(struct sis_raid_conf), 3791c1b3d7c5SThomas E. Spanjaard ATA_R_WRITE | ATA_R_DIRECT)) { 3792c1b3d7c5SThomas E. Spanjaard device_printf(rdp->disks[disk].dev, "write metadata failed\n"); 3793c1b3d7c5SThomas E. Spanjaard error = EIO; 3794c1b3d7c5SThomas E. Spanjaard } 3795c1b3d7c5SThomas E. Spanjaard } 3796c1b3d7c5SThomas E. Spanjaard } 3797d438c7c2SThomas E. Spanjaard kfree(meta, M_AR); 3798c1b3d7c5SThomas E. Spanjaard return error; 3799c1b3d7c5SThomas E. Spanjaard } 3800c1b3d7c5SThomas E. Spanjaard 3801c1b3d7c5SThomas E. Spanjaard /* VIA Tech V-RAID Metadata */ 3802c1b3d7c5SThomas E. Spanjaard static int 3803c1b3d7c5SThomas E. Spanjaard ata_raid_via_read_meta(device_t dev, struct ar_softc **raidp) 3804c1b3d7c5SThomas E. Spanjaard { 3805c1b3d7c5SThomas E. Spanjaard struct ata_raid_subdisk *ars = device_get_softc(dev); 3806c1b3d7c5SThomas E. Spanjaard device_t parent = device_get_parent(dev); 3807c1b3d7c5SThomas E. Spanjaard struct via_raid_conf *meta; 3808c1b3d7c5SThomas E. Spanjaard struct ar_softc *raid = NULL; 3809c1b3d7c5SThomas E. Spanjaard u_int8_t checksum, *ptr; 3810c1b3d7c5SThomas E. Spanjaard int array, count, disk, retval = 0; 3811c1b3d7c5SThomas E. Spanjaard 3812978400d3SSascha Wildner meta = (struct via_raid_conf *)kmalloc(sizeof(struct via_raid_conf), M_AR, 3813978400d3SSascha Wildner M_WAITOK | M_ZERO); 3814c1b3d7c5SThomas E. Spanjaard 3815c1b3d7c5SThomas E. Spanjaard if (ata_raid_rw(parent, VIA_LBA(parent), 3816c1b3d7c5SThomas E. Spanjaard meta, sizeof(struct via_raid_conf), ATA_R_READ)) { 3817c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3818c1b3d7c5SThomas E. Spanjaard device_printf(parent, "VIA read metadata failed\n"); 3819c1b3d7c5SThomas E. Spanjaard goto via_out; 3820c1b3d7c5SThomas E. Spanjaard } 3821c1b3d7c5SThomas E. Spanjaard 3822c1b3d7c5SThomas E. Spanjaard /* check if this is a VIA RAID struct */ 3823c1b3d7c5SThomas E. Spanjaard if (meta->magic != VIA_MAGIC) { 3824c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3825c1b3d7c5SThomas E. Spanjaard device_printf(parent, "VIA check1 failed\n"); 3826c1b3d7c5SThomas E. Spanjaard goto via_out; 3827c1b3d7c5SThomas E. Spanjaard } 3828c1b3d7c5SThomas E. Spanjaard 3829c1b3d7c5SThomas E. Spanjaard /* calculate checksum and compare for valid */ 3830c1b3d7c5SThomas E. Spanjaard for (checksum = 0, ptr = (u_int8_t *)meta, count = 0; count < 50; count++) 3831c1b3d7c5SThomas E. Spanjaard checksum += *ptr++; 3832c1b3d7c5SThomas E. Spanjaard if (checksum != meta->checksum) { 3833c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3834c1b3d7c5SThomas E. Spanjaard device_printf(parent, "VIA check2 failed\n"); 3835c1b3d7c5SThomas E. Spanjaard goto via_out; 3836c1b3d7c5SThomas E. Spanjaard } 3837c1b3d7c5SThomas E. Spanjaard 3838c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 3839c1b3d7c5SThomas E. Spanjaard ata_raid_via_print_meta(meta); 3840c1b3d7c5SThomas E. Spanjaard 3841c1b3d7c5SThomas E. Spanjaard /* now convert VIA meta into our generic form */ 3842c1b3d7c5SThomas E. Spanjaard for (array = 0; array < MAX_ARRAYS; array++) { 3843c1b3d7c5SThomas E. Spanjaard if (!raidp[array]) { 3844c1b3d7c5SThomas E. Spanjaard raidp[array] = 3845d438c7c2SThomas E. Spanjaard (struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR, 3846d438c7c2SThomas E. Spanjaard M_WAITOK | M_ZERO); 3847c1b3d7c5SThomas E. Spanjaard } 3848c1b3d7c5SThomas E. Spanjaard raid = raidp[array]; 3849c1b3d7c5SThomas E. Spanjaard if (raid->format && (raid->format != AR_F_VIA_RAID)) 3850c1b3d7c5SThomas E. Spanjaard continue; 3851c1b3d7c5SThomas E. Spanjaard 3852c1b3d7c5SThomas E. Spanjaard if (raid->format == AR_F_VIA_RAID && (raid->magic_0 != meta->disks[0])) 3853c1b3d7c5SThomas E. Spanjaard continue; 3854c1b3d7c5SThomas E. Spanjaard 3855c1b3d7c5SThomas E. Spanjaard switch (meta->type & VIA_T_MASK) { 3856c1b3d7c5SThomas E. Spanjaard case VIA_T_RAID0: 3857c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID0; 3858c1b3d7c5SThomas E. Spanjaard raid->width = meta->stripe_layout & VIA_L_DISKS; 3859c1b3d7c5SThomas E. Spanjaard if (!raid->total_sectors || 3860c1b3d7c5SThomas E. Spanjaard (raid->total_sectors > (raid->width * meta->disk_sectors))) 3861c1b3d7c5SThomas E. Spanjaard raid->total_sectors = raid->width * meta->disk_sectors; 3862c1b3d7c5SThomas E. Spanjaard break; 3863c1b3d7c5SThomas E. Spanjaard 3864c1b3d7c5SThomas E. Spanjaard case VIA_T_RAID1: 3865c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID1; 3866c1b3d7c5SThomas E. Spanjaard raid->width = 1; 3867c1b3d7c5SThomas E. Spanjaard raid->total_sectors = meta->disk_sectors; 3868c1b3d7c5SThomas E. Spanjaard break; 3869c1b3d7c5SThomas E. Spanjaard 3870c1b3d7c5SThomas E. Spanjaard case VIA_T_RAID01: 3871c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID01; 3872c1b3d7c5SThomas E. Spanjaard raid->width = meta->stripe_layout & VIA_L_DISKS; 3873c1b3d7c5SThomas E. Spanjaard if (!raid->total_sectors || 3874c1b3d7c5SThomas E. Spanjaard (raid->total_sectors > (raid->width * meta->disk_sectors))) 3875c1b3d7c5SThomas E. Spanjaard raid->total_sectors = raid->width * meta->disk_sectors; 3876c1b3d7c5SThomas E. Spanjaard break; 3877c1b3d7c5SThomas E. Spanjaard 3878c1b3d7c5SThomas E. Spanjaard case VIA_T_RAID5: 3879c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_RAID5; 3880c1b3d7c5SThomas E. Spanjaard raid->width = meta->stripe_layout & VIA_L_DISKS; 3881c1b3d7c5SThomas E. Spanjaard if (!raid->total_sectors || 3882c1b3d7c5SThomas E. Spanjaard (raid->total_sectors > ((raid->width - 1)*meta->disk_sectors))) 3883c1b3d7c5SThomas E. Spanjaard raid->total_sectors = (raid->width - 1) * meta->disk_sectors; 3884c1b3d7c5SThomas E. Spanjaard break; 3885c1b3d7c5SThomas E. Spanjaard 3886c1b3d7c5SThomas E. Spanjaard case VIA_T_SPAN: 3887c1b3d7c5SThomas E. Spanjaard raid->type = AR_T_SPAN; 3888c1b3d7c5SThomas E. Spanjaard raid->width = 1; 3889c1b3d7c5SThomas E. Spanjaard raid->total_sectors += meta->disk_sectors; 3890c1b3d7c5SThomas E. Spanjaard break; 3891c1b3d7c5SThomas E. Spanjaard 3892c1b3d7c5SThomas E. Spanjaard default: 3893c1b3d7c5SThomas E. Spanjaard device_printf(parent,"VIA unknown RAID type 0x%02x\n", meta->type); 3894d438c7c2SThomas E. Spanjaard kfree(raidp[array], M_AR); 3895c1b3d7c5SThomas E. Spanjaard raidp[array] = NULL; 3896c1b3d7c5SThomas E. Spanjaard goto via_out; 3897c1b3d7c5SThomas E. Spanjaard } 3898c1b3d7c5SThomas E. Spanjaard raid->magic_0 = meta->disks[0]; 3899c1b3d7c5SThomas E. Spanjaard raid->format = AR_F_VIA_RAID; 3900c1b3d7c5SThomas E. Spanjaard raid->generation = 0; 3901c1b3d7c5SThomas E. Spanjaard raid->interleave = 3902c1b3d7c5SThomas E. Spanjaard 0x08 << ((meta->stripe_layout & VIA_L_MASK) >> VIA_L_SHIFT); 3903c1b3d7c5SThomas E. Spanjaard for (count = 0, disk = 0; disk < 8; disk++) 3904c1b3d7c5SThomas E. Spanjaard if (meta->disks[disk]) 3905c1b3d7c5SThomas E. Spanjaard count++; 3906c1b3d7c5SThomas E. Spanjaard raid->total_disks = count; 3907c1b3d7c5SThomas E. Spanjaard raid->heads = 255; 3908c1b3d7c5SThomas E. Spanjaard raid->sectors = 63; 3909c1b3d7c5SThomas E. Spanjaard raid->cylinders = raid->total_sectors / (63 * 255); 3910c1b3d7c5SThomas E. Spanjaard raid->offset_sectors = 0; 3911c1b3d7c5SThomas E. Spanjaard raid->rebuild_lba = 0; 3912c1b3d7c5SThomas E. Spanjaard raid->lun = array; 3913c1b3d7c5SThomas E. Spanjaard 3914c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < raid->total_disks; disk++) { 3915c1b3d7c5SThomas E. Spanjaard if (meta->disks[disk] == meta->disk_id) { 3916c1b3d7c5SThomas E. Spanjaard raid->disks[disk].dev = parent; 3917c1b3d7c5SThomas E. Spanjaard bcopy(&meta->disk_id, raid->disks[disk].serial, 3918c1b3d7c5SThomas E. Spanjaard sizeof(u_int32_t)); 3919c1b3d7c5SThomas E. Spanjaard raid->disks[disk].sectors = meta->disk_sectors; 3920c1b3d7c5SThomas E. Spanjaard raid->disks[disk].flags = 3921c1b3d7c5SThomas E. Spanjaard (AR_DF_ONLINE | AR_DF_PRESENT | AR_DF_ASSIGNED); 3922c1b3d7c5SThomas E. Spanjaard ars->raid[raid->volume] = raid; 3923c1b3d7c5SThomas E. Spanjaard ars->disk_number[raid->volume] = disk; 3924c1b3d7c5SThomas E. Spanjaard retval = 1; 3925c1b3d7c5SThomas E. Spanjaard break; 3926c1b3d7c5SThomas E. Spanjaard } 3927c1b3d7c5SThomas E. Spanjaard } 3928c1b3d7c5SThomas E. Spanjaard break; 3929c1b3d7c5SThomas E. Spanjaard } 3930c1b3d7c5SThomas E. Spanjaard 3931c1b3d7c5SThomas E. Spanjaard via_out: 3932d438c7c2SThomas E. Spanjaard kfree(meta, M_AR); 3933c1b3d7c5SThomas E. Spanjaard return retval; 3934c1b3d7c5SThomas E. Spanjaard } 3935c1b3d7c5SThomas E. Spanjaard 3936c1b3d7c5SThomas E. Spanjaard static int 3937c1b3d7c5SThomas E. Spanjaard ata_raid_via_write_meta(struct ar_softc *rdp) 3938c1b3d7c5SThomas E. Spanjaard { 3939c1b3d7c5SThomas E. Spanjaard struct via_raid_conf *meta; 3940c1b3d7c5SThomas E. Spanjaard int disk, error = 0; 3941c1b3d7c5SThomas E. Spanjaard 3942978400d3SSascha Wildner meta = (struct via_raid_conf *)kmalloc(sizeof(struct via_raid_conf), M_AR, 3943978400d3SSascha Wildner M_WAITOK | M_ZERO); 3944c1b3d7c5SThomas E. Spanjaard 3945c1b3d7c5SThomas E. Spanjaard rdp->generation++; 3946c1b3d7c5SThomas E. Spanjaard 3947c1b3d7c5SThomas E. Spanjaard meta->magic = VIA_MAGIC; 3948c1b3d7c5SThomas E. Spanjaard meta->dummy_0 = 0x02; 3949c1b3d7c5SThomas E. Spanjaard switch (rdp->type) { 3950c1b3d7c5SThomas E. Spanjaard case AR_T_SPAN: 3951c1b3d7c5SThomas E. Spanjaard meta->type = VIA_T_SPAN; 3952c1b3d7c5SThomas E. Spanjaard meta->stripe_layout = (rdp->total_disks & VIA_L_DISKS); 3953c1b3d7c5SThomas E. Spanjaard break; 3954c1b3d7c5SThomas E. Spanjaard 3955c1b3d7c5SThomas E. Spanjaard case AR_T_RAID0: 3956c1b3d7c5SThomas E. Spanjaard meta->type = VIA_T_RAID0; 3957c1b3d7c5SThomas E. Spanjaard meta->stripe_layout = ((rdp->interleave >> 1) & VIA_L_MASK); 3958c1b3d7c5SThomas E. Spanjaard meta->stripe_layout |= (rdp->total_disks & VIA_L_DISKS); 3959c1b3d7c5SThomas E. Spanjaard break; 3960c1b3d7c5SThomas E. Spanjaard 3961c1b3d7c5SThomas E. Spanjaard case AR_T_RAID1: 3962c1b3d7c5SThomas E. Spanjaard meta->type = VIA_T_RAID1; 3963c1b3d7c5SThomas E. Spanjaard meta->stripe_layout = (rdp->total_disks & VIA_L_DISKS); 3964c1b3d7c5SThomas E. Spanjaard break; 3965c1b3d7c5SThomas E. Spanjaard 3966c1b3d7c5SThomas E. Spanjaard case AR_T_RAID5: 3967c1b3d7c5SThomas E. Spanjaard meta->type = VIA_T_RAID5; 3968c1b3d7c5SThomas E. Spanjaard meta->stripe_layout = ((rdp->interleave >> 1) & VIA_L_MASK); 3969c1b3d7c5SThomas E. Spanjaard meta->stripe_layout |= (rdp->total_disks & VIA_L_DISKS); 3970c1b3d7c5SThomas E. Spanjaard break; 3971c1b3d7c5SThomas E. Spanjaard 3972c1b3d7c5SThomas E. Spanjaard case AR_T_RAID01: 3973c1b3d7c5SThomas E. Spanjaard meta->type = VIA_T_RAID01; 3974c1b3d7c5SThomas E. Spanjaard meta->stripe_layout = ((rdp->interleave >> 1) & VIA_L_MASK); 3975c1b3d7c5SThomas E. Spanjaard meta->stripe_layout |= (rdp->width & VIA_L_DISKS); 3976c1b3d7c5SThomas E. Spanjaard break; 3977c1b3d7c5SThomas E. Spanjaard 3978c1b3d7c5SThomas E. Spanjaard default: 3979d438c7c2SThomas E. Spanjaard kfree(meta, M_AR); 3980c1b3d7c5SThomas E. Spanjaard return ENODEV; 3981c1b3d7c5SThomas E. Spanjaard } 3982c1b3d7c5SThomas E. Spanjaard meta->type |= VIA_T_BOOTABLE; /* XXX SOS */ 3983c1b3d7c5SThomas E. Spanjaard meta->disk_sectors = 3984c1b3d7c5SThomas E. Spanjaard rdp->total_sectors / (rdp->width - (rdp->type == AR_RAID5)); 3985c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < rdp->total_disks; disk++) 3986c1b3d7c5SThomas E. Spanjaard meta->disks[disk] = (u_int32_t)(uintptr_t)rdp->disks[disk].dev; 3987c1b3d7c5SThomas E. Spanjaard 3988c1b3d7c5SThomas E. Spanjaard for (disk = 0; disk < rdp->total_disks; disk++) { 3989c1b3d7c5SThomas E. Spanjaard if (rdp->disks[disk].dev) { 3990c1b3d7c5SThomas E. Spanjaard u_int8_t *ptr; 3991c1b3d7c5SThomas E. Spanjaard int count; 3992c1b3d7c5SThomas E. Spanjaard 3993c1b3d7c5SThomas E. Spanjaard meta->disk_index = disk * sizeof(u_int32_t); 3994c1b3d7c5SThomas E. Spanjaard if (rdp->type == AR_T_RAID01) 3995c1b3d7c5SThomas E. Spanjaard meta->disk_index = ((meta->disk_index & 0x08) << 2) | 3996c1b3d7c5SThomas E. Spanjaard (meta->disk_index & ~0x08); 3997c1b3d7c5SThomas E. Spanjaard meta->disk_id = meta->disks[disk]; 3998c1b3d7c5SThomas E. Spanjaard meta->checksum = 0; 3999c1b3d7c5SThomas E. Spanjaard for (ptr = (u_int8_t *)meta, count = 0; count < 50; count++) 4000c1b3d7c5SThomas E. Spanjaard meta->checksum += *ptr++; 4001c1b3d7c5SThomas E. Spanjaard 4002c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 4003c1b3d7c5SThomas E. Spanjaard ata_raid_via_print_meta(meta); 4004c1b3d7c5SThomas E. Spanjaard 4005c1b3d7c5SThomas E. Spanjaard if (ata_raid_rw(rdp->disks[disk].dev, 4006c1b3d7c5SThomas E. Spanjaard VIA_LBA(rdp->disks[disk].dev), 4007c1b3d7c5SThomas E. Spanjaard meta, sizeof(struct via_raid_conf), 4008c1b3d7c5SThomas E. Spanjaard ATA_R_WRITE | ATA_R_DIRECT)) { 4009c1b3d7c5SThomas E. Spanjaard device_printf(rdp->disks[disk].dev, "write metadata failed\n"); 4010c1b3d7c5SThomas E. Spanjaard error = EIO; 4011c1b3d7c5SThomas E. Spanjaard } 4012c1b3d7c5SThomas E. Spanjaard } 4013c1b3d7c5SThomas E. Spanjaard } 4014d438c7c2SThomas E. Spanjaard kfree(meta, M_AR); 4015c1b3d7c5SThomas E. Spanjaard return error; 4016c1b3d7c5SThomas E. Spanjaard } 4017c1b3d7c5SThomas E. Spanjaard 4018c1b3d7c5SThomas E. Spanjaard static struct ata_request * 4019c1b3d7c5SThomas E. Spanjaard ata_raid_init_request(struct ar_softc *rdp, struct bio *bio) 4020c1b3d7c5SThomas E. Spanjaard { 4021c1b3d7c5SThomas E. Spanjaard struct ata_request *request; 4022c1b3d7c5SThomas E. Spanjaard 4023c1b3d7c5SThomas E. Spanjaard if (!(request = ata_alloc_request())) { 4024e3869ec7SSascha Wildner kprintf("FAILURE - out of memory in ata_raid_init_request\n"); 4025c1b3d7c5SThomas E. Spanjaard return NULL; 4026c1b3d7c5SThomas E. Spanjaard } 40273c0963e0SMatthew Dillon request->timeout = ATA_DEFAULT_TIMEOUT; 4028c1b3d7c5SThomas E. Spanjaard request->retries = 2; 4029c1b3d7c5SThomas E. Spanjaard request->callback = ata_raid_done; 4030c1b3d7c5SThomas E. Spanjaard request->driver = rdp; 4031c1b3d7c5SThomas E. Spanjaard request->bio = bio; 4032c1b3d7c5SThomas E. Spanjaard switch (request->bio->bio_buf->b_cmd) { 4033c1b3d7c5SThomas E. Spanjaard case BUF_CMD_READ: 4034c1b3d7c5SThomas E. Spanjaard request->flags = ATA_R_READ; 4035c1b3d7c5SThomas E. Spanjaard break; 4036c1b3d7c5SThomas E. Spanjaard case BUF_CMD_WRITE: 4037c1b3d7c5SThomas E. Spanjaard request->flags = ATA_R_WRITE; 4038c1b3d7c5SThomas E. Spanjaard break; 4039b106cb48SMatthew Dillon case BUF_CMD_FLUSH: 4040b106cb48SMatthew Dillon request->flags = ATA_R_CONTROL; 4041b106cb48SMatthew Dillon break; 4042d438c7c2SThomas E. Spanjaard default: 4043d438c7c2SThomas E. Spanjaard kprintf("ar%d: FAILURE - unknown BUF operation\n", rdp->lun); 4044d438c7c2SThomas E. Spanjaard ata_free_request(request); 4045d438c7c2SThomas E. Spanjaard return(NULL); 4046c1b3d7c5SThomas E. Spanjaard } 4047c1b3d7c5SThomas E. Spanjaard return request; 4048c1b3d7c5SThomas E. Spanjaard } 4049c1b3d7c5SThomas E. Spanjaard 4050c1b3d7c5SThomas E. Spanjaard static int 4051c1b3d7c5SThomas E. Spanjaard ata_raid_send_request(struct ata_request *request) 4052c1b3d7c5SThomas E. Spanjaard { 4053c1b3d7c5SThomas E. Spanjaard struct ata_device *atadev = device_get_softc(request->dev); 4054c1b3d7c5SThomas E. Spanjaard 4055c1b3d7c5SThomas E. Spanjaard request->transfersize = min(request->bytecount, atadev->max_iosize); 4056c1b3d7c5SThomas E. Spanjaard if (request->flags & ATA_R_READ) { 4057c1b3d7c5SThomas E. Spanjaard if (atadev->mode >= ATA_DMA) { 4058c1b3d7c5SThomas E. Spanjaard request->flags |= ATA_R_DMA; 4059c1b3d7c5SThomas E. Spanjaard request->u.ata.command = ATA_READ_DMA; 4060c1b3d7c5SThomas E. Spanjaard } 4061c1b3d7c5SThomas E. Spanjaard else if (atadev->max_iosize > DEV_BSIZE) 4062c1b3d7c5SThomas E. Spanjaard request->u.ata.command = ATA_READ_MUL; 4063c1b3d7c5SThomas E. Spanjaard else 4064c1b3d7c5SThomas E. Spanjaard request->u.ata.command = ATA_READ; 4065c1b3d7c5SThomas E. Spanjaard } 4066c1b3d7c5SThomas E. Spanjaard else if (request->flags & ATA_R_WRITE) { 4067c1b3d7c5SThomas E. Spanjaard if (atadev->mode >= ATA_DMA) { 4068c1b3d7c5SThomas E. Spanjaard request->flags |= ATA_R_DMA; 4069c1b3d7c5SThomas E. Spanjaard request->u.ata.command = ATA_WRITE_DMA; 4070c1b3d7c5SThomas E. Spanjaard } 4071c1b3d7c5SThomas E. Spanjaard else if (atadev->max_iosize > DEV_BSIZE) 4072c1b3d7c5SThomas E. Spanjaard request->u.ata.command = ATA_WRITE_MUL; 4073c1b3d7c5SThomas E. Spanjaard else 4074c1b3d7c5SThomas E. Spanjaard request->u.ata.command = ATA_WRITE; 4075c1b3d7c5SThomas E. Spanjaard } 4076c1b3d7c5SThomas E. Spanjaard else { 4077c1b3d7c5SThomas E. Spanjaard device_printf(request->dev, "FAILURE - unknown IO operation\n"); 4078c1b3d7c5SThomas E. Spanjaard ata_free_request(request); 4079c1b3d7c5SThomas E. Spanjaard return EIO; 4080c1b3d7c5SThomas E. Spanjaard } 4081c1b3d7c5SThomas E. Spanjaard request->flags |= (ATA_R_ORDERED | ATA_R_THREAD); 4082c1b3d7c5SThomas E. Spanjaard ata_queue_request(request); 4083c1b3d7c5SThomas E. Spanjaard return 0; 4084c1b3d7c5SThomas E. Spanjaard } 4085c1b3d7c5SThomas E. Spanjaard 4086c1b3d7c5SThomas E. Spanjaard static int 4087c1b3d7c5SThomas E. Spanjaard ata_raid_rw(device_t dev, u_int64_t lba, void *data, u_int bcount, int flags) 4088c1b3d7c5SThomas E. Spanjaard { 4089c1b3d7c5SThomas E. Spanjaard struct ata_device *atadev = device_get_softc(dev); 4090c1b3d7c5SThomas E. Spanjaard struct ata_request *request; 4091c1b3d7c5SThomas E. Spanjaard int error; 4092c1b3d7c5SThomas E. Spanjaard 4093c1b3d7c5SThomas E. Spanjaard if (bcount % DEV_BSIZE) { 4094c1b3d7c5SThomas E. Spanjaard device_printf(dev, "FAILURE - transfers must be modulo sectorsize\n"); 4095c1b3d7c5SThomas E. Spanjaard return ENOMEM; 4096c1b3d7c5SThomas E. Spanjaard } 4097c1b3d7c5SThomas E. Spanjaard 4098c1b3d7c5SThomas E. Spanjaard if (!(request = ata_alloc_request())) { 4099c1b3d7c5SThomas E. Spanjaard device_printf(dev, "FAILURE - out of memory in ata_raid_rw\n"); 4100c1b3d7c5SThomas E. Spanjaard return ENOMEM; 4101c1b3d7c5SThomas E. Spanjaard } 4102c1b3d7c5SThomas E. Spanjaard 4103c1b3d7c5SThomas E. Spanjaard /* setup request */ 4104c1b3d7c5SThomas E. Spanjaard request->dev = dev; 4105*bb15467aSzrj request->timeout = ATA_DEFAULT_TIMEOUT; 4106c1b3d7c5SThomas E. Spanjaard request->retries = 0; 4107c1b3d7c5SThomas E. Spanjaard request->data = data; 4108c1b3d7c5SThomas E. Spanjaard request->bytecount = bcount; 4109c1b3d7c5SThomas E. Spanjaard request->transfersize = DEV_BSIZE; 4110c1b3d7c5SThomas E. Spanjaard request->u.ata.lba = lba; 4111c1b3d7c5SThomas E. Spanjaard request->u.ata.count = request->bytecount / DEV_BSIZE; 4112c1b3d7c5SThomas E. Spanjaard request->flags = flags; 4113c1b3d7c5SThomas E. Spanjaard 4114c1b3d7c5SThomas E. Spanjaard if (flags & ATA_R_READ) { 4115c1b3d7c5SThomas E. Spanjaard if (atadev->mode >= ATA_DMA) { 4116c1b3d7c5SThomas E. Spanjaard request->u.ata.command = ATA_READ_DMA; 4117c1b3d7c5SThomas E. Spanjaard request->flags |= ATA_R_DMA; 4118c1b3d7c5SThomas E. Spanjaard } 4119c1b3d7c5SThomas E. Spanjaard else 4120c1b3d7c5SThomas E. Spanjaard request->u.ata.command = ATA_READ; 4121c1b3d7c5SThomas E. Spanjaard ata_queue_request(request); 4122c1b3d7c5SThomas E. Spanjaard } 4123c1b3d7c5SThomas E. Spanjaard else if (flags & ATA_R_WRITE) { 4124c1b3d7c5SThomas E. Spanjaard if (atadev->mode >= ATA_DMA) { 4125c1b3d7c5SThomas E. Spanjaard request->u.ata.command = ATA_WRITE_DMA; 4126c1b3d7c5SThomas E. Spanjaard request->flags |= ATA_R_DMA; 4127c1b3d7c5SThomas E. Spanjaard } 4128c1b3d7c5SThomas E. Spanjaard else 4129c1b3d7c5SThomas E. Spanjaard request->u.ata.command = ATA_WRITE; 4130c1b3d7c5SThomas E. Spanjaard ata_queue_request(request); 4131c1b3d7c5SThomas E. Spanjaard } 4132c1b3d7c5SThomas E. Spanjaard else { 4133c1b3d7c5SThomas E. Spanjaard device_printf(dev, "FAILURE - unknown IO operation\n"); 4134c1b3d7c5SThomas E. Spanjaard request->result = EIO; 4135c1b3d7c5SThomas E. Spanjaard } 4136c1b3d7c5SThomas E. Spanjaard error = request->result; 4137c1b3d7c5SThomas E. Spanjaard ata_free_request(request); 4138c1b3d7c5SThomas E. Spanjaard return error; 4139c1b3d7c5SThomas E. Spanjaard } 4140c1b3d7c5SThomas E. Spanjaard 4141c1b3d7c5SThomas E. Spanjaard /* 4142c1b3d7c5SThomas E. Spanjaard * module handeling 4143c1b3d7c5SThomas E. Spanjaard */ 4144c1b3d7c5SThomas E. Spanjaard static int 4145c1b3d7c5SThomas E. Spanjaard ata_raid_subdisk_probe(device_t dev) 4146c1b3d7c5SThomas E. Spanjaard { 4147c1b3d7c5SThomas E. Spanjaard device_quiet(dev); 4148c1b3d7c5SThomas E. Spanjaard return 0; 4149c1b3d7c5SThomas E. Spanjaard } 4150c1b3d7c5SThomas E. Spanjaard 4151c1b3d7c5SThomas E. Spanjaard static int 4152c1b3d7c5SThomas E. Spanjaard ata_raid_subdisk_attach(device_t dev) 4153c1b3d7c5SThomas E. Spanjaard { 4154c1b3d7c5SThomas E. Spanjaard struct ata_raid_subdisk *ars = device_get_softc(dev); 4155c1b3d7c5SThomas E. Spanjaard int volume; 4156c1b3d7c5SThomas E. Spanjaard 4157c1b3d7c5SThomas E. Spanjaard for (volume = 0; volume < MAX_VOLUMES; volume++) { 4158c1b3d7c5SThomas E. Spanjaard ars->raid[volume] = NULL; 4159c1b3d7c5SThomas E. Spanjaard ars->disk_number[volume] = -1; 4160c1b3d7c5SThomas E. Spanjaard } 4161c1b3d7c5SThomas E. Spanjaard ata_raid_read_metadata(dev); 4162c1b3d7c5SThomas E. Spanjaard return 0; 4163c1b3d7c5SThomas E. Spanjaard } 4164c1b3d7c5SThomas E. Spanjaard 4165c1b3d7c5SThomas E. Spanjaard static int 4166c1b3d7c5SThomas E. Spanjaard ata_raid_subdisk_detach(device_t dev) 4167c1b3d7c5SThomas E. Spanjaard { 4168c1b3d7c5SThomas E. Spanjaard struct ata_raid_subdisk *ars = device_get_softc(dev); 4169c1b3d7c5SThomas E. Spanjaard int volume; 4170c1b3d7c5SThomas E. Spanjaard 4171c1b3d7c5SThomas E. Spanjaard for (volume = 0; volume < MAX_VOLUMES; volume++) { 4172c1b3d7c5SThomas E. Spanjaard if (ars->raid[volume]) { 4173c1b3d7c5SThomas E. Spanjaard ars->raid[volume]->disks[ars->disk_number[volume]].flags &= 4174c1b3d7c5SThomas E. Spanjaard ~(AR_DF_PRESENT | AR_DF_ONLINE); 4175c1b3d7c5SThomas E. Spanjaard ars->raid[volume]->disks[ars->disk_number[volume]].dev = NULL; 41769243051bSzrj #if 0 41779243051bSzrj if (mtx_initialized(&ars->raid[volume]->lock)) 41789243051bSzrj #endif 4179c1b3d7c5SThomas E. Spanjaard ata_raid_config_changed(ars->raid[volume], 1); 4180c1b3d7c5SThomas E. Spanjaard ars->raid[volume] = NULL; 4181c1b3d7c5SThomas E. Spanjaard ars->disk_number[volume] = -1; 4182c1b3d7c5SThomas E. Spanjaard } 4183c1b3d7c5SThomas E. Spanjaard } 4184c1b3d7c5SThomas E. Spanjaard return 0; 4185c1b3d7c5SThomas E. Spanjaard } 4186c1b3d7c5SThomas E. Spanjaard 4187c1b3d7c5SThomas E. Spanjaard static device_method_t ata_raid_sub_methods[] = { 4188c1b3d7c5SThomas E. Spanjaard /* device interface */ 4189c1b3d7c5SThomas E. Spanjaard DEVMETHOD(device_probe, ata_raid_subdisk_probe), 4190c1b3d7c5SThomas E. Spanjaard DEVMETHOD(device_attach, ata_raid_subdisk_attach), 4191c1b3d7c5SThomas E. Spanjaard DEVMETHOD(device_detach, ata_raid_subdisk_detach), 4192d3c9c58eSSascha Wildner DEVMETHOD_END 4193c1b3d7c5SThomas E. Spanjaard }; 4194c1b3d7c5SThomas E. Spanjaard 4195c1b3d7c5SThomas E. Spanjaard static driver_t ata_raid_sub_driver = { 4196c1b3d7c5SThomas E. Spanjaard "subdisk", 4197c1b3d7c5SThomas E. Spanjaard ata_raid_sub_methods, 4198c1b3d7c5SThomas E. Spanjaard sizeof(struct ata_raid_subdisk) 4199c1b3d7c5SThomas E. Spanjaard }; 4200c1b3d7c5SThomas E. Spanjaard 4201c1b3d7c5SThomas E. Spanjaard DRIVER_MODULE(subdisk, ad, ata_raid_sub_driver, ata_raid_sub_devclass, NULL, NULL); 4202c1b3d7c5SThomas E. Spanjaard 4203c1b3d7c5SThomas E. Spanjaard static int 4204c1b3d7c5SThomas E. Spanjaard ata_raid_module_event_handler(module_t mod, int what, void *arg) 4205c1b3d7c5SThomas E. Spanjaard { 4206c1b3d7c5SThomas E. Spanjaard int i; 4207c1b3d7c5SThomas E. Spanjaard 4208c1b3d7c5SThomas E. Spanjaard switch (what) { 4209c1b3d7c5SThomas E. Spanjaard case MOD_LOAD: 4210c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 4211e3869ec7SSascha Wildner kprintf("ATA PseudoRAID loaded\n"); 4212c1b3d7c5SThomas E. Spanjaard #if 0 4213c1b3d7c5SThomas E. Spanjaard /* setup table to hold metadata for all ATA PseudoRAID arrays */ 4214d438c7c2SThomas E. Spanjaard ata_raid_arrays = kmalloc(sizeof(struct ar_soft *) * MAX_ARRAYS, 4215d438c7c2SThomas E. Spanjaard M_AR, M_WAITOK | M_ZERO); 4216c1b3d7c5SThomas E. Spanjaard #endif 4217c1b3d7c5SThomas E. Spanjaard /* attach 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->format) 4222c1b3d7c5SThomas E. Spanjaard continue; 4223c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 4224c1b3d7c5SThomas E. Spanjaard ata_raid_print_meta(rdp); 4225c1b3d7c5SThomas E. Spanjaard ata_raid_attach(rdp, 0); 4226c1b3d7c5SThomas E. Spanjaard } 4227c1b3d7c5SThomas E. Spanjaard ata_raid_ioctl_func = ata_raid_ioctl; 4228c1b3d7c5SThomas E. Spanjaard return 0; 4229c1b3d7c5SThomas E. Spanjaard 4230c1b3d7c5SThomas E. Spanjaard case MOD_UNLOAD: 4231c1b3d7c5SThomas E. Spanjaard /* detach found PseudoRAID arrays */ 4232c1b3d7c5SThomas E. Spanjaard for (i = 0; i < MAX_ARRAYS; i++) { 4233c1b3d7c5SThomas E. Spanjaard struct ar_softc *rdp = ata_raid_arrays[i]; 4234c1b3d7c5SThomas E. Spanjaard 4235c1b3d7c5SThomas E. Spanjaard if (!rdp || !rdp->status) 4236c1b3d7c5SThomas E. Spanjaard continue; 42379243051bSzrj #if 0 42389243051bSzrj if (mtx_initialized(&rdp->lock)) 42399243051bSzrj lockuninit(&rdp->lock); 42409243051bSzrj #endif 4241d438c7c2SThomas E. Spanjaard disk_destroy(&rdp->disk); 4242c1b3d7c5SThomas E. Spanjaard } 4243c1b3d7c5SThomas E. Spanjaard if (testing || bootverbose) 4244e3869ec7SSascha Wildner kprintf("ATA PseudoRAID unloaded\n"); 4245c1b3d7c5SThomas E. Spanjaard #if 0 4246d438c7c2SThomas E. Spanjaard kfree(ata_raid_arrays, M_AR); 4247c1b3d7c5SThomas E. Spanjaard #endif 4248c1b3d7c5SThomas E. Spanjaard ata_raid_ioctl_func = NULL; 4249c1b3d7c5SThomas E. Spanjaard return 0; 4250c1b3d7c5SThomas E. Spanjaard 4251c1b3d7c5SThomas E. Spanjaard default: 4252c1b3d7c5SThomas E. Spanjaard return EOPNOTSUPP; 4253c1b3d7c5SThomas E. Spanjaard } 4254c1b3d7c5SThomas E. Spanjaard } 4255c1b3d7c5SThomas E. Spanjaard 4256c1b3d7c5SThomas E. Spanjaard static moduledata_t ata_raid_moduledata = 4257c1b3d7c5SThomas E. Spanjaard { "ataraid", ata_raid_module_event_handler, NULL }; 4258c1b3d7c5SThomas E. Spanjaard DECLARE_MODULE(ata, ata_raid_moduledata, SI_SUB_RAID, SI_ORDER_FIRST); 4259c1b3d7c5SThomas E. Spanjaard MODULE_VERSION(ataraid, 1); 4260c1b3d7c5SThomas E. Spanjaard MODULE_DEPEND(ataraid, ata, 1, 1, 1); 4261c1b3d7c5SThomas E. Spanjaard MODULE_DEPEND(ataraid, ad, 1, 1, 1); 4262c1b3d7c5SThomas E. Spanjaard 4263c1b3d7c5SThomas E. Spanjaard static char * 4264c1b3d7c5SThomas E. Spanjaard ata_raid_format(struct ar_softc *rdp) 4265c1b3d7c5SThomas E. Spanjaard { 4266c1b3d7c5SThomas E. Spanjaard switch (rdp->format) { 4267c1b3d7c5SThomas E. Spanjaard case AR_F_FREEBSD_RAID: return "FreeBSD PseudoRAID"; 4268c1b3d7c5SThomas E. Spanjaard case AR_F_ADAPTEC_RAID: return "Adaptec HostRAID"; 4269c1b3d7c5SThomas E. Spanjaard case AR_F_HPTV2_RAID: return "HighPoint v2 RocketRAID"; 4270c1b3d7c5SThomas E. Spanjaard case AR_F_HPTV3_RAID: return "HighPoint v3 RocketRAID"; 4271c1b3d7c5SThomas E. Spanjaard case AR_F_INTEL_RAID: return "Intel MatrixRAID"; 4272c1b3d7c5SThomas E. Spanjaard case AR_F_ITE_RAID: return "Integrated Technology Express"; 4273c1b3d7c5SThomas E. Spanjaard case AR_F_JMICRON_RAID: return "JMicron Technology Corp"; 4274c1b3d7c5SThomas E. Spanjaard case AR_F_LSIV2_RAID: return "LSILogic v2 MegaRAID"; 4275c1b3d7c5SThomas E. Spanjaard case AR_F_LSIV3_RAID: return "LSILogic v3 MegaRAID"; 4276c1b3d7c5SThomas E. Spanjaard case AR_F_NVIDIA_RAID: return "nVidia MediaShield"; 4277c1b3d7c5SThomas E. Spanjaard case AR_F_PROMISE_RAID: return "Promise Fasttrak"; 4278c1b3d7c5SThomas E. Spanjaard case AR_F_SII_RAID: return "Silicon Image Medley"; 4279c1b3d7c5SThomas E. Spanjaard case AR_F_SIS_RAID: return "Silicon Integrated Systems"; 4280c1b3d7c5SThomas E. Spanjaard case AR_F_VIA_RAID: return "VIA Tech V-RAID"; 4281c1b3d7c5SThomas E. Spanjaard default: return "UNKNOWN"; 4282c1b3d7c5SThomas E. Spanjaard } 4283c1b3d7c5SThomas E. Spanjaard } 4284c1b3d7c5SThomas E. Spanjaard 4285c1b3d7c5SThomas E. Spanjaard static char * 4286c1b3d7c5SThomas E. Spanjaard ata_raid_type(struct ar_softc *rdp) 4287c1b3d7c5SThomas E. Spanjaard { 4288c1b3d7c5SThomas E. Spanjaard switch (rdp->type) { 4289c1b3d7c5SThomas E. Spanjaard case AR_T_JBOD: return "JBOD"; 4290c1b3d7c5SThomas E. Spanjaard case AR_T_SPAN: return "SPAN"; 4291c1b3d7c5SThomas E. Spanjaard case AR_T_RAID0: return "RAID0"; 4292c1b3d7c5SThomas E. Spanjaard case AR_T_RAID1: return "RAID1"; 4293c1b3d7c5SThomas E. Spanjaard case AR_T_RAID3: return "RAID3"; 4294c1b3d7c5SThomas E. Spanjaard case AR_T_RAID4: return "RAID4"; 4295c1b3d7c5SThomas E. Spanjaard case AR_T_RAID5: return "RAID5"; 4296c1b3d7c5SThomas E. Spanjaard case AR_T_RAID01: return "RAID0+1"; 4297c1b3d7c5SThomas E. Spanjaard default: return "UNKNOWN"; 4298c1b3d7c5SThomas E. Spanjaard } 4299c1b3d7c5SThomas E. Spanjaard } 4300c1b3d7c5SThomas E. Spanjaard 4301c1b3d7c5SThomas E. Spanjaard static char * 4302c1b3d7c5SThomas E. Spanjaard ata_raid_flags(struct ar_softc *rdp) 4303c1b3d7c5SThomas E. Spanjaard { 4304c1b3d7c5SThomas E. Spanjaard switch (rdp->status & (AR_S_READY | AR_S_DEGRADED | AR_S_REBUILDING)) { 4305c1b3d7c5SThomas E. Spanjaard case AR_S_READY: return "READY"; 4306c1b3d7c5SThomas E. Spanjaard case AR_S_READY | AR_S_DEGRADED: return "DEGRADED"; 4307c1b3d7c5SThomas E. Spanjaard case AR_S_READY | AR_S_REBUILDING: 4308c1b3d7c5SThomas E. Spanjaard case AR_S_READY | AR_S_DEGRADED | AR_S_REBUILDING: return "REBUILDING"; 4309c1b3d7c5SThomas E. Spanjaard default: return "BROKEN"; 4310c1b3d7c5SThomas E. Spanjaard } 4311c1b3d7c5SThomas E. Spanjaard } 4312c1b3d7c5SThomas E. Spanjaard 4313c1b3d7c5SThomas E. Spanjaard /* debugging gunk */ 4314c1b3d7c5SThomas E. Spanjaard static void 4315c1b3d7c5SThomas E. Spanjaard ata_raid_print_meta(struct ar_softc *raid) 4316c1b3d7c5SThomas E. Spanjaard { 4317c1b3d7c5SThomas E. Spanjaard int i; 4318c1b3d7c5SThomas E. Spanjaard 4319e3869ec7SSascha Wildner kprintf("********** ATA PseudoRAID ar%d Metadata **********\n", raid->lun); 4320e3869ec7SSascha Wildner kprintf("=================================================\n"); 4321e3869ec7SSascha Wildner kprintf("format %s\n", ata_raid_format(raid)); 4322e3869ec7SSascha Wildner kprintf("type %s\n", ata_raid_type(raid)); 4323e3869ec7SSascha Wildner kprintf("flags 0x%02x %b\n", raid->status, raid->status, 4324c1b3d7c5SThomas E. Spanjaard "\20\3REBUILDING\2DEGRADED\1READY\n"); 4325e3869ec7SSascha Wildner kprintf("magic_0 0x%016jx\n", raid->magic_0); 4326e3869ec7SSascha Wildner kprintf("magic_1 0x%016jx\n",raid->magic_1); 4327e3869ec7SSascha Wildner kprintf("generation %u\n", raid->generation); 4328e3869ec7SSascha Wildner kprintf("total_sectors %ju\n", raid->total_sectors); 4329e3869ec7SSascha Wildner kprintf("offset_sectors %ju\n", raid->offset_sectors); 4330e3869ec7SSascha Wildner kprintf("heads %u\n", raid->heads); 4331e3869ec7SSascha Wildner kprintf("sectors %u\n", raid->sectors); 4332e3869ec7SSascha Wildner kprintf("cylinders %u\n", raid->cylinders); 4333e3869ec7SSascha Wildner kprintf("width %u\n", raid->width); 4334e3869ec7SSascha Wildner kprintf("interleave %u\n", raid->interleave); 4335e3869ec7SSascha Wildner kprintf("total_disks %u\n", raid->total_disks); 4336c1b3d7c5SThomas E. Spanjaard for (i = 0; i < raid->total_disks; i++) { 4337e3869ec7SSascha Wildner kprintf(" disk %d: flags = 0x%02x %b\n", i, raid->disks[i].flags, 4338c1b3d7c5SThomas E. Spanjaard raid->disks[i].flags, "\20\4ONLINE\3SPARE\2ASSIGNED\1PRESENT\n"); 4339c1b3d7c5SThomas E. Spanjaard if (raid->disks[i].dev) { 4340e3869ec7SSascha Wildner kprintf(" "); 4341c1b3d7c5SThomas E. Spanjaard device_printf(raid->disks[i].dev, " sectors %jd\n", 4342c1b3d7c5SThomas E. Spanjaard raid->disks[i].sectors); 4343c1b3d7c5SThomas E. Spanjaard } 4344c1b3d7c5SThomas E. Spanjaard } 4345e3869ec7SSascha Wildner kprintf("=================================================\n"); 4346c1b3d7c5SThomas E. Spanjaard } 4347c1b3d7c5SThomas E. Spanjaard 4348c1b3d7c5SThomas E. Spanjaard static char * 4349c1b3d7c5SThomas E. Spanjaard ata_raid_adaptec_type(int type) 4350c1b3d7c5SThomas E. Spanjaard { 4351c1b3d7c5SThomas E. Spanjaard static char buffer[16]; 4352c1b3d7c5SThomas E. Spanjaard 4353c1b3d7c5SThomas E. Spanjaard switch (type) { 4354c1b3d7c5SThomas E. Spanjaard case ADP_T_RAID0: return "RAID0"; 4355c1b3d7c5SThomas E. Spanjaard case ADP_T_RAID1: return "RAID1"; 4356f8c7a42dSMatthew Dillon default: ksprintf(buffer, "UNKNOWN 0x%02x", type); 4357c1b3d7c5SThomas E. Spanjaard return buffer; 4358c1b3d7c5SThomas E. Spanjaard } 4359c1b3d7c5SThomas E. Spanjaard } 4360c1b3d7c5SThomas E. Spanjaard 4361c1b3d7c5SThomas E. Spanjaard static void 4362c1b3d7c5SThomas E. Spanjaard ata_raid_adaptec_print_meta(struct adaptec_raid_conf *meta) 4363c1b3d7c5SThomas E. Spanjaard { 4364c1b3d7c5SThomas E. Spanjaard int i; 4365c1b3d7c5SThomas E. Spanjaard 4366e3869ec7SSascha Wildner kprintf("********* ATA Adaptec HostRAID Metadata *********\n"); 4367e3869ec7SSascha Wildner kprintf("magic_0 <0x%08x>\n", be32toh(meta->magic_0)); 4368e3869ec7SSascha Wildner kprintf("generation 0x%08x\n", be32toh(meta->generation)); 4369e3869ec7SSascha Wildner kprintf("dummy_0 0x%04x\n", be16toh(meta->dummy_0)); 4370e3869ec7SSascha Wildner kprintf("total_configs %u\n", be16toh(meta->total_configs)); 4371e3869ec7SSascha Wildner kprintf("dummy_1 0x%04x\n", be16toh(meta->dummy_1)); 4372e3869ec7SSascha Wildner kprintf("checksum 0x%04x\n", be16toh(meta->checksum)); 4373e3869ec7SSascha Wildner kprintf("dummy_2 0x%08x\n", be32toh(meta->dummy_2)); 4374e3869ec7SSascha Wildner kprintf("dummy_3 0x%08x\n", be32toh(meta->dummy_3)); 4375e3869ec7SSascha Wildner kprintf("flags 0x%08x\n", be32toh(meta->flags)); 4376e3869ec7SSascha Wildner kprintf("timestamp 0x%08x\n", be32toh(meta->timestamp)); 4377e3869ec7SSascha Wildner kprintf("dummy_4 0x%08x 0x%08x 0x%08x 0x%08x\n", 4378c1b3d7c5SThomas E. Spanjaard be32toh(meta->dummy_4[0]), be32toh(meta->dummy_4[1]), 4379c1b3d7c5SThomas E. Spanjaard be32toh(meta->dummy_4[2]), be32toh(meta->dummy_4[3])); 4380e3869ec7SSascha Wildner kprintf("dummy_5 0x%08x 0x%08x 0x%08x 0x%08x\n", 4381c1b3d7c5SThomas E. Spanjaard be32toh(meta->dummy_5[0]), be32toh(meta->dummy_5[1]), 4382c1b3d7c5SThomas E. Spanjaard be32toh(meta->dummy_5[2]), be32toh(meta->dummy_5[3])); 4383c1b3d7c5SThomas E. Spanjaard 4384c1b3d7c5SThomas E. Spanjaard for (i = 0; i < be16toh(meta->total_configs); i++) { 4385e3869ec7SSascha Wildner kprintf(" %d total_disks %u\n", i, 4386c1b3d7c5SThomas E. Spanjaard be16toh(meta->configs[i].disk_number)); 4387e3869ec7SSascha Wildner kprintf(" %d generation %u\n", i, 4388c1b3d7c5SThomas E. Spanjaard be16toh(meta->configs[i].generation)); 4389e3869ec7SSascha Wildner kprintf(" %d magic_0 0x%08x\n", i, 4390c1b3d7c5SThomas E. Spanjaard be32toh(meta->configs[i].magic_0)); 4391e3869ec7SSascha Wildner kprintf(" %d dummy_0 0x%02x\n", i, meta->configs[i].dummy_0); 4392e3869ec7SSascha Wildner kprintf(" %d type %s\n", i, 4393c1b3d7c5SThomas E. Spanjaard ata_raid_adaptec_type(meta->configs[i].type)); 4394e3869ec7SSascha Wildner kprintf(" %d dummy_1 0x%02x\n", i, meta->configs[i].dummy_1); 4395e3869ec7SSascha Wildner kprintf(" %d flags %d\n", i, 4396c1b3d7c5SThomas E. Spanjaard be32toh(meta->configs[i].flags)); 4397e3869ec7SSascha Wildner kprintf(" %d dummy_2 0x%02x\n", i, meta->configs[i].dummy_2); 4398e3869ec7SSascha Wildner kprintf(" %d dummy_3 0x%02x\n", i, meta->configs[i].dummy_3); 4399e3869ec7SSascha Wildner kprintf(" %d dummy_4 0x%02x\n", i, meta->configs[i].dummy_4); 4400e3869ec7SSascha Wildner kprintf(" %d dummy_5 0x%02x\n", i, meta->configs[i].dummy_5); 4401e3869ec7SSascha Wildner kprintf(" %d disk_number %u\n", i, 4402c1b3d7c5SThomas E. Spanjaard be32toh(meta->configs[i].disk_number)); 4403e3869ec7SSascha Wildner kprintf(" %d dummy_6 0x%08x\n", i, 4404c1b3d7c5SThomas E. Spanjaard be32toh(meta->configs[i].dummy_6)); 4405e3869ec7SSascha Wildner kprintf(" %d sectors %u\n", i, 4406c1b3d7c5SThomas E. Spanjaard be32toh(meta->configs[i].sectors)); 4407e3869ec7SSascha Wildner kprintf(" %d stripe_shift %u\n", i, 4408c1b3d7c5SThomas E. Spanjaard be16toh(meta->configs[i].stripe_shift)); 4409e3869ec7SSascha Wildner kprintf(" %d dummy_7 0x%08x\n", i, 4410c1b3d7c5SThomas E. Spanjaard be32toh(meta->configs[i].dummy_7)); 4411e3869ec7SSascha Wildner kprintf(" %d dummy_8 0x%08x 0x%08x 0x%08x 0x%08x\n", i, 4412c1b3d7c5SThomas E. Spanjaard be32toh(meta->configs[i].dummy_8[0]), 4413c1b3d7c5SThomas E. Spanjaard be32toh(meta->configs[i].dummy_8[1]), 4414c1b3d7c5SThomas E. Spanjaard be32toh(meta->configs[i].dummy_8[2]), 4415c1b3d7c5SThomas E. Spanjaard be32toh(meta->configs[i].dummy_8[3])); 4416e3869ec7SSascha Wildner kprintf(" %d name <%s>\n", i, meta->configs[i].name); 4417c1b3d7c5SThomas E. Spanjaard } 4418e3869ec7SSascha Wildner kprintf("magic_1 <0x%08x>\n", be32toh(meta->magic_1)); 4419e3869ec7SSascha Wildner kprintf("magic_2 <0x%08x>\n", be32toh(meta->magic_2)); 4420e3869ec7SSascha Wildner kprintf("magic_3 <0x%08x>\n", be32toh(meta->magic_3)); 4421e3869ec7SSascha Wildner kprintf("magic_4 <0x%08x>\n", be32toh(meta->magic_4)); 4422e3869ec7SSascha Wildner kprintf("=================================================\n"); 4423c1b3d7c5SThomas E. Spanjaard } 4424c1b3d7c5SThomas E. Spanjaard 4425c1b3d7c5SThomas E. Spanjaard static char * 4426c1b3d7c5SThomas E. Spanjaard ata_raid_hptv2_type(int type) 4427c1b3d7c5SThomas E. Spanjaard { 4428c1b3d7c5SThomas E. Spanjaard static char buffer[16]; 4429c1b3d7c5SThomas E. Spanjaard 4430c1b3d7c5SThomas E. Spanjaard switch (type) { 4431c1b3d7c5SThomas E. Spanjaard case HPTV2_T_RAID0: return "RAID0"; 4432c1b3d7c5SThomas E. Spanjaard case HPTV2_T_RAID1: return "RAID1"; 4433c1b3d7c5SThomas E. Spanjaard case HPTV2_T_RAID01_RAID0: return "RAID01_RAID0"; 4434c1b3d7c5SThomas E. Spanjaard case HPTV2_T_SPAN: return "SPAN"; 4435c1b3d7c5SThomas E. Spanjaard case HPTV2_T_RAID_3: return "RAID3"; 4436c1b3d7c5SThomas E. Spanjaard case HPTV2_T_RAID_5: return "RAID5"; 4437c1b3d7c5SThomas E. Spanjaard case HPTV2_T_JBOD: return "JBOD"; 4438c1b3d7c5SThomas E. Spanjaard case HPTV2_T_RAID01_RAID1: return "RAID01_RAID1"; 4439f8c7a42dSMatthew Dillon default: ksprintf(buffer, "UNKNOWN 0x%02x", type); 4440c1b3d7c5SThomas E. Spanjaard return buffer; 4441c1b3d7c5SThomas E. Spanjaard } 4442c1b3d7c5SThomas E. Spanjaard } 4443c1b3d7c5SThomas E. Spanjaard 4444c1b3d7c5SThomas E. Spanjaard static void 4445c1b3d7c5SThomas E. Spanjaard ata_raid_hptv2_print_meta(struct hptv2_raid_conf *meta) 4446c1b3d7c5SThomas E. Spanjaard { 4447c1b3d7c5SThomas E. Spanjaard int i; 4448c1b3d7c5SThomas E. Spanjaard 4449e3869ec7SSascha Wildner kprintf("****** ATA Highpoint V2 RocketRAID Metadata *****\n"); 4450e3869ec7SSascha Wildner kprintf("magic 0x%08x\n", meta->magic); 4451e3869ec7SSascha Wildner kprintf("magic_0 0x%08x\n", meta->magic_0); 4452e3869ec7SSascha Wildner kprintf("magic_1 0x%08x\n", meta->magic_1); 4453e3869ec7SSascha Wildner kprintf("order 0x%08x\n", meta->order); 4454e3869ec7SSascha Wildner kprintf("array_width %u\n", meta->array_width); 4455e3869ec7SSascha Wildner kprintf("stripe_shift %u\n", meta->stripe_shift); 4456e3869ec7SSascha Wildner kprintf("type %s\n", ata_raid_hptv2_type(meta->type)); 4457e3869ec7SSascha Wildner kprintf("disk_number %u\n", meta->disk_number); 4458e3869ec7SSascha Wildner kprintf("total_sectors %u\n", meta->total_sectors); 4459e3869ec7SSascha Wildner kprintf("disk_mode 0x%08x\n", meta->disk_mode); 4460e3869ec7SSascha Wildner kprintf("boot_mode 0x%08x\n", meta->boot_mode); 4461e3869ec7SSascha Wildner kprintf("boot_disk 0x%02x\n", meta->boot_disk); 4462e3869ec7SSascha Wildner kprintf("boot_protect 0x%02x\n", meta->boot_protect); 4463e3869ec7SSascha Wildner kprintf("log_entries 0x%02x\n", meta->error_log_entries); 4464e3869ec7SSascha Wildner kprintf("log_index 0x%02x\n", meta->error_log_index); 4465c1b3d7c5SThomas E. Spanjaard if (meta->error_log_entries) { 4466e3869ec7SSascha Wildner kprintf(" timestamp reason disk status sectors lba\n"); 4467c1b3d7c5SThomas E. Spanjaard for (i = meta->error_log_index; 4468c1b3d7c5SThomas E. Spanjaard i < meta->error_log_index + meta->error_log_entries; i++) 4469e3869ec7SSascha Wildner kprintf(" 0x%08x 0x%02x 0x%02x 0x%02x 0x%02x 0x%08x\n", 4470c1b3d7c5SThomas E. Spanjaard meta->errorlog[i%32].timestamp, 4471c1b3d7c5SThomas E. Spanjaard meta->errorlog[i%32].reason, 4472c1b3d7c5SThomas E. Spanjaard meta->errorlog[i%32].disk, meta->errorlog[i%32].status, 4473c1b3d7c5SThomas E. Spanjaard meta->errorlog[i%32].sectors, meta->errorlog[i%32].lba); 4474c1b3d7c5SThomas E. Spanjaard } 4475e3869ec7SSascha Wildner kprintf("rebuild_lba 0x%08x\n", meta->rebuild_lba); 4476e3869ec7SSascha Wildner kprintf("dummy_1 0x%02x\n", meta->dummy_1); 4477e3869ec7SSascha Wildner kprintf("name_1 <%.15s>\n", meta->name_1); 4478e3869ec7SSascha Wildner kprintf("dummy_2 0x%02x\n", meta->dummy_2); 4479e3869ec7SSascha Wildner kprintf("name_2 <%.15s>\n", meta->name_2); 4480e3869ec7SSascha Wildner kprintf("=================================================\n"); 4481c1b3d7c5SThomas E. Spanjaard } 4482c1b3d7c5SThomas E. Spanjaard 4483c1b3d7c5SThomas E. Spanjaard static char * 4484c1b3d7c5SThomas E. Spanjaard ata_raid_hptv3_type(int type) 4485c1b3d7c5SThomas E. Spanjaard { 4486c1b3d7c5SThomas E. Spanjaard static char buffer[16]; 4487c1b3d7c5SThomas E. Spanjaard 4488c1b3d7c5SThomas E. Spanjaard switch (type) { 4489c1b3d7c5SThomas E. Spanjaard case HPTV3_T_SPARE: return "SPARE"; 4490c1b3d7c5SThomas E. Spanjaard case HPTV3_T_JBOD: return "JBOD"; 4491c1b3d7c5SThomas E. Spanjaard case HPTV3_T_SPAN: return "SPAN"; 4492c1b3d7c5SThomas E. Spanjaard case HPTV3_T_RAID0: return "RAID0"; 4493c1b3d7c5SThomas E. Spanjaard case HPTV3_T_RAID1: return "RAID1"; 4494c1b3d7c5SThomas E. Spanjaard case HPTV3_T_RAID3: return "RAID3"; 4495c1b3d7c5SThomas E. Spanjaard case HPTV3_T_RAID5: return "RAID5"; 4496f8c7a42dSMatthew Dillon default: ksprintf(buffer, "UNKNOWN 0x%02x", type); 4497c1b3d7c5SThomas E. Spanjaard return buffer; 4498c1b3d7c5SThomas E. Spanjaard } 4499c1b3d7c5SThomas E. Spanjaard } 4500c1b3d7c5SThomas E. Spanjaard 4501c1b3d7c5SThomas E. Spanjaard static void 4502c1b3d7c5SThomas E. Spanjaard ata_raid_hptv3_print_meta(struct hptv3_raid_conf *meta) 4503c1b3d7c5SThomas E. Spanjaard { 4504c1b3d7c5SThomas E. Spanjaard int i; 4505c1b3d7c5SThomas E. Spanjaard 4506e3869ec7SSascha Wildner kprintf("****** ATA Highpoint V3 RocketRAID Metadata *****\n"); 4507e3869ec7SSascha Wildner kprintf("magic 0x%08x\n", meta->magic); 4508e3869ec7SSascha Wildner kprintf("magic_0 0x%08x\n", meta->magic_0); 4509e3869ec7SSascha Wildner kprintf("checksum_0 0x%02x\n", meta->checksum_0); 4510e3869ec7SSascha Wildner kprintf("mode 0x%02x\n", meta->mode); 4511e3869ec7SSascha Wildner kprintf("user_mode 0x%02x\n", meta->user_mode); 4512e3869ec7SSascha Wildner kprintf("config_entries 0x%02x\n", meta->config_entries); 4513c1b3d7c5SThomas E. Spanjaard for (i = 0; i < meta->config_entries; i++) { 4514e3869ec7SSascha Wildner kprintf("config %d:\n", i); 4515e3869ec7SSascha Wildner kprintf(" total_sectors %ju\n", 4516c1b3d7c5SThomas E. Spanjaard meta->configs[0].total_sectors + 4517c1b3d7c5SThomas E. Spanjaard ((u_int64_t)meta->configs_high[0].total_sectors << 32)); 4518e3869ec7SSascha Wildner kprintf(" type %s\n", 4519c1b3d7c5SThomas E. Spanjaard ata_raid_hptv3_type(meta->configs[i].type)); 4520e3869ec7SSascha Wildner kprintf(" total_disks %u\n", meta->configs[i].total_disks); 4521e3869ec7SSascha Wildner kprintf(" disk_number %u\n", meta->configs[i].disk_number); 4522e3869ec7SSascha Wildner kprintf(" stripe_shift %u\n", meta->configs[i].stripe_shift); 4523e3869ec7SSascha Wildner kprintf(" status %b\n", meta->configs[i].status, 4524c1b3d7c5SThomas E. Spanjaard "\20\2RAID5\1NEED_REBUILD\n"); 4525e3869ec7SSascha Wildner kprintf(" critical_disks %u\n", meta->configs[i].critical_disks); 4526e3869ec7SSascha Wildner kprintf(" rebuild_lba %ju\n", 4527c1b3d7c5SThomas E. Spanjaard meta->configs_high[0].rebuild_lba + 4528c1b3d7c5SThomas E. Spanjaard ((u_int64_t)meta->configs_high[0].rebuild_lba << 32)); 4529c1b3d7c5SThomas E. Spanjaard } 4530e3869ec7SSascha Wildner kprintf("name <%.16s>\n", meta->name); 4531e3869ec7SSascha Wildner kprintf("timestamp 0x%08x\n", meta->timestamp); 4532e3869ec7SSascha Wildner kprintf("description <%.16s>\n", meta->description); 4533e3869ec7SSascha Wildner kprintf("creator <%.16s>\n", meta->creator); 4534e3869ec7SSascha Wildner kprintf("checksum_1 0x%02x\n", meta->checksum_1); 4535e3869ec7SSascha Wildner kprintf("dummy_0 0x%02x\n", meta->dummy_0); 4536e3869ec7SSascha Wildner kprintf("dummy_1 0x%02x\n", meta->dummy_1); 4537e3869ec7SSascha Wildner kprintf("flags %b\n", meta->flags, 4538c1b3d7c5SThomas E. Spanjaard "\20\4RCACHE\3WCACHE\2NCQ\1TCQ\n"); 4539e3869ec7SSascha Wildner kprintf("=================================================\n"); 4540c1b3d7c5SThomas E. Spanjaard } 4541c1b3d7c5SThomas E. Spanjaard 4542c1b3d7c5SThomas E. Spanjaard static char * 4543c1b3d7c5SThomas E. Spanjaard ata_raid_intel_type(int type) 4544c1b3d7c5SThomas E. Spanjaard { 4545c1b3d7c5SThomas E. Spanjaard static char buffer[16]; 4546c1b3d7c5SThomas E. Spanjaard 4547c1b3d7c5SThomas E. Spanjaard switch (type) { 4548c1b3d7c5SThomas E. Spanjaard case INTEL_T_RAID0: return "RAID0"; 4549c1b3d7c5SThomas E. Spanjaard case INTEL_T_RAID1: return "RAID1"; 4550c1b3d7c5SThomas E. Spanjaard case INTEL_T_RAID5: return "RAID5"; 4551f8c7a42dSMatthew Dillon default: ksprintf(buffer, "UNKNOWN 0x%02x", type); 4552c1b3d7c5SThomas E. Spanjaard return buffer; 4553c1b3d7c5SThomas E. Spanjaard } 4554c1b3d7c5SThomas E. Spanjaard } 4555c1b3d7c5SThomas E. Spanjaard 4556c1b3d7c5SThomas E. Spanjaard static void 4557c1b3d7c5SThomas E. Spanjaard ata_raid_intel_print_meta(struct intel_raid_conf *meta) 4558c1b3d7c5SThomas E. Spanjaard { 4559c1b3d7c5SThomas E. Spanjaard struct intel_raid_mapping *map; 4560c1b3d7c5SThomas E. Spanjaard int i, j; 4561c1b3d7c5SThomas E. Spanjaard 4562e3869ec7SSascha Wildner kprintf("********* ATA Intel MatrixRAID Metadata *********\n"); 4563e3869ec7SSascha Wildner kprintf("intel_id <%.24s>\n", meta->intel_id); 4564e3869ec7SSascha Wildner kprintf("version <%.6s>\n", meta->version); 4565e3869ec7SSascha Wildner kprintf("checksum 0x%08x\n", meta->checksum); 4566e3869ec7SSascha Wildner kprintf("config_size 0x%08x\n", meta->config_size); 4567e3869ec7SSascha Wildner kprintf("config_id 0x%08x\n", meta->config_id); 4568e3869ec7SSascha Wildner kprintf("generation 0x%08x\n", meta->generation); 4569e3869ec7SSascha Wildner kprintf("total_disks %u\n", meta->total_disks); 4570e3869ec7SSascha Wildner kprintf("total_volumes %u\n", meta->total_volumes); 4571e3869ec7SSascha Wildner kprintf("DISK# serial disk_sectors disk_id flags\n"); 4572c1b3d7c5SThomas E. Spanjaard for (i = 0; i < meta->total_disks; i++ ) { 4573e3869ec7SSascha Wildner kprintf(" %d <%.16s> %u 0x%08x 0x%08x\n", i, 4574c1b3d7c5SThomas E. Spanjaard meta->disk[i].serial, meta->disk[i].sectors, 4575c1b3d7c5SThomas E. Spanjaard meta->disk[i].id, meta->disk[i].flags); 4576c1b3d7c5SThomas E. Spanjaard } 4577c1b3d7c5SThomas E. Spanjaard map = (struct intel_raid_mapping *)&meta->disk[meta->total_disks]; 4578c1b3d7c5SThomas E. Spanjaard for (j = 0; j < meta->total_volumes; j++) { 4579e3869ec7SSascha Wildner kprintf("name %.16s\n", map->name); 4580e3869ec7SSascha Wildner kprintf("total_sectors %ju\n", map->total_sectors); 4581e3869ec7SSascha Wildner kprintf("state %u\n", map->state); 4582e3869ec7SSascha Wildner kprintf("reserved %u\n", map->reserved); 4583e3869ec7SSascha Wildner kprintf("offset %u\n", map->offset); 4584e3869ec7SSascha Wildner kprintf("disk_sectors %u\n", map->disk_sectors); 4585e3869ec7SSascha Wildner kprintf("stripe_count %u\n", map->stripe_count); 4586e3869ec7SSascha Wildner kprintf("stripe_sectors %u\n", map->stripe_sectors); 4587e3869ec7SSascha Wildner kprintf("status %u\n", map->status); 4588e3869ec7SSascha Wildner kprintf("type %s\n", ata_raid_intel_type(map->type)); 4589e3869ec7SSascha Wildner kprintf("total_disks %u\n", map->total_disks); 4590e3869ec7SSascha Wildner kprintf("magic[0] 0x%02x\n", map->magic[0]); 4591e3869ec7SSascha Wildner kprintf("magic[1] 0x%02x\n", map->magic[1]); 4592e3869ec7SSascha Wildner kprintf("magic[2] 0x%02x\n", map->magic[2]); 4593c1b3d7c5SThomas E. Spanjaard for (i = 0; i < map->total_disks; i++ ) { 4594e3869ec7SSascha Wildner kprintf(" disk %d at disk_idx 0x%08x\n", i, map->disk_idx[i]); 4595c1b3d7c5SThomas E. Spanjaard } 4596c1b3d7c5SThomas E. Spanjaard map = (struct intel_raid_mapping *)&map->disk_idx[map->total_disks]; 4597c1b3d7c5SThomas E. Spanjaard } 4598e3869ec7SSascha Wildner kprintf("=================================================\n"); 4599c1b3d7c5SThomas E. Spanjaard } 4600c1b3d7c5SThomas E. Spanjaard 4601c1b3d7c5SThomas E. Spanjaard static char * 4602c1b3d7c5SThomas E. Spanjaard ata_raid_ite_type(int type) 4603c1b3d7c5SThomas E. Spanjaard { 4604c1b3d7c5SThomas E. Spanjaard static char buffer[16]; 4605c1b3d7c5SThomas E. Spanjaard 4606c1b3d7c5SThomas E. Spanjaard switch (type) { 4607c1b3d7c5SThomas E. Spanjaard case ITE_T_RAID0: return "RAID0"; 4608c1b3d7c5SThomas E. Spanjaard case ITE_T_RAID1: return "RAID1"; 4609c1b3d7c5SThomas E. Spanjaard case ITE_T_RAID01: return "RAID0+1"; 4610c1b3d7c5SThomas E. Spanjaard case ITE_T_SPAN: return "SPAN"; 4611f8c7a42dSMatthew Dillon default: ksprintf(buffer, "UNKNOWN 0x%02x", type); 4612c1b3d7c5SThomas E. Spanjaard return buffer; 4613c1b3d7c5SThomas E. Spanjaard } 4614c1b3d7c5SThomas E. Spanjaard } 4615c1b3d7c5SThomas E. Spanjaard 4616c1b3d7c5SThomas E. Spanjaard static void 4617c1b3d7c5SThomas E. Spanjaard ata_raid_ite_print_meta(struct ite_raid_conf *meta) 4618c1b3d7c5SThomas E. Spanjaard { 4619e3869ec7SSascha Wildner kprintf("*** ATA Integrated Technology Express Metadata **\n"); 4620e3869ec7SSascha Wildner kprintf("ite_id <%.40s>\n", meta->ite_id); 4621e3869ec7SSascha Wildner kprintf("timestamp_0 %04x/%02x/%02x %02x:%02x:%02x.%02x\n", 4622c1b3d7c5SThomas E. Spanjaard *((u_int16_t *)meta->timestamp_0), meta->timestamp_0[2], 4623c1b3d7c5SThomas E. Spanjaard meta->timestamp_0[3], meta->timestamp_0[5], meta->timestamp_0[4], 4624c1b3d7c5SThomas E. Spanjaard meta->timestamp_0[7], meta->timestamp_0[6]); 4625e3869ec7SSascha Wildner kprintf("total_sectors %jd\n", meta->total_sectors); 4626e3869ec7SSascha Wildner kprintf("type %s\n", ata_raid_ite_type(meta->type)); 4627e3869ec7SSascha Wildner kprintf("stripe_1kblocks %u\n", meta->stripe_1kblocks); 4628e3869ec7SSascha Wildner kprintf("timestamp_1 %04x/%02x/%02x %02x:%02x:%02x.%02x\n", 4629c1b3d7c5SThomas E. Spanjaard *((u_int16_t *)meta->timestamp_1), meta->timestamp_1[2], 4630c1b3d7c5SThomas E. Spanjaard meta->timestamp_1[3], meta->timestamp_1[5], meta->timestamp_1[4], 4631c1b3d7c5SThomas E. Spanjaard meta->timestamp_1[7], meta->timestamp_1[6]); 4632e3869ec7SSascha Wildner kprintf("stripe_sectors %u\n", meta->stripe_sectors); 4633e3869ec7SSascha Wildner kprintf("array_width %u\n", meta->array_width); 4634e3869ec7SSascha Wildner kprintf("disk_number %u\n", meta->disk_number); 4635e3869ec7SSascha Wildner kprintf("disk_sectors %u\n", meta->disk_sectors); 4636e3869ec7SSascha Wildner kprintf("=================================================\n"); 4637c1b3d7c5SThomas E. Spanjaard } 4638c1b3d7c5SThomas E. Spanjaard 4639c1b3d7c5SThomas E. Spanjaard static char * 4640c1b3d7c5SThomas E. Spanjaard ata_raid_jmicron_type(int type) 4641c1b3d7c5SThomas E. Spanjaard { 4642c1b3d7c5SThomas E. Spanjaard static char buffer[16]; 4643c1b3d7c5SThomas E. Spanjaard 4644c1b3d7c5SThomas E. Spanjaard switch (type) { 4645c1b3d7c5SThomas E. Spanjaard case JM_T_RAID0: return "RAID0"; 4646c1b3d7c5SThomas E. Spanjaard case JM_T_RAID1: return "RAID1"; 4647c1b3d7c5SThomas E. Spanjaard case JM_T_RAID01: return "RAID0+1"; 4648c1b3d7c5SThomas E. Spanjaard case JM_T_JBOD: return "JBOD"; 4649c1b3d7c5SThomas E. Spanjaard case JM_T_RAID5: return "RAID5"; 4650f8c7a42dSMatthew Dillon default: ksprintf(buffer, "UNKNOWN 0x%02x", type); 4651c1b3d7c5SThomas E. Spanjaard return buffer; 4652c1b3d7c5SThomas E. Spanjaard } 4653c1b3d7c5SThomas E. Spanjaard } 4654c1b3d7c5SThomas E. Spanjaard 4655c1b3d7c5SThomas E. Spanjaard static void 4656c1b3d7c5SThomas E. Spanjaard ata_raid_jmicron_print_meta(struct jmicron_raid_conf *meta) 4657c1b3d7c5SThomas E. Spanjaard { 4658c1b3d7c5SThomas E. Spanjaard int i; 4659c1b3d7c5SThomas E. Spanjaard 4660e3869ec7SSascha Wildner kprintf("***** ATA JMicron Technology Corp Metadata ******\n"); 4661e3869ec7SSascha Wildner kprintf("signature %.2s\n", meta->signature); 4662e3869ec7SSascha Wildner kprintf("version 0x%04x\n", meta->version); 4663e3869ec7SSascha Wildner kprintf("checksum 0x%04x\n", meta->checksum); 4664e3869ec7SSascha Wildner kprintf("disk_id 0x%08x\n", meta->disk_id); 4665e3869ec7SSascha Wildner kprintf("offset 0x%08x\n", meta->offset); 4666e3869ec7SSascha Wildner kprintf("disk_sectors_low 0x%08x\n", meta->disk_sectors_low); 4667e3869ec7SSascha Wildner kprintf("disk_sectors_high 0x%08x\n", meta->disk_sectors_high); 4668e3869ec7SSascha Wildner kprintf("name %.16s\n", meta->name); 4669e3869ec7SSascha Wildner kprintf("type %s\n", ata_raid_jmicron_type(meta->type)); 4670e3869ec7SSascha Wildner kprintf("stripe_shift %d\n", meta->stripe_shift); 4671e3869ec7SSascha Wildner kprintf("flags 0x%04x\n", meta->flags); 4672e3869ec7SSascha Wildner kprintf("spare:\n"); 4673c1b3d7c5SThomas E. Spanjaard for (i=0; i < 2 && meta->spare[i]; i++) 4674e3869ec7SSascha Wildner kprintf(" %d 0x%08x\n", i, meta->spare[i]); 4675e3869ec7SSascha Wildner kprintf("disks:\n"); 4676c1b3d7c5SThomas E. Spanjaard for (i=0; i < 8 && meta->disks[i]; i++) 4677e3869ec7SSascha Wildner kprintf(" %d 0x%08x\n", i, meta->disks[i]); 4678e3869ec7SSascha Wildner kprintf("=================================================\n"); 4679c1b3d7c5SThomas E. Spanjaard } 4680c1b3d7c5SThomas E. Spanjaard 4681c1b3d7c5SThomas E. Spanjaard static char * 4682c1b3d7c5SThomas E. Spanjaard ata_raid_lsiv2_type(int type) 4683c1b3d7c5SThomas E. Spanjaard { 4684c1b3d7c5SThomas E. Spanjaard static char buffer[16]; 4685c1b3d7c5SThomas E. Spanjaard 4686c1b3d7c5SThomas E. Spanjaard switch (type) { 4687c1b3d7c5SThomas E. Spanjaard case LSIV2_T_RAID0: return "RAID0"; 4688c1b3d7c5SThomas E. Spanjaard case LSIV2_T_RAID1: return "RAID1"; 4689c1b3d7c5SThomas E. Spanjaard case LSIV2_T_SPARE: return "SPARE"; 4690f8c7a42dSMatthew Dillon default: ksprintf(buffer, "UNKNOWN 0x%02x", type); 4691c1b3d7c5SThomas E. Spanjaard return buffer; 4692c1b3d7c5SThomas E. Spanjaard } 4693c1b3d7c5SThomas E. Spanjaard } 4694c1b3d7c5SThomas E. Spanjaard 4695c1b3d7c5SThomas E. Spanjaard static void 4696c1b3d7c5SThomas E. Spanjaard ata_raid_lsiv2_print_meta(struct lsiv2_raid_conf *meta) 4697c1b3d7c5SThomas E. Spanjaard { 4698c1b3d7c5SThomas E. Spanjaard int i; 4699c1b3d7c5SThomas E. Spanjaard 4700e3869ec7SSascha Wildner kprintf("******* ATA LSILogic V2 MegaRAID Metadata *******\n"); 4701e3869ec7SSascha Wildner kprintf("lsi_id <%s>\n", meta->lsi_id); 4702e3869ec7SSascha Wildner kprintf("dummy_0 0x%02x\n", meta->dummy_0); 4703e3869ec7SSascha Wildner kprintf("flags 0x%02x\n", meta->flags); 4704e3869ec7SSascha Wildner kprintf("version 0x%04x\n", meta->version); 4705e3869ec7SSascha Wildner kprintf("config_entries 0x%02x\n", meta->config_entries); 4706e3869ec7SSascha Wildner kprintf("raid_count 0x%02x\n", meta->raid_count); 4707e3869ec7SSascha Wildner kprintf("total_disks 0x%02x\n", meta->total_disks); 4708e3869ec7SSascha Wildner kprintf("dummy_1 0x%02x\n", meta->dummy_1); 4709e3869ec7SSascha Wildner kprintf("dummy_2 0x%04x\n", meta->dummy_2); 4710c1b3d7c5SThomas E. Spanjaard for (i = 0; i < meta->config_entries; i++) { 4711e3869ec7SSascha Wildner kprintf(" type %s\n", 4712c1b3d7c5SThomas E. Spanjaard ata_raid_lsiv2_type(meta->configs[i].raid.type)); 4713e3869ec7SSascha Wildner kprintf(" dummy_0 %02x\n", meta->configs[i].raid.dummy_0); 4714e3869ec7SSascha Wildner kprintf(" stripe_sectors %u\n", 4715c1b3d7c5SThomas E. Spanjaard meta->configs[i].raid.stripe_sectors); 4716e3869ec7SSascha Wildner kprintf(" array_width %u\n", 4717c1b3d7c5SThomas E. Spanjaard meta->configs[i].raid.array_width); 4718e3869ec7SSascha Wildner kprintf(" disk_count %u\n", meta->configs[i].raid.disk_count); 4719e3869ec7SSascha Wildner kprintf(" config_offset %u\n", 4720c1b3d7c5SThomas E. Spanjaard meta->configs[i].raid.config_offset); 4721e3869ec7SSascha Wildner kprintf(" dummy_1 %u\n", meta->configs[i].raid.dummy_1); 4722e3869ec7SSascha Wildner kprintf(" flags %02x\n", meta->configs[i].raid.flags); 4723e3869ec7SSascha Wildner kprintf(" total_sectors %u\n", 4724c1b3d7c5SThomas E. Spanjaard meta->configs[i].raid.total_sectors); 4725c1b3d7c5SThomas E. Spanjaard } 4726e3869ec7SSascha Wildner kprintf("disk_number 0x%02x\n", meta->disk_number); 4727e3869ec7SSascha Wildner kprintf("raid_number 0x%02x\n", meta->raid_number); 4728e3869ec7SSascha Wildner kprintf("timestamp 0x%08x\n", meta->timestamp); 4729e3869ec7SSascha Wildner kprintf("=================================================\n"); 4730c1b3d7c5SThomas E. Spanjaard } 4731c1b3d7c5SThomas E. Spanjaard 4732c1b3d7c5SThomas E. Spanjaard static char * 4733c1b3d7c5SThomas E. Spanjaard ata_raid_lsiv3_type(int type) 4734c1b3d7c5SThomas E. Spanjaard { 4735c1b3d7c5SThomas E. Spanjaard static char buffer[16]; 4736c1b3d7c5SThomas E. Spanjaard 4737c1b3d7c5SThomas E. Spanjaard switch (type) { 4738c1b3d7c5SThomas E. Spanjaard case LSIV3_T_RAID0: return "RAID0"; 4739c1b3d7c5SThomas E. Spanjaard case LSIV3_T_RAID1: return "RAID1"; 4740f8c7a42dSMatthew Dillon default: ksprintf(buffer, "UNKNOWN 0x%02x", type); 4741c1b3d7c5SThomas E. Spanjaard return buffer; 4742c1b3d7c5SThomas E. Spanjaard } 4743c1b3d7c5SThomas E. Spanjaard } 4744c1b3d7c5SThomas E. Spanjaard 4745c1b3d7c5SThomas E. Spanjaard static void 4746c1b3d7c5SThomas E. Spanjaard ata_raid_lsiv3_print_meta(struct lsiv3_raid_conf *meta) 4747c1b3d7c5SThomas E. Spanjaard { 4748c1b3d7c5SThomas E. Spanjaard int i; 4749c1b3d7c5SThomas E. Spanjaard 4750e3869ec7SSascha Wildner kprintf("******* ATA LSILogic V3 MegaRAID Metadata *******\n"); 4751e3869ec7SSascha Wildner kprintf("lsi_id <%.6s>\n", meta->lsi_id); 4752e3869ec7SSascha Wildner kprintf("dummy_0 0x%04x\n", meta->dummy_0); 4753e3869ec7SSascha Wildner kprintf("version 0x%04x\n", meta->version); 4754e3869ec7SSascha Wildner kprintf("dummy_0 0x%04x\n", meta->dummy_1); 4755e3869ec7SSascha Wildner kprintf("RAID configs:\n"); 4756c1b3d7c5SThomas E. Spanjaard for (i = 0; i < 8; i++) { 4757c1b3d7c5SThomas E. Spanjaard if (meta->raid[i].total_disks) { 4758e3869ec7SSascha Wildner kprintf("%02d stripe_pages %u\n", i, 4759c1b3d7c5SThomas E. Spanjaard meta->raid[i].stripe_pages); 4760e3869ec7SSascha Wildner kprintf("%02d type %s\n", i, 4761c1b3d7c5SThomas E. Spanjaard ata_raid_lsiv3_type(meta->raid[i].type)); 4762e3869ec7SSascha Wildner kprintf("%02d total_disks %u\n", i, 4763c1b3d7c5SThomas E. Spanjaard meta->raid[i].total_disks); 4764e3869ec7SSascha Wildner kprintf("%02d array_width %u\n", i, 4765c1b3d7c5SThomas E. Spanjaard meta->raid[i].array_width); 4766e3869ec7SSascha Wildner kprintf("%02d sectors %u\n", i, meta->raid[i].sectors); 4767e3869ec7SSascha Wildner kprintf("%02d offset %u\n", i, meta->raid[i].offset); 4768e3869ec7SSascha Wildner kprintf("%02d device 0x%02x\n", i, 4769c1b3d7c5SThomas E. Spanjaard meta->raid[i].device); 4770c1b3d7c5SThomas E. Spanjaard } 4771c1b3d7c5SThomas E. Spanjaard } 4772e3869ec7SSascha Wildner kprintf("DISK configs:\n"); 4773c1b3d7c5SThomas E. Spanjaard for (i = 0; i < 6; i++) { 4774c1b3d7c5SThomas E. Spanjaard if (meta->disk[i].disk_sectors) { 4775e3869ec7SSascha Wildner kprintf("%02d disk_sectors %u\n", i, 4776c1b3d7c5SThomas E. Spanjaard meta->disk[i].disk_sectors); 4777e3869ec7SSascha Wildner kprintf("%02d flags 0x%02x\n", i, meta->disk[i].flags); 4778c1b3d7c5SThomas E. Spanjaard } 4779c1b3d7c5SThomas E. Spanjaard } 4780e3869ec7SSascha Wildner kprintf("device 0x%02x\n", meta->device); 4781e3869ec7SSascha Wildner kprintf("timestamp 0x%08x\n", meta->timestamp); 4782e3869ec7SSascha Wildner kprintf("checksum_1 0x%02x\n", meta->checksum_1); 4783e3869ec7SSascha Wildner kprintf("=================================================\n"); 4784c1b3d7c5SThomas E. Spanjaard } 4785c1b3d7c5SThomas E. Spanjaard 4786c1b3d7c5SThomas E. Spanjaard static char * 4787c1b3d7c5SThomas E. Spanjaard ata_raid_nvidia_type(int type) 4788c1b3d7c5SThomas E. Spanjaard { 4789c1b3d7c5SThomas E. Spanjaard static char buffer[16]; 4790c1b3d7c5SThomas E. Spanjaard 4791c1b3d7c5SThomas E. Spanjaard switch (type) { 4792c1b3d7c5SThomas E. Spanjaard case NV_T_SPAN: return "SPAN"; 4793c1b3d7c5SThomas E. Spanjaard case NV_T_RAID0: return "RAID0"; 4794c1b3d7c5SThomas E. Spanjaard case NV_T_RAID1: return "RAID1"; 4795c1b3d7c5SThomas E. Spanjaard case NV_T_RAID3: return "RAID3"; 4796c1b3d7c5SThomas E. Spanjaard case NV_T_RAID5: return "RAID5"; 4797c1b3d7c5SThomas E. Spanjaard case NV_T_RAID01: return "RAID0+1"; 4798f8c7a42dSMatthew Dillon default: ksprintf(buffer, "UNKNOWN 0x%02x", type); 4799c1b3d7c5SThomas E. Spanjaard return buffer; 4800c1b3d7c5SThomas E. Spanjaard } 4801c1b3d7c5SThomas E. Spanjaard } 4802c1b3d7c5SThomas E. Spanjaard 4803c1b3d7c5SThomas E. Spanjaard static void 4804c1b3d7c5SThomas E. Spanjaard ata_raid_nvidia_print_meta(struct nvidia_raid_conf *meta) 4805c1b3d7c5SThomas E. Spanjaard { 4806e3869ec7SSascha Wildner kprintf("******** ATA nVidia MediaShield Metadata ********\n"); 4807e3869ec7SSascha Wildner kprintf("nvidia_id <%.8s>\n", meta->nvidia_id); 4808b8ba70b8SSascha Wildner kprintf("config_size %u\n", meta->config_size); 4809e3869ec7SSascha Wildner kprintf("checksum 0x%08x\n", meta->checksum); 4810e3869ec7SSascha Wildner kprintf("version 0x%04x\n", meta->version); 4811b8ba70b8SSascha Wildner kprintf("disk_number %u\n", meta->disk_number); 4812e3869ec7SSascha Wildner kprintf("dummy_0 0x%02x\n", meta->dummy_0); 4813b8ba70b8SSascha Wildner kprintf("total_sectors %u\n", meta->total_sectors); 4814b8ba70b8SSascha Wildner kprintf("sectors_size %u\n", meta->sector_size); 4815e3869ec7SSascha Wildner kprintf("serial %.16s\n", meta->serial); 4816e3869ec7SSascha Wildner kprintf("revision %.4s\n", meta->revision); 4817e3869ec7SSascha Wildner kprintf("dummy_1 0x%08x\n", meta->dummy_1); 4818e3869ec7SSascha Wildner kprintf("magic_0 0x%08x\n", meta->magic_0); 4819e3869ec7SSascha Wildner kprintf("magic_1 0x%016jx\n", meta->magic_1); 4820e3869ec7SSascha Wildner kprintf("magic_2 0x%016jx\n", meta->magic_2); 4821e3869ec7SSascha Wildner kprintf("flags 0x%02x\n", meta->flags); 4822b8ba70b8SSascha Wildner kprintf("array_width %u\n", meta->array_width); 4823b8ba70b8SSascha Wildner kprintf("total_disks %u\n", meta->total_disks); 4824e3869ec7SSascha Wildner kprintf("dummy_2 0x%02x\n", meta->dummy_2); 4825e3869ec7SSascha Wildner kprintf("type %s\n", ata_raid_nvidia_type(meta->type)); 4826e3869ec7SSascha Wildner kprintf("dummy_3 0x%04x\n", meta->dummy_3); 4827b8ba70b8SSascha Wildner kprintf("stripe_sectors %u\n", meta->stripe_sectors); 4828b8ba70b8SSascha Wildner kprintf("stripe_bytes %u\n", meta->stripe_bytes); 4829b8ba70b8SSascha Wildner kprintf("stripe_shift %u\n", meta->stripe_shift); 4830e3869ec7SSascha Wildner kprintf("stripe_mask 0x%08x\n", meta->stripe_mask); 4831b8ba70b8SSascha Wildner kprintf("stripe_sizesectors %u\n", meta->stripe_sizesectors); 4832b8ba70b8SSascha Wildner kprintf("stripe_sizebytes %u\n", meta->stripe_sizebytes); 4833b8ba70b8SSascha Wildner kprintf("rebuild_lba %u\n", meta->rebuild_lba); 4834e3869ec7SSascha Wildner kprintf("dummy_4 0x%08x\n", meta->dummy_4); 4835e3869ec7SSascha Wildner kprintf("dummy_5 0x%08x\n", meta->dummy_5); 4836e3869ec7SSascha Wildner kprintf("status 0x%08x\n", meta->status); 4837e3869ec7SSascha Wildner kprintf("=================================================\n"); 4838c1b3d7c5SThomas E. Spanjaard } 4839c1b3d7c5SThomas E. Spanjaard 4840c1b3d7c5SThomas E. Spanjaard static char * 4841c1b3d7c5SThomas E. Spanjaard ata_raid_promise_type(int type) 4842c1b3d7c5SThomas E. Spanjaard { 4843c1b3d7c5SThomas E. Spanjaard static char buffer[16]; 4844c1b3d7c5SThomas E. Spanjaard 4845c1b3d7c5SThomas E. Spanjaard switch (type) { 4846c1b3d7c5SThomas E. Spanjaard case PR_T_RAID0: return "RAID0"; 4847c1b3d7c5SThomas E. Spanjaard case PR_T_RAID1: return "RAID1"; 4848c1b3d7c5SThomas E. Spanjaard case PR_T_RAID3: return "RAID3"; 4849c1b3d7c5SThomas E. Spanjaard case PR_T_RAID5: return "RAID5"; 4850c1b3d7c5SThomas E. Spanjaard case PR_T_SPAN: return "SPAN"; 4851f8c7a42dSMatthew Dillon default: ksprintf(buffer, "UNKNOWN 0x%02x", type); 4852c1b3d7c5SThomas E. Spanjaard return buffer; 4853c1b3d7c5SThomas E. Spanjaard } 4854c1b3d7c5SThomas E. Spanjaard } 4855c1b3d7c5SThomas E. Spanjaard 4856c1b3d7c5SThomas E. Spanjaard static void 4857c1b3d7c5SThomas E. Spanjaard ata_raid_promise_print_meta(struct promise_raid_conf *meta) 4858c1b3d7c5SThomas E. Spanjaard { 4859c1b3d7c5SThomas E. Spanjaard int i; 4860c1b3d7c5SThomas E. Spanjaard 4861e3869ec7SSascha Wildner kprintf("********* ATA Promise FastTrak Metadata *********\n"); 4862e3869ec7SSascha Wildner kprintf("promise_id <%s>\n", meta->promise_id); 4863e3869ec7SSascha Wildner kprintf("dummy_0 0x%08x\n", meta->dummy_0); 4864e3869ec7SSascha Wildner kprintf("magic_0 0x%016jx\n", meta->magic_0); 4865e3869ec7SSascha Wildner kprintf("magic_1 0x%04x\n", meta->magic_1); 4866e3869ec7SSascha Wildner kprintf("magic_2 0x%08x\n", meta->magic_2); 4867e3869ec7SSascha Wildner kprintf("integrity 0x%08x %b\n", meta->raid.integrity, 4868c1b3d7c5SThomas E. Spanjaard meta->raid.integrity, "\20\10VALID\n" ); 4869e3869ec7SSascha Wildner kprintf("flags 0x%02x %b\n", 4870c1b3d7c5SThomas E. Spanjaard meta->raid.flags, meta->raid.flags, 4871c1b3d7c5SThomas E. Spanjaard "\20\10READY\7DOWN\6REDIR\5DUPLICATE\4SPARE" 4872c1b3d7c5SThomas E. Spanjaard "\3ASSIGNED\2ONLINE\1VALID\n"); 4873e3869ec7SSascha Wildner kprintf("disk_number %d\n", meta->raid.disk_number); 4874e3869ec7SSascha Wildner kprintf("channel 0x%02x\n", meta->raid.channel); 4875e3869ec7SSascha Wildner kprintf("device 0x%02x\n", meta->raid.device); 4876e3869ec7SSascha Wildner kprintf("magic_0 0x%016jx\n", meta->raid.magic_0); 4877e3869ec7SSascha Wildner kprintf("disk_offset %u\n", meta->raid.disk_offset); 4878e3869ec7SSascha Wildner kprintf("disk_sectors %u\n", meta->raid.disk_sectors); 4879e3869ec7SSascha Wildner kprintf("rebuild_lba 0x%08x\n", meta->raid.rebuild_lba); 4880e3869ec7SSascha Wildner kprintf("generation 0x%04x\n", meta->raid.generation); 4881e3869ec7SSascha Wildner kprintf("status 0x%02x %b\n", 4882c1b3d7c5SThomas E. Spanjaard meta->raid.status, meta->raid.status, 4883c1b3d7c5SThomas E. Spanjaard "\20\6MARKED\5DEGRADED\4READY\3INITED\2ONLINE\1VALID\n"); 4884e3869ec7SSascha Wildner kprintf("type %s\n", ata_raid_promise_type(meta->raid.type)); 4885e3869ec7SSascha Wildner kprintf("total_disks %u\n", meta->raid.total_disks); 4886e3869ec7SSascha Wildner kprintf("stripe_shift %u\n", meta->raid.stripe_shift); 4887e3869ec7SSascha Wildner kprintf("array_width %u\n", meta->raid.array_width); 4888e3869ec7SSascha Wildner kprintf("array_number %u\n", meta->raid.array_number); 4889e3869ec7SSascha Wildner kprintf("total_sectors %u\n", meta->raid.total_sectors); 4890e3869ec7SSascha Wildner kprintf("cylinders %u\n", meta->raid.cylinders); 4891e3869ec7SSascha Wildner kprintf("heads %u\n", meta->raid.heads); 4892e3869ec7SSascha Wildner kprintf("sectors %u\n", meta->raid.sectors); 4893e3869ec7SSascha Wildner kprintf("magic_1 0x%016jx\n", meta->raid.magic_1); 4894e3869ec7SSascha Wildner kprintf("DISK# flags dummy_0 channel device magic_0\n"); 4895c1b3d7c5SThomas E. Spanjaard for (i = 0; i < 8; i++) { 4896e3869ec7SSascha Wildner kprintf(" %d %b 0x%02x 0x%02x 0x%02x ", 4897c1b3d7c5SThomas E. Spanjaard i, meta->raid.disk[i].flags, 4898c1b3d7c5SThomas E. Spanjaard "\20\10READY\7DOWN\6REDIR\5DUPLICATE\4SPARE" 4899c1b3d7c5SThomas E. Spanjaard "\3ASSIGNED\2ONLINE\1VALID\n", meta->raid.disk[i].dummy_0, 4900c1b3d7c5SThomas E. Spanjaard meta->raid.disk[i].channel, meta->raid.disk[i].device); 4901e3869ec7SSascha Wildner kprintf("0x%016jx\n", meta->raid.disk[i].magic_0); 4902c1b3d7c5SThomas E. Spanjaard } 4903e3869ec7SSascha Wildner kprintf("checksum 0x%08x\n", meta->checksum); 4904e3869ec7SSascha Wildner kprintf("=================================================\n"); 4905c1b3d7c5SThomas E. Spanjaard } 4906c1b3d7c5SThomas E. Spanjaard 4907c1b3d7c5SThomas E. Spanjaard static char * 4908c1b3d7c5SThomas E. Spanjaard ata_raid_sii_type(int type) 4909c1b3d7c5SThomas E. Spanjaard { 4910c1b3d7c5SThomas E. Spanjaard static char buffer[16]; 4911c1b3d7c5SThomas E. Spanjaard 4912c1b3d7c5SThomas E. Spanjaard switch (type) { 4913c1b3d7c5SThomas E. Spanjaard case SII_T_RAID0: return "RAID0"; 4914c1b3d7c5SThomas E. Spanjaard case SII_T_RAID1: return "RAID1"; 4915c1b3d7c5SThomas E. Spanjaard case SII_T_RAID01: return "RAID0+1"; 4916c1b3d7c5SThomas E. Spanjaard case SII_T_SPARE: return "SPARE"; 4917f8c7a42dSMatthew Dillon default: ksprintf(buffer, "UNKNOWN 0x%02x", type); 4918c1b3d7c5SThomas E. Spanjaard return buffer; 4919c1b3d7c5SThomas E. Spanjaard } 4920c1b3d7c5SThomas E. Spanjaard } 4921c1b3d7c5SThomas E. Spanjaard 4922c1b3d7c5SThomas E. Spanjaard static void 4923c1b3d7c5SThomas E. Spanjaard ata_raid_sii_print_meta(struct sii_raid_conf *meta) 4924c1b3d7c5SThomas E. Spanjaard { 4925e3869ec7SSascha Wildner kprintf("******* ATA Silicon Image Medley Metadata *******\n"); 4926e3869ec7SSascha Wildner kprintf("total_sectors %ju\n", meta->total_sectors); 4927e3869ec7SSascha Wildner kprintf("dummy_0 0x%04x\n", meta->dummy_0); 4928e3869ec7SSascha Wildner kprintf("dummy_1 0x%04x\n", meta->dummy_1); 4929e3869ec7SSascha Wildner kprintf("controller_pci_id 0x%08x\n", meta->controller_pci_id); 4930e3869ec7SSascha Wildner kprintf("version_minor 0x%04x\n", meta->version_minor); 4931e3869ec7SSascha Wildner kprintf("version_major 0x%04x\n", meta->version_major); 4932e3869ec7SSascha Wildner kprintf("timestamp 20%02x/%02x/%02x %02x:%02x:%02x\n", 4933c1b3d7c5SThomas E. Spanjaard meta->timestamp[5], meta->timestamp[4], meta->timestamp[3], 4934c1b3d7c5SThomas E. Spanjaard meta->timestamp[2], meta->timestamp[1], meta->timestamp[0]); 4935e3869ec7SSascha Wildner kprintf("stripe_sectors %u\n", meta->stripe_sectors); 4936e3869ec7SSascha Wildner kprintf("dummy_2 0x%04x\n", meta->dummy_2); 4937e3869ec7SSascha Wildner kprintf("disk_number %u\n", meta->disk_number); 4938e3869ec7SSascha Wildner kprintf("type %s\n", ata_raid_sii_type(meta->type)); 4939e3869ec7SSascha Wildner kprintf("raid0_disks %u\n", meta->raid0_disks); 4940e3869ec7SSascha Wildner kprintf("raid0_ident %u\n", meta->raid0_ident); 4941e3869ec7SSascha Wildner kprintf("raid1_disks %u\n", meta->raid1_disks); 4942e3869ec7SSascha Wildner kprintf("raid1_ident %u\n", meta->raid1_ident); 4943e3869ec7SSascha Wildner kprintf("rebuild_lba %ju\n", meta->rebuild_lba); 4944e3869ec7SSascha Wildner kprintf("generation 0x%08x\n", meta->generation); 4945e3869ec7SSascha Wildner kprintf("status 0x%02x %b\n", 4946c1b3d7c5SThomas E. Spanjaard meta->status, meta->status, 4947c1b3d7c5SThomas E. Spanjaard "\20\1READY\n"); 4948e3869ec7SSascha Wildner kprintf("base_raid1_position %02x\n", meta->base_raid1_position); 4949e3869ec7SSascha Wildner kprintf("base_raid0_position %02x\n", meta->base_raid0_position); 4950e3869ec7SSascha Wildner kprintf("position %02x\n", meta->position); 4951e3869ec7SSascha Wildner kprintf("dummy_3 %04x\n", meta->dummy_3); 4952e3869ec7SSascha Wildner kprintf("name <%.16s>\n", meta->name); 4953e3869ec7SSascha Wildner kprintf("checksum_0 0x%04x\n", meta->checksum_0); 4954e3869ec7SSascha Wildner kprintf("checksum_1 0x%04x\n", meta->checksum_1); 4955e3869ec7SSascha Wildner kprintf("=================================================\n"); 4956c1b3d7c5SThomas E. Spanjaard } 4957c1b3d7c5SThomas E. Spanjaard 4958c1b3d7c5SThomas E. Spanjaard static char * 4959c1b3d7c5SThomas E. Spanjaard ata_raid_sis_type(int type) 4960c1b3d7c5SThomas E. Spanjaard { 4961c1b3d7c5SThomas E. Spanjaard static char buffer[16]; 4962c1b3d7c5SThomas E. Spanjaard 4963c1b3d7c5SThomas E. Spanjaard switch (type) { 4964c1b3d7c5SThomas E. Spanjaard case SIS_T_JBOD: return "JBOD"; 4965c1b3d7c5SThomas E. Spanjaard case SIS_T_RAID0: return "RAID0"; 4966c1b3d7c5SThomas E. Spanjaard case SIS_T_RAID1: return "RAID1"; 4967f8c7a42dSMatthew Dillon default: ksprintf(buffer, "UNKNOWN 0x%02x", type); 4968c1b3d7c5SThomas E. Spanjaard return buffer; 4969c1b3d7c5SThomas E. Spanjaard } 4970c1b3d7c5SThomas E. Spanjaard } 4971c1b3d7c5SThomas E. Spanjaard 4972c1b3d7c5SThomas E. Spanjaard static void 4973c1b3d7c5SThomas E. Spanjaard ata_raid_sis_print_meta(struct sis_raid_conf *meta) 4974c1b3d7c5SThomas E. Spanjaard { 4975e3869ec7SSascha Wildner kprintf("**** ATA Silicon Integrated Systems Metadata ****\n"); 4976e3869ec7SSascha Wildner kprintf("magic 0x%04x\n", meta->magic); 4977e3869ec7SSascha Wildner kprintf("disks 0x%02x\n", meta->disks); 4978e3869ec7SSascha Wildner kprintf("type %s\n", 4979c1b3d7c5SThomas E. Spanjaard ata_raid_sis_type(meta->type_total_disks & SIS_T_MASK)); 4980e3869ec7SSascha Wildner kprintf("total_disks %u\n", meta->type_total_disks & SIS_D_MASK); 4981e3869ec7SSascha Wildner kprintf("dummy_0 0x%08x\n", meta->dummy_0); 4982e3869ec7SSascha Wildner kprintf("controller_pci_id 0x%08x\n", meta->controller_pci_id); 4983e3869ec7SSascha Wildner kprintf("stripe_sectors %u\n", meta->stripe_sectors); 4984e3869ec7SSascha Wildner kprintf("dummy_1 0x%04x\n", meta->dummy_1); 4985e3869ec7SSascha Wildner kprintf("timestamp 0x%08x\n", meta->timestamp); 4986e3869ec7SSascha Wildner kprintf("model %.40s\n", meta->model); 4987e3869ec7SSascha Wildner kprintf("disk_number %u\n", meta->disk_number); 4988e3869ec7SSascha Wildner kprintf("dummy_2 0x%02x 0x%02x 0x%02x\n", 4989c1b3d7c5SThomas E. Spanjaard meta->dummy_2[0], meta->dummy_2[1], meta->dummy_2[2]); 4990e3869ec7SSascha Wildner kprintf("=================================================\n"); 4991c1b3d7c5SThomas E. Spanjaard } 4992c1b3d7c5SThomas E. Spanjaard 4993c1b3d7c5SThomas E. Spanjaard static char * 4994c1b3d7c5SThomas E. Spanjaard ata_raid_via_type(int type) 4995c1b3d7c5SThomas E. Spanjaard { 4996c1b3d7c5SThomas E. Spanjaard static char buffer[16]; 4997c1b3d7c5SThomas E. Spanjaard 4998c1b3d7c5SThomas E. Spanjaard switch (type) { 4999c1b3d7c5SThomas E. Spanjaard case VIA_T_RAID0: return "RAID0"; 5000c1b3d7c5SThomas E. Spanjaard case VIA_T_RAID1: return "RAID1"; 5001c1b3d7c5SThomas E. Spanjaard case VIA_T_RAID5: return "RAID5"; 5002c1b3d7c5SThomas E. Spanjaard case VIA_T_RAID01: return "RAID0+1"; 5003c1b3d7c5SThomas E. Spanjaard case VIA_T_SPAN: return "SPAN"; 5004f8c7a42dSMatthew Dillon default: ksprintf(buffer, "UNKNOWN 0x%02x", type); 5005c1b3d7c5SThomas E. Spanjaard return buffer; 5006c1b3d7c5SThomas E. Spanjaard } 5007c1b3d7c5SThomas E. Spanjaard } 5008c1b3d7c5SThomas E. Spanjaard 5009c1b3d7c5SThomas E. Spanjaard static void 5010c1b3d7c5SThomas E. Spanjaard ata_raid_via_print_meta(struct via_raid_conf *meta) 5011c1b3d7c5SThomas E. Spanjaard { 5012c1b3d7c5SThomas E. Spanjaard int i; 5013c1b3d7c5SThomas E. Spanjaard 5014e3869ec7SSascha Wildner kprintf("*************** ATA VIA Metadata ****************\n"); 5015e3869ec7SSascha Wildner kprintf("magic 0x%02x\n", meta->magic); 5016e3869ec7SSascha Wildner kprintf("dummy_0 0x%02x\n", meta->dummy_0); 5017e3869ec7SSascha Wildner kprintf("type %s\n", 5018c1b3d7c5SThomas E. Spanjaard ata_raid_via_type(meta->type & VIA_T_MASK)); 5019e3869ec7SSascha Wildner kprintf("bootable %d\n", meta->type & VIA_T_BOOTABLE); 5020e3869ec7SSascha Wildner kprintf("unknown %d\n", meta->type & VIA_T_UNKNOWN); 5021e3869ec7SSascha Wildner kprintf("disk_index 0x%02x\n", meta->disk_index); 5022e3869ec7SSascha Wildner kprintf("stripe_layout 0x%02x\n", meta->stripe_layout); 5023e3869ec7SSascha Wildner kprintf(" stripe_disks %d\n", meta->stripe_layout & VIA_L_DISKS); 5024e3869ec7SSascha Wildner kprintf(" stripe_sectors %d\n", 5025c1b3d7c5SThomas E. Spanjaard 0x08 << ((meta->stripe_layout & VIA_L_MASK) >> VIA_L_SHIFT)); 5026e3869ec7SSascha Wildner kprintf("disk_sectors %ju\n", meta->disk_sectors); 5027e3869ec7SSascha Wildner kprintf("disk_id 0x%08x\n", meta->disk_id); 5028e3869ec7SSascha Wildner kprintf("DISK# disk_id\n"); 5029c1b3d7c5SThomas E. Spanjaard for (i = 0; i < 8; i++) { 5030c1b3d7c5SThomas E. Spanjaard if (meta->disks[i]) 5031e3869ec7SSascha Wildner kprintf(" %d 0x%08x\n", i, meta->disks[i]); 5032c1b3d7c5SThomas E. Spanjaard } 5033e3869ec7SSascha Wildner kprintf("checksum 0x%02x\n", meta->checksum); 5034e3869ec7SSascha Wildner kprintf("=================================================\n"); 5035c1b3d7c5SThomas E. Spanjaard } 5036