| 
									
										
										
										
											2016-01-26 18:17:16 +00:00
										 |  |  | #include "qemu/osdep.h"
 | 
					
						
							| 
									
										
										
										
											2013-02-04 15:40:22 +01:00
										 |  |  | #include "hw/hw.h"
 | 
					
						
							| 
									
										
											  
											
												include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef.  Since then, we've moved to include qemu/osdep.h
everywhere.  Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h.  That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h.  Include qapi/error.h in .c files that need it and don't
get it now.  Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly.  Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h.  Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third.  Unfortunately, the number depending on
qapi-types.h shrinks only a little.  More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
											
										 
											2016-03-14 09:01:28 +01:00
										 |  |  | #include "qapi/error.h"
 | 
					
						
							| 
									
										
										
										
											2012-12-17 18:20:00 +01:00
										 |  |  | #include "qemu/error-report.h"
 | 
					
						
							| 
									
										
										
										
											2018-02-01 12:18:46 +01:00
										 |  |  | #include "qemu/option.h"
 | 
					
						
							| 
									
										
										
										
											2013-02-05 17:06:20 +01:00
										 |  |  | #include "hw/scsi/scsi.h"
 | 
					
						
							| 
									
										
										
										
											2017-08-22 09:23:55 +02:00
										 |  |  | #include "scsi/constants.h"
 | 
					
						
							| 
									
										
										
										
											2013-02-04 15:40:22 +01:00
										 |  |  | #include "hw/qdev.h"
 | 
					
						
							| 
									
										
										
										
											2014-10-07 13:59:13 +02:00
										 |  |  | #include "sysemu/block-backend.h"
 | 
					
						
							| 
									
										
										
										
											2012-12-17 18:20:04 +01:00
										 |  |  | #include "sysemu/blockdev.h"
 | 
					
						
							| 
									
										
										
										
											2011-04-15 11:51:13 +02:00
										 |  |  | #include "trace.h"
 | 
					
						
							| 
									
										
										
										
											2012-12-17 18:20:04 +01:00
										 |  |  | #include "sysemu/dma.h"
 | 
					
						
							| 
									
										
										
										
											2016-03-20 19:16:19 +02:00
										 |  |  | #include "qemu/cutils.h"
 | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-19 15:30:40 +01:00
										 |  |  | static char *scsibus_get_dev_path(DeviceState *dev); | 
					
						
							| 
									
										
										
										
											2010-12-08 13:35:04 +02:00
										 |  |  | static char *scsibus_get_fw_dev_path(DeviceState *dev); | 
					
						
							| 
									
										
										
										
											2011-10-25 12:53:36 +02:00
										 |  |  | static void scsi_req_dequeue(SCSIRequest *req); | 
					
						
							| 
									
										
										
										
											2013-10-09 15:41:03 +08:00
										 |  |  | static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len); | 
					
						
							|  |  |  | static void scsi_target_free_buf(SCSIRequest *req); | 
					
						
							| 
									
										
										
										
											2010-12-08 13:35:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-28 18:01:36 +02:00
										 |  |  | static Property scsi_props[] = { | 
					
						
							|  |  |  |     DEFINE_PROP_UINT32("channel", SCSIDevice, channel, 0), | 
					
						
							|  |  |  |     DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1), | 
					
						
							|  |  |  |     DEFINE_PROP_UINT32("lun", SCSIDevice, lun, -1), | 
					
						
							|  |  |  |     DEFINE_PROP_END_OF_LIST(), | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-02 09:00:20 +02:00
										 |  |  | static void scsi_bus_class_init(ObjectClass *klass, void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     BusClass *k = BUS_CLASS(klass); | 
					
						
							| 
									
										
										
										
											2014-09-26 09:28:31 +00:00
										 |  |  |     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass); | 
					
						
							| 
									
										
										
										
											2012-05-02 09:00:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     k->get_dev_path = scsibus_get_dev_path; | 
					
						
							|  |  |  |     k->get_fw_dev_path = scsibus_get_fw_dev_path; | 
					
						
							| 
									
										
										
										
											2014-09-26 09:28:31 +00:00
										 |  |  |     hc->unplug = qdev_simple_device_unplug_cb; | 
					
						
							| 
									
										
										
										
											2012-05-02 09:00:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const TypeInfo scsi_bus_info = { | 
					
						
							|  |  |  |     .name = TYPE_SCSI_BUS, | 
					
						
							|  |  |  |     .parent = TYPE_BUS, | 
					
						
							|  |  |  |     .instance_size = sizeof(SCSIBus), | 
					
						
							|  |  |  |     .class_init = scsi_bus_class_init, | 
					
						
							| 
									
										
										
										
											2014-09-26 09:28:31 +00:00
										 |  |  |     .interfaces = (InterfaceInfo[]) { | 
					
						
							|  |  |  |         { TYPE_HOTPLUG_HANDLER }, | 
					
						
							|  |  |  |         { } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | static int next_scsi_bus; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-12 10:12:55 +08:00
										 |  |  | static void scsi_device_realize(SCSIDevice *s, Error **errp) | 
					
						
							| 
									
										
										
										
											2011-12-15 14:50:08 -06:00
										 |  |  | { | 
					
						
							|  |  |  |     SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s); | 
					
						
							| 
									
										
										
										
											2014-08-12 10:12:55 +08:00
										 |  |  |     if (sc->realize) { | 
					
						
							|  |  |  |         sc->realize(s, errp); | 
					
						
							| 
									
										
										
										
											2011-12-15 14:50:08 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-16 10:39:05 +02:00
										 |  |  | int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf, | 
					
						
							|  |  |  |                        void *hba_private) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus); | 
					
						
							|  |  |  |     int rc; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(cmd->len == 0); | 
					
						
							|  |  |  |     rc = scsi_req_parse_cdb(dev, cmd, buf); | 
					
						
							|  |  |  |     if (bus->info->parse_cdb) { | 
					
						
							|  |  |  |         rc = bus->info->parse_cdb(dev, cmd, buf, hba_private); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return rc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-15 14:50:08 -06:00
										 |  |  | static SCSIRequest *scsi_device_alloc_req(SCSIDevice *s, uint32_t tag, uint32_t lun, | 
					
						
							|  |  |  |                                           uint8_t *buf, void *hba_private) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s); | 
					
						
							|  |  |  |     if (sc->alloc_req) { | 
					
						
							|  |  |  |         return sc->alloc_req(s, tag, lun, buf, hba_private); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-29 13:00:11 +01:00
										 |  |  | void scsi_device_unit_attention_reported(SCSIDevice *s) | 
					
						
							| 
									
										
										
										
											2011-12-15 14:50:08 -06:00
										 |  |  | { | 
					
						
							|  |  |  |     SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s); | 
					
						
							|  |  |  |     if (sc->unit_attention_reported) { | 
					
						
							|  |  |  |         sc->unit_attention_reported(s); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  | /* Create a scsi bus, and attach devices to it.  */ | 
					
						
							| 
									
										
										
										
											2013-08-23 20:30:03 +02:00
										 |  |  | void scsi_bus_new(SCSIBus *bus, size_t bus_size, DeviceState *host, | 
					
						
							|  |  |  |                   const SCSIBusInfo *info, const char *bus_name) | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-08-24 00:02:27 +02:00
										 |  |  |     qbus_create_inplace(bus, bus_size, TYPE_SCSI_BUS, host, bus_name); | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  |     bus->busnr = next_scsi_bus++; | 
					
						
							| 
									
										
										
										
											2011-08-13 15:44:45 +02:00
										 |  |  |     bus->info = info; | 
					
						
							| 
									
										
										
										
											2014-09-26 09:28:31 +00:00
										 |  |  |     qbus_set_bus_hotplug_handler(BUS(bus), &error_abort); | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-25 12:53:36 +02:00
										 |  |  | static void scsi_dma_restart_bh(void *opaque) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SCSIDevice *s = opaque; | 
					
						
							|  |  |  |     SCSIRequest *req, *next; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     qemu_bh_delete(s->bh); | 
					
						
							|  |  |  |     s->bh = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-13 14:52:31 +01:00
										 |  |  |     aio_context_acquire(blk_get_aio_context(s->conf.blk)); | 
					
						
							| 
									
										
										
										
											2011-10-25 12:53:36 +02:00
										 |  |  |     QTAILQ_FOREACH_SAFE(req, &s->requests, next, next) { | 
					
						
							|  |  |  |         scsi_req_ref(req); | 
					
						
							|  |  |  |         if (req->retry) { | 
					
						
							|  |  |  |             req->retry = false; | 
					
						
							|  |  |  |             switch (req->cmd.mode) { | 
					
						
							|  |  |  |             case SCSI_XFER_FROM_DEV: | 
					
						
							|  |  |  |             case SCSI_XFER_TO_DEV: | 
					
						
							|  |  |  |                 scsi_req_continue(req); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case SCSI_XFER_NONE: | 
					
						
							|  |  |  |                 scsi_req_dequeue(req); | 
					
						
							|  |  |  |                 scsi_req_enqueue(req); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         scsi_req_unref(req); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-02-13 14:52:31 +01:00
										 |  |  |     aio_context_release(blk_get_aio_context(s->conf.blk)); | 
					
						
							| 
									
										
										
										
											2011-10-25 12:53:36 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void scsi_req_retry(SCSIRequest *req) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* No need to save a reference, because scsi_dma_restart_bh just
 | 
					
						
							|  |  |  |      * looks at the request list.  */ | 
					
						
							|  |  |  |     req->retry = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void scsi_dma_restart_cb(void *opaque, int running, RunState state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SCSIDevice *s = opaque; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!running) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!s->bh) { | 
					
						
							| 
									
										
										
										
											2015-07-22 16:38:17 +02:00
										 |  |  |         AioContext *ctx = blk_get_aio_context(s->conf.blk); | 
					
						
							|  |  |  |         s->bh = aio_bh_new(ctx, scsi_dma_restart_bh, s); | 
					
						
							| 
									
										
										
										
											2011-10-25 12:53:36 +02:00
										 |  |  |         qemu_bh_schedule(s->bh); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-12 10:12:55 +08:00
										 |  |  | static void scsi_qdev_realize(DeviceState *qdev, Error **errp) | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-15 14:50:08 -06:00
										 |  |  |     SCSIDevice *dev = SCSI_DEVICE(qdev); | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  |     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus); | 
					
						
							| 
									
										
										
										
											2011-08-13 18:55:17 +02:00
										 |  |  |     SCSIDevice *d; | 
					
						
							| 
									
										
										
										
											2014-08-12 10:12:55 +08:00
										 |  |  |     Error *local_err = NULL; | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-27 23:24:50 +02:00
										 |  |  |     if (dev->channel > bus->info->max_channel) { | 
					
						
							| 
									
										
										
										
											2014-08-12 10:12:55 +08:00
										 |  |  |         error_setg(errp, "bad scsi channel id: %d", dev->channel); | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2011-07-27 23:24:50 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-08-13 18:55:17 +02:00
										 |  |  |     if (dev->id != -1 && dev->id > bus->info->max_target) { | 
					
						
							| 
									
										
										
										
											2014-08-12 10:12:55 +08:00
										 |  |  |         error_setg(errp, "bad scsi device id: %d", dev->id); | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-02-15 09:22:54 +01:00
										 |  |  |     if (dev->lun != -1 && dev->lun > bus->info->max_lun) { | 
					
						
							| 
									
										
										
										
											2014-08-12 10:12:55 +08:00
										 |  |  |         error_setg(errp, "bad scsi device lun: %d", dev->lun); | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2012-02-15 09:22:54 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-13 18:55:17 +02:00
										 |  |  |     if (dev->id == -1) { | 
					
						
							|  |  |  |         int id = -1; | 
					
						
							|  |  |  |         if (dev->lun == -1) { | 
					
						
							|  |  |  |             dev->lun = 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         do { | 
					
						
							| 
									
										
										
										
											2011-07-27 23:24:50 +02:00
										 |  |  |             d = scsi_device_find(bus, dev->channel, ++id, dev->lun); | 
					
						
							| 
									
										
										
										
											2012-02-15 09:22:54 +01:00
										 |  |  |         } while (d && d->lun == dev->lun && id < bus->info->max_target); | 
					
						
							|  |  |  |         if (d && d->lun == dev->lun) { | 
					
						
							| 
									
										
										
										
											2014-08-12 10:12:55 +08:00
										 |  |  |             error_setg(errp, "no free target"); | 
					
						
							|  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2011-08-13 18:55:17 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         dev->id = id; | 
					
						
							|  |  |  |     } else if (dev->lun == -1) { | 
					
						
							|  |  |  |         int lun = -1; | 
					
						
							|  |  |  |         do { | 
					
						
							| 
									
										
										
										
											2011-07-27 23:24:50 +02:00
										 |  |  |             d = scsi_device_find(bus, dev->channel, dev->id, ++lun); | 
					
						
							| 
									
										
										
										
											2011-08-13 18:55:17 +02:00
										 |  |  |         } while (d && d->lun == lun && lun < bus->info->max_lun); | 
					
						
							| 
									
										
										
										
											2012-02-15 09:22:54 +01:00
										 |  |  |         if (d && d->lun == lun) { | 
					
						
							| 
									
										
										
										
											2014-08-12 10:12:55 +08:00
										 |  |  |             error_setg(errp, "no free lun"); | 
					
						
							|  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2011-08-13 18:55:17 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         dev->lun = lun; | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2011-07-27 23:24:50 +02:00
										 |  |  |         d = scsi_device_find(bus, dev->channel, dev->id, dev->lun); | 
					
						
							| 
									
										
										
										
											2012-02-15 09:22:54 +01:00
										 |  |  |         assert(d); | 
					
						
							|  |  |  |         if (d->lun == dev->lun && dev != d) { | 
					
						
							| 
									
										
										
										
											2014-08-12 10:12:55 +08:00
										 |  |  |             error_setg(errp, "lun already used by '%s'", d->qdev.id); | 
					
						
							|  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2011-08-13 18:55:17 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:49 +01:00
										 |  |  |     QTAILQ_INIT(&dev->requests); | 
					
						
							| 
									
										
										
										
											2014-08-12 10:12:55 +08:00
										 |  |  |     scsi_device_realize(dev, &local_err); | 
					
						
							|  |  |  |     if (local_err) { | 
					
						
							|  |  |  |         error_propagate(errp, local_err); | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2011-10-25 12:53:36 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-08-12 10:12:55 +08:00
										 |  |  |     dev->vmsentry = qemu_add_vm_change_state_handler(scsi_dma_restart_cb, | 
					
						
							|  |  |  |                                                      dev); | 
					
						
							| 
									
										
										
										
											2009-09-25 21:42:36 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-12 10:12:55 +08:00
										 |  |  | static void scsi_qdev_unrealize(DeviceState *qdev, Error **errp) | 
					
						
							| 
									
										
										
										
											2009-09-25 21:42:36 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-15 14:50:08 -06:00
										 |  |  |     SCSIDevice *dev = SCSI_DEVICE(qdev); | 
					
						
							| 
									
										
										
										
											2009-09-25 21:42:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-25 12:53:36 +02:00
										 |  |  |     if (dev->vmsentry) { | 
					
						
							|  |  |  |         qemu_del_vm_change_state_handler(dev->vmsentry); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-09-17 15:09:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     scsi_device_purge_requests(dev, SENSE_CODE(NO_SENSE)); | 
					
						
							|  |  |  |     blockdev_mark_auto_del(dev->conf.blk); | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* handle legacy '-drive if=scsi,...' cmd line args */ | 
					
						
							| 
									
										
										
										
											2014-10-07 13:59:18 +02:00
										 |  |  | SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk, | 
					
						
							| 
									
										
										
										
											2013-04-03 12:41:46 +02:00
										 |  |  |                                       int unit, bool removable, int bootindex, | 
					
						
							| 
									
										
										
										
											2018-01-17 08:52:22 +08:00
										 |  |  |                                       bool share_rw, | 
					
						
							| 
									
										
										
										
											2018-06-25 18:39:00 +02:00
										 |  |  |                                       BlockdevOnError rerror, | 
					
						
							|  |  |  |                                       BlockdevOnError werror, | 
					
						
							| 
									
										
										
										
											2013-07-21 12:16:34 +02:00
										 |  |  |                                       const char *serial, Error **errp) | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     const char *driver; | 
					
						
							| 
									
										
										
										
											2015-02-19 17:05:46 +01:00
										 |  |  |     char *name; | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  |     DeviceState *dev; | 
					
						
							| 
									
										
										
										
											2013-07-21 12:16:34 +02:00
										 |  |  |     Error *err = NULL; | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-07 13:59:18 +02:00
										 |  |  |     driver = blk_is_sg(blk) ? "scsi-generic" : "scsi-disk"; | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  |     dev = qdev_create(&bus->qbus, driver); | 
					
						
							| 
									
										
										
										
											2015-02-19 17:05:46 +01:00
										 |  |  |     name = g_strdup_printf("legacy[%d]", unit); | 
					
						
							|  |  |  |     object_property_add_child(OBJECT(bus), name, OBJECT(dev), NULL); | 
					
						
							|  |  |  |     g_free(name); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  |     qdev_prop_set_uint32(dev, "scsi-id", unit); | 
					
						
							| 
									
										
										
										
											2011-11-18 16:32:00 +01:00
										 |  |  |     if (bootindex >= 0) { | 
					
						
							| 
									
										
										
										
											2014-10-07 16:00:31 +08:00
										 |  |  |         object_property_set_int(OBJECT(dev), bootindex, "bootindex", | 
					
						
							|  |  |  |                                 &error_abort); | 
					
						
							| 
									
										
										
										
											2011-11-18 16:32:00 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-04-12 18:00:18 +02:00
										 |  |  |     if (object_property_find(OBJECT(dev), "removable", NULL)) { | 
					
						
							| 
									
										
										
										
											2011-01-24 15:34:59 +00:00
										 |  |  |         qdev_prop_set_bit(dev, "removable", removable); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-08-23 18:01:58 +02:00
										 |  |  |     if (serial && object_property_find(OBJECT(dev), "serial", NULL)) { | 
					
						
							| 
									
										
										
										
											2013-04-03 12:41:46 +02:00
										 |  |  |         qdev_prop_set_string(dev, "serial", serial); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-09 19:17:26 +01:00
										 |  |  |     qdev_prop_set_drive(dev, "drive", blk, &err); | 
					
						
							|  |  |  |     if (err) { | 
					
						
							| 
									
										
										
										
											2015-03-09 19:17:27 +01:00
										 |  |  |         error_propagate(errp, err); | 
					
						
							| 
									
										
										
										
											2013-09-11 14:54:09 +02:00
										 |  |  |         object_unparent(OBJECT(dev)); | 
					
						
							| 
									
										
										
										
											2010-06-29 16:58:30 +02:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-17 08:52:22 +08:00
										 |  |  |     object_property_set_bool(OBJECT(dev), share_rw, "share-rw", &err); | 
					
						
							|  |  |  |     if (err != NULL) { | 
					
						
							|  |  |  |         error_propagate(errp, err); | 
					
						
							|  |  |  |         object_unparent(OBJECT(dev)); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-06-25 18:39:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     qdev_prop_set_enum(dev, "rerror", rerror); | 
					
						
							|  |  |  |     qdev_prop_set_enum(dev, "werror", werror); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-21 12:16:34 +02:00
										 |  |  |     object_property_set_bool(OBJECT(dev), true, "realized", &err); | 
					
						
							|  |  |  |     if (err != NULL) { | 
					
						
							|  |  |  |         error_propagate(errp, err); | 
					
						
							| 
									
										
										
										
											2013-09-11 14:54:09 +02:00
										 |  |  |         object_unparent(OBJECT(dev)); | 
					
						
							| 
									
										
										
										
											2009-10-07 01:15:57 +02:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2013-07-21 12:16:34 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-12-15 14:50:08 -06:00
										 |  |  |     return SCSI_DEVICE(dev); | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-20 11:42:37 +01:00
										 |  |  | void scsi_bus_legacy_handle_cmdline(SCSIBus *bus) | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-27 21:08:18 +02:00
										 |  |  |     Location loc; | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  |     DriveInfo *dinfo; | 
					
						
							| 
									
										
										
										
											2013-07-21 12:16:34 +02:00
										 |  |  |     int unit; | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-27 21:08:18 +02:00
										 |  |  |     loc_push_none(&loc); | 
					
						
							| 
									
										
										
										
											2012-02-15 09:22:54 +01:00
										 |  |  |     for (unit = 0; unit <= bus->info->max_target; unit++) { | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  |         dinfo = drive_get(IF_SCSI, bus->busnr, unit); | 
					
						
							|  |  |  |         if (dinfo == NULL) { | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-05-27 21:08:18 +02:00
										 |  |  |         qemu_opts_loc_restore(dinfo->opts); | 
					
						
							| 
									
										
										
										
											2014-10-07 13:59:18 +02:00
										 |  |  |         scsi_bus_legacy_add_drive(bus, blk_by_legacy_dinfo(dinfo), | 
					
						
							| 
									
										
										
										
											2018-06-25 18:39:00 +02:00
										 |  |  |                                   unit, false, -1, false, | 
					
						
							|  |  |  |                                   BLOCKDEV_ON_ERROR_AUTO, | 
					
						
							|  |  |  |                                   BLOCKDEV_ON_ERROR_AUTO, | 
					
						
							|  |  |  |                                   NULL, &error_fatal); | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-05-27 21:08:18 +02:00
										 |  |  |     loc_pop(&loc); | 
					
						
							| 
									
										
										
										
											2009-08-31 14:24:04 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-08 11:49:43 +01:00
										 |  |  | static int32_t scsi_invalid_field(SCSIRequest *req, uint8_t *buf) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD)); | 
					
						
							|  |  |  |     scsi_req_complete(req, CHECK_CONDITION); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const struct SCSIReqOps reqops_invalid_field = { | 
					
						
							|  |  |  |     .size         = sizeof(SCSIRequest), | 
					
						
							|  |  |  |     .send_command = scsi_invalid_field | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:13 +02:00
										 |  |  | /* SCSIReqOps implementation for invalid commands.  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int32_t scsi_invalid_command(SCSIRequest *req, uint8_t *buf) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE)); | 
					
						
							|  |  |  |     scsi_req_complete(req, CHECK_CONDITION); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-12 12:57:59 +02:00
										 |  |  | static const struct SCSIReqOps reqops_invalid_opcode = { | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:13 +02:00
										 |  |  |     .size         = sizeof(SCSIRequest), | 
					
						
							|  |  |  |     .send_command = scsi_invalid_command | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:17 +02:00
										 |  |  | /* SCSIReqOps implementation for unit attention conditions.  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int32_t scsi_unit_attention(SCSIRequest *req, uint8_t *buf) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-01-17 13:07:47 +01:00
										 |  |  |     if (req->dev->unit_attention.key == UNIT_ATTENTION) { | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:17 +02:00
										 |  |  |         scsi_req_build_sense(req, req->dev->unit_attention); | 
					
						
							|  |  |  |     } else if (req->bus->unit_attention.key == UNIT_ATTENTION) { | 
					
						
							|  |  |  |         scsi_req_build_sense(req, req->bus->unit_attention); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     scsi_req_complete(req, CHECK_CONDITION); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-12 12:57:59 +02:00
										 |  |  | static const struct SCSIReqOps reqops_unit_attention = { | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:17 +02:00
										 |  |  |     .size         = sizeof(SCSIRequest), | 
					
						
							|  |  |  |     .send_command = scsi_unit_attention | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:14 +02:00
										 |  |  | /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
 | 
					
						
							|  |  |  |    an invalid LUN.  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct SCSITargetReq SCSITargetReq; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct SCSITargetReq { | 
					
						
							|  |  |  |     SCSIRequest req; | 
					
						
							|  |  |  |     int len; | 
					
						
							| 
									
										
										
										
											2013-10-09 15:41:03 +08:00
										 |  |  |     uint8_t *buf; | 
					
						
							|  |  |  |     int buf_len; | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:14 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void store_lun(uint8_t *outbuf, int lun) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (lun < 256) { | 
					
						
							|  |  |  |         outbuf[1] = lun; | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     outbuf[1] = (lun & 255); | 
					
						
							|  |  |  |     outbuf[0] = (lun >> 8) | 0x40; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool scsi_target_emulate_report_luns(SCSITargetReq *r) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-23 15:34:39 -06:00
										 |  |  |     BusChild *kid; | 
					
						
							| 
									
										
										
										
											2011-09-14 20:39:36 +02:00
										 |  |  |     int i, len, n; | 
					
						
							| 
									
										
										
										
											2011-07-27 23:24:50 +02:00
										 |  |  |     int channel, id; | 
					
						
							| 
									
										
										
										
											2011-09-14 20:39:36 +02:00
										 |  |  |     bool found_lun0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:14 +02:00
										 |  |  |     if (r->req.cmd.xfer < 16) { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (r->req.cmd.buf[2] > 2) { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-07-27 23:24:50 +02:00
										 |  |  |     channel = r->req.dev->channel; | 
					
						
							| 
									
										
										
										
											2011-09-14 20:39:36 +02:00
										 |  |  |     id = r->req.dev->id; | 
					
						
							|  |  |  |     found_lun0 = false; | 
					
						
							|  |  |  |     n = 0; | 
					
						
							| 
									
										
										
										
											2011-12-23 15:34:39 -06:00
										 |  |  |     QTAILQ_FOREACH(kid, &r->req.bus->qbus.children, sibling) { | 
					
						
							|  |  |  |         DeviceState *qdev = kid->child; | 
					
						
							| 
									
										
										
										
											2011-12-15 14:50:08 -06:00
										 |  |  |         SCSIDevice *dev = SCSI_DEVICE(qdev); | 
					
						
							| 
									
										
										
										
											2011-09-14 20:39:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-27 23:24:50 +02:00
										 |  |  |         if (dev->channel == channel && dev->id == id) { | 
					
						
							| 
									
										
										
										
											2011-09-14 20:39:36 +02:00
										 |  |  |             if (dev->lun == 0) { | 
					
						
							|  |  |  |                 found_lun0 = true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             n += 8; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!found_lun0) { | 
					
						
							|  |  |  |         n += 8; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-09 15:41:03 +08:00
										 |  |  |     scsi_target_alloc_buf(&r->req, n + 8); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     len = MIN(n + 8, r->req.cmd.xfer & ~7); | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:14 +02:00
										 |  |  |     memset(r->buf, 0, len); | 
					
						
							| 
									
										
										
										
											2013-10-09 15:41:03 +08:00
										 |  |  |     stl_be_p(&r->buf[0], n); | 
					
						
							| 
									
										
										
										
											2011-09-14 20:39:36 +02:00
										 |  |  |     i = found_lun0 ? 8 : 16; | 
					
						
							| 
									
										
										
										
											2011-12-23 15:34:39 -06:00
										 |  |  |     QTAILQ_FOREACH(kid, &r->req.bus->qbus.children, sibling) { | 
					
						
							|  |  |  |         DeviceState *qdev = kid->child; | 
					
						
							| 
									
										
										
										
											2011-12-15 14:50:08 -06:00
										 |  |  |         SCSIDevice *dev = SCSI_DEVICE(qdev); | 
					
						
							| 
									
										
										
										
											2011-09-14 20:39:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-27 23:24:50 +02:00
										 |  |  |         if (dev->channel == channel && dev->id == id) { | 
					
						
							| 
									
										
										
										
											2011-09-14 20:39:36 +02:00
										 |  |  |             store_lun(&r->buf[i], dev->lun); | 
					
						
							|  |  |  |             i += 8; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:14 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-09-14 20:39:36 +02:00
										 |  |  |     assert(i == n + 8); | 
					
						
							|  |  |  |     r->len = len; | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:14 +02:00
										 |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool scsi_target_emulate_inquiry(SCSITargetReq *r) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     assert(r->req.dev->lun != r->req.lun); | 
					
						
							| 
									
										
										
										
											2013-10-09 15:41:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     scsi_target_alloc_buf(&r->req, SCSI_INQUIRY_LEN); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:14 +02:00
										 |  |  |     if (r->req.cmd.buf[1] & 0x2) { | 
					
						
							|  |  |  |         /* Command support data - optional, not implemented */ | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (r->req.cmd.buf[1] & 0x1) { | 
					
						
							|  |  |  |         /* Vital product data */ | 
					
						
							|  |  |  |         uint8_t page_code = r->req.cmd.buf[2]; | 
					
						
							|  |  |  |         r->buf[r->len++] = page_code ; /* this page */ | 
					
						
							|  |  |  |         r->buf[r->len++] = 0x00; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         switch (page_code) { | 
					
						
							|  |  |  |         case 0x00: /* Supported page codes, mandatory */ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             int pages; | 
					
						
							|  |  |  |             pages = r->len++; | 
					
						
							|  |  |  |             r->buf[r->len++] = 0x00; /* list of supported pages (this page) */ | 
					
						
							|  |  |  |             r->buf[pages] = r->len - pages - 1; /* number of pages */ | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         /* done with EVPD */ | 
					
						
							| 
									
										
										
										
											2013-10-09 15:41:03 +08:00
										 |  |  |         assert(r->len < r->buf_len); | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:14 +02:00
										 |  |  |         r->len = MIN(r->req.cmd.xfer, r->len); | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Standard INQUIRY data */ | 
					
						
							|  |  |  |     if (r->req.cmd.buf[2] != 0) { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* PAGE CODE == 0 */ | 
					
						
							| 
									
										
										
										
											2013-10-09 15:41:03 +08:00
										 |  |  |     r->len = MIN(r->req.cmd.xfer, SCSI_INQUIRY_LEN); | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:14 +02:00
										 |  |  |     memset(r->buf, 0, r->len); | 
					
						
							|  |  |  |     if (r->req.lun != 0) { | 
					
						
							|  |  |  |         r->buf[0] = TYPE_NO_LUN; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         r->buf[0] = TYPE_NOT_PRESENT | TYPE_INACTIVE; | 
					
						
							|  |  |  |         r->buf[2] = 5; /* Version */ | 
					
						
							|  |  |  |         r->buf[3] = 2 | 0x10; /* HiSup, response data format */ | 
					
						
							|  |  |  |         r->buf[4] = r->len - 5; /* Additional Length = (Len - 1) - 4 */ | 
					
						
							| 
									
										
										
										
											2011-08-13 15:44:45 +02:00
										 |  |  |         r->buf[7] = 0x10 | (r->req.bus->info->tcq ? 0x02 : 0); /* Sync, TCQ.  */ | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:14 +02:00
										 |  |  |         memcpy(&r->buf[8], "QEMU    ", 8); | 
					
						
							|  |  |  |         memcpy(&r->buf[16], "QEMU TARGET     ", 16); | 
					
						
							| 
									
										
										
										
											2015-10-30 17:36:08 -02:00
										 |  |  |         pstrcpy((char *) &r->buf[32], 4, qemu_hw_version()); | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:14 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-29 03:11:46 +03:00
										 |  |  | static size_t scsi_sense_len(SCSIRequest *req) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (req->dev->type == TYPE_SCANNER) | 
					
						
							|  |  |  |         return SCSI_SENSE_LEN_SCANNER; | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         return SCSI_SENSE_LEN; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:14 +02:00
										 |  |  | static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req); | 
					
						
							| 
									
										
										
										
											2017-08-18 11:37:02 +02:00
										 |  |  |     int fixed_sense = (req->cmd.buf[1] & 1) == 0; | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-18 11:37:02 +02:00
										 |  |  |     if (req->lun != 0 && | 
					
						
							|  |  |  |         buf[0] != INQUIRY && buf[0] != REQUEST_SENSE) { | 
					
						
							| 
									
										
										
										
											2017-08-04 10:36:34 +02:00
										 |  |  |         scsi_req_build_sense(req, SENSE_CODE(LUN_NOT_SUPPORTED)); | 
					
						
							|  |  |  |         scsi_req_complete(req, CHECK_CONDITION); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:14 +02:00
										 |  |  |     switch (buf[0]) { | 
					
						
							|  |  |  |     case REPORT_LUNS: | 
					
						
							|  |  |  |         if (!scsi_target_emulate_report_luns(r)) { | 
					
						
							|  |  |  |             goto illegal_request; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case INQUIRY: | 
					
						
							|  |  |  |         if (!scsi_target_emulate_inquiry(r)) { | 
					
						
							|  |  |  |             goto illegal_request; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:15 +02:00
										 |  |  |     case REQUEST_SENSE: | 
					
						
							| 
									
										
										
										
											2016-06-29 03:11:46 +03:00
										 |  |  |         scsi_target_alloc_buf(&r->req, scsi_sense_len(req)); | 
					
						
							| 
									
										
										
										
											2017-08-18 11:37:02 +02:00
										 |  |  |         if (req->lun != 0) { | 
					
						
							|  |  |  |             const struct SCSISense sense = SENSE_CODE(LUN_NOT_SUPPORTED); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-27 13:27:41 +01:00
										 |  |  |             r->len = scsi_build_sense_buf(r->buf, req->cmd.xfer, | 
					
						
							|  |  |  |                                           sense, fixed_sense); | 
					
						
							| 
									
										
										
										
											2017-08-18 11:37:02 +02:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             r->len = scsi_device_get_sense(r->req.dev, r->buf, | 
					
						
							|  |  |  |                                            MIN(req->cmd.xfer, r->buf_len), | 
					
						
							|  |  |  |                                            fixed_sense); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-09-13 16:19:53 +02:00
										 |  |  |         if (r->req.dev->sense_is_ua) { | 
					
						
							| 
									
										
										
										
											2011-12-15 14:50:08 -06:00
										 |  |  |             scsi_device_unit_attention_reported(req->dev); | 
					
						
							| 
									
										
										
										
											2011-09-13 16:19:53 +02:00
										 |  |  |             r->req.dev->sense_len = 0; | 
					
						
							|  |  |  |             r->req.dev->sense_is_ua = false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:15 +02:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2014-01-16 13:06:13 +01:00
										 |  |  |     case TEST_UNIT_READY: | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:14 +02:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											2017-08-04 10:36:34 +02:00
										 |  |  |         scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE)); | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:14 +02:00
										 |  |  |         scsi_req_complete(req, CHECK_CONDITION); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     illegal_request: | 
					
						
							|  |  |  |         scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD)); | 
					
						
							|  |  |  |         scsi_req_complete(req, CHECK_CONDITION); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!r->len) { | 
					
						
							|  |  |  |         scsi_req_complete(req, GOOD); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return r->len; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void scsi_target_read_data(SCSIRequest *req) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req); | 
					
						
							|  |  |  |     uint32_t n; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     n = r->len; | 
					
						
							|  |  |  |     if (n > 0) { | 
					
						
							|  |  |  |         r->len = 0; | 
					
						
							|  |  |  |         scsi_req_data(&r->req, n); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         scsi_req_complete(&r->req, GOOD); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static uint8_t *scsi_target_get_buf(SCSIRequest *req) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return r->buf; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-09 15:41:03 +08:00
										 |  |  | static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     r->buf = g_malloc(len); | 
					
						
							|  |  |  |     r->buf_len = len; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return r->buf; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void scsi_target_free_buf(SCSIRequest *req) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     g_free(r->buf); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-12 12:57:59 +02:00
										 |  |  | static const struct SCSIReqOps reqops_target_command = { | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:14 +02:00
										 |  |  |     .size         = sizeof(SCSITargetReq), | 
					
						
							|  |  |  |     .send_command = scsi_target_send_command, | 
					
						
							|  |  |  |     .read_data    = scsi_target_read_data, | 
					
						
							|  |  |  |     .get_buf      = scsi_target_get_buf, | 
					
						
							| 
									
										
										
										
											2013-10-09 15:41:03 +08:00
										 |  |  |     .free_req     = scsi_target_free_buf, | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:14 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-12 12:57:59 +02:00
										 |  |  | SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d, | 
					
						
							|  |  |  |                             uint32_t tag, uint32_t lun, void *hba_private) | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:50 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     SCSIRequest *req; | 
					
						
							| 
									
										
										
										
											2013-06-03 14:09:39 +02:00
										 |  |  |     SCSIBus *bus = scsi_bus_from_device(d); | 
					
						
							|  |  |  |     BusState *qbus = BUS(bus); | 
					
						
							| 
									
										
										
										
											2014-09-16 15:20:17 +08:00
										 |  |  |     const int memset_off = offsetof(SCSIRequest, sense) | 
					
						
							|  |  |  |                            + sizeof(req->sense); | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-01 12:59:01 +02:00
										 |  |  |     req = g_malloc(reqops->size); | 
					
						
							| 
									
										
										
										
											2014-09-16 15:20:17 +08:00
										 |  |  |     memset((uint8_t *)req + memset_off, 0, reqops->size - memset_off); | 
					
						
							| 
									
										
										
										
											2011-04-18 12:35:39 +02:00
										 |  |  |     req->refcount = 1; | 
					
						
							| 
									
										
										
										
											2013-06-03 14:09:39 +02:00
										 |  |  |     req->bus = bus; | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:50 +01:00
										 |  |  |     req->dev = d; | 
					
						
							|  |  |  |     req->tag = tag; | 
					
						
							|  |  |  |     req->lun = lun; | 
					
						
							| 
									
										
										
										
											2011-07-11 15:02:24 +02:00
										 |  |  |     req->hba_private = hba_private; | 
					
						
							| 
									
										
										
										
											2009-11-26 15:34:00 +01:00
										 |  |  |     req->status = -1; | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:08 +02:00
										 |  |  |     req->ops = reqops; | 
					
						
							| 
									
										
										
										
											2013-06-03 14:09:39 +02:00
										 |  |  |     object_ref(OBJECT(d)); | 
					
						
							|  |  |  |     object_ref(OBJECT(qbus->parent)); | 
					
						
							| 
									
										
										
										
											2014-09-28 09:48:00 +08:00
										 |  |  |     notifier_list_init(&req->cancel_notifiers); | 
					
						
							| 
									
										
										
										
											2011-04-15 11:51:13 +02:00
										 |  |  |     trace_scsi_req_alloc(req->dev->id, req->lun, req->tag); | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:50 +01:00
										 |  |  |     return req; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-11 15:02:24 +02:00
										 |  |  | SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun, | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:10 +02:00
										 |  |  |                           uint8_t *buf, void *hba_private) | 
					
						
							| 
									
										
										
										
											2011-04-18 19:09:55 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:17 +02:00
										 |  |  |     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus); | 
					
						
							| 
									
										
										
										
											2014-07-16 10:23:43 +02:00
										 |  |  |     const SCSIReqOps *ops; | 
					
						
							| 
									
										
										
										
											2014-07-16 10:39:05 +02:00
										 |  |  |     SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(d); | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:10 +02:00
										 |  |  |     SCSIRequest *req; | 
					
						
							| 
									
										
										
										
											2014-07-16 10:23:43 +02:00
										 |  |  |     SCSICommand cmd = { .len = 0 }; | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ((d->unit_attention.key == UNIT_ATTENTION || | 
					
						
							|  |  |  |          bus->unit_attention.key == UNIT_ATTENTION) && | 
					
						
							|  |  |  |         (buf[0] != INQUIRY && | 
					
						
							|  |  |  |          buf[0] != REPORT_LUNS && | 
					
						
							|  |  |  |          buf[0] != GET_CONFIGURATION && | 
					
						
							|  |  |  |          buf[0] != GET_EVENT_STATUS_NOTIFICATION && | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          /*
 | 
					
						
							|  |  |  |           * If we already have a pending unit attention condition, | 
					
						
							|  |  |  |           * report this one before triggering another one. | 
					
						
							|  |  |  |           */ | 
					
						
							|  |  |  |          !(buf[0] == REQUEST_SENSE && d->sense_is_ua))) { | 
					
						
							|  |  |  |         ops = &reqops_unit_attention; | 
					
						
							|  |  |  |     } else if (lun != d->lun || | 
					
						
							|  |  |  |                buf[0] == REPORT_LUNS || | 
					
						
							|  |  |  |                (buf[0] == REQUEST_SENSE && d->sense_len)) { | 
					
						
							|  |  |  |         ops = &reqops_target_command; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         ops = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-16 10:39:05 +02:00
										 |  |  |     if (ops != NULL || !sc->parse_cdb) { | 
					
						
							|  |  |  |         ret = scsi_req_parse_cdb(d, &cmd, buf); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         ret = sc->parse_cdb(d, &cmd, buf, hba_private); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-16 10:23:43 +02:00
										 |  |  |     if (ret != 0) { | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:13 +02:00
										 |  |  |         trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]); | 
					
						
							|  |  |  |         req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private); | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2014-07-16 10:23:43 +02:00
										 |  |  |         assert(cmd.len != 0); | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:13 +02:00
										 |  |  |         trace_scsi_req_parsed(d->id, lun, tag, buf[0], | 
					
						
							|  |  |  |                               cmd.mode, cmd.xfer); | 
					
						
							| 
									
										
										
										
											2011-08-12 17:49:36 +01:00
										 |  |  |         if (cmd.lba != -1) { | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:13 +02:00
										 |  |  |             trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0], | 
					
						
							|  |  |  |                                       cmd.lba); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-08 11:49:43 +01:00
										 |  |  |         if (cmd.xfer > INT32_MAX) { | 
					
						
							|  |  |  |             req = scsi_req_alloc(&reqops_invalid_field, d, tag, lun, hba_private); | 
					
						
							| 
									
										
										
										
											2014-07-16 10:23:43 +02:00
										 |  |  |         } else if (ops) { | 
					
						
							|  |  |  |             req = scsi_req_alloc(ops, d, tag, lun, hba_private); | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:14 +02:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2011-12-15 14:50:08 -06:00
										 |  |  |             req = scsi_device_alloc_req(d, tag, lun, buf, hba_private); | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:14 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:13 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     req->cmd = cmd; | 
					
						
							| 
									
										
										
										
											2011-07-06 11:55:37 +02:00
										 |  |  |     req->resid = req->cmd.xfer; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:19 +02:00
										 |  |  |     switch (buf[0]) { | 
					
						
							|  |  |  |     case INQUIRY: | 
					
						
							|  |  |  |         trace_scsi_inquiry(d->id, lun, tag, cmd.buf[1], cmd.buf[2]); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case TEST_UNIT_READY: | 
					
						
							|  |  |  |         trace_scsi_test_unit_ready(d->id, lun, tag); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case REPORT_LUNS: | 
					
						
							|  |  |  |         trace_scsi_report_luns(d->id, lun, tag); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case REQUEST_SENSE: | 
					
						
							|  |  |  |         trace_scsi_request_sense(d->id, lun, tag); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:10 +02:00
										 |  |  |     return req; | 
					
						
							| 
									
										
										
										
											2011-04-18 19:09:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-21 13:21:02 +02:00
										 |  |  | uint8_t *scsi_req_get_buf(SCSIRequest *req) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:09 +02:00
										 |  |  |     return req->ops->get_buf(req); | 
					
						
							| 
									
										
										
										
											2011-04-21 13:21:02 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:17 +02:00
										 |  |  | static void scsi_clear_unit_attention(SCSIRequest *req) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SCSISense *ua; | 
					
						
							|  |  |  |     if (req->dev->unit_attention.key != UNIT_ATTENTION && | 
					
						
							|  |  |  |         req->bus->unit_attention.key != UNIT_ATTENTION) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * If an INQUIRY command enters the enabled command state, | 
					
						
							|  |  |  |      * the device server shall [not] clear any unit attention condition; | 
					
						
							|  |  |  |      * See also MMC-6, paragraphs 6.5 and 6.6.2. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     if (req->cmd.buf[0] == INQUIRY || | 
					
						
							|  |  |  |         req->cmd.buf[0] == GET_CONFIGURATION || | 
					
						
							|  |  |  |         req->cmd.buf[0] == GET_EVENT_STATUS_NOTIFICATION) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (req->dev->unit_attention.key == UNIT_ATTENTION) { | 
					
						
							|  |  |  |         ua = &req->dev->unit_attention; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         ua = &req->bus->unit_attention; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * If a REPORT LUNS command enters the enabled command state, [...] | 
					
						
							|  |  |  |      * the device server shall clear any pending unit attention condition | 
					
						
							|  |  |  |      * with an additional sense code of REPORTED LUNS DATA HAS CHANGED. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     if (req->cmd.buf[0] == REPORT_LUNS && | 
					
						
							|  |  |  |         !(ua->asc == SENSE_CODE(REPORTED_LUNS_CHANGED).asc && | 
					
						
							|  |  |  |           ua->ascq == SENSE_CODE(REPORTED_LUNS_CHANGED).ascq)) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     *ua = SENSE_CODE(NO_SENSE); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-18 13:36:02 +02:00
										 |  |  | int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:17 +02:00
										 |  |  |     int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:07 +02:00
										 |  |  |     assert(len >= 14); | 
					
						
							|  |  |  |     if (!req->sense_len) { | 
					
						
							| 
									
										
										
										
											2011-04-18 13:36:02 +02:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-22 09:31:36 +02:00
										 |  |  |     ret = scsi_convert_sense(req->sense, req->sense_len, buf, len, true); | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * FIXME: clearing unit attention conditions upon autosense should be done | 
					
						
							|  |  |  |      * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b | 
					
						
							|  |  |  |      * (SAM-5, 5.14). | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and | 
					
						
							|  |  |  |      * 10b for HBAs that do not support it (do not call scsi_req_get_sense). | 
					
						
							| 
									
										
										
										
											2011-09-13 16:19:53 +02:00
										 |  |  |      * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b. | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:17 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-09-13 16:19:53 +02:00
										 |  |  |     if (req->dev->sense_is_ua) { | 
					
						
							| 
									
										
										
										
											2011-12-15 14:50:08 -06:00
										 |  |  |         scsi_device_unit_attention_reported(req->dev); | 
					
						
							| 
									
										
										
										
											2011-09-13 16:19:53 +02:00
										 |  |  |         req->dev->sense_len = 0; | 
					
						
							|  |  |  |         req->dev->sense_is_ua = false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:17 +02:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:07 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-22 09:31:36 +02:00
										 |  |  |     return scsi_convert_sense(dev->sense, dev->sense_len, buf, len, fixed); | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:07 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void scsi_req_build_sense(SCSIRequest *req, SCSISense sense) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     trace_scsi_req_build_sense(req->dev->id, req->lun, req->tag, | 
					
						
							|  |  |  |                                sense.key, sense.asc, sense.ascq); | 
					
						
							| 
									
										
										
										
											2017-08-22 09:42:59 +02:00
										 |  |  |     req->sense_len = scsi_build_sense(req->sense, sense); | 
					
						
							| 
									
										
										
										
											2011-04-18 13:36:02 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-02 16:27:02 +01:00
										 |  |  | static void scsi_req_enqueue_internal(SCSIRequest *req) | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:50 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-04-18 12:35:39 +02:00
										 |  |  |     assert(!req->enqueued); | 
					
						
							|  |  |  |     scsi_req_ref(req); | 
					
						
							| 
									
										
										
										
											2011-07-06 11:26:47 +02:00
										 |  |  |     if (req->bus->info->get_sg_list) { | 
					
						
							|  |  |  |         req->sg = req->bus->info->get_sg_list(req); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         req->sg = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-04-18 12:35:39 +02:00
										 |  |  |     req->enqueued = true; | 
					
						
							|  |  |  |     QTAILQ_INSERT_TAIL(&req->dev->requests, req, next); | 
					
						
							| 
									
										
										
										
											2011-12-02 16:27:02 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-04-18 19:07:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-02 16:27:02 +01:00
										 |  |  | int32_t scsi_req_enqueue(SCSIRequest *req) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int32_t rc; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(!req->retry); | 
					
						
							|  |  |  |     scsi_req_enqueue_internal(req); | 
					
						
							| 
									
										
										
										
											2011-04-18 19:07:23 +02:00
										 |  |  |     scsi_req_ref(req); | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:10 +02:00
										 |  |  |     rc = req->ops->send_command(req, req->cmd.buf); | 
					
						
							| 
									
										
										
										
											2011-04-18 19:07:23 +02:00
										 |  |  |     scsi_req_unref(req); | 
					
						
							|  |  |  |     return rc; | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:50 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-18 12:53:14 +02:00
										 |  |  | static void scsi_req_dequeue(SCSIRequest *req) | 
					
						
							| 
									
										
										
										
											2010-07-13 14:13:45 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-04-15 11:51:13 +02:00
										 |  |  |     trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag); | 
					
						
							| 
									
										
										
										
											2011-10-25 12:53:36 +02:00
										 |  |  |     req->retry = false; | 
					
						
							| 
									
										
										
										
											2010-07-13 14:13:45 +02:00
										 |  |  |     if (req->enqueued) { | 
					
						
							|  |  |  |         QTAILQ_REMOVE(&req->dev->requests, req, next); | 
					
						
							|  |  |  |         req->enqueued = false; | 
					
						
							| 
									
										
										
										
											2011-04-18 16:01:56 +02:00
										 |  |  |         scsi_req_unref(req); | 
					
						
							| 
									
										
										
										
											2010-07-13 14:13:45 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-14 14:31:49 +01:00
										 |  |  | static int scsi_get_performance_length(int num_desc, int type, int data_type) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* MMC-6, paragraph 6.7.  */ | 
					
						
							|  |  |  |     switch (type) { | 
					
						
							|  |  |  |     case 0: | 
					
						
							|  |  |  |         if ((data_type & 3) == 0) { | 
					
						
							|  |  |  |             /* Each descriptor is as in Table 295 - Nominal performance.  */ | 
					
						
							|  |  |  |             return 16 * num_desc + 8; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             /* Each descriptor is as in Table 296 - Exceptions.  */ | 
					
						
							|  |  |  |             return 6 * num_desc + 8; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     case 1: | 
					
						
							|  |  |  |     case 4: | 
					
						
							|  |  |  |     case 5: | 
					
						
							|  |  |  |         return 8 * num_desc + 8; | 
					
						
							|  |  |  |     case 2: | 
					
						
							|  |  |  |         return 2048 * num_desc + 8; | 
					
						
							|  |  |  |     case 3: | 
					
						
							|  |  |  |         return 16 * num_desc + 8; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         return 8; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-03 12:02:19 +08:00
										 |  |  | static int ata_passthrough_xfer_unit(SCSIDevice *dev, uint8_t *buf) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int byte_block = (buf[2] >> 2) & 0x1; | 
					
						
							|  |  |  |     int type = (buf[2] >> 4) & 0x1; | 
					
						
							|  |  |  |     int xfer_unit; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (byte_block) { | 
					
						
							|  |  |  |         if (type) { | 
					
						
							|  |  |  |             xfer_unit = dev->blocksize; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             xfer_unit = 512; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         xfer_unit = 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return xfer_unit; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-29 13:00:04 +01:00
										 |  |  | static int ata_passthrough_12_xfer(SCSIDevice *dev, uint8_t *buf) | 
					
						
							| 
									
										
										
										
											2012-08-03 12:02:19 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     int length = buf[2] & 0x3; | 
					
						
							|  |  |  |     int xfer; | 
					
						
							|  |  |  |     int unit = ata_passthrough_xfer_unit(dev, buf); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (length) { | 
					
						
							|  |  |  |     case 0: | 
					
						
							|  |  |  |     case 3: /* USB-specific.  */ | 
					
						
							| 
									
										
										
										
											2012-08-13 13:05:44 +02:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											2012-08-03 12:02:19 +08:00
										 |  |  |         xfer = 0; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case 1: | 
					
						
							|  |  |  |         xfer = buf[3]; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case 2: | 
					
						
							|  |  |  |         xfer = buf[4]; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return xfer * unit; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-29 13:00:04 +01:00
										 |  |  | static int ata_passthrough_16_xfer(SCSIDevice *dev, uint8_t *buf) | 
					
						
							| 
									
										
										
										
											2012-08-03 12:02:19 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     int extend = buf[1] & 0x1; | 
					
						
							|  |  |  |     int length = buf[2] & 0x3; | 
					
						
							|  |  |  |     int xfer; | 
					
						
							|  |  |  |     int unit = ata_passthrough_xfer_unit(dev, buf); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (length) { | 
					
						
							|  |  |  |     case 0: | 
					
						
							|  |  |  |     case 3: /* USB-specific.  */ | 
					
						
							| 
									
										
										
										
											2012-08-13 13:05:44 +02:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											2012-08-03 12:02:19 +08:00
										 |  |  |         xfer = 0; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case 1: | 
					
						
							|  |  |  |         xfer = buf[4]; | 
					
						
							|  |  |  |         xfer |= (extend ? buf[3] << 8 : 0); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case 2: | 
					
						
							|  |  |  |         xfer = buf[6]; | 
					
						
							|  |  |  |         xfer |= (extend ? buf[5] << 8 : 0); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return xfer * unit; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-29 13:00:04 +01:00
										 |  |  | static int scsi_req_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) | 
					
						
							| 
									
										
										
										
											2012-09-05 17:57:19 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-10-29 13:00:04 +01:00
										 |  |  |     cmd->xfer = scsi_cdb_xfer(buf); | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:11 +02:00
										 |  |  |     switch (buf[0]) { | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |     case TEST_UNIT_READY: | 
					
						
							| 
									
										
										
											
												scsi: Sanitize command definitions
Sanitize SCSI command definitions.
Add _10 suffix to READ_CAPACITY, WRITE_VERIFY, VERIFY, READ_LONG,
WRITE_LONG, and WRITE_SAME.
Add new command definitions for LOCATE_10, UNMAP, VARLENGTH_CDB,
WRITE_FILEMARKS_16, EXTENDED_COPY, ATA_PASSTHROUGH, ACCESS_CONTROL_IN,
ACCESS_CONTROL_OUT, COMPARE_AND_WRITE, VERIFY_16, SYNCHRONIZE_CACHE_16,
LOCATE_16, ERASE_16, WRITE_LONG_16, LOAD_UNLOAD, VERIFY_12.
Remove invalid definition of WRITE_LONG_2.
Signed-off-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
											
										 
											2011-07-22 16:51:15 +02:00
										 |  |  |     case REWIND: | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |     case START_STOP: | 
					
						
							| 
									
										
										
										
											2011-11-14 14:31:48 +01:00
										 |  |  |     case SET_CAPACITY: | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |     case WRITE_FILEMARKS: | 
					
						
							| 
									
										
										
										
											2011-11-14 14:31:49 +01:00
										 |  |  |     case WRITE_FILEMARKS_16: | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |     case SPACE: | 
					
						
							| 
									
										
										
										
											2010-09-06 16:58:44 +02:00
										 |  |  |     case RESERVE: | 
					
						
							|  |  |  |     case RELEASE: | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |     case ERASE: | 
					
						
							|  |  |  |     case ALLOW_MEDIUM_REMOVAL: | 
					
						
							|  |  |  |     case SEEK_10: | 
					
						
							|  |  |  |     case SYNCHRONIZE_CACHE: | 
					
						
							| 
									
										
										
										
											2011-11-14 14:31:49 +01:00
										 |  |  |     case SYNCHRONIZE_CACHE_16: | 
					
						
							|  |  |  |     case LOCATE_16: | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |     case LOCK_UNLOCK_CACHE: | 
					
						
							|  |  |  |     case SET_CD_SPEED: | 
					
						
							|  |  |  |     case SET_LIMITS: | 
					
						
							| 
									
										
										
											
												scsi: Sanitize command definitions
Sanitize SCSI command definitions.
Add _10 suffix to READ_CAPACITY, WRITE_VERIFY, VERIFY, READ_LONG,
WRITE_LONG, and WRITE_SAME.
Add new command definitions for LOCATE_10, UNMAP, VARLENGTH_CDB,
WRITE_FILEMARKS_16, EXTENDED_COPY, ATA_PASSTHROUGH, ACCESS_CONTROL_IN,
ACCESS_CONTROL_OUT, COMPARE_AND_WRITE, VERIFY_16, SYNCHRONIZE_CACHE_16,
LOCATE_16, ERASE_16, WRITE_LONG_16, LOAD_UNLOAD, VERIFY_12.
Remove invalid definition of WRITE_LONG_2.
Signed-off-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
											
										 
											2011-07-22 16:51:15 +02:00
										 |  |  |     case WRITE_LONG_10: | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |     case UPDATE_BLOCK: | 
					
						
							| 
									
										
										
										
											2011-11-14 14:31:49 +01:00
										 |  |  |     case RESERVE_TRACK: | 
					
						
							|  |  |  |     case SET_READ_AHEAD: | 
					
						
							|  |  |  |     case PRE_FETCH: | 
					
						
							|  |  |  |     case PRE_FETCH_16: | 
					
						
							|  |  |  |     case ALLOW_OVERWRITE: | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:11 +02:00
										 |  |  |         cmd->xfer = 0; | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2013-11-28 11:01:13 +01:00
										 |  |  |     case VERIFY_10: | 
					
						
							|  |  |  |     case VERIFY_12: | 
					
						
							|  |  |  |     case VERIFY_16: | 
					
						
							|  |  |  |         if ((buf[1] & 2) == 0) { | 
					
						
							|  |  |  |             cmd->xfer = 0; | 
					
						
							| 
									
										
										
										
											2014-01-29 18:47:39 +01:00
										 |  |  |         } else if ((buf[1] & 4) != 0) { | 
					
						
							| 
									
										
										
										
											2013-11-28 11:01:13 +01:00
										 |  |  |             cmd->xfer = 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         cmd->xfer *= dev->blocksize; | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |     case MODE_SENSE: | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
											
												scsi: Sanitize command definitions
Sanitize SCSI command definitions.
Add _10 suffix to READ_CAPACITY, WRITE_VERIFY, VERIFY, READ_LONG,
WRITE_LONG, and WRITE_SAME.
Add new command definitions for LOCATE_10, UNMAP, VARLENGTH_CDB,
WRITE_FILEMARKS_16, EXTENDED_COPY, ATA_PASSTHROUGH, ACCESS_CONTROL_IN,
ACCESS_CONTROL_OUT, COMPARE_AND_WRITE, VERIFY_16, SYNCHRONIZE_CACHE_16,
LOCATE_16, ERASE_16, WRITE_LONG_16, LOAD_UNLOAD, VERIFY_12.
Remove invalid definition of WRITE_LONG_2.
Signed-off-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
											
										 
											2011-07-22 16:51:15 +02:00
										 |  |  |     case WRITE_SAME_10: | 
					
						
							| 
									
										
										
										
											2012-02-08 10:40:37 +01:00
										 |  |  |     case WRITE_SAME_16: | 
					
						
							| 
									
										
										
										
											2018-03-08 16:17:59 +01:00
										 |  |  |         cmd->xfer = buf[1] & 1 ? 0 : dev->blocksize; | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
											
												scsi: Sanitize command definitions
Sanitize SCSI command definitions.
Add _10 suffix to READ_CAPACITY, WRITE_VERIFY, VERIFY, READ_LONG,
WRITE_LONG, and WRITE_SAME.
Add new command definitions for LOCATE_10, UNMAP, VARLENGTH_CDB,
WRITE_FILEMARKS_16, EXTENDED_COPY, ATA_PASSTHROUGH, ACCESS_CONTROL_IN,
ACCESS_CONTROL_OUT, COMPARE_AND_WRITE, VERIFY_16, SYNCHRONIZE_CACHE_16,
LOCATE_16, ERASE_16, WRITE_LONG_16, LOAD_UNLOAD, VERIFY_12.
Remove invalid definition of WRITE_LONG_2.
Signed-off-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
											
										 
											2011-07-22 16:51:15 +02:00
										 |  |  |     case READ_CAPACITY_10: | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:11 +02:00
										 |  |  |         cmd->xfer = 8; | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case READ_BLOCK_LIMITS: | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:11 +02:00
										 |  |  |         cmd->xfer = 6; | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case SEND_VOLUME_TAG: | 
					
						
							| 
									
										
										
										
											2011-11-14 14:31:49 +01:00
										 |  |  |         /* GPCMD_SET_STREAMING from multimedia commands.  */ | 
					
						
							|  |  |  |         if (dev->type == TYPE_ROM) { | 
					
						
							|  |  |  |             cmd->xfer = buf[10] | (buf[9] << 8); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             cmd->xfer = buf[9] | (buf[8] << 8); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2012-05-03 15:28:05 +02:00
										 |  |  |     case WRITE_6: | 
					
						
							|  |  |  |         /* length 0 means 256 blocks */ | 
					
						
							|  |  |  |         if (cmd->xfer == 0) { | 
					
						
							|  |  |  |             cmd->xfer = 256; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-05-16 17:44:05 +02:00
										 |  |  |         /* fall through */ | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |     case WRITE_10: | 
					
						
							| 
									
										
										
											
												scsi: Sanitize command definitions
Sanitize SCSI command definitions.
Add _10 suffix to READ_CAPACITY, WRITE_VERIFY, VERIFY, READ_LONG,
WRITE_LONG, and WRITE_SAME.
Add new command definitions for LOCATE_10, UNMAP, VARLENGTH_CDB,
WRITE_FILEMARKS_16, EXTENDED_COPY, ATA_PASSTHROUGH, ACCESS_CONTROL_IN,
ACCESS_CONTROL_OUT, COMPARE_AND_WRITE, VERIFY_16, SYNCHRONIZE_CACHE_16,
LOCATE_16, ERASE_16, WRITE_LONG_16, LOAD_UNLOAD, VERIFY_12.
Remove invalid definition of WRITE_LONG_2.
Signed-off-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
											
										 
											2011-07-22 16:51:15 +02:00
										 |  |  |     case WRITE_VERIFY_10: | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |     case WRITE_12: | 
					
						
							|  |  |  |     case WRITE_VERIFY_12: | 
					
						
							| 
									
										
										
										
											2009-11-26 15:34:16 +01:00
										 |  |  |     case WRITE_16: | 
					
						
							|  |  |  |     case WRITE_VERIFY_16: | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:11 +02:00
										 |  |  |         cmd->xfer *= dev->blocksize; | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case READ_6: | 
					
						
							|  |  |  |     case READ_REVERSE: | 
					
						
							| 
									
										
										
										
											2012-05-03 15:28:05 +02:00
										 |  |  |         /* length 0 means 256 blocks */ | 
					
						
							|  |  |  |         if (cmd->xfer == 0) { | 
					
						
							|  |  |  |             cmd->xfer = 256; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-05-16 17:44:05 +02:00
										 |  |  |         /* fall through */ | 
					
						
							| 
									
										
										
										
											2012-05-03 15:28:05 +02:00
										 |  |  |     case READ_10: | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |     case READ_12: | 
					
						
							| 
									
										
										
										
											2009-11-26 15:34:16 +01:00
										 |  |  |     case READ_16: | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:11 +02:00
										 |  |  |         cmd->xfer *= dev->blocksize; | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2011-11-14 14:31:49 +01:00
										 |  |  |     case FORMAT_UNIT: | 
					
						
							|  |  |  |         /* MMC mandates the parameter list to be 12-bytes long.  Parameters
 | 
					
						
							|  |  |  |          * for block devices are restricted to the header right now.  */ | 
					
						
							|  |  |  |         if (dev->type == TYPE_ROM && (buf[1] & 16)) { | 
					
						
							|  |  |  |             cmd->xfer = 12; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             cmd->xfer = (buf[1] & 16) == 0 ? 0 : (buf[1] & 32 ? 8 : 4); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |     case INQUIRY: | 
					
						
							| 
									
										
										
										
											2011-11-14 14:31:49 +01:00
										 |  |  |     case RECEIVE_DIAGNOSTIC: | 
					
						
							|  |  |  |     case SEND_DIAGNOSTIC: | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:11 +02:00
										 |  |  |         cmd->xfer = buf[4] | (buf[3] << 8); | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2011-11-14 14:31:49 +01:00
										 |  |  |     case READ_CD: | 
					
						
							|  |  |  |     case READ_BUFFER: | 
					
						
							|  |  |  |     case WRITE_BUFFER: | 
					
						
							|  |  |  |     case SEND_CUE_SHEET: | 
					
						
							|  |  |  |         cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case PERSISTENT_RESERVE_OUT: | 
					
						
							|  |  |  |         cmd->xfer = ldl_be_p(&buf[5]) & 0xffffffffULL; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case ERASE_12: | 
					
						
							|  |  |  |         if (dev->type == TYPE_ROM) { | 
					
						
							|  |  |  |             /* MMC command GET PERFORMANCE.  */ | 
					
						
							|  |  |  |             cmd->xfer = scsi_get_performance_length(buf[9] | (buf[8] << 8), | 
					
						
							|  |  |  |                                                     buf[10], buf[1] & 0x1f); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case MECHANISM_STATUS: | 
					
						
							|  |  |  |     case READ_DVD_STRUCTURE: | 
					
						
							|  |  |  |     case SEND_DVD_STRUCTURE: | 
					
						
							| 
									
										
										
										
											2010-06-16 06:43:06 -07:00
										 |  |  |     case MAINTENANCE_OUT: | 
					
						
							|  |  |  |     case MAINTENANCE_IN: | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:11 +02:00
										 |  |  |         if (dev->type == TYPE_ROM) { | 
					
						
							| 
									
										
										
										
											2010-06-16 06:43:06 -07:00
										 |  |  |             /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */ | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:11 +02:00
										 |  |  |             cmd->xfer = buf[9] | (buf[8] << 8); | 
					
						
							| 
									
										
										
										
											2010-06-16 06:43:06 -07:00
										 |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2012-08-03 12:02:19 +08:00
										 |  |  |     case ATA_PASSTHROUGH_12: | 
					
						
							|  |  |  |         if (dev->type == TYPE_ROM) { | 
					
						
							|  |  |  |             /* BLANK command of MMC */ | 
					
						
							|  |  |  |             cmd->xfer = 0; | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2014-10-29 13:00:04 +01:00
										 |  |  |             cmd->xfer = ata_passthrough_12_xfer(dev, buf); | 
					
						
							| 
									
										
										
										
											2012-08-03 12:02:19 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case ATA_PASSTHROUGH_16: | 
					
						
							| 
									
										
										
										
											2014-10-29 13:00:04 +01:00
										 |  |  |         cmd->xfer = ata_passthrough_16_xfer(dev, buf); | 
					
						
							| 
									
										
										
										
											2012-08-03 12:02:19 +08:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-29 13:00:04 +01:00
										 |  |  | static int scsi_req_stream_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:11 +02:00
										 |  |  |     switch (buf[0]) { | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |     /* stream commands */ | 
					
						
							| 
									
										
										
										
											2011-11-14 14:31:49 +01:00
										 |  |  |     case ERASE_12: | 
					
						
							|  |  |  |     case ERASE_16: | 
					
						
							|  |  |  |         cmd->xfer = 0; | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |     case READ_6: | 
					
						
							|  |  |  |     case READ_REVERSE: | 
					
						
							|  |  |  |     case RECOVER_BUFFERED_DATA: | 
					
						
							|  |  |  |     case WRITE_6: | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:11 +02:00
										 |  |  |         cmd->xfer = buf[4] | (buf[3] << 8) | (buf[2] << 16); | 
					
						
							|  |  |  |         if (buf[1] & 0x01) { /* fixed */ | 
					
						
							|  |  |  |             cmd->xfer *= dev->blocksize; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2012-05-04 10:28:55 +02:00
										 |  |  |     case READ_16: | 
					
						
							|  |  |  |     case READ_REVERSE_16: | 
					
						
							|  |  |  |     case VERIFY_16: | 
					
						
							|  |  |  |     case WRITE_16: | 
					
						
							|  |  |  |         cmd->xfer = buf[14] | (buf[13] << 8) | (buf[12] << 16); | 
					
						
							|  |  |  |         if (buf[1] & 0x01) { /* fixed */ | 
					
						
							|  |  |  |             cmd->xfer *= dev->blocksize; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |     case REWIND: | 
					
						
							| 
									
										
										
										
											2012-06-14 15:55:26 +02:00
										 |  |  |     case LOAD_UNLOAD: | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:11 +02:00
										 |  |  |         cmd->xfer = 0; | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2011-11-14 14:31:49 +01:00
										 |  |  |     case SPACE_16: | 
					
						
							|  |  |  |         cmd->xfer = buf[13] | (buf[12] << 8); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case READ_POSITION: | 
					
						
							| 
									
										
										
										
											2012-06-14 15:55:28 +02:00
										 |  |  |         switch (buf[1] & 0x1f) /* operation code */ { | 
					
						
							|  |  |  |         case SHORT_FORM_BLOCK_ID: | 
					
						
							|  |  |  |         case SHORT_FORM_VENDOR_SPECIFIC: | 
					
						
							|  |  |  |             cmd->xfer = 20; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case LONG_FORM: | 
					
						
							|  |  |  |             cmd->xfer = 32; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case EXTENDED_FORM: | 
					
						
							|  |  |  |             cmd->xfer = buf[8] | (buf[7] << 8); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-14 14:31:49 +01:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case FORMAT_UNIT: | 
					
						
							|  |  |  |         cmd->xfer = buf[4] | (buf[3] << 8); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |     /* generic commands */ | 
					
						
							|  |  |  |     default: | 
					
						
							| 
									
										
										
										
											2014-10-29 13:00:04 +01:00
										 |  |  |         return scsi_req_xfer(cmd, dev, buf); | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-29 13:00:04 +01:00
										 |  |  | static int scsi_req_medium_changer_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) | 
					
						
							| 
									
										
										
										
											2012-06-14 15:55:27 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     switch (buf[0]) { | 
					
						
							|  |  |  |     /* medium changer commands */ | 
					
						
							|  |  |  |     case EXCHANGE_MEDIUM: | 
					
						
							|  |  |  |     case INITIALIZE_ELEMENT_STATUS: | 
					
						
							|  |  |  |     case INITIALIZE_ELEMENT_STATUS_WITH_RANGE: | 
					
						
							|  |  |  |     case MOVE_MEDIUM: | 
					
						
							|  |  |  |     case POSITION_TO_ELEMENT: | 
					
						
							|  |  |  |         cmd->xfer = 0; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case READ_ELEMENT_STATUS: | 
					
						
							|  |  |  |         cmd->xfer = buf[9] | (buf[8] << 8) | (buf[7] << 16); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* generic commands */ | 
					
						
							|  |  |  |     default: | 
					
						
							| 
									
										
										
										
											2014-10-29 13:00:04 +01:00
										 |  |  |         return scsi_req_xfer(cmd, dev, buf); | 
					
						
							| 
									
										
										
										
											2012-06-14 15:55:27 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-28 21:51:52 +03:00
										 |  |  | static int scsi_req_scanner_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     switch (buf[0]) { | 
					
						
							|  |  |  |     /* Scanner commands */ | 
					
						
							|  |  |  |     case OBJECT_POSITION: | 
					
						
							|  |  |  |         cmd->xfer = 0; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case SCAN: | 
					
						
							|  |  |  |         cmd->xfer = buf[4]; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case READ_10: | 
					
						
							|  |  |  |     case SEND: | 
					
						
							|  |  |  |     case GET_WINDOW: | 
					
						
							|  |  |  |     case SET_WINDOW: | 
					
						
							|  |  |  |         cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         /* GET_DATA_BUFFER_STATUS xfer handled by scsi_req_xfer */ | 
					
						
							|  |  |  |         return scsi_req_xfer(cmd, dev, buf); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2012-06-14 15:55:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:11 +02:00
										 |  |  | static void scsi_cmd_xfer_mode(SCSICommand *cmd) | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:57 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-02-08 10:40:37 +01:00
										 |  |  |     if (!cmd->xfer) { | 
					
						
							|  |  |  |         cmd->mode = SCSI_XFER_NONE; | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:11 +02:00
										 |  |  |     switch (cmd->buf[0]) { | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:57 +01:00
										 |  |  |     case WRITE_6: | 
					
						
							|  |  |  |     case WRITE_10: | 
					
						
							| 
									
										
										
											
												scsi: Sanitize command definitions
Sanitize SCSI command definitions.
Add _10 suffix to READ_CAPACITY, WRITE_VERIFY, VERIFY, READ_LONG,
WRITE_LONG, and WRITE_SAME.
Add new command definitions for LOCATE_10, UNMAP, VARLENGTH_CDB,
WRITE_FILEMARKS_16, EXTENDED_COPY, ATA_PASSTHROUGH, ACCESS_CONTROL_IN,
ACCESS_CONTROL_OUT, COMPARE_AND_WRITE, VERIFY_16, SYNCHRONIZE_CACHE_16,
LOCATE_16, ERASE_16, WRITE_LONG_16, LOAD_UNLOAD, VERIFY_12.
Remove invalid definition of WRITE_LONG_2.
Signed-off-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
											
										 
											2011-07-22 16:51:15 +02:00
										 |  |  |     case WRITE_VERIFY_10: | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:57 +01:00
										 |  |  |     case WRITE_12: | 
					
						
							|  |  |  |     case WRITE_VERIFY_12: | 
					
						
							| 
									
										
										
										
											2009-11-26 15:34:16 +01:00
										 |  |  |     case WRITE_16: | 
					
						
							|  |  |  |     case WRITE_VERIFY_16: | 
					
						
							| 
									
										
										
										
											2013-11-28 11:01:13 +01:00
										 |  |  |     case VERIFY_10: | 
					
						
							|  |  |  |     case VERIFY_12: | 
					
						
							|  |  |  |     case VERIFY_16: | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:57 +01:00
										 |  |  |     case COPY: | 
					
						
							|  |  |  |     case COPY_VERIFY: | 
					
						
							|  |  |  |     case COMPARE: | 
					
						
							|  |  |  |     case CHANGE_DEFINITION: | 
					
						
							|  |  |  |     case LOG_SELECT: | 
					
						
							|  |  |  |     case MODE_SELECT: | 
					
						
							|  |  |  |     case MODE_SELECT_10: | 
					
						
							|  |  |  |     case SEND_DIAGNOSTIC: | 
					
						
							|  |  |  |     case WRITE_BUFFER: | 
					
						
							|  |  |  |     case FORMAT_UNIT: | 
					
						
							|  |  |  |     case REASSIGN_BLOCKS: | 
					
						
							|  |  |  |     case SEARCH_EQUAL: | 
					
						
							|  |  |  |     case SEARCH_HIGH: | 
					
						
							|  |  |  |     case SEARCH_LOW: | 
					
						
							|  |  |  |     case UPDATE_BLOCK: | 
					
						
							| 
									
										
										
											
												scsi: Sanitize command definitions
Sanitize SCSI command definitions.
Add _10 suffix to READ_CAPACITY, WRITE_VERIFY, VERIFY, READ_LONG,
WRITE_LONG, and WRITE_SAME.
Add new command definitions for LOCATE_10, UNMAP, VARLENGTH_CDB,
WRITE_FILEMARKS_16, EXTENDED_COPY, ATA_PASSTHROUGH, ACCESS_CONTROL_IN,
ACCESS_CONTROL_OUT, COMPARE_AND_WRITE, VERIFY_16, SYNCHRONIZE_CACHE_16,
LOCATE_16, ERASE_16, WRITE_LONG_16, LOAD_UNLOAD, VERIFY_12.
Remove invalid definition of WRITE_LONG_2.
Signed-off-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
											
										 
											2011-07-22 16:51:15 +02:00
										 |  |  |     case WRITE_LONG_10: | 
					
						
							|  |  |  |     case WRITE_SAME_10: | 
					
						
							| 
									
										
										
										
											2012-02-08 10:40:37 +01:00
										 |  |  |     case WRITE_SAME_16: | 
					
						
							| 
									
										
										
										
											2012-04-28 23:49:36 +10:00
										 |  |  |     case UNMAP: | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:57 +01:00
										 |  |  |     case SEARCH_HIGH_12: | 
					
						
							|  |  |  |     case SEARCH_EQUAL_12: | 
					
						
							|  |  |  |     case SEARCH_LOW_12: | 
					
						
							|  |  |  |     case MEDIUM_SCAN: | 
					
						
							|  |  |  |     case SEND_VOLUME_TAG: | 
					
						
							| 
									
										
										
										
											2011-11-14 14:31:49 +01:00
										 |  |  |     case SEND_CUE_SHEET: | 
					
						
							|  |  |  |     case SEND_DVD_STRUCTURE: | 
					
						
							| 
									
										
										
										
											2010-06-16 06:42:58 -07:00
										 |  |  |     case PERSISTENT_RESERVE_OUT: | 
					
						
							| 
									
										
										
										
											2010-06-16 06:43:06 -07:00
										 |  |  |     case MAINTENANCE_OUT: | 
					
						
							| 
									
										
										
										
											2016-06-28 21:51:52 +03:00
										 |  |  |     case SET_WINDOW: | 
					
						
							|  |  |  |     case SCAN: | 
					
						
							|  |  |  |         /* SCAN conflicts with START_STOP.  START_STOP has cmd->xfer set to 0 for
 | 
					
						
							|  |  |  |          * non-scanner devices, so we only get here for SCAN and not for START_STOP. | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:11 +02:00
										 |  |  |         cmd->mode = SCSI_XFER_TO_DEV; | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:57 +01:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2012-08-03 12:02:19 +08:00
										 |  |  |     case ATA_PASSTHROUGH_12: | 
					
						
							|  |  |  |     case ATA_PASSTHROUGH_16: | 
					
						
							|  |  |  |         /* T_DIR */ | 
					
						
							|  |  |  |         cmd->mode = (cmd->buf[2] & 0x8) ? | 
					
						
							|  |  |  |                    SCSI_XFER_FROM_DEV : SCSI_XFER_TO_DEV; | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:57 +01:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											2012-02-08 10:40:37 +01:00
										 |  |  |         cmd->mode = SCSI_XFER_FROM_DEV; | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:57 +01:00
										 |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-29 13:00:04 +01:00
										 |  |  | int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int rc; | 
					
						
							| 
									
										
										
										
											2015-07-21 08:59:39 +02:00
										 |  |  |     int len; | 
					
						
							| 
									
										
										
										
											2014-10-29 13:00:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     cmd->lba = -1; | 
					
						
							| 
									
										
										
										
											2015-07-21 08:59:39 +02:00
										 |  |  |     len = scsi_cdb_length(buf); | 
					
						
							|  |  |  |     if (len < 0) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-06-14 16:13:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-21 08:59:39 +02:00
										 |  |  |     cmd->len = len; | 
					
						
							| 
									
										
										
										
											2012-06-14 15:55:27 +02:00
										 |  |  |     switch (dev->type) { | 
					
						
							|  |  |  |     case TYPE_TAPE: | 
					
						
							| 
									
										
										
										
											2014-10-29 13:00:04 +01:00
										 |  |  |         rc = scsi_req_stream_xfer(cmd, dev, buf); | 
					
						
							| 
									
										
										
										
											2012-06-14 15:55:27 +02:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case TYPE_MEDIUM_CHANGER: | 
					
						
							| 
									
										
										
										
											2014-10-29 13:00:04 +01:00
										 |  |  |         rc = scsi_req_medium_changer_xfer(cmd, dev, buf); | 
					
						
							| 
									
										
										
										
											2012-06-14 15:55:27 +02:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2016-06-28 21:51:52 +03:00
										 |  |  |     case TYPE_SCANNER: | 
					
						
							|  |  |  |         rc = scsi_req_scanner_length(cmd, dev, buf); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2012-06-14 15:55:27 +02:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											2014-10-29 13:00:04 +01:00
										 |  |  |         rc = scsi_req_xfer(cmd, dev, buf); | 
					
						
							| 
									
										
										
										
											2012-06-14 15:55:27 +02:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-06-14 15:55:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |     if (rc != 0) | 
					
						
							|  |  |  |         return rc; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:13 +02:00
										 |  |  |     memcpy(cmd->buf, buf, cmd->len); | 
					
						
							|  |  |  |     scsi_cmd_xfer_mode(cmd); | 
					
						
							|  |  |  |     cmd->lba = scsi_cmd_lba(cmd); | 
					
						
							| 
									
										
										
										
											2009-11-26 15:33:55 +01:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-11-26 15:34:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-16 14:22:36 +02:00
										 |  |  | void scsi_device_report_change(SCSIDevice *dev, SCSISense sense) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     scsi_device_set_ua(dev, sense); | 
					
						
							|  |  |  |     if (bus->info->change) { | 
					
						
							|  |  |  |         bus->info->change(bus, dev, sense); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-18 16:01:56 +02:00
										 |  |  | SCSIRequest *scsi_req_ref(SCSIRequest *req) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-07-09 12:06:28 +02:00
										 |  |  |     assert(req->refcount > 0); | 
					
						
							| 
									
										
										
										
											2011-04-18 16:01:56 +02:00
										 |  |  |     req->refcount++; | 
					
						
							|  |  |  |     return req; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void scsi_req_unref(SCSIRequest *req) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-05-04 08:51:16 +02:00
										 |  |  |     assert(req->refcount > 0); | 
					
						
							| 
									
										
										
										
											2011-04-18 16:01:56 +02:00
										 |  |  |     if (--req->refcount == 0) { | 
					
						
							| 
									
										
										
										
											2013-06-03 14:09:39 +02:00
										 |  |  |         BusState *qbus = req->dev->qdev.parent_bus; | 
					
						
							|  |  |  |         SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, qbus); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-09 12:06:28 +02:00
										 |  |  |         if (bus->info->free_request && req->hba_private) { | 
					
						
							|  |  |  |             bus->info->free_request(bus, req->hba_private); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:09 +02:00
										 |  |  |         if (req->ops->free_req) { | 
					
						
							|  |  |  |             req->ops->free_req(req); | 
					
						
							| 
									
										
										
										
											2011-04-18 16:01:56 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-06-03 14:09:39 +02:00
										 |  |  |         object_unref(OBJECT(req->dev)); | 
					
						
							|  |  |  |         object_unref(OBJECT(qbus->parent)); | 
					
						
							| 
									
										
										
										
											2015-10-01 12:59:01 +02:00
										 |  |  |         g_free(req); | 
					
						
							| 
									
										
										
										
											2011-04-18 16:01:56 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-18 15:28:11 +02:00
										 |  |  | /* Tell the device that we finished processing this chunk of I/O.  It
 | 
					
						
							|  |  |  |    will start the next chunk or complete the command.  */ | 
					
						
							|  |  |  | void scsi_req_continue(SCSIRequest *req) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-02-25 12:12:58 +01:00
										 |  |  |     if (req->io_canceled) { | 
					
						
							|  |  |  |         trace_scsi_req_continue_canceled(req->dev->id, req->lun, req->tag); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-04-18 15:28:11 +02:00
										 |  |  |     trace_scsi_req_continue(req->dev->id, req->lun, req->tag); | 
					
						
							|  |  |  |     if (req->cmd.mode == SCSI_XFER_TO_DEV) { | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:09 +02:00
										 |  |  |         req->ops->write_data(req); | 
					
						
							| 
									
										
										
										
											2011-04-18 15:28:11 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:09 +02:00
										 |  |  |         req->ops->read_data(req); | 
					
						
							| 
									
										
										
										
											2011-04-18 15:28:11 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-18 14:59:13 +02:00
										 |  |  | /* Called by the devices when data is ready for the HBA.  The HBA should
 | 
					
						
							|  |  |  |    start a DMA operation to read or fill the device's data buffer. | 
					
						
							| 
									
										
										
										
											2011-04-18 15:28:11 +02:00
										 |  |  |    Once it completes, calling scsi_req_continue will restart I/O.  */ | 
					
						
							| 
									
										
										
										
											2011-04-18 14:59:13 +02:00
										 |  |  | void scsi_req_data(SCSIRequest *req, int len) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-07-06 11:26:47 +02:00
										 |  |  |     uint8_t *buf; | 
					
						
							| 
									
										
										
										
											2011-10-25 12:53:33 +02:00
										 |  |  |     if (req->io_canceled) { | 
					
						
							|  |  |  |         trace_scsi_req_data_canceled(req->dev->id, req->lun, req->tag, len); | 
					
						
							| 
									
										
										
										
											2011-07-06 11:55:37 +02:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2011-10-25 12:53:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-07-06 11:55:37 +02:00
										 |  |  |     trace_scsi_req_data(req->dev->id, req->lun, req->tag, len); | 
					
						
							|  |  |  |     assert(req->cmd.mode != SCSI_XFER_NONE); | 
					
						
							| 
									
										
										
										
											2011-07-06 11:26:47 +02:00
										 |  |  |     if (!req->sg) { | 
					
						
							|  |  |  |         req->resid -= len; | 
					
						
							|  |  |  |         req->bus->info->transfer_data(req, len); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* If the device calls scsi_req_data and the HBA specified a
 | 
					
						
							|  |  |  |      * scatter/gather list, the transfer has to happen in a single | 
					
						
							|  |  |  |      * step.  */ | 
					
						
							|  |  |  |     assert(!req->dma_started); | 
					
						
							|  |  |  |     req->dma_started = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     buf = scsi_req_get_buf(req); | 
					
						
							|  |  |  |     if (req->cmd.mode == SCSI_XFER_FROM_DEV) { | 
					
						
							|  |  |  |         req->resid = dma_buf_read(buf, len, req->sg); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         req->resid = dma_buf_write(buf, len, req->sg); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     scsi_req_continue(req); | 
					
						
							| 
									
										
										
										
											2011-04-18 14:59:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-26 15:34:01 +01:00
										 |  |  | void scsi_req_print(SCSIRequest *req) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     FILE *fp = stderr; | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fprintf(fp, "[%s id=%d] %s", | 
					
						
							|  |  |  |             req->dev->qdev.parent_bus->name, | 
					
						
							|  |  |  |             req->dev->id, | 
					
						
							|  |  |  |             scsi_command_name(req->cmd.buf[0])); | 
					
						
							|  |  |  |     for (i = 1; i < req->cmd.len; i++) { | 
					
						
							|  |  |  |         fprintf(fp, " 0x%02x", req->cmd.buf[i]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     switch (req->cmd.mode) { | 
					
						
							|  |  |  |     case SCSI_XFER_NONE: | 
					
						
							|  |  |  |         fprintf(fp, " - none\n"); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case SCSI_XFER_FROM_DEV: | 
					
						
							|  |  |  |         fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case SCSI_XFER_TO_DEV: | 
					
						
							|  |  |  |         fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         fprintf(fp, " - Oops\n"); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:06 +02:00
										 |  |  | void scsi_req_complete(SCSIRequest *req, int status) | 
					
						
							| 
									
										
										
										
											2009-11-26 15:34:00 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:06 +02:00
										 |  |  |     assert(req->status == -1); | 
					
						
							|  |  |  |     req->status = status; | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-14 15:55:25 +02:00
										 |  |  |     assert(req->sense_len <= sizeof(req->sense)); | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:07 +02:00
										 |  |  |     if (status == GOOD) { | 
					
						
							|  |  |  |         req->sense_len = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (req->sense_len) { | 
					
						
							|  |  |  |         memcpy(req->dev->sense, req->sense, req->sense_len); | 
					
						
							| 
									
										
										
										
											2011-09-13 16:19:53 +02:00
										 |  |  |         req->dev->sense_len = req->sense_len; | 
					
						
							|  |  |  |         req->dev->sense_is_ua = (req->ops == &reqops_unit_attention); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         req->dev->sense_len = 0; | 
					
						
							|  |  |  |         req->dev->sense_is_ua = false; | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:07 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:17 +02:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * Unit attention state is now stored in the device's sense buffer | 
					
						
							|  |  |  |      * if the HBA didn't do autosense.  Clear the pending unit attention | 
					
						
							|  |  |  |      * flags. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     scsi_clear_unit_attention(req); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-18 16:01:56 +02:00
										 |  |  |     scsi_req_ref(req); | 
					
						
							| 
									
										
										
										
											2010-07-13 14:13:45 +02:00
										 |  |  |     scsi_req_dequeue(req); | 
					
						
							| 
									
										
										
										
											2011-07-06 11:55:37 +02:00
										 |  |  |     req->bus->info->complete(req, req->status, req->resid); | 
					
						
							| 
									
										
										
										
											2014-09-28 09:48:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Cancelled requests might end up being completed instead of cancelled */ | 
					
						
							|  |  |  |     notifier_list_notify(&req->cancel_notifiers, req); | 
					
						
							| 
									
										
										
										
											2011-04-18 16:01:56 +02:00
										 |  |  |     scsi_req_unref(req); | 
					
						
							| 
									
										
										
										
											2009-11-26 15:34:00 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-12-08 13:35:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-25 10:20:47 +08:00
										 |  |  | /* Called by the devices when the request is canceled. */ | 
					
						
							|  |  |  | void scsi_req_cancel_complete(SCSIRequest *req) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     assert(req->io_canceled); | 
					
						
							|  |  |  |     if (req->bus->info->cancel) { | 
					
						
							|  |  |  |         req->bus->info->cancel(req); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-09-28 09:48:00 +08:00
										 |  |  |     notifier_list_notify(&req->cancel_notifiers, req); | 
					
						
							| 
									
										
										
										
											2014-09-25 10:20:47 +08:00
										 |  |  |     scsi_req_unref(req); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-28 09:48:00 +08:00
										 |  |  | /* Cancel @req asynchronously. @notifier is added to @req's cancellation
 | 
					
						
							|  |  |  |  * notifier list, the bus will be notified the requests cancellation is | 
					
						
							|  |  |  |  * completed. | 
					
						
							|  |  |  |  * */ | 
					
						
							|  |  |  | void scsi_req_cancel_async(SCSIRequest *req, Notifier *notifier) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     trace_scsi_req_cancel(req->dev->id, req->lun, req->tag); | 
					
						
							|  |  |  |     if (notifier) { | 
					
						
							|  |  |  |         notifier_list_add(&req->cancel_notifiers, notifier); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-12-18 09:54:53 +01:00
										 |  |  |     if (req->io_canceled) { | 
					
						
							|  |  |  |         /* A blk_aio_cancel_async is pending; when it finishes,
 | 
					
						
							|  |  |  |          * scsi_req_cancel_complete will be called and will | 
					
						
							|  |  |  |          * call the notifier we just added.  Just wait for that. | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         assert(req->aiocb); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     /* Dropped in scsi_req_cancel_complete.  */ | 
					
						
							| 
									
										
										
										
											2014-09-28 09:48:00 +08:00
										 |  |  |     scsi_req_ref(req); | 
					
						
							|  |  |  |     scsi_req_dequeue(req); | 
					
						
							|  |  |  |     req->io_canceled = true; | 
					
						
							|  |  |  |     if (req->aiocb) { | 
					
						
							| 
									
										
										
										
											2014-10-07 13:59:18 +02:00
										 |  |  |         blk_aio_cancel_async(req->aiocb); | 
					
						
							| 
									
										
										
										
											2015-01-27 17:16:59 +08:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         scsi_req_cancel_complete(req); | 
					
						
							| 
									
										
										
										
											2014-09-28 09:48:00 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-18 22:53:08 +02:00
										 |  |  | void scsi_req_cancel(SCSIRequest *req) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-07-27 08:23:07 +02:00
										 |  |  |     trace_scsi_req_cancel(req->dev->id, req->lun, req->tag); | 
					
						
							| 
									
										
										
										
											2011-10-25 12:53:33 +02:00
										 |  |  |     if (!req->enqueued) { | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2011-04-18 22:53:08 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-12-18 09:54:53 +01:00
										 |  |  |     assert(!req->io_canceled); | 
					
						
							|  |  |  |     /* Dropped in scsi_req_cancel_complete.  */ | 
					
						
							| 
									
										
										
										
											2011-04-18 22:53:08 +02:00
										 |  |  |     scsi_req_ref(req); | 
					
						
							|  |  |  |     scsi_req_dequeue(req); | 
					
						
							| 
									
										
										
										
											2011-10-25 12:53:33 +02:00
										 |  |  |     req->io_canceled = true; | 
					
						
							| 
									
										
										
										
											2014-09-24 16:27:55 +08:00
										 |  |  |     if (req->aiocb) { | 
					
						
							| 
									
										
										
										
											2014-10-07 13:59:18 +02:00
										 |  |  |         blk_aio_cancel(req->aiocb); | 
					
						
							| 
									
										
										
										
											2015-01-12 11:47:30 +01:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         scsi_req_cancel_complete(req); | 
					
						
							| 
									
										
										
										
											2011-10-25 12:53:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-04-18 22:53:08 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-16 14:18:58 +02:00
										 |  |  | static int scsi_ua_precedence(SCSISense sense) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (sense.key != UNIT_ATTENTION) { | 
					
						
							|  |  |  |         return INT_MAX; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (sense.asc == 0x29 && sense.ascq == 0x04) { | 
					
						
							|  |  |  |         /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */ | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } else if (sense.asc == 0x3F && sense.ascq == 0x01) { | 
					
						
							|  |  |  |         /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */ | 
					
						
							|  |  |  |         return 2; | 
					
						
							|  |  |  |     } else if (sense.asc == 0x29 && (sense.ascq == 0x05 || sense.ascq == 0x06)) { | 
					
						
							|  |  |  |         /* These two go with "all others". */ | 
					
						
							|  |  |  |         ; | 
					
						
							|  |  |  |     } else if (sense.asc == 0x29 && sense.ascq <= 0x07) { | 
					
						
							|  |  |  |         /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
 | 
					
						
							|  |  |  |          * POWER ON OCCURRED = 1 | 
					
						
							|  |  |  |          * SCSI BUS RESET OCCURRED = 2 | 
					
						
							|  |  |  |          * BUS DEVICE RESET FUNCTION OCCURRED = 3 | 
					
						
							|  |  |  |          * I_T NEXUS LOSS OCCURRED = 7 | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         return sense.ascq; | 
					
						
							|  |  |  |     } else if (sense.asc == 0x2F && sense.ascq == 0x01) { | 
					
						
							|  |  |  |         /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION  */ | 
					
						
							|  |  |  |         return 8; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return (sense.asc << 8) | sense.ascq; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int prec1, prec2; | 
					
						
							|  |  |  |     if (sense.key != UNIT_ATTENTION) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     trace_scsi_device_set_ua(sdev->id, sdev->lun, sense.key, | 
					
						
							|  |  |  |                              sense.asc, sense.ascq); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * Override a pre-existing unit attention condition, except for a more | 
					
						
							|  |  |  |      * important reset condition. | 
					
						
							|  |  |  |     */ | 
					
						
							|  |  |  |     prec1 = scsi_ua_precedence(sdev->unit_attention); | 
					
						
							|  |  |  |     prec2 = scsi_ua_precedence(sense); | 
					
						
							|  |  |  |     if (prec2 < prec1) { | 
					
						
							|  |  |  |         sdev->unit_attention = sense; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-03 10:49:18 +02:00
										 |  |  | void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense) | 
					
						
							| 
									
										
										
										
											2011-04-18 18:47:12 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     SCSIRequest *req; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 19:33:43 +01:00
										 |  |  |     aio_context_acquire(blk_get_aio_context(sdev->conf.blk)); | 
					
						
							| 
									
										
										
										
											2011-04-18 18:47:12 +02:00
										 |  |  |     while (!QTAILQ_EMPTY(&sdev->requests)) { | 
					
						
							|  |  |  |         req = QTAILQ_FIRST(&sdev->requests); | 
					
						
							| 
									
										
										
										
											2015-12-16 19:33:43 +01:00
										 |  |  |         scsi_req_cancel_async(req, NULL); | 
					
						
							| 
									
										
										
										
											2011-04-18 18:47:12 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-12-16 19:33:43 +01:00
										 |  |  |     blk_drain(sdev->conf.blk); | 
					
						
							|  |  |  |     aio_context_release(blk_get_aio_context(sdev->conf.blk)); | 
					
						
							| 
									
										
										
										
											2012-07-16 14:18:58 +02:00
										 |  |  |     scsi_device_set_ua(sdev, sense); | 
					
						
							| 
									
										
										
										
											2011-04-18 18:47:12 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-19 15:30:40 +01:00
										 |  |  | static char *scsibus_get_dev_path(DeviceState *dev) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-01-06 17:37:46 +08:00
										 |  |  |     SCSIDevice *d = SCSI_DEVICE(dev); | 
					
						
							| 
									
										
										
										
											2012-03-19 15:30:40 +01:00
										 |  |  |     DeviceState *hba = dev->parent_bus->parent; | 
					
						
							| 
									
										
										
										
											2012-02-03 12:28:43 -06:00
										 |  |  |     char *id; | 
					
						
							| 
									
										
										
										
											2012-04-06 14:12:42 +02:00
										 |  |  |     char *path; | 
					
						
							| 
									
										
										
										
											2012-03-19 15:30:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-03 12:28:43 -06:00
										 |  |  |     id = qdev_get_dev_path(hba); | 
					
						
							| 
									
										
										
										
											2012-03-19 15:30:40 +01:00
										 |  |  |     if (id) { | 
					
						
							| 
									
										
										
										
											2012-04-06 14:12:42 +02:00
										 |  |  |         path = g_strdup_printf("%s/%d:%d:%d", id, d->channel, d->id, d->lun); | 
					
						
							| 
									
										
										
										
											2012-03-19 15:30:40 +01:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2012-04-06 14:12:42 +02:00
										 |  |  |         path = g_strdup_printf("%d:%d:%d", d->channel, d->id, d->lun); | 
					
						
							| 
									
										
										
										
											2012-03-19 15:30:40 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-04-06 14:12:42 +02:00
										 |  |  |     g_free(id); | 
					
						
							|  |  |  |     return path; | 
					
						
							| 
									
										
										
										
											2012-03-19 15:30:40 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-08 13:35:04 +02:00
										 |  |  | static char *scsibus_get_fw_dev_path(DeviceState *dev) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-15 14:50:08 -06:00
										 |  |  |     SCSIDevice *d = SCSI_DEVICE(dev); | 
					
						
							| 
									
										
										
											
												scsi, pci, qdev, isa-bus, sysbus: don't let *_get_fw_dev_path return NULL
Use g_strdup rather than strdup, because the sole caller
(qdev_get_fw_dev_path_helper) assumes it gets non-NULL, and dereferences
it.  Besides, in that caller, the allocated buffer is already freed with
g_free, so it's better to allocate with a matching g_strdup.
In one case, (scsi-bus.c) it was trivial, so I replaced an snprintf+
g_strdup combination with an equivalent g_strdup_printf use.
Signed-off-by: Jim Meyering <meyering@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
											
										 
											2012-10-04 13:09:44 +02:00
										 |  |  |     return g_strdup_printf("channel@%x/%s@%x,%x", d->channel, | 
					
						
							|  |  |  |                            qdev_fw_name(dev), d->id, d->lun); | 
					
						
							| 
									
										
										
										
											2011-07-28 18:02:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-27 23:24:50 +02:00
										 |  |  | SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun) | 
					
						
							| 
									
										
										
										
											2011-07-28 18:02:13 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-23 15:34:39 -06:00
										 |  |  |     BusChild *kid; | 
					
						
							| 
									
										
										
										
											2011-07-28 18:02:13 +02:00
										 |  |  |     SCSIDevice *target_dev = NULL; | 
					
						
							| 
									
										
										
										
											2010-12-08 13:35:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-23 15:34:39 -06:00
										 |  |  |     QTAILQ_FOREACH_REVERSE(kid, &bus->qbus.children, ChildrenHead, sibling) { | 
					
						
							|  |  |  |         DeviceState *qdev = kid->child; | 
					
						
							| 
									
										
										
										
											2011-12-15 14:50:08 -06:00
										 |  |  |         SCSIDevice *dev = SCSI_DEVICE(qdev); | 
					
						
							| 
									
										
										
										
											2010-12-08 13:35:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-27 23:24:50 +02:00
										 |  |  |         if (dev->channel == channel && dev->id == id) { | 
					
						
							| 
									
										
										
										
											2011-07-28 18:02:13 +02:00
										 |  |  |             if (dev->lun == lun) { | 
					
						
							|  |  |  |                 return dev; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             target_dev = dev; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return target_dev; | 
					
						
							| 
									
										
										
										
											2010-12-08 13:35:04 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-12-15 14:50:08 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-02 16:27:02 +01:00
										 |  |  | /* SCSI request list.  For simplicity, pv points to the whole device */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-19 11:00:50 -08:00
										 |  |  | static int put_scsi_requests(QEMUFile *f, void *pv, size_t size, | 
					
						
							| 
									
										
										
										
											2018-11-14 17:29:30 +04:00
										 |  |  |                              const VMStateField *field, QJSON *vmdesc) | 
					
						
							| 
									
										
										
										
											2011-12-02 16:27:02 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     SCSIDevice *s = pv; | 
					
						
							|  |  |  |     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus); | 
					
						
							|  |  |  |     SCSIRequest *req; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QTAILQ_FOREACH(req, &s->requests, next) { | 
					
						
							|  |  |  |         assert(!req->io_canceled); | 
					
						
							|  |  |  |         assert(req->status == -1); | 
					
						
							|  |  |  |         assert(req->enqueued); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-16 14:20:03 +02:00
										 |  |  |         qemu_put_sbyte(f, req->retry ? 1 : 2); | 
					
						
							| 
									
										
										
										
											2011-12-02 16:27:02 +01:00
										 |  |  |         qemu_put_buffer(f, req->cmd.buf, sizeof(req->cmd.buf)); | 
					
						
							|  |  |  |         qemu_put_be32s(f, &req->tag); | 
					
						
							|  |  |  |         qemu_put_be32s(f, &req->lun); | 
					
						
							|  |  |  |         if (bus->info->save_request) { | 
					
						
							|  |  |  |             bus->info->save_request(f, req); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (req->ops->save_request) { | 
					
						
							|  |  |  |             req->ops->save_request(f, req); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     qemu_put_sbyte(f, 0); | 
					
						
							| 
									
										
										
										
											2017-01-19 11:00:50 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2011-12-02 16:27:02 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-19 11:00:50 -08:00
										 |  |  | static int get_scsi_requests(QEMUFile *f, void *pv, size_t size, | 
					
						
							| 
									
										
										
										
											2018-11-14 17:29:30 +04:00
										 |  |  |                              const VMStateField *field) | 
					
						
							| 
									
										
										
										
											2011-12-02 16:27:02 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     SCSIDevice *s = pv; | 
					
						
							|  |  |  |     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus); | 
					
						
							| 
									
										
										
										
											2012-05-16 14:20:03 +02:00
										 |  |  |     int8_t sbyte; | 
					
						
							| 
									
										
										
										
											2011-12-02 16:27:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-16 14:20:03 +02:00
										 |  |  |     while ((sbyte = qemu_get_sbyte(f)) > 0) { | 
					
						
							| 
									
										
										
										
											2011-12-02 16:27:02 +01:00
										 |  |  |         uint8_t buf[SCSI_CMD_BUF_SIZE]; | 
					
						
							|  |  |  |         uint32_t tag; | 
					
						
							|  |  |  |         uint32_t lun; | 
					
						
							|  |  |  |         SCSIRequest *req; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         qemu_get_buffer(f, buf, sizeof(buf)); | 
					
						
							|  |  |  |         qemu_get_be32s(f, &tag); | 
					
						
							|  |  |  |         qemu_get_be32s(f, &lun); | 
					
						
							|  |  |  |         req = scsi_req_new(s, tag, lun, buf, NULL); | 
					
						
							| 
									
										
										
										
											2012-05-16 14:20:03 +02:00
										 |  |  |         req->retry = (sbyte == 1); | 
					
						
							| 
									
										
										
										
											2011-12-02 16:27:02 +01:00
										 |  |  |         if (bus->info->load_request) { | 
					
						
							|  |  |  |             req->hba_private = bus->info->load_request(f, req); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (req->ops->load_request) { | 
					
						
							|  |  |  |             req->ops->load_request(f, req); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* Just restart it later.  */ | 
					
						
							|  |  |  |         scsi_req_enqueue_internal(req); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* At this point, the request will be kept alive by the reference
 | 
					
						
							|  |  |  |          * added by scsi_req_enqueue_internal, so we can release our reference. | 
					
						
							|  |  |  |          * The HBA of course will add its own reference in the load_request | 
					
						
							|  |  |  |          * callback if it needs to hold on the SCSIRequest. | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         scsi_req_unref(req); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-21 12:03:10 +02:00
										 |  |  | static const VMStateInfo vmstate_info_scsi_requests = { | 
					
						
							| 
									
										
										
										
											2011-12-02 16:27:02 +01:00
										 |  |  |     .name = "scsi-requests", | 
					
						
							|  |  |  |     .get  = get_scsi_requests, | 
					
						
							|  |  |  |     .put  = put_scsi_requests, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-06 16:26:02 +08:00
										 |  |  | static bool scsi_sense_state_needed(void *opaque) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SCSIDevice *s = opaque; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return s->sense_len > SCSI_SENSE_BUF_SIZE_OLD; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const VMStateDescription vmstate_scsi_sense_state = { | 
					
						
							|  |  |  |     .name = "SCSIDevice/sense", | 
					
						
							|  |  |  |     .version_id = 1, | 
					
						
							|  |  |  |     .minimum_version_id = 1, | 
					
						
							| 
									
										
										
										
											2014-09-23 14:09:54 +02:00
										 |  |  |     .needed = scsi_sense_state_needed, | 
					
						
							| 
									
										
										
										
											2014-04-16 15:32:32 +02:00
										 |  |  |     .fields = (VMStateField[]) { | 
					
						
							| 
									
										
										
										
											2014-03-06 16:26:02 +08:00
										 |  |  |         VMSTATE_UINT8_SUB_ARRAY(sense, SCSIDevice, | 
					
						
							|  |  |  |                                 SCSI_SENSE_BUF_SIZE_OLD, | 
					
						
							|  |  |  |                                 SCSI_SENSE_BUF_SIZE - SCSI_SENSE_BUF_SIZE_OLD), | 
					
						
							|  |  |  |         VMSTATE_END_OF_LIST() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-02 16:27:02 +01:00
										 |  |  | const VMStateDescription vmstate_scsi_device = { | 
					
						
							|  |  |  |     .name = "SCSIDevice", | 
					
						
							|  |  |  |     .version_id = 1, | 
					
						
							|  |  |  |     .minimum_version_id = 1, | 
					
						
							|  |  |  |     .fields = (VMStateField[]) { | 
					
						
							|  |  |  |         VMSTATE_UINT8(unit_attention.key, SCSIDevice), | 
					
						
							|  |  |  |         VMSTATE_UINT8(unit_attention.asc, SCSIDevice), | 
					
						
							|  |  |  |         VMSTATE_UINT8(unit_attention.ascq, SCSIDevice), | 
					
						
							|  |  |  |         VMSTATE_BOOL(sense_is_ua, SCSIDevice), | 
					
						
							| 
									
										
										
										
											2014-03-06 16:26:02 +08:00
										 |  |  |         VMSTATE_UINT8_SUB_ARRAY(sense, SCSIDevice, 0, SCSI_SENSE_BUF_SIZE_OLD), | 
					
						
							| 
									
										
										
										
											2011-12-02 16:27:02 +01:00
										 |  |  |         VMSTATE_UINT32(sense_len, SCSIDevice), | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             .name         = "requests", | 
					
						
							|  |  |  |             .version_id   = 0, | 
					
						
							|  |  |  |             .field_exists = NULL, | 
					
						
							|  |  |  |             .size         = 0,   /* ouch */ | 
					
						
							|  |  |  |             .info         = &vmstate_info_scsi_requests, | 
					
						
							|  |  |  |             .flags        = VMS_SINGLE, | 
					
						
							|  |  |  |             .offset       = 0, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         VMSTATE_END_OF_LIST() | 
					
						
							| 
									
										
										
										
											2014-03-06 16:26:02 +08:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2014-09-23 14:09:54 +02:00
										 |  |  |     .subsections = (const VMStateDescription*[]) { | 
					
						
							|  |  |  |         &vmstate_scsi_sense_state, | 
					
						
							|  |  |  |         NULL | 
					
						
							| 
									
										
										
										
											2011-12-02 16:27:02 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-07 21:34:16 -06:00
										 |  |  | static void scsi_device_class_init(ObjectClass *klass, void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     DeviceClass *k = DEVICE_CLASS(klass); | 
					
						
							| 
									
										
										
										
											2013-07-29 17:17:45 +03:00
										 |  |  |     set_bit(DEVICE_CATEGORY_STORAGE, k->categories); | 
					
						
							| 
									
										
										
										
											2014-08-12 10:12:55 +08:00
										 |  |  |     k->bus_type  = TYPE_SCSI_BUS; | 
					
						
							|  |  |  |     k->realize   = scsi_qdev_realize; | 
					
						
							|  |  |  |     k->unrealize = scsi_qdev_unrealize; | 
					
						
							|  |  |  |     k->props     = scsi_props; | 
					
						
							| 
									
										
										
										
											2011-12-07 21:34:16 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-07 16:00:28 +08:00
										 |  |  | static void scsi_dev_instance_init(Object *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     DeviceState *dev = DEVICE(obj); | 
					
						
							| 
									
										
										
										
											2016-01-06 17:37:46 +08:00
										 |  |  |     SCSIDevice *s = SCSI_DEVICE(dev); | 
					
						
							| 
									
										
										
										
											2014-10-07 16:00:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     device_add_bootindex_property(obj, &s->conf.bootindex, | 
					
						
							|  |  |  |                                   "bootindex", NULL, | 
					
						
							|  |  |  |                                   &s->qdev, NULL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-10 16:19:07 +01:00
										 |  |  | static const TypeInfo scsi_device_type_info = { | 
					
						
							| 
									
										
										
										
											2011-12-15 14:50:08 -06:00
										 |  |  |     .name = TYPE_SCSI_DEVICE, | 
					
						
							|  |  |  |     .parent = TYPE_DEVICE, | 
					
						
							|  |  |  |     .instance_size = sizeof(SCSIDevice), | 
					
						
							|  |  |  |     .abstract = true, | 
					
						
							|  |  |  |     .class_size = sizeof(SCSIDeviceClass), | 
					
						
							| 
									
										
										
										
											2011-12-07 21:34:16 -06:00
										 |  |  |     .class_init = scsi_device_class_init, | 
					
						
							| 
									
										
										
										
											2014-10-07 16:00:28 +08:00
										 |  |  |     .instance_init = scsi_dev_instance_init, | 
					
						
							| 
									
										
										
										
											2011-12-15 14:50:08 -06:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-09 15:20:55 +01:00
										 |  |  | static void scsi_register_types(void) | 
					
						
							| 
									
										
										
										
											2011-12-15 14:50:08 -06:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-05-02 09:00:20 +02:00
										 |  |  |     type_register_static(&scsi_bus_info); | 
					
						
							| 
									
										
										
										
											2011-12-15 14:50:08 -06:00
										 |  |  |     type_register_static(&scsi_device_type_info); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-09 15:20:55 +01:00
										 |  |  | type_init(scsi_register_types) |