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]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
30 * Combine uscsi command ans send it out via ioctl
33 SendScsiInquiry(const char *devpath
, HBA_UINT8 cdb1
, HBA_UINT8 cdb2
,
34 void *responseBuffer
, HBA_UINT32
*responseSize
, HBA_UINT8
*scsiStatus
,
35 void *senseBuffer
, HBA_UINT32
*senseSize
)
38 struct uscsi_cmd ucmd_buf
;
41 bzero(&cdb
, sizeof (cdb
));
42 bzero(&ucmd_buf
, sizeof (ucmd_buf
));
43 bzero(senseBuffer
, *senseSize
);
44 bzero(responseBuffer
, *responseSize
);
46 cdb
.scc_cmd
= SCMD_INQUIRY
;
49 cdb
.g0_count0
= *responseSize
;
51 ucmd_buf
.uscsi_cdb
= (char *)&cdb
;
52 ucmd_buf
.uscsi_cdblen
= CDB_GROUP0
;
53 ucmd_buf
.uscsi_bufaddr
= (caddr_t
)responseBuffer
;
54 ucmd_buf
.uscsi_buflen
= *responseSize
;
55 ucmd_buf
.uscsi_rqbuf
= (caddr_t
)senseBuffer
;
56 ucmd_buf
.uscsi_rqlen
= *senseSize
;
57 ucmd_buf
.uscsi_flags
= USCSI_READ
| USCSI_SILENT
| USCSI_RQENABLE
;
59 status
= send_uscsi_cmd(devpath
, &ucmd_buf
);
60 *scsiStatus
= ucmd_buf
.uscsi_status
;
66 * Send a SCSI inquiry to a remote WWN
69 Sun_sasScsiInquiry(HBA_HANDLE handle
, HBA_WWN portWWN
, HBA_WWN targetPortWWN
,
70 HBA_WWN domainPortWWN
, SMHBA_SCSILUN smhbaLUN
, HBA_UINT8 cdb1
,
71 HBA_UINT8 cdb2
, void *responseBuffer
, HBA_UINT32
*responseSize
,
72 HBA_UINT8
*scsiStatus
, void *senseBuffer
, HBA_UINT32
*senseSize
)
74 const char ROUTINE
[] = "Sun_sasScsiInquiry";
77 int domainPortFound
= 0;
79 int chkDomainPort
= 0;
80 struct sun_sas_hba
*hba_ptr
= NULL
;
81 struct sun_sas_port
*hba_port_ptr
, *hba_disco_port
;
82 struct ScsiEntryList
*mapping_ptr
;
88 /* Validate the arguments */
89 if (responseBuffer
== NULL
) {
90 log(LOG_DEBUG
, ROUTINE
, "NULL response buffer");
91 return (HBA_STATUS_ERROR_ARG
);
93 if (senseBuffer
== NULL
) {
94 log(LOG_DEBUG
, ROUTINE
, "NULL sense buffer");
95 return (HBA_STATUS_ERROR_ARG
);
97 if (responseSize
== NULL
) {
98 log(LOG_DEBUG
, ROUTINE
, "NULL response size");
99 return (HBA_STATUS_ERROR_ARG
);
101 if (senseSize
== NULL
) {
102 log(LOG_DEBUG
, ROUTINE
, "NULL sense size");
103 return (HBA_STATUS_ERROR_ARG
);
105 if (scsiStatus
== NULL
) {
106 log(LOG_DEBUG
, ROUTINE
, "NULL scsi status");
107 return (HBA_STATUS_ERROR_ARG
);
110 lock(&all_hbas_lock
);
111 index
= RetrieveIndex(handle
);
112 lock(&open_handles_lock
);
113 if ((hba_ptr
= RetrieveHandle(index
)) == NULL
) {
114 log(LOG_DEBUG
, ROUTINE
, "Invalid handle %08lx", handle
);
115 unlock(&open_handles_lock
);
116 unlock(&all_hbas_lock
);
117 return (HBA_STATUS_ERROR_INVALID_HANDLE
);
120 /* Check for stale data */
121 status
= verifyAdapter(hba_ptr
);
122 if (status
!= HBA_STATUS_OK
) {
123 log(LOG_DEBUG
, ROUTINE
, "Verify adapter failed");
124 unlock(&open_handles_lock
);
125 unlock(&all_hbas_lock
);
130 * We are not checking to see if our data is stale.
131 * By verifying this information here, we will take a big performance
132 * hit. This check will be done later only if the Inquiry ioctl fails
134 if (hba_ptr
->device_path
== NULL
) {
135 log(LOG_DEBUG
, ROUTINE
,
136 "HBA handle had NULL device path. \
137 Unable to send SCSI cmd");
138 unlock(&open_handles_lock
);
139 unlock(&all_hbas_lock
);
140 return (HBA_STATUS_ERROR
);
143 if (wwnConversion(domainPortWWN
.wwn
))
146 /* Determine which port to use */
147 for (hba_port_ptr
= hba_ptr
->first_port
;
148 hba_port_ptr
!= NULL
;
149 hba_port_ptr
= hba_port_ptr
->next
) {
151 if (hbaPortFound
== 0) {
152 if (wwnConversion(hba_port_ptr
->port_attributes
.
153 PortSpecificAttribute
.SASPort
->LocalSASAddress
.wwn
)
154 != wwnConversion(portWWN
.wwn
)) {
156 * Since all the ports under the same HBA have
157 * the same LocalSASAddress, we should break
158 * the loop once we find it dosn't match.
167 if (hba_port_ptr
->first_phy
!= NULL
&&
168 wwnConversion(hba_port_ptr
->first_phy
->
169 phy
.domainPortWWN
.wwn
) ==
170 wwnConversion(domainPortWWN
.wwn
)) {
173 if (!(domainPortFound
)) {
178 for (hba_disco_port
= hba_port_ptr
->first_attached_port
;
179 hba_disco_port
!= NULL
;
180 hba_disco_port
= hba_disco_port
->next
) {
183 * If discoveredPort is not given targetPort, just skip
185 if (wwnConversion(hba_disco_port
->port_attributes
.\
186 PortSpecificAttribute
.SASPort
->LocalSASAddress
.wwn
)
187 != wwnConversion(targetPortWWN
.wwn
)) {
193 * If discoveredPort is not a SAS/SATA port, it is not a
196 if ((hba_disco_port
->port_attributes
.PortType
!=
197 HBA_PORTTYPE_SATADEVICE
) &&
198 (hba_disco_port
->port_attributes
.PortType
!=
199 HBA_PORTTYPE_SASDEVICE
)) {
200 unlock(&open_handles_lock
);
201 unlock(&all_hbas_lock
);
202 log(LOG_DEBUG
, ROUTINE
, "Target Port WWN "
203 "%016llx on handle %08lx is not a Target",
204 wwnConversion(targetPortWWN
.wwn
), handle
);
205 return (HBA_STATUS_ERROR_NOT_A_TARGET
);
209 * Iterating and matching is needed.
211 for (mapping_ptr
= hba_disco_port
->scsiInfo
;
213 mapping_ptr
= mapping_ptr
->next
) {
216 &mapping_ptr
->entry
.PortLun
.TargetLun
,
217 &smhbaLUN
, sizeof (HBA_SCSILUN
))
222 status
= SendScsiInquiry(
223 mapping_ptr
->entry
.ScsiId
.OSDeviceName
,
225 responseBuffer
, responseSize
,
226 scsiStatus
, senseBuffer
,
229 unlock(&open_handles_lock
);
230 unlock(&all_hbas_lock
);
232 duration
= end
- start
;
233 duration
/= HR_SECOND
;
234 log(LOG_DEBUG
, ROUTINE
, "Took total\
235 of %.4f seconds", duration
);
238 unlock(&open_handles_lock
);
239 unlock(&all_hbas_lock
);
240 (void *) memcpy(&hba_lun
, &smhbaLUN
,
241 sizeof (HBA_SCSILUN
));
242 log(LOG_DEBUG
, ROUTINE
, "Unable to locate lun"
243 " %08lx for target %016llx on handle %08lx",
244 hba_lun
, wwnConversion(targetPortWWN
.wwn
), handle
);
245 return (HBA_STATUS_ERROR_INVALID_LUN
);
248 unlock(&open_handles_lock
);
249 unlock(&all_hbas_lock
);
250 log(LOG_DEBUG
, ROUTINE
, "Unable to locate requested "
251 "Port WWN %016llx on handle %08lx",
252 wwnConversion(targetPortWWN
.wwn
), handle
);
253 return (HBA_STATUS_ERROR_ILLEGAL_WWN
);
257 unlock(&open_handles_lock
);
258 unlock(&all_hbas_lock
);
259 if (hbaPortFound
== 0) {
260 log(LOG_DEBUG
, ROUTINE
,
261 "Unable to locate requested Port WWN %016llx on "
262 "handle %08lx", wwnConversion(portWWN
.wwn
), handle
);
263 } else if (chkDomainPort
&& !domainPortFound
) {
264 log(LOG_DEBUG
, ROUTINE
, "Unable to locate requested"
265 " domainPortWWN %016llx on handle %08lx",
266 wwnConversion(domainPortWWN
.wwn
), handle
);
268 log(LOG_DEBUG
, ROUTINE
, "Unable to locate requested "
269 "Port WWN %016llx on handle %08lx",
270 wwnConversion(targetPortWWN
.wwn
), handle
);
272 return (HBA_STATUS_ERROR_ILLEGAL_WWN
);