14582Scth /*
24582Scth * CDDL HEADER START
34582Scth *
44582Scth * The contents of this file are subject to the terms of the
54582Scth * Common Development and Distribution License (the "License").
64582Scth * You may not use this file except in compliance with the License.
74582Scth *
84582Scth * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94582Scth * or http://www.opensolaris.org/os/licensing.
104582Scth * See the License for the specific language governing permissions
114582Scth * and limitations under the License.
124582Scth *
134582Scth * When distributing Covered Code, include this CDDL HEADER in each
144582Scth * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
154582Scth * If applicable, add the following below this CDDL HEADER, with the
164582Scth * fields enclosed by brackets "[]" replaced with your own identifying
174582Scth * information: Portions Copyright [yyyy] [name of copyright owner]
184582Scth *
194582Scth * CDDL HEADER END
204582Scth */
214582Scth /*
22*12126SHyon.Kim@Sun.COM * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
234582Scth */
244582Scth
254582Scth #include <strings.h>
264582Scth #include <devid.h>
274582Scth #include <pthread.h>
284582Scth #include <inttypes.h>
294582Scth #include <sys/dkio.h>
304582Scth #include <sys/scsi/scsi_types.h>
314582Scth #include <fm/topo_mod.h>
324582Scth #include <fm/topo_list.h>
334582Scth #include <fm/libdiskstatus.h>
344582Scth #include <sys/fm/protocol.h>
354582Scth #include "disk.h"
364582Scth
374582Scth static int disk_enum(topo_mod_t *, tnode_t *, const char *,
384582Scth topo_instance_t, topo_instance_t, void *, void *);
394582Scth
404582Scth static const topo_modops_t disk_ops =
414582Scth { disk_enum, NULL };
424582Scth
436640Scth static const topo_modinfo_t disk_info =
444582Scth {DISK, FM_FMRI_SCHEME_HC, DISK_VERSION, &disk_ops};
454582Scth
466640Scth /*ARGSUSED*/
476640Scth static int
disk_enum(topo_mod_t * mod,tnode_t * baynode,const char * name,topo_instance_t min,topo_instance_t max,void * arg,void * notused)486640Scth disk_enum(topo_mod_t *mod, tnode_t *baynode,
496640Scth const char *name, topo_instance_t min, topo_instance_t max,
506640Scth void *arg, void *notused)
514582Scth {
526640Scth char *device;
534582Scth int err;
544582Scth nvlist_t *fmri;
556640Scth topo_list_t *dlistp = topo_mod_getspecific(mod);
564582Scth
576640Scth if (strcmp(name, DISK) != 0) {
586640Scth topo_mod_dprintf(mod, "disk_enum: "
596640Scth "only know how to enumerate %s components.\n", DISK);
606640Scth return (-1);
614582Scth }
624582Scth
634582Scth /* set the parent fru */
646640Scth if (topo_node_resource(baynode, &fmri, &err) != 0) {
656640Scth topo_mod_dprintf(mod, "disk_enum: "
666640Scth "topo_node_resource error %s\n", topo_strerror(err));
676640Scth return (-1);
684582Scth }
696640Scth if (topo_node_fru_set(baynode, fmri, 0, &err) != 0) {
706640Scth topo_mod_dprintf(mod, "disk_enum: "
716640Scth "topo_node_fru error %s\n", topo_strerror(err));
724582Scth nvlist_free(fmri);
736640Scth return (-1);
744582Scth }
754582Scth nvlist_free(fmri);
764582Scth
776640Scth /*
786640Scth * For internal storage, get the path to the occupant from the
796640Scth * binding group of the bay node
806640Scth */
816640Scth if (topo_prop_get_string(baynode, TOPO_PGROUP_BINDING,
826640Scth TOPO_BINDING_OCCUPANT, &device, &err) != 0) {
836640Scth topo_mod_dprintf(mod, "disk_enum: "
846640Scth "binding error %s\n", topo_strerror(err));
854582Scth return (-1);
864582Scth }
874582Scth
884582Scth
896640Scth /* locate and topo enumerate the disk with that path */
906640Scth err = disk_declare_path(mod, baynode, dlistp, device);
914582Scth
926640Scth topo_mod_strfree(mod, device);
936640Scth return (err);
944582Scth }
954582Scth
964582Scth /*ARGSUSED*/
974582Scth int
_topo_init(topo_mod_t * mod,topo_version_t version)984582Scth _topo_init(topo_mod_t *mod, topo_version_t version)
994582Scth {
1006640Scth topo_list_t *dlistp;
1014582Scth
1024582Scth /*
1034582Scth * Turn on module debugging output
1044582Scth */
1054582Scth if (getenv("TOPODISKDEBUG") != NULL)
1064582Scth topo_mod_setdebug(mod);
1076640Scth topo_mod_dprintf(mod, "_topo_init: "
1086640Scth "initializing %s enumerator\n", DISK);
1094582Scth
1104582Scth if (topo_mod_register(mod, &disk_info, TOPO_VERSION) != 0) {
1116640Scth topo_mod_dprintf(mod, "_topo_init: "
1126640Scth "%s registration failed: %s\n", DISK, topo_mod_errmsg(mod));
1136640Scth return (-1); /* mod errno already set */
1144582Scth }
1154582Scth
1166640Scth if ((dlistp = topo_mod_zalloc(mod, sizeof (topo_list_t))) == NULL) {
1176640Scth topo_mod_dprintf(mod, "_topo_inti: failed to allocate "
1186640Scth "disk list");
1194582Scth return (-1);
1204582Scth }
1216640Scth
122*12126SHyon.Kim@Sun.COM if (dev_list_gather(mod, dlistp) != 0) {
1234582Scth topo_mod_unregister(mod);
1246640Scth topo_mod_free(mod, dlistp, sizeof (topo_list_t));
1256640Scth topo_mod_dprintf(mod, "_topo_init: "
1266640Scth "failed to locate disks");
1274582Scth return (-1);
1284582Scth }
1294582Scth
1306640Scth topo_mod_dprintf(mod, "_topo_init: "
1316640Scth "%s enumerator initialized\n", DISK);
1324582Scth
1336640Scth topo_mod_setspecific(mod, dlistp);
1344582Scth
1354582Scth return (0);
1364582Scth }
1374582Scth
1384582Scth void
_topo_fini(topo_mod_t * mod)1394582Scth _topo_fini(topo_mod_t *mod)
1404582Scth {
1416640Scth topo_list_t *dlistp = topo_mod_getspecific(mod);
142*12126SHyon.Kim@Sun.COM dev_list_free(mod, dlistp);
1436640Scth topo_mod_free(mod, dlistp, sizeof (topo_list_t));
1444582Scth topo_mod_unregister(mod);
1456640Scth topo_mod_dprintf(mod, "_topo_fini: "
1466640Scth "%s enumerator uninitialized\n", DISK);
1474582Scth }
148