xref: /openbmc/linux/drivers/block/rnbd/rnbd-proto.h (revision f97769fd)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * RDMA Network Block Driver
4  *
5  * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved.
6  * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved.
7  * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved.
8  */
9 #ifndef RNBD_PROTO_H
10 #define RNBD_PROTO_H
11 
12 #include <linux/types.h>
13 #include <linux/blkdev.h>
14 #include <linux/limits.h>
15 #include <linux/inet.h>
16 #include <linux/in.h>
17 #include <linux/in6.h>
18 #include <rdma/ib.h>
19 
20 #define RNBD_PROTO_VER_MAJOR 2
21 #define RNBD_PROTO_VER_MINOR 0
22 
23 /* The default port number the RTRS server is listening on. */
24 #define RTRS_PORT 1234
25 
26 /**
27  * enum rnbd_msg_types - RNBD message types
28  * @RNBD_MSG_SESS_INFO:	initial session info from client to server
29  * @RNBD_MSG_SESS_INFO_RSP:	initial session info from server to client
30  * @RNBD_MSG_OPEN:		open (map) device request
31  * @RNBD_MSG_OPEN_RSP:		response to an @RNBD_MSG_OPEN
32  * @RNBD_MSG_IO:		block IO request operation
33  * @RNBD_MSG_CLOSE:		close (unmap) device request
34  */
35 enum rnbd_msg_type {
36 	RNBD_MSG_SESS_INFO,
37 	RNBD_MSG_SESS_INFO_RSP,
38 	RNBD_MSG_OPEN,
39 	RNBD_MSG_OPEN_RSP,
40 	RNBD_MSG_IO,
41 	RNBD_MSG_CLOSE,
42 };
43 
44 /**
45  * struct rnbd_msg_hdr - header of RNBD messages
46  * @type:	Message type, valid values see: enum rnbd_msg_types
47  */
48 struct rnbd_msg_hdr {
49 	__le16		type;
50 	__le16		__padding;
51 };
52 
53 /**
54  * We allow to map RO many times and RW only once. We allow to map yet another
55  * time RW, if MIGRATION is provided (second RW export can be required for
56  * example for VM migration)
57  */
58 enum rnbd_access_mode {
59 	RNBD_ACCESS_RO,
60 	RNBD_ACCESS_RW,
61 	RNBD_ACCESS_MIGRATION,
62 };
63 
64 /**
65  * struct rnbd_msg_sess_info - initial session info from client to server
66  * @hdr:		message header
67  * @ver:		RNBD protocol version
68  */
69 struct rnbd_msg_sess_info {
70 	struct rnbd_msg_hdr hdr;
71 	u8		ver;
72 	u8		reserved[31];
73 };
74 
75 /**
76  * struct rnbd_msg_sess_info_rsp - initial session info from server to client
77  * @hdr:		message header
78  * @ver:		RNBD protocol version
79  */
80 struct rnbd_msg_sess_info_rsp {
81 	struct rnbd_msg_hdr hdr;
82 	u8		ver;
83 	u8		reserved[31];
84 };
85 
86 /**
87  * struct rnbd_msg_open - request to open a remote device.
88  * @hdr:		message header
89  * @access_mode:	the mode to open remote device, valid values see:
90  *			enum rnbd_access_mode
91  * @device_name:	device path on remote side
92  */
93 struct rnbd_msg_open {
94 	struct rnbd_msg_hdr hdr;
95 	u8		access_mode;
96 	u8		resv1;
97 	s8		dev_name[NAME_MAX];
98 	u8		reserved[3];
99 };
100 
101 /**
102  * struct rnbd_msg_close - request to close a remote device.
103  * @hdr:	message header
104  * @device_id:	device_id on server side to identify the device
105  */
106 struct rnbd_msg_close {
107 	struct rnbd_msg_hdr hdr;
108 	__le32		device_id;
109 };
110 
111 /**
112  * struct rnbd_msg_open_rsp - response message to RNBD_MSG_OPEN
113  * @hdr:		message header
114  * @device_id:		device_id on server side to identify the device
115  * @nsectors:		number of sectors in the usual 512b unit
116  * @max_hw_sectors:	max hardware sectors in the usual 512b unit
117  * @max_write_same_sectors: max sectors for WRITE SAME in the 512b unit
118  * @max_discard_sectors: max. sectors that can be discarded at once in 512b
119  * unit.
120  * @discard_granularity: size of the internal discard allocation unit in bytes
121  * @discard_alignment: offset from internal allocation assignment in bytes
122  * @physical_block_size: physical block size device supports in bytes
123  * @logical_block_size: logical block size device supports in bytes
124  * @max_segments:	max segments hardware support in one transfer
125  * @secure_discard:	supports secure discard
126  * @rotation:		is a rotational disc?
127  */
128 struct rnbd_msg_open_rsp {
129 	struct rnbd_msg_hdr	hdr;
130 	__le32			device_id;
131 	__le64			nsectors;
132 	__le32			max_hw_sectors;
133 	__le32			max_write_same_sectors;
134 	__le32			max_discard_sectors;
135 	__le32			discard_granularity;
136 	__le32			discard_alignment;
137 	__le16			physical_block_size;
138 	__le16			logical_block_size;
139 	__le16			max_segments;
140 	__le16			secure_discard;
141 	u8			rotational;
142 	u8			reserved[11];
143 };
144 
145 /**
146  * struct rnbd_msg_io - message for I/O read/write
147  * @hdr:	message header
148  * @device_id:	device_id on server side to find the right device
149  * @sector:	bi_sector attribute from struct bio
150  * @rw:		valid values are defined in enum rnbd_io_flags
151  * @bi_size:    number of bytes for I/O read/write
152  * @prio:       priority
153  */
154 struct rnbd_msg_io {
155 	struct rnbd_msg_hdr hdr;
156 	__le32		device_id;
157 	__le64		sector;
158 	__le32		rw;
159 	__le32		bi_size;
160 	__le16		prio;
161 };
162 
163 #define RNBD_OP_BITS  8
164 #define RNBD_OP_MASK  ((1 << RNBD_OP_BITS) - 1)
165 
166 /**
167  * enum rnbd_io_flags - RNBD request types from rq_flag_bits
168  * @RNBD_OP_READ:	     read sectors from the device
169  * @RNBD_OP_WRITE:	     write sectors to the device
170  * @RNBD_OP_FLUSH:	     flush the volatile write cache
171  * @RNBD_OP_DISCARD:        discard sectors
172  * @RNBD_OP_SECURE_ERASE:   securely erase sectors
173  * @RNBD_OP_WRITE_SAME:     write the same sectors many times
174 
175  * @RNBD_F_SYNC:	     request is sync (sync write or read)
176  * @RNBD_F_FUA:             forced unit access
177  */
178 enum rnbd_io_flags {
179 
180 	/* Operations */
181 
182 	RNBD_OP_READ		= 0,
183 	RNBD_OP_WRITE		= 1,
184 	RNBD_OP_FLUSH		= 2,
185 	RNBD_OP_DISCARD	= 3,
186 	RNBD_OP_SECURE_ERASE	= 4,
187 	RNBD_OP_WRITE_SAME	= 5,
188 
189 	RNBD_OP_LAST,
190 
191 	/* Flags */
192 
193 	RNBD_F_SYNC  = 1<<(RNBD_OP_BITS + 0),
194 	RNBD_F_FUA   = 1<<(RNBD_OP_BITS + 1),
195 
196 	RNBD_F_ALL   = (RNBD_F_SYNC | RNBD_F_FUA)
197 
198 };
199 
200 static inline u32 rnbd_op(u32 flags)
201 {
202 	return flags & RNBD_OP_MASK;
203 }
204 
205 static inline u32 rnbd_flags(u32 flags)
206 {
207 	return flags & ~RNBD_OP_MASK;
208 }
209 
210 static inline bool rnbd_flags_supported(u32 flags)
211 {
212 	u32 op;
213 
214 	op = rnbd_op(flags);
215 	flags = rnbd_flags(flags);
216 
217 	if (op >= RNBD_OP_LAST)
218 		return false;
219 	if (flags & ~RNBD_F_ALL)
220 		return false;
221 
222 	return true;
223 }
224 
225 static inline u32 rnbd_to_bio_flags(u32 rnbd_opf)
226 {
227 	u32 bio_opf;
228 
229 	switch (rnbd_op(rnbd_opf)) {
230 	case RNBD_OP_READ:
231 		bio_opf = REQ_OP_READ;
232 		break;
233 	case RNBD_OP_WRITE:
234 		bio_opf = REQ_OP_WRITE;
235 		break;
236 	case RNBD_OP_FLUSH:
237 		bio_opf = REQ_OP_FLUSH | REQ_PREFLUSH;
238 		break;
239 	case RNBD_OP_DISCARD:
240 		bio_opf = REQ_OP_DISCARD;
241 		break;
242 	case RNBD_OP_SECURE_ERASE:
243 		bio_opf = REQ_OP_SECURE_ERASE;
244 		break;
245 	case RNBD_OP_WRITE_SAME:
246 		bio_opf = REQ_OP_WRITE_SAME;
247 		break;
248 	default:
249 		WARN(1, "Unknown RNBD type: %d (flags %d)\n",
250 		     rnbd_op(rnbd_opf), rnbd_opf);
251 		bio_opf = 0;
252 	}
253 
254 	if (rnbd_opf & RNBD_F_SYNC)
255 		bio_opf |= REQ_SYNC;
256 
257 	if (rnbd_opf & RNBD_F_FUA)
258 		bio_opf |= REQ_FUA;
259 
260 	return bio_opf;
261 }
262 
263 static inline u32 rq_to_rnbd_flags(struct request *rq)
264 {
265 	u32 rnbd_opf;
266 
267 	switch (req_op(rq)) {
268 	case REQ_OP_READ:
269 		rnbd_opf = RNBD_OP_READ;
270 		break;
271 	case REQ_OP_WRITE:
272 		rnbd_opf = RNBD_OP_WRITE;
273 		break;
274 	case REQ_OP_DISCARD:
275 		rnbd_opf = RNBD_OP_DISCARD;
276 		break;
277 	case REQ_OP_SECURE_ERASE:
278 		rnbd_opf = RNBD_OP_SECURE_ERASE;
279 		break;
280 	case REQ_OP_WRITE_SAME:
281 		rnbd_opf = RNBD_OP_WRITE_SAME;
282 		break;
283 	case REQ_OP_FLUSH:
284 		rnbd_opf = RNBD_OP_FLUSH;
285 		break;
286 	default:
287 		WARN(1, "Unknown request type %d (flags %llu)\n",
288 		     req_op(rq), (unsigned long long)rq->cmd_flags);
289 		rnbd_opf = 0;
290 	}
291 
292 	if (op_is_sync(rq->cmd_flags))
293 		rnbd_opf |= RNBD_F_SYNC;
294 
295 	if (op_is_flush(rq->cmd_flags))
296 		rnbd_opf |= RNBD_F_FUA;
297 
298 	return rnbd_opf;
299 }
300 
301 const char *rnbd_access_mode_str(enum rnbd_access_mode mode);
302 
303 #endif /* RNBD_PROTO_H */
304