1 /*
2  * Copyright (c) 2004-2008 Voltaire, Inc. All rights reserved.
3  * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4  * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  *
34  */
35 
36 #if HAVE_CONFIG_H
37 #  include <config.h>
38 #endif				/* HAVE_CONFIG_H */
39 
40 #if defined(OSM_VENDOR_INTF_ANAFA)
41 #undef IN
42 #undef OUT
43 
44 #include <stdlib.h>
45 #include <stddef.h>
46 #include <string.h>
47 
48 #include <vendor/osm_vendor_api.h>
49 #include <opensm/osm_log.h>
50 #include <sys/ioctl.h>
51 
52 #include <vendor/osm_vendor_mlx_transport_anafa.h>
53 #include <vendor/osm_ts_useraccess.h>
54 
55 /********************************************************************************
56  *
57  * Provide the functionality for selecting an HCA Port and Obtaining it's guid.
58  *
59  ********************************************************************************/
60 
61 typedef struct _osm_ca_info {
62 	/* ib_net64_t guid; ?? */
63 	/* size_t attr_size; ?? */
64 	ib_ca_attr_t attr;
65 } osm_ca_info_t;
66 
67 /**********************************************************************
68  * Convert the given GID to GUID by copy of it's upper 8 bytes
69  **********************************************************************/
70 ib_api_status_t
71 __osm_vendor_gid_to_guid(IN tTS_IB_GID gid, OUT ib_net64_t * p_guid)
72 {
73 	memcpy(p_guid, gid + 8, 8);
74 	return (IB_SUCCESS);
75 }
76 
77 /**********************************************************************
78  * Initialize an Info Struct for the Given HCA by its Id
79  **********************************************************************/
80 static ib_api_status_t
81 __osm_ca_info_init(IN osm_vendor_t * const p_vend,
82 		   OUT osm_ca_info_t * const p_ca_info)
83 {
84 	ib_api_status_t status = IB_ERROR;
85 	int ioctl_ret = 0;
86 	osmv_TOPSPIN_ANAFA_transport_info_t *p_tpot_info =
87 	    p_vend->p_transport_info;
88 	osm_ts_gid_entry_ioctl gid_ioctl;
89 	osm_ts_get_port_info_ioctl port_info;
90 	struct ib_get_dev_info_ioctl dev_info;
91 
92 	OSM_LOG_ENTER(p_vend->p_log);
93 
94 	/* query HCA guid */
95 	ioctl_ret = ioctl(p_tpot_info->device_fd, TS_IB_IOCGDEVINFO, &dev_info);
96 	if (ioctl_ret != 0) {
97 		osm_log(p_vend->p_log, OSM_LOG_ERROR,
98 			"__osm_ca_info_init: ERR 7001: "
99 			"Fail to get HCA Capabilities (%d).\n", ioctl_ret);
100 		goto Exit;
101 	}
102 
103 	memcpy(&(p_ca_info->attr.ca_guid), dev_info.dev_info.node_guid,
104 	       8 * sizeof(uint8_t));
105 
106 /* now obtain the attributes of the ports - on our case port 1*/
107 
108 	p_ca_info->attr.num_ports = 1;
109 	p_ca_info->attr.p_port_attr =
110 	    (ib_port_attr_t *) malloc(1 * sizeof(ib_port_attr_t));
111 
112 	port_info.port = 1;
113 	ioctl_ret =
114 	    ioctl(p_tpot_info->device_fd, TS_IB_IOCGPORTINFO, &port_info);
115 	if (ioctl_ret) {
116 		osm_log(p_vend->p_log, OSM_LOG_ERROR,
117 			"__osm_ca_info_init: ERR 7002: "
118 			"Fail to get HCA Port Attributes (%d).\n", ioctl_ret);
119 		goto Exit;
120 	}
121 
122 	gid_ioctl.port = 1;
123 	gid_ioctl.index = 0;
124 	ioctl_ret =
125 	    ioctl(p_tpot_info->device_fd, TS_IB_IOCGGIDENTRY, &gid_ioctl);
126 	if (ioctl_ret) {
127 		osm_log(p_vend->p_log, OSM_LOG_ERROR,
128 			"__osm_ca_info_init: ERR 7003: "
129 			"Fail to get HCA Port GID (%d).\n", ioctl_ret);
130 		goto Exit;
131 	}
132 
133 	__osm_vendor_gid_to_guid(gid_ioctl.gid_entry,
134 				 &(p_ca_info->attr.p_port_attr[0].port_guid));
135 	p_ca_info->attr.p_port_attr[0].lid = port_info.port_info.lid;
136 	p_ca_info->attr.p_port_attr[0].link_state =
137 	    port_info.port_info.port_state;
138 	p_ca_info->attr.p_port_attr[0].sm_lid = port_info.port_info.sm_lid;
139 
140 	status = IB_SUCCESS;
141 Exit:
142 	OSM_LOG_EXIT(p_vend->p_log);
143 	return (status);
144 }
145 
146 /**********************************************************************
147  * Fill in port_attr
148  * ALSO -
149  * Update the vendor object list of ca_info structs
150  **********************************************************************/
151 ib_api_status_t
152 osm_vendor_get_all_port_attr(IN osm_vendor_t * const p_vend,
153 			     IN ib_port_attr_t * const p_attr_array,
154 			     IN uint32_t * const p_num_ports)
155 {
156 	ib_api_status_t status;
157 	osm_ca_info_t ca_info;
158 	uint32_t attr_array_sz = *p_num_ports;
159 
160 	OSM_LOG_ENTER(p_vend->p_log);
161 	CL_ASSERT(p_vend);
162 
163 	/* anafa has one port - the user didnt supply enough storage space */
164 	if (attr_array_sz < 1) {
165 		status = IB_INSUFFICIENT_MEMORY;
166 		goto Exit;
167 	}
168 
169 	/*
170 	 * retrieve the CA info attributes
171 	 */
172 	status = __osm_ca_info_init(p_vend, &ca_info);
173 	if (status != IB_SUCCESS) {
174 		osm_log(p_vend->p_log, OSM_LOG_ERROR,
175 			"osm_vendor_get_all_port_attr: ERR 7004: "
176 			"Unable to initialize CA Info object (%s).\n",
177 			ib_get_err_str(status));
178 		goto Exit;
179 	}
180 
181 	*p_num_ports = 1;
182 
183 	p_attr_array[0] = ca_info.attr.p_port_attr[0];	/* anafa has only one port */
184 	status = IB_SUCCESS;
185 
186 Exit:
187 
188 	OSM_LOG_EXIT(p_vend->p_log);
189 	return (status);
190 }
191 
192 #endif
193