xref: /freebsd-13-stable/sys/sys/memdesc.h (revision f8167e0404dab9ffeaca95853dd237ab7c587f82)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012 EMC Corp.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef _SYS_MEMDESC_H_
30 #define	_SYS_MEMDESC_H_
31 
32 struct bio;
33 struct bus_dma_segment;
34 struct uio;
35 struct mbuf;
36 union ccb;
37 
38 /*
39  * struct memdesc encapsulates various memory descriptors and provides
40  * abstract access to them.
41  */
42 struct memdesc {
43 	union {
44 		void			*md_vaddr;
45 		vm_paddr_t		md_paddr;
46 		struct bus_dma_segment	*md_list;
47 		struct bio		*md_bio;
48 		struct uio		*md_uio;
49 		struct mbuf		*md_mbuf;
50 		union ccb		*md_ccb;
51 	} u;
52 	size_t		md_opaque;	/* type specific data. */
53 	uint32_t	md_type;	/* Type of memory. */
54 };
55 
56 #define	MEMDESC_VADDR	1	/* Contiguous virtual address. */
57 #define	MEMDESC_PADDR	2	/* Contiguous physical address. */
58 #define	MEMDESC_VLIST	3	/* scatter/gather list of kva addresses. */
59 #define	MEMDESC_PLIST	4	/* scatter/gather list of physical addresses. */
60 #define	MEMDESC_BIO	5	/* Pointer to a bio (block io). */
61 #define	MEMDESC_UIO	6	/* Pointer to a uio (any io). */
62 #define	MEMDESC_MBUF	7	/* Pointer to a mbuf (network io). */
63 #define	MEMDESC_CCB	8	/* Cam control block. (scsi/ata io). */
64 
65 static inline struct memdesc
memdesc_vaddr(void * vaddr,size_t len)66 memdesc_vaddr(void *vaddr, size_t len)
67 {
68 	struct memdesc mem;
69 
70 	mem.u.md_vaddr = vaddr;
71 	mem.md_opaque = len;
72 	mem.md_type = MEMDESC_VADDR;
73 
74 	return (mem);
75 }
76 
77 static inline struct memdesc
memdesc_paddr(vm_paddr_t paddr,size_t len)78 memdesc_paddr(vm_paddr_t paddr, size_t len)
79 {
80 	struct memdesc mem;
81 
82 	mem.u.md_paddr = paddr;
83 	mem.md_opaque = len;
84 	mem.md_type = MEMDESC_PADDR;
85 
86 	return (mem);
87 }
88 
89 static inline struct memdesc
memdesc_vlist(struct bus_dma_segment * vlist,int sglist_cnt)90 memdesc_vlist(struct bus_dma_segment *vlist, int sglist_cnt)
91 {
92 	struct memdesc mem;
93 
94 	mem.u.md_list = vlist;
95 	mem.md_opaque = sglist_cnt;
96 	mem.md_type = MEMDESC_VLIST;
97 
98 	return (mem);
99 }
100 
101 static inline struct memdesc
memdesc_plist(struct bus_dma_segment * plist,int sglist_cnt)102 memdesc_plist(struct bus_dma_segment *plist, int sglist_cnt)
103 {
104 	struct memdesc mem;
105 
106 	mem.u.md_list = plist;
107 	mem.md_opaque = sglist_cnt;
108 	mem.md_type = MEMDESC_PLIST;
109 
110 	return (mem);
111 }
112 
113 static inline struct memdesc
memdesc_bio(struct bio * bio)114 memdesc_bio(struct bio *bio)
115 {
116 	struct memdesc mem;
117 
118 	mem.u.md_bio = bio;
119 	mem.md_type = MEMDESC_BIO;
120 
121 	return (mem);
122 }
123 
124 static inline struct memdesc
memdesc_uio(struct uio * uio)125 memdesc_uio(struct uio *uio)
126 {
127 	struct memdesc mem;
128 
129 	mem.u.md_uio = uio;
130 	mem.md_type = MEMDESC_UIO;
131 
132 	return (mem);
133 }
134 
135 static inline struct memdesc
memdesc_mbuf(struct mbuf * mbuf)136 memdesc_mbuf(struct mbuf *mbuf)
137 {
138 	struct memdesc mem;
139 
140 	mem.u.md_mbuf = mbuf;
141 	mem.md_type = MEMDESC_MBUF;
142 
143 	return (mem);
144 }
145 
146 static inline struct memdesc
memdesc_ccb(union ccb * ccb)147 memdesc_ccb(union ccb *ccb)
148 {
149 	struct memdesc mem;
150 
151 	mem.u.md_ccb = ccb;
152 	mem.md_type = MEMDESC_CCB;
153 
154 	return (mem);
155 }
156 #endif /* _SYS_MEMDESC_H_ */
157