xref: /netbsd-src/sys/arch/xen/x86/autoconf.c (revision 6d322f2f4598f0d8a138f10ea648ec4fabe41f8b)
1 /*	$NetBSD: autoconf.c,v 1.16 2012/10/03 18:58:33 dsl 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.16 2012/10/03 18:58:33 dsl 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 
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/buf.h>
58 #include <sys/disklabel.h>
59 #include <sys/disk.h>
60 #include <sys/conf.h>
61 #ifdef COMPAT_OLDBOOT
62 #include <sys/reboot.h>
63 #endif
64 #include <sys/device.h>
65 #include <sys/vnode.h>
66 #include <sys/fcntl.h>
67 #include <sys/dkio.h>
68 #include <sys/proc.h>
69 #include <sys/kauth.h>
70 
71 #ifdef NFS_BOOT_BOOTSTATIC
72 #include <net/if.h>
73 #include <net/if_ether.h>
74 #include <netinet/in.h>
75 #include <nfs/rpcv2.h>
76 #include <nfs/nfsproto.h>
77 #include <nfs/nfs.h>
78 #include <nfs/nfsmount.h>
79 #include <nfs/nfsdiskless.h>
80 #include <xen/if_xennetvar.h>
81 #endif
82 
83 #include <machine/pte.h>
84 #include <machine/cpu.h>
85 #include <machine/gdt.h>
86 #include <machine/pcb.h>
87 #include <machine/bootinfo.h>
88 
89 static void findroot(void);
90 static int is_valid_disk(device_t);
91 
92 struct disklist *x86_alldisks;
93 int x86_ndisks;
94 
95 #include "bios32.h"
96 #if NBIOS32 > 0
97 #include <machine/bios32.h>
98 #endif
99 
100 #include "opt_pcibios.h"
101 #ifdef PCIBIOS
102 #include <dev/pci/pcireg.h>
103 #include <dev/pci/pcivar.h>
104 #include <i386/pci/pcibios.h>
105 #endif
106 
107 /*
108  * Determine i/o configuration for a machine.
109  */
110 void
111 cpu_configure(void)
112 {
113 	struct pcb *pcb;
114 
115 	startrtclock();
116 
117 #if NBIOS32 > 0 && defined(DOM0OPS)
118 	if (xendomain_is_dom0())
119 		bios32_init();
120 #endif /* NBIOS32 > 0 && DOM0OPS */
121 #ifdef PCIBIOS
122 	pcibios_init();
123 #endif
124 
125 	if (config_rootfound("mainbus", NULL) == NULL)
126 		panic("configure: mainbus not configured");
127 
128 #ifdef INTRDEBUG
129 	intr_printconfig();
130 #endif
131 
132 	/* resync cr0 after FPU configuration */
133 	pcb = lwp_getpcb(&lwp0);
134 	pcb->pcb_cr0 = rcr0();
135 #ifdef MULTIPROCESSOR
136 	/* propagate this to the idle pcb's. */
137 	cpu_init_idle_lwps();
138 #endif
139 
140 	spl0();
141 }
142 
143 void
144 cpu_rootconf(void)
145 {
146 	findroot();
147 
148 	printf("boot device: %s\n",
149 	    booted_device ? device_xname(booted_device) : "<unknown>");
150 	rootconf();
151 }
152 
153 
154 /*
155  * Attempt to find the device from which we were booted.
156  * If we can do so, and not instructed not to do so,
157  * change rootdev to correspond to the load device.
158  */
159 void
160 findroot(void)
161 {
162 	device_t dv;
163 	deviter_t di;
164 	union xen_cmdline_parseinfo xcp;
165 
166 	if (booted_device)
167 		return;
168 
169 	xen_parse_cmdline(XEN_PARSE_BOOTDEV, &xcp);
170 
171 	for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
172 	     dv != NULL;
173 	     dv = deviter_next(&di)) {
174 		bool is_ifnet, is_disk;
175 		const char *devname;
176 
177 		is_ifnet = (device_class(dv) == DV_IFNET);
178 		is_disk = is_valid_disk(dv);
179 		devname = device_xname(dv);
180 
181 		if (!is_ifnet && !is_disk)
182 			continue;
183 
184 		if (is_disk && xcp.xcp_bootdev[0] == 0) {
185 			booted_device = dv;
186 			break;
187 		}
188 
189 		if (strncmp(xcp.xcp_bootdev, devname, strlen(devname)))
190 			continue;
191 
192 		if (is_disk && strlen(xcp.xcp_bootdev) > strlen(devname)) {
193 			booted_partition = toupper(
194 				xcp.xcp_bootdev[strlen(devname)]) - 'A';
195 		}
196 
197 		booted_device = dv;
198 		break;
199 	}
200 	deviter_release(&di);
201 }
202 
203 #include "pci.h"
204 
205 #include <dev/isa/isavar.h>
206 #if NPCI > 0
207 #include <dev/pci/pcivar.h>
208 #endif
209 
210 
211 #if defined(NFS_BOOT_BOOTSTATIC) && defined(DOM0OPS)
212 static int
213 dom0_bootstatic_callback(struct nfs_diskless *nd)
214 {
215 #if 0
216 	struct ifnet *ifp = nd->nd_ifp;
217 #endif
218 	int flags = 0;
219 	union xen_cmdline_parseinfo xcp;
220 	struct sockaddr_in *sin;
221 
222 	memset(&xcp, 0, sizeof(xcp.xcp_netinfo));
223 	xcp.xcp_netinfo.xi_ifno = 0; /* XXX first interface hardcoded */
224 	xcp.xcp_netinfo.xi_root = nd->nd_root.ndm_host;
225 	xen_parse_cmdline(XEN_PARSE_NETINFO, &xcp);
226 
227 	if (xcp.xcp_netinfo.xi_root[0] != '\0') {
228 		flags |= NFS_BOOT_HAS_SERVER;
229 		if (strchr(xcp.xcp_netinfo.xi_root, ':') != NULL)
230 			flags |= NFS_BOOT_HAS_ROOTPATH;
231 	}
232 
233 	nd->nd_myip.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[0]);
234 	nd->nd_gwip.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[2]);
235 	nd->nd_mask.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[3]);
236 
237 	sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
238 	memset((void *)sin, 0, sizeof(*sin));
239 	sin->sin_len = sizeof(*sin);
240 	sin->sin_family = AF_INET;
241 	sin->sin_addr.s_addr = ntohl(xcp.xcp_netinfo.xi_ip[1]);
242 
243 	if (nd->nd_myip.s_addr)
244 		flags |= NFS_BOOT_HAS_MYIP;
245 	if (nd->nd_gwip.s_addr)
246 		flags |= NFS_BOOT_HAS_GWIP;
247 	if (nd->nd_mask.s_addr)
248 		flags |= NFS_BOOT_HAS_MASK;
249 	if (sin->sin_addr.s_addr)
250 		flags |= NFS_BOOT_HAS_SERVADDR;
251 
252 	return flags;
253 }
254 #endif
255 
256 void
257 device_register(device_t dev, void *aux)
258 {
259 	/*
260 	 * Handle network interfaces here, the attachment information is
261 	 * not available driver independently later.
262 	 * For disks, there is nothing useful available at attach time.
263 	 */
264 #if NXENNET_HYPERVISOR > 0 || NXENNET_XENBUS > 0 || defined(DOM0OPS)
265 	if (device_class(dev) == DV_IFNET) {
266 		union xen_cmdline_parseinfo xcp;
267 
268 #ifdef NFS_BOOT_BOOTSTATIC
269 #ifdef DOM0OPS
270 		if (xendomain_is_privileged()) {
271 			nfs_bootstatic_callback = dom0_bootstatic_callback;
272 		} else
273 #endif
274 #if NXENNET_HYPERVISOR > 0 || NXENNET_XENBUS > 0
275 		nfs_bootstatic_callback = xennet_bootstatic_callback;
276 #endif
277 #endif
278 		xen_parse_cmdline(XEN_PARSE_BOOTDEV, &xcp);
279 		if (strncmp(xcp.xcp_bootdev, device_xname(dev),
280 		    sizeof(xcp.xcp_bootdev)) == 0)
281 		{
282 			goto found;
283 		}
284 	}
285 #endif
286 	if (device_class(dev) == DV_IFNET) {
287 		struct btinfo_netif *bin = lookup_bootinfo(BTINFO_NETIF);
288 		if (bin == NULL)
289 			return;
290 
291 		/*
292 		 * We don't check the driver name against the device name
293 		 * passed by the boot ROM. The ROM should stay usable
294 		 * if the driver gets obsoleted.
295 		 * The physical attachment information (checked below)
296 		 * must be sufficient to identify the device.
297 		 */
298 
299 		if (bin->bus == BI_BUS_ISA &&
300 		    device_is_a(device_parent(dev), "isa")) {
301 			struct isa_attach_args *iaa = aux;
302 
303 			/* compare IO base address */
304 			/* XXXJRT what about multiple I/O addrs? */
305 			if (iaa->ia_nio > 0 &&
306 			    bin->addr.iobase == iaa->ia_io[0].ir_addr)
307 				goto found;
308 		}
309 #if NPCI > 0
310 		if (bin->bus == BI_BUS_PCI &&
311 		    device_is_a(device_parent(dev), "pci")) {
312 			struct pci_attach_args *paa = aux;
313 			int b, d, f;
314 
315 			/*
316 			 * Calculate BIOS representation of:
317 			 *
318 			 *	<bus,device,function>
319 			 *
320 			 * and compare.
321 			 */
322 			pci_decompose_tag(paa->pa_pc, paa->pa_tag, &b, &d, &f);
323 			if (bin->addr.tag == ((b << 8) | (d << 3) | f))
324 				goto found;
325 		}
326 #endif
327 	}
328 	return;
329 
330 found:
331 	if (booted_device) {
332 		/* XXX should be a "panic()" */
333 		printf("warning: double match for boot device (%s, %s)\n",
334 		    device_xname(booted_device), device_xname(dev));
335 		return;
336 	}
337 	booted_device = dev;
338 }
339 
340 static int
341 is_valid_disk(device_t dv)
342 {
343 
344 	if (device_class(dv) != DV_DISK)
345 		return (0);
346 
347 	return (device_is_a(dv, "dk") ||
348 		device_is_a(dv, "sd") ||
349 		device_is_a(dv, "wd") ||
350 		device_is_a(dv, "ld") ||
351 		device_is_a(dv, "ed") ||
352 		device_is_a(dv, "xbd"));
353 }
354