xref: /titanic_41/usr/src/cmd/devfsadm/cfg_link.c (revision 91c7793e73e8fb0edb9c55f2828d2dfd8ff09994)
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 <devfsadm.h>
30 #include <stdio.h>
31 #include <strings.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <limits.h>
35 #include <unistd.h>
36 #include <config_admin.h>
37 #include <cfg_link.h>
38 #include <sys/types.h>
39 #include <sys/mkdev.h>
40 #include <sys/hotplug/pci/pcihp.h>
41 
42 #ifdef	DEBUG
43 #define	dprint(args)	devfsadm_errprint args
44 /*
45  * for use in print routine arg list as a shorthand way to locate node via
46  * "prtconf -D" to avoid messy and cluttered debugging code
47  * don't forget the corresponding "%s%d" format
48  */
49 #define	DRVINST(node)	di_driver_name(node), di_instance(node)
50 #else
51 #define	dprint(args)
52 #endif
53 
54 
55 static int	scsi_cfg_creat_cb(di_minor_t minor, di_node_t node);
56 static int	sbd_cfg_creat_cb(di_minor_t minor, di_node_t node);
57 static int	usb_cfg_creat_cb(di_minor_t minor, di_node_t node);
58 static char	*get_roothub(const char *path, void *cb_arg);
59 static int	pci_cfg_creat_cb(di_minor_t minor, di_node_t node);
60 static int	ib_cfg_creat_cb(di_minor_t minor, di_node_t node);
61 static int	sata_cfg_creat_cb(di_minor_t minor, di_node_t node);
62 
63 static di_node_t	pci_cfg_chassis_node(di_node_t, di_prom_handle_t);
64 static char 	*pci_cfg_slotname(di_node_t, di_prom_handle_t, minor_t);
65 static int	pci_cfg_ap_node(minor_t, di_node_t, di_prom_handle_t,
66 		    char *, int, int);
67 static int	pci_cfg_iob_name(di_minor_t, di_node_t, di_prom_handle_t,
68 		    char *, int);
69 static minor_t	pci_cfg_pcidev(di_node_t, di_prom_handle_t);
70 static int	pci_cfg_ap_path(di_minor_t, di_node_t, di_prom_handle_t,
71 		    char *, int, char **);
72 static char 	*pci_cfg_info_data(char *);
73 static int	pci_cfg_is_ap_path(di_node_t, di_prom_handle_t);
74 static int	pci_cfg_ap_legacy(di_minor_t, di_node_t, di_prom_handle_t,
75 		    char *, int);
76 static void	pci_cfg_rm_invalid_links(char *, char *);
77 static void	pci_cfg_rm_link(char *);
78 static void	pci_cfg_rm_all(char *);
79 static char	*pci_cfg_devpath(di_node_t, di_minor_t);
80 static di_node_t	pci_cfg_snapshot(di_node_t, di_minor_t,
81 			    di_node_t *, di_minor_t *);
82 
83 /* flag definitions for di_propall_*(); value "0" is always the default flag */
84 #define	DIPROP_PRI_NODE		0x0
85 #define	DIPROP_PRI_PROM		0x1
86 static int	di_propall_lookup_ints(di_prom_handle_t, int,
87 		    dev_t, di_node_t, const char *, int **);
88 static int	di_propall_lookup_strings(di_prom_handle_t, int,
89 		    dev_t, di_node_t, const char *, char **);
90 
91 
92 /*
93  * NOTE: The CREATE_DEFER flag is private to this module.
94  *	 NOT to be used by other modules
95  */
96 static devfsadm_create_t cfg_create_cbt[] = {
97 	{ "attachment-point", DDI_NT_SCSI_ATTACHMENT_POINT, NULL,
98 	    TYPE_EXACT | CREATE_DEFER, ILEVEL_0, scsi_cfg_creat_cb
99 	},
100 	{ "attachment-point", DDI_NT_SBD_ATTACHMENT_POINT, NULL,
101 	    TYPE_EXACT, ILEVEL_0, sbd_cfg_creat_cb
102 	},
103 	{ "fc-attachment-point", DDI_NT_FC_ATTACHMENT_POINT, NULL,
104 	    TYPE_EXACT | CREATE_DEFER, ILEVEL_0, scsi_cfg_creat_cb
105 	},
106 	{ "attachment-point", DDI_NT_USB_ATTACHMENT_POINT, NULL,
107 	    TYPE_EXACT, ILEVEL_0, usb_cfg_creat_cb
108 	},
109 	{ "attachment-point", DDI_NT_PCI_ATTACHMENT_POINT, NULL,
110 	    TYPE_EXACT, ILEVEL_0, pci_cfg_creat_cb
111 	},
112 	{ "attachment-point", DDI_NT_IB_ATTACHMENT_POINT, NULL,
113 	    TYPE_EXACT, ILEVEL_0, ib_cfg_creat_cb
114 	},
115 	{ "attachment-point", DDI_NT_SATA_ATTACHMENT_POINT, NULL,
116 	    TYPE_EXACT, ILEVEL_0, sata_cfg_creat_cb
117 	}
118 };
119 
120 DEVFSADM_CREATE_INIT_V0(cfg_create_cbt);
121 
122 static devfsadm_remove_t cfg_remove_cbt[] = {
123 	{ "attachment-point", SCSI_CFG_LINK_RE, RM_POST,
124 	    ILEVEL_0, devfsadm_rm_all
125 	},
126 	{ "attachment-point", SBD_CFG_LINK_RE, RM_POST,
127 	    ILEVEL_0, devfsadm_rm_all
128 	},
129 	{ "fc-attachment-point", SCSI_CFG_LINK_RE, RM_POST,
130 	    ILEVEL_0, devfsadm_rm_all
131 	},
132 	{ "attachment-point", USB_CFG_LINK_RE, RM_POST|RM_HOT|RM_ALWAYS,
133 	    ILEVEL_0, devfsadm_rm_all
134 	},
135 	{ "attachment-point", PCI_CFG_LINK_RE, RM_POST,
136 	    ILEVEL_0, devfsadm_rm_all
137 	},
138 	{ "attachment-point", PCI_CFG_PATH_LINK_RE, RM_POST|RM_HOT,
139 	    ILEVEL_0, pci_cfg_rm_all
140 	},
141 	{ "attachment-point", IB_CFG_LINK_RE, RM_POST|RM_HOT|RM_ALWAYS,
142 	    ILEVEL_0, devfsadm_rm_all
143 	},
144 	{ "attachment-point", SATA_CFG_LINK_RE, RM_POST|RM_HOT|RM_ALWAYS,
145 	    ILEVEL_0, devfsadm_rm_all
146 	}
147 };
148 
149 DEVFSADM_REMOVE_INIT_V0(cfg_remove_cbt);
150 
151 static int
152 scsi_cfg_creat_cb(di_minor_t minor, di_node_t node)
153 {
154 	char path[PATH_MAX + 1];
155 	char *c_num = NULL, *devfs_path, *mn;
156 	devfsadm_enumerate_t rules[3] = {
157 	    {"^r?dsk$/^c([0-9]+)", 1, MATCH_PARENT},
158 	    {"^cfg$/^c([0-9]+)$", 1, MATCH_ADDR},
159 	    {"^scsi$/^.+$/^c([0-9]+)", 1, MATCH_PARENT}
160 	};
161 
162 	mn = di_minor_name(minor);
163 
164 	if ((devfs_path = di_devfs_path(node)) == NULL) {
165 		return (DEVFSADM_CONTINUE);
166 	}
167 	(void) strcpy(path, devfs_path);
168 	(void) strcat(path, ":");
169 	(void) strcat(path, mn);
170 	di_devfs_path_free(devfs_path);
171 
172 	if (devfsadm_enumerate_int(path, 1, &c_num, rules, 3)
173 	    == DEVFSADM_FAILURE) {
174 		/*
175 		 * Unlike the disks module we don't retry on failure.
176 		 * If we have multiple "c" numbers for a single physical
177 		 * controller due to bug 4045879, we will not assign a
178 		 * c-number/symlink for the controller.
179 		 */
180 		return (DEVFSADM_CONTINUE);
181 	}
182 
183 	(void) strcpy(path, CFG_DIRNAME);
184 	(void) strcat(path, "/c");
185 	(void) strcat(path, c_num);
186 
187 	free(c_num);
188 
189 	(void) devfsadm_mklink(path, node, minor, 0);
190 
191 	return (DEVFSADM_CONTINUE);
192 }
193 
194 static int
195 sbd_cfg_creat_cb(di_minor_t minor, di_node_t node)
196 {
197 	char path[PATH_MAX + 1];
198 
199 	(void) strcpy(path, CFG_DIRNAME);
200 	(void) strcat(path, "/");
201 	(void) strcat(path, di_minor_name(minor));
202 	(void) devfsadm_mklink(path, node, minor, 0);
203 	return (DEVFSADM_CONTINUE);
204 }
205 
206 
207 static int
208 usb_cfg_creat_cb(di_minor_t minor, di_node_t node)
209 {
210 	char *cp, path[PATH_MAX + 1];
211 	devfsadm_enumerate_t rules[1] =
212 		{"^cfg$/^usb([0-9]+)$", 1, MATCH_CALLBACK, NULL, get_roothub};
213 
214 	if ((cp = di_devfs_path(node)) == NULL) {
215 		return (DEVFSADM_CONTINUE);
216 	}
217 
218 	(void) snprintf(path, sizeof (path), "%s:%s", cp, di_minor_name(minor));
219 	di_devfs_path_free(cp);
220 
221 	if (devfsadm_enumerate_int(path, 0, &cp, rules, 1)) {
222 		return (DEVFSADM_CONTINUE);
223 	}
224 
225 	/* create usbN and the symlink */
226 	(void) snprintf(path, sizeof (path), "%s/usb%s/%s", CFG_DIRNAME, cp,
227 	    di_minor_name(minor));
228 	free(cp);
229 
230 	(void) devfsadm_mklink(path, node, minor, 0);
231 
232 	return (DEVFSADM_CONTINUE);
233 }
234 
235 
236 static int
237 sata_cfg_creat_cb(di_minor_t minor, di_node_t node)
238 {
239 	char path[PATH_MAX + 1], l_path[PATH_MAX], *buf, *devfspath;
240 	char *minor_nm;
241 	devfsadm_enumerate_t rules[1] =
242 		{"^cfg$/^sata([0-9]+)$", 1, MATCH_ADDR};
243 
244 	minor_nm = di_minor_name(minor);
245 	if (minor_nm == NULL)
246 		return (DEVFSADM_CONTINUE);
247 
248 	devfspath = di_devfs_path(node);
249 	if (devfspath == NULL)
250 		return (DEVFSADM_CONTINUE);
251 
252 	(void) strlcpy(path, devfspath, sizeof (path));
253 	(void) strlcat(path, ":", sizeof (path));
254 	(void) strlcat(path, minor_nm, sizeof (path));
255 	di_devfs_path_free(devfspath);
256 
257 	/* build the physical path from the components */
258 	if (devfsadm_enumerate_int(path, 0, &buf, rules, 1) ==
259 	    DEVFSADM_FAILURE) {
260 		return (DEVFSADM_CONTINUE);
261 	}
262 
263 	(void) snprintf(l_path, sizeof (l_path), "%s/sata%s/%s", CFG_DIRNAME,
264 			buf, minor_nm);
265 	free(buf);
266 
267 	(void) devfsadm_mklink(l_path, node, minor, 0);
268 
269 	return (DEVFSADM_CONTINUE);
270 }
271 
272 
273 /*
274  * get_roothub:
275  *	figure out the root hub path to calculate /dev/cfg/usbN
276  */
277 /* ARGSUSED */
278 static char *
279 get_roothub(const char *path, void *cb_arg)
280 {
281 	int  i, count = 0;
282 	char *physpath, *cp;
283 
284 	/* make a copy */
285 	if ((physpath = strdup(path)) == NULL) {
286 		return (NULL);
287 	}
288 
289 	/*
290 	 * physpath must always have a minor name component
291 	 */
292 	if ((cp = strrchr(physpath, ':')) == NULL) {
293 		free(physpath);
294 		return (NULL);
295 	}
296 	*cp++ = '\0';
297 
298 	/*
299 	 * No '.' in the minor name indicates a roothub port.
300 	 */
301 	if (strchr(cp, '.') == NULL) {
302 		/* roothub device */
303 		return (physpath);
304 	}
305 
306 	while (*cp) {
307 		if (*cp == '.')
308 			count++;
309 		cp++;
310 	}
311 
312 	/* Remove as many trailing path components as there are '.'s */
313 	for (i = 0; i < count; i++) {
314 		if ((cp = strrchr(physpath, '/')) == NULL || (cp == physpath)) {
315 			free(physpath);
316 			return (NULL);
317 		}
318 		*cp = '\0';
319 	}
320 
321 	return (physpath);
322 }
323 
324 
325 /*
326  * returns an allocted string containing the device path for <node> and
327  * <minor>
328  */
329 static char *
330 pci_cfg_devpath(di_node_t node, di_minor_t minor)
331 {
332 	char *path;
333 	char *bufp;
334 	char *minor_nm;
335 	int buflen;
336 
337 	path = di_devfs_path(node);
338 	minor_nm = di_minor_name(minor);
339 	buflen = snprintf(NULL, 0, "%s:%s", path, minor_nm);
340 
341 	bufp = malloc(sizeof (char) * buflen);
342 	if (bufp == NULL)
343 		goto OUT;
344 	(void) snprintf(bufp, buflen, "%s:%s", path, minor_nm);
345 
346 OUT:
347 	di_devfs_path_free(path);
348 	return (bufp);
349 }
350 
351 
352 static int
353 di_propall_lookup_ints(di_prom_handle_t ph, int flags,
354     dev_t dev, di_node_t node, const char *prop_name, int **prop_data)
355 {
356 	int rv;
357 
358 	if (flags & DIPROP_PRI_PROM) {
359 		rv = di_prom_prop_lookup_ints(ph, node, prop_name, prop_data);
360 		if (rv < 0)
361 			rv = di_prop_lookup_ints(dev, node, prop_name,
362 			    prop_data);
363 	} else {
364 		rv = di_prop_lookup_ints(dev, node, prop_name, prop_data);
365 		if (rv < 0)
366 			rv = di_prom_prop_lookup_ints(ph, node, prop_name,
367 			    prop_data);
368 	}
369 	return (rv);
370 }
371 
372 
373 static int
374 di_propall_lookup_strings(di_prom_handle_t ph, int flags,
375     dev_t dev, di_node_t node, const char *prop_name, char **prop_data)
376 {
377 	int rv;
378 
379 	if (flags & DIPROP_PRI_PROM) {
380 		rv = di_prom_prop_lookup_strings(ph, node, prop_name,
381 		    prop_data);
382 		if (rv < 0)
383 			rv = di_prop_lookup_strings(dev, node, prop_name,
384 			    prop_data);
385 	} else {
386 		rv = di_prop_lookup_strings(dev, node, prop_name, prop_data);
387 		if (rv < 0)
388 			rv = di_prom_prop_lookup_strings(ph, node, prop_name,
389 			    prop_data);
390 	}
391 	return (rv);
392 }
393 
394 
395 static di_node_t
396 pci_cfg_chassis_node(di_node_t node, di_prom_handle_t ph)
397 {
398 	di_node_t curnode = node;
399 	int *firstchas;
400 
401 	do {
402 		if (di_propall_lookup_ints(ph, 0, DDI_DEV_T_ANY, curnode,
403 		    PROP_FIRST_CHAS, &firstchas) >= 0)
404 			return (curnode);
405 	} while ((curnode = di_parent_node(curnode)) != DI_NODE_NIL);
406 
407 	return (DI_NODE_NIL);
408 }
409 
410 
411 /*
412  * yet another redundant common routine to:
413  * decode the ieee1275 "slot-names" property and returns the string matching
414  * the pci device number <pci_dev>, if any.
415  *
416  * callers must NOT free the returned string
417  *
418  * "slot-names" format: [int][string1][string2]...[stringN]
419  *	- each bit position in [int] represent a pci device number
420  *	- [string1]...[stringN] are concatenated null-terminated strings
421  *	- the number of bits set in [int] == the number of strings that follow
422  *	- each bit that is set corresponds to a string in the following segment
423  */
424 static char *
425 pci_cfg_slotname(di_node_t node, di_prom_handle_t ph, minor_t pci_dev)
426 {
427 #ifdef	DEBUG
428 	char *fnm = "pci_cfg_slotname";
429 #endif
430 	int *snp;
431 	int snlen;
432 	int snentlen = sizeof (int);
433 	int i, max, len, place, curplace;
434 	char *str;
435 
436 	snlen = di_propall_lookup_ints(ph, 0, DDI_DEV_T_ANY, node,
437 	    PROP_SLOT_NAMES, &snp);
438 	if (snlen < 1)
439 		return (NULL);
440 	if ((snp[0] & (1 << pci_dev)) == 0)
441 		return (NULL);
442 
443 	/*
444 	 * pci device number must be less than the amount of bits in the first
445 	 * [int] component of slot-names
446 	 */
447 	if (pci_dev >= snentlen * 8) {
448 		dprint(("%s: pci_dev out of range for %s%d\n",
449 		    fnm, DRVINST(node)));
450 		return (NULL);
451 	}
452 
453 	place = 0;
454 	for (i = 0; i < pci_dev; i++) {
455 		if (snp[0] & (1 << i))
456 			place++;
457 	}
458 
459 	max = (snlen * snentlen) - snentlen;
460 	str = (char *)&snp[1];
461 	i = 0;
462 	curplace = 0;
463 	while (i < max && curplace < place) {
464 		len = strlen(str);
465 		if (len <= 0)
466 			break;
467 		str += len + 1;
468 		i += len + 1;
469 		curplace++;
470 	}
471 	/* the following condition indicates a badly formed slot-names */
472 	if (i >= max || *str == '\0') {
473 		dprint(("%s: badly formed slot-names for %s%d\n",
474 		    fnm, DRVINST(node)));
475 		str = NULL;
476 	}
477 	return (str);
478 }
479 
480 
481 /*
482  * returns non-zero if we can return a valid attachment point name for <node>,
483  * for its slot identified by child pci device number <pci_dev>, through <buf>
484  *
485  * prioritized naming scheme:
486  *	1) <PROP_SLOT_NAMES property>    (see pci_cfg_slotname())
487  *	2) <device-type><PROP_PHYS_SLOT property>
488  *	3) <drv name><drv inst>.<device-type><pci_dev>
489  *
490  * where <device-type> is derived from the PROP_DEV_TYPE property:
491  *	if its value is "pciex" then <device-type> is "pcie"
492  *	else the raw value is used
493  *
494  * if <flags> contains APNODE_DEFNAME, then scheme (3) is used
495  */
496 static int
497 pci_cfg_ap_node(minor_t pci_dev, di_node_t node, di_prom_handle_t ph,
498     char *buf, int bufsz, int flags)
499 {
500 	int *nump;
501 	int rv;
502 	char *str, *devtype;
503 
504 	rv = di_propall_lookup_strings(ph, 0, DDI_DEV_T_ANY, node,
505 	    PROP_DEV_TYPE, &devtype);
506 	if (rv < 1)
507 		return (0);
508 
509 	if (strcmp(devtype, PROPVAL_PCIEX) == 0)
510 		devtype = DEVTYPE_PCIE;
511 
512 	if (flags & APNODE_DEFNAME)
513 		goto DEF;
514 
515 	str = pci_cfg_slotname(node, ph, pci_dev);
516 	if (str != NULL) {
517 		(void) strlcpy(buf, str, bufsz);
518 		return (1);
519 	}
520 
521 	if (di_propall_lookup_ints(ph, 0, DDI_DEV_T_ANY, node, PROP_PHYS_SLOT,
522 	    &nump) > 0) {
523 		if (*nump > 0) {
524 			(void) snprintf(buf, bufsz, "%s%d", devtype, *nump);
525 			return (1);
526 		}
527 	}
528 DEF:
529 	(void) snprintf(buf, bufsz, "%s%d.%s%d",
530 	    di_driver_name(node), di_instance(node), devtype, pci_dev);
531 
532 	return (1);
533 }
534 
535 
536 /*
537  * returns non-zero if we can return a valid expansion chassis name for <node>
538  * through <buf>
539  *
540  * prioritized naming scheme:
541  *	1) <IOB_PRE string><PROP_SERID property: sun specific portion>
542  *	2) <IOB_PRE string><full PROP_SERID property in hex>
543  *	3) <IOB_PRE string>
544  *
545  * PROP_SERID encoding <64-bit int: msb ... lsb>:
546  * <24 bits: vendor id><40 bits: serial number>
547  *
548  * sun encoding of 40 bit serial number:
549  * first byte = device type indicator (ignored in naming scheme)
550  * next 4 bytes = 4 ascii characters
551  */
552 /*ARGSUSED*/
553 static int
554 pci_cfg_iob_name(di_minor_t minor, di_node_t node, di_prom_handle_t ph,
555     char *buf, int bufsz)
556 {
557 	int64_t *seridp;
558 	int64_t serid;
559 	char *idstr;
560 
561 	if (di_prop_lookup_int64(DDI_DEV_T_ANY, node, PROP_SERID,
562 	    &seridp) < 1) {
563 		(void) strlcpy(buf, IOB_PRE, bufsz);
564 		return (1);
565 	}
566 	serid = *seridp;
567 
568 	if (serid >> 40 != VENDID_SUN) {
569 		(void) snprintf(buf, bufsz, "%s%llx", IOB_PRE, serid);
570 		return (1);
571 	}
572 
573 	serid &= SIZE2MASK64(40);
574 	idstr = (char *)&serid;
575 	idstr[sizeof (serid) - 1] = '\0';
576 	/* skip device type indicator */
577 	idstr++;
578 	(void) snprintf(buf, bufsz, "%s%s", IOB_PRE, idstr);
579 	return (1);
580 }
581 
582 
583 static minor_t
584 pci_cfg_pcidev(di_node_t node, di_prom_handle_t ph)
585 {
586 	int rv;
587 	int *regp;
588 
589 	rv = di_propall_lookup_ints(ph, 0, DDI_DEV_T_ANY, node, PROP_REG,
590 	    &regp);
591 
592 	if (rv < 1) {
593 		dprint(("pci_cfg_pcidev: property %s not found "
594 		    "for %s%d\n", PROP_REG, DRVINST(node)));
595 		return (rv);
596 	}
597 
598 	return (REG_PCIDEV(regp));
599 }
600 
601 
602 /*
603  * returns non-zero when it can successfully return an attachment point
604  * through <ap_path> whose length is less than <ap_pathsz>; returns the full
605  * path of the AP through <pathret> which may be larger than <ap_pathsz>.
606  * Callers need to free <pathret>.  If it cannot return the full path through
607  * <pathret> it will be set to NULL
608  *
609  * The ap path reflects a subset of the device path from an onboard host slot
610  * up to <node>.  We traverse up the device tree starting from <node>, naming
611  * each component using pci_cfg_ap_node().  If we detect that a certain
612  * segment is contained within an expansion chassis, then we skip any bus
613  * nodes in between our current node and the topmost node of the chassis,
614  * which is identified by the PROP_FIRST_CHAS property, and prepend the name
615  * of the expansion chassis as given by pci_cfg_iob_name()
616  *
617  * This scheme is always used for <pathret>.  If however, the size of
618  * <pathret> is greater than <ap_pathsz> then only the default name as given
619  * by pci_cfg_ap_node() for <node> will be used
620  */
621 static int
622 pci_cfg_ap_path(di_minor_t minor, di_node_t node, di_prom_handle_t ph,
623     char *ap_path, int ap_pathsz, char **pathret)
624 {
625 #ifdef	DEBUG
626 	char *fnm = "pci_cfg_ap_path";
627 #endif
628 #define	seplen		(sizeof (AP_PATH_SEP) - 1)
629 #define	iob_pre_len	(sizeof (IOB_PRE) - 1)
630 #define	ap_path_iob_sep_len	(sizeof (AP_PATH_IOB_SEP) - 1)
631 
632 	char *bufptr;
633 	char buf[MAXPATHLEN];
634 	char pathbuf[MAXPATHLEN];
635 	int bufsz;
636 	char *pathptr;
637 	char *pathend = NULL;
638 	int len;
639 	int rv = 0;
640 	int chasflag = 0;
641 	di_node_t curnode = node;
642 	di_node_t chasnode = DI_NODE_NIL;
643 	minor_t pci_dev;
644 
645 	buf[0] = '\0';
646 	pathbuf[0] = '\0';
647 	pathptr = &pathbuf[sizeof (pathbuf) - 1];
648 	*pathptr = '\0';
649 
650 	/*
651 	 * as we traverse up the device tree, we prepend components of our
652 	 * path inside pathbuf, using pathptr and decrementing
653 	 */
654 	pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(di_minor_devt(minor));
655 	do {
656 		bufptr = buf;
657 		bufsz = sizeof (buf);
658 
659 		chasnode = pci_cfg_chassis_node(curnode, ph);
660 		if (chasnode != DI_NODE_NIL) {
661 			rv = pci_cfg_iob_name(minor, chasnode, ph,
662 			    bufptr, bufsz);
663 			if (rv == 0) {
664 				dprint(("%s: cannot create iob name "
665 				    "for %s%d\n", fnm, DRVINST(node)));
666 				*pathptr = '\0';
667 				goto OUT;
668 			}
669 
670 			(void) strncat(bufptr, AP_PATH_IOB_SEP, bufsz);
671 			len = strlen(bufptr);
672 			bufptr += len;
673 			bufsz -= len - 1;
674 
675 			/* set chasflag when the leaf node is within an iob */
676 			if ((curnode == node) != NULL)
677 				chasflag = 1;
678 		}
679 		rv = pci_cfg_ap_node(pci_dev, curnode, ph, bufptr, bufsz, 0);
680 		if (rv == 0) {
681 			dprint(("%s: cannot create ap node name "
682 			    "for %s%d\n", fnm, DRVINST(node)));
683 			*pathptr = '\0';
684 			goto OUT;
685 		}
686 
687 		/*
688 		 * if we can't fit the entire path in our pathbuf, then use
689 		 * the default short name and nullify pathptr; also, since
690 		 * we prepend in the buffer, we must avoid adding a null char
691 		 */
692 		if (curnode != node) {
693 			pathptr -= seplen;
694 			if (pathptr < pathbuf) {
695 				pathptr = pathbuf;
696 				*pathptr = '\0';
697 				goto DEF;
698 			}
699 			(void) memcpy(pathptr, AP_PATH_SEP, seplen);
700 		}
701 		len = strlen(buf);
702 		pathptr -= len;
703 		if (pathptr < pathbuf) {
704 			pathptr = pathbuf;
705 			*pathptr = '\0';
706 			goto DEF;
707 		}
708 		(void) memcpy(pathptr, buf, len);
709 
710 		/* remember the leaf component */
711 		if (curnode == node)
712 			pathend = pathptr;
713 
714 		/*
715 		 * go no further than the hosts' onboard slots
716 		 */
717 		if (chasnode == DI_NODE_NIL)
718 			break;
719 		curnode = chasnode;
720 
721 		/*
722 		 * the pci device number of the current node is used to
723 		 * identify which slot of the parent's bus (next iteration)
724 		 * the current node is on
725 		 */
726 		pci_dev = pci_cfg_pcidev(curnode, ph);
727 	} while ((curnode = di_parent_node(curnode)) != DI_NODE_NIL);
728 
729 	pathbuf[sizeof (pathbuf) - 1] = '\0';
730 	if (strlen(pathptr) < ap_pathsz) {
731 		(void) strlcpy(ap_path, pathptr, ap_pathsz);
732 		rv = 1;
733 		goto OUT;
734 	}
735 
736 DEF:
737 	/*
738 	 * when our name won't fit <ap_pathsz> we use the endpoint/leaf
739 	 * <node>'s name ONLY IF it has a serialid# which will make the apid
740 	 * globally unique
741 	 */
742 	if (chasflag && pathend != NULL) {
743 		if ((strncmp(pathend + iob_pre_len, AP_PATH_IOB_SEP,
744 		    ap_path_iob_sep_len) != 0) &&
745 		    (strlen(pathend) < ap_pathsz)) {
746 			(void) strlcpy(ap_path, pathend, ap_pathsz);
747 			rv = 1;
748 			goto OUT;
749 		}
750 	}
751 
752 	/*
753 	 * if our name still won't fit <ap_pathsz>, then use the leaf <node>'s
754 	 * default name
755 	 */
756 	rv = pci_cfg_ap_node(pci_dev, node, ph, buf, bufsz, APNODE_DEFNAME);
757 	if (rv == 0) {
758 		dprint(("%s: cannot create default ap node name for %s%d\n",
759 		    fnm, DRVINST(node)));
760 		*pathptr = '\0';
761 		goto OUT;
762 	}
763 	if (strlen(buf) < ap_pathsz) {
764 		(void) strlcpy(ap_path, buf, ap_pathsz);
765 		rv = 1;
766 		goto OUT;
767 	}
768 
769 	/*
770 	 * in this case, cfgadm goes through an expensive process to generate
771 	 * a purely dynamic logical apid: the framework will look through
772 	 * the device tree for attachment point minor nodes and will invoke
773 	 * each plugin responsible for that attachment point class, and if
774 	 * the plugin returns a logical apid that matches the queried apid
775 	 * or matches the default apid generated by the cfgadm framework for
776 	 * that driver/class (occurs when plugin returns an empty logical apid)
777 	 * then that is what it will use
778 	 *
779 	 * it is doubly expensive because the cfgadm pci plugin itself will
780 	 * also search the entire device tree in the absence of a link
781 	 */
782 	rv = 0;
783 	dprint(("%s: cannot create apid for %s%d within length of %d\n",
784 	    fnm, DRVINST(node), ap_pathsz));
785 
786 OUT:
787 	ap_path[ap_pathsz - 1] = '\0';
788 	*pathret = (*pathptr == '\0') ? NULL : strdup(pathptr);
789 	return (rv);
790 
791 #undef	seplen
792 #undef	iob_pre_len
793 #undef	ap_path_iob_sep_len
794 }
795 
796 
797 /*
798  * the PROP_AP_NAMES property contains the first integer section of the
799  * ieee1275 "slot-names" property and functions as a bitmask; see comment for
800  * pci_cfg_slotname()
801  *
802  * we use the name of the attachment point minor node if its pci device
803  * number (encoded in the minor number) is allowed by PROP_AP_NAMES
804  *
805  * returns non-zero if we return a valid attachment point through <path>
806  */
807 static int
808 pci_cfg_ap_legacy(di_minor_t minor, di_node_t node, di_prom_handle_t ph,
809     char *ap_path, int ap_pathsz)
810 {
811 	minor_t pci_dev;
812 	int *anp;
813 
814 	if (di_propall_lookup_ints(ph, 0, DDI_DEV_T_ANY, node, PROP_AP_NAMES,
815 	    &anp) < 1)
816 		return (0);
817 
818 	pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(di_minor_devt(minor));
819 	if ((*anp & (1 << pci_dev)) == 0)
820 		return (0);
821 
822 	(void) strlcpy(ap_path, di_minor_name(minor), ap_pathsz);
823 	return (1);
824 }
825 
826 
827 /*
828  * determine if <node> qualifies for a path style apid
829  */
830 static int
831 pci_cfg_is_ap_path(di_node_t node, di_prom_handle_t ph)
832 {
833 	char *devtype;
834 	di_node_t curnode = node;
835 
836 	do {
837 		if (di_propall_lookup_strings(ph, 0, DDI_DEV_T_ANY, curnode,
838 		    PROP_DEV_TYPE, &devtype) > 0)
839 			if (strcmp(devtype, PROPVAL_PCIEX) == 0)
840 				return (1);
841 	} while ((curnode = di_parent_node(curnode)) != DI_NODE_NIL);
842 
843 	return (0);
844 }
845 
846 
847 /*
848  * takes a full path as returned by <pathret> from pci_cfg_ap_path() and
849  * returns an allocated string intendend to be stored in a devlink info (dli)
850  * file
851  *
852  * data format: "Location: <transformed path>"
853  * where <transformed path> is <path> with occurrances of AP_PATH_SEP
854  * replaced by "/"
855  */
856 static char *
857 pci_cfg_info_data(char *path)
858 {
859 #define	head	"Location: "
860 #define	headlen	(sizeof (head) - 1)
861 #define	seplen	(sizeof (AP_PATH_SEP) - 1)
862 
863 	char *sep, *prev, *np;
864 	char *newpath;
865 	int pathlen = strlen(path);
866 	int len;
867 
868 	newpath = malloc(sizeof (char) * (headlen + pathlen + 1));
869 	np = newpath;
870 	(void) strcpy(np, head);
871 	np += headlen;
872 
873 	prev = path;
874 	while ((sep = strstr(prev, AP_PATH_SEP)) != NULL) {
875 		len = sep - prev;
876 		(void) memcpy(np, prev, len);
877 		np += len;
878 		*np++ = '/';
879 		prev = sep + seplen;
880 	}
881 	(void) strcpy(np, prev);
882 	return (newpath);
883 
884 #undef	head
885 #undef	headlen
886 #undef	seplen
887 }
888 
889 
890 static void
891 pci_cfg_rm_link(char *file)
892 {
893 	char *dlipath;
894 
895 	dlipath = di_dli_name(file);
896 	(void) unlink(dlipath);
897 
898 	devfsadm_rm_all(file);
899 	free(dlipath);
900 }
901 
902 /*
903  * removes all registered devlinks to physical path <physpath> except for
904  * the devlink <valid> if not NULL;
905  * <physpath> must include the minor node
906  */
907 static void
908 pci_cfg_rm_invalid_links(char *physpath, char *valid)
909 {
910 	char **dnp;
911 	char *cp, *vcp;
912 	int i, dnlen;
913 
914 	dnp = devfsadm_lookup_dev_names(physpath, NULL, &dnlen);
915 	if (dnp == NULL)
916 		return;
917 
918 	if (valid != NULL) {
919 		if (strncmp(valid, DEV "/", DEV_LEN + 1) == 0)
920 			vcp = valid + DEV_LEN + 1;
921 		else
922 			vcp = valid;
923 	}
924 
925 	for (i = 0; i < dnlen; i++) {
926 		if (strncmp(dnp[i], DEV "/", DEV_LEN + 1) == 0)
927 			cp = dnp[i] + DEV_LEN + 1;
928 		else
929 			cp = dnp[i];
930 
931 		if (valid != NULL) {
932 			if (strcmp(vcp, cp) == 0)
933 				continue;
934 		}
935 		pci_cfg_rm_link(cp);
936 	}
937 	devfsadm_free_dev_names(dnp, dnlen);
938 }
939 
940 
941 /*
942  * takes a complete devinfo snapshot and returns the root node;
943  * callers must do a di_fini() on the returned node;
944  * if the snapshot failed, DI_NODE_NIL is returned instead
945  *
946  * if <pci_node> is not DI_NODE_NIL, it will search for the same devinfo node
947  * in the new snapshot and return it through <ret_node> if it is found,
948  * else DI_NODE_NIL is returned instead
949  *
950  * in addition, if <pci_minor> is not DI_MINOR_NIL, it will also return
951  * the matching minor in the new snapshot through <ret_minor> if it is found,
952  * else DI_MINOR_NIL is returned instead
953  */
954 static di_node_t
955 pci_cfg_snapshot(di_node_t pci_node, di_minor_t pci_minor,
956     di_node_t *ret_node, di_minor_t *ret_minor)
957 {
958 	di_node_t root_node;
959 	di_node_t node;
960 	di_minor_t minor;
961 	int pci_inst;
962 	dev_t pci_devt;
963 
964 	*ret_node = DI_NODE_NIL;
965 	*ret_minor = DI_MINOR_NIL;
966 
967 	root_node = di_init("/", DINFOCPYALL);
968 	if (root_node == DI_NODE_NIL)
969 		return (DI_NODE_NIL);
970 
971 	/*
972 	 * narrow down search by driver, then instance, then minor
973 	 */
974 	if (pci_node == DI_NODE_NIL)
975 		return (root_node);
976 
977 	pci_inst = di_instance(pci_node);
978 	node = di_drv_first_node(di_driver_name(pci_node), root_node);
979 	do {
980 		if (pci_inst == di_instance(node)) {
981 			*ret_node = node;
982 			break;
983 		}
984 	} while ((node = di_drv_next_node(node)) != DI_NODE_NIL);
985 
986 	if (node == DI_NODE_NIL)
987 		return (root_node);
988 
989 	/*
990 	 * found node, now search minors
991 	 */
992 	if (pci_minor == DI_MINOR_NIL)
993 		return (root_node);
994 
995 	pci_devt = di_minor_devt(pci_minor);
996 	minor = DI_MINOR_NIL;
997 	while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
998 		if (pci_devt == di_minor_devt(minor)) {
999 			*ret_minor = minor;
1000 			break;
1001 		}
1002 	}
1003 	return (root_node);
1004 }
1005 
1006 
1007 static int
1008 pci_cfg_creat_cb(di_minor_t pci_minor, di_node_t pci_node)
1009 {
1010 #ifdef	DEBUG
1011 	char *fnm = "pci_cfg_creat_cb";
1012 #endif
1013 #define	ap_pathsz	(sizeof (ap_path))
1014 
1015 	char ap_path[CFGA_LOG_EXT_LEN];
1016 	char linkbuf[MAXPATHLEN];
1017 	char *fullpath = NULL;
1018 	char *pathinfo = NULL;
1019 	char *devpath = NULL;
1020 	int rv, fd;
1021 	size_t sz;
1022 	di_prom_handle_t ph;
1023 	di_node_t node;
1024 	di_node_t root_node = DI_NODE_NIL;
1025 	di_minor_t minor;
1026 
1027 	ph = di_prom_init();
1028 	if (ph == DI_PROM_HANDLE_NIL) {
1029 		dprint(("%s: di_prom_init() failed for %s%d\n",
1030 		    fnm, DRVINST(pci_node)));
1031 		goto OUT;
1032 	}
1033 
1034 	/*
1035 	 * Since incoming nodes from hotplug events are from snapshots that
1036 	 * do NOT contain parent/ancestor data, we must retake our own
1037 	 * snapshot and search for the target node
1038 	 */
1039 	root_node = pci_cfg_snapshot(pci_node, pci_minor, &node, &minor);
1040 	if (root_node == DI_NODE_NIL || node == DI_NODE_NIL ||
1041 	    minor == DI_MINOR_NIL) {
1042 		dprint(("%s: devinfo snapshot or search failed for %s%d\n",
1043 		    fnm, DRVINST(pci_node)));
1044 		goto OUT;
1045 	}
1046 
1047 	if (pci_cfg_is_ap_path(node, ph)) {
1048 		rv = pci_cfg_ap_path(minor, node, ph, ap_path, ap_pathsz,
1049 		    &fullpath);
1050 		if (rv == 0)
1051 			goto OUT;
1052 
1053 		(void) snprintf(linkbuf, sizeof (linkbuf), "%s/%s",
1054 		    CFG_DIRNAME, ap_path);
1055 
1056 		/*
1057 		 * We must remove existing links because we may have invalid
1058 		 * apids that are valid links.  Since these are not dangling,
1059 		 * devfsadm will not invoke the remove callback on them.
1060 		 *
1061 		 * What are "invalid apids with valid links"?  Consider swapping
1062 		 * an attachment point bus with another while the system is
1063 		 * down, on the same device path bound to the same drivers
1064 		 * but with the new AP bus having different properties
1065 		 * (e.g. serialid#).  If the previous apid is not removed,
1066 		 * there will now be two different links pointing to the same
1067 		 * attachment point, but only one reflects the correct
1068 		 * logical apid
1069 		 */
1070 		devpath = pci_cfg_devpath(node, minor);
1071 		if (devpath == NULL)
1072 			goto OUT;
1073 		pci_cfg_rm_invalid_links(devpath, linkbuf);
1074 		free(devpath);
1075 
1076 		(void) devfsadm_mklink(linkbuf, node, minor, 0);
1077 
1078 		/*
1079 		 * we store the full logical path of the attachment point for
1080 		 * cfgadm to display in its info field which is useful when
1081 		 * the full logical path exceeds the size limit for logical
1082 		 * apids (CFGA_LOG_EXT_LEN)
1083 		 *
1084 		 * for the cfgadm pci plugin to do the same would be expensive
1085 		 * (i.e. devinfo snapshot + top down exhaustive minor search +
1086 		 * equivalent of pci_cfg_ap_path() on every invocation)
1087 		 *
1088 		 * note that if we do not create a link (pci_cfg_ap_path() is
1089 		 * not successful), that is what cfgadm will do anyways to
1090 		 * create a purely dynamic apid
1091 		 */
1092 		pathinfo = pci_cfg_info_data(fullpath);
1093 		fd = di_dli_openw(linkbuf);
1094 		if (fd < 0)
1095 			goto OUT;
1096 
1097 		sz = strlen(pathinfo) + 1;
1098 		rv = write(fd, pathinfo, sz);
1099 		if (rv < sz) {
1100 			dprint(("%s: could not write full pathinfo to dli "
1101 			    "file for %s%d\n", fnm, DRVINST(node)));
1102 			goto OUT;
1103 		}
1104 		di_dli_close(fd);
1105 	} else {
1106 		rv = pci_cfg_ap_legacy(minor, node, ph, ap_path,
1107 		    ap_pathsz);
1108 		if (rv == 0)
1109 			goto OUT;
1110 
1111 		(void) snprintf(linkbuf, sizeof (linkbuf), "%s/%s",
1112 		    CFG_DIRNAME, ap_path);
1113 		(void) devfsadm_mklink(linkbuf, node, minor, 0);
1114 	}
1115 
1116 OUT:
1117 	if (fullpath != NULL)
1118 		free(fullpath);
1119 	if (pathinfo != NULL)
1120 		free(pathinfo);
1121 	if (ph != DI_PROM_HANDLE_NIL)
1122 		di_prom_fini(ph);
1123 	if (root_node != DI_NODE_NIL)
1124 		di_fini(root_node);
1125 	return (DEVFSADM_CONTINUE);
1126 
1127 #undef	ap_pathsz
1128 }
1129 
1130 
1131 static void
1132 pci_cfg_rm_all(char *file)
1133 {
1134 	pci_cfg_rm_link(file);
1135 }
1136 
1137 
1138 /*
1139  * ib_cfg_creat_cb() creates two types of links
1140  * One for the fabric as /dev/cfg/ib
1141  * Another for each HCA seen in the fabric as /dev/cfg/hca:<HCA-GUID>
1142  */
1143 static int
1144 ib_cfg_creat_cb(di_minor_t minor, di_node_t node)
1145 {
1146 	char	*cp;
1147 	char	path[PATH_MAX + 1];
1148 
1149 	if ((cp = di_devfs_path(node)) == NULL) {
1150 		return (DEVFSADM_CONTINUE);
1151 	}
1152 
1153 	(void) snprintf(path, sizeof (path), "%s:%s", cp, di_minor_name(minor));
1154 	di_devfs_path_free(cp);
1155 
1156 	/* create fabric or hca:GUID and the symlink */
1157 	if (strstr(path, "ib:fabric") != NULL) {
1158 		(void) snprintf(path, sizeof (path), "%s/ib", CFG_DIRNAME);
1159 	} else {
1160 		(void) snprintf(path, sizeof (path), "%s/hca:%s", CFG_DIRNAME,
1161 		    di_minor_name(minor));
1162 	}
1163 
1164 	(void) devfsadm_mklink(path, node, minor, 0);
1165 	return (DEVFSADM_CONTINUE);
1166 }
1167