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  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 
27 
28 #include "Trace.h"
29 #include "Exceptions.h"
30 #include "sun_fc.h"
31 
32 
33 
34 #include <string.h>
35 #include "Handle.h"
36 #include "HBA.h"
37 #include "HBAPort.h"
38 inline HBA_WWN
getAdapterPortWWN(HBA_HANDLE handle,HBA_UINT32 index)39 getAdapterPortWWN(HBA_HANDLE handle,HBA_UINT32 index) {
40 	HBA_WWN hba_wwn;
41 	memset(hba_wwn.wwn, 0, sizeof (hba_wwn));
42 	try {
43 	    Handle *myHandle = Handle::findHandle(handle);
44 	    HBA *hba = myHandle->getHBA();
45 		HBAPort *port = hba->getPortByIndex(index);
46 	    uint64_t tmp = htonll(port->getPortWWN());
47 	    memcpy(hba_wwn.wwn, &tmp, sizeof (hba_wwn));
48 	} catch (...) { }
49 	return (hba_wwn);
50 }
51 
52 #ifdef	__cplusplus
53 extern "C" {
54 #endif
55 
56 /**
57  * @memo	    Retrieves the mapping between FCP targets and OS
58  *		    SCSI information
59  * @return	    HBA_STATUS_OK if the mapping structure contains valid
60  *		    mapping data.
61  * @param	    handle The HBA to fetch mappings for
62  * @param	    mapping The user-allocated mapping structure
63  *
64  * @doc		    This routine will call the V2 interface and convert
65  *		    the results to the old data structure.  It will
66  *		    call the V2 interface for all ports on the HBA.
67  */
68 HBA_STATUS
Sun_fcGetFcpTargetMapping(HBA_HANDLE handle,PHBA_FCPTARGETMAPPING mapping)69 Sun_fcGetFcpTargetMapping(HBA_HANDLE handle, PHBA_FCPTARGETMAPPING mapping) {
70 	HBA_STATUS		    status;
71 	int			    count;
72 	PHBA_FCPTARGETMAPPINGV2	    mappingV2;
73 	HBA_ADAPTERATTRIBUTES       attributes;
74 	HBA_UINT32                  entries = 0;
75 	HBA_UINT32                  current = 0;
76 	HBA_UINT32                  port;
77 	HBA_UINT32                  limit;
78 
79 	Trace log("Sun_fcGetFcpTargetMapping");
80 
81 	if (mapping == NULL) {
82 	    log.userError("NULL mapping argument.");
83 	    return (HBA_STATUS_ERROR_ARG);
84 	}
85 
86 	entries = mapping->NumberOfEntries;
87 
88 	/* get adapter attributes for number of ports */
89 	status = Sun_fcGetAdapterAttributes(handle,&attributes);
90 	if (status != HBA_STATUS_OK) {
91 		log.userError("Unable to get adapter attributes");
92 		return HBA_STATUS_ERROR;
93 	}
94 
95 	mappingV2 = (PHBA_FCPTARGETMAPPINGV2) new uchar_t[
96 	    (sizeof (HBA_FCPSCSIENTRYV2)*(mapping->NumberOfEntries-1)) +
97 	    sizeof (HBA_FCPTARGETMAPPINGV2)];
98 	mapping->NumberOfEntries = 0;
99 
100 	for(port = 0; port < attributes.NumberOfPorts; port++) {
101 		mappingV2->NumberOfEntries = mapping->NumberOfEntries < entries ?
102 		    entries - mapping->NumberOfEntries : 0 ;
103 		status = Sun_fcGetFcpTargetMappingV2(handle,
104 			getAdapterPortWWN(handle,port), mappingV2);
105 		mapping->NumberOfEntries += mappingV2->NumberOfEntries;
106 
107 		if (status != HBA_STATUS_OK && status != HBA_STATUS_ERROR_MORE_DATA) {
108 				log.userError("Unable to get mappings for port");
109 				return status;
110 		}
111 		/*
112 		 * need to copy from PHBA_FCPTARGETMAPPINGV2 to
113 		 * PHBA_FCPTARGETMAPPING
114 		 */
115 		limit = (mapping->NumberOfEntries < entries) ? mapping->NumberOfEntries : entries;
116 		for (count = current; count < limit; count++) {
117 			memcpy(&mapping->entry[count].ScsiId,
118 				&mappingV2->entry[count-current].ScsiId,
119 			    sizeof (mapping->entry[count].ScsiId));
120 			memcpy(&mapping->entry[count].FcpId,
121 				&mappingV2->entry[count-current].FcpId,
122 			    sizeof (mapping->entry[count].FcpId));
123 		}
124 		current = mapping->NumberOfEntries;
125 	}
126 
127 	delete[](mappingV2);
128 	return (status);
129 }
130 #ifdef	__cplusplus
131 }
132 #endif
133