1221828Sgrehan/*-
2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3330449Seadler *
4221828Sgrehan * Copyright (c) 2011 NetApp, Inc.
5221828Sgrehan * All rights reserved.
6221828Sgrehan *
7221828Sgrehan * Redistribution and use in source and binary forms, with or without
8221828Sgrehan * modification, are permitted provided that the following conditions
9221828Sgrehan * are met:
10221828Sgrehan * 1. Redistributions of source code must retain the above copyright
11221828Sgrehan *    notice, this list of conditions and the following disclaimer.
12221828Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
13221828Sgrehan *    notice, this list of conditions and the following disclaimer in the
14221828Sgrehan *    documentation and/or other materials provided with the distribution.
15221828Sgrehan *
16221828Sgrehan * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17221828Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18221828Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19221828Sgrehan * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20221828Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21221828Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22221828Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23221828Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24221828Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25221828Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26221828Sgrehan * SUCH DAMAGE.
27221828Sgrehan *
28221828Sgrehan * $FreeBSD: stable/11/usr.sbin/bhyve/inout.h 330449 2018-03-05 07:26:05Z eadler $
29221828Sgrehan */
30221828Sgrehan
31221828Sgrehan#ifndef _INOUT_H_
32221828Sgrehan#define	_INOUT_H_
33221828Sgrehan
34221828Sgrehan#include <sys/linker_set.h>
35221828Sgrehan
36221828Sgrehanstruct vmctx;
37266573Sneelstruct vm_exit;
38221828Sgrehan
39269094Sneel/*
40269094Sneel * inout emulation handlers return 0 on success and -1 on failure.
41269094Sneel */
42221828Sgrehantypedef int (*inout_func_t)(struct vmctx *ctx, int vcpu, int in, int port,
43221828Sgrehan			    int bytes, uint32_t *eax, void *arg);
44221828Sgrehan
45221828Sgrehanstruct inout_port {
46221828Sgrehan	const char 	*name;
47221828Sgrehan	int		port;
48249321Sneel	int		size;
49221828Sgrehan	int		flags;
50221828Sgrehan	inout_func_t	handler;
51221828Sgrehan	void		*arg;
52221828Sgrehan};
53221828Sgrehan#define	IOPORT_F_IN		0x1
54221828Sgrehan#define	IOPORT_F_OUT		0x2
55257293Sneel#define	IOPORT_F_INOUT		(IOPORT_F_IN | IOPORT_F_OUT)
56221828Sgrehan
57257293Sneel/*
58257293Sneel * The following flags are used internally and must not be used by
59257293Sneel * device models.
60257293Sneel */
61257293Sneel#define	IOPORT_F_DEFAULT	0x80000000	/* claimed by default handler */
62257293Sneel
63221828Sgrehan#define	INOUT_PORT(name, port, flags, handler)				\
64221828Sgrehan	static struct inout_port __CONCAT(__inout_port, __LINE__) = {	\
65221828Sgrehan		#name,							\
66221828Sgrehan		(port),							\
67249321Sneel		1,							\
68221828Sgrehan		(flags),						\
69221942Sjhb		(handler),						\
70221942Sjhb		0							\
71221828Sgrehan	};								\
72221828Sgrehan	DATA_SET(inout_port_set, __CONCAT(__inout_port, __LINE__))
73221828Sgrehan
74221828Sgrehanvoid	init_inout(void);
75266573Sneelint	emulate_inout(struct vmctx *, int vcpu, struct vm_exit *vmexit,
76266573Sneel		      int strict);
77221828Sgrehanint	register_inout(struct inout_port *iop);
78249321Sneelint	unregister_inout(struct inout_port *iop);
79242192Sneelvoid	init_bvmcons(void);
80242192Sneel
81221828Sgrehan#endif	/* _INOUT_H_ */
82