1 /*
2 * Dynamic DMA mapping support. Common code
3 */
4
5 #include <linux/types.h>
6 #include <linux/mm.h>
7 #include <linux/string.h>
8 #include <linux/pci.h>
9 #include <asm/io.h>
10
11 dma_addr_t bad_dma_address = -1UL;
12
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).
18 *
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.
24 *
25 * Device ownership issues as mentioned above for pci_map_single are
26 * the same here.
27 */
pci_map_sg(struct pci_dev * hwdev,struct scatterlist * sg,int nents,int direction)28 int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sg,
29 int nents, int direction)
30 {
31 int i;
32
33 BUG_ON(direction == PCI_DMA_NONE);
34
35 /*
36 * temporary 2.4 hack
37 */
38 for (i = 0; i < nents; i++ ) {
39 struct scatterlist *s = &sg[i];
40 void *addr = s->address;
41 if (addr)
42 BUG_ON(s->page || s->offset);
43 else if (s->page)
44 addr = page_address(s->page) + s->offset;
45 else
46 BUG();
47 s->dma_address = pci_map_single(hwdev, addr, s->length, direction);
48 if (unlikely(s->dma_address == bad_dma_address))
49 goto error;
50 }
51 return nents;
52
53 error:
54 pci_unmap_sg(hwdev, sg, i, direction);
55 return 0;
56 }
57
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.
61 */
pci_unmap_sg(struct pci_dev * dev,struct scatterlist * sg,int nents,int dir)62 void pci_unmap_sg(struct pci_dev *dev, struct scatterlist *sg,
63 int nents, int dir)
64 {
65 int i;
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);
71 }
72 }
73