19da9560cSBryan Venteicher /*-
29da9560cSBryan Venteicher * SPDX-License-Identifier: BSD-2-Clause
39da9560cSBryan Venteicher *
49da9560cSBryan Venteicher * Copyright (c) 2017, Bryan Venteicher <bryanv@FreeBSD.org>
59da9560cSBryan Venteicher * All rights reserved.
69da9560cSBryan Venteicher *
79da9560cSBryan Venteicher * Redistribution and use in source and binary forms, with or without
89da9560cSBryan Venteicher * modification, are permitted provided that the following conditions
99da9560cSBryan Venteicher * are met:
109da9560cSBryan Venteicher * 1. Redistributions of source code must retain the above copyright
119da9560cSBryan Venteicher * notice unmodified, this list of conditions, and the following
129da9560cSBryan Venteicher * disclaimer.
139da9560cSBryan Venteicher * 2. Redistributions in binary form must reproduce the above copyright
149da9560cSBryan Venteicher * notice, this list of conditions and the following disclaimer in the
159da9560cSBryan Venteicher * documentation and/or other materials provided with the distribution.
169da9560cSBryan Venteicher *
179da9560cSBryan Venteicher * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
189da9560cSBryan Venteicher * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
199da9560cSBryan Venteicher * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
209da9560cSBryan Venteicher * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
219da9560cSBryan Venteicher * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
229da9560cSBryan Venteicher * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
239da9560cSBryan Venteicher * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
249da9560cSBryan Venteicher * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
259da9560cSBryan Venteicher * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
269da9560cSBryan Venteicher * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
279da9560cSBryan Venteicher */
289da9560cSBryan Venteicher
299da9560cSBryan Venteicher #ifndef _VIRTIO_ENDIAN_H_
309da9560cSBryan Venteicher #define _VIRTIO_ENDIAN_H_
319da9560cSBryan Venteicher
329da9560cSBryan Venteicher #include <sys/endian.h>
3354ac6f72SKa Ho Ng #ifndef _KERNEL
3454ac6f72SKa Ho Ng #include <stdbool.h>
3554ac6f72SKa Ho Ng #endif /* _KERNEL */
369da9560cSBryan Venteicher
379da9560cSBryan Venteicher /*
389da9560cSBryan Venteicher * VirtIO V1 (modern) uses little endian, while legacy VirtIO uses the guest's
399da9560cSBryan Venteicher * native endian. These functions convert to and from the Guest's (driver's)
409da9560cSBryan Venteicher * and the Host's (device's) endianness when needed.
419da9560cSBryan Venteicher */
429da9560cSBryan Venteicher
439da9560cSBryan Venteicher static inline uint16_t
virtio_htog16(bool modern,uint16_t val)449da9560cSBryan Venteicher virtio_htog16(bool modern, uint16_t val)
459da9560cSBryan Venteicher {
46db494cebSMark Johnston if (modern)
479da9560cSBryan Venteicher return (le16toh(val));
489da9560cSBryan Venteicher else
499da9560cSBryan Venteicher return (val);
509da9560cSBryan Venteicher }
519da9560cSBryan Venteicher
529da9560cSBryan Venteicher static inline uint16_t
virtio_gtoh16(bool modern,uint16_t val)539da9560cSBryan Venteicher virtio_gtoh16(bool modern, uint16_t val)
549da9560cSBryan Venteicher {
55db494cebSMark Johnston if (modern)
569da9560cSBryan Venteicher return (htole16(val));
579da9560cSBryan Venteicher else
589da9560cSBryan Venteicher return (val);
599da9560cSBryan Venteicher }
609da9560cSBryan Venteicher
619da9560cSBryan Venteicher static inline uint32_t
virtio_htog32(bool modern,uint32_t val)629da9560cSBryan Venteicher virtio_htog32(bool modern, uint32_t val)
639da9560cSBryan Venteicher {
64db494cebSMark Johnston if (modern)
659da9560cSBryan Venteicher return (le32toh(val));
669da9560cSBryan Venteicher else
679da9560cSBryan Venteicher return (val);
689da9560cSBryan Venteicher }
699da9560cSBryan Venteicher
709da9560cSBryan Venteicher static inline uint32_t
virtio_gtoh32(bool modern,uint32_t val)719da9560cSBryan Venteicher virtio_gtoh32(bool modern, uint32_t val)
729da9560cSBryan Venteicher {
73db494cebSMark Johnston if (modern)
749da9560cSBryan Venteicher return (htole32(val));
759da9560cSBryan Venteicher else
769da9560cSBryan Venteicher return (val);
779da9560cSBryan Venteicher }
789da9560cSBryan Venteicher
799da9560cSBryan Venteicher static inline uint64_t
virtio_htog64(bool modern,uint64_t val)809da9560cSBryan Venteicher virtio_htog64(bool modern, uint64_t val)
819da9560cSBryan Venteicher {
82db494cebSMark Johnston if (modern)
839da9560cSBryan Venteicher return (le64toh(val));
849da9560cSBryan Venteicher else
859da9560cSBryan Venteicher return (val);
869da9560cSBryan Venteicher }
879da9560cSBryan Venteicher
889da9560cSBryan Venteicher static inline uint64_t
virtio_gtoh64(bool modern,uint64_t val)899da9560cSBryan Venteicher virtio_gtoh64(bool modern, uint64_t val)
909da9560cSBryan Venteicher {
91db494cebSMark Johnston if (modern)
929da9560cSBryan Venteicher return (htole64(val));
939da9560cSBryan Venteicher else
949da9560cSBryan Venteicher return (val);
959da9560cSBryan Venteicher }
969da9560cSBryan Venteicher
979da9560cSBryan Venteicher #endif /* _VIRTIO_ENDIAN_H_ */
98