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.
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.
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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
32 #include <sys/int_fmtio.h>
33 #include <sys/scsi/scsi_address.h>
34 #include <sys/libdevid.h>
36 #define SGEN_LINK_RE "^scsi/.+/c[0-9]+t[0-9A-F]+d[0-9]+$"
37 #define SGEN_DIR "scsi"
38 #define SGEN_CLASS "generic-scsi"
40 static int sgen_callback(di_minor_t minor
, di_node_t node
);
41 static char *find_ctrlr(di_node_t node
, di_minor_t minor
);
44 static devfsadm_create_t sgen_create_cbt
[] = {
45 { SGEN_CLASS
, "ddi_generic:scsi", NULL
,
46 TYPE_EXACT
| CREATE_DEFER
, ILEVEL_0
, sgen_callback
50 DEVFSADM_CREATE_INIT_V0(sgen_create_cbt
);
53 * HOT auto cleanup of sgen links not desired.
55 static devfsadm_remove_t sgen_remove_cbt
[] = {
56 { SGEN_CLASS
, SGEN_LINK_RE
, RM_POST
,
57 ILEVEL_0
, devfsadm_rm_all
61 DEVFSADM_REMOVE_INIT_V0(sgen_remove_cbt
);
64 sgen_callback(di_minor_t minor
, di_node_t node
)
66 char *baddr
, *cnum
, *tstr
;
67 char lpath
[PATH_MAX
], buf
[PATH_MAX
];
72 if ((cnum
= find_ctrlr(node
, minor
)) == NULL
)
76 * SCSAv3 attached devices.
78 if (di_prop_lookup_strings(DDI_DEV_T_ANY
, node
,
79 SCSI_ADDR_PROP_TARGET_PORT
, &tgt_port
) > 0) {
88 /* Get lun property */
89 if ((di_prop_lookup_int64(DDI_DEV_T_ANY
, node
,
90 SCSI_ADDR_PROP_LUN64
, &lun64p
) > 0) &&
91 (*lun64p
!= SCSI_LUN64_ILLEGAL
)) {
93 } else if (di_prop_lookup_ints(DDI_DEV_T_ANY
, node
,
94 SCSI_ADDR_PROP_LUN
, &intp
) > 0) {
95 lun64
= (uint64_t)*intp
;
98 lun
= scsi_lun64_to_lun(lun64
);
100 addr_method
= (lun
.sl_lun1_msb
& SCSI_LUN_AM_MASK
);
102 (void) scsi_wwnstr_to_wwn(tgt_port
, &wwn
);
103 if ((addr_method
== SCSI_LUN_AM_PDEV
) &&
104 (lun
.sl_lun2_msb
== 0) && (lun
.sl_lun2_lsb
== 0) &&
105 (lun
.sl_lun3_msb
== 0) && (lun
.sl_lun3_lsb
== 0) &&
106 (lun
.sl_lun4_msb
== 0) && (lun
.sl_lun4_lsb
== 0)) {
107 (void) snprintf(lpath
, PATH_MAX
,
108 "%s/%s/c%st%"PRIX64
"d%"PRId64
, SGEN_DIR
,
109 di_minor_name(minor
), cnum
, wwn
, lun64
);
110 } else if ((addr_method
== SCSI_LUN_AM_FLAT
) &&
111 (lun
.sl_lun2_msb
== 0) && (lun
.sl_lun2_lsb
== 0) &&
112 (lun
.sl_lun3_msb
== 0) && (lun
.sl_lun3_lsb
== 0) &&
113 (lun
.sl_lun4_msb
== 0) && (lun
.sl_lun4_lsb
== 0)) {
114 sl
= (lun
.sl_lun1_msb
<< 8) | lun
.sl_lun1_lsb
;
115 (void) snprintf(lpath
, PATH_MAX
,
116 "%s/%s/c%st%"PRIX64
"d%"PRIX16
, SGEN_DIR
,
117 di_minor_name(minor
), cnum
, wwn
, sl
);
119 (void) snprintf(lpath
, PATH_MAX
,
120 "%s/%s/c%st%"PRIX64
"d%"PRIX64
, SGEN_DIR
,
121 di_minor_name(minor
), cnum
, wwn
, lun64
);
123 } else if (di_prop_lookup_strings(DDI_DEV_T_ANY
, node
,
124 "client-guid", (char **)&wwnstr
) > 0) {
126 * MPXIO-enabled devices; lun is always 0.
128 if (strlcpy((char *)buf
, (char *)wwnstr
, sizeof (buf
)) >=
132 for (tstr
= buf
; *tstr
!= '\0'; tstr
++) {
133 *tstr
= toupper(*tstr
);
135 if (snprintf(lpath
, sizeof (lpath
), "%s/%s/c%st%sd0", SGEN_DIR
,
136 di_minor_name(minor
), cnum
, buf
) >= sizeof (lpath
))
139 } else if (di_prop_lookup_bytes(DDI_DEV_T_ANY
, node
,
140 "port-wwn", &wwnstr
) == 8) {
142 * "normal" fibre channel devices
144 int lun
, *lunp
, count
;
145 if (di_prop_lookup_ints(DDI_DEV_T_ANY
, node
, "lun", &lunp
) > 0)
150 for (count
= 0, tstr
= buf
; count
< 8; count
++, tstr
+= 2)
151 (void) sprintf(tstr
, "%02X", wwnstr
[count
]);
154 if (snprintf(lpath
, sizeof (lpath
), "%s/%s/c%st%sd%d", SGEN_DIR
,
155 di_minor_name(minor
), cnum
, buf
, lun
) >= sizeof (lpath
))
159 * Parallel SCSI devices
163 if ((baddr
= di_bus_addr(node
)) == NULL
)
166 if (sscanf(baddr
, "%X,%X", &targ
, &lun
) != 2)
169 if (snprintf(lpath
, sizeof (lpath
), "%s/%s/c%st%dd%d", SGEN_DIR
,
170 di_minor_name(minor
), cnum
, targ
, lun
) >= sizeof (lpath
))
174 (void) devfsadm_mklink(lpath
, node
, minor
, 0);
177 return (DEVFSADM_CONTINUE
);
180 /* index of enumeration rule applicable to this module */
184 find_ctrlr(di_node_t node
, di_minor_t minor
)
186 char path
[PATH_MAX
+ 1];
190 devfsadm_enumerate_t rules
[3] = {
191 {"^r?dsk$/^c([0-9]+)", 1, MATCH_PARENT
},
192 {"^cfg$/^c([0-9]+)$", 1, MATCH_ADDR
},
193 {"^scsi$/^.+$/^c([0-9]+)", 1, MATCH_PARENT
}
196 mn
= di_minor_name(minor
);
198 if ((devfspath
= di_devfs_path(node
)) == NULL
) {
201 (void) strcpy(path
, devfspath
);
202 (void) strcat(path
, ":");
203 (void) strcat(path
, mn
);
204 di_devfs_path_free(devfspath
);
207 * Use controller (parent) component of device path
209 if (disk_enumerate_int(path
, RULE_INDEX
, &buf
, rules
, 3) ==
213 * We failed because there are multiple logical controller
214 * numbers for a single physical controller. If we use node
215 * name also for DEVICE paths in the match it should fix this
216 * and only find one logical controller. (See 4045879).
217 * NOTE: Rules for controllers are not changed, as there is
218 * no unique controller number for them in this case.
220 * MATCH_UNCACHED flag is private to the "disks" and "sgen"
221 * modules. NOT to be used by other modules.
223 rules
[0].flags
= MATCH_NODE
| MATCH_UNCACHED
; /* disks */
224 rules
[2].flags
= MATCH_NODE
| MATCH_UNCACHED
; /* generic scsi */
225 if (devfsadm_enumerate_int(path
, RULE_INDEX
, &buf
, rules
, 3)) {