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