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