2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6 * Copyright (c) 2015 Matthew Dillon <dillon@backplane.com>
7 * Copyright (c) 2016 Matt Macy <mmacy@nextbsd.org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice unmodified, this list of conditions, and the following
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifndef _LINUX_SCATTERLIST_H_
33 #define _LINUX_SCATTERLIST_H_
35 #include <linux/highmem.h>
36 #include <linux/string.h>
37 #include <linux/types.h>
38 #include <linux/bug.h>
45 * If flags bit 0 is set, then the sg field contains a pointer to the next sg
46 * table list. Otherwise the next entry is at sg + 1, can be determined using
47 * the sg_is_chain() function.
49 * If flags bit 1 is set, then this sg entry is the last element in a list,
50 * can be determined using the sg_is_last() function.
59 struct scatterlist *sg;
63 dma_addr_t dma_address;
68 struct scatterlist *sgl; /* the list */
69 unsigned int nents; /* number of mapped entries */
70 unsigned int orig_nents; /* original size of list */
74 struct scatterlist *sg;
75 unsigned int sg_pgoffset; /* page index */
83 * Maximum number of entries that will be allocated in one piece, if
84 * a list larger than this is required then chaining will be utilized.
86 #define SG_MAX_SINGLE_ALLOC (PAGE_SIZE / sizeof(struct scatterlist))
88 #define sg_dma_address(sg) (sg)->dma_address
89 #define sg_dma_len(sg) (sg)->length
90 #define sg_page(sg) (sg)->sl_un.page
91 #define sg_scatternext(sg) (sg)->sl_un.sg
97 sg_set_page(struct scatterlist *sg, struct vm_page *page, unsigned int len,
101 sg_dma_len(sg) = len;
103 if (offset > PAGE_SIZE)
104 panic("sg_set_page: Invalid offset %d\n", offset);
109 sg_set_buf(struct scatterlist *sg, const void *buf, unsigned int buflen)
111 sg_set_page(sg, virt_to_page(buf), buflen,
112 ((uintptr_t)buf) & ~PAGE_MASK);
117 sg_init_table(struct scatterlist *sg, unsigned int nents)
119 bzero(sg, sizeof(*sg) * nents);
120 sg[nents - 1].flags = SG_END;
123 static inline struct scatterlist *
124 sg_next(struct scatterlist *sg)
126 if (sg->flags & SG_END)
129 if (sg->flags & SG_CHAIN)
130 sg = sg_scatternext(sg);
134 static inline vm_paddr_t
135 sg_phys(struct scatterlist *sg)
137 return sg_page(sg)->phys_addr + sg->offset;
141 * sg_chain - Chain two sglists together
142 * @prv: First scatterlist
143 * @prv_nents: Number of entries in prv
144 * @sgl: Second scatterlist
147 * Links @prv@ and @sgl@ together, to form a longer scatterlist.
151 sg_chain(struct scatterlist *prv, unsigned int prv_nents,
152 struct scatterlist *sgl)
155 * offset and length are unused for chain entry. Clear them.
157 struct scatterlist *sg = &prv[prv_nents - 1];
163 * Indicate a link pointer, and set the link to the second list.
165 sg->flags = SG_CHAIN;
170 * sg_mark_end - Mark the end of the scatterlist
171 * @sg: SG entryScatterlist
174 * Marks the passed in sg entry as the termination point for the sg
175 * table. A call to sg_next() on this entry will return NULL.
178 static inline void sg_mark_end(struct scatterlist *sg)
184 * __sg_free_table - Free a previously mapped sg table
185 * @table: The sg table header to use
186 * @max_ents: The maximum number of entries per single scatterlist
189 * Free an sg table previously allocated and setup with
190 * __sg_alloc_table(). The @max_ents value must be identical to
191 * that previously used with __sg_alloc_table().
195 __sg_free_table(struct sg_table *table, unsigned int max_ents)
197 struct scatterlist *sgl, *next;
199 if (unlikely(!table->sgl))
203 while (table->orig_nents) {
204 unsigned int alloc_size = table->orig_nents;
205 unsigned int sg_size;
208 * If we have more than max_ents segments left,
209 * then assign 'next' to the sg table after the current one.
210 * sg_size is then one less than alloc size, since the last
211 * element is the chain pointer.
213 if (alloc_size > max_ents) {
214 next = sgl[max_ents - 1].sl_un.sg;
215 alloc_size = max_ents;
216 sg_size = alloc_size - 1;
218 sg_size = alloc_size;
222 table->orig_nents -= sg_size;
231 * sg_free_table - Free a previously allocated sg table
232 * @table: The mapped sg table header
236 sg_free_table(struct sg_table *table)
238 __sg_free_table(table, SG_MAX_SINGLE_ALLOC);
242 * __sg_alloc_table - Allocate and initialize an sg table with given allocator
243 * @table: The sg table header to use
244 * @nents: Number of entries in sg list
245 * @max_ents: The maximum number of entries the allocator returns per call
246 * @gfp_mask: GFP allocation mask
249 * This function returns a @table @nents long. The allocator is
250 * defined to return scatterlist chunks of maximum size @max_ents.
251 * Thus if @nents is bigger than @max_ents, the scatterlists will be
252 * chained in units of @max_ents.
255 * If this function returns non-0 (eg failure), the caller must call
256 * __sg_free_table() to cleanup any leftover allocations.
260 __sg_alloc_table(struct sg_table *table, unsigned int nents,
261 unsigned int max_ents, gfp_t gfp_mask)
263 struct scatterlist *sg, *prv;
266 memset(table, 0, sizeof(*table));
273 unsigned int sg_size, alloc_size = left;
275 if (alloc_size > max_ents) {
276 alloc_size = max_ents;
277 sg_size = alloc_size - 1;
279 sg_size = alloc_size;
283 sg = kmalloc(alloc_size * sizeof(struct scatterlist), M_DRM, gfp_mask);
286 * Adjust entry count to reflect that the last
287 * entry of the previous table won't be used for
288 * linkage. Without this, sg_kfree() may get
292 table->nents = ++table->orig_nents;
297 sg_init_table(sg, alloc_size);
298 table->nents = table->orig_nents += sg_size;
301 * If this is the first mapping, assign the sg table header.
302 * If this is not the first mapping, chain previous part.
305 sg_chain(prv, max_ents, sg);
310 * If no more entries after this one, mark the end
313 sg_mark_end(&sg[sg_size - 1]);
322 * sg_alloc_table - Allocate and initialize an sg table
323 * @table: The sg table header to use
324 * @nents: Number of entries in sg list
325 * @gfp_mask: GFP allocation mask
328 * Allocate and initialize an sg table. If @nents@ is larger than
329 * SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
334 sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
338 ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
341 __sg_free_table(table, SG_MAX_SINGLE_ALLOC);
347 sg_nents(struct scatterlist *sg)
350 for (nents = 0; sg; sg = sg_next(sg))
356 __sg_page_iter_start(struct sg_page_iter *piter,
357 struct scatterlist *sglist, unsigned int nents,
358 unsigned long pgoffset)
360 piter->__pg_advance = 0;
361 piter->__nents = nents;
364 piter->sg_pgoffset = pgoffset;
368 * Iterate pages in sg list.
371 _sg_iter_next(struct sg_page_iter *iter)
373 struct scatterlist *sg;
374 unsigned int pgcount;
377 pgcount = (sg->offset + sg->length + PAGE_MASK) >> PAGE_SHIFT;
380 while (iter->sg_pgoffset >= pgcount) {
381 iter->sg_pgoffset -= pgcount;
384 if (sg == NULL || iter->maxents == 0)
386 pgcount = (sg->offset + sg->length + PAGE_MASK) >> PAGE_SHIFT;
392 sg_page_count(struct scatterlist *sg)
394 return PAGE_ALIGN(sg->offset + sg->length) >> PAGE_SHIFT;
398 __sg_page_iter_next(struct sg_page_iter *piter)
400 if (piter->__nents == 0)
402 if (piter->sg == NULL)
405 piter->sg_pgoffset += piter->__pg_advance;
406 piter->__pg_advance = 1;
408 while (piter->sg_pgoffset >= sg_page_count(piter->sg)) {
409 piter->sg_pgoffset -= sg_page_count(piter->sg);
410 piter->sg = sg_next(piter->sg);
411 if (--piter->__nents == 0)
413 if (piter->sg == NULL)
420 * NOTE: pgoffset is really a page index, not a byte offset.
423 _sg_iter_init(struct scatterlist *sgl, struct sg_page_iter *iter,
424 unsigned int nents, unsigned long pgoffset)
428 * Nominal case. Note subtract 1 from starting page index
429 * for initial _sg_iter_next() call.
432 iter->sg_pgoffset = pgoffset - 1;
433 iter->maxents = nents;
440 iter->sg_pgoffset = 0;
445 static inline struct vm_page *
446 sg_page_iter_page(struct sg_page_iter *piter)
448 return nth_page(sg_page(piter->sg), piter->sg_pgoffset);
451 static inline dma_addr_t
452 sg_page_iter_dma_address(struct sg_page_iter *spi)
454 return spi->sg->dma_address + (spi->sg_pgoffset << PAGE_SHIFT);
457 #define for_each_sg_page(sgl, iter, nents, pgoffset) \
458 for (_sg_iter_init(sgl, iter, nents, pgoffset); \
459 (iter)->sg; _sg_iter_next(iter))
461 #define for_each_sg(sglist, sg, sgmax, _itr) \
462 for (_itr = 0, sg = (sglist); _itr < (sgmax); _itr++, sg = sg_next(sg))
466 * XXX please review these
469 sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents,
470 const void *buf, size_t buflen, off_t skip)
473 int len, curlen, curoff;
474 struct sg_page_iter iter;
475 struct scatterlist *sg;
476 struct vm_page *page;
480 for_each_sg_page(sgl, &iter, nents, 0) {
484 if (skip && curlen >= skip) {
493 len = min(curlen, buflen - off);
494 page = sg_page_iter_page(&iter);
495 vaddr = (char *)kmap(page) + sg->offset;
496 memcpy(vaddr, (const char *)buf + off, len);
506 sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
507 const char *buf, size_t buflen)
509 return (sg_pcopy_from_buffer(sgl, nents, buf, buflen, 0));
513 sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents,
514 void *buf, size_t buflen, off_t skip)
517 int len, curlen, curoff;
518 struct sg_page_iter iter;
519 struct scatterlist *sg;
520 struct vm_page *page;
524 for_each_sg_page(sgl, &iter, nents, 0) {
528 if (skip && curlen >= skip) {
537 len = min(curlen, buflen - off);
538 page = sg_page_iter_page(&iter);
539 vaddr = (char *)kmap(page) + sg->offset;
540 memcpy((char *)buf + off, vaddr, len);
549 sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
550 char *buf, size_t buflen)
553 return (sg_pcopy_to_buffer(sgl, nents, buf, buflen, 0));
556 #endif /* _LINUX_SCATTERLIST_H_ */