190284Sobrien/*	$NetBSD: ld_at_virtio.c,v 1.5 2022/03/31 19:30:18 pgoyette Exp $	*/
290284Sobrien
3132743Skan/*
418334Speter * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
5132743Skan *
618334Speter * Redistribution and use in source and binary forms, with or without
7132743Skan * modification, are permitted provided that the following conditions
818334Speter * are met:
918334Speter * 1. Redistributions of source code must retain the above copyright
1018334Speter *    notice, this list of conditions and the following disclaimer.
1118334Speter * 2. Redistributions in binary form must reproduce the above copyright
12132743Skan *    notice, this list of conditions and the following disclaimer in the
1318334Speter *    documentation and/or other materials provided with the distribution.
1418334Speter *
1518334Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
1618334Speter * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1718334Speter * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18132743Skan * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1918334Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2090284Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2118334Speter * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2290284Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2351411Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2451411Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2590284Sobrien * SUCH DAMAGE.
2618334Speter */
2751411Sobrien
28132743Skan#include <sys/cdefs.h>
29132743Skan__KERNEL_RCSID(0, "$NetBSD: ld_at_virtio.c,v 1.5 2022/03/31 19:30:18 pgoyette Exp $");
3018334Speter
3190284Sobrien#include <sys/param.h>
3290284Sobrien#include <sys/conf.h>
3318334Speter#include <sys/device.h>
3418334Speter#include <sys/bus.h>
3518334Speter#include <sys/stat.h>
3618334Speter#include <sys/disklabel.h>
3718334Speter
3818334Speter#include <rump-sys/kern.h>
3918334Speter#include <rump-sys/vfs.h>
4018334Speter
4151411Sobrien#include "ioconf.c"
4218334Speter
4351411SobrienRUMP_COMPONENT(RUMP_COMPONENT_DEV)
4451411Sobrien{
4590284Sobrien	extern const struct bdevsw ld_bdevsw;
4651411Sobrien	extern const struct cdevsw ld_cdevsw;
4790284Sobrien	devmajor_t bmaj = -1, cmaj = -1;
4890284Sobrien	int error;
4990284Sobrien
5090284Sobrien	if ((error = devsw_attach("ld", &ld_bdevsw, &bmaj,
51117408Skan	    &ld_cdevsw, &cmaj)) != 0)
52132743Skan		panic("cannot attach ld: %d", error);
5318334Speter
5451411Sobrien	config_init_component(cfdriver_ioconf_ld_virtio,
5590284Sobrien	    cfattach_ioconf_ld_virtio, cfdata_ioconf_ld_virtio);
5651411Sobrien}
5718334Speter
58132743Skan/*
59132743Skan * Pseudo-devfs.  Since creating device nodes is non-free, don't
60132743Skan * speculatively create hundreds of them (= milliseconds slower
61132743Skan * bootstrap).  Instead, after the probe is done, see which units
62132743Skan * were found and create nodes only for them.
63132743Skan */
64132743SkanRUMP_COMPONENT(RUMP_COMPONENT_POSTINIT)
65132743Skan{
6690284Sobrien	int error, i;
67117408Skan
6890284Sobrien	for (i = 0; i < 10; i++) {
6990284Sobrien		char bbase[] = "/dev/ldX";
7090284Sobrien		char rbase[] = "/dev/rldX";
7190284Sobrien
7290284Sobrien		if (device_lookup(&ld_cd, i) == NULL)
73132743Skan			break;
7490284Sobrien
75132743Skan		bbase[sizeof(bbase)-2] = '0' + i;
7690284Sobrien		rbase[sizeof(rbase)-2] = '0' + i;
7790284Sobrien
7890284Sobrien		if ((error = rump_vfs_makedevnodes(S_IFBLK, bbase, 'a',
7990284Sobrien		    bmaj, DISKMINOR(i, 0), 5)) != 0)
8090284Sobrien			panic("cannot create cooked ld dev nodes: %d", error);
8190284Sobrien		if ((error = rump_vfs_makedevnodes(S_IFCHR, rbase, 'a',
8290284Sobrien		    cmaj, DISKMINOR(i, 0), 5)) != 0)
8390284Sobrien			panic("cannot create raw ld dev nodes: %d", error);
8490284Sobrien	}
8590284Sobrien}
8690284Sobrien