Home | History | Annotate | Line # | Download | only in ata
ata_raid_jmicron.c revision 1.2
      1  1.2  tron /*	$NetBSD: ata_raid_jmicron.c,v 1.2 2008/09/10 16:59:32 tron Exp $	*/
      2  1.1  tron 
      3  1.1  tron /*-
      4  1.1  tron  * Copyright (c) 2000-2008 Sren Schmidt <sos (at) FreeBSD.org>
      5  1.1  tron  * All rights reserved.
      6  1.1  tron  *
      7  1.1  tron  * Redistribution and use in source and binary forms, with or without
      8  1.1  tron  * modification, are permitted provided that the following conditions
      9  1.1  tron  * are met:
     10  1.1  tron  * 1. Redistributions of source code must retain the above copyright
     11  1.1  tron  *    notice, this list of conditions and the following disclaimer,
     12  1.1  tron  *    without modification, immediately at the beginning of the file.
     13  1.1  tron  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  tron  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  tron  *    documentation and/or other materials provided with the distribution.
     16  1.1  tron  * 3. The name of the author may not be used to endorse or promote products
     17  1.1  tron  *    derived from this software without specific prior written permission.
     18  1.1  tron  *
     19  1.1  tron  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  1.1  tron  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  1.1  tron  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  1.1  tron  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  1.1  tron  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  1.1  tron  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  1.1  tron  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  1.1  tron  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  1.1  tron  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  1.1  tron  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  1.1  tron  */
     30  1.1  tron 
     31  1.1  tron /*
     32  1.1  tron  * Support for parsing JMicron Technology RAID controller configuration blocks.
     33  1.1  tron  *
     34  1.1  tron  * Adapted to NetBSD by Juan Romero Pardines (xtraeme (at) gmail.org).
     35  1.1  tron  */
     36  1.1  tron 
     37  1.1  tron #include <sys/cdefs.h>
     38  1.2  tron __KERNEL_RCSID(0, "$NetBSD: ata_raid_jmicron.c,v 1.2 2008/09/10 16:59:32 tron Exp $");
     39  1.1  tron 
     40  1.1  tron #include <sys/param.h>
     41  1.1  tron #include <sys/buf.h>
     42  1.1  tron #include <sys/bufq.h>
     43  1.1  tron #include <sys/conf.h>
     44  1.1  tron #include <sys/device.h>
     45  1.1  tron #include <sys/disk.h>
     46  1.1  tron #include <sys/disklabel.h>
     47  1.1  tron #include <sys/fcntl.h>
     48  1.1  tron #include <sys/malloc.h>
     49  1.1  tron #include <sys/vnode.h>
     50  1.1  tron #include <sys/kauth.h>
     51  1.1  tron 
     52  1.1  tron #include <miscfs/specfs/specdev.h>
     53  1.1  tron 
     54  1.1  tron #include <dev/ata/atareg.h>
     55  1.1  tron #include <dev/ata/atavar.h>
     56  1.1  tron #include <dev/ata/wdvar.h>
     57  1.1  tron 
     58  1.1  tron #include <dev/ata/ata_raidreg.h>
     59  1.1  tron #include <dev/ata/ata_raidvar.h>
     60  1.1  tron 
     61  1.1  tron #ifdef ATA_RAID_DEBUG
     62  1.1  tron #define	DPRINTF(x)	printf x
     63  1.1  tron #else
     64  1.1  tron #define	DPRINTF(x)	/* nothing */
     65  1.1  tron #endif
     66  1.1  tron 
     67  1.1  tron #ifdef ATA_RAID_DEBUG
     68  1.1  tron static const char *
     69  1.1  tron ata_raid_jmicron_type(int type)
     70  1.1  tron {
     71  1.1  tron 	static char buffer[16];
     72  1.1  tron 
     73  1.1  tron 	switch (type) {
     74  1.1  tron 	case JM_T_RAID0:
     75  1.1  tron 		return "RAID0";
     76  1.1  tron 	case JM_T_RAID1:
     77  1.1  tron 		return "RAID1";
     78  1.1  tron 	case JM_T_RAID01:
     79  1.1  tron 		return "RAID0+1";
     80  1.1  tron 	case JM_T_RAID5:
     81  1.1  tron 		return "RAID5";
     82  1.1  tron 	case JM_T_JBOD:
     83  1.1  tron 		return "JBOD";
     84  1.1  tron 	default:
     85  1.1  tron 		sprintf(buffer, "UNKNOWN 0x%02x", type);
     86  1.1  tron 		return buffer;
     87  1.1  tron 	}
     88  1.1  tron }
     89  1.1  tron 
     90  1.1  tron static void
     91  1.1  tron ata_raid_jmicron_print_info(struct jmicron_raid_conf *info)
     92  1.1  tron {
     93  1.1  tron 	int i;
     94  1.1  tron 
     95  1.1  tron 	printf("****** ATA JMicron Technology Corp Metadata ******\n");
     96  1.1  tron 	printf("signature           %.2s\n",	info->signature);
     97  1.1  tron 	printf("version             0x%04x\n",	info->version);
     98  1.1  tron 	printf("checksum            0x%04x\n",	info->checksum);
     99  1.1  tron 	printf("disk_id             0x%08x\n",	info->disk_id);
    100  1.1  tron 	printf("offset              0x%08x\n",	info->offset);
    101  1.1  tron 	printf("disk_sectors_low    0x%08x\n",	info->disk_sectors_low);
    102  1.1  tron 	printf("disk_sectors_high   0x%08x\n",	info->disk_sectors_high);
    103  1.1  tron 	printf("name                %.16s\n",	info->name);
    104  1.1  tron 	printf("type                %s\n",
    105  1.1  tron 	    ata_raid_jmicron_type(info->type));
    106  1.1  tron 	printf("stripe_shift        %d\n",	info->stripe_shift);
    107  1.1  tron 	printf("flags               0x%04x\n",	info->flags);
    108  1.1  tron 	printf("spare:\n");
    109  1.1  tron 	for (i = 0; i < 2 && info->spare[i]; i++)
    110  1.1  tron 		printf("    %d                  0x%08x\n", i, info->spare[i]);
    111  1.1  tron 	printf("disks:\n");
    112  1.1  tron 	for (i = 0; i < 8 && info->disks[i]; i++)
    113  1.1  tron 		printf("    %d                  0x%08x\n", i, info->disks[i]);
    114  1.1  tron 	printf("=================================================\n");
    115  1.1  tron }
    116  1.1  tron #endif
    117  1.1  tron 
    118  1.1  tron int
    119  1.1  tron ata_raid_read_config_jmicron(struct wd_softc *sc)
    120  1.1  tron {
    121  1.1  tron 	struct atabus_softc *atabus;
    122  1.1  tron 	struct jmicron_raid_conf *info;
    123  1.1  tron 	struct vnode *vp;
    124  1.1  tron 	struct ataraid_array_info *aai;
    125  1.1  tron 	struct ataraid_disk_info *adi;
    126  1.1  tron 	uint64_t disk_size;
    127  1.1  tron 	uint32_t drive;
    128  1.1  tron 	uint16_t checksum, *ptr;
    129  1.1  tron 	int bmajor, error, count, disk, total_disks;
    130  1.1  tron 	dev_t dev;
    131  1.1  tron 
    132  1.1  tron 	info = malloc(sizeof(*info), M_DEVBUF, M_WAITOK|M_ZERO);
    133  1.1  tron 
    134  1.1  tron 	bmajor = devsw_name2blk(device_xname(sc->sc_dev), NULL, 0);
    135  1.1  tron 
    136  1.1  tron 	/* Get a vnode for the raw partition of this disk. */
    137  1.1  tron 	dev = MAKEDISKDEV(bmajor, device_unit(sc->sc_dev), RAW_PART);
    138  1.1  tron 	error = bdevvp(dev, &vp);
    139  1.1  tron 	if (error)
    140  1.1  tron 		goto out;
    141  1.1  tron 
    142  1.1  tron 	error = VOP_OPEN(vp, FREAD, NOCRED);
    143  1.1  tron 	if (error) {
    144  1.1  tron 		vput(vp);
    145  1.1  tron 		goto out;
    146  1.1  tron 	}
    147  1.1  tron 
    148  1.1  tron 	error = ata_raid_config_block_rw(vp, JMICRON_LBA(sc), info,
    149  1.1  tron 	    sizeof(*info), B_READ);
    150  1.1  tron 	VOP_CLOSE(vp, FREAD, NOCRED);
    151  1.1  tron 	vput(vp);
    152  1.1  tron 	if (error) {
    153  1.1  tron 		DPRINTF(("%s: error %d reading JMicron config block\n",
    154  1.1  tron 		    device_xname(sc->sc_dev), error));
    155  1.1  tron 		goto out;
    156  1.1  tron 	}
    157  1.1  tron 
    158  1.1  tron 	/* Check for JMicron signature. */
    159  1.1  tron 	if (strncmp(info->signature, JMICRON_MAGIC, 2)) {
    160  1.1  tron 		DPRINTF(("%s: JMicron RAID signature check failed\n",
    161  1.1  tron 		    device_xname(sc->sc_dev)));
    162  1.1  tron 		error = ESRCH;
    163  1.1  tron 		goto out;
    164  1.1  tron 	}
    165  1.1  tron 
    166  1.1  tron 	/* calculate checksum and compare for valid */
    167  1.1  tron 	for (checksum = 0, ptr = (uint16_t *)info, count = 0;
    168  1.1  tron 	     count < 64; count++)
    169  1.1  tron 		checksum += *ptr++;
    170  1.1  tron 	if (checksum) {
    171  1.1  tron 		DPRINTF(("%s: JMicron checksum failed\n",
    172  1.1  tron 		    device_xname(sc->sc_dev)));
    173  1.1  tron 		error = ESRCH;
    174  1.1  tron 		goto out;
    175  1.1  tron 	}
    176  1.1  tron 
    177  1.1  tron #ifdef ATA_RAID_DEBUG
    178  1.1  tron 	ata_raid_jmicron_print_info(info);
    179  1.1  tron #endif
    180  1.2  tron 	/*
    181  1.2  tron 	 * Check that there aren't stale config blocks without
    182  1.2  tron 	 * any array set configured.
    183  1.2  tron 	 */
    184  1.2  tron 	for (total_disks = 0, disk = 0; disk < JM_MAX_DISKS; disk++)
    185  1.2  tron 		if (info->disks[disk] == info->disk_id)
    186  1.2  tron 			total_disks++;
    187  1.2  tron 	if (total_disks <= 1) {
    188  1.2  tron 		error = EINVAL;
    189  1.2  tron 		goto out;
    190  1.2  tron 	}
    191  1.2  tron 
    192  1.2  tron 	/*
    193  1.2  tron 	 * Check volume's state and bail out if it's not acceptable.
    194  1.2  tron 	 */
    195  1.2  tron 	if ((info->flags & (JM_F_READY|JM_F_BOOTABLE|JM_F_ACTIVE)) == 0) {
    196  1.2  tron 		error = EINVAL;
    197  1.2  tron 		goto out;
    198  1.2  tron 	}
    199  1.1  tron 
    200  1.1  tron 	/*
    201  1.1  tron 	 * Lookup or allocate a new array info structure for
    202  1.1  tron 	 * this array.
    203  1.1  tron 	 */
    204  1.1  tron 	aai = ata_raid_get_array_info(ATA_RAID_TYPE_JMICRON, 0);
    205  1.1  tron 	aai->aai_status = AAI_S_READY;
    206  1.1  tron 
    207  1.1  tron 	switch (info->type) {
    208  1.1  tron 	case JM_T_RAID0:
    209  1.1  tron 		aai->aai_level = AAI_L_RAID0;
    210  1.1  tron 		aai->aai_width = total_disks;
    211  1.1  tron 		break;
    212  1.1  tron 	case JM_T_RAID1:
    213  1.1  tron 		aai->aai_level = AAI_L_RAID1;
    214  1.1  tron 		aai->aai_width = 1;
    215  1.1  tron 		break;
    216  1.1  tron 	case JM_T_RAID01:
    217  1.1  tron 		aai->aai_level = AAI_L_RAID0 | AAI_L_RAID1;
    218  1.1  tron 		aai->aai_width = total_disks / 2;
    219  1.1  tron 		break;
    220  1.1  tron 	case JM_T_JBOD:
    221  1.1  tron 		aai->aai_level = AAI_L_SPAN;
    222  1.1  tron 		aai->aai_width = total_disks;
    223  1.1  tron 		break;
    224  1.1  tron 	default:
    225  1.1  tron 		DPRINTF(("%s: unknown JMicron RAID type 0x%02x\n",
    226  1.1  tron 		    sc->sc_dev->dv_xname, info->type));
    227  1.1  tron 		error = EINVAL;
    228  1.1  tron 		goto out;
    229  1.1  tron 	}
    230  1.1  tron 
    231  1.1  tron 	disk_size = (info->disk_sectors_high << 16) + info->disk_sectors_low;
    232  1.1  tron 	aai->aai_type = ATA_RAID_TYPE_JMICRON;
    233  1.1  tron 	aai->aai_generation = 0;
    234  1.1  tron 	aai->aai_capacity = disk_size * aai->aai_width;
    235  1.1  tron 	aai->aai_interleave = 2 << info->stripe_shift;
    236  1.1  tron 	aai->aai_ndisks = total_disks;
    237  1.1  tron 	aai->aai_heads = 255;
    238  1.1  tron 	aai->aai_sectors = 63;
    239  1.1  tron 	aai->aai_cylinders =
    240  1.1  tron 	    aai->aai_capacity / (aai->aai_heads * aai->aai_sectors);
    241  1.1  tron 	aai->aai_offset = info->offset * 16;
    242  1.2  tron 	aai->aai_reserved = 2;
    243  1.1  tron 
    244  1.1  tron 	atabus = device_private(device_parent(sc->sc_dev));
    245  1.1  tron 	drive = atabus->sc_chan->ch_channel;
    246  1.1  tron 	if (drive >= aai->aai_ndisks) {
    247  1.1  tron 		DPRINTF(("%s: drive number %d doesn't make sense within "
    248  1.1  tron 		    "%d-disk array\n", device_xname(sc->sc_dev),
    249  1.1  tron 		    drive, aai->aai_ndisks));
    250  1.1  tron 		error = EINVAL;
    251  1.1  tron 		goto out;
    252  1.1  tron 	}
    253  1.1  tron 
    254  1.1  tron 	if (info->disks[drive] == info->disk_id) {
    255  1.1  tron 		adi = &aai->aai_disks[drive];
    256  1.1  tron 		adi->adi_dev = sc->sc_dev;
    257  1.1  tron 		adi->adi_status = ADI_S_ONLINE | ADI_S_ASSIGNED;
    258  1.1  tron 		adi->adi_sectors = aai->aai_capacity;
    259  1.2  tron 		adi->adi_compsize = disk_size - aai->aai_reserved;
    260  1.1  tron 	}
    261  1.1  tron 
    262  1.1  tron 	error = 0;
    263  1.1  tron 
    264  1.1  tron  out:
    265  1.1  tron 	free(info, M_DEVBUF);
    266  1.1  tron 	return error;
    267  1.1  tron }
    268