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>
36 #include <sys/malloc.h>
39 #include <sys/sglist.h>
44 #include <vm/vm_map.h>
48 static MALLOC_DEFINE(M_SGLIST, "sglist", "scatter/gather lists");
51 * Append a single (paddr, len) to a sglist. sg is the list and ss is
52 * the current segment in the list. If we run out of segments then
53 * EFBIG will be returned.
56 _sglist_append_range(struct sglist *sg, struct sglist_seg **ssp,
57 vm_paddr_t paddr, size_t len)
59 struct sglist_seg *ss;
62 if (ss->ss_paddr + ss->ss_len == paddr)
65 if (sg->sg_nseg == sg->sg_maxseg) {
79 * Worker routine to append a virtual address range (either kernel or
80 * user) to a scatter/gather list.
83 _sglist_append_buf(struct sglist *sg, void *buf, size_t len, pmap_t pmap,
86 struct sglist_seg *ss;
87 vm_offset_t vaddr, offset;
97 /* Do the first page. It may have an offset. */
98 vaddr = (vm_offset_t)buf;
99 offset = vaddr & PAGE_MASK;
101 paddr = pmap_extract(pmap, vaddr);
103 paddr = pmap_kextract(vaddr);
104 seglen = MIN(len, PAGE_SIZE - offset);
105 if (sg->sg_nseg == 0) {
107 ss->ss_paddr = paddr;
112 ss = &sg->sg_segs[sg->sg_nseg - 1];
113 error = _sglist_append_range(sg, &ss, paddr, seglen);
116 while (error == 0 && len > seglen) {
121 seglen = MIN(len, PAGE_SIZE);
123 paddr = pmap_extract(pmap, vaddr);
125 paddr = pmap_kextract(vaddr);
126 error = _sglist_append_range(sg, &ss, paddr, seglen);
133 * Determine the number of scatter/gather list elements needed to
134 * describe a kernel virtual address range.
137 sglist_count(void *buf, size_t len)
139 vm_offset_t vaddr, vendaddr;
140 vm_paddr_t lastaddr, paddr;
146 vaddr = trunc_page((vm_offset_t)buf);
147 vendaddr = (vm_offset_t)buf + len;
149 lastaddr = pmap_kextract(vaddr);
151 while (vaddr < vendaddr) {
152 paddr = pmap_kextract(vaddr);
153 if (lastaddr + PAGE_SIZE != paddr)
162 * Allocate a scatter/gather list along with 'nsegs' segments. The
163 * 'mflags' parameters are the same as passed to malloc(9). The caller
164 * should use sglist_free() to free this list.
167 sglist_alloc(int nsegs, int mflags)
171 sg = malloc(sizeof(struct sglist) + nsegs * sizeof(struct sglist_seg),
175 sglist_init(sg, nsegs, (struct sglist_seg *)(sg + 1));
180 * Free a scatter/gather list allocated via sglist_allc().
183 sglist_free(struct sglist *sg)
186 if (refcount_release(&sg->sg_refs))
191 * Append the segments to describe a single kernel virtual address
192 * range to a scatter/gather list. If there are insufficient
193 * segments, then this fails with EFBIG.
196 sglist_append(struct sglist *sg, void *buf, size_t len)
199 if (sg->sg_maxseg == 0)
201 return (_sglist_append_buf(sg, buf, len, NULL, NULL));
205 * Append a single physical address range to a scatter/gather list.
206 * If there are insufficient segments, then this fails with EFBIG.
209 sglist_append_phys(struct sglist *sg, vm_paddr_t paddr, size_t len)
211 struct sglist_seg *ss;
213 if (sg->sg_maxseg == 0)
218 if (sg->sg_nseg == 0) {
219 sg->sg_segs[0].ss_paddr = paddr;
220 sg->sg_segs[0].ss_len = len;
224 ss = &sg->sg_segs[sg->sg_nseg - 1];
225 return (_sglist_append_range(sg, &ss, paddr, len));
229 * Append the segments that describe a single mbuf chain to a
230 * scatter/gather list. If there are insufficient segments, then this
234 sglist_append_mbuf(struct sglist *sg, struct mbuf *m0)
239 if (sg->sg_maxseg == 0)
243 for (m = m0; m != NULL; m = m->m_next) {
245 error = sglist_append(sg, m->m_data, m->m_len);
254 * Append the segments that describe a single user address range to a
255 * scatter/gather list. If there are insufficient segments, then this
259 sglist_append_user(struct sglist *sg, void *buf, size_t len, struct thread *td)
262 if (sg->sg_maxseg == 0)
264 return (_sglist_append_buf(sg, buf, len,
265 vmspace_pmap(td->td_proc->p_vmspace), NULL));
269 * Append the segments that describe a single uio to a scatter/gather
270 * list. If there are insufficient segments, then this fails with
274 sglist_append_uio(struct sglist *sg, struct uio *uio)
277 size_t resid, minlen;
281 if (sg->sg_maxseg == 0)
284 resid = uio->uio_resid;
287 if (uio->uio_segflg == UIO_USERSPACE) {
288 KASSERT(uio->uio_td != NULL,
289 ("sglist_append_uio: USERSPACE but no thread"));
290 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
295 for (i = 0; i < uio->uio_iovcnt && resid != 0; i++) {
297 * Now at the first iovec to load. Load each iovec
298 * until we have exhausted the residual count.
300 minlen = MIN(resid, iov[i].iov_len);
302 error = _sglist_append_buf(sg, iov[i].iov_base, minlen,
313 * Append the segments that describe at most 'resid' bytes from a
314 * single uio to a scatter/gather list. If there are insufficient
315 * segments, then only the amount that fits is appended.
318 sglist_consume_uio(struct sglist *sg, struct uio *uio, int resid)
325 if (sg->sg_maxseg == 0)
328 if (uio->uio_segflg == UIO_USERSPACE) {
329 KASSERT(uio->uio_td != NULL,
330 ("sglist_consume_uio: USERSPACE but no thread"));
331 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
336 while (resid > 0 && uio->uio_resid) {
348 * Try to append this iovec. If we run out of room,
349 * then break out of the loop.
351 error = _sglist_append_buf(sg, iov->iov_base, len, pmap, &done);
352 iov->iov_base = (char *)iov->iov_base + done;
353 iov->iov_len -= done;
354 uio->uio_resid -= done;
355 uio->uio_offset += done;
364 * Allocate and populate a scatter/gather list to describe a single
365 * kernel virtual address range.
368 sglist_build(void *buf, size_t len, int mflags)
376 nsegs = sglist_count(buf, len);
377 sg = sglist_alloc(nsegs, mflags);
380 if (sglist_append(sg, buf, len) != 0) {
388 * Clone a new copy of a scatter/gather list.
391 sglist_clone(struct sglist *sg, int mflags)
397 new = sglist_alloc(sg->sg_maxseg, mflags);
400 bcopy(sg->sg_segs, new->sg_segs, sizeof(struct sglist_seg) *
406 * Calculate the total length of the segments described in a
407 * scatter/gather list.
410 sglist_length(struct sglist *sg)
416 for (i = 0; i < sg->sg_nseg; i++)
417 space += sg->sg_segs[i].ss_len;
422 * Split a scatter/gather list into two lists. The scatter/gather
423 * entries for the first 'length' bytes of the 'original' list are
424 * stored in the '*head' list and are removed from 'original'.
426 * If '*head' is NULL, then a new list will be allocated using
427 * 'mflags'. If M_NOWAIT is specified and the allocation fails,
428 * ENOMEM will be returned.
430 * If '*head' is not NULL, it should point to an empty sglist. If it
431 * does not have enough room for the remaining space, then EFBIG will
432 * be returned. If '*head' is not empty, then EINVAL will be
435 * If 'original' is shared (refcount > 1), then EDOOFUS will be
439 sglist_split(struct sglist *original, struct sglist **head, size_t length,
446 if (original->sg_refs > 1)
449 /* Figure out how big of a sglist '*head' has to hold. */
453 for (i = 0; i < original->sg_nseg; i++) {
454 space += original->sg_segs[i].ss_len;
456 if (space >= length) {
458 * If 'length' falls in the middle of a
459 * scatter/gather list entry, then 'split'
460 * holds how much of that entry will remain in
463 split = space - length;
468 /* Nothing to do, so leave head empty. */
473 sg = sglist_alloc(count, mflags);
479 if (sg->sg_maxseg < count)
481 if (sg->sg_nseg != 0)
485 /* Copy 'count' entries to 'sg' from 'original'. */
486 bcopy(original->sg_segs, sg->sg_segs, count *
487 sizeof(struct sglist_seg));
491 * If we had to split a list entry, fixup the last entry in
492 * 'sg' and the new first entry in 'original'. We also
493 * decrement 'count' by 1 since we will only be removing
494 * 'count - 1' segments from 'original' now.
498 sg->sg_segs[count].ss_len -= split;
499 original->sg_segs[count].ss_paddr =
500 sg->sg_segs[count].ss_paddr + split;
501 original->sg_segs[count].ss_len = split;
504 /* Trim 'count' entries from the front of 'original'. */
505 original->sg_nseg -= count;
506 bcopy(original->sg_segs + count, original->sg_segs, count *
507 sizeof(struct sglist_seg));
512 * Append the scatter/gather list elements in 'second' to the
513 * scatter/gather list 'first'. If there is not enough space in
514 * 'first', EFBIG is returned.
517 sglist_join(struct sglist *first, struct sglist *second)
519 struct sglist_seg *flast, *sfirst;
522 /* If 'second' is empty, there is nothing to do. */
523 if (second->sg_nseg == 0)
527 * If the first entry in 'second' can be appended to the last entry
528 * in 'first' then set append to '1'.
531 flast = &first->sg_segs[first->sg_nseg - 1];
532 sfirst = &second->sg_segs[0];
533 if (first->sg_nseg != 0 &&
534 flast->ss_paddr + flast->ss_len == sfirst->ss_paddr)
537 /* Make sure 'first' has enough room. */
538 if (first->sg_nseg + second->sg_nseg - append > first->sg_maxseg)
541 /* Merge last in 'first' and first in 'second' if needed. */
543 flast->ss_len += sfirst->ss_len;
545 /* Append new segments from 'second' to 'first'. */
546 bcopy(first->sg_segs + first->sg_nseg, second->sg_segs + append,
547 (second->sg_nseg - append) * sizeof(struct sglist_seg));
548 first->sg_nseg += second->sg_nseg - append;
549 sglist_reset(second);
554 * Generate a new scatter/gather list from a range of an existing
555 * scatter/gather list. The 'offset' and 'length' parameters specify
556 * the logical range of the 'original' list to extract. If that range
557 * is not a subset of the length of 'original', then EINVAL is
558 * returned. The new scatter/gather list is stored in '*slice'.
560 * If '*slice' is NULL, then a new list will be allocated using
561 * 'mflags'. If M_NOWAIT is specified and the allocation fails,
562 * ENOMEM will be returned.
564 * If '*slice' is not NULL, it should point to an empty sglist. If it
565 * does not have enough room for the remaining space, then EFBIG will
566 * be returned. If '*slice' is not empty, then EINVAL will be
570 sglist_slice(struct sglist *original, struct sglist **slice, size_t offset,
571 size_t length, int mflags)
574 size_t space, end, foffs, loffs;
581 /* Figure out how many segments '*slice' needs to have. */
582 end = offset + length;
587 for (i = 0; i < original->sg_nseg; i++) {
588 space += original->sg_segs[i].ss_len;
589 if (space > offset) {
591 * When we hit the first segment, store its index
592 * in 'fseg' and the offset into the first segment
593 * of 'offset' in 'foffs'.
597 foffs = offset - (space -
598 original->sg_segs[i].ss_len);
599 CTR1(KTR_DEV, "sglist_slice: foffs = %08lx",
605 * When we hit the last segment, break out of
606 * the loop. Store the amount of extra space
607 * at the end of this segment in 'loffs'.
611 CTR1(KTR_DEV, "sglist_slice: loffs = %08lx",
618 /* If we never hit 'end', then 'length' ran off the end, so fail. */
622 if (*slice == NULL) {
623 sg = sglist_alloc(count, mflags);
629 if (sg->sg_maxseg < count)
631 if (sg->sg_nseg != 0)
636 * Copy over 'count' segments from 'original' starting at
639 bcopy(original->sg_segs + fseg, sg->sg_segs,
640 count * sizeof(struct sglist_seg));
643 /* Fixup first and last segments if needed. */
645 sg->sg_segs[0].ss_paddr += foffs;
646 sg->sg_segs[0].ss_len -= foffs;
647 CTR2(KTR_DEV, "sglist_slice seg[0]: %08lx:%08lx",
648 (long)sg->sg_segs[0].ss_paddr, sg->sg_segs[0].ss_len);
651 sg->sg_segs[count - 1].ss_len -= loffs;
652 CTR2(KTR_DEV, "sglist_slice seg[%d]: len %08x", count - 1,
653 sg->sg_segs[count - 1].ss_len);