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/buf.h>
58 #include <sys/conf.h>
59 #include <sys/diskslice.h>
60 #include <sys/reboot.h>
61 #include <sys/kernel.h>
62 #include <sys/malloc.h>
63 #include <sys/mount.h>
64 #include <sys/cons.h>
65 #include <sys/thread.h>
66 #include <sys/device.h>
67 #include <sys/machintr.h>
68 
69 #include <vm/vm_kern.h>
70 #include <vm/vm_extern.h>
71 #include <vm/vm_pager.h>
72 
73 #if 0
74 #include <machine/pcb.h>
75 #include <machine/pcb_ext.h>
76 #endif
77 #include <machine/smp.h>
78 #include <machine/globaldata.h>
79 #include <machine/md_var.h>
80 
81 #define MAXBUFSTRUCTSIZE	((size_t)512 * 1024 * 1024)
82 
83 #if NISA > 0
84 #include <bus/isa/isavar.h>
85 
86 device_t isa_bus_device = NULL;
87 #endif
88 
89 static void cpu_startup (void *);
90 static void configure_first (void *);
91 static void configure (void *);
92 static void configure_final (void *);
93 
94 #if defined(NFS) && defined(NFS_ROOT)
95 #if !defined(BOOTP_NFSROOT)
96 static void	pxe_setup_nfsdiskless(void);
97 #endif
98 #endif
99 
100 SYSINIT(cpu, SI_BOOT2_START_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
101 SYSINIT(configure1, SI_SUB_CONFIGURE, SI_ORDER_FIRST, configure_first, NULL);
102 /* SI_ORDER_SECOND is hookable */
103 SYSINIT(configure2, SI_SUB_CONFIGURE, SI_ORDER_THIRD, configure, NULL);
104 /* SI_ORDER_MIDDLE is hookable */
105 SYSINIT(configure3, SI_SUB_CONFIGURE, SI_ORDER_ANY, configure_final, NULL);
106 
107 cdev_t	rootdev = NULL;
108 cdev_t	dumpdev = NULL;
109 
110 /*
111  * nfsroot.iosize may be set in loader.conf, 32768 is recommended to
112  * be able to max-out a GigE link if the server supports it.  Many servers
113  * do not so the default is 8192.
114  *
115  * nfsroot.rahead defaults to something reasonable, can be overridden.
116  */
117 static int nfsroot_iosize = 8192;
118 TUNABLE_INT("nfsroot.iosize", &nfsroot_iosize);
119 static int nfsroot_rahead = 4;
120 TUNABLE_INT("nfsroot.rahead", &nfsroot_rahead);
121 
122 /*
123  *
124  */
125 static void
126 cpu_startup(void *dummy)
127 {
128 	vm_offset_t buffer_sva;
129 	vm_offset_t buffer_eva;
130 	vm_offset_t pager_sva;
131 	vm_offset_t pager_eva;
132 
133 	kprintf("%s", version);
134 	kprintf("real memory = %ju (%juK bytes)\n",
135 	    (uintmax_t)ptoa(Maxmem), (uintmax_t)(ptoa(Maxmem) / 1024));
136 
137        if (nbuf == 0) {
138                 long factor = NBUFCALCSIZE / 1024;              /* KB/nbuf */
139                 long kbytes = physmem * (PAGE_SIZE / 1024);     /* physmem */
140 
141                 nbuf = 50;
142 
143                 if (kbytes > 128 * 1024)
144                         nbuf += (kbytes - 128 * 1024) / (factor * 20);
145                 if (maxbcache && nbuf > maxbcache / NBUFCALCSIZE)
146                         nbuf = maxbcache / NBUFCALCSIZE;
147 
148 		if ((size_t)nbuf * sizeof(struct buf) > MAXBUFSTRUCTSIZE) {
149 			kprintf("Warning: nbuf capped at %ld due to the "
150 				"reasonability limit\n", nbuf);
151 			nbuf = MAXBUFSTRUCTSIZE / sizeof(struct buf);
152 		}
153         }
154 
155 	if (nbuf > (virtual_end - virtual_start) / (MAXBSIZE * 4)) {
156 		nbuf = (virtual_end - virtual_start) / (MAXBSIZE * 4);
157 		kprintf("Warning: nbufs capped at %ld for "
158 			"valloc considerations\n", nbuf);
159 	}
160 
161 	nswbuf_mem = lmax(lmin(nbuf / 32, 32), 4);
162 #ifdef NSWBUF_MIN
163 	if (nswbuf_mem < NSWBUF_MIN)
164 		nswbuf_mem = NSWBUF_MIN;
165 #endif
166 	nswbuf_kva = lmax(lmin(nbuf / 4, 256), 16);
167 #ifdef NSWBUF_MIN
168 	if (nswbuf_kva < NSWBUF_MIN)
169 		nswbuf_kva = NSWBUF_MIN;
170 #endif
171 
172 	/*
173 	 * Allocate memory for the buffer cache
174 	 */
175 	buf = (void *)kmem_alloc(kernel_map,
176 				 nbuf * sizeof(struct buf),
177 				 VM_SUBSYS_BUF);
178 	swbuf_mem = (void *)kmem_alloc(kernel_map,
179 				       nswbuf_mem * sizeof(struct buf),
180 				       VM_SUBSYS_BUF);
181 	swbuf_kva = (void *)kmem_alloc(kernel_map,
182 				       nswbuf_kva * sizeof(struct buf),
183 				       VM_SUBSYS_BUF);
184 
185 	kmem_suballoc(kernel_map, clean_map, &clean_sva, &clean_eva,
186 		      (nbuf * MAXBSIZE * 2) +
187 		      (nswbuf_mem + nswbuf_kva) * MAXPHYS +
188 		      pager_map_size);
189 	kmem_suballoc(clean_map, buffer_map, &buffer_sva, &buffer_eva,
190 		      (nbuf * MAXBSIZE * 2));
191 	buffer_map->system_map = 1;
192 	kmem_suballoc(clean_map, pager_map, &pager_sva, &pager_eva,
193 		      (nswbuf_mem + nswbuf_kva) * MAXPHYS +
194 		      pager_map_size);
195 	pager_map->system_map = 1;
196 	kprintf("avail memory = %lu (%luK bytes)\n", ptoa(vmstats.v_free_count),
197 		ptoa(vmstats.v_free_count) / 1024);
198 	mp_start();
199 	mp_announce();
200 	cpu_setregs();
201 }
202 
203 /*
204  * Determine i/o configuration for a machine.
205  */
206 static void
207 configure_first(void *dummy)
208 {
209 }
210 
211 static void
212 configure(void *dummy)
213 {
214         /*
215 	 * Final interrupt support acviation, then enable hardware interrupts.
216 	 */
217 	MachIntrABI.finalize();
218 	cpu_enable_intr();
219 
220 	/*
221 	 * This will configure all devices, generally starting with the
222 	 * nexus (pc64/x86_64/nexus.c).  The nexus ISA code explicitly
223 	 * dummies up the attach in order to delay legacy initialization
224 	 * until after all other busses/subsystems have had a chance
225 	 * at those resources.
226 	 */
227 	root_bus_configure();
228 
229 #if NISA > 0
230 	/*
231 	 * Explicitly probe and attach ISA last.  The isa bus saves
232 	 * it's device node at attach time for us here.
233 	 */
234 	if (isa_bus_device)
235 		isa_probe_children(isa_bus_device);
236 #endif
237 
238 	/*
239 	 * Allow lowering of the ipl to the lowest kernel level if we
240 	 * panic (or call tsleep() before clearing `cold').  No level is
241 	 * completely safe (since a panic may occur in a critical region
242 	 * at splhigh()), but we want at least bio interrupts to work.
243 	 */
244 	safepri = TDPRI_KERN_USER;
245 }
246 
247 /*
248  * Finalize configure.  Reprobe for the console, in case it was one
249  * of the devices which attached, then finish console initialization.
250  */
251 static void
252 configure_final(void *dummy)
253 {
254 	cninit();
255 	cninit_finish();
256 
257 	if (bootverbose)
258 		kprintf("Device configuration finished.\n");
259 }
260 
261 #ifdef BOOTP
262 void bootpc_init(void);
263 #endif
264 /*
265  * Do legacy root filesystem discovery.
266  */
267 void
268 cpu_rootconf(void)
269 {
270 #ifdef BOOTP
271         bootpc_init();
272 #endif
273 #if defined(NFS) && defined(NFS_ROOT)
274 #if !defined(BOOTP_NFSROOT)
275 	pxe_setup_nfsdiskless();
276 	if (nfs_diskless_valid)
277 #endif
278 		rootdevnames[0] = "nfs:";
279 #endif
280 }
281 SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, cpu_rootconf, NULL);
282 
283 #if defined(NFS) && defined(NFS_ROOT)
284 #if !defined(BOOTP_NFSROOT)
285 
286 #include <sys/socket.h>
287 #include <net/if.h>
288 #include <net/if_dl.h>
289 #include <net/if_types.h>
290 #include <net/if_var.h>
291 #include <net/ethernet.h>
292 #include <netinet/in.h>
293 #include <vfs/nfs/rpcv2.h>
294 #include <vfs/nfs/nfsproto.h>
295 #include <vfs/nfs/nfs.h>
296 #include <vfs/nfs/nfsdiskless.h>
297 
298 extern struct nfs_diskless	nfs_diskless;
299 
300 /*
301  * Convert a kenv variable to a sockaddr.  If the kenv variable does not
302  * exist the sockaddr will remain zerod out (callers typically just check
303  * sin_len).  A network address of 0.0.0.0 is equivalent to failure.
304  */
305 static int
306 inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
307 {
308 	u_int32_t	a[4];
309 	char		*cp;
310 
311 	bzero(sa, sizeof(*sa));
312 
313 	if ((cp = kgetenv(ev)) == NULL)
314 		return(1);
315 	if (ksscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]) != 4)
316 		return(1);
317 	if (a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0)
318 		return(1);
319 	/* XXX is this ordering correct? */
320 	sa->sin_addr.s_addr = (a[3] << 24) + (a[2] << 16) + (a[1] << 8) + a[0];
321 	sa->sin_len = sizeof(*sa);
322 	sa->sin_family = AF_INET;
323 	return(0);
324 }
325 
326 static int
327 hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
328 {
329 	char		*cp;
330 	u_int32_t	a[6];
331 
332 	bzero(sa, sizeof(*sa));
333 	sa->sdl_len = sizeof(*sa);
334 	sa->sdl_family = AF_LINK;
335 	sa->sdl_type = IFT_ETHER;
336 	sa->sdl_alen = ETHER_ADDR_LEN;
337 	if ((cp = kgetenv(ev)) == NULL)
338 		return(1);
339 	if (ksscanf(cp, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]) != 6)
340 		return(1);
341 	sa->sdl_data[0] = a[0];
342 	sa->sdl_data[1] = a[1];
343 	sa->sdl_data[2] = a[2];
344 	sa->sdl_data[3] = a[3];
345 	sa->sdl_data[4] = a[4];
346 	sa->sdl_data[5] = a[5];
347 	return(0);
348 }
349 
350 static int
351 decode_nfshandle(char *ev, u_char *fh)
352 {
353 	u_char	*cp;
354 	int	len, val;
355 
356 	if (((cp = kgetenv(ev)) == NULL) || (strlen(cp) < 2) || (*cp != 'X'))
357 		return(0);
358 	len = 0;
359 	cp++;
360 	for (;;) {
361 		if (*cp == 'X')
362 			return(len);
363 		if ((ksscanf(cp, "%2x", &val) != 1) || (val > 0xff))
364 			return(0);
365 		*(fh++) = val;
366 		len++;
367 		cp += 2;
368 		if (len > NFSX_V2FH)
369 		    return(0);
370 	}
371 }
372 
373 /*
374  * Populate the essential fields in the nfsv3_diskless structure.
375  *
376  * The loader is expected to export the following environment variables:
377  *
378  * boot.netif.ip		IP address on boot interface
379  * boot.netif.netmask		netmask on boot interface
380  * boot.netif.gateway		default gateway (optional)
381  * boot.netif.hwaddr		hardware address of boot interface
382  * boot.netif.name		name of boot interface (instead of hw addr)
383  * boot.nfsroot.server		IP address of root filesystem server
384  * boot.nfsroot.path		path of the root filesystem on server
385  * boot.nfsroot.nfshandle	NFS handle for root filesystem on server
386  */
387 static void
388 pxe_setup_nfsdiskless(void)
389 {
390 	struct nfs_diskless	*nd = &nfs_diskless;
391 	struct ifnet		*ifp;
392 	struct ifaddr		*ifa;
393 	struct sockaddr_dl	*sdl, ourdl;
394 	struct sockaddr_in	myaddr, netmask;
395 	char			*cp;
396 
397 	/* set up interface */
398 	if (inaddr_to_sockaddr("boot.netif.ip", &myaddr))
399 		return;
400 	if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) {
401 		kprintf("PXE: no netmask\n");
402 		return;
403 	}
404 	bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr));
405 	bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr));
406 	((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
407 		myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
408 	bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask));
409 
410 	if ((cp = kgetenv("boot.netif.name")) != NULL) {
411 		ifnet_lock();
412 		ifp = ifunit(cp);
413 		if (ifp) {
414 			strlcpy(nd->myif.ifra_name, ifp->if_xname,
415 			    sizeof(nd->myif.ifra_name));
416 			ifnet_unlock();
417 			goto match_done;
418 		}
419 		ifnet_unlock();
420 		kprintf("PXE: cannot find interface %s\n", cp);
421 		return;
422 	}
423 
424 	if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) {
425 		kprintf("PXE: no hardware address\n");
426 		return;
427 	}
428 	ifa = NULL;
429 	ifnet_lock();
430 	TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
431 		struct ifaddr_container *ifac;
432 
433 		TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
434 			ifa = ifac->ifa;
435 
436 			if ((ifa->ifa_addr->sa_family == AF_LINK) &&
437 			    (sdl = ((struct sockaddr_dl *)ifa->ifa_addr))) {
438 				if ((sdl->sdl_type == ourdl.sdl_type) &&
439 				    (sdl->sdl_alen == ourdl.sdl_alen) &&
440 				    !bcmp(sdl->sdl_data + sdl->sdl_nlen,
441 					  ourdl.sdl_data + ourdl.sdl_nlen,
442 					  sdl->sdl_alen)) {
443 					strlcpy(nd->myif.ifra_name,
444 					    ifp->if_xname,
445 					    sizeof(nd->myif.ifra_name));
446 					ifnet_unlock();
447 					goto match_done;
448 				}
449 			}
450 		}
451 	}
452 	ifnet_unlock();
453 	kprintf("PXE: no interface\n");
454 	return;	/* no matching interface */
455 match_done:
456 	/* set up gateway */
457 	inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway);
458 
459 	/* XXX set up swap? */
460 
461 	/* set up root mount */
462 	nd->root_args.rsize = nfsroot_iosize;
463 	nd->root_args.wsize = nfsroot_iosize;
464 	nd->root_args.sotype = SOCK_STREAM;
465 	nd->root_args.readahead = nfsroot_rahead;
466 	nd->root_args.flags = NFSMNT_WSIZE | NFSMNT_RSIZE | NFSMNT_RESVPORT |
467 			      NFSMNT_READAHEAD;
468 	if (inaddr_to_sockaddr("boot.nfsroot.server", &nd->root_saddr)) {
469 		kprintf("PXE: no server\n");
470 		return;
471 	}
472 	nd->root_saddr.sin_port = htons(NFS_PORT);
473 
474 	/*
475 	 * A tftp-only loader may pass NFS path information without a
476 	 * root handle.  Generate a warning but continue configuring.
477 	 */
478 	if (decode_nfshandle("boot.nfsroot.nfshandle", &nd->root_fh[0]) == 0) {
479 		kprintf("PXE: Warning, no NFS handle passed from loader\n");
480 	}
481 	if ((cp = kgetenv("boot.nfsroot.path")) != NULL)
482 		strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
483 
484 	nfs_diskless_valid = 1;
485 }
486 
487 #endif
488 #endif
489