Home | History | Annotate | Line # | Download | only in ata
ata_raid_promise.c revision 1.1.2.3
      1  1.1.2.3    skrll /*	$NetBSD: ata_raid_promise.c,v 1.1.2.3 2004/09/21 13:27:23 skrll 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.1  thorpej 
     37  1.1.2.1    skrll #include <sys/cdefs.h>
     38  1.1.2.3    skrll __KERNEL_RCSID(0, "$NetBSD: ata_raid_promise.c,v 1.1.2.3 2004/09/21 13:27:23 skrll Exp $");
     39  1.1.2.1    skrll 
     40      1.1  thorpej #include <sys/param.h>
     41      1.1  thorpej #include <sys/buf.h>
     42      1.1  thorpej #include <sys/conf.h>
     43      1.1  thorpej #include <sys/device.h>
     44      1.1  thorpej #include <sys/disk.h>
     45      1.1  thorpej #include <sys/disklabel.h>
     46      1.1  thorpej #include <sys/fcntl.h>
     47      1.1  thorpej #include <sys/malloc.h>
     48      1.1  thorpej #include <sys/vnode.h>
     49      1.1  thorpej 
     50      1.1  thorpej #include <miscfs/specfs/specdev.h>
     51      1.1  thorpej 
     52      1.1  thorpej #include <dev/ata/atareg.h>
     53      1.1  thorpej #include <dev/ata/atavar.h>
     54      1.1  thorpej #include <dev/ata/wdvar.h>
     55      1.1  thorpej 
     56      1.1  thorpej #include <dev/ata/ata_raidreg.h>
     57      1.1  thorpej #include <dev/ata/ata_raidvar.h>
     58      1.1  thorpej 
     59      1.1  thorpej #ifdef ATA_RAID_DEBUG
     60      1.1  thorpej #define	DPRINTF(x)	printf x
     61      1.1  thorpej #else
     62      1.1  thorpej #define	DPRINTF(x)	/* nothing */
     63      1.1  thorpej #endif
     64      1.1  thorpej 
     65      1.1  thorpej int
     66      1.1  thorpej ata_raid_read_config_promise(struct wd_softc *sc)
     67      1.1  thorpej {
     68      1.1  thorpej 	struct promise_raid_conf *info;
     69      1.1  thorpej 	struct vnode *vp;
     70      1.1  thorpej 	int bmajor, error, count;
     71      1.1  thorpej 	u_int disk;
     72      1.1  thorpej 	dev_t dev;
     73      1.1  thorpej 	uint32_t cksum, *ckptr;
     74      1.1  thorpej 	struct ataraid_array_info *aai;
     75      1.1  thorpej 	struct ataraid_disk_info *adi;
     76      1.1  thorpej 
     77      1.1  thorpej 	info = malloc(sizeof(*info), M_DEVBUF, M_WAITOK);
     78      1.1  thorpej 
     79      1.1  thorpej 	bmajor = devsw_name2blk(sc->sc_dev.dv_xname, NULL, 0);
     80      1.1  thorpej 
     81      1.1  thorpej 	/* Get a vnode for the raw partition of this disk. */
     82      1.1  thorpej 	dev = MAKEDISKDEV(bmajor, sc->sc_dev.dv_unit, RAW_PART);
     83      1.1  thorpej 	error = bdevvp(dev, &vp);
     84      1.1  thorpej 	if (error)
     85      1.1  thorpej 		goto out;
     86      1.1  thorpej 
     87      1.1  thorpej 	error = VOP_OPEN(vp, FREAD, NOCRED, 0);
     88      1.1  thorpej 	if (error) {
     89      1.1  thorpej 		vput(vp);
     90      1.1  thorpej 		goto out;
     91      1.1  thorpej 	}
     92      1.1  thorpej 
     93      1.1  thorpej 	error = ata_raid_config_block_rw(vp, PR_LBA(sc), info,
     94      1.1  thorpej 	    sizeof(*info), B_READ);
     95      1.1  thorpej 	vput(vp);
     96      1.1  thorpej 	if (error) {
     97      1.1  thorpej 		printf("%s: error %d reading Promise config block\n",
     98      1.1  thorpej 		    sc->sc_dev.dv_xname, error);
     99      1.1  thorpej 		goto out;
    100      1.1  thorpej 	}
    101      1.1  thorpej 
    102      1.1  thorpej 	/* Check the signature. */
    103      1.1  thorpej 	if (strncmp(info->promise_id, PR_MAGIC, sizeof(PR_MAGIC)) != 0) {
    104      1.1  thorpej 		DPRINTF(("%s: Promise signature check failed\n",
    105      1.1  thorpej 		    sc->sc_dev.dv_xname));
    106      1.1  thorpej 		error = ESRCH;
    107      1.1  thorpej 		goto out;
    108      1.1  thorpej 	}
    109      1.1  thorpej 
    110      1.1  thorpej 	/* Verify the checksum. */
    111      1.1  thorpej 	for (cksum = 0, ckptr = (uint32_t *) info, count = 0; count < 511;
    112      1.1  thorpej 	     count++)
    113      1.1  thorpej 		cksum += *ckptr++;
    114      1.1  thorpej 	if (cksum != *ckptr) {
    115      1.1  thorpej 		DPRINTF(("%s: Promise checksum failed\n", sc->sc_dev.dv_xname));
    116      1.1  thorpej 		error = ESRCH;
    117      1.1  thorpej 		goto out;
    118      1.1  thorpej 	}
    119      1.1  thorpej 
    120      1.1  thorpej 	if (info->raid.integrity != PR_I_VALID) {
    121      1.1  thorpej 		DPRINTF(("%s: Promise config block marked invalid\n",
    122      1.1  thorpej 		    sc->sc_dev.dv_xname));
    123      1.1  thorpej 		error = ESRCH;
    124      1.1  thorpej 		goto out;
    125      1.1  thorpej 	}
    126      1.1  thorpej 
    127      1.1  thorpej 	/*
    128      1.1  thorpej 	 * Lookup or allocate a new array info structure for
    129      1.1  thorpej 	 * this array.
    130      1.1  thorpej 	 */
    131      1.1  thorpej 	aai = ata_raid_get_array_info(ATA_RAID_TYPE_PROMISE,
    132      1.1  thorpej 	    info->raid.array_number);
    133      1.1  thorpej 
    134      1.1  thorpej 	if (info->raid.generation == 0 ||
    135      1.1  thorpej 	    info->raid.generation > aai->aai_generation) {
    136      1.1  thorpej 		aai->aai_generation = info->raid.generation;
    137      1.1  thorpej 		/* aai_type and aai_arrayno filled in already */
    138      1.1  thorpej 		if ((info->raid.status &
    139      1.1  thorpej 		     (PR_S_VALID | PR_S_ONLINE | PR_S_INITED | PR_S_READY)) ==
    140      1.1  thorpej 		    (PR_S_VALID | PR_S_ONLINE | PR_S_INITED | PR_S_READY)) {
    141      1.1  thorpej 			aai->aai_status |= AAI_S_READY;
    142      1.1  thorpej 			if (info->raid.status & PR_S_DEGRADED)
    143      1.1  thorpej 				aai->aai_status |= AAI_S_DEGRADED;
    144      1.1  thorpej 		} else
    145      1.1  thorpej 			aai->aai_status &= ~AAI_S_READY;
    146      1.1  thorpej 
    147      1.1  thorpej 		switch (info->raid.type) {
    148      1.1  thorpej 		case PR_T_RAID0:
    149      1.1  thorpej 			aai->aai_level = AAI_L_RAID0;
    150      1.1  thorpej 			break;
    151      1.1  thorpej 
    152      1.1  thorpej 		case PR_T_RAID1:
    153      1.1  thorpej 			aai->aai_level = AAI_L_RAID1;
    154      1.1  thorpej 			if (info->raid.array_width > 1)
    155      1.1  thorpej 				aai->aai_level |= AAI_L_RAID0;
    156      1.1  thorpej 			break;
    157      1.1  thorpej 
    158      1.1  thorpej 		case PR_T_SPAN:
    159      1.1  thorpej 			aai->aai_level = AAI_L_SPAN;
    160      1.1  thorpej 			break;
    161      1.1  thorpej 
    162      1.1  thorpej 		default:
    163      1.1  thorpej 			printf("%s: unknown Promise RAID type 0x%02x\n",
    164      1.1  thorpej 			    sc->sc_dev.dv_xname, info->raid.type);
    165      1.1  thorpej 			error = EINVAL;
    166      1.1  thorpej 			goto out;
    167      1.1  thorpej 		}
    168      1.1  thorpej 
    169      1.1  thorpej 		aai->aai_interleave = 1U << info->raid.stripe_shift;
    170      1.1  thorpej 		aai->aai_width = info->raid.array_width;
    171      1.1  thorpej 		aai->aai_ndisks = info->raid.total_disks;
    172      1.1  thorpej 		aai->aai_heads = info->raid.heads + 1;
    173      1.1  thorpej 		aai->aai_sectors = info->raid.sectors;
    174      1.1  thorpej 		aai->aai_cylinders = info->raid.cylinders + 1;
    175      1.1  thorpej 		aai->aai_capacity = info->raid.total_sectors;
    176      1.1  thorpej 		aai->aai_offset = 0;
    177      1.1  thorpej 		aai->aai_reserved = 63;
    178      1.1  thorpej 
    179      1.1  thorpej 		for (disk = 0; disk < aai->aai_ndisks; disk++) {
    180      1.1  thorpej 			adi = &aai->aai_disks[disk];
    181      1.1  thorpej 			adi->adi_status = 0;
    182      1.1  thorpej 			if (info->raid.disk[disk].flags & PR_F_ONLINE)
    183      1.1  thorpej 				adi->adi_status |= ADI_S_ONLINE;
    184      1.1  thorpej 			if (info->raid.disk[disk].flags & PR_F_ASSIGNED)
    185      1.1  thorpej 				adi->adi_status |= ADI_S_ASSIGNED;
    186      1.1  thorpej 			if (info->raid.disk[disk].flags & PR_F_SPARE) {
    187      1.1  thorpej 				adi->adi_status &= ~ADI_S_ONLINE;
    188      1.1  thorpej 				adi->adi_status |= ADI_S_SPARE;
    189      1.1  thorpej 			}
    190      1.1  thorpej 			if (info->raid.disk[disk].flags &
    191      1.1  thorpej 			    (PR_F_REDIR | PR_F_DOWN))
    192      1.1  thorpej 				adi->adi_status &= ~ADI_S_ONLINE;
    193      1.1  thorpej 		}
    194      1.1  thorpej 	}
    195      1.1  thorpej 	adi = &aai->aai_disks[info->raid.disk_number];
    196      1.1  thorpej 	if (adi->adi_status) {
    197      1.1  thorpej 		adi->adi_dev = &sc->sc_dev;
    198      1.1  thorpej 		adi->adi_sectors = info->raid.disk_sectors;
    199      1.1  thorpej 		adi->adi_compsize = sc->sc_capacity - aai->aai_reserved;
    200      1.1  thorpej 	}
    201      1.1  thorpej 
    202      1.1  thorpej 	error = 0;
    203      1.1  thorpej 
    204      1.1  thorpej  out:
    205      1.1  thorpej 	free(info, M_DEVBUF);
    206      1.1  thorpej 	return (error);
    207      1.1  thorpej }
    208