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 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2019 Joyent, Inc.
25  */
26 
27 /*
28  * Functions in this file are shared between the disk and ses enumerators.
29  *
30  * A topo_list_t of all disks is returned by a successful disk_list_gather()
31  * call, and the list is freed by a disk_list_free(). To create a 'disk' topo
32  * node below a specific 'bay' parent node either disk_declare_path() or
33  * disk_declare_addr() are called. The caller determines which 'disk' is
34  * in which 'bay'. A disk's 'label' and 'authority' information come from
35  * its parent 'bay' node.
36  */
37 
38 #include <ctype.h>
39 #include <strings.h>
40 #include <libdevinfo.h>
41 #include <libdiskmgt.h>
42 #include <devid.h>
43 #include <sys/libdevid.h>
44 #include <pthread.h>
45 #include <inttypes.h>
46 #include <sys/dkio.h>
47 #include <sys/scsi/scsi_types.h>
48 #include <fm/topo_mod.h>
49 #include <fm/topo_list.h>
50 #include <fm/libdiskstatus.h>
51 #include <sys/fm/protocol.h>
52 #include <sys/scsi/generic/inquiry.h>
53 #include "disk.h"
54 
55 /* common callback information for di_walk_node() and di_devlink_walk */
56 typedef struct disk_cbdata {
57 	topo_mod_t		*dcb_mod;
58 	topo_list_t		*dcb_list;
59 
60 	di_devlink_handle_t	dcb_devhdl;
61 	dev_di_node_t		*dcb_dnode;	/* for di_devlink_walk only */
62 } disk_cbdata_t;
63 
64 /*
65  * Given a /devices path for a whole disk, appending this extension gives the
66  * path to a raw device that can be opened.
67  */
68 #if defined(__i386) || defined(__amd64)
69 #define	PHYS_EXTN	":q,raw"
70 #elif defined(__sparc) || defined(__sparcv9)
71 #define	PHYS_EXTN	":c,raw"
72 #else
73 #error	Unknown architecture
74 #endif
75 
76 /*
77  * Methods for disks. This is used by the disk-transport module to
78  * generate ereports based off SCSI disk status.
79  */
80 static int disk_status(topo_mod_t *, tnode_t *, topo_version_t,
81 	nvlist_t *, nvlist_t **);
82 
83 static const topo_method_t disk_methods[] = {
84 	{ TOPO_METH_DISK_STATUS, TOPO_METH_DISK_STATUS_DESC,
85 	    TOPO_METH_DISK_STATUS_VERSION, TOPO_STABILITY_INTERNAL,
86 	    disk_status },
87 	{ NULL }
88 };
89 
90 static int disk_temp_reading(topo_mod_t *, tnode_t *, topo_version_t,
91     nvlist_t *, nvlist_t **);
92 
93 #define	TOPO_METH_DISK_TEMP		"disk_temp_reading"
94 #define	TOPO_METH_DISK_TEMP_DESC	"Disk Temperature Reading"
95 #define	TOPO_METH_DISK_TEMP_VERSION	0
96 
97 static const topo_method_t disk_fac_methods[] = {
98 	{ TOPO_METH_DISK_TEMP, TOPO_METH_DISK_TEMP_DESC,
99 	    TOPO_METH_DISK_TEMP_VERSION, TOPO_STABILITY_INTERNAL,
100 	    disk_temp_reading },
101 	{ NULL }
102 };
103 
104 static const topo_pgroup_info_t io_pgroup = {
105 	TOPO_PGROUP_IO,
106 	TOPO_STABILITY_PRIVATE,
107 	TOPO_STABILITY_PRIVATE,
108 	1
109 };
110 
111 static const topo_pgroup_info_t disk_auth_pgroup = {
112 	FM_FMRI_AUTHORITY,
113 	TOPO_STABILITY_PRIVATE,
114 	TOPO_STABILITY_PRIVATE,
115 	1
116 };
117 
118 static const topo_pgroup_info_t storage_pgroup = {
119 	TOPO_PGROUP_STORAGE,
120 	TOPO_STABILITY_PRIVATE,
121 	TOPO_STABILITY_PRIVATE,
122 	1
123 };
124 
125 /*
126  * Set the properties of the disk node, from dev_di_node_t data.
127  * Properties include:
128  *	group: protocol	 properties: resource, asru, label, fru
129  *	group: authority properties: product-id, chasis-id, server-id
130  *	group: io	 properties: devfs-path, devid
131  *	group: storage	 properties:
132  *		- logical-disk, disk-model, disk-manufacturer, serial-number
133  *		- firmware-revision, capacity-in-bytes
134  *
135  * NOTE: the io and storage groups won't be present if the dnode passed in is
136  * NULL. This happens when a disk is found through ses, but is not enumerated
137  * in the devinfo tree.
138  */
139 static int
140 disk_set_props(topo_mod_t *mod, tnode_t *parent,
141     tnode_t *dtn, dev_di_node_t *dnode)
142 {
143 	nvlist_t	*asru = NULL, *drive_attrs;
144 	char		*label = NULL;
145 	nvlist_t	*fmri = NULL;
146 	dm_descriptor_t drive_descr = NULL;
147 	uint32_t	rpm;
148 	int		err;
149 
150 	/* pull the label property down from our parent 'bay' node */
151 	if (topo_node_label(parent, &label, &err) != 0) {
152 		if (err != ETOPO_PROP_NOENT) {
153 			topo_mod_dprintf(mod, "disk_set_props: "
154 			    "label error %s\n", topo_strerror(err));
155 			goto error;
156 		}
157 	} else if (topo_prop_set_string(dtn, TOPO_PGROUP_PROTOCOL,
158 	    TOPO_PROP_LABEL, TOPO_PROP_MUTABLE, label, &err) != 0) {
159 		topo_mod_dprintf(mod, "disk_set_props: "
160 		    "label_set error %s\n", topo_strerror(err));
161 		goto error;
162 	}
163 
164 	/* get the resource fmri, and use it as the fru */
165 	if (topo_node_resource(dtn, &fmri, &err) != 0) {
166 		topo_mod_dprintf(mod, "disk_set_props: "
167 		    "resource error: %s\n", topo_strerror(err));
168 		goto error;
169 	}
170 	if (topo_node_fru_set(dtn, fmri, 0, &err) != 0) {
171 		topo_mod_dprintf(mod, "disk_set_props: "
172 		    "fru_set error: %s\n", topo_strerror(err));
173 		goto error;
174 	}
175 
176 	/* create/set the authority group */
177 	if ((topo_pgroup_create(dtn, &disk_auth_pgroup, &err) != 0) &&
178 	    (err != ETOPO_PROP_DEFD)) {
179 		topo_mod_dprintf(mod, "disk_set_props: "
180 		    "create disk_auth error %s\n", topo_strerror(err));
181 		goto error;
182 	}
183 
184 	/* create the storage group */
185 	if (topo_pgroup_create(dtn, &storage_pgroup, &err) != 0) {
186 		topo_mod_dprintf(mod, "disk_set_props: "
187 		    "create storage error %s\n", topo_strerror(err));
188 		goto error;
189 	}
190 
191 	/* no dnode was found for this disk - skip the io and storage groups */
192 	if (dnode == NULL) {
193 		err = 0;
194 		goto out;
195 	}
196 
197 	/* form and set the asru */
198 	if ((asru = topo_mod_devfmri(mod, FM_DEV_SCHEME_VERSION,
199 	    dnode->ddn_dpath, dnode->ddn_devid)) == NULL) {
200 		err = ETOPO_FMRI_UNKNOWN;
201 		topo_mod_dprintf(mod, "disk_set_props: "
202 		    "asru error %s\n", topo_strerror(err));
203 		goto error;
204 	}
205 	if (topo_node_asru_set(dtn, asru, 0, &err) != 0) {
206 		topo_mod_dprintf(mod, "disk_set_props: "
207 		    "asru_set error %s\n", topo_strerror(err));
208 		goto error;
209 	}
210 
211 	/* create/set the devfs-path and devid in the io group */
212 	if (topo_pgroup_create(dtn, &io_pgroup, &err) != 0) {
213 		topo_mod_dprintf(mod, "disk_set_props: "
214 		    "create io error %s\n", topo_strerror(err));
215 		goto error;
216 	}
217 
218 	if (topo_prop_set_string(dtn, TOPO_PGROUP_IO, TOPO_IO_DEV_PATH,
219 	    TOPO_PROP_IMMUTABLE, dnode->ddn_dpath, &err) != 0) {
220 		topo_mod_dprintf(mod, "disk_set_props: "
221 		    "set dev error %s\n", topo_strerror(err));
222 		goto error;
223 	}
224 
225 	if (dnode->ddn_devid && topo_prop_set_string(dtn, TOPO_PGROUP_IO,
226 	    TOPO_IO_DEVID, TOPO_PROP_IMMUTABLE, dnode->ddn_devid, &err) != 0) {
227 		topo_mod_dprintf(mod, "disk_set_props: "
228 		    "set devid error %s\n", topo_strerror(err));
229 		goto error;
230 	}
231 
232 	if (dnode->ddn_ppath_count != 0 &&
233 	    topo_prop_set_string_array(dtn, TOPO_PGROUP_IO, TOPO_IO_PHYS_PATH,
234 	    TOPO_PROP_IMMUTABLE, (const char **)dnode->ddn_ppath,
235 	    dnode->ddn_ppath_count, &err) != 0) {
236 		topo_mod_dprintf(mod, "disk_set_props: "
237 		    "set phys-path error %s\n", topo_strerror(err));
238 		goto error;
239 	}
240 
241 	/* set the storage group public /dev name */
242 	if (dnode->ddn_lpath != NULL &&
243 	    topo_prop_set_string(dtn, TOPO_PGROUP_STORAGE,
244 	    TOPO_STORAGE_LOGICAL_DISK_NAME, TOPO_PROP_IMMUTABLE,
245 	    dnode->ddn_lpath, &err) != 0) {
246 		topo_mod_dprintf(mod, "disk_set_props: "
247 		    "set disk_name error %s\n", topo_strerror(err));
248 		goto error;
249 	}
250 
251 	/* populate other misc storage group properties */
252 	if (dnode->ddn_mfg && (topo_prop_set_string(dtn, TOPO_PGROUP_STORAGE,
253 	    TOPO_STORAGE_MANUFACTURER, TOPO_PROP_IMMUTABLE,
254 	    dnode->ddn_mfg, &err) != 0)) {
255 		topo_mod_dprintf(mod, "disk_set_props: "
256 		    "set mfg error %s\n", topo_strerror(err));
257 		goto error;
258 	}
259 	if (dnode->ddn_model && (topo_prop_set_string(dtn, TOPO_PGROUP_STORAGE,
260 	    TOPO_STORAGE_MODEL, TOPO_PROP_IMMUTABLE,
261 	    dnode->ddn_model, &err) != 0)) {
262 		topo_mod_dprintf(mod, "disk_set_props: "
263 		    "set model error %s\n", topo_strerror(err));
264 		goto error;
265 	}
266 	if (dnode->ddn_serial && (topo_prop_set_string(dtn, TOPO_PGROUP_STORAGE,
267 	    TOPO_STORAGE_SERIAL_NUM, TOPO_PROP_IMMUTABLE,
268 	    dnode->ddn_serial, &err) != 0)) {
269 		topo_mod_dprintf(mod, "disk_set_props: "
270 		    "set serial error %s\n", topo_strerror(err));
271 		goto error;
272 	}
273 	if (dnode->ddn_firm && (topo_prop_set_string(dtn, TOPO_PGROUP_STORAGE,
274 	    TOPO_STORAGE_FIRMWARE_REV, TOPO_PROP_IMMUTABLE,
275 	    dnode->ddn_firm, &err) != 0)) {
276 		topo_mod_dprintf(mod, "disk_set_props: "
277 		    "set firm error %s\n", topo_strerror(err));
278 		goto error;
279 	}
280 	if (dnode->ddn_cap && (topo_prop_set_string(dtn, TOPO_PGROUP_STORAGE,
281 	    TOPO_STORAGE_CAPACITY, TOPO_PROP_IMMUTABLE,
282 	    dnode->ddn_cap, &err) != 0)) {
283 		topo_mod_dprintf(mod, "disk_set_props: "
284 		    "set cap error %s\n", topo_strerror(err));
285 		goto error;
286 	}
287 
288 	if (dnode->ddn_devid == NULL ||
289 	    (drive_descr = dm_get_descriptor_by_name(DM_DRIVE,
290 	    dnode->ddn_devid, &err)) == NULL ||
291 	    (drive_attrs = dm_get_attributes(drive_descr, &err)) == NULL)
292 		goto out;
293 
294 	if (nvlist_lookup_boolean(drive_attrs, DM_SOLIDSTATE) == 0 ||
295 	    nvlist_lookup_uint32(drive_attrs, DM_RPM, &rpm) != 0)
296 		goto out;
297 
298 	if (topo_prop_set_uint32(dtn, TOPO_PGROUP_STORAGE, TOPO_STORAGE_RPM,
299 	    TOPO_PROP_IMMUTABLE, rpm, &err) != 0) {
300 		topo_mod_dprintf(mod, "disk_set_props: "
301 		    "set rpm error %s\n", topo_strerror(err));
302 		dm_free_descriptor(drive_descr);
303 		goto error;
304 	}
305 	err = 0;
306 
307 out:
308 	if (drive_descr != NULL)
309 		dm_free_descriptor(drive_descr);
310 	nvlist_free(fmri);
311 	if (label)
312 		topo_mod_strfree(mod, label);
313 	nvlist_free(asru);
314 	return (err);
315 
316 error:	err = topo_mod_seterrno(mod, err);
317 	goto out;
318 }
319 
320 /*
321  * Trim leading and trailing whitespace from the string.
322  */
323 static char *
324 disk_trim_whitespace(topo_mod_t *mod, const char *begin)
325 {
326 	const char *end;
327 	char *buf;
328 	size_t count;
329 
330 	if (begin == NULL)
331 		return (NULL);
332 
333 	end = begin + strlen(begin);
334 
335 	while (begin < end && isspace(*begin))
336 		begin++;
337 	while (begin < end && isspace(*(end - 1)))
338 		end--;
339 
340 	count = end - begin;
341 	if ((buf = topo_mod_alloc(mod, count + 1)) == NULL)
342 		return (NULL);
343 
344 	(void) strlcpy(buf, begin, count + 1);
345 
346 	return (buf);
347 }
348 
349 /*ARGSUSED*/
350 static int
351 disk_temp_reading(topo_mod_t *mod, tnode_t *node, topo_version_t vers,
352     nvlist_t *in, nvlist_t **out)
353 {
354 	char *devid;
355 	uint32_t temp;
356 	dm_descriptor_t drive_descr = NULL;
357 	nvlist_t *drive_stats, *pargs, *nvl;
358 	int err;
359 
360 	if (vers > TOPO_METH_DISK_TEMP_VERSION)
361 		return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW));
362 
363 	if (nvlist_lookup_nvlist(in, TOPO_PROP_ARGS, &pargs) != 0 ||
364 	    nvlist_lookup_string(pargs, TOPO_IO_DEVID, &devid) != 0) {
365 		topo_mod_dprintf(mod, "Failed to lookup %s arg",
366 		    TOPO_IO_DEVID);
367 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
368 	}
369 
370 	if ((drive_descr = dm_get_descriptor_by_name(DM_DRIVE, devid,
371 	    &err)) == NULL) {
372 		topo_mod_dprintf(mod, "failed to get drive decriptor for %s",
373 		    devid);
374 		return (topo_mod_seterrno(mod, EMOD_UNKNOWN));
375 	}
376 
377 	if ((drive_stats = dm_get_stats(drive_descr, DM_DRV_STAT_TEMPERATURE,
378 	    &err)) == NULL ||
379 	    nvlist_lookup_uint32(drive_stats, DM_TEMPERATURE, &temp) != 0) {
380 		topo_mod_dprintf(mod, "failed to read disk temp for %s",
381 		    devid);
382 		dm_free_descriptor(drive_descr);
383 		return (topo_mod_seterrno(mod, EMOD_UNKNOWN));
384 	}
385 	dm_free_descriptor(drive_descr);
386 
387 	if (topo_mod_nvalloc(mod, &nvl, NV_UNIQUE_NAME) != 0 ||
388 	    nvlist_add_string(nvl, TOPO_PROP_VAL_NAME,
389 	    TOPO_SENSOR_READING) != 0 ||
390 	    nvlist_add_uint32(nvl, TOPO_PROP_VAL_TYPE, TOPO_TYPE_DOUBLE) !=
391 	    0 || nvlist_add_double(nvl, TOPO_PROP_VAL_VAL, (double)temp) != 0) {
392 		topo_mod_dprintf(mod, "Failed to allocate 'out' nvlist\n");
393 		nvlist_free(nvl);
394 		return (topo_mod_seterrno(mod, EMOD_NOMEM));
395 	}
396 	*out = nvl;
397 
398 	return (0);
399 }
400 
401 static int
402 disk_add_temp_sensor(topo_mod_t *mod, tnode_t *pnode, const char *devid)
403 {
404 	tnode_t *fnode;
405 	topo_pgroup_info_t pgi;
406 	nvlist_t *arg_nvl = NULL;
407 	int err;
408 
409 	if ((fnode = topo_node_facbind(mod, pnode, "temp",
410 	    TOPO_FAC_TYPE_SENSOR)) == NULL) {
411 		topo_mod_dprintf(mod, "failed to bind facility node");
412 		/* errno set */
413 		return (-1);
414 	}
415 
416 	/*
417 	 * Set props:
418 	 * - facility/sensor-class
419 	 * - facility/sensor-type
420 	 * - facility/units
421 	 */
422 	pgi.tpi_name = TOPO_PGROUP_FACILITY;
423 	pgi.tpi_namestab = TOPO_STABILITY_PRIVATE;
424 	pgi.tpi_datastab = TOPO_STABILITY_PRIVATE;
425 	pgi.tpi_version = 1;
426 	if (topo_pgroup_create(fnode, &pgi, &err) != 0) {
427 		if (err != ETOPO_PROP_DEFD) {
428 			topo_mod_dprintf(mod,  "pgroups create failure (%s)\n",
429 			    topo_strerror(err));
430 			/* errno set */
431 			goto err;
432 		}
433 	}
434 	if (topo_prop_set_string(fnode, TOPO_PGROUP_FACILITY,
435 	    TOPO_SENSOR_CLASS, TOPO_PROP_IMMUTABLE,
436 	    TOPO_SENSOR_CLASS_THRESHOLD, &err) != 0 ||
437 	    topo_prop_set_uint32(fnode, TOPO_PGROUP_FACILITY,
438 	    TOPO_FACILITY_TYPE, TOPO_PROP_IMMUTABLE, TOPO_SENSOR_TYPE_TEMP,
439 	    &err) != 0 ||
440 	    topo_prop_set_uint32(fnode, TOPO_PGROUP_FACILITY,
441 	    TOPO_SENSOR_UNITS, TOPO_PROP_IMMUTABLE,
442 	    TOPO_SENSOR_UNITS_DEGREES_C, &err) != 0) {
443 		topo_mod_dprintf(mod, "Failed to set props on facnode (%s)",
444 		    topo_strerror(err));
445 		/* errno set */
446 		goto err;
447 	}
448 
449 	/*
450 	 * Register a property method for facility/reading
451 	 */
452 	if (topo_method_register(mod, fnode, disk_fac_methods) < 0) {
453 		topo_mod_dprintf(mod, "failed to register facility methods");
454 		goto err;
455 	}
456 	if (topo_mod_nvalloc(mod, &arg_nvl, NV_UNIQUE_NAME) < 0 ||
457 	    nvlist_add_string(arg_nvl, TOPO_IO_DEVID, devid) != 0) {
458 		topo_mod_dprintf(mod, "Failed build arg nvlist\n");
459 		(void) topo_mod_seterrno(mod, EMOD_NOMEM);
460 		goto err;
461 	}
462 	if (topo_prop_method_register(fnode, TOPO_PGROUP_FACILITY,
463 	    TOPO_SENSOR_READING, TOPO_TYPE_DOUBLE, "disk_temp_reading",
464 	    arg_nvl, &err) != 0) {
465 		topo_mod_dprintf(mod, "Failed to register %s propmeth "
466 		    "on fac node %s (%s)\n", TOPO_SENSOR_READING,
467 		    topo_node_name(fnode), topo_strerror(err));
468 		/* errno set */
469 		goto err;
470 	}
471 	nvlist_free(arg_nvl);
472 	return (0);
473 err:
474 	topo_node_unbind(fnode);
475 	nvlist_free(arg_nvl);
476 	return (-1);
477 }
478 
479 /* create the disk topo node */
480 static int
481 disk_tnode_create(topo_mod_t *mod, tnode_t *parent,
482     dev_di_node_t *dnode, const char *name, topo_instance_t i, tnode_t **rval)
483 {
484 	int		len;
485 	nvlist_t	*fmri;
486 	tnode_t		*dtn;
487 	char		*part = NULL;
488 	nvlist_t	*auth;
489 	char		*mfg, *model, *firm, *serial;
490 
491 	*rval = NULL;
492 	if (dnode != NULL) {
493 		mfg = topo_mod_clean_str(mod, dnode->ddn_mfg);
494 		model = topo_mod_clean_str(mod, dnode->ddn_model);
495 		firm = topo_mod_clean_str(mod, dnode->ddn_firm);
496 		serial = topo_mod_clean_str(mod, dnode->ddn_serial);
497 	} else {
498 		mfg = model = firm = serial = NULL;
499 	}
500 
501 	/* form 'part=' of fmri as "<mfg>-<model>" */
502 	if (mfg != NULL && model != NULL) {
503 		len = strlen(mfg) + 1 + strlen(model) + 1;
504 		if ((part = topo_mod_alloc(mod, len)) != NULL)
505 			(void) snprintf(part, len, "%s-%s",
506 			    mfg, model);
507 	}
508 
509 	auth = topo_mod_auth(mod, parent);
510 	fmri = topo_mod_hcfmri(mod, parent, FM_HC_SCHEME_VERSION, name, i, NULL,
511 	    auth, part ? part : model, firm, serial);
512 	nvlist_free(auth);
513 
514 	topo_mod_strfree(mod, part);
515 	topo_mod_strfree(mod, mfg);
516 	topo_mod_strfree(mod, model);
517 	topo_mod_strfree(mod, firm);
518 	topo_mod_strfree(mod, serial);
519 
520 	if (fmri == NULL) {
521 		topo_mod_dprintf(mod, "disk_tnode_create: "
522 		    "hcfmri (%s%d/%s%d) error %s\n",
523 		    topo_node_name(parent), topo_node_instance(parent),
524 		    name, i, topo_strerror(topo_mod_errno(mod)));
525 		return (-1);
526 	}
527 
528 	if ((dtn = topo_node_bind(mod, parent, name, i, fmri)) == NULL) {
529 		if (topo_mod_errno(mod) == EMOD_NODE_BOUND) {
530 			/*
531 			 * if disk 0 is already there then we're done
532 			 */
533 			nvlist_free(fmri);
534 			return (0);
535 		}
536 		topo_mod_dprintf(mod, "disk_tnode_create: "
537 		    "bind (%s%d/%s%d) error %s\n",
538 		    topo_node_name(parent), topo_node_instance(parent),
539 		    name, i, topo_strerror(topo_mod_errno(mod)));
540 		nvlist_free(fmri);
541 		return (-1);
542 	}
543 	nvlist_free(fmri);
544 
545 	/* add the properties of the disk */
546 	if (disk_set_props(mod, parent, dtn, dnode) != 0) {
547 		topo_mod_dprintf(mod, "disk_tnode_create: "
548 		    "disk_set_props (%s%d/%s%d) error %s\n",
549 		    topo_node_name(parent), topo_node_instance(parent),
550 		    name, i, topo_strerror(topo_mod_errno(mod)));
551 		topo_node_unbind(dtn);
552 		return (-1);
553 	}
554 
555 	if (dnode != NULL && dnode->ddn_devid != NULL &&
556 	    disk_add_temp_sensor(mod, dtn, dnode->ddn_devid) != 0) {
557 		topo_mod_dprintf(mod, "disk_tnode_create: failed to create "
558 		    "temperature sensor node on bay=%d/disk=0",
559 		    topo_node_instance(parent));
560 	}
561 	*rval = dtn;
562 	return (0);
563 }
564 
565 static int
566 disk_declare(topo_mod_t *mod, tnode_t *parent, dev_di_node_t *dnode,
567     tnode_t **childp)
568 {
569 	tnode_t		*dtn = NULL;
570 	int		rval;
571 
572 	rval = disk_tnode_create(mod, parent, dnode, DISK, 0, &dtn);
573 	if (dtn == NULL) {
574 		if (rval == 0)
575 			return (0);
576 		topo_mod_dprintf(mod, "disk_declare: "
577 		    "disk_tnode_create error %s\n",
578 		    topo_strerror(topo_mod_errno(mod)));
579 		return (-1);
580 	}
581 
582 	/* register disk_methods against the disk topo node */
583 	if (topo_method_register(mod, dtn, disk_methods) != 0) {
584 		topo_mod_dprintf(mod, "disk_declare: "
585 		    "topo_method_register error %s\n",
586 		    topo_strerror(topo_mod_errno(mod)));
587 		topo_node_unbind(dtn);
588 		return (-1);
589 	}
590 	if (childp != NULL)
591 		*childp = dtn;
592 	return (0);
593 }
594 
595 int
596 disk_declare_path(topo_mod_t *mod, tnode_t *parent, topo_list_t *listp,
597     const char *path)
598 {
599 	dev_di_node_t		*dnode;
600 	int i;
601 
602 	/*
603 	 * Check for match using physical phci (ddn_ppath). Use
604 	 * di_devfs_path_match so generic.vs.non-generic names match.
605 	 */
606 	for (dnode = topo_list_next(listp); dnode != NULL;
607 	    dnode = topo_list_next(dnode)) {
608 		if (dnode->ddn_ppath == NULL)
609 			continue;
610 
611 		for (i = 0; i < dnode->ddn_ppath_count; i++) {
612 			if (di_devfs_path_match(dnode->ddn_ppath[0], path))
613 				return (disk_declare(mod, parent, dnode, NULL));
614 		}
615 	}
616 
617 	topo_mod_dprintf(mod, "disk_declare_path: "
618 	    "failed to find disk matching path %s", path);
619 	return (0);
620 }
621 
622 int
623 disk_declare_addr(topo_mod_t *mod, tnode_t *parent, topo_list_t *listp,
624     const char *addr, tnode_t **childp)
625 {
626 	dev_di_node_t *dnode;
627 	int i;
628 
629 	/* Check for match using addr. */
630 	for (dnode = topo_list_next(listp); dnode != NULL;
631 	    dnode = topo_list_next(dnode)) {
632 		if (dnode->ddn_target_port == NULL)
633 			continue;
634 
635 		for (i = 0; i < dnode->ddn_ppath_count; i++) {
636 			if ((dnode->ddn_target_port[i] != NULL) &&
637 			    (strncmp(dnode->ddn_target_port[i], addr,
638 			    strcspn(dnode->ddn_target_port[i], ":"))) == 0) {
639 				topo_mod_dprintf(mod, "disk_declare_addr: "
640 				    "found disk matching addr %s", addr);
641 				return (disk_declare(mod, parent, dnode,
642 				    childp));
643 			}
644 		}
645 	}
646 
647 	topo_mod_dprintf(mod, "disk_declare_addr: "
648 	    "failed to find disk matching addr %s", addr);
649 
650 	return (1);
651 }
652 
653 /*
654  * Try to find a disk based on the bridge-port property. This is most often used
655  * for SATA devices which are attached to a SAS controller and are therefore
656  * behind a SATL bridge port. SES only knows of devices based on this SAS WWN,
657  * not based on any SATA GUIDs.
658  */
659 int
660 disk_declare_bridge(topo_mod_t *mod, tnode_t *parent, topo_list_t *listp,
661     const char *addr, tnode_t **childp)
662 {
663 	dev_di_node_t *dnode;
664 	int i;
665 
666 	/* Check for match using addr. */
667 	for (dnode = topo_list_next(listp); dnode != NULL;
668 	    dnode = topo_list_next(dnode)) {
669 		if (dnode->ddn_bridge_port == NULL)
670 			continue;
671 
672 		for (i = 0; i < dnode->ddn_ppath_count; i++) {
673 			if ((dnode->ddn_bridge_port[i] != NULL) &&
674 			    (strncmp(dnode->ddn_bridge_port[i], addr,
675 			    strcspn(dnode->ddn_bridge_port[i], ":"))) == 0) {
676 				topo_mod_dprintf(mod, "disk_declare_bridge: "
677 				    "found disk matching bridge %s", addr);
678 				return (disk_declare(mod, parent, dnode,
679 				    childp));
680 			}
681 		}
682 	}
683 
684 	topo_mod_dprintf(mod, "disk_declare_bridge: "
685 	    "failed to find disk matching bridge %s", addr);
686 
687 	return (1);
688 }
689 
690 /*
691  * Used to declare a disk that has been discovered through other means (usually
692  * ses), that is not enumerated in the devinfo tree.
693  */
694 int
695 disk_declare_non_enumerated(topo_mod_t *mod, tnode_t *parent, tnode_t **childp)
696 {
697 	return (disk_declare(mod, parent, NULL, childp));
698 }
699 
700 /* di_devlink callback for dev_di_node_add */
701 static int
702 disk_devlink_callback(di_devlink_t dl, void *arg)
703 {
704 	disk_cbdata_t	*cbp = (disk_cbdata_t *)arg;
705 	topo_mod_t	*mod = cbp->dcb_mod;
706 	dev_di_node_t	*dnode = cbp->dcb_dnode;
707 	const char	*devpath;
708 	char		*ctds, *slice;
709 
710 	devpath = di_devlink_path(dl);
711 	if ((dnode == NULL) || (devpath == NULL))
712 		return (DI_WALK_TERMINATE);
713 
714 	/* trim the slice off the public name */
715 	if (((ctds = strrchr(devpath, '/')) != NULL) &&
716 	    ((slice = strchr(ctds, 's')) != NULL))
717 		*slice = '\0';
718 
719 	/* Establish the public /dev name (no slice) */
720 	dnode->ddn_lpath = topo_mod_strdup(mod, ctds ? ctds + 1 : devpath);
721 
722 	if (ctds && slice)
723 		*slice = 's';
724 	return (DI_WALK_TERMINATE);
725 }
726 
727 static void
728 dev_di_node_free(topo_mod_t *mod, dev_di_node_t *dnode)
729 {
730 	int i;
731 
732 	/* free the stuff we point to */
733 	if (dnode->ddn_devid)
734 		topo_mod_strfree(mod, dnode->ddn_devid);
735 	for (i = 0; i < dnode->ddn_ppath_count; i++) {
736 		/* topo_mod_strfree does NULL checking. */
737 		topo_mod_strfree(mod, dnode->ddn_ppath[i]);
738 		topo_mod_strfree(mod, dnode->ddn_target_port[i]);
739 		topo_mod_strfree(mod, dnode->ddn_attached_port[i]);
740 		topo_mod_strfree(mod, dnode->ddn_bridge_port[i]);
741 	}
742 	topo_mod_free(mod, dnode->ddn_ppath,
743 	    dnode->ddn_ppath_count * sizeof (char *));
744 	topo_mod_free(mod, dnode->ddn_target_port,
745 	    dnode->ddn_ppath_count * sizeof (char *));
746 	topo_mod_free(mod, dnode->ddn_attached_port,
747 	    dnode->ddn_ppath_count * sizeof (char *));
748 	topo_mod_free(mod, dnode->ddn_bridge_port,
749 	    dnode->ddn_ppath_count * sizeof (char *));
750 	topo_mod_strfree(mod, dnode->ddn_dpath);
751 	topo_mod_strfree(mod, dnode->ddn_lpath);
752 
753 	topo_mod_strfree(mod, dnode->ddn_mfg);
754 	topo_mod_strfree(mod, dnode->ddn_model);
755 	topo_mod_strfree(mod, dnode->ddn_serial);
756 	topo_mod_strfree(mod, dnode->ddn_firm);
757 	topo_mod_strfree(mod, dnode->ddn_cap);
758 
759 	/* free self */
760 	topo_mod_free(mod, dnode, sizeof (dev_di_node_t));
761 }
762 
763 static int
764 dev_di_node_add(di_node_t node, char *devid, disk_cbdata_t *cbp)
765 {
766 	topo_mod_t	*mod = cbp->dcb_mod;
767 	dev_di_node_t	*dnode;
768 	di_path_t	pnode;
769 	char		*path;
770 	int		mlen;
771 	char		*minorpath;
772 	char		*extn = ":a";
773 	char		*s;
774 	int64_t		*nblocksp;
775 	uint64_t	nblocks;
776 	int		*blksizep;
777 	uint_t		blksize;
778 	char		lentry[MAXPATHLEN];
779 	int		pathcount;
780 	int		*inq_dtype, itype;
781 	int		i;
782 
783 	if (devid) {
784 		/*
785 		 * Check for list duplicate using devid search.
786 		 * Note if there is no devid, then we can end up with duplicates
787 		 * in the list, but this doesn't do any harm.
788 		 */
789 		for (dnode = topo_list_next(cbp->dcb_list);
790 		    dnode != NULL; dnode = topo_list_next(dnode)) {
791 			if (dnode->ddn_devid &&
792 			    devid_str_compare(dnode->ddn_devid, devid) == 0) {
793 				topo_mod_dprintf(mod, "dev_di_node_add: "
794 				    "already there %s\n", devid);
795 				return (0);
796 			}
797 		}
798 	}
799 
800 	if ((dnode = topo_mod_zalloc(mod, sizeof (dev_di_node_t))) == NULL)
801 		return (-1);
802 
803 	if (devid) {
804 		/* Establish the devid. */
805 		dnode->ddn_devid = topo_mod_strdup(mod, devid);
806 		if (dnode->ddn_devid == NULL)
807 			goto error;
808 	}
809 
810 	/* Establish the devinfo dpath */
811 	if ((path = di_devfs_path(node)) == NULL) {
812 		(void) topo_mod_seterrno(mod, errno);
813 		goto error;
814 	}
815 
816 	dnode->ddn_dpath = topo_mod_strdup(mod, path);
817 	di_devfs_path_free(path);
818 	if (dnode->ddn_dpath == NULL)
819 		goto error;
820 
821 	/*
822 	 * Establish the physical ppath and target ports. If the device is
823 	 * non-mpxio then dpath and ppath are the same, and the target port is a
824 	 * property of the device node.
825 	 *
826 	 * If dpath is a client node under scsi_vhci, then iterate over all
827 	 * paths and get their physical paths and target port properrties.
828 	 * di_path_client_next_path call below will
829 	 * return non-NULL, and ppath is set to the physical path to the first
830 	 * pathinfo node.
831 	 *
832 	 * NOTE: It is possible to get a generic.vs.non-generic path
833 	 * for di_devfs_path.vs.di_path_devfs_path like:
834 	 *    xml: /pci@7b,0/pci1022,7458@11/pci1000,3060@2/sd@2,0
835 	 *  pnode: /pci@7b,0/pci1022,7458@11/pci1000,3060@2/disk@2,0
836 	 * To resolve this issue disk_declare_path() needs to use the
837 	 * special di_devfs_path_match() interface.
838 	 */
839 	pathcount = 0;
840 	pnode = NULL;
841 	while ((pnode = di_path_client_next_path(node, pnode)) != NULL) {
842 		pathcount++;
843 	}
844 
845 	if (pathcount == 0) {
846 		if ((dnode->ddn_ppath =
847 		    topo_mod_zalloc(mod, sizeof (char *))) == NULL)
848 			goto error;
849 
850 		dnode->ddn_ppath_count = 1;
851 		if ((dnode->ddn_ppath[0] = topo_mod_strdup(mod,
852 		    dnode->ddn_dpath)) == NULL)
853 			goto error;
854 
855 		if ((dnode->ddn_target_port = topo_mod_zalloc(mod,
856 		    sizeof (char *))) == NULL)
857 			goto error;
858 
859 		if ((dnode->ddn_attached_port = topo_mod_zalloc(mod,
860 		    sizeof (char *))) == NULL)
861 			goto error;
862 
863 		if ((dnode->ddn_bridge_port = topo_mod_zalloc(mod,
864 		    sizeof (char *))) == NULL)
865 			goto error;
866 
867 		/* There should be only one target port for a devinfo node. */
868 		if ((di_prop_lookup_strings(DDI_DEV_T_ANY, node,
869 		    SCSI_ADDR_PROP_TARGET_PORT, &s)) == 1) {
870 			if ((dnode->ddn_target_port[0] =
871 			    topo_mod_strdup(mod,
872 			    scsi_wwnstr_skip_ua_prefix(s))) ==
873 			    NULL)
874 				goto error;
875 		}
876 
877 		if ((di_prop_lookup_strings(DDI_DEV_T_ANY, node,
878 		    SCSI_ADDR_PROP_ATTACHED_PORT, &s)) == 1) {
879 			/* There should be one attached port if any. */
880 			if ((dnode->ddn_attached_port[0] =
881 			    topo_mod_strdup(mod,
882 			    scsi_wwnstr_skip_ua_prefix(s))) ==
883 			    NULL)
884 				goto error;
885 		}
886 
887 		if ((di_prop_lookup_strings(DDI_DEV_T_ANY, node,
888 		    SCSI_ADDR_PROP_BRIDGE_PORT, &s)) == 1) {
889 			/* There should be one bridge port if any. */
890 			if ((dnode->ddn_bridge_port[0] =
891 			    topo_mod_strdup(mod,
892 			    scsi_wwnstr_skip_ua_prefix(s))) ==
893 			    NULL)
894 				goto error;
895 		}
896 
897 	} else {
898 		/* processing a scsi_vhci device. */
899 		if ((dnode->ddn_ppath = topo_mod_zalloc(mod,
900 		    pathcount * sizeof (char *))) == NULL)
901 			goto error;
902 
903 		dnode->ddn_ppath_count = pathcount;
904 
905 		if ((dnode->ddn_target_port = topo_mod_zalloc(mod,
906 		    pathcount * sizeof (char *))) == NULL)
907 			goto error;
908 
909 		if ((dnode->ddn_attached_port = topo_mod_zalloc(mod,
910 		    pathcount * sizeof (char *))) == NULL)
911 			goto error;
912 
913 		if ((dnode->ddn_bridge_port = topo_mod_zalloc(mod,
914 		    pathcount * sizeof (char *))) == NULL)
915 			goto error;
916 
917 		pnode = NULL;
918 		pathcount = 0;
919 		while ((pnode = di_path_client_next_path(node,
920 		    pnode)) != NULL) {
921 			if ((path = di_path_devfs_path(pnode)) == NULL) {
922 				(void) topo_mod_seterrno(mod, errno);
923 				goto error;
924 			}
925 
926 			dnode->ddn_ppath[pathcount] =
927 			    topo_mod_strdup(mod, path);
928 			di_devfs_path_free(path);
929 			if (dnode->ddn_ppath[pathcount] == NULL)
930 				goto error;
931 
932 			if ((di_path_prop_lookup_strings(pnode,
933 			    SCSI_ADDR_PROP_TARGET_PORT, &s)) == 1) {
934 				if ((dnode->ddn_target_port[pathcount] =
935 				    topo_mod_strdup(mod,
936 				    scsi_wwnstr_skip_ua_prefix(s))) ==
937 				    NULL)
938 					goto error;
939 			}
940 
941 			if ((di_path_prop_lookup_strings(pnode,
942 			    SCSI_ADDR_PROP_ATTACHED_PORT, &s)) == 1) {
943 				if ((dnode->ddn_attached_port[pathcount] =
944 				    topo_mod_strdup(mod,
945 				    scsi_wwnstr_skip_ua_prefix(s))) ==
946 				    NULL)
947 					goto error;
948 			}
949 
950 			if ((di_path_prop_lookup_strings(pnode,
951 			    SCSI_ADDR_PROP_BRIDGE_PORT, &s)) == 1) {
952 				if ((dnode->ddn_bridge_port[pathcount] =
953 				    topo_mod_strdup(mod,
954 				    scsi_wwnstr_skip_ua_prefix(s))) ==
955 				    NULL)
956 					goto error;
957 			}
958 
959 			pathcount++;
960 		}
961 	}
962 
963 	/*
964 	 * Find the public /dev name for a disk by adding a minor name and using
965 	 * di_devlink interface for reverse translation (use devinfo path).
966 	 */
967 	if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "inquiry-device-type",
968 	    &inq_dtype) > 0) {
969 		dnode->ddn_dtype = *inq_dtype;
970 		itype = (*inq_dtype) & DTYPE_MASK;
971 		if (itype == DTYPE_DIRECT) {
972 			mlen = strlen(dnode->ddn_dpath) + strlen(extn) + 1;
973 			if ((minorpath = topo_mod_alloc(mod, mlen)) == NULL)
974 				goto error;
975 			(void) snprintf(minorpath, mlen, "%s%s",
976 			    dnode->ddn_dpath, extn);
977 			cbp->dcb_dnode = dnode;
978 			(void) di_devlink_walk(cbp->dcb_devhdl, "^dsk/",
979 			    minorpath, DI_PRIMARY_LINK, cbp,
980 			    disk_devlink_callback);
981 			topo_mod_free(mod, minorpath, mlen);
982 			if (dnode->ddn_lpath == NULL) {
983 				topo_mod_dprintf(mod, "dev_di_node_add: "
984 				    "failed to determine logical path");
985 			}
986 		}
987 	} else {
988 		dnode->ddn_dtype = DTYPE_UNKNOWN;
989 	}
990 
991 	/* cache various bits of optional information about the device. */
992 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
993 	    INQUIRY_VENDOR_ID, &s) > 0) {
994 		if ((dnode->ddn_mfg = disk_trim_whitespace(mod, s)) == NULL)
995 			goto error;
996 	}
997 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
998 	    INQUIRY_PRODUCT_ID, &s) > 0) {
999 		if ((dnode->ddn_model = disk_trim_whitespace(mod, s)) == NULL)
1000 			goto error;
1001 	}
1002 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
1003 	    INQUIRY_REVISION_ID, &s) > 0) {
1004 		if ((dnode->ddn_firm = disk_trim_whitespace(mod, s)) == NULL)
1005 			goto error;
1006 	}
1007 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
1008 	    INQUIRY_SERIAL_NO, &s) > 0) {
1009 		if ((dnode->ddn_serial = disk_trim_whitespace(mod, s)) == NULL)
1010 			goto error;
1011 	} else {
1012 		/*
1013 		 * Many USB disk devices don't emulate serial inquiry number
1014 		 * because their serial number can be longer than the standard
1015 		 * SCSI length. If we didn't get an inquiry serial number, fill
1016 		 * one in this way.
1017 		 */
1018 		di_node_t parent;
1019 
1020 		if ((parent = di_parent_node(node)) != DI_NODE_NIL &&
1021 		    di_prop_lookup_strings(DDI_DEV_T_ANY, parent,
1022 		    "usb-serialno", &s) > 0) {
1023 			if ((dnode->ddn_serial = disk_trim_whitespace(mod,
1024 			    s)) == NULL) {
1025 				goto error;
1026 			}
1027 		}
1028 	}
1029 
1030 	if (di_prop_lookup_int64(DDI_DEV_T_ANY, node,
1031 	    "device-nblocks", &nblocksp) > 0) {
1032 		nblocks = (uint64_t)*nblocksp;
1033 		/*
1034 		 * To save kernel memory, the driver may not define
1035 		 * "device-blksize" when its value is default DEV_BSIZE.
1036 		 */
1037 		if (di_prop_lookup_ints(DDI_DEV_T_ANY, node,
1038 		    "device-blksize", &blksizep) > 0)
1039 			blksize = (uint_t)*blksizep;
1040 		else
1041 			blksize = DEV_BSIZE;		/* default value */
1042 		(void) snprintf(lentry, sizeof (lentry),
1043 		    "%" PRIu64, nblocks * blksize);
1044 		if ((dnode->ddn_cap = topo_mod_strdup(mod, lentry)) == NULL)
1045 			goto error;
1046 	}
1047 
1048 	topo_mod_dprintf(mod, "dev_di_node_add: "
1049 	    "adding %s\n", devid ? dnode->ddn_devid : "NULL devid");
1050 	topo_mod_dprintf(mod, "                  "
1051 	    "       %s\n", dnode->ddn_dpath);
1052 	for (i = 0; i < dnode->ddn_ppath_count; i++) {
1053 		topo_mod_dprintf(mod, "                  "
1054 		    "       %s\n", dnode->ddn_ppath[i]);
1055 	}
1056 	topo_list_append(cbp->dcb_list, dnode);
1057 	return (0);
1058 
1059 error:
1060 	dev_di_node_free(mod, dnode);
1061 	return (-1);
1062 }
1063 
1064 /* di_walk_node callback for disk_list_gather */
1065 static int
1066 dev_walk_di_nodes(di_node_t node, void *arg)
1067 {
1068 	char			*devidstr = NULL;
1069 	char			*s;
1070 	int			*val;
1071 
1072 	/*
1073 	 * If it's not a scsi_vhci client and doesn't have a target_port
1074 	 * property and doesn't have a target property then it's not a storage
1075 	 * device and we're not interested.
1076 	 */
1077 	if (di_path_client_next_path(node, NULL) == NULL &&
1078 	    di_prop_lookup_strings(DDI_DEV_T_ANY, node,
1079 	    SCSI_ADDR_PROP_TARGET_PORT, &s) <= 0 &&
1080 	    di_prop_lookup_ints(DDI_DEV_T_ANY, node,
1081 	    SCSI_ADDR_PROP_TARGET, &val) <= 0) {
1082 		return (DI_WALK_CONTINUE);
1083 	}
1084 	(void) di_prop_lookup_strings(DDI_DEV_T_ANY, node,
1085 	    DEVID_PROP_NAME, &devidstr);
1086 
1087 	/* create/find the devid scsi topology node */
1088 	(void) dev_di_node_add(node, devidstr, arg);
1089 
1090 	return (DI_WALK_CONTINUE);
1091 }
1092 
1093 int
1094 dev_list_gather(topo_mod_t *mod, topo_list_t *listp)
1095 {
1096 	di_node_t		devtree;
1097 	di_devlink_handle_t	devhdl;
1098 	disk_cbdata_t		dcb;
1099 
1100 	if ((devtree = topo_mod_devinfo(mod)) == DI_NODE_NIL) {
1101 		topo_mod_dprintf(mod, "disk_list_gather: "
1102 		    "topo_mod_devinfo() failed");
1103 		return (-1);
1104 	}
1105 
1106 	if ((devhdl = di_devlink_init(NULL, 0)) == DI_NODE_NIL) {
1107 		topo_mod_dprintf(mod, "disk_list_gather: "
1108 		    "di_devlink_init() failed");
1109 		return (-1);
1110 	}
1111 
1112 	dcb.dcb_mod = mod;
1113 	dcb.dcb_list = listp;
1114 	dcb.dcb_devhdl = devhdl;
1115 
1116 	/* walk the devinfo snapshot looking for disk nodes */
1117 	(void) di_walk_node(devtree, DI_WALK_CLDFIRST, &dcb,
1118 	    dev_walk_di_nodes);
1119 
1120 	(void) di_devlink_fini(&devhdl);
1121 
1122 	return (0);
1123 }
1124 
1125 void
1126 dev_list_free(topo_mod_t *mod, topo_list_t *listp)
1127 {
1128 	dev_di_node_t	*dnode;
1129 
1130 	while ((dnode = topo_list_next(listp)) != NULL) {
1131 		/* order of delete/free is important */
1132 		topo_list_delete(listp, dnode);
1133 		dev_di_node_free(mod, dnode);
1134 	}
1135 }
1136 
1137 /*
1138  * Query the current disk status. If successful, the disk status is returned
1139  * as an nvlist consisting of at least the following members:
1140  *
1141  *	protocol	string		Supported protocol (currently "scsi")
1142  *
1143  *	status		nvlist		Arbitrary protocol-specific information
1144  *					about the current state of the disk.
1145  *
1146  *	faults		nvlist		A list of supported faults. Each
1147  *					element of this list is a boolean value.
1148  *					An element's existence indicates that
1149  *					the drive supports detecting this fault,
1150  *					and the value indicates the current
1151  *					state of the fault.
1152  *
1153  *	<fault-name>	nvlist		For each fault named in 'faults', a
1154  *					nvlist describing protocol-specific
1155  *					attributes of the fault.
1156  *
1157  * This method relies on the libdiskstatus library to query this information.
1158  */
1159 static int
1160 disk_status(topo_mod_t *mod, tnode_t *nodep, topo_version_t vers,
1161     nvlist_t *in_nvl, nvlist_t **out_nvl)
1162 {
1163 	disk_status_t	*dsp;
1164 	char		*devpath, *fullpath;
1165 	size_t		pathlen;
1166 	nvlist_t	*status;
1167 	int		err;
1168 
1169 	*out_nvl = NULL;
1170 
1171 	if (vers != TOPO_METH_DISK_STATUS_VERSION)
1172 		return (topo_mod_seterrno(mod, EMOD_VER_NEW));
1173 
1174 	/*
1175 	 * If the caller specifies the "path" parameter, then this indicates
1176 	 * that we should use this instead of deriving it from the topo node
1177 	 * itself.
1178 	 */
1179 	if (nvlist_lookup_string(in_nvl, "path", &fullpath) == 0) {
1180 		devpath = NULL;
1181 	} else {
1182 		/*
1183 		 * Get the /devices path and attempt to open the disk status
1184 		 * handle.
1185 		 */
1186 		if (topo_prop_get_string(nodep, TOPO_PGROUP_IO,
1187 		    TOPO_IO_DEV_PATH, &devpath, &err) != 0)
1188 			return (topo_mod_seterrno(mod, EMOD_METHOD_NOTSUP));
1189 
1190 		/*
1191 		 * Note that sizeof(string) includes the terminating NULL byte
1192 		 */
1193 		pathlen = strlen(devpath) + sizeof ("/devices") +
1194 		    sizeof (PHYS_EXTN) - 1;
1195 
1196 		if ((fullpath = topo_mod_alloc(mod, pathlen)) == NULL)
1197 			return (topo_mod_seterrno(mod, EMOD_NOMEM));
1198 
1199 		(void) snprintf(fullpath, pathlen, "/devices%s%s", devpath,
1200 		    PHYS_EXTN);
1201 
1202 		topo_mod_strfree(mod, devpath);
1203 	}
1204 
1205 	if ((dsp = disk_status_open(fullpath, &err)) == NULL) {
1206 		if (devpath)
1207 			topo_mod_free(mod, fullpath, pathlen);
1208 		return (topo_mod_seterrno(mod, err == EDS_NOMEM ?
1209 		    EMOD_NOMEM : EMOD_METHOD_NOTSUP));
1210 	}
1211 
1212 	if (devpath)
1213 		topo_mod_free(mod, fullpath, pathlen);
1214 
1215 	if ((status = disk_status_get(dsp)) == NULL) {
1216 		err = (disk_status_errno(dsp) == EDS_NOMEM ?
1217 		    EMOD_NOMEM : EMOD_METHOD_NOTSUP);
1218 		disk_status_close(dsp);
1219 		return (topo_mod_seterrno(mod, err));
1220 	}
1221 
1222 	*out_nvl = status;
1223 	disk_status_close(dsp);
1224 	return (0);
1225 }
1226