xref: /openbsd-src/sys/dev/bio.c (revision 1ad61ae0a79a724d2d3ec69e69c8e1d1ff6b53a0)
1 /*	$OpenBSD: bio.c,v 1.18 2023/11/09 14:07:18 dlg 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 	default:
93 		bio = (struct bio *)addr;
94 		if (!bio_validate(bio->bio_cookie))
95 			return (ENOENT);
96 
97 		error = bio_delegate_ioctl(
98 		    (struct bio_mapping *)bio->bio_cookie, cmd, addr);
99 		break;
100 	}
101 
102 	return (0);
103 }
104 
105 int
106 bio_register(struct device *dev, int (*ioctl)(struct device *, u_long, caddr_t))
107 {
108 	struct bio_mapping *bm;
109 
110 	bm = malloc(sizeof *bm, M_DEVBUF, M_NOWAIT);
111 	if (bm == NULL)
112 		return (ENOMEM);
113 	bm->bm_dev = dev;
114 	bm->bm_ioctl = ioctl;
115 	LIST_INSERT_HEAD(&bios, bm, bm_link);
116 	return (0);
117 }
118 
119 void
120 bio_unregister(struct device *dev)
121 {
122 	struct bio_mapping *bm, *next;
123 
124 	for (bm = LIST_FIRST(&bios); bm != NULL; bm = next) {
125 		next = LIST_NEXT(bm, bm_link);
126 
127 		if (dev == bm->bm_dev) {
128 			LIST_REMOVE(bm, bm_link);
129 			free(bm, M_DEVBUF, sizeof(*bm));
130 		}
131 	}
132 }
133 
134 struct bio_mapping *
135 bio_lookup(char *name)
136 {
137 	struct bio_mapping *bm;
138 
139 	LIST_FOREACH(bm, &bios, bm_link)
140 		if (strcmp(name, bm->bm_dev->dv_xname) == 0)
141 			return (bm);
142 	return (NULL);
143 }
144 
145 int
146 bio_validate(void *cookie)
147 {
148 	struct bio_mapping *bm;
149 
150 	LIST_FOREACH(bm, &bios, bm_link)
151 		if (bm == cookie)
152 			return (1);
153 	return (0);
154 }
155 
156 int
157 bio_delegate_ioctl(struct bio_mapping *bm, u_long cmd, caddr_t addr)
158 {
159 	return (bm->bm_ioctl(bm->bm_dev, cmd, addr));
160 }
161 
162 void
163 bio_info(struct bio_status *bs, int print, const char *fmt, ...)
164 {
165 	va_list	ap;
166 
167 	va_start(ap, fmt);
168 	bio_status(bs, print, BIO_MSG_INFO, fmt, &ap);
169 	va_end(ap);
170 }
171 
172 void
173 bio_warn(struct bio_status *bs, int print, const char *fmt, ...)
174 {
175 	va_list	ap;
176 
177 	va_start(ap, fmt);
178 	bio_status(bs, print, BIO_MSG_WARN, fmt, &ap);
179 	va_end(ap);
180 }
181 
182 void
183 bio_error(struct bio_status *bs, int print, const char *fmt, ...)
184 {
185 	va_list	ap;
186 
187 	va_start(ap, fmt);
188 	bio_status(bs, print, BIO_MSG_ERROR, fmt, &ap);
189 	va_end(ap);
190 }
191 
192 void
193 bio_status_init(struct bio_status *bs, struct device *dv)
194 {
195 	bzero(bs, sizeof(struct bio_status));
196 
197 	bs->bs_status = BIO_STATUS_UNKNOWN;
198 
199 	strlcpy(bs->bs_controller, dv->dv_xname, sizeof(bs->bs_controller));
200 }
201 
202 void
203 bio_status(struct bio_status *bs, int print, int msg_type, const char *fmt,
204     va_list *ap)
205 {
206 	int idx;
207 
208 	if (bs->bs_msg_count >= BIO_MSG_COUNT) {
209 		printf("%s: insufficient message buffers\n", bs->bs_controller);
210 		return;
211 	}
212 
213 	idx = bs->bs_msg_count++;
214 
215 	bs->bs_msgs[idx].bm_type = msg_type;
216 	vsnprintf(bs->bs_msgs[idx].bm_msg, BIO_MSG_LEN, fmt, *ap);
217 
218 	if (print)
219 		printf("%s: %s\n", bs->bs_controller, bs->bs_msgs[idx].bm_msg);
220 }
221