1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * RDMA Transport Layer
4  *
5  * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved.
6  * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved.
7  * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved.
8  */
9 
10 #ifndef RTRS_SRV_H
11 #define RTRS_SRV_H
12 
13 #include <linux/device.h>
14 #include <linux/refcount.h>
15 #include "rtrs-pri.h"
16 
17 /*
18  * enum rtrs_srv_state - Server states.
19  */
20 enum rtrs_srv_state {
21 	RTRS_SRV_CONNECTING,
22 	RTRS_SRV_CONNECTED,
23 	RTRS_SRV_CLOSING,
24 	RTRS_SRV_CLOSED,
25 };
26 
27 /* stats for Read and write operation.
28  * see Documentation/ABI/testing/sysfs-class-rtrs-server for details
29  */
30 struct rtrs_srv_stats_rdma_stats {
31 	struct {
32 		atomic64_t	cnt;
33 		atomic64_t	size_total;
34 	} dir[2];
35 };
36 
37 struct rtrs_srv_stats {
38 	struct kobject				kobj_stats;
39 	struct rtrs_srv_stats_rdma_stats	rdma_stats;
40 	struct rtrs_srv_sess			*sess;
41 };
42 
43 struct rtrs_srv_con {
44 	struct rtrs_con		c;
45 	atomic_t		wr_cnt;
46 	atomic_t		sq_wr_avail;
47 	struct list_head	rsp_wr_wait_list;
48 	spinlock_t		rsp_wr_wait_lock;
49 };
50 
51 /* IO context in rtrs_srv, each io has one */
52 struct rtrs_srv_op {
53 	struct rtrs_srv_con		*con;
54 	u32				msg_id;
55 	u8				dir;
56 	struct rtrs_msg_rdma_read	*rd_msg;
57 	struct ib_rdma_wr		tx_wr;
58 	struct ib_sge			tx_sg;
59 	struct list_head		wait_list;
60 	int				status;
61 };
62 
63 /*
64  * server side memory region context, when always_invalidate=Y, we need
65  * queue_depth of memory region to invalidate each memory region.
66  */
67 struct rtrs_srv_mr {
68 	struct ib_mr	*mr;
69 	struct sg_table	sgt;
70 	struct ib_cqe	inv_cqe;	/* only for always_invalidate=true */
71 	u32		msg_id;		/* only for always_invalidate=true */
72 	u32		msg_off;	/* only for always_invalidate=true */
73 	struct rtrs_iu	*iu;		/* send buffer for new rkey msg */
74 };
75 
76 struct rtrs_srv_sess {
77 	struct rtrs_sess	s;
78 	struct rtrs_srv	*srv;
79 	struct work_struct	close_work;
80 	enum rtrs_srv_state	state;
81 	spinlock_t		state_lock;
82 	int			cur_cq_vector;
83 	struct rtrs_srv_op	**ops_ids;
84 	atomic_t		ids_inflight;
85 	wait_queue_head_t	ids_waitq;
86 	struct rtrs_srv_mr	*mrs;
87 	unsigned int		mrs_num;
88 	dma_addr_t		*dma_addr;
89 	bool			established;
90 	unsigned int		mem_bits;
91 	struct kobject		kobj;
92 	struct rtrs_srv_stats	*stats;
93 };
94 
95 struct rtrs_srv {
96 	struct list_head	paths_list;
97 	int			paths_up;
98 	struct mutex		paths_ev_mutex;
99 	size_t			paths_num;
100 	struct mutex		paths_mutex;
101 	uuid_t			paths_uuid;
102 	refcount_t		refcount;
103 	struct rtrs_srv_ctx	*ctx;
104 	struct list_head	ctx_list;
105 	void			*priv;
106 	size_t			queue_depth;
107 	struct page		**chunks;
108 	struct device		dev;
109 	unsigned int		dev_ref;
110 	struct kobject		*kobj_paths;
111 };
112 
113 struct rtrs_srv_ctx {
114 	struct rtrs_srv_ops ops;
115 	struct rdma_cm_id *cm_id_ip;
116 	struct rdma_cm_id *cm_id_ib;
117 	struct mutex srv_mutex;
118 	struct list_head srv_list;
119 };
120 
121 struct rtrs_srv_ib_ctx {
122 	struct rtrs_srv_ctx	*srv_ctx;
123 	u16			port;
124 	struct mutex            ib_dev_mutex;
125 	int			ib_dev_count;
126 };
127 
128 extern struct class *rtrs_dev_class;
129 
130 void close_sess(struct rtrs_srv_sess *sess);
131 
132 static inline void rtrs_srv_update_rdma_stats(struct rtrs_srv_stats *s,
133 					      size_t size, int d)
134 {
135 	atomic64_inc(&s->rdma_stats.dir[d].cnt);
136 	atomic64_add(size, &s->rdma_stats.dir[d].size_total);
137 }
138 
139 /* functions which are implemented in rtrs-srv-stats.c */
140 int rtrs_srv_reset_rdma_stats(struct rtrs_srv_stats *stats, bool enable);
141 ssize_t rtrs_srv_stats_rdma_to_str(struct rtrs_srv_stats *stats,
142 				    char *page, size_t len);
143 int rtrs_srv_reset_wc_completion_stats(struct rtrs_srv_stats *stats,
144 					bool enable);
145 int rtrs_srv_stats_wc_completion_to_str(struct rtrs_srv_stats *stats, char *buf,
146 					 size_t len);
147 int rtrs_srv_reset_all_stats(struct rtrs_srv_stats *stats, bool enable);
148 ssize_t rtrs_srv_reset_all_help(struct rtrs_srv_stats *stats,
149 				 char *page, size_t len);
150 
151 /* functions which are implemented in rtrs-srv-sysfs.c */
152 int rtrs_srv_create_sess_files(struct rtrs_srv_sess *sess);
153 void rtrs_srv_destroy_sess_files(struct rtrs_srv_sess *sess);
154 
155 #endif /* RTRS_SRV_H */
156