1 /*-
2  * Copyright (c) 2013  Peter Grehan <grehan@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 /*
30  * The block API to be used by acrn-dm block-device emulations. The routines
31  * are thread safe, with no assumptions about the context of the completion
32  * callback - it may occur in the caller's context, or asynchronously in
33  * another thread.
34  */
35 
36 #ifndef _BLOCK_IF_H_
37 #define _BLOCK_IF_H_
38 
39 #include <sys/uio.h>
40 #include <sys/unistd.h>
41 
42 #include "iothread.h"
43 
44 #define BLOCKIF_IOV_MAX		256	/* not practical to be IOV_MAX */
45 
46 /*
47  *  |<------------------------------------- bounced_size --------------------------------->|
48  *  |<-------- alignment ------->|                            |<-------- alignment ------->|
49  *  |<--- head --->|<------------------------ org_size ---------------------->|<-- tail -->|
50  *  |              |             |                            |               |            |
51  *  *--------------$-------------*----------- ... ------------*---------------$------------*
52  *  |              |             |                            |               |            |
53  *  |              start                                                      end          |
54  *  aligned_dn_start                                          aligned_dn_end
55  *  |__________head_area_________|                            |__________tail_area_________|
56  *  |<--- head --->|             |                            |<-- end_rmd -->|<-- tail -->|
57  *  |<-------- alignment ------->|                            |<-------- alignment ------->|
58  *
59  */
60 struct br_align_info {
61 	uint32_t	alignment;
62 
63 	bool		is_iov_base_aligned;
64 	bool		is_iov_len_aligned;
65 	bool		is_offset_aligned;
66 
67 	/*
68 	 * Needs to convert the misaligned request to an aligned one when
69 	 * O_DIRECT is used, but the request (either buffer address/length, or offset) is not aligned.
70 	 */
71 	bool		need_conversion;
72 
73 	uint32_t	head;
74 	uint32_t	tail;
75 	uint32_t	org_size;
76 	uint32_t	bounced_size;
77 
78 	off_t		aligned_dn_start;
79 	off_t		aligned_dn_end;
80 
81 	/*
82 	 * A bounce_iov for aligned read/write access.
83 	 * bounce_iov.iov_base is aligned to @alignment
84 	 * bounce_iov.iov_len is @bounced_size (@head + @org_size + @tail)
85 	 */
86 	struct iovec	bounce_iov;
87 };
88 
89 struct blockif_req {
90 	struct iovec		iov[BLOCKIF_IOV_MAX];
91 	int			iovcnt;
92 	off_t			offset;
93 	ssize_t			resid;
94 	void			(*callback)(struct blockif_req *req, int err);
95 	void			*param;
96 	int			qidx;
97 
98 	struct br_align_info	align_info;
99 };
100 
101 struct blockif_ctxt;
102 struct blockif_ctxt *blockif_open(const char *optstr, const char *ident, int queue_num,
103 	struct iothreads_info *iothrds_info);
104 off_t	blockif_size(struct blockif_ctxt *bc);
105 void	blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h,
106 		    uint8_t *s);
107 int	blockif_sectsz(struct blockif_ctxt *bc);
108 void	blockif_psectsz(struct blockif_ctxt *bc, int *size, int *off);
109 int	blockif_queuesz(struct blockif_ctxt *bc);
110 int	blockif_is_ro(struct blockif_ctxt *bc);
111 int	blockif_candiscard(struct blockif_ctxt *bc);
112 int	blockif_read(struct blockif_ctxt *bc, struct blockif_req *breq);
113 int	blockif_write(struct blockif_ctxt *bc, struct blockif_req *breq);
114 int	blockif_flush(struct blockif_ctxt *bc, struct blockif_req *breq);
115 int	blockif_discard(struct blockif_ctxt *bc, struct blockif_req *breq);
116 int	blockif_cancel(struct blockif_ctxt *bc, struct blockif_req *breq);
117 int	blockif_close(struct blockif_ctxt *bc);
118 uint8_t	blockif_get_wce(struct blockif_ctxt *bc);
119 void	blockif_set_wce(struct blockif_ctxt *bc, uint8_t wce);
120 int	blockif_flush_all(struct blockif_ctxt *bc);
121 int	blockif_max_discard_sectors(struct blockif_ctxt *bc);
122 int	blockif_max_discard_seg(struct blockif_ctxt *bc);
123 int	blockif_discard_sector_alignment(struct blockif_ctxt *bc);
124 
125 #endif /* _BLOCK_IF_H_ */
126