2005-01-06 01:43:34 +00:00
|
|
|
/*-
|
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>
|
|
|
|
#include <sys/kernel.h>
|
2004-12-21 02:23:35 +00:00
|
|
|
#include <sys/kthread.h>
|
|
|
|
#include <sys/linker.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/proc.h>
|
2002-04-15 10:54:22 +00:00
|
|
|
|
2004-12-21 02:23:35 +00:00
|
|
|
#include <geom/geom.h>
|
2002-04-15 10:54:22 +00:00
|
|
|
|
2004-12-21 02:23:35 +00:00
|
|
|
#include <dev/ofw/openfirm.h>
|
2002-04-15 10:54:22 +00:00
|
|
|
|
|
|
|
#define OFWD_BLOCKSIZE 512
|
|
|
|
|
|
|
|
struct ofwd_softc
|
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
struct bio_queue_head ofwd_bio_queue;
|
|
|
|
struct mtx ofwd_queue_mtx;
|
2002-04-15 10:54:22 +00:00
|
|
|
ihandle_t ofwd_instance;
|
2004-12-21 02:23:35 +00:00
|
|
|
off_t ofwd_mediasize;
|
2009-06-14 00:05:38 +00:00
|
|
|
unsigned ofwd_sectorsize;
|
|
|
|
unsigned ofwd_fwheads;
|
|
|
|
unsigned ofwd_fwsectors;
|
|
|
|
struct proc *ofwd_procp;
|
|
|
|
struct g_geom *ofwd_gp;
|
|
|
|
struct g_provider *ofwd_pp;
|
2004-12-21 02:23:35 +00:00
|
|
|
} ofwd_softc;
|
|
|
|
|
|
|
|
static g_init_t g_ofwd_init;
|
|
|
|
static g_start_t g_ofwd_start;
|
|
|
|
static g_access_t g_ofwd_access;
|
|
|
|
|
|
|
|
struct g_class g_ofwd_class = {
|
|
|
|
.name = "OFWD",
|
|
|
|
.version = G_VERSION,
|
|
|
|
.init = g_ofwd_init,
|
|
|
|
.start = g_ofwd_start,
|
|
|
|
.access = g_ofwd_access,
|
2002-04-15 10:54:22 +00:00
|
|
|
};
|
|
|
|
|
2004-12-21 02:23:35 +00:00
|
|
|
DECLARE_GEOM_CLASS(g_ofwd_class, g_ofwd);
|
2002-04-15 10:54:22 +00:00
|
|
|
|
2004-12-28 05:07:49 +00:00
|
|
|
static int ofwd_enable = 0;
|
|
|
|
TUNABLE_INT("kern.ofw.disk", &ofwd_enable);
|
|
|
|
|
2004-12-21 02:23:35 +00:00
|
|
|
static int
|
|
|
|
ofwd_startio(struct ofwd_softc *sc, struct bio *bp)
|
2002-04-15 10:54:22 +00:00
|
|
|
{
|
2004-12-21 02:23:35 +00:00
|
|
|
u_int r;
|
2004-06-25 13:43:56 +00:00
|
|
|
|
2003-10-18 17:26:13 +00:00
|
|
|
r = OF_seek(sc->ofwd_instance, bp->bio_offset);
|
2005-10-31 03:09:38 +00:00
|
|
|
|
2009-06-14 00:05:38 +00:00
|
|
|
switch (bp->bio_cmd) {
|
|
|
|
case BIO_READ:
|
2002-04-15 10:54:22 +00:00
|
|
|
r = OF_read(sc->ofwd_instance, (void *)bp->bio_data,
|
2009-06-14 00:15:26 +00:00
|
|
|
bp->bio_length);
|
2009-06-14 00:05:38 +00:00
|
|
|
break;
|
|
|
|
case BIO_WRITE:
|
2002-04-15 10:54:22 +00:00
|
|
|
r = OF_write(sc->ofwd_instance, (void *)bp->bio_data,
|
2009-06-14 00:15:26 +00:00
|
|
|
bp->bio_length);
|
2009-06-14 00:05:38 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-12-21 02:23:35 +00:00
|
|
|
if (r != bp->bio_length)
|
|
|
|
panic("ofwd: incorrect i/o count");
|
|
|
|
|
2009-06-14 00:05:38 +00:00
|
|
|
bp->bio_resid = 0;
|
|
|
|
return (0);
|
2004-12-21 02:23:35 +00:00
|
|
|
}
|
2004-02-04 12:52:57 +00:00
|
|
|
|
2004-12-21 02:23:35 +00:00
|
|
|
static void
|
|
|
|
ofwd_kthread(void *arg)
|
|
|
|
{
|
|
|
|
struct ofwd_softc *sc;
|
|
|
|
struct bio *bp;
|
|
|
|
int error;
|
|
|
|
|
2009-06-14 00:05:38 +00:00
|
|
|
sc = arg;
|
|
|
|
curthread->td_base_pri = PRIBIO;
|
2004-12-21 02:23:35 +00:00
|
|
|
|
2009-06-14 00:05:38 +00:00
|
|
|
for (;;) {
|
2004-12-21 02:23:35 +00:00
|
|
|
mtx_lock(&sc->ofwd_queue_mtx);
|
|
|
|
bp = bioq_takefirst(&sc->ofwd_bio_queue);
|
|
|
|
if (!bp) {
|
|
|
|
msleep(sc, &sc->ofwd_queue_mtx, PRIBIO | PDROP,
|
|
|
|
"ofwdwait", 0);
|
2009-06-14 00:05:38 +00:00
|
|
|
continue;
|
2004-12-21 02:23:35 +00:00
|
|
|
}
|
2009-06-14 00:05:38 +00:00
|
|
|
mtx_unlock(&sc->ofwd_queue_mtx);
|
|
|
|
if (bp->bio_cmd == BIO_GETATTR) {
|
2004-12-21 02:23:35 +00:00
|
|
|
error = EOPNOTSUPP;
|
2009-06-14 00:05:38 +00:00
|
|
|
} else
|
2004-12-21 02:23:35 +00:00
|
|
|
error = ofwd_startio(sc, bp);
|
|
|
|
|
|
|
|
if (error != -1) {
|
2009-06-14 00:05:38 +00:00
|
|
|
bp->bio_completed = bp->bio_length;
|
|
|
|
g_io_deliver(bp, error);
|
|
|
|
}
|
2004-02-04 12:52:57 +00:00
|
|
|
}
|
2002-04-15 10:54:22 +00:00
|
|
|
}
|
|
|
|
|
2004-02-04 12:52:57 +00:00
|
|
|
static void
|
2004-12-21 02:23:35 +00:00
|
|
|
g_ofwd_init(struct g_class *mp __unused)
|
2004-02-04 12:52:57 +00:00
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
char path[128];
|
|
|
|
char fname[32];
|
2004-02-04 12:52:57 +00:00
|
|
|
phandle_t ofd;
|
2009-06-14 00:05:38 +00:00
|
|
|
struct ofwd_softc *sc;
|
|
|
|
struct g_geom *gp;
|
|
|
|
struct g_provider *pp;
|
2004-12-21 02:23:35 +00:00
|
|
|
ihandle_t ifd;
|
2009-06-14 00:05:38 +00:00
|
|
|
int error;
|
2004-02-04 12:52:57 +00:00
|
|
|
|
2004-12-28 05:07:49 +00:00
|
|
|
if (ofwd_enable == 0)
|
|
|
|
return;
|
|
|
|
|
2004-02-04 12:52:57 +00:00
|
|
|
ofd = OF_finddevice("ofwdisk");
|
|
|
|
if (ofd == -1)
|
|
|
|
return;
|
|
|
|
|
2004-12-21 02:23:35 +00:00
|
|
|
bzero(path, 128);
|
|
|
|
OF_package_to_path(ofd, path, 128);
|
|
|
|
OF_getprop(ofd, "file", fname, sizeof(fname));
|
|
|
|
printf("ofw_disk located at %s, file %s\n", path, fname);
|
|
|
|
ifd = OF_open(path);
|
|
|
|
if (ifd == -1) {
|
|
|
|
printf("ofw_disk: could not create instance\n");
|
|
|
|
return;
|
|
|
|
}
|
2004-02-04 12:52:57 +00:00
|
|
|
|
2004-12-21 02:23:35 +00:00
|
|
|
sc = (struct ofwd_softc *)malloc(sizeof *sc, M_DEVBUF,
|
2009-06-14 00:15:26 +00:00
|
|
|
M_WAITOK | M_ZERO);
|
2004-12-21 02:23:35 +00:00
|
|
|
bioq_init(&sc->ofwd_bio_queue);
|
2009-06-14 00:05:38 +00:00
|
|
|
mtx_init(&sc->ofwd_queue_mtx, "ofwd bio queue", NULL, MTX_DEF);
|
2004-12-21 02:23:35 +00:00
|
|
|
sc->ofwd_instance = ifd;
|
2009-06-14 00:05:38 +00:00
|
|
|
sc->ofwd_mediasize = (off_t)2 * 33554432;
|
2004-12-21 02:23:35 +00:00
|
|
|
sc->ofwd_sectorsize = OFWD_BLOCKSIZE;
|
|
|
|
sc->ofwd_fwsectors = 0;
|
|
|
|
sc->ofwd_fwheads = 0;
|
2007-10-20 23:23:23 +00:00
|
|
|
error = kproc_create(ofwd_kthread, sc, &sc->ofwd_procp, 0, 0,
|
2009-06-14 00:05:38 +00:00
|
|
|
"ofwd0");
|
|
|
|
if (error != 0) {
|
2004-12-21 02:23:35 +00:00
|
|
|
free(sc, M_DEVBUF);
|
2009-06-14 00:05:38 +00:00
|
|
|
return;
|
2004-02-04 12:52:57 +00:00
|
|
|
}
|
2004-12-21 02:23:35 +00:00
|
|
|
|
|
|
|
gp = g_new_geomf(&g_ofwd_class, "ofwd0");
|
|
|
|
gp->softc = sc;
|
|
|
|
pp = g_new_providerf(gp, "ofwd0");
|
|
|
|
pp->mediasize = sc->ofwd_mediasize;
|
|
|
|
pp->sectorsize = sc->ofwd_sectorsize;
|
|
|
|
sc->ofwd_gp = gp;
|
|
|
|
sc->ofwd_pp = pp;
|
|
|
|
g_error_provider(pp, 0);
|
2004-02-04 12:52:57 +00:00
|
|
|
}
|
|
|
|
|
2004-12-21 02:23:35 +00:00
|
|
|
static void
|
|
|
|
g_ofwd_start(struct bio *bp)
|
2002-04-15 10:54:22 +00:00
|
|
|
{
|
2009-06-14 00:05:38 +00:00
|
|
|
struct ofwd_softc *sc;
|
2003-12-15 09:53:53 +00:00
|
|
|
|
2009-06-14 00:05:38 +00:00
|
|
|
sc = bp->bio_to->geom->softc;
|
|
|
|
mtx_lock(&sc->ofwd_queue_mtx);
|
|
|
|
bioq_disksort(&sc->ofwd_bio_queue, bp);
|
|
|
|
mtx_unlock(&sc->ofwd_queue_mtx);
|
|
|
|
wakeup(sc);
|
2002-04-15 10:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-12-21 02:23:35 +00:00
|
|
|
g_ofwd_access(struct g_provider *pp, int r, int w, int e)
|
2002-04-15 10:54:22 +00:00
|
|
|
{
|
|
|
|
|
2004-12-21 02:23:35 +00:00
|
|
|
if (pp->geom->softc == NULL)
|
2002-04-15 10:54:22 +00:00
|
|
|
return (ENXIO);
|
2009-06-14 00:05:38 +00:00
|
|
|
return (0);
|
2002-04-15 10:54:22 +00:00
|
|
|
}
|