blob: 08274254eb887531068d8239dd13c95a16ef190c [file] [log] [blame]
Moni Shoua8700e3e2016-06-16 16:45:23 +03001/*
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 retailuce 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 <linux/vmalloc.h>
35#include "rxe.h"
36#include "rxe_loc.h"
37#include "rxe_queue.h"
38
39int do_mmap_info(struct rxe_dev *rxe,
40 struct ib_udata *udata,
41 bool is_req,
42 struct ib_ucontext *context,
43 struct rxe_queue_buf *buf,
44 size_t buf_size,
45 struct rxe_mmap_info **ip_p)
46{
47 int err;
48 u32 len, offset;
49 struct rxe_mmap_info *ip = NULL;
50
51 if (udata) {
52 if (is_req) {
53 len = udata->outlen - sizeof(struct mminfo);
54 offset = sizeof(struct mminfo);
55 } else {
56 len = udata->outlen;
57 offset = 0;
58 }
59
60 if (len < sizeof(ip->info))
61 goto err1;
62
63 ip = rxe_create_mmap_info(rxe, buf_size, context, buf);
64 if (!ip)
65 goto err1;
66
67 err = copy_to_user(udata->outbuf + offset, &ip->info,
68 sizeof(ip->info));
69 if (err)
70 goto err2;
71
72 spin_lock_bh(&rxe->pending_lock);
73 list_add(&ip->pending_mmaps, &rxe->pending_mmaps);
74 spin_unlock_bh(&rxe->pending_lock);
75 }
76
77 *ip_p = ip;
78
79 return 0;
80
81err2:
82 kfree(ip);
83err1:
84 return -EINVAL;
85}
86
87struct rxe_queue *rxe_queue_init(struct rxe_dev *rxe,
88 int *num_elem,
89 unsigned int elem_size)
90{
91 struct rxe_queue *q;
92 size_t buf_size;
93 unsigned int num_slots;
94
95 /* num_elem == 0 is allowed, but uninteresting */
96 if (*num_elem < 0)
97 goto err1;
98
99 q = kmalloc(sizeof(*q), GFP_KERNEL);
100 if (!q)
101 goto err1;
102
103 q->rxe = rxe;
104
105 /* used in resize, only need to copy used part of queue */
106 q->elem_size = elem_size;
107
108 /* pad element up to at least a cacheline and always a power of 2 */
109 if (elem_size < cache_line_size())
110 elem_size = cache_line_size();
111 elem_size = roundup_pow_of_two(elem_size);
112
113 q->log2_elem_size = order_base_2(elem_size);
114
115 num_slots = *num_elem + 1;
116 num_slots = roundup_pow_of_two(num_slots);
117 q->index_mask = num_slots - 1;
118
119 buf_size = sizeof(struct rxe_queue_buf) + num_slots * elem_size;
120
121 q->buf = vmalloc_user(buf_size);
122 if (!q->buf)
123 goto err2;
124
125 q->buf->log2_elem_size = q->log2_elem_size;
126 q->buf->index_mask = q->index_mask;
127
128 q->buf_size = buf_size;
129
130 *num_elem = num_slots - 1;
131 return q;
132
133err2:
134 kfree(q);
135err1:
136 return NULL;
137}
138
139/* copies elements from original q to new q and then swaps the contents of the
140 * two q headers. This is so that if anyone is holding a pointer to q it will
141 * still work
142 */
143static int resize_finish(struct rxe_queue *q, struct rxe_queue *new_q,
144 unsigned int num_elem)
145{
146 if (!queue_empty(q) && (num_elem < queue_count(q)))
147 return -EINVAL;
148
149 while (!queue_empty(q)) {
150 memcpy(producer_addr(new_q), consumer_addr(q),
151 new_q->elem_size);
152 advance_producer(new_q);
153 advance_consumer(q);
154 }
155
156 swap(*q, *new_q);
157
158 return 0;
159}
160
161int rxe_queue_resize(struct rxe_queue *q,
162 unsigned int *num_elem_p,
163 unsigned int elem_size,
164 struct ib_ucontext *context,
165 struct ib_udata *udata,
166 spinlock_t *producer_lock,
167 spinlock_t *consumer_lock)
168{
169 struct rxe_queue *new_q;
170 unsigned int num_elem = *num_elem_p;
171 int err;
172 unsigned long flags = 0, flags1;
173
174 new_q = rxe_queue_init(q->rxe, &num_elem, elem_size);
175 if (!new_q)
176 return -ENOMEM;
177
178 err = do_mmap_info(new_q->rxe, udata, false, context, new_q->buf,
179 new_q->buf_size, &new_q->ip);
180 if (err) {
181 vfree(new_q->buf);
182 kfree(new_q);
183 goto err1;
184 }
185
186 spin_lock_irqsave(consumer_lock, flags1);
187
188 if (producer_lock) {
189 spin_lock_irqsave(producer_lock, flags);
190 err = resize_finish(q, new_q, num_elem);
191 spin_unlock_irqrestore(producer_lock, flags);
192 } else {
193 err = resize_finish(q, new_q, num_elem);
194 }
195
196 spin_unlock_irqrestore(consumer_lock, flags1);
197
198 rxe_queue_cleanup(new_q); /* new/old dep on err */
199 if (err)
200 goto err1;
201
202 *num_elem_p = num_elem;
203 return 0;
204
205err1:
206 return err;
207}
208
209void rxe_queue_cleanup(struct rxe_queue *q)
210{
211 if (q->ip)
212 kref_put(&q->ip->ref, rxe_mmap_release);
213 else
214 vfree(q->buf);
215
216 kfree(q);
217}