1130803Smarcel/*
2130803Smarcel * Copyright (c) 2004-2007 Voltaire, Inc. All rights reserved.
3130803Smarcel * Copyright (c) 2002-2006 Mellanox Technologies LTD. All rights reserved.
4130803Smarcel * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
5130803Smarcel *
6130803Smarcel * This software is available to you under a choice of one of two
7130803Smarcel * licenses.  You may choose to be licensed under the terms of the GNU
8130803Smarcel * General Public License (GPL) Version 2, available from the file
9130803Smarcel * COPYING in the main directory of this source tree, or the
10130803Smarcel * OpenIB.org BSD license below:
11130803Smarcel *
12130803Smarcel *     Redistribution and use in source and binary forms, with or
13130803Smarcel *     without modification, are permitted provided that the following
14130803Smarcel *     conditions are met:
15130803Smarcel *
16130803Smarcel *      - Redistributions of source code must retain the above
17130803Smarcel *        copyright notice, this list of conditions and the following
18130803Smarcel *        disclaimer.
19130803Smarcel *
20130803Smarcel *      - Redistributions in binary form must reproduce the above
21130803Smarcel *        copyright notice, this list of conditions and the following
22130803Smarcel *        disclaimer in the documentation and/or other materials
23130803Smarcel *        provided with the distribution.
24130803Smarcel *
25130803Smarcel * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26130803Smarcel * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27130803Smarcel * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28130803Smarcel * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29130803Smarcel * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30130803Smarcel * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31130803Smarcel * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32130803Smarcel * SOFTWARE.
33130803Smarcel *
34130803Smarcel */
35130803Smarcel
36130803Smarcel#if HAVE_CONFIG_H
37130803Smarcel#  include <config.h>
38130803Smarcel#endif				/* HAVE_CONFIG_H */
39
40#include <stdlib.h>
41#include <complib/cl_debug.h>
42#include <opensm/osm_db_pack.h>
43
44static inline void __osm_pack_guid(uint64_t guid, char *p_guid_str)
45{
46	sprintf(p_guid_str, "0x%016" PRIx64, guid);
47}
48
49static inline uint64_t __osm_unpack_guid(char *p_guid_str)
50{
51	return strtoull(p_guid_str, NULL, 0);
52}
53
54static inline void
55__osm_pack_lids(uint16_t min_lid, uint16_t max_lid, char *p_lid_str)
56{
57	sprintf(p_lid_str, "0x%04x 0x%04x", min_lid, max_lid);
58}
59
60static inline int
61__osm_unpack_lids(IN char *p_lid_str,
62		  OUT uint16_t * p_min_lid, OUT uint16_t * p_max_lid)
63{
64	unsigned long tmp;
65	char *p_next;
66	char *p_num;
67	char lids_str[24];
68
69	strncpy(lids_str, p_lid_str, 23);
70	lids_str[23] = '\0';
71	p_num = strtok_r(lids_str, " \t", &p_next);
72	if (!p_num)
73		return 1;
74	tmp = strtoul(p_num, NULL, 0);
75	CL_ASSERT(tmp < 0x10000);
76	*p_min_lid = (uint16_t) tmp;
77
78	p_num = strtok_r(NULL, " \t", &p_next);
79	if (!p_num)
80		return 1;
81	tmp = strtoul(p_num, NULL, 0);
82	CL_ASSERT(tmp < 0x10000);
83	*p_max_lid = (uint16_t) tmp;
84
85	return 0;
86}
87
88int
89osm_db_guid2lid_guids(IN osm_db_domain_t * const p_g2l,
90		      OUT cl_qlist_t * p_guid_list)
91{
92	char *p_key;
93	cl_list_t keys;
94	osm_db_guid_elem_t *p_guid_elem;
95
96	cl_list_construct(&keys);
97	cl_list_init(&keys, 10);
98
99	if (osm_db_keys(p_g2l, &keys))
100		return 1;
101
102	while ((p_key = cl_list_remove_head(&keys)) != NULL) {
103		p_guid_elem =
104		    (osm_db_guid_elem_t *) malloc(sizeof(osm_db_guid_elem_t));
105		CL_ASSERT(p_guid_elem != NULL);
106
107		p_guid_elem->guid = __osm_unpack_guid(p_key);
108		cl_qlist_insert_head(p_guid_list, &p_guid_elem->item);
109	}
110
111	cl_list_destroy(&keys);
112	return 0;
113}
114
115int
116osm_db_guid2lid_get(IN osm_db_domain_t * const p_g2l,
117		    IN uint64_t guid,
118		    OUT uint16_t * p_min_lid, OUT uint16_t * p_max_lid)
119{
120	char guid_str[20];
121	char *p_lid_str;
122	uint16_t min_lid, max_lid;
123
124	__osm_pack_guid(guid, guid_str);
125	p_lid_str = osm_db_lookup(p_g2l, guid_str);
126	if (!p_lid_str)
127		return 1;
128	if (__osm_unpack_lids(p_lid_str, &min_lid, &max_lid))
129		return 1;
130
131	if (p_min_lid)
132		*p_min_lid = min_lid;
133	if (p_max_lid)
134		*p_max_lid = max_lid;
135
136	return 0;
137}
138
139int
140osm_db_guid2lid_set(IN osm_db_domain_t * const p_g2l,
141		    IN uint64_t guid, IN uint16_t min_lid, IN uint16_t max_lid)
142{
143	char guid_str[20];
144	char lid_str[16];
145
146	__osm_pack_guid(guid, guid_str);
147	__osm_pack_lids(min_lid, max_lid, lid_str);
148
149	return (osm_db_update(p_g2l, guid_str, lid_str));
150}
151
152int osm_db_guid2lid_delete(IN osm_db_domain_t * const p_g2l, IN uint64_t guid)
153{
154	char guid_str[20];
155	__osm_pack_guid(guid, guid_str);
156	return (osm_db_delete(p_g2l, guid_str));
157}
158