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/*
37 * Abstract:
38 *    Implementation of osm_resp_t.
39 * This object represents the generic attribute responder.
40 * This object is part of the opensm family of objects.
41 */
42
43#if HAVE_CONFIG_H
44#  include <config.h>
45#endif				/* HAVE_CONFIG_H */
46
47#include <string.h>
48#include <iba/ib_types.h>
49#include <complib/cl_debug.h>
50#include <opensm/osm_madw.h>
51#include <opensm/osm_attrib_req.h>
52#include <opensm/osm_log.h>
53#include <opensm/osm_helper.h>
54#include <opensm/osm_mad_pool.h>
55#include <opensm/osm_vl15intf.h>
56#include <opensm/osm_msgdef.h>
57#include <opensm/osm_opensm.h>
58
59/**********************************************************************
60 **********************************************************************/
61static void
62osm_resp_make_resp_smp(IN osm_sm_t * sm,
63		       IN const ib_smp_t * const p_src_smp,
64		       IN const ib_net16_t status,
65		       IN const uint8_t * const p_payload,
66		       OUT ib_smp_t * const p_dest_smp)
67{
68	OSM_LOG_ENTER(sm->p_log);
69
70	CL_ASSERT(p_dest_smp);
71	CL_ASSERT(p_src_smp);
72	CL_ASSERT(!ib_smp_is_response(p_src_smp));
73
74	*p_dest_smp = *p_src_smp;
75	if (p_src_smp->method == IB_MAD_METHOD_GET ||
76	    p_src_smp->method == IB_MAD_METHOD_SET) {
77		p_dest_smp->method = IB_MAD_METHOD_GET_RESP;
78		p_dest_smp->status = status;
79	} else if (p_src_smp->method == IB_MAD_METHOD_TRAP) {
80		p_dest_smp->method = IB_MAD_METHOD_TRAP_REPRESS;
81		p_dest_smp->status = 0;
82	} else {
83		OSM_LOG(sm->p_log, OSM_LOG_ERROR, "ERR 1302: "
84			"src smp method unsupported 0x%X\n", p_src_smp->method);
85		goto Exit;
86	}
87
88	if (p_src_smp->mgmt_class == IB_MCLASS_SUBN_DIR)
89		p_dest_smp->status |= IB_SMP_DIRECTION;
90
91	p_dest_smp->dr_dlid = p_dest_smp->dr_slid;
92	p_dest_smp->dr_slid = p_dest_smp->dr_dlid;
93	memcpy(&p_dest_smp->data, p_payload, IB_SMP_DATA_SIZE);
94
95Exit:
96	OSM_LOG_EXIT(sm->p_log);
97}
98
99/**********************************************************************
100 **********************************************************************/
101ib_api_status_t
102osm_resp_send(IN osm_sm_t * sm,
103	      IN const osm_madw_t * const p_req_madw,
104	      IN const ib_net16_t mad_status,
105	      IN const uint8_t * const p_payload)
106{
107	const ib_smp_t *p_req_smp;
108	ib_smp_t *p_smp;
109	osm_madw_t *p_madw;
110	ib_api_status_t status = IB_SUCCESS;
111
112	OSM_LOG_ENTER(sm->p_log);
113
114	CL_ASSERT(p_req_madw);
115	CL_ASSERT(p_payload);
116
117	/* do nothing if we are exiting ... */
118	if (osm_exit_flag)
119		goto Exit;
120
121	p_madw = osm_mad_pool_get(sm->p_mad_pool,
122				  osm_madw_get_bind_handle(p_req_madw),
123				  MAD_BLOCK_SIZE, NULL);
124
125	if (p_madw == NULL) {
126		OSM_LOG(sm->p_log, OSM_LOG_ERROR,
127			"ERR 1301: Unable to acquire MAD\n");
128		status = IB_INSUFFICIENT_RESOURCES;
129		goto Exit;
130	}
131
132	/*
133	   Copy the request smp to the response smp, then just
134	   update the necessary fields.
135	 */
136	p_smp = osm_madw_get_smp_ptr(p_madw);
137	p_req_smp = osm_madw_get_smp_ptr(p_req_madw);
138	osm_resp_make_resp_smp(sm, p_req_smp, mad_status, p_payload, p_smp);
139	p_madw->mad_addr.dest_lid =
140	    p_req_madw->mad_addr.addr_type.smi.source_lid;
141	p_madw->mad_addr.addr_type.smi.source_lid =
142	    p_req_madw->mad_addr.dest_lid;
143
144	p_madw->resp_expected = FALSE;
145	p_madw->fail_msg = CL_DISP_MSGID_NONE;
146
147	OSM_LOG(sm->p_log, OSM_LOG_DEBUG, "Responding to %s (0x%X)"
148		"\n\t\t\t\tattribute modifier 0x%X, TID 0x%" PRIx64 "\n",
149		ib_get_sm_attr_str(p_smp->attr_id), cl_ntoh16(p_smp->attr_id),
150		cl_ntoh32(p_smp->attr_mod), cl_ntoh64(p_smp->trans_id));
151
152	osm_vl15_post(sm->p_vl15, p_madw);
153
154Exit:
155	OSM_LOG_EXIT(sm->p_log);
156	return (status);
157}
158