3
* Copyright (c) 2011, Microsoft Corporation.
5
* This program is free software; you can redistribute it and/or modify it
6
* under the terms and conditions of the GNU General Public License,
7
* version 2, as published by the Free Software Foundation.
9
* This program is distributed in the hope it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14
* You should have received a copy of the GNU General Public License along with
15
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16
* Place - Suite 330, Boston, MA 02111-1307 USA.
19
* Haiyang Zhang <haiyangz@microsoft.com>
20
* Hank Janssen <hjanssen@microsoft.com>
21
* K. Y. Srinivasan <kys@microsoft.com>
25
#ifndef _HYPERV_STORAGE_H
26
#define _HYPERV_STORAGE_H
29
/* vstorage.w revision number. This is used in the case of a version match, */
30
/* to alert the user that structure sizes may be mismatched even though the */
31
/* protocol versions match. */
34
#define REVISION_STRING(REVISION_) #REVISION_
35
#define FILL_VMSTOR_REVISION(RESULT_LVALUE_) \
37
char *revision_string \
38
= REVISION_STRING($Rev : 6 $) + 6; \
40
while (*revision_string >= '0' \
41
&& *revision_string <= '9') { \
42
RESULT_LVALUE_ *= 10; \
43
RESULT_LVALUE_ += *revision_string - '0'; \
48
/* Major/minor macros. Minor version is in LSB, meaning that earlier flat */
49
/* version numbers will be interpreted as "0.x" (i.e., 1 becomes 0.1). */
50
#define VMSTOR_PROTOCOL_MAJOR(VERSION_) (((VERSION_) >> 8) & 0xff)
51
#define VMSTOR_PROTOCOL_MINOR(VERSION_) (((VERSION_)) & 0xff)
52
#define VMSTOR_PROTOCOL_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \
54
#define VMSTOR_INVALID_PROTOCOL_VERSION (-1)
56
/* Version history: */
58
/* V1 RC < 2008/1/31 1.0 */
59
/* V1 RC > 2008/1/31 2.0 */
60
#define VMSTOR_PROTOCOL_VERSION_CURRENT VMSTOR_PROTOCOL_VERSION(2, 0)
65
/* This will get replaced with the max transfer length that is possible on */
66
/* the host adapter. */
67
/* The max transfer length will be published when we offer a vmbus channel. */
68
#define MAX_TRANSFER_LENGTH 0x40000
69
#define DEFAULT_PACKET_SIZE (sizeof(struct vmdata_gpa_direct) + \
70
sizeof(struct vstor_packet) + \
71
sizesizeof(u64) * (MAX_TRANSFER_LENGTH / PAGE_SIZE)))
74
/* Packet structure describing virtual storage requests. */
75
enum vstor_packet_operation {
76
VSTOR_OPERATION_COMPLETE_IO = 1,
77
VSTOR_OPERATION_REMOVE_DEVICE = 2,
78
VSTOR_OPERATION_EXECUTE_SRB = 3,
79
VSTOR_OPERATION_RESET_LUN = 4,
80
VSTOR_OPERATION_RESET_ADAPTER = 5,
81
VSTOR_OPERATION_RESET_BUS = 6,
82
VSTOR_OPERATION_BEGIN_INITIALIZATION = 7,
83
VSTOR_OPERATION_END_INITIALIZATION = 8,
84
VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9,
85
VSTOR_OPERATION_QUERY_PROPERTIES = 10,
86
VSTOR_OPERATION_MAXIMUM = 10
90
* Platform neutral description of a scsi request -
91
* this remains the same across the write regardless of 32/64 bit
92
* note: it's patterned off the SCSI_PASS_THROUGH structure
94
#define CDB16GENERIC_LENGTH 0x10
96
#ifndef SENSE_BUFFER_SIZE
97
#define SENSE_BUFFER_SIZE 0x12
100
#define MAX_DATA_BUF_LEN_WITH_PADDING 0x14
102
struct vmscsi_request {
103
unsigned short length;
104
unsigned char srb_status;
105
unsigned char scsi_status;
107
unsigned char port_number;
108
unsigned char path_id;
109
unsigned char target_id;
112
unsigned char cdb_length;
113
unsigned char sense_info_length;
114
unsigned char data_in;
115
unsigned char reserved;
117
unsigned int data_transfer_length;
120
unsigned char cdb[CDB16GENERIC_LENGTH];
121
unsigned char sense_data[SENSE_BUFFER_SIZE];
122
unsigned char reserved_array[MAX_DATA_BUF_LEN_WITH_PADDING];
124
} __attribute((packed));
128
* This structure is sent during the intialization phase to get the different
129
* properties of the channel.
131
struct vmstorage_channel_properties {
132
unsigned short protocol_version;
133
unsigned char path_id;
134
unsigned char target_id;
136
/* Note: port number is only really known on the client side */
137
unsigned int port_number;
139
unsigned int max_transfer_bytes;
141
/* This id is unique for each channel and will correspond with */
142
/* vendor specific data in the inquirydata */
143
unsigned long long unique_id;
146
/* This structure is sent during the storage protocol negotiations. */
147
struct vmstorage_protocol_version {
148
/* Major (MSW) and minor (LSW) version numbers. */
149
unsigned short major_minor;
152
* Revision number is auto-incremented whenever this file is changed
153
* (See FILL_VMSTOR_REVISION macro above). Mismatch does not
154
* definitely indicate incompatibility--but it does indicate mismatched
157
unsigned short revision;
160
/* Channel Property Flags */
161
#define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
162
#define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
164
struct vstor_packet {
165
/* Requested operation type */
166
enum vstor_packet_operation operation;
168
/* Flags - see below for values */
171
/* Status of the request returned from the server side. */
174
/* Data payload area */
177
* Structure used to forward SCSI commands from the
178
* client to the server.
180
struct vmscsi_request vm_srb;
182
/* Structure used to query channel properties. */
183
struct vmstorage_channel_properties storage_channel_properties;
185
/* Used during version negotiations. */
186
struct vmstorage_protocol_version version;
192
* This flag indicates that the server should send back a completion for this
195
#define REQUEST_COMPLETION_FLAG 0x1
197
/* This is the set of flags that the vsc can set in any packets it sends */
198
#define VSC_LEGAL_FLAGS (REQUEST_COMPLETION_FLAG)
201
#include <linux/kernel.h>
202
#include <linux/wait.h>
203
#include "hyperv_storage.h"
207
#define STORVSC_RING_BUFFER_SIZE (20*PAGE_SIZE)
208
#define BLKVSC_RING_BUFFER_SIZE (20*PAGE_SIZE)
210
#define STORVSC_MAX_IO_REQUESTS 128
213
* In Hyper-V, each port/path/target maps to 1 scsi host adapter. In
214
* reality, the path/target is not used (ie always set to 0) so our
215
* scsi host adapter essentially has 1 bus with 1 target that contains
218
#define STORVSC_MAX_LUNS_PER_TARGET 64
219
#define STORVSC_MAX_TARGETS 1
220
#define STORVSC_MAX_CHANNELS 1
222
struct hv_storvsc_request;
224
/* Matches Windows-end */
225
enum storvsc_request_type {
232
struct hv_storvsc_request {
233
struct hv_storvsc_request *request;
234
struct hv_device *device;
236
/* Synchronize the request/response if needed */
237
struct completion wait_event;
239
unsigned char *sense_buffer;
241
void (*on_io_completion)(struct hv_storvsc_request *request);
242
struct hv_multipage_buffer data_buffer;
244
struct vstor_packet vstor_packet;
248
struct storvsc_device_info {
249
u32 ring_buffer_size;
250
unsigned int port_number;
251
unsigned char path_id;
252
unsigned char target_id;
255
struct storvsc_major_info {
263
/* A storvsc device is a device object that contains a vmbus channel */
264
struct storvsc_device {
265
struct hv_device *device;
267
/* 0 indicates the device is being destroyed */
271
atomic_t num_outstanding_req;
273
wait_queue_head_t waiting_to_drain;
276
* Each unique Port/Path/Target represents 1 channel ie scsi
277
* controller. In reality, the pathid, targetid is always 0
278
* and the port is set by us
280
unsigned int port_number;
281
unsigned char path_id;
282
unsigned char target_id;
284
/* Used for vsc/vsp channel reset process */
285
struct hv_storvsc_request init_request;
286
struct hv_storvsc_request reset_request;
290
/* Get the stordevice object iff exists and its refcount > 1 */
291
static inline struct storvsc_device *get_stor_device(struct hv_device *device)
293
struct storvsc_device *stor_device;
295
stor_device = (struct storvsc_device *)device->ext;
296
if (stor_device && atomic_read(&stor_device->ref_count) > 1)
297
atomic_inc(&stor_device->ref_count);
305
static inline void put_stor_device(struct hv_device *device)
307
struct storvsc_device *stor_device;
309
stor_device = (struct storvsc_device *)device->ext;
311
atomic_dec(&stor_device->ref_count);
314
static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
316
dev->drain_notify = true;
317
wait_event(dev->waiting_to_drain,
318
atomic_read(&dev->num_outstanding_req) == 0);
319
dev->drain_notify = false;
324
int storvsc_dev_add(struct hv_device *device,
325
void *additional_info);
326
int storvsc_dev_remove(struct hv_device *device);
328
int storvsc_do_io(struct hv_device *device,
329
struct hv_storvsc_request *request);
331
int storvsc_get_major_info(struct storvsc_device_info *device_info,
332
struct storvsc_major_info *major_info);
334
#endif /* _HYPERV_STORAGE_H */