1 /*
2 * Copyright (C) 2017, Microsoft Corporation.
3 *
4 * Author(s): Long Li <longli@microsoft.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 */
16 #ifndef _SMBDIRECT_H
17 #define _SMBDIRECT_H
18
19 #ifdef CONFIG_CIFS_SMB_DIRECT
20 #define cifs_rdma_enabled(server) ((server)->rdma)
21
22 #include "cifsglob.h"
23 #include <rdma/ib_verbs.h>
24 #include <rdma/rdma_cm.h>
25 #include <linux/mempool.h>
26
27 extern int rdma_readwrite_threshold;
28 extern int smbd_max_frmr_depth;
29 extern int smbd_keep_alive_interval;
30 extern int smbd_max_receive_size;
31 extern int smbd_max_fragmented_recv_size;
32 extern int smbd_max_send_size;
33 extern int smbd_send_credit_target;
34 extern int smbd_receive_credit_max;
35
36 enum keep_alive_status {
37 KEEP_ALIVE_NONE,
38 KEEP_ALIVE_PENDING,
39 KEEP_ALIVE_SENT,
40 };
41
42 enum smbd_connection_status {
43 SMBD_CREATED,
44 SMBD_CONNECTING,
45 SMBD_CONNECTED,
46 SMBD_NEGOTIATE_FAILED,
47 SMBD_DISCONNECTING,
48 SMBD_DISCONNECTED,
49 SMBD_DESTROYED
50 };
51
52 /*
53 * The context for the SMBDirect transport
54 * Everything related to the transport is here. It has several logical parts
55 * 1. RDMA related structures
56 * 2. SMBDirect connection parameters
57 * 3. Memory registrations
58 * 4. Receive and reassembly queues for data receive path
59 * 5. mempools for allocating packets
60 */
61 struct smbd_connection {
62 enum smbd_connection_status transport_status;
63
64 /* RDMA related */
65 struct rdma_cm_id *id;
66 struct ib_qp_init_attr qp_attr;
67 struct ib_pd *pd;
68 struct ib_cq *send_cq, *recv_cq;
69 struct ib_device_attr dev_attr;
70 int ri_rc;
71 struct completion ri_done;
72 wait_queue_head_t conn_wait;
73 wait_queue_head_t wait_destroy;
74 wait_queue_head_t disconn_wait;
75
76 struct completion negotiate_completion;
77 bool negotiate_done;
78
79 struct work_struct destroy_work;
80 struct work_struct disconnect_work;
81 struct work_struct recv_done_work;
82 struct work_struct post_send_credits_work;
83
84 spinlock_t lock_new_credits_offered;
85 int new_credits_offered;
86
87 /* Connection parameters defined in [MS-SMBD] 3.1.1.1 */
88 int receive_credit_max;
89 int send_credit_target;
90 int max_send_size;
91 int max_fragmented_recv_size;
92 int max_fragmented_send_size;
93 int max_receive_size;
94 int keep_alive_interval;
95 int max_readwrite_size;
96 enum keep_alive_status keep_alive_requested;
97 int protocol;
98 atomic_t send_credits;
99 atomic_t receive_credits;
100 int receive_credit_target;
101 int fragment_reassembly_remaining;
102
103 /* Memory registrations */
104 /* Maximum number of RDMA read/write outstanding on this connection */
105 int responder_resources;
106 /* Maximum number of SGEs in a RDMA write/read */
107 int max_frmr_depth;
108 /*
109 * If payload is less than or equal to the threshold,
110 * use RDMA send/recv to send upper layer I/O.
111 * If payload is more than the threshold,
112 * use RDMA read/write through memory registration for I/O.
113 */
114 int rdma_readwrite_threshold;
115 enum ib_mr_type mr_type;
116 struct list_head mr_list;
117 spinlock_t mr_list_lock;
118 /* The number of available MRs ready for memory registration */
119 atomic_t mr_ready_count;
120 atomic_t mr_used_count;
121 wait_queue_head_t wait_mr;
122 struct work_struct mr_recovery_work;
123 /* Used by transport to wait until all MRs are returned */
124 wait_queue_head_t wait_for_mr_cleanup;
125
126 /* Activity accoutning */
127 /* Pending reqeusts issued from upper layer */
128 int smbd_send_pending;
129 wait_queue_head_t wait_smbd_send_pending;
130
131 int smbd_recv_pending;
132 wait_queue_head_t wait_smbd_recv_pending;
133
134 atomic_t send_pending;
135 wait_queue_head_t wait_send_pending;
136 atomic_t send_payload_pending;
137 wait_queue_head_t wait_send_payload_pending;
138
139 /* Receive queue */
140 struct list_head receive_queue;
141 int count_receive_queue;
142 spinlock_t receive_queue_lock;
143
144 struct list_head empty_packet_queue;
145 int count_empty_packet_queue;
146 spinlock_t empty_packet_queue_lock;
147
148 wait_queue_head_t wait_receive_queues;
149
150 /* Reassembly queue */
151 struct list_head reassembly_queue;
152 spinlock_t reassembly_queue_lock;
153 wait_queue_head_t wait_reassembly_queue;
154
155 /* total data length of reassembly queue */
156 int reassembly_data_length;
157 int reassembly_queue_length;
158 /* the offset to first buffer in reassembly queue */
159 int first_entry_offset;
160
161 bool send_immediate;
162
163 wait_queue_head_t wait_send_queue;
164
165 /*
166 * Indicate if we have received a full packet on the connection
167 * This is used to identify the first SMBD packet of a assembled
168 * payload (SMB packet) in reassembly queue so we can return a
169 * RFC1002 length to upper layer to indicate the length of the SMB
170 * packet received
171 */
172 bool full_packet_received;
173
174 struct workqueue_struct *workqueue;
175 struct delayed_work idle_timer_work;
176 struct delayed_work send_immediate_work;
177
178 /* Memory pool for preallocating buffers */
179 /* request pool for RDMA send */
180 struct kmem_cache *request_cache;
181 mempool_t *request_mempool;
182
183 /* response pool for RDMA receive */
184 struct kmem_cache *response_cache;
185 mempool_t *response_mempool;
186
187 /* for debug purposes */
188 unsigned int count_get_receive_buffer;
189 unsigned int count_put_receive_buffer;
190 unsigned int count_reassembly_queue;
191 unsigned int count_enqueue_reassembly_queue;
192 unsigned int count_dequeue_reassembly_queue;
193 unsigned int count_send_empty;
194 };
195
196 enum smbd_message_type {
197 SMBD_NEGOTIATE_RESP,
198 SMBD_TRANSFER_DATA,
199 };
200
201 #define SMB_DIRECT_RESPONSE_REQUESTED 0x0001
202
203 /* SMBD negotiation request packet [MS-SMBD] 2.2.1 */
204 struct smbd_negotiate_req {
205 __le16 min_version;
206 __le16 max_version;
207 __le16 reserved;
208 __le16 credits_requested;
209 __le32 preferred_send_size;
210 __le32 max_receive_size;
211 __le32 max_fragmented_size;
212 } __packed;
213
214 /* SMBD negotiation response packet [MS-SMBD] 2.2.2 */
215 struct smbd_negotiate_resp {
216 __le16 min_version;
217 __le16 max_version;
218 __le16 negotiated_version;
219 __le16 reserved;
220 __le16 credits_requested;
221 __le16 credits_granted;
222 __le32 status;
223 __le32 max_readwrite_size;
224 __le32 preferred_send_size;
225 __le32 max_receive_size;
226 __le32 max_fragmented_size;
227 } __packed;
228
229 /* SMBD data transfer packet with payload [MS-SMBD] 2.2.3 */
230 struct smbd_data_transfer {
231 __le16 credits_requested;
232 __le16 credits_granted;
233 __le16 flags;
234 __le16 reserved;
235 __le32 remaining_data_length;
236 __le32 data_offset;
237 __le32 data_length;
238 __le32 padding;
239 __u8 buffer[];
240 } __packed;
241
242 /* The packet fields for a registered RDMA buffer */
243 struct smbd_buffer_descriptor_v1 {
244 __le64 offset;
245 __le32 token;
246 __le32 length;
247 } __packed;
248
249 /* Default maximum number of SGEs in a RDMA send/recv */
250 #define SMBDIRECT_MAX_SGE 16
251 /* The context for a SMBD request */
252 struct smbd_request {
253 struct smbd_connection *info;
254 struct ib_cqe cqe;
255
256 /* true if this request carries upper layer payload */
257 bool has_payload;
258
259 /* the SGE entries for this packet */
260 struct ib_sge sge[SMBDIRECT_MAX_SGE];
261 int num_sge;
262
263 /* SMBD packet header follows this structure */
264 u8 packet[];
265 };
266
267 /* The context for a SMBD response */
268 struct smbd_response {
269 struct smbd_connection *info;
270 struct ib_cqe cqe;
271 struct ib_sge sge;
272
273 enum smbd_message_type type;
274
275 /* Link to receive queue or reassembly queue */
276 struct list_head list;
277
278 /* Indicate if this is the 1st packet of a payload */
279 bool first_segment;
280
281 /* SMBD packet header and payload follows this structure */
282 u8 packet[];
283 };
284
285 /* Create a SMBDirect session */
286 struct smbd_connection *smbd_get_connection(
287 struct TCP_Server_Info *server, struct sockaddr *dstaddr);
288
289 /* Reconnect SMBDirect session */
290 int smbd_reconnect(struct TCP_Server_Info *server);
291 /* Destroy SMBDirect session */
292 void smbd_destroy(struct TCP_Server_Info *server);
293
294 /* Interface for carrying upper layer I/O through send/recv */
295 int smbd_recv(struct smbd_connection *info, struct msghdr *msg);
296 int smbd_send(struct TCP_Server_Info *server,
297 int num_rqst, struct smb_rqst *rqst);
298
299 enum mr_state {
300 MR_READY,
301 MR_REGISTERED,
302 MR_INVALIDATED,
303 MR_ERROR
304 };
305
306 struct smbd_mr {
307 struct smbd_connection *conn;
308 struct list_head list;
309 enum mr_state state;
310 struct ib_mr *mr;
311 struct scatterlist *sgl;
312 int sgl_count;
313 enum dma_data_direction dir;
314 union {
315 struct ib_reg_wr wr;
316 struct ib_send_wr inv_wr;
317 };
318 struct ib_cqe cqe;
319 bool need_invalidate;
320 struct completion invalidate_done;
321 };
322
323 /* Interfaces to register and deregister MR for RDMA read/write */
324 struct smbd_mr *smbd_register_mr(
325 struct smbd_connection *info, struct page *pages[], int num_pages,
326 int offset, int tailsz, bool writing, bool need_invalidate);
327 int smbd_deregister_mr(struct smbd_mr *mr);
328
329 #else
330 #define cifs_rdma_enabled(server) 0
331 struct smbd_connection {};
smbd_get_connection(struct TCP_Server_Info * server,struct sockaddr * dstaddr)332 static inline void *smbd_get_connection(
333 struct TCP_Server_Info *server, struct sockaddr *dstaddr) {return NULL;}
smbd_reconnect(struct TCP_Server_Info * server)334 static inline int smbd_reconnect(struct TCP_Server_Info *server) {return -1; }
smbd_destroy(struct TCP_Server_Info * server)335 static inline void smbd_destroy(struct TCP_Server_Info *server) {}
smbd_recv(struct smbd_connection * info,struct msghdr * msg)336 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)337 static inline int smbd_send(struct TCP_Server_Info *server, int num_rqst, struct smb_rqst *rqst) {return -1; }
338 #endif
339
340 #endif
341