xref: /linux/fs/smb/client/smbdirect.h (revision 522cd6acd250dea76afaabc52e028fef280fd753)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *   Copyright (C) 2017, Microsoft Corporation.
4  *
5  *   Author(s): Long Li <longli@microsoft.com>
6  */
7 #ifndef _SMBDIRECT_H
8 #define _SMBDIRECT_H
9 
10 #ifdef CONFIG_CIFS_SMB_DIRECT
11 #define cifs_rdma_enabled(server)	((server)->rdma)
12 
13 #include "cifsglob.h"
14 #include <rdma/ib_verbs.h>
15 #include <rdma/rdma_cm.h>
16 #include <linux/mempool.h>
17 
18 #include "../common/smbdirect/smbdirect.h"
19 #include "../common/smbdirect/smbdirect_socket.h"
20 
21 extern int rdma_readwrite_threshold;
22 extern int smbd_max_frmr_depth;
23 extern int smbd_keep_alive_interval;
24 extern int smbd_max_receive_size;
25 extern int smbd_max_fragmented_recv_size;
26 extern int smbd_max_send_size;
27 extern int smbd_send_credit_target;
28 extern int smbd_receive_credit_max;
29 
30 enum keep_alive_status {
31 	KEEP_ALIVE_NONE,
32 	KEEP_ALIVE_PENDING,
33 	KEEP_ALIVE_SENT,
34 };
35 
36 enum smbd_connection_status {
37 	SMBD_CREATED,
38 	SMBD_CONNECTING,
39 	SMBD_CONNECTED,
40 	SMBD_NEGOTIATE_FAILED,
41 	SMBD_DISCONNECTING,
42 	SMBD_DISCONNECTED,
43 	SMBD_DESTROYED
44 };
45 
46 /*
47  * The context for the SMBDirect transport
48  * Everything related to the transport is here. It has several logical parts
49  * 1. RDMA related structures
50  * 2. SMBDirect connection parameters
51  * 3. Memory registrations
52  * 4. Receive and reassembly queues for data receive path
53  * 5. mempools for allocating packets
54  */
55 struct smbd_connection {
56 	struct smbdirect_socket socket;
57 
58 	int ri_rc;
59 	struct completion ri_done;
60 	wait_queue_head_t conn_wait;
61 	wait_queue_head_t disconn_wait;
62 
63 	struct completion negotiate_completion;
64 	bool negotiate_done;
65 
66 	struct work_struct disconnect_work;
67 	struct work_struct post_send_credits_work;
68 
69 	spinlock_t lock_new_credits_offered;
70 	int new_credits_offered;
71 
72 	/* dynamic connection parameters defined in [MS-SMBD] 3.1.1.1 */
73 	enum keep_alive_status keep_alive_requested;
74 	int protocol;
75 	atomic_t send_credits;
76 	atomic_t receive_credits;
77 	int receive_credit_target;
78 	int fragment_reassembly_remaining;
79 
80 	/* Memory registrations */
81 	/* Maximum number of RDMA read/write outstanding on this connection */
82 	int responder_resources;
83 	/* Maximum number of pages in a single RDMA write/read on this connection */
84 	int max_frmr_depth;
85 	/*
86 	 * If payload is less than or equal to the threshold,
87 	 * use RDMA send/recv to send upper layer I/O.
88 	 * If payload is more than the threshold,
89 	 * use RDMA read/write through memory registration for I/O.
90 	 */
91 	int rdma_readwrite_threshold;
92 	enum ib_mr_type mr_type;
93 	struct list_head mr_list;
94 	spinlock_t mr_list_lock;
95 	/* The number of available MRs ready for memory registration */
96 	atomic_t mr_ready_count;
97 	atomic_t mr_used_count;
98 	wait_queue_head_t wait_mr;
99 	struct work_struct mr_recovery_work;
100 	/* Used by transport to wait until all MRs are returned */
101 	wait_queue_head_t wait_for_mr_cleanup;
102 
103 	/* Activity accounting */
104 	atomic_t send_pending;
105 	wait_queue_head_t wait_send_pending;
106 	wait_queue_head_t wait_post_send;
107 
108 	/* Receive queue */
109 	struct list_head receive_queue;
110 	int count_receive_queue;
111 	spinlock_t receive_queue_lock;
112 
113 	struct list_head empty_packet_queue;
114 	int count_empty_packet_queue;
115 	spinlock_t empty_packet_queue_lock;
116 
117 	wait_queue_head_t wait_receive_queues;
118 
119 	/* Reassembly queue */
120 	struct list_head reassembly_queue;
121 	spinlock_t reassembly_queue_lock;
122 	wait_queue_head_t wait_reassembly_queue;
123 
124 	/* total data length of reassembly queue */
125 	int reassembly_data_length;
126 	int reassembly_queue_length;
127 	/* the offset to first buffer in reassembly queue */
128 	int first_entry_offset;
129 
130 	bool send_immediate;
131 
132 	wait_queue_head_t wait_send_queue;
133 
134 	/*
135 	 * Indicate if we have received a full packet on the connection
136 	 * This is used to identify the first SMBD packet of a assembled
137 	 * payload (SMB packet) in reassembly queue so we can return a
138 	 * RFC1002 length to upper layer to indicate the length of the SMB
139 	 * packet received
140 	 */
141 	bool full_packet_received;
142 
143 	struct workqueue_struct *workqueue;
144 	struct delayed_work idle_timer_work;
145 
146 	/* Memory pool for preallocating buffers */
147 	/* request pool for RDMA send */
148 	struct kmem_cache *request_cache;
149 	mempool_t *request_mempool;
150 
151 	/* response pool for RDMA receive */
152 	struct kmem_cache *response_cache;
153 	mempool_t *response_mempool;
154 
155 	/* for debug purposes */
156 	unsigned int count_get_receive_buffer;
157 	unsigned int count_put_receive_buffer;
158 	unsigned int count_reassembly_queue;
159 	unsigned int count_enqueue_reassembly_queue;
160 	unsigned int count_dequeue_reassembly_queue;
161 	unsigned int count_send_empty;
162 };
163 
164 enum smbd_message_type {
165 	SMBD_NEGOTIATE_RESP,
166 	SMBD_TRANSFER_DATA,
167 };
168 
169 /* Maximum number of SGEs used by smbdirect.c in any send work request */
170 #define SMBDIRECT_MAX_SEND_SGE	6
171 
172 /* The context for a SMBD request */
173 struct smbd_request {
174 	struct smbd_connection *info;
175 	struct ib_cqe cqe;
176 
177 	/* the SGE entries for this work request */
178 	struct ib_sge sge[SMBDIRECT_MAX_SEND_SGE];
179 	int num_sge;
180 
181 	/* SMBD packet header follows this structure */
182 	u8 packet[];
183 };
184 
185 /* Maximum number of SGEs used by smbdirect.c in any receive work request */
186 #define SMBDIRECT_MAX_RECV_SGE	1
187 
188 /* The context for a SMBD response */
189 struct smbd_response {
190 	struct smbd_connection *info;
191 	struct ib_cqe cqe;
192 	struct ib_sge sge;
193 
194 	enum smbd_message_type type;
195 
196 	/* Link to receive queue or reassembly queue */
197 	struct list_head list;
198 
199 	/* Indicate if this is the 1st packet of a payload */
200 	bool first_segment;
201 
202 	/* SMBD packet header and payload follows this structure */
203 	u8 packet[];
204 };
205 
206 /* Create a SMBDirect session */
207 struct smbd_connection *smbd_get_connection(
208 	struct TCP_Server_Info *server, struct sockaddr *dstaddr);
209 
210 /* Reconnect SMBDirect session */
211 int smbd_reconnect(struct TCP_Server_Info *server);
212 /* Destroy SMBDirect session */
213 void smbd_destroy(struct TCP_Server_Info *server);
214 
215 /* Interface for carrying upper layer I/O through send/recv */
216 int smbd_recv(struct smbd_connection *info, struct msghdr *msg);
217 int smbd_send(struct TCP_Server_Info *server,
218 	int num_rqst, struct smb_rqst *rqst);
219 
220 enum mr_state {
221 	MR_READY,
222 	MR_REGISTERED,
223 	MR_INVALIDATED,
224 	MR_ERROR
225 };
226 
227 struct smbd_mr {
228 	struct smbd_connection	*conn;
229 	struct list_head	list;
230 	enum mr_state		state;
231 	struct ib_mr		*mr;
232 	struct sg_table		sgt;
233 	enum dma_data_direction	dir;
234 	union {
235 		struct ib_reg_wr	wr;
236 		struct ib_send_wr	inv_wr;
237 	};
238 	struct ib_cqe		cqe;
239 	bool			need_invalidate;
240 	struct completion	invalidate_done;
241 };
242 
243 /* Interfaces to register and deregister MR for RDMA read/write */
244 struct smbd_mr *smbd_register_mr(
245 	struct smbd_connection *info, struct iov_iter *iter,
246 	bool writing, bool need_invalidate);
247 int smbd_deregister_mr(struct smbd_mr *mr);
248 
249 #else
250 #define cifs_rdma_enabled(server)	0
251 struct smbd_connection {};
smbd_get_connection(struct TCP_Server_Info * server,struct sockaddr * dstaddr)252 static inline void *smbd_get_connection(
253 	struct TCP_Server_Info *server, struct sockaddr *dstaddr) {return NULL;}
smbd_reconnect(struct TCP_Server_Info * server)254 static inline int smbd_reconnect(struct TCP_Server_Info *server) {return -1; }
smbd_destroy(struct TCP_Server_Info * server)255 static inline void smbd_destroy(struct TCP_Server_Info *server) {}
smbd_recv(struct smbd_connection * info,struct msghdr * msg)256 static inline int smbd_recv(struct smbd_connection *info, struct msghdr *msg) {return -1; }
smbd_send(struct TCP_Server_Info * server,int num_rqst,struct smb_rqst * rqst)257 static inline int smbd_send(struct TCP_Server_Info *server, int num_rqst, struct smb_rqst *rqst) {return -1; }
258 #endif
259 
260 #endif
261