1321936Shselasky/*
2321936Shselasky * Copyright (c) 2004-2009 Voltaire, Inc. All rights reserved.
3321936Shselasky * Copyright (c) 2002-2006 Mellanox Technologies LTD. All rights reserved.
4321936Shselasky * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
5321936Shselasky *
6321936Shselasky * This software is available to you under a choice of one of two
7321936Shselasky * licenses.  You may choose to be licensed under the terms of the GNU
8321936Shselasky * General Public License (GPL) Version 2, available from the file
9321936Shselasky * COPYING in the main directory of this source tree, or the
10321936Shselasky * OpenIB.org BSD license below:
11321936Shselasky *
12321936Shselasky *     Redistribution and use in source and binary forms, with or
13321936Shselasky *     without modification, are permitted provided that the following
14321936Shselasky *     conditions are met:
15321936Shselasky *
16321936Shselasky *      - Redistributions of source code must retain the above
17321936Shselasky *        copyright notice, this list of conditions and the following
18321936Shselasky *        disclaimer.
19321936Shselasky *
20321936Shselasky *      - Redistributions in binary form must reproduce the above
21321936Shselasky *        copyright notice, this list of conditions and the following
22321936Shselasky *        disclaimer in the documentation and/or other materials
23321936Shselasky *        provided with the distribution.
24321936Shselasky *
25321936Shselasky * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26321936Shselasky * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27321936Shselasky * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28321936Shselasky * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29321936Shselasky * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30321936Shselasky * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31321936Shselasky * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32321936Shselasky * SOFTWARE.
33321936Shselasky *
34321936Shselasky */
35321936Shselasky
36321936Shselasky/*
37321936Shselasky * Abstract:
38321936Shselasky *    Implementation of service record functions.
39321936Shselasky */
40321936Shselasky
41321936Shselasky#if HAVE_CONFIG_H
42321936Shselasky#  include <config.h>
43321936Shselasky#endif				/* HAVE_CONFIG_H */
44321936Shselasky
45321936Shselasky#include <stdlib.h>
46321936Shselasky#include <complib/cl_debug.h>
47321936Shselasky#include <complib/cl_timer.h>
48321936Shselasky#include <opensm/osm_file_ids.h>
49321936Shselasky#define FILE_ID OSM_FILE_SERVICE_C
50321936Shselasky#include <opensm/osm_service.h>
51321936Shselasky#include <opensm/osm_opensm.h>
52321936Shselasky
53321936Shselaskyvoid osm_svcr_delete(IN osm_svcr_t * p_svcr)
54321936Shselasky{
55321936Shselasky	free(p_svcr);
56321936Shselasky}
57321936Shselasky
58321936Shselaskyvoid osm_svcr_init(IN osm_svcr_t * p_svcr,
59321936Shselasky		   IN const ib_service_record_t * p_svc_rec)
60321936Shselasky{
61321936Shselasky	CL_ASSERT(p_svcr);
62321936Shselasky
63321936Shselasky	p_svcr->modified_time = cl_get_time_stamp_sec();
64321936Shselasky
65321936Shselasky	/* We track the time left for this service in
66321936Shselasky	   an external field to avoid extra cl_ntoh/hton
67321936Shselasky	   required for working with the MAD field */
68321936Shselasky	p_svcr->lease_period = cl_ntoh32(p_svc_rec->service_lease);
69321936Shselasky	p_svcr->service_record = *p_svc_rec;
70321936Shselasky}
71321936Shselasky
72321936Shselaskyosm_svcr_t *osm_svcr_new(IN const ib_service_record_t * p_svc_rec)
73321936Shselasky{
74321936Shselasky	osm_svcr_t *p_svcr;
75321936Shselasky
76321936Shselasky	CL_ASSERT(p_svc_rec);
77321936Shselasky
78321936Shselasky	p_svcr = (osm_svcr_t *) malloc(sizeof(*p_svcr));
79321936Shselasky	if (p_svcr) {
80321936Shselasky		memset(p_svcr, 0, sizeof(*p_svcr));
81321936Shselasky		osm_svcr_init(p_svcr, p_svc_rec);
82321936Shselasky	}
83321936Shselasky
84321936Shselasky	return p_svcr;
85321936Shselasky}
86321936Shselasky
87321936Shselaskystatic cl_status_t match_rid_of_svc_rec(IN const cl_list_item_t * p_list_item,
88321936Shselasky					IN void *context)
89321936Shselasky{
90321936Shselasky	ib_service_record_t *p_svc_rec = (ib_service_record_t *) context;
91321936Shselasky	osm_svcr_t *p_svcr = (osm_svcr_t *) p_list_item;
92321936Shselasky
93321936Shselasky	if (memcmp(&p_svcr->service_record, p_svc_rec,
94321936Shselasky		   sizeof(p_svc_rec->service_id) +
95321936Shselasky		   sizeof(p_svc_rec->service_gid) +
96321936Shselasky		   sizeof(p_svc_rec->service_pkey)))
97321936Shselasky		return CL_NOT_FOUND;
98321936Shselasky	else
99321936Shselasky		return CL_SUCCESS;
100321936Shselasky}
101321936Shselasky
102321936Shselaskyosm_svcr_t *osm_svcr_get_by_rid(IN osm_subn_t const *p_subn,
103321936Shselasky				IN osm_log_t * p_log,
104321936Shselasky				IN ib_service_record_t * p_svc_rec)
105321936Shselasky{
106321936Shselasky	cl_list_item_t *p_list_item;
107321936Shselasky
108321936Shselasky	OSM_LOG_ENTER(p_log);
109321936Shselasky
110321936Shselasky	p_list_item = cl_qlist_find_from_head(&p_subn->sa_sr_list,
111321936Shselasky					      match_rid_of_svc_rec, p_svc_rec);
112321936Shselasky	if (p_list_item == cl_qlist_end(&p_subn->sa_sr_list))
113321936Shselasky		p_list_item = NULL;
114321936Shselasky
115321936Shselasky	OSM_LOG_EXIT(p_log);
116321936Shselasky	return (osm_svcr_t *) p_list_item;
117321936Shselasky}
118321936Shselasky
119321936Shselaskyvoid osm_svcr_insert_to_db(IN osm_subn_t * p_subn, IN osm_log_t * p_log,
120321936Shselasky			   IN osm_svcr_t * p_svcr)
121321936Shselasky{
122321936Shselasky	OSM_LOG_ENTER(p_log);
123321936Shselasky
124321936Shselasky	OSM_LOG(p_log, OSM_LOG_DEBUG,
125321936Shselasky		"Inserting new Service Record into Database\n");
126321936Shselasky
127321936Shselasky	cl_qlist_insert_head(&p_subn->sa_sr_list, &p_svcr->list_item);
128321936Shselasky	p_subn->p_osm->sa.dirty = TRUE;
129321936Shselasky
130321936Shselasky	OSM_LOG_EXIT(p_log);
131321936Shselasky}
132321936Shselasky
133321936Shselaskyvoid osm_svcr_remove_from_db(IN osm_subn_t * p_subn, IN osm_log_t * p_log,
134321936Shselasky			     IN osm_svcr_t * p_svcr)
135321936Shselasky{
136321936Shselasky	OSM_LOG_ENTER(p_log);
137321936Shselasky
138321936Shselasky	OSM_LOG(p_log, OSM_LOG_DEBUG,
139321936Shselasky		"Removing Service Record Name:%s ID:0x%016" PRIx64
140321936Shselasky		" from Database\n", p_svcr->service_record.service_name,
141321936Shselasky		cl_ntoh64(p_svcr->service_record.service_id));
142321936Shselasky
143321936Shselasky	cl_qlist_remove_item(&p_subn->sa_sr_list, &p_svcr->list_item);
144321936Shselasky	p_subn->p_osm->sa.dirty = TRUE;
145321936Shselasky
146321936Shselasky	OSM_LOG_EXIT(p_log);
147321936Shselasky}
148