xref: /freebsd/sys/ofed/include/rdma/rdma_vt.h (revision 1b10e191f341111fad7be32ead11484dfd09b800)
1 #ifndef DEF_RDMA_VT_H
2 #define DEF_RDMA_VT_H
3 
4 /*-
5  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
6  *
7  * Copyright(c) 2016 Intel Corporation.
8  *
9  * This file is provided under a dual BSD/GPLv2 license.  When using or
10  * redistributing this file, you may do so under either license.
11  *
12  * GPL LICENSE SUMMARY
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of version 2 of the GNU General Public License as
16  * published by the Free Software Foundation.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * General Public License for more details.
22  *
23  * BSD LICENSE
24  *
25  * Redistribution and use in source and binary forms, with or without
26  * modification, are permitted provided that the following conditions
27  * are met:
28  *
29  *  - Redistributions of source code must retain the above copyright
30  *    notice, this list of conditions and the following disclaimer.
31  *  - Redistributions in binary form must reproduce the above copyright
32  *    notice, this list of conditions and the following disclaimer in
33  *    the documentation and/or other materials provided with the
34  *    distribution.
35  *  - Neither the name of Intel Corporation nor the names of its
36  *    contributors may be used to endorse or promote products derived
37  *    from this software without specific prior written permission.
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
40  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
41  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
42  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
43  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
46  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
47  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
48  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
49  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50  *
51  * $FreeBSD$
52  */
53 
54 /*
55  * Structure that low level drivers will populate in order to register with the
56  * rdmavt layer.
57  */
58 
59 #include <linux/spinlock.h>
60 #include <linux/list.h>
61 #include <linux/hash.h>
62 #include <rdma/ib_verbs.h>
63 #include <rdma/rdmavt_mr.h>
64 #include <rdma/rdmavt_qp.h>
65 
66 #define RVT_MAX_PKEY_VALUES 16
67 
68 struct rvt_ibport {
69 	struct rvt_qp __rcu *qp[2];
70 	struct ib_mad_agent *send_agent;	/* agent for SMI (traps) */
71 	struct rb_root mcast_tree;
72 	spinlock_t lock;		/* protect changes in this struct */
73 
74 	/* non-zero when timer is set */
75 	unsigned long mkey_lease_timeout;
76 	unsigned long trap_timeout;
77 	__be64 gid_prefix;      /* in network order */
78 	__be64 mkey;
79 	u64 tid;
80 	u32 port_cap_flags;
81 	u32 pma_sample_start;
82 	u32 pma_sample_interval;
83 	__be16 pma_counter_select[5];
84 	u16 pma_tag;
85 	u16 mkey_lease_period;
86 	u16 sm_lid;
87 	u8 sm_sl;
88 	u8 mkeyprot;
89 	u8 subnet_timeout;
90 	u8 vl_high_limit;
91 
92 	/*
93 	 * Driver is expected to keep these up to date. These
94 	 * counters are informational only and not required to be
95 	 * completely accurate.
96 	 */
97 	u64 n_rc_resends;
98 	u64 n_seq_naks;
99 	u64 n_rdma_seq;
100 	u64 n_rnr_naks;
101 	u64 n_other_naks;
102 	u64 n_loop_pkts;
103 	u64 n_pkt_drops;
104 	u64 n_vl15_dropped;
105 	u64 n_rc_timeouts;
106 	u64 n_dmawait;
107 	u64 n_unaligned;
108 	u64 n_rc_dupreq;
109 	u64 n_rc_seqnak;
110 	u16 pkey_violations;
111 	u16 qkey_violations;
112 	u16 mkey_violations;
113 
114 	/* Hot-path per CPU counters to avoid cacheline trading to update */
115 	u64 z_rc_acks;
116 	u64 z_rc_qacks;
117 	u64 z_rc_delayed_comp;
118 	u64 __percpu *rc_acks;
119 	u64 __percpu *rc_qacks;
120 	u64 __percpu *rc_delayed_comp;
121 
122 	void *priv; /* driver private data */
123 
124 	/*
125 	 * The pkey table is allocated and maintained by the driver. Drivers
126 	 * need to have access to this before registering with rdmav. However
127 	 * rdmavt will need access to it so drivers need to proviee this during
128 	 * the attach port API call.
129 	 */
130 	u16 *pkey_table;
131 
132 	struct rvt_ah *sm_ah;
133 };
134 
135 #define RVT_CQN_MAX 16 /* maximum length of cq name */
136 
137 /*
138  * Things that are driver specific, module parameters in hfi1 and qib
139  */
140 struct rvt_driver_params {
141 	struct ib_device_attr props;
142 
143 	/*
144 	 * Anything driver specific that is not covered by props
145 	 * For instance special module parameters. Goes here.
146 	 */
147 	unsigned int lkey_table_size;
148 	unsigned int qp_table_size;
149 	int qpn_start;
150 	int qpn_inc;
151 	int qpn_res_start;
152 	int qpn_res_end;
153 	int nports;
154 	int npkeys;
155 	char cq_name[RVT_CQN_MAX];
156 	int node;
157 	int psn_mask;
158 	int psn_shift;
159 	int psn_modify_mask;
160 	u32 core_cap_flags;
161 	u32 max_mad_size;
162 	u8 qos_shift;
163 	u8 max_rdma_atomic;
164 	u8 reserved_operations;
165 };
166 
167 /* Protection domain */
168 struct rvt_pd {
169 	struct ib_pd ibpd;
170 	int user;               /* non-zero if created from user space */
171 };
172 
173 /* Address handle */
174 struct rvt_ah {
175 	struct ib_ah ibah;
176 	struct ib_ah_attr attr;
177 	atomic_t refcount;
178 	u8 vl;
179 	u8 log_pmtu;
180 };
181 
182 struct rvt_dev_info;
183 struct rvt_swqe;
184 struct rvt_driver_provided {
185 	/*
186 	 * Which functions are required depends on which verbs rdmavt is
187 	 * providing and which verbs the driver is overriding. See
188 	 * check_support() for details.
189 	 */
190 
191 	/* Passed to ib core registration. Callback to create syfs files */
192 	int (*port_callback)(struct ib_device *, u8, struct kobject *);
193 
194 	/*
195 	 * Returns a string to represent the device for which is being
196 	 * registered. This is primarily used for error and debug messages on
197 	 * the console.
198 	 */
199 	const char * (*get_card_name)(struct rvt_dev_info *rdi);
200 
201 	/*
202 	 * Returns a pointer to the undelying hardware's PCI device. This is
203 	 * used to display information as to what hardware is being referenced
204 	 * in an output message
205 	 */
206 	struct pci_dev * (*get_pci_dev)(struct rvt_dev_info *rdi);
207 
208 	/*
209 	 * Allocate a private queue pair data structure for driver specific
210 	 * information which is opaque to rdmavt.  Errors are returned via
211 	 * ERR_PTR(err).  The driver is free to return NULL or a valid
212 	 * pointer.
213 	 */
214 	void * (*qp_priv_alloc)(struct rvt_dev_info *rdi, struct rvt_qp *qp,
215 				gfp_t gfp);
216 
217 	/*
218 	 * Free the driver's private qp structure.
219 	 */
220 	void (*qp_priv_free)(struct rvt_dev_info *rdi, struct rvt_qp *qp);
221 
222 	/*
223 	 * Inform the driver the particular qp in quesiton has been reset so
224 	 * that it can clean up anything it needs to.
225 	 */
226 	void (*notify_qp_reset)(struct rvt_qp *qp);
227 
228 	/*
229 	 * Give the driver a notice that there is send work to do. It is up to
230 	 * the driver to generally push the packets out, this just queues the
231 	 * work with the driver. There are two variants here. The no_lock
232 	 * version requires the s_lock not to be held. The other assumes the
233 	 * s_lock is held.
234 	 */
235 	void (*schedule_send)(struct rvt_qp *qp);
236 	void (*schedule_send_no_lock)(struct rvt_qp *qp);
237 
238 	/*
239 	 * Sometimes rdmavt needs to kick the driver's send progress. That is
240 	 * done by this call back.
241 	 */
242 	void (*do_send)(struct rvt_qp *qp);
243 
244 	/*
245 	 * Get a path mtu from the driver based on qp attributes.
246 	 */
247 	int (*get_pmtu_from_attr)(struct rvt_dev_info *rdi, struct rvt_qp *qp,
248 				  struct ib_qp_attr *attr);
249 
250 	/*
251 	 * Notify driver that it needs to flush any outstanding IO requests that
252 	 * are waiting on a qp.
253 	 */
254 	void (*flush_qp_waiters)(struct rvt_qp *qp);
255 
256 	/*
257 	 * Notify driver to stop its queue of sending packets. Nothing else
258 	 * should be posted to the queue pair after this has been called.
259 	 */
260 	void (*stop_send_queue)(struct rvt_qp *qp);
261 
262 	/*
263 	 * Have the drivr drain any in progress operations
264 	 */
265 	void (*quiesce_qp)(struct rvt_qp *qp);
266 
267 	/*
268 	 * Inform the driver a qp has went to error state.
269 	 */
270 	void (*notify_error_qp)(struct rvt_qp *qp);
271 
272 	/*
273 	 * Get an MTU for a qp.
274 	 */
275 	u32 (*mtu_from_qp)(struct rvt_dev_info *rdi, struct rvt_qp *qp,
276 			   u32 pmtu);
277 	/*
278 	 * Convert an mtu to a path mtu
279 	 */
280 	int (*mtu_to_path_mtu)(u32 mtu);
281 
282 	/*
283 	 * Get the guid of a port in big endian byte order
284 	 */
285 	int (*get_guid_be)(struct rvt_dev_info *rdi, struct rvt_ibport *rvp,
286 			   int guid_index, __be64 *guid);
287 
288 	/*
289 	 * Query driver for the state of the port.
290 	 */
291 	int (*query_port_state)(struct rvt_dev_info *rdi, u8 port_num,
292 				struct ib_port_attr *props);
293 
294 	/*
295 	 * Tell driver to shutdown a port
296 	 */
297 	int (*shut_down_port)(struct rvt_dev_info *rdi, u8 port_num);
298 
299 	/* Tell driver to send a trap for changed  port capabilities */
300 	void (*cap_mask_chg)(struct rvt_dev_info *rdi, u8 port_num);
301 
302 	/*
303 	 * The following functions can be safely ignored completely. Any use of
304 	 * these is checked for NULL before blindly calling. Rdmavt should also
305 	 * be functional if drivers omit these.
306 	 */
307 
308 	/* Called to inform the driver that all qps should now be freed. */
309 	unsigned (*free_all_qps)(struct rvt_dev_info *rdi);
310 
311 	/* Driver specific AH validation */
312 	int (*check_ah)(struct ib_device *, struct ib_ah_attr *);
313 
314 	/* Inform the driver a new AH has been created */
315 	void (*notify_new_ah)(struct ib_device *, struct ib_ah_attr *,
316 			      struct rvt_ah *);
317 
318 	/* Let the driver pick the next queue pair number*/
319 	int (*alloc_qpn)(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
320 			 enum ib_qp_type type, u8 port_num, gfp_t gfp);
321 
322 	/* Determine if its safe or allowed to modify the qp */
323 	int (*check_modify_qp)(struct rvt_qp *qp, struct ib_qp_attr *attr,
324 			       int attr_mask, struct ib_udata *udata);
325 
326 	/* Driver specific QP modification/notification-of */
327 	void (*modify_qp)(struct rvt_qp *qp, struct ib_qp_attr *attr,
328 			  int attr_mask, struct ib_udata *udata);
329 
330 	/* Driver specific work request checking */
331 	int (*check_send_wqe)(struct rvt_qp *qp, struct rvt_swqe *wqe);
332 
333 	/* Notify driver a mad agent has been created */
334 	void (*notify_create_mad_agent)(struct rvt_dev_info *rdi, int port_idx);
335 
336 	/* Notify driver a mad agent has been removed */
337 	void (*notify_free_mad_agent)(struct rvt_dev_info *rdi, int port_idx);
338 
339 };
340 
341 struct rvt_dev_info {
342 	struct ib_device ibdev; /* Keep this first. Nothing above here */
343 
344 	/*
345 	 * Prior to calling for registration the driver will be responsible for
346 	 * allocating space for this structure.
347 	 *
348 	 * The driver will also be responsible for filling in certain members of
349 	 * dparms.props. The driver needs to fill in dparms exactly as it would
350 	 * want values reported to a ULP. This will be returned to the caller
351 	 * in rdmavt's device. The driver should also therefore refrain from
352 	 * modifying this directly after registration with rdmavt.
353 	 */
354 
355 	/* Driver specific properties */
356 	struct rvt_driver_params dparms;
357 
358 	/* post send table */
359 	const struct rvt_operation_params *post_parms;
360 
361 	struct rvt_mregion __rcu *dma_mr;
362 	struct rvt_lkey_table lkey_table;
363 
364 	/* Driver specific helper functions */
365 	struct rvt_driver_provided driver_f;
366 
367 	/* Internal use */
368 	int n_pds_allocated;
369 	spinlock_t n_pds_lock; /* Protect pd allocated count */
370 
371 	int n_ahs_allocated;
372 	spinlock_t n_ahs_lock; /* Protect ah allocated count */
373 
374 	u32 n_srqs_allocated;
375 	spinlock_t n_srqs_lock; /* Protect srqs allocated count */
376 
377 	int flags;
378 	struct rvt_ibport **ports;
379 
380 	/* QP */
381 	struct rvt_qp_ibdev *qp_dev;
382 	u32 n_qps_allocated;    /* number of QPs allocated for device */
383 	u32 n_rc_qps;		/* number of RC QPs allocated for device */
384 	u32 busy_jiffies;	/* timeout scaling based on RC QP count */
385 	spinlock_t n_qps_lock;	/* protect qps, rc qps and busy jiffy counts */
386 
387 	/* memory maps */
388 	struct list_head pending_mmaps;
389 	spinlock_t mmap_offset_lock; /* protect mmap_offset */
390 	u32 mmap_offset;
391 	spinlock_t pending_lock; /* protect pending mmap list */
392 
393 	/* CQ */
394 	struct kthread_worker *worker; /* per device cq worker */
395 	u32 n_cqs_allocated;    /* number of CQs allocated for device */
396 	spinlock_t n_cqs_lock; /* protect count of in use cqs */
397 
398 	/* Multicast */
399 	u32 n_mcast_grps_allocated; /* number of mcast groups allocated */
400 	spinlock_t n_mcast_grps_lock;
401 
402 };
403 
404 static inline struct rvt_pd *ibpd_to_rvtpd(struct ib_pd *ibpd)
405 {
406 	return container_of(ibpd, struct rvt_pd, ibpd);
407 }
408 
409 static inline struct rvt_ah *ibah_to_rvtah(struct ib_ah *ibah)
410 {
411 	return container_of(ibah, struct rvt_ah, ibah);
412 }
413 
414 static inline struct rvt_dev_info *ib_to_rvt(struct ib_device *ibdev)
415 {
416 	return  container_of(ibdev, struct rvt_dev_info, ibdev);
417 }
418 
419 static inline struct rvt_srq *ibsrq_to_rvtsrq(struct ib_srq *ibsrq)
420 {
421 	return container_of(ibsrq, struct rvt_srq, ibsrq);
422 }
423 
424 static inline struct rvt_qp *ibqp_to_rvtqp(struct ib_qp *ibqp)
425 {
426 	return container_of(ibqp, struct rvt_qp, ibqp);
427 }
428 
429 static inline unsigned rvt_get_npkeys(struct rvt_dev_info *rdi)
430 {
431 	/*
432 	 * All ports have same number of pkeys.
433 	 */
434 	return rdi->dparms.npkeys;
435 }
436 
437 /*
438  * Return the max atomic suitable for determining
439  * the size of the ack ring buffer in a QP.
440  */
441 static inline unsigned int rvt_max_atomic(struct rvt_dev_info *rdi)
442 {
443 	return rdi->dparms.max_rdma_atomic + 1;
444 }
445 
446 /*
447  * Return the indexed PKEY from the port PKEY table.
448  */
449 static inline u16 rvt_get_pkey(struct rvt_dev_info *rdi,
450 			       int port_index,
451 			       unsigned index)
452 {
453 	if (index >= rvt_get_npkeys(rdi))
454 		return 0;
455 	else
456 		return rdi->ports[port_index]->pkey_table[index];
457 }
458 
459 /**
460  * rvt_lookup_qpn - return the QP with the given QPN
461  * @ibp: the ibport
462  * @qpn: the QP number to look up
463  *
464  * The caller must hold the rcu_read_lock(), and keep the lock until
465  * the returned qp is no longer in use.
466  */
467 /* TODO: Remove this and put in rdmavt/qp.h when no longer needed by drivers */
468 static inline struct rvt_qp *rvt_lookup_qpn(struct rvt_dev_info *rdi,
469 					    struct rvt_ibport *rvp,
470 					    u32 qpn) __must_hold(RCU)
471 {
472 	struct rvt_qp *qp = NULL;
473 
474 	if (unlikely(qpn <= 1)) {
475 		qp = rcu_dereference(rvp->qp[qpn]);
476 	} else {
477 		u32 n = hash_32(qpn, rdi->qp_dev->qp_table_bits);
478 
479 		for (qp = rcu_dereference(rdi->qp_dev->qp_table[n]); qp;
480 			qp = rcu_dereference(qp->next))
481 			if (qp->ibqp.qp_num == qpn)
482 				break;
483 	}
484 	return qp;
485 }
486 
487 struct rvt_dev_info *rvt_alloc_device(size_t size, int nports);
488 void rvt_dealloc_device(struct rvt_dev_info *rdi);
489 int rvt_register_device(struct rvt_dev_info *rvd);
490 void rvt_unregister_device(struct rvt_dev_info *rvd);
491 int rvt_check_ah(struct ib_device *ibdev, struct ib_ah_attr *ah_attr);
492 int rvt_init_port(struct rvt_dev_info *rdi, struct rvt_ibport *port,
493 		  int port_index, u16 *pkey_table);
494 int rvt_fast_reg_mr(struct rvt_qp *qp, struct ib_mr *ibmr, u32 key,
495 		    int access);
496 int rvt_invalidate_rkey(struct rvt_qp *qp, u32 rkey);
497 int rvt_rkey_ok(struct rvt_qp *qp, struct rvt_sge *sge,
498 		u32 len, u64 vaddr, u32 rkey, int acc);
499 int rvt_lkey_ok(struct rvt_lkey_table *rkt, struct rvt_pd *pd,
500 		struct rvt_sge *isge, struct ib_sge *sge, int acc);
501 struct rvt_mcast *rvt_mcast_find(struct rvt_ibport *ibp, union ib_gid *mgid);
502 
503 #endif          /* DEF_RDMA_VT_H */
504