Home | History | Annotate | Line # | Download | only in ata
ata_raid_jmicron.c revision 1.6.2.1
      1  1.6.2.1  pgoyette /*	$NetBSD: ata_raid_jmicron.c,v 1.6.2.1 2018/11/26 01:52:30 pgoyette 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.6.2.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: ata_raid_jmicron.c,v 1.6.2.1 2018/11/26 01:52:30 pgoyette 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/vnode.h>
     49      1.1      tron #include <sys/kauth.h>
     50      1.1      tron 
     51      1.1      tron #include <miscfs/specfs/specdev.h>
     52      1.1      tron 
     53      1.1      tron #include <dev/ata/atareg.h>
     54      1.1      tron #include <dev/ata/atavar.h>
     55      1.1      tron #include <dev/ata/wdvar.h>
     56      1.1      tron 
     57      1.1      tron #include <dev/ata/ata_raidreg.h>
     58      1.1      tron #include <dev/ata/ata_raidvar.h>
     59      1.1      tron 
     60      1.1      tron #ifdef ATA_RAID_DEBUG
     61      1.1      tron #define	DPRINTF(x)	printf x
     62      1.1      tron #else
     63      1.1      tron #define	DPRINTF(x)	/* nothing */
     64      1.1      tron #endif
     65      1.1      tron 
     66      1.1      tron #ifdef ATA_RAID_DEBUG
     67      1.1      tron static const char *
     68      1.1      tron ata_raid_jmicron_type(int type)
     69      1.1      tron {
     70      1.1      tron 	static char buffer[16];
     71      1.1      tron 
     72      1.1      tron 	switch (type) {
     73      1.1      tron 	case JM_T_RAID0:
     74      1.1      tron 		return "RAID0";
     75      1.1      tron 	case JM_T_RAID1:
     76      1.1      tron 		return "RAID1";
     77      1.1      tron 	case JM_T_RAID01:
     78      1.1      tron 		return "RAID0+1";
     79      1.1      tron 	case JM_T_RAID5:
     80      1.1      tron 		return "RAID5";
     81      1.1      tron 	case JM_T_JBOD:
     82      1.1      tron 		return "JBOD";
     83      1.1      tron 	default:
     84      1.5  christos 		snprintf(buffer, sizeof(buffer), "UNKNOWN 0x%02x", type);
     85      1.1      tron 		return buffer;
     86      1.1      tron 	}
     87      1.1      tron }
     88      1.1      tron 
     89      1.1      tron static void
     90      1.1      tron ata_raid_jmicron_print_info(struct jmicron_raid_conf *info)
     91      1.1      tron {
     92      1.1      tron 	int i;
     93      1.1      tron 
     94      1.1      tron 	printf("****** ATA JMicron Technology Corp Metadata ******\n");
     95      1.1      tron 	printf("signature           %.2s\n",	info->signature);
     96      1.1      tron 	printf("version             0x%04x\n",	info->version);
     97      1.1      tron 	printf("checksum            0x%04x\n",	info->checksum);
     98      1.1      tron 	printf("disk_id             0x%08x\n",	info->disk_id);
     99      1.1      tron 	printf("offset              0x%08x\n",	info->offset);
    100      1.1      tron 	printf("disk_sectors_low    0x%08x\n",	info->disk_sectors_low);
    101      1.1      tron 	printf("disk_sectors_high   0x%08x\n",	info->disk_sectors_high);
    102      1.1      tron 	printf("name                %.16s\n",	info->name);
    103      1.1      tron 	printf("type                %s\n",
    104      1.1      tron 	    ata_raid_jmicron_type(info->type));
    105      1.1      tron 	printf("stripe_shift        %d\n",	info->stripe_shift);
    106      1.1      tron 	printf("flags               0x%04x\n",	info->flags);
    107      1.1      tron 	printf("spare:\n");
    108      1.1      tron 	for (i = 0; i < 2 && info->spare[i]; i++)
    109      1.1      tron 		printf("    %d                  0x%08x\n", i, info->spare[i]);
    110      1.1      tron 	printf("disks:\n");
    111      1.1      tron 	for (i = 0; i < 8 && info->disks[i]; i++)
    112      1.1      tron 		printf("    %d                  0x%08x\n", i, info->disks[i]);
    113      1.1      tron 	printf("=================================================\n");
    114      1.1      tron }
    115      1.1      tron #endif
    116      1.1      tron 
    117      1.1      tron int
    118      1.1      tron ata_raid_read_config_jmicron(struct wd_softc *sc)
    119      1.1      tron {
    120      1.6   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    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.6.2.1  pgoyette 	info = kmem_zalloc(sizeof(*info), KM_SLEEP);
    133      1.1      tron 
    134      1.6   mlelstv 	bmajor = devsw_name2blk(dksc->sc_xname, NULL, 0);
    135      1.1      tron 
    136      1.1      tron 	/* Get a vnode for the raw partition of this disk. */
    137      1.6   mlelstv 	dev = MAKEDISKDEV(bmajor, device_unit(dksc->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.6   mlelstv 		    dksc->sc_xname, 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.6   mlelstv 		    dksc->sc_xname));
    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.6   mlelstv 		    dksc->sc_xname));
    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.6   mlelstv 		    dksc->sc_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.3      tron 	if (info->name)
    244      1.3      tron 		strlcpy(aai->aai_name, info->name, sizeof(aai->aai_name));
    245      1.1      tron 
    246      1.6   mlelstv 	atabus = device_private(device_parent(dksc->sc_dev));
    247      1.1      tron 	drive = atabus->sc_chan->ch_channel;
    248      1.1      tron 	if (drive >= aai->aai_ndisks) {
    249      1.1      tron 		DPRINTF(("%s: drive number %d doesn't make sense within "
    250      1.6   mlelstv 		    "%d-disk array\n", dksc->sc_xname,
    251      1.1      tron 		    drive, aai->aai_ndisks));
    252      1.1      tron 		error = EINVAL;
    253      1.1      tron 		goto out;
    254      1.1      tron 	}
    255      1.1      tron 
    256      1.1      tron 	if (info->disks[drive] == info->disk_id) {
    257      1.1      tron 		adi = &aai->aai_disks[drive];
    258      1.6   mlelstv 		adi->adi_dev = dksc->sc_dev;
    259      1.1      tron 		adi->adi_status = ADI_S_ONLINE | ADI_S_ASSIGNED;
    260      1.1      tron 		adi->adi_sectors = aai->aai_capacity;
    261      1.2      tron 		adi->adi_compsize = disk_size - aai->aai_reserved;
    262      1.1      tron 	}
    263      1.1      tron 
    264      1.1      tron 	error = 0;
    265      1.1      tron 
    266      1.1      tron  out:
    267  1.6.2.1  pgoyette 	kmem_free(info, sizeof(*info));
    268      1.1      tron 	return error;
    269      1.1      tron }
    270