1251767Sgibbs/******************************************************************************
2251767Sgibbs * vscsiif.h
3288917Sroyger *
4251767Sgibbs * Based on the blkif.h code.
5288917Sroyger *
6251767Sgibbs * Permission is hereby granted, free of charge, to any person obtaining a copy
7251767Sgibbs * of this software and associated documentation files (the "Software"), to
8251767Sgibbs * deal in the Software without restriction, including without limitation the
9251767Sgibbs * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10251767Sgibbs * sell copies of the Software, and to permit persons to whom the Software is
11251767Sgibbs * furnished to do so, subject to the following conditions:
12251767Sgibbs *
13251767Sgibbs * The above copyright notice and this permission notice shall be included in
14251767Sgibbs * all copies or substantial portions of the Software.
15251767Sgibbs *
16251767Sgibbs * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17251767Sgibbs * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18251767Sgibbs * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19251767Sgibbs * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20251767Sgibbs * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21251767Sgibbs * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22251767Sgibbs * DEALINGS IN THE SOFTWARE.
23251767Sgibbs *
24251767Sgibbs * Copyright(c) FUJITSU Limited 2008.
25251767Sgibbs */
26251767Sgibbs
27251767Sgibbs#ifndef __XEN__PUBLIC_IO_SCSI_H__
28251767Sgibbs#define __XEN__PUBLIC_IO_SCSI_H__
29251767Sgibbs
30251767Sgibbs#include "ring.h"
31251767Sgibbs#include "../grant_table.h"
32251767Sgibbs
33288917Sroyger/*
34288917Sroyger * Feature and Parameter Negotiation
35288917Sroyger * =================================
36288917Sroyger * The two halves of a Xen pvSCSI driver utilize nodes within the XenStore to
37288917Sroyger * communicate capabilities and to negotiate operating parameters.  This
38288917Sroyger * section enumerates these nodes which reside in the respective front and
39288917Sroyger * backend portions of the XenStore, following the XenBus convention.
40288917Sroyger *
41288917Sroyger * Any specified default value is in effect if the corresponding XenBus node
42288917Sroyger * is not present in the XenStore.
43288917Sroyger *
44288917Sroyger * XenStore nodes in sections marked "PRIVATE" are solely for use by the
45288917Sroyger * driver side whose XenBus tree contains them.
46288917Sroyger *
47288917Sroyger *****************************************************************************
48288917Sroyger *                            Backend XenBus Nodes
49288917Sroyger *****************************************************************************
50288917Sroyger *
51288917Sroyger *------------------ Backend Device Identification (PRIVATE) ------------------
52288917Sroyger *
53288917Sroyger * p-devname
54288917Sroyger *      Values:         string
55288917Sroyger *
56288917Sroyger *      A free string used to identify the physical device (e.g. a disk name).
57288917Sroyger *
58288917Sroyger * p-dev
59288917Sroyger *      Values:         string
60288917Sroyger *
61288917Sroyger *      A string specifying the backend device: either a 4-tuple "h:c:t:l"
62288917Sroyger *      (host, controller, target, lun, all integers), or a WWN (e.g.
63288917Sroyger *      "naa.60014054ac780582").
64288917Sroyger *
65288917Sroyger * v-dev
66288917Sroyger *      Values:         string
67288917Sroyger *
68288917Sroyger *      A string specifying the frontend device in form of a 4-tuple "h:c:t:l"
69288917Sroyger *      (host, controller, target, lun, all integers).
70288917Sroyger *
71288917Sroyger *--------------------------------- Features ---------------------------------
72288917Sroyger *
73288917Sroyger * feature-sg-grant
74288917Sroyger *      Values:         unsigned [VSCSIIF_SG_TABLESIZE...65535]
75288917Sroyger *      Default Value:  0
76288917Sroyger *
77288917Sroyger *      Specifies the maximum number of scatter/gather elements in grant pages
78288917Sroyger *      supported. If not set, the backend supports up to VSCSIIF_SG_TABLESIZE
79288917Sroyger *      SG elements specified directly in the request.
80288917Sroyger *
81288917Sroyger *****************************************************************************
82288917Sroyger *                            Frontend XenBus Nodes
83288917Sroyger *****************************************************************************
84288917Sroyger *
85288917Sroyger *----------------------- Request Transport Parameters -----------------------
86288917Sroyger *
87288917Sroyger * event-channel
88288917Sroyger *      Values:         unsigned
89288917Sroyger *
90288917Sroyger *      The identifier of the Xen event channel used to signal activity
91288917Sroyger *      in the ring buffer.
92288917Sroyger *
93288917Sroyger * ring-ref
94288917Sroyger *      Values:         unsigned
95288917Sroyger *
96288917Sroyger *      The Xen grant reference granting permission for the backend to map
97288917Sroyger *      the sole page in a single page sized ring buffer.
98288917Sroyger *
99288917Sroyger * protocol
100288917Sroyger *      Values:         string (XEN_IO_PROTO_ABI_*)
101288917Sroyger *      Default Value:  XEN_IO_PROTO_ABI_NATIVE
102288917Sroyger *
103288917Sroyger *      The machine ABI rules governing the format of all ring request and
104288917Sroyger *      response structures.
105288917Sroyger */
106251767Sgibbs
107288917Sroyger/* Requests from the frontend to the backend */
108251767Sgibbs
109288917Sroyger/*
110288917Sroyger * Request a SCSI operation specified via a CDB in vscsiif_request.cmnd.
111288917Sroyger * The target is specified via channel, id and lun.
112288917Sroyger *
113288917Sroyger * The operation to be performed is specified via a CDB in cmnd[], the length
114288917Sroyger * of the CDB is in cmd_len. sc_data_direction specifies the direction of data
115288917Sroyger * (to the device, from the device, or none at all).
116288917Sroyger *
117288917Sroyger * If data is to be transferred to or from the device the buffer(s) in the
118288917Sroyger * guest memory is/are specified via one or multiple scsiif_request_segment
119288917Sroyger * descriptors each specifying a memory page via a grant_ref_t, a offset into
120288917Sroyger * the page and the length of the area in that page. All scsiif_request_segment
121288917Sroyger * areas concatenated form the resulting data buffer used by the operation.
122288917Sroyger * If the number of scsiif_request_segment areas is not too large (less than
123288917Sroyger * or equal VSCSIIF_SG_TABLESIZE) the areas can be specified directly in the
124288917Sroyger * seg[] array and the number of valid scsiif_request_segment elements is to be
125288917Sroyger * set in nr_segments.
126288917Sroyger *
127288917Sroyger * If "feature-sg-grant" in the Xenstore is set it is possible to specify more
128288917Sroyger * than VSCSIIF_SG_TABLESIZE scsiif_request_segment elements via indirection.
129288917Sroyger * The maximum number of allowed scsiif_request_segment elements is the value
130288917Sroyger * of the "feature-sg-grant" entry from Xenstore. When using indirection the
131288917Sroyger * seg[] array doesn't contain specifications of the data buffers, but
132288917Sroyger * references to scsiif_request_segment arrays, which in turn reference the
133288917Sroyger * data buffers. While nr_segments holds the number of populated seg[] entries
134288917Sroyger * (plus the set VSCSIIF_SG_GRANT bit), the number of scsiif_request_segment
135288917Sroyger * elements referencing the target data buffers is calculated from the lengths
136288917Sroyger * of the seg[] elements (the sum of all valid seg[].length divided by the
137288917Sroyger * size of one scsiif_request_segment structure). The frontend may use a mix of
138288917Sroyger * direct and indirect requests.
139288917Sroyger */
140288917Sroyger#define VSCSIIF_ACT_SCSI_CDB         1
141251767Sgibbs
142251767Sgibbs/*
143288917Sroyger * Request abort of a running operation for the specified target given by
144288917Sroyger * channel, id, lun and the operation's rqid in ref_rqid.
145288917Sroyger */
146288917Sroyger#define VSCSIIF_ACT_SCSI_ABORT       2
147288917Sroyger
148288917Sroyger/*
149288917Sroyger * Request a device reset of the specified target (channel and id).
150288917Sroyger */
151288917Sroyger#define VSCSIIF_ACT_SCSI_RESET       3
152288917Sroyger
153288917Sroyger/*
154288917Sroyger * Preset scatter/gather elements for a following request. Deprecated.
155288917Sroyger * Keeping the define only to avoid usage of the value "4" for other actions.
156288917Sroyger */
157288917Sroyger#define VSCSIIF_ACT_SCSI_SG_PRESET   4
158288917Sroyger
159288917Sroyger/*
160251767Sgibbs * Maximum scatter/gather segments per request.
161251767Sgibbs *
162288917Sroyger * Considering balance between allocating at least 16 "vscsiif_request"
163288917Sroyger * structures on one page (4096 bytes) and the number of scatter/gather
164288917Sroyger * elements needed, we decided to use 26 as a magic number.
165288917Sroyger *
166288917Sroyger * If "feature-sg-grant" is set, more scatter/gather elements can be specified
167288917Sroyger * by placing them in one or more (up to VSCSIIF_SG_TABLESIZE) granted pages.
168288917Sroyger * In this case the vscsiif_request seg elements don't contain references to
169288917Sroyger * the user data, but to the SG elements referencing the user data.
170251767Sgibbs */
171251767Sgibbs#define VSCSIIF_SG_TABLESIZE             26
172251767Sgibbs
173251767Sgibbs/*
174288917Sroyger * based on Linux kernel 2.6.18, still valid
175288917Sroyger *
176288917Sroyger * Changing these values requires support of multiple protocols via the rings
177288917Sroyger * as "old clients" will blindly use these values and the resulting structure
178288917Sroyger * sizes.
179251767Sgibbs */
180251767Sgibbs#define VSCSIIF_MAX_COMMAND_SIZE         16
181251767Sgibbs#define VSCSIIF_SENSE_BUFFERSIZE         96
182251767Sgibbs
183288917Sroygerstruct scsiif_request_segment {
184288917Sroyger    grant_ref_t gref;
185288917Sroyger    uint16_t offset;
186288917Sroyger    uint16_t length;
187288917Sroyger};
188288917Sroygertypedef struct scsiif_request_segment vscsiif_segment_t;
189251767Sgibbs
190288917Sroyger#define VSCSIIF_SG_PER_PAGE (PAGE_SIZE / sizeof(struct scsiif_request_segment))
191288917Sroyger
192288917Sroyger/* Size of one request is 252 bytes */
193251767Sgibbsstruct vscsiif_request {
194251767Sgibbs    uint16_t rqid;          /* private guest value, echoed in resp  */
195251767Sgibbs    uint8_t act;            /* command between backend and frontend */
196288917Sroyger    uint8_t cmd_len;        /* valid CDB bytes */
197251767Sgibbs
198288917Sroyger    uint8_t cmnd[VSCSIIF_MAX_COMMAND_SIZE]; /* the CDB */
199288917Sroyger    uint16_t timeout_per_command;   /* deprecated: timeout in secs, 0=default */
200288917Sroyger    uint16_t channel, id, lun;      /* (virtual) device specification */
201288917Sroyger    uint16_t ref_rqid;              /* command abort reference */
202288917Sroyger    uint8_t sc_data_direction;      /* for DMA_TO_DEVICE(1)
203288917Sroyger                                       DMA_FROM_DEVICE(2)
204288917Sroyger                                       DMA_NONE(3) requests  */
205288917Sroyger    uint8_t nr_segments;            /* Number of pieces of scatter-gather */
206288917Sroyger/*
207288917Sroyger * flag in nr_segments: SG elements via grant page
208288917Sroyger *
209288917Sroyger * If VSCSIIF_SG_GRANT is set, the low 7 bits of nr_segments specify the number
210288917Sroyger * of grant pages containing SG elements. Usable if "feature-sg-grant" set.
211288917Sroyger */
212288917Sroyger#define VSCSIIF_SG_GRANT    0x80
213251767Sgibbs
214288917Sroyger    vscsiif_segment_t seg[VSCSIIF_SG_TABLESIZE];
215251767Sgibbs    uint32_t reserved[3];
216251767Sgibbs};
217251767Sgibbstypedef struct vscsiif_request vscsiif_request_t;
218251767Sgibbs
219288917Sroyger/*
220288917Sroyger * The following interface is deprecated!
221288917Sroyger */
222288917Sroyger#define VSCSIIF_SG_LIST_SIZE ((sizeof(vscsiif_request_t) - 4) \
223288917Sroyger                              / sizeof(vscsiif_segment_t))
224288917Sroyger
225288917Sroygerstruct vscsiif_sg_list {
226288917Sroyger    /* First two fields must match struct vscsiif_request! */
227288917Sroyger    uint16_t rqid;          /* private guest value, must match main req */
228288917Sroyger    uint8_t act;            /* VSCSIIF_ACT_SCSI_SG_PRESET */
229288917Sroyger    uint8_t nr_segments;    /* Number of pieces of scatter-gather */
230288917Sroyger    vscsiif_segment_t seg[VSCSIIF_SG_LIST_SIZE];
231288917Sroyger};
232288917Sroygertypedef struct vscsiif_sg_list vscsiif_sg_list_t;
233288917Sroyger/* End of deprecated interface */
234288917Sroyger
235288917Sroyger/* Size of one response is 252 bytes */
236251767Sgibbsstruct vscsiif_response {
237288917Sroyger    uint16_t rqid;          /* identifies request */
238288917Sroyger    uint8_t act;            /* deprecated: valid only if SG_PRESET supported */
239251767Sgibbs    uint8_t sense_len;
240251767Sgibbs    uint8_t sense_buffer[VSCSIIF_SENSE_BUFFERSIZE];
241251767Sgibbs    int32_t rslt;
242251767Sgibbs    uint32_t residual_len;     /* request bufflen -
243251767Sgibbs                                  return the value from physical device */
244251767Sgibbs    uint32_t reserved[36];
245251767Sgibbs};
246251767Sgibbstypedef struct vscsiif_response vscsiif_response_t;
247251767Sgibbs
248251767SgibbsDEFINE_RING_TYPES(vscsiif, struct vscsiif_request, struct vscsiif_response);
249251767Sgibbs
250251767Sgibbs
251251767Sgibbs#endif  /*__XEN__PUBLIC_IO_SCSI_H__*/
252251767Sgibbs/*
253251767Sgibbs * Local variables:
254251767Sgibbs * mode: C
255288917Sroyger * c-file-style: "BSD"
256251767Sgibbs * c-basic-offset: 4
257251767Sgibbs * tab-width: 4
258251767Sgibbs * indent-tabs-mode: nil
259251767Sgibbs * End:
260251767Sgibbs */
261