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 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 wait_destroy;
66 struct completion negotiate_completion;
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;
74 spinlock_t lock_new_credits_offered;
75 int new_credits_offered;
77 /* Connection parameters defined in [MS-SMBD] 3.1.1.1 */
78 int receive_credit_max;
79 int send_credit_target;
81 int max_fragmented_recv_size;
82 int max_fragmented_send_size;
84 int keep_alive_interval;
85 int max_readwrite_size;
86 enum keep_alive_status keep_alive_requested;
88 atomic_t send_credits;
89 atomic_t receive_credits;
90 int receive_credit_target;
91 int fragment_reassembly_remaining;
93 /* Activity accoutning */
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;
101 struct list_head receive_queue;
102 int count_receive_queue;
103 spinlock_t receive_queue_lock;
105 struct list_head empty_packet_queue;
106 int count_empty_packet_queue;
107 spinlock_t empty_packet_queue_lock;
109 wait_queue_head_t wait_receive_queues;
111 /* Reassembly queue */
112 struct list_head reassembly_queue;
113 spinlock_t reassembly_queue_lock;
114 wait_queue_head_t wait_reassembly_queue;
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;
124 wait_queue_head_t wait_send_queue;
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
133 bool full_packet_received;
135 struct workqueue_struct *workqueue;
136 struct delayed_work idle_timer_work;
137 struct delayed_work send_immediate_work;
139 /* Memory pool for preallocating buffers */
140 /* request pool for RDMA send */
141 struct kmem_cache *request_cache;
142 mempool_t *request_mempool;
144 /* response pool for RDMA receive */
145 struct kmem_cache *response_cache;
146 mempool_t *response_mempool;
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;
157 enum smbd_message_type {
162 #define SMB_DIRECT_RESPONSE_REQUESTED 0x0001
164 /* SMBD negotiation request packet [MS-SMBD] 2.2.1 */
165 struct smbd_negotiate_req {
169 __le16 credits_requested;
170 __le32 preferred_send_size;
171 __le32 max_receive_size;
172 __le32 max_fragmented_size;
175 /* SMBD negotiation response packet [MS-SMBD] 2.2.2 */
176 struct smbd_negotiate_resp {
179 __le16 negotiated_version;
181 __le16 credits_requested;
182 __le16 credits_granted;
184 __le32 max_readwrite_size;
185 __le32 preferred_send_size;
186 __le32 max_receive_size;
187 __le32 max_fragmented_size;
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;
196 __le32 remaining_data_length;
203 /* The packet fields for a registered RDMA buffer */
204 struct smbd_buffer_descriptor_v1 {
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;
217 /* true if this request carries upper layer payload */
220 /* the SGE entries for this packet */
221 struct ib_sge sge[SMBDIRECT_MAX_SGE];
224 /* SMBD packet header follows this structure */
228 /* The context for a SMBD response */
229 struct smbd_response {
230 struct smbd_connection *info;
234 enum smbd_message_type type;
236 /* Link to receive queue or reassembly queue */
237 struct list_head list;
239 /* Indicate if this is the 1st packet of a payload */
242 /* SMBD packet header and payload follows this structure */
246 /* Create a SMBDirect session */
247 struct smbd_connection *smbd_get_connection(
248 struct TCP_Server_Info *server, struct sockaddr *dstaddr);
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);
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) {}