1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23  *
24  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
25  */
26 
27 #ifndef	_STMF_SBD_H
28 #define	_STMF_SBD_H
29 
30 #include <sys/dkio.h>
31 
32 #ifdef	__cplusplus
33 extern "C" {
34 #endif
35 
36 typedef	stmf_status_t	sbd_status_t;
37 #include "ats_copy_mgr.h"
38 extern char sbd_vendor_id[];
39 extern char sbd_product_id[];
40 extern char sbd_revision[];
41 extern char *sbd_mgmt_url;
42 extern uint16_t sbd_mgmt_url_alloc_size;
43 extern krwlock_t sbd_global_prop_lock;
44 
45 /*
46  * Error codes
47  */
48 #define	SBD_SUCCESS		STMF_SUCCESS
49 #define	SBD_FAILURE		STMF_LU_FAILURE
50 
51 #define	SBD_ALREADY		(SBD_FAILURE | STMF_FSC(1))
52 #define	SBD_NOT_SUPPORTED	(SBD_FAILURE | STMF_FSC(2))
53 #define	SBD_META_CORRUPTED	(SBD_FAILURE | STMF_FSC(3))
54 #define	SBD_INVALID_ARG		(SBD_FAILURE | STMF_FSC(4))
55 #define	SBD_NOT_FOUND		(SBD_FAILURE | STMF_FSC(5))
56 #define	SBD_ALLOC_FAILURE	(SBD_FAILURE | STMF_FSC(6))
57 #define	SBD_FILEIO_FAILURE	(SBD_FAILURE | STMF_FSC(7))
58 #define	SBD_IO_PAST_EOF		(SBD_FAILURE | STMF_FSC(8))
59 #define	SBD_BUSY		(SBD_FAILURE | STMF_FSC(9))
60 #define	SBD_COMPARE_FAILED	(SBD_FAILURE | STMF_FSC(10))
61 
62 #define	SHARED_META_DATA_SIZE	65536
63 #define	SBD_META_OFFSET		4096
64 #define	SBD_MIN_LU_SIZE		(1024 * 1024)
65 
66 /*
67  * sms endianess
68  */
69 #define	SMS_BIG_ENDIAN			0x00
70 #define	SMS_LITTLE_ENDIAN		0xFF
71 
72 #ifdef	_BIG_ENDIAN
73 #define	SMS_DATA_ORDER	SMS_BIG_ENDIAN
74 #else
75 #define	SMS_DATA_ORDER	SMS_LITTLE_ENDIAN
76 #endif
77 
78 #define	SBD_MAGIC	0x53554e5342444c55
79 
80 #define	SBD_VER_MAJOR		1
81 #define	SBD_VER_MINOR		1
82 #define	SBD_VER_SUBMINOR	0
83 
84 #if 0
85 typedef struct sbd_meta_start {
86 	uint64_t		sm_magic;
87 	uint64_t		sm_meta_size;
88 	uint64_t		sm_meta_size_used;
89 	uint64_t		sm_rsvd1;	/* Defaults to zero */
90 	uint64_t		sm_rsvd2;
91 	uint16_t		sm_ver_major;
92 	uint16_t		sm_ver_minor;
93 	uint16_t		sm_ver_subminor;
94 	uint8_t			sm_flags;
95 	uint8_t			sm_chksum;
96 } sbd_meta_start_t;
97 #endif
98 
99 typedef struct sm_section_hdr {
100 	uint64_t	sms_offset;	/* Offset of this section */
101 	uint32_t	sms_size;	/* Includes the header and padding */
102 	uint16_t	sms_id;		/* Section identifier */
103 	uint8_t		sms_data_order; /* 0x00 or 0xff */
104 	uint8_t		sms_chksum;
105 } sm_section_hdr_t;
106 
107 /*
108  * sbd meta section identifiers
109  */
110 #define	SMS_ID_LU_INFO_1_0	0
111 #define	SMS_ID_LU_INFO_1_1	1
112 #define	SMS_ID_PGR_INFO		2
113 #define	SMS_ID_UNUSED		0x1000
114 
115 typedef struct sbd_lu_info_1_0 {
116 	sm_section_hdr_t	sli_sms_header;
117 	uint64_t		sli_total_store_size;
118 	uint64_t		sli_total_meta_size;
119 	uint64_t		sli_lu_data_offset;
120 	uint64_t		sli_lu_data_size;
121 	uint32_t		sli_flags;
122 	uint16_t		sli_blocksize;
123 	uint8_t			sli_data_order;
124 	uint8_t			rsvd1;
125 	uint8_t			sli_lu_devid[20];
126 	uint32_t		rsvd2;
127 } sbd_lu_info_1_0_t;
128 
129 typedef struct sbd_lu_info_1_1 {
130 	sm_section_hdr_t	sli_sms_header;
131 	uint32_t		sli_flags;
132 	char			sli_rev[4];
133 	char			sli_vid[8];
134 	char			sli_pid[16];
135 	uint64_t		sli_lu_size;	/* Read capacity size */
136 
137 	/*
138 	 * Essetially zfs volume name for zvols to verify that the
139 	 * metadata is coming in from the correct zvol and not from a
140 	 * clone. Has no meaning in any other case.
141 	 */
142 	uint64_t		sli_meta_fname_offset;
143 
144 	/*
145 	 * Data filename or the media filename when the metadata is in
146 	 * a separate file. Its not needed if the metadata is shared
147 	 * with data as the user supplied name is the data filename.
148 	 */
149 	uint64_t		sli_data_fname_offset;
150 	uint64_t		sli_serial_offset;
151 	uint64_t		sli_alias_offset;
152 	uint8_t			sli_data_blocksize_shift;
153 	uint8_t			sli_data_order;
154 	uint8_t			sli_serial_size;
155 	uint8_t			sli_rsvd1;
156 	uint8_t			sli_device_id[20];
157 	uint64_t		sli_mgmt_url_offset;
158 	uint8_t			sli_rsvd2[248];
159 
160 	/*
161 	 * In case there is no separate meta, sli_meta_fname_offset wont
162 	 * be valid. The same is true for zfs based metadata. The data_fname
163 	 * is the zvol.
164 	 */
165 	uint8_t			sli_buf[8];
166 } sbd_lu_info_1_1_t;
167 
168 /*
169  * sli flags
170  */
171 #define	SLI_SEPARATE_META			0x0001
172 #define	SLI_WRITE_PROTECTED			0x0002
173 #define	SLI_VID_VALID				0x0004
174 #define	SLI_PID_VALID				0x0008
175 #define	SLI_REV_VALID				0x0010
176 #define	SLI_META_FNAME_VALID			0x0020
177 #define	SLI_DATA_FNAME_VALID			0x0040
178 #define	SLI_SERIAL_VALID			0x0080
179 #define	SLI_ALIAS_VALID				0x0100
180 #define	SLI_WRITEBACK_CACHE_DISABLE		0x0200
181 #define	SLI_ZFS_META				0x0400
182 #define	SLI_MGMT_URL_VALID			0x0800
183 
184 struct sbd_it_data;
185 
186 typedef struct sbd_lu {
187 	struct sbd_lu	*sl_next;
188 	stmf_lu_t	*sl_lu;
189 	uint32_t	sl_alloc_size;
190 
191 	/* Current LU state */
192 	kmutex_t	sl_lock;
193 	uint32_t	sl_flags;
194 	uint8_t		sl_trans_op;
195 	uint8_t		sl_state:7,
196 			sl_state_not_acked:1;
197 
198 	char		*sl_name;		/* refers to meta or data */
199 
200 	/* Metadata */
201 	kmutex_t	sl_metadata_lock;
202 	krwlock_t	sl_access_state_lock;
203 	char		*sl_alias;
204 	char		*sl_meta_filename;	/* If applicable */
205 	char		*sl_mgmt_url;
206 	vnode_t		*sl_meta_vp;
207 	vtype_t		sl_meta_vtype;
208 	uint8_t		sl_device_id[20];	/* 4(hdr) + 16(GUID) */
209 	uint8_t		sl_meta_blocksize_shift; /* Left shift multiplier */
210 	uint8_t		sl_data_blocksize_shift;
211 	uint8_t		sl_data_fs_nbits;
212 	uint8_t		sl_serial_no_size;
213 	uint64_t	sl_total_meta_size;
214 	uint64_t	sl_meta_size_used;
215 	uint8_t		*sl_serial_no;		/* optional */
216 	char		sl_vendor_id[8];
217 	char		sl_product_id[16];
218 	char		sl_revision[4];
219 	uint32_t	sl_data_fname_alloc_size; /* for an explicit alloc */
220 	uint16_t	sl_alias_alloc_size;
221 	uint16_t	sl_mgmt_url_alloc_size;
222 	uint8_t		sl_serial_no_alloc_size;
223 	uint8_t		sl_access_state;
224 	uint64_t	sl_meta_offset;
225 
226 	/* zfs metadata */
227 	krwlock_t	sl_zfs_meta_lock;
228 	char		*sl_zfs_meta;
229 	minor_t		sl_zvol_minor;		/* for direct zvol calls */
230 	/* opaque handles for zvol direct calls */
231 	void		*sl_zvol_minor_hdl;
232 	void		*sl_zvol_objset_hdl;
233 	void		*sl_zvol_zil_hdl;
234 	void		*sl_zvol_rl_hdl;
235 	void		*sl_zvol_dn_hdl;
236 
237 	/* Backing store */
238 	char		*sl_data_filename;
239 	vnode_t		*sl_data_vp;
240 	vtype_t		sl_data_vtype;
241 	uint64_t	sl_total_data_size;
242 	uint64_t	sl_data_readable_size;	/* read() fails after this */
243 	uint64_t	sl_data_offset;		/* After the metadata,if any */
244 	uint64_t	sl_lu_size;		/* READ CAPACITY size */
245 	uint64_t	sl_blksize;		/* used for zvols */
246 	uint64_t	sl_max_xfer_len;	/* used for zvols */
247 
248 	struct sbd_it_data	*sl_it_list;
249 	struct sbd_pgr		*sl_pgr;
250 	uint64_t	sl_rs_owner_session_id;
251 	list_t		sl_ats_io_list;
252 } sbd_lu_t;
253 
254 /*
255  * sl_flags
256  */
257 #define	SL_LINKED			    0x00000001
258 #define	SL_META_OPENED			    0x00000002
259 #define	SL_REGISTERED			    0x00000004
260 #define	SL_META_NEEDS_FLUSH		    0x00000008
261 #define	SL_DATA_NEEDS_FLUSH		    0x00000010
262 #define	SL_VID_VALID			    0x00000020
263 #define	SL_PID_VALID			    0x00000040
264 #define	SL_REV_VALID			    0x00000080
265 #define	SL_WRITE_PROTECTED		    0x00000100
266 #define	SL_MEDIA_LOADED			    0x00000200
267 #define	SL_LU_HAS_SCSI2_RESERVATION	    0x00000400
268 #define	SL_WRITEBACK_CACHE_DISABLE	    0x00000800
269 #define	SL_SAVED_WRITE_CACHE_DISABLE	    0x00001000
270 #define	SL_MEDIUM_REMOVAL_PREVENTED	    0x00002000
271 #define	SL_NO_DATA_DKIOFLUSH		    0x00004000
272 #define	SL_SHARED_META			    0x00008000
273 #define	SL_ZFS_META			    0x00010000
274 #define	SL_WRITEBACK_CACHE_SET_UNSUPPORTED  0x00020000
275 #define	SL_FLUSH_ON_DISABLED_WRITECACHE	    0x00040000
276 #define	SL_CALL_ZVOL			    0x00080000
277 #define	SL_UNMAP_ENABLED		    0x00100000
278 
279 /*
280  * sl_trans_op. LU is undergoing some transition and this field
281  * tells what kind of transition that is.
282  */
283 #define	SL_OP_NONE				0
284 #define	SL_OP_CREATE_REGISTER_LU		1
285 #define	SL_OP_IMPORT_LU				2
286 #define	SL_OP_DELETE_LU				3
287 #define	SL_OP_MODIFY_LU				4
288 #define	SL_OP_LU_PROPS				5
289 
290 sbd_status_t sbd_data_read(sbd_lu_t *sl, scsi_task_t *task,
291     uint64_t offset, uint64_t size, uint8_t *buf);
292 sbd_status_t sbd_data_write(sbd_lu_t *sl, scsi_task_t *task,
293     uint64_t offset, uint64_t size, uint8_t *buf);
294 stmf_status_t sbd_task_alloc(struct scsi_task *task);
295 void sbd_new_task(struct scsi_task *task, struct stmf_data_buf *initial_dbuf);
296 void sbd_dbuf_xfer_done(struct scsi_task *task, struct stmf_data_buf *dbuf);
297 void sbd_send_status_done(struct scsi_task *task);
298 void sbd_task_free(struct scsi_task *task);
299 stmf_status_t sbd_abort(struct stmf_lu *lu, int abort_cmd, void *arg,
300     uint32_t flags);
301 void sbd_ctl(struct stmf_lu *lu, int cmd, void *arg);
302 stmf_status_t sbd_info(uint32_t cmd, stmf_lu_t *lu, void *arg, uint8_t *buf,
303     uint32_t *bufsizep);
304 sbd_status_t sbd_write_lu_info(sbd_lu_t *sl);
305 sbd_status_t sbd_flush_data_cache(sbd_lu_t *sl, int fsync_done);
306 sbd_status_t sbd_wcd_set(int wcd, sbd_lu_t *sl);
307 void sbd_wcd_get(int *wcd, sbd_lu_t *sl);
308 int sbd_unmap(sbd_lu_t *sl, dkioc_free_list_t *dfl);
309 
310 void sbd_handle_short_write_transfers(scsi_task_t *, stmf_data_buf_t *,
311     uint32_t);
312 void sbd_handle_short_read_transfers(scsi_task_t *, stmf_data_buf_t *,
313     uint8_t *, uint32_t, uint32_t);
314 
315 #ifdef	__cplusplus
316 }
317 #endif
318 
319 #endif /* _STMF_SBD_H */
320