2 * Copyright (c) 2008 Yahoo!, Inc.
4 * Written by: John Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the names of any co-contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/kernel.h>
37 #include <sys/malloc.h>
40 #include <sys/sglist.h>
44 #include <vm/vm_page.h>
46 #include <vm/vm_map.h>
50 static MALLOC_DEFINE(M_SGLIST, "sglist", "scatter/gather lists");
53 * Convenience macros to save the state of an sglist so it can be restored
54 * if an append attempt fails. Since sglist's only grow we only need to
55 * save the current count of segments and the length of the ending segment.
56 * Earlier segments will not be changed by an append, and the only change
57 * that can occur to the ending segment is that it can be extended.
64 #define SGLIST_SAVE(sg, sgsave) do { \
65 (sgsave).sg_nseg = (sg)->sg_nseg; \
66 if ((sgsave).sg_nseg > 0) \
67 (sgsave).ss_len = (sg)->sg_segs[(sgsave).sg_nseg - 1].ss_len; \
69 (sgsave).ss_len = 0; \
72 #define SGLIST_RESTORE(sg, sgsave) do { \
73 (sg)->sg_nseg = (sgsave).sg_nseg; \
74 if ((sgsave).sg_nseg > 0) \
75 (sg)->sg_segs[(sgsave).sg_nseg - 1].ss_len = (sgsave).ss_len; \
79 * Append a single (paddr, len) to a sglist. sg is the list and ss is
80 * the current segment in the list. If we run out of segments then
81 * EFBIG will be returned.
84 _sglist_append_range(struct sglist *sg, struct sglist_seg **ssp,
85 vm_paddr_t paddr, size_t len)
87 struct sglist_seg *ss;
90 if (ss->ss_paddr + ss->ss_len == paddr)
93 if (sg->sg_nseg == sg->sg_maxseg)
105 * Worker routine to append a virtual address range (either kernel or
106 * user) to a scatter/gather list.
109 _sglist_append_buf(struct sglist *sg, void *buf, size_t len, pmap_t pmap,
112 struct sglist_seg *ss;
113 vm_offset_t vaddr, offset;
123 /* Do the first page. It may have an offset. */
124 vaddr = (vm_offset_t)buf;
125 offset = vaddr & PAGE_MASK;
127 paddr = pmap_extract(pmap, vaddr);
129 paddr = pmap_kextract(vaddr);
130 seglen = MIN(len, PAGE_SIZE - offset);
131 if (sg->sg_nseg == 0) {
133 ss->ss_paddr = paddr;
137 ss = &sg->sg_segs[sg->sg_nseg - 1];
138 error = _sglist_append_range(sg, &ss, paddr, seglen);
148 seglen = MIN(len, PAGE_SIZE);
150 paddr = pmap_extract(pmap, vaddr);
152 paddr = pmap_kextract(vaddr);
153 error = _sglist_append_range(sg, &ss, paddr, seglen);
166 * Determine the number of scatter/gather list elements needed to
167 * describe a kernel virtual address range.
170 sglist_count(void *buf, size_t len)
172 vm_offset_t vaddr, vendaddr;
173 vm_paddr_t lastaddr, paddr;
179 vaddr = trunc_page((vm_offset_t)buf);
180 vendaddr = (vm_offset_t)buf + len;
182 lastaddr = pmap_kextract(vaddr);
184 while (vaddr < vendaddr) {
185 paddr = pmap_kextract(vaddr);
186 if (lastaddr + PAGE_SIZE != paddr)
195 * Allocate a scatter/gather list along with 'nsegs' segments. The
196 * 'mflags' parameters are the same as passed to malloc(9). The caller
197 * should use sglist_free() to free this list.
200 sglist_alloc(int nsegs, int mflags)
204 sg = malloc(sizeof(struct sglist) + nsegs * sizeof(struct sglist_seg),
208 sglist_init(sg, nsegs, (struct sglist_seg *)(sg + 1));
213 * Free a scatter/gather list allocated via sglist_allc().
216 sglist_free(struct sglist *sg)
222 if (refcount_release(&sg->sg_refs))
227 * Append the segments to describe a single kernel virtual address
228 * range to a scatter/gather list. If there are insufficient
229 * segments, then this fails with EFBIG.
232 sglist_append(struct sglist *sg, void *buf, size_t len)
237 if (sg->sg_maxseg == 0)
239 SGLIST_SAVE(sg, save);
240 error = _sglist_append_buf(sg, buf, len, NULL, NULL);
242 SGLIST_RESTORE(sg, save);
247 * Append the segments to describe a bio's data to a scatter/gather list.
248 * If there are insufficient segments, then this fails with EFBIG.
250 * NOTE: This function expects bio_bcount to be initialized.
253 sglist_append_bio(struct sglist *sg, struct bio *bp)
258 int error, i, ma_offs;
260 if ((bp->bio_flags & BIO_UNMAPPED) == 0) {
261 error = sglist_append(sg, bp->bio_data, bp->bio_bcount);
265 if (sg->sg_maxseg == 0)
268 SGLIST_SAVE(sg, save);
269 tlen = bp->bio_bcount;
270 ma_offs = bp->bio_ma_offset;
271 for (i = 0; tlen > 0; i++, tlen -= len) {
272 len = min(PAGE_SIZE - ma_offs, tlen);
273 paddr = VM_PAGE_TO_PHYS(bp->bio_ma[i]) + ma_offs;
274 error = sglist_append_phys(sg, paddr, len);
276 SGLIST_RESTORE(sg, save);
285 * Append a single physical address range to a scatter/gather list.
286 * If there are insufficient segments, then this fails with EFBIG.
289 sglist_append_phys(struct sglist *sg, vm_paddr_t paddr, size_t len)
291 struct sglist_seg *ss;
295 if (sg->sg_maxseg == 0)
300 if (sg->sg_nseg == 0) {
301 sg->sg_segs[0].ss_paddr = paddr;
302 sg->sg_segs[0].ss_len = len;
306 ss = &sg->sg_segs[sg->sg_nseg - 1];
307 SGLIST_SAVE(sg, save);
308 error = _sglist_append_range(sg, &ss, paddr, len);
310 SGLIST_RESTORE(sg, save);
315 * Append the segments that describe a single mbuf chain to a
316 * scatter/gather list. If there are insufficient segments, then this
320 sglist_append_mbuf(struct sglist *sg, struct mbuf *m0)
326 if (sg->sg_maxseg == 0)
330 SGLIST_SAVE(sg, save);
331 for (m = m0; m != NULL; m = m->m_next) {
333 error = sglist_append(sg, m->m_data, m->m_len);
335 SGLIST_RESTORE(sg, save);
344 * Append the segments that describe a single user address range to a
345 * scatter/gather list. If there are insufficient segments, then this
349 sglist_append_user(struct sglist *sg, void *buf, size_t len, struct thread *td)
354 if (sg->sg_maxseg == 0)
356 SGLIST_SAVE(sg, save);
357 error = _sglist_append_buf(sg, buf, len,
358 vmspace_pmap(td->td_proc->p_vmspace), NULL);
360 SGLIST_RESTORE(sg, save);
365 * Append the segments that describe a single uio to a scatter/gather
366 * list. If there are insufficient segments, then this fails with
370 sglist_append_uio(struct sglist *sg, struct uio *uio)
374 size_t resid, minlen;
378 if (sg->sg_maxseg == 0)
381 resid = uio->uio_resid;
384 if (uio->uio_segflg == UIO_USERSPACE) {
385 KASSERT(uio->uio_td != NULL,
386 ("sglist_append_uio: USERSPACE but no thread"));
387 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
392 SGLIST_SAVE(sg, save);
393 for (i = 0; i < uio->uio_iovcnt && resid != 0; i++) {
395 * Now at the first iovec to load. Load each iovec
396 * until we have exhausted the residual count.
398 minlen = MIN(resid, iov[i].iov_len);
400 error = _sglist_append_buf(sg, iov[i].iov_base, minlen,
403 SGLIST_RESTORE(sg, save);
413 * Append the segments that describe at most 'resid' bytes from a
414 * single uio to a scatter/gather list. If there are insufficient
415 * segments, then only the amount that fits is appended.
418 sglist_consume_uio(struct sglist *sg, struct uio *uio, size_t resid)
425 if (sg->sg_maxseg == 0)
428 if (uio->uio_segflg == UIO_USERSPACE) {
429 KASSERT(uio->uio_td != NULL,
430 ("sglist_consume_uio: USERSPACE but no thread"));
431 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
436 while (resid > 0 && uio->uio_resid) {
448 * Try to append this iovec. If we run out of room,
449 * then break out of the loop.
451 error = _sglist_append_buf(sg, iov->iov_base, len, pmap, &done);
452 iov->iov_base = (char *)iov->iov_base + done;
453 iov->iov_len -= done;
454 uio->uio_resid -= done;
455 uio->uio_offset += done;
464 * Allocate and populate a scatter/gather list to describe a single
465 * kernel virtual address range.
468 sglist_build(void *buf, size_t len, int mflags)
476 nsegs = sglist_count(buf, len);
477 sg = sglist_alloc(nsegs, mflags);
480 if (sglist_append(sg, buf, len) != 0) {
488 * Clone a new copy of a scatter/gather list.
491 sglist_clone(struct sglist *sg, int mflags)
497 new = sglist_alloc(sg->sg_maxseg, mflags);
500 new->sg_nseg = sg->sg_nseg;
501 bcopy(sg->sg_segs, new->sg_segs, sizeof(struct sglist_seg) *
507 * Calculate the total length of the segments described in a
508 * scatter/gather list.
511 sglist_length(struct sglist *sg)
517 for (i = 0; i < sg->sg_nseg; i++)
518 space += sg->sg_segs[i].ss_len;
523 * Split a scatter/gather list into two lists. The scatter/gather
524 * entries for the first 'length' bytes of the 'original' list are
525 * stored in the '*head' list and are removed from 'original'.
527 * If '*head' is NULL, then a new list will be allocated using
528 * 'mflags'. If M_NOWAIT is specified and the allocation fails,
529 * ENOMEM will be returned.
531 * If '*head' is not NULL, it should point to an empty sglist. If it
532 * does not have enough room for the remaining space, then EFBIG will
533 * be returned. If '*head' is not empty, then EINVAL will be
536 * If 'original' is shared (refcount > 1), then EDOOFUS will be
540 sglist_split(struct sglist *original, struct sglist **head, size_t length,
547 if (original->sg_refs > 1)
550 /* Figure out how big of a sglist '*head' has to hold. */
554 for (i = 0; i < original->sg_nseg; i++) {
555 space += original->sg_segs[i].ss_len;
557 if (space >= length) {
559 * If 'length' falls in the middle of a
560 * scatter/gather list entry, then 'split'
561 * holds how much of that entry will remain in
564 split = space - length;
569 /* Nothing to do, so leave head empty. */
574 sg = sglist_alloc(count, mflags);
580 if (sg->sg_maxseg < count)
582 if (sg->sg_nseg != 0)
586 /* Copy 'count' entries to 'sg' from 'original'. */
587 bcopy(original->sg_segs, sg->sg_segs, count *
588 sizeof(struct sglist_seg));
592 * If we had to split a list entry, fixup the last entry in
593 * 'sg' and the new first entry in 'original'. We also
594 * decrement 'count' by 1 since we will only be removing
595 * 'count - 1' segments from 'original' now.
599 sg->sg_segs[count].ss_len -= split;
600 original->sg_segs[count].ss_paddr =
601 sg->sg_segs[count].ss_paddr + split;
602 original->sg_segs[count].ss_len = split;
605 /* Trim 'count' entries from the front of 'original'. */
606 original->sg_nseg -= count;
607 bcopy(original->sg_segs + count, original->sg_segs, count *
608 sizeof(struct sglist_seg));
613 * Append the scatter/gather list elements in 'second' to the
614 * scatter/gather list 'first'. If there is not enough space in
615 * 'first', EFBIG is returned.
618 sglist_join(struct sglist *first, struct sglist *second)
620 struct sglist_seg *flast, *sfirst;
623 /* If 'second' is empty, there is nothing to do. */
624 if (second->sg_nseg == 0)
628 * If the first entry in 'second' can be appended to the last entry
629 * in 'first' then set append to '1'.
632 flast = &first->sg_segs[first->sg_nseg - 1];
633 sfirst = &second->sg_segs[0];
634 if (first->sg_nseg != 0 &&
635 flast->ss_paddr + flast->ss_len == sfirst->ss_paddr)
638 /* Make sure 'first' has enough room. */
639 if (first->sg_nseg + second->sg_nseg - append > first->sg_maxseg)
642 /* Merge last in 'first' and first in 'second' if needed. */
644 flast->ss_len += sfirst->ss_len;
646 /* Append new segments from 'second' to 'first'. */
647 bcopy(first->sg_segs + first->sg_nseg, second->sg_segs + append,
648 (second->sg_nseg - append) * sizeof(struct sglist_seg));
649 first->sg_nseg += second->sg_nseg - append;
650 sglist_reset(second);
655 * Generate a new scatter/gather list from a range of an existing
656 * scatter/gather list. The 'offset' and 'length' parameters specify
657 * the logical range of the 'original' list to extract. If that range
658 * is not a subset of the length of 'original', then EINVAL is
659 * returned. The new scatter/gather list is stored in '*slice'.
661 * If '*slice' is NULL, then a new list will be allocated using
662 * 'mflags'. If M_NOWAIT is specified and the allocation fails,
663 * ENOMEM will be returned.
665 * If '*slice' is not NULL, it should point to an empty sglist. If it
666 * does not have enough room for the remaining space, then EFBIG will
667 * be returned. If '*slice' is not empty, then EINVAL will be
671 sglist_slice(struct sglist *original, struct sglist **slice, size_t offset,
672 size_t length, int mflags)
675 size_t space, end, foffs, loffs;
682 /* Figure out how many segments '*slice' needs to have. */
683 end = offset + length;
688 for (i = 0; i < original->sg_nseg; i++) {
689 space += original->sg_segs[i].ss_len;
690 if (space > offset) {
692 * When we hit the first segment, store its index
693 * in 'fseg' and the offset into the first segment
694 * of 'offset' in 'foffs'.
698 foffs = offset - (space -
699 original->sg_segs[i].ss_len);
700 CTR1(KTR_DEV, "sglist_slice: foffs = %08lx",
706 * When we hit the last segment, break out of
707 * the loop. Store the amount of extra space
708 * at the end of this segment in 'loffs'.
712 CTR1(KTR_DEV, "sglist_slice: loffs = %08lx",
719 /* If we never hit 'end', then 'length' ran off the end, so fail. */
723 if (*slice == NULL) {
724 sg = sglist_alloc(count, mflags);
730 if (sg->sg_maxseg < count)
732 if (sg->sg_nseg != 0)
737 * Copy over 'count' segments from 'original' starting at
740 bcopy(original->sg_segs + fseg, sg->sg_segs,
741 count * sizeof(struct sglist_seg));
744 /* Fixup first and last segments if needed. */
746 sg->sg_segs[0].ss_paddr += foffs;
747 sg->sg_segs[0].ss_len -= foffs;
748 CTR2(KTR_DEV, "sglist_slice seg[0]: %08lx:%08lx",
749 (long)sg->sg_segs[0].ss_paddr, sg->sg_segs[0].ss_len);
752 sg->sg_segs[count - 1].ss_len -= loffs;
753 CTR2(KTR_DEV, "sglist_slice seg[%d]: len %08x", count - 1,
754 sg->sg_segs[count - 1].ss_len);