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 /*
  22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  * Copyright 2012 Nexenta Systems, Inc.  All rights reserved.
  25  */
  26 
  27 /*
  28  * This file contains ddi functions needed during boot and DR.
  29  * Many functions in swapgeneric.c can be moved here.
  30  *
  31  * The object file is currently linked into unix.
  32  */
  33 
  34 #include <sys/bootconf.h>
  35 #include <sys/conf.h>
  36 #include <sys/ddi_impldefs.h>
  37 #include <sys/ddi_implfuncs.h>
  38 #include <sys/hwconf.h>
  39 #include <sys/instance.h>
  40 #include <sys/kmem.h>
  41 #include <sys/modctl.h>
  42 #include <sys/promif.h>
  43 #include <sys/sunndi.h>
  44 #include <sys/ndi_impldefs.h>
  45 #include <sys/systeminfo.h>
  46 #include <sys/hwconf.h>
  47 #include <sys/sysevent_impl.h>
  48 #include <sys/sunldi_impl.h>
  49 #include <sys/disp.h>
  50 #include <sys/bootconf.h>
  51 #include <sys/fm/util.h>
  52 #include <sys/ddifm_impl.h>
  53 
  54 extern dev_info_t *top_devinfo;
  55 extern dev_info_t *scsi_vhci_dip;
  56 extern struct hwc_class *hcl_head;
  57 static char *rootname;          /* node name of top_devinfo */
  58 
  59 /*
  60  * This lock must be held while updating devi_sibling pointers of
  61  * rootnex immediate children
  62  */
  63 kmutex_t global_vhci_lock;
  64 
  65 major_t mm_major;
  66 major_t nulldriver_major;
  67 
  68 /*
  69  * Forward declarations
  70  */
  71 static void impl_create_root_class(void);
  72 static void create_devinfo_tree(void);
  73 
  74 #if defined(__x86)
  75 char *bootpath_prop = NULL;
  76 char *fstype_prop = NULL;
  77 #endif
  78 
  79 char *aoepath_prop = NULL;
  80 
  81 /*
  82  * Setup the DDI but don't necessarily init the DDI.  This will happen
  83  * later once /boot is released.
  84  */
  85 void
  86 setup_ddi(void)
  87 {
  88         impl_ddi_init_nodeid();
  89         impl_create_root_class();
  90         create_devinfo_tree();
  91         e_ddi_instance_init();
  92         impl_ddi_callback_init();
  93         log_event_init();
  94         fm_init();
  95         ndi_fm_init();
  96         irm_init();
  97 
  98         (void) i_ddi_load_drvconf(DDI_MAJOR_T_NONE);
  99 
 100         ldi_init();
 101 
 102         i_ddi_devices_init();
 103         i_ddi_read_devices_files();
 104 }
 105 
 106 /*
 107  * Perform setup actions post startup (i_ddi_io_initialized)
 108  */
 109 void
 110 setup_ddi_poststartup(void)
 111 {
 112         extern void i_ddi_start_flush_daemon(void);
 113         extern void i_ddi_irm_poststartup(void);
 114         extern void i_ddi_intr_redist_all_cpus(void);
 115 
 116         i_ddi_start_flush_daemon();
 117 
 118         /* Startup Interrupt Resource Management (IRM) */
 119         i_ddi_irm_poststartup();
 120 
 121         /*
 122          * For platforms that support INTR_WEIGHTED_DIST, we perform a
 123          * redistribution at this point (after NICs configured) so that
 124          * "isolation" relative to "ddi-intr-weight" occurs.
 125          */
 126         i_ddi_intr_redist_all_cpus();
 127 }
 128 
 129 /*
 130  * Create classes and major number bindings for the name of my root.
 131  * Called immediately before 'loadrootmodules'
 132  */
 133 static void
 134 impl_create_root_class(void)
 135 {
 136         major_t major;
 137         size_t size;
 138         char *cp;
 139 
 140         /*
 141          * The name for the root nexus is exactly as the manufacturer
 142          * placed it in the prom name property.  No translation.
 143          */
 144         if ((major = ddi_name_to_major("rootnex")) == DDI_MAJOR_T_NONE)
 145                 panic("Couldn't find major number for 'rootnex'");
 146 
 147         /*
 148          * C OBP (Serengeti) does not include the NULL when returning
 149          * the length of the name property, while this violates 1275,
 150          * Solaris needs to work around this by allocating space for
 151          * an extra character.
 152          */
 153         size = (size_t)BOP_GETPROPLEN(bootops, "mfg-name") + 1;
 154         rootname = kmem_zalloc(size, KM_SLEEP);
 155         (void) BOP_GETPROP(bootops, "mfg-name", rootname);
 156 
 157         /*
 158          * Fix conflict between OBP names and filesystem names.
 159          * Substitute '_' for '/' in the name.  Ick.  This is only
 160          * needed for the root node since '/' is not a legal name
 161          * character in an OBP device name.
 162          */
 163         for (cp = rootname; *cp; cp++)
 164                 if (*cp == '/')
 165                         *cp = '_';
 166 
 167         /*
 168          * Bind rootname to rootnex driver
 169          */
 170         if (make_mbind(rootname, major, NULL, mb_hashtab) != 0) {
 171                 cmn_err(CE_WARN, "A driver or driver alias has already "
 172                     "registered the name \"%s\".  The root nexus needs to "
 173                     "use this name, and will override the existing entry. "
 174                     "Please correct /etc/name_to_major and/or "
 175                     "/etc/driver_aliases and reboot.", rootname);
 176 
 177                 /*
 178                  * Resort to the emergency measure of blowing away the
 179                  * existing hash entry and replacing it with rootname's.
 180                  */
 181                 delete_mbind(rootname, mb_hashtab);
 182                 if (make_mbind(rootname, major, NULL, mb_hashtab) != 0)
 183                         panic("mb_hashtab: inconsistent state.");
 184         }
 185 
 186         /*
 187          * The `platform' or `implementation architecture' name has been
 188          * translated by boot to be proper for file system use.  It is
 189          * the `name' of the platform actually booted.  Note the assumption
 190          * is that the name will `fit' in the buffer platform (which is
 191          * of size SYS_NMLN, which is far bigger than will actually ever
 192          * be needed).
 193          */
 194         (void) BOP_GETPROP(bootops, "impl-arch-name", platform);
 195 
 196         /*
 197          * If boot-aoepath is defined, assume it's AoE boot and set bootpath to
 198          * aoeblk/blkdev device corresponding to specified shelf.slot numbers.
 199          */
 200         size = (size_t)BOP_GETPROPLEN(bootops, "boot-aoepath");
 201         if (size != -1) {
 202                 char    aoedev[MAXPATHLEN];
 203                 char    *delim;
 204                 int     shelf, slot;
 205 
 206                 aoepath_prop = kmem_zalloc(size, KM_SLEEP);
 207                 (void) BOP_GETPROP(bootops, "boot-aoepath", aoepath_prop);
 208                 /*
 209                  * If boot-aoepath is set to "auto", device will be
 210                  * configured later during AoE autoconfiguration.
 211                  */
 212                 if (strcmp(aoepath_prop, "auto") != 0) {
 213                         if ((delim = strchr(aoepath_prop, '.')) != NULL)
 214                                 *delim++ = '\0';
 215                         if (ddi_strtol(aoepath_prop, (char **)NULL, 10,
 216                             (long *)&shelf) != 0)
 217                                 shelf = 0;
 218                         if (delim == NULL ||
 219                             ddi_strtol(delim, (char **)NULL, 10,
 220                             (long *)&slot) != 0)
 221                                 slot = 0;
 222                         /* FIXME aoeblk@0,0 ?! */
 223                         (void) snprintf(aoedev, MAXPATHLEN,
 224                             "/aoe/aoeblk@0,0/blkdev@%d,%d",
 225                             shelf, slot);
 226                         setbootpath(aoedev);
 227                 }
 228         }
 229 
 230 #if defined(__x86)
 231         /*
 232          * Retrieve and honor the bootpath and optional fstype properties
 233          */
 234         size = (size_t)BOP_GETPROPLEN(bootops, "bootpath");
 235         if (size != -1) {
 236                 bootpath_prop = kmem_zalloc(size, KM_SLEEP);
 237                 (void) BOP_GETPROP(bootops, "bootpath", bootpath_prop);
 238                 setbootpath(bootpath_prop);
 239         }
 240 
 241         size = (size_t)BOP_GETPROPLEN(bootops, "fstype");
 242         if (size != -1) {
 243                 fstype_prop = kmem_zalloc(size, KM_SLEEP);
 244                 (void) BOP_GETPROP(bootops, "fstype", fstype_prop);
 245                 setbootfstype(fstype_prop);
 246         }
 247 #endif
 248 }
 249 
 250 /*
 251  * Note that this routine does not take into account the endianness
 252  * of the host or the device (or PROM) when retrieving properties.
 253  */
 254 static int
 255 getlongprop_buf(int id, char *name, char *buf, int maxlen)
 256 {
 257         int size;
 258 
 259         size = prom_getproplen((pnode_t)id, name);
 260         if (size <= 0 || (size > maxlen - 1))
 261                 return (-1);
 262 
 263         if (-1 == prom_getprop((pnode_t)id, name, buf))
 264                 return (-1);
 265 
 266         /*
 267          * Workaround for bugid 1085575 - OBP may return a "name" property
 268          * without null terminating the string with '\0'.  When this occurs,
 269          * append a '\0' and return (size + 1).
 270          */
 271         if (strcmp("name", name) == 0) {
 272                 if (buf[size - 1] != '\0') {
 273                         buf[size] = '\0';
 274                         size += 1;
 275                 }
 276         }
 277 
 278         return (size);
 279 }
 280 
 281 /*ARGSUSED1*/
 282 static int
 283 get_neighbors(dev_info_t *di, int flag)
 284 {
 285         register int nid, snid, cnid;
 286         dev_info_t *parent;
 287         char buf[OBP_MAXPROPNAME];
 288 
 289         if (di == NULL)
 290                 return (DDI_WALK_CONTINUE);
 291 
 292         nid = ddi_get_nodeid(di);
 293 
 294         snid = cnid = 0;
 295         switch (flag) {
 296                 case DDI_WALK_PRUNESIB:
 297                         cnid = (int)prom_childnode((pnode_t)nid);
 298                         break;
 299                 case DDI_WALK_PRUNECHILD:
 300                         snid = (int)prom_nextnode((pnode_t)nid);
 301                         break;
 302                 case 0:
 303                         snid = (int)prom_nextnode((pnode_t)nid);
 304                         cnid = (int)prom_childnode((pnode_t)nid);
 305                         break;
 306                 default:
 307                         return (DDI_WALK_TERMINATE);
 308         }
 309 
 310 
 311         if (snid && (snid != -1) && ((parent = ddi_get_parent(di)) != NULL)) {
 312                 /*
 313                  * add the first sibling that passes check_status()
 314                  */
 315                 for (; snid && (snid != -1);
 316                     snid = (int)prom_nextnode((pnode_t)snid)) {
 317                         if (getlongprop_buf(snid, OBP_NAME, buf,
 318                             sizeof (buf)) > 0) {
 319                                 if (check_status(snid, buf, parent) ==
 320                                     DDI_SUCCESS) {
 321                                         (void) ddi_add_child(parent, buf,
 322                                             snid, -1);
 323                                         break;
 324                                 }
 325                         }
 326                 }
 327         }
 328 
 329         if (cnid && (cnid != -1)) {
 330                 /*
 331                  * add the first child that passes check_status()
 332                  */
 333                 if (getlongprop_buf(cnid, OBP_NAME, buf, sizeof (buf)) > 0) {
 334                         if (check_status(cnid, buf, di) == DDI_SUCCESS) {
 335                                 (void) ddi_add_child(di, buf, cnid, -1);
 336                         } else {
 337                                 for (cnid = (int)prom_nextnode((pnode_t)cnid);
 338                                     cnid && (cnid != -1);
 339                                     cnid = (int)prom_nextnode((pnode_t)cnid)) {
 340                                         if (getlongprop_buf(cnid, OBP_NAME,
 341                                             buf, sizeof (buf)) > 0) {
 342                                                 if (check_status(cnid, buf, di)
 343                                                     == DDI_SUCCESS) {
 344                                                         (void) ddi_add_child(
 345                                                             di, buf, cnid, -1);
 346                                                         break;
 347                                                 }
 348                                         }
 349                                 }
 350                         }
 351                 }
 352         }
 353 
 354         return (DDI_WALK_CONTINUE);
 355 }
 356 
 357 static void
 358 di_dfs(dev_info_t *devi, int (*f)(dev_info_t *, int), caddr_t arg)
 359 {
 360         (void) (*f)(devi, 0);
 361         if (devi) {
 362                 di_dfs((dev_info_t *)DEVI(devi)->devi_child, f, arg);
 363                 di_dfs((dev_info_t *)DEVI(devi)->devi_sibling, f, arg);
 364         }
 365 }
 366 
 367 dev_info_t *
 368 i_ddi_create_branch(dev_info_t *pdip, int nid)
 369 {
 370         char *buf;
 371         dev_info_t *dip = NULL;
 372 
 373         if (pdip == NULL || nid == OBP_NONODE || nid == OBP_BADNODE)
 374                 return (NULL);
 375 
 376         buf = kmem_alloc(OBP_MAXPROPNAME, KM_SLEEP);
 377 
 378         if (getlongprop_buf(nid, OBP_NAME, buf, OBP_MAXPROPNAME) > 0) {
 379                 if (check_status(nid, buf, pdip) == DDI_SUCCESS)
 380                         dip = ddi_add_child(pdip, buf, nid, -1);
 381         }
 382 
 383         kmem_free(buf, OBP_MAXPROPNAME);
 384 
 385         if (dip == NULL)
 386                 return (NULL);
 387 
 388         /*
 389          * Don't create any siblings of the branch root, just
 390          * children.
 391          */
 392         (void) get_neighbors(dip, DDI_WALK_PRUNESIB);
 393 
 394         di_dfs(ddi_get_child(dip), get_neighbors, 0);
 395 
 396         return (dip);
 397 }
 398 
 399 static void
 400 create_devinfo_tree(void)
 401 {
 402         major_t major;
 403         pnode_t nodeid;
 404 
 405         i_ddi_node_cache_init();
 406 #if defined(__sparc)
 407         nodeid = prom_nextnode(0);
 408 #else /* x86 */
 409         nodeid = DEVI_SID_NODEID;
 410 #endif
 411         top_devinfo = i_ddi_alloc_node(NULL, rootname,
 412             nodeid, -1, NULL, KM_SLEEP);
 413         ndi_hold_devi(top_devinfo);     /* never release the root */
 414 
 415         i_ddi_add_devimap(top_devinfo);
 416 
 417         /*
 418          * Bind root node.
 419          * This code is special because root node has no parent
 420          */
 421         major = ddi_name_to_major("rootnex");
 422         ASSERT(major != DDI_MAJOR_T_NONE);
 423         DEVI(top_devinfo)->devi_major = major;
 424         devnamesp[major].dn_head = top_devinfo;
 425         i_ddi_set_binding_name(top_devinfo, rootname);
 426         i_ddi_set_node_state(top_devinfo, DS_BOUND);
 427 
 428         /*
 429          * Record that devinfos have been made for "rootnex."
 430          * di_dfs() is used to read the prom because it doesn't get the
 431          * next sibling until the function returns, unlike ddi_walk_devs().
 432          */
 433         di_dfs(ddi_root_node(), get_neighbors, 0);
 434 
 435 #if !defined(__sparc)
 436         /*
 437          * On x86, there is no prom. Create device tree by
 438          * probing pci config space
 439          */
 440         {
 441                 extern void impl_setup_ddi(void);
 442                 impl_setup_ddi();
 443         }
 444 #endif /* x86 */
 445 }
 446 
 447 /*
 448  * Init and attach the root node. root node is the first one to be
 449  * attached, so the process is somewhat "handcrafted".
 450  */
 451 void
 452 i_ddi_init_root()
 453 {
 454 #ifdef  DDI_PROP_DEBUG
 455         (void) ddi_prop_debug(1);       /* Enable property debugging */
 456 #endif  /* DDI_PROP_DEBUG */
 457 
 458         /*
 459          * Initialize root node
 460          */
 461         if (impl_ddi_sunbus_initchild(top_devinfo) != DDI_SUCCESS)
 462                 panic("Could not initialize root nexus");
 463 
 464         /*
 465          * Attach root node (no need to probe)
 466          * Hold both devinfo and rootnex driver so they can't go away.
 467          */
 468         DEVI(top_devinfo)->devi_ops = ndi_hold_driver(top_devinfo);
 469         ASSERT(DEV_OPS_HELD(DEVI(top_devinfo)->devi_ops));
 470         DEVI(top_devinfo)->devi_instance = e_ddi_assign_instance(top_devinfo);
 471 
 472         (void) i_ddi_load_drvconf(DEVI(top_devinfo)->devi_major);
 473 
 474         mutex_enter(&(DEVI(top_devinfo)->devi_lock));
 475         DEVI_SET_ATTACHING(top_devinfo);
 476         mutex_exit(&(DEVI(top_devinfo)->devi_lock));
 477 
 478         if (devi_attach(top_devinfo, DDI_ATTACH) != DDI_SUCCESS)
 479                 panic("Could not attach root nexus");
 480 
 481         mutex_enter(&(DEVI(top_devinfo)->devi_lock));
 482         DEVI_CLR_ATTACHING(top_devinfo);
 483         mutex_exit(&(DEVI(top_devinfo)->devi_lock));
 484 
 485         mutex_init(&global_vhci_lock, NULL, MUTEX_DEFAULT, NULL);
 486 
 487         ndi_hold_devi(top_devinfo);     /* hold it forever */
 488         i_ddi_set_node_state(top_devinfo, DS_READY);
 489 
 490         /*
 491          * Now, expand .conf children of root
 492          */
 493         (void) i_ndi_make_spec_children(top_devinfo, 0);
 494 
 495         /*
 496          * Must be set up before attaching root or pseudo drivers
 497          */
 498         pm_init_locks();
 499 
 500         /*
 501          * Attach options dip
 502          */
 503         options_dip = i_ddi_attach_pseudo_node("options");
 504 
 505         /*
 506          * Attach pseudo nexus and enumerate its children
 507          */
 508         pseudo_dip = i_ddi_attach_pseudo_node(DEVI_PSEUDO_NEXNAME);
 509         (void) i_ndi_make_spec_children(pseudo_dip, 0);
 510 
 511         /*
 512          * Attach and hold clone dip
 513          */
 514         clone_dip = i_ddi_attach_pseudo_node("clone");
 515         clone_major = ddi_driver_major(clone_dip);
 516         mm_major = ddi_name_to_major("mm");
 517         nulldriver_major = ddi_name_to_major("nulldriver");
 518 
 519         /*
 520          * Attach scsi_vhci for MPXIO, this registers scsi vhci class
 521          * with the MPXIO framework.
 522          */
 523         scsi_vhci_dip = i_ddi_attach_pseudo_node("scsi_vhci");
 524 }