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 32 /* 33 * Combine uscsi command ans send it out via ioctl 34 */ 35 static HBA_STATUS 36 SendScsiInquiry(const char *devpath, HBA_UINT8 cdb1, HBA_UINT8 cdb2, 37 void *responseBuffer, HBA_UINT32 *responseSize, HBA_UINT8 *scsiStatus, 38 void *senseBuffer, HBA_UINT32 *senseSize) 39 { 40 HBA_UINT32 status; 41 struct uscsi_cmd ucmd_buf; 42 union scsi_cdb cdb; 43 44 bzero(&cdb, sizeof (cdb)); 45 bzero(&ucmd_buf, sizeof (ucmd_buf)); 46 bzero(senseBuffer, *senseSize); 47 bzero(responseBuffer, *responseSize); 48 49 cdb.scc_cmd = SCMD_INQUIRY; 50 cdb.g0_addr1 = cdb2; 51 cdb.g0_addr2 = cdb1; 52 cdb.g0_count0 = *responseSize; 53 54 ucmd_buf.uscsi_cdb = (char *)&cdb; 55 ucmd_buf.uscsi_cdblen = CDB_GROUP0; 56 ucmd_buf.uscsi_bufaddr = (caddr_t)responseBuffer; 57 ucmd_buf.uscsi_buflen = *responseSize; 58 ucmd_buf.uscsi_rqbuf = (caddr_t)senseBuffer; 59 ucmd_buf.uscsi_rqlen = *senseSize; 60 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT | USCSI_RQENABLE; 61 62 status = send_uscsi_cmd(devpath, &ucmd_buf); 63 *scsiStatus = ucmd_buf.uscsi_status; 64 return (status); 65 } 66 67 68 /* 69 * Send a SCSI inquiry to a remote WWN 70 */ 71 HBA_STATUS 72 Sun_sasScsiInquiry(HBA_HANDLE handle, HBA_WWN portWWN, HBA_WWN targetPortWWN, 73 HBA_WWN domainPortWWN, SMHBA_SCSILUN smhbaLUN, HBA_UINT8 cdb1, 74 HBA_UINT8 cdb2, void *responseBuffer, HBA_UINT32 *responseSize, 75 HBA_UINT8 *scsiStatus, void *senseBuffer, HBA_UINT32 *senseSize) 76 { 77 const char ROUTINE[] = "Sun_sasScsiInquiry"; 78 HBA_STATUS status; 79 int index = 0; 80 int domainPortFound = 0; 81 int hbaPortFound = 0; 82 int chkDomainPort = 0; 83 struct sun_sas_hba *hba_ptr = NULL; 84 struct sun_sas_port *hba_port_ptr, *hba_disco_port; 85 struct ScsiEntryList *mapping_ptr; 86 hrtime_t start, end; 87 double duration; 88 HBA_SCSILUN hba_lun; 89 90 start = gethrtime(); 91 /* Validate the arguments */ 92 if (responseBuffer == NULL) { 93 log(LOG_DEBUG, ROUTINE, "NULL response buffer"); 94 return (HBA_STATUS_ERROR_ARG); 95 } 96 if (senseBuffer == NULL) { 97 log(LOG_DEBUG, ROUTINE, "NULL sense buffer"); 98 return (HBA_STATUS_ERROR_ARG); 99 } 100 if (responseSize == NULL) { 101 log(LOG_DEBUG, ROUTINE, "NULL response size"); 102 return (HBA_STATUS_ERROR_ARG); 103 } 104 if (senseSize == NULL) { 105 log(LOG_DEBUG, ROUTINE, "NULL sense size"); 106 return (HBA_STATUS_ERROR_ARG); 107 } 108 if (scsiStatus == NULL) { 109 log(LOG_DEBUG, ROUTINE, "NULL scsi status"); 110 return (HBA_STATUS_ERROR_ARG); 111 } 112 113 lock(&all_hbas_lock); 114 index = RetrieveIndex(handle); 115 lock(&open_handles_lock); 116 if ((hba_ptr = RetrieveHandle(index)) == NULL) { 117 log(LOG_DEBUG, ROUTINE, "Invalid handle %08lx", handle); 118 unlock(&open_handles_lock); 119 unlock(&all_hbas_lock); 120 return (HBA_STATUS_ERROR_INVALID_HANDLE); 121 } 122 123 /* Check for stale data */ 124 status = verifyAdapter(hba_ptr); 125 if (status != HBA_STATUS_OK) { 126 log(LOG_DEBUG, ROUTINE, "Verify adapter failed"); 127 unlock(&open_handles_lock); 128 unlock(&all_hbas_lock); 129 return (status); 130 } 131 132 /* 133 * We are not checking to see if our data is stale. 134 * By verifying this information here, we will take a big performance 135 * hit. This check will be done later only if the Inquiry ioctl fails 136 */ 137 if (hba_ptr->device_path[0] == '\0') { 138 log(LOG_DEBUG, ROUTINE, 139 "HBA handle had empty device path. \ 140 Unable to send SCSI cmd"); 141 unlock(&open_handles_lock); 142 unlock(&all_hbas_lock); 143 return (HBA_STATUS_ERROR); 144 } 145 146 if (wwnConversion(domainPortWWN.wwn)) 147 chkDomainPort = 1; 148 149 /* Determine which port to use */ 150 for (hba_port_ptr = hba_ptr->first_port; 151 hba_port_ptr != NULL; 152 hba_port_ptr = hba_port_ptr->next) { 153 154 if (hbaPortFound == 0) { 155 if (wwnConversion(hba_port_ptr->port_attributes. 156 PortSpecificAttribute.SASPort->LocalSASAddress.wwn) 157 != wwnConversion(portWWN.wwn)) { 158 /* 159 * Since all the ports under the same HBA have 160 * the same LocalSASAddress, we should break 161 * the loop once we find it dosn't match. 162 */ 163 break; 164 } else { 165 hbaPortFound = 1; 166 } 167 } 168 169 if (chkDomainPort) { 170 if (hba_port_ptr->first_phy != NULL && 171 wwnConversion(hba_port_ptr->first_phy-> 172 phy.domainPortWWN.wwn) == 173 wwnConversion(domainPortWWN.wwn)) { 174 domainPortFound = 1; 175 } 176 if (!(domainPortFound)) { 177 continue; 178 } 179 } 180 181 for (hba_disco_port = hba_port_ptr->first_attached_port; 182 hba_disco_port != NULL; 183 hba_disco_port = hba_disco_port->next) { 184 185 /* 186 * If discoveredPort is not given targetPort, just skip 187 */ 188 if (wwnConversion(hba_disco_port->port_attributes.\ 189 PortSpecificAttribute.SASPort->LocalSASAddress.wwn) 190 != wwnConversion(targetPortWWN.wwn)) { 191 /* Does not match */ 192 continue; 193 } 194 195 /* 196 * If discoveredPort is not a SAS/SATA port, it is not a 197 * target port 198 */ 199 if ((hba_disco_port->port_attributes.PortType != 200 HBA_PORTTYPE_SATADEVICE) && 201 (hba_disco_port->port_attributes.PortType != 202 HBA_PORTTYPE_SASDEVICE)) { 203 unlock(&open_handles_lock); 204 unlock(&all_hbas_lock); 205 log(LOG_DEBUG, ROUTINE, "Target Port WWN " 206 "%016llx on handle %08lx is not a Target", 207 wwnConversion(targetPortWWN.wwn), handle); 208 return (HBA_STATUS_ERROR_NOT_A_TARGET); 209 } 210 211 /* 212 * Iterating and matching is needed. 213 */ 214 for (mapping_ptr = hba_disco_port->scsiInfo; 215 mapping_ptr != NULL; 216 mapping_ptr = mapping_ptr->next) { 217 218 if (memcmp( 219 &mapping_ptr->entry.PortLun.TargetLun, 220 &smhbaLUN, sizeof (HBA_SCSILUN)) 221 != 0) { 222 continue; 223 } 224 225 status = SendScsiInquiry( 226 mapping_ptr->entry.ScsiId.OSDeviceName, 227 cdb1, cdb2, 228 responseBuffer, responseSize, 229 scsiStatus, senseBuffer, 230 senseSize); 231 232 unlock(&open_handles_lock); 233 unlock(&all_hbas_lock); 234 end = gethrtime(); 235 duration = end - start; 236 duration /= HR_SECOND; 237 log(LOG_DEBUG, ROUTINE, "Took total\ 238 of %.4f seconds", duration); 239 return (status); 240 } 241 unlock(&open_handles_lock); 242 unlock(&all_hbas_lock); 243 (void *) memcpy(&hba_lun, &smhbaLUN, 244 sizeof (HBA_SCSILUN)); 245 log(LOG_DEBUG, ROUTINE, "Unable to locate lun" 246 " %08lx for target %016llx on handle %08lx", 247 hba_lun, wwnConversion(targetPortWWN.wwn), handle); 248 return (HBA_STATUS_ERROR_INVALID_LUN); 249 } 250 if (chkDomainPort) { 251 unlock(&open_handles_lock); 252 unlock(&all_hbas_lock); 253 log(LOG_DEBUG, ROUTINE, "Unable to locate requested " 254 "Port WWN %016llx on handle %08lx", 255 wwnConversion(targetPortWWN.wwn), handle); 256 return (HBA_STATUS_ERROR_ILLEGAL_WWN); 257 } 258 } 259 260 unlock(&open_handles_lock); 261 unlock(&all_hbas_lock); 262 if (hbaPortFound == 0) { 263 log(LOG_DEBUG, ROUTINE, 264 "Unable to locate requested Port WWN %016llx on " 265 "handle %08lx", wwnConversion(portWWN.wwn), handle); 266 } else if (chkDomainPort && !domainPortFound) { 267 log(LOG_DEBUG, ROUTINE, "Unable to locate requested" 268 " domainPortWWN %016llx on handle %08lx", 269 wwnConversion(domainPortWWN.wwn), handle); 270 } else { 271 log(LOG_DEBUG, ROUTINE, "Unable to locate requested " 272 "Port WWN %016llx on handle %08lx", 273 wwnConversion(targetPortWWN.wwn), handle); 274 } 275 return (HBA_STATUS_ERROR_ILLEGAL_WWN); 276 } 277