a84587a2daeca015e8f3f89b567f3066a2ee23a2
[dragonfly.git] / sys / platform / pc64 / x86_64 / autoconf.c
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. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by the University of
20  *      California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *      from: @(#)autoconf.c    7.1 (Berkeley) 5/9/91
38  * $FreeBSD: src/sys/i386/i386/autoconf.c,v 1.146.2.2 2001/06/07 06:05:58 dd Exp $
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_rootdevname.h"
55
56 #include "use_isa.h"
57
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/bootmaj.h>
61 #include <sys/bus.h>
62 #include <sys/conf.h>
63 #include <sys/diskslice.h>
64 #include <sys/reboot.h>
65 #include <sys/kernel.h>
66 #include <sys/malloc.h>
67 #include <sys/mount.h>
68 #include <sys/cons.h>
69 #include <sys/thread.h>
70 #include <sys/device.h>
71 #include <sys/machintr.h>
72
73 #include <machine/bootinfo.h>
74 #include <machine/md_var.h>
75 #include <machine/smp.h>
76 #include <machine_base/icu/icu.h>
77
78 #include <machine/pcb.h>
79 #include <machine/pcb_ext.h>
80 #include <machine/globaldata.h>
81
82 #if NISA > 0
83 #include <bus/isa/isavar.h>
84
85 device_t isa_bus_device = NULL;
86 #endif
87
88 static void     configure_first (void *);
89 static void     configure (void *);
90 static void     configure_final (void *);
91
92 #if defined(FFS) && defined(FFS_ROOT)
93 static void     setroot (void);
94 #endif
95
96 #if defined(NFS) && defined(NFS_ROOT)
97 #if !defined(BOOTP_NFSROOT)
98 static void     pxe_setup_nfsdiskless(void);
99 #endif
100 #endif
101
102 SYSINIT(configure1, SI_SUB_CONFIGURE, SI_ORDER_FIRST, configure_first, NULL);
103 /* SI_ORDER_SECOND is hookable */
104 SYSINIT(configure2, SI_SUB_CONFIGURE, SI_ORDER_THIRD, configure, NULL);
105 /* SI_ORDER_MIDDLE is hookable */
106 SYSINIT(configure3, SI_SUB_CONFIGURE, SI_ORDER_ANY, configure_final, NULL);
107
108 cdev_t  rootdev = NULL;
109 cdev_t  dumpdev = NULL;
110
111 /*
112  * nfsroot.iosize may be set in loader.conf, 32768 is recommended to
113  * be able to max-out a GigE link if the server supports it.  Many servers
114  * do not so the default is 8192.
115  *
116  * nfsroot.rahead defaults to something reasonable, can be overridden.
117  */
118 static int nfsroot_iosize = 8192;
119 TUNABLE_INT("nfsroot.iosize", &nfsroot_iosize);
120 static int nfsroot_rahead = 4;
121 TUNABLE_INT("nfsroot.rahead", &nfsroot_rahead);
122
123 /*
124  * Determine i/o configuration for a machine.
125  */
126 static void
127 configure_first(void *dummy)
128 {
129 }
130
131 static void
132 configure(void *dummy)
133 {
134         /*
135          * This will configure all devices, generally starting with the
136          * nexus (i386/i386/nexus.c).  The nexus ISA code explicitly
137          * dummies up the attach in order to delay legacy initialization
138          * until after all other busses/subsystems have had a chance
139          * at those resources.
140          */
141         root_bus_configure();
142
143 #if NISA > 0
144         /*
145          * Explicitly probe and attach ISA last.  The isa bus saves
146          * it's device node at attach time for us here.
147          */
148         if (isa_bus_device)
149                 isa_probe_children(isa_bus_device);
150 #endif
151
152         /*
153          * Allow lowering of the ipl to the lowest kernel level if we
154          * panic (or call tsleep() before clearing `cold').  No level is
155          * completely safe (since a panic may occur in a critical region
156          * at splhigh()), but we want at least bio interrupts to work.
157          */
158         safepri = TDPRI_KERN_USER;
159 }
160
161 static void
162 configure_final(void *dummy)
163 {
164         cninit_finish();
165
166         if (bootverbose) {
167 #if 0 /* JG */
168                 /*
169                  * Print out the BIOS's idea of the disk geometries.
170                  */
171                 int i;
172                 kprintf("BIOS Geometries:\n");
173                 for (i = 0; i < N_BIOS_GEOM; i++) {
174                         unsigned long bios_geom;
175                         int max_cylinder, max_head, max_sector;
176
177                         bios_geom = bootinfo.bi_bios_geom[i];
178
179                         /*
180                          * XXX the bootstrap punts a 1200K floppy geometry
181                          * when the get-disk-geometry interrupt fails.  Skip
182                          * drives that have this geometry.
183                          */
184                         if (bios_geom == 0x4f010f)
185                                 continue;
186
187                         kprintf(" %x:%08lx ", i, bios_geom);
188                         max_cylinder = bios_geom >> 16;
189                         max_head = (bios_geom >> 8) & 0xff;
190                         max_sector = bios_geom & 0xff;
191                         kprintf(
192                 "0..%d=%d cylinders, 0..%d=%d heads, 1..%d=%d sectors\n",
193                                max_cylinder, max_cylinder + 1,
194                                max_head, max_head + 1,
195                                max_sector, max_sector);
196                 }
197                 kprintf(" %d accounted for\n", bootinfo.bi_n_bios_used);
198
199                 kprintf("Device configuration finished.\n");
200 #endif
201         }
202 }
203
204 #ifdef BOOTP
205 void bootpc_init(void);
206 #endif
207 /*
208  * Do legacy root filesystem discovery.
209  */
210 void
211 cpu_rootconf(void)
212 {
213 #ifdef BOOTP
214         bootpc_init();
215 #endif
216 #if defined(NFS) && defined(NFS_ROOT)
217 #if !defined(BOOTP_NFSROOT)
218         pxe_setup_nfsdiskless();
219         if (nfs_diskless_valid)
220 #endif
221                 rootdevnames[0] = "nfs:";
222 #endif
223 #if defined(FFS) && defined(FFS_ROOT)
224         if (!rootdevnames[0])
225                 setroot();
226 #endif
227 }
228 SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, cpu_rootconf, NULL);
229
230 u_long  bootdev = 0;            /* not a cdev_t - encoding is different */
231
232 #if defined(FFS) && defined(FFS_ROOT)
233 #define FDMAJOR         2
234 #define FDUNITSHIFT     6
235
236 /*
237  * The boot code uses old block device major numbers to pass bootdev to
238  * us.  We have to translate these to character device majors because
239  * we don't have block devices any more.
240  */
241 static int
242 boot_translate_majdev(int bmajor)
243 {
244         static int conv[] = { BOOTMAJOR_CONVARY };
245
246         if (bmajor >= 0 && bmajor < NELEM(conv))
247                 return(conv[bmajor]);
248         return(-1);
249 }
250
251 /*
252  * Attempt to find the device from which we were booted.
253  * If we can do so, and not instructed not to do so,
254  * set rootdevs[] and rootdevnames[] to correspond to the
255  * boot device(s).
256  *
257  * This code survives in order to allow the system to be 
258  * booted from legacy environments that do not correctly
259  * populate the kernel environment. There are significant
260  * restrictions on the bootability of the system in this
261  * situation; it can only be mounting root from a 'da'
262  * 'wd' or 'fd' device, and the root filesystem must be ufs.
263  */
264 static void
265 setroot(void)
266 {
267         int majdev, mindev, unit, slice, part;
268         cdev_t newrootdev, dev;
269         char partname[2];
270         char *sname;
271
272         if ((bootdev & B_MAGICMASK) != B_DEVMAGIC) {
273                 kprintf("no B_DEVMAGIC (bootdev=%#lx)\n", bootdev);
274                 return;
275         }
276         majdev = boot_translate_majdev(B_TYPE(bootdev));
277         if (bootverbose) {
278                 kprintf("bootdev: %08lx type=%ld unit=%ld "
279                         "slice=%ld part=%ld major=%d\n",
280                         bootdev, B_TYPE(bootdev), B_UNIT(bootdev),
281                         B_SLICE(bootdev), B_PARTITION(bootdev), majdev);
282         }
283         dev = udev2dev(makeudev(majdev, 0), 0);
284         if (!dev_is_good(dev))
285                 return;
286         unit = B_UNIT(bootdev);
287         slice = B_SLICE(bootdev);
288         if (slice == WHOLE_DISK_SLICE)
289                 slice = COMPATIBILITY_SLICE;
290         if (slice < 0 || slice >= MAX_SLICES) {
291                 kprintf("bad slice\n");
292                 return;
293         }
294
295         part = B_PARTITION(bootdev);
296         mindev = dkmakeminor(unit, slice, part);
297         newrootdev = udev2dev(makeudev(majdev, mindev), 0);
298         if (!dev_is_good(newrootdev))
299                 return;
300         sname = dsname(newrootdev, unit, slice, part, partname);
301         rootdevnames[0] = kmalloc(strlen(sname) + 6, M_DEVBUF, M_WAITOK);
302         ksprintf(rootdevnames[0], "ufs:%s%s", sname, partname);
303
304         /*
305          * For properly dangerously dedicated disks (ones with a historical
306          * bogus partition table), the boot blocks will give slice = 4, but
307          * the kernel will only provide the compatibility slice since it
308          * knows that slice 4 is not a real slice.  Arrange to try mounting
309          * the compatibility slice as root if mounting the slice passed by
310          * the boot blocks fails.  This handles the dangerously dedicated
311          * case and perhaps others.
312          */
313         if (slice == COMPATIBILITY_SLICE)
314                 return;
315         slice = COMPATIBILITY_SLICE;
316         sname = dsname(newrootdev, unit, slice, part, partname);
317         rootdevnames[1] = kmalloc(strlen(sname) + 6, M_DEVBUF, M_WAITOK);
318         ksprintf(rootdevnames[1], "ufs:%s%s", sname, partname);
319 }
320 #endif
321
322 #if defined(NFS) && defined(NFS_ROOT)
323 #if !defined(BOOTP_NFSROOT)
324
325 #include <sys/socket.h>
326 #include <net/if.h>
327 #include <net/if_dl.h>
328 #include <net/if_types.h>
329 #include <net/if_var.h>
330 #include <net/ethernet.h>
331 #include <netinet/in.h>
332 #include <vfs/nfs/rpcv2.h>
333 #include <vfs/nfs/nfsproto.h>
334 #include <vfs/nfs/nfs.h>
335 #include <vfs/nfs/nfsdiskless.h>
336
337 extern struct nfs_diskless      nfs_diskless;
338
339 /*
340  * Convert a kenv variable to a sockaddr.  If the kenv variable does not
341  * exist the sockaddr will remain zerod out (callers typically just check
342  * sin_len).  A network address of 0.0.0.0 is equivalent to failure.
343  */
344 static int
345 inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
346 {
347         u_int32_t       a[4];
348         char            *cp;
349
350         bzero(sa, sizeof(*sa));
351
352         if ((cp = kgetenv(ev)) == NULL)
353                 return(1);
354         if (ksscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]) != 4)
355                 return(1);
356         if (a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0)
357                 return(1);
358         /* XXX is this ordering correct? */
359         sa->sin_addr.s_addr = (a[3] << 24) + (a[2] << 16) + (a[1] << 8) + a[0];
360         sa->sin_len = sizeof(*sa);
361         sa->sin_family = AF_INET;
362         return(0);
363 }
364
365 static int
366 hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
367 {
368         char            *cp;
369         u_int32_t       a[6];
370
371         bzero(sa, sizeof(*sa));
372         sa->sdl_len = sizeof(*sa);
373         sa->sdl_family = AF_LINK;
374         sa->sdl_type = IFT_ETHER;
375         sa->sdl_alen = ETHER_ADDR_LEN;
376         if ((cp = kgetenv(ev)) == NULL)
377                 return(1);
378         if (ksscanf(cp, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]) != 6)
379                 return(1);
380         sa->sdl_data[0] = a[0];
381         sa->sdl_data[1] = a[1];
382         sa->sdl_data[2] = a[2];
383         sa->sdl_data[3] = a[3];
384         sa->sdl_data[4] = a[4];
385         sa->sdl_data[5] = a[5];
386         return(0);
387 }
388
389 static int
390 decode_nfshandle(char *ev, u_char *fh) 
391 {
392         u_char  *cp;
393         int     len, val;
394
395         if (((cp = kgetenv(ev)) == NULL) || (strlen(cp) < 2) || (*cp != 'X'))
396                 return(0);
397         len = 0;
398         cp++;
399         for (;;) {
400                 if (*cp == 'X')
401                         return(len);
402                 if ((ksscanf(cp, "%2x", &val) != 1) || (val > 0xff))
403                         return(0);
404                 *(fh++) = val;
405                 len++;
406                 cp += 2;
407                 if (len > NFSX_V2FH)
408                     return(0);
409         }
410 }
411
412 /*
413  * Populate the essential fields in the nfsv3_diskless structure.
414  *
415  * The loader is expected to export the following environment variables:
416  *
417  * boot.netif.ip                IP address on boot interface
418  * boot.netif.netmask           netmask on boot interface
419  * boot.netif.gateway           default gateway (optional)
420  * boot.netif.hwaddr            hardware address of boot interface
421  * boot.nfsroot.server          IP address of root filesystem server
422  * boot.nfsroot.path            path of the root filesystem on server
423  * boot.nfsroot.nfshandle       NFS handle for root filesystem on server
424  */
425 static void
426 pxe_setup_nfsdiskless(void)
427 {
428         struct nfs_diskless     *nd = &nfs_diskless;
429         struct ifnet            *ifp;
430         struct sockaddr_dl      *sdl, ourdl;
431         struct sockaddr_in      myaddr, netmask;
432         char                    *cp;
433
434         /* set up interface */
435         if (inaddr_to_sockaddr("boot.netif.ip", &myaddr))
436                 return;
437         if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) {
438                 kprintf("PXE: no netmask\n");
439                 return;
440         }
441         bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr));
442         bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr));
443         ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
444                 myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
445         bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask));
446
447         if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) {
448                 kprintf("PXE: no hardware address\n");
449                 return;
450         }
451         ifnet_lock();
452         TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
453                 struct ifaddr_container *ifac;
454
455                 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
456                         struct ifaddr *ifa = ifac->ifa;
457
458                         if ((ifa->ifa_addr->sa_family == AF_LINK) &&
459                             (sdl = ((struct sockaddr_dl *)ifa->ifa_addr))) {
460                                 if ((sdl->sdl_type == ourdl.sdl_type) &&
461                                     (sdl->sdl_alen == ourdl.sdl_alen) &&
462                                     !bcmp(sdl->sdl_data + sdl->sdl_nlen,
463                                           ourdl.sdl_data + ourdl.sdl_nlen, 
464                                           sdl->sdl_alen)) {
465                                         strlcpy(nd->myif.ifra_name,
466                                             ifp->if_xname,
467                                             sizeof(nd->myif.ifra_name));
468                                         ifnet_unlock();
469                                         goto match_done;
470                                 }
471                         }
472                 }
473         }
474         ifnet_unlock();
475         kprintf("PXE: no interface\n");
476         return; /* no matching interface */
477 match_done:
478         /* set up gateway */
479         inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway);
480
481         /* XXX set up swap? */
482
483         /* set up root mount */
484         nd->root_args.rsize = nfsroot_iosize;
485         nd->root_args.wsize = nfsroot_iosize;
486         nd->root_args.sotype = SOCK_STREAM;
487         nd->root_args.readahead = nfsroot_rahead;
488         nd->root_args.flags = NFSMNT_WSIZE | NFSMNT_RSIZE | NFSMNT_RESVPORT |
489                               NFSMNT_READAHEAD;
490         if (inaddr_to_sockaddr("boot.nfsroot.server", &nd->root_saddr)) {
491                 kprintf("PXE: no server\n");
492                 return;
493         }
494         nd->root_saddr.sin_port = htons(NFS_PORT);
495
496         /*
497          * A tftp-only loader may pass NFS path information without a 
498          * root handle.  Generate a warning but continue configuring.
499          */
500         if (decode_nfshandle("boot.nfsroot.nfshandle", &nd->root_fh[0]) == 0) {
501                 kprintf("PXE: Warning, no NFS handle passed from loader\n");
502         }
503         if ((cp = kgetenv("boot.nfsroot.path")) != NULL)
504                 strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
505
506         nfs_diskless_valid = 1;
507 }
508
509 #endif
510 #endif