2 * QEMU VMWARE PVSCSI paravirtual SCSI bus
4 * Copyright (c) 2012 Ravello Systems LTD (http://ravellosystems.com)
6 * Developed by Daynix Computing LTD (http://www.daynix.com)
8 * Based on implementation by Paolo Bonzini
9 * http://lists.gnu.org/archive/html/qemu-devel/2011-08/msg00729.html
12 * Paolo Bonzini <pbonzini@redhat.com>
13 * Dmitry Fleytman <dmitry@daynix.com>
14 * Yan Vugenfirer <yan@daynix.com>
16 * This work is licensed under the terms of the GNU GPL, version 2.
17 * See the COPYING file in the top-level directory.
20 * MSI-X support has been removed for the moment because it leads Windows OS
21 * to crash on startup. The crash happens because Windows driver requires
22 * MSI-X shared memory to be part of the same BAR used for rings state
23 * registers, etc. This is not supported by QEMU infrastructure so separate
24 * BAR created from MSI-X purposes. Windows driver fails to deal with 2 BARs.
28 #include "hw/scsi/scsi.h"
29 #include <block/scsi.h>
30 #include "hw/pci/msi.h"
31 #include "vmw_pvscsi.h"
35 #define PVSCSI_MSI_OFFSET (0x50)
36 #define PVSCSI_USE_64BIT (true)
37 #define PVSCSI_PER_VECTOR_MASK (false)
39 #define PVSCSI_MAX_DEVS (64)
40 #define PVSCSI_MSIX_NUM_VECTORS (1)
42 #define PVSCSI_MAX_CMD_DATA_WORDS \
43 (sizeof(PVSCSICmdDescSetupRings)/sizeof(uint32_t))
45 #define RS_GET_FIELD(rs_pa, field) \
46 (ldl_le_phys(rs_pa + offsetof(struct PVSCSIRingsState, field)))
47 #define RS_SET_FIELD(rs_pa, field, val) \
48 (stl_le_phys(rs_pa + offsetof(struct PVSCSIRingsState, field), val))
50 #define TYPE_PVSCSI "pvscsi"
51 #define PVSCSI(obj) OBJECT_CHECK(PVSCSIState, (obj), TYPE_PVSCSI)
53 typedef struct PVSCSIRingInfo
{
55 uint32_t txr_len_mask
;
56 uint32_t rxr_len_mask
;
57 uint32_t msg_len_mask
;
58 uint64_t req_ring_pages_pa
[PVSCSI_SETUP_RINGS_MAX_NUM_PAGES
];
59 uint64_t cmp_ring_pages_pa
[PVSCSI_SETUP_RINGS_MAX_NUM_PAGES
];
60 uint64_t msg_ring_pages_pa
[PVSCSI_SETUP_MSG_RING_MAX_NUM_PAGES
];
61 uint64_t consumed_ptr
;
62 uint64_t filled_cmp_ptr
;
63 uint64_t filled_msg_ptr
;
66 typedef struct PVSCSISGState
{
72 typedef QTAILQ_HEAD(, PVSCSIRequest
) PVSCSIRequestList
;
76 MemoryRegion io_space
;
78 QEMUBH
*completion_worker
;
79 PVSCSIRequestList pending_queue
;
80 PVSCSIRequestList completion_queue
;
82 uint64_t reg_interrupt_status
; /* Interrupt status register value */
83 uint64_t reg_interrupt_enabled
; /* Interrupt mask register value */
84 uint64_t reg_command_status
; /* Command status register value */
86 /* Command data adoption mechanism */
87 uint64_t curr_cmd
; /* Last command arrived */
88 uint32_t curr_cmd_data_cntr
; /* Amount of data for last command */
90 /* Collector for current command data */
91 uint32_t curr_cmd_data
[PVSCSI_MAX_CMD_DATA_WORDS
];
93 uint8_t rings_info_valid
; /* Whether data rings initialized */
94 uint8_t msg_ring_info_valid
; /* Whether message ring initialized */
95 uint8_t use_msg
; /* Whether to use message ring */
97 uint8_t msi_used
; /* Whether MSI support was installed successfully */
99 PVSCSIRingInfo rings
; /* Data transfer rings manager */
100 uint32_t resetting
; /* Reset in progress */
103 typedef struct PVSCSIRequest
{
111 struct PVSCSIRingReqDesc req
;
112 struct PVSCSIRingCmpDesc cmp
;
113 QTAILQ_ENTRY(PVSCSIRequest
) next
;
116 /* Integer binary logarithm */
118 pvscsi_log2(uint32_t input
)
122 while (input
>> ++log
) {
128 pvscsi_ring_init_data(PVSCSIRingInfo
*m
, PVSCSICmdDescSetupRings
*ri
)
131 uint32_t txr_len_log2
, rxr_len_log2
;
132 uint32_t req_ring_size
, cmp_ring_size
;
133 m
->rs_pa
= ri
->ringsStatePPN
<< VMW_PAGE_SHIFT
;
135 req_ring_size
= ri
->reqRingNumPages
* PVSCSI_MAX_NUM_REQ_ENTRIES_PER_PAGE
;
136 cmp_ring_size
= ri
->cmpRingNumPages
* PVSCSI_MAX_NUM_CMP_ENTRIES_PER_PAGE
;
137 txr_len_log2
= pvscsi_log2(req_ring_size
- 1);
138 rxr_len_log2
= pvscsi_log2(cmp_ring_size
- 1);
140 m
->txr_len_mask
= MASK(txr_len_log2
);
141 m
->rxr_len_mask
= MASK(rxr_len_log2
);
144 m
->filled_cmp_ptr
= 0;
146 for (i
= 0; i
< ri
->reqRingNumPages
; i
++) {
147 m
->req_ring_pages_pa
[i
] = ri
->reqRingPPNs
[i
] << VMW_PAGE_SHIFT
;
150 for (i
= 0; i
< ri
->cmpRingNumPages
; i
++) {
151 m
->cmp_ring_pages_pa
[i
] = ri
->cmpRingPPNs
[i
] << VMW_PAGE_SHIFT
;
154 RS_SET_FIELD(m
->rs_pa
, reqProdIdx
, 0);
155 RS_SET_FIELD(m
->rs_pa
, reqConsIdx
, 0);
156 RS_SET_FIELD(m
->rs_pa
, reqNumEntriesLog2
, txr_len_log2
);
158 RS_SET_FIELD(m
->rs_pa
, cmpProdIdx
, 0);
159 RS_SET_FIELD(m
->rs_pa
, cmpConsIdx
, 0);
160 RS_SET_FIELD(m
->rs_pa
, cmpNumEntriesLog2
, rxr_len_log2
);
162 trace_pvscsi_ring_init_data(txr_len_log2
, rxr_len_log2
);
164 /* Flush ring state page changes */
169 pvscsi_ring_init_msg(PVSCSIRingInfo
*m
, PVSCSICmdDescSetupMsgRing
*ri
)
175 ring_size
= ri
->numPages
* PVSCSI_MAX_NUM_MSG_ENTRIES_PER_PAGE
;
176 len_log2
= pvscsi_log2(ring_size
- 1);
178 m
->msg_len_mask
= MASK(len_log2
);
180 m
->filled_msg_ptr
= 0;
182 for (i
= 0; i
< ri
->numPages
; i
++) {
183 m
->msg_ring_pages_pa
[i
] = ri
->ringPPNs
[i
] << VMW_PAGE_SHIFT
;
186 RS_SET_FIELD(m
->rs_pa
, msgProdIdx
, 0);
187 RS_SET_FIELD(m
->rs_pa
, msgConsIdx
, 0);
188 RS_SET_FIELD(m
->rs_pa
, msgNumEntriesLog2
, len_log2
);
190 trace_pvscsi_ring_init_msg(len_log2
);
192 /* Flush ring state page changes */
197 pvscsi_ring_cleanup(PVSCSIRingInfo
*mgr
)
200 mgr
->txr_len_mask
= 0;
201 mgr
->rxr_len_mask
= 0;
202 mgr
->msg_len_mask
= 0;
203 mgr
->consumed_ptr
= 0;
204 mgr
->filled_cmp_ptr
= 0;
205 mgr
->filled_msg_ptr
= 0;
206 memset(mgr
->req_ring_pages_pa
, 0, sizeof(mgr
->req_ring_pages_pa
));
207 memset(mgr
->cmp_ring_pages_pa
, 0, sizeof(mgr
->cmp_ring_pages_pa
));
208 memset(mgr
->msg_ring_pages_pa
, 0, sizeof(mgr
->msg_ring_pages_pa
));
212 pvscsi_ring_pop_req_descr(PVSCSIRingInfo
*mgr
)
214 uint32_t ready_ptr
= RS_GET_FIELD(mgr
->rs_pa
, reqProdIdx
);
216 if (ready_ptr
!= mgr
->consumed_ptr
) {
217 uint32_t next_ready_ptr
=
218 mgr
->consumed_ptr
++ & mgr
->txr_len_mask
;
219 uint32_t next_ready_page
=
220 next_ready_ptr
/ PVSCSI_MAX_NUM_REQ_ENTRIES_PER_PAGE
;
221 uint32_t inpage_idx
=
222 next_ready_ptr
% PVSCSI_MAX_NUM_REQ_ENTRIES_PER_PAGE
;
224 return mgr
->req_ring_pages_pa
[next_ready_page
] +
225 inpage_idx
* sizeof(PVSCSIRingReqDesc
);
232 pvscsi_ring_flush_req(PVSCSIRingInfo
*mgr
)
234 RS_SET_FIELD(mgr
->rs_pa
, reqConsIdx
, mgr
->consumed_ptr
);
238 pvscsi_ring_pop_cmp_descr(PVSCSIRingInfo
*mgr
)
241 * According to Linux driver code it explicitly verifies that number
242 * of requests being processed by device is less then the size of
243 * completion queue, so device may omit completion queue overflow
244 * conditions check. We assume that this is true for other (Windows)
248 uint32_t free_cmp_ptr
=
249 mgr
->filled_cmp_ptr
++ & mgr
->rxr_len_mask
;
250 uint32_t free_cmp_page
=
251 free_cmp_ptr
/ PVSCSI_MAX_NUM_CMP_ENTRIES_PER_PAGE
;
252 uint32_t inpage_idx
=
253 free_cmp_ptr
% PVSCSI_MAX_NUM_CMP_ENTRIES_PER_PAGE
;
254 return mgr
->cmp_ring_pages_pa
[free_cmp_page
] +
255 inpage_idx
* sizeof(PVSCSIRingCmpDesc
);
259 pvscsi_ring_pop_msg_descr(PVSCSIRingInfo
*mgr
)
261 uint32_t free_msg_ptr
=
262 mgr
->filled_msg_ptr
++ & mgr
->msg_len_mask
;
263 uint32_t free_msg_page
=
264 free_msg_ptr
/ PVSCSI_MAX_NUM_MSG_ENTRIES_PER_PAGE
;
265 uint32_t inpage_idx
=
266 free_msg_ptr
% PVSCSI_MAX_NUM_MSG_ENTRIES_PER_PAGE
;
267 return mgr
->msg_ring_pages_pa
[free_msg_page
] +
268 inpage_idx
* sizeof(PVSCSIRingMsgDesc
);
272 pvscsi_ring_flush_cmp(PVSCSIRingInfo
*mgr
)
274 /* Flush descriptor changes */
277 trace_pvscsi_ring_flush_cmp(mgr
->filled_cmp_ptr
);
279 RS_SET_FIELD(mgr
->rs_pa
, cmpProdIdx
, mgr
->filled_cmp_ptr
);
283 pvscsi_ring_msg_has_room(PVSCSIRingInfo
*mgr
)
285 uint32_t prodIdx
= RS_GET_FIELD(mgr
->rs_pa
, msgProdIdx
);
286 uint32_t consIdx
= RS_GET_FIELD(mgr
->rs_pa
, msgConsIdx
);
288 return (prodIdx
- consIdx
) < (mgr
->msg_len_mask
+ 1);
292 pvscsi_ring_flush_msg(PVSCSIRingInfo
*mgr
)
294 /* Flush descriptor changes */
297 trace_pvscsi_ring_flush_msg(mgr
->filled_msg_ptr
);
299 RS_SET_FIELD(mgr
->rs_pa
, msgProdIdx
, mgr
->filled_msg_ptr
);
303 pvscsi_reset_state(PVSCSIState
*s
)
305 s
->curr_cmd
= PVSCSI_CMD_FIRST
;
306 s
->curr_cmd_data_cntr
= 0;
307 s
->reg_command_status
= PVSCSI_COMMAND_PROCESSING_SUCCEEDED
;
308 s
->reg_interrupt_status
= 0;
309 pvscsi_ring_cleanup(&s
->rings
);
310 s
->rings_info_valid
= FALSE
;
311 s
->msg_ring_info_valid
= FALSE
;
312 QTAILQ_INIT(&s
->pending_queue
);
313 QTAILQ_INIT(&s
->completion_queue
);
317 pvscsi_update_irq_status(PVSCSIState
*s
)
319 PCIDevice
*d
= PCI_DEVICE(s
);
320 bool should_raise
= s
->reg_interrupt_enabled
& s
->reg_interrupt_status
;
322 trace_pvscsi_update_irq_level(should_raise
, s
->reg_interrupt_enabled
,
323 s
->reg_interrupt_status
);
325 if (s
->msi_used
&& msi_enabled(d
)) {
327 trace_pvscsi_update_irq_msi();
328 msi_notify(d
, PVSCSI_VECTOR_COMPLETION
);
333 qemu_set_irq(d
->irq
[0], !!should_raise
);
337 pvscsi_raise_completion_interrupt(PVSCSIState
*s
)
339 s
->reg_interrupt_status
|= PVSCSI_INTR_CMPL_0
;
341 /* Memory barrier to flush interrupt status register changes*/
344 pvscsi_update_irq_status(s
);
348 pvscsi_raise_message_interrupt(PVSCSIState
*s
)
350 s
->reg_interrupt_status
|= PVSCSI_INTR_MSG_0
;
352 /* Memory barrier to flush interrupt status register changes*/
355 pvscsi_update_irq_status(s
);
359 pvscsi_cmp_ring_put(PVSCSIState
*s
, struct PVSCSIRingCmpDesc
*cmp_desc
)
363 cmp_descr_pa
= pvscsi_ring_pop_cmp_descr(&s
->rings
);
364 trace_pvscsi_cmp_ring_put(cmp_descr_pa
);
365 cpu_physical_memory_write(cmp_descr_pa
, (void *)cmp_desc
,
370 pvscsi_msg_ring_put(PVSCSIState
*s
, struct PVSCSIRingMsgDesc
*msg_desc
)
374 msg_descr_pa
= pvscsi_ring_pop_msg_descr(&s
->rings
);
375 trace_pvscsi_msg_ring_put(msg_descr_pa
);
376 cpu_physical_memory_write(msg_descr_pa
, (void *)msg_desc
,
381 pvscsi_process_completion_queue(void *opaque
)
383 PVSCSIState
*s
= opaque
;
384 PVSCSIRequest
*pvscsi_req
;
385 bool has_completed
= false;
387 while (!QTAILQ_EMPTY(&s
->completion_queue
)) {
388 pvscsi_req
= QTAILQ_FIRST(&s
->completion_queue
);
389 QTAILQ_REMOVE(&s
->completion_queue
, pvscsi_req
, next
);
390 pvscsi_cmp_ring_put(s
, &pvscsi_req
->cmp
);
392 has_completed
= true;
396 pvscsi_ring_flush_cmp(&s
->rings
);
397 pvscsi_raise_completion_interrupt(s
);
402 pvscsi_reset_adapter(PVSCSIState
*s
)
405 qbus_reset_all_fn(&s
->bus
);
407 pvscsi_process_completion_queue(s
);
408 assert(QTAILQ_EMPTY(&s
->pending_queue
));
409 pvscsi_reset_state(s
);
413 pvscsi_schedule_completion_processing(PVSCSIState
*s
)
415 /* Try putting more complete requests on the ring. */
416 if (!QTAILQ_EMPTY(&s
->completion_queue
)) {
417 qemu_bh_schedule(s
->completion_worker
);
422 pvscsi_complete_request(PVSCSIState
*s
, PVSCSIRequest
*r
)
424 assert(!r
->completed
);
426 trace_pvscsi_complete_request(r
->cmp
.context
, r
->cmp
.dataLen
,
428 if (r
->sreq
!= NULL
) {
429 scsi_req_unref(r
->sreq
);
433 QTAILQ_REMOVE(&s
->pending_queue
, r
, next
);
434 QTAILQ_INSERT_TAIL(&s
->completion_queue
, r
, next
);
435 pvscsi_schedule_completion_processing(s
);
438 static QEMUSGList
*pvscsi_get_sg_list(SCSIRequest
*r
)
440 PVSCSIRequest
*req
= r
->hba_private
;
442 trace_pvscsi_get_sg_list(req
->sgl
.nsg
, req
->sgl
.size
);
448 pvscsi_get_next_sg_elem(PVSCSISGState
*sg
)
450 struct PVSCSISGElement elem
;
452 cpu_physical_memory_read(sg
->elemAddr
, (void *)&elem
, sizeof(elem
));
453 if ((elem
.flags
& ~PVSCSI_KNOWN_FLAGS
) != 0) {
455 * There is PVSCSI_SGE_FLAG_CHAIN_ELEMENT flag described in
456 * header file but its value is unknown. This flag requires
457 * additional processing, so we put warning here to catch it
458 * some day and make proper implementation
460 trace_pvscsi_get_next_sg_elem(elem
.flags
);
463 sg
->elemAddr
+= sizeof(elem
);
464 sg
->dataAddr
= elem
.addr
;
465 sg
->resid
= elem
.length
;
469 pvscsi_write_sense(PVSCSIRequest
*r
, uint8_t *sense
, int len
)
471 r
->cmp
.senseLen
= MIN(r
->req
.senseLen
, len
);
472 r
->sense_key
= sense
[(sense
[0] & 2) ? 1 : 2];
473 cpu_physical_memory_write(r
->req
.senseAddr
, sense
, r
->cmp
.senseLen
);
477 pvscsi_command_complete(SCSIRequest
*req
, uint32_t status
, size_t resid
)
479 PVSCSIRequest
*pvscsi_req
= req
->hba_private
;
480 PVSCSIState
*s
= pvscsi_req
->dev
;
483 trace_pvscsi_command_complete_not_found(req
->tag
);
488 /* Short transfer. */
489 trace_pvscsi_command_complete_data_run();
490 pvscsi_req
->cmp
.hostStatus
= BTSTAT_DATARUN
;
493 pvscsi_req
->cmp
.scsiStatus
= status
;
494 if (pvscsi_req
->cmp
.scsiStatus
== CHECK_CONDITION
) {
495 uint8_t sense
[SCSI_SENSE_BUF_SIZE
];
497 scsi_req_get_sense(pvscsi_req
->sreq
, sense
, sizeof(sense
));
499 trace_pvscsi_command_complete_sense_len(sense_len
);
500 pvscsi_write_sense(pvscsi_req
, sense
, sense_len
);
502 qemu_sglist_destroy(&pvscsi_req
->sgl
);
503 pvscsi_complete_request(s
, pvscsi_req
);
507 pvscsi_send_msg(PVSCSIState
*s
, SCSIDevice
*dev
, uint32_t msg_type
)
509 if (s
->msg_ring_info_valid
&& pvscsi_ring_msg_has_room(&s
->rings
)) {
510 PVSCSIMsgDescDevStatusChanged msg
= {0};
513 msg
.bus
= dev
->channel
;
514 msg
.target
= dev
->id
;
515 msg
.lun
[1] = dev
->lun
;
517 pvscsi_msg_ring_put(s
, (PVSCSIRingMsgDesc
*)&msg
);
518 pvscsi_ring_flush_msg(&s
->rings
);
519 pvscsi_raise_message_interrupt(s
);
524 pvscsi_hotplug(SCSIBus
*bus
, SCSIDevice
*dev
)
526 PVSCSIState
*s
= container_of(bus
, PVSCSIState
, bus
);
527 pvscsi_send_msg(s
, dev
, PVSCSI_MSG_DEV_ADDED
);
531 pvscsi_hot_unplug(SCSIBus
*bus
, SCSIDevice
*dev
)
533 PVSCSIState
*s
= container_of(bus
, PVSCSIState
, bus
);
534 pvscsi_send_msg(s
, dev
, PVSCSI_MSG_DEV_REMOVED
);
538 pvscsi_request_cancelled(SCSIRequest
*req
)
540 PVSCSIRequest
*pvscsi_req
= req
->hba_private
;
541 PVSCSIState
*s
= pvscsi_req
->dev
;
543 if (pvscsi_req
->completed
) {
547 if (pvscsi_req
->dev
->resetting
) {
548 pvscsi_req
->cmp
.hostStatus
= BTSTAT_BUSRESET
;
550 pvscsi_req
->cmp
.hostStatus
= BTSTAT_ABORTQUEUE
;
553 pvscsi_complete_request(s
, pvscsi_req
);
557 pvscsi_device_find(PVSCSIState
*s
, int channel
, int target
,
558 uint8_t *requested_lun
, uint8_t *target_lun
)
560 if (requested_lun
[0] || requested_lun
[2] || requested_lun
[3] ||
561 requested_lun
[4] || requested_lun
[5] || requested_lun
[6] ||
562 requested_lun
[7] || (target
> PVSCSI_MAX_DEVS
)) {
565 *target_lun
= requested_lun
[1];
566 return scsi_device_find(&s
->bus
, channel
, target
, *target_lun
);
570 static PVSCSIRequest
*
571 pvscsi_queue_pending_descriptor(PVSCSIState
*s
, SCSIDevice
**d
,
572 struct PVSCSIRingReqDesc
*descr
)
574 PVSCSIRequest
*pvscsi_req
;
577 pvscsi_req
= g_malloc0(sizeof(*pvscsi_req
));
579 pvscsi_req
->req
= *descr
;
580 pvscsi_req
->cmp
.context
= pvscsi_req
->req
.context
;
581 QTAILQ_INSERT_TAIL(&s
->pending_queue
, pvscsi_req
, next
);
583 *d
= pvscsi_device_find(s
, descr
->bus
, descr
->target
, descr
->lun
, &lun
);
585 pvscsi_req
->lun
= lun
;
592 pvscsi_convert_sglist(PVSCSIRequest
*r
)
595 uint64_t data_length
= r
->req
.dataLen
;
596 PVSCSISGState sg
= r
->sg
;
597 while (data_length
) {
599 pvscsi_get_next_sg_elem(&sg
);
600 trace_pvscsi_convert_sglist(r
->req
.context
, r
->sg
.dataAddr
,
603 assert(data_length
> 0);
604 chunk_size
= MIN((unsigned) data_length
, sg
.resid
);
606 qemu_sglist_add(&r
->sgl
, sg
.dataAddr
, chunk_size
);
609 sg
.dataAddr
+= chunk_size
;
610 data_length
-= chunk_size
;
611 sg
.resid
-= chunk_size
;
616 pvscsi_build_sglist(PVSCSIState
*s
, PVSCSIRequest
*r
)
618 PCIDevice
*d
= PCI_DEVICE(s
);
620 pci_dma_sglist_init(&r
->sgl
, d
, 1);
621 if (r
->req
.flags
& PVSCSI_FLAG_CMD_WITH_SG_LIST
) {
622 pvscsi_convert_sglist(r
);
624 qemu_sglist_add(&r
->sgl
, r
->req
.dataAddr
, r
->req
.dataLen
);
629 pvscsi_process_request_descriptor(PVSCSIState
*s
,
630 struct PVSCSIRingReqDesc
*descr
)
633 PVSCSIRequest
*r
= pvscsi_queue_pending_descriptor(s
, &d
, descr
);
636 trace_pvscsi_process_req_descr(descr
->cdb
[0], descr
->context
);
639 r
->cmp
.hostStatus
= BTSTAT_SELTIMEO
;
640 trace_pvscsi_process_req_descr_unknown_device();
641 pvscsi_complete_request(s
, r
);
645 if (descr
->flags
& PVSCSI_FLAG_CMD_WITH_SG_LIST
) {
646 r
->sg
.elemAddr
= descr
->dataAddr
;
649 r
->sreq
= scsi_req_new(d
, descr
->context
, r
->lun
, descr
->cdb
, r
);
650 if (r
->sreq
->cmd
.mode
== SCSI_XFER_FROM_DEV
&&
651 (descr
->flags
& PVSCSI_FLAG_CMD_DIR_TODEVICE
)) {
652 r
->cmp
.hostStatus
= BTSTAT_BADMSG
;
653 trace_pvscsi_process_req_descr_invalid_dir();
654 scsi_req_cancel(r
->sreq
);
657 if (r
->sreq
->cmd
.mode
== SCSI_XFER_TO_DEV
&&
658 (descr
->flags
& PVSCSI_FLAG_CMD_DIR_TOHOST
)) {
659 r
->cmp
.hostStatus
= BTSTAT_BADMSG
;
660 trace_pvscsi_process_req_descr_invalid_dir();
661 scsi_req_cancel(r
->sreq
);
665 pvscsi_build_sglist(s
, r
);
666 n
= scsi_req_enqueue(r
->sreq
);
669 scsi_req_continue(r
->sreq
);
674 pvscsi_process_io(PVSCSIState
*s
)
676 PVSCSIRingReqDesc descr
;
677 hwaddr next_descr_pa
;
679 assert(s
->rings_info_valid
);
680 while ((next_descr_pa
= pvscsi_ring_pop_req_descr(&s
->rings
)) != 0) {
682 /* Only read after production index verification */
685 trace_pvscsi_process_io(next_descr_pa
);
686 cpu_physical_memory_read(next_descr_pa
, &descr
, sizeof(descr
));
687 pvscsi_process_request_descriptor(s
, &descr
);
690 pvscsi_ring_flush_req(&s
->rings
);
694 pvscsi_dbg_dump_tx_rings_config(PVSCSICmdDescSetupRings
*rc
)
697 trace_pvscsi_tx_rings_ppn("Rings State", rc
->ringsStatePPN
);
699 trace_pvscsi_tx_rings_num_pages("Request Ring", rc
->reqRingNumPages
);
700 for (i
= 0; i
< rc
->reqRingNumPages
; i
++) {
701 trace_pvscsi_tx_rings_ppn("Request Ring", rc
->reqRingPPNs
[i
]);
704 trace_pvscsi_tx_rings_num_pages("Confirm Ring", rc
->cmpRingNumPages
);
705 for (i
= 0; i
< rc
->cmpRingNumPages
; i
++) {
706 trace_pvscsi_tx_rings_ppn("Confirm Ring", rc
->reqRingPPNs
[i
]);
711 pvscsi_on_cmd_config(PVSCSIState
*s
)
713 trace_pvscsi_on_cmd_noimpl("PVSCSI_CMD_CONFIG");
714 return PVSCSI_COMMAND_PROCESSING_FAILED
;
718 pvscsi_on_cmd_unplug(PVSCSIState
*s
)
720 trace_pvscsi_on_cmd_noimpl("PVSCSI_CMD_DEVICE_UNPLUG");
721 return PVSCSI_COMMAND_PROCESSING_FAILED
;
725 pvscsi_on_issue_scsi(PVSCSIState
*s
)
727 trace_pvscsi_on_cmd_noimpl("PVSCSI_CMD_ISSUE_SCSI");
728 return PVSCSI_COMMAND_PROCESSING_FAILED
;
732 pvscsi_on_cmd_setup_rings(PVSCSIState
*s
)
734 PVSCSICmdDescSetupRings
*rc
=
735 (PVSCSICmdDescSetupRings
*) s
->curr_cmd_data
;
737 trace_pvscsi_on_cmd_arrived("PVSCSI_CMD_SETUP_RINGS");
739 pvscsi_dbg_dump_tx_rings_config(rc
);
740 pvscsi_ring_init_data(&s
->rings
, rc
);
741 s
->rings_info_valid
= TRUE
;
742 return PVSCSI_COMMAND_PROCESSING_SUCCEEDED
;
746 pvscsi_on_cmd_abort(PVSCSIState
*s
)
748 PVSCSICmdDescAbortCmd
*cmd
= (PVSCSICmdDescAbortCmd
*) s
->curr_cmd_data
;
749 PVSCSIRequest
*r
, *next
;
751 trace_pvscsi_on_cmd_abort(cmd
->context
, cmd
->target
);
753 QTAILQ_FOREACH_SAFE(r
, &s
->pending_queue
, next
, next
) {
754 if (r
->req
.context
== cmd
->context
) {
759 assert(!r
->completed
);
760 r
->cmp
.hostStatus
= BTSTAT_ABORTQUEUE
;
761 scsi_req_cancel(r
->sreq
);
764 return PVSCSI_COMMAND_PROCESSING_SUCCEEDED
;
768 pvscsi_on_cmd_unknown(PVSCSIState
*s
)
770 trace_pvscsi_on_cmd_unknown_data(s
->curr_cmd_data
[0]);
771 return PVSCSI_COMMAND_PROCESSING_FAILED
;
775 pvscsi_on_cmd_reset_device(PVSCSIState
*s
)
777 uint8_t target_lun
= 0;
778 struct PVSCSICmdDescResetDevice
*cmd
=
779 (struct PVSCSICmdDescResetDevice
*) s
->curr_cmd_data
;
782 sdev
= pvscsi_device_find(s
, 0, cmd
->target
, cmd
->lun
, &target_lun
);
784 trace_pvscsi_on_cmd_reset_dev(cmd
->target
, (int) target_lun
, sdev
);
788 device_reset(&sdev
->qdev
);
790 return PVSCSI_COMMAND_PROCESSING_SUCCEEDED
;
793 return PVSCSI_COMMAND_PROCESSING_FAILED
;
797 pvscsi_on_cmd_reset_bus(PVSCSIState
*s
)
799 trace_pvscsi_on_cmd_arrived("PVSCSI_CMD_RESET_BUS");
802 qbus_reset_all_fn(&s
->bus
);
804 return PVSCSI_COMMAND_PROCESSING_SUCCEEDED
;
808 pvscsi_on_cmd_setup_msg_ring(PVSCSIState
*s
)
810 PVSCSICmdDescSetupMsgRing
*rc
=
811 (PVSCSICmdDescSetupMsgRing
*) s
->curr_cmd_data
;
813 trace_pvscsi_on_cmd_arrived("PVSCSI_CMD_SETUP_MSG_RING");
816 return PVSCSI_COMMAND_PROCESSING_FAILED
;
819 if (s
->rings_info_valid
) {
820 pvscsi_ring_init_msg(&s
->rings
, rc
);
821 s
->msg_ring_info_valid
= TRUE
;
823 return sizeof(PVSCSICmdDescSetupMsgRing
) / sizeof(uint32_t);
827 pvscsi_on_cmd_adapter_reset(PVSCSIState
*s
)
829 trace_pvscsi_on_cmd_arrived("PVSCSI_CMD_ADAPTER_RESET");
831 pvscsi_reset_adapter(s
);
832 return PVSCSI_COMMAND_PROCESSING_SUCCEEDED
;
835 static const struct {
837 uint64_t (*handler_fn
)(PVSCSIState
*s
);
838 } pvscsi_commands
[] = {
839 [PVSCSI_CMD_FIRST
] = {
841 .handler_fn
= pvscsi_on_cmd_unknown
,
844 /* Not implemented, data size defined based on what arrives on windows */
845 [PVSCSI_CMD_CONFIG
] = {
846 .data_size
= 6 * sizeof(uint32_t),
847 .handler_fn
= pvscsi_on_cmd_config
,
850 /* Command not implemented, data size is unknown */
851 [PVSCSI_CMD_ISSUE_SCSI
] = {
853 .handler_fn
= pvscsi_on_issue_scsi
,
856 /* Command not implemented, data size is unknown */
857 [PVSCSI_CMD_DEVICE_UNPLUG
] = {
859 .handler_fn
= pvscsi_on_cmd_unplug
,
862 [PVSCSI_CMD_SETUP_RINGS
] = {
863 .data_size
= sizeof(PVSCSICmdDescSetupRings
),
864 .handler_fn
= pvscsi_on_cmd_setup_rings
,
867 [PVSCSI_CMD_RESET_DEVICE
] = {
868 .data_size
= sizeof(struct PVSCSICmdDescResetDevice
),
869 .handler_fn
= pvscsi_on_cmd_reset_device
,
872 [PVSCSI_CMD_RESET_BUS
] = {
874 .handler_fn
= pvscsi_on_cmd_reset_bus
,
877 [PVSCSI_CMD_SETUP_MSG_RING
] = {
878 .data_size
= sizeof(PVSCSICmdDescSetupMsgRing
),
879 .handler_fn
= pvscsi_on_cmd_setup_msg_ring
,
882 [PVSCSI_CMD_ADAPTER_RESET
] = {
884 .handler_fn
= pvscsi_on_cmd_adapter_reset
,
887 [PVSCSI_CMD_ABORT_CMD
] = {
888 .data_size
= sizeof(struct PVSCSICmdDescAbortCmd
),
889 .handler_fn
= pvscsi_on_cmd_abort
,
894 pvscsi_do_command_processing(PVSCSIState
*s
)
896 size_t bytes_arrived
= s
->curr_cmd_data_cntr
* sizeof(uint32_t);
898 assert(s
->curr_cmd
< PVSCSI_CMD_LAST
);
899 if (bytes_arrived
>= pvscsi_commands
[s
->curr_cmd
].data_size
) {
900 s
->reg_command_status
= pvscsi_commands
[s
->curr_cmd
].handler_fn(s
);
901 s
->curr_cmd
= PVSCSI_CMD_FIRST
;
902 s
->curr_cmd_data_cntr
= 0;
907 pvscsi_on_command_data(PVSCSIState
*s
, uint32_t value
)
909 size_t bytes_arrived
= s
->curr_cmd_data_cntr
* sizeof(uint32_t);
911 assert(bytes_arrived
< sizeof(s
->curr_cmd_data
));
912 s
->curr_cmd_data
[s
->curr_cmd_data_cntr
++] = value
;
914 pvscsi_do_command_processing(s
);
918 pvscsi_on_command(PVSCSIState
*s
, uint64_t cmd_id
)
920 if ((cmd_id
> PVSCSI_CMD_FIRST
) && (cmd_id
< PVSCSI_CMD_LAST
)) {
921 s
->curr_cmd
= cmd_id
;
923 s
->curr_cmd
= PVSCSI_CMD_FIRST
;
924 trace_pvscsi_on_cmd_unknown(cmd_id
);
927 s
->curr_cmd_data_cntr
= 0;
928 s
->reg_command_status
= PVSCSI_COMMAND_NOT_ENOUGH_DATA
;
930 pvscsi_do_command_processing(s
);
934 pvscsi_io_write(void *opaque
, hwaddr addr
,
935 uint64_t val
, unsigned size
)
937 PVSCSIState
*s
= opaque
;
940 case PVSCSI_REG_OFFSET_COMMAND
:
941 pvscsi_on_command(s
, val
);
944 case PVSCSI_REG_OFFSET_COMMAND_DATA
:
945 pvscsi_on_command_data(s
, (uint32_t) val
);
948 case PVSCSI_REG_OFFSET_INTR_STATUS
:
949 trace_pvscsi_io_write("PVSCSI_REG_OFFSET_INTR_STATUS", val
);
950 s
->reg_interrupt_status
&= ~val
;
951 pvscsi_update_irq_status(s
);
952 pvscsi_schedule_completion_processing(s
);
955 case PVSCSI_REG_OFFSET_INTR_MASK
:
956 trace_pvscsi_io_write("PVSCSI_REG_OFFSET_INTR_MASK", val
);
957 s
->reg_interrupt_enabled
= val
;
958 pvscsi_update_irq_status(s
);
961 case PVSCSI_REG_OFFSET_KICK_NON_RW_IO
:
962 trace_pvscsi_io_write("PVSCSI_REG_OFFSET_KICK_NON_RW_IO", val
);
963 pvscsi_process_io(s
);
966 case PVSCSI_REG_OFFSET_KICK_RW_IO
:
967 trace_pvscsi_io_write("PVSCSI_REG_OFFSET_KICK_RW_IO", val
);
968 pvscsi_process_io(s
);
971 case PVSCSI_REG_OFFSET_DEBUG
:
972 trace_pvscsi_io_write("PVSCSI_REG_OFFSET_DEBUG", val
);
976 trace_pvscsi_io_write_unknown(addr
, size
, val
);
983 pvscsi_io_read(void *opaque
, hwaddr addr
, unsigned size
)
985 PVSCSIState
*s
= opaque
;
988 case PVSCSI_REG_OFFSET_INTR_STATUS
:
989 trace_pvscsi_io_read("PVSCSI_REG_OFFSET_INTR_STATUS",
990 s
->reg_interrupt_status
);
991 return s
->reg_interrupt_status
;
993 case PVSCSI_REG_OFFSET_INTR_MASK
:
994 trace_pvscsi_io_read("PVSCSI_REG_OFFSET_INTR_MASK",
995 s
->reg_interrupt_status
);
996 return s
->reg_interrupt_enabled
;
998 case PVSCSI_REG_OFFSET_COMMAND_STATUS
:
999 trace_pvscsi_io_read("PVSCSI_REG_OFFSET_COMMAND_STATUS",
1000 s
->reg_interrupt_status
);
1001 return s
->reg_command_status
;
1004 trace_pvscsi_io_read_unknown(addr
, size
);
1011 pvscsi_init_msi(PVSCSIState
*s
)
1014 PCIDevice
*d
= PCI_DEVICE(s
);
1016 res
= msi_init(d
, PVSCSI_MSI_OFFSET
, PVSCSI_MSIX_NUM_VECTORS
,
1017 PVSCSI_USE_64BIT
, PVSCSI_PER_VECTOR_MASK
);
1019 trace_pvscsi_init_msi_fail(res
);
1020 s
->msi_used
= false;
1029 pvscsi_cleanup_msi(PVSCSIState
*s
)
1031 PCIDevice
*d
= PCI_DEVICE(s
);
1038 static const MemoryRegionOps pvscsi_ops
= {
1039 .read
= pvscsi_io_read
,
1040 .write
= pvscsi_io_write
,
1041 .endianness
= DEVICE_LITTLE_ENDIAN
,
1043 .min_access_size
= 4,
1044 .max_access_size
= 4,
1048 static const struct SCSIBusInfo pvscsi_scsi_info
= {
1050 .max_target
= PVSCSI_MAX_DEVS
,
1054 .get_sg_list
= pvscsi_get_sg_list
,
1055 .complete
= pvscsi_command_complete
,
1056 .cancel
= pvscsi_request_cancelled
,
1057 .hotplug
= pvscsi_hotplug
,
1058 .hot_unplug
= pvscsi_hot_unplug
,
1062 pvscsi_init(PCIDevice
*pci_dev
)
1064 PVSCSIState
*s
= PVSCSI(pci_dev
);
1066 trace_pvscsi_state("init");
1068 /* PCI subsystem ID */
1069 pci_dev
->config
[PCI_SUBSYSTEM_ID
] = 0x00;
1070 pci_dev
->config
[PCI_SUBSYSTEM_ID
+ 1] = 0x10;
1072 /* PCI latency timer = 255 */
1073 pci_dev
->config
[PCI_LATENCY_TIMER
] = 0xff;
1075 /* Interrupt pin A */
1076 pci_config_set_interrupt_pin(pci_dev
->config
, 1);
1078 memory_region_init_io(&s
->io_space
, NULL
, &pvscsi_ops
, s
,
1079 "pvscsi-io", PVSCSI_MEM_SPACE_SIZE
);
1080 pci_register_bar(pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
, &s
->io_space
);
1084 s
->completion_worker
= qemu_bh_new(pvscsi_process_completion_queue
, s
);
1085 if (!s
->completion_worker
) {
1086 pvscsi_cleanup_msi(s
);
1087 memory_region_destroy(&s
->io_space
);
1091 scsi_bus_new(&s
->bus
, &pci_dev
->qdev
, &pvscsi_scsi_info
, NULL
);
1092 pvscsi_reset_state(s
);
1098 pvscsi_uninit(PCIDevice
*pci_dev
)
1100 PVSCSIState
*s
= PVSCSI(pci_dev
);
1102 trace_pvscsi_state("uninit");
1103 qemu_bh_delete(s
->completion_worker
);
1105 pvscsi_cleanup_msi(s
);
1107 memory_region_destroy(&s
->io_space
);
1111 pvscsi_reset(DeviceState
*dev
)
1113 PCIDevice
*d
= PCI_DEVICE(dev
);
1114 PVSCSIState
*s
= PVSCSI(d
);
1116 trace_pvscsi_state("reset");
1117 pvscsi_reset_adapter(s
);
1121 pvscsi_pre_save(void *opaque
)
1123 PVSCSIState
*s
= (PVSCSIState
*) opaque
;
1125 trace_pvscsi_state("presave");
1127 assert(QTAILQ_EMPTY(&s
->pending_queue
));
1128 assert(QTAILQ_EMPTY(&s
->completion_queue
));
1132 pvscsi_post_load(void *opaque
, int version_id
)
1134 trace_pvscsi_state("postload");
1138 static const VMStateDescription vmstate_pvscsi
= {
1139 .name
= TYPE_PVSCSI
,
1141 .minimum_version_id
= 0,
1142 .minimum_version_id_old
= 0,
1143 .pre_save
= pvscsi_pre_save
,
1144 .post_load
= pvscsi_post_load
,
1145 .fields
= (VMStateField
[]) {
1146 VMSTATE_PCI_DEVICE(parent_obj
, PVSCSIState
),
1147 VMSTATE_UINT8(msi_used
, PVSCSIState
),
1148 VMSTATE_UINT32(resetting
, PVSCSIState
),
1149 VMSTATE_UINT64(reg_interrupt_status
, PVSCSIState
),
1150 VMSTATE_UINT64(reg_interrupt_enabled
, PVSCSIState
),
1151 VMSTATE_UINT64(reg_command_status
, PVSCSIState
),
1152 VMSTATE_UINT64(curr_cmd
, PVSCSIState
),
1153 VMSTATE_UINT32(curr_cmd_data_cntr
, PVSCSIState
),
1154 VMSTATE_UINT32_ARRAY(curr_cmd_data
, PVSCSIState
,
1155 ARRAY_SIZE(((PVSCSIState
*)NULL
)->curr_cmd_data
)),
1156 VMSTATE_UINT8(rings_info_valid
, PVSCSIState
),
1157 VMSTATE_UINT8(msg_ring_info_valid
, PVSCSIState
),
1158 VMSTATE_UINT8(use_msg
, PVSCSIState
),
1160 VMSTATE_UINT64(rings
.rs_pa
, PVSCSIState
),
1161 VMSTATE_UINT32(rings
.txr_len_mask
, PVSCSIState
),
1162 VMSTATE_UINT32(rings
.rxr_len_mask
, PVSCSIState
),
1163 VMSTATE_UINT64_ARRAY(rings
.req_ring_pages_pa
, PVSCSIState
,
1164 PVSCSI_SETUP_RINGS_MAX_NUM_PAGES
),
1165 VMSTATE_UINT64_ARRAY(rings
.cmp_ring_pages_pa
, PVSCSIState
,
1166 PVSCSI_SETUP_RINGS_MAX_NUM_PAGES
),
1167 VMSTATE_UINT64(rings
.consumed_ptr
, PVSCSIState
),
1168 VMSTATE_UINT64(rings
.filled_cmp_ptr
, PVSCSIState
),
1170 VMSTATE_END_OF_LIST()
1175 pvscsi_write_config(PCIDevice
*pci
, uint32_t addr
, uint32_t val
, int len
)
1177 pci_default_write_config(pci
, addr
, val
, len
);
1178 msi_write_config(pci
, addr
, val
, len
);
1181 static Property pvscsi_properties
[] = {
1182 DEFINE_PROP_UINT8("use_msg", PVSCSIState
, use_msg
, 1),
1183 DEFINE_PROP_END_OF_LIST(),
1186 static void pvscsi_class_init(ObjectClass
*klass
, void *data
)
1188 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1189 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1191 k
->init
= pvscsi_init
;
1192 k
->exit
= pvscsi_uninit
;
1193 k
->vendor_id
= PCI_VENDOR_ID_VMWARE
;
1194 k
->device_id
= PCI_DEVICE_ID_VMWARE_PVSCSI
;
1195 k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1196 k
->subsystem_id
= 0x1000;
1197 dc
->reset
= pvscsi_reset
;
1198 dc
->vmsd
= &vmstate_pvscsi
;
1199 dc
->props
= pvscsi_properties
;
1200 k
->config_write
= pvscsi_write_config
;
1203 static const TypeInfo pvscsi_info
= {
1205 .parent
= TYPE_PCI_DEVICE
,
1206 .instance_size
= sizeof(PVSCSIState
),
1207 .class_init
= pvscsi_class_init
,
1211 pvscsi_register_types(void)
1213 type_register_static(&pvscsi_info
);
1216 type_init(pvscsi_register_types
);