Back to home page

LXR

 
 

    


0001 /*
0002  * Copyright (C) 2015 Robert Jarzmik <robert.jarzmik@free.fr>
0003  *
0004  * Scatterlist splitting helpers.
0005  *
0006  * This source code is licensed under the GNU General Public License,
0007  * Version 2. See the file COPYING for more details.
0008  */
0009 
0010 #include <linux/scatterlist.h>
0011 #include <linux/slab.h>
0012 
0013 struct sg_splitter {
0014     struct scatterlist *in_sg0;
0015     int nents;
0016     off_t skip_sg0;
0017     unsigned int length_last_sg;
0018 
0019     struct scatterlist *out_sg;
0020 };
0021 
0022 static int sg_calculate_split(struct scatterlist *in, int nents, int nb_splits,
0023                   off_t skip, const size_t *sizes,
0024                   struct sg_splitter *splitters, bool mapped)
0025 {
0026     int i;
0027     unsigned int sglen;
0028     size_t size = sizes[0], len;
0029     struct sg_splitter *curr = splitters;
0030     struct scatterlist *sg;
0031 
0032     for (i = 0; i < nb_splits; i++) {
0033         splitters[i].in_sg0 = NULL;
0034         splitters[i].nents = 0;
0035     }
0036 
0037     for_each_sg(in, sg, nents, i) {
0038         sglen = mapped ? sg_dma_len(sg) : sg->length;
0039         if (skip > sglen) {
0040             skip -= sglen;
0041             continue;
0042         }
0043 
0044         len = min_t(size_t, size, sglen - skip);
0045         if (!curr->in_sg0) {
0046             curr->in_sg0 = sg;
0047             curr->skip_sg0 = skip;
0048         }
0049         size -= len;
0050         curr->nents++;
0051         curr->length_last_sg = len;
0052 
0053         while (!size && (skip + len < sglen) && (--nb_splits > 0)) {
0054             curr++;
0055             size = *(++sizes);
0056             skip += len;
0057             len = min_t(size_t, size, sglen - skip);
0058 
0059             curr->in_sg0 = sg;
0060             curr->skip_sg0 = skip;
0061             curr->nents = 1;
0062             curr->length_last_sg = len;
0063             size -= len;
0064         }
0065         skip = 0;
0066 
0067         if (!size && --nb_splits > 0) {
0068             curr++;
0069             size = *(++sizes);
0070         }
0071 
0072         if (!nb_splits)
0073             break;
0074     }
0075 
0076     return (size || !splitters[0].in_sg0) ? -EINVAL : 0;
0077 }
0078 
0079 static void sg_split_phys(struct sg_splitter *splitters, const int nb_splits)
0080 {
0081     int i, j;
0082     struct scatterlist *in_sg, *out_sg;
0083     struct sg_splitter *split;
0084 
0085     for (i = 0, split = splitters; i < nb_splits; i++, split++) {
0086         in_sg = split->in_sg0;
0087         out_sg = split->out_sg;
0088         for (j = 0; j < split->nents; j++, out_sg++) {
0089             *out_sg = *in_sg;
0090             if (!j) {
0091                 out_sg->offset += split->skip_sg0;
0092                 out_sg->length -= split->skip_sg0;
0093             } else {
0094                 out_sg->offset = 0;
0095             }
0096             sg_dma_address(out_sg) = 0;
0097             sg_dma_len(out_sg) = 0;
0098             in_sg = sg_next(in_sg);
0099         }
0100         out_sg[-1].length = split->length_last_sg;
0101         sg_mark_end(out_sg - 1);
0102     }
0103 }
0104 
0105 static void sg_split_mapped(struct sg_splitter *splitters, const int nb_splits)
0106 {
0107     int i, j;
0108     struct scatterlist *in_sg, *out_sg;
0109     struct sg_splitter *split;
0110 
0111     for (i = 0, split = splitters; i < nb_splits; i++, split++) {
0112         in_sg = split->in_sg0;
0113         out_sg = split->out_sg;
0114         for (j = 0; j < split->nents; j++, out_sg++) {
0115             sg_dma_address(out_sg) = sg_dma_address(in_sg);
0116             sg_dma_len(out_sg) = sg_dma_len(in_sg);
0117             if (!j) {
0118                 sg_dma_address(out_sg) += split->skip_sg0;
0119                 sg_dma_len(out_sg) -= split->skip_sg0;
0120             }
0121             in_sg = sg_next(in_sg);
0122         }
0123         sg_dma_len(--out_sg) = split->length_last_sg;
0124     }
0125 }
0126 
0127 /**
0128  * sg_split - split a scatterlist into several scatterlists
0129  * @in: the input sg list
0130  * @in_mapped_nents: the result of a dma_map_sg(in, ...), or 0 if not mapped.
0131  * @skip: the number of bytes to skip in the input sg list
0132  * @nb_splits: the number of desired sg outputs
0133  * @split_sizes: the respective size of each output sg list in bytes
0134  * @out: an array where to store the allocated output sg lists
0135  * @out_mapped_nents: the resulting sg lists mapped number of sg entries. Might
0136  *                    be NULL if sglist not already mapped (in_mapped_nents = 0)
0137  * @gfp_mask: the allocation flag
0138  *
0139  * This function splits the input sg list into nb_splits sg lists, which are
0140  * allocated and stored into out.
0141  * The @in is split into :
0142  *  - @out[0], which covers bytes [@skip .. @skip + @split_sizes[0] - 1] of @in
0143  *  - @out[1], which covers bytes [@skip + split_sizes[0] ..
0144  *                                 @skip + @split_sizes[0] + @split_sizes[1] -1]
0145  * etc ...
0146  * It will be the caller's duty to kfree() out array members.
0147  *
0148  * Returns 0 upon success, or error code
0149  */
0150 int sg_split(struct scatterlist *in, const int in_mapped_nents,
0151          const off_t skip, const int nb_splits,
0152          const size_t *split_sizes,
0153          struct scatterlist **out, int *out_mapped_nents,
0154          gfp_t gfp_mask)
0155 {
0156     int i, ret;
0157     struct sg_splitter *splitters;
0158 
0159     splitters = kcalloc(nb_splits, sizeof(*splitters), gfp_mask);
0160     if (!splitters)
0161         return -ENOMEM;
0162 
0163     ret = sg_calculate_split(in, sg_nents(in), nb_splits, skip, split_sizes,
0164                splitters, false);
0165     if (ret < 0)
0166         goto err;
0167 
0168     ret = -ENOMEM;
0169     for (i = 0; i < nb_splits; i++) {
0170         splitters[i].out_sg = kmalloc_array(splitters[i].nents,
0171                             sizeof(struct scatterlist),
0172                             gfp_mask);
0173         if (!splitters[i].out_sg)
0174             goto err;
0175     }
0176 
0177     /*
0178      * The order of these 3 calls is important and should be kept.
0179      */
0180     sg_split_phys(splitters, nb_splits);
0181     ret = sg_calculate_split(in, in_mapped_nents, nb_splits, skip,
0182                  split_sizes, splitters, true);
0183     if (ret < 0)
0184         goto err;
0185     sg_split_mapped(splitters, nb_splits);
0186 
0187     for (i = 0; i < nb_splits; i++) {
0188         out[i] = splitters[i].out_sg;
0189         if (out_mapped_nents)
0190             out_mapped_nents[i] = splitters[i].nents;
0191     }
0192 
0193     kfree(splitters);
0194     return 0;
0195 
0196 err:
0197     for (i = 0; i < nb_splits; i++)
0198         kfree(splitters[i].out_sg);
0199     kfree(splitters);
0200     return ret;
0201 }
0202 EXPORT_SYMBOL(sg_split);