1/*-
2 * Copyright (c) 2016 Landon Fuller <landonf@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 *    without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 *    redistribution must be conditioned upon including a substantially
14 *    similar Disclaimer requirement for further binary redistribution.
15 *
16 * NO WARRANTY
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD$");
32
33#ifdef _KERNEL
34#include <sys/param.h>
35#else /* !_KERNEL */
36#include <errno.h>
37#include <stdint.h>
38#include <stdlib.h>
39#endif /* _KERNEL */
40
41#include "bhnd_nvram_io.h"
42#include "bhnd_nvram_iovar.h"
43
44/**
45 * Read exactly @p nbytes from @p io at @p offset.
46 *
47 * @param io NVRAM I/O context.
48 * @param offset The offset within @p io at which to perform the read.
49 * @param[out] buffer Output buffer to which @p nbytes from @p io will be
50 * written.
51 * @param nbytes The maximum number of bytes to be read from @p io.
52 *
53 * @retval 0 success
54 * @retval EIO if an input error occured reading @p io.
55 * @retval ENXIO if the request for @p offset or @p nbytes exceeds the size
56 * of @p io.
57 * @retval EFAULT if @p io requires I/O request alignment and @p offset is
58 * misaligned.
59 * @retval EFAULT if @p io requires I/O request alignment and @p nbytes is
60 * misaligned and cannot be rounded down to an aligned non-zero value.
61 */
62int
63bhnd_nvram_io_read(struct bhnd_nvram_io *io, size_t offset, void *buffer,
64    size_t nbytes)
65{
66	return (io->iops->read(io, offset, buffer, nbytes));
67}
68
69
70/**
71 * Attempt to fetch a pointer to @p io's internal read buffer, if
72 * supported by @p io.
73 *
74 * The returned pointer is only gauranteed to be valid until the next I/O
75 * operation performed on @p io; concrete implementations of bhnd_nvram_io
76 * may provide stronger gaurantees.
77 *
78 * @param io NVRAM I/O context.
79 * @param offset The offset within @p io for which to return a buffer pointer.
80 * @param[out] ptr On success, will be initialized with a pointer to @p io's
81 * internal read buffer.
82 * @param nbytes The minimum number of bytes that must be readable at @p offset.
83 * @param[out] navail The actual number of readable bytes, which may be greater
84 * than @p nbytes. If this value is not required, a NULL pointer may be
85 * provided.
86 *
87 * @retval 0 success
88 * @retval EIO if an input error occured reading @p io.
89 * @retval ENODEV if @p io does not support direct access to its backing read
90 * buffer.
91 * @retval ENXIO if the request exceeds the size of @p io.
92 * @retval EFAULT if @p io requires I/O request alignment and @p offset or
93 * @p nbytes are misaligned.
94 */
95int
96bhnd_nvram_io_read_ptr(struct bhnd_nvram_io *io, size_t offset,
97    const void **ptr, size_t nbytes, size_t *navail)
98{
99	return (io->iops->read_ptr(io, offset, ptr, nbytes, navail));
100}
101
102/**
103 * Write @p nbytes to @p io at @p offset.
104 *
105 * @param io NVRAM I/O context.
106 * @param offset The offset within @p io at which to perform the write.
107 * @param buffer Data to be written to @p io.
108 * @param nbytes The number of bytes to be written from @p buffer.
109 *
110 * @retval 0 success
111 * @retval EIO if an output error occurs writing to @p io.
112 * @retval ENODEV if @p io does not support writing.
113 * @retval ENXIO if @p io does not support writes beyond the existing
114 * end-of-file, and a write at @p offset would exceed the size of the @p io
115 * backing data store.
116 * @retval EFAULT if @p io requires I/O request alignment and @p offset or
117 * @p nbytes are misaligned.
118 */
119int
120bhnd_nvram_io_write(struct bhnd_nvram_io *io, size_t offset, void *buffer,
121    size_t nbytes)
122{
123	return (io->iops->write(io, offset, buffer, nbytes));
124}
125
126/**
127 * Attempt to fetch a writable pointer to @p io's internal write buffer, if
128 * supported by @p io.
129 *
130 * The returned pointer is only gauranteed to be valid until the next I/O
131 * operation performed on @p io; concrete implementations of bhnd_nvram_io
132 * may provide stronger gaurantees.
133 *
134 * @param io NVRAM I/O context.
135 * @param offset The offset within @p io for which to return a buffer pointer.
136 * @param[in,out] ptr On success, will be initialized with a pointer to @p io's
137 * internal buffer at which up to @p nbytes may be written.
138 * @param nbytes The minimum number of bytes that must be writable at @p offset.
139 * @param[out] navail The actual number of writable bytes, which may be greater
140 * than @p nbytes. If this value is not required, a NULL pointer may be
141 * provided.
142 *
143 * @retval 0 success
144 * @retval EIO if an output error occurs preparing @p io's write buffer.
145 * @retval ENODEV if @p io does not support direct access to its backing write
146 * buffer.
147 * @retval ENXIO if @p io does not support writes beyond the existing
148 * end-of-file, and a write at @p offset of @p nbytes would exceed the size of
149 * the @p io backing data store.
150 * @retval EFAULT if @p io requires I/O request alignment and @p offset or
151 * @p nbytes are misaligned.
152 */
153int
154bhnd_nvram_io_write_ptr(struct bhnd_nvram_io *io, size_t offset, void **ptr,
155    size_t nbytes, size_t *navail)
156{
157	return (io->iops->write_ptr(io, offset, ptr, nbytes, navail));
158}
159
160/**
161 * Return the total number of bytes readable via @p io.
162 *
163 * @param io NVRAM I/O context.
164 */
165size_t
166bhnd_nvram_io_getsize(struct bhnd_nvram_io *io)
167{
168	return (io->iops->getsize(io));
169}
170
171/**
172 * Attempt to set the size of @p io to @p size.
173 *
174 * If the total size of @p io is increased, the contents of the newly mapped
175 * bytes are undefined; concrete implementations of bhnd_nvram_io may
176 * provide stronger gaurantees.
177 *
178 * @param io NVRAM I/O context.
179 * @param size The new size.
180 *
181 * @retval 0 success
182 * @retval EIO if an I/O error occurs resizing @p io.
183 * @retval ENODEV if @p io does not support resizing.
184 * @retval ENXIO if @p size exceeds the capacity or other limits of @p io.
185 * @retval EFAULT if @p io requires I/O request alignment and @p size is
186 * misaligned.
187 */
188int
189bhnd_nvram_io_setsize(struct bhnd_nvram_io *io, size_t size)
190{
191	return (io->iops->setsize(io, size));
192}
193
194/**
195 * Free a previously allocated I/O context, releasing all associated
196 * resources.
197 *
198 * @param io The I/O context to be freed.
199 */
200void
201bhnd_nvram_io_free(struct bhnd_nvram_io *io)
202{
203	return (io->iops->free(io));
204}
205
206