virtio-pci: make QEMU_VIRTIO_PCI_QUEUE_MEM_MULT smaller
[qemu/cris-port.git] / hw / scsi / vmw_pvscsi.c
blobc6148d380e2b253b1ecb78d1b4a59b852efe1933
1 /*
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
11 * Authors:
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.
19 * NOTE about MSI-X:
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"
32 #include "trace.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(m, field) \
46 (ldl_le_pci_dma(&container_of(m, PVSCSIState, rings)->parent_obj, \
47 (m)->rs_pa + offsetof(struct PVSCSIRingsState, field)))
48 #define RS_SET_FIELD(m, field, val) \
49 (stl_le_pci_dma(&container_of(m, PVSCSIState, rings)->parent_obj, \
50 (m)->rs_pa + offsetof(struct PVSCSIRingsState, field), val))
52 #define TYPE_PVSCSI "pvscsi"
53 #define PVSCSI(obj) OBJECT_CHECK(PVSCSIState, (obj), TYPE_PVSCSI)
55 typedef struct PVSCSIRingInfo {
56 uint64_t rs_pa;
57 uint32_t txr_len_mask;
58 uint32_t rxr_len_mask;
59 uint32_t msg_len_mask;
60 uint64_t req_ring_pages_pa[PVSCSI_SETUP_RINGS_MAX_NUM_PAGES];
61 uint64_t cmp_ring_pages_pa[PVSCSI_SETUP_RINGS_MAX_NUM_PAGES];
62 uint64_t msg_ring_pages_pa[PVSCSI_SETUP_MSG_RING_MAX_NUM_PAGES];
63 uint64_t consumed_ptr;
64 uint64_t filled_cmp_ptr;
65 uint64_t filled_msg_ptr;
66 } PVSCSIRingInfo;
68 typedef struct PVSCSISGState {
69 hwaddr elemAddr;
70 hwaddr dataAddr;
71 uint32_t resid;
72 } PVSCSISGState;
74 typedef QTAILQ_HEAD(, PVSCSIRequest) PVSCSIRequestList;
76 typedef struct {
77 PCIDevice parent_obj;
78 MemoryRegion io_space;
79 SCSIBus bus;
80 QEMUBH *completion_worker;
81 PVSCSIRequestList pending_queue;
82 PVSCSIRequestList completion_queue;
84 uint64_t reg_interrupt_status; /* Interrupt status register value */
85 uint64_t reg_interrupt_enabled; /* Interrupt mask register value */
86 uint64_t reg_command_status; /* Command status register value */
88 /* Command data adoption mechanism */
89 uint64_t curr_cmd; /* Last command arrived */
90 uint32_t curr_cmd_data_cntr; /* Amount of data for last command */
92 /* Collector for current command data */
93 uint32_t curr_cmd_data[PVSCSI_MAX_CMD_DATA_WORDS];
95 uint8_t rings_info_valid; /* Whether data rings initialized */
96 uint8_t msg_ring_info_valid; /* Whether message ring initialized */
97 uint8_t use_msg; /* Whether to use message ring */
99 uint8_t msi_used; /* Whether MSI support was installed successfully */
101 PVSCSIRingInfo rings; /* Data transfer rings manager */
102 uint32_t resetting; /* Reset in progress */
103 } PVSCSIState;
105 typedef struct PVSCSIRequest {
106 SCSIRequest *sreq;
107 PVSCSIState *dev;
108 uint8_t sense_key;
109 uint8_t completed;
110 int lun;
111 QEMUSGList sgl;
112 PVSCSISGState sg;
113 struct PVSCSIRingReqDesc req;
114 struct PVSCSIRingCmpDesc cmp;
115 QTAILQ_ENTRY(PVSCSIRequest) next;
116 } PVSCSIRequest;
118 /* Integer binary logarithm */
119 static int
120 pvscsi_log2(uint32_t input)
122 int log = 0;
123 assert(input > 0);
124 while (input >> ++log) {
126 return log;
129 static void
130 pvscsi_ring_init_data(PVSCSIRingInfo *m, PVSCSICmdDescSetupRings *ri)
132 int i;
133 uint32_t txr_len_log2, rxr_len_log2;
134 uint32_t req_ring_size, cmp_ring_size;
135 m->rs_pa = ri->ringsStatePPN << VMW_PAGE_SHIFT;
137 req_ring_size = ri->reqRingNumPages * PVSCSI_MAX_NUM_REQ_ENTRIES_PER_PAGE;
138 cmp_ring_size = ri->cmpRingNumPages * PVSCSI_MAX_NUM_CMP_ENTRIES_PER_PAGE;
139 txr_len_log2 = pvscsi_log2(req_ring_size - 1);
140 rxr_len_log2 = pvscsi_log2(cmp_ring_size - 1);
142 m->txr_len_mask = MASK(txr_len_log2);
143 m->rxr_len_mask = MASK(rxr_len_log2);
145 m->consumed_ptr = 0;
146 m->filled_cmp_ptr = 0;
148 for (i = 0; i < ri->reqRingNumPages; i++) {
149 m->req_ring_pages_pa[i] = ri->reqRingPPNs[i] << VMW_PAGE_SHIFT;
152 for (i = 0; i < ri->cmpRingNumPages; i++) {
153 m->cmp_ring_pages_pa[i] = ri->cmpRingPPNs[i] << VMW_PAGE_SHIFT;
156 RS_SET_FIELD(m, reqProdIdx, 0);
157 RS_SET_FIELD(m, reqConsIdx, 0);
158 RS_SET_FIELD(m, reqNumEntriesLog2, txr_len_log2);
160 RS_SET_FIELD(m, cmpProdIdx, 0);
161 RS_SET_FIELD(m, cmpConsIdx, 0);
162 RS_SET_FIELD(m, cmpNumEntriesLog2, rxr_len_log2);
164 trace_pvscsi_ring_init_data(txr_len_log2, rxr_len_log2);
166 /* Flush ring state page changes */
167 smp_wmb();
170 static void
171 pvscsi_ring_init_msg(PVSCSIRingInfo *m, PVSCSICmdDescSetupMsgRing *ri)
173 int i;
174 uint32_t len_log2;
175 uint32_t ring_size;
177 ring_size = ri->numPages * PVSCSI_MAX_NUM_MSG_ENTRIES_PER_PAGE;
178 len_log2 = pvscsi_log2(ring_size - 1);
180 m->msg_len_mask = MASK(len_log2);
182 m->filled_msg_ptr = 0;
184 for (i = 0; i < ri->numPages; i++) {
185 m->msg_ring_pages_pa[i] = ri->ringPPNs[i] << VMW_PAGE_SHIFT;
188 RS_SET_FIELD(m, msgProdIdx, 0);
189 RS_SET_FIELD(m, msgConsIdx, 0);
190 RS_SET_FIELD(m, msgNumEntriesLog2, len_log2);
192 trace_pvscsi_ring_init_msg(len_log2);
194 /* Flush ring state page changes */
195 smp_wmb();
198 static void
199 pvscsi_ring_cleanup(PVSCSIRingInfo *mgr)
201 mgr->rs_pa = 0;
202 mgr->txr_len_mask = 0;
203 mgr->rxr_len_mask = 0;
204 mgr->msg_len_mask = 0;
205 mgr->consumed_ptr = 0;
206 mgr->filled_cmp_ptr = 0;
207 mgr->filled_msg_ptr = 0;
208 memset(mgr->req_ring_pages_pa, 0, sizeof(mgr->req_ring_pages_pa));
209 memset(mgr->cmp_ring_pages_pa, 0, sizeof(mgr->cmp_ring_pages_pa));
210 memset(mgr->msg_ring_pages_pa, 0, sizeof(mgr->msg_ring_pages_pa));
213 static hwaddr
214 pvscsi_ring_pop_req_descr(PVSCSIRingInfo *mgr)
216 uint32_t ready_ptr = RS_GET_FIELD(mgr, reqProdIdx);
218 if (ready_ptr != mgr->consumed_ptr) {
219 uint32_t next_ready_ptr =
220 mgr->consumed_ptr++ & mgr->txr_len_mask;
221 uint32_t next_ready_page =
222 next_ready_ptr / PVSCSI_MAX_NUM_REQ_ENTRIES_PER_PAGE;
223 uint32_t inpage_idx =
224 next_ready_ptr % PVSCSI_MAX_NUM_REQ_ENTRIES_PER_PAGE;
226 return mgr->req_ring_pages_pa[next_ready_page] +
227 inpage_idx * sizeof(PVSCSIRingReqDesc);
228 } else {
229 return 0;
233 static void
234 pvscsi_ring_flush_req(PVSCSIRingInfo *mgr)
236 RS_SET_FIELD(mgr, reqConsIdx, mgr->consumed_ptr);
239 static hwaddr
240 pvscsi_ring_pop_cmp_descr(PVSCSIRingInfo *mgr)
243 * According to Linux driver code it explicitly verifies that number
244 * of requests being processed by device is less then the size of
245 * completion queue, so device may omit completion queue overflow
246 * conditions check. We assume that this is true for other (Windows)
247 * drivers as well.
250 uint32_t free_cmp_ptr =
251 mgr->filled_cmp_ptr++ & mgr->rxr_len_mask;
252 uint32_t free_cmp_page =
253 free_cmp_ptr / PVSCSI_MAX_NUM_CMP_ENTRIES_PER_PAGE;
254 uint32_t inpage_idx =
255 free_cmp_ptr % PVSCSI_MAX_NUM_CMP_ENTRIES_PER_PAGE;
256 return mgr->cmp_ring_pages_pa[free_cmp_page] +
257 inpage_idx * sizeof(PVSCSIRingCmpDesc);
260 static hwaddr
261 pvscsi_ring_pop_msg_descr(PVSCSIRingInfo *mgr)
263 uint32_t free_msg_ptr =
264 mgr->filled_msg_ptr++ & mgr->msg_len_mask;
265 uint32_t free_msg_page =
266 free_msg_ptr / PVSCSI_MAX_NUM_MSG_ENTRIES_PER_PAGE;
267 uint32_t inpage_idx =
268 free_msg_ptr % PVSCSI_MAX_NUM_MSG_ENTRIES_PER_PAGE;
269 return mgr->msg_ring_pages_pa[free_msg_page] +
270 inpage_idx * sizeof(PVSCSIRingMsgDesc);
273 static void
274 pvscsi_ring_flush_cmp(PVSCSIRingInfo *mgr)
276 /* Flush descriptor changes */
277 smp_wmb();
279 trace_pvscsi_ring_flush_cmp(mgr->filled_cmp_ptr);
281 RS_SET_FIELD(mgr, cmpProdIdx, mgr->filled_cmp_ptr);
284 static bool
285 pvscsi_ring_msg_has_room(PVSCSIRingInfo *mgr)
287 uint32_t prodIdx = RS_GET_FIELD(mgr, msgProdIdx);
288 uint32_t consIdx = RS_GET_FIELD(mgr, msgConsIdx);
290 return (prodIdx - consIdx) < (mgr->msg_len_mask + 1);
293 static void
294 pvscsi_ring_flush_msg(PVSCSIRingInfo *mgr)
296 /* Flush descriptor changes */
297 smp_wmb();
299 trace_pvscsi_ring_flush_msg(mgr->filled_msg_ptr);
301 RS_SET_FIELD(mgr, msgProdIdx, mgr->filled_msg_ptr);
304 static void
305 pvscsi_reset_state(PVSCSIState *s)
307 s->curr_cmd = PVSCSI_CMD_FIRST;
308 s->curr_cmd_data_cntr = 0;
309 s->reg_command_status = PVSCSI_COMMAND_PROCESSING_SUCCEEDED;
310 s->reg_interrupt_status = 0;
311 pvscsi_ring_cleanup(&s->rings);
312 s->rings_info_valid = FALSE;
313 s->msg_ring_info_valid = FALSE;
314 QTAILQ_INIT(&s->pending_queue);
315 QTAILQ_INIT(&s->completion_queue);
318 static void
319 pvscsi_update_irq_status(PVSCSIState *s)
321 PCIDevice *d = PCI_DEVICE(s);
322 bool should_raise = s->reg_interrupt_enabled & s->reg_interrupt_status;
324 trace_pvscsi_update_irq_level(should_raise, s->reg_interrupt_enabled,
325 s->reg_interrupt_status);
327 if (s->msi_used && msi_enabled(d)) {
328 if (should_raise) {
329 trace_pvscsi_update_irq_msi();
330 msi_notify(d, PVSCSI_VECTOR_COMPLETION);
332 return;
335 pci_set_irq(d, !!should_raise);
338 static void
339 pvscsi_raise_completion_interrupt(PVSCSIState *s)
341 s->reg_interrupt_status |= PVSCSI_INTR_CMPL_0;
343 /* Memory barrier to flush interrupt status register changes*/
344 smp_wmb();
346 pvscsi_update_irq_status(s);
349 static void
350 pvscsi_raise_message_interrupt(PVSCSIState *s)
352 s->reg_interrupt_status |= PVSCSI_INTR_MSG_0;
354 /* Memory barrier to flush interrupt status register changes*/
355 smp_wmb();
357 pvscsi_update_irq_status(s);
360 static void
361 pvscsi_cmp_ring_put(PVSCSIState *s, struct PVSCSIRingCmpDesc *cmp_desc)
363 hwaddr cmp_descr_pa;
365 cmp_descr_pa = pvscsi_ring_pop_cmp_descr(&s->rings);
366 trace_pvscsi_cmp_ring_put(cmp_descr_pa);
367 cpu_physical_memory_write(cmp_descr_pa, (void *)cmp_desc,
368 sizeof(*cmp_desc));
371 static void
372 pvscsi_msg_ring_put(PVSCSIState *s, struct PVSCSIRingMsgDesc *msg_desc)
374 hwaddr msg_descr_pa;
376 msg_descr_pa = pvscsi_ring_pop_msg_descr(&s->rings);
377 trace_pvscsi_msg_ring_put(msg_descr_pa);
378 cpu_physical_memory_write(msg_descr_pa, (void *)msg_desc,
379 sizeof(*msg_desc));
382 static void
383 pvscsi_process_completion_queue(void *opaque)
385 PVSCSIState *s = opaque;
386 PVSCSIRequest *pvscsi_req;
387 bool has_completed = false;
389 while (!QTAILQ_EMPTY(&s->completion_queue)) {
390 pvscsi_req = QTAILQ_FIRST(&s->completion_queue);
391 QTAILQ_REMOVE(&s->completion_queue, pvscsi_req, next);
392 pvscsi_cmp_ring_put(s, &pvscsi_req->cmp);
393 g_free(pvscsi_req);
394 has_completed = true;
397 if (has_completed) {
398 pvscsi_ring_flush_cmp(&s->rings);
399 pvscsi_raise_completion_interrupt(s);
403 static void
404 pvscsi_reset_adapter(PVSCSIState *s)
406 s->resetting++;
407 qbus_reset_all_fn(&s->bus);
408 s->resetting--;
409 pvscsi_process_completion_queue(s);
410 assert(QTAILQ_EMPTY(&s->pending_queue));
411 pvscsi_reset_state(s);
414 static void
415 pvscsi_schedule_completion_processing(PVSCSIState *s)
417 /* Try putting more complete requests on the ring. */
418 if (!QTAILQ_EMPTY(&s->completion_queue)) {
419 qemu_bh_schedule(s->completion_worker);
423 static void
424 pvscsi_complete_request(PVSCSIState *s, PVSCSIRequest *r)
426 assert(!r->completed);
428 trace_pvscsi_complete_request(r->cmp.context, r->cmp.dataLen,
429 r->sense_key);
430 if (r->sreq != NULL) {
431 scsi_req_unref(r->sreq);
432 r->sreq = NULL;
434 r->completed = 1;
435 QTAILQ_REMOVE(&s->pending_queue, r, next);
436 QTAILQ_INSERT_TAIL(&s->completion_queue, r, next);
437 pvscsi_schedule_completion_processing(s);
440 static QEMUSGList *pvscsi_get_sg_list(SCSIRequest *r)
442 PVSCSIRequest *req = r->hba_private;
444 trace_pvscsi_get_sg_list(req->sgl.nsg, req->sgl.size);
446 return &req->sgl;
449 static void
450 pvscsi_get_next_sg_elem(PVSCSISGState *sg)
452 struct PVSCSISGElement elem;
454 cpu_physical_memory_read(sg->elemAddr, (void *)&elem, sizeof(elem));
455 if ((elem.flags & ~PVSCSI_KNOWN_FLAGS) != 0) {
457 * There is PVSCSI_SGE_FLAG_CHAIN_ELEMENT flag described in
458 * header file but its value is unknown. This flag requires
459 * additional processing, so we put warning here to catch it
460 * some day and make proper implementation
462 trace_pvscsi_get_next_sg_elem(elem.flags);
465 sg->elemAddr += sizeof(elem);
466 sg->dataAddr = elem.addr;
467 sg->resid = elem.length;
470 static void
471 pvscsi_write_sense(PVSCSIRequest *r, uint8_t *sense, int len)
473 r->cmp.senseLen = MIN(r->req.senseLen, len);
474 r->sense_key = sense[(sense[0] & 2) ? 1 : 2];
475 cpu_physical_memory_write(r->req.senseAddr, sense, r->cmp.senseLen);
478 static void
479 pvscsi_command_complete(SCSIRequest *req, uint32_t status, size_t resid)
481 PVSCSIRequest *pvscsi_req = req->hba_private;
482 PVSCSIState *s;
484 if (!pvscsi_req) {
485 trace_pvscsi_command_complete_not_found(req->tag);
486 return;
488 s = pvscsi_req->dev;
490 if (resid) {
491 /* Short transfer. */
492 trace_pvscsi_command_complete_data_run();
493 pvscsi_req->cmp.hostStatus = BTSTAT_DATARUN;
496 pvscsi_req->cmp.scsiStatus = status;
497 if (pvscsi_req->cmp.scsiStatus == CHECK_CONDITION) {
498 uint8_t sense[SCSI_SENSE_BUF_SIZE];
499 int sense_len =
500 scsi_req_get_sense(pvscsi_req->sreq, sense, sizeof(sense));
502 trace_pvscsi_command_complete_sense_len(sense_len);
503 pvscsi_write_sense(pvscsi_req, sense, sense_len);
505 qemu_sglist_destroy(&pvscsi_req->sgl);
506 pvscsi_complete_request(s, pvscsi_req);
509 static void
510 pvscsi_send_msg(PVSCSIState *s, SCSIDevice *dev, uint32_t msg_type)
512 if (s->msg_ring_info_valid && pvscsi_ring_msg_has_room(&s->rings)) {
513 PVSCSIMsgDescDevStatusChanged msg = {0};
515 msg.type = msg_type;
516 msg.bus = dev->channel;
517 msg.target = dev->id;
518 msg.lun[1] = dev->lun;
520 pvscsi_msg_ring_put(s, (PVSCSIRingMsgDesc *)&msg);
521 pvscsi_ring_flush_msg(&s->rings);
522 pvscsi_raise_message_interrupt(s);
526 static void
527 pvscsi_hotplug(HotplugHandler *hotplug_dev, DeviceState *dev, Error **errp)
529 PVSCSIState *s = PVSCSI(hotplug_dev);
531 pvscsi_send_msg(s, SCSI_DEVICE(dev), PVSCSI_MSG_DEV_ADDED);
534 static void
535 pvscsi_hot_unplug(HotplugHandler *hotplug_dev, DeviceState *dev, Error **errp)
537 PVSCSIState *s = PVSCSI(hotplug_dev);
539 pvscsi_send_msg(s, SCSI_DEVICE(dev), PVSCSI_MSG_DEV_REMOVED);
540 qdev_simple_device_unplug_cb(hotplug_dev, dev, errp);
543 static void
544 pvscsi_request_cancelled(SCSIRequest *req)
546 PVSCSIRequest *pvscsi_req = req->hba_private;
547 PVSCSIState *s = pvscsi_req->dev;
549 if (pvscsi_req->completed) {
550 return;
553 if (pvscsi_req->dev->resetting) {
554 pvscsi_req->cmp.hostStatus = BTSTAT_BUSRESET;
555 } else {
556 pvscsi_req->cmp.hostStatus = BTSTAT_ABORTQUEUE;
559 pvscsi_complete_request(s, pvscsi_req);
562 static SCSIDevice*
563 pvscsi_device_find(PVSCSIState *s, int channel, int target,
564 uint8_t *requested_lun, uint8_t *target_lun)
566 if (requested_lun[0] || requested_lun[2] || requested_lun[3] ||
567 requested_lun[4] || requested_lun[5] || requested_lun[6] ||
568 requested_lun[7] || (target > PVSCSI_MAX_DEVS)) {
569 return NULL;
570 } else {
571 *target_lun = requested_lun[1];
572 return scsi_device_find(&s->bus, channel, target, *target_lun);
576 static PVSCSIRequest *
577 pvscsi_queue_pending_descriptor(PVSCSIState *s, SCSIDevice **d,
578 struct PVSCSIRingReqDesc *descr)
580 PVSCSIRequest *pvscsi_req;
581 uint8_t lun;
583 pvscsi_req = g_malloc0(sizeof(*pvscsi_req));
584 pvscsi_req->dev = s;
585 pvscsi_req->req = *descr;
586 pvscsi_req->cmp.context = pvscsi_req->req.context;
587 QTAILQ_INSERT_TAIL(&s->pending_queue, pvscsi_req, next);
589 *d = pvscsi_device_find(s, descr->bus, descr->target, descr->lun, &lun);
590 if (*d) {
591 pvscsi_req->lun = lun;
594 return pvscsi_req;
597 static void
598 pvscsi_convert_sglist(PVSCSIRequest *r)
600 int chunk_size;
601 uint64_t data_length = r->req.dataLen;
602 PVSCSISGState sg = r->sg;
603 while (data_length) {
604 while (!sg.resid) {
605 pvscsi_get_next_sg_elem(&sg);
606 trace_pvscsi_convert_sglist(r->req.context, r->sg.dataAddr,
607 r->sg.resid);
609 assert(data_length > 0);
610 chunk_size = MIN((unsigned) data_length, sg.resid);
611 if (chunk_size) {
612 qemu_sglist_add(&r->sgl, sg.dataAddr, chunk_size);
615 sg.dataAddr += chunk_size;
616 data_length -= chunk_size;
617 sg.resid -= chunk_size;
621 static void
622 pvscsi_build_sglist(PVSCSIState *s, PVSCSIRequest *r)
624 PCIDevice *d = PCI_DEVICE(s);
626 pci_dma_sglist_init(&r->sgl, d, 1);
627 if (r->req.flags & PVSCSI_FLAG_CMD_WITH_SG_LIST) {
628 pvscsi_convert_sglist(r);
629 } else {
630 qemu_sglist_add(&r->sgl, r->req.dataAddr, r->req.dataLen);
634 static void
635 pvscsi_process_request_descriptor(PVSCSIState *s,
636 struct PVSCSIRingReqDesc *descr)
638 SCSIDevice *d;
639 PVSCSIRequest *r = pvscsi_queue_pending_descriptor(s, &d, descr);
640 int64_t n;
642 trace_pvscsi_process_req_descr(descr->cdb[0], descr->context);
644 if (!d) {
645 r->cmp.hostStatus = BTSTAT_SELTIMEO;
646 trace_pvscsi_process_req_descr_unknown_device();
647 pvscsi_complete_request(s, r);
648 return;
651 if (descr->flags & PVSCSI_FLAG_CMD_WITH_SG_LIST) {
652 r->sg.elemAddr = descr->dataAddr;
655 r->sreq = scsi_req_new(d, descr->context, r->lun, descr->cdb, r);
656 if (r->sreq->cmd.mode == SCSI_XFER_FROM_DEV &&
657 (descr->flags & PVSCSI_FLAG_CMD_DIR_TODEVICE)) {
658 r->cmp.hostStatus = BTSTAT_BADMSG;
659 trace_pvscsi_process_req_descr_invalid_dir();
660 scsi_req_cancel(r->sreq);
661 return;
663 if (r->sreq->cmd.mode == SCSI_XFER_TO_DEV &&
664 (descr->flags & PVSCSI_FLAG_CMD_DIR_TOHOST)) {
665 r->cmp.hostStatus = BTSTAT_BADMSG;
666 trace_pvscsi_process_req_descr_invalid_dir();
667 scsi_req_cancel(r->sreq);
668 return;
671 pvscsi_build_sglist(s, r);
672 n = scsi_req_enqueue(r->sreq);
674 if (n) {
675 scsi_req_continue(r->sreq);
679 static void
680 pvscsi_process_io(PVSCSIState *s)
682 PVSCSIRingReqDesc descr;
683 hwaddr next_descr_pa;
685 assert(s->rings_info_valid);
686 while ((next_descr_pa = pvscsi_ring_pop_req_descr(&s->rings)) != 0) {
688 /* Only read after production index verification */
689 smp_rmb();
691 trace_pvscsi_process_io(next_descr_pa);
692 cpu_physical_memory_read(next_descr_pa, &descr, sizeof(descr));
693 pvscsi_process_request_descriptor(s, &descr);
696 pvscsi_ring_flush_req(&s->rings);
699 static void
700 pvscsi_dbg_dump_tx_rings_config(PVSCSICmdDescSetupRings *rc)
702 int i;
703 trace_pvscsi_tx_rings_ppn("Rings State", rc->ringsStatePPN);
705 trace_pvscsi_tx_rings_num_pages("Request Ring", rc->reqRingNumPages);
706 for (i = 0; i < rc->reqRingNumPages; i++) {
707 trace_pvscsi_tx_rings_ppn("Request Ring", rc->reqRingPPNs[i]);
710 trace_pvscsi_tx_rings_num_pages("Confirm Ring", rc->cmpRingNumPages);
711 for (i = 0; i < rc->cmpRingNumPages; i++) {
712 trace_pvscsi_tx_rings_ppn("Confirm Ring", rc->reqRingPPNs[i]);
716 static uint64_t
717 pvscsi_on_cmd_config(PVSCSIState *s)
719 trace_pvscsi_on_cmd_noimpl("PVSCSI_CMD_CONFIG");
720 return PVSCSI_COMMAND_PROCESSING_FAILED;
723 static uint64_t
724 pvscsi_on_cmd_unplug(PVSCSIState *s)
726 trace_pvscsi_on_cmd_noimpl("PVSCSI_CMD_DEVICE_UNPLUG");
727 return PVSCSI_COMMAND_PROCESSING_FAILED;
730 static uint64_t
731 pvscsi_on_issue_scsi(PVSCSIState *s)
733 trace_pvscsi_on_cmd_noimpl("PVSCSI_CMD_ISSUE_SCSI");
734 return PVSCSI_COMMAND_PROCESSING_FAILED;
737 static uint64_t
738 pvscsi_on_cmd_setup_rings(PVSCSIState *s)
740 PVSCSICmdDescSetupRings *rc =
741 (PVSCSICmdDescSetupRings *) s->curr_cmd_data;
743 trace_pvscsi_on_cmd_arrived("PVSCSI_CMD_SETUP_RINGS");
745 pvscsi_dbg_dump_tx_rings_config(rc);
746 pvscsi_ring_init_data(&s->rings, rc);
747 s->rings_info_valid = TRUE;
748 return PVSCSI_COMMAND_PROCESSING_SUCCEEDED;
751 static uint64_t
752 pvscsi_on_cmd_abort(PVSCSIState *s)
754 PVSCSICmdDescAbortCmd *cmd = (PVSCSICmdDescAbortCmd *) s->curr_cmd_data;
755 PVSCSIRequest *r, *next;
757 trace_pvscsi_on_cmd_abort(cmd->context, cmd->target);
759 QTAILQ_FOREACH_SAFE(r, &s->pending_queue, next, next) {
760 if (r->req.context == cmd->context) {
761 break;
764 if (r) {
765 assert(!r->completed);
766 r->cmp.hostStatus = BTSTAT_ABORTQUEUE;
767 scsi_req_cancel(r->sreq);
770 return PVSCSI_COMMAND_PROCESSING_SUCCEEDED;
773 static uint64_t
774 pvscsi_on_cmd_unknown(PVSCSIState *s)
776 trace_pvscsi_on_cmd_unknown_data(s->curr_cmd_data[0]);
777 return PVSCSI_COMMAND_PROCESSING_FAILED;
780 static uint64_t
781 pvscsi_on_cmd_reset_device(PVSCSIState *s)
783 uint8_t target_lun = 0;
784 struct PVSCSICmdDescResetDevice *cmd =
785 (struct PVSCSICmdDescResetDevice *) s->curr_cmd_data;
786 SCSIDevice *sdev;
788 sdev = pvscsi_device_find(s, 0, cmd->target, cmd->lun, &target_lun);
790 trace_pvscsi_on_cmd_reset_dev(cmd->target, (int) target_lun, sdev);
792 if (sdev != NULL) {
793 s->resetting++;
794 device_reset(&sdev->qdev);
795 s->resetting--;
796 return PVSCSI_COMMAND_PROCESSING_SUCCEEDED;
799 return PVSCSI_COMMAND_PROCESSING_FAILED;
802 static uint64_t
803 pvscsi_on_cmd_reset_bus(PVSCSIState *s)
805 trace_pvscsi_on_cmd_arrived("PVSCSI_CMD_RESET_BUS");
807 s->resetting++;
808 qbus_reset_all_fn(&s->bus);
809 s->resetting--;
810 return PVSCSI_COMMAND_PROCESSING_SUCCEEDED;
813 static uint64_t
814 pvscsi_on_cmd_setup_msg_ring(PVSCSIState *s)
816 PVSCSICmdDescSetupMsgRing *rc =
817 (PVSCSICmdDescSetupMsgRing *) s->curr_cmd_data;
819 trace_pvscsi_on_cmd_arrived("PVSCSI_CMD_SETUP_MSG_RING");
821 if (!s->use_msg) {
822 return PVSCSI_COMMAND_PROCESSING_FAILED;
825 if (s->rings_info_valid) {
826 pvscsi_ring_init_msg(&s->rings, rc);
827 s->msg_ring_info_valid = TRUE;
829 return sizeof(PVSCSICmdDescSetupMsgRing) / sizeof(uint32_t);
832 static uint64_t
833 pvscsi_on_cmd_adapter_reset(PVSCSIState *s)
835 trace_pvscsi_on_cmd_arrived("PVSCSI_CMD_ADAPTER_RESET");
837 pvscsi_reset_adapter(s);
838 return PVSCSI_COMMAND_PROCESSING_SUCCEEDED;
841 static const struct {
842 int data_size;
843 uint64_t (*handler_fn)(PVSCSIState *s);
844 } pvscsi_commands[] = {
845 [PVSCSI_CMD_FIRST] = {
846 .data_size = 0,
847 .handler_fn = pvscsi_on_cmd_unknown,
850 /* Not implemented, data size defined based on what arrives on windows */
851 [PVSCSI_CMD_CONFIG] = {
852 .data_size = 6 * sizeof(uint32_t),
853 .handler_fn = pvscsi_on_cmd_config,
856 /* Command not implemented, data size is unknown */
857 [PVSCSI_CMD_ISSUE_SCSI] = {
858 .data_size = 0,
859 .handler_fn = pvscsi_on_issue_scsi,
862 /* Command not implemented, data size is unknown */
863 [PVSCSI_CMD_DEVICE_UNPLUG] = {
864 .data_size = 0,
865 .handler_fn = pvscsi_on_cmd_unplug,
868 [PVSCSI_CMD_SETUP_RINGS] = {
869 .data_size = sizeof(PVSCSICmdDescSetupRings),
870 .handler_fn = pvscsi_on_cmd_setup_rings,
873 [PVSCSI_CMD_RESET_DEVICE] = {
874 .data_size = sizeof(struct PVSCSICmdDescResetDevice),
875 .handler_fn = pvscsi_on_cmd_reset_device,
878 [PVSCSI_CMD_RESET_BUS] = {
879 .data_size = 0,
880 .handler_fn = pvscsi_on_cmd_reset_bus,
883 [PVSCSI_CMD_SETUP_MSG_RING] = {
884 .data_size = sizeof(PVSCSICmdDescSetupMsgRing),
885 .handler_fn = pvscsi_on_cmd_setup_msg_ring,
888 [PVSCSI_CMD_ADAPTER_RESET] = {
889 .data_size = 0,
890 .handler_fn = pvscsi_on_cmd_adapter_reset,
893 [PVSCSI_CMD_ABORT_CMD] = {
894 .data_size = sizeof(struct PVSCSICmdDescAbortCmd),
895 .handler_fn = pvscsi_on_cmd_abort,
899 static void
900 pvscsi_do_command_processing(PVSCSIState *s)
902 size_t bytes_arrived = s->curr_cmd_data_cntr * sizeof(uint32_t);
904 assert(s->curr_cmd < PVSCSI_CMD_LAST);
905 if (bytes_arrived >= pvscsi_commands[s->curr_cmd].data_size) {
906 s->reg_command_status = pvscsi_commands[s->curr_cmd].handler_fn(s);
907 s->curr_cmd = PVSCSI_CMD_FIRST;
908 s->curr_cmd_data_cntr = 0;
912 static void
913 pvscsi_on_command_data(PVSCSIState *s, uint32_t value)
915 size_t bytes_arrived = s->curr_cmd_data_cntr * sizeof(uint32_t);
917 assert(bytes_arrived < sizeof(s->curr_cmd_data));
918 s->curr_cmd_data[s->curr_cmd_data_cntr++] = value;
920 pvscsi_do_command_processing(s);
923 static void
924 pvscsi_on_command(PVSCSIState *s, uint64_t cmd_id)
926 if ((cmd_id > PVSCSI_CMD_FIRST) && (cmd_id < PVSCSI_CMD_LAST)) {
927 s->curr_cmd = cmd_id;
928 } else {
929 s->curr_cmd = PVSCSI_CMD_FIRST;
930 trace_pvscsi_on_cmd_unknown(cmd_id);
933 s->curr_cmd_data_cntr = 0;
934 s->reg_command_status = PVSCSI_COMMAND_NOT_ENOUGH_DATA;
936 pvscsi_do_command_processing(s);
939 static void
940 pvscsi_io_write(void *opaque, hwaddr addr,
941 uint64_t val, unsigned size)
943 PVSCSIState *s = opaque;
945 switch (addr) {
946 case PVSCSI_REG_OFFSET_COMMAND:
947 pvscsi_on_command(s, val);
948 break;
950 case PVSCSI_REG_OFFSET_COMMAND_DATA:
951 pvscsi_on_command_data(s, (uint32_t) val);
952 break;
954 case PVSCSI_REG_OFFSET_INTR_STATUS:
955 trace_pvscsi_io_write("PVSCSI_REG_OFFSET_INTR_STATUS", val);
956 s->reg_interrupt_status &= ~val;
957 pvscsi_update_irq_status(s);
958 pvscsi_schedule_completion_processing(s);
959 break;
961 case PVSCSI_REG_OFFSET_INTR_MASK:
962 trace_pvscsi_io_write("PVSCSI_REG_OFFSET_INTR_MASK", val);
963 s->reg_interrupt_enabled = val;
964 pvscsi_update_irq_status(s);
965 break;
967 case PVSCSI_REG_OFFSET_KICK_NON_RW_IO:
968 trace_pvscsi_io_write("PVSCSI_REG_OFFSET_KICK_NON_RW_IO", val);
969 pvscsi_process_io(s);
970 break;
972 case PVSCSI_REG_OFFSET_KICK_RW_IO:
973 trace_pvscsi_io_write("PVSCSI_REG_OFFSET_KICK_RW_IO", val);
974 pvscsi_process_io(s);
975 break;
977 case PVSCSI_REG_OFFSET_DEBUG:
978 trace_pvscsi_io_write("PVSCSI_REG_OFFSET_DEBUG", val);
979 break;
981 default:
982 trace_pvscsi_io_write_unknown(addr, size, val);
983 break;
988 static uint64_t
989 pvscsi_io_read(void *opaque, hwaddr addr, unsigned size)
991 PVSCSIState *s = opaque;
993 switch (addr) {
994 case PVSCSI_REG_OFFSET_INTR_STATUS:
995 trace_pvscsi_io_read("PVSCSI_REG_OFFSET_INTR_STATUS",
996 s->reg_interrupt_status);
997 return s->reg_interrupt_status;
999 case PVSCSI_REG_OFFSET_INTR_MASK:
1000 trace_pvscsi_io_read("PVSCSI_REG_OFFSET_INTR_MASK",
1001 s->reg_interrupt_status);
1002 return s->reg_interrupt_enabled;
1004 case PVSCSI_REG_OFFSET_COMMAND_STATUS:
1005 trace_pvscsi_io_read("PVSCSI_REG_OFFSET_COMMAND_STATUS",
1006 s->reg_interrupt_status);
1007 return s->reg_command_status;
1009 default:
1010 trace_pvscsi_io_read_unknown(addr, size);
1011 return 0;
1016 static bool
1017 pvscsi_init_msi(PVSCSIState *s)
1019 int res;
1020 PCIDevice *d = PCI_DEVICE(s);
1022 res = msi_init(d, PVSCSI_MSI_OFFSET, PVSCSI_MSIX_NUM_VECTORS,
1023 PVSCSI_USE_64BIT, PVSCSI_PER_VECTOR_MASK);
1024 if (res < 0) {
1025 trace_pvscsi_init_msi_fail(res);
1026 s->msi_used = false;
1027 } else {
1028 s->msi_used = true;
1031 return s->msi_used;
1034 static void
1035 pvscsi_cleanup_msi(PVSCSIState *s)
1037 PCIDevice *d = PCI_DEVICE(s);
1039 if (s->msi_used) {
1040 msi_uninit(d);
1044 static const MemoryRegionOps pvscsi_ops = {
1045 .read = pvscsi_io_read,
1046 .write = pvscsi_io_write,
1047 .endianness = DEVICE_LITTLE_ENDIAN,
1048 .impl = {
1049 .min_access_size = 4,
1050 .max_access_size = 4,
1054 static const struct SCSIBusInfo pvscsi_scsi_info = {
1055 .tcq = true,
1056 .max_target = PVSCSI_MAX_DEVS,
1057 .max_channel = 0,
1058 .max_lun = 0,
1060 .get_sg_list = pvscsi_get_sg_list,
1061 .complete = pvscsi_command_complete,
1062 .cancel = pvscsi_request_cancelled,
1065 static int
1066 pvscsi_init(PCIDevice *pci_dev)
1068 PVSCSIState *s = PVSCSI(pci_dev);
1070 trace_pvscsi_state("init");
1072 /* PCI subsystem ID */
1073 pci_dev->config[PCI_SUBSYSTEM_ID] = 0x00;
1074 pci_dev->config[PCI_SUBSYSTEM_ID + 1] = 0x10;
1076 /* PCI latency timer = 255 */
1077 pci_dev->config[PCI_LATENCY_TIMER] = 0xff;
1079 /* Interrupt pin A */
1080 pci_config_set_interrupt_pin(pci_dev->config, 1);
1082 memory_region_init_io(&s->io_space, OBJECT(s), &pvscsi_ops, s,
1083 "pvscsi-io", PVSCSI_MEM_SPACE_SIZE);
1084 pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->io_space);
1086 pvscsi_init_msi(s);
1088 s->completion_worker = qemu_bh_new(pvscsi_process_completion_queue, s);
1089 if (!s->completion_worker) {
1090 pvscsi_cleanup_msi(s);
1091 return -ENOMEM;
1094 scsi_bus_new(&s->bus, sizeof(s->bus), DEVICE(pci_dev),
1095 &pvscsi_scsi_info, NULL);
1096 /* override default SCSI bus hotplug-handler, with pvscsi's one */
1097 qbus_set_hotplug_handler(BUS(&s->bus), DEVICE(s), &error_abort);
1098 pvscsi_reset_state(s);
1100 return 0;
1103 static void
1104 pvscsi_uninit(PCIDevice *pci_dev)
1106 PVSCSIState *s = PVSCSI(pci_dev);
1108 trace_pvscsi_state("uninit");
1109 qemu_bh_delete(s->completion_worker);
1111 pvscsi_cleanup_msi(s);
1114 static void
1115 pvscsi_reset(DeviceState *dev)
1117 PCIDevice *d = PCI_DEVICE(dev);
1118 PVSCSIState *s = PVSCSI(d);
1120 trace_pvscsi_state("reset");
1121 pvscsi_reset_adapter(s);
1124 static void
1125 pvscsi_pre_save(void *opaque)
1127 PVSCSIState *s = (PVSCSIState *) opaque;
1129 trace_pvscsi_state("presave");
1131 assert(QTAILQ_EMPTY(&s->pending_queue));
1132 assert(QTAILQ_EMPTY(&s->completion_queue));
1135 static int
1136 pvscsi_post_load(void *opaque, int version_id)
1138 trace_pvscsi_state("postload");
1139 return 0;
1142 static const VMStateDescription vmstate_pvscsi = {
1143 .name = "pvscsi",
1144 .version_id = 0,
1145 .minimum_version_id = 0,
1146 .pre_save = pvscsi_pre_save,
1147 .post_load = pvscsi_post_load,
1148 .fields = (VMStateField[]) {
1149 VMSTATE_PCI_DEVICE(parent_obj, PVSCSIState),
1150 VMSTATE_UINT8(msi_used, PVSCSIState),
1151 VMSTATE_UINT32(resetting, PVSCSIState),
1152 VMSTATE_UINT64(reg_interrupt_status, PVSCSIState),
1153 VMSTATE_UINT64(reg_interrupt_enabled, PVSCSIState),
1154 VMSTATE_UINT64(reg_command_status, PVSCSIState),
1155 VMSTATE_UINT64(curr_cmd, PVSCSIState),
1156 VMSTATE_UINT32(curr_cmd_data_cntr, PVSCSIState),
1157 VMSTATE_UINT32_ARRAY(curr_cmd_data, PVSCSIState,
1158 ARRAY_SIZE(((PVSCSIState *)NULL)->curr_cmd_data)),
1159 VMSTATE_UINT8(rings_info_valid, PVSCSIState),
1160 VMSTATE_UINT8(msg_ring_info_valid, PVSCSIState),
1161 VMSTATE_UINT8(use_msg, PVSCSIState),
1163 VMSTATE_UINT64(rings.rs_pa, PVSCSIState),
1164 VMSTATE_UINT32(rings.txr_len_mask, PVSCSIState),
1165 VMSTATE_UINT32(rings.rxr_len_mask, PVSCSIState),
1166 VMSTATE_UINT64_ARRAY(rings.req_ring_pages_pa, PVSCSIState,
1167 PVSCSI_SETUP_RINGS_MAX_NUM_PAGES),
1168 VMSTATE_UINT64_ARRAY(rings.cmp_ring_pages_pa, PVSCSIState,
1169 PVSCSI_SETUP_RINGS_MAX_NUM_PAGES),
1170 VMSTATE_UINT64(rings.consumed_ptr, PVSCSIState),
1171 VMSTATE_UINT64(rings.filled_cmp_ptr, PVSCSIState),
1173 VMSTATE_END_OF_LIST()
1177 static void
1178 pvscsi_write_config(PCIDevice *pci, uint32_t addr, uint32_t val, int len)
1180 pci_default_write_config(pci, addr, val, len);
1181 msi_write_config(pci, addr, val, len);
1184 static Property pvscsi_properties[] = {
1185 DEFINE_PROP_UINT8("use_msg", PVSCSIState, use_msg, 1),
1186 DEFINE_PROP_END_OF_LIST(),
1189 static void pvscsi_class_init(ObjectClass *klass, void *data)
1191 DeviceClass *dc = DEVICE_CLASS(klass);
1192 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1193 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
1195 k->init = pvscsi_init;
1196 k->exit = pvscsi_uninit;
1197 k->vendor_id = PCI_VENDOR_ID_VMWARE;
1198 k->device_id = PCI_DEVICE_ID_VMWARE_PVSCSI;
1199 k->class_id = PCI_CLASS_STORAGE_SCSI;
1200 k->subsystem_id = 0x1000;
1201 dc->reset = pvscsi_reset;
1202 dc->vmsd = &vmstate_pvscsi;
1203 dc->props = pvscsi_properties;
1204 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1205 k->config_write = pvscsi_write_config;
1206 hc->unplug = pvscsi_hot_unplug;
1207 hc->plug = pvscsi_hotplug;
1210 static const TypeInfo pvscsi_info = {
1211 .name = TYPE_PVSCSI,
1212 .parent = TYPE_PCI_DEVICE,
1213 .instance_size = sizeof(PVSCSIState),
1214 .class_init = pvscsi_class_init,
1215 .interfaces = (InterfaceInfo[]) {
1216 { TYPE_HOTPLUG_HANDLER },
1221 static void
1222 pvscsi_register_types(void)
1224 type_register_static(&pvscsi_info);
1227 type_init(pvscsi_register_types);