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