aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/bhnd/nvram/bhnd_nvram_ioptr.c
blob: 82b9c09d502de5bd737757014825353669bb5ceb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*-
 * Copyright (c) 2016 Landon Fuller <landonf@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
 *    redistribution must be conditioned upon including a substantially
 *    similar Disclaimer requirement for further binary redistribution.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGES.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#ifdef _KERNEL
#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/systm.h>
#else /* !_KERNEL */
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#endif /* _KERNEL */

#include "bhnd_nvram_private.h"

#include "bhnd_nvram_io.h"
#include "bhnd_nvram_iovar.h"

/**
 * Memory-backed NVRAM I/O context.
 *
 * ioptr instances are gauranteed to provide persistent references to its
 * backing contigious memory via bhnd_nvram_io_read_ptr() and
 * bhnd_nvram_io_write_ptr().
 */
struct bhnd_nvram_ioptr {
	struct bhnd_nvram_io	 io;		/**< common I/O instance state */
	void			*ptr;		/**< backing memory */
	size_t			 size;		/**< size at @p ptr */
	size_t			 capacity;	/**< capacity at @p ptr */
	uint32_t		 flags;		/**< flags (see BHND_NVRAM_IOPTR_*) */
};

BHND_NVRAM_IOPS_DEFN(ioptr)

/**
 * Allocate and return a new I/O context, mapping @p size bytes at @p ptr.

 * The caller is responsible for deallocating the returned I/O context via
 * bhnd_nvram_io_free().
 *
 * @param	ptr		The pointer to be mapped by the returned I/O
 *				context. Must remain valid for the lifetime of
 *				the returned I/O context.
 * @param	size		The total number of bytes mapped at @p ptr.
 * @param	capacity	The maximum number of bytes that may be mapped
 *				at @p ptr via bhnd_nvram_ioptr_setsize().
 * @param	flags		Access flags (see BHND_NVRAM_IOPTR_*).
 *
 * @retval	bhnd_nvram_io	success.
 * @retval	NULL		allocation failed.
 * @retval	NULL		the requested @p capacity is less than @p size.
 */
struct bhnd_nvram_io *
bhnd_nvram_ioptr_new(const void *ptr, size_t size, size_t capacity,
    uint32_t flags)
{
	struct bhnd_nvram_ioptr	*ioptr;

	/* Sanity check the capacity */
	if (size > capacity)
		return (NULL);

	/* Allocate I/O context */
	ioptr = bhnd_nv_malloc(sizeof(*ioptr));
	if (ioptr == NULL)
		return (NULL);

	ioptr->io.iops = &bhnd_nvram_ioptr_ops;
	ioptr->ptr = __DECONST(void *, ptr);
	ioptr->size = size;
	ioptr->capacity = capacity;
	ioptr->flags = flags;

	return (&ioptr->io);
}

static void
bhnd_nvram_ioptr_free(struct bhnd_nvram_io *io)
{	
	bhnd_nv_free(io);
}

static size_t
bhnd_nvram_ioptr_getsize(struct bhnd_nvram_io *io)
{
	struct bhnd_nvram_ioptr	*ioptr = (struct bhnd_nvram_ioptr *)io;
	return (ioptr->size);
}

static int
bhnd_nvram_ioptr_setsize(struct bhnd_nvram_io *io, size_t size)
{
	struct bhnd_nvram_ioptr	*ioptr = (struct bhnd_nvram_ioptr *)io;

	/* Must be writable */
	if (!(ioptr->flags & BHND_NVRAM_IOPTR_RDWR))
		return (ENODEV);

	/* Can't exceed the actual capacity */
	if (size > ioptr->capacity)
		return (ENXIO);

	ioptr->size = size;
	return (0);
}

/* Common ioptr_(read|write)_ptr implementation */
static int
bhnd_nvram_ioptr_ptr(struct bhnd_nvram_ioptr *ioptr, size_t offset, void **ptr,
		     size_t nbytes, size_t *navail)
{
	size_t avail;

	/* Verify offset+nbytes fall within the buffer range */
	if (offset > ioptr->size)
		return (ENXIO);

	avail = ioptr->size - offset;
	if (avail < nbytes)
		return (ENXIO);

	/* Valid I/O range, provide a pointer to the buffer and the
	 * total count of available bytes */
	*ptr = ((uint8_t *)ioptr->ptr) + offset;
	if (navail != NULL)
		*navail = avail;

	return (0);
}

static int
bhnd_nvram_ioptr_read_ptr(struct bhnd_nvram_io *io, size_t offset,
			  const void **ptr, size_t nbytes, size_t *navail)
{
	struct bhnd_nvram_ioptr	*ioptr;
	void			*writep;
	int			 error;

	ioptr = (struct bhnd_nvram_ioptr *) io;

	/* Return a pointer into our backing buffer */
	error = bhnd_nvram_ioptr_ptr(ioptr, offset, &writep, nbytes, navail);
	if (error)
		return (error);

	*ptr = writep;

	return (0);
}

static int
bhnd_nvram_ioptr_write_ptr(struct bhnd_nvram_io *io, size_t offset,
			   void **ptr, size_t nbytes, size_t *navail)
{
	struct bhnd_nvram_ioptr	*ioptr;

	ioptr = (struct bhnd_nvram_ioptr *) io;

	/* Must be writable */
	if (!(ioptr->flags & BHND_NVRAM_IOPTR_RDWR))
		return (ENODEV);

	/* Return a pointer into our backing buffer */
	return (bhnd_nvram_ioptr_ptr(ioptr, offset, ptr, nbytes, navail));
}

static int
bhnd_nvram_ioptr_read(struct bhnd_nvram_io *io, size_t offset, void *buffer,
		      size_t nbytes)
{
	const void	*ptr;
	int		 error;

	/* Try to fetch a direct pointer for at least nbytes */
	if ((error = bhnd_nvram_io_read_ptr(io, offset, &ptr, nbytes, NULL)))
		return (error);

	/* Copy out the requested data */
	memcpy(buffer, ptr, nbytes);
	return (0);
}

static int
bhnd_nvram_ioptr_write(struct bhnd_nvram_io *io, size_t offset,
		       void *buffer, size_t nbytes)
{
	void	*ptr;
	int	 error;

	/* Try to fetch a direct pointer for at least nbytes */
	if ((error = bhnd_nvram_io_write_ptr(io, offset, &ptr, nbytes, NULL)))
		return (error);

	/* Copy in the provided data */
	memcpy(ptr, buffer, nbytes);
	return (0);
}