xref: /illumos-gate/usr/src/lib/fm/topo/modules/common/pcibus/did_props.c (revision 24da5b34f49324ed742a340010ed5bd3d4e06625)
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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <assert.h>
30 #include <alloca.h>
31 #include <string.h>
32 #include <strings.h>
33 #include <limits.h>
34 #include <sys/types.h>
35 #include <sys/pci.h>
36 #include <sys/pcie.h>
37 #include <sys/fm/protocol.h>
38 #include <fm/topo_mod.h>
39 #include <fm/topo_hc.h>
40 #include <libdevinfo.h>
41 #include <hostbridge.h>
42 #include <pcibus.h>
43 #include <did.h>
44 #include <did_props.h>
45 #include <fm/libtopo.h>
46 
47 static int ASRU_set(tnode_t *, did_t *,
48     const char *, const char *, const char *);
49 static int FRU_set(tnode_t *, did_t *,
50     const char *, const char *, const char *);
51 static int DEVprop_set(tnode_t *, did_t *,
52     const char *, const char *, const char *);
53 static int DRIVERprop_set(tnode_t *, did_t *,
54     const char *, const char *, const char *);
55 static int MODULEprop_set(tnode_t *, did_t *,
56     const char *, const char *, const char *);
57 static int EXCAP_set(tnode_t *, did_t *,
58     const char *, const char *, const char *);
59 static int BDF_set(tnode_t *, did_t *,
60     const char *, const char *, const char *);
61 static int label_set(tnode_t *, did_t *,
62     const char *, const char *, const char *);
63 static int maybe_di_chars_copy(tnode_t *, did_t *,
64     const char *, const char *, const char *);
65 static int maybe_di_uint_to_str(tnode_t *, did_t *,
66     const char *, const char *, const char *);
67 
68 /*
69  * Arrays of "property translation routines" to set the properties a
70  * given type of topology node should have.
71  *
72  * Note that the label_set translation *MUST COME BEFORE* the FRU
73  * translation.  For the near term we're setting the FRU fmri to
74  * be a legacy-hc style FMRI based on the label, so the label needs
75  * to have been set before we do the FRU translation.
76  *
77  */
78 
79 static const topo_pgroup_info_t io_pgroup =
80 	{ TOPO_PGROUP_IO, TOPO_STABILITY_PRIVATE, TOPO_STABILITY_PRIVATE, 1 };
81 static const topo_pgroup_info_t pci_pgroup =
82 	{ TOPO_PGROUP_PCI, TOPO_STABILITY_PRIVATE, TOPO_STABILITY_PRIVATE, 1 };
83 
84 static const topo_pgroup_info_t protocol_pgroup = {
85 	TOPO_PGROUP_PROTOCOL,
86 	TOPO_STABILITY_PRIVATE,
87 	TOPO_STABILITY_PRIVATE,
88 	1
89 }; /* Request to create protocol will be ignored by libtopo */
90 
91 txprop_t Fn_common_props[] = {
92 	{ NULL, &io_pgroup, TOPO_IO_DEV, DEVprop_set },
93 	{ DI_DEVTYPPROP, &io_pgroup, TOPO_IO_DEVTYPE, maybe_di_chars_copy },
94 	{ DI_DEVIDPROP, &pci_pgroup, TOPO_PCI_DEVID, maybe_di_uint_to_str },
95 	{ NULL, &io_pgroup, TOPO_IO_DRIVER, DRIVERprop_set },
96 	{ NULL, &io_pgroup, TOPO_IO_MODULE, MODULEprop_set },
97 	{ NULL, &pci_pgroup, TOPO_PCI_EXCAP, EXCAP_set },
98 	{ DI_CLASSPROP, &pci_pgroup, TOPO_PCI_CLASS, maybe_di_uint_to_str },
99 	{ DI_VENDIDPROP, &pci_pgroup, TOPO_PCI_VENDID, maybe_di_uint_to_str },
100 	{ NULL, &protocol_pgroup, TOPO_PROP_LABEL, label_set },
101 	{ NULL, &protocol_pgroup, TOPO_PROP_FRU, FRU_set },
102 	{ NULL, &protocol_pgroup, TOPO_PROP_ASRU, ASRU_set }
103 };
104 
105 txprop_t Dev_common_props[] = {
106 	{ NULL, &protocol_pgroup, TOPO_PROP_LABEL, label_set },
107 	{ NULL, &protocol_pgroup, TOPO_PROP_FRU, FRU_set },
108 	{ NULL, &protocol_pgroup, TOPO_PROP_ASRU, ASRU_set }
109 };
110 
111 txprop_t Bus_common_props[] = {
112 	{ DI_DEVTYPPROP, &io_pgroup, TOPO_IO_DEVTYPE, maybe_di_chars_copy },
113 	{ NULL, &io_pgroup, TOPO_IO_DRIVER, DRIVERprop_set },
114 	{ NULL, &io_pgroup, TOPO_IO_MODULE, MODULEprop_set },
115 	{ NULL, &protocol_pgroup, TOPO_PROP_LABEL, label_set },
116 	{ NULL, &protocol_pgroup, TOPO_PROP_FRU, FRU_set },
117 	{ NULL, &protocol_pgroup, TOPO_PROP_ASRU, ASRU_set }
118 };
119 
120 txprop_t RC_common_props[] = {
121 	{ NULL, &io_pgroup, TOPO_IO_DEV, DEVprop_set },
122 	{ DI_DEVTYPPROP, &io_pgroup, TOPO_IO_DEVTYPE, maybe_di_chars_copy },
123 	{ NULL, &io_pgroup, TOPO_IO_DRIVER, DRIVERprop_set },
124 	{ NULL, &io_pgroup, TOPO_IO_MODULE, MODULEprop_set },
125 	{ NULL, &pci_pgroup, TOPO_PCI_EXCAP, EXCAP_set },
126 	{ NULL, &pci_pgroup, TOPO_PCI_BDF, BDF_set },
127 	{ NULL, &protocol_pgroup, TOPO_PROP_LABEL, label_set },
128 	{ NULL, &protocol_pgroup, TOPO_PROP_FRU, FRU_set },
129 	{ NULL, &protocol_pgroup, TOPO_PROP_ASRU, ASRU_set }
130 };
131 
132 txprop_t ExHB_common_props[] = {
133 	{ NULL, &protocol_pgroup, TOPO_PROP_LABEL, label_set },
134 	{ NULL, &protocol_pgroup, TOPO_PROP_FRU, FRU_set },
135 	{ NULL, &protocol_pgroup, TOPO_PROP_ASRU, ASRU_set }
136 };
137 
138 txprop_t IOB_common_props[] = {
139 	{ NULL, &protocol_pgroup, TOPO_PROP_LABEL, label_set },
140 	{ NULL, &protocol_pgroup, TOPO_PROP_FRU, FRU_set },
141 	{ NULL, &protocol_pgroup, TOPO_PROP_ASRU, ASRU_set }
142 };
143 
144 txprop_t HB_common_props[] = {
145 	{ NULL, &io_pgroup, TOPO_IO_DEV, DEVprop_set },
146 	{ NULL, &io_pgroup, TOPO_IO_DRIVER, DRIVERprop_set },
147 	{ NULL, &io_pgroup, TOPO_IO_MODULE, MODULEprop_set },
148 	{ NULL, &protocol_pgroup, TOPO_PROP_LABEL, label_set },
149 	{ NULL, &protocol_pgroup, TOPO_PROP_FRU, FRU_set },
150 	{ NULL, &protocol_pgroup, TOPO_PROP_ASRU, ASRU_set }
151 };
152 
153 int Bus_propcnt = sizeof (Bus_common_props) / sizeof (txprop_t);
154 int Dev_propcnt = sizeof (Dev_common_props) / sizeof (txprop_t);
155 int ExHB_propcnt = sizeof (ExHB_common_props) / sizeof (txprop_t);
156 int HB_propcnt = sizeof (HB_common_props) / sizeof (txprop_t);
157 int IOB_propcnt = sizeof (IOB_common_props) / sizeof (txprop_t);
158 int RC_propcnt = sizeof (RC_common_props) / sizeof (txprop_t);
159 int Fn_propcnt = sizeof (Fn_common_props) / sizeof (txprop_t);
160 
161 /*
162  * If this devinfo node came originally from OBP data, we'll have prom
163  * properties associated with the node where we can find properties of
164  * interest.  We ignore anything after the the first four bytes of the
165  * property, and interpet those first four bytes as our unsigned
166  * integer.  If we don't find the property or it's not large enough,
167  * 'val' will remained unchanged and we'll return -1.  Otherwise 'val'
168  * gets updated with the property value and we return 0.
169  */
170 static int
171 promprop2uint(topo_mod_t *mod, di_node_t n, const char *propnm, uint_t *val)
172 {
173 	di_prom_handle_t ptp = DI_PROM_HANDLE_NIL;
174 	di_prom_prop_t pp = DI_PROM_PROP_NIL;
175 	uchar_t *buf;
176 
177 	if ((ptp = topo_mod_prominfo(mod)) == DI_PROM_HANDLE_NIL)
178 		return (-1);
179 
180 	while ((pp = di_prom_prop_next(ptp, n, pp)) != DI_PROM_PROP_NIL) {
181 		if (strcmp(di_prom_prop_name(pp), propnm) == 0) {
182 			if (di_prom_prop_data(pp, &buf) < sizeof (uint_t))
183 				continue;
184 			bcopy(buf, val, sizeof (uint_t));
185 			return (0);
186 		}
187 	}
188 	return (-1);
189 }
190 
191 /*
192  * If this devinfo node was added by the PCI hotplug framework it
193  * doesn't have the PROM properties, but hopefully has the properties
194  * we're looking for attached directly to the devinfo node.  We only
195  * care about the first four bytes of the property, which we read as
196  * our unsigned integer.  The remaining bytes are ignored.  If we
197  * don't find the property we're looking for, or can't get its value,
198  * 'val' remains unchanged and we return -1.  Otherwise 'val' gets the
199  * property value and we return 0.
200  */
201 static int
202 hwprop2uint(di_node_t n, const char *propnm, uint_t *val)
203 {
204 	di_prop_t hp = DI_PROP_NIL;
205 	uchar_t *buf;
206 
207 	while ((hp = di_prop_next(n, hp)) != DI_PROP_NIL) {
208 		if (strcmp(di_prop_name(hp), propnm) == 0) {
209 			if (di_prop_bytes(hp, &buf) < sizeof (uint_t))
210 				continue;
211 			bcopy(buf, val, sizeof (uint_t));
212 			return (0);
213 		}
214 	}
215 	return (-1);
216 }
217 
218 int
219 di_uintprop_get(topo_mod_t *mod, di_node_t n, const char *pnm, uint_t *pv)
220 {
221 	if (hwprop2uint(n, pnm, pv) < 0)
222 		if (promprop2uint(mod, n, pnm, pv) < 0)
223 			return (-1);
224 	return (0);
225 }
226 
227 int
228 di_bytes_get(topo_mod_t *mod, di_node_t n, const char *pnm, int *sz,
229     uchar_t **db)
230 {
231 	di_prom_handle_t ptp = DI_PROM_HANDLE_NIL;
232 	di_prom_prop_t pp = DI_PROM_PROP_NIL;
233 	di_prop_t hp = DI_PROP_NIL;
234 
235 	if ((ptp = topo_mod_prominfo(mod)) == DI_PROM_HANDLE_NIL)
236 		return (-1);
237 
238 	*sz = -1;
239 	while ((hp = di_prop_next(n, hp)) != DI_PROP_NIL) {
240 		if (strcmp(di_prop_name(hp), pnm) == 0) {
241 			if ((*sz = di_prop_bytes(hp, db)) < 0)
242 				continue;
243 			break;
244 		}
245 	}
246 	if (*sz < 0) {
247 		while ((pp = di_prom_prop_next(ptp, n, pp)) !=
248 		    DI_PROM_PROP_NIL) {
249 			if (strcmp(di_prom_prop_name(pp), pnm) == 0) {
250 				*sz = di_prom_prop_data(pp, db);
251 				if (*sz < 0)
252 					continue;
253 				break;
254 			}
255 		}
256 	}
257 
258 	if (*sz < 0)
259 		return (-1);
260 	return (0);
261 }
262 
263 /*
264  * fix_dev_prop -- sometimes di_devfs_path() doesn't tell the whole
265  * story, leaving off the device and function number.  Chances are if
266  * devfs doesn't put these on then we'll never see this device as an
267  * error detector called out in an ereport.  Unfortunately, there are
268  * races and we sometimes do get ereports from devices that devfs
269  * decides aren't there.  For example, the error injector card seems
270  * to bounce in and out of existence according to devfs.  We tack on
271  * the missing dev and fn here so that the DEV property used to look
272  * up the topology node is correct.
273  */
274 static char *
275 dev_path_fix(topo_mod_t *mp, char *path, int devno, int fnno)
276 {
277 	char *lastslash;
278 	char *newpath;
279 	int need;
280 
281 	/*
282 	 * We only care about the last component of the dev path. If
283 	 * we don't find a slash, something is weird.
284 	 */
285 	lastslash = strrchr(path, '/');
286 	assert(lastslash != NULL);
287 
288 	/*
289 	 * If an @ sign is present in the last component, the
290 	 * di_devfs_path() result had the device,fn unit-address.
291 	 * In that case there's nothing we need do.
292 	 */
293 	if (strchr(lastslash, '@') != NULL)
294 		return (path);
295 
296 	if (fnno == 0)
297 		need = snprintf(NULL, 0, "%s@%x", path, devno);
298 	else
299 		need = snprintf(NULL, 0, "%s@%x,%x", path, devno, fnno);
300 	need++;
301 
302 	if ((newpath = topo_mod_alloc(mp, need)) == NULL) {
303 		topo_mod_strfree(mp, path);
304 		return (NULL);
305 	}
306 
307 	if (fnno == 0)
308 		(void) snprintf(newpath, need, "%s@%x", path, devno);
309 	else
310 		(void) snprintf(newpath, need, "%s@%x,%x", path, devno, fnno);
311 
312 	topo_mod_strfree(mp, path);
313 	return (newpath);
314 }
315 
316 /*
317  * dev_for_hostbridge() -- For hostbridges we truncate the devfs path
318  * after the first element in the bus address.
319  */
320 static char *
321 dev_for_hostbridge(topo_mod_t *mp, char *path)
322 {
323 	char *lastslash;
324 	char *newpath;
325 	char *comma;
326 
327 	/*
328 	 * We only care about the last component of the dev path. If
329 	 * we don't find a slash, something is weird.
330 	 */
331 	lastslash = strrchr(path, '/');
332 	assert(lastslash != NULL);
333 
334 	/*
335 	 * Find the comma in the last component component@x,y, and
336 	 * truncate the comma and any following number.
337 	 */
338 	comma = strchr(lastslash, ',');
339 	assert(comma != NULL);
340 
341 	*comma = '\0';
342 	if ((newpath = topo_mod_strdup(mp, path)) == NULL)
343 		return (path);
344 	*comma = ',';
345 	topo_mod_strfree(mp, path);
346 	return (newpath);
347 }
348 
349 /*ARGSUSED*/
350 static int
351 ASRU_set(tnode_t *tn, did_t *pd,
352     const char *dpnm, const char *tpgrp, const char *tpnm)
353 {
354 	topo_mod_t *mp;
355 	nvlist_t *fmri;
356 	char *dnpath, *path, *fpath, *nm;
357 	int d, e, f;
358 
359 	/*
360 	 * If this topology node represents a function of device,
361 	 * set the ASRU to a dev scheme FMRI based on the value of
362 	 * di_devfs_path().  If that path is NULL, set the ASRU to
363 	 * be the resource describing this topology node.  If this
364 	 * isn't a function, inherit any ASRU from the parent.
365 	 */
366 	mp = did_mod(pd);
367 	nm = topo_node_name(tn);
368 	if (strcmp(nm, PCI_FUNCTION) == 0 || strcmp(nm, PCIEX_FUNCTION) == 0 ||
369 	    strcmp(nm, PCIEX_ROOT) == 0) {
370 		if ((dnpath = di_devfs_path(did_dinode(pd))) != NULL) {
371 			/*
372 			 * Dup the path, dev_path_fix() may replace it and
373 			 * dev_path_fix() wouldn't know to use
374 			 * di_devfs_path_free()
375 			 */
376 			if ((path = topo_mod_strdup(mp, dnpath)) == NULL) {
377 				di_devfs_path_free(dnpath);
378 				return (topo_mod_seterrno(mp, EMOD_NOMEM));
379 			}
380 			di_devfs_path_free(dnpath);
381 			did_BDF(pd, NULL, &d, &f);
382 			if ((fpath = dev_path_fix(mp, path, d, f)) == NULL)
383 				return (topo_mod_seterrno(mp, EMOD_NOMEM));
384 
385 			fmri = topo_mod_devfmri(mp, FM_DEV_SCHEME_VERSION,
386 			    fpath, NULL);
387 			if (fmri == NULL) {
388 				topo_mod_dprintf(mp,
389 				    "dev:///%s fmri creation failed.\n", fpath);
390 				topo_mod_strfree(mp, fpath);
391 				return (-1);
392 			}
393 			topo_mod_strfree(mp, fpath);
394 		} else {
395 			topo_mod_dprintf(mp, "NULL di_devfs_path.\n");
396 			if (topo_prop_get_fmri(tn, TOPO_PGROUP_PROTOCOL,
397 			    TOPO_PROP_RESOURCE, &fmri, &e) < 0)
398 				return (topo_mod_seterrno(mp, e));
399 		}
400 		if (topo_node_asru_set(tn, fmri, 0, &e) < 0) {
401 			nvlist_free(fmri);
402 			return (topo_mod_seterrno(mp, e));
403 		}
404 		nvlist_free(fmri);
405 		return (0);
406 	}
407 	(void) topo_node_asru_set(tn, NULL, 0, &e);
408 
409 	return (0);
410 }
411 
412 /*
413  * Set the FRU property to the hc fmri of this tnode
414  */
415 int
416 FRU_fmri_set(topo_mod_t *mp, tnode_t *tn)
417 {
418 	nvlist_t *fmri;
419 	int err, e;
420 
421 	if (topo_node_resource(tn, &fmri, &err) < 0 ||
422 	    fmri == NULL) {
423 		topo_mod_dprintf(mp, "FRU_fmri_set error: %s\n",
424 			topo_strerror(topo_mod_errno(mp)));
425 		return (topo_mod_seterrno(mp, err));
426 	}
427 	e = topo_node_fru_set(tn, fmri, 0, &err);
428 	nvlist_free(fmri);
429 	if (e < 0)
430 		return (topo_mod_seterrno(mp, err));
431 	return (0);
432 }
433 
434 /*ARGSUSED*/
435 static int
436 FRU_set(tnode_t *tn, did_t *pd,
437     const char *dpnm, const char *tpgrp, const char *tpnm)
438 {
439 	topo_mod_t *mp;
440 	char *nm;
441 	int e = 0, err = 0;
442 
443 	nm = topo_node_name(tn);
444 	mp = did_mod(pd);
445 	/*
446 	 * If this topology node represents something other than an
447 	 * ioboard or a device that implements a slot, inherit the
448 	 * parent's FRU value.  If there is no label, inherit our
449 	 * parent's FRU value.  Otherwise, munge up an fmri based on
450 	 * the label.
451 	 */
452 	if (strcmp(nm, IOBOARD) != 0 && strcmp(nm, PCI_DEVICE) != 0 &&
453 	    strcmp(nm, PCIEX_DEVICE) != 0) {
454 		(void) topo_node_fru_set(tn, NULL, 0, &e);
455 		return (0);
456 	}
457 
458 	/*
459 	 * If ioboard, set fru fmri to hc fmri
460 	 */
461 	if (strcmp(nm, IOBOARD) == 0) {
462 		e = FRU_fmri_set(mp, tn);
463 		return (e);
464 	} else if (strcmp(nm, PCI_DEVICE) == 0 ||
465 		strcmp(nm, PCIEX_DEVICE) == 0) {
466 		nvlist_t *in, *out;
467 
468 		mp = did_mod(pd);
469 		if (topo_mod_nvalloc(mp, &in, NV_UNIQUE_NAME) != 0)
470 			return (topo_mod_seterrno(mp, EMOD_FMRI_NVL));
471 		if (nvlist_add_uint64(in, "nv1", (uintptr_t)pd) != 0) {
472 			nvlist_free(in);
473 			return (topo_mod_seterrno(mp, EMOD_NOMEM));
474 		}
475 		if (topo_method_invoke(tn,
476 			TOPO_METH_FRU_COMPUTE, TOPO_METH_FRU_COMPUTE_VERSION,
477 			in, &out, &err) != 0) {
478 			nvlist_free(in);
479 			return (topo_mod_seterrno(mp, err));
480 		}
481 		nvlist_free(in);
482 		(void) topo_node_fru_set(tn, out, 0, &err);
483 		if (out != NULL)
484 			nvlist_free(out);
485 	} else
486 		(void) topo_node_fru_set(tn, NULL, 0, &err);
487 
488 	return (0);
489 }
490 
491 /*ARGSUSED*/
492 static int
493 label_set(tnode_t *tn, did_t *pd,
494     const char *dpnm, const char *tpgrp, const char *tpnm)
495 {
496 	topo_mod_t *mp;
497 	nvlist_t *in, *out;
498 	char *label;
499 	int err;
500 
501 	mp = did_mod(pd);
502 	if (topo_mod_nvalloc(mp, &in, NV_UNIQUE_NAME) != 0)
503 		return (topo_mod_seterrno(mp, EMOD_FMRI_NVL));
504 	if (nvlist_add_uint64(in, TOPO_METH_LABEL_ARG_NVL, (uintptr_t)pd) !=
505 	    0) {
506 		nvlist_free(in);
507 		return (topo_mod_seterrno(mp, EMOD_NOMEM));
508 	}
509 	if (topo_method_invoke(tn,
510 	    TOPO_METH_LABEL, TOPO_METH_LABEL_VERSION, in, &out, &err) != 0) {
511 		nvlist_free(in);
512 		return (topo_mod_seterrno(mp, err));
513 	}
514 	nvlist_free(in);
515 	if (out != NULL &&
516 	    nvlist_lookup_string(out, TOPO_METH_LABEL_RET_STR, &label) == 0) {
517 		if (topo_prop_set_string(tn, TOPO_PGROUP_PROTOCOL,
518 		    TOPO_PROP_LABEL, TOPO_PROP_IMMUTABLE, label, &err) != 0) {
519 			nvlist_free(out);
520 			return (topo_mod_seterrno(mp, err));
521 		}
522 		nvlist_free(out);
523 	}
524 	return (0);
525 }
526 
527 /*ARGSUSED*/
528 static int
529 EXCAP_set(tnode_t *tn, did_t *pd,
530     const char *dpnm, const char *tpgrp, const char *tpnm)
531 {
532 	int excap;
533 	int err;
534 	int e = 0;
535 
536 	if ((excap = did_excap(pd)) <= 0)
537 		return (0);
538 
539 	switch (excap & PCIE_PCIECAP_DEV_TYPE_MASK) {
540 	case PCIE_PCIECAP_DEV_TYPE_ROOT:
541 		e = topo_prop_set_string(tn, TOPO_PGROUP_PCI,
542 		    TOPO_PCI_EXCAP, TOPO_PROP_IMMUTABLE, PCIEX_ROOT, &err);
543 		break;
544 	case PCIE_PCIECAP_DEV_TYPE_UP:
545 		e = topo_prop_set_string(tn, TOPO_PGROUP_PCI,
546 		    TOPO_PCI_EXCAP, TOPO_PROP_IMMUTABLE, PCIEX_SWUP, &err);
547 		break;
548 	case PCIE_PCIECAP_DEV_TYPE_DOWN:
549 		e = topo_prop_set_string(tn, TOPO_PGROUP_PCI,
550 		    TOPO_PCI_EXCAP, TOPO_PROP_IMMUTABLE, PCIEX_SWDWN, &err);
551 		break;
552 	case PCIE_PCIECAP_DEV_TYPE_PCI2PCIE:
553 		e = topo_prop_set_string(tn, TOPO_PGROUP_PCI,
554 		    TOPO_PCI_EXCAP, TOPO_PROP_IMMUTABLE, PCIEX_BUS, &err);
555 		break;
556 	case PCIE_PCIECAP_DEV_TYPE_PCIE2PCI:
557 		e = topo_prop_set_string(tn, TOPO_PGROUP_PCI,
558 		    TOPO_PCI_EXCAP, TOPO_PROP_IMMUTABLE, PCI_BUS, &err);
559 		break;
560 	case PCIE_PCIECAP_DEV_TYPE_PCIE_DEV:
561 		e = topo_prop_set_string(tn, TOPO_PGROUP_PCI,
562 		    TOPO_PCI_EXCAP, TOPO_PROP_IMMUTABLE, PCIEX_DEVICE, &err);
563 		break;
564 	}
565 	if (e != 0)
566 		return (topo_mod_seterrno(did_mod(pd), err));
567 	return (0);
568 }
569 
570 /*ARGSUSED*/
571 static int
572 DEVprop_set(tnode_t *tn, did_t *pd,
573     const char *dpnm, const char *tpgrp, const char *tpnm)
574 {
575 	topo_mod_t *mp;
576 	char *dnpath;
577 	char *path, *fpath;
578 	int d, f;
579 	int err, e;
580 
581 	mp = did_mod(pd);
582 	if ((dnpath = di_devfs_path(did_dinode(pd))) == NULL) {
583 		topo_mod_dprintf(mp, "NULL di_devfs_path.\n");
584 		return (topo_mod_seterrno(mp, ETOPO_PROP_NOENT));
585 	}
586 	if ((path = topo_mod_strdup(mp, dnpath)) == NULL) {
587 		di_devfs_path_free(dnpath);
588 		return (-1);
589 	}
590 	di_devfs_path_free(dnpath);
591 
592 	/* The DEV path is modified for hostbridges */
593 	if (strcmp(topo_node_name(tn), HOSTBRIDGE) == 0) {
594 		fpath = dev_for_hostbridge(did_mod(pd), path);
595 	} else {
596 		did_BDF(pd, NULL, &d, &f);
597 		fpath = dev_path_fix(mp, path, d, f);
598 	}
599 	if (fpath == NULL)
600 		return (-1);
601 	e = topo_prop_set_string(tn,
602 	    tpgrp, tpnm, TOPO_PROP_IMMUTABLE, fpath, &err);
603 	topo_mod_strfree(mp, fpath);
604 	if (e != 0)
605 		return (topo_mod_seterrno(mp, err));
606 	return (0);
607 }
608 
609 /*ARGSUSED*/
610 static int
611 DRIVERprop_set(tnode_t *tn, did_t *pd,
612     const char *dpnm, const char *tpgrp, const char *tpnm)
613 {
614 	char *dnm;
615 	int err;
616 
617 	if ((dnm = di_driver_name(did_dinode(pd))) == NULL)
618 		return (0);
619 	if (topo_prop_set_string(tn,
620 	    tpgrp, tpnm, TOPO_PROP_IMMUTABLE, dnm, &err) < 0)
621 		return (topo_mod_seterrno(did_mod(pd), err));
622 
623 	return (0);
624 }
625 
626 /*ARGSUSED*/
627 static int
628 MODULEprop_set(tnode_t *tn, did_t *pd,
629     const char *dpnm, const char *tpgrp, const char *tpnm)
630 {
631 	nvlist_t *mod;
632 	topo_mod_t *mp;
633 	char *dnm;
634 	int err;
635 
636 	if ((dnm = di_driver_name(did_dinode(pd))) == NULL)
637 		return (0);
638 
639 	mp = did_mod(pd);
640 	if ((mod = topo_mod_modfmri(mp, FM_MOD_SCHEME_VERSION, dnm)) == NULL)
641 		return (0); /* driver maybe detached, return success */
642 
643 	if (topo_prop_set_fmri(tn, tpgrp, tpnm, TOPO_PROP_IMMUTABLE, mod,
644 	    &err) < 0) {
645 		nvlist_free(mod);
646 		return (topo_mod_seterrno(mp, err));
647 	}
648 	nvlist_free(mod);
649 
650 	return (0);
651 }
652 
653 /*ARGSUSED*/
654 static int
655 maybe_di_chars_copy(tnode_t *tn, did_t *pd,
656     const char *dpnm, const char *tpgrp, const char *tpnm)
657 {
658 	topo_mod_t *mp;
659 	uchar_t *typbuf;
660 	char *tmpbuf;
661 	int sz = -1;
662 	int err, e;
663 
664 	if (di_bytes_get(did_mod(pd), did_dinode(pd), dpnm, &sz, &typbuf) < 0)
665 		return (0);
666 	mp = did_mod(pd);
667 	tmpbuf = topo_mod_alloc(mp, sz + 1);
668 	bcopy(typbuf, tmpbuf, sz);
669 	tmpbuf[sz] = 0;
670 	e = topo_prop_set_string(tn,
671 	    tpgrp, tpnm, TOPO_PROP_IMMUTABLE, tmpbuf, &err);
672 	topo_mod_free(mp, tmpbuf, sz + 1);
673 	if (e != 0)
674 		return (topo_mod_seterrno(mp, err));
675 	return (0);
676 }
677 
678 static int
679 uint_to_strprop(topo_mod_t *mp, uint_t v, tnode_t *tn,
680     const char *tpgrp, const char *tpnm)
681 {
682 	char str[21]; /* sizeof (UINT64_MAX) + '\0' */
683 	int e;
684 
685 	(void) snprintf(str, 21, "%x", v);
686 	if (topo_prop_set_string(tn,
687 	    tpgrp, tpnm, TOPO_PROP_IMMUTABLE, str, &e) < 0)
688 		return (topo_mod_seterrno(mp, e));
689 	return (0);
690 }
691 
692 static int
693 maybe_di_uint_to_str(tnode_t *tn, did_t *pd,
694     const char *dpnm, const char *tpgrp, const char *tpnm)
695 {
696 	uint_t v;
697 
698 	if (di_uintprop_get(did_mod(pd), did_dinode(pd), dpnm, &v) < 0)
699 		return (0);
700 
701 	return (uint_to_strprop(did_mod(pd), v, tn, tpgrp, tpnm));
702 }
703 
704 /*ARGSUSED*/
705 static int
706 BDF_set(tnode_t *tn, did_t *pd, const char *dpnm, const char *tpgrp,
707     const char *tpnm)
708 {
709 	int bdf;
710 	char str[23]; /* '0x' + sizeof (UINT64_MAX) + '\0' */
711 	int e;
712 
713 	if ((bdf = did_bdf(pd)) <= 0)
714 		return (0);
715 
716 	(void) snprintf(str, 23, "0x%x", bdf);
717 	if (topo_prop_set_string(tn,
718 	    tpgrp, tpnm, TOPO_PROP_IMMUTABLE, str, &e) < 0)
719 		return (topo_mod_seterrno(did_mod(pd), e));
720 	return (0);
721 }
722 
723 int
724 did_props_set(tnode_t *tn, did_t *pd, txprop_t txarray[], int txnum)
725 {
726 	topo_mod_t *mp;
727 	int i, r, e;
728 
729 	mp = did_mod(pd);
730 	for (i = 0; i < txnum; i++) {
731 		/*
732 		 * Ensure the property group has been created.
733 		 */
734 		if (txarray[i].tx_tpgroup != NULL) {
735 			if (topo_pgroup_create(tn, txarray[i].tx_tpgroup, &e)
736 			    < 0) {
737 				if (e != ETOPO_PROP_DEFD)
738 					return (topo_mod_seterrno(mp, e));
739 			}
740 		}
741 
742 		topo_mod_dprintf(mp,
743 		    "Setting property %s in group %s.\n",
744 		    txarray[i].tx_tprop, txarray[i].tx_tpgroup->tpi_name);
745 		r = txarray[i].tx_xlate(tn, pd,
746 		    txarray[i].tx_diprop, txarray[i].tx_tpgroup->tpi_name,
747 		    txarray[i].tx_tprop);
748 		if (r != 0) {
749 			topo_mod_dprintf(mp, "failed.\n");
750 			topo_mod_dprintf(mp, "Error was %s.\n",
751 			    topo_strerror(topo_mod_errno(mp)));
752 			return (-1);
753 		}
754 		topo_mod_dprintf(mp, "succeeded.\n");
755 	}
756 	return (0);
757 }
758