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 * Attempt to fetch a pointer to @p io's internal read buffer, if
71 * supported by @p io.
72 *
73 * The returned pointer is only gauranteed to be valid until the next I/O
74 * operation performed on @p io; concrete implementations of bhnd_nvram_io
75 * may provide stronger gaurantees.
76 *
77 * @param io NVRAM I/O context.
78 * @param offset The offset within @p io for which to return a buffer pointer.
79 * @param[out] ptr On success, will be initialized with a pointer to @p io's
80 * internal read buffer.
81 * @param nbytes The minimum number of bytes that must be readable at @p offset.
82 * @param[out] navail The actual number of readable bytes, which may be greater
83 * than @p nbytes. If this value is not required, a NULL pointer may be
84 * provided.
85 *
86 * @retval 0 success
87 * @retval EIO if an input error occured reading @p io.
88 * @retval ENODEV if @p io does not support direct access to its backing read
89 * buffer.
90 * @retval ENXIO if the request exceeds the size of @p io.
91 * @retval EFAULT if @p io requires I/O request alignment and @p offset or
92 * @p nbytes are misaligned.
93 */
94int
95bhnd_nvram_io_read_ptr(struct bhnd_nvram_io *io, size_t offset,
96    const void **ptr, size_t nbytes, size_t *navail)
97{
98	return (io->iops->read_ptr(io, offset, ptr, nbytes, navail));
99}
100
101/**
102 * Write @p nbytes to @p io at @p offset.
103 *
104 * @param io NVRAM I/O context.
105 * @param offset The offset within @p io at which to perform the write.
106 * @param buffer Data to be written to @p io.
107 * @param nbytes The number of bytes to be written from @p buffer.
108 *
109 * @retval 0 success
110 * @retval EIO if an output error occurs writing to @p io.
111 * @retval ENODEV if @p io does not support writing.
112 * @retval ENXIO if @p io does not support writes beyond the existing
113 * end-of-file, and a write at @p offset would exceed the size of the @p io
114 * backing data store.
115 * @retval EFAULT if @p io requires I/O request alignment and @p offset or
116 * @p nbytes are misaligned.
117 */
118int
119bhnd_nvram_io_write(struct bhnd_nvram_io *io, size_t offset, void *buffer,
120    size_t nbytes)
121{
122	return (io->iops->write(io, offset, buffer, nbytes));
123}
124
125/**
126 * Attempt to fetch a writable pointer to @p io's internal write buffer, if
127 * supported by @p io.
128 *
129 * The returned pointer is only gauranteed to be valid until the next I/O
130 * operation performed on @p io; concrete implementations of bhnd_nvram_io
131 * may provide stronger gaurantees.
132 *
133 * @param io NVRAM I/O context.
134 * @param offset The offset within @p io for which to return a buffer pointer.
135 * @param[in,out] ptr On success, will be initialized with a pointer to @p io's
136 * internal buffer at which up to @p nbytes may be written.
137 * @param nbytes The minimum number of bytes that must be writable at @p offset.
138 * @param[out] navail The actual number of writable bytes, which may be greater
139 * than @p nbytes. If this value is not required, a NULL pointer may be
140 * provided.
141 *
142 * @retval 0 success
143 * @retval EIO if an output error occurs preparing @p io's write buffer.
144 * @retval ENODEV if @p io does not support direct access to its backing write
145 * buffer.
146 * @retval ENXIO if @p io does not support writes beyond the existing
147 * end-of-file, and a write at @p offset of @p nbytes would exceed the size of
148 * the @p io backing data store.
149 * @retval EFAULT if @p io requires I/O request alignment and @p offset or
150 * @p nbytes are misaligned.
151 */
152int
153bhnd_nvram_io_write_ptr(struct bhnd_nvram_io *io, size_t offset, void **ptr,
154    size_t nbytes, size_t *navail)
155{
156	return (io->iops->write_ptr(io, offset, ptr, nbytes, navail));
157}
158
159/**
160 * Return the total number of bytes readable via @p io.
161 *
162 * @param io NVRAM I/O context.
163 */
164size_t
165bhnd_nvram_io_getsize(struct bhnd_nvram_io *io)
166{
167	return (io->iops->getsize(io));
168}
169
170/**
171 * Attempt to set the size of @p io to @p size.
172 *
173 * If the total size of @p io is increased, the contents of the newly mapped
174 * bytes are undefined; concrete implementations of bhnd_nvram_io may
175 * provide stronger gaurantees.
176 *
177 * @param io NVRAM I/O context.
178 * @param size The new size.
179 *
180 * @retval 0 success
181 * @retval EIO if an I/O error occurs resizing @p io.
182 * @retval ENODEV if @p io does not support resizing.
183 * @retval ENXIO if @p size exceeds the capacity or other limits of @p io.
184 * @retval EFAULT if @p io requires I/O request alignment and @p size is
185 * misaligned.
186 */
187int
188bhnd_nvram_io_setsize(struct bhnd_nvram_io *io, size_t size)
189{
190	return (io->iops->setsize(io, size));
191}
192
193/**
194 * Free a previously allocated I/O context, releasing all associated
195 * resources.
196 *
197 * @param io The I/O context to be freed.
198 */
199void
200bhnd_nvram_io_free(struct bhnd_nvram_io *io)
201{
202	return (io->iops->free(io));
203}
204