scatterlist.h revision 289644
1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc.
3219820Sjeff * Copyright (c) 2010 iX Systems, Inc.
4219820Sjeff * Copyright (c) 2010 Panasas, Inc.
5270710Shselasky * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6289567Shselasky * Copyright (c) 2015 Matthew Dillon <dillon@backplane.com>
7219820Sjeff * All rights reserved.
8219820Sjeff *
9219820Sjeff * Redistribution and use in source and binary forms, with or without
10219820Sjeff * modification, are permitted provided that the following conditions
11219820Sjeff * are met:
12219820Sjeff * 1. Redistributions of source code must retain the above copyright
13219820Sjeff *    notice unmodified, this list of conditions, and the following
14219820Sjeff *    disclaimer.
15219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
16219820Sjeff *    notice, this list of conditions and the following disclaimer in the
17219820Sjeff *    documentation and/or other materials provided with the distribution.
18219820Sjeff *
19219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29289644Shselasky *
30289644Shselasky * $FreeBSD: head/sys/ofed/include/linux/scatterlist.h 289644 2015-10-20 19:08:26Z hselasky $
31219820Sjeff */
32219820Sjeff#ifndef	_LINUX_SCATTERLIST_H_
33219820Sjeff#define	_LINUX_SCATTERLIST_H_
34219820Sjeff
35219820Sjeff#include <linux/page.h>
36273135Shselasky#include <linux/slab.h>
37219820Sjeff
38273135Shselasky/*
39273135Shselasky * SG table design.
40273135Shselasky *
41273135Shselasky * If flags bit 0 is set, then the sg field contains a pointer to the next sg
42273135Shselasky * table list. Otherwise the next entry is at sg + 1, can be determined using
43273135Shselasky * the sg_is_chain() function.
44273135Shselasky *
45273135Shselasky * If flags bit 1 is set, then this sg entry is the last element in a list,
46273135Shselasky * can be determined using the sg_is_last() function.
47273135Shselasky *
48273135Shselasky * See sg_next().
49273135Shselasky *
50273135Shselasky */
51273135Shselasky
52219820Sjeffstruct scatterlist {
53219820Sjeff	union {
54219820Sjeff		struct page		*page;
55219820Sjeff		struct scatterlist	*sg;
56219820Sjeff	} sl_un;
57219846Skib	dma_addr_t	address;
58219820Sjeff	unsigned long	offset;
59219820Sjeff	uint32_t	length;
60219820Sjeff	uint32_t	flags;
61219820Sjeff};
62219820Sjeff
63270710Shselaskystruct sg_table {
64270710Shselasky	struct scatterlist *sgl;        /* the list */
65270710Shselasky	unsigned int nents;             /* number of mapped entries */
66270710Shselasky	unsigned int orig_nents;        /* original size of list */
67270710Shselasky};
68270710Shselasky
69289567Shselaskystruct sg_page_iter {
70289567Shselasky	struct scatterlist	*sg;
71289567Shselasky	unsigned int		sg_pgoffset;	/* page index */
72289567Shselasky	unsigned int		maxents;
73289567Shselasky};
74289567Shselasky
75273135Shselasky/*
76273135Shselasky * Maximum number of entries that will be allocated in one piece, if
77273135Shselasky * a list larger than this is required then chaining will be utilized.
78273135Shselasky */
79273135Shselasky#define SG_MAX_SINGLE_ALLOC             (PAGE_SIZE / sizeof(struct scatterlist))
80273135Shselasky
81219820Sjeff#define	sg_dma_address(sg)	(sg)->address
82219820Sjeff#define	sg_dma_len(sg)		(sg)->length
83219820Sjeff#define	sg_page(sg)		(sg)->sl_un.page
84219820Sjeff#define	sg_scatternext(sg)	(sg)->sl_un.sg
85219820Sjeff
86219820Sjeff#define	SG_END		0x01
87219820Sjeff#define	SG_CHAIN	0x02
88219820Sjeff
89219820Sjeffstatic inline void
90219820Sjeffsg_set_page(struct scatterlist *sg, struct page *page, unsigned int len,
91219820Sjeff    unsigned int offset)
92219820Sjeff{
93219820Sjeff	sg_page(sg) = page;
94219820Sjeff	sg_dma_len(sg) = len;
95219820Sjeff	sg->offset = offset;
96219820Sjeff	if (offset > PAGE_SIZE)
97219820Sjeff		panic("sg_set_page: Invalid offset %d\n", offset);
98219820Sjeff}
99219820Sjeff
100219820Sjeffstatic inline void
101219820Sjeffsg_set_buf(struct scatterlist *sg, const void *buf, unsigned int buflen)
102219820Sjeff{
103219820Sjeff	sg_set_page(sg, virt_to_page(buf), buflen,
104289567Shselasky	    ((uintptr_t)buf) & (PAGE_SIZE - 1));
105219820Sjeff}
106219820Sjeff
107219820Sjeffstatic inline void
108219820Sjeffsg_init_table(struct scatterlist *sg, unsigned int nents)
109219820Sjeff{
110219820Sjeff	bzero(sg, sizeof(*sg) * nents);
111219820Sjeff	sg[nents - 1].flags = SG_END;
112219820Sjeff}
113219820Sjeff
114219820Sjeffstatic inline struct scatterlist *
115219820Sjeffsg_next(struct scatterlist *sg)
116219820Sjeff{
117219820Sjeff	if (sg->flags & SG_END)
118219820Sjeff		return (NULL);
119219820Sjeff	sg++;
120219820Sjeff	if (sg->flags & SG_CHAIN)
121219820Sjeff		sg = sg_scatternext(sg);
122219820Sjeff	return (sg);
123219820Sjeff}
124219820Sjeff
125219820Sjeffstatic inline vm_paddr_t
126219820Sjeffsg_phys(struct scatterlist *sg)
127219820Sjeff{
128219820Sjeff	return sg_page(sg)->phys_addr + sg->offset;
129219820Sjeff}
130219820Sjeff
131273135Shselasky/**
132273135Shselasky * sg_chain - Chain two sglists together
133273135Shselasky * @prv:        First scatterlist
134273135Shselasky * @prv_nents:  Number of entries in prv
135273135Shselasky * @sgl:        Second scatterlist
136273135Shselasky *
137273135Shselasky * Description:
138273135Shselasky *   Links @prv@ and @sgl@ together, to form a longer scatterlist.
139273135Shselasky *
140273135Shselasky **/
141273135Shselaskystatic inline void
142273135Shselaskysg_chain(struct scatterlist *prv, unsigned int prv_nents,
143273135Shselasky					struct scatterlist *sgl)
144273135Shselasky{
145273135Shselasky/*
146273135Shselasky * offset and length are unused for chain entry.  Clear them.
147273135Shselasky */
148273135Shselasky	struct scatterlist *sg = &prv[prv_nents - 1];
149273135Shselasky
150273135Shselasky	sg->offset = 0;
151273135Shselasky	sg->length = 0;
152273135Shselasky
153273135Shselasky	/*
154273135Shselasky	 * Indicate a link pointer, and set the link to the second list.
155273135Shselasky	 */
156273135Shselasky	sg->flags = SG_CHAIN;
157273135Shselasky	sg->sl_un.sg = sgl;
158273135Shselasky}
159273135Shselasky
160273135Shselasky/**
161273135Shselasky * sg_mark_end - Mark the end of the scatterlist
162273135Shselasky * @sg:          SG entryScatterlist
163273135Shselasky *
164273135Shselasky * Description:
165273135Shselasky *   Marks the passed in sg entry as the termination point for the sg
166273135Shselasky *   table. A call to sg_next() on this entry will return NULL.
167273135Shselasky *
168273135Shselasky **/
169273135Shselaskystatic inline void sg_mark_end(struct scatterlist *sg)
170273135Shselasky{
171273135Shselasky        sg->flags = SG_END;
172273135Shselasky}
173273135Shselasky
174273135Shselasky/**
175273135Shselasky * __sg_free_table - Free a previously mapped sg table
176273135Shselasky * @table:      The sg table header to use
177273135Shselasky * @max_ents:   The maximum number of entries per single scatterlist
178273135Shselasky *
179273135Shselasky *  Description:
180273135Shselasky *    Free an sg table previously allocated and setup with
181273135Shselasky *    __sg_alloc_table().  The @max_ents value must be identical to
182273135Shselasky *    that previously used with __sg_alloc_table().
183273135Shselasky *
184273135Shselasky **/
185273135Shselaskystatic inline void
186273135Shselasky__sg_free_table(struct sg_table *table, unsigned int max_ents)
187273135Shselasky{
188273135Shselasky	struct scatterlist *sgl, *next;
189273135Shselasky
190273135Shselasky	if (unlikely(!table->sgl))
191273135Shselasky		return;
192273135Shselasky
193273135Shselasky	sgl = table->sgl;
194273135Shselasky	while (table->orig_nents) {
195273135Shselasky		unsigned int alloc_size = table->orig_nents;
196273135Shselasky		unsigned int sg_size;
197273135Shselasky
198273135Shselasky		/*
199273135Shselasky		 * If we have more than max_ents segments left,
200273135Shselasky		 * then assign 'next' to the sg table after the current one.
201273135Shselasky		 * sg_size is then one less than alloc size, since the last
202273135Shselasky		 * element is the chain pointer.
203273135Shselasky		 */
204273135Shselasky		if (alloc_size > max_ents) {
205273135Shselasky			next = sgl[max_ents - 1].sl_un.sg;
206273135Shselasky			alloc_size = max_ents;
207273135Shselasky			sg_size = alloc_size - 1;
208273135Shselasky		} else {
209273135Shselasky			sg_size = alloc_size;
210273135Shselasky			next = NULL;
211273135Shselasky		}
212273135Shselasky
213273135Shselasky		table->orig_nents -= sg_size;
214273135Shselasky		kfree(sgl);
215273135Shselasky		sgl = next;
216273135Shselasky	}
217273135Shselasky
218273135Shselasky	table->sgl = NULL;
219273135Shselasky}
220273135Shselasky
221273135Shselasky/**
222273135Shselasky * sg_free_table - Free a previously allocated sg table
223273135Shselasky * @table:      The mapped sg table header
224273135Shselasky *
225273135Shselasky **/
226273135Shselaskystatic inline void
227273135Shselaskysg_free_table(struct sg_table *table)
228273135Shselasky{
229273135Shselasky	__sg_free_table(table, SG_MAX_SINGLE_ALLOC);
230273135Shselasky}
231273135Shselasky
232273135Shselasky/**
233273135Shselasky * __sg_alloc_table - Allocate and initialize an sg table with given allocator
234273135Shselasky * @table:      The sg table header to use
235273135Shselasky * @nents:      Number of entries in sg list
236273135Shselasky * @max_ents:   The maximum number of entries the allocator returns per call
237273135Shselasky * @gfp_mask:   GFP allocation mask
238273135Shselasky *
239273135Shselasky * Description:
240273135Shselasky *   This function returns a @table @nents long. The allocator is
241273135Shselasky *   defined to return scatterlist chunks of maximum size @max_ents.
242273135Shselasky *   Thus if @nents is bigger than @max_ents, the scatterlists will be
243273135Shselasky *   chained in units of @max_ents.
244273135Shselasky *
245273135Shselasky * Notes:
246273135Shselasky *   If this function returns non-0 (eg failure), the caller must call
247273135Shselasky *   __sg_free_table() to cleanup any leftover allocations.
248273135Shselasky *
249273135Shselasky **/
250273135Shselaskystatic inline int
251273135Shselasky__sg_alloc_table(struct sg_table *table, unsigned int nents,
252273135Shselasky		unsigned int max_ents, gfp_t gfp_mask)
253273135Shselasky{
254273135Shselasky	struct scatterlist *sg, *prv;
255273135Shselasky	unsigned int left;
256273135Shselasky
257273135Shselasky	memset(table, 0, sizeof(*table));
258273135Shselasky
259273135Shselasky	if (nents == 0)
260273135Shselasky		return -EINVAL;
261273135Shselasky	left = nents;
262273135Shselasky	prv = NULL;
263273135Shselasky	do {
264273135Shselasky		unsigned int sg_size, alloc_size = left;
265273135Shselasky
266273135Shselasky		if (alloc_size > max_ents) {
267273135Shselasky			alloc_size = max_ents;
268273135Shselasky			sg_size = alloc_size - 1;
269273135Shselasky		} else
270273135Shselasky			sg_size = alloc_size;
271273135Shselasky
272273135Shselasky		left -= sg_size;
273273135Shselasky
274273135Shselasky		sg = kmalloc(alloc_size * sizeof(struct scatterlist), gfp_mask);
275273135Shselasky		if (unlikely(!sg)) {
276273135Shselasky		/*
277273135Shselasky		 * Adjust entry count to reflect that the last
278273135Shselasky		 * entry of the previous table won't be used for
279273135Shselasky		 * linkage.  Without this, sg_kfree() may get
280273135Shselasky		 * confused.
281273135Shselasky		 */
282273135Shselasky			if (prv)
283273135Shselasky				table->nents = ++table->orig_nents;
284273135Shselasky
285273135Shselasky			return -ENOMEM;
286273135Shselasky		}
287273135Shselasky
288273135Shselasky		sg_init_table(sg, alloc_size);
289273135Shselasky		table->nents = table->orig_nents += sg_size;
290273135Shselasky
291273135Shselasky		/*
292273135Shselasky		 * If this is the first mapping, assign the sg table header.
293273135Shselasky		 * If this is not the first mapping, chain previous part.
294273135Shselasky		 */
295273135Shselasky		if (prv)
296273135Shselasky			sg_chain(prv, max_ents, sg);
297273135Shselasky		else
298273135Shselasky			table->sgl = sg;
299273135Shselasky
300273135Shselasky		/*
301273135Shselasky		* If no more entries after this one, mark the end
302273135Shselasky		*/
303273135Shselasky		if (!left)
304273135Shselasky			sg_mark_end(&sg[sg_size - 1]);
305273135Shselasky
306273135Shselasky		prv = sg;
307273135Shselasky	} while (left);
308273135Shselasky
309273135Shselasky	return 0;
310273135Shselasky}
311273135Shselasky
312273135Shselasky/**
313273135Shselasky * sg_alloc_table - Allocate and initialize an sg table
314273135Shselasky * @table:      The sg table header to use
315273135Shselasky * @nents:      Number of entries in sg list
316273135Shselasky * @gfp_mask:   GFP allocation mask
317273135Shselasky *
318273135Shselasky *  Description:
319273135Shselasky *    Allocate and initialize an sg table. If @nents@ is larger than
320273135Shselasky *    SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
321273135Shselasky *
322273135Shselasky **/
323273135Shselasky
324273135Shselaskystatic inline int
325273135Shselaskysg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
326273135Shselasky{
327273135Shselasky	int ret;
328273135Shselasky
329273135Shselasky	ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
330273135Shselasky		gfp_mask);
331273135Shselasky	if (unlikely(ret))
332273135Shselasky		__sg_free_table(table, SG_MAX_SINGLE_ALLOC);
333273135Shselasky
334273135Shselasky	return ret;
335273135Shselasky}
336273135Shselasky
337289567Shselasky/*
338289567Shselasky * Iterate pages in sg list.
339289567Shselasky */
340289567Shselaskystatic inline void
341289567Shselasky_sg_iter_next(struct sg_page_iter *iter)
342289567Shselasky{
343289567Shselasky	struct scatterlist *sg;
344289567Shselasky	unsigned int pgcount;
345289567Shselasky
346289567Shselasky	sg = iter->sg;
347289567Shselasky	pgcount = (sg->offset + sg->length + PAGE_SIZE - 1) >> PAGE_SHIFT;
348289567Shselasky
349289567Shselasky	++iter->sg_pgoffset;
350289567Shselasky	while (iter->sg_pgoffset >= pgcount) {
351289567Shselasky		iter->sg_pgoffset -= pgcount;
352289567Shselasky		sg = sg_next(sg);
353289567Shselasky		--iter->maxents;
354289567Shselasky		if (sg == NULL || iter->maxents == 0)
355289567Shselasky			break;
356289567Shselasky		pgcount = (sg->offset + sg->length + PAGE_SIZE - 1) >> PAGE_SHIFT;
357289567Shselasky	}
358289567Shselasky	iter->sg = sg;
359289567Shselasky}
360289567Shselasky
361289567Shselasky/*
362289567Shselasky * NOTE: pgoffset is really a page index, not a byte offset.
363289567Shselasky */
364289567Shselaskystatic inline void
365289567Shselasky_sg_iter_init(struct scatterlist *sgl, struct sg_page_iter *iter,
366289567Shselasky	      unsigned int nents, unsigned long pgoffset)
367289567Shselasky{
368289567Shselasky	if (nents) {
369289567Shselasky		/*
370289567Shselasky		 * Nominal case.  Note subtract 1 from starting page index
371289567Shselasky		 * for initial _sg_iter_next() call.
372289567Shselasky		 */
373289567Shselasky		iter->sg = sgl;
374289567Shselasky		iter->sg_pgoffset = pgoffset - 1;
375289567Shselasky		iter->maxents = nents;
376289567Shselasky		_sg_iter_next(iter);
377289567Shselasky	} else {
378289567Shselasky		/*
379289567Shselasky		 * Degenerate case
380289567Shselasky		 */
381289567Shselasky		iter->sg = NULL;
382289567Shselasky		iter->sg_pgoffset = 0;
383289567Shselasky		iter->maxents = 0;
384289567Shselasky	}
385289567Shselasky}
386289567Shselasky
387289567Shselaskystatic inline dma_addr_t
388289567Shselaskysg_page_iter_dma_address(struct sg_page_iter *spi)
389289567Shselasky{
390289567Shselasky	return spi->sg->address + (spi->sg_pgoffset << PAGE_SHIFT);
391289567Shselasky}
392289567Shselasky
393289567Shselasky#define	for_each_sg_page(sgl, iter, nents, pgoffset)			\
394289567Shselasky	for (_sg_iter_init(sgl, iter, nents, pgoffset);			\
395289567Shselasky	     (iter)->sg; _sg_iter_next(iter))
396289567Shselasky
397219820Sjeff#define	for_each_sg(sglist, sg, sgmax, _itr)				\
398219820Sjeff	for (_itr = 0, sg = (sglist); _itr < (sgmax); _itr++, sg = sg_next(sg))
399219820Sjeff
400219820Sjeff#endif	/* _LINUX_SCATTERLIST_H_ */
401