blob: 4233fd9edfd18e0ae2633453a7c5920d5a8a65da [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
Bart Van Assche0bbb3b72017-01-20 13:04:37 -08007#include <linux/dma-mapping.h>
Yuval Shaia4d6f2852017-03-14 16:01:57 +02008#include <net/addrconf.h>
Shamir Rabinovitch89944452019-02-07 18:44:49 +02009#include <rdma/uverbs_ioctl.h>
Moni Shoua8700e3e2016-06-16 16:45:23 +030010#include "rxe.h"
11#include "rxe_loc.h"
12#include "rxe_queue.h"
Yonatan Cohen0b1e5b92017-03-10 18:23:56 +020013#include "rxe_hw_counters.h"
Moni Shoua8700e3e2016-06-16 16:45:23 +030014
15static int rxe_query_device(struct ib_device *dev,
16 struct ib_device_attr *attr,
17 struct ib_udata *uhw)
18{
19 struct rxe_dev *rxe = to_rdev(dev);
20
21 if (uhw->inlen || uhw->outlen)
22 return -EINVAL;
23
24 *attr = rxe->attr;
25 return 0;
26}
27
Moni Shoua8700e3e2016-06-16 16:45:23 +030028static int rxe_query_port(struct ib_device *dev,
Mark Bloch1fb7f892021-03-01 09:04:20 +020029 u32 port_num, struct ib_port_attr *attr)
Moni Shoua8700e3e2016-06-16 16:45:23 +030030{
31 struct rxe_dev *rxe = to_rdev(dev);
Yuval Shaia59590b82018-12-09 13:06:10 +020032 int rc;
Moni Shoua8700e3e2016-06-16 16:45:23 +030033
Or Gerlitzc4550c62017-01-24 13:02:39 +020034 /* *attr being zeroed by the caller, avoid zeroing it here */
Zhu Yanjund12faf22021-09-14 21:24:56 -040035 *attr = rxe->port.attr;
Moni Shoua8700e3e2016-06-16 16:45:23 +030036
37 mutex_lock(&rxe->usdev_lock);
Yuval Shaiad4186192017-06-14 23:13:34 +030038 rc = ib_get_eth_speed(dev, port_num, &attr->active_speed,
39 &attr->active_width);
Andrew Boyer5736c7c2018-11-01 09:18:45 -040040
41 if (attr->state == IB_PORT_ACTIVE)
Kamal Heib72a77202019-08-07 13:31:35 +030042 attr->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
Andrew Boyer5736c7c2018-11-01 09:18:45 -040043 else if (dev_get_flags(rxe->ndev) & IFF_UP)
Kamal Heib72a77202019-08-07 13:31:35 +030044 attr->phys_state = IB_PORT_PHYS_STATE_POLLING;
Andrew Boyer5736c7c2018-11-01 09:18:45 -040045 else
Kamal Heib72a77202019-08-07 13:31:35 +030046 attr->phys_state = IB_PORT_PHYS_STATE_DISABLED;
Andrew Boyer5736c7c2018-11-01 09:18:45 -040047
Moni Shoua8700e3e2016-06-16 16:45:23 +030048 mutex_unlock(&rxe->usdev_lock);
49
Yuval Shaiad4186192017-06-14 23:13:34 +030050 return rc;
Moni Shoua8700e3e2016-06-16 16:45:23 +030051}
52
Moni Shoua8700e3e2016-06-16 16:45:23 +030053static int rxe_query_pkey(struct ib_device *device,
Mark Bloch1fb7f892021-03-01 09:04:20 +020054 u32 port_num, u16 index, u16 *pkey)
Moni Shoua8700e3e2016-06-16 16:45:23 +030055{
Kamal Heib76251e12020-07-21 13:16:18 +030056 if (index > 0)
57 return -EINVAL;
Moni Shoua8700e3e2016-06-16 16:45:23 +030058
Kamal Heib76251e12020-07-21 13:16:18 +030059 *pkey = IB_DEFAULT_PKEY_FULL;
Moni Shoua8700e3e2016-06-16 16:45:23 +030060 return 0;
Moni Shoua8700e3e2016-06-16 16:45:23 +030061}
62
63static int rxe_modify_device(struct ib_device *dev,
64 int mask, struct ib_device_modify *attr)
65{
66 struct rxe_dev *rxe = to_rdev(dev);
67
Kamal Heibf3fceba2019-09-23 13:41:58 +030068 if (mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
69 IB_DEVICE_MODIFY_NODE_DESC))
70 return -EOPNOTSUPP;
71
Moni Shoua8700e3e2016-06-16 16:45:23 +030072 if (mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID)
73 rxe->attr.sys_image_guid = cpu_to_be64(attr->sys_image_guid);
74
75 if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
76 memcpy(rxe->ib_dev.node_desc,
77 attr->node_desc, sizeof(rxe->ib_dev.node_desc));
78 }
79
80 return 0;
81}
82
83static int rxe_modify_port(struct ib_device *dev,
Mark Bloch1fb7f892021-03-01 09:04:20 +020084 u32 port_num, int mask, struct ib_port_modify *attr)
Moni Shoua8700e3e2016-06-16 16:45:23 +030085{
86 struct rxe_dev *rxe = to_rdev(dev);
87 struct rxe_port *port;
88
Moni Shoua8700e3e2016-06-16 16:45:23 +030089 port = &rxe->port;
90
91 port->attr.port_cap_flags |= attr->set_port_cap_mask;
92 port->attr.port_cap_flags &= ~attr->clr_port_cap_mask;
93
94 if (mask & IB_PORT_RESET_QKEY_CNTR)
95 port->attr.qkey_viol_cntr = 0;
96
97 return 0;
Moni Shoua8700e3e2016-06-16 16:45:23 +030098}
99
100static enum rdma_link_layer rxe_get_link_layer(struct ib_device *dev,
Mark Bloch1fb7f892021-03-01 09:04:20 +0200101 u32 port_num)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300102{
Kamal Heib420bd9e2020-07-05 13:43:13 +0300103 return IB_LINK_LAYER_ETHERNET;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300104}
105
Bob Pearson91a42c52020-12-16 17:15:48 -0600106static int rxe_alloc_ucontext(struct ib_ucontext *ibuc, struct ib_udata *udata)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300107{
Bob Pearson91a42c52020-12-16 17:15:48 -0600108 struct rxe_dev *rxe = to_rdev(ibuc->device);
109 struct rxe_ucontext *uc = to_ruc(ibuc);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300110
Bob Pearson91a42c52020-12-16 17:15:48 -0600111 return rxe_add_to_pool(&rxe->uc_pool, uc);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300112}
113
Leon Romanovskya2a074e2019-02-12 20:39:16 +0200114static void rxe_dealloc_ucontext(struct ib_ucontext *ibuc)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300115{
116 struct rxe_ucontext *uc = to_ruc(ibuc);
117
118 rxe_drop_ref(uc);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300119}
120
Mark Bloch1fb7f892021-03-01 09:04:20 +0200121static int rxe_port_immutable(struct ib_device *dev, u32 port_num,
Moni Shoua8700e3e2016-06-16 16:45:23 +0300122 struct ib_port_immutable *immutable)
123{
124 int err;
125 struct ib_port_attr attr;
126
Or Gerlitzc4550c62017-01-24 13:02:39 +0200127 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
128
129 err = ib_query_port(dev, port_num, &attr);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300130 if (err)
131 return err;
132
133 immutable->pkey_tbl_len = attr.pkey_tbl_len;
134 immutable->gid_tbl_len = attr.gid_tbl_len;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300135 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
136
137 return 0;
138}
139
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +0300140static int rxe_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300141{
Leon Romanovsky21a428a2019-02-03 14:55:51 +0200142 struct rxe_dev *rxe = to_rdev(ibpd->device);
143 struct rxe_pd *pd = to_rpd(ibpd);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300144
Bob Pearson91a42c52020-12-16 17:15:48 -0600145 return rxe_add_to_pool(&rxe->pd_pool, pd);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300146}
147
Leon Romanovsky91a7c582020-09-07 15:09:13 +0300148static int rxe_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300149{
150 struct rxe_pd *pd = to_rpd(ibpd);
151
152 rxe_drop_ref(pd);
Leon Romanovsky91a7c582020-09-07 15:09:13 +0300153 return 0;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300154}
155
Maor Gottliebfa5d0102020-04-30 22:21:42 +0300156static int rxe_create_ah(struct ib_ah *ibah,
157 struct rdma_ah_init_attr *init_attr,
158 struct ib_udata *udata)
Moni Shoua477864c2016-11-23 08:23:24 +0200159
Moni Shoua8700e3e2016-06-16 16:45:23 +0300160{
161 int err;
Leon Romanovskyd3456912019-04-03 16:42:42 +0300162 struct rxe_dev *rxe = to_rdev(ibah->device);
163 struct rxe_ah *ah = to_rah(ibah);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300164
Maor Gottliebfa5d0102020-04-30 22:21:42 +0300165 err = rxe_av_chk_attr(rxe, init_attr->ah_attr);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300166 if (err)
Leon Romanovskyd3456912019-04-03 16:42:42 +0300167 return err;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300168
Bob Pearson91a42c52020-12-16 17:15:48 -0600169 err = rxe_add_to_pool(&rxe->ah_pool, ah);
Leon Romanovskyd3456912019-04-03 16:42:42 +0300170 if (err)
171 return err;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300172
Maor Gottliebfa5d0102020-04-30 22:21:42 +0300173 rxe_init_av(init_attr->ah_attr, &ah->av);
Leon Romanovskyd3456912019-04-03 16:42:42 +0300174 return 0;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300175}
176
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -0400177static int rxe_modify_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300178{
179 int err;
180 struct rxe_dev *rxe = to_rdev(ibah->device);
181 struct rxe_ah *ah = to_rah(ibah);
182
183 err = rxe_av_chk_attr(rxe, attr);
184 if (err)
185 return err;
186
Kamal Heibfa407182019-01-29 12:08:49 +0200187 rxe_init_av(attr, &ah->av);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300188 return 0;
189}
190
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -0400191static int rxe_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300192{
Moni Shoua8700e3e2016-06-16 16:45:23 +0300193 struct rxe_ah *ah = to_rah(ibah);
194
Dasaratharaman Chandramoulieca7ddf2017-04-29 14:41:17 -0400195 memset(attr, 0, sizeof(*attr));
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -0400196 attr->type = ibah->type;
Zhu Yanjun9c96f3d2018-01-31 06:06:56 -0500197 rxe_av_to_attr(&ah->av, attr);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300198 return 0;
199}
200
Leon Romanovsky9a9ebf82020-09-07 15:09:14 +0300201static int rxe_destroy_ah(struct ib_ah *ibah, u32 flags)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300202{
203 struct rxe_ah *ah = to_rah(ibah);
204
Moni Shoua8700e3e2016-06-16 16:45:23 +0300205 rxe_drop_ref(ah);
Leon Romanovsky9a9ebf82020-09-07 15:09:14 +0300206 return 0;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300207}
208
Bart Van Assched34ac5c2018-07-18 09:25:32 -0700209static int post_one_recv(struct rxe_rq *rq, const struct ib_recv_wr *ibwr)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300210{
211 int err;
212 int i;
213 u32 length;
214 struct rxe_recv_wqe *recv_wqe;
215 int num_sge = ibwr->num_sge;
Bob Pearson5bcf5a52021-05-27 14:47:48 -0500216 int full;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300217
Bob Pearsonae6e8432021-09-14 11:42:03 -0500218 full = queue_full(rq->queue, QUEUE_TYPE_TO_DRIVER);
Bob Pearson5bcf5a52021-05-27 14:47:48 -0500219 if (unlikely(full)) {
Moni Shoua8700e3e2016-06-16 16:45:23 +0300220 err = -ENOMEM;
221 goto err1;
222 }
223
224 if (unlikely(num_sge > rq->max_sge)) {
225 err = -EINVAL;
226 goto err1;
227 }
228
229 length = 0;
230 for (i = 0; i < num_sge; i++)
231 length += ibwr->sg_list[i].length;
232
Bob Pearsonae6e8432021-09-14 11:42:03 -0500233 recv_wqe = queue_producer_addr(rq->queue, QUEUE_TYPE_TO_DRIVER);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300234 recv_wqe->wr_id = ibwr->wr_id;
235 recv_wqe->num_sge = num_sge;
236
237 memcpy(recv_wqe->dma.sge, ibwr->sg_list,
238 num_sge * sizeof(struct ib_sge));
239
240 recv_wqe->dma.length = length;
241 recv_wqe->dma.resid = length;
242 recv_wqe->dma.num_sge = num_sge;
243 recv_wqe->dma.cur_sge = 0;
244 recv_wqe->dma.sge_offset = 0;
245
Bob Pearsonae6e8432021-09-14 11:42:03 -0500246 queue_advance_producer(rq->queue, QUEUE_TYPE_TO_DRIVER);
Bob Pearson5bcf5a52021-05-27 14:47:48 -0500247
Moni Shoua8700e3e2016-06-16 16:45:23 +0300248 return 0;
249
250err1:
251 return err;
252}
253
Leon Romanovsky68e326d2019-04-03 16:42:43 +0300254static int rxe_create_srq(struct ib_srq *ibsrq, struct ib_srq_init_attr *init,
255 struct ib_udata *udata)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300256{
257 int err;
Leon Romanovsky68e326d2019-04-03 16:42:43 +0300258 struct rxe_dev *rxe = to_rdev(ibsrq->device);
259 struct rxe_pd *pd = to_rpd(ibsrq->pd);
260 struct rxe_srq *srq = to_rsrq(ibsrq);
Jason Gunthorpe0c43ab32018-03-13 16:33:18 -0600261 struct rxe_create_srq_resp __user *uresp = NULL;
262
Jason Gunthorpe652caba2020-10-03 20:20:05 -0300263 if (init->srq_type != IB_SRQT_BASIC)
264 return -EOPNOTSUPP;
265
Jason Gunthorpe0c43ab32018-03-13 16:33:18 -0600266 if (udata) {
267 if (udata->outlen < sizeof(*uresp))
Leon Romanovsky68e326d2019-04-03 16:42:43 +0300268 return -EINVAL;
Jason Gunthorpe0c43ab32018-03-13 16:33:18 -0600269 uresp = udata->outbuf;
270 }
Moni Shoua8700e3e2016-06-16 16:45:23 +0300271
272 err = rxe_srq_chk_attr(rxe, NULL, &init->attr, IB_SRQ_INIT_MASK);
273 if (err)
274 goto err1;
275
Bob Pearson91a42c52020-12-16 17:15:48 -0600276 err = rxe_add_to_pool(&rxe->srq_pool, srq);
Leon Romanovsky68e326d2019-04-03 16:42:43 +0300277 if (err)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300278 goto err1;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300279
Moni Shoua8700e3e2016-06-16 16:45:23 +0300280 rxe_add_ref(pd);
281 srq->pd = pd;
282
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +0300283 err = rxe_srq_from_init(rxe, srq, init, udata, uresp);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300284 if (err)
285 goto err2;
286
Leon Romanovsky68e326d2019-04-03 16:42:43 +0300287 return 0;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300288
289err2:
290 rxe_drop_ref(pd);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300291 rxe_drop_ref(srq);
292err1:
Leon Romanovsky68e326d2019-04-03 16:42:43 +0300293 return err;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300294}
295
296static int rxe_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
297 enum ib_srq_attr_mask mask,
298 struct ib_udata *udata)
299{
300 int err;
301 struct rxe_srq *srq = to_rsrq(ibsrq);
302 struct rxe_dev *rxe = to_rdev(ibsrq->device);
Jason Gunthorpe0c43ab32018-03-13 16:33:18 -0600303 struct rxe_modify_srq_cmd ucmd = {};
304
305 if (udata) {
306 if (udata->inlen < sizeof(ucmd))
307 return -EINVAL;
308
309 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
310 if (err)
311 return err;
312 }
Moni Shoua8700e3e2016-06-16 16:45:23 +0300313
314 err = rxe_srq_chk_attr(rxe, srq, attr, mask);
315 if (err)
316 goto err1;
317
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +0300318 err = rxe_srq_from_attr(rxe, srq, attr, mask, &ucmd, udata);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300319 if (err)
320 goto err1;
321
322 return 0;
323
324err1:
325 return err;
326}
327
328static int rxe_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
329{
330 struct rxe_srq *srq = to_rsrq(ibsrq);
331
332 if (srq->error)
333 return -EINVAL;
334
335 attr->max_wr = srq->rq.queue->buf->index_mask;
336 attr->max_sge = srq->rq.max_sge;
337 attr->srq_limit = srq->limit;
338 return 0;
339}
340
Leon Romanovsky119181d2020-09-07 15:09:16 +0300341static int rxe_destroy_srq(struct ib_srq *ibsrq, struct ib_udata *udata)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300342{
343 struct rxe_srq *srq = to_rsrq(ibsrq);
344
345 if (srq->rq.queue)
346 rxe_queue_cleanup(srq->rq.queue);
347
348 rxe_drop_ref(srq->pd);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300349 rxe_drop_ref(srq);
Leon Romanovsky119181d2020-09-07 15:09:16 +0300350 return 0;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300351}
352
Bart Van Assched34ac5c2018-07-18 09:25:32 -0700353static int rxe_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
354 const struct ib_recv_wr **bad_wr)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300355{
356 int err = 0;
357 unsigned long flags;
358 struct rxe_srq *srq = to_rsrq(ibsrq);
359
360 spin_lock_irqsave(&srq->rq.producer_lock, flags);
361
362 while (wr) {
363 err = post_one_recv(&srq->rq, wr);
364 if (unlikely(err))
365 break;
366 wr = wr->next;
367 }
368
369 spin_unlock_irqrestore(&srq->rq.producer_lock, flags);
370
371 if (err)
372 *bad_wr = wr;
373
374 return err;
375}
376
Leon Romanovsky514aee62021-07-23 14:39:50 +0300377static int rxe_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init,
378 struct ib_udata *udata)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300379{
380 int err;
Leon Romanovsky514aee62021-07-23 14:39:50 +0300381 struct rxe_dev *rxe = to_rdev(ibqp->device);
382 struct rxe_pd *pd = to_rpd(ibqp->pd);
383 struct rxe_qp *qp = to_rqp(ibqp);
Jason Gunthorpe0c43ab32018-03-13 16:33:18 -0600384 struct rxe_create_qp_resp __user *uresp = NULL;
385
386 if (udata) {
387 if (udata->outlen < sizeof(*uresp))
Leon Romanovsky514aee62021-07-23 14:39:50 +0300388 return -EINVAL;
Jason Gunthorpe0c43ab32018-03-13 16:33:18 -0600389 uresp = udata->outbuf;
390 }
Moni Shoua8700e3e2016-06-16 16:45:23 +0300391
Jason Gunthorpe1f11a762020-10-03 20:20:08 -0300392 if (init->create_flags)
Leon Romanovsky514aee62021-07-23 14:39:50 +0300393 return -EOPNOTSUPP;
Jason Gunthorpe1f11a762020-10-03 20:20:08 -0300394
Moni Shoua8700e3e2016-06-16 16:45:23 +0300395 err = rxe_qp_chk_init(rxe, init);
396 if (err)
Leon Romanovsky514aee62021-07-23 14:39:50 +0300397 return err;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300398
399 if (udata) {
Leon Romanovsky514aee62021-07-23 14:39:50 +0300400 if (udata->inlen)
401 return -EINVAL;
402
Bob Pearson5bcf5a52021-05-27 14:47:48 -0500403 qp->is_user = true;
404 } else {
405 qp->is_user = false;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300406 }
407
Leon Romanovsky514aee62021-07-23 14:39:50 +0300408 err = rxe_add_to_pool(&rxe->qp_pool, qp);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300409 if (err)
Leon Romanovsky514aee62021-07-23 14:39:50 +0300410 return err;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300411
Leon Romanovsky514aee62021-07-23 14:39:50 +0300412 rxe_add_index(qp);
413 err = rxe_qp_from_init(rxe, qp, pd, init, uresp, ibqp->pd, udata);
414 if (err)
415 goto qp_init;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300416
Leon Romanovsky514aee62021-07-23 14:39:50 +0300417 return 0;
418
419qp_init:
Moni Shoua8700e3e2016-06-16 16:45:23 +0300420 rxe_drop_index(qp);
421 rxe_drop_ref(qp);
Leon Romanovsky514aee62021-07-23 14:39:50 +0300422 return err;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300423}
424
425static int rxe_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
426 int mask, struct ib_udata *udata)
427{
428 int err;
429 struct rxe_dev *rxe = to_rdev(ibqp->device);
430 struct rxe_qp *qp = to_rqp(ibqp);
431
Jason Gunthorpe26e990b2020-10-03 20:20:06 -0300432 if (mask & ~IB_QP_ATTR_STANDARD_BITS)
433 return -EOPNOTSUPP;
434
Moni Shoua8700e3e2016-06-16 16:45:23 +0300435 err = rxe_qp_chk_attr(rxe, qp, attr, mask);
436 if (err)
437 goto err1;
438
439 err = rxe_qp_from_attr(qp, attr, mask, udata);
440 if (err)
441 goto err1;
442
443 return 0;
444
445err1:
446 return err;
447}
448
449static int rxe_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
450 int mask, struct ib_qp_init_attr *init)
451{
452 struct rxe_qp *qp = to_rqp(ibqp);
453
454 rxe_qp_to_init(qp, init);
455 rxe_qp_to_attr(qp, attr, mask);
456
457 return 0;
458}
459
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +0300460static int rxe_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300461{
462 struct rxe_qp *qp = to_rqp(ibqp);
463
464 rxe_qp_destroy(qp);
465 rxe_drop_index(qp);
466 rxe_drop_ref(qp);
467 return 0;
468}
469
Bart Van Asschef696bf62018-07-18 09:25:14 -0700470static int validate_send_wr(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
Moni Shoua8700e3e2016-06-16 16:45:23 +0300471 unsigned int mask, unsigned int length)
472{
473 int num_sge = ibwr->num_sge;
474 struct rxe_sq *sq = &qp->sq;
475
476 if (unlikely(num_sge > sq->max_sge))
477 goto err1;
478
479 if (unlikely(mask & WR_ATOMIC_MASK)) {
480 if (length < 8)
481 goto err1;
482
483 if (atomic_wr(ibwr)->remote_addr & 0x7)
484 goto err1;
485 }
486
487 if (unlikely((ibwr->send_flags & IB_SEND_INLINE) &&
488 (length > sq->max_inline)))
489 goto err1;
490
491 return 0;
492
493err1:
494 return -EINVAL;
495}
496
497static void init_send_wr(struct rxe_qp *qp, struct rxe_send_wr *wr,
Bart Van Asschef696bf62018-07-18 09:25:14 -0700498 const struct ib_send_wr *ibwr)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300499{
500 wr->wr_id = ibwr->wr_id;
501 wr->num_sge = ibwr->num_sge;
502 wr->opcode = ibwr->opcode;
503 wr->send_flags = ibwr->send_flags;
504
505 if (qp_type(qp) == IB_QPT_UD ||
506 qp_type(qp) == IB_QPT_SMI ||
507 qp_type(qp) == IB_QPT_GSI) {
508 wr->wr.ud.remote_qpn = ud_wr(ibwr)->remote_qpn;
509 wr->wr.ud.remote_qkey = ud_wr(ibwr)->remote_qkey;
510 if (qp_type(qp) == IB_QPT_GSI)
511 wr->wr.ud.pkey_index = ud_wr(ibwr)->pkey_index;
512 if (wr->opcode == IB_WR_SEND_WITH_IMM)
513 wr->ex.imm_data = ibwr->ex.imm_data;
514 } else {
515 switch (wr->opcode) {
516 case IB_WR_RDMA_WRITE_WITH_IMM:
517 wr->ex.imm_data = ibwr->ex.imm_data;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500518 fallthrough;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300519 case IB_WR_RDMA_READ:
520 case IB_WR_RDMA_WRITE:
521 wr->wr.rdma.remote_addr = rdma_wr(ibwr)->remote_addr;
522 wr->wr.rdma.rkey = rdma_wr(ibwr)->rkey;
523 break;
524 case IB_WR_SEND_WITH_IMM:
525 wr->ex.imm_data = ibwr->ex.imm_data;
526 break;
527 case IB_WR_SEND_WITH_INV:
528 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
529 break;
530 case IB_WR_ATOMIC_CMP_AND_SWP:
531 case IB_WR_ATOMIC_FETCH_AND_ADD:
532 wr->wr.atomic.remote_addr =
533 atomic_wr(ibwr)->remote_addr;
534 wr->wr.atomic.compare_add =
535 atomic_wr(ibwr)->compare_add;
536 wr->wr.atomic.swap = atomic_wr(ibwr)->swap;
537 wr->wr.atomic.rkey = atomic_wr(ibwr)->rkey;
538 break;
539 case IB_WR_LOCAL_INV:
540 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
541 break;
542 case IB_WR_REG_MR:
543 wr->wr.reg.mr = reg_wr(ibwr)->mr;
544 wr->wr.reg.key = reg_wr(ibwr)->key;
545 wr->wr.reg.access = reg_wr(ibwr)->access;
546 break;
547 default:
548 break;
549 }
550 }
551}
552
Bob Pearson086f5802021-02-05 18:24:37 -0600553static void copy_inline_data_to_wqe(struct rxe_send_wqe *wqe,
554 const struct ib_send_wr *ibwr)
555{
556 struct ib_sge *sge = ibwr->sg_list;
557 u8 *p = wqe->dma.inline_data;
558 int i;
559
560 for (i = 0; i < ibwr->num_sge; i++, sge++) {
561 memcpy(p, (void *)(uintptr_t)sge->addr, sge->length);
562 p += sge->length;
563 }
564}
565
566static void init_send_wqe(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
Moni Shoua8700e3e2016-06-16 16:45:23 +0300567 unsigned int mask, unsigned int length,
568 struct rxe_send_wqe *wqe)
569{
570 int num_sge = ibwr->num_sge;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300571
572 init_send_wr(qp, &wqe->wr, ibwr);
573
Bob Pearsondc780742021-02-05 17:05:26 -0600574 /* local operation */
Bob Pearson886441f2021-06-07 23:25:48 -0500575 if (unlikely(mask & WR_LOCAL_OP_MASK)) {
Bob Pearsondc780742021-02-05 17:05:26 -0600576 wqe->mask = mask;
577 wqe->state = wqe_state_posted;
Bob Pearson086f5802021-02-05 18:24:37 -0600578 return;
Bob Pearsondc780742021-02-05 17:05:26 -0600579 }
580
Moni Shoua8700e3e2016-06-16 16:45:23 +0300581 if (qp_type(qp) == IB_QPT_UD ||
582 qp_type(qp) == IB_QPT_SMI ||
583 qp_type(qp) == IB_QPT_GSI)
Bob Pearsoncfc03122021-10-07 15:40:47 -0500584 memcpy(&wqe->wr.wr.ud.av, &to_rah(ud_wr(ibwr)->ah)->av,
585 sizeof(struct rxe_av));
Moni Shoua8700e3e2016-06-16 16:45:23 +0300586
Bob Pearson086f5802021-02-05 18:24:37 -0600587 if (unlikely(ibwr->send_flags & IB_SEND_INLINE))
588 copy_inline_data_to_wqe(wqe, ibwr);
589 else
Moni Shoua8700e3e2016-06-16 16:45:23 +0300590 memcpy(wqe->dma.sge, ibwr->sg_list,
591 num_sge * sizeof(struct ib_sge));
592
Bart Van Asschea6544a62018-03-01 14:00:29 -0800593 wqe->iova = mask & WR_ATOMIC_MASK ? atomic_wr(ibwr)->remote_addr :
594 mask & WR_READ_OR_WRITE_MASK ? rdma_wr(ibwr)->remote_addr : 0;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300595 wqe->mask = mask;
596 wqe->dma.length = length;
597 wqe->dma.resid = length;
598 wqe->dma.num_sge = num_sge;
599 wqe->dma.cur_sge = 0;
600 wqe->dma.sge_offset = 0;
601 wqe->state = wqe_state_posted;
602 wqe->ssn = atomic_add_return(1, &qp->ssn);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300603}
604
Bart Van Asschef696bf62018-07-18 09:25:14 -0700605static int post_one_send(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
Parav Pandite404f942016-09-28 20:26:26 +0000606 unsigned int mask, u32 length)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300607{
608 int err;
609 struct rxe_sq *sq = &qp->sq;
610 struct rxe_send_wqe *send_wqe;
611 unsigned long flags;
Bob Pearson5bcf5a52021-05-27 14:47:48 -0500612 int full;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300613
614 err = validate_send_wr(qp, ibwr, mask, length);
615 if (err)
616 return err;
617
618 spin_lock_irqsave(&qp->sq.sq_lock, flags);
619
Bob Pearsonae6e8432021-09-14 11:42:03 -0500620 full = queue_full(sq->queue, QUEUE_TYPE_TO_DRIVER);
Bob Pearson5bcf5a52021-05-27 14:47:48 -0500621
622 if (unlikely(full)) {
623 spin_unlock_irqrestore(&qp->sq.sq_lock, flags);
624 return -ENOMEM;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300625 }
626
Bob Pearsonae6e8432021-09-14 11:42:03 -0500627 send_wqe = queue_producer_addr(sq->queue, QUEUE_TYPE_TO_DRIVER);
Bob Pearson086f5802021-02-05 18:24:37 -0600628 init_send_wqe(qp, ibwr, mask, length, send_wqe);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300629
Bob Pearsonae6e8432021-09-14 11:42:03 -0500630 queue_advance_producer(sq->queue, QUEUE_TYPE_TO_DRIVER);
Bob Pearson5bcf5a52021-05-27 14:47:48 -0500631
Moni Shoua8700e3e2016-06-16 16:45:23 +0300632 spin_unlock_irqrestore(&qp->sq.sq_lock, flags);
633
634 return 0;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300635}
636
Bart Van Assched34ac5c2018-07-18 09:25:32 -0700637static int rxe_post_send_kernel(struct rxe_qp *qp, const struct ib_send_wr *wr,
638 const struct ib_send_wr **bad_wr)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300639{
640 int err = 0;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300641 unsigned int mask;
642 unsigned int length = 0;
643 int i;
Mikhail Malygin5f0b2a62020-07-16 22:03:41 +0300644 struct ib_send_wr *next;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300645
Moni Shoua8700e3e2016-06-16 16:45:23 +0300646 while (wr) {
647 mask = wr_opcode_mask(wr->opcode, qp);
648 if (unlikely(!mask)) {
649 err = -EINVAL;
650 *bad_wr = wr;
651 break;
652 }
653
654 if (unlikely((wr->send_flags & IB_SEND_INLINE) &&
655 !(mask & WR_INLINE_MASK))) {
656 err = -EINVAL;
657 *bad_wr = wr;
658 break;
659 }
660
Mikhail Malygin5f0b2a62020-07-16 22:03:41 +0300661 next = wr->next;
662
Moni Shoua8700e3e2016-06-16 16:45:23 +0300663 length = 0;
664 for (i = 0; i < wr->num_sge; i++)
665 length += wr->sg_list[i].length;
666
667 err = post_one_send(qp, wr, mask, length);
668
669 if (err) {
670 *bad_wr = wr;
671 break;
672 }
Mikhail Malygin5f0b2a62020-07-16 22:03:41 +0300673 wr = next;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300674 }
675
Alexandru Moise1661d3b02018-05-08 11:02:02 +0200676 rxe_run_task(&qp->req.task, 1);
Bart Van Assche6f301e02018-01-09 11:23:40 -0800677 if (unlikely(qp->req.state == QP_STATE_ERROR))
678 rxe_run_task(&qp->comp.task, 1);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300679
680 return err;
681}
682
Bart Van Assched34ac5c2018-07-18 09:25:32 -0700683static int rxe_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
684 const struct ib_send_wr **bad_wr)
Parav Pandit063af592016-09-28 20:24:12 +0000685{
686 struct rxe_qp *qp = to_rqp(ibqp);
687
688 if (unlikely(!qp->valid)) {
689 *bad_wr = wr;
690 return -EINVAL;
691 }
692
693 if (unlikely(qp->req.state < QP_STATE_READY)) {
694 *bad_wr = wr;
695 return -EINVAL;
696 }
697
698 if (qp->is_user) {
699 /* Utilize process context to do protocol processing */
700 rxe_run_task(&qp->req.task, 0);
701 return 0;
702 } else
703 return rxe_post_send_kernel(qp, wr, bad_wr);
704}
705
Bart Van Assched34ac5c2018-07-18 09:25:32 -0700706static int rxe_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
707 const struct ib_recv_wr **bad_wr)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300708{
709 int err = 0;
710 struct rxe_qp *qp = to_rqp(ibqp);
711 struct rxe_rq *rq = &qp->rq;
712 unsigned long flags;
713
714 if (unlikely((qp_state(qp) < IB_QPS_INIT) || !qp->valid)) {
715 *bad_wr = wr;
716 err = -EINVAL;
717 goto err1;
718 }
719
720 if (unlikely(qp->srq)) {
721 *bad_wr = wr;
722 err = -EINVAL;
723 goto err1;
724 }
725
726 spin_lock_irqsave(&rq->producer_lock, flags);
727
728 while (wr) {
729 err = post_one_recv(rq, wr);
730 if (unlikely(err)) {
731 *bad_wr = wr;
732 break;
733 }
734 wr = wr->next;
735 }
736
737 spin_unlock_irqrestore(&rq->producer_lock, flags);
738
Vijay Immanuel12171972017-06-27 12:19:38 +0300739 if (qp->resp.state == QP_STATE_ERROR)
740 rxe_run_task(&qp->resp.task, 1);
741
Moni Shoua8700e3e2016-06-16 16:45:23 +0300742err1:
743 return err;
744}
745
Leon Romanovskye39afe32019-05-28 14:37:29 +0300746static int rxe_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
747 struct ib_udata *udata)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300748{
749 int err;
Leon Romanovskye39afe32019-05-28 14:37:29 +0300750 struct ib_device *dev = ibcq->device;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300751 struct rxe_dev *rxe = to_rdev(dev);
Leon Romanovskye39afe32019-05-28 14:37:29 +0300752 struct rxe_cq *cq = to_rcq(ibcq);
Jason Gunthorpe0c43ab32018-03-13 16:33:18 -0600753 struct rxe_create_cq_resp __user *uresp = NULL;
754
755 if (udata) {
756 if (udata->outlen < sizeof(*uresp))
Leon Romanovskye39afe32019-05-28 14:37:29 +0300757 return -EINVAL;
Jason Gunthorpe0c43ab32018-03-13 16:33:18 -0600758 uresp = udata->outbuf;
759 }
Moni Shoua8700e3e2016-06-16 16:45:23 +0300760
761 if (attr->flags)
Jason Gunthorpe1c407cb2020-10-03 20:20:07 -0300762 return -EOPNOTSUPP;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300763
Jason Gunthorpeb92ec0f2018-03-13 16:33:17 -0600764 err = rxe_cq_chk_attr(rxe, NULL, attr->cqe, attr->comp_vector);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300765 if (err)
Leon Romanovskye39afe32019-05-28 14:37:29 +0300766 return err;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300767
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +0300768 err = rxe_cq_from_init(rxe, cq, attr->cqe, attr->comp_vector, udata,
769 uresp);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300770 if (err)
Leon Romanovskye39afe32019-05-28 14:37:29 +0300771 return err;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300772
Bob Pearson91a42c52020-12-16 17:15:48 -0600773 return rxe_add_to_pool(&rxe->cq_pool, cq);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300774}
775
Leon Romanovsky43d781b2020-09-07 15:09:18 +0300776static int rxe_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300777{
778 struct rxe_cq *cq = to_rcq(ibcq);
779
Andrew Boyerbfc3ae02017-08-28 16:11:50 -0400780 rxe_cq_disable(cq);
781
Moni Shoua8700e3e2016-06-16 16:45:23 +0300782 rxe_drop_ref(cq);
Leon Romanovsky43d781b2020-09-07 15:09:18 +0300783 return 0;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300784}
785
786static int rxe_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
787{
788 int err;
789 struct rxe_cq *cq = to_rcq(ibcq);
790 struct rxe_dev *rxe = to_rdev(ibcq->device);
Jason Gunthorpe0c43ab32018-03-13 16:33:18 -0600791 struct rxe_resize_cq_resp __user *uresp = NULL;
792
793 if (udata) {
794 if (udata->outlen < sizeof(*uresp))
795 return -EINVAL;
796 uresp = udata->outbuf;
797 }
Moni Shoua8700e3e2016-06-16 16:45:23 +0300798
Jason Gunthorpeb92ec0f2018-03-13 16:33:17 -0600799 err = rxe_cq_chk_attr(rxe, cq, cqe, 0);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300800 if (err)
801 goto err1;
802
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +0300803 err = rxe_cq_resize_queue(cq, cqe, uresp, udata);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300804 if (err)
805 goto err1;
806
807 return 0;
808
809err1:
810 return err;
811}
812
813static int rxe_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
814{
815 int i;
816 struct rxe_cq *cq = to_rcq(ibcq);
817 struct rxe_cqe *cqe;
818 unsigned long flags;
819
820 spin_lock_irqsave(&cq->cq_lock, flags);
821 for (i = 0; i < num_entries; i++) {
Bob Pearsonae6e8432021-09-14 11:42:03 -0500822 cqe = queue_head(cq->queue, QUEUE_TYPE_FROM_DRIVER);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300823 if (!cqe)
824 break;
825
826 memcpy(wc++, &cqe->ibwc, sizeof(*wc));
Bob Pearsonae6e8432021-09-14 11:42:03 -0500827 queue_advance_consumer(cq->queue, QUEUE_TYPE_FROM_DRIVER);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300828 }
829 spin_unlock_irqrestore(&cq->cq_lock, flags);
830
831 return i;
832}
833
834static int rxe_peek_cq(struct ib_cq *ibcq, int wc_cnt)
835{
836 struct rxe_cq *cq = to_rcq(ibcq);
Bob Pearson5bcf5a52021-05-27 14:47:48 -0500837 int count;
838
Bob Pearsonae6e8432021-09-14 11:42:03 -0500839 count = queue_count(cq->queue, QUEUE_TYPE_FROM_DRIVER);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300840
841 return (count > wc_cnt) ? wc_cnt : count;
842}
843
844static int rxe_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
845{
846 struct rxe_cq *cq = to_rcq(ibcq);
Andrew Boyeraccacb82016-11-23 12:39:22 -0500847 unsigned long irq_flags;
848 int ret = 0;
Bob Pearson5bcf5a52021-05-27 14:47:48 -0500849 int empty;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300850
Andrew Boyeraccacb82016-11-23 12:39:22 -0500851 spin_lock_irqsave(&cq->cq_lock, irq_flags);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300852 if (cq->notify != IB_CQ_NEXT_COMP)
853 cq->notify = flags & IB_CQ_SOLICITED_MASK;
854
Bob Pearsonae6e8432021-09-14 11:42:03 -0500855 empty = queue_empty(cq->queue, QUEUE_TYPE_FROM_DRIVER);
Bob Pearson5bcf5a52021-05-27 14:47:48 -0500856
857 if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !empty)
Andrew Boyeraccacb82016-11-23 12:39:22 -0500858 ret = 1;
859
860 spin_unlock_irqrestore(&cq->cq_lock, irq_flags);
861
862 return ret;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300863}
864
865static struct ib_mr *rxe_get_dma_mr(struct ib_pd *ibpd, int access)
866{
867 struct rxe_dev *rxe = to_rdev(ibpd->device);
868 struct rxe_pd *pd = to_rpd(ibpd);
Bob Pearson364e2822021-03-25 16:24:26 -0500869 struct rxe_mr *mr;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300870
871 mr = rxe_alloc(&rxe->mr_pool);
Kamal Heib293d8442020-07-05 13:43:12 +0300872 if (!mr)
873 return ERR_PTR(-ENOMEM);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300874
875 rxe_add_index(mr);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300876 rxe_add_ref(pd);
Bob Pearson364e2822021-03-25 16:24:26 -0500877 rxe_mr_init_dma(pd, access, mr);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300878
879 return &mr->ibmr;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300880}
881
882static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd,
883 u64 start,
884 u64 length,
885 u64 iova,
886 int access, struct ib_udata *udata)
887{
888 int err;
889 struct rxe_dev *rxe = to_rdev(ibpd->device);
890 struct rxe_pd *pd = to_rpd(ibpd);
Bob Pearson364e2822021-03-25 16:24:26 -0500891 struct rxe_mr *mr;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300892
893 mr = rxe_alloc(&rxe->mr_pool);
894 if (!mr) {
895 err = -ENOMEM;
896 goto err2;
897 }
898
899 rxe_add_index(mr);
900
901 rxe_add_ref(pd);
902
Lang Chengcd5b0102021-05-12 16:12:22 +0800903 err = rxe_mr_init_user(pd, start, length, iova, access, mr);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300904 if (err)
905 goto err3;
906
907 return &mr->ibmr;
908
909err3:
910 rxe_drop_ref(pd);
911 rxe_drop_index(mr);
912 rxe_drop_ref(mr);
913err2:
914 return ERR_PTR(err);
915}
916
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +0300917static struct ib_mr *rxe_alloc_mr(struct ib_pd *ibpd, enum ib_mr_type mr_type,
Gal Pressman42a3b152020-07-06 15:03:43 +0300918 u32 max_num_sg)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300919{
920 struct rxe_dev *rxe = to_rdev(ibpd->device);
921 struct rxe_pd *pd = to_rpd(ibpd);
Bob Pearson364e2822021-03-25 16:24:26 -0500922 struct rxe_mr *mr;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300923 int err;
924
925 if (mr_type != IB_MR_TYPE_MEM_REG)
926 return ERR_PTR(-EINVAL);
927
928 mr = rxe_alloc(&rxe->mr_pool);
929 if (!mr) {
930 err = -ENOMEM;
931 goto err1;
932 }
933
934 rxe_add_index(mr);
935
936 rxe_add_ref(pd);
937
Bob Pearson364e2822021-03-25 16:24:26 -0500938 err = rxe_mr_init_fast(pd, max_num_sg, mr);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300939 if (err)
940 goto err2;
941
942 return &mr->ibmr;
943
944err2:
945 rxe_drop_ref(pd);
946 rxe_drop_index(mr);
947 rxe_drop_ref(mr);
948err1:
949 return ERR_PTR(err);
950}
951
Bob Pearson647bf132021-09-14 11:42:06 -0500952/* build next_map_set from scatterlist
953 * The IB_WR_REG_MR WR will swap map_sets
954 */
Parav Pandite404f942016-09-28 20:26:26 +0000955static int rxe_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg,
956 int sg_nents, unsigned int *sg_offset)
Moni Shoua8700e3e2016-06-16 16:45:23 +0300957{
Bob Pearson364e2822021-03-25 16:24:26 -0500958 struct rxe_mr *mr = to_rmr(ibmr);
Bob Pearson647bf132021-09-14 11:42:06 -0500959 struct rxe_map_set *set = mr->next_map_set;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300960 int n;
961
Bob Pearson647bf132021-09-14 11:42:06 -0500962 set->nbuf = 0;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300963
Bob Pearson647bf132021-09-14 11:42:06 -0500964 n = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, rxe_mr_set_page);
Moni Shoua8700e3e2016-06-16 16:45:23 +0300965
Bob Pearson647bf132021-09-14 11:42:06 -0500966 set->va = ibmr->iova;
967 set->iova = ibmr->iova;
968 set->length = ibmr->length;
969 set->page_shift = ilog2(ibmr->page_size);
970 set->page_mask = ibmr->page_size - 1;
971 set->offset = set->iova & set->page_mask;
Moni Shoua8700e3e2016-06-16 16:45:23 +0300972
973 return n;
974}
975
976static int rxe_attach_mcast(struct ib_qp *ibqp, union ib_gid *mgid, u16 mlid)
977{
978 int err;
979 struct rxe_dev *rxe = to_rdev(ibqp->device);
980 struct rxe_qp *qp = to_rqp(ibqp);
981 struct rxe_mc_grp *grp;
982
983 /* takes a ref on grp if successful */
984 err = rxe_mcast_get_grp(rxe, mgid, &grp);
985 if (err)
986 return err;
987
988 err = rxe_mcast_add_grp_elem(rxe, qp, grp);
989
990 rxe_drop_ref(grp);
991 return err;
992}
993
994static int rxe_detach_mcast(struct ib_qp *ibqp, union ib_gid *mgid, u16 mlid)
995{
996 struct rxe_dev *rxe = to_rdev(ibqp->device);
997 struct rxe_qp *qp = to_rqp(ibqp);
998
999 return rxe_mcast_drop_grp_elem(rxe, qp, mgid);
1000}
1001
Kamal Heibc05d2662017-06-15 11:29:05 +03001002static ssize_t parent_show(struct device *device,
1003 struct device_attribute *attr, char *buf)
Moni Shoua8700e3e2016-06-16 16:45:23 +03001004{
Parav Pandit54747232018-12-18 14:15:56 +02001005 struct rxe_dev *rxe =
1006 rdma_device_to_drv_device(device, struct rxe_dev, ib_dev);
Moni Shoua8700e3e2016-06-16 16:45:23 +03001007
Joe Perches1c7fd722020-10-07 19:36:24 -07001008 return sysfs_emit(buf, "%s\n", rxe_parent_name(rxe, 1));
Moni Shoua8700e3e2016-06-16 16:45:23 +03001009}
1010
Kamal Heibc05d2662017-06-15 11:29:05 +03001011static DEVICE_ATTR_RO(parent);
Moni Shoua8700e3e2016-06-16 16:45:23 +03001012
Parav Pandit508a5232018-10-11 22:31:54 +03001013static struct attribute *rxe_dev_attributes[] = {
1014 &dev_attr_parent.attr,
1015 NULL
1016};
1017
1018static const struct attribute_group rxe_attr_group = {
1019 .attrs = rxe_dev_attributes,
Moni Shoua8700e3e2016-06-16 16:45:23 +03001020};
1021
Jason Gunthorpeca223542019-02-12 21:12:56 -07001022static int rxe_enable_driver(struct ib_device *ib_dev)
1023{
1024 struct rxe_dev *rxe = container_of(ib_dev, struct rxe_dev, ib_dev);
1025
1026 rxe_set_port_state(rxe);
1027 dev_info(&rxe->ib_dev.dev, "added %s\n", netdev_name(rxe->ndev));
1028 return 0;
1029}
1030
Kamal Heib573efc42018-12-10 21:09:46 +02001031static const struct ib_device_ops rxe_dev_ops = {
Jason Gunthorpe7a154142019-06-05 14:39:26 -03001032 .owner = THIS_MODULE,
Jason Gunthorpeb9560a42019-06-05 14:39:24 -03001033 .driver_id = RDMA_DRIVER_RXE,
Jason Gunthorpe72c6ec12019-06-05 14:39:25 -03001034 .uverbs_abi_ver = RXE_UVERBS_ABI_VERSION,
Jason Gunthorpeb9560a42019-06-05 14:39:24 -03001035
Jason Gunthorpe4b5f4d32021-06-11 19:00:20 +03001036 .alloc_hw_port_stats = rxe_ib_alloc_hw_port_stats,
Kamal Heib573efc42018-12-10 21:09:46 +02001037 .alloc_mr = rxe_alloc_mr,
Bob Pearsonbeec0232021-06-07 23:25:47 -05001038 .alloc_mw = rxe_alloc_mw,
Kamal Heib573efc42018-12-10 21:09:46 +02001039 .alloc_pd = rxe_alloc_pd,
1040 .alloc_ucontext = rxe_alloc_ucontext,
1041 .attach_mcast = rxe_attach_mcast,
1042 .create_ah = rxe_create_ah,
1043 .create_cq = rxe_create_cq,
1044 .create_qp = rxe_create_qp,
1045 .create_srq = rxe_create_srq,
Jason Gunthorpe676a80a2020-10-03 20:20:11 -03001046 .create_user_ah = rxe_create_ah,
Jason Gunthorpec3670742019-01-22 16:27:24 -07001047 .dealloc_driver = rxe_dealloc,
Bob Pearsonbeec0232021-06-07 23:25:47 -05001048 .dealloc_mw = rxe_dealloc_mw,
Kamal Heib573efc42018-12-10 21:09:46 +02001049 .dealloc_pd = rxe_dealloc_pd,
1050 .dealloc_ucontext = rxe_dealloc_ucontext,
1051 .dereg_mr = rxe_dereg_mr,
1052 .destroy_ah = rxe_destroy_ah,
1053 .destroy_cq = rxe_destroy_cq,
1054 .destroy_qp = rxe_destroy_qp,
1055 .destroy_srq = rxe_destroy_srq,
1056 .detach_mcast = rxe_detach_mcast,
Jason Gunthorpe915e4af2021-06-11 19:00:34 +03001057 .device_group = &rxe_attr_group,
Jason Gunthorpeca223542019-02-12 21:12:56 -07001058 .enable_driver = rxe_enable_driver,
Kamal Heib573efc42018-12-10 21:09:46 +02001059 .get_dma_mr = rxe_get_dma_mr,
1060 .get_hw_stats = rxe_ib_get_hw_stats,
1061 .get_link_layer = rxe_get_link_layer,
Kamal Heib573efc42018-12-10 21:09:46 +02001062 .get_port_immutable = rxe_port_immutable,
1063 .map_mr_sg = rxe_map_mr_sg,
1064 .mmap = rxe_mmap,
1065 .modify_ah = rxe_modify_ah,
1066 .modify_device = rxe_modify_device,
1067 .modify_port = rxe_modify_port,
1068 .modify_qp = rxe_modify_qp,
1069 .modify_srq = rxe_modify_srq,
1070 .peek_cq = rxe_peek_cq,
1071 .poll_cq = rxe_poll_cq,
1072 .post_recv = rxe_post_recv,
1073 .post_send = rxe_post_send,
1074 .post_srq_recv = rxe_post_srq_recv,
1075 .query_ah = rxe_query_ah,
1076 .query_device = rxe_query_device,
1077 .query_pkey = rxe_query_pkey,
1078 .query_port = rxe_query_port,
1079 .query_qp = rxe_query_qp,
1080 .query_srq = rxe_query_srq,
1081 .reg_user_mr = rxe_reg_user_mr,
1082 .req_notify_cq = rxe_req_notify_cq,
1083 .resize_cq = rxe_resize_cq,
Leon Romanovskyd3456912019-04-03 16:42:42 +03001084
1085 INIT_RDMA_OBJ_SIZE(ib_ah, rxe_ah, ibah),
Leon Romanovskye39afe32019-05-28 14:37:29 +03001086 INIT_RDMA_OBJ_SIZE(ib_cq, rxe_cq, ibcq),
Leon Romanovsky21a428a2019-02-03 14:55:51 +02001087 INIT_RDMA_OBJ_SIZE(ib_pd, rxe_pd, ibpd),
Leon Romanovsky514aee62021-07-23 14:39:50 +03001088 INIT_RDMA_OBJ_SIZE(ib_qp, rxe_qp, ibqp),
Leon Romanovsky68e326d2019-04-03 16:42:43 +03001089 INIT_RDMA_OBJ_SIZE(ib_srq, rxe_srq, ibsrq),
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001090 INIT_RDMA_OBJ_SIZE(ib_ucontext, rxe_ucontext, ibuc),
Bob Pearson364e2822021-03-25 16:24:26 -05001091 INIT_RDMA_OBJ_SIZE(ib_mw, rxe_mw, ibmw),
Kamal Heib573efc42018-12-10 21:09:46 +02001092};
1093
Steve Wise66920e12019-02-15 11:03:57 -08001094int rxe_register_device(struct rxe_dev *rxe, const char *ibdev_name)
Moni Shoua8700e3e2016-06-16 16:45:23 +03001095{
1096 int err;
Moni Shoua8700e3e2016-06-16 16:45:23 +03001097 struct ib_device *dev = &rxe->ib_dev;
1098
Bob Pearsondc780742021-02-05 17:05:26 -06001099 strscpy(dev->node_desc, "rxe", sizeof(dev->node_desc));
Moni Shoua8700e3e2016-06-16 16:45:23 +03001100
Moni Shoua8700e3e2016-06-16 16:45:23 +03001101 dev->node_type = RDMA_NODE_IB_CA;
1102 dev->phys_port_cnt = 1;
Sagi Grimberg67cf3622017-05-04 16:23:07 +03001103 dev->num_comp_vectors = num_possible_cpus();
Moni Shoua8700e3e2016-06-16 16:45:23 +03001104 dev->local_dma_lkey = 0;
Yuval Shaia4d6f2852017-03-14 16:01:57 +02001105 addrconf_addr_eui48((unsigned char *)&dev->node_guid,
1106 rxe->ndev->dev_addr);
Moni Shoua8700e3e2016-06-16 16:45:23 +03001107
Jason Gunthorpe5c419362020-10-30 11:03:05 -03001108 dev->uverbs_cmd_mask |= BIT_ULL(IB_USER_VERBS_CMD_POST_SEND) |
1109 BIT_ULL(IB_USER_VERBS_CMD_REQ_NOTIFY_CQ);
Moni Shoua8700e3e2016-06-16 16:45:23 +03001110
Kamal Heib573efc42018-12-10 21:09:46 +02001111 ib_set_device_ops(dev, &rxe_dev_ops);
Jason Gunthorpe4c173f52019-02-12 21:12:52 -07001112 err = ib_device_set_netdev(&rxe->ib_dev, rxe->ndev, 1);
1113 if (err)
1114 return err;
Moni Shoua8700e3e2016-06-16 16:45:23 +03001115
Bob Pearsonadd2b3b2021-07-06 23:00:39 -05001116 err = rxe_icrc_init(rxe);
1117 if (err)
1118 return err;
yonatanccee26882017-04-20 20:55:55 +03001119
Jason Gunthorpee0477b32020-10-08 11:27:52 +03001120 err = ib_register_device(dev, ibdev_name, NULL);
Jason Gunthorpec3670742019-01-22 16:27:24 -07001121 if (err)
Kamal Heib61013822017-06-15 11:29:06 +03001122 pr_warn("%s failed with error %d\n", __func__, err);
yonatanccee26882017-04-20 20:55:55 +03001123
Jason Gunthorpeca223542019-02-12 21:12:56 -07001124 /*
1125 * Note that rxe may be invalid at this point if another thread
1126 * unregistered it.
1127 */
Moni Shoua8700e3e2016-06-16 16:45:23 +03001128 return err;
1129}