Home | History | Annotate | Line # | Download | only in x86
autoconf.c revision 1.2.4.2
      1  1.2.4.2  ad /*	$NetBSD: autoconf.c,v 1.2.4.2 2007/12/03 19:04:38 ad Exp $	*/
      2  1.2.4.2  ad /*	NetBSD: autoconf.c,v 1.75 2003/12/30 12:33:22 pk Exp 	*/
      3  1.2.4.2  ad 
      4  1.2.4.2  ad /*-
      5  1.2.4.2  ad  * Copyright (c) 1990 The Regents of the University of California.
      6  1.2.4.2  ad  * All rights reserved.
      7  1.2.4.2  ad  *
      8  1.2.4.2  ad  * This code is derived from software contributed to Berkeley by
      9  1.2.4.2  ad  * William Jolitz.
     10  1.2.4.2  ad  *
     11  1.2.4.2  ad  * Redistribution and use in source and binary forms, with or without
     12  1.2.4.2  ad  * modification, are permitted provided that the following conditions
     13  1.2.4.2  ad  * are met:
     14  1.2.4.2  ad  * 1. Redistributions of source code must retain the above copyright
     15  1.2.4.2  ad  *    notice, this list of conditions and the following disclaimer.
     16  1.2.4.2  ad  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.2.4.2  ad  *    notice, this list of conditions and the following disclaimer in the
     18  1.2.4.2  ad  *    documentation and/or other materials provided with the distribution.
     19  1.2.4.2  ad  * 3. Neither the name of the University nor the names of its contributors
     20  1.2.4.2  ad  *    may be used to endorse or promote products derived from this software
     21  1.2.4.2  ad  *    without specific prior written permission.
     22  1.2.4.2  ad  *
     23  1.2.4.2  ad  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.2.4.2  ad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.2.4.2  ad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.2.4.2  ad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.2.4.2  ad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.2.4.2  ad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.2.4.2  ad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.2.4.2  ad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.2.4.2  ad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.2.4.2  ad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.2.4.2  ad  * SUCH DAMAGE.
     34  1.2.4.2  ad  *
     35  1.2.4.2  ad  *	@(#)autoconf.c	7.1 (Berkeley) 5/9/91
     36  1.2.4.2  ad  */
     37  1.2.4.2  ad 
     38  1.2.4.2  ad /*
     39  1.2.4.2  ad  * Setup the system to run on the current machine.
     40  1.2.4.2  ad  *
     41  1.2.4.2  ad  * Configure() is called at boot time and initializes the vba
     42  1.2.4.2  ad  * device tables and the memory controller monitoring.  Available
     43  1.2.4.2  ad  * devices are determined (from possibilities mentioned in ioconf.c),
     44  1.2.4.2  ad  * and the drivers are initialized.
     45  1.2.4.2  ad  */
     46  1.2.4.2  ad 
     47  1.2.4.2  ad #include <sys/cdefs.h>
     48  1.2.4.2  ad __KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.2.4.2 2007/12/03 19:04:38 ad Exp $");
     49  1.2.4.2  ad 
     50  1.2.4.2  ad #include "opt_xen.h"
     51  1.2.4.2  ad #include "opt_compat_oldboot.h"
     52  1.2.4.2  ad #include "opt_multiprocessor.h"
     53  1.2.4.2  ad #include "opt_nfs_boot.h"
     54  1.2.4.2  ad #include "xennet_hypervisor.h"
     55  1.2.4.2  ad #include "xennet_xenbus.h"
     56  1.2.4.2  ad 
     57  1.2.4.2  ad #include <sys/param.h>
     58  1.2.4.2  ad #include <sys/systm.h>
     59  1.2.4.2  ad #include <sys/buf.h>
     60  1.2.4.2  ad #include <sys/disklabel.h>
     61  1.2.4.2  ad #include <sys/disk.h>
     62  1.2.4.2  ad #include <sys/conf.h>
     63  1.2.4.2  ad #ifdef COMPAT_OLDBOOT
     64  1.2.4.2  ad #include <sys/reboot.h>
     65  1.2.4.2  ad #endif
     66  1.2.4.2  ad #include <sys/device.h>
     67  1.2.4.2  ad #include <sys/malloc.h>
     68  1.2.4.2  ad #include <sys/vnode.h>
     69  1.2.4.2  ad #include <sys/fcntl.h>
     70  1.2.4.2  ad #include <sys/dkio.h>
     71  1.2.4.2  ad #include <sys/proc.h>
     72  1.2.4.2  ad #include <sys/user.h>
     73  1.2.4.2  ad #include <sys/kauth.h>
     74  1.2.4.2  ad 
     75  1.2.4.2  ad #ifdef NFS_BOOT_BOOTSTATIC
     76  1.2.4.2  ad #include <net/if.h>
     77  1.2.4.2  ad #include <net/if_ether.h>
     78  1.2.4.2  ad #include <netinet/in.h>
     79  1.2.4.2  ad #include <nfs/rpcv2.h>
     80  1.2.4.2  ad #include <nfs/nfsproto.h>
     81  1.2.4.2  ad #include <nfs/nfs.h>
     82  1.2.4.2  ad #include <nfs/nfsmount.h>
     83  1.2.4.2  ad #include <nfs/nfsdiskless.h>
     84  1.2.4.2  ad #include <xen/if_xennetvar.h>
     85  1.2.4.2  ad #endif
     86  1.2.4.2  ad 
     87  1.2.4.2  ad #include <machine/pte.h>
     88  1.2.4.2  ad #include <machine/cpu.h>
     89  1.2.4.2  ad #include <machine/gdt.h>
     90  1.2.4.2  ad #include <machine/pcb.h>
     91  1.2.4.2  ad #include <machine/bootinfo.h>
     92  1.2.4.2  ad 
     93  1.2.4.2  ad static void findroot(void);
     94  1.2.4.2  ad static int is_valid_disk(struct device *);
     95  1.2.4.2  ad static void handle_wedges(struct device *, int);
     96  1.2.4.2  ad 
     97  1.2.4.2  ad struct disklist *x86_alldisks;
     98  1.2.4.2  ad int x86_ndisks;
     99  1.2.4.2  ad 
    100  1.2.4.2  ad #include "bios32.h"
    101  1.2.4.2  ad #if NBIOS32 > 0
    102  1.2.4.2  ad #include <machine/bios32.h>
    103  1.2.4.2  ad #endif
    104  1.2.4.2  ad 
    105  1.2.4.2  ad #include "opt_pcibios.h"
    106  1.2.4.2  ad #ifdef PCIBIOS
    107  1.2.4.2  ad #include <dev/pci/pcireg.h>
    108  1.2.4.2  ad #include <dev/pci/pcivar.h>
    109  1.2.4.2  ad #include <i386/pci/pcibios.h>
    110  1.2.4.2  ad #endif
    111  1.2.4.2  ad 
    112  1.2.4.2  ad #include "opt_kvm86.h"
    113  1.2.4.2  ad #ifdef KVM86
    114  1.2.4.2  ad #include <machine/kvm86.h>
    115  1.2.4.2  ad #endif
    116  1.2.4.2  ad 
    117  1.2.4.2  ad /*
    118  1.2.4.2  ad  * Determine i/o configuration for a machine.
    119  1.2.4.2  ad  */
    120  1.2.4.2  ad void
    121  1.2.4.2  ad cpu_configure(void)
    122  1.2.4.2  ad {
    123  1.2.4.2  ad 
    124  1.2.4.2  ad 	startrtclock();
    125  1.2.4.2  ad 
    126  1.2.4.2  ad #if NBIOS32 > 0 && defined(DOM0OPS)
    127  1.2.4.2  ad #ifdef XEN3
    128  1.2.4.2  ad 	if (xen_start_info.flags & SIF_INITDOMAIN)
    129  1.2.4.2  ad #endif
    130  1.2.4.2  ad 		bios32_init();
    131  1.2.4.2  ad #endif /* NBIOS32 > 0 && DOM0OPS */
    132  1.2.4.2  ad #ifdef PCIBIOS
    133  1.2.4.2  ad 	pcibios_init();
    134  1.2.4.2  ad #endif
    135  1.2.4.2  ad 
    136  1.2.4.2  ad #ifdef KVM86
    137  1.2.4.2  ad 	kvm86_init();
    138  1.2.4.2  ad #endif
    139  1.2.4.2  ad 
    140  1.2.4.2  ad 	if (config_rootfound("mainbus", NULL) == NULL)
    141  1.2.4.2  ad 		panic("configure: mainbus not configured");
    142  1.2.4.2  ad 
    143  1.2.4.2  ad #ifdef INTRDEBUG
    144  1.2.4.2  ad 	intr_printconfig();
    145  1.2.4.2  ad #endif
    146  1.2.4.2  ad 
    147  1.2.4.2  ad 	/* resync cr0 after FPU configuration */
    148  1.2.4.2  ad 	lwp0.l_addr->u_pcb.pcb_cr0 = rcr0();
    149  1.2.4.2  ad #ifdef MULTIPROCESSOR
    150  1.2.4.2  ad 	/* propagate this to the idle pcb's. */
    151  1.2.4.2  ad 	cpu_init_idle_lwps();
    152  1.2.4.2  ad #endif
    153  1.2.4.2  ad 
    154  1.2.4.2  ad 	spl0();
    155  1.2.4.2  ad }
    156  1.2.4.2  ad 
    157  1.2.4.2  ad void
    158  1.2.4.2  ad cpu_rootconf(void)
    159  1.2.4.2  ad {
    160  1.2.4.2  ad 	findroot();
    161  1.2.4.2  ad 
    162  1.2.4.2  ad 	if (booted_wedge) {
    163  1.2.4.2  ad 		KASSERT(booted_device != NULL);
    164  1.2.4.2  ad 		printf("boot device: %s (%s)\n",
    165  1.2.4.2  ad 		    booted_wedge->dv_xname, booted_device->dv_xname);
    166  1.2.4.2  ad 		setroot(booted_wedge, 0);
    167  1.2.4.2  ad 	} else {
    168  1.2.4.2  ad 		printf("boot device: %s\n",
    169  1.2.4.2  ad 		    booted_device ? booted_device->dv_xname : "<unknown>");
    170  1.2.4.2  ad 		setroot(booted_device, booted_partition);
    171  1.2.4.2  ad 	}
    172  1.2.4.2  ad }
    173  1.2.4.2  ad 
    174  1.2.4.2  ad 
    175  1.2.4.2  ad /*
    176  1.2.4.2  ad  * Attempt to find the device from which we were booted.
    177  1.2.4.2  ad  * If we can do so, and not instructed not to do so,
    178  1.2.4.2  ad  * change rootdev to correspond to the load device.
    179  1.2.4.2  ad  */
    180  1.2.4.2  ad void
    181  1.2.4.2  ad findroot(void)
    182  1.2.4.2  ad {
    183  1.2.4.2  ad 	struct device *dv;
    184  1.2.4.2  ad 	union xen_cmdline_parseinfo xcp;
    185  1.2.4.2  ad 
    186  1.2.4.2  ad 	if (booted_device)
    187  1.2.4.2  ad 		return;
    188  1.2.4.2  ad 
    189  1.2.4.2  ad 	xen_parse_cmdline(XEN_PARSE_BOOTDEV, &xcp);
    190  1.2.4.2  ad 
    191  1.2.4.2  ad 	TAILQ_FOREACH(dv, &alldevs, dv_list) {
    192  1.2.4.2  ad 		if (is_valid_disk(dv) == 0)
    193  1.2.4.2  ad 			continue;
    194  1.2.4.2  ad 
    195  1.2.4.2  ad 		if (xcp.xcp_bootdev[0] == 0) {
    196  1.2.4.2  ad 			handle_wedges(dv, 0);
    197  1.2.4.2  ad 			break;
    198  1.2.4.2  ad 		}
    199  1.2.4.2  ad 
    200  1.2.4.2  ad 		if (strncmp(xcp.xcp_bootdev, dv->dv_xname,
    201  1.2.4.2  ad 		    strlen(dv->dv_xname)))
    202  1.2.4.2  ad 			continue;
    203  1.2.4.2  ad 
    204  1.2.4.2  ad 		if (strlen(xcp.xcp_bootdev) > strlen(dv->dv_xname)) {
    205  1.2.4.2  ad 			booted_partition = toupper(
    206  1.2.4.2  ad 				xcp.xcp_bootdev[strlen(dv->dv_xname)]) - 'A';
    207  1.2.4.2  ad 		}
    208  1.2.4.2  ad 
    209  1.2.4.2  ad 		booted_device = dv;
    210  1.2.4.2  ad 		break;
    211  1.2.4.2  ad 	}
    212  1.2.4.2  ad }
    213  1.2.4.2  ad 
    214  1.2.4.2  ad #include "pci.h"
    215  1.2.4.2  ad 
    216  1.2.4.2  ad #include <dev/isa/isavar.h>
    217  1.2.4.2  ad #if NPCI > 0
    218  1.2.4.2  ad #include <dev/pci/pcivar.h>
    219  1.2.4.2  ad #endif
    220  1.2.4.2  ad 
    221  1.2.4.2  ad 
    222  1.2.4.2  ad #if defined(NFS_BOOT_BOOTSTATIC) && defined(DOM0OPS)
    223  1.2.4.2  ad static int
    224  1.2.4.2  ad dom0_bootstatic_callback(struct nfs_diskless *nd)
    225  1.2.4.2  ad {
    226  1.2.4.2  ad #if 0
    227  1.2.4.2  ad 	struct ifnet *ifp = nd->nd_ifp;
    228  1.2.4.2  ad #endif
    229  1.2.4.2  ad 	union xen_cmdline_parseinfo xcp;
    230  1.2.4.2  ad 	struct sockaddr_in *sin;
    231  1.2.4.2  ad 
    232  1.2.4.2  ad 	memset(&xcp, 0, sizeof(xcp.xcp_netinfo));
    233  1.2.4.2  ad 	xcp.xcp_netinfo.xi_ifno = 0; /* XXX first interface hardcoded */
    234  1.2.4.2  ad 	xcp.xcp_netinfo.xi_root = nd->nd_root.ndm_host;
    235  1.2.4.2  ad 	xen_parse_cmdline(XEN_PARSE_NETINFO, &xcp);
    236  1.2.4.2  ad 
    237  1.2.4.2  ad 	nd->nd_myip.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[0]);
    238  1.2.4.2  ad 	nd->nd_gwip.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[2]);
    239  1.2.4.2  ad 	nd->nd_mask.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[3]);
    240  1.2.4.2  ad 
    241  1.2.4.2  ad 	sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
    242  1.2.4.2  ad 	memset((void *)sin, 0, sizeof(*sin));
    243  1.2.4.2  ad 	sin->sin_len = sizeof(*sin);
    244  1.2.4.2  ad 	sin->sin_family = AF_INET;
    245  1.2.4.2  ad 	sin->sin_addr.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[1]);
    246  1.2.4.2  ad 
    247  1.2.4.2  ad 	if (nd->nd_myip.s_addr == 0)
    248  1.2.4.2  ad 		return NFS_BOOTSTATIC_NOSTATIC;
    249  1.2.4.2  ad 	else
    250  1.2.4.2  ad 		return (NFS_BOOTSTATIC_HAS_MYIP|NFS_BOOTSTATIC_HAS_GWIP|
    251  1.2.4.2  ad 		    NFS_BOOTSTATIC_HAS_MASK|NFS_BOOTSTATIC_HAS_SERVADDR|
    252  1.2.4.2  ad 		    NFS_BOOTSTATIC_HAS_SERVER);
    253  1.2.4.2  ad }
    254  1.2.4.2  ad #endif
    255  1.2.4.2  ad 
    256  1.2.4.2  ad void
    257  1.2.4.2  ad device_register(struct device *dev, void *aux)
    258  1.2.4.2  ad {
    259  1.2.4.2  ad 	/*
    260  1.2.4.2  ad 	 * Handle network interfaces here, the attachment information is
    261  1.2.4.2  ad 	 * not available driver independently later.
    262  1.2.4.2  ad 	 * For disks, there is nothing useful available at attach time.
    263  1.2.4.2  ad 	 */
    264  1.2.4.2  ad #if NXENNET_HYPERVISOR > 0 || NXENNET_XENBUS > 0 || defined(DOM0OPS)
    265  1.2.4.2  ad 	if (device_class(dev) == DV_IFNET) {
    266  1.2.4.2  ad 		union xen_cmdline_parseinfo xcp;
    267  1.2.4.2  ad 
    268  1.2.4.2  ad 		xen_parse_cmdline(XEN_PARSE_BOOTDEV, &xcp);
    269  1.2.4.2  ad 		if (strncmp(xcp.xcp_bootdev, dev->dv_xname, 16) == 0) {
    270  1.2.4.2  ad #ifdef NFS_BOOT_BOOTSTATIC
    271  1.2.4.2  ad #ifdef DOM0OPS
    272  1.2.4.2  ad 			if (xen_start_info.flags & SIF_PRIVILEGED) {
    273  1.2.4.2  ad 				nfs_bootstatic_callback = dom0_bootstatic_callback;
    274  1.2.4.2  ad 			} else
    275  1.2.4.2  ad #endif
    276  1.2.4.2  ad #if NXENNET_HYPERVISOR > 0 || NXENNET_XENBUS > 0
    277  1.2.4.2  ad 			nfs_bootstatic_callback = xennet_bootstatic_callback;
    278  1.2.4.2  ad #endif
    279  1.2.4.2  ad #endif
    280  1.2.4.2  ad 			goto found;
    281  1.2.4.2  ad 		}
    282  1.2.4.2  ad 	}
    283  1.2.4.2  ad #endif
    284  1.2.4.2  ad 	if (device_class(dev) == DV_IFNET) {
    285  1.2.4.2  ad 		struct btinfo_netif *bin = lookup_bootinfo(BTINFO_NETIF);
    286  1.2.4.2  ad 		if (bin == NULL)
    287  1.2.4.2  ad 			return;
    288  1.2.4.2  ad 
    289  1.2.4.2  ad 		/*
    290  1.2.4.2  ad 		 * We don't check the driver name against the device name
    291  1.2.4.2  ad 		 * passed by the boot ROM. The ROM should stay usable
    292  1.2.4.2  ad 		 * if the driver gets obsoleted.
    293  1.2.4.2  ad 		 * The physical attachment information (checked below)
    294  1.2.4.2  ad 		 * must be sufficient to identify the device.
    295  1.2.4.2  ad 		 */
    296  1.2.4.2  ad 
    297  1.2.4.2  ad 		if (bin->bus == BI_BUS_ISA &&
    298  1.2.4.2  ad 		    device_is_a(device_parent(dev), "isa")) {
    299  1.2.4.2  ad 			struct isa_attach_args *iaa = aux;
    300  1.2.4.2  ad 
    301  1.2.4.2  ad 			/* compare IO base address */
    302  1.2.4.2  ad 			/* XXXJRT what about multiple I/O addrs? */
    303  1.2.4.2  ad 			if (iaa->ia_nio > 0 &&
    304  1.2.4.2  ad 			    bin->addr.iobase == iaa->ia_io[0].ir_addr)
    305  1.2.4.2  ad 				goto found;
    306  1.2.4.2  ad 		}
    307  1.2.4.2  ad #if NPCI > 0
    308  1.2.4.2  ad 		if (bin->bus == BI_BUS_PCI &&
    309  1.2.4.2  ad 		    device_is_a(device_parent(dev), "pci")) {
    310  1.2.4.2  ad 			struct pci_attach_args *paa = aux;
    311  1.2.4.2  ad 			int b, d, f;
    312  1.2.4.2  ad 
    313  1.2.4.2  ad 			/*
    314  1.2.4.2  ad 			 * Calculate BIOS representation of:
    315  1.2.4.2  ad 			 *
    316  1.2.4.2  ad 			 *	<bus,device,function>
    317  1.2.4.2  ad 			 *
    318  1.2.4.2  ad 			 * and compare.
    319  1.2.4.2  ad 			 */
    320  1.2.4.2  ad 			pci_decompose_tag(paa->pa_pc, paa->pa_tag, &b, &d, &f);
    321  1.2.4.2  ad 			if (bin->addr.tag == ((b << 8) | (d << 3) | f))
    322  1.2.4.2  ad 				goto found;
    323  1.2.4.2  ad 		}
    324  1.2.4.2  ad #endif
    325  1.2.4.2  ad 	}
    326  1.2.4.2  ad 	return;
    327  1.2.4.2  ad 
    328  1.2.4.2  ad found:
    329  1.2.4.2  ad 	if (booted_device) {
    330  1.2.4.2  ad 		/* XXX should be a "panic()" */
    331  1.2.4.2  ad 		printf("warning: double match for boot device (%s, %s)\n",
    332  1.2.4.2  ad 		    booted_device->dv_xname, dev->dv_xname);
    333  1.2.4.2  ad 		return;
    334  1.2.4.2  ad 	}
    335  1.2.4.2  ad 	booted_device = dev;
    336  1.2.4.2  ad }
    337  1.2.4.2  ad 
    338  1.2.4.2  ad static void
    339  1.2.4.2  ad handle_wedges(struct device *dv, int par)
    340  1.2.4.2  ad {
    341  1.2.4.2  ad 	if (config_handle_wedges(dv, par) == 0)
    342  1.2.4.2  ad 		return;
    343  1.2.4.2  ad 	booted_device = dv;
    344  1.2.4.2  ad 	booted_partition = par;
    345  1.2.4.2  ad }
    346  1.2.4.2  ad 
    347  1.2.4.2  ad static int
    348  1.2.4.2  ad is_valid_disk(struct device *dv)
    349  1.2.4.2  ad {
    350  1.2.4.2  ad 
    351  1.2.4.2  ad 	if (device_class(dv) != DV_DISK)
    352  1.2.4.2  ad 		return (0);
    353  1.2.4.2  ad 
    354  1.2.4.2  ad 	return (device_is_a(dv, "dk") ||
    355  1.2.4.2  ad 		device_is_a(dv, "sd") ||
    356  1.2.4.2  ad 		device_is_a(dv, "wd") ||
    357  1.2.4.2  ad 		device_is_a(dv, "ld") ||
    358  1.2.4.2  ad 		device_is_a(dv, "ed") ||
    359  1.2.4.2  ad 		device_is_a(dv, "xbd"));
    360  1.2.4.2  ad }
    361  1.2.4.2  ad /*	$NetBSD: autoconf.c,v 1.2.4.2 2007/12/03 19:04:38 ad Exp $	*/
    362  1.2.4.2  ad /*	NetBSD: autoconf.c,v 1.75 2003/12/30 12:33:22 pk Exp 	*/
    363  1.2.4.2  ad 
    364  1.2.4.2  ad /*-
    365  1.2.4.2  ad  * Copyright (c) 1990 The Regents of the University of California.
    366  1.2.4.2  ad  * All rights reserved.
    367  1.2.4.2  ad  *
    368  1.2.4.2  ad  * This code is derived from software contributed to Berkeley by
    369  1.2.4.2  ad  * William Jolitz.
    370  1.2.4.2  ad  *
    371  1.2.4.2  ad  * Redistribution and use in source and binary forms, with or without
    372  1.2.4.2  ad  * modification, are permitted provided that the following conditions
    373  1.2.4.2  ad  * are met:
    374  1.2.4.2  ad  * 1. Redistributions of source code must retain the above copyright
    375  1.2.4.2  ad  *    notice, this list of conditions and the following disclaimer.
    376  1.2.4.2  ad  * 2. Redistributions in binary form must reproduce the above copyright
    377  1.2.4.2  ad  *    notice, this list of conditions and the following disclaimer in the
    378  1.2.4.2  ad  *    documentation and/or other materials provided with the distribution.
    379  1.2.4.2  ad  * 3. Neither the name of the University nor the names of its contributors
    380  1.2.4.2  ad  *    may be used to endorse or promote products derived from this software
    381  1.2.4.2  ad  *    without specific prior written permission.
    382  1.2.4.2  ad  *
    383  1.2.4.2  ad  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    384  1.2.4.2  ad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    385  1.2.4.2  ad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    386  1.2.4.2  ad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    387  1.2.4.2  ad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    388  1.2.4.2  ad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    389  1.2.4.2  ad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    390  1.2.4.2  ad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    391  1.2.4.2  ad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    392  1.2.4.2  ad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    393  1.2.4.2  ad  * SUCH DAMAGE.
    394  1.2.4.2  ad  *
    395  1.2.4.2  ad  *	@(#)autoconf.c	7.1 (Berkeley) 5/9/91
    396  1.2.4.2  ad  */
    397  1.2.4.2  ad 
    398  1.2.4.2  ad /*
    399  1.2.4.2  ad  * Setup the system to run on the current machine.
    400  1.2.4.2  ad  *
    401  1.2.4.2  ad  * Configure() is called at boot time and initializes the vba
    402  1.2.4.2  ad  * device tables and the memory controller monitoring.  Available
    403  1.2.4.2  ad  * devices are determined (from possibilities mentioned in ioconf.c),
    404  1.2.4.2  ad  * and the drivers are initialized.
    405  1.2.4.2  ad  */
    406  1.2.4.2  ad 
    407  1.2.4.2  ad #include <sys/cdefs.h>
    408  1.2.4.2  ad __KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.2.4.2 2007/12/03 19:04:38 ad Exp $");
    409  1.2.4.2  ad 
    410  1.2.4.2  ad #include "opt_xen.h"
    411  1.2.4.2  ad #include "opt_compat_oldboot.h"
    412  1.2.4.2  ad #include "opt_multiprocessor.h"
    413  1.2.4.2  ad #include "opt_nfs_boot.h"
    414  1.2.4.2  ad #include "xennet_hypervisor.h"
    415  1.2.4.2  ad #include "xennet_xenbus.h"
    416  1.2.4.2  ad 
    417  1.2.4.2  ad #include <sys/param.h>
    418  1.2.4.2  ad #include <sys/systm.h>
    419  1.2.4.2  ad #include <sys/buf.h>
    420  1.2.4.2  ad #include <sys/disklabel.h>
    421  1.2.4.2  ad #include <sys/disk.h>
    422  1.2.4.2  ad #include <sys/conf.h>
    423  1.2.4.2  ad #ifdef COMPAT_OLDBOOT
    424  1.2.4.2  ad #include <sys/reboot.h>
    425  1.2.4.2  ad #endif
    426  1.2.4.2  ad #include <sys/device.h>
    427  1.2.4.2  ad #include <sys/malloc.h>
    428  1.2.4.2  ad #include <sys/vnode.h>
    429  1.2.4.2  ad #include <sys/fcntl.h>
    430  1.2.4.2  ad #include <sys/dkio.h>
    431  1.2.4.2  ad #include <sys/proc.h>
    432  1.2.4.2  ad #include <sys/user.h>
    433  1.2.4.2  ad #include <sys/kauth.h>
    434  1.2.4.2  ad 
    435  1.2.4.2  ad #ifdef NFS_BOOT_BOOTSTATIC
    436  1.2.4.2  ad #include <net/if.h>
    437  1.2.4.2  ad #include <net/if_ether.h>
    438  1.2.4.2  ad #include <netinet/in.h>
    439  1.2.4.2  ad #include <nfs/rpcv2.h>
    440  1.2.4.2  ad #include <nfs/nfsproto.h>
    441  1.2.4.2  ad #include <nfs/nfs.h>
    442  1.2.4.2  ad #include <nfs/nfsmount.h>
    443  1.2.4.2  ad #include <nfs/nfsdiskless.h>
    444  1.2.4.2  ad #include <xen/if_xennetvar.h>
    445  1.2.4.2  ad #endif
    446  1.2.4.2  ad 
    447  1.2.4.2  ad #include <machine/pte.h>
    448  1.2.4.2  ad #include <machine/cpu.h>
    449  1.2.4.2  ad #include <machine/gdt.h>
    450  1.2.4.2  ad #include <machine/pcb.h>
    451  1.2.4.2  ad #include <machine/bootinfo.h>
    452  1.2.4.2  ad 
    453  1.2.4.2  ad static void findroot(void);
    454  1.2.4.2  ad static int is_valid_disk(struct device *);
    455  1.2.4.2  ad static void handle_wedges(struct device *, int);
    456  1.2.4.2  ad 
    457  1.2.4.2  ad struct disklist *x86_alldisks;
    458  1.2.4.2  ad int x86_ndisks;
    459  1.2.4.2  ad 
    460  1.2.4.2  ad #include "bios32.h"
    461  1.2.4.2  ad #if NBIOS32 > 0
    462  1.2.4.2  ad #include <machine/bios32.h>
    463  1.2.4.2  ad #endif
    464  1.2.4.2  ad 
    465  1.2.4.2  ad #include "opt_pcibios.h"
    466  1.2.4.2  ad #ifdef PCIBIOS
    467  1.2.4.2  ad #include <dev/pci/pcireg.h>
    468  1.2.4.2  ad #include <dev/pci/pcivar.h>
    469  1.2.4.2  ad #include <i386/pci/pcibios.h>
    470  1.2.4.2  ad #endif
    471  1.2.4.2  ad 
    472  1.2.4.2  ad #include "opt_kvm86.h"
    473  1.2.4.2  ad #ifdef KVM86
    474  1.2.4.2  ad #include <machine/kvm86.h>
    475  1.2.4.2  ad #endif
    476  1.2.4.2  ad 
    477  1.2.4.2  ad /*
    478  1.2.4.2  ad  * Determine i/o configuration for a machine.
    479  1.2.4.2  ad  */
    480  1.2.4.2  ad void
    481  1.2.4.2  ad cpu_configure(void)
    482  1.2.4.2  ad {
    483  1.2.4.2  ad 
    484  1.2.4.2  ad 	startrtclock();
    485  1.2.4.2  ad 
    486  1.2.4.2  ad #if NBIOS32 > 0 && defined(DOM0OPS)
    487  1.2.4.2  ad #ifdef XEN3
    488  1.2.4.2  ad 	if (xen_start_info.flags & SIF_INITDOMAIN)
    489  1.2.4.2  ad #endif
    490  1.2.4.2  ad 		bios32_init();
    491  1.2.4.2  ad #endif /* NBIOS32 > 0 && DOM0OPS */
    492  1.2.4.2  ad #ifdef PCIBIOS
    493  1.2.4.2  ad 	pcibios_init();
    494  1.2.4.2  ad #endif
    495  1.2.4.2  ad 
    496  1.2.4.2  ad #ifdef KVM86
    497  1.2.4.2  ad 	kvm86_init();
    498  1.2.4.2  ad #endif
    499  1.2.4.2  ad 
    500  1.2.4.2  ad 	if (config_rootfound("mainbus", NULL) == NULL)
    501  1.2.4.2  ad 		panic("configure: mainbus not configured");
    502  1.2.4.2  ad 
    503  1.2.4.2  ad #ifdef INTRDEBUG
    504  1.2.4.2  ad 	intr_printconfig();
    505  1.2.4.2  ad #endif
    506  1.2.4.2  ad 
    507  1.2.4.2  ad 	/* resync cr0 after FPU configuration */
    508  1.2.4.2  ad 	lwp0.l_addr->u_pcb.pcb_cr0 = rcr0();
    509  1.2.4.2  ad #ifdef MULTIPROCESSOR
    510  1.2.4.2  ad 	/* propagate this to the idle pcb's. */
    511  1.2.4.2  ad 	cpu_init_idle_lwps();
    512  1.2.4.2  ad #endif
    513  1.2.4.2  ad 
    514  1.2.4.2  ad 	spl0();
    515  1.2.4.2  ad }
    516  1.2.4.2  ad 
    517  1.2.4.2  ad void
    518  1.2.4.2  ad cpu_rootconf(void)
    519  1.2.4.2  ad {
    520  1.2.4.2  ad 	findroot();
    521  1.2.4.2  ad 
    522  1.2.4.2  ad 	if (booted_wedge) {
    523  1.2.4.2  ad 		KASSERT(booted_device != NULL);
    524  1.2.4.2  ad 		printf("boot device: %s (%s)\n",
    525  1.2.4.2  ad 		    booted_wedge->dv_xname, booted_device->dv_xname);
    526  1.2.4.2  ad 		setroot(booted_wedge, 0);
    527  1.2.4.2  ad 	} else {
    528  1.2.4.2  ad 		printf("boot device: %s\n",
    529  1.2.4.2  ad 		    booted_device ? booted_device->dv_xname : "<unknown>");
    530  1.2.4.2  ad 		setroot(booted_device, booted_partition);
    531  1.2.4.2  ad 	}
    532  1.2.4.2  ad }
    533  1.2.4.2  ad 
    534  1.2.4.2  ad 
    535  1.2.4.2  ad /*
    536  1.2.4.2  ad  * Attempt to find the device from which we were booted.
    537  1.2.4.2  ad  * If we can do so, and not instructed not to do so,
    538  1.2.4.2  ad  * change rootdev to correspond to the load device.
    539  1.2.4.2  ad  */
    540  1.2.4.2  ad void
    541  1.2.4.2  ad findroot(void)
    542  1.2.4.2  ad {
    543  1.2.4.2  ad 	struct device *dv;
    544  1.2.4.2  ad 	union xen_cmdline_parseinfo xcp;
    545  1.2.4.2  ad 
    546  1.2.4.2  ad 	if (booted_device)
    547  1.2.4.2  ad 		return;
    548  1.2.4.2  ad 
    549  1.2.4.2  ad 	xen_parse_cmdline(XEN_PARSE_BOOTDEV, &xcp);
    550  1.2.4.2  ad 
    551  1.2.4.2  ad 	TAILQ_FOREACH(dv, &alldevs, dv_list) {
    552  1.2.4.2  ad 		if (is_valid_disk(dv) == 0)
    553  1.2.4.2  ad 			continue;
    554  1.2.4.2  ad 
    555  1.2.4.2  ad 		if (xcp.xcp_bootdev[0] == 0) {
    556  1.2.4.2  ad 			handle_wedges(dv, 0);
    557  1.2.4.2  ad 			break;
    558  1.2.4.2  ad 		}
    559  1.2.4.2  ad 
    560  1.2.4.2  ad 		if (strncmp(xcp.xcp_bootdev, dv->dv_xname,
    561  1.2.4.2  ad 		    strlen(dv->dv_xname)))
    562  1.2.4.2  ad 			continue;
    563  1.2.4.2  ad 
    564  1.2.4.2  ad 		if (strlen(xcp.xcp_bootdev) > strlen(dv->dv_xname)) {
    565  1.2.4.2  ad 			booted_partition = toupper(
    566  1.2.4.2  ad 				xcp.xcp_bootdev[strlen(dv->dv_xname)]) - 'A';
    567  1.2.4.2  ad 		}
    568  1.2.4.2  ad 
    569  1.2.4.2  ad 		booted_device = dv;
    570  1.2.4.2  ad 		break;
    571  1.2.4.2  ad 	}
    572  1.2.4.2  ad }
    573  1.2.4.2  ad 
    574  1.2.4.2  ad #include "pci.h"
    575  1.2.4.2  ad 
    576  1.2.4.2  ad #include <dev/isa/isavar.h>
    577  1.2.4.2  ad #if NPCI > 0
    578  1.2.4.2  ad #include <dev/pci/pcivar.h>
    579  1.2.4.2  ad #endif
    580  1.2.4.2  ad 
    581  1.2.4.2  ad 
    582  1.2.4.2  ad #if defined(NFS_BOOT_BOOTSTATIC) && defined(DOM0OPS)
    583  1.2.4.2  ad static int
    584  1.2.4.2  ad dom0_bootstatic_callback(struct nfs_diskless *nd)
    585  1.2.4.2  ad {
    586  1.2.4.2  ad #if 0
    587  1.2.4.2  ad 	struct ifnet *ifp = nd->nd_ifp;
    588  1.2.4.2  ad #endif
    589  1.2.4.2  ad 	union xen_cmdline_parseinfo xcp;
    590  1.2.4.2  ad 	struct sockaddr_in *sin;
    591  1.2.4.2  ad 
    592  1.2.4.2  ad 	memset(&xcp, 0, sizeof(xcp.xcp_netinfo));
    593  1.2.4.2  ad 	xcp.xcp_netinfo.xi_ifno = 0; /* XXX first interface hardcoded */
    594  1.2.4.2  ad 	xcp.xcp_netinfo.xi_root = nd->nd_root.ndm_host;
    595  1.2.4.2  ad 	xen_parse_cmdline(XEN_PARSE_NETINFO, &xcp);
    596  1.2.4.2  ad 
    597  1.2.4.2  ad 	nd->nd_myip.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[0]);
    598  1.2.4.2  ad 	nd->nd_gwip.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[2]);
    599  1.2.4.2  ad 	nd->nd_mask.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[3]);
    600  1.2.4.2  ad 
    601  1.2.4.2  ad 	sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
    602  1.2.4.2  ad 	memset((void *)sin, 0, sizeof(*sin));
    603  1.2.4.2  ad 	sin->sin_len = sizeof(*sin);
    604  1.2.4.2  ad 	sin->sin_family = AF_INET;
    605  1.2.4.2  ad 	sin->sin_addr.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[1]);
    606  1.2.4.2  ad 
    607  1.2.4.2  ad 	if (nd->nd_myip.s_addr == 0)
    608  1.2.4.2  ad 		return NFS_BOOTSTATIC_NOSTATIC;
    609  1.2.4.2  ad 	else
    610  1.2.4.2  ad 		return (NFS_BOOTSTATIC_HAS_MYIP|NFS_BOOTSTATIC_HAS_GWIP|
    611  1.2.4.2  ad 		    NFS_BOOTSTATIC_HAS_MASK|NFS_BOOTSTATIC_HAS_SERVADDR|
    612  1.2.4.2  ad 		    NFS_BOOTSTATIC_HAS_SERVER);
    613  1.2.4.2  ad }
    614  1.2.4.2  ad #endif
    615  1.2.4.2  ad 
    616  1.2.4.2  ad void
    617  1.2.4.2  ad device_register(struct device *dev, void *aux)
    618  1.2.4.2  ad {
    619  1.2.4.2  ad 	/*
    620  1.2.4.2  ad 	 * Handle network interfaces here, the attachment information is
    621  1.2.4.2  ad 	 * not available driver independently later.
    622  1.2.4.2  ad 	 * For disks, there is nothing useful available at attach time.
    623  1.2.4.2  ad 	 */
    624  1.2.4.2  ad #if NXENNET_HYPERVISOR > 0 || NXENNET_XENBUS > 0 || defined(DOM0OPS)
    625  1.2.4.2  ad 	if (device_class(dev) == DV_IFNET) {
    626  1.2.4.2  ad 		union xen_cmdline_parseinfo xcp;
    627  1.2.4.2  ad 
    628  1.2.4.2  ad 		xen_parse_cmdline(XEN_PARSE_BOOTDEV, &xcp);
    629  1.2.4.2  ad 		if (strncmp(xcp.xcp_bootdev, dev->dv_xname, 16) == 0) {
    630  1.2.4.2  ad #ifdef NFS_BOOT_BOOTSTATIC
    631  1.2.4.2  ad #ifdef DOM0OPS
    632  1.2.4.2  ad 			if (xen_start_info.flags & SIF_PRIVILEGED) {
    633  1.2.4.2  ad 				nfs_bootstatic_callback = dom0_bootstatic_callback;
    634  1.2.4.2  ad 			} else
    635  1.2.4.2  ad #endif
    636  1.2.4.2  ad #if NXENNET_HYPERVISOR > 0 || NXENNET_XENBUS > 0
    637  1.2.4.2  ad 			nfs_bootstatic_callback = xennet_bootstatic_callback;
    638  1.2.4.2  ad #endif
    639  1.2.4.2  ad #endif
    640  1.2.4.2  ad 			goto found;
    641  1.2.4.2  ad 		}
    642  1.2.4.2  ad 	}
    643  1.2.4.2  ad #endif
    644  1.2.4.2  ad 	if (device_class(dev) == DV_IFNET) {
    645  1.2.4.2  ad 		struct btinfo_netif *bin = lookup_bootinfo(BTINFO_NETIF);
    646  1.2.4.2  ad 		if (bin == NULL)
    647  1.2.4.2  ad 			return;
    648  1.2.4.2  ad 
    649  1.2.4.2  ad 		/*
    650  1.2.4.2  ad 		 * We don't check the driver name against the device name
    651  1.2.4.2  ad 		 * passed by the boot ROM. The ROM should stay usable
    652  1.2.4.2  ad 		 * if the driver gets obsoleted.
    653  1.2.4.2  ad 		 * The physical attachment information (checked below)
    654  1.2.4.2  ad 		 * must be sufficient to identify the device.
    655  1.2.4.2  ad 		 */
    656  1.2.4.2  ad 
    657  1.2.4.2  ad 		if (bin->bus == BI_BUS_ISA &&
    658  1.2.4.2  ad 		    device_is_a(device_parent(dev), "isa")) {
    659  1.2.4.2  ad 			struct isa_attach_args *iaa = aux;
    660  1.2.4.2  ad 
    661  1.2.4.2  ad 			/* compare IO base address */
    662  1.2.4.2  ad 			/* XXXJRT what about multiple I/O addrs? */
    663  1.2.4.2  ad 			if (iaa->ia_nio > 0 &&
    664  1.2.4.2  ad 			    bin->addr.iobase == iaa->ia_io[0].ir_addr)
    665  1.2.4.2  ad 				goto found;
    666  1.2.4.2  ad 		}
    667  1.2.4.2  ad #if NPCI > 0
    668  1.2.4.2  ad 		if (bin->bus == BI_BUS_PCI &&
    669  1.2.4.2  ad 		    device_is_a(device_parent(dev), "pci")) {
    670  1.2.4.2  ad 			struct pci_attach_args *paa = aux;
    671  1.2.4.2  ad 			int b, d, f;
    672  1.2.4.2  ad 
    673  1.2.4.2  ad 			/*
    674  1.2.4.2  ad 			 * Calculate BIOS representation of:
    675  1.2.4.2  ad 			 *
    676  1.2.4.2  ad 			 *	<bus,device,function>
    677  1.2.4.2  ad 			 *
    678  1.2.4.2  ad 			 * and compare.
    679  1.2.4.2  ad 			 */
    680  1.2.4.2  ad 			pci_decompose_tag(paa->pa_pc, paa->pa_tag, &b, &d, &f);
    681  1.2.4.2  ad 			if (bin->addr.tag == ((b << 8) | (d << 3) | f))
    682  1.2.4.2  ad 				goto found;
    683  1.2.4.2  ad 		}
    684  1.2.4.2  ad #endif
    685  1.2.4.2  ad 	}
    686  1.2.4.2  ad 	return;
    687  1.2.4.2  ad 
    688  1.2.4.2  ad found:
    689  1.2.4.2  ad 	if (booted_device) {
    690  1.2.4.2  ad 		/* XXX should be a "panic()" */
    691  1.2.4.2  ad 		printf("warning: double match for boot device (%s, %s)\n",
    692  1.2.4.2  ad 		    booted_device->dv_xname, dev->dv_xname);
    693  1.2.4.2  ad 		return;
    694  1.2.4.2  ad 	}
    695  1.2.4.2  ad 	booted_device = dev;
    696  1.2.4.2  ad }
    697  1.2.4.2  ad 
    698  1.2.4.2  ad static void
    699  1.2.4.2  ad handle_wedges(struct device *dv, int par)
    700  1.2.4.2  ad {
    701  1.2.4.2  ad 	if (config_handle_wedges(dv, par) == 0)
    702  1.2.4.2  ad 		return;
    703  1.2.4.2  ad 	booted_device = dv;
    704  1.2.4.2  ad 	booted_partition = par;
    705  1.2.4.2  ad }
    706  1.2.4.2  ad 
    707  1.2.4.2  ad static int
    708  1.2.4.2  ad is_valid_disk(struct device *dv)
    709  1.2.4.2  ad {
    710  1.2.4.2  ad 
    711  1.2.4.2  ad 	if (device_class(dv) != DV_DISK)
    712  1.2.4.2  ad 		return (0);
    713  1.2.4.2  ad 
    714  1.2.4.2  ad 	return (device_is_a(dv, "dk") ||
    715  1.2.4.2  ad 		device_is_a(dv, "sd") ||
    716  1.2.4.2  ad 		device_is_a(dv, "wd") ||
    717  1.2.4.2  ad 		device_is_a(dv, "ld") ||
    718  1.2.4.2  ad 		device_is_a(dv, "ed") ||
    719  1.2.4.2  ad 		device_is_a(dv, "xbd"));
    720  1.2.4.2  ad }
    721