sglist.h revision 196404
1193260Sjhb/*-
2193260Sjhb * Copyright (c) 2008 Yahoo!, Inc.
3193260Sjhb * All rights reserved.
4193260Sjhb * Written by: John Baldwin <jhb@FreeBSD.org>
5193260Sjhb *
6193260Sjhb * Redistribution and use in source and binary forms, with or without
7193260Sjhb * modification, are permitted provided that the following conditions
8193260Sjhb * are met:
9193260Sjhb * 1. Redistributions of source code must retain the above copyright
10193260Sjhb *    notice, this list of conditions and the following disclaimer.
11193260Sjhb * 2. Redistributions in binary form must reproduce the above copyright
12193260Sjhb *    notice, this list of conditions and the following disclaimer in the
13193260Sjhb *    documentation and/or other materials provided with the distribution.
14193260Sjhb * 3. Neither the name of the author nor the names of any co-contributors
15193260Sjhb *    may be used to endorse or promote products derived from this software
16193260Sjhb *    without specific prior written permission.
17193260Sjhb *
18193260Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19193260Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20193260Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21193260Sjhb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22193260Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23193260Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24193260Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25193260Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26193260Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27193260Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28193260Sjhb * SUCH DAMAGE.
29193260Sjhb *
30193260Sjhb * $FreeBSD: head/sys/sys/sglist.h 196404 2009-08-20 19:23:58Z jhb $
31193260Sjhb */
32193260Sjhb
33193260Sjhb/*
34193260Sjhb * A scatter/gather list describes a group of physical address ranges.
35193260Sjhb * Each physical address range consists of a starting address and a
36193260Sjhb * length.
37193260Sjhb */
38193260Sjhb
39193260Sjhb#ifndef __SGLIST_H__
40193260Sjhb#define	__SGLIST_H__
41193260Sjhb
42193260Sjhb#include <sys/refcount.h>
43193260Sjhb
44193260Sjhbstruct sglist_seg {
45193260Sjhb	vm_paddr_t	ss_paddr;
46193260Sjhb	size_t		ss_len;
47193260Sjhb};
48193260Sjhb
49193260Sjhbstruct sglist {
50193260Sjhb	struct sglist_seg *sg_segs;
51193260Sjhb	int		sg_refs;
52193260Sjhb	u_short		sg_nseg;
53193260Sjhb	u_short		sg_maxseg;
54193260Sjhb};
55193260Sjhb
56193260Sjhbstruct mbuf;
57193260Sjhbstruct uio;
58193260Sjhb
59193260Sjhbstatic __inline void
60193260Sjhbsglist_init(struct sglist *sg, u_short maxsegs, struct sglist_seg *segs)
61193260Sjhb{
62193260Sjhb
63193260Sjhb	sg->sg_segs = segs;
64193260Sjhb	sg->sg_nseg = 0;
65193260Sjhb	sg->sg_maxseg = maxsegs;
66193260Sjhb	refcount_init(&sg->sg_refs, 1);
67193260Sjhb}
68193260Sjhb
69193260Sjhbstatic __inline void
70193260Sjhbsglist_reset(struct sglist *sg)
71193260Sjhb{
72193260Sjhb
73193260Sjhb	sg->sg_nseg = 0;
74193260Sjhb}
75193260Sjhb
76193260Sjhbstatic __inline struct sglist *
77193260Sjhbsglist_hold(struct sglist *sg)
78193260Sjhb{
79193260Sjhb
80193260Sjhb	refcount_acquire(&sg->sg_refs);
81193260Sjhb	return (sg);
82193260Sjhb}
83193260Sjhb
84193260Sjhbstruct sglist *sglist_alloc(int nsegs, int mflags);
85193260Sjhbint	sglist_append(struct sglist *sg, void *buf, size_t len);
86193260Sjhbint	sglist_append_mbuf(struct sglist *sg, struct mbuf *m0);
87193260Sjhbint	sglist_append_phys(struct sglist *sg, vm_paddr_t paddr,
88193260Sjhb	    size_t len);
89193260Sjhbint	sglist_append_uio(struct sglist *sg, struct uio *uio);
90193260Sjhbint	sglist_append_user(struct sglist *sg, void *buf, size_t len,
91193260Sjhb	    struct thread *td);
92193260Sjhbstruct sglist *sglist_build(void *buf, size_t len, int mflags);
93193260Sjhbstruct sglist *sglist_clone(struct sglist *sg, int mflags);
94196404Sjhbint	sglist_consume_uio(struct sglist *sg, struct uio *uio, size_t resid);
95193260Sjhbint	sglist_count(void *buf, size_t len);
96193260Sjhbvoid	sglist_free(struct sglist *sg);
97193260Sjhbint	sglist_join(struct sglist *first, struct sglist *second);
98193260Sjhbsize_t	sglist_length(struct sglist *sg);
99193260Sjhbint	sglist_slice(struct sglist *original, struct sglist **slice,
100193260Sjhb	    size_t offset, size_t length, int mflags);
101193260Sjhbint	sglist_split(struct sglist *original, struct sglist **head,
102193260Sjhb	    size_t length, int mflags);
103193260Sjhb
104193260Sjhb#endif	/* !__SGLIST_H__ */
105