xref: /freebsd/sys/dev/hyperv/storvsc/hv_vstorage.h (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2009-2012,2017 Microsoft Corp.
5  * Copyright (c) 2012 NetApp Inc.
6  * Copyright (c) 2012 Citrix Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice unmodified, this list of conditions, and the following
14  *    disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef __HV_VSTORAGE_H__
32 #define __HV_VSTORAGE_H__
33 
34 /*
35  * Major/minor macros.  Minor version is in LSB, meaning that earlier flat
36  * version numbers will be interpreted as "0.x" (i.e., 1 becomes 0.1).
37  */
38 
39 #define VMSTOR_PROTOCOL_MAJOR(VERSION_)         (((VERSION_) >> 8) & 0xff)
40 #define VMSTOR_PROTOCOL_MINOR(VERSION_)         (((VERSION_)     ) & 0xff)
41 #define VMSTOR_PROTOCOL_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \
42                                                  (((MINOR_) & 0xff)     ))
43 
44 #define VMSTOR_PROTOCOL_VERSION_WIN6       VMSTOR_PROTOCOL_VERSION(2, 0)
45 #define VMSTOR_PROTOCOL_VERSION_WIN7       VMSTOR_PROTOCOL_VERSION(4, 2)
46 #define VMSTOR_PROTOCOL_VERSION_WIN8       VMSTOR_PROTOCOL_VERSION(5, 1)
47 #define VMSTOR_PROTOCOL_VERSION_WIN8_1     VMSTOR_PROTOCOL_VERSION(6, 0)
48 #define VMSTOR_PROTOCOL_VERSION_WIN10      VMSTOR_PROTOCOL_VERSION(6, 2)
49 /*
50  * Invalid version.
51  */
52 #define VMSTOR_INVALID_PROTOCOL_VERSION  -1
53 
54 /*
55  * Version history:
56  * V1 Beta                    0.1
57  * V1 RC < 2008/1/31          1.0
58  * V1 RC > 2008/1/31          2.0
59  * Win7: 4.2
60  * Win8: 5.1
61  */
62 
63 #define VMSTOR_PROTOCOL_VERSION_CURRENT	VMSTOR_PROTOCOL_VERSION(5, 1)
64 
65 /**
66  *  Packet structure ops describing virtual storage requests.
67  */
68 enum vstor_packet_ops {
69 	VSTOR_OPERATION_COMPLETEIO            = 1,
70 	VSTOR_OPERATION_REMOVEDEVICE          = 2,
71 	VSTOR_OPERATION_EXECUTESRB            = 3,
72 	VSTOR_OPERATION_RESETLUN              = 4,
73 	VSTOR_OPERATION_RESETADAPTER          = 5,
74 	VSTOR_OPERATION_RESETBUS              = 6,
75 	VSTOR_OPERATION_BEGININITIALIZATION   = 7,
76 	VSTOR_OPERATION_ENDINITIALIZATION     = 8,
77 	VSTOR_OPERATION_QUERYPROTOCOLVERSION  = 9,
78 	VSTOR_OPERATION_QUERYPROPERTIES       = 10,
79 	VSTOR_OPERATION_ENUMERATE_BUS         = 11,
80 	VSTOR_OPERATION_FCHBA_DATA            = 12,
81 	VSTOR_OPERATION_CREATE_MULTI_CHANNELS = 13,
82 	VSTOR_OPERATION_MAXIMUM               = 13
83 };
84 
85 
86 /*
87  *  Platform neutral description of a scsi request -
88  *  this remains the same across the write regardless of 32/64 bit
89  *  note: it's patterned off the Windows DDK SCSI_PASS_THROUGH structure
90  */
91 
92 #define CDB16GENERIC_LENGTH			0x10
93 #define SENSE_BUFFER_SIZE			0x14
94 #define MAX_DATA_BUFFER_LENGTH_WITH_PADDING	0x14
95 
96 #define POST_WIN7_STORVSC_SENSE_BUFFER_SIZE	0x14
97 #define PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE	0x12
98 
99 
100 struct vmscsi_win8_extension {
101 	/*
102 	 * The following were added in Windows 8
103 	 */
104 	uint16_t reserve;
105 	uint8_t  queue_tag;
106 	uint8_t  queue_action;
107 	uint32_t srb_flags;
108 	uint32_t time_out_value;
109 	uint32_t queue_sort_ey;
110 } __packed;
111 
112 struct vmscsi_req {
113 	uint16_t length;
114 	uint8_t  srb_status;
115 	uint8_t  scsi_status;
116 
117 	/* HBA number, set to the order number detected by initiator. */
118 	uint8_t  port;
119 	/* SCSI bus number or bus_id, different from CAM's path_id. */
120 	uint8_t  path_id;
121 
122 	uint8_t  target_id;
123 	uint8_t  lun;
124 
125 	uint8_t  cdb_len;
126 	uint8_t  sense_info_len;
127 	uint8_t  data_in;
128 	uint8_t  reserved;
129 
130 	uint32_t transfer_len;
131 
132 	union {
133 	    uint8_t cdb[CDB16GENERIC_LENGTH];
134 
135 	    uint8_t sense_data[SENSE_BUFFER_SIZE];
136 
137 	    uint8_t reserved_array[MAX_DATA_BUFFER_LENGTH_WITH_PADDING];
138 	} u;
139 
140 	/*
141 	 * The following was added in win8.
142 	 */
143 	struct vmscsi_win8_extension win8_extension;
144 
145 } __packed;
146 
147 /**
148  *  This structure is sent during the initialization phase to get the different
149  *  properties of the channel.
150  */
151 
152 struct vmstor_chan_props {
153 	uint16_t proto_ver;
154 	uint8_t  path_id;
155 	uint8_t  target_id;
156 
157 	uint16_t max_channel_cnt;
158 
159 	/**
160 	 * Note: port number is only really known on the client side
161 	 */
162 	uint16_t port;
163 	uint32_t flags;
164 	uint32_t max_transfer_bytes;
165 
166 	/**
167 	 *  This id is unique for each channel and will correspond with
168 	 *  vendor specific data in the inquiry_ata
169 	 */
170 	uint64_t unique_id;
171 
172 } __packed;
173 
174 /**
175  *  This structure is sent during the storage protocol negotiations.
176  */
177 
178 struct vmstor_proto_ver
179 {
180 	/**
181 	 * Major (MSW) and minor (LSW) version numbers.
182 	 */
183 	uint16_t major_minor;
184 
185 	uint16_t revision;			/* always zero */
186 } __packed;
187 
188 /**
189  * Channel Property Flags
190  */
191 
192 #define STORAGE_CHANNEL_REMOVABLE_FLAG                  0x1
193 #define STORAGE_CHANNEL_EMULATED_IDE_FLAG               0x2
194 
195 
196 struct vstor_packet {
197 	/**
198 	 * Requested operation type
199 	 */
200 	enum vstor_packet_ops operation;
201 
202 	/*
203 	 * Flags - see below for values
204 	 */
205 	uint32_t flags;
206 
207 	/**
208 	 * Status of the request returned from the server side.
209 	 */
210 	uint32_t status;
211 
212 	union
213 	{
214 	    /**
215 	     * Structure used to forward SCSI commands from the client to
216 	     * the server.
217 	     */
218 	    struct vmscsi_req vm_srb;
219 
220 	    /**
221 	     * Structure used to query channel properties.
222 	     */
223 	    struct vmstor_chan_props chan_props;
224 
225 	    /**
226 	     * Used during version negotiations.
227 	     */
228 	    struct vmstor_proto_ver version;
229 
230 	    /**
231              * Number of multichannels to create
232 	     */
233 	    uint16_t multi_channels_cnt;
234 	} u;
235 
236 } __packed;
237 
238 
239 /**
240  * SRB (SCSI Request Block) Status Codes
241  */
242 #define SRB_STATUS_PENDING                  0x00
243 #define SRB_STATUS_SUCCESS                  0x01
244 #define SRB_STATUS_ABORTED                  0x02
245 #define SRB_STATUS_ABORT_FAILED             0x03
246 #define SRB_STATUS_ERROR                    0x04
247 #define SRB_STATUS_BUSY                     0x05
248 #define SRB_STATUS_INVALID_REQUEST          0x06
249 #define SRB_STATUS_INVALID_PATH_ID          0x07
250 #define SRB_STATUS_NO_DEVICE                0x08
251 #define SRB_STATUS_TIMEOUT                  0x09
252 #define SRB_STATUS_SELECTION_TIMEOUT        0x0A
253 #define SRB_STATUS_COMMAND_TIMEOUT          0x0B
254 #define SRB_STATUS_MESSAGE_REJECTED         0x0D
255 #define SRB_STATUS_BUS_RESET                0x0E
256 #define SRB_STATUS_PARITY_ERROR             0x0F
257 #define SRB_STATUS_REQUEST_SENSE_FAILED     0x10
258 #define SRB_STATUS_NO_HBA                   0x11
259 #define SRB_STATUS_DATA_OVERRUN             0x12
260 #define SRB_STATUS_UNEXPECTED_BUS_FREE      0x13
261 #define SRB_STATUS_PHASE_SEQUENCE_FAILURE   0x14
262 #define SRB_STATUS_BAD_SRB_BLOCK_LENGTH     0x15
263 #define SRB_STATUS_REQUEST_FLUSHED          0x16
264 #define SRB_STATUS_INVALID_LUN              0x20
265 #define SRB_STATUS_INVALID_TARGET_ID        0x21
266 #define SRB_STATUS_BAD_FUNCTION             0x22
267 #define SRB_STATUS_ERROR_RECOVERY           0x23
268 #define SRB_STATUS_NOT_POWERED              0x24
269 #define SRB_STATUS_LINK_DOWN                0x25
270 /**
271  * SRB Status Masks (can be combined with above status codes)
272  */
273 #define SRB_STATUS_QUEUE_FROZEN         0x40
274 #define SRB_STATUS_AUTOSENSE_VALID      0x80
275 
276 #define SRB_STATUS(status)	\
277 	((status) & ~(SRB_STATUS_AUTOSENSE_VALID | SRB_STATUS_QUEUE_FROZEN))
278 /*
279  * SRB Flag Bits
280  */
281 
282 #define SRB_FLAGS_QUEUE_ACTION_ENABLE           0x00000002
283 #define SRB_FLAGS_DISABLE_DISCONNECT            0x00000004
284 #define SRB_FLAGS_DISABLE_SYNCH_TRANSFER        0x00000008
285 #define SRB_FLAGS_BYPASS_FROZEN_QUEUE           0x00000010
286 #define SRB_FLAGS_DISABLE_AUTOSENSE             0x00000020
287 #define SRB_FLAGS_DATA_IN                       0x00000040
288 #define SRB_FLAGS_DATA_OUT                      0x00000080
289 #define SRB_FLAGS_NO_DATA_TRANSFER              0x00000000
290 #define SRB_FLAGS_UNSPECIFIED_DIRECTION (SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT)
291 #define SRB_FLAGS_NO_QUEUE_FREEZE               0x00000100
292 #define SRB_FLAGS_ADAPTER_CACHE_ENABLE          0x00000200
293 #define SRB_FLAGS_FREE_SENSE_BUFFER             0x00000400
294 /**
295  *  Packet flags
296  */
297 
298 /**
299  *  This flag indicates that the server should send back a completion for this
300  *  packet.
301  */
302 #define REQUEST_COMPLETION_FLAG	0x1
303 
304 /**
305  *  This is the set of flags that the vsc can set in any packets it sends
306  */
307 #define VSC_LEGAL_FLAGS (REQUEST_COMPLETION_FLAG)
308 
309 #endif /* __HV_VSTORAGE_H__ */
310