xref: /illumos-gate/usr/src/lib/sun_sas/common/Sun_sasGetPortType.c (revision b6805bf78d2bbbeeaea8909a05623587b42d58b3)
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  */
34 HBA_UINT32 Sun_sasGetPortType(HBA_HANDLE handle, HBA_UINT32 port,
35     HBA_PORTTYPE *porttype)
36 {
37 	const char		    ROUTINE[] = "Sun_sasGetPortType";
38 	int			    index;
39 	struct  sun_sas_hba	    *hba_ptr;
40 	struct  sun_sas_port	    *hba_port_ptr;
41 
42 	/* Validate the arguments */
43 	if (porttype == NULL) {
44 		log(LOG_DEBUG, ROUTINE, "NULL attributes.");
45 		return (HBA_STATUS_ERROR_ARG);
46 	}
47 
48 	lock(&all_hbas_lock);
49 	index = RetrieveIndex(handle);
50 	lock(&open_handles_lock);
51 	hba_ptr = RetrieveHandle(index);
52 	if (hba_ptr == NULL) {
53 		log(LOG_DEBUG, ROUTINE, "Invalid handle %08lx.", handle);
54 		/* on error, need to set NumberOfEntries to 0 */
55 		unlock(&open_handles_lock);
56 		unlock(&all_hbas_lock);
57 		return (HBA_STATUS_ERROR_INVALID_HANDLE);
58 	}
59 
60 	if (hba_ptr->first_port == NULL) {
61 	    /* This is probably an internal failure of the library */
62 		if (hba_ptr->device_path) {
63 			log(LOG_DEBUG, ROUTINE,
64 			    "Internal failure:  Adapter %s contains no port "
65 			    "data.", hba_ptr->device_path);
66 		} else {
67 			log(LOG_DEBUG, ROUTINE,
68 			    "Internal failure:  Adapter at index %d contains "
69 			    "no port data", hba_ptr->index);
70 		}
71 		unlock(&open_handles_lock);
72 		unlock(&all_hbas_lock);
73 		return (HBA_STATUS_ERROR);
74 	}
75 
76 	for (hba_port_ptr = hba_ptr->first_port;
77 	    hba_port_ptr != NULL; hba_port_ptr = hba_port_ptr->next) {
78 		if (hba_port_ptr->index == port) {
79 			break;
80 		}
81 	}
82 
83 	if (hba_port_ptr == NULL || hba_port_ptr->index != port) {
84 		log(LOG_DEBUG, ROUTINE,
85 		    "Invalid port index %d for handle %08lx.",
86 		    port, handle);
87 		unlock(&open_handles_lock);
88 		unlock(&all_hbas_lock);
89 		return (HBA_STATUS_ERROR_ILLEGAL_INDEX);
90 	}
91 
92 	*porttype = HBA_PORTTYPE_SASDEVICE;
93 
94 	unlock(&open_handles_lock);
95 	unlock(&all_hbas_lock);
96 
97 	return (HBA_STATUS_OK);
98 }
99