xref: /onnv-gate/usr/src/lib/sun_sas/common/devtree_phy_disco.c (revision 10652:9d0aff74d6fd)
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 #include <sys/types.h>
29*10652SHyon.Kim@Sun.COM #include <sys/stat.h>
30*10652SHyon.Kim@Sun.COM #include <fcntl.h>
31*10652SHyon.Kim@Sun.COM #include <unistd.h>
32*10652SHyon.Kim@Sun.COM #include <libdevinfo.h>
33*10652SHyon.Kim@Sun.COM #include <netinet/in.h>
34*10652SHyon.Kim@Sun.COM #include <inttypes.h>
35*10652SHyon.Kim@Sun.COM 
36*10652SHyon.Kim@Sun.COM /*
37*10652SHyon.Kim@Sun.COM  * structure for di_devlink_walk
38*10652SHyon.Kim@Sun.COM  */
39*10652SHyon.Kim@Sun.COM typedef struct walk_devlink {
40*10652SHyon.Kim@Sun.COM 	char *path;
41*10652SHyon.Kim@Sun.COM 	size_t len;
42*10652SHyon.Kim@Sun.COM 	char **linkpp;
43*10652SHyon.Kim@Sun.COM } walk_devlink_t;
44*10652SHyon.Kim@Sun.COM 
45*10652SHyon.Kim@Sun.COM /*
46*10652SHyon.Kim@Sun.COM  * Free the phy allocation.
47*10652SHyon.Kim@Sun.COM  */
48*10652SHyon.Kim@Sun.COM static void
free_phy_info(struct sun_sas_port * port_ptr)49*10652SHyon.Kim@Sun.COM free_phy_info(struct sun_sas_port *port_ptr)
50*10652SHyon.Kim@Sun.COM {
51*10652SHyon.Kim@Sun.COM 	struct phy_info *phy_ptr, *last_phy;
52*10652SHyon.Kim@Sun.COM 
53*10652SHyon.Kim@Sun.COM 	phy_ptr = port_ptr->first_phy;
54*10652SHyon.Kim@Sun.COM 	while (phy_ptr != NULL) {
55*10652SHyon.Kim@Sun.COM 		last_phy = phy_ptr;
56*10652SHyon.Kim@Sun.COM 		phy_ptr = phy_ptr->next;
57*10652SHyon.Kim@Sun.COM 		free(last_phy);
58*10652SHyon.Kim@Sun.COM 	}
59*10652SHyon.Kim@Sun.COM 
60*10652SHyon.Kim@Sun.COM 	port_ptr->first_phy = NULL;
61*10652SHyon.Kim@Sun.COM 
62*10652SHyon.Kim@Sun.COM }
63*10652SHyon.Kim@Sun.COM 
64*10652SHyon.Kim@Sun.COM /*
65*10652SHyon.Kim@Sun.COM  * callback funtion for di_devlink_walk
66*10652SHyon.Kim@Sun.COM  * Find matching /dev link for the given path argument.
67*10652SHyon.Kim@Sun.COM  * devlink element and callback function argument.
68*10652SHyon.Kim@Sun.COM  * The input path is expected to not have "/devices".
69*10652SHyon.Kim@Sun.COM  */
70*10652SHyon.Kim@Sun.COM extern HBA_STATUS
get_phy_info(di_node_t node,struct sun_sas_port * port_ptr)71*10652SHyon.Kim@Sun.COM get_phy_info(di_node_t node, struct sun_sas_port *port_ptr)
72*10652SHyon.Kim@Sun.COM {
73*10652SHyon.Kim@Sun.COM 	const char ROUTINE[] = "get_phy_info";
74*10652SHyon.Kim@Sun.COM 	char *portDevpath = NULL;
75*10652SHyon.Kim@Sun.COM 	uchar_t	*propByteData = NULL;
76*10652SHyon.Kim@Sun.COM 	struct phy_info *phy_ptr;
77*10652SHyon.Kim@Sun.COM 	uint_t nvcount;
78*10652SHyon.Kim@Sun.COM 	int rval, count, i;
79*10652SHyon.Kim@Sun.COM 	nvlist_t *nvl, **phyInfoVal;
80*10652SHyon.Kim@Sun.COM 	uint8_t phyId;
81*10652SHyon.Kim@Sun.COM 	int8_t negoRate, prgmMinRate, prgmMaxRate, hwMinRate, hwMaxRate;
82*10652SHyon.Kim@Sun.COM 
83*10652SHyon.Kim@Sun.COM 	/*
84*10652SHyon.Kim@Sun.COM 	 * When path is specified, it doesn't have minor
85*10652SHyon.Kim@Sun.COM 	 * name. Therefore, the ../.. prefixes needs to be stripped.
86*10652SHyon.Kim@Sun.COM 	 */
87*10652SHyon.Kim@Sun.COM 	if ((portDevpath = di_devfs_path(node)) == NULL) {
88*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE,
89*10652SHyon.Kim@Sun.COM 		"Unable to get device path from portNode.");
90*10652SHyon.Kim@Sun.COM 	}
91*10652SHyon.Kim@Sun.COM 
92*10652SHyon.Kim@Sun.COM 	count = di_prop_lookup_bytes(DDI_DEV_T_ANY, node, "phy-info",
93*10652SHyon.Kim@Sun.COM 	    (uchar_t **)&propByteData);
94*10652SHyon.Kim@Sun.COM 	if (count < 0) {
95*10652SHyon.Kim@Sun.COM 		if (portDevpath) {
96*10652SHyon.Kim@Sun.COM 			log(LOG_DEBUG, ROUTINE,
97*10652SHyon.Kim@Sun.COM 			    "Property phy-info not found on port %s%s",
98*10652SHyon.Kim@Sun.COM 			    DEVICES_DIR, portDevpath);
99*10652SHyon.Kim@Sun.COM 			di_devfs_path_free(portDevpath);
100*10652SHyon.Kim@Sun.COM 		} else {
101*10652SHyon.Kim@Sun.COM 			log(LOG_DEBUG, ROUTINE, "Property phy-info not found.");
102*10652SHyon.Kim@Sun.COM 		}
103*10652SHyon.Kim@Sun.COM 		return (HBA_STATUS_ERROR);
104*10652SHyon.Kim@Sun.COM 	} else {
105*10652SHyon.Kim@Sun.COM 		rval = nvlist_unpack((char *)propByteData, count, &nvl, 0);
106*10652SHyon.Kim@Sun.COM 		if (rval != 0) {
107*10652SHyon.Kim@Sun.COM 			if (portDevpath) {
108*10652SHyon.Kim@Sun.COM 				log(LOG_DEBUG, ROUTINE,
109*10652SHyon.Kim@Sun.COM 				    "nvlist_unpack failed on port %s%s",
110*10652SHyon.Kim@Sun.COM 				    DEVICES_DIR, portDevpath);
111*10652SHyon.Kim@Sun.COM 				di_devfs_path_free(portDevpath);
112*10652SHyon.Kim@Sun.COM 			} else {
113*10652SHyon.Kim@Sun.COM 				log(LOG_DEBUG, ROUTINE,
114*10652SHyon.Kim@Sun.COM 				    "nvlist_unpack failed.");
115*10652SHyon.Kim@Sun.COM 			}
116*10652SHyon.Kim@Sun.COM 			return (HBA_STATUS_ERROR);
117*10652SHyon.Kim@Sun.COM 		} else {
118*10652SHyon.Kim@Sun.COM 			rval = nvlist_lookup_nvlist_array(nvl, "phy-info-nvl",
119*10652SHyon.Kim@Sun.COM 			    &phyInfoVal, &nvcount);
120*10652SHyon.Kim@Sun.COM 			if (rval != 0) {
121*10652SHyon.Kim@Sun.COM 				if (portDevpath) {
122*10652SHyon.Kim@Sun.COM 					log(LOG_DEBUG, ROUTINE,
123*10652SHyon.Kim@Sun.COM 					    "nvlist array phy-info-nvl not\
124*10652SHyon.Kim@Sun.COM 					    found on port %s%s", DEVICES_DIR,
125*10652SHyon.Kim@Sun.COM 					    portDevpath);
126*10652SHyon.Kim@Sun.COM 					di_devfs_path_free(portDevpath);
127*10652SHyon.Kim@Sun.COM 				} else {
128*10652SHyon.Kim@Sun.COM 					log(LOG_DEBUG, ROUTINE,
129*10652SHyon.Kim@Sun.COM 					    "nvlist array phy-info-nvl not\
130*10652SHyon.Kim@Sun.COM 					    found");
131*10652SHyon.Kim@Sun.COM 				}
132*10652SHyon.Kim@Sun.COM 				nvlist_free(nvl);
133*10652SHyon.Kim@Sun.COM 				return (HBA_STATUS_ERROR);
134*10652SHyon.Kim@Sun.COM 			} else {
135*10652SHyon.Kim@Sun.COM 		/* indentation moved */
136*10652SHyon.Kim@Sun.COM 		for (i = 0; i < nvcount; i++) {
137*10652SHyon.Kim@Sun.COM 			if (nvlist_lookup_uint8(phyInfoVal[i],
138*10652SHyon.Kim@Sun.COM 			    "PhyIdentifier", &phyId) != 0) {
139*10652SHyon.Kim@Sun.COM 				/* Indicate a failure : no better way to set */
140*10652SHyon.Kim@Sun.COM 				phyId = 0xff;
141*10652SHyon.Kim@Sun.COM 			}
142*10652SHyon.Kim@Sun.COM 			if (nvlist_lookup_int8(phyInfoVal[i],
143*10652SHyon.Kim@Sun.COM 			    "NegotiatedLinkRate", &negoRate) != 0) {
144*10652SHyon.Kim@Sun.COM 				negoRate = HBA_SASSTATE_UNKNOWN;
145*10652SHyon.Kim@Sun.COM 			}
146*10652SHyon.Kim@Sun.COM 			if (nvlist_lookup_int8(phyInfoVal[i],
147*10652SHyon.Kim@Sun.COM 			    "ProgrammedMinLinkRate", &prgmMinRate) != 0) {
148*10652SHyon.Kim@Sun.COM 				prgmMinRate = HBA_SASSTATE_UNKNOWN;
149*10652SHyon.Kim@Sun.COM 			}
150*10652SHyon.Kim@Sun.COM 			if (nvlist_lookup_int8(phyInfoVal[i],
151*10652SHyon.Kim@Sun.COM 			    "ProgrammedMaxLinkRate", &prgmMaxRate) != 0) {
152*10652SHyon.Kim@Sun.COM 				prgmMaxRate = HBA_SASSTATE_UNKNOWN;
153*10652SHyon.Kim@Sun.COM 			}
154*10652SHyon.Kim@Sun.COM 			if (nvlist_lookup_int8(phyInfoVal[i],
155*10652SHyon.Kim@Sun.COM 			    "HardwareMinLinkRate", &hwMinRate) != 0) {
156*10652SHyon.Kim@Sun.COM 				hwMinRate = HBA_SASSTATE_UNKNOWN;
157*10652SHyon.Kim@Sun.COM 			}
158*10652SHyon.Kim@Sun.COM 			if (nvlist_lookup_int8(phyInfoVal[i],
159*10652SHyon.Kim@Sun.COM 			    "HardwareMaxLinkRate", &hwMaxRate) != 0) {
160*10652SHyon.Kim@Sun.COM 				hwMaxRate = HBA_SASSTATE_UNKNOWN;
161*10652SHyon.Kim@Sun.COM 			}
162*10652SHyon.Kim@Sun.COM 
163*10652SHyon.Kim@Sun.COM 			if ((phy_ptr = (struct phy_info *)calloc(1,
164*10652SHyon.Kim@Sun.COM 			    sizeof (struct phy_info))) == NULL)  {
165*10652SHyon.Kim@Sun.COM 				OUT_OF_MEMORY(ROUTINE);
166*10652SHyon.Kim@Sun.COM 				if (portDevpath)
167*10652SHyon.Kim@Sun.COM 					di_devfs_path_free(portDevpath);
168*10652SHyon.Kim@Sun.COM 				free_phy_info(port_ptr);
169*10652SHyon.Kim@Sun.COM 				nvlist_free(nvl);
170*10652SHyon.Kim@Sun.COM 				return (HBA_STATUS_ERROR);
171*10652SHyon.Kim@Sun.COM 			}
172*10652SHyon.Kim@Sun.COM 			phy_ptr->phy.PhyIdentifier = phyId;
173*10652SHyon.Kim@Sun.COM 			phy_ptr->phy.NegotiatedLinkRate = negoRate;
174*10652SHyon.Kim@Sun.COM 			phy_ptr->phy.ProgrammedMinLinkRate = prgmMinRate;
175*10652SHyon.Kim@Sun.COM 			phy_ptr->phy.ProgrammedMaxLinkRate = prgmMaxRate;
176*10652SHyon.Kim@Sun.COM 			phy_ptr->phy.HardwareMinLinkRate = hwMinRate;
177*10652SHyon.Kim@Sun.COM 			phy_ptr->phy.HardwareMaxLinkRate = hwMaxRate;
178*10652SHyon.Kim@Sun.COM 			/*
179*10652SHyon.Kim@Sun.COM 			 * we will fill domain port later.
180*10652SHyon.Kim@Sun.COM 			 */
181*10652SHyon.Kim@Sun.COM 			(void) memset(phy_ptr->phy.domainPortWWN.wwn, 0, 8);
182*10652SHyon.Kim@Sun.COM 			phy_ptr->index = i;
183*10652SHyon.Kim@Sun.COM 			if (port_ptr->first_phy == NULL) {
184*10652SHyon.Kim@Sun.COM 				port_ptr->first_phy = phy_ptr;
185*10652SHyon.Kim@Sun.COM 			} else {
186*10652SHyon.Kim@Sun.COM 				phy_ptr->next = port_ptr->first_phy;
187*10652SHyon.Kim@Sun.COM 				port_ptr->first_phy = phy_ptr;
188*10652SHyon.Kim@Sun.COM 			}
189*10652SHyon.Kim@Sun.COM 
190*10652SHyon.Kim@Sun.COM 		}
191*10652SHyon.Kim@Sun.COM 		nvlist_free(nvl);
192*10652SHyon.Kim@Sun.COM 		/* end of indentation move */
193*10652SHyon.Kim@Sun.COM 			}
194*10652SHyon.Kim@Sun.COM 		}
195*10652SHyon.Kim@Sun.COM 	}
196*10652SHyon.Kim@Sun.COM 
197*10652SHyon.Kim@Sun.COM 	if (portDevpath) {
198*10652SHyon.Kim@Sun.COM 		di_devfs_path_free(portDevpath);
199*10652SHyon.Kim@Sun.COM 	}
200*10652SHyon.Kim@Sun.COM 
201*10652SHyon.Kim@Sun.COM 	return (HBA_STATUS_OK);
202*10652SHyon.Kim@Sun.COM }
203