Finish migrating the cpl into the thread structure.
[dragonfly.git] / sys / i386 / i386 / autoconf.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * William Jolitz.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from: @(#)autoconf.c 7.1 (Berkeley) 5/9/91
37 * $FreeBSD: src/sys/i386/i386/autoconf.c,v 1.146.2.2 2001/06/07 06:05:58 dd Exp $
8f41e33b 38 * $DragonFly: src/sys/i386/i386/Attic/autoconf.c,v 1.3 2003/06/22 08:54:18 dillon Exp $
984263bc
MD
39 */
40
41/*
42 * Setup the system to run on the current machine.
43 *
44 * Configure() is called at boot time and initializes the vba
45 * device tables and the memory controller monitoring. Available
46 * devices are determined (from possibilities mentioned in ioconf.c),
47 * and the drivers are initialized.
48 */
49#include "opt_bootp.h"
50#include "opt_ffs.h"
51#include "opt_cd9660.h"
52#include "opt_nfs.h"
53#include "opt_nfsroot.h"
54#include "opt_bus.h"
55#include "opt_rootdevname.h"
56
57#include "isa.h"
58
59#include <sys/param.h>
60#include <sys/systm.h>
61#include <sys/bus.h>
62#include <sys/conf.h>
63#include <sys/disklabel.h>
64#include <sys/diskslice.h>
65#include <sys/reboot.h>
66#include <sys/kernel.h>
67#include <sys/malloc.h>
68#include <sys/mount.h>
69#include <sys/cons.h>
8f41e33b 70#include <sys/thread.h>
984263bc
MD
71
72#include <machine/bootinfo.h>
73#include <machine/ipl.h>
74#include <machine/md_var.h>
75#ifdef APIC_IO
76#include <machine/smp.h>
77#else
78#include <i386/isa/icu.h>
79#endif /* APIC_IO */
80
8f41e33b
MD
81#include <machine/pcb.h>
82#include <machine/pcb_ext.h>
83#include <machine/vm86.h>
84#include <machine/globaldata.h>
85#include <machine/globals.h>
86
984263bc
MD
87#if NISA > 0
88#include <isa/isavar.h>
89
90device_t isa_bus_device = 0;
91#endif
92
93static void configure_first __P((void *));
94static void configure __P((void *));
95static void configure_final __P((void *));
96
97#if defined(FFS) && defined(FFS_ROOT)
98static void setroot __P((void));
99#endif
100
101#if defined(NFS) && defined(NFS_ROOT)
102static void pxe_setup_nfsdiskless(void);
103#endif
104
105SYSINIT(configure1, SI_SUB_CONFIGURE, SI_ORDER_FIRST, configure_first, NULL);
106/* SI_ORDER_SECOND is hookable */
107SYSINIT(configure2, SI_SUB_CONFIGURE, SI_ORDER_THIRD, configure, NULL);
108/* SI_ORDER_MIDDLE is hookable */
109SYSINIT(configure3, SI_SUB_CONFIGURE, SI_ORDER_ANY, configure_final, NULL);
110
111dev_t rootdev = NODEV;
112dev_t dumpdev = NODEV;
113
114device_t nexus_dev;
115
116/*
117 * Determine i/o configuration for a machine.
118 */
119static void
120configure_first(dummy)
121 void *dummy;
122{
123}
124
125static void
126configure(dummy)
127 void *dummy;
128{
129
130 /*
131 * Activate the ICU's. Note that we are explicitly at splhigh()
132 * at present as we have no way to disable stray PCI level triggered
133 * interrupts until the devices have had a driver attached. This
134 * is particularly a problem when the interrupts are shared. For
135 * example, if IRQ 10 is shared between a disk and network device
136 * and the disk device generates an interrupt, if we "activate"
137 * IRQ 10 when the network driver is set up, then we will get
138 * recursive interrupt 10's as nothing will know how to turn off
139 * the disk device's interrupt.
140 *
141 * Having the ICU's active means we can probe interrupt routing to
142 * see if a device causes the corresponding pending bit to be set.
143 *
144 * This is all rather inconvenient.
145 */
146#ifdef APIC_IO
147 bsp_apic_configure();
148 enable_intr();
149#else
150 enable_intr();
151 INTREN(IRQ_SLAVE);
152#endif /* APIC_IO */
153
154 /* nexus0 is the top of the i386 device tree */
155 device_add_child(root_bus, "nexus", 0);
156
157 /* initialize new bus architecture */
158 root_bus_configure();
159
160#if NISA > 0
161 /*
162 * Explicitly probe and attach ISA last. The isa bus saves
163 * it's device node at attach time for us here.
164 */
165 if (isa_bus_device)
166 isa_probe_children(isa_bus_device);
167#endif
168
169 /*
170 * Now we're ready to handle (pending) interrupts.
171 * XXX this is slightly misplaced.
172 */
173 spl0();
174
175 /*
176 * Allow lowering of the ipl to the lowest kernel level if we
177 * panic (or call tsleep() before clearing `cold'). No level is
178 * completely safe (since a panic may occur in a critical region
179 * at splhigh()), but we want at least bio interrupts to work.
180 */
8f41e33b 181 safepri = curthread->td_cpl;
984263bc
MD
182}
183
184static void
185configure_final(dummy)
186 void *dummy;
187{
188 int i;
189
190 cninit_finish();
191
192 if (bootverbose) {
193
194#ifdef APIC_IO
195 imen_dump();
196#endif /* APIC_IO */
197
198 /*
199 * Print out the BIOS's idea of the disk geometries.
200 */
201 printf("BIOS Geometries:\n");
202 for (i = 0; i < N_BIOS_GEOM; i++) {
203 unsigned long bios_geom;
204 int max_cylinder, max_head, max_sector;
205
206 bios_geom = bootinfo.bi_bios_geom[i];
207
208 /*
209 * XXX the bootstrap punts a 1200K floppy geometry
210 * when the get-disk-geometry interrupt fails. Skip
211 * drives that have this geometry.
212 */
213 if (bios_geom == 0x4f010f)
214 continue;
215
216 printf(" %x:%08lx ", i, bios_geom);
217 max_cylinder = bios_geom >> 16;
218 max_head = (bios_geom >> 8) & 0xff;
219 max_sector = bios_geom & 0xff;
220 printf(
221 "0..%d=%d cylinders, 0..%d=%d heads, 1..%d=%d sectors\n",
222 max_cylinder, max_cylinder + 1,
223 max_head, max_head + 1,
224 max_sector, max_sector);
225 }
226 printf(" %d accounted for\n", bootinfo.bi_n_bios_used);
227
228 printf("Device configuration finished.\n");
229 }
230 cold = 0;
231}
232
233#ifdef BOOTP
234extern void bootpc_init(void);
235#endif
236/*
237 * Do legacy root filesystem discovery.
238 */
239void
240cpu_rootconf()
241{
242#ifdef BOOTP
243 bootpc_init();
244#endif
245#if defined(NFS) && defined(NFS_ROOT)
246#if !defined(BOOTP_NFSROOT)
247 pxe_setup_nfsdiskless();
248 if (nfs_diskless_valid)
249#endif
250 rootdevnames[0] = "nfs:";
251#endif
252#if defined(FFS) && defined(FFS_ROOT)
253 if (!rootdevnames[0])
254 setroot();
255#endif
256}
257SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, cpu_rootconf, NULL)
258
259u_long bootdev = 0; /* not a dev_t - encoding is different */
260
261#if defined(FFS) && defined(FFS_ROOT)
262#define FDMAJOR 2
263#define FDUNITSHIFT 6
264
265/*
266 * Attempt to find the device from which we were booted.
267 * If we can do so, and not instructed not to do so,
268 * set rootdevs[] and rootdevnames[] to correspond to the
269 * boot device(s).
270 *
271 * This code survives in order to allow the system to be
272 * booted from legacy environments that do not correctly
273 * populate the kernel environment. There are significant
274 * restrictions on the bootability of the system in this
275 * situation; it can only be mounting root from a 'da'
276 * 'wd' or 'fd' device, and the root filesystem must be ufs.
277 */
278static void
279setroot()
280{
281 int majdev, mindev, unit, slice, part;
282 dev_t newrootdev, dev;
283 char partname[2];
284 char *sname;
285
286 if ((bootdev & B_MAGICMASK) != B_DEVMAGIC) {
287 printf("no B_DEVMAGIC (bootdev=%#lx)\n", bootdev);
288 return;
289 }
290 majdev = B_TYPE(bootdev);
291 dev = makebdev(majdev, 0);
292 if (devsw(dev) == NULL)
293 return;
294 unit = B_UNIT(bootdev);
295 slice = B_SLICE(bootdev);
296 if (slice == WHOLE_DISK_SLICE)
297 slice = COMPATIBILITY_SLICE;
298 if (slice < 0 || slice >= MAX_SLICES) {
299 printf("bad slice\n");
300 return;
301 }
302
303 /*
304 * XXX kludge for inconsistent unit numbering and lack of slice
305 * support for floppies.
306 */
307 if (majdev == FDMAJOR) {
308 slice = COMPATIBILITY_SLICE;
309 part = RAW_PART;
310 mindev = unit << FDUNITSHIFT;
311 } else {
312 part = B_PARTITION(bootdev);
313 mindev = dkmakeminor(unit, slice, part);
314 }
315
316 newrootdev = makebdev(majdev, mindev);
317 sname = dsname(newrootdev, unit, slice, part, partname);
318 rootdevnames[0] = malloc(strlen(sname) + 6, M_DEVBUF, M_NOWAIT);
319 sprintf(rootdevnames[0], "ufs:%s%s", sname, partname);
320
321 /*
322 * For properly dangerously dedicated disks (ones with a historical
323 * bogus partition table), the boot blocks will give slice = 4, but
324 * the kernel will only provide the compatibility slice since it
325 * knows that slice 4 is not a real slice. Arrange to try mounting
326 * the compatibility slice as root if mounting the slice passed by
327 * the boot blocks fails. This handles the dangerously dedicated
328 * case and perhaps others.
329 */
330 if (slice == COMPATIBILITY_SLICE)
331 return;
332 slice = COMPATIBILITY_SLICE;
333 sname = dsname(newrootdev, unit, slice, part, partname);
334 rootdevnames[1] = malloc(strlen(sname) + 6, M_DEVBUF, M_NOWAIT);
335 sprintf(rootdevnames[1], "ufs:%s%s", sname, partname);
336}
337#endif
338
339#if defined(NFS) && defined(NFS_ROOT)
340
341#include <sys/socket.h>
342#include <net/if.h>
343#include <net/if_dl.h>
344#include <net/if_types.h>
345#include <net/if_var.h>
346#include <net/ethernet.h>
347#include <netinet/in.h>
348#include <nfs/rpcv2.h>
349#include <nfs/nfsproto.h>
350#include <nfs/nfs.h>
351#include <nfs/nfsdiskless.h>
352
353extern struct nfs_diskless nfs_diskless;
354
355static int
356inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
357{
358 u_int32_t a[4];
359 char *cp;
360
361 bzero(sa, sizeof(*sa));
362 sa->sin_len = sizeof(*sa);
363 sa->sin_family = AF_INET;
364
365 if ((cp = getenv(ev)) == NULL)
366 return(1);
367 if (sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]) != 4)
368 return(1);
369 /* XXX is this ordering correct? */
370 sa->sin_addr.s_addr = (a[3] << 24) + (a[2] << 16) + (a[1] << 8) + a[0];
371 return(0);
372}
373
374static int
375hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
376{
377 char *cp;
378 u_int32_t a[6];
379
380 bzero(sa, sizeof(*sa));
381 sa->sdl_len = sizeof(*sa);
382 sa->sdl_family = AF_LINK;
383 sa->sdl_type = IFT_ETHER;
384 sa->sdl_alen = ETHER_ADDR_LEN;
385 if ((cp = getenv(ev)) == NULL)
386 return(1);
387 if (sscanf(cp, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]) != 6)
388 return(1);
389 sa->sdl_data[0] = a[0];
390 sa->sdl_data[1] = a[1];
391 sa->sdl_data[2] = a[2];
392 sa->sdl_data[3] = a[3];
393 sa->sdl_data[4] = a[4];
394 sa->sdl_data[5] = a[5];
395 return(0);
396}
397
398static int
399decode_nfshandle(char *ev, u_char *fh)
400{
401 u_char *cp;
402 int len, val;
403
404 if (((cp = getenv(ev)) == NULL) || (strlen(cp) < 2) || (*cp != 'X'))
405 return(0);
406 len = 0;
407 cp++;
408 for (;;) {
409 if (*cp == 'X')
410 return(len);
411 if ((sscanf(cp, "%2x", &val) != 1) || (val > 0xff))
412 return(0);
413 *(fh++) = val;
414 len++;
415 cp += 2;
416 if (len > NFSX_V2FH)
417 return(0);
418 }
419}
420
421/*
422 * Populate the essential fields in the nfsv3_diskless structure.
423 *
424 * The loader is expected to export the following environment variables:
425 *
426 * boot.netif.ip IP address on boot interface
427 * boot.netif.netmask netmask on boot interface
428 * boot.netif.gateway default gateway (optional)
429 * boot.netif.hwaddr hardware address of boot interface
430 * boot.nfsroot.server IP address of root filesystem server
431 * boot.nfsroot.path path of the root filesystem on server
432 * boot.nfsroot.nfshandle NFS handle for root filesystem on server
433 */
434static void
435pxe_setup_nfsdiskless()
436{
437 struct nfs_diskless *nd = &nfs_diskless;
438 struct ifnet *ifp;
439 struct ifaddr *ifa;
440 struct sockaddr_dl *sdl, ourdl;
441 struct sockaddr_in myaddr, netmask;
442 char *cp;
443
444 /* set up interface */
445 if (inaddr_to_sockaddr("boot.netif.ip", &myaddr))
446 return;
447 if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) {
448 printf("PXE: no netmask\n");
449 return;
450 }
451 bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr));
452 bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr));
453 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
454 myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
455 bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask));
456
457 if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) {
458 printf("PXE: no hardware address\n");
459 return;
460 }
461 ifa = NULL;
462 ifp = TAILQ_FIRST(&ifnet);
463 TAILQ_FOREACH(ifp, &ifnet, if_link) {
464 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
465 if ((ifa->ifa_addr->sa_family == AF_LINK) &&
466 (sdl = ((struct sockaddr_dl *)ifa->ifa_addr))) {
467 if ((sdl->sdl_type == ourdl.sdl_type) &&
468 (sdl->sdl_alen == ourdl.sdl_alen) &&
469 !bcmp(sdl->sdl_data + sdl->sdl_nlen,
470 ourdl.sdl_data + ourdl.sdl_nlen,
471 sdl->sdl_alen))
472 goto match_done;
473 }
474 }
475 }
476 printf("PXE: no interface\n");
477 return; /* no matching interface */
478match_done:
479 sprintf(nd->myif.ifra_name, "%s%d", ifp->if_name, ifp->if_unit);
480
481
482 /* set up gateway */
483 inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway);
484
485 /* XXX set up swap? */
486
487 /* set up root mount */
488 nd->root_args.rsize = 8192; /* XXX tunable? */
489 nd->root_args.wsize = 8192;
490 nd->root_args.sotype = SOCK_DGRAM;
491 nd->root_args.flags = (NFSMNT_WSIZE | NFSMNT_RSIZE | NFSMNT_RESVPORT);
492 if (inaddr_to_sockaddr("boot.nfsroot.server", &nd->root_saddr)) {
493 printf("PXE: no server\n");
494 return;
495 }
496 nd->root_saddr.sin_port = htons(NFS_PORT);
497 if (decode_nfshandle("boot.nfsroot.nfshandle", &nd->root_fh[0]) == 0) {
498 printf("PXE: no NFS handle\n");
499 return;
500 }
501 if ((cp = getenv("boot.nfsroot.path")) != NULL)
502 strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
503
504 nfs_diskless_valid = 1;
505}
506#endif