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  * Pass scsi request buffer into uscsi command and sent it out via ioctl
31  */
32 static HBA_STATUS
SendScsiReportLUNs(const char * devpath,void * responseBuffer,HBA_UINT32 * responseSize,HBA_UINT8 * scsiStatus,void * senseBuffer,HBA_UINT32 * senseSize)33 SendScsiReportLUNs(const char *devpath, void *responseBuffer,
34     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 
45 	cdb.scc_cmd = SCMD_REPORT_LUNS;
46 	FORMG5COUNT(&cdb, *responseSize);
47 
48 	ucmd_buf.uscsi_cdb = (char *)&cdb;
49 	ucmd_buf.uscsi_cdblen = CDB_GROUP5;
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;
56 
57 	status = send_uscsi_cmd(devpath, &ucmd_buf);
58 	*scsiStatus = ucmd_buf.uscsi_status;
59 	return (status);
60 
61 }
62 
63 /*
64  * Send a SCSI report luns command to a remote WWN
65  */
66 HBA_STATUS
Sun_sasScsiReportLUNs(HBA_HANDLE handle,HBA_WWN portWWN,HBA_WWN targetPortWWN,HBA_WWN domainPortWWN,void * responseBuffer,HBA_UINT32 * responseSize,HBA_UINT8 * scsiStatus,void * senseBuffer,HBA_UINT32 * senseSize)67 Sun_sasScsiReportLUNs(HBA_HANDLE handle, HBA_WWN portWWN, HBA_WWN targetPortWWN,
68     HBA_WWN domainPortWWN, void *responseBuffer, HBA_UINT32 *responseSize,
69     HBA_UINT8 *scsiStatus, void *senseBuffer, HBA_UINT32 *senseSize)
70 {
71 	const char		ROUTINE[] = "Sun_sasScsiReportLUNs";
72 	HBA_STATUS		status;
73 	int			index = 0, domainPortFound = 0;
74 	int			chkDomainPort = 0;
75 	int			hbaPortFound = 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;
79 	hrtime_t		start, end;
80 	double			duration;
81 
82 	start = gethrtime();
83 
84 	/* Validate the arguments */
85 	if (responseBuffer == NULL) {
86 		log(LOG_DEBUG, ROUTINE, "NULL response buffer");
87 		return (HBA_STATUS_ERROR_ARG);
88 	}
89 	if (senseBuffer == NULL) {
90 		log(LOG_DEBUG, ROUTINE, "NULL sense buffer");
91 		return (HBA_STATUS_ERROR_ARG);
92 	}
93 	if (responseSize == NULL) {
94 		log(LOG_DEBUG, ROUTINE, "NULL response size");
95 		return (HBA_STATUS_ERROR_ARG);
96 	}
97 	if (senseSize == NULL) {
98 		log(LOG_DEBUG, ROUTINE, "NULL sense size");
99 		return (HBA_STATUS_ERROR_ARG);
100 	}
101 	if (scsiStatus == NULL) {
102 		log(LOG_DEBUG, ROUTINE, "NULL scsi status");
103 		return (HBA_STATUS_ERROR_ARG);
104 	}
105 
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);
114 	}
115 
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);
122 		return (status);
123 	}
124 
125 	if (wwnConversion(domainPortWWN.wwn))
126 		chkDomainPort = 1;
127 	/* Determine which port to use */
128 	for (hba_port_ptr = hba_ptr->first_port;
129 	    hba_port_ptr != NULL;
130 	    hba_port_ptr = hba_port_ptr->next) {
131 
132 		if (hbaPortFound == 0) {
133 			if (wwnConversion(hba_port_ptr->port_attributes.
134 			    PortSpecificAttribute.SASPort->LocalSASAddress.wwn)
135 			    != wwnConversion(portWWN.wwn)) {
136 				/*
137 				 * Since all the ports under the same HBA have
138 				 * the same LocalSASAddress, we should break
139 				 * the loop once we find it dosn't match.
140 				 */
141 				break;
142 			} else {
143 				hbaPortFound = 1;
144 			}
145 		}
146 
147 		if (chkDomainPort != 0) {
148 			if (hba_port_ptr->first_phy != NULL &&
149 			    wwnConversion(hba_port_ptr->first_phy->
150 			    phy.domainPortWWN.wwn) ==
151 			    wwnConversion(domainPortWWN.wwn)) {
152 				domainPortFound = 1;
153 			}
154 			if (!(domainPortFound)) {
155 				continue;
156 			}
157 		}
158 
159 		for (hba_disco_port = hba_port_ptr->first_attached_port;
160 		    hba_disco_port != NULL;
161 		    hba_disco_port = hba_disco_port->next) {
162 
163 			/*
164 			 * If discoveredPort is not given targetPort, skip
165 			 */
166 			if (wwnConversion(hba_disco_port->port_attributes.\
167 			    PortSpecificAttribute.SASPort->LocalSASAddress.wwn)
168 			    != wwnConversion(targetPortWWN.wwn)) {
169 				/* Does not match */
170 				continue;
171 			}
172 
173 			/*
174 			 * If discoveredPort is not a SAS/SATA port, it is not a
175 			 * target port
176 			 */
177 			if ((hba_disco_port->port_attributes.PortType !=
178 			    HBA_PORTTYPE_SATADEVICE) &&
179 			    (hba_disco_port->port_attributes.PortType !=
180 			    HBA_PORTTYPE_SASDEVICE)) {
181 				unlock(&open_handles_lock);
182 				unlock(&all_hbas_lock);
183 				log(LOG_DEBUG, ROUTINE, "Target Port WWN "
184 				    "%016llx on handle %08lx is not a Target",
185 				    wwnConversion(targetPortWWN.wwn), handle);
186 				return (HBA_STATUS_ERROR_NOT_A_TARGET);
187 			}
188 
189 			if ((mapping_ptr = hba_disco_port->scsiInfo) != NULL) {
190 
191 				status = SendScsiReportLUNs(
192 				    mapping_ptr->entry.ScsiId.OSDeviceName,
193 				    responseBuffer, responseSize,
194 				    scsiStatus, senseBuffer, senseSize);
195 
196 				unlock(&open_handles_lock);
197 				unlock(&all_hbas_lock);
198 				end = gethrtime();
199 				duration = end - start;
200 				duration /= HR_SECOND;
201 				log(LOG_DEBUG, ROUTINE, "Took total\
202 				    of %.4f seconds", duration);
203 				return (status);
204 			}
205 		}
206 
207 		if (chkDomainPort) {
208 			unlock(&open_handles_lock);
209 			unlock(&all_hbas_lock);
210 			log(LOG_DEBUG, ROUTINE, "Unable to located requested "
211 			    "Port %016llx on handle %08lx",
212 			    wwnConversion(targetPortWWN.wwn), handle);
213 			return (HBA_STATUS_ERROR_ILLEGAL_WWN);
214 		}
215 	}
216 
217 	unlock(&open_handles_lock);
218 	unlock(&all_hbas_lock);
219 	if (hbaPortFound == 0) {
220 		log(LOG_DEBUG, ROUTINE,
221 		    "Unable to locate requested Port WWN %016llx on "
222 		    "handle %08lx", wwnConversion(portWWN.wwn), handle);
223 	} else if (chkDomainPort && !domainPortFound) {
224 		log(LOG_DEBUG, ROUTINE, "Unable to locate requested"
225 		    " domainPortWWN %016llx on handle %08lx",
226 		    wwnConversion(domainPortWWN.wwn), handle);
227 	} else {
228 		log(LOG_DEBUG, ROUTINE, "Unable to locate requested "
229 		    "Port WWN %016llx on handle %08lx",
230 		    wwnConversion(targetPortWWN.wwn), handle);
231 	}
232 	return (HBA_STATUS_ERROR_ILLEGAL_WWN);
233 }
234