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