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 2006 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 
42 #include <hostbridge.h>
43 #include <pcibus.h>
44 #include <did.h>
45 #include <did_props.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  * Hopefully this hack routine goes away when fmdump can print the labels.
414  */
415 static int
416 FRU_fmri_hack(topo_mod_t *mp, tnode_t *tn, const char *label)
417 {
418 	char buf[PATH_MAX];
419 	nvlist_t *fmri;
420 	int err, e;
421 
422 
423 	(void) snprintf(buf, PATH_MAX, "hc:///component=%s", label);
424 	if (topo_mod_str2nvl(mp, buf, &fmri) < 0)
425 		return (-1);
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 *label, *nm;
441 	int e;
442 
443 	nm = topo_node_name(tn);
444 	mp = did_mod(pd);
445 
446 	/*
447 	 * If this topology node represents something other than an
448 	 * ioboard or a device that implements a slot, inherit the
449 	 * parent's FRU value.  If there is no label, inherit our
450 	 * parent's FRU value.  Otherwise, munge up an fmri based on
451 	 * the label.
452 	 */
453 	if (strcmp(nm, "ioboard") != 0 && strcmp(nm, PCI_DEVICE) != 0 &&
454 	    strcmp(nm, PCIEX_DEVICE) != 0) {
455 		(void) topo_node_fru_set(tn, NULL, 0, &e);
456 		return (0);
457 	}
458 
459 	if (topo_prop_get_string(tn,
460 	    TOPO_PGROUP_PROTOCOL, TOPO_PROP_LABEL, &label, &e) < 0) {
461 		if (e != ETOPO_PROP_NOENT)
462 			return (topo_mod_seterrno(mp, e));
463 		(void) topo_node_fru_set(tn, NULL, 0, &e);
464 		return (0);
465 	}
466 	e = FRU_fmri_hack(mp, tn, label);
467 	topo_mod_strfree(mp, label);
468 	return (e);
469 }
470 
471 /*ARGSUSED*/
472 static int
473 label_set(tnode_t *tn, did_t *pd,
474     const char *dpnm, const char *tpgrp, const char *tpnm)
475 {
476 	topo_mod_t *mp;
477 	nvlist_t *in, *out;
478 	char *label;
479 	int err;
480 
481 	mp = did_mod(pd);
482 	if (topo_mod_nvalloc(mp, &in, NV_UNIQUE_NAME) != 0)
483 		return (topo_mod_seterrno(mp, EMOD_FMRI_NVL));
484 	if (nvlist_add_uint64(in, TOPO_METH_LABEL_ARG_NVL, (uintptr_t)pd) !=
485 	    0) {
486 		nvlist_free(in);
487 		return (topo_mod_seterrno(mp, EMOD_NOMEM));
488 	}
489 	if (topo_method_invoke(tn,
490 	    TOPO_METH_LABEL, TOPO_METH_LABEL_VERSION, in, &out, &err) != 0) {
491 		nvlist_free(in);
492 		return (topo_mod_seterrno(mp, err));
493 	}
494 	nvlist_free(in);
495 	if (out != NULL &&
496 	    nvlist_lookup_string(out, TOPO_METH_LABEL_RET_STR, &label) == 0) {
497 		if (topo_prop_set_string(tn, TOPO_PGROUP_PROTOCOL,
498 		    TOPO_PROP_LABEL, TOPO_PROP_IMMUTABLE, label, &err) != 0) {
499 			nvlist_free(out);
500 			return (topo_mod_seterrno(mp, err));
501 		}
502 		nvlist_free(out);
503 	}
504 	return (0);
505 }
506 
507 /*ARGSUSED*/
508 static int
509 EXCAP_set(tnode_t *tn, did_t *pd,
510     const char *dpnm, const char *tpgrp, const char *tpnm)
511 {
512 	int excap;
513 	int err;
514 	int e = 0;
515 
516 	if ((excap = did_excap(pd)) <= 0)
517 		return (0);
518 
519 	switch (excap & PCIE_PCIECAP_DEV_TYPE_MASK) {
520 	case PCIE_PCIECAP_DEV_TYPE_ROOT:
521 		e = topo_prop_set_string(tn, TOPO_PGROUP_PCI,
522 		    TOPO_PCI_EXCAP, TOPO_PROP_IMMUTABLE, PCIEX_ROOT, &err);
523 		break;
524 	case PCIE_PCIECAP_DEV_TYPE_UP:
525 		e = topo_prop_set_string(tn, TOPO_PGROUP_PCI,
526 		    TOPO_PCI_EXCAP, TOPO_PROP_IMMUTABLE, PCIEX_SWUP, &err);
527 		break;
528 	case PCIE_PCIECAP_DEV_TYPE_DOWN:
529 		e = topo_prop_set_string(tn, TOPO_PGROUP_PCI,
530 		    TOPO_PCI_EXCAP, TOPO_PROP_IMMUTABLE, PCIEX_SWDWN, &err);
531 		break;
532 	case PCIE_PCIECAP_DEV_TYPE_PCI2PCIE:
533 		e = topo_prop_set_string(tn, TOPO_PGROUP_PCI,
534 		    TOPO_PCI_EXCAP, TOPO_PROP_IMMUTABLE, PCIEX_BUS, &err);
535 		break;
536 	case PCIE_PCIECAP_DEV_TYPE_PCIE2PCI:
537 		e = topo_prop_set_string(tn, TOPO_PGROUP_PCI,
538 		    TOPO_PCI_EXCAP, TOPO_PROP_IMMUTABLE, PCI_BUS, &err);
539 		break;
540 	case PCIE_PCIECAP_DEV_TYPE_PCIE_DEV:
541 		e = topo_prop_set_string(tn, TOPO_PGROUP_PCI,
542 		    TOPO_PCI_EXCAP, TOPO_PROP_IMMUTABLE, PCIEX_DEVICE, &err);
543 		break;
544 	}
545 	if (e != 0)
546 		return (topo_mod_seterrno(did_mod(pd), err));
547 	return (0);
548 }
549 
550 /*ARGSUSED*/
551 static int
552 DEVprop_set(tnode_t *tn, did_t *pd,
553     const char *dpnm, const char *tpgrp, const char *tpnm)
554 {
555 	topo_mod_t *mp;
556 	char *dnpath;
557 	char *path, *fpath;
558 	int d, f;
559 	int err, e;
560 
561 	mp = did_mod(pd);
562 	if ((dnpath = di_devfs_path(did_dinode(pd))) == NULL) {
563 		topo_mod_dprintf(mp, "NULL di_devfs_path.\n");
564 		return (topo_mod_seterrno(mp, ETOPO_PROP_NOENT));
565 	}
566 	if ((path = topo_mod_strdup(mp, dnpath)) == NULL) {
567 		di_devfs_path_free(dnpath);
568 		return (-1);
569 	}
570 	di_devfs_path_free(dnpath);
571 
572 	/* The DEV path is modified for hostbridges */
573 	if (strcmp(topo_node_name(tn), HOSTBRIDGE) == 0) {
574 		fpath = dev_for_hostbridge(did_mod(pd), path);
575 	} else {
576 		did_BDF(pd, NULL, &d, &f);
577 		fpath = dev_path_fix(mp, path, d, f);
578 	}
579 	if (fpath == NULL)
580 		return (-1);
581 	e = topo_prop_set_string(tn,
582 	    tpgrp, tpnm, TOPO_PROP_IMMUTABLE, fpath, &err);
583 	topo_mod_strfree(mp, fpath);
584 	if (e != 0)
585 		return (topo_mod_seterrno(mp, err));
586 	return (0);
587 }
588 
589 /*ARGSUSED*/
590 static int
591 DRIVERprop_set(tnode_t *tn, did_t *pd,
592     const char *dpnm, const char *tpgrp, const char *tpnm)
593 {
594 	char *dnm;
595 	int err;
596 
597 	if ((dnm = di_driver_name(did_dinode(pd))) == NULL)
598 		return (0);
599 	if (topo_prop_set_string(tn,
600 	    tpgrp, tpnm, TOPO_PROP_IMMUTABLE, dnm, &err) < 0)
601 		return (topo_mod_seterrno(did_mod(pd), err));
602 
603 	return (0);
604 }
605 
606 /*ARGSUSED*/
607 static int
608 MODULEprop_set(tnode_t *tn, did_t *pd,
609     const char *dpnm, const char *tpgrp, const char *tpnm)
610 {
611 	nvlist_t *mod;
612 	topo_mod_t *mp;
613 	char *dnm;
614 	int err;
615 
616 	if ((dnm = di_driver_name(did_dinode(pd))) == NULL)
617 		return (0);
618 
619 	mp = did_mod(pd);
620 	if ((mod = topo_mod_modfmri(mp, FM_MOD_SCHEME_VERSION, dnm)) == NULL)
621 		return (-1); /* mod errno set */
622 
623 	if (topo_prop_set_fmri(tn, tpgrp, tpnm, TOPO_PROP_IMMUTABLE, mod,
624 	    &err) < 0) {
625 		nvlist_free(mod);
626 		return (topo_mod_seterrno(mp, err));
627 	}
628 	nvlist_free(mod);
629 
630 	return (0);
631 }
632 
633 /*ARGSUSED*/
634 static int
635 maybe_di_chars_copy(tnode_t *tn, did_t *pd,
636     const char *dpnm, const char *tpgrp, const char *tpnm)
637 {
638 	topo_mod_t *mp;
639 	uchar_t *typbuf;
640 	char *tmpbuf;
641 	int sz = -1;
642 	int err, e;
643 
644 	if (di_bytes_get(did_mod(pd), did_dinode(pd), dpnm, &sz, &typbuf) < 0)
645 		return (0);
646 	mp = did_mod(pd);
647 	tmpbuf = topo_mod_alloc(mp, sz + 1);
648 	bcopy(typbuf, tmpbuf, sz);
649 	tmpbuf[sz] = 0;
650 	e = topo_prop_set_string(tn,
651 	    tpgrp, tpnm, TOPO_PROP_IMMUTABLE, tmpbuf, &err);
652 	topo_mod_free(mp, tmpbuf, sz + 1);
653 	if (e != 0)
654 		return (topo_mod_seterrno(mp, err));
655 	return (0);
656 }
657 
658 static int
659 uint_to_strprop(topo_mod_t *mp, uint_t v, tnode_t *tn,
660     const char *tpgrp, const char *tpnm)
661 {
662 	char str[21]; /* sizeof (UINT64_MAX) + '\0' */
663 	int e;
664 
665 	(void) snprintf(str, 21, "%x", v);
666 	if (topo_prop_set_string(tn,
667 	    tpgrp, tpnm, TOPO_PROP_IMMUTABLE, str, &e) < 0)
668 		return (topo_mod_seterrno(mp, e));
669 	return (0);
670 }
671 
672 static int
673 maybe_di_uint_to_str(tnode_t *tn, did_t *pd,
674     const char *dpnm, const char *tpgrp, const char *tpnm)
675 {
676 	uint_t v;
677 
678 	if (di_uintprop_get(did_mod(pd), did_dinode(pd), dpnm, &v) < 0)
679 		return (0);
680 
681 	return (uint_to_strprop(did_mod(pd), v, tn, tpgrp, tpnm));
682 }
683 
684 /*ARGSUSED*/
685 static int
686 BDF_set(tnode_t *tn, did_t *pd, const char *dpnm, const char *tpgrp,
687     const char *tpnm)
688 {
689 	int bdf;
690 	char str[23]; /* '0x' + sizeof (UINT64_MAX) + '\0' */
691 	int e;
692 
693 	if ((bdf = did_bdf(pd)) <= 0)
694 		return (0);
695 
696 	(void) snprintf(str, 23, "0x%x", bdf);
697 	if (topo_prop_set_string(tn,
698 	    tpgrp, tpnm, TOPO_PROP_IMMUTABLE, str, &e) < 0)
699 		return (topo_mod_seterrno(did_mod(pd), e));
700 	return (0);
701 }
702 
703 int
704 did_props_set(tnode_t *tn, did_t *pd, txprop_t txarray[], int txnum)
705 {
706 	topo_mod_t *mp;
707 	int i, r, e;
708 
709 	mp = did_mod(pd);
710 	for (i = 0; i < txnum; i++) {
711 		/*
712 		 * Ensure the property group has been created.
713 		 */
714 		if (txarray[i].tx_tpgroup != NULL) {
715 			if (topo_pgroup_create(tn, txarray[i].tx_tpgroup, &e)
716 			    < 0) {
717 				if (e != ETOPO_PROP_DEFD)
718 					return (topo_mod_seterrno(mp, e));
719 			}
720 		}
721 
722 		topo_mod_dprintf(mp,
723 		    "Setting property %s in group %s.\n",
724 		    txarray[i].tx_tprop, txarray[i].tx_tpgroup->tpi_name);
725 		r = txarray[i].tx_xlate(tn, pd,
726 		    txarray[i].tx_diprop, txarray[i].tx_tpgroup->tpi_name,
727 		    txarray[i].tx_tprop);
728 		if (r != 0) {
729 			topo_mod_dprintf(mp, "failed.\n");
730 			topo_mod_dprintf(mp, "Error was %s.\n",
731 			    topo_strerror(topo_mod_errno(mp)));
732 			return (-1);
733 		}
734 		topo_mod_dprintf(mp, "succeeded.\n");
735 	}
736 	return (0);
737 }
738