Skip to content

Commit

Permalink
scatterlist: introduce sg_nents_for_len
Browse files Browse the repository at this point in the history
When performing a dma_map_sg() call, the number of sg entries to map is
required. Using sg_nents to retrieve the number of sg entries will
return the total number of entries in the sg list up to the entry marked
as the end. If there happen to be unused entries in the list, these will
still be counted. Some dma_map_sg() implementations will not handle the
unused entries correctly (lib/swiotlb.c) and execute a BUG_ON.

The sg_nents_for_len() function will traverse the sg list and return the
number of entries required to satisfy the supplied length argument. This
can then be supplied to the dma_map_sg() call to successfully map the
sg.

Signed-off-by: Tom Lendacky <[email protected]>
Signed-off-by: Herbert Xu <[email protected]>
  • Loading branch information
tlendacky authored and herbertx committed Jun 3, 2015
1 parent 1605440 commit cfaed10
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/linux/scatterlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ static inline void *sg_virt(struct scatterlist *sg)
}

int sg_nents(struct scatterlist *sg);
int sg_nents_for_len(struct scatterlist *sg, u64 len);
struct scatterlist *sg_next(struct scatterlist *);
struct scatterlist *sg_last(struct scatterlist *s, unsigned int);
void sg_init_table(struct scatterlist *, unsigned int);
Expand Down
32 changes: 32 additions & 0 deletions lib/scatterlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,38 @@ int sg_nents(struct scatterlist *sg)
}
EXPORT_SYMBOL(sg_nents);

/**
* sg_nents_for_len - return total count of entries in scatterlist
* needed to satisfy the supplied length
* @sg: The scatterlist
* @len: The total required length
*
* Description:
* Determines the number of entries in sg that are required to meet
* the supplied length, taking into acount chaining as well
*
* Returns:
* the number of sg entries needed, negative error on failure
*
**/
int sg_nents_for_len(struct scatterlist *sg, u64 len)
{
int nents;
u64 total;

if (!len)
return 0;

for (nents = 0, total = 0; sg; sg = sg_next(sg)) {
nents++;
total += sg->length;
if (total >= len)
return nents;
}

return -EINVAL;
}
EXPORT_SYMBOL(sg_nents_for_len);

/**
* sg_last - return the last scatterlist entry in a list
Expand Down

0 comments on commit cfaed10

Please sign in to comment.