xref: /titanic_41/usr/src/lib/sun_sas/common/Sun_sasGetNumberOfPorts.c (revision 9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bb)
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 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sun_sas.h>
28 
29 /*
30  * Returns the number of HBAs supported by the library.  This returns the
31  * current number of HBAs, even if this changes
32  *
33  */
Sun_sasGetNumberOfPorts(HBA_HANDLE handle,HBA_UINT32 * numberofports)34 HBA_UINT32 Sun_sasGetNumberOfPorts(
35     HBA_HANDLE handle, HBA_UINT32 *numberofports)
36 {
37 	const char		    ROUTINE[] = "Sun_sasGetNumberOfPorts";
38 	int			    count, index;
39 	struct  sun_sas_hba	    *hba_ptr;
40 	struct  sun_sas_port	    *hba_port_ptr;
41 
42 	if (numberofports == NULL) {
43 		log(LOG_DEBUG, ROUTINE, "NULL numberofPorts pointer");
44 		return (HBA_STATUS_ERROR_ARG);
45 	}
46 
47 	lock(&all_hbas_lock);
48 	index = RetrieveIndex(handle);
49 	lock(&open_handles_lock);
50 	hba_ptr = RetrieveHandle(index);
51 	if (hba_ptr == NULL) {
52 		log(LOG_DEBUG, ROUTINE, "Invalid handle %08lx.", handle);
53 		/* on error, need to set NumberOfEntries to 0 */
54 		unlock(&open_handles_lock);
55 		unlock(&all_hbas_lock);
56 		return (HBA_STATUS_ERROR_INVALID_HANDLE);
57 	}
58 	/* goes through hba list counting all the hbas found */
59 	if (hba_ptr->first_port == NULL) {
60 		log(LOG_DEBUG, ROUTINE, "No HBA Port found on handle %08lx.",
61 		    handle);
62 		/* on error, need to set NumberOfPorts to 0 */
63 		*numberofports = 0;
64 		unlock(&open_handles_lock);
65 		unlock(&all_hbas_lock);
66 		return (HBA_STATUS_OK);
67 	}
68 
69 	for (count = 0, hba_port_ptr = hba_ptr->first_port;
70 	    hba_port_ptr != NULL; hba_port_ptr = hba_port_ptr->next, count++) {}
71 
72 	*numberofports = count;
73 
74 	unlock(&open_handles_lock);
75 	unlock(&all_hbas_lock);
76 
77 	return (HBA_STATUS_OK);
78 
79 }
80