2 * Dynamic DMA mapping support. Common code
5 #include <linux/types.h>
7 #include <linux/string.h>
11 dma_addr_t bad_dma_address = -1UL;
13 /* Map a set of buffers described by scatterlist in streaming
14 * mode for DMA. This is the scather-gather version of the
15 * above pci_map_single interface. Here the scatter gather list
16 * elements are each tagged with the appropriate dma address
17 * and length. They are obtained via sg_dma_{address,length}(SG).
19 * NOTE: An implementation may be able to use a smaller number of
20 * DMA address/length pairs than there are SG table elements.
21 * (for example via virtual mapping capabilities)
22 * The routine returns the number of addr/length pairs actually
23 * used, at most nents.
25 * Device ownership issues as mentioned above for pci_map_single are
28 int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sg,
29 int nents, int direction)
33 BUG_ON(direction == PCI_DMA_NONE);
38 for (i = 0; i < nents; i++ ) {
39 struct scatterlist *s = &sg[i];
40 void *addr = s->address;
42 BUG_ON(s->page || s->offset);
44 addr = page_address(s->page) + s->offset;
47 s->dma_address = pci_map_single(hwdev, addr, s->length, direction);
48 if (unlikely(s->dma_address == bad_dma_address))
54 pci_unmap_sg(hwdev, sg, i, direction);
58 /* Unmap a set of streaming mode DMA translations.
59 * Again, cpu read rules concerning calls here are the same as for
60 * pci_unmap_single() above.
62 void pci_unmap_sg(struct pci_dev *dev, struct scatterlist *sg,
66 for (i = 0; i < nents; i++) {
67 struct scatterlist *s = &sg[i];
68 BUG_ON(s->address == NULL && s->page == NULL);
69 BUG_ON(s->dma_address == 0);
70 pci_unmap_single(dev, s->dma_address, s->length, dir);