2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2017 Mellanox Technologies, Ltd.
6 * Copyright (c) 2015 Matthew Dillon <dillon@backplane.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifndef _LINUX_SCATTERLIST_H_
33 #define _LINUX_SCATTERLIST_H_
35 #include <linux/page.h>
36 #include <linux/slab.h>
41 unsigned long page_link;
42 #define SG_PAGE_LINK_CHAIN 0x1UL
43 #define SG_PAGE_LINK_LAST 0x2UL
44 #define SG_PAGE_LINK_MASK 0x3UL
47 dma_addr_t dma_address;
48 struct bus_dmamap *dma_map; /* FreeBSD specific */
51 CTASSERT((sizeof(struct scatterlist) & SG_PAGE_LINK_MASK) == 0);
54 struct scatterlist *sgl;
56 unsigned int orig_nents;
60 struct scatterlist *sg;
61 unsigned int sg_pgoffset;
69 struct sg_dma_page_iter {
70 struct sg_page_iter base;
73 #define SCATTERLIST_MAX_SEGMENT (-1U & ~(PAGE_SIZE - 1))
75 #define SG_MAX_SINGLE_ALLOC (PAGE_SIZE / sizeof(struct scatterlist))
77 #define SG_MAGIC 0x87654321UL
78 #define SG_CHAIN SG_PAGE_LINK_CHAIN
79 #define SG_END SG_PAGE_LINK_LAST
81 #define sg_is_chain(sg) ((sg)->page_link & SG_PAGE_LINK_CHAIN)
82 #define sg_is_last(sg) ((sg)->page_link & SG_PAGE_LINK_LAST)
83 #define sg_chain_ptr(sg) \
84 ((struct scatterlist *) ((sg)->page_link & ~SG_PAGE_LINK_MASK))
86 #define sg_dma_address(sg) (sg)->dma_address
87 #define sg_dma_len(sg) (sg)->length
89 #define for_each_sg_page(sgl, iter, nents, pgoffset) \
90 for (_sg_iter_init(sgl, iter, nents, pgoffset); \
91 (iter)->sg; _sg_iter_next(iter))
92 #define for_each_sg_dma_page(sgl, iter, nents, pgoffset) \
93 for_each_sg_page(sgl, &(iter)->base, nents, pgoffset)
95 #define for_each_sg(sglist, sg, sgmax, iter) \
96 for (iter = 0, sg = (sglist); iter < (sgmax); iter++, sg = sg_next(sg))
98 typedef struct scatterlist *(sg_alloc_fn) (unsigned int, gfp_t);
99 typedef void (sg_free_fn) (struct scatterlist *, unsigned int);
102 sg_assign_page(struct scatterlist *sg, struct page *page)
104 unsigned long page_link = sg->page_link & SG_PAGE_LINK_MASK;
106 sg->page_link = page_link | (unsigned long)page;
110 sg_set_page(struct scatterlist *sg, struct page *page, unsigned int len,
113 sg_assign_page(sg, page);
118 static inline struct page *
119 sg_page(struct scatterlist *sg)
121 return ((struct page *)((sg)->page_link & ~SG_PAGE_LINK_MASK));
125 sg_set_buf(struct scatterlist *sg, const void *buf, unsigned int buflen)
127 sg_set_page(sg, virt_to_page(buf), buflen,
128 ((uintptr_t)buf) & (PAGE_SIZE - 1));
131 static inline struct scatterlist *
132 sg_next(struct scatterlist *sg)
138 sg = sg_chain_ptr(sg);
142 static inline vm_paddr_t
143 sg_phys(struct scatterlist *sg)
145 return (VM_PAGE_TO_PHYS(sg_page(sg)) + sg->offset);
149 sg_virt(struct scatterlist *sg)
152 return ((void *)((unsigned long)page_address(sg_page(sg)) + sg->offset));
156 sg_chain(struct scatterlist *prv, unsigned int prv_nents,
157 struct scatterlist *sgl)
159 struct scatterlist *sg = &prv[prv_nents - 1];
163 sg->page_link = ((unsigned long)sgl |
164 SG_PAGE_LINK_CHAIN) & ~SG_PAGE_LINK_LAST;
168 sg_mark_end(struct scatterlist *sg)
170 sg->page_link |= SG_PAGE_LINK_LAST;
171 sg->page_link &= ~SG_PAGE_LINK_CHAIN;
175 sg_init_table(struct scatterlist *sg, unsigned int nents)
177 bzero(sg, sizeof(*sg) * nents);
178 sg_mark_end(&sg[nents - 1]);
181 static struct scatterlist *
182 sg_kmalloc(unsigned int nents, gfp_t gfp_mask)
184 if (nents == SG_MAX_SINGLE_ALLOC) {
185 return ((void *)__get_free_page(gfp_mask));
187 return (kmalloc(nents * sizeof(struct scatterlist), gfp_mask));
191 sg_kfree(struct scatterlist *sg, unsigned int nents)
193 if (nents == SG_MAX_SINGLE_ALLOC) {
194 free_page((unsigned long)sg);
200 __sg_free_table(struct sg_table *table, unsigned int max_ents,
201 bool skip_first_chunk, sg_free_fn * free_fn)
203 struct scatterlist *sgl, *next;
205 if (unlikely(!table->sgl))
209 while (table->orig_nents) {
210 unsigned int alloc_size = table->orig_nents;
211 unsigned int sg_size;
213 if (alloc_size > max_ents) {
214 next = sg_chain_ptr(&sgl[max_ents - 1]);
215 alloc_size = max_ents;
216 sg_size = alloc_size - 1;
218 sg_size = alloc_size;
222 table->orig_nents -= sg_size;
223 if (skip_first_chunk)
224 skip_first_chunk = 0;
226 free_fn(sgl, alloc_size);
234 sg_free_table(struct sg_table *table)
236 __sg_free_table(table, SG_MAX_SINGLE_ALLOC, 0, sg_kfree);
240 __sg_alloc_table(struct sg_table *table, unsigned int nents,
241 unsigned int max_ents, struct scatterlist *first_chunk,
242 gfp_t gfp_mask, sg_alloc_fn *alloc_fn)
244 struct scatterlist *sg, *prv;
247 memset(table, 0, sizeof(*table));
254 unsigned int sg_size;
255 unsigned int alloc_size = left;
257 if (alloc_size > max_ents) {
258 alloc_size = max_ents;
259 sg_size = alloc_size - 1;
261 sg_size = alloc_size;
269 sg = alloc_fn(alloc_size, gfp_mask);
273 table->nents = ++table->orig_nents;
277 sg_init_table(sg, alloc_size);
278 table->nents = table->orig_nents += sg_size;
281 sg_chain(prv, max_ents, sg);
286 sg_mark_end(&sg[sg_size - 1]);
295 sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
299 ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
300 NULL, gfp_mask, sg_kmalloc);
302 __sg_free_table(table, SG_MAX_SINGLE_ALLOC, 0, sg_kfree);
308 __sg_alloc_table_from_pages(struct sg_table *sgt,
309 struct page **pages, unsigned int count,
310 unsigned long off, unsigned long size,
311 unsigned int max_segment, gfp_t gfp_mask)
313 unsigned int i, segs, cur, len;
315 struct scatterlist *s;
317 if (__predict_false(!max_segment || offset_in_page(max_segment)))
321 for (segs = i = 1; i < count; ++i) {
323 if (len >= max_segment ||
324 page_to_pfn(pages[i]) != page_to_pfn(pages[i - 1]) + 1) {
329 if (__predict_false((rc = sg_alloc_table(sgt, segs, gfp_mask))))
333 for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
334 unsigned long seg_size;
338 for (j = cur + 1; j < count; ++j) {
340 if (len >= max_segment || page_to_pfn(pages[j]) !=
341 page_to_pfn(pages[j - 1]) + 1)
345 seg_size = ((j - cur) << PAGE_SHIFT) - off;
346 sg_set_page(s, pages[cur], min(size, seg_size), off);
355 sg_alloc_table_from_pages(struct sg_table *sgt,
356 struct page **pages, unsigned int count,
357 unsigned long off, unsigned long size,
361 return (__sg_alloc_table_from_pages(sgt, pages, count, off, size,
362 SCATTERLIST_MAX_SEGMENT, gfp_mask));
366 sg_nents(struct scatterlist *sg)
370 for (nents = 0; sg; sg = sg_next(sg))
376 __sg_page_iter_start(struct sg_page_iter *piter,
377 struct scatterlist *sglist, unsigned int nents,
378 unsigned long pgoffset)
380 piter->internal.pg_advance = 0;
381 piter->internal.nents = nents;
384 piter->sg_pgoffset = pgoffset;
388 _sg_iter_next(struct sg_page_iter *iter)
390 struct scatterlist *sg;
391 unsigned int pgcount;
394 pgcount = (sg->offset + sg->length + PAGE_SIZE - 1) >> PAGE_SHIFT;
397 while (iter->sg_pgoffset >= pgcount) {
398 iter->sg_pgoffset -= pgcount;
401 if (sg == NULL || iter->maxents == 0)
403 pgcount = (sg->offset + sg->length + PAGE_SIZE - 1) >> PAGE_SHIFT;
409 sg_page_count(struct scatterlist *sg)
411 return (PAGE_ALIGN(sg->offset + sg->length) >> PAGE_SHIFT);
413 #define sg_dma_page_count(sg) \
417 __sg_page_iter_next(struct sg_page_iter *piter)
419 unsigned int pgcount;
421 if (piter->internal.nents == 0)
423 if (piter->sg == NULL)
426 piter->sg_pgoffset += piter->internal.pg_advance;
427 piter->internal.pg_advance = 1;
430 pgcount = sg_page_count(piter->sg);
431 if (likely(piter->sg_pgoffset < pgcount))
433 piter->sg_pgoffset -= pgcount;
434 piter->sg = sg_next(piter->sg);
435 if (--piter->internal.nents == 0)
437 if (piter->sg == NULL)
442 #define __sg_page_iter_dma_next(itr) \
443 __sg_page_iter_next(&(itr)->base)
446 _sg_iter_init(struct scatterlist *sgl, struct sg_page_iter *iter,
447 unsigned int nents, unsigned long pgoffset)
451 iter->sg_pgoffset = pgoffset - 1;
452 iter->maxents = nents;
456 iter->sg_pgoffset = 0;
462 * sg_page_iter_dma_address() is implemented as a macro because it
463 * needs to accept two different and identical structure types. This
464 * allows both old and new code to co-exist. The compile time assert
465 * adds some safety, that the structure sizes match.
467 #define sg_page_iter_dma_address(spi) ({ \
468 struct sg_page_iter *__spi = (void *)(spi); \
469 dma_addr_t __dma_address; \
470 CTASSERT(sizeof(*(spi)) == sizeof(*__spi)); \
471 __dma_address = __spi->sg->dma_address + \
472 (__spi->sg_pgoffset << PAGE_SHIFT); \
476 static inline struct page *
477 sg_page_iter_page(struct sg_page_iter *piter)
479 return (nth_page(sg_page(piter->sg), piter->sg_pgoffset));
483 #endif /* _LINUX_SCATTERLIST_H_ */