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>
53 #define RVT_UVERBS_ABI_VERSION 2
55 MODULE_LICENSE("Dual BSD/GPL");
56 MODULE_DESCRIPTION("RDMA Verbs Transport Library");
58 static int rvt_init(void)
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.
66 module_init(rvt_init
);
68 static void rvt_cleanup(void)
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.
76 module_exit(rvt_cleanup
);
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
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
87 * We also allocate a port array based on the number of ports.
89 * Return: pointer to allocated rdi
91 struct rvt_dev_info
*rvt_alloc_device(size_t size
, int nports
)
93 struct rvt_dev_info
*rdi
= ERR_PTR(-ENOMEM
);
95 rdi
= (struct rvt_dev_info
*)ib_alloc_device(size
);
99 rdi
->ports
= kcalloc(nports
,
100 sizeof(struct rvt_ibport
**),
103 ib_dealloc_device(&rdi
->ibdev
);
107 EXPORT_SYMBOL(rvt_alloc_device
);
110 * rvt_dealloc_device - deallocate rdi
111 * @rdi: structure to free
113 * Free a structure allocated with rvt_alloc_device()
115 void rvt_dealloc_device(struct rvt_dev_info
*rdi
)
118 ib_dealloc_device(&rdi
->ibdev
);
120 EXPORT_SYMBOL(rvt_dealloc_device
);
122 static int rvt_query_device(struct ib_device
*ibdev
,
123 struct ib_device_attr
*props
,
124 struct ib_udata
*uhw
)
126 struct rvt_dev_info
*rdi
= ib_to_rvt(ibdev
);
128 if (uhw
->inlen
|| uhw
->outlen
)
131 * Return rvt_dev_info.dparms.props contents
133 *props
= rdi
->dparms
.props
;
137 static int rvt_modify_device(struct ib_device
*device
,
138 int device_modify_mask
,
139 struct ib_device_modify
*device_modify
)
142 * There is currently no need to supply this based on qib and hfi1.
143 * Future drivers may need to implement this though.
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
155 * Return: 0 on success
157 static int rvt_query_port(struct ib_device
*ibdev
, u8 port_num
,
158 struct ib_port_attr
*props
)
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
);
167 rvp
= rdi
->ports
[port_index
];
168 memset(props
, 0, sizeof(*props
));
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;
179 /* Populate the remaining ib_port_attr elements */
180 return rdi
->driver_f
.query_port_state(rdi
, port_num
, props
);
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
190 * Return: 0 on success
192 static int rvt_modify_port(struct ib_device
*ibdev
, u8 port_num
,
193 int port_modify_mask
, struct ib_port_modify
*props
)
195 struct rvt_dev_info
*rdi
= ib_to_rvt(ibdev
);
196 struct rvt_ibport
*rvp
;
198 int port_index
= ibport_num_to_idx(ibdev
, port_num
);
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
;
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;
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
223 * Return: 0 on failure pkey otherwise
225 static int rvt_query_pkey(struct ib_device
*ibdev
, u8 port_num
, u16 index
,
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.
234 struct rvt_dev_info
*rdi
= ib_to_rvt(ibdev
);
237 port_index
= ibport_num_to_idx(ibdev
, port_num
);
241 if (index
>= rvt_get_npkeys(rdi
))
244 *pkey
= rvt_get_pkey(rdi
, port_index
, index
);
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
255 * Return: 0 on success
257 static int rvt_query_gid(struct ib_device
*ibdev
, u8 port_num
,
258 int guid_index
, union ib_gid
*gid
)
260 struct rvt_dev_info
*rdi
;
261 struct rvt_ibport
*rvp
;
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()
269 port_index
= ibport_num_to_idx(ibdev
, port_num
);
273 rdi
= ib_to_rvt(ibdev
);
274 rvp
= rdi
->ports
[port_index
];
276 gid
->global
.subnet_prefix
= rvp
->gid_prefix
;
278 return rdi
->driver_f
.get_guid_be(rdi
, rvp
, guid_index
,
279 &gid
->global
.interface_id
);
282 struct rvt_ucontext
{
283 struct ib_ucontext ibucontext
;
286 static inline struct rvt_ucontext
*to_iucontext(struct ib_ucontext
289 return container_of(ibucontext
, struct rvt_ucontext
, ibucontext
);
293 * rvt_alloc_ucontext - Allocate a user context
294 * @ibdev: Vers IB dev
295 * @data: User data allocated
297 static struct ib_ucontext
*rvt_alloc_ucontext(struct ib_device
*ibdev
,
298 struct ib_udata
*udata
)
300 struct rvt_ucontext
*context
;
302 context
= kmalloc(sizeof(*context
), GFP_KERNEL
);
304 return ERR_PTR(-ENOMEM
);
305 return &context
->ibucontext
;
309 *rvt_dealloc_ucontext - Free a user context
310 *@context - Free this
312 static int rvt_dealloc_ucontext(struct ib_ucontext
*context
)
314 kfree(to_iucontext(context
));
318 static int rvt_get_port_immutable(struct ib_device
*ibdev
, u8 port_num
,
319 struct ib_port_immutable
*immutable
)
321 struct rvt_dev_info
*rdi
= ib_to_rvt(ibdev
);
322 struct ib_port_attr attr
;
325 port_index
= ibport_num_to_idx(ibdev
, port_num
);
329 err
= rvt_query_port(ibdev
, port_num
, &attr
);
333 immutable
->pkey_tbl_len
= attr
.pkey_tbl_len
;
334 immutable
->gid_tbl_len
= attr
.gid_tbl_len
;
335 immutable
->core_cap_flags
= rdi
->dparms
.core_cap_flags
;
336 immutable
->max_mad_size
= rdi
->dparms
.max_mad_size
;
386 _VERB_IDX_MAX
/* Must always be last! */
389 static inline int check_driver_override(struct rvt_dev_info
*rdi
,
390 size_t offset
, void *func
)
392 if (!*(void **)((void *)&rdi
->ibdev
+ offset
)) {
393 *(void **)((void *)&rdi
->ibdev
+ offset
) = func
;
400 static noinline
int check_support(struct rvt_dev_info
*rdi
, int verb
)
405 * These functions are not part of verbs specifically but are
406 * required for rdmavt to function.
408 if ((!rdi
->driver_f
.port_callback
) ||
409 (!rdi
->driver_f
.get_card_name
) ||
410 (!rdi
->driver_f
.get_pci_dev
))
415 check_driver_override(rdi
, offsetof(struct ib_device
,
422 * rdmavt does not support modify device currently drivers must
425 if (!check_driver_override(rdi
, offsetof(struct ib_device
,
432 if (!check_driver_override(rdi
, offsetof(struct ib_device
,
435 if (!rdi
->driver_f
.query_port_state
)
440 if (!check_driver_override(rdi
, offsetof(struct ib_device
,
443 if (!rdi
->driver_f
.cap_mask_chg
||
444 !rdi
->driver_f
.shut_down_port
)
449 check_driver_override(rdi
, offsetof(struct ib_device
,
455 if (!check_driver_override(rdi
, offsetof(struct ib_device
,
458 if (!rdi
->driver_f
.get_guid_be
)
463 check_driver_override(rdi
, offsetof(struct ib_device
,
468 case DEALLOC_UCONTEXT
:
469 check_driver_override(rdi
, offsetof(struct ib_device
,
471 rvt_dealloc_ucontext
);
474 case GET_PORT_IMMUTABLE
:
475 check_driver_override(rdi
, offsetof(struct ib_device
,
477 rvt_get_port_immutable
);
481 if (!check_driver_override(rdi
, offsetof(struct ib_device
,
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
)
494 if (!check_driver_override(rdi
, offsetof(struct ib_device
,
497 if (!rdi
->driver_f
.notify_qp_reset
||
498 !rdi
->driver_f
.schedule_send
||
499 !rdi
->driver_f
.get_pmtu_from_attr
||
500 !rdi
->driver_f
.flush_qp_waiters
||
501 !rdi
->driver_f
.stop_send_queue
||
502 !rdi
->driver_f
.quiesce_qp
||
503 !rdi
->driver_f
.notify_error_qp
||
504 !rdi
->driver_f
.mtu_from_qp
||
505 !rdi
->driver_f
.mtu_to_path_mtu
)
510 if (!check_driver_override(rdi
, offsetof(struct ib_device
,
513 if (!rdi
->driver_f
.qp_priv_free
||
514 !rdi
->driver_f
.notify_qp_reset
||
515 !rdi
->driver_f
.flush_qp_waiters
||
516 !rdi
->driver_f
.stop_send_queue
||
517 !rdi
->driver_f
.quiesce_qp
)
522 check_driver_override(rdi
, offsetof(struct ib_device
,
528 if (!check_driver_override(rdi
, offsetof(struct ib_device
,
531 if (!rdi
->driver_f
.schedule_send
||
532 !rdi
->driver_f
.do_send
||
538 check_driver_override(rdi
, offsetof(struct ib_device
,
543 check_driver_override(rdi
, offsetof(struct ib_device
,
549 check_driver_override(rdi
, offsetof(struct ib_device
,
555 check_driver_override(rdi
, offsetof(struct ib_device
,
561 check_driver_override(rdi
, offsetof(struct ib_device
,
567 check_driver_override(rdi
, offsetof(struct ib_device
,
573 check_driver_override(rdi
, offsetof(struct ib_device
,
579 check_driver_override(rdi
, offsetof(struct ib_device
,
585 check_driver_override(rdi
, offsetof(struct ib_device
,
591 check_driver_override(rdi
, offsetof(struct ib_device
,
597 check_driver_override(rdi
, offsetof(struct ib_device
,
603 check_driver_override(rdi
, offsetof(struct ib_device
,
609 check_driver_override(rdi
, offsetof(struct ib_device
,
615 check_driver_override(rdi
, offsetof(struct ib_device
,
621 check_driver_override(rdi
, offsetof(struct ib_device
,
627 check_driver_override(rdi
, offsetof(struct ib_device
,
633 check_driver_override(rdi
, offsetof(struct ib_device
,
639 check_driver_override(rdi
, offsetof(struct ib_device
,
645 check_driver_override(rdi
, offsetof(struct ib_device
,
651 check_driver_override(rdi
, offsetof(struct ib_device
,
657 check_driver_override(rdi
, offsetof(struct ib_device
,
663 check_driver_override(rdi
, offsetof(struct ib_device
,
669 check_driver_override(rdi
, offsetof(struct ib_device
,
675 check_driver_override(rdi
, offsetof(struct ib_device
,
681 check_driver_override(rdi
, offsetof(struct ib_device
,
687 check_driver_override(rdi
, offsetof(struct ib_device
,
693 check_driver_override(rdi
, offsetof(struct ib_device
,
699 check_driver_override(rdi
, offsetof(struct ib_device
,
705 check_driver_override(rdi
, offsetof(struct ib_device
,
718 * rvt_register_device - register a driver
719 * @rdi: main dev structure for all of rdmavt operations
721 * It is up to drivers to allocate the rdi and fill in the appropriate
724 * Return: 0 on success otherwise an errno.
726 int rvt_register_device(struct rvt_dev_info
*rdi
)
734 * Check to ensure drivers have setup the required helpers for the verbs
735 * they want rdmavt to handle
737 for (i
= 0; i
< _VERB_IDX_MAX
; i
++)
738 if (check_support(rdi
, i
)) {
739 pr_err("Driver support req not met at %d\n", i
);
744 /* Once we get past here we can use rvt_pr macros and tracepoints */
745 trace_rvt_dbg(rdi
, "Driver attempting registration");
749 ret
= rvt_driver_qp_init(rdi
);
751 pr_err("Error in driver QP init.\n");
756 spin_lock_init(&rdi
->n_ahs_lock
);
757 rdi
->n_ahs_allocated
= 0;
759 /* Shared Receive Queue */
760 rvt_driver_srq_init(rdi
);
763 rvt_driver_mcast_init(rdi
);
766 ret
= rvt_driver_mr_init(rdi
);
768 pr_err("Error in driver MR init.\n");
772 /* Completion queues */
773 ret
= rvt_driver_cq_init(rdi
);
775 pr_err("Error in driver CQ init.\n");
781 rdi
->ibdev
.dma_ops
? : &rvt_default_dma_mapping_ops
;
783 /* Protection Domain */
784 spin_lock_init(&rdi
->n_pds_lock
);
785 rdi
->n_pds_allocated
= 0;
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.
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;
828 /* We are now good to announce we exist */
829 ret
= ib_register_device(&rdi
->ibdev
, rdi
->driver_f
.port_callback
);
831 rvt_pr_err(rdi
, "Failed to register driver with ib core.\n");
835 rvt_create_mad_agents(rdi
);
837 rvt_pr_info(rdi
, "Registration with rdmavt done.\n");
851 EXPORT_SYMBOL(rvt_register_device
);
854 * rvt_unregister_device - remove a driver
855 * @rdi: rvt dev struct
857 void rvt_unregister_device(struct rvt_dev_info
*rdi
)
859 trace_rvt_dbg(rdi
, "Driver is unregistering.");
863 rvt_free_mad_agents(rdi
);
865 ib_unregister_device(&rdi
->ibdev
);
870 EXPORT_SYMBOL(rvt_unregister_device
);
873 * rvt_init_port - init internal data for driver port
874 * @rdi: rvt dev strut
876 * @port_index: 0 based index of ports, different from IB core port num
878 * Keep track of a list of ports. No need to have a detach port.
879 * They persist until the driver goes away.
883 int rvt_init_port(struct rvt_dev_info
*rdi
, struct rvt_ibport
*port
,
884 int port_index
, u16
*pkey_table
)
887 rdi
->ports
[port_index
] = port
;
888 rdi
->ports
[port_index
]->pkey_table
= pkey_table
;
892 EXPORT_SYMBOL(rvt_init_port
);