xref: /freebsd/usr.sbin/bhyve/block_if.h (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
17cf5a7eeSPeter Grehan /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
47cf5a7eeSPeter Grehan  * Copyright (c) 2013  Peter Grehan <grehan@freebsd.org>
57cf5a7eeSPeter Grehan  * All rights reserved.
67cf5a7eeSPeter Grehan  *
77cf5a7eeSPeter Grehan  * Redistribution and use in source and binary forms, with or without
87cf5a7eeSPeter Grehan  * modification, are permitted provided that the following conditions
97cf5a7eeSPeter Grehan  * are met:
107cf5a7eeSPeter Grehan  * 1. Redistributions of source code must retain the above copyright
117cf5a7eeSPeter Grehan  *    notice, this list of conditions and the following disclaimer.
127cf5a7eeSPeter Grehan  * 2. Redistributions in binary form must reproduce the above copyright
137cf5a7eeSPeter Grehan  *    notice, this list of conditions and the following disclaimer in the
147cf5a7eeSPeter Grehan  *    documentation and/or other materials provided with the distribution.
157cf5a7eeSPeter Grehan  *
167cf5a7eeSPeter Grehan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
177cf5a7eeSPeter Grehan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
187cf5a7eeSPeter Grehan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
197cf5a7eeSPeter Grehan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
207cf5a7eeSPeter Grehan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
217cf5a7eeSPeter Grehan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
227cf5a7eeSPeter Grehan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
237cf5a7eeSPeter Grehan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
247cf5a7eeSPeter Grehan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
257cf5a7eeSPeter Grehan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
267cf5a7eeSPeter Grehan  * SUCH DAMAGE.
277cf5a7eeSPeter Grehan  */
287cf5a7eeSPeter Grehan 
297cf5a7eeSPeter Grehan /*
307cf5a7eeSPeter Grehan  * The block API to be used by bhyve block-device emulations. The routines
317cf5a7eeSPeter Grehan  * are thread safe, with no assumptions about the context of the completion
327cf5a7eeSPeter Grehan  * callback - it may occur in the caller's context, or asynchronously in
337cf5a7eeSPeter Grehan  * another thread.
347cf5a7eeSPeter Grehan  */
357cf5a7eeSPeter Grehan 
367cf5a7eeSPeter Grehan #ifndef _BLOCK_IF_H_
377cf5a7eeSPeter Grehan #define _BLOCK_IF_H_
387cf5a7eeSPeter Grehan 
39621b5090SJohn Baldwin #include <sys/nv.h>
407cf5a7eeSPeter Grehan #include <sys/uio.h>
417cf5a7eeSPeter Grehan #include <sys/unistd.h>
427cf5a7eeSPeter Grehan 
43483d953aSJohn Baldwin struct vm_snapshot_meta;
44483d953aSJohn Baldwin 
45483d953aSJohn Baldwin 
468c74ade8SJohn Baldwin /*
478c74ade8SJohn Baldwin  * BLOCKIF_IOV_MAX is the maximum number of scatter/gather entries in
488c74ade8SJohn Baldwin  * a single request.  BLOCKIF_RING_MAX is the maxmimum number of
498c74ade8SJohn Baldwin  * pending requests that can be queued.
508c74ade8SJohn Baldwin  */
518c74ade8SJohn Baldwin #define	BLOCKIF_IOV_MAX		128	/* not practical to be IOV_MAX */
528c74ade8SJohn Baldwin #define	BLOCKIF_RING_MAX	128
537cf5a7eeSPeter Grehan 
547cf5a7eeSPeter Grehan struct blockif_req {
557cf5a7eeSPeter Grehan 	int		br_iovcnt;
567cf5a7eeSPeter Grehan 	off_t		br_offset;
57bb1524afSAlexander Motin 	ssize_t		br_resid;
587cf5a7eeSPeter Grehan 	void		(*br_callback)(struct blockif_req *req, int err);
597cf5a7eeSPeter Grehan 	void		*br_param;
60c066c68cSMarcelo Araujo 	struct iovec	br_iov[BLOCKIF_IOV_MAX];
617cf5a7eeSPeter Grehan };
627cf5a7eeSPeter Grehan 
63*480bef94SCorvin Köhne struct pci_devinst;
647cf5a7eeSPeter Grehan struct blockif_ctxt;
658794846aSJohn Baldwin 
668794846aSJohn Baldwin typedef void blockif_resize_cb(struct blockif_ctxt *, void *, size_t);
678794846aSJohn Baldwin 
68621b5090SJohn Baldwin int	blockif_legacy_config(nvlist_t *nvl, const char *opts);
69*480bef94SCorvin Köhne int 	blockif_add_boot_device(struct pci_devinst *const pi, struct blockif_ctxt *const bc);
70621b5090SJohn Baldwin struct blockif_ctxt *blockif_open(nvlist_t *nvl, const char *ident);
718794846aSJohn Baldwin int	blockif_register_resize_callback(struct blockif_ctxt *bc,
728794846aSJohn Baldwin     blockif_resize_cb *cb, void *cb_arg);
737cf5a7eeSPeter Grehan off_t	blockif_size(struct blockif_ctxt *bc);
74c4813fadSPeter Grehan void	blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h,
75c4813fadSPeter Grehan     uint8_t *s);
767cf5a7eeSPeter Grehan int	blockif_sectsz(struct blockif_ctxt *bc);
7794682383SAlexander Motin void	blockif_psectsz(struct blockif_ctxt *bc, int *size, int *off);
787cf5a7eeSPeter Grehan int	blockif_queuesz(struct blockif_ctxt *bc);
797cf5a7eeSPeter Grehan int	blockif_is_ro(struct blockif_ctxt *bc);
800b9d25c9SAlexander Motin int	blockif_candelete(struct blockif_ctxt *bc);
817cf5a7eeSPeter Grehan int	blockif_read(struct blockif_ctxt *bc, struct blockif_req *breq);
827cf5a7eeSPeter Grehan int	blockif_write(struct blockif_ctxt *bc, struct blockif_req *breq);
837cf5a7eeSPeter Grehan int	blockif_flush(struct blockif_ctxt *bc, struct blockif_req *breq);
840b9d25c9SAlexander Motin int	blockif_delete(struct blockif_ctxt *bc, struct blockif_req *breq);
857cf5a7eeSPeter Grehan int	blockif_cancel(struct blockif_ctxt *bc, struct blockif_req *breq);
867cf5a7eeSPeter Grehan int	blockif_close(struct blockif_ctxt *bc);
87483d953aSJohn Baldwin #ifdef BHYVE_SNAPSHOT
88483d953aSJohn Baldwin void	blockif_pause(struct blockif_ctxt *bc);
89483d953aSJohn Baldwin void	blockif_resume(struct blockif_ctxt *bc);
90483d953aSJohn Baldwin #endif
917cf5a7eeSPeter Grehan 
927cf5a7eeSPeter Grehan #endif /* _BLOCK_IF_H_ */
93