xref: /openbsd-src/sys/dev/bio.c (revision ac9b4aacc1da35008afea06a5d23c2f2dea9b93e)
1 /*	$OpenBSD: bio.c,v 1.14 2012/01/20 12:38:20 jsing Exp $	*/
2 
3 /*
4  * Copyright (c) 2002 Niklas Hallqvist.  All rights reserved.
5  * Copyright (c) 2012 Joel Sing <jsing@openbsd.org>.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /* A device controller ioctl tunnelling device.  */
29 
30 #include <sys/param.h>
31 #include <sys/device.h>
32 #include <sys/ioctl.h>
33 #include <sys/malloc.h>
34 #include <sys/queue.h>
35 #include <sys/systm.h>
36 
37 #include <dev/biovar.h>
38 
39 struct bio_mapping {
40 	LIST_ENTRY(bio_mapping) bm_link;
41 	struct device *bm_dev;
42 	int (*bm_ioctl)(struct device *, u_long, caddr_t);
43 };
44 
45 LIST_HEAD(, bio_mapping) bios = LIST_HEAD_INITIALIZER(bios);
46 
47 void	bioattach(int);
48 int	bioclose(dev_t, int, int, struct proc *);
49 int	bioioctl(dev_t, u_long, caddr_t, int, struct proc *);
50 int	bioopen(dev_t, int, int, struct proc *);
51 
52 int	bio_delegate_ioctl(struct bio_mapping *, u_long, caddr_t);
53 struct	bio_mapping *bio_lookup(char *);
54 int	bio_validate(void *);
55 
56 void
57 bioattach(int nunits)
58 {
59 }
60 
61 int
62 bioopen(dev_t dev, int flags, int mode, struct proc *p)
63 {
64 	return (0);
65 }
66 
67 int
68 bioclose(dev_t dev, int flags, int mode, struct proc *p)
69 {
70 	return (0);
71 }
72 
73 int
74 bioioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
75 {
76 	struct bio_locate *locate;
77 	struct bio *bio;
78 	char name[16];
79 	int error;
80 
81 	switch (cmd) {
82 	case BIOCLOCATE:
83 		locate = (struct bio_locate *)addr;
84 		error = copyinstr(locate->bl_name, name, sizeof name, NULL);
85 		if (error != 0)
86 			return (error);
87 		locate->bl_bio.bio_cookie = bio_lookup(name);
88 		if (locate->bl_bio.bio_cookie == NULL)
89 			return (ENOENT);
90 		break;
91 
92 	case BIOCINQ:
93 	case BIOCDISK:
94 	case BIOCVOL:
95 	case BIOCALARM:
96 	case BIOCBLINK:
97 	case BIOCSETSTATE:
98 	case BIOCCREATERAID:
99 	case BIOCDELETERAID:
100 	case BIOCDISCIPLINE:
101 		bio = (struct bio *)addr;
102 		if (!bio_validate(bio->bio_cookie))
103 			return (ENOENT);
104 		return (bio_delegate_ioctl(
105 		    (struct bio_mapping *)bio->bio_cookie, cmd, addr));
106 
107 	default:
108 		return (ENXIO);
109 	}
110 	return (0);
111 }
112 
113 int
114 bio_register(struct device *dev, int (*ioctl)(struct device *, u_long, caddr_t))
115 {
116 	struct bio_mapping *bm;
117 
118 	bm = malloc(sizeof *bm, M_DEVBUF, M_NOWAIT);
119 	if (bm == NULL)
120 		return (ENOMEM);
121 	bm->bm_dev = dev;
122 	bm->bm_ioctl = ioctl;
123 	LIST_INSERT_HEAD(&bios, bm, bm_link);
124 	return (0);
125 }
126 
127 void
128 bio_unregister(struct device *dev)
129 {
130 	struct bio_mapping *bm, *next;
131 
132 	for (bm = LIST_FIRST(&bios); bm != NULL; bm = next) {
133 		next = LIST_NEXT(bm, bm_link);
134 
135 		if (dev == bm->bm_dev) {
136 			LIST_REMOVE(bm, bm_link);
137 			free(bm, M_DEVBUF);
138 		}
139 	}
140 }
141 
142 struct bio_mapping *
143 bio_lookup(char *name)
144 {
145 	struct bio_mapping *bm;
146 
147 	LIST_FOREACH(bm, &bios, bm_link)
148 		if (strcmp(name, bm->bm_dev->dv_xname) == 0)
149 			return (bm);
150 	return (NULL);
151 }
152 
153 int
154 bio_validate(void *cookie)
155 {
156 	struct bio_mapping *bm;
157 
158 	LIST_FOREACH(bm, &bios, bm_link)
159 		if (bm == cookie)
160 			return (1);
161 	return (0);
162 }
163 
164 int
165 bio_delegate_ioctl(struct bio_mapping *bm, u_long cmd, caddr_t addr)
166 {
167 	return (bm->bm_ioctl(bm->bm_dev, cmd, addr));
168 }
169 
170 void
171 bio_info(struct bio_status *bs, int print, const char *fmt, ...)
172 {
173 	va_list	ap;
174 
175 	va_start(ap, fmt);
176 	bio_status(bs, print, BIO_MSG_INFO, fmt, &ap);
177 	va_end(ap);
178 }
179 
180 void
181 bio_warn(struct bio_status *bs, int print, const char *fmt, ...)
182 {
183 	va_list	ap;
184 
185 	va_start(ap, fmt);
186 	bio_status(bs, print, BIO_MSG_WARN, fmt, &ap);
187 	va_end(ap);
188 }
189 
190 void
191 bio_error(struct bio_status *bs, int print, const char *fmt, ...)
192 {
193 	va_list	ap;
194 
195 	va_start(ap, fmt);
196 	bio_status(bs, print, BIO_MSG_ERROR, fmt, &ap);
197 	va_end(ap);
198 }
199 
200 void
201 bio_status_init(struct bio_status *bs, struct device *dv)
202 {
203 	bzero(bs, sizeof(struct bio_status));
204 
205 	bs->bs_status = BIO_STATUS_UNKNOWN;
206 
207 	strlcpy(bs->bs_controller, dv->dv_xname, sizeof(bs->bs_controller));
208 }
209 
210 void
211 bio_status(struct bio_status *bs, int print, int msg_type, const char *fmt,
212     va_list *ap)
213 {
214 	int idx;
215 
216 	if (bs->bs_msg_count >= BIO_MSG_COUNT) {
217 		printf("%s: insufficient message buffers\n", bs->bs_controller);
218 		return;
219 	}
220 
221 	idx = bs->bs_msg_count++;
222 
223 	bs->bs_msgs[idx].bm_type = msg_type;
224 	vsnprintf(bs->bs_msgs[idx].bm_msg, BIO_MSG_LEN, fmt, *ap);
225 
226 	if (print)
227 		printf("%s: %s\n", bs->bs_controller, bs->bs_msgs[idx].bm_msg);
228 }
229