Statistics
| Branch: | Revision:

root / hw / scsi-bus.c @ fc4f0754

History | View | Annotate | Download (19.5 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
int32_t scsi_req_enqueue(SCSIRequest *req, uint8_t *buf)
150
{
151
    int32_t rc;
152

    
153
    assert(!req->enqueued);
154
    scsi_req_ref(req);
155
    req->enqueued = true;
156
    QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
157

    
158
    scsi_req_ref(req);
159
    rc = req->dev->info->send_command(req, buf);
160
    scsi_req_unref(req);
161
    return rc;
162
}
163

    
164
static void scsi_req_dequeue(SCSIRequest *req)
165
{
166
    trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
167
    if (req->enqueued) {
168
        QTAILQ_REMOVE(&req->dev->requests, req, next);
169
        req->enqueued = false;
170
        scsi_req_unref(req);
171
    }
172
}
173

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

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

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

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

    
349
static uint64_t scsi_req_lba(SCSIRequest *req)
350
{
351
    uint8_t *buf = req->cmd.buf;
352
    uint64_t lba;
353

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

    
377
    }
378
    return lba;
379
}
380

    
381
int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
382
{
383
    int rc;
384

    
385
    if (req->dev->type == TYPE_TAPE) {
386
        rc = scsi_req_stream_length(req, buf);
387
    } else {
388
        rc = scsi_req_length(req, buf);
389
    }
390
    if (rc != 0)
391
        return rc;
392

    
393
    memcpy(req->cmd.buf, buf, req->cmd.len);
394
    scsi_req_xfer_mode(req);
395
    req->cmd.lba = scsi_req_lba(req);
396
    trace_scsi_req_parsed(req->dev->id, req->lun, req->tag, buf[0],
397
                          req->cmd.mode, req->cmd.xfer, req->cmd.lba);
398
    return 0;
399
}
400

    
401
/*
402
 * Predefined sense codes
403
 */
404

    
405
/* No sense data available */
406
const struct SCSISense sense_code_NO_SENSE = {
407
    .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00
408
};
409

    
410
/* LUN not ready, Manual intervention required */
411
const struct SCSISense sense_code_LUN_NOT_READY = {
412
    .key = NOT_READY, .asc = 0x04, .ascq = 0x03
413
};
414

    
415
/* LUN not ready, Medium not present */
416
const struct SCSISense sense_code_NO_MEDIUM = {
417
    .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
418
};
419

    
420
/* Hardware error, internal target failure */
421
const struct SCSISense sense_code_TARGET_FAILURE = {
422
    .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
423
};
424

    
425
/* Illegal request, invalid command operation code */
426
const struct SCSISense sense_code_INVALID_OPCODE = {
427
    .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00
428
};
429

    
430
/* Illegal request, LBA out of range */
431
const struct SCSISense sense_code_LBA_OUT_OF_RANGE = {
432
    .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00
433
};
434

    
435
/* Illegal request, Invalid field in CDB */
436
const struct SCSISense sense_code_INVALID_FIELD = {
437
    .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00
438
};
439

    
440
/* Illegal request, LUN not supported */
441
const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
442
    .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
443
};
444

    
445
/* Command aborted, I/O process terminated */
446
const struct SCSISense sense_code_IO_ERROR = {
447
    .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
448
};
449

    
450
/* Command aborted, I_T Nexus loss occurred */
451
const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
452
    .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
453
};
454

    
455
/* Command aborted, Logical Unit failure */
456
const struct SCSISense sense_code_LUN_FAILURE = {
457
    .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
458
};
459

    
460
/*
461
 * scsi_build_sense
462
 *
463
 * Build a sense buffer
464
 */
465
int scsi_build_sense(SCSISense sense, uint8_t *buf, int len, int fixed)
466
{
467
    if (!fixed && len < 8) {
468
        return 0;
469
    }
470

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

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

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

    
567
        [ REPORT_DENSITY_SUPPORT   ] = "REPORT_DENSITY_SUPPORT",
568
        [ GET_CONFIGURATION        ] = "GET_CONFIGURATION",
569
        [ READ_16                  ] = "READ_16",
570
        [ WRITE_16                 ] = "WRITE_16",
571
        [ WRITE_VERIFY_16          ] = "WRITE_VERIFY_16",
572
        [ SERVICE_ACTION_IN        ] = "SERVICE_ACTION_IN",
573
        [ REPORT_LUNS              ] = "REPORT_LUNS",
574
        [ LOAD_UNLOAD              ] = "LOAD_UNLOAD",
575
        [ SET_CD_SPEED             ] = "SET_CD_SPEED",
576
        [ BLANK                    ] = "BLANK",
577
    };
578

    
579
    if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
580
        return "*UNKNOWN*";
581
    return names[cmd];
582
}
583

    
584
SCSIRequest *scsi_req_ref(SCSIRequest *req)
585
{
586
    req->refcount++;
587
    return req;
588
}
589

    
590
void scsi_req_unref(SCSIRequest *req)
591
{
592
    if (--req->refcount == 0) {
593
        if (req->dev->info->free_req) {
594
            req->dev->info->free_req(req);
595
        }
596
        qemu_free(req);
597
    }
598
}
599

    
600
/* Called by the devices when data is ready for the HBA.  The HBA should
601
   start a DMA operation to read or fill the device's data buffer.
602
   Once it completes, calling one of req->dev->info->read_data or
603
   req->dev->info->write_data (depending on the direction of the
604
   transfer) will restart I/O.  */
605
void scsi_req_data(SCSIRequest *req, int len)
606
{
607
    trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
608
    req->bus->ops->complete(req, SCSI_REASON_DATA, len);
609
}
610

    
611
void scsi_req_print(SCSIRequest *req)
612
{
613
    FILE *fp = stderr;
614
    int i;
615

    
616
    fprintf(fp, "[%s id=%d] %s",
617
            req->dev->qdev.parent_bus->name,
618
            req->dev->id,
619
            scsi_command_name(req->cmd.buf[0]));
620
    for (i = 1; i < req->cmd.len; i++) {
621
        fprintf(fp, " 0x%02x", req->cmd.buf[i]);
622
    }
623
    switch (req->cmd.mode) {
624
    case SCSI_XFER_NONE:
625
        fprintf(fp, " - none\n");
626
        break;
627
    case SCSI_XFER_FROM_DEV:
628
        fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
629
        break;
630
    case SCSI_XFER_TO_DEV:
631
        fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
632
        break;
633
    default:
634
        fprintf(fp, " - Oops\n");
635
        break;
636
    }
637
}
638

    
639
void scsi_req_complete(SCSIRequest *req)
640
{
641
    assert(req->status != -1);
642
    scsi_req_ref(req);
643
    scsi_req_dequeue(req);
644
    req->bus->ops->complete(req, SCSI_REASON_DONE, req->status);
645
    scsi_req_unref(req);
646
}
647

    
648
void scsi_req_cancel(SCSIRequest *req)
649
{
650
    if (req->dev && req->dev->info->cancel_io) {
651
        req->dev->info->cancel_io(req);
652
    }
653
    scsi_req_ref(req);
654
    scsi_req_dequeue(req);
655
    if (req->bus->ops->cancel) {
656
        req->bus->ops->cancel(req);
657
    }
658
    scsi_req_unref(req);
659
}
660

    
661
void scsi_req_abort(SCSIRequest *req, int status)
662
{
663
    req->status = status;
664
    if (req->dev && req->dev->info->cancel_io) {
665
        req->dev->info->cancel_io(req);
666
    }
667
    scsi_req_complete(req);
668
}
669

    
670
void scsi_device_purge_requests(SCSIDevice *sdev)
671
{
672
    SCSIRequest *req;
673

    
674
    while (!QTAILQ_EMPTY(&sdev->requests)) {
675
        req = QTAILQ_FIRST(&sdev->requests);
676
        scsi_req_cancel(req);
677
    }
678
}
679

    
680
static char *scsibus_get_fw_dev_path(DeviceState *dev)
681
{
682
    SCSIDevice *d = (SCSIDevice*)dev;
683
    SCSIBus *bus = scsi_bus_from_device(d);
684
    char path[100];
685
    int i;
686

    
687
    for (i = 0; i < bus->ndev; i++) {
688
        if (bus->devs[i] == d) {
689
            break;
690
        }
691
    }
692

    
693
    assert(i != bus->ndev);
694

    
695
    snprintf(path, sizeof(path), "%s@%x", qdev_fw_name(dev), i);
696

    
697
    return strdup(path);
698
}