xref: /netbsd-src/sys/rump/dev/lib/libvirtio_ld/ld_at_virtio.c (revision 97f8debd624665fdeaf9373bf4602036de3fcd85)
1*97f8debdSpgoyette /*	$NetBSD: ld_at_virtio.c,v 1.5 2022/03/31 19:30:18 pgoyette Exp $	*/
2db494c87Spooka 
3db494c87Spooka /*
4db494c87Spooka  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
5db494c87Spooka  *
6db494c87Spooka  * Redistribution and use in source and binary forms, with or without
7db494c87Spooka  * modification, are permitted provided that the following conditions
8db494c87Spooka  * are met:
9db494c87Spooka  * 1. Redistributions of source code must retain the above copyright
10db494c87Spooka  *    notice, this list of conditions and the following disclaimer.
11db494c87Spooka  * 2. Redistributions in binary form must reproduce the above copyright
12db494c87Spooka  *    notice, this list of conditions and the following disclaimer in the
13db494c87Spooka  *    documentation and/or other materials provided with the distribution.
14db494c87Spooka  *
15db494c87Spooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16db494c87Spooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17db494c87Spooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18db494c87Spooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19db494c87Spooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20db494c87Spooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21db494c87Spooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22db494c87Spooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23db494c87Spooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24db494c87Spooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25db494c87Spooka  * SUCH DAMAGE.
26db494c87Spooka  */
27db494c87Spooka 
28db494c87Spooka #include <sys/cdefs.h>
29*97f8debdSpgoyette __KERNEL_RCSID(0, "$NetBSD: ld_at_virtio.c,v 1.5 2022/03/31 19:30:18 pgoyette Exp $");
30db494c87Spooka 
31db494c87Spooka #include <sys/param.h>
32db494c87Spooka #include <sys/conf.h>
33db494c87Spooka #include <sys/device.h>
34db494c87Spooka #include <sys/bus.h>
35db494c87Spooka #include <sys/stat.h>
36c67798ecSpooka #include <sys/disklabel.h>
37db494c87Spooka 
386bb51422Spooka #include <rump-sys/kern.h>
396bb51422Spooka #include <rump-sys/vfs.h>
40db494c87Spooka 
41db494c87Spooka #include "ioconf.c"
42db494c87Spooka 
RUMP_COMPONENT(RUMP_COMPONENT_DEV)43db494c87Spooka RUMP_COMPONENT(RUMP_COMPONENT_DEV)
44db494c87Spooka {
45*97f8debdSpgoyette 	extern const struct bdevsw ld_bdevsw;
46*97f8debdSpgoyette 	extern const struct cdevsw ld_cdevsw;
47*97f8debdSpgoyette 	devmajor_t bmaj = -1, cmaj = -1;
48*97f8debdSpgoyette 	int error;
49*97f8debdSpgoyette 
50*97f8debdSpgoyette 	if ((error = devsw_attach("ld", &ld_bdevsw, &bmaj,
51*97f8debdSpgoyette 	    &ld_cdevsw, &cmaj)) != 0)
52*97f8debdSpgoyette 		panic("cannot attach ld: %d", error);
53db494c87Spooka 
54a3692735Ssevan 	config_init_component(cfdriver_ioconf_ld_virtio,
55a3692735Ssevan 	    cfattach_ioconf_ld_virtio, cfdata_ioconf_ld_virtio);
56db494c87Spooka }
57db494c87Spooka 
58c67798ecSpooka /*
59c67798ecSpooka  * Pseudo-devfs.  Since creating device nodes is non-free, don't
60c67798ecSpooka  * speculatively create hundreds of them (= milliseconds slower
61c67798ecSpooka  * bootstrap).  Instead, after the probe is done, see which units
62c67798ecSpooka  * were found and create nodes only for them.
63c67798ecSpooka  */
RUMP_COMPONENT(RUMP_COMPONENT_POSTINIT)64c67798ecSpooka RUMP_COMPONENT(RUMP_COMPONENT_POSTINIT)
65db494c87Spooka {
66c67798ecSpooka 	int error, i;
67db494c87Spooka 
68c67798ecSpooka 	for (i = 0; i < 10; i++) {
69c67798ecSpooka 		char bbase[] = "/dev/ldX";
70c67798ecSpooka 		char rbase[] = "/dev/rldX";
71c67798ecSpooka 
72c67798ecSpooka 		if (device_lookup(&ld_cd, i) == NULL)
73c67798ecSpooka 			break;
74c67798ecSpooka 
75c67798ecSpooka 		bbase[sizeof(bbase)-2] = '0' + i;
76c67798ecSpooka 		rbase[sizeof(rbase)-2] = '0' + i;
77c67798ecSpooka 
78c67798ecSpooka 		if ((error = rump_vfs_makedevnodes(S_IFBLK, bbase, 'a',
79c67798ecSpooka 		    bmaj, DISKMINOR(i, 0), 5)) != 0)
80db494c87Spooka 			panic("cannot create cooked ld dev nodes: %d", error);
81c67798ecSpooka 		if ((error = rump_vfs_makedevnodes(S_IFCHR, rbase, 'a',
82c67798ecSpooka 		    cmaj, DISKMINOR(i, 0), 5)) != 0)
83db494c87Spooka 			panic("cannot create raw ld dev nodes: %d", error);
84db494c87Spooka 	}
85c67798ecSpooka }
86