IB/ipath - Implement new verbs DMA mapping functions

This patch implements the interposing DMA mapping functions to allow
support for IOMMUs and remove the dependence on phys_to_virt().

From: Ralph Campbell <[EMAIL PROTECTED]>

diff -r f37bd0e41fec drivers/infiniband/hw/ipath/Makefile
--- a/drivers/infiniband/hw/ipath/Makefile      Thu Oct 26 21:44:41 2006 +0700
+++ b/drivers/infiniband/hw/ipath/Makefile      Thu Oct 26 11:16:16 2006 -0800
@@ -6,6 +6,7 @@ ib_ipath-y := \
 ib_ipath-y := \
        ipath_cq.o \
        ipath_diag.o \
+       ipath_dma.o \
        ipath_driver.o \
        ipath_eeprom.o \
        ipath_file_ops.o \
diff -r f37bd0e41fec drivers/infiniband/hw/ipath/ipath_keys.c
--- a/drivers/infiniband/hw/ipath/ipath_keys.c  Thu Oct 26 21:44:41 2006 +0700
+++ b/drivers/infiniband/hw/ipath/ipath_keys.c  Fri Oct 27 16:22:43 2006 -0800
@@ -134,7 +134,7 @@ int ipath_lkey_ok(struct ipath_qp *qp, s
         */
        if (sge->lkey == 0) {
                isge->mr = NULL;
-               isge->vaddr = bus_to_virt(sge->addr);
+               isge->vaddr = (void *) sge->addr;
                isge->length = sge->length;
                isge->sge_length = sge->length;
                ret = 1;
@@ -202,12 +202,12 @@ int ipath_rkey_ok(struct ipath_qp *qp, s
        int ret;
 
        /*
-        * We use RKEY == zero for physical addresses
-        * (see ipath_get_dma_mr).
+        * We use RKEY == zero for kernel virtual addresses
+        * (see ipath_get_dma_mr and ipath_dma.c).
         */
        if (rkey == 0) {
                sge->mr = NULL;
-               sge->vaddr = phys_to_virt(vaddr);
+               sge->vaddr = (void *) vaddr;
                sge->length = len;
                sge->sge_length = len;
                ss->sg_list = NULL;
diff -r f37bd0e41fec drivers/infiniband/hw/ipath/ipath_mr.c
--- a/drivers/infiniband/hw/ipath/ipath_mr.c    Thu Oct 26 21:44:41 2006 +0700
+++ b/drivers/infiniband/hw/ipath/ipath_mr.c    Thu Oct 26 13:35:12 2006 -0800
@@ -54,6 +54,8 @@ static inline struct ipath_fmr *to_ifmr(
  * @acc: access flags
  *
  * Returns the memory region on success, otherwise returns an errno.
+ * Note that all DMA addresses should be created via the
+ * struct ib_dma_mapping_ops functions (see ipath_dma.c).
  */
 struct ib_mr *ipath_get_dma_mr(struct ib_pd *pd, int acc)
 {
@@ -149,8 +151,7 @@ struct ib_mr *ipath_reg_phys_mr(struct i
        m = 0;
        n = 0;
        for (i = 0; i < num_phys_buf; i++) {
-               mr->mr.map[m]->segs[n].vaddr =
-                       phys_to_virt(buffer_list[i].addr);
+               mr->mr.map[m]->segs[n].vaddr = (void *) buffer_list[i].addr;
                mr->mr.map[m]->segs[n].length = buffer_list[i].size;
                mr->mr.length += buffer_list[i].size;
                n++;
@@ -347,7 +348,7 @@ int ipath_map_phys_fmr(struct ib_fmr *ib
        n = 0;
        ps = 1 << fmr->page_shift;
        for (i = 0; i < list_len; i++) {
-               fmr->mr.map[m]->segs[n].vaddr = phys_to_virt(page_list[i]);
+               fmr->mr.map[m]->segs[n].vaddr = (void *) page_list[i];
                fmr->mr.map[m]->segs[n].length = ps;
                if (++n == IPATH_SEGSZ) {
                        m++;
diff -r f37bd0e41fec drivers/infiniband/hw/ipath/ipath_verbs.c
--- a/drivers/infiniband/hw/ipath/ipath_verbs.c Thu Oct 26 21:44:41 2006 +0700
+++ b/drivers/infiniband/hw/ipath/ipath_verbs.c Thu Oct 26 11:17:23 2006 -0800
@@ -1599,6 +1599,7 @@ int ipath_register_ib_device(struct ipat
        dev->detach_mcast = ipath_multicast_detach;
        dev->process_mad = ipath_process_mad;
        dev->mmap = ipath_mmap;
+       dev->dma_ops = &ipath_dma_mapping_ops;
 
        snprintf(dev->node_desc, sizeof(dev->node_desc),
                 IPATH_IDSTR " %s", init_utsname()->nodename);
diff -r f37bd0e41fec drivers/infiniband/hw/ipath/ipath_verbs.h
--- a/drivers/infiniband/hw/ipath/ipath_verbs.h Thu Oct 26 21:44:41 2006 +0700
+++ b/drivers/infiniband/hw/ipath/ipath_verbs.h Thu Oct 26 11:17:38 2006 -0800
@@ -812,4 +812,6 @@ extern unsigned int ib_ipath_max_srq_wrs
 
 extern const u32 ib_ipath_rnr_table[];
 
+extern struct ib_dma_mapping_ops ipath_dma_mapping_ops;
+
 #endif                         /* IPATH_VERBS_H */
diff -r f37bd0e41fec drivers/infiniband/hw/ipath/ipath_dma.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/drivers/infiniband/hw/ipath/ipath_dma.c   Fri Oct 27 10:40:03 2006 -0800
@@ -0,0 +1,229 @@
+/*
+ * Copyright (c) 2006 QLogic, Inc. All rights reserved.
+ *
+ * This software is available to you under a choice of one of two
+ * licenses.  You may choose to be licensed under the terms of the GNU
+ * General Public License (GPL) Version 2, available from the file
+ * COPYING in the main directory of this source tree, or the
+ * OpenIB.org BSD license below:
+ *
+ *     Redistribution and use in source and binary forms, with or
+ *     without modification, are permitted provided that the following
+ *     conditions are met:
+ *
+ *      - Redistributions of source code must retain the above
+ *        copyright notice, this list of conditions and the following
+ *        disclaimer.
+ *
+ *      - Redistributions in binary form must reproduce the above
+ *        copyright notice, this list of conditions and the following
+ *        disclaimer in the documentation and/or other materials
+ *        provided with the distribution.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <rdma/ib_verbs.h>
+
+#include "ipath_verbs.h"
+
+#define BAD_DMA_ADDRESS ((dma_addr_t) 0)
+
+/**
+ * ipath_dma_mapping_error - check a dma_addr_t for error
+ * @device: The device for which the dma_addr was created
+ * @dma_addr: The DMA address to check
+ */
+static int ipath_mapping_error(struct ib_device *dev, dma_addr_t dma_addr)
+{
+       return dma_addr == BAD_DMA_ADDRESS;
+}
+
+/**
+ * ipath_dma_map_single - Map a kernel virtual address to DMA address
+ * @device: The device for which the dma_addr is to be created
+ * @cpu_addr: The kernel virtual address
+ * @size: The size of the region in bytes
+ * @direction: The direction of the DMA
+ */
+static dma_addr_t ipath_dma_map_single(struct ib_device *dev,
+                                      void *cpu_addr, size_t size,
+                                      enum dma_data_direction direction)
+{
+       BUG_ON(direction == DMA_NONE);
+       return (dma_addr_t) cpu_addr;
+}
+
+/**
+ * ipath_dma_unmap_single - Destroy a mapping created by ipath_dma_map_single()
+ * @device: The device for which the DMA address was created
+ * @addr: The DMA address
+ * @size: The size of the region in bytes
+ * @direction: The direction of the DMA
+ */
+static void ipath_dma_unmap_single(struct ib_device *dev,
+                              dma_addr_t addr, size_t size,
+                              enum dma_data_direction direction)
+{
+       BUG_ON(direction == DMA_NONE);
+}
+
+/**
+ * ipath_dma_map_page - Map a physical page to DMA address
+ * @device: The device for which the dma_addr is to be created
+ * @page: The page to be mapped
+ * @offset: The offset within the page
+ * @size: The size of the region in bytes
+ * @direction: The direction of the DMA
+ */
+static dma_addr_t ipath_dma_map_page(struct ib_device *dev,
+                                    struct page *page,
+                                    unsigned long offset,
+                                    size_t size,
+                                    enum dma_data_direction direction)
+{
+       dma_addr_t addr;
+
+       BUG_ON(direction == DMA_NONE);
+
+       if (offset + size > PAGE_SIZE) {
+               addr = BAD_DMA_ADDRESS;
+               goto done;
+       }
+
+       addr = (dma_addr_t) page_address(page);
+       if (addr)
+               addr += offset;
+       /* TODO: handle highmem pages */
+
+done:
+       return addr;
+}
+
+/**
+ * ipath_dma_unmap_page - Destroy a mapping created by ipath_dma_map_page()
+ * @device: The device for which the DMA address was created
+ * @addr: The DMA address
+ * @size: The size of the region in bytes
+ * @direction: The direction of the DMA
+ */
+static void ipath_dma_unmap_page(struct ib_device *dev,
+                                dma_addr_t addr, size_t size,
+                                enum dma_data_direction direction)
+{
+       BUG_ON(direction == DMA_NONE);
+}
+
+/**
+ * ipath_map_sg - Map a scatter/gather list to DMA addresses
+ * @device: The device for which the DMA addresses are to be created
+ * @sg: The array of scatter/gather entries
+ * @nents: The number of scatter/gather entries
+ * @direction: The direction of the DMA
+ */
+int ipath_map_sg(struct ib_device *dev, struct scatterlist *sg, int nents,
+                enum dma_data_direction direction)
+{
+       dma_addr_t addr;
+       int i;
+       int ret = nents;
+
+       BUG_ON(direction == DMA_NONE);
+
+       for (i = 0; i < nents; i++) {
+               addr = (dma_addr_t) page_address(sg[i].page);
+               /* TODO: handle highmem pages */
+               if (!addr) {
+                       ret = 0;
+                       break;
+               }
+       }
+       return ret;
+}
+
+/**
+ * ipath_unmap_sg - Unmap a scatter/gather list of DMA addresses
+ * @device: The device for which the DMA addresses were created
+ * @sg: The array of scatter/gather entries
+ * @nents: The number of scatter/gather entries
+ * @direction: The direction of the DMA
+ */
+static void ipath_unmap_sg(struct ib_device *dev,
+                          struct scatterlist *sg, int nents,
+                          enum dma_data_direction direction)
+{
+       BUG_ON(direction == DMA_NONE);
+}
+
+/**
+ * ipath_sg_dma_address - Return the DMA address from a scatter/gather entry
+ * @device: The device for which the DMA addresses were created
+ * @sg: The scatter/gather entry
+ */
+static dma_addr_t ipath_sg_dma_address(struct ib_device *dev,
+                                      struct scatterlist *sg)
+{
+       return (dma_addr_t) page_address(sg->page);
+}
+
+/**
+ * ipath_sg_dma_len - Return the DMA length from a scatter/gather entry
+ * @device: The device for which the DMA addresses were created
+ * @sg: The scatter/gather entry
+ */
+static unsigned int ipath_sg_dma_len(struct ib_device *dev,
+                                    struct scatterlist *sg)
+{
+       return sg->length;
+}
+
+/**
+ * ipath_sync_single_for_cpu - Prepare DMA region to be accessed by CPU
+ * @device: The device for which the DMA address was created
+ * @addr: The DMA address
+ * @size: The size of the region in bytes
+ * @dir: The direction of the DMA
+ */
+static void ipath_sync_single_for_cpu(struct ib_device *dev,
+                                     dma_addr_t addr,
+                                     size_t size,
+                                     enum dma_data_direction dir)
+{
+       dma_sync_single_for_cpu(dev->dma_device, addr, size, dir);
+}
+
+/**
+ * ipath_sync_single_for_device - Prepare DMA region to be accessed by device
+ * @device: The device for which the DMA address was created
+ * @addr: The DMA address
+ * @size: The size of the region in bytes
+ * @dir: The direction of the DMA
+ */
+static void ipath_sync_single_for_device(struct ib_device *dev,
+                                        dma_addr_t addr,
+                                        size_t size,
+                                        enum dma_data_direction dir)
+{
+       dma_sync_single_for_device(dev->dma_device, addr, size, dir);
+}
+
+struct ib_dma_mapping_ops ipath_dma_mapping_ops = {
+       ipath_mapping_error,
+       ipath_dma_map_single,
+       ipath_dma_unmap_single,
+       ipath_dma_map_page,
+       ipath_dma_unmap_page,
+       ipath_map_sg,
+       ipath_unmap_sg,
+       ipath_sg_dma_address,
+       ipath_sg_dma_len,
+       ipath_sync_single_for_cpu,
+       ipath_sync_single_for_device
+};



_______________________________________________
openib-general mailing list
[email protected]
http://openib.org/mailman/listinfo/openib-general

To unsubscribe, please visit http://openib.org/mailman/listinfo/openib-general

Reply via email to