Sun_sasGetTargetMapping.c revision 10652:9d0aff74d6fd
1234285Sdim/*
2234285Sdim * CDDL HEADER START
3234285Sdim *
4234285Sdim * The contents of this file are subject to the terms of the
5234285Sdim * Common Development and Distribution License (the "License").
6234285Sdim * You may not use this file except in compliance with the License.
7234285Sdim *
8234285Sdim * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9234285Sdim * or http://www.opensolaris.org/os/licensing.
10234285Sdim * See the License for the specific language governing permissions
11234285Sdim * and limitations under the License.
12234285Sdim *
13234285Sdim * When distributing Covered Code, include this CDDL HEADER in each
14234285Sdim * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15234285Sdim * If applicable, add the following below this CDDL HEADER, with the
16239462Sdim * fields enclosed by brackets "[]" replaced with your own identifying
17234285Sdim * information: Portions Copyright [yyyy] [name of copyright owner]
18234285Sdim *
19234285Sdim * CDDL HEADER END
20234285Sdim */
21234285Sdim
22234285Sdim/*
23234285Sdim * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24234285Sdim * Use is subject to license terms.
25234285Sdim */
26234285Sdim
27234285Sdim#include    <sun_sas.h>
28234285Sdim
29234285Sdim/*
30234285Sdim * Retrieves the mapping between targets and OS SCSI information
31234285Sdim */
32249423SdimHBA_STATUS
33249423SdimSun_sasGetTargetMapping(HBA_HANDLE handle, HBA_WWN hbaPortWWN,
34249423Sdim    HBA_WWN domainPortWWN, SMHBA_TARGETMAPPING *mapping)
35234285Sdim{
36239462Sdim	const char		ROUTINE[] = "Sun_sasGetTargetMapping";
37249423Sdim	int			i, index;
38249423Sdim	int			hbaPortFound = 0;
39249423Sdim	int			domainPortFound = 0;
40249423Sdim	uint_t			total_entries = 0;
41249423Sdim	struct  sun_sas_hba	*hba_ptr;
42249423Sdim	struct  sun_sas_port	*hba_port_ptr, *hba_disco_port;
43249423Sdim	struct	ScsiEntryList	*mapping_ptr;
44239462Sdim
45239462Sdim	if (mapping == NULL) {
46239462Sdim		log(LOG_DEBUG, ROUTINE, "NULL mapping buffer");
47239462Sdim		return (HBA_STATUS_ERROR_ARG);
48239462Sdim	}
49234285Sdim
50234285Sdim	lock(&all_hbas_lock);
51234285Sdim	index = RetrieveIndex(handle);
52234285Sdim	lock(&open_handles_lock);
53243830Sdim	hba_ptr = RetrieveHandle(index);
54243830Sdim	if (hba_ptr == NULL) {
55243830Sdim		log(LOG_DEBUG, ROUTINE, "Invalid handle %08lx.", handle);
56243830Sdim		/* on error, need to set NumberOfEntries to 0 */
57243830Sdim		mapping->NumberOfEntries = 0;
58243830Sdim		unlock(&open_handles_lock);
59243830Sdim		unlock(&all_hbas_lock);
60234285Sdim		return (HBA_STATUS_ERROR_INVALID_HANDLE);
61243830Sdim	}
62243830Sdim
63243830Sdim	/*
64243830Sdim	 * We should indicate an error if no domainPortWWN passed in.
65243830Sdim	 */
66243830Sdim	if (wwnConversion(domainPortWWN.wwn) == 0) {
67243830Sdim		log(LOG_DEBUG, ROUTINE, "domainPortWWN must be provided");
68234285Sdim		mapping->NumberOfEntries = 0;
69234285Sdim		unlock(&open_handles_lock);
70243830Sdim		unlock(&all_hbas_lock);
71243830Sdim		return (HBA_STATUS_ERROR_ARG);
72234285Sdim	}
73234285Sdim	/*
74234285Sdim	 * walk through the list of ports for this hba and count up the number
75249423Sdim	 * of discovered ports on each hba port
76249423Sdim	 */
77249423Sdim	i = 0;
78249423Sdim	for (hba_port_ptr = hba_ptr->first_port; hba_port_ptr != NULL;
79234285Sdim	    hba_port_ptr = hba_port_ptr->next) {
80234285Sdim		if (hbaPortFound == 0) {
81234285Sdim			if (wwnConversion(hba_port_ptr->port_attributes.
82239462Sdim			    PortSpecificAttribute.SASPort->LocalSASAddress.wwn)
83239462Sdim			    != wwnConversion(hbaPortWWN.wwn)) {
84239462Sdim				/*
85239462Sdim				 * Since all the ports under the same HBA have
86239462Sdim				 * the same LocalSASAddress, we should break
87234285Sdim				 * the loop once we find it dosn't match.
88239462Sdim				 */
89263508Sdim				break;
90263508Sdim			} else {
91				hbaPortFound = 1;
92			}
93		}
94
95		/*
96		 * Check whether the domainPortWWN matches.
97		 */
98		if ((validateDomainAddress(hba_port_ptr, domainPortWWN))
99		    != HBA_STATUS_OK) {
100			continue;
101		}
102		domainPortFound = 1;
103
104		for (hba_disco_port = hba_port_ptr->first_attached_port;
105		    hba_disco_port != NULL;
106		    hba_disco_port = hba_disco_port->next) {
107			for (mapping_ptr = hba_disco_port->scsiInfo;
108			    mapping_ptr != NULL;
109			    mapping_ptr = mapping_ptr->next) {
110				/*
111				 * Add the information as much as mapping
112				 * can hold.
113				 */
114				if (wwnConversion(domainPortWWN.wwn) !=
115				    wwnConversion(mapping_ptr->entry.
116				    PortLun.domainPortWWN.wwn)) {
117					continue;
118				}
119
120				if (total_entries < mapping->NumberOfEntries) {
121					(void) memcpy(&mapping->entry[i].ScsiId,
122					    &mapping_ptr->entry.ScsiId,
123					    sizeof (SMHBA_SCSIID));
124					(void) memcpy(&mapping->entry[i].
125					    PortLun, &mapping_ptr->entry.
126					    PortLun, sizeof (SMHBA_PORTLUN));
127					(void) memcpy(&mapping->entry[i].LUID,
128					    &mapping_ptr->entry.LUID,
129					    sizeof (SMHBA_LUID));
130					i++;
131				}
132				total_entries++;
133			}
134		}
135	}
136
137	/*
138	 * check to make sure user has passed in an acceptable PortWWN for
139	 * the given handle
140	 */
141	if (hbaPortFound == 0) {
142		log(LOG_DEBUG, ROUTINE, "Unable to locate requested "
143		    "HBA Port WWN %016llx on handle %08lx",
144		    wwnConversion(hbaPortWWN.wwn), handle);
145		unlock(&open_handles_lock);
146		unlock(&all_hbas_lock);
147		return (HBA_STATUS_ERROR_ILLEGAL_WWN);
148	}
149
150	if (domainPortFound == 0) {
151		log(LOG_DEBUG, ROUTINE, "No matching domain "
152		    "port %016llx for port %016llx on handle "
153		    "%08lx", wwnConversion(domainPortWWN.wwn),
154		    wwnConversion(hbaPortWWN.wwn), handle);
155		unlock(&open_handles_lock);
156		unlock(&all_hbas_lock);
157		return (HBA_STATUS_ERROR_ILLEGAL_WWN);
158	}
159
160	if (total_entries > mapping->NumberOfEntries) {
161		log(LOG_DEBUG, ROUTINE,
162		    "total entries: %d: mapping->NumberofEntries: %d.",
163		    total_entries, mapping->NumberOfEntries);
164		mapping->NumberOfEntries = total_entries;
165		unlock(&open_handles_lock);
166		unlock(&all_hbas_lock);
167		return (HBA_STATUS_ERROR_MORE_DATA);
168	}
169
170	mapping->NumberOfEntries = total_entries;
171
172	/* convert devpath to dev link */
173	convertDevpathToDevlink(mapping);
174
175	unlock(&open_handles_lock);
176	unlock(&all_hbas_lock);
177
178	return (HBA_STATUS_OK);
179}
180