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