summaryrefslogtreecommitdiff
path: root/sys/dev/ata/ata_raid_via.c
blob: 49955eb6f5f83c2c0f7b86c313a9f8c7574e9eeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*	$NetBSD: ata_raid_via.c,v 1.10 2022/03/19 13:51:01 hannken Exp $	*/

/*-
 * Copyright (c) 2000,2001,2002 Søren Schmidt <sos@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer,
 *    without modification, immediately at the beginning of the file.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Support for parsing VIA V-RAID ATA RAID controller configuration blocks.
 *
 * Adapted to NetBSD by Tim Rightnour (garbled@netbsd.org)
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ata_raid_via.c,v 1.10 2022/03/19 13:51:01 hannken Exp $");

#include <sys/param.h>
#include <sys/buf.h>
#include <sys/bufq.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/disk.h>
#include <sys/disklabel.h>
#include <sys/fcntl.h>
#include <sys/vnode.h>
#include <sys/kauth.h>

#include <miscfs/specfs/specdev.h>

#include <dev/ata/atareg.h>
#include <dev/ata/atavar.h>
#include <dev/ata/wdvar.h>

#include <dev/ata/ata_raidreg.h>
#include <dev/ata/ata_raidvar.h>

#ifdef ATA_RAID_DEBUG
#define	DPRINTF(x)	printf x
#else
#define	DPRINTF(x)	/* nothing */
#endif

#ifdef ATA_RAID_DEBUG
static const char *
ata_raid_via_type(int type)
{
	static char buffer[16];

	switch (type) {
	case VIA_T_RAID0:   return "RAID0";
	case VIA_T_RAID1:   return "RAID1";
	case VIA_T_RAID5:   return "RAID5";
	case VIA_T_RAID01:  return "RAID0+1";
	case VIA_T_SPAN:    return "SPAN";
	default:
		snprintf(buffer, sizeof(buffer), "UNKNOWN 0x%02x", type);
		return buffer;
	}
}

static void
ata_raid_via_print_info(struct via_raid_conf *info)
{
	int i;
  
	printf("*************** ATA VIA Metadata ****************\n");
	printf("magic               0x%02x\n", info->magic);
	printf("dummy_0             0x%02x\n", info->dummy_0);
	printf("type                %s\n",
	    ata_raid_via_type(info->type & VIA_T_MASK));
	printf("bootable            %d\n", info->type & VIA_T_BOOTABLE);
	printf("unknown             %d\n", info->type & VIA_T_UNKNOWN);
	printf("disk_index          0x%02x\n", info->disk_index);
	printf("stripe_layout       0x%02x\n", info->stripe_layout);
	printf(" stripe_disks       %d\n", info->stripe_layout & VIA_L_DISKS);
	printf(" stripe_sectors     %d\n",
	    0x08 << ((info->stripe_layout & VIA_L_MASK) >> VIA_L_SHIFT));
	printf("disk_sectors        %ju\n", info->disk_sectors);
	printf("disk_id             0x%08x\n", info->disk_id);
	printf("DISK#   disk_id\n");
	for (i = 0; i < 8; i++) {
	if (info->disks[i])
		printf("  %d    0x%08x\n", i, info->disks[i]);
	}    
	printf("checksum            0x%02x\n", info->checksum);
	printf("=================================================\n");
}
#endif

int
ata_raid_read_config_via(struct wd_softc *sc)
{
	struct dk_softc *dksc = &sc->sc_dksc;
	struct via_raid_conf *info;
	struct atabus_softc *atabus;
	struct vnode *vp;
	int bmajor, error;
	dev_t dev;
	uint32_t drive;
	uint8_t checksum, checksum_alt, byte3, *ptr;
	int count, disk;
	struct ataraid_array_info *aai;
	struct ataraid_disk_info *adi;

	info = kmem_zalloc(sizeof(*info), KM_SLEEP);

	bmajor = devsw_name2blk(dksc->sc_xname, NULL, 0);

	/* Get a vnode for the raw partition of this disk. */
	dev = MAKEDISKDEV(bmajor, device_unit(dksc->sc_dev), RAW_PART);
	error = bdevvp(dev, &vp);
	if (error)
		goto out;

	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
	error = VOP_OPEN(vp, FREAD, NOCRED);
	if (error) {
		vput(vp);
		goto out;
	}

	error = ata_raid_config_block_rw(vp, VIA_LBA(sc), info,
	    sizeof(*info), B_READ);
	VOP_CLOSE(vp, FREAD, NOCRED);
	vput(vp);
	if (error) {
		aprint_error_dev(dksc->sc_dev,
		    "error %d reading VIA V-RAID config block\n", error);
		goto out;
	}

#ifdef ATA_RAID_DEBUG
	ata_raid_via_print_info(info);
	printf("MAGIC == 0x%02x\n", info->magic);
#endif

	/* Check the signature. */
	if (info->magic != VIA_MAGIC) {
		DPRINTF(("%s: VIA V-RAID signature check failed\n",
		    dksc->sc_xname));
		error = ESRCH;
		goto out;
	}

	/* calculate checksum and compare for valid */
	for (byte3 = 0, checksum = 0, ptr = (uint8_t *)info, count = 0;
	    count < 50; count++)
		if (count == 3)
			byte3 = *ptr++;
		else
			checksum += *ptr++;
	checksum_alt = checksum + (byte3 & ~VIA_T_BOOTABLE);
	checksum += byte3;
	if (checksum != info->checksum && checksum_alt != info->checksum) {
		DPRINTF(("%s: VIA V-RAID checksum failed 0x%02x != "
		    "0x%02x or 0x%02x\n", dksc->sc_xname,
		    info->checksum, checksum, checksum_alt));
		error = ESRCH;
		goto out;
	}
	
	/*
	 * Lookup or allocate a new array info structure for
	 * this array. Use the serial number of disk0 as the array#
	 */
	aai = ata_raid_get_array_info(ATA_RAID_TYPE_VIA, info->disks[0]);

	aai->aai_status = AAI_S_READY;

	switch (info->type & VIA_T_MASK) {
	case VIA_T_RAID0:
		aai->aai_level = AAI_L_RAID0;
		aai->aai_width = info->stripe_layout & VIA_L_DISKS;
		aai->aai_capacity = aai->aai_width * info->disk_sectors;
		break;

	case VIA_T_RAID1:
		aai->aai_level = AAI_L_RAID1;
		aai->aai_width = 1;
		aai->aai_capacity = aai->aai_width * info->disk_sectors;
		break;

	case VIA_T_RAID5:
		aai->aai_level = AAI_L_RAID5;
		aai->aai_width = info->stripe_layout & VIA_L_DISKS;
		aai->aai_capacity = (aai->aai_width - 1) * info->disk_sectors;
		break;

	case VIA_T_SPAN:
		aai->aai_level = AAI_L_SPAN;
		aai->aai_width = 1;
		aai->aai_capacity += info->disk_sectors; /* XXX ??? */
		break;

	default:
		aprint_error_dev(dksc->sc_dev,
		    "unknown VIA V-RAID type 0x%02x\n", info->type);
		error = EINVAL;
		goto out;
	}

	aai->aai_type = ATA_RAID_TYPE_VIA;
	for (count = 0, disk = 0; disk < 8; disk++)
		if (info->disks[disk])
			count++;
	aai->aai_interleave =
		0x08 << ((info->stripe_layout & VIA_L_MASK) >> VIA_L_SHIFT);
	aai->aai_ndisks = count;
	aai->aai_heads = 255;
	aai->aai_sectors = 63;
	aai->aai_cylinders = aai->aai_capacity / (63 * 255);
	aai->aai_offset = 0;
	aai->aai_reserved = 1;

	/* XXX - bogus.  RAID1 shouldn't really have an interleave */
	if (aai->aai_interleave == 0)
		aai->aai_interleave = aai->aai_capacity;

	atabus = device_private(device_parent(dksc->sc_dev));
	drive = atabus->sc_chan->ch_channel;
	if (drive >= aai->aai_ndisks) {
		aprint_error_dev(dksc->sc_dev,
		    "drive number %d doesn't make sense within %d-disk "
		    "array\n", drive, aai->aai_ndisks);
		error = EINVAL;
		goto out;
	}

	adi = &aai->aai_disks[drive];
	adi->adi_dev = dksc->sc_dev;
	adi->adi_status = ADI_S_ONLINE | ADI_S_ASSIGNED;
	adi->adi_sectors = aai->aai_capacity;
	adi->adi_compsize = info->disk_sectors;

	error = 0;

 out:
	kmem_free(info, sizeof(*info));
	return (error);
}