1 /*        $NetBSD: ata_raid_nvidia.c,v 1.5 2022/03/19 13:51:01 hannken 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  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * Support for parsing nVIDIA MediaShield RAID controller configuration blocks.
31  *
32  * Adapted to NetBSD by Tatoku Ogaito (tacha@NetBSD.org)
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: ata_raid_nvidia.c,v 1.5 2022/03/19 13:51:01 hannken Exp $");
37 
38 #include <sys/param.h>
39 #include <sys/buf.h>
40 #include <sys/bufq.h>
41 #include <sys/conf.h>
42 #include <sys/device.h>
43 #include <sys/disk.h>
44 #include <sys/disklabel.h>
45 #include <sys/fcntl.h>
46 #include <sys/vnode.h>
47 #include <sys/kauth.h>
48 
49 #include <miscfs/specfs/specdev.h>
50 
51 #include <dev/ata/atareg.h>
52 #include <dev/ata/atavar.h>
53 #include <dev/ata/wdvar.h>
54 
55 #include <dev/ata/ata_raidreg.h>
56 #include <dev/ata/ata_raidvar.h>
57 
58 #ifdef ATA_RAID_DEBUG
59 #define   DPRINTF(x)          printf x
60 #else
61 #define   DPRINTF(x)          /* nothing */
62 #endif
63 
64 #ifdef ATA_RAID_DEBUG
65 static const char *
ata_raid_nvidia_type(int type)66 ata_raid_nvidia_type(int type)
67 {
68           static char buffer[16];
69 
70           switch (type) {
71           case NV_T_SPAN:     return "SPAN";
72           case NV_T_RAID0:    return "RAID0";
73           case NV_T_RAID1:    return "RAID1";
74           case NV_T_RAID3:    return "RAID3";
75           case NV_T_RAID5:    return "RAID5";
76           case NV_T_RAID01:   return "RAID0+1";
77           default:
78                     snprintf(buffer, sizeof(buffer), "UNKNOWN 0x%02x", type);
79                     return buffer;
80     }
81 }
82 
83 static void
ata_raid_nvidia_print_info(struct nvidia_raid_conf * info)84 ata_raid_nvidia_print_info(struct nvidia_raid_conf *info)
85 {
86     printf("******** ATA nVidia MediaShield Metadata ********\n");
87     printf("nvidia_id           <%.8s>\n", info->nvidia_id);
88     printf("config_size         %d\n", info->config_size);
89     printf("checksum            0x%08x\n", info->checksum);
90     printf("version             0x%04x\n", info->version);
91     printf("disk_number         %d\n", info->disk_number);
92     printf("dummy_0             0x%02x\n", info->dummy_0);
93     printf("total_sectors       %d\n", info->total_sectors);
94     printf("sectors_size        %d\n", info->sector_size);
95     printf("serial              %.16s\n", info->serial);
96     printf("revision            %.4s\n", info->revision);
97     printf("dummy_1             0x%08x\n", info->dummy_1);
98     printf("magic_0             0x%08x\n", info->magic_0);
99     printf("magic_1             0x%016jx\n", info->magic_1);
100     printf("magic_2             0x%016jx\n", info->magic_2);
101     printf("flags               0x%02x\n", info->flags);
102     printf("array_width         %d\n", info->array_width);
103     printf("total_disks         %d\n", info->total_disks);
104     printf("dummy_2             0x%02x\n", info->dummy_2);
105     printf("type                %s\n", ata_raid_nvidia_type(info->type));
106     printf("dummy_3             0x%04x\n", info->dummy_3);
107     printf("stripe_sectors      %d\n", info->stripe_sectors);
108     printf("stripe_bytes        %d\n", info->stripe_bytes);
109     printf("stripe_shift        %d\n", info->stripe_shift);
110     printf("stripe_mask         0x%08x\n", info->stripe_mask);
111     printf("stripe_sizesectors  %d\n", info->stripe_sizesectors);
112     printf("stripe_sizebytes    %d\n", info->stripe_sizebytes);
113     printf("rebuild_lba         %d\n", info->rebuild_lba);
114     printf("dummy_4             0x%08x\n", info->dummy_4);
115     printf("dummy_5             0x%08x\n", info->dummy_5);
116     printf("status              0x%08x\n", info->status);
117     printf("=================================================\n");
118 }
119 #endif
120 
121 int
ata_raid_read_config_nvidia(struct wd_softc * sc)122 ata_raid_read_config_nvidia(struct wd_softc *sc)
123 {
124           struct dk_softc *dksc = &sc->sc_dksc;
125           struct nvidia_raid_conf *info;
126           struct vnode *vp;
127           int bmajor, error, count;
128           dev_t dev;
129           uint32_t cksum, *ckptr;
130           struct ataraid_array_info *aai;
131           struct ataraid_disk_info *adi;
132           static struct _arrayno {
133             uint64_t magic1;
134             uint64_t magic2;
135             struct _arrayno *next;
136           } arrayno = { 0, 0, NULL}, *anptr;
137 
138           info = kmem_zalloc(sizeof(*info), KM_SLEEP);
139 
140           bmajor = devsw_name2blk(dksc->sc_xname, NULL, 0);
141 
142           /* Get a vnode for the raw partition of this disk. */
143           dev = MAKEDISKDEV(bmajor, device_unit(dksc->sc_dev), RAW_PART);
144           error = bdevvp(dev, &vp);
145           if (error)
146                     goto out;
147 
148           vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
149           error = VOP_OPEN(vp, FREAD, NOCRED);
150           if (error) {
151                     vput(vp);
152                     goto out;
153           }
154 
155           error = ata_raid_config_block_rw(vp, NVIDIA_LBA(sc), info,
156               sizeof(*info), B_READ);
157           VOP_CLOSE(vp, FREAD, NOCRED);
158           vput(vp);
159           if (error) {
160                     aprint_error_dev(dksc->sc_dev,
161                         "error %d reading nVidia MediaShield config block\n", error);
162                     goto out;
163           }
164 
165 #ifdef ATA_RAID_DEBUG
166           ata_raid_nvidia_print_info(info);
167 #endif
168 
169           /* Check the signature. */
170           if (strncmp(info->nvidia_id, NV_MAGIC, strlen(NV_MAGIC)) != 0) {
171                     DPRINTF(("%s: nVidia signature check failed\n",
172                         dksc->sc_xname));
173                     error = ESRCH;
174                     goto out;
175           }
176 
177           /* check if the checksum is OK */
178           for (cksum = 0, ckptr = (uint32_t *)info, count = 0; count < info->config_size;
179                count++)
180                     cksum += *ckptr++;
181           if (cksum) {
182                     DPRINTF(("%s: nVidia checksum failed (0x%02x)\n",
183                                dksc->sc_xname, cksum));
184                     error = ESRCH;
185                     goto out;
186           }
187 
188           /*
189            * Lookup or allocate a new array info structure for
190            * this array. Since nVidia raid information does not
191            * provides array# directory, we must count the number.
192            * The available traces are magic_1 and magic_2.
193            */
194           for (anptr = &arrayno, count = 0; anptr->next; anptr = anptr->next, count++) {
195                     if (anptr->magic1 == info->magic_1 &&
196                         anptr->magic2 == info->magic_2)
197               break;
198           }
199           if (anptr->next == NULL) {
200                     /* new array */
201                     anptr->magic1 = info->magic_1;
202                     anptr->magic2 = info->magic_2;
203                     anptr->next = kmem_zalloc(sizeof(arrayno), KM_SLEEP);
204           }
205           aai = ata_raid_get_array_info(ATA_RAID_TYPE_NVIDIA, count);
206 
207           aai->aai_status = AAI_S_READY;
208           if (info->status & NV_S_DEGRADED)
209                     aai->aai_status |= AAI_S_DEGRADED;
210 
211           switch (info->type) {
212           case NV_T_RAID0:
213                     aai->aai_level = AAI_L_RAID0;
214                     break;
215 
216           case NV_T_RAID1:
217                     aai->aai_level = AAI_L_RAID1;
218                     break;
219 
220           case NV_T_RAID5:
221                     aai->aai_level = AAI_L_RAID5;
222                     break;
223 
224           case NV_T_RAID01:
225                     aai->aai_level = AAI_L_RAID0 | AAI_L_RAID1;
226                     break;
227 
228           case NV_T_SPAN:
229                     aai->aai_level = AAI_L_SPAN;
230                     break;
231 
232           default:
233                     aprint_error_dev(dksc->sc_dev,
234                                "unknown nVidia type 0x%02x\n", info->type);
235                     error = EINVAL;
236                     goto out;
237           }
238 
239           aai->aai_type = ATA_RAID_TYPE_NVIDIA;
240           aai->aai_interleave = info->stripe_sectors;
241           aai->aai_width = info->array_width;
242 
243           aai->aai_ndisks = info->total_disks;
244           aai->aai_capacity = info->total_sectors;
245           aai->aai_heads = 255;
246           aai->aai_sectors = 63;
247           aai->aai_cylinders = aai->aai_capacity / (aai->aai_heads * aai->aai_sectors);
248           aai->aai_offset = 0;
249           aai->aai_reserved = 2;
250 
251           adi = &aai->aai_disks[info->disk_number];
252           adi->adi_dev = dksc->sc_dev;
253           adi->adi_status = ADI_S_ONLINE | ADI_S_ASSIGNED;
254           adi->adi_sectors = aai->aai_capacity;
255           adi->adi_compsize = aai->aai_capacity / info->array_width;
256 
257           error = 0;
258 
259  out:
260           kmem_free(info, sizeof(*info));
261           return (error);
262 }
263