CIFS: SMBD: Implement function to destroy a SMB Direct connection
[platform/kernel/linux-starfive.git] / fs / cifs / smbdirect.h
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 enum keep_alive_status {
28         KEEP_ALIVE_NONE,
29         KEEP_ALIVE_PENDING,
30         KEEP_ALIVE_SENT,
31 };
32
33 enum 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  */
52 struct 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
93         /* Activity accoutning */
94
95         atomic_t send_pending;
96         wait_queue_head_t wait_send_pending;
97         atomic_t send_payload_pending;
98         wait_queue_head_t wait_send_payload_pending;
99
100         /* Receive queue */
101         struct list_head receive_queue;
102         int count_receive_queue;
103         spinlock_t receive_queue_lock;
104
105         struct list_head empty_packet_queue;
106         int count_empty_packet_queue;
107         spinlock_t empty_packet_queue_lock;
108
109         wait_queue_head_t wait_receive_queues;
110
111         /* Reassembly queue */
112         struct list_head reassembly_queue;
113         spinlock_t reassembly_queue_lock;
114         wait_queue_head_t wait_reassembly_queue;
115
116         /* total data length of reassembly queue */
117         int reassembly_data_length;
118         int reassembly_queue_length;
119         /* the offset to first buffer in reassembly queue */
120         int first_entry_offset;
121
122         bool send_immediate;
123
124         wait_queue_head_t wait_send_queue;
125
126         /*
127          * Indicate if we have received a full packet on the connection
128          * This is used to identify the first SMBD packet of a assembled
129          * payload (SMB packet) in reassembly queue so we can return a
130          * RFC1002 length to upper layer to indicate the length of the SMB
131          * packet received
132          */
133         bool full_packet_received;
134
135         struct workqueue_struct *workqueue;
136         struct delayed_work idle_timer_work;
137         struct delayed_work send_immediate_work;
138
139         /* Memory pool for preallocating buffers */
140         /* request pool for RDMA send */
141         struct kmem_cache *request_cache;
142         mempool_t *request_mempool;
143
144         /* response pool for RDMA receive */
145         struct kmem_cache *response_cache;
146         mempool_t *response_mempool;
147
148         /* for debug purposes */
149         unsigned int count_get_receive_buffer;
150         unsigned int count_put_receive_buffer;
151         unsigned int count_reassembly_queue;
152         unsigned int count_enqueue_reassembly_queue;
153         unsigned int count_dequeue_reassembly_queue;
154         unsigned int count_send_empty;
155 };
156
157 enum smbd_message_type {
158         SMBD_NEGOTIATE_RESP,
159         SMBD_TRANSFER_DATA,
160 };
161
162 #define SMB_DIRECT_RESPONSE_REQUESTED 0x0001
163
164 /* SMBD negotiation request packet [MS-SMBD] 2.2.1 */
165 struct smbd_negotiate_req {
166         __le16 min_version;
167         __le16 max_version;
168         __le16 reserved;
169         __le16 credits_requested;
170         __le32 preferred_send_size;
171         __le32 max_receive_size;
172         __le32 max_fragmented_size;
173 } __packed;
174
175 /* SMBD negotiation response packet [MS-SMBD] 2.2.2 */
176 struct smbd_negotiate_resp {
177         __le16 min_version;
178         __le16 max_version;
179         __le16 negotiated_version;
180         __le16 reserved;
181         __le16 credits_requested;
182         __le16 credits_granted;
183         __le32 status;
184         __le32 max_readwrite_size;
185         __le32 preferred_send_size;
186         __le32 max_receive_size;
187         __le32 max_fragmented_size;
188 } __packed;
189
190 /* SMBD data transfer packet with payload [MS-SMBD] 2.2.3 */
191 struct smbd_data_transfer {
192         __le16 credits_requested;
193         __le16 credits_granted;
194         __le16 flags;
195         __le16 reserved;
196         __le32 remaining_data_length;
197         __le32 data_offset;
198         __le32 data_length;
199         __le32 padding;
200         __u8 buffer[];
201 } __packed;
202
203 /* The packet fields for a registered RDMA buffer */
204 struct smbd_buffer_descriptor_v1 {
205         __le64 offset;
206         __le32 token;
207         __le32 length;
208 } __packed;
209
210 /* Default maximum number of SGEs in a RDMA send/recv */
211 #define SMBDIRECT_MAX_SGE       16
212 /* The context for a SMBD request */
213 struct smbd_request {
214         struct smbd_connection *info;
215         struct ib_cqe cqe;
216
217         /* true if this request carries upper layer payload */
218         bool has_payload;
219
220         /* the SGE entries for this packet */
221         struct ib_sge sge[SMBDIRECT_MAX_SGE];
222         int num_sge;
223
224         /* SMBD packet header follows this structure */
225         u8 packet[];
226 };
227
228 /* The context for a SMBD response */
229 struct smbd_response {
230         struct smbd_connection *info;
231         struct ib_cqe cqe;
232         struct ib_sge sge;
233
234         enum smbd_message_type type;
235
236         /* Link to receive queue or reassembly queue */
237         struct list_head list;
238
239         /* Indicate if this is the 1st packet of a payload */
240         bool first_segment;
241
242         /* SMBD packet header and payload follows this structure */
243         u8 packet[];
244 };
245
246 /* Create a SMBDirect session */
247 struct smbd_connection *smbd_get_connection(
248         struct TCP_Server_Info *server, struct sockaddr *dstaddr);
249
250 /* Reconnect SMBDirect session */
251 int smbd_reconnect(struct TCP_Server_Info *server);
252 /* Destroy SMBDirect session */
253 void smbd_destroy(struct smbd_connection *info);
254
255 #else
256 #define cifs_rdma_enabled(server)       0
257 struct smbd_connection {};
258 static inline void *smbd_get_connection(
259         struct TCP_Server_Info *server, struct sockaddr *dstaddr) {return NULL;}
260 static inline int smbd_reconnect(struct TCP_Server_Info *server) {return -1; }
261 static inline void smbd_destroy(struct smbd_connection *info) {}
262 #endif
263
264 #endif