Home | History | Annotate | Line # | Download | only in x86
autoconf.c revision 1.6
      1 /*	$NetBSD: autoconf.c,v 1.6 2008/10/24 16:37:25 cegger Exp $	*/
      2 /*	NetBSD: autoconf.c,v 1.75 2003/12/30 12:33:22 pk Exp 	*/
      3 
      4 /*-
      5  * Copyright (c) 1990 The Regents of the University of California.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to Berkeley by
      9  * William Jolitz.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)autoconf.c	7.1 (Berkeley) 5/9/91
     36  */
     37 
     38 /*
     39  * Setup the system to run on the current machine.
     40  *
     41  * Configure() is called at boot time and initializes the vba
     42  * device tables and the memory controller monitoring.  Available
     43  * devices are determined (from possibilities mentioned in ioconf.c),
     44  * and the drivers are initialized.
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.6 2008/10/24 16:37:25 cegger Exp $");
     49 
     50 #include "opt_xen.h"
     51 #include "opt_compat_oldboot.h"
     52 #include "opt_multiprocessor.h"
     53 #include "opt_nfs_boot.h"
     54 #include "xennet_hypervisor.h"
     55 #include "xennet_xenbus.h"
     56 
     57 #include <sys/param.h>
     58 #include <sys/systm.h>
     59 #include <sys/buf.h>
     60 #include <sys/disklabel.h>
     61 #include <sys/disk.h>
     62 #include <sys/conf.h>
     63 #ifdef COMPAT_OLDBOOT
     64 #include <sys/reboot.h>
     65 #endif
     66 #include <sys/device.h>
     67 #include <sys/malloc.h>
     68 #include <sys/vnode.h>
     69 #include <sys/fcntl.h>
     70 #include <sys/dkio.h>
     71 #include <sys/proc.h>
     72 #include <sys/user.h>
     73 #include <sys/kauth.h>
     74 
     75 #ifdef NFS_BOOT_BOOTSTATIC
     76 #include <net/if.h>
     77 #include <net/if_ether.h>
     78 #include <netinet/in.h>
     79 #include <nfs/rpcv2.h>
     80 #include <nfs/nfsproto.h>
     81 #include <nfs/nfs.h>
     82 #include <nfs/nfsmount.h>
     83 #include <nfs/nfsdiskless.h>
     84 #include <xen/if_xennetvar.h>
     85 #endif
     86 
     87 #include <machine/pte.h>
     88 #include <machine/cpu.h>
     89 #include <machine/gdt.h>
     90 #include <machine/pcb.h>
     91 #include <machine/bootinfo.h>
     92 
     93 static void findroot(void);
     94 static int is_valid_disk(device_t);
     95 static void handle_wedges(device_t, int);
     96 
     97 struct disklist *x86_alldisks;
     98 int x86_ndisks;
     99 
    100 #include "bios32.h"
    101 #if NBIOS32 > 0
    102 #include <machine/bios32.h>
    103 #endif
    104 
    105 #include "opt_pcibios.h"
    106 #ifdef PCIBIOS
    107 #include <dev/pci/pcireg.h>
    108 #include <dev/pci/pcivar.h>
    109 #include <i386/pci/pcibios.h>
    110 #endif
    111 
    112 #include "opt_kvm86.h"
    113 #ifdef KVM86
    114 #include <machine/kvm86.h>
    115 #endif
    116 
    117 /*
    118  * Determine i/o configuration for a machine.
    119  */
    120 void
    121 cpu_configure(void)
    122 {
    123 
    124 	startrtclock();
    125 
    126 #if NBIOS32 > 0 && defined(DOM0OPS)
    127 #ifdef XEN3
    128 	if (xendomain_is_dom0())
    129 #endif
    130 		bios32_init();
    131 #endif /* NBIOS32 > 0 && DOM0OPS */
    132 #ifdef PCIBIOS
    133 	pcibios_init();
    134 #endif
    135 
    136 #ifdef KVM86
    137 	kvm86_init();
    138 #endif
    139 
    140 	if (config_rootfound("mainbus", NULL) == NULL)
    141 		panic("configure: mainbus not configured");
    142 
    143 #ifdef INTRDEBUG
    144 	intr_printconfig();
    145 #endif
    146 
    147 	/* resync cr0 after FPU configuration */
    148 	lwp0.l_addr->u_pcb.pcb_cr0 = rcr0();
    149 #ifdef MULTIPROCESSOR
    150 	/* propagate this to the idle pcb's. */
    151 	cpu_init_idle_lwps();
    152 #endif
    153 
    154 	spl0();
    155 }
    156 
    157 void
    158 cpu_rootconf(void)
    159 {
    160 	findroot();
    161 
    162 	if (booted_wedge) {
    163 		KASSERT(booted_device != NULL);
    164 		printf("boot device: %s (%s)\n",
    165 		    device_xname(booted_wedge), device_xname(booted_device));
    166 		setroot(booted_wedge, 0);
    167 	} else {
    168 		printf("boot device: %s\n",
    169 		    booted_device ? device_xname(booted_device) : "<unknown>");
    170 		setroot(booted_device, booted_partition);
    171 	}
    172 }
    173 
    174 
    175 /*
    176  * Attempt to find the device from which we were booted.
    177  * If we can do so, and not instructed not to do so,
    178  * change rootdev to correspond to the load device.
    179  */
    180 void
    181 findroot(void)
    182 {
    183 	device_t dv;
    184 	union xen_cmdline_parseinfo xcp;
    185 
    186 	if (booted_device)
    187 		return;
    188 
    189 	xen_parse_cmdline(XEN_PARSE_BOOTDEV, &xcp);
    190 
    191 	TAILQ_FOREACH(dv, &alldevs, dv_list) {
    192 		bool is_ifnet, is_disk;
    193 		const char *devname;
    194 
    195 		is_ifnet = (device_class(dv) == DV_IFNET);
    196 		is_disk = is_valid_disk(dv);
    197 		devname = device_xname(dv);
    198 
    199 		if (!is_ifnet && !is_disk)
    200 			continue;
    201 
    202 		if (is_disk && xcp.xcp_bootdev[0] == 0) {
    203 			handle_wedges(dv, 0);
    204 			break;
    205 		}
    206 
    207 		if (strncmp(xcp.xcp_bootdev, devname, strlen(devname)))
    208 			continue;
    209 
    210 		if (is_disk && strlen(xcp.xcp_bootdev) > strlen(devname)) {
    211 			booted_partition = toupper(
    212 				xcp.xcp_bootdev[strlen(devname)]) - 'A';
    213 		}
    214 
    215 		booted_device = dv;
    216 		break;
    217 	}
    218 }
    219 
    220 #include "pci.h"
    221 
    222 #include <dev/isa/isavar.h>
    223 #if NPCI > 0
    224 #include <dev/pci/pcivar.h>
    225 #endif
    226 
    227 
    228 #if defined(NFS_BOOT_BOOTSTATIC) && defined(DOM0OPS)
    229 static int
    230 dom0_bootstatic_callback(struct nfs_diskless *nd)
    231 {
    232 #if 0
    233 	struct ifnet *ifp = nd->nd_ifp;
    234 #endif
    235 	union xen_cmdline_parseinfo xcp;
    236 	struct sockaddr_in *sin;
    237 
    238 	memset(&xcp, 0, sizeof(xcp.xcp_netinfo));
    239 	xcp.xcp_netinfo.xi_ifno = 0; /* XXX first interface hardcoded */
    240 	xcp.xcp_netinfo.xi_root = nd->nd_root.ndm_host;
    241 	xen_parse_cmdline(XEN_PARSE_NETINFO, &xcp);
    242 
    243 	nd->nd_myip.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[0]);
    244 	nd->nd_gwip.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[2]);
    245 	nd->nd_mask.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[3]);
    246 
    247 	sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
    248 	memset((void *)sin, 0, sizeof(*sin));
    249 	sin->sin_len = sizeof(*sin);
    250 	sin->sin_family = AF_INET;
    251 	sin->sin_addr.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[1]);
    252 
    253 	if (nd->nd_myip.s_addr == 0)
    254 		return NFS_BOOTSTATIC_NOSTATIC;
    255 	else
    256 		return (NFS_BOOTSTATIC_HAS_MYIP|NFS_BOOTSTATIC_HAS_GWIP|
    257 		    NFS_BOOTSTATIC_HAS_MASK|NFS_BOOTSTATIC_HAS_SERVADDR|
    258 		    NFS_BOOTSTATIC_HAS_SERVER);
    259 }
    260 #endif
    261 
    262 void
    263 device_register(device_t dev, void *aux)
    264 {
    265 	/*
    266 	 * Handle network interfaces here, the attachment information is
    267 	 * not available driver independently later.
    268 	 * For disks, there is nothing useful available at attach time.
    269 	 */
    270 #if NXENNET_HYPERVISOR > 0 || NXENNET_XENBUS > 0 || defined(DOM0OPS)
    271 	if (device_class(dev) == DV_IFNET) {
    272 		union xen_cmdline_parseinfo xcp;
    273 
    274 		xen_parse_cmdline(XEN_PARSE_BOOTDEV, &xcp);
    275 		if (strncmp(xcp.xcp_bootdev, device_xname(dev),
    276 		    sizeof(xcp.xcp_bootdev)) == 0) {
    277 #ifdef NFS_BOOT_BOOTSTATIC
    278 #ifdef DOM0OPS
    279 			if (xendomain_is_privileged()) {
    280 				nfs_bootstatic_callback = dom0_bootstatic_callback;
    281 			} else
    282 #endif
    283 #if NXENNET_HYPERVISOR > 0 || NXENNET_XENBUS > 0
    284 			nfs_bootstatic_callback = xennet_bootstatic_callback;
    285 #endif
    286 #endif
    287 			goto found;
    288 		}
    289 	}
    290 #endif
    291 	if (device_class(dev) == DV_IFNET) {
    292 		struct btinfo_netif *bin = lookup_bootinfo(BTINFO_NETIF);
    293 		if (bin == NULL)
    294 			return;
    295 
    296 		/*
    297 		 * We don't check the driver name against the device name
    298 		 * passed by the boot ROM. The ROM should stay usable
    299 		 * if the driver gets obsoleted.
    300 		 * The physical attachment information (checked below)
    301 		 * must be sufficient to identify the device.
    302 		 */
    303 
    304 		if (bin->bus == BI_BUS_ISA &&
    305 		    device_is_a(device_parent(dev), "isa")) {
    306 			struct isa_attach_args *iaa = aux;
    307 
    308 			/* compare IO base address */
    309 			/* XXXJRT what about multiple I/O addrs? */
    310 			if (iaa->ia_nio > 0 &&
    311 			    bin->addr.iobase == iaa->ia_io[0].ir_addr)
    312 				goto found;
    313 		}
    314 #if NPCI > 0
    315 		if (bin->bus == BI_BUS_PCI &&
    316 		    device_is_a(device_parent(dev), "pci")) {
    317 			struct pci_attach_args *paa = aux;
    318 			int b, d, f;
    319 
    320 			/*
    321 			 * Calculate BIOS representation of:
    322 			 *
    323 			 *	<bus,device,function>
    324 			 *
    325 			 * and compare.
    326 			 */
    327 			pci_decompose_tag(paa->pa_pc, paa->pa_tag, &b, &d, &f);
    328 			if (bin->addr.tag == ((b << 8) | (d << 3) | f))
    329 				goto found;
    330 		}
    331 #endif
    332 	}
    333 	return;
    334 
    335 found:
    336 	if (booted_device) {
    337 		/* XXX should be a "panic()" */
    338 		printf("warning: double match for boot device (%s, %s)\n",
    339 		    device_xname(booted_device), device_xname(dev));
    340 		return;
    341 	}
    342 	booted_device = dev;
    343 }
    344 
    345 static void
    346 handle_wedges(device_t dv, int par)
    347 {
    348 	if (config_handle_wedges(dv, par) == 0)
    349 		return;
    350 	booted_device = dv;
    351 	booted_partition = par;
    352 }
    353 
    354 static int
    355 is_valid_disk(device_t dv)
    356 {
    357 
    358 	if (device_class(dv) != DV_DISK)
    359 		return (0);
    360 
    361 	return (device_is_a(dv, "dk") ||
    362 		device_is_a(dv, "sd") ||
    363 		device_is_a(dv, "wd") ||
    364 		device_is_a(dv, "ld") ||
    365 		device_is_a(dv, "ed") ||
    366 		device_is_a(dv, "xbd"));
    367 }
    368