1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2014 by Chunwei Chen. All rights reserved.
23  * Copyright (c) 2016, 2019 by Delphix. All rights reserved.
24  */
25 
26 #ifndef _ABD_IMPL_H
27 #define	_ABD_IMPL_H
28 
29 #include <sys/abd.h>
30 #include <sys/wmsum.h>
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 typedef enum abd_stats_op {
37 	ABDSTAT_INCR, /* Increase abdstat values */
38 	ABDSTAT_DECR  /* Decrease abdstat values */
39 } abd_stats_op_t;
40 
41 struct scatterlist; /* forward declaration */
42 
43 struct abd_iter {
44 	/* public interface */
45 	void		*iter_mapaddr;	/* addr corresponding to iter_pos */
46 	size_t		iter_mapsize;	/* length of data valid at mapaddr */
47 
48 	/* private */
49 	abd_t		*iter_abd;	/* ABD being iterated through */
50 	size_t		iter_pos;
51 	size_t		iter_offset;	/* offset in current sg/abd_buf, */
52 					/* abd_offset included */
53 	struct scatterlist *iter_sg;	/* current sg */
54 };
55 
56 extern abd_t *abd_zero_scatter;
57 
58 abd_t *abd_gang_get_offset(abd_t *, size_t *);
59 abd_t *abd_alloc_struct(size_t);
60 void abd_free_struct(abd_t *);
61 
62 /*
63  * OS specific functions
64  */
65 
66 abd_t *abd_alloc_struct_impl(size_t);
67 abd_t *abd_get_offset_scatter(abd_t *, abd_t *, size_t, size_t);
68 void abd_free_struct_impl(abd_t *);
69 void abd_alloc_chunks(abd_t *, size_t);
70 void abd_free_chunks(abd_t *);
71 void abd_update_scatter_stats(abd_t *, abd_stats_op_t);
72 void abd_update_linear_stats(abd_t *, abd_stats_op_t);
73 void abd_verify_scatter(abd_t *);
74 void abd_free_linear_page(abd_t *);
75 /* OS specific abd_iter functions */
76 void abd_iter_init(struct abd_iter  *, abd_t *);
77 boolean_t abd_iter_at_end(struct abd_iter *);
78 void abd_iter_advance(struct abd_iter *, size_t);
79 void abd_iter_map(struct abd_iter *);
80 void abd_iter_unmap(struct abd_iter *);
81 
82 /*
83  * Helper macros
84  */
85 #define	ABDSTAT_INCR(stat, val) \
86 	wmsum_add(&abd_sums.stat, (val))
87 #define	ABDSTAT_BUMP(stat)	ABDSTAT_INCR(stat, 1)
88 #define	ABDSTAT_BUMPDOWN(stat)	ABDSTAT_INCR(stat, -1)
89 
90 #define	ABD_SCATTER(abd)	(abd->abd_u.abd_scatter)
91 #define	ABD_LINEAR_BUF(abd)	(abd->abd_u.abd_linear.abd_buf)
92 #define	ABD_GANG(abd)		(abd->abd_u.abd_gang)
93 
94 #if defined(_KERNEL)
95 #if defined(__FreeBSD__)
96 #define	abd_enter_critical(flags)	critical_enter()
97 #define	abd_exit_critical(flags)	critical_exit()
98 #else
99 #define	abd_enter_critical(flags)	local_irq_save(flags)
100 #define	abd_exit_critical(flags)	local_irq_restore(flags)
101 #endif
102 #else /* !_KERNEL */
103 #define	abd_enter_critical(flags)	((void)0)
104 #define	abd_exit_critical(flags)	((void)0)
105 #endif
106 
107 #ifdef __cplusplus
108 }
109 #endif
110 
111 #endif	/* _ABD_IMPL_H */
112