xref: /titanic_50/usr/src/lib/sun_sas/common/Sun_sasScsiInquiry.c (revision 6ba597c56d749c61b4f783157f63196d7b2445f0)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sun_sas.h>
28 
29 /*
30  * Combine uscsi command ans send it out via ioctl
31  */
32 static HBA_STATUS
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)
36 {
37 	HBA_UINT32		status;
38 	struct uscsi_cmd	ucmd_buf;
39 	union scsi_cdb		cdb;
40 
41 	bzero(&cdb, sizeof (cdb));
42 	bzero(&ucmd_buf, sizeof (ucmd_buf));
43 	bzero(senseBuffer, *senseSize);
44 	bzero(responseBuffer, *responseSize);
45 
46 	cdb.scc_cmd = SCMD_INQUIRY;
47 	cdb.g0_addr1 = cdb2;
48 	cdb.g0_addr2 = cdb1;
49 	cdb.g0_count0 = *responseSize;
50 
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;
58 
59 	status = send_uscsi_cmd(devpath, &ucmd_buf);
60 	*scsiStatus = ucmd_buf.uscsi_status;
61 	return (status);
62 }
63 
64 
65 /*
66  * Send a SCSI inquiry to a remote WWN
67  */
68 HBA_STATUS
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)
73 {
74 	const char		ROUTINE[] = "Sun_sasScsiInquiry";
75 	HBA_STATUS		status;
76 	int			index = 0;
77 	int			domainPortFound = 0;
78 	int			hbaPortFound = 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;
83 	hrtime_t		start, end;
84 	double			duration;
85 	HBA_SCSILUN		hba_lun;
86 
87 	start = gethrtime();
88 	/* Validate the arguments */
89 	if (responseBuffer == NULL) {
90 		log(LOG_DEBUG, ROUTINE, "NULL response buffer");
91 		return (HBA_STATUS_ERROR_ARG);
92 	}
93 	if (senseBuffer == NULL) {
94 		log(LOG_DEBUG, ROUTINE, "NULL sense buffer");
95 		return (HBA_STATUS_ERROR_ARG);
96 	}
97 	if (responseSize == NULL) {
98 		log(LOG_DEBUG, ROUTINE, "NULL response size");
99 		return (HBA_STATUS_ERROR_ARG);
100 	}
101 	if (senseSize == NULL) {
102 		log(LOG_DEBUG, ROUTINE, "NULL sense size");
103 		return (HBA_STATUS_ERROR_ARG);
104 	}
105 	if (scsiStatus == NULL) {
106 		log(LOG_DEBUG, ROUTINE, "NULL scsi status");
107 		return (HBA_STATUS_ERROR_ARG);
108 	}
109 
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);
118 	}
119 
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);
126 		return (status);
127 	}
128 
129 	/*
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
133 	 */
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);
141 	}
142 
143 	if (wwnConversion(domainPortWWN.wwn))
144 		chkDomainPort = 1;
145 
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) {
150 
151 		if (hbaPortFound == 0) {
152 			if (wwnConversion(hba_port_ptr->port_attributes.
153 			    PortSpecificAttribute.SASPort->LocalSASAddress.wwn)
154 			    != wwnConversion(portWWN.wwn)) {
155 				/*
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.
159 				 */
160 				break;
161 			} else {
162 				hbaPortFound = 1;
163 			}
164 		}
165 
166 		if (chkDomainPort) {
167 			if (hba_port_ptr->first_phy != NULL &&
168 			    wwnConversion(hba_port_ptr->first_phy->
169 			    phy.domainPortWWN.wwn) ==
170 			    wwnConversion(domainPortWWN.wwn)) {
171 				domainPortFound = 1;
172 			}
173 			if (!(domainPortFound)) {
174 				continue;
175 			}
176 		}
177 
178 		for (hba_disco_port = hba_port_ptr->first_attached_port;
179 		    hba_disco_port != NULL;
180 		    hba_disco_port = hba_disco_port->next) {
181 
182 			/*
183 			 * If discoveredPort is not given targetPort, just skip
184 			 */
185 			if (wwnConversion(hba_disco_port->port_attributes.\
186 			    PortSpecificAttribute.SASPort->LocalSASAddress.wwn)
187 			    != wwnConversion(targetPortWWN.wwn)) {
188 				/* Does not match */
189 				continue;
190 			}
191 
192 			/*
193 			 * If discoveredPort is not a SAS/SATA port, it is not a
194 			 * target port
195 			 */
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);
206 			}
207 
208 			/*
209 			 * Iterating and matching is needed.
210 			 */
211 			for (mapping_ptr = hba_disco_port->scsiInfo;
212 			    mapping_ptr != NULL;
213 			    mapping_ptr = mapping_ptr->next) {
214 
215 				if (memcmp(
216 				    &mapping_ptr->entry.PortLun.TargetLun,
217 				    &smhbaLUN, sizeof (HBA_SCSILUN))
218 				    != 0) {
219 					continue;
220 				}
221 
222 				status = SendScsiInquiry(
223 				    mapping_ptr->entry.ScsiId.OSDeviceName,
224 				    cdb1, cdb2,
225 				    responseBuffer, responseSize,
226 				    scsiStatus, senseBuffer,
227 				    senseSize);
228 
229 				unlock(&open_handles_lock);
230 				unlock(&all_hbas_lock);
231 				end = gethrtime();
232 				duration = end - start;
233 				duration /= HR_SECOND;
234 				log(LOG_DEBUG, ROUTINE, "Took total\
235 				    of %.4f seconds", duration);
236 				return (status);
237 			}
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);
246 		}
247 		if (chkDomainPort) {
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);
254 		}
255 	}
256 
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);
267 	} else {
268 		log(LOG_DEBUG, ROUTINE, "Unable to locate requested "
269 		    "Port WWN %016llx on handle %08lx",
270 		    wwnConversion(targetPortWWN.wwn), handle);
271 	}
272 	return (HBA_STATUS_ERROR_ILLEGAL_WWN);
273 }
274