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 wait_queue_head_t wait_receive_queues;
114
115 /* Reassembly queue */
116 struct list_head reassembly_queue;
117 spinlock_t reassembly_queue_lock;
118 wait_queue_head_t wait_reassembly_queue;
119
120 /* total data length of reassembly queue */
121 int reassembly_data_length;
122 int reassembly_queue_length;
123 /* the offset to first buffer in reassembly queue */
124 int first_entry_offset;
125
126 bool send_immediate;
127
128 wait_queue_head_t wait_send_queue;
129
130 /*
131 * Indicate if we have received a full packet on the connection
132 * This is used to identify the first SMBD packet of a assembled
133 * payload (SMB packet) in reassembly queue so we can return a
134 * RFC1002 length to upper layer to indicate the length of the SMB
135 * packet received
136 */
137 bool full_packet_received;
138
139 struct workqueue_struct *workqueue;
140 struct delayed_work idle_timer_work;
141
142 /* Memory pool for preallocating buffers */
143 /* request pool for RDMA send */
144 struct kmem_cache *request_cache;
145 mempool_t *request_mempool;
146
147 /* response pool for RDMA receive */
148 struct kmem_cache *response_cache;
149 mempool_t *response_mempool;
150
151 /* for debug purposes */
152 unsigned int count_get_receive_buffer;
153 unsigned int count_put_receive_buffer;
154 unsigned int count_reassembly_queue;
155 unsigned int count_enqueue_reassembly_queue;
156 unsigned int count_dequeue_reassembly_queue;
157 unsigned int count_send_empty;
158 };
159
160 enum smbd_message_type {
161 SMBD_NEGOTIATE_RESP,
162 SMBD_TRANSFER_DATA,
163 };
164
165 /* The packet fields for a registered RDMA buffer */
166 struct smbd_buffer_descriptor_v1 {
167 __le64 offset;
168 __le32 token;
169 __le32 length;
170 } __packed;
171
172 /* Maximum number of SGEs used by smbdirect.c in any send work request */
173 #define SMBDIRECT_MAX_SEND_SGE 6
174
175 /* The context for a SMBD request */
176 struct smbd_request {
177 struct smbd_connection *info;
178 struct ib_cqe cqe;
179
180 /* the SGE entries for this work request */
181 struct ib_sge sge[SMBDIRECT_MAX_SEND_SGE];
182 int num_sge;
183
184 /* SMBD packet header follows this structure */
185 u8 packet[];
186 };
187
188 /* Maximum number of SGEs used by smbdirect.c in any receive work request */
189 #define SMBDIRECT_MAX_RECV_SGE 1
190
191 /* The context for a SMBD response */
192 struct smbd_response {
193 struct smbd_connection *info;
194 struct ib_cqe cqe;
195 struct ib_sge sge;
196
197 enum smbd_message_type type;
198
199 /* Link to receive queue or reassembly queue */
200 struct list_head list;
201
202 /* Indicate if this is the 1st packet of a payload */
203 bool first_segment;
204
205 /* SMBD packet header and payload follows this structure */
206 u8 packet[];
207 };
208
209 /* Create a SMBDirect session */
210 struct smbd_connection *smbd_get_connection(
211 struct TCP_Server_Info *server, struct sockaddr *dstaddr);
212
213 /* Reconnect SMBDirect session */
214 int smbd_reconnect(struct TCP_Server_Info *server);
215 /* Destroy SMBDirect session */
216 void smbd_destroy(struct TCP_Server_Info *server);
217
218 /* Interface for carrying upper layer I/O through send/recv */
219 int smbd_recv(struct smbd_connection *info, struct msghdr *msg);
220 int smbd_send(struct TCP_Server_Info *server,
221 int num_rqst, struct smb_rqst *rqst);
222
223 enum mr_state {
224 MR_READY,
225 MR_REGISTERED,
226 MR_INVALIDATED,
227 MR_ERROR
228 };
229
230 struct smbd_mr {
231 struct smbd_connection *conn;
232 struct list_head list;
233 enum mr_state state;
234 struct ib_mr *mr;
235 struct sg_table sgt;
236 enum dma_data_direction dir;
237 union {
238 struct ib_reg_wr wr;
239 struct ib_send_wr inv_wr;
240 };
241 struct ib_cqe cqe;
242 bool need_invalidate;
243 struct completion invalidate_done;
244 };
245
246 /* Interfaces to register and deregister MR for RDMA read/write */
247 struct smbd_mr *smbd_register_mr(
248 struct smbd_connection *info, struct iov_iter *iter,
249 bool writing, bool need_invalidate);
250 int smbd_deregister_mr(struct smbd_mr *mr);
251
252 #else
253 #define cifs_rdma_enabled(server) 0
254 struct smbd_connection {};
smbd_get_connection(struct TCP_Server_Info * server,struct sockaddr * dstaddr)255 static inline void *smbd_get_connection(
256 struct TCP_Server_Info *server, struct sockaddr *dstaddr) {return NULL;}
smbd_reconnect(struct TCP_Server_Info * server)257 static inline int smbd_reconnect(struct TCP_Server_Info *server) {return -1; }
smbd_destroy(struct TCP_Server_Info * server)258 static inline void smbd_destroy(struct TCP_Server_Info *server) {}
smbd_recv(struct smbd_connection * info,struct msghdr * msg)259 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)260 static inline int smbd_send(struct TCP_Server_Info *server, int num_rqst, struct smb_rqst *rqst) {return -1; }
261 #endif
262
263 #endif
264