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