xref: /netbsd-src/sys/arch/cobalt/stand/boot/wd.c (revision 7f21db1c0118155e0dd40b75182e30c589d9f63e)
1 /*	$NetBSD: wd.c,v 1.12 2010/01/19 15:28:52 tsutsui Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Manuel Bouyer.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/types.h>
33 #include <sys/stdint.h>
34 
35 #include <lib/libsa/stand.h>
36 #include <lib/libkern/libkern.h>
37 
38 #include <machine/param.h>
39 #include <machine/stdarg.h>
40 #include <dev/raidframe/raidframevar.h>		/* For RF_PROTECTED_SECTORS */
41 
42 #include "boot.h"
43 #include "wdvar.h"
44 
45 static int  wd_get_params(struct wd_softc *wd);
46 static int  wdgetdisklabel(struct wd_softc *wd);
47 static void wdgetdefaultlabel(struct wd_softc *wd, struct disklabel *lp);
48 
49 /*
50  * Get drive parameters through 'device identify' command.
51  */
52 int
53 wd_get_params(struct wd_softc *wd)
54 {
55 	int error;
56 	uint8_t buf[DEV_BSIZE];
57 
58 	if ((error = wdc_exec_identify(wd, buf)) != 0)
59 		return error;
60 
61 	wd->sc_params = *(struct ataparams *)buf;
62 
63 	/* 48-bit LBA addressing */
64 	if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0)
65 		wd->sc_flags |= WDF_LBA48;
66 
67 	/* Prior to ATA-4, LBA was optional. */
68 	if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0)
69 		wd->sc_flags |= WDF_LBA;
70 
71 	if ((wd->sc_flags & WDF_LBA48) != 0) {
72 		DPRINTF(("Drive supports LBA48.\n"));
73 		wd->sc_capacity =
74 		    ((uint64_t)wd->sc_params.atap_max_lba[3] << 48) |
75 		    ((uint64_t)wd->sc_params.atap_max_lba[2] << 32) |
76 		    ((uint64_t)wd->sc_params.atap_max_lba[1] << 16) |
77 		    ((uint64_t)wd->sc_params.atap_max_lba[0] <<  0);
78 		DPRINTF(("atap_max_lba = (0x%x, 0x%x, 0x%x, 0x%x)\n",
79 		    wd->sc_params.atap_max_lba[3],
80 		    wd->sc_params.atap_max_lba[2],
81 		    wd->sc_params.atap_max_lba[1],
82 		    wd->sc_params.atap_max_lba[0]));
83 		wd->sc_capacity28 =
84 		    ((uint32_t)wd->sc_params.atap_capacity[1] << 16) |
85 		    ((uint32_t)wd->sc_params.atap_capacity[0] <<  0);
86 		DPRINTF(("atap_capacity = (0x%x, 0x%x)\n",
87 		    wd->sc_params.atap_capacity[1],
88 		    wd->sc_params.atap_capacity[0]));
89 	} else if ((wd->sc_flags & WDF_LBA) != 0) {
90 		DPRINTF(("Drive supports LBA.\n"));
91 		wd->sc_capacity = wd->sc_capacity28 =
92 		    ((uint32_t)wd->sc_params.atap_capacity[1] << 16) |
93 		    ((uint32_t)wd->sc_params.atap_capacity[0] <<  0);
94 	} else {
95 		DPRINTF(("Drive doesn't support LBA; using CHS.\n"));
96 		wd->sc_capacity = wd->sc_capacity28 =
97 		    wd->sc_params.atap_cylinders *
98 		    wd->sc_params.atap_heads *
99 		    wd->sc_params.atap_sectors;
100 	}
101 	DPRINTF(("wd->sc_capacity = %" PRId64 ", wd->sc_capacity28 = %d.\n",
102 	    wd->sc_capacity, wd->sc_capacity28));
103 
104 	return 0;
105 }
106 
107 /*
108  * Initialize disk label to the default value.
109  */
110 void
111 wdgetdefaultlabel(struct wd_softc *wd, struct disklabel *lp)
112 {
113 
114 	memset(lp, 0, sizeof(struct disklabel));
115 
116 	lp->d_secsize = DEV_BSIZE;
117 	lp->d_ntracks = wd->sc_params.atap_heads;
118 	lp->d_nsectors = wd->sc_params.atap_sectors;
119 	lp->d_ncylinders = wd->sc_params.atap_cylinders;
120 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
121 
122 	if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
123 		lp->d_type = DTYPE_ST506;
124 	else
125 		lp->d_type = DTYPE_ESDI;
126 
127 	strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
128 	strncpy(lp->d_packname, "fictitious", 16);
129 	if (wd->sc_capacity > UINT32_MAX)
130 		lp->d_secperunit = UINT32_MAX;
131 	else
132 		lp->d_secperunit = wd->sc_capacity;
133 	lp->d_rpm = 3600;
134 	lp->d_interleave = 1;
135 	lp->d_flags = 0;
136 
137 	lp->d_partitions[RAW_PART].p_offset = 0;
138 	lp->d_partitions[RAW_PART].p_size =
139 	lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
140 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
141 	lp->d_npartitions = MAXPARTITIONS;	/* RAW_PART + 1 ??? */
142 
143 	lp->d_magic = DISKMAGIC;
144 	lp->d_magic2 = DISKMAGIC;
145 	lp->d_checksum = dkcksum(lp);
146 }
147 
148 /*
149  * Read disk label from the device.
150  */
151 int
152 wdgetdisklabel(struct wd_softc *wd)
153 {
154 	char *msg;
155 	int sector;
156 	size_t rsize;
157 	struct disklabel *lp;
158 	uint8_t buf[DEV_BSIZE];
159 
160 	wdgetdefaultlabel(wd, &wd->sc_label);
161 
162 	/*
163 	 * Find NetBSD Partition in DOS partition table.
164 	 */
165 	sector = 0;
166 	if (wdstrategy(wd, F_READ, MBR_BBSECTOR, DEV_BSIZE, buf, &rsize))
167 		return EOFFSET;
168 
169 	if (*(uint16_t *)&buf[MBR_MAGIC_OFFSET] == MBR_MAGIC) {
170 		int i;
171 		struct mbr_partition *mp;
172 
173 		/*
174 		 * Lookup NetBSD slice. If there is none, go ahead
175 		 * and try to read the disklabel off sector #0.
176 		 */
177 		mp = (struct mbr_partition *)&buf[MBR_PART_OFFSET];
178 		for (i = 0; i < MBR_PART_COUNT; i++) {
179 			if (mp[i].mbrp_type == MBR_PTYPE_NETBSD) {
180 				sector = mp[i].mbrp_start;
181 				break;
182 			}
183 		}
184 	}
185 
186 	if (wdstrategy(wd, F_READ, sector + LABELSECTOR, DEV_BSIZE,
187 	    buf, &rsize))
188 		return EOFFSET;
189 
190 	if ((msg = getdisklabel(buf + LABELOFFSET, &wd->sc_label)))
191 		printf("wd%d: getdisklabel: %s\n", wd->sc_unit, msg);
192 
193 	lp = &wd->sc_label;
194 
195 	/* check partition */
196 	if ((wd->sc_part >= lp->d_npartitions) ||
197 	    (lp->d_partitions[wd->sc_part].p_fstype == FS_UNUSED)) {
198 		DPRINTF(("illegal partition\n"));
199 		return EPART;
200 	}
201 
202 	DPRINTF(("label info: d_secsize %d, d_nsectors %d, d_ncylinders %d,"
203 	    " d_ntracks %d, d_secpercyl %d\n",
204 	    wd->sc_label.d_secsize,
205 	    wd->sc_label.d_nsectors,
206 	    wd->sc_label.d_ncylinders,
207 	    wd->sc_label.d_ntracks,
208 	    wd->sc_label.d_secpercyl));
209 
210 	return 0;
211 }
212 
213 /*
214  * Open device (read drive parameters and disklabel)
215  */
216 int
217 wdopen(struct open_file *f, ...)
218 {
219 	int error;
220 	va_list ap;
221 	u_int unit, part;
222 	struct wd_softc *wd;
223 
224 	va_start(ap, f);
225 	unit = va_arg(ap, u_int);
226 	part = va_arg(ap, u_int);
227 	va_end(ap);
228 
229 	DPRINTF(("wdopen: %d:%d\n", unit, part));
230 
231 	wd = alloc(sizeof(struct wd_softc));
232 	if (wd == NULL)
233 		return ENOMEM;
234 
235 	memset(wd, 0, sizeof(struct wd_softc));
236 
237 	if (wdc_init(wd, &unit) != 0)
238 		return (ENXIO);
239 
240 	wd->sc_part = part;
241 	wd->sc_unit = unit;
242 
243 	if ((error = wd_get_params(wd)) != 0)
244 		return error;
245 
246 	if ((error = wdgetdisklabel(wd)) != 0)
247 		return error;
248 
249 	f->f_devdata = wd;
250 	return 0;
251 }
252 
253 /*
254  * Close device.
255  */
256 int
257 wdclose(struct open_file *f)
258 {
259 
260 	return 0;
261 }
262 
263 /*
264  * Read some data.
265  */
266 int
267 wdstrategy(void *f, int rw, daddr_t dblk, size_t size, void *p, size_t *rsize)
268 {
269 	int i, nsect;
270 	daddr_t blkno;
271 	struct wd_softc *wd;
272 	struct partition *pp;
273 	uint8_t *buf;
274 
275 	if (size == 0)
276 		return 0;
277 
278 	if (rw != F_READ)
279 		return EOPNOTSUPP;
280 
281 	buf = p;
282 	wd = f;
283 	pp = &wd->sc_label.d_partitions[wd->sc_part];
284 
285 	nsect = howmany(size, wd->sc_label.d_secsize);
286 	blkno = dblk + pp->p_offset;
287 	if (pp->p_fstype == FS_RAID)
288 		blkno += RF_PROTECTED_SECTORS;
289 
290 	for (i = 0; i < nsect; i++, blkno++) {
291 		int error;
292 
293 		if ((error = wdc_exec_read(wd, WDCC_READ, blkno, buf)) != 0)
294 			return error;
295 
296 		buf += wd->sc_label.d_secsize;
297 	}
298 
299 	*rsize = size;
300 	return 0;
301 }
302