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 * Pass request buffer into uscsi command and sent it out via ioctl
33 SendScsiReadCapacity(const char *devpath
, void *responseBuffer
,
34 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_READ_CAPACITY
;
48 ucmd_buf
.uscsi_cdb
= (char *)&cdb
;
49 ucmd_buf
.uscsi_cdblen
= CDB_GROUP1
;
50 ucmd_buf
.uscsi_bufaddr
= (caddr_t
)responseBuffer
;
51 ucmd_buf
.uscsi_buflen
= *responseSize
;
52 ucmd_buf
.uscsi_rqbuf
= (caddr_t
)senseBuffer
;
53 ucmd_buf
.uscsi_rqlen
= *senseSize
;
54 ucmd_buf
.uscsi_flags
= USCSI_READ
| USCSI_SILENT
| USCSI_RQENABLE
;
55 ucmd_buf
.uscsi_timeout
= 60;
57 status
= send_uscsi_cmd(devpath
, &ucmd_buf
);
58 *scsiStatus
= ucmd_buf
.uscsi_status
;
63 * Send a read capacity to a remote WWN
66 Sun_sasScsiReadCapacity(HBA_HANDLE handle
, HBA_WWN portWWN
,
67 HBA_WWN targetPortWWN
, HBA_WWN domainPortWWN
, SMHBA_SCSILUN smhbaLUN
,
68 void *responseBuffer
, HBA_UINT32
*responseSize
,
69 HBA_UINT8
*scsiStatus
, void *senseBuffer
, HBA_UINT32
*senseSize
)
71 const char ROUTINE
[] = "Sun_sasScsiReadCapacity";
73 int index
= 0, domainPortFound
= 0;
75 int chkDomainPort
= 0;
76 struct sun_sas_hba
*hba_ptr
= NULL
;
77 struct sun_sas_port
*hba_port_ptr
, *hba_disco_port
;
78 struct ScsiEntryList
*mapping_ptr
;
84 /* Validate the arguments */
85 if (responseBuffer
== NULL
) {
86 log(LOG_DEBUG
, ROUTINE
, "NULL response buffer");
87 return (HBA_STATUS_ERROR_ARG
);
89 if (senseBuffer
== NULL
) {
90 log(LOG_DEBUG
, ROUTINE
, "NULL sense buffer");
91 return (HBA_STATUS_ERROR_ARG
);
93 if (responseSize
== NULL
) {
94 log(LOG_DEBUG
, ROUTINE
, "NULL response size");
95 return (HBA_STATUS_ERROR_ARG
);
97 if (senseSize
== NULL
) {
98 log(LOG_DEBUG
, ROUTINE
, "NULL sense size");
99 return (HBA_STATUS_ERROR_ARG
);
101 if (scsiStatus
== NULL
) {
102 log(LOG_DEBUG
, ROUTINE
, "NULL scsi status");
103 return (HBA_STATUS_ERROR_ARG
);
106 lock(&all_hbas_lock
);
107 index
= RetrieveIndex(handle
);
108 lock(&open_handles_lock
);
109 if ((hba_ptr
= RetrieveHandle(index
)) == NULL
) {
110 log(LOG_DEBUG
, ROUTINE
, "Invalid handle %08lx", handle
);
111 unlock(&open_handles_lock
);
112 unlock(&all_hbas_lock
);
113 return (HBA_STATUS_ERROR_INVALID_HANDLE
);
116 /* Check for stale data */
117 status
= verifyAdapter(hba_ptr
);
118 if (status
!= HBA_STATUS_OK
) {
119 log(LOG_DEBUG
, ROUTINE
, "Verify adapter failed");
120 unlock(&open_handles_lock
);
121 unlock(&all_hbas_lock
);
125 if (wwnConversion(domainPortWWN
.wwn
))
129 * We are not checking to see if our data is stale.
130 * By verifying this information here, we will take a big performance
131 * hit. This check will be done later only if the FCSM ioctl fails
134 /* Determine which port to use */
135 for (hba_port_ptr
= hba_ptr
->first_port
;
136 hba_port_ptr
!= NULL
;
137 hba_port_ptr
= hba_port_ptr
->next
) {
139 if (hbaPortFound
== 0) {
140 if (wwnConversion(hba_port_ptr
->port_attributes
.
141 PortSpecificAttribute
.SASPort
->LocalSASAddress
.wwn
)
142 != wwnConversion(portWWN
.wwn
)) {
144 * Since all the ports under the same HBA have
145 * the same LocalSASAddress, we should break
146 * the loop once we find it dosn't match.
154 if (chkDomainPort
!= 0) {
155 if (hba_port_ptr
->first_phy
!= NULL
&&
156 wwnConversion(hba_port_ptr
->first_phy
->
157 phy
.domainPortWWN
.wwn
) ==
158 wwnConversion(domainPortWWN
.wwn
)) {
161 if (!(domainPortFound
)) {
166 for (hba_disco_port
= hba_port_ptr
->first_attached_port
;
167 hba_disco_port
!= NULL
;
168 hba_disco_port
= hba_disco_port
->next
) {
171 * If discoveredPort is not given targetPort, just skip
173 if (wwnConversion(hba_disco_port
->port_attributes
.\
174 PortSpecificAttribute
.SASPort
->LocalSASAddress
.wwn
)
175 != wwnConversion(targetPortWWN
.wwn
)) {
181 * If discoveredPort is not a SAS/SATA port, it is not a
184 if ((hba_disco_port
->port_attributes
.PortType
!=
185 HBA_PORTTYPE_SATADEVICE
) &&
186 (hba_disco_port
->port_attributes
.PortType
!=
187 HBA_PORTTYPE_SASDEVICE
)) {
188 unlock(&open_handles_lock
);
189 unlock(&all_hbas_lock
);
190 log(LOG_DEBUG
, ROUTINE
, "Target Port WWN "
191 "%016llx on handle %08lx is not a Target",
192 wwnConversion(targetPortWWN
.wwn
), handle
);
193 return (HBA_STATUS_ERROR_NOT_A_TARGET
);
197 * Iterating and matching is needed.
199 for (mapping_ptr
= hba_disco_port
->scsiInfo
;
201 mapping_ptr
= mapping_ptr
->next
) {
204 &mapping_ptr
->entry
.PortLun
.TargetLun
,
205 &smhbaLUN
, sizeof (HBA_SCSILUN
))
210 status
= SendScsiReadCapacity(
211 mapping_ptr
->entry
.ScsiId
.\
213 responseBuffer
, responseSize
,
214 scsiStatus
, senseBuffer
, senseSize
);
216 unlock(&open_handles_lock
);
217 unlock(&all_hbas_lock
);
219 duration
= end
- start
;
220 duration
/= HR_SECOND
;
221 log(LOG_DEBUG
, ROUTINE
, "Took total\
222 of %.4f seconds", duration
);
225 unlock(&open_handles_lock
);
226 unlock(&all_hbas_lock
);
227 (void *) memcpy(&hba_lun
, &smhbaLUN
,
228 sizeof (HBA_SCSILUN
));
229 log(LOG_DEBUG
, ROUTINE
, "Unable to locate lun"
230 " %08lx for target %016llx on handle %08lx",
231 hba_lun
, wwnConversion(targetPortWWN
.wwn
), handle
);
232 return (HBA_STATUS_ERROR_INVALID_LUN
);
236 unlock(&open_handles_lock
);
237 unlock(&all_hbas_lock
);
238 log(LOG_DEBUG
, ROUTINE
, "Unable to locate requested "
239 "Port WWN %016llx on handle %08lx",
240 wwnConversion(targetPortWWN
.wwn
), handle
);
241 return (HBA_STATUS_ERROR_ILLEGAL_WWN
);
245 unlock(&open_handles_lock
);
246 unlock(&all_hbas_lock
);
247 if (hbaPortFound
== 0) {
248 log(LOG_DEBUG
, ROUTINE
,
249 "Unable to locate requested Port WWN %016llx on "
250 "handle %08lx", wwnConversion(portWWN
.wwn
), handle
);
251 } else if (chkDomainPort
&& !domainPortFound
) {
252 log(LOG_DEBUG
, ROUTINE
, "Unable to locate requested"
253 " domainPortWWN %016llx on handle %08lx",
254 wwnConversion(domainPortWWN
.wwn
), handle
);
256 log(LOG_DEBUG
, ROUTINE
, "Unable to locate requested "
257 "Port WWN %016llx on handle %08lx",
258 wwnConversion(targetPortWWN
.wwn
), handle
);
260 return (HBA_STATUS_ERROR_ILLEGAL_WWN
);