Statistics
| Branch: | Revision:

root / hw / scsi-bus.c @ ab9adc88

History | View | Annotate | Download (16.3 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
                  scsi_completionfn complete)
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->complete = complete;
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->bus = scsi_bus_from_device(d);
140
    req->dev = d;
141
    req->tag = tag;
142
    req->lun = lun;
143
    req->status = -1;
144
    req->enqueued = true;
145
    trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
146
    QTAILQ_INSERT_TAIL(&d->requests, req, next);
147
    return req;
148
}
149

    
150
SCSIRequest *scsi_req_find(SCSIDevice *d, uint32_t tag)
151
{
152
    SCSIRequest *req;
153

    
154
    QTAILQ_FOREACH(req, &d->requests, next) {
155
        if (req->tag == tag) {
156
            return req;
157
        }
158
    }
159
    return NULL;
160
}
161

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

    
171
void scsi_req_free(SCSIRequest *req)
172
{
173
    scsi_req_dequeue(req);
174
    qemu_free(req);
175
}
176

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

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

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

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

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

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

    
380
    }
381
    return lba;
382
}
383

    
384
int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
385
{
386
    int rc;
387

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

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

    
404
static const char *scsi_command_name(uint8_t cmd)
405
{
406
    static const char *names[] = {
407
        [ TEST_UNIT_READY          ] = "TEST_UNIT_READY",
408
        [ REZERO_UNIT              ] = "REZERO_UNIT",
409
        /* REWIND and REZERO_UNIT use the same operation code */
410
        [ REQUEST_SENSE            ] = "REQUEST_SENSE",
411
        [ FORMAT_UNIT              ] = "FORMAT_UNIT",
412
        [ READ_BLOCK_LIMITS        ] = "READ_BLOCK_LIMITS",
413
        [ REASSIGN_BLOCKS          ] = "REASSIGN_BLOCKS",
414
        [ READ_6                   ] = "READ_6",
415
        [ WRITE_6                  ] = "WRITE_6",
416
        [ SEEK_6                   ] = "SEEK_6",
417
        [ READ_REVERSE             ] = "READ_REVERSE",
418
        [ WRITE_FILEMARKS          ] = "WRITE_FILEMARKS",
419
        [ SPACE                    ] = "SPACE",
420
        [ INQUIRY                  ] = "INQUIRY",
421
        [ RECOVER_BUFFERED_DATA    ] = "RECOVER_BUFFERED_DATA",
422
        [ MAINTENANCE_IN           ] = "MAINTENANCE_IN",
423
        [ MAINTENANCE_OUT          ] = "MAINTENANCE_OUT",
424
        [ MODE_SELECT              ] = "MODE_SELECT",
425
        [ RESERVE                  ] = "RESERVE",
426
        [ RELEASE                  ] = "RELEASE",
427
        [ COPY                     ] = "COPY",
428
        [ ERASE                    ] = "ERASE",
429
        [ MODE_SENSE               ] = "MODE_SENSE",
430
        [ START_STOP               ] = "START_STOP",
431
        [ RECEIVE_DIAGNOSTIC       ] = "RECEIVE_DIAGNOSTIC",
432
        [ SEND_DIAGNOSTIC          ] = "SEND_DIAGNOSTIC",
433
        [ ALLOW_MEDIUM_REMOVAL     ] = "ALLOW_MEDIUM_REMOVAL",
434

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

    
481
        [ REPORT_DENSITY_SUPPORT   ] = "REPORT_DENSITY_SUPPORT",
482
        [ GET_CONFIGURATION        ] = "GET_CONFIGURATION",
483
        [ READ_16                  ] = "READ_16",
484
        [ WRITE_16                 ] = "WRITE_16",
485
        [ WRITE_VERIFY_16          ] = "WRITE_VERIFY_16",
486
        [ SERVICE_ACTION_IN        ] = "SERVICE_ACTION_IN",
487
        [ REPORT_LUNS              ] = "REPORT_LUNS",
488
        [ LOAD_UNLOAD              ] = "LOAD_UNLOAD",
489
        [ SET_CD_SPEED             ] = "SET_CD_SPEED",
490
        [ BLANK                    ] = "BLANK",
491
    };
492

    
493
    if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
494
        return "*UNKNOWN*";
495
    return names[cmd];
496
}
497

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

    
509
void scsi_req_print(SCSIRequest *req)
510
{
511
    FILE *fp = stderr;
512
    int i;
513

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

    
537
void scsi_req_complete(SCSIRequest *req)
538
{
539
    assert(req->status != -1);
540
    scsi_req_dequeue(req);
541
    req->bus->complete(req->bus, SCSI_REASON_DONE,
542
                       req->tag,
543
                       req->status);
544
}
545

    
546
static char *scsibus_get_fw_dev_path(DeviceState *dev)
547
{
548
    SCSIDevice *d = (SCSIDevice*)dev;
549
    SCSIBus *bus = scsi_bus_from_device(d);
550
    char path[100];
551
    int i;
552

    
553
    for (i = 0; i < bus->ndev; i++) {
554
        if (bus->devs[i] == d) {
555
            break;
556
        }
557
    }
558

    
559
    assert(i != bus->ndev);
560

    
561
    snprintf(path, sizeof(path), "%s@%x", qdev_fw_name(dev), i);
562

    
563
    return strdup(path);
564
}