sg_split.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2015 Robert Jarzmik <robert.jarzmik@free.fr>
  4. *
  5. * Scatterlist splitting helpers.
  6. */
  7. #include <linux/scatterlist.h>
  8. #include <linux/slab.h>
  9. struct sg_splitter {
  10. struct scatterlist *in_sg0;
  11. int nents;
  12. off_t skip_sg0;
  13. unsigned int length_last_sg;
  14. struct scatterlist *out_sg;
  15. };
  16. static int sg_calculate_split(struct scatterlist *in, int nents, int nb_splits,
  17. off_t skip, const size_t *sizes,
  18. struct sg_splitter *splitters, bool mapped)
  19. {
  20. int i;
  21. unsigned int sglen;
  22. size_t size = sizes[0], len;
  23. struct sg_splitter *curr = splitters;
  24. struct scatterlist *sg;
  25. for (i = 0; i < nb_splits; i++) {
  26. splitters[i].in_sg0 = NULL;
  27. splitters[i].nents = 0;
  28. }
  29. for_each_sg(in, sg, nents, i) {
  30. sglen = mapped ? sg_dma_len(sg) : sg->length;
  31. if (skip > sglen) {
  32. skip -= sglen;
  33. continue;
  34. }
  35. len = min_t(size_t, size, sglen - skip);
  36. if (!curr->in_sg0) {
  37. curr->in_sg0 = sg;
  38. curr->skip_sg0 = skip;
  39. }
  40. size -= len;
  41. curr->nents++;
  42. curr->length_last_sg = len;
  43. while (!size && (skip + len < sglen) && (--nb_splits > 0)) {
  44. curr++;
  45. size = *(++sizes);
  46. skip += len;
  47. len = min_t(size_t, size, sglen - skip);
  48. curr->in_sg0 = sg;
  49. curr->skip_sg0 = skip;
  50. curr->nents = 1;
  51. curr->length_last_sg = len;
  52. size -= len;
  53. }
  54. skip = 0;
  55. if (!size && --nb_splits > 0) {
  56. curr++;
  57. size = *(++sizes);
  58. }
  59. if (!nb_splits)
  60. break;
  61. }
  62. return (size || !splitters[0].in_sg0) ? -EINVAL : 0;
  63. }
  64. static void sg_split_phys(struct sg_splitter *splitters, const int nb_splits)
  65. {
  66. int i, j;
  67. struct scatterlist *in_sg, *out_sg;
  68. struct sg_splitter *split;
  69. for (i = 0, split = splitters; i < nb_splits; i++, split++) {
  70. in_sg = split->in_sg0;
  71. out_sg = split->out_sg;
  72. for (j = 0; j < split->nents; j++, out_sg++) {
  73. *out_sg = *in_sg;
  74. if (!j) {
  75. out_sg->offset += split->skip_sg0;
  76. out_sg->length -= split->skip_sg0;
  77. }
  78. sg_dma_address(out_sg) = 0;
  79. sg_dma_len(out_sg) = 0;
  80. in_sg = sg_next(in_sg);
  81. }
  82. out_sg[-1].length = split->length_last_sg;
  83. sg_mark_end(out_sg - 1);
  84. }
  85. }
  86. static void sg_split_mapped(struct sg_splitter *splitters, const int nb_splits)
  87. {
  88. int i, j;
  89. struct scatterlist *in_sg, *out_sg;
  90. struct sg_splitter *split;
  91. for (i = 0, split = splitters; i < nb_splits; i++, split++) {
  92. in_sg = split->in_sg0;
  93. out_sg = split->out_sg;
  94. for (j = 0; j < split->nents; j++, out_sg++) {
  95. sg_dma_address(out_sg) = sg_dma_address(in_sg);
  96. sg_dma_len(out_sg) = sg_dma_len(in_sg);
  97. if (!j) {
  98. sg_dma_address(out_sg) += split->skip_sg0;
  99. sg_dma_len(out_sg) -= split->skip_sg0;
  100. }
  101. in_sg = sg_next(in_sg);
  102. }
  103. sg_dma_len(--out_sg) = split->length_last_sg;
  104. }
  105. }
  106. /**
  107. * sg_split - split a scatterlist into several scatterlists
  108. * @in: the input sg list
  109. * @in_mapped_nents: the result of a dma_map_sg(in, ...), or 0 if not mapped.
  110. * @skip: the number of bytes to skip in the input sg list
  111. * @nb_splits: the number of desired sg outputs
  112. * @split_sizes: the respective size of each output sg list in bytes
  113. * @out: an array where to store the allocated output sg lists
  114. * @out_mapped_nents: the resulting sg lists mapped number of sg entries. Might
  115. * be NULL if sglist not already mapped (in_mapped_nents = 0)
  116. * @gfp_mask: the allocation flag
  117. *
  118. * This function splits the input sg list into nb_splits sg lists, which are
  119. * allocated and stored into out.
  120. * The @in is split into :
  121. * - @out[0], which covers bytes [@skip .. @skip + @split_sizes[0] - 1] of @in
  122. * - @out[1], which covers bytes [@skip + split_sizes[0] ..
  123. * @skip + @split_sizes[0] + @split_sizes[1] -1]
  124. * etc ...
  125. * It will be the caller's duty to kfree() out array members.
  126. *
  127. * Returns 0 upon success, or error code
  128. */
  129. int sg_split(struct scatterlist *in, const int in_mapped_nents,
  130. const off_t skip, const int nb_splits,
  131. const size_t *split_sizes,
  132. struct scatterlist **out, int *out_mapped_nents,
  133. gfp_t gfp_mask)
  134. {
  135. int i, ret;
  136. struct sg_splitter *splitters;
  137. splitters = kzalloc_objs(*splitters, nb_splits, gfp_mask);
  138. if (!splitters)
  139. return -ENOMEM;
  140. ret = sg_calculate_split(in, sg_nents(in), nb_splits, skip, split_sizes,
  141. splitters, false);
  142. if (ret < 0)
  143. goto err;
  144. ret = -ENOMEM;
  145. for (i = 0; i < nb_splits; i++) {
  146. splitters[i].out_sg = kmalloc_objs(struct scatterlist,
  147. splitters[i].nents, gfp_mask);
  148. if (!splitters[i].out_sg)
  149. goto err;
  150. }
  151. /*
  152. * The order of these 3 calls is important and should be kept.
  153. */
  154. sg_split_phys(splitters, nb_splits);
  155. if (in_mapped_nents) {
  156. ret = sg_calculate_split(in, in_mapped_nents, nb_splits, skip,
  157. split_sizes, splitters, true);
  158. if (ret < 0)
  159. goto err;
  160. sg_split_mapped(splitters, nb_splits);
  161. }
  162. for (i = 0; i < nb_splits; i++) {
  163. out[i] = splitters[i].out_sg;
  164. if (out_mapped_nents)
  165. out_mapped_nents[i] = splitters[i].nents;
  166. }
  167. kfree(splitters);
  168. return 0;
  169. err:
  170. for (i = 0; i < nb_splits; i++)
  171. kfree(splitters[i].out_sg);
  172. kfree(splitters);
  173. return ret;
  174. }
  175. EXPORT_SYMBOL(sg_split);