1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 * $FreeBSD$
29 */
30
31#ifndef _SYS_MEMDESC_H_
32#define	_SYS_MEMDESC_H_
33
34struct bio;
35struct bus_dma_segment;
36struct uio;
37struct mbuf;
38union ccb;
39
40/*
41 * struct memdesc encapsulates various memory descriptors and provides
42 * abstract access to them.
43 */
44struct memdesc {
45	union {
46		void			*md_vaddr;
47		vm_paddr_t		md_paddr;
48		struct bus_dma_segment	*md_list;
49		struct bio		*md_bio;
50		struct uio		*md_uio;
51		struct mbuf		*md_mbuf;
52		union ccb		*md_ccb;
53	} u;
54	size_t		md_opaque;	/* type specific data. */
55	uint32_t	md_type;	/* Type of memory. */
56};
57
58#define	MEMDESC_VADDR	1	/* Contiguous virtual address. */
59#define	MEMDESC_PADDR	2	/* Contiguous physical address. */
60#define	MEMDESC_VLIST	3	/* scatter/gather list of kva addresses. */
61#define	MEMDESC_PLIST	4	/* scatter/gather list of physical addresses. */
62#define	MEMDESC_BIO	5	/* Pointer to a bio (block io). */
63#define	MEMDESC_UIO	6	/* Pointer to a uio (any io). */
64#define	MEMDESC_MBUF	7	/* Pointer to a mbuf (network io). */
65#define	MEMDESC_CCB	8	/* Cam control block. (scsi/ata io). */
66
67static inline struct memdesc
68memdesc_vaddr(void *vaddr, size_t len)
69{
70	struct memdesc mem;
71
72	mem.u.md_vaddr = vaddr;
73	mem.md_opaque = len;
74	mem.md_type = MEMDESC_VADDR;
75
76	return (mem);
77}
78
79static inline struct memdesc
80memdesc_paddr(vm_paddr_t paddr, size_t len)
81{
82	struct memdesc mem;
83
84	mem.u.md_paddr = paddr;
85	mem.md_opaque = len;
86	mem.md_type = MEMDESC_PADDR;
87
88	return (mem);
89}
90
91static inline struct memdesc
92memdesc_vlist(struct bus_dma_segment *vlist, int sglist_cnt)
93{
94	struct memdesc mem;
95
96	mem.u.md_list = vlist;
97	mem.md_opaque = sglist_cnt;
98	mem.md_type = MEMDESC_VLIST;
99
100	return (mem);
101}
102
103static inline struct memdesc
104memdesc_plist(struct bus_dma_segment *plist, int sglist_cnt)
105{
106	struct memdesc mem;
107
108	mem.u.md_list = plist;
109	mem.md_opaque = sglist_cnt;
110	mem.md_type = MEMDESC_PLIST;
111
112	return (mem);
113}
114
115static inline struct memdesc
116memdesc_bio(struct bio *bio)
117{
118	struct memdesc mem;
119
120	mem.u.md_bio = bio;
121	mem.md_type = MEMDESC_BIO;
122
123	return (mem);
124}
125
126static inline struct memdesc
127memdesc_uio(struct uio *uio)
128{
129	struct memdesc mem;
130
131	mem.u.md_uio = uio;
132	mem.md_type = MEMDESC_UIO;
133
134	return (mem);
135}
136
137static inline struct memdesc
138memdesc_mbuf(struct mbuf *mbuf)
139{
140	struct memdesc mem;
141
142	mem.u.md_mbuf = mbuf;
143	mem.md_type = MEMDESC_MBUF;
144
145	return (mem);
146}
147
148static inline struct memdesc
149memdesc_ccb(union ccb *ccb)
150{
151	struct memdesc mem;
152
153	mem.u.md_ccb = ccb;
154	mem.md_type = MEMDESC_CCB;
155
156	return (mem);
157}
158#endif /* _SYS_MEMDESC_H_ */
159