2 * Copyright(c) 2016 - 2018 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>
55 #define RVT_UVERBS_ABI_VERSION 2
57 MODULE_LICENSE("Dual BSD/GPL");
58 MODULE_DESCRIPTION("RDMA Verbs Transport Library");
60 static int rvt_init(void)
62 int ret = rvt_driver_cq_init();
65 pr_err("Error in driver CQ init.\n");
69 module_init(rvt_init);
71 static void rvt_cleanup(void)
75 module_exit(rvt_cleanup);
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
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
86 * We also allocate a port array based on the number of ports.
88 * Return: pointer to allocated rdi
90 struct rvt_dev_info *rvt_alloc_device(size_t size, int nports)
92 struct rvt_dev_info *rdi;
94 rdi = (struct rvt_dev_info *)ib_alloc_device(size);
98 rdi->ports = kcalloc(nports, sizeof(*rdi->ports), GFP_KERNEL);
100 ib_dealloc_device(&rdi->ibdev);
104 EXPORT_SYMBOL(rvt_alloc_device);
107 * rvt_dealloc_device - deallocate rdi
108 * @rdi: structure to free
110 * Free a structure allocated with rvt_alloc_device()
112 void rvt_dealloc_device(struct rvt_dev_info *rdi)
115 ib_dealloc_device(&rdi->ibdev);
117 EXPORT_SYMBOL(rvt_dealloc_device);
119 static int rvt_query_device(struct ib_device *ibdev,
120 struct ib_device_attr *props,
121 struct ib_udata *uhw)
123 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
125 if (uhw->inlen || uhw->outlen)
128 * Return rvt_dev_info.dparms.props contents
130 *props = rdi->dparms.props;
134 static int rvt_modify_device(struct ib_device *device,
135 int device_modify_mask,
136 struct ib_device_modify *device_modify)
139 * There is currently no need to supply this based on qib and hfi1.
140 * Future drivers may need to implement this though.
147 * rvt_query_port: Passes the query port call to the driver
148 * @ibdev: Verbs IB dev
149 * @port_num: port number, 1 based from ib core
150 * @props: structure to hold returned properties
152 * Return: 0 on success
154 static int rvt_query_port(struct ib_device *ibdev, u8 port_num,
155 struct ib_port_attr *props)
157 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
158 struct rvt_ibport *rvp;
159 int port_index = ibport_num_to_idx(ibdev, port_num);
164 rvp = rdi->ports[port_index];
165 /* props being zeroed by the caller, avoid zeroing it here */
166 props->sm_lid = rvp->sm_lid;
167 props->sm_sl = rvp->sm_sl;
168 props->port_cap_flags = rvp->port_cap_flags;
169 props->max_msg_sz = 0x80000000;
170 props->pkey_tbl_len = rvt_get_npkeys(rdi);
171 props->bad_pkey_cntr = rvp->pkey_violations;
172 props->qkey_viol_cntr = rvp->qkey_violations;
173 props->subnet_timeout = rvp->subnet_timeout;
174 props->init_type_reply = 0;
176 /* Populate the remaining ib_port_attr elements */
177 return rdi->driver_f.query_port_state(rdi, port_num, props);
182 * @ibdev: Verbs IB dev
183 * @port_num: Port number, 1 based from ib core
184 * @port_modify_mask: How to change the port
185 * @props: Structure to fill in
187 * Return: 0 on success
189 static int rvt_modify_port(struct ib_device *ibdev, u8 port_num,
190 int port_modify_mask, struct ib_port_modify *props)
192 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
193 struct rvt_ibport *rvp;
195 int port_index = ibport_num_to_idx(ibdev, port_num);
200 rvp = rdi->ports[port_index];
201 if (port_modify_mask & IB_PORT_OPA_MASK_CHG) {
202 rvp->port_cap3_flags |= props->set_port_cap_mask;
203 rvp->port_cap3_flags &= ~props->clr_port_cap_mask;
205 rvp->port_cap_flags |= props->set_port_cap_mask;
206 rvp->port_cap_flags &= ~props->clr_port_cap_mask;
209 if (props->set_port_cap_mask || props->clr_port_cap_mask)
210 rdi->driver_f.cap_mask_chg(rdi, port_num);
211 if (port_modify_mask & IB_PORT_SHUTDOWN)
212 ret = rdi->driver_f.shut_down_port(rdi, port_num);
213 if (port_modify_mask & IB_PORT_RESET_QKEY_CNTR)
214 rvp->qkey_violations = 0;
220 * rvt_query_pkey - Return a pkey from the table at a given index
221 * @ibdev: Verbs IB dev
222 * @port_num: Port number, 1 based from ib core
223 * @index: Index into pkey table
224 * @pkey: returned pkey from the port pkey table
226 * Return: 0 on failure pkey otherwise
228 static int rvt_query_pkey(struct ib_device *ibdev, u8 port_num, u16 index,
232 * Driver will be responsible for keeping rvt_dev_info.pkey_table up to
233 * date. This function will just return that value. There is no need to
234 * lock, if a stale value is read and sent to the user so be it there is
235 * no way to protect against that anyway.
237 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
240 port_index = ibport_num_to_idx(ibdev, port_num);
244 if (index >= rvt_get_npkeys(rdi))
247 *pkey = rvt_get_pkey(rdi, port_index, index);
252 * rvt_query_gid - Return a gid from the table
253 * @ibdev: Verbs IB dev
254 * @port_num: Port number, 1 based from ib core
255 * @guid_index: Index in table
256 * @gid: Gid to return
258 * Return: 0 on success
260 static int rvt_query_gid(struct ib_device *ibdev, u8 port_num,
261 int guid_index, union ib_gid *gid)
263 struct rvt_dev_info *rdi;
264 struct rvt_ibport *rvp;
268 * Driver is responsible for updating the guid table. Which will be used
269 * to craft the return value. This will work similar to how query_pkey()
272 port_index = ibport_num_to_idx(ibdev, port_num);
276 rdi = ib_to_rvt(ibdev);
277 rvp = rdi->ports[port_index];
279 gid->global.subnet_prefix = rvp->gid_prefix;
281 return rdi->driver_f.get_guid_be(rdi, rvp, guid_index,
282 &gid->global.interface_id);
285 struct rvt_ucontext {
286 struct ib_ucontext ibucontext;
289 static inline struct rvt_ucontext *to_iucontext(struct ib_ucontext
292 return container_of(ibucontext, struct rvt_ucontext, ibucontext);
296 * rvt_alloc_ucontext - Allocate a user context
297 * @ibdev: Verbs IB dev
298 * @udata: User data allocated
300 static struct ib_ucontext *rvt_alloc_ucontext(struct ib_device *ibdev,
301 struct ib_udata *udata)
303 struct rvt_ucontext *context;
305 context = kmalloc(sizeof(*context), GFP_KERNEL);
307 return ERR_PTR(-ENOMEM);
308 return &context->ibucontext;
312 *rvt_dealloc_ucontext - Free a user context
313 *@context - Free this
315 static int rvt_dealloc_ucontext(struct ib_ucontext *context)
317 kfree(to_iucontext(context));
321 static int rvt_get_port_immutable(struct ib_device *ibdev, u8 port_num,
322 struct ib_port_immutable *immutable)
324 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
325 struct ib_port_attr attr;
328 port_index = ibport_num_to_idx(ibdev, port_num);
332 immutable->core_cap_flags = rdi->dparms.core_cap_flags;
334 err = ib_query_port(ibdev, port_num, &attr);
338 immutable->pkey_tbl_len = attr.pkey_tbl_len;
339 immutable->gid_tbl_len = attr.gid_tbl_len;
340 immutable->max_mad_size = rdi->dparms.max_mad_size;
390 _VERB_IDX_MAX /* Must always be last! */
393 static inline int check_driver_override(struct rvt_dev_info *rdi,
394 size_t offset, void *func)
396 if (!*(void **)((void *)&rdi->ibdev + offset)) {
397 *(void **)((void *)&rdi->ibdev + offset) = func;
404 static noinline int check_support(struct rvt_dev_info *rdi, int verb)
409 * These functions are not part of verbs specifically but are
410 * required for rdmavt to function.
412 if ((!rdi->driver_f.port_callback) ||
413 (!rdi->driver_f.get_pci_dev))
418 check_driver_override(rdi, offsetof(struct ib_device,
425 * rdmavt does not support modify device currently drivers must
428 if (!check_driver_override(rdi, offsetof(struct ib_device,
435 if (!check_driver_override(rdi, offsetof(struct ib_device,
438 if (!rdi->driver_f.query_port_state)
443 if (!check_driver_override(rdi, offsetof(struct ib_device,
446 if (!rdi->driver_f.cap_mask_chg ||
447 !rdi->driver_f.shut_down_port)
452 check_driver_override(rdi, offsetof(struct ib_device,
458 if (!check_driver_override(rdi, offsetof(struct ib_device,
461 if (!rdi->driver_f.get_guid_be)
466 check_driver_override(rdi, offsetof(struct ib_device,
471 case DEALLOC_UCONTEXT:
472 check_driver_override(rdi, offsetof(struct ib_device,
474 rvt_dealloc_ucontext);
477 case GET_PORT_IMMUTABLE:
478 check_driver_override(rdi, offsetof(struct ib_device,
480 rvt_get_port_immutable);
484 if (!check_driver_override(rdi, offsetof(struct ib_device,
487 if (!rdi->driver_f.qp_priv_alloc ||
488 !rdi->driver_f.qp_priv_free ||
489 !rdi->driver_f.notify_qp_reset ||
490 !rdi->driver_f.flush_qp_waiters ||
491 !rdi->driver_f.stop_send_queue ||
492 !rdi->driver_f.quiesce_qp)
497 if (!check_driver_override(rdi, offsetof(struct ib_device,
500 if (!rdi->driver_f.notify_qp_reset ||
501 !rdi->driver_f.schedule_send ||
502 !rdi->driver_f.get_pmtu_from_attr ||
503 !rdi->driver_f.flush_qp_waiters ||
504 !rdi->driver_f.stop_send_queue ||
505 !rdi->driver_f.quiesce_qp ||
506 !rdi->driver_f.notify_error_qp ||
507 !rdi->driver_f.mtu_from_qp ||
508 !rdi->driver_f.mtu_to_path_mtu)
513 if (!check_driver_override(rdi, offsetof(struct ib_device,
516 if (!rdi->driver_f.qp_priv_free ||
517 !rdi->driver_f.notify_qp_reset ||
518 !rdi->driver_f.flush_qp_waiters ||
519 !rdi->driver_f.stop_send_queue ||
520 !rdi->driver_f.quiesce_qp)
525 check_driver_override(rdi, offsetof(struct ib_device,
531 if (!check_driver_override(rdi, offsetof(struct ib_device,
534 if (!rdi->driver_f.schedule_send ||
535 !rdi->driver_f.do_send ||
541 check_driver_override(rdi, offsetof(struct ib_device,
546 check_driver_override(rdi, offsetof(struct ib_device,
552 check_driver_override(rdi, offsetof(struct ib_device,
558 check_driver_override(rdi, offsetof(struct ib_device,
564 check_driver_override(rdi, offsetof(struct ib_device,
570 check_driver_override(rdi, offsetof(struct ib_device,
576 check_driver_override(rdi, offsetof(struct ib_device,
582 check_driver_override(rdi, offsetof(struct ib_device,
588 check_driver_override(rdi, offsetof(struct ib_device,
594 check_driver_override(rdi, offsetof(struct ib_device,
600 check_driver_override(rdi, offsetof(struct ib_device,
606 check_driver_override(rdi, offsetof(struct ib_device,
612 check_driver_override(rdi, offsetof(struct ib_device,
618 check_driver_override(rdi, offsetof(struct ib_device,
624 check_driver_override(rdi, offsetof(struct ib_device,
630 check_driver_override(rdi, offsetof(struct ib_device,
636 check_driver_override(rdi, offsetof(struct ib_device,
642 check_driver_override(rdi, offsetof(struct ib_device,
648 check_driver_override(rdi, offsetof(struct ib_device,
654 check_driver_override(rdi, offsetof(struct ib_device,
660 check_driver_override(rdi, offsetof(struct ib_device,
666 check_driver_override(rdi, offsetof(struct ib_device,
672 check_driver_override(rdi, offsetof(struct ib_device,
678 check_driver_override(rdi, offsetof(struct ib_device,
684 check_driver_override(rdi, offsetof(struct ib_device,
690 check_driver_override(rdi, offsetof(struct ib_device,
696 check_driver_override(rdi, offsetof(struct ib_device,
702 check_driver_override(rdi, offsetof(struct ib_device,
708 check_driver_override(rdi, offsetof(struct ib_device,
721 * rvt_register_device - register a driver
722 * @rdi: main dev structure for all of rdmavt operations
724 * It is up to drivers to allocate the rdi and fill in the appropriate
727 * Return: 0 on success otherwise an errno.
729 int rvt_register_device(struct rvt_dev_info *rdi, u32 driver_id)
737 * Check to ensure drivers have setup the required helpers for the verbs
738 * they want rdmavt to handle
740 for (i = 0; i < _VERB_IDX_MAX; i++)
741 if (check_support(rdi, i)) {
742 pr_err("Driver support req not met at %d\n", i);
747 /* Once we get past here we can use rvt_pr macros and tracepoints */
748 trace_rvt_dbg(rdi, "Driver attempting registration");
752 ret = rvt_driver_qp_init(rdi);
754 pr_err("Error in driver QP init.\n");
759 spin_lock_init(&rdi->n_ahs_lock);
760 rdi->n_ahs_allocated = 0;
762 /* Shared Receive Queue */
763 rvt_driver_srq_init(rdi);
766 rvt_driver_mcast_init(rdi);
769 ret = rvt_driver_mr_init(rdi);
771 pr_err("Error in driver MR init.\n");
775 /* Completion queues */
776 spin_lock_init(&rdi->n_cqs_lock);
779 rdi->ibdev.dev.dma_ops = rdi->ibdev.dev.dma_ops ? : &dma_virt_ops;
781 /* Protection Domain */
782 spin_lock_init(&rdi->n_pds_lock);
783 rdi->n_pds_allocated = 0;
786 * There are some things which could be set by underlying drivers but
787 * really should be up to rdmavt to set. For instance drivers can't know
788 * exactly which functions rdmavt supports, nor do they know the ABI
789 * version, so we do all of this sort of stuff here.
791 rdi->ibdev.uverbs_abi_ver = RVT_UVERBS_ABI_VERSION;
792 rdi->ibdev.uverbs_cmd_mask =
793 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
794 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
795 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
796 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
797 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
798 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
799 (1ull << IB_USER_VERBS_CMD_MODIFY_AH) |
800 (1ull << IB_USER_VERBS_CMD_QUERY_AH) |
801 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
802 (1ull << IB_USER_VERBS_CMD_REG_MR) |
803 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
804 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
805 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
806 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
807 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
808 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
809 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
810 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
811 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
812 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
813 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
814 (1ull << IB_USER_VERBS_CMD_POST_SEND) |
815 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
816 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
817 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
818 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
819 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
820 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
821 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
822 (1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
823 rdi->ibdev.node_type = RDMA_NODE_IB_CA;
824 if (!rdi->ibdev.num_comp_vectors)
825 rdi->ibdev.num_comp_vectors = 1;
827 rdi->ibdev.driver_id = driver_id;
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");
848 EXPORT_SYMBOL(rvt_register_device);
851 * rvt_unregister_device - remove a driver
852 * @rdi: rvt dev struct
854 void rvt_unregister_device(struct rvt_dev_info *rdi)
856 trace_rvt_dbg(rdi, "Driver is unregistering.");
860 rvt_free_mad_agents(rdi);
862 ib_unregister_device(&rdi->ibdev);
866 EXPORT_SYMBOL(rvt_unregister_device);
869 * rvt_init_port - init internal data for driver port
870 * @rdi: rvt dev strut
872 * @port_index: 0 based index of ports, different from IB core port num
874 * Keep track of a list of ports. No need to have a detach port.
875 * They persist until the driver goes away.
879 int rvt_init_port(struct rvt_dev_info *rdi, struct rvt_ibport *port,
880 int port_index, u16 *pkey_table)
883 rdi->ports[port_index] = port;
884 rdi->ports[port_index]->pkey_table = pkey_table;
888 EXPORT_SYMBOL(rvt_init_port);