xref: /titanic_44/usr/src/cmd/devfsadm/cfg_link.c (revision 0ba2cbe97e0678a691742f98d2532caed0a2c4aa)
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) + 1;
340 
341 	bufp = malloc(sizeof (char) * buflen);
342 	if (bufp != NULL)
343 		(void) snprintf(bufp, buflen, "%s:%s", path, minor_nm);
344 
345 	di_devfs_path_free(path);
346 	return (bufp);
347 }
348 
349 
350 static int
351 di_propall_lookup_ints(di_prom_handle_t ph, int flags,
352     dev_t dev, di_node_t node, const char *prop_name, int **prop_data)
353 {
354 	int rv;
355 
356 	if (flags & DIPROP_PRI_PROM) {
357 		rv = di_prom_prop_lookup_ints(ph, node, prop_name, prop_data);
358 		if (rv < 0)
359 			rv = di_prop_lookup_ints(dev, node, prop_name,
360 			    prop_data);
361 	} else {
362 		rv = di_prop_lookup_ints(dev, node, prop_name, prop_data);
363 		if (rv < 0)
364 			rv = di_prom_prop_lookup_ints(ph, node, prop_name,
365 			    prop_data);
366 	}
367 	return (rv);
368 }
369 
370 
371 static int
372 di_propall_lookup_strings(di_prom_handle_t ph, int flags,
373     dev_t dev, di_node_t node, const char *prop_name, char **prop_data)
374 {
375 	int rv;
376 
377 	if (flags & DIPROP_PRI_PROM) {
378 		rv = di_prom_prop_lookup_strings(ph, node, prop_name,
379 		    prop_data);
380 		if (rv < 0)
381 			rv = di_prop_lookup_strings(dev, node, prop_name,
382 			    prop_data);
383 	} else {
384 		rv = di_prop_lookup_strings(dev, node, prop_name, prop_data);
385 		if (rv < 0)
386 			rv = di_prom_prop_lookup_strings(ph, node, prop_name,
387 			    prop_data);
388 	}
389 	return (rv);
390 }
391 
392 
393 static di_node_t
394 pci_cfg_chassis_node(di_node_t node, di_prom_handle_t ph)
395 {
396 	di_node_t curnode = node;
397 	int *firstchas;
398 
399 	do {
400 		if (di_propall_lookup_ints(ph, 0, DDI_DEV_T_ANY, curnode,
401 		    PROP_FIRST_CHAS, &firstchas) >= 0)
402 			return (curnode);
403 	} while ((curnode = di_parent_node(curnode)) != DI_NODE_NIL);
404 
405 	return (DI_NODE_NIL);
406 }
407 
408 
409 /*
410  * yet another redundant common routine to:
411  * decode the ieee1275 "slot-names" property and returns the string matching
412  * the pci device number <pci_dev>, if any.
413  *
414  * callers must NOT free the returned string
415  *
416  * "slot-names" format: [int][string1][string2]...[stringN]
417  *	- each bit position in [int] represent a pci device number
418  *	- [string1]...[stringN] are concatenated null-terminated strings
419  *	- the number of bits set in [int] == the number of strings that follow
420  *	- each bit that is set corresponds to a string in the following segment
421  */
422 static char *
423 pci_cfg_slotname(di_node_t node, di_prom_handle_t ph, minor_t pci_dev)
424 {
425 #ifdef	DEBUG
426 	char *fnm = "pci_cfg_slotname";
427 #endif
428 	int *snp;
429 	int snlen;
430 	int snentlen = sizeof (int);
431 	int i, max, len, place, curplace;
432 	char *str;
433 
434 	snlen = di_propall_lookup_ints(ph, 0, DDI_DEV_T_ANY, node,
435 	    PROP_SLOT_NAMES, &snp);
436 	if (snlen < 1)
437 		return (NULL);
438 	if ((snp[0] & (1 << pci_dev)) == 0)
439 		return (NULL);
440 
441 	/*
442 	 * pci device number must be less than the amount of bits in the first
443 	 * [int] component of slot-names
444 	 */
445 	if (pci_dev >= snentlen * 8) {
446 		dprint(("%s: pci_dev out of range for %s%d\n",
447 		    fnm, DRVINST(node)));
448 		return (NULL);
449 	}
450 
451 	place = 0;
452 	for (i = 0; i < pci_dev; i++) {
453 		if (snp[0] & (1 << i))
454 			place++;
455 	}
456 
457 	max = (snlen * snentlen) - snentlen;
458 	str = (char *)&snp[1];
459 	i = 0;
460 	curplace = 0;
461 	while (i < max && curplace < place) {
462 		len = strlen(str);
463 		if (len <= 0)
464 			break;
465 		str += len + 1;
466 		i += len + 1;
467 		curplace++;
468 	}
469 	/* the following condition indicates a badly formed slot-names */
470 	if (i >= max || *str == '\0') {
471 		dprint(("%s: badly formed slot-names for %s%d\n",
472 		    fnm, DRVINST(node)));
473 		str = NULL;
474 	}
475 	return (str);
476 }
477 
478 
479 /*
480  * returns non-zero if we can return a valid attachment point name for <node>,
481  * for its slot identified by child pci device number <pci_dev>, through <buf>
482  *
483  * prioritized naming scheme:
484  *	1) <PROP_SLOT_NAMES property>    (see pci_cfg_slotname())
485  *	2) <device-type><PROP_PHYS_SLOT property>
486  *	3) <drv name><drv inst>.<device-type><pci_dev>
487  *
488  * where <device-type> is derived from the PROP_DEV_TYPE property:
489  *	if its value is "pciex" then <device-type> is "pcie"
490  *	else the raw value is used
491  *
492  * if <flags> contains APNODE_DEFNAME, then scheme (3) is used
493  */
494 static int
495 pci_cfg_ap_node(minor_t pci_dev, di_node_t node, di_prom_handle_t ph,
496     char *buf, int bufsz, int flags)
497 {
498 	int *nump;
499 	int rv;
500 	char *str, *devtype;
501 
502 	rv = di_propall_lookup_strings(ph, 0, DDI_DEV_T_ANY, node,
503 	    PROP_DEV_TYPE, &devtype);
504 	if (rv < 1)
505 		return (0);
506 
507 	if (strcmp(devtype, PROPVAL_PCIEX) == 0)
508 		devtype = DEVTYPE_PCIE;
509 
510 	if (flags & APNODE_DEFNAME)
511 		goto DEF;
512 
513 	str = pci_cfg_slotname(node, ph, pci_dev);
514 	if (str != NULL) {
515 		(void) strlcpy(buf, str, bufsz);
516 		return (1);
517 	}
518 
519 	if (di_propall_lookup_ints(ph, 0, DDI_DEV_T_ANY, node, PROP_PHYS_SLOT,
520 	    &nump) > 0) {
521 		if (*nump > 0) {
522 			(void) snprintf(buf, bufsz, "%s%d", devtype, *nump);
523 			return (1);
524 		}
525 	}
526 DEF:
527 	(void) snprintf(buf, bufsz, "%s%d.%s%d",
528 	    di_driver_name(node), di_instance(node), devtype, pci_dev);
529 
530 	return (1);
531 }
532 
533 
534 /*
535  * returns non-zero if we can return a valid expansion chassis name for <node>
536  * through <buf>
537  *
538  * prioritized naming scheme:
539  *	1) <IOB_PRE string><PROP_SERID property: sun specific portion>
540  *	2) <IOB_PRE string><full PROP_SERID property in hex>
541  *	3) <IOB_PRE string>
542  *
543  * PROP_SERID encoding <64-bit int: msb ... lsb>:
544  * <24 bits: vendor id><40 bits: serial number>
545  *
546  * sun encoding of 40 bit serial number:
547  * first byte = device type indicator (ignored in naming scheme)
548  * next 4 bytes = 4 ascii characters
549  */
550 /*ARGSUSED*/
551 static int
552 pci_cfg_iob_name(di_minor_t minor, di_node_t node, di_prom_handle_t ph,
553     char *buf, int bufsz)
554 {
555 	int64_t *seridp;
556 	int64_t serid;
557 	char *idstr;
558 
559 	if (di_prop_lookup_int64(DDI_DEV_T_ANY, node, PROP_SERID,
560 	    &seridp) < 1) {
561 		(void) strlcpy(buf, IOB_PRE, bufsz);
562 		return (1);
563 	}
564 	serid = *seridp;
565 
566 	if (serid >> 40 != VENDID_SUN) {
567 		(void) snprintf(buf, bufsz, "%s%llx", IOB_PRE, serid);
568 		return (1);
569 	}
570 
571 	serid &= SIZE2MASK64(40);
572 	idstr = (char *)&serid;
573 	idstr[sizeof (serid) - 1] = '\0';
574 	/* skip device type indicator */
575 	idstr++;
576 	(void) snprintf(buf, bufsz, "%s%s", IOB_PRE, idstr);
577 	return (1);
578 }
579 
580 
581 /*
582  * returns the pci device number for <node> if found, else returns PCIDEV_NIL
583  */
584 static minor_t
585 pci_cfg_pcidev(di_node_t node, di_prom_handle_t ph)
586 {
587 	int rv;
588 	int *regp;
589 
590 	rv = di_propall_lookup_ints(ph, 0, DDI_DEV_T_ANY, node, PROP_REG,
591 	    &regp);
592 
593 	if (rv < 1) {
594 		dprint(("pci_cfg_pcidev: property %s not found "
595 		    "for %s%d\n", PROP_REG, DRVINST(node)));
596 		return (rv);
597 	}
598 
599 	return (REG_PCIDEV(regp));
600 }
601 
602 
603 /*
604  * returns non-zero when it can successfully return an attachment point
605  * through <ap_path> whose length is less than <ap_pathsz>; returns the full
606  * path of the AP through <pathret> which may be larger than <ap_pathsz>.
607  * Callers need to free <pathret>.  If it cannot return the full path through
608  * <pathret> it will be set to NULL
609  *
610  * The ap path reflects a subset of the device path from an onboard host slot
611  * up to <node>.  We traverse up the device tree starting from <node>, naming
612  * each component using pci_cfg_ap_node().  If we detect that a certain
613  * segment is contained within an expansion chassis, then we skip any bus
614  * nodes in between our current node and the topmost node of the chassis,
615  * which is identified by the PROP_FIRST_CHAS property, and prepend the name
616  * of the expansion chassis as given by pci_cfg_iob_name()
617  *
618  * This scheme is always used for <pathret>.  If however, the size of
619  * <pathret> is greater than <ap_pathsz> then only the default name as given
620  * by pci_cfg_ap_node() for <node> will be used
621  */
622 static int
623 pci_cfg_ap_path(di_minor_t minor, di_node_t node, di_prom_handle_t ph,
624     char *ap_path, int ap_pathsz, char **pathret)
625 {
626 #ifdef	DEBUG
627 	char *fnm = "pci_cfg_ap_path";
628 #endif
629 #define	seplen		(sizeof (AP_PATH_SEP) - 1)
630 #define	iob_pre_len	(sizeof (IOB_PRE) - 1)
631 #define	ap_path_iob_sep_len	(sizeof (AP_PATH_IOB_SEP) - 1)
632 
633 	char *bufptr;
634 	char buf[MAXPATHLEN];
635 	char pathbuf[MAXPATHLEN];
636 	int bufsz;
637 	char *pathptr;
638 	char *pathend = NULL;
639 	int len;
640 	int rv = 0;
641 	int chasflag = 0;
642 	di_node_t curnode = node;
643 	di_node_t chasnode = DI_NODE_NIL;
644 	minor_t pci_dev;
645 
646 	buf[0] = '\0';
647 	pathbuf[0] = '\0';
648 	pathptr = &pathbuf[sizeof (pathbuf) - 1];
649 	*pathptr = '\0';
650 
651 	/*
652 	 * as we traverse up the device tree, we prepend components of our
653 	 * path inside pathbuf, using pathptr and decrementing
654 	 */
655 	pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(di_minor_devt(minor));
656 	do {
657 		bufptr = buf;
658 		bufsz = sizeof (buf);
659 
660 		chasnode = pci_cfg_chassis_node(curnode, ph);
661 		if (chasnode != DI_NODE_NIL) {
662 			rv = pci_cfg_iob_name(minor, chasnode, ph,
663 			    bufptr, bufsz);
664 			if (rv == 0) {
665 				dprint(("%s: cannot create iob name "
666 				    "for %s%d\n", fnm, DRVINST(node)));
667 				*pathptr = '\0';
668 				goto OUT;
669 			}
670 
671 			(void) strncat(bufptr, AP_PATH_IOB_SEP, bufsz);
672 			len = strlen(bufptr);
673 			bufptr += len;
674 			bufsz -= len - 1;
675 
676 			/* set chasflag when the leaf node is within an iob */
677 			if ((curnode == node) != NULL)
678 				chasflag = 1;
679 		}
680 		rv = pci_cfg_ap_node(pci_dev, curnode, ph, bufptr, bufsz, 0);
681 		if (rv == 0) {
682 			dprint(("%s: cannot create ap node name "
683 			    "for %s%d\n", fnm, DRVINST(node)));
684 			*pathptr = '\0';
685 			goto OUT;
686 		}
687 
688 		/*
689 		 * if we can't fit the entire path in our pathbuf, then use
690 		 * the default short name and nullify pathptr; also, since
691 		 * we prepend in the buffer, we must avoid adding a null char
692 		 */
693 		if (curnode != node) {
694 			pathptr -= seplen;
695 			if (pathptr < pathbuf) {
696 				pathptr = pathbuf;
697 				*pathptr = '\0';
698 				goto DEF;
699 			}
700 			(void) memcpy(pathptr, AP_PATH_SEP, seplen);
701 		}
702 		len = strlen(buf);
703 		pathptr -= len;
704 		if (pathptr < pathbuf) {
705 			pathptr = pathbuf;
706 			*pathptr = '\0';
707 			goto DEF;
708 		}
709 		(void) memcpy(pathptr, buf, len);
710 
711 		/* remember the leaf component */
712 		if (curnode == node)
713 			pathend = pathptr;
714 
715 		/*
716 		 * go no further than the hosts' onboard slots
717 		 */
718 		if (chasnode == DI_NODE_NIL)
719 			break;
720 		curnode = chasnode;
721 
722 		/*
723 		 * the pci device number of the current node is used to
724 		 * identify which slot of the parent's bus (next iteration)
725 		 * the current node is on
726 		 */
727 		pci_dev = pci_cfg_pcidev(curnode, ph);
728 		if (pci_dev == PCIDEV_NIL) {
729 			dprint(("%s: cannot obtain pci device number "
730 			    "for %s%d\n", fnm, DRVINST(node)));
731 			*pathptr = '\0';
732 			goto OUT;
733 		}
734 	} while ((curnode = di_parent_node(curnode)) != DI_NODE_NIL);
735 
736 	pathbuf[sizeof (pathbuf) - 1] = '\0';
737 	if (strlen(pathptr) < ap_pathsz) {
738 		(void) strlcpy(ap_path, pathptr, ap_pathsz);
739 		rv = 1;
740 		goto OUT;
741 	}
742 
743 DEF:
744 	/*
745 	 * when our name won't fit <ap_pathsz> we use the endpoint/leaf
746 	 * <node>'s name ONLY IF it has a serialid# which will make the apid
747 	 * globally unique
748 	 */
749 	if (chasflag && pathend != NULL) {
750 		if ((strncmp(pathend + iob_pre_len, AP_PATH_IOB_SEP,
751 		    ap_path_iob_sep_len) != 0) &&
752 		    (strlen(pathend) < ap_pathsz)) {
753 			(void) strlcpy(ap_path, pathend, ap_pathsz);
754 			rv = 1;
755 			goto OUT;
756 		}
757 	}
758 
759 	/*
760 	 * if our name still won't fit <ap_pathsz>, then use the leaf <node>'s
761 	 * default name
762 	 */
763 	rv = pci_cfg_ap_node(pci_dev, node, ph, buf, bufsz, APNODE_DEFNAME);
764 	if (rv == 0) {
765 		dprint(("%s: cannot create default ap node name for %s%d\n",
766 		    fnm, DRVINST(node)));
767 		*pathptr = '\0';
768 		goto OUT;
769 	}
770 	if (strlen(buf) < ap_pathsz) {
771 		(void) strlcpy(ap_path, buf, ap_pathsz);
772 		rv = 1;
773 		goto OUT;
774 	}
775 
776 	/*
777 	 * in this case, cfgadm goes through an expensive process to generate
778 	 * a purely dynamic logical apid: the framework will look through
779 	 * the device tree for attachment point minor nodes and will invoke
780 	 * each plugin responsible for that attachment point class, and if
781 	 * the plugin returns a logical apid that matches the queried apid
782 	 * or matches the default apid generated by the cfgadm framework for
783 	 * that driver/class (occurs when plugin returns an empty logical apid)
784 	 * then that is what it will use
785 	 *
786 	 * it is doubly expensive because the cfgadm pci plugin itself will
787 	 * also search the entire device tree in the absence of a link
788 	 */
789 	rv = 0;
790 	dprint(("%s: cannot create apid for %s%d within length of %d\n",
791 	    fnm, DRVINST(node), ap_pathsz));
792 
793 OUT:
794 	ap_path[ap_pathsz - 1] = '\0';
795 	*pathret = (*pathptr == '\0') ? NULL : strdup(pathptr);
796 	return (rv);
797 
798 #undef	seplen
799 #undef	iob_pre_len
800 #undef	ap_path_iob_sep_len
801 }
802 
803 
804 /*
805  * the PROP_AP_NAMES property contains the first integer section of the
806  * ieee1275 "slot-names" property and functions as a bitmask; see comment for
807  * pci_cfg_slotname()
808  *
809  * we use the name of the attachment point minor node if its pci device
810  * number (encoded in the minor number) is allowed by PROP_AP_NAMES
811  *
812  * returns non-zero if we return a valid attachment point through <path>
813  */
814 static int
815 pci_cfg_ap_legacy(di_minor_t minor, di_node_t node, di_prom_handle_t ph,
816     char *ap_path, int ap_pathsz)
817 {
818 	minor_t pci_dev;
819 	int *anp;
820 
821 	if (di_propall_lookup_ints(ph, 0, DDI_DEV_T_ANY, node, PROP_AP_NAMES,
822 	    &anp) < 1)
823 		return (0);
824 
825 	pci_dev = PCIHP_AP_MINOR_NUM_TO_PCI_DEVNUM(di_minor_devt(minor));
826 	if ((*anp & (1 << pci_dev)) == 0)
827 		return (0);
828 
829 	(void) strlcpy(ap_path, di_minor_name(minor), ap_pathsz);
830 	return (1);
831 }
832 
833 
834 /*
835  * determine if <node> qualifies for a path style apid
836  */
837 static int
838 pci_cfg_is_ap_path(di_node_t node, di_prom_handle_t ph)
839 {
840 	char *devtype;
841 	di_node_t curnode = node;
842 
843 	do {
844 		if (di_propall_lookup_strings(ph, 0, DDI_DEV_T_ANY, curnode,
845 		    PROP_DEV_TYPE, &devtype) > 0)
846 			if (strcmp(devtype, PROPVAL_PCIEX) == 0)
847 				return (1);
848 	} while ((curnode = di_parent_node(curnode)) != DI_NODE_NIL);
849 
850 	return (0);
851 }
852 
853 
854 /*
855  * takes a full path as returned by <pathret> from pci_cfg_ap_path() and
856  * returns an allocated string intendend to be stored in a devlink info (dli)
857  * file
858  *
859  * data format: "Location: <transformed path>"
860  * where <transformed path> is <path> with occurrances of AP_PATH_SEP
861  * replaced by "/"
862  */
863 static char *
864 pci_cfg_info_data(char *path)
865 {
866 #define	head	"Location: "
867 #define	headlen	(sizeof (head) - 1)
868 #define	seplen	(sizeof (AP_PATH_SEP) - 1)
869 
870 	char *sep, *prev, *np;
871 	char *newpath;
872 	int pathlen = strlen(path);
873 	int len;
874 
875 	newpath = malloc(sizeof (char) * (headlen + pathlen + 1));
876 	np = newpath;
877 	(void) strcpy(np, head);
878 	np += headlen;
879 
880 	prev = path;
881 	while ((sep = strstr(prev, AP_PATH_SEP)) != NULL) {
882 		len = sep - prev;
883 		(void) memcpy(np, prev, len);
884 		np += len;
885 		*np++ = '/';
886 		prev = sep + seplen;
887 	}
888 	(void) strcpy(np, prev);
889 	return (newpath);
890 
891 #undef	head
892 #undef	headlen
893 #undef	seplen
894 }
895 
896 
897 static void
898 pci_cfg_rm_link(char *file)
899 {
900 	char *dlipath;
901 
902 	dlipath = di_dli_name(file);
903 	(void) unlink(dlipath);
904 
905 	devfsadm_rm_all(file);
906 	free(dlipath);
907 }
908 
909 /*
910  * removes all registered devlinks to physical path <physpath> except for
911  * the devlink <valid> if not NULL;
912  * <physpath> must include the minor node
913  */
914 static void
915 pci_cfg_rm_invalid_links(char *physpath, char *valid)
916 {
917 	char **dnp;
918 	char *cp, *vcp;
919 	int i, dnlen;
920 
921 	dnp = devfsadm_lookup_dev_names(physpath, NULL, &dnlen);
922 	if (dnp == NULL)
923 		return;
924 
925 	if (valid != NULL) {
926 		if (strncmp(valid, DEV "/", DEV_LEN + 1) == 0)
927 			vcp = valid + DEV_LEN + 1;
928 		else
929 			vcp = valid;
930 	}
931 
932 	for (i = 0; i < dnlen; i++) {
933 		if (strncmp(dnp[i], DEV "/", DEV_LEN + 1) == 0)
934 			cp = dnp[i] + DEV_LEN + 1;
935 		else
936 			cp = dnp[i];
937 
938 		if (valid != NULL) {
939 			if (strcmp(vcp, cp) == 0)
940 				continue;
941 		}
942 		pci_cfg_rm_link(cp);
943 	}
944 	devfsadm_free_dev_names(dnp, dnlen);
945 }
946 
947 
948 /*
949  * takes a complete devinfo snapshot and returns the root node;
950  * callers must do a di_fini() on the returned node;
951  * if the snapshot failed, DI_NODE_NIL is returned instead
952  *
953  * if <pci_node> is not DI_NODE_NIL, it will search for the same devinfo node
954  * in the new snapshot and return it through <ret_node> if it is found,
955  * else DI_NODE_NIL is returned instead
956  *
957  * in addition, if <pci_minor> is not DI_MINOR_NIL, it will also return
958  * the matching minor in the new snapshot through <ret_minor> if it is found,
959  * else DI_MINOR_NIL is returned instead
960  */
961 static di_node_t
962 pci_cfg_snapshot(di_node_t pci_node, di_minor_t pci_minor,
963     di_node_t *ret_node, di_minor_t *ret_minor)
964 {
965 	di_node_t root_node;
966 	di_node_t node;
967 	di_minor_t minor;
968 	int pci_inst;
969 	dev_t pci_devt;
970 
971 	*ret_node = DI_NODE_NIL;
972 	*ret_minor = DI_MINOR_NIL;
973 
974 	root_node = di_init("/", DINFOCPYALL);
975 	if (root_node == DI_NODE_NIL)
976 		return (DI_NODE_NIL);
977 
978 	/*
979 	 * narrow down search by driver, then instance, then minor
980 	 */
981 	if (pci_node == DI_NODE_NIL)
982 		return (root_node);
983 
984 	pci_inst = di_instance(pci_node);
985 	node = di_drv_first_node(di_driver_name(pci_node), root_node);
986 	do {
987 		if (pci_inst == di_instance(node)) {
988 			*ret_node = node;
989 			break;
990 		}
991 	} while ((node = di_drv_next_node(node)) != DI_NODE_NIL);
992 
993 	if (node == DI_NODE_NIL)
994 		return (root_node);
995 
996 	/*
997 	 * found node, now search minors
998 	 */
999 	if (pci_minor == DI_MINOR_NIL)
1000 		return (root_node);
1001 
1002 	pci_devt = di_minor_devt(pci_minor);
1003 	minor = DI_MINOR_NIL;
1004 	while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
1005 		if (pci_devt == di_minor_devt(minor)) {
1006 			*ret_minor = minor;
1007 			break;
1008 		}
1009 	}
1010 	return (root_node);
1011 }
1012 
1013 
1014 static int
1015 pci_cfg_creat_cb(di_minor_t pci_minor, di_node_t pci_node)
1016 {
1017 #ifdef	DEBUG
1018 	char *fnm = "pci_cfg_creat_cb";
1019 #endif
1020 #define	ap_pathsz	(sizeof (ap_path))
1021 
1022 	char ap_path[CFGA_LOG_EXT_LEN];
1023 	char linkbuf[MAXPATHLEN];
1024 	char *fullpath = NULL;
1025 	char *pathinfo = NULL;
1026 	char *devpath = NULL;
1027 	int rv, fd = -1;
1028 	size_t sz;
1029 	di_prom_handle_t ph;
1030 	di_node_t node;
1031 	di_node_t root_node = DI_NODE_NIL;
1032 	di_minor_t minor;
1033 
1034 	ph = di_prom_init();
1035 	if (ph == DI_PROM_HANDLE_NIL) {
1036 		dprint(("%s: di_prom_init() failed for %s%d\n",
1037 		    fnm, DRVINST(pci_node)));
1038 		goto OUT;
1039 	}
1040 
1041 	/*
1042 	 * Since incoming nodes from hotplug events are from snapshots that
1043 	 * do NOT contain parent/ancestor data, we must retake our own
1044 	 * snapshot and search for the target node
1045 	 */
1046 	root_node = pci_cfg_snapshot(pci_node, pci_minor, &node, &minor);
1047 	if (root_node == DI_NODE_NIL || node == DI_NODE_NIL ||
1048 	    minor == DI_MINOR_NIL) {
1049 		dprint(("%s: devinfo snapshot or search failed for %s%d\n",
1050 		    fnm, DRVINST(pci_node)));
1051 		goto OUT;
1052 	}
1053 
1054 	if (pci_cfg_is_ap_path(node, ph)) {
1055 		rv = pci_cfg_ap_path(minor, node, ph, ap_path, ap_pathsz,
1056 		    &fullpath);
1057 		if (rv == 0)
1058 			goto OUT;
1059 
1060 		(void) snprintf(linkbuf, sizeof (linkbuf), "%s/%s",
1061 		    CFG_DIRNAME, ap_path);
1062 
1063 		/*
1064 		 * We must remove existing links because we may have invalid
1065 		 * apids that are valid links.  Since these are not dangling,
1066 		 * devfsadm will not invoke the remove callback on them.
1067 		 *
1068 		 * What are "invalid apids with valid links"?  Consider swapping
1069 		 * an attachment point bus with another while the system is
1070 		 * down, on the same device path bound to the same drivers
1071 		 * but with the new AP bus having different properties
1072 		 * (e.g. serialid#).  If the previous apid is not removed,
1073 		 * there will now be two different links pointing to the same
1074 		 * attachment point, but only one reflects the correct
1075 		 * logical apid
1076 		 */
1077 		devpath = pci_cfg_devpath(node, minor);
1078 		if (devpath == NULL)
1079 			goto OUT;
1080 		pci_cfg_rm_invalid_links(devpath, linkbuf);
1081 		free(devpath);
1082 
1083 		(void) devfsadm_mklink(linkbuf, node, minor, 0);
1084 
1085 		/*
1086 		 * we store the full logical path of the attachment point for
1087 		 * cfgadm to display in its info field which is useful when
1088 		 * the full logical path exceeds the size limit for logical
1089 		 * apids (CFGA_LOG_EXT_LEN)
1090 		 *
1091 		 * for the cfgadm pci plugin to do the same would be expensive
1092 		 * (i.e. devinfo snapshot + top down exhaustive minor search +
1093 		 * equivalent of pci_cfg_ap_path() on every invocation)
1094 		 *
1095 		 * note that if we do not create a link (pci_cfg_ap_path() is
1096 		 * not successful), that is what cfgadm will do anyways to
1097 		 * create a purely dynamic apid
1098 		 */
1099 		pathinfo = pci_cfg_info_data(fullpath);
1100 		fd = di_dli_openw(linkbuf);
1101 		if (fd < 0)
1102 			goto OUT;
1103 
1104 		sz = strlen(pathinfo) + 1;
1105 		rv = write(fd, pathinfo, sz);
1106 		if (rv < sz) {
1107 			dprint(("%s: could not write full pathinfo to dli "
1108 			    "file for %s%d\n", fnm, DRVINST(node)));
1109 			goto OUT;
1110 		}
1111 		di_dli_close(fd);
1112 	} else {
1113 		rv = pci_cfg_ap_legacy(minor, node, ph, ap_path,
1114 		    ap_pathsz);
1115 		if (rv == 0)
1116 			goto OUT;
1117 
1118 		(void) snprintf(linkbuf, sizeof (linkbuf), "%s/%s",
1119 		    CFG_DIRNAME, ap_path);
1120 		(void) devfsadm_mklink(linkbuf, node, minor, 0);
1121 	}
1122 
1123 OUT:
1124 	if (fd >= 0)
1125 		di_dli_close(fd);
1126 	if (fullpath != NULL)
1127 		free(fullpath);
1128 	if (pathinfo != NULL)
1129 		free(pathinfo);
1130 	if (ph != DI_PROM_HANDLE_NIL)
1131 		di_prom_fini(ph);
1132 	if (root_node != DI_NODE_NIL)
1133 		di_fini(root_node);
1134 	return (DEVFSADM_CONTINUE);
1135 
1136 #undef	ap_pathsz
1137 }
1138 
1139 
1140 static void
1141 pci_cfg_rm_all(char *file)
1142 {
1143 	pci_cfg_rm_link(file);
1144 }
1145 
1146 
1147 /*
1148  * ib_cfg_creat_cb() creates two types of links
1149  * One for the fabric as /dev/cfg/ib
1150  * Another for each HCA seen in the fabric as /dev/cfg/hca:<HCA-GUID>
1151  */
1152 static int
1153 ib_cfg_creat_cb(di_minor_t minor, di_node_t node)
1154 {
1155 	char	*cp;
1156 	char	path[PATH_MAX + 1];
1157 
1158 	if ((cp = di_devfs_path(node)) == NULL) {
1159 		return (DEVFSADM_CONTINUE);
1160 	}
1161 
1162 	(void) snprintf(path, sizeof (path), "%s:%s", cp, di_minor_name(minor));
1163 	di_devfs_path_free(cp);
1164 
1165 	/* create fabric or hca:GUID and the symlink */
1166 	if (strstr(path, "ib:fabric") != NULL) {
1167 		(void) snprintf(path, sizeof (path), "%s/ib", CFG_DIRNAME);
1168 	} else {
1169 		(void) snprintf(path, sizeof (path), "%s/hca:%s", CFG_DIRNAME,
1170 		    di_minor_name(minor));
1171 	}
1172 
1173 	(void) devfsadm_mklink(path, node, minor, 0);
1174 	return (DEVFSADM_CONTINUE);
1175 }
1176