xref: /netbsd-src/sys/dev/ofw/ofdisk.c (revision 37b34d511dea595d3ba03a661cf3b775038ea5f8)
1 /*	$NetBSD: ofdisk.c,v 1.23 2002/10/02 16:34:33 thorpej 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.23 2002/10/02 16:34:33 thorpej 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 #include <sys/conf.h>
49 
50 #include <dev/ofw/openfirm.h>
51 
52 struct ofdisk_softc {
53 	struct device sc_dev;
54 	int sc_phandle;
55 	int sc_unit;
56 	int sc_flags;
57 	struct disk sc_dk;
58 	int sc_ihandle;
59 	u_long max_transfer;
60 	char sc_name[16];
61 };
62 
63 /* sc_flags */
64 #define OFDF_ISFLOPPY	0x01		/* we are a floppy drive */
65 
66 static int ofdisk_match (struct device *, struct cfdata *, void *);
67 static void ofdisk_attach (struct device *, struct device *, void *);
68 
69 CFATTACH_DECL(ofdisk, sizeof(struct ofdisk_softc),
70     ofdisk_match, ofdisk_attach, NULL, NULL);
71 
72 extern struct cfdriver ofdisk_cd;
73 
74 dev_type_open(ofdisk_open);
75 dev_type_close(ofdisk_close);
76 dev_type_read(ofdisk_read);
77 dev_type_write(ofdisk_write);
78 dev_type_ioctl(ofdisk_ioctl);
79 dev_type_strategy(ofdisk_strategy);
80 dev_type_dump(ofdisk_dump);
81 dev_type_size(ofdisk_size);
82 
83 const struct bdevsw ofdisk_bdevsw = {
84 	ofdisk_open, ofdisk_close, ofdisk_strategy, ofdisk_ioctl,
85 	ofdisk_dump, ofdisk_size, D_DISK
86 };
87 
88 const struct cdevsw ofdisk_cdevsw = {
89 	ofdisk_open, ofdisk_close, ofdisk_read, ofdisk_write, ofdisk_ioctl,
90 	nostop, notty, nopoll, nommap, D_DISK
91 };
92 
93 struct dkdriver ofdisk_dkdriver = { ofdisk_strategy };
94 
95 void ofdisk_getdefaultlabel (struct ofdisk_softc *, struct disklabel *);
96 void ofdisk_getdisklabel (dev_t);
97 
98 static int
99 ofdisk_match(struct device *parent, struct cfdata *match, void *aux)
100 {
101 	struct ofbus_attach_args *oba = aux;
102 	char type[8];
103 	int l;
104 
105 	if (strcmp(oba->oba_busname, "ofw"))
106 		return (0);
107 	if ((l = OF_getprop(oba->oba_phandle, "device_type", type,
108 	    sizeof type - 1)) < 0)
109 		return 0;
110 	if (l >= sizeof type)
111 		return 0;
112 	type[l] = 0;
113 	return !strcmp(type, "block");
114 }
115 
116 static void
117 ofdisk_attach(struct device *parent, struct device *self, void *aux)
118 {
119 	struct ofdisk_softc *of = (void *)self;
120 	struct ofbus_attach_args *oba = aux;
121 	char child[64];
122 	int l;
123 
124 	if ((l = OF_getprop(oba->oba_phandle, "name", child,
125 	    sizeof child - 1)) < 0)
126 		panic("device without name?");
127 	if (l >= sizeof child)
128 		l = sizeof child - 1;
129 	child[l] = 0;
130 
131 	of->sc_flags = 0;
132 	of->sc_phandle = oba->oba_phandle;
133 	of->sc_unit = oba->oba_unit;
134 	of->sc_ihandle = 0;
135 	of->sc_dk.dk_driver = &ofdisk_dkdriver;
136 	of->sc_dk.dk_name = of->sc_name;
137 	strcpy(of->sc_name, of->sc_dev.dv_xname);
138 	disk_attach(&of->sc_dk);
139 	printf("\n");
140 
141 	if (strcmp(child, "floppy") == 0)
142 		of->sc_flags |= OFDF_ISFLOPPY;
143 }
144 
145 int
146 ofdisk_open(dev_t dev, int flags, int fmt, struct proc *p)
147 {
148 	int unit = DISKUNIT(dev);
149 	struct ofdisk_softc *of;
150 	char path[256];
151 	int l;
152 
153 	if (unit >= ofdisk_cd.cd_ndevs)
154 		return ENXIO;
155 	if (!(of = ofdisk_cd.cd_devs[unit]))
156 		return ENXIO;
157 
158 	if (!of->sc_ihandle) {
159 		if ((l = OF_package_to_path(of->sc_phandle, path,
160 		    sizeof path - 3)) < 0 ||
161 		    l >= sizeof path - 3)
162 			return ENXIO;
163 		path[l] = 0;
164 
165 		/*
166 		 * XXX This is for the benefit of SCSI/IDE disks that don't
167 		 * XXX have all their childs in the device tree.
168 		 * XXX YES, I DO THINK THIS IS A BUG IN OPENFIRMWARE!!!
169 		 * XXX And yes, this is a very gross hack!
170 		 * XXX See also ofscsi.c
171 		 */
172 		if (!strcmp(path + l - 4, "disk")) {
173 			path[l++] = '@';
174 			path[l++] = '0' + of->sc_unit;
175 			path[l] = 0;
176 		}
177 
178 		strcat(path, ":0");
179 
180 		if ((of->sc_ihandle = OF_open(path)) == -1)
181 			return ENXIO;
182 
183 		/*
184 		 * Try to get characteristics of the disk.
185 		 */
186 		of->max_transfer = OF_call_method_1("max-transfer",
187 		    of->sc_ihandle, 0);
188 		if (of->max_transfer > MAXPHYS)
189 			of->max_transfer = MAXPHYS;
190 
191 		ofdisk_getdisklabel(dev);
192 	}
193 
194 	switch (fmt) {
195 	case S_IFCHR:
196 		of->sc_dk.dk_copenmask |= 1 << DISKPART(dev);
197 		break;
198 	case S_IFBLK:
199 		of->sc_dk.dk_bopenmask |= 1 << DISKPART(dev);
200 		break;
201 	}
202 	of->sc_dk.dk_openmask =
203 	    of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
204 
205 	return 0;
206 }
207 
208 int
209 ofdisk_close(dev_t dev, int flags, int fmt, struct proc *p)
210 {
211 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
212 
213 	switch (fmt) {
214 	case S_IFCHR:
215 		of->sc_dk.dk_copenmask &= ~(1 << DISKPART(dev));
216 		break;
217 	case S_IFBLK:
218 		of->sc_dk.dk_bopenmask &= ~(1 << DISKPART(dev));
219 		break;
220 	}
221 	of->sc_dk.dk_openmask = of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
222 
223 #ifdef	FIRMWORKSBUGS
224 	/*
225 	 * This is a hack to get the firmware to flush its buffers.
226 	 */
227 	OF_seek(of->sc_ihandle, 0);
228 #endif
229 	if (!of->sc_dk.dk_openmask) {
230 		OF_close(of->sc_ihandle);
231 		of->sc_ihandle = 0;
232 	}
233 
234 	return 0;
235 }
236 
237 void
238 ofdisk_strategy(struct buf *bp)
239 {
240 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)];
241 	struct partition *p;
242 	u_quad_t off;
243 	int read;
244 	int (*OF_io)(int, void *, int);
245 	daddr_t blkno = bp->b_blkno;
246 
247 	bp->b_resid = 0;
248 	if (bp->b_bcount == 0)
249 		goto done;
250 
251 	OF_io = bp->b_flags & B_READ ? OF_read : OF_write;
252 
253 	if (DISKPART(bp->b_dev) != RAW_PART) {
254 		if (bounds_check_with_label(bp, of->sc_dk.dk_label, 0) <= 0) {
255 			bp->b_resid = bp->b_bcount;
256 			goto done;
257 		}
258 		p = &of->sc_dk.dk_label->d_partitions[DISKPART(bp->b_dev)];
259 		blkno = bp->b_blkno + p->p_offset;
260 	}
261 
262 	disk_busy(&of->sc_dk);
263 
264 	off = (u_quad_t)blkno * DEV_BSIZE;
265 	read = -1;
266 	do {
267 		if (OF_seek(of->sc_ihandle, off) < 0)
268 			break;
269 		read = OF_io(of->sc_ihandle, bp->b_data, bp->b_bcount);
270 	} while (read == -2);
271 
272 	if (read < 0) {
273 		bp->b_error = EIO;
274 		bp->b_flags |= B_ERROR;
275 		bp->b_resid = bp->b_bcount;
276 	} else
277 		bp->b_resid = bp->b_bcount - read;
278 
279 	disk_unbusy(&of->sc_dk, bp->b_bcount - bp->b_resid);
280 
281 done:
282 	biodone(bp);
283 }
284 
285 static void
286 ofminphys(struct buf *bp)
287 {
288 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)];
289 
290 	if (bp->b_bcount > of->max_transfer)
291 		bp->b_bcount = of->max_transfer;
292 }
293 
294 int
295 ofdisk_read(dev_t dev, struct uio *uio, int flags)
296 {
297 	return physio(ofdisk_strategy, NULL, dev, B_READ, ofminphys, uio);
298 }
299 
300 int
301 ofdisk_write(dev_t dev, struct uio *uio, int flags)
302 {
303 	return physio(ofdisk_strategy, NULL, dev, B_WRITE, ofminphys, uio);
304 }
305 
306 int
307 ofdisk_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
308 {
309 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
310 	int error;
311 #ifdef __HAVE_OLD_DISKLABEL
312 	struct disklabel newlabel;
313 #endif
314 
315 	switch (cmd) {
316 	case DIOCGDINFO:
317 		*(struct disklabel *)data = *of->sc_dk.dk_label;
318 		return 0;
319 #ifdef __HAVE_OLD_DISKLABEL
320 	case ODIOCGDINFO:
321 		newlabel = *of->sc_dk.dk_label;
322 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
323 			return ENOTTY;
324 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
325 		return 0;
326 #endif
327 
328 	case DIOCGPART:
329 		((struct partinfo *)data)->disklab = of->sc_dk.dk_label;
330 		((struct partinfo *)data)->part =
331 			&of->sc_dk.dk_label->d_partitions[DISKPART(dev)];
332 		return 0;
333 
334 	case DIOCWDINFO:
335 	case DIOCSDINFO:
336 #ifdef __HAVE_OLD_DISKLABEL
337 	case ODIOCWDINFO:
338 	case ODIOCSDINFO:
339 #endif
340 	{
341 		struct disklabel *lp;
342 
343 #ifdef __HAVE_OLD_DISKLABEL
344 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
345 			memset(&newlabel, 0, sizeof newlabel);
346 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
347 			lp = &newlabel;
348 		} else
349 #endif
350 		lp = (struct disklabel *)data;
351 
352 		if ((flag & FWRITE) == 0)
353 			return EBADF;
354 
355 		error = setdisklabel(of->sc_dk.dk_label,
356 		    lp, /*of->sc_dk.dk_openmask */0,
357 		    of->sc_dk.dk_cpulabel);
358 		if (error == 0 && cmd == DIOCWDINFO
359 #ifdef __HAVE_OLD_DISKLABEL
360 		    || xfer == ODIOCWDINFO
361 #endif
362 		    )
363 			error = writedisklabel(MAKEDISKDEV(major(dev),
364 			    DISKUNIT(dev), RAW_PART), ofdisk_strategy,
365 			    of->sc_dk.dk_label, of->sc_dk.dk_cpulabel);
366 
367 		return error;
368 	}
369 
370 	case DIOCGDEFLABEL:
371 		ofdisk_getdefaultlabel(of, (struct disklabel *)data);
372 		return 0;
373 #ifdef __HAVE_OLD_DISKLABEL
374 	case DIOCGDEFLABEL:
375 		ofdisk_getdefaultlabel(of, &newlabel);
376 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
377 			return ENOTTY;
378 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
379 		return 0;
380 #endif
381 
382 	default:
383 		return ENOTTY;
384 	}
385 }
386 
387 int
388 ofdisk_dump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
389 {
390 	return EINVAL;
391 }
392 
393 int
394 ofdisk_size(dev_t dev)
395 {
396 	struct ofdisk_softc *of;
397 	struct disklabel *lp;
398 	int size, part, omask, unit;
399 
400 	unit = DISKUNIT(dev);
401 	if (unit >= ofdisk_cd.cd_ndevs ||
402 	    (of = ofdisk_cd.cd_devs[unit]) == NULL)
403 		return -1;
404 
405 	part = DISKPART(dev);
406 	omask = of->sc_dk.dk_openmask & (1 << part);
407 	lp = of->sc_dk.dk_label;
408 
409 	if (omask == 0 && ofdisk_open(dev, 0, S_IFBLK, curproc) != 0)
410 		return -1;
411 
412 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
413 		size = -1;
414 	else
415 		size = lp->d_partitions[part].p_size *
416 		    (lp->d_secsize / DEV_BSIZE);
417 
418 	if (omask == 0 && ofdisk_close(dev, 0, S_IFBLK, curproc) != 0)
419 		return -1;
420 
421 	return size;
422 }
423 
424 void
425 ofdisk_getdefaultlabel(struct ofdisk_softc *of, struct disklabel *lp)
426 {
427 
428 	memset(lp, 0, sizeof *lp);
429 
430 	/*
431 	 * XXX Firmware bug?  Asking for block size gives a
432 	 * XXX ridiculous number!  So we use what the boot program
433 	 * XXX uses.
434 	 */
435 	lp->d_secsize = DEV_BSIZE;
436 
437 	lp->d_secperunit = OF_call_method_1("#blocks",
438 	    of->sc_ihandle, 0);
439 	if (lp->d_secperunit == (u_int32_t)-1)
440 		lp->d_secperunit = 0x7fffffff;
441 
442 	lp->d_secpercyl = 1;
443 	lp->d_nsectors = 1;
444 	lp->d_ntracks = 1;
445 	lp->d_ncylinders = lp->d_secperunit;
446 
447 	lp->d_partitions[RAW_PART].p_offset = 0;
448 	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
449 	lp->d_npartitions = RAW_PART + 1;
450 
451 	lp->d_magic = DISKMAGIC;
452 	lp->d_magic2 = DISKMAGIC;
453 	lp->d_checksum = dkcksum(lp);
454 }
455 
456 void
457 ofdisk_getdisklabel(dev)
458 	dev_t dev;
459 {
460 	int unit = DISKUNIT(dev);
461 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[unit];
462 	struct disklabel *lp = of->sc_dk.dk_label;
463 	char *errmes;
464 	int l;
465 
466 	ofdisk_getdefaultlabel(of, lp);
467 
468 	/*
469 	 * Don't read the disklabel on a floppy; simply
470 	 * assign all partitions the same size/offset as
471 	 * RAW_PART.  (This is essentially what the ISA
472 	 * floppy driver does, but we don't deal with
473 	 * density stuff.)
474 	 */
475 	if (of->sc_flags & OFDF_ISFLOPPY) {
476 		lp->d_npartitions = MAXPARTITIONS;
477 		for (l = 0; l < lp->d_npartitions; l++) {
478 			if (l == RAW_PART)
479 				continue;
480 			/* struct copy */
481 			lp->d_partitions[l] =
482 			    lp->d_partitions[RAW_PART];
483 		}
484 		lp->d_checksum = dkcksum(lp);
485 	} else {
486 		errmes = readdisklabel(MAKEDISKDEV(major(dev),
487 		    unit, RAW_PART), ofdisk_strategy, lp,
488 		    of->sc_dk.dk_cpulabel);
489 		if (errmes != NULL)
490 			printf("%s: %s\n", of->sc_dev.dv_xname, errmes);
491 	}
492 }
493