1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
4 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
5 */
6
7 #include <linux/dma-mapping.h>
8 #include <net/addrconf.h>
9 #include <rdma/uverbs_ioctl.h>
10
11 #include "rxe.h"
12 #include "rxe_queue.h"
13 #include "rxe_hw_counters.h"
14
rxe_query_device(struct ib_device * dev,struct ib_device_attr * attr,struct ib_udata * uhw)15 static 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
rxe_query_port(struct ib_device * dev,u32 port_num,struct ib_port_attr * attr)28 static int rxe_query_port(struct ib_device *dev,
29 u32 port_num, struct ib_port_attr *attr)
30 {
31 struct rxe_dev *rxe = to_rdev(dev);
32 int rc;
33
34 /* *attr being zeroed by the caller, avoid zeroing it here */
35 *attr = rxe->port.attr;
36
37 mutex_lock(&rxe->usdev_lock);
38 rc = ib_get_eth_speed(dev, port_num, &attr->active_speed,
39 &attr->active_width);
40
41 if (attr->state == IB_PORT_ACTIVE)
42 attr->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
43 else if (dev_get_flags(rxe->ndev) & IFF_UP)
44 attr->phys_state = IB_PORT_PHYS_STATE_POLLING;
45 else
46 attr->phys_state = IB_PORT_PHYS_STATE_DISABLED;
47
48 mutex_unlock(&rxe->usdev_lock);
49
50 return rc;
51 }
52
rxe_query_pkey(struct ib_device * device,u32 port_num,u16 index,u16 * pkey)53 static int rxe_query_pkey(struct ib_device *device,
54 u32 port_num, u16 index, u16 *pkey)
55 {
56 if (index > 0)
57 return -EINVAL;
58
59 *pkey = IB_DEFAULT_PKEY_FULL;
60 return 0;
61 }
62
rxe_modify_device(struct ib_device * dev,int mask,struct ib_device_modify * attr)63 static 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
68 if (mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
69 IB_DEVICE_MODIFY_NODE_DESC))
70 return -EOPNOTSUPP;
71
72 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
rxe_modify_port(struct ib_device * dev,u32 port_num,int mask,struct ib_port_modify * attr)83 static int rxe_modify_port(struct ib_device *dev,
84 u32 port_num, int mask, struct ib_port_modify *attr)
85 {
86 struct rxe_dev *rxe = to_rdev(dev);
87 struct rxe_port *port;
88
89 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;
98 }
99
rxe_get_link_layer(struct ib_device * dev,u32 port_num)100 static enum rdma_link_layer rxe_get_link_layer(struct ib_device *dev,
101 u32 port_num)
102 {
103 return IB_LINK_LAYER_ETHERNET;
104 }
105
rxe_alloc_ucontext(struct ib_ucontext * ibuc,struct ib_udata * udata)106 static int rxe_alloc_ucontext(struct ib_ucontext *ibuc, struct ib_udata *udata)
107 {
108 struct rxe_dev *rxe = to_rdev(ibuc->device);
109 struct rxe_ucontext *uc = to_ruc(ibuc);
110
111 return rxe_add_to_pool(&rxe->uc_pool, uc);
112 }
113
rxe_dealloc_ucontext(struct ib_ucontext * ibuc)114 static void rxe_dealloc_ucontext(struct ib_ucontext *ibuc)
115 {
116 struct rxe_ucontext *uc = to_ruc(ibuc);
117
118 rxe_cleanup(uc);
119 }
120
rxe_port_immutable(struct ib_device * dev,u32 port_num,struct ib_port_immutable * immutable)121 static int rxe_port_immutable(struct ib_device *dev, u32 port_num,
122 struct ib_port_immutable *immutable)
123 {
124 int err;
125 struct ib_port_attr attr;
126
127 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
128
129 err = ib_query_port(dev, port_num, &attr);
130 if (err)
131 return err;
132
133 immutable->pkey_tbl_len = attr.pkey_tbl_len;
134 immutable->gid_tbl_len = attr.gid_tbl_len;
135 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
136
137 return 0;
138 }
139
rxe_alloc_pd(struct ib_pd * ibpd,struct ib_udata * udata)140 static int rxe_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
141 {
142 struct rxe_dev *rxe = to_rdev(ibpd->device);
143 struct rxe_pd *pd = to_rpd(ibpd);
144
145 return rxe_add_to_pool(&rxe->pd_pool, pd);
146 }
147
rxe_dealloc_pd(struct ib_pd * ibpd,struct ib_udata * udata)148 static int rxe_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
149 {
150 struct rxe_pd *pd = to_rpd(ibpd);
151
152 rxe_cleanup(pd);
153 return 0;
154 }
155
rxe_create_ah(struct ib_ah * ibah,struct rdma_ah_init_attr * init_attr,struct ib_udata * udata)156 static int rxe_create_ah(struct ib_ah *ibah,
157 struct rdma_ah_init_attr *init_attr,
158 struct ib_udata *udata)
159
160 {
161 struct rxe_dev *rxe = to_rdev(ibah->device);
162 struct rxe_ah *ah = to_rah(ibah);
163 struct rxe_create_ah_resp __user *uresp = NULL;
164 int err;
165
166 if (udata) {
167 /* test if new user provider */
168 if (udata->outlen >= sizeof(*uresp))
169 uresp = udata->outbuf;
170 ah->is_user = true;
171 } else {
172 ah->is_user = false;
173 }
174
175 err = rxe_av_chk_attr(rxe, init_attr->ah_attr);
176 if (err)
177 return err;
178
179 err = rxe_add_to_pool_ah(&rxe->ah_pool, ah,
180 init_attr->flags & RDMA_CREATE_AH_SLEEPABLE);
181 if (err)
182 return err;
183
184 /* create index > 0 */
185 ah->ah_num = ah->elem.index;
186
187 if (uresp) {
188 /* only if new user provider */
189 err = copy_to_user(&uresp->ah_num, &ah->ah_num,
190 sizeof(uresp->ah_num));
191 if (err) {
192 rxe_cleanup(ah);
193 return -EFAULT;
194 }
195 } else if (ah->is_user) {
196 /* only if old user provider */
197 ah->ah_num = 0;
198 }
199
200 rxe_init_av(init_attr->ah_attr, &ah->av);
201 rxe_finalize(ah);
202
203 return 0;
204 }
205
rxe_modify_ah(struct ib_ah * ibah,struct rdma_ah_attr * attr)206 static int rxe_modify_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
207 {
208 int err;
209 struct rxe_dev *rxe = to_rdev(ibah->device);
210 struct rxe_ah *ah = to_rah(ibah);
211
212 err = rxe_av_chk_attr(rxe, attr);
213 if (err)
214 return err;
215
216 rxe_init_av(attr, &ah->av);
217 return 0;
218 }
219
rxe_query_ah(struct ib_ah * ibah,struct rdma_ah_attr * attr)220 static int rxe_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
221 {
222 struct rxe_ah *ah = to_rah(ibah);
223
224 memset(attr, 0, sizeof(*attr));
225 attr->type = ibah->type;
226 rxe_av_to_attr(&ah->av, attr);
227 return 0;
228 }
229
rxe_destroy_ah(struct ib_ah * ibah,u32 flags)230 static int rxe_destroy_ah(struct ib_ah *ibah, u32 flags)
231 {
232 struct rxe_ah *ah = to_rah(ibah);
233
234 rxe_cleanup_ah(ah, flags & RDMA_DESTROY_AH_SLEEPABLE);
235
236 return 0;
237 }
238
post_one_recv(struct rxe_rq * rq,const struct ib_recv_wr * ibwr)239 static int post_one_recv(struct rxe_rq *rq, const struct ib_recv_wr *ibwr)
240 {
241 int i;
242 u32 length;
243 struct rxe_recv_wqe *recv_wqe;
244 int num_sge = ibwr->num_sge;
245 int full;
246
247 full = queue_full(rq->queue, QUEUE_TYPE_FROM_ULP);
248 if (unlikely(full))
249 return -ENOMEM;
250
251 if (unlikely(num_sge > rq->max_sge))
252 return -EINVAL;
253
254 length = 0;
255 for (i = 0; i < num_sge; i++)
256 length += ibwr->sg_list[i].length;
257
258 recv_wqe = queue_producer_addr(rq->queue, QUEUE_TYPE_FROM_ULP);
259 recv_wqe->wr_id = ibwr->wr_id;
260
261 memcpy(recv_wqe->dma.sge, ibwr->sg_list,
262 num_sge * sizeof(struct ib_sge));
263
264 recv_wqe->dma.length = length;
265 recv_wqe->dma.resid = length;
266 recv_wqe->dma.num_sge = num_sge;
267 recv_wqe->dma.cur_sge = 0;
268 recv_wqe->dma.sge_offset = 0;
269
270 queue_advance_producer(rq->queue, QUEUE_TYPE_FROM_ULP);
271
272 return 0;
273 }
274
rxe_create_srq(struct ib_srq * ibsrq,struct ib_srq_init_attr * init,struct ib_udata * udata)275 static int rxe_create_srq(struct ib_srq *ibsrq, struct ib_srq_init_attr *init,
276 struct ib_udata *udata)
277 {
278 int err;
279 struct rxe_dev *rxe = to_rdev(ibsrq->device);
280 struct rxe_pd *pd = to_rpd(ibsrq->pd);
281 struct rxe_srq *srq = to_rsrq(ibsrq);
282 struct rxe_create_srq_resp __user *uresp = NULL;
283
284 if (udata) {
285 if (udata->outlen < sizeof(*uresp))
286 return -EINVAL;
287 uresp = udata->outbuf;
288 }
289
290 if (init->srq_type != IB_SRQT_BASIC)
291 return -EOPNOTSUPP;
292
293 err = rxe_srq_chk_init(rxe, init);
294 if (err)
295 return err;
296
297 err = rxe_add_to_pool(&rxe->srq_pool, srq);
298 if (err)
299 return err;
300
301 rxe_get(pd);
302 srq->pd = pd;
303
304 err = rxe_srq_from_init(rxe, srq, init, udata, uresp);
305 if (err)
306 goto err_cleanup;
307
308 return 0;
309
310 err_cleanup:
311 rxe_cleanup(srq);
312
313 return err;
314 }
315
rxe_modify_srq(struct ib_srq * ibsrq,struct ib_srq_attr * attr,enum ib_srq_attr_mask mask,struct ib_udata * udata)316 static int rxe_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
317 enum ib_srq_attr_mask mask,
318 struct ib_udata *udata)
319 {
320 int err;
321 struct rxe_srq *srq = to_rsrq(ibsrq);
322 struct rxe_dev *rxe = to_rdev(ibsrq->device);
323 struct rxe_modify_srq_cmd ucmd = {};
324
325 if (udata) {
326 if (udata->inlen < sizeof(ucmd))
327 return -EINVAL;
328
329 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
330 if (err)
331 return err;
332 }
333
334 err = rxe_srq_chk_attr(rxe, srq, attr, mask);
335 if (err)
336 return err;
337
338 return rxe_srq_from_attr(rxe, srq, attr, mask, &ucmd, udata);
339 }
340
rxe_query_srq(struct ib_srq * ibsrq,struct ib_srq_attr * attr)341 static int rxe_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
342 {
343 struct rxe_srq *srq = to_rsrq(ibsrq);
344
345 if (srq->error)
346 return -EINVAL;
347
348 attr->max_wr = srq->rq.queue->buf->index_mask;
349 attr->max_sge = srq->rq.max_sge;
350 attr->srq_limit = srq->limit;
351 return 0;
352 }
353
rxe_destroy_srq(struct ib_srq * ibsrq,struct ib_udata * udata)354 static int rxe_destroy_srq(struct ib_srq *ibsrq, struct ib_udata *udata)
355 {
356 struct rxe_srq *srq = to_rsrq(ibsrq);
357
358 rxe_cleanup(srq);
359 return 0;
360 }
361
rxe_post_srq_recv(struct ib_srq * ibsrq,const struct ib_recv_wr * wr,const struct ib_recv_wr ** bad_wr)362 static int rxe_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
363 const struct ib_recv_wr **bad_wr)
364 {
365 int err = 0;
366 struct rxe_srq *srq = to_rsrq(ibsrq);
367 unsigned long flags;
368
369 spin_lock_irqsave(&srq->rq.producer_lock, flags);
370
371 while (wr) {
372 err = post_one_recv(&srq->rq, wr);
373 if (unlikely(err))
374 break;
375 wr = wr->next;
376 }
377
378 spin_unlock_irqrestore(&srq->rq.producer_lock, flags);
379
380 if (err)
381 *bad_wr = wr;
382
383 return err;
384 }
385
rxe_create_qp(struct ib_qp * ibqp,struct ib_qp_init_attr * init,struct ib_udata * udata)386 static int rxe_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init,
387 struct ib_udata *udata)
388 {
389 int err;
390 struct rxe_dev *rxe = to_rdev(ibqp->device);
391 struct rxe_pd *pd = to_rpd(ibqp->pd);
392 struct rxe_qp *qp = to_rqp(ibqp);
393 struct rxe_create_qp_resp __user *uresp = NULL;
394
395 if (udata) {
396 if (udata->outlen < sizeof(*uresp))
397 return -EINVAL;
398 uresp = udata->outbuf;
399 }
400
401 if (init->create_flags)
402 return -EOPNOTSUPP;
403
404 err = rxe_qp_chk_init(rxe, init);
405 if (err)
406 return err;
407
408 if (udata) {
409 if (udata->inlen)
410 return -EINVAL;
411
412 qp->is_user = true;
413 } else {
414 qp->is_user = false;
415 }
416
417 err = rxe_add_to_pool(&rxe->qp_pool, qp);
418 if (err)
419 return err;
420
421 err = rxe_qp_from_init(rxe, qp, pd, init, uresp, ibqp->pd, udata);
422 if (err)
423 goto qp_init;
424
425 rxe_finalize(qp);
426 return 0;
427
428 qp_init:
429 rxe_cleanup(qp);
430 return err;
431 }
432
rxe_modify_qp(struct ib_qp * ibqp,struct ib_qp_attr * attr,int mask,struct ib_udata * udata)433 static int rxe_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
434 int mask, struct ib_udata *udata)
435 {
436 int err;
437 struct rxe_dev *rxe = to_rdev(ibqp->device);
438 struct rxe_qp *qp = to_rqp(ibqp);
439
440 if (mask & ~IB_QP_ATTR_STANDARD_BITS)
441 return -EOPNOTSUPP;
442
443 err = rxe_qp_chk_attr(rxe, qp, attr, mask);
444 if (err)
445 return err;
446
447 err = rxe_qp_from_attr(qp, attr, mask, udata);
448 if (err)
449 return err;
450
451 if ((mask & IB_QP_AV) && (attr->ah_attr.ah_flags & IB_AH_GRH))
452 qp->src_port = rdma_get_udp_sport(attr->ah_attr.grh.flow_label,
453 qp->ibqp.qp_num,
454 qp->attr.dest_qp_num);
455
456 return 0;
457 }
458
rxe_query_qp(struct ib_qp * ibqp,struct ib_qp_attr * attr,int mask,struct ib_qp_init_attr * init)459 static int rxe_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
460 int mask, struct ib_qp_init_attr *init)
461 {
462 struct rxe_qp *qp = to_rqp(ibqp);
463
464 rxe_qp_to_init(qp, init);
465 rxe_qp_to_attr(qp, attr, mask);
466
467 return 0;
468 }
469
rxe_destroy_qp(struct ib_qp * ibqp,struct ib_udata * udata)470 static int rxe_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata)
471 {
472 struct rxe_qp *qp = to_rqp(ibqp);
473 int ret;
474
475 ret = rxe_qp_chk_destroy(qp);
476 if (ret)
477 return ret;
478
479 rxe_cleanup(qp);
480 return 0;
481 }
482
validate_send_wr(struct rxe_qp * qp,const struct ib_send_wr * ibwr,unsigned int mask,unsigned int length)483 static int validate_send_wr(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
484 unsigned int mask, unsigned int length)
485 {
486 int num_sge = ibwr->num_sge;
487 struct rxe_sq *sq = &qp->sq;
488
489 if (unlikely(num_sge > sq->max_sge))
490 return -EINVAL;
491
492 if (unlikely(mask & WR_ATOMIC_MASK)) {
493 if (length < 8)
494 return -EINVAL;
495
496 if (atomic_wr(ibwr)->remote_addr & 0x7)
497 return -EINVAL;
498 }
499
500 if (unlikely((ibwr->send_flags & IB_SEND_INLINE) &&
501 (length > sq->max_inline)))
502 return -EINVAL;
503
504 return 0;
505 }
506
init_send_wr(struct rxe_qp * qp,struct rxe_send_wr * wr,const struct ib_send_wr * ibwr)507 static void init_send_wr(struct rxe_qp *qp, struct rxe_send_wr *wr,
508 const struct ib_send_wr *ibwr)
509 {
510 wr->wr_id = ibwr->wr_id;
511 wr->opcode = ibwr->opcode;
512 wr->send_flags = ibwr->send_flags;
513
514 if (qp_type(qp) == IB_QPT_UD ||
515 qp_type(qp) == IB_QPT_GSI) {
516 struct ib_ah *ibah = ud_wr(ibwr)->ah;
517
518 wr->wr.ud.remote_qpn = ud_wr(ibwr)->remote_qpn;
519 wr->wr.ud.remote_qkey = ud_wr(ibwr)->remote_qkey;
520 wr->wr.ud.ah_num = to_rah(ibah)->ah_num;
521 if (qp_type(qp) == IB_QPT_GSI)
522 wr->wr.ud.pkey_index = ud_wr(ibwr)->pkey_index;
523 if (wr->opcode == IB_WR_SEND_WITH_IMM)
524 wr->ex.imm_data = ibwr->ex.imm_data;
525 } else {
526 switch (wr->opcode) {
527 case IB_WR_RDMA_WRITE_WITH_IMM:
528 wr->ex.imm_data = ibwr->ex.imm_data;
529 fallthrough;
530 case IB_WR_RDMA_READ:
531 case IB_WR_RDMA_WRITE:
532 wr->wr.rdma.remote_addr = rdma_wr(ibwr)->remote_addr;
533 wr->wr.rdma.rkey = rdma_wr(ibwr)->rkey;
534 break;
535 case IB_WR_SEND_WITH_IMM:
536 wr->ex.imm_data = ibwr->ex.imm_data;
537 break;
538 case IB_WR_SEND_WITH_INV:
539 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
540 break;
541 case IB_WR_ATOMIC_CMP_AND_SWP:
542 case IB_WR_ATOMIC_FETCH_AND_ADD:
543 wr->wr.atomic.remote_addr =
544 atomic_wr(ibwr)->remote_addr;
545 wr->wr.atomic.compare_add =
546 atomic_wr(ibwr)->compare_add;
547 wr->wr.atomic.swap = atomic_wr(ibwr)->swap;
548 wr->wr.atomic.rkey = atomic_wr(ibwr)->rkey;
549 break;
550 case IB_WR_LOCAL_INV:
551 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
552 break;
553 case IB_WR_REG_MR:
554 wr->wr.reg.mr = reg_wr(ibwr)->mr;
555 wr->wr.reg.key = reg_wr(ibwr)->key;
556 wr->wr.reg.access = reg_wr(ibwr)->access;
557 break;
558 default:
559 break;
560 }
561 }
562 }
563
copy_inline_data_to_wqe(struct rxe_send_wqe * wqe,const struct ib_send_wr * ibwr)564 static void copy_inline_data_to_wqe(struct rxe_send_wqe *wqe,
565 const struct ib_send_wr *ibwr)
566 {
567 struct ib_sge *sge = ibwr->sg_list;
568 u8 *p = wqe->dma.inline_data;
569 int i;
570
571 for (i = 0; i < ibwr->num_sge; i++, sge++) {
572 memcpy(p, (void *)(uintptr_t)sge->addr, sge->length);
573 p += sge->length;
574 }
575 }
576
init_send_wqe(struct rxe_qp * qp,const struct ib_send_wr * ibwr,unsigned int mask,unsigned int length,struct rxe_send_wqe * wqe)577 static void init_send_wqe(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
578 unsigned int mask, unsigned int length,
579 struct rxe_send_wqe *wqe)
580 {
581 int num_sge = ibwr->num_sge;
582
583 init_send_wr(qp, &wqe->wr, ibwr);
584
585 /* local operation */
586 if (unlikely(mask & WR_LOCAL_OP_MASK)) {
587 wqe->mask = mask;
588 wqe->state = wqe_state_posted;
589 return;
590 }
591
592 if (unlikely(ibwr->send_flags & IB_SEND_INLINE))
593 copy_inline_data_to_wqe(wqe, ibwr);
594 else
595 memcpy(wqe->dma.sge, ibwr->sg_list,
596 num_sge * sizeof(struct ib_sge));
597
598 wqe->iova = mask & WR_ATOMIC_MASK ? atomic_wr(ibwr)->remote_addr :
599 mask & WR_READ_OR_WRITE_MASK ? rdma_wr(ibwr)->remote_addr : 0;
600 wqe->mask = mask;
601 wqe->dma.length = length;
602 wqe->dma.resid = length;
603 wqe->dma.num_sge = num_sge;
604 wqe->dma.cur_sge = 0;
605 wqe->dma.sge_offset = 0;
606 wqe->state = wqe_state_posted;
607 wqe->ssn = atomic_add_return(1, &qp->ssn);
608 }
609
post_one_send(struct rxe_qp * qp,const struct ib_send_wr * ibwr,unsigned int mask,u32 length)610 static int post_one_send(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
611 unsigned int mask, u32 length)
612 {
613 int err;
614 struct rxe_sq *sq = &qp->sq;
615 struct rxe_send_wqe *send_wqe;
616 unsigned long flags;
617 int full;
618
619 err = validate_send_wr(qp, ibwr, mask, length);
620 if (err)
621 return err;
622
623 spin_lock_irqsave(&qp->sq.sq_lock, flags);
624
625 full = queue_full(sq->queue, QUEUE_TYPE_FROM_ULP);
626
627 if (unlikely(full)) {
628 spin_unlock_irqrestore(&qp->sq.sq_lock, flags);
629 return -ENOMEM;
630 }
631
632 send_wqe = queue_producer_addr(sq->queue, QUEUE_TYPE_FROM_ULP);
633 init_send_wqe(qp, ibwr, mask, length, send_wqe);
634
635 queue_advance_producer(sq->queue, QUEUE_TYPE_FROM_ULP);
636
637 spin_unlock_irqrestore(&qp->sq.sq_lock, flags);
638
639 return 0;
640 }
641
rxe_post_send_kernel(struct rxe_qp * qp,const struct ib_send_wr * wr,const struct ib_send_wr ** bad_wr)642 static int rxe_post_send_kernel(struct rxe_qp *qp, const struct ib_send_wr *wr,
643 const struct ib_send_wr **bad_wr)
644 {
645 int err = 0;
646 unsigned int mask;
647 unsigned int length = 0;
648 int i;
649 struct ib_send_wr *next;
650
651 while (wr) {
652 mask = wr_opcode_mask(wr->opcode, qp);
653 if (unlikely(!mask)) {
654 err = -EINVAL;
655 *bad_wr = wr;
656 break;
657 }
658
659 if (unlikely((wr->send_flags & IB_SEND_INLINE) &&
660 !(mask & WR_INLINE_MASK))) {
661 err = -EINVAL;
662 *bad_wr = wr;
663 break;
664 }
665
666 next = wr->next;
667
668 length = 0;
669 for (i = 0; i < wr->num_sge; i++)
670 length += wr->sg_list[i].length;
671
672 err = post_one_send(qp, wr, mask, length);
673
674 if (err) {
675 *bad_wr = wr;
676 break;
677 }
678 wr = next;
679 }
680
681 rxe_sched_task(&qp->req.task);
682 if (unlikely(qp->req.state == QP_STATE_ERROR))
683 rxe_sched_task(&qp->comp.task);
684
685 return err;
686 }
687
rxe_post_send(struct ib_qp * ibqp,const struct ib_send_wr * wr,const struct ib_send_wr ** bad_wr)688 static int rxe_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
689 const struct ib_send_wr **bad_wr)
690 {
691 struct rxe_qp *qp = to_rqp(ibqp);
692
693 if (unlikely(!qp->valid)) {
694 *bad_wr = wr;
695 return -EINVAL;
696 }
697
698 if (unlikely(qp->req.state < QP_STATE_READY)) {
699 *bad_wr = wr;
700 return -EINVAL;
701 }
702
703 if (qp->is_user) {
704 /* Utilize process context to do protocol processing */
705 rxe_run_task(&qp->req.task);
706 return 0;
707 } else
708 return rxe_post_send_kernel(qp, wr, bad_wr);
709 }
710
rxe_post_recv(struct ib_qp * ibqp,const struct ib_recv_wr * wr,const struct ib_recv_wr ** bad_wr)711 static int rxe_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
712 const struct ib_recv_wr **bad_wr)
713 {
714 int err = 0;
715 struct rxe_qp *qp = to_rqp(ibqp);
716 struct rxe_rq *rq = &qp->rq;
717 unsigned long flags;
718
719 if (unlikely((qp_state(qp) < IB_QPS_INIT) || !qp->valid)) {
720 *bad_wr = wr;
721 return -EINVAL;
722 }
723
724 if (unlikely(qp->srq)) {
725 *bad_wr = wr;
726 return -EINVAL;
727 }
728
729 spin_lock_irqsave(&rq->producer_lock, flags);
730
731 while (wr) {
732 err = post_one_recv(rq, wr);
733 if (unlikely(err)) {
734 *bad_wr = wr;
735 break;
736 }
737 wr = wr->next;
738 }
739
740 spin_unlock_irqrestore(&rq->producer_lock, flags);
741
742 if (qp->resp.state == QP_STATE_ERROR)
743 rxe_sched_task(&qp->resp.task);
744
745 return err;
746 }
747
rxe_create_cq(struct ib_cq * ibcq,const struct ib_cq_init_attr * attr,struct ib_udata * udata)748 static int rxe_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
749 struct ib_udata *udata)
750 {
751 int err;
752 struct ib_device *dev = ibcq->device;
753 struct rxe_dev *rxe = to_rdev(dev);
754 struct rxe_cq *cq = to_rcq(ibcq);
755 struct rxe_create_cq_resp __user *uresp = NULL;
756
757 if (udata) {
758 if (udata->outlen < sizeof(*uresp))
759 return -EINVAL;
760 uresp = udata->outbuf;
761 }
762
763 if (attr->flags)
764 return -EOPNOTSUPP;
765
766 err = rxe_cq_chk_attr(rxe, NULL, attr->cqe, attr->comp_vector);
767 if (err)
768 return err;
769
770 err = rxe_cq_from_init(rxe, cq, attr->cqe, attr->comp_vector, udata,
771 uresp);
772 if (err)
773 return err;
774
775 return rxe_add_to_pool(&rxe->cq_pool, cq);
776 }
777
rxe_destroy_cq(struct ib_cq * ibcq,struct ib_udata * udata)778 static int rxe_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata)
779 {
780 struct rxe_cq *cq = to_rcq(ibcq);
781
782 /* See IBA C11-17: The CI shall return an error if this Verb is
783 * invoked while a Work Queue is still associated with the CQ.
784 */
785 if (atomic_read(&cq->num_wq))
786 return -EINVAL;
787
788 rxe_cq_disable(cq);
789
790 rxe_cleanup(cq);
791 return 0;
792 }
793
rxe_resize_cq(struct ib_cq * ibcq,int cqe,struct ib_udata * udata)794 static int rxe_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
795 {
796 int err;
797 struct rxe_cq *cq = to_rcq(ibcq);
798 struct rxe_dev *rxe = to_rdev(ibcq->device);
799 struct rxe_resize_cq_resp __user *uresp = NULL;
800
801 if (udata) {
802 if (udata->outlen < sizeof(*uresp))
803 return -EINVAL;
804 uresp = udata->outbuf;
805 }
806
807 err = rxe_cq_chk_attr(rxe, cq, cqe, 0);
808 if (err)
809 return err;
810
811 return rxe_cq_resize_queue(cq, cqe, uresp, udata);
812 }
813
rxe_poll_cq(struct ib_cq * ibcq,int num_entries,struct ib_wc * wc)814 static int rxe_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
815 {
816 int i;
817 struct rxe_cq *cq = to_rcq(ibcq);
818 struct rxe_cqe *cqe;
819 unsigned long flags;
820
821 spin_lock_irqsave(&cq->cq_lock, flags);
822 for (i = 0; i < num_entries; i++) {
823 cqe = queue_head(cq->queue, QUEUE_TYPE_TO_ULP);
824 if (!cqe)
825 break;
826
827 memcpy(wc++, &cqe->ibwc, sizeof(*wc));
828 queue_advance_consumer(cq->queue, QUEUE_TYPE_TO_ULP);
829 }
830 spin_unlock_irqrestore(&cq->cq_lock, flags);
831
832 return i;
833 }
834
rxe_peek_cq(struct ib_cq * ibcq,int wc_cnt)835 static int rxe_peek_cq(struct ib_cq *ibcq, int wc_cnt)
836 {
837 struct rxe_cq *cq = to_rcq(ibcq);
838 int count;
839
840 count = queue_count(cq->queue, QUEUE_TYPE_TO_ULP);
841
842 return (count > wc_cnt) ? wc_cnt : count;
843 }
844
rxe_req_notify_cq(struct ib_cq * ibcq,enum ib_cq_notify_flags flags)845 static int rxe_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
846 {
847 struct rxe_cq *cq = to_rcq(ibcq);
848 int ret = 0;
849 int empty;
850 unsigned long irq_flags;
851
852 spin_lock_irqsave(&cq->cq_lock, irq_flags);
853 if (cq->notify != IB_CQ_NEXT_COMP)
854 cq->notify = flags & IB_CQ_SOLICITED_MASK;
855
856 empty = queue_empty(cq->queue, QUEUE_TYPE_TO_ULP);
857
858 if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !empty)
859 ret = 1;
860
861 spin_unlock_irqrestore(&cq->cq_lock, irq_flags);
862
863 return ret;
864 }
865
rxe_get_dma_mr(struct ib_pd * ibpd,int access)866 static struct ib_mr *rxe_get_dma_mr(struct ib_pd *ibpd, int access)
867 {
868 struct rxe_dev *rxe = to_rdev(ibpd->device);
869 struct rxe_pd *pd = to_rpd(ibpd);
870 struct rxe_mr *mr;
871
872 mr = rxe_alloc(&rxe->mr_pool);
873 if (!mr)
874 return ERR_PTR(-ENOMEM);
875
876 rxe_get(pd);
877 mr->ibmr.pd = ibpd;
878
879 rxe_mr_init_dma(access, mr);
880 rxe_finalize(mr);
881
882 return &mr->ibmr;
883 }
884
rxe_reg_user_mr(struct ib_pd * ibpd,u64 start,u64 length,u64 iova,int access,struct ib_udata * udata)885 static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd,
886 u64 start,
887 u64 length,
888 u64 iova,
889 int access, struct ib_udata *udata)
890 {
891 int err;
892 struct rxe_dev *rxe = to_rdev(ibpd->device);
893 struct rxe_pd *pd = to_rpd(ibpd);
894 struct rxe_mr *mr;
895
896 mr = rxe_alloc(&rxe->mr_pool);
897 if (!mr)
898 return ERR_PTR(-ENOMEM);
899
900 rxe_get(pd);
901 mr->ibmr.pd = ibpd;
902
903 err = rxe_mr_init_user(rxe, start, length, iova, access, mr);
904 if (err)
905 goto err1;
906
907 rxe_finalize(mr);
908
909 return &mr->ibmr;
910
911 err1:
912 rxe_cleanup(mr);
913 return ERR_PTR(err);
914 }
915
rxe_alloc_mr(struct ib_pd * ibpd,enum ib_mr_type mr_type,u32 max_num_sg)916 static struct ib_mr *rxe_alloc_mr(struct ib_pd *ibpd, enum ib_mr_type mr_type,
917 u32 max_num_sg)
918 {
919 struct rxe_dev *rxe = to_rdev(ibpd->device);
920 struct rxe_pd *pd = to_rpd(ibpd);
921 struct rxe_mr *mr;
922 int err;
923
924 if (mr_type != IB_MR_TYPE_MEM_REG)
925 return ERR_PTR(-EINVAL);
926
927 mr = rxe_alloc(&rxe->mr_pool);
928 if (!mr)
929 return ERR_PTR(-ENOMEM);
930
931 rxe_get(pd);
932 mr->ibmr.pd = ibpd;
933
934 err = rxe_mr_init_fast(max_num_sg, mr);
935 if (err)
936 goto err1;
937
938 rxe_finalize(mr);
939
940 return &mr->ibmr;
941
942 err1:
943 rxe_cleanup(mr);
944 return ERR_PTR(err);
945 }
946
rxe_set_page(struct ib_mr * ibmr,u64 addr)947 static int rxe_set_page(struct ib_mr *ibmr, u64 addr)
948 {
949 struct rxe_mr *mr = to_rmr(ibmr);
950 struct rxe_map *map;
951 struct rxe_phys_buf *buf;
952
953 if (unlikely(mr->nbuf == mr->num_buf))
954 return -ENOMEM;
955
956 map = mr->map[mr->nbuf / RXE_BUF_PER_MAP];
957 buf = &map->buf[mr->nbuf % RXE_BUF_PER_MAP];
958
959 buf->addr = addr;
960 buf->size = ibmr->page_size;
961 mr->nbuf++;
962
963 return 0;
964 }
965
rxe_map_mr_sg(struct ib_mr * ibmr,struct scatterlist * sg,int sg_nents,unsigned int * sg_offset)966 static int rxe_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg,
967 int sg_nents, unsigned int *sg_offset)
968 {
969 struct rxe_mr *mr = to_rmr(ibmr);
970 int n;
971
972 mr->nbuf = 0;
973
974 n = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, rxe_set_page);
975
976 mr->page_shift = ilog2(ibmr->page_size);
977 mr->page_mask = ibmr->page_size - 1;
978 mr->offset = ibmr->iova & mr->page_mask;
979
980 return n;
981 }
982
parent_show(struct device * device,struct device_attribute * attr,char * buf)983 static ssize_t parent_show(struct device *device,
984 struct device_attribute *attr, char *buf)
985 {
986 struct rxe_dev *rxe =
987 rdma_device_to_drv_device(device, struct rxe_dev, ib_dev);
988
989 return sysfs_emit(buf, "%s\n", rxe_parent_name(rxe, 1));
990 }
991
992 static DEVICE_ATTR_RO(parent);
993
994 static struct attribute *rxe_dev_attributes[] = {
995 &dev_attr_parent.attr,
996 NULL
997 };
998
999 static const struct attribute_group rxe_attr_group = {
1000 .attrs = rxe_dev_attributes,
1001 };
1002
rxe_enable_driver(struct ib_device * ib_dev)1003 static int rxe_enable_driver(struct ib_device *ib_dev)
1004 {
1005 struct rxe_dev *rxe = container_of(ib_dev, struct rxe_dev, ib_dev);
1006
1007 rxe_set_port_state(rxe);
1008 dev_info(&rxe->ib_dev.dev, "added %s\n", netdev_name(rxe->ndev));
1009 return 0;
1010 }
1011
1012 static const struct ib_device_ops rxe_dev_ops = {
1013 .owner = THIS_MODULE,
1014 .driver_id = RDMA_DRIVER_RXE,
1015 .uverbs_abi_ver = RXE_UVERBS_ABI_VERSION,
1016
1017 .alloc_hw_port_stats = rxe_ib_alloc_hw_port_stats,
1018 .alloc_mr = rxe_alloc_mr,
1019 .alloc_mw = rxe_alloc_mw,
1020 .alloc_pd = rxe_alloc_pd,
1021 .alloc_ucontext = rxe_alloc_ucontext,
1022 .attach_mcast = rxe_attach_mcast,
1023 .create_ah = rxe_create_ah,
1024 .create_cq = rxe_create_cq,
1025 .create_qp = rxe_create_qp,
1026 .create_srq = rxe_create_srq,
1027 .create_user_ah = rxe_create_ah,
1028 .dealloc_driver = rxe_dealloc,
1029 .dealloc_mw = rxe_dealloc_mw,
1030 .dealloc_pd = rxe_dealloc_pd,
1031 .dealloc_ucontext = rxe_dealloc_ucontext,
1032 .dereg_mr = rxe_dereg_mr,
1033 .destroy_ah = rxe_destroy_ah,
1034 .destroy_cq = rxe_destroy_cq,
1035 .destroy_qp = rxe_destroy_qp,
1036 .destroy_srq = rxe_destroy_srq,
1037 .detach_mcast = rxe_detach_mcast,
1038 .device_group = &rxe_attr_group,
1039 .enable_driver = rxe_enable_driver,
1040 .get_dma_mr = rxe_get_dma_mr,
1041 .get_hw_stats = rxe_ib_get_hw_stats,
1042 .get_link_layer = rxe_get_link_layer,
1043 .get_port_immutable = rxe_port_immutable,
1044 .map_mr_sg = rxe_map_mr_sg,
1045 .mmap = rxe_mmap,
1046 .modify_ah = rxe_modify_ah,
1047 .modify_device = rxe_modify_device,
1048 .modify_port = rxe_modify_port,
1049 .modify_qp = rxe_modify_qp,
1050 .modify_srq = rxe_modify_srq,
1051 .peek_cq = rxe_peek_cq,
1052 .poll_cq = rxe_poll_cq,
1053 .post_recv = rxe_post_recv,
1054 .post_send = rxe_post_send,
1055 .post_srq_recv = rxe_post_srq_recv,
1056 .query_ah = rxe_query_ah,
1057 .query_device = rxe_query_device,
1058 .query_pkey = rxe_query_pkey,
1059 .query_port = rxe_query_port,
1060 .query_qp = rxe_query_qp,
1061 .query_srq = rxe_query_srq,
1062 .reg_user_mr = rxe_reg_user_mr,
1063 .req_notify_cq = rxe_req_notify_cq,
1064 .resize_cq = rxe_resize_cq,
1065
1066 INIT_RDMA_OBJ_SIZE(ib_ah, rxe_ah, ibah),
1067 INIT_RDMA_OBJ_SIZE(ib_cq, rxe_cq, ibcq),
1068 INIT_RDMA_OBJ_SIZE(ib_pd, rxe_pd, ibpd),
1069 INIT_RDMA_OBJ_SIZE(ib_qp, rxe_qp, ibqp),
1070 INIT_RDMA_OBJ_SIZE(ib_srq, rxe_srq, ibsrq),
1071 INIT_RDMA_OBJ_SIZE(ib_ucontext, rxe_ucontext, ibuc),
1072 INIT_RDMA_OBJ_SIZE(ib_mw, rxe_mw, ibmw),
1073 };
1074
rxe_register_device(struct rxe_dev * rxe,const char * ibdev_name)1075 int rxe_register_device(struct rxe_dev *rxe, const char *ibdev_name)
1076 {
1077 int err;
1078 struct ib_device *dev = &rxe->ib_dev;
1079
1080 strscpy(dev->node_desc, "rxe", sizeof(dev->node_desc));
1081
1082 dev->node_type = RDMA_NODE_IB_CA;
1083 dev->phys_port_cnt = 1;
1084 dev->num_comp_vectors = num_possible_cpus();
1085 dev->local_dma_lkey = 0;
1086 addrconf_addr_eui48((unsigned char *)&dev->node_guid,
1087 rxe->ndev->dev_addr);
1088
1089 dev->uverbs_cmd_mask |= BIT_ULL(IB_USER_VERBS_CMD_POST_SEND) |
1090 BIT_ULL(IB_USER_VERBS_CMD_REQ_NOTIFY_CQ);
1091
1092 ib_set_device_ops(dev, &rxe_dev_ops);
1093 err = ib_device_set_netdev(&rxe->ib_dev, rxe->ndev, 1);
1094 if (err)
1095 return err;
1096
1097 err = rxe_icrc_init(rxe);
1098 if (err)
1099 return err;
1100
1101 err = ib_register_device(dev, ibdev_name, NULL);
1102 if (err)
1103 pr_warn("%s failed with error %d\n", __func__, err);
1104
1105 /*
1106 * Note that rxe may be invalid at this point if another thread
1107 * unregistered it.
1108 */
1109 return err;
1110 }
1111