summaryrefslogtreecommitdiffstats
path: root/sys/dev/ofw/ofw_disk.c
blob: 67a9451d94dba550d44eb63354614469d380fee4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * 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.
 *
 *
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bio.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/linker.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/proc.h>

#include <geom/geom.h>

#include <dev/ofw/openfirm.h>

#define	OFWD_BLOCKSIZE	512

struct ofwd_softc
{
        struct bio_queue_head ofwd_bio_queue;
        struct mtx	ofwd_queue_mtx;
	ihandle_t	ofwd_instance;
	off_t		ofwd_mediasize;
        unsigned	ofwd_sectorsize;
        unsigned	ofwd_fwheads;
        unsigned	ofwd_fwsectors;
        struct proc	*ofwd_procp;
        struct g_geom	*ofwd_gp;
        struct g_provider *ofwd_pp;
} 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,
};

DECLARE_GEOM_CLASS(g_ofwd_class, g_ofwd);

static int ofwd_enable = 0;
TUNABLE_INT("kern.ofw.disk", &ofwd_enable);

static int
ofwd_startio(struct ofwd_softc *sc, struct bio *bp)
{
	u_int r;

	r = OF_seek(sc->ofwd_instance, bp->bio_offset);
        switch (bp->bio_cmd) {
        case BIO_READ:
		r = OF_read(sc->ofwd_instance, (void *)bp->bio_data,
		        bp->bio_length);
                break;
        case BIO_WRITE:
		r = OF_write(sc->ofwd_instance, (void *)bp->bio_data,
		        bp->bio_length);
                break;
        }
	if (r != bp->bio_length)
		panic("ofwd: incorrect i/o count");

        bp->bio_resid = 0;
        return (0);
}

static void
ofwd_kthread(void *arg)
{
	struct ofwd_softc *sc;
	struct bio *bp;
	int error;

        sc = arg;
        curthread->td_base_pri = PRIBIO;

        for (;;) {
		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);
                        continue;
		}
                mtx_unlock(&sc->ofwd_queue_mtx);
                if (bp->bio_cmd == BIO_GETATTR) {
			error = EOPNOTSUPP;
                } else
			error = ofwd_startio(sc, bp);

		if (error != -1) {
                        bp->bio_completed = bp->bio_length;
                        g_io_deliver(bp, error);
                }
	}
}

static void
g_ofwd_init(struct g_class *mp __unused)
{
	struct ofwd_softc *sc;
        struct g_geom *gp;
        struct g_provider *pp;
	char	path[128];
	char	fname[32];
	phandle_t ofd;
	ihandle_t ifd;
	int	error;

	if (ofwd_enable == 0)
		return;

	ofd = OF_finddevice("ofwdisk");
	if (ofd == -1)
		return;

	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;
	}

	sc = (struct ofwd_softc *)malloc(sizeof *sc, M_DEVBUF,
	         M_WAITOK|M_ZERO);
	bioq_init(&sc->ofwd_bio_queue);
        mtx_init(&sc->ofwd_queue_mtx, "ofwd bio queue", NULL, MTX_DEF);
	sc->ofwd_instance = ifd;
	sc->ofwd_mediasize = (off_t)2*33554432 * OFWD_BLOCKSIZE;
	sc->ofwd_sectorsize = OFWD_BLOCKSIZE;
	sc->ofwd_fwsectors = 0;
	sc->ofwd_fwheads = 0;
	error = kthread_create(ofwd_kthread, sc, &sc->ofwd_procp, 0, 0,
		     "ofwd0");
        if (error != 0) {
		free(sc, M_DEVBUF);
                return;
	}

	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);
}

static void
g_ofwd_start(struct bio *bp)
{
        struct ofwd_softc *sc;

        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);
}

static int
g_ofwd_access(struct g_provider *pp, int r, int w, int e)
{

	if (pp->geom->softc == NULL)
		return (ENXIO);
        return (0);
}
OpenPOWER on IntegriCloud