]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/infiniband/sw/rxe/rxe_verbs.c
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dledford/rdma
[karo-tx-linux.git] / drivers / infiniband / sw / rxe / rxe_verbs.c
1 /*
2  * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
3  * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33
34 #include "rxe.h"
35 #include "rxe_loc.h"
36 #include "rxe_queue.h"
37
38 static int rxe_query_device(struct ib_device *dev,
39                             struct ib_device_attr *attr,
40                             struct ib_udata *uhw)
41 {
42         struct rxe_dev *rxe = to_rdev(dev);
43
44         if (uhw->inlen || uhw->outlen)
45                 return -EINVAL;
46
47         *attr = rxe->attr;
48         return 0;
49 }
50
51 static void rxe_eth_speed_to_ib_speed(int speed, u8 *active_speed,
52                                       u8 *active_width)
53 {
54         if (speed <= 1000) {
55                 *active_width = IB_WIDTH_1X;
56                 *active_speed = IB_SPEED_SDR;
57         } else if (speed <= 10000) {
58                 *active_width = IB_WIDTH_1X;
59                 *active_speed = IB_SPEED_FDR10;
60         } else if (speed <= 20000) {
61                 *active_width = IB_WIDTH_4X;
62                 *active_speed = IB_SPEED_DDR;
63         } else if (speed <= 30000) {
64                 *active_width = IB_WIDTH_4X;
65                 *active_speed = IB_SPEED_QDR;
66         } else if (speed <= 40000) {
67                 *active_width = IB_WIDTH_4X;
68                 *active_speed = IB_SPEED_FDR10;
69         } else {
70                 *active_width = IB_WIDTH_4X;
71                 *active_speed = IB_SPEED_EDR;
72         }
73 }
74
75 static int rxe_query_port(struct ib_device *dev,
76                           u8 port_num, struct ib_port_attr *attr)
77 {
78         struct rxe_dev *rxe = to_rdev(dev);
79         struct rxe_port *port;
80         u32 speed;
81
82         if (unlikely(port_num != 1)) {
83                 pr_warn("invalid port_number %d\n", port_num);
84                 goto err1;
85         }
86
87         port = &rxe->port;
88
89         /* *attr being zeroed by the caller, avoid zeroing it here */
90         *attr = port->attr;
91
92         mutex_lock(&rxe->usdev_lock);
93         if (rxe->ndev->ethtool_ops->get_link_ksettings) {
94                 struct ethtool_link_ksettings ks;
95
96                 rxe->ndev->ethtool_ops->get_link_ksettings(rxe->ndev, &ks);
97                 speed = ks.base.speed;
98         } else if (rxe->ndev->ethtool_ops->get_settings) {
99                 struct ethtool_cmd cmd;
100
101                 rxe->ndev->ethtool_ops->get_settings(rxe->ndev, &cmd);
102                 speed = cmd.speed;
103         } else {
104                 pr_warn("%s speed is unknown, defaulting to 1000\n",
105                         rxe->ndev->name);
106                 speed = 1000;
107         }
108         rxe_eth_speed_to_ib_speed(speed, &attr->active_speed,
109                                   &attr->active_width);
110         mutex_unlock(&rxe->usdev_lock);
111
112         return 0;
113
114 err1:
115         return -EINVAL;
116 }
117
118 static int rxe_query_gid(struct ib_device *device,
119                          u8 port_num, int index, union ib_gid *gid)
120 {
121         int ret;
122
123         if (index > RXE_PORT_GID_TBL_LEN)
124                 return -EINVAL;
125
126         ret = ib_get_cached_gid(device, port_num, index, gid, NULL);
127         if (ret == -EAGAIN) {
128                 memcpy(gid, &zgid, sizeof(*gid));
129                 return 0;
130         }
131
132         return ret;
133 }
134
135 static int rxe_add_gid(struct ib_device *device, u8 port_num, unsigned int
136                        index, const union ib_gid *gid,
137                        const struct ib_gid_attr *attr, void **context)
138 {
139         if (index >= RXE_PORT_GID_TBL_LEN)
140                 return -EINVAL;
141         return 0;
142 }
143
144 static int rxe_del_gid(struct ib_device *device, u8 port_num, unsigned int
145                        index, void **context)
146 {
147         if (index >= RXE_PORT_GID_TBL_LEN)
148                 return -EINVAL;
149         return 0;
150 }
151
152 static struct net_device *rxe_get_netdev(struct ib_device *device,
153                                          u8 port_num)
154 {
155         struct rxe_dev *rxe = to_rdev(device);
156
157         if (rxe->ndev) {
158                 dev_hold(rxe->ndev);
159                 return rxe->ndev;
160         }
161
162         return NULL;
163 }
164
165 static int rxe_query_pkey(struct ib_device *device,
166                           u8 port_num, u16 index, u16 *pkey)
167 {
168         struct rxe_dev *rxe = to_rdev(device);
169         struct rxe_port *port;
170
171         if (unlikely(port_num != 1)) {
172                 dev_warn(device->dma_device, "invalid port_num = %d\n",
173                          port_num);
174                 goto err1;
175         }
176
177         port = &rxe->port;
178
179         if (unlikely(index >= port->attr.pkey_tbl_len)) {
180                 dev_warn(device->dma_device, "invalid index = %d\n",
181                          index);
182                 goto err1;
183         }
184
185         *pkey = port->pkey_tbl[index];
186         return 0;
187
188 err1:
189         return -EINVAL;
190 }
191
192 static int rxe_modify_device(struct ib_device *dev,
193                              int mask, struct ib_device_modify *attr)
194 {
195         struct rxe_dev *rxe = to_rdev(dev);
196
197         if (mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID)
198                 rxe->attr.sys_image_guid = cpu_to_be64(attr->sys_image_guid);
199
200         if (mask & IB_DEVICE_MODIFY_NODE_DESC) {
201                 memcpy(rxe->ib_dev.node_desc,
202                        attr->node_desc, sizeof(rxe->ib_dev.node_desc));
203         }
204
205         return 0;
206 }
207
208 static int rxe_modify_port(struct ib_device *dev,
209                            u8 port_num, int mask, struct ib_port_modify *attr)
210 {
211         struct rxe_dev *rxe = to_rdev(dev);
212         struct rxe_port *port;
213
214         if (unlikely(port_num != 1)) {
215                 pr_warn("invalid port_num = %d\n", port_num);
216                 goto err1;
217         }
218
219         port = &rxe->port;
220
221         port->attr.port_cap_flags |= attr->set_port_cap_mask;
222         port->attr.port_cap_flags &= ~attr->clr_port_cap_mask;
223
224         if (mask & IB_PORT_RESET_QKEY_CNTR)
225                 port->attr.qkey_viol_cntr = 0;
226
227         return 0;
228
229 err1:
230         return -EINVAL;
231 }
232
233 static enum rdma_link_layer rxe_get_link_layer(struct ib_device *dev,
234                                                u8 port_num)
235 {
236         struct rxe_dev *rxe = to_rdev(dev);
237
238         return rxe_link_layer(rxe, port_num);
239 }
240
241 static struct ib_ucontext *rxe_alloc_ucontext(struct ib_device *dev,
242                                               struct ib_udata *udata)
243 {
244         struct rxe_dev *rxe = to_rdev(dev);
245         struct rxe_ucontext *uc;
246
247         uc = rxe_alloc(&rxe->uc_pool);
248         return uc ? &uc->ibuc : ERR_PTR(-ENOMEM);
249 }
250
251 static int rxe_dealloc_ucontext(struct ib_ucontext *ibuc)
252 {
253         struct rxe_ucontext *uc = to_ruc(ibuc);
254
255         rxe_drop_ref(uc);
256         return 0;
257 }
258
259 static int rxe_port_immutable(struct ib_device *dev, u8 port_num,
260                               struct ib_port_immutable *immutable)
261 {
262         int err;
263         struct ib_port_attr attr;
264
265         immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
266
267         err = ib_query_port(dev, port_num, &attr);
268         if (err)
269                 return err;
270
271         immutable->pkey_tbl_len = attr.pkey_tbl_len;
272         immutable->gid_tbl_len = attr.gid_tbl_len;
273         immutable->max_mad_size = IB_MGMT_MAD_SIZE;
274
275         return 0;
276 }
277
278 static struct ib_pd *rxe_alloc_pd(struct ib_device *dev,
279                                   struct ib_ucontext *context,
280                                   struct ib_udata *udata)
281 {
282         struct rxe_dev *rxe = to_rdev(dev);
283         struct rxe_pd *pd;
284
285         pd = rxe_alloc(&rxe->pd_pool);
286         return pd ? &pd->ibpd : ERR_PTR(-ENOMEM);
287 }
288
289 static int rxe_dealloc_pd(struct ib_pd *ibpd)
290 {
291         struct rxe_pd *pd = to_rpd(ibpd);
292
293         rxe_drop_ref(pd);
294         return 0;
295 }
296
297 static int rxe_init_av(struct rxe_dev *rxe, struct ib_ah_attr *attr,
298                        struct rxe_av *av)
299 {
300         int err;
301         union ib_gid sgid;
302         struct ib_gid_attr sgid_attr;
303
304         err = ib_get_cached_gid(&rxe->ib_dev, attr->port_num,
305                                 attr->grh.sgid_index, &sgid,
306                                 &sgid_attr);
307         if (err) {
308                 pr_err("Failed to query sgid. err = %d\n", err);
309                 return err;
310         }
311
312         err = rxe_av_from_attr(rxe, attr->port_num, av, attr);
313         if (!err)
314                 err = rxe_av_fill_ip_info(rxe, av, attr, &sgid_attr, &sgid);
315
316         if (sgid_attr.ndev)
317                 dev_put(sgid_attr.ndev);
318         return err;
319 }
320
321 static struct ib_ah *rxe_create_ah(struct ib_pd *ibpd, struct ib_ah_attr *attr,
322                                    struct ib_udata *udata)
323
324 {
325         int err;
326         struct rxe_dev *rxe = to_rdev(ibpd->device);
327         struct rxe_pd *pd = to_rpd(ibpd);
328         struct rxe_ah *ah;
329
330         err = rxe_av_chk_attr(rxe, attr);
331         if (err)
332                 goto err1;
333
334         ah = rxe_alloc(&rxe->ah_pool);
335         if (!ah) {
336                 err = -ENOMEM;
337                 goto err1;
338         }
339
340         rxe_add_ref(pd);
341         ah->pd = pd;
342
343         err = rxe_init_av(rxe, attr, &ah->av);
344         if (err)
345                 goto err2;
346
347         return &ah->ibah;
348
349 err2:
350         rxe_drop_ref(pd);
351         rxe_drop_ref(ah);
352 err1:
353         return ERR_PTR(err);
354 }
355
356 static int rxe_modify_ah(struct ib_ah *ibah, struct ib_ah_attr *attr)
357 {
358         int err;
359         struct rxe_dev *rxe = to_rdev(ibah->device);
360         struct rxe_ah *ah = to_rah(ibah);
361
362         err = rxe_av_chk_attr(rxe, attr);
363         if (err)
364                 return err;
365
366         err = rxe_init_av(rxe, attr, &ah->av);
367         if (err)
368                 return err;
369
370         return 0;
371 }
372
373 static int rxe_query_ah(struct ib_ah *ibah, struct ib_ah_attr *attr)
374 {
375         struct rxe_dev *rxe = to_rdev(ibah->device);
376         struct rxe_ah *ah = to_rah(ibah);
377
378         rxe_av_to_attr(rxe, &ah->av, attr);
379         return 0;
380 }
381
382 static int rxe_destroy_ah(struct ib_ah *ibah)
383 {
384         struct rxe_ah *ah = to_rah(ibah);
385
386         rxe_drop_ref(ah->pd);
387         rxe_drop_ref(ah);
388         return 0;
389 }
390
391 static int post_one_recv(struct rxe_rq *rq, struct ib_recv_wr *ibwr)
392 {
393         int err;
394         int i;
395         u32 length;
396         struct rxe_recv_wqe *recv_wqe;
397         int num_sge = ibwr->num_sge;
398
399         if (unlikely(queue_full(rq->queue))) {
400                 err = -ENOMEM;
401                 goto err1;
402         }
403
404         if (unlikely(num_sge > rq->max_sge)) {
405                 err = -EINVAL;
406                 goto err1;
407         }
408
409         length = 0;
410         for (i = 0; i < num_sge; i++)
411                 length += ibwr->sg_list[i].length;
412
413         recv_wqe = producer_addr(rq->queue);
414         recv_wqe->wr_id = ibwr->wr_id;
415         recv_wqe->num_sge = num_sge;
416
417         memcpy(recv_wqe->dma.sge, ibwr->sg_list,
418                num_sge * sizeof(struct ib_sge));
419
420         recv_wqe->dma.length            = length;
421         recv_wqe->dma.resid             = length;
422         recv_wqe->dma.num_sge           = num_sge;
423         recv_wqe->dma.cur_sge           = 0;
424         recv_wqe->dma.sge_offset        = 0;
425
426         /* make sure all changes to the work queue are written before we
427          * update the producer pointer
428          */
429         smp_wmb();
430
431         advance_producer(rq->queue);
432         return 0;
433
434 err1:
435         return err;
436 }
437
438 static struct ib_srq *rxe_create_srq(struct ib_pd *ibpd,
439                                      struct ib_srq_init_attr *init,
440                                      struct ib_udata *udata)
441 {
442         int err;
443         struct rxe_dev *rxe = to_rdev(ibpd->device);
444         struct rxe_pd *pd = to_rpd(ibpd);
445         struct rxe_srq *srq;
446         struct ib_ucontext *context = udata ? ibpd->uobject->context : NULL;
447
448         err = rxe_srq_chk_attr(rxe, NULL, &init->attr, IB_SRQ_INIT_MASK);
449         if (err)
450                 goto err1;
451
452         srq = rxe_alloc(&rxe->srq_pool);
453         if (!srq) {
454                 err = -ENOMEM;
455                 goto err1;
456         }
457
458         rxe_add_index(srq);
459         rxe_add_ref(pd);
460         srq->pd = pd;
461
462         err = rxe_srq_from_init(rxe, srq, init, context, udata);
463         if (err)
464                 goto err2;
465
466         return &srq->ibsrq;
467
468 err2:
469         rxe_drop_ref(pd);
470         rxe_drop_index(srq);
471         rxe_drop_ref(srq);
472 err1:
473         return ERR_PTR(err);
474 }
475
476 static int rxe_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
477                           enum ib_srq_attr_mask mask,
478                           struct ib_udata *udata)
479 {
480         int err;
481         struct rxe_srq *srq = to_rsrq(ibsrq);
482         struct rxe_dev *rxe = to_rdev(ibsrq->device);
483
484         err = rxe_srq_chk_attr(rxe, srq, attr, mask);
485         if (err)
486                 goto err1;
487
488         err = rxe_srq_from_attr(rxe, srq, attr, mask, udata);
489         if (err)
490                 goto err1;
491
492         return 0;
493
494 err1:
495         return err;
496 }
497
498 static int rxe_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
499 {
500         struct rxe_srq *srq = to_rsrq(ibsrq);
501
502         if (srq->error)
503                 return -EINVAL;
504
505         attr->max_wr = srq->rq.queue->buf->index_mask;
506         attr->max_sge = srq->rq.max_sge;
507         attr->srq_limit = srq->limit;
508         return 0;
509 }
510
511 static int rxe_destroy_srq(struct ib_srq *ibsrq)
512 {
513         struct rxe_srq *srq = to_rsrq(ibsrq);
514
515         if (srq->rq.queue)
516                 rxe_queue_cleanup(srq->rq.queue);
517
518         rxe_drop_ref(srq->pd);
519         rxe_drop_index(srq);
520         rxe_drop_ref(srq);
521
522         return 0;
523 }
524
525 static int rxe_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
526                              struct ib_recv_wr **bad_wr)
527 {
528         int err = 0;
529         unsigned long flags;
530         struct rxe_srq *srq = to_rsrq(ibsrq);
531
532         spin_lock_irqsave(&srq->rq.producer_lock, flags);
533
534         while (wr) {
535                 err = post_one_recv(&srq->rq, wr);
536                 if (unlikely(err))
537                         break;
538                 wr = wr->next;
539         }
540
541         spin_unlock_irqrestore(&srq->rq.producer_lock, flags);
542
543         if (err)
544                 *bad_wr = wr;
545
546         return err;
547 }
548
549 static struct ib_qp *rxe_create_qp(struct ib_pd *ibpd,
550                                    struct ib_qp_init_attr *init,
551                                    struct ib_udata *udata)
552 {
553         int err;
554         struct rxe_dev *rxe = to_rdev(ibpd->device);
555         struct rxe_pd *pd = to_rpd(ibpd);
556         struct rxe_qp *qp;
557
558         err = rxe_qp_chk_init(rxe, init);
559         if (err)
560                 goto err1;
561
562         qp = rxe_alloc(&rxe->qp_pool);
563         if (!qp) {
564                 err = -ENOMEM;
565                 goto err1;
566         }
567
568         if (udata) {
569                 if (udata->inlen) {
570                         err = -EINVAL;
571                         goto err2;
572                 }
573                 qp->is_user = 1;
574         }
575
576         rxe_add_index(qp);
577
578         err = rxe_qp_from_init(rxe, qp, pd, init, udata, ibpd);
579         if (err)
580                 goto err3;
581
582         return &qp->ibqp;
583
584 err3:
585         rxe_drop_index(qp);
586 err2:
587         rxe_drop_ref(qp);
588 err1:
589         return ERR_PTR(err);
590 }
591
592 static int rxe_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
593                          int mask, struct ib_udata *udata)
594 {
595         int err;
596         struct rxe_dev *rxe = to_rdev(ibqp->device);
597         struct rxe_qp *qp = to_rqp(ibqp);
598
599         err = rxe_qp_chk_attr(rxe, qp, attr, mask);
600         if (err)
601                 goto err1;
602
603         err = rxe_qp_from_attr(qp, attr, mask, udata);
604         if (err)
605                 goto err1;
606
607         return 0;
608
609 err1:
610         return err;
611 }
612
613 static int rxe_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
614                         int mask, struct ib_qp_init_attr *init)
615 {
616         struct rxe_qp *qp = to_rqp(ibqp);
617
618         rxe_qp_to_init(qp, init);
619         rxe_qp_to_attr(qp, attr, mask);
620
621         return 0;
622 }
623
624 static int rxe_destroy_qp(struct ib_qp *ibqp)
625 {
626         struct rxe_qp *qp = to_rqp(ibqp);
627
628         rxe_qp_destroy(qp);
629         rxe_drop_index(qp);
630         rxe_drop_ref(qp);
631         return 0;
632 }
633
634 static int validate_send_wr(struct rxe_qp *qp, struct ib_send_wr *ibwr,
635                             unsigned int mask, unsigned int length)
636 {
637         int num_sge = ibwr->num_sge;
638         struct rxe_sq *sq = &qp->sq;
639
640         if (unlikely(num_sge > sq->max_sge))
641                 goto err1;
642
643         if (unlikely(mask & WR_ATOMIC_MASK)) {
644                 if (length < 8)
645                         goto err1;
646
647                 if (atomic_wr(ibwr)->remote_addr & 0x7)
648                         goto err1;
649         }
650
651         if (unlikely((ibwr->send_flags & IB_SEND_INLINE) &&
652                      (length > sq->max_inline)))
653                 goto err1;
654
655         return 0;
656
657 err1:
658         return -EINVAL;
659 }
660
661 static void init_send_wr(struct rxe_qp *qp, struct rxe_send_wr *wr,
662                          struct ib_send_wr *ibwr)
663 {
664         wr->wr_id = ibwr->wr_id;
665         wr->num_sge = ibwr->num_sge;
666         wr->opcode = ibwr->opcode;
667         wr->send_flags = ibwr->send_flags;
668
669         if (qp_type(qp) == IB_QPT_UD ||
670             qp_type(qp) == IB_QPT_SMI ||
671             qp_type(qp) == IB_QPT_GSI) {
672                 wr->wr.ud.remote_qpn = ud_wr(ibwr)->remote_qpn;
673                 wr->wr.ud.remote_qkey = ud_wr(ibwr)->remote_qkey;
674                 if (qp_type(qp) == IB_QPT_GSI)
675                         wr->wr.ud.pkey_index = ud_wr(ibwr)->pkey_index;
676                 if (wr->opcode == IB_WR_SEND_WITH_IMM)
677                         wr->ex.imm_data = ibwr->ex.imm_data;
678         } else {
679                 switch (wr->opcode) {
680                 case IB_WR_RDMA_WRITE_WITH_IMM:
681                         wr->ex.imm_data = ibwr->ex.imm_data;
682                 case IB_WR_RDMA_READ:
683                 case IB_WR_RDMA_WRITE:
684                         wr->wr.rdma.remote_addr = rdma_wr(ibwr)->remote_addr;
685                         wr->wr.rdma.rkey        = rdma_wr(ibwr)->rkey;
686                         break;
687                 case IB_WR_SEND_WITH_IMM:
688                         wr->ex.imm_data = ibwr->ex.imm_data;
689                         break;
690                 case IB_WR_SEND_WITH_INV:
691                         wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
692                         break;
693                 case IB_WR_ATOMIC_CMP_AND_SWP:
694                 case IB_WR_ATOMIC_FETCH_AND_ADD:
695                         wr->wr.atomic.remote_addr =
696                                 atomic_wr(ibwr)->remote_addr;
697                         wr->wr.atomic.compare_add =
698                                 atomic_wr(ibwr)->compare_add;
699                         wr->wr.atomic.swap = atomic_wr(ibwr)->swap;
700                         wr->wr.atomic.rkey = atomic_wr(ibwr)->rkey;
701                         break;
702                 case IB_WR_LOCAL_INV:
703                         wr->ex.invalidate_rkey = ibwr->ex.invalidate_rkey;
704                 break;
705                 case IB_WR_REG_MR:
706                         wr->wr.reg.mr = reg_wr(ibwr)->mr;
707                         wr->wr.reg.key = reg_wr(ibwr)->key;
708                         wr->wr.reg.access = reg_wr(ibwr)->access;
709                 break;
710                 default:
711                         break;
712                 }
713         }
714 }
715
716 static int init_send_wqe(struct rxe_qp *qp, struct ib_send_wr *ibwr,
717                          unsigned int mask, unsigned int length,
718                          struct rxe_send_wqe *wqe)
719 {
720         int num_sge = ibwr->num_sge;
721         struct ib_sge *sge;
722         int i;
723         u8 *p;
724
725         init_send_wr(qp, &wqe->wr, ibwr);
726
727         if (qp_type(qp) == IB_QPT_UD ||
728             qp_type(qp) == IB_QPT_SMI ||
729             qp_type(qp) == IB_QPT_GSI)
730                 memcpy(&wqe->av, &to_rah(ud_wr(ibwr)->ah)->av, sizeof(wqe->av));
731
732         if (unlikely(ibwr->send_flags & IB_SEND_INLINE)) {
733                 p = wqe->dma.inline_data;
734
735                 sge = ibwr->sg_list;
736                 for (i = 0; i < num_sge; i++, sge++) {
737                         if (qp->is_user && copy_from_user(p, (__user void *)
738                                             (uintptr_t)sge->addr, sge->length))
739                                 return -EFAULT;
740
741                         else if (!qp->is_user)
742                                 memcpy(p, (void *)(uintptr_t)sge->addr,
743                                        sge->length);
744
745                         p += sge->length;
746                 }
747         } else if (mask & WR_REG_MASK) {
748                 wqe->mask = mask;
749                 wqe->state = wqe_state_posted;
750                 return 0;
751         } else
752                 memcpy(wqe->dma.sge, ibwr->sg_list,
753                        num_sge * sizeof(struct ib_sge));
754
755         wqe->iova               = (mask & WR_ATOMIC_MASK) ?
756                                         atomic_wr(ibwr)->remote_addr :
757                                         rdma_wr(ibwr)->remote_addr;
758         wqe->mask               = mask;
759         wqe->dma.length         = length;
760         wqe->dma.resid          = length;
761         wqe->dma.num_sge        = num_sge;
762         wqe->dma.cur_sge        = 0;
763         wqe->dma.sge_offset     = 0;
764         wqe->state              = wqe_state_posted;
765         wqe->ssn                = atomic_add_return(1, &qp->ssn);
766
767         return 0;
768 }
769
770 static int post_one_send(struct rxe_qp *qp, struct ib_send_wr *ibwr,
771                          unsigned int mask, u32 length)
772 {
773         int err;
774         struct rxe_sq *sq = &qp->sq;
775         struct rxe_send_wqe *send_wqe;
776         unsigned long flags;
777
778         err = validate_send_wr(qp, ibwr, mask, length);
779         if (err)
780                 return err;
781
782         spin_lock_irqsave(&qp->sq.sq_lock, flags);
783
784         if (unlikely(queue_full(sq->queue))) {
785                 err = -ENOMEM;
786                 goto err1;
787         }
788
789         send_wqe = producer_addr(sq->queue);
790
791         err = init_send_wqe(qp, ibwr, mask, length, send_wqe);
792         if (unlikely(err))
793                 goto err1;
794
795         /*
796          * make sure all changes to the work queue are
797          * written before we update the producer pointer
798          */
799         smp_wmb();
800
801         advance_producer(sq->queue);
802         spin_unlock_irqrestore(&qp->sq.sq_lock, flags);
803
804         return 0;
805
806 err1:
807         spin_unlock_irqrestore(&qp->sq.sq_lock, flags);
808         return err;
809 }
810
811 static int rxe_post_send_kernel(struct rxe_qp *qp, struct ib_send_wr *wr,
812                                 struct ib_send_wr **bad_wr)
813 {
814         int err = 0;
815         unsigned int mask;
816         unsigned int length = 0;
817         int i;
818         int must_sched;
819
820         while (wr) {
821                 mask = wr_opcode_mask(wr->opcode, qp);
822                 if (unlikely(!mask)) {
823                         err = -EINVAL;
824                         *bad_wr = wr;
825                         break;
826                 }
827
828                 if (unlikely((wr->send_flags & IB_SEND_INLINE) &&
829                              !(mask & WR_INLINE_MASK))) {
830                         err = -EINVAL;
831                         *bad_wr = wr;
832                         break;
833                 }
834
835                 length = 0;
836                 for (i = 0; i < wr->num_sge; i++)
837                         length += wr->sg_list[i].length;
838
839                 err = post_one_send(qp, wr, mask, length);
840
841                 if (err) {
842                         *bad_wr = wr;
843                         break;
844                 }
845                 wr = wr->next;
846         }
847
848         /*
849          * Must sched in case of GSI QP because ib_send_mad() hold irq lock,
850          * and the requester call ip_local_out_sk() that takes spin_lock_bh.
851          */
852         must_sched = (qp_type(qp) == IB_QPT_GSI) ||
853                         (queue_count(qp->sq.queue) > 1);
854
855         rxe_run_task(&qp->req.task, must_sched);
856
857         return err;
858 }
859
860 static int rxe_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
861                          struct ib_send_wr **bad_wr)
862 {
863         struct rxe_qp *qp = to_rqp(ibqp);
864
865         if (unlikely(!qp->valid)) {
866                 *bad_wr = wr;
867                 return -EINVAL;
868         }
869
870         if (unlikely(qp->req.state < QP_STATE_READY)) {
871                 *bad_wr = wr;
872                 return -EINVAL;
873         }
874
875         if (qp->is_user) {
876                 /* Utilize process context to do protocol processing */
877                 rxe_run_task(&qp->req.task, 0);
878                 return 0;
879         } else
880                 return rxe_post_send_kernel(qp, wr, bad_wr);
881 }
882
883 static int rxe_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
884                          struct ib_recv_wr **bad_wr)
885 {
886         int err = 0;
887         struct rxe_qp *qp = to_rqp(ibqp);
888         struct rxe_rq *rq = &qp->rq;
889         unsigned long flags;
890
891         if (unlikely((qp_state(qp) < IB_QPS_INIT) || !qp->valid)) {
892                 *bad_wr = wr;
893                 err = -EINVAL;
894                 goto err1;
895         }
896
897         if (unlikely(qp->srq)) {
898                 *bad_wr = wr;
899                 err = -EINVAL;
900                 goto err1;
901         }
902
903         spin_lock_irqsave(&rq->producer_lock, flags);
904
905         while (wr) {
906                 err = post_one_recv(rq, wr);
907                 if (unlikely(err)) {
908                         *bad_wr = wr;
909                         break;
910                 }
911                 wr = wr->next;
912         }
913
914         spin_unlock_irqrestore(&rq->producer_lock, flags);
915
916 err1:
917         return err;
918 }
919
920 static struct ib_cq *rxe_create_cq(struct ib_device *dev,
921                                    const struct ib_cq_init_attr *attr,
922                                    struct ib_ucontext *context,
923                                    struct ib_udata *udata)
924 {
925         int err;
926         struct rxe_dev *rxe = to_rdev(dev);
927         struct rxe_cq *cq;
928
929         if (attr->flags)
930                 return ERR_PTR(-EINVAL);
931
932         err = rxe_cq_chk_attr(rxe, NULL, attr->cqe, attr->comp_vector, udata);
933         if (err)
934                 goto err1;
935
936         cq = rxe_alloc(&rxe->cq_pool);
937         if (!cq) {
938                 err = -ENOMEM;
939                 goto err1;
940         }
941
942         err = rxe_cq_from_init(rxe, cq, attr->cqe, attr->comp_vector,
943                                context, udata);
944         if (err)
945                 goto err2;
946
947         return &cq->ibcq;
948
949 err2:
950         rxe_drop_ref(cq);
951 err1:
952         return ERR_PTR(err);
953 }
954
955 static int rxe_destroy_cq(struct ib_cq *ibcq)
956 {
957         struct rxe_cq *cq = to_rcq(ibcq);
958
959         rxe_drop_ref(cq);
960         return 0;
961 }
962
963 static int rxe_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
964 {
965         int err;
966         struct rxe_cq *cq = to_rcq(ibcq);
967         struct rxe_dev *rxe = to_rdev(ibcq->device);
968
969         err = rxe_cq_chk_attr(rxe, cq, cqe, 0, udata);
970         if (err)
971                 goto err1;
972
973         err = rxe_cq_resize_queue(cq, cqe, udata);
974         if (err)
975                 goto err1;
976
977         return 0;
978
979 err1:
980         return err;
981 }
982
983 static int rxe_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
984 {
985         int i;
986         struct rxe_cq *cq = to_rcq(ibcq);
987         struct rxe_cqe *cqe;
988         unsigned long flags;
989
990         spin_lock_irqsave(&cq->cq_lock, flags);
991         for (i = 0; i < num_entries; i++) {
992                 cqe = queue_head(cq->queue);
993                 if (!cqe)
994                         break;
995
996                 memcpy(wc++, &cqe->ibwc, sizeof(*wc));
997                 advance_consumer(cq->queue);
998         }
999         spin_unlock_irqrestore(&cq->cq_lock, flags);
1000
1001         return i;
1002 }
1003
1004 static int rxe_peek_cq(struct ib_cq *ibcq, int wc_cnt)
1005 {
1006         struct rxe_cq *cq = to_rcq(ibcq);
1007         int count = queue_count(cq->queue);
1008
1009         return (count > wc_cnt) ? wc_cnt : count;
1010 }
1011
1012 static int rxe_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
1013 {
1014         struct rxe_cq *cq = to_rcq(ibcq);
1015         unsigned long irq_flags;
1016         int ret = 0;
1017
1018         spin_lock_irqsave(&cq->cq_lock, irq_flags);
1019         if (cq->notify != IB_CQ_NEXT_COMP)
1020                 cq->notify = flags & IB_CQ_SOLICITED_MASK;
1021
1022         if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !queue_empty(cq->queue))
1023                 ret = 1;
1024
1025         spin_unlock_irqrestore(&cq->cq_lock, irq_flags);
1026
1027         return ret;
1028 }
1029
1030 static struct ib_mr *rxe_get_dma_mr(struct ib_pd *ibpd, int access)
1031 {
1032         struct rxe_dev *rxe = to_rdev(ibpd->device);
1033         struct rxe_pd *pd = to_rpd(ibpd);
1034         struct rxe_mem *mr;
1035         int err;
1036
1037         mr = rxe_alloc(&rxe->mr_pool);
1038         if (!mr) {
1039                 err = -ENOMEM;
1040                 goto err1;
1041         }
1042
1043         rxe_add_index(mr);
1044
1045         rxe_add_ref(pd);
1046
1047         err = rxe_mem_init_dma(rxe, pd, access, mr);
1048         if (err)
1049                 goto err2;
1050
1051         return &mr->ibmr;
1052
1053 err2:
1054         rxe_drop_ref(pd);
1055         rxe_drop_index(mr);
1056         rxe_drop_ref(mr);
1057 err1:
1058         return ERR_PTR(err);
1059 }
1060
1061 static struct ib_mr *rxe_reg_user_mr(struct ib_pd *ibpd,
1062                                      u64 start,
1063                                      u64 length,
1064                                      u64 iova,
1065                                      int access, struct ib_udata *udata)
1066 {
1067         int err;
1068         struct rxe_dev *rxe = to_rdev(ibpd->device);
1069         struct rxe_pd *pd = to_rpd(ibpd);
1070         struct rxe_mem *mr;
1071
1072         mr = rxe_alloc(&rxe->mr_pool);
1073         if (!mr) {
1074                 err = -ENOMEM;
1075                 goto err2;
1076         }
1077
1078         rxe_add_index(mr);
1079
1080         rxe_add_ref(pd);
1081
1082         err = rxe_mem_init_user(rxe, pd, start, length, iova,
1083                                 access, udata, mr);
1084         if (err)
1085                 goto err3;
1086
1087         return &mr->ibmr;
1088
1089 err3:
1090         rxe_drop_ref(pd);
1091         rxe_drop_index(mr);
1092         rxe_drop_ref(mr);
1093 err2:
1094         return ERR_PTR(err);
1095 }
1096
1097 static int rxe_dereg_mr(struct ib_mr *ibmr)
1098 {
1099         struct rxe_mem *mr = to_rmr(ibmr);
1100
1101         mr->state = RXE_MEM_STATE_ZOMBIE;
1102         rxe_drop_ref(mr->pd);
1103         rxe_drop_index(mr);
1104         rxe_drop_ref(mr);
1105         return 0;
1106 }
1107
1108 static struct ib_mr *rxe_alloc_mr(struct ib_pd *ibpd,
1109                                   enum ib_mr_type mr_type,
1110                                   u32 max_num_sg)
1111 {
1112         struct rxe_dev *rxe = to_rdev(ibpd->device);
1113         struct rxe_pd *pd = to_rpd(ibpd);
1114         struct rxe_mem *mr;
1115         int err;
1116
1117         if (mr_type != IB_MR_TYPE_MEM_REG)
1118                 return ERR_PTR(-EINVAL);
1119
1120         mr = rxe_alloc(&rxe->mr_pool);
1121         if (!mr) {
1122                 err = -ENOMEM;
1123                 goto err1;
1124         }
1125
1126         rxe_add_index(mr);
1127
1128         rxe_add_ref(pd);
1129
1130         err = rxe_mem_init_fast(rxe, pd, max_num_sg, mr);
1131         if (err)
1132                 goto err2;
1133
1134         return &mr->ibmr;
1135
1136 err2:
1137         rxe_drop_ref(pd);
1138         rxe_drop_index(mr);
1139         rxe_drop_ref(mr);
1140 err1:
1141         return ERR_PTR(err);
1142 }
1143
1144 static int rxe_set_page(struct ib_mr *ibmr, u64 addr)
1145 {
1146         struct rxe_mem *mr = to_rmr(ibmr);
1147         struct rxe_map *map;
1148         struct rxe_phys_buf *buf;
1149
1150         if (unlikely(mr->nbuf == mr->num_buf))
1151                 return -ENOMEM;
1152
1153         map = mr->map[mr->nbuf / RXE_BUF_PER_MAP];
1154         buf = &map->buf[mr->nbuf % RXE_BUF_PER_MAP];
1155
1156         buf->addr = addr;
1157         buf->size = ibmr->page_size;
1158         mr->nbuf++;
1159
1160         return 0;
1161 }
1162
1163 static int rxe_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg,
1164                          int sg_nents, unsigned int *sg_offset)
1165 {
1166         struct rxe_mem *mr = to_rmr(ibmr);
1167         int n;
1168
1169         mr->nbuf = 0;
1170
1171         n = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, rxe_set_page);
1172
1173         mr->va = ibmr->iova;
1174         mr->iova = ibmr->iova;
1175         mr->length = ibmr->length;
1176         mr->page_shift = ilog2(ibmr->page_size);
1177         mr->page_mask = ibmr->page_size - 1;
1178         mr->offset = mr->iova & mr->page_mask;
1179
1180         return n;
1181 }
1182
1183 static int rxe_attach_mcast(struct ib_qp *ibqp, union ib_gid *mgid, u16 mlid)
1184 {
1185         int err;
1186         struct rxe_dev *rxe = to_rdev(ibqp->device);
1187         struct rxe_qp *qp = to_rqp(ibqp);
1188         struct rxe_mc_grp *grp;
1189
1190         /* takes a ref on grp if successful */
1191         err = rxe_mcast_get_grp(rxe, mgid, &grp);
1192         if (err)
1193                 return err;
1194
1195         err = rxe_mcast_add_grp_elem(rxe, qp, grp);
1196
1197         rxe_drop_ref(grp);
1198         return err;
1199 }
1200
1201 static int rxe_detach_mcast(struct ib_qp *ibqp, union ib_gid *mgid, u16 mlid)
1202 {
1203         struct rxe_dev *rxe = to_rdev(ibqp->device);
1204         struct rxe_qp *qp = to_rqp(ibqp);
1205
1206         return rxe_mcast_drop_grp_elem(rxe, qp, mgid);
1207 }
1208
1209 static ssize_t rxe_show_parent(struct device *device,
1210                                struct device_attribute *attr, char *buf)
1211 {
1212         struct rxe_dev *rxe = container_of(device, struct rxe_dev,
1213                                            ib_dev.dev);
1214
1215         return snprintf(buf, 16, "%s\n", rxe_parent_name(rxe, 1));
1216 }
1217
1218 static DEVICE_ATTR(parent, S_IRUGO, rxe_show_parent, NULL);
1219
1220 static struct device_attribute *rxe_dev_attributes[] = {
1221         &dev_attr_parent,
1222 };
1223
1224 int rxe_register_device(struct rxe_dev *rxe)
1225 {
1226         int err;
1227         int i;
1228         struct ib_device *dev = &rxe->ib_dev;
1229
1230         strlcpy(dev->name, "rxe%d", IB_DEVICE_NAME_MAX);
1231         strlcpy(dev->node_desc, "rxe", sizeof(dev->node_desc));
1232
1233         dev->owner = THIS_MODULE;
1234         dev->node_type = RDMA_NODE_IB_CA;
1235         dev->phys_port_cnt = 1;
1236         dev->num_comp_vectors = RXE_NUM_COMP_VECTORS;
1237         dev->dma_device = rxe_dma_device(rxe);
1238         dev->local_dma_lkey = 0;
1239         dev->node_guid = rxe_node_guid(rxe);
1240         dev->dma_ops = &rxe_dma_mapping_ops;
1241
1242         dev->uverbs_abi_ver = RXE_UVERBS_ABI_VERSION;
1243         dev->uverbs_cmd_mask = BIT_ULL(IB_USER_VERBS_CMD_GET_CONTEXT)
1244             | BIT_ULL(IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)
1245             | BIT_ULL(IB_USER_VERBS_CMD_QUERY_DEVICE)
1246             | BIT_ULL(IB_USER_VERBS_CMD_QUERY_PORT)
1247             | BIT_ULL(IB_USER_VERBS_CMD_ALLOC_PD)
1248             | BIT_ULL(IB_USER_VERBS_CMD_DEALLOC_PD)
1249             | BIT_ULL(IB_USER_VERBS_CMD_CREATE_SRQ)
1250             | BIT_ULL(IB_USER_VERBS_CMD_MODIFY_SRQ)
1251             | BIT_ULL(IB_USER_VERBS_CMD_QUERY_SRQ)
1252             | BIT_ULL(IB_USER_VERBS_CMD_DESTROY_SRQ)
1253             | BIT_ULL(IB_USER_VERBS_CMD_POST_SRQ_RECV)
1254             | BIT_ULL(IB_USER_VERBS_CMD_CREATE_QP)
1255             | BIT_ULL(IB_USER_VERBS_CMD_MODIFY_QP)
1256             | BIT_ULL(IB_USER_VERBS_CMD_QUERY_QP)
1257             | BIT_ULL(IB_USER_VERBS_CMD_DESTROY_QP)
1258             | BIT_ULL(IB_USER_VERBS_CMD_POST_SEND)
1259             | BIT_ULL(IB_USER_VERBS_CMD_POST_RECV)
1260             | BIT_ULL(IB_USER_VERBS_CMD_CREATE_CQ)
1261             | BIT_ULL(IB_USER_VERBS_CMD_RESIZE_CQ)
1262             | BIT_ULL(IB_USER_VERBS_CMD_DESTROY_CQ)
1263             | BIT_ULL(IB_USER_VERBS_CMD_POLL_CQ)
1264             | BIT_ULL(IB_USER_VERBS_CMD_PEEK_CQ)
1265             | BIT_ULL(IB_USER_VERBS_CMD_REQ_NOTIFY_CQ)
1266             | BIT_ULL(IB_USER_VERBS_CMD_REG_MR)
1267             | BIT_ULL(IB_USER_VERBS_CMD_DEREG_MR)
1268             | BIT_ULL(IB_USER_VERBS_CMD_CREATE_AH)
1269             | BIT_ULL(IB_USER_VERBS_CMD_MODIFY_AH)
1270             | BIT_ULL(IB_USER_VERBS_CMD_QUERY_AH)
1271             | BIT_ULL(IB_USER_VERBS_CMD_DESTROY_AH)
1272             | BIT_ULL(IB_USER_VERBS_CMD_ATTACH_MCAST)
1273             | BIT_ULL(IB_USER_VERBS_CMD_DETACH_MCAST)
1274             ;
1275
1276         dev->query_device = rxe_query_device;
1277         dev->modify_device = rxe_modify_device;
1278         dev->query_port = rxe_query_port;
1279         dev->modify_port = rxe_modify_port;
1280         dev->get_link_layer = rxe_get_link_layer;
1281         dev->query_gid = rxe_query_gid;
1282         dev->get_netdev = rxe_get_netdev;
1283         dev->add_gid = rxe_add_gid;
1284         dev->del_gid = rxe_del_gid;
1285         dev->query_pkey = rxe_query_pkey;
1286         dev->alloc_ucontext = rxe_alloc_ucontext;
1287         dev->dealloc_ucontext = rxe_dealloc_ucontext;
1288         dev->mmap = rxe_mmap;
1289         dev->get_port_immutable = rxe_port_immutable;
1290         dev->alloc_pd = rxe_alloc_pd;
1291         dev->dealloc_pd = rxe_dealloc_pd;
1292         dev->create_ah = rxe_create_ah;
1293         dev->modify_ah = rxe_modify_ah;
1294         dev->query_ah = rxe_query_ah;
1295         dev->destroy_ah = rxe_destroy_ah;
1296         dev->create_srq = rxe_create_srq;
1297         dev->modify_srq = rxe_modify_srq;
1298         dev->query_srq = rxe_query_srq;
1299         dev->destroy_srq = rxe_destroy_srq;
1300         dev->post_srq_recv = rxe_post_srq_recv;
1301         dev->create_qp = rxe_create_qp;
1302         dev->modify_qp = rxe_modify_qp;
1303         dev->query_qp = rxe_query_qp;
1304         dev->destroy_qp = rxe_destroy_qp;
1305         dev->post_send = rxe_post_send;
1306         dev->post_recv = rxe_post_recv;
1307         dev->create_cq = rxe_create_cq;
1308         dev->destroy_cq = rxe_destroy_cq;
1309         dev->resize_cq = rxe_resize_cq;
1310         dev->poll_cq = rxe_poll_cq;
1311         dev->peek_cq = rxe_peek_cq;
1312         dev->req_notify_cq = rxe_req_notify_cq;
1313         dev->get_dma_mr = rxe_get_dma_mr;
1314         dev->reg_user_mr = rxe_reg_user_mr;
1315         dev->dereg_mr = rxe_dereg_mr;
1316         dev->alloc_mr = rxe_alloc_mr;
1317         dev->map_mr_sg = rxe_map_mr_sg;
1318         dev->attach_mcast = rxe_attach_mcast;
1319         dev->detach_mcast = rxe_detach_mcast;
1320
1321         err = ib_register_device(dev, NULL);
1322         if (err) {
1323                 pr_warn("rxe_register_device failed, err = %d\n", err);
1324                 goto err1;
1325         }
1326
1327         for (i = 0; i < ARRAY_SIZE(rxe_dev_attributes); ++i) {
1328                 err = device_create_file(&dev->dev, rxe_dev_attributes[i]);
1329                 if (err) {
1330                         pr_warn("device_create_file failed, i = %d, err = %d\n",
1331                                 i, err);
1332                         goto err2;
1333                 }
1334         }
1335
1336         return 0;
1337
1338 err2:
1339         ib_unregister_device(dev);
1340 err1:
1341         return err;
1342 }
1343
1344 int rxe_unregister_device(struct rxe_dev *rxe)
1345 {
1346         int i;
1347         struct ib_device *dev = &rxe->ib_dev;
1348
1349         for (i = 0; i < ARRAY_SIZE(rxe_dev_attributes); ++i)
1350                 device_remove_file(&dev->dev, rxe_dev_attributes[i]);
1351
1352         ib_unregister_device(dev);
1353
1354         return 0;
1355 }