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