Statistics
| Branch: | Revision:

root / hw / scsi-bus.c @ 39ec9a50

History | View | Annotate | Download (13.9 kB)

1
#include "hw.h"
2
#include "sysemu.h"
3
#include "scsi.h"
4
#include "scsi-defs.h"
5
#include "block.h"
6
#include "qdev.h"
7

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

    
18
/* Create a scsi bus, and attach devices to it.  */
19
void scsi_bus_new(SCSIBus *bus, DeviceState *host, int tcq, int ndev,
20
                  scsi_completionfn complete)
21
{
22
    qbus_create_inplace(&bus->qbus, &scsi_bus_info, host, NULL);
23
    bus->busnr = next_scsi_bus++;
24
    bus->tcq = tcq;
25
    bus->ndev = ndev;
26
    bus->complete = complete;
27
    bus->qbus.allow_hotplug = 1;
28
}
29

    
30
static int scsi_qdev_init(DeviceState *qdev, DeviceInfo *base)
31
{
32
    SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
33
    SCSIDeviceInfo *info = DO_UPCAST(SCSIDeviceInfo, qdev, base);
34
    SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
35
    int rc = -1;
36

    
37
    if (dev->id == -1) {
38
        for (dev->id = 0; dev->id < bus->ndev; dev->id++) {
39
            if (bus->devs[dev->id] == NULL)
40
                break;
41
        }
42
    }
43
    if (dev->id >= bus->ndev) {
44
        qemu_error("bad scsi device id: %d\n", dev->id);
45
        goto err;
46
    }
47

    
48
    if (bus->devs[dev->id]) {
49
        qdev_free(&bus->devs[dev->id]->qdev);
50
    }
51
    bus->devs[dev->id] = dev;
52

    
53
    dev->info = info;
54
    QTAILQ_INIT(&dev->requests);
55
    rc = dev->info->init(dev);
56
    if (rc != 0) {
57
        bus->devs[dev->id] = NULL;
58
    }
59

    
60
err:
61
    return rc;
62
}
63

    
64
static int scsi_qdev_exit(DeviceState *qdev)
65
{
66
    SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
67
    SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
68

    
69
    assert(bus->devs[dev->id] != NULL);
70
    if (bus->devs[dev->id]->info->destroy) {
71
        bus->devs[dev->id]->info->destroy(bus->devs[dev->id]);
72
    }
73
    bus->devs[dev->id] = NULL;
74
    return 0;
75
}
76

    
77
void scsi_qdev_register(SCSIDeviceInfo *info)
78
{
79
    info->qdev.bus_info = &scsi_bus_info;
80
    info->qdev.init     = scsi_qdev_init;
81
    info->qdev.unplug   = qdev_simple_unplug_cb;
82
    info->qdev.exit     = scsi_qdev_exit;
83
    qdev_register(&info->qdev);
84
}
85

    
86
/* handle legacy '-drive if=scsi,...' cmd line args */
87
/* FIXME callers should check for failure, but don't */
88
SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, DriveInfo *dinfo, int unit)
89
{
90
    const char *driver;
91
    DeviceState *dev;
92

    
93
    driver = bdrv_is_sg(dinfo->bdrv) ? "scsi-generic" : "scsi-disk";
94
    dev = qdev_create(&bus->qbus, driver);
95
    qdev_prop_set_uint32(dev, "scsi-id", unit);
96
    qdev_prop_set_drive(dev, "drive", dinfo);
97
    if (qdev_init(dev) < 0)
98
        return NULL;
99
    return DO_UPCAST(SCSIDevice, qdev, dev);
100
}
101

    
102
void scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
103
{
104
    DriveInfo *dinfo;
105
    int unit;
106

    
107
    for (unit = 0; unit < MAX_SCSI_DEVS; unit++) {
108
        dinfo = drive_get(IF_SCSI, bus->busnr, unit);
109
        if (dinfo == NULL) {
110
            continue;
111
        }
112
        scsi_bus_legacy_add_drive(bus, dinfo, unit);
113
    }
114
}
115

    
116
void scsi_dev_clear_sense(SCSIDevice *dev)
117
{
118
    memset(&dev->sense, 0, sizeof(dev->sense));
119
}
120

    
121
void scsi_dev_set_sense(SCSIDevice *dev, uint8_t key)
122
{
123
    dev->sense.key = key;
124
}
125

    
126
SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t lun)
127
{
128
    SCSIRequest *req;
129

    
130
    req = qemu_mallocz(size);
131
    req->bus = scsi_bus_from_device(d);
132
    req->dev = d;
133
    req->tag = tag;
134
    req->lun = lun;
135
    req->status = -1;
136
    QTAILQ_INSERT_TAIL(&d->requests, req, next);
137
    return req;
138
}
139

    
140
SCSIRequest *scsi_req_find(SCSIDevice *d, uint32_t tag)
141
{
142
    SCSIRequest *req;
143

    
144
    QTAILQ_FOREACH(req, &d->requests, next) {
145
        if (req->tag == tag) {
146
            return req;
147
        }
148
    }
149
    return NULL;
150
}
151

    
152
void scsi_req_free(SCSIRequest *req)
153
{
154
    QTAILQ_REMOVE(&req->dev->requests, req, next);
155
    qemu_free(req);
156
}
157

    
158
static int scsi_req_length(SCSIRequest *req, uint8_t *cmd)
159
{
160
    switch (cmd[0] >> 5) {
161
    case 0:
162
        req->cmd.xfer = cmd[4];
163
        req->cmd.len = 6;
164
        /* length 0 means 256 blocks */
165
        if (req->cmd.xfer == 0)
166
            req->cmd.xfer = 256;
167
        break;
168
    case 1:
169
    case 2:
170
        req->cmd.xfer = cmd[8] | (cmd[7] << 8);
171
        req->cmd.len = 10;
172
        break;
173
    case 4:
174
        req->cmd.xfer = cmd[13] | (cmd[12] << 8) | (cmd[11] << 16) | (cmd[10] << 24);
175
        req->cmd.len = 16;
176
        break;
177
    case 5:
178
        req->cmd.xfer = cmd[9] | (cmd[8] << 8) | (cmd[7] << 16) | (cmd[6] << 24);
179
        req->cmd.len = 12;
180
        break;
181
    default:
182
        return -1;
183
    }
184

    
185
    switch(cmd[0]) {
186
    case TEST_UNIT_READY:
187
    case REZERO_UNIT:
188
    case START_STOP:
189
    case SEEK_6:
190
    case WRITE_FILEMARKS:
191
    case SPACE:
192
    case ERASE:
193
    case ALLOW_MEDIUM_REMOVAL:
194
    case VERIFY:
195
    case SEEK_10:
196
    case SYNCHRONIZE_CACHE:
197
    case LOCK_UNLOCK_CACHE:
198
    case LOAD_UNLOAD:
199
    case SET_CD_SPEED:
200
    case SET_LIMITS:
201
    case WRITE_LONG:
202
    case MOVE_MEDIUM:
203
    case UPDATE_BLOCK:
204
        req->cmd.xfer = 0;
205
        break;
206
    case MODE_SENSE:
207
        break;
208
    case WRITE_SAME:
209
        req->cmd.xfer = 1;
210
        break;
211
    case READ_CAPACITY:
212
        req->cmd.xfer = 8;
213
        break;
214
    case READ_BLOCK_LIMITS:
215
        req->cmd.xfer = 6;
216
        break;
217
    case READ_POSITION:
218
        req->cmd.xfer = 20;
219
        break;
220
    case SEND_VOLUME_TAG:
221
        req->cmd.xfer *= 40;
222
        break;
223
    case MEDIUM_SCAN:
224
        req->cmd.xfer *= 8;
225
        break;
226
    case WRITE_10:
227
    case WRITE_VERIFY:
228
    case WRITE_6:
229
    case WRITE_12:
230
    case WRITE_VERIFY_12:
231
        req->cmd.xfer *= req->dev->blocksize;
232
        break;
233
    case READ_10:
234
    case READ_6:
235
    case READ_REVERSE:
236
    case RECOVER_BUFFERED_DATA:
237
    case READ_12:
238
        req->cmd.xfer *= req->dev->blocksize;
239
        break;
240
    case INQUIRY:
241
        req->cmd.xfer = cmd[4] | (cmd[3] << 8);
242
        break;
243
    }
244
    return 0;
245
}
246

    
247
static int scsi_req_stream_length(SCSIRequest *req, uint8_t *cmd)
248
{
249
    switch(cmd[0]) {
250
    /* stream commands */
251
    case READ_6:
252
    case READ_REVERSE:
253
    case RECOVER_BUFFERED_DATA:
254
    case WRITE_6:
255
        req->cmd.len = 6;
256
        req->cmd.xfer = cmd[4] | (cmd[3] << 8) | (cmd[2] << 16);
257
        if (cmd[1] & 0x01) /* fixed */
258
            req->cmd.xfer *= req->dev->blocksize;
259
        break;
260
    case REWIND:
261
    case START_STOP:
262
        req->cmd.len = 6;
263
        req->cmd.xfer = 0;
264
        break;
265
    /* generic commands */
266
    default:
267
        return scsi_req_length(req, cmd);
268
    }
269
    return 0;
270
}
271

    
272
static void scsi_req_xfer_mode(SCSIRequest *req)
273
{
274
    switch (req->cmd.buf[0]) {
275
    case WRITE_6:
276
    case WRITE_10:
277
    case WRITE_VERIFY:
278
    case WRITE_12:
279
    case WRITE_VERIFY_12:
280
    case COPY:
281
    case COPY_VERIFY:
282
    case COMPARE:
283
    case CHANGE_DEFINITION:
284
    case LOG_SELECT:
285
    case MODE_SELECT:
286
    case MODE_SELECT_10:
287
    case SEND_DIAGNOSTIC:
288
    case WRITE_BUFFER:
289
    case FORMAT_UNIT:
290
    case REASSIGN_BLOCKS:
291
    case RESERVE:
292
    case SEARCH_EQUAL:
293
    case SEARCH_HIGH:
294
    case SEARCH_LOW:
295
    case UPDATE_BLOCK:
296
    case WRITE_LONG:
297
    case WRITE_SAME:
298
    case SEARCH_HIGH_12:
299
    case SEARCH_EQUAL_12:
300
    case SEARCH_LOW_12:
301
    case SET_WINDOW:
302
    case MEDIUM_SCAN:
303
    case SEND_VOLUME_TAG:
304
    case WRITE_LONG_2:
305
        req->cmd.mode = SCSI_XFER_TO_DEV;
306
        break;
307
    default:
308
        if (req->cmd.xfer)
309
            req->cmd.mode = SCSI_XFER_FROM_DEV;
310
        else {
311
            req->cmd.mode = SCSI_XFER_NONE;
312
        }
313
        break;
314
    }
315
}
316

    
317
static uint64_t scsi_req_lba(SCSIRequest *req)
318
{
319
    uint8_t *buf = req->cmd.buf;
320
    uint64_t lba;
321

    
322
    switch (buf[0] >> 5) {
323
    case 0:
324
        lba = (uint64_t) buf[3] | ((uint64_t) buf[2] << 8) |
325
              (((uint64_t) buf[1] & 0x1f) << 16);
326
        break;
327
    case 1:
328
    case 2:
329
        lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
330
              ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
331
        break;
332
    case 4:
333
        lba = (uint64_t) buf[9] | ((uint64_t) buf[8] << 8) |
334
              ((uint64_t) buf[7] << 16) | ((uint64_t) buf[6] << 24) |
335
              ((uint64_t) buf[5] << 32) | ((uint64_t) buf[4] << 40) |
336
              ((uint64_t) buf[3] << 48) | ((uint64_t) buf[2] << 56);
337
        break;
338
    case 5:
339
        lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
340
              ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
341
        break;
342
    default:
343
        lba = -1;
344

    
345
    }
346
    return lba;
347
}
348

    
349
int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
350
{
351
    int rc;
352

    
353
    if (req->dev->type == TYPE_TAPE) {
354
        rc = scsi_req_stream_length(req, buf);
355
    } else {
356
        rc = scsi_req_length(req, buf);
357
    }
358
    if (rc != 0)
359
        return rc;
360

    
361
    memcpy(req->cmd.buf, buf, req->cmd.len);
362
    scsi_req_xfer_mode(req);
363
    req->cmd.lba = scsi_req_lba(req);
364
    return 0;
365
}
366

    
367
static const char *scsi_command_name(uint8_t cmd)
368
{
369
    static const char *names[] = {
370
        [ TEST_UNIT_READY          ] = "TEST_UNIT_READY",
371
        [ REZERO_UNIT              ] = "REZERO_UNIT",
372
        [ REQUEST_SENSE            ] = "REQUEST_SENSE",
373
        [ FORMAT_UNIT              ] = "FORMAT_UNIT",
374
        [ READ_BLOCK_LIMITS        ] = "READ_BLOCK_LIMITS",
375
        [ REASSIGN_BLOCKS          ] = "REASSIGN_BLOCKS",
376
        [ READ_6                   ] = "READ_6",
377
        [ WRITE_6                  ] = "WRITE_6",
378
        [ SEEK_6                   ] = "SEEK_6",
379
        [ READ_REVERSE             ] = "READ_REVERSE",
380
        [ WRITE_FILEMARKS          ] = "WRITE_FILEMARKS",
381
        [ SPACE                    ] = "SPACE",
382
        [ INQUIRY                  ] = "INQUIRY",
383
        [ RECOVER_BUFFERED_DATA    ] = "RECOVER_BUFFERED_DATA",
384
        [ MODE_SELECT              ] = "MODE_SELECT",
385
        [ RESERVE                  ] = "RESERVE",
386
        [ RELEASE                  ] = "RELEASE",
387
        [ COPY                     ] = "COPY",
388
        [ ERASE                    ] = "ERASE",
389
        [ MODE_SENSE               ] = "MODE_SENSE",
390
        [ START_STOP               ] = "START_STOP",
391
        [ RECEIVE_DIAGNOSTIC       ] = "RECEIVE_DIAGNOSTIC",
392
        [ SEND_DIAGNOSTIC          ] = "SEND_DIAGNOSTIC",
393
        [ ALLOW_MEDIUM_REMOVAL     ] = "ALLOW_MEDIUM_REMOVAL",
394

    
395
        [ SET_WINDOW               ] = "SET_WINDOW",
396
        [ READ_CAPACITY            ] = "READ_CAPACITY",
397
        [ READ_10                  ] = "READ_10",
398
        [ WRITE_10                 ] = "WRITE_10",
399
        [ SEEK_10                  ] = "SEEK_10",
400
        [ WRITE_VERIFY             ] = "WRITE_VERIFY",
401
        [ VERIFY                   ] = "VERIFY",
402
        [ SEARCH_HIGH              ] = "SEARCH_HIGH",
403
        [ SEARCH_EQUAL             ] = "SEARCH_EQUAL",
404
        [ SEARCH_LOW               ] = "SEARCH_LOW",
405
        [ SET_LIMITS               ] = "SET_LIMITS",
406
        [ PRE_FETCH                ] = "PRE_FETCH",
407
        [ READ_POSITION            ] = "READ_POSITION",
408
        [ SYNCHRONIZE_CACHE        ] = "SYNCHRONIZE_CACHE",
409
        [ LOCK_UNLOCK_CACHE        ] = "LOCK_UNLOCK_CACHE",
410
        [ READ_DEFECT_DATA         ] = "READ_DEFECT_DATA",
411
        [ MEDIUM_SCAN              ] = "MEDIUM_SCAN",
412
        [ COMPARE                  ] = "COMPARE",
413
        [ COPY_VERIFY              ] = "COPY_VERIFY",
414
        [ WRITE_BUFFER             ] = "WRITE_BUFFER",
415
        [ READ_BUFFER              ] = "READ_BUFFER",
416
        [ UPDATE_BLOCK             ] = "UPDATE_BLOCK",
417
        [ READ_LONG                ] = "READ_LONG",
418
        [ WRITE_LONG               ] = "WRITE_LONG",
419
        [ CHANGE_DEFINITION        ] = "CHANGE_DEFINITION",
420
        [ WRITE_SAME               ] = "WRITE_SAME",
421
        [ READ_TOC                 ] = "READ_TOC",
422
        [ LOG_SELECT               ] = "LOG_SELECT",
423
        [ LOG_SENSE                ] = "LOG_SENSE",
424
        [ MODE_SELECT_10           ] = "MODE_SELECT_10",
425
        [ RESERVE_10               ] = "RESERVE_10",
426
        [ RELEASE_10               ] = "RELEASE_10",
427
        [ MODE_SENSE_10            ] = "MODE_SENSE_10",
428
        [ PERSISTENT_RESERVE_IN    ] = "PERSISTENT_RESERVE_IN",
429
        [ PERSISTENT_RESERVE_OUT   ] = "PERSISTENT_RESERVE_OUT",
430
        [ MOVE_MEDIUM              ] = "MOVE_MEDIUM",
431
        [ READ_12                  ] = "READ_12",
432
        [ WRITE_12                 ] = "WRITE_12",
433
        [ WRITE_VERIFY_12          ] = "WRITE_VERIFY_12",
434
        [ SEARCH_HIGH_12           ] = "SEARCH_HIGH_12",
435
        [ SEARCH_EQUAL_12          ] = "SEARCH_EQUAL_12",
436
        [ SEARCH_LOW_12            ] = "SEARCH_LOW_12",
437
        [ READ_ELEMENT_STATUS      ] = "READ_ELEMENT_STATUS",
438
        [ SEND_VOLUME_TAG          ] = "SEND_VOLUME_TAG",
439
        [ WRITE_LONG_2             ] = "WRITE_LONG_2",
440

    
441
        [ REWIND                   ] = "REWIND",
442
        [ REPORT_DENSITY_SUPPORT   ] = "REPORT_DENSITY_SUPPORT",
443
        [ GET_CONFIGURATION        ] = "GET_CONFIGURATION",
444
        [ SERVICE_ACTION_IN        ] = "SERVICE_ACTION_IN",
445
        [ REPORT_LUNS              ] = "REPORT_LUNS",
446
        [ LOAD_UNLOAD              ] = "LOAD_UNLOAD",
447
        [ SET_CD_SPEED             ] = "SET_CD_SPEED",
448
        [ BLANK                    ] = "BLANK",
449
    };
450

    
451
    if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
452
        return "*UNKNOWN*";
453
    return names[cmd];
454
}
455

    
456
void scsi_req_print(SCSIRequest *req)
457
{
458
    FILE *fp = stderr;
459
    int i;
460

    
461
    fprintf(fp, "[%s id=%d] %s",
462
            req->dev->qdev.parent_bus->name,
463
            req->dev->id,
464
            scsi_command_name(req->cmd.buf[0]));
465
    for (i = 1; i < req->cmd.len; i++) {
466
        fprintf(fp, " 0x%02x", req->cmd.buf[i]);
467
    }
468
    switch (req->cmd.mode) {
469
    case SCSI_XFER_NONE:
470
        fprintf(fp, " - none\n");
471
        break;
472
    case SCSI_XFER_FROM_DEV:
473
        fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
474
        break;
475
    case SCSI_XFER_TO_DEV:
476
        fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
477
        break;
478
    default:
479
        fprintf(fp, " - Oops\n");
480
        break;
481
    }
482
}
483

    
484
void scsi_req_complete(SCSIRequest *req)
485
{
486
    assert(req->status != -1);
487
    req->bus->complete(req->bus, SCSI_REASON_DONE,
488
                       req->tag,
489
                       req->status);
490
}