xref: /illumos-gate/usr/src/lib/sun_sas/common/Sun_sasGetAdapterPortAttributes.c (revision aedf2b3bb56b025fcaf87b49ec6c8aeea07f16d7)
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  * Retrieves the attributes for a specified port of an adapter
31  */
32 HBA_STATUS
33 Sun_sasGetAdapterPortAttributes(HBA_HANDLE handle,
34 	    HBA_UINT32 port, PSMHBA_PORTATTRIBUTES attributes) {
35 	const char		ROUTINE[] = "Sun_sasGetAdapterPortAttributes";
36 	HBA_STATUS		status;
37 	struct sun_sas_hba	*hba_ptr;
38 	struct sun_sas_port	*hba_port_ptr;
39 	int			index;
40 
41 	/* Validate the arguments */
42 	if ((attributes == NULL) ||
43 	    (attributes->PortSpecificAttribute.SASPort == 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 		unlock(&open_handles_lock);
55 		unlock(&all_hbas_lock);
56 		return (HBA_STATUS_ERROR_INVALID_HANDLE);
57 	}
58 
59 	/* Check for stale data */
60 	status = verifyAdapter(hba_ptr);
61 	if (status != HBA_STATUS_OK) {
62 		log(LOG_DEBUG, ROUTINE, "Verify Adapter failed");
63 		unlock(&open_handles_lock);
64 		unlock(&all_hbas_lock);
65 		return (status);
66 	}
67 
68 	if (hba_ptr->first_port == NULL) {
69 	    /* This is probably an internal failure of the library */
70 	    if (hba_ptr->device_path) {
71 		log(LOG_DEBUG, ROUTINE,
72 		    "Internal failure:  Adapter %s contains no port data",
73 		    hba_ptr->device_path);
74 	    } else {
75 		log(LOG_DEBUG, ROUTINE,
76 		    "Internal failure:  Adapter at index %d contains no port "
77 		    "data", hba_ptr->index);
78 	    }
79 	    unlock(&open_handles_lock);
80 	    unlock(&all_hbas_lock);
81 	    return (HBA_STATUS_ERROR);
82 	}
83 	for (hba_port_ptr = hba_ptr->first_port;
84 	    hba_port_ptr != NULL; hba_port_ptr = hba_port_ptr->next) {
85 		if (hba_port_ptr->index == port) {
86 			break;
87 		}
88 	}
89 	if (hba_port_ptr == NULL || hba_port_ptr->index != port) {
90 		log(LOG_DEBUG, ROUTINE,
91 		    "Invalid port index %d for handle %08lx.",
92 		    port, handle);
93 		unlock(&open_handles_lock);
94 		unlock(&all_hbas_lock);
95 		return (HBA_STATUS_ERROR_ILLEGAL_INDEX);
96 	}
97 
98 	attributes->PortType =  hba_port_ptr->port_attributes.PortType;
99 	attributes->PortState =  hba_port_ptr->port_attributes.PortState;
100 	(void) strlcpy(attributes->OSDeviceName,
101 	    hba_port_ptr->port_attributes.OSDeviceName,
102 	    sizeof (attributes->OSDeviceName));
103 	(void) memcpy(attributes->PortSpecificAttribute.SASPort,
104 	    hba_port_ptr->port_attributes.PortSpecificAttribute.SASPort,
105 	    sizeof (struct SMHBA_SAS_Port));
106 
107 	unlock(&open_handles_lock);
108 	unlock(&all_hbas_lock);
109 
110 	return (HBA_STATUS_OK);
111 }
112