xref: /illumos-gate/usr/src/lib/fm/topo/modules/common/disk/disk.c (revision 5328fc53d11d7151861fa272e4fb0248b8f0e145)
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 (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 /*
25  * Copyright (c) 2018, Joyent, Inc.
26  */
27 
28 #include <strings.h>
29 #include <devid.h>
30 #include <pthread.h>
31 #include <inttypes.h>
32 #include <sys/dkio.h>
33 #include <sys/scsi/scsi_types.h>
34 #include <fm/topo_mod.h>
35 #include <fm/topo_list.h>
36 #include <fm/libdiskstatus.h>
37 #include <sys/fm/protocol.h>
38 #include "disk.h"
39 #include "disk_drivers.h"
40 
41 static int disk_enum(topo_mod_t *, tnode_t *, const char *,
42 	topo_instance_t, topo_instance_t, void *, void *);
43 
44 static const topo_modops_t disk_ops =
45 	{ disk_enum, NULL };
46 
47 static const topo_modinfo_t disk_info =
48 	{DISK, FM_FMRI_SCHEME_HC, DISK_VERSION, &disk_ops};
49 
50 static int
51 disk_declare_driver(topo_mod_t *mod, tnode_t *baynode, topo_list_t *dlistp,
52     char *driver)
53 {
54 	int err;
55 
56 	if (strcmp("mpt_sas", driver) == 0) {
57 		char *sas_address = NULL;
58 		tnode_t *child = NULL;
59 
60 		if ((err = disk_mptsas_find_disk(mod, baynode,
61 		    &sas_address)) != 0)
62 			return (err);
63 
64 		err = disk_declare_addr(mod, baynode, dlistp,
65 		    sas_address, &child);
66 		topo_mod_strfree(mod, sas_address);
67 
68 		return (err);
69 	}
70 
71 	topo_mod_dprintf(mod, "unknown disk driver '%s'\n", driver);
72 	return (-1);
73 }
74 
75 /*ARGSUSED*/
76 static int
77 disk_enum(topo_mod_t *mod, tnode_t *baynode,
78     const char *name, topo_instance_t min, topo_instance_t max,
79     void *arg, void *notused)
80 {
81 	char		*device, *driver, *pname;
82 	int		err;
83 	topo_list_t	*dlistp = topo_mod_getspecific(mod);
84 
85 	if (strcmp(name, DISK) != 0) {
86 		topo_mod_dprintf(mod, "disk_enum: "
87 		    "only know how to enumerate %s components.\n", DISK);
88 		return (-1);
89 	}
90 
91 	/*
92 	 * Historically we've always set the parent FRU on nodes; however, it's
93 	 * not clear why. Certain node types like USB don't want this, so we
94 	 * only do this if the parent is actually a bay.
95 	 */
96 	pname = topo_node_name(baynode);
97 	if (strcmp(pname, BAY) == 0) {
98 		nvlist_t	*fmri;
99 		if (topo_node_resource(baynode, &fmri, &err) != 0) {
100 			topo_mod_dprintf(mod, "disk_enum: "
101 			    "topo_node_resource error %s\n",
102 			    topo_strerror(err));
103 			return (-1);
104 		}
105 		if (topo_node_fru_set(baynode, fmri, 0, &err) != 0) {
106 			topo_mod_dprintf(mod, "disk_enum: "
107 			    "topo_node_fru error %s\n", topo_strerror(err));
108 			nvlist_free(fmri);
109 			return (-1);
110 		}
111 		nvlist_free(fmri);
112 	}
113 
114 	/*
115 	 * For internal storage, first check to see if we need to
116 	 * request more detail from an HBA driver.
117 	 */
118 	if (topo_prop_get_string(baynode, TOPO_PGROUP_BINDING,
119 	    TOPO_BINDING_DRIVER, &driver, &err) == 0) {
120 		err = disk_declare_driver(mod, baynode, dlistp, driver);
121 
122 		topo_mod_strfree(mod, driver);
123 		return (err);
124 	} else if (err != ETOPO_PROP_NOENT) {
125 		topo_mod_dprintf(mod, "disk_enum: "
126 		    "binding error %s\n", topo_strerror(err));
127 		return (-1);
128 	}
129 
130 	/*
131 	 * For internal storage, get the path to the occupant from the
132 	 * binding group of the bay node
133 	 */
134 	if (topo_prop_get_string(baynode, TOPO_PGROUP_BINDING,
135 	    TOPO_BINDING_OCCUPANT, &device, &err) != 0) {
136 		topo_mod_dprintf(mod, "disk_enum: "
137 		    "binding error %s\n", topo_strerror(err));
138 		return (-1);
139 	}
140 
141 
142 	/* locate and topo enumerate the disk with that path */
143 	err = disk_declare_path(mod, baynode, dlistp, device);
144 
145 	topo_mod_strfree(mod, device);
146 	return (err);
147 }
148 
149 /*ARGSUSED*/
150 int
151 _topo_init(topo_mod_t *mod, topo_version_t version)
152 {
153 	topo_list_t *dlistp;
154 
155 	/*
156 	 * Turn on module debugging output
157 	 */
158 	if (getenv("TOPODISKDEBUG") != NULL)
159 		topo_mod_setdebug(mod);
160 	topo_mod_dprintf(mod, "_topo_init: "
161 	    "initializing %s enumerator\n", DISK);
162 
163 	if (topo_mod_register(mod, &disk_info, TOPO_VERSION) != 0) {
164 		topo_mod_dprintf(mod, "_topo_init: "
165 		    "%s registration failed: %s\n", DISK, topo_mod_errmsg(mod));
166 		return (-1);		/* mod errno already set */
167 	}
168 
169 	if ((dlistp = topo_mod_zalloc(mod, sizeof (topo_list_t))) == NULL) {
170 		topo_mod_dprintf(mod, "_topo_inti: failed to allocate "
171 		    "disk list");
172 		return (-1);
173 	}
174 
175 	if (dev_list_gather(mod, dlistp) != 0) {
176 		topo_mod_unregister(mod);
177 		topo_mod_free(mod, dlistp, sizeof (topo_list_t));
178 		topo_mod_dprintf(mod, "_topo_init: "
179 		    "failed to locate disks");
180 		return (-1);
181 	}
182 
183 	topo_mod_dprintf(mod, "_topo_init: "
184 	    "%s enumerator initialized\n", DISK);
185 
186 	topo_mod_setspecific(mod, dlistp);
187 
188 	return (0);
189 }
190 
191 void
192 _topo_fini(topo_mod_t *mod)
193 {
194 	topo_list_t *dlistp = topo_mod_getspecific(mod);
195 	dev_list_free(mod, dlistp);
196 	topo_mod_free(mod, dlistp, sizeof (topo_list_t));
197 	topo_mod_unregister(mod);
198 	topo_mod_dprintf(mod, "_topo_fini: "
199 	    "%s enumerator uninitialized\n", DISK);
200 }
201