1 /* $NetBSD: ata_raid_promise.c,v 1.14 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 Promise ATA RAID controller configuration blocks.
33 *
34 * Adapted to NetBSD by Jason R. Thorpe of Wasabi Systems, Inc.
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: ata_raid_promise.c,v 1.14 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 int
ata_raid_read_config_promise(struct wd_softc * sc)67 ata_raid_read_config_promise(struct wd_softc *sc)
68 {
69 struct dk_softc *dksc = &sc->sc_dksc;
70 struct promise_raid_conf *info;
71 struct vnode *vp;
72 int bmajor, error, count;
73 u_int disk;
74 dev_t dev;
75 uint32_t cksum, *ckptr;
76 struct ataraid_array_info *aai;
77 struct ataraid_disk_info *adi;
78
79 info = kmem_zalloc(sizeof(*info), KM_SLEEP);
80
81 bmajor = devsw_name2blk(dksc->sc_xname, NULL, 0);
82
83 /* Get a vnode for the raw partition of this disk. */
84 dev = MAKEDISKDEV(bmajor, device_unit(dksc->sc_dev), RAW_PART);
85 error = bdevvp(dev, &vp);
86 if (error)
87 goto out;
88
89 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
90 error = VOP_OPEN(vp, FREAD, NOCRED);
91 if (error) {
92 vput(vp);
93 goto out;
94 }
95
96 error = ata_raid_config_block_rw(vp, PR_LBA(sc), info,
97 sizeof(*info), B_READ);
98 VOP_CLOSE(vp, FREAD, NOCRED);
99 vput(vp);
100 if (error) {
101 aprint_error_dev(dksc->sc_dev,
102 "error %d reading Promise config block\n", error);
103 goto out;
104 }
105
106 /* Check the signature. */
107 if (strncmp(info->promise_id, PR_MAGIC, sizeof(PR_MAGIC)) != 0) {
108 DPRINTF(("%s: Promise signature check failed\n",
109 dksc->sc_xname));
110 error = ESRCH;
111 goto out;
112 }
113
114 /* Verify the checksum. */
115 for (cksum = 0, ckptr = (uint32_t *) info, count = 0; count < 511;
116 count++)
117 cksum += *ckptr++;
118 if (cksum != *ckptr) {
119 DPRINTF(("%s: Promise checksum failed\n",
120 dksc->sc_xname));
121 error = ESRCH;
122 goto out;
123 }
124
125 if (info->raid.integrity != PR_I_VALID) {
126 DPRINTF(("%s: Promise config block marked invalid\n",
127 dksc->sc_xname));
128 error = ESRCH;
129 goto out;
130 }
131
132 /*
133 * Lookup or allocate a new array info structure for
134 * this array.
135 */
136 aai = ata_raid_get_array_info(ATA_RAID_TYPE_PROMISE,
137 info->raid.array_number);
138
139 if (info->raid.generation == 0 ||
140 info->raid.generation > aai->aai_generation) {
141 aai->aai_generation = info->raid.generation;
142 /* aai_type and aai_arrayno filled in already */
143 if ((info->raid.status &
144 (PR_S_VALID | PR_S_ONLINE | PR_S_INITED | PR_S_READY)) ==
145 (PR_S_VALID | PR_S_ONLINE | PR_S_INITED | PR_S_READY)) {
146 aai->aai_status |= AAI_S_READY;
147 if (info->raid.status & PR_S_DEGRADED)
148 aai->aai_status |= AAI_S_DEGRADED;
149 } else
150 aai->aai_status &= ~AAI_S_READY;
151
152 switch (info->raid.type) {
153 case PR_T_RAID0:
154 aai->aai_level = AAI_L_RAID0;
155 break;
156
157 case PR_T_RAID1:
158 aai->aai_level = AAI_L_RAID1;
159 if (info->raid.array_width > 1)
160 aai->aai_level |= AAI_L_RAID0;
161 break;
162
163 case PR_T_SPAN:
164 aai->aai_level = AAI_L_SPAN;
165 break;
166
167 default:
168 aprint_error_dev(dksc->sc_dev,
169 "unknown Promise RAID type 0x%02x\n",
170 info->raid.type);
171 error = EINVAL;
172 goto out;
173 }
174
175 aai->aai_interleave = 1U << info->raid.stripe_shift;
176 aai->aai_width = info->raid.array_width;
177 aai->aai_ndisks = info->raid.total_disks;
178 aai->aai_heads = info->raid.heads + 1;
179 aai->aai_sectors = info->raid.sectors;
180 aai->aai_cylinders = info->raid.cylinders + 1;
181 aai->aai_capacity = info->raid.total_sectors;
182 aai->aai_offset = 0;
183 aai->aai_reserved = 63;
184
185 for (disk = 0; disk < aai->aai_ndisks; disk++) {
186 adi = &aai->aai_disks[disk];
187 adi->adi_status = 0;
188 if (info->raid.disk[disk].flags & PR_F_ONLINE)
189 adi->adi_status |= ADI_S_ONLINE;
190 if (info->raid.disk[disk].flags & PR_F_ASSIGNED)
191 adi->adi_status |= ADI_S_ASSIGNED;
192 if (info->raid.disk[disk].flags & PR_F_SPARE) {
193 adi->adi_status &= ~ADI_S_ONLINE;
194 adi->adi_status |= ADI_S_SPARE;
195 }
196 if (info->raid.disk[disk].flags &
197 (PR_F_REDIR | PR_F_DOWN))
198 adi->adi_status &= ~ADI_S_ONLINE;
199 }
200 }
201 adi = &aai->aai_disks[info->raid.disk_number];
202 if (adi->adi_status) {
203 adi->adi_dev = dksc->sc_dev;
204 adi->adi_sectors = info->raid.disk_sectors;
205 adi->adi_compsize = sc->sc_capacity - aai->aai_reserved;
206 }
207
208 error = 0;
209
210 out:
211 kmem_free(info, sizeof(*info));
212 return (error);
213 }
214