1/*-
2 * Copyright (c) 2012 EMC Corp.
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 AND CONTRIBUTORS ``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#ifndef _SYS_MEMDESC_H_
30#define	_SYS_MEMDESC_H_
31
32struct bio;
33struct bus_dma_segment;
34struct uio;
35struct mbuf;
36union ccb;
37
38/*
39 * struct memdesc encapsulates various memory descriptors and provides
40 * abstract access to them.
41 */
42struct 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
65static inline struct memdesc
66memdesc_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
77static inline struct memdesc
78memdesc_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
89static inline struct memdesc
90memdesc_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
101static inline struct memdesc
102memdesc_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
113static inline struct memdesc
114memdesc_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
124static inline struct memdesc
125memdesc_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
135static inline struct memdesc
136memdesc_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
146static inline struct memdesc
147memdesc_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