pud.c revision 1.6 1 1.6 pooka /* $NetBSD: pud.c,v 1.6 2007/11/28 17:01:59 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Development of this software was supported by the
7 1.1 pooka * Research Foundation of Helsinki University of Technology
8 1.1 pooka *
9 1.1 pooka * Redistribution and use in source and binary forms, with or without
10 1.1 pooka * modification, are permitted provided that the following conditions
11 1.1 pooka * are met:
12 1.1 pooka * 1. Redistributions of source code must retain the above copyright
13 1.1 pooka * notice, this list of conditions and the following disclaimer.
14 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 pooka * notice, this list of conditions and the following disclaimer in the
16 1.1 pooka * documentation and/or other materials provided with the distribution.
17 1.1 pooka *
18 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 pooka * SUCH DAMAGE.
29 1.1 pooka */
30 1.1 pooka
31 1.1 pooka #include <sys/cdefs.h>
32 1.6 pooka __KERNEL_RCSID(0, "$NetBSD: pud.c,v 1.6 2007/11/28 17:01:59 pooka Exp $");
33 1.1 pooka
34 1.1 pooka #include <sys/param.h>
35 1.1 pooka #include <sys/conf.h>
36 1.1 pooka #include <sys/kmem.h>
37 1.1 pooka #include <sys/poll.h>
38 1.1 pooka #include <sys/queue.h>
39 1.1 pooka
40 1.1 pooka #include <dev/pud/pud_sys.h>
41 1.1 pooka #include <dev/putter/putter_sys.h>
42 1.1 pooka
43 1.1 pooka void pudattach(void);
44 1.1 pooka
45 1.1 pooka static int pud_putter_getout(void *, size_t, int, uint8_t **,
46 1.1 pooka size_t *, void **);
47 1.1 pooka static void pud_putter_releaseout(void *, void *, int);
48 1.1 pooka static int pud_putter_dispatch(void *, struct putter_hdr *);
49 1.1 pooka static size_t pud_putter_waitcount(void *);
50 1.1 pooka static int pud_putter_close(void *);
51 1.1 pooka
52 1.1 pooka struct putter_ops pud_putter = {
53 1.1 pooka .pop_getout = pud_putter_getout,
54 1.1 pooka .pop_releaseout = pud_putter_releaseout,
55 1.1 pooka .pop_waitcount = pud_putter_waitcount,
56 1.1 pooka .pop_dispatch = pud_putter_dispatch,
57 1.1 pooka .pop_close = pud_putter_close,
58 1.1 pooka };
59 1.1 pooka
60 1.1 pooka extern struct bdevsw pud_bdevsw;
61 1.1 pooka extern struct cdevsw pud_cdevsw;
62 1.1 pooka
63 1.1 pooka kmutex_t pud_mtx;
64 1.1 pooka static LIST_HEAD(, pud_dev) pudlist = LIST_HEAD_INITIALIZER(pudlist);
65 1.1 pooka
66 1.1 pooka static uint64_t
67 1.1 pooka nextreq(struct pud_dev *pd)
68 1.1 pooka {
69 1.1 pooka uint64_t rv;
70 1.1 pooka
71 1.1 pooka mutex_enter(&pd->pd_mtx);
72 1.1 pooka rv = pd->pd_nextreq++;
73 1.1 pooka mutex_exit(&pd->pd_mtx);
74 1.1 pooka
75 1.1 pooka return rv;
76 1.1 pooka }
77 1.1 pooka
78 1.1 pooka static int
79 1.1 pooka pud_putter_getout(void *this, size_t maxsize, int nonblock,
80 1.1 pooka uint8_t **data, size_t *dlen, void **cookie)
81 1.1 pooka {
82 1.1 pooka struct pud_dev *pd = this;
83 1.1 pooka struct pud_touser *putp;
84 1.2 dogcow int error = 0;
85 1.1 pooka
86 1.1 pooka mutex_enter(&pd->pd_mtx);
87 1.1 pooka for (;;) {
88 1.1 pooka if (TAILQ_EMPTY(&pd->pd_waitq_req)) {
89 1.1 pooka if (nonblock) {
90 1.1 pooka error = EWOULDBLOCK;
91 1.1 pooka break;
92 1.1 pooka }
93 1.1 pooka
94 1.1 pooka error = cv_wait_sig(&pd->pd_waitq_req_cv, &pd->pd_mtx);
95 1.1 pooka if (error)
96 1.1 pooka break;
97 1.1 pooka else
98 1.1 pooka continue;
99 1.1 pooka }
100 1.1 pooka
101 1.1 pooka putp = TAILQ_FIRST(&pd->pd_waitq_req);
102 1.1 pooka TAILQ_REMOVE(&pd->pd_waitq_req, putp, pt_entries);
103 1.3 pooka KASSERT(error == 0);
104 1.1 pooka break;
105 1.1 pooka }
106 1.1 pooka mutex_exit(&pd->pd_mtx);
107 1.1 pooka
108 1.1 pooka if (error == 0) {
109 1.1 pooka *data = (uint8_t *)putp->pt_pdr;
110 1.1 pooka *dlen = putp->pt_pdr->pdr_pth.pth_framelen;
111 1.1 pooka *cookie = putp;
112 1.1 pooka }
113 1.1 pooka
114 1.1 pooka return error;
115 1.1 pooka }
116 1.1 pooka
117 1.1 pooka static void
118 1.1 pooka pud_putter_releaseout(void *this, void *cookie, int status)
119 1.1 pooka {
120 1.1 pooka struct pud_dev *pd = this;
121 1.1 pooka struct pud_touser *putp = cookie;
122 1.1 pooka
123 1.1 pooka mutex_enter(&pd->pd_mtx);
124 1.1 pooka TAILQ_INSERT_TAIL(&pd->pd_waitq_resp, putp, pt_entries);
125 1.1 pooka mutex_exit(&pd->pd_mtx);
126 1.1 pooka
127 1.1 pooka }
128 1.1 pooka
129 1.1 pooka static size_t
130 1.1 pooka pud_putter_waitcount(void *this)
131 1.1 pooka {
132 1.1 pooka struct pud_dev *pd = this;
133 1.1 pooka size_t rv;
134 1.1 pooka
135 1.1 pooka mutex_enter(&pd->pd_mtx);
136 1.1 pooka rv = pd->pd_waitcount;
137 1.1 pooka mutex_exit(&pd->pd_mtx);
138 1.1 pooka
139 1.1 pooka return rv;
140 1.1 pooka }
141 1.1 pooka
142 1.1 pooka static int
143 1.1 pooka pudop_dev(struct pud_dev *pd, struct pud_req *pdr)
144 1.1 pooka {
145 1.1 pooka struct putter_hdr *pth = (void *)pdr;
146 1.1 pooka struct pud_touser *putp;
147 1.1 pooka
148 1.1 pooka mutex_enter(&pd->pd_mtx);
149 1.1 pooka TAILQ_FOREACH(putp, &pd->pd_waitq_resp, pt_entries)
150 1.1 pooka if (putp->pt_pdr->pdr_reqid == pdr->pdr_reqid)
151 1.1 pooka break;
152 1.1 pooka if (putp == NULL) {
153 1.1 pooka mutex_exit(&pd->pd_mtx);
154 1.1 pooka return EINVAL;
155 1.1 pooka }
156 1.1 pooka TAILQ_REMOVE(&pd->pd_waitq_resp, putp, pt_entries);
157 1.1 pooka mutex_exit(&pd->pd_mtx);
158 1.1 pooka
159 1.1 pooka if (pth->pth_framelen > putp->pt_pdr->pdr_len) {
160 1.1 pooka return EINVAL;
161 1.1 pooka }
162 1.1 pooka memcpy(putp->pt_pdr, pth, pth->pth_framelen);
163 1.1 pooka cv_signal(&putp->pt_cv);
164 1.1 pooka
165 1.1 pooka return 0;
166 1.1 pooka }
167 1.1 pooka
168 1.1 pooka /*
169 1.1 pooka * Register our major number. Always register char device functions,
170 1.1 pooka * register block devices optionally.
171 1.1 pooka *
172 1.1 pooka * XXX: no way to configure "any major you like" currently.
173 1.1 pooka */
174 1.1 pooka static int
175 1.1 pooka pudconf_reg(struct pud_dev *pd, struct pud_conf_reg *pcr)
176 1.1 pooka {
177 1.1 pooka struct bdevsw *bsw;
178 1.1 pooka int cmajor, bmajor, error;
179 1.1 pooka
180 1.5 pooka if (pcr->pm_version != (PUD_DEVELVERSION | PUD_VERSION)) {
181 1.5 pooka printf("pud version mismatch %d vs %d\n",
182 1.5 pooka pcr->pm_version & ~PUD_DEVELVERSION, PUD_VERSION);
183 1.5 pooka return EINVAL; /* XXX */
184 1.5 pooka }
185 1.5 pooka
186 1.1 pooka cmajor = major(pcr->pm_regdev);
187 1.3 pooka if (pcr->pm_flags & PUD_CONFFLAG_BDEV) {
188 1.1 pooka bsw = &pud_bdevsw;
189 1.1 pooka bmajor = cmajor;
190 1.1 pooka } else {
191 1.1 pooka bsw = NULL;
192 1.1 pooka bmajor = -1;
193 1.1 pooka }
194 1.1 pooka
195 1.1 pooka pcr->pm_devname[PUD_DEVNAME_MAX] = '\0';
196 1.1 pooka error = devsw_attach(pcr->pm_devname, bsw, &bmajor,
197 1.1 pooka &pud_cdevsw, &cmajor);
198 1.1 pooka if (error == 0)
199 1.1 pooka pd->pd_dev = pcr->pm_regdev;
200 1.1 pooka
201 1.1 pooka return error;
202 1.1 pooka }
203 1.1 pooka
204 1.1 pooka static int
205 1.1 pooka pudop_conf(struct pud_dev *pd, struct pud_req *pdr)
206 1.1 pooka {
207 1.1 pooka int rv;
208 1.1 pooka
209 1.1 pooka switch (pdr->pdr_reqtype) {
210 1.1 pooka case PUD_CONF_REG:
211 1.1 pooka rv = pudconf_reg(pd, (struct pud_conf_reg *)pdr);
212 1.1 pooka break;
213 1.1 pooka case PUD_CONF_DEREG:
214 1.1 pooka /* unimplemented */
215 1.1 pooka rv = 0;
216 1.1 pooka break;
217 1.1 pooka default:
218 1.1 pooka rv = EINVAL;
219 1.1 pooka break;
220 1.1 pooka }
221 1.1 pooka
222 1.1 pooka return rv;
223 1.1 pooka }
224 1.1 pooka
225 1.1 pooka static int
226 1.1 pooka pud_putter_dispatch(void *this, struct putter_hdr *pth)
227 1.1 pooka {
228 1.1 pooka struct pud_dev *pd = this;
229 1.1 pooka struct pud_req *pdr = (void *)pth;
230 1.1 pooka int rv;
231 1.1 pooka
232 1.1 pooka if (pdr->pdr_pth.pth_framelen < sizeof(struct pud_req))
233 1.1 pooka return EINVAL;
234 1.1 pooka
235 1.1 pooka switch (pdr->pdr_reqclass) {
236 1.1 pooka case PUD_REQ_CDEV:
237 1.1 pooka case PUD_REQ_BDEV:
238 1.1 pooka rv = pudop_dev(pd, pdr);
239 1.1 pooka break;
240 1.1 pooka case PUD_REQ_CONF:
241 1.1 pooka rv = pudop_conf(pd, pdr);
242 1.1 pooka break;
243 1.1 pooka default:
244 1.1 pooka rv = EINVAL;
245 1.1 pooka break;
246 1.1 pooka }
247 1.1 pooka
248 1.1 pooka return rv;
249 1.1 pooka }
250 1.1 pooka
251 1.1 pooka /* Device server severed the umbilical cord */
252 1.1 pooka static int
253 1.1 pooka pud_putter_close(void *this)
254 1.1 pooka {
255 1.1 pooka struct pud_dev *pd = this;
256 1.1 pooka struct pud_touser *putp;
257 1.1 pooka
258 1.1 pooka mutex_enter(&pud_mtx);
259 1.1 pooka LIST_REMOVE(pd, pd_entries);
260 1.1 pooka mutex_exit(&pud_mtx);
261 1.1 pooka
262 1.1 pooka mutex_enter(&pd->pd_mtx);
263 1.1 pooka while ((putp = TAILQ_FIRST(&pd->pd_waitq_req)) != NULL) {
264 1.1 pooka putp->pt_pdr->pdr_rv = ENXIO;
265 1.1 pooka cv_signal(&putp->pt_cv);
266 1.1 pooka TAILQ_REMOVE(&pd->pd_waitq_req, putp, pt_entries);
267 1.1 pooka }
268 1.1 pooka
269 1.1 pooka while ((putp = TAILQ_FIRST(&pd->pd_waitq_resp)) != NULL) {
270 1.1 pooka putp->pt_pdr->pdr_rv = ENXIO;
271 1.1 pooka cv_signal(&putp->pt_cv);
272 1.1 pooka TAILQ_REMOVE(&pd->pd_waitq_resp, putp, pt_entries);
273 1.1 pooka }
274 1.1 pooka if (pd->pd_waitcount)
275 1.1 pooka cv_wait(&pd->pd_draincv, &pd->pd_mtx);
276 1.1 pooka KASSERT(pd->pd_waitcount == 0);
277 1.1 pooka
278 1.1 pooka mutex_exit(&pd->pd_mtx);
279 1.1 pooka
280 1.1 pooka if (pd->pd_dev)
281 1.1 pooka devsw_detach(&pud_bdevsw /* XXX */, &pud_cdevsw);
282 1.1 pooka
283 1.1 pooka putter_detach(pd->pd_pi);
284 1.5 pooka
285 1.5 pooka mutex_destroy(&pd->pd_mtx);
286 1.5 pooka cv_destroy(&pd->pd_draincv);
287 1.5 pooka cv_destroy(&pd->pd_waitq_req_cv);
288 1.1 pooka kmem_free(pd, sizeof(struct pud_dev));
289 1.1 pooka
290 1.1 pooka return 0;
291 1.1 pooka }
292 1.1 pooka
293 1.1 pooka struct pud_dev *
294 1.1 pooka pud_dev2pud(dev_t dev)
295 1.1 pooka {
296 1.1 pooka struct pud_dev *pd;
297 1.1 pooka
298 1.1 pooka mutex_enter(&pud_mtx);
299 1.1 pooka LIST_FOREACH(pd, &pudlist, pd_entries)
300 1.1 pooka if (major(pd->pd_dev) == major(dev))
301 1.1 pooka break;
302 1.1 pooka mutex_exit(&pud_mtx);
303 1.1 pooka
304 1.1 pooka return pd;
305 1.1 pooka }
306 1.1 pooka
307 1.1 pooka /* Toss request to the device server and wait for result */
308 1.1 pooka int
309 1.1 pooka pud_request(dev_t dev, void *data, size_t dlen, int class, int type)
310 1.1 pooka {
311 1.1 pooka struct pud_touser put;
312 1.1 pooka struct pud_req *pdr = data;
313 1.1 pooka struct pud_dev *pd;
314 1.1 pooka
315 1.1 pooka pd = pud_dev2pud(dev);
316 1.1 pooka if (pd == NULL)
317 1.1 pooka return ENXIO;
318 1.1 pooka
319 1.1 pooka pdr->pdr_dev = dev;
320 1.1 pooka pdr->pdr_len = pdr->pdr_pth.pth_framelen = dlen;
321 1.1 pooka pdr->pdr_reqid = nextreq(pd);
322 1.1 pooka
323 1.1 pooka pdr->pdr_reqclass = class;
324 1.1 pooka pdr->pdr_reqtype = type;
325 1.1 pooka
326 1.1 pooka put.pt_pdr = pdr;
327 1.1 pooka cv_init(&put.pt_cv, "pudresp");
328 1.1 pooka
329 1.1 pooka mutex_enter(&pd->pd_mtx);
330 1.1 pooka pd->pd_waitcount++;
331 1.1 pooka
332 1.1 pooka TAILQ_INSERT_TAIL(&pd->pd_waitq_req, &put, pt_entries);
333 1.1 pooka putter_notify(pd->pd_pi);
334 1.1 pooka cv_broadcast(&pd->pd_waitq_req_cv);
335 1.1 pooka cv_wait(&put.pt_cv, &pd->pd_mtx);
336 1.1 pooka
337 1.1 pooka if (--pd->pd_waitcount == 0)
338 1.1 pooka cv_signal(&pd->pd_draincv);
339 1.1 pooka mutex_exit(&pd->pd_mtx);
340 1.6 pooka cv_destroy(&put.pt_cv);
341 1.1 pooka
342 1.1 pooka return pdr->pdr_rv;
343 1.1 pooka }
344 1.1 pooka
345 1.1 pooka /* Called from putter based on minor dev number */
346 1.1 pooka int
347 1.1 pooka pud_config(int fd, int flags, int fmt)
348 1.1 pooka {
349 1.1 pooka struct pud_dev *pd;
350 1.1 pooka
351 1.1 pooka pd = kmem_zalloc(sizeof(struct pud_dev), KM_SLEEP);
352 1.1 pooka pd->pd_pi = putter_attach(curlwp->l_proc->p_pid, fd, pd, &pud_putter);
353 1.1 pooka if (pd->pd_pi == NULL) {
354 1.1 pooka kmem_free(pd, sizeof(struct pud_dev));
355 1.1 pooka return ENOENT; /* XXX */
356 1.1 pooka }
357 1.1 pooka pd->pd_dev = NODEV;
358 1.1 pooka
359 1.1 pooka mutex_init(&pd->pd_mtx, MUTEX_DEFAULT, IPL_NONE);
360 1.1 pooka TAILQ_INIT(&pd->pd_waitq_req);
361 1.1 pooka TAILQ_INIT(&pd->pd_waitq_resp);
362 1.1 pooka cv_init(&pd->pd_waitq_req_cv, "pudreq");
363 1.1 pooka cv_init(&pd->pd_draincv, "pudrain");
364 1.1 pooka
365 1.1 pooka mutex_enter(&pud_mtx);
366 1.1 pooka LIST_INSERT_HEAD(&pudlist, pd, pd_entries);
367 1.1 pooka mutex_exit(&pud_mtx);
368 1.1 pooka
369 1.1 pooka return 0;
370 1.1 pooka }
371 1.1 pooka
372 1.1 pooka void
373 1.1 pooka pudattach()
374 1.1 pooka {
375 1.1 pooka int error;
376 1.1 pooka
377 1.1 pooka if ((error = putter_register(pud_config, PUTTER_MINOR_PUD)) != 0) {
378 1.1 pooka printf("pudattach: can't register to putter: %d\n", error);
379 1.1 pooka return;
380 1.1 pooka }
381 1.1 pooka mutex_init(&pud_mtx, MUTEX_DEFAULT, IPL_NONE);
382 1.1 pooka }
383