xref: /netbsd/sys/dev/ofw/ofdisk.c (revision bf9ec67e)
1 /*	$NetBSD: ofdisk.c,v 1.18 2001/11/13 07:26:28 lukem Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5  * Copyright (C) 1995, 1996 TooLs GmbH.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
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. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by TooLs GmbH.
19  * 4. The name of TooLs GmbH may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ofdisk.c,v 1.18 2001/11/13 07:26:28 lukem Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/buf.h>
39 #include <sys/device.h>
40 #include <sys/conf.h>
41 #include <sys/disklabel.h>
42 #include <sys/disk.h>
43 #include <sys/fcntl.h>
44 #include <sys/ioctl.h>
45 #include <sys/stat.h>
46 #include <sys/systm.h>
47 #include <sys/proc.h>
48 
49 #include <dev/ofw/openfirm.h>
50 
51 struct ofdisk_softc {
52 	struct device sc_dev;
53 	int sc_phandle;
54 	int sc_unit;
55 	int sc_flags;
56 	struct disk sc_dk;
57 	int sc_ihandle;
58 	u_long max_transfer;
59 	char sc_name[16];
60 };
61 
62 bdev_decl(ofdisk_);
63 cdev_decl(ofdisk_);
64 
65 /* sc_flags */
66 #define OFDF_ISFLOPPY	0x01		/* we are a floppy drive */
67 
68 static int ofdisk_match (struct device *, struct cfdata *, void *);
69 static void ofdisk_attach (struct device *, struct device *, void *);
70 
71 struct cfattach ofdisk_ca = {
72 	sizeof(struct ofdisk_softc), ofdisk_match, ofdisk_attach
73 };
74 
75 extern struct cfdriver ofdisk_cd;
76 
77 void ofdisk_strategy (struct buf *);
78 
79 struct dkdriver ofdisk_dkdriver = { ofdisk_strategy };
80 
81 void ofdisk_getdefaultlabel (struct ofdisk_softc *, struct disklabel *);
82 void ofdisk_getdisklabel (dev_t);
83 
84 static int
85 ofdisk_match(struct device *parent, struct cfdata *match, void *aux)
86 {
87 	struct ofbus_attach_args *oba = aux;
88 	char type[8];
89 	int l;
90 
91 	if (strcmp(oba->oba_busname, "ofw"))
92 		return (0);
93 	if ((l = OF_getprop(oba->oba_phandle, "device_type", type,
94 	    sizeof type - 1)) < 0)
95 		return 0;
96 	if (l >= sizeof type)
97 		return 0;
98 	type[l] = 0;
99 	return !strcmp(type, "block");
100 }
101 
102 static void
103 ofdisk_attach(struct device *parent, struct device *self, void *aux)
104 {
105 	struct ofdisk_softc *of = (void *)self;
106 	struct ofbus_attach_args *oba = aux;
107 	char child[64];
108 	int l;
109 
110 	if ((l = OF_getprop(oba->oba_phandle, "name", child,
111 	    sizeof child - 1)) < 0)
112 		panic("device without name?");
113 	if (l >= sizeof child)
114 		l = sizeof child - 1;
115 	child[l] = 0;
116 
117 	of->sc_flags = 0;
118 	of->sc_phandle = oba->oba_phandle;
119 	of->sc_unit = oba->oba_unit;
120 	of->sc_ihandle = 0;
121 	of->sc_dk.dk_driver = &ofdisk_dkdriver;
122 	of->sc_dk.dk_name = of->sc_name;
123 	strcpy(of->sc_name, of->sc_dev.dv_xname);
124 	disk_attach(&of->sc_dk);
125 #ifdef __BROKEN_DK_ESTABLISH
126 	dk_establish(&of->sc_dk, self);				/* XXX */
127 #endif
128 	printf("\n");
129 
130 	if (strcmp(child, "floppy") == 0)
131 		of->sc_flags |= OFDF_ISFLOPPY;
132 }
133 
134 int
135 ofdisk_open(dev_t dev, int flags, int fmt, struct proc *p)
136 {
137 	int unit = DISKUNIT(dev);
138 	struct ofdisk_softc *of;
139 	char path[256];
140 	int l;
141 
142 	if (unit >= ofdisk_cd.cd_ndevs)
143 		return ENXIO;
144 	if (!(of = ofdisk_cd.cd_devs[unit]))
145 		return ENXIO;
146 
147 	if (!of->sc_ihandle) {
148 		if ((l = OF_package_to_path(of->sc_phandle, path,
149 		    sizeof path - 3)) < 0 ||
150 		    l >= sizeof path - 3)
151 			return ENXIO;
152 		path[l] = 0;
153 
154 		/*
155 		 * XXX This is for the benefit of SCSI/IDE disks that don't
156 		 * XXX have all their childs in the device tree.
157 		 * XXX YES, I DO THINK THIS IS A BUG IN OPENFIRMWARE!!!
158 		 * XXX And yes, this is a very gross hack!
159 		 * XXX See also ofscsi.c
160 		 */
161 		if (!strcmp(path + l - 4, "disk")) {
162 			path[l++] = '@';
163 			path[l++] = '0' + of->sc_unit;
164 			path[l] = 0;
165 		}
166 
167 		strcat(path, ":0");
168 
169 		if (!(of->sc_ihandle = OF_open(path)))
170 			return ENXIO;
171 
172 		/*
173 		 * Try to get characteristics of the disk.
174 		 */
175 		of->max_transfer = OF_call_method_1("max-transfer",
176 		    of->sc_ihandle, 0);
177 		if (of->max_transfer > MAXPHYS)
178 			of->max_transfer = MAXPHYS;
179 
180 		ofdisk_getdisklabel(dev);
181 	}
182 
183 	switch (fmt) {
184 	case S_IFCHR:
185 		of->sc_dk.dk_copenmask |= 1 << DISKPART(dev);
186 		break;
187 	case S_IFBLK:
188 		of->sc_dk.dk_bopenmask |= 1 << DISKPART(dev);
189 		break;
190 	}
191 	of->sc_dk.dk_openmask =
192 	    of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
193 
194 	return 0;
195 }
196 
197 int
198 ofdisk_close(dev_t dev, int flags, int fmt, struct proc *p)
199 {
200 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
201 
202 	switch (fmt) {
203 	case S_IFCHR:
204 		of->sc_dk.dk_copenmask &= ~(1 << DISKPART(dev));
205 		break;
206 	case S_IFBLK:
207 		of->sc_dk.dk_bopenmask &= ~(1 << DISKPART(dev));
208 		break;
209 	}
210 	of->sc_dk.dk_openmask = of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
211 
212 #ifdef	FIRMWORKSBUGS
213 	/*
214 	 * This is a hack to get the firmware to flush its buffers.
215 	 */
216 	OF_seek(of->sc_ihandle, 0);
217 #endif
218 	if (!of->sc_dk.dk_openmask) {
219 		OF_close(of->sc_ihandle);
220 		of->sc_ihandle = 0;
221 	}
222 
223 	return 0;
224 }
225 
226 void
227 ofdisk_strategy(struct buf *bp)
228 {
229 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)];
230 	struct partition *p;
231 	u_quad_t off;
232 	int read;
233 	int (*OF_io)(int, void *, int);
234 	daddr_t blkno = bp->b_blkno;
235 
236 	bp->b_resid = 0;
237 	if (bp->b_bcount == 0)
238 		goto done;
239 
240 	OF_io = bp->b_flags & B_READ ? OF_read : OF_write;
241 
242 	if (DISKPART(bp->b_dev) != RAW_PART) {
243 		if (bounds_check_with_label(bp, of->sc_dk.dk_label, 0) <= 0) {
244 			bp->b_resid = bp->b_bcount;
245 			goto done;
246 		}
247 		p = &of->sc_dk.dk_label->d_partitions[DISKPART(bp->b_dev)];
248 		blkno = bp->b_blkno + p->p_offset;
249 	}
250 
251 	disk_busy(&of->sc_dk);
252 
253 	off = (u_quad_t)blkno * DEV_BSIZE;
254 	read = -1;
255 	do {
256 		if (OF_seek(of->sc_ihandle, off) < 0)
257 			break;
258 		read = OF_io(of->sc_ihandle, bp->b_data, bp->b_bcount);
259 	} while (read == -2);
260 
261 	if (read < 0) {
262 		bp->b_error = EIO;
263 		bp->b_flags |= B_ERROR;
264 		bp->b_resid = bp->b_bcount;
265 	} else
266 		bp->b_resid = bp->b_bcount - read;
267 
268 	disk_unbusy(&of->sc_dk, bp->b_bcount - bp->b_resid);
269 
270 done:
271 	biodone(bp);
272 }
273 
274 static void
275 ofminphys(struct buf *bp)
276 {
277 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)];
278 
279 	if (bp->b_bcount > of->max_transfer)
280 		bp->b_bcount = of->max_transfer;
281 }
282 
283 int
284 ofdisk_read(dev_t dev, struct uio *uio, int flags)
285 {
286 	return physio(ofdisk_strategy, NULL, dev, B_READ, ofminphys, uio);
287 }
288 
289 int
290 ofdisk_write(dev_t dev, struct uio *uio, int flags)
291 {
292 	return physio(ofdisk_strategy, NULL, dev, B_WRITE, ofminphys, uio);
293 }
294 
295 int
296 ofdisk_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
297 {
298 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
299 	int error;
300 #ifdef __HAVE_OLD_DISKLABEL
301 	struct disklabel newlabel;
302 #endif
303 
304 	switch (cmd) {
305 	case DIOCGDINFO:
306 		*(struct disklabel *)data = *of->sc_dk.dk_label;
307 		return 0;
308 #ifdef __HAVE_OLD_DISKLABEL
309 	case ODIOCGDINFO:
310 		newlabel = *of->sc_dk.dk_label;
311 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
312 			return ENOTTY;
313 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
314 		return 0;
315 #endif
316 
317 	case DIOCGPART:
318 		((struct partinfo *)data)->disklab = of->sc_dk.dk_label;
319 		((struct partinfo *)data)->part =
320 			&of->sc_dk.dk_label->d_partitions[DISKPART(dev)];
321 		return 0;
322 
323 	case DIOCWDINFO:
324 	case DIOCSDINFO:
325 #ifdef __HAVE_OLD_DISKLABEL
326 	case ODIOCWDINFO:
327 	case ODIOCSDINFO:
328 #endif
329 	{
330 		struct disklabel *lp;
331 
332 #ifdef __HAVE_OLD_DISKLABEL
333 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
334 			memset(&newlabel, 0, sizeof newlabel);
335 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
336 			lp = &newlabel;
337 		} else
338 #endif
339 		lp = (struct disklabel *)data;
340 
341 		if ((flag & FWRITE) == 0)
342 			return EBADF;
343 
344 		error = setdisklabel(of->sc_dk.dk_label,
345 		    lp, /*of->sc_dk.dk_openmask */0,
346 		    of->sc_dk.dk_cpulabel);
347 		if (error == 0 && cmd == DIOCWDINFO
348 #ifdef __HAVE_OLD_DISKLABEL
349 		    || xfer == ODIOCWDINFO
350 #endif
351 		    )
352 			error = writedisklabel(MAKEDISKDEV(major(dev),
353 			    DISKUNIT(dev), RAW_PART), ofdisk_strategy,
354 			    of->sc_dk.dk_label, of->sc_dk.dk_cpulabel);
355 
356 		return error;
357 	}
358 
359 	case DIOCGDEFLABEL:
360 		ofdisk_getdefaultlabel(of, (struct disklabel *)data);
361 		return 0;
362 #ifdef __HAVE_OLD_DISKLABEL
363 	case DIOCGDEFLABEL:
364 		ofdisk_getdefaultlabel(of, &newlabel);
365 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
366 			return ENOTTY;
367 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
368 		return 0;
369 #endif
370 
371 	default:
372 		return ENOTTY;
373 	}
374 }
375 
376 int
377 ofdisk_dump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
378 {
379 	return EINVAL;
380 }
381 
382 int
383 ofdisk_size(dev_t dev)
384 {
385 	struct ofdisk_softc *of;
386 	struct disklabel *lp;
387 	int size, part, omask, unit;
388 
389 	unit = DISKUNIT(dev);
390 	if (unit >= ofdisk_cd.cd_ndevs ||
391 	    (of = ofdisk_cd.cd_devs[unit]) == NULL)
392 		return -1;
393 
394 	part = DISKPART(dev);
395 	omask = of->sc_dk.dk_openmask & (1 << part);
396 	lp = of->sc_dk.dk_label;
397 
398 	if (omask == 0 && ofdisk_open(dev, 0, S_IFBLK, curproc) != 0)
399 		return -1;
400 
401 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
402 		size = -1;
403 	else
404 		size = lp->d_partitions[part].p_size *
405 		    (lp->d_secsize / DEV_BSIZE);
406 
407 	if (omask == 0 && ofdisk_close(dev, 0, S_IFBLK, curproc) != 0)
408 		return -1;
409 
410 	return size;
411 }
412 
413 void
414 ofdisk_getdefaultlabel(struct ofdisk_softc *of, struct disklabel *lp)
415 {
416 
417 	memset(lp, 0, sizeof *lp);
418 
419 	/*
420 	 * XXX Firmware bug?  Asking for block size gives a
421 	 * XXX rediculous number!  So we use what the boot program
422 	 * XXX uses.
423 	 */
424 	lp->d_secsize = DEV_BSIZE;
425 
426 	lp->d_secperunit = OF_call_method_1("#blocks",
427 	    of->sc_ihandle, 0);
428 	if (lp->d_secperunit == (u_int32_t)-1)
429 		lp->d_secperunit = 0x7fffffff;
430 
431 	lp->d_secpercyl = 1;
432 	lp->d_nsectors = 1;
433 	lp->d_ntracks = 1;
434 	lp->d_ncylinders = lp->d_secperunit;
435 
436 	lp->d_partitions[RAW_PART].p_offset = 0;
437 	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
438 	lp->d_npartitions = RAW_PART + 1;
439 
440 	lp->d_magic = DISKMAGIC;
441 	lp->d_magic2 = DISKMAGIC;
442 	lp->d_checksum = dkcksum(lp);
443 }
444 
445 void
446 ofdisk_getdisklabel(dev)
447 	dev_t dev;
448 {
449 	int unit = DISKUNIT(dev);
450 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[unit];
451 	struct disklabel *lp = of->sc_dk.dk_label;
452 	char *errmes;
453 	int l;
454 
455 	ofdisk_getdefaultlabel(of, lp);
456 
457 	/*
458 	 * Don't read the disklabel on a floppy; simply
459 	 * assign all partitions the same size/offset as
460 	 * RAW_PART.  (This is essentially what the ISA
461 	 * floppy driver does, but we don't deal with
462 	 * density stuff.)
463 	 */
464 	if (of->sc_flags & OFDF_ISFLOPPY) {
465 		lp->d_npartitions = MAXPARTITIONS;
466 		for (l = 0; l < lp->d_npartitions; l++) {
467 			if (l == RAW_PART)
468 				continue;
469 			/* struct copy */
470 			lp->d_partitions[l] =
471 			    lp->d_partitions[RAW_PART];
472 		}
473 		lp->d_checksum = dkcksum(lp);
474 	} else {
475 		errmes = readdisklabel(MAKEDISKDEV(major(dev),
476 		    unit, RAW_PART), ofdisk_strategy, lp,
477 		    of->sc_dk.dk_cpulabel);
478 		if (errmes != NULL)
479 			printf("%s: %s\n", of->sc_dev.dv_xname, errmes);
480 	}
481 }
482