xref: /netbsd-src/sys/dev/ata/ata_raid_adaptec.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: ata_raid_adaptec.c,v 1.2 2005/12/11 12:21:14 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000,2001,2002 S�ren Schmidt <sos@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /*
32  * Support for parsing Adaptec ATA RAID controller configuration blocks.
33  *
34  * Adapted to NetBSD by Allen K. Briggs
35  */
36 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: ata_raid_adaptec.c,v 1.2 2005/12/11 12:21:14 christos Exp $");
39 
40 #include <sys/param.h>
41 #include <sys/buf.h>
42 #include <sys/bufq.h>
43 #include <sys/conf.h>
44 #include <sys/device.h>
45 #include <sys/disk.h>
46 #include <sys/disklabel.h>
47 #include <sys/fcntl.h>
48 #include <sys/malloc.h>
49 #include <sys/vnode.h>
50 
51 #include <miscfs/specfs/specdev.h>
52 
53 #include <dev/ata/atareg.h>
54 #include <dev/ata/atavar.h>
55 #include <dev/ata/wdvar.h>
56 
57 #include <dev/ata/ata_raidreg.h>
58 #include <dev/ata/ata_raidvar.h>
59 
60 #ifdef ATA_RAID_DEBUG
61 #define	DPRINTF(x)	printf x
62 #else
63 #define	DPRINTF(x)	/* nothing */
64 #endif
65 
66 int
67 ata_raid_read_config_adaptec(struct wd_softc *sc)
68 {
69 	struct adaptec_raid_conf *info;
70 	struct atabus_softc *atabus;
71 	struct vnode *vp;
72 	int bmajor, error;
73 	dev_t dev;
74 	uint32_t gen, drive;
75 	struct ataraid_array_info *aai;
76 	struct ataraid_disk_info *adi;
77 
78 	info = malloc(sizeof(*info), M_DEVBUF, M_WAITOK);
79 
80 	bmajor = devsw_name2blk(sc->sc_dev.dv_xname, NULL, 0);
81 
82 	/* Get a vnode for the raw partition of this disk. */
83 	dev = MAKEDISKDEV(bmajor, sc->sc_dev.dv_unit, RAW_PART);
84 	error = bdevvp(dev, &vp);
85 	if (error)
86 		goto out;
87 
88 	error = VOP_OPEN(vp, FREAD, NOCRED, 0);
89 	if (error) {
90 		vput(vp);
91 		goto out;
92 	}
93 
94 	error = ata_raid_config_block_rw(vp, ADP_LBA(sc), info,
95 	    sizeof(*info), B_READ);
96 	vput(vp);
97 	if (error) {
98 		printf("%s: error %d reading Adaptec config block\n",
99 		    sc->sc_dev.dv_xname, error);
100 		goto out;
101 	}
102 
103 	info->magic_0 = be32toh(info->magic_0);
104 	info->magic_1 = be32toh(info->magic_1);
105 	info->magic_2 = be32toh(info->magic_2);
106 	info->magic_3 = be32toh(info->magic_3);
107 	info->magic_4 = be32toh(info->magic_4);
108 	/* Check the signature. */
109 	if (info->magic_0 != ADP_MAGIC_0 || info->magic_3 != ADP_MAGIC_3) {
110 		DPRINTF(("%s: Adaptec signature check failed\n",
111 		    sc->sc_dev.dv_xname));
112 		error = ESRCH;
113 		goto out;
114 	}
115 
116 	/*
117 	 * Lookup or allocate a new array info structure for
118 	 * this array.
119 	 */
120 	aai = ata_raid_get_array_info(ATA_RAID_TYPE_ADAPTEC,
121 		be32toh(info->configs[0].disk_number));
122 
123 	gen = be32toh(info->generation);
124 
125 	if (gen == 0 || gen > aai->aai_generation) {
126 		aai->aai_generation = gen;
127 
128 		aai->aai_status = AAI_S_READY;
129 
130 		switch (info->configs[0].type) {
131 		case ADP_T_RAID0:
132 			aai->aai_level = AAI_L_RAID0;
133 			aai->aai_interleave =
134 			    (be16toh(info->configs[0].stripe_sectors) >> 1);
135 			aai->aai_width = be16toh(info->configs[0].total_disks);
136 			break;
137 
138 		case ADP_T_RAID1:
139 			aai->aai_level = AAI_L_RAID1;
140 			aai->aai_interleave = 0;
141 			aai->aai_width = be16toh(info->configs[0].total_disks)
142 			    / 2;
143 			break;
144 
145 		default:
146 			aprint_error("%s: unknown Adaptec RAID type 0x%02x\n",
147 			    sc->sc_dev.dv_xname, info->configs[0].type);
148 			error = EINVAL;
149 			goto out;
150 		}
151 
152 		aai->aai_type = ATA_RAID_TYPE_ADAPTEC;
153 		aai->aai_ndisks = be16toh(info->configs[0].total_disks);
154 		aai->aai_capacity = be32toh(info->configs[0].sectors);
155 		aai->aai_heads = 255;
156 		aai->aai_sectors = 63;
157 		aai->aai_cylinders = aai->aai_capacity / (63 * 255);
158 		aai->aai_offset = 0;
159 		aai->aai_reserved = 17;
160 
161 		/* XXX - bogus.  RAID1 shouldn't really have an interleave */
162 		if (aai->aai_interleave == 0)
163 			aai->aai_interleave = aai->aai_capacity;
164 	}
165 
166 	atabus = (struct atabus_softc *) sc->sc_dev.dv_parent;
167 	drive = atabus->sc_chan->ch_channel;
168 	if (drive >= aai->aai_ndisks) {
169 		aprint_error("%s: drive number %d doesn't make sense within "
170 		    "%d-disk array\n",
171 		    sc->sc_dev.dv_xname, drive, aai->aai_ndisks);
172 		error = EINVAL;
173 		goto out;
174 	}
175 
176 	adi = &aai->aai_disks[drive];
177 	adi->adi_dev = &sc->sc_dev;
178 	adi->adi_status = ADI_S_ONLINE | ADI_S_ASSIGNED;
179 	adi->adi_sectors = aai->aai_capacity;
180 	adi->adi_compsize = be32toh(info->configs[drive+1].sectors);
181 
182 	error = 0;
183 
184  out:
185 	free(info, M_DEVBUF);
186 	return (error);
187 }
188