Home | History | Annotate | Line # | Download | only in ata
ata_raid_promise.c revision 1.5
      1  1.5    perry /*	$NetBSD: ata_raid_promise.c,v 1.5 2005/02/27 00:26:58 perry Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 2000,2001,2002 Sren Schmidt <sos (at) FreeBSD.org>
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
      8  1.1  thorpej  * modification, are permitted provided that the following conditions
      9  1.1  thorpej  * are met:
     10  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     11  1.1  thorpej  *    notice, this list of conditions and the following disclaimer,
     12  1.1  thorpej  *    without modification, immediately at the beginning of the file.
     13  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     16  1.1  thorpej  * 3. The name of the author may not be used to endorse or promote products
     17  1.1  thorpej  *    derived from this software without specific prior written permission.
     18  1.1  thorpej  *
     19  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  1.1  thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  1.1  thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  1.1  thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  1.1  thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  1.1  thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  1.1  thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  1.1  thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  1.1  thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  1.1  thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  1.1  thorpej  */
     30  1.1  thorpej 
     31  1.1  thorpej /*
     32  1.1  thorpej  * Support for parsing Promise ATA RAID controller configuration blocks.
     33  1.1  thorpej  *
     34  1.1  thorpej  * Adapted to NetBSD by Jason R. Thorpe of Wasabi Systems, Inc.
     35  1.1  thorpej  */
     36  1.2    lukem 
     37  1.2    lukem #include <sys/cdefs.h>
     38  1.5    perry __KERNEL_RCSID(0, "$NetBSD: ata_raid_promise.c,v 1.5 2005/02/27 00:26:58 perry Exp $");
     39  1.1  thorpej 
     40  1.1  thorpej #include <sys/param.h>
     41  1.1  thorpej #include <sys/buf.h>
     42  1.4     yamt #include <sys/bufq.h>
     43  1.1  thorpej #include <sys/conf.h>
     44  1.1  thorpej #include <sys/device.h>
     45  1.1  thorpej #include <sys/disk.h>
     46  1.1  thorpej #include <sys/disklabel.h>
     47  1.1  thorpej #include <sys/fcntl.h>
     48  1.1  thorpej #include <sys/malloc.h>
     49  1.1  thorpej #include <sys/vnode.h>
     50  1.1  thorpej 
     51  1.1  thorpej #include <miscfs/specfs/specdev.h>
     52  1.1  thorpej 
     53  1.1  thorpej #include <dev/ata/atareg.h>
     54  1.1  thorpej #include <dev/ata/atavar.h>
     55  1.1  thorpej #include <dev/ata/wdvar.h>
     56  1.1  thorpej 
     57  1.1  thorpej #include <dev/ata/ata_raidreg.h>
     58  1.1  thorpej #include <dev/ata/ata_raidvar.h>
     59  1.1  thorpej 
     60  1.1  thorpej #ifdef ATA_RAID_DEBUG
     61  1.1  thorpej #define	DPRINTF(x)	printf x
     62  1.1  thorpej #else
     63  1.1  thorpej #define	DPRINTF(x)	/* nothing */
     64  1.1  thorpej #endif
     65  1.1  thorpej 
     66  1.1  thorpej int
     67  1.1  thorpej ata_raid_read_config_promise(struct wd_softc *sc)
     68  1.1  thorpej {
     69  1.1  thorpej 	struct promise_raid_conf *info;
     70  1.1  thorpej 	struct vnode *vp;
     71  1.1  thorpej 	int bmajor, error, count;
     72  1.1  thorpej 	u_int disk;
     73  1.1  thorpej 	dev_t dev;
     74  1.1  thorpej 	uint32_t cksum, *ckptr;
     75  1.1  thorpej 	struct ataraid_array_info *aai;
     76  1.1  thorpej 	struct ataraid_disk_info *adi;
     77  1.1  thorpej 
     78  1.1  thorpej 	info = malloc(sizeof(*info), M_DEVBUF, M_WAITOK);
     79  1.5    perry 
     80  1.1  thorpej 	bmajor = devsw_name2blk(sc->sc_dev.dv_xname, NULL, 0);
     81  1.1  thorpej 
     82  1.1  thorpej 	/* Get a vnode for the raw partition of this disk. */
     83  1.1  thorpej 	dev = MAKEDISKDEV(bmajor, sc->sc_dev.dv_unit, RAW_PART);
     84  1.1  thorpej 	error = bdevvp(dev, &vp);
     85  1.1  thorpej 	if (error)
     86  1.1  thorpej 		goto out;
     87  1.1  thorpej 
     88  1.1  thorpej 	error = VOP_OPEN(vp, FREAD, NOCRED, 0);
     89  1.1  thorpej 	if (error) {
     90  1.1  thorpej 		vput(vp);
     91  1.1  thorpej 		goto out;
     92  1.1  thorpej 	}
     93  1.1  thorpej 
     94  1.1  thorpej 	error = ata_raid_config_block_rw(vp, PR_LBA(sc), info,
     95  1.1  thorpej 	    sizeof(*info), B_READ);
     96  1.1  thorpej 	vput(vp);
     97  1.1  thorpej 	if (error) {
     98  1.1  thorpej 		printf("%s: error %d reading Promise config block\n",
     99  1.1  thorpej 		    sc->sc_dev.dv_xname, error);
    100  1.1  thorpej 		goto out;
    101  1.1  thorpej 	}
    102  1.1  thorpej 
    103  1.1  thorpej 	/* Check the signature. */
    104  1.1  thorpej 	if (strncmp(info->promise_id, PR_MAGIC, sizeof(PR_MAGIC)) != 0) {
    105  1.1  thorpej 		DPRINTF(("%s: Promise signature check failed\n",
    106  1.1  thorpej 		    sc->sc_dev.dv_xname));
    107  1.1  thorpej 		error = ESRCH;
    108  1.1  thorpej 		goto out;
    109  1.1  thorpej 	}
    110  1.1  thorpej 
    111  1.1  thorpej 	/* Verify the checksum. */
    112  1.1  thorpej 	for (cksum = 0, ckptr = (uint32_t *) info, count = 0; count < 511;
    113  1.1  thorpej 	     count++)
    114  1.1  thorpej 		cksum += *ckptr++;
    115  1.1  thorpej 	if (cksum != *ckptr) {
    116  1.1  thorpej 		DPRINTF(("%s: Promise checksum failed\n", sc->sc_dev.dv_xname));
    117  1.1  thorpej 		error = ESRCH;
    118  1.1  thorpej 		goto out;
    119  1.1  thorpej 	}
    120  1.1  thorpej 
    121  1.1  thorpej 	if (info->raid.integrity != PR_I_VALID) {
    122  1.1  thorpej 		DPRINTF(("%s: Promise config block marked invalid\n",
    123  1.1  thorpej 		    sc->sc_dev.dv_xname));
    124  1.1  thorpej 		error = ESRCH;
    125  1.1  thorpej 		goto out;
    126  1.1  thorpej 	}
    127  1.1  thorpej 
    128  1.1  thorpej 	/*
    129  1.1  thorpej 	 * Lookup or allocate a new array info structure for
    130  1.1  thorpej 	 * this array.
    131  1.1  thorpej 	 */
    132  1.1  thorpej 	aai = ata_raid_get_array_info(ATA_RAID_TYPE_PROMISE,
    133  1.1  thorpej 	    info->raid.array_number);
    134  1.1  thorpej 
    135  1.1  thorpej 	if (info->raid.generation == 0 ||
    136  1.1  thorpej 	    info->raid.generation > aai->aai_generation) {
    137  1.1  thorpej 		aai->aai_generation = info->raid.generation;
    138  1.1  thorpej 		/* aai_type and aai_arrayno filled in already */
    139  1.1  thorpej 		if ((info->raid.status &
    140  1.1  thorpej 		     (PR_S_VALID | PR_S_ONLINE | PR_S_INITED | PR_S_READY)) ==
    141  1.1  thorpej 		    (PR_S_VALID | PR_S_ONLINE | PR_S_INITED | PR_S_READY)) {
    142  1.1  thorpej 			aai->aai_status |= AAI_S_READY;
    143  1.1  thorpej 			if (info->raid.status & PR_S_DEGRADED)
    144  1.1  thorpej 				aai->aai_status |= AAI_S_DEGRADED;
    145  1.1  thorpej 		} else
    146  1.1  thorpej 			aai->aai_status &= ~AAI_S_READY;
    147  1.1  thorpej 
    148  1.1  thorpej 		switch (info->raid.type) {
    149  1.1  thorpej 		case PR_T_RAID0:
    150  1.1  thorpej 			aai->aai_level = AAI_L_RAID0;
    151  1.1  thorpej 			break;
    152  1.1  thorpej 
    153  1.1  thorpej 		case PR_T_RAID1:
    154  1.1  thorpej 			aai->aai_level = AAI_L_RAID1;
    155  1.1  thorpej 			if (info->raid.array_width > 1)
    156  1.1  thorpej 				aai->aai_level |= AAI_L_RAID0;
    157  1.1  thorpej 			break;
    158  1.1  thorpej 
    159  1.1  thorpej 		case PR_T_SPAN:
    160  1.1  thorpej 			aai->aai_level = AAI_L_SPAN;
    161  1.1  thorpej 			break;
    162  1.1  thorpej 
    163  1.1  thorpej 		default:
    164  1.1  thorpej 			printf("%s: unknown Promise RAID type 0x%02x\n",
    165  1.1  thorpej 			    sc->sc_dev.dv_xname, info->raid.type);
    166  1.1  thorpej 			error = EINVAL;
    167  1.1  thorpej 			goto out;
    168  1.1  thorpej 		}
    169  1.1  thorpej 
    170  1.1  thorpej 		aai->aai_interleave = 1U << info->raid.stripe_shift;
    171  1.1  thorpej 		aai->aai_width = info->raid.array_width;
    172  1.1  thorpej 		aai->aai_ndisks = info->raid.total_disks;
    173  1.1  thorpej 		aai->aai_heads = info->raid.heads + 1;
    174  1.1  thorpej 		aai->aai_sectors = info->raid.sectors;
    175  1.1  thorpej 		aai->aai_cylinders = info->raid.cylinders + 1;
    176  1.1  thorpej 		aai->aai_capacity = info->raid.total_sectors;
    177  1.1  thorpej 		aai->aai_offset = 0;
    178  1.1  thorpej 		aai->aai_reserved = 63;
    179  1.1  thorpej 
    180  1.1  thorpej 		for (disk = 0; disk < aai->aai_ndisks; disk++) {
    181  1.1  thorpej 			adi = &aai->aai_disks[disk];
    182  1.1  thorpej 			adi->adi_status = 0;
    183  1.1  thorpej 			if (info->raid.disk[disk].flags & PR_F_ONLINE)
    184  1.1  thorpej 				adi->adi_status |= ADI_S_ONLINE;
    185  1.1  thorpej 			if (info->raid.disk[disk].flags & PR_F_ASSIGNED)
    186  1.1  thorpej 				adi->adi_status |= ADI_S_ASSIGNED;
    187  1.1  thorpej 			if (info->raid.disk[disk].flags & PR_F_SPARE) {
    188  1.1  thorpej 				adi->adi_status &= ~ADI_S_ONLINE;
    189  1.1  thorpej 				adi->adi_status |= ADI_S_SPARE;
    190  1.1  thorpej 			}
    191  1.1  thorpej 			if (info->raid.disk[disk].flags &
    192  1.1  thorpej 			    (PR_F_REDIR | PR_F_DOWN))
    193  1.1  thorpej 				adi->adi_status &= ~ADI_S_ONLINE;
    194  1.1  thorpej 		}
    195  1.1  thorpej 	}
    196  1.1  thorpej 	adi = &aai->aai_disks[info->raid.disk_number];
    197  1.1  thorpej 	if (adi->adi_status) {
    198  1.1  thorpej 		adi->adi_dev = &sc->sc_dev;
    199  1.1  thorpej 		adi->adi_sectors = info->raid.disk_sectors;
    200  1.1  thorpej 		adi->adi_compsize = sc->sc_capacity - aai->aai_reserved;
    201  1.1  thorpej 	}
    202  1.1  thorpej 
    203  1.1  thorpej 	error = 0;
    204  1.1  thorpej 
    205  1.1  thorpej  out:
    206  1.1  thorpej 	free(info, M_DEVBUF);
    207  1.1  thorpej 	return (error);
    208  1.1  thorpej }
    209