NetBSD/sys/dev/ofw/ofdisk.c

482 lines
12 KiB
C
Raw Normal View History

2001-11-13 10:24:43 +03:00
/* $NetBSD: ofdisk.c,v 1.18 2001/11/13 07:26:28 lukem Exp $ */
1996-09-30 20:34:14 +04:00
/*
* Copyright (C) 1995, 1996 Wolfgang Solfrank.
* Copyright (C) 1995, 1996 TooLs GmbH.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by TooLs GmbH.
* 4. The name of TooLs GmbH may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2001-11-13 10:24:43 +03:00
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ofdisk.c,v 1.18 2001/11/13 07:26:28 lukem Exp $");
1996-09-30 20:34:14 +04:00
#include <sys/param.h>
#include <sys/buf.h>
#include <sys/device.h>
#include <sys/conf.h>
1996-09-30 20:34:14 +04:00
#include <sys/disklabel.h>
#include <sys/disk.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/systm.h>
1997-07-23 22:42:40 +04:00
#include <sys/proc.h>
1996-09-30 20:34:14 +04:00
#include <dev/ofw/openfirm.h>
struct ofdisk_softc {
1996-09-30 20:34:14 +04:00
struct device sc_dev;
int sc_phandle;
int sc_unit;
int sc_flags;
1996-09-30 20:34:14 +04:00
struct disk sc_dk;
int sc_ihandle;
u_long max_transfer;
char sc_name[16];
};
bdev_decl(ofdisk_);
cdev_decl(ofdisk_);
/* sc_flags */
#define OFDF_ISFLOPPY 0x01 /* we are a floppy drive */
static int ofdisk_match (struct device *, struct cfdata *, void *);
static void ofdisk_attach (struct device *, struct device *, void *);
1996-09-30 20:34:14 +04:00
struct cfattach ofdisk_ca = {
sizeof(struct ofdisk_softc), ofdisk_match, ofdisk_attach
1996-09-30 20:34:14 +04:00
};
1998-01-12 12:33:30 +03:00
extern struct cfdriver ofdisk_cd;
1996-09-30 20:34:14 +04:00
void ofdisk_strategy (struct buf *);
1996-09-30 20:34:14 +04:00
struct dkdriver ofdisk_dkdriver = { ofdisk_strategy };
1996-09-30 20:34:14 +04:00
void ofdisk_getdefaultlabel (struct ofdisk_softc *, struct disklabel *);
void ofdisk_getdisklabel (dev_t);
1997-10-09 03:23:13 +04:00
1996-09-30 20:34:14 +04:00
static int
ofdisk_match(struct device *parent, struct cfdata *match, void *aux)
1996-09-30 20:34:14 +04:00
{
struct ofbus_attach_args *oba = aux;
1996-09-30 20:34:14 +04:00
char type[8];
int l;
if (strcmp(oba->oba_busname, "ofw"))
return (0);
if ((l = OF_getprop(oba->oba_phandle, "device_type", type,
sizeof type - 1)) < 0)
1996-09-30 20:34:14 +04:00
return 0;
if (l >= sizeof type)
return 0;
type[l] = 0;
return !strcmp(type, "block");
}
static void
ofdisk_attach(struct device *parent, struct device *self, void *aux)
1996-09-30 20:34:14 +04:00
{
struct ofdisk_softc *of = (void *)self;
struct ofbus_attach_args *oba = aux;
char child[64];
1996-09-30 20:34:14 +04:00
int l;
if ((l = OF_getprop(oba->oba_phandle, "name", child,
sizeof child - 1)) < 0)
panic("device without name?");
if (l >= sizeof child)
l = sizeof child - 1;
child[l] = 0;
of->sc_flags = 0;
of->sc_phandle = oba->oba_phandle;
of->sc_unit = oba->oba_unit;
1996-09-30 20:34:14 +04:00
of->sc_ihandle = 0;
of->sc_dk.dk_driver = &ofdisk_dkdriver;
1996-09-30 20:34:14 +04:00
of->sc_dk.dk_name = of->sc_name;
strcpy(of->sc_name, of->sc_dev.dv_xname);
disk_attach(&of->sc_dk);
#ifdef __BROKEN_DK_ESTABLISH
1996-09-30 20:34:14 +04:00
dk_establish(&of->sc_dk, self); /* XXX */
#endif
1996-10-13 05:37:04 +04:00
printf("\n");
if (strcmp(child, "floppy") == 0)
of->sc_flags |= OFDF_ISFLOPPY;
1996-09-30 20:34:14 +04:00
}
int
ofdisk_open(dev_t dev, int flags, int fmt, struct proc *p)
1996-09-30 20:34:14 +04:00
{
int unit = DISKUNIT(dev);
struct ofdisk_softc *of;
1996-09-30 20:34:14 +04:00
char path[256];
int l;
if (unit >= ofdisk_cd.cd_ndevs)
return ENXIO;
if (!(of = ofdisk_cd.cd_devs[unit]))
return ENXIO;
if (!of->sc_ihandle) {
if ((l = OF_package_to_path(of->sc_phandle, path,
1998-02-24 10:10:39 +03:00
sizeof path - 3)) < 0 ||
l >= sizeof path - 3)
1996-09-30 20:34:14 +04:00
return ENXIO;
path[l] = 0;
/*
* XXX This is for the benefit of SCSI/IDE disks that don't
* XXX have all their childs in the device tree.
* XXX YES, I DO THINK THIS IS A BUG IN OPENFIRMWARE!!!
* XXX And yes, this is a very gross hack!
* XXX See also ofscsi.c
1996-09-30 20:34:14 +04:00
*/
if (!strcmp(path + l - 4, "disk")) {
path[l++] = '@';
path[l++] = '0' + of->sc_unit;
path[l] = 0;
}
strcat(path, ":0");
if (!(of->sc_ihandle = OF_open(path)))
return ENXIO;
/*
* Try to get characteristics of the disk.
*/
of->max_transfer = OF_call_method_1("max-transfer",
of->sc_ihandle, 0);
1996-09-30 20:34:14 +04:00
if (of->max_transfer > MAXPHYS)
of->max_transfer = MAXPHYS;
ofdisk_getdisklabel(dev);
1996-09-30 20:34:14 +04:00
}
switch (fmt) {
case S_IFCHR:
of->sc_dk.dk_copenmask |= 1 << DISKPART(dev);
break;
case S_IFBLK:
of->sc_dk.dk_bopenmask |= 1 << DISKPART(dev);
break;
}
of->sc_dk.dk_openmask =
of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
1996-09-30 20:34:14 +04:00
return 0;
}
int
ofdisk_close(dev_t dev, int flags, int fmt, struct proc *p)
1996-09-30 20:34:14 +04:00
{
struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
1996-09-30 20:34:14 +04:00
switch (fmt) {
case S_IFCHR:
of->sc_dk.dk_copenmask &= ~(1 << DISKPART(dev));
break;
case S_IFBLK:
of->sc_dk.dk_bopenmask &= ~(1 << DISKPART(dev));
break;
}
of->sc_dk.dk_openmask = of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
#ifdef FIRMWORKSBUGS
1996-09-30 20:34:14 +04:00
/*
* This is a hack to get the firmware to flush its buffers.
*/
OF_seek(of->sc_ihandle, 0);
#endif
if (!of->sc_dk.dk_openmask) {
OF_close(of->sc_ihandle);
of->sc_ihandle = 0;
}
return 0;
}
void
ofdisk_strategy(struct buf *bp)
1996-09-30 20:34:14 +04:00
{
struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)];
1996-09-30 20:34:14 +04:00
struct partition *p;
u_quad_t off;
int read;
int (*OF_io)(int, void *, int);
daddr_t blkno = bp->b_blkno;
1996-09-30 20:34:14 +04:00
bp->b_resid = 0;
if (bp->b_bcount == 0)
goto done;
OF_io = bp->b_flags & B_READ ? OF_read : OF_write;
if (DISKPART(bp->b_dev) != RAW_PART) {
if (bounds_check_with_label(bp, of->sc_dk.dk_label, 0) <= 0) {
bp->b_resid = bp->b_bcount;
goto done;
}
p = &of->sc_dk.dk_label->d_partitions[DISKPART(bp->b_dev)];
blkno = bp->b_blkno + p->p_offset;
}
1996-09-30 20:34:14 +04:00
disk_busy(&of->sc_dk);
off = (u_quad_t)blkno * DEV_BSIZE;
read = -1;
do {
if (OF_seek(of->sc_ihandle, off) < 0)
break;
read = OF_io(of->sc_ihandle, bp->b_data, bp->b_bcount);
} while (read == -2);
1996-09-30 20:34:14 +04:00
if (read < 0) {
bp->b_error = EIO;
bp->b_flags |= B_ERROR;
bp->b_resid = bp->b_bcount;
} else
bp->b_resid = bp->b_bcount - read;
disk_unbusy(&of->sc_dk, bp->b_bcount - bp->b_resid);
done:
biodone(bp);
}
static void
ofminphys(struct buf *bp)
1996-09-30 20:34:14 +04:00
{
struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)];
1996-09-30 20:34:14 +04:00
if (bp->b_bcount > of->max_transfer)
bp->b_bcount = of->max_transfer;
}
int
ofdisk_read(dev_t dev, struct uio *uio, int flags)
1996-09-30 20:34:14 +04:00
{
return physio(ofdisk_strategy, NULL, dev, B_READ, ofminphys, uio);
1996-09-30 20:34:14 +04:00
}
int
ofdisk_write(dev_t dev, struct uio *uio, int flags)
1996-09-30 20:34:14 +04:00
{
return physio(ofdisk_strategy, NULL, dev, B_WRITE, ofminphys, uio);
1996-09-30 20:34:14 +04:00
}
int
ofdisk_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
1996-09-30 20:34:14 +04:00
{
struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
1996-09-30 20:34:14 +04:00
int error;
#ifdef __HAVE_OLD_DISKLABEL
struct disklabel newlabel;
#endif
1996-09-30 20:34:14 +04:00
switch (cmd) {
case DIOCGDINFO:
*(struct disklabel *)data = *of->sc_dk.dk_label;
return 0;
#ifdef __HAVE_OLD_DISKLABEL
case ODIOCGDINFO:
newlabel = *of->sc_dk.dk_label;
if (newlabel.d_npartitions > OLDMAXPARTITIONS)
return ENOTTY;
memcpy(data, &newlabel, sizeof (struct olddisklabel));
return 0;
#endif
1996-09-30 20:34:14 +04:00
case DIOCGPART:
((struct partinfo *)data)->disklab = of->sc_dk.dk_label;
((struct partinfo *)data)->part =
&of->sc_dk.dk_label->d_partitions[DISKPART(dev)];
return 0;
case DIOCWDINFO:
case DIOCSDINFO:
#ifdef __HAVE_OLD_DISKLABEL
case ODIOCWDINFO:
case ODIOCSDINFO:
#endif
{
struct disklabel *lp;
#ifdef __HAVE_OLD_DISKLABEL
if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
memset(&newlabel, 0, sizeof newlabel);
memcpy(&newlabel, data, sizeof (struct olddisklabel));
lp = &newlabel;
} else
#endif
lp = (struct disklabel *)data;
1996-09-30 20:34:14 +04:00
if ((flag & FWRITE) == 0)
return EBADF;
error = setdisklabel(of->sc_dk.dk_label,
lp, /*of->sc_dk.dk_openmask */0,
of->sc_dk.dk_cpulabel);
if (error == 0 && cmd == DIOCWDINFO
#ifdef __HAVE_OLD_DISKLABEL
|| xfer == ODIOCWDINFO
#endif
)
1996-09-30 20:34:14 +04:00
error = writedisklabel(MAKEDISKDEV(major(dev),
DISKUNIT(dev), RAW_PART), ofdisk_strategy,
of->sc_dk.dk_label, of->sc_dk.dk_cpulabel);
1996-09-30 20:34:14 +04:00
return error;
}
1997-10-09 03:23:13 +04:00
case DIOCGDEFLABEL:
ofdisk_getdefaultlabel(of, (struct disklabel *)data);
1997-10-09 03:23:13 +04:00
return 0;
#ifdef __HAVE_OLD_DISKLABEL
case DIOCGDEFLABEL:
ofdisk_getdefaultlabel(of, &newlabel);
if (newlabel.d_npartitions > OLDMAXPARTITIONS)
return ENOTTY;
memcpy(data, &newlabel, sizeof (struct olddisklabel));
return 0;
#endif
1997-10-09 03:23:13 +04:00
1996-09-30 20:34:14 +04:00
default:
return ENOTTY;
}
}
int
ofdisk_dump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
1996-09-30 20:34:14 +04:00
{
return EINVAL;
}
int
ofdisk_size(dev_t dev)
1996-09-30 20:34:14 +04:00
{
struct ofdisk_softc *of;
struct disklabel *lp;
int size, part, omask, unit;
unit = DISKUNIT(dev);
if (unit >= ofdisk_cd.cd_ndevs ||
(of = ofdisk_cd.cd_devs[unit]) == NULL)
1996-09-30 20:34:14 +04:00
return -1;
1996-09-30 20:34:14 +04:00
part = DISKPART(dev);
omask = of->sc_dk.dk_openmask & (1 << part);
1997-07-23 22:42:40 +04:00
lp = of->sc_dk.dk_label;
if (omask == 0 && ofdisk_open(dev, 0, S_IFBLK, curproc) != 0)
return -1;
if (lp->d_partitions[part].p_fstype != FS_SWAP)
1996-09-30 20:34:14 +04:00
size = -1;
else
size = lp->d_partitions[part].p_size *
(lp->d_secsize / DEV_BSIZE);
if (omask == 0 && ofdisk_close(dev, 0, S_IFBLK, curproc) != 0)
1996-09-30 20:34:14 +04:00
return -1;
1996-09-30 20:34:14 +04:00
return size;
}
1997-10-09 03:23:13 +04:00
void
ofdisk_getdefaultlabel(struct ofdisk_softc *of, struct disklabel *lp)
1997-10-09 03:23:13 +04:00
{
memset(lp, 0, sizeof *lp);
1997-10-09 03:23:13 +04:00
/*
* XXX Firmware bug? Asking for block size gives a
* XXX rediculous number! So we use what the boot program
* XXX uses.
*/
lp->d_secsize = DEV_BSIZE;
lp->d_secperunit = OF_call_method_1("#blocks",
of->sc_ihandle, 0);
if (lp->d_secperunit == (u_int32_t)-1)
lp->d_secperunit = 0x7fffffff;
lp->d_secpercyl = 1;
lp->d_nsectors = 1;
lp->d_ntracks = 1;
lp->d_ncylinders = lp->d_secperunit;
lp->d_partitions[RAW_PART].p_offset = 0;
lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
lp->d_npartitions = RAW_PART + 1;
lp->d_magic = DISKMAGIC;
lp->d_magic2 = DISKMAGIC;
lp->d_checksum = dkcksum(lp);
}
void
ofdisk_getdisklabel(dev)
1997-10-09 03:23:13 +04:00
dev_t dev;
{
int unit = DISKUNIT(dev);
struct ofdisk_softc *of = ofdisk_cd.cd_devs[unit];
1997-10-09 03:23:13 +04:00
struct disklabel *lp = of->sc_dk.dk_label;
char *errmes;
int l;
ofdisk_getdefaultlabel(of, lp);
1997-10-09 03:23:13 +04:00
/*
* Don't read the disklabel on a floppy; simply
* assign all partitions the same size/offset as
* RAW_PART. (This is essentially what the ISA
* floppy driver does, but we don't deal with
* density stuff.)
*/
if (of->sc_flags & OFDF_ISFLOPPY) {
lp->d_npartitions = MAXPARTITIONS;
for (l = 0; l < lp->d_npartitions; l++) {
if (l == RAW_PART)
continue;
/* struct copy */
lp->d_partitions[l] =
lp->d_partitions[RAW_PART];
}
lp->d_checksum = dkcksum(lp);
} else {
errmes = readdisklabel(MAKEDISKDEV(major(dev),
unit, RAW_PART), ofdisk_strategy, lp,
1997-10-09 03:23:13 +04:00
of->sc_dk.dk_cpulabel);
if (errmes != NULL)
1998-03-21 05:04:55 +03:00
printf("%s: %s\n", of->sc_dev.dv_xname, errmes);
1997-10-09 03:23:13 +04:00
}
}