xref: /netbsd-src/sys/dev/ofw/ofdisk.c (revision dd255ccea4286b0c44fa8fd48a9a19a768afe8e1)
1 /*	$NetBSD: ofdisk.c,v 1.44 2011/07/26 08:59:38 mrg 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.44 2011/07/26 08:59:38 mrg 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 	device_t 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 };
60 
61 /* sc_flags */
62 #define OFDF_ISFLOPPY	0x01		/* we are a floppy drive */
63 
64 #define	OFDISK_FLOPPY_P(of)		((of)->sc_flags & OFDF_ISFLOPPY)
65 
66 static int ofdisk_match (device_t, cfdata_t, void *);
67 static void ofdisk_attach (device_t, device_t, void *);
68 
69 CFATTACH_DECL_NEW(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, nokqfilter, D_DISK
91 };
92 
93 static void ofminphys(struct buf *);
94 
95 struct dkdriver ofdisk_dkdriver = { ofdisk_strategy, ofminphys };
96 
97 void ofdisk_getdefaultlabel (struct ofdisk_softc *, struct disklabel *);
98 void ofdisk_getdisklabel (dev_t);
99 
100 static int
101 ofdisk_match(device_t parent, cfdata_t match, void *aux)
102 {
103 	struct ofbus_attach_args *oba = aux;
104 	char type[8];
105 	int l;
106 
107 	if (strcmp(oba->oba_busname, "ofw"))
108 		return (0);
109 	if ((l = OF_getprop(oba->oba_phandle, "device_type", type,
110 	    sizeof type - 1)) < 0)
111 		return 0;
112 	if (l >= sizeof type)
113 		return 0;
114 	type[l] = 0;
115 	return !strcmp(type, "block");
116 }
117 
118 static void
119 ofdisk_attach(device_t parent, device_t self, void *aux)
120 {
121 	struct ofdisk_softc *of = device_private(self);
122 	struct ofbus_attach_args *oba = aux;
123 	char child[64];
124 	int l;
125 
126 	of->sc_dev = self;
127 	if ((l = OF_getprop(oba->oba_phandle, "name", child,
128 	    sizeof child - 1)) < 0)
129 		panic("device without name?");
130 	if (l >= sizeof child)
131 		l = sizeof child - 1;
132 	child[l] = 0;
133 
134 	of->sc_flags = 0;
135 	of->sc_phandle = oba->oba_phandle;
136 	of->sc_unit = oba->oba_unit;
137 	of->sc_ihandle = 0;
138 	disk_init(&of->sc_dk, device_xname(of->sc_dev), &ofdisk_dkdriver);
139 	disk_attach(&of->sc_dk);
140 	printf("\n");
141 
142 	if (strcmp(child, "floppy") == 0)
143 		of->sc_flags |= OFDF_ISFLOPPY;
144 	else {
145 		/* Discover wedges on this disk. */
146 		dkwedge_discover(&of->sc_dk);
147 	}
148 }
149 
150 int
151 ofdisk_open(dev_t dev, int flags, int fmt, struct lwp *lwp)
152 {
153 	struct ofdisk_softc *of;
154 	char path[256];
155 	int error, l, part;
156 
157 	of = device_lookup_private(&ofdisk_cd, DISKUNIT(dev));
158 	if (of == NULL)
159 		return ENXIO;
160 
161 	part = DISKPART(dev);
162 
163 	mutex_enter(&of->sc_dk.dk_openlock);
164 
165 	/*
166 	 * If there are wedges, and this is not RAW_PART, then we
167 	 * need to fail.
168 	 */
169 	if (of->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
170 		error = EBUSY;
171 		goto bad1;
172 	}
173 
174 	if (!of->sc_ihandle) {
175 		if ((l = OF_package_to_path(of->sc_phandle, path,
176 		    sizeof path - 3)) < 0 ||
177 		    l >= sizeof path - 3) {
178 			error = ENXIO;
179 			goto bad1;
180 		}
181 		path[l] = 0;
182 
183 		/*
184 		 * XXX This is for the benefit of SCSI/IDE disks that don't
185 		 * XXX have all their childs in the device tree.
186 		 * XXX YES, I DO THINK THIS IS A BUG IN OPENFIRMWARE!!!
187 		 * XXX And yes, this is a very gross hack!
188 		 * XXX See also ofscsi.c
189 		 */
190 		if (!strcmp(path + l - 4, "disk")) {
191 			path[l++] = '@';
192 			path[l++] = '0' + of->sc_unit;
193 			path[l] = 0;
194 		}
195 
196 		strlcat(path, ":0", sizeof(path));
197 
198 		if ((of->sc_ihandle = OF_open(path)) == -1) {
199 			error = ENXIO;
200 			goto bad1;
201 		}
202 
203 		/*
204 		 * Try to get characteristics of the disk.
205 		 */
206 		of->max_transfer = OF_call_method_1("max-transfer",
207 		    of->sc_ihandle, 0);
208 		if (of->max_transfer > MAXPHYS)
209 			of->max_transfer = MAXPHYS;
210 
211 		ofdisk_getdisklabel(dev);
212 	}
213 
214 	switch (fmt) {
215 	case S_IFCHR:
216 		of->sc_dk.dk_copenmask |= 1 << part;
217 		break;
218 	case S_IFBLK:
219 		of->sc_dk.dk_bopenmask |= 1 << part;
220 		break;
221 	}
222 	of->sc_dk.dk_openmask =
223 	    of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
224 
225 
226 	error = 0;
227  bad1:
228 	mutex_exit(&of->sc_dk.dk_openlock);
229 	return (error);
230 }
231 
232 int
233 ofdisk_close(dev_t dev, int flags, int fmt, struct lwp *l)
234 {
235 	struct ofdisk_softc *of =
236 		device_lookup_private(&ofdisk_cd, DISKUNIT(dev));
237 
238 	mutex_enter(&of->sc_dk.dk_openlock);
239 
240 	switch (fmt) {
241 	case S_IFCHR:
242 		of->sc_dk.dk_copenmask &= ~(1 << DISKPART(dev));
243 		break;
244 	case S_IFBLK:
245 		of->sc_dk.dk_bopenmask &= ~(1 << DISKPART(dev));
246 		break;
247 	}
248 	of->sc_dk.dk_openmask = of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
249 
250 #ifdef	FIRMWORKSBUGS
251 	/*
252 	 * This is a hack to get the firmware to flush its buffers.
253 	 */
254 	OF_seek(of->sc_ihandle, 0);
255 #endif
256 	if (!of->sc_dk.dk_openmask) {
257 		OF_close(of->sc_ihandle);
258 		of->sc_ihandle = 0;
259 	}
260 
261 	mutex_exit(&of->sc_dk.dk_openlock);
262 	return 0;
263 }
264 
265 void
266 ofdisk_strategy(struct buf *bp)
267 {
268 	struct ofdisk_softc *of =
269 		device_lookup_private(&ofdisk_cd, DISKUNIT(bp->b_dev));
270 	struct partition *p;
271 	u_quad_t off;
272 	int read;
273 	int (*OF_io)(int, void *, int);
274 	daddr_t blkno = bp->b_blkno;
275 
276 	bp->b_resid = 0;
277 	if (bp->b_bcount == 0)
278 		goto done;
279 
280 	OF_io = bp->b_flags & B_READ ? OF_read :
281 		(int(*)(int, void*, int))OF_write;
282 
283 	if (DISKPART(bp->b_dev) != RAW_PART) {
284 		if (bounds_check_with_label(&of->sc_dk, bp, 0) <= 0) {
285 			bp->b_resid = bp->b_bcount;
286 			goto done;
287 		}
288 		p = &of->sc_dk.dk_label->d_partitions[DISKPART(bp->b_dev)];
289 		blkno = bp->b_blkno + p->p_offset;
290 	}
291 
292 	disk_busy(&of->sc_dk);
293 
294 	off = (u_quad_t)blkno * DEV_BSIZE;
295 	read = -1;
296 	do {
297 		if (OF_seek(of->sc_ihandle, off) < 0)
298 			break;
299 		read = OF_io(of->sc_ihandle, bp->b_data, bp->b_bcount);
300 	} while (read == -2);
301 
302 	if (read < 0) {
303 		bp->b_error = EIO;
304 		bp->b_resid = bp->b_bcount;
305 	} else
306 		bp->b_resid = bp->b_bcount - read;
307 
308 	disk_unbusy(&of->sc_dk, bp->b_bcount - bp->b_resid,
309 	    (bp->b_flags & B_READ));
310 
311 done:
312 	biodone(bp);
313 }
314 
315 static void
316 ofminphys(struct buf *bp)
317 {
318 	struct ofdisk_softc *of =
319 		device_lookup_private(&ofdisk_cd, DISKUNIT(bp->b_dev));
320 
321 	if (bp->b_bcount > of->max_transfer)
322 		bp->b_bcount = of->max_transfer;
323 }
324 
325 int
326 ofdisk_read(dev_t dev, struct uio *uio, int flags)
327 {
328 	return physio(ofdisk_strategy, NULL, dev, B_READ, ofminphys, uio);
329 }
330 
331 int
332 ofdisk_write(dev_t dev, struct uio *uio, int flags)
333 {
334 	return physio(ofdisk_strategy, NULL, dev, B_WRITE, ofminphys, uio);
335 }
336 
337 int
338 ofdisk_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
339 {
340 	struct ofdisk_softc *of =
341 		device_lookup_private(&ofdisk_cd, DISKUNIT(dev));
342 	int error;
343 #ifdef __HAVE_OLD_DISKLABEL
344 	struct disklabel newlabel;
345 #endif
346 
347 	switch (cmd) {
348 	case DIOCGDINFO:
349 		*(struct disklabel *)data = *of->sc_dk.dk_label;
350 		return 0;
351 #ifdef __HAVE_OLD_DISKLABEL
352 	case ODIOCGDINFO:
353 		newlabel = *of->sc_dk.dk_label;
354 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
355 			return ENOTTY;
356 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
357 		return 0;
358 #endif
359 
360 	case DIOCGPART:
361 		((struct partinfo *)data)->disklab = of->sc_dk.dk_label;
362 		((struct partinfo *)data)->part =
363 			&of->sc_dk.dk_label->d_partitions[DISKPART(dev)];
364 		return 0;
365 
366 	case DIOCWDINFO:
367 	case DIOCSDINFO:
368 #ifdef __HAVE_OLD_DISKLABEL
369 	case ODIOCWDINFO:
370 	case ODIOCSDINFO:
371 #endif
372 	{
373 		struct disklabel *lp;
374 
375 #ifdef __HAVE_OLD_DISKLABEL
376 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
377 			memset(&newlabel, 0, sizeof newlabel);
378 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
379 			lp = &newlabel;
380 		} else
381 #endif
382 		lp = (struct disklabel *)data;
383 
384 		if ((flag & FWRITE) == 0)
385 			return EBADF;
386 
387 		mutex_enter(&of->sc_dk.dk_openlock);
388 
389 		error = setdisklabel(of->sc_dk.dk_label,
390 		    lp, /*of->sc_dk.dk_openmask */0,
391 		    of->sc_dk.dk_cpulabel);
392 		if (error == 0 && cmd == DIOCWDINFO
393 #ifdef __HAVE_OLD_DISKLABEL
394 		    || xfer == ODIOCWDINFO
395 #endif
396 		    )
397 			error = writedisklabel(MAKEDISKDEV(major(dev),
398 			    DISKUNIT(dev), RAW_PART), ofdisk_strategy,
399 			    of->sc_dk.dk_label, of->sc_dk.dk_cpulabel);
400 
401 		mutex_exit(&of->sc_dk.dk_openlock);
402 
403 		return error;
404 	}
405 
406 	case DIOCGDEFLABEL:
407 		ofdisk_getdefaultlabel(of, (struct disklabel *)data);
408 		return 0;
409 #ifdef __HAVE_OLD_DISKLABEL
410 	case DIOCGDEFLABEL:
411 		ofdisk_getdefaultlabel(of, &newlabel);
412 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
413 			return ENOTTY;
414 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
415 		return 0;
416 #endif
417 
418 	case DIOCAWEDGE:
419 	    {
420 	    	struct dkwedge_info *dkw = (void *) data;
421 
422 		if (OFDISK_FLOPPY_P(of))
423 			return (ENOTTY);
424 
425 		if ((flag & FWRITE) == 0)
426 			return (EBADF);
427 
428 		/* If the ioctl happens here, the parent is us. */
429 		strlcpy(dkw->dkw_parent, device_xname(of->sc_dev),
430 			sizeof(dkw->dkw_parent));
431 		return (dkwedge_add(dkw));
432 	    }
433 
434 	case DIOCDWEDGE:
435 	    {
436 	    	struct dkwedge_info *dkw = (void *) data;
437 
438 		if (OFDISK_FLOPPY_P(of))
439 			return (ENOTTY);
440 
441 		if ((flag & FWRITE) == 0)
442 			return (EBADF);
443 
444 		/* If the ioctl happens here, the parent is us. */
445 		strlcpy(dkw->dkw_parent, device_xname(of->sc_dev),
446 			sizeof(dkw->dkw_parent));
447 		return (dkwedge_del(dkw));
448 	    }
449 
450 	case DIOCLWEDGES:
451 	    {
452 	    	struct dkwedge_list *dkwl = (void *) data;
453 
454 		if (OFDISK_FLOPPY_P(of))
455 			return (ENOTTY);
456 
457 		return (dkwedge_list(&of->sc_dk, dkwl, l));
458 	    }
459 
460 	default:
461 		return ENOTTY;
462 	}
463 }
464 
465 int
466 ofdisk_dump(dev_t dev, daddr_t blkno, void *va, size_t size)
467 {
468 	return EINVAL;
469 }
470 
471 int
472 ofdisk_size(dev_t dev)
473 {
474 	struct ofdisk_softc *of;
475 	struct disklabel *lp;
476 	int size, part, omask;
477 
478 	of = device_lookup_private(&ofdisk_cd, DISKUNIT(dev));
479 	if (of == NULL)
480 		return ENXIO;
481 
482 	part = DISKPART(dev);
483 	omask = of->sc_dk.dk_openmask & (1 << part);
484 	lp = of->sc_dk.dk_label;
485 
486 	if (omask == 0 && ofdisk_open(dev, 0, S_IFBLK, curlwp) != 0)
487 		return -1;
488 
489 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
490 		size = -1;
491 	else
492 		size = lp->d_partitions[part].p_size *
493 		    (lp->d_secsize / DEV_BSIZE);
494 
495 	if (omask == 0 && ofdisk_close(dev, 0, S_IFBLK, curlwp) != 0)
496 		return -1;
497 
498 	return size;
499 }
500 
501 void
502 ofdisk_getdefaultlabel(struct ofdisk_softc *of, struct disklabel *lp)
503 {
504 
505 	memset(lp, 0, sizeof *lp);
506 
507 	/*
508 	 * XXX Firmware bug?  Asking for block size gives a
509 	 * XXX ridiculous number!  So we use what the boot program
510 	 * XXX uses.
511 	 */
512 	lp->d_secsize = DEV_BSIZE;
513 
514 	lp->d_secperunit = OF_call_method_1("#blocks",
515 	    of->sc_ihandle, 0);
516 	if (lp->d_secperunit == (u_int32_t)-1)
517 		lp->d_secperunit = 0x7fffffff;
518 
519 	lp->d_secpercyl = 1;
520 	lp->d_nsectors = 1;
521 	lp->d_ntracks = 1;
522 	lp->d_ncylinders = lp->d_secperunit;
523 
524 	lp->d_partitions[RAW_PART].p_offset = 0;
525 	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
526 	lp->d_npartitions = RAW_PART + 1;
527 
528 	lp->d_magic = DISKMAGIC;
529 	lp->d_magic2 = DISKMAGIC;
530 	lp->d_checksum = dkcksum(lp);
531 }
532 
533 void
534 ofdisk_getdisklabel(dev_t dev)
535 {
536 	int unit = DISKUNIT(dev);
537 	struct ofdisk_softc *of =
538 		device_lookup_private(&ofdisk_cd, unit);
539 	struct disklabel *lp = of->sc_dk.dk_label;
540 	const char *errmes;
541 	int l;
542 
543 	ofdisk_getdefaultlabel(of, lp);
544 
545 	/*
546 	 * Don't read the disklabel on a floppy; simply
547 	 * assign all partitions the same size/offset as
548 	 * RAW_PART.  (This is essentially what the ISA
549 	 * floppy driver does, but we don't deal with
550 	 * density stuff.)
551 	 */
552 	if (OFDISK_FLOPPY_P(of)) {
553 		lp->d_npartitions = MAXPARTITIONS;
554 		for (l = 0; l < lp->d_npartitions; l++) {
555 			if (l == RAW_PART)
556 				continue;
557 			/* struct copy */
558 			lp->d_partitions[l] =
559 			    lp->d_partitions[RAW_PART];
560 		}
561 		lp->d_checksum = dkcksum(lp);
562 	} else {
563 		errmes = readdisklabel(MAKEDISKDEV(major(dev),
564 		    unit, RAW_PART), ofdisk_strategy, lp,
565 		    of->sc_dk.dk_cpulabel);
566 		if (errmes != NULL)
567 			printf("%s: %s\n", device_xname(of->sc_dev), errmes);
568 	}
569 }
570