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 <linux/gfp.h>
35 #include <rdma/ib_verbs.h>
36 
37 #include "ipath_verbs.h"
38 
39 #define BAD_DMA_ADDRESS ((u64) 0)
40 
41 /*
42  * The following functions implement driver specific replacements
43  * for the ib_dma_*() functions.
44  *
45  * These functions return kernel virtual addresses instead of
46  * device bus addresses since the driver uses the CPU to copy
47  * data instead of using hardware DMA.
48  */
49 
ipath_mapping_error(struct ib_device * dev,u64 dma_addr)50 static int ipath_mapping_error(struct ib_device *dev, u64 dma_addr)
51 {
52 	return dma_addr == BAD_DMA_ADDRESS;
53 }
54 
ipath_dma_map_single(struct ib_device * dev,void * cpu_addr,size_t size,enum dma_data_direction direction)55 static u64 ipath_dma_map_single(struct ib_device *dev,
56 			        void *cpu_addr, size_t size,
57 			        enum dma_data_direction direction)
58 {
59 	BUG_ON(!valid_dma_direction(direction));
60 	return (u64) cpu_addr;
61 }
62 
ipath_dma_unmap_single(struct ib_device * dev,u64 addr,size_t size,enum dma_data_direction direction)63 static void ipath_dma_unmap_single(struct ib_device *dev,
64 				   u64 addr, size_t size,
65 				   enum dma_data_direction direction)
66 {
67 	BUG_ON(!valid_dma_direction(direction));
68 }
69 
ipath_dma_map_page(struct ib_device * dev,struct page * page,unsigned long offset,size_t size,enum dma_data_direction direction)70 static u64 ipath_dma_map_page(struct ib_device *dev,
71 			      struct page *page,
72 			      unsigned long offset,
73 			      size_t size,
74 			      enum dma_data_direction direction)
75 {
76 	u64 addr;
77 
78 	BUG_ON(!valid_dma_direction(direction));
79 
80 	if (offset + size > PAGE_SIZE) {
81 		addr = BAD_DMA_ADDRESS;
82 		goto done;
83 	}
84 
85 	addr = (u64) page_address(page);
86 	if (addr)
87 		addr += offset;
88 	/* TODO: handle highmem pages */
89 
90 done:
91 	return addr;
92 }
93 
ipath_dma_unmap_page(struct ib_device * dev,u64 addr,size_t size,enum dma_data_direction direction)94 static void ipath_dma_unmap_page(struct ib_device *dev,
95 				 u64 addr, size_t size,
96 				 enum dma_data_direction direction)
97 {
98 	BUG_ON(!valid_dma_direction(direction));
99 }
100 
ipath_map_sg(struct ib_device * dev,struct scatterlist * sgl,int nents,enum dma_data_direction direction)101 static int ipath_map_sg(struct ib_device *dev, struct scatterlist *sgl,
102 			int nents, enum dma_data_direction direction)
103 {
104 	struct scatterlist *sg;
105 	u64 addr;
106 	int i;
107 	int ret = nents;
108 
109 	BUG_ON(!valid_dma_direction(direction));
110 
111 	for_each_sg(sgl, sg, nents, i) {
112 		addr = (u64) page_address(sg_page(sg));
113 		/* TODO: handle highmem pages */
114 		if (!addr) {
115 			ret = 0;
116 			break;
117 		}
118 	}
119 	return ret;
120 }
121 
ipath_unmap_sg(struct ib_device * dev,struct scatterlist * sg,int nents,enum dma_data_direction direction)122 static void ipath_unmap_sg(struct ib_device *dev,
123 			   struct scatterlist *sg, int nents,
124 			   enum dma_data_direction direction)
125 {
126 	BUG_ON(!valid_dma_direction(direction));
127 }
128 
ipath_sg_dma_address(struct ib_device * dev,struct scatterlist * sg)129 static u64 ipath_sg_dma_address(struct ib_device *dev, struct scatterlist *sg)
130 {
131 	u64 addr = (u64) page_address(sg_page(sg));
132 
133 	if (addr)
134 		addr += sg->offset;
135 	return addr;
136 }
137 
ipath_sg_dma_len(struct ib_device * dev,struct scatterlist * sg)138 static unsigned int ipath_sg_dma_len(struct ib_device *dev,
139 				     struct scatterlist *sg)
140 {
141 	return sg->length;
142 }
143 
ipath_sync_single_for_cpu(struct ib_device * dev,u64 addr,size_t size,enum dma_data_direction dir)144 static void ipath_sync_single_for_cpu(struct ib_device *dev,
145 				      u64 addr,
146 				      size_t size,
147 				      enum dma_data_direction dir)
148 {
149 }
150 
ipath_sync_single_for_device(struct ib_device * dev,u64 addr,size_t size,enum dma_data_direction dir)151 static void ipath_sync_single_for_device(struct ib_device *dev,
152 					 u64 addr,
153 					 size_t size,
154 					 enum dma_data_direction dir)
155 {
156 }
157 
ipath_dma_alloc_coherent(struct ib_device * dev,size_t size,u64 * dma_handle,gfp_t flag)158 static void *ipath_dma_alloc_coherent(struct ib_device *dev, size_t size,
159 				      u64 *dma_handle, gfp_t flag)
160 {
161 	struct page *p;
162 	void *addr = NULL;
163 
164 	p = alloc_pages(flag, get_order(size));
165 	if (p)
166 		addr = page_address(p);
167 	if (dma_handle)
168 		*dma_handle = (u64) addr;
169 	return addr;
170 }
171 
ipath_dma_free_coherent(struct ib_device * dev,size_t size,void * cpu_addr,u64 dma_handle)172 static void ipath_dma_free_coherent(struct ib_device *dev, size_t size,
173 				    void *cpu_addr, u64 dma_handle)
174 {
175 	free_pages((unsigned long) cpu_addr, get_order(size));
176 }
177 
178 struct ib_dma_mapping_ops ipath_dma_mapping_ops = {
179 	ipath_mapping_error,
180 	ipath_dma_map_single,
181 	ipath_dma_unmap_single,
182 	ipath_dma_map_page,
183 	ipath_dma_unmap_page,
184 	ipath_map_sg,
185 	ipath_unmap_sg,
186 	ipath_sg_dma_address,
187 	ipath_sg_dma_len,
188 	ipath_sync_single_for_cpu,
189 	ipath_sync_single_for_device,
190 	ipath_dma_alloc_coherent,
191 	ipath_dma_free_coherent
192 };
193