Statistics
| Branch: | Revision:

root / hw / scsi-bus.c @ fc4f0754

History | View | Annotate | Download (19.5 kB)

1 d52affa7 Gerd Hoffmann
#include "hw.h"
2 2f792016 Markus Armbruster
#include "qemu-error.h"
3 43b443b6 Gerd Hoffmann
#include "scsi.h"
4 2ec749cb Gerd Hoffmann
#include "scsi-defs.h"
5 d52affa7 Gerd Hoffmann
#include "qdev.h"
6 2446333c Blue Swirl
#include "blockdev.h"
7 5138efec Paolo Bonzini
#include "trace.h"
8 d52affa7 Gerd Hoffmann
9 db07c0f8 Gleb Natapov
static char *scsibus_get_fw_dev_path(DeviceState *dev);
10 db07c0f8 Gleb Natapov
11 d52affa7 Gerd Hoffmann
static struct BusInfo scsi_bus_info = {
12 d52affa7 Gerd Hoffmann
    .name  = "SCSI",
13 d52affa7 Gerd Hoffmann
    .size  = sizeof(SCSIBus),
14 db07c0f8 Gleb Natapov
    .get_fw_dev_path = scsibus_get_fw_dev_path,
15 d52affa7 Gerd Hoffmann
    .props = (Property[]) {
16 d52affa7 Gerd Hoffmann
        DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1),
17 d52affa7 Gerd Hoffmann
        DEFINE_PROP_END_OF_LIST(),
18 d52affa7 Gerd Hoffmann
    },
19 d52affa7 Gerd Hoffmann
};
20 d52affa7 Gerd Hoffmann
static int next_scsi_bus;
21 d52affa7 Gerd Hoffmann
22 d52affa7 Gerd Hoffmann
/* Create a scsi bus, and attach devices to it.  */
23 ca9c39fa Gerd Hoffmann
void scsi_bus_new(SCSIBus *bus, DeviceState *host, int tcq, int ndev,
24 cfdc1bb0 Paolo Bonzini
                  const SCSIBusOps *ops)
25 d52affa7 Gerd Hoffmann
{
26 ca9c39fa Gerd Hoffmann
    qbus_create_inplace(&bus->qbus, &scsi_bus_info, host, NULL);
27 d52affa7 Gerd Hoffmann
    bus->busnr = next_scsi_bus++;
28 d52affa7 Gerd Hoffmann
    bus->tcq = tcq;
29 d52affa7 Gerd Hoffmann
    bus->ndev = ndev;
30 cfdc1bb0 Paolo Bonzini
    bus->ops = ops;
31 cb23117b Gerd Hoffmann
    bus->qbus.allow_hotplug = 1;
32 d52affa7 Gerd Hoffmann
}
33 d52affa7 Gerd Hoffmann
34 d52affa7 Gerd Hoffmann
static int scsi_qdev_init(DeviceState *qdev, DeviceInfo *base)
35 d52affa7 Gerd Hoffmann
{
36 d52affa7 Gerd Hoffmann
    SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
37 d52affa7 Gerd Hoffmann
    SCSIDeviceInfo *info = DO_UPCAST(SCSIDeviceInfo, qdev, base);
38 d52affa7 Gerd Hoffmann
    SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
39 01985dcf Gerd Hoffmann
    int rc = -1;
40 d52affa7 Gerd Hoffmann
41 d52affa7 Gerd Hoffmann
    if (dev->id == -1) {
42 d52affa7 Gerd Hoffmann
        for (dev->id = 0; dev->id < bus->ndev; dev->id++) {
43 d52affa7 Gerd Hoffmann
            if (bus->devs[dev->id] == NULL)
44 d52affa7 Gerd Hoffmann
                break;
45 d52affa7 Gerd Hoffmann
        }
46 d52affa7 Gerd Hoffmann
    }
47 d52affa7 Gerd Hoffmann
    if (dev->id >= bus->ndev) {
48 1ecda02b Markus Armbruster
        error_report("bad scsi device id: %d", dev->id);
49 d52affa7 Gerd Hoffmann
        goto err;
50 d52affa7 Gerd Hoffmann
    }
51 d52affa7 Gerd Hoffmann
52 d52affa7 Gerd Hoffmann
    if (bus->devs[dev->id]) {
53 01985dcf Gerd Hoffmann
        qdev_free(&bus->devs[dev->id]->qdev);
54 d52affa7 Gerd Hoffmann
    }
55 d52affa7 Gerd Hoffmann
    bus->devs[dev->id] = dev;
56 d52affa7 Gerd Hoffmann
57 d52affa7 Gerd Hoffmann
    dev->info = info;
58 9af99d98 Gerd Hoffmann
    QTAILQ_INIT(&dev->requests);
59 01985dcf Gerd Hoffmann
    rc = dev->info->init(dev);
60 01985dcf Gerd Hoffmann
    if (rc != 0) {
61 01985dcf Gerd Hoffmann
        bus->devs[dev->id] = NULL;
62 01985dcf Gerd Hoffmann
    }
63 d52affa7 Gerd Hoffmann
64 d52affa7 Gerd Hoffmann
err:
65 01985dcf Gerd Hoffmann
    return rc;
66 01985dcf Gerd Hoffmann
}
67 01985dcf Gerd Hoffmann
68 01985dcf Gerd Hoffmann
static int scsi_qdev_exit(DeviceState *qdev)
69 01985dcf Gerd Hoffmann
{
70 01985dcf Gerd Hoffmann
    SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
71 01985dcf Gerd Hoffmann
    SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
72 01985dcf Gerd Hoffmann
73 01985dcf Gerd Hoffmann
    assert(bus->devs[dev->id] != NULL);
74 01985dcf Gerd Hoffmann
    if (bus->devs[dev->id]->info->destroy) {
75 01985dcf Gerd Hoffmann
        bus->devs[dev->id]->info->destroy(bus->devs[dev->id]);
76 01985dcf Gerd Hoffmann
    }
77 01985dcf Gerd Hoffmann
    bus->devs[dev->id] = NULL;
78 01985dcf Gerd Hoffmann
    return 0;
79 d52affa7 Gerd Hoffmann
}
80 d52affa7 Gerd Hoffmann
81 d52affa7 Gerd Hoffmann
void scsi_qdev_register(SCSIDeviceInfo *info)
82 d52affa7 Gerd Hoffmann
{
83 d52affa7 Gerd Hoffmann
    info->qdev.bus_info = &scsi_bus_info;
84 d52affa7 Gerd Hoffmann
    info->qdev.init     = scsi_qdev_init;
85 cb23117b Gerd Hoffmann
    info->qdev.unplug   = qdev_simple_unplug_cb;
86 01985dcf Gerd Hoffmann
    info->qdev.exit     = scsi_qdev_exit;
87 d52affa7 Gerd Hoffmann
    qdev_register(&info->qdev);
88 d52affa7 Gerd Hoffmann
}
89 d52affa7 Gerd Hoffmann
90 d52affa7 Gerd Hoffmann
/* handle legacy '-drive if=scsi,...' cmd line args */
91 2d1fd261 Stefan Hajnoczi
SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
92 2d1fd261 Stefan Hajnoczi
                                      int unit, bool removable)
93 d52affa7 Gerd Hoffmann
{
94 d52affa7 Gerd Hoffmann
    const char *driver;
95 d52affa7 Gerd Hoffmann
    DeviceState *dev;
96 d52affa7 Gerd Hoffmann
97 f8b6cc00 Markus Armbruster
    driver = bdrv_is_sg(bdrv) ? "scsi-generic" : "scsi-disk";
98 d52affa7 Gerd Hoffmann
    dev = qdev_create(&bus->qbus, driver);
99 d52affa7 Gerd Hoffmann
    qdev_prop_set_uint32(dev, "scsi-id", unit);
100 2d1fd261 Stefan Hajnoczi
    if (qdev_prop_exists(dev, "removable")) {
101 2d1fd261 Stefan Hajnoczi
        qdev_prop_set_bit(dev, "removable", removable);
102 2d1fd261 Stefan Hajnoczi
    }
103 18846dee Markus Armbruster
    if (qdev_prop_set_drive(dev, "drive", bdrv) < 0) {
104 18846dee Markus Armbruster
        qdev_free(dev);
105 18846dee Markus Armbruster
        return NULL;
106 18846dee Markus Armbruster
    }
107 33e66b86 Markus Armbruster
    if (qdev_init(dev) < 0)
108 33e66b86 Markus Armbruster
        return NULL;
109 d52affa7 Gerd Hoffmann
    return DO_UPCAST(SCSIDevice, qdev, dev);
110 d52affa7 Gerd Hoffmann
}
111 d52affa7 Gerd Hoffmann
112 fa66b909 Markus Armbruster
int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
113 d52affa7 Gerd Hoffmann
{
114 42e766a2 Markus Armbruster
    Location loc;
115 d52affa7 Gerd Hoffmann
    DriveInfo *dinfo;
116 fa66b909 Markus Armbruster
    int res = 0, unit;
117 d52affa7 Gerd Hoffmann
118 42e766a2 Markus Armbruster
    loc_push_none(&loc);
119 622b520f Hannes Reinecke
    for (unit = 0; unit < bus->ndev; unit++) {
120 d52affa7 Gerd Hoffmann
        dinfo = drive_get(IF_SCSI, bus->busnr, unit);
121 d52affa7 Gerd Hoffmann
        if (dinfo == NULL) {
122 d52affa7 Gerd Hoffmann
            continue;
123 d52affa7 Gerd Hoffmann
        }
124 42e766a2 Markus Armbruster
        qemu_opts_loc_restore(dinfo->opts);
125 2d1fd261 Stefan Hajnoczi
        if (!scsi_bus_legacy_add_drive(bus, dinfo->bdrv, unit, false)) {
126 fa66b909 Markus Armbruster
            res = -1;
127 fa66b909 Markus Armbruster
            break;
128 fa66b909 Markus Armbruster
        }
129 d52affa7 Gerd Hoffmann
    }
130 42e766a2 Markus Armbruster
    loc_pop(&loc);
131 fa66b909 Markus Armbruster
    return res;
132 d52affa7 Gerd Hoffmann
}
133 89b08ae1 Gerd Hoffmann
134 89b08ae1 Gerd Hoffmann
SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t lun)
135 89b08ae1 Gerd Hoffmann
{
136 89b08ae1 Gerd Hoffmann
    SCSIRequest *req;
137 89b08ae1 Gerd Hoffmann
138 89b08ae1 Gerd Hoffmann
    req = qemu_mallocz(size);
139 5c6c0e51 Hannes Reinecke
    req->refcount = 1;
140 89b08ae1 Gerd Hoffmann
    req->bus = scsi_bus_from_device(d);
141 89b08ae1 Gerd Hoffmann
    req->dev = d;
142 89b08ae1 Gerd Hoffmann
    req->tag = tag;
143 89b08ae1 Gerd Hoffmann
    req->lun = lun;
144 ed3a34a3 Gerd Hoffmann
    req->status = -1;
145 5138efec Paolo Bonzini
    trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
146 89b08ae1 Gerd Hoffmann
    return req;
147 89b08ae1 Gerd Hoffmann
}
148 89b08ae1 Gerd Hoffmann
149 fc4f0754 Paolo Bonzini
int32_t scsi_req_enqueue(SCSIRequest *req, uint8_t *buf)
150 89b08ae1 Gerd Hoffmann
{
151 fc4f0754 Paolo Bonzini
    int32_t rc;
152 fc4f0754 Paolo Bonzini
153 5c6c0e51 Hannes Reinecke
    assert(!req->enqueued);
154 5c6c0e51 Hannes Reinecke
    scsi_req_ref(req);
155 5c6c0e51 Hannes Reinecke
    req->enqueued = true;
156 5c6c0e51 Hannes Reinecke
    QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
157 fc4f0754 Paolo Bonzini
158 fc4f0754 Paolo Bonzini
    scsi_req_ref(req);
159 fc4f0754 Paolo Bonzini
    rc = req->dev->info->send_command(req, buf);
160 fc4f0754 Paolo Bonzini
    scsi_req_unref(req);
161 fc4f0754 Paolo Bonzini
    return rc;
162 89b08ae1 Gerd Hoffmann
}
163 89b08ae1 Gerd Hoffmann
164 a1f0cce2 Hannes Reinecke
static void scsi_req_dequeue(SCSIRequest *req)
165 e8637c90 Jan Kiszka
{
166 5138efec Paolo Bonzini
    trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
167 e8637c90 Jan Kiszka
    if (req->enqueued) {
168 e8637c90 Jan Kiszka
        QTAILQ_REMOVE(&req->dev->requests, req, next);
169 e8637c90 Jan Kiszka
        req->enqueued = false;
170 ad2d30f7 Paolo Bonzini
        scsi_req_unref(req);
171 e8637c90 Jan Kiszka
    }
172 e8637c90 Jan Kiszka
}
173 e8637c90 Jan Kiszka
174 2ec749cb Gerd Hoffmann
static int scsi_req_length(SCSIRequest *req, uint8_t *cmd)
175 2ec749cb Gerd Hoffmann
{
176 2ec749cb Gerd Hoffmann
    switch (cmd[0] >> 5) {
177 2ec749cb Gerd Hoffmann
    case 0:
178 2ec749cb Gerd Hoffmann
        req->cmd.xfer = cmd[4];
179 2ec749cb Gerd Hoffmann
        req->cmd.len = 6;
180 2ec749cb Gerd Hoffmann
        /* length 0 means 256 blocks */
181 2ec749cb Gerd Hoffmann
        if (req->cmd.xfer == 0)
182 2ec749cb Gerd Hoffmann
            req->cmd.xfer = 256;
183 2ec749cb Gerd Hoffmann
        break;
184 2ec749cb Gerd Hoffmann
    case 1:
185 2ec749cb Gerd Hoffmann
    case 2:
186 2ec749cb Gerd Hoffmann
        req->cmd.xfer = cmd[8] | (cmd[7] << 8);
187 2ec749cb Gerd Hoffmann
        req->cmd.len = 10;
188 2ec749cb Gerd Hoffmann
        break;
189 2ec749cb Gerd Hoffmann
    case 4:
190 2ec749cb Gerd Hoffmann
        req->cmd.xfer = cmd[13] | (cmd[12] << 8) | (cmd[11] << 16) | (cmd[10] << 24);
191 2ec749cb Gerd Hoffmann
        req->cmd.len = 16;
192 2ec749cb Gerd Hoffmann
        break;
193 2ec749cb Gerd Hoffmann
    case 5:
194 2ec749cb Gerd Hoffmann
        req->cmd.xfer = cmd[9] | (cmd[8] << 8) | (cmd[7] << 16) | (cmd[6] << 24);
195 2ec749cb Gerd Hoffmann
        req->cmd.len = 12;
196 2ec749cb Gerd Hoffmann
        break;
197 2ec749cb Gerd Hoffmann
    default:
198 5138efec Paolo Bonzini
        trace_scsi_req_parse_bad(req->dev->id, req->lun, req->tag, cmd[0]);
199 2ec749cb Gerd Hoffmann
        return -1;
200 2ec749cb Gerd Hoffmann
    }
201 2ec749cb Gerd Hoffmann
202 2ec749cb Gerd Hoffmann
    switch(cmd[0]) {
203 2ec749cb Gerd Hoffmann
    case TEST_UNIT_READY:
204 2ec749cb Gerd Hoffmann
    case REZERO_UNIT:
205 2ec749cb Gerd Hoffmann
    case START_STOP:
206 2ec749cb Gerd Hoffmann
    case SEEK_6:
207 2ec749cb Gerd Hoffmann
    case WRITE_FILEMARKS:
208 2ec749cb Gerd Hoffmann
    case SPACE:
209 a5e3d9ef Bernhard Kohl
    case RESERVE:
210 a5e3d9ef Bernhard Kohl
    case RELEASE:
211 2ec749cb Gerd Hoffmann
    case ERASE:
212 2ec749cb Gerd Hoffmann
    case ALLOW_MEDIUM_REMOVAL:
213 2ec749cb Gerd Hoffmann
    case VERIFY:
214 2ec749cb Gerd Hoffmann
    case SEEK_10:
215 2ec749cb Gerd Hoffmann
    case SYNCHRONIZE_CACHE:
216 2ec749cb Gerd Hoffmann
    case LOCK_UNLOCK_CACHE:
217 2ec749cb Gerd Hoffmann
    case LOAD_UNLOAD:
218 2ec749cb Gerd Hoffmann
    case SET_CD_SPEED:
219 2ec749cb Gerd Hoffmann
    case SET_LIMITS:
220 2ec749cb Gerd Hoffmann
    case WRITE_LONG:
221 2ec749cb Gerd Hoffmann
    case MOVE_MEDIUM:
222 2ec749cb Gerd Hoffmann
    case UPDATE_BLOCK:
223 2ec749cb Gerd Hoffmann
        req->cmd.xfer = 0;
224 2ec749cb Gerd Hoffmann
        break;
225 2ec749cb Gerd Hoffmann
    case MODE_SENSE:
226 2ec749cb Gerd Hoffmann
        break;
227 2ec749cb Gerd Hoffmann
    case WRITE_SAME:
228 2ec749cb Gerd Hoffmann
        req->cmd.xfer = 1;
229 2ec749cb Gerd Hoffmann
        break;
230 2ec749cb Gerd Hoffmann
    case READ_CAPACITY:
231 2ec749cb Gerd Hoffmann
        req->cmd.xfer = 8;
232 2ec749cb Gerd Hoffmann
        break;
233 2ec749cb Gerd Hoffmann
    case READ_BLOCK_LIMITS:
234 2ec749cb Gerd Hoffmann
        req->cmd.xfer = 6;
235 2ec749cb Gerd Hoffmann
        break;
236 2ec749cb Gerd Hoffmann
    case READ_POSITION:
237 2ec749cb Gerd Hoffmann
        req->cmd.xfer = 20;
238 2ec749cb Gerd Hoffmann
        break;
239 2ec749cb Gerd Hoffmann
    case SEND_VOLUME_TAG:
240 2ec749cb Gerd Hoffmann
        req->cmd.xfer *= 40;
241 2ec749cb Gerd Hoffmann
        break;
242 2ec749cb Gerd Hoffmann
    case MEDIUM_SCAN:
243 2ec749cb Gerd Hoffmann
        req->cmd.xfer *= 8;
244 2ec749cb Gerd Hoffmann
        break;
245 2ec749cb Gerd Hoffmann
    case WRITE_10:
246 2ec749cb Gerd Hoffmann
    case WRITE_VERIFY:
247 2ec749cb Gerd Hoffmann
    case WRITE_6:
248 2ec749cb Gerd Hoffmann
    case WRITE_12:
249 2ec749cb Gerd Hoffmann
    case WRITE_VERIFY_12:
250 bd536cf3 Gerd Hoffmann
    case WRITE_16:
251 bd536cf3 Gerd Hoffmann
    case WRITE_VERIFY_16:
252 2ec749cb Gerd Hoffmann
        req->cmd.xfer *= req->dev->blocksize;
253 2ec749cb Gerd Hoffmann
        break;
254 2ec749cb Gerd Hoffmann
    case READ_10:
255 2ec749cb Gerd Hoffmann
    case READ_6:
256 2ec749cb Gerd Hoffmann
    case READ_REVERSE:
257 2ec749cb Gerd Hoffmann
    case RECOVER_BUFFERED_DATA:
258 2ec749cb Gerd Hoffmann
    case READ_12:
259 bd536cf3 Gerd Hoffmann
    case READ_16:
260 2ec749cb Gerd Hoffmann
        req->cmd.xfer *= req->dev->blocksize;
261 2ec749cb Gerd Hoffmann
        break;
262 2ec749cb Gerd Hoffmann
    case INQUIRY:
263 2ec749cb Gerd Hoffmann
        req->cmd.xfer = cmd[4] | (cmd[3] << 8);
264 2ec749cb Gerd Hoffmann
        break;
265 c7126d5b Nicholas Bellinger
    case MAINTENANCE_OUT:
266 c7126d5b Nicholas Bellinger
    case MAINTENANCE_IN:
267 c7126d5b Nicholas Bellinger
        if (req->dev->type == TYPE_ROM) {
268 c7126d5b Nicholas Bellinger
            /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
269 c7126d5b Nicholas Bellinger
            req->cmd.xfer = cmd[9] | (cmd[8] << 8);
270 c7126d5b Nicholas Bellinger
        }
271 c7126d5b Nicholas Bellinger
        break;
272 2ec749cb Gerd Hoffmann
    }
273 2ec749cb Gerd Hoffmann
    return 0;
274 2ec749cb Gerd Hoffmann
}
275 2ec749cb Gerd Hoffmann
276 2ec749cb Gerd Hoffmann
static int scsi_req_stream_length(SCSIRequest *req, uint8_t *cmd)
277 2ec749cb Gerd Hoffmann
{
278 2ec749cb Gerd Hoffmann
    switch(cmd[0]) {
279 2ec749cb Gerd Hoffmann
    /* stream commands */
280 2ec749cb Gerd Hoffmann
    case READ_6:
281 2ec749cb Gerd Hoffmann
    case READ_REVERSE:
282 2ec749cb Gerd Hoffmann
    case RECOVER_BUFFERED_DATA:
283 2ec749cb Gerd Hoffmann
    case WRITE_6:
284 2ec749cb Gerd Hoffmann
        req->cmd.len = 6;
285 2ec749cb Gerd Hoffmann
        req->cmd.xfer = cmd[4] | (cmd[3] << 8) | (cmd[2] << 16);
286 2ec749cb Gerd Hoffmann
        if (cmd[1] & 0x01) /* fixed */
287 2ec749cb Gerd Hoffmann
            req->cmd.xfer *= req->dev->blocksize;
288 2ec749cb Gerd Hoffmann
        break;
289 2ec749cb Gerd Hoffmann
    case REWIND:
290 2ec749cb Gerd Hoffmann
    case START_STOP:
291 2ec749cb Gerd Hoffmann
        req->cmd.len = 6;
292 2ec749cb Gerd Hoffmann
        req->cmd.xfer = 0;
293 2ec749cb Gerd Hoffmann
        break;
294 2ec749cb Gerd Hoffmann
    /* generic commands */
295 2ec749cb Gerd Hoffmann
    default:
296 2ec749cb Gerd Hoffmann
        return scsi_req_length(req, cmd);
297 2ec749cb Gerd Hoffmann
    }
298 2ec749cb Gerd Hoffmann
    return 0;
299 2ec749cb Gerd Hoffmann
}
300 2ec749cb Gerd Hoffmann
301 97a06435 Gerd Hoffmann
static void scsi_req_xfer_mode(SCSIRequest *req)
302 97a06435 Gerd Hoffmann
{
303 97a06435 Gerd Hoffmann
    switch (req->cmd.buf[0]) {
304 97a06435 Gerd Hoffmann
    case WRITE_6:
305 97a06435 Gerd Hoffmann
    case WRITE_10:
306 97a06435 Gerd Hoffmann
    case WRITE_VERIFY:
307 97a06435 Gerd Hoffmann
    case WRITE_12:
308 97a06435 Gerd Hoffmann
    case WRITE_VERIFY_12:
309 bd536cf3 Gerd Hoffmann
    case WRITE_16:
310 bd536cf3 Gerd Hoffmann
    case WRITE_VERIFY_16:
311 97a06435 Gerd Hoffmann
    case COPY:
312 97a06435 Gerd Hoffmann
    case COPY_VERIFY:
313 97a06435 Gerd Hoffmann
    case COMPARE:
314 97a06435 Gerd Hoffmann
    case CHANGE_DEFINITION:
315 97a06435 Gerd Hoffmann
    case LOG_SELECT:
316 97a06435 Gerd Hoffmann
    case MODE_SELECT:
317 97a06435 Gerd Hoffmann
    case MODE_SELECT_10:
318 97a06435 Gerd Hoffmann
    case SEND_DIAGNOSTIC:
319 97a06435 Gerd Hoffmann
    case WRITE_BUFFER:
320 97a06435 Gerd Hoffmann
    case FORMAT_UNIT:
321 97a06435 Gerd Hoffmann
    case REASSIGN_BLOCKS:
322 97a06435 Gerd Hoffmann
    case SEARCH_EQUAL:
323 97a06435 Gerd Hoffmann
    case SEARCH_HIGH:
324 97a06435 Gerd Hoffmann
    case SEARCH_LOW:
325 97a06435 Gerd Hoffmann
    case UPDATE_BLOCK:
326 97a06435 Gerd Hoffmann
    case WRITE_LONG:
327 97a06435 Gerd Hoffmann
    case WRITE_SAME:
328 97a06435 Gerd Hoffmann
    case SEARCH_HIGH_12:
329 97a06435 Gerd Hoffmann
    case SEARCH_EQUAL_12:
330 97a06435 Gerd Hoffmann
    case SEARCH_LOW_12:
331 97a06435 Gerd Hoffmann
    case SET_WINDOW:
332 97a06435 Gerd Hoffmann
    case MEDIUM_SCAN:
333 97a06435 Gerd Hoffmann
    case SEND_VOLUME_TAG:
334 97a06435 Gerd Hoffmann
    case WRITE_LONG_2:
335 01bedeba Nicholas Bellinger
    case PERSISTENT_RESERVE_OUT:
336 c7126d5b Nicholas Bellinger
    case MAINTENANCE_OUT:
337 97a06435 Gerd Hoffmann
        req->cmd.mode = SCSI_XFER_TO_DEV;
338 97a06435 Gerd Hoffmann
        break;
339 97a06435 Gerd Hoffmann
    default:
340 97a06435 Gerd Hoffmann
        if (req->cmd.xfer)
341 97a06435 Gerd Hoffmann
            req->cmd.mode = SCSI_XFER_FROM_DEV;
342 97a06435 Gerd Hoffmann
        else {
343 97a06435 Gerd Hoffmann
            req->cmd.mode = SCSI_XFER_NONE;
344 97a06435 Gerd Hoffmann
        }
345 97a06435 Gerd Hoffmann
        break;
346 97a06435 Gerd Hoffmann
    }
347 97a06435 Gerd Hoffmann
}
348 97a06435 Gerd Hoffmann
349 2ec749cb Gerd Hoffmann
static uint64_t scsi_req_lba(SCSIRequest *req)
350 2ec749cb Gerd Hoffmann
{
351 2ec749cb Gerd Hoffmann
    uint8_t *buf = req->cmd.buf;
352 2ec749cb Gerd Hoffmann
    uint64_t lba;
353 2ec749cb Gerd Hoffmann
354 2ec749cb Gerd Hoffmann
    switch (buf[0] >> 5) {
355 2ec749cb Gerd Hoffmann
    case 0:
356 2ec749cb Gerd Hoffmann
        lba = (uint64_t) buf[3] | ((uint64_t) buf[2] << 8) |
357 2ec749cb Gerd Hoffmann
              (((uint64_t) buf[1] & 0x1f) << 16);
358 2ec749cb Gerd Hoffmann
        break;
359 2ec749cb Gerd Hoffmann
    case 1:
360 2ec749cb Gerd Hoffmann
    case 2:
361 2ec749cb Gerd Hoffmann
        lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
362 2ec749cb Gerd Hoffmann
              ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
363 2ec749cb Gerd Hoffmann
        break;
364 2ec749cb Gerd Hoffmann
    case 4:
365 2ec749cb Gerd Hoffmann
        lba = (uint64_t) buf[9] | ((uint64_t) buf[8] << 8) |
366 2ec749cb Gerd Hoffmann
              ((uint64_t) buf[7] << 16) | ((uint64_t) buf[6] << 24) |
367 2ec749cb Gerd Hoffmann
              ((uint64_t) buf[5] << 32) | ((uint64_t) buf[4] << 40) |
368 2ec749cb Gerd Hoffmann
              ((uint64_t) buf[3] << 48) | ((uint64_t) buf[2] << 56);
369 2ec749cb Gerd Hoffmann
        break;
370 2ec749cb Gerd Hoffmann
    case 5:
371 2ec749cb Gerd Hoffmann
        lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
372 2ec749cb Gerd Hoffmann
              ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
373 2ec749cb Gerd Hoffmann
        break;
374 2ec749cb Gerd Hoffmann
    default:
375 2ec749cb Gerd Hoffmann
        lba = -1;
376 2ec749cb Gerd Hoffmann
377 2ec749cb Gerd Hoffmann
    }
378 2ec749cb Gerd Hoffmann
    return lba;
379 2ec749cb Gerd Hoffmann
}
380 2ec749cb Gerd Hoffmann
381 2ec749cb Gerd Hoffmann
int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
382 2ec749cb Gerd Hoffmann
{
383 2ec749cb Gerd Hoffmann
    int rc;
384 2ec749cb Gerd Hoffmann
385 2ec749cb Gerd Hoffmann
    if (req->dev->type == TYPE_TAPE) {
386 2ec749cb Gerd Hoffmann
        rc = scsi_req_stream_length(req, buf);
387 2ec749cb Gerd Hoffmann
    } else {
388 2ec749cb Gerd Hoffmann
        rc = scsi_req_length(req, buf);
389 2ec749cb Gerd Hoffmann
    }
390 2ec749cb Gerd Hoffmann
    if (rc != 0)
391 2ec749cb Gerd Hoffmann
        return rc;
392 2ec749cb Gerd Hoffmann
393 2ec749cb Gerd Hoffmann
    memcpy(req->cmd.buf, buf, req->cmd.len);
394 97a06435 Gerd Hoffmann
    scsi_req_xfer_mode(req);
395 2ec749cb Gerd Hoffmann
    req->cmd.lba = scsi_req_lba(req);
396 5138efec Paolo Bonzini
    trace_scsi_req_parsed(req->dev->id, req->lun, req->tag, buf[0],
397 5138efec Paolo Bonzini
                          req->cmd.mode, req->cmd.xfer, req->cmd.lba);
398 2ec749cb Gerd Hoffmann
    return 0;
399 2ec749cb Gerd Hoffmann
}
400 ed3a34a3 Gerd Hoffmann
401 a1f0cce2 Hannes Reinecke
/*
402 a1f0cce2 Hannes Reinecke
 * Predefined sense codes
403 a1f0cce2 Hannes Reinecke
 */
404 a1f0cce2 Hannes Reinecke
405 a1f0cce2 Hannes Reinecke
/* No sense data available */
406 a1f0cce2 Hannes Reinecke
const struct SCSISense sense_code_NO_SENSE = {
407 a1f0cce2 Hannes Reinecke
    .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00
408 a1f0cce2 Hannes Reinecke
};
409 a1f0cce2 Hannes Reinecke
410 a1f0cce2 Hannes Reinecke
/* LUN not ready, Manual intervention required */
411 a1f0cce2 Hannes Reinecke
const struct SCSISense sense_code_LUN_NOT_READY = {
412 a1f0cce2 Hannes Reinecke
    .key = NOT_READY, .asc = 0x04, .ascq = 0x03
413 a1f0cce2 Hannes Reinecke
};
414 a1f0cce2 Hannes Reinecke
415 a1f0cce2 Hannes Reinecke
/* LUN not ready, Medium not present */
416 a1f0cce2 Hannes Reinecke
const struct SCSISense sense_code_NO_MEDIUM = {
417 a1f0cce2 Hannes Reinecke
    .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
418 a1f0cce2 Hannes Reinecke
};
419 a1f0cce2 Hannes Reinecke
420 a1f0cce2 Hannes Reinecke
/* Hardware error, internal target failure */
421 a1f0cce2 Hannes Reinecke
const struct SCSISense sense_code_TARGET_FAILURE = {
422 a1f0cce2 Hannes Reinecke
    .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
423 a1f0cce2 Hannes Reinecke
};
424 a1f0cce2 Hannes Reinecke
425 a1f0cce2 Hannes Reinecke
/* Illegal request, invalid command operation code */
426 a1f0cce2 Hannes Reinecke
const struct SCSISense sense_code_INVALID_OPCODE = {
427 a1f0cce2 Hannes Reinecke
    .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00
428 a1f0cce2 Hannes Reinecke
};
429 a1f0cce2 Hannes Reinecke
430 a1f0cce2 Hannes Reinecke
/* Illegal request, LBA out of range */
431 a1f0cce2 Hannes Reinecke
const struct SCSISense sense_code_LBA_OUT_OF_RANGE = {
432 a1f0cce2 Hannes Reinecke
    .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00
433 a1f0cce2 Hannes Reinecke
};
434 a1f0cce2 Hannes Reinecke
435 a1f0cce2 Hannes Reinecke
/* Illegal request, Invalid field in CDB */
436 a1f0cce2 Hannes Reinecke
const struct SCSISense sense_code_INVALID_FIELD = {
437 a1f0cce2 Hannes Reinecke
    .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00
438 a1f0cce2 Hannes Reinecke
};
439 a1f0cce2 Hannes Reinecke
440 a1f0cce2 Hannes Reinecke
/* Illegal request, LUN not supported */
441 a1f0cce2 Hannes Reinecke
const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
442 a1f0cce2 Hannes Reinecke
    .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
443 a1f0cce2 Hannes Reinecke
};
444 a1f0cce2 Hannes Reinecke
445 a1f0cce2 Hannes Reinecke
/* Command aborted, I/O process terminated */
446 a1f0cce2 Hannes Reinecke
const struct SCSISense sense_code_IO_ERROR = {
447 a1f0cce2 Hannes Reinecke
    .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
448 a1f0cce2 Hannes Reinecke
};
449 a1f0cce2 Hannes Reinecke
450 a1f0cce2 Hannes Reinecke
/* Command aborted, I_T Nexus loss occurred */
451 a1f0cce2 Hannes Reinecke
const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
452 a1f0cce2 Hannes Reinecke
    .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
453 a1f0cce2 Hannes Reinecke
};
454 a1f0cce2 Hannes Reinecke
455 a1f0cce2 Hannes Reinecke
/* Command aborted, Logical Unit failure */
456 a1f0cce2 Hannes Reinecke
const struct SCSISense sense_code_LUN_FAILURE = {
457 a1f0cce2 Hannes Reinecke
    .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
458 a1f0cce2 Hannes Reinecke
};
459 a1f0cce2 Hannes Reinecke
460 a1f0cce2 Hannes Reinecke
/*
461 a1f0cce2 Hannes Reinecke
 * scsi_build_sense
462 a1f0cce2 Hannes Reinecke
 *
463 a1f0cce2 Hannes Reinecke
 * Build a sense buffer
464 a1f0cce2 Hannes Reinecke
 */
465 a1f0cce2 Hannes Reinecke
int scsi_build_sense(SCSISense sense, uint8_t *buf, int len, int fixed)
466 a1f0cce2 Hannes Reinecke
{
467 a1f0cce2 Hannes Reinecke
    if (!fixed && len < 8) {
468 a1f0cce2 Hannes Reinecke
        return 0;
469 a1f0cce2 Hannes Reinecke
    }
470 a1f0cce2 Hannes Reinecke
471 a1f0cce2 Hannes Reinecke
    memset(buf, 0, len);
472 a1f0cce2 Hannes Reinecke
    if (fixed) {
473 a1f0cce2 Hannes Reinecke
        /* Return fixed format sense buffer */
474 a1f0cce2 Hannes Reinecke
        buf[0] = 0xf0;
475 a1f0cce2 Hannes Reinecke
        buf[2] = sense.key;
476 a1f0cce2 Hannes Reinecke
        buf[7] = 7;
477 a1f0cce2 Hannes Reinecke
        buf[12] = sense.asc;
478 a1f0cce2 Hannes Reinecke
        buf[13] = sense.ascq;
479 a1f0cce2 Hannes Reinecke
        return MIN(len, 18);
480 a1f0cce2 Hannes Reinecke
    } else {
481 a1f0cce2 Hannes Reinecke
        /* Return descriptor format sense buffer */
482 a1f0cce2 Hannes Reinecke
        buf[0] = 0x72;
483 a1f0cce2 Hannes Reinecke
        buf[1] = sense.key;
484 a1f0cce2 Hannes Reinecke
        buf[2] = sense.asc;
485 a1f0cce2 Hannes Reinecke
        buf[3] = sense.ascq;
486 a1f0cce2 Hannes Reinecke
        return 8;
487 a1f0cce2 Hannes Reinecke
    }
488 a1f0cce2 Hannes Reinecke
}
489 a1f0cce2 Hannes Reinecke
490 ec766865 Gerd Hoffmann
static const char *scsi_command_name(uint8_t cmd)
491 ec766865 Gerd Hoffmann
{
492 ec766865 Gerd Hoffmann
    static const char *names[] = {
493 ec766865 Gerd Hoffmann
        [ TEST_UNIT_READY          ] = "TEST_UNIT_READY",
494 ec766865 Gerd Hoffmann
        [ REZERO_UNIT              ] = "REZERO_UNIT",
495 545557d4 Blue Swirl
        /* REWIND and REZERO_UNIT use the same operation code */
496 ec766865 Gerd Hoffmann
        [ REQUEST_SENSE            ] = "REQUEST_SENSE",
497 ec766865 Gerd Hoffmann
        [ FORMAT_UNIT              ] = "FORMAT_UNIT",
498 ec766865 Gerd Hoffmann
        [ READ_BLOCK_LIMITS        ] = "READ_BLOCK_LIMITS",
499 ec766865 Gerd Hoffmann
        [ REASSIGN_BLOCKS          ] = "REASSIGN_BLOCKS",
500 ec766865 Gerd Hoffmann
        [ READ_6                   ] = "READ_6",
501 ec766865 Gerd Hoffmann
        [ WRITE_6                  ] = "WRITE_6",
502 ec766865 Gerd Hoffmann
        [ SEEK_6                   ] = "SEEK_6",
503 ec766865 Gerd Hoffmann
        [ READ_REVERSE             ] = "READ_REVERSE",
504 ec766865 Gerd Hoffmann
        [ WRITE_FILEMARKS          ] = "WRITE_FILEMARKS",
505 ec766865 Gerd Hoffmann
        [ SPACE                    ] = "SPACE",
506 ec766865 Gerd Hoffmann
        [ INQUIRY                  ] = "INQUIRY",
507 ec766865 Gerd Hoffmann
        [ RECOVER_BUFFERED_DATA    ] = "RECOVER_BUFFERED_DATA",
508 c7126d5b Nicholas Bellinger
        [ MAINTENANCE_IN           ] = "MAINTENANCE_IN",
509 c7126d5b Nicholas Bellinger
        [ MAINTENANCE_OUT          ] = "MAINTENANCE_OUT",
510 ec766865 Gerd Hoffmann
        [ MODE_SELECT              ] = "MODE_SELECT",
511 ec766865 Gerd Hoffmann
        [ RESERVE                  ] = "RESERVE",
512 ec766865 Gerd Hoffmann
        [ RELEASE                  ] = "RELEASE",
513 ec766865 Gerd Hoffmann
        [ COPY                     ] = "COPY",
514 ec766865 Gerd Hoffmann
        [ ERASE                    ] = "ERASE",
515 ec766865 Gerd Hoffmann
        [ MODE_SENSE               ] = "MODE_SENSE",
516 ec766865 Gerd Hoffmann
        [ START_STOP               ] = "START_STOP",
517 ec766865 Gerd Hoffmann
        [ RECEIVE_DIAGNOSTIC       ] = "RECEIVE_DIAGNOSTIC",
518 ec766865 Gerd Hoffmann
        [ SEND_DIAGNOSTIC          ] = "SEND_DIAGNOSTIC",
519 ec766865 Gerd Hoffmann
        [ ALLOW_MEDIUM_REMOVAL     ] = "ALLOW_MEDIUM_REMOVAL",
520 ec766865 Gerd Hoffmann
521 ec766865 Gerd Hoffmann
        [ SET_WINDOW               ] = "SET_WINDOW",
522 ec766865 Gerd Hoffmann
        [ READ_CAPACITY            ] = "READ_CAPACITY",
523 ec766865 Gerd Hoffmann
        [ READ_10                  ] = "READ_10",
524 ec766865 Gerd Hoffmann
        [ WRITE_10                 ] = "WRITE_10",
525 ec766865 Gerd Hoffmann
        [ SEEK_10                  ] = "SEEK_10",
526 ec766865 Gerd Hoffmann
        [ WRITE_VERIFY             ] = "WRITE_VERIFY",
527 ec766865 Gerd Hoffmann
        [ VERIFY                   ] = "VERIFY",
528 ec766865 Gerd Hoffmann
        [ SEARCH_HIGH              ] = "SEARCH_HIGH",
529 ec766865 Gerd Hoffmann
        [ SEARCH_EQUAL             ] = "SEARCH_EQUAL",
530 ec766865 Gerd Hoffmann
        [ SEARCH_LOW               ] = "SEARCH_LOW",
531 ec766865 Gerd Hoffmann
        [ SET_LIMITS               ] = "SET_LIMITS",
532 ec766865 Gerd Hoffmann
        [ PRE_FETCH                ] = "PRE_FETCH",
533 545557d4 Blue Swirl
        /* READ_POSITION and PRE_FETCH use the same operation code */
534 ec766865 Gerd Hoffmann
        [ SYNCHRONIZE_CACHE        ] = "SYNCHRONIZE_CACHE",
535 ec766865 Gerd Hoffmann
        [ LOCK_UNLOCK_CACHE        ] = "LOCK_UNLOCK_CACHE",
536 ec766865 Gerd Hoffmann
        [ READ_DEFECT_DATA         ] = "READ_DEFECT_DATA",
537 ec766865 Gerd Hoffmann
        [ MEDIUM_SCAN              ] = "MEDIUM_SCAN",
538 ec766865 Gerd Hoffmann
        [ COMPARE                  ] = "COMPARE",
539 ec766865 Gerd Hoffmann
        [ COPY_VERIFY              ] = "COPY_VERIFY",
540 ec766865 Gerd Hoffmann
        [ WRITE_BUFFER             ] = "WRITE_BUFFER",
541 ec766865 Gerd Hoffmann
        [ READ_BUFFER              ] = "READ_BUFFER",
542 ec766865 Gerd Hoffmann
        [ UPDATE_BLOCK             ] = "UPDATE_BLOCK",
543 ec766865 Gerd Hoffmann
        [ READ_LONG                ] = "READ_LONG",
544 ec766865 Gerd Hoffmann
        [ WRITE_LONG               ] = "WRITE_LONG",
545 ec766865 Gerd Hoffmann
        [ CHANGE_DEFINITION        ] = "CHANGE_DEFINITION",
546 ec766865 Gerd Hoffmann
        [ WRITE_SAME               ] = "WRITE_SAME",
547 ec766865 Gerd Hoffmann
        [ READ_TOC                 ] = "READ_TOC",
548 ec766865 Gerd Hoffmann
        [ LOG_SELECT               ] = "LOG_SELECT",
549 ec766865 Gerd Hoffmann
        [ LOG_SENSE                ] = "LOG_SENSE",
550 ec766865 Gerd Hoffmann
        [ MODE_SELECT_10           ] = "MODE_SELECT_10",
551 ec766865 Gerd Hoffmann
        [ RESERVE_10               ] = "RESERVE_10",
552 ec766865 Gerd Hoffmann
        [ RELEASE_10               ] = "RELEASE_10",
553 ec766865 Gerd Hoffmann
        [ MODE_SENSE_10            ] = "MODE_SENSE_10",
554 ec766865 Gerd Hoffmann
        [ PERSISTENT_RESERVE_IN    ] = "PERSISTENT_RESERVE_IN",
555 ec766865 Gerd Hoffmann
        [ PERSISTENT_RESERVE_OUT   ] = "PERSISTENT_RESERVE_OUT",
556 ec766865 Gerd Hoffmann
        [ MOVE_MEDIUM              ] = "MOVE_MEDIUM",
557 ec766865 Gerd Hoffmann
        [ READ_12                  ] = "READ_12",
558 ec766865 Gerd Hoffmann
        [ WRITE_12                 ] = "WRITE_12",
559 ec766865 Gerd Hoffmann
        [ WRITE_VERIFY_12          ] = "WRITE_VERIFY_12",
560 ec766865 Gerd Hoffmann
        [ SEARCH_HIGH_12           ] = "SEARCH_HIGH_12",
561 ec766865 Gerd Hoffmann
        [ SEARCH_EQUAL_12          ] = "SEARCH_EQUAL_12",
562 ec766865 Gerd Hoffmann
        [ SEARCH_LOW_12            ] = "SEARCH_LOW_12",
563 ec766865 Gerd Hoffmann
        [ READ_ELEMENT_STATUS      ] = "READ_ELEMENT_STATUS",
564 ec766865 Gerd Hoffmann
        [ SEND_VOLUME_TAG          ] = "SEND_VOLUME_TAG",
565 ec766865 Gerd Hoffmann
        [ WRITE_LONG_2             ] = "WRITE_LONG_2",
566 ec766865 Gerd Hoffmann
567 ec766865 Gerd Hoffmann
        [ REPORT_DENSITY_SUPPORT   ] = "REPORT_DENSITY_SUPPORT",
568 38215553 Gerd Hoffmann
        [ GET_CONFIGURATION        ] = "GET_CONFIGURATION",
569 bd536cf3 Gerd Hoffmann
        [ READ_16                  ] = "READ_16",
570 bd536cf3 Gerd Hoffmann
        [ WRITE_16                 ] = "WRITE_16",
571 bd536cf3 Gerd Hoffmann
        [ WRITE_VERIFY_16          ] = "WRITE_VERIFY_16",
572 5dd90e2a Gerd Hoffmann
        [ SERVICE_ACTION_IN        ] = "SERVICE_ACTION_IN",
573 39ec9a50 Gerd Hoffmann
        [ REPORT_LUNS              ] = "REPORT_LUNS",
574 ec766865 Gerd Hoffmann
        [ LOAD_UNLOAD              ] = "LOAD_UNLOAD",
575 ec766865 Gerd Hoffmann
        [ SET_CD_SPEED             ] = "SET_CD_SPEED",
576 ec766865 Gerd Hoffmann
        [ BLANK                    ] = "BLANK",
577 ec766865 Gerd Hoffmann
    };
578 ec766865 Gerd Hoffmann
579 ec766865 Gerd Hoffmann
    if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
580 ec766865 Gerd Hoffmann
        return "*UNKNOWN*";
581 ec766865 Gerd Hoffmann
    return names[cmd];
582 ec766865 Gerd Hoffmann
}
583 ec766865 Gerd Hoffmann
584 ad2d30f7 Paolo Bonzini
SCSIRequest *scsi_req_ref(SCSIRequest *req)
585 ad2d30f7 Paolo Bonzini
{
586 ad2d30f7 Paolo Bonzini
    req->refcount++;
587 ad2d30f7 Paolo Bonzini
    return req;
588 ad2d30f7 Paolo Bonzini
}
589 ad2d30f7 Paolo Bonzini
590 ad2d30f7 Paolo Bonzini
void scsi_req_unref(SCSIRequest *req)
591 ad2d30f7 Paolo Bonzini
{
592 ad2d30f7 Paolo Bonzini
    if (--req->refcount == 0) {
593 ad2d30f7 Paolo Bonzini
        if (req->dev->info->free_req) {
594 ad2d30f7 Paolo Bonzini
            req->dev->info->free_req(req);
595 ad2d30f7 Paolo Bonzini
        }
596 ad2d30f7 Paolo Bonzini
        qemu_free(req);
597 ad2d30f7 Paolo Bonzini
    }
598 ad2d30f7 Paolo Bonzini
}
599 ad2d30f7 Paolo Bonzini
600 ab9adc88 Paolo Bonzini
/* Called by the devices when data is ready for the HBA.  The HBA should
601 ab9adc88 Paolo Bonzini
   start a DMA operation to read or fill the device's data buffer.
602 ab9adc88 Paolo Bonzini
   Once it completes, calling one of req->dev->info->read_data or
603 ab9adc88 Paolo Bonzini
   req->dev->info->write_data (depending on the direction of the
604 ab9adc88 Paolo Bonzini
   transfer) will restart I/O.  */
605 ab9adc88 Paolo Bonzini
void scsi_req_data(SCSIRequest *req, int len)
606 ab9adc88 Paolo Bonzini
{
607 ab9adc88 Paolo Bonzini
    trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
608 5c6c0e51 Hannes Reinecke
    req->bus->ops->complete(req, SCSI_REASON_DATA, len);
609 ab9adc88 Paolo Bonzini
}
610 ab9adc88 Paolo Bonzini
611 ec766865 Gerd Hoffmann
void scsi_req_print(SCSIRequest *req)
612 ec766865 Gerd Hoffmann
{
613 ec766865 Gerd Hoffmann
    FILE *fp = stderr;
614 ec766865 Gerd Hoffmann
    int i;
615 ec766865 Gerd Hoffmann
616 ec766865 Gerd Hoffmann
    fprintf(fp, "[%s id=%d] %s",
617 ec766865 Gerd Hoffmann
            req->dev->qdev.parent_bus->name,
618 ec766865 Gerd Hoffmann
            req->dev->id,
619 ec766865 Gerd Hoffmann
            scsi_command_name(req->cmd.buf[0]));
620 ec766865 Gerd Hoffmann
    for (i = 1; i < req->cmd.len; i++) {
621 ec766865 Gerd Hoffmann
        fprintf(fp, " 0x%02x", req->cmd.buf[i]);
622 ec766865 Gerd Hoffmann
    }
623 ec766865 Gerd Hoffmann
    switch (req->cmd.mode) {
624 ec766865 Gerd Hoffmann
    case SCSI_XFER_NONE:
625 ec766865 Gerd Hoffmann
        fprintf(fp, " - none\n");
626 ec766865 Gerd Hoffmann
        break;
627 ec766865 Gerd Hoffmann
    case SCSI_XFER_FROM_DEV:
628 ec766865 Gerd Hoffmann
        fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
629 ec766865 Gerd Hoffmann
        break;
630 ec766865 Gerd Hoffmann
    case SCSI_XFER_TO_DEV:
631 ec766865 Gerd Hoffmann
        fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
632 ec766865 Gerd Hoffmann
        break;
633 ec766865 Gerd Hoffmann
    default:
634 ec766865 Gerd Hoffmann
        fprintf(fp, " - Oops\n");
635 ec766865 Gerd Hoffmann
        break;
636 ec766865 Gerd Hoffmann
    }
637 ec766865 Gerd Hoffmann
}
638 ec766865 Gerd Hoffmann
639 ed3a34a3 Gerd Hoffmann
void scsi_req_complete(SCSIRequest *req)
640 ed3a34a3 Gerd Hoffmann
{
641 ed3a34a3 Gerd Hoffmann
    assert(req->status != -1);
642 ad2d30f7 Paolo Bonzini
    scsi_req_ref(req);
643 e8637c90 Jan Kiszka
    scsi_req_dequeue(req);
644 5c6c0e51 Hannes Reinecke
    req->bus->ops->complete(req, SCSI_REASON_DONE, req->status);
645 ad2d30f7 Paolo Bonzini
    scsi_req_unref(req);
646 ed3a34a3 Gerd Hoffmann
}
647 db07c0f8 Gleb Natapov
648 94d3f98a Paolo Bonzini
void scsi_req_cancel(SCSIRequest *req)
649 94d3f98a Paolo Bonzini
{
650 94d3f98a Paolo Bonzini
    if (req->dev && req->dev->info->cancel_io) {
651 94d3f98a Paolo Bonzini
        req->dev->info->cancel_io(req);
652 94d3f98a Paolo Bonzini
    }
653 94d3f98a Paolo Bonzini
    scsi_req_ref(req);
654 94d3f98a Paolo Bonzini
    scsi_req_dequeue(req);
655 94d3f98a Paolo Bonzini
    if (req->bus->ops->cancel) {
656 94d3f98a Paolo Bonzini
        req->bus->ops->cancel(req);
657 94d3f98a Paolo Bonzini
    }
658 94d3f98a Paolo Bonzini
    scsi_req_unref(req);
659 94d3f98a Paolo Bonzini
}
660 94d3f98a Paolo Bonzini
661 19d110ab Paolo Bonzini
void scsi_req_abort(SCSIRequest *req, int status)
662 19d110ab Paolo Bonzini
{
663 19d110ab Paolo Bonzini
    req->status = status;
664 19d110ab Paolo Bonzini
    if (req->dev && req->dev->info->cancel_io) {
665 19d110ab Paolo Bonzini
        req->dev->info->cancel_io(req);
666 19d110ab Paolo Bonzini
    }
667 19d110ab Paolo Bonzini
    scsi_req_complete(req);
668 19d110ab Paolo Bonzini
}
669 19d110ab Paolo Bonzini
670 c557e889 Paolo Bonzini
void scsi_device_purge_requests(SCSIDevice *sdev)
671 c557e889 Paolo Bonzini
{
672 c557e889 Paolo Bonzini
    SCSIRequest *req;
673 c557e889 Paolo Bonzini
674 c557e889 Paolo Bonzini
    while (!QTAILQ_EMPTY(&sdev->requests)) {
675 c557e889 Paolo Bonzini
        req = QTAILQ_FIRST(&sdev->requests);
676 94d3f98a Paolo Bonzini
        scsi_req_cancel(req);
677 c557e889 Paolo Bonzini
    }
678 c557e889 Paolo Bonzini
}
679 c557e889 Paolo Bonzini
680 db07c0f8 Gleb Natapov
static char *scsibus_get_fw_dev_path(DeviceState *dev)
681 db07c0f8 Gleb Natapov
{
682 db07c0f8 Gleb Natapov
    SCSIDevice *d = (SCSIDevice*)dev;
683 db07c0f8 Gleb Natapov
    SCSIBus *bus = scsi_bus_from_device(d);
684 db07c0f8 Gleb Natapov
    char path[100];
685 db07c0f8 Gleb Natapov
    int i;
686 db07c0f8 Gleb Natapov
687 db07c0f8 Gleb Natapov
    for (i = 0; i < bus->ndev; i++) {
688 db07c0f8 Gleb Natapov
        if (bus->devs[i] == d) {
689 db07c0f8 Gleb Natapov
            break;
690 db07c0f8 Gleb Natapov
        }
691 db07c0f8 Gleb Natapov
    }
692 db07c0f8 Gleb Natapov
693 db07c0f8 Gleb Natapov
    assert(i != bus->ndev);
694 db07c0f8 Gleb Natapov
695 db07c0f8 Gleb Natapov
    snprintf(path, sizeof(path), "%s@%x", qdev_fw_name(dev), i);
696 db07c0f8 Gleb Natapov
697 db07c0f8 Gleb Natapov
    return strdup(path);
698 db07c0f8 Gleb Natapov
}