blob: a1b283dd2d4c35391057785ab2ea304b1df086d1 [file] [log] [blame]
Bob Pearson63fa15d2020-08-27 09:54:40 -05001// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
Moni Shoua8700e3e2016-06-16 16:45:23 +03002/*
3 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
4 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
Moni Shoua8700e3e2016-06-16 16:45:23 +03005 */
6
7#include <linux/vmalloc.h>
8#include "rxe.h"
9#include "rxe_loc.h"
10#include "rxe_queue.h"
11
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +030012int do_mmap_info(struct rxe_dev *rxe, struct mminfo __user *outbuf,
13 struct ib_udata *udata, struct rxe_queue_buf *buf,
14 size_t buf_size, struct rxe_mmap_info **ip_p)
Moni Shoua8700e3e2016-06-16 16:45:23 +030015{
16 int err;
Moni Shoua8700e3e2016-06-16 16:45:23 +030017 struct rxe_mmap_info *ip = NULL;
18
Jason Gunthorpe0c43ab32018-03-13 16:33:18 -060019 if (outbuf) {
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +030020 ip = rxe_create_mmap_info(rxe, buf_size, udata, buf);
Sudip Mukherjeebb43c8e2020-04-26 00:35:45 +010021 if (IS_ERR(ip)) {
22 err = PTR_ERR(ip);
Moni Shoua8700e3e2016-06-16 16:45:23 +030023 goto err1;
Sudip Mukherjeebb43c8e2020-04-26 00:35:45 +010024 }
Moni Shoua8700e3e2016-06-16 16:45:23 +030025
Sudip Mukherjeebb43c8e2020-04-26 00:35:45 +010026 if (copy_to_user(outbuf, &ip->info, sizeof(ip->info))) {
27 err = -EFAULT;
Moni Shoua8700e3e2016-06-16 16:45:23 +030028 goto err2;
Sudip Mukherjeebb43c8e2020-04-26 00:35:45 +010029 }
Moni Shoua8700e3e2016-06-16 16:45:23 +030030
31 spin_lock_bh(&rxe->pending_lock);
32 list_add(&ip->pending_mmaps, &rxe->pending_mmaps);
33 spin_unlock_bh(&rxe->pending_lock);
34 }
35
36 *ip_p = ip;
37
38 return 0;
39
40err2:
41 kfree(ip);
42err1:
Sudip Mukherjeebb43c8e2020-04-26 00:35:45 +010043 return err;
Moni Shoua8700e3e2016-06-16 16:45:23 +030044}
45
Yonatan Cohenaa75b072016-11-16 10:39:17 +020046inline void rxe_queue_reset(struct rxe_queue *q)
47{
48 /* queue is comprised from header and the memory
49 * of the actual queue. See "struct rxe_queue_buf" in rxe_queue.h
50 * reset only the queue itself and not the management header
51 */
52 memset(q->buf->data, 0, q->buf_size - sizeof(struct rxe_queue_buf));
53}
54
Bob Pearson59daff42021-05-27 14:47:46 -050055struct rxe_queue *rxe_queue_init(struct rxe_dev *rxe, int *num_elem,
56 unsigned int elem_size, enum queue_type type)
Moni Shoua8700e3e2016-06-16 16:45:23 +030057{
58 struct rxe_queue *q;
59 size_t buf_size;
60 unsigned int num_slots;
61
62 /* num_elem == 0 is allowed, but uninteresting */
63 if (*num_elem < 0)
64 goto err1;
65
Xiao Yangcc4f5962021-08-20 19:15:09 +080066 q = kzalloc(sizeof(*q), GFP_KERNEL);
Moni Shoua8700e3e2016-06-16 16:45:23 +030067 if (!q)
68 goto err1;
69
70 q->rxe = rxe;
Bob Pearson59daff42021-05-27 14:47:46 -050071 q->type = type;
Moni Shoua8700e3e2016-06-16 16:45:23 +030072
73 /* used in resize, only need to copy used part of queue */
74 q->elem_size = elem_size;
75
76 /* pad element up to at least a cacheline and always a power of 2 */
77 if (elem_size < cache_line_size())
78 elem_size = cache_line_size();
79 elem_size = roundup_pow_of_two(elem_size);
80
81 q->log2_elem_size = order_base_2(elem_size);
82
83 num_slots = *num_elem + 1;
84 num_slots = roundup_pow_of_two(num_slots);
85 q->index_mask = num_slots - 1;
86
87 buf_size = sizeof(struct rxe_queue_buf) + num_slots * elem_size;
88
89 q->buf = vmalloc_user(buf_size);
90 if (!q->buf)
91 goto err2;
92
93 q->buf->log2_elem_size = q->log2_elem_size;
94 q->buf->index_mask = q->index_mask;
95
96 q->buf_size = buf_size;
97
98 *num_elem = num_slots - 1;
99 return q;
100
101err2:
102 kfree(q);
103err1:
104 return NULL;
105}
106
107/* copies elements from original q to new q and then swaps the contents of the
108 * two q headers. This is so that if anyone is holding a pointer to q it will
109 * still work
110 */
111static int resize_finish(struct rxe_queue *q, struct rxe_queue *new_q,
112 unsigned int num_elem)
113{
Bob Pearsonae6e8432021-09-14 11:42:03 -0500114 enum queue_type type = q->type;
115 u32 prod;
116 u32 cons;
117
118 if (!queue_empty(q, q->type) && (num_elem < queue_count(q, type)))
Moni Shoua8700e3e2016-06-16 16:45:23 +0300119 return -EINVAL;
120
Bob Pearsonae6e8432021-09-14 11:42:03 -0500121 prod = queue_get_producer(new_q, type);
122 cons = queue_get_consumer(q, type);
123
124 while (!queue_empty(q, type)) {
125 memcpy(queue_addr_from_index(new_q, prod),
126 queue_addr_from_index(q, cons), new_q->elem_size);
127 prod = queue_next_index(new_q, prod);
128 cons = queue_next_index(q, cons);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300129 }
130
Bob Pearsonae6e8432021-09-14 11:42:03 -0500131 new_q->buf->producer_index = prod;
132 q->buf->consumer_index = cons;
133
134 /* update private index copies */
135 if (type == QUEUE_TYPE_TO_CLIENT)
136 new_q->index = new_q->buf->producer_index;
137 else
138 q->index = q->buf->consumer_index;
139
140 /* exchange rxe_queue headers */
Moni Shoua8700e3e2016-06-16 16:45:23 +0300141 swap(*q, *new_q);
142
143 return 0;
144}
145
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +0300146int rxe_queue_resize(struct rxe_queue *q, unsigned int *num_elem_p,
147 unsigned int elem_size, struct ib_udata *udata,
148 struct mminfo __user *outbuf, spinlock_t *producer_lock,
Moni Shoua8700e3e2016-06-16 16:45:23 +0300149 spinlock_t *consumer_lock)
150{
151 struct rxe_queue *new_q;
152 unsigned int num_elem = *num_elem_p;
153 int err;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300154
Bob Pearson59daff42021-05-27 14:47:46 -0500155 new_q = rxe_queue_init(q->rxe, &num_elem, elem_size, q->type);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300156 if (!new_q)
157 return -ENOMEM;
158
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +0300159 err = do_mmap_info(new_q->rxe, outbuf, udata, new_q->buf,
Moni Shoua8700e3e2016-06-16 16:45:23 +0300160 new_q->buf_size, &new_q->ip);
161 if (err) {
162 vfree(new_q->buf);
163 kfree(new_q);
164 goto err1;
165 }
166
Bob Pearson21adfa72021-11-03 00:02:30 -0500167 spin_lock_bh(consumer_lock);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300168
169 if (producer_lock) {
Bob Pearson21adfa72021-11-03 00:02:30 -0500170 spin_lock_bh(producer_lock);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300171 err = resize_finish(q, new_q, num_elem);
Bob Pearson21adfa72021-11-03 00:02:30 -0500172 spin_unlock_bh(producer_lock);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300173 } else {
174 err = resize_finish(q, new_q, num_elem);
175 }
176
Bob Pearson21adfa72021-11-03 00:02:30 -0500177 spin_unlock_bh(consumer_lock);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300178
179 rxe_queue_cleanup(new_q); /* new/old dep on err */
180 if (err)
181 goto err1;
182
183 *num_elem_p = num_elem;
184 return 0;
185
186err1:
187 return err;
188}
189
190void rxe_queue_cleanup(struct rxe_queue *q)
191{
192 if (q->ip)
193 kref_put(&q->ip->ref, rxe_mmap_release);
194 else
195 vfree(q->buf);
196
197 kfree(q);
198}