No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / ata / ata_raid_jmicron.c
blobd914d1ca3234b614c876c7f8a13bf68b01f03249
1 /* $NetBSD: ata_raid_jmicron.c,v 1.3 2008/09/15 11:44:50 tron Exp $ */
3 /*-
4 * Copyright (c) 2000-2008 Søren Schmidt <sos@FreeBSD.org>
5 * All rights reserved.
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.
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.
32 * Support for parsing JMicron Technology RAID controller configuration blocks.
34 * Adapted to NetBSD by Juan Romero Pardines (xtraeme@gmail.org).
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: ata_raid_jmicron.c,v 1.3 2008/09/15 11:44:50 tron Exp $");
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>
52 #include <miscfs/specfs/specdev.h>
54 #include <dev/ata/atareg.h>
55 #include <dev/ata/atavar.h>
56 #include <dev/ata/wdvar.h>
58 #include <dev/ata/ata_raidreg.h>
59 #include <dev/ata/ata_raidvar.h>
61 #ifdef ATA_RAID_DEBUG
62 #define DPRINTF(x) printf x
63 #else
64 #define DPRINTF(x) /* nothing */
65 #endif
67 #ifdef ATA_RAID_DEBUG
68 static const char *
69 ata_raid_jmicron_type(int type)
71 static char buffer[16];
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 sprintf(buffer, "UNKNOWN 0x%02x", type);
86 return buffer;
90 static void
91 ata_raid_jmicron_print_info(struct jmicron_raid_conf *info)
93 int i;
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");
116 #endif
119 ata_raid_read_config_jmicron(struct wd_softc *sc)
121 struct atabus_softc *atabus;
122 struct jmicron_raid_conf *info;
123 struct vnode *vp;
124 struct ataraid_array_info *aai;
125 struct ataraid_disk_info *adi;
126 uint64_t disk_size;
127 uint32_t drive;
128 uint16_t checksum, *ptr;
129 int bmajor, error, count, disk, total_disks;
130 dev_t dev;
132 info = malloc(sizeof(*info), M_DEVBUF, M_WAITOK|M_ZERO);
134 bmajor = devsw_name2blk(device_xname(sc->sc_dev), NULL, 0);
136 /* Get a vnode for the raw partition of this disk. */
137 dev = MAKEDISKDEV(bmajor, device_unit(sc->sc_dev), RAW_PART);
138 error = bdevvp(dev, &vp);
139 if (error)
140 goto out;
142 error = VOP_OPEN(vp, FREAD, NOCRED);
143 if (error) {
144 vput(vp);
145 goto out;
148 error = ata_raid_config_block_rw(vp, JMICRON_LBA(sc), info,
149 sizeof(*info), B_READ);
150 VOP_CLOSE(vp, FREAD, NOCRED);
151 vput(vp);
152 if (error) {
153 DPRINTF(("%s: error %d reading JMicron config block\n",
154 device_xname(sc->sc_dev), error));
155 goto out;
158 /* Check for JMicron signature. */
159 if (strncmp(info->signature, JMICRON_MAGIC, 2)) {
160 DPRINTF(("%s: JMicron RAID signature check failed\n",
161 device_xname(sc->sc_dev)));
162 error = ESRCH;
163 goto out;
166 /* calculate checksum and compare for valid */
167 for (checksum = 0, ptr = (uint16_t *)info, count = 0;
168 count < 64; count++)
169 checksum += *ptr++;
170 if (checksum) {
171 DPRINTF(("%s: JMicron checksum failed\n",
172 device_xname(sc->sc_dev)));
173 error = ESRCH;
174 goto out;
177 #ifdef ATA_RAID_DEBUG
178 ata_raid_jmicron_print_info(info);
179 #endif
181 * Check that there aren't stale config blocks without
182 * any array set configured.
184 for (total_disks = 0, disk = 0; disk < JM_MAX_DISKS; disk++)
185 if (info->disks[disk] == info->disk_id)
186 total_disks++;
187 if (total_disks <= 1) {
188 error = EINVAL;
189 goto out;
193 * Check volume's state and bail out if it's not acceptable.
195 if ((info->flags & (JM_F_READY|JM_F_BOOTABLE|JM_F_ACTIVE)) == 0) {
196 error = EINVAL;
197 goto out;
201 * Lookup or allocate a new array info structure for
202 * this array.
204 aai = ata_raid_get_array_info(ATA_RAID_TYPE_JMICRON, 0);
205 aai->aai_status = AAI_S_READY;
207 switch (info->type) {
208 case JM_T_RAID0:
209 aai->aai_level = AAI_L_RAID0;
210 aai->aai_width = total_disks;
211 break;
212 case JM_T_RAID1:
213 aai->aai_level = AAI_L_RAID1;
214 aai->aai_width = 1;
215 break;
216 case JM_T_RAID01:
217 aai->aai_level = AAI_L_RAID0 | AAI_L_RAID1;
218 aai->aai_width = total_disks / 2;
219 break;
220 case JM_T_JBOD:
221 aai->aai_level = AAI_L_SPAN;
222 aai->aai_width = total_disks;
223 break;
224 default:
225 DPRINTF(("%s: unknown JMicron RAID type 0x%02x\n",
226 device_xname(sc->sc_dev), info->type));
227 error = EINVAL;
228 goto out;
231 disk_size = (info->disk_sectors_high << 16) + info->disk_sectors_low;
232 aai->aai_type = ATA_RAID_TYPE_JMICRON;
233 aai->aai_generation = 0;
234 aai->aai_capacity = disk_size * aai->aai_width;
235 aai->aai_interleave = 2 << info->stripe_shift;
236 aai->aai_ndisks = total_disks;
237 aai->aai_heads = 255;
238 aai->aai_sectors = 63;
239 aai->aai_cylinders =
240 aai->aai_capacity / (aai->aai_heads * aai->aai_sectors);
241 aai->aai_offset = info->offset * 16;
242 aai->aai_reserved = 2;
243 if (info->name)
244 strlcpy(aai->aai_name, info->name, sizeof(aai->aai_name));
246 atabus = device_private(device_parent(sc->sc_dev));
247 drive = atabus->sc_chan->ch_channel;
248 if (drive >= aai->aai_ndisks) {
249 DPRINTF(("%s: drive number %d doesn't make sense within "
250 "%d-disk array\n", device_xname(sc->sc_dev),
251 drive, aai->aai_ndisks));
252 error = EINVAL;
253 goto out;
256 if (info->disks[drive] == info->disk_id) {
257 adi = &aai->aai_disks[drive];
258 adi->adi_dev = sc->sc_dev;
259 adi->adi_status = ADI_S_ONLINE | ADI_S_ASSIGNED;
260 adi->adi_sectors = aai->aai_capacity;
261 adi->adi_compsize = disk_size - aai->aai_reserved;
264 error = 0;
266 out:
267 free(info, M_DEVBUF);
268 return error;