xref: /linux/drivers/infiniband/sw/rdmavt/vt.c (revision fbf5df34a4dbcd09d433dd4f0916bf9b2ddb16de)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright(c) 2016 - 2018 Intel Corporation.
4  */
5 
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/dma-mapping.h>
9 #include "vt.h"
10 #include "cq.h"
11 #include "trace.h"
12 
13 #define RVT_UVERBS_ABI_VERSION 2
14 
15 MODULE_LICENSE("Dual BSD/GPL");
16 MODULE_DESCRIPTION("RDMA Verbs Transport Library");
17 
18 static int __init rvt_init(void)
19 {
20 	int ret = rvt_driver_cq_init();
21 
22 	if (ret)
23 		pr_err("Error in driver CQ init.\n");
24 
25 	return ret;
26 }
27 module_init(rvt_init);
28 
29 static void __exit rvt_cleanup(void)
30 {
31 	rvt_cq_exit();
32 }
33 module_exit(rvt_cleanup);
34 
35 /**
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
39  *
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
42  * included in size.
43  *
44  * We also allocate a port array based on the number of ports.
45  *
46  * Return: pointer to allocated rdi
47  */
48 struct rvt_dev_info *rvt_alloc_device(size_t size, int nports)
49 {
50 	struct rvt_dev_info *rdi;
51 
52 	rdi = container_of(_ib_alloc_device(size, &init_net), struct rvt_dev_info, ibdev);
53 	if (!rdi)
54 		return rdi;
55 
56 	rdi->ports = kzalloc_objs(*rdi->ports, nports);
57 	if (!rdi->ports)
58 		ib_dealloc_device(&rdi->ibdev);
59 
60 	return rdi;
61 }
62 EXPORT_SYMBOL(rvt_alloc_device);
63 
64 /**
65  * rvt_dealloc_device - deallocate rdi
66  * @rdi: structure to free
67  *
68  * Free a structure allocated with rvt_alloc_device()
69  */
70 void rvt_dealloc_device(struct rvt_dev_info *rdi)
71 {
72 	kfree(rdi->ports);
73 	ib_dealloc_device(&rdi->ibdev);
74 }
75 EXPORT_SYMBOL(rvt_dealloc_device);
76 
77 static int rvt_query_device(struct ib_device *ibdev,
78 			    struct ib_device_attr *props,
79 			    struct ib_udata *uhw)
80 {
81 	struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
82 
83 	if (uhw->inlen || uhw->outlen)
84 		return -EINVAL;
85 	/*
86 	 * Return rvt_dev_info.dparms.props contents
87 	 */
88 	*props = rdi->dparms.props;
89 	return 0;
90 }
91 
92 static int rvt_get_numa_node(struct ib_device *ibdev)
93 {
94 	struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
95 
96 	return rdi->dparms.node;
97 }
98 
99 static int rvt_modify_device(struct ib_device *device,
100 			     int device_modify_mask,
101 			     struct ib_device_modify *device_modify)
102 {
103 	/*
104 	 * There is currently no need to supply this based on qib and hfi1.
105 	 * Future drivers may need to implement this though.
106 	 */
107 
108 	return -EOPNOTSUPP;
109 }
110 
111 /**
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
116  *
117  * Return: 0 on success
118  */
119 static int rvt_query_port(struct ib_device *ibdev, u32 port_num,
120 			  struct ib_port_attr *props)
121 {
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);
125 
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;
137 
138 	/* Populate the remaining ib_port_attr elements */
139 	return rdi->driver_f.query_port_state(rdi, port_num, props);
140 }
141 
142 /**
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
148  *
149  * Return: 0 on success
150  */
151 static int rvt_modify_port(struct ib_device *ibdev, u32 port_num,
152 			   int port_modify_mask, struct ib_port_modify *props)
153 {
154 	struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
155 	struct rvt_ibport *rvp;
156 	int ret = 0;
157 	u32 port_index = ibport_num_to_idx(ibdev, port_num);
158 
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;
163 	} else {
164 		rvp->port_cap_flags |= props->set_port_cap_mask;
165 		rvp->port_cap_flags &= ~props->clr_port_cap_mask;
166 	}
167 
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;
174 
175 	return ret;
176 }
177 
178 /**
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
184  *
185  * Return: 0 on failure pkey otherwise
186  */
187 static int rvt_query_pkey(struct ib_device *ibdev, u32 port_num, u16 index,
188 			  u16 *pkey)
189 {
190 	/*
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.
195 	 */
196 	struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
197 	u32 port_index;
198 
199 	port_index = ibport_num_to_idx(ibdev, port_num);
200 
201 	if (index >= rvt_get_npkeys(rdi))
202 		return -EINVAL;
203 
204 	*pkey = rvt_get_pkey(rdi, port_index, index);
205 	return 0;
206 }
207 
208 /**
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
214  *
215  * Return: 0 on success
216  */
217 static int rvt_query_gid(struct ib_device *ibdev, u32 port_num,
218 			 int guid_index, union ib_gid *gid)
219 {
220 	struct rvt_dev_info *rdi;
221 	struct rvt_ibport *rvp;
222 	u32 port_index;
223 
224 	/*
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()
227 	 * is being done.
228 	 */
229 	port_index = ibport_num_to_idx(ibdev, port_num);
230 
231 	rdi = ib_to_rvt(ibdev);
232 	rvp = rdi->ports[port_index];
233 
234 	gid->global.subnet_prefix = rvp->gid_prefix;
235 
236 	return rdi->driver_f.get_guid_be(rdi, rvp, guid_index,
237 					 &gid->global.interface_id);
238 }
239 
240 /**
241  * rvt_alloc_ucontext - Allocate a user context
242  * @uctx: Verbs context
243  * @udata: User data allocated
244  */
245 static int rvt_alloc_ucontext(struct ib_ucontext *uctx, struct ib_udata *udata)
246 {
247 	struct rvt_dev_info *rdi = ib_to_rvt(uctx->device);
248 
249 	if (rdi->driver_f.alloc_ucontext)
250 		return rdi->driver_f.alloc_ucontext(uctx, udata);
251 	return 0;
252 }
253 
254 /**
255  * rvt_dealloc_ucontext - Free a user context
256  * @context: Unused
257  */
258 static void rvt_dealloc_ucontext(struct ib_ucontext *context)
259 {
260 	struct rvt_dev_info *rdi = ib_to_rvt(context->device);
261 
262 	if (rdi->driver_f.dealloc_ucontext)
263 		rdi->driver_f.dealloc_ucontext(context);
264 	return;
265 }
266 
267 static int rvt_get_port_immutable(struct ib_device *ibdev, u32 port_num,
268 				  struct ib_port_immutable *immutable)
269 {
270 	struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
271 	struct ib_port_attr attr;
272 	int err;
273 
274 	immutable->core_cap_flags = rdi->dparms.core_cap_flags;
275 
276 	err = ib_query_port(ibdev, port_num, &attr);
277 	if (err)
278 		return err;
279 
280 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
281 	immutable->gid_tbl_len = attr.gid_tbl_len;
282 	immutable->max_mad_size = rdi->dparms.max_mad_size;
283 
284 	return 0;
285 }
286 
287 enum {
288 	MISC,
289 	QUERY_DEVICE,
290 	MODIFY_DEVICE,
291 	QUERY_PORT,
292 	MODIFY_PORT,
293 	QUERY_PKEY,
294 	QUERY_GID,
295 	ALLOC_UCONTEXT,
296 	DEALLOC_UCONTEXT,
297 	GET_PORT_IMMUTABLE,
298 	CREATE_QP,
299 	MODIFY_QP,
300 	DESTROY_QP,
301 	QUERY_QP,
302 	POST_SEND,
303 	POST_RECV,
304 	POST_SRQ_RECV,
305 	CREATE_AH,
306 	DESTROY_AH,
307 	MODIFY_AH,
308 	QUERY_AH,
309 	CREATE_SRQ,
310 	MODIFY_SRQ,
311 	DESTROY_SRQ,
312 	QUERY_SRQ,
313 	ATTACH_MCAST,
314 	DETACH_MCAST,
315 	GET_DMA_MR,
316 	REG_USER_MR,
317 	DEREG_MR,
318 	ALLOC_MR,
319 	MAP_MR_SG,
320 	ALLOC_FMR,
321 	MAP_PHYS_FMR,
322 	UNMAP_FMR,
323 	DEALLOC_FMR,
324 	MMAP,
325 	CREATE_CQ,
326 	DESTROY_CQ,
327 	POLL_CQ,
328 	REQ_NOTFIY_CQ,
329 	RESIZE_CQ,
330 	ALLOC_PD,
331 	DEALLOC_PD,
332 	_VERB_IDX_MAX /* Must always be last! */
333 };
334 
335 static const struct ib_device_ops rvt_dev_ops = {
336 	.uverbs_abi_ver = RVT_UVERBS_ABI_VERSION,
337 
338 	.alloc_mr = rvt_alloc_mr,
339 	.alloc_pd = rvt_alloc_pd,
340 	.alloc_ucontext = rvt_alloc_ucontext,
341 	.attach_mcast = rvt_attach_mcast,
342 	.create_ah = rvt_create_ah,
343 	.create_cq = rvt_create_cq,
344 	.create_qp = rvt_create_qp,
345 	.create_srq = rvt_create_srq,
346 	.create_user_ah = rvt_create_ah,
347 	.dealloc_pd = rvt_dealloc_pd,
348 	.dealloc_ucontext = rvt_dealloc_ucontext,
349 	.dereg_mr = rvt_dereg_mr,
350 	.destroy_ah = rvt_destroy_ah,
351 	.destroy_cq = rvt_destroy_cq,
352 	.destroy_qp = rvt_destroy_qp,
353 	.destroy_srq = rvt_destroy_srq,
354 	.detach_mcast = rvt_detach_mcast,
355 	.get_dma_mr = rvt_get_dma_mr,
356 	.get_numa_node = rvt_get_numa_node,
357 	.get_port_immutable = rvt_get_port_immutable,
358 	.map_mr_sg = rvt_map_mr_sg,
359 	.mmap = rvt_mmap,
360 	.modify_ah = rvt_modify_ah,
361 	.modify_device = rvt_modify_device,
362 	.modify_port = rvt_modify_port,
363 	.modify_qp = rvt_modify_qp,
364 	.modify_srq = rvt_modify_srq,
365 	.poll_cq = rvt_poll_cq,
366 	.post_recv = rvt_post_recv,
367 	.post_send = rvt_post_send,
368 	.post_srq_recv = rvt_post_srq_recv,
369 	.query_ah = rvt_query_ah,
370 	.query_device = rvt_query_device,
371 	.query_gid = rvt_query_gid,
372 	.query_pkey = rvt_query_pkey,
373 	.query_port = rvt_query_port,
374 	.query_qp = rvt_query_qp,
375 	.query_srq = rvt_query_srq,
376 	.reg_user_mr = rvt_reg_user_mr,
377 	.req_notify_cq = rvt_req_notify_cq,
378 	.resize_user_cq = rvt_resize_cq,
379 
380 	INIT_RDMA_OBJ_SIZE(ib_ah, rvt_ah, ibah),
381 	INIT_RDMA_OBJ_SIZE(ib_cq, rvt_cq, ibcq),
382 	INIT_RDMA_OBJ_SIZE(ib_pd, rvt_pd, ibpd),
383 	INIT_RDMA_OBJ_SIZE(ib_qp, rvt_qp, ibqp),
384 	INIT_RDMA_OBJ_SIZE(ib_srq, rvt_srq, ibsrq),
385 	INIT_RDMA_OBJ_SIZE(ib_ucontext, rvt_ucontext, ibucontext),
386 };
387 
388 static noinline int check_support(struct rvt_dev_info *rdi, int verb)
389 {
390 	switch (verb) {
391 	case MISC:
392 		/*
393 		 * These functions are not part of verbs specifically but are
394 		 * required for rdmavt to function.
395 		 */
396 		if ((!rdi->ibdev.ops.port_groups) ||
397 		    (!rdi->driver_f.get_pci_dev))
398 			return -EINVAL;
399 		break;
400 
401 	case MODIFY_DEVICE:
402 		/*
403 		 * rdmavt does not support modify device currently drivers must
404 		 * provide.
405 		 */
406 		if (!rdi->ibdev.ops.modify_device)
407 			return -EOPNOTSUPP;
408 		break;
409 
410 	case QUERY_PORT:
411 		if (!rdi->ibdev.ops.query_port)
412 			if (!rdi->driver_f.query_port_state)
413 				return -EINVAL;
414 		break;
415 
416 	case MODIFY_PORT:
417 		if (!rdi->ibdev.ops.modify_port)
418 			if (!rdi->driver_f.cap_mask_chg ||
419 			    !rdi->driver_f.shut_down_port)
420 				return -EINVAL;
421 		break;
422 
423 	case QUERY_GID:
424 		if (!rdi->ibdev.ops.query_gid)
425 			if (!rdi->driver_f.get_guid_be)
426 				return -EINVAL;
427 		break;
428 
429 	case CREATE_QP:
430 		if (!rdi->ibdev.ops.create_qp)
431 			if (!rdi->driver_f.qp_priv_alloc ||
432 			    !rdi->driver_f.qp_priv_free ||
433 			    !rdi->driver_f.notify_qp_reset ||
434 			    !rdi->driver_f.flush_qp_waiters ||
435 			    !rdi->driver_f.stop_send_queue ||
436 			    !rdi->driver_f.quiesce_qp)
437 				return -EINVAL;
438 		break;
439 
440 	case MODIFY_QP:
441 		if (!rdi->ibdev.ops.modify_qp)
442 			if (!rdi->driver_f.notify_qp_reset ||
443 			    !rdi->driver_f.schedule_send ||
444 			    !rdi->driver_f.get_pmtu_from_attr ||
445 			    !rdi->driver_f.flush_qp_waiters ||
446 			    !rdi->driver_f.stop_send_queue ||
447 			    !rdi->driver_f.quiesce_qp ||
448 			    !rdi->driver_f.notify_error_qp ||
449 			    !rdi->driver_f.mtu_from_qp ||
450 			    !rdi->driver_f.mtu_to_path_mtu)
451 				return -EINVAL;
452 		break;
453 
454 	case DESTROY_QP:
455 		if (!rdi->ibdev.ops.destroy_qp)
456 			if (!rdi->driver_f.qp_priv_free ||
457 			    !rdi->driver_f.notify_qp_reset ||
458 			    !rdi->driver_f.flush_qp_waiters ||
459 			    !rdi->driver_f.stop_send_queue ||
460 			    !rdi->driver_f.quiesce_qp)
461 				return -EINVAL;
462 		break;
463 
464 	case POST_SEND:
465 		if (!rdi->ibdev.ops.post_send)
466 			if (!rdi->driver_f.schedule_send ||
467 			    !rdi->driver_f.do_send ||
468 			    !rdi->post_parms)
469 				return -EINVAL;
470 		break;
471 
472 	}
473 
474 	return 0;
475 }
476 
477 /**
478  * rvt_register_device - register a driver
479  * @rdi: main dev structure for all of rdmavt operations
480  *
481  * It is up to drivers to allocate the rdi and fill in the appropriate
482  * information.
483  *
484  * Return: 0 on success otherwise an errno.
485  */
486 int rvt_register_device(struct rvt_dev_info *rdi)
487 {
488 	int ret = 0, i;
489 
490 	if (!rdi)
491 		return -EINVAL;
492 
493 	/*
494 	 * Check to ensure drivers have setup the required helpers for the verbs
495 	 * they want rdmavt to handle
496 	 */
497 	for (i = 0; i < _VERB_IDX_MAX; i++)
498 		if (check_support(rdi, i)) {
499 			pr_err("Driver support req not met at %d\n", i);
500 			return -EINVAL;
501 		}
502 
503 	ib_set_device_ops(&rdi->ibdev, &rvt_dev_ops);
504 
505 	/* Once we get past here we can use rvt_pr macros and tracepoints */
506 	trace_rvt_dbg(rdi, "Driver attempting registration");
507 	rvt_mmap_init(rdi);
508 
509 	/* Queue Pairs */
510 	ret = rvt_driver_qp_init(rdi);
511 	if (ret) {
512 		pr_err("Error in driver QP init.\n");
513 		return -EINVAL;
514 	}
515 
516 	/* Address Handle */
517 	spin_lock_init(&rdi->n_ahs_lock);
518 	rdi->n_ahs_allocated = 0;
519 
520 	/* Shared Receive Queue */
521 	rvt_driver_srq_init(rdi);
522 
523 	/* Multicast */
524 	rvt_driver_mcast_init(rdi);
525 
526 	/* Mem Region */
527 	ret = rvt_driver_mr_init(rdi);
528 	if (ret) {
529 		pr_err("Error in driver MR init.\n");
530 		goto bail_no_mr;
531 	}
532 
533 	/* Memory Working Set Size */
534 	ret = rvt_wss_init(rdi);
535 	if (ret) {
536 		rvt_pr_err(rdi, "Error in WSS init.\n");
537 		goto bail_mr;
538 	}
539 
540 	/* Completion queues */
541 	spin_lock_init(&rdi->n_cqs_lock);
542 
543 	/* Protection Domain */
544 	spin_lock_init(&rdi->n_pds_lock);
545 	rdi->n_pds_allocated = 0;
546 
547 	/*
548 	 * There are some things which could be set by underlying drivers but
549 	 * really should be up to rdmavt to set. For instance drivers can't know
550 	 * exactly which functions rdmavt supports, nor do they know the ABI
551 	 * version, so we do all of this sort of stuff here.
552 	 */
553 	rdi->ibdev.uverbs_cmd_mask |=
554 		(1ull << IB_USER_VERBS_CMD_POLL_CQ)             |
555 		(1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ)       |
556 		(1ull << IB_USER_VERBS_CMD_POST_SEND)           |
557 		(1ull << IB_USER_VERBS_CMD_POST_RECV)           |
558 		(1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
559 	rdi->ibdev.node_type = RDMA_NODE_IB_CA;
560 	if (!rdi->ibdev.num_comp_vectors)
561 		rdi->ibdev.num_comp_vectors = 1;
562 
563 	/* We are now good to announce we exist */
564 	ret = ib_register_device(&rdi->ibdev, dev_name(&rdi->ibdev.dev), NULL);
565 	if (ret) {
566 		rvt_pr_err(rdi, "Failed to register driver with ib core.\n");
567 		goto bail_wss;
568 	}
569 
570 	rvt_create_mad_agents(rdi);
571 
572 	rvt_pr_info(rdi, "Registration with rdmavt done.\n");
573 	return ret;
574 
575 bail_wss:
576 	rvt_wss_exit(rdi);
577 bail_mr:
578 	rvt_mr_exit(rdi);
579 
580 bail_no_mr:
581 	rvt_qp_exit(rdi);
582 
583 	return ret;
584 }
585 EXPORT_SYMBOL(rvt_register_device);
586 
587 /**
588  * rvt_unregister_device - remove a driver
589  * @rdi: rvt dev struct
590  */
591 void rvt_unregister_device(struct rvt_dev_info *rdi)
592 {
593 	trace_rvt_dbg(rdi, "Driver is unregistering.");
594 	if (!rdi)
595 		return;
596 
597 	rvt_free_mad_agents(rdi);
598 
599 	ib_unregister_device(&rdi->ibdev);
600 	rvt_wss_exit(rdi);
601 	rvt_mr_exit(rdi);
602 	rvt_qp_exit(rdi);
603 }
604 EXPORT_SYMBOL(rvt_unregister_device);
605 
606 /**
607  * rvt_init_port - init internal data for driver port
608  * @rdi: rvt_dev_info struct
609  * @port: rvt port
610  * @port_index: 0 based index of ports, different from IB core port num
611  * @pkey_table: pkey_table for @port
612  *
613  * Keep track of a list of ports. No need to have a detach port.
614  * They persist until the driver goes away.
615  *
616  * Return: always 0
617  */
618 int rvt_init_port(struct rvt_dev_info *rdi, struct rvt_ibport *port,
619 		  int port_index, u16 *pkey_table)
620 {
621 
622 	rdi->ports[port_index] = port;
623 	rdi->ports[port_index]->pkey_table = pkey_table;
624 
625 	return 0;
626 }
627 EXPORT_SYMBOL(rvt_init_port);
628