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