1 /* $NetBSD: ata_raid_jmicron.c,v 1.3 2008/09/15 11:44:50 tron Exp $ */
4 * Copyright (c) 2000-2008 Søren Schmidt <sos@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * Support for parsing JMicron Technology RAID controller configuration blocks.
34 * Adapted to NetBSD by Juan Romero Pardines (xtraeme@gmail.org).
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: ata_raid_jmicron.c,v 1.3 2008/09/15 11:44:50 tron Exp $");
40 #include <sys/param.h>
44 #include <sys/device.h>
46 #include <sys/disklabel.h>
47 #include <sys/fcntl.h>
48 #include <sys/malloc.h>
49 #include <sys/vnode.h>
50 #include <sys/kauth.h>
52 #include <miscfs/specfs/specdev.h>
54 #include <dev/ata/atareg.h>
55 #include <dev/ata/atavar.h>
56 #include <dev/ata/wdvar.h>
58 #include <dev/ata/ata_raidreg.h>
59 #include <dev/ata/ata_raidvar.h>
62 #define DPRINTF(x) printf x
64 #define DPRINTF(x) /* nothing */
69 ata_raid_jmicron_type(int type
)
71 static char buffer
[16];
85 sprintf(buffer
, "UNKNOWN 0x%02x", type
);
91 ata_raid_jmicron_print_info(struct jmicron_raid_conf
*info
)
95 printf("****** ATA JMicron Technology Corp Metadata ******\n");
96 printf("signature %.2s\n", info
->signature
);
97 printf("version 0x%04x\n", info
->version
);
98 printf("checksum 0x%04x\n", info
->checksum
);
99 printf("disk_id 0x%08x\n", info
->disk_id
);
100 printf("offset 0x%08x\n", info
->offset
);
101 printf("disk_sectors_low 0x%08x\n", info
->disk_sectors_low
);
102 printf("disk_sectors_high 0x%08x\n", info
->disk_sectors_high
);
103 printf("name %.16s\n", info
->name
);
105 ata_raid_jmicron_type(info
->type
));
106 printf("stripe_shift %d\n", info
->stripe_shift
);
107 printf("flags 0x%04x\n", info
->flags
);
109 for (i
= 0; i
< 2 && info
->spare
[i
]; i
++)
110 printf(" %d 0x%08x\n", i
, info
->spare
[i
]);
112 for (i
= 0; i
< 8 && info
->disks
[i
]; i
++)
113 printf(" %d 0x%08x\n", i
, info
->disks
[i
]);
114 printf("=================================================\n");
119 ata_raid_read_config_jmicron(struct wd_softc
*sc
)
121 struct atabus_softc
*atabus
;
122 struct jmicron_raid_conf
*info
;
124 struct ataraid_array_info
*aai
;
125 struct ataraid_disk_info
*adi
;
128 uint16_t checksum
, *ptr
;
129 int bmajor
, error
, count
, disk
, total_disks
;
132 info
= malloc(sizeof(*info
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
134 bmajor
= devsw_name2blk(device_xname(sc
->sc_dev
), NULL
, 0);
136 /* Get a vnode for the raw partition of this disk. */
137 dev
= MAKEDISKDEV(bmajor
, device_unit(sc
->sc_dev
), RAW_PART
);
138 error
= bdevvp(dev
, &vp
);
142 error
= VOP_OPEN(vp
, FREAD
, NOCRED
);
148 error
= ata_raid_config_block_rw(vp
, JMICRON_LBA(sc
), info
,
149 sizeof(*info
), B_READ
);
150 VOP_CLOSE(vp
, FREAD
, NOCRED
);
153 DPRINTF(("%s: error %d reading JMicron config block\n",
154 device_xname(sc
->sc_dev
), error
));
158 /* Check for JMicron signature. */
159 if (strncmp(info
->signature
, JMICRON_MAGIC
, 2)) {
160 DPRINTF(("%s: JMicron RAID signature check failed\n",
161 device_xname(sc
->sc_dev
)));
166 /* calculate checksum and compare for valid */
167 for (checksum
= 0, ptr
= (uint16_t *)info
, count
= 0;
171 DPRINTF(("%s: JMicron checksum failed\n",
172 device_xname(sc
->sc_dev
)));
177 #ifdef ATA_RAID_DEBUG
178 ata_raid_jmicron_print_info(info
);
181 * Check that there aren't stale config blocks without
182 * any array set configured.
184 for (total_disks
= 0, disk
= 0; disk
< JM_MAX_DISKS
; disk
++)
185 if (info
->disks
[disk
] == info
->disk_id
)
187 if (total_disks
<= 1) {
193 * Check volume's state and bail out if it's not acceptable.
195 if ((info
->flags
& (JM_F_READY
|JM_F_BOOTABLE
|JM_F_ACTIVE
)) == 0) {
201 * Lookup or allocate a new array info structure for
204 aai
= ata_raid_get_array_info(ATA_RAID_TYPE_JMICRON
, 0);
205 aai
->aai_status
= AAI_S_READY
;
207 switch (info
->type
) {
209 aai
->aai_level
= AAI_L_RAID0
;
210 aai
->aai_width
= total_disks
;
213 aai
->aai_level
= AAI_L_RAID1
;
217 aai
->aai_level
= AAI_L_RAID0
| AAI_L_RAID1
;
218 aai
->aai_width
= total_disks
/ 2;
221 aai
->aai_level
= AAI_L_SPAN
;
222 aai
->aai_width
= total_disks
;
225 DPRINTF(("%s: unknown JMicron RAID type 0x%02x\n",
226 device_xname(sc
->sc_dev
), info
->type
));
231 disk_size
= (info
->disk_sectors_high
<< 16) + info
->disk_sectors_low
;
232 aai
->aai_type
= ATA_RAID_TYPE_JMICRON
;
233 aai
->aai_generation
= 0;
234 aai
->aai_capacity
= disk_size
* aai
->aai_width
;
235 aai
->aai_interleave
= 2 << info
->stripe_shift
;
236 aai
->aai_ndisks
= total_disks
;
237 aai
->aai_heads
= 255;
238 aai
->aai_sectors
= 63;
240 aai
->aai_capacity
/ (aai
->aai_heads
* aai
->aai_sectors
);
241 aai
->aai_offset
= info
->offset
* 16;
242 aai
->aai_reserved
= 2;
244 strlcpy(aai
->aai_name
, info
->name
, sizeof(aai
->aai_name
));
246 atabus
= device_private(device_parent(sc
->sc_dev
));
247 drive
= atabus
->sc_chan
->ch_channel
;
248 if (drive
>= aai
->aai_ndisks
) {
249 DPRINTF(("%s: drive number %d doesn't make sense within "
250 "%d-disk array\n", device_xname(sc
->sc_dev
),
251 drive
, aai
->aai_ndisks
));
256 if (info
->disks
[drive
] == info
->disk_id
) {
257 adi
= &aai
->aai_disks
[drive
];
258 adi
->adi_dev
= sc
->sc_dev
;
259 adi
->adi_status
= ADI_S_ONLINE
| ADI_S_ASSIGNED
;
260 adi
->adi_sectors
= aai
->aai_capacity
;
261 adi
->adi_compsize
= disk_size
- aai
->aai_reserved
;
267 free(info
, M_DEVBUF
);