blob: fdb8df84fa1fecd8bf8cee3714df4face84425dd [file] [log] [blame]
Long Li03bee012017-11-07 01:54:56 -07001/*
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
Long Lif1981862017-11-04 18:17:24 -070019#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
27enum keep_alive_status {
28 KEEP_ALIVE_NONE,
29 KEEP_ALIVE_PENDING,
30 KEEP_ALIVE_SENT,
31};
32
33enum smbd_connection_status {
34 SMBD_CREATED,
35 SMBD_CONNECTING,
36 SMBD_CONNECTED,
37 SMBD_NEGOTIATE_FAILED,
38 SMBD_DISCONNECTING,
39 SMBD_DISCONNECTED,
40 SMBD_DESTROYED
41};
42
43/*
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
51 */
52struct smbd_connection {
53 enum smbd_connection_status transport_status;
54
55 /* RDMA related */
56 struct rdma_cm_id *id;
57 struct ib_qp_init_attr qp_attr;
58 struct ib_pd *pd;
59 struct ib_cq *send_cq, *recv_cq;
60 struct ib_device_attr dev_attr;
61 int ri_rc;
62 struct completion ri_done;
63 wait_queue_head_t conn_wait;
64 wait_queue_head_t wait_destroy;
65
66 struct completion negotiate_completion;
67 bool negotiate_done;
68
69 struct work_struct destroy_work;
70 struct work_struct disconnect_work;
71 struct work_struct recv_done_work;
72 struct work_struct post_send_credits_work;
73
74 spinlock_t lock_new_credits_offered;
75 int new_credits_offered;
76
77 /* Connection parameters defined in [MS-SMBD] 3.1.1.1 */
78 int receive_credit_max;
79 int send_credit_target;
80 int max_send_size;
81 int max_fragmented_recv_size;
82 int max_fragmented_send_size;
83 int max_receive_size;
84 int keep_alive_interval;
85 int max_readwrite_size;
86 enum keep_alive_status keep_alive_requested;
87 int protocol;
88 atomic_t send_credits;
89 atomic_t receive_credits;
90 int receive_credit_target;
91 int fragment_reassembly_remaining;
92
Long Lic7398582017-11-22 17:38:44 -070093 /* Memory registrations */
94 /* Maximum number of RDMA read/write outstanding on this connection */
95 int responder_resources;
96 /* Maximum number of SGEs in a RDMA write/read */
97 int max_frmr_depth;
98 /*
99 * If payload is less than or equal to the threshold,
100 * use RDMA send/recv to send upper layer I/O.
101 * If payload is more than the threshold,
102 * use RDMA read/write through memory registration for I/O.
103 */
104 int rdma_readwrite_threshold;
105 enum ib_mr_type mr_type;
106 struct list_head mr_list;
107 spinlock_t mr_list_lock;
108 /* The number of available MRs ready for memory registration */
109 atomic_t mr_ready_count;
110 atomic_t mr_used_count;
111 wait_queue_head_t wait_mr;
112 struct work_struct mr_recovery_work;
113 /* Used by transport to wait until all MRs are returned */
114 wait_queue_head_t wait_for_mr_cleanup;
115
Long Lif1981862017-11-04 18:17:24 -0700116 /* Activity accoutning */
Long Lif64b78f2017-11-22 17:38:40 -0700117 /* Pending reqeusts issued from upper layer */
Long Lid649e1b2017-11-22 17:38:42 -0700118 int smbd_send_pending;
119 wait_queue_head_t wait_smbd_send_pending;
120
Long Lif64b78f2017-11-22 17:38:40 -0700121 int smbd_recv_pending;
122 wait_queue_head_t wait_smbd_recv_pending;
Long Lif1981862017-11-04 18:17:24 -0700123
124 atomic_t send_pending;
125 wait_queue_head_t wait_send_pending;
126 atomic_t send_payload_pending;
127 wait_queue_head_t wait_send_payload_pending;
128
129 /* Receive queue */
130 struct list_head receive_queue;
131 int count_receive_queue;
132 spinlock_t receive_queue_lock;
133
134 struct list_head empty_packet_queue;
135 int count_empty_packet_queue;
136 spinlock_t empty_packet_queue_lock;
137
138 wait_queue_head_t wait_receive_queues;
139
140 /* Reassembly queue */
141 struct list_head reassembly_queue;
142 spinlock_t reassembly_queue_lock;
143 wait_queue_head_t wait_reassembly_queue;
144
145 /* total data length of reassembly queue */
146 int reassembly_data_length;
147 int reassembly_queue_length;
148 /* the offset to first buffer in reassembly queue */
149 int first_entry_offset;
150
151 bool send_immediate;
152
153 wait_queue_head_t wait_send_queue;
154
155 /*
156 * Indicate if we have received a full packet on the connection
157 * This is used to identify the first SMBD packet of a assembled
158 * payload (SMB packet) in reassembly queue so we can return a
159 * RFC1002 length to upper layer to indicate the length of the SMB
160 * packet received
161 */
162 bool full_packet_received;
163
164 struct workqueue_struct *workqueue;
165 struct delayed_work idle_timer_work;
166 struct delayed_work send_immediate_work;
167
168 /* Memory pool for preallocating buffers */
169 /* request pool for RDMA send */
170 struct kmem_cache *request_cache;
171 mempool_t *request_mempool;
172
173 /* response pool for RDMA receive */
174 struct kmem_cache *response_cache;
175 mempool_t *response_mempool;
176
177 /* for debug purposes */
178 unsigned int count_get_receive_buffer;
179 unsigned int count_put_receive_buffer;
180 unsigned int count_reassembly_queue;
181 unsigned int count_enqueue_reassembly_queue;
182 unsigned int count_dequeue_reassembly_queue;
183 unsigned int count_send_empty;
184};
185
186enum smbd_message_type {
187 SMBD_NEGOTIATE_RESP,
188 SMBD_TRANSFER_DATA,
189};
190
191#define SMB_DIRECT_RESPONSE_REQUESTED 0x0001
192
193/* SMBD negotiation request packet [MS-SMBD] 2.2.1 */
194struct smbd_negotiate_req {
195 __le16 min_version;
196 __le16 max_version;
197 __le16 reserved;
198 __le16 credits_requested;
199 __le32 preferred_send_size;
200 __le32 max_receive_size;
201 __le32 max_fragmented_size;
202} __packed;
203
204/* SMBD negotiation response packet [MS-SMBD] 2.2.2 */
205struct smbd_negotiate_resp {
206 __le16 min_version;
207 __le16 max_version;
208 __le16 negotiated_version;
209 __le16 reserved;
210 __le16 credits_requested;
211 __le16 credits_granted;
212 __le32 status;
213 __le32 max_readwrite_size;
214 __le32 preferred_send_size;
215 __le32 max_receive_size;
216 __le32 max_fragmented_size;
217} __packed;
218
219/* SMBD data transfer packet with payload [MS-SMBD] 2.2.3 */
220struct smbd_data_transfer {
221 __le16 credits_requested;
222 __le16 credits_granted;
223 __le16 flags;
224 __le16 reserved;
225 __le32 remaining_data_length;
226 __le32 data_offset;
227 __le32 data_length;
228 __le32 padding;
229 __u8 buffer[];
230} __packed;
231
232/* The packet fields for a registered RDMA buffer */
233struct smbd_buffer_descriptor_v1 {
234 __le64 offset;
235 __le32 token;
236 __le32 length;
237} __packed;
238
Long Li03bee012017-11-07 01:54:56 -0700239/* Default maximum number of SGEs in a RDMA send/recv */
240#define SMBDIRECT_MAX_SGE 16
Long Lif1981862017-11-04 18:17:24 -0700241/* The context for a SMBD request */
242struct smbd_request {
243 struct smbd_connection *info;
244 struct ib_cqe cqe;
245
246 /* true if this request carries upper layer payload */
247 bool has_payload;
248
249 /* the SGE entries for this packet */
250 struct ib_sge sge[SMBDIRECT_MAX_SGE];
251 int num_sge;
252
253 /* SMBD packet header follows this structure */
254 u8 packet[];
255};
256
257/* The context for a SMBD response */
258struct smbd_response {
259 struct smbd_connection *info;
260 struct ib_cqe cqe;
261 struct ib_sge sge;
262
263 enum smbd_message_type type;
264
265 /* Link to receive queue or reassembly queue */
266 struct list_head list;
267
268 /* Indicate if this is the 1st packet of a payload */
269 bool first_segment;
270
271 /* SMBD packet header and payload follows this structure */
272 u8 packet[];
273};
274
Long Li399f9532017-11-17 17:26:52 -0800275/* Create a SMBDirect session */
276struct smbd_connection *smbd_get_connection(
277 struct TCP_Server_Info *server, struct sockaddr *dstaddr);
278
Long Liad57b8e2017-11-22 17:38:35 -0700279/* Reconnect SMBDirect session */
280int smbd_reconnect(struct TCP_Server_Info *server);
Long Li8ef130f2017-11-22 17:38:37 -0700281/* Destroy SMBDirect session */
282void smbd_destroy(struct smbd_connection *info);
Long Liad57b8e2017-11-22 17:38:35 -0700283
Long Lif64b78f2017-11-22 17:38:40 -0700284/* Interface for carrying upper layer I/O through send/recv */
285int smbd_recv(struct smbd_connection *info, struct msghdr *msg);
Long Lid649e1b2017-11-22 17:38:42 -0700286int smbd_send(struct smbd_connection *info, struct smb_rqst *rqst);
Long Lif64b78f2017-11-22 17:38:40 -0700287
Long Lic7398582017-11-22 17:38:44 -0700288enum mr_state {
289 MR_READY,
290 MR_REGISTERED,
291 MR_INVALIDATED,
292 MR_ERROR
293};
294
295struct smbd_mr {
296 struct smbd_connection *conn;
297 struct list_head list;
298 enum mr_state state;
299 struct ib_mr *mr;
300 struct scatterlist *sgl;
301 int sgl_count;
302 enum dma_data_direction dir;
303 union {
304 struct ib_reg_wr wr;
305 struct ib_send_wr inv_wr;
306 };
307 struct ib_cqe cqe;
308 bool need_invalidate;
309 struct completion invalidate_done;
310};
311
312/* Interfaces to register and deregister MR for RDMA read/write */
313struct smbd_mr *smbd_register_mr(
314 struct smbd_connection *info, struct page *pages[], int num_pages,
315 int tailsz, bool writing, bool need_invalidate);
316int smbd_deregister_mr(struct smbd_mr *mr);
317
Long Lif1981862017-11-04 18:17:24 -0700318#else
319#define cifs_rdma_enabled(server) 0
320struct smbd_connection {};
Long Li399f9532017-11-17 17:26:52 -0800321static inline void *smbd_get_connection(
322 struct TCP_Server_Info *server, struct sockaddr *dstaddr) {return NULL;}
Long Liad57b8e2017-11-22 17:38:35 -0700323static inline int smbd_reconnect(struct TCP_Server_Info *server) {return -1; }
Long Li8ef130f2017-11-22 17:38:37 -0700324static inline void smbd_destroy(struct smbd_connection *info) {}
Long Lif64b78f2017-11-22 17:38:40 -0700325static inline int smbd_recv(struct smbd_connection *info, struct msghdr *msg) {return -1; }
Long Lid649e1b2017-11-22 17:38:42 -0700326static inline int smbd_send(struct smbd_connection *info, struct smb_rqst *rqst) {return -1; }
Long Lif1981862017-11-04 18:17:24 -0700327#endif
328
Long Li03bee012017-11-07 01:54:56 -0700329#endif