Path: blob/main/sys/dev/bhnd/nvram/bhnd_nvram_io.c
39536 views
/*-1* Copyright (c) 2016 Landon Fuller <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer,9* without modification.10* 2. Redistributions in binary form must reproduce at minimum a disclaimer11* similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any12* redistribution must be conditioned upon including a substantially13* similar Disclaimer requirement for further binary redistribution.14*15* NO WARRANTY16* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18* LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY19* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL20* THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,21* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF22* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS23* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER24* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)25* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF26* THE POSSIBILITY OF SUCH DAMAGES.27*/2829#include <sys/cdefs.h>30#ifdef _KERNEL31#include <sys/param.h>32#else /* !_KERNEL */33#include <errno.h>34#include <stdint.h>35#include <stdlib.h>36#endif /* _KERNEL */3738#include "bhnd_nvram_io.h"39#include "bhnd_nvram_iovar.h"4041/**42* Read exactly @p nbytes from @p io at @p offset.43*44* @param io NVRAM I/O context.45* @param offset The offset within @p io at which to perform the read.46* @param[out] buffer Output buffer to which @p nbytes from @p io will be47* written.48* @param nbytes The maximum number of bytes to be read from @p io.49*50* @retval 0 success51* @retval EIO if an input error occurred reading @p io.52* @retval ENXIO if the request for @p offset or @p nbytes exceeds the size53* of @p io.54* @retval EFAULT if @p io requires I/O request alignment and @p offset is55* misaligned.56* @retval EFAULT if @p io requires I/O request alignment and @p nbytes is57* misaligned and cannot be rounded down to an aligned non-zero value.58*/59int60bhnd_nvram_io_read(struct bhnd_nvram_io *io, size_t offset, void *buffer,61size_t nbytes)62{63return (io->iops->read(io, offset, buffer, nbytes));64}6566/**67* Attempt to fetch a pointer to @p io's internal read buffer, if68* supported by @p io.69*70* The returned pointer is only gauranteed to be valid until the next I/O71* operation performed on @p io; concrete implementations of bhnd_nvram_io72* may provide stronger gaurantees.73*74* @param io NVRAM I/O context.75* @param offset The offset within @p io for which to return a buffer pointer.76* @param[out] ptr On success, will be initialized with a pointer to @p io's77* internal read buffer.78* @param nbytes The minimum number of bytes that must be readable at @p offset.79* @param[out] navail The actual number of readable bytes, which may be greater80* than @p nbytes. If this value is not required, a NULL pointer may be81* provided.82*83* @retval 0 success84* @retval EIO if an input error occurred reading @p io.85* @retval ENODEV if @p io does not support direct access to its backing read86* buffer.87* @retval ENXIO if the request exceeds the size of @p io.88* @retval EFAULT if @p io requires I/O request alignment and @p offset or89* @p nbytes are misaligned.90*/91int92bhnd_nvram_io_read_ptr(struct bhnd_nvram_io *io, size_t offset,93const void **ptr, size_t nbytes, size_t *navail)94{95return (io->iops->read_ptr(io, offset, ptr, nbytes, navail));96}9798/**99* Write @p nbytes to @p io at @p offset.100*101* @param io NVRAM I/O context.102* @param offset The offset within @p io at which to perform the write.103* @param buffer Data to be written to @p io.104* @param nbytes The number of bytes to be written from @p buffer.105*106* @retval 0 success107* @retval EIO if an output error occurs writing to @p io.108* @retval ENODEV if @p io does not support writing.109* @retval ENXIO if @p io does not support writes beyond the existing110* end-of-file, and a write at @p offset would exceed the size of the @p io111* backing data store.112* @retval EFAULT if @p io requires I/O request alignment and @p offset or113* @p nbytes are misaligned.114*/115int116bhnd_nvram_io_write(struct bhnd_nvram_io *io, size_t offset, void *buffer,117size_t nbytes)118{119return (io->iops->write(io, offset, buffer, nbytes));120}121122/**123* Attempt to fetch a writable pointer to @p io's internal write buffer, if124* supported by @p io.125*126* The returned pointer is only gauranteed to be valid until the next I/O127* operation performed on @p io; concrete implementations of bhnd_nvram_io128* may provide stronger gaurantees.129*130* @param io NVRAM I/O context.131* @param offset The offset within @p io for which to return a buffer pointer.132* @param[in,out] ptr On success, will be initialized with a pointer to @p io's133* internal buffer at which up to @p nbytes may be written.134* @param nbytes The minimum number of bytes that must be writable at @p offset.135* @param[out] navail The actual number of writable bytes, which may be greater136* than @p nbytes. If this value is not required, a NULL pointer may be137* provided.138*139* @retval 0 success140* @retval EIO if an output error occurs preparing @p io's write buffer.141* @retval ENODEV if @p io does not support direct access to its backing write142* buffer.143* @retval ENXIO if @p io does not support writes beyond the existing144* end-of-file, and a write at @p offset of @p nbytes would exceed the size of145* the @p io backing data store.146* @retval EFAULT if @p io requires I/O request alignment and @p offset or147* @p nbytes are misaligned.148*/149int150bhnd_nvram_io_write_ptr(struct bhnd_nvram_io *io, size_t offset, void **ptr,151size_t nbytes, size_t *navail)152{153return (io->iops->write_ptr(io, offset, ptr, nbytes, navail));154}155156/**157* Return the total number of bytes readable via @p io.158*159* @param io NVRAM I/O context.160*/161size_t162bhnd_nvram_io_getsize(struct bhnd_nvram_io *io)163{164return (io->iops->getsize(io));165}166167/**168* Attempt to set the size of @p io to @p size.169*170* If the total size of @p io is increased, the contents of the newly mapped171* bytes are undefined; concrete implementations of bhnd_nvram_io may172* provide stronger gaurantees.173*174* @param io NVRAM I/O context.175* @param size The new size.176*177* @retval 0 success178* @retval EIO if an I/O error occurs resizing @p io.179* @retval ENODEV if @p io does not support resizing.180* @retval ENXIO if @p size exceeds the capacity or other limits of @p io.181* @retval EFAULT if @p io requires I/O request alignment and @p size is182* misaligned.183*/184int185bhnd_nvram_io_setsize(struct bhnd_nvram_io *io, size_t size)186{187return (io->iops->setsize(io, size));188}189190/**191* Free a previously allocated I/O context, releasing all associated192* resources.193*194* @param io The I/O context to be freed.195*/196void197bhnd_nvram_io_free(struct bhnd_nvram_io *io)198{199return (io->iops->free(io));200}201202203