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
15 static int post_one_recv(struct rxe_rq *rq, const struct ib_recv_wr *ibwr);
16
17 /* dev */
rxe_query_device(struct ib_device * ibdev,struct ib_device_attr * attr,struct ib_udata * udata)18 static int rxe_query_device(struct ib_device *ibdev,
19 struct ib_device_attr *attr,
20 struct ib_udata *udata)
21 {
22 struct rxe_dev *rxe = to_rdev(ibdev);
23 int err;
24
25 if (udata->inlen || udata->outlen) {
26 rxe_dbg_dev(rxe, "malformed udata");
27 err = -EINVAL;
28 goto err_out;
29 }
30
31 memcpy(attr, &rxe->attr, sizeof(*attr));
32
33 return 0;
34
35 err_out:
36 rxe_err_dev(rxe, "returned err = %d", err);
37 return err;
38 }
39
rxe_query_port(struct ib_device * ibdev,u32 port_num,struct ib_port_attr * attr)40 static int rxe_query_port(struct ib_device *ibdev,
41 u32 port_num, struct ib_port_attr *attr)
42 {
43 struct rxe_dev *rxe = to_rdev(ibdev);
44 int err, ret;
45
46 if (port_num != 1) {
47 err = -EINVAL;
48 rxe_dbg_dev(rxe, "bad port_num = %d", port_num);
49 goto err_out;
50 }
51
52 memcpy(attr, &rxe->port.attr, sizeof(*attr));
53
54 mutex_lock(&rxe->usdev_lock);
55 ret = ib_get_eth_speed(ibdev, port_num, &attr->active_speed,
56 &attr->active_width);
57
58 if (attr->state == IB_PORT_ACTIVE)
59 attr->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
60 else if (dev_get_flags(rxe->ndev) & IFF_UP)
61 attr->phys_state = IB_PORT_PHYS_STATE_POLLING;
62 else
63 attr->phys_state = IB_PORT_PHYS_STATE_DISABLED;
64
65 mutex_unlock(&rxe->usdev_lock);
66
67 return ret;
68
69 err_out:
70 rxe_err_dev(rxe, "returned err = %d", err);
71 return err;
72 }
73
rxe_query_pkey(struct ib_device * ibdev,u32 port_num,u16 index,u16 * pkey)74 static int rxe_query_pkey(struct ib_device *ibdev,
75 u32 port_num, u16 index, u16 *pkey)
76 {
77 struct rxe_dev *rxe = to_rdev(ibdev);
78 int err;
79
80 if (index != 0) {
81 err = -EINVAL;
82 rxe_dbg_dev(rxe, "bad pkey index = %d", index);
83 goto err_out;
84 }
85
86 *pkey = IB_DEFAULT_PKEY_FULL;
87 return 0;
88
89 err_out:
90 rxe_err_dev(rxe, "returned err = %d", err);
91 return err;
92 }
93
rxe_modify_device(struct ib_device * ibdev,int mask,struct ib_device_modify * attr)94 static int rxe_modify_device(struct ib_device *ibdev,
95 int mask, struct ib_device_modify *attr)
96 {
97 struct rxe_dev *rxe = to_rdev(ibdev);
98 int err;
99
100 if (mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
101 IB_DEVICE_MODIFY_NODE_DESC)) {
102 err = -EOPNOTSUPP;
103 rxe_dbg_dev(rxe, "unsupported mask = 0x%x", mask);
104 goto err_out;
105 }
106
107 if (mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID)
108 rxe->attr.sys_image_guid = cpu_to_be64(attr->sys_image_guid);
109
110 if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
111 memcpy(rxe->ib_dev.node_desc,
112 attr->node_desc, sizeof(rxe->ib_dev.node_desc));
113 }
114
115 return 0;
116
117 err_out:
118 rxe_err_dev(rxe, "returned err = %d", err);
119 return err;
120 }
121
rxe_modify_port(struct ib_device * ibdev,u32 port_num,int mask,struct ib_port_modify * attr)122 static int rxe_modify_port(struct ib_device *ibdev, u32 port_num,
123 int mask, struct ib_port_modify *attr)
124 {
125 struct rxe_dev *rxe = to_rdev(ibdev);
126 struct rxe_port *port;
127 int err;
128
129 if (port_num != 1) {
130 err = -EINVAL;
131 rxe_dbg_dev(rxe, "bad port_num = %d", port_num);
132 goto err_out;
133 }
134
135 //TODO is shutdown useful
136 if (mask & ~(IB_PORT_RESET_QKEY_CNTR)) {
137 err = -EOPNOTSUPP;
138 rxe_dbg_dev(rxe, "unsupported mask = 0x%x", mask);
139 goto err_out;
140 }
141
142 port = &rxe->port;
143 port->attr.port_cap_flags |= attr->set_port_cap_mask;
144 port->attr.port_cap_flags &= ~attr->clr_port_cap_mask;
145
146 if (mask & IB_PORT_RESET_QKEY_CNTR)
147 port->attr.qkey_viol_cntr = 0;
148
149 return 0;
150
151 err_out:
152 rxe_err_dev(rxe, "returned err = %d", err);
153 return err;
154 }
155
rxe_get_link_layer(struct ib_device * ibdev,u32 port_num)156 static enum rdma_link_layer rxe_get_link_layer(struct ib_device *ibdev,
157 u32 port_num)
158 {
159 struct rxe_dev *rxe = to_rdev(ibdev);
160 int err;
161
162 if (port_num != 1) {
163 err = -EINVAL;
164 rxe_dbg_dev(rxe, "bad port_num = %d", port_num);
165 goto err_out;
166 }
167
168 return IB_LINK_LAYER_ETHERNET;
169
170 err_out:
171 rxe_err_dev(rxe, "returned err = %d", err);
172 return err;
173 }
174
rxe_port_immutable(struct ib_device * ibdev,u32 port_num,struct ib_port_immutable * immutable)175 static int rxe_port_immutable(struct ib_device *ibdev, u32 port_num,
176 struct ib_port_immutable *immutable)
177 {
178 struct rxe_dev *rxe = to_rdev(ibdev);
179 struct ib_port_attr attr = {};
180 int err;
181
182 if (port_num != 1) {
183 err = -EINVAL;
184 rxe_dbg_dev(rxe, "bad port_num = %d", port_num);
185 goto err_out;
186 }
187
188 err = ib_query_port(ibdev, port_num, &attr);
189 if (err)
190 goto err_out;
191
192 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
193 immutable->pkey_tbl_len = attr.pkey_tbl_len;
194 immutable->gid_tbl_len = attr.gid_tbl_len;
195 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
196
197 return 0;
198
199 err_out:
200 rxe_err_dev(rxe, "returned err = %d", err);
201 return err;
202 }
203
204 /* uc */
rxe_alloc_ucontext(struct ib_ucontext * ibuc,struct ib_udata * udata)205 static int rxe_alloc_ucontext(struct ib_ucontext *ibuc, struct ib_udata *udata)
206 {
207 struct rxe_dev *rxe = to_rdev(ibuc->device);
208 struct rxe_ucontext *uc = to_ruc(ibuc);
209 int err;
210
211 err = rxe_add_to_pool(&rxe->uc_pool, uc);
212 if (err)
213 rxe_err_dev(rxe, "unable to create uc");
214
215 return err;
216 }
217
rxe_dealloc_ucontext(struct ib_ucontext * ibuc)218 static void rxe_dealloc_ucontext(struct ib_ucontext *ibuc)
219 {
220 struct rxe_ucontext *uc = to_ruc(ibuc);
221 int err;
222
223 err = rxe_cleanup(uc);
224 if (err)
225 rxe_err_uc(uc, "cleanup failed, err = %d", err);
226 }
227
228 /* pd */
rxe_alloc_pd(struct ib_pd * ibpd,struct ib_udata * udata)229 static int rxe_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
230 {
231 struct rxe_dev *rxe = to_rdev(ibpd->device);
232 struct rxe_pd *pd = to_rpd(ibpd);
233 int err;
234
235 err = rxe_add_to_pool(&rxe->pd_pool, pd);
236 if (err) {
237 rxe_dbg_dev(rxe, "unable to alloc pd");
238 goto err_out;
239 }
240
241 return 0;
242
243 err_out:
244 rxe_err_dev(rxe, "returned err = %d", err);
245 return err;
246 }
247
rxe_dealloc_pd(struct ib_pd * ibpd,struct ib_udata * udata)248 static int rxe_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
249 {
250 struct rxe_pd *pd = to_rpd(ibpd);
251 int err;
252
253 err = rxe_cleanup(pd);
254 if (err)
255 rxe_err_pd(pd, "cleanup failed, err = %d", err);
256
257 return 0;
258 }
259
260 /* ah */
rxe_create_ah(struct ib_ah * ibah,struct rdma_ah_init_attr * init_attr,struct ib_udata * udata)261 static int rxe_create_ah(struct ib_ah *ibah,
262 struct rdma_ah_init_attr *init_attr,
263 struct ib_udata *udata)
264 {
265 struct rxe_dev *rxe = to_rdev(ibah->device);
266 struct rxe_ah *ah = to_rah(ibah);
267 struct rxe_create_ah_resp __user *uresp = NULL;
268 int err, cleanup_err;
269
270 if (udata) {
271 /* test if new user provider */
272 if (udata->outlen >= sizeof(*uresp))
273 uresp = udata->outbuf;
274 ah->is_user = true;
275 } else {
276 ah->is_user = false;
277 }
278
279 err = rxe_add_to_pool_ah(&rxe->ah_pool, ah,
280 init_attr->flags & RDMA_CREATE_AH_SLEEPABLE);
281 if (err) {
282 rxe_dbg_dev(rxe, "unable to create ah");
283 goto err_out;
284 }
285
286 /* create index > 0 */
287 ah->ah_num = ah->elem.index;
288
289 err = rxe_ah_chk_attr(ah, init_attr->ah_attr);
290 if (err) {
291 rxe_dbg_ah(ah, "bad attr");
292 goto err_cleanup;
293 }
294
295 if (uresp) {
296 /* only if new user provider */
297 err = copy_to_user(&uresp->ah_num, &ah->ah_num,
298 sizeof(uresp->ah_num));
299 if (err) {
300 err = -EFAULT;
301 rxe_dbg_ah(ah, "unable to copy to user");
302 goto err_cleanup;
303 }
304 } else if (ah->is_user) {
305 /* only if old user provider */
306 ah->ah_num = 0;
307 }
308
309 rxe_init_av(init_attr->ah_attr, &ah->av);
310 rxe_finalize(ah);
311
312 return 0;
313
314 err_cleanup:
315 cleanup_err = rxe_cleanup(ah);
316 if (cleanup_err)
317 rxe_err_ah(ah, "cleanup failed, err = %d", cleanup_err);
318 err_out:
319 rxe_err_ah(ah, "returned err = %d", err);
320 return err;
321 }
322
rxe_modify_ah(struct ib_ah * ibah,struct rdma_ah_attr * attr)323 static int rxe_modify_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
324 {
325 struct rxe_ah *ah = to_rah(ibah);
326 int err;
327
328 err = rxe_ah_chk_attr(ah, attr);
329 if (err) {
330 rxe_dbg_ah(ah, "bad attr");
331 goto err_out;
332 }
333
334 rxe_init_av(attr, &ah->av);
335
336 return 0;
337
338 err_out:
339 rxe_err_ah(ah, "returned err = %d", err);
340 return err;
341 }
342
rxe_query_ah(struct ib_ah * ibah,struct rdma_ah_attr * attr)343 static int rxe_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *attr)
344 {
345 struct rxe_ah *ah = to_rah(ibah);
346
347 memset(attr, 0, sizeof(*attr));
348 attr->type = ibah->type;
349 rxe_av_to_attr(&ah->av, attr);
350
351 return 0;
352 }
353
rxe_destroy_ah(struct ib_ah * ibah,u32 flags)354 static int rxe_destroy_ah(struct ib_ah *ibah, u32 flags)
355 {
356 struct rxe_ah *ah = to_rah(ibah);
357 int err;
358
359 err = rxe_cleanup_ah(ah, flags & RDMA_DESTROY_AH_SLEEPABLE);
360 if (err)
361 rxe_err_ah(ah, "cleanup failed, err = %d", err);
362
363 return 0;
364 }
365
366 /* srq */
rxe_create_srq(struct ib_srq * ibsrq,struct ib_srq_init_attr * init,struct ib_udata * udata)367 static int rxe_create_srq(struct ib_srq *ibsrq, struct ib_srq_init_attr *init,
368 struct ib_udata *udata)
369 {
370 struct rxe_dev *rxe = to_rdev(ibsrq->device);
371 struct rxe_pd *pd = to_rpd(ibsrq->pd);
372 struct rxe_srq *srq = to_rsrq(ibsrq);
373 struct rxe_create_srq_resp __user *uresp = NULL;
374 int err, cleanup_err;
375
376 if (udata) {
377 if (udata->outlen < sizeof(*uresp)) {
378 err = -EINVAL;
379 rxe_err_dev(rxe, "malformed udata");
380 goto err_out;
381 }
382 uresp = udata->outbuf;
383 }
384
385 if (init->srq_type != IB_SRQT_BASIC) {
386 err = -EOPNOTSUPP;
387 rxe_dbg_dev(rxe, "srq type = %d, not supported",
388 init->srq_type);
389 goto err_out;
390 }
391
392 err = rxe_srq_chk_init(rxe, init);
393 if (err) {
394 rxe_dbg_dev(rxe, "invalid init attributes");
395 goto err_out;
396 }
397
398 err = rxe_add_to_pool(&rxe->srq_pool, srq);
399 if (err) {
400 rxe_dbg_dev(rxe, "unable to create srq, err = %d", err);
401 goto err_out;
402 }
403
404 rxe_get(pd);
405 srq->pd = pd;
406
407 err = rxe_srq_from_init(rxe, srq, init, udata, uresp);
408 if (err) {
409 rxe_dbg_srq(srq, "create srq failed, err = %d", err);
410 goto err_cleanup;
411 }
412
413 return 0;
414
415 err_cleanup:
416 cleanup_err = rxe_cleanup(srq);
417 if (cleanup_err)
418 rxe_err_srq(srq, "cleanup failed, err = %d", cleanup_err);
419 err_out:
420 rxe_err_dev(rxe, "returned err = %d", err);
421 return err;
422 }
423
rxe_modify_srq(struct ib_srq * ibsrq,struct ib_srq_attr * attr,enum ib_srq_attr_mask mask,struct ib_udata * udata)424 static int rxe_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
425 enum ib_srq_attr_mask mask,
426 struct ib_udata *udata)
427 {
428 struct rxe_srq *srq = to_rsrq(ibsrq);
429 struct rxe_dev *rxe = to_rdev(ibsrq->device);
430 struct rxe_modify_srq_cmd cmd = {};
431 int err;
432
433 if (udata) {
434 if (udata->inlen < sizeof(cmd)) {
435 err = -EINVAL;
436 rxe_dbg_srq(srq, "malformed udata");
437 goto err_out;
438 }
439
440 err = ib_copy_from_udata(&cmd, udata, sizeof(cmd));
441 if (err) {
442 err = -EFAULT;
443 rxe_dbg_srq(srq, "unable to read udata");
444 goto err_out;
445 }
446 }
447
448 err = rxe_srq_chk_attr(rxe, srq, attr, mask);
449 if (err) {
450 rxe_dbg_srq(srq, "bad init attributes");
451 goto err_out;
452 }
453
454 err = rxe_srq_from_attr(rxe, srq, attr, mask, &cmd, udata);
455 if (err) {
456 rxe_dbg_srq(srq, "bad attr");
457 goto err_out;
458 }
459
460 return 0;
461
462 err_out:
463 rxe_err_srq(srq, "returned err = %d", err);
464 return err;
465 }
466
rxe_query_srq(struct ib_srq * ibsrq,struct ib_srq_attr * attr)467 static int rxe_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
468 {
469 struct rxe_srq *srq = to_rsrq(ibsrq);
470 int err;
471
472 if (srq->error) {
473 err = -EINVAL;
474 rxe_dbg_srq(srq, "srq in error state");
475 goto err_out;
476 }
477
478 attr->max_wr = srq->rq.queue->buf->index_mask;
479 attr->max_sge = srq->rq.max_sge;
480 attr->srq_limit = srq->limit;
481 return 0;
482
483 err_out:
484 rxe_err_srq(srq, "returned err = %d", err);
485 return err;
486 }
487
rxe_post_srq_recv(struct ib_srq * ibsrq,const struct ib_recv_wr * wr,const struct ib_recv_wr ** bad_wr)488 static int rxe_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
489 const struct ib_recv_wr **bad_wr)
490 {
491 int err = 0;
492 struct rxe_srq *srq = to_rsrq(ibsrq);
493 unsigned long flags;
494
495 spin_lock_irqsave(&srq->rq.producer_lock, flags);
496
497 while (wr) {
498 err = post_one_recv(&srq->rq, wr);
499 if (unlikely(err))
500 break;
501 wr = wr->next;
502 }
503
504 spin_unlock_irqrestore(&srq->rq.producer_lock, flags);
505
506 if (err) {
507 *bad_wr = wr;
508 rxe_err_srq(srq, "returned err = %d", err);
509 }
510
511 return err;
512 }
513
rxe_destroy_srq(struct ib_srq * ibsrq,struct ib_udata * udata)514 static int rxe_destroy_srq(struct ib_srq *ibsrq, struct ib_udata *udata)
515 {
516 struct rxe_srq *srq = to_rsrq(ibsrq);
517 int err;
518
519 err = rxe_cleanup(srq);
520 if (err)
521 rxe_err_srq(srq, "cleanup failed, err = %d", err);
522
523 return 0;
524 }
525
526 /* qp */
rxe_create_qp(struct ib_qp * ibqp,struct ib_qp_init_attr * init,struct ib_udata * udata)527 static int rxe_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init,
528 struct ib_udata *udata)
529 {
530 struct rxe_dev *rxe = to_rdev(ibqp->device);
531 struct rxe_pd *pd = to_rpd(ibqp->pd);
532 struct rxe_qp *qp = to_rqp(ibqp);
533 struct rxe_create_qp_resp __user *uresp = NULL;
534 int err, cleanup_err;
535
536 if (udata) {
537 if (udata->inlen) {
538 err = -EINVAL;
539 rxe_dbg_dev(rxe, "malformed udata, err = %d", err);
540 goto err_out;
541 }
542
543 if (udata->outlen < sizeof(*uresp)) {
544 err = -EINVAL;
545 rxe_dbg_dev(rxe, "malformed udata, err = %d", err);
546 goto err_out;
547 }
548
549 qp->is_user = true;
550 uresp = udata->outbuf;
551 } else {
552 qp->is_user = false;
553 }
554
555 if (init->create_flags) {
556 err = -EOPNOTSUPP;
557 rxe_dbg_dev(rxe, "unsupported create_flags, err = %d", err);
558 goto err_out;
559 }
560
561 err = rxe_qp_chk_init(rxe, init);
562 if (err) {
563 rxe_dbg_dev(rxe, "bad init attr, err = %d", err);
564 goto err_out;
565 }
566
567 err = rxe_add_to_pool(&rxe->qp_pool, qp);
568 if (err) {
569 rxe_dbg_dev(rxe, "unable to create qp, err = %d", err);
570 goto err_out;
571 }
572
573 err = rxe_qp_from_init(rxe, qp, pd, init, uresp, ibqp->pd, udata);
574 if (err) {
575 rxe_dbg_qp(qp, "create qp failed, err = %d", err);
576 goto err_cleanup;
577 }
578
579 rxe_finalize(qp);
580 return 0;
581
582 err_cleanup:
583 cleanup_err = rxe_cleanup(qp);
584 if (cleanup_err)
585 rxe_err_qp(qp, "cleanup failed, err = %d", cleanup_err);
586 err_out:
587 rxe_err_dev(rxe, "returned err = %d", err);
588 return err;
589 }
590
rxe_modify_qp(struct ib_qp * ibqp,struct ib_qp_attr * attr,int mask,struct ib_udata * udata)591 static int rxe_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
592 int mask, struct ib_udata *udata)
593 {
594 struct rxe_dev *rxe = to_rdev(ibqp->device);
595 struct rxe_qp *qp = to_rqp(ibqp);
596 int err;
597
598 if (mask & ~IB_QP_ATTR_STANDARD_BITS) {
599 err = -EOPNOTSUPP;
600 rxe_dbg_qp(qp, "unsupported mask = 0x%x, err = %d",
601 mask, err);
602 goto err_out;
603 }
604
605 err = rxe_qp_chk_attr(rxe, qp, attr, mask);
606 if (err) {
607 rxe_dbg_qp(qp, "bad mask/attr, err = %d", err);
608 goto err_out;
609 }
610
611 err = rxe_qp_from_attr(qp, attr, mask, udata);
612 if (err) {
613 rxe_dbg_qp(qp, "modify qp failed, err = %d", err);
614 goto err_out;
615 }
616
617 if ((mask & IB_QP_AV) && (attr->ah_attr.ah_flags & IB_AH_GRH))
618 qp->src_port = rdma_get_udp_sport(attr->ah_attr.grh.flow_label,
619 qp->ibqp.qp_num,
620 qp->attr.dest_qp_num);
621
622 return 0;
623
624 err_out:
625 rxe_err_qp(qp, "returned err = %d", err);
626 return err;
627 }
628
rxe_query_qp(struct ib_qp * ibqp,struct ib_qp_attr * attr,int mask,struct ib_qp_init_attr * init)629 static int rxe_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
630 int mask, struct ib_qp_init_attr *init)
631 {
632 struct rxe_qp *qp = to_rqp(ibqp);
633
634 rxe_qp_to_init(qp, init);
635 rxe_qp_to_attr(qp, attr, mask);
636
637 return 0;
638 }
639
rxe_destroy_qp(struct ib_qp * ibqp,struct ib_udata * udata)640 static int rxe_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata)
641 {
642 struct rxe_qp *qp = to_rqp(ibqp);
643 int err;
644
645 err = rxe_qp_chk_destroy(qp);
646 if (err) {
647 rxe_dbg_qp(qp, "unable to destroy qp, err = %d", err);
648 goto err_out;
649 }
650
651 err = rxe_cleanup(qp);
652 if (err)
653 rxe_err_qp(qp, "cleanup failed, err = %d", err);
654
655 return 0;
656
657 err_out:
658 rxe_err_qp(qp, "returned err = %d", err);
659 return err;
660 }
661
662 /* send wr */
663
664 /* sanity check incoming send work request */
validate_send_wr(struct rxe_qp * qp,const struct ib_send_wr * ibwr,unsigned int * maskp,unsigned int * lengthp)665 static int validate_send_wr(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
666 unsigned int *maskp, unsigned int *lengthp)
667 {
668 int num_sge = ibwr->num_sge;
669 struct rxe_sq *sq = &qp->sq;
670 unsigned int mask = 0;
671 unsigned long length = 0;
672 int err = -EINVAL;
673 int i;
674
675 do {
676 mask = wr_opcode_mask(ibwr->opcode, qp);
677 if (!mask) {
678 rxe_err_qp(qp, "bad wr opcode for qp type");
679 break;
680 }
681
682 if (num_sge > sq->max_sge) {
683 rxe_err_qp(qp, "num_sge > max_sge");
684 break;
685 }
686
687 length = 0;
688 for (i = 0; i < ibwr->num_sge; i++)
689 length += ibwr->sg_list[i].length;
690
691 if (length > (1UL << 31)) {
692 rxe_err_qp(qp, "message length too long");
693 break;
694 }
695
696 if (mask & WR_ATOMIC_MASK) {
697 if (length != 8) {
698 rxe_err_qp(qp, "atomic length != 8");
699 break;
700 }
701 if (atomic_wr(ibwr)->remote_addr & 0x7) {
702 rxe_err_qp(qp, "misaligned atomic address");
703 break;
704 }
705 }
706 if (ibwr->send_flags & IB_SEND_INLINE) {
707 if (!(mask & WR_INLINE_MASK)) {
708 rxe_err_qp(qp, "opcode doesn't support inline data");
709 break;
710 }
711 if (length > sq->max_inline) {
712 rxe_err_qp(qp, "inline length too big");
713 break;
714 }
715 }
716
717 err = 0;
718 } while (0);
719
720 *maskp = mask;
721 *lengthp = (int)length;
722
723 return err;
724 }
725
init_send_wr(struct rxe_qp * qp,struct rxe_send_wr * wr,const struct ib_send_wr * ibwr)726 static int init_send_wr(struct rxe_qp *qp, struct rxe_send_wr *wr,
727 const struct ib_send_wr *ibwr)
728 {
729 wr->wr_id = ibwr->wr_id;
730 wr->opcode = ibwr->opcode;
731 wr->send_flags = ibwr->send_flags;
732
733 if (qp_type(qp) == IB_QPT_UD ||
734 qp_type(qp) == IB_QPT_GSI) {
735 struct ib_ah *ibah = ud_wr(ibwr)->ah;
736
737 wr->wr.ud.remote_qpn = ud_wr(ibwr)->remote_qpn;
738 wr->wr.ud.remote_qkey = ud_wr(ibwr)->remote_qkey;
739 wr->wr.ud.ah_num = to_rah(ibah)->ah_num;
740 if (qp_type(qp) == IB_QPT_GSI)
741 wr->wr.ud.pkey_index = ud_wr(ibwr)->pkey_index;
742
743 switch (wr->opcode) {
744 case IB_WR_SEND_WITH_IMM:
745 wr->ex.imm_data = ibwr->ex.imm_data;
746 break;
747 case IB_WR_SEND:
748 break;
749 default:
750 rxe_err_qp(qp, "bad wr opcode %d for UD/GSI QP",
751 wr->opcode);
752 return -EINVAL;
753 }
754 } else {
755 switch (wr->opcode) {
756 case IB_WR_RDMA_WRITE_WITH_IMM:
757 wr->ex.imm_data = ibwr->ex.imm_data;
758 fallthrough;
759 case IB_WR_RDMA_READ:
760 case IB_WR_RDMA_WRITE:
761 wr->wr.rdma.remote_addr = rdma_wr(ibwr)->remote_addr;
762 wr->wr.rdma.rkey = rdma_wr(ibwr)->rkey;
763 break;
764 case IB_WR_SEND_WITH_IMM:
765 wr->ex.imm_data = ibwr->ex.imm_data;
766 break;
767 case IB_WR_SEND_WITH_INV:
768 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
769 break;
770 case IB_WR_RDMA_READ_WITH_INV:
771 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
772 wr->wr.rdma.remote_addr = rdma_wr(ibwr)->remote_addr;
773 wr->wr.rdma.rkey = rdma_wr(ibwr)->rkey;
774 break;
775 case IB_WR_ATOMIC_CMP_AND_SWP:
776 case IB_WR_ATOMIC_FETCH_AND_ADD:
777 wr->wr.atomic.remote_addr =
778 atomic_wr(ibwr)->remote_addr;
779 wr->wr.atomic.compare_add =
780 atomic_wr(ibwr)->compare_add;
781 wr->wr.atomic.swap = atomic_wr(ibwr)->swap;
782 wr->wr.atomic.rkey = atomic_wr(ibwr)->rkey;
783 break;
784 case IB_WR_LOCAL_INV:
785 wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
786 break;
787 case IB_WR_REG_MR:
788 wr->wr.reg.mr = reg_wr(ibwr)->mr;
789 wr->wr.reg.key = reg_wr(ibwr)->key;
790 wr->wr.reg.access = reg_wr(ibwr)->access;
791 break;
792 case IB_WR_SEND:
793 case IB_WR_BIND_MW:
794 case IB_WR_FLUSH:
795 case IB_WR_ATOMIC_WRITE:
796 break;
797 default:
798 rxe_err_qp(qp, "unsupported wr opcode %d",
799 wr->opcode);
800 return -EINVAL;
801 }
802 }
803
804 return 0;
805 }
806
copy_inline_data_to_wqe(struct rxe_send_wqe * wqe,const struct ib_send_wr * ibwr)807 static void copy_inline_data_to_wqe(struct rxe_send_wqe *wqe,
808 const struct ib_send_wr *ibwr)
809 {
810 struct ib_sge *sge = ibwr->sg_list;
811 u8 *p = wqe->dma.inline_data;
812 int i;
813
814 for (i = 0; i < ibwr->num_sge; i++, sge++) {
815 memcpy(p, ib_virt_dma_to_ptr(sge->addr), sge->length);
816 p += sge->length;
817 }
818 }
819
init_send_wqe(struct rxe_qp * qp,const struct ib_send_wr * ibwr,unsigned int mask,unsigned int length,struct rxe_send_wqe * wqe)820 static int init_send_wqe(struct rxe_qp *qp, const struct ib_send_wr *ibwr,
821 unsigned int mask, unsigned int length,
822 struct rxe_send_wqe *wqe)
823 {
824 int num_sge = ibwr->num_sge;
825 int err;
826
827 err = init_send_wr(qp, &wqe->wr, ibwr);
828 if (err)
829 return err;
830
831 /* local operation */
832 if (unlikely(mask & WR_LOCAL_OP_MASK)) {
833 wqe->mask = mask;
834 wqe->state = wqe_state_posted;
835 return 0;
836 }
837
838 if (unlikely(ibwr->send_flags & IB_SEND_INLINE))
839 copy_inline_data_to_wqe(wqe, ibwr);
840 else
841 memcpy(wqe->dma.sge, ibwr->sg_list,
842 num_sge * sizeof(struct ib_sge));
843
844 wqe->iova = mask & WR_ATOMIC_MASK ? atomic_wr(ibwr)->remote_addr :
845 mask & WR_READ_OR_WRITE_MASK ? rdma_wr(ibwr)->remote_addr : 0;
846 wqe->mask = mask;
847 wqe->dma.length = length;
848 wqe->dma.resid = length;
849 wqe->dma.num_sge = num_sge;
850 wqe->dma.cur_sge = 0;
851 wqe->dma.sge_offset = 0;
852 wqe->state = wqe_state_posted;
853 wqe->ssn = atomic_add_return(1, &qp->ssn);
854
855 return 0;
856 }
857
post_one_send(struct rxe_qp * qp,const struct ib_send_wr * ibwr)858 static int post_one_send(struct rxe_qp *qp, const struct ib_send_wr *ibwr)
859 {
860 int err;
861 struct rxe_sq *sq = &qp->sq;
862 struct rxe_send_wqe *send_wqe;
863 unsigned int mask;
864 unsigned int length;
865 int full;
866
867 err = validate_send_wr(qp, ibwr, &mask, &length);
868 if (err)
869 return err;
870
871 full = queue_full(sq->queue, QUEUE_TYPE_FROM_ULP);
872 if (unlikely(full)) {
873 rxe_err_qp(qp, "send queue full");
874 return -ENOMEM;
875 }
876
877 send_wqe = queue_producer_addr(sq->queue, QUEUE_TYPE_FROM_ULP);
878 err = init_send_wqe(qp, ibwr, mask, length, send_wqe);
879 if (!err)
880 queue_advance_producer(sq->queue, QUEUE_TYPE_FROM_ULP);
881
882 return err;
883 }
884
rxe_post_send_kernel(struct rxe_qp * qp,const struct ib_send_wr * ibwr,const struct ib_send_wr ** bad_wr)885 static int rxe_post_send_kernel(struct rxe_qp *qp,
886 const struct ib_send_wr *ibwr,
887 const struct ib_send_wr **bad_wr)
888 {
889 int err = 0;
890 unsigned long flags;
891 int good = 0;
892
893 spin_lock_irqsave(&qp->sq.sq_lock, flags);
894 while (ibwr) {
895 err = post_one_send(qp, ibwr);
896 if (err) {
897 *bad_wr = ibwr;
898 break;
899 } else {
900 good++;
901 }
902 ibwr = ibwr->next;
903 }
904 spin_unlock_irqrestore(&qp->sq.sq_lock, flags);
905
906 /* kickoff processing of any posted wqes */
907 if (good)
908 rxe_sched_task(&qp->req.task);
909
910 spin_lock_irqsave(&qp->state_lock, flags);
911 if (qp_state(qp) == IB_QPS_ERR)
912 rxe_sched_task(&qp->comp.task);
913 spin_unlock_irqrestore(&qp->state_lock, flags);
914
915 return err;
916 }
917
rxe_post_send(struct ib_qp * ibqp,const struct ib_send_wr * wr,const struct ib_send_wr ** bad_wr)918 static int rxe_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
919 const struct ib_send_wr **bad_wr)
920 {
921 struct rxe_qp *qp = to_rqp(ibqp);
922 int err;
923 unsigned long flags;
924
925 spin_lock_irqsave(&qp->state_lock, flags);
926 /* caller has already called destroy_qp */
927 if (WARN_ON_ONCE(!qp->valid)) {
928 spin_unlock_irqrestore(&qp->state_lock, flags);
929 rxe_err_qp(qp, "qp has been destroyed");
930 return -EINVAL;
931 }
932
933 if (unlikely(qp_state(qp) < IB_QPS_RTS)) {
934 spin_unlock_irqrestore(&qp->state_lock, flags);
935 *bad_wr = wr;
936 rxe_err_qp(qp, "qp not ready to send");
937 return -EINVAL;
938 }
939 spin_unlock_irqrestore(&qp->state_lock, flags);
940
941 if (qp->is_user) {
942 /* Utilize process context to do protocol processing */
943 rxe_run_task(&qp->req.task);
944 } else {
945 err = rxe_post_send_kernel(qp, wr, bad_wr);
946 if (err)
947 return err;
948 }
949
950 return 0;
951 }
952
953 /* recv wr */
post_one_recv(struct rxe_rq * rq,const struct ib_recv_wr * ibwr)954 static int post_one_recv(struct rxe_rq *rq, const struct ib_recv_wr *ibwr)
955 {
956 int i;
957 unsigned long length;
958 struct rxe_recv_wqe *recv_wqe;
959 int num_sge = ibwr->num_sge;
960 int full;
961 int err;
962
963 full = queue_full(rq->queue, QUEUE_TYPE_FROM_ULP);
964 if (unlikely(full)) {
965 err = -ENOMEM;
966 rxe_dbg("queue full");
967 goto err_out;
968 }
969
970 if (unlikely(num_sge > rq->max_sge)) {
971 err = -EINVAL;
972 rxe_dbg("bad num_sge > max_sge");
973 goto err_out;
974 }
975
976 length = 0;
977 for (i = 0; i < num_sge; i++)
978 length += ibwr->sg_list[i].length;
979
980 /* IBA max message size is 2^31 */
981 if (length >= (1UL<<31)) {
982 err = -EINVAL;
983 rxe_dbg("message length too long");
984 goto err_out;
985 }
986
987 recv_wqe = queue_producer_addr(rq->queue, QUEUE_TYPE_FROM_ULP);
988
989 recv_wqe->wr_id = ibwr->wr_id;
990 recv_wqe->dma.length = length;
991 recv_wqe->dma.resid = length;
992 recv_wqe->dma.num_sge = num_sge;
993 recv_wqe->dma.cur_sge = 0;
994 recv_wqe->dma.sge_offset = 0;
995 memcpy(recv_wqe->dma.sge, ibwr->sg_list,
996 num_sge * sizeof(struct ib_sge));
997
998 queue_advance_producer(rq->queue, QUEUE_TYPE_FROM_ULP);
999
1000 return 0;
1001
1002 err_out:
1003 rxe_dbg("returned err = %d", err);
1004 return err;
1005 }
1006
rxe_post_recv(struct ib_qp * ibqp,const struct ib_recv_wr * wr,const struct ib_recv_wr ** bad_wr)1007 static int rxe_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
1008 const struct ib_recv_wr **bad_wr)
1009 {
1010 int err = 0;
1011 struct rxe_qp *qp = to_rqp(ibqp);
1012 struct rxe_rq *rq = &qp->rq;
1013 unsigned long flags;
1014
1015 spin_lock_irqsave(&qp->state_lock, flags);
1016 /* caller has already called destroy_qp */
1017 if (WARN_ON_ONCE(!qp->valid)) {
1018 spin_unlock_irqrestore(&qp->state_lock, flags);
1019 rxe_err_qp(qp, "qp has been destroyed");
1020 return -EINVAL;
1021 }
1022
1023 /* see C10-97.2.1 */
1024 if (unlikely((qp_state(qp) < IB_QPS_INIT))) {
1025 spin_unlock_irqrestore(&qp->state_lock, flags);
1026 *bad_wr = wr;
1027 rxe_dbg_qp(qp, "qp not ready to post recv");
1028 return -EINVAL;
1029 }
1030 spin_unlock_irqrestore(&qp->state_lock, flags);
1031
1032 if (unlikely(qp->srq)) {
1033 *bad_wr = wr;
1034 rxe_dbg_qp(qp, "qp has srq, use post_srq_recv instead");
1035 return -EINVAL;
1036 }
1037
1038 spin_lock_irqsave(&rq->producer_lock, flags);
1039
1040 while (wr) {
1041 err = post_one_recv(rq, wr);
1042 if (unlikely(err)) {
1043 *bad_wr = wr;
1044 break;
1045 }
1046 wr = wr->next;
1047 }
1048
1049 spin_unlock_irqrestore(&rq->producer_lock, flags);
1050
1051 spin_lock_irqsave(&qp->state_lock, flags);
1052 if (qp_state(qp) == IB_QPS_ERR)
1053 rxe_sched_task(&qp->resp.task);
1054 spin_unlock_irqrestore(&qp->state_lock, flags);
1055
1056 return err;
1057 }
1058
1059 /* cq */
rxe_create_cq(struct ib_cq * ibcq,const struct ib_cq_init_attr * attr,struct ib_udata * udata)1060 static int rxe_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
1061 struct ib_udata *udata)
1062 {
1063 struct ib_device *dev = ibcq->device;
1064 struct rxe_dev *rxe = to_rdev(dev);
1065 struct rxe_cq *cq = to_rcq(ibcq);
1066 struct rxe_create_cq_resp __user *uresp = NULL;
1067 int err, cleanup_err;
1068
1069 if (udata) {
1070 if (udata->outlen < sizeof(*uresp)) {
1071 err = -EINVAL;
1072 rxe_dbg_dev(rxe, "malformed udata, err = %d", err);
1073 goto err_out;
1074 }
1075 uresp = udata->outbuf;
1076 }
1077
1078 if (attr->flags) {
1079 err = -EOPNOTSUPP;
1080 rxe_dbg_dev(rxe, "bad attr->flags, err = %d", err);
1081 goto err_out;
1082 }
1083
1084 err = rxe_cq_chk_attr(rxe, NULL, attr->cqe, attr->comp_vector);
1085 if (err) {
1086 rxe_dbg_dev(rxe, "bad init attributes, err = %d", err);
1087 goto err_out;
1088 }
1089
1090 err = rxe_add_to_pool(&rxe->cq_pool, cq);
1091 if (err) {
1092 rxe_dbg_dev(rxe, "unable to create cq, err = %d", err);
1093 goto err_out;
1094 }
1095
1096 err = rxe_cq_from_init(rxe, cq, attr->cqe, attr->comp_vector, udata,
1097 uresp);
1098 if (err) {
1099 rxe_dbg_cq(cq, "create cq failed, err = %d", err);
1100 goto err_cleanup;
1101 }
1102
1103 return 0;
1104
1105 err_cleanup:
1106 cleanup_err = rxe_cleanup(cq);
1107 if (cleanup_err)
1108 rxe_err_cq(cq, "cleanup failed, err = %d", cleanup_err);
1109 err_out:
1110 rxe_err_dev(rxe, "returned err = %d", err);
1111 return err;
1112 }
1113
rxe_resize_cq(struct ib_cq * ibcq,int cqe,struct ib_udata * udata)1114 static int rxe_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
1115 {
1116 struct rxe_cq *cq = to_rcq(ibcq);
1117 struct rxe_dev *rxe = to_rdev(ibcq->device);
1118 struct rxe_resize_cq_resp __user *uresp = NULL;
1119 int err;
1120
1121 if (udata) {
1122 if (udata->outlen < sizeof(*uresp)) {
1123 err = -EINVAL;
1124 rxe_dbg_cq(cq, "malformed udata");
1125 goto err_out;
1126 }
1127 uresp = udata->outbuf;
1128 }
1129
1130 err = rxe_cq_chk_attr(rxe, cq, cqe, 0);
1131 if (err) {
1132 rxe_dbg_cq(cq, "bad attr, err = %d", err);
1133 goto err_out;
1134 }
1135
1136 err = rxe_cq_resize_queue(cq, cqe, uresp, udata);
1137 if (err) {
1138 rxe_dbg_cq(cq, "resize cq failed, err = %d", err);
1139 goto err_out;
1140 }
1141
1142 return 0;
1143
1144 err_out:
1145 rxe_err_cq(cq, "returned err = %d", err);
1146 return err;
1147 }
1148
rxe_poll_cq(struct ib_cq * ibcq,int num_entries,struct ib_wc * wc)1149 static int rxe_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
1150 {
1151 int i;
1152 struct rxe_cq *cq = to_rcq(ibcq);
1153 struct rxe_cqe *cqe;
1154 unsigned long flags;
1155
1156 spin_lock_irqsave(&cq->cq_lock, flags);
1157 for (i = 0; i < num_entries; i++) {
1158 cqe = queue_head(cq->queue, QUEUE_TYPE_TO_ULP);
1159 if (!cqe)
1160 break; /* queue empty */
1161
1162 memcpy(wc++, &cqe->ibwc, sizeof(*wc));
1163 queue_advance_consumer(cq->queue, QUEUE_TYPE_TO_ULP);
1164 }
1165 spin_unlock_irqrestore(&cq->cq_lock, flags);
1166
1167 return i;
1168 }
1169
rxe_peek_cq(struct ib_cq * ibcq,int wc_cnt)1170 static int rxe_peek_cq(struct ib_cq *ibcq, int wc_cnt)
1171 {
1172 struct rxe_cq *cq = to_rcq(ibcq);
1173 int count;
1174
1175 count = queue_count(cq->queue, QUEUE_TYPE_TO_ULP);
1176
1177 return (count > wc_cnt) ? wc_cnt : count;
1178 }
1179
rxe_req_notify_cq(struct ib_cq * ibcq,enum ib_cq_notify_flags flags)1180 static int rxe_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
1181 {
1182 struct rxe_cq *cq = to_rcq(ibcq);
1183 int ret = 0;
1184 int empty;
1185 unsigned long irq_flags;
1186
1187 spin_lock_irqsave(&cq->cq_lock, irq_flags);
1188 cq->notify |= flags & IB_CQ_SOLICITED_MASK;
1189 empty = queue_empty(cq->queue, QUEUE_TYPE_TO_ULP);
1190
1191 if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !empty)
1192 ret = 1;
1193
1194 spin_unlock_irqrestore(&cq->cq_lock, irq_flags);
1195
1196 return ret;
1197 }
1198
rxe_destroy_cq(struct ib_cq * ibcq,struct ib_udata * udata)1199 static int rxe_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata)
1200 {
1201 struct rxe_cq *cq = to_rcq(ibcq);
1202 int err;
1203
1204 /* See IBA C11-17: The CI shall return an error if this Verb is
1205 * invoked while a Work Queue is still associated with the CQ.
1206 */
1207 if (atomic_read(&cq->num_wq)) {
1208 err = -EINVAL;
1209 rxe_dbg_cq(cq, "still in use");
1210 goto err_out;
1211 }
1212
1213 err = rxe_cleanup(cq);
1214 if (err)
1215 rxe_err_cq(cq, "cleanup failed, err = %d", err);
1216
1217 return 0;
1218
1219 err_out:
1220 rxe_err_cq(cq, "returned err = %d", err);
1221 return err;
1222 }
1223
1224 /* mr */
rxe_get_dma_mr(struct ib_pd * ibpd,int access)1225 static struct ib_mr *rxe_get_dma_mr(struct ib_pd *ibpd, int access)
1226 {
1227 struct rxe_dev *rxe = to_rdev(ibpd->device);
1228 struct rxe_pd *pd = to_rpd(ibpd);
1229 struct rxe_mr *mr;
1230 int err;
1231
1232 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1233 if (!mr)
1234 return ERR_PTR(-ENOMEM);
1235
1236 err = rxe_add_to_pool(&rxe->mr_pool, mr);
1237 if (err) {
1238 rxe_dbg_dev(rxe, "unable to create mr");
1239 goto err_free;
1240 }
1241
1242 rxe_get(pd);
1243 mr->ibmr.pd = ibpd;
1244 mr->ibmr.device = ibpd->device;
1245
1246 rxe_mr_init_dma(access, mr);
1247 rxe_finalize(mr);
1248 return &mr->ibmr;
1249
1250 err_free:
1251 kfree(mr);
1252 rxe_err_pd(pd, "returned err = %d", err);
1253 return ERR_PTR(err);
1254 }
1255
rxe_reg_user_mr(struct ib_pd * ibpd,u64 start,u64 length,u64 iova,int access,struct ib_udata * udata)1256 static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd, u64 start,
1257 u64 length, u64 iova, int access,
1258 struct ib_udata *udata)
1259 {
1260 struct rxe_dev *rxe = to_rdev(ibpd->device);
1261 struct rxe_pd *pd = to_rpd(ibpd);
1262 struct rxe_mr *mr;
1263 int err, cleanup_err;
1264
1265 if (access & ~RXE_ACCESS_SUPPORTED_MR) {
1266 rxe_err_pd(pd, "access = %#x not supported (%#x)", access,
1267 RXE_ACCESS_SUPPORTED_MR);
1268 return ERR_PTR(-EOPNOTSUPP);
1269 }
1270
1271 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1272 if (!mr)
1273 return ERR_PTR(-ENOMEM);
1274
1275 err = rxe_add_to_pool(&rxe->mr_pool, mr);
1276 if (err) {
1277 rxe_dbg_pd(pd, "unable to create mr");
1278 goto err_free;
1279 }
1280
1281 rxe_get(pd);
1282 mr->ibmr.pd = ibpd;
1283 mr->ibmr.device = ibpd->device;
1284
1285 err = rxe_mr_init_user(rxe, start, length, iova, access, mr);
1286 if (err) {
1287 rxe_dbg_mr(mr, "reg_user_mr failed, err = %d", err);
1288 goto err_cleanup;
1289 }
1290
1291 rxe_finalize(mr);
1292 return &mr->ibmr;
1293
1294 err_cleanup:
1295 cleanup_err = rxe_cleanup(mr);
1296 if (cleanup_err)
1297 rxe_err_mr(mr, "cleanup failed, err = %d", cleanup_err);
1298 err_free:
1299 kfree(mr);
1300 rxe_err_pd(pd, "returned err = %d", err);
1301 return ERR_PTR(err);
1302 }
1303
rxe_rereg_user_mr(struct ib_mr * ibmr,int flags,u64 start,u64 length,u64 iova,int access,struct ib_pd * ibpd,struct ib_udata * udata)1304 static struct ib_mr *rxe_rereg_user_mr(struct ib_mr *ibmr, int flags,
1305 u64 start, u64 length, u64 iova,
1306 int access, struct ib_pd *ibpd,
1307 struct ib_udata *udata)
1308 {
1309 struct rxe_mr *mr = to_rmr(ibmr);
1310 struct rxe_pd *old_pd = to_rpd(ibmr->pd);
1311 struct rxe_pd *pd = to_rpd(ibpd);
1312
1313 /* for now only support the two easy cases:
1314 * rereg_pd and rereg_access
1315 */
1316 if (flags & ~RXE_MR_REREG_SUPPORTED) {
1317 rxe_err_mr(mr, "flags = %#x not supported", flags);
1318 return ERR_PTR(-EOPNOTSUPP);
1319 }
1320
1321 if (flags & IB_MR_REREG_PD) {
1322 rxe_put(old_pd);
1323 rxe_get(pd);
1324 mr->ibmr.pd = ibpd;
1325 }
1326
1327 if (flags & IB_MR_REREG_ACCESS) {
1328 if (access & ~RXE_ACCESS_SUPPORTED_MR) {
1329 rxe_err_mr(mr, "access = %#x not supported", access);
1330 return ERR_PTR(-EOPNOTSUPP);
1331 }
1332 mr->access = access;
1333 }
1334
1335 return NULL;
1336 }
1337
rxe_alloc_mr(struct ib_pd * ibpd,enum ib_mr_type mr_type,u32 max_num_sg)1338 static struct ib_mr *rxe_alloc_mr(struct ib_pd *ibpd, enum ib_mr_type mr_type,
1339 u32 max_num_sg)
1340 {
1341 struct rxe_dev *rxe = to_rdev(ibpd->device);
1342 struct rxe_pd *pd = to_rpd(ibpd);
1343 struct rxe_mr *mr;
1344 int err, cleanup_err;
1345
1346 if (mr_type != IB_MR_TYPE_MEM_REG) {
1347 err = -EINVAL;
1348 rxe_dbg_pd(pd, "mr type %d not supported, err = %d",
1349 mr_type, err);
1350 goto err_out;
1351 }
1352
1353 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1354 if (!mr)
1355 return ERR_PTR(-ENOMEM);
1356
1357 err = rxe_add_to_pool(&rxe->mr_pool, mr);
1358 if (err)
1359 goto err_free;
1360
1361 rxe_get(pd);
1362 mr->ibmr.pd = ibpd;
1363 mr->ibmr.device = ibpd->device;
1364
1365 err = rxe_mr_init_fast(max_num_sg, mr);
1366 if (err) {
1367 rxe_dbg_mr(mr, "alloc_mr failed, err = %d", err);
1368 goto err_cleanup;
1369 }
1370
1371 rxe_finalize(mr);
1372 return &mr->ibmr;
1373
1374 err_cleanup:
1375 cleanup_err = rxe_cleanup(mr);
1376 if (cleanup_err)
1377 rxe_err_mr(mr, "cleanup failed, err = %d", err);
1378 err_free:
1379 kfree(mr);
1380 err_out:
1381 rxe_err_pd(pd, "returned err = %d", err);
1382 return ERR_PTR(err);
1383 }
1384
rxe_dereg_mr(struct ib_mr * ibmr,struct ib_udata * udata)1385 static int rxe_dereg_mr(struct ib_mr *ibmr, struct ib_udata *udata)
1386 {
1387 struct rxe_mr *mr = to_rmr(ibmr);
1388 int err, cleanup_err;
1389
1390 /* See IBA 10.6.7.2.6 */
1391 if (atomic_read(&mr->num_mw) > 0) {
1392 err = -EINVAL;
1393 rxe_dbg_mr(mr, "mr has mw's bound");
1394 goto err_out;
1395 }
1396
1397 cleanup_err = rxe_cleanup(mr);
1398 if (cleanup_err)
1399 rxe_err_mr(mr, "cleanup failed, err = %d", cleanup_err);
1400
1401 kfree_rcu_mightsleep(mr);
1402 return 0;
1403
1404 err_out:
1405 rxe_err_mr(mr, "returned err = %d", err);
1406 return err;
1407 }
1408
parent_show(struct device * device,struct device_attribute * attr,char * buf)1409 static ssize_t parent_show(struct device *device,
1410 struct device_attribute *attr, char *buf)
1411 {
1412 struct rxe_dev *rxe =
1413 rdma_device_to_drv_device(device, struct rxe_dev, ib_dev);
1414
1415 return sysfs_emit(buf, "%s\n", rxe_parent_name(rxe, 1));
1416 }
1417
1418 static DEVICE_ATTR_RO(parent);
1419
1420 static struct attribute *rxe_dev_attributes[] = {
1421 &dev_attr_parent.attr,
1422 NULL
1423 };
1424
1425 static const struct attribute_group rxe_attr_group = {
1426 .attrs = rxe_dev_attributes,
1427 };
1428
rxe_enable_driver(struct ib_device * ib_dev)1429 static int rxe_enable_driver(struct ib_device *ib_dev)
1430 {
1431 struct rxe_dev *rxe = container_of(ib_dev, struct rxe_dev, ib_dev);
1432
1433 rxe_set_port_state(rxe);
1434 dev_info(&rxe->ib_dev.dev, "added %s\n", netdev_name(rxe->ndev));
1435 return 0;
1436 }
1437
1438 static const struct ib_device_ops rxe_dev_ops = {
1439 .owner = THIS_MODULE,
1440 .driver_id = RDMA_DRIVER_RXE,
1441 .uverbs_abi_ver = RXE_UVERBS_ABI_VERSION,
1442
1443 .alloc_hw_port_stats = rxe_ib_alloc_hw_port_stats,
1444 .alloc_mr = rxe_alloc_mr,
1445 .alloc_mw = rxe_alloc_mw,
1446 .alloc_pd = rxe_alloc_pd,
1447 .alloc_ucontext = rxe_alloc_ucontext,
1448 .attach_mcast = rxe_attach_mcast,
1449 .create_ah = rxe_create_ah,
1450 .create_cq = rxe_create_cq,
1451 .create_qp = rxe_create_qp,
1452 .create_srq = rxe_create_srq,
1453 .create_user_ah = rxe_create_ah,
1454 .dealloc_driver = rxe_dealloc,
1455 .dealloc_mw = rxe_dealloc_mw,
1456 .dealloc_pd = rxe_dealloc_pd,
1457 .dealloc_ucontext = rxe_dealloc_ucontext,
1458 .dereg_mr = rxe_dereg_mr,
1459 .destroy_ah = rxe_destroy_ah,
1460 .destroy_cq = rxe_destroy_cq,
1461 .destroy_qp = rxe_destroy_qp,
1462 .destroy_srq = rxe_destroy_srq,
1463 .detach_mcast = rxe_detach_mcast,
1464 .device_group = &rxe_attr_group,
1465 .enable_driver = rxe_enable_driver,
1466 .get_dma_mr = rxe_get_dma_mr,
1467 .get_hw_stats = rxe_ib_get_hw_stats,
1468 .get_link_layer = rxe_get_link_layer,
1469 .get_port_immutable = rxe_port_immutable,
1470 .map_mr_sg = rxe_map_mr_sg,
1471 .mmap = rxe_mmap,
1472 .modify_ah = rxe_modify_ah,
1473 .modify_device = rxe_modify_device,
1474 .modify_port = rxe_modify_port,
1475 .modify_qp = rxe_modify_qp,
1476 .modify_srq = rxe_modify_srq,
1477 .peek_cq = rxe_peek_cq,
1478 .poll_cq = rxe_poll_cq,
1479 .post_recv = rxe_post_recv,
1480 .post_send = rxe_post_send,
1481 .post_srq_recv = rxe_post_srq_recv,
1482 .query_ah = rxe_query_ah,
1483 .query_device = rxe_query_device,
1484 .query_pkey = rxe_query_pkey,
1485 .query_port = rxe_query_port,
1486 .query_qp = rxe_query_qp,
1487 .query_srq = rxe_query_srq,
1488 .reg_user_mr = rxe_reg_user_mr,
1489 .req_notify_cq = rxe_req_notify_cq,
1490 .rereg_user_mr = rxe_rereg_user_mr,
1491 .resize_cq = rxe_resize_cq,
1492
1493 INIT_RDMA_OBJ_SIZE(ib_ah, rxe_ah, ibah),
1494 INIT_RDMA_OBJ_SIZE(ib_cq, rxe_cq, ibcq),
1495 INIT_RDMA_OBJ_SIZE(ib_pd, rxe_pd, ibpd),
1496 INIT_RDMA_OBJ_SIZE(ib_qp, rxe_qp, ibqp),
1497 INIT_RDMA_OBJ_SIZE(ib_srq, rxe_srq, ibsrq),
1498 INIT_RDMA_OBJ_SIZE(ib_ucontext, rxe_ucontext, ibuc),
1499 INIT_RDMA_OBJ_SIZE(ib_mw, rxe_mw, ibmw),
1500 };
1501
rxe_register_device(struct rxe_dev * rxe,const char * ibdev_name)1502 int rxe_register_device(struct rxe_dev *rxe, const char *ibdev_name)
1503 {
1504 int err;
1505 struct ib_device *dev = &rxe->ib_dev;
1506
1507 strscpy(dev->node_desc, "rxe", sizeof(dev->node_desc));
1508
1509 dev->node_type = RDMA_NODE_IB_CA;
1510 dev->phys_port_cnt = 1;
1511 dev->num_comp_vectors = num_possible_cpus();
1512 dev->local_dma_lkey = 0;
1513 addrconf_addr_eui48((unsigned char *)&dev->node_guid,
1514 rxe->ndev->dev_addr);
1515
1516 dev->uverbs_cmd_mask |= BIT_ULL(IB_USER_VERBS_CMD_POST_SEND) |
1517 BIT_ULL(IB_USER_VERBS_CMD_REQ_NOTIFY_CQ);
1518
1519 ib_set_device_ops(dev, &rxe_dev_ops);
1520 err = ib_device_set_netdev(&rxe->ib_dev, rxe->ndev, 1);
1521 if (err)
1522 return err;
1523
1524 err = rxe_icrc_init(rxe);
1525 if (err)
1526 return err;
1527
1528 err = ib_register_device(dev, ibdev_name, NULL);
1529 if (err)
1530 rxe_dbg_dev(rxe, "failed with error %d\n", err);
1531
1532 /*
1533 * Note that rxe may be invalid at this point if another thread
1534 * unregistered it.
1535 */
1536 return err;
1537 }
1538