scatterlist.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_SCATTERLIST_H
  3. #define _LINUX_SCATTERLIST_H
  4. #include <linux/string.h>
  5. #include <linux/types.h>
  6. #include <linux/bug.h>
  7. #include <linux/mm.h>
  8. #include <asm/io.h>
  9. struct scatterlist {
  10. unsigned long page_link;
  11. unsigned int offset;
  12. unsigned int length;
  13. dma_addr_t dma_address;
  14. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  15. unsigned int dma_length;
  16. #endif
  17. #ifdef CONFIG_PCI_P2PDMA
  18. unsigned int dma_flags;
  19. #endif
  20. };
  21. /*
  22. * These macros should be used after a dma_map_sg call has been done
  23. * to get bus addresses of each of the SG entries and their lengths.
  24. * You should only work with the number of sg entries dma_map_sg
  25. * returns, or alternatively stop on the first sg_dma_len(sg) which
  26. * is 0.
  27. */
  28. #define sg_dma_address(sg) ((sg)->dma_address)
  29. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  30. #define sg_dma_len(sg) ((sg)->dma_length)
  31. #else
  32. #define sg_dma_len(sg) ((sg)->length)
  33. #endif
  34. struct sg_table {
  35. struct scatterlist *sgl; /* the list */
  36. unsigned int nents; /* number of mapped entries */
  37. unsigned int orig_nents; /* original size of list */
  38. };
  39. struct sg_append_table {
  40. struct sg_table sgt; /* The scatter list table */
  41. struct scatterlist *prv; /* last populated sge in the table */
  42. unsigned int total_nents; /* Total entries in the table */
  43. };
  44. /*
  45. * Notes on SG table design.
  46. *
  47. * We use the unsigned long page_link field in the scatterlist struct to place
  48. * the page pointer AND encode information about the sg table as well. The two
  49. * lower bits are reserved for this information.
  50. *
  51. * If bit 0 is set, then the page_link contains a pointer to the next sg
  52. * table list. Otherwise the next entry is at sg + 1.
  53. *
  54. * If bit 1 is set, then this sg entry is the last element in a list.
  55. *
  56. * See sg_next().
  57. *
  58. */
  59. #define SG_CHAIN 0x01UL
  60. #define SG_END 0x02UL
  61. /*
  62. * We overload the LSB of the page pointer to indicate whether it's
  63. * a valid sg entry, or whether it points to the start of a new scatterlist.
  64. * Those low bits are there for everyone! (thanks mason :-)
  65. */
  66. #define SG_PAGE_LINK_MASK (SG_CHAIN | SG_END)
  67. static inline unsigned int __sg_flags(struct scatterlist *sg)
  68. {
  69. return sg->page_link & SG_PAGE_LINK_MASK;
  70. }
  71. static inline struct scatterlist *sg_chain_ptr(struct scatterlist *sg)
  72. {
  73. return (struct scatterlist *)(sg->page_link & ~SG_PAGE_LINK_MASK);
  74. }
  75. static inline bool sg_is_chain(struct scatterlist *sg)
  76. {
  77. return __sg_flags(sg) & SG_CHAIN;
  78. }
  79. static inline bool sg_is_last(struct scatterlist *sg)
  80. {
  81. return __sg_flags(sg) & SG_END;
  82. }
  83. /**
  84. * sg_assign_page - Assign a given page to an SG entry
  85. * @sg: SG entry
  86. * @page: The page
  87. *
  88. * Description:
  89. * Assign page to sg entry. Also see sg_set_page(), the most commonly used
  90. * variant.
  91. *
  92. **/
  93. static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
  94. {
  95. unsigned long page_link = sg->page_link & (SG_CHAIN | SG_END);
  96. /*
  97. * In order for the low bit stealing approach to work, pages
  98. * must be aligned at a 32-bit boundary as a minimum.
  99. */
  100. BUG_ON((unsigned long)page & SG_PAGE_LINK_MASK);
  101. #ifdef CONFIG_DEBUG_SG
  102. BUG_ON(sg_is_chain(sg));
  103. #endif
  104. sg->page_link = page_link | (unsigned long) page;
  105. }
  106. /**
  107. * sg_set_page - Set sg entry to point at given page
  108. * @sg: SG entry
  109. * @page: The page
  110. * @len: Length of data
  111. * @offset: Offset into page
  112. *
  113. * Description:
  114. * Use this function to set an sg entry pointing at a page, never assign
  115. * the page directly. We encode sg table information in the lower bits
  116. * of the page pointer. See sg_page() for looking up the page belonging
  117. * to an sg entry.
  118. *
  119. **/
  120. static inline void sg_set_page(struct scatterlist *sg, struct page *page,
  121. unsigned int len, unsigned int offset)
  122. {
  123. sg_assign_page(sg, page);
  124. sg->offset = offset;
  125. sg->length = len;
  126. }
  127. static inline struct page *sg_page(struct scatterlist *sg)
  128. {
  129. #ifdef CONFIG_DEBUG_SG
  130. BUG_ON(sg_is_chain(sg));
  131. #endif
  132. return (struct page *)((sg)->page_link & ~SG_PAGE_LINK_MASK);
  133. }
  134. /**
  135. * sg_set_buf - Set sg entry to point at given data
  136. * @sg: SG entry
  137. * @buf: Data
  138. * @buflen: Data length
  139. *
  140. **/
  141. static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
  142. unsigned int buflen)
  143. {
  144. #ifdef CONFIG_DEBUG_SG
  145. BUG_ON(!virt_addr_valid(buf));
  146. #endif
  147. sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
  148. }
  149. /*
  150. * Loop over each sg element, following the pointer to a new list if necessary
  151. */
  152. #define for_each_sg(sglist, sg, nr, __i) \
  153. for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
  154. /*
  155. * Loop over each sg element in the given sg_table object.
  156. */
  157. #define for_each_sgtable_sg(sgt, sg, i) \
  158. for_each_sg((sgt)->sgl, sg, (sgt)->orig_nents, i)
  159. /*
  160. * Loop over each sg element in the given *DMA mapped* sg_table object.
  161. * Please use sg_dma_address(sg) and sg_dma_len(sg) to extract DMA addresses
  162. * of the each element.
  163. */
  164. #define for_each_sgtable_dma_sg(sgt, sg, i) \
  165. for_each_sg((sgt)->sgl, sg, (sgt)->nents, i)
  166. static inline void __sg_chain(struct scatterlist *chain_sg,
  167. struct scatterlist *sgl)
  168. {
  169. /*
  170. * offset and length are unused for chain entry. Clear them.
  171. */
  172. chain_sg->offset = 0;
  173. chain_sg->length = 0;
  174. /*
  175. * Set lowest bit to indicate a link pointer, and make sure to clear
  176. * the termination bit if it happens to be set.
  177. */
  178. chain_sg->page_link = ((unsigned long) sgl | SG_CHAIN) & ~SG_END;
  179. }
  180. /**
  181. * sg_chain - Chain two sglists together
  182. * @prv: First scatterlist
  183. * @prv_nents: Number of entries in prv
  184. * @sgl: Second scatterlist
  185. *
  186. * Description:
  187. * Links @prv@ and @sgl@ together, to form a longer scatterlist.
  188. *
  189. **/
  190. static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
  191. struct scatterlist *sgl)
  192. {
  193. __sg_chain(&prv[prv_nents - 1], sgl);
  194. }
  195. /**
  196. * sg_mark_end - Mark the end of the scatterlist
  197. * @sg: SG entryScatterlist
  198. *
  199. * Description:
  200. * Marks the passed in sg entry as the termination point for the sg
  201. * table. A call to sg_next() on this entry will return NULL.
  202. *
  203. **/
  204. static inline void sg_mark_end(struct scatterlist *sg)
  205. {
  206. /*
  207. * Set termination bit, clear potential chain bit
  208. */
  209. sg->page_link |= SG_END;
  210. sg->page_link &= ~SG_CHAIN;
  211. }
  212. /**
  213. * sg_unmark_end - Undo setting the end of the scatterlist
  214. * @sg: SG entryScatterlist
  215. *
  216. * Description:
  217. * Removes the termination marker from the given entry of the scatterlist.
  218. *
  219. **/
  220. static inline void sg_unmark_end(struct scatterlist *sg)
  221. {
  222. sg->page_link &= ~SG_END;
  223. }
  224. /*
  225. * CONFGI_PCI_P2PDMA depends on CONFIG_64BIT which means there is 4 bytes
  226. * in struct scatterlist (assuming also CONFIG_NEED_SG_DMA_LENGTH is set).
  227. * Use this padding for DMA flags bits to indicate when a specific
  228. * dma address is a bus address.
  229. */
  230. #ifdef CONFIG_PCI_P2PDMA
  231. #define SG_DMA_BUS_ADDRESS (1 << 0)
  232. /**
  233. * sg_dma_is_bus address - Return whether a given segment was marked
  234. * as a bus address
  235. * @sg: SG entry
  236. *
  237. * Description:
  238. * Returns true if sg_dma_mark_bus_address() has been called on
  239. * this segment.
  240. **/
  241. static inline bool sg_is_dma_bus_address(struct scatterlist *sg)
  242. {
  243. return sg->dma_flags & SG_DMA_BUS_ADDRESS;
  244. }
  245. /**
  246. * sg_dma_mark_bus address - Mark the scatterlist entry as a bus address
  247. * @sg: SG entry
  248. *
  249. * Description:
  250. * Marks the passed in sg entry to indicate that the dma_address is
  251. * a bus address and doesn't need to be unmapped. This should only be
  252. * used by dma_map_sg() implementations to mark bus addresses
  253. * so they can be properly cleaned up in dma_unmap_sg().
  254. **/
  255. static inline void sg_dma_mark_bus_address(struct scatterlist *sg)
  256. {
  257. sg->dma_flags |= SG_DMA_BUS_ADDRESS;
  258. }
  259. /**
  260. * sg_unmark_bus_address - Unmark the scatterlist entry as a bus address
  261. * @sg: SG entry
  262. *
  263. * Description:
  264. * Clears the bus address mark.
  265. **/
  266. static inline void sg_dma_unmark_bus_address(struct scatterlist *sg)
  267. {
  268. sg->dma_flags &= ~SG_DMA_BUS_ADDRESS;
  269. }
  270. #else
  271. static inline bool sg_is_dma_bus_address(struct scatterlist *sg)
  272. {
  273. return false;
  274. }
  275. static inline void sg_dma_mark_bus_address(struct scatterlist *sg)
  276. {
  277. }
  278. static inline void sg_dma_unmark_bus_address(struct scatterlist *sg)
  279. {
  280. }
  281. #endif
  282. /**
  283. * sg_phys - Return physical address of an sg entry
  284. * @sg: SG entry
  285. *
  286. * Description:
  287. * This calls page_to_phys() on the page in this sg entry, and adds the
  288. * sg offset. The caller must know that it is legal to call page_to_phys()
  289. * on the sg page.
  290. *
  291. **/
  292. static inline dma_addr_t sg_phys(struct scatterlist *sg)
  293. {
  294. return page_to_phys(sg_page(sg)) + sg->offset;
  295. }
  296. /**
  297. * sg_virt - Return virtual address of an sg entry
  298. * @sg: SG entry
  299. *
  300. * Description:
  301. * This calls page_address() on the page in this sg entry, and adds the
  302. * sg offset. The caller must know that the sg page has a valid virtual
  303. * mapping.
  304. *
  305. **/
  306. static inline void *sg_virt(struct scatterlist *sg)
  307. {
  308. return page_address(sg_page(sg)) + sg->offset;
  309. }
  310. /**
  311. * sg_init_marker - Initialize markers in sg table
  312. * @sgl: The SG table
  313. * @nents: Number of entries in table
  314. *
  315. **/
  316. static inline void sg_init_marker(struct scatterlist *sgl,
  317. unsigned int nents)
  318. {
  319. sg_mark_end(&sgl[nents - 1]);
  320. }
  321. int sg_nents(struct scatterlist *sg);
  322. int sg_nents_for_len(struct scatterlist *sg, u64 len);
  323. struct scatterlist *sg_next(struct scatterlist *);
  324. struct scatterlist *sg_last(struct scatterlist *s, unsigned int);
  325. void sg_init_table(struct scatterlist *, unsigned int);
  326. void sg_init_one(struct scatterlist *, const void *, unsigned int);
  327. int sg_split(struct scatterlist *in, const int in_mapped_nents,
  328. const off_t skip, const int nb_splits,
  329. const size_t *split_sizes,
  330. struct scatterlist **out, int *out_mapped_nents,
  331. gfp_t gfp_mask);
  332. typedef struct scatterlist *(sg_alloc_fn)(unsigned int, gfp_t);
  333. typedef void (sg_free_fn)(struct scatterlist *, unsigned int);
  334. void __sg_free_table(struct sg_table *, unsigned int, unsigned int,
  335. sg_free_fn *, unsigned int);
  336. void sg_free_table(struct sg_table *);
  337. void sg_free_append_table(struct sg_append_table *sgt);
  338. int __sg_alloc_table(struct sg_table *, unsigned int, unsigned int,
  339. struct scatterlist *, unsigned int, gfp_t, sg_alloc_fn *);
  340. int sg_alloc_table(struct sg_table *, unsigned int, gfp_t);
  341. int sg_alloc_append_table_from_pages(struct sg_append_table *sgt,
  342. struct page **pages, unsigned int n_pages,
  343. unsigned int offset, unsigned long size,
  344. unsigned int max_segment,
  345. unsigned int left_pages, gfp_t gfp_mask);
  346. int sg_alloc_table_from_pages_segment(struct sg_table *sgt, struct page **pages,
  347. unsigned int n_pages, unsigned int offset,
  348. unsigned long size,
  349. unsigned int max_segment, gfp_t gfp_mask);
  350. /**
  351. * sg_alloc_table_from_pages - Allocate and initialize an sg table from
  352. * an array of pages
  353. * @sgt: The sg table header to use
  354. * @pages: Pointer to an array of page pointers
  355. * @n_pages: Number of pages in the pages array
  356. * @offset: Offset from start of the first page to the start of a buffer
  357. * @size: Number of valid bytes in the buffer (after offset)
  358. * @gfp_mask: GFP allocation mask
  359. *
  360. * Description:
  361. * Allocate and initialize an sg table from a list of pages. Contiguous
  362. * ranges of the pages are squashed into a single scatterlist node. A user
  363. * may provide an offset at a start and a size of valid data in a buffer
  364. * specified by the page array. The returned sg table is released by
  365. * sg_free_table.
  366. *
  367. * Returns:
  368. * 0 on success, negative error on failure
  369. */
  370. static inline int sg_alloc_table_from_pages(struct sg_table *sgt,
  371. struct page **pages,
  372. unsigned int n_pages,
  373. unsigned int offset,
  374. unsigned long size, gfp_t gfp_mask)
  375. {
  376. return sg_alloc_table_from_pages_segment(sgt, pages, n_pages, offset,
  377. size, UINT_MAX, gfp_mask);
  378. }
  379. #ifdef CONFIG_SGL_ALLOC
  380. struct scatterlist *sgl_alloc_order(unsigned long long length,
  381. unsigned int order, bool chainable,
  382. gfp_t gfp, unsigned int *nent_p);
  383. struct scatterlist *sgl_alloc(unsigned long long length, gfp_t gfp,
  384. unsigned int *nent_p);
  385. void sgl_free_n_order(struct scatterlist *sgl, int nents, int order);
  386. void sgl_free_order(struct scatterlist *sgl, int order);
  387. void sgl_free(struct scatterlist *sgl);
  388. #endif /* CONFIG_SGL_ALLOC */
  389. size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
  390. size_t buflen, off_t skip, bool to_buffer);
  391. size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
  392. const void *buf, size_t buflen);
  393. size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
  394. void *buf, size_t buflen);
  395. size_t sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents,
  396. const void *buf, size_t buflen, off_t skip);
  397. size_t sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents,
  398. void *buf, size_t buflen, off_t skip);
  399. size_t sg_zero_buffer(struct scatterlist *sgl, unsigned int nents,
  400. size_t buflen, off_t skip);
  401. /*
  402. * Maximum number of entries that will be allocated in one piece, if
  403. * a list larger than this is required then chaining will be utilized.
  404. */
  405. #define SG_MAX_SINGLE_ALLOC (PAGE_SIZE / sizeof(struct scatterlist))
  406. /*
  407. * The maximum number of SG segments that we will put inside a
  408. * scatterlist (unless chaining is used). Should ideally fit inside a
  409. * single page, to avoid a higher order allocation. We could define this
  410. * to SG_MAX_SINGLE_ALLOC to pack correctly at the highest order. The
  411. * minimum value is 32
  412. */
  413. #define SG_CHUNK_SIZE 128
  414. /*
  415. * Like SG_CHUNK_SIZE, but for archs that have sg chaining. This limit
  416. * is totally arbitrary, a setting of 2048 will get you at least 8mb ios.
  417. */
  418. #ifdef CONFIG_ARCH_NO_SG_CHAIN
  419. #define SG_MAX_SEGMENTS SG_CHUNK_SIZE
  420. #else
  421. #define SG_MAX_SEGMENTS 2048
  422. #endif
  423. #ifdef CONFIG_SG_POOL
  424. void sg_free_table_chained(struct sg_table *table,
  425. unsigned nents_first_chunk);
  426. int sg_alloc_table_chained(struct sg_table *table, int nents,
  427. struct scatterlist *first_chunk,
  428. unsigned nents_first_chunk);
  429. #endif
  430. /*
  431. * sg page iterator
  432. *
  433. * Iterates over sg entries page-by-page. On each successful iteration, you
  434. * can call sg_page_iter_page(@piter) to get the current page.
  435. * @piter->sg will point to the sg holding this page and @piter->sg_pgoffset to
  436. * the page's page offset within the sg. The iteration will stop either when a
  437. * maximum number of sg entries was reached or a terminating sg
  438. * (sg_last(sg) == true) was reached.
  439. */
  440. struct sg_page_iter {
  441. struct scatterlist *sg; /* sg holding the page */
  442. unsigned int sg_pgoffset; /* page offset within the sg */
  443. /* these are internal states, keep away */
  444. unsigned int __nents; /* remaining sg entries */
  445. int __pg_advance; /* nr pages to advance at the
  446. * next step */
  447. };
  448. /*
  449. * sg page iterator for DMA addresses
  450. *
  451. * This is the same as sg_page_iter however you can call
  452. * sg_page_iter_dma_address(@dma_iter) to get the page's DMA
  453. * address. sg_page_iter_page() cannot be called on this iterator.
  454. */
  455. struct sg_dma_page_iter {
  456. struct sg_page_iter base;
  457. };
  458. bool __sg_page_iter_next(struct sg_page_iter *piter);
  459. bool __sg_page_iter_dma_next(struct sg_dma_page_iter *dma_iter);
  460. void __sg_page_iter_start(struct sg_page_iter *piter,
  461. struct scatterlist *sglist, unsigned int nents,
  462. unsigned long pgoffset);
  463. /**
  464. * sg_page_iter_page - get the current page held by the page iterator
  465. * @piter: page iterator holding the page
  466. */
  467. static inline struct page *sg_page_iter_page(struct sg_page_iter *piter)
  468. {
  469. return nth_page(sg_page(piter->sg), piter->sg_pgoffset);
  470. }
  471. /**
  472. * sg_page_iter_dma_address - get the dma address of the current page held by
  473. * the page iterator.
  474. * @dma_iter: page iterator holding the page
  475. */
  476. static inline dma_addr_t
  477. sg_page_iter_dma_address(struct sg_dma_page_iter *dma_iter)
  478. {
  479. return sg_dma_address(dma_iter->base.sg) +
  480. (dma_iter->base.sg_pgoffset << PAGE_SHIFT);
  481. }
  482. /**
  483. * for_each_sg_page - iterate over the pages of the given sg list
  484. * @sglist: sglist to iterate over
  485. * @piter: page iterator to hold current page, sg, sg_pgoffset
  486. * @nents: maximum number of sg entries to iterate over
  487. * @pgoffset: starting page offset (in pages)
  488. *
  489. * Callers may use sg_page_iter_page() to get each page pointer.
  490. * In each loop it operates on PAGE_SIZE unit.
  491. */
  492. #define for_each_sg_page(sglist, piter, nents, pgoffset) \
  493. for (__sg_page_iter_start((piter), (sglist), (nents), (pgoffset)); \
  494. __sg_page_iter_next(piter);)
  495. /**
  496. * for_each_sg_dma_page - iterate over the pages of the given sg list
  497. * @sglist: sglist to iterate over
  498. * @dma_iter: DMA page iterator to hold current page
  499. * @dma_nents: maximum number of sg entries to iterate over, this is the value
  500. * returned from dma_map_sg
  501. * @pgoffset: starting page offset (in pages)
  502. *
  503. * Callers may use sg_page_iter_dma_address() to get each page's DMA address.
  504. * In each loop it operates on PAGE_SIZE unit.
  505. */
  506. #define for_each_sg_dma_page(sglist, dma_iter, dma_nents, pgoffset) \
  507. for (__sg_page_iter_start(&(dma_iter)->base, sglist, dma_nents, \
  508. pgoffset); \
  509. __sg_page_iter_dma_next(dma_iter);)
  510. /**
  511. * for_each_sgtable_page - iterate over all pages in the sg_table object
  512. * @sgt: sg_table object to iterate over
  513. * @piter: page iterator to hold current page
  514. * @pgoffset: starting page offset (in pages)
  515. *
  516. * Iterates over the all memory pages in the buffer described by
  517. * a scatterlist stored in the given sg_table object.
  518. * See also for_each_sg_page(). In each loop it operates on PAGE_SIZE unit.
  519. */
  520. #define for_each_sgtable_page(sgt, piter, pgoffset) \
  521. for_each_sg_page((sgt)->sgl, piter, (sgt)->orig_nents, pgoffset)
  522. /**
  523. * for_each_sgtable_dma_page - iterate over the DMA mapped sg_table object
  524. * @sgt: sg_table object to iterate over
  525. * @dma_iter: DMA page iterator to hold current page
  526. * @pgoffset: starting page offset (in pages)
  527. *
  528. * Iterates over the all DMA mapped pages in the buffer described by
  529. * a scatterlist stored in the given sg_table object.
  530. * See also for_each_sg_dma_page(). In each loop it operates on PAGE_SIZE
  531. * unit.
  532. */
  533. #define for_each_sgtable_dma_page(sgt, dma_iter, pgoffset) \
  534. for_each_sg_dma_page((sgt)->sgl, dma_iter, (sgt)->nents, pgoffset)
  535. /*
  536. * Mapping sg iterator
  537. *
  538. * Iterates over sg entries mapping page-by-page. On each successful
  539. * iteration, @miter->page points to the mapped page and
  540. * @miter->length bytes of data can be accessed at @miter->addr. As
  541. * long as an iteration is enclosed between start and stop, the user
  542. * is free to choose control structure and when to stop.
  543. *
  544. * @miter->consumed is set to @miter->length on each iteration. It
  545. * can be adjusted if the user can't consume all the bytes in one go.
  546. * Also, a stopped iteration can be resumed by calling next on it.
  547. * This is useful when iteration needs to release all resources and
  548. * continue later (e.g. at the next interrupt).
  549. */
  550. #define SG_MITER_ATOMIC (1 << 0) /* use kmap_atomic */
  551. #define SG_MITER_TO_SG (1 << 1) /* flush back to phys on unmap */
  552. #define SG_MITER_FROM_SG (1 << 2) /* nop */
  553. struct sg_mapping_iter {
  554. /* the following three fields can be accessed directly */
  555. struct page *page; /* currently mapped page */
  556. void *addr; /* pointer to the mapped area */
  557. size_t length; /* length of the mapped area */
  558. size_t consumed; /* number of consumed bytes */
  559. struct sg_page_iter piter; /* page iterator */
  560. /* these are internal states, keep away */
  561. unsigned int __offset; /* offset within page */
  562. unsigned int __remaining; /* remaining bytes on page */
  563. unsigned int __flags;
  564. };
  565. void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
  566. unsigned int nents, unsigned int flags);
  567. bool sg_miter_skip(struct sg_mapping_iter *miter, off_t offset);
  568. bool sg_miter_next(struct sg_mapping_iter *miter);
  569. void sg_miter_stop(struct sg_mapping_iter *miter);
  570. #endif /* _LINUX_SCATTERLIST_H */