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