Merge git://git.linux-nfs.org/pub/linux/nfs-2.6
[pandora-kernel.git] / lib / swiotlb.c
index 10c13ad..1a8050a 100644 (file)
@@ -35,7 +35,7 @@
 #define OFFSET(val,align) ((unsigned long)     \
                           ( (val) & ( (align) - 1)))
 
-#define SG_ENT_VIRT_ADDRESS(sg)        (page_address((sg)->page) + (sg)->offset)
+#define SG_ENT_VIRT_ADDRESS(sg)        (sg_virt((sg)))
 #define SG_ENT_PHYS_ADDRESS(sg)        virt_to_bus(SG_ENT_VIRT_ADDRESS(sg))
 
 /*
@@ -357,7 +357,8 @@ map_single(struct device *hwdev, char *buffer, size_t size, int dir)
         * This is needed when we sync the memory.  Then we sync the buffer if
         * needed.
         */
-       io_tlb_orig_addr[index] = buffer;
+       for (i = 0; i < nslots; i++)
+               io_tlb_orig_addr[index+i] = buffer + (i << IO_TLB_SHIFT);
        if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
                memcpy(dma_addr, buffer, size);
 
@@ -418,6 +419,8 @@ sync_single(struct device *hwdev, char *dma_addr, size_t size,
        int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
        char *buffer = io_tlb_orig_addr[index];
 
+       buffer += ((unsigned long)dma_addr & ((1 << IO_TLB_SHIFT) - 1));
+
        switch (target) {
        case SYNC_FOR_CPU:
                if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
@@ -494,6 +497,7 @@ void
 swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
                      dma_addr_t dma_handle)
 {
+       WARN_ON(irqs_disabled());
        if (!(vaddr >= (void *)io_tlb_start
                     && vaddr < (void *)io_tlb_end))
                free_pages((unsigned long) vaddr, get_order(size));
@@ -673,16 +677,17 @@ swiotlb_sync_single_range_for_device(struct device *hwdev, dma_addr_t dev_addr,
  * same here.
  */
 int
-swiotlb_map_sg(struct device *hwdev, struct scatterlist *sg, int nelems,
+swiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
               int dir)
 {
+       struct scatterlist *sg;
        void *addr;
        dma_addr_t dev_addr;
        int i;
 
        BUG_ON(dir == DMA_NONE);
 
-       for (i = 0; i < nelems; i++, sg++) {
+       for_each_sg(sgl, sg, nelems, i) {
                addr = SG_ENT_VIRT_ADDRESS(sg);
                dev_addr = virt_to_bus(addr);
                if (swiotlb_force || address_needs_mapping(hwdev, dev_addr)) {
@@ -691,8 +696,8 @@ swiotlb_map_sg(struct device *hwdev, struct scatterlist *sg, int nelems,
                                /* Don't panic here, we expect map_sg users
                                   to do proper error handling. */
                                swiotlb_full(hwdev, sg->length, dir, 0);
-                               swiotlb_unmap_sg(hwdev, sg - i, i, dir);
-                               sg[0].dma_length = 0;
+                               swiotlb_unmap_sg(hwdev, sgl, i, dir);
+                               sgl[0].dma_length = 0;
                                return 0;
                        }
                        sg->dma_address = virt_to_bus(map);
@@ -708,19 +713,21 @@ swiotlb_map_sg(struct device *hwdev, struct scatterlist *sg, int nelems,
  * concerning calls here are the same as for swiotlb_unmap_single() above.
  */
 void
-swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nelems,
+swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
                 int dir)
 {
+       struct scatterlist *sg;
        int i;
 
        BUG_ON(dir == DMA_NONE);
 
-       for (i = 0; i < nelems; i++, sg++)
+       for_each_sg(sgl, sg, nelems, i) {
                if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
                        unmap_single(hwdev, bus_to_virt(sg->dma_address),
                                     sg->dma_length, dir);
                else if (dir == DMA_FROM_DEVICE)
                        dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
+       }
 }
 
 /*
@@ -731,19 +738,21 @@ swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nelems,
  * and usage.
  */
 static void
-swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sg,
+swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl,
                int nelems, int dir, int target)
 {
+       struct scatterlist *sg;
        int i;
 
        BUG_ON(dir == DMA_NONE);
 
-       for (i = 0; i < nelems; i++, sg++)
+       for_each_sg(sgl, sg, nelems, i) {
                if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
                        sync_single(hwdev, bus_to_virt(sg->dma_address),
                                    sg->dma_length, dir, target);
                else if (dir == DMA_FROM_DEVICE)
                        dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
+       }
 }
 
 void