xref: /illumos-gate/usr/src/lib/sun_fc/common/Sun_fcGetFcpTargetMapping.cc (revision fcf3ce441efd61da9bb2884968af01cb7c1452cc)
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 2008 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 getFirstAdapterPortWWN(HBA_HANDLE handle) {
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(0);
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 
74 	Trace log("Sun_fcGetFcpTargetMapping");
75 
76 	if (mapping == NULL) {
77 	    log.userError("NULL mapping argument.");
78 	    return (HBA_STATUS_ERROR_ARG);
79 	}
80 	mappingV2 = (PHBA_FCPTARGETMAPPINGV2) new uchar_t[
81 	    (sizeof (HBA_FCPSCSIENTRYV2)*(mapping->NumberOfEntries-1)) +
82 	    sizeof (HBA_FCPTARGETMAPPINGV2)];
83 	mappingV2->NumberOfEntries = mapping->NumberOfEntries;
84 
85 
86 
87 	status = Sun_fcGetFcpTargetMappingV2(handle,
88 	    getFirstAdapterPortWWN(handle), mappingV2);
89 	mapping->NumberOfEntries = mappingV2->NumberOfEntries;
90 	if (status == HBA_STATUS_OK) {
91 		/*
92 		 * need to copy from PHBA_FCPTARGETMAPPINGV2 to
93 		 * PHBA_FCPTARGETMAPPING
94 		 */
95 		for (count = 0; count < mapping->NumberOfEntries; count++) {
96 			memcpy(&mapping->entry[count].ScsiId,
97 			    &mappingV2->entry[count].ScsiId,
98 			    sizeof (mapping->entry[count].ScsiId));
99 			memcpy(&mapping->entry[count].FcpId,
100 			    &mappingV2->entry[count].FcpId,
101 			    sizeof (mapping->entry[count].FcpId));
102 		}
103 	}
104 
105 	delete(mappingV2);
106 	return (status);
107 }
108 #ifdef	__cplusplus
109 }
110 #endif
111