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 service record functions.
39 */
40
41#if HAVE_CONFIG_H
42#  include <config.h>
43#endif				/* HAVE_CONFIG_H */
44
45#include <stdlib.h>
46#include <complib/cl_debug.h>
47#include <complib/cl_timer.h>
48#include <opensm/osm_service.h>
49
50/**********************************************************************
51 **********************************************************************/
52void osm_svcr_delete(IN osm_svcr_t * const p_svcr)
53{
54	free(p_svcr);
55}
56
57/**********************************************************************
58 **********************************************************************/
59void
60osm_svcr_init(IN osm_svcr_t * const p_svcr,
61	      IN const ib_service_record_t * p_svc_rec)
62{
63	CL_ASSERT(p_svcr);
64
65	p_svcr->modified_time = cl_get_time_stamp_sec();
66
67	/* We track the time left for this service in
68	   an external field to avoid extra cl_ntoh/hton
69	   required for working with the MAD field */
70	p_svcr->lease_period = cl_ntoh32(p_svc_rec->service_lease);
71	p_svcr->service_record = *p_svc_rec;
72}
73
74/**********************************************************************
75 **********************************************************************/
76osm_svcr_t *osm_svcr_new(IN const ib_service_record_t * p_svc_rec)
77{
78	osm_svcr_t *p_svcr;
79
80	CL_ASSERT(p_svc_rec);
81
82	p_svcr = (osm_svcr_t *) malloc(sizeof(*p_svcr));
83	if (p_svcr) {
84		memset(p_svcr, 0, sizeof(*p_svcr));
85		osm_svcr_init(p_svcr, p_svc_rec);
86	}
87
88	return (p_svcr);
89}
90
91/**********************************************************************
92 **********************************************************************/
93static
94    cl_status_t
95__match_rid_of_svc_rec(IN const cl_list_item_t * const p_list_item,
96		       IN void *context)
97{
98	ib_service_record_t *p_svc_rec = (ib_service_record_t *) context;
99	osm_svcr_t *p_svcr = (osm_svcr_t *) p_list_item;
100	int32_t count;
101
102	count = memcmp(&p_svcr->service_record,
103		       p_svc_rec,
104		       sizeof(p_svc_rec->service_id) +
105		       sizeof(p_svc_rec->service_gid) +
106		       sizeof(p_svc_rec->service_pkey));
107
108	if (count == 0)
109		return CL_SUCCESS;
110	else
111		return CL_NOT_FOUND;
112
113}
114
115/**********************************************************************
116 **********************************************************************/
117osm_svcr_t *osm_svcr_get_by_rid(IN osm_subn_t const *p_subn,
118				IN osm_log_t * p_log,
119				IN ib_service_record_t * const p_svc_rec)
120{
121	cl_list_item_t *p_list_item;
122
123	OSM_LOG_ENTER(p_log);
124
125	p_list_item = cl_qlist_find_from_head(&p_subn->sa_sr_list,
126					      __match_rid_of_svc_rec,
127					      p_svc_rec);
128
129	if (p_list_item == cl_qlist_end(&p_subn->sa_sr_list))
130		p_list_item = NULL;
131
132	OSM_LOG_EXIT(p_log);
133	return (osm_svcr_t *) p_list_item;
134}
135
136/**********************************************************************
137 **********************************************************************/
138void
139osm_svcr_insert_to_db(IN osm_subn_t * p_subn,
140		      IN osm_log_t * p_log, IN osm_svcr_t * p_svcr)
141{
142	OSM_LOG_ENTER(p_log);
143
144	OSM_LOG(p_log, OSM_LOG_DEBUG,
145		"Inserting new Service Record into Database\n");
146
147	cl_qlist_insert_head(&p_subn->sa_sr_list, &p_svcr->list_item);
148
149	OSM_LOG_EXIT(p_log);
150}
151
152void
153osm_svcr_remove_from_db(IN osm_subn_t * p_subn,
154			IN osm_log_t * p_log, IN osm_svcr_t * p_svcr)
155{
156	OSM_LOG_ENTER(p_log);
157
158	OSM_LOG(p_log, OSM_LOG_DEBUG,
159		"Removing Service Record Name:%s ID:0x%016" PRIx64
160		" from Database\n", p_svcr->service_record.service_name,
161		p_svcr->service_record.service_id);
162
163	cl_qlist_remove_item(&p_subn->sa_sr_list, &p_svcr->list_item);
164
165	OSM_LOG_EXIT(p_log);
166}
167