Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cascardo/linux.git] / drivers / infiniband / sw / rxe / rxe_dma.c
1 /*
2  * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
3  * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33
34 #include "rxe.h"
35 #include "rxe_loc.h"
36
37 #define DMA_BAD_ADDER ((u64)0)
38
39 static int rxe_mapping_error(struct ib_device *dev, u64 dma_addr)
40 {
41         return dma_addr == DMA_BAD_ADDER;
42 }
43
44 static u64 rxe_dma_map_single(struct ib_device *dev,
45                               void *cpu_addr, size_t size,
46                               enum dma_data_direction direction)
47 {
48         WARN_ON(!valid_dma_direction(direction));
49         return (uintptr_t)cpu_addr;
50 }
51
52 static void rxe_dma_unmap_single(struct ib_device *dev,
53                                  u64 addr, size_t size,
54                                  enum dma_data_direction direction)
55 {
56         WARN_ON(!valid_dma_direction(direction));
57 }
58
59 static u64 rxe_dma_map_page(struct ib_device *dev,
60                             struct page *page,
61                             unsigned long offset,
62                             size_t size, enum dma_data_direction direction)
63 {
64         u64 addr;
65
66         WARN_ON(!valid_dma_direction(direction));
67
68         if (offset + size > PAGE_SIZE) {
69                 addr = DMA_BAD_ADDER;
70                 goto done;
71         }
72
73         addr = (uintptr_t)page_address(page);
74         if (addr)
75                 addr += offset;
76
77 done:
78         return addr;
79 }
80
81 static void rxe_dma_unmap_page(struct ib_device *dev,
82                                u64 addr, size_t size,
83                                enum dma_data_direction direction)
84 {
85         WARN_ON(!valid_dma_direction(direction));
86 }
87
88 static int rxe_map_sg(struct ib_device *dev, struct scatterlist *sgl,
89                       int nents, enum dma_data_direction direction)
90 {
91         struct scatterlist *sg;
92         u64 addr;
93         int i;
94         int ret = nents;
95
96         WARN_ON(!valid_dma_direction(direction));
97
98         for_each_sg(sgl, sg, nents, i) {
99                 addr = (uintptr_t)page_address(sg_page(sg));
100                 if (!addr) {
101                         ret = 0;
102                         break;
103                 }
104                 sg->dma_address = addr + sg->offset;
105 #ifdef CONFIG_NEED_SG_DMA_LENGTH
106                 sg->dma_length = sg->length;
107 #endif
108         }
109
110         return ret;
111 }
112
113 static void rxe_unmap_sg(struct ib_device *dev,
114                          struct scatterlist *sg, int nents,
115                          enum dma_data_direction direction)
116 {
117         WARN_ON(!valid_dma_direction(direction));
118 }
119
120 static void rxe_sync_single_for_cpu(struct ib_device *dev,
121                                     u64 addr,
122                                     size_t size, enum dma_data_direction dir)
123 {
124 }
125
126 static void rxe_sync_single_for_device(struct ib_device *dev,
127                                        u64 addr,
128                                        size_t size, enum dma_data_direction dir)
129 {
130 }
131
132 static void *rxe_dma_alloc_coherent(struct ib_device *dev, size_t size,
133                                     u64 *dma_handle, gfp_t flag)
134 {
135         struct page *p;
136         void *addr = NULL;
137
138         p = alloc_pages(flag, get_order(size));
139         if (p)
140                 addr = page_address(p);
141
142         if (dma_handle)
143                 *dma_handle = (uintptr_t)addr;
144
145         return addr;
146 }
147
148 static void rxe_dma_free_coherent(struct ib_device *dev, size_t size,
149                                   void *cpu_addr, u64 dma_handle)
150 {
151         free_pages((unsigned long)cpu_addr, get_order(size));
152 }
153
154 struct ib_dma_mapping_ops rxe_dma_mapping_ops = {
155         .mapping_error          = rxe_mapping_error,
156         .map_single             = rxe_dma_map_single,
157         .unmap_single           = rxe_dma_unmap_single,
158         .map_page               = rxe_dma_map_page,
159         .unmap_page             = rxe_dma_unmap_page,
160         .map_sg                 = rxe_map_sg,
161         .unmap_sg               = rxe_unmap_sg,
162         .sync_single_for_cpu    = rxe_sync_single_for_cpu,
163         .sync_single_for_device = rxe_sync_single_for_device,
164         .alloc_coherent         = rxe_dma_alloc_coherent,
165         .free_coherent          = rxe_dma_free_coherent
166 };