xref: /freebsd/sys/compat/linuxkpi/common/include/linux/scatterlist.h (revision e92ffd9b626833ebdbf2742c8ffddc6cd94b963e)
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-2017 Mellanox Technologies, Ltd.
6  * Copyright (c) 2015 Matthew Dillon <dillon@backplane.com>
7  * Copyright (c) 2016 Matthew Macy
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice unmodified, this list of conditions, and the following
15  *    disclaimer.
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.
19  *
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.
30  *
31  * $FreeBSD$
32  */
33 #ifndef	_LINUXKPI_LINUX_SCATTERLIST_H_
34 #define	_LINUXKPI_LINUX_SCATTERLIST_H_
35 
36 #include <sys/types.h>
37 #include <sys/sf_buf.h>
38 
39 #include <linux/page.h>
40 #include <linux/slab.h>
41 #include <linux/mm.h>
42 
43 struct bus_dmamap;
44 struct scatterlist {
45 	unsigned long page_link;
46 #define	SG_PAGE_LINK_CHAIN	0x1UL
47 #define	SG_PAGE_LINK_LAST	0x2UL
48 #define	SG_PAGE_LINK_MASK	0x3UL
49 	unsigned int offset;
50 	unsigned int length;
51 	dma_addr_t dma_address;
52 	struct bus_dmamap *dma_map;	/* FreeBSD specific */
53 };
54 
55 CTASSERT((sizeof(struct scatterlist) & SG_PAGE_LINK_MASK) == 0);
56 
57 struct sg_table {
58 	struct scatterlist *sgl;
59 	unsigned int nents;
60 	unsigned int orig_nents;
61 };
62 
63 struct sg_page_iter {
64 	struct scatterlist *sg;
65 	unsigned int sg_pgoffset;
66 	unsigned int maxents;
67 	struct {
68 		unsigned int nents;
69 		int	pg_advance;
70 	} internal;
71 };
72 
73 struct sg_dma_page_iter {
74 	struct sg_page_iter base;
75 };
76 
77 #define	SCATTERLIST_MAX_SEGMENT	(-1U & ~(PAGE_SIZE - 1))
78 
79 #define	SG_MAX_SINGLE_ALLOC	(PAGE_SIZE / sizeof(struct scatterlist))
80 
81 #define	SG_MAGIC		0x87654321UL
82 #define	SG_CHAIN		SG_PAGE_LINK_CHAIN
83 #define	SG_END			SG_PAGE_LINK_LAST
84 
85 #define	sg_is_chain(sg)		((sg)->page_link & SG_PAGE_LINK_CHAIN)
86 #define	sg_is_last(sg)		((sg)->page_link & SG_PAGE_LINK_LAST)
87 #define	sg_chain_ptr(sg)	\
88 	((struct scatterlist *) ((sg)->page_link & ~SG_PAGE_LINK_MASK))
89 
90 #define	sg_dma_address(sg)	(sg)->dma_address
91 #define	sg_dma_len(sg)		(sg)->length
92 
93 #define	for_each_sg_page(sgl, iter, nents, pgoffset)			\
94 	for (_sg_iter_init(sgl, iter, nents, pgoffset);			\
95 	     (iter)->sg; _sg_iter_next(iter))
96 #define	for_each_sg_dma_page(sgl, iter, nents, pgoffset) 		\
97 	for_each_sg_page(sgl, &(iter)->base, nents, pgoffset)
98 
99 #define	for_each_sg(sglist, sg, sgmax, iter)				\
100 	for (iter = 0, sg = (sglist); iter < (sgmax); iter++, sg = sg_next(sg))
101 
102 typedef struct scatterlist *(sg_alloc_fn) (unsigned int, gfp_t);
103 typedef void (sg_free_fn) (struct scatterlist *, unsigned int);
104 
105 static inline void
106 sg_assign_page(struct scatterlist *sg, struct page *page)
107 {
108 	unsigned long page_link = sg->page_link & SG_PAGE_LINK_MASK;
109 
110 	sg->page_link = page_link | (unsigned long)page;
111 }
112 
113 static inline void
114 sg_set_page(struct scatterlist *sg, struct page *page, unsigned int len,
115     unsigned int offset)
116 {
117 	sg_assign_page(sg, page);
118 	sg->offset = offset;
119 	sg->length = len;
120 }
121 
122 static inline struct page *
123 sg_page(struct scatterlist *sg)
124 {
125 	return ((struct page *)((sg)->page_link & ~SG_PAGE_LINK_MASK));
126 }
127 
128 static inline void
129 sg_set_buf(struct scatterlist *sg, const void *buf, unsigned int buflen)
130 {
131 	sg_set_page(sg, virt_to_page(buf), buflen,
132 	    ((uintptr_t)buf) & (PAGE_SIZE - 1));
133 }
134 
135 static inline struct scatterlist *
136 sg_next(struct scatterlist *sg)
137 {
138 	if (sg_is_last(sg))
139 		return (NULL);
140 	sg++;
141 	if (sg_is_chain(sg))
142 		sg = sg_chain_ptr(sg);
143 	return (sg);
144 }
145 
146 static inline vm_paddr_t
147 sg_phys(struct scatterlist *sg)
148 {
149 	return (VM_PAGE_TO_PHYS(sg_page(sg)) + sg->offset);
150 }
151 
152 static inline void *
153 sg_virt(struct scatterlist *sg)
154 {
155 
156 	return ((void *)((unsigned long)page_address(sg_page(sg)) + sg->offset));
157 }
158 
159 static inline void
160 sg_chain(struct scatterlist *prv, unsigned int prv_nents,
161     struct scatterlist *sgl)
162 {
163 	struct scatterlist *sg = &prv[prv_nents - 1];
164 
165 	sg->offset = 0;
166 	sg->length = 0;
167 	sg->page_link = ((unsigned long)sgl |
168 	    SG_PAGE_LINK_CHAIN) & ~SG_PAGE_LINK_LAST;
169 }
170 
171 static inline void
172 sg_mark_end(struct scatterlist *sg)
173 {
174 	sg->page_link |= SG_PAGE_LINK_LAST;
175 	sg->page_link &= ~SG_PAGE_LINK_CHAIN;
176 }
177 
178 static inline void
179 sg_init_table(struct scatterlist *sg, unsigned int nents)
180 {
181 	bzero(sg, sizeof(*sg) * nents);
182 	sg_mark_end(&sg[nents - 1]);
183 }
184 
185 static inline void
186 sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen)
187 {
188 	sg_init_table(sg, 1);
189 	sg_set_buf(sg, buf, buflen);
190 }
191 
192 static struct scatterlist *
193 sg_kmalloc(unsigned int nents, gfp_t gfp_mask)
194 {
195 	if (nents == SG_MAX_SINGLE_ALLOC) {
196 		return ((void *)__get_free_page(gfp_mask));
197 	} else
198 		return (kmalloc(nents * sizeof(struct scatterlist), gfp_mask));
199 }
200 
201 static inline void
202 sg_kfree(struct scatterlist *sg, unsigned int nents)
203 {
204 	if (nents == SG_MAX_SINGLE_ALLOC) {
205 		free_page((unsigned long)sg);
206 	} else
207 		kfree(sg);
208 }
209 
210 static inline void
211 __sg_free_table(struct sg_table *table, unsigned int max_ents,
212     bool skip_first_chunk, sg_free_fn * free_fn)
213 {
214 	struct scatterlist *sgl, *next;
215 
216 	if (unlikely(!table->sgl))
217 		return;
218 
219 	sgl = table->sgl;
220 	while (table->orig_nents) {
221 		unsigned int alloc_size = table->orig_nents;
222 		unsigned int sg_size;
223 
224 		if (alloc_size > max_ents) {
225 			next = sg_chain_ptr(&sgl[max_ents - 1]);
226 			alloc_size = max_ents;
227 			sg_size = alloc_size - 1;
228 		} else {
229 			sg_size = alloc_size;
230 			next = NULL;
231 		}
232 
233 		table->orig_nents -= sg_size;
234 		if (skip_first_chunk)
235 			skip_first_chunk = 0;
236 		else
237 			free_fn(sgl, alloc_size);
238 		sgl = next;
239 	}
240 
241 	table->sgl = NULL;
242 }
243 
244 static inline void
245 sg_free_table(struct sg_table *table)
246 {
247 	__sg_free_table(table, SG_MAX_SINGLE_ALLOC, 0, sg_kfree);
248 }
249 
250 static inline int
251 __sg_alloc_table(struct sg_table *table, unsigned int nents,
252     unsigned int max_ents, struct scatterlist *first_chunk,
253     gfp_t gfp_mask, sg_alloc_fn *alloc_fn)
254 {
255 	struct scatterlist *sg, *prv;
256 	unsigned int left;
257 
258 	memset(table, 0, sizeof(*table));
259 
260 	if (nents == 0)
261 		return (-EINVAL);
262 	left = nents;
263 	prv = NULL;
264 	do {
265 		unsigned int sg_size;
266 		unsigned int alloc_size = left;
267 
268 		if (alloc_size > max_ents) {
269 			alloc_size = max_ents;
270 			sg_size = alloc_size - 1;
271 		} else
272 			sg_size = alloc_size;
273 
274 		left -= sg_size;
275 
276 		if (first_chunk) {
277 			sg = first_chunk;
278 			first_chunk = NULL;
279 		} else {
280 			sg = alloc_fn(alloc_size, gfp_mask);
281 		}
282 		if (unlikely(!sg)) {
283 			if (prv)
284 				table->nents = ++table->orig_nents;
285 
286 			return (-ENOMEM);
287 		}
288 		sg_init_table(sg, alloc_size);
289 		table->nents = table->orig_nents += sg_size;
290 
291 		if (prv)
292 			sg_chain(prv, max_ents, sg);
293 		else
294 			table->sgl = sg;
295 
296 		if (!left)
297 			sg_mark_end(&sg[sg_size - 1]);
298 
299 		prv = sg;
300 	} while (left);
301 
302 	return (0);
303 }
304 
305 static inline int
306 sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
307 {
308 	int ret;
309 
310 	ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
311 	    NULL, gfp_mask, sg_kmalloc);
312 	if (unlikely(ret))
313 		__sg_free_table(table, SG_MAX_SINGLE_ALLOC, 0, sg_kfree);
314 
315 	return (ret);
316 }
317 
318 static inline int
319 __sg_alloc_table_from_pages(struct sg_table *sgt,
320     struct page **pages, unsigned int count,
321     unsigned long off, unsigned long size,
322     unsigned int max_segment, gfp_t gfp_mask)
323 {
324 	unsigned int i, segs, cur, len;
325 	int rc;
326 	struct scatterlist *s;
327 
328 	if (__predict_false(!max_segment || offset_in_page(max_segment)))
329 		return (-EINVAL);
330 
331 	len = 0;
332 	for (segs = i = 1; i < count; ++i) {
333 		len += PAGE_SIZE;
334 		if (len >= max_segment ||
335 		    page_to_pfn(pages[i]) != page_to_pfn(pages[i - 1]) + 1) {
336 			++segs;
337 			len = 0;
338 		}
339 	}
340 	if (__predict_false((rc = sg_alloc_table(sgt, segs, gfp_mask))))
341 		return (rc);
342 
343 	cur = 0;
344 	for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
345 		unsigned long seg_size;
346 		unsigned int j;
347 
348 		len = 0;
349 		for (j = cur + 1; j < count; ++j) {
350 			len += PAGE_SIZE;
351 			if (len >= max_segment || page_to_pfn(pages[j]) !=
352 			    page_to_pfn(pages[j - 1]) + 1)
353 				break;
354 		}
355 
356 		seg_size = ((j - cur) << PAGE_SHIFT) - off;
357 		sg_set_page(s, pages[cur], MIN(size, seg_size), off);
358 		size -= seg_size;
359 		off = 0;
360 		cur = j;
361 	}
362 	return (0);
363 }
364 
365 static inline int
366 sg_alloc_table_from_pages(struct sg_table *sgt,
367     struct page **pages, unsigned int count,
368     unsigned long off, unsigned long size,
369     gfp_t gfp_mask)
370 {
371 
372 	return (__sg_alloc_table_from_pages(sgt, pages, count, off, size,
373 	    SCATTERLIST_MAX_SEGMENT, gfp_mask));
374 }
375 
376 static inline int
377 sg_nents(struct scatterlist *sg)
378 {
379 	int nents;
380 
381 	for (nents = 0; sg; sg = sg_next(sg))
382 		nents++;
383 	return (nents);
384 }
385 
386 static inline void
387 __sg_page_iter_start(struct sg_page_iter *piter,
388     struct scatterlist *sglist, unsigned int nents,
389     unsigned long pgoffset)
390 {
391 	piter->internal.pg_advance = 0;
392 	piter->internal.nents = nents;
393 
394 	piter->sg = sglist;
395 	piter->sg_pgoffset = pgoffset;
396 }
397 
398 static inline void
399 _sg_iter_next(struct sg_page_iter *iter)
400 {
401 	struct scatterlist *sg;
402 	unsigned int pgcount;
403 
404 	sg = iter->sg;
405 	pgcount = (sg->offset + sg->length + PAGE_SIZE - 1) >> PAGE_SHIFT;
406 
407 	++iter->sg_pgoffset;
408 	while (iter->sg_pgoffset >= pgcount) {
409 		iter->sg_pgoffset -= pgcount;
410 		sg = sg_next(sg);
411 		--iter->maxents;
412 		if (sg == NULL || iter->maxents == 0)
413 			break;
414 		pgcount = (sg->offset + sg->length + PAGE_SIZE - 1) >> PAGE_SHIFT;
415 	}
416 	iter->sg = sg;
417 }
418 
419 static inline int
420 sg_page_count(struct scatterlist *sg)
421 {
422 	return (PAGE_ALIGN(sg->offset + sg->length) >> PAGE_SHIFT);
423 }
424 #define	sg_dma_page_count(sg) \
425 	sg_page_count(sg)
426 
427 static inline bool
428 __sg_page_iter_next(struct sg_page_iter *piter)
429 {
430 	unsigned int pgcount;
431 
432 	if (piter->internal.nents == 0)
433 		return (0);
434 	if (piter->sg == NULL)
435 		return (0);
436 
437 	piter->sg_pgoffset += piter->internal.pg_advance;
438 	piter->internal.pg_advance = 1;
439 
440 	while (1) {
441 		pgcount = sg_page_count(piter->sg);
442 		if (likely(piter->sg_pgoffset < pgcount))
443 			break;
444 		piter->sg_pgoffset -= pgcount;
445 		piter->sg = sg_next(piter->sg);
446 		if (--piter->internal.nents == 0)
447 			return (0);
448 		if (piter->sg == NULL)
449 			return (0);
450 	}
451 	return (1);
452 }
453 #define	__sg_page_iter_dma_next(itr) \
454 	__sg_page_iter_next(&(itr)->base)
455 
456 static inline void
457 _sg_iter_init(struct scatterlist *sgl, struct sg_page_iter *iter,
458     unsigned int nents, unsigned long pgoffset)
459 {
460 	if (nents) {
461 		iter->sg = sgl;
462 		iter->sg_pgoffset = pgoffset - 1;
463 		iter->maxents = nents;
464 		_sg_iter_next(iter);
465 	} else {
466 		iter->sg = NULL;
467 		iter->sg_pgoffset = 0;
468 		iter->maxents = 0;
469 	}
470 }
471 
472 /*
473  * sg_page_iter_dma_address() is implemented as a macro because it
474  * needs to accept two different and identical structure types. This
475  * allows both old and new code to co-exist. The compile time assert
476  * adds some safety, that the structure sizes match.
477  */
478 #define	sg_page_iter_dma_address(spi) ({		\
479 	struct sg_page_iter *__spi = (void *)(spi);	\
480 	dma_addr_t __dma_address;			\
481 	CTASSERT(sizeof(*(spi)) == sizeof(*__spi));	\
482 	__dma_address = __spi->sg->dma_address +	\
483 	    (__spi->sg_pgoffset << PAGE_SHIFT);		\
484 	__dma_address;					\
485 })
486 
487 static inline struct page *
488 sg_page_iter_page(struct sg_page_iter *piter)
489 {
490 	return (nth_page(sg_page(piter->sg), piter->sg_pgoffset));
491 }
492 
493 static __inline size_t
494 sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents,
495     const void *buf, size_t buflen, off_t skip)
496 {
497 	struct sg_page_iter piter;
498 	struct page *page;
499 	struct sf_buf *sf;
500 	size_t len, copied;
501 	char *p, *b;
502 
503 	if (buflen == 0)
504 		return (0);
505 
506 	b = __DECONST(char *, buf);
507 	copied = 0;
508 	sched_pin();
509 	for_each_sg_page(sgl, &piter, nents, 0) {
510 
511 		/* Skip to the start. */
512 		if (piter.sg->length <= skip) {
513 			skip -= piter.sg->length;
514 			continue;
515 		}
516 
517 		/* See how much to copy. */
518 		KASSERT(((piter.sg->length - skip) != 0 && (buflen != 0)),
519 		    ("%s: sg len %u - skip %ju || buflen %zu is 0\n",
520 		    __func__, piter.sg->length, (uintmax_t)skip, buflen));
521 		len = min(piter.sg->length - skip, buflen);
522 
523 		page = sg_page_iter_page(&piter);
524 		sf = sf_buf_alloc(page, SFB_CPUPRIVATE | SFB_NOWAIT);
525 		if (sf == NULL)
526 			break;
527 		p = (char *)sf_buf_kva(sf) + piter.sg_pgoffset + skip;
528 		memcpy(p, b, len);
529 		sf_buf_free(sf);
530 
531 		/* We copied so nothing more to skip. */
532 		skip = 0;
533 		copied += len;
534 		/* Either we exactly filled the page, or we are done. */
535 		buflen -= len;
536 		if (buflen == 0)
537 			break;
538 		b += len;
539 	}
540 	sched_unpin();
541 
542 	return (copied);
543 }
544 
545 static inline size_t
546 sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
547     const void *buf, size_t buflen)
548 {
549 	return (sg_pcopy_from_buffer(sgl, nents, buf, buflen, 0));
550 }
551 
552 static inline size_t
553 sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents,
554     void *buf, size_t buflen, off_t offset)
555 {
556 	struct sg_page_iter iter;
557 	struct scatterlist *sg;
558 	struct page *page;
559 	struct sf_buf *sf;
560 	char *vaddr;
561 	size_t total = 0;
562 	size_t len;
563 
564 	if (!PMAP_HAS_DMAP)
565 		sched_pin();
566 	for_each_sg_page(sgl, &iter, nents, 0) {
567 		sg = iter.sg;
568 
569 		if (offset >= sg->length) {
570 			offset -= sg->length;
571 			continue;
572 		}
573 		len = ulmin(buflen, sg->length - offset);
574 		if (len == 0)
575 			break;
576 
577 		page = sg_page_iter_page(&iter);
578 		if (!PMAP_HAS_DMAP) {
579 			sf = sf_buf_alloc(page, SFB_CPUPRIVATE | SFB_NOWAIT);
580 			if (sf == NULL)
581 				break;
582 			vaddr = (char *)sf_buf_kva(sf);
583 		} else
584 			vaddr = (char *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(page));
585 		memcpy(buf, vaddr + sg->offset + offset, len);
586 		if (!PMAP_HAS_DMAP)
587 			sf_buf_free(sf);
588 
589 		/* start at beginning of next page */
590 		offset = 0;
591 
592 		/* advance buffer */
593 		buf = (char *)buf + len;
594 		buflen -= len;
595 		total += len;
596 	}
597 	if (!PMAP_HAS_DMAP)
598 		sched_unpin();
599 	return (total);
600 }
601 
602 #endif					/* _LINUXKPI_LINUX_SCATTERLIST_H_ */
603