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