xref: /linux/drivers/infiniband/sw/rdmavt/vt.c (revision 82ffd0454bd9bd57780966d47bfd56d579dd4fb3)
1 /*
2  * Copyright(c) 2016 - 2018 Intel Corporation.
3  *
4  * This file is provided under a dual BSD/GPLv2 license.  When using or
5  * redistributing this file, you may do so under either license.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of version 2 of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * BSD LICENSE
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  *
24  *  - Redistributions of source code must retain the above copyright
25  *    notice, this list of conditions and the following disclaimer.
26  *  - Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in
28  *    the documentation and/or other materials provided with the
29  *    distribution.
30  *  - Neither the name of Intel Corporation nor the names of its
31  *    contributors may be used to endorse or promote products derived
32  *    from this software without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  */
47 
48 #include <linux/module.h>
49 #include <linux/kernel.h>
50 #include <linux/dma-mapping.h>
51 #include "vt.h"
52 #include "cq.h"
53 #include "trace.h"
54 
55 #define RVT_UVERBS_ABI_VERSION 2
56 
57 MODULE_LICENSE("Dual BSD/GPL");
58 MODULE_DESCRIPTION("RDMA Verbs Transport Library");
59 
60 static int rvt_init(void)
61 {
62 	int ret = rvt_driver_cq_init();
63 
64 	if (ret)
65 		pr_err("Error in driver CQ init.\n");
66 
67 	return ret;
68 }
69 module_init(rvt_init);
70 
71 static void rvt_cleanup(void)
72 {
73 	rvt_cq_exit();
74 }
75 module_exit(rvt_cleanup);
76 
77 /**
78  * rvt_alloc_device - allocate rdi
79  * @size: how big of a structure to allocate
80  * @nports: number of ports to allocate array slots for
81  *
82  * Use IB core device alloc to allocate space for the rdi which is assumed to be
83  * inside of the ib_device. Any extra space that drivers require should be
84  * included in size.
85  *
86  * We also allocate a port array based on the number of ports.
87  *
88  * Return: pointer to allocated rdi
89  */
90 struct rvt_dev_info *rvt_alloc_device(size_t size, int nports)
91 {
92 	struct rvt_dev_info *rdi;
93 
94 	rdi = (struct rvt_dev_info *)ib_alloc_device(size);
95 	if (!rdi)
96 		return rdi;
97 
98 	rdi->ports = kcalloc(nports,
99 			     sizeof(struct rvt_ibport **),
100 			     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  * @index: Index into pkey table
226  * @pkey: returned pkey from the port pkey table
227  *
228  * Return: 0 on failure pkey otherwise
229  */
230 static int rvt_query_pkey(struct ib_device *ibdev, u8 port_num, u16 index,
231 			  u16 *pkey)
232 {
233 	/*
234 	 * Driver will be responsible for keeping rvt_dev_info.pkey_table up to
235 	 * date. This function will just return that value. There is no need to
236 	 * lock, if a stale value is read and sent to the user so be it there is
237 	 * no way to protect against that anyway.
238 	 */
239 	struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
240 	int port_index;
241 
242 	port_index = ibport_num_to_idx(ibdev, port_num);
243 	if (port_index < 0)
244 		return -EINVAL;
245 
246 	if (index >= rvt_get_npkeys(rdi))
247 		return -EINVAL;
248 
249 	*pkey = rvt_get_pkey(rdi, port_index, index);
250 	return 0;
251 }
252 
253 /**
254  * rvt_query_gid - Return a gid from the table
255  * @ibdev: Verbs IB dev
256  * @port_num: Port number, 1 based from ib core
257  * @guid_index: Index in table
258  * @gid: Gid to return
259  *
260  * Return: 0 on success
261  */
262 static int rvt_query_gid(struct ib_device *ibdev, u8 port_num,
263 			 int guid_index, union ib_gid *gid)
264 {
265 	struct rvt_dev_info *rdi;
266 	struct rvt_ibport *rvp;
267 	int port_index;
268 
269 	/*
270 	 * Driver is responsible for updating the guid table. Which will be used
271 	 * to craft the return value. This will work similar to how query_pkey()
272 	 * is being done.
273 	 */
274 	port_index = ibport_num_to_idx(ibdev, port_num);
275 	if (port_index < 0)
276 		return -EINVAL;
277 
278 	rdi = ib_to_rvt(ibdev);
279 	rvp = rdi->ports[port_index];
280 
281 	gid->global.subnet_prefix = rvp->gid_prefix;
282 
283 	return rdi->driver_f.get_guid_be(rdi, rvp, guid_index,
284 					 &gid->global.interface_id);
285 }
286 
287 struct rvt_ucontext {
288 	struct ib_ucontext ibucontext;
289 };
290 
291 static inline struct rvt_ucontext *to_iucontext(struct ib_ucontext
292 						*ibucontext)
293 {
294 	return container_of(ibucontext, struct rvt_ucontext, ibucontext);
295 }
296 
297 /**
298  * rvt_alloc_ucontext - Allocate a user context
299  * @ibdev: Verbs IB dev
300  * @udata: User data allocated
301  */
302 static struct ib_ucontext *rvt_alloc_ucontext(struct ib_device *ibdev,
303 					      struct ib_udata *udata)
304 {
305 	struct rvt_ucontext *context;
306 
307 	context = kmalloc(sizeof(*context), GFP_KERNEL);
308 	if (!context)
309 		return ERR_PTR(-ENOMEM);
310 	return &context->ibucontext;
311 }
312 
313 /**
314  *rvt_dealloc_ucontext - Free a user context
315  *@context - Free this
316  */
317 static int rvt_dealloc_ucontext(struct ib_ucontext *context)
318 {
319 	kfree(to_iucontext(context));
320 	return 0;
321 }
322 
323 static int rvt_get_port_immutable(struct ib_device *ibdev, u8 port_num,
324 				  struct ib_port_immutable *immutable)
325 {
326 	struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
327 	struct ib_port_attr attr;
328 	int err, port_index;
329 
330 	port_index = ibport_num_to_idx(ibdev, port_num);
331 	if (port_index < 0)
332 		return -EINVAL;
333 
334 	immutable->core_cap_flags = rdi->dparms.core_cap_flags;
335 
336 	err = ib_query_port(ibdev, port_num, &attr);
337 	if (err)
338 		return err;
339 
340 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
341 	immutable->gid_tbl_len = attr.gid_tbl_len;
342 	immutable->max_mad_size = rdi->dparms.max_mad_size;
343 
344 	return 0;
345 }
346 
347 enum {
348 	MISC,
349 	QUERY_DEVICE,
350 	MODIFY_DEVICE,
351 	QUERY_PORT,
352 	MODIFY_PORT,
353 	QUERY_PKEY,
354 	QUERY_GID,
355 	ALLOC_UCONTEXT,
356 	DEALLOC_UCONTEXT,
357 	GET_PORT_IMMUTABLE,
358 	CREATE_QP,
359 	MODIFY_QP,
360 	DESTROY_QP,
361 	QUERY_QP,
362 	POST_SEND,
363 	POST_RECV,
364 	POST_SRQ_RECV,
365 	CREATE_AH,
366 	DESTROY_AH,
367 	MODIFY_AH,
368 	QUERY_AH,
369 	CREATE_SRQ,
370 	MODIFY_SRQ,
371 	DESTROY_SRQ,
372 	QUERY_SRQ,
373 	ATTACH_MCAST,
374 	DETACH_MCAST,
375 	GET_DMA_MR,
376 	REG_USER_MR,
377 	DEREG_MR,
378 	ALLOC_MR,
379 	MAP_MR_SG,
380 	ALLOC_FMR,
381 	MAP_PHYS_FMR,
382 	UNMAP_FMR,
383 	DEALLOC_FMR,
384 	MMAP,
385 	CREATE_CQ,
386 	DESTROY_CQ,
387 	POLL_CQ,
388 	REQ_NOTFIY_CQ,
389 	RESIZE_CQ,
390 	ALLOC_PD,
391 	DEALLOC_PD,
392 	_VERB_IDX_MAX /* Must always be last! */
393 };
394 
395 static const struct ib_device_ops rvt_dev_ops = {
396 	.alloc_fmr = rvt_alloc_fmr,
397 	.alloc_mr = rvt_alloc_mr,
398 	.alloc_pd = rvt_alloc_pd,
399 	.alloc_ucontext = rvt_alloc_ucontext,
400 	.attach_mcast = rvt_attach_mcast,
401 	.create_ah = rvt_create_ah,
402 	.create_cq = rvt_create_cq,
403 	.create_qp = rvt_create_qp,
404 	.create_srq = rvt_create_srq,
405 	.dealloc_fmr = rvt_dealloc_fmr,
406 	.dealloc_pd = rvt_dealloc_pd,
407 	.dealloc_ucontext = rvt_dealloc_ucontext,
408 	.dereg_mr = rvt_dereg_mr,
409 	.destroy_ah = rvt_destroy_ah,
410 	.destroy_cq = rvt_destroy_cq,
411 	.destroy_qp = rvt_destroy_qp,
412 	.destroy_srq = rvt_destroy_srq,
413 	.detach_mcast = rvt_detach_mcast,
414 	.get_dma_mr = rvt_get_dma_mr,
415 	.get_port_immutable = rvt_get_port_immutable,
416 	.map_mr_sg = rvt_map_mr_sg,
417 	.map_phys_fmr = rvt_map_phys_fmr,
418 	.mmap = rvt_mmap,
419 	.modify_ah = rvt_modify_ah,
420 	.modify_device = rvt_modify_device,
421 	.modify_port = rvt_modify_port,
422 	.modify_qp = rvt_modify_qp,
423 	.modify_srq = rvt_modify_srq,
424 	.poll_cq = rvt_poll_cq,
425 	.post_recv = rvt_post_recv,
426 	.post_send = rvt_post_send,
427 	.post_srq_recv = rvt_post_srq_recv,
428 	.query_ah = rvt_query_ah,
429 	.query_device = rvt_query_device,
430 	.query_gid = rvt_query_gid,
431 	.query_pkey = rvt_query_pkey,
432 	.query_port = rvt_query_port,
433 	.query_qp = rvt_query_qp,
434 	.query_srq = rvt_query_srq,
435 	.reg_user_mr = rvt_reg_user_mr,
436 	.req_notify_cq = rvt_req_notify_cq,
437 	.resize_cq = rvt_resize_cq,
438 	.unmap_fmr = rvt_unmap_fmr,
439 };
440 
441 static noinline int check_support(struct rvt_dev_info *rdi, int verb)
442 {
443 	switch (verb) {
444 	case MISC:
445 		/*
446 		 * These functions are not part of verbs specifically but are
447 		 * required for rdmavt to function.
448 		 */
449 		if ((!rdi->driver_f.port_callback) ||
450 		    (!rdi->driver_f.get_pci_dev))
451 			return -EINVAL;
452 		break;
453 
454 	case MODIFY_DEVICE:
455 		/*
456 		 * rdmavt does not support modify device currently drivers must
457 		 * provide.
458 		 */
459 		if (!rdi->ibdev.ops.modify_device)
460 			return -EOPNOTSUPP;
461 		break;
462 
463 	case QUERY_PORT:
464 		if (!rdi->ibdev.ops.query_port)
465 			if (!rdi->driver_f.query_port_state)
466 				return -EINVAL;
467 		break;
468 
469 	case MODIFY_PORT:
470 		if (!rdi->ibdev.ops.modify_port)
471 			if (!rdi->driver_f.cap_mask_chg ||
472 			    !rdi->driver_f.shut_down_port)
473 				return -EINVAL;
474 		break;
475 
476 	case QUERY_GID:
477 		if (!rdi->ibdev.ops.query_gid)
478 			if (!rdi->driver_f.get_guid_be)
479 				return -EINVAL;
480 		break;
481 
482 	case CREATE_QP:
483 		if (!rdi->ibdev.ops.create_qp)
484 			if (!rdi->driver_f.qp_priv_alloc ||
485 			    !rdi->driver_f.qp_priv_free ||
486 			    !rdi->driver_f.notify_qp_reset ||
487 			    !rdi->driver_f.flush_qp_waiters ||
488 			    !rdi->driver_f.stop_send_queue ||
489 			    !rdi->driver_f.quiesce_qp)
490 				return -EINVAL;
491 		break;
492 
493 	case MODIFY_QP:
494 		if (!rdi->ibdev.ops.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 (!rdi->ibdev.ops.destroy_qp)
509 			if (!rdi->driver_f.qp_priv_free ||
510 			    !rdi->driver_f.notify_qp_reset ||
511 			    !rdi->driver_f.flush_qp_waiters ||
512 			    !rdi->driver_f.stop_send_queue ||
513 			    !rdi->driver_f.quiesce_qp)
514 				return -EINVAL;
515 		break;
516 
517 	case POST_SEND:
518 		if (!rdi->ibdev.ops.post_send)
519 			if (!rdi->driver_f.schedule_send ||
520 			    !rdi->driver_f.do_send ||
521 			    !rdi->post_parms)
522 				return -EINVAL;
523 		break;
524 
525 	}
526 
527 	return 0;
528 }
529 
530 /**
531  * rvt_register_device - register a driver
532  * @rdi: main dev structure for all of rdmavt operations
533  *
534  * It is up to drivers to allocate the rdi and fill in the appropriate
535  * information.
536  *
537  * Return: 0 on success otherwise an errno.
538  */
539 int rvt_register_device(struct rvt_dev_info *rdi, u32 driver_id)
540 {
541 	int ret = 0, i;
542 
543 	if (!rdi)
544 		return -EINVAL;
545 
546 	/*
547 	 * Check to ensure drivers have setup the required helpers for the verbs
548 	 * they want rdmavt to handle
549 	 */
550 	for (i = 0; i < _VERB_IDX_MAX; i++)
551 		if (check_support(rdi, i)) {
552 			pr_err("Driver support req not met at %d\n", i);
553 			return -EINVAL;
554 		}
555 
556 	ib_set_device_ops(&rdi->ibdev, &rvt_dev_ops);
557 
558 	/* Once we get past here we can use rvt_pr macros and tracepoints */
559 	trace_rvt_dbg(rdi, "Driver attempting registration");
560 	rvt_mmap_init(rdi);
561 
562 	/* Queue Pairs */
563 	ret = rvt_driver_qp_init(rdi);
564 	if (ret) {
565 		pr_err("Error in driver QP init.\n");
566 		return -EINVAL;
567 	}
568 
569 	/* Address Handle */
570 	spin_lock_init(&rdi->n_ahs_lock);
571 	rdi->n_ahs_allocated = 0;
572 
573 	/* Shared Receive Queue */
574 	rvt_driver_srq_init(rdi);
575 
576 	/* Multicast */
577 	rvt_driver_mcast_init(rdi);
578 
579 	/* Mem Region */
580 	ret = rvt_driver_mr_init(rdi);
581 	if (ret) {
582 		pr_err("Error in driver MR init.\n");
583 		goto bail_no_mr;
584 	}
585 
586 	/* Memory Working Set Size */
587 	ret = rvt_wss_init(rdi);
588 	if (ret) {
589 		rvt_pr_err(rdi, "Error in WSS init.\n");
590 		goto bail_mr;
591 	}
592 
593 	/* Completion queues */
594 	spin_lock_init(&rdi->n_cqs_lock);
595 
596 	/* DMA Operations */
597 	rdi->ibdev.dev.dma_ops = rdi->ibdev.dev.dma_ops ? : &dma_virt_ops;
598 
599 	/* Protection Domain */
600 	spin_lock_init(&rdi->n_pds_lock);
601 	rdi->n_pds_allocated = 0;
602 
603 	/*
604 	 * There are some things which could be set by underlying drivers but
605 	 * really should be up to rdmavt to set. For instance drivers can't know
606 	 * exactly which functions rdmavt supports, nor do they know the ABI
607 	 * version, so we do all of this sort of stuff here.
608 	 */
609 	rdi->ibdev.uverbs_abi_ver = RVT_UVERBS_ABI_VERSION;
610 	rdi->ibdev.uverbs_cmd_mask =
611 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)         |
612 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)        |
613 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)          |
614 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)            |
615 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)          |
616 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)           |
617 		(1ull << IB_USER_VERBS_CMD_MODIFY_AH)           |
618 		(1ull << IB_USER_VERBS_CMD_QUERY_AH)            |
619 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)          |
620 		(1ull << IB_USER_VERBS_CMD_REG_MR)              |
621 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)            |
622 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
623 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)           |
624 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)           |
625 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)          |
626 		(1ull << IB_USER_VERBS_CMD_POLL_CQ)             |
627 		(1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ)       |
628 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)           |
629 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)            |
630 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)           |
631 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)          |
632 		(1ull << IB_USER_VERBS_CMD_POST_SEND)           |
633 		(1ull << IB_USER_VERBS_CMD_POST_RECV)           |
634 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)        |
635 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)        |
636 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)          |
637 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)          |
638 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)           |
639 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)         |
640 		(1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
641 	rdi->ibdev.node_type = RDMA_NODE_IB_CA;
642 	if (!rdi->ibdev.num_comp_vectors)
643 		rdi->ibdev.num_comp_vectors = 1;
644 
645 	rdi->ibdev.driver_id = driver_id;
646 	/* We are now good to announce we exist */
647 	ret = ib_register_device(&rdi->ibdev, dev_name(&rdi->ibdev.dev),
648 				 rdi->driver_f.port_callback);
649 	if (ret) {
650 		rvt_pr_err(rdi, "Failed to register driver with ib core.\n");
651 		goto bail_wss;
652 	}
653 
654 	rvt_create_mad_agents(rdi);
655 
656 	rvt_pr_info(rdi, "Registration with rdmavt done.\n");
657 	return ret;
658 
659 bail_wss:
660 	rvt_wss_exit(rdi);
661 bail_mr:
662 	rvt_mr_exit(rdi);
663 
664 bail_no_mr:
665 	rvt_qp_exit(rdi);
666 
667 	return ret;
668 }
669 EXPORT_SYMBOL(rvt_register_device);
670 
671 /**
672  * rvt_unregister_device - remove a driver
673  * @rdi: rvt dev struct
674  */
675 void rvt_unregister_device(struct rvt_dev_info *rdi)
676 {
677 	trace_rvt_dbg(rdi, "Driver is unregistering.");
678 	if (!rdi)
679 		return;
680 
681 	rvt_free_mad_agents(rdi);
682 
683 	ib_unregister_device(&rdi->ibdev);
684 	rvt_wss_exit(rdi);
685 	rvt_mr_exit(rdi);
686 	rvt_qp_exit(rdi);
687 }
688 EXPORT_SYMBOL(rvt_unregister_device);
689 
690 /**
691  * rvt_init_port - init internal data for driver port
692  * @rdi: rvt dev strut
693  * @port: rvt port
694  * @port_index: 0 based index of ports, different from IB core port num
695  *
696  * Keep track of a list of ports. No need to have a detach port.
697  * They persist until the driver goes away.
698  *
699  * Return: always 0
700  */
701 int rvt_init_port(struct rvt_dev_info *rdi, struct rvt_ibport *port,
702 		  int port_index, u16 *pkey_table)
703 {
704 
705 	rdi->ports[port_index] = port;
706 	rdi->ports[port_index]->pkey_table = pkey_table;
707 
708 	return 0;
709 }
710 EXPORT_SYMBOL(rvt_init_port);
711