xref: /illumos-gate/usr/src/lib/sun_fc/common/Sun_fcGetFcpTargetMapping.cc (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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
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
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 = mapping->NumberOfEntries;
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 	/* get adapter attributes for number of ports */
87 	status = Sun_fcGetAdapterAttributes(handle,&attributes);
88 	if (status != HBA_STATUS_OK) {
89 		log.userError("Unable to get adapter attributes");
90 		return HBA_STATUS_ERROR;
91 	}
92 
93 	mappingV2 = (PHBA_FCPTARGETMAPPINGV2) new uchar_t[
94 	    (sizeof (HBA_FCPSCSIENTRYV2)*(mapping->NumberOfEntries-1)) +
95 	    sizeof (HBA_FCPTARGETMAPPINGV2)];
96 	mapping->NumberOfEntries = 0;
97 
98 	for(port = 0; port < attributes.NumberOfPorts; port++) {
99 		mappingV2->NumberOfEntries = mapping->NumberOfEntries < entries ?
100 		    entries - mapping->NumberOfEntries : 0 ;
101 		status = Sun_fcGetFcpTargetMappingV2(handle,
102 			getAdapterPortWWN(handle,port), mappingV2);
103 		mapping->NumberOfEntries += mappingV2->NumberOfEntries;
104 
105 		if (status != HBA_STATUS_OK && status != HBA_STATUS_ERROR_MORE_DATA) {
106 				log.userError("Unable to get mappings for port");
107 				return status;
108 		}
109 		/*
110 		 * need to copy from PHBA_FCPTARGETMAPPINGV2 to
111 		 * PHBA_FCPTARGETMAPPING
112 		 */
113 		limit = (mapping->NumberOfEntries < entries) ? mapping->NumberOfEntries : entries;
114 		for (count = current; count < limit; count++) {
115 			memcpy(&mapping->entry[count].ScsiId,
116 				&mappingV2->entry[count-current].ScsiId,
117 			    sizeof (mapping->entry[count].ScsiId));
118 			memcpy(&mapping->entry[count].FcpId,
119 				&mappingV2->entry[count-current].FcpId,
120 			    sizeof (mapping->entry[count].FcpId));
121 		}
122 		current = mapping->NumberOfEntries;
123 	}
124 
125 	delete(mappingV2);
126 	return (status);
127 }
128 #ifdef	__cplusplus
129 }
130 #endif
131