2 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
3 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 #include <linux/vmalloc.h>
37 #include "rxe_queue.h"
39 int do_mmap_info(struct rxe_dev
*rxe
,
40 struct mminfo __user
*outbuf
,
41 struct ib_ucontext
*context
,
42 struct rxe_queue_buf
*buf
,
44 struct rxe_mmap_info
**ip_p
)
47 struct rxe_mmap_info
*ip
= NULL
;
50 ip
= rxe_create_mmap_info(rxe
, buf_size
, context
, buf
);
54 err
= copy_to_user(outbuf
, &ip
->info
, sizeof(ip
->info
));
58 spin_lock_bh(&rxe
->pending_lock
);
59 list_add(&ip
->pending_mmaps
, &rxe
->pending_mmaps
);
60 spin_unlock_bh(&rxe
->pending_lock
);
73 inline void rxe_queue_reset(struct rxe_queue
*q
)
75 /* queue is comprised from header and the memory
76 * of the actual queue. See "struct rxe_queue_buf" in rxe_queue.h
77 * reset only the queue itself and not the management header
79 memset(q
->buf
->data
, 0, q
->buf_size
- sizeof(struct rxe_queue_buf
));
82 struct rxe_queue
*rxe_queue_init(struct rxe_dev
*rxe
,
84 unsigned int elem_size
)
88 unsigned int num_slots
;
90 /* num_elem == 0 is allowed, but uninteresting */
94 q
= kmalloc(sizeof(*q
), GFP_KERNEL
);
100 /* used in resize, only need to copy used part of queue */
101 q
->elem_size
= elem_size
;
103 /* pad element up to at least a cacheline and always a power of 2 */
104 if (elem_size
< cache_line_size())
105 elem_size
= cache_line_size();
106 elem_size
= roundup_pow_of_two(elem_size
);
108 q
->log2_elem_size
= order_base_2(elem_size
);
110 num_slots
= *num_elem
+ 1;
111 num_slots
= roundup_pow_of_two(num_slots
);
112 q
->index_mask
= num_slots
- 1;
114 buf_size
= sizeof(struct rxe_queue_buf
) + num_slots
* elem_size
;
116 q
->buf
= vmalloc_user(buf_size
);
120 q
->buf
->log2_elem_size
= q
->log2_elem_size
;
121 q
->buf
->index_mask
= q
->index_mask
;
123 q
->buf_size
= buf_size
;
125 *num_elem
= num_slots
- 1;
134 /* copies elements from original q to new q and then swaps the contents of the
135 * two q headers. This is so that if anyone is holding a pointer to q it will
138 static int resize_finish(struct rxe_queue
*q
, struct rxe_queue
*new_q
,
139 unsigned int num_elem
)
141 if (!queue_empty(q
) && (num_elem
< queue_count(q
)))
144 while (!queue_empty(q
)) {
145 memcpy(producer_addr(new_q
), consumer_addr(q
),
147 advance_producer(new_q
);
156 int rxe_queue_resize(struct rxe_queue
*q
,
157 unsigned int *num_elem_p
,
158 unsigned int elem_size
,
159 struct ib_ucontext
*context
,
160 struct mminfo __user
*outbuf
,
161 spinlock_t
*producer_lock
,
162 spinlock_t
*consumer_lock
)
164 struct rxe_queue
*new_q
;
165 unsigned int num_elem
= *num_elem_p
;
167 unsigned long flags
= 0, flags1
;
169 new_q
= rxe_queue_init(q
->rxe
, &num_elem
, elem_size
);
173 err
= do_mmap_info(new_q
->rxe
, outbuf
, context
, new_q
->buf
,
174 new_q
->buf_size
, &new_q
->ip
);
181 spin_lock_irqsave(consumer_lock
, flags1
);
184 spin_lock_irqsave(producer_lock
, flags
);
185 err
= resize_finish(q
, new_q
, num_elem
);
186 spin_unlock_irqrestore(producer_lock
, flags
);
188 err
= resize_finish(q
, new_q
, num_elem
);
191 spin_unlock_irqrestore(consumer_lock
, flags1
);
193 rxe_queue_cleanup(new_q
); /* new/old dep on err */
197 *num_elem_p
= num_elem
;
204 void rxe_queue_cleanup(struct rxe_queue
*q
)
207 kref_put(&q
->ip
->ref
, rxe_mmap_release
);