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