Skip to content

Commit 82f66fb

Browse files
author
Jens Axboe
committed
[SG] Add helpers for manipulating SG entries
We can then transition drivers without changing the generated code. Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
1 parent 55b70a0 commit 82f66fb

File tree

1 file changed

+104
-8
lines changed

1 file changed

+104
-8
lines changed

include/linux/scatterlist.h

Lines changed: 104 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,37 @@
22
#define _LINUX_SCATTERLIST_H
33

44
#include <asm/scatterlist.h>
5+
#include <asm/io.h>
56
#include <linux/mm.h>
67
#include <linux/string.h>
78

9+
/**
10+
* sg_set_page - Set sg entry to point at given page
11+
* @sg: SG entry
12+
* @page: The page
13+
*
14+
* Description:
15+
* Use this function to set an sg entry pointing at a page, never assign
16+
* the page directly. We encode sg table information in the lower bits
17+
* of the page pointer. See sg_page() for looking up the page belonging
18+
* to an sg entry.
19+
*
20+
**/
21+
static inline void sg_set_page(struct scatterlist *sg, struct page *page)
22+
{
23+
sg->page = page;
24+
}
25+
26+
#define sg_page(sg) ((sg)->page)
27+
828
static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
929
unsigned int buflen)
1030
{
11-
sg->page = virt_to_page(buf);
31+
sg_set_page(sg, virt_to_page(buf));
1232
sg->offset = offset_in_page(buf);
1333
sg->length = buflen;
1434
}
1535

16-
static inline void sg_init_one(struct scatterlist *sg, const void *buf,
17-
unsigned int buflen)
18-
{
19-
memset(sg, 0, sizeof(*sg));
20-
sg_set_buf(sg, buf, buflen);
21-
}
22-
2336
/*
2437
* We overload the LSB of the page pointer to indicate whether it's
2538
* a valid sg entry, or whether it points to the start of a new scatterlist.
@@ -104,4 +117,87 @@ static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
104117
prv[prv_nents - 1].page = (struct page *) ((unsigned long) sgl | 0x01);
105118
}
106119

120+
/**
121+
* sg_mark_end - Mark the end of the scatterlist
122+
* @sgl: Scatterlist
123+
* @nents: Number of entries in sgl
124+
*
125+
* Description:
126+
* Marks the last entry as the termination point for sg_next()
127+
*
128+
**/
129+
static inline void sg_mark_end(struct scatterlist *sgl, unsigned int nents)
130+
{
131+
}
132+
133+
static inline void __sg_mark_end(struct scatterlist *sg)
134+
{
135+
}
136+
137+
138+
/**
139+
* sg_init_one - Initialize a single entry sg list
140+
* @sg: SG entry
141+
* @buf: Virtual address for IO
142+
* @buflen: IO length
143+
*
144+
* Notes:
145+
* This should not be used on a single entry that is part of a larger
146+
* table. Use sg_init_table() for that.
147+
*
148+
**/
149+
static inline void sg_init_one(struct scatterlist *sg, const void *buf,
150+
unsigned int buflen)
151+
{
152+
memset(sg, 0, sizeof(*sg));
153+
sg_mark_end(sg, 1);
154+
sg_set_buf(sg, buf, buflen);
155+
}
156+
157+
/**
158+
* sg_init_table - Initialize SG table
159+
* @sgl: The SG table
160+
* @nents: Number of entries in table
161+
*
162+
* Notes:
163+
* If this is part of a chained sg table, sg_mark_end() should be
164+
* used only on the last table part.
165+
*
166+
**/
167+
static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents)
168+
{
169+
memset(sgl, 0, sizeof(*sgl) * nents);
170+
sg_mark_end(sgl, nents);
171+
}
172+
173+
/**
174+
* sg_phys - Return physical address of an sg entry
175+
* @sg: SG entry
176+
*
177+
* Description:
178+
* This calls page_to_phys() on the page in this sg entry, and adds the
179+
* sg offset. The caller must know that it is legal to call page_to_phys()
180+
* on the sg page.
181+
*
182+
**/
183+
static inline unsigned long sg_phys(struct scatterlist *sg)
184+
{
185+
return page_to_phys(sg_page(sg)) + sg->offset;
186+
}
187+
188+
/**
189+
* sg_virt - Return virtual address of an sg entry
190+
* @sg: SG entry
191+
*
192+
* Description:
193+
* This calls page_address() on the page in this sg entry, and adds the
194+
* sg offset. The caller must know that the sg page has a valid virtual
195+
* mapping.
196+
*
197+
**/
198+
static inline void *sg_virt(struct scatterlist *sg)
199+
{
200+
return page_address(sg_page(sg)) + sg->offset;
201+
}
202+
107203
#endif /* _LINUX_SCATTERLIST_H */

0 commit comments

Comments
 (0)