17836SJohn.Forte@Sun.COM /*
27836SJohn.Forte@Sun.COM  * CDDL HEADER START
37836SJohn.Forte@Sun.COM  *
47836SJohn.Forte@Sun.COM  * The contents of this file are subject to the terms of the
57836SJohn.Forte@Sun.COM  * Common Development and Distribution License (the "License").
67836SJohn.Forte@Sun.COM  * You may not use this file except in compliance with the License.
77836SJohn.Forte@Sun.COM  *
87836SJohn.Forte@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97836SJohn.Forte@Sun.COM  * or http://www.opensolaris.org/os/licensing.
107836SJohn.Forte@Sun.COM  * See the License for the specific language governing permissions
117836SJohn.Forte@Sun.COM  * and limitations under the License.
127836SJohn.Forte@Sun.COM  *
137836SJohn.Forte@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
147836SJohn.Forte@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157836SJohn.Forte@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
167836SJohn.Forte@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
177836SJohn.Forte@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
187836SJohn.Forte@Sun.COM  *
197836SJohn.Forte@Sun.COM  * CDDL HEADER END
207836SJohn.Forte@Sun.COM  */
217836SJohn.Forte@Sun.COM /*
2210155SDuo.Liu@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237836SJohn.Forte@Sun.COM  * Use is subject to license terms.
247836SJohn.Forte@Sun.COM  */
257836SJohn.Forte@Sun.COM 
267836SJohn.Forte@Sun.COM 
277836SJohn.Forte@Sun.COM 
287836SJohn.Forte@Sun.COM #include "Trace.h"
297836SJohn.Forte@Sun.COM #include "Exceptions.h"
307836SJohn.Forte@Sun.COM #include "sun_fc.h"
317836SJohn.Forte@Sun.COM 
327836SJohn.Forte@Sun.COM 
337836SJohn.Forte@Sun.COM 
347836SJohn.Forte@Sun.COM #include <string.h>
357836SJohn.Forte@Sun.COM #include "Handle.h"
367836SJohn.Forte@Sun.COM #include "HBA.h"
377836SJohn.Forte@Sun.COM #include "HBAPort.h"
387836SJohn.Forte@Sun.COM inline HBA_WWN
getAdapterPortWWN(HBA_HANDLE handle,HBA_UINT32 index)3910155SDuo.Liu@Sun.COM getAdapterPortWWN(HBA_HANDLE handle,HBA_UINT32 index) {
407836SJohn.Forte@Sun.COM 	HBA_WWN hba_wwn;
417836SJohn.Forte@Sun.COM 	memset(hba_wwn.wwn, 0, sizeof (hba_wwn));
427836SJohn.Forte@Sun.COM 	try {
437836SJohn.Forte@Sun.COM 	    Handle *myHandle = Handle::findHandle(handle);
447836SJohn.Forte@Sun.COM 	    HBA *hba = myHandle->getHBA();
4510155SDuo.Liu@Sun.COM 		HBAPort *port = hba->getPortByIndex(index);
467836SJohn.Forte@Sun.COM 	    uint64_t tmp = htonll(port->getPortWWN());
477836SJohn.Forte@Sun.COM 	    memcpy(hba_wwn.wwn, &tmp, sizeof (hba_wwn));
487836SJohn.Forte@Sun.COM 	} catch (...) { }
497836SJohn.Forte@Sun.COM 	return (hba_wwn);
507836SJohn.Forte@Sun.COM }
517836SJohn.Forte@Sun.COM 
527836SJohn.Forte@Sun.COM #ifdef	__cplusplus
537836SJohn.Forte@Sun.COM extern "C" {
547836SJohn.Forte@Sun.COM #endif
557836SJohn.Forte@Sun.COM 
567836SJohn.Forte@Sun.COM /**
577836SJohn.Forte@Sun.COM  * @memo	    Retrieves the mapping between FCP targets and OS
587836SJohn.Forte@Sun.COM  *		    SCSI information
597836SJohn.Forte@Sun.COM  * @return	    HBA_STATUS_OK if the mapping structure contains valid
607836SJohn.Forte@Sun.COM  *		    mapping data.
617836SJohn.Forte@Sun.COM  * @param	    handle The HBA to fetch mappings for
627836SJohn.Forte@Sun.COM  * @param	    mapping The user-allocated mapping structure
637836SJohn.Forte@Sun.COM  *
647836SJohn.Forte@Sun.COM  * @doc		    This routine will call the V2 interface and convert
657836SJohn.Forte@Sun.COM  *		    the results to the old data structure.  It will
667836SJohn.Forte@Sun.COM  *		    call the V2 interface for all ports on the HBA.
677836SJohn.Forte@Sun.COM  */
687836SJohn.Forte@Sun.COM HBA_STATUS
Sun_fcGetFcpTargetMapping(HBA_HANDLE handle,PHBA_FCPTARGETMAPPING mapping)697836SJohn.Forte@Sun.COM Sun_fcGetFcpTargetMapping(HBA_HANDLE handle, PHBA_FCPTARGETMAPPING mapping) {
707836SJohn.Forte@Sun.COM 	HBA_STATUS		    status;
717836SJohn.Forte@Sun.COM 	int			    count;
727836SJohn.Forte@Sun.COM 	PHBA_FCPTARGETMAPPINGV2	    mappingV2;
7310155SDuo.Liu@Sun.COM 	HBA_ADAPTERATTRIBUTES       attributes;
74*10469SDuo.Liu@Sun.COM 	HBA_UINT32                  entries = 0;
7510155SDuo.Liu@Sun.COM 	HBA_UINT32                  current = 0;
7610155SDuo.Liu@Sun.COM 	HBA_UINT32                  port;
7710155SDuo.Liu@Sun.COM 	HBA_UINT32                  limit;
787836SJohn.Forte@Sun.COM 
797836SJohn.Forte@Sun.COM 	Trace log("Sun_fcGetFcpTargetMapping");
807836SJohn.Forte@Sun.COM 
817836SJohn.Forte@Sun.COM 	if (mapping == NULL) {
827836SJohn.Forte@Sun.COM 	    log.userError("NULL mapping argument.");
837836SJohn.Forte@Sun.COM 	    return (HBA_STATUS_ERROR_ARG);
847836SJohn.Forte@Sun.COM 	}
8510155SDuo.Liu@Sun.COM 
86*10469SDuo.Liu@Sun.COM 	entries = mapping->NumberOfEntries;
87*10469SDuo.Liu@Sun.COM 
8810155SDuo.Liu@Sun.COM 	/* get adapter attributes for number of ports */
8910155SDuo.Liu@Sun.COM 	status = Sun_fcGetAdapterAttributes(handle,&attributes);
9010155SDuo.Liu@Sun.COM 	if (status != HBA_STATUS_OK) {
9110155SDuo.Liu@Sun.COM 		log.userError("Unable to get adapter attributes");
9210155SDuo.Liu@Sun.COM 		return HBA_STATUS_ERROR;
9310155SDuo.Liu@Sun.COM 	}
9410155SDuo.Liu@Sun.COM 
957836SJohn.Forte@Sun.COM 	mappingV2 = (PHBA_FCPTARGETMAPPINGV2) new uchar_t[
967836SJohn.Forte@Sun.COM 	    (sizeof (HBA_FCPSCSIENTRYV2)*(mapping->NumberOfEntries-1)) +
977836SJohn.Forte@Sun.COM 	    sizeof (HBA_FCPTARGETMAPPINGV2)];
9810155SDuo.Liu@Sun.COM 	mapping->NumberOfEntries = 0;
997836SJohn.Forte@Sun.COM 
10010155SDuo.Liu@Sun.COM 	for(port = 0; port < attributes.NumberOfPorts; port++) {
10110155SDuo.Liu@Sun.COM 		mappingV2->NumberOfEntries = mapping->NumberOfEntries < entries ?
10210155SDuo.Liu@Sun.COM 		    entries - mapping->NumberOfEntries : 0 ;
10310155SDuo.Liu@Sun.COM 		status = Sun_fcGetFcpTargetMappingV2(handle,
10410155SDuo.Liu@Sun.COM 			getAdapterPortWWN(handle,port), mappingV2);
10510155SDuo.Liu@Sun.COM 		mapping->NumberOfEntries += mappingV2->NumberOfEntries;
10610155SDuo.Liu@Sun.COM 
10710155SDuo.Liu@Sun.COM 		if (status != HBA_STATUS_OK && status != HBA_STATUS_ERROR_MORE_DATA) {
10810155SDuo.Liu@Sun.COM 				log.userError("Unable to get mappings for port");
10910155SDuo.Liu@Sun.COM 				return status;
11010155SDuo.Liu@Sun.COM 		}
1117836SJohn.Forte@Sun.COM 		/*
1127836SJohn.Forte@Sun.COM 		 * need to copy from PHBA_FCPTARGETMAPPINGV2 to
1137836SJohn.Forte@Sun.COM 		 * PHBA_FCPTARGETMAPPING
1147836SJohn.Forte@Sun.COM 		 */
11510155SDuo.Liu@Sun.COM 		limit = (mapping->NumberOfEntries < entries) ? mapping->NumberOfEntries : entries;
11610155SDuo.Liu@Sun.COM 		for (count = current; count < limit; count++) {
1177836SJohn.Forte@Sun.COM 			memcpy(&mapping->entry[count].ScsiId,
11810155SDuo.Liu@Sun.COM 				&mappingV2->entry[count-current].ScsiId,
1197836SJohn.Forte@Sun.COM 			    sizeof (mapping->entry[count].ScsiId));
1207836SJohn.Forte@Sun.COM 			memcpy(&mapping->entry[count].FcpId,
12110155SDuo.Liu@Sun.COM 				&mappingV2->entry[count-current].FcpId,
1227836SJohn.Forte@Sun.COM 			    sizeof (mapping->entry[count].FcpId));
1237836SJohn.Forte@Sun.COM 		}
12410155SDuo.Liu@Sun.COM 		current = mapping->NumberOfEntries;
1257836SJohn.Forte@Sun.COM 	}
1267836SJohn.Forte@Sun.COM 
1277836SJohn.Forte@Sun.COM 	delete(mappingV2);
1287836SJohn.Forte@Sun.COM 	return (status);
1297836SJohn.Forte@Sun.COM }
1307836SJohn.Forte@Sun.COM #ifdef	__cplusplus
1317836SJohn.Forte@Sun.COM }
1327836SJohn.Forte@Sun.COM #endif
133