xref: /titanic_53/usr/src/cmd/devfsadm/sgen_link.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <devfsadm.h>
30*7c478bd9Sstevel@tonic-gate #include <stdio.h>
31*7c478bd9Sstevel@tonic-gate #include <strings.h>
32*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
33*7c478bd9Sstevel@tonic-gate #include <limits.h>
34*7c478bd9Sstevel@tonic-gate #include <ctype.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #define	SGEN_LINK_RE	"^scsi/.+/c[0-9]+t[0-9A-F]+d[0-9]+$"
37*7c478bd9Sstevel@tonic-gate #define	SGEN_DIR	"scsi"
38*7c478bd9Sstevel@tonic-gate #define	SGEN_CLASS	"generic-scsi"
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate static int sgen_callback(di_minor_t minor, di_node_t node);
41*7c478bd9Sstevel@tonic-gate static char *find_ctrlr(di_node_t node, di_minor_t minor);
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate static devfsadm_create_t sgen_create_cbt[] = {
45*7c478bd9Sstevel@tonic-gate 	{ SGEN_CLASS, "ddi_generic:scsi", NULL,
46*7c478bd9Sstevel@tonic-gate 	    TYPE_EXACT | CREATE_DEFER, ILEVEL_0, sgen_callback
47*7c478bd9Sstevel@tonic-gate 	}
48*7c478bd9Sstevel@tonic-gate };
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate DEVFSADM_CREATE_INIT_V0(sgen_create_cbt);
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate /*
53*7c478bd9Sstevel@tonic-gate  * HOT auto cleanup of sgen links not desired.
54*7c478bd9Sstevel@tonic-gate  */
55*7c478bd9Sstevel@tonic-gate static devfsadm_remove_t sgen_remove_cbt[] = {
56*7c478bd9Sstevel@tonic-gate 	{ SGEN_CLASS, SGEN_LINK_RE, RM_POST,
57*7c478bd9Sstevel@tonic-gate 		ILEVEL_0, devfsadm_rm_all
58*7c478bd9Sstevel@tonic-gate 	}
59*7c478bd9Sstevel@tonic-gate };
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate DEVFSADM_REMOVE_INIT_V0(sgen_remove_cbt);
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate static int
64*7c478bd9Sstevel@tonic-gate sgen_callback(di_minor_t minor, di_node_t node)
65*7c478bd9Sstevel@tonic-gate {
66*7c478bd9Sstevel@tonic-gate 	char *baddr, *cnum, *tstr;
67*7c478bd9Sstevel@tonic-gate 	char lpath[PATH_MAX], buf[PATH_MAX];
68*7c478bd9Sstevel@tonic-gate 	uchar_t *wwn;
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	if ((cnum = find_ctrlr(node, minor)) == NULL)
71*7c478bd9Sstevel@tonic-gate 		goto done;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node,
74*7c478bd9Sstevel@tonic-gate 	    "client-guid", (char **)&wwn) > 0) {
75*7c478bd9Sstevel@tonic-gate 		/*
76*7c478bd9Sstevel@tonic-gate 		 * MPXIO-enabled devices; lun is always 0.
77*7c478bd9Sstevel@tonic-gate 		 */
78*7c478bd9Sstevel@tonic-gate 		if (strlcpy((char *)buf, (char *)wwn, sizeof (buf)) >=
79*7c478bd9Sstevel@tonic-gate 		    sizeof (buf))
80*7c478bd9Sstevel@tonic-gate 			goto done;
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 		for (tstr = buf; *tstr != '\0'; tstr++) {
83*7c478bd9Sstevel@tonic-gate 			*tstr = toupper(*tstr);
84*7c478bd9Sstevel@tonic-gate 		}
85*7c478bd9Sstevel@tonic-gate 		if (snprintf(lpath, sizeof (lpath), "%s/%s/c%st%sd0", SGEN_DIR,
86*7c478bd9Sstevel@tonic-gate 		    di_minor_name(minor), cnum, buf) >= sizeof (lpath))
87*7c478bd9Sstevel@tonic-gate 			goto done;
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	} else if (di_prop_lookup_bytes(DDI_DEV_T_ANY, node,
90*7c478bd9Sstevel@tonic-gate 	    "port-wwn", &wwn) == 8) {
91*7c478bd9Sstevel@tonic-gate 		/*
92*7c478bd9Sstevel@tonic-gate 		 * "normal" fibre channel devices
93*7c478bd9Sstevel@tonic-gate 		 */
94*7c478bd9Sstevel@tonic-gate 		int lun, *lunp, count;
95*7c478bd9Sstevel@tonic-gate 		if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "lun", &lunp) > 0)
96*7c478bd9Sstevel@tonic-gate 			lun = *lunp;
97*7c478bd9Sstevel@tonic-gate 		else
98*7c478bd9Sstevel@tonic-gate 			lun = 0;
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 		for (count = 0, tstr = buf; count < 8; count++, tstr += 2)
101*7c478bd9Sstevel@tonic-gate 			(void) sprintf(tstr, "%02X", wwn[count]);
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 		*tstr = '\0';
104*7c478bd9Sstevel@tonic-gate 		if (snprintf(lpath, sizeof (lpath), "%s/%s/c%st%sd%d", SGEN_DIR,
105*7c478bd9Sstevel@tonic-gate 		    di_minor_name(minor), cnum, buf, lun) >= sizeof (lpath))
106*7c478bd9Sstevel@tonic-gate 			goto done;
107*7c478bd9Sstevel@tonic-gate 	} else {
108*7c478bd9Sstevel@tonic-gate 		/*
109*7c478bd9Sstevel@tonic-gate 		 * Parallel SCSI devices
110*7c478bd9Sstevel@tonic-gate 		 */
111*7c478bd9Sstevel@tonic-gate 		uint_t targ, lun;
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 		if ((baddr = di_bus_addr(node)) == NULL)
114*7c478bd9Sstevel@tonic-gate 			goto done;
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 		if (sscanf(baddr, "%X,%X", &targ, &lun) != 2)
117*7c478bd9Sstevel@tonic-gate 			goto done;
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 		if (snprintf(lpath, sizeof (lpath), "%s/%s/c%st%dd%d", SGEN_DIR,
120*7c478bd9Sstevel@tonic-gate 		    di_minor_name(minor), cnum, targ, lun) >= sizeof (lpath))
121*7c478bd9Sstevel@tonic-gate 			goto done;
122*7c478bd9Sstevel@tonic-gate 	}
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	(void) devfsadm_mklink(lpath, node, minor, 0);
125*7c478bd9Sstevel@tonic-gate done:
126*7c478bd9Sstevel@tonic-gate 	free(cnum);
127*7c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
128*7c478bd9Sstevel@tonic-gate }
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate /* index of enumeration rule applicable to this module */
131*7c478bd9Sstevel@tonic-gate #define	RULE_INDEX	2
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate static char *
134*7c478bd9Sstevel@tonic-gate find_ctrlr(di_node_t node, di_minor_t minor)
135*7c478bd9Sstevel@tonic-gate {
136*7c478bd9Sstevel@tonic-gate 	char path[PATH_MAX + 1];
137*7c478bd9Sstevel@tonic-gate 	char *devfspath;
138*7c478bd9Sstevel@tonic-gate 	char *buf, *mn;
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	devfsadm_enumerate_t rules[3] = {
141*7c478bd9Sstevel@tonic-gate 	    {"^r?dsk$/^c([0-9]+)", 1, MATCH_PARENT},
142*7c478bd9Sstevel@tonic-gate 	    {"^cfg$/^c([0-9]+)$", 1, MATCH_ADDR},
143*7c478bd9Sstevel@tonic-gate 	    {"^scsi$/^.+$/^c([0-9]+)", 1, MATCH_PARENT}
144*7c478bd9Sstevel@tonic-gate 	};
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	mn = di_minor_name(minor);
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	if ((devfspath = di_devfs_path(node)) == NULL) {
149*7c478bd9Sstevel@tonic-gate 		return (NULL);
150*7c478bd9Sstevel@tonic-gate 	}
151*7c478bd9Sstevel@tonic-gate 	(void) strcpy(path, devfspath);
152*7c478bd9Sstevel@tonic-gate 	(void) strcat(path, ":");
153*7c478bd9Sstevel@tonic-gate 	(void) strcat(path, mn);
154*7c478bd9Sstevel@tonic-gate 	di_devfs_path_free(devfspath);
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	/*
157*7c478bd9Sstevel@tonic-gate 	 * Use controller (parent) component of device path
158*7c478bd9Sstevel@tonic-gate 	 */
159*7c478bd9Sstevel@tonic-gate 	if (disk_enumerate_int(path, RULE_INDEX, &buf, rules, 3) ==
160*7c478bd9Sstevel@tonic-gate 	    DEVFSADM_MULTIPLE) {
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 		/*
163*7c478bd9Sstevel@tonic-gate 		 * We failed because there are multiple logical controller
164*7c478bd9Sstevel@tonic-gate 		 * numbers for a single physical controller.  If we use node
165*7c478bd9Sstevel@tonic-gate 		 * name also for DEVICE paths in the match it should fix this
166*7c478bd9Sstevel@tonic-gate 		 * and only find one logical controller. (See 4045879).
167*7c478bd9Sstevel@tonic-gate 		 * NOTE: Rules for controllers are not changed, as there is
168*7c478bd9Sstevel@tonic-gate 		 * no unique controller number for them in this case.
169*7c478bd9Sstevel@tonic-gate 		 *
170*7c478bd9Sstevel@tonic-gate 		 * MATCH_UNCACHED flag is private to the "disks" and "sgen"
171*7c478bd9Sstevel@tonic-gate 		 * modules. NOT to be used by other modules.
172*7c478bd9Sstevel@tonic-gate 		 */
173*7c478bd9Sstevel@tonic-gate 		rules[0].flags = MATCH_NODE | MATCH_UNCACHED; /* disks */
174*7c478bd9Sstevel@tonic-gate 		rules[2].flags = MATCH_NODE | MATCH_UNCACHED; /* generic scsi */
175*7c478bd9Sstevel@tonic-gate 		if (devfsadm_enumerate_int(path, RULE_INDEX, &buf, rules, 3)) {
176*7c478bd9Sstevel@tonic-gate 			return (NULL);
177*7c478bd9Sstevel@tonic-gate 		}
178*7c478bd9Sstevel@tonic-gate 	}
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	return (buf);
181*7c478bd9Sstevel@tonic-gate }
182