2002-04-15 10:54:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2002 Benno Rice <benno@FreeBSD.org>
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY Benno Rice ``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.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-08-24 17:55:58 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2002-04-15 10:54:22 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/bio.h>
|
2004-06-25 13:43:56 +00:00
|
|
|
#include <sys/module.h>
|
2002-04-15 10:54:22 +00:00
|
|
|
#include <sys/bus.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/kernel.h>
|
2004-02-04 12:52:57 +00:00
|
|
|
#include <sys/limits.h>
|
2003-04-03 16:28:57 +00:00
|
|
|
#include <geom/geom_disk.h>
|
2002-04-15 10:54:22 +00:00
|
|
|
|
|
|
|
#include <dev/ofw/openfirm.h>
|
|
|
|
|
|
|
|
#include <machine/bus.h>
|
|
|
|
#include <machine/md_var.h>
|
|
|
|
#include <machine/nexusvar.h>
|
|
|
|
|
|
|
|
#define OFWD_BLOCKSIZE 512
|
|
|
|
|
|
|
|
struct ofwd_softc
|
|
|
|
{
|
|
|
|
device_t ofwd_dev;
|
2004-02-18 21:36:53 +00:00
|
|
|
struct disk *ofwd_disk;
|
2002-04-15 10:54:22 +00:00
|
|
|
phandle_t ofwd_package;
|
|
|
|
ihandle_t ofwd_instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disk device bus interface.
|
|
|
|
*/
|
2004-02-04 12:52:57 +00:00
|
|
|
static void ofwd_identify(driver_t *, device_t);
|
2002-04-15 10:54:22 +00:00
|
|
|
static int ofwd_probe(device_t);
|
|
|
|
static int ofwd_attach(device_t);
|
|
|
|
|
|
|
|
static device_method_t ofwd_methods[] = {
|
2004-02-04 12:52:57 +00:00
|
|
|
DEVMETHOD(device_identify, ofwd_identify),
|
2002-04-15 10:54:22 +00:00
|
|
|
DEVMETHOD(device_probe, ofwd_probe),
|
|
|
|
DEVMETHOD(device_attach, ofwd_attach),
|
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static driver_t ofwd_driver = {
|
|
|
|
"ofwd",
|
|
|
|
ofwd_methods,
|
|
|
|
sizeof(struct ofwd_softc)
|
|
|
|
};
|
|
|
|
|
|
|
|
static devclass_t ofwd_devclass;
|
|
|
|
|
|
|
|
DRIVER_MODULE(ofwd, nexus, ofwd_driver, ofwd_devclass, 0, 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disk device control interface.
|
|
|
|
*/
|
2003-02-23 19:15:34 +00:00
|
|
|
static disk_strategy_t ofwd_strategy;
|
2002-04-15 10:54:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle an I/O request.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ofwd_strategy(struct bio *bp)
|
|
|
|
{
|
|
|
|
struct ofwd_softc *sc;
|
|
|
|
long r;
|
|
|
|
|
2003-02-23 19:15:34 +00:00
|
|
|
sc = (struct ofwd_softc *)bp->bio_disk->d_drv1;
|
2002-04-15 10:54:22 +00:00
|
|
|
|
|
|
|
if (sc == NULL) {
|
|
|
|
printf("ofwd: bio for invalid disk!\n");
|
2003-02-23 19:15:34 +00:00
|
|
|
biofinish(bp, NULL, EINVAL);
|
2002-04-15 10:54:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-06-25 13:43:56 +00:00
|
|
|
bp->bio_resid = bp->bio_bcount;
|
|
|
|
|
2003-10-18 17:26:13 +00:00
|
|
|
r = OF_seek(sc->ofwd_instance, bp->bio_offset);
|
2002-04-15 10:54:22 +00:00
|
|
|
if (r == -1) {
|
|
|
|
device_printf(sc->ofwd_dev, "seek failed\n");
|
2003-02-23 19:15:34 +00:00
|
|
|
biofinish(bp, NULL, EIO);
|
2002-04-15 10:54:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bp->bio_cmd == BIO_READ) {
|
|
|
|
r = OF_read(sc->ofwd_instance, (void *)bp->bio_data,
|
|
|
|
bp->bio_bcount);
|
|
|
|
} else {
|
|
|
|
r = OF_write(sc->ofwd_instance, (void *)bp->bio_data,
|
|
|
|
bp->bio_bcount);
|
|
|
|
}
|
|
|
|
|
2003-02-23 19:15:34 +00:00
|
|
|
if (r > bp->bio_bcount)
|
|
|
|
panic("ofwd: more bytes read/written than requested");
|
|
|
|
if (r == -1) {
|
2002-04-15 10:54:22 +00:00
|
|
|
device_printf(sc->ofwd_dev, "r (%ld) < bp->bio_bcount (%ld)\n",
|
|
|
|
r, bp->bio_bcount);
|
2003-02-23 19:15:34 +00:00
|
|
|
biofinish(bp, NULL, EIO);
|
|
|
|
return;
|
|
|
|
}
|
2002-04-15 10:54:22 +00:00
|
|
|
|
|
|
|
bp->bio_resid -= r;
|
2004-02-04 12:52:57 +00:00
|
|
|
|
2003-02-23 19:15:34 +00:00
|
|
|
if (r < bp->bio_bcount) {
|
|
|
|
device_printf(sc->ofwd_dev, "r (%ld) < bp->bio_bcount (%ld)\n",
|
|
|
|
r, bp->bio_bcount);
|
|
|
|
biofinish(bp, NULL, EIO); /* XXX: probably not an error */
|
|
|
|
return;
|
2004-02-04 12:52:57 +00:00
|
|
|
}
|
2003-03-08 08:01:31 +00:00
|
|
|
biodone(bp);
|
2002-04-15 10:54:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-02-04 12:52:57 +00:00
|
|
|
/*
|
2004-08-16 15:45:27 +00:00
|
|
|
* Attach the Open Firmware disk to nexus if present.
|
2004-02-04 12:52:57 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ofwd_identify(driver_t *driver, device_t parent)
|
|
|
|
{
|
|
|
|
device_t child;
|
|
|
|
phandle_t ofd;
|
|
|
|
static char type[8];
|
|
|
|
|
|
|
|
ofd = OF_finddevice("ofwdisk");
|
|
|
|
if (ofd == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
OF_getprop(ofd, "device_type", type, sizeof(type));
|
|
|
|
|
|
|
|
child = BUS_ADD_CHILD(parent, INT_MAX, "ofwd", 0);
|
|
|
|
if (child != NULL) {
|
|
|
|
nexus_set_device_type(child, type);
|
|
|
|
nexus_set_node(child, ofd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-15 10:54:22 +00:00
|
|
|
/*
|
2004-08-16 15:45:27 +00:00
|
|
|
* Probe for an Open Firmware disk.
|
2002-04-15 10:54:22 +00:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ofwd_probe(device_t dev)
|
|
|
|
{
|
|
|
|
char *type;
|
2003-12-15 09:53:53 +00:00
|
|
|
char fname[32];
|
|
|
|
phandle_t node;
|
2002-04-15 10:54:22 +00:00
|
|
|
|
|
|
|
type = nexus_get_device_type(dev);
|
2003-12-15 09:53:53 +00:00
|
|
|
node = nexus_get_node(dev);
|
2002-04-15 10:54:22 +00:00
|
|
|
|
2004-02-04 12:52:57 +00:00
|
|
|
if (type == NULL ||
|
2003-12-12 09:54:39 +00:00
|
|
|
(strcmp(type, "disk") != 0 && strcmp(type, "block") != 0))
|
2002-04-15 10:54:22 +00:00
|
|
|
return (ENXIO);
|
|
|
|
|
2003-12-15 09:53:53 +00:00
|
|
|
if (OF_getprop(node, "file", fname, sizeof(fname)) == -1)
|
|
|
|
return (ENXIO);
|
|
|
|
|
2004-08-16 15:45:27 +00:00
|
|
|
device_set_desc(dev, "Open Firmware disk");
|
2002-04-15 10:54:22 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ofwd_attach(device_t dev)
|
|
|
|
{
|
|
|
|
struct ofwd_softc *sc;
|
|
|
|
char path[128];
|
2003-12-15 09:53:53 +00:00
|
|
|
char fname[32];
|
2002-04-15 10:54:22 +00:00
|
|
|
|
|
|
|
sc = device_get_softc(dev);
|
|
|
|
sc->ofwd_dev = dev;
|
|
|
|
|
|
|
|
bzero(path, 128);
|
|
|
|
OF_package_to_path(nexus_get_node(dev), path, 128);
|
2003-12-15 09:53:53 +00:00
|
|
|
OF_getprop(nexus_get_node(dev), "file", fname, sizeof(fname));
|
|
|
|
device_printf(dev, "located at %s, file %s\n", path, fname);
|
2002-04-15 10:54:22 +00:00
|
|
|
sc->ofwd_instance = OF_open(path);
|
|
|
|
if (sc->ofwd_instance == -1) {
|
|
|
|
device_printf(dev, "could not create instance\n");
|
|
|
|
return (ENXIO);
|
|
|
|
}
|
|
|
|
|
2004-02-18 21:36:53 +00:00
|
|
|
sc->ofwd_disk = disk_alloc();
|
|
|
|
sc->ofwd_disk->d_strategy = ofwd_strategy;
|
|
|
|
sc->ofwd_disk->d_name = "ofwd";
|
|
|
|
sc->ofwd_disk->d_sectorsize = OFWD_BLOCKSIZE;
|
|
|
|
sc->ofwd_disk->d_mediasize = (off_t)33554432 * OFWD_BLOCKSIZE;
|
|
|
|
sc->ofwd_disk->d_fwsectors = 0;
|
|
|
|
sc->ofwd_disk->d_fwheads = 0;
|
|
|
|
sc->ofwd_disk->d_drv1 = sc;
|
|
|
|
sc->ofwd_disk->d_maxsize = PAGE_SIZE;
|
|
|
|
sc->ofwd_disk->d_unit = device_get_unit(dev);
|
|
|
|
sc->ofwd_disk->d_flags = DISKFLAG_NEEDSGIANT;
|
|
|
|
disk_create(sc->ofwd_disk, DISK_VERSION);
|
2002-04-15 10:54:22 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|