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 2016 Nexenta Systems, Inc. All rights reserved.
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
33 #include <sys/int_fmtio.h>
34 #include <sys/scsi/scsi_address.h>
35 #include <sys/libdevid.h>
37 #define SGEN_LINK_RE "^scsi/.+/c[0-9]+t[0-9A-F]+d[0-9]+$"
38 #define SGEN_DIR "scsi"
39 #define SGEN_CLASS "generic-scsi"
41 static int sgen_callback(di_minor_t minor
, di_node_t node
);
42 static char *find_ctrlr(di_node_t node
, di_minor_t minor
);
45 static devfsadm_create_t sgen_create_cbt
[] = {
46 { SGEN_CLASS
, "ddi_generic:scsi", NULL
,
47 TYPE_EXACT
| CREATE_DEFER
, ILEVEL_0
, sgen_callback
51 DEVFSADM_CREATE_INIT_V0(sgen_create_cbt
);
54 * HOT auto cleanup of sgen links not desired.
56 static devfsadm_remove_t sgen_remove_cbt
[] = {
57 { SGEN_CLASS
, SGEN_LINK_RE
, RM_POST
,
58 ILEVEL_0
, devfsadm_rm_all
62 DEVFSADM_REMOVE_INIT_V0(sgen_remove_cbt
);
65 sgen_callback(di_minor_t minor
, di_node_t node
)
67 char *baddr
, *cnum
, *tstr
;
68 char lpath
[PATH_MAX
], buf
[PATH_MAX
];
73 if ((cnum
= find_ctrlr(node
, minor
)) == NULL
)
77 * SCSAv3 attached devices.
79 if (di_prop_lookup_strings(DDI_DEV_T_ANY
, node
,
80 SCSI_ADDR_PROP_TARGET_PORT
, &tgt_port
) > 0) {
89 /* Get lun property */
90 if ((di_prop_lookup_int64(DDI_DEV_T_ANY
, node
,
91 SCSI_ADDR_PROP_LUN64
, &lun64p
) > 0) &&
92 (*lun64p
!= SCSI_LUN64_ILLEGAL
)) {
94 } else if (di_prop_lookup_ints(DDI_DEV_T_ANY
, node
,
95 SCSI_ADDR_PROP_LUN
, &intp
) > 0) {
96 lun64
= (uint64_t)*intp
;
99 lun
= scsi_lun64_to_lun(lun64
);
101 addr_method
= (lun
.sl_lun1_msb
& SCSI_LUN_AM_MASK
);
103 (void) scsi_wwnstr_to_wwn(tgt_port
, &wwn
);
104 if ((addr_method
== SCSI_LUN_AM_PDEV
) &&
105 (lun
.sl_lun2_msb
== 0) && (lun
.sl_lun2_lsb
== 0) &&
106 (lun
.sl_lun3_msb
== 0) && (lun
.sl_lun3_lsb
== 0) &&
107 (lun
.sl_lun4_msb
== 0) && (lun
.sl_lun4_lsb
== 0)) {
108 (void) snprintf(lpath
, PATH_MAX
,
109 "%s/%s/c%st%"PRIX64
"d%"PRId64
, SGEN_DIR
,
110 di_minor_name(minor
), cnum
, wwn
, lun64
);
111 } else if ((addr_method
== SCSI_LUN_AM_FLAT
) &&
112 (lun
.sl_lun2_msb
== 0) && (lun
.sl_lun2_lsb
== 0) &&
113 (lun
.sl_lun3_msb
== 0) && (lun
.sl_lun3_lsb
== 0) &&
114 (lun
.sl_lun4_msb
== 0) && (lun
.sl_lun4_lsb
== 0)) {
115 sl
= (lun
.sl_lun1_msb
<< 8) | lun
.sl_lun1_lsb
;
116 (void) snprintf(lpath
, PATH_MAX
,
117 "%s/%s/c%st%"PRIX64
"d%"PRIX16
, SGEN_DIR
,
118 di_minor_name(minor
), cnum
, wwn
, sl
);
120 (void) snprintf(lpath
, PATH_MAX
,
121 "%s/%s/c%st%"PRIX64
"d%"PRIX64
, SGEN_DIR
,
122 di_minor_name(minor
), cnum
, wwn
, lun64
);
124 } else if (di_prop_lookup_strings(DDI_DEV_T_ANY
, node
,
125 "client-guid", (char **)&wwnstr
) > 0) {
127 * MPXIO-enabled devices; lun is always 0.
129 if (strlcpy((char *)buf
, (char *)wwnstr
, sizeof (buf
)) >=
133 for (tstr
= buf
; *tstr
!= '\0'; tstr
++) {
134 *tstr
= toupper(*tstr
);
136 if (snprintf(lpath
, sizeof (lpath
), "%s/%s/c%st%sd0", SGEN_DIR
,
137 di_minor_name(minor
), cnum
, buf
) >= sizeof (lpath
))
140 } else if (di_prop_lookup_bytes(DDI_DEV_T_ANY
, node
,
141 "port-wwn", &wwnstr
) == 8) {
143 * "normal" fibre channel devices
145 int lun
, *lunp
, count
;
146 if (di_prop_lookup_ints(DDI_DEV_T_ANY
, node
, "lun", &lunp
) > 0)
151 for (count
= 0, tstr
= buf
; count
< 8; count
++, tstr
+= 2)
152 (void) sprintf(tstr
, "%02X", wwnstr
[count
]);
155 if (snprintf(lpath
, sizeof (lpath
), "%s/%s/c%st%sd%d", SGEN_DIR
,
156 di_minor_name(minor
), cnum
, buf
, lun
) >= sizeof (lpath
))
160 * Parallel SCSI devices
164 if ((baddr
= di_bus_addr(node
)) == NULL
)
167 if (sscanf(baddr
, "%X,%X", &targ
, &lun
) != 2)
170 if (snprintf(lpath
, sizeof (lpath
), "%s/%s/c%st%dd%d", SGEN_DIR
,
171 di_minor_name(minor
), cnum
, targ
, lun
) >= sizeof (lpath
))
175 (void) devfsadm_mklink(lpath
, node
, minor
, 0);
178 return (DEVFSADM_CONTINUE
);
181 /* index of enumeration rule applicable to this module */
185 find_ctrlr(di_node_t node
, di_minor_t minor
)
187 char path
[PATH_MAX
+ 1];
192 devfsadm_enumerate_t rules
[3] = {
193 {"^r?dsk$/^c([0-9]+)", 1, MATCH_PARENT
},
194 {"^cfg$/^c([0-9]+)$", 1, MATCH_ADDR
},
195 {"^scsi$/^.+$/^c([0-9]+)", 1, MATCH_PARENT
}
198 mn
= di_minor_name(minor
);
200 if ((devfspath
= di_devfs_path(node
)) == NULL
) {
203 (void) strcpy(path
, devfspath
);
204 (void) strcat(path
, ":");
205 (void) strcat(path
, mn
);
206 di_devfs_path_free(devfspath
);
209 * Use controller (parent) component of device path
211 is_vhci
= (strncmp(path
, "/scsi_vhci/", 11) == 0);
213 if (ctrl_enumerate_int(path
, RULE_INDEX
, &buf
, rules
, 3, 1, is_vhci
) ==
216 * We failed because there are multiple logical controller
217 * numbers for a single physical controller. If we use node
218 * name also for DEVICE paths in the match it should fix this
219 * and only find one logical controller. (See 4045879).
220 * NOTE: Rules for controllers are not changed, as there is
221 * no unique controller number for them in this case.
223 * MATCH_UNCACHED flag is private to the "disks" and "sgen"
224 * modules. NOT to be used by other modules.
226 rules
[0].flags
= MATCH_NODE
| MATCH_UNCACHED
; /* disks */
227 rules
[2].flags
= MATCH_NODE
| MATCH_UNCACHED
; /* generic scsi */
228 if (ctrl_enumerate_int(path
, RULE_INDEX
, &buf
, rules
, 3, 0,