Remove bogus checks after kmalloc(M_WAITOK) which never returns NULL.
[dragonfly.git] / sys / dev / disk / nata / ata-raid.c
blob02107f9add58f6f53946d1c024beb9eddd60a39b
1 /*-
2 * Copyright (c) 2000 - 2006 Søren Schmidt <sos@FreeBSD.org>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: src/sys/dev/ata/ata-raid.c,v 1.120 2006/04/15 10:27:41 maxim Exp $
27 * $DragonFly: src/sys/dev/disk/nata/ata-raid.c,v 1.8 2008/01/06 16:55:49 swildner Exp $
30 #include "opt_ata.h"
32 #include <sys/param.h>
33 #include <sys/bio.h>
34 #include <sys/buf.h>
35 #include <sys/buf2.h>
36 #include <sys/bus.h>
37 #include <sys/conf.h>
38 #include <sys/device.h>
39 #include <sys/disk.h>
40 #include <sys/endian.h>
41 #include <sys/libkern.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/nata.h>
45 #include <sys/spinlock2.h>
46 #include <sys/systm.h>
48 #include <vm/pmap.h>
50 #include <machine/md_var.h>
52 #include <bus/pci/pcivar.h>
54 #include "ata-all.h"
55 #include "ata-disk.h"
56 #include "ata-raid.h"
57 #include "ata-pci.h"
58 #include "ata_if.h"
61 /* device structure */
62 static d_strategy_t ata_raid_strategy;
63 static d_dump_t ata_raid_dump;
64 static struct dev_ops ar_ops = {
65 { "ar", 157, D_DISK },
66 .d_open = nullopen,
67 .d_close = nullclose,
68 .d_read = physread,
69 .d_write = physwrite,
70 .d_strategy = ata_raid_strategy,
71 .d_dump = ata_raid_dump,
74 /* prototypes */
75 static void ata_raid_done(struct ata_request *request);
76 static void ata_raid_config_changed(struct ar_softc *rdp, int writeback);
77 static int ata_raid_status(struct ata_ioc_raid_config *config);
78 static int ata_raid_create(struct ata_ioc_raid_config *config);
79 static int ata_raid_delete(int array);
80 static int ata_raid_addspare(struct ata_ioc_raid_config *config);
81 static int ata_raid_rebuild(int array);
82 static int ata_raid_read_metadata(device_t subdisk);
83 static int ata_raid_write_metadata(struct ar_softc *rdp);
84 static int ata_raid_wipe_metadata(struct ar_softc *rdp);
85 static int ata_raid_adaptec_read_meta(device_t dev, struct ar_softc **raidp);
86 static int ata_raid_hptv2_read_meta(device_t dev, struct ar_softc **raidp);
87 static int ata_raid_hptv2_write_meta(struct ar_softc *rdp);
88 static int ata_raid_hptv3_read_meta(device_t dev, struct ar_softc **raidp);
89 static int ata_raid_intel_read_meta(device_t dev, struct ar_softc **raidp);
90 static int ata_raid_intel_write_meta(struct ar_softc *rdp);
91 static int ata_raid_ite_read_meta(device_t dev, struct ar_softc **raidp);
92 static int ata_raid_jmicron_read_meta(device_t dev, struct ar_softc **raidp);
93 static int ata_raid_jmicron_write_meta(struct ar_softc *rdp);
94 static int ata_raid_lsiv2_read_meta(device_t dev, struct ar_softc **raidp);
95 static int ata_raid_lsiv3_read_meta(device_t dev, struct ar_softc **raidp);
96 static int ata_raid_nvidia_read_meta(device_t dev, struct ar_softc **raidp);
97 static int ata_raid_promise_read_meta(device_t dev, struct ar_softc **raidp, int native);
98 static int ata_raid_promise_write_meta(struct ar_softc *rdp);
99 static int ata_raid_sii_read_meta(device_t dev, struct ar_softc **raidp);
100 static int ata_raid_sis_read_meta(device_t dev, struct ar_softc **raidp);
101 static int ata_raid_sis_write_meta(struct ar_softc *rdp);
102 static int ata_raid_via_read_meta(device_t dev, struct ar_softc **raidp);
103 static int ata_raid_via_write_meta(struct ar_softc *rdp);
104 static struct ata_request *ata_raid_init_request(struct ar_softc *rdp, struct bio *bio);
105 static int ata_raid_send_request(struct ata_request *request);
106 static int ata_raid_rw(device_t dev, u_int64_t lba, void *data, u_int bcount, int flags);
107 static char * ata_raid_format(struct ar_softc *rdp);
108 static char * ata_raid_type(struct ar_softc *rdp);
109 static char * ata_raid_flags(struct ar_softc *rdp);
111 /* debugging only */
112 static void ata_raid_print_meta(struct ar_softc *meta);
113 static void ata_raid_adaptec_print_meta(struct adaptec_raid_conf *meta);
114 static void ata_raid_hptv2_print_meta(struct hptv2_raid_conf *meta);
115 static void ata_raid_hptv3_print_meta(struct hptv3_raid_conf *meta);
116 static void ata_raid_intel_print_meta(struct intel_raid_conf *meta);
117 static void ata_raid_ite_print_meta(struct ite_raid_conf *meta);
118 static void ata_raid_jmicron_print_meta(struct jmicron_raid_conf *meta);
119 static void ata_raid_lsiv2_print_meta(struct lsiv2_raid_conf *meta);
120 static void ata_raid_lsiv3_print_meta(struct lsiv3_raid_conf *meta);
121 static void ata_raid_nvidia_print_meta(struct nvidia_raid_conf *meta);
122 static void ata_raid_promise_print_meta(struct promise_raid_conf *meta);
123 static void ata_raid_sii_print_meta(struct sii_raid_conf *meta);
124 static void ata_raid_sis_print_meta(struct sis_raid_conf *meta);
125 static void ata_raid_via_print_meta(struct via_raid_conf *meta);
127 /* internal vars */
128 static struct ar_softc *ata_raid_arrays[MAX_ARRAYS];
129 static MALLOC_DEFINE(M_AR, "ar_driver", "ATA PseudoRAID driver");
130 static devclass_t ata_raid_sub_devclass;
131 static int testing = 0;
133 static void
134 ata_raid_attach(struct ar_softc *rdp, int writeback)
136 struct disk_info info;
137 cdev_t cdev;
138 char buffer[32];
139 int disk;
141 spin_init(&rdp->lock);
142 ata_raid_config_changed(rdp, writeback);
144 /* sanitize arrays total_size % (width * interleave) == 0 */
145 if (rdp->type == AR_T_RAID0 || rdp->type == AR_T_RAID01 ||
146 rdp->type == AR_T_RAID5) {
147 rdp->total_sectors = (rdp->total_sectors/(rdp->interleave*rdp->width))*
148 (rdp->interleave * rdp->width);
149 ksprintf(buffer, " (stripe %d KB)",
150 (rdp->interleave * DEV_BSIZE) / 1024);
152 else
153 buffer[0] = '\0';
154 /* XXX TGEN add devstats? */
155 cdev = disk_create(rdp->lun, &rdp->disk, &ar_ops);
156 cdev->si_drv1 = rdp;
157 cdev->si_iosize_max = 128 * DEV_BSIZE;
158 rdp->cdev = cdev;
160 bzero(&info, sizeof(info));
161 info.d_media_blksize = DEV_BSIZE; /* mandatory */
162 info.d_media_blocks = rdp->total_sectors;
164 info.d_secpertrack = rdp->sectors; /* optional */
165 info.d_nheads = rdp->heads;
166 info.d_ncylinders = rdp->total_sectors/(rdp->heads*rdp->sectors);
167 info.d_secpercyl = rdp->sectors * rdp->heads;
169 kprintf("ar%d: %juMB <%s %s%s> status: %s\n", rdp->lun,
170 rdp->total_sectors / ((1024L * 1024L) / DEV_BSIZE),
171 ata_raid_format(rdp), ata_raid_type(rdp),
172 buffer, ata_raid_flags(rdp));
174 if (testing || bootverbose)
175 kprintf("ar%d: %ju sectors [%dC/%dH/%dS] <%s> subdisks defined as:\n",
176 rdp->lun, rdp->total_sectors,
177 rdp->cylinders, rdp->heads, rdp->sectors, rdp->name);
179 for (disk = 0; disk < rdp->total_disks; disk++) {
180 kprintf("ar%d: disk%d ", rdp->lun, disk);
181 if (rdp->disks[disk].dev) {
182 if (rdp->disks[disk].flags & AR_DF_PRESENT) {
183 /* status of this disk in the array */
184 if (rdp->disks[disk].flags & AR_DF_ONLINE)
185 kprintf("READY ");
186 else if (rdp->disks[disk].flags & AR_DF_SPARE)
187 kprintf("SPARE ");
188 else
189 kprintf("FREE ");
191 /* what type of disk is this in the array */
192 switch (rdp->type) {
193 case AR_T_RAID1:
194 case AR_T_RAID01:
195 if (disk < rdp->width)
196 kprintf("(master) ");
197 else
198 kprintf("(mirror) ");
201 /* which physical disk is used */
202 kprintf("using %s at ata%d-%s\n",
203 device_get_nameunit(rdp->disks[disk].dev),
204 device_get_unit(device_get_parent(rdp->disks[disk].dev)),
205 (((struct ata_device *)
206 device_get_softc(rdp->disks[disk].dev))->unit ==
207 ATA_MASTER) ? "master" : "slave");
209 else if (rdp->disks[disk].flags & AR_DF_ASSIGNED)
210 kprintf("DOWN\n");
211 else
212 kprintf("INVALID no RAID config on this subdisk\n");
214 else
215 kprintf("DOWN no device found for this subdisk\n");
218 disk_setdiskinfo(&rdp->disk, &info);
222 * ATA PseudoRAID ioctl function. Note that this does not need to be adjusted
223 * to the dev_ops way, because it's just chained from the generic ata ioctl.
225 static int
226 ata_raid_ioctl(u_long cmd, caddr_t data)
228 struct ata_ioc_raid_config *config = (struct ata_ioc_raid_config *)data;
229 int *lun = (int *)data;
230 int error = EOPNOTSUPP;
232 switch (cmd) {
233 case IOCATARAIDSTATUS:
234 error = ata_raid_status(config);
235 break;
237 case IOCATARAIDCREATE:
238 error = ata_raid_create(config);
239 break;
241 case IOCATARAIDDELETE:
242 error = ata_raid_delete(*lun);
243 break;
245 case IOCATARAIDADDSPARE:
246 error = ata_raid_addspare(config);
247 break;
249 case IOCATARAIDREBUILD:
250 error = ata_raid_rebuild(*lun);
251 break;
253 return error;
257 * XXX TGEN there are a lot of offset -> block number conversions going on
258 * here, which is suboptimal.
260 static int
261 ata_raid_strategy(struct dev_strategy_args *ap)
263 struct ar_softc *rdp = ap->a_head.a_dev->si_drv1;
264 struct bio *bp = ap->a_bio;
265 struct buf *bbp = bp->bio_buf;
266 struct ata_request *request;
267 caddr_t data;
268 u_int64_t blkno, lba, blk = 0;
269 int count, chunk, drv, par = 0, change = 0;
271 if (!(rdp->status & AR_S_READY) ||
272 (bbp->b_cmd != BUF_CMD_READ && bbp->b_cmd != BUF_CMD_WRITE)) {
273 bbp->b_flags |= B_ERROR;
274 bbp->b_error = EIO;
275 biodone(bp);
276 return(0);
279 bbp->b_resid = bbp->b_bcount;
280 for (count = howmany(bbp->b_bcount, DEV_BSIZE),
281 /* bio_offset is byte granularity, convert */
282 blkno = (u_int64_t)(bp->bio_offset >> DEV_BSHIFT),
283 data = bbp->b_data;
284 count > 0;
285 count -= chunk, blkno += chunk, data += (chunk * DEV_BSIZE)) {
287 switch (rdp->type) {
288 case AR_T_RAID1:
289 drv = 0;
290 lba = blkno;
291 chunk = count;
292 break;
294 case AR_T_JBOD:
295 case AR_T_SPAN:
296 drv = 0;
297 lba = blkno;
298 while (lba >= rdp->disks[drv].sectors)
299 lba -= rdp->disks[drv++].sectors;
300 chunk = min(rdp->disks[drv].sectors - lba, count);
301 break;
303 case AR_T_RAID0:
304 case AR_T_RAID01:
305 chunk = blkno % rdp->interleave;
306 drv = (blkno / rdp->interleave) % rdp->width;
307 lba = (((blkno/rdp->interleave)/rdp->width)*rdp->interleave)+chunk;
308 chunk = min(count, rdp->interleave - chunk);
309 break;
311 case AR_T_RAID5:
312 drv = (blkno / rdp->interleave) % (rdp->width - 1);
313 par = rdp->width - 1 -
314 (blkno / (rdp->interleave * (rdp->width - 1))) % rdp->width;
315 if (drv >= par)
316 drv++;
317 lba = ((blkno/rdp->interleave)/(rdp->width-1))*(rdp->interleave) +
318 ((blkno%(rdp->interleave*(rdp->width-1)))%rdp->interleave);
319 chunk = min(count, rdp->interleave - (lba % rdp->interleave));
320 break;
322 default:
323 kprintf("ar%d: unknown array type in ata_raid_strategy\n", rdp->lun);
324 bbp->b_flags |= B_ERROR;
325 bbp->b_error = EIO;
326 biodone(bp);
327 return(0);
330 /* offset on all but "first on HPTv2" */
331 if (!(drv == 0 && rdp->format == AR_F_HPTV2_RAID))
332 lba += rdp->offset_sectors;
334 if (!(request = ata_raid_init_request(rdp, bp))) {
335 bbp->b_flags |= B_ERROR;
336 bbp->b_error = EIO;
337 biodone(bp);
338 return(0);
340 request->data = data;
341 request->bytecount = chunk * DEV_BSIZE;
342 request->u.ata.lba = lba;
343 request->u.ata.count = request->bytecount / DEV_BSIZE;
345 switch (rdp->type) {
346 case AR_T_JBOD:
347 case AR_T_SPAN:
348 case AR_T_RAID0:
349 if (((rdp->disks[drv].flags & (AR_DF_PRESENT|AR_DF_ONLINE)) ==
350 (AR_DF_PRESENT|AR_DF_ONLINE) && !rdp->disks[drv].dev)) {
351 rdp->disks[drv].flags &= ~AR_DF_ONLINE;
352 ata_raid_config_changed(rdp, 1);
353 ata_free_request(request);
354 bbp->b_flags |= B_ERROR;
355 bbp->b_error = EIO;
356 biodone(bp);
357 return(0);
359 request->this = drv;
360 request->dev = rdp->disks[request->this].dev;
361 ata_raid_send_request(request);
362 break;
364 case AR_T_RAID1:
365 case AR_T_RAID01:
366 if ((rdp->disks[drv].flags &
367 (AR_DF_PRESENT|AR_DF_ONLINE))==(AR_DF_PRESENT|AR_DF_ONLINE) &&
368 !rdp->disks[drv].dev) {
369 rdp->disks[drv].flags &= ~AR_DF_ONLINE;
370 change = 1;
372 if ((rdp->disks[drv + rdp->width].flags &
373 (AR_DF_PRESENT|AR_DF_ONLINE))==(AR_DF_PRESENT|AR_DF_ONLINE) &&
374 !rdp->disks[drv + rdp->width].dev) {
375 rdp->disks[drv + rdp->width].flags &= ~AR_DF_ONLINE;
376 change = 1;
378 if (change)
379 ata_raid_config_changed(rdp, 1);
380 if (!(rdp->status & AR_S_READY)) {
381 ata_free_request(request);
382 bbp->b_flags |= B_ERROR;
383 bbp->b_error = EIO;
384 biodone(bp);
385 return(0);
388 if (rdp->status & AR_S_REBUILDING)
389 blk = ((lba / rdp->interleave) * rdp->width) * rdp->interleave +
390 (rdp->interleave * (drv % rdp->width)) +
391 lba % rdp->interleave;;
393 if (bbp->b_cmd == BUF_CMD_READ) {
394 int src_online =
395 (rdp->disks[drv].flags & AR_DF_ONLINE);
396 int mir_online =
397 (rdp->disks[drv+rdp->width].flags & AR_DF_ONLINE);
399 /* if mirror gone or close to last access on source */
400 if (!mir_online ||
401 ((src_online) &&
402 ((u_int64_t)(bp->bio_offset >> DEV_BSHIFT)) >=
403 (rdp->disks[drv].last_lba - AR_PROXIMITY) &&
404 ((u_int64_t)(bp->bio_offset >> DEV_BSHIFT)) <=
405 (rdp->disks[drv].last_lba + AR_PROXIMITY))) {
406 rdp->toggle = 0;
408 /* if source gone or close to last access on mirror */
409 else if (!src_online ||
410 ((mir_online) &&
411 ((u_int64_t)(bp->bio_offset >> DEV_BSHIFT)) >=
412 (rdp->disks[drv+rdp->width].last_lba-AR_PROXIMITY) &&
413 ((u_int64_t)(bp->bio_offset >> DEV_BSHIFT)) <=
414 (rdp->disks[drv+rdp->width].last_lba+AR_PROXIMITY))) {
415 drv += rdp->width;
416 rdp->toggle = 1;
418 /* not close to any previous access, toggle */
419 else {
420 if (rdp->toggle)
421 rdp->toggle = 0;
422 else {
423 drv += rdp->width;
424 rdp->toggle = 1;
428 if ((rdp->status & AR_S_REBUILDING) &&
429 (blk <= rdp->rebuild_lba) &&
430 ((blk + chunk) > rdp->rebuild_lba)) {
431 struct ata_composite *composite;
432 struct ata_request *rebuild;
433 int this;
435 /* figure out what part to rebuild */
436 if (drv < rdp->width)
437 this = drv + rdp->width;
438 else
439 this = drv - rdp->width;
441 /* do we have a spare to rebuild on ? */
442 if (rdp->disks[this].flags & AR_DF_SPARE) {
443 if ((composite = ata_alloc_composite())) {
444 if ((rebuild = ata_alloc_request())) {
445 rdp->rebuild_lba = blk + chunk;
446 bcopy(request, rebuild,
447 sizeof(struct ata_request));
448 rebuild->this = this;
449 rebuild->dev = rdp->disks[this].dev;
450 rebuild->flags &= ~ATA_R_READ;
451 rebuild->flags |= ATA_R_WRITE;
452 spin_init(&composite->lock);
453 composite->residual = request->bytecount;
454 composite->rd_needed |= (1 << drv);
455 composite->wr_depend |= (1 << drv);
456 composite->wr_needed |= (1 << this);
457 composite->request[drv] = request;
458 composite->request[this] = rebuild;
459 request->composite = composite;
460 rebuild->composite = composite;
461 ata_raid_send_request(rebuild);
463 else {
464 ata_free_composite(composite);
465 kprintf("DOH! ata_alloc_request failed!\n");
468 else {
469 kprintf("DOH! ata_alloc_composite failed!\n");
472 else if (rdp->disks[this].flags & AR_DF_ONLINE) {
474 * if we got here we are a chunk of a RAID01 that
475 * does not need a rebuild, but we need to increment
476 * the rebuild_lba address to get the rebuild to
477 * move to the next chunk correctly
479 rdp->rebuild_lba = blk + chunk;
481 else
482 kprintf("DOH! we didn't find the rebuild part\n");
485 if (bbp->b_cmd == BUF_CMD_WRITE) {
486 if ((rdp->disks[drv+rdp->width].flags & AR_DF_ONLINE) ||
487 ((rdp->status & AR_S_REBUILDING) &&
488 (rdp->disks[drv+rdp->width].flags & AR_DF_SPARE) &&
489 ((blk < rdp->rebuild_lba) ||
490 ((blk <= rdp->rebuild_lba) &&
491 ((blk + chunk) > rdp->rebuild_lba))))) {
492 if ((rdp->disks[drv].flags & AR_DF_ONLINE) ||
493 ((rdp->status & AR_S_REBUILDING) &&
494 (rdp->disks[drv].flags & AR_DF_SPARE) &&
495 ((blk < rdp->rebuild_lba) ||
496 ((blk <= rdp->rebuild_lba) &&
497 ((blk + chunk) > rdp->rebuild_lba))))) {
498 struct ata_request *mirror;
499 struct ata_composite *composite;
500 int this = drv + rdp->width;
502 if ((composite = ata_alloc_composite())) {
503 if ((mirror = ata_alloc_request())) {
504 if ((blk <= rdp->rebuild_lba) &&
505 ((blk + chunk) > rdp->rebuild_lba))
506 rdp->rebuild_lba = blk + chunk;
507 bcopy(request, mirror,
508 sizeof(struct ata_request));
509 mirror->this = this;
510 mirror->dev = rdp->disks[this].dev;
511 spin_init(&composite->lock);
512 composite->residual = request->bytecount;
513 composite->wr_needed |= (1 << drv);
514 composite->wr_needed |= (1 << this);
515 composite->request[drv] = request;
516 composite->request[this] = mirror;
517 request->composite = composite;
518 mirror->composite = composite;
519 ata_raid_send_request(mirror);
520 rdp->disks[this].last_lba =
521 (u_int64_t)(bp->bio_offset >> DEV_BSHIFT) +
522 chunk;
524 else {
525 ata_free_composite(composite);
526 kprintf("DOH! ata_alloc_request failed!\n");
529 else {
530 kprintf("DOH! ata_alloc_composite failed!\n");
533 else
534 drv += rdp->width;
537 request->this = drv;
538 request->dev = rdp->disks[request->this].dev;
539 ata_raid_send_request(request);
540 rdp->disks[request->this].last_lba =
541 ((u_int64_t)(bp->bio_offset) >> DEV_BSHIFT) + chunk;
542 break;
544 case AR_T_RAID5:
545 if (((rdp->disks[drv].flags & (AR_DF_PRESENT|AR_DF_ONLINE)) ==
546 (AR_DF_PRESENT|AR_DF_ONLINE) && !rdp->disks[drv].dev)) {
547 rdp->disks[drv].flags &= ~AR_DF_ONLINE;
548 change = 1;
550 if (((rdp->disks[par].flags & (AR_DF_PRESENT|AR_DF_ONLINE)) ==
551 (AR_DF_PRESENT|AR_DF_ONLINE) && !rdp->disks[par].dev)) {
552 rdp->disks[par].flags &= ~AR_DF_ONLINE;
553 change = 1;
555 if (change)
556 ata_raid_config_changed(rdp, 1);
557 if (!(rdp->status & AR_S_READY)) {
558 ata_free_request(request);
559 bbp->b_flags |= B_ERROR;
560 bbp->b_error = EIO;
561 biodone(bp);
562 return(0);
564 if (rdp->status & AR_S_DEGRADED) {
565 /* do the XOR game if possible */
567 else {
568 request->this = drv;
569 request->dev = rdp->disks[request->this].dev;
570 if (bbp->b_cmd == BUF_CMD_READ) {
571 ata_raid_send_request(request);
573 if (bbp->b_cmd == BUF_CMD_WRITE) {
574 ata_raid_send_request(request);
575 /* XXX TGEN no, I don't speak Danish either */
577 * sikre at læs-modify-skriv til hver disk er atomarisk.
578 * par kopi af request
579 * læse orgdata fra drv
580 * skriv nydata til drv
581 * læse parorgdata fra par
582 * skriv orgdata xor parorgdata xor nydata til par
586 break;
588 default:
589 kprintf("ar%d: unknown array type in ata_raid_strategy\n", rdp->lun);
593 return(0);
596 static void
597 ata_raid_done(struct ata_request *request)
599 struct ar_softc *rdp = request->driver;
600 struct ata_composite *composite = NULL;
601 struct bio *bp = request->bio;
602 struct buf *bbp = bp->bio_buf;
603 int i, mirror, finished = 0;
605 switch (rdp->type) {
606 case AR_T_JBOD:
607 case AR_T_SPAN:
608 case AR_T_RAID0:
609 if (request->result) {
610 rdp->disks[request->this].flags &= ~AR_DF_ONLINE;
611 ata_raid_config_changed(rdp, 1);
612 bbp->b_error = request->result;
613 finished = 1;
615 else {
616 bbp->b_resid -= request->donecount;
617 if (!bbp->b_resid)
618 finished = 1;
620 break;
622 case AR_T_RAID1:
623 case AR_T_RAID01:
624 if (request->this < rdp->width)
625 mirror = request->this + rdp->width;
626 else
627 mirror = request->this - rdp->width;
628 if (request->result) {
629 rdp->disks[request->this].flags &= ~AR_DF_ONLINE;
630 ata_raid_config_changed(rdp, 1);
632 if (rdp->status & AR_S_READY) {
633 u_int64_t blk = 0;
635 if (rdp->status & AR_S_REBUILDING)
636 blk = ((request->u.ata.lba / rdp->interleave) * rdp->width) *
637 rdp->interleave + (rdp->interleave *
638 (request->this % rdp->width)) +
639 request->u.ata.lba % rdp->interleave;
641 if (bbp->b_cmd == BUF_CMD_READ) {
643 /* is this a rebuild composite */
644 if ((composite = request->composite)) {
645 spin_lock_wr(&composite->lock);
647 /* handle the read part of a rebuild composite */
648 if (request->flags & ATA_R_READ) {
650 /* if read failed array is now broken */
651 if (request->result) {
652 rdp->disks[request->this].flags &= ~AR_DF_ONLINE;
653 ata_raid_config_changed(rdp, 1);
654 bbp->b_error = request->result;
655 rdp->rebuild_lba = blk;
656 finished = 1;
659 /* good data, update how far we've gotten */
660 else {
661 bbp->b_resid -= request->donecount;
662 composite->residual -= request->donecount;
663 if (!composite->residual) {
664 if (composite->wr_done & (1 << mirror))
665 finished = 1;
670 /* handle the write part of a rebuild composite */
671 else if (request->flags & ATA_R_WRITE) {
672 if (composite->rd_done & (1 << mirror)) {
673 if (request->result) {
674 kprintf("DOH! rebuild failed\n"); /* XXX SOS */
675 rdp->rebuild_lba = blk;
677 if (!composite->residual)
678 finished = 1;
681 spin_unlock_wr(&composite->lock);
684 /* if read failed retry on the mirror */
685 else if (request->result) {
686 request->dev = rdp->disks[mirror].dev;
687 request->flags &= ~ATA_R_TIMEOUT;
688 ata_raid_send_request(request);
689 return;
692 /* we have good data */
693 else {
694 bbp->b_resid -= request->donecount;
695 if (!bbp->b_resid)
696 finished = 1;
699 else if (bbp->b_cmd == BUF_CMD_WRITE) {
700 /* do we have a mirror or rebuild to deal with ? */
701 if ((composite = request->composite)) {
702 spin_lock_wr(&composite->lock);
703 if (composite->wr_done & (1 << mirror)) {
704 if (request->result) {
705 if (composite->request[mirror]->result) {
706 kprintf("DOH! all disks failed and got here\n");
707 bbp->b_error = EIO;
709 if (rdp->status & AR_S_REBUILDING) {
710 rdp->rebuild_lba = blk;
711 kprintf("DOH! rebuild failed\n"); /* XXX SOS */
713 bbp->b_resid -=
714 composite->request[mirror]->donecount;
715 composite->residual -=
716 composite->request[mirror]->donecount;
718 else {
719 bbp->b_resid -= request->donecount;
720 composite->residual -= request->donecount;
722 if (!composite->residual)
723 finished = 1;
725 spin_unlock_wr(&composite->lock);
727 /* no mirror we are done */
728 else {
729 bbp->b_resid -= request->donecount;
730 if (!bbp->b_resid)
731 finished = 1;
735 else {
736 /* XXX TGEN bbp->b_flags |= B_ERROR; */
737 bbp->b_error = request->result;
738 biodone(bp);
740 break;
742 case AR_T_RAID5:
743 if (request->result) {
744 rdp->disks[request->this].flags &= ~AR_DF_ONLINE;
745 ata_raid_config_changed(rdp, 1);
746 if (rdp->status & AR_S_READY) {
747 if (bbp->b_cmd == BUF_CMD_READ) {
748 /* do the XOR game to recover data */
750 if (bbp->b_cmd == BUF_CMD_WRITE) {
751 /* if the parity failed we're OK sortof */
752 /* otherwise wee need to do the XOR long dance */
754 finished = 1;
756 else {
757 /* XXX TGEN bbp->b_flags |= B_ERROR; */
758 bbp->b_error = request->result;
759 biodone(bp);
762 else {
763 /* did we have an XOR game going ?? */
764 bbp->b_resid -= request->donecount;
765 if (!bbp->b_resid)
766 finished = 1;
768 break;
770 default:
771 kprintf("ar%d: unknown array type in ata_raid_done\n", rdp->lun);
774 if (finished) {
775 if ((rdp->status & AR_S_REBUILDING) &&
776 rdp->rebuild_lba >= rdp->total_sectors) {
777 int disk;
779 for (disk = 0; disk < rdp->total_disks; disk++) {
780 if ((rdp->disks[disk].flags &
781 (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_SPARE)) ==
782 (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_SPARE)) {
783 rdp->disks[disk].flags &= ~AR_DF_SPARE;
784 rdp->disks[disk].flags |= AR_DF_ONLINE;
787 rdp->status &= ~AR_S_REBUILDING;
788 ata_raid_config_changed(rdp, 1);
790 if (!bbp->b_resid)
791 biodone(bp);
794 if (composite) {
795 if (finished) {
796 /* we are done with this composite, free all resources */
797 for (i = 0; i < 32; i++) {
798 if (composite->rd_needed & (1 << i) ||
799 composite->wr_needed & (1 << i)) {
800 ata_free_request(composite->request[i]);
803 spin_uninit(&composite->lock);
804 ata_free_composite(composite);
807 else
808 ata_free_request(request);
811 static int
812 ata_raid_dump(struct dev_dump_args *ap)
814 struct ar_softc *rdp = ap->a_head.a_dev->si_drv1;
815 struct buf dbuf;
816 vm_paddr_t addr = 0;
817 long blkcnt;
818 int dumppages = MAXDUMPPGS;
819 int error = 0;
820 int i, disk;
822 blkcnt = howmany(PAGE_SIZE, ap->a_secsize);
824 while (ap->a_count > 0) {
825 caddr_t va = NULL;
827 if ((ap->a_count / blkcnt) < dumppages)
828 dumppages = ap->a_count / blkcnt;
830 for (i = 0; i < dumppages; ++i) {
831 vm_paddr_t a = addr + (i * PAGE_SIZE);
832 if (is_physical_memory(a))
833 va = pmap_kenter_temporary(trunc_page(a), i);
834 else
835 va = pmap_kenter_temporary(trunc_page(0), i);
838 bzero(&dbuf, sizeof(struct buf));
839 BUF_LOCKINIT(&dbuf);
840 BUF_LOCK(&dbuf, LK_EXCLUSIVE);
841 initbufbio(&dbuf);
842 /* bio_offset is byte granularity, convert block granularity a_blkno */
843 dbuf.b_bio1.bio_offset = (off_t)(ap->a_blkno << DEV_BSHIFT);
844 dbuf.b_bio1.bio_caller_info1.ptr = (void *)rdp;
845 dbuf.b_bcount = dumppages * PAGE_SIZE;
846 dbuf.b_data = va;
847 dbuf.b_cmd = BUF_CMD_WRITE;
848 dev_dstrategy(rdp->cdev, &dbuf.b_bio1);
849 /* wait for completion, unlock the buffer, check status */
850 if (biowait(&dbuf)) {
851 BUF_UNLOCK(&dbuf);
852 return(dbuf.b_error ? dbuf.b_error : EIO);
854 BUF_UNLOCK(&dbuf);
856 if (dumpstatus(addr, (off_t)ap->a_count * DEV_BSIZE) < 0)
857 return(EINTR);
859 ap->a_blkno += blkcnt * dumppages;
860 ap->a_count -= blkcnt * dumppages;
861 addr += PAGE_SIZE * dumppages;
864 /* flush subdisk buffers to media */
865 for (disk = 0; disk < rdp->total_disks; disk++)
866 if (rdp->disks[disk].dev)
867 error |= ata_controlcmd(rdp->disks[disk].dev, ATA_FLUSHCACHE, 0, 0,
869 return (error ? EIO : 0);
872 static void
873 ata_raid_config_changed(struct ar_softc *rdp, int writeback)
875 int disk, count, status;
877 spin_lock_wr(&rdp->lock);
878 /* set default all working mode */
879 status = rdp->status;
880 rdp->status &= ~AR_S_DEGRADED;
881 rdp->status |= AR_S_READY;
883 /* make sure all lost drives are accounted for */
884 for (disk = 0; disk < rdp->total_disks; disk++) {
885 if (!(rdp->disks[disk].flags & AR_DF_PRESENT))
886 rdp->disks[disk].flags &= ~AR_DF_ONLINE;
889 /* depending on RAID type figure out our health status */
890 switch (rdp->type) {
891 case AR_T_JBOD:
892 case AR_T_SPAN:
893 case AR_T_RAID0:
894 for (disk = 0; disk < rdp->total_disks; disk++)
895 if (!(rdp->disks[disk].flags & AR_DF_ONLINE))
896 rdp->status &= ~AR_S_READY;
897 break;
899 case AR_T_RAID1:
900 case AR_T_RAID01:
901 for (disk = 0; disk < rdp->width; disk++) {
902 if (!(rdp->disks[disk].flags & AR_DF_ONLINE) &&
903 !(rdp->disks[disk + rdp->width].flags & AR_DF_ONLINE)) {
904 rdp->status &= ~AR_S_READY;
906 else if (((rdp->disks[disk].flags & AR_DF_ONLINE) &&
907 !(rdp->disks[disk + rdp->width].flags & AR_DF_ONLINE)) ||
908 (!(rdp->disks[disk].flags & AR_DF_ONLINE) &&
909 (rdp->disks [disk + rdp->width].flags & AR_DF_ONLINE))) {
910 rdp->status |= AR_S_DEGRADED;
913 break;
915 case AR_T_RAID5:
916 for (count = 0, disk = 0; disk < rdp->total_disks; disk++) {
917 if (!(rdp->disks[disk].flags & AR_DF_ONLINE))
918 count++;
920 if (count) {
921 if (count > 1)
922 rdp->status &= ~AR_S_READY;
923 else
924 rdp->status |= AR_S_DEGRADED;
926 break;
927 default:
928 rdp->status &= ~AR_S_READY;
931 if (rdp->status != status) {
932 if (!(rdp->status & AR_S_READY)) {
933 kprintf("ar%d: FAILURE - %s array broken\n",
934 rdp->lun, ata_raid_type(rdp));
936 else if (rdp->status & AR_S_DEGRADED) {
937 if (rdp->type & (AR_T_RAID1 | AR_T_RAID01))
938 kprintf("ar%d: WARNING - mirror", rdp->lun);
939 else
940 kprintf("ar%d: WARNING - parity", rdp->lun);
941 kprintf(" protection lost. %s array in DEGRADED mode\n",
942 ata_raid_type(rdp));
945 spin_unlock_wr(&rdp->lock);
946 if (writeback)
947 ata_raid_write_metadata(rdp);
951 static int
952 ata_raid_status(struct ata_ioc_raid_config *config)
954 struct ar_softc *rdp;
955 int i;
957 if (!(rdp = ata_raid_arrays[config->lun]))
958 return ENXIO;
960 config->type = rdp->type;
961 config->total_disks = rdp->total_disks;
962 for (i = 0; i < rdp->total_disks; i++ ) {
963 if ((rdp->disks[i].flags & AR_DF_PRESENT) && rdp->disks[i].dev)
964 config->disks[i] = device_get_unit(rdp->disks[i].dev);
965 else
966 config->disks[i] = -1;
968 config->interleave = rdp->interleave;
969 config->status = rdp->status;
970 config->progress = 100 * rdp->rebuild_lba / rdp->total_sectors;
971 return 0;
974 static int
975 ata_raid_create(struct ata_ioc_raid_config *config)
977 struct ar_softc *rdp;
978 device_t subdisk;
979 int array, disk;
980 int ctlr = 0, disk_size = 0, total_disks = 0;
982 for (array = 0; array < MAX_ARRAYS; array++) {
983 if (!ata_raid_arrays[array])
984 break;
986 if (array >= MAX_ARRAYS)
987 return ENOSPC;
989 rdp = (struct ar_softc*)kmalloc(sizeof(struct ar_softc), M_AR,
990 M_WAITOK | M_ZERO);
992 for (disk = 0; disk < config->total_disks; disk++) {
993 if ((subdisk = devclass_get_device(ata_raid_sub_devclass,
994 config->disks[disk]))) {
995 struct ata_raid_subdisk *ars = device_get_softc(subdisk);
997 /* is device already assigned to another array ? */
998 if (ars->raid[rdp->volume]) {
999 config->disks[disk] = -1;
1000 kfree(rdp, M_AR);
1001 return EBUSY;
1003 rdp->disks[disk].dev = device_get_parent(subdisk);
1005 switch (pci_get_vendor(GRANDPARENT(rdp->disks[disk].dev))) {
1006 case ATA_HIGHPOINT_ID:
1008 * we need some way to decide if it should be v2 or v3
1009 * for now just use v2 since the v3 BIOS knows how to
1010 * handle that as well.
1012 ctlr = AR_F_HPTV2_RAID;
1013 rdp->disks[disk].sectors = HPTV3_LBA(rdp->disks[disk].dev);
1014 break;
1016 case ATA_INTEL_ID:
1017 ctlr = AR_F_INTEL_RAID;
1018 rdp->disks[disk].sectors = INTEL_LBA(rdp->disks[disk].dev);
1019 break;
1021 case ATA_ITE_ID:
1022 ctlr = AR_F_ITE_RAID;
1023 rdp->disks[disk].sectors = ITE_LBA(rdp->disks[disk].dev);
1024 break;
1026 case ATA_JMICRON_ID:
1027 ctlr = AR_F_JMICRON_RAID;
1028 rdp->disks[disk].sectors = JMICRON_LBA(rdp->disks[disk].dev);
1029 break;
1031 case 0: /* XXX SOS cover up for bug in our PCI code */
1032 case ATA_PROMISE_ID:
1033 ctlr = AR_F_PROMISE_RAID;
1034 rdp->disks[disk].sectors = PROMISE_LBA(rdp->disks[disk].dev);
1035 break;
1037 case ATA_SIS_ID:
1038 ctlr = AR_F_SIS_RAID;
1039 rdp->disks[disk].sectors = SIS_LBA(rdp->disks[disk].dev);
1040 break;
1042 case ATA_ATI_ID:
1043 case ATA_VIA_ID:
1044 ctlr = AR_F_VIA_RAID;
1045 rdp->disks[disk].sectors = VIA_LBA(rdp->disks[disk].dev);
1046 break;
1048 default:
1049 /* XXX SOS
1050 * right, so here we are, we have an ATA chip and we want
1051 * to create a RAID and store the metadata.
1052 * we need to find a way to tell what kind of metadata this
1053 * hardware's BIOS might be using (good ideas are welcomed)
1054 * for now we just use our own native FreeBSD format.
1055 * the only way to get support for the BIOS format is to
1056 * setup the RAID from there, in that case we pickup the
1057 * metadata format from the disks (if we support it).
1059 kprintf("WARNING!! - not able to determine metadata format\n"
1060 "WARNING!! - Using FreeBSD PseudoRAID metadata\n"
1061 "If that is not what you want, use the BIOS to "
1062 "create the array\n");
1063 ctlr = AR_F_FREEBSD_RAID;
1064 rdp->disks[disk].sectors = PROMISE_LBA(rdp->disks[disk].dev);
1065 break;
1068 /* we need all disks to be of the same format */
1069 if ((rdp->format & AR_F_FORMAT_MASK) &&
1070 (rdp->format & AR_F_FORMAT_MASK) != (ctlr & AR_F_FORMAT_MASK)) {
1071 kfree(rdp, M_AR);
1072 return EXDEV;
1074 else
1075 rdp->format = ctlr;
1077 /* use the smallest disk of the lots size */
1078 /* gigabyte boundry ??? XXX SOS */
1079 if (disk_size)
1080 disk_size = min(rdp->disks[disk].sectors, disk_size);
1081 else
1082 disk_size = rdp->disks[disk].sectors;
1083 rdp->disks[disk].flags =
1084 (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_ONLINE);
1086 total_disks++;
1088 else {
1089 config->disks[disk] = -1;
1090 kfree(rdp, M_AR);
1091 return ENXIO;
1095 if (total_disks != config->total_disks) {
1096 kfree(rdp, M_AR);
1097 return ENODEV;
1100 switch (config->type) {
1101 case AR_T_JBOD:
1102 case AR_T_SPAN:
1103 case AR_T_RAID0:
1104 break;
1106 case AR_T_RAID1:
1107 if (total_disks != 2) {
1108 kfree(rdp, M_AR);
1109 return EPERM;
1111 break;
1113 case AR_T_RAID01:
1114 if (total_disks % 2 != 0) {
1115 kfree(rdp, M_AR);
1116 return EPERM;
1118 break;
1120 case AR_T_RAID5:
1121 if (total_disks < 3) {
1122 kfree(rdp, M_AR);
1123 return EPERM;
1125 break;
1127 default:
1128 kfree(rdp, M_AR);
1129 return EOPNOTSUPP;
1131 rdp->type = config->type;
1132 rdp->lun = array;
1133 if (rdp->type == AR_T_RAID0 || rdp->type == AR_T_RAID01 ||
1134 rdp->type == AR_T_RAID5) {
1135 int bit = 0;
1137 while (config->interleave >>= 1)
1138 bit++;
1139 rdp->interleave = 1 << bit;
1141 rdp->offset_sectors = 0;
1143 /* values that depend on metadata format */
1144 switch (rdp->format) {
1145 case AR_F_ADAPTEC_RAID:
1146 rdp->interleave = min(max(32, rdp->interleave), 128); /*+*/
1147 break;
1149 case AR_F_HPTV2_RAID:
1150 rdp->interleave = min(max(8, rdp->interleave), 128); /*+*/
1151 rdp->offset_sectors = HPTV2_LBA(x) + 1;
1152 break;
1154 case AR_F_HPTV3_RAID:
1155 rdp->interleave = min(max(32, rdp->interleave), 4096); /*+*/
1156 break;
1158 case AR_F_INTEL_RAID:
1159 rdp->interleave = min(max(8, rdp->interleave), 256); /*+*/
1160 break;
1162 case AR_F_ITE_RAID:
1163 rdp->interleave = min(max(2, rdp->interleave), 128); /*+*/
1164 break;
1166 case AR_F_JMICRON_RAID:
1167 rdp->interleave = min(max(8, rdp->interleave), 256); /*+*/
1168 break;
1170 case AR_F_LSIV2_RAID:
1171 rdp->interleave = min(max(2, rdp->interleave), 4096);
1172 break;
1174 case AR_F_LSIV3_RAID:
1175 rdp->interleave = min(max(2, rdp->interleave), 256);
1176 break;
1178 case AR_F_PROMISE_RAID:
1179 rdp->interleave = min(max(2, rdp->interleave), 2048); /*+*/
1180 break;
1182 case AR_F_SII_RAID:
1183 rdp->interleave = min(max(8, rdp->interleave), 256); /*+*/
1184 break;
1186 case AR_F_SIS_RAID:
1187 rdp->interleave = min(max(32, rdp->interleave), 512); /*+*/
1188 break;
1190 case AR_F_VIA_RAID:
1191 rdp->interleave = min(max(8, rdp->interleave), 128); /*+*/
1192 break;
1195 rdp->total_disks = total_disks;
1196 rdp->width = total_disks / (rdp->type & (AR_RAID1 | AR_T_RAID01) ? 2 : 1);
1197 rdp->total_sectors = disk_size * (rdp->width - (rdp->type == AR_RAID5));
1198 rdp->heads = 255;
1199 rdp->sectors = 63;
1200 rdp->cylinders = rdp->total_sectors / (255 * 63);
1201 rdp->rebuild_lba = 0;
1202 rdp->status |= AR_S_READY;
1204 /* we are committed to this array, grap the subdisks */
1205 for (disk = 0; disk < config->total_disks; disk++) {
1206 if ((subdisk = devclass_get_device(ata_raid_sub_devclass,
1207 config->disks[disk]))) {
1208 struct ata_raid_subdisk *ars = device_get_softc(subdisk);
1210 ars->raid[rdp->volume] = rdp;
1211 ars->disk_number[rdp->volume] = disk;
1214 ata_raid_attach(rdp, 1);
1215 ata_raid_arrays[array] = rdp;
1216 config->lun = array;
1217 return 0;
1220 static int
1221 ata_raid_delete(int array)
1223 struct ar_softc *rdp;
1224 device_t subdisk;
1225 int disk;
1227 if (!(rdp = ata_raid_arrays[array]))
1228 return ENXIO;
1230 rdp->status &= ~AR_S_READY;
1231 disk_destroy(&rdp->disk);
1233 for (disk = 0; disk < rdp->total_disks; disk++) {
1234 if ((rdp->disks[disk].flags & AR_DF_PRESENT) && rdp->disks[disk].dev) {
1235 if ((subdisk = devclass_get_device(ata_raid_sub_devclass,
1236 device_get_unit(rdp->disks[disk].dev)))) {
1237 struct ata_raid_subdisk *ars = device_get_softc(subdisk);
1239 if (ars->raid[rdp->volume] != rdp) /* XXX SOS */
1240 device_printf(subdisk, "DOH! this disk doesn't belong\n");
1241 if (ars->disk_number[rdp->volume] != disk) /* XXX SOS */
1242 device_printf(subdisk, "DOH! this disk number is wrong\n");
1243 ars->raid[rdp->volume] = NULL;
1244 ars->disk_number[rdp->volume] = -1;
1246 rdp->disks[disk].flags = 0;
1249 ata_raid_wipe_metadata(rdp);
1250 ata_raid_arrays[array] = NULL;
1251 kfree(rdp, M_AR);
1252 return 0;
1255 static int
1256 ata_raid_addspare(struct ata_ioc_raid_config *config)
1258 struct ar_softc *rdp;
1259 device_t subdisk;
1260 int disk;
1262 if (!(rdp = ata_raid_arrays[config->lun]))
1263 return ENXIO;
1264 if (!(rdp->status & AR_S_DEGRADED) || !(rdp->status & AR_S_READY))
1265 return ENXIO;
1266 if (rdp->status & AR_S_REBUILDING)
1267 return EBUSY;
1268 switch (rdp->type) {
1269 case AR_T_RAID1:
1270 case AR_T_RAID01:
1271 case AR_T_RAID5:
1272 for (disk = 0; disk < rdp->total_disks; disk++ ) {
1274 if (((rdp->disks[disk].flags & (AR_DF_PRESENT | AR_DF_ONLINE)) ==
1275 (AR_DF_PRESENT | AR_DF_ONLINE)) && rdp->disks[disk].dev)
1276 continue;
1278 if ((subdisk = devclass_get_device(ata_raid_sub_devclass,
1279 config->disks[0] ))) {
1280 struct ata_raid_subdisk *ars = device_get_softc(subdisk);
1282 if (ars->raid[rdp->volume])
1283 return EBUSY;
1285 /* XXX SOS validate size etc etc */
1286 ars->raid[rdp->volume] = rdp;
1287 ars->disk_number[rdp->volume] = disk;
1288 rdp->disks[disk].dev = device_get_parent(subdisk);
1289 rdp->disks[disk].flags =
1290 (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_SPARE);
1292 device_printf(rdp->disks[disk].dev,
1293 "inserted into ar%d disk%d as spare\n",
1294 rdp->lun, disk);
1295 ata_raid_config_changed(rdp, 1);
1296 return 0;
1299 return ENXIO;
1301 default:
1302 return EPERM;
1306 static int
1307 ata_raid_rebuild(int array)
1309 struct ar_softc *rdp;
1310 int disk, count;
1312 if (!(rdp = ata_raid_arrays[array]))
1313 return ENXIO;
1314 /* XXX SOS we should lock the rdp softc here */
1315 if (!(rdp->status & AR_S_DEGRADED) || !(rdp->status & AR_S_READY))
1316 return ENXIO;
1317 if (rdp->status & AR_S_REBUILDING)
1318 return EBUSY;
1320 switch (rdp->type) {
1321 case AR_T_RAID1:
1322 case AR_T_RAID01:
1323 case AR_T_RAID5:
1324 for (count = 0, disk = 0; disk < rdp->total_disks; disk++ ) {
1325 if (((rdp->disks[disk].flags &
1326 (AR_DF_PRESENT|AR_DF_ASSIGNED|AR_DF_ONLINE|AR_DF_SPARE)) ==
1327 (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_SPARE)) &&
1328 rdp->disks[disk].dev) {
1329 count++;
1333 if (count) {
1334 rdp->rebuild_lba = 0;
1335 rdp->status |= AR_S_REBUILDING;
1336 return 0;
1338 return EIO;
1340 default:
1341 return EPERM;
1345 static int
1346 ata_raid_read_metadata(device_t subdisk)
1348 devclass_t pci_devclass = devclass_find("pci");
1349 devclass_t devclass=device_get_devclass(GRANDPARENT(GRANDPARENT(subdisk)));
1351 /* prioritize vendor native metadata layout if possible */
1352 if (devclass == pci_devclass) {
1353 switch (pci_get_vendor(GRANDPARENT(device_get_parent(subdisk)))) {
1354 case ATA_HIGHPOINT_ID:
1355 if (ata_raid_hptv3_read_meta(subdisk, ata_raid_arrays))
1356 return 0;
1357 if (ata_raid_hptv2_read_meta(subdisk, ata_raid_arrays))
1358 return 0;
1359 break;
1361 case ATA_INTEL_ID:
1362 if (ata_raid_intel_read_meta(subdisk, ata_raid_arrays))
1363 return 0;
1364 break;
1366 case ATA_ITE_ID:
1367 if (ata_raid_ite_read_meta(subdisk, ata_raid_arrays))
1368 return 0;
1369 break;
1371 case ATA_JMICRON_ID:
1372 if (ata_raid_jmicron_read_meta(subdisk, ata_raid_arrays))
1373 return 0;
1374 break;
1376 case ATA_NVIDIA_ID:
1377 if (ata_raid_nvidia_read_meta(subdisk, ata_raid_arrays))
1378 return 0;
1379 break;
1381 case 0: /* XXX SOS cover up for bug in our PCI code */
1382 case ATA_PROMISE_ID:
1383 if (ata_raid_promise_read_meta(subdisk, ata_raid_arrays, 0))
1384 return 0;
1385 break;
1387 case ATA_ATI_ID:
1388 case ATA_SILICON_IMAGE_ID:
1389 if (ata_raid_sii_read_meta(subdisk, ata_raid_arrays))
1390 return 0;
1391 break;
1393 case ATA_SIS_ID:
1394 if (ata_raid_sis_read_meta(subdisk, ata_raid_arrays))
1395 return 0;
1396 break;
1398 case ATA_VIA_ID:
1399 if (ata_raid_via_read_meta(subdisk, ata_raid_arrays))
1400 return 0;
1401 break;
1405 /* handle controllers that have multiple layout possibilities */
1406 /* NOTE: the order of these are not insignificant */
1408 /* Adaptec HostRAID */
1409 if (ata_raid_adaptec_read_meta(subdisk, ata_raid_arrays))
1410 return 0;
1412 /* LSILogic v3 and v2 */
1413 if (ata_raid_lsiv3_read_meta(subdisk, ata_raid_arrays))
1414 return 0;
1415 if (ata_raid_lsiv2_read_meta(subdisk, ata_raid_arrays))
1416 return 0;
1418 /* if none of the above matched, try FreeBSD native format */
1419 return ata_raid_promise_read_meta(subdisk, ata_raid_arrays, 1);
1422 static int
1423 ata_raid_write_metadata(struct ar_softc *rdp)
1425 switch (rdp->format) {
1426 case AR_F_FREEBSD_RAID:
1427 case AR_F_PROMISE_RAID:
1428 return ata_raid_promise_write_meta(rdp);
1430 case AR_F_HPTV3_RAID:
1431 case AR_F_HPTV2_RAID:
1433 * always write HPT v2 metadata, the v3 BIOS knows it as well.
1434 * this is handy since we cannot know what version BIOS is on there
1436 return ata_raid_hptv2_write_meta(rdp);
1438 case AR_F_INTEL_RAID:
1439 return ata_raid_intel_write_meta(rdp);
1441 case AR_F_JMICRON_RAID:
1442 return ata_raid_jmicron_write_meta(rdp);
1444 case AR_F_SIS_RAID:
1445 return ata_raid_sis_write_meta(rdp);
1447 case AR_F_VIA_RAID:
1448 return ata_raid_via_write_meta(rdp);
1449 #if 0
1450 case AR_F_HPTV3_RAID:
1451 return ata_raid_hptv3_write_meta(rdp);
1453 case AR_F_ADAPTEC_RAID:
1454 return ata_raid_adaptec_write_meta(rdp);
1456 case AR_F_ITE_RAID:
1457 return ata_raid_ite_write_meta(rdp);
1459 case AR_F_LSIV2_RAID:
1460 return ata_raid_lsiv2_write_meta(rdp);
1462 case AR_F_LSIV3_RAID:
1463 return ata_raid_lsiv3_write_meta(rdp);
1465 case AR_F_NVIDIA_RAID:
1466 return ata_raid_nvidia_write_meta(rdp);
1468 case AR_F_SII_RAID:
1469 return ata_raid_sii_write_meta(rdp);
1471 #endif
1472 default:
1473 kprintf("ar%d: writing of %s metadata is NOT supported yet\n",
1474 rdp->lun, ata_raid_format(rdp));
1476 return -1;
1479 static int
1480 ata_raid_wipe_metadata(struct ar_softc *rdp)
1482 int disk, error = 0;
1483 u_int64_t lba;
1484 u_int32_t size;
1485 u_int8_t *meta;
1487 for (disk = 0; disk < rdp->total_disks; disk++) {
1488 if (rdp->disks[disk].dev) {
1489 switch (rdp->format) {
1490 case AR_F_ADAPTEC_RAID:
1491 lba = ADP_LBA(rdp->disks[disk].dev);
1492 size = sizeof(struct adaptec_raid_conf);
1493 break;
1495 case AR_F_HPTV2_RAID:
1496 lba = HPTV2_LBA(rdp->disks[disk].dev);
1497 size = sizeof(struct hptv2_raid_conf);
1498 break;
1500 case AR_F_HPTV3_RAID:
1501 lba = HPTV3_LBA(rdp->disks[disk].dev);
1502 size = sizeof(struct hptv3_raid_conf);
1503 break;
1505 case AR_F_INTEL_RAID:
1506 lba = INTEL_LBA(rdp->disks[disk].dev);
1507 size = 3 * 512; /* XXX SOS */
1508 break;
1510 case AR_F_ITE_RAID:
1511 lba = ITE_LBA(rdp->disks[disk].dev);
1512 size = sizeof(struct ite_raid_conf);
1513 break;
1515 case AR_F_JMICRON_RAID:
1516 lba = JMICRON_LBA(rdp->disks[disk].dev);
1517 size = sizeof(struct jmicron_raid_conf);
1518 break;
1520 case AR_F_LSIV2_RAID:
1521 lba = LSIV2_LBA(rdp->disks[disk].dev);
1522 size = sizeof(struct lsiv2_raid_conf);
1523 break;
1525 case AR_F_LSIV3_RAID:
1526 lba = LSIV3_LBA(rdp->disks[disk].dev);
1527 size = sizeof(struct lsiv3_raid_conf);
1528 break;
1530 case AR_F_NVIDIA_RAID:
1531 lba = NVIDIA_LBA(rdp->disks[disk].dev);
1532 size = sizeof(struct nvidia_raid_conf);
1533 break;
1535 case AR_F_FREEBSD_RAID:
1536 case AR_F_PROMISE_RAID:
1537 lba = PROMISE_LBA(rdp->disks[disk].dev);
1538 size = sizeof(struct promise_raid_conf);
1539 break;
1541 case AR_F_SII_RAID:
1542 lba = SII_LBA(rdp->disks[disk].dev);
1543 size = sizeof(struct sii_raid_conf);
1544 break;
1546 case AR_F_SIS_RAID:
1547 lba = SIS_LBA(rdp->disks[disk].dev);
1548 size = sizeof(struct sis_raid_conf);
1549 break;
1551 case AR_F_VIA_RAID:
1552 lba = VIA_LBA(rdp->disks[disk].dev);
1553 size = sizeof(struct via_raid_conf);
1554 break;
1556 default:
1557 kprintf("ar%d: wiping of %s metadata is NOT supported yet\n",
1558 rdp->lun, ata_raid_format(rdp));
1559 return ENXIO;
1561 meta = kmalloc(size, M_AR, M_WAITOK | M_ZERO);
1562 if (ata_raid_rw(rdp->disks[disk].dev, lba, meta, size,
1563 ATA_R_WRITE | ATA_R_DIRECT)) {
1564 device_printf(rdp->disks[disk].dev, "wipe metadata failed\n");
1565 error = EIO;
1567 kfree(meta, M_AR);
1570 return error;
1573 /* Adaptec HostRAID Metadata */
1574 static int
1575 ata_raid_adaptec_read_meta(device_t dev, struct ar_softc **raidp)
1577 struct ata_raid_subdisk *ars = device_get_softc(dev);
1578 device_t parent = device_get_parent(dev);
1579 struct adaptec_raid_conf *meta;
1580 struct ar_softc *raid;
1581 int array, disk, retval = 0;
1583 meta = (struct adaptec_raid_conf *)
1584 kmalloc(sizeof(struct adaptec_raid_conf), M_AR, M_WAITOK | M_ZERO);
1586 if (ata_raid_rw(parent, ADP_LBA(parent),
1587 meta, sizeof(struct adaptec_raid_conf), ATA_R_READ)) {
1588 if (testing || bootverbose)
1589 device_printf(parent, "Adaptec read metadata failed\n");
1590 goto adaptec_out;
1593 /* check if this is a Adaptec RAID struct */
1594 if (meta->magic_0 != ADP_MAGIC_0 || meta->magic_3 != ADP_MAGIC_3) {
1595 if (testing || bootverbose)
1596 device_printf(parent, "Adaptec check1 failed\n");
1597 goto adaptec_out;
1600 if (testing || bootverbose)
1601 ata_raid_adaptec_print_meta(meta);
1603 /* now convert Adaptec metadata into our generic form */
1604 for (array = 0; array < MAX_ARRAYS; array++) {
1605 if (!raidp[array]) {
1606 raidp[array] =
1607 (struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
1608 M_WAITOK | M_ZERO);
1610 raid = raidp[array];
1611 if (raid->format && (raid->format != AR_F_ADAPTEC_RAID))
1612 continue;
1614 if (raid->magic_0 && raid->magic_0 != meta->configs[0].magic_0)
1615 continue;
1617 if (!meta->generation || be32toh(meta->generation) > raid->generation) {
1618 switch (meta->configs[0].type) {
1619 case ADP_T_RAID0:
1620 raid->magic_0 = meta->configs[0].magic_0;
1621 raid->type = AR_T_RAID0;
1622 raid->interleave = 1 << (meta->configs[0].stripe_shift >> 1);
1623 raid->width = be16toh(meta->configs[0].total_disks);
1624 break;
1626 case ADP_T_RAID1:
1627 raid->magic_0 = meta->configs[0].magic_0;
1628 raid->type = AR_T_RAID1;
1629 raid->width = be16toh(meta->configs[0].total_disks) / 2;
1630 break;
1632 default:
1633 device_printf(parent, "Adaptec unknown RAID type 0x%02x\n",
1634 meta->configs[0].type);
1635 kfree(raidp[array], M_AR);
1636 raidp[array] = NULL;
1637 goto adaptec_out;
1640 raid->format = AR_F_ADAPTEC_RAID;
1641 raid->generation = be32toh(meta->generation);
1642 raid->total_disks = be16toh(meta->configs[0].total_disks);
1643 raid->total_sectors = be32toh(meta->configs[0].sectors);
1644 raid->heads = 255;
1645 raid->sectors = 63;
1646 raid->cylinders = raid->total_sectors / (63 * 255);
1647 raid->offset_sectors = 0;
1648 raid->rebuild_lba = 0;
1649 raid->lun = array;
1650 strncpy(raid->name, meta->configs[0].name,
1651 min(sizeof(raid->name), sizeof(meta->configs[0].name)));
1653 /* clear out any old info */
1654 if (raid->generation) {
1655 for (disk = 0; disk < raid->total_disks; disk++) {
1656 raid->disks[disk].dev = NULL;
1657 raid->disks[disk].flags = 0;
1661 if (be32toh(meta->generation) >= raid->generation) {
1662 struct ata_device *atadev = device_get_softc(parent);
1663 struct ata_channel *ch = device_get_softc(GRANDPARENT(dev));
1664 int disk_number = (ch->unit << !(ch->flags & ATA_NO_SLAVE)) +
1665 ATA_DEV(atadev->unit);
1667 raid->disks[disk_number].dev = parent;
1668 raid->disks[disk_number].sectors =
1669 be32toh(meta->configs[disk_number + 1].sectors);
1670 raid->disks[disk_number].flags =
1671 (AR_DF_ONLINE | AR_DF_PRESENT | AR_DF_ASSIGNED);
1672 ars->raid[raid->volume] = raid;
1673 ars->disk_number[raid->volume] = disk_number;
1674 retval = 1;
1676 break;
1679 adaptec_out:
1680 kfree(meta, M_AR);
1681 return retval;
1684 /* Highpoint V2 RocketRAID Metadata */
1685 static int
1686 ata_raid_hptv2_read_meta(device_t dev, struct ar_softc **raidp)
1688 struct ata_raid_subdisk *ars = device_get_softc(dev);
1689 device_t parent = device_get_parent(dev);
1690 struct hptv2_raid_conf *meta;
1691 struct ar_softc *raid = NULL;
1692 int array, disk_number = 0, retval = 0;
1694 meta = (struct hptv2_raid_conf *)kmalloc(sizeof(struct hptv2_raid_conf),
1695 M_AR, M_WAITOK | M_ZERO);
1697 if (ata_raid_rw(parent, HPTV2_LBA(parent),
1698 meta, sizeof(struct hptv2_raid_conf), ATA_R_READ)) {
1699 if (testing || bootverbose)
1700 device_printf(parent, "HighPoint (v2) read metadata failed\n");
1701 goto hptv2_out;
1704 /* check if this is a HighPoint v2 RAID struct */
1705 if (meta->magic != HPTV2_MAGIC_OK && meta->magic != HPTV2_MAGIC_BAD) {
1706 if (testing || bootverbose)
1707 device_printf(parent, "HighPoint (v2) check1 failed\n");
1708 goto hptv2_out;
1711 /* is this disk defined, or an old leftover/spare ? */
1712 if (!meta->magic_0) {
1713 if (testing || bootverbose)
1714 device_printf(parent, "HighPoint (v2) check2 failed\n");
1715 goto hptv2_out;
1718 if (testing || bootverbose)
1719 ata_raid_hptv2_print_meta(meta);
1721 /* now convert HighPoint (v2) metadata into our generic form */
1722 for (array = 0; array < MAX_ARRAYS; array++) {
1723 if (!raidp[array]) {
1724 raidp[array] =
1725 (struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
1726 M_WAITOK | M_ZERO);
1728 raid = raidp[array];
1729 if (raid->format && (raid->format != AR_F_HPTV2_RAID))
1730 continue;
1732 switch (meta->type) {
1733 case HPTV2_T_RAID0:
1734 if ((meta->order & (HPTV2_O_RAID0|HPTV2_O_OK)) ==
1735 (HPTV2_O_RAID0|HPTV2_O_OK))
1736 goto highpoint_raid1;
1737 if (meta->order & (HPTV2_O_RAID0 | HPTV2_O_RAID1))
1738 goto highpoint_raid01;
1739 if (raid->magic_0 && raid->magic_0 != meta->magic_0)
1740 continue;
1741 raid->magic_0 = meta->magic_0;
1742 raid->type = AR_T_RAID0;
1743 raid->interleave = 1 << meta->stripe_shift;
1744 disk_number = meta->disk_number;
1745 if (!(meta->order & HPTV2_O_OK))
1746 meta->magic = 0; /* mark bad */
1747 break;
1749 case HPTV2_T_RAID1:
1750 highpoint_raid1:
1751 if (raid->magic_0 && raid->magic_0 != meta->magic_0)
1752 continue;
1753 raid->magic_0 = meta->magic_0;
1754 raid->type = AR_T_RAID1;
1755 disk_number = (meta->disk_number > 0);
1756 break;
1758 case HPTV2_T_RAID01_RAID0:
1759 highpoint_raid01:
1760 if (meta->order & HPTV2_O_RAID0) {
1761 if ((raid->magic_0 && raid->magic_0 != meta->magic_0) ||
1762 (raid->magic_1 && raid->magic_1 != meta->magic_1))
1763 continue;
1764 raid->magic_0 = meta->magic_0;
1765 raid->magic_1 = meta->magic_1;
1766 raid->type = AR_T_RAID01;
1767 raid->interleave = 1 << meta->stripe_shift;
1768 disk_number = meta->disk_number;
1770 else {
1771 if (raid->magic_1 && raid->magic_1 != meta->magic_1)
1772 continue;
1773 raid->magic_1 = meta->magic_1;
1774 raid->type = AR_T_RAID01;
1775 raid->interleave = 1 << meta->stripe_shift;
1776 disk_number = meta->disk_number + meta->array_width;
1777 if (!(meta->order & HPTV2_O_RAID1))
1778 meta->magic = 0; /* mark bad */
1780 break;
1782 case HPTV2_T_SPAN:
1783 if (raid->magic_0 && raid->magic_0 != meta->magic_0)
1784 continue;
1785 raid->magic_0 = meta->magic_0;
1786 raid->type = AR_T_SPAN;
1787 disk_number = meta->disk_number;
1788 break;
1790 default:
1791 device_printf(parent, "Highpoint (v2) unknown RAID type 0x%02x\n",
1792 meta->type);
1793 kfree(raidp[array], M_AR);
1794 raidp[array] = NULL;
1795 goto hptv2_out;
1798 raid->format |= AR_F_HPTV2_RAID;
1799 raid->disks[disk_number].dev = parent;
1800 raid->disks[disk_number].flags = (AR_DF_PRESENT | AR_DF_ASSIGNED);
1801 raid->lun = array;
1802 strncpy(raid->name, meta->name_1,
1803 min(sizeof(raid->name), sizeof(meta->name_1)));
1804 if (meta->magic == HPTV2_MAGIC_OK) {
1805 raid->disks[disk_number].flags |= AR_DF_ONLINE;
1806 raid->width = meta->array_width;
1807 raid->total_sectors = meta->total_sectors;
1808 raid->heads = 255;
1809 raid->sectors = 63;
1810 raid->cylinders = raid->total_sectors / (63 * 255);
1811 raid->offset_sectors = HPTV2_LBA(parent) + 1;
1812 raid->rebuild_lba = meta->rebuild_lba;
1813 raid->disks[disk_number].sectors =
1814 raid->total_sectors / raid->width;
1816 else
1817 raid->disks[disk_number].flags &= ~AR_DF_ONLINE;
1819 if ((raid->type & AR_T_RAID0) && (raid->total_disks < raid->width))
1820 raid->total_disks = raid->width;
1821 if (disk_number >= raid->total_disks)
1822 raid->total_disks = disk_number + 1;
1823 ars->raid[raid->volume] = raid;
1824 ars->disk_number[raid->volume] = disk_number;
1825 retval = 1;
1826 break;
1829 hptv2_out:
1830 kfree(meta, M_AR);
1831 return retval;
1834 static int
1835 ata_raid_hptv2_write_meta(struct ar_softc *rdp)
1837 struct hptv2_raid_conf *meta;
1838 struct timeval timestamp;
1839 int disk, error = 0;
1841 meta = (struct hptv2_raid_conf *)kmalloc(sizeof(struct hptv2_raid_conf),
1842 M_AR, M_WAITOK | M_ZERO);
1844 microtime(&timestamp);
1845 rdp->magic_0 = timestamp.tv_sec + 2;
1846 rdp->magic_1 = timestamp.tv_sec;
1848 for (disk = 0; disk < rdp->total_disks; disk++) {
1849 if ((rdp->disks[disk].flags & (AR_DF_PRESENT | AR_DF_ONLINE)) ==
1850 (AR_DF_PRESENT | AR_DF_ONLINE))
1851 meta->magic = HPTV2_MAGIC_OK;
1852 if (rdp->disks[disk].flags & AR_DF_ASSIGNED) {
1853 meta->magic_0 = rdp->magic_0;
1854 if (strlen(rdp->name))
1855 strncpy(meta->name_1, rdp->name, sizeof(meta->name_1));
1856 else
1857 strcpy(meta->name_1, "FreeBSD");
1859 meta->disk_number = disk;
1861 switch (rdp->type) {
1862 case AR_T_RAID0:
1863 meta->type = HPTV2_T_RAID0;
1864 strcpy(meta->name_2, "RAID 0");
1865 if (rdp->disks[disk].flags & AR_DF_ONLINE)
1866 meta->order = HPTV2_O_OK;
1867 break;
1869 case AR_T_RAID1:
1870 meta->type = HPTV2_T_RAID0;
1871 strcpy(meta->name_2, "RAID 1");
1872 meta->disk_number = (disk < rdp->width) ? disk : disk + 5;
1873 meta->order = HPTV2_O_RAID0 | HPTV2_O_OK;
1874 break;
1876 case AR_T_RAID01:
1877 meta->type = HPTV2_T_RAID01_RAID0;
1878 strcpy(meta->name_2, "RAID 0+1");
1879 if (rdp->disks[disk].flags & AR_DF_ONLINE) {
1880 if (disk < rdp->width) {
1881 meta->order = (HPTV2_O_RAID0 | HPTV2_O_RAID1);
1882 meta->magic_0 = rdp->magic_0 - 1;
1884 else {
1885 meta->order = HPTV2_O_RAID1;
1886 meta->disk_number -= rdp->width;
1889 else
1890 meta->magic_0 = rdp->magic_0 - 1;
1891 meta->magic_1 = rdp->magic_1;
1892 break;
1894 case AR_T_SPAN:
1895 meta->type = HPTV2_T_SPAN;
1896 strcpy(meta->name_2, "SPAN");
1897 break;
1898 default:
1899 kfree(meta, M_AR);
1900 return ENODEV;
1903 meta->array_width = rdp->width;
1904 meta->stripe_shift = (rdp->width > 1) ? (ffs(rdp->interleave)-1) : 0;
1905 meta->total_sectors = rdp->total_sectors;
1906 meta->rebuild_lba = rdp->rebuild_lba;
1907 if (testing || bootverbose)
1908 ata_raid_hptv2_print_meta(meta);
1909 if (rdp->disks[disk].dev) {
1910 if (ata_raid_rw(rdp->disks[disk].dev,
1911 HPTV2_LBA(rdp->disks[disk].dev), meta,
1912 sizeof(struct promise_raid_conf),
1913 ATA_R_WRITE | ATA_R_DIRECT)) {
1914 device_printf(rdp->disks[disk].dev, "write metadata failed\n");
1915 error = EIO;
1919 kfree(meta, M_AR);
1920 return error;
1923 /* Highpoint V3 RocketRAID Metadata */
1924 static int
1925 ata_raid_hptv3_read_meta(device_t dev, struct ar_softc **raidp)
1927 struct ata_raid_subdisk *ars = device_get_softc(dev);
1928 device_t parent = device_get_parent(dev);
1929 struct hptv3_raid_conf *meta;
1930 struct ar_softc *raid = NULL;
1931 int array, disk_number, retval = 0;
1933 meta = (struct hptv3_raid_conf *)kmalloc(sizeof(struct hptv3_raid_conf),
1934 M_AR, M_WAITOK | M_ZERO);
1936 if (ata_raid_rw(parent, HPTV3_LBA(parent),
1937 meta, sizeof(struct hptv3_raid_conf), ATA_R_READ)) {
1938 if (testing || bootverbose)
1939 device_printf(parent, "HighPoint (v3) read metadata failed\n");
1940 goto hptv3_out;
1943 /* check if this is a HighPoint v3 RAID struct */
1944 if (meta->magic != HPTV3_MAGIC) {
1945 if (testing || bootverbose)
1946 device_printf(parent, "HighPoint (v3) check1 failed\n");
1947 goto hptv3_out;
1950 /* check if there are any config_entries */
1951 if (meta->config_entries < 1) {
1952 if (testing || bootverbose)
1953 device_printf(parent, "HighPoint (v3) check2 failed\n");
1954 goto hptv3_out;
1957 if (testing || bootverbose)
1958 ata_raid_hptv3_print_meta(meta);
1960 /* now convert HighPoint (v3) metadata into our generic form */
1961 for (array = 0; array < MAX_ARRAYS; array++) {
1962 if (!raidp[array]) {
1963 raidp[array] =
1964 (struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
1965 M_WAITOK | M_ZERO);
1967 raid = raidp[array];
1968 if (raid->format && (raid->format != AR_F_HPTV3_RAID))
1969 continue;
1971 if ((raid->format & AR_F_HPTV3_RAID) && raid->magic_0 != meta->magic_0)
1972 continue;
1974 switch (meta->configs[0].type) {
1975 case HPTV3_T_RAID0:
1976 raid->type = AR_T_RAID0;
1977 raid->width = meta->configs[0].total_disks;
1978 disk_number = meta->configs[0].disk_number;
1979 break;
1981 case HPTV3_T_RAID1:
1982 raid->type = AR_T_RAID1;
1983 raid->width = meta->configs[0].total_disks / 2;
1984 disk_number = meta->configs[0].disk_number;
1985 break;
1987 case HPTV3_T_RAID5:
1988 raid->type = AR_T_RAID5;
1989 raid->width = meta->configs[0].total_disks;
1990 disk_number = meta->configs[0].disk_number;
1991 break;
1993 case HPTV3_T_SPAN:
1994 raid->type = AR_T_SPAN;
1995 raid->width = meta->configs[0].total_disks;
1996 disk_number = meta->configs[0].disk_number;
1997 break;
1999 default:
2000 device_printf(parent, "Highpoint (v3) unknown RAID type 0x%02x\n",
2001 meta->configs[0].type);
2002 kfree(raidp[array], M_AR);
2003 raidp[array] = NULL;
2004 goto hptv3_out;
2006 if (meta->config_entries == 2) {
2007 switch (meta->configs[1].type) {
2008 case HPTV3_T_RAID1:
2009 if (raid->type == AR_T_RAID0) {
2010 raid->type = AR_T_RAID01;
2011 disk_number = meta->configs[1].disk_number +
2012 (meta->configs[0].disk_number << 1);
2013 break;
2015 default:
2016 device_printf(parent, "Highpoint (v3) unknown level 2 0x%02x\n",
2017 meta->configs[1].type);
2018 kfree(raidp[array], M_AR);
2019 raidp[array] = NULL;
2020 goto hptv3_out;
2024 raid->magic_0 = meta->magic_0;
2025 raid->format = AR_F_HPTV3_RAID;
2026 raid->generation = meta->timestamp;
2027 raid->interleave = 1 << meta->configs[0].stripe_shift;
2028 raid->total_disks = meta->configs[0].total_disks +
2029 meta->configs[1].total_disks;
2030 raid->total_sectors = meta->configs[0].total_sectors +
2031 ((u_int64_t)meta->configs_high[0].total_sectors << 32);
2032 raid->heads = 255;
2033 raid->sectors = 63;
2034 raid->cylinders = raid->total_sectors / (63 * 255);
2035 raid->offset_sectors = 0;
2036 raid->rebuild_lba = meta->configs[0].rebuild_lba +
2037 ((u_int64_t)meta->configs_high[0].rebuild_lba << 32);
2038 raid->lun = array;
2039 strncpy(raid->name, meta->name,
2040 min(sizeof(raid->name), sizeof(meta->name)));
2041 raid->disks[disk_number].sectors = raid->total_sectors /
2042 (raid->type == AR_T_RAID5 ? raid->width - 1 : raid->width);
2043 raid->disks[disk_number].dev = parent;
2044 raid->disks[disk_number].flags =
2045 (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_ONLINE);
2046 ars->raid[raid->volume] = raid;
2047 ars->disk_number[raid->volume] = disk_number;
2048 retval = 1;
2049 break;
2052 hptv3_out:
2053 kfree(meta, M_AR);
2054 return retval;
2057 /* Intel MatrixRAID Metadata */
2058 static int
2059 ata_raid_intel_read_meta(device_t dev, struct ar_softc **raidp)
2061 struct ata_raid_subdisk *ars = device_get_softc(dev);
2062 device_t parent = device_get_parent(dev);
2063 struct intel_raid_conf *meta;
2064 struct intel_raid_mapping *map;
2065 struct ar_softc *raid = NULL;
2066 u_int32_t checksum, *ptr;
2067 int array, count, disk, volume = 1, retval = 0;
2068 char *tmp;
2070 meta = (struct intel_raid_conf *)kmalloc(1536, M_AR, M_WAITOK | M_ZERO);
2072 if (ata_raid_rw(parent, INTEL_LBA(parent), meta, 1024, ATA_R_READ)) {
2073 if (testing || bootverbose)
2074 device_printf(parent, "Intel read metadata failed\n");
2075 goto intel_out;
2077 tmp = (char *)meta;
2078 bcopy(tmp, tmp+1024, 512);
2079 bcopy(tmp+512, tmp, 1024);
2080 bzero(tmp+1024, 512);
2082 /* check if this is a Intel RAID struct */
2083 if (strncmp(meta->intel_id, INTEL_MAGIC, strlen(INTEL_MAGIC))) {
2084 if (testing || bootverbose)
2085 device_printf(parent, "Intel check1 failed\n");
2086 goto intel_out;
2089 for (checksum = 0, ptr = (u_int32_t *)meta, count = 0;
2090 count < (meta->config_size / sizeof(u_int32_t)); count++) {
2091 checksum += *ptr++;
2093 checksum -= meta->checksum;
2094 if (checksum != meta->checksum) {
2095 if (testing || bootverbose)
2096 device_printf(parent, "Intel check2 failed\n");
2097 goto intel_out;
2100 if (testing || bootverbose)
2101 ata_raid_intel_print_meta(meta);
2103 map = (struct intel_raid_mapping *)&meta->disk[meta->total_disks];
2105 /* now convert Intel metadata into our generic form */
2106 for (array = 0; array < MAX_ARRAYS; array++) {
2107 if (!raidp[array]) {
2108 raidp[array] =
2109 (struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
2110 M_WAITOK | M_ZERO);
2112 raid = raidp[array];
2113 if (raid->format && (raid->format != AR_F_INTEL_RAID))
2114 continue;
2116 if ((raid->format & AR_F_INTEL_RAID) &&
2117 (raid->magic_0 != meta->config_id))
2118 continue;
2121 * update our knowledge about the array config based on generation
2122 * NOTE: there can be multiple volumes on a disk set
2124 if (!meta->generation || meta->generation > raid->generation) {
2125 switch (map->type) {
2126 case INTEL_T_RAID0:
2127 raid->type = AR_T_RAID0;
2128 raid->width = map->total_disks;
2129 break;
2131 case INTEL_T_RAID1:
2132 if (map->total_disks == 4)
2133 raid->type = AR_T_RAID01;
2134 else
2135 raid->type = AR_T_RAID1;
2136 raid->width = map->total_disks / 2;
2137 break;
2139 case INTEL_T_RAID5:
2140 raid->type = AR_T_RAID5;
2141 raid->width = map->total_disks;
2142 break;
2144 default:
2145 device_printf(parent, "Intel unknown RAID type 0x%02x\n",
2146 map->type);
2147 kfree(raidp[array], M_AR);
2148 raidp[array] = NULL;
2149 goto intel_out;
2152 switch (map->status) {
2153 case INTEL_S_READY:
2154 raid->status = AR_S_READY;
2155 break;
2156 case INTEL_S_DEGRADED:
2157 raid->status |= AR_S_DEGRADED;
2158 break;
2159 case INTEL_S_DISABLED:
2160 case INTEL_S_FAILURE:
2161 raid->status = 0;
2164 raid->magic_0 = meta->config_id;
2165 raid->format = AR_F_INTEL_RAID;
2166 raid->generation = meta->generation;
2167 raid->interleave = map->stripe_sectors;
2168 raid->total_disks = map->total_disks;
2169 raid->total_sectors = map->total_sectors;
2170 raid->heads = 255;
2171 raid->sectors = 63;
2172 raid->cylinders = raid->total_sectors / (63 * 255);
2173 raid->offset_sectors = map->offset;
2174 raid->rebuild_lba = 0;
2175 raid->lun = array;
2176 raid->volume = volume - 1;
2177 strncpy(raid->name, map->name,
2178 min(sizeof(raid->name), sizeof(map->name)));
2180 /* clear out any old info */
2181 for (disk = 0; disk < raid->total_disks; disk++) {
2182 raid->disks[disk].dev = NULL;
2183 bcopy(meta->disk[map->disk_idx[disk]].serial,
2184 raid->disks[disk].serial,
2185 sizeof(raid->disks[disk].serial));
2186 raid->disks[disk].sectors =
2187 meta->disk[map->disk_idx[disk]].sectors;
2188 raid->disks[disk].flags = 0;
2189 if (meta->disk[map->disk_idx[disk]].flags & INTEL_F_ONLINE)
2190 raid->disks[disk].flags |= AR_DF_ONLINE;
2191 if (meta->disk[map->disk_idx[disk]].flags & INTEL_F_ASSIGNED)
2192 raid->disks[disk].flags |= AR_DF_ASSIGNED;
2193 if (meta->disk[map->disk_idx[disk]].flags & INTEL_F_SPARE) {
2194 raid->disks[disk].flags &= ~(AR_DF_ONLINE | AR_DF_ASSIGNED);
2195 raid->disks[disk].flags |= AR_DF_SPARE;
2197 if (meta->disk[map->disk_idx[disk]].flags & INTEL_F_DOWN)
2198 raid->disks[disk].flags &= ~AR_DF_ONLINE;
2201 if (meta->generation >= raid->generation) {
2202 for (disk = 0; disk < raid->total_disks; disk++) {
2203 struct ata_device *atadev = device_get_softc(parent);
2205 if (!strncmp(raid->disks[disk].serial, atadev->param.serial,
2206 sizeof(raid->disks[disk].serial))) {
2207 raid->disks[disk].dev = parent;
2208 raid->disks[disk].flags |= (AR_DF_PRESENT | AR_DF_ONLINE);
2209 ars->raid[raid->volume] = raid;
2210 ars->disk_number[raid->volume] = disk;
2211 retval = 1;
2215 else
2216 goto intel_out;
2218 if (retval) {
2219 if (volume < meta->total_volumes) {
2220 map = (struct intel_raid_mapping *)
2221 &map->disk_idx[map->total_disks];
2222 volume++;
2223 retval = 0;
2224 continue;
2226 break;
2228 else {
2229 kfree(raidp[array], M_AR);
2230 raidp[array] = NULL;
2231 if (volume == 2)
2232 retval = 1;
2236 intel_out:
2237 kfree(meta, M_AR);
2238 return retval;
2241 static int
2242 ata_raid_intel_write_meta(struct ar_softc *rdp)
2244 struct intel_raid_conf *meta;
2245 struct intel_raid_mapping *map;
2246 struct timeval timestamp;
2247 u_int32_t checksum, *ptr;
2248 int count, disk, error = 0;
2249 char *tmp;
2251 meta = (struct intel_raid_conf *)kmalloc(1536, M_AR, M_WAITOK | M_ZERO);
2253 rdp->generation++;
2254 microtime(&timestamp);
2256 bcopy(INTEL_MAGIC, meta->intel_id, sizeof(meta->intel_id));
2257 bcopy(INTEL_VERSION_1100, meta->version, sizeof(meta->version));
2258 meta->config_id = timestamp.tv_sec;
2259 meta->generation = rdp->generation;
2260 meta->total_disks = rdp->total_disks;
2261 meta->total_volumes = 1; /* XXX SOS */
2262 for (disk = 0; disk < rdp->total_disks; disk++) {
2263 if (rdp->disks[disk].dev) {
2264 struct ata_channel *ch =
2265 device_get_softc(device_get_parent(rdp->disks[disk].dev));
2266 struct ata_device *atadev =
2267 device_get_softc(rdp->disks[disk].dev);
2269 bcopy(atadev->param.serial, meta->disk[disk].serial,
2270 sizeof(rdp->disks[disk].serial));
2271 meta->disk[disk].sectors = rdp->disks[disk].sectors;
2272 meta->disk[disk].id = (ch->unit << 16) | ATA_DEV(atadev->unit);
2274 else
2275 meta->disk[disk].sectors = rdp->total_sectors / rdp->width;
2276 meta->disk[disk].flags = 0;
2277 if (rdp->disks[disk].flags & AR_DF_SPARE)
2278 meta->disk[disk].flags |= INTEL_F_SPARE;
2279 else {
2280 if (rdp->disks[disk].flags & AR_DF_ONLINE)
2281 meta->disk[disk].flags |= INTEL_F_ONLINE;
2282 else
2283 meta->disk[disk].flags |= INTEL_F_DOWN;
2284 if (rdp->disks[disk].flags & AR_DF_ASSIGNED)
2285 meta->disk[disk].flags |= INTEL_F_ASSIGNED;
2288 map = (struct intel_raid_mapping *)&meta->disk[meta->total_disks];
2290 bcopy(rdp->name, map->name, sizeof(rdp->name));
2291 map->total_sectors = rdp->total_sectors;
2292 map->state = 12; /* XXX SOS */
2293 map->offset = rdp->offset_sectors;
2294 map->stripe_count = rdp->total_sectors / (rdp->interleave*rdp->total_disks);
2295 map->stripe_sectors = rdp->interleave;
2296 map->disk_sectors = rdp->total_sectors / rdp->width;
2297 map->status = INTEL_S_READY; /* XXX SOS */
2298 switch (rdp->type) {
2299 case AR_T_RAID0:
2300 map->type = INTEL_T_RAID0;
2301 break;
2302 case AR_T_RAID1:
2303 map->type = INTEL_T_RAID1;
2304 break;
2305 case AR_T_RAID01:
2306 map->type = INTEL_T_RAID1;
2307 break;
2308 case AR_T_RAID5:
2309 map->type = INTEL_T_RAID5;
2310 break;
2311 default:
2312 kfree(meta, M_AR);
2313 return ENODEV;
2315 map->total_disks = rdp->total_disks;
2316 map->magic[0] = 0x02;
2317 map->magic[1] = 0xff;
2318 map->magic[2] = 0x01;
2319 for (disk = 0; disk < rdp->total_disks; disk++)
2320 map->disk_idx[disk] = disk;
2322 meta->config_size = (char *)&map->disk_idx[disk] - (char *)meta;
2323 for (checksum = 0, ptr = (u_int32_t *)meta, count = 0;
2324 count < (meta->config_size / sizeof(u_int32_t)); count++) {
2325 checksum += *ptr++;
2327 meta->checksum = checksum;
2329 if (testing || bootverbose)
2330 ata_raid_intel_print_meta(meta);
2332 tmp = (char *)meta;
2333 bcopy(tmp, tmp+1024, 512);
2334 bcopy(tmp+512, tmp, 1024);
2335 bzero(tmp+1024, 512);
2337 for (disk = 0; disk < rdp->total_disks; disk++) {
2338 if (rdp->disks[disk].dev) {
2339 if (ata_raid_rw(rdp->disks[disk].dev,
2340 INTEL_LBA(rdp->disks[disk].dev),
2341 meta, 1024, ATA_R_WRITE | ATA_R_DIRECT)) {
2342 device_printf(rdp->disks[disk].dev, "write metadata failed\n");
2343 error = EIO;
2347 kfree(meta, M_AR);
2348 return error;
2352 /* Integrated Technology Express Metadata */
2353 static int
2354 ata_raid_ite_read_meta(device_t dev, struct ar_softc **raidp)
2356 struct ata_raid_subdisk *ars = device_get_softc(dev);
2357 device_t parent = device_get_parent(dev);
2358 struct ite_raid_conf *meta;
2359 struct ar_softc *raid = NULL;
2360 int array, disk_number, count, retval = 0;
2361 u_int16_t *ptr;
2363 meta = (struct ite_raid_conf *)kmalloc(sizeof(struct ite_raid_conf), M_AR,
2364 M_WAITOK | M_ZERO);
2366 if (ata_raid_rw(parent, ITE_LBA(parent),
2367 meta, sizeof(struct ite_raid_conf), ATA_R_READ)) {
2368 if (testing || bootverbose)
2369 device_printf(parent, "ITE read metadata failed\n");
2370 goto ite_out;
2373 /* check if this is a ITE RAID struct */
2374 for (ptr = (u_int16_t *)meta->ite_id, count = 0;
2375 count < sizeof(meta->ite_id)/sizeof(uint16_t); count++)
2376 ptr[count] = be16toh(ptr[count]);
2378 if (strncmp(meta->ite_id, ITE_MAGIC, strlen(ITE_MAGIC))) {
2379 if (testing || bootverbose)
2380 device_printf(parent, "ITE check1 failed\n");
2381 goto ite_out;
2384 if (testing || bootverbose)
2385 ata_raid_ite_print_meta(meta);
2387 /* now convert ITE metadata into our generic form */
2388 for (array = 0; array < MAX_ARRAYS; array++) {
2389 if ((raid = raidp[array])) {
2390 if (raid->format != AR_F_ITE_RAID)
2391 continue;
2392 if (raid->magic_0 != *((u_int64_t *)meta->timestamp_0))
2393 continue;
2396 /* if we dont have a disks timestamp the RAID is invalidated */
2397 if (*((u_int64_t *)meta->timestamp_1) == 0)
2398 goto ite_out;
2400 if (!raid) {
2401 raidp[array] = (struct ar_softc *)kmalloc(sizeof(struct ar_softc),
2402 M_AR, M_WAITOK | M_ZERO);
2405 switch (meta->type) {
2406 case ITE_T_RAID0:
2407 raid->type = AR_T_RAID0;
2408 raid->width = meta->array_width;
2409 raid->total_disks = meta->array_width;
2410 disk_number = meta->disk_number;
2411 break;
2413 case ITE_T_RAID1:
2414 raid->type = AR_T_RAID1;
2415 raid->width = 1;
2416 raid->total_disks = 2;
2417 disk_number = meta->disk_number;
2418 break;
2420 case ITE_T_RAID01:
2421 raid->type = AR_T_RAID01;
2422 raid->width = meta->array_width;
2423 raid->total_disks = 4;
2424 disk_number = ((meta->disk_number & 0x02) >> 1) |
2425 ((meta->disk_number & 0x01) << 1);
2426 break;
2428 case ITE_T_SPAN:
2429 raid->type = AR_T_SPAN;
2430 raid->width = 1;
2431 raid->total_disks = meta->array_width;
2432 disk_number = meta->disk_number;
2433 break;
2435 default:
2436 device_printf(parent, "ITE unknown RAID type 0x%02x\n", meta->type);
2437 kfree(raidp[array], M_AR);
2438 raidp[array] = NULL;
2439 goto ite_out;
2442 raid->magic_0 = *((u_int64_t *)meta->timestamp_0);
2443 raid->format = AR_F_ITE_RAID;
2444 raid->generation = 0;
2445 raid->interleave = meta->stripe_sectors;
2446 raid->total_sectors = meta->total_sectors;
2447 raid->heads = 255;
2448 raid->sectors = 63;
2449 raid->cylinders = raid->total_sectors / (63 * 255);
2450 raid->offset_sectors = 0;
2451 raid->rebuild_lba = 0;
2452 raid->lun = array;
2454 raid->disks[disk_number].dev = parent;
2455 raid->disks[disk_number].sectors = raid->total_sectors / raid->width;
2456 raid->disks[disk_number].flags =
2457 (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_ONLINE);
2458 ars->raid[raid->volume] = raid;
2459 ars->disk_number[raid->volume] = disk_number;
2460 retval = 1;
2461 break;
2463 ite_out:
2464 kfree(meta, M_AR);
2465 return retval;
2468 /* JMicron Technology Corp Metadata */
2469 static int
2470 ata_raid_jmicron_read_meta(device_t dev, struct ar_softc **raidp)
2472 struct ata_raid_subdisk *ars = device_get_softc(dev);
2473 device_t parent = device_get_parent(dev);
2474 struct jmicron_raid_conf *meta;
2475 struct ar_softc *raid = NULL;
2476 u_int16_t checksum, *ptr;
2477 u_int64_t disk_size;
2478 int count, array, disk, total_disks, retval = 0;
2480 meta = (struct jmicron_raid_conf *)
2481 kmalloc(sizeof(struct jmicron_raid_conf), M_AR, M_WAITOK | M_ZERO);
2483 if (ata_raid_rw(parent, JMICRON_LBA(parent),
2484 meta, sizeof(struct jmicron_raid_conf), ATA_R_READ)) {
2485 if (testing || bootverbose)
2486 device_printf(parent,
2487 "JMicron read metadata failed\n");
2490 /* check for JMicron signature */
2491 if (strncmp(meta->signature, JMICRON_MAGIC, 2)) {
2492 if (testing || bootverbose)
2493 device_printf(parent, "JMicron check1 failed\n");
2494 goto jmicron_out;
2497 /* calculate checksum and compare for valid */
2498 for (checksum = 0, ptr = (u_int16_t *)meta, count = 0; count < 64; count++)
2499 checksum += *ptr++;
2500 if (checksum) {
2501 if (testing || bootverbose)
2502 device_printf(parent, "JMicron check2 failed\n");
2503 goto jmicron_out;
2506 if (testing || bootverbose)
2507 ata_raid_jmicron_print_meta(meta);
2509 /* now convert JMicron meta into our generic form */
2510 for (array = 0; array < MAX_ARRAYS; array++) {
2511 jmicron_next:
2512 if (!raidp[array]) {
2513 raidp[array] =
2514 (struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
2515 M_WAITOK | M_ZERO);
2517 raid = raidp[array];
2518 if (raid->format && (raid->format != AR_F_JMICRON_RAID))
2519 continue;
2521 for (total_disks = 0, disk = 0; disk < JM_MAX_DISKS; disk++) {
2522 if (meta->disks[disk]) {
2523 if (raid->format == AR_F_JMICRON_RAID) {
2524 if (bcmp(&meta->disks[disk],
2525 raid->disks[disk].serial, sizeof(u_int32_t))) {
2526 array++;
2527 goto jmicron_next;
2530 else
2531 bcopy(&meta->disks[disk],
2532 raid->disks[disk].serial, sizeof(u_int32_t));
2533 total_disks++;
2536 /* handle spares XXX SOS */
2538 switch (meta->type) {
2539 case JM_T_RAID0:
2540 raid->type = AR_T_RAID0;
2541 raid->width = total_disks;
2542 break;
2544 case JM_T_RAID1:
2545 raid->type = AR_T_RAID1;
2546 raid->width = 1;
2547 break;
2549 case JM_T_RAID01:
2550 raid->type = AR_T_RAID01;
2551 raid->width = total_disks / 2;
2552 break;
2554 case JM_T_RAID5:
2555 raid->type = AR_T_RAID5;
2556 raid->width = total_disks;
2557 break;
2559 case JM_T_JBOD:
2560 raid->type = AR_T_SPAN;
2561 raid->width = 1;
2562 break;
2564 default:
2565 device_printf(parent,
2566 "JMicron unknown RAID type 0x%02x\n", meta->type);
2567 kfree(raidp[array], M_AR);
2568 raidp[array] = NULL;
2569 goto jmicron_out;
2571 disk_size = (meta->disk_sectors_high << 16) + meta->disk_sectors_low;
2572 raid->format = AR_F_JMICRON_RAID;
2573 strncpy(raid->name, meta->name, sizeof(meta->name));
2574 raid->generation = 0;
2575 raid->interleave = 2 << meta->stripe_shift;
2576 raid->total_disks = total_disks;
2577 raid->total_sectors = disk_size * (raid->width-(raid->type==AR_RAID5));
2578 raid->heads = 255;
2579 raid->sectors = 63;
2580 raid->cylinders = raid->total_sectors / (63 * 255);
2581 raid->offset_sectors = meta->offset * 16;
2582 raid->rebuild_lba = 0;
2583 raid->lun = array;
2585 for (disk = 0; disk < raid->total_disks; disk++) {
2586 if (meta->disks[disk] == meta->disk_id) {
2587 raid->disks[disk].dev = parent;
2588 raid->disks[disk].sectors = disk_size;
2589 raid->disks[disk].flags =
2590 (AR_DF_ONLINE | AR_DF_PRESENT | AR_DF_ASSIGNED);
2591 ars->raid[raid->volume] = raid;
2592 ars->disk_number[raid->volume] = disk;
2593 retval = 1;
2594 break;
2597 break;
2599 jmicron_out:
2600 kfree(meta, M_AR);
2601 return retval;
2604 static int
2605 ata_raid_jmicron_write_meta(struct ar_softc *rdp)
2607 struct jmicron_raid_conf *meta;
2608 u_int64_t disk_sectors;
2609 int disk, error = 0;
2611 meta = (struct jmicron_raid_conf *)
2612 kmalloc(sizeof(struct jmicron_raid_conf), M_AR, M_WAITOK | M_ZERO);
2614 rdp->generation++;
2615 switch (rdp->type) {
2616 case AR_T_JBOD:
2617 meta->type = JM_T_JBOD;
2618 break;
2620 case AR_T_RAID0:
2621 meta->type = JM_T_RAID0;
2622 break;
2624 case AR_T_RAID1:
2625 meta->type = JM_T_RAID1;
2626 break;
2628 case AR_T_RAID5:
2629 meta->type = JM_T_RAID5;
2630 break;
2632 case AR_T_RAID01:
2633 meta->type = JM_T_RAID01;
2634 break;
2636 default:
2637 kfree(meta, M_AR);
2638 return ENODEV;
2640 bcopy(JMICRON_MAGIC, meta->signature, sizeof(JMICRON_MAGIC));
2641 meta->version = JMICRON_VERSION;
2642 meta->offset = rdp->offset_sectors / 16;
2643 disk_sectors = rdp->total_sectors / (rdp->width - (rdp->type == AR_RAID5));
2644 meta->disk_sectors_low = disk_sectors & 0xffff;
2645 meta->disk_sectors_high = disk_sectors >> 16;
2646 strncpy(meta->name, rdp->name, sizeof(meta->name));
2647 meta->stripe_shift = ffs(rdp->interleave) - 2;
2649 for (disk = 0; disk < rdp->total_disks; disk++) {
2650 if (rdp->disks[disk].serial[0])
2651 bcopy(rdp->disks[disk].serial,&meta->disks[disk],sizeof(u_int32_t));
2652 else
2653 meta->disks[disk] = (u_int32_t)(uintptr_t)rdp->disks[disk].dev;
2656 for (disk = 0; disk < rdp->total_disks; disk++) {
2657 if (rdp->disks[disk].dev) {
2658 u_int16_t checksum = 0, *ptr;
2659 int count;
2661 meta->disk_id = meta->disks[disk];
2662 meta->checksum = 0;
2663 for (ptr = (u_int16_t *)meta, count = 0; count < 64; count++)
2664 checksum += *ptr++;
2665 meta->checksum -= checksum;
2667 if (testing || bootverbose)
2668 ata_raid_jmicron_print_meta(meta);
2670 if (ata_raid_rw(rdp->disks[disk].dev,
2671 JMICRON_LBA(rdp->disks[disk].dev),
2672 meta, sizeof(struct jmicron_raid_conf),
2673 ATA_R_WRITE | ATA_R_DIRECT)) {
2674 device_printf(rdp->disks[disk].dev, "write metadata failed\n");
2675 error = EIO;
2679 /* handle spares XXX SOS */
2681 kfree(meta, M_AR);
2682 return error;
2685 /* LSILogic V2 MegaRAID Metadata */
2686 static int
2687 ata_raid_lsiv2_read_meta(device_t dev, struct ar_softc **raidp)
2689 struct ata_raid_subdisk *ars = device_get_softc(dev);
2690 device_t parent = device_get_parent(dev);
2691 struct lsiv2_raid_conf *meta;
2692 struct ar_softc *raid = NULL;
2693 int array, retval = 0;
2695 meta = (struct lsiv2_raid_conf *)kmalloc(sizeof(struct lsiv2_raid_conf),
2696 M_AR, M_WAITOK | M_ZERO);
2698 if (ata_raid_rw(parent, LSIV2_LBA(parent),
2699 meta, sizeof(struct lsiv2_raid_conf), ATA_R_READ)) {
2700 if (testing || bootverbose)
2701 device_printf(parent, "LSI (v2) read metadata failed\n");
2702 goto lsiv2_out;
2705 /* check if this is a LSI RAID struct */
2706 if (strncmp(meta->lsi_id, LSIV2_MAGIC, strlen(LSIV2_MAGIC))) {
2707 if (testing || bootverbose)
2708 device_printf(parent, "LSI (v2) check1 failed\n");
2709 goto lsiv2_out;
2712 if (testing || bootverbose)
2713 ata_raid_lsiv2_print_meta(meta);
2715 /* now convert LSI (v2) config meta into our generic form */
2716 for (array = 0; array < MAX_ARRAYS; array++) {
2717 int raid_entry, conf_entry;
2719 if (!raidp[array + meta->raid_number]) {
2720 raidp[array + meta->raid_number] =
2721 (struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
2722 M_WAITOK | M_ZERO);
2724 raid = raidp[array + meta->raid_number];
2725 if (raid->format && (raid->format != AR_F_LSIV2_RAID))
2726 continue;
2728 if (raid->magic_0 &&
2729 ((raid->magic_0 != meta->timestamp) ||
2730 (raid->magic_1 != meta->raid_number)))
2731 continue;
2733 array += meta->raid_number;
2735 raid_entry = meta->raid_number;
2736 conf_entry = (meta->configs[raid_entry].raid.config_offset >> 4) +
2737 meta->disk_number - 1;
2739 switch (meta->configs[raid_entry].raid.type) {
2740 case LSIV2_T_RAID0:
2741 raid->magic_0 = meta->timestamp;
2742 raid->magic_1 = meta->raid_number;
2743 raid->type = AR_T_RAID0;
2744 raid->interleave = meta->configs[raid_entry].raid.stripe_sectors;
2745 raid->width = meta->configs[raid_entry].raid.array_width;
2746 break;
2748 case LSIV2_T_RAID1:
2749 raid->magic_0 = meta->timestamp;
2750 raid->magic_1 = meta->raid_number;
2751 raid->type = AR_T_RAID1;
2752 raid->width = meta->configs[raid_entry].raid.array_width;
2753 break;
2755 case LSIV2_T_RAID0 | LSIV2_T_RAID1:
2756 raid->magic_0 = meta->timestamp;
2757 raid->magic_1 = meta->raid_number;
2758 raid->type = AR_T_RAID01;
2759 raid->interleave = meta->configs[raid_entry].raid.stripe_sectors;
2760 raid->width = meta->configs[raid_entry].raid.array_width;
2761 break;
2763 default:
2764 device_printf(parent, "LSI v2 unknown RAID type 0x%02x\n",
2765 meta->configs[raid_entry].raid.type);
2766 kfree(raidp[array], M_AR);
2767 raidp[array] = NULL;
2768 goto lsiv2_out;
2771 raid->format = AR_F_LSIV2_RAID;
2772 raid->generation = 0;
2773 raid->total_disks = meta->configs[raid_entry].raid.disk_count;
2774 raid->total_sectors = meta->configs[raid_entry].raid.total_sectors;
2775 raid->heads = 255;
2776 raid->sectors = 63;
2777 raid->cylinders = raid->total_sectors / (63 * 255);
2778 raid->offset_sectors = 0;
2779 raid->rebuild_lba = 0;
2780 raid->lun = array;
2782 if (meta->configs[conf_entry].disk.device != LSIV2_D_NONE) {
2783 raid->disks[meta->disk_number].dev = parent;
2784 raid->disks[meta->disk_number].sectors =
2785 meta->configs[conf_entry].disk.disk_sectors;
2786 raid->disks[meta->disk_number].flags =
2787 (AR_DF_ONLINE | AR_DF_PRESENT | AR_DF_ASSIGNED);
2788 ars->raid[raid->volume] = raid;
2789 ars->disk_number[raid->volume] = meta->disk_number;
2790 retval = 1;
2792 else
2793 raid->disks[meta->disk_number].flags &= ~AR_DF_ONLINE;
2795 break;
2798 lsiv2_out:
2799 kfree(meta, M_AR);
2800 return retval;
2803 /* LSILogic V3 MegaRAID Metadata */
2804 static int
2805 ata_raid_lsiv3_read_meta(device_t dev, struct ar_softc **raidp)
2807 struct ata_raid_subdisk *ars = device_get_softc(dev);
2808 device_t parent = device_get_parent(dev);
2809 struct lsiv3_raid_conf *meta;
2810 struct ar_softc *raid = NULL;
2811 u_int8_t checksum, *ptr;
2812 int array, entry, count, disk_number, retval = 0;
2814 meta = (struct lsiv3_raid_conf *)kmalloc(sizeof(struct lsiv3_raid_conf),
2815 M_AR, M_WAITOK | M_ZERO);
2817 if (ata_raid_rw(parent, LSIV3_LBA(parent),
2818 meta, sizeof(struct lsiv3_raid_conf), ATA_R_READ)) {
2819 if (testing || bootverbose)
2820 device_printf(parent, "LSI (v3) read metadata failed\n");
2821 goto lsiv3_out;
2824 /* check if this is a LSI RAID struct */
2825 if (strncmp(meta->lsi_id, LSIV3_MAGIC, strlen(LSIV3_MAGIC))) {
2826 if (testing || bootverbose)
2827 device_printf(parent, "LSI (v3) check1 failed\n");
2828 goto lsiv3_out;
2831 /* check if the checksum is OK */
2832 for (checksum = 0, ptr = meta->lsi_id, count = 0; count < 512; count++)
2833 checksum += *ptr++;
2834 if (checksum) {
2835 if (testing || bootverbose)
2836 device_printf(parent, "LSI (v3) check2 failed\n");
2837 goto lsiv3_out;
2840 if (testing || bootverbose)
2841 ata_raid_lsiv3_print_meta(meta);
2843 /* now convert LSI (v3) config meta into our generic form */
2844 for (array = 0, entry = 0; array < MAX_ARRAYS && entry < 8;) {
2845 if (!raidp[array]) {
2846 raidp[array] =
2847 (struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
2848 M_WAITOK | M_ZERO);
2850 raid = raidp[array];
2851 if (raid->format && (raid->format != AR_F_LSIV3_RAID)) {
2852 array++;
2853 continue;
2856 if ((raid->format == AR_F_LSIV3_RAID) &&
2857 (raid->magic_0 != meta->timestamp)) {
2858 array++;
2859 continue;
2862 switch (meta->raid[entry].total_disks) {
2863 case 0:
2864 entry++;
2865 continue;
2866 case 1:
2867 if (meta->raid[entry].device == meta->device) {
2868 disk_number = 0;
2869 break;
2871 if (raid->format)
2872 array++;
2873 entry++;
2874 continue;
2875 case 2:
2876 disk_number = (meta->device & (LSIV3_D_DEVICE|LSIV3_D_CHANNEL))?1:0;
2877 break;
2878 default:
2879 device_printf(parent, "lsiv3 > 2 disk support untested!!\n");
2880 disk_number = (meta->device & LSIV3_D_DEVICE ? 1 : 0) +
2881 (meta->device & LSIV3_D_CHANNEL ? 2 : 0);
2882 break;
2885 switch (meta->raid[entry].type) {
2886 case LSIV3_T_RAID0:
2887 raid->type = AR_T_RAID0;
2888 raid->width = meta->raid[entry].total_disks;
2889 break;
2891 case LSIV3_T_RAID1:
2892 raid->type = AR_T_RAID1;
2893 raid->width = meta->raid[entry].array_width;
2894 break;
2896 default:
2897 device_printf(parent, "LSI v3 unknown RAID type 0x%02x\n",
2898 meta->raid[entry].type);
2899 kfree(raidp[array], M_AR);
2900 raidp[array] = NULL;
2901 entry++;
2902 continue;
2905 raid->magic_0 = meta->timestamp;
2906 raid->format = AR_F_LSIV3_RAID;
2907 raid->generation = 0;
2908 raid->interleave = meta->raid[entry].stripe_pages * 8;
2909 raid->total_disks = meta->raid[entry].total_disks;
2910 raid->total_sectors = raid->width * meta->raid[entry].sectors;
2911 raid->heads = 255;
2912 raid->sectors = 63;
2913 raid->cylinders = raid->total_sectors / (63 * 255);
2914 raid->offset_sectors = meta->raid[entry].offset;
2915 raid->rebuild_lba = 0;
2916 raid->lun = array;
2918 raid->disks[disk_number].dev = parent;
2919 raid->disks[disk_number].sectors = raid->total_sectors / raid->width;
2920 raid->disks[disk_number].flags =
2921 (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_ONLINE);
2922 ars->raid[raid->volume] = raid;
2923 ars->disk_number[raid->volume] = disk_number;
2924 retval = 1;
2925 entry++;
2926 array++;
2929 lsiv3_out:
2930 kfree(meta, M_AR);
2931 return retval;
2934 /* nVidia MediaShield Metadata */
2935 static int
2936 ata_raid_nvidia_read_meta(device_t dev, struct ar_softc **raidp)
2938 struct ata_raid_subdisk *ars = device_get_softc(dev);
2939 device_t parent = device_get_parent(dev);
2940 struct nvidia_raid_conf *meta;
2941 struct ar_softc *raid = NULL;
2942 u_int32_t checksum, *ptr;
2943 int array, count, retval = 0;
2945 meta = (struct nvidia_raid_conf *)kmalloc(sizeof(struct nvidia_raid_conf),
2946 M_AR, M_WAITOK | M_ZERO);
2948 if (ata_raid_rw(parent, NVIDIA_LBA(parent),
2949 meta, sizeof(struct nvidia_raid_conf), ATA_R_READ)) {
2950 if (testing || bootverbose)
2951 device_printf(parent, "nVidia read metadata failed\n");
2952 goto nvidia_out;
2955 /* check if this is a nVidia RAID struct */
2956 if (strncmp(meta->nvidia_id, NV_MAGIC, strlen(NV_MAGIC))) {
2957 if (testing || bootverbose)
2958 device_printf(parent, "nVidia check1 failed\n");
2959 goto nvidia_out;
2962 /* check if the checksum is OK */
2963 for (checksum = 0, ptr = (u_int32_t*)meta, count = 0;
2964 count < meta->config_size; count++)
2965 checksum += *ptr++;
2966 if (checksum) {
2967 if (testing || bootverbose)
2968 device_printf(parent, "nVidia check2 failed\n");
2969 goto nvidia_out;
2972 if (testing || bootverbose)
2973 ata_raid_nvidia_print_meta(meta);
2975 /* now convert nVidia meta into our generic form */
2976 for (array = 0; array < MAX_ARRAYS; array++) {
2977 if (!raidp[array]) {
2978 raidp[array] =
2979 (struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
2980 M_WAITOK | M_ZERO);
2982 raid = raidp[array];
2983 if (raid->format && (raid->format != AR_F_NVIDIA_RAID))
2984 continue;
2986 if (raid->format == AR_F_NVIDIA_RAID &&
2987 ((raid->magic_0 != meta->magic_1) ||
2988 (raid->magic_1 != meta->magic_2))) {
2989 continue;
2992 switch (meta->type) {
2993 case NV_T_SPAN:
2994 raid->type = AR_T_SPAN;
2995 break;
2997 case NV_T_RAID0:
2998 raid->type = AR_T_RAID0;
2999 break;
3001 case NV_T_RAID1:
3002 raid->type = AR_T_RAID1;
3003 break;
3005 case NV_T_RAID5:
3006 raid->type = AR_T_RAID5;
3007 break;
3009 case NV_T_RAID01:
3010 raid->type = AR_T_RAID01;
3011 break;
3013 default:
3014 device_printf(parent, "nVidia unknown RAID type 0x%02x\n",
3015 meta->type);
3016 kfree(raidp[array], M_AR);
3017 raidp[array] = NULL;
3018 goto nvidia_out;
3020 raid->magic_0 = meta->magic_1;
3021 raid->magic_1 = meta->magic_2;
3022 raid->format = AR_F_NVIDIA_RAID;
3023 raid->generation = 0;
3024 raid->interleave = meta->stripe_sectors;
3025 raid->width = meta->array_width;
3026 raid->total_disks = meta->total_disks;
3027 raid->total_sectors = meta->total_sectors;
3028 raid->heads = 255;
3029 raid->sectors = 63;
3030 raid->cylinders = raid->total_sectors / (63 * 255);
3031 raid->offset_sectors = 0;
3032 raid->rebuild_lba = meta->rebuild_lba;
3033 raid->lun = array;
3034 raid->status = AR_S_READY;
3035 if (meta->status & NV_S_DEGRADED)
3036 raid->status |= AR_S_DEGRADED;
3038 raid->disks[meta->disk_number].dev = parent;
3039 raid->disks[meta->disk_number].sectors =
3040 raid->total_sectors / raid->width;
3041 raid->disks[meta->disk_number].flags =
3042 (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_ONLINE);
3043 ars->raid[raid->volume] = raid;
3044 ars->disk_number[raid->volume] = meta->disk_number;
3045 retval = 1;
3046 break;
3049 nvidia_out:
3050 kfree(meta, M_AR);
3051 return retval;
3054 /* Promise FastTrak Metadata */
3055 static int
3056 ata_raid_promise_read_meta(device_t dev, struct ar_softc **raidp, int native)
3058 struct ata_raid_subdisk *ars = device_get_softc(dev);
3059 device_t parent = device_get_parent(dev);
3060 struct promise_raid_conf *meta;
3061 struct ar_softc *raid;
3062 u_int32_t checksum, *ptr;
3063 int array, count, disk, disksum = 0, retval = 0;
3065 meta = (struct promise_raid_conf *)
3066 kmalloc(sizeof(struct promise_raid_conf), M_AR, M_WAITOK | M_ZERO);
3068 if (ata_raid_rw(parent, PROMISE_LBA(parent),
3069 meta, sizeof(struct promise_raid_conf), ATA_R_READ)) {
3070 if (testing || bootverbose)
3071 device_printf(parent, "%s read metadata failed\n",
3072 native ? "FreeBSD" : "Promise");
3073 goto promise_out;
3076 /* check the signature */
3077 if (native) {
3078 if (strncmp(meta->promise_id, ATA_MAGIC, strlen(ATA_MAGIC))) {
3079 if (testing || bootverbose)
3080 device_printf(parent, "FreeBSD check1 failed\n");
3081 goto promise_out;
3084 else {
3085 if (strncmp(meta->promise_id, PR_MAGIC, strlen(PR_MAGIC))) {
3086 if (testing || bootverbose)
3087 device_printf(parent, "Promise check1 failed\n");
3088 goto promise_out;
3092 /* check if the checksum is OK */
3093 for (checksum = 0, ptr = (u_int32_t *)meta, count = 0; count < 511; count++)
3094 checksum += *ptr++;
3095 if (checksum != *ptr) {
3096 if (testing || bootverbose)
3097 device_printf(parent, "%s check2 failed\n",
3098 native ? "FreeBSD" : "Promise");
3099 goto promise_out;
3102 /* check on disk integrity status */
3103 if (meta->raid.integrity != PR_I_VALID) {
3104 if (testing || bootverbose)
3105 device_printf(parent, "%s check3 failed\n",
3106 native ? "FreeBSD" : "Promise");
3107 goto promise_out;
3110 if (testing || bootverbose)
3111 ata_raid_promise_print_meta(meta);
3113 /* now convert Promise metadata into our generic form */
3114 for (array = 0; array < MAX_ARRAYS; array++) {
3115 if (!raidp[array]) {
3116 raidp[array] =
3117 (struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
3118 M_WAITOK | M_ZERO);
3120 raid = raidp[array];
3121 if (raid->format &&
3122 (raid->format != (native ? AR_F_FREEBSD_RAID : AR_F_PROMISE_RAID)))
3123 continue;
3125 if ((raid->format == (native ? AR_F_FREEBSD_RAID : AR_F_PROMISE_RAID))&&
3126 !(meta->raid.magic_1 == (raid->magic_1)))
3127 continue;
3129 /* update our knowledge about the array config based on generation */
3130 if (!meta->raid.generation || meta->raid.generation > raid->generation){
3131 switch (meta->raid.type) {
3132 case PR_T_SPAN:
3133 raid->type = AR_T_SPAN;
3134 break;
3136 case PR_T_JBOD:
3137 raid->type = AR_T_JBOD;
3138 break;
3140 case PR_T_RAID0:
3141 raid->type = AR_T_RAID0;
3142 break;
3144 case PR_T_RAID1:
3145 raid->type = AR_T_RAID1;
3146 if (meta->raid.array_width > 1)
3147 raid->type = AR_T_RAID01;
3148 break;
3150 case PR_T_RAID5:
3151 raid->type = AR_T_RAID5;
3152 break;
3154 default:
3155 device_printf(parent, "%s unknown RAID type 0x%02x\n",
3156 native ? "FreeBSD" : "Promise", meta->raid.type);
3157 kfree(raidp[array], M_AR);
3158 raidp[array] = NULL;
3159 goto promise_out;
3161 raid->magic_1 = meta->raid.magic_1;
3162 raid->format = (native ? AR_F_FREEBSD_RAID : AR_F_PROMISE_RAID);
3163 raid->generation = meta->raid.generation;
3164 raid->interleave = 1 << meta->raid.stripe_shift;
3165 raid->width = meta->raid.array_width;
3166 raid->total_disks = meta->raid.total_disks;
3167 raid->heads = meta->raid.heads + 1;
3168 raid->sectors = meta->raid.sectors;
3169 raid->cylinders = meta->raid.cylinders + 1;
3170 raid->total_sectors = meta->raid.total_sectors;
3171 raid->offset_sectors = 0;
3172 raid->rebuild_lba = meta->raid.rebuild_lba;
3173 raid->lun = array;
3174 if ((meta->raid.status &
3175 (PR_S_VALID | PR_S_ONLINE | PR_S_INITED | PR_S_READY)) ==
3176 (PR_S_VALID | PR_S_ONLINE | PR_S_INITED | PR_S_READY)) {
3177 raid->status |= AR_S_READY;
3178 if (meta->raid.status & PR_S_DEGRADED)
3179 raid->status |= AR_S_DEGRADED;
3181 else
3182 raid->status &= ~AR_S_READY;
3184 /* convert disk flags to our internal types */
3185 for (disk = 0; disk < meta->raid.total_disks; disk++) {
3186 raid->disks[disk].dev = NULL;
3187 raid->disks[disk].flags = 0;
3188 *((u_int64_t *)(raid->disks[disk].serial)) =
3189 meta->raid.disk[disk].magic_0;
3190 disksum += meta->raid.disk[disk].flags;
3191 if (meta->raid.disk[disk].flags & PR_F_ONLINE)
3192 raid->disks[disk].flags |= AR_DF_ONLINE;
3193 if (meta->raid.disk[disk].flags & PR_F_ASSIGNED)
3194 raid->disks[disk].flags |= AR_DF_ASSIGNED;
3195 if (meta->raid.disk[disk].flags & PR_F_SPARE) {
3196 raid->disks[disk].flags &= ~(AR_DF_ONLINE | AR_DF_ASSIGNED);
3197 raid->disks[disk].flags |= AR_DF_SPARE;
3199 if (meta->raid.disk[disk].flags & (PR_F_REDIR | PR_F_DOWN))
3200 raid->disks[disk].flags &= ~AR_DF_ONLINE;
3202 if (!disksum) {
3203 device_printf(parent, "%s subdisks has no flags\n",
3204 native ? "FreeBSD" : "Promise");
3205 kfree(raidp[array], M_AR);
3206 raidp[array] = NULL;
3207 goto promise_out;
3210 if (meta->raid.generation >= raid->generation) {
3211 int disk_number = meta->raid.disk_number;
3213 if (raid->disks[disk_number].flags && (meta->magic_0 ==
3214 *((u_int64_t *)(raid->disks[disk_number].serial)))) {
3215 raid->disks[disk_number].dev = parent;
3216 raid->disks[disk_number].flags |= AR_DF_PRESENT;
3217 raid->disks[disk_number].sectors = meta->raid.disk_sectors;
3218 if ((raid->disks[disk_number].flags &
3219 (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_ONLINE)) ==
3220 (AR_DF_PRESENT | AR_DF_ASSIGNED | AR_DF_ONLINE)) {
3221 ars->raid[raid->volume] = raid;
3222 ars->disk_number[raid->volume] = disk_number;
3223 retval = 1;
3227 break;
3230 promise_out:
3231 kfree(meta, M_AR);
3232 return retval;
3235 static int
3236 ata_raid_promise_write_meta(struct ar_softc *rdp)
3238 struct promise_raid_conf *meta;
3239 struct timeval timestamp;
3240 u_int32_t *ckptr;
3241 int count, disk, drive, error = 0;
3243 meta = (struct promise_raid_conf *)
3244 kmalloc(sizeof(struct promise_raid_conf), M_AR, M_WAITOK);
3246 rdp->generation++;
3247 microtime(&timestamp);
3249 for (disk = 0; disk < rdp->total_disks; disk++) {
3250 for (count = 0; count < sizeof(struct promise_raid_conf); count++)
3251 *(((u_int8_t *)meta) + count) = 255 - (count % 256);
3252 meta->dummy_0 = 0x00020000;
3253 meta->raid.disk_number = disk;
3255 if (rdp->disks[disk].dev) {
3256 struct ata_device *atadev = device_get_softc(rdp->disks[disk].dev);
3257 struct ata_channel *ch =
3258 device_get_softc(device_get_parent(rdp->disks[disk].dev));
3260 meta->raid.channel = ch->unit;
3261 meta->raid.device = ATA_DEV(atadev->unit);
3262 meta->raid.disk_sectors = rdp->disks[disk].sectors;
3263 meta->raid.disk_offset = rdp->offset_sectors;
3265 else {
3266 meta->raid.channel = 0;
3267 meta->raid.device = 0;
3268 meta->raid.disk_sectors = 0;
3269 meta->raid.disk_offset = 0;
3271 meta->magic_0 = PR_MAGIC0(meta->raid) | timestamp.tv_sec;
3272 meta->magic_1 = timestamp.tv_sec >> 16;
3273 meta->magic_2 = timestamp.tv_sec;
3274 meta->raid.integrity = PR_I_VALID;
3275 meta->raid.magic_0 = meta->magic_0;
3276 meta->raid.rebuild_lba = rdp->rebuild_lba;
3277 meta->raid.generation = rdp->generation;
3279 if (rdp->status & AR_S_READY) {
3280 meta->raid.flags = (PR_F_VALID | PR_F_ASSIGNED | PR_F_ONLINE);
3281 meta->raid.status =
3282 (PR_S_VALID | PR_S_ONLINE | PR_S_INITED | PR_S_READY);
3283 if (rdp->status & AR_S_DEGRADED)
3284 meta->raid.status |= PR_S_DEGRADED;
3285 else
3286 meta->raid.status |= PR_S_FUNCTIONAL;
3288 else {
3289 meta->raid.flags = PR_F_DOWN;
3290 meta->raid.status = 0;
3293 switch (rdp->type) {
3294 case AR_T_RAID0:
3295 meta->raid.type = PR_T_RAID0;
3296 break;
3297 case AR_T_RAID1:
3298 meta->raid.type = PR_T_RAID1;
3299 break;
3300 case AR_T_RAID01:
3301 meta->raid.type = PR_T_RAID1;
3302 break;
3303 case AR_T_RAID5:
3304 meta->raid.type = PR_T_RAID5;
3305 break;
3306 case AR_T_SPAN:
3307 meta->raid.type = PR_T_SPAN;
3308 break;
3309 case AR_T_JBOD:
3310 meta->raid.type = PR_T_JBOD;
3311 break;
3312 default:
3313 kfree(meta, M_AR);
3314 return ENODEV;
3317 meta->raid.total_disks = rdp->total_disks;
3318 meta->raid.stripe_shift = ffs(rdp->interleave) - 1;
3319 meta->raid.array_width = rdp->width;
3320 meta->raid.array_number = rdp->lun;
3321 meta->raid.total_sectors = rdp->total_sectors;
3322 meta->raid.cylinders = rdp->cylinders - 1;
3323 meta->raid.heads = rdp->heads - 1;
3324 meta->raid.sectors = rdp->sectors;
3325 meta->raid.magic_1 = (u_int64_t)meta->magic_2<<16 | meta->magic_1;
3327 bzero(&meta->raid.disk, 8 * 12);
3328 for (drive = 0; drive < rdp->total_disks; drive++) {
3329 meta->raid.disk[drive].flags = 0;
3330 if (rdp->disks[drive].flags & AR_DF_PRESENT)
3331 meta->raid.disk[drive].flags |= PR_F_VALID;
3332 if (rdp->disks[drive].flags & AR_DF_ASSIGNED)
3333 meta->raid.disk[drive].flags |= PR_F_ASSIGNED;
3334 if (rdp->disks[drive].flags & AR_DF_ONLINE)
3335 meta->raid.disk[drive].flags |= PR_F_ONLINE;
3336 else
3337 if (rdp->disks[drive].flags & AR_DF_PRESENT)
3338 meta->raid.disk[drive].flags = (PR_F_REDIR | PR_F_DOWN);
3339 if (rdp->disks[drive].flags & AR_DF_SPARE)
3340 meta->raid.disk[drive].flags |= PR_F_SPARE;
3341 meta->raid.disk[drive].dummy_0 = 0x0;
3342 if (rdp->disks[drive].dev) {
3343 struct ata_channel *ch =
3344 device_get_softc(device_get_parent(rdp->disks[drive].dev));
3345 struct ata_device *atadev =
3346 device_get_softc(rdp->disks[drive].dev);
3348 meta->raid.disk[drive].channel = ch->unit;
3349 meta->raid.disk[drive].device = ATA_DEV(atadev->unit);
3351 meta->raid.disk[drive].magic_0 =
3352 PR_MAGIC0(meta->raid.disk[drive]) | timestamp.tv_sec;
3355 if (rdp->disks[disk].dev) {
3356 if ((rdp->disks[disk].flags & (AR_DF_PRESENT | AR_DF_ONLINE)) ==
3357 (AR_DF_PRESENT | AR_DF_ONLINE)) {
3358 if (rdp->format == AR_F_FREEBSD_RAID)
3359 bcopy(ATA_MAGIC, meta->promise_id, sizeof(ATA_MAGIC));
3360 else
3361 bcopy(PR_MAGIC, meta->promise_id, sizeof(PR_MAGIC));
3363 else
3364 bzero(meta->promise_id, sizeof(meta->promise_id));
3365 meta->checksum = 0;
3366 for (ckptr = (int32_t *)meta, count = 0; count < 511; count++)
3367 meta->checksum += *ckptr++;
3368 if (testing || bootverbose)
3369 ata_raid_promise_print_meta(meta);
3370 if (ata_raid_rw(rdp->disks[disk].dev,
3371 PROMISE_LBA(rdp->disks[disk].dev),
3372 meta, sizeof(struct promise_raid_conf),
3373 ATA_R_WRITE | ATA_R_DIRECT)) {
3374 device_printf(rdp->disks[disk].dev, "write metadata failed\n");
3375 error = EIO;
3379 kfree(meta, M_AR);
3380 return error;
3383 /* Silicon Image Medley Metadata */
3384 static int
3385 ata_raid_sii_read_meta(device_t dev, struct ar_softc **raidp)
3387 struct ata_raid_subdisk *ars = device_get_softc(dev);
3388 device_t parent = device_get_parent(dev);
3389 struct sii_raid_conf *meta;
3390 struct ar_softc *raid = NULL;
3391 u_int16_t checksum, *ptr;
3392 int array, count, disk, retval = 0;
3394 meta = (struct sii_raid_conf *)kmalloc(sizeof(struct sii_raid_conf), M_AR,
3395 M_WAITOK | M_ZERO);
3397 if (ata_raid_rw(parent, SII_LBA(parent),
3398 meta, sizeof(struct sii_raid_conf), ATA_R_READ)) {
3399 if (testing || bootverbose)
3400 device_printf(parent, "Silicon Image read metadata failed\n");
3401 goto sii_out;
3404 /* check if this is a Silicon Image (Medley) RAID struct */
3405 for (checksum = 0, ptr = (u_int16_t *)meta, count = 0; count < 160; count++)
3406 checksum += *ptr++;
3407 if (checksum) {
3408 if (testing || bootverbose)
3409 device_printf(parent, "Silicon Image check1 failed\n");
3410 goto sii_out;
3413 for (checksum = 0, ptr = (u_int16_t *)meta, count = 0; count < 256; count++)
3414 checksum += *ptr++;
3415 if (checksum != meta->checksum_1) {
3416 if (testing || bootverbose)
3417 device_printf(parent, "Silicon Image check2 failed\n");
3418 goto sii_out;
3421 /* check verison */
3422 if (meta->version_major != 0x0002 ||
3423 (meta->version_minor != 0x0000 && meta->version_minor != 0x0001)) {
3424 if (testing || bootverbose)
3425 device_printf(parent, "Silicon Image check3 failed\n");
3426 goto sii_out;
3429 if (testing || bootverbose)
3430 ata_raid_sii_print_meta(meta);
3432 /* now convert Silicon Image meta into our generic form */
3433 for (array = 0; array < MAX_ARRAYS; array++) {
3434 if (!raidp[array]) {
3435 raidp[array] =
3436 (struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
3437 M_WAITOK | M_ZERO);
3439 raid = raidp[array];
3440 if (raid->format && (raid->format != AR_F_SII_RAID))
3441 continue;
3443 if (raid->format == AR_F_SII_RAID &&
3444 (raid->magic_0 != *((u_int64_t *)meta->timestamp))) {
3445 continue;
3448 /* update our knowledge about the array config based on generation */
3449 if (!meta->generation || meta->generation > raid->generation) {
3450 switch (meta->type) {
3451 case SII_T_RAID0:
3452 raid->type = AR_T_RAID0;
3453 break;
3455 case SII_T_RAID1:
3456 raid->type = AR_T_RAID1;
3457 break;
3459 case SII_T_RAID01:
3460 raid->type = AR_T_RAID01;
3461 break;
3463 case SII_T_SPARE:
3464 device_printf(parent, "Silicon Image SPARE disk\n");
3465 kfree(raidp[array], M_AR);
3466 raidp[array] = NULL;
3467 goto sii_out;
3469 default:
3470 device_printf(parent,"Silicon Image unknown RAID type 0x%02x\n",
3471 meta->type);
3472 kfree(raidp[array], M_AR);
3473 raidp[array] = NULL;
3474 goto sii_out;
3476 raid->magic_0 = *((u_int64_t *)meta->timestamp);
3477 raid->format = AR_F_SII_RAID;
3478 raid->generation = meta->generation;
3479 raid->interleave = meta->stripe_sectors;
3480 raid->width = (meta->raid0_disks != 0xff) ? meta->raid0_disks : 1;
3481 raid->total_disks =
3482 ((meta->raid0_disks != 0xff) ? meta->raid0_disks : 0) +
3483 ((meta->raid1_disks != 0xff) ? meta->raid1_disks : 0);
3484 raid->total_sectors = meta->total_sectors;
3485 raid->heads = 255;
3486 raid->sectors = 63;
3487 raid->cylinders = raid->total_sectors / (63 * 255);
3488 raid->offset_sectors = 0;
3489 raid->rebuild_lba = meta->rebuild_lba;
3490 raid->lun = array;
3491 strncpy(raid->name, meta->name,
3492 min(sizeof(raid->name), sizeof(meta->name)));
3494 /* clear out any old info */
3495 if (raid->generation) {
3496 for (disk = 0; disk < raid->total_disks; disk++) {
3497 raid->disks[disk].dev = NULL;
3498 raid->disks[disk].flags = 0;
3502 if (meta->generation >= raid->generation) {
3503 /* XXX SOS add check for the right physical disk by serial# */
3504 if (meta->status & SII_S_READY) {
3505 int disk_number = (raid->type == AR_T_RAID01) ?
3506 meta->raid1_ident + (meta->raid0_ident << 1) :
3507 meta->disk_number;
3509 raid->disks[disk_number].dev = parent;
3510 raid->disks[disk_number].sectors =
3511 raid->total_sectors / raid->width;
3512 raid->disks[disk_number].flags =
3513 (AR_DF_ONLINE | AR_DF_PRESENT | AR_DF_ASSIGNED);
3514 ars->raid[raid->volume] = raid;
3515 ars->disk_number[raid->volume] = disk_number;
3516 retval = 1;
3519 break;
3522 sii_out:
3523 kfree(meta, M_AR);
3524 return retval;
3527 /* Silicon Integrated Systems Metadata */
3528 static int
3529 ata_raid_sis_read_meta(device_t dev, struct ar_softc **raidp)
3531 struct ata_raid_subdisk *ars = device_get_softc(dev);
3532 device_t parent = device_get_parent(dev);
3533 struct sis_raid_conf *meta;
3534 struct ar_softc *raid = NULL;
3535 int array, disk_number, drive, retval = 0;
3537 meta = (struct sis_raid_conf *)kmalloc(sizeof(struct sis_raid_conf), M_AR,
3538 M_WAITOK | M_ZERO);
3540 if (ata_raid_rw(parent, SIS_LBA(parent),
3541 meta, sizeof(struct sis_raid_conf), ATA_R_READ)) {
3542 if (testing || bootverbose)
3543 device_printf(parent,
3544 "Silicon Integrated Systems read metadata failed\n");
3547 /* check for SiS magic */
3548 if (meta->magic != SIS_MAGIC) {
3549 if (testing || bootverbose)
3550 device_printf(parent,
3551 "Silicon Integrated Systems check1 failed\n");
3552 goto sis_out;
3555 if (testing || bootverbose)
3556 ata_raid_sis_print_meta(meta);
3558 /* now convert SiS meta into our generic form */
3559 for (array = 0; array < MAX_ARRAYS; array++) {
3560 if (!raidp[array]) {
3561 raidp[array] =
3562 (struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
3563 M_WAITOK | M_ZERO);
3566 raid = raidp[array];
3567 if (raid->format && (raid->format != AR_F_SIS_RAID))
3568 continue;
3570 if ((raid->format == AR_F_SIS_RAID) &&
3571 ((raid->magic_0 != meta->controller_pci_id) ||
3572 (raid->magic_1 != meta->timestamp))) {
3573 continue;
3576 switch (meta->type_total_disks & SIS_T_MASK) {
3577 case SIS_T_JBOD:
3578 raid->type = AR_T_JBOD;
3579 raid->width = (meta->type_total_disks & SIS_D_MASK);
3580 raid->total_sectors += SIS_LBA(parent);
3581 break;
3583 case SIS_T_RAID0:
3584 raid->type = AR_T_RAID0;
3585 raid->width = (meta->type_total_disks & SIS_D_MASK);
3586 if (!raid->total_sectors ||
3587 (raid->total_sectors > (raid->width * SIS_LBA(parent))))
3588 raid->total_sectors = raid->width * SIS_LBA(parent);
3589 break;
3591 case SIS_T_RAID1:
3592 raid->type = AR_T_RAID1;
3593 raid->width = 1;
3594 if (!raid->total_sectors || (raid->total_sectors > SIS_LBA(parent)))
3595 raid->total_sectors = SIS_LBA(parent);
3596 break;
3598 default:
3599 device_printf(parent, "Silicon Integrated Systems "
3600 "unknown RAID type 0x%08x\n", meta->magic);
3601 kfree(raidp[array], M_AR);
3602 raidp[array] = NULL;
3603 goto sis_out;
3605 raid->magic_0 = meta->controller_pci_id;
3606 raid->magic_1 = meta->timestamp;
3607 raid->format = AR_F_SIS_RAID;
3608 raid->generation = 0;
3609 raid->interleave = meta->stripe_sectors;
3610 raid->total_disks = (meta->type_total_disks & SIS_D_MASK);
3611 raid->heads = 255;
3612 raid->sectors = 63;
3613 raid->cylinders = raid->total_sectors / (63 * 255);
3614 raid->offset_sectors = 0;
3615 raid->rebuild_lba = 0;
3616 raid->lun = array;
3617 /* XXX SOS if total_disks > 2 this doesn't float */
3618 if (((meta->disks & SIS_D_MASTER) >> 4) == meta->disk_number)
3619 disk_number = 0;
3620 else
3621 disk_number = 1;
3623 for (drive = 0; drive < raid->total_disks; drive++) {
3624 raid->disks[drive].sectors = raid->total_sectors/raid->width;
3625 if (drive == disk_number) {
3626 raid->disks[disk_number].dev = parent;
3627 raid->disks[disk_number].flags =
3628 (AR_DF_ONLINE | AR_DF_PRESENT | AR_DF_ASSIGNED);
3629 ars->raid[raid->volume] = raid;
3630 ars->disk_number[raid->volume] = disk_number;
3633 retval = 1;
3634 break;
3637 sis_out:
3638 kfree(meta, M_AR);
3639 return retval;
3642 static int
3643 ata_raid_sis_write_meta(struct ar_softc *rdp)
3645 struct sis_raid_conf *meta;
3646 struct timeval timestamp;
3647 int disk, error = 0;
3649 meta = (struct sis_raid_conf *)kmalloc(sizeof(struct sis_raid_conf), M_AR,
3650 M_WAITOK | M_ZERO);
3652 rdp->generation++;
3653 microtime(&timestamp);
3655 meta->magic = SIS_MAGIC;
3656 /* XXX SOS if total_disks > 2 this doesn't float */
3657 for (disk = 0; disk < rdp->total_disks; disk++) {
3658 if (rdp->disks[disk].dev) {
3659 struct ata_channel *ch =
3660 device_get_softc(device_get_parent(rdp->disks[disk].dev));
3661 struct ata_device *atadev = device_get_softc(rdp->disks[disk].dev);
3662 int disk_number = 1 + ATA_DEV(atadev->unit) + (ch->unit << 1);
3664 meta->disks |= disk_number << ((1 - disk) << 2);
3667 switch (rdp->type) {
3668 case AR_T_JBOD:
3669 meta->type_total_disks = SIS_T_JBOD;
3670 break;
3672 case AR_T_RAID0:
3673 meta->type_total_disks = SIS_T_RAID0;
3674 break;
3676 case AR_T_RAID1:
3677 meta->type_total_disks = SIS_T_RAID1;
3678 break;
3680 default:
3681 kfree(meta, M_AR);
3682 return ENODEV;
3684 meta->type_total_disks |= (rdp->total_disks & SIS_D_MASK);
3685 meta->stripe_sectors = rdp->interleave;
3686 meta->timestamp = timestamp.tv_sec;
3688 for (disk = 0; disk < rdp->total_disks; disk++) {
3689 if (rdp->disks[disk].dev) {
3690 struct ata_channel *ch =
3691 device_get_softc(device_get_parent(rdp->disks[disk].dev));
3692 struct ata_device *atadev = device_get_softc(rdp->disks[disk].dev);
3694 meta->controller_pci_id =
3695 (pci_get_vendor(GRANDPARENT(rdp->disks[disk].dev)) << 16) |
3696 pci_get_device(GRANDPARENT(rdp->disks[disk].dev));
3697 bcopy(atadev->param.model, meta->model, sizeof(meta->model));
3699 /* XXX SOS if total_disks > 2 this may not float */
3700 meta->disk_number = 1 + ATA_DEV(atadev->unit) + (ch->unit << 1);
3702 if (testing || bootverbose)
3703 ata_raid_sis_print_meta(meta);
3705 if (ata_raid_rw(rdp->disks[disk].dev,
3706 SIS_LBA(rdp->disks[disk].dev),
3707 meta, sizeof(struct sis_raid_conf),
3708 ATA_R_WRITE | ATA_R_DIRECT)) {
3709 device_printf(rdp->disks[disk].dev, "write metadata failed\n");
3710 error = EIO;
3714 kfree(meta, M_AR);
3715 return error;
3718 /* VIA Tech V-RAID Metadata */
3719 static int
3720 ata_raid_via_read_meta(device_t dev, struct ar_softc **raidp)
3722 struct ata_raid_subdisk *ars = device_get_softc(dev);
3723 device_t parent = device_get_parent(dev);
3724 struct via_raid_conf *meta;
3725 struct ar_softc *raid = NULL;
3726 u_int8_t checksum, *ptr;
3727 int array, count, disk, retval = 0;
3729 meta = (struct via_raid_conf *)kmalloc(sizeof(struct via_raid_conf), M_AR,
3730 M_WAITOK | M_ZERO);
3732 if (ata_raid_rw(parent, VIA_LBA(parent),
3733 meta, sizeof(struct via_raid_conf), ATA_R_READ)) {
3734 if (testing || bootverbose)
3735 device_printf(parent, "VIA read metadata failed\n");
3736 goto via_out;
3739 /* check if this is a VIA RAID struct */
3740 if (meta->magic != VIA_MAGIC) {
3741 if (testing || bootverbose)
3742 device_printf(parent, "VIA check1 failed\n");
3743 goto via_out;
3746 /* calculate checksum and compare for valid */
3747 for (checksum = 0, ptr = (u_int8_t *)meta, count = 0; count < 50; count++)
3748 checksum += *ptr++;
3749 if (checksum != meta->checksum) {
3750 if (testing || bootverbose)
3751 device_printf(parent, "VIA check2 failed\n");
3752 goto via_out;
3755 if (testing || bootverbose)
3756 ata_raid_via_print_meta(meta);
3758 /* now convert VIA meta into our generic form */
3759 for (array = 0; array < MAX_ARRAYS; array++) {
3760 if (!raidp[array]) {
3761 raidp[array] =
3762 (struct ar_softc *)kmalloc(sizeof(struct ar_softc), M_AR,
3763 M_WAITOK | M_ZERO);
3765 raid = raidp[array];
3766 if (raid->format && (raid->format != AR_F_VIA_RAID))
3767 continue;
3769 if (raid->format == AR_F_VIA_RAID && (raid->magic_0 != meta->disks[0]))
3770 continue;
3772 switch (meta->type & VIA_T_MASK) {
3773 case VIA_T_RAID0:
3774 raid->type = AR_T_RAID0;
3775 raid->width = meta->stripe_layout & VIA_L_DISKS;
3776 if (!raid->total_sectors ||
3777 (raid->total_sectors > (raid->width * meta->disk_sectors)))
3778 raid->total_sectors = raid->width * meta->disk_sectors;
3779 break;
3781 case VIA_T_RAID1:
3782 raid->type = AR_T_RAID1;
3783 raid->width = 1;
3784 raid->total_sectors = meta->disk_sectors;
3785 break;
3787 case VIA_T_RAID01:
3788 raid->type = AR_T_RAID01;
3789 raid->width = meta->stripe_layout & VIA_L_DISKS;
3790 if (!raid->total_sectors ||
3791 (raid->total_sectors > (raid->width * meta->disk_sectors)))
3792 raid->total_sectors = raid->width * meta->disk_sectors;
3793 break;
3795 case VIA_T_RAID5:
3796 raid->type = AR_T_RAID5;
3797 raid->width = meta->stripe_layout & VIA_L_DISKS;
3798 if (!raid->total_sectors ||
3799 (raid->total_sectors > ((raid->width - 1)*meta->disk_sectors)))
3800 raid->total_sectors = (raid->width - 1) * meta->disk_sectors;
3801 break;
3803 case VIA_T_SPAN:
3804 raid->type = AR_T_SPAN;
3805 raid->width = 1;
3806 raid->total_sectors += meta->disk_sectors;
3807 break;
3809 default:
3810 device_printf(parent,"VIA unknown RAID type 0x%02x\n", meta->type);
3811 kfree(raidp[array], M_AR);
3812 raidp[array] = NULL;
3813 goto via_out;
3815 raid->magic_0 = meta->disks[0];
3816 raid->format = AR_F_VIA_RAID;
3817 raid->generation = 0;
3818 raid->interleave =
3819 0x08 << ((meta->stripe_layout & VIA_L_MASK) >> VIA_L_SHIFT);
3820 for (count = 0, disk = 0; disk < 8; disk++)
3821 if (meta->disks[disk])
3822 count++;
3823 raid->total_disks = count;
3824 raid->heads = 255;
3825 raid->sectors = 63;
3826 raid->cylinders = raid->total_sectors / (63 * 255);
3827 raid->offset_sectors = 0;
3828 raid->rebuild_lba = 0;
3829 raid->lun = array;
3831 for (disk = 0; disk < raid->total_disks; disk++) {
3832 if (meta->disks[disk] == meta->disk_id) {
3833 raid->disks[disk].dev = parent;
3834 bcopy(&meta->disk_id, raid->disks[disk].serial,
3835 sizeof(u_int32_t));
3836 raid->disks[disk].sectors = meta->disk_sectors;
3837 raid->disks[disk].flags =
3838 (AR_DF_ONLINE | AR_DF_PRESENT | AR_DF_ASSIGNED);
3839 ars->raid[raid->volume] = raid;
3840 ars->disk_number[raid->volume] = disk;
3841 retval = 1;
3842 break;
3845 break;
3848 via_out:
3849 kfree(meta, M_AR);
3850 return retval;
3853 static int
3854 ata_raid_via_write_meta(struct ar_softc *rdp)
3856 struct via_raid_conf *meta;
3857 int disk, error = 0;
3859 meta = (struct via_raid_conf *)kmalloc(sizeof(struct via_raid_conf), M_AR,
3860 M_WAITOK | M_ZERO);
3862 rdp->generation++;
3864 meta->magic = VIA_MAGIC;
3865 meta->dummy_0 = 0x02;
3866 switch (rdp->type) {
3867 case AR_T_SPAN:
3868 meta->type = VIA_T_SPAN;
3869 meta->stripe_layout = (rdp->total_disks & VIA_L_DISKS);
3870 break;
3872 case AR_T_RAID0:
3873 meta->type = VIA_T_RAID0;
3874 meta->stripe_layout = ((rdp->interleave >> 1) & VIA_L_MASK);
3875 meta->stripe_layout |= (rdp->total_disks & VIA_L_DISKS);
3876 break;
3878 case AR_T_RAID1:
3879 meta->type = VIA_T_RAID1;
3880 meta->stripe_layout = (rdp->total_disks & VIA_L_DISKS);
3881 break;
3883 case AR_T_RAID5:
3884 meta->type = VIA_T_RAID5;
3885 meta->stripe_layout = ((rdp->interleave >> 1) & VIA_L_MASK);
3886 meta->stripe_layout |= (rdp->total_disks & VIA_L_DISKS);
3887 break;
3889 case AR_T_RAID01:
3890 meta->type = VIA_T_RAID01;
3891 meta->stripe_layout = ((rdp->interleave >> 1) & VIA_L_MASK);
3892 meta->stripe_layout |= (rdp->width & VIA_L_DISKS);
3893 break;
3895 default:
3896 kfree(meta, M_AR);
3897 return ENODEV;
3899 meta->type |= VIA_T_BOOTABLE; /* XXX SOS */
3900 meta->disk_sectors =
3901 rdp->total_sectors / (rdp->width - (rdp->type == AR_RAID5));
3902 for (disk = 0; disk < rdp->total_disks; disk++)
3903 meta->disks[disk] = (u_int32_t)(uintptr_t)rdp->disks[disk].dev;
3905 for (disk = 0; disk < rdp->total_disks; disk++) {
3906 if (rdp->disks[disk].dev) {
3907 u_int8_t *ptr;
3908 int count;
3910 meta->disk_index = disk * sizeof(u_int32_t);
3911 if (rdp->type == AR_T_RAID01)
3912 meta->disk_index = ((meta->disk_index & 0x08) << 2) |
3913 (meta->disk_index & ~0x08);
3914 meta->disk_id = meta->disks[disk];
3915 meta->checksum = 0;
3916 for (ptr = (u_int8_t *)meta, count = 0; count < 50; count++)
3917 meta->checksum += *ptr++;
3919 if (testing || bootverbose)
3920 ata_raid_via_print_meta(meta);
3922 if (ata_raid_rw(rdp->disks[disk].dev,
3923 VIA_LBA(rdp->disks[disk].dev),
3924 meta, sizeof(struct via_raid_conf),
3925 ATA_R_WRITE | ATA_R_DIRECT)) {
3926 device_printf(rdp->disks[disk].dev, "write metadata failed\n");
3927 error = EIO;
3931 kfree(meta, M_AR);
3932 return error;
3935 static struct ata_request *
3936 ata_raid_init_request(struct ar_softc *rdp, struct bio *bio)
3938 struct ata_request *request;
3940 if (!(request = ata_alloc_request())) {
3941 kprintf("FAILURE - out of memory in ata_raid_init_request\n");
3942 return NULL;
3944 request->timeout = 5;
3945 request->retries = 2;
3946 request->callback = ata_raid_done;
3947 request->driver = rdp;
3948 request->bio = bio;
3949 switch (request->bio->bio_buf->b_cmd) {
3950 case BUF_CMD_READ:
3951 request->flags = ATA_R_READ;
3952 break;
3953 case BUF_CMD_WRITE:
3954 request->flags = ATA_R_WRITE;
3955 break;
3956 default:
3957 kprintf("ar%d: FAILURE - unknown BUF operation\n", rdp->lun);
3958 ata_free_request(request);
3959 #if 0
3960 bio->bio_buf->b_flags |= B_ERROR;
3961 bio->bio_buf->b_error = EIO;
3962 biodone(bio);
3963 #endif /* 0 */
3964 return(NULL);
3966 return request;
3969 static int
3970 ata_raid_send_request(struct ata_request *request)
3972 struct ata_device *atadev = device_get_softc(request->dev);
3974 request->transfersize = min(request->bytecount, atadev->max_iosize);
3975 if (request->flags & ATA_R_READ) {
3976 if (atadev->mode >= ATA_DMA) {
3977 request->flags |= ATA_R_DMA;
3978 request->u.ata.command = ATA_READ_DMA;
3980 else if (atadev->max_iosize > DEV_BSIZE)
3981 request->u.ata.command = ATA_READ_MUL;
3982 else
3983 request->u.ata.command = ATA_READ;
3985 else if (request->flags & ATA_R_WRITE) {
3986 if (atadev->mode >= ATA_DMA) {
3987 request->flags |= ATA_R_DMA;
3988 request->u.ata.command = ATA_WRITE_DMA;
3990 else if (atadev->max_iosize > DEV_BSIZE)
3991 request->u.ata.command = ATA_WRITE_MUL;
3992 else
3993 request->u.ata.command = ATA_WRITE;
3995 else {
3996 device_printf(request->dev, "FAILURE - unknown IO operation\n");
3997 ata_free_request(request);
3998 return EIO;
4000 request->flags |= (ATA_R_ORDERED | ATA_R_THREAD);
4001 ata_queue_request(request);
4002 return 0;
4005 static int
4006 ata_raid_rw(device_t dev, u_int64_t lba, void *data, u_int bcount, int flags)
4008 struct ata_device *atadev = device_get_softc(dev);
4009 struct ata_request *request;
4010 int error;
4012 if (bcount % DEV_BSIZE) {
4013 device_printf(dev, "FAILURE - transfers must be modulo sectorsize\n");
4014 return ENOMEM;
4017 if (!(request = ata_alloc_request())) {
4018 device_printf(dev, "FAILURE - out of memory in ata_raid_rw\n");
4019 return ENOMEM;
4022 /* setup request */
4023 request->dev = dev;
4024 request->timeout = 10;
4025 request->retries = 0;
4026 request->data = data;
4027 request->bytecount = bcount;
4028 request->transfersize = DEV_BSIZE;
4029 request->u.ata.lba = lba;
4030 request->u.ata.count = request->bytecount / DEV_BSIZE;
4031 request->flags = flags;
4033 if (flags & ATA_R_READ) {
4034 if (atadev->mode >= ATA_DMA) {
4035 request->u.ata.command = ATA_READ_DMA;
4036 request->flags |= ATA_R_DMA;
4038 else
4039 request->u.ata.command = ATA_READ;
4040 ata_queue_request(request);
4042 else if (flags & ATA_R_WRITE) {
4043 if (atadev->mode >= ATA_DMA) {
4044 request->u.ata.command = ATA_WRITE_DMA;
4045 request->flags |= ATA_R_DMA;
4047 else
4048 request->u.ata.command = ATA_WRITE;
4049 ata_queue_request(request);
4051 else {
4052 device_printf(dev, "FAILURE - unknown IO operation\n");
4053 request->result = EIO;
4055 error = request->result;
4056 ata_free_request(request);
4057 return error;
4061 * module handeling
4063 static int
4064 ata_raid_subdisk_probe(device_t dev)
4066 device_quiet(dev);
4067 return 0;
4070 static int
4071 ata_raid_subdisk_attach(device_t dev)
4073 struct ata_raid_subdisk *ars = device_get_softc(dev);
4074 int volume;
4076 for (volume = 0; volume < MAX_VOLUMES; volume++) {
4077 ars->raid[volume] = NULL;
4078 ars->disk_number[volume] = -1;
4080 ata_raid_read_metadata(dev);
4081 return 0;
4084 static int
4085 ata_raid_subdisk_detach(device_t dev)
4087 struct ata_raid_subdisk *ars = device_get_softc(dev);
4088 int volume;
4090 for (volume = 0; volume < MAX_VOLUMES; volume++) {
4091 if (ars->raid[volume]) {
4092 ars->raid[volume]->disks[ars->disk_number[volume]].flags &=
4093 ~(AR_DF_PRESENT | AR_DF_ONLINE);
4094 ars->raid[volume]->disks[ars->disk_number[volume]].dev = NULL;
4095 ata_raid_config_changed(ars->raid[volume], 1);
4096 ars->raid[volume] = NULL;
4097 ars->disk_number[volume] = -1;
4100 return 0;
4103 static device_method_t ata_raid_sub_methods[] = {
4104 /* device interface */
4105 DEVMETHOD(device_probe, ata_raid_subdisk_probe),
4106 DEVMETHOD(device_attach, ata_raid_subdisk_attach),
4107 DEVMETHOD(device_detach, ata_raid_subdisk_detach),
4108 { 0, 0 }
4111 static driver_t ata_raid_sub_driver = {
4112 "subdisk",
4113 ata_raid_sub_methods,
4114 sizeof(struct ata_raid_subdisk)
4117 DRIVER_MODULE(subdisk, ad, ata_raid_sub_driver, ata_raid_sub_devclass, NULL, NULL);
4119 static int
4120 ata_raid_module_event_handler(module_t mod, int what, void *arg)
4122 int i;
4124 switch (what) {
4125 case MOD_LOAD:
4126 if (testing || bootverbose)
4127 kprintf("ATA PseudoRAID loaded\n");
4128 #if 0
4129 /* setup table to hold metadata for all ATA PseudoRAID arrays */
4130 ata_raid_arrays = kmalloc(sizeof(struct ar_soft *) * MAX_ARRAYS,
4131 M_AR, M_WAITOK | M_ZERO);
4132 #endif
4133 /* attach found PseudoRAID arrays */
4134 for (i = 0; i < MAX_ARRAYS; i++) {
4135 struct ar_softc *rdp = ata_raid_arrays[i];
4137 if (!rdp || !rdp->format)
4138 continue;
4139 if (testing || bootverbose)
4140 ata_raid_print_meta(rdp);
4141 ata_raid_attach(rdp, 0);
4143 ata_raid_ioctl_func = ata_raid_ioctl;
4144 return 0;
4146 case MOD_UNLOAD:
4147 /* detach found PseudoRAID arrays */
4148 for (i = 0; i < MAX_ARRAYS; i++) {
4149 struct ar_softc *rdp = ata_raid_arrays[i];
4151 if (!rdp || !rdp->status)
4152 continue;
4153 disk_destroy(&rdp->disk);
4155 if (testing || bootverbose)
4156 kprintf("ATA PseudoRAID unloaded\n");
4157 #if 0
4158 kfree(ata_raid_arrays, M_AR);
4159 #endif
4160 ata_raid_ioctl_func = NULL;
4161 return 0;
4163 default:
4164 return EOPNOTSUPP;
4168 static moduledata_t ata_raid_moduledata =
4169 { "ataraid", ata_raid_module_event_handler, NULL };
4170 DECLARE_MODULE(ata, ata_raid_moduledata, SI_SUB_RAID, SI_ORDER_FIRST);
4171 MODULE_VERSION(ataraid, 1);
4172 MODULE_DEPEND(ataraid, ata, 1, 1, 1);
4173 MODULE_DEPEND(ataraid, ad, 1, 1, 1);
4175 static char *
4176 ata_raid_format(struct ar_softc *rdp)
4178 switch (rdp->format) {
4179 case AR_F_FREEBSD_RAID: return "FreeBSD PseudoRAID";
4180 case AR_F_ADAPTEC_RAID: return "Adaptec HostRAID";
4181 case AR_F_HPTV2_RAID: return "HighPoint v2 RocketRAID";
4182 case AR_F_HPTV3_RAID: return "HighPoint v3 RocketRAID";
4183 case AR_F_INTEL_RAID: return "Intel MatrixRAID";
4184 case AR_F_ITE_RAID: return "Integrated Technology Express";
4185 case AR_F_JMICRON_RAID: return "JMicron Technology Corp";
4186 case AR_F_LSIV2_RAID: return "LSILogic v2 MegaRAID";
4187 case AR_F_LSIV3_RAID: return "LSILogic v3 MegaRAID";
4188 case AR_F_NVIDIA_RAID: return "nVidia MediaShield";
4189 case AR_F_PROMISE_RAID: return "Promise Fasttrak";
4190 case AR_F_SII_RAID: return "Silicon Image Medley";
4191 case AR_F_SIS_RAID: return "Silicon Integrated Systems";
4192 case AR_F_VIA_RAID: return "VIA Tech V-RAID";
4193 default: return "UNKNOWN";
4197 static char *
4198 ata_raid_type(struct ar_softc *rdp)
4200 switch (rdp->type) {
4201 case AR_T_JBOD: return "JBOD";
4202 case AR_T_SPAN: return "SPAN";
4203 case AR_T_RAID0: return "RAID0";
4204 case AR_T_RAID1: return "RAID1";
4205 case AR_T_RAID3: return "RAID3";
4206 case AR_T_RAID4: return "RAID4";
4207 case AR_T_RAID5: return "RAID5";
4208 case AR_T_RAID01: return "RAID0+1";
4209 default: return "UNKNOWN";
4213 static char *
4214 ata_raid_flags(struct ar_softc *rdp)
4216 switch (rdp->status & (AR_S_READY | AR_S_DEGRADED | AR_S_REBUILDING)) {
4217 case AR_S_READY: return "READY";
4218 case AR_S_READY | AR_S_DEGRADED: return "DEGRADED";
4219 case AR_S_READY | AR_S_REBUILDING:
4220 case AR_S_READY | AR_S_DEGRADED | AR_S_REBUILDING: return "REBUILDING";
4221 default: return "BROKEN";
4225 /* debugging gunk */
4226 static void
4227 ata_raid_print_meta(struct ar_softc *raid)
4229 int i;
4231 kprintf("********** ATA PseudoRAID ar%d Metadata **********\n", raid->lun);
4232 kprintf("=================================================\n");
4233 kprintf("format %s\n", ata_raid_format(raid));
4234 kprintf("type %s\n", ata_raid_type(raid));
4235 kprintf("flags 0x%02x %b\n", raid->status, raid->status,
4236 "\20\3REBUILDING\2DEGRADED\1READY\n");
4237 kprintf("magic_0 0x%016jx\n", raid->magic_0);
4238 kprintf("magic_1 0x%016jx\n",raid->magic_1);
4239 kprintf("generation %u\n", raid->generation);
4240 kprintf("total_sectors %ju\n", raid->total_sectors);
4241 kprintf("offset_sectors %ju\n", raid->offset_sectors);
4242 kprintf("heads %u\n", raid->heads);
4243 kprintf("sectors %u\n", raid->sectors);
4244 kprintf("cylinders %u\n", raid->cylinders);
4245 kprintf("width %u\n", raid->width);
4246 kprintf("interleave %u\n", raid->interleave);
4247 kprintf("total_disks %u\n", raid->total_disks);
4248 for (i = 0; i < raid->total_disks; i++) {
4249 kprintf(" disk %d: flags = 0x%02x %b\n", i, raid->disks[i].flags,
4250 raid->disks[i].flags, "\20\4ONLINE\3SPARE\2ASSIGNED\1PRESENT\n");
4251 if (raid->disks[i].dev) {
4252 kprintf(" ");
4253 device_printf(raid->disks[i].dev, " sectors %jd\n",
4254 raid->disks[i].sectors);
4257 kprintf("=================================================\n");
4260 static char *
4261 ata_raid_adaptec_type(int type)
4263 static char buffer[16];
4265 switch (type) {
4266 case ADP_T_RAID0: return "RAID0";
4267 case ADP_T_RAID1: return "RAID1";
4268 default: ksprintf(buffer, "UNKNOWN 0x%02x", type);
4269 return buffer;
4273 static void
4274 ata_raid_adaptec_print_meta(struct adaptec_raid_conf *meta)
4276 int i;
4278 kprintf("********* ATA Adaptec HostRAID Metadata *********\n");
4279 kprintf("magic_0 <0x%08x>\n", be32toh(meta->magic_0));
4280 kprintf("generation 0x%08x\n", be32toh(meta->generation));
4281 kprintf("dummy_0 0x%04x\n", be16toh(meta->dummy_0));
4282 kprintf("total_configs %u\n", be16toh(meta->total_configs));
4283 kprintf("dummy_1 0x%04x\n", be16toh(meta->dummy_1));
4284 kprintf("checksum 0x%04x\n", be16toh(meta->checksum));
4285 kprintf("dummy_2 0x%08x\n", be32toh(meta->dummy_2));
4286 kprintf("dummy_3 0x%08x\n", be32toh(meta->dummy_3));
4287 kprintf("flags 0x%08x\n", be32toh(meta->flags));
4288 kprintf("timestamp 0x%08x\n", be32toh(meta->timestamp));
4289 kprintf("dummy_4 0x%08x 0x%08x 0x%08x 0x%08x\n",
4290 be32toh(meta->dummy_4[0]), be32toh(meta->dummy_4[1]),
4291 be32toh(meta->dummy_4[2]), be32toh(meta->dummy_4[3]));
4292 kprintf("dummy_5 0x%08x 0x%08x 0x%08x 0x%08x\n",
4293 be32toh(meta->dummy_5[0]), be32toh(meta->dummy_5[1]),
4294 be32toh(meta->dummy_5[2]), be32toh(meta->dummy_5[3]));
4296 for (i = 0; i < be16toh(meta->total_configs); i++) {
4297 kprintf(" %d total_disks %u\n", i,
4298 be16toh(meta->configs[i].disk_number));
4299 kprintf(" %d generation %u\n", i,
4300 be16toh(meta->configs[i].generation));
4301 kprintf(" %d magic_0 0x%08x\n", i,
4302 be32toh(meta->configs[i].magic_0));
4303 kprintf(" %d dummy_0 0x%02x\n", i, meta->configs[i].dummy_0);
4304 kprintf(" %d type %s\n", i,
4305 ata_raid_adaptec_type(meta->configs[i].type));
4306 kprintf(" %d dummy_1 0x%02x\n", i, meta->configs[i].dummy_1);
4307 kprintf(" %d flags %d\n", i,
4308 be32toh(meta->configs[i].flags));
4309 kprintf(" %d dummy_2 0x%02x\n", i, meta->configs[i].dummy_2);
4310 kprintf(" %d dummy_3 0x%02x\n", i, meta->configs[i].dummy_3);
4311 kprintf(" %d dummy_4 0x%02x\n", i, meta->configs[i].dummy_4);
4312 kprintf(" %d dummy_5 0x%02x\n", i, meta->configs[i].dummy_5);
4313 kprintf(" %d disk_number %u\n", i,
4314 be32toh(meta->configs[i].disk_number));
4315 kprintf(" %d dummy_6 0x%08x\n", i,
4316 be32toh(meta->configs[i].dummy_6));
4317 kprintf(" %d sectors %u\n", i,
4318 be32toh(meta->configs[i].sectors));
4319 kprintf(" %d stripe_shift %u\n", i,
4320 be16toh(meta->configs[i].stripe_shift));
4321 kprintf(" %d dummy_7 0x%08x\n", i,
4322 be32toh(meta->configs[i].dummy_7));
4323 kprintf(" %d dummy_8 0x%08x 0x%08x 0x%08x 0x%08x\n", i,
4324 be32toh(meta->configs[i].dummy_8[0]),
4325 be32toh(meta->configs[i].dummy_8[1]),
4326 be32toh(meta->configs[i].dummy_8[2]),
4327 be32toh(meta->configs[i].dummy_8[3]));
4328 kprintf(" %d name <%s>\n", i, meta->configs[i].name);
4330 kprintf("magic_1 <0x%08x>\n", be32toh(meta->magic_1));
4331 kprintf("magic_2 <0x%08x>\n", be32toh(meta->magic_2));
4332 kprintf("magic_3 <0x%08x>\n", be32toh(meta->magic_3));
4333 kprintf("magic_4 <0x%08x>\n", be32toh(meta->magic_4));
4334 kprintf("=================================================\n");
4337 static char *
4338 ata_raid_hptv2_type(int type)
4340 static char buffer[16];
4342 switch (type) {
4343 case HPTV2_T_RAID0: return "RAID0";
4344 case HPTV2_T_RAID1: return "RAID1";
4345 case HPTV2_T_RAID01_RAID0: return "RAID01_RAID0";
4346 case HPTV2_T_SPAN: return "SPAN";
4347 case HPTV2_T_RAID_3: return "RAID3";
4348 case HPTV2_T_RAID_5: return "RAID5";
4349 case HPTV2_T_JBOD: return "JBOD";
4350 case HPTV2_T_RAID01_RAID1: return "RAID01_RAID1";
4351 default: ksprintf(buffer, "UNKNOWN 0x%02x", type);
4352 return buffer;
4356 static void
4357 ata_raid_hptv2_print_meta(struct hptv2_raid_conf *meta)
4359 int i;
4361 kprintf("****** ATA Highpoint V2 RocketRAID Metadata *****\n");
4362 kprintf("magic 0x%08x\n", meta->magic);
4363 kprintf("magic_0 0x%08x\n", meta->magic_0);
4364 kprintf("magic_1 0x%08x\n", meta->magic_1);
4365 kprintf("order 0x%08x\n", meta->order);
4366 kprintf("array_width %u\n", meta->array_width);
4367 kprintf("stripe_shift %u\n", meta->stripe_shift);
4368 kprintf("type %s\n", ata_raid_hptv2_type(meta->type));
4369 kprintf("disk_number %u\n", meta->disk_number);
4370 kprintf("total_sectors %u\n", meta->total_sectors);
4371 kprintf("disk_mode 0x%08x\n", meta->disk_mode);
4372 kprintf("boot_mode 0x%08x\n", meta->boot_mode);
4373 kprintf("boot_disk 0x%02x\n", meta->boot_disk);
4374 kprintf("boot_protect 0x%02x\n", meta->boot_protect);
4375 kprintf("log_entries 0x%02x\n", meta->error_log_entries);
4376 kprintf("log_index 0x%02x\n", meta->error_log_index);
4377 if (meta->error_log_entries) {
4378 kprintf(" timestamp reason disk status sectors lba\n");
4379 for (i = meta->error_log_index;
4380 i < meta->error_log_index + meta->error_log_entries; i++)
4381 kprintf(" 0x%08x 0x%02x 0x%02x 0x%02x 0x%02x 0x%08x\n",
4382 meta->errorlog[i%32].timestamp,
4383 meta->errorlog[i%32].reason,
4384 meta->errorlog[i%32].disk, meta->errorlog[i%32].status,
4385 meta->errorlog[i%32].sectors, meta->errorlog[i%32].lba);
4387 kprintf("rebuild_lba 0x%08x\n", meta->rebuild_lba);
4388 kprintf("dummy_1 0x%02x\n", meta->dummy_1);
4389 kprintf("name_1 <%.15s>\n", meta->name_1);
4390 kprintf("dummy_2 0x%02x\n", meta->dummy_2);
4391 kprintf("name_2 <%.15s>\n", meta->name_2);
4392 kprintf("=================================================\n");
4395 static char *
4396 ata_raid_hptv3_type(int type)
4398 static char buffer[16];
4400 switch (type) {
4401 case HPTV3_T_SPARE: return "SPARE";
4402 case HPTV3_T_JBOD: return "JBOD";
4403 case HPTV3_T_SPAN: return "SPAN";
4404 case HPTV3_T_RAID0: return "RAID0";
4405 case HPTV3_T_RAID1: return "RAID1";
4406 case HPTV3_T_RAID3: return "RAID3";
4407 case HPTV3_T_RAID5: return "RAID5";
4408 default: ksprintf(buffer, "UNKNOWN 0x%02x", type);
4409 return buffer;
4413 static void
4414 ata_raid_hptv3_print_meta(struct hptv3_raid_conf *meta)
4416 int i;
4418 kprintf("****** ATA Highpoint V3 RocketRAID Metadata *****\n");
4419 kprintf("magic 0x%08x\n", meta->magic);
4420 kprintf("magic_0 0x%08x\n", meta->magic_0);
4421 kprintf("checksum_0 0x%02x\n", meta->checksum_0);
4422 kprintf("mode 0x%02x\n", meta->mode);
4423 kprintf("user_mode 0x%02x\n", meta->user_mode);
4424 kprintf("config_entries 0x%02x\n", meta->config_entries);
4425 for (i = 0; i < meta->config_entries; i++) {
4426 kprintf("config %d:\n", i);
4427 kprintf(" total_sectors %ju\n",
4428 meta->configs[0].total_sectors +
4429 ((u_int64_t)meta->configs_high[0].total_sectors << 32));
4430 kprintf(" type %s\n",
4431 ata_raid_hptv3_type(meta->configs[i].type));
4432 kprintf(" total_disks %u\n", meta->configs[i].total_disks);
4433 kprintf(" disk_number %u\n", meta->configs[i].disk_number);
4434 kprintf(" stripe_shift %u\n", meta->configs[i].stripe_shift);
4435 kprintf(" status %b\n", meta->configs[i].status,
4436 "\20\2RAID5\1NEED_REBUILD\n");
4437 kprintf(" critical_disks %u\n", meta->configs[i].critical_disks);
4438 kprintf(" rebuild_lba %ju\n",
4439 meta->configs_high[0].rebuild_lba +
4440 ((u_int64_t)meta->configs_high[0].rebuild_lba << 32));
4442 kprintf("name <%.16s>\n", meta->name);
4443 kprintf("timestamp 0x%08x\n", meta->timestamp);
4444 kprintf("description <%.16s>\n", meta->description);
4445 kprintf("creator <%.16s>\n", meta->creator);
4446 kprintf("checksum_1 0x%02x\n", meta->checksum_1);
4447 kprintf("dummy_0 0x%02x\n", meta->dummy_0);
4448 kprintf("dummy_1 0x%02x\n", meta->dummy_1);
4449 kprintf("flags %b\n", meta->flags,
4450 "\20\4RCACHE\3WCACHE\2NCQ\1TCQ\n");
4451 kprintf("=================================================\n");
4454 static char *
4455 ata_raid_intel_type(int type)
4457 static char buffer[16];
4459 switch (type) {
4460 case INTEL_T_RAID0: return "RAID0";
4461 case INTEL_T_RAID1: return "RAID1";
4462 case INTEL_T_RAID5: return "RAID5";
4463 default: ksprintf(buffer, "UNKNOWN 0x%02x", type);
4464 return buffer;
4468 static void
4469 ata_raid_intel_print_meta(struct intel_raid_conf *meta)
4471 struct intel_raid_mapping *map;
4472 int i, j;
4474 kprintf("********* ATA Intel MatrixRAID Metadata *********\n");
4475 kprintf("intel_id <%.24s>\n", meta->intel_id);
4476 kprintf("version <%.6s>\n", meta->version);
4477 kprintf("checksum 0x%08x\n", meta->checksum);
4478 kprintf("config_size 0x%08x\n", meta->config_size);
4479 kprintf("config_id 0x%08x\n", meta->config_id);
4480 kprintf("generation 0x%08x\n", meta->generation);
4481 kprintf("total_disks %u\n", meta->total_disks);
4482 kprintf("total_volumes %u\n", meta->total_volumes);
4483 kprintf("DISK# serial disk_sectors disk_id flags\n");
4484 for (i = 0; i < meta->total_disks; i++ ) {
4485 kprintf(" %d <%.16s> %u 0x%08x 0x%08x\n", i,
4486 meta->disk[i].serial, meta->disk[i].sectors,
4487 meta->disk[i].id, meta->disk[i].flags);
4489 map = (struct intel_raid_mapping *)&meta->disk[meta->total_disks];
4490 for (j = 0; j < meta->total_volumes; j++) {
4491 kprintf("name %.16s\n", map->name);
4492 kprintf("total_sectors %ju\n", map->total_sectors);
4493 kprintf("state %u\n", map->state);
4494 kprintf("reserved %u\n", map->reserved);
4495 kprintf("offset %u\n", map->offset);
4496 kprintf("disk_sectors %u\n", map->disk_sectors);
4497 kprintf("stripe_count %u\n", map->stripe_count);
4498 kprintf("stripe_sectors %u\n", map->stripe_sectors);
4499 kprintf("status %u\n", map->status);
4500 kprintf("type %s\n", ata_raid_intel_type(map->type));
4501 kprintf("total_disks %u\n", map->total_disks);
4502 kprintf("magic[0] 0x%02x\n", map->magic[0]);
4503 kprintf("magic[1] 0x%02x\n", map->magic[1]);
4504 kprintf("magic[2] 0x%02x\n", map->magic[2]);
4505 for (i = 0; i < map->total_disks; i++ ) {
4506 kprintf(" disk %d at disk_idx 0x%08x\n", i, map->disk_idx[i]);
4508 map = (struct intel_raid_mapping *)&map->disk_idx[map->total_disks];
4510 kprintf("=================================================\n");
4513 static char *
4514 ata_raid_ite_type(int type)
4516 static char buffer[16];
4518 switch (type) {
4519 case ITE_T_RAID0: return "RAID0";
4520 case ITE_T_RAID1: return "RAID1";
4521 case ITE_T_RAID01: return "RAID0+1";
4522 case ITE_T_SPAN: return "SPAN";
4523 default: ksprintf(buffer, "UNKNOWN 0x%02x", type);
4524 return buffer;
4528 static void
4529 ata_raid_ite_print_meta(struct ite_raid_conf *meta)
4531 kprintf("*** ATA Integrated Technology Express Metadata **\n");
4532 kprintf("ite_id <%.40s>\n", meta->ite_id);
4533 kprintf("timestamp_0 %04x/%02x/%02x %02x:%02x:%02x.%02x\n",
4534 *((u_int16_t *)meta->timestamp_0), meta->timestamp_0[2],
4535 meta->timestamp_0[3], meta->timestamp_0[5], meta->timestamp_0[4],
4536 meta->timestamp_0[7], meta->timestamp_0[6]);
4537 kprintf("total_sectors %jd\n", meta->total_sectors);
4538 kprintf("type %s\n", ata_raid_ite_type(meta->type));
4539 kprintf("stripe_1kblocks %u\n", meta->stripe_1kblocks);
4540 kprintf("timestamp_1 %04x/%02x/%02x %02x:%02x:%02x.%02x\n",
4541 *((u_int16_t *)meta->timestamp_1), meta->timestamp_1[2],
4542 meta->timestamp_1[3], meta->timestamp_1[5], meta->timestamp_1[4],
4543 meta->timestamp_1[7], meta->timestamp_1[6]);
4544 kprintf("stripe_sectors %u\n", meta->stripe_sectors);
4545 kprintf("array_width %u\n", meta->array_width);
4546 kprintf("disk_number %u\n", meta->disk_number);
4547 kprintf("disk_sectors %u\n", meta->disk_sectors);
4548 kprintf("=================================================\n");
4551 static char *
4552 ata_raid_jmicron_type(int type)
4554 static char buffer[16];
4556 switch (type) {
4557 case JM_T_RAID0: return "RAID0";
4558 case JM_T_RAID1: return "RAID1";
4559 case JM_T_RAID01: return "RAID0+1";
4560 case JM_T_JBOD: return "JBOD";
4561 case JM_T_RAID5: return "RAID5";
4562 default: ksprintf(buffer, "UNKNOWN 0x%02x", type);
4563 return buffer;
4567 static void
4568 ata_raid_jmicron_print_meta(struct jmicron_raid_conf *meta)
4570 int i;
4572 kprintf("***** ATA JMicron Technology Corp Metadata ******\n");
4573 kprintf("signature %.2s\n", meta->signature);
4574 kprintf("version 0x%04x\n", meta->version);
4575 kprintf("checksum 0x%04x\n", meta->checksum);
4576 kprintf("disk_id 0x%08x\n", meta->disk_id);
4577 kprintf("offset 0x%08x\n", meta->offset);
4578 kprintf("disk_sectors_low 0x%08x\n", meta->disk_sectors_low);
4579 kprintf("disk_sectors_high 0x%08x\n", meta->disk_sectors_high);
4580 kprintf("name %.16s\n", meta->name);
4581 kprintf("type %s\n", ata_raid_jmicron_type(meta->type));
4582 kprintf("stripe_shift %d\n", meta->stripe_shift);
4583 kprintf("flags 0x%04x\n", meta->flags);
4584 kprintf("spare:\n");
4585 for (i=0; i < 2 && meta->spare[i]; i++)
4586 kprintf(" %d 0x%08x\n", i, meta->spare[i]);
4587 kprintf("disks:\n");
4588 for (i=0; i < 8 && meta->disks[i]; i++)
4589 kprintf(" %d 0x%08x\n", i, meta->disks[i]);
4590 kprintf("=================================================\n");
4593 static char *
4594 ata_raid_lsiv2_type(int type)
4596 static char buffer[16];
4598 switch (type) {
4599 case LSIV2_T_RAID0: return "RAID0";
4600 case LSIV2_T_RAID1: return "RAID1";
4601 case LSIV2_T_SPARE: return "SPARE";
4602 default: ksprintf(buffer, "UNKNOWN 0x%02x", type);
4603 return buffer;
4607 static void
4608 ata_raid_lsiv2_print_meta(struct lsiv2_raid_conf *meta)
4610 int i;
4612 kprintf("******* ATA LSILogic V2 MegaRAID Metadata *******\n");
4613 kprintf("lsi_id <%s>\n", meta->lsi_id);
4614 kprintf("dummy_0 0x%02x\n", meta->dummy_0);
4615 kprintf("flags 0x%02x\n", meta->flags);
4616 kprintf("version 0x%04x\n", meta->version);
4617 kprintf("config_entries 0x%02x\n", meta->config_entries);
4618 kprintf("raid_count 0x%02x\n", meta->raid_count);
4619 kprintf("total_disks 0x%02x\n", meta->total_disks);
4620 kprintf("dummy_1 0x%02x\n", meta->dummy_1);
4621 kprintf("dummy_2 0x%04x\n", meta->dummy_2);
4622 for (i = 0; i < meta->config_entries; i++) {
4623 kprintf(" type %s\n",
4624 ata_raid_lsiv2_type(meta->configs[i].raid.type));
4625 kprintf(" dummy_0 %02x\n", meta->configs[i].raid.dummy_0);
4626 kprintf(" stripe_sectors %u\n",
4627 meta->configs[i].raid.stripe_sectors);
4628 kprintf(" array_width %u\n",
4629 meta->configs[i].raid.array_width);
4630 kprintf(" disk_count %u\n", meta->configs[i].raid.disk_count);
4631 kprintf(" config_offset %u\n",
4632 meta->configs[i].raid.config_offset);
4633 kprintf(" dummy_1 %u\n", meta->configs[i].raid.dummy_1);
4634 kprintf(" flags %02x\n", meta->configs[i].raid.flags);
4635 kprintf(" total_sectors %u\n",
4636 meta->configs[i].raid.total_sectors);
4638 kprintf("disk_number 0x%02x\n", meta->disk_number);
4639 kprintf("raid_number 0x%02x\n", meta->raid_number);
4640 kprintf("timestamp 0x%08x\n", meta->timestamp);
4641 kprintf("=================================================\n");
4644 static char *
4645 ata_raid_lsiv3_type(int type)
4647 static char buffer[16];
4649 switch (type) {
4650 case LSIV3_T_RAID0: return "RAID0";
4651 case LSIV3_T_RAID1: return "RAID1";
4652 default: ksprintf(buffer, "UNKNOWN 0x%02x", type);
4653 return buffer;
4657 static void
4658 ata_raid_lsiv3_print_meta(struct lsiv3_raid_conf *meta)
4660 int i;
4662 kprintf("******* ATA LSILogic V3 MegaRAID Metadata *******\n");
4663 kprintf("lsi_id <%.6s>\n", meta->lsi_id);
4664 kprintf("dummy_0 0x%04x\n", meta->dummy_0);
4665 kprintf("version 0x%04x\n", meta->version);
4666 kprintf("dummy_0 0x%04x\n", meta->dummy_1);
4667 kprintf("RAID configs:\n");
4668 for (i = 0; i < 8; i++) {
4669 if (meta->raid[i].total_disks) {
4670 kprintf("%02d stripe_pages %u\n", i,
4671 meta->raid[i].stripe_pages);
4672 kprintf("%02d type %s\n", i,
4673 ata_raid_lsiv3_type(meta->raid[i].type));
4674 kprintf("%02d total_disks %u\n", i,
4675 meta->raid[i].total_disks);
4676 kprintf("%02d array_width %u\n", i,
4677 meta->raid[i].array_width);
4678 kprintf("%02d sectors %u\n", i, meta->raid[i].sectors);
4679 kprintf("%02d offset %u\n", i, meta->raid[i].offset);
4680 kprintf("%02d device 0x%02x\n", i,
4681 meta->raid[i].device);
4684 kprintf("DISK configs:\n");
4685 for (i = 0; i < 6; i++) {
4686 if (meta->disk[i].disk_sectors) {
4687 kprintf("%02d disk_sectors %u\n", i,
4688 meta->disk[i].disk_sectors);
4689 kprintf("%02d flags 0x%02x\n", i, meta->disk[i].flags);
4692 kprintf("device 0x%02x\n", meta->device);
4693 kprintf("timestamp 0x%08x\n", meta->timestamp);
4694 kprintf("checksum_1 0x%02x\n", meta->checksum_1);
4695 kprintf("=================================================\n");
4698 static char *
4699 ata_raid_nvidia_type(int type)
4701 static char buffer[16];
4703 switch (type) {
4704 case NV_T_SPAN: return "SPAN";
4705 case NV_T_RAID0: return "RAID0";
4706 case NV_T_RAID1: return "RAID1";
4707 case NV_T_RAID3: return "RAID3";
4708 case NV_T_RAID5: return "RAID5";
4709 case NV_T_RAID01: return "RAID0+1";
4710 default: ksprintf(buffer, "UNKNOWN 0x%02x", type);
4711 return buffer;
4715 static void
4716 ata_raid_nvidia_print_meta(struct nvidia_raid_conf *meta)
4718 kprintf("******** ATA nVidia MediaShield Metadata ********\n");
4719 kprintf("nvidia_id <%.8s>\n", meta->nvidia_id);
4720 kprintf("config_size %d\n", meta->config_size);
4721 kprintf("checksum 0x%08x\n", meta->checksum);
4722 kprintf("version 0x%04x\n", meta->version);
4723 kprintf("disk_number %d\n", meta->disk_number);
4724 kprintf("dummy_0 0x%02x\n", meta->dummy_0);
4725 kprintf("total_sectors %d\n", meta->total_sectors);
4726 kprintf("sectors_size %d\n", meta->sector_size);
4727 kprintf("serial %.16s\n", meta->serial);
4728 kprintf("revision %.4s\n", meta->revision);
4729 kprintf("dummy_1 0x%08x\n", meta->dummy_1);
4730 kprintf("magic_0 0x%08x\n", meta->magic_0);
4731 kprintf("magic_1 0x%016jx\n", meta->magic_1);
4732 kprintf("magic_2 0x%016jx\n", meta->magic_2);
4733 kprintf("flags 0x%02x\n", meta->flags);
4734 kprintf("array_width %d\n", meta->array_width);
4735 kprintf("total_disks %d\n", meta->total_disks);
4736 kprintf("dummy_2 0x%02x\n", meta->dummy_2);
4737 kprintf("type %s\n", ata_raid_nvidia_type(meta->type));
4738 kprintf("dummy_3 0x%04x\n", meta->dummy_3);
4739 kprintf("stripe_sectors %d\n", meta->stripe_sectors);
4740 kprintf("stripe_bytes %d\n", meta->stripe_bytes);
4741 kprintf("stripe_shift %d\n", meta->stripe_shift);
4742 kprintf("stripe_mask 0x%08x\n", meta->stripe_mask);
4743 kprintf("stripe_sizesectors %d\n", meta->stripe_sizesectors);
4744 kprintf("stripe_sizebytes %d\n", meta->stripe_sizebytes);
4745 kprintf("rebuild_lba %d\n", meta->rebuild_lba);
4746 kprintf("dummy_4 0x%08x\n", meta->dummy_4);
4747 kprintf("dummy_5 0x%08x\n", meta->dummy_5);
4748 kprintf("status 0x%08x\n", meta->status);
4749 kprintf("=================================================\n");
4752 static char *
4753 ata_raid_promise_type(int type)
4755 static char buffer[16];
4757 switch (type) {
4758 case PR_T_RAID0: return "RAID0";
4759 case PR_T_RAID1: return "RAID1";
4760 case PR_T_RAID3: return "RAID3";
4761 case PR_T_RAID5: return "RAID5";
4762 case PR_T_SPAN: return "SPAN";
4763 default: ksprintf(buffer, "UNKNOWN 0x%02x", type);
4764 return buffer;
4768 static void
4769 ata_raid_promise_print_meta(struct promise_raid_conf *meta)
4771 int i;
4773 kprintf("********* ATA Promise FastTrak Metadata *********\n");
4774 kprintf("promise_id <%s>\n", meta->promise_id);
4775 kprintf("dummy_0 0x%08x\n", meta->dummy_0);
4776 kprintf("magic_0 0x%016jx\n", meta->magic_0);
4777 kprintf("magic_1 0x%04x\n", meta->magic_1);
4778 kprintf("magic_2 0x%08x\n", meta->magic_2);
4779 kprintf("integrity 0x%08x %b\n", meta->raid.integrity,
4780 meta->raid.integrity, "\20\10VALID\n" );
4781 kprintf("flags 0x%02x %b\n",
4782 meta->raid.flags, meta->raid.flags,
4783 "\20\10READY\7DOWN\6REDIR\5DUPLICATE\4SPARE"
4784 "\3ASSIGNED\2ONLINE\1VALID\n");
4785 kprintf("disk_number %d\n", meta->raid.disk_number);
4786 kprintf("channel 0x%02x\n", meta->raid.channel);
4787 kprintf("device 0x%02x\n", meta->raid.device);
4788 kprintf("magic_0 0x%016jx\n", meta->raid.magic_0);
4789 kprintf("disk_offset %u\n", meta->raid.disk_offset);
4790 kprintf("disk_sectors %u\n", meta->raid.disk_sectors);
4791 kprintf("rebuild_lba 0x%08x\n", meta->raid.rebuild_lba);
4792 kprintf("generation 0x%04x\n", meta->raid.generation);
4793 kprintf("status 0x%02x %b\n",
4794 meta->raid.status, meta->raid.status,
4795 "\20\6MARKED\5DEGRADED\4READY\3INITED\2ONLINE\1VALID\n");
4796 kprintf("type %s\n", ata_raid_promise_type(meta->raid.type));
4797 kprintf("total_disks %u\n", meta->raid.total_disks);
4798 kprintf("stripe_shift %u\n", meta->raid.stripe_shift);
4799 kprintf("array_width %u\n", meta->raid.array_width);
4800 kprintf("array_number %u\n", meta->raid.array_number);
4801 kprintf("total_sectors %u\n", meta->raid.total_sectors);
4802 kprintf("cylinders %u\n", meta->raid.cylinders);
4803 kprintf("heads %u\n", meta->raid.heads);
4804 kprintf("sectors %u\n", meta->raid.sectors);
4805 kprintf("magic_1 0x%016jx\n", meta->raid.magic_1);
4806 kprintf("DISK# flags dummy_0 channel device magic_0\n");
4807 for (i = 0; i < 8; i++) {
4808 kprintf(" %d %b 0x%02x 0x%02x 0x%02x ",
4809 i, meta->raid.disk[i].flags,
4810 "\20\10READY\7DOWN\6REDIR\5DUPLICATE\4SPARE"
4811 "\3ASSIGNED\2ONLINE\1VALID\n", meta->raid.disk[i].dummy_0,
4812 meta->raid.disk[i].channel, meta->raid.disk[i].device);
4813 kprintf("0x%016jx\n", meta->raid.disk[i].magic_0);
4815 kprintf("checksum 0x%08x\n", meta->checksum);
4816 kprintf("=================================================\n");
4819 static char *
4820 ata_raid_sii_type(int type)
4822 static char buffer[16];
4824 switch (type) {
4825 case SII_T_RAID0: return "RAID0";
4826 case SII_T_RAID1: return "RAID1";
4827 case SII_T_RAID01: return "RAID0+1";
4828 case SII_T_SPARE: return "SPARE";
4829 default: ksprintf(buffer, "UNKNOWN 0x%02x", type);
4830 return buffer;
4834 static void
4835 ata_raid_sii_print_meta(struct sii_raid_conf *meta)
4837 kprintf("******* ATA Silicon Image Medley Metadata *******\n");
4838 kprintf("total_sectors %ju\n", meta->total_sectors);
4839 kprintf("dummy_0 0x%04x\n", meta->dummy_0);
4840 kprintf("dummy_1 0x%04x\n", meta->dummy_1);
4841 kprintf("controller_pci_id 0x%08x\n", meta->controller_pci_id);
4842 kprintf("version_minor 0x%04x\n", meta->version_minor);
4843 kprintf("version_major 0x%04x\n", meta->version_major);
4844 kprintf("timestamp 20%02x/%02x/%02x %02x:%02x:%02x\n",
4845 meta->timestamp[5], meta->timestamp[4], meta->timestamp[3],
4846 meta->timestamp[2], meta->timestamp[1], meta->timestamp[0]);
4847 kprintf("stripe_sectors %u\n", meta->stripe_sectors);
4848 kprintf("dummy_2 0x%04x\n", meta->dummy_2);
4849 kprintf("disk_number %u\n", meta->disk_number);
4850 kprintf("type %s\n", ata_raid_sii_type(meta->type));
4851 kprintf("raid0_disks %u\n", meta->raid0_disks);
4852 kprintf("raid0_ident %u\n", meta->raid0_ident);
4853 kprintf("raid1_disks %u\n", meta->raid1_disks);
4854 kprintf("raid1_ident %u\n", meta->raid1_ident);
4855 kprintf("rebuild_lba %ju\n", meta->rebuild_lba);
4856 kprintf("generation 0x%08x\n", meta->generation);
4857 kprintf("status 0x%02x %b\n",
4858 meta->status, meta->status,
4859 "\20\1READY\n");
4860 kprintf("base_raid1_position %02x\n", meta->base_raid1_position);
4861 kprintf("base_raid0_position %02x\n", meta->base_raid0_position);
4862 kprintf("position %02x\n", meta->position);
4863 kprintf("dummy_3 %04x\n", meta->dummy_3);
4864 kprintf("name <%.16s>\n", meta->name);
4865 kprintf("checksum_0 0x%04x\n", meta->checksum_0);
4866 kprintf("checksum_1 0x%04x\n", meta->checksum_1);
4867 kprintf("=================================================\n");
4870 static char *
4871 ata_raid_sis_type(int type)
4873 static char buffer[16];
4875 switch (type) {
4876 case SIS_T_JBOD: return "JBOD";
4877 case SIS_T_RAID0: return "RAID0";
4878 case SIS_T_RAID1: return "RAID1";
4879 default: ksprintf(buffer, "UNKNOWN 0x%02x", type);
4880 return buffer;
4884 static void
4885 ata_raid_sis_print_meta(struct sis_raid_conf *meta)
4887 kprintf("**** ATA Silicon Integrated Systems Metadata ****\n");
4888 kprintf("magic 0x%04x\n", meta->magic);
4889 kprintf("disks 0x%02x\n", meta->disks);
4890 kprintf("type %s\n",
4891 ata_raid_sis_type(meta->type_total_disks & SIS_T_MASK));
4892 kprintf("total_disks %u\n", meta->type_total_disks & SIS_D_MASK);
4893 kprintf("dummy_0 0x%08x\n", meta->dummy_0);
4894 kprintf("controller_pci_id 0x%08x\n", meta->controller_pci_id);
4895 kprintf("stripe_sectors %u\n", meta->stripe_sectors);
4896 kprintf("dummy_1 0x%04x\n", meta->dummy_1);
4897 kprintf("timestamp 0x%08x\n", meta->timestamp);
4898 kprintf("model %.40s\n", meta->model);
4899 kprintf("disk_number %u\n", meta->disk_number);
4900 kprintf("dummy_2 0x%02x 0x%02x 0x%02x\n",
4901 meta->dummy_2[0], meta->dummy_2[1], meta->dummy_2[2]);
4902 kprintf("=================================================\n");
4905 static char *
4906 ata_raid_via_type(int type)
4908 static char buffer[16];
4910 switch (type) {
4911 case VIA_T_RAID0: return "RAID0";
4912 case VIA_T_RAID1: return "RAID1";
4913 case VIA_T_RAID5: return "RAID5";
4914 case VIA_T_RAID01: return "RAID0+1";
4915 case VIA_T_SPAN: return "SPAN";
4916 default: ksprintf(buffer, "UNKNOWN 0x%02x", type);
4917 return buffer;
4921 static void
4922 ata_raid_via_print_meta(struct via_raid_conf *meta)
4924 int i;
4926 kprintf("*************** ATA VIA Metadata ****************\n");
4927 kprintf("magic 0x%02x\n", meta->magic);
4928 kprintf("dummy_0 0x%02x\n", meta->dummy_0);
4929 kprintf("type %s\n",
4930 ata_raid_via_type(meta->type & VIA_T_MASK));
4931 kprintf("bootable %d\n", meta->type & VIA_T_BOOTABLE);
4932 kprintf("unknown %d\n", meta->type & VIA_T_UNKNOWN);
4933 kprintf("disk_index 0x%02x\n", meta->disk_index);
4934 kprintf("stripe_layout 0x%02x\n", meta->stripe_layout);
4935 kprintf(" stripe_disks %d\n", meta->stripe_layout & VIA_L_DISKS);
4936 kprintf(" stripe_sectors %d\n",
4937 0x08 << ((meta->stripe_layout & VIA_L_MASK) >> VIA_L_SHIFT));
4938 kprintf("disk_sectors %ju\n", meta->disk_sectors);
4939 kprintf("disk_id 0x%08x\n", meta->disk_id);
4940 kprintf("DISK# disk_id\n");
4941 for (i = 0; i < 8; i++) {
4942 if (meta->disks[i])
4943 kprintf(" %d 0x%08x\n", i, meta->disks[i]);
4945 kprintf("checksum 0x%02x\n", meta->checksum);
4946 kprintf("=================================================\n");