1*10652SHyon.Kim@Sun.COM /*
2*10652SHyon.Kim@Sun.COM  * CDDL HEADER START
3*10652SHyon.Kim@Sun.COM  *
4*10652SHyon.Kim@Sun.COM  * The contents of this file are subject to the terms of the
5*10652SHyon.Kim@Sun.COM  * Common Development and Distribution License (the "License").
6*10652SHyon.Kim@Sun.COM  * You may not use this file except in compliance with the License.
7*10652SHyon.Kim@Sun.COM  *
8*10652SHyon.Kim@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*10652SHyon.Kim@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*10652SHyon.Kim@Sun.COM  * See the License for the specific language governing permissions
11*10652SHyon.Kim@Sun.COM  * and limitations under the License.
12*10652SHyon.Kim@Sun.COM  *
13*10652SHyon.Kim@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*10652SHyon.Kim@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*10652SHyon.Kim@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*10652SHyon.Kim@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*10652SHyon.Kim@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*10652SHyon.Kim@Sun.COM  *
19*10652SHyon.Kim@Sun.COM  * CDDL HEADER END
20*10652SHyon.Kim@Sun.COM  */
21*10652SHyon.Kim@Sun.COM 
22*10652SHyon.Kim@Sun.COM /*
23*10652SHyon.Kim@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24*10652SHyon.Kim@Sun.COM  * Use is subject to license terms.
25*10652SHyon.Kim@Sun.COM  */
26*10652SHyon.Kim@Sun.COM 
27*10652SHyon.Kim@Sun.COM #include    <sun_sas.h>
28*10652SHyon.Kim@Sun.COM 
29*10652SHyon.Kim@Sun.COM /*
30*10652SHyon.Kim@Sun.COM  * Retrieves the attributes for a specified port discovered in the network
31*10652SHyon.Kim@Sun.COM  */
32*10652SHyon.Kim@Sun.COM HBA_STATUS
Sun_sasGetDiscoveredPortAttributes(HBA_HANDLE handle,HBA_UINT32 port,HBA_UINT32 discoveredport,SMHBA_PORTATTRIBUTES * attributes)33*10652SHyon.Kim@Sun.COM Sun_sasGetDiscoveredPortAttributes(HBA_HANDLE handle,
34*10652SHyon.Kim@Sun.COM 	    HBA_UINT32 port, HBA_UINT32 discoveredport,
35*10652SHyon.Kim@Sun.COM 	    SMHBA_PORTATTRIBUTES *attributes) {
36*10652SHyon.Kim@Sun.COM 	const char		ROUTINE[] =
37*10652SHyon.Kim@Sun.COM 	    "Sun_sasGetDiscoveredPortAttributes";
38*10652SHyon.Kim@Sun.COM 	HBA_STATUS		status;
39*10652SHyon.Kim@Sun.COM 	HBA_STATUS		ret = HBA_STATUS_OK;
40*10652SHyon.Kim@Sun.COM 	struct sun_sas_hba	*hba_ptr;
41*10652SHyon.Kim@Sun.COM 	struct sun_sas_port	*hba_port_ptr, *hba_disco_port;
42*10652SHyon.Kim@Sun.COM 	int			index;
43*10652SHyon.Kim@Sun.COM 
44*10652SHyon.Kim@Sun.COM 	if (attributes == NULL) {
45*10652SHyon.Kim@Sun.COM 	    log(LOG_DEBUG, ROUTINE,
46*10652SHyon.Kim@Sun.COM 		"NULL attributes argument. Handle %08lx, port %d, "
47*10652SHyon.Kim@Sun.COM 		"discovered port %d", handle, port, discoveredport);
48*10652SHyon.Kim@Sun.COM 	    return (HBA_STATUS_ERROR_ARG);
49*10652SHyon.Kim@Sun.COM 	}
50*10652SHyon.Kim@Sun.COM 
51*10652SHyon.Kim@Sun.COM 	lock(&all_hbas_lock);
52*10652SHyon.Kim@Sun.COM 	index = RetrieveIndex(handle);
53*10652SHyon.Kim@Sun.COM 	lock(&open_handles_lock);
54*10652SHyon.Kim@Sun.COM 	hba_ptr = RetrieveHandle(index);
55*10652SHyon.Kim@Sun.COM 	if (hba_ptr == NULL) {
56*10652SHyon.Kim@Sun.COM 	    log(LOG_DEBUG, ROUTINE,
57*10652SHyon.Kim@Sun.COM 		"Invalid handle %08lx.", handle);
58*10652SHyon.Kim@Sun.COM 	    unlock(&open_handles_lock);
59*10652SHyon.Kim@Sun.COM 	    unlock(&all_hbas_lock);
60*10652SHyon.Kim@Sun.COM 	    return (HBA_STATUS_ERROR_INVALID_HANDLE);
61*10652SHyon.Kim@Sun.COM 	}
62*10652SHyon.Kim@Sun.COM 
63*10652SHyon.Kim@Sun.COM 	/* Check for stale data */
64*10652SHyon.Kim@Sun.COM 	status = verifyAdapter(hba_ptr);
65*10652SHyon.Kim@Sun.COM 	if (status != HBA_STATUS_OK) {
66*10652SHyon.Kim@Sun.COM 	    log(LOG_DEBUG, ROUTINE, "Verify Adapter failed");
67*10652SHyon.Kim@Sun.COM 	    unlock(&open_handles_lock);
68*10652SHyon.Kim@Sun.COM 	    unlock(&all_hbas_lock);
69*10652SHyon.Kim@Sun.COM 	    return (status);
70*10652SHyon.Kim@Sun.COM 	}
71*10652SHyon.Kim@Sun.COM 
72*10652SHyon.Kim@Sun.COM 
73*10652SHyon.Kim@Sun.COM 	if (hba_ptr->first_port == NULL) {
74*10652SHyon.Kim@Sun.COM 	    /* This is probably an internal failure of the library */
75*10652SHyon.Kim@Sun.COM 	    if (hba_ptr->device_path) {
76*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE,
77*10652SHyon.Kim@Sun.COM 		    "Internal failure:  Adapter %s contains no port data",
78*10652SHyon.Kim@Sun.COM 		    hba_ptr->device_path);
79*10652SHyon.Kim@Sun.COM 	    } else {
80*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE,
81*10652SHyon.Kim@Sun.COM 		    "Internal failure:  Adapter at index %d contains no port "
82*10652SHyon.Kim@Sun.COM 		    "data", hba_ptr->index);
83*10652SHyon.Kim@Sun.COM 	    }
84*10652SHyon.Kim@Sun.COM 	    unlock(&open_handles_lock);
85*10652SHyon.Kim@Sun.COM 	    unlock(&all_hbas_lock);
86*10652SHyon.Kim@Sun.COM 	    return (HBA_STATUS_ERROR);
87*10652SHyon.Kim@Sun.COM 	}
88*10652SHyon.Kim@Sun.COM 
89*10652SHyon.Kim@Sun.COM 	for (hba_port_ptr = hba_ptr->first_port;
90*10652SHyon.Kim@Sun.COM 	    hba_port_ptr != NULL; hba_port_ptr = hba_port_ptr->next) {
91*10652SHyon.Kim@Sun.COM 		if (hba_port_ptr->index == port) {
92*10652SHyon.Kim@Sun.COM 			break;
93*10652SHyon.Kim@Sun.COM 		}
94*10652SHyon.Kim@Sun.COM 	}
95*10652SHyon.Kim@Sun.COM 
96*10652SHyon.Kim@Sun.COM 	if (hba_port_ptr == NULL) {
97*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE,
98*10652SHyon.Kim@Sun.COM 		    "Invalid port index %d for handle %08lx",
99*10652SHyon.Kim@Sun.COM 		    port, handle);
100*10652SHyon.Kim@Sun.COM 		unlock(&open_handles_lock);
101*10652SHyon.Kim@Sun.COM 		unlock(&all_hbas_lock);
102*10652SHyon.Kim@Sun.COM 		return (HBA_STATUS_ERROR_ILLEGAL_INDEX);
103*10652SHyon.Kim@Sun.COM 	}
104*10652SHyon.Kim@Sun.COM 
105*10652SHyon.Kim@Sun.COM 	/* check to make sure there are devices attached to this port */
106*10652SHyon.Kim@Sun.COM 	if (hba_port_ptr->first_attached_port != NULL) {
107*10652SHyon.Kim@Sun.COM 		for (hba_disco_port = hba_port_ptr->first_attached_port;
108*10652SHyon.Kim@Sun.COM 			hba_disco_port != NULL;
109*10652SHyon.Kim@Sun.COM 			hba_disco_port = hba_disco_port->next) {
110*10652SHyon.Kim@Sun.COM 		    if (hba_disco_port->index == discoveredport) {
111*10652SHyon.Kim@Sun.COM 			break;
112*10652SHyon.Kim@Sun.COM 		    }
113*10652SHyon.Kim@Sun.COM 		}
114*10652SHyon.Kim@Sun.COM 		if (hba_disco_port == NULL) {
115*10652SHyon.Kim@Sun.COM 			log(LOG_DEBUG, ROUTINE,
116*10652SHyon.Kim@Sun.COM 			    "Invalid discovered port index %d for hba port "
117*10652SHyon.Kim@Sun.COM 			    "index %d on handle %08lx.",
118*10652SHyon.Kim@Sun.COM 			    discoveredport, port, handle);
119*10652SHyon.Kim@Sun.COM 			ret = HBA_STATUS_ERROR_ILLEGAL_INDEX;
120*10652SHyon.Kim@Sun.COM 		} else {
121*10652SHyon.Kim@Sun.COM 		    attributes->PortType =
122*10652SHyon.Kim@Sun.COM 			hba_disco_port->port_attributes.PortType;
123*10652SHyon.Kim@Sun.COM 		    attributes->PortState =
124*10652SHyon.Kim@Sun.COM 			hba_disco_port->port_attributes.PortState;
125*10652SHyon.Kim@Sun.COM 		    (void) strlcpy(attributes->OSDeviceName,
126*10652SHyon.Kim@Sun.COM 			hba_disco_port->port_attributes.OSDeviceName,
127*10652SHyon.Kim@Sun.COM 			sizeof (attributes->OSDeviceName));
128*10652SHyon.Kim@Sun.COM 		    (void) memcpy(attributes->PortSpecificAttribute.SASPort,
129*10652SHyon.Kim@Sun.COM 			hba_disco_port->port_attributes.PortSpecificAttribute.
130*10652SHyon.Kim@Sun.COM 			SASPort, sizeof (struct SMHBA_SAS_Port));
131*10652SHyon.Kim@Sun.COM 		}
132*10652SHyon.Kim@Sun.COM 	} else {
133*10652SHyon.Kim@Sun.COM 		/* No ports, so we can't possibly return anything */
134*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE,
135*10652SHyon.Kim@Sun.COM 		    "No discovered port on HBA port index %d for handle %08lx",
136*10652SHyon.Kim@Sun.COM 		    port, handle);
137*10652SHyon.Kim@Sun.COM 		ret = HBA_STATUS_ERROR;
138*10652SHyon.Kim@Sun.COM 	}
139*10652SHyon.Kim@Sun.COM 	unlock(&open_handles_lock);
140*10652SHyon.Kim@Sun.COM 	unlock(&all_hbas_lock);
141*10652SHyon.Kim@Sun.COM 
142*10652SHyon.Kim@Sun.COM 	return (ret);
143*10652SHyon.Kim@Sun.COM }
144