Merge commit '7e934d3acc051b7ee3ef0d11571fd1225800a607'
[unleashed.git] / kernel / os / autoconf.c
blobe30bd309e94276136b3e71e9fbdc797d10316ab1
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
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * This file contains ddi functions needed during boot and DR.
28 * Many functions in swapgeneric.c can be moved here.
30 * The object file is currently linked into unix.
33 #include <sys/bootconf.h>
34 #include <sys/conf.h>
35 #include <sys/ddi_impldefs.h>
36 #include <sys/ddi_implfuncs.h>
37 #include <sys/hwconf.h>
38 #include <sys/instance.h>
39 #include <sys/kmem.h>
40 #include <sys/modctl.h>
41 #include <sys/promif.h>
42 #include <sys/sunndi.h>
43 #include <sys/ndi_impldefs.h>
44 #include <sys/systeminfo.h>
45 #include <sys/hwconf.h>
46 #include <sys/sysevent_impl.h>
47 #include <sys/sunldi_impl.h>
48 #include <sys/disp.h>
49 #include <sys/bootconf.h>
50 #include <sys/fm/util.h>
51 #include <sys/ddifm_impl.h>
53 extern dev_info_t *top_devinfo;
54 extern dev_info_t *scsi_vhci_dip;
55 extern struct hwc_class *hcl_head;
56 static char *rootname; /* node name of top_devinfo */
59 * This lock must be held while updating devi_sibling pointers of
60 * rootnex immediate children
62 kmutex_t global_vhci_lock;
64 major_t mm_major;
65 major_t nulldriver_major;
68 * Forward declarations
70 static void impl_create_root_class(void);
71 static void create_devinfo_tree(void);
73 #if defined(__x86)
74 char *bootpath_prop = NULL;
75 char *fstype_prop = NULL;
76 #endif
79 * Setup the DDI but don't necessarily init the DDI. This will happen
80 * later once /boot is released.
82 void
83 setup_ddi(void)
85 impl_ddi_init_nodeid();
86 impl_create_root_class();
87 create_devinfo_tree();
88 e_ddi_instance_init();
89 impl_ddi_callback_init();
90 log_event_init();
91 fm_init();
92 ndi_fm_init();
93 irm_init();
95 (void) i_ddi_load_drvconf(DDI_MAJOR_T_NONE);
97 ldi_init();
99 i_ddi_devices_init();
100 i_ddi_read_devices_files();
104 * Perform setup actions post startup (i_ddi_io_initialized)
106 void
107 setup_ddi_poststartup(void)
109 extern void i_ddi_start_flush_daemon(void);
110 extern void i_ddi_irm_poststartup(void);
111 extern void i_ddi_intr_redist_all_cpus(void);
113 i_ddi_start_flush_daemon();
115 /* Startup Interrupt Resource Management (IRM) */
116 i_ddi_irm_poststartup();
119 * For platforms that support INTR_WEIGHTED_DIST, we perform a
120 * redistribution at this point (after NICs configured) so that
121 * "isolation" relative to "ddi-intr-weight" occurs.
123 i_ddi_intr_redist_all_cpus();
127 * Create classes and major number bindings for the name of my root.
128 * Called immediately before 'loadrootmodules'
130 static void
131 impl_create_root_class(void)
133 major_t major;
134 size_t size;
135 char *cp;
138 * The name for the root nexus is exactly as the manufacturer
139 * placed it in the prom name property. No translation.
141 if ((major = ddi_name_to_major("rootnex")) == DDI_MAJOR_T_NONE)
142 panic("Couldn't find major number for 'rootnex'");
145 * C OBP (Serengeti) does not include the NULL when returning
146 * the length of the name property, while this violates 1275,
147 * Solaris needs to work around this by allocating space for
148 * an extra character.
150 size = (size_t)BOP_GETPROPLEN(bootops, "mfg-name") + 1;
151 rootname = kmem_zalloc(size, KM_SLEEP);
152 (void) BOP_GETPROP(bootops, "mfg-name", rootname);
155 * Fix conflict between OBP names and filesystem names.
156 * Substitute '_' for '/' in the name. Ick. This is only
157 * needed for the root node since '/' is not a legal name
158 * character in an OBP device name.
160 for (cp = rootname; *cp; cp++)
161 if (*cp == '/')
162 *cp = '_';
165 * Bind rootname to rootnex driver
167 if (make_mbind(rootname, major, NULL, mb_hashtab) != 0) {
168 cmn_err(CE_WARN, "A driver or driver alias has already "
169 "registered the name \"%s\". The root nexus needs to "
170 "use this name, and will override the existing entry. "
171 "Please correct /etc/name_to_major and/or "
172 "/etc/driver_aliases and reboot.", rootname);
175 * Resort to the emergency measure of blowing away the
176 * existing hash entry and replacing it with rootname's.
178 delete_mbind(rootname, mb_hashtab);
179 if (make_mbind(rootname, major, NULL, mb_hashtab) != 0)
180 panic("mb_hashtab: inconsistent state.");
184 * The `platform' or `implementation architecture' name has been
185 * translated by boot to be proper for file system use. It is
186 * the `name' of the platform actually booted. Note the assumption
187 * is that the name will `fit' in the buffer platform (which is
188 * of size SYS_NMLN, which is far bigger than will actually ever
189 * be needed).
191 (void) BOP_GETPROP(bootops, "impl-arch-name", platform);
193 #if defined(__x86)
195 * Retrieve and honor the bootpath and optional fstype properties
197 size = (size_t)BOP_GETPROPLEN(bootops, "bootpath");
198 if (size != -1) {
199 bootpath_prop = kmem_zalloc(size, KM_SLEEP);
200 (void) BOP_GETPROP(bootops, "bootpath", bootpath_prop);
201 setbootpath(bootpath_prop);
204 size = (size_t)BOP_GETPROPLEN(bootops, "fstype");
205 if (size != -1) {
206 fstype_prop = kmem_zalloc(size, KM_SLEEP);
207 (void) BOP_GETPROP(bootops, "fstype", fstype_prop);
208 setbootfstype(fstype_prop);
210 #endif
214 * Note that this routine does not take into account the endianness
215 * of the host or the device (or PROM) when retrieving properties.
217 static int
218 getlongprop_buf(int id, char *name, char *buf, int maxlen)
220 int size;
222 size = prom_getproplen((pnode_t)id, name);
223 if (size <= 0 || (size > maxlen - 1))
224 return (-1);
226 if (-1 == prom_getprop((pnode_t)id, name, buf))
227 return (-1);
230 * Workaround for bugid 1085575 - OBP may return a "name" property
231 * without null terminating the string with '\0'. When this occurs,
232 * append a '\0' and return (size + 1).
234 if (strcmp("name", name) == 0) {
235 if (buf[size - 1] != '\0') {
236 buf[size] = '\0';
237 size += 1;
241 return (size);
244 /*ARGSUSED1*/
245 static int
246 get_neighbors(dev_info_t *di, int flag)
248 register int nid, snid, cnid;
249 dev_info_t *parent;
250 char buf[OBP_MAXPROPNAME];
252 if (di == NULL)
253 return (DDI_WALK_CONTINUE);
255 nid = ddi_get_nodeid(di);
257 snid = cnid = 0;
258 switch (flag) {
259 case DDI_WALK_PRUNESIB:
260 cnid = (int)prom_childnode((pnode_t)nid);
261 break;
262 case DDI_WALK_PRUNECHILD:
263 snid = (int)prom_nextnode((pnode_t)nid);
264 break;
265 case 0:
266 snid = (int)prom_nextnode((pnode_t)nid);
267 cnid = (int)prom_childnode((pnode_t)nid);
268 break;
269 default:
270 return (DDI_WALK_TERMINATE);
274 if (snid && (snid != -1) && ((parent = ddi_get_parent(di)) != NULL)) {
276 * add the first sibling that passes check_status()
278 for (; snid && (snid != -1);
279 snid = (int)prom_nextnode((pnode_t)snid)) {
280 if (getlongprop_buf(snid, OBP_NAME, buf,
281 sizeof (buf)) > 0) {
282 if (check_status(snid, buf, parent) ==
283 DDI_SUCCESS) {
284 (void) ddi_add_child(parent, buf,
285 snid, -1);
286 break;
292 if (cnid && (cnid != -1)) {
294 * add the first child that passes check_status()
296 if (getlongprop_buf(cnid, OBP_NAME, buf, sizeof (buf)) > 0) {
297 if (check_status(cnid, buf, di) == DDI_SUCCESS) {
298 (void) ddi_add_child(di, buf, cnid, -1);
299 } else {
300 for (cnid = (int)prom_nextnode((pnode_t)cnid);
301 cnid && (cnid != -1);
302 cnid = (int)prom_nextnode((pnode_t)cnid)) {
303 if (getlongprop_buf(cnid, OBP_NAME,
304 buf, sizeof (buf)) > 0) {
305 if (check_status(cnid, buf, di)
306 == DDI_SUCCESS) {
307 (void) ddi_add_child(
308 di, buf, cnid, -1);
309 break;
317 return (DDI_WALK_CONTINUE);
320 static void
321 di_dfs(dev_info_t *devi, int (*f)(dev_info_t *, int), caddr_t arg)
323 (void) (*f)(devi, 0);
324 if (devi) {
325 di_dfs((dev_info_t *)DEVI(devi)->devi_child, f, arg);
326 di_dfs((dev_info_t *)DEVI(devi)->devi_sibling, f, arg);
330 dev_info_t *
331 i_ddi_create_branch(dev_info_t *pdip, int nid)
333 char *buf;
334 dev_info_t *dip = NULL;
336 if (pdip == NULL || nid == OBP_NONODE || nid == OBP_BADNODE)
337 return (NULL);
339 buf = kmem_alloc(OBP_MAXPROPNAME, KM_SLEEP);
341 if (getlongprop_buf(nid, OBP_NAME, buf, OBP_MAXPROPNAME) > 0) {
342 if (check_status(nid, buf, pdip) == DDI_SUCCESS)
343 dip = ddi_add_child(pdip, buf, nid, -1);
346 kmem_free(buf, OBP_MAXPROPNAME);
348 if (dip == NULL)
349 return (NULL);
352 * Don't create any siblings of the branch root, just
353 * children.
355 (void) get_neighbors(dip, DDI_WALK_PRUNESIB);
357 di_dfs(ddi_get_child(dip), get_neighbors, 0);
359 return (dip);
362 static void
363 create_devinfo_tree(void)
365 major_t major;
366 pnode_t nodeid;
368 i_ddi_node_cache_init();
369 nodeid = DEVI_SID_NODEID;
370 top_devinfo = i_ddi_alloc_node(NULL, rootname,
371 nodeid, -1, NULL, KM_SLEEP);
372 ndi_hold_devi(top_devinfo); /* never release the root */
374 i_ddi_add_devimap(top_devinfo);
377 * Bind root node.
378 * This code is special because root node has no parent
380 major = ddi_name_to_major("rootnex");
381 ASSERT(major != DDI_MAJOR_T_NONE);
382 DEVI(top_devinfo)->devi_major = major;
383 devnamesp[major].dn_head = top_devinfo;
384 i_ddi_set_binding_name(top_devinfo, rootname);
385 i_ddi_set_node_state(top_devinfo, DS_BOUND);
388 * Record that devinfos have been made for "rootnex."
389 * di_dfs() is used to read the prom because it doesn't get the
390 * next sibling until the function returns, unlike ddi_walk_devs().
392 di_dfs(ddi_root_node(), get_neighbors, 0);
395 * On x86, there is no prom. Create device tree by
396 * probing pci config space
399 extern void impl_setup_ddi(void);
400 impl_setup_ddi();
405 * Init and attach the root node. root node is the first one to be
406 * attached, so the process is somewhat "handcrafted".
408 void
409 i_ddi_init_root()
411 #ifdef DDI_PROP_DEBUG
412 (void) ddi_prop_debug(1); /* Enable property debugging */
413 #endif /* DDI_PROP_DEBUG */
416 * Initialize root node
418 if (impl_ddi_sunbus_initchild(top_devinfo) != DDI_SUCCESS)
419 panic("Could not initialize root nexus");
422 * Attach root node (no need to probe)
423 * Hold both devinfo and rootnex driver so they can't go away.
425 DEVI(top_devinfo)->devi_ops = ndi_hold_driver(top_devinfo);
426 ASSERT(DEV_OPS_HELD(DEVI(top_devinfo)->devi_ops));
427 DEVI(top_devinfo)->devi_instance = e_ddi_assign_instance(top_devinfo);
429 (void) i_ddi_load_drvconf(DEVI(top_devinfo)->devi_major);
431 mutex_enter(&(DEVI(top_devinfo)->devi_lock));
432 DEVI_SET_ATTACHING(top_devinfo);
433 mutex_exit(&(DEVI(top_devinfo)->devi_lock));
435 if (devi_attach(top_devinfo, DDI_ATTACH) != DDI_SUCCESS)
436 panic("Could not attach root nexus");
438 mutex_enter(&(DEVI(top_devinfo)->devi_lock));
439 DEVI_CLR_ATTACHING(top_devinfo);
440 mutex_exit(&(DEVI(top_devinfo)->devi_lock));
442 mutex_init(&global_vhci_lock, NULL, MUTEX_DEFAULT, NULL);
444 ndi_hold_devi(top_devinfo); /* hold it forever */
445 i_ddi_set_node_state(top_devinfo, DS_READY);
448 * Now, expand .conf children of root
450 (void) i_ndi_make_spec_children(top_devinfo, 0);
453 * Must be set up before attaching root or pseudo drivers
455 pm_init_locks();
458 * Attach options dip
460 options_dip = i_ddi_attach_pseudo_node("options");
463 * Attach pseudo nexus and enumerate its children
465 pseudo_dip = i_ddi_attach_pseudo_node(DEVI_PSEUDO_NEXNAME);
466 (void) i_ndi_make_spec_children(pseudo_dip, 0);
469 * Attach and hold clone dip
471 clone_dip = i_ddi_attach_pseudo_node("clone");
472 clone_major = ddi_driver_major(clone_dip);
473 mm_major = ddi_name_to_major("mm");
474 nulldriver_major = ddi_name_to_major("nulldriver");
477 * Attach scsi_vhci for MPXIO, this registers scsi vhci class
478 * with the MPXIO framework.
480 scsi_vhci_dip = i_ddi_attach_pseudo_node("scsi_vhci");