scsi-bus.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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. static char *scsibus_get_fw_dev_path(DeviceState *dev);
  9. static struct BusInfo scsi_bus_info = {
  10. .name = "SCSI",
  11. .size = sizeof(SCSIBus),
  12. .get_fw_dev_path = scsibus_get_fw_dev_path,
  13. .props = (Property[]) {
  14. DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1),
  15. DEFINE_PROP_END_OF_LIST(),
  16. },
  17. };
  18. static int next_scsi_bus;
  19. /* Create a scsi bus, and attach devices to it. */
  20. void scsi_bus_new(SCSIBus *bus, DeviceState *host, int tcq, int ndev,
  21. const SCSIBusOps *ops)
  22. {
  23. qbus_create_inplace(&bus->qbus, &scsi_bus_info, host, NULL);
  24. bus->busnr = next_scsi_bus++;
  25. bus->tcq = tcq;
  26. bus->ndev = ndev;
  27. bus->ops = ops;
  28. bus->qbus.allow_hotplug = 1;
  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. if (dev->id == -1) {
  37. for (dev->id = 0; dev->id < bus->ndev; dev->id++) {
  38. if (bus->devs[dev->id] == NULL)
  39. break;
  40. }
  41. }
  42. if (dev->id >= bus->ndev) {
  43. error_report("bad scsi device id: %d", dev->id);
  44. goto err;
  45. }
  46. if (bus->devs[dev->id]) {
  47. qdev_free(&bus->devs[dev->id]->qdev);
  48. }
  49. bus->devs[dev->id] = dev;
  50. dev->info = info;
  51. QTAILQ_INIT(&dev->requests);
  52. rc = dev->info->init(dev);
  53. if (rc != 0) {
  54. bus->devs[dev->id] = NULL;
  55. }
  56. err:
  57. return rc;
  58. }
  59. static int scsi_qdev_exit(DeviceState *qdev)
  60. {
  61. SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
  62. SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
  63. assert(bus->devs[dev->id] != NULL);
  64. if (bus->devs[dev->id]->info->destroy) {
  65. bus->devs[dev->id]->info->destroy(bus->devs[dev->id]);
  66. }
  67. bus->devs[dev->id] = NULL;
  68. return 0;
  69. }
  70. void scsi_qdev_register(SCSIDeviceInfo *info)
  71. {
  72. info->qdev.bus_info = &scsi_bus_info;
  73. info->qdev.init = scsi_qdev_init;
  74. info->qdev.unplug = qdev_simple_unplug_cb;
  75. info->qdev.exit = scsi_qdev_exit;
  76. qdev_register(&info->qdev);
  77. }
  78. /* handle legacy '-drive if=scsi,...' cmd line args */
  79. SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
  80. int unit, bool removable)
  81. {
  82. const char *driver;
  83. DeviceState *dev;
  84. driver = bdrv_is_sg(bdrv) ? "scsi-generic" : "scsi-disk";
  85. dev = qdev_create(&bus->qbus, driver);
  86. qdev_prop_set_uint32(dev, "scsi-id", unit);
  87. if (qdev_prop_exists(dev, "removable")) {
  88. qdev_prop_set_bit(dev, "removable", removable);
  89. }
  90. if (qdev_prop_set_drive(dev, "drive", bdrv) < 0) {
  91. qdev_free(dev);
  92. return NULL;
  93. }
  94. if (qdev_init(dev) < 0)
  95. return NULL;
  96. return DO_UPCAST(SCSIDevice, qdev, dev);
  97. }
  98. int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
  99. {
  100. Location loc;
  101. DriveInfo *dinfo;
  102. int res = 0, unit;
  103. loc_push_none(&loc);
  104. for (unit = 0; unit < bus->ndev; unit++) {
  105. dinfo = drive_get(IF_SCSI, bus->busnr, unit);
  106. if (dinfo == NULL) {
  107. continue;
  108. }
  109. qemu_opts_loc_restore(dinfo->opts);
  110. if (!scsi_bus_legacy_add_drive(bus, dinfo->bdrv, unit, false)) {
  111. res = -1;
  112. break;
  113. }
  114. }
  115. loc_pop(&loc);
  116. return res;
  117. }
  118. SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag,
  119. uint32_t lun, void *hba_private)
  120. {
  121. SCSIRequest *req;
  122. req = qemu_mallocz(size);
  123. req->refcount = 1;
  124. req->bus = scsi_bus_from_device(d);
  125. req->dev = d;
  126. req->tag = tag;
  127. req->lun = lun;
  128. req->hba_private = hba_private;
  129. req->status = -1;
  130. trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
  131. return req;
  132. }
  133. SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
  134. void *hba_private)
  135. {
  136. return d->info->alloc_req(d, tag, lun, hba_private);
  137. }
  138. uint8_t *scsi_req_get_buf(SCSIRequest *req)
  139. {
  140. return req->dev->info->get_buf(req);
  141. }
  142. int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len)
  143. {
  144. if (req->dev->info->get_sense) {
  145. return req->dev->info->get_sense(req, buf, len);
  146. } else {
  147. return 0;
  148. }
  149. }
  150. int32_t scsi_req_enqueue(SCSIRequest *req, uint8_t *buf)
  151. {
  152. int32_t rc;
  153. assert(!req->enqueued);
  154. scsi_req_ref(req);
  155. req->enqueued = true;
  156. QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
  157. scsi_req_ref(req);
  158. rc = req->dev->info->send_command(req, buf);
  159. scsi_req_unref(req);
  160. return rc;
  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. scsi_req_unref(req);
  169. }
  170. }
  171. static int scsi_req_length(SCSIRequest *req, uint8_t *cmd)
  172. {
  173. switch (cmd[0] >> 5) {
  174. case 0:
  175. req->cmd.xfer = cmd[4];
  176. req->cmd.len = 6;
  177. /* length 0 means 256 blocks */
  178. if (req->cmd.xfer == 0)
  179. req->cmd.xfer = 256;
  180. break;
  181. case 1:
  182. case 2:
  183. req->cmd.xfer = cmd[8] | (cmd[7] << 8);
  184. req->cmd.len = 10;
  185. break;
  186. case 4:
  187. req->cmd.xfer = cmd[13] | (cmd[12] << 8) | (cmd[11] << 16) | (cmd[10] << 24);
  188. req->cmd.len = 16;
  189. break;
  190. case 5:
  191. req->cmd.xfer = cmd[9] | (cmd[8] << 8) | (cmd[7] << 16) | (cmd[6] << 24);
  192. req->cmd.len = 12;
  193. break;
  194. default:
  195. trace_scsi_req_parse_bad(req->dev->id, req->lun, req->tag, cmd[0]);
  196. return -1;
  197. }
  198. switch(cmd[0]) {
  199. case TEST_UNIT_READY:
  200. case REZERO_UNIT:
  201. case START_STOP:
  202. case SEEK_6:
  203. case WRITE_FILEMARKS:
  204. case SPACE:
  205. case RESERVE:
  206. case RELEASE:
  207. case ERASE:
  208. case ALLOW_MEDIUM_REMOVAL:
  209. case VERIFY:
  210. case SEEK_10:
  211. case SYNCHRONIZE_CACHE:
  212. case LOCK_UNLOCK_CACHE:
  213. case LOAD_UNLOAD:
  214. case SET_CD_SPEED:
  215. case SET_LIMITS:
  216. case WRITE_LONG:
  217. case MOVE_MEDIUM:
  218. case UPDATE_BLOCK:
  219. req->cmd.xfer = 0;
  220. break;
  221. case MODE_SENSE:
  222. break;
  223. case WRITE_SAME:
  224. req->cmd.xfer = 1;
  225. break;
  226. case READ_CAPACITY:
  227. req->cmd.xfer = 8;
  228. break;
  229. case READ_BLOCK_LIMITS:
  230. req->cmd.xfer = 6;
  231. break;
  232. case READ_POSITION:
  233. req->cmd.xfer = 20;
  234. break;
  235. case SEND_VOLUME_TAG:
  236. req->cmd.xfer *= 40;
  237. break;
  238. case MEDIUM_SCAN:
  239. req->cmd.xfer *= 8;
  240. break;
  241. case WRITE_10:
  242. case WRITE_VERIFY:
  243. case WRITE_6:
  244. case WRITE_12:
  245. case WRITE_VERIFY_12:
  246. case WRITE_16:
  247. case WRITE_VERIFY_16:
  248. req->cmd.xfer *= req->dev->blocksize;
  249. break;
  250. case READ_10:
  251. case READ_6:
  252. case READ_REVERSE:
  253. case RECOVER_BUFFERED_DATA:
  254. case READ_12:
  255. case READ_16:
  256. req->cmd.xfer *= req->dev->blocksize;
  257. break;
  258. case INQUIRY:
  259. req->cmd.xfer = cmd[4] | (cmd[3] << 8);
  260. break;
  261. case MAINTENANCE_OUT:
  262. case MAINTENANCE_IN:
  263. if (req->dev->type == TYPE_ROM) {
  264. /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
  265. req->cmd.xfer = cmd[9] | (cmd[8] << 8);
  266. }
  267. break;
  268. }
  269. return 0;
  270. }
  271. static int scsi_req_stream_length(SCSIRequest *req, uint8_t *cmd)
  272. {
  273. switch(cmd[0]) {
  274. /* stream commands */
  275. case READ_6:
  276. case READ_REVERSE:
  277. case RECOVER_BUFFERED_DATA:
  278. case WRITE_6:
  279. req->cmd.len = 6;
  280. req->cmd.xfer = cmd[4] | (cmd[3] << 8) | (cmd[2] << 16);
  281. if (cmd[1] & 0x01) /* fixed */
  282. req->cmd.xfer *= req->dev->blocksize;
  283. break;
  284. case REWIND:
  285. case START_STOP:
  286. req->cmd.len = 6;
  287. req->cmd.xfer = 0;
  288. break;
  289. /* generic commands */
  290. default:
  291. return scsi_req_length(req, cmd);
  292. }
  293. return 0;
  294. }
  295. static void scsi_req_xfer_mode(SCSIRequest *req)
  296. {
  297. switch (req->cmd.buf[0]) {
  298. case WRITE_6:
  299. case WRITE_10:
  300. case WRITE_VERIFY:
  301. case WRITE_12:
  302. case WRITE_VERIFY_12:
  303. case WRITE_16:
  304. case WRITE_VERIFY_16:
  305. case COPY:
  306. case COPY_VERIFY:
  307. case COMPARE:
  308. case CHANGE_DEFINITION:
  309. case LOG_SELECT:
  310. case MODE_SELECT:
  311. case MODE_SELECT_10:
  312. case SEND_DIAGNOSTIC:
  313. case WRITE_BUFFER:
  314. case FORMAT_UNIT:
  315. case REASSIGN_BLOCKS:
  316. case SEARCH_EQUAL:
  317. case SEARCH_HIGH:
  318. case SEARCH_LOW:
  319. case UPDATE_BLOCK:
  320. case WRITE_LONG:
  321. case WRITE_SAME:
  322. case SEARCH_HIGH_12:
  323. case SEARCH_EQUAL_12:
  324. case SEARCH_LOW_12:
  325. case SET_WINDOW:
  326. case MEDIUM_SCAN:
  327. case SEND_VOLUME_TAG:
  328. case WRITE_LONG_2:
  329. case PERSISTENT_RESERVE_OUT:
  330. case MAINTENANCE_OUT:
  331. req->cmd.mode = SCSI_XFER_TO_DEV;
  332. break;
  333. default:
  334. if (req->cmd.xfer)
  335. req->cmd.mode = SCSI_XFER_FROM_DEV;
  336. else {
  337. req->cmd.mode = SCSI_XFER_NONE;
  338. }
  339. break;
  340. }
  341. }
  342. static uint64_t scsi_req_lba(SCSIRequest *req)
  343. {
  344. uint8_t *buf = req->cmd.buf;
  345. uint64_t lba;
  346. switch (buf[0] >> 5) {
  347. case 0:
  348. lba = (uint64_t) buf[3] | ((uint64_t) buf[2] << 8) |
  349. (((uint64_t) buf[1] & 0x1f) << 16);
  350. break;
  351. case 1:
  352. case 2:
  353. lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
  354. ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
  355. break;
  356. case 4:
  357. lba = (uint64_t) buf[9] | ((uint64_t) buf[8] << 8) |
  358. ((uint64_t) buf[7] << 16) | ((uint64_t) buf[6] << 24) |
  359. ((uint64_t) buf[5] << 32) | ((uint64_t) buf[4] << 40) |
  360. ((uint64_t) buf[3] << 48) | ((uint64_t) buf[2] << 56);
  361. break;
  362. case 5:
  363. lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
  364. ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
  365. break;
  366. default:
  367. lba = -1;
  368. }
  369. return lba;
  370. }
  371. int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
  372. {
  373. int rc;
  374. if (req->dev->type == TYPE_TAPE) {
  375. rc = scsi_req_stream_length(req, buf);
  376. } else {
  377. rc = scsi_req_length(req, buf);
  378. }
  379. if (rc != 0)
  380. return rc;
  381. memcpy(req->cmd.buf, buf, req->cmd.len);
  382. scsi_req_xfer_mode(req);
  383. req->cmd.lba = scsi_req_lba(req);
  384. trace_scsi_req_parsed(req->dev->id, req->lun, req->tag, buf[0],
  385. req->cmd.mode, req->cmd.xfer);
  386. if (req->cmd.lba != -1) {
  387. trace_scsi_req_parsed_lba(req->dev->id, req->lun, req->tag, buf[0],
  388. req->cmd.lba);
  389. }
  390. return 0;
  391. }
  392. /*
  393. * Predefined sense codes
  394. */
  395. /* No sense data available */
  396. const struct SCSISense sense_code_NO_SENSE = {
  397. .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00
  398. };
  399. /* LUN not ready, Manual intervention required */
  400. const struct SCSISense sense_code_LUN_NOT_READY = {
  401. .key = NOT_READY, .asc = 0x04, .ascq = 0x03
  402. };
  403. /* LUN not ready, Medium not present */
  404. const struct SCSISense sense_code_NO_MEDIUM = {
  405. .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
  406. };
  407. /* Hardware error, internal target failure */
  408. const struct SCSISense sense_code_TARGET_FAILURE = {
  409. .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
  410. };
  411. /* Illegal request, invalid command operation code */
  412. const struct SCSISense sense_code_INVALID_OPCODE = {
  413. .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00
  414. };
  415. /* Illegal request, LBA out of range */
  416. const struct SCSISense sense_code_LBA_OUT_OF_RANGE = {
  417. .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00
  418. };
  419. /* Illegal request, Invalid field in CDB */
  420. const struct SCSISense sense_code_INVALID_FIELD = {
  421. .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00
  422. };
  423. /* Illegal request, LUN not supported */
  424. const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
  425. .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
  426. };
  427. /* Command aborted, I/O process terminated */
  428. const struct SCSISense sense_code_IO_ERROR = {
  429. .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
  430. };
  431. /* Command aborted, I_T Nexus loss occurred */
  432. const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
  433. .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
  434. };
  435. /* Command aborted, Logical Unit failure */
  436. const struct SCSISense sense_code_LUN_FAILURE = {
  437. .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
  438. };
  439. /*
  440. * scsi_build_sense
  441. *
  442. * Build a sense buffer
  443. */
  444. int scsi_build_sense(SCSISense sense, uint8_t *buf, int len, int fixed)
  445. {
  446. if (!fixed && len < 8) {
  447. return 0;
  448. }
  449. memset(buf, 0, len);
  450. if (fixed) {
  451. /* Return fixed format sense buffer */
  452. buf[0] = 0xf0;
  453. buf[2] = sense.key;
  454. buf[7] = 7;
  455. buf[12] = sense.asc;
  456. buf[13] = sense.ascq;
  457. return MIN(len, 18);
  458. } else {
  459. /* Return descriptor format sense buffer */
  460. buf[0] = 0x72;
  461. buf[1] = sense.key;
  462. buf[2] = sense.asc;
  463. buf[3] = sense.ascq;
  464. return 8;
  465. }
  466. }
  467. static const char *scsi_command_name(uint8_t cmd)
  468. {
  469. static const char *names[] = {
  470. [ TEST_UNIT_READY ] = "TEST_UNIT_READY",
  471. [ REZERO_UNIT ] = "REZERO_UNIT",
  472. /* REWIND and REZERO_UNIT use the same operation code */
  473. [ REQUEST_SENSE ] = "REQUEST_SENSE",
  474. [ FORMAT_UNIT ] = "FORMAT_UNIT",
  475. [ READ_BLOCK_LIMITS ] = "READ_BLOCK_LIMITS",
  476. [ REASSIGN_BLOCKS ] = "REASSIGN_BLOCKS",
  477. [ READ_6 ] = "READ_6",
  478. [ WRITE_6 ] = "WRITE_6",
  479. [ SEEK_6 ] = "SEEK_6",
  480. [ READ_REVERSE ] = "READ_REVERSE",
  481. [ WRITE_FILEMARKS ] = "WRITE_FILEMARKS",
  482. [ SPACE ] = "SPACE",
  483. [ INQUIRY ] = "INQUIRY",
  484. [ RECOVER_BUFFERED_DATA ] = "RECOVER_BUFFERED_DATA",
  485. [ MAINTENANCE_IN ] = "MAINTENANCE_IN",
  486. [ MAINTENANCE_OUT ] = "MAINTENANCE_OUT",
  487. [ MODE_SELECT ] = "MODE_SELECT",
  488. [ RESERVE ] = "RESERVE",
  489. [ RELEASE ] = "RELEASE",
  490. [ COPY ] = "COPY",
  491. [ ERASE ] = "ERASE",
  492. [ MODE_SENSE ] = "MODE_SENSE",
  493. [ START_STOP ] = "START_STOP",
  494. [ RECEIVE_DIAGNOSTIC ] = "RECEIVE_DIAGNOSTIC",
  495. [ SEND_DIAGNOSTIC ] = "SEND_DIAGNOSTIC",
  496. [ ALLOW_MEDIUM_REMOVAL ] = "ALLOW_MEDIUM_REMOVAL",
  497. [ SET_WINDOW ] = "SET_WINDOW",
  498. [ READ_CAPACITY ] = "READ_CAPACITY",
  499. [ READ_10 ] = "READ_10",
  500. [ WRITE_10 ] = "WRITE_10",
  501. [ SEEK_10 ] = "SEEK_10",
  502. [ WRITE_VERIFY ] = "WRITE_VERIFY",
  503. [ VERIFY ] = "VERIFY",
  504. [ SEARCH_HIGH ] = "SEARCH_HIGH",
  505. [ SEARCH_EQUAL ] = "SEARCH_EQUAL",
  506. [ SEARCH_LOW ] = "SEARCH_LOW",
  507. [ SET_LIMITS ] = "SET_LIMITS",
  508. [ PRE_FETCH ] = "PRE_FETCH",
  509. /* READ_POSITION and PRE_FETCH use the same operation code */
  510. [ SYNCHRONIZE_CACHE ] = "SYNCHRONIZE_CACHE",
  511. [ LOCK_UNLOCK_CACHE ] = "LOCK_UNLOCK_CACHE",
  512. [ READ_DEFECT_DATA ] = "READ_DEFECT_DATA",
  513. [ MEDIUM_SCAN ] = "MEDIUM_SCAN",
  514. [ COMPARE ] = "COMPARE",
  515. [ COPY_VERIFY ] = "COPY_VERIFY",
  516. [ WRITE_BUFFER ] = "WRITE_BUFFER",
  517. [ READ_BUFFER ] = "READ_BUFFER",
  518. [ UPDATE_BLOCK ] = "UPDATE_BLOCK",
  519. [ READ_LONG ] = "READ_LONG",
  520. [ WRITE_LONG ] = "WRITE_LONG",
  521. [ CHANGE_DEFINITION ] = "CHANGE_DEFINITION",
  522. [ WRITE_SAME ] = "WRITE_SAME",
  523. [ READ_TOC ] = "READ_TOC",
  524. [ LOG_SELECT ] = "LOG_SELECT",
  525. [ LOG_SENSE ] = "LOG_SENSE",
  526. [ MODE_SELECT_10 ] = "MODE_SELECT_10",
  527. [ RESERVE_10 ] = "RESERVE_10",
  528. [ RELEASE_10 ] = "RELEASE_10",
  529. [ MODE_SENSE_10 ] = "MODE_SENSE_10",
  530. [ PERSISTENT_RESERVE_IN ] = "PERSISTENT_RESERVE_IN",
  531. [ PERSISTENT_RESERVE_OUT ] = "PERSISTENT_RESERVE_OUT",
  532. [ MOVE_MEDIUM ] = "MOVE_MEDIUM",
  533. [ READ_12 ] = "READ_12",
  534. [ WRITE_12 ] = "WRITE_12",
  535. [ WRITE_VERIFY_12 ] = "WRITE_VERIFY_12",
  536. [ SEARCH_HIGH_12 ] = "SEARCH_HIGH_12",
  537. [ SEARCH_EQUAL_12 ] = "SEARCH_EQUAL_12",
  538. [ SEARCH_LOW_12 ] = "SEARCH_LOW_12",
  539. [ READ_ELEMENT_STATUS ] = "READ_ELEMENT_STATUS",
  540. [ SEND_VOLUME_TAG ] = "SEND_VOLUME_TAG",
  541. [ WRITE_LONG_2 ] = "WRITE_LONG_2",
  542. [ REPORT_DENSITY_SUPPORT ] = "REPORT_DENSITY_SUPPORT",
  543. [ GET_CONFIGURATION ] = "GET_CONFIGURATION",
  544. [ READ_16 ] = "READ_16",
  545. [ WRITE_16 ] = "WRITE_16",
  546. [ WRITE_VERIFY_16 ] = "WRITE_VERIFY_16",
  547. [ SERVICE_ACTION_IN ] = "SERVICE_ACTION_IN",
  548. [ REPORT_LUNS ] = "REPORT_LUNS",
  549. [ LOAD_UNLOAD ] = "LOAD_UNLOAD",
  550. [ SET_CD_SPEED ] = "SET_CD_SPEED",
  551. [ BLANK ] = "BLANK",
  552. };
  553. if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
  554. return "*UNKNOWN*";
  555. return names[cmd];
  556. }
  557. SCSIRequest *scsi_req_ref(SCSIRequest *req)
  558. {
  559. req->refcount++;
  560. return req;
  561. }
  562. void scsi_req_unref(SCSIRequest *req)
  563. {
  564. if (--req->refcount == 0) {
  565. if (req->dev->info->free_req) {
  566. req->dev->info->free_req(req);
  567. }
  568. qemu_free(req);
  569. }
  570. }
  571. /* Tell the device that we finished processing this chunk of I/O. It
  572. will start the next chunk or complete the command. */
  573. void scsi_req_continue(SCSIRequest *req)
  574. {
  575. trace_scsi_req_continue(req->dev->id, req->lun, req->tag);
  576. if (req->cmd.mode == SCSI_XFER_TO_DEV) {
  577. req->dev->info->write_data(req);
  578. } else {
  579. req->dev->info->read_data(req);
  580. }
  581. }
  582. /* Called by the devices when data is ready for the HBA. The HBA should
  583. start a DMA operation to read or fill the device's data buffer.
  584. Once it completes, calling scsi_req_continue will restart I/O. */
  585. void scsi_req_data(SCSIRequest *req, int len)
  586. {
  587. trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
  588. req->bus->ops->transfer_data(req, len);
  589. }
  590. void scsi_req_print(SCSIRequest *req)
  591. {
  592. FILE *fp = stderr;
  593. int i;
  594. fprintf(fp, "[%s id=%d] %s",
  595. req->dev->qdev.parent_bus->name,
  596. req->dev->id,
  597. scsi_command_name(req->cmd.buf[0]));
  598. for (i = 1; i < req->cmd.len; i++) {
  599. fprintf(fp, " 0x%02x", req->cmd.buf[i]);
  600. }
  601. switch (req->cmd.mode) {
  602. case SCSI_XFER_NONE:
  603. fprintf(fp, " - none\n");
  604. break;
  605. case SCSI_XFER_FROM_DEV:
  606. fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
  607. break;
  608. case SCSI_XFER_TO_DEV:
  609. fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
  610. break;
  611. default:
  612. fprintf(fp, " - Oops\n");
  613. break;
  614. }
  615. }
  616. void scsi_req_complete(SCSIRequest *req)
  617. {
  618. assert(req->status != -1);
  619. scsi_req_ref(req);
  620. scsi_req_dequeue(req);
  621. req->bus->ops->complete(req, req->status);
  622. scsi_req_unref(req);
  623. }
  624. void scsi_req_cancel(SCSIRequest *req)
  625. {
  626. if (req->dev && req->dev->info->cancel_io) {
  627. req->dev->info->cancel_io(req);
  628. }
  629. scsi_req_ref(req);
  630. scsi_req_dequeue(req);
  631. if (req->bus->ops->cancel) {
  632. req->bus->ops->cancel(req);
  633. }
  634. scsi_req_unref(req);
  635. }
  636. void scsi_req_abort(SCSIRequest *req, int status)
  637. {
  638. req->status = status;
  639. if (req->dev && req->dev->info->cancel_io) {
  640. req->dev->info->cancel_io(req);
  641. }
  642. scsi_req_complete(req);
  643. }
  644. void scsi_device_purge_requests(SCSIDevice *sdev)
  645. {
  646. SCSIRequest *req;
  647. while (!QTAILQ_EMPTY(&sdev->requests)) {
  648. req = QTAILQ_FIRST(&sdev->requests);
  649. scsi_req_cancel(req);
  650. }
  651. }
  652. static char *scsibus_get_fw_dev_path(DeviceState *dev)
  653. {
  654. SCSIDevice *d = (SCSIDevice*)dev;
  655. SCSIBus *bus = scsi_bus_from_device(d);
  656. char path[100];
  657. int i;
  658. for (i = 0; i < bus->ndev; i++) {
  659. if (bus->devs[i] == d) {
  660. break;
  661. }
  662. }
  663. assert(i != bus->ndev);
  664. snprintf(path, sizeof(path), "%s@%x", qdev_fw_name(dev), i);
  665. return strdup(path);
  666. }