GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / infiniband / sw / rdmavt / vt.c
1 /*
2  * Copyright(c) 2016 - 2018 Intel Corporation.
3  *
4  * This file is provided under a dual BSD/GPLv2 license.  When using or
5  * redistributing this file, you may do so under either license.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of version 2 of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * BSD LICENSE
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  *
24  *  - Redistributions of source code must retain the above copyright
25  *    notice, this list of conditions and the following disclaimer.
26  *  - Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in
28  *    the documentation and/or other materials provided with the
29  *    distribution.
30  *  - Neither the name of Intel Corporation nor the names of its
31  *    contributors may be used to endorse or promote products derived
32  *    from this software without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  */
47
48 #include <linux/module.h>
49 #include <linux/kernel.h>
50 #include <linux/dma-mapping.h>
51 #include "vt.h"
52 #include "cq.h"
53 #include "trace.h"
54
55 #define RVT_UVERBS_ABI_VERSION 2
56
57 MODULE_LICENSE("Dual BSD/GPL");
58 MODULE_DESCRIPTION("RDMA Verbs Transport Library");
59
60 static int rvt_init(void)
61 {
62         int ret = rvt_driver_cq_init();
63
64         if (ret)
65                 pr_err("Error in driver CQ init.\n");
66
67         return ret;
68 }
69 module_init(rvt_init);
70
71 static void rvt_cleanup(void)
72 {
73         rvt_cq_exit();
74 }
75 module_exit(rvt_cleanup);
76
77 /**
78  * rvt_alloc_device - allocate rdi
79  * @size: how big of a structure to allocate
80  * @nports: number of ports to allocate array slots for
81  *
82  * Use IB core device alloc to allocate space for the rdi which is assumed to be
83  * inside of the ib_device. Any extra space that drivers require should be
84  * included in size.
85  *
86  * We also allocate a port array based on the number of ports.
87  *
88  * Return: pointer to allocated rdi
89  */
90 struct rvt_dev_info *rvt_alloc_device(size_t size, int nports)
91 {
92         struct rvt_dev_info *rdi;
93
94         rdi = (struct rvt_dev_info *)ib_alloc_device(size);
95         if (!rdi)
96                 return rdi;
97
98         rdi->ports = kcalloc(nports, sizeof(*rdi->ports), GFP_KERNEL);
99         if (!rdi->ports)
100                 ib_dealloc_device(&rdi->ibdev);
101
102         return rdi;
103 }
104 EXPORT_SYMBOL(rvt_alloc_device);
105
106 /**
107  * rvt_dealloc_device - deallocate rdi
108  * @rdi: structure to free
109  *
110  * Free a structure allocated with rvt_alloc_device()
111  */
112 void rvt_dealloc_device(struct rvt_dev_info *rdi)
113 {
114         kfree(rdi->ports);
115         ib_dealloc_device(&rdi->ibdev);
116 }
117 EXPORT_SYMBOL(rvt_dealloc_device);
118
119 static int rvt_query_device(struct ib_device *ibdev,
120                             struct ib_device_attr *props,
121                             struct ib_udata *uhw)
122 {
123         struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
124
125         if (uhw->inlen || uhw->outlen)
126                 return -EINVAL;
127         /*
128          * Return rvt_dev_info.dparms.props contents
129          */
130         *props = rdi->dparms.props;
131         return 0;
132 }
133
134 static int rvt_modify_device(struct ib_device *device,
135                              int device_modify_mask,
136                              struct ib_device_modify *device_modify)
137 {
138         /*
139          * There is currently no need to supply this based on qib and hfi1.
140          * Future drivers may need to implement this though.
141          */
142
143         return -EOPNOTSUPP;
144 }
145
146 /**
147  * rvt_query_port: Passes the query port call to the driver
148  * @ibdev: Verbs IB dev
149  * @port_num: port number, 1 based from ib core
150  * @props: structure to hold returned properties
151  *
152  * Return: 0 on success
153  */
154 static int rvt_query_port(struct ib_device *ibdev, u8 port_num,
155                           struct ib_port_attr *props)
156 {
157         struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
158         struct rvt_ibport *rvp;
159         int port_index = ibport_num_to_idx(ibdev, port_num);
160
161         if (port_index < 0)
162                 return -EINVAL;
163
164         rvp = rdi->ports[port_index];
165         /* props being zeroed by the caller, avoid zeroing it here */
166         props->sm_lid = rvp->sm_lid;
167         props->sm_sl = rvp->sm_sl;
168         props->port_cap_flags = rvp->port_cap_flags;
169         props->max_msg_sz = 0x80000000;
170         props->pkey_tbl_len = rvt_get_npkeys(rdi);
171         props->bad_pkey_cntr = rvp->pkey_violations;
172         props->qkey_viol_cntr = rvp->qkey_violations;
173         props->subnet_timeout = rvp->subnet_timeout;
174         props->init_type_reply = 0;
175
176         /* Populate the remaining ib_port_attr elements */
177         return rdi->driver_f.query_port_state(rdi, port_num, props);
178 }
179
180 /**
181  * rvt_modify_port
182  * @ibdev: Verbs IB dev
183  * @port_num: Port number, 1 based from ib core
184  * @port_modify_mask: How to change the port
185  * @props: Structure to fill in
186  *
187  * Return: 0 on success
188  */
189 static int rvt_modify_port(struct ib_device *ibdev, u8 port_num,
190                            int port_modify_mask, struct ib_port_modify *props)
191 {
192         struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
193         struct rvt_ibport *rvp;
194         int ret = 0;
195         int port_index = ibport_num_to_idx(ibdev, port_num);
196
197         if (port_index < 0)
198                 return -EINVAL;
199
200         rvp = rdi->ports[port_index];
201         if (port_modify_mask & IB_PORT_OPA_MASK_CHG) {
202                 rvp->port_cap3_flags |= props->set_port_cap_mask;
203                 rvp->port_cap3_flags &= ~props->clr_port_cap_mask;
204         } else {
205                 rvp->port_cap_flags |= props->set_port_cap_mask;
206                 rvp->port_cap_flags &= ~props->clr_port_cap_mask;
207         }
208
209         if (props->set_port_cap_mask || props->clr_port_cap_mask)
210                 rdi->driver_f.cap_mask_chg(rdi, port_num);
211         if (port_modify_mask & IB_PORT_SHUTDOWN)
212                 ret = rdi->driver_f.shut_down_port(rdi, port_num);
213         if (port_modify_mask & IB_PORT_RESET_QKEY_CNTR)
214                 rvp->qkey_violations = 0;
215
216         return ret;
217 }
218
219 /**
220  * rvt_query_pkey - Return a pkey from the table at a given index
221  * @ibdev: Verbs IB dev
222  * @port_num: Port number, 1 based from ib core
223  * @index: Index into pkey table
224  * @pkey: returned pkey from the port pkey table
225  *
226  * Return: 0 on failure pkey otherwise
227  */
228 static int rvt_query_pkey(struct ib_device *ibdev, u8 port_num, u16 index,
229                           u16 *pkey)
230 {
231         /*
232          * Driver will be responsible for keeping rvt_dev_info.pkey_table up to
233          * date. This function will just return that value. There is no need to
234          * lock, if a stale value is read and sent to the user so be it there is
235          * no way to protect against that anyway.
236          */
237         struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
238         int port_index;
239
240         port_index = ibport_num_to_idx(ibdev, port_num);
241         if (port_index < 0)
242                 return -EINVAL;
243
244         if (index >= rvt_get_npkeys(rdi))
245                 return -EINVAL;
246
247         *pkey = rvt_get_pkey(rdi, port_index, index);
248         return 0;
249 }
250
251 /**
252  * rvt_query_gid - Return a gid from the table
253  * @ibdev: Verbs IB dev
254  * @port_num: Port number, 1 based from ib core
255  * @guid_index: Index in table
256  * @gid: Gid to return
257  *
258  * Return: 0 on success
259  */
260 static int rvt_query_gid(struct ib_device *ibdev, u8 port_num,
261                          int guid_index, union ib_gid *gid)
262 {
263         struct rvt_dev_info *rdi;
264         struct rvt_ibport *rvp;
265         int port_index;
266
267         /*
268          * Driver is responsible for updating the guid table. Which will be used
269          * to craft the return value. This will work similar to how query_pkey()
270          * is being done.
271          */
272         port_index = ibport_num_to_idx(ibdev, port_num);
273         if (port_index < 0)
274                 return -EINVAL;
275
276         rdi = ib_to_rvt(ibdev);
277         rvp = rdi->ports[port_index];
278
279         gid->global.subnet_prefix = rvp->gid_prefix;
280
281         return rdi->driver_f.get_guid_be(rdi, rvp, guid_index,
282                                          &gid->global.interface_id);
283 }
284
285 struct rvt_ucontext {
286         struct ib_ucontext ibucontext;
287 };
288
289 static inline struct rvt_ucontext *to_iucontext(struct ib_ucontext
290                                                 *ibucontext)
291 {
292         return container_of(ibucontext, struct rvt_ucontext, ibucontext);
293 }
294
295 /**
296  * rvt_alloc_ucontext - Allocate a user context
297  * @ibdev: Verbs IB dev
298  * @udata: User data allocated
299  */
300 static struct ib_ucontext *rvt_alloc_ucontext(struct ib_device *ibdev,
301                                               struct ib_udata *udata)
302 {
303         struct rvt_ucontext *context;
304
305         context = kmalloc(sizeof(*context), GFP_KERNEL);
306         if (!context)
307                 return ERR_PTR(-ENOMEM);
308         return &context->ibucontext;
309 }
310
311 /**
312  *rvt_dealloc_ucontext - Free a user context
313  *@context - Free this
314  */
315 static int rvt_dealloc_ucontext(struct ib_ucontext *context)
316 {
317         kfree(to_iucontext(context));
318         return 0;
319 }
320
321 static int rvt_get_port_immutable(struct ib_device *ibdev, u8 port_num,
322                                   struct ib_port_immutable *immutable)
323 {
324         struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
325         struct ib_port_attr attr;
326         int err, port_index;
327
328         port_index = ibport_num_to_idx(ibdev, port_num);
329         if (port_index < 0)
330                 return -EINVAL;
331
332         immutable->core_cap_flags = rdi->dparms.core_cap_flags;
333
334         err = ib_query_port(ibdev, port_num, &attr);
335         if (err)
336                 return err;
337
338         immutable->pkey_tbl_len = attr.pkey_tbl_len;
339         immutable->gid_tbl_len = attr.gid_tbl_len;
340         immutable->max_mad_size = rdi->dparms.max_mad_size;
341
342         return 0;
343 }
344
345 enum {
346         MISC,
347         QUERY_DEVICE,
348         MODIFY_DEVICE,
349         QUERY_PORT,
350         MODIFY_PORT,
351         QUERY_PKEY,
352         QUERY_GID,
353         ALLOC_UCONTEXT,
354         DEALLOC_UCONTEXT,
355         GET_PORT_IMMUTABLE,
356         CREATE_QP,
357         MODIFY_QP,
358         DESTROY_QP,
359         QUERY_QP,
360         POST_SEND,
361         POST_RECV,
362         POST_SRQ_RECV,
363         CREATE_AH,
364         DESTROY_AH,
365         MODIFY_AH,
366         QUERY_AH,
367         CREATE_SRQ,
368         MODIFY_SRQ,
369         DESTROY_SRQ,
370         QUERY_SRQ,
371         ATTACH_MCAST,
372         DETACH_MCAST,
373         GET_DMA_MR,
374         REG_USER_MR,
375         DEREG_MR,
376         ALLOC_MR,
377         MAP_MR_SG,
378         ALLOC_FMR,
379         MAP_PHYS_FMR,
380         UNMAP_FMR,
381         DEALLOC_FMR,
382         MMAP,
383         CREATE_CQ,
384         DESTROY_CQ,
385         POLL_CQ,
386         REQ_NOTFIY_CQ,
387         RESIZE_CQ,
388         ALLOC_PD,
389         DEALLOC_PD,
390         _VERB_IDX_MAX /* Must always be last! */
391 };
392
393 static inline int check_driver_override(struct rvt_dev_info *rdi,
394                                         size_t offset, void *func)
395 {
396         if (!*(void **)((void *)&rdi->ibdev + offset)) {
397                 *(void **)((void *)&rdi->ibdev + offset) = func;
398                 return 0;
399         }
400
401         return 1;
402 }
403
404 static noinline int check_support(struct rvt_dev_info *rdi, int verb)
405 {
406         switch (verb) {
407         case MISC:
408                 /*
409                  * These functions are not part of verbs specifically but are
410                  * required for rdmavt to function.
411                  */
412                 if ((!rdi->driver_f.port_callback) ||
413                     (!rdi->driver_f.get_pci_dev))
414                         return -EINVAL;
415                 break;
416
417         case QUERY_DEVICE:
418                 check_driver_override(rdi, offsetof(struct ib_device,
419                                                     query_device),
420                                                     rvt_query_device);
421                 break;
422
423         case MODIFY_DEVICE:
424                 /*
425                  * rdmavt does not support modify device currently drivers must
426                  * provide.
427                  */
428                 if (!check_driver_override(rdi, offsetof(struct ib_device,
429                                                          modify_device),
430                                            rvt_modify_device))
431                         return -EOPNOTSUPP;
432                 break;
433
434         case QUERY_PORT:
435                 if (!check_driver_override(rdi, offsetof(struct ib_device,
436                                                          query_port),
437                                            rvt_query_port))
438                         if (!rdi->driver_f.query_port_state)
439                                 return -EINVAL;
440                 break;
441
442         case MODIFY_PORT:
443                 if (!check_driver_override(rdi, offsetof(struct ib_device,
444                                                          modify_port),
445                                            rvt_modify_port))
446                         if (!rdi->driver_f.cap_mask_chg ||
447                             !rdi->driver_f.shut_down_port)
448                                 return -EINVAL;
449                 break;
450
451         case QUERY_PKEY:
452                 check_driver_override(rdi, offsetof(struct ib_device,
453                                                     query_pkey),
454                                       rvt_query_pkey);
455                 break;
456
457         case QUERY_GID:
458                 if (!check_driver_override(rdi, offsetof(struct ib_device,
459                                                          query_gid),
460                                            rvt_query_gid))
461                         if (!rdi->driver_f.get_guid_be)
462                                 return -EINVAL;
463                 break;
464
465         case ALLOC_UCONTEXT:
466                 check_driver_override(rdi, offsetof(struct ib_device,
467                                                     alloc_ucontext),
468                                       rvt_alloc_ucontext);
469                 break;
470
471         case DEALLOC_UCONTEXT:
472                 check_driver_override(rdi, offsetof(struct ib_device,
473                                                     dealloc_ucontext),
474                                       rvt_dealloc_ucontext);
475                 break;
476
477         case GET_PORT_IMMUTABLE:
478                 check_driver_override(rdi, offsetof(struct ib_device,
479                                                     get_port_immutable),
480                                       rvt_get_port_immutable);
481                 break;
482
483         case CREATE_QP:
484                 if (!check_driver_override(rdi, offsetof(struct ib_device,
485                                                          create_qp),
486                                            rvt_create_qp))
487                         if (!rdi->driver_f.qp_priv_alloc ||
488                             !rdi->driver_f.qp_priv_free ||
489                             !rdi->driver_f.notify_qp_reset ||
490                             !rdi->driver_f.flush_qp_waiters ||
491                             !rdi->driver_f.stop_send_queue ||
492                             !rdi->driver_f.quiesce_qp)
493                                 return -EINVAL;
494                 break;
495
496         case MODIFY_QP:
497                 if (!check_driver_override(rdi, offsetof(struct ib_device,
498                                                          modify_qp),
499                                            rvt_modify_qp))
500                         if (!rdi->driver_f.notify_qp_reset ||
501                             !rdi->driver_f.schedule_send ||
502                             !rdi->driver_f.get_pmtu_from_attr ||
503                             !rdi->driver_f.flush_qp_waiters ||
504                             !rdi->driver_f.stop_send_queue ||
505                             !rdi->driver_f.quiesce_qp ||
506                             !rdi->driver_f.notify_error_qp ||
507                             !rdi->driver_f.mtu_from_qp ||
508                             !rdi->driver_f.mtu_to_path_mtu)
509                                 return -EINVAL;
510                 break;
511
512         case DESTROY_QP:
513                 if (!check_driver_override(rdi, offsetof(struct ib_device,
514                                                          destroy_qp),
515                                            rvt_destroy_qp))
516                         if (!rdi->driver_f.qp_priv_free ||
517                             !rdi->driver_f.notify_qp_reset ||
518                             !rdi->driver_f.flush_qp_waiters ||
519                             !rdi->driver_f.stop_send_queue ||
520                             !rdi->driver_f.quiesce_qp)
521                                 return -EINVAL;
522                 break;
523
524         case QUERY_QP:
525                 check_driver_override(rdi, offsetof(struct ib_device,
526                                                     query_qp),
527                                                     rvt_query_qp);
528                 break;
529
530         case POST_SEND:
531                 if (!check_driver_override(rdi, offsetof(struct ib_device,
532                                                          post_send),
533                                            rvt_post_send))
534                         if (!rdi->driver_f.schedule_send ||
535                             !rdi->driver_f.do_send ||
536                             !rdi->post_parms)
537                                 return -EINVAL;
538                 break;
539
540         case POST_RECV:
541                 check_driver_override(rdi, offsetof(struct ib_device,
542                                                     post_recv),
543                                       rvt_post_recv);
544                 break;
545         case POST_SRQ_RECV:
546                 check_driver_override(rdi, offsetof(struct ib_device,
547                                                     post_srq_recv),
548                                       rvt_post_srq_recv);
549                 break;
550
551         case CREATE_AH:
552                 check_driver_override(rdi, offsetof(struct ib_device,
553                                                     create_ah),
554                                       rvt_create_ah);
555                 break;
556
557         case DESTROY_AH:
558                 check_driver_override(rdi, offsetof(struct ib_device,
559                                                     destroy_ah),
560                                       rvt_destroy_ah);
561                 break;
562
563         case MODIFY_AH:
564                 check_driver_override(rdi, offsetof(struct ib_device,
565                                                     modify_ah),
566                                       rvt_modify_ah);
567                 break;
568
569         case QUERY_AH:
570                 check_driver_override(rdi, offsetof(struct ib_device,
571                                                     query_ah),
572                                       rvt_query_ah);
573                 break;
574
575         case CREATE_SRQ:
576                 check_driver_override(rdi, offsetof(struct ib_device,
577                                                     create_srq),
578                                       rvt_create_srq);
579                 break;
580
581         case MODIFY_SRQ:
582                 check_driver_override(rdi, offsetof(struct ib_device,
583                                                     modify_srq),
584                                       rvt_modify_srq);
585                 break;
586
587         case DESTROY_SRQ:
588                 check_driver_override(rdi, offsetof(struct ib_device,
589                                                     destroy_srq),
590                                       rvt_destroy_srq);
591                 break;
592
593         case QUERY_SRQ:
594                 check_driver_override(rdi, offsetof(struct ib_device,
595                                                     query_srq),
596                                       rvt_query_srq);
597                 break;
598
599         case ATTACH_MCAST:
600                 check_driver_override(rdi, offsetof(struct ib_device,
601                                                     attach_mcast),
602                                       rvt_attach_mcast);
603                 break;
604
605         case DETACH_MCAST:
606                 check_driver_override(rdi, offsetof(struct ib_device,
607                                                     detach_mcast),
608                                       rvt_detach_mcast);
609                 break;
610
611         case GET_DMA_MR:
612                 check_driver_override(rdi, offsetof(struct ib_device,
613                                                     get_dma_mr),
614                                       rvt_get_dma_mr);
615                 break;
616
617         case REG_USER_MR:
618                 check_driver_override(rdi, offsetof(struct ib_device,
619                                                     reg_user_mr),
620                                       rvt_reg_user_mr);
621                 break;
622
623         case DEREG_MR:
624                 check_driver_override(rdi, offsetof(struct ib_device,
625                                                     dereg_mr),
626                                       rvt_dereg_mr);
627                 break;
628
629         case ALLOC_FMR:
630                 check_driver_override(rdi, offsetof(struct ib_device,
631                                                     alloc_fmr),
632                                       rvt_alloc_fmr);
633                 break;
634
635         case ALLOC_MR:
636                 check_driver_override(rdi, offsetof(struct ib_device,
637                                                     alloc_mr),
638                                       rvt_alloc_mr);
639                 break;
640
641         case MAP_MR_SG:
642                 check_driver_override(rdi, offsetof(struct ib_device,
643                                                     map_mr_sg),
644                                       rvt_map_mr_sg);
645                 break;
646
647         case MAP_PHYS_FMR:
648                 check_driver_override(rdi, offsetof(struct ib_device,
649                                                     map_phys_fmr),
650                                       rvt_map_phys_fmr);
651                 break;
652
653         case UNMAP_FMR:
654                 check_driver_override(rdi, offsetof(struct ib_device,
655                                                     unmap_fmr),
656                                       rvt_unmap_fmr);
657                 break;
658
659         case DEALLOC_FMR:
660                 check_driver_override(rdi, offsetof(struct ib_device,
661                                                     dealloc_fmr),
662                                       rvt_dealloc_fmr);
663                 break;
664
665         case MMAP:
666                 check_driver_override(rdi, offsetof(struct ib_device,
667                                                     mmap),
668                                       rvt_mmap);
669                 break;
670
671         case CREATE_CQ:
672                 check_driver_override(rdi, offsetof(struct ib_device,
673                                                     create_cq),
674                                       rvt_create_cq);
675                 break;
676
677         case DESTROY_CQ:
678                 check_driver_override(rdi, offsetof(struct ib_device,
679                                                     destroy_cq),
680                                       rvt_destroy_cq);
681                 break;
682
683         case POLL_CQ:
684                 check_driver_override(rdi, offsetof(struct ib_device,
685                                                     poll_cq),
686                                       rvt_poll_cq);
687                 break;
688
689         case REQ_NOTFIY_CQ:
690                 check_driver_override(rdi, offsetof(struct ib_device,
691                                                     req_notify_cq),
692                                       rvt_req_notify_cq);
693                 break;
694
695         case RESIZE_CQ:
696                 check_driver_override(rdi, offsetof(struct ib_device,
697                                                     resize_cq),
698                                       rvt_resize_cq);
699                 break;
700
701         case ALLOC_PD:
702                 check_driver_override(rdi, offsetof(struct ib_device,
703                                                     alloc_pd),
704                                       rvt_alloc_pd);
705                 break;
706
707         case DEALLOC_PD:
708                 check_driver_override(rdi, offsetof(struct ib_device,
709                                                     dealloc_pd),
710                                       rvt_dealloc_pd);
711                 break;
712
713         default:
714                 return -EINVAL;
715         }
716
717         return 0;
718 }
719
720 /**
721  * rvt_register_device - register a driver
722  * @rdi: main dev structure for all of rdmavt operations
723  *
724  * It is up to drivers to allocate the rdi and fill in the appropriate
725  * information.
726  *
727  * Return: 0 on success otherwise an errno.
728  */
729 int rvt_register_device(struct rvt_dev_info *rdi, u32 driver_id)
730 {
731         int ret = 0, i;
732
733         if (!rdi)
734                 return -EINVAL;
735
736         /*
737          * Check to ensure drivers have setup the required helpers for the verbs
738          * they want rdmavt to handle
739          */
740         for (i = 0; i < _VERB_IDX_MAX; i++)
741                 if (check_support(rdi, i)) {
742                         pr_err("Driver support req not met at %d\n", i);
743                         return -EINVAL;
744                 }
745
746
747         /* Once we get past here we can use rvt_pr macros and tracepoints */
748         trace_rvt_dbg(rdi, "Driver attempting registration");
749         rvt_mmap_init(rdi);
750
751         /* Queue Pairs */
752         ret = rvt_driver_qp_init(rdi);
753         if (ret) {
754                 pr_err("Error in driver QP init.\n");
755                 return -EINVAL;
756         }
757
758         /* Address Handle */
759         spin_lock_init(&rdi->n_ahs_lock);
760         rdi->n_ahs_allocated = 0;
761
762         /* Shared Receive Queue */
763         rvt_driver_srq_init(rdi);
764
765         /* Multicast */
766         rvt_driver_mcast_init(rdi);
767
768         /* Mem Region */
769         ret = rvt_driver_mr_init(rdi);
770         if (ret) {
771                 pr_err("Error in driver MR init.\n");
772                 goto bail_no_mr;
773         }
774
775         /* Completion queues */
776         spin_lock_init(&rdi->n_cqs_lock);
777
778         /* DMA Operations */
779         rdi->ibdev.dev.dma_ops = rdi->ibdev.dev.dma_ops ? : &dma_virt_ops;
780
781         /* Protection Domain */
782         spin_lock_init(&rdi->n_pds_lock);
783         rdi->n_pds_allocated = 0;
784
785         /*
786          * There are some things which could be set by underlying drivers but
787          * really should be up to rdmavt to set. For instance drivers can't know
788          * exactly which functions rdmavt supports, nor do they know the ABI
789          * version, so we do all of this sort of stuff here.
790          */
791         rdi->ibdev.uverbs_abi_ver = RVT_UVERBS_ABI_VERSION;
792         rdi->ibdev.uverbs_cmd_mask =
793                 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT)         |
794                 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)        |
795                 (1ull << IB_USER_VERBS_CMD_QUERY_PORT)          |
796                 (1ull << IB_USER_VERBS_CMD_ALLOC_PD)            |
797                 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD)          |
798                 (1ull << IB_USER_VERBS_CMD_CREATE_AH)           |
799                 (1ull << IB_USER_VERBS_CMD_MODIFY_AH)           |
800                 (1ull << IB_USER_VERBS_CMD_QUERY_AH)            |
801                 (1ull << IB_USER_VERBS_CMD_DESTROY_AH)          |
802                 (1ull << IB_USER_VERBS_CMD_REG_MR)              |
803                 (1ull << IB_USER_VERBS_CMD_DEREG_MR)            |
804                 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
805                 (1ull << IB_USER_VERBS_CMD_CREATE_CQ)           |
806                 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ)           |
807                 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ)          |
808                 (1ull << IB_USER_VERBS_CMD_POLL_CQ)             |
809                 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ)       |
810                 (1ull << IB_USER_VERBS_CMD_CREATE_QP)           |
811                 (1ull << IB_USER_VERBS_CMD_QUERY_QP)            |
812                 (1ull << IB_USER_VERBS_CMD_MODIFY_QP)           |
813                 (1ull << IB_USER_VERBS_CMD_DESTROY_QP)          |
814                 (1ull << IB_USER_VERBS_CMD_POST_SEND)           |
815                 (1ull << IB_USER_VERBS_CMD_POST_RECV)           |
816                 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)        |
817                 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST)        |
818                 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ)          |
819                 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)          |
820                 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ)           |
821                 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)         |
822                 (1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
823         rdi->ibdev.node_type = RDMA_NODE_IB_CA;
824         if (!rdi->ibdev.num_comp_vectors)
825                 rdi->ibdev.num_comp_vectors = 1;
826
827         rdi->ibdev.driver_id = driver_id;
828         /* We are now good to announce we exist */
829         ret =  ib_register_device(&rdi->ibdev, rdi->driver_f.port_callback);
830         if (ret) {
831                 rvt_pr_err(rdi, "Failed to register driver with ib core.\n");
832                 goto bail_mr;
833         }
834
835         rvt_create_mad_agents(rdi);
836
837         rvt_pr_info(rdi, "Registration with rdmavt done.\n");
838         return ret;
839
840 bail_mr:
841         rvt_mr_exit(rdi);
842
843 bail_no_mr:
844         rvt_qp_exit(rdi);
845
846         return ret;
847 }
848 EXPORT_SYMBOL(rvt_register_device);
849
850 /**
851  * rvt_unregister_device - remove a driver
852  * @rdi: rvt dev struct
853  */
854 void rvt_unregister_device(struct rvt_dev_info *rdi)
855 {
856         trace_rvt_dbg(rdi, "Driver is unregistering.");
857         if (!rdi)
858                 return;
859
860         rvt_free_mad_agents(rdi);
861
862         ib_unregister_device(&rdi->ibdev);
863         rvt_mr_exit(rdi);
864         rvt_qp_exit(rdi);
865 }
866 EXPORT_SYMBOL(rvt_unregister_device);
867
868 /**
869  * rvt_init_port - init internal data for driver port
870  * @rdi: rvt dev strut
871  * @port: rvt port
872  * @port_index: 0 based index of ports, different from IB core port num
873  *
874  * Keep track of a list of ports. No need to have a detach port.
875  * They persist until the driver goes away.
876  *
877  * Return: always 0
878  */
879 int rvt_init_port(struct rvt_dev_info *rdi, struct rvt_ibport *port,
880                   int port_index, u16 *pkey_table)
881 {
882
883         rdi->ports[port_index] = port;
884         rdi->ports[port_index]->pkey_table = pkey_table;
885
886         return 0;
887 }
888 EXPORT_SYMBOL(rvt_init_port);