xref: /freebsd/sys/dev/bhnd/nvram/bhnd_nvram_io.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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 #ifdef _KERNEL
32 #include <sys/param.h>
33 #else /* !_KERNEL */
34 #include <errno.h>
35 #include <stdint.h>
36 #include <stdlib.h>
37 #endif /* _KERNEL */
38 
39 #include "bhnd_nvram_io.h"
40 #include "bhnd_nvram_iovar.h"
41 
42 /**
43  * Read exactly @p nbytes from @p io at @p offset.
44  *
45  * @param io NVRAM I/O context.
46  * @param offset The offset within @p io at which to perform the read.
47  * @param[out] buffer Output buffer to which @p nbytes from @p io will be
48  * written.
49  * @param nbytes The maximum number of bytes to be read from @p io.
50  *
51  * @retval 0 success
52  * @retval EIO if an input error occurred reading @p io.
53  * @retval ENXIO if the request for @p offset or @p nbytes exceeds the size
54  * of @p io.
55  * @retval EFAULT if @p io requires I/O request alignment and @p offset is
56  * misaligned.
57  * @retval EFAULT if @p io requires I/O request alignment and @p nbytes is
58  * misaligned and cannot be rounded down to an aligned non-zero value.
59  */
60 int
61 bhnd_nvram_io_read(struct bhnd_nvram_io *io, size_t offset, void *buffer,
62     size_t nbytes)
63 {
64 	return (io->iops->read(io, offset, buffer, nbytes));
65 }
66 
67 /**
68  * Attempt to fetch a pointer to @p io's internal read buffer, if
69  * supported by @p io.
70  *
71  * The returned pointer is only gauranteed to be valid until the next I/O
72  * operation performed on @p io; concrete implementations of bhnd_nvram_io
73  * may provide stronger gaurantees.
74  *
75  * @param io NVRAM I/O context.
76  * @param offset The offset within @p io for which to return a buffer pointer.
77  * @param[out] ptr On success, will be initialized with a pointer to @p io's
78  * internal read buffer.
79  * @param nbytes The minimum number of bytes that must be readable at @p offset.
80  * @param[out] navail The actual number of readable bytes, which may be greater
81  * than @p nbytes. If this value is not required, a NULL pointer may be
82  * provided.
83  *
84  * @retval 0 success
85  * @retval EIO if an input error occurred reading @p io.
86  * @retval ENODEV if @p io does not support direct access to its backing read
87  * buffer.
88  * @retval ENXIO if the request exceeds the size of @p io.
89  * @retval EFAULT if @p io requires I/O request alignment and @p offset or
90  * @p nbytes are misaligned.
91  */
92 int
93 bhnd_nvram_io_read_ptr(struct bhnd_nvram_io *io, size_t offset,
94     const void **ptr, size_t nbytes, size_t *navail)
95 {
96 	return (io->iops->read_ptr(io, offset, ptr, nbytes, navail));
97 }
98 
99 /**
100  * Write @p nbytes to @p io at @p offset.
101  *
102  * @param io NVRAM I/O context.
103  * @param offset The offset within @p io at which to perform the write.
104  * @param buffer Data to be written to @p io.
105  * @param nbytes The number of bytes to be written from @p buffer.
106  *
107  * @retval 0 success
108  * @retval EIO if an output error occurs writing to @p io.
109  * @retval ENODEV if @p io does not support writing.
110  * @retval ENXIO if @p io does not support writes beyond the existing
111  * end-of-file, and a write at @p offset would exceed the size of the @p io
112  * backing data store.
113  * @retval EFAULT if @p io requires I/O request alignment and @p offset or
114  * @p nbytes are misaligned.
115  */
116 int
117 bhnd_nvram_io_write(struct bhnd_nvram_io *io, size_t offset, void *buffer,
118     size_t nbytes)
119 {
120 	return (io->iops->write(io, offset, buffer, nbytes));
121 }
122 
123 /**
124  * Attempt to fetch a writable pointer to @p io's internal write buffer, if
125  * supported by @p io.
126  *
127  * The returned pointer is only gauranteed to be valid until the next I/O
128  * operation performed on @p io; concrete implementations of bhnd_nvram_io
129  * may provide stronger gaurantees.
130  *
131  * @param io NVRAM I/O context.
132  * @param offset The offset within @p io for which to return a buffer pointer.
133  * @param[in,out] ptr On success, will be initialized with a pointer to @p io's
134  * internal buffer at which up to @p nbytes may be written.
135  * @param nbytes The minimum number of bytes that must be writable at @p offset.
136  * @param[out] navail The actual number of writable bytes, which may be greater
137  * than @p nbytes. If this value is not required, a NULL pointer may be
138  * provided.
139  *
140  * @retval 0 success
141  * @retval EIO if an output error occurs preparing @p io's write buffer.
142  * @retval ENODEV if @p io does not support direct access to its backing write
143  * buffer.
144  * @retval ENXIO if @p io does not support writes beyond the existing
145  * end-of-file, and a write at @p offset of @p nbytes would exceed the size of
146  * the @p io backing data store.
147  * @retval EFAULT if @p io requires I/O request alignment and @p offset or
148  * @p nbytes are misaligned.
149  */
150 int
151 bhnd_nvram_io_write_ptr(struct bhnd_nvram_io *io, size_t offset, void **ptr,
152     size_t nbytes, size_t *navail)
153 {
154 	return (io->iops->write_ptr(io, offset, ptr, nbytes, navail));
155 }
156 
157 /**
158  * Return the total number of bytes readable via @p io.
159  *
160  * @param io NVRAM I/O context.
161  */
162 size_t
163 bhnd_nvram_io_getsize(struct bhnd_nvram_io *io)
164 {
165 	return (io->iops->getsize(io));
166 }
167 
168 /**
169  * Attempt to set the size of @p io to @p size.
170  *
171  * If the total size of @p io is increased, the contents of the newly mapped
172  * bytes are undefined; concrete implementations of bhnd_nvram_io may
173  * provide stronger gaurantees.
174  *
175  * @param io NVRAM I/O context.
176  * @param size The new size.
177  *
178  * @retval 0 success
179  * @retval EIO if an I/O error occurs resizing @p io.
180  * @retval ENODEV if @p io does not support resizing.
181  * @retval ENXIO if @p size exceeds the capacity or other limits of @p io.
182  * @retval EFAULT if @p io requires I/O request alignment and @p size is
183  * misaligned.
184  */
185 int
186 bhnd_nvram_io_setsize(struct bhnd_nvram_io *io, size_t size)
187 {
188 	return (io->iops->setsize(io, size));
189 }
190 
191 /**
192  * Free a previously allocated I/O context, releasing all associated
193  * resources.
194  *
195  * @param io The I/O context to be freed.
196  */
197 void
198 bhnd_nvram_io_free(struct bhnd_nvram_io *io)
199 {
200 	return (io->iops->free(io));
201 }
202