0001
0002 #ifndef _LINUX_SCATTERLIST_H
0003 #define _LINUX_SCATTERLIST_H
0004
0005 #include <linux/string.h>
0006 #include <linux/types.h>
0007 #include <linux/bug.h>
0008 #include <linux/mm.h>
0009 #include <asm/io.h>
0010
0011 struct scatterlist {
0012 unsigned long page_link;
0013 unsigned int offset;
0014 unsigned int length;
0015 dma_addr_t dma_address;
0016 #ifdef CONFIG_NEED_SG_DMA_LENGTH
0017 unsigned int dma_length;
0018 #endif
0019 #ifdef CONFIG_PCI_P2PDMA
0020 unsigned int dma_flags;
0021 #endif
0022 };
0023
0024
0025
0026
0027
0028
0029
0030
0031 #define sg_dma_address(sg) ((sg)->dma_address)
0032
0033 #ifdef CONFIG_NEED_SG_DMA_LENGTH
0034 #define sg_dma_len(sg) ((sg)->dma_length)
0035 #else
0036 #define sg_dma_len(sg) ((sg)->length)
0037 #endif
0038
0039 struct sg_table {
0040 struct scatterlist *sgl;
0041 unsigned int nents;
0042 unsigned int orig_nents;
0043 };
0044
0045 struct sg_append_table {
0046 struct sg_table sgt;
0047 struct scatterlist *prv;
0048 unsigned int total_nents;
0049 };
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067 #define SG_CHAIN 0x01UL
0068 #define SG_END 0x02UL
0069
0070
0071
0072
0073
0074
0075 #define SG_PAGE_LINK_MASK (SG_CHAIN | SG_END)
0076
0077 static inline unsigned int __sg_flags(struct scatterlist *sg)
0078 {
0079 return sg->page_link & SG_PAGE_LINK_MASK;
0080 }
0081
0082 static inline struct scatterlist *sg_chain_ptr(struct scatterlist *sg)
0083 {
0084 return (struct scatterlist *)(sg->page_link & ~SG_PAGE_LINK_MASK);
0085 }
0086
0087 static inline bool sg_is_chain(struct scatterlist *sg)
0088 {
0089 return __sg_flags(sg) & SG_CHAIN;
0090 }
0091
0092 static inline bool sg_is_last(struct scatterlist *sg)
0093 {
0094 return __sg_flags(sg) & SG_END;
0095 }
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107 static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
0108 {
0109 unsigned long page_link = sg->page_link & (SG_CHAIN | SG_END);
0110
0111
0112
0113
0114
0115 BUG_ON((unsigned long)page & SG_PAGE_LINK_MASK);
0116 #ifdef CONFIG_DEBUG_SG
0117 BUG_ON(sg_is_chain(sg));
0118 #endif
0119 sg->page_link = page_link | (unsigned long) page;
0120 }
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136 static inline void sg_set_page(struct scatterlist *sg, struct page *page,
0137 unsigned int len, unsigned int offset)
0138 {
0139 sg_assign_page(sg, page);
0140 sg->offset = offset;
0141 sg->length = len;
0142 }
0143
0144 static inline struct page *sg_page(struct scatterlist *sg)
0145 {
0146 #ifdef CONFIG_DEBUG_SG
0147 BUG_ON(sg_is_chain(sg));
0148 #endif
0149 return (struct page *)((sg)->page_link & ~SG_PAGE_LINK_MASK);
0150 }
0151
0152
0153
0154
0155
0156
0157
0158
0159 static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
0160 unsigned int buflen)
0161 {
0162 #ifdef CONFIG_DEBUG_SG
0163 BUG_ON(!virt_addr_valid(buf));
0164 #endif
0165 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
0166 }
0167
0168
0169
0170
0171 #define for_each_sg(sglist, sg, nr, __i) \
0172 for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
0173
0174
0175
0176
0177 #define for_each_sgtable_sg(sgt, sg, i) \
0178 for_each_sg((sgt)->sgl, sg, (sgt)->orig_nents, i)
0179
0180
0181
0182
0183
0184
0185 #define for_each_sgtable_dma_sg(sgt, sg, i) \
0186 for_each_sg((sgt)->sgl, sg, (sgt)->nents, i)
0187
0188 static inline void __sg_chain(struct scatterlist *chain_sg,
0189 struct scatterlist *sgl)
0190 {
0191
0192
0193
0194 chain_sg->offset = 0;
0195 chain_sg->length = 0;
0196
0197
0198
0199
0200
0201 chain_sg->page_link = ((unsigned long) sgl | SG_CHAIN) & ~SG_END;
0202 }
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214 static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
0215 struct scatterlist *sgl)
0216 {
0217 __sg_chain(&prv[prv_nents - 1], sgl);
0218 }
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229 static inline void sg_mark_end(struct scatterlist *sg)
0230 {
0231
0232
0233
0234 sg->page_link |= SG_END;
0235 sg->page_link &= ~SG_CHAIN;
0236 }
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246 static inline void sg_unmark_end(struct scatterlist *sg)
0247 {
0248 sg->page_link &= ~SG_END;
0249 }
0250
0251
0252
0253
0254
0255
0256
0257 #ifdef CONFIG_PCI_P2PDMA
0258
0259 #define SG_DMA_BUS_ADDRESS (1 << 0)
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270 static inline bool sg_is_dma_bus_address(struct scatterlist *sg)
0271 {
0272 return sg->dma_flags & SG_DMA_BUS_ADDRESS;
0273 }
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285 static inline void sg_dma_mark_bus_address(struct scatterlist *sg)
0286 {
0287 sg->dma_flags |= SG_DMA_BUS_ADDRESS;
0288 }
0289
0290
0291
0292
0293
0294
0295
0296
0297 static inline void sg_dma_unmark_bus_address(struct scatterlist *sg)
0298 {
0299 sg->dma_flags &= ~SG_DMA_BUS_ADDRESS;
0300 }
0301
0302 #else
0303
0304 static inline bool sg_is_dma_bus_address(struct scatterlist *sg)
0305 {
0306 return false;
0307 }
0308 static inline void sg_dma_mark_bus_address(struct scatterlist *sg)
0309 {
0310 }
0311 static inline void sg_dma_unmark_bus_address(struct scatterlist *sg)
0312 {
0313 }
0314
0315 #endif
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327 static inline dma_addr_t sg_phys(struct scatterlist *sg)
0328 {
0329 return page_to_phys(sg_page(sg)) + sg->offset;
0330 }
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342 static inline void *sg_virt(struct scatterlist *sg)
0343 {
0344 return page_address(sg_page(sg)) + sg->offset;
0345 }
0346
0347
0348
0349
0350
0351
0352
0353 static inline void sg_init_marker(struct scatterlist *sgl,
0354 unsigned int nents)
0355 {
0356 sg_mark_end(&sgl[nents - 1]);
0357 }
0358
0359 int sg_nents(struct scatterlist *sg);
0360 int sg_nents_for_len(struct scatterlist *sg, u64 len);
0361 struct scatterlist *sg_next(struct scatterlist *);
0362 struct scatterlist *sg_last(struct scatterlist *s, unsigned int);
0363 void sg_init_table(struct scatterlist *, unsigned int);
0364 void sg_init_one(struct scatterlist *, const void *, unsigned int);
0365 int sg_split(struct scatterlist *in, const int in_mapped_nents,
0366 const off_t skip, const int nb_splits,
0367 const size_t *split_sizes,
0368 struct scatterlist **out, int *out_mapped_nents,
0369 gfp_t gfp_mask);
0370
0371 typedef struct scatterlist *(sg_alloc_fn)(unsigned int, gfp_t);
0372 typedef void (sg_free_fn)(struct scatterlist *, unsigned int);
0373
0374 void __sg_free_table(struct sg_table *, unsigned int, unsigned int,
0375 sg_free_fn *, unsigned int);
0376 void sg_free_table(struct sg_table *);
0377 void sg_free_append_table(struct sg_append_table *sgt);
0378 int __sg_alloc_table(struct sg_table *, unsigned int, unsigned int,
0379 struct scatterlist *, unsigned int, gfp_t, sg_alloc_fn *);
0380 int sg_alloc_table(struct sg_table *, unsigned int, gfp_t);
0381 int sg_alloc_append_table_from_pages(struct sg_append_table *sgt,
0382 struct page **pages, unsigned int n_pages,
0383 unsigned int offset, unsigned long size,
0384 unsigned int max_segment,
0385 unsigned int left_pages, gfp_t gfp_mask);
0386 int sg_alloc_table_from_pages_segment(struct sg_table *sgt, struct page **pages,
0387 unsigned int n_pages, unsigned int offset,
0388 unsigned long size,
0389 unsigned int max_segment, gfp_t gfp_mask);
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411 static inline int sg_alloc_table_from_pages(struct sg_table *sgt,
0412 struct page **pages,
0413 unsigned int n_pages,
0414 unsigned int offset,
0415 unsigned long size, gfp_t gfp_mask)
0416 {
0417 return sg_alloc_table_from_pages_segment(sgt, pages, n_pages, offset,
0418 size, UINT_MAX, gfp_mask);
0419 }
0420
0421 #ifdef CONFIG_SGL_ALLOC
0422 struct scatterlist *sgl_alloc_order(unsigned long long length,
0423 unsigned int order, bool chainable,
0424 gfp_t gfp, unsigned int *nent_p);
0425 struct scatterlist *sgl_alloc(unsigned long long length, gfp_t gfp,
0426 unsigned int *nent_p);
0427 void sgl_free_n_order(struct scatterlist *sgl, int nents, int order);
0428 void sgl_free_order(struct scatterlist *sgl, int order);
0429 void sgl_free(struct scatterlist *sgl);
0430 #endif
0431
0432 size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
0433 size_t buflen, off_t skip, bool to_buffer);
0434
0435 size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
0436 const void *buf, size_t buflen);
0437 size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
0438 void *buf, size_t buflen);
0439
0440 size_t sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents,
0441 const void *buf, size_t buflen, off_t skip);
0442 size_t sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents,
0443 void *buf, size_t buflen, off_t skip);
0444 size_t sg_zero_buffer(struct scatterlist *sgl, unsigned int nents,
0445 size_t buflen, off_t skip);
0446
0447
0448
0449
0450
0451 #define SG_MAX_SINGLE_ALLOC (PAGE_SIZE / sizeof(struct scatterlist))
0452
0453
0454
0455
0456
0457
0458
0459
0460 #define SG_CHUNK_SIZE 128
0461
0462
0463
0464
0465
0466 #ifdef CONFIG_ARCH_NO_SG_CHAIN
0467 #define SG_MAX_SEGMENTS SG_CHUNK_SIZE
0468 #else
0469 #define SG_MAX_SEGMENTS 2048
0470 #endif
0471
0472 #ifdef CONFIG_SG_POOL
0473 void sg_free_table_chained(struct sg_table *table,
0474 unsigned nents_first_chunk);
0475 int sg_alloc_table_chained(struct sg_table *table, int nents,
0476 struct scatterlist *first_chunk,
0477 unsigned nents_first_chunk);
0478 #endif
0479
0480
0481
0482
0483
0484
0485
0486
0487
0488
0489
0490 struct sg_page_iter {
0491 struct scatterlist *sg;
0492 unsigned int sg_pgoffset;
0493
0494
0495 unsigned int __nents;
0496 int __pg_advance;
0497
0498 };
0499
0500
0501
0502
0503
0504
0505
0506
0507 struct sg_dma_page_iter {
0508 struct sg_page_iter base;
0509 };
0510
0511 bool __sg_page_iter_next(struct sg_page_iter *piter);
0512 bool __sg_page_iter_dma_next(struct sg_dma_page_iter *dma_iter);
0513 void __sg_page_iter_start(struct sg_page_iter *piter,
0514 struct scatterlist *sglist, unsigned int nents,
0515 unsigned long pgoffset);
0516
0517
0518
0519
0520 static inline struct page *sg_page_iter_page(struct sg_page_iter *piter)
0521 {
0522 return nth_page(sg_page(piter->sg), piter->sg_pgoffset);
0523 }
0524
0525
0526
0527
0528
0529
0530 static inline dma_addr_t
0531 sg_page_iter_dma_address(struct sg_dma_page_iter *dma_iter)
0532 {
0533 return sg_dma_address(dma_iter->base.sg) +
0534 (dma_iter->base.sg_pgoffset << PAGE_SHIFT);
0535 }
0536
0537
0538
0539
0540
0541
0542
0543
0544
0545
0546
0547 #define for_each_sg_page(sglist, piter, nents, pgoffset) \
0548 for (__sg_page_iter_start((piter), (sglist), (nents), (pgoffset)); \
0549 __sg_page_iter_next(piter);)
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562 #define for_each_sg_dma_page(sglist, dma_iter, dma_nents, pgoffset) \
0563 for (__sg_page_iter_start(&(dma_iter)->base, sglist, dma_nents, \
0564 pgoffset); \
0565 __sg_page_iter_dma_next(dma_iter);)
0566
0567
0568
0569
0570
0571
0572
0573
0574
0575
0576
0577 #define for_each_sgtable_page(sgt, piter, pgoffset) \
0578 for_each_sg_page((sgt)->sgl, piter, (sgt)->orig_nents, pgoffset)
0579
0580
0581
0582
0583
0584
0585
0586
0587
0588
0589
0590
0591 #define for_each_sgtable_dma_page(sgt, dma_iter, pgoffset) \
0592 for_each_sg_dma_page((sgt)->sgl, dma_iter, (sgt)->nents, pgoffset)
0593
0594
0595
0596
0597
0598
0599
0600
0601
0602
0603
0604
0605
0606
0607
0608
0609
0610
0611 #define SG_MITER_ATOMIC (1 << 0)
0612 #define SG_MITER_TO_SG (1 << 1)
0613 #define SG_MITER_FROM_SG (1 << 2)
0614
0615 struct sg_mapping_iter {
0616
0617 struct page *page;
0618 void *addr;
0619 size_t length;
0620 size_t consumed;
0621 struct sg_page_iter piter;
0622
0623
0624 unsigned int __offset;
0625 unsigned int __remaining;
0626 unsigned int __flags;
0627 };
0628
0629 void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
0630 unsigned int nents, unsigned int flags);
0631 bool sg_miter_skip(struct sg_mapping_iter *miter, off_t offset);
0632 bool sg_miter_next(struct sg_mapping_iter *miter);
0633 void sg_miter_stop(struct sg_mapping_iter *miter);
0634
0635 #endif