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 * Copyright 2019 Joyent, Inc.
28 */
29
30 #include <sun_sas.h>
31 #include <sys/scsi/impl/usmp.h>
32
33 /*
34 * Pass usmp_cmd into ioctl
35 */
36 static HBA_STATUS
SendSMPPassThru(const char * devpath,void * reqframe,HBA_UINT32 * reqsize,void * rspframe,HBA_UINT32 * rspsize)37 SendSMPPassThru(const char *devpath, void *reqframe, HBA_UINT32 *reqsize,
38 void *rspframe, HBA_UINT32 *rspsize)
39 {
40 const char ROUTINE[] = "SendSMPPassThru";
41 int fd;
42 usmp_cmd_t ucmd_buf;
43 HBA_STATUS ret;
44
45 bzero(&ucmd_buf, sizeof (ucmd_buf));
46
47 ucmd_buf.usmp_req = (caddr_t)reqframe;
48 ucmd_buf.usmp_rsp = (caddr_t)rspframe;
49 ucmd_buf.usmp_reqsize = (size_t)(*reqsize);
50 ucmd_buf.usmp_rspsize = (size_t)(*rspsize);
51 ucmd_buf.usmp_timeout = SMP_DEFAULT_TIMEOUT;
52
53 /*
54 * open smp device
55 */
56
57 if ((fd = open(devpath, O_RDONLY | O_NONBLOCK)) == -1) {
58 log(LOG_DEBUG, ROUTINE,
59 "open devpath %s failed due to %s",
60 devpath, strerror(errno));
61 return (HBA_STATUS_ERROR);
62 }
63
64 /*
65 * send usmp command
66 */
67 if (ioctl(fd, USMPFUNC, &ucmd_buf) == -1) {
68 if ((errno == ETIME) || (errno == ETIMEDOUT) ||
69 (errno == EAGAIN)) {
70 ret = HBA_STATUS_ERROR_TRY_AGAIN;
71 } else if (errno == EBUSY) {
72 ret = HBA_STATUS_ERROR_BUSY;
73 } else {
74 ret = HBA_STATUS_ERROR;
75 }
76 log(LOG_DEBUG, ROUTINE, "ioctl:USMPFUNC failed due to %s",
77 strerror(errno));
78 (void) close(fd);
79 return (ret);
80 }
81
82 (void) close(fd);
83 return (HBA_STATUS_OK);
84 }
85
86 /*
87 * Send a USMP command to a remote SMP node
88 */
89 HBA_STATUS
Sun_sasSendSMPPassThru(HBA_HANDLE handle,HBA_WWN hbaPortWWN,HBA_WWN destPortWWN,HBA_WWN domainPortWWN,void * pReqBuffer,HBA_UINT32 ReqBufferSize,void * pRspBuffer,HBA_UINT32 * pRspBufferSize)90 Sun_sasSendSMPPassThru(HBA_HANDLE handle, HBA_WWN hbaPortWWN,
91 HBA_WWN destPortWWN, HBA_WWN domainPortWWN, void *pReqBuffer,
92 HBA_UINT32 ReqBufferSize, void *pRspBuffer, HBA_UINT32 *pRspBufferSize)
93 {
94 const char ROUTINE[] = "Sun_sasSendSMPPassThru";
95 HBA_STATUS status;
96 struct sun_sas_hba *hba_ptr;
97 int domainPortFound = 0;
98 int chkDomainPort = 0;
99 int hbaPortFound = 0;
100 struct sun_sas_port *hba_port_ptr, *hba_disco_port;
101 hrtime_t start, end;
102 double duration;
103
104 start = gethrtime();
105 /* Validate the arguments */
106 if (pRspBuffer == NULL) {
107 log(LOG_DEBUG, ROUTINE, "NULL response buffer");
108 return (HBA_STATUS_ERROR_ARG);
109 }
110 if (pReqBuffer == NULL) {
111 log(LOG_DEBUG, ROUTINE, "NULL sense buffer");
112 return (HBA_STATUS_ERROR_ARG);
113 }
114 if (pRspBufferSize == NULL) {
115 log(LOG_DEBUG, ROUTINE, "NULL response size");
116 return (HBA_STATUS_ERROR_ARG);
117 }
118
119 lock(&all_hbas_lock);
120 if ((hba_ptr = Retrieve_Sun_sasHandle(handle)) == NULL) {
121 log(LOG_DEBUG, ROUTINE, "Invalid handle %08lx", handle);
122 unlock(&all_hbas_lock);
123 return (HBA_STATUS_ERROR_INVALID_HANDLE);
124 }
125
126 /* Check for stale data */
127 status = verifyAdapter(hba_ptr);
128 if (status != HBA_STATUS_OK) {
129 log(LOG_DEBUG, ROUTINE, "Verify adapter failed");
130 unlock(&all_hbas_lock);
131 return (status);
132 }
133
134 /*
135 * We are not checking to see if our data is stale.
136 * By verifying this information here, we will take a big performance
137 * hit. This check will be done later only if the Inquiry ioctl fails
138 */
139
140 if (hba_ptr->device_path[0] == '\0') {
141 log(LOG_DEBUG, ROUTINE,
142 "HBA handle had empty device path.\
143 Unable to send SCSI cmd");
144 unlock(&all_hbas_lock);
145 return (HBA_STATUS_ERROR);
146 }
147
148 if (wwnConversion(domainPortWWN.wwn))
149 chkDomainPort = 1;
150
151 /* Determine which port to use */
152 for (hba_port_ptr = hba_ptr->first_port;
153 hba_port_ptr != NULL;
154 hba_port_ptr = hba_port_ptr->next) {
155
156 if (hbaPortFound == 0) {
157 if (wwnConversion(hba_port_ptr->port_attributes.
158 PortSpecificAttribute.SASPort->LocalSASAddress.wwn)
159 != wwnConversion(hbaPortWWN.wwn)) {
160 /*
161 * Since all the ports under the same HBA have
162 * the same LocalSASAddress, we should break
163 * the loop once we find it dosn't match.
164 */
165 break;
166 } else {
167 hbaPortFound = 1;
168 }
169 }
170
171 if (chkDomainPort != 0) {
172 if (hba_port_ptr->first_phy != NULL &&
173 wwnConversion(hba_port_ptr->first_phy->
174 phy.domainPortWWN.wwn) ==
175 wwnConversion(domainPortWWN.wwn)) {
176 domainPortFound = 1;
177 }
178 if (!(domainPortFound)) {
179 continue;
180 }
181 }
182
183 for (hba_disco_port = hba_port_ptr->first_attached_port;
184 hba_disco_port != NULL;
185 hba_disco_port = hba_disco_port->next) {
186
187 /*
188 * If discoveredPort is not given targetPort, just skip
189 */
190 if (wwnConversion(hba_disco_port->port_attributes.\
191 PortSpecificAttribute.SASPort->LocalSASAddress.wwn)
192 != wwnConversion(destPortWWN.wwn)) {
193 /* Does not match */
194 continue;
195 }
196
197 /*
198 * If matching targetPort does not support SMP protocal
199 * return error.
200 * comment it out for testing only
201 */
202 if ((hba_disco_port->port_attributes.\
203 PortSpecificAttribute.SASPort->PortProtocol &
204 HBA_SASPORTPROTOCOL_SMP) == 0) {
205 log(LOG_DEBUG, ROUTINE, "Input WWN %01611x\
206 does not support SMP protocol",
207 wwnConversion(hbaPortWWN.wwn));
208 unlock(&all_hbas_lock);
209 return (HBA_STATUS_ERROR_INVALID_PROTOCOL_TYPE);
210 }
211
212 /*
213 * SMP target port doesn't have any scsi info.
214 * - like /dev/rdsk/cxtxdxsx
215 * So we use OSDeviceName from port attributes.
216 * - like /dev/smp/expd[0-9]
217 */
218 status = SendSMPPassThru(
219 hba_disco_port->port_attributes.OSDeviceName,
220 pReqBuffer, &ReqBufferSize,
221 pRspBuffer, pRspBufferSize);
222
223 unlock(&all_hbas_lock);
224 end = gethrtime();
225 duration = end - start;
226 duration /= HR_SECOND;
227 log(LOG_DEBUG, ROUTINE, "Took total\
228 of %.4f seconds", duration);
229 return (status);
230 }
231 if (chkDomainPort) {
232 unlock(&all_hbas_lock);
233 log(LOG_DEBUG, ROUTINE, "Unable to locate"
234 "requested SMP target port %16llx",
235 wwnConversion(destPortWWN.wwn));
236 return (HBA_STATUS_ERROR_ILLEGAL_WWN);
237 }
238 }
239 unlock(&all_hbas_lock);
240 if (hbaPortFound == 0) {
241 log(LOG_DEBUG, ROUTINE,
242 "Unable to locate requested Port WWN %016llx on "
243 "handle %08lx", wwnConversion(hbaPortWWN.wwn), handle);
244 } else if (chkDomainPort && !domainPortFound) {
245 log(LOG_DEBUG, ROUTINE, "Unable to locate requested"
246 " domainPortWWN %016llx on handle %08lx",
247 wwnConversion(domainPortWWN.wwn), handle);
248 } else {
249 log(LOG_DEBUG, ROUTINE, "Unable to locate"
250 "requested SMP target port %16llx",
251 wwnConversion(destPortWWN.wwn));
252 }
253 return (HBA_STATUS_ERROR_ILLEGAL_WWN);
254 }
255