Home | History | Annotate | Line # | Download | only in ata
ata_raid_adaptec.c revision 1.5.34.1
      1  1.5.34.1     matt /*	$NetBSD: ata_raid_adaptec.c,v 1.5.34.1 2008/01/09 01:52:24 matt Exp $	*/
      2       1.1   briggs 
      3       1.1   briggs /*-
      4       1.1   briggs  * Copyright (c) 2000,2001,2002 Sren Schmidt <sos (at) FreeBSD.org>
      5       1.1   briggs  * All rights reserved.
      6       1.1   briggs  *
      7       1.1   briggs  * Redistribution and use in source and binary forms, with or without
      8       1.1   briggs  * modification, are permitted provided that the following conditions
      9       1.1   briggs  * are met:
     10       1.1   briggs  * 1. Redistributions of source code must retain the above copyright
     11       1.1   briggs  *    notice, this list of conditions and the following disclaimer,
     12       1.1   briggs  *    without modification, immediately at the beginning of the file.
     13       1.1   briggs  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1   briggs  *    notice, this list of conditions and the following disclaimer in the
     15       1.1   briggs  *    documentation and/or other materials provided with the distribution.
     16       1.1   briggs  * 3. The name of the author may not be used to endorse or promote products
     17       1.1   briggs  *    derived from this software without specific prior written permission.
     18       1.1   briggs  *
     19       1.1   briggs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20       1.1   briggs  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21       1.1   briggs  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22       1.1   briggs  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23       1.1   briggs  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24       1.1   briggs  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25       1.1   briggs  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26       1.1   briggs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27       1.1   briggs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28       1.1   briggs  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29       1.1   briggs  */
     30       1.1   briggs 
     31       1.1   briggs /*
     32       1.1   briggs  * Support for parsing Adaptec ATA RAID controller configuration blocks.
     33       1.1   briggs  *
     34       1.1   briggs  * Adapted to NetBSD by Allen K. Briggs
     35       1.1   briggs  */
     36       1.1   briggs 
     37       1.1   briggs #include <sys/cdefs.h>
     38  1.5.34.1     matt __KERNEL_RCSID(0, "$NetBSD: ata_raid_adaptec.c,v 1.5.34.1 2008/01/09 01:52:24 matt Exp $");
     39       1.1   briggs 
     40       1.1   briggs #include <sys/param.h>
     41       1.1   briggs #include <sys/buf.h>
     42       1.1   briggs #include <sys/bufq.h>
     43       1.1   briggs #include <sys/conf.h>
     44       1.1   briggs #include <sys/device.h>
     45       1.1   briggs #include <sys/disk.h>
     46       1.1   briggs #include <sys/disklabel.h>
     47       1.1   briggs #include <sys/fcntl.h>
     48       1.1   briggs #include <sys/malloc.h>
     49       1.1   briggs #include <sys/vnode.h>
     50       1.5     elad #include <sys/kauth.h>
     51       1.1   briggs 
     52       1.1   briggs #include <miscfs/specfs/specdev.h>
     53       1.1   briggs 
     54       1.1   briggs #include <dev/ata/atareg.h>
     55       1.1   briggs #include <dev/ata/atavar.h>
     56       1.1   briggs #include <dev/ata/wdvar.h>
     57       1.1   briggs 
     58       1.1   briggs #include <dev/ata/ata_raidreg.h>
     59       1.1   briggs #include <dev/ata/ata_raidvar.h>
     60       1.1   briggs 
     61       1.1   briggs #ifdef ATA_RAID_DEBUG
     62       1.1   briggs #define	DPRINTF(x)	printf x
     63       1.1   briggs #else
     64       1.1   briggs #define	DPRINTF(x)	/* nothing */
     65       1.1   briggs #endif
     66       1.1   briggs 
     67       1.1   briggs int
     68       1.1   briggs ata_raid_read_config_adaptec(struct wd_softc *sc)
     69       1.1   briggs {
     70       1.1   briggs 	struct adaptec_raid_conf *info;
     71       1.1   briggs 	struct atabus_softc *atabus;
     72       1.1   briggs 	struct vnode *vp;
     73       1.1   briggs 	int bmajor, error;
     74       1.1   briggs 	dev_t dev;
     75       1.1   briggs 	uint32_t gen, drive;
     76       1.1   briggs 	struct ataraid_array_info *aai;
     77       1.1   briggs 	struct ataraid_disk_info *adi;
     78       1.1   briggs 
     79       1.1   briggs 	info = malloc(sizeof(*info), M_DEVBUF, M_WAITOK);
     80       1.1   briggs 
     81       1.1   briggs 	bmajor = devsw_name2blk(sc->sc_dev.dv_xname, NULL, 0);
     82       1.1   briggs 
     83       1.1   briggs 	/* Get a vnode for the raw partition of this disk. */
     84       1.4  thorpej 	dev = MAKEDISKDEV(bmajor, device_unit(&sc->sc_dev), RAW_PART);
     85       1.1   briggs 	error = bdevvp(dev, &vp);
     86       1.1   briggs 	if (error)
     87       1.1   briggs 		goto out;
     88       1.1   briggs 
     89  1.5.34.1     matt 	error = VOP_OPEN(vp, FREAD, NOCRED);
     90       1.1   briggs 	if (error) {
     91       1.1   briggs 		vput(vp);
     92       1.1   briggs 		goto out;
     93       1.1   briggs 	}
     94       1.1   briggs 
     95       1.1   briggs 	error = ata_raid_config_block_rw(vp, ADP_LBA(sc), info,
     96       1.1   briggs 	    sizeof(*info), B_READ);
     97       1.1   briggs 	vput(vp);
     98       1.1   briggs 	if (error) {
     99       1.1   briggs 		printf("%s: error %d reading Adaptec config block\n",
    100       1.1   briggs 		    sc->sc_dev.dv_xname, error);
    101       1.1   briggs 		goto out;
    102       1.1   briggs 	}
    103       1.1   briggs 
    104       1.1   briggs 	info->magic_0 = be32toh(info->magic_0);
    105       1.1   briggs 	info->magic_1 = be32toh(info->magic_1);
    106       1.1   briggs 	info->magic_2 = be32toh(info->magic_2);
    107       1.1   briggs 	info->magic_3 = be32toh(info->magic_3);
    108       1.1   briggs 	info->magic_4 = be32toh(info->magic_4);
    109       1.1   briggs 	/* Check the signature. */
    110       1.1   briggs 	if (info->magic_0 != ADP_MAGIC_0 || info->magic_3 != ADP_MAGIC_3) {
    111       1.1   briggs 		DPRINTF(("%s: Adaptec signature check failed\n",
    112       1.1   briggs 		    sc->sc_dev.dv_xname));
    113       1.1   briggs 		error = ESRCH;
    114       1.1   briggs 		goto out;
    115       1.1   briggs 	}
    116       1.1   briggs 
    117       1.1   briggs 	/*
    118       1.1   briggs 	 * Lookup or allocate a new array info structure for
    119       1.1   briggs 	 * this array.
    120       1.1   briggs 	 */
    121       1.1   briggs 	aai = ata_raid_get_array_info(ATA_RAID_TYPE_ADAPTEC,
    122       1.1   briggs 		be32toh(info->configs[0].disk_number));
    123       1.1   briggs 
    124       1.1   briggs 	gen = be32toh(info->generation);
    125       1.1   briggs 
    126       1.1   briggs 	if (gen == 0 || gen > aai->aai_generation) {
    127       1.1   briggs 		aai->aai_generation = gen;
    128       1.1   briggs 
    129       1.1   briggs 		aai->aai_status = AAI_S_READY;
    130       1.1   briggs 
    131       1.1   briggs 		switch (info->configs[0].type) {
    132       1.1   briggs 		case ADP_T_RAID0:
    133       1.1   briggs 			aai->aai_level = AAI_L_RAID0;
    134       1.1   briggs 			aai->aai_interleave =
    135       1.1   briggs 			    (be16toh(info->configs[0].stripe_sectors) >> 1);
    136       1.1   briggs 			aai->aai_width = be16toh(info->configs[0].total_disks);
    137       1.1   briggs 			break;
    138       1.1   briggs 
    139       1.1   briggs 		case ADP_T_RAID1:
    140       1.1   briggs 			aai->aai_level = AAI_L_RAID1;
    141       1.1   briggs 			aai->aai_interleave = 0;
    142       1.1   briggs 			aai->aai_width = be16toh(info->configs[0].total_disks)
    143       1.1   briggs 			    / 2;
    144       1.1   briggs 			break;
    145       1.1   briggs 
    146       1.1   briggs 		default:
    147       1.1   briggs 			aprint_error("%s: unknown Adaptec RAID type 0x%02x\n",
    148       1.1   briggs 			    sc->sc_dev.dv_xname, info->configs[0].type);
    149       1.1   briggs 			error = EINVAL;
    150       1.1   briggs 			goto out;
    151       1.1   briggs 		}
    152       1.1   briggs 
    153       1.1   briggs 		aai->aai_type = ATA_RAID_TYPE_ADAPTEC;
    154       1.1   briggs 		aai->aai_ndisks = be16toh(info->configs[0].total_disks);
    155       1.1   briggs 		aai->aai_capacity = be32toh(info->configs[0].sectors);
    156       1.1   briggs 		aai->aai_heads = 255;
    157       1.1   briggs 		aai->aai_sectors = 63;
    158       1.1   briggs 		aai->aai_cylinders = aai->aai_capacity / (63 * 255);
    159       1.1   briggs 		aai->aai_offset = 0;
    160       1.1   briggs 		aai->aai_reserved = 17;
    161       1.1   briggs 
    162       1.1   briggs 		/* XXX - bogus.  RAID1 shouldn't really have an interleave */
    163       1.1   briggs 		if (aai->aai_interleave == 0)
    164       1.1   briggs 			aai->aai_interleave = aai->aai_capacity;
    165       1.1   briggs 	}
    166       1.1   briggs 
    167       1.3  thorpej 	atabus = (struct atabus_softc *) device_parent(&sc->sc_dev);
    168       1.1   briggs 	drive = atabus->sc_chan->ch_channel;
    169       1.1   briggs 	if (drive >= aai->aai_ndisks) {
    170       1.1   briggs 		aprint_error("%s: drive number %d doesn't make sense within "
    171       1.1   briggs 		    "%d-disk array\n",
    172       1.1   briggs 		    sc->sc_dev.dv_xname, drive, aai->aai_ndisks);
    173       1.1   briggs 		error = EINVAL;
    174       1.1   briggs 		goto out;
    175       1.1   briggs 	}
    176       1.1   briggs 
    177       1.1   briggs 	adi = &aai->aai_disks[drive];
    178       1.1   briggs 	adi->adi_dev = &sc->sc_dev;
    179       1.1   briggs 	adi->adi_status = ADI_S_ONLINE | ADI_S_ASSIGNED;
    180       1.1   briggs 	adi->adi_sectors = aai->aai_capacity;
    181       1.1   briggs 	adi->adi_compsize = be32toh(info->configs[drive+1].sectors);
    182       1.1   briggs 
    183       1.1   briggs 	error = 0;
    184       1.1   briggs 
    185       1.1   briggs  out:
    186       1.1   briggs 	free(info, M_DEVBUF);
    187       1.1   briggs 	return (error);
    188       1.1   briggs }
    189