pci-dma.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:2k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Dynamic DMA mapping support. Common code
  3.  */
  4. #include <linux/types.h>
  5. #include <linux/mm.h>
  6. #include <linux/string.h>
  7. #include <linux/pci.h>
  8. #include <asm/io.h>
  9. dma_addr_t bad_dma_address = -1UL; 
  10. /* Map a set of buffers described by scatterlist in streaming
  11.  * mode for DMA.  This is the scather-gather version of the
  12.  * above pci_map_single interface.  Here the scatter gather list
  13.  * elements are each tagged with the appropriate dma address
  14.  * and length.  They are obtained via sg_dma_{address,length}(SG).
  15.  *
  16.  * NOTE: An implementation may be able to use a smaller number of
  17.  *       DMA address/length pairs than there are SG table elements.
  18.  *       (for example via virtual mapping capabilities)
  19.  *       The routine returns the number of addr/length pairs actually
  20.  *       used, at most nents.
  21.  *
  22.  * Device ownership issues as mentioned above for pci_map_single are
  23.  * the same here.
  24.  */
  25. int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sg,
  26.      int nents, int direction)
  27. {
  28. int i;
  29. BUG_ON(direction == PCI_DMA_NONE);
  30.  
  31.   /*
  32.    * temporary 2.4 hack
  33.    */
  34.   for (i = 0; i < nents; i++ ) {
  35. struct scatterlist *s = &sg[i];
  36.   if (s->address) {
  37. BUG_ON(s->page || s->offset); 
  38.   s->dma_address = pci_map_single(hwdev, s->address, s->length, 
  39. direction); 
  40.   } else if (s->page) { 
  41. s->dma_address = pci_map_page(hwdev, s->page, s->offset,
  42.       s->length, direction); 
  43. } else
  44. BUG(); 
  45. if (unlikely(s->dma_address == bad_dma_address))
  46. goto error; 
  47.   }
  48. return nents;
  49.  error: 
  50. pci_unmap_sg(hwdev, sg, i, direction); 
  51. return 0; 
  52. }
  53. /* Unmap a set of streaming mode DMA translations.
  54.  * Again, cpu read rules concerning calls here are the same as for
  55.  * pci_unmap_single() above.
  56.  */
  57. void pci_unmap_sg(struct pci_dev *dev, struct scatterlist *sg, 
  58.   int nents, int dir)
  59. {
  60. int i;
  61. for (i = 0; i < nents; i++) { 
  62. struct scatterlist *s = &sg[i];
  63. BUG_ON(s->address == NULL && s->page == NULL); 
  64. BUG_ON(s->dma_address == 0); 
  65. pci_unmap_single(dev, s->dma_address, s->length, dir); 
  66. }