Merge commit '7e934d3acc051b7ee3ef0d11571fd1225800a607'
[unleashed.git] / kernel / os / swapgeneric.c
blob0e59167a0d07c61dfa54c8911b7aacdbeb7999fb
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 1982, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2016 Nexenta Systems, Inc.
28 * Configure root, swap and dump devices.
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/sysmacros.h>
34 #include <sys/signal.h>
35 #include <sys/cred.h>
36 #include <sys/proc.h>
37 #include <sys/user.h>
38 #include <sys/conf.h>
39 #include <sys/buf.h>
40 #include <sys/systm.h>
41 #include <sys/vm.h>
42 #include <sys/reboot.h>
43 #include <sys/file.h>
44 #include <sys/vfs.h>
45 #include <sys/vnode.h>
46 #include <sys/errno.h>
47 #include <sys/kmem.h>
48 #include <sys/uio.h>
49 #include <sys/open.h>
50 #include <sys/mount.h>
51 #include <sys/kobj.h>
52 #include <sys/bootconf.h>
53 #include <sys/sysconf.h>
54 #include <sys/modctl.h>
55 #include <sys/autoconf.h>
56 #include <sys/debug.h>
57 #include <sys/fs/snode.h>
58 #include <sys/fs_subr.h>
59 #include <sys/socket.h>
60 #include <net/if.h>
62 #include <sys/mkdev.h>
63 #include <sys/cmn_err.h>
64 #include <sys/console.h>
66 #include <sys/conf.h>
67 #include <sys/ddi.h>
68 #include <sys/sunddi.h>
69 #include <sys/hwconf.h>
70 #include <sys/promif.h>
71 #include <sys/bootprops.h>
74 * Local routines
76 static int preload_module(struct sysparam *, void *);
77 static struct vfssw *getfstype(char *, char *, size_t);
78 static int getphysdev(char *, char *, size_t);
79 static int load_bootpath_drivers(char *bootpath);
80 static int load_boot_driver(char *drv);
81 static int load_boot_platform_modules(char *drv);
82 static dev_info_t *path_to_devinfo(char *path);
83 static boolean_t netboot_over_ib(char *bootpath);
84 static boolean_t netboot_over_iscsi(void);
87 * Module linkage information for the kernel.
89 static struct modlmisc modlmisc = {
90 &mod_miscops, "root and swap configuration"
93 static struct modlinkage modlinkage = {
94 MODREV_1, (void *)&modlmisc, NULL
97 int
98 _init(void)
100 return (mod_install(&modlinkage));
104 _fini(void)
106 return (mod_remove(&modlinkage));
110 _info(struct modinfo *modinfop)
112 return (mod_info(&modlinkage, modinfop));
115 extern ib_boot_prop_t *iscsiboot_prop;
117 * Configure root file system.
120 rootconf(void)
122 int error;
123 struct vfssw *vsw;
124 extern void pm_init(void);
125 int ret = -1;
126 BMDPRINTF(("rootconf: fstype %s\n", rootfs.bo_fstype));
127 BMDPRINTF(("rootconf: name %s\n", rootfs.bo_name));
128 BMDPRINTF(("rootconf: flags 0x%x\n", rootfs.bo_flags));
129 BMDPRINTF(("rootconf: obp_bootpath %s\n", obp_bootpath));
132 * Run _init on the root filesystem (we already loaded it
133 * but we've been waiting until now to _init it) which will
134 * have the side-effect of running vsw_init() on this vfs.
135 * Because all the nfs filesystems are lumped into one
136 * module we need to special case it.
138 if (strncmp(rootfs.bo_fstype, "nfs", 3) == 0) {
139 if (modload("fs", "nfs") == -1) {
140 cmn_err(CE_CONT, "Cannot initialize %s filesystem\n",
141 rootfs.bo_fstype);
142 return (ENXIO);
144 } else {
145 if (modload("fs", rootfs.bo_fstype) == -1) {
146 cmn_err(CE_CONT, "Cannot initialize %s filesystem\n",
147 rootfs.bo_fstype);
148 return (ENXIO);
151 RLOCK_VFSSW();
152 vsw = vfs_getvfsswbyname(rootfs.bo_fstype);
153 RUNLOCK_VFSSW();
154 if (vsw == NULL) {
155 cmn_err(CE_CONT, "Cannot find %s filesystem\n",
156 rootfs.bo_fstype);
157 return (ENXIO);
159 VFS_INIT(rootvfs, &vsw->vsw_vfsops, (caddr_t)0);
160 VFS_HOLD(rootvfs);
163 * This pm-releated call has to occur before root is mounted since we
164 * need to power up all devices. It is placed after VFS_INIT() such
165 * that opening a device via ddi_lyr_ interface just before root has
166 * been mounted would work.
168 pm_init();
170 if (netboot && iscsiboot_prop) {
171 cmn_err(CE_WARN, "NFS boot and iSCSI boot"
172 " shouldn't happen in the same time");
173 return (EINVAL);
176 if (netboot || iscsiboot_prop) {
177 ret = strplumb();
178 if (ret != 0) {
179 cmn_err(CE_WARN, "Cannot plumb network device %d", ret);
180 return (EFAULT);
184 if ((ret == 0) && iscsiboot_prop) {
185 ret = modload("drv", "iscsi");
186 /* -1 indicates fail */
187 if (ret == -1) {
188 cmn_err(CE_WARN, "Failed to load iscsi module");
189 iscsi_boot_prop_free();
190 return (EINVAL);
191 } else {
192 if (!i_ddi_attach_pseudo_node("iscsi")) {
193 cmn_err(CE_WARN,
194 "Failed to attach iscsi driver");
195 iscsi_boot_prop_free();
196 return (ENODEV);
202 * ufs_mountroot() ends up calling getrootdev()
203 * (below) which actually triggers the _init, identify,
204 * probe and attach of the drivers that make up root device
205 * bush; these are also quietly waiting in memory.
207 BMDPRINTF(("rootconf: calling VFS_MOUNTROOT %s\n", rootfs.bo_fstype));
209 error = VFS_MOUNTROOT(rootvfs, ROOT_INIT);
210 vfs_unrefvfssw(vsw);
211 rootdev = rootvfs->vfs_dev;
213 if (error)
214 cmn_err(CE_CONT, "Cannot mount root on %s fstype %s\n",
215 rootfs.bo_name, rootfs.bo_fstype);
216 else
217 cmn_err(CE_CONT, "?root on %s fstype %s\n",
218 rootfs.bo_name, rootfs.bo_fstype);
219 return (error);
223 * Under the assumption that our root file system is on a
224 * disk partition, get the dev_t of the partition in question.
226 * By now, boot has faithfully loaded all our modules into memory, and
227 * we've taken over resource management. Before we go any further, we
228 * have to fire up the device drivers and stuff we need to mount the
229 * root filesystem. That's what we do here. Fingers crossed.
231 dev_t
232 getrootdev(void)
234 dev_t d;
236 d = ddi_pathname_to_dev_t(rootfs.bo_name);
237 if ((d == NODEV) && (iscsiboot_prop != NULL)) {
238 /* Give it another try with the 'disk' path */
239 get_iscsi_bootpath_phy(rootfs.bo_name);
240 d = ddi_pathname_to_dev_t(rootfs.bo_name);
242 if (d == NODEV)
243 cmn_err(CE_CONT, "Cannot assemble drivers for root %s\n",
244 rootfs.bo_name);
245 return (d);
249 * If booted with ASKNAME, prompt on the console for a filesystem
250 * name and return it.
252 void
253 getfsname(char *askfor, char *name, size_t namelen)
255 if (boothowto & RB_ASKNAME) {
256 printf("%s name: ", askfor);
257 console_gets(name, namelen);
261 /*ARGSUSED1*/
262 static int
263 preload_module(struct sysparam *sysp, void *p)
265 static char *wmesg = "forceload of %s failed";
266 char *name;
268 name = sysp->sys_ptr;
269 BMDPRINTF(("preload_module: %s\n", name));
270 if (modloadonly(NULL, name) < 0)
271 cmn_err(CE_WARN, wmesg, name);
272 return (0);
276 * We want to load all the modules needed to mount the root filesystem,
277 * so that when we start the ball rolling in 'getrootdev', every module
278 * should already be in memory, just waiting to be init-ed.
282 loadrootmodules(void)
284 struct vfssw *vsw;
285 char *this;
286 char *name;
287 int err;
288 int i, proplen;
289 extern char *impl_module_list[];
290 extern char *platform_module_list[];
292 /* Make sure that the PROM's devinfo tree has been created */
293 ASSERT(ddi_root_node());
295 BMDPRINTF(("loadrootmodules: fstype %s\n", rootfs.bo_fstype));
296 BMDPRINTF(("loadrootmodules: name %s\n", rootfs.bo_name));
297 BMDPRINTF(("loadrootmodules: flags 0x%x\n", rootfs.bo_flags));
300 * Get the root fstype and root device path from boot.
302 rootfs.bo_fstype[0] = '\0';
303 rootfs.bo_name[0] = '\0';
306 * This lookup will result in modloadonly-ing the root
307 * filesystem module - it gets _init-ed in rootconf()
309 if ((vsw = getfstype("root", rootfs.bo_fstype, BO_MAXFSNAME)) == NULL)
310 return (ENXIO); /* in case we have no file system types */
312 (void) strcpy(rootfs.bo_fstype, vsw->vsw_name);
314 vfs_unrefvfssw(vsw);
317 * Load the favored drivers of the implementation.
318 * e.g. 'sbus' and possibly 'zs' (even).
320 * Called whilst boot is still loaded (because boot does
321 * the i/o for us), and DDI services are unavailable.
323 BMDPRINTF(("loadrootmodules: impl_module_list\n"));
324 for (i = 0; (this = impl_module_list[i]) != NULL; i++) {
325 if ((err = load_boot_driver(this)) != 0) {
326 cmn_err(CE_WARN, "Cannot load drv/%s", this);
327 return (err);
331 * Now load the platform modules (if any)
333 BMDPRINTF(("loadrootmodules: platform_module_list\n"));
334 for (i = 0; (this = platform_module_list[i]) != NULL; i++) {
335 if ((err = load_boot_platform_modules(this)) != 0) {
336 cmn_err(CE_WARN, "Cannot load drv/%s", this);
337 return (err);
341 loop:
342 (void) getphysdev("root", rootfs.bo_name, BO_MAXOBJNAME);
344 * Given a physical pathname, load the correct set of driver
345 * modules into memory, including all possible parents.
347 * NB: The code sets the variable 'name' for error reporting.
349 err = 0;
350 BMDPRINTF(("loadrootmodules: rootfs %s\n", rootfs.bo_name));
351 name = rootfs.bo_name;
352 err = load_bootpath_drivers(rootfs.bo_name);
355 * Load driver modules in obp_bootpath, this is always
356 * required for mountroot to succeed. obp_bootpath is
357 * is set if rootdev is set via /etc/system.
359 if ((err == 0) && obp_bootpath[0] != '\0') {
360 BMDPRINTF(("loadrootmodules: obp_bootpath %s\n", obp_bootpath));
361 name = obp_bootpath;
362 err = load_bootpath_drivers(obp_bootpath);
365 if (err != 0) {
366 cmn_err(CE_CONT, "Cannot load drivers for %s\n", name);
367 goto out;
371 * Check to see if the booter performed DHCP configuration
372 * ("bootp-response" boot property exists). If so, then before
373 * bootops disappears we need to save the value of this property
374 * such that the userland dhcpagent can adopt the DHCP management
375 * of our primary network interface.
377 proplen = BOP_GETPROPLEN(bootops, "bootp-response");
378 if (proplen > 0) {
379 dhcack = kmem_zalloc(proplen, KM_SLEEP);
380 if (BOP_GETPROP(bootops, "bootp-response", dhcack) == -1) {
381 cmn_err(CE_WARN, "BOP_GETPROP of "
382 "\"bootp-response\" failed\n");
383 kmem_free(dhcack, dhcacklen);
384 dhcack = NULL;
385 goto out;
387 dhcacklen = proplen;
390 * Fetch the "netdev-path" boot property (if it exists), and
391 * stash it for later use by sysinfo(SI_DHCP_CACHE, ...).
393 proplen = BOP_GETPROPLEN(bootops, "netdev-path");
394 if (proplen > 0) {
395 netdev_path = kmem_zalloc(proplen, KM_SLEEP);
396 if (BOP_GETPROP(bootops, "netdev-path",
397 (uchar_t *)netdev_path) == -1) {
398 cmn_err(CE_WARN, "BOP_GETPROP of "
399 "\"netdev-path\" failed\n");
400 kmem_free(netdev_path, proplen);
401 goto out;
407 * Preload (load-only, no init) all modules which
408 * were added to the /etc/system file with the
409 * FORCELOAD keyword.
411 BMDPRINTF(("loadrootmodules: preload_module\n"));
412 (void) mod_sysctl_type(MOD_FORCELOAD, preload_module, NULL);
415 * If we booted otw then load in the plumbing
416 * routine now while we still can. If we didn't
417 * boot otw then we will load strplumb in main().
419 * NFS is actually a set of modules, the core routines,
420 * a diskless helper module, rpcmod, and the tli interface. Load
421 * them now while we still can.
423 * Because we glomb all versions of nfs into a single module
424 * we check based on the initial string "nfs".
426 * XXX: A better test for this is to see if device_type
427 * XXX: from the PROM is "network".
430 if (strncmp(rootfs.bo_fstype, "nfs", 3) == 0) {
431 ++netboot;
434 * Preload (load-only, no init) the dacf module. We cannot
435 * init the module because one of its requisite modules is
436 * dld whose _init function will call taskq_create(), which
437 * will panic the system at this point.
439 if ((err = modloadonly("dacf", "net_dacf")) < 0) {
440 cmn_err(CE_CONT, "Cannot load dacf/net_dacf\n");
441 goto out;
443 if ((err = modload("misc", "tlimod")) < 0) {
444 cmn_err(CE_CONT, "Cannot load misc/tlimod\n");
445 goto out;
447 if ((err = modload("strmod", "rpcmod")) < 0) {
448 cmn_err(CE_CONT, "Cannot load strmod/rpcmod\n");
449 goto out;
451 if ((err = modload("misc", "nfs_dlboot")) < 0) {
452 cmn_err(CE_CONT, "Cannot load misc/nfs_dlboot\n");
453 goto out;
455 if ((err = modload("mac", "mac_ether")) < 0) {
456 cmn_err(CE_CONT, "Cannot load mac/mac_ether\n");
457 goto out;
459 if ((err = modload("misc", "strplumb")) < 0) {
460 cmn_err(CE_CONT, "Cannot load misc/strplumb\n");
461 goto out;
463 if ((err = strplumb_load()) < 0) {
464 goto out;
467 if (netboot_over_iscsi() == B_TRUE) {
468 /* iscsi boot */
469 if ((err = modloadonly("dacf", "net_dacf")) < 0) {
470 cmn_err(CE_CONT, "Cannot load dacf/net_dacf\n");
471 goto out;
473 if ((err = modload("misc", "tlimod")) < 0) {
474 cmn_err(CE_CONT, "Cannot load misc/tlimod\n");
475 goto out;
477 if ((err = modload("mac", "mac_ether")) < 0) {
478 cmn_err(CE_CONT, "Cannot load mac/mac_ether\n");
479 goto out;
481 if ((err = modloadonly("drv", "iscsi")) < 0) {
482 cmn_err(CE_CONT, "Cannot load drv/iscsi\n");
483 goto out;
485 if ((err = modloadonly("drv", "ssd")) < 0) {
486 cmn_err(CE_CONT, "Cannot load drv/ssd\n");
487 goto out;
489 if ((err = modloadonly("drv", "sd")) < 0) {
490 cmn_err(CE_CONT, "Cannot load drv/sd\n");
491 goto out;
493 if ((err = modload("misc", "strplumb")) < 0) {
494 cmn_err(CE_CONT, "Cannot load misc/strplumb\n");
495 goto out;
497 if ((err = strplumb_load()) < 0) {
498 goto out;
502 out:
503 if (err != 0 && (boothowto & RB_ASKNAME))
504 goto loop;
506 return (err);
509 static int
510 get_bootpath_prop(char *bootpath)
512 if (root_is_ramdisk) {
513 if (BOP_GETPROP(bootops, "bootarchive", bootpath) == -1)
514 return (-1);
515 (void) strlcat(bootpath, ":a", BO_MAXOBJNAME);
516 } else {
518 * Look for the 1275 compliant name 'bootpath' first,
519 * but make certain it has a non-NULL value as well.
521 if ((BOP_GETPROP(bootops, "bootpath", bootpath) == -1) ||
522 strlen(bootpath) == 0) {
523 if (BOP_GETPROP(bootops,
524 "boot-path", bootpath) == -1)
525 return (-1);
527 if (memcmp(bootpath, BP_ISCSI_DISK,
528 strlen(BP_ISCSI_DISK)) == 0) {
529 /* iscsi boot */
530 get_iscsi_bootpath_vhci(bootpath);
533 return (0);
536 static int
537 get_fstype_prop(char *fstype)
539 char *prop = (root_is_ramdisk) ? "archive-fstype" : "fstype";
541 return (BOP_GETPROP(bootops, prop, fstype));
545 * Get the name of the root or swap filesystem type, and return
546 * the corresponding entry in the vfs switch.
548 * If we're not asking the user, and we're trying to find the
549 * root filesystem type, we ask boot for the filesystem
550 * type that it came from and use that. Similarly, if we're
551 * trying to find the swap filesystem, we try and derive it from
552 * the root filesystem type.
554 * If we are booting via NFS we currently have these options:
555 * nfs - dynamically choose NFS V2. V3, or V4 (default)
556 * nfs2 - force NFS V2
557 * nfs3 - force NFS V3
558 * nfs4 - force NFS V4
559 * Because we need to maintain backward compatibility with the naming
560 * convention that the NFS V2 filesystem name is "nfs" (see vfs_conf.c)
561 * we need to map "nfs" => "nfsdyn" and "nfs2" => "nfs". The dynamic
562 * nfs module will map the type back to either "nfs", "nfs3", or "nfs4".
563 * This is only for root filesystems, all other uses will expect
564 * that "nfs" == NFS V2.
566 * If the filesystem isn't already loaded, vfs_getvfssw() will load
567 * it for us, but if (at the time we call it) modrootloaded is
568 * still not set, it won't run the filesystems _init routine (and
569 * implicitly it won't run the filesystems vsw_init() entry either).
570 * We do that explicitly in rootconf().
572 static struct vfssw *
573 getfstype(char *askfor, char *fsname, size_t fsnamelen)
575 struct vfssw *vsw;
576 static char defaultfs[BO_MAXFSNAME];
577 int root = 0;
579 if (strcmp(askfor, "root") == 0) {
580 (void) get_fstype_prop(defaultfs);
581 root++;
582 } else {
583 (void) strcpy(defaultfs, "swapfs");
586 if (boothowto & RB_ASKNAME) {
587 for (*fsname = '\0'; *fsname == '\0'; *fsname = '\0') {
588 printf("%s filesystem type [%s]: ", askfor, defaultfs);
589 console_gets(fsname, fsnamelen);
590 if (*fsname == '\0')
591 (void) strcpy(fsname, defaultfs);
592 if (root) {
593 if (strcmp(fsname, "nfs2") == 0)
594 (void) strcpy(fsname, "nfs");
595 else if (strcmp(fsname, "nfs") == 0)
596 (void) strcpy(fsname, "nfsdyn");
598 if ((vsw = vfs_getvfssw(fsname)) != NULL)
599 return (vsw);
600 printf("Unknown filesystem type '%s'\n", fsname);
602 } else if (*fsname == '\0') {
603 fsname = defaultfs;
605 if (*fsname == '\0') {
606 return (NULL);
609 if (root) {
610 if (strcmp(fsname, "nfs2") == 0)
611 (void) strcpy(fsname, "nfs");
612 else if (strcmp(fsname, "nfs") == 0)
613 (void) strcpy(fsname, "nfsdyn");
616 return (vfs_getvfssw(fsname));
621 * Get a physical device name, and maybe load and attach
622 * the driver.
624 * XXX Need better checking of whether or not a device
625 * actually exists if the user typed in a pathname.
627 * XXX Are we sure we want to expose users to this sort
628 * of physical namespace gobbledygook (now there's
629 * a word to conjure with..)
631 * XXX Note that on an OBP machine, we can easily ask the
632 * prom and pretty-print some plausible set of bootable
633 * devices. We can also user the prom to verify any
634 * such device. Later tim.. later.
636 static int
637 getphysdev(char *askfor, char *name, size_t namelen)
639 static char fmt[] = "Enter physical name of %s device\n[%s]: ";
640 dev_t dev;
641 static char defaultpath[BO_MAXOBJNAME];
644 * Establish 'default' values - we get the root device from
645 * boot, and we infer the swap device is the same but with
646 * a 'b' on the end instead of an 'a'. A first stab at
647 * ease-of-use ..
649 if (strcmp(askfor, "root") == 0) {
650 if (get_bootpath_prop(defaultpath) == -1)
651 boothowto |= RB_ASKNAME | RB_VERBOSE;
652 } else {
653 (void) strcpy(defaultpath, rootfs.bo_name);
654 defaultpath[strlen(defaultpath) - 1] = 'b';
657 retry:
658 if (boothowto & RB_ASKNAME) {
659 printf(fmt, askfor, defaultpath);
660 console_gets(name, namelen);
662 if (*name == '\0')
663 (void) strcpy(name, defaultpath);
665 if (strcmp(askfor, "swap") == 0) {
668 * Try to load and install the swap device driver.
670 dev = ddi_pathname_to_dev_t(name);
672 if (dev == (dev_t)-1) {
673 printf("Not a supported device for swap.\n");
674 boothowto |= RB_ASKNAME | RB_VERBOSE;
675 goto retry;
679 * Ensure that we're not trying to swap on the floppy.
681 if (strncmp(ddi_major_to_name(getmajor(dev)), "fd", 2) == 0) {
682 printf("Too dangerous to swap on the floppy\n");
683 if (boothowto & RB_ASKNAME)
684 goto retry;
685 return (-1);
689 return (0);
694 * Load a driver needed to boot.
696 static int
697 load_boot_driver(char *drv)
699 char *drvname;
700 major_t major;
701 #ifdef sparc
702 struct devnames *dnp;
703 ddi_prop_t *propp;
704 char *module;
705 char *dir, *mf;
706 int plen;
707 int mlen;
708 #endif /* sparc */
710 if ((major = ddi_name_to_major(drv)) == DDI_MAJOR_T_NONE) {
711 cmn_err(CE_CONT, "%s: no major number\n", drv);
712 return (-1);
715 * resolve aliases
717 drvname = ddi_major_to_name(major);
719 #ifdef DEBUG
720 if (strcmp(drv, drvname) == 0) {
721 BMDPRINTF(("load_boot_driver: %s\n", drv));
722 } else {
723 BMDPRINTF(("load_boot_driver: %s -> %s\n", drv, drvname));
725 #endif /* DEBUG */
727 if (modloadonly("drv", drvname) == -1) {
728 cmn_err(CE_CONT, "%s: cannot load driver\n", drvname);
729 return (-1);
732 #ifdef sparc
734 * NOTE: this can be removed when newboot-sparc is delivered.
736 * Check to see if the driver had a 'ddi-forceload' global driver.conf
737 * property to identify additional modules that need to be loaded.
738 * The driver still needs to use ddi_modopen() to open these modules,
739 * but the 'ddi-forceload' property allows the modules to be loaded
740 * into memory prior to lights-out, so that driver ddi_modopen()
741 * calls during lights-out (when mounting root) will work correctly.
742 * Use of 'ddi-forceload' is only required for drivers involved in
743 * getting root mounted.
745 dnp = &devnamesp[major];
746 if (dnp->dn_global_prop_ptr && dnp->dn_global_prop_ptr->prop_list &&
747 ((propp = i_ddi_prop_search(DDI_DEV_T_ANY,
748 "ddi-forceload", DDI_PROP_TYPE_STRING,
749 &dnp->dn_global_prop_ptr->prop_list)) != NULL)) {
751 module = (char *)propp->prop_val;
752 plen = propp->prop_len;
753 while (plen > 0) {
754 mlen = strlen(module);
755 mf = strrchr(module, '/');
756 if (mf) {
757 dir = module;
758 *mf++ = '\0'; /* '/' -> '\0' */
759 } else {
760 dir = "misc";
761 mf = module;
763 if (modloadonly(dir, mf) == -1)
764 cmn_err(CE_CONT,
765 "misc/%s: can't load module\n", mf);
766 if (mf != module)
767 *(mf - 1) = '/'; /* '\0' -> '/' */
769 module += mlen + 1;
770 plen -= mlen + 1;
773 #endif /* sparc */
775 return (0);
780 * For a given instance, load that driver and its parents
782 static int
783 load_parent_drivers(dev_info_t *dip, char *path)
785 int rval = 0;
786 major_t major = DDI_MAJOR_T_NONE;
787 char *drv;
788 char *p;
790 while (dip) {
791 /* check for path-oriented alias */
792 if (path)
793 major = ddi_name_to_major(path);
794 else
795 major = DDI_MAJOR_T_NONE;
797 if (major != DDI_MAJOR_T_NONE)
798 drv = ddi_major_to_name(major);
799 else
800 drv = ddi_binding_name(dip);
802 if (load_boot_driver(drv) != 0)
803 rval = -1;
805 dip = ddi_get_parent(dip);
806 if (path) {
807 p = strrchr(path, '/');
808 if (p)
809 *p = 0;
813 return (rval);
818 * For a given path to a boot device,
819 * load that driver and all its parents.
821 static int
822 load_bootpath_drivers(char *bootpath)
824 dev_info_t *dip;
825 char *pathcopy;
826 int pathcopy_len;
827 int rval;
828 char *p;
829 int proplen;
830 char iscsi_network_path[BO_MAXOBJNAME];
832 if (bootpath == NULL || *bootpath == 0)
833 return (-1);
835 BMDPRINTF(("load_bootpath_drivers: %s\n", bootpath));
836 #ifdef _OBP
837 if (netboot_over_iscsi()) {
838 /* iscsi boot */
839 if (root_is_ramdisk) {
840 if (modloadonly("drv", "ramdisk") < 0)
841 return (-1);
843 proplen = BOP_GETPROPLEN(bootops, BP_ISCSI_NETWORK_BOOTPATH);
844 if (proplen > 0) {
845 if (BOP_GETPROP(bootops, BP_ISCSI_NETWORK_BOOTPATH,
846 iscsi_network_path) > 0) {
847 p = strchr(iscsi_network_path, ':');
848 if (p != NULL) {
849 *p = '\0';
851 pathcopy = i_ddi_strdup(iscsi_network_path,
852 KM_SLEEP);
853 pathcopy_len = strlen(pathcopy) + 1;
854 } else {
855 return (-1);
857 } else {
858 return (-1);
860 } else {
861 #endif
862 pathcopy = i_ddi_strdup(bootpath, KM_SLEEP);
863 pathcopy_len = strlen(pathcopy) + 1;
864 #ifdef _OBP
866 #endif
867 dip = path_to_devinfo(pathcopy);
869 #if defined(__i386) || defined(__amd64)
871 * i386 does not provide stub nodes for all boot devices,
872 * but we should be able to find the node for the parent,
873 * and the leaf of the boot path should be the driver name,
874 * which we go ahead and load here.
876 if (dip == NULL) {
877 char *leaf;
880 * Find last slash to build the full path to the
881 * parent of the leaf boot device
883 p = strrchr(pathcopy, '/');
884 *p++ = 0;
887 * Now isolate the driver name of the leaf device
889 leaf = p;
890 p = strchr(leaf, '@');
891 *p = 0;
893 BMDPRINTF(("load_bootpath_drivers: parent=%s leaf=%s\n",
894 bootpath, leaf));
896 dip = path_to_devinfo(pathcopy);
897 if (leaf) {
898 rval = load_boot_driver(leaf, NULL);
899 if (rval == -1) {
900 kmem_free(pathcopy, pathcopy_len);
901 return (NULL);
905 #endif
907 if (dip == NULL) {
908 cmn_err(CE_WARN, "can't bind driver for boot path <%s>",
909 bootpath);
910 kmem_free(pathcopy, pathcopy_len);
911 return (NULL);
915 * Load IP over IB driver when netbooting over IB.
916 * As per IB 1275 binding, IP over IB is represented as
917 * service on the top of the HCA node. So, there is no
918 * PROM node and generic framework cannot pre-load
919 * IP over IB driver based on the bootpath. The following
920 * code preloads IP over IB driver when doing netboot over
921 * InfiniBand.
923 if (netboot_over_ib(bootpath) &&
924 modloadonly("drv", "ibp") == -1) {
925 cmn_err(CE_CONT, "ibp: cannot load platform driver\n");
926 kmem_free(pathcopy, pathcopy_len);
927 return (NULL);
931 * The PROM node for hubs have incomplete compatible
932 * properties and therefore do not bind to the hubd driver.
933 * As a result load_bootpath_drivers() loads the usb_mid driver
934 * for hub nodes rather than the hubd driver. This causes
935 * mountroot failures when booting off USB storage. To prevent
936 * this, if we are booting via USB hubs, we preload the hubd driver.
938 if (strstr(bootpath, "/hub@") && modloadonly("drv", "hubd") == -1) {
939 cmn_err(CE_WARN, "bootpath contains a USB hub, "
940 "but cannot load hubd driver");
943 /* get rid of minor node at end of copy (if not already done above) */
944 p = strrchr(pathcopy, '/');
945 if (p) {
946 p = strchr(p, ':');
947 if (p)
948 *p = 0;
951 rval = load_parent_drivers(dip, pathcopy);
952 kmem_free(pathcopy, pathcopy_len);
953 return (rval);
960 * Load drivers required for a platform
961 * Since all hardware nodes should be available in the device
962 * tree, walk the per-driver list and load the parents of
963 * each node found. If not a hardware node, try to load it.
964 * Pseudo nexus is already loaded.
966 static int
967 load_boot_platform_modules(char *drv)
969 major_t major;
970 dev_info_t *dip;
971 char *drvname;
972 int rval = 0;
974 if ((major = ddi_name_to_major(drv)) == DDI_MAJOR_T_NONE) {
975 cmn_err(CE_CONT, "%s: no major number\n", drv);
976 return (-1);
980 * resolve aliases
982 drvname = ddi_major_to_name(major);
983 if ((major = ddi_name_to_major(drvname)) == DDI_MAJOR_T_NONE)
984 return (-1);
986 #ifdef DEBUG
987 if (strcmp(drv, drvname) == 0) {
988 BMDPRINTF(("load_boot_platform_modules: %s\n", drv));
989 } else {
990 BMDPRINTF(("load_boot_platform_modules: %s -> %s\n",
991 drv, drvname));
993 #endif /* DEBUG */
995 dip = devnamesp[major].dn_head;
996 if (dip == NULL) {
997 /* pseudo node, not-enumerated, needs to be loaded */
998 if (modloadonly("drv", drvname) == -1) {
999 cmn_err(CE_CONT, "%s: cannot load platform driver\n",
1000 drvname);
1001 rval = -1;
1003 } else {
1004 while (dip) {
1005 if (load_parent_drivers(dip, NULL) != 0)
1006 rval = -1;
1007 dip = ddi_get_next(dip);
1011 return (rval);
1016 * i_find_node: Internal routine used by path_to_devinfo
1017 * to locate a given nodeid in the device tree.
1019 struct i_path_findnode {
1020 pnode_t nodeid;
1021 dev_info_t *dip;
1024 static int
1025 i_path_find_node(dev_info_t *dev, void *arg)
1027 struct i_path_findnode *f = (struct i_path_findnode *)arg;
1030 if (ddi_get_nodeid(dev) == (int)f->nodeid) {
1031 f->dip = dev;
1032 return (DDI_WALK_TERMINATE);
1034 return (DDI_WALK_CONTINUE);
1038 * Return the devinfo node to a boot device
1040 static dev_info_t *
1041 path_to_devinfo(char *path)
1043 struct i_path_findnode fn;
1044 extern dev_info_t *top_devinfo;
1047 * Get the nodeid of the given pathname, if such a mapping exists.
1049 fn.dip = NULL;
1050 fn.nodeid = prom_finddevice(path);
1051 if (fn.nodeid != OBP_BADNODE) {
1053 * Find the nodeid in our copy of the device tree and return
1054 * whatever name we used to bind this node to a driver.
1056 ddi_walk_devs(top_devinfo, i_path_find_node, (void *)(&fn));
1059 #ifdef DEBUG
1061 * If we're bound to something other than the nodename,
1062 * note that in the message buffer and system log.
1064 if (fn.dip) {
1065 char *p, *q;
1067 p = ddi_binding_name(fn.dip);
1068 q = ddi_node_name(fn.dip);
1069 if (p && q && (strcmp(p, q) != 0)) {
1070 BMDPRINTF(("path_to_devinfo: %s bound to %s\n",
1071 path, p));
1074 #endif /* DEBUG */
1076 return (fn.dip);
1080 * This routine returns B_TRUE if the bootpath corresponds to
1081 * IP over IB driver.
1083 * The format of the bootpath for the IP over IB looks like
1084 * /pci@1f,700000/pci@1/ib@0:port=1,pkey=8001,protocol=ip
1086 * The minor node portion "port=1,pkey=8001,protocol=ip" represents
1087 * IP over IB driver.
1089 static boolean_t
1090 netboot_over_ib(char *bootpath)
1093 char *temp;
1094 boolean_t ret = B_FALSE;
1095 pnode_t node = prom_finddevice(bootpath);
1096 int len;
1097 char devicetype[OBP_MAXDRVNAME];
1099 /* Is this IB node ? */
1100 if (node == OBP_BADNODE || node == OBP_NONODE) {
1101 return (B_FALSE);
1103 len = prom_getproplen(node, OBP_DEVICETYPE);
1104 if (len <= 1 || len >= OBP_MAXDRVNAME)
1105 return (B_FALSE);
1107 (void) prom_getprop(node, OBP_DEVICETYPE, (caddr_t)devicetype);
1109 if (strncmp("ib", devicetype, 2) == 0) {
1110 /* Check for proper IP over IB string */
1111 if ((temp = strstr(bootpath, ":port=")) != NULL) {
1112 if ((temp = strstr(temp, ",pkey=")) != NULL)
1113 if ((temp = strstr(temp,
1114 ",protocol=ip")) != NULL) {
1115 ret = B_TRUE;
1119 return (ret);
1122 static boolean_t
1123 netboot_over_iscsi(void)
1125 int proplen;
1126 boolean_t ret = B_FALSE;
1127 char bootpath[OBP_MAXPATHLEN];
1129 proplen = BOP_GETPROPLEN(bootops, BP_BOOTPATH);
1130 if (proplen > 0) {
1131 if (BOP_GETPROP(bootops, BP_BOOTPATH, bootpath) > 0) {
1132 if (memcmp(bootpath, BP_ISCSI_DISK,
1133 strlen(BP_ISCSI_DISK)) == 0) {
1134 ret = B_TRUE;
1138 return (ret);