Home | History | Annotate | Line # | Download | only in ata
      1  1.11    andvar /*	$NetBSD: ata_raid_via.c,v 1.11 2025/03/11 16:51:31 andvar Exp $	*/
      2   1.1   garbled 
      3   1.1   garbled /*-
      4   1.1   garbled  * Copyright (c) 2000,2001,2002 Sren Schmidt <sos (at) FreeBSD.org>
      5   1.1   garbled  * All rights reserved.
      6   1.1   garbled  *
      7   1.1   garbled  * Redistribution and use in source and binary forms, with or without
      8   1.1   garbled  * modification, are permitted provided that the following conditions
      9   1.1   garbled  * are met:
     10   1.1   garbled  * 1. Redistributions of source code must retain the above copyright
     11   1.1   garbled  *    notice, this list of conditions and the following disclaimer,
     12   1.1   garbled  *    without modification, immediately at the beginning of the file.
     13   1.1   garbled  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1   garbled  *    notice, this list of conditions and the following disclaimer in the
     15   1.1   garbled  *    documentation and/or other materials provided with the distribution.
     16   1.1   garbled  * 3. The name of the author may not be used to endorse or promote products
     17   1.1   garbled  *    derived from this software without specific prior written permission.
     18   1.1   garbled  *
     19   1.1   garbled  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20   1.1   garbled  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21   1.1   garbled  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22   1.1   garbled  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23   1.1   garbled  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24   1.1   garbled  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25   1.1   garbled  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26   1.1   garbled  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27   1.1   garbled  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28   1.1   garbled  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29   1.1   garbled  */
     30   1.1   garbled 
     31   1.1   garbled /*
     32   1.1   garbled  * Support for parsing VIA V-RAID ATA RAID controller configuration blocks.
     33   1.1   garbled  *
     34   1.1   garbled  * Adapted to NetBSD by Tim Rightnour (garbled (at) netbsd.org)
     35   1.1   garbled  */
     36   1.1   garbled 
     37   1.1   garbled #include <sys/cdefs.h>
     38  1.11    andvar __KERNEL_RCSID(0, "$NetBSD: ata_raid_via.c,v 1.11 2025/03/11 16:51:31 andvar Exp $");
     39   1.1   garbled 
     40   1.1   garbled #include <sys/param.h>
     41   1.1   garbled #include <sys/buf.h>
     42   1.1   garbled #include <sys/bufq.h>
     43   1.1   garbled #include <sys/conf.h>
     44   1.1   garbled #include <sys/device.h>
     45   1.1   garbled #include <sys/disk.h>
     46   1.1   garbled #include <sys/disklabel.h>
     47   1.1   garbled #include <sys/fcntl.h>
     48   1.1   garbled #include <sys/vnode.h>
     49   1.1   garbled #include <sys/kauth.h>
     50   1.1   garbled 
     51   1.1   garbled #include <miscfs/specfs/specdev.h>
     52   1.1   garbled 
     53   1.1   garbled #include <dev/ata/atareg.h>
     54   1.1   garbled #include <dev/ata/atavar.h>
     55   1.1   garbled #include <dev/ata/wdvar.h>
     56   1.1   garbled 
     57   1.1   garbled #include <dev/ata/ata_raidreg.h>
     58   1.1   garbled #include <dev/ata/ata_raidvar.h>
     59   1.1   garbled 
     60   1.1   garbled #ifdef ATA_RAID_DEBUG
     61   1.1   garbled #define	DPRINTF(x)	printf x
     62   1.1   garbled #else
     63   1.1   garbled #define	DPRINTF(x)	/* nothing */
     64   1.1   garbled #endif
     65   1.1   garbled 
     66   1.1   garbled #ifdef ATA_RAID_DEBUG
     67   1.1   garbled static const char *
     68   1.1   garbled ata_raid_via_type(int type)
     69   1.1   garbled {
     70   1.1   garbled 	static char buffer[16];
     71   1.1   garbled 
     72   1.1   garbled 	switch (type) {
     73   1.7  christos 	case VIA_T_RAID0:   return "RAID0";
     74   1.7  christos 	case VIA_T_RAID1:   return "RAID1";
     75   1.7  christos 	case VIA_T_RAID5:   return "RAID5";
     76   1.7  christos 	case VIA_T_RAID01:  return "RAID0+1";
     77   1.7  christos 	case VIA_T_SPAN:    return "SPAN";
     78   1.7  christos 	default:
     79   1.7  christos 		snprintf(buffer, sizeof(buffer), "UNKNOWN 0x%02x", type);
     80   1.1   garbled 		return buffer;
     81   1.1   garbled 	}
     82   1.1   garbled }
     83   1.1   garbled 
     84   1.1   garbled static void
     85   1.1   garbled ata_raid_via_print_info(struct via_raid_conf *info)
     86   1.1   garbled {
     87   1.1   garbled 	int i;
     88   1.1   garbled 
     89   1.1   garbled 	printf("*************** ATA VIA Metadata ****************\n");
     90   1.1   garbled 	printf("magic               0x%02x\n", info->magic);
     91   1.1   garbled 	printf("dummy_0             0x%02x\n", info->dummy_0);
     92   1.1   garbled 	printf("type                %s\n",
     93   1.1   garbled 	    ata_raid_via_type(info->type & VIA_T_MASK));
     94   1.1   garbled 	printf("bootable            %d\n", info->type & VIA_T_BOOTABLE);
     95   1.1   garbled 	printf("unknown             %d\n", info->type & VIA_T_UNKNOWN);
     96   1.1   garbled 	printf("disk_index          0x%02x\n", info->disk_index);
     97   1.1   garbled 	printf("stripe_layout       0x%02x\n", info->stripe_layout);
     98   1.1   garbled 	printf(" stripe_disks       %d\n", info->stripe_layout & VIA_L_DISKS);
     99   1.1   garbled 	printf(" stripe_sectors     %d\n",
    100   1.1   garbled 	    0x08 << ((info->stripe_layout & VIA_L_MASK) >> VIA_L_SHIFT));
    101   1.1   garbled 	printf("disk_sectors        %ju\n", info->disk_sectors);
    102   1.1   garbled 	printf("disk_id             0x%08x\n", info->disk_id);
    103   1.1   garbled 	printf("DISK#   disk_id\n");
    104   1.1   garbled 	for (i = 0; i < 8; i++) {
    105   1.1   garbled 	if (info->disks[i])
    106   1.1   garbled 		printf("  %d    0x%08x\n", i, info->disks[i]);
    107   1.1   garbled 	}
    108   1.1   garbled 	printf("checksum            0x%02x\n", info->checksum);
    109   1.1   garbled 	printf("=================================================\n");
    110   1.1   garbled }
    111   1.1   garbled #endif
    112   1.1   garbled 
    113   1.1   garbled int
    114   1.1   garbled ata_raid_read_config_via(struct wd_softc *sc)
    115   1.1   garbled {
    116   1.8   mlelstv 	struct dk_softc *dksc = &sc->sc_dksc;
    117   1.1   garbled 	struct via_raid_conf *info;
    118   1.1   garbled 	struct vnode *vp;
    119   1.1   garbled 	int bmajor, error;
    120   1.1   garbled 	dev_t dev;
    121   1.1   garbled 	uint32_t drive;
    122   1.6  christos 	uint8_t checksum, checksum_alt, byte3, *ptr;
    123   1.1   garbled 	int count, disk;
    124   1.1   garbled 	struct ataraid_array_info *aai;
    125   1.1   garbled 	struct ataraid_disk_info *adi;
    126   1.1   garbled 
    127   1.9  jdolecek 	info = kmem_zalloc(sizeof(*info), KM_SLEEP);
    128   1.1   garbled 
    129   1.8   mlelstv 	bmajor = devsw_name2blk(dksc->sc_xname, NULL, 0);
    130   1.1   garbled 
    131   1.1   garbled 	/* Get a vnode for the raw partition of this disk. */
    132   1.8   mlelstv 	dev = MAKEDISKDEV(bmajor, device_unit(dksc->sc_dev), RAW_PART);
    133   1.1   garbled 	error = bdevvp(dev, &vp);
    134   1.1   garbled 	if (error)
    135   1.1   garbled 		goto out;
    136   1.1   garbled 
    137  1.10   hannken 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    138   1.2     pooka 	error = VOP_OPEN(vp, FREAD, NOCRED);
    139   1.1   garbled 	if (error) {
    140   1.1   garbled 		vput(vp);
    141   1.1   garbled 		goto out;
    142   1.1   garbled 	}
    143   1.1   garbled 
    144   1.1   garbled 	error = ata_raid_config_block_rw(vp, VIA_LBA(sc), info,
    145   1.1   garbled 	    sizeof(*info), B_READ);
    146   1.3       mjf 	VOP_CLOSE(vp, FREAD, NOCRED);
    147   1.1   garbled 	vput(vp);
    148   1.1   garbled 	if (error) {
    149   1.8   mlelstv 		aprint_error_dev(dksc->sc_dev,
    150   1.5      cube 		    "error %d reading VIA V-RAID config block\n", error);
    151   1.1   garbled 		goto out;
    152   1.1   garbled 	}
    153   1.1   garbled 
    154   1.1   garbled #ifdef ATA_RAID_DEBUG
    155   1.1   garbled 	ata_raid_via_print_info(info);
    156   1.4   xtraeme 	printf("MAGIC == 0x%02x\n", info->magic);
    157   1.1   garbled #endif
    158   1.1   garbled 
    159   1.1   garbled 	/* Check the signature. */
    160   1.1   garbled 	if (info->magic != VIA_MAGIC) {
    161   1.1   garbled 		DPRINTF(("%s: VIA V-RAID signature check failed\n",
    162   1.8   mlelstv 		    dksc->sc_xname));
    163   1.1   garbled 		error = ESRCH;
    164   1.1   garbled 		goto out;
    165   1.1   garbled 	}
    166   1.1   garbled 
    167   1.1   garbled 	/* calculate checksum and compare for valid */
    168   1.6  christos 	for (byte3 = 0, checksum = 0, ptr = (uint8_t *)info, count = 0;
    169   1.6  christos 	    count < 50; count++)
    170   1.1   garbled 		if (count == 3)
    171   1.6  christos 			byte3 = *ptr++;
    172   1.1   garbled 		else
    173   1.1   garbled 			checksum += *ptr++;
    174   1.6  christos 	checksum_alt = checksum + (byte3 & ~VIA_T_BOOTABLE);
    175   1.6  christos 	checksum += byte3;
    176   1.6  christos 	if (checksum != info->checksum && checksum_alt != info->checksum) {
    177   1.6  christos 		DPRINTF(("%s: VIA V-RAID checksum failed 0x%02x != "
    178   1.8   mlelstv 		    "0x%02x or 0x%02x\n", dksc->sc_xname,
    179   1.6  christos 		    info->checksum, checksum, checksum_alt));
    180   1.1   garbled 		error = ESRCH;
    181   1.1   garbled 		goto out;
    182   1.1   garbled 	}
    183   1.1   garbled 
    184   1.1   garbled 	/*
    185   1.1   garbled 	 * Lookup or allocate a new array info structure for
    186   1.1   garbled 	 * this array. Use the serial number of disk0 as the array#
    187   1.1   garbled 	 */
    188   1.1   garbled 	aai = ata_raid_get_array_info(ATA_RAID_TYPE_VIA, info->disks[0]);
    189   1.1   garbled 
    190   1.1   garbled 	aai->aai_status = AAI_S_READY;
    191   1.1   garbled 
    192   1.1   garbled 	switch (info->type & VIA_T_MASK) {
    193   1.1   garbled 	case VIA_T_RAID0:
    194   1.1   garbled 		aai->aai_level = AAI_L_RAID0;
    195   1.1   garbled 		aai->aai_width = info->stripe_layout & VIA_L_DISKS;
    196   1.1   garbled 		aai->aai_capacity = aai->aai_width * info->disk_sectors;
    197   1.1   garbled 		break;
    198   1.1   garbled 
    199   1.1   garbled 	case VIA_T_RAID1:
    200   1.1   garbled 		aai->aai_level = AAI_L_RAID1;
    201   1.1   garbled 		aai->aai_width = 1;
    202   1.1   garbled 		aai->aai_capacity = aai->aai_width * info->disk_sectors;
    203   1.1   garbled 		break;
    204   1.1   garbled 
    205   1.1   garbled 	case VIA_T_RAID5:
    206   1.1   garbled 		aai->aai_level = AAI_L_RAID5;
    207   1.1   garbled 		aai->aai_width = info->stripe_layout & VIA_L_DISKS;
    208   1.1   garbled 		aai->aai_capacity = (aai->aai_width - 1) * info->disk_sectors;
    209   1.1   garbled 		break;
    210   1.1   garbled 
    211   1.1   garbled 	case VIA_T_SPAN:
    212   1.1   garbled 		aai->aai_level = AAI_L_SPAN;
    213   1.1   garbled 		aai->aai_width = 1;
    214   1.1   garbled 		aai->aai_capacity += info->disk_sectors; /* XXX ??? */
    215   1.1   garbled 		break;
    216   1.1   garbled 
    217   1.1   garbled 	default:
    218   1.8   mlelstv 		aprint_error_dev(dksc->sc_dev,
    219   1.5      cube 		    "unknown VIA V-RAID type 0x%02x\n", info->type);
    220   1.1   garbled 		error = EINVAL;
    221   1.1   garbled 		goto out;
    222   1.1   garbled 	}
    223   1.1   garbled 
    224   1.1   garbled 	aai->aai_type = ATA_RAID_TYPE_VIA;
    225  1.11    andvar 	/*
    226  1.11    andvar 	 * VIA V-RAID supports up to four drives in RAID 0 and JBOD
    227  1.11    andvar 	 * configurations and up to two drives in a RAID 1 configuration.
    228  1.11    andvar 	 */
    229  1.11    andvar 	for (count = 0, disk = 0; disk < 4; disk++)
    230   1.1   garbled 		if (info->disks[disk])
    231   1.1   garbled 			count++;
    232   1.1   garbled 	aai->aai_interleave =
    233   1.1   garbled 		0x08 << ((info->stripe_layout & VIA_L_MASK) >> VIA_L_SHIFT);
    234   1.1   garbled 	aai->aai_ndisks = count;
    235   1.1   garbled 	aai->aai_heads = 255;
    236   1.1   garbled 	aai->aai_sectors = 63;
    237   1.1   garbled 	aai->aai_cylinders = aai->aai_capacity / (63 * 255);
    238   1.1   garbled 	aai->aai_offset = 0;
    239   1.1   garbled 	aai->aai_reserved = 1;
    240   1.1   garbled 
    241   1.1   garbled 	/* XXX - bogus.  RAID1 shouldn't really have an interleave */
    242   1.1   garbled 	if (aai->aai_interleave == 0)
    243   1.1   garbled 		aai->aai_interleave = aai->aai_capacity;
    244   1.1   garbled 
    245  1.11    andvar 	/*
    246  1.11    andvar 	 * VIA V-RAID configuration blocks store the disk index as a value
    247  1.11    andvar 	 * incrementing by 0x04 (0x00, 0x04, 0x08, 0x0C).  Therefore, shift
    248  1.11    andvar 	 * the value left by 2 to obtain the disk number.
    249  1.11    andvar 	 */
    250  1.11    andvar 	drive = info->disk_index >> 2;
    251   1.1   garbled 	if (drive >= aai->aai_ndisks) {
    252   1.8   mlelstv 		aprint_error_dev(dksc->sc_dev,
    253   1.5      cube 		    "drive number %d doesn't make sense within %d-disk "
    254   1.5      cube 		    "array\n", drive, aai->aai_ndisks);
    255   1.1   garbled 		error = EINVAL;
    256   1.1   garbled 		goto out;
    257   1.1   garbled 	}
    258   1.1   garbled 
    259   1.1   garbled 	adi = &aai->aai_disks[drive];
    260   1.8   mlelstv 	adi->adi_dev = dksc->sc_dev;
    261   1.1   garbled 	adi->adi_status = ADI_S_ONLINE | ADI_S_ASSIGNED;
    262   1.1   garbled 	adi->adi_sectors = aai->aai_capacity;
    263   1.1   garbled 	adi->adi_compsize = info->disk_sectors;
    264   1.1   garbled 
    265   1.1   garbled 	error = 0;
    266   1.1   garbled 
    267   1.1   garbled  out:
    268   1.9  jdolecek 	kmem_free(info, sizeof(*info));
    269   1.1   garbled 	return (error);
    270   1.1   garbled }
    271