1 /*-
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  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice unmodified, this list of conditions, and the following
14  *    disclaimer.
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.
18  *
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.
29  */
30 
31 #ifndef	_LINUX_SCATTERLIST_H_
32 #define	_LINUX_SCATTERLIST_H_
33 
34 #include <linux/string.h>
35 #include <linux/types.h>
36 #include <linux/bug.h>
37 #include <linux/mm.h>
38 #include <asm/io.h>
39 
40 /*
41  * SG table design.
42  *
43  * If flags bit 0 is set, then the sg field contains a pointer to the next sg
44  * table list. Otherwise the next entry is at sg + 1, can be determined using
45  * the sg_is_chain() function.
46  *
47  * If flags bit 1 is set, then this sg entry is the last element in a list,
48  * can be determined using the sg_is_last() function.
49  *
50  * See sg_next().
51  *
52  */
53 
54 struct scatterlist {
55 	union {
56 		struct vm_page		*page;
57 		struct scatterlist	*sg;
58 	} sl_un;
59 	unsigned long	offset;
60 	uint32_t	length;
61 	dma_addr_t	dma_address;
62 	uint32_t	flags;
63 };
64 
65 struct sg_table {
66 	struct scatterlist *sgl;        /* the list */
67 	unsigned int nents;             /* number of mapped entries */
68 	unsigned int orig_nents;        /* original size of list */
69 };
70 
71 struct sg_page_iter {
72 	struct scatterlist	*sg;
73 	unsigned int		sg_pgoffset;	/* page index */
74 	unsigned int		maxents;
75 };
76 
77 
78 /*
79  * Maximum number of entries that will be allocated in one piece, if
80  * a list larger than this is required then chaining will be utilized.
81  */
82 #define SG_MAX_SINGLE_ALLOC             (PAGE_SIZE / sizeof(struct scatterlist))
83 
84 #define	sg_dma_address(sg)	(sg)->dma_address
85 #define	sg_dma_len(sg)		(sg)->length
86 #define	sg_page(sg)		(sg)->sl_un.page
87 #define	sg_scatternext(sg)	(sg)->sl_un.sg
88 
89 #define	SG_END		0x01
90 #define	SG_CHAIN	0x02
91 
92 static inline void
93 sg_set_page(struct scatterlist *sg, struct vm_page *page, unsigned int len,
94     unsigned int offset)
95 {
96 	sg_page(sg) = page;
97 	sg_dma_len(sg) = len;
98 	sg->offset = offset;
99 	if (offset > PAGE_SIZE)
100 		panic("sg_set_page: Invalid offset %d\n", offset);
101 }
102 
103 #if 0
104 static inline void
105 sg_set_buf(struct scatterlist *sg, const void *buf, unsigned int buflen)
106 {
107 	sg_set_page(sg, virt_to_page(buf), buflen,
108 	    ((uintptr_t)buf) & ~PAGE_MASK);
109 }
110 #endif
111 
112 static inline void
113 sg_init_table(struct scatterlist *sg, unsigned int nents)
114 {
115 	bzero(sg, sizeof(*sg) * nents);
116 	sg[nents - 1].flags = SG_END;
117 }
118 
119 static inline struct scatterlist *
120 sg_next(struct scatterlist *sg)
121 {
122 	if (sg->flags & SG_END)
123 		return (NULL);
124 	sg++;
125 	if (sg->flags & SG_CHAIN)
126 		sg = sg_scatternext(sg);
127 	return (sg);
128 }
129 
130 static inline vm_paddr_t
131 sg_phys(struct scatterlist *sg)
132 {
133 	return sg_page(sg)->phys_addr + sg->offset;
134 }
135 
136 /**
137  * sg_chain - Chain two sglists together
138  * @prv:        First scatterlist
139  * @prv_nents:  Number of entries in prv
140  * @sgl:        Second scatterlist
141  *
142  * Description:
143  *   Links @prv@ and @sgl@ together, to form a longer scatterlist.
144  *
145  **/
146 static inline void
147 sg_chain(struct scatterlist *prv, unsigned int prv_nents,
148 					struct scatterlist *sgl)
149 {
150 /*
151  * offset and length are unused for chain entry.  Clear them.
152  */
153 	struct scatterlist *sg = &prv[prv_nents - 1];
154 
155 	sg->offset = 0;
156 	sg->length = 0;
157 
158 	/*
159 	 * Indicate a link pointer, and set the link to the second list.
160 	 */
161 	sg->flags = SG_CHAIN;
162 	sg->sl_un.sg = sgl;
163 }
164 
165 /**
166  * sg_mark_end - Mark the end of the scatterlist
167  * @sg:          SG entryScatterlist
168  *
169  * Description:
170  *   Marks the passed in sg entry as the termination point for the sg
171  *   table. A call to sg_next() on this entry will return NULL.
172  *
173  **/
174 static inline void sg_mark_end(struct scatterlist *sg)
175 {
176         sg->flags = SG_END;
177 }
178 
179 /**
180  * __sg_free_table - Free a previously mapped sg table
181  * @table:      The sg table header to use
182  * @max_ents:   The maximum number of entries per single scatterlist
183  *
184  *  Description:
185  *    Free an sg table previously allocated and setup with
186  *    __sg_alloc_table().  The @max_ents value must be identical to
187  *    that previously used with __sg_alloc_table().
188  *
189  **/
190 static inline void
191 __sg_free_table(struct sg_table *table, unsigned int max_ents)
192 {
193 	struct scatterlist *sgl, *next;
194 
195 	if (unlikely(!table->sgl))
196 		return;
197 
198 	sgl = table->sgl;
199 	while (table->orig_nents) {
200 		unsigned int alloc_size = table->orig_nents;
201 		unsigned int sg_size;
202 
203 		/*
204 		 * If we have more than max_ents segments left,
205 		 * then assign 'next' to the sg table after the current one.
206 		 * sg_size is then one less than alloc size, since the last
207 		 * element is the chain pointer.
208 		 */
209 		if (alloc_size > max_ents) {
210 			next = sgl[max_ents - 1].sl_un.sg;
211 			alloc_size = max_ents;
212 			sg_size = alloc_size - 1;
213 		} else {
214 			sg_size = alloc_size;
215 			next = NULL;
216 		}
217 
218 		table->orig_nents -= sg_size;
219 		kfree(sgl);
220 		sgl = next;
221 	}
222 
223 	table->sgl = NULL;
224 }
225 
226 /**
227  * sg_free_table - Free a previously allocated sg table
228  * @table:      The mapped sg table header
229  *
230  **/
231 static inline void
232 sg_free_table(struct sg_table *table)
233 {
234 	__sg_free_table(table, SG_MAX_SINGLE_ALLOC);
235 }
236 
237 /**
238  * __sg_alloc_table - Allocate and initialize an sg table with given allocator
239  * @table:      The sg table header to use
240  * @nents:      Number of entries in sg list
241  * @max_ents:   The maximum number of entries the allocator returns per call
242  * @gfp_mask:   GFP allocation mask
243  *
244  * Description:
245  *   This function returns a @table @nents long. The allocator is
246  *   defined to return scatterlist chunks of maximum size @max_ents.
247  *   Thus if @nents is bigger than @max_ents, the scatterlists will be
248  *   chained in units of @max_ents.
249  *
250  * Notes:
251  *   If this function returns non-0 (eg failure), the caller must call
252  *   __sg_free_table() to cleanup any leftover allocations.
253  *
254  **/
255 static inline int
256 __sg_alloc_table(struct sg_table *table, unsigned int nents,
257 		unsigned int max_ents, gfp_t gfp_mask)
258 {
259 	struct scatterlist *sg, *prv;
260 	unsigned int left;
261 
262 	memset(table, 0, sizeof(*table));
263 
264 	if (nents == 0)
265 		return -EINVAL;
266 	left = nents;
267 	prv = NULL;
268 	do {
269 		unsigned int sg_size, alloc_size = left;
270 
271 		if (alloc_size > max_ents) {
272 			alloc_size = max_ents;
273 			sg_size = alloc_size - 1;
274 		} else
275 			sg_size = alloc_size;
276 
277 		left -= sg_size;
278 
279 		sg = kmalloc(alloc_size * sizeof(struct scatterlist), M_DRM, gfp_mask);
280 		if (unlikely(!sg)) {
281 		/*
282 		 * Adjust entry count to reflect that the last
283 		 * entry of the previous table won't be used for
284 		 * linkage.  Without this, sg_kfree() may get
285 		 * confused.
286 		 */
287 			if (prv)
288 				table->nents = ++table->orig_nents;
289 
290 			return -ENOMEM;
291 		}
292 
293 		sg_init_table(sg, alloc_size);
294 		table->nents = table->orig_nents += sg_size;
295 
296 		/*
297 		 * If this is the first mapping, assign the sg table header.
298 		 * If this is not the first mapping, chain previous part.
299 		 */
300 		if (prv)
301 			sg_chain(prv, max_ents, sg);
302 		else
303 			table->sgl = sg;
304 
305 		/*
306 		* If no more entries after this one, mark the end
307 		*/
308 		if (!left)
309 			sg_mark_end(&sg[sg_size - 1]);
310 
311 		prv = sg;
312 	} while (left);
313 
314 	return 0;
315 }
316 
317 /**
318  * sg_alloc_table - Allocate and initialize an sg table
319  * @table:      The sg table header to use
320  * @nents:      Number of entries in sg list
321  * @gfp_mask:   GFP allocation mask
322  *
323  *  Description:
324  *    Allocate and initialize an sg table. If @nents@ is larger than
325  *    SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
326  *
327  **/
328 
329 static inline int
330 sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
331 {
332 	int ret;
333 
334 	ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
335 		gfp_mask);
336 	if (unlikely(ret))
337 		__sg_free_table(table, SG_MAX_SINGLE_ALLOC);
338 
339 	return ret;
340 }
341 
342 /*
343  * Iterate pages in sg list.
344  */
345 static inline void
346 _sg_iter_next(struct sg_page_iter *iter)
347 {
348 	struct scatterlist *sg;
349 	unsigned int pgcount;
350 
351 	sg = iter->sg;
352 	pgcount = (sg->offset + sg->length + PAGE_MASK) >> PAGE_SHIFT;
353 
354 	++iter->sg_pgoffset;
355 	while (iter->sg_pgoffset >= pgcount) {
356 		iter->sg_pgoffset -= pgcount;
357 		sg = sg_next(sg);
358 		--iter->maxents;
359 		if (sg == NULL || iter->maxents == 0)
360 			break;
361 		pgcount = (sg->offset + sg->length + PAGE_MASK) >> PAGE_SHIFT;
362 	}
363 	iter->sg = sg;
364 }
365 
366 /*
367  * NOTE: pgoffset is really a page index, not a byte offset.
368  */
369 static inline void
370 _sg_iter_init(struct scatterlist *sgl, struct sg_page_iter *iter,
371 	      unsigned int nents, unsigned long pgoffset)
372 {
373 	if (nents) {
374 		/*
375 		 * Nominal case.  Note subtract 1 from starting page index
376 		 * for initial _sg_iter_next() call.
377 		 */
378 		iter->sg = sgl;
379 		iter->sg_pgoffset = pgoffset - 1;
380 		iter->maxents = nents;
381 		_sg_iter_next(iter);
382 	} else {
383 		/*
384 		 * Degenerate case
385 		 */
386 		iter->sg = NULL;
387 		iter->sg_pgoffset = 0;
388 		iter->maxents = 0;
389 	}
390 }
391 
392 static inline struct vm_page *
393 sg_page_iter_page(struct sg_page_iter *piter)
394 {
395 	return nth_page(sg_page(piter->sg), piter->sg_pgoffset);
396 }
397 
398 static inline dma_addr_t
399 sg_page_iter_dma_address(struct sg_page_iter *spi)
400 {
401 	return spi->sg->dma_address + (spi->sg_pgoffset << PAGE_SHIFT);
402 }
403 
404 #define for_each_sg_page(sgl, iter, nents, pgoffset)			\
405 	for (_sg_iter_init(sgl, iter, nents, pgoffset);			\
406 	     (iter)->sg; _sg_iter_next(iter))
407 
408 #define	for_each_sg(sglist, sg, sgmax, _itr)				\
409 	for (_itr = 0, sg = (sglist); _itr < (sgmax); _itr++, sg = sg_next(sg))
410 
411 #endif	/* _LINUX_SCATTERLIST_H_ */
412