opensolaris_sunddi.c revision 324745
1/*-
2 * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: stable/10/sys/cddl/compat/opensolaris/kern/opensolaris_sunddi.c 324745 2017-10-19 07:21:45Z avg $");
29
30#include <sys/param.h>
31#include <sys/jail.h>
32#include <sys/kernel.h>
33#include <sys/libkern.h>
34#include <sys/limits.h>
35#include <sys/misc.h>
36#include <sys/sunddi.h>
37#include <sys/sysctl.h>
38
39int
40ddi_strtol(const char *str, char **nptr, int base, long *result)
41{
42
43	*result = strtol(str, nptr, base);
44	return (0);
45}
46
47int
48ddi_strtoul(const char *str, char **nptr, int base, unsigned long *result)
49{
50
51	if (str == hw_serial) {
52		*result = prison0.pr_hostid;
53		return (0);
54	}
55
56	*result = strtoul(str, nptr, base);
57	return (0);
58}
59
60int
61ddi_strtoull(const char *str, char **nptr, int base, unsigned long long *result)
62{
63
64	*result = (unsigned long long)strtouq(str, nptr, base);
65	return (0);
66}
67
68struct ddi_soft_state_item {
69	int	 ssi_item;
70	void	*ssi_data;
71	LIST_ENTRY(ddi_soft_state_item) ssi_next;
72};
73
74struct ddi_soft_state {
75	size_t		ss_size;
76	kmutex_t	ss_lock;
77	LIST_HEAD(, ddi_soft_state_item) ss_list;
78};
79
80static void *
81ddi_get_soft_state_locked(struct ddi_soft_state *ss, int item)
82{
83	struct ddi_soft_state_item *itemp;
84
85	ASSERT(MUTEX_HELD(&ss->ss_lock));
86
87	LIST_FOREACH(itemp, &ss->ss_list, ssi_next) {
88		if (itemp->ssi_item == item)
89			return (itemp->ssi_data);
90	}
91	return (NULL);
92}
93
94void *
95ddi_get_soft_state(void *state, int item)
96{
97	struct ddi_soft_state *ss = state;
98	void *data;
99
100	mutex_enter(&ss->ss_lock);
101	data = ddi_get_soft_state_locked(ss, item);
102	mutex_exit(&ss->ss_lock);
103	return (data);
104}
105
106int
107ddi_soft_state_zalloc(void *state, int item)
108{
109	struct ddi_soft_state *ss = state;
110	struct ddi_soft_state_item *itemp;
111
112	itemp = kmem_alloc(sizeof(*itemp), KM_SLEEP);
113	itemp->ssi_item = item;
114	itemp->ssi_data = kmem_zalloc(ss->ss_size, KM_SLEEP);
115
116	mutex_enter(&ss->ss_lock);
117	if (ddi_get_soft_state_locked(ss, item) != NULL) {
118		mutex_exit(&ss->ss_lock);
119		kmem_free(itemp->ssi_data, ss->ss_size);
120		kmem_free(itemp, sizeof(*itemp));
121		return (DDI_FAILURE);
122	}
123	LIST_INSERT_HEAD(&ss->ss_list, itemp, ssi_next);
124	mutex_exit(&ss->ss_lock);
125	return (DDI_SUCCESS);
126}
127
128static void
129ddi_soft_state_free_locked(struct ddi_soft_state *ss, int item)
130{
131	struct ddi_soft_state_item *itemp;
132
133	ASSERT(MUTEX_HELD(&ss->ss_lock));
134
135	LIST_FOREACH(itemp, &ss->ss_list, ssi_next) {
136		if (itemp->ssi_item == item)
137			break;
138	}
139	if (itemp != NULL) {
140		LIST_REMOVE(itemp, ssi_next);
141		kmem_free(itemp->ssi_data, ss->ss_size);
142		kmem_free(itemp, sizeof(*itemp));
143	}
144}
145
146void
147ddi_soft_state_free(void *state, int item)
148{
149	struct ddi_soft_state *ss = state;
150
151	mutex_enter(&ss->ss_lock);
152	ddi_soft_state_free_locked(ss, item);
153	mutex_exit(&ss->ss_lock);
154}
155
156int
157ddi_soft_state_init(void **statep, size_t size, size_t nitems __unused)
158{
159	struct ddi_soft_state *ss;
160
161	ss = kmem_alloc(sizeof(*ss), KM_SLEEP);
162	mutex_init(&ss->ss_lock, NULL, MUTEX_DEFAULT, NULL);
163	ss->ss_size = size;
164	LIST_INIT(&ss->ss_list);
165	*statep = ss;
166	return (0);
167}
168
169void
170ddi_soft_state_fini(void **statep)
171{
172	struct ddi_soft_state *ss = *statep;
173	struct ddi_soft_state_item *itemp;
174	int item;
175
176	mutex_enter(&ss->ss_lock);
177	while ((itemp = LIST_FIRST(&ss->ss_list)) != NULL) {
178		item = itemp->ssi_item;
179		ddi_soft_state_free_locked(ss, item);
180	}
181	mutex_exit(&ss->ss_lock);
182	mutex_destroy(&ss->ss_lock);
183	kmem_free(ss, sizeof(*ss));
184
185	*statep = NULL;
186}
187