Statistics
| Branch: | Revision:

root / hw / scsi-bus.c @ 94d3f98a

History | View | Annotate | Download (17.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
void scsi_req_enqueue(SCSIRequest *req)
150
{
151
    assert(!req->enqueued);
152
    scsi_req_ref(req);
153
    req->enqueued = true;
154
    QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
155
}
156

    
157
void scsi_req_dequeue(SCSIRequest *req)
158
{
159
    trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
160
    if (req->enqueued) {
161
        QTAILQ_REMOVE(&req->dev->requests, req, next);
162
        req->enqueued = false;
163
        scsi_req_unref(req);
164
    }
165
}
166

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

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

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

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

    
342
static uint64_t scsi_req_lba(SCSIRequest *req)
343
{
344
    uint8_t *buf = req->cmd.buf;
345
    uint64_t lba;
346

    
347
    switch (buf[0] >> 5) {
348
    case 0:
349
        lba = (uint64_t) buf[3] | ((uint64_t) buf[2] << 8) |
350
              (((uint64_t) buf[1] & 0x1f) << 16);
351
        break;
352
    case 1:
353
    case 2:
354
        lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
355
              ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
356
        break;
357
    case 4:
358
        lba = (uint64_t) buf[9] | ((uint64_t) buf[8] << 8) |
359
              ((uint64_t) buf[7] << 16) | ((uint64_t) buf[6] << 24) |
360
              ((uint64_t) buf[5] << 32) | ((uint64_t) buf[4] << 40) |
361
              ((uint64_t) buf[3] << 48) | ((uint64_t) buf[2] << 56);
362
        break;
363
    case 5:
364
        lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
365
              ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
366
        break;
367
    default:
368
        lba = -1;
369

    
370
    }
371
    return lba;
372
}
373

    
374
int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
375
{
376
    int rc;
377

    
378
    if (req->dev->type == TYPE_TAPE) {
379
        rc = scsi_req_stream_length(req, buf);
380
    } else {
381
        rc = scsi_req_length(req, buf);
382
    }
383
    if (rc != 0)
384
        return rc;
385

    
386
    memcpy(req->cmd.buf, buf, req->cmd.len);
387
    scsi_req_xfer_mode(req);
388
    req->cmd.lba = scsi_req_lba(req);
389
    trace_scsi_req_parsed(req->dev->id, req->lun, req->tag, buf[0],
390
                          req->cmd.mode, req->cmd.xfer, req->cmd.lba);
391
    return 0;
392
}
393

    
394
static const char *scsi_command_name(uint8_t cmd)
395
{
396
    static const char *names[] = {
397
        [ TEST_UNIT_READY          ] = "TEST_UNIT_READY",
398
        [ REZERO_UNIT              ] = "REZERO_UNIT",
399
        /* REWIND and REZERO_UNIT use the same operation code */
400
        [ REQUEST_SENSE            ] = "REQUEST_SENSE",
401
        [ FORMAT_UNIT              ] = "FORMAT_UNIT",
402
        [ READ_BLOCK_LIMITS        ] = "READ_BLOCK_LIMITS",
403
        [ REASSIGN_BLOCKS          ] = "REASSIGN_BLOCKS",
404
        [ READ_6                   ] = "READ_6",
405
        [ WRITE_6                  ] = "WRITE_6",
406
        [ SEEK_6                   ] = "SEEK_6",
407
        [ READ_REVERSE             ] = "READ_REVERSE",
408
        [ WRITE_FILEMARKS          ] = "WRITE_FILEMARKS",
409
        [ SPACE                    ] = "SPACE",
410
        [ INQUIRY                  ] = "INQUIRY",
411
        [ RECOVER_BUFFERED_DATA    ] = "RECOVER_BUFFERED_DATA",
412
        [ MAINTENANCE_IN           ] = "MAINTENANCE_IN",
413
        [ MAINTENANCE_OUT          ] = "MAINTENANCE_OUT",
414
        [ MODE_SELECT              ] = "MODE_SELECT",
415
        [ RESERVE                  ] = "RESERVE",
416
        [ RELEASE                  ] = "RELEASE",
417
        [ COPY                     ] = "COPY",
418
        [ ERASE                    ] = "ERASE",
419
        [ MODE_SENSE               ] = "MODE_SENSE",
420
        [ START_STOP               ] = "START_STOP",
421
        [ RECEIVE_DIAGNOSTIC       ] = "RECEIVE_DIAGNOSTIC",
422
        [ SEND_DIAGNOSTIC          ] = "SEND_DIAGNOSTIC",
423
        [ ALLOW_MEDIUM_REMOVAL     ] = "ALLOW_MEDIUM_REMOVAL",
424

    
425
        [ SET_WINDOW               ] = "SET_WINDOW",
426
        [ READ_CAPACITY            ] = "READ_CAPACITY",
427
        [ READ_10                  ] = "READ_10",
428
        [ WRITE_10                 ] = "WRITE_10",
429
        [ SEEK_10                  ] = "SEEK_10",
430
        [ WRITE_VERIFY             ] = "WRITE_VERIFY",
431
        [ VERIFY                   ] = "VERIFY",
432
        [ SEARCH_HIGH              ] = "SEARCH_HIGH",
433
        [ SEARCH_EQUAL             ] = "SEARCH_EQUAL",
434
        [ SEARCH_LOW               ] = "SEARCH_LOW",
435
        [ SET_LIMITS               ] = "SET_LIMITS",
436
        [ PRE_FETCH                ] = "PRE_FETCH",
437
        /* READ_POSITION and PRE_FETCH use the same operation code */
438
        [ SYNCHRONIZE_CACHE        ] = "SYNCHRONIZE_CACHE",
439
        [ LOCK_UNLOCK_CACHE        ] = "LOCK_UNLOCK_CACHE",
440
        [ READ_DEFECT_DATA         ] = "READ_DEFECT_DATA",
441
        [ MEDIUM_SCAN              ] = "MEDIUM_SCAN",
442
        [ COMPARE                  ] = "COMPARE",
443
        [ COPY_VERIFY              ] = "COPY_VERIFY",
444
        [ WRITE_BUFFER             ] = "WRITE_BUFFER",
445
        [ READ_BUFFER              ] = "READ_BUFFER",
446
        [ UPDATE_BLOCK             ] = "UPDATE_BLOCK",
447
        [ READ_LONG                ] = "READ_LONG",
448
        [ WRITE_LONG               ] = "WRITE_LONG",
449
        [ CHANGE_DEFINITION        ] = "CHANGE_DEFINITION",
450
        [ WRITE_SAME               ] = "WRITE_SAME",
451
        [ READ_TOC                 ] = "READ_TOC",
452
        [ LOG_SELECT               ] = "LOG_SELECT",
453
        [ LOG_SENSE                ] = "LOG_SENSE",
454
        [ MODE_SELECT_10           ] = "MODE_SELECT_10",
455
        [ RESERVE_10               ] = "RESERVE_10",
456
        [ RELEASE_10               ] = "RELEASE_10",
457
        [ MODE_SENSE_10            ] = "MODE_SENSE_10",
458
        [ PERSISTENT_RESERVE_IN    ] = "PERSISTENT_RESERVE_IN",
459
        [ PERSISTENT_RESERVE_OUT   ] = "PERSISTENT_RESERVE_OUT",
460
        [ MOVE_MEDIUM              ] = "MOVE_MEDIUM",
461
        [ READ_12                  ] = "READ_12",
462
        [ WRITE_12                 ] = "WRITE_12",
463
        [ WRITE_VERIFY_12          ] = "WRITE_VERIFY_12",
464
        [ SEARCH_HIGH_12           ] = "SEARCH_HIGH_12",
465
        [ SEARCH_EQUAL_12          ] = "SEARCH_EQUAL_12",
466
        [ SEARCH_LOW_12            ] = "SEARCH_LOW_12",
467
        [ READ_ELEMENT_STATUS      ] = "READ_ELEMENT_STATUS",
468
        [ SEND_VOLUME_TAG          ] = "SEND_VOLUME_TAG",
469
        [ WRITE_LONG_2             ] = "WRITE_LONG_2",
470

    
471
        [ REPORT_DENSITY_SUPPORT   ] = "REPORT_DENSITY_SUPPORT",
472
        [ GET_CONFIGURATION        ] = "GET_CONFIGURATION",
473
        [ READ_16                  ] = "READ_16",
474
        [ WRITE_16                 ] = "WRITE_16",
475
        [ WRITE_VERIFY_16          ] = "WRITE_VERIFY_16",
476
        [ SERVICE_ACTION_IN        ] = "SERVICE_ACTION_IN",
477
        [ REPORT_LUNS              ] = "REPORT_LUNS",
478
        [ LOAD_UNLOAD              ] = "LOAD_UNLOAD",
479
        [ SET_CD_SPEED             ] = "SET_CD_SPEED",
480
        [ BLANK                    ] = "BLANK",
481
    };
482

    
483
    if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
484
        return "*UNKNOWN*";
485
    return names[cmd];
486
}
487

    
488
SCSIRequest *scsi_req_ref(SCSIRequest *req)
489
{
490
    req->refcount++;
491
    return req;
492
}
493

    
494
void scsi_req_unref(SCSIRequest *req)
495
{
496
    if (--req->refcount == 0) {
497
        if (req->dev->info->free_req) {
498
            req->dev->info->free_req(req);
499
        }
500
        qemu_free(req);
501
    }
502
}
503

    
504
/* Called by the devices when data is ready for the HBA.  The HBA should
505
   start a DMA operation to read or fill the device's data buffer.
506
   Once it completes, calling one of req->dev->info->read_data or
507
   req->dev->info->write_data (depending on the direction of the
508
   transfer) will restart I/O.  */
509
void scsi_req_data(SCSIRequest *req, int len)
510
{
511
    trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
512
    req->bus->ops->complete(req, SCSI_REASON_DATA, len);
513
}
514

    
515
void scsi_req_print(SCSIRequest *req)
516
{
517
    FILE *fp = stderr;
518
    int i;
519

    
520
    fprintf(fp, "[%s id=%d] %s",
521
            req->dev->qdev.parent_bus->name,
522
            req->dev->id,
523
            scsi_command_name(req->cmd.buf[0]));
524
    for (i = 1; i < req->cmd.len; i++) {
525
        fprintf(fp, " 0x%02x", req->cmd.buf[i]);
526
    }
527
    switch (req->cmd.mode) {
528
    case SCSI_XFER_NONE:
529
        fprintf(fp, " - none\n");
530
        break;
531
    case SCSI_XFER_FROM_DEV:
532
        fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
533
        break;
534
    case SCSI_XFER_TO_DEV:
535
        fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
536
        break;
537
    default:
538
        fprintf(fp, " - Oops\n");
539
        break;
540
    }
541
}
542

    
543
void scsi_req_complete(SCSIRequest *req)
544
{
545
    assert(req->status != -1);
546
    scsi_req_ref(req);
547
    scsi_req_dequeue(req);
548
    req->bus->ops->complete(req, SCSI_REASON_DONE, req->status);
549
    scsi_req_unref(req);
550
}
551

    
552
void scsi_req_cancel(SCSIRequest *req)
553
{
554
    if (req->dev && req->dev->info->cancel_io) {
555
        req->dev->info->cancel_io(req);
556
    }
557
    scsi_req_ref(req);
558
    scsi_req_dequeue(req);
559
    if (req->bus->ops->cancel) {
560
        req->bus->ops->cancel(req);
561
    }
562
    scsi_req_unref(req);
563
}
564

    
565
void scsi_req_abort(SCSIRequest *req, int status)
566
{
567
    req->status = status;
568
    if (req->dev && req->dev->info->cancel_io) {
569
        req->dev->info->cancel_io(req);
570
    }
571
    scsi_req_complete(req);
572
}
573

    
574
void scsi_device_purge_requests(SCSIDevice *sdev)
575
{
576
    SCSIRequest *req;
577

    
578
    while (!QTAILQ_EMPTY(&sdev->requests)) {
579
        req = QTAILQ_FIRST(&sdev->requests);
580
        scsi_req_cancel(req);
581
    }
582
}
583

    
584
static char *scsibus_get_fw_dev_path(DeviceState *dev)
585
{
586
    SCSIDevice *d = (SCSIDevice*)dev;
587
    SCSIBus *bus = scsi_bus_from_device(d);
588
    char path[100];
589
    int i;
590

    
591
    for (i = 0; i < bus->ndev; i++) {
592
        if (bus->devs[i] == d) {
593
            break;
594
        }
595
    }
596

    
597
    assert(i != bus->ndev);
598

    
599
    snprintf(path, sizeof(path), "%s@%x", qdev_fw_name(dev), i);
600

    
601
    return strdup(path);
602
}