2 * Copyright (C) 2017, Microsoft Corporation.
4 * Author(s): Long Li <longli@microsoft.com>
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.
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.
19 #ifdef CONFIG_CIFS_SMB_DIRECT
20 #define cifs_rdma_enabled(server) ((server)->rdma)
23 #include <rdma/ib_verbs.h>
24 #include <rdma/rdma_cm.h>
25 #include <linux/mempool.h>
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;
36 enum keep_alive_status {
42 enum smbd_connection_status {
46 SMBD_NEGOTIATE_FAILED,
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
61 struct smbd_connection {
62 enum smbd_connection_status transport_status;
65 struct rdma_cm_id *id;
66 struct ib_qp_init_attr qp_attr;
68 struct ib_cq *send_cq, *recv_cq;
69 struct ib_device_attr dev_attr;
71 struct completion ri_done;
72 wait_queue_head_t conn_wait;
73 wait_queue_head_t disconn_wait;
75 struct completion negotiate_completion;
78 struct work_struct disconnect_work;
79 struct work_struct recv_done_work;
80 struct work_struct post_send_credits_work;
82 spinlock_t lock_new_credits_offered;
83 int new_credits_offered;
85 /* Connection parameters defined in [MS-SMBD] 3.1.1.1 */
86 int receive_credit_max;
87 int send_credit_target;
89 int max_fragmented_recv_size;
90 int max_fragmented_send_size;
92 int keep_alive_interval;
93 int max_readwrite_size;
94 enum keep_alive_status keep_alive_requested;
96 atomic_t send_credits;
97 atomic_t receive_credits;
98 int receive_credit_target;
99 int fragment_reassembly_remaining;
101 /* Memory registrations */
102 /* Maximum number of RDMA read/write outstanding on this connection */
103 int responder_resources;
104 /* Maximum number of SGEs in a RDMA write/read */
107 * If payload is less than or equal to the threshold,
108 * use RDMA send/recv to send upper layer I/O.
109 * If payload is more than the threshold,
110 * use RDMA read/write through memory registration for I/O.
112 int rdma_readwrite_threshold;
113 enum ib_mr_type mr_type;
114 struct list_head mr_list;
115 spinlock_t mr_list_lock;
116 /* The number of available MRs ready for memory registration */
117 atomic_t mr_ready_count;
118 atomic_t mr_used_count;
119 wait_queue_head_t wait_mr;
120 struct work_struct mr_recovery_work;
121 /* Used by transport to wait until all MRs are returned */
122 wait_queue_head_t wait_for_mr_cleanup;
124 /* Activity accoutning */
125 atomic_t send_pending;
126 wait_queue_head_t wait_send_pending;
127 atomic_t send_payload_pending;
128 wait_queue_head_t wait_send_payload_pending;
131 struct list_head receive_queue;
132 int count_receive_queue;
133 spinlock_t receive_queue_lock;
135 struct list_head empty_packet_queue;
136 int count_empty_packet_queue;
137 spinlock_t empty_packet_queue_lock;
139 wait_queue_head_t wait_receive_queues;
141 /* Reassembly queue */
142 struct list_head reassembly_queue;
143 spinlock_t reassembly_queue_lock;
144 wait_queue_head_t wait_reassembly_queue;
146 /* total data length of reassembly queue */
147 int reassembly_data_length;
148 int reassembly_queue_length;
149 /* the offset to first buffer in reassembly queue */
150 int first_entry_offset;
154 wait_queue_head_t wait_send_queue;
157 * Indicate if we have received a full packet on the connection
158 * This is used to identify the first SMBD packet of a assembled
159 * payload (SMB packet) in reassembly queue so we can return a
160 * RFC1002 length to upper layer to indicate the length of the SMB
163 bool full_packet_received;
165 struct workqueue_struct *workqueue;
166 struct delayed_work idle_timer_work;
167 struct delayed_work send_immediate_work;
169 /* Memory pool for preallocating buffers */
170 /* request pool for RDMA send */
171 struct kmem_cache *request_cache;
172 mempool_t *request_mempool;
174 /* response pool for RDMA receive */
175 struct kmem_cache *response_cache;
176 mempool_t *response_mempool;
178 /* for debug purposes */
179 unsigned int count_get_receive_buffer;
180 unsigned int count_put_receive_buffer;
181 unsigned int count_reassembly_queue;
182 unsigned int count_enqueue_reassembly_queue;
183 unsigned int count_dequeue_reassembly_queue;
184 unsigned int count_send_empty;
187 enum smbd_message_type {
192 #define SMB_DIRECT_RESPONSE_REQUESTED 0x0001
194 /* SMBD negotiation request packet [MS-SMBD] 2.2.1 */
195 struct smbd_negotiate_req {
199 __le16 credits_requested;
200 __le32 preferred_send_size;
201 __le32 max_receive_size;
202 __le32 max_fragmented_size;
205 /* SMBD negotiation response packet [MS-SMBD] 2.2.2 */
206 struct smbd_negotiate_resp {
209 __le16 negotiated_version;
211 __le16 credits_requested;
212 __le16 credits_granted;
214 __le32 max_readwrite_size;
215 __le32 preferred_send_size;
216 __le32 max_receive_size;
217 __le32 max_fragmented_size;
220 /* SMBD data transfer packet with payload [MS-SMBD] 2.2.3 */
221 struct smbd_data_transfer {
222 __le16 credits_requested;
223 __le16 credits_granted;
226 __le32 remaining_data_length;
233 /* The packet fields for a registered RDMA buffer */
234 struct smbd_buffer_descriptor_v1 {
240 /* Default maximum number of SGEs in a RDMA send/recv */
241 #define SMBDIRECT_MAX_SGE 16
242 /* The context for a SMBD request */
243 struct smbd_request {
244 struct smbd_connection *info;
247 /* true if this request carries upper layer payload */
250 /* the SGE entries for this packet */
251 struct ib_sge sge[SMBDIRECT_MAX_SGE];
254 /* SMBD packet header follows this structure */
258 /* The context for a SMBD response */
259 struct smbd_response {
260 struct smbd_connection *info;
264 enum smbd_message_type type;
266 /* Link to receive queue or reassembly queue */
267 struct list_head list;
269 /* Indicate if this is the 1st packet of a payload */
272 /* SMBD packet header and payload follows this structure */
276 /* Create a SMBDirect session */
277 struct smbd_connection *smbd_get_connection(
278 struct TCP_Server_Info *server, struct sockaddr *dstaddr);
280 /* Reconnect SMBDirect session */
281 int smbd_reconnect(struct TCP_Server_Info *server);
282 /* Destroy SMBDirect session */
283 void smbd_destroy(struct TCP_Server_Info *server);
285 /* Interface for carrying upper layer I/O through send/recv */
286 int smbd_recv(struct smbd_connection *info, struct msghdr *msg);
287 int smbd_send(struct TCP_Server_Info *server,
288 int num_rqst, struct smb_rqst *rqst);
298 struct smbd_connection *conn;
299 struct list_head list;
302 struct scatterlist *sgl;
304 enum dma_data_direction dir;
307 struct ib_send_wr inv_wr;
310 bool need_invalidate;
311 struct completion invalidate_done;
314 /* Interfaces to register and deregister MR for RDMA read/write */
315 struct smbd_mr *smbd_register_mr(
316 struct smbd_connection *info, struct page *pages[], int num_pages,
317 int offset, int tailsz, bool writing, bool need_invalidate);
318 int smbd_deregister_mr(struct smbd_mr *mr);
321 #define cifs_rdma_enabled(server) 0
322 struct smbd_connection {};
323 static inline void *smbd_get_connection(
324 struct TCP_Server_Info *server, struct sockaddr *dstaddr) {return NULL;}
325 static inline int smbd_reconnect(struct TCP_Server_Info *server) {return -1; }
326 static inline void smbd_destroy(struct TCP_Server_Info *server) {}
327 static inline int smbd_recv(struct smbd_connection *info, struct msghdr *msg) {return -1; }
328 static inline int smbd_send(struct TCP_Server_Info *server, int num_rqst, struct smb_rqst *rqst) {return -1; }