Statistics
| Branch: | Revision:

root / hw / scsi-bus.c @ c6df7102

History | View | Annotate | Download (20.1 kB)

1
#include "hw.h"
2
#include "qemu-error.h"
3
#include "scsi.h"
4
#include "scsi-defs.h"
5
#include "qdev.h"
6
#include "blockdev.h"
7
#include "trace.h"
8

    
9
static char *scsibus_get_fw_dev_path(DeviceState *dev);
10

    
11
static struct BusInfo scsi_bus_info = {
12
    .name  = "SCSI",
13
    .size  = sizeof(SCSIBus),
14
    .get_fw_dev_path = scsibus_get_fw_dev_path,
15
    .props = (Property[]) {
16
        DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1),
17
        DEFINE_PROP_END_OF_LIST(),
18
    },
19
};
20
static int next_scsi_bus;
21

    
22
/* Create a scsi bus, and attach devices to it.  */
23
void scsi_bus_new(SCSIBus *bus, DeviceState *host, int tcq, int ndev,
24
                  const SCSIBusOps *ops)
25
{
26
    qbus_create_inplace(&bus->qbus, &scsi_bus_info, host, NULL);
27
    bus->busnr = next_scsi_bus++;
28
    bus->tcq = tcq;
29
    bus->ndev = ndev;
30
    bus->ops = ops;
31
    bus->qbus.allow_hotplug = 1;
32
}
33

    
34
static int scsi_qdev_init(DeviceState *qdev, DeviceInfo *base)
35
{
36
    SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
37
    SCSIDeviceInfo *info = DO_UPCAST(SCSIDeviceInfo, qdev, base);
38
    SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
39
    int rc = -1;
40

    
41
    if (dev->id == -1) {
42
        for (dev->id = 0; dev->id < bus->ndev; dev->id++) {
43
            if (bus->devs[dev->id] == NULL)
44
                break;
45
        }
46
    }
47
    if (dev->id >= bus->ndev) {
48
        error_report("bad scsi device id: %d", dev->id);
49
        goto err;
50
    }
51

    
52
    if (bus->devs[dev->id]) {
53
        qdev_free(&bus->devs[dev->id]->qdev);
54
    }
55
    bus->devs[dev->id] = dev;
56

    
57
    dev->info = info;
58
    QTAILQ_INIT(&dev->requests);
59
    rc = dev->info->init(dev);
60
    if (rc != 0) {
61
        bus->devs[dev->id] = NULL;
62
    }
63

    
64
err:
65
    return rc;
66
}
67

    
68
static int scsi_qdev_exit(DeviceState *qdev)
69
{
70
    SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
71
    SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
72

    
73
    assert(bus->devs[dev->id] != NULL);
74
    if (bus->devs[dev->id]->info->destroy) {
75
        bus->devs[dev->id]->info->destroy(bus->devs[dev->id]);
76
    }
77
    bus->devs[dev->id] = NULL;
78
    return 0;
79
}
80

    
81
void scsi_qdev_register(SCSIDeviceInfo *info)
82
{
83
    info->qdev.bus_info = &scsi_bus_info;
84
    info->qdev.init     = scsi_qdev_init;
85
    info->qdev.unplug   = qdev_simple_unplug_cb;
86
    info->qdev.exit     = scsi_qdev_exit;
87
    qdev_register(&info->qdev);
88
}
89

    
90
/* handle legacy '-drive if=scsi,...' cmd line args */
91
SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
92
                                      int unit, bool removable)
93
{
94
    const char *driver;
95
    DeviceState *dev;
96

    
97
    driver = bdrv_is_sg(bdrv) ? "scsi-generic" : "scsi-disk";
98
    dev = qdev_create(&bus->qbus, driver);
99
    qdev_prop_set_uint32(dev, "scsi-id", unit);
100
    if (qdev_prop_exists(dev, "removable")) {
101
        qdev_prop_set_bit(dev, "removable", removable);
102
    }
103
    if (qdev_prop_set_drive(dev, "drive", bdrv) < 0) {
104
        qdev_free(dev);
105
        return NULL;
106
    }
107
    if (qdev_init(dev) < 0)
108
        return NULL;
109
    return DO_UPCAST(SCSIDevice, qdev, dev);
110
}
111

    
112
int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
113
{
114
    Location loc;
115
    DriveInfo *dinfo;
116
    int res = 0, unit;
117

    
118
    loc_push_none(&loc);
119
    for (unit = 0; unit < bus->ndev; unit++) {
120
        dinfo = drive_get(IF_SCSI, bus->busnr, unit);
121
        if (dinfo == NULL) {
122
            continue;
123
        }
124
        qemu_opts_loc_restore(dinfo->opts);
125
        if (!scsi_bus_legacy_add_drive(bus, dinfo->bdrv, unit, false)) {
126
            res = -1;
127
            break;
128
        }
129
    }
130
    loc_pop(&loc);
131
    return res;
132
}
133

    
134
SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t lun)
135
{
136
    SCSIRequest *req;
137

    
138
    req = qemu_mallocz(size);
139
    req->refcount = 1;
140
    req->bus = scsi_bus_from_device(d);
141
    req->dev = d;
142
    req->tag = tag;
143
    req->lun = lun;
144
    req->status = -1;
145
    trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
146
    return req;
147
}
148

    
149
SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun)
150
{
151
    return d->info->alloc_req(d, tag, lun);
152
}
153

    
154
uint8_t *scsi_req_get_buf(SCSIRequest *req)
155
{
156
    return req->dev->info->get_buf(req);
157
}
158

    
159
int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len)
160
{
161
    if (req->dev->info->get_sense) {
162
        return req->dev->info->get_sense(req, buf, len);
163
    } else {
164
        return 0;
165
    }
166
}
167

    
168
int32_t scsi_req_enqueue(SCSIRequest *req, uint8_t *buf)
169
{
170
    int32_t rc;
171

    
172
    assert(!req->enqueued);
173
    scsi_req_ref(req);
174
    req->enqueued = true;
175
    QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
176

    
177
    scsi_req_ref(req);
178
    rc = req->dev->info->send_command(req, buf);
179
    scsi_req_unref(req);
180
    return rc;
181
}
182

    
183
static void scsi_req_dequeue(SCSIRequest *req)
184
{
185
    trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
186
    if (req->enqueued) {
187
        QTAILQ_REMOVE(&req->dev->requests, req, next);
188
        req->enqueued = false;
189
        scsi_req_unref(req);
190
    }
191
}
192

    
193
static int scsi_req_length(SCSIRequest *req, uint8_t *cmd)
194
{
195
    switch (cmd[0] >> 5) {
196
    case 0:
197
        req->cmd.xfer = cmd[4];
198
        req->cmd.len = 6;
199
        /* length 0 means 256 blocks */
200
        if (req->cmd.xfer == 0)
201
            req->cmd.xfer = 256;
202
        break;
203
    case 1:
204
    case 2:
205
        req->cmd.xfer = cmd[8] | (cmd[7] << 8);
206
        req->cmd.len = 10;
207
        break;
208
    case 4:
209
        req->cmd.xfer = cmd[13] | (cmd[12] << 8) | (cmd[11] << 16) | (cmd[10] << 24);
210
        req->cmd.len = 16;
211
        break;
212
    case 5:
213
        req->cmd.xfer = cmd[9] | (cmd[8] << 8) | (cmd[7] << 16) | (cmd[6] << 24);
214
        req->cmd.len = 12;
215
        break;
216
    default:
217
        trace_scsi_req_parse_bad(req->dev->id, req->lun, req->tag, cmd[0]);
218
        return -1;
219
    }
220

    
221
    switch(cmd[0]) {
222
    case TEST_UNIT_READY:
223
    case REZERO_UNIT:
224
    case START_STOP:
225
    case SEEK_6:
226
    case WRITE_FILEMARKS:
227
    case SPACE:
228
    case RESERVE:
229
    case RELEASE:
230
    case ERASE:
231
    case ALLOW_MEDIUM_REMOVAL:
232
    case VERIFY:
233
    case SEEK_10:
234
    case SYNCHRONIZE_CACHE:
235
    case LOCK_UNLOCK_CACHE:
236
    case LOAD_UNLOAD:
237
    case SET_CD_SPEED:
238
    case SET_LIMITS:
239
    case WRITE_LONG:
240
    case MOVE_MEDIUM:
241
    case UPDATE_BLOCK:
242
        req->cmd.xfer = 0;
243
        break;
244
    case MODE_SENSE:
245
        break;
246
    case WRITE_SAME:
247
        req->cmd.xfer = 1;
248
        break;
249
    case READ_CAPACITY:
250
        req->cmd.xfer = 8;
251
        break;
252
    case READ_BLOCK_LIMITS:
253
        req->cmd.xfer = 6;
254
        break;
255
    case READ_POSITION:
256
        req->cmd.xfer = 20;
257
        break;
258
    case SEND_VOLUME_TAG:
259
        req->cmd.xfer *= 40;
260
        break;
261
    case MEDIUM_SCAN:
262
        req->cmd.xfer *= 8;
263
        break;
264
    case WRITE_10:
265
    case WRITE_VERIFY:
266
    case WRITE_6:
267
    case WRITE_12:
268
    case WRITE_VERIFY_12:
269
    case WRITE_16:
270
    case WRITE_VERIFY_16:
271
        req->cmd.xfer *= req->dev->blocksize;
272
        break;
273
    case READ_10:
274
    case READ_6:
275
    case READ_REVERSE:
276
    case RECOVER_BUFFERED_DATA:
277
    case READ_12:
278
    case READ_16:
279
        req->cmd.xfer *= req->dev->blocksize;
280
        break;
281
    case INQUIRY:
282
        req->cmd.xfer = cmd[4] | (cmd[3] << 8);
283
        break;
284
    case MAINTENANCE_OUT:
285
    case MAINTENANCE_IN:
286
        if (req->dev->type == TYPE_ROM) {
287
            /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
288
            req->cmd.xfer = cmd[9] | (cmd[8] << 8);
289
        }
290
        break;
291
    }
292
    return 0;
293
}
294

    
295
static int scsi_req_stream_length(SCSIRequest *req, uint8_t *cmd)
296
{
297
    switch(cmd[0]) {
298
    /* stream commands */
299
    case READ_6:
300
    case READ_REVERSE:
301
    case RECOVER_BUFFERED_DATA:
302
    case WRITE_6:
303
        req->cmd.len = 6;
304
        req->cmd.xfer = cmd[4] | (cmd[3] << 8) | (cmd[2] << 16);
305
        if (cmd[1] & 0x01) /* fixed */
306
            req->cmd.xfer *= req->dev->blocksize;
307
        break;
308
    case REWIND:
309
    case START_STOP:
310
        req->cmd.len = 6;
311
        req->cmd.xfer = 0;
312
        break;
313
    /* generic commands */
314
    default:
315
        return scsi_req_length(req, cmd);
316
    }
317
    return 0;
318
}
319

    
320
static void scsi_req_xfer_mode(SCSIRequest *req)
321
{
322
    switch (req->cmd.buf[0]) {
323
    case WRITE_6:
324
    case WRITE_10:
325
    case WRITE_VERIFY:
326
    case WRITE_12:
327
    case WRITE_VERIFY_12:
328
    case WRITE_16:
329
    case WRITE_VERIFY_16:
330
    case COPY:
331
    case COPY_VERIFY:
332
    case COMPARE:
333
    case CHANGE_DEFINITION:
334
    case LOG_SELECT:
335
    case MODE_SELECT:
336
    case MODE_SELECT_10:
337
    case SEND_DIAGNOSTIC:
338
    case WRITE_BUFFER:
339
    case FORMAT_UNIT:
340
    case REASSIGN_BLOCKS:
341
    case SEARCH_EQUAL:
342
    case SEARCH_HIGH:
343
    case SEARCH_LOW:
344
    case UPDATE_BLOCK:
345
    case WRITE_LONG:
346
    case WRITE_SAME:
347
    case SEARCH_HIGH_12:
348
    case SEARCH_EQUAL_12:
349
    case SEARCH_LOW_12:
350
    case SET_WINDOW:
351
    case MEDIUM_SCAN:
352
    case SEND_VOLUME_TAG:
353
    case WRITE_LONG_2:
354
    case PERSISTENT_RESERVE_OUT:
355
    case MAINTENANCE_OUT:
356
        req->cmd.mode = SCSI_XFER_TO_DEV;
357
        break;
358
    default:
359
        if (req->cmd.xfer)
360
            req->cmd.mode = SCSI_XFER_FROM_DEV;
361
        else {
362
            req->cmd.mode = SCSI_XFER_NONE;
363
        }
364
        break;
365
    }
366
}
367

    
368
static uint64_t scsi_req_lba(SCSIRequest *req)
369
{
370
    uint8_t *buf = req->cmd.buf;
371
    uint64_t lba;
372

    
373
    switch (buf[0] >> 5) {
374
    case 0:
375
        lba = (uint64_t) buf[3] | ((uint64_t) buf[2] << 8) |
376
              (((uint64_t) buf[1] & 0x1f) << 16);
377
        break;
378
    case 1:
379
    case 2:
380
        lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
381
              ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
382
        break;
383
    case 4:
384
        lba = (uint64_t) buf[9] | ((uint64_t) buf[8] << 8) |
385
              ((uint64_t) buf[7] << 16) | ((uint64_t) buf[6] << 24) |
386
              ((uint64_t) buf[5] << 32) | ((uint64_t) buf[4] << 40) |
387
              ((uint64_t) buf[3] << 48) | ((uint64_t) buf[2] << 56);
388
        break;
389
    case 5:
390
        lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
391
              ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
392
        break;
393
    default:
394
        lba = -1;
395

    
396
    }
397
    return lba;
398
}
399

    
400
int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
401
{
402
    int rc;
403

    
404
    if (req->dev->type == TYPE_TAPE) {
405
        rc = scsi_req_stream_length(req, buf);
406
    } else {
407
        rc = scsi_req_length(req, buf);
408
    }
409
    if (rc != 0)
410
        return rc;
411

    
412
    memcpy(req->cmd.buf, buf, req->cmd.len);
413
    scsi_req_xfer_mode(req);
414
    req->cmd.lba = scsi_req_lba(req);
415
    trace_scsi_req_parsed(req->dev->id, req->lun, req->tag, buf[0],
416
                          req->cmd.mode, req->cmd.xfer, req->cmd.lba);
417
    return 0;
418
}
419

    
420
/*
421
 * Predefined sense codes
422
 */
423

    
424
/* No sense data available */
425
const struct SCSISense sense_code_NO_SENSE = {
426
    .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00
427
};
428

    
429
/* LUN not ready, Manual intervention required */
430
const struct SCSISense sense_code_LUN_NOT_READY = {
431
    .key = NOT_READY, .asc = 0x04, .ascq = 0x03
432
};
433

    
434
/* LUN not ready, Medium not present */
435
const struct SCSISense sense_code_NO_MEDIUM = {
436
    .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
437
};
438

    
439
/* Hardware error, internal target failure */
440
const struct SCSISense sense_code_TARGET_FAILURE = {
441
    .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
442
};
443

    
444
/* Illegal request, invalid command operation code */
445
const struct SCSISense sense_code_INVALID_OPCODE = {
446
    .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00
447
};
448

    
449
/* Illegal request, LBA out of range */
450
const struct SCSISense sense_code_LBA_OUT_OF_RANGE = {
451
    .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00
452
};
453

    
454
/* Illegal request, Invalid field in CDB */
455
const struct SCSISense sense_code_INVALID_FIELD = {
456
    .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00
457
};
458

    
459
/* Illegal request, LUN not supported */
460
const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
461
    .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
462
};
463

    
464
/* Command aborted, I/O process terminated */
465
const struct SCSISense sense_code_IO_ERROR = {
466
    .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
467
};
468

    
469
/* Command aborted, I_T Nexus loss occurred */
470
const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
471
    .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
472
};
473

    
474
/* Command aborted, Logical Unit failure */
475
const struct SCSISense sense_code_LUN_FAILURE = {
476
    .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
477
};
478

    
479
/*
480
 * scsi_build_sense
481
 *
482
 * Build a sense buffer
483
 */
484
int scsi_build_sense(SCSISense sense, uint8_t *buf, int len, int fixed)
485
{
486
    if (!fixed && len < 8) {
487
        return 0;
488
    }
489

    
490
    memset(buf, 0, len);
491
    if (fixed) {
492
        /* Return fixed format sense buffer */
493
        buf[0] = 0xf0;
494
        buf[2] = sense.key;
495
        buf[7] = 7;
496
        buf[12] = sense.asc;
497
        buf[13] = sense.ascq;
498
        return MIN(len, 18);
499
    } else {
500
        /* Return descriptor format sense buffer */
501
        buf[0] = 0x72;
502
        buf[1] = sense.key;
503
        buf[2] = sense.asc;
504
        buf[3] = sense.ascq;
505
        return 8;
506
    }
507
}
508

    
509
static const char *scsi_command_name(uint8_t cmd)
510
{
511
    static const char *names[] = {
512
        [ TEST_UNIT_READY          ] = "TEST_UNIT_READY",
513
        [ REZERO_UNIT              ] = "REZERO_UNIT",
514
        /* REWIND and REZERO_UNIT use the same operation code */
515
        [ REQUEST_SENSE            ] = "REQUEST_SENSE",
516
        [ FORMAT_UNIT              ] = "FORMAT_UNIT",
517
        [ READ_BLOCK_LIMITS        ] = "READ_BLOCK_LIMITS",
518
        [ REASSIGN_BLOCKS          ] = "REASSIGN_BLOCKS",
519
        [ READ_6                   ] = "READ_6",
520
        [ WRITE_6                  ] = "WRITE_6",
521
        [ SEEK_6                   ] = "SEEK_6",
522
        [ READ_REVERSE             ] = "READ_REVERSE",
523
        [ WRITE_FILEMARKS          ] = "WRITE_FILEMARKS",
524
        [ SPACE                    ] = "SPACE",
525
        [ INQUIRY                  ] = "INQUIRY",
526
        [ RECOVER_BUFFERED_DATA    ] = "RECOVER_BUFFERED_DATA",
527
        [ MAINTENANCE_IN           ] = "MAINTENANCE_IN",
528
        [ MAINTENANCE_OUT          ] = "MAINTENANCE_OUT",
529
        [ MODE_SELECT              ] = "MODE_SELECT",
530
        [ RESERVE                  ] = "RESERVE",
531
        [ RELEASE                  ] = "RELEASE",
532
        [ COPY                     ] = "COPY",
533
        [ ERASE                    ] = "ERASE",
534
        [ MODE_SENSE               ] = "MODE_SENSE",
535
        [ START_STOP               ] = "START_STOP",
536
        [ RECEIVE_DIAGNOSTIC       ] = "RECEIVE_DIAGNOSTIC",
537
        [ SEND_DIAGNOSTIC          ] = "SEND_DIAGNOSTIC",
538
        [ ALLOW_MEDIUM_REMOVAL     ] = "ALLOW_MEDIUM_REMOVAL",
539

    
540
        [ SET_WINDOW               ] = "SET_WINDOW",
541
        [ READ_CAPACITY            ] = "READ_CAPACITY",
542
        [ READ_10                  ] = "READ_10",
543
        [ WRITE_10                 ] = "WRITE_10",
544
        [ SEEK_10                  ] = "SEEK_10",
545
        [ WRITE_VERIFY             ] = "WRITE_VERIFY",
546
        [ VERIFY                   ] = "VERIFY",
547
        [ SEARCH_HIGH              ] = "SEARCH_HIGH",
548
        [ SEARCH_EQUAL             ] = "SEARCH_EQUAL",
549
        [ SEARCH_LOW               ] = "SEARCH_LOW",
550
        [ SET_LIMITS               ] = "SET_LIMITS",
551
        [ PRE_FETCH                ] = "PRE_FETCH",
552
        /* READ_POSITION and PRE_FETCH use the same operation code */
553
        [ SYNCHRONIZE_CACHE        ] = "SYNCHRONIZE_CACHE",
554
        [ LOCK_UNLOCK_CACHE        ] = "LOCK_UNLOCK_CACHE",
555
        [ READ_DEFECT_DATA         ] = "READ_DEFECT_DATA",
556
        [ MEDIUM_SCAN              ] = "MEDIUM_SCAN",
557
        [ COMPARE                  ] = "COMPARE",
558
        [ COPY_VERIFY              ] = "COPY_VERIFY",
559
        [ WRITE_BUFFER             ] = "WRITE_BUFFER",
560
        [ READ_BUFFER              ] = "READ_BUFFER",
561
        [ UPDATE_BLOCK             ] = "UPDATE_BLOCK",
562
        [ READ_LONG                ] = "READ_LONG",
563
        [ WRITE_LONG               ] = "WRITE_LONG",
564
        [ CHANGE_DEFINITION        ] = "CHANGE_DEFINITION",
565
        [ WRITE_SAME               ] = "WRITE_SAME",
566
        [ READ_TOC                 ] = "READ_TOC",
567
        [ LOG_SELECT               ] = "LOG_SELECT",
568
        [ LOG_SENSE                ] = "LOG_SENSE",
569
        [ MODE_SELECT_10           ] = "MODE_SELECT_10",
570
        [ RESERVE_10               ] = "RESERVE_10",
571
        [ RELEASE_10               ] = "RELEASE_10",
572
        [ MODE_SENSE_10            ] = "MODE_SENSE_10",
573
        [ PERSISTENT_RESERVE_IN    ] = "PERSISTENT_RESERVE_IN",
574
        [ PERSISTENT_RESERVE_OUT   ] = "PERSISTENT_RESERVE_OUT",
575
        [ MOVE_MEDIUM              ] = "MOVE_MEDIUM",
576
        [ READ_12                  ] = "READ_12",
577
        [ WRITE_12                 ] = "WRITE_12",
578
        [ WRITE_VERIFY_12          ] = "WRITE_VERIFY_12",
579
        [ SEARCH_HIGH_12           ] = "SEARCH_HIGH_12",
580
        [ SEARCH_EQUAL_12          ] = "SEARCH_EQUAL_12",
581
        [ SEARCH_LOW_12            ] = "SEARCH_LOW_12",
582
        [ READ_ELEMENT_STATUS      ] = "READ_ELEMENT_STATUS",
583
        [ SEND_VOLUME_TAG          ] = "SEND_VOLUME_TAG",
584
        [ WRITE_LONG_2             ] = "WRITE_LONG_2",
585

    
586
        [ REPORT_DENSITY_SUPPORT   ] = "REPORT_DENSITY_SUPPORT",
587
        [ GET_CONFIGURATION        ] = "GET_CONFIGURATION",
588
        [ READ_16                  ] = "READ_16",
589
        [ WRITE_16                 ] = "WRITE_16",
590
        [ WRITE_VERIFY_16          ] = "WRITE_VERIFY_16",
591
        [ SERVICE_ACTION_IN        ] = "SERVICE_ACTION_IN",
592
        [ REPORT_LUNS              ] = "REPORT_LUNS",
593
        [ LOAD_UNLOAD              ] = "LOAD_UNLOAD",
594
        [ SET_CD_SPEED             ] = "SET_CD_SPEED",
595
        [ BLANK                    ] = "BLANK",
596
    };
597

    
598
    if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
599
        return "*UNKNOWN*";
600
    return names[cmd];
601
}
602

    
603
SCSIRequest *scsi_req_ref(SCSIRequest *req)
604
{
605
    req->refcount++;
606
    return req;
607
}
608

    
609
void scsi_req_unref(SCSIRequest *req)
610
{
611
    if (--req->refcount == 0) {
612
        if (req->dev->info->free_req) {
613
            req->dev->info->free_req(req);
614
        }
615
        qemu_free(req);
616
    }
617
}
618

    
619
/* Tell the device that we finished processing this chunk of I/O.  It
620
   will start the next chunk or complete the command.  */
621
void scsi_req_continue(SCSIRequest *req)
622
{
623
    trace_scsi_req_continue(req->dev->id, req->lun, req->tag);
624
    if (req->cmd.mode == SCSI_XFER_TO_DEV) {
625
        req->dev->info->write_data(req);
626
    } else {
627
        req->dev->info->read_data(req);
628
    }
629
}
630

    
631
/* Called by the devices when data is ready for the HBA.  The HBA should
632
   start a DMA operation to read or fill the device's data buffer.
633
   Once it completes, calling scsi_req_continue will restart I/O.  */
634
void scsi_req_data(SCSIRequest *req, int len)
635
{
636
    trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
637
    req->bus->ops->transfer_data(req, len);
638
}
639

    
640
void scsi_req_print(SCSIRequest *req)
641
{
642
    FILE *fp = stderr;
643
    int i;
644

    
645
    fprintf(fp, "[%s id=%d] %s",
646
            req->dev->qdev.parent_bus->name,
647
            req->dev->id,
648
            scsi_command_name(req->cmd.buf[0]));
649
    for (i = 1; i < req->cmd.len; i++) {
650
        fprintf(fp, " 0x%02x", req->cmd.buf[i]);
651
    }
652
    switch (req->cmd.mode) {
653
    case SCSI_XFER_NONE:
654
        fprintf(fp, " - none\n");
655
        break;
656
    case SCSI_XFER_FROM_DEV:
657
        fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
658
        break;
659
    case SCSI_XFER_TO_DEV:
660
        fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
661
        break;
662
    default:
663
        fprintf(fp, " - Oops\n");
664
        break;
665
    }
666
}
667

    
668
void scsi_req_complete(SCSIRequest *req)
669
{
670
    assert(req->status != -1);
671
    scsi_req_ref(req);
672
    scsi_req_dequeue(req);
673
    req->bus->ops->complete(req, req->status);
674
    scsi_req_unref(req);
675
}
676

    
677
void scsi_req_cancel(SCSIRequest *req)
678
{
679
    if (req->dev && req->dev->info->cancel_io) {
680
        req->dev->info->cancel_io(req);
681
    }
682
    scsi_req_ref(req);
683
    scsi_req_dequeue(req);
684
    if (req->bus->ops->cancel) {
685
        req->bus->ops->cancel(req);
686
    }
687
    scsi_req_unref(req);
688
}
689

    
690
void scsi_req_abort(SCSIRequest *req, int status)
691
{
692
    req->status = status;
693
    if (req->dev && req->dev->info->cancel_io) {
694
        req->dev->info->cancel_io(req);
695
    }
696
    scsi_req_complete(req);
697
}
698

    
699
void scsi_device_purge_requests(SCSIDevice *sdev)
700
{
701
    SCSIRequest *req;
702

    
703
    while (!QTAILQ_EMPTY(&sdev->requests)) {
704
        req = QTAILQ_FIRST(&sdev->requests);
705
        scsi_req_cancel(req);
706
    }
707
}
708

    
709
static char *scsibus_get_fw_dev_path(DeviceState *dev)
710
{
711
    SCSIDevice *d = (SCSIDevice*)dev;
712
    SCSIBus *bus = scsi_bus_from_device(d);
713
    char path[100];
714
    int i;
715

    
716
    for (i = 0; i < bus->ndev; i++) {
717
        if (bus->devs[i] == d) {
718
            break;
719
        }
720
    }
721

    
722
    assert(i != bus->ndev);
723

    
724
    snprintf(path, sizeof(path), "%s@%x", qdev_fw_name(dev), i);
725

    
726
    return strdup(path);
727
}