Statistics
| Branch: | Revision:

root / hw / scsi-bus.c @ ad3376cc

History | View | Annotate | Download (19.9 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
int32_t scsi_req_enqueue(SCSIRequest *req, uint8_t *buf)
155
{
156
    int32_t rc;
157

    
158
    assert(!req->enqueued);
159
    scsi_req_ref(req);
160
    req->enqueued = true;
161
    QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
162

    
163
    scsi_req_ref(req);
164
    rc = req->dev->info->send_command(req, buf);
165
    scsi_req_unref(req);
166
    return rc;
167
}
168

    
169
static void scsi_req_dequeue(SCSIRequest *req)
170
{
171
    trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
172
    if (req->enqueued) {
173
        QTAILQ_REMOVE(&req->dev->requests, req, next);
174
        req->enqueued = false;
175
        scsi_req_unref(req);
176
    }
177
}
178

    
179
static int scsi_req_length(SCSIRequest *req, uint8_t *cmd)
180
{
181
    switch (cmd[0] >> 5) {
182
    case 0:
183
        req->cmd.xfer = cmd[4];
184
        req->cmd.len = 6;
185
        /* length 0 means 256 blocks */
186
        if (req->cmd.xfer == 0)
187
            req->cmd.xfer = 256;
188
        break;
189
    case 1:
190
    case 2:
191
        req->cmd.xfer = cmd[8] | (cmd[7] << 8);
192
        req->cmd.len = 10;
193
        break;
194
    case 4:
195
        req->cmd.xfer = cmd[13] | (cmd[12] << 8) | (cmd[11] << 16) | (cmd[10] << 24);
196
        req->cmd.len = 16;
197
        break;
198
    case 5:
199
        req->cmd.xfer = cmd[9] | (cmd[8] << 8) | (cmd[7] << 16) | (cmd[6] << 24);
200
        req->cmd.len = 12;
201
        break;
202
    default:
203
        trace_scsi_req_parse_bad(req->dev->id, req->lun, req->tag, cmd[0]);
204
        return -1;
205
    }
206

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

    
281
static int scsi_req_stream_length(SCSIRequest *req, uint8_t *cmd)
282
{
283
    switch(cmd[0]) {
284
    /* stream commands */
285
    case READ_6:
286
    case READ_REVERSE:
287
    case RECOVER_BUFFERED_DATA:
288
    case WRITE_6:
289
        req->cmd.len = 6;
290
        req->cmd.xfer = cmd[4] | (cmd[3] << 8) | (cmd[2] << 16);
291
        if (cmd[1] & 0x01) /* fixed */
292
            req->cmd.xfer *= req->dev->blocksize;
293
        break;
294
    case REWIND:
295
    case START_STOP:
296
        req->cmd.len = 6;
297
        req->cmd.xfer = 0;
298
        break;
299
    /* generic commands */
300
    default:
301
        return scsi_req_length(req, cmd);
302
    }
303
    return 0;
304
}
305

    
306
static void scsi_req_xfer_mode(SCSIRequest *req)
307
{
308
    switch (req->cmd.buf[0]) {
309
    case WRITE_6:
310
    case WRITE_10:
311
    case WRITE_VERIFY:
312
    case WRITE_12:
313
    case WRITE_VERIFY_12:
314
    case WRITE_16:
315
    case WRITE_VERIFY_16:
316
    case COPY:
317
    case COPY_VERIFY:
318
    case COMPARE:
319
    case CHANGE_DEFINITION:
320
    case LOG_SELECT:
321
    case MODE_SELECT:
322
    case MODE_SELECT_10:
323
    case SEND_DIAGNOSTIC:
324
    case WRITE_BUFFER:
325
    case FORMAT_UNIT:
326
    case REASSIGN_BLOCKS:
327
    case SEARCH_EQUAL:
328
    case SEARCH_HIGH:
329
    case SEARCH_LOW:
330
    case UPDATE_BLOCK:
331
    case WRITE_LONG:
332
    case WRITE_SAME:
333
    case SEARCH_HIGH_12:
334
    case SEARCH_EQUAL_12:
335
    case SEARCH_LOW_12:
336
    case SET_WINDOW:
337
    case MEDIUM_SCAN:
338
    case SEND_VOLUME_TAG:
339
    case WRITE_LONG_2:
340
    case PERSISTENT_RESERVE_OUT:
341
    case MAINTENANCE_OUT:
342
        req->cmd.mode = SCSI_XFER_TO_DEV;
343
        break;
344
    default:
345
        if (req->cmd.xfer)
346
            req->cmd.mode = SCSI_XFER_FROM_DEV;
347
        else {
348
            req->cmd.mode = SCSI_XFER_NONE;
349
        }
350
        break;
351
    }
352
}
353

    
354
static uint64_t scsi_req_lba(SCSIRequest *req)
355
{
356
    uint8_t *buf = req->cmd.buf;
357
    uint64_t lba;
358

    
359
    switch (buf[0] >> 5) {
360
    case 0:
361
        lba = (uint64_t) buf[3] | ((uint64_t) buf[2] << 8) |
362
              (((uint64_t) buf[1] & 0x1f) << 16);
363
        break;
364
    case 1:
365
    case 2:
366
        lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
367
              ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
368
        break;
369
    case 4:
370
        lba = (uint64_t) buf[9] | ((uint64_t) buf[8] << 8) |
371
              ((uint64_t) buf[7] << 16) | ((uint64_t) buf[6] << 24) |
372
              ((uint64_t) buf[5] << 32) | ((uint64_t) buf[4] << 40) |
373
              ((uint64_t) buf[3] << 48) | ((uint64_t) buf[2] << 56);
374
        break;
375
    case 5:
376
        lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
377
              ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
378
        break;
379
    default:
380
        lba = -1;
381

    
382
    }
383
    return lba;
384
}
385

    
386
int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
387
{
388
    int rc;
389

    
390
    if (req->dev->type == TYPE_TAPE) {
391
        rc = scsi_req_stream_length(req, buf);
392
    } else {
393
        rc = scsi_req_length(req, buf);
394
    }
395
    if (rc != 0)
396
        return rc;
397

    
398
    memcpy(req->cmd.buf, buf, req->cmd.len);
399
    scsi_req_xfer_mode(req);
400
    req->cmd.lba = scsi_req_lba(req);
401
    trace_scsi_req_parsed(req->dev->id, req->lun, req->tag, buf[0],
402
                          req->cmd.mode, req->cmd.xfer, req->cmd.lba);
403
    return 0;
404
}
405

    
406
/*
407
 * Predefined sense codes
408
 */
409

    
410
/* No sense data available */
411
const struct SCSISense sense_code_NO_SENSE = {
412
    .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00
413
};
414

    
415
/* LUN not ready, Manual intervention required */
416
const struct SCSISense sense_code_LUN_NOT_READY = {
417
    .key = NOT_READY, .asc = 0x04, .ascq = 0x03
418
};
419

    
420
/* LUN not ready, Medium not present */
421
const struct SCSISense sense_code_NO_MEDIUM = {
422
    .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
423
};
424

    
425
/* Hardware error, internal target failure */
426
const struct SCSISense sense_code_TARGET_FAILURE = {
427
    .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
428
};
429

    
430
/* Illegal request, invalid command operation code */
431
const struct SCSISense sense_code_INVALID_OPCODE = {
432
    .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00
433
};
434

    
435
/* Illegal request, LBA out of range */
436
const struct SCSISense sense_code_LBA_OUT_OF_RANGE = {
437
    .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00
438
};
439

    
440
/* Illegal request, Invalid field in CDB */
441
const struct SCSISense sense_code_INVALID_FIELD = {
442
    .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00
443
};
444

    
445
/* Illegal request, LUN not supported */
446
const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
447
    .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
448
};
449

    
450
/* Command aborted, I/O process terminated */
451
const struct SCSISense sense_code_IO_ERROR = {
452
    .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
453
};
454

    
455
/* Command aborted, I_T Nexus loss occurred */
456
const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
457
    .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
458
};
459

    
460
/* Command aborted, Logical Unit failure */
461
const struct SCSISense sense_code_LUN_FAILURE = {
462
    .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
463
};
464

    
465
/*
466
 * scsi_build_sense
467
 *
468
 * Build a sense buffer
469
 */
470
int scsi_build_sense(SCSISense sense, uint8_t *buf, int len, int fixed)
471
{
472
    if (!fixed && len < 8) {
473
        return 0;
474
    }
475

    
476
    memset(buf, 0, len);
477
    if (fixed) {
478
        /* Return fixed format sense buffer */
479
        buf[0] = 0xf0;
480
        buf[2] = sense.key;
481
        buf[7] = 7;
482
        buf[12] = sense.asc;
483
        buf[13] = sense.ascq;
484
        return MIN(len, 18);
485
    } else {
486
        /* Return descriptor format sense buffer */
487
        buf[0] = 0x72;
488
        buf[1] = sense.key;
489
        buf[2] = sense.asc;
490
        buf[3] = sense.ascq;
491
        return 8;
492
    }
493
}
494

    
495
static const char *scsi_command_name(uint8_t cmd)
496
{
497
    static const char *names[] = {
498
        [ TEST_UNIT_READY          ] = "TEST_UNIT_READY",
499
        [ REZERO_UNIT              ] = "REZERO_UNIT",
500
        /* REWIND and REZERO_UNIT use the same operation code */
501
        [ REQUEST_SENSE            ] = "REQUEST_SENSE",
502
        [ FORMAT_UNIT              ] = "FORMAT_UNIT",
503
        [ READ_BLOCK_LIMITS        ] = "READ_BLOCK_LIMITS",
504
        [ REASSIGN_BLOCKS          ] = "REASSIGN_BLOCKS",
505
        [ READ_6                   ] = "READ_6",
506
        [ WRITE_6                  ] = "WRITE_6",
507
        [ SEEK_6                   ] = "SEEK_6",
508
        [ READ_REVERSE             ] = "READ_REVERSE",
509
        [ WRITE_FILEMARKS          ] = "WRITE_FILEMARKS",
510
        [ SPACE                    ] = "SPACE",
511
        [ INQUIRY                  ] = "INQUIRY",
512
        [ RECOVER_BUFFERED_DATA    ] = "RECOVER_BUFFERED_DATA",
513
        [ MAINTENANCE_IN           ] = "MAINTENANCE_IN",
514
        [ MAINTENANCE_OUT          ] = "MAINTENANCE_OUT",
515
        [ MODE_SELECT              ] = "MODE_SELECT",
516
        [ RESERVE                  ] = "RESERVE",
517
        [ RELEASE                  ] = "RELEASE",
518
        [ COPY                     ] = "COPY",
519
        [ ERASE                    ] = "ERASE",
520
        [ MODE_SENSE               ] = "MODE_SENSE",
521
        [ START_STOP               ] = "START_STOP",
522
        [ RECEIVE_DIAGNOSTIC       ] = "RECEIVE_DIAGNOSTIC",
523
        [ SEND_DIAGNOSTIC          ] = "SEND_DIAGNOSTIC",
524
        [ ALLOW_MEDIUM_REMOVAL     ] = "ALLOW_MEDIUM_REMOVAL",
525

    
526
        [ SET_WINDOW               ] = "SET_WINDOW",
527
        [ READ_CAPACITY            ] = "READ_CAPACITY",
528
        [ READ_10                  ] = "READ_10",
529
        [ WRITE_10                 ] = "WRITE_10",
530
        [ SEEK_10                  ] = "SEEK_10",
531
        [ WRITE_VERIFY             ] = "WRITE_VERIFY",
532
        [ VERIFY                   ] = "VERIFY",
533
        [ SEARCH_HIGH              ] = "SEARCH_HIGH",
534
        [ SEARCH_EQUAL             ] = "SEARCH_EQUAL",
535
        [ SEARCH_LOW               ] = "SEARCH_LOW",
536
        [ SET_LIMITS               ] = "SET_LIMITS",
537
        [ PRE_FETCH                ] = "PRE_FETCH",
538
        /* READ_POSITION and PRE_FETCH use the same operation code */
539
        [ SYNCHRONIZE_CACHE        ] = "SYNCHRONIZE_CACHE",
540
        [ LOCK_UNLOCK_CACHE        ] = "LOCK_UNLOCK_CACHE",
541
        [ READ_DEFECT_DATA         ] = "READ_DEFECT_DATA",
542
        [ MEDIUM_SCAN              ] = "MEDIUM_SCAN",
543
        [ COMPARE                  ] = "COMPARE",
544
        [ COPY_VERIFY              ] = "COPY_VERIFY",
545
        [ WRITE_BUFFER             ] = "WRITE_BUFFER",
546
        [ READ_BUFFER              ] = "READ_BUFFER",
547
        [ UPDATE_BLOCK             ] = "UPDATE_BLOCK",
548
        [ READ_LONG                ] = "READ_LONG",
549
        [ WRITE_LONG               ] = "WRITE_LONG",
550
        [ CHANGE_DEFINITION        ] = "CHANGE_DEFINITION",
551
        [ WRITE_SAME               ] = "WRITE_SAME",
552
        [ READ_TOC                 ] = "READ_TOC",
553
        [ LOG_SELECT               ] = "LOG_SELECT",
554
        [ LOG_SENSE                ] = "LOG_SENSE",
555
        [ MODE_SELECT_10           ] = "MODE_SELECT_10",
556
        [ RESERVE_10               ] = "RESERVE_10",
557
        [ RELEASE_10               ] = "RELEASE_10",
558
        [ MODE_SENSE_10            ] = "MODE_SENSE_10",
559
        [ PERSISTENT_RESERVE_IN    ] = "PERSISTENT_RESERVE_IN",
560
        [ PERSISTENT_RESERVE_OUT   ] = "PERSISTENT_RESERVE_OUT",
561
        [ MOVE_MEDIUM              ] = "MOVE_MEDIUM",
562
        [ READ_12                  ] = "READ_12",
563
        [ WRITE_12                 ] = "WRITE_12",
564
        [ WRITE_VERIFY_12          ] = "WRITE_VERIFY_12",
565
        [ SEARCH_HIGH_12           ] = "SEARCH_HIGH_12",
566
        [ SEARCH_EQUAL_12          ] = "SEARCH_EQUAL_12",
567
        [ SEARCH_LOW_12            ] = "SEARCH_LOW_12",
568
        [ READ_ELEMENT_STATUS      ] = "READ_ELEMENT_STATUS",
569
        [ SEND_VOLUME_TAG          ] = "SEND_VOLUME_TAG",
570
        [ WRITE_LONG_2             ] = "WRITE_LONG_2",
571

    
572
        [ REPORT_DENSITY_SUPPORT   ] = "REPORT_DENSITY_SUPPORT",
573
        [ GET_CONFIGURATION        ] = "GET_CONFIGURATION",
574
        [ READ_16                  ] = "READ_16",
575
        [ WRITE_16                 ] = "WRITE_16",
576
        [ WRITE_VERIFY_16          ] = "WRITE_VERIFY_16",
577
        [ SERVICE_ACTION_IN        ] = "SERVICE_ACTION_IN",
578
        [ REPORT_LUNS              ] = "REPORT_LUNS",
579
        [ LOAD_UNLOAD              ] = "LOAD_UNLOAD",
580
        [ SET_CD_SPEED             ] = "SET_CD_SPEED",
581
        [ BLANK                    ] = "BLANK",
582
    };
583

    
584
    if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
585
        return "*UNKNOWN*";
586
    return names[cmd];
587
}
588

    
589
SCSIRequest *scsi_req_ref(SCSIRequest *req)
590
{
591
    req->refcount++;
592
    return req;
593
}
594

    
595
void scsi_req_unref(SCSIRequest *req)
596
{
597
    if (--req->refcount == 0) {
598
        if (req->dev->info->free_req) {
599
            req->dev->info->free_req(req);
600
        }
601
        qemu_free(req);
602
    }
603
}
604

    
605
/* Tell the device that we finished processing this chunk of I/O.  It
606
   will start the next chunk or complete the command.  */
607
void scsi_req_continue(SCSIRequest *req)
608
{
609
    trace_scsi_req_continue(req->dev->id, req->lun, req->tag);
610
    if (req->cmd.mode == SCSI_XFER_TO_DEV) {
611
        req->dev->info->write_data(req);
612
    } else {
613
        req->dev->info->read_data(req);
614
    }
615
}
616

    
617
/* Called by the devices when data is ready for the HBA.  The HBA should
618
   start a DMA operation to read or fill the device's data buffer.
619
   Once it completes, calling scsi_req_continue will restart I/O.  */
620
void scsi_req_data(SCSIRequest *req, int len)
621
{
622
    trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
623
    req->bus->ops->complete(req, SCSI_REASON_DATA, len);
624
}
625

    
626
void scsi_req_print(SCSIRequest *req)
627
{
628
    FILE *fp = stderr;
629
    int i;
630

    
631
    fprintf(fp, "[%s id=%d] %s",
632
            req->dev->qdev.parent_bus->name,
633
            req->dev->id,
634
            scsi_command_name(req->cmd.buf[0]));
635
    for (i = 1; i < req->cmd.len; i++) {
636
        fprintf(fp, " 0x%02x", req->cmd.buf[i]);
637
    }
638
    switch (req->cmd.mode) {
639
    case SCSI_XFER_NONE:
640
        fprintf(fp, " - none\n");
641
        break;
642
    case SCSI_XFER_FROM_DEV:
643
        fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
644
        break;
645
    case SCSI_XFER_TO_DEV:
646
        fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
647
        break;
648
    default:
649
        fprintf(fp, " - Oops\n");
650
        break;
651
    }
652
}
653

    
654
void scsi_req_complete(SCSIRequest *req)
655
{
656
    assert(req->status != -1);
657
    scsi_req_ref(req);
658
    scsi_req_dequeue(req);
659
    req->bus->ops->complete(req, SCSI_REASON_DONE, req->status);
660
    scsi_req_unref(req);
661
}
662

    
663
void scsi_req_cancel(SCSIRequest *req)
664
{
665
    if (req->dev && req->dev->info->cancel_io) {
666
        req->dev->info->cancel_io(req);
667
    }
668
    scsi_req_ref(req);
669
    scsi_req_dequeue(req);
670
    if (req->bus->ops->cancel) {
671
        req->bus->ops->cancel(req);
672
    }
673
    scsi_req_unref(req);
674
}
675

    
676
void scsi_req_abort(SCSIRequest *req, int status)
677
{
678
    req->status = status;
679
    if (req->dev && req->dev->info->cancel_io) {
680
        req->dev->info->cancel_io(req);
681
    }
682
    scsi_req_complete(req);
683
}
684

    
685
void scsi_device_purge_requests(SCSIDevice *sdev)
686
{
687
    SCSIRequest *req;
688

    
689
    while (!QTAILQ_EMPTY(&sdev->requests)) {
690
        req = QTAILQ_FIRST(&sdev->requests);
691
        scsi_req_cancel(req);
692
    }
693
}
694

    
695
static char *scsibus_get_fw_dev_path(DeviceState *dev)
696
{
697
    SCSIDevice *d = (SCSIDevice*)dev;
698
    SCSIBus *bus = scsi_bus_from_device(d);
699
    char path[100];
700
    int i;
701

    
702
    for (i = 0; i < bus->ndev; i++) {
703
        if (bus->devs[i] == d) {
704
            break;
705
        }
706
    }
707

    
708
    assert(i != bus->ndev);
709

    
710
    snprintf(path, sizeof(path), "%s@%x", qdev_fw_name(dev), i);
711

    
712
    return strdup(path);
713
}