177cb4d3eSLandon J. Fuller /*-
277cb4d3eSLandon J. Fuller * Copyright (c) 2016 Landon Fuller <landonf@FreeBSD.org>
377cb4d3eSLandon J. Fuller * All rights reserved.
477cb4d3eSLandon J. Fuller *
577cb4d3eSLandon J. Fuller * Redistribution and use in source and binary forms, with or without
677cb4d3eSLandon J. Fuller * modification, are permitted provided that the following conditions
777cb4d3eSLandon J. Fuller * are met:
877cb4d3eSLandon J. Fuller * 1. Redistributions of source code must retain the above copyright
977cb4d3eSLandon J. Fuller * notice, this list of conditions and the following disclaimer,
1077cb4d3eSLandon J. Fuller * without modification.
1177cb4d3eSLandon J. Fuller * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1277cb4d3eSLandon J. Fuller * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
1377cb4d3eSLandon J. Fuller * redistribution must be conditioned upon including a substantially
1477cb4d3eSLandon J. Fuller * similar Disclaimer requirement for further binary redistribution.
1577cb4d3eSLandon J. Fuller *
1677cb4d3eSLandon J. Fuller * NO WARRANTY
1777cb4d3eSLandon J. Fuller * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1877cb4d3eSLandon J. Fuller * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1977cb4d3eSLandon J. Fuller * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
2077cb4d3eSLandon J. Fuller * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
2177cb4d3eSLandon J. Fuller * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
2277cb4d3eSLandon J. Fuller * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2377cb4d3eSLandon J. Fuller * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2477cb4d3eSLandon J. Fuller * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2577cb4d3eSLandon J. Fuller * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2677cb4d3eSLandon J. Fuller * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
2777cb4d3eSLandon J. Fuller * THE POSSIBILITY OF SUCH DAMAGES.
2877cb4d3eSLandon J. Fuller */
2977cb4d3eSLandon J. Fuller
3077cb4d3eSLandon J. Fuller #include <sys/cdefs.h>
3177cb4d3eSLandon J. Fuller #ifdef _KERNEL
3277cb4d3eSLandon J. Fuller #include <sys/param.h>
3377cb4d3eSLandon J. Fuller #else /* !_KERNEL */
3477cb4d3eSLandon J. Fuller #include <errno.h>
3577cb4d3eSLandon J. Fuller #include <stdint.h>
3677cb4d3eSLandon J. Fuller #include <stdlib.h>
3777cb4d3eSLandon J. Fuller #endif /* _KERNEL */
3877cb4d3eSLandon J. Fuller
3977cb4d3eSLandon J. Fuller #include "bhnd_nvram_io.h"
4077cb4d3eSLandon J. Fuller #include "bhnd_nvram_iovar.h"
4177cb4d3eSLandon J. Fuller
4277cb4d3eSLandon J. Fuller /**
4377cb4d3eSLandon J. Fuller * Read exactly @p nbytes from @p io at @p offset.
4477cb4d3eSLandon J. Fuller *
4577cb4d3eSLandon J. Fuller * @param io NVRAM I/O context.
4677cb4d3eSLandon J. Fuller * @param offset The offset within @p io at which to perform the read.
4777cb4d3eSLandon J. Fuller * @param[out] buffer Output buffer to which @p nbytes from @p io will be
4877cb4d3eSLandon J. Fuller * written.
4977cb4d3eSLandon J. Fuller * @param nbytes The maximum number of bytes to be read from @p io.
5077cb4d3eSLandon J. Fuller *
5177cb4d3eSLandon J. Fuller * @retval 0 success
52*88cdf609SGordon Bergling * @retval EIO if an input error occurred reading @p io.
5377cb4d3eSLandon J. Fuller * @retval ENXIO if the request for @p offset or @p nbytes exceeds the size
5477cb4d3eSLandon J. Fuller * of @p io.
5577cb4d3eSLandon J. Fuller * @retval EFAULT if @p io requires I/O request alignment and @p offset is
5677cb4d3eSLandon J. Fuller * misaligned.
5777cb4d3eSLandon J. Fuller * @retval EFAULT if @p io requires I/O request alignment and @p nbytes is
5877cb4d3eSLandon J. Fuller * misaligned and cannot be rounded down to an aligned non-zero value.
5977cb4d3eSLandon J. Fuller */
6077cb4d3eSLandon J. Fuller int
bhnd_nvram_io_read(struct bhnd_nvram_io * io,size_t offset,void * buffer,size_t nbytes)6177cb4d3eSLandon J. Fuller bhnd_nvram_io_read(struct bhnd_nvram_io *io, size_t offset, void *buffer,
6277cb4d3eSLandon J. Fuller size_t nbytes)
6377cb4d3eSLandon J. Fuller {
6477cb4d3eSLandon J. Fuller return (io->iops->read(io, offset, buffer, nbytes));
6577cb4d3eSLandon J. Fuller }
6677cb4d3eSLandon J. Fuller
6777cb4d3eSLandon J. Fuller /**
6877cb4d3eSLandon J. Fuller * Attempt to fetch a pointer to @p io's internal read buffer, if
6977cb4d3eSLandon J. Fuller * supported by @p io.
7077cb4d3eSLandon J. Fuller *
7177cb4d3eSLandon J. Fuller * The returned pointer is only gauranteed to be valid until the next I/O
7277cb4d3eSLandon J. Fuller * operation performed on @p io; concrete implementations of bhnd_nvram_io
7377cb4d3eSLandon J. Fuller * may provide stronger gaurantees.
7477cb4d3eSLandon J. Fuller *
7577cb4d3eSLandon J. Fuller * @param io NVRAM I/O context.
7677cb4d3eSLandon J. Fuller * @param offset The offset within @p io for which to return a buffer pointer.
7777cb4d3eSLandon J. Fuller * @param[out] ptr On success, will be initialized with a pointer to @p io's
7877cb4d3eSLandon J. Fuller * internal read buffer.
7977cb4d3eSLandon J. Fuller * @param nbytes The minimum number of bytes that must be readable at @p offset.
8077cb4d3eSLandon J. Fuller * @param[out] navail The actual number of readable bytes, which may be greater
8177cb4d3eSLandon J. Fuller * than @p nbytes. If this value is not required, a NULL pointer may be
8277cb4d3eSLandon J. Fuller * provided.
8377cb4d3eSLandon J. Fuller *
8477cb4d3eSLandon J. Fuller * @retval 0 success
85*88cdf609SGordon Bergling * @retval EIO if an input error occurred reading @p io.
8677cb4d3eSLandon J. Fuller * @retval ENODEV if @p io does not support direct access to its backing read
8777cb4d3eSLandon J. Fuller * buffer.
8877cb4d3eSLandon J. Fuller * @retval ENXIO if the request exceeds the size of @p io.
8977cb4d3eSLandon J. Fuller * @retval EFAULT if @p io requires I/O request alignment and @p offset or
9077cb4d3eSLandon J. Fuller * @p nbytes are misaligned.
9177cb4d3eSLandon J. Fuller */
9277cb4d3eSLandon J. Fuller int
bhnd_nvram_io_read_ptr(struct bhnd_nvram_io * io,size_t offset,const void ** ptr,size_t nbytes,size_t * navail)9377cb4d3eSLandon J. Fuller bhnd_nvram_io_read_ptr(struct bhnd_nvram_io *io, size_t offset,
9477cb4d3eSLandon J. Fuller const void **ptr, size_t nbytes, size_t *navail)
9577cb4d3eSLandon J. Fuller {
9677cb4d3eSLandon J. Fuller return (io->iops->read_ptr(io, offset, ptr, nbytes, navail));
9777cb4d3eSLandon J. Fuller }
9877cb4d3eSLandon J. Fuller
9977cb4d3eSLandon J. Fuller /**
10077cb4d3eSLandon J. Fuller * Write @p nbytes to @p io at @p offset.
10177cb4d3eSLandon J. Fuller *
10277cb4d3eSLandon J. Fuller * @param io NVRAM I/O context.
10377cb4d3eSLandon J. Fuller * @param offset The offset within @p io at which to perform the write.
10477cb4d3eSLandon J. Fuller * @param buffer Data to be written to @p io.
10577cb4d3eSLandon J. Fuller * @param nbytes The number of bytes to be written from @p buffer.
10677cb4d3eSLandon J. Fuller *
10777cb4d3eSLandon J. Fuller * @retval 0 success
10877cb4d3eSLandon J. Fuller * @retval EIO if an output error occurs writing to @p io.
10977cb4d3eSLandon J. Fuller * @retval ENODEV if @p io does not support writing.
11077cb4d3eSLandon J. Fuller * @retval ENXIO if @p io does not support writes beyond the existing
11177cb4d3eSLandon J. Fuller * end-of-file, and a write at @p offset would exceed the size of the @p io
11277cb4d3eSLandon J. Fuller * backing data store.
11377cb4d3eSLandon J. Fuller * @retval EFAULT if @p io requires I/O request alignment and @p offset or
11477cb4d3eSLandon J. Fuller * @p nbytes are misaligned.
11577cb4d3eSLandon J. Fuller */
11677cb4d3eSLandon J. Fuller int
bhnd_nvram_io_write(struct bhnd_nvram_io * io,size_t offset,void * buffer,size_t nbytes)11777cb4d3eSLandon J. Fuller bhnd_nvram_io_write(struct bhnd_nvram_io *io, size_t offset, void *buffer,
11877cb4d3eSLandon J. Fuller size_t nbytes)
11977cb4d3eSLandon J. Fuller {
12077cb4d3eSLandon J. Fuller return (io->iops->write(io, offset, buffer, nbytes));
12177cb4d3eSLandon J. Fuller }
12277cb4d3eSLandon J. Fuller
12377cb4d3eSLandon J. Fuller /**
12477cb4d3eSLandon J. Fuller * Attempt to fetch a writable pointer to @p io's internal write buffer, if
12577cb4d3eSLandon J. Fuller * supported by @p io.
12677cb4d3eSLandon J. Fuller *
12777cb4d3eSLandon J. Fuller * The returned pointer is only gauranteed to be valid until the next I/O
12877cb4d3eSLandon J. Fuller * operation performed on @p io; concrete implementations of bhnd_nvram_io
12977cb4d3eSLandon J. Fuller * may provide stronger gaurantees.
13077cb4d3eSLandon J. Fuller *
13177cb4d3eSLandon J. Fuller * @param io NVRAM I/O context.
13277cb4d3eSLandon J. Fuller * @param offset The offset within @p io for which to return a buffer pointer.
13377cb4d3eSLandon J. Fuller * @param[in,out] ptr On success, will be initialized with a pointer to @p io's
13477cb4d3eSLandon J. Fuller * internal buffer at which up to @p nbytes may be written.
13577cb4d3eSLandon J. Fuller * @param nbytes The minimum number of bytes that must be writable at @p offset.
13677cb4d3eSLandon J. Fuller * @param[out] navail The actual number of writable bytes, which may be greater
13777cb4d3eSLandon J. Fuller * than @p nbytes. If this value is not required, a NULL pointer may be
13877cb4d3eSLandon J. Fuller * provided.
13977cb4d3eSLandon J. Fuller *
14077cb4d3eSLandon J. Fuller * @retval 0 success
14177cb4d3eSLandon J. Fuller * @retval EIO if an output error occurs preparing @p io's write buffer.
14277cb4d3eSLandon J. Fuller * @retval ENODEV if @p io does not support direct access to its backing write
14377cb4d3eSLandon J. Fuller * buffer.
14477cb4d3eSLandon J. Fuller * @retval ENXIO if @p io does not support writes beyond the existing
14577cb4d3eSLandon J. Fuller * end-of-file, and a write at @p offset of @p nbytes would exceed the size of
14677cb4d3eSLandon J. Fuller * the @p io backing data store.
14777cb4d3eSLandon J. Fuller * @retval EFAULT if @p io requires I/O request alignment and @p offset or
14877cb4d3eSLandon J. Fuller * @p nbytes are misaligned.
14977cb4d3eSLandon J. Fuller */
15077cb4d3eSLandon J. Fuller int
bhnd_nvram_io_write_ptr(struct bhnd_nvram_io * io,size_t offset,void ** ptr,size_t nbytes,size_t * navail)15177cb4d3eSLandon J. Fuller bhnd_nvram_io_write_ptr(struct bhnd_nvram_io *io, size_t offset, void **ptr,
15277cb4d3eSLandon J. Fuller size_t nbytes, size_t *navail)
15377cb4d3eSLandon J. Fuller {
15477cb4d3eSLandon J. Fuller return (io->iops->write_ptr(io, offset, ptr, nbytes, navail));
15577cb4d3eSLandon J. Fuller }
15677cb4d3eSLandon J. Fuller
15777cb4d3eSLandon J. Fuller /**
15877cb4d3eSLandon J. Fuller * Return the total number of bytes readable via @p io.
15977cb4d3eSLandon J. Fuller *
16077cb4d3eSLandon J. Fuller * @param io NVRAM I/O context.
16177cb4d3eSLandon J. Fuller */
16277cb4d3eSLandon J. Fuller size_t
bhnd_nvram_io_getsize(struct bhnd_nvram_io * io)16377cb4d3eSLandon J. Fuller bhnd_nvram_io_getsize(struct bhnd_nvram_io *io)
16477cb4d3eSLandon J. Fuller {
16577cb4d3eSLandon J. Fuller return (io->iops->getsize(io));
16677cb4d3eSLandon J. Fuller }
16777cb4d3eSLandon J. Fuller
16877cb4d3eSLandon J. Fuller /**
16977cb4d3eSLandon J. Fuller * Attempt to set the size of @p io to @p size.
17077cb4d3eSLandon J. Fuller *
17177cb4d3eSLandon J. Fuller * If the total size of @p io is increased, the contents of the newly mapped
17277cb4d3eSLandon J. Fuller * bytes are undefined; concrete implementations of bhnd_nvram_io may
17377cb4d3eSLandon J. Fuller * provide stronger gaurantees.
17477cb4d3eSLandon J. Fuller *
17577cb4d3eSLandon J. Fuller * @param io NVRAM I/O context.
17677cb4d3eSLandon J. Fuller * @param size The new size.
17777cb4d3eSLandon J. Fuller *
17877cb4d3eSLandon J. Fuller * @retval 0 success
17977cb4d3eSLandon J. Fuller * @retval EIO if an I/O error occurs resizing @p io.
18077cb4d3eSLandon J. Fuller * @retval ENODEV if @p io does not support resizing.
18177cb4d3eSLandon J. Fuller * @retval ENXIO if @p size exceeds the capacity or other limits of @p io.
18277cb4d3eSLandon J. Fuller * @retval EFAULT if @p io requires I/O request alignment and @p size is
18377cb4d3eSLandon J. Fuller * misaligned.
18477cb4d3eSLandon J. Fuller */
18577cb4d3eSLandon J. Fuller int
bhnd_nvram_io_setsize(struct bhnd_nvram_io * io,size_t size)18677cb4d3eSLandon J. Fuller bhnd_nvram_io_setsize(struct bhnd_nvram_io *io, size_t size)
18777cb4d3eSLandon J. Fuller {
18877cb4d3eSLandon J. Fuller return (io->iops->setsize(io, size));
18977cb4d3eSLandon J. Fuller }
19077cb4d3eSLandon J. Fuller
19177cb4d3eSLandon J. Fuller /**
19277cb4d3eSLandon J. Fuller * Free a previously allocated I/O context, releasing all associated
19377cb4d3eSLandon J. Fuller * resources.
19477cb4d3eSLandon J. Fuller *
19577cb4d3eSLandon J. Fuller * @param io The I/O context to be freed.
19677cb4d3eSLandon J. Fuller */
19777cb4d3eSLandon J. Fuller void
bhnd_nvram_io_free(struct bhnd_nvram_io * io)19877cb4d3eSLandon J. Fuller bhnd_nvram_io_free(struct bhnd_nvram_io *io)
19977cb4d3eSLandon J. Fuller {
20077cb4d3eSLandon J. Fuller return (io->iops->free(io));
20177cb4d3eSLandon J. Fuller }
202