scatterlist.h revision 270710
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.
6219820Sjeff * All rights reserved.
7219820Sjeff *
8219820Sjeff * Redistribution and use in source and binary forms, with or without
9219820Sjeff * modification, are permitted provided that the following conditions
10219820Sjeff * are met:
11219820Sjeff * 1. Redistributions of source code must retain the above copyright
12219820Sjeff *    notice unmodified, this list of conditions, and the following
13219820Sjeff *    disclaimer.
14219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
15219820Sjeff *    notice, this list of conditions and the following disclaimer in the
16219820Sjeff *    documentation and/or other materials provided with the distribution.
17219820Sjeff *
18219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28219820Sjeff */
29270710Shselasky
30219820Sjeff#ifndef	_LINUX_SCATTERLIST_H_
31219820Sjeff#define	_LINUX_SCATTERLIST_H_
32219820Sjeff
33219820Sjeff#include <linux/page.h>
34219820Sjeff
35219820Sjeffstruct scatterlist {
36219820Sjeff	union {
37219820Sjeff		struct page		*page;
38219820Sjeff		struct scatterlist	*sg;
39219820Sjeff	} sl_un;
40219846Skib	dma_addr_t	address;
41219820Sjeff	unsigned long	offset;
42219820Sjeff	uint32_t	length;
43219820Sjeff	uint32_t	flags;
44219820Sjeff};
45219820Sjeff
46270710Shselaskystruct sg_table {
47270710Shselasky	struct scatterlist *sgl;        /* the list */
48270710Shselasky	unsigned int nents;             /* number of mapped entries */
49270710Shselasky	unsigned int orig_nents;        /* original size of list */
50270710Shselasky};
51270710Shselasky
52219820Sjeff#define	sg_dma_address(sg)	(sg)->address
53219820Sjeff#define	sg_dma_len(sg)		(sg)->length
54219820Sjeff#define	sg_page(sg)		(sg)->sl_un.page
55219820Sjeff#define	sg_scatternext(sg)	(sg)->sl_un.sg
56219820Sjeff
57219820Sjeff#define	SG_END		0x01
58219820Sjeff#define	SG_CHAIN	0x02
59219820Sjeff
60219820Sjeffstatic inline void
61219820Sjeffsg_set_page(struct scatterlist *sg, struct page *page, unsigned int len,
62219820Sjeff    unsigned int offset)
63219820Sjeff{
64219820Sjeff	sg_page(sg) = page;
65219820Sjeff	sg_dma_len(sg) = len;
66219820Sjeff	sg->offset = offset;
67219820Sjeff	if (offset > PAGE_SIZE)
68219820Sjeff		panic("sg_set_page: Invalid offset %d\n", offset);
69219820Sjeff}
70219820Sjeff
71219820Sjeffstatic inline void
72219820Sjeffsg_set_buf(struct scatterlist *sg, const void *buf, unsigned int buflen)
73219820Sjeff{
74219820Sjeff	sg_set_page(sg, virt_to_page(buf), buflen,
75219820Sjeff	    ((uintptr_t)buf) & ~PAGE_MASK);
76219820Sjeff}
77219820Sjeff
78219820Sjeffstatic inline void
79219820Sjeffsg_init_table(struct scatterlist *sg, unsigned int nents)
80219820Sjeff{
81219820Sjeff	bzero(sg, sizeof(*sg) * nents);
82219820Sjeff	sg[nents - 1].flags = SG_END;
83219820Sjeff}
84219820Sjeff
85219820Sjeffstatic inline struct scatterlist *
86219820Sjeffsg_next(struct scatterlist *sg)
87219820Sjeff{
88219820Sjeff	if (sg->flags & SG_END)
89219820Sjeff		return (NULL);
90219820Sjeff	sg++;
91219820Sjeff	if (sg->flags & SG_CHAIN)
92219820Sjeff		sg = sg_scatternext(sg);
93219820Sjeff	return (sg);
94219820Sjeff}
95219820Sjeff
96219820Sjeffstatic inline vm_paddr_t
97219820Sjeffsg_phys(struct scatterlist *sg)
98219820Sjeff{
99219820Sjeff	return sg_page(sg)->phys_addr + sg->offset;
100219820Sjeff}
101219820Sjeff
102219820Sjeff#define	for_each_sg(sglist, sg, sgmax, _itr)				\
103219820Sjeff	for (_itr = 0, sg = (sglist); _itr < (sgmax); _itr++, sg = sg_next(sg))
104219820Sjeff
105219820Sjeff#endif	/* _LINUX_SCATTERLIST_H_ */
106