xref: /linux/fs/smb/common/smbdirect/smbdirect_socket.h (revision cfaf773b7946fa911e311acd3b82d61c7a9e42c2)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *   Copyright (c) 2025 Stefan Metzmacher
4  */
5 
6 #ifndef __FS_SMB_COMMON_SMBDIRECT_SMBDIRECT_SOCKET_H__
7 #define __FS_SMB_COMMON_SMBDIRECT_SMBDIRECT_SOCKET_H__
8 
9 enum smbdirect_socket_status {
10 	SMBDIRECT_SOCKET_CREATED,
11 	SMBDIRECT_SOCKET_CONNECTING,
12 	SMBDIRECT_SOCKET_CONNECTED,
13 	SMBDIRECT_SOCKET_NEGOTIATE_FAILED,
14 	SMBDIRECT_SOCKET_DISCONNECTING,
15 	SMBDIRECT_SOCKET_DISCONNECTED,
16 	SMBDIRECT_SOCKET_DESTROYED
17 };
18 
19 struct smbdirect_socket {
20 	enum smbdirect_socket_status status;
21 
22 	/* RDMA related */
23 	struct {
24 		struct rdma_cm_id *cm_id;
25 	} rdma;
26 
27 	/* IB verbs related */
28 	struct {
29 		struct ib_pd *pd;
30 		struct ib_cq *send_cq;
31 		struct ib_cq *recv_cq;
32 
33 		/*
34 		 * shortcuts for rdma.cm_id->{qp,device};
35 		 */
36 		struct ib_qp *qp;
37 		struct ib_device *dev;
38 	} ib;
39 
40 	struct smbdirect_socket_parameters parameters;
41 
42 	/*
43 	 * The state for posted send buffers
44 	 */
45 	struct {
46 		/*
47 		 * Memory pools for preallocating
48 		 * smbdirect_send_io buffers
49 		 */
50 		struct {
51 			struct kmem_cache	*cache;
52 			mempool_t		*pool;
53 		} mem;
54 	} send_io;
55 
56 	/*
57 	 * The state for posted receive buffers
58 	 */
59 	struct {
60 		/*
61 		 * The type of PDU we are expecting
62 		 */
63 		enum {
64 			SMBDIRECT_EXPECT_NEGOTIATE_REQ = 1,
65 			SMBDIRECT_EXPECT_NEGOTIATE_REP = 2,
66 			SMBDIRECT_EXPECT_DATA_TRANSFER = 3,
67 		} expected;
68 
69 		/*
70 		 * Memory pools for preallocating
71 		 * smbdirect_recv_io buffers
72 		 */
73 		struct {
74 			struct kmem_cache	*cache;
75 			mempool_t		*pool;
76 		} mem;
77 
78 		/*
79 		 * The list of free smbdirect_recv_io
80 		 * structures
81 		 */
82 		struct {
83 			struct list_head list;
84 			spinlock_t lock;
85 		} free;
86 
87 		/*
88 		 * The list of arrived non-empty smbdirect_recv_io
89 		 * structures
90 		 *
91 		 * This represents the reassembly queue.
92 		 */
93 		struct {
94 			struct list_head list;
95 			spinlock_t lock;
96 			wait_queue_head_t wait_queue;
97 			/* total data length of reassembly queue */
98 			int data_length;
99 			int queue_length;
100 			/* the offset to first buffer in reassembly queue */
101 			int first_entry_offset;
102 			/*
103 			 * Indicate if we have received a full packet on the
104 			 * connection This is used to identify the first SMBD
105 			 * packet of a assembled payload (SMB packet) in
106 			 * reassembly queue so we can return a RFC1002 length to
107 			 * upper layer to indicate the length of the SMB packet
108 			 * received
109 			 */
110 			bool full_packet_received;
111 		} reassembly;
112 	} recv_io;
113 };
114 
115 struct smbdirect_send_io {
116 	struct smbdirect_socket *socket;
117 	struct ib_cqe cqe;
118 
119 	/*
120 	 * The SGE entries for this work request
121 	 *
122 	 * The first points to the packet header
123 	 */
124 #define SMBDIRECT_SEND_IO_MAX_SGE 6
125 	size_t num_sge;
126 	struct ib_sge sge[SMBDIRECT_SEND_IO_MAX_SGE];
127 
128 	/*
129 	 * Link to the list of sibling smbdirect_send_io
130 	 * messages.
131 	 */
132 	struct list_head sibling_list;
133 	struct ib_send_wr wr;
134 
135 	/* SMBD packet header follows this structure */
136 	u8 packet[];
137 };
138 
139 struct smbdirect_recv_io {
140 	struct smbdirect_socket *socket;
141 	struct ib_cqe cqe;
142 
143 	/*
144 	 * For now we only use a single SGE
145 	 * as we have just one large buffer
146 	 * per posted recv.
147 	 */
148 #define SMBDIRECT_RECV_IO_MAX_SGE 1
149 	struct ib_sge sge;
150 
151 	/* Link to free or reassembly list */
152 	struct list_head list;
153 
154 	/* Indicate if this is the 1st packet of a payload */
155 	bool first_segment;
156 
157 	/* SMBD packet header and payload follows this structure */
158 	u8 packet[];
159 };
160 
161 #endif /* __FS_SMB_COMMON_SMBDIRECT_SMBDIRECT_SOCKET_H__ */
162