2 * Copyright (c) 2016 Landon Fuller <landonf@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
41 #include "bhnd_nvram_io.h"
42 #include "bhnd_nvram_iovar.h"
45 * Read exactly @p nbytes from @p io at @p offset.
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
51 * @param nbytes The maximum number of bytes to be read from @p io.
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
57 * @retval EFAULT if @p io requires I/O request alignment and @p offset is
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.
63 bhnd_nvram_io_read(struct bhnd_nvram_io *io, size_t offset, void *buffer,
66 return (io->iops->read(io, offset, buffer, nbytes));
71 * Attempt to fetch a pointer to @p io's internal read buffer, if
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.
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
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
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.
96 bhnd_nvram_io_read_ptr(struct bhnd_nvram_io *io, size_t offset,
97 const void **ptr, size_t nbytes, size_t *navail)
99 return (io->iops->read_ptr(io, offset, ptr, nbytes, navail));
103 * Write @p nbytes to @p io at @p offset.
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.
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.
120 bhnd_nvram_io_write(struct bhnd_nvram_io *io, size_t offset, void *buffer,
123 return (io->iops->write(io, offset, buffer, nbytes));
127 * Attempt to fetch a writable pointer to @p io's internal write buffer, if
128 * supported by @p io.
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.
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
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
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.
154 bhnd_nvram_io_write_ptr(struct bhnd_nvram_io *io, size_t offset, void **ptr,
155 size_t nbytes, size_t *navail)
157 return (io->iops->write_ptr(io, offset, ptr, nbytes, navail));
161 * Return the total number of bytes readable via @p io.
163 * @param io NVRAM I/O context.
166 bhnd_nvram_io_getsize(struct bhnd_nvram_io *io)
168 return (io->iops->getsize(io));
172 * Attempt to set the size of @p io to @p size.
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.
178 * @param io NVRAM I/O context.
179 * @param size The new size.
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
189 bhnd_nvram_io_setsize(struct bhnd_nvram_io *io, size_t size)
191 return (io->iops->setsize(io, size));
195 * Free a previously allocated I/O context, releasing all associated
198 * @param io The I/O context to be freed.
201 bhnd_nvram_io_free(struct bhnd_nvram_io *io)
203 return (io->iops->free(io));