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