altmem.c revision 1.3 1 1.3 dholland /* $NetBSD: altmem.c,v 1.3 2014/07/25 08:02:19 dholland Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2009 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. The name of the author may not be used to endorse or promote products
13 1.1 jmcneill * derived from this software without specific prior written permission.
14 1.1 jmcneill *
15 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 jmcneill * SUCH DAMAGE.
26 1.1 jmcneill */
27 1.1 jmcneill
28 1.1 jmcneill #include <sys/cdefs.h>
29 1.3 dholland __KERNEL_RCSID(0, "$NetBSD: altmem.c,v 1.3 2014/07/25 08:02:19 dholland Exp $");
30 1.1 jmcneill #include <sys/param.h>
31 1.1 jmcneill #include <sys/types.h>
32 1.1 jmcneill #include <sys/device.h>
33 1.1 jmcneill #include <sys/conf.h>
34 1.1 jmcneill #include <sys/buf.h>
35 1.1 jmcneill #include <sys/disklabel.h>
36 1.1 jmcneill #include <sys/disk.h>
37 1.1 jmcneill
38 1.1 jmcneill #include <dev/altmem/altmemvar.h>
39 1.1 jmcneill
40 1.1 jmcneill struct altmem_softc {
41 1.1 jmcneill device_t sc_dev;
42 1.1 jmcneill
43 1.1 jmcneill struct disk sc_dkdev;
44 1.1 jmcneill
45 1.1 jmcneill void *sc_cookie;
46 1.1 jmcneill const struct altmem_memops *sc_memops;
47 1.1 jmcneill
48 1.1 jmcneill size_t sc_size;
49 1.1 jmcneill };
50 1.1 jmcneill
51 1.1 jmcneill static dev_type_open(altmemopen);
52 1.1 jmcneill static dev_type_close(altmemclose);
53 1.1 jmcneill static dev_type_read(altmemread);
54 1.1 jmcneill static dev_type_write(altmemwrite);
55 1.1 jmcneill static dev_type_ioctl(altmemioctl);
56 1.1 jmcneill static dev_type_strategy(altmemstrategy);
57 1.1 jmcneill static dev_type_size(altmemsize);
58 1.1 jmcneill
59 1.1 jmcneill static int altmem_match(device_t, cfdata_t, void *);
60 1.1 jmcneill static void altmem_attach(device_t, device_t, void *);
61 1.1 jmcneill
62 1.1 jmcneill const struct bdevsw altmem_bdevsw = {
63 1.2 dholland .d_open = altmemopen,
64 1.2 dholland .d_close = altmemclose,
65 1.2 dholland .d_strategy = altmemstrategy,
66 1.2 dholland .d_ioctl = altmemioctl,
67 1.2 dholland .d_dump = nodump,
68 1.2 dholland .d_psize = altmemsize,
69 1.3 dholland .d_discard = nodiscard,
70 1.2 dholland .d_flag = D_DISK
71 1.1 jmcneill };
72 1.1 jmcneill const struct cdevsw altmem_cdevsw = {
73 1.2 dholland .d_open = altmemopen,
74 1.2 dholland .d_close = altmemclose,
75 1.2 dholland .d_read = altmemread,
76 1.2 dholland .d_write = altmemwrite,
77 1.2 dholland .d_ioctl = altmemioctl,
78 1.2 dholland .d_stop = nostop,
79 1.2 dholland .d_tty = notty,
80 1.2 dholland .d_poll = nopoll,
81 1.2 dholland .d_mmap = nommap,
82 1.2 dholland .d_kqfilter = nokqfilter,
83 1.2 dholland .d_flag = D_DISK
84 1.1 jmcneill };
85 1.1 jmcneill static struct dkdriver altmemdkdriver = { altmemstrategy, minphys };
86 1.1 jmcneill extern struct cfdriver altmem_cd;
87 1.1 jmcneill
88 1.1 jmcneill CFATTACH_DECL_NEW(altmem, sizeof(struct altmem_softc), altmem_match,
89 1.1 jmcneill altmem_attach, NULL, NULL);
90 1.1 jmcneill
91 1.1 jmcneill static int
92 1.1 jmcneill altmem_match(device_t parent, cfdata_t match, void *opaque)
93 1.1 jmcneill {
94 1.1 jmcneill return 1;
95 1.1 jmcneill }
96 1.1 jmcneill
97 1.1 jmcneill static void
98 1.1 jmcneill altmem_attach(device_t parent, device_t self, void *opaque)
99 1.1 jmcneill {
100 1.1 jmcneill struct altmem_softc *sc = device_private(self);
101 1.1 jmcneill struct altmem_attach_args *aaa = opaque;
102 1.1 jmcneill char pbuf[9];
103 1.1 jmcneill
104 1.1 jmcneill sc->sc_dev = self;
105 1.1 jmcneill sc->sc_cookie = aaa->cookie;
106 1.1 jmcneill sc->sc_memops = aaa->memops;
107 1.1 jmcneill sc->sc_size = sc->sc_memops->getsize(sc->sc_cookie);
108 1.1 jmcneill
109 1.1 jmcneill format_bytes(pbuf, sizeof(pbuf), sc->sc_size);
110 1.1 jmcneill
111 1.1 jmcneill aprint_naive("\n");
112 1.1 jmcneill aprint_normal(": %s\n", pbuf);
113 1.1 jmcneill
114 1.1 jmcneill disk_init(&sc->sc_dkdev, device_xname(self), &altmemdkdriver);
115 1.1 jmcneill disk_attach(&sc->sc_dkdev);
116 1.1 jmcneill }
117 1.1 jmcneill
118 1.1 jmcneill static int
119 1.1 jmcneill altmemsize(dev_t dev)
120 1.1 jmcneill {
121 1.1 jmcneill struct altmem_softc *sc = device_lookup_private(&altmem_cd, DISKUNIT(dev));
122 1.1 jmcneill if (sc == NULL)
123 1.1 jmcneill return 0;
124 1.1 jmcneill return sc->sc_size >> DEV_BSHIFT;
125 1.1 jmcneill }
126 1.1 jmcneill
127 1.1 jmcneill static int
128 1.1 jmcneill altmemopen(dev_t dev, int flag, int fmt, struct lwp *l)
129 1.1 jmcneill {
130 1.1 jmcneill struct altmem_softc *sc = device_lookup_private(&altmem_cd, DISKUNIT(dev));
131 1.1 jmcneill if (sc == NULL)
132 1.1 jmcneill return ENXIO;
133 1.1 jmcneill return 0;
134 1.1 jmcneill }
135 1.1 jmcneill
136 1.1 jmcneill static int
137 1.1 jmcneill altmemclose(dev_t dev, int flag, int fmt, struct lwp *l)
138 1.1 jmcneill {
139 1.1 jmcneill return 0;
140 1.1 jmcneill }
141 1.1 jmcneill
142 1.1 jmcneill static int
143 1.1 jmcneill altmemread(dev_t dev, struct uio *uio, int flags)
144 1.1 jmcneill {
145 1.1 jmcneill if (device_lookup_private(&altmem_cd, DISKUNIT(dev)) == NULL)
146 1.1 jmcneill return ENXIO;
147 1.1 jmcneill return physio(altmemstrategy, NULL, dev, B_READ, minphys, uio);
148 1.1 jmcneill }
149 1.1 jmcneill
150 1.1 jmcneill static int
151 1.1 jmcneill altmemwrite(dev_t dev, struct uio *uio, int flags)
152 1.1 jmcneill {
153 1.1 jmcneill if (device_lookup_private(&altmem_cd, DISKUNIT(dev)) == NULL)
154 1.1 jmcneill return ENXIO;
155 1.1 jmcneill return physio(altmemstrategy, NULL, dev, B_WRITE, minphys, uio);
156 1.1 jmcneill }
157 1.1 jmcneill
158 1.1 jmcneill static void
159 1.1 jmcneill altmemstrategy(struct buf *bp)
160 1.1 jmcneill {
161 1.1 jmcneill struct altmem_softc *sc = device_lookup_private(&altmem_cd, DISKUNIT(bp->b_dev));
162 1.1 jmcneill
163 1.1 jmcneill if (sc == NULL) {
164 1.1 jmcneill bp->b_error = ENXIO;
165 1.1 jmcneill biodone(bp);
166 1.1 jmcneill return;
167 1.1 jmcneill }
168 1.1 jmcneill if (bp->b_bcount == 0) {
169 1.1 jmcneill biodone(bp);
170 1.1 jmcneill return;
171 1.1 jmcneill }
172 1.1 jmcneill
173 1.1 jmcneill sc->sc_memops->strategy(sc->sc_cookie, bp);
174 1.1 jmcneill biodone(bp);
175 1.1 jmcneill }
176 1.1 jmcneill
177 1.1 jmcneill static int
178 1.1 jmcneill altmemioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
179 1.1 jmcneill {
180 1.1 jmcneill struct altmem_softc *sc = device_lookup_private(&altmem_cd, DISKUNIT(dev));
181 1.1 jmcneill struct dkwedge_info *dkw;
182 1.1 jmcneill
183 1.1 jmcneill switch (cmd) {
184 1.1 jmcneill case DIOCGWEDGEINFO:
185 1.1 jmcneill dkw = (void *)data;
186 1.1 jmcneill strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
187 1.1 jmcneill sizeof(dkw->dkw_devname));
188 1.1 jmcneill strlcpy(dkw->dkw_wname, "altmem", sizeof(dkw->dkw_wname));
189 1.1 jmcneill dkw->dkw_parent[0] = '\0';
190 1.1 jmcneill dkw->dkw_offset = 0;
191 1.1 jmcneill dkw->dkw_size = sc->sc_size >> DEV_BSHIFT;
192 1.1 jmcneill strcpy(dkw->dkw_ptype, DKW_PTYPE_UNUSED);
193 1.1 jmcneill break;
194 1.1 jmcneill default:
195 1.1 jmcneill return ENOTTY;
196 1.1 jmcneill }
197 1.1 jmcneill return 0;
198 1.1 jmcneill }
199