xref: /openbmc/linux/drivers/infiniband/sw/rdmavt/vt.c (revision 4e1a33b1)
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 /**
110  * rvt_dealloc_device - deallocate rdi
111  * @rdi: structure to free
112  *
113  * Free a structure allocated with rvt_alloc_device()
114  */
115 void rvt_dealloc_device(struct rvt_dev_info *rdi)
116 {
117 	kfree(rdi->ports);
118 	ib_dealloc_device(&rdi->ibdev);
119 }
120 EXPORT_SYMBOL(rvt_dealloc_device);
121 
122 static int rvt_query_device(struct ib_device *ibdev,
123 			    struct ib_device_attr *props,
124 			    struct ib_udata *uhw)
125 {
126 	struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
127 
128 	if (uhw->inlen || uhw->outlen)
129 		return -EINVAL;
130 	/*
131 	 * Return rvt_dev_info.dparms.props contents
132 	 */
133 	*props = rdi->dparms.props;
134 	return 0;
135 }
136 
137 static int rvt_modify_device(struct ib_device *device,
138 			     int device_modify_mask,
139 			     struct ib_device_modify *device_modify)
140 {
141 	/*
142 	 * There is currently no need to supply this based on qib and hfi1.
143 	 * Future drivers may need to implement this though.
144 	 */
145 
146 	return -EOPNOTSUPP;
147 }
148 
149 /**
150  * rvt_query_port: Passes the query port call to the driver
151  * @ibdev: Verbs IB dev
152  * @port_num: port number, 1 based from ib core
153  * @props: structure to hold returned properties
154  *
155  * Return: 0 on success
156  */
157 static int rvt_query_port(struct ib_device *ibdev, u8 port_num,
158 			  struct ib_port_attr *props)
159 {
160 	struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
161 	struct rvt_ibport *rvp;
162 	int port_index = ibport_num_to_idx(ibdev, port_num);
163 
164 	if (port_index < 0)
165 		return -EINVAL;
166 
167 	rvp = rdi->ports[port_index];
168 	/* props being zeroed by the caller, avoid zeroing it here */
169 	props->sm_lid = rvp->sm_lid;
170 	props->sm_sl = rvp->sm_sl;
171 	props->port_cap_flags = rvp->port_cap_flags;
172 	props->max_msg_sz = 0x80000000;
173 	props->pkey_tbl_len = rvt_get_npkeys(rdi);
174 	props->bad_pkey_cntr = rvp->pkey_violations;
175 	props->qkey_viol_cntr = rvp->qkey_violations;
176 	props->subnet_timeout = rvp->subnet_timeout;
177 	props->init_type_reply = 0;
178 
179 	/* Populate the remaining ib_port_attr elements */
180 	return rdi->driver_f.query_port_state(rdi, port_num, props);
181 }
182 
183 /**
184  * rvt_modify_port
185  * @ibdev: Verbs IB dev
186  * @port_num: Port number, 1 based from ib core
187  * @port_modify_mask: How to change the port
188  * @props: Structure to fill in
189  *
190  * Return: 0 on success
191  */
192 static int rvt_modify_port(struct ib_device *ibdev, u8 port_num,
193 			   int port_modify_mask, struct ib_port_modify *props)
194 {
195 	struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
196 	struct rvt_ibport *rvp;
197 	int ret = 0;
198 	int port_index = ibport_num_to_idx(ibdev, port_num);
199 
200 	if (port_index < 0)
201 		return -EINVAL;
202 
203 	rvp = rdi->ports[port_index];
204 	rvp->port_cap_flags |= props->set_port_cap_mask;
205 	rvp->port_cap_flags &= ~props->clr_port_cap_mask;
206 
207 	if (props->set_port_cap_mask || props->clr_port_cap_mask)
208 		rdi->driver_f.cap_mask_chg(rdi, port_num);
209 	if (port_modify_mask & IB_PORT_SHUTDOWN)
210 		ret = rdi->driver_f.shut_down_port(rdi, port_num);
211 	if (port_modify_mask & IB_PORT_RESET_QKEY_CNTR)
212 		rvp->qkey_violations = 0;
213 
214 	return ret;
215 }
216 
217 /**
218  * rvt_query_pkey - Return a pkey from the table at a given index
219  * @ibdev: Verbs IB dev
220  * @port_num: Port number, 1 based from ib core
221  * @intex: Index into pkey table
222  *
223  * Return: 0 on failure pkey otherwise
224  */
225 static int rvt_query_pkey(struct ib_device *ibdev, u8 port_num, u16 index,
226 			  u16 *pkey)
227 {
228 	/*
229 	 * Driver will be responsible for keeping rvt_dev_info.pkey_table up to
230 	 * date. This function will just return that value. There is no need to
231 	 * lock, if a stale value is read and sent to the user so be it there is
232 	 * no way to protect against that anyway.
233 	 */
234 	struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
235 	int port_index;
236 
237 	port_index = ibport_num_to_idx(ibdev, port_num);
238 	if (port_index < 0)
239 		return -EINVAL;
240 
241 	if (index >= rvt_get_npkeys(rdi))
242 		return -EINVAL;
243 
244 	*pkey = rvt_get_pkey(rdi, port_index, index);
245 	return 0;
246 }
247 
248 /**
249  * rvt_query_gid - Return a gid from the table
250  * @ibdev: Verbs IB dev
251  * @port_num: Port number, 1 based from ib core
252  * @index: = Index in table
253  * @gid: Gid to return
254  *
255  * Return: 0 on success
256  */
257 static int rvt_query_gid(struct ib_device *ibdev, u8 port_num,
258 			 int guid_index, union ib_gid *gid)
259 {
260 	struct rvt_dev_info *rdi;
261 	struct rvt_ibport *rvp;
262 	int port_index;
263 
264 	/*
265 	 * Driver is responsible for updating the guid table. Which will be used
266 	 * to craft the return value. This will work similar to how query_pkey()
267 	 * is being done.
268 	 */
269 	port_index = ibport_num_to_idx(ibdev, port_num);
270 	if (port_index < 0)
271 		return -EINVAL;
272 
273 	rdi = ib_to_rvt(ibdev);
274 	rvp = rdi->ports[port_index];
275 
276 	gid->global.subnet_prefix = rvp->gid_prefix;
277 
278 	return rdi->driver_f.get_guid_be(rdi, rvp, guid_index,
279 					 &gid->global.interface_id);
280 }
281 
282 struct rvt_ucontext {
283 	struct ib_ucontext ibucontext;
284 };
285 
286 static inline struct rvt_ucontext *to_iucontext(struct ib_ucontext
287 						*ibucontext)
288 {
289 	return container_of(ibucontext, struct rvt_ucontext, ibucontext);
290 }
291 
292 /**
293  * rvt_alloc_ucontext - Allocate a user context
294  * @ibdev: Vers IB dev
295  * @data: User data allocated
296  */
297 static struct ib_ucontext *rvt_alloc_ucontext(struct ib_device *ibdev,
298 					      struct ib_udata *udata)
299 {
300 	struct rvt_ucontext *context;
301 
302 	context = kmalloc(sizeof(*context), GFP_KERNEL);
303 	if (!context)
304 		return ERR_PTR(-ENOMEM);
305 	return &context->ibucontext;
306 }
307 
308 /**
309  *rvt_dealloc_ucontext - Free a user context
310  *@context - Free this
311  */
312 static int rvt_dealloc_ucontext(struct ib_ucontext *context)
313 {
314 	kfree(to_iucontext(context));
315 	return 0;
316 }
317 
318 static int rvt_get_port_immutable(struct ib_device *ibdev, u8 port_num,
319 				  struct ib_port_immutable *immutable)
320 {
321 	struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
322 	struct ib_port_attr attr;
323 	int err, port_index;
324 
325 	port_index = ibport_num_to_idx(ibdev, port_num);
326 	if (port_index < 0)
327 		return -EINVAL;
328 
329 	immutable->core_cap_flags = rdi->dparms.core_cap_flags;
330 
331 	err = ib_query_port(ibdev, port_num, &attr);
332 	if (err)
333 		return err;
334 
335 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
336 	immutable->gid_tbl_len = attr.gid_tbl_len;
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.dma_ops =
782 		rdi->ibdev.dma_ops ? : &rvt_default_dma_mapping_ops;
783 
784 	/* Protection Domain */
785 	spin_lock_init(&rdi->n_pds_lock);
786 	rdi->n_pds_allocated = 0;
787 
788 	/*
789 	 * There are some things which could be set by underlying drivers but
790 	 * really should be up to rdmavt to set. For instance drivers can't know
791 	 * exactly which functions rdmavt supports, nor do they know the ABI
792 	 * version, so we do all of this sort of stuff here.
793 	 */
794 	rdi->ibdev.uverbs_abi_ver = RVT_UVERBS_ABI_VERSION;
795 	rdi->ibdev.uverbs_cmd_mask =
796 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)         |
797 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)        |
798 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)          |
799 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)            |
800 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)          |
801 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)           |
802 		(1ull << IB_USER_VERBS_CMD_MODIFY_AH)           |
803 		(1ull << IB_USER_VERBS_CMD_QUERY_AH)            |
804 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)          |
805 		(1ull << IB_USER_VERBS_CMD_REG_MR)              |
806 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)            |
807 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
808 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)           |
809 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)           |
810 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)          |
811 		(1ull << IB_USER_VERBS_CMD_POLL_CQ)             |
812 		(1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ)       |
813 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)           |
814 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)            |
815 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)           |
816 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)          |
817 		(1ull << IB_USER_VERBS_CMD_POST_SEND)           |
818 		(1ull << IB_USER_VERBS_CMD_POST_RECV)           |
819 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)        |
820 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)        |
821 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)          |
822 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)          |
823 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)           |
824 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)         |
825 		(1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
826 	rdi->ibdev.node_type = RDMA_NODE_IB_CA;
827 	rdi->ibdev.num_comp_vectors = 1;
828 
829 	/* We are now good to announce we exist */
830 	ret =  ib_register_device(&rdi->ibdev, rdi->driver_f.port_callback);
831 	if (ret) {
832 		rvt_pr_err(rdi, "Failed to register driver with ib core.\n");
833 		goto bail_cq;
834 	}
835 
836 	rvt_create_mad_agents(rdi);
837 
838 	rvt_pr_info(rdi, "Registration with rdmavt done.\n");
839 	return ret;
840 
841 bail_cq:
842 	rvt_cq_exit(rdi);
843 
844 bail_mr:
845 	rvt_mr_exit(rdi);
846 
847 bail_no_mr:
848 	rvt_qp_exit(rdi);
849 
850 	return ret;
851 }
852 EXPORT_SYMBOL(rvt_register_device);
853 
854 /**
855  * rvt_unregister_device - remove a driver
856  * @rdi: rvt dev struct
857  */
858 void rvt_unregister_device(struct rvt_dev_info *rdi)
859 {
860 	trace_rvt_dbg(rdi, "Driver is unregistering.");
861 	if (!rdi)
862 		return;
863 
864 	rvt_free_mad_agents(rdi);
865 
866 	ib_unregister_device(&rdi->ibdev);
867 	rvt_cq_exit(rdi);
868 	rvt_mr_exit(rdi);
869 	rvt_qp_exit(rdi);
870 }
871 EXPORT_SYMBOL(rvt_unregister_device);
872 
873 /**
874  * rvt_init_port - init internal data for driver port
875  * @rdi: rvt dev strut
876  * @port: rvt port
877  * @port_index: 0 based index of ports, different from IB core port num
878  *
879  * Keep track of a list of ports. No need to have a detach port.
880  * They persist until the driver goes away.
881  *
882  * Return: always 0
883  */
884 int rvt_init_port(struct rvt_dev_info *rdi, struct rvt_ibport *port,
885 		  int port_index, u16 *pkey_table)
886 {
887 
888 	rdi->ports[port_index] = port;
889 	rdi->ports[port_index]->pkey_table = pkey_table;
890 
891 	return 0;
892 }
893 EXPORT_SYMBOL(rvt_init_port);
894