1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright (C) 2017, Microsoft Corporation.
5 * Author(s): Long Li <longli@microsoft.com>
10 #ifdef CONFIG_CIFS_SMB_DIRECT
11 #define cifs_rdma_enabled(server) ((server)->rdma)
14 #include <rdma/ib_verbs.h>
15 #include <rdma/rdma_cm.h>
16 #include <linux/mempool.h>
18 extern int rdma_readwrite_threshold
;
19 extern int smbd_max_frmr_depth
;
20 extern int smbd_keep_alive_interval
;
21 extern int smbd_max_receive_size
;
22 extern int smbd_max_fragmented_recv_size
;
23 extern int smbd_max_send_size
;
24 extern int smbd_send_credit_target
;
25 extern int smbd_receive_credit_max
;
27 enum keep_alive_status
{
33 enum smbd_connection_status
{
37 SMBD_NEGOTIATE_FAILED
,
44 * The context for the SMBDirect transport
45 * Everything related to the transport is here. It has several logical parts
46 * 1. RDMA related structures
47 * 2. SMBDirect connection parameters
48 * 3. Memory registrations
49 * 4. Receive and reassembly queues for data receive path
50 * 5. mempools for allocating packets
52 struct smbd_connection
{
53 enum smbd_connection_status transport_status
;
56 struct rdma_cm_id
*id
;
57 struct ib_qp_init_attr qp_attr
;
59 struct ib_cq
*send_cq
, *recv_cq
;
60 struct ib_device_attr dev_attr
;
62 struct completion ri_done
;
63 wait_queue_head_t conn_wait
;
64 wait_queue_head_t disconn_wait
;
66 struct completion negotiate_completion
;
69 struct work_struct disconnect_work
;
70 struct work_struct post_send_credits_work
;
72 spinlock_t lock_new_credits_offered
;
73 int new_credits_offered
;
75 /* Connection parameters defined in [MS-SMBD] 3.1.1.1 */
76 int receive_credit_max
;
77 int send_credit_target
;
79 int max_fragmented_recv_size
;
80 int max_fragmented_send_size
;
82 int keep_alive_interval
;
83 int max_readwrite_size
;
84 enum keep_alive_status keep_alive_requested
;
86 atomic_t send_credits
;
87 atomic_t receive_credits
;
88 int receive_credit_target
;
89 int fragment_reassembly_remaining
;
91 /* Memory registrations */
92 /* Maximum number of RDMA read/write outstanding on this connection */
93 int responder_resources
;
94 /* Maximum number of SGEs in a RDMA write/read */
97 * If payload is less than or equal to the threshold,
98 * use RDMA send/recv to send upper layer I/O.
99 * If payload is more than the threshold,
100 * use RDMA read/write through memory registration for I/O.
102 int rdma_readwrite_threshold
;
103 enum ib_mr_type mr_type
;
104 struct list_head mr_list
;
105 spinlock_t mr_list_lock
;
106 /* The number of available MRs ready for memory registration */
107 atomic_t mr_ready_count
;
108 atomic_t mr_used_count
;
109 wait_queue_head_t wait_mr
;
110 struct work_struct mr_recovery_work
;
111 /* Used by transport to wait until all MRs are returned */
112 wait_queue_head_t wait_for_mr_cleanup
;
114 /* Activity accoutning */
115 atomic_t send_pending
;
116 wait_queue_head_t wait_send_pending
;
117 wait_queue_head_t wait_post_send
;
120 struct list_head receive_queue
;
121 int count_receive_queue
;
122 spinlock_t receive_queue_lock
;
124 struct list_head empty_packet_queue
;
125 int count_empty_packet_queue
;
126 spinlock_t empty_packet_queue_lock
;
128 wait_queue_head_t wait_receive_queues
;
130 /* Reassembly queue */
131 struct list_head reassembly_queue
;
132 spinlock_t reassembly_queue_lock
;
133 wait_queue_head_t wait_reassembly_queue
;
135 /* total data length of reassembly queue */
136 int reassembly_data_length
;
137 int reassembly_queue_length
;
138 /* the offset to first buffer in reassembly queue */
139 int first_entry_offset
;
143 wait_queue_head_t wait_send_queue
;
146 * Indicate if we have received a full packet on the connection
147 * This is used to identify the first SMBD packet of a assembled
148 * payload (SMB packet) in reassembly queue so we can return a
149 * RFC1002 length to upper layer to indicate the length of the SMB
152 bool full_packet_received
;
154 struct workqueue_struct
*workqueue
;
155 struct delayed_work idle_timer_work
;
157 /* Memory pool for preallocating buffers */
158 /* request pool for RDMA send */
159 struct kmem_cache
*request_cache
;
160 mempool_t
*request_mempool
;
162 /* response pool for RDMA receive */
163 struct kmem_cache
*response_cache
;
164 mempool_t
*response_mempool
;
166 /* for debug purposes */
167 unsigned int count_get_receive_buffer
;
168 unsigned int count_put_receive_buffer
;
169 unsigned int count_reassembly_queue
;
170 unsigned int count_enqueue_reassembly_queue
;
171 unsigned int count_dequeue_reassembly_queue
;
172 unsigned int count_send_empty
;
175 enum smbd_message_type
{
180 #define SMB_DIRECT_RESPONSE_REQUESTED 0x0001
182 /* SMBD negotiation request packet [MS-SMBD] 2.2.1 */
183 struct smbd_negotiate_req
{
187 __le16 credits_requested
;
188 __le32 preferred_send_size
;
189 __le32 max_receive_size
;
190 __le32 max_fragmented_size
;
193 /* SMBD negotiation response packet [MS-SMBD] 2.2.2 */
194 struct smbd_negotiate_resp
{
197 __le16 negotiated_version
;
199 __le16 credits_requested
;
200 __le16 credits_granted
;
202 __le32 max_readwrite_size
;
203 __le32 preferred_send_size
;
204 __le32 max_receive_size
;
205 __le32 max_fragmented_size
;
208 /* SMBD data transfer packet with payload [MS-SMBD] 2.2.3 */
209 struct smbd_data_transfer
{
210 __le16 credits_requested
;
211 __le16 credits_granted
;
214 __le32 remaining_data_length
;
221 /* The packet fields for a registered RDMA buffer */
222 struct smbd_buffer_descriptor_v1
{
228 /* Default maximum number of SGEs in a RDMA send/recv */
229 #define SMBDIRECT_MAX_SGE 16
230 /* The context for a SMBD request */
231 struct smbd_request
{
232 struct smbd_connection
*info
;
235 /* the SGE entries for this packet */
236 struct ib_sge sge
[SMBDIRECT_MAX_SGE
];
239 /* SMBD packet header follows this structure */
243 /* The context for a SMBD response */
244 struct smbd_response
{
245 struct smbd_connection
*info
;
249 enum smbd_message_type type
;
251 /* Link to receive queue or reassembly queue */
252 struct list_head list
;
254 /* Indicate if this is the 1st packet of a payload */
257 /* SMBD packet header and payload follows this structure */
261 /* Create a SMBDirect session */
262 struct smbd_connection
*smbd_get_connection(
263 struct TCP_Server_Info
*server
, struct sockaddr
*dstaddr
);
265 /* Reconnect SMBDirect session */
266 int smbd_reconnect(struct TCP_Server_Info
*server
);
267 /* Destroy SMBDirect session */
268 void smbd_destroy(struct TCP_Server_Info
*server
);
270 /* Interface for carrying upper layer I/O through send/recv */
271 int smbd_recv(struct smbd_connection
*info
, struct msghdr
*msg
);
272 int smbd_send(struct TCP_Server_Info
*server
,
273 int num_rqst
, struct smb_rqst
*rqst
);
283 struct smbd_connection
*conn
;
284 struct list_head list
;
287 struct scatterlist
*sgl
;
289 enum dma_data_direction dir
;
292 struct ib_send_wr inv_wr
;
295 bool need_invalidate
;
296 struct completion invalidate_done
;
299 /* Interfaces to register and deregister MR for RDMA read/write */
300 struct smbd_mr
*smbd_register_mr(
301 struct smbd_connection
*info
, struct page
*pages
[], int num_pages
,
302 int offset
, int tailsz
, bool writing
, bool need_invalidate
);
303 int smbd_deregister_mr(struct smbd_mr
*mr
);
306 #define cifs_rdma_enabled(server) 0
307 struct smbd_connection
{};
308 static inline void *smbd_get_connection(
309 struct TCP_Server_Info
*server
, struct sockaddr
*dstaddr
) {return NULL
;}
310 static inline int smbd_reconnect(struct TCP_Server_Info
*server
) {return -1; }
311 static inline void smbd_destroy(struct TCP_Server_Info
*server
) {}
312 static inline int smbd_recv(struct smbd_connection
*info
, struct msghdr
*msg
) {return -1; }
313 static inline int smbd_send(struct TCP_Server_Info
*server
, int num_rqst
, struct smb_rqst
*rqst
) {return -1; }