subr_sglist.c revision 277759
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: head/sys/kern/subr_sglist.c 277759 2015-01-26 16:26:28Z jhb $");
33193260Sjhb
34193260Sjhb#include <sys/param.h>
35193260Sjhb#include <sys/kernel.h>
36260581Sbryanv#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>
44260581Sbryanv#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
219277759Sjhb	if (sg == NULL)
220277759Sjhb		return;
221277759Sjhb
222193260Sjhb	if (refcount_release(&sg->sg_refs))
223193260Sjhb		free(sg, M_SGLIST);
224193260Sjhb}
225193260Sjhb
226193260Sjhb/*
227193260Sjhb * Append the segments to describe a single kernel virtual address
228193260Sjhb * range to a scatter/gather list.  If there are insufficient
229193260Sjhb * segments, then this fails with EFBIG.
230193260Sjhb */
231193260Sjhbint
232193260Sjhbsglist_append(struct sglist *sg, void *buf, size_t len)
233193260Sjhb{
234196417Sjhb	struct sgsave save;
235196417Sjhb	int error;
236193260Sjhb
237193260Sjhb	if (sg->sg_maxseg == 0)
238193260Sjhb		return (EINVAL);
239196417Sjhb	SGLIST_SAVE(sg, save);
240196417Sjhb	error = _sglist_append_buf(sg, buf, len, NULL, NULL);
241196417Sjhb	if (error)
242196417Sjhb		SGLIST_RESTORE(sg, save);
243196417Sjhb	return (error);
244193260Sjhb}
245193260Sjhb
246193260Sjhb/*
247260581Sbryanv * Append the segments to describe a bio's data to a scatter/gather list.
248260581Sbryanv * If there are insufficient segments, then this fails with EFBIG.
249260581Sbryanv *
250260581Sbryanv * NOTE: This function expects bio_bcount to be initialized.
251260581Sbryanv */
252260581Sbryanvint
253260581Sbryanvsglist_append_bio(struct sglist *sg, struct bio *bp)
254260581Sbryanv{
255260581Sbryanv	struct sgsave save;
256260581Sbryanv	vm_paddr_t paddr;
257260581Sbryanv	size_t len, tlen;
258260581Sbryanv	int error, i, ma_offs;
259260581Sbryanv
260260581Sbryanv	if ((bp->bio_flags & BIO_UNMAPPED) == 0) {
261260581Sbryanv		error = sglist_append(sg, bp->bio_data, bp->bio_bcount);
262260581Sbryanv		return (error);
263260581Sbryanv	}
264260581Sbryanv
265260581Sbryanv	if (sg->sg_maxseg == 0)
266260581Sbryanv		return (EINVAL);
267260581Sbryanv
268260581Sbryanv	SGLIST_SAVE(sg, save);
269260581Sbryanv	tlen = bp->bio_bcount;
270260581Sbryanv	ma_offs = bp->bio_ma_offset;
271260581Sbryanv	for (i = 0; tlen > 0; i++, tlen -= len) {
272260581Sbryanv		len = min(PAGE_SIZE - ma_offs, tlen);
273260581Sbryanv		paddr = VM_PAGE_TO_PHYS(bp->bio_ma[i]) + ma_offs;
274260581Sbryanv		error = sglist_append_phys(sg, paddr, len);
275260581Sbryanv		if (error) {
276260581Sbryanv			SGLIST_RESTORE(sg, save);
277260581Sbryanv			return (error);
278260581Sbryanv		}
279260581Sbryanv		ma_offs = 0;
280260581Sbryanv	}
281260581Sbryanv	return (0);
282260581Sbryanv}
283260581Sbryanv
284260581Sbryanv/*
285193260Sjhb * Append a single physical address range to a scatter/gather list.
286193260Sjhb * If there are insufficient segments, then this fails with EFBIG.
287193260Sjhb */
288193260Sjhbint
289193260Sjhbsglist_append_phys(struct sglist *sg, vm_paddr_t paddr, size_t len)
290193260Sjhb{
291193260Sjhb	struct sglist_seg *ss;
292196417Sjhb	struct sgsave save;
293196417Sjhb	int error;
294193260Sjhb
295193260Sjhb	if (sg->sg_maxseg == 0)
296193260Sjhb		return (EINVAL);
297193260Sjhb	if (len == 0)
298193260Sjhb		return (0);
299193260Sjhb
300193260Sjhb	if (sg->sg_nseg == 0) {
301193260Sjhb		sg->sg_segs[0].ss_paddr = paddr;
302193260Sjhb		sg->sg_segs[0].ss_len = len;
303193260Sjhb		sg->sg_nseg = 1;
304193260Sjhb		return (0);
305193260Sjhb	}
306193260Sjhb	ss = &sg->sg_segs[sg->sg_nseg - 1];
307196417Sjhb	SGLIST_SAVE(sg, save);
308196417Sjhb	error = _sglist_append_range(sg, &ss, paddr, len);
309196417Sjhb	if (error)
310196417Sjhb		SGLIST_RESTORE(sg, save);
311196417Sjhb	return (error);
312193260Sjhb}
313193260Sjhb
314193260Sjhb/*
315193260Sjhb * Append the segments that describe a single mbuf chain to a
316193260Sjhb * scatter/gather list.  If there are insufficient segments, then this
317193260Sjhb * fails with EFBIG.
318193260Sjhb */
319193260Sjhbint
320193260Sjhbsglist_append_mbuf(struct sglist *sg, struct mbuf *m0)
321193260Sjhb{
322196417Sjhb	struct sgsave save;
323193260Sjhb	struct mbuf *m;
324193260Sjhb	int error;
325193260Sjhb
326193260Sjhb	if (sg->sg_maxseg == 0)
327193260Sjhb		return (EINVAL);
328193260Sjhb
329193260Sjhb	error = 0;
330196417Sjhb	SGLIST_SAVE(sg, save);
331193260Sjhb	for (m = m0; m != NULL; m = m->m_next) {
332193260Sjhb		if (m->m_len > 0) {
333193260Sjhb			error = sglist_append(sg, m->m_data, m->m_len);
334196417Sjhb			if (error) {
335196417Sjhb				SGLIST_RESTORE(sg, save);
336193260Sjhb				return (error);
337196417Sjhb			}
338193260Sjhb		}
339193260Sjhb	}
340193260Sjhb	return (0);
341193260Sjhb}
342193260Sjhb
343193260Sjhb/*
344193260Sjhb * Append the segments that describe a single user address range to a
345193260Sjhb * scatter/gather list.  If there are insufficient segments, then this
346193260Sjhb * fails with EFBIG.
347193260Sjhb */
348193260Sjhbint
349193260Sjhbsglist_append_user(struct sglist *sg, void *buf, size_t len, struct thread *td)
350193260Sjhb{
351196417Sjhb	struct sgsave save;
352196417Sjhb	int error;
353193260Sjhb
354193260Sjhb	if (sg->sg_maxseg == 0)
355193260Sjhb		return (EINVAL);
356196417Sjhb	SGLIST_SAVE(sg, save);
357196417Sjhb	error = _sglist_append_buf(sg, buf, len,
358196417Sjhb	    vmspace_pmap(td->td_proc->p_vmspace), NULL);
359196417Sjhb	if (error)
360196417Sjhb		SGLIST_RESTORE(sg, save);
361196417Sjhb	return (error);
362193260Sjhb}
363193260Sjhb
364193260Sjhb/*
365193260Sjhb * Append the segments that describe a single uio to a scatter/gather
366193260Sjhb * list.  If there are insufficient segments, then this fails with
367193260Sjhb * EFBIG.
368193260Sjhb */
369193260Sjhbint
370193260Sjhbsglist_append_uio(struct sglist *sg, struct uio *uio)
371193260Sjhb{
372193260Sjhb	struct iovec *iov;
373196417Sjhb	struct sgsave save;
374193260Sjhb	size_t resid, minlen;
375193260Sjhb	pmap_t pmap;
376193260Sjhb	int error, i;
377193260Sjhb
378193260Sjhb	if (sg->sg_maxseg == 0)
379193260Sjhb		return (EINVAL);
380193260Sjhb
381193260Sjhb	resid = uio->uio_resid;
382193260Sjhb	iov = uio->uio_iov;
383193260Sjhb
384193260Sjhb	if (uio->uio_segflg == UIO_USERSPACE) {
385193260Sjhb		KASSERT(uio->uio_td != NULL,
386193260Sjhb		    ("sglist_append_uio: USERSPACE but no thread"));
387193260Sjhb		pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
388193260Sjhb	} else
389193260Sjhb		pmap = NULL;
390193260Sjhb
391193260Sjhb	error = 0;
392196417Sjhb	SGLIST_SAVE(sg, save);
393193260Sjhb	for (i = 0; i < uio->uio_iovcnt && resid != 0; i++) {
394193260Sjhb		/*
395193260Sjhb		 * Now at the first iovec to load.  Load each iovec
396193260Sjhb		 * until we have exhausted the residual count.
397193260Sjhb		 */
398193260Sjhb		minlen = MIN(resid, iov[i].iov_len);
399193260Sjhb		if (minlen > 0) {
400193260Sjhb			error = _sglist_append_buf(sg, iov[i].iov_base, minlen,
401193260Sjhb			    pmap, NULL);
402196417Sjhb			if (error) {
403196417Sjhb				SGLIST_RESTORE(sg, save);
404193260Sjhb				return (error);
405196417Sjhb			}
406193260Sjhb			resid -= minlen;
407193260Sjhb		}
408193260Sjhb	}
409193260Sjhb	return (0);
410193260Sjhb}
411193260Sjhb
412193260Sjhb/*
413193260Sjhb * Append the segments that describe at most 'resid' bytes from a
414193260Sjhb * single uio to a scatter/gather list.  If there are insufficient
415193260Sjhb * segments, then only the amount that fits is appended.
416193260Sjhb */
417193260Sjhbint
418196404Sjhbsglist_consume_uio(struct sglist *sg, struct uio *uio, size_t resid)
419193260Sjhb{
420193260Sjhb	struct iovec *iov;
421193260Sjhb	size_t done;
422193260Sjhb	pmap_t pmap;
423193260Sjhb	int error, len;
424193260Sjhb
425193260Sjhb	if (sg->sg_maxseg == 0)
426193260Sjhb		return (EINVAL);
427193260Sjhb
428193260Sjhb	if (uio->uio_segflg == UIO_USERSPACE) {
429193260Sjhb		KASSERT(uio->uio_td != NULL,
430193260Sjhb		    ("sglist_consume_uio: USERSPACE but no thread"));
431193260Sjhb		pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
432193260Sjhb	} else
433193260Sjhb		pmap = NULL;
434193260Sjhb
435193260Sjhb	error = 0;
436193260Sjhb	while (resid > 0 && uio->uio_resid) {
437193260Sjhb		iov = uio->uio_iov;
438193260Sjhb		len = iov->iov_len;
439193260Sjhb		if (len == 0) {
440193260Sjhb			uio->uio_iov++;
441193260Sjhb			uio->uio_iovcnt--;
442193260Sjhb			continue;
443193260Sjhb		}
444193260Sjhb		if (len > resid)
445193260Sjhb			len = resid;
446193260Sjhb
447193260Sjhb		/*
448193260Sjhb		 * Try to append this iovec.  If we run out of room,
449193260Sjhb		 * then break out of the loop.
450193260Sjhb		 */
451193260Sjhb		error = _sglist_append_buf(sg, iov->iov_base, len, pmap, &done);
452193260Sjhb		iov->iov_base = (char *)iov->iov_base + done;
453193260Sjhb		iov->iov_len -= done;
454193260Sjhb		uio->uio_resid -= done;
455193260Sjhb		uio->uio_offset += done;
456193260Sjhb		resid -= done;
457193260Sjhb		if (error)
458193260Sjhb			break;
459193260Sjhb	}
460193260Sjhb	return (0);
461193260Sjhb}
462193260Sjhb
463193260Sjhb/*
464193260Sjhb * Allocate and populate a scatter/gather list to describe a single
465193260Sjhb * kernel virtual address range.
466193260Sjhb */
467193260Sjhbstruct sglist *
468193260Sjhbsglist_build(void *buf, size_t len, int mflags)
469193260Sjhb{
470193260Sjhb	struct sglist *sg;
471193260Sjhb	int nsegs;
472193260Sjhb
473193260Sjhb	if (len == 0)
474193260Sjhb		return (NULL);
475193260Sjhb
476193260Sjhb	nsegs = sglist_count(buf, len);
477193260Sjhb	sg = sglist_alloc(nsegs, mflags);
478193260Sjhb	if (sg == NULL)
479193260Sjhb		return (NULL);
480193260Sjhb	if (sglist_append(sg, buf, len) != 0) {
481193260Sjhb		sglist_free(sg);
482193260Sjhb		return (NULL);
483193260Sjhb	}
484193260Sjhb	return (sg);
485193260Sjhb}
486193260Sjhb
487193260Sjhb/*
488193260Sjhb * Clone a new copy of a scatter/gather list.
489193260Sjhb */
490193260Sjhbstruct sglist *
491193260Sjhbsglist_clone(struct sglist *sg, int mflags)
492193260Sjhb{
493193260Sjhb	struct sglist *new;
494193260Sjhb
495193260Sjhb	if (sg == NULL)
496193260Sjhb		return (NULL);
497193260Sjhb	new = sglist_alloc(sg->sg_maxseg, mflags);
498193260Sjhb	if (new == NULL)
499193260Sjhb		return (NULL);
500196417Sjhb	new->sg_nseg = sg->sg_nseg;
501193260Sjhb	bcopy(sg->sg_segs, new->sg_segs, sizeof(struct sglist_seg) *
502193260Sjhb	    sg->sg_nseg);
503193260Sjhb	return (new);
504193260Sjhb}
505193260Sjhb
506193260Sjhb/*
507193260Sjhb * Calculate the total length of the segments described in a
508193260Sjhb * scatter/gather list.
509193260Sjhb */
510193260Sjhbsize_t
511193260Sjhbsglist_length(struct sglist *sg)
512193260Sjhb{
513193260Sjhb	size_t space;
514193260Sjhb	int i;
515193260Sjhb
516193260Sjhb	space = 0;
517193260Sjhb	for (i = 0; i < sg->sg_nseg; i++)
518193260Sjhb		space += sg->sg_segs[i].ss_len;
519193260Sjhb	return (space);
520193260Sjhb}
521193260Sjhb
522193260Sjhb/*
523193260Sjhb * Split a scatter/gather list into two lists.  The scatter/gather
524193260Sjhb * entries for the first 'length' bytes of the 'original' list are
525193260Sjhb * stored in the '*head' list and are removed from 'original'.
526193260Sjhb *
527193260Sjhb * If '*head' is NULL, then a new list will be allocated using
528193260Sjhb * 'mflags'.  If M_NOWAIT is specified and the allocation fails,
529193260Sjhb * ENOMEM will be returned.
530193260Sjhb *
531193260Sjhb * If '*head' is not NULL, it should point to an empty sglist.  If it
532193260Sjhb * does not have enough room for the remaining space, then EFBIG will
533193260Sjhb * be returned.  If '*head' is not empty, then EINVAL will be
534193260Sjhb * returned.
535193260Sjhb *
536193260Sjhb * If 'original' is shared (refcount > 1), then EDOOFUS will be
537193260Sjhb * returned.
538193260Sjhb */
539193260Sjhbint
540193260Sjhbsglist_split(struct sglist *original, struct sglist **head, size_t length,
541193260Sjhb    int mflags)
542193260Sjhb{
543193260Sjhb	struct sglist *sg;
544193260Sjhb	size_t space, split;
545193260Sjhb	int count, i;
546193260Sjhb
547193260Sjhb	if (original->sg_refs > 1)
548193260Sjhb		return (EDOOFUS);
549193260Sjhb
550193260Sjhb	/* Figure out how big of a sglist '*head' has to hold. */
551193260Sjhb	count = 0;
552193260Sjhb	space = 0;
553193260Sjhb	split = 0;
554193260Sjhb	for (i = 0; i < original->sg_nseg; i++) {
555193260Sjhb		space += original->sg_segs[i].ss_len;
556193260Sjhb		count++;
557193260Sjhb		if (space >= length) {
558193260Sjhb			/*
559193260Sjhb			 * If 'length' falls in the middle of a
560193260Sjhb			 * scatter/gather list entry, then 'split'
561193260Sjhb			 * holds how much of that entry will remain in
562193260Sjhb			 * 'original'.
563193260Sjhb			 */
564193260Sjhb			split = space - length;
565193260Sjhb			break;
566193260Sjhb		}
567193260Sjhb	}
568193260Sjhb
569193260Sjhb	/* Nothing to do, so leave head empty. */
570193260Sjhb	if (count == 0)
571193260Sjhb		return (0);
572193260Sjhb
573193260Sjhb	if (*head == NULL) {
574193260Sjhb		sg = sglist_alloc(count, mflags);
575193260Sjhb		if (sg == NULL)
576193260Sjhb			return (ENOMEM);
577193260Sjhb		*head = sg;
578193260Sjhb	} else {
579193260Sjhb		sg = *head;
580193260Sjhb		if (sg->sg_maxseg < count)
581193260Sjhb			return (EFBIG);
582193260Sjhb		if (sg->sg_nseg != 0)
583193260Sjhb			return (EINVAL);
584193260Sjhb	}
585193260Sjhb
586193260Sjhb	/* Copy 'count' entries to 'sg' from 'original'. */
587193260Sjhb	bcopy(original->sg_segs, sg->sg_segs, count *
588193260Sjhb	    sizeof(struct sglist_seg));
589193260Sjhb	sg->sg_nseg = count;
590193260Sjhb
591193260Sjhb	/*
592193260Sjhb	 * If we had to split a list entry, fixup the last entry in
593193260Sjhb	 * 'sg' and the new first entry in 'original'.  We also
594193260Sjhb	 * decrement 'count' by 1 since we will only be removing
595193260Sjhb	 * 'count - 1' segments from 'original' now.
596193260Sjhb	 */
597193260Sjhb	if (split != 0) {
598193260Sjhb		count--;
599193260Sjhb		sg->sg_segs[count].ss_len -= split;
600193260Sjhb		original->sg_segs[count].ss_paddr =
601193260Sjhb		    sg->sg_segs[count].ss_paddr + split;
602193260Sjhb		original->sg_segs[count].ss_len = split;
603193260Sjhb	}
604193260Sjhb
605193260Sjhb	/* Trim 'count' entries from the front of 'original'. */
606193260Sjhb	original->sg_nseg -= count;
607193260Sjhb	bcopy(original->sg_segs + count, original->sg_segs, count *
608193260Sjhb	    sizeof(struct sglist_seg));
609193260Sjhb	return (0);
610193260Sjhb}
611193260Sjhb
612193260Sjhb/*
613193260Sjhb * Append the scatter/gather list elements in 'second' to the
614193260Sjhb * scatter/gather list 'first'.  If there is not enough space in
615193260Sjhb * 'first', EFBIG is returned.
616193260Sjhb */
617193260Sjhbint
618193260Sjhbsglist_join(struct sglist *first, struct sglist *second)
619193260Sjhb{
620193260Sjhb	struct sglist_seg *flast, *sfirst;
621193260Sjhb	int append;
622193260Sjhb
623193260Sjhb	/* If 'second' is empty, there is nothing to do. */
624193260Sjhb	if (second->sg_nseg == 0)
625193260Sjhb		return (0);
626193260Sjhb
627193260Sjhb	/*
628193260Sjhb	 * If the first entry in 'second' can be appended to the last entry
629193260Sjhb	 * in 'first' then set append to '1'.
630193260Sjhb	 */
631193260Sjhb	append = 0;
632193260Sjhb	flast = &first->sg_segs[first->sg_nseg - 1];
633193260Sjhb	sfirst = &second->sg_segs[0];
634193260Sjhb	if (first->sg_nseg != 0 &&
635193260Sjhb	    flast->ss_paddr + flast->ss_len == sfirst->ss_paddr)
636193260Sjhb		append = 1;
637193260Sjhb
638193260Sjhb	/* Make sure 'first' has enough room. */
639193260Sjhb	if (first->sg_nseg + second->sg_nseg - append > first->sg_maxseg)
640193260Sjhb		return (EFBIG);
641193260Sjhb
642193260Sjhb	/* Merge last in 'first' and first in 'second' if needed. */
643193260Sjhb	if (append)
644193260Sjhb		flast->ss_len += sfirst->ss_len;
645193260Sjhb
646193260Sjhb	/* Append new segments from 'second' to 'first'. */
647193260Sjhb	bcopy(first->sg_segs + first->sg_nseg, second->sg_segs + append,
648193260Sjhb	    (second->sg_nseg - append) * sizeof(struct sglist_seg));
649193260Sjhb	first->sg_nseg += second->sg_nseg - append;
650193260Sjhb	sglist_reset(second);
651193260Sjhb	return (0);
652193260Sjhb}
653193260Sjhb
654193260Sjhb/*
655193260Sjhb * Generate a new scatter/gather list from a range of an existing
656193260Sjhb * scatter/gather list.  The 'offset' and 'length' parameters specify
657193260Sjhb * the logical range of the 'original' list to extract.  If that range
658193260Sjhb * is not a subset of the length of 'original', then EINVAL is
659193260Sjhb * returned.  The new scatter/gather list is stored in '*slice'.
660193260Sjhb *
661193260Sjhb * If '*slice' is NULL, then a new list will be allocated using
662193260Sjhb * 'mflags'.  If M_NOWAIT is specified and the allocation fails,
663193260Sjhb * ENOMEM will be returned.
664193260Sjhb *
665193260Sjhb * If '*slice' is not NULL, it should point to an empty sglist.  If it
666193260Sjhb * does not have enough room for the remaining space, then EFBIG will
667193260Sjhb * be returned.  If '*slice' is not empty, then EINVAL will be
668193260Sjhb * returned.
669193260Sjhb */
670193260Sjhbint
671193260Sjhbsglist_slice(struct sglist *original, struct sglist **slice, size_t offset,
672193260Sjhb    size_t length, int mflags)
673193260Sjhb{
674193260Sjhb	struct sglist *sg;
675193260Sjhb	size_t space, end, foffs, loffs;
676193260Sjhb	int count, i, fseg;
677193260Sjhb
678193260Sjhb	/* Nothing to do. */
679193260Sjhb	if (length == 0)
680193260Sjhb		return (0);
681193260Sjhb
682193260Sjhb	/* Figure out how many segments '*slice' needs to have. */
683193260Sjhb	end = offset + length;
684193260Sjhb	space = 0;
685193260Sjhb	count = 0;
686193260Sjhb	fseg = 0;
687193260Sjhb	foffs = loffs = 0;
688193260Sjhb	for (i = 0; i < original->sg_nseg; i++) {
689193260Sjhb		space += original->sg_segs[i].ss_len;
690193260Sjhb		if (space > offset) {
691193260Sjhb			/*
692193260Sjhb			 * When we hit the first segment, store its index
693193260Sjhb			 * in 'fseg' and the offset into the first segment
694193260Sjhb			 * of 'offset' in 'foffs'.
695193260Sjhb			 */
696193260Sjhb			if (count == 0) {
697193260Sjhb				fseg = i;
698193260Sjhb				foffs = offset - (space -
699193260Sjhb				    original->sg_segs[i].ss_len);
700193260Sjhb				CTR1(KTR_DEV, "sglist_slice: foffs = %08lx",
701193260Sjhb				    foffs);
702193260Sjhb			}
703193260Sjhb			count++;
704193260Sjhb
705193260Sjhb			/*
706193260Sjhb			 * When we hit the last segment, break out of
707193260Sjhb			 * the loop.  Store the amount of extra space
708193260Sjhb			 * at the end of this segment in 'loffs'.
709193260Sjhb			 */
710193260Sjhb			if (space >= end) {
711193260Sjhb				loffs = space - end;
712193260Sjhb				CTR1(KTR_DEV, "sglist_slice: loffs = %08lx",
713193260Sjhb				    loffs);
714193260Sjhb				break;
715193260Sjhb			}
716193260Sjhb		}
717193260Sjhb	}
718193260Sjhb
719193260Sjhb	/* If we never hit 'end', then 'length' ran off the end, so fail. */
720193260Sjhb	if (space < end)
721193260Sjhb		return (EINVAL);
722193260Sjhb
723193260Sjhb	if (*slice == NULL) {
724193260Sjhb		sg = sglist_alloc(count, mflags);
725193260Sjhb		if (sg == NULL)
726193260Sjhb			return (ENOMEM);
727193260Sjhb		*slice = sg;
728193260Sjhb	} else {
729193260Sjhb		sg = *slice;
730193260Sjhb		if (sg->sg_maxseg < count)
731193260Sjhb			return (EFBIG);
732193260Sjhb		if (sg->sg_nseg != 0)
733193260Sjhb			return (EINVAL);
734193260Sjhb	}
735193260Sjhb
736193260Sjhb	/*
737193260Sjhb	 * Copy over 'count' segments from 'original' starting at
738193260Sjhb	 * 'fseg' to 'sg'.
739193260Sjhb	 */
740193260Sjhb	bcopy(original->sg_segs + fseg, sg->sg_segs,
741193260Sjhb	    count * sizeof(struct sglist_seg));
742193260Sjhb	sg->sg_nseg = count;
743193260Sjhb
744193260Sjhb	/* Fixup first and last segments if needed. */
745193260Sjhb	if (foffs != 0) {
746193260Sjhb		sg->sg_segs[0].ss_paddr += foffs;
747193260Sjhb		sg->sg_segs[0].ss_len -= foffs;
748193260Sjhb		CTR2(KTR_DEV, "sglist_slice seg[0]: %08lx:%08lx",
749193260Sjhb		    (long)sg->sg_segs[0].ss_paddr, sg->sg_segs[0].ss_len);
750193260Sjhb	}
751193260Sjhb	if (loffs != 0) {
752193260Sjhb		sg->sg_segs[count - 1].ss_len -= loffs;
753193260Sjhb		CTR2(KTR_DEV, "sglist_slice seg[%d]: len %08x", count - 1,
754193260Sjhb		    sg->sg_segs[count - 1].ss_len);
755193260Sjhb	}
756193260Sjhb	return (0);
757193260Sjhb}
758