~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/virtio/linux/scatterlist.h

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* SPDX-License-Identifier: GPL-2.0 */
  2 #ifndef SCATTERLIST_H
  3 #define SCATTERLIST_H
  4 #include <linux/kernel.h>
  5 #include <linux/bug.h>
  6 
  7 struct scatterlist {
  8         unsigned long   page_link;
  9         unsigned int    offset;
 10         unsigned int    length;
 11         dma_addr_t      dma_address;
 12 };
 13 
 14 /* Scatterlist helpers, stolen from linux/scatterlist.h */
 15 #define sg_is_chain(sg)         ((sg)->page_link & 0x01)
 16 #define sg_is_last(sg)          ((sg)->page_link & 0x02)
 17 #define sg_chain_ptr(sg)        \
 18         ((struct scatterlist *) ((sg)->page_link & ~0x03))
 19 
 20 /**
 21  * sg_assign_page - Assign a given page to an SG entry
 22  * @sg:             SG entry
 23  * @page:           The page
 24  *
 25  * Description:
 26  *   Assign page to sg entry. Also see sg_set_page(), the most commonly used
 27  *   variant.
 28  *
 29  **/
 30 static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
 31 {
 32         unsigned long page_link = sg->page_link & 0x3;
 33 
 34         /*
 35          * In order for the low bit stealing approach to work, pages
 36          * must be aligned at a 32-bit boundary as a minimum.
 37          */
 38         BUG_ON((unsigned long) page & 0x03);
 39 #ifdef CONFIG_DEBUG_SG
 40         BUG_ON(sg_is_chain(sg));
 41 #endif
 42         sg->page_link = page_link | (unsigned long) page;
 43 }
 44 
 45 /**
 46  * sg_set_page - Set sg entry to point at given page
 47  * @sg:          SG entry
 48  * @page:        The page
 49  * @len:         Length of data
 50  * @offset:      Offset into page
 51  *
 52  * Description:
 53  *   Use this function to set an sg entry pointing at a page, never assign
 54  *   the page directly. We encode sg table information in the lower bits
 55  *   of the page pointer. See sg_page() for looking up the page belonging
 56  *   to an sg entry.
 57  *
 58  **/
 59 static inline void sg_set_page(struct scatterlist *sg, struct page *page,
 60                                unsigned int len, unsigned int offset)
 61 {
 62         sg_assign_page(sg, page);
 63         sg->offset = offset;
 64         sg->length = len;
 65 }
 66 
 67 static inline struct page *sg_page(struct scatterlist *sg)
 68 {
 69 #ifdef CONFIG_DEBUG_SG
 70         BUG_ON(sg_is_chain(sg));
 71 #endif
 72         return (struct page *)((sg)->page_link & ~0x3);
 73 }
 74 
 75 /*
 76  * Loop over each sg element, following the pointer to a new list if necessary
 77  */
 78 #define for_each_sg(sglist, sg, nr, __i)        \
 79         for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
 80 
 81 /**
 82  * sg_chain - Chain two sglists together
 83  * @prv:        First scatterlist
 84  * @prv_nents:  Number of entries in prv
 85  * @sgl:        Second scatterlist
 86  *
 87  * Description:
 88  *   Links @prv@ and @sgl@ together, to form a longer scatterlist.
 89  *
 90  **/
 91 static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
 92                             struct scatterlist *sgl)
 93 {
 94         /*
 95          * offset and length are unused for chain entry.  Clear them.
 96          */
 97         prv[prv_nents - 1].offset = 0;
 98         prv[prv_nents - 1].length = 0;
 99 
100         /*
101          * Set lowest bit to indicate a link pointer, and make sure to clear
102          * the termination bit if it happens to be set.
103          */
104         prv[prv_nents - 1].page_link = ((unsigned long) sgl | 0x01) & ~0x02;
105 }
106 
107 /**
108  * sg_mark_end - Mark the end of the scatterlist
109  * @sg:          SG entryScatterlist
110  *
111  * Description:
112  *   Marks the passed in sg entry as the termination point for the sg
113  *   table. A call to sg_next() on this entry will return NULL.
114  *
115  **/
116 static inline void sg_mark_end(struct scatterlist *sg)
117 {
118         /*
119          * Set termination bit, clear potential chain bit
120          */
121         sg->page_link |= 0x02;
122         sg->page_link &= ~0x01;
123 }
124 
125 /**
126  * sg_unmark_end - Undo setting the end of the scatterlist
127  * @sg:          SG entryScatterlist
128  *
129  * Description:
130  *   Removes the termination marker from the given entry of the scatterlist.
131  *
132  **/
133 static inline void sg_unmark_end(struct scatterlist *sg)
134 {
135         sg->page_link &= ~0x02;
136 }
137 
138 static inline struct scatterlist *sg_next(struct scatterlist *sg)
139 {
140         if (sg_is_last(sg))
141                 return NULL;
142 
143         sg++;
144         if (unlikely(sg_is_chain(sg)))
145                 sg = sg_chain_ptr(sg);
146 
147         return sg;
148 }
149 
150 static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents)
151 {
152         memset(sgl, 0, sizeof(*sgl) * nents);
153         sg_mark_end(&sgl[nents - 1]);
154 }
155 
156 static inline dma_addr_t sg_phys(struct scatterlist *sg)
157 {
158         return page_to_phys(sg_page(sg)) + sg->offset;
159 }
160 
161 static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
162                               unsigned int buflen)
163 {
164         sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
165 }
166 
167 static inline void sg_init_one(struct scatterlist *sg,
168                                const void *buf, unsigned int buflen)
169 {
170         sg_init_table(sg, 1);
171         sg_set_buf(sg, buf, buflen);
172 }
173 #endif /* SCATTERLIST_H */
174 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php