xref: /illumos-gate/usr/src/cmd/bhyve/block_if.h (revision 32640292339b07090f10ce34d455f98711077343)
1bf21cd93STycho Nightingale /*-
2*32640292SAndy Fiddaman  * SPDX-License-Identifier: BSD-2-Clause
34c87aefeSPatrick Mooney  *
4bf21cd93STycho Nightingale  * Copyright (c) 2013  Peter Grehan <grehan@freebsd.org>
5bf21cd93STycho Nightingale  * All rights reserved.
6bf21cd93STycho Nightingale  *
7bf21cd93STycho Nightingale  * Redistribution and use in source and binary forms, with or without
8bf21cd93STycho Nightingale  * modification, are permitted provided that the following conditions
9bf21cd93STycho Nightingale  * are met:
10bf21cd93STycho Nightingale  * 1. Redistributions of source code must retain the above copyright
11bf21cd93STycho Nightingale  *    notice, this list of conditions and the following disclaimer.
12bf21cd93STycho Nightingale  * 2. Redistributions in binary form must reproduce the above copyright
13bf21cd93STycho Nightingale  *    notice, this list of conditions and the following disclaimer in the
14bf21cd93STycho Nightingale  *    documentation and/or other materials provided with the distribution.
15bf21cd93STycho Nightingale  *
16bf21cd93STycho Nightingale  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
17bf21cd93STycho Nightingale  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18bf21cd93STycho Nightingale  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19bf21cd93STycho Nightingale  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20bf21cd93STycho Nightingale  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21bf21cd93STycho Nightingale  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22bf21cd93STycho Nightingale  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23bf21cd93STycho Nightingale  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24bf21cd93STycho Nightingale  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25bf21cd93STycho Nightingale  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26bf21cd93STycho Nightingale  * SUCH DAMAGE.
27bf21cd93STycho Nightingale  */
28bf21cd93STycho Nightingale 
29bf21cd93STycho Nightingale /*
30bf21cd93STycho Nightingale  * The block API to be used by bhyve block-device emulations. The routines
31bf21cd93STycho Nightingale  * are thread safe, with no assumptions about the context of the completion
32bf21cd93STycho Nightingale  * callback - it may occur in the caller's context, or asynchronously in
33bf21cd93STycho Nightingale  * another thread.
34bf21cd93STycho Nightingale  */
35bf21cd93STycho Nightingale 
36bf21cd93STycho Nightingale #ifndef _BLOCK_IF_H_
37bf21cd93STycho Nightingale #define _BLOCK_IF_H_
38bf21cd93STycho Nightingale 
392b948146SAndy Fiddaman #include <sys/nv.h>
40bf21cd93STycho Nightingale #include <sys/uio.h>
41bf21cd93STycho Nightingale #include <sys/unistd.h>
42bf21cd93STycho Nightingale 
434c87aefeSPatrick Mooney /*
444c87aefeSPatrick Mooney  * BLOCKIF_IOV_MAX is the maximum number of scatter/gather entries in
454c87aefeSPatrick Mooney  * a single request.  BLOCKIF_RING_MAX is the maxmimum number of
464c87aefeSPatrick Mooney  * pending requests that can be queued.
474c87aefeSPatrick Mooney  */
484c87aefeSPatrick Mooney #define	BLOCKIF_IOV_MAX		128	/* not practical to be IOV_MAX */
494c87aefeSPatrick Mooney #define	BLOCKIF_RING_MAX	128
50bf21cd93STycho Nightingale 
51bf21cd93STycho Nightingale struct blockif_req {
52bf21cd93STycho Nightingale 	int		br_iovcnt;
53bf21cd93STycho Nightingale 	off_t		br_offset;
544c87aefeSPatrick Mooney 	ssize_t		br_resid;
55bf21cd93STycho Nightingale 	void		(*br_callback)(struct blockif_req *req, int err);
56bf21cd93STycho Nightingale 	void		*br_param;
574c87aefeSPatrick Mooney 	struct iovec	br_iov[BLOCKIF_IOV_MAX];
58bf21cd93STycho Nightingale };
59bf21cd93STycho Nightingale 
60*32640292SAndy Fiddaman struct pci_devinst;
61bf21cd93STycho Nightingale struct blockif_ctxt;
62b0de25cbSAndy Fiddaman 
63b0de25cbSAndy Fiddaman typedef void blockif_resize_cb(struct blockif_ctxt *, void *, size_t);
64b0de25cbSAndy Fiddaman 
652b948146SAndy Fiddaman int	blockif_legacy_config(nvlist_t *nvl, const char *opts);
66*32640292SAndy Fiddaman int 	blockif_add_boot_device(struct pci_devinst *const pi, struct blockif_ctxt *const bc);
672b948146SAndy Fiddaman struct blockif_ctxt *blockif_open(nvlist_t *nvl, const char *ident);
68b0de25cbSAndy Fiddaman int	blockif_register_resize_callback(struct blockif_ctxt *bc,
69b0de25cbSAndy Fiddaman     blockif_resize_cb *cb, void *cb_arg);
70bf21cd93STycho Nightingale off_t	blockif_size(struct blockif_ctxt *bc);
71bf21cd93STycho Nightingale void	blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h,
72bf21cd93STycho Nightingale     uint8_t *s);
73bf21cd93STycho Nightingale int	blockif_sectsz(struct blockif_ctxt *bc);
744c87aefeSPatrick Mooney void	blockif_psectsz(struct blockif_ctxt *bc, int *size, int *off);
75bf21cd93STycho Nightingale int	blockif_queuesz(struct blockif_ctxt *bc);
76bf21cd93STycho Nightingale int	blockif_is_ro(struct blockif_ctxt *bc);
774c87aefeSPatrick Mooney int	blockif_candelete(struct blockif_ctxt *bc);
784c87aefeSPatrick Mooney #ifndef __FreeBSD__
794c87aefeSPatrick Mooney int	blockif_set_wce(struct blockif_ctxt *bc, int enable);
804c87aefeSPatrick Mooney #endif
81bf21cd93STycho Nightingale int	blockif_read(struct blockif_ctxt *bc, struct blockif_req *breq);
82bf21cd93STycho Nightingale int	blockif_write(struct blockif_ctxt *bc, struct blockif_req *breq);
83bf21cd93STycho Nightingale int	blockif_flush(struct blockif_ctxt *bc, struct blockif_req *breq);
844c87aefeSPatrick Mooney int	blockif_delete(struct blockif_ctxt *bc, struct blockif_req *breq);
85bf21cd93STycho Nightingale int	blockif_cancel(struct blockif_ctxt *bc, struct blockif_req *breq);
86bf21cd93STycho Nightingale int	blockif_close(struct blockif_ctxt *bc);
87bf21cd93STycho Nightingale 
88bf21cd93STycho Nightingale #endif /* _BLOCK_IF_H_ */
89