master
h 448 lines 14.8 KB
Raw
1 /*
2 * VMware PVSCSI header file
3 *
4 * Copyright (C) 2008-2009, VMware, Inc. All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; version 2 of the License and no later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more
14 * details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see
18 * <https://www.gnu.org/licenses/>.
19 *
20 * Maintained by: Arvind Kumar <arvindkumar@vmware.com>
21 *
22 */
23
24 #ifndef VMW_PVSCSI_H
25 #define VMW_PVSCSI_H
26
27 #define VMW_PAGE_SIZE (4096)
28 #define VMW_PAGE_SHIFT (12)
29
30 #define MASK(n) ((1 << (n)) - 1) /* make an n-bit mask */
31
32 /*
33 * host adapter status/error codes
34 */
35 enum HostBusAdapterStatus {
36 BTSTAT_SUCCESS = 0x00, /* CCB complete normally with no errors */
37 BTSTAT_LINKED_COMMAND_COMPLETED = 0x0a,
38 BTSTAT_LINKED_COMMAND_COMPLETED_WITH_FLAG = 0x0b,
39 BTSTAT_DATA_UNDERRUN = 0x0c,
40 BTSTAT_SELTIMEO = 0x11, /* SCSI selection timeout */
41 BTSTAT_DATARUN = 0x12, /* data overrun/underrun */
42 BTSTAT_BUSFREE = 0x13, /* unexpected bus free */
43 BTSTAT_INVPHASE = 0x14, /* invalid bus phase or sequence */
44 /* requested by target */
45 BTSTAT_LUNMISMATCH = 0x17, /* linked CCB has different LUN */
46 /* from first CCB */
47 BTSTAT_SENSFAILED = 0x1b, /* auto request sense failed */
48 BTSTAT_TAGREJECT = 0x1c, /* SCSI II tagged queueing message */
49 /* rejected by target */
50 BTSTAT_BADMSG = 0x1d, /* unsupported message received by */
51 /* the host adapter */
52 BTSTAT_HAHARDWARE = 0x20, /* host adapter hardware failed */
53 BTSTAT_NORESPONSE = 0x21, /* target did not respond to SCSI ATN, */
54 /* sent a SCSI RST */
55 BTSTAT_SENTRST = 0x22, /* host adapter asserted a SCSI RST */
56 BTSTAT_RECVRST = 0x23, /* other SCSI devices asserted a SCSI RST */
57 BTSTAT_DISCONNECT = 0x24, /* target device reconnected improperly */
58 /* (w/o tag) */
59 BTSTAT_BUSRESET = 0x25, /* host adapter issued BUS device reset */
60 BTSTAT_ABORTQUEUE = 0x26, /* abort queue generated */
61 BTSTAT_HASOFTWARE = 0x27, /* host adapter software error */
62 BTSTAT_HATIMEOUT = 0x30, /* host adapter hardware timeout error */
63 BTSTAT_SCSIPARITY = 0x34, /* SCSI parity error detected */
64 };
65
66 /*
67 * Register offsets.
68 *
69 * These registers are accessible both via i/o space and mm i/o.
70 */
71
72 enum PVSCSIRegOffset {
73 PVSCSI_REG_OFFSET_COMMAND = 0x0,
74 PVSCSI_REG_OFFSET_COMMAND_DATA = 0x4,
75 PVSCSI_REG_OFFSET_COMMAND_STATUS = 0x8,
76 PVSCSI_REG_OFFSET_LAST_STS_0 = 0x100,
77 PVSCSI_REG_OFFSET_LAST_STS_1 = 0x104,
78 PVSCSI_REG_OFFSET_LAST_STS_2 = 0x108,
79 PVSCSI_REG_OFFSET_LAST_STS_3 = 0x10c,
80 PVSCSI_REG_OFFSET_INTR_STATUS = 0x100c,
81 PVSCSI_REG_OFFSET_INTR_MASK = 0x2010,
82 PVSCSI_REG_OFFSET_KICK_NON_RW_IO = 0x3014,
83 PVSCSI_REG_OFFSET_DEBUG = 0x3018,
84 PVSCSI_REG_OFFSET_KICK_RW_IO = 0x4018,
85 };
86
87 /*
88 * Virtual h/w commands.
89 */
90
91 enum PVSCSICommands {
92 PVSCSI_CMD_FIRST = 0, /* has to be first */
93
94 PVSCSI_CMD_ADAPTER_RESET = 1,
95 PVSCSI_CMD_ISSUE_SCSI = 2,
96 PVSCSI_CMD_SETUP_RINGS = 3,
97 PVSCSI_CMD_RESET_BUS = 4,
98 PVSCSI_CMD_RESET_DEVICE = 5,
99 PVSCSI_CMD_ABORT_CMD = 6,
100 PVSCSI_CMD_CONFIG = 7,
101 PVSCSI_CMD_SETUP_MSG_RING = 8,
102 PVSCSI_CMD_DEVICE_UNPLUG = 9,
103
104 PVSCSI_CMD_LAST = 10 /* has to be last */
105 };
106
107 #define PVSCSI_COMMAND_PROCESSING_SUCCEEDED (0)
108 #define PVSCSI_COMMAND_PROCESSING_FAILED (-1)
109 #define PVSCSI_COMMAND_NOT_ENOUGH_DATA (-2)
110
111 /*
112 * About endianess for the below structs:
113 *
114 * These structs are used to describe the data that is exchanged between the
115 * guest and the PVSCSI device. The endianess of the fields in these structs
116 * is not defined by any standard. The current implemented drivers are designed
117 * to only work on x86 architecture, so there is no endianess awareness in the
118 * drivers and thus we have no idea whether the fields should be in little-
119 * endian or target native endian format.
120 *
121 * Considering the above, we assume that PVSCSI is implicitly little-endian and
122 * expect the fields in these structs to be in little-endian format.
123 */
124
125 /*
126 * Command descriptor for PVSCSI_CMD_RESET_DEVICE --
127 */
128
129 struct PVSCSICmdDescResetDevice {
130 uint32_t target;
131 uint8_t lun[8];
132 } QEMU_PACKED;
133
134 typedef struct PVSCSICmdDescResetDevice PVSCSICmdDescResetDevice;
135
136 /*
137 * Command descriptor for PVSCSI_CMD_ABORT_CMD --
138 *
139 * - currently does not support specifying the LUN.
140 * - pad should be 0.
141 */
142
143 struct PVSCSICmdDescAbortCmd {
144 uint64_t context;
145 uint32_t target;
146 uint32_t pad;
147 } QEMU_PACKED;
148
149 typedef struct PVSCSICmdDescAbortCmd PVSCSICmdDescAbortCmd;
150
151 /*
152 * Command descriptor for PVSCSI_CMD_SETUP_RINGS --
153 *
154 * Notes:
155 * - reqRingNumPages and cmpRingNumPages need to be power of two.
156 * - reqRingNumPages and cmpRingNumPages need to be different from 0,
157 * - reqRingNumPages and cmpRingNumPages need to be inferior to
158 * PVSCSI_SETUP_RINGS_MAX_NUM_PAGES.
159 */
160
161 #define PVSCSI_SETUP_RINGS_MAX_NUM_PAGES 32
162 struct PVSCSICmdDescSetupRings {
163 uint32_t reqRingNumPages;
164 uint32_t cmpRingNumPages;
165 uint64_t ringsStatePPN;
166 uint64_t reqRingPPNs[PVSCSI_SETUP_RINGS_MAX_NUM_PAGES];
167 uint64_t cmpRingPPNs[PVSCSI_SETUP_RINGS_MAX_NUM_PAGES];
168 } QEMU_PACKED;
169
170 typedef struct PVSCSICmdDescSetupRings PVSCSICmdDescSetupRings;
171
172 /*
173 * Command descriptor for PVSCSI_CMD_SETUP_MSG_RING --
174 *
175 * Notes:
176 * - this command was not supported in the initial revision of the h/w
177 * interface. Before using it, you need to check that it is supported by
178 * writing PVSCSI_CMD_SETUP_MSG_RING to the 'command' register, then
179 * immediately after read the 'command status' register:
180 * * a value of -1 means that the cmd is NOT supported,
181 * * a value != -1 means that the cmd IS supported.
182 * If it's supported the 'command status' register should return:
183 * sizeof(PVSCSICmdDescSetupMsgRing) / sizeof(uint32_t).
184 * - this command should be issued _after_ the usual SETUP_RINGS so that the
185 * RingsState page is already setup. If not, the command is a nop.
186 * - numPages needs to be a power of two,
187 * - numPages needs to be different from 0,
188 * - pad should be zero.
189 */
190
191 #define PVSCSI_SETUP_MSG_RING_MAX_NUM_PAGES 16
192
193 struct PVSCSICmdDescSetupMsgRing {
194 uint32_t numPages;
195 uint32_t pad;
196 uint64_t ringPPNs[PVSCSI_SETUP_MSG_RING_MAX_NUM_PAGES];
197 } QEMU_PACKED;
198
199 typedef struct PVSCSICmdDescSetupMsgRing PVSCSICmdDescSetupMsgRing;
200
201 enum PVSCSIMsgType {
202 PVSCSI_MSG_DEV_ADDED = 0,
203 PVSCSI_MSG_DEV_REMOVED = 1,
204 PVSCSI_MSG_LAST = 2,
205 };
206
207 /*
208 * Msg descriptor.
209 *
210 * sizeof(struct PVSCSIRingMsgDesc) == 128.
211 *
212 * - type is of type enum PVSCSIMsgType.
213 * - the content of args depend on the type of event being delivered.
214 */
215
216 struct PVSCSIRingMsgDesc {
217 uint32_t type;
218 uint32_t args[31];
219 } QEMU_PACKED;
220
221 typedef struct PVSCSIRingMsgDesc PVSCSIRingMsgDesc;
222
223 struct PVSCSIMsgDescDevStatusChanged {
224 uint32_t type; /* PVSCSI_MSG_DEV _ADDED / _REMOVED */
225 uint32_t bus;
226 uint32_t target;
227 uint8_t lun[8];
228 uint32_t pad[27];
229 } QEMU_PACKED;
230
231 typedef struct PVSCSIMsgDescDevStatusChanged PVSCSIMsgDescDevStatusChanged;
232
233 /*
234 * Rings state.
235 *
236 * - the fields:
237 * . msgProdIdx,
238 * . msgConsIdx,
239 * . msgNumEntriesLog2,
240 * .. are only used once the SETUP_MSG_RING cmd has been issued.
241 * - 'pad' helps to ensure that the msg related fields are on their own
242 * cache-line.
243 */
244
245 struct PVSCSIRingsState {
246 uint32_t reqProdIdx;
247 uint32_t reqConsIdx;
248 uint32_t reqNumEntriesLog2;
249
250 uint32_t cmpProdIdx;
251 uint32_t cmpConsIdx;
252 uint32_t cmpNumEntriesLog2;
253
254 uint8_t pad[104];
255
256 uint32_t msgProdIdx;
257 uint32_t msgConsIdx;
258 uint32_t msgNumEntriesLog2;
259 } QEMU_PACKED;
260
261 typedef struct PVSCSIRingsState PVSCSIRingsState;
262
263 /*
264 * Request descriptor.
265 *
266 * sizeof(RingReqDesc) = 128
267 *
268 * - context: is a unique identifier of a command. It could normally be any
269 * 64bit value, however we currently store it in the serialNumber variable
270 * of struct SCSI_Command, so we have the following restrictions due to the
271 * way this field is handled in the vmkernel storage stack:
272 * * this value can't be 0,
273 * * the upper 32bit need to be 0 since serialNumber is as a uint32_t.
274 * Currently tracked as PR 292060.
275 * - dataLen: contains the total number of bytes that need to be transferred.
276 * - dataAddr:
277 * * if PVSCSI_FLAG_CMD_WITH_SG_LIST is set: dataAddr is the PA of the first
278 * s/g table segment, each s/g segment is entirely contained on a single
279 * page of physical memory,
280 * * if PVSCSI_FLAG_CMD_WITH_SG_LIST is NOT set, then dataAddr is the PA of
281 * the buffer used for the DMA transfer,
282 * - flags:
283 * * PVSCSI_FLAG_CMD_WITH_SG_LIST: see dataAddr above,
284 * * PVSCSI_FLAG_CMD_DIR_NONE: no DMA involved,
285 * * PVSCSI_FLAG_CMD_DIR_TOHOST: transfer from device to main memory,
286 * * PVSCSI_FLAG_CMD_DIR_TODEVICE: transfer from main memory to device,
287 * * PVSCSI_FLAG_CMD_OUT_OF_BAND_CDB: reserved to handle CDBs larger than
288 * 16bytes. To be specified.
289 * - vcpuHint: vcpuId of the processor that will be most likely waiting for the
290 * completion of the i/o. For guest OSes that use lowest priority message
291 * delivery mode (such as windows), we use this "hint" to deliver the
292 * completion action to the proper vcpu. For now, we can use the vcpuId of
293 * the processor that initiated the i/o as a likely candidate for the vcpu
294 * that will be waiting for the completion..
295 * - bus should be 0: we currently only support bus 0 for now.
296 * - unused should be zero'd.
297 */
298
299 #define PVSCSI_FLAG_CMD_WITH_SG_LIST (1 << 0)
300 #define PVSCSI_FLAG_CMD_OUT_OF_BAND_CDB (1 << 1)
301 #define PVSCSI_FLAG_CMD_DIR_NONE (1 << 2)
302 #define PVSCSI_FLAG_CMD_DIR_TOHOST (1 << 3)
303 #define PVSCSI_FLAG_CMD_DIR_TODEVICE (1 << 4)
304
305 #define PVSCSI_KNOWN_FLAGS \
306 (PVSCSI_FLAG_CMD_WITH_SG_LIST | \
307 PVSCSI_FLAG_CMD_OUT_OF_BAND_CDB | \
308 PVSCSI_FLAG_CMD_DIR_NONE | \
309 PVSCSI_FLAG_CMD_DIR_TOHOST | \
310 PVSCSI_FLAG_CMD_DIR_TODEVICE)
311
312 struct PVSCSIRingReqDesc {
313 uint64_t context;
314 uint64_t dataAddr;
315 uint64_t dataLen;
316 uint64_t senseAddr;
317 uint32_t senseLen;
318 uint32_t flags;
319 uint8_t cdb[16];
320 uint8_t cdbLen;
321 uint8_t lun[8];
322 uint8_t tag;
323 uint8_t bus;
324 uint8_t target;
325 uint8_t vcpuHint;
326 uint8_t unused[59];
327 } QEMU_PACKED;
328
329 typedef struct PVSCSIRingReqDesc PVSCSIRingReqDesc;
330
331 /*
332 * Scatter-gather list management.
333 *
334 * As described above, when PVSCSI_FLAG_CMD_WITH_SG_LIST is set in the
335 * RingReqDesc.flags, then RingReqDesc.dataAddr is the PA of the first s/g
336 * table segment.
337 *
338 * - each segment of the s/g table contain a succession of struct
339 * PVSCSISGElement.
340 * - each segment is entirely contained on a single physical page of memory.
341 * - a "chain" s/g element has the flag PVSCSI_SGE_FLAG_CHAIN_ELEMENT set in
342 * PVSCSISGElement.flags and in this case:
343 * * addr is the PA of the next s/g segment,
344 * * length is undefined, assumed to be 0.
345 */
346
347 struct PVSCSISGElement {
348 uint64_t addr;
349 uint32_t length;
350 uint32_t flags;
351 } QEMU_PACKED;
352
353 typedef struct PVSCSISGElement PVSCSISGElement;
354
355 /*
356 * Completion descriptor.
357 *
358 * sizeof(RingCmpDesc) = 32
359 *
360 * - context: identifier of the command. The same thing that was specified
361 * under "context" as part of struct RingReqDesc at initiation time,
362 * - dataLen: number of bytes transferred for the actual i/o operation,
363 * - senseLen: number of bytes written into the sense buffer,
364 * - hostStatus: adapter status,
365 * - scsiStatus: device status,
366 * - pad should be zero.
367 */
368
369 struct PVSCSIRingCmpDesc {
370 uint64_t context;
371 uint64_t dataLen;
372 uint32_t senseLen;
373 uint16_t hostStatus;
374 uint16_t scsiStatus;
375 uint32_t pad[2];
376 } QEMU_PACKED;
377
378 typedef struct PVSCSIRingCmpDesc PVSCSIRingCmpDesc;
379
380 /*
381 * Interrupt status / IRQ bits.
382 */
383
384 #define PVSCSI_INTR_CMPL_0 (1 << 0)
385 #define PVSCSI_INTR_CMPL_1 (1 << 1)
386 #define PVSCSI_INTR_CMPL_MASK MASK(2)
387
388 #define PVSCSI_INTR_MSG_0 (1 << 2)
389 #define PVSCSI_INTR_MSG_1 (1 << 3)
390 #define PVSCSI_INTR_MSG_MASK (MASK(2) << 2)
391
392 #define PVSCSI_INTR_ALL_SUPPORTED MASK(4)
393
394 /*
395 * Number of MSI-X vectors supported.
396 */
397 #define PVSCSI_MAX_INTRS 24
398
399 /*
400 * Enumeration of supported MSI-X vectors
401 */
402 #define PVSCSI_VECTOR_COMPLETION 0
403
404 /*
405 * Misc constants for the rings.
406 */
407
408 #define PVSCSI_MAX_NUM_PAGES_REQ_RING PVSCSI_SETUP_RINGS_MAX_NUM_PAGES
409 #define PVSCSI_MAX_NUM_PAGES_CMP_RING PVSCSI_SETUP_RINGS_MAX_NUM_PAGES
410 #define PVSCSI_MAX_NUM_PAGES_MSG_RING PVSCSI_SETUP_MSG_RING_MAX_NUM_PAGES
411
412 #define PVSCSI_MAX_NUM_REQ_ENTRIES_PER_PAGE \
413 (VMW_PAGE_SIZE / sizeof(struct PVSCSIRingReqDesc))
414
415 #define PVSCSI_MAX_NUM_CMP_ENTRIES_PER_PAGE \
416 (VMW_PAGE_SIZE / sizeof(PVSCSIRingCmpDesc))
417
418 #define PVSCSI_MAX_NUM_MSG_ENTRIES_PER_PAGE \
419 (VMW_PAGE_SIZE / sizeof(PVSCSIRingMsgDesc))
420
421 #define PVSCSI_MAX_REQ_QUEUE_DEPTH \
422 (PVSCSI_MAX_NUM_PAGES_REQ_RING * PVSCSI_MAX_NUM_REQ_ENTRIES_PER_PAGE)
423
424 #define PVSCSI_MEM_SPACE_COMMAND_NUM_PAGES 1
425 #define PVSCSI_MEM_SPACE_INTR_STATUS_NUM_PAGES 1
426 #define PVSCSI_MEM_SPACE_MISC_NUM_PAGES 2
427 #define PVSCSI_MEM_SPACE_KICK_IO_NUM_PAGES 2
428 #define PVSCSI_MEM_SPACE_MSIX_NUM_PAGES 2
429
430 enum PVSCSIMemSpace {
431 PVSCSI_MEM_SPACE_COMMAND_PAGE = 0,
432 PVSCSI_MEM_SPACE_INTR_STATUS_PAGE = 1,
433 PVSCSI_MEM_SPACE_MISC_PAGE = 2,
434 PVSCSI_MEM_SPACE_KICK_IO_PAGE = 4,
435 PVSCSI_MEM_SPACE_MSIX_TABLE_PAGE = 6,
436 PVSCSI_MEM_SPACE_MSIX_PBA_PAGE = 7,
437 };
438
439 #define PVSCSI_MEM_SPACE_NUM_PAGES \
440 (PVSCSI_MEM_SPACE_COMMAND_NUM_PAGES + \
441 PVSCSI_MEM_SPACE_INTR_STATUS_NUM_PAGES + \
442 PVSCSI_MEM_SPACE_MISC_NUM_PAGES + \
443 PVSCSI_MEM_SPACE_KICK_IO_NUM_PAGES + \
444 PVSCSI_MEM_SPACE_MSIX_NUM_PAGES)
445
446 #define PVSCSI_MEM_SPACE_SIZE (PVSCSI_MEM_SPACE_NUM_PAGES * VMW_PAGE_SIZE)
447
448 #endif /* VMW_PVSCSI_H */