2 * Copyright(c) 2016 Intel Corporation.
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.
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.
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.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
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
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.
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.
48 #include <linux/module.h>
49 #include <linux/kernel.h>
50 #include <linux/dma-mapping.h>
54 #define RVT_UVERBS_ABI_VERSION 2
56 MODULE_LICENSE("Dual BSD/GPL");
57 MODULE_DESCRIPTION("RDMA Verbs Transport Library");
59 static int rvt_init(void)
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.
67 module_init(rvt_init
);
69 static void rvt_cleanup(void)
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.
77 module_exit(rvt_cleanup
);
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
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
88 * We also allocate a port array based on the number of ports.
90 * Return: pointer to allocated rdi
92 struct rvt_dev_info
*rvt_alloc_device(size_t size
, int nports
)
94 struct rvt_dev_info
*rdi
= ERR_PTR(-ENOMEM
);
96 rdi
= (struct rvt_dev_info
*)ib_alloc_device(size
);
100 rdi
->ports
= kcalloc(nports
,
101 sizeof(struct rvt_ibport
**),
104 ib_dealloc_device(&rdi
->ibdev
);
108 EXPORT_SYMBOL(rvt_alloc_device
);
111 * rvt_dealloc_device - deallocate rdi
112 * @rdi: structure to free
114 * Free a structure allocated with rvt_alloc_device()
116 void rvt_dealloc_device(struct rvt_dev_info
*rdi
)
119 ib_dealloc_device(&rdi
->ibdev
);
121 EXPORT_SYMBOL(rvt_dealloc_device
);
123 static int rvt_query_device(struct ib_device
*ibdev
,
124 struct ib_device_attr
*props
,
125 struct ib_udata
*uhw
)
127 struct rvt_dev_info
*rdi
= ib_to_rvt(ibdev
);
129 if (uhw
->inlen
|| uhw
->outlen
)
132 * Return rvt_dev_info.dparms.props contents
134 *props
= rdi
->dparms
.props
;
138 static int rvt_modify_device(struct ib_device
*device
,
139 int device_modify_mask
,
140 struct ib_device_modify
*device_modify
)
143 * There is currently no need to supply this based on qib and hfi1.
144 * Future drivers may need to implement this though.
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
156 * Return: 0 on success
158 static int rvt_query_port(struct ib_device
*ibdev
, u8 port_num
,
159 struct ib_port_attr
*props
)
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
);
168 rvp
= rdi
->ports
[port_index
];
169 /* props being zeroed by the caller, avoid zeroing it here */
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;
180 /* Populate the remaining ib_port_attr elements */
181 return rdi
->driver_f
.query_port_state(rdi
, port_num
, props
);
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
191 * Return: 0 on success
193 static int rvt_modify_port(struct ib_device
*ibdev
, u8 port_num
,
194 int port_modify_mask
, struct ib_port_modify
*props
)
196 struct rvt_dev_info
*rdi
= ib_to_rvt(ibdev
);
197 struct rvt_ibport
*rvp
;
199 int port_index
= ibport_num_to_idx(ibdev
, port_num
);
204 rvp
= rdi
->ports
[port_index
];
205 if (port_modify_mask
& IB_PORT_OPA_MASK_CHG
) {
206 rvp
->port_cap3_flags
|= props
->set_port_cap_mask
;
207 rvp
->port_cap3_flags
&= ~props
->clr_port_cap_mask
;
209 rvp
->port_cap_flags
|= props
->set_port_cap_mask
;
210 rvp
->port_cap_flags
&= ~props
->clr_port_cap_mask
;
213 if (props
->set_port_cap_mask
|| props
->clr_port_cap_mask
)
214 rdi
->driver_f
.cap_mask_chg(rdi
, port_num
);
215 if (port_modify_mask
& IB_PORT_SHUTDOWN
)
216 ret
= rdi
->driver_f
.shut_down_port(rdi
, port_num
);
217 if (port_modify_mask
& IB_PORT_RESET_QKEY_CNTR
)
218 rvp
->qkey_violations
= 0;
224 * rvt_query_pkey - Return a pkey from the table at a given index
225 * @ibdev: Verbs IB dev
226 * @port_num: Port number, 1 based from ib core
227 * @index: Index into pkey table
228 * @pkey: returned pkey from the port pkey table
230 * Return: 0 on failure pkey otherwise
232 static int rvt_query_pkey(struct ib_device
*ibdev
, u8 port_num
, u16 index
,
236 * Driver will be responsible for keeping rvt_dev_info.pkey_table up to
237 * date. This function will just return that value. There is no need to
238 * lock, if a stale value is read and sent to the user so be it there is
239 * no way to protect against that anyway.
241 struct rvt_dev_info
*rdi
= ib_to_rvt(ibdev
);
244 port_index
= ibport_num_to_idx(ibdev
, port_num
);
248 if (index
>= rvt_get_npkeys(rdi
))
251 *pkey
= rvt_get_pkey(rdi
, port_index
, index
);
256 * rvt_query_gid - Return a gid from the table
257 * @ibdev: Verbs IB dev
258 * @port_num: Port number, 1 based from ib core
259 * @guid_index: Index in table
260 * @gid: Gid to return
262 * Return: 0 on success
264 static int rvt_query_gid(struct ib_device
*ibdev
, u8 port_num
,
265 int guid_index
, union ib_gid
*gid
)
267 struct rvt_dev_info
*rdi
;
268 struct rvt_ibport
*rvp
;
272 * Driver is responsible for updating the guid table. Which will be used
273 * to craft the return value. This will work similar to how query_pkey()
276 port_index
= ibport_num_to_idx(ibdev
, port_num
);
280 rdi
= ib_to_rvt(ibdev
);
281 rvp
= rdi
->ports
[port_index
];
283 gid
->global
.subnet_prefix
= rvp
->gid_prefix
;
285 return rdi
->driver_f
.get_guid_be(rdi
, rvp
, guid_index
,
286 &gid
->global
.interface_id
);
289 struct rvt_ucontext
{
290 struct ib_ucontext ibucontext
;
293 static inline struct rvt_ucontext
*to_iucontext(struct ib_ucontext
296 return container_of(ibucontext
, struct rvt_ucontext
, ibucontext
);
300 * rvt_alloc_ucontext - Allocate a user context
301 * @ibdev: Verbs IB dev
302 * @udata: User data allocated
304 static struct ib_ucontext
*rvt_alloc_ucontext(struct ib_device
*ibdev
,
305 struct ib_udata
*udata
)
307 struct rvt_ucontext
*context
;
309 context
= kmalloc(sizeof(*context
), GFP_KERNEL
);
311 return ERR_PTR(-ENOMEM
);
312 return &context
->ibucontext
;
316 *rvt_dealloc_ucontext - Free a user context
317 *@context - Free this
319 static int rvt_dealloc_ucontext(struct ib_ucontext
*context
)
321 kfree(to_iucontext(context
));
325 static int rvt_get_port_immutable(struct ib_device
*ibdev
, u8 port_num
,
326 struct ib_port_immutable
*immutable
)
328 struct rvt_dev_info
*rdi
= ib_to_rvt(ibdev
);
329 struct ib_port_attr attr
;
332 port_index
= ibport_num_to_idx(ibdev
, port_num
);
336 immutable
->core_cap_flags
= rdi
->dparms
.core_cap_flags
;
338 err
= ib_query_port(ibdev
, port_num
, &attr
);
342 immutable
->pkey_tbl_len
= attr
.pkey_tbl_len
;
343 immutable
->gid_tbl_len
= attr
.gid_tbl_len
;
344 immutable
->max_mad_size
= rdi
->dparms
.max_mad_size
;
394 _VERB_IDX_MAX
/* Must always be last! */
397 static inline int check_driver_override(struct rvt_dev_info
*rdi
,
398 size_t offset
, void *func
)
400 if (!*(void **)((void *)&rdi
->ibdev
+ offset
)) {
401 *(void **)((void *)&rdi
->ibdev
+ offset
) = func
;
408 static noinline
int check_support(struct rvt_dev_info
*rdi
, int verb
)
413 * These functions are not part of verbs specifically but are
414 * required for rdmavt to function.
416 if ((!rdi
->driver_f
.port_callback
) ||
417 (!rdi
->driver_f
.get_pci_dev
))
422 check_driver_override(rdi
, offsetof(struct ib_device
,
429 * rdmavt does not support modify device currently drivers must
432 if (!check_driver_override(rdi
, offsetof(struct ib_device
,
439 if (!check_driver_override(rdi
, offsetof(struct ib_device
,
442 if (!rdi
->driver_f
.query_port_state
)
447 if (!check_driver_override(rdi
, offsetof(struct ib_device
,
450 if (!rdi
->driver_f
.cap_mask_chg
||
451 !rdi
->driver_f
.shut_down_port
)
456 check_driver_override(rdi
, offsetof(struct ib_device
,
462 if (!check_driver_override(rdi
, offsetof(struct ib_device
,
465 if (!rdi
->driver_f
.get_guid_be
)
470 check_driver_override(rdi
, offsetof(struct ib_device
,
475 case DEALLOC_UCONTEXT
:
476 check_driver_override(rdi
, offsetof(struct ib_device
,
478 rvt_dealloc_ucontext
);
481 case GET_PORT_IMMUTABLE
:
482 check_driver_override(rdi
, offsetof(struct ib_device
,
484 rvt_get_port_immutable
);
488 if (!check_driver_override(rdi
, offsetof(struct ib_device
,
491 if (!rdi
->driver_f
.qp_priv_alloc
||
492 !rdi
->driver_f
.qp_priv_free
||
493 !rdi
->driver_f
.notify_qp_reset
||
494 !rdi
->driver_f
.flush_qp_waiters
||
495 !rdi
->driver_f
.stop_send_queue
||
496 !rdi
->driver_f
.quiesce_qp
)
501 if (!check_driver_override(rdi
, offsetof(struct ib_device
,
504 if (!rdi
->driver_f
.notify_qp_reset
||
505 !rdi
->driver_f
.schedule_send
||
506 !rdi
->driver_f
.get_pmtu_from_attr
||
507 !rdi
->driver_f
.flush_qp_waiters
||
508 !rdi
->driver_f
.stop_send_queue
||
509 !rdi
->driver_f
.quiesce_qp
||
510 !rdi
->driver_f
.notify_error_qp
||
511 !rdi
->driver_f
.mtu_from_qp
||
512 !rdi
->driver_f
.mtu_to_path_mtu
)
517 if (!check_driver_override(rdi
, offsetof(struct ib_device
,
520 if (!rdi
->driver_f
.qp_priv_free
||
521 !rdi
->driver_f
.notify_qp_reset
||
522 !rdi
->driver_f
.flush_qp_waiters
||
523 !rdi
->driver_f
.stop_send_queue
||
524 !rdi
->driver_f
.quiesce_qp
)
529 check_driver_override(rdi
, offsetof(struct ib_device
,
535 if (!check_driver_override(rdi
, offsetof(struct ib_device
,
538 if (!rdi
->driver_f
.schedule_send
||
539 !rdi
->driver_f
.do_send
||
545 check_driver_override(rdi
, offsetof(struct ib_device
,
550 check_driver_override(rdi
, offsetof(struct ib_device
,
556 check_driver_override(rdi
, offsetof(struct ib_device
,
562 check_driver_override(rdi
, offsetof(struct ib_device
,
568 check_driver_override(rdi
, offsetof(struct ib_device
,
574 check_driver_override(rdi
, offsetof(struct ib_device
,
580 check_driver_override(rdi
, offsetof(struct ib_device
,
586 check_driver_override(rdi
, offsetof(struct ib_device
,
592 check_driver_override(rdi
, offsetof(struct ib_device
,
598 check_driver_override(rdi
, offsetof(struct ib_device
,
604 check_driver_override(rdi
, offsetof(struct ib_device
,
610 check_driver_override(rdi
, offsetof(struct ib_device
,
616 check_driver_override(rdi
, offsetof(struct ib_device
,
622 check_driver_override(rdi
, offsetof(struct ib_device
,
628 check_driver_override(rdi
, offsetof(struct ib_device
,
634 check_driver_override(rdi
, offsetof(struct ib_device
,
640 check_driver_override(rdi
, offsetof(struct ib_device
,
646 check_driver_override(rdi
, offsetof(struct ib_device
,
652 check_driver_override(rdi
, offsetof(struct ib_device
,
658 check_driver_override(rdi
, offsetof(struct ib_device
,
664 check_driver_override(rdi
, offsetof(struct ib_device
,
670 check_driver_override(rdi
, offsetof(struct ib_device
,
676 check_driver_override(rdi
, offsetof(struct ib_device
,
682 check_driver_override(rdi
, offsetof(struct ib_device
,
688 check_driver_override(rdi
, offsetof(struct ib_device
,
694 check_driver_override(rdi
, offsetof(struct ib_device
,
700 check_driver_override(rdi
, offsetof(struct ib_device
,
706 check_driver_override(rdi
, offsetof(struct ib_device
,
712 check_driver_override(rdi
, offsetof(struct ib_device
,
725 * rvt_register_device - register a driver
726 * @rdi: main dev structure for all of rdmavt operations
728 * It is up to drivers to allocate the rdi and fill in the appropriate
731 * Return: 0 on success otherwise an errno.
733 int rvt_register_device(struct rvt_dev_info
*rdi
)
741 * Check to ensure drivers have setup the required helpers for the verbs
742 * they want rdmavt to handle
744 for (i
= 0; i
< _VERB_IDX_MAX
; i
++)
745 if (check_support(rdi
, i
)) {
746 pr_err("Driver support req not met at %d\n", i
);
751 /* Once we get past here we can use rvt_pr macros and tracepoints */
752 trace_rvt_dbg(rdi
, "Driver attempting registration");
756 ret
= rvt_driver_qp_init(rdi
);
758 pr_err("Error in driver QP init.\n");
763 spin_lock_init(&rdi
->n_ahs_lock
);
764 rdi
->n_ahs_allocated
= 0;
766 /* Shared Receive Queue */
767 rvt_driver_srq_init(rdi
);
770 rvt_driver_mcast_init(rdi
);
773 ret
= rvt_driver_mr_init(rdi
);
775 pr_err("Error in driver MR init.\n");
779 /* Completion queues */
780 ret
= rvt_driver_cq_init(rdi
);
782 pr_err("Error in driver CQ init.\n");
787 rdi
->ibdev
.dev
.dma_ops
= rdi
->ibdev
.dev
.dma_ops
? : &dma_virt_ops
;
789 /* Protection Domain */
790 spin_lock_init(&rdi
->n_pds_lock
);
791 rdi
->n_pds_allocated
= 0;
794 * There are some things which could be set by underlying drivers but
795 * really should be up to rdmavt to set. For instance drivers can't know
796 * exactly which functions rdmavt supports, nor do they know the ABI
797 * version, so we do all of this sort of stuff here.
799 rdi
->ibdev
.uverbs_abi_ver
= RVT_UVERBS_ABI_VERSION
;
800 rdi
->ibdev
.uverbs_cmd_mask
=
801 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT
) |
802 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE
) |
803 (1ull << IB_USER_VERBS_CMD_QUERY_PORT
) |
804 (1ull << IB_USER_VERBS_CMD_ALLOC_PD
) |
805 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD
) |
806 (1ull << IB_USER_VERBS_CMD_CREATE_AH
) |
807 (1ull << IB_USER_VERBS_CMD_MODIFY_AH
) |
808 (1ull << IB_USER_VERBS_CMD_QUERY_AH
) |
809 (1ull << IB_USER_VERBS_CMD_DESTROY_AH
) |
810 (1ull << IB_USER_VERBS_CMD_REG_MR
) |
811 (1ull << IB_USER_VERBS_CMD_DEREG_MR
) |
812 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL
) |
813 (1ull << IB_USER_VERBS_CMD_CREATE_CQ
) |
814 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ
) |
815 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ
) |
816 (1ull << IB_USER_VERBS_CMD_POLL_CQ
) |
817 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ
) |
818 (1ull << IB_USER_VERBS_CMD_CREATE_QP
) |
819 (1ull << IB_USER_VERBS_CMD_QUERY_QP
) |
820 (1ull << IB_USER_VERBS_CMD_MODIFY_QP
) |
821 (1ull << IB_USER_VERBS_CMD_DESTROY_QP
) |
822 (1ull << IB_USER_VERBS_CMD_POST_SEND
) |
823 (1ull << IB_USER_VERBS_CMD_POST_RECV
) |
824 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST
) |
825 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST
) |
826 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ
) |
827 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ
) |
828 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ
) |
829 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ
) |
830 (1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV
);
831 rdi
->ibdev
.node_type
= RDMA_NODE_IB_CA
;
832 rdi
->ibdev
.num_comp_vectors
= 1;
834 /* We are now good to announce we exist */
835 ret
= ib_register_device(&rdi
->ibdev
, rdi
->driver_f
.port_callback
);
837 rvt_pr_err(rdi
, "Failed to register driver with ib core.\n");
841 rvt_create_mad_agents(rdi
);
843 rvt_pr_info(rdi
, "Registration with rdmavt done.\n");
857 EXPORT_SYMBOL(rvt_register_device
);
860 * rvt_unregister_device - remove a driver
861 * @rdi: rvt dev struct
863 void rvt_unregister_device(struct rvt_dev_info
*rdi
)
865 trace_rvt_dbg(rdi
, "Driver is unregistering.");
869 rvt_free_mad_agents(rdi
);
871 ib_unregister_device(&rdi
->ibdev
);
876 EXPORT_SYMBOL(rvt_unregister_device
);
879 * rvt_init_port - init internal data for driver port
880 * @rdi: rvt dev strut
882 * @port_index: 0 based index of ports, different from IB core port num
884 * Keep track of a list of ports. No need to have a detach port.
885 * They persist until the driver goes away.
889 int rvt_init_port(struct rvt_dev_info
*rdi
, struct rvt_ibport
*port
,
890 int port_index
, u16
*pkey_table
)
893 rdi
->ports
[port_index
] = port
;
894 rdi
->ports
[port_index
]->pkey_table
= pkey_table
;
898 EXPORT_SYMBOL(rvt_init_port
);