1219820Sjeff/*
2219820Sjeff * Copyright (c) 2004-2006 Voltaire, Inc. All rights reserved.
3219820Sjeff * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4219820Sjeff * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
5219820Sjeff *
6219820Sjeff * This software is available to you under a choice of one of two
7219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
8219820Sjeff * General Public License (GPL) Version 2, available from the file
9219820Sjeff * COPYING in the main directory of this source tree, or the
10219820Sjeff * OpenIB.org BSD license below:
11219820Sjeff *
12219820Sjeff *     Redistribution and use in source and binary forms, with or
13219820Sjeff *     without modification, are permitted provided that the following
14219820Sjeff *     conditions are met:
15219820Sjeff *
16219820Sjeff *      - Redistributions of source code must retain the above
17219820Sjeff *        copyright notice, this list of conditions and the following
18219820Sjeff *        disclaimer.
19219820Sjeff *
20219820Sjeff *      - Redistributions in binary form must reproduce the above
21219820Sjeff *        copyright notice, this list of conditions and the following
22219820Sjeff *        disclaimer in the documentation and/or other materials
23219820Sjeff *        provided with the distribution.
24219820Sjeff *
25219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32219820Sjeff * SOFTWARE.
33219820Sjeff *
34219820Sjeff */
35219820Sjeff
36219820Sjeff#if HAVE_CONFIG_H
37219820Sjeff#  include <config.h>
38219820Sjeff#endif				/* HAVE_CONFIG_H */
39219820Sjeff
40219820Sjeff#include <stdlib.h>
41219820Sjeff#include <string.h>
42219820Sjeff#include <stdio.h>
43219820Sjeff#include <vendor/osm_vendor_mlx_sar.h>
44219820Sjeff
45219820Sjeffib_api_status_t
46219820Sjeffosmv_rmpp_sar_init(osmv_rmpp_sar_t * p_sar, void *p_arbt_mad,
47219820Sjeff		   uint32_t mad_size, boolean_t is_sa_mad)
48219820Sjeff{
49219820Sjeff	CL_ASSERT(p_sar);
50219820Sjeff	p_sar->p_arbt_mad = p_arbt_mad;
51219820Sjeff	if (is_sa_mad) {
52219820Sjeff		p_sar->data_len = mad_size - IB_SA_MAD_HDR_SIZE;
53219820Sjeff		p_sar->hdr_sz = IB_SA_MAD_HDR_SIZE;
54219820Sjeff		p_sar->data_sz = IB_SA_DATA_SIZE;
55219820Sjeff	} else {
56219820Sjeff		p_sar->data_len = mad_size - MAD_RMPP_HDR_SIZE;
57219820Sjeff		p_sar->hdr_sz = MAD_RMPP_HDR_SIZE;
58219820Sjeff		p_sar->data_sz = MAD_RMPP_DATA_SIZE;
59219820Sjeff	}
60219820Sjeff	return IB_SUCCESS;
61219820Sjeff}
62219820Sjeff
63219820Sjeffvoid osmv_rmpp_sar_done(osmv_rmpp_sar_t * p_sar)
64219820Sjeff{
65219820Sjeff	p_sar->p_arbt_mad = NULL;
66219820Sjeff}
67219820Sjeff
68219820Sjeff/* the big mad should be with mad header, rmpp header ( &sa hdr) space */
69219820Sjeffib_api_status_t
70219820Sjeffosmv_rmpp_sar_get_mad_seg(IN osmv_rmpp_sar_t * p_sar,
71219820Sjeff			  IN uint32_t seg_idx, OUT void *p_buf)
72219820Sjeff{
73219820Sjeff	void *p_seg;
74219820Sjeff	uint32_t sz_left;
75219820Sjeff	uint32_t num_segs;
76219820Sjeff
77219820Sjeff	CL_ASSERT(p_sar);
78219820Sjeff
79219820Sjeff	num_segs = p_sar->data_len / p_sar->data_sz;
80219820Sjeff	if ((p_sar->data_len % p_sar->data_sz) > 0) {
81219820Sjeff		num_segs++;
82219820Sjeff	}
83219820Sjeff
84219820Sjeff	if ((seg_idx > num_segs) && (seg_idx != 1)) {
85219820Sjeff		return IB_NOT_FOUND;
86219820Sjeff	}
87219820Sjeff
88219820Sjeff	/* cleanup */
89219820Sjeff	memset(p_buf, 0, MAD_BLOCK_SIZE);
90219820Sjeff
91219820Sjeff	/* attach header */
92219820Sjeff	memcpy(p_buf, p_sar->p_arbt_mad, p_sar->hdr_sz);
93219820Sjeff
94219820Sjeff	/* fill data */
95219820Sjeff	p_seg =
96219820Sjeff	    (char *)p_sar->p_arbt_mad + p_sar->hdr_sz +
97219820Sjeff	    ((seg_idx - 1) * p_sar->data_sz);
98219820Sjeff	sz_left = p_sar->data_len - ((seg_idx - 1) * p_sar->data_sz);
99219820Sjeff	if (sz_left > p_sar->data_sz)
100219820Sjeff		memcpy((char *)p_buf + p_sar->hdr_sz, (char *)p_seg,
101219820Sjeff		       p_sar->data_sz);
102219820Sjeff	else
103219820Sjeff		memcpy((char *)p_buf + p_sar->hdr_sz, (char *)p_seg, sz_left);
104219820Sjeff
105219820Sjeff	return IB_SUCCESS;
106219820Sjeff}
107219820Sjeff
108219820Sjeff/* turns a list of mads to one big mad - including header */
109219820Sjeff/* ALSO - deallocates the list                              */
110219820Sjeffib_api_status_t
111219820Sjeffosmv_rmpp_sar_reassemble_arbt_mad(osmv_rmpp_sar_t * p_sar, cl_qlist_t * p_bufs)
112219820Sjeff{
113219820Sjeff	void *buf_tmp, *p_mad;
114219820Sjeff	cl_list_item_t *p_item;
115219820Sjeff	cl_list_obj_t *p_obj;
116219820Sjeff	uint32_t space_left = p_sar->data_len + p_sar->hdr_sz;
117219820Sjeff
118219820Sjeff	CL_ASSERT(p_sar);
119219820Sjeff	CL_ASSERT(FALSE == cl_is_qlist_empty(p_bufs));
120219820Sjeff
121219820Sjeff	/* attach header */
122219820Sjeff	p_mad = p_sar->p_arbt_mad;
123219820Sjeff	p_item = cl_qlist_head(p_bufs);
124219820Sjeff	p_obj = PARENT_STRUCT(p_item, cl_list_obj_t, list_item);
125219820Sjeff	buf_tmp = cl_qlist_obj(p_obj);
126219820Sjeff	memcpy(p_mad, buf_tmp, p_sar->hdr_sz);
127219820Sjeff	p_mad = (char *)p_mad + p_sar->hdr_sz;
128219820Sjeff	space_left -= p_sar->hdr_sz;
129219820Sjeff
130219820Sjeff	/* reassemble data */
131219820Sjeff	while (FALSE == cl_is_qlist_empty(p_bufs)) {
132219820Sjeff
133219820Sjeff		p_item = cl_qlist_remove_head(p_bufs);
134219820Sjeff		p_obj = PARENT_STRUCT(p_item, cl_list_obj_t, list_item);
135219820Sjeff		buf_tmp = cl_qlist_obj(p_obj);
136219820Sjeff
137219820Sjeff		if (FALSE == cl_is_qlist_empty(p_bufs)) {
138219820Sjeff			memcpy((char *)p_mad, (char *)buf_tmp + p_sar->hdr_sz,
139219820Sjeff			       p_sar->data_sz);
140219820Sjeff			p_mad = (char *)p_mad + p_sar->data_sz;
141219820Sjeff			space_left -= p_sar->data_sz;
142219820Sjeff		} else {
143219820Sjeff			/* the last mad on the list */
144219820Sjeff			memcpy((char *)p_mad, (char *)buf_tmp + p_sar->hdr_sz,
145219820Sjeff			       space_left);
146219820Sjeff			p_mad = (char *)p_mad + space_left;
147219820Sjeff		}
148219820Sjeff
149219820Sjeff		free(buf_tmp);
150219820Sjeff		free(p_obj);
151219820Sjeff	}
152219820Sjeff
153219820Sjeff	return IB_SUCCESS;
154219820Sjeff}
155