sg_split.c 5.1 KB

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