1 // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
3 * Copyright(c) 2016 - 2018 Intel Corporation.
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/dma-mapping.h>
13 #define RVT_UVERBS_ABI_VERSION 2
15 MODULE_LICENSE("Dual BSD/GPL");
16 MODULE_DESCRIPTION("RDMA Verbs Transport Library");
18 static int rvt_init(void)
20 int ret = rvt_driver_cq_init();
23 pr_err("Error in driver CQ init.\n");
27 module_init(rvt_init);
29 static void rvt_cleanup(void)
33 module_exit(rvt_cleanup);
36 * rvt_alloc_device - allocate rdi
37 * @size: how big of a structure to allocate
38 * @nports: number of ports to allocate array slots for
40 * Use IB core device alloc to allocate space for the rdi which is assumed to be
41 * inside of the ib_device. Any extra space that drivers require should be
44 * We also allocate a port array based on the number of ports.
46 * Return: pointer to allocated rdi
48 struct rvt_dev_info *rvt_alloc_device(size_t size, int nports)
50 struct rvt_dev_info *rdi;
52 rdi = container_of(_ib_alloc_device(size), struct rvt_dev_info, ibdev);
56 rdi->ports = kcalloc(nports, sizeof(*rdi->ports), GFP_KERNEL);
58 ib_dealloc_device(&rdi->ibdev);
62 EXPORT_SYMBOL(rvt_alloc_device);
65 * rvt_dealloc_device - deallocate rdi
66 * @rdi: structure to free
68 * Free a structure allocated with rvt_alloc_device()
70 void rvt_dealloc_device(struct rvt_dev_info *rdi)
73 ib_dealloc_device(&rdi->ibdev);
75 EXPORT_SYMBOL(rvt_dealloc_device);
77 static int rvt_query_device(struct ib_device *ibdev,
78 struct ib_device_attr *props,
81 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
83 if (uhw->inlen || uhw->outlen)
86 * Return rvt_dev_info.dparms.props contents
88 *props = rdi->dparms.props;
92 static int rvt_get_numa_node(struct ib_device *ibdev)
94 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
96 return rdi->dparms.node;
99 static int rvt_modify_device(struct ib_device *device,
100 int device_modify_mask,
101 struct ib_device_modify *device_modify)
104 * There is currently no need to supply this based on qib and hfi1.
105 * Future drivers may need to implement this though.
112 * rvt_query_port - Passes the query port call to the driver
113 * @ibdev: Verbs IB dev
114 * @port_num: port number, 1 based from ib core
115 * @props: structure to hold returned properties
117 * Return: 0 on success
119 static int rvt_query_port(struct ib_device *ibdev, u32 port_num,
120 struct ib_port_attr *props)
122 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
123 struct rvt_ibport *rvp;
124 u32 port_index = ibport_num_to_idx(ibdev, port_num);
126 rvp = rdi->ports[port_index];
127 /* props being zeroed by the caller, avoid zeroing it here */
128 props->sm_lid = rvp->sm_lid;
129 props->sm_sl = rvp->sm_sl;
130 props->port_cap_flags = rvp->port_cap_flags;
131 props->max_msg_sz = 0x80000000;
132 props->pkey_tbl_len = rvt_get_npkeys(rdi);
133 props->bad_pkey_cntr = rvp->pkey_violations;
134 props->qkey_viol_cntr = rvp->qkey_violations;
135 props->subnet_timeout = rvp->subnet_timeout;
136 props->init_type_reply = 0;
138 /* Populate the remaining ib_port_attr elements */
139 return rdi->driver_f.query_port_state(rdi, port_num, props);
143 * rvt_modify_port - modify port
144 * @ibdev: Verbs IB dev
145 * @port_num: Port number, 1 based from ib core
146 * @port_modify_mask: How to change the port
147 * @props: Structure to fill in
149 * Return: 0 on success
151 static int rvt_modify_port(struct ib_device *ibdev, u32 port_num,
152 int port_modify_mask, struct ib_port_modify *props)
154 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
155 struct rvt_ibport *rvp;
157 u32 port_index = ibport_num_to_idx(ibdev, port_num);
159 rvp = rdi->ports[port_index];
160 if (port_modify_mask & IB_PORT_OPA_MASK_CHG) {
161 rvp->port_cap3_flags |= props->set_port_cap_mask;
162 rvp->port_cap3_flags &= ~props->clr_port_cap_mask;
164 rvp->port_cap_flags |= props->set_port_cap_mask;
165 rvp->port_cap_flags &= ~props->clr_port_cap_mask;
168 if (props->set_port_cap_mask || props->clr_port_cap_mask)
169 rdi->driver_f.cap_mask_chg(rdi, port_num);
170 if (port_modify_mask & IB_PORT_SHUTDOWN)
171 ret = rdi->driver_f.shut_down_port(rdi, port_num);
172 if (port_modify_mask & IB_PORT_RESET_QKEY_CNTR)
173 rvp->qkey_violations = 0;
179 * rvt_query_pkey - Return a pkey from the table at a given index
180 * @ibdev: Verbs IB dev
181 * @port_num: Port number, 1 based from ib core
182 * @index: Index into pkey table
183 * @pkey: returned pkey from the port pkey table
185 * Return: 0 on failure pkey otherwise
187 static int rvt_query_pkey(struct ib_device *ibdev, u32 port_num, u16 index,
191 * Driver will be responsible for keeping rvt_dev_info.pkey_table up to
192 * date. This function will just return that value. There is no need to
193 * lock, if a stale value is read and sent to the user so be it there is
194 * no way to protect against that anyway.
196 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
199 port_index = ibport_num_to_idx(ibdev, port_num);
201 if (index >= rvt_get_npkeys(rdi))
204 *pkey = rvt_get_pkey(rdi, port_index, index);
209 * rvt_query_gid - Return a gid from the table
210 * @ibdev: Verbs IB dev
211 * @port_num: Port number, 1 based from ib core
212 * @guid_index: Index in table
213 * @gid: Gid to return
215 * Return: 0 on success
217 static int rvt_query_gid(struct ib_device *ibdev, u32 port_num,
218 int guid_index, union ib_gid *gid)
220 struct rvt_dev_info *rdi;
221 struct rvt_ibport *rvp;
225 * Driver is responsible for updating the guid table. Which will be used
226 * to craft the return value. This will work similar to how query_pkey()
229 port_index = ibport_num_to_idx(ibdev, port_num);
231 rdi = ib_to_rvt(ibdev);
232 rvp = rdi->ports[port_index];
234 gid->global.subnet_prefix = rvp->gid_prefix;
236 return rdi->driver_f.get_guid_be(rdi, rvp, guid_index,
237 &gid->global.interface_id);
241 * rvt_alloc_ucontext - Allocate a user context
242 * @uctx: Verbs context
243 * @udata: User data allocated
245 static int rvt_alloc_ucontext(struct ib_ucontext *uctx, struct ib_udata *udata)
251 * rvt_dealloc_ucontext - Free a user context
254 static void rvt_dealloc_ucontext(struct ib_ucontext *context)
259 static int rvt_get_port_immutable(struct ib_device *ibdev, u32 port_num,
260 struct ib_port_immutable *immutable)
262 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
263 struct ib_port_attr attr;
266 immutable->core_cap_flags = rdi->dparms.core_cap_flags;
268 err = ib_query_port(ibdev, port_num, &attr);
272 immutable->pkey_tbl_len = attr.pkey_tbl_len;
273 immutable->gid_tbl_len = attr.gid_tbl_len;
274 immutable->max_mad_size = rdi->dparms.max_mad_size;
324 _VERB_IDX_MAX /* Must always be last! */
327 static const struct ib_device_ops rvt_dev_ops = {
328 .uverbs_abi_ver = RVT_UVERBS_ABI_VERSION,
330 .alloc_mr = rvt_alloc_mr,
331 .alloc_pd = rvt_alloc_pd,
332 .alloc_ucontext = rvt_alloc_ucontext,
333 .attach_mcast = rvt_attach_mcast,
334 .create_ah = rvt_create_ah,
335 .create_cq = rvt_create_cq,
336 .create_qp = rvt_create_qp,
337 .create_srq = rvt_create_srq,
338 .create_user_ah = rvt_create_ah,
339 .dealloc_pd = rvt_dealloc_pd,
340 .dealloc_ucontext = rvt_dealloc_ucontext,
341 .dereg_mr = rvt_dereg_mr,
342 .destroy_ah = rvt_destroy_ah,
343 .destroy_cq = rvt_destroy_cq,
344 .destroy_qp = rvt_destroy_qp,
345 .destroy_srq = rvt_destroy_srq,
346 .detach_mcast = rvt_detach_mcast,
347 .get_dma_mr = rvt_get_dma_mr,
348 .get_numa_node = rvt_get_numa_node,
349 .get_port_immutable = rvt_get_port_immutable,
350 .map_mr_sg = rvt_map_mr_sg,
352 .modify_ah = rvt_modify_ah,
353 .modify_device = rvt_modify_device,
354 .modify_port = rvt_modify_port,
355 .modify_qp = rvt_modify_qp,
356 .modify_srq = rvt_modify_srq,
357 .poll_cq = rvt_poll_cq,
358 .post_recv = rvt_post_recv,
359 .post_send = rvt_post_send,
360 .post_srq_recv = rvt_post_srq_recv,
361 .query_ah = rvt_query_ah,
362 .query_device = rvt_query_device,
363 .query_gid = rvt_query_gid,
364 .query_pkey = rvt_query_pkey,
365 .query_port = rvt_query_port,
366 .query_qp = rvt_query_qp,
367 .query_srq = rvt_query_srq,
368 .reg_user_mr = rvt_reg_user_mr,
369 .req_notify_cq = rvt_req_notify_cq,
370 .resize_cq = rvt_resize_cq,
372 INIT_RDMA_OBJ_SIZE(ib_ah, rvt_ah, ibah),
373 INIT_RDMA_OBJ_SIZE(ib_cq, rvt_cq, ibcq),
374 INIT_RDMA_OBJ_SIZE(ib_pd, rvt_pd, ibpd),
375 INIT_RDMA_OBJ_SIZE(ib_qp, rvt_qp, ibqp),
376 INIT_RDMA_OBJ_SIZE(ib_srq, rvt_srq, ibsrq),
377 INIT_RDMA_OBJ_SIZE(ib_ucontext, rvt_ucontext, ibucontext),
380 static noinline int check_support(struct rvt_dev_info *rdi, int verb)
385 * These functions are not part of verbs specifically but are
386 * required for rdmavt to function.
388 if ((!rdi->ibdev.ops.port_groups) ||
389 (!rdi->driver_f.get_pci_dev))
395 * rdmavt does not support modify device currently drivers must
398 if (!rdi->ibdev.ops.modify_device)
403 if (!rdi->ibdev.ops.query_port)
404 if (!rdi->driver_f.query_port_state)
409 if (!rdi->ibdev.ops.modify_port)
410 if (!rdi->driver_f.cap_mask_chg ||
411 !rdi->driver_f.shut_down_port)
416 if (!rdi->ibdev.ops.query_gid)
417 if (!rdi->driver_f.get_guid_be)
422 if (!rdi->ibdev.ops.create_qp)
423 if (!rdi->driver_f.qp_priv_alloc ||
424 !rdi->driver_f.qp_priv_free ||
425 !rdi->driver_f.notify_qp_reset ||
426 !rdi->driver_f.flush_qp_waiters ||
427 !rdi->driver_f.stop_send_queue ||
428 !rdi->driver_f.quiesce_qp)
433 if (!rdi->ibdev.ops.modify_qp)
434 if (!rdi->driver_f.notify_qp_reset ||
435 !rdi->driver_f.schedule_send ||
436 !rdi->driver_f.get_pmtu_from_attr ||
437 !rdi->driver_f.flush_qp_waiters ||
438 !rdi->driver_f.stop_send_queue ||
439 !rdi->driver_f.quiesce_qp ||
440 !rdi->driver_f.notify_error_qp ||
441 !rdi->driver_f.mtu_from_qp ||
442 !rdi->driver_f.mtu_to_path_mtu)
447 if (!rdi->ibdev.ops.destroy_qp)
448 if (!rdi->driver_f.qp_priv_free ||
449 !rdi->driver_f.notify_qp_reset ||
450 !rdi->driver_f.flush_qp_waiters ||
451 !rdi->driver_f.stop_send_queue ||
452 !rdi->driver_f.quiesce_qp)
457 if (!rdi->ibdev.ops.post_send)
458 if (!rdi->driver_f.schedule_send ||
459 !rdi->driver_f.do_send ||
470 * rvt_register_device - register a driver
471 * @rdi: main dev structure for all of rdmavt operations
473 * It is up to drivers to allocate the rdi and fill in the appropriate
476 * Return: 0 on success otherwise an errno.
478 int rvt_register_device(struct rvt_dev_info *rdi)
486 * Check to ensure drivers have setup the required helpers for the verbs
487 * they want rdmavt to handle
489 for (i = 0; i < _VERB_IDX_MAX; i++)
490 if (check_support(rdi, i)) {
491 pr_err("Driver support req not met at %d\n", i);
495 ib_set_device_ops(&rdi->ibdev, &rvt_dev_ops);
497 /* Once we get past here we can use rvt_pr macros and tracepoints */
498 trace_rvt_dbg(rdi, "Driver attempting registration");
502 ret = rvt_driver_qp_init(rdi);
504 pr_err("Error in driver QP init.\n");
509 spin_lock_init(&rdi->n_ahs_lock);
510 rdi->n_ahs_allocated = 0;
512 /* Shared Receive Queue */
513 rvt_driver_srq_init(rdi);
516 rvt_driver_mcast_init(rdi);
519 ret = rvt_driver_mr_init(rdi);
521 pr_err("Error in driver MR init.\n");
525 /* Memory Working Set Size */
526 ret = rvt_wss_init(rdi);
528 rvt_pr_err(rdi, "Error in WSS init.\n");
532 /* Completion queues */
533 spin_lock_init(&rdi->n_cqs_lock);
535 /* Protection Domain */
536 spin_lock_init(&rdi->n_pds_lock);
537 rdi->n_pds_allocated = 0;
540 * There are some things which could be set by underlying drivers but
541 * really should be up to rdmavt to set. For instance drivers can't know
542 * exactly which functions rdmavt supports, nor do they know the ABI
543 * version, so we do all of this sort of stuff here.
545 rdi->ibdev.uverbs_cmd_mask |=
546 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
547 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
548 (1ull << IB_USER_VERBS_CMD_POST_SEND) |
549 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
550 (1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
551 rdi->ibdev.node_type = RDMA_NODE_IB_CA;
552 if (!rdi->ibdev.num_comp_vectors)
553 rdi->ibdev.num_comp_vectors = 1;
555 /* We are now good to announce we exist */
556 ret = ib_register_device(&rdi->ibdev, dev_name(&rdi->ibdev.dev), NULL);
558 rvt_pr_err(rdi, "Failed to register driver with ib core.\n");
562 rvt_create_mad_agents(rdi);
564 rvt_pr_info(rdi, "Registration with rdmavt done.\n");
577 EXPORT_SYMBOL(rvt_register_device);
580 * rvt_unregister_device - remove a driver
581 * @rdi: rvt dev struct
583 void rvt_unregister_device(struct rvt_dev_info *rdi)
585 trace_rvt_dbg(rdi, "Driver is unregistering.");
589 rvt_free_mad_agents(rdi);
591 ib_unregister_device(&rdi->ibdev);
596 EXPORT_SYMBOL(rvt_unregister_device);
599 * rvt_init_port - init internal data for driver port
600 * @rdi: rvt_dev_info struct
602 * @port_index: 0 based index of ports, different from IB core port num
603 * @pkey_table: pkey_table for @port
605 * Keep track of a list of ports. No need to have a detach port.
606 * They persist until the driver goes away.
610 int rvt_init_port(struct rvt_dev_info *rdi, struct rvt_ibport *port,
611 int port_index, u16 *pkey_table)
614 rdi->ports[port_index] = port;
615 rdi->ports[port_index]->pkey_table = pkey_table;
619 EXPORT_SYMBOL(rvt_init_port);