Merge branch 'upstream-jeff' of git://git.kernel.org/pub/scm/linux/kernel/git/romieu...
[linux-2.6] / drivers / infiniband / hw / ipath / ipath_dma.c
1 /*
2  * Copyright (c) 2006 QLogic, Corporation. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include <linux/scatterlist.h>
34 #include <rdma/ib_verbs.h>
35
36 #include "ipath_verbs.h"
37
38 #define BAD_DMA_ADDRESS ((u64) 0)
39
40 /*
41  * The following functions implement driver specific replacements
42  * for the ib_dma_*() functions.
43  *
44  * These functions return kernel virtual addresses instead of
45  * device bus addresses since the driver uses the CPU to copy
46  * data instead of using hardware DMA.
47  */
48
49 static int ipath_mapping_error(struct ib_device *dev, u64 dma_addr)
50 {
51         return dma_addr == BAD_DMA_ADDRESS;
52 }
53
54 static u64 ipath_dma_map_single(struct ib_device *dev,
55                                 void *cpu_addr, size_t size,
56                                 enum dma_data_direction direction)
57 {
58         BUG_ON(!valid_dma_direction(direction));
59         return (u64) cpu_addr;
60 }
61
62 static void ipath_dma_unmap_single(struct ib_device *dev,
63                                    u64 addr, size_t size,
64                                    enum dma_data_direction direction)
65 {
66         BUG_ON(!valid_dma_direction(direction));
67 }
68
69 static u64 ipath_dma_map_page(struct ib_device *dev,
70                               struct page *page,
71                               unsigned long offset,
72                               size_t size,
73                               enum dma_data_direction direction)
74 {
75         u64 addr;
76
77         BUG_ON(!valid_dma_direction(direction));
78
79         if (offset + size > PAGE_SIZE) {
80                 addr = BAD_DMA_ADDRESS;
81                 goto done;
82         }
83
84         addr = (u64) page_address(page);
85         if (addr)
86                 addr += offset;
87         /* TODO: handle highmem pages */
88
89 done:
90         return addr;
91 }
92
93 static void ipath_dma_unmap_page(struct ib_device *dev,
94                                  u64 addr, size_t size,
95                                  enum dma_data_direction direction)
96 {
97         BUG_ON(!valid_dma_direction(direction));
98 }
99
100 static int ipath_map_sg(struct ib_device *dev, struct scatterlist *sgl,
101                         int nents, enum dma_data_direction direction)
102 {
103         struct scatterlist *sg;
104         u64 addr;
105         int i;
106         int ret = nents;
107
108         BUG_ON(!valid_dma_direction(direction));
109
110         for_each_sg(sgl, sg, nents, i) {
111                 addr = (u64) page_address(sg_page(sg));
112                 /* TODO: handle highmem pages */
113                 if (!addr) {
114                         ret = 0;
115                         break;
116                 }
117         }
118         return ret;
119 }
120
121 static void ipath_unmap_sg(struct ib_device *dev,
122                            struct scatterlist *sg, int nents,
123                            enum dma_data_direction direction)
124 {
125         BUG_ON(!valid_dma_direction(direction));
126 }
127
128 static u64 ipath_sg_dma_address(struct ib_device *dev, struct scatterlist *sg)
129 {
130         u64 addr = (u64) page_address(sg_page(sg));
131
132         if (addr)
133                 addr += sg->offset;
134         return addr;
135 }
136
137 static unsigned int ipath_sg_dma_len(struct ib_device *dev,
138                                      struct scatterlist *sg)
139 {
140         return sg->length;
141 }
142
143 static void ipath_sync_single_for_cpu(struct ib_device *dev,
144                                       u64 addr,
145                                       size_t size,
146                                       enum dma_data_direction dir)
147 {
148 }
149
150 static void ipath_sync_single_for_device(struct ib_device *dev,
151                                          u64 addr,
152                                          size_t size,
153                                          enum dma_data_direction dir)
154 {
155 }
156
157 static void *ipath_dma_alloc_coherent(struct ib_device *dev, size_t size,
158                                       u64 *dma_handle, gfp_t flag)
159 {
160         struct page *p;
161         void *addr = NULL;
162
163         p = alloc_pages(flag, get_order(size));
164         if (p)
165                 addr = page_address(p);
166         if (dma_handle)
167                 *dma_handle = (u64) addr;
168         return addr;
169 }
170
171 static void ipath_dma_free_coherent(struct ib_device *dev, size_t size,
172                                     void *cpu_addr, u64 dma_handle)
173 {
174         free_pages((unsigned long) cpu_addr, get_order(size));
175 }
176
177 struct ib_dma_mapping_ops ipath_dma_mapping_ops = {
178         ipath_mapping_error,
179         ipath_dma_map_single,
180         ipath_dma_unmap_single,
181         ipath_dma_map_page,
182         ipath_dma_unmap_page,
183         ipath_map_sg,
184         ipath_unmap_sg,
185         ipath_sg_dma_address,
186         ipath_sg_dma_len,
187         ipath_sync_single_for_cpu,
188         ipath_sync_single_for_device,
189         ipath_dma_alloc_coherent,
190         ipath_dma_free_coherent
191 };