xref: /illumos-gate/usr/src/uts/common/os/swapgeneric.c (revision 491f61a1e1c1fc54a47bbcf53dbbbe1293b93b27)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /* ONC_PLUS EXTRACT START */
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /* ONC_PLUS EXTRACT END */
27 
28 /*
29  * Configure root, swap and dump devices.
30  */
31 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/sysmacros.h>
35 #include <sys/signal.h>
36 #include <sys/cred.h>
37 #include <sys/proc.h>
38 #include <sys/user.h>
39 #include <sys/conf.h>
40 #include <sys/buf.h>
41 #include <sys/systm.h>
42 #include <sys/vm.h>
43 #include <sys/reboot.h>
44 #include <sys/file.h>
45 #include <sys/vfs.h>
46 #include <sys/vnode.h>
47 #include <sys/errno.h>
48 #include <sys/kmem.h>
49 #include <sys/uio.h>
50 #include <sys/open.h>
51 #include <sys/mount.h>
52 #include <sys/kobj.h>
53 #include <sys/bootconf.h>
54 #include <sys/sysconf.h>
55 #include <sys/modctl.h>
56 #include <sys/autoconf.h>
57 #include <sys/debug.h>
58 #include <sys/fs/snode.h>
59 #include <fs/fs_subr.h>
60 #include <sys/socket.h>
61 #include <net/if.h>
62 
63 #include <sys/mkdev.h>
64 #include <sys/cmn_err.h>
65 #include <sys/console.h>
66 
67 #include <sys/conf.h>
68 #include <sys/ddi.h>
69 #include <sys/sunddi.h>
70 #include <sys/hwconf.h>
71 #include <sys/dc_ki.h>
72 #include <sys/promif.h>
73 #include <sys/bootprops.h>
74 
75 /*
76  * Local routines
77  */
78 static int preload_module(struct sysparam *, void *);
79 static struct vfssw *getfstype(char *, char *, size_t);
80 static int getphysdev(char *, char *, size_t);
81 static int load_bootpath_drivers(char *bootpath);
82 static int load_boot_driver(char *drv);
83 static int load_boot_platform_modules(char *drv);
84 static dev_info_t *path_to_devinfo(char *path);
85 static boolean_t netboot_over_ib(char *bootpath);
86 static boolean_t netboot_over_iscsi(void);
87 
88 /*
89  * Module linkage information for the kernel.
90  */
91 static struct modlmisc modlmisc = {
92 	&mod_miscops, "root and swap configuration"
93 };
94 
95 static struct modlinkage modlinkage = {
96 	MODREV_1, (void *)&modlmisc, NULL
97 };
98 
99 int
100 _init(void)
101 {
102 	return (mod_install(&modlinkage));
103 }
104 
105 int
106 _fini(void)
107 {
108 	return (mod_remove(&modlinkage));
109 }
110 
111 int
112 _info(struct modinfo *modinfop)
113 {
114 	return (mod_info(&modlinkage, modinfop));
115 }
116 
117 extern ib_boot_prop_t *iscsiboot_prop;
118 /*
119  * Configure root file system.
120  */
121 int
122 rootconf(void)
123 {
124 	int		error;
125 	struct vfssw	*vsw;
126 	extern void pm_init(void);
127 	int ret = -1;
128 	BMDPRINTF(("rootconf: fstype %s\n", rootfs.bo_fstype));
129 	BMDPRINTF(("rootconf: name %s\n", rootfs.bo_name));
130 	BMDPRINTF(("rootconf: flags 0x%x\n", rootfs.bo_flags));
131 	BMDPRINTF(("rootconf: obp_bootpath %s\n", obp_bootpath));
132 
133 	/*
134 	 * Install cluster modules that were only loaded during
135 	 * loadrootmodules().
136 	 */
137 	if (error = clboot_rootconf())
138 		return (error);
139 
140 	if (root_is_svm) {
141 		(void) strncpy(rootfs.bo_name, obp_bootpath, BO_MAXOBJNAME);
142 
143 		BMDPRINTF(("rootconf: svm: rootfs name %s\n", rootfs.bo_name));
144 		BMDPRINTF(("rootconf: svm: svm name %s\n", svm_bootpath));
145 	}
146 
147 	/*
148 	 * Run _init on the root filesystem (we already loaded it
149 	 * but we've been waiting until now to _init it) which will
150 	 * have the side-effect of running vsw_init() on this vfs.
151 	 * Because all the nfs filesystems are lumped into one
152 	 * module we need to special case it.
153 	 */
154 	if (strncmp(rootfs.bo_fstype, "nfs", 3) == 0) {
155 		if (modload("fs", "nfs") == -1) {
156 			cmn_err(CE_CONT, "Cannot initialize %s filesystem\n",
157 			    rootfs.bo_fstype);
158 			return (ENXIO);
159 		}
160 	} else {
161 		if (modload("fs", rootfs.bo_fstype) == -1) {
162 			cmn_err(CE_CONT, "Cannot initialize %s filesystem\n",
163 			    rootfs.bo_fstype);
164 			return (ENXIO);
165 		}
166 	}
167 	RLOCK_VFSSW();
168 	vsw = vfs_getvfsswbyname(rootfs.bo_fstype);
169 	RUNLOCK_VFSSW();
170 	if (vsw == NULL) {
171 		cmn_err(CE_CONT, "Cannot find %s filesystem\n",
172 		    rootfs.bo_fstype);
173 		return (ENXIO);
174 	}
175 	VFS_INIT(rootvfs, &vsw->vsw_vfsops, (caddr_t)0);
176 	VFS_HOLD(rootvfs);
177 
178 	if (root_is_svm) {
179 		rootvfs->vfs_flag |= VFS_RDONLY;
180 	}
181 
182 	/*
183 	 * This pm-releated call has to occur before root is mounted since we
184 	 * need to power up all devices.  It is placed after VFS_INIT() such
185 	 * that opening a device via ddi_lyr_ interface just before root has
186 	 * been mounted would work.
187 	 */
188 	pm_init();
189 
190 	if (netboot && iscsiboot_prop) {
191 		cmn_err(CE_WARN, "NFS boot and iSCSI boot"
192 		    " shouldn't happen in the same time");
193 		return (EINVAL);
194 	}
195 
196 	if (netboot || iscsiboot_prop) {
197 		ret = strplumb();
198 		if (ret != 0) {
199 			cmn_err(CE_WARN, "Cannot plumb network device %d", ret);
200 			return (EFAULT);
201 		}
202 	}
203 
204 	if ((ret == 0) && iscsiboot_prop) {
205 		ret = modload("drv", "iscsi");
206 		/* -1 indicates fail */
207 		if (ret == -1) {
208 			cmn_err(CE_WARN, "Failed to load iscsi module");
209 			iscsi_boot_prop_free();
210 			return (EINVAL);
211 		} else {
212 			if (!i_ddi_attach_pseudo_node("iscsi")) {
213 				cmn_err(CE_WARN,
214 				    "Failed to attach iscsi driver");
215 				iscsi_boot_prop_free();
216 				return (ENODEV);
217 			}
218 		}
219 	}
220 
221 	/*
222 	 * ufs_mountroot() ends up calling getrootdev()
223 	 * (below) which actually triggers the _init, identify,
224 	 * probe and attach of the drivers that make up root device
225 	 * bush; these are also quietly waiting in memory.
226 	 */
227 	BMDPRINTF(("rootconf: calling VFS_MOUNTROOT %s\n", rootfs.bo_fstype));
228 
229 	error = VFS_MOUNTROOT(rootvfs, ROOT_INIT);
230 	vfs_unrefvfssw(vsw);
231 	rootdev = rootvfs->vfs_dev;
232 
233 	if (error)
234 		cmn_err(CE_CONT, "Cannot mount root on %s fstype %s\n",
235 		    rootfs.bo_name, rootfs.bo_fstype);
236 	else
237 		cmn_err(CE_CONT, "?root on %s fstype %s\n",
238 		    rootfs.bo_name, rootfs.bo_fstype);
239 	return (error);
240 }
241 
242 /*
243  * Remount root on an SVM mirror root device
244  * Only supported on UFS filesystems at present
245  */
246 int
247 svm_rootconf(void)
248 {
249 	int	error;
250 	extern int ufs_remountroot(struct vfs *vfsp);
251 
252 	ASSERT(root_is_svm == 1);
253 
254 	if (strcmp(rootfs.bo_fstype, "ufs") != 0) {
255 		cmn_err(CE_CONT, "Mounting root on %s with filesystem "
256 		    "type %s is not supported\n",
257 		    rootfs.bo_name, rootfs.bo_fstype);
258 		return (EINVAL);
259 	}
260 
261 	(void) strncpy(rootfs.bo_name, svm_bootpath, BO_MAXOBJNAME);
262 
263 	BMDPRINTF(("svm_rootconf: rootfs %s\n", rootfs.bo_name));
264 
265 	error = ufs_remountroot(rootvfs);
266 
267 	if (error) {
268 		cmn_err(CE_CONT, "Cannot remount root on %s fstype %s\n",
269 		    rootfs.bo_name, rootfs.bo_fstype);
270 	} else {
271 		cmn_err(CE_CONT, "?root remounted on %s fstype %s\n",
272 		    rootfs.bo_name, rootfs.bo_fstype);
273 	}
274 	return (error);
275 }
276 
277 /*
278  * Under the assumption that our root file system is on a
279  * disk partition, get the dev_t of the partition in question.
280  *
281  * By now, boot has faithfully loaded all our modules into memory, and
282  * we've taken over resource management.  Before we go any further, we
283  * have to fire up the device drivers and stuff we need to mount the
284  * root filesystem.  That's what we do here.  Fingers crossed.
285  */
286 dev_t
287 getrootdev(void)
288 {
289 	dev_t	d;
290 
291 	d = ddi_pathname_to_dev_t(rootfs.bo_name);
292 	if ((d == NODEV) && (iscsiboot_prop != NULL)) {
293 		/* Give it another try with the 'disk' path */
294 		get_iscsi_bootpath_phy(rootfs.bo_name);
295 		d = ddi_pathname_to_dev_t(rootfs.bo_name);
296 	}
297 	if (d == NODEV)
298 		cmn_err(CE_CONT, "Cannot assemble drivers for root %s\n",
299 		    rootfs.bo_name);
300 	return (d);
301 }
302 
303 /*
304  * If booted with ASKNAME, prompt on the console for a filesystem
305  * name and return it.
306  */
307 void
308 getfsname(char *askfor, char *name, size_t namelen)
309 {
310 	if (boothowto & RB_ASKNAME) {
311 		printf("%s name: ", askfor);
312 		console_gets(name, namelen);
313 	}
314 }
315 
316 /*ARGSUSED1*/
317 static int
318 preload_module(struct sysparam *sysp, void *p)
319 {
320 	static char *wmesg = "forceload of %s failed";
321 	char *name;
322 
323 	name = sysp->sys_ptr;
324 	BMDPRINTF(("preload_module: %s\n", name));
325 	if (modloadonly(NULL, name) < 0)
326 		cmn_err(CE_WARN, wmesg, name);
327 	return (0);
328 }
329 
330 /* ONC_PLUS EXTRACT START */
331 /*
332  * We want to load all the modules needed to mount the root filesystem,
333  * so that when we start the ball rolling in 'getrootdev', every module
334  * should already be in memory, just waiting to be init-ed.
335  */
336 
337 int
338 loadrootmodules(void)
339 {
340 	struct vfssw	*vsw;
341 	char		*this;
342 	char		*name;
343 	int		err;
344 /* ONC_PLUS EXTRACT END */
345 	int		i, proplen;
346 	extern char	*impl_module_list[];
347 	extern char	*platform_module_list[];
348 
349 	/* Make sure that the PROM's devinfo tree has been created */
350 	ASSERT(ddi_root_node());
351 
352 	BMDPRINTF(("loadrootmodules: fstype %s\n", rootfs.bo_fstype));
353 	BMDPRINTF(("loadrootmodules: name %s\n", rootfs.bo_name));
354 	BMDPRINTF(("loadrootmodules: flags 0x%x\n", rootfs.bo_flags));
355 
356 	/*
357 	 * zzz We need to honor what's in rootfs if it's not null.
358 	 * non-null means use what's there.  This way we can
359 	 * change rootfs with /etc/system AND with tunetool.
360 	 */
361 	if (root_is_svm) {
362 		/* user replaced rootdev, record obp_bootpath */
363 		obp_bootpath[0] = '\0';
364 		(void) getphysdev("root", obp_bootpath, BO_MAXOBJNAME);
365 		BMDPRINTF(("loadrootmodules: obp_bootpath %s\n", obp_bootpath));
366 	} else {
367 		/*
368 		 * Get the root fstype and root device path from boot.
369 		 */
370 		rootfs.bo_fstype[0] = '\0';
371 		rootfs.bo_name[0] = '\0';
372 	}
373 
374 	/*
375 	 * This lookup will result in modloadonly-ing the root
376 	 * filesystem module - it gets _init-ed in rootconf()
377 	 */
378 	if ((vsw = getfstype("root", rootfs.bo_fstype, BO_MAXFSNAME)) == NULL)
379 		return (ENXIO);	/* in case we have no file system types */
380 
381 	(void) strcpy(rootfs.bo_fstype, vsw->vsw_name);
382 
383 	vfs_unrefvfssw(vsw);
384 
385 	/*
386 	 * Load the favored drivers of the implementation.
387 	 * e.g. 'sbus' and possibly 'zs' (even).
388 	 *
389 	 * Called whilst boot is still loaded (because boot does
390 	 * the i/o for us), and DDI services are unavailable.
391 	 */
392 	BMDPRINTF(("loadrootmodules: impl_module_list\n"));
393 	for (i = 0; (this = impl_module_list[i]) != NULL; i++) {
394 		if ((err = load_boot_driver(this)) != 0) {
395 			cmn_err(CE_WARN, "Cannot load drv/%s", this);
396 			return (err);
397 		}
398 	}
399 	/*
400 	 * Now load the platform modules (if any)
401 	 */
402 	BMDPRINTF(("loadrootmodules: platform_module_list\n"));
403 	for (i = 0; (this = platform_module_list[i]) != NULL; i++) {
404 		if ((err = load_boot_platform_modules(this)) != 0) {
405 			cmn_err(CE_WARN, "Cannot load drv/%s", this);
406 			return (err);
407 		}
408 	}
409 
410 loop:
411 	(void) getphysdev("root", rootfs.bo_name, BO_MAXOBJNAME);
412 	/*
413 	 * Given a physical pathname, load the correct set of driver
414 	 * modules into memory, including all possible parents.
415 	 *
416 	 * NB: The code sets the variable 'name' for error reporting.
417 	 */
418 	err = 0;
419 	BMDPRINTF(("loadrootmodules: rootfs %s\n", rootfs.bo_name));
420 	if (root_is_svm == 0) {
421 		BMDPRINTF(("loadrootmodules: rootfs %s\n", rootfs.bo_name));
422 		name = rootfs.bo_name;
423 		err = load_bootpath_drivers(rootfs.bo_name);
424 	}
425 
426 	/*
427 	 * Load driver modules in obp_bootpath, this is always
428 	 * required for mountroot to succeed. obp_bootpath is
429 	 * is set if rootdev is set via /etc/system, which is
430 	 * the case if booting of a SVM/VxVM mirror.
431 	 */
432 	if ((err == 0) && obp_bootpath[0] != '\0') {
433 		BMDPRINTF(("loadrootmodules: obp_bootpath %s\n", obp_bootpath));
434 		name = obp_bootpath;
435 		err = load_bootpath_drivers(obp_bootpath);
436 	}
437 
438 	if (err != 0) {
439 		cmn_err(CE_CONT, "Cannot load drivers for %s\n", name);
440 		goto out;
441 	}
442 
443 	/*
444 	 * Check to see if the booter performed DHCP configuration
445 	 * ("bootp-response" boot property exists). If so, then before
446 	 * bootops disappears we need to save the value of this property
447 	 * such that the userland dhcpagent can adopt the DHCP management
448 	 * of our primary network interface.
449 	 */
450 	proplen = BOP_GETPROPLEN(bootops, "bootp-response");
451 	if (proplen > 0) {
452 		dhcack = kmem_zalloc(proplen, KM_SLEEP);
453 		if (BOP_GETPROP(bootops, "bootp-response", dhcack) == -1) {
454 			cmn_err(CE_WARN, "BOP_GETPROP of  "
455 			    "\"bootp-response\" failed\n");
456 			kmem_free(dhcack, dhcacklen);
457 			dhcack = NULL;
458 			goto out;
459 		}
460 		dhcacklen = proplen;
461 
462 		/*
463 		 * Fetch the "netdev-path" boot property (if it exists), and
464 		 * stash it for later use by sysinfo(SI_DHCP_CACHE, ...).
465 		 */
466 		proplen = BOP_GETPROPLEN(bootops, "netdev-path");
467 		if (proplen > 0) {
468 			netdev_path = kmem_zalloc(proplen, KM_SLEEP);
469 			if (BOP_GETPROP(bootops, "netdev-path",
470 			    (uchar_t *)netdev_path) == -1) {
471 				cmn_err(CE_WARN, "BOP_GETPROP of  "
472 				    "\"netdev-path\" failed\n");
473 				kmem_free(netdev_path, proplen);
474 				goto out;
475 			}
476 		}
477 	}
478 
479 	/*
480 	 * Preload (load-only, no init) all modules which
481 	 * were added to the /etc/system file with the
482 	 * FORCELOAD keyword.
483 	 */
484 	BMDPRINTF(("loadrootmodules: preload_module\n"));
485 	(void) mod_sysctl_type(MOD_FORCELOAD, preload_module, NULL);
486 
487 /* ONC_PLUS EXTRACT START */
488 	/*
489 	 * If we booted otw then load in the plumbing
490 	 * routine now while we still can. If we didn't
491 	 * boot otw then we will load strplumb in main().
492 	 *
493 	 * NFS is actually a set of modules, the core routines,
494 	 * a diskless helper module, rpcmod, and the tli interface.  Load
495 	 * them now while we still can.
496 	 *
497 	 * Because we glomb all versions of nfs into a single module
498 	 * we check based on the initial string "nfs".
499 	 *
500 	 * XXX: A better test for this is to see if device_type
501 	 * XXX: from the PROM is "network".
502 	 */
503 
504 	if (strncmp(rootfs.bo_fstype, "nfs", 3) == 0) {
505 		++netboot;
506 
507 		/*
508 		 * Preload (load-only, no init) the dacf module. We cannot
509 		 * init the module because one of its requisite modules is
510 		 * dld whose _init function will call taskq_create(), which
511 		 * will panic the system at this point.
512 		 */
513 		if ((err = modloadonly("dacf", "net_dacf")) < 0)  {
514 			cmn_err(CE_CONT, "Cannot load dacf/net_dacf\n");
515 			goto out;
516 		}
517 		if ((err = modload("misc", "tlimod")) < 0)  {
518 			cmn_err(CE_CONT, "Cannot load misc/tlimod\n");
519 			goto out;
520 		}
521 		if ((err = modload("strmod", "rpcmod")) < 0)  {
522 			cmn_err(CE_CONT, "Cannot load strmod/rpcmod\n");
523 			goto out;
524 		}
525 		if ((err = modload("misc", "nfs_dlboot")) < 0)  {
526 			cmn_err(CE_CONT, "Cannot load misc/nfs_dlboot\n");
527 			goto out;
528 		}
529 		if ((err = modload("mac", "mac_ether")) < 0)  {
530 			cmn_err(CE_CONT, "Cannot load mac/mac_ether\n");
531 			goto out;
532 		}
533 		if ((err = modload("misc", "strplumb")) < 0)  {
534 			cmn_err(CE_CONT, "Cannot load misc/strplumb\n");
535 			goto out;
536 		}
537 		if ((err = strplumb_load()) < 0) {
538 			goto out;
539 		}
540 	}
541 	if (netboot_over_iscsi() == B_TRUE) {
542 		/* iscsi boot */
543 		if ((err = modloadonly("dacf", "net_dacf")) < 0) {
544 			cmn_err(CE_CONT, "Cannot load dacf/net_dacf\n");
545 			goto out;
546 		}
547 		if ((err = modload("misc", "tlimod")) < 0) {
548 			cmn_err(CE_CONT, "Cannot load misc/tlimod\n");
549 			goto out;
550 		}
551 		if ((err = modload("mac", "mac_ether")) < 0) {
552 			cmn_err(CE_CONT, "Cannot load mac/mac_ether\n");
553 			goto out;
554 		}
555 		if ((err = modloadonly("drv", "iscsi")) < 0) {
556 			cmn_err(CE_CONT, "Cannot load drv/iscsi\n");
557 			goto out;
558 		}
559 		if ((err = modloadonly("drv", "ssd")) < 0) {
560 			cmn_err(CE_CONT, "Cannot load drv/ssd\n");
561 			goto out;
562 		}
563 		if ((err = modloadonly("drv", "sd")) < 0) {
564 			cmn_err(CE_CONT, "Cannot load drv/sd\n");
565 			goto out;
566 		}
567 		if ((err = modload("misc", "strplumb")) < 0) {
568 			cmn_err(CE_CONT, "Cannot load misc/strplumb\n");
569 			goto out;
570 		}
571 		if ((err = strplumb_load()) < 0) {
572 			goto out;
573 		}
574 	}
575 	/*
576 	 * Preload modules needed for booting as a cluster.
577 	 */
578 	err = clboot_loadrootmodules();
579 
580 out:
581 	if (err != 0 && (boothowto & RB_ASKNAME))
582 		goto loop;
583 
584 	return (err);
585 }
586 /* ONC_PLUS EXTRACT END */
587 
588 static int
589 get_bootpath_prop(char *bootpath)
590 {
591 	if (root_is_ramdisk) {
592 		if (BOP_GETPROP(bootops, "bootarchive", bootpath) == -1)
593 			return (-1);
594 		(void) strlcat(bootpath, ":a", BO_MAXOBJNAME);
595 	} else {
596 		/*
597 		 * Look for the 1275 compliant name 'bootpath' first,
598 		 * but make certain it has a non-NULL value as well.
599 		 */
600 		if ((BOP_GETPROP(bootops, "bootpath", bootpath) == -1) ||
601 		    strlen(bootpath) == 0) {
602 			if (BOP_GETPROP(bootops,
603 			    "boot-path", bootpath) == -1)
604 				return (-1);
605 		}
606 		if (memcmp(bootpath, BP_ISCSI_DISK,
607 		    strlen(BP_ISCSI_DISK)) == 0) {
608 			/* iscsi boot */
609 			get_iscsi_bootpath_vhci(bootpath);
610 		}
611 	}
612 	return (0);
613 }
614 
615 static int
616 get_fstype_prop(char *fstype)
617 {
618 	char *prop = (root_is_ramdisk) ? "archive-fstype" : "fstype";
619 
620 	return (BOP_GETPROP(bootops, prop, fstype));
621 }
622 
623 /*
624  * Get the name of the root or swap filesystem type, and return
625  * the corresponding entry in the vfs switch.
626  *
627  * If we're not asking the user, and we're trying to find the
628  * root filesystem type, we ask boot for the filesystem
629  * type that it came from and use that.  Similarly, if we're
630  * trying to find the swap filesystem, we try and derive it from
631  * the root filesystem type.
632  *
633  * If we are booting via NFS we currently have these options:
634  *	nfs -	dynamically choose NFS V2. V3, or V4 (default)
635  *	nfs2 -	force NFS V2
636  *	nfs3 -	force NFS V3
637  *	nfs4 -	force NFS V4
638  * Because we need to maintain backward compatibility with the naming
639  * convention that the NFS V2 filesystem name is "nfs" (see vfs_conf.c)
640  * we need to map "nfs" => "nfsdyn" and "nfs2" => "nfs".  The dynamic
641  * nfs module will map the type back to either "nfs", "nfs3", or "nfs4".
642  * This is only for root filesystems, all other uses such as cachefs
643  * will expect that "nfs" == NFS V2.
644  *
645  * If the filesystem isn't already loaded, vfs_getvfssw() will load
646  * it for us, but if (at the time we call it) modrootloaded is
647  * still not set, it won't run the filesystems _init routine (and
648  * implicitly it won't run the filesystems vsw_init() entry either).
649  * We do that explicitly in rootconf().
650  */
651 static struct vfssw *
652 getfstype(char *askfor, char *fsname, size_t fsnamelen)
653 {
654 	struct vfssw *vsw;
655 	static char defaultfs[BO_MAXFSNAME];
656 	int root = 0;
657 
658 	if (strcmp(askfor, "root") == 0) {
659 		(void) get_fstype_prop(defaultfs);
660 		root++;
661 	} else {
662 		(void) strcpy(defaultfs, "swapfs");
663 	}
664 
665 	if (boothowto & RB_ASKNAME) {
666 		for (*fsname = '\0'; *fsname == '\0'; *fsname = '\0') {
667 			printf("%s filesystem type [%s]: ", askfor, defaultfs);
668 			console_gets(fsname, fsnamelen);
669 			if (*fsname == '\0')
670 				(void) strcpy(fsname, defaultfs);
671 			if (root) {
672 				if (strcmp(fsname, "nfs2") == 0)
673 					(void) strcpy(fsname, "nfs");
674 				else if (strcmp(fsname, "nfs") == 0)
675 					(void) strcpy(fsname, "nfsdyn");
676 			}
677 			if ((vsw = vfs_getvfssw(fsname)) != NULL)
678 				return (vsw);
679 			printf("Unknown filesystem type '%s'\n", fsname);
680 		}
681 	} else if (*fsname == '\0') {
682 		fsname = defaultfs;
683 	}
684 	if (*fsname == '\0') {
685 		return (NULL);
686 	}
687 
688 	if (root) {
689 		if (strcmp(fsname, "nfs2") == 0)
690 			(void) strcpy(fsname, "nfs");
691 		else if (strcmp(fsname, "nfs") == 0)
692 			(void) strcpy(fsname, "nfsdyn");
693 	}
694 
695 	return (vfs_getvfssw(fsname));
696 }
697 
698 
699 /*
700  * Get a physical device name, and maybe load and attach
701  * the driver.
702  *
703  * XXX	Need better checking of whether or not a device
704  *	actually exists if the user typed in a pathname.
705  *
706  * XXX	Are we sure we want to expose users to this sort
707  *	of physical namespace gobbledygook (now there's
708  *	a word to conjure with..)
709  *
710  * XXX	Note that on an OBP machine, we can easily ask the
711  *	prom and pretty-print some plausible set of bootable
712  *	devices.  We can also user the prom to verify any
713  *	such device.  Later tim.. later.
714  */
715 static int
716 getphysdev(char *askfor, char *name, size_t namelen)
717 {
718 	static char fmt[] = "Enter physical name of %s device\n[%s]: ";
719 	dev_t dev;
720 	static char defaultpath[BO_MAXOBJNAME];
721 
722 	/*
723 	 * Establish 'default' values - we get the root device from
724 	 * boot, and we infer the swap device is the same but with
725 	 * a 'b' on the end instead of an 'a'.  A first stab at
726 	 * ease-of-use ..
727 	 */
728 	if (strcmp(askfor, "root") == 0) {
729 		if (get_bootpath_prop(defaultpath) == -1)
730 			boothowto |= RB_ASKNAME | RB_VERBOSE;
731 	} else {
732 		(void) strcpy(defaultpath, rootfs.bo_name);
733 		defaultpath[strlen(defaultpath) - 1] = 'b';
734 	}
735 
736 retry:
737 	if (boothowto & RB_ASKNAME) {
738 		printf(fmt, askfor, defaultpath);
739 		console_gets(name, namelen);
740 	}
741 	if (*name == '\0')
742 		(void) strcpy(name, defaultpath);
743 
744 	if (strcmp(askfor, "swap") == 0)   {
745 
746 		/*
747 		 * Try to load and install the swap device driver.
748 		 */
749 		dev = ddi_pathname_to_dev_t(name);
750 
751 		if (dev == (dev_t)-1)  {
752 			printf("Not a supported device for swap.\n");
753 			boothowto |= RB_ASKNAME | RB_VERBOSE;
754 			goto retry;
755 		}
756 
757 		/*
758 		 * Ensure that we're not trying to swap on the floppy.
759 		 */
760 		if (strncmp(ddi_major_to_name(getmajor(dev)), "fd", 2) == 0) {
761 			printf("Too dangerous to swap on the floppy\n");
762 			if (boothowto & RB_ASKNAME)
763 				goto retry;
764 			return (-1);
765 		}
766 	}
767 
768 	return (0);
769 }
770 
771 
772 /*
773  * Load a driver needed to boot.
774  */
775 static int
776 load_boot_driver(char *drv)
777 {
778 	char		*drvname;
779 	major_t		major;
780 #ifdef	sparc
781 	struct devnames *dnp;
782 	ddi_prop_t	*propp;
783 	char		*module;
784 	char		*dir, *mf;
785 	int		plen;
786 	int		mlen;
787 #endif	/* sparc */
788 
789 	if ((major = ddi_name_to_major(drv)) == DDI_MAJOR_T_NONE) {
790 		cmn_err(CE_CONT, "%s: no major number\n", drv);
791 		return (-1);
792 	}
793 	/*
794 	 * resolve aliases
795 	 */
796 	drvname = ddi_major_to_name(major);
797 
798 #ifdef	DEBUG
799 	if (strcmp(drv, drvname) == 0) {
800 		BMDPRINTF(("load_boot_driver: %s\n", drv));
801 	} else {
802 		BMDPRINTF(("load_boot_driver: %s -> %s\n", drv, drvname));
803 	}
804 #endif	/* DEBUG */
805 
806 	if (modloadonly("drv", drvname) == -1) {
807 		cmn_err(CE_CONT, "%s: cannot load driver\n", drvname);
808 		return (-1);
809 	}
810 
811 #ifdef	sparc
812 	/*
813 	 * NOTE: this can be removed when newboot-sparc is delivered.
814 	 *
815 	 * Check to see if the driver had a 'ddi-forceload' global driver.conf
816 	 * property to identify additional modules that need to be loaded.
817 	 * The driver still needs to use ddi_modopen() to open these modules,
818 	 * but the 'ddi-forceload' property allows the modules to be loaded
819 	 * into memory prior to lights-out, so that driver ddi_modopen()
820 	 * calls during lights-out (when mounting root) will work correctly.
821 	 * Use of 'ddi-forceload' is only required for drivers involved in
822 	 * getting root mounted.
823 	 */
824 	dnp = &devnamesp[major];
825 	if (dnp->dn_global_prop_ptr && dnp->dn_global_prop_ptr->prop_list &&
826 	    ((propp = i_ddi_prop_search(DDI_DEV_T_ANY,
827 	    "ddi-forceload", DDI_PROP_TYPE_STRING,
828 	    &dnp->dn_global_prop_ptr->prop_list)) != NULL)) {
829 
830 		module = (char *)propp->prop_val;
831 		plen = propp->prop_len;
832 		while (plen > 0) {
833 			mlen = strlen(module);
834 			mf = strrchr(module, '/');
835 			if (mf) {
836 				dir = module;
837 				*mf++ = '\0';		/* '/' -> '\0' */
838 			} else {
839 				dir = "misc";
840 				mf = module;
841 			}
842 			if (modloadonly(dir, mf) == -1)
843 				cmn_err(CE_CONT,
844 				    "misc/%s: can't load module\n", mf);
845 			if (mf != module)
846 				*(mf - 1) = '/';	/* '\0' -> '/' */
847 
848 			module += mlen + 1;
849 			plen -= mlen + 1;
850 		}
851 	}
852 #endif	/* sparc */
853 
854 	return (0);
855 }
856 
857 
858 /*
859  * For a given instance, load that driver and its parents
860  */
861 static int
862 load_parent_drivers(dev_info_t *dip, char *path)
863 {
864 	int	rval = 0;
865 	major_t	major = DDI_MAJOR_T_NONE;
866 	char	*drv;
867 	char	*p;
868 
869 	while (dip) {
870 		/* check for path-oriented alias */
871 		if (path)
872 			major = ddi_name_to_major(path);
873 		else
874 			major = DDI_MAJOR_T_NONE;
875 
876 		if (major != DDI_MAJOR_T_NONE)
877 			drv = ddi_major_to_name(major);
878 		else
879 			drv = ddi_binding_name(dip);
880 
881 		if (load_boot_driver(drv) != 0)
882 			rval = -1;
883 
884 		dip = ddi_get_parent(dip);
885 		if (path) {
886 			p = strrchr(path, '/');
887 			if (p)
888 				*p = 0;
889 		}
890 	}
891 
892 	return (rval);
893 }
894 
895 
896 /*
897  * For a given path to a boot device,
898  * load that driver and all its parents.
899  */
900 static int
901 load_bootpath_drivers(char *bootpath)
902 {
903 	dev_info_t	*dip;
904 	char		*pathcopy;
905 	int		pathcopy_len;
906 	int		rval;
907 	char		*p;
908 	int		proplen;
909 	char		iscsi_network_path[BO_MAXOBJNAME];
910 
911 	if (bootpath == NULL || *bootpath == 0)
912 		return (-1);
913 
914 	BMDPRINTF(("load_bootpath_drivers: %s\n", bootpath));
915 #ifdef _OBP
916 	if (netboot_over_iscsi()) {
917 		/* iscsi boot */
918 		if (root_is_ramdisk) {
919 			modloadonly("drv", "ramdisk");
920 		}
921 		proplen = BOP_GETPROPLEN(bootops, BP_ISCSI_NETWORK_BOOTPATH);
922 		if (proplen > 0) {
923 			if (BOP_GETPROP(bootops, BP_ISCSI_NETWORK_BOOTPATH,
924 			    iscsi_network_path) > 0) {
925 				p = strchr(iscsi_network_path, ':');
926 				if (p != NULL) {
927 					*p = '\0';
928 				}
929 				pathcopy = i_ddi_strdup(iscsi_network_path,
930 				    KM_SLEEP);
931 				pathcopy_len = strlen(pathcopy) + 1;
932 			} else {
933 				return (-1);
934 			}
935 		} else {
936 			return (-1);
937 		}
938 	} else {
939 #endif
940 		pathcopy = i_ddi_strdup(bootpath, KM_SLEEP);
941 		pathcopy_len = strlen(pathcopy) + 1;
942 #ifdef _OBP
943 	}
944 #endif
945 	dip = path_to_devinfo(pathcopy);
946 
947 #if defined(__i386) || defined(__amd64)
948 	/*
949 	 * i386 does not provide stub nodes for all boot devices,
950 	 * but we should be able to find the node for the parent,
951 	 * and the leaf of the boot path should be the driver name,
952 	 * which we go ahead and load here.
953 	 */
954 	if (dip == NULL) {
955 		char	*leaf;
956 
957 		/*
958 		 * Find last slash to build the full path to the
959 		 * parent of the leaf boot device
960 		 */
961 		p = strrchr(pathcopy, '/');
962 		*p++ = 0;
963 
964 		/*
965 		 * Now isolate the driver name of the leaf device
966 		 */
967 		leaf = p;
968 		p = strchr(leaf, '@');
969 		*p = 0;
970 
971 		BMDPRINTF(("load_bootpath_drivers: parent=%s leaf=%s\n",
972 		    bootpath, leaf));
973 
974 		dip = path_to_devinfo(pathcopy);
975 		if (leaf) {
976 			rval = load_boot_driver(leaf, NULL);
977 			if (rval == -1) {
978 				kmem_free(pathcopy, pathcopy_len);
979 				return (NULL);
980 			}
981 		}
982 	}
983 #endif
984 
985 	if (dip == NULL) {
986 		cmn_err(CE_WARN, "can't bind driver for boot path <%s>",
987 		    bootpath);
988 		kmem_free(pathcopy, pathcopy_len);
989 		return (NULL);
990 	}
991 
992 	/*
993 	 * Load IP over IB driver when netbooting over IB.
994 	 * As per IB 1275 binding, IP over IB is represented as
995 	 * service on the top of the HCA node. So, there is no
996 	 * PROM node and generic framework cannot pre-load
997 	 * IP over IB driver based on the bootpath. The following
998 	 * code preloads IP over IB driver when doing netboot over
999 	 * InfiniBand.
1000 	 */
1001 	if (netboot_over_ib(bootpath) &&
1002 	    modloadonly("drv", "ibd") == -1) {
1003 		cmn_err(CE_CONT, "ibd: cannot load platform driver\n");
1004 		kmem_free(pathcopy, pathcopy_len);
1005 		return (NULL);
1006 	}
1007 
1008 	/*
1009 	 * The PROM node for hubs have incomplete compatible
1010 	 * properties and therefore do not bind to the hubd driver.
1011 	 * As a result load_bootpath_drivers() loads the usb_mid driver
1012 	 * for hub nodes rather than the hubd driver. This causes
1013 	 * mountroot failures when booting off USB storage. To prevent
1014 	 * this, if we are booting via USB hubs, we preload the hubd driver.
1015 	 */
1016 	if (strstr(bootpath, "/hub@") && modloadonly("drv", "hubd") == -1) {
1017 		cmn_err(CE_WARN, "bootpath contains a USB hub, "
1018 		    "but cannot load hubd driver");
1019 	}
1020 
1021 	/* get rid of minor node at end of copy (if not already done above) */
1022 	p = strrchr(pathcopy, '/');
1023 	if (p) {
1024 		p = strchr(p, ':');
1025 		if (p)
1026 			*p = 0;
1027 	}
1028 
1029 	rval = load_parent_drivers(dip, pathcopy);
1030 	kmem_free(pathcopy, pathcopy_len);
1031 	return (rval);
1032 }
1033 
1034 
1035 
1036 
1037 /*
1038  * Load drivers required for a platform
1039  * Since all hardware nodes should be available in the device
1040  * tree, walk the per-driver list and load the parents of
1041  * each node found. If not a hardware node, try to load it.
1042  * Pseudo nexus is already loaded.
1043  */
1044 static int
1045 load_boot_platform_modules(char *drv)
1046 {
1047 	major_t	major;
1048 	dev_info_t *dip;
1049 	char	*drvname;
1050 	int	rval = 0;
1051 
1052 	if ((major = ddi_name_to_major(drv)) == DDI_MAJOR_T_NONE) {
1053 		cmn_err(CE_CONT, "%s: no major number\n", drv);
1054 		return (-1);
1055 	}
1056 
1057 	/*
1058 	 * resolve aliases
1059 	 */
1060 	drvname = ddi_major_to_name(major);
1061 	if ((major = ddi_name_to_major(drvname)) == DDI_MAJOR_T_NONE)
1062 		return (-1);
1063 
1064 #ifdef	DEBUG
1065 	if (strcmp(drv, drvname) == 0) {
1066 		BMDPRINTF(("load_boot_platform_modules: %s\n", drv));
1067 	} else {
1068 		BMDPRINTF(("load_boot_platform_modules: %s -> %s\n",
1069 		    drv, drvname));
1070 	}
1071 #endif	/* DEBUG */
1072 
1073 	dip = devnamesp[major].dn_head;
1074 	if (dip == NULL) {
1075 		/* pseudo node, not-enumerated, needs to be loaded */
1076 		if (modloadonly("drv", drvname) == -1) {
1077 			cmn_err(CE_CONT, "%s: cannot load platform driver\n",
1078 			    drvname);
1079 			rval = -1;
1080 		}
1081 	} else {
1082 		while (dip) {
1083 			if (load_parent_drivers(dip, NULL) != 0)
1084 				rval = -1;
1085 			dip = ddi_get_next(dip);
1086 		}
1087 	}
1088 
1089 	return (rval);
1090 }
1091 
1092 
1093 /*
1094  * i_find_node: Internal routine used by path_to_devinfo
1095  * to locate a given nodeid in the device tree.
1096  */
1097 struct i_path_findnode {
1098 	pnode_t nodeid;
1099 	dev_info_t *dip;
1100 };
1101 
1102 static int
1103 i_path_find_node(dev_info_t *dev, void *arg)
1104 {
1105 	struct i_path_findnode *f = (struct i_path_findnode *)arg;
1106 
1107 
1108 	if (ddi_get_nodeid(dev) == (int)f->nodeid) {
1109 		f->dip = dev;
1110 		return (DDI_WALK_TERMINATE);
1111 	}
1112 	return (DDI_WALK_CONTINUE);
1113 }
1114 
1115 /*
1116  * Return the devinfo node to a boot device
1117  */
1118 static dev_info_t *
1119 path_to_devinfo(char *path)
1120 {
1121 	struct i_path_findnode fn;
1122 	extern dev_info_t *top_devinfo;
1123 
1124 	/*
1125 	 * Get the nodeid of the given pathname, if such a mapping exists.
1126 	 */
1127 	fn.dip = NULL;
1128 	fn.nodeid = prom_finddevice(path);
1129 	if (fn.nodeid != OBP_BADNODE) {
1130 		/*
1131 		 * Find the nodeid in our copy of the device tree and return
1132 		 * whatever name we used to bind this node to a driver.
1133 		 */
1134 		ddi_walk_devs(top_devinfo, i_path_find_node, (void *)(&fn));
1135 	}
1136 
1137 #ifdef	DEBUG
1138 	/*
1139 	 * If we're bound to something other than the nodename,
1140 	 * note that in the message buffer and system log.
1141 	 */
1142 	if (fn.dip) {
1143 		char *p, *q;
1144 
1145 		p = ddi_binding_name(fn.dip);
1146 		q = ddi_node_name(fn.dip);
1147 		if (p && q && (strcmp(p, q) != 0)) {
1148 			BMDPRINTF(("path_to_devinfo: %s bound to %s\n",
1149 			    path, p));
1150 		}
1151 	}
1152 #endif	/* DEBUG */
1153 
1154 	return (fn.dip);
1155 }
1156 
1157 /*
1158  * This routine returns B_TRUE if the bootpath corresponds to
1159  * IP over IB driver.
1160  *
1161  * The format of the bootpath for the IP over IB looks like
1162  * /pci@1f,700000/pci@1/ib@0:port=1,pkey=8001,protocol=ip
1163  *
1164  * The minor node portion "port=1,pkey=8001,protocol=ip" represents
1165  * IP over IB driver.
1166  */
1167 static boolean_t
1168 netboot_over_ib(char *bootpath)
1169 {
1170 
1171 	char		*temp;
1172 	boolean_t	ret = B_FALSE;
1173 	pnode_t		node = prom_finddevice(bootpath);
1174 	int		len;
1175 	char		devicetype[OBP_MAXDRVNAME];
1176 
1177 	/* Is this IB node ? */
1178 	if (node == OBP_BADNODE || node == OBP_NONODE) {
1179 		return (B_FALSE);
1180 	}
1181 	len = prom_getproplen(node, OBP_DEVICETYPE);
1182 	if (len <= 1 || len >= OBP_MAXDRVNAME)
1183 		return (B_FALSE);
1184 
1185 	(void) prom_getprop(node, OBP_DEVICETYPE, (caddr_t)devicetype);
1186 
1187 	if (strncmp("ib", devicetype, 2) == 0) {
1188 		/* Check for proper IP over IB string */
1189 		if ((temp = strstr(bootpath, ":port=")) != NULL) {
1190 			if ((temp = strstr(temp, ",pkey=")) != NULL)
1191 				if ((temp = strstr(temp,
1192 				    ",protocol=ip")) != NULL) {
1193 					ret = B_TRUE;
1194 				}
1195 		}
1196 	}
1197 	return (ret);
1198 }
1199 
1200 static boolean_t
1201 netboot_over_iscsi(void)
1202 {
1203 	int proplen;
1204 	boolean_t	ret = B_FALSE;
1205 	char	bootpath[OBP_MAXPATHLEN];
1206 
1207 	proplen = BOP_GETPROPLEN(bootops, BP_BOOTPATH);
1208 	if (proplen > 0) {
1209 		if (BOP_GETPROP(bootops, BP_BOOTPATH, bootpath) > 0) {
1210 			if (memcmp(bootpath, BP_ISCSI_DISK,
1211 			    strlen(BP_ISCSI_DISK)) == 0) {
1212 				ret = B_TRUE;
1213 			}
1214 		}
1215 	}
1216 	return (ret);
1217 }
1218