xref: /dragonfly/sys/platform/pc64/x86_64/autoconf.c (revision 5062ee70)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * Copyright (c) 2008 The DragonFly Project.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * William Jolitz.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	from: @(#)autoconf.c	7.1 (Berkeley) 5/9/91
34  * $FreeBSD: src/sys/i386/i386/autoconf.c,v 1.146.2.2 2001/06/07 06:05:58 dd Exp $
35  */
36 
37 /*
38  * Setup the system to run on the current machine.
39  *
40  * Configure() is called at boot time and initializes the vba
41  * device tables and the memory controller monitoring.  Available
42  * devices are determined (from possibilities mentioned in ioconf.c),
43  * and the drivers are initialized.
44  */
45 #include "opt_bootp.h"
46 #include "opt_cd9660.h"
47 #include "opt_nfs.h"
48 #include "opt_nfsroot.h"
49 #include "opt_rootdevname.h"
50 
51 #include "use_isa.h"
52 
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/bootmaj.h>
56 #include <sys/bus.h>
57 #include <sys/conf.h>
58 #include <sys/diskslice.h>
59 #include <sys/reboot.h>
60 #include <sys/kernel.h>
61 #include <sys/malloc.h>
62 #include <sys/mount.h>
63 #include <sys/cons.h>
64 #include <sys/thread.h>
65 #include <sys/device.h>
66 #include <sys/machintr.h>
67 
68 #include <machine/bootinfo.h>
69 #include <machine/md_var.h>
70 #include <machine/smp.h>
71 #include <machine_base/icu/icu.h>
72 
73 #include <machine/pcb.h>
74 #include <machine/pcb_ext.h>
75 #include <machine/globaldata.h>
76 
77 #if NISA > 0
78 #include <bus/isa/isavar.h>
79 
80 device_t isa_bus_device = NULL;
81 #endif
82 
83 static void	configure_first (void *);
84 static void	configure (void *);
85 static void	configure_final (void *);
86 
87 #if defined(NFS) && defined(NFS_ROOT)
88 #if !defined(BOOTP_NFSROOT)
89 static void	pxe_setup_nfsdiskless(void);
90 #endif
91 #endif
92 
93 SYSINIT(configure1, SI_SUB_CONFIGURE, SI_ORDER_FIRST, configure_first, NULL);
94 /* SI_ORDER_SECOND is hookable */
95 SYSINIT(configure2, SI_SUB_CONFIGURE, SI_ORDER_THIRD, configure, NULL);
96 /* SI_ORDER_MIDDLE is hookable */
97 SYSINIT(configure3, SI_SUB_CONFIGURE, SI_ORDER_ANY, configure_final, NULL);
98 
99 cdev_t	rootdev = NULL;
100 cdev_t	dumpdev = NULL;
101 
102 /*
103  * nfsroot.iosize may be set in loader.conf, 32768 is recommended to
104  * be able to max-out a GigE link if the server supports it.  Many servers
105  * do not so the default is 8192.
106  *
107  * nfsroot.rahead defaults to something reasonable, can be overridden.
108  */
109 static int nfsroot_iosize = 8192;
110 TUNABLE_INT("nfsroot.iosize", &nfsroot_iosize);
111 static int nfsroot_rahead = 4;
112 TUNABLE_INT("nfsroot.rahead", &nfsroot_rahead);
113 
114 /*
115  * Determine i/o configuration for a machine.
116  */
117 static void
118 configure_first(void *dummy)
119 {
120 }
121 
122 static void
123 configure(void *dummy)
124 {
125 #if NISA > 0
126 	void *low_dma_reserve;
127 
128 	/*
129 	 * Try to reserve 512k of low dma memory for later isa attach.
130 	 * Document as a REALLY bad hack.
131 	 */
132 	low_dma_reserve = contigmalloc(0x80000, M_TEMP, M_NOWAIT, 0ul,
133 				       0xfffffful, 1ul, 0x20000ul);
134 	if (bootverbose)
135 		kprintf("low dma reserve placed @ %p\n", low_dma_reserve);
136 #endif
137 
138 	/*
139 	 * This will configure all devices, generally starting with the
140 	 * nexus (pc64/x86_64/nexus.c).  The nexus ISA code explicitly
141 	 * dummies up the attach in order to delay legacy initialization
142 	 * until after all other busses/subsystems have had a chance
143 	 * at those resources.
144 	 */
145 	root_bus_configure();
146 
147 #if NISA > 0
148 	/*
149 	 * Free up reserve if we got it.
150 	 */
151 	if (low_dma_reserve != NULL) {
152 		kprintf("Freeing low dma reserve @ %p\n", low_dma_reserve);
153 		contigfree(low_dma_reserve, 0x80000, M_TEMP);
154 	} else if (bootverbose) {
155 		kprintf("low dma reserve was not allocated\n");
156 	}
157 
158 	/*
159 	 * Explicitly probe and attach ISA last.  The isa bus saves
160 	 * it's device node at attach time for us here.
161 	 */
162 	if (isa_bus_device)
163 		isa_probe_children(isa_bus_device);
164 #endif
165 
166 	/*
167 	 * Allow lowering of the ipl to the lowest kernel level if we
168 	 * panic (or call tsleep() before clearing `cold').  No level is
169 	 * completely safe (since a panic may occur in a critical region
170 	 * at splhigh()), but we want at least bio interrupts to work.
171 	 */
172 	safepri = TDPRI_KERN_USER;
173 }
174 
175 static void
176 configure_final(void *dummy)
177 {
178 	cninit_finish();
179 
180 	if (bootverbose) {
181 #if 0 /* JG */
182 		/*
183 		 * Print out the BIOS's idea of the disk geometries.
184 		 */
185 		int i;
186 		kprintf("BIOS Geometries:\n");
187 		for (i = 0; i < N_BIOS_GEOM; i++) {
188 			unsigned long bios_geom;
189 			int max_cylinder, max_head, max_sector;
190 
191 			bios_geom = bootinfo.bi_bios_geom[i];
192 
193 			/*
194 			 * XXX the bootstrap punts a 1200K floppy geometry
195 			 * when the get-disk-geometry interrupt fails.  Skip
196 			 * drives that have this geometry.
197 			 */
198 			if (bios_geom == 0x4f010f)
199 				continue;
200 
201 			kprintf(" %x:%08lx ", i, bios_geom);
202 			max_cylinder = bios_geom >> 16;
203 			max_head = (bios_geom >> 8) & 0xff;
204 			max_sector = bios_geom & 0xff;
205 			kprintf(
206 		"0..%d=%d cylinders, 0..%d=%d heads, 1..%d=%d sectors\n",
207 			       max_cylinder, max_cylinder + 1,
208 			       max_head, max_head + 1,
209 			       max_sector, max_sector);
210 		}
211 		kprintf(" %d accounted for\n", bootinfo.bi_n_bios_used);
212 
213 		kprintf("Device configuration finished.\n");
214 #endif
215 	}
216 }
217 
218 #ifdef BOOTP
219 void bootpc_init(void);
220 #endif
221 /*
222  * Do legacy root filesystem discovery.
223  */
224 void
225 cpu_rootconf(void)
226 {
227 #ifdef BOOTP
228         bootpc_init();
229 #endif
230 #if defined(NFS) && defined(NFS_ROOT)
231 #if !defined(BOOTP_NFSROOT)
232 	pxe_setup_nfsdiskless();
233 	if (nfs_diskless_valid)
234 #endif
235 		rootdevnames[0] = "nfs:";
236 #endif
237 }
238 SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, cpu_rootconf, NULL);
239 
240 #if defined(NFS) && defined(NFS_ROOT)
241 #if !defined(BOOTP_NFSROOT)
242 
243 #include <sys/socket.h>
244 #include <net/if.h>
245 #include <net/if_dl.h>
246 #include <net/if_types.h>
247 #include <net/if_var.h>
248 #include <net/ethernet.h>
249 #include <netinet/in.h>
250 #include <vfs/nfs/rpcv2.h>
251 #include <vfs/nfs/nfsproto.h>
252 #include <vfs/nfs/nfs.h>
253 #include <vfs/nfs/nfsdiskless.h>
254 
255 extern struct nfs_diskless	nfs_diskless;
256 
257 /*
258  * Convert a kenv variable to a sockaddr.  If the kenv variable does not
259  * exist the sockaddr will remain zerod out (callers typically just check
260  * sin_len).  A network address of 0.0.0.0 is equivalent to failure.
261  */
262 static int
263 inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
264 {
265 	u_int32_t	a[4];
266 	char		*cp;
267 
268 	bzero(sa, sizeof(*sa));
269 
270 	if ((cp = kgetenv(ev)) == NULL)
271 		return(1);
272 	if (ksscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]) != 4)
273 		return(1);
274 	if (a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0)
275 		return(1);
276 	/* XXX is this ordering correct? */
277 	sa->sin_addr.s_addr = (a[3] << 24) + (a[2] << 16) + (a[1] << 8) + a[0];
278 	sa->sin_len = sizeof(*sa);
279 	sa->sin_family = AF_INET;
280 	return(0);
281 }
282 
283 static int
284 hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
285 {
286 	char		*cp;
287 	u_int32_t	a[6];
288 
289 	bzero(sa, sizeof(*sa));
290 	sa->sdl_len = sizeof(*sa);
291 	sa->sdl_family = AF_LINK;
292 	sa->sdl_type = IFT_ETHER;
293 	sa->sdl_alen = ETHER_ADDR_LEN;
294 	if ((cp = kgetenv(ev)) == NULL)
295 		return(1);
296 	if (ksscanf(cp, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]) != 6)
297 		return(1);
298 	sa->sdl_data[0] = a[0];
299 	sa->sdl_data[1] = a[1];
300 	sa->sdl_data[2] = a[2];
301 	sa->sdl_data[3] = a[3];
302 	sa->sdl_data[4] = a[4];
303 	sa->sdl_data[5] = a[5];
304 	return(0);
305 }
306 
307 static int
308 decode_nfshandle(char *ev, u_char *fh)
309 {
310 	u_char	*cp;
311 	int	len, val;
312 
313 	if (((cp = kgetenv(ev)) == NULL) || (strlen(cp) < 2) || (*cp != 'X'))
314 		return(0);
315 	len = 0;
316 	cp++;
317 	for (;;) {
318 		if (*cp == 'X')
319 			return(len);
320 		if ((ksscanf(cp, "%2x", &val) != 1) || (val > 0xff))
321 			return(0);
322 		*(fh++) = val;
323 		len++;
324 		cp += 2;
325 		if (len > NFSX_V2FH)
326 		    return(0);
327 	}
328 }
329 
330 /*
331  * Populate the essential fields in the nfsv3_diskless structure.
332  *
333  * The loader is expected to export the following environment variables:
334  *
335  * boot.netif.ip		IP address on boot interface
336  * boot.netif.netmask		netmask on boot interface
337  * boot.netif.gateway		default gateway (optional)
338  * boot.netif.hwaddr		hardware address of boot interface
339  * boot.nfsroot.server		IP address of root filesystem server
340  * boot.nfsroot.path		path of the root filesystem on server
341  * boot.nfsroot.nfshandle	NFS handle for root filesystem on server
342  */
343 static void
344 pxe_setup_nfsdiskless(void)
345 {
346 	struct nfs_diskless	*nd = &nfs_diskless;
347 	struct ifnet		*ifp;
348 	struct sockaddr_dl	*sdl, ourdl;
349 	struct sockaddr_in	myaddr, netmask;
350 	char			*cp;
351 
352 	/* set up interface */
353 	if (inaddr_to_sockaddr("boot.netif.ip", &myaddr))
354 		return;
355 	if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) {
356 		kprintf("PXE: no netmask\n");
357 		return;
358 	}
359 	bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr));
360 	bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr));
361 	((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
362 		myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
363 	bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask));
364 
365 	if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) {
366 		kprintf("PXE: no hardware address\n");
367 		return;
368 	}
369 	ifnet_lock();
370 	TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
371 		struct ifaddr_container *ifac;
372 
373 		TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
374 			struct ifaddr *ifa = ifac->ifa;
375 
376 			if ((ifa->ifa_addr->sa_family == AF_LINK) &&
377 			    (sdl = ((struct sockaddr_dl *)ifa->ifa_addr))) {
378 				if ((sdl->sdl_type == ourdl.sdl_type) &&
379 				    (sdl->sdl_alen == ourdl.sdl_alen) &&
380 				    !bcmp(sdl->sdl_data + sdl->sdl_nlen,
381 					  ourdl.sdl_data + ourdl.sdl_nlen,
382 					  sdl->sdl_alen)) {
383 					strlcpy(nd->myif.ifra_name,
384 					    ifp->if_xname,
385 					    sizeof(nd->myif.ifra_name));
386 					ifnet_unlock();
387 					goto match_done;
388 				}
389 			}
390 		}
391 	}
392 	ifnet_unlock();
393 	kprintf("PXE: no interface\n");
394 	return;	/* no matching interface */
395 match_done:
396 	/* set up gateway */
397 	inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway);
398 
399 	/* XXX set up swap? */
400 
401 	/* set up root mount */
402 	nd->root_args.rsize = nfsroot_iosize;
403 	nd->root_args.wsize = nfsroot_iosize;
404 	nd->root_args.sotype = SOCK_STREAM;
405 	nd->root_args.readahead = nfsroot_rahead;
406 	nd->root_args.flags = NFSMNT_WSIZE | NFSMNT_RSIZE | NFSMNT_RESVPORT |
407 			      NFSMNT_READAHEAD;
408 	if (inaddr_to_sockaddr("boot.nfsroot.server", &nd->root_saddr)) {
409 		kprintf("PXE: no server\n");
410 		return;
411 	}
412 	nd->root_saddr.sin_port = htons(NFS_PORT);
413 
414 	/*
415 	 * A tftp-only loader may pass NFS path information without a
416 	 * root handle.  Generate a warning but continue configuring.
417 	 */
418 	if (decode_nfshandle("boot.nfsroot.nfshandle", &nd->root_fh[0]) == 0) {
419 		kprintf("PXE: Warning, no NFS handle passed from loader\n");
420 	}
421 	if ((cp = kgetenv("boot.nfsroot.path")) != NULL)
422 		strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
423 
424 	nfs_diskless_valid = 1;
425 }
426 
427 #endif
428 #endif
429