1*9e86db79SHyon Kim /* 2*9e86db79SHyon Kim * CDDL HEADER START 3*9e86db79SHyon Kim * 4*9e86db79SHyon Kim * The contents of this file are subject to the terms of the 5*9e86db79SHyon Kim * Common Development and Distribution License (the "License"). 6*9e86db79SHyon Kim * You may not use this file except in compliance with the License. 7*9e86db79SHyon Kim * 8*9e86db79SHyon Kim * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*9e86db79SHyon Kim * or http://www.opensolaris.org/os/licensing. 10*9e86db79SHyon Kim * See the License for the specific language governing permissions 11*9e86db79SHyon Kim * and limitations under the License. 12*9e86db79SHyon Kim * 13*9e86db79SHyon Kim * When distributing Covered Code, include this CDDL HEADER in each 14*9e86db79SHyon Kim * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*9e86db79SHyon Kim * If applicable, add the following below this CDDL HEADER, with the 16*9e86db79SHyon Kim * fields enclosed by brackets "[]" replaced with your own identifying 17*9e86db79SHyon Kim * information: Portions Copyright [yyyy] [name of copyright owner] 18*9e86db79SHyon Kim * 19*9e86db79SHyon Kim * CDDL HEADER END 20*9e86db79SHyon Kim */ 21*9e86db79SHyon Kim 22*9e86db79SHyon Kim /* 23*9e86db79SHyon Kim * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24*9e86db79SHyon Kim * Use is subject to license terms. 25*9e86db79SHyon Kim */ 26*9e86db79SHyon Kim 27*9e86db79SHyon Kim #include <sun_sas.h> 28*9e86db79SHyon Kim 29*9e86db79SHyon Kim /* 30*9e86db79SHyon Kim * Pass request buffer into uscsi command and sent it out via ioctl 31*9e86db79SHyon Kim */ 32*9e86db79SHyon Kim static HBA_STATUS 33*9e86db79SHyon Kim SendScsiReadCapacity(const char *devpath, void *responseBuffer, 34*9e86db79SHyon Kim HBA_UINT32 *responseSize, HBA_UINT8 *scsiStatus, 35*9e86db79SHyon Kim void *senseBuffer, HBA_UINT32 *senseSize) 36*9e86db79SHyon Kim { 37*9e86db79SHyon Kim HBA_UINT32 status; 38*9e86db79SHyon Kim struct uscsi_cmd ucmd_buf; 39*9e86db79SHyon Kim union scsi_cdb cdb; 40*9e86db79SHyon Kim 41*9e86db79SHyon Kim bzero(&cdb, sizeof (cdb)); 42*9e86db79SHyon Kim bzero(&ucmd_buf, sizeof (ucmd_buf)); 43*9e86db79SHyon Kim bzero(senseBuffer, *senseSize); 44*9e86db79SHyon Kim bzero(responseBuffer, *responseSize); 45*9e86db79SHyon Kim 46*9e86db79SHyon Kim cdb.scc_cmd = SCMD_READ_CAPACITY; 47*9e86db79SHyon Kim 48*9e86db79SHyon Kim ucmd_buf.uscsi_cdb = (char *)&cdb; 49*9e86db79SHyon Kim ucmd_buf.uscsi_cdblen = CDB_GROUP1; 50*9e86db79SHyon Kim ucmd_buf.uscsi_bufaddr = (caddr_t)responseBuffer; 51*9e86db79SHyon Kim ucmd_buf.uscsi_buflen = *responseSize; 52*9e86db79SHyon Kim ucmd_buf.uscsi_rqbuf = (caddr_t)senseBuffer; 53*9e86db79SHyon Kim ucmd_buf.uscsi_rqlen = *senseSize; 54*9e86db79SHyon Kim ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT | USCSI_RQENABLE; 55*9e86db79SHyon Kim ucmd_buf.uscsi_timeout = 60; 56*9e86db79SHyon Kim 57*9e86db79SHyon Kim status = send_uscsi_cmd(devpath, &ucmd_buf); 58*9e86db79SHyon Kim *scsiStatus = ucmd_buf.uscsi_status; 59*9e86db79SHyon Kim return (status); 60*9e86db79SHyon Kim } 61*9e86db79SHyon Kim 62*9e86db79SHyon Kim /* 63*9e86db79SHyon Kim * Send a read capacity to a remote WWN 64*9e86db79SHyon Kim */ 65*9e86db79SHyon Kim HBA_STATUS 66*9e86db79SHyon Kim Sun_sasScsiReadCapacity(HBA_HANDLE handle, HBA_WWN portWWN, 67*9e86db79SHyon Kim HBA_WWN targetPortWWN, HBA_WWN domainPortWWN, SMHBA_SCSILUN smhbaLUN, 68*9e86db79SHyon Kim void *responseBuffer, HBA_UINT32 *responseSize, 69*9e86db79SHyon Kim HBA_UINT8 *scsiStatus, void *senseBuffer, HBA_UINT32 *senseSize) 70*9e86db79SHyon Kim { 71*9e86db79SHyon Kim const char ROUTINE[] = "Sun_sasScsiReadCapacity"; 72*9e86db79SHyon Kim HBA_STATUS status; 73*9e86db79SHyon Kim int index = 0, domainPortFound = 0; 74*9e86db79SHyon Kim int hbaPortFound = 0; 75*9e86db79SHyon Kim int chkDomainPort = 0; 76*9e86db79SHyon Kim struct sun_sas_hba *hba_ptr = NULL; 77*9e86db79SHyon Kim struct sun_sas_port *hba_port_ptr, *hba_disco_port; 78*9e86db79SHyon Kim struct ScsiEntryList *mapping_ptr; 79*9e86db79SHyon Kim hrtime_t start, end; 80*9e86db79SHyon Kim double duration; 81*9e86db79SHyon Kim HBA_SCSILUN hba_lun; 82*9e86db79SHyon Kim 83*9e86db79SHyon Kim start = gethrtime(); 84*9e86db79SHyon Kim /* Validate the arguments */ 85*9e86db79SHyon Kim if (responseBuffer == NULL) { 86*9e86db79SHyon Kim log(LOG_DEBUG, ROUTINE, "NULL response buffer"); 87*9e86db79SHyon Kim return (HBA_STATUS_ERROR_ARG); 88*9e86db79SHyon Kim } 89*9e86db79SHyon Kim if (senseBuffer == NULL) { 90*9e86db79SHyon Kim log(LOG_DEBUG, ROUTINE, "NULL sense buffer"); 91*9e86db79SHyon Kim return (HBA_STATUS_ERROR_ARG); 92*9e86db79SHyon Kim } 93*9e86db79SHyon Kim if (responseSize == NULL) { 94*9e86db79SHyon Kim log(LOG_DEBUG, ROUTINE, "NULL response size"); 95*9e86db79SHyon Kim return (HBA_STATUS_ERROR_ARG); 96*9e86db79SHyon Kim } 97*9e86db79SHyon Kim if (senseSize == NULL) { 98*9e86db79SHyon Kim log(LOG_DEBUG, ROUTINE, "NULL sense size"); 99*9e86db79SHyon Kim return (HBA_STATUS_ERROR_ARG); 100*9e86db79SHyon Kim } 101*9e86db79SHyon Kim if (scsiStatus == NULL) { 102*9e86db79SHyon Kim log(LOG_DEBUG, ROUTINE, "NULL scsi status"); 103*9e86db79SHyon Kim return (HBA_STATUS_ERROR_ARG); 104*9e86db79SHyon Kim } 105*9e86db79SHyon Kim 106*9e86db79SHyon Kim lock(&all_hbas_lock); 107*9e86db79SHyon Kim index = RetrieveIndex(handle); 108*9e86db79SHyon Kim lock(&open_handles_lock); 109*9e86db79SHyon Kim if ((hba_ptr = RetrieveHandle(index)) == NULL) { 110*9e86db79SHyon Kim log(LOG_DEBUG, ROUTINE, "Invalid handle %08lx", handle); 111*9e86db79SHyon Kim unlock(&open_handles_lock); 112*9e86db79SHyon Kim unlock(&all_hbas_lock); 113*9e86db79SHyon Kim return (HBA_STATUS_ERROR_INVALID_HANDLE); 114*9e86db79SHyon Kim } 115*9e86db79SHyon Kim 116*9e86db79SHyon Kim /* Check for stale data */ 117*9e86db79SHyon Kim status = verifyAdapter(hba_ptr); 118*9e86db79SHyon Kim if (status != HBA_STATUS_OK) { 119*9e86db79SHyon Kim log(LOG_DEBUG, ROUTINE, "Verify adapter failed"); 120*9e86db79SHyon Kim unlock(&open_handles_lock); 121*9e86db79SHyon Kim unlock(&all_hbas_lock); 122*9e86db79SHyon Kim return (status); 123*9e86db79SHyon Kim } 124*9e86db79SHyon Kim 125*9e86db79SHyon Kim if (wwnConversion(domainPortWWN.wwn)) 126*9e86db79SHyon Kim chkDomainPort = 1; 127*9e86db79SHyon Kim 128*9e86db79SHyon Kim /* 129*9e86db79SHyon Kim * We are not checking to see if our data is stale. 130*9e86db79SHyon Kim * By verifying this information here, we will take a big performance 131*9e86db79SHyon Kim * hit. This check will be done later only if the FCSM ioctl fails 132*9e86db79SHyon Kim */ 133*9e86db79SHyon Kim 134*9e86db79SHyon Kim /* Determine which port to use */ 135*9e86db79SHyon Kim for (hba_port_ptr = hba_ptr->first_port; 136*9e86db79SHyon Kim hba_port_ptr != NULL; 137*9e86db79SHyon Kim hba_port_ptr = hba_port_ptr->next) { 138*9e86db79SHyon Kim 139*9e86db79SHyon Kim if (hbaPortFound == 0) { 140*9e86db79SHyon Kim if (wwnConversion(hba_port_ptr->port_attributes. 141*9e86db79SHyon Kim PortSpecificAttribute.SASPort->LocalSASAddress.wwn) 142*9e86db79SHyon Kim != wwnConversion(portWWN.wwn)) { 143*9e86db79SHyon Kim /* 144*9e86db79SHyon Kim * Since all the ports under the same HBA have 145*9e86db79SHyon Kim * the same LocalSASAddress, we should break 146*9e86db79SHyon Kim * the loop once we find it dosn't match. 147*9e86db79SHyon Kim */ 148*9e86db79SHyon Kim break; 149*9e86db79SHyon Kim } else { 150*9e86db79SHyon Kim hbaPortFound = 1; 151*9e86db79SHyon Kim } 152*9e86db79SHyon Kim } 153*9e86db79SHyon Kim 154*9e86db79SHyon Kim if (chkDomainPort != 0) { 155*9e86db79SHyon Kim if (hba_port_ptr->first_phy != NULL && 156*9e86db79SHyon Kim wwnConversion(hba_port_ptr->first_phy-> 157*9e86db79SHyon Kim phy.domainPortWWN.wwn) == 158*9e86db79SHyon Kim wwnConversion(domainPortWWN.wwn)) { 159*9e86db79SHyon Kim domainPortFound = 1; 160*9e86db79SHyon Kim } 161*9e86db79SHyon Kim if (!(domainPortFound)) { 162*9e86db79SHyon Kim continue; 163*9e86db79SHyon Kim } 164*9e86db79SHyon Kim } 165*9e86db79SHyon Kim 166*9e86db79SHyon Kim for (hba_disco_port = hba_port_ptr->first_attached_port; 167*9e86db79SHyon Kim hba_disco_port != NULL; 168*9e86db79SHyon Kim hba_disco_port = hba_disco_port->next) { 169*9e86db79SHyon Kim 170*9e86db79SHyon Kim /* 171*9e86db79SHyon Kim * If discoveredPort is not given targetPort, just skip 172*9e86db79SHyon Kim */ 173*9e86db79SHyon Kim if (wwnConversion(hba_disco_port->port_attributes.\ 174*9e86db79SHyon Kim PortSpecificAttribute.SASPort->LocalSASAddress.wwn) 175*9e86db79SHyon Kim != wwnConversion(targetPortWWN.wwn)) { 176*9e86db79SHyon Kim /* Does not match */ 177*9e86db79SHyon Kim continue; 178*9e86db79SHyon Kim } 179*9e86db79SHyon Kim 180*9e86db79SHyon Kim /* 181*9e86db79SHyon Kim * If discoveredPort is not a SAS/SATA port, it is not a 182*9e86db79SHyon Kim * target port 183*9e86db79SHyon Kim */ 184*9e86db79SHyon Kim if ((hba_disco_port->port_attributes.PortType != 185*9e86db79SHyon Kim HBA_PORTTYPE_SATADEVICE) && 186*9e86db79SHyon Kim (hba_disco_port->port_attributes.PortType != 187*9e86db79SHyon Kim HBA_PORTTYPE_SASDEVICE)) { 188*9e86db79SHyon Kim unlock(&open_handles_lock); 189*9e86db79SHyon Kim unlock(&all_hbas_lock); 190*9e86db79SHyon Kim log(LOG_DEBUG, ROUTINE, "Target Port WWN " 191*9e86db79SHyon Kim "%016llx on handle %08lx is not a Target", 192*9e86db79SHyon Kim wwnConversion(targetPortWWN.wwn), handle); 193*9e86db79SHyon Kim return (HBA_STATUS_ERROR_NOT_A_TARGET); 194*9e86db79SHyon Kim } 195*9e86db79SHyon Kim 196*9e86db79SHyon Kim /* 197*9e86db79SHyon Kim * Iterating and matching is needed. 198*9e86db79SHyon Kim */ 199*9e86db79SHyon Kim for (mapping_ptr = hba_disco_port->scsiInfo; 200*9e86db79SHyon Kim mapping_ptr != NULL; 201*9e86db79SHyon Kim mapping_ptr = mapping_ptr->next) { 202*9e86db79SHyon Kim 203*9e86db79SHyon Kim if (memcmp( 204*9e86db79SHyon Kim &mapping_ptr->entry.PortLun.TargetLun, 205*9e86db79SHyon Kim &smhbaLUN, sizeof (HBA_SCSILUN)) 206*9e86db79SHyon Kim != 0) { 207*9e86db79SHyon Kim continue; 208*9e86db79SHyon Kim } 209*9e86db79SHyon Kim 210*9e86db79SHyon Kim status = SendScsiReadCapacity( 211*9e86db79SHyon Kim mapping_ptr->entry.ScsiId.\ 212*9e86db79SHyon Kim OSDeviceName, 213*9e86db79SHyon Kim responseBuffer, responseSize, 214*9e86db79SHyon Kim scsiStatus, senseBuffer, senseSize); 215*9e86db79SHyon Kim 216*9e86db79SHyon Kim unlock(&open_handles_lock); 217*9e86db79SHyon Kim unlock(&all_hbas_lock); 218*9e86db79SHyon Kim end = gethrtime(); 219*9e86db79SHyon Kim duration = end - start; 220*9e86db79SHyon Kim duration /= HR_SECOND; 221*9e86db79SHyon Kim log(LOG_DEBUG, ROUTINE, "Took total\ 222*9e86db79SHyon Kim of %.4f seconds", duration); 223*9e86db79SHyon Kim return (status); 224*9e86db79SHyon Kim } 225*9e86db79SHyon Kim unlock(&open_handles_lock); 226*9e86db79SHyon Kim unlock(&all_hbas_lock); 227*9e86db79SHyon Kim (void *) memcpy(&hba_lun, &smhbaLUN, 228*9e86db79SHyon Kim sizeof (HBA_SCSILUN)); 229*9e86db79SHyon Kim log(LOG_DEBUG, ROUTINE, "Unable to locate lun" 230*9e86db79SHyon Kim " %08lx for target %016llx on handle %08lx", 231*9e86db79SHyon Kim hba_lun, wwnConversion(targetPortWWN.wwn), handle); 232*9e86db79SHyon Kim return (HBA_STATUS_ERROR_INVALID_LUN); 233*9e86db79SHyon Kim } 234*9e86db79SHyon Kim 235*9e86db79SHyon Kim if (chkDomainPort) { 236*9e86db79SHyon Kim unlock(&open_handles_lock); 237*9e86db79SHyon Kim unlock(&all_hbas_lock); 238*9e86db79SHyon Kim log(LOG_DEBUG, ROUTINE, "Unable to locate requested " 239*9e86db79SHyon Kim "Port WWN %016llx on handle %08lx", 240*9e86db79SHyon Kim wwnConversion(targetPortWWN.wwn), handle); 241*9e86db79SHyon Kim return (HBA_STATUS_ERROR_ILLEGAL_WWN); 242*9e86db79SHyon Kim } 243*9e86db79SHyon Kim } 244*9e86db79SHyon Kim 245*9e86db79SHyon Kim unlock(&open_handles_lock); 246*9e86db79SHyon Kim unlock(&all_hbas_lock); 247*9e86db79SHyon Kim if (hbaPortFound == 0) { 248*9e86db79SHyon Kim log(LOG_DEBUG, ROUTINE, 249*9e86db79SHyon Kim "Unable to locate requested Port WWN %016llx on " 250*9e86db79SHyon Kim "handle %08lx", wwnConversion(portWWN.wwn), handle); 251*9e86db79SHyon Kim } else if (chkDomainPort && !domainPortFound) { 252*9e86db79SHyon Kim log(LOG_DEBUG, ROUTINE, "Unable to locate requested" 253*9e86db79SHyon Kim " domainPortWWN %016llx on handle %08lx", 254*9e86db79SHyon Kim wwnConversion(domainPortWWN.wwn), handle); 255*9e86db79SHyon Kim } else { 256*9e86db79SHyon Kim log(LOG_DEBUG, ROUTINE, "Unable to locate requested " 257*9e86db79SHyon Kim "Port WWN %016llx on handle %08lx", 258*9e86db79SHyon Kim wwnConversion(targetPortWWN.wwn), handle); 259*9e86db79SHyon Kim } 260*9e86db79SHyon Kim return (HBA_STATUS_ERROR_ILLEGAL_WWN); 261*9e86db79SHyon Kim } 262