184cd04c26b064536977dfbb913a1240eaf6f708cth/*
184cd04c26b064536977dfbb913a1240eaf6f708cth * CDDL HEADER START
184cd04c26b064536977dfbb913a1240eaf6f708cth *
184cd04c26b064536977dfbb913a1240eaf6f708cth * The contents of this file are subject to the terms of the
184cd04c26b064536977dfbb913a1240eaf6f708cth * Common Development and Distribution License (the "License").
184cd04c26b064536977dfbb913a1240eaf6f708cth * You may not use this file except in compliance with the License.
184cd04c26b064536977dfbb913a1240eaf6f708cth *
184cd04c26b064536977dfbb913a1240eaf6f708cth * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
184cd04c26b064536977dfbb913a1240eaf6f708cth * or http://www.opensolaris.org/os/licensing.
184cd04c26b064536977dfbb913a1240eaf6f708cth * See the License for the specific language governing permissions
184cd04c26b064536977dfbb913a1240eaf6f708cth * and limitations under the License.
184cd04c26b064536977dfbb913a1240eaf6f708cth *
184cd04c26b064536977dfbb913a1240eaf6f708cth * When distributing Covered Code, include this CDDL HEADER in each
184cd04c26b064536977dfbb913a1240eaf6f708cth * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
184cd04c26b064536977dfbb913a1240eaf6f708cth * If applicable, add the following below this CDDL HEADER, with the
184cd04c26b064536977dfbb913a1240eaf6f708cth * fields enclosed by brackets "[]" replaced with your own identifying
184cd04c26b064536977dfbb913a1240eaf6f708cth * information: Portions Copyright [yyyy] [name of copyright owner]
184cd04c26b064536977dfbb913a1240eaf6f708cth *
184cd04c26b064536977dfbb913a1240eaf6f708cth * CDDL HEADER END
184cd04c26b064536977dfbb913a1240eaf6f708cth */
184cd04c26b064536977dfbb913a1240eaf6f708cth/*
ac88567a7a5bb7f01cf22cf366bc9d6203e24d7aHyon Kim * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
184cd04c26b064536977dfbb913a1240eaf6f708cth */
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow/*
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow * Copyright (c) 2013, Joyent, Inc. All rights reserved.
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow */
184cd04c26b064536977dfbb913a1240eaf6f708cth
184cd04c26b064536977dfbb913a1240eaf6f708cth#include <strings.h>
184cd04c26b064536977dfbb913a1240eaf6f708cth#include <devid.h>
184cd04c26b064536977dfbb913a1240eaf6f708cth#include <pthread.h>
184cd04c26b064536977dfbb913a1240eaf6f708cth#include <inttypes.h>
184cd04c26b064536977dfbb913a1240eaf6f708cth#include <sys/dkio.h>
184cd04c26b064536977dfbb913a1240eaf6f708cth#include <sys/scsi/scsi_types.h>
184cd04c26b064536977dfbb913a1240eaf6f708cth#include <fm/topo_mod.h>
184cd04c26b064536977dfbb913a1240eaf6f708cth#include <fm/topo_list.h>
184cd04c26b064536977dfbb913a1240eaf6f708cth#include <fm/libdiskstatus.h>
184cd04c26b064536977dfbb913a1240eaf6f708cth#include <sys/fm/protocol.h>
184cd04c26b064536977dfbb913a1240eaf6f708cth#include "disk.h"
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow#include "disk_drivers.h"
184cd04c26b064536977dfbb913a1240eaf6f708cth
184cd04c26b064536977dfbb913a1240eaf6f708cthstatic int disk_enum(topo_mod_t *, tnode_t *, const char *,
184cd04c26b064536977dfbb913a1240eaf6f708cth topo_instance_t, topo_instance_t, void *, void *);
184cd04c26b064536977dfbb913a1240eaf6f708cth
184cd04c26b064536977dfbb913a1240eaf6f708cthstatic const topo_modops_t disk_ops =
184cd04c26b064536977dfbb913a1240eaf6f708cth { disk_enum, NULL };
184cd04c26b064536977dfbb913a1240eaf6f708cth
602ca9ea8f9ce0933f0944601cc5d230e91a950dcthstatic const topo_modinfo_t disk_info =
184cd04c26b064536977dfbb913a1240eaf6f708cth {DISK, FM_FMRI_SCHEME_HC, DISK_VERSION, &disk_ops};
184cd04c26b064536977dfbb913a1240eaf6f708cth
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulowstatic int
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulowdisk_declare_driver(topo_mod_t *mod, tnode_t *baynode, topo_list_t *dlistp,
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow char *driver)
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow{
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow int err;
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow if (strcmp("mpt_sas", driver) == 0) {
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow char *sas_address = NULL;
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow tnode_t *child = NULL;
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow if ((err = disk_mptsas_find_disk(mod, baynode,
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow &sas_address)) != 0)
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow return (err);
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow err = disk_declare_addr(mod, baynode, dlistp,
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow sas_address, &child);
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow topo_mod_strfree(mod, sas_address);
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow return (err);
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow }
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow topo_mod_dprintf(mod, "unknown disk driver '%s'\n", driver);
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow return (-1);
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow}
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow
184cd04c26b064536977dfbb913a1240eaf6f708cth/*ARGSUSED*/
184cd04c26b064536977dfbb913a1240eaf6f708cthstatic int
602ca9ea8f9ce0933f0944601cc5d230e91a950dcthdisk_enum(topo_mod_t *mod, tnode_t *baynode,
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth const char *name, topo_instance_t min, topo_instance_t max,
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth void *arg, void *notused)
184cd04c26b064536977dfbb913a1240eaf6f708cth{
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow char *device, *driver;
184cd04c26b064536977dfbb913a1240eaf6f708cth int err;
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth nvlist_t *fmri;
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth topo_list_t *dlistp = topo_mod_getspecific(mod);
184cd04c26b064536977dfbb913a1240eaf6f708cth
184cd04c26b064536977dfbb913a1240eaf6f708cth if (strcmp(name, DISK) != 0) {
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth topo_mod_dprintf(mod, "disk_enum: "
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth "only know how to enumerate %s components.\n", DISK);
184cd04c26b064536977dfbb913a1240eaf6f708cth return (-1);
184cd04c26b064536977dfbb913a1240eaf6f708cth }
184cd04c26b064536977dfbb913a1240eaf6f708cth
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth /* set the parent fru */
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth if (topo_node_resource(baynode, &fmri, &err) != 0) {
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth topo_mod_dprintf(mod, "disk_enum: "
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth "topo_node_resource error %s\n", topo_strerror(err));
184cd04c26b064536977dfbb913a1240eaf6f708cth return (-1);
184cd04c26b064536977dfbb913a1240eaf6f708cth }
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth if (topo_node_fru_set(baynode, fmri, 0, &err) != 0) {
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth topo_mod_dprintf(mod, "disk_enum: "
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth "topo_node_fru error %s\n", topo_strerror(err));
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth nvlist_free(fmri);
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth return (-1);
184cd04c26b064536977dfbb913a1240eaf6f708cth }
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth nvlist_free(fmri);
184cd04c26b064536977dfbb913a1240eaf6f708cth
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow /*
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow * For internal storage, first check to see if we need to
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow * request more detail from an HBA driver.
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow */
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow if (topo_prop_get_string(baynode, TOPO_PGROUP_BINDING,
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow TOPO_BINDING_DRIVER, &driver, &err) == 0) {
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow err = disk_declare_driver(mod, baynode, dlistp, driver);
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow topo_mod_strfree(mod, driver);
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow return (err);
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow } else if (err != ETOPO_PROP_NOENT) {
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow topo_mod_dprintf(mod, "disk_enum: "
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow "binding error %s\n", topo_strerror(err));
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow return (-1);
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow }
aed5247ff899ec457005d93dfbdb4ffd74574695Joshua M. Clulow
184cd04c26b064536977dfbb913a1240eaf6f708cth /*
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth * For internal storage, get the path to the occupant from the
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth * binding group of the bay node
184cd04c26b064536977dfbb913a1240eaf6f708cth */
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth if (topo_prop_get_string(baynode, TOPO_PGROUP_BINDING,
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth TOPO_BINDING_OCCUPANT, &device, &err) != 0) {
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth topo_mod_dprintf(mod, "disk_enum: "
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth "binding error %s\n", topo_strerror(err));
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth return (-1);
184cd04c26b064536977dfbb913a1240eaf6f708cth }
184cd04c26b064536977dfbb913a1240eaf6f708cth
184cd04c26b064536977dfbb913a1240eaf6f708cth
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth /* locate and topo enumerate the disk with that path */
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth err = disk_declare_path(mod, baynode, dlistp, device);
184cd04c26b064536977dfbb913a1240eaf6f708cth
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth topo_mod_strfree(mod, device);
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth return (err);
184cd04c26b064536977dfbb913a1240eaf6f708cth}
184cd04c26b064536977dfbb913a1240eaf6f708cth
184cd04c26b064536977dfbb913a1240eaf6f708cth/*ARGSUSED*/
184cd04c26b064536977dfbb913a1240eaf6f708cthint
184cd04c26b064536977dfbb913a1240eaf6f708cth_topo_init(topo_mod_t *mod, topo_version_t version)
184cd04c26b064536977dfbb913a1240eaf6f708cth{
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth topo_list_t *dlistp;
184cd04c26b064536977dfbb913a1240eaf6f708cth
184cd04c26b064536977dfbb913a1240eaf6f708cth /*
184cd04c26b064536977dfbb913a1240eaf6f708cth * Turn on module debugging output
184cd04c26b064536977dfbb913a1240eaf6f708cth */
184cd04c26b064536977dfbb913a1240eaf6f708cth if (getenv("TOPODISKDEBUG") != NULL)
184cd04c26b064536977dfbb913a1240eaf6f708cth topo_mod_setdebug(mod);
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth topo_mod_dprintf(mod, "_topo_init: "
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth "initializing %s enumerator\n", DISK);
184cd04c26b064536977dfbb913a1240eaf6f708cth
184cd04c26b064536977dfbb913a1240eaf6f708cth if (topo_mod_register(mod, &disk_info, TOPO_VERSION) != 0) {
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth topo_mod_dprintf(mod, "_topo_init: "
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth "%s registration failed: %s\n", DISK, topo_mod_errmsg(mod));
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth return (-1); /* mod errno already set */
184cd04c26b064536977dfbb913a1240eaf6f708cth }
184cd04c26b064536977dfbb913a1240eaf6f708cth
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth if ((dlistp = topo_mod_zalloc(mod, sizeof (topo_list_t))) == NULL) {
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth topo_mod_dprintf(mod, "_topo_inti: failed to allocate "
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth "disk list");
184cd04c26b064536977dfbb913a1240eaf6f708cth return (-1);
184cd04c26b064536977dfbb913a1240eaf6f708cth }
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth
ac88567a7a5bb7f01cf22cf366bc9d6203e24d7aHyon Kim if (dev_list_gather(mod, dlistp) != 0) {
184cd04c26b064536977dfbb913a1240eaf6f708cth topo_mod_unregister(mod);
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth topo_mod_free(mod, dlistp, sizeof (topo_list_t));
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth topo_mod_dprintf(mod, "_topo_init: "
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth "failed to locate disks");
184cd04c26b064536977dfbb913a1240eaf6f708cth return (-1);
184cd04c26b064536977dfbb913a1240eaf6f708cth }
184cd04c26b064536977dfbb913a1240eaf6f708cth
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth topo_mod_dprintf(mod, "_topo_init: "
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth "%s enumerator initialized\n", DISK);
184cd04c26b064536977dfbb913a1240eaf6f708cth
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth topo_mod_setspecific(mod, dlistp);
184cd04c26b064536977dfbb913a1240eaf6f708cth
184cd04c26b064536977dfbb913a1240eaf6f708cth return (0);
184cd04c26b064536977dfbb913a1240eaf6f708cth}
184cd04c26b064536977dfbb913a1240eaf6f708cth
184cd04c26b064536977dfbb913a1240eaf6f708cthvoid
184cd04c26b064536977dfbb913a1240eaf6f708cth_topo_fini(topo_mod_t *mod)
184cd04c26b064536977dfbb913a1240eaf6f708cth{
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth topo_list_t *dlistp = topo_mod_getspecific(mod);
ac88567a7a5bb7f01cf22cf366bc9d6203e24d7aHyon Kim dev_list_free(mod, dlistp);
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth topo_mod_free(mod, dlistp, sizeof (topo_list_t));
184cd04c26b064536977dfbb913a1240eaf6f708cth topo_mod_unregister(mod);
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth topo_mod_dprintf(mod, "_topo_fini: "
602ca9ea8f9ce0933f0944601cc5d230e91a950dcth "%s enumerator uninitialized\n", DISK);
184cd04c26b064536977dfbb913a1240eaf6f708cth}