ata_raid_jmicron.c revision 1.6
1/*	$NetBSD: ata_raid_jmicron.c,v 1.6 2017/11/01 19:34:46 mlelstv Exp $	*/
2
3/*-
4 * Copyright (c) 2000-2008 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 JMicron Technology RAID controller configuration blocks.
33 *
34 * Adapted to NetBSD by Juan Romero Pardines (xtraeme@gmail.org).
35 */
36
37#include <sys/cdefs.h>
38__KERNEL_RCSID(0, "$NetBSD: ata_raid_jmicron.c,v 1.6 2017/11/01 19:34:46 mlelstv 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/malloc.h>
49#include <sys/vnode.h>
50#include <sys/kauth.h>
51
52#include <miscfs/specfs/specdev.h>
53
54#include <dev/ata/atareg.h>
55#include <dev/ata/atavar.h>
56#include <dev/ata/wdvar.h>
57
58#include <dev/ata/ata_raidreg.h>
59#include <dev/ata/ata_raidvar.h>
60
61#ifdef ATA_RAID_DEBUG
62#define	DPRINTF(x)	printf x
63#else
64#define	DPRINTF(x)	/* nothing */
65#endif
66
67#ifdef ATA_RAID_DEBUG
68static const char *
69ata_raid_jmicron_type(int type)
70{
71	static char buffer[16];
72
73	switch (type) {
74	case JM_T_RAID0:
75		return "RAID0";
76	case JM_T_RAID1:
77		return "RAID1";
78	case JM_T_RAID01:
79		return "RAID0+1";
80	case JM_T_RAID5:
81		return "RAID5";
82	case JM_T_JBOD:
83		return "JBOD";
84	default:
85		snprintf(buffer, sizeof(buffer), "UNKNOWN 0x%02x", type);
86		return buffer;
87	}
88}
89
90static void
91ata_raid_jmicron_print_info(struct jmicron_raid_conf *info)
92{
93	int i;
94
95	printf("****** ATA JMicron Technology Corp Metadata ******\n");
96	printf("signature           %.2s\n",	info->signature);
97	printf("version             0x%04x\n",	info->version);
98	printf("checksum            0x%04x\n",	info->checksum);
99	printf("disk_id             0x%08x\n",	info->disk_id);
100	printf("offset              0x%08x\n",	info->offset);
101	printf("disk_sectors_low    0x%08x\n",	info->disk_sectors_low);
102	printf("disk_sectors_high   0x%08x\n",	info->disk_sectors_high);
103	printf("name                %.16s\n",	info->name);
104	printf("type                %s\n",
105	    ata_raid_jmicron_type(info->type));
106	printf("stripe_shift        %d\n",	info->stripe_shift);
107	printf("flags               0x%04x\n",	info->flags);
108	printf("spare:\n");
109	for (i = 0; i < 2 && info->spare[i]; i++)
110		printf("    %d                  0x%08x\n", i, info->spare[i]);
111	printf("disks:\n");
112	for (i = 0; i < 8 && info->disks[i]; i++)
113		printf("    %d                  0x%08x\n", i, info->disks[i]);
114	printf("=================================================\n");
115}
116#endif
117
118int
119ata_raid_read_config_jmicron(struct wd_softc *sc)
120{
121	struct dk_softc *dksc = &sc->sc_dksc;
122	struct atabus_softc *atabus;
123	struct jmicron_raid_conf *info;
124	struct vnode *vp;
125	struct ataraid_array_info *aai;
126	struct ataraid_disk_info *adi;
127	uint64_t disk_size;
128	uint32_t drive;
129	uint16_t checksum, *ptr;
130	int bmajor, error, count, disk, total_disks;
131	dev_t dev;
132
133	info = malloc(sizeof(*info), M_DEVBUF, M_WAITOK|M_ZERO);
134
135	bmajor = devsw_name2blk(dksc->sc_xname, NULL, 0);
136
137	/* Get a vnode for the raw partition of this disk. */
138	dev = MAKEDISKDEV(bmajor, device_unit(dksc->sc_dev), RAW_PART);
139	error = bdevvp(dev, &vp);
140	if (error)
141		goto out;
142
143	error = VOP_OPEN(vp, FREAD, NOCRED);
144	if (error) {
145		vput(vp);
146		goto out;
147	}
148
149	error = ata_raid_config_block_rw(vp, JMICRON_LBA(sc), info,
150	    sizeof(*info), B_READ);
151	VOP_CLOSE(vp, FREAD, NOCRED);
152	vput(vp);
153	if (error) {
154		DPRINTF(("%s: error %d reading JMicron config block\n",
155		    dksc->sc_xname, error));
156		goto out;
157	}
158
159	/* Check for JMicron signature. */
160	if (strncmp(info->signature, JMICRON_MAGIC, 2)) {
161		DPRINTF(("%s: JMicron RAID signature check failed\n",
162		    dksc->sc_xname));
163		error = ESRCH;
164		goto out;
165	}
166
167	/* calculate checksum and compare for valid */
168	for (checksum = 0, ptr = (uint16_t *)info, count = 0;
169	     count < 64; count++)
170		checksum += *ptr++;
171	if (checksum) {
172		DPRINTF(("%s: JMicron checksum failed\n",
173		    dksc->sc_xname));
174		error = ESRCH;
175		goto out;
176	}
177
178#ifdef ATA_RAID_DEBUG
179	ata_raid_jmicron_print_info(info);
180#endif
181	/*
182	 * Check that there aren't stale config blocks without
183	 * any array set configured.
184	 */
185	for (total_disks = 0, disk = 0; disk < JM_MAX_DISKS; disk++)
186		if (info->disks[disk] == info->disk_id)
187			total_disks++;
188	if (total_disks <= 1) {
189		error = EINVAL;
190		goto out;
191	}
192
193	/*
194	 * Check volume's state and bail out if it's not acceptable.
195	 */
196	if ((info->flags & (JM_F_READY|JM_F_BOOTABLE|JM_F_ACTIVE)) == 0) {
197		error = EINVAL;
198		goto out;
199	}
200
201	/*
202	 * Lookup or allocate a new array info structure for
203	 * this array.
204	 */
205	aai = ata_raid_get_array_info(ATA_RAID_TYPE_JMICRON, 0);
206	aai->aai_status = AAI_S_READY;
207
208	switch (info->type) {
209	case JM_T_RAID0:
210		aai->aai_level = AAI_L_RAID0;
211		aai->aai_width = total_disks;
212		break;
213	case JM_T_RAID1:
214		aai->aai_level = AAI_L_RAID1;
215		aai->aai_width = 1;
216		break;
217	case JM_T_RAID01:
218		aai->aai_level = AAI_L_RAID0 | AAI_L_RAID1;
219		aai->aai_width = total_disks / 2;
220		break;
221	case JM_T_JBOD:
222		aai->aai_level = AAI_L_SPAN;
223		aai->aai_width = total_disks;
224		break;
225	default:
226		DPRINTF(("%s: unknown JMicron RAID type 0x%02x\n",
227		    dksc->sc_xname, info->type));
228		error = EINVAL;
229		goto out;
230	}
231
232	disk_size = (info->disk_sectors_high << 16) + info->disk_sectors_low;
233	aai->aai_type = ATA_RAID_TYPE_JMICRON;
234	aai->aai_generation = 0;
235	aai->aai_capacity = disk_size * aai->aai_width;
236	aai->aai_interleave = 2 << info->stripe_shift;
237	aai->aai_ndisks = total_disks;
238	aai->aai_heads = 255;
239	aai->aai_sectors = 63;
240	aai->aai_cylinders =
241	    aai->aai_capacity / (aai->aai_heads * aai->aai_sectors);
242	aai->aai_offset = info->offset * 16;
243	aai->aai_reserved = 2;
244	if (info->name)
245		strlcpy(aai->aai_name, info->name, sizeof(aai->aai_name));
246
247	atabus = device_private(device_parent(dksc->sc_dev));
248	drive = atabus->sc_chan->ch_channel;
249	if (drive >= aai->aai_ndisks) {
250		DPRINTF(("%s: drive number %d doesn't make sense within "
251		    "%d-disk array\n", dksc->sc_xname,
252		    drive, aai->aai_ndisks));
253		error = EINVAL;
254		goto out;
255	}
256
257	if (info->disks[drive] == info->disk_id) {
258		adi = &aai->aai_disks[drive];
259		adi->adi_dev = dksc->sc_dev;
260		adi->adi_status = ADI_S_ONLINE | ADI_S_ASSIGNED;
261		adi->adi_sectors = aai->aai_capacity;
262		adi->adi_compsize = disk_size - aai->aai_reserved;
263	}
264
265	error = 0;
266
267 out:
268	free(info, M_DEVBUF);
269	return error;
270}
271