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