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
31193260Sjhb#include <sys/cdefs.h>
32193260Sjhb__FBSDID("$FreeBSD: releng/10.3/sys/kern/subr_sglist.c 260856 2014-01-18 18:36:41Z bryanv $");
33193260Sjhb
34193260Sjhb#include <sys/param.h>
35193260Sjhb#include <sys/kernel.h>
36260856Sbryanv#include <sys/bio.h>
37193260Sjhb#include <sys/malloc.h>
38193260Sjhb#include <sys/mbuf.h>
39193260Sjhb#include <sys/proc.h>
40193260Sjhb#include <sys/sglist.h>
41193260Sjhb#include <sys/uio.h>
42193260Sjhb
43193260Sjhb#include <vm/vm.h>
44260856Sbryanv#include <vm/vm_page.h>
45193260Sjhb#include <vm/pmap.h>
46193260Sjhb#include <vm/vm_map.h>
47193260Sjhb
48193260Sjhb#include <sys/ktr.h>
49193260Sjhb
50193260Sjhbstatic MALLOC_DEFINE(M_SGLIST, "sglist", "scatter/gather lists");
51193260Sjhb
52193260Sjhb/*
53196417Sjhb * Convenience macros to save the state of an sglist so it can be restored
54196417Sjhb * if an append attempt fails.  Since sglist's only grow we only need to
55196417Sjhb * save the current count of segments and the length of the ending segment.
56196417Sjhb * Earlier segments will not be changed by an append, and the only change
57196417Sjhb * that can occur to the ending segment is that it can be extended.
58196417Sjhb */
59196417Sjhbstruct sgsave {
60196417Sjhb	u_short sg_nseg;
61196417Sjhb	size_t ss_len;
62196417Sjhb};
63196417Sjhb
64196417Sjhb#define	SGLIST_SAVE(sg, sgsave) do {					\
65196417Sjhb	(sgsave).sg_nseg = (sg)->sg_nseg;				\
66196417Sjhb	if ((sgsave).sg_nseg > 0)					\
67196417Sjhb		(sgsave).ss_len = (sg)->sg_segs[(sgsave).sg_nseg - 1].ss_len; \
68196417Sjhb	else								\
69196417Sjhb		(sgsave).ss_len = 0;					\
70196417Sjhb} while (0)
71196417Sjhb
72196417Sjhb#define	SGLIST_RESTORE(sg, sgsave) do {					\
73196417Sjhb	(sg)->sg_nseg = (sgsave).sg_nseg;				\
74196417Sjhb	if ((sgsave).sg_nseg > 0)					\
75196417Sjhb		(sg)->sg_segs[(sgsave).sg_nseg - 1].ss_len = (sgsave).ss_len; \
76196417Sjhb} while (0)
77196417Sjhb
78196417Sjhb/*
79193260Sjhb * Append a single (paddr, len) to a sglist.  sg is the list and ss is
80193260Sjhb * the current segment in the list.  If we run out of segments then
81193260Sjhb * EFBIG will be returned.
82193260Sjhb */
83193260Sjhbstatic __inline int
84193260Sjhb_sglist_append_range(struct sglist *sg, struct sglist_seg **ssp,
85193260Sjhb    vm_paddr_t paddr, size_t len)
86193260Sjhb{
87193260Sjhb	struct sglist_seg *ss;
88193260Sjhb
89193260Sjhb	ss = *ssp;
90193260Sjhb	if (ss->ss_paddr + ss->ss_len == paddr)
91193260Sjhb		ss->ss_len += len;
92193260Sjhb	else {
93196417Sjhb		if (sg->sg_nseg == sg->sg_maxseg)
94193260Sjhb			return (EFBIG);
95193260Sjhb		ss++;
96193260Sjhb		ss->ss_paddr = paddr;
97193260Sjhb		ss->ss_len = len;
98193260Sjhb		sg->sg_nseg++;
99193260Sjhb		*ssp = ss;
100193260Sjhb	}
101193260Sjhb	return (0);
102193260Sjhb}
103193260Sjhb
104193260Sjhb/*
105193260Sjhb * Worker routine to append a virtual address range (either kernel or
106193260Sjhb * user) to a scatter/gather list.
107193260Sjhb */
108193260Sjhbstatic __inline int
109193260Sjhb_sglist_append_buf(struct sglist *sg, void *buf, size_t len, pmap_t pmap,
110193260Sjhb    size_t *donep)
111193260Sjhb{
112193260Sjhb	struct sglist_seg *ss;
113193260Sjhb	vm_offset_t vaddr, offset;
114193260Sjhb	vm_paddr_t paddr;
115193260Sjhb	size_t seglen;
116193260Sjhb	int error;
117193260Sjhb
118193260Sjhb	if (donep)
119193260Sjhb		*donep = 0;
120193260Sjhb	if (len == 0)
121193260Sjhb		return (0);
122193260Sjhb
123193260Sjhb	/* Do the first page.  It may have an offset. */
124193260Sjhb	vaddr = (vm_offset_t)buf;
125193260Sjhb	offset = vaddr & PAGE_MASK;
126193260Sjhb	if (pmap != NULL)
127193260Sjhb		paddr = pmap_extract(pmap, vaddr);
128193260Sjhb	else
129193260Sjhb		paddr = pmap_kextract(vaddr);
130193260Sjhb	seglen = MIN(len, PAGE_SIZE - offset);
131193260Sjhb	if (sg->sg_nseg == 0) {
132193260Sjhb		ss = sg->sg_segs;
133193260Sjhb		ss->ss_paddr = paddr;
134193260Sjhb		ss->ss_len = seglen;
135193260Sjhb		sg->sg_nseg = 1;
136193260Sjhb	} else {
137193260Sjhb		ss = &sg->sg_segs[sg->sg_nseg - 1];
138193260Sjhb		error = _sglist_append_range(sg, &ss, paddr, seglen);
139196417Sjhb		if (error)
140196417Sjhb			return (error);
141193260Sjhb	}
142196417Sjhb	vaddr += seglen;
143196417Sjhb	len -= seglen;
144196417Sjhb	if (donep)
145196417Sjhb		*donep += seglen;
146193260Sjhb
147196417Sjhb	while (len > 0) {
148193260Sjhb		seglen = MIN(len, PAGE_SIZE);
149193260Sjhb		if (pmap != NULL)
150193260Sjhb			paddr = pmap_extract(pmap, vaddr);
151193260Sjhb		else
152193260Sjhb			paddr = pmap_kextract(vaddr);
153193260Sjhb		error = _sglist_append_range(sg, &ss, paddr, seglen);
154196417Sjhb		if (error)
155196417Sjhb			return (error);
156196417Sjhb		vaddr += seglen;
157196417Sjhb		len -= seglen;
158196417Sjhb		if (donep)
159196417Sjhb			*donep += seglen;
160193260Sjhb	}
161193260Sjhb
162196417Sjhb	return (0);
163193260Sjhb}
164193260Sjhb
165193260Sjhb/*
166193260Sjhb * Determine the number of scatter/gather list elements needed to
167193260Sjhb * describe a kernel virtual address range.
168193260Sjhb */
169193260Sjhbint
170193260Sjhbsglist_count(void *buf, size_t len)
171193260Sjhb{
172193260Sjhb	vm_offset_t vaddr, vendaddr;
173193260Sjhb	vm_paddr_t lastaddr, paddr;
174193260Sjhb	int nsegs;
175193260Sjhb
176193260Sjhb	if (len == 0)
177193260Sjhb		return (0);
178193260Sjhb
179193260Sjhb	vaddr = trunc_page((vm_offset_t)buf);
180193260Sjhb	vendaddr = (vm_offset_t)buf + len;
181193260Sjhb	nsegs = 1;
182193260Sjhb	lastaddr = pmap_kextract(vaddr);
183193260Sjhb	vaddr += PAGE_SIZE;
184193260Sjhb	while (vaddr < vendaddr) {
185193260Sjhb		paddr = pmap_kextract(vaddr);
186193260Sjhb		if (lastaddr + PAGE_SIZE != paddr)
187193260Sjhb			nsegs++;
188193260Sjhb		lastaddr = paddr;
189193260Sjhb		vaddr += PAGE_SIZE;
190193260Sjhb	}
191193260Sjhb	return (nsegs);
192193260Sjhb}
193193260Sjhb
194193260Sjhb/*
195193260Sjhb * Allocate a scatter/gather list along with 'nsegs' segments.  The
196193260Sjhb * 'mflags' parameters are the same as passed to malloc(9).  The caller
197193260Sjhb * should use sglist_free() to free this list.
198193260Sjhb */
199193260Sjhbstruct sglist *
200193260Sjhbsglist_alloc(int nsegs, int mflags)
201193260Sjhb{
202193260Sjhb	struct sglist *sg;
203193260Sjhb
204193260Sjhb	sg = malloc(sizeof(struct sglist) + nsegs * sizeof(struct sglist_seg),
205193260Sjhb	    M_SGLIST, mflags);
206193260Sjhb	if (sg == NULL)
207193260Sjhb		return (NULL);
208193260Sjhb	sglist_init(sg, nsegs, (struct sglist_seg *)(sg + 1));
209193260Sjhb	return (sg);
210193260Sjhb}
211193260Sjhb
212193260Sjhb/*
213193260Sjhb * Free a scatter/gather list allocated via sglist_allc().
214193260Sjhb */
215193260Sjhbvoid
216193260Sjhbsglist_free(struct sglist *sg)
217193260Sjhb{
218193260Sjhb
219193260Sjhb	if (refcount_release(&sg->sg_refs))
220193260Sjhb		free(sg, M_SGLIST);
221193260Sjhb}
222193260Sjhb
223193260Sjhb/*
224193260Sjhb * Append the segments to describe a single kernel virtual address
225193260Sjhb * range to a scatter/gather list.  If there are insufficient
226193260Sjhb * segments, then this fails with EFBIG.
227193260Sjhb */
228193260Sjhbint
229193260Sjhbsglist_append(struct sglist *sg, void *buf, size_t len)
230193260Sjhb{
231196417Sjhb	struct sgsave save;
232196417Sjhb	int error;
233193260Sjhb
234193260Sjhb	if (sg->sg_maxseg == 0)
235193260Sjhb		return (EINVAL);
236196417Sjhb	SGLIST_SAVE(sg, save);
237196417Sjhb	error = _sglist_append_buf(sg, buf, len, NULL, NULL);
238196417Sjhb	if (error)
239196417Sjhb		SGLIST_RESTORE(sg, save);
240196417Sjhb	return (error);
241193260Sjhb}
242193260Sjhb
243193260Sjhb/*
244260856Sbryanv * Append the segments to describe a bio's data to a scatter/gather list.
245260856Sbryanv * If there are insufficient segments, then this fails with EFBIG.
246260856Sbryanv *
247260856Sbryanv * NOTE: This function expects bio_bcount to be initialized.
248260856Sbryanv */
249260856Sbryanvint
250260856Sbryanvsglist_append_bio(struct sglist *sg, struct bio *bp)
251260856Sbryanv{
252260856Sbryanv	struct sgsave save;
253260856Sbryanv	vm_paddr_t paddr;
254260856Sbryanv	size_t len, tlen;
255260856Sbryanv	int error, i, ma_offs;
256260856Sbryanv
257260856Sbryanv	if ((bp->bio_flags & BIO_UNMAPPED) == 0) {
258260856Sbryanv		error = sglist_append(sg, bp->bio_data, bp->bio_bcount);
259260856Sbryanv		return (error);
260260856Sbryanv	}
261260856Sbryanv
262260856Sbryanv	if (sg->sg_maxseg == 0)
263260856Sbryanv		return (EINVAL);
264260856Sbryanv
265260856Sbryanv	SGLIST_SAVE(sg, save);
266260856Sbryanv	tlen = bp->bio_bcount;
267260856Sbryanv	ma_offs = bp->bio_ma_offset;
268260856Sbryanv	for (i = 0; tlen > 0; i++, tlen -= len) {
269260856Sbryanv		len = min(PAGE_SIZE - ma_offs, tlen);
270260856Sbryanv		paddr = VM_PAGE_TO_PHYS(bp->bio_ma[i]) + ma_offs;
271260856Sbryanv		error = sglist_append_phys(sg, paddr, len);
272260856Sbryanv		if (error) {
273260856Sbryanv			SGLIST_RESTORE(sg, save);
274260856Sbryanv			return (error);
275260856Sbryanv		}
276260856Sbryanv		ma_offs = 0;
277260856Sbryanv	}
278260856Sbryanv	return (0);
279260856Sbryanv}
280260856Sbryanv
281260856Sbryanv/*
282193260Sjhb * Append a single physical address range to a scatter/gather list.
283193260Sjhb * If there are insufficient segments, then this fails with EFBIG.
284193260Sjhb */
285193260Sjhbint
286193260Sjhbsglist_append_phys(struct sglist *sg, vm_paddr_t paddr, size_t len)
287193260Sjhb{
288193260Sjhb	struct sglist_seg *ss;
289196417Sjhb	struct sgsave save;
290196417Sjhb	int error;
291193260Sjhb
292193260Sjhb	if (sg->sg_maxseg == 0)
293193260Sjhb		return (EINVAL);
294193260Sjhb	if (len == 0)
295193260Sjhb		return (0);
296193260Sjhb
297193260Sjhb	if (sg->sg_nseg == 0) {
298193260Sjhb		sg->sg_segs[0].ss_paddr = paddr;
299193260Sjhb		sg->sg_segs[0].ss_len = len;
300193260Sjhb		sg->sg_nseg = 1;
301193260Sjhb		return (0);
302193260Sjhb	}
303193260Sjhb	ss = &sg->sg_segs[sg->sg_nseg - 1];
304196417Sjhb	SGLIST_SAVE(sg, save);
305196417Sjhb	error = _sglist_append_range(sg, &ss, paddr, len);
306196417Sjhb	if (error)
307196417Sjhb		SGLIST_RESTORE(sg, save);
308196417Sjhb	return (error);
309193260Sjhb}
310193260Sjhb
311193260Sjhb/*
312193260Sjhb * Append the segments that describe a single mbuf chain to a
313193260Sjhb * scatter/gather list.  If there are insufficient segments, then this
314193260Sjhb * fails with EFBIG.
315193260Sjhb */
316193260Sjhbint
317193260Sjhbsglist_append_mbuf(struct sglist *sg, struct mbuf *m0)
318193260Sjhb{
319196417Sjhb	struct sgsave save;
320193260Sjhb	struct mbuf *m;
321193260Sjhb	int error;
322193260Sjhb
323193260Sjhb	if (sg->sg_maxseg == 0)
324193260Sjhb		return (EINVAL);
325193260Sjhb
326193260Sjhb	error = 0;
327196417Sjhb	SGLIST_SAVE(sg, save);
328193260Sjhb	for (m = m0; m != NULL; m = m->m_next) {
329193260Sjhb		if (m->m_len > 0) {
330193260Sjhb			error = sglist_append(sg, m->m_data, m->m_len);
331196417Sjhb			if (error) {
332196417Sjhb				SGLIST_RESTORE(sg, save);
333193260Sjhb				return (error);
334196417Sjhb			}
335193260Sjhb		}
336193260Sjhb	}
337193260Sjhb	return (0);
338193260Sjhb}
339193260Sjhb
340193260Sjhb/*
341193260Sjhb * Append the segments that describe a single user address range to a
342193260Sjhb * scatter/gather list.  If there are insufficient segments, then this
343193260Sjhb * fails with EFBIG.
344193260Sjhb */
345193260Sjhbint
346193260Sjhbsglist_append_user(struct sglist *sg, void *buf, size_t len, struct thread *td)
347193260Sjhb{
348196417Sjhb	struct sgsave save;
349196417Sjhb	int error;
350193260Sjhb
351193260Sjhb	if (sg->sg_maxseg == 0)
352193260Sjhb		return (EINVAL);
353196417Sjhb	SGLIST_SAVE(sg, save);
354196417Sjhb	error = _sglist_append_buf(sg, buf, len,
355196417Sjhb	    vmspace_pmap(td->td_proc->p_vmspace), NULL);
356196417Sjhb	if (error)
357196417Sjhb		SGLIST_RESTORE(sg, save);
358196417Sjhb	return (error);
359193260Sjhb}
360193260Sjhb
361193260Sjhb/*
362193260Sjhb * Append the segments that describe a single uio to a scatter/gather
363193260Sjhb * list.  If there are insufficient segments, then this fails with
364193260Sjhb * EFBIG.
365193260Sjhb */
366193260Sjhbint
367193260Sjhbsglist_append_uio(struct sglist *sg, struct uio *uio)
368193260Sjhb{
369193260Sjhb	struct iovec *iov;
370196417Sjhb	struct sgsave save;
371193260Sjhb	size_t resid, minlen;
372193260Sjhb	pmap_t pmap;
373193260Sjhb	int error, i;
374193260Sjhb
375193260Sjhb	if (sg->sg_maxseg == 0)
376193260Sjhb		return (EINVAL);
377193260Sjhb
378193260Sjhb	resid = uio->uio_resid;
379193260Sjhb	iov = uio->uio_iov;
380193260Sjhb
381193260Sjhb	if (uio->uio_segflg == UIO_USERSPACE) {
382193260Sjhb		KASSERT(uio->uio_td != NULL,
383193260Sjhb		    ("sglist_append_uio: USERSPACE but no thread"));
384193260Sjhb		pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
385193260Sjhb	} else
386193260Sjhb		pmap = NULL;
387193260Sjhb
388193260Sjhb	error = 0;
389196417Sjhb	SGLIST_SAVE(sg, save);
390193260Sjhb	for (i = 0; i < uio->uio_iovcnt && resid != 0; i++) {
391193260Sjhb		/*
392193260Sjhb		 * Now at the first iovec to load.  Load each iovec
393193260Sjhb		 * until we have exhausted the residual count.
394193260Sjhb		 */
395193260Sjhb		minlen = MIN(resid, iov[i].iov_len);
396193260Sjhb		if (minlen > 0) {
397193260Sjhb			error = _sglist_append_buf(sg, iov[i].iov_base, minlen,
398193260Sjhb			    pmap, NULL);
399196417Sjhb			if (error) {
400196417Sjhb				SGLIST_RESTORE(sg, save);
401193260Sjhb				return (error);
402196417Sjhb			}
403193260Sjhb			resid -= minlen;
404193260Sjhb		}
405193260Sjhb	}
406193260Sjhb	return (0);
407193260Sjhb}
408193260Sjhb
409193260Sjhb/*
410193260Sjhb * Append the segments that describe at most 'resid' bytes from a
411193260Sjhb * single uio to a scatter/gather list.  If there are insufficient
412193260Sjhb * segments, then only the amount that fits is appended.
413193260Sjhb */
414193260Sjhbint
415196404Sjhbsglist_consume_uio(struct sglist *sg, struct uio *uio, size_t resid)
416193260Sjhb{
417193260Sjhb	struct iovec *iov;
418193260Sjhb	size_t done;
419193260Sjhb	pmap_t pmap;
420193260Sjhb	int error, len;
421193260Sjhb
422193260Sjhb	if (sg->sg_maxseg == 0)
423193260Sjhb		return (EINVAL);
424193260Sjhb
425193260Sjhb	if (uio->uio_segflg == UIO_USERSPACE) {
426193260Sjhb		KASSERT(uio->uio_td != NULL,
427193260Sjhb		    ("sglist_consume_uio: USERSPACE but no thread"));
428193260Sjhb		pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
429193260Sjhb	} else
430193260Sjhb		pmap = NULL;
431193260Sjhb
432193260Sjhb	error = 0;
433193260Sjhb	while (resid > 0 && uio->uio_resid) {
434193260Sjhb		iov = uio->uio_iov;
435193260Sjhb		len = iov->iov_len;
436193260Sjhb		if (len == 0) {
437193260Sjhb			uio->uio_iov++;
438193260Sjhb			uio->uio_iovcnt--;
439193260Sjhb			continue;
440193260Sjhb		}
441193260Sjhb		if (len > resid)
442193260Sjhb			len = resid;
443193260Sjhb
444193260Sjhb		/*
445193260Sjhb		 * Try to append this iovec.  If we run out of room,
446193260Sjhb		 * then break out of the loop.
447193260Sjhb		 */
448193260Sjhb		error = _sglist_append_buf(sg, iov->iov_base, len, pmap, &done);
449193260Sjhb		iov->iov_base = (char *)iov->iov_base + done;
450193260Sjhb		iov->iov_len -= done;
451193260Sjhb		uio->uio_resid -= done;
452193260Sjhb		uio->uio_offset += done;
453193260Sjhb		resid -= done;
454193260Sjhb		if (error)
455193260Sjhb			break;
456193260Sjhb	}
457193260Sjhb	return (0);
458193260Sjhb}
459193260Sjhb
460193260Sjhb/*
461193260Sjhb * Allocate and populate a scatter/gather list to describe a single
462193260Sjhb * kernel virtual address range.
463193260Sjhb */
464193260Sjhbstruct sglist *
465193260Sjhbsglist_build(void *buf, size_t len, int mflags)
466193260Sjhb{
467193260Sjhb	struct sglist *sg;
468193260Sjhb	int nsegs;
469193260Sjhb
470193260Sjhb	if (len == 0)
471193260Sjhb		return (NULL);
472193260Sjhb
473193260Sjhb	nsegs = sglist_count(buf, len);
474193260Sjhb	sg = sglist_alloc(nsegs, mflags);
475193260Sjhb	if (sg == NULL)
476193260Sjhb		return (NULL);
477193260Sjhb	if (sglist_append(sg, buf, len) != 0) {
478193260Sjhb		sglist_free(sg);
479193260Sjhb		return (NULL);
480193260Sjhb	}
481193260Sjhb	return (sg);
482193260Sjhb}
483193260Sjhb
484193260Sjhb/*
485193260Sjhb * Clone a new copy of a scatter/gather list.
486193260Sjhb */
487193260Sjhbstruct sglist *
488193260Sjhbsglist_clone(struct sglist *sg, int mflags)
489193260Sjhb{
490193260Sjhb	struct sglist *new;
491193260Sjhb
492193260Sjhb	if (sg == NULL)
493193260Sjhb		return (NULL);
494193260Sjhb	new = sglist_alloc(sg->sg_maxseg, mflags);
495193260Sjhb	if (new == NULL)
496193260Sjhb		return (NULL);
497196417Sjhb	new->sg_nseg = sg->sg_nseg;
498193260Sjhb	bcopy(sg->sg_segs, new->sg_segs, sizeof(struct sglist_seg) *
499193260Sjhb	    sg->sg_nseg);
500193260Sjhb	return (new);
501193260Sjhb}
502193260Sjhb
503193260Sjhb/*
504193260Sjhb * Calculate the total length of the segments described in a
505193260Sjhb * scatter/gather list.
506193260Sjhb */
507193260Sjhbsize_t
508193260Sjhbsglist_length(struct sglist *sg)
509193260Sjhb{
510193260Sjhb	size_t space;
511193260Sjhb	int i;
512193260Sjhb
513193260Sjhb	space = 0;
514193260Sjhb	for (i = 0; i < sg->sg_nseg; i++)
515193260Sjhb		space += sg->sg_segs[i].ss_len;
516193260Sjhb	return (space);
517193260Sjhb}
518193260Sjhb
519193260Sjhb/*
520193260Sjhb * Split a scatter/gather list into two lists.  The scatter/gather
521193260Sjhb * entries for the first 'length' bytes of the 'original' list are
522193260Sjhb * stored in the '*head' list and are removed from 'original'.
523193260Sjhb *
524193260Sjhb * If '*head' is NULL, then a new list will be allocated using
525193260Sjhb * 'mflags'.  If M_NOWAIT is specified and the allocation fails,
526193260Sjhb * ENOMEM will be returned.
527193260Sjhb *
528193260Sjhb * If '*head' is not NULL, it should point to an empty sglist.  If it
529193260Sjhb * does not have enough room for the remaining space, then EFBIG will
530193260Sjhb * be returned.  If '*head' is not empty, then EINVAL will be
531193260Sjhb * returned.
532193260Sjhb *
533193260Sjhb * If 'original' is shared (refcount > 1), then EDOOFUS will be
534193260Sjhb * returned.
535193260Sjhb */
536193260Sjhbint
537193260Sjhbsglist_split(struct sglist *original, struct sglist **head, size_t length,
538193260Sjhb    int mflags)
539193260Sjhb{
540193260Sjhb	struct sglist *sg;
541193260Sjhb	size_t space, split;
542193260Sjhb	int count, i;
543193260Sjhb
544193260Sjhb	if (original->sg_refs > 1)
545193260Sjhb		return (EDOOFUS);
546193260Sjhb
547193260Sjhb	/* Figure out how big of a sglist '*head' has to hold. */
548193260Sjhb	count = 0;
549193260Sjhb	space = 0;
550193260Sjhb	split = 0;
551193260Sjhb	for (i = 0; i < original->sg_nseg; i++) {
552193260Sjhb		space += original->sg_segs[i].ss_len;
553193260Sjhb		count++;
554193260Sjhb		if (space >= length) {
555193260Sjhb			/*
556193260Sjhb			 * If 'length' falls in the middle of a
557193260Sjhb			 * scatter/gather list entry, then 'split'
558193260Sjhb			 * holds how much of that entry will remain in
559193260Sjhb			 * 'original'.
560193260Sjhb			 */
561193260Sjhb			split = space - length;
562193260Sjhb			break;
563193260Sjhb		}
564193260Sjhb	}
565193260Sjhb
566193260Sjhb	/* Nothing to do, so leave head empty. */
567193260Sjhb	if (count == 0)
568193260Sjhb		return (0);
569193260Sjhb
570193260Sjhb	if (*head == NULL) {
571193260Sjhb		sg = sglist_alloc(count, mflags);
572193260Sjhb		if (sg == NULL)
573193260Sjhb			return (ENOMEM);
574193260Sjhb		*head = sg;
575193260Sjhb	} else {
576193260Sjhb		sg = *head;
577193260Sjhb		if (sg->sg_maxseg < count)
578193260Sjhb			return (EFBIG);
579193260Sjhb		if (sg->sg_nseg != 0)
580193260Sjhb			return (EINVAL);
581193260Sjhb	}
582193260Sjhb
583193260Sjhb	/* Copy 'count' entries to 'sg' from 'original'. */
584193260Sjhb	bcopy(original->sg_segs, sg->sg_segs, count *
585193260Sjhb	    sizeof(struct sglist_seg));
586193260Sjhb	sg->sg_nseg = count;
587193260Sjhb
588193260Sjhb	/*
589193260Sjhb	 * If we had to split a list entry, fixup the last entry in
590193260Sjhb	 * 'sg' and the new first entry in 'original'.  We also
591193260Sjhb	 * decrement 'count' by 1 since we will only be removing
592193260Sjhb	 * 'count - 1' segments from 'original' now.
593193260Sjhb	 */
594193260Sjhb	if (split != 0) {
595193260Sjhb		count--;
596193260Sjhb		sg->sg_segs[count].ss_len -= split;
597193260Sjhb		original->sg_segs[count].ss_paddr =
598193260Sjhb		    sg->sg_segs[count].ss_paddr + split;
599193260Sjhb		original->sg_segs[count].ss_len = split;
600193260Sjhb	}
601193260Sjhb
602193260Sjhb	/* Trim 'count' entries from the front of 'original'. */
603193260Sjhb	original->sg_nseg -= count;
604193260Sjhb	bcopy(original->sg_segs + count, original->sg_segs, count *
605193260Sjhb	    sizeof(struct sglist_seg));
606193260Sjhb	return (0);
607193260Sjhb}
608193260Sjhb
609193260Sjhb/*
610193260Sjhb * Append the scatter/gather list elements in 'second' to the
611193260Sjhb * scatter/gather list 'first'.  If there is not enough space in
612193260Sjhb * 'first', EFBIG is returned.
613193260Sjhb */
614193260Sjhbint
615193260Sjhbsglist_join(struct sglist *first, struct sglist *second)
616193260Sjhb{
617193260Sjhb	struct sglist_seg *flast, *sfirst;
618193260Sjhb	int append;
619193260Sjhb
620193260Sjhb	/* If 'second' is empty, there is nothing to do. */
621193260Sjhb	if (second->sg_nseg == 0)
622193260Sjhb		return (0);
623193260Sjhb
624193260Sjhb	/*
625193260Sjhb	 * If the first entry in 'second' can be appended to the last entry
626193260Sjhb	 * in 'first' then set append to '1'.
627193260Sjhb	 */
628193260Sjhb	append = 0;
629193260Sjhb	flast = &first->sg_segs[first->sg_nseg - 1];
630193260Sjhb	sfirst = &second->sg_segs[0];
631193260Sjhb	if (first->sg_nseg != 0 &&
632193260Sjhb	    flast->ss_paddr + flast->ss_len == sfirst->ss_paddr)
633193260Sjhb		append = 1;
634193260Sjhb
635193260Sjhb	/* Make sure 'first' has enough room. */
636193260Sjhb	if (first->sg_nseg + second->sg_nseg - append > first->sg_maxseg)
637193260Sjhb		return (EFBIG);
638193260Sjhb
639193260Sjhb	/* Merge last in 'first' and first in 'second' if needed. */
640193260Sjhb	if (append)
641193260Sjhb		flast->ss_len += sfirst->ss_len;
642193260Sjhb
643193260Sjhb	/* Append new segments from 'second' to 'first'. */
644193260Sjhb	bcopy(first->sg_segs + first->sg_nseg, second->sg_segs + append,
645193260Sjhb	    (second->sg_nseg - append) * sizeof(struct sglist_seg));
646193260Sjhb	first->sg_nseg += second->sg_nseg - append;
647193260Sjhb	sglist_reset(second);
648193260Sjhb	return (0);
649193260Sjhb}
650193260Sjhb
651193260Sjhb/*
652193260Sjhb * Generate a new scatter/gather list from a range of an existing
653193260Sjhb * scatter/gather list.  The 'offset' and 'length' parameters specify
654193260Sjhb * the logical range of the 'original' list to extract.  If that range
655193260Sjhb * is not a subset of the length of 'original', then EINVAL is
656193260Sjhb * returned.  The new scatter/gather list is stored in '*slice'.
657193260Sjhb *
658193260Sjhb * If '*slice' is NULL, then a new list will be allocated using
659193260Sjhb * 'mflags'.  If M_NOWAIT is specified and the allocation fails,
660193260Sjhb * ENOMEM will be returned.
661193260Sjhb *
662193260Sjhb * If '*slice' is not NULL, it should point to an empty sglist.  If it
663193260Sjhb * does not have enough room for the remaining space, then EFBIG will
664193260Sjhb * be returned.  If '*slice' is not empty, then EINVAL will be
665193260Sjhb * returned.
666193260Sjhb */
667193260Sjhbint
668193260Sjhbsglist_slice(struct sglist *original, struct sglist **slice, size_t offset,
669193260Sjhb    size_t length, int mflags)
670193260Sjhb{
671193260Sjhb	struct sglist *sg;
672193260Sjhb	size_t space, end, foffs, loffs;
673193260Sjhb	int count, i, fseg;
674193260Sjhb
675193260Sjhb	/* Nothing to do. */
676193260Sjhb	if (length == 0)
677193260Sjhb		return (0);
678193260Sjhb
679193260Sjhb	/* Figure out how many segments '*slice' needs to have. */
680193260Sjhb	end = offset + length;
681193260Sjhb	space = 0;
682193260Sjhb	count = 0;
683193260Sjhb	fseg = 0;
684193260Sjhb	foffs = loffs = 0;
685193260Sjhb	for (i = 0; i < original->sg_nseg; i++) {
686193260Sjhb		space += original->sg_segs[i].ss_len;
687193260Sjhb		if (space > offset) {
688193260Sjhb			/*
689193260Sjhb			 * When we hit the first segment, store its index
690193260Sjhb			 * in 'fseg' and the offset into the first segment
691193260Sjhb			 * of 'offset' in 'foffs'.
692193260Sjhb			 */
693193260Sjhb			if (count == 0) {
694193260Sjhb				fseg = i;
695193260Sjhb				foffs = offset - (space -
696193260Sjhb				    original->sg_segs[i].ss_len);
697193260Sjhb				CTR1(KTR_DEV, "sglist_slice: foffs = %08lx",
698193260Sjhb				    foffs);
699193260Sjhb			}
700193260Sjhb			count++;
701193260Sjhb
702193260Sjhb			/*
703193260Sjhb			 * When we hit the last segment, break out of
704193260Sjhb			 * the loop.  Store the amount of extra space
705193260Sjhb			 * at the end of this segment in 'loffs'.
706193260Sjhb			 */
707193260Sjhb			if (space >= end) {
708193260Sjhb				loffs = space - end;
709193260Sjhb				CTR1(KTR_DEV, "sglist_slice: loffs = %08lx",
710193260Sjhb				    loffs);
711193260Sjhb				break;
712193260Sjhb			}
713193260Sjhb		}
714193260Sjhb	}
715193260Sjhb
716193260Sjhb	/* If we never hit 'end', then 'length' ran off the end, so fail. */
717193260Sjhb	if (space < end)
718193260Sjhb		return (EINVAL);
719193260Sjhb
720193260Sjhb	if (*slice == NULL) {
721193260Sjhb		sg = sglist_alloc(count, mflags);
722193260Sjhb		if (sg == NULL)
723193260Sjhb			return (ENOMEM);
724193260Sjhb		*slice = sg;
725193260Sjhb	} else {
726193260Sjhb		sg = *slice;
727193260Sjhb		if (sg->sg_maxseg < count)
728193260Sjhb			return (EFBIG);
729193260Sjhb		if (sg->sg_nseg != 0)
730193260Sjhb			return (EINVAL);
731193260Sjhb	}
732193260Sjhb
733193260Sjhb	/*
734193260Sjhb	 * Copy over 'count' segments from 'original' starting at
735193260Sjhb	 * 'fseg' to 'sg'.
736193260Sjhb	 */
737193260Sjhb	bcopy(original->sg_segs + fseg, sg->sg_segs,
738193260Sjhb	    count * sizeof(struct sglist_seg));
739193260Sjhb	sg->sg_nseg = count;
740193260Sjhb
741193260Sjhb	/* Fixup first and last segments if needed. */
742193260Sjhb	if (foffs != 0) {
743193260Sjhb		sg->sg_segs[0].ss_paddr += foffs;
744193260Sjhb		sg->sg_segs[0].ss_len -= foffs;
745193260Sjhb		CTR2(KTR_DEV, "sglist_slice seg[0]: %08lx:%08lx",
746193260Sjhb		    (long)sg->sg_segs[0].ss_paddr, sg->sg_segs[0].ss_len);
747193260Sjhb	}
748193260Sjhb	if (loffs != 0) {
749193260Sjhb		sg->sg_segs[count - 1].ss_len -= loffs;
750193260Sjhb		CTR2(KTR_DEV, "sglist_slice seg[%d]: len %08x", count - 1,
751193260Sjhb		    sg->sg_segs[count - 1].ss_len);
752193260Sjhb	}
753193260Sjhb	return (0);
754193260Sjhb}
755