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