win32: Add missing function setenv
[qemu/lumag.git] / hw / scsi-bus.c
blobb860a09edf5810c426816cd1dd8a0a62429f442c
1 #include "hw.h"
2 #include "qemu-error.h"
3 #include "scsi.h"
4 #include "scsi-defs.h"
5 #include "qdev.h"
7 static struct BusInfo scsi_bus_info = {
8 .name = "SCSI",
9 .size = sizeof(SCSIBus),
10 .props = (Property[]) {
11 DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1),
12 DEFINE_PROP_END_OF_LIST(),
15 static int next_scsi_bus;
17 /* Create a scsi bus, and attach devices to it. */
18 void scsi_bus_new(SCSIBus *bus, DeviceState *host, int tcq, int ndev,
19 scsi_completionfn complete)
21 qbus_create_inplace(&bus->qbus, &scsi_bus_info, host, NULL);
22 bus->busnr = next_scsi_bus++;
23 bus->tcq = tcq;
24 bus->ndev = ndev;
25 bus->complete = complete;
26 bus->qbus.allow_hotplug = 1;
29 static int scsi_qdev_init(DeviceState *qdev, DeviceInfo *base)
31 SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
32 SCSIDeviceInfo *info = DO_UPCAST(SCSIDeviceInfo, qdev, base);
33 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
34 int rc = -1;
36 if (dev->id == -1) {
37 for (dev->id = 0; dev->id < bus->ndev; dev->id++) {
38 if (bus->devs[dev->id] == NULL)
39 break;
42 if (dev->id >= bus->ndev) {
43 error_report("bad scsi device id: %d", dev->id);
44 goto err;
47 if (bus->devs[dev->id]) {
48 qdev_free(&bus->devs[dev->id]->qdev);
50 bus->devs[dev->id] = dev;
52 dev->info = info;
53 QTAILQ_INIT(&dev->requests);
54 rc = dev->info->init(dev);
55 if (rc != 0) {
56 bus->devs[dev->id] = NULL;
59 err:
60 return rc;
63 static int scsi_qdev_exit(DeviceState *qdev)
65 SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
66 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
68 assert(bus->devs[dev->id] != NULL);
69 if (bus->devs[dev->id]->info->destroy) {
70 bus->devs[dev->id]->info->destroy(bus->devs[dev->id]);
72 bus->devs[dev->id] = NULL;
73 return 0;
76 void scsi_qdev_register(SCSIDeviceInfo *info)
78 info->qdev.bus_info = &scsi_bus_info;
79 info->qdev.init = scsi_qdev_init;
80 info->qdev.unplug = qdev_simple_unplug_cb;
81 info->qdev.exit = scsi_qdev_exit;
82 qdev_register(&info->qdev);
85 /* handle legacy '-drive if=scsi,...' cmd line args */
86 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv, int unit)
88 const char *driver;
89 DeviceState *dev;
91 driver = bdrv_is_sg(bdrv) ? "scsi-generic" : "scsi-disk";
92 dev = qdev_create(&bus->qbus, driver);
93 qdev_prop_set_uint32(dev, "scsi-id", unit);
94 if (qdev_prop_set_drive(dev, "drive", bdrv) < 0) {
95 qdev_free(dev);
96 return NULL;
98 if (qdev_init(dev) < 0)
99 return NULL;
100 return DO_UPCAST(SCSIDevice, qdev, dev);
103 int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
105 Location loc;
106 DriveInfo *dinfo;
107 int res = 0, unit;
109 loc_push_none(&loc);
110 for (unit = 0; unit < MAX_SCSI_DEVS; unit++) {
111 dinfo = drive_get(IF_SCSI, bus->busnr, unit);
112 if (dinfo == NULL) {
113 continue;
115 qemu_opts_loc_restore(dinfo->opts);
116 if (!scsi_bus_legacy_add_drive(bus, dinfo->bdrv, unit)) {
117 res = -1;
118 break;
121 loc_pop(&loc);
122 return res;
125 void scsi_dev_clear_sense(SCSIDevice *dev)
127 memset(&dev->sense, 0, sizeof(dev->sense));
130 void scsi_dev_set_sense(SCSIDevice *dev, uint8_t key)
132 dev->sense.key = key;
135 SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t lun)
137 SCSIRequest *req;
139 req = qemu_mallocz(size);
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 req->enqueued = true;
146 QTAILQ_INSERT_TAIL(&d->requests, req, next);
147 return req;
150 SCSIRequest *scsi_req_find(SCSIDevice *d, uint32_t tag)
152 SCSIRequest *req;
154 QTAILQ_FOREACH(req, &d->requests, next) {
155 if (req->tag == tag) {
156 return req;
159 return NULL;
162 static void scsi_req_dequeue(SCSIRequest *req)
164 if (req->enqueued) {
165 QTAILQ_REMOVE(&req->dev->requests, req, next);
166 req->enqueued = false;
170 void scsi_req_free(SCSIRequest *req)
172 scsi_req_dequeue(req);
173 qemu_free(req);
176 static int scsi_req_length(SCSIRequest *req, uint8_t *cmd)
178 switch (cmd[0] >> 5) {
179 case 0:
180 req->cmd.xfer = cmd[4];
181 req->cmd.len = 6;
182 /* length 0 means 256 blocks */
183 if (req->cmd.xfer == 0)
184 req->cmd.xfer = 256;
185 break;
186 case 1:
187 case 2:
188 req->cmd.xfer = cmd[8] | (cmd[7] << 8);
189 req->cmd.len = 10;
190 break;
191 case 4:
192 req->cmd.xfer = cmd[13] | (cmd[12] << 8) | (cmd[11] << 16) | (cmd[10] << 24);
193 req->cmd.len = 16;
194 break;
195 case 5:
196 req->cmd.xfer = cmd[9] | (cmd[8] << 8) | (cmd[7] << 16) | (cmd[6] << 24);
197 req->cmd.len = 12;
198 break;
199 default:
200 return -1;
203 switch(cmd[0]) {
204 case TEST_UNIT_READY:
205 case REZERO_UNIT:
206 case START_STOP:
207 case SEEK_6:
208 case WRITE_FILEMARKS:
209 case SPACE:
210 case ERASE:
211 case ALLOW_MEDIUM_REMOVAL:
212 case VERIFY:
213 case SEEK_10:
214 case SYNCHRONIZE_CACHE:
215 case LOCK_UNLOCK_CACHE:
216 case LOAD_UNLOAD:
217 case SET_CD_SPEED:
218 case SET_LIMITS:
219 case WRITE_LONG:
220 case MOVE_MEDIUM:
221 case UPDATE_BLOCK:
222 req->cmd.xfer = 0;
223 break;
224 case MODE_SENSE:
225 break;
226 case WRITE_SAME:
227 req->cmd.xfer = 1;
228 break;
229 case READ_CAPACITY:
230 req->cmd.xfer = 8;
231 break;
232 case READ_BLOCK_LIMITS:
233 req->cmd.xfer = 6;
234 break;
235 case READ_POSITION:
236 req->cmd.xfer = 20;
237 break;
238 case SEND_VOLUME_TAG:
239 req->cmd.xfer *= 40;
240 break;
241 case MEDIUM_SCAN:
242 req->cmd.xfer *= 8;
243 break;
244 case WRITE_10:
245 case WRITE_VERIFY:
246 case WRITE_6:
247 case WRITE_12:
248 case WRITE_VERIFY_12:
249 case WRITE_16:
250 case WRITE_VERIFY_16:
251 req->cmd.xfer *= req->dev->blocksize;
252 break;
253 case READ_10:
254 case READ_6:
255 case READ_REVERSE:
256 case RECOVER_BUFFERED_DATA:
257 case READ_12:
258 case READ_16:
259 req->cmd.xfer *= req->dev->blocksize;
260 break;
261 case INQUIRY:
262 req->cmd.xfer = cmd[4] | (cmd[3] << 8);
263 break;
264 case MAINTENANCE_OUT:
265 case MAINTENANCE_IN:
266 if (req->dev->type == TYPE_ROM) {
267 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
268 req->cmd.xfer = cmd[9] | (cmd[8] << 8);
270 break;
272 return 0;
275 static int scsi_req_stream_length(SCSIRequest *req, uint8_t *cmd)
277 switch(cmd[0]) {
278 /* stream commands */
279 case READ_6:
280 case READ_REVERSE:
281 case RECOVER_BUFFERED_DATA:
282 case WRITE_6:
283 req->cmd.len = 6;
284 req->cmd.xfer = cmd[4] | (cmd[3] << 8) | (cmd[2] << 16);
285 if (cmd[1] & 0x01) /* fixed */
286 req->cmd.xfer *= req->dev->blocksize;
287 break;
288 case REWIND:
289 case START_STOP:
290 req->cmd.len = 6;
291 req->cmd.xfer = 0;
292 break;
293 /* generic commands */
294 default:
295 return scsi_req_length(req, cmd);
297 return 0;
300 static void scsi_req_xfer_mode(SCSIRequest *req)
302 switch (req->cmd.buf[0]) {
303 case WRITE_6:
304 case WRITE_10:
305 case WRITE_VERIFY:
306 case WRITE_12:
307 case WRITE_VERIFY_12:
308 case WRITE_16:
309 case WRITE_VERIFY_16:
310 case COPY:
311 case COPY_VERIFY:
312 case COMPARE:
313 case CHANGE_DEFINITION:
314 case LOG_SELECT:
315 case MODE_SELECT:
316 case MODE_SELECT_10:
317 case SEND_DIAGNOSTIC:
318 case WRITE_BUFFER:
319 case FORMAT_UNIT:
320 case REASSIGN_BLOCKS:
321 case RESERVE:
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;
345 break;
349 static uint64_t scsi_req_lba(SCSIRequest *req)
351 uint8_t *buf = req->cmd.buf;
352 uint64_t lba;
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;
378 return lba;
381 int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
383 int rc;
385 if (req->dev->type == TYPE_TAPE) {
386 rc = scsi_req_stream_length(req, buf);
387 } else {
388 rc = scsi_req_length(req, buf);
390 if (rc != 0)
391 return rc;
393 memcpy(req->cmd.buf, buf, req->cmd.len);
394 scsi_req_xfer_mode(req);
395 req->cmd.lba = scsi_req_lba(req);
396 return 0;
399 static const char *scsi_command_name(uint8_t cmd)
401 static const char *names[] = {
402 [ TEST_UNIT_READY ] = "TEST_UNIT_READY",
403 [ REZERO_UNIT ] = "REZERO_UNIT",
404 /* REWIND and REZERO_UNIT use the same operation code */
405 [ REQUEST_SENSE ] = "REQUEST_SENSE",
406 [ FORMAT_UNIT ] = "FORMAT_UNIT",
407 [ READ_BLOCK_LIMITS ] = "READ_BLOCK_LIMITS",
408 [ REASSIGN_BLOCKS ] = "REASSIGN_BLOCKS",
409 [ READ_6 ] = "READ_6",
410 [ WRITE_6 ] = "WRITE_6",
411 [ SEEK_6 ] = "SEEK_6",
412 [ READ_REVERSE ] = "READ_REVERSE",
413 [ WRITE_FILEMARKS ] = "WRITE_FILEMARKS",
414 [ SPACE ] = "SPACE",
415 [ INQUIRY ] = "INQUIRY",
416 [ RECOVER_BUFFERED_DATA ] = "RECOVER_BUFFERED_DATA",
417 [ MAINTENANCE_IN ] = "MAINTENANCE_IN",
418 [ MAINTENANCE_OUT ] = "MAINTENANCE_OUT",
419 [ MODE_SELECT ] = "MODE_SELECT",
420 [ RESERVE ] = "RESERVE",
421 [ RELEASE ] = "RELEASE",
422 [ COPY ] = "COPY",
423 [ ERASE ] = "ERASE",
424 [ MODE_SENSE ] = "MODE_SENSE",
425 [ START_STOP ] = "START_STOP",
426 [ RECEIVE_DIAGNOSTIC ] = "RECEIVE_DIAGNOSTIC",
427 [ SEND_DIAGNOSTIC ] = "SEND_DIAGNOSTIC",
428 [ ALLOW_MEDIUM_REMOVAL ] = "ALLOW_MEDIUM_REMOVAL",
430 [ SET_WINDOW ] = "SET_WINDOW",
431 [ READ_CAPACITY ] = "READ_CAPACITY",
432 [ READ_10 ] = "READ_10",
433 [ WRITE_10 ] = "WRITE_10",
434 [ SEEK_10 ] = "SEEK_10",
435 [ WRITE_VERIFY ] = "WRITE_VERIFY",
436 [ VERIFY ] = "VERIFY",
437 [ SEARCH_HIGH ] = "SEARCH_HIGH",
438 [ SEARCH_EQUAL ] = "SEARCH_EQUAL",
439 [ SEARCH_LOW ] = "SEARCH_LOW",
440 [ SET_LIMITS ] = "SET_LIMITS",
441 [ PRE_FETCH ] = "PRE_FETCH",
442 /* READ_POSITION and PRE_FETCH use the same operation code */
443 [ SYNCHRONIZE_CACHE ] = "SYNCHRONIZE_CACHE",
444 [ LOCK_UNLOCK_CACHE ] = "LOCK_UNLOCK_CACHE",
445 [ READ_DEFECT_DATA ] = "READ_DEFECT_DATA",
446 [ MEDIUM_SCAN ] = "MEDIUM_SCAN",
447 [ COMPARE ] = "COMPARE",
448 [ COPY_VERIFY ] = "COPY_VERIFY",
449 [ WRITE_BUFFER ] = "WRITE_BUFFER",
450 [ READ_BUFFER ] = "READ_BUFFER",
451 [ UPDATE_BLOCK ] = "UPDATE_BLOCK",
452 [ READ_LONG ] = "READ_LONG",
453 [ WRITE_LONG ] = "WRITE_LONG",
454 [ CHANGE_DEFINITION ] = "CHANGE_DEFINITION",
455 [ WRITE_SAME ] = "WRITE_SAME",
456 [ READ_TOC ] = "READ_TOC",
457 [ LOG_SELECT ] = "LOG_SELECT",
458 [ LOG_SENSE ] = "LOG_SENSE",
459 [ MODE_SELECT_10 ] = "MODE_SELECT_10",
460 [ RESERVE_10 ] = "RESERVE_10",
461 [ RELEASE_10 ] = "RELEASE_10",
462 [ MODE_SENSE_10 ] = "MODE_SENSE_10",
463 [ PERSISTENT_RESERVE_IN ] = "PERSISTENT_RESERVE_IN",
464 [ PERSISTENT_RESERVE_OUT ] = "PERSISTENT_RESERVE_OUT",
465 [ MOVE_MEDIUM ] = "MOVE_MEDIUM",
466 [ READ_12 ] = "READ_12",
467 [ WRITE_12 ] = "WRITE_12",
468 [ WRITE_VERIFY_12 ] = "WRITE_VERIFY_12",
469 [ SEARCH_HIGH_12 ] = "SEARCH_HIGH_12",
470 [ SEARCH_EQUAL_12 ] = "SEARCH_EQUAL_12",
471 [ SEARCH_LOW_12 ] = "SEARCH_LOW_12",
472 [ READ_ELEMENT_STATUS ] = "READ_ELEMENT_STATUS",
473 [ SEND_VOLUME_TAG ] = "SEND_VOLUME_TAG",
474 [ WRITE_LONG_2 ] = "WRITE_LONG_2",
476 [ REPORT_DENSITY_SUPPORT ] = "REPORT_DENSITY_SUPPORT",
477 [ GET_CONFIGURATION ] = "GET_CONFIGURATION",
478 [ READ_16 ] = "READ_16",
479 [ WRITE_16 ] = "WRITE_16",
480 [ WRITE_VERIFY_16 ] = "WRITE_VERIFY_16",
481 [ SERVICE_ACTION_IN ] = "SERVICE_ACTION_IN",
482 [ REPORT_LUNS ] = "REPORT_LUNS",
483 [ LOAD_UNLOAD ] = "LOAD_UNLOAD",
484 [ SET_CD_SPEED ] = "SET_CD_SPEED",
485 [ BLANK ] = "BLANK",
488 if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
489 return "*UNKNOWN*";
490 return names[cmd];
493 void scsi_req_print(SCSIRequest *req)
495 FILE *fp = stderr;
496 int i;
498 fprintf(fp, "[%s id=%d] %s",
499 req->dev->qdev.parent_bus->name,
500 req->dev->id,
501 scsi_command_name(req->cmd.buf[0]));
502 for (i = 1; i < req->cmd.len; i++) {
503 fprintf(fp, " 0x%02x", req->cmd.buf[i]);
505 switch (req->cmd.mode) {
506 case SCSI_XFER_NONE:
507 fprintf(fp, " - none\n");
508 break;
509 case SCSI_XFER_FROM_DEV:
510 fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
511 break;
512 case SCSI_XFER_TO_DEV:
513 fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
514 break;
515 default:
516 fprintf(fp, " - Oops\n");
517 break;
521 void scsi_req_complete(SCSIRequest *req)
523 assert(req->status != -1);
524 scsi_req_dequeue(req);
525 req->bus->complete(req->bus, SCSI_REASON_DONE,
526 req->tag,
527 req->status);