]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/i386/autoconf.c
This commit was generated by cvs2svn to compensate for changes in r38776,
[FreeBSD/FreeBSD.git] / sys / i386 / i386 / autoconf.c
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  *      $Id: autoconf.c,v 1.102 1998/07/19 11:12:14 jkh Exp $
38  */
39
40 /*
41  * Setup the system to run on the current machine.
42  *
43  * Configure() is called at boot time and initializes the vba
44  * device tables and the memory controller monitoring.  Available
45  * devices are determined (from possibilities mentioned in ioconf.c),
46  * and the drivers are initialized.
47  */
48 #include "opt_devfs.h" /* for SLICE */
49 #include "opt_bootp.h"
50 #include "opt_ffs.h"
51 #include "opt_cd9660.h"
52 #include "opt_mfs.h"
53 #include "opt_nfsroot.h"
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/conf.h>
58 #include <sys/disklabel.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/sysctl.h>
65
66 #include <machine/bootinfo.h>
67 #include <machine/cons.h>
68 #include <machine/ipl.h>
69 #include <machine/md_var.h>
70 #ifdef APIC_IO
71 #include <machine/smp.h>
72 #endif /* APIC_IO */
73
74 #include <i386/isa/icu.h>
75
76 #include "isa.h"
77 #if NISA > 0
78 #include <i386/isa/isa_device.h>
79 #endif
80
81 #include "pnp.h"
82 #if NPNP > 0
83 #include <i386/isa/pnp.h>
84 #endif
85
86 #include "eisa.h"
87 #if NEISA > 0
88 #include <i386/eisa/eisaconf.h>
89 #endif
90
91 #include "pci.h"
92 #if NPCI > 0
93 #include <pci/pcivar.h>
94 #endif
95
96 #include "card.h"
97 #if NCARD > 0
98 #include <pccard/driver.h>
99 #endif
100
101 #include "scbus.h"
102 #if NSCBUS > 0
103 #include <scsi/scsiconf.h>
104 #endif
105
106 static void     configure __P((void *));
107 SYSINIT(configure, SI_SUB_CONFIGURE, SI_ORDER_FIRST, configure, NULL)
108
109 static void     configure_finish __P((void));
110 static void     configure_start __P((void));
111 static int      setdumpdev __P((dev_t dev));
112 #ifndef SLICE
113 static void     setroot __P((void));
114
115 #ifdef CD9660
116
117 #include <sys/fcntl.h>
118 #include <sys/proc.h>
119 #include <sys/stat.h>
120 #include <machine/clock.h>
121
122 /*
123  * XXX All this CD-ROM root stuff is fairly messy.  Ick.
124  *
125  * We need to try out all our potential CDROM drives, so we need a table.
126  */
127 static struct {
128         char *name;
129         int major;
130 } try_cdrom[] = {
131         { "cd", 6 },
132         { "mcd", 7 },
133         { "scd", 16 },
134         { "matcd", 17 },
135         { "wcd", 19 },
136         { 0, 0}
137 };
138
139 static int      find_cdrom_root __P((void));
140
141 static int
142 find_cdrom_root()
143 {
144         int i, j, error;
145         struct cdevsw *bd;
146         dev_t orootdev;
147
148 #if CD9660_ROOTDELAY > 0
149         DELAY(CD9660_ROOTDELAY * 1000000);
150 #endif
151         orootdev = rootdev;
152         for (i = 0 ; i < 2; i++)
153                 for (j = 0 ; try_cdrom[j].name ; j++) {
154                         if (try_cdrom[j].major >= nblkdev)
155                                 continue;
156                         rootdev = makedev(try_cdrom[j].major, i * 8);
157                         bd = bdevsw[major(rootdev)];
158                         if (bd == NULL || bd->d_open == NULL)
159                                 continue;
160                         if (bootverbose)
161                                 printf("trying %s%d as rootdev (0x%x)\n",
162                                        try_cdrom[j].name, i, rootdev);
163                         error = (bd->d_open)(rootdev, FREAD, S_IFBLK, curproc);
164                         if (error == 0) {
165                                 if (bd->d_close != NULL)
166                                         (bd->d_close)(rootdev, FREAD, S_IFBLK,
167                                                       curproc);
168                                 return 0;
169                         }
170                 }
171
172         rootdev = orootdev;
173         return EINVAL;
174 }
175 #endif /* CD9660 */
176 #endif /* !SLICE */
177
178 static void
179 configure_start()
180 {
181 #if NSCBUS > 0
182         scsi_configure_start();
183 #endif
184 }
185
186 static void
187 configure_finish()
188 {
189 #if NSCBUS > 0
190         scsi_configure_finish();
191 #endif
192 }
193
194 /*
195  * Determine i/o configuration for a machine.
196  */
197 static void
198 configure(dummy)
199         void *dummy;
200 {
201         int i;
202
203         configure_start();
204
205         /* Allow all routines to decide for themselves if they want intrs */
206         /*
207          * XXX Since this cannot be achieved on all architectures, we should
208          * XXX go back to disabling all interrupts until configuration is
209          * XXX completed and switch any devices that rely on the current
210          * XXX behavior to no longer rely on interrupts or to register an
211          * XXX interrupt_driven_config_hook for the task.
212          */
213         /*
214          * XXX The above is wrong, because we're implicitly at splhigh(),
215          * XXX and should stay there, so enabling interrupts in the CPU
216          * XXX and the ICU at most gives pending interrupts which just get
217          * XXX in the way.
218          */
219 #ifdef APIC_IO
220         bsp_apic_configure();
221         enable_intr();
222 #else
223         enable_intr();
224         INTREN(IRQ_SLAVE);
225 #endif /* APIC_IO */
226
227 #if NEISA > 0
228         eisa_configure();
229 #endif
230
231 #if NPCI > 0
232         pci_configure();
233 #endif
234
235 #if NPNP > 0
236         pnp_configure();
237 #endif
238
239 #if NISA > 0
240         isa_configure();
241 #endif
242
243         /*
244          * Now we're ready to handle (pending) interrupts.
245          * XXX this is slightly misplaced.
246          */
247         spl0();
248
249         /*
250          * Allow lowering of the ipl to the lowest kernel level if we
251          * panic (or call tsleep() before clearing `cold').  No level is
252          * completely safe (since a panic may occur in a critical region
253          * at splhigh()), but we want at least bio interrupts to work.
254          */
255         safepri = cpl;
256
257 #if NCARD > 0
258         /* After everyone else has a chance at grabbing resources */
259         pccard_configure();
260 #endif
261
262         configure_finish();
263
264         cninit_finish();
265
266         if (bootverbose) {
267
268 #ifdef APIC_IO
269                 imen_dump();
270 #endif /* APIC_IO */
271
272                 /*
273                  * Print out the BIOS's idea of the disk geometries.
274                  */
275                 printf("BIOS Geometries:\n");
276                 for (i = 0; i < N_BIOS_GEOM; i++) {
277                         unsigned long bios_geom;
278                         int max_cylinder, max_head, max_sector;
279
280                         bios_geom = bootinfo.bi_bios_geom[i];
281
282                         /*
283                          * XXX the bootstrap punts a 1200K floppy geometry
284                          * when the get-disk-geometry interrupt fails.  Skip
285                          * drives that have this geometry.
286                          */
287                         if (bios_geom == 0x4f010f)
288                                 continue;
289
290                         printf(" %x:%08lx ", i, bios_geom);
291                         max_cylinder = bios_geom >> 16;
292                         max_head = (bios_geom >> 8) & 0xff;
293                         max_sector = bios_geom & 0xff;
294                         printf(
295                 "0..%d=%d cylinders, 0..%d=%d heads, 1..%d=%d sectors\n",
296                                max_cylinder, max_cylinder + 1,
297                                max_head, max_head + 1,
298                                max_sector, max_sector);
299                 }
300                 printf(" %d accounted for\n", bootinfo.bi_n_bios_used);
301
302                 printf("Device configuration finished.\n");
303         }
304         cold = 0;
305 }
306
307 #ifndef SLICE
308
309 void
310 cpu_rootconf()
311 {
312         /*
313          * XXX NetBSD has a much cleaner approach to finding root.
314          * XXX We should adopt their code.
315          */
316 #if defined(CD9660) || defined(CD9660_ROOT)
317         if ((boothowto & RB_CDROM)) {
318                 if (bootverbose)
319                         printf("Considering CD-ROM root f/s.\n");
320                 /* NB: find_cdrom_root() sets rootdev if successful. */
321                 if (find_cdrom_root() == 0)
322                         mountrootfsname = "cd9660";
323                 else if (bootverbose)
324                         printf("No CD-ROM available as root f/s.\n");
325         }
326 #endif
327
328 #ifdef MFS_ROOT
329         if (!mountrootfsname) {
330                 if (bootverbose)
331                         printf("Considering MFS root f/s.\n");
332                 mountrootfsname = "mfs";
333                 /*
334                  * Ignore the -a flag if this kernel isn't compiled
335                  * with a generic root/swap configuration: if we skip
336                  * setroot() and we aren't a generic kernel, chaos
337                  * will ensue because setconf() will be a no-op.
338                  * (rootdev is always initialized to NODEV in a
339                  * generic configuration, so we test for that.)
340                  */
341                 if ((boothowto & RB_ASKNAME) == 0 || rootdev != NODEV)
342                         setroot();
343         }
344 #endif
345
346 #ifdef BOOTP_NFSROOT
347         if (!mountrootfsname && !nfs_diskless_valid) {
348                 if (bootverbose)
349                         printf("Considering BOOTP NFS root f/s.\n");
350                 mountrootfsname = "nfs";
351         }
352 #endif /* BOOTP_NFSROOT */
353 #if defined(NFS) || defined(NFS_ROOT)
354         if (!mountrootfsname && nfs_diskless_valid) {
355                 if (bootverbose)
356                         printf("Considering NFS root f/s.\n");
357                 mountrootfsname = "nfs";
358         }
359 #endif /* NFS */
360
361 #if defined(FFS) || defined(FFS_ROOT)
362         if (!mountrootfsname) {
363                 mountrootfsname = "ufs";
364                 if (bootverbose)
365                         printf("Considering FFS root f/s.\n");
366                 /*
367                  * Ignore the -a flag if this kernel isn't compiled
368                  * with a generic root/swap configuration: if we skip
369                  * setroot() and we aren't a generic kernel, chaos
370                  * will ensue because setconf() will be a no-op.
371                  * (rootdev is always initialized to NODEV in a
372                  * generic configuration, so we test for that.)
373                  */
374                 if ((boothowto & RB_ASKNAME) == 0 || rootdev != NODEV)
375                         setroot();
376         }
377 #endif
378
379         if (!mountrootfsname) {
380                 panic("Nobody wants to mount my root for me");
381         }
382
383         setconf();
384 }
385
386 #endif /* !SLICE */
387
388 void
389 cpu_dumpconf()
390 {
391         if (setdumpdev(dumpdev) != 0)
392                 dumpdev = NODEV;
393 }
394
395 static int
396 setdumpdev(dev)
397         dev_t dev;
398 {
399         int maj, psize;
400         long newdumplo;
401
402         if (dev == NODEV) {
403                 dumpdev = dev;
404                 return (0);
405         }
406         maj = major(dev);
407         if (maj >= nblkdev)
408                 return (ENXIO);
409         if (bdevsw[maj] == NULL)
410                 return (ENXIO);         /* XXX is this right? */
411         if (bdevsw[maj]->d_psize == NULL)
412                 return (ENXIO);         /* XXX should be ENODEV ? */
413         psize = bdevsw[maj]->d_psize(dev);
414         if (psize == -1)
415                 return (ENXIO);         /* XXX should be ENODEV ? */
416         /*
417          * XXX should clean up checking in dumpsys() to be more like this,
418          * and nuke dodump sysctl (too many knobs), and move this to
419          * kern_shutdown.c...
420          */
421         newdumplo = psize - Maxmem * PAGE_SIZE / DEV_BSIZE;
422         if (newdumplo < 0)
423                 return (ENOSPC);
424         dumpdev = dev;
425         dumplo = newdumplo;
426         return (0);
427 }
428
429 #ifndef SLICE
430
431 u_long  bootdev = 0;            /* not a dev_t - encoding is different */
432
433 #define FDMAJOR 2
434 #define FDUNITSHIFT     6
435
436 /*
437  * Attempt to find the device from which we were booted.
438  * If we can do so, and not instructed not to do so,
439  * set rootdevs[] and rootdevnames[] to correspond to the
440  * boot device(s).
441  */
442 static void
443 setroot()
444 {
445         int majdev, mindev, unit, slice, part;
446         dev_t newrootdev;
447         char partname[2];
448         char *sname;
449
450         if (boothowto & RB_DFLTROOT || (bootdev & B_MAGICMASK) != B_DEVMAGIC)
451                 return;
452         majdev = B_TYPE(bootdev);
453         if (bdevsw[majdev] == NULL)
454                 return;
455         unit = B_UNIT(bootdev);
456         slice = B_SLICE(bootdev);
457         if (slice == WHOLE_DISK_SLICE)
458                 slice = COMPATIBILITY_SLICE;
459         if (slice < 0 || slice >= MAX_SLICES)
460                 return;
461
462         /*
463          * XXX kludge for inconsistent unit numbering and lack of slice
464          * support for floppies.
465          */
466         if (majdev == FDMAJOR) {
467                 slice = COMPATIBILITY_SLICE;
468                 part = RAW_PART;
469                 mindev = unit << FDUNITSHIFT;
470         } else {
471                 part = B_PARTITION(bootdev);
472                 mindev = dkmakeminor(unit, slice, part);
473         }
474
475         newrootdev = makedev(majdev, mindev);
476         rootdevs[0] = newrootdev;
477         sname = dsname(bdevsw[majdev]->d_name, unit, slice, part, partname);
478         rootdevnames[0] = malloc(strlen(sname) + 2, M_DEVBUF, M_NOWAIT);
479         sprintf(rootdevnames[0], "%s%s", sname, partname);
480
481         /*
482          * For properly dangerously dedicated disks (ones with a historical
483          * bogus partition table), the boot blocks will give slice = 4, but
484          * the kernel will only provide the compatibility slice since it
485          * knows that slice 4 is not a real slice.  Arrange to try mounting
486          * the compatibility slice as root if mounting the slice passed by
487          * the boot blocks fails.  This handles the dangerously dedicated
488          * case and perhaps others.
489          */
490         if (slice == COMPATIBILITY_SLICE)
491                 return;
492         slice = COMPATIBILITY_SLICE;
493         rootdevs[1] = dkmodslice(newrootdev, slice);
494         sname = dsname(bdevsw[majdev]->d_name, unit, slice, part, partname);
495         rootdevnames[1] = malloc(strlen(sname) + 2, M_DEVBUF, M_NOWAIT);
496         sprintf(rootdevnames[1], "%s%s", sname, partname);
497 }
498
499 #endif /* !SLICE */
500
501 static int
502 sysctl_kern_dumpdev SYSCTL_HANDLER_ARGS
503 {
504         int error;
505         dev_t ndumpdev;
506
507         ndumpdev = dumpdev;
508         error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req);
509         if (error == 0 && req->newptr != NULL)
510                 error = setdumpdev(ndumpdev);
511         return (error);
512 }
513
514 SYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW,
515         0, sizeof dumpdev, sysctl_kern_dumpdev, "T,dev_t", "");