ata_raid_via.c revision 1.8 1 1.8 mlelstv /* $NetBSD: ata_raid_via.c,v 1.8 2017/11/01 19:34:46 mlelstv 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.8 mlelstv __KERNEL_RCSID(0, "$NetBSD: ata_raid_via.c,v 1.8 2017/11/01 19:34:46 mlelstv 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/malloc.h>
49 1.1 garbled #include <sys/vnode.h>
50 1.1 garbled #include <sys/kauth.h>
51 1.1 garbled
52 1.1 garbled #include <miscfs/specfs/specdev.h>
53 1.1 garbled
54 1.1 garbled #include <dev/ata/atareg.h>
55 1.1 garbled #include <dev/ata/atavar.h>
56 1.1 garbled #include <dev/ata/wdvar.h>
57 1.1 garbled
58 1.1 garbled #include <dev/ata/ata_raidreg.h>
59 1.1 garbled #include <dev/ata/ata_raidvar.h>
60 1.1 garbled
61 1.1 garbled #ifdef ATA_RAID_DEBUG
62 1.1 garbled #define DPRINTF(x) printf x
63 1.1 garbled #else
64 1.1 garbled #define DPRINTF(x) /* nothing */
65 1.1 garbled #endif
66 1.1 garbled
67 1.1 garbled #ifdef ATA_RAID_DEBUG
68 1.1 garbled static const char *
69 1.1 garbled ata_raid_via_type(int type)
70 1.1 garbled {
71 1.1 garbled static char buffer[16];
72 1.1 garbled
73 1.1 garbled switch (type) {
74 1.7 christos case VIA_T_RAID0: return "RAID0";
75 1.7 christos case VIA_T_RAID1: return "RAID1";
76 1.7 christos case VIA_T_RAID5: return "RAID5";
77 1.7 christos case VIA_T_RAID01: return "RAID0+1";
78 1.7 christos case VIA_T_SPAN: return "SPAN";
79 1.7 christos default:
80 1.7 christos snprintf(buffer, sizeof(buffer), "UNKNOWN 0x%02x", type);
81 1.1 garbled return buffer;
82 1.1 garbled }
83 1.1 garbled }
84 1.1 garbled
85 1.1 garbled static void
86 1.1 garbled ata_raid_via_print_info(struct via_raid_conf *info)
87 1.1 garbled {
88 1.1 garbled int i;
89 1.1 garbled
90 1.1 garbled printf("*************** ATA VIA Metadata ****************\n");
91 1.1 garbled printf("magic 0x%02x\n", info->magic);
92 1.1 garbled printf("dummy_0 0x%02x\n", info->dummy_0);
93 1.1 garbled printf("type %s\n",
94 1.1 garbled ata_raid_via_type(info->type & VIA_T_MASK));
95 1.1 garbled printf("bootable %d\n", info->type & VIA_T_BOOTABLE);
96 1.1 garbled printf("unknown %d\n", info->type & VIA_T_UNKNOWN);
97 1.1 garbled printf("disk_index 0x%02x\n", info->disk_index);
98 1.1 garbled printf("stripe_layout 0x%02x\n", info->stripe_layout);
99 1.1 garbled printf(" stripe_disks %d\n", info->stripe_layout & VIA_L_DISKS);
100 1.1 garbled printf(" stripe_sectors %d\n",
101 1.1 garbled 0x08 << ((info->stripe_layout & VIA_L_MASK) >> VIA_L_SHIFT));
102 1.1 garbled printf("disk_sectors %ju\n", info->disk_sectors);
103 1.1 garbled printf("disk_id 0x%08x\n", info->disk_id);
104 1.1 garbled printf("DISK# disk_id\n");
105 1.1 garbled for (i = 0; i < 8; i++) {
106 1.1 garbled if (info->disks[i])
107 1.1 garbled printf(" %d 0x%08x\n", i, info->disks[i]);
108 1.1 garbled }
109 1.1 garbled printf("checksum 0x%02x\n", info->checksum);
110 1.1 garbled printf("=================================================\n");
111 1.1 garbled }
112 1.1 garbled #endif
113 1.1 garbled
114 1.1 garbled int
115 1.1 garbled ata_raid_read_config_via(struct wd_softc *sc)
116 1.1 garbled {
117 1.8 mlelstv struct dk_softc *dksc = &sc->sc_dksc;
118 1.1 garbled struct via_raid_conf *info;
119 1.1 garbled struct atabus_softc *atabus;
120 1.1 garbled struct vnode *vp;
121 1.1 garbled int bmajor, error;
122 1.1 garbled dev_t dev;
123 1.1 garbled uint32_t drive;
124 1.6 christos uint8_t checksum, checksum_alt, byte3, *ptr;
125 1.1 garbled int count, disk;
126 1.1 garbled struct ataraid_array_info *aai;
127 1.1 garbled struct ataraid_disk_info *adi;
128 1.1 garbled
129 1.1 garbled info = malloc(sizeof(*info), M_DEVBUF, M_WAITOK);
130 1.1 garbled
131 1.8 mlelstv bmajor = devsw_name2blk(dksc->sc_xname, NULL, 0);
132 1.1 garbled
133 1.1 garbled /* Get a vnode for the raw partition of this disk. */
134 1.8 mlelstv dev = MAKEDISKDEV(bmajor, device_unit(dksc->sc_dev), RAW_PART);
135 1.1 garbled error = bdevvp(dev, &vp);
136 1.1 garbled if (error)
137 1.1 garbled goto out;
138 1.1 garbled
139 1.2 pooka error = VOP_OPEN(vp, FREAD, NOCRED);
140 1.1 garbled if (error) {
141 1.1 garbled vput(vp);
142 1.1 garbled goto out;
143 1.1 garbled }
144 1.1 garbled
145 1.1 garbled error = ata_raid_config_block_rw(vp, VIA_LBA(sc), info,
146 1.1 garbled sizeof(*info), B_READ);
147 1.3 mjf VOP_CLOSE(vp, FREAD, NOCRED);
148 1.1 garbled vput(vp);
149 1.1 garbled if (error) {
150 1.8 mlelstv aprint_error_dev(dksc->sc_dev,
151 1.5 cube "error %d reading VIA V-RAID config block\n", error);
152 1.1 garbled goto out;
153 1.1 garbled }
154 1.1 garbled
155 1.1 garbled #ifdef ATA_RAID_DEBUG
156 1.1 garbled ata_raid_via_print_info(info);
157 1.4 xtraeme printf("MAGIC == 0x%02x\n", info->magic);
158 1.1 garbled #endif
159 1.1 garbled
160 1.1 garbled /* Check the signature. */
161 1.1 garbled if (info->magic != VIA_MAGIC) {
162 1.1 garbled DPRINTF(("%s: VIA V-RAID signature check failed\n",
163 1.8 mlelstv dksc->sc_xname));
164 1.1 garbled error = ESRCH;
165 1.1 garbled goto out;
166 1.1 garbled }
167 1.1 garbled
168 1.1 garbled /* calculate checksum and compare for valid */
169 1.6 christos for (byte3 = 0, checksum = 0, ptr = (uint8_t *)info, count = 0;
170 1.6 christos count < 50; count++)
171 1.1 garbled if (count == 3)
172 1.6 christos byte3 = *ptr++;
173 1.1 garbled else
174 1.1 garbled checksum += *ptr++;
175 1.6 christos checksum_alt = checksum + (byte3 & ~VIA_T_BOOTABLE);
176 1.6 christos checksum += byte3;
177 1.6 christos if (checksum != info->checksum && checksum_alt != info->checksum) {
178 1.6 christos DPRINTF(("%s: VIA V-RAID checksum failed 0x%02x != "
179 1.8 mlelstv "0x%02x or 0x%02x\n", dksc->sc_xname,
180 1.6 christos info->checksum, checksum, checksum_alt));
181 1.1 garbled error = ESRCH;
182 1.1 garbled goto out;
183 1.1 garbled }
184 1.1 garbled
185 1.1 garbled /*
186 1.1 garbled * Lookup or allocate a new array info structure for
187 1.1 garbled * this array. Use the serial number of disk0 as the array#
188 1.1 garbled */
189 1.1 garbled aai = ata_raid_get_array_info(ATA_RAID_TYPE_VIA, info->disks[0]);
190 1.1 garbled
191 1.1 garbled aai->aai_status = AAI_S_READY;
192 1.1 garbled
193 1.1 garbled switch (info->type & VIA_T_MASK) {
194 1.1 garbled case VIA_T_RAID0:
195 1.1 garbled aai->aai_level = AAI_L_RAID0;
196 1.1 garbled aai->aai_width = info->stripe_layout & VIA_L_DISKS;
197 1.1 garbled aai->aai_capacity = aai->aai_width * info->disk_sectors;
198 1.1 garbled break;
199 1.1 garbled
200 1.1 garbled case VIA_T_RAID1:
201 1.1 garbled aai->aai_level = AAI_L_RAID1;
202 1.1 garbled aai->aai_width = 1;
203 1.1 garbled aai->aai_capacity = aai->aai_width * info->disk_sectors;
204 1.1 garbled break;
205 1.1 garbled
206 1.1 garbled case VIA_T_RAID5:
207 1.1 garbled aai->aai_level = AAI_L_RAID5;
208 1.1 garbled aai->aai_width = info->stripe_layout & VIA_L_DISKS;
209 1.1 garbled aai->aai_capacity = (aai->aai_width - 1) * info->disk_sectors;
210 1.1 garbled break;
211 1.1 garbled
212 1.1 garbled case VIA_T_SPAN:
213 1.1 garbled aai->aai_level = AAI_L_SPAN;
214 1.1 garbled aai->aai_width = 1;
215 1.1 garbled aai->aai_capacity += info->disk_sectors; /* XXX ??? */
216 1.1 garbled break;
217 1.1 garbled
218 1.1 garbled default:
219 1.8 mlelstv aprint_error_dev(dksc->sc_dev,
220 1.5 cube "unknown VIA V-RAID type 0x%02x\n", info->type);
221 1.1 garbled error = EINVAL;
222 1.1 garbled goto out;
223 1.1 garbled }
224 1.1 garbled
225 1.1 garbled aai->aai_type = ATA_RAID_TYPE_VIA;
226 1.1 garbled for (count = 0, disk = 0; disk < 8; disk++)
227 1.1 garbled if (info->disks[disk])
228 1.1 garbled count++;
229 1.1 garbled aai->aai_interleave =
230 1.1 garbled 0x08 << ((info->stripe_layout & VIA_L_MASK) >> VIA_L_SHIFT);
231 1.1 garbled aai->aai_ndisks = count;
232 1.1 garbled aai->aai_heads = 255;
233 1.1 garbled aai->aai_sectors = 63;
234 1.1 garbled aai->aai_cylinders = aai->aai_capacity / (63 * 255);
235 1.1 garbled aai->aai_offset = 0;
236 1.1 garbled aai->aai_reserved = 1;
237 1.1 garbled
238 1.1 garbled /* XXX - bogus. RAID1 shouldn't really have an interleave */
239 1.1 garbled if (aai->aai_interleave == 0)
240 1.1 garbled aai->aai_interleave = aai->aai_capacity;
241 1.1 garbled
242 1.8 mlelstv atabus = device_private(device_parent(dksc->sc_dev));
243 1.1 garbled drive = atabus->sc_chan->ch_channel;
244 1.1 garbled if (drive >= aai->aai_ndisks) {
245 1.8 mlelstv aprint_error_dev(dksc->sc_dev,
246 1.5 cube "drive number %d doesn't make sense within %d-disk "
247 1.5 cube "array\n", drive, aai->aai_ndisks);
248 1.1 garbled error = EINVAL;
249 1.1 garbled goto out;
250 1.1 garbled }
251 1.1 garbled
252 1.1 garbled adi = &aai->aai_disks[drive];
253 1.8 mlelstv adi->adi_dev = dksc->sc_dev;
254 1.1 garbled adi->adi_status = ADI_S_ONLINE | ADI_S_ASSIGNED;
255 1.1 garbled adi->adi_sectors = aai->aai_capacity;
256 1.1 garbled adi->adi_compsize = info->disk_sectors;
257 1.1 garbled
258 1.1 garbled error = 0;
259 1.1 garbled
260 1.1 garbled out:
261 1.1 garbled free(info, M_DEVBUF);
262 1.1 garbled return (error);
263 1.1 garbled }
264