scsi-bus.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  1. #include "qemu/osdep.h"
  2. #include "qapi/error.h"
  3. #include "qemu/error-report.h"
  4. #include "qemu/module.h"
  5. #include "qemu/option.h"
  6. #include "hw/qdev-properties.h"
  7. #include "hw/scsi/scsi.h"
  8. #include "migration/qemu-file-types.h"
  9. #include "migration/vmstate.h"
  10. #include "scsi/constants.h"
  11. #include "sysemu/block-backend.h"
  12. #include "sysemu/blockdev.h"
  13. #include "sysemu/sysemu.h"
  14. #include "sysemu/runstate.h"
  15. #include "trace.h"
  16. #include "sysemu/dma.h"
  17. #include "qemu/cutils.h"
  18. static char *scsibus_get_dev_path(DeviceState *dev);
  19. static char *scsibus_get_fw_dev_path(DeviceState *dev);
  20. static void scsi_req_dequeue(SCSIRequest *req);
  21. static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len);
  22. static void scsi_target_free_buf(SCSIRequest *req);
  23. static int next_scsi_bus;
  24. static SCSIDevice *do_scsi_device_find(SCSIBus *bus,
  25. int channel, int id, int lun,
  26. bool include_unrealized)
  27. {
  28. BusChild *kid;
  29. SCSIDevice *retval = NULL;
  30. QTAILQ_FOREACH_RCU(kid, &bus->qbus.children, sibling) {
  31. DeviceState *qdev = kid->child;
  32. SCSIDevice *dev = SCSI_DEVICE(qdev);
  33. if (dev->channel == channel && dev->id == id) {
  34. if (dev->lun == lun) {
  35. retval = dev;
  36. break;
  37. }
  38. /*
  39. * If we don't find exact match (channel/bus/lun),
  40. * we will return the first device which matches channel/bus
  41. */
  42. if (!retval) {
  43. retval = dev;
  44. }
  45. }
  46. }
  47. /*
  48. * This function might run on the IO thread and we might race against
  49. * main thread hot-plugging the device.
  50. * We assume that as soon as .realized is set to true we can let
  51. * the user access the device.
  52. */
  53. if (retval && !include_unrealized &&
  54. !qatomic_load_acquire(&retval->qdev.realized)) {
  55. retval = NULL;
  56. }
  57. return retval;
  58. }
  59. SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun)
  60. {
  61. RCU_READ_LOCK_GUARD();
  62. return do_scsi_device_find(bus, channel, id, lun, false);
  63. }
  64. SCSIDevice *scsi_device_get(SCSIBus *bus, int channel, int id, int lun)
  65. {
  66. SCSIDevice *d;
  67. RCU_READ_LOCK_GUARD();
  68. d = do_scsi_device_find(bus, channel, id, lun, false);
  69. if (d) {
  70. object_ref(d);
  71. }
  72. return d;
  73. }
  74. static void scsi_device_realize(SCSIDevice *s, Error **errp)
  75. {
  76. SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
  77. if (sc->realize) {
  78. sc->realize(s, errp);
  79. }
  80. }
  81. static void scsi_device_unrealize(SCSIDevice *s)
  82. {
  83. SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
  84. if (sc->unrealize) {
  85. sc->unrealize(s);
  86. }
  87. }
  88. int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
  89. void *hba_private)
  90. {
  91. SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
  92. int rc;
  93. assert(cmd->len == 0);
  94. rc = scsi_req_parse_cdb(dev, cmd, buf);
  95. if (bus->info->parse_cdb) {
  96. rc = bus->info->parse_cdb(dev, cmd, buf, hba_private);
  97. }
  98. return rc;
  99. }
  100. static SCSIRequest *scsi_device_alloc_req(SCSIDevice *s, uint32_t tag, uint32_t lun,
  101. uint8_t *buf, void *hba_private)
  102. {
  103. SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
  104. if (sc->alloc_req) {
  105. return sc->alloc_req(s, tag, lun, buf, hba_private);
  106. }
  107. return NULL;
  108. }
  109. void scsi_device_unit_attention_reported(SCSIDevice *s)
  110. {
  111. SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
  112. if (sc->unit_attention_reported) {
  113. sc->unit_attention_reported(s);
  114. }
  115. }
  116. /* Create a scsi bus, and attach devices to it. */
  117. void scsi_bus_new(SCSIBus *bus, size_t bus_size, DeviceState *host,
  118. const SCSIBusInfo *info, const char *bus_name)
  119. {
  120. qbus_create_inplace(bus, bus_size, TYPE_SCSI_BUS, host, bus_name);
  121. bus->busnr = next_scsi_bus++;
  122. bus->info = info;
  123. qbus_set_bus_hotplug_handler(BUS(bus));
  124. }
  125. static void scsi_dma_restart_bh(void *opaque)
  126. {
  127. SCSIDevice *s = opaque;
  128. SCSIRequest *req, *next;
  129. qemu_bh_delete(s->bh);
  130. s->bh = NULL;
  131. aio_context_acquire(blk_get_aio_context(s->conf.blk));
  132. QTAILQ_FOREACH_SAFE(req, &s->requests, next, next) {
  133. scsi_req_ref(req);
  134. if (req->retry) {
  135. req->retry = false;
  136. switch (req->cmd.mode) {
  137. case SCSI_XFER_FROM_DEV:
  138. case SCSI_XFER_TO_DEV:
  139. scsi_req_continue(req);
  140. break;
  141. case SCSI_XFER_NONE:
  142. scsi_req_dequeue(req);
  143. scsi_req_enqueue(req);
  144. break;
  145. }
  146. }
  147. scsi_req_unref(req);
  148. }
  149. aio_context_release(blk_get_aio_context(s->conf.blk));
  150. }
  151. void scsi_req_retry(SCSIRequest *req)
  152. {
  153. /* No need to save a reference, because scsi_dma_restart_bh just
  154. * looks at the request list. */
  155. req->retry = true;
  156. }
  157. static void scsi_dma_restart_cb(void *opaque, int running, RunState state)
  158. {
  159. SCSIDevice *s = opaque;
  160. if (!running) {
  161. return;
  162. }
  163. if (!s->bh) {
  164. AioContext *ctx = blk_get_aio_context(s->conf.blk);
  165. s->bh = aio_bh_new(ctx, scsi_dma_restart_bh, s);
  166. qemu_bh_schedule(s->bh);
  167. }
  168. }
  169. static bool scsi_bus_is_address_free(SCSIBus *bus,
  170. int channel, int target, int lun,
  171. SCSIDevice **p_dev)
  172. {
  173. SCSIDevice *d;
  174. RCU_READ_LOCK_GUARD();
  175. d = do_scsi_device_find(bus, channel, target, lun, true);
  176. if (d && d->lun == lun) {
  177. if (p_dev) {
  178. *p_dev = d;
  179. }
  180. return false;
  181. }
  182. if (p_dev) {
  183. *p_dev = NULL;
  184. }
  185. return true;
  186. }
  187. static bool scsi_bus_check_address(BusState *qbus, DeviceState *qdev, Error **errp)
  188. {
  189. SCSIDevice *dev = SCSI_DEVICE(qdev);
  190. SCSIBus *bus = SCSI_BUS(qbus);
  191. if (dev->channel > bus->info->max_channel) {
  192. error_setg(errp, "bad scsi channel id: %d", dev->channel);
  193. return false;
  194. }
  195. if (dev->id != -1 && dev->id > bus->info->max_target) {
  196. error_setg(errp, "bad scsi device id: %d", dev->id);
  197. return false;
  198. }
  199. if (dev->lun != -1 && dev->lun > bus->info->max_lun) {
  200. error_setg(errp, "bad scsi device lun: %d", dev->lun);
  201. return false;
  202. }
  203. if (dev->id != -1 && dev->lun != -1) {
  204. SCSIDevice *d;
  205. if (!scsi_bus_is_address_free(bus, dev->channel, dev->id, dev->lun, &d)) {
  206. error_setg(errp, "lun already used by '%s'", d->qdev.id);
  207. return false;
  208. }
  209. }
  210. return true;
  211. }
  212. static void scsi_qdev_realize(DeviceState *qdev, Error **errp)
  213. {
  214. SCSIDevice *dev = SCSI_DEVICE(qdev);
  215. SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
  216. bool is_free;
  217. Error *local_err = NULL;
  218. if (dev->id == -1) {
  219. int id = -1;
  220. if (dev->lun == -1) {
  221. dev->lun = 0;
  222. }
  223. do {
  224. is_free = scsi_bus_is_address_free(bus, dev->channel, ++id, dev->lun, NULL);
  225. } while (!is_free && id < bus->info->max_target);
  226. if (!is_free) {
  227. error_setg(errp, "no free target");
  228. return;
  229. }
  230. dev->id = id;
  231. } else if (dev->lun == -1) {
  232. int lun = -1;
  233. do {
  234. is_free = scsi_bus_is_address_free(bus, dev->channel, dev->id, ++lun, NULL);
  235. } while (!is_free && lun < bus->info->max_lun);
  236. if (!is_free) {
  237. error_setg(errp, "no free lun");
  238. return;
  239. }
  240. dev->lun = lun;
  241. }
  242. QTAILQ_INIT(&dev->requests);
  243. scsi_device_realize(dev, &local_err);
  244. if (local_err) {
  245. error_propagate(errp, local_err);
  246. return;
  247. }
  248. dev->vmsentry = qdev_add_vm_change_state_handler(DEVICE(dev),
  249. scsi_dma_restart_cb, dev);
  250. }
  251. static void scsi_qdev_unrealize(DeviceState *qdev)
  252. {
  253. SCSIDevice *dev = SCSI_DEVICE(qdev);
  254. if (dev->vmsentry) {
  255. qemu_del_vm_change_state_handler(dev->vmsentry);
  256. }
  257. scsi_device_purge_requests(dev, SENSE_CODE(NO_SENSE));
  258. scsi_device_unrealize(dev);
  259. blockdev_mark_auto_del(dev->conf.blk);
  260. }
  261. /* handle legacy '-drive if=scsi,...' cmd line args */
  262. SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk,
  263. int unit, bool removable, int bootindex,
  264. bool share_rw,
  265. BlockdevOnError rerror,
  266. BlockdevOnError werror,
  267. const char *serial, Error **errp)
  268. {
  269. const char *driver;
  270. char *name;
  271. DeviceState *dev;
  272. DriveInfo *dinfo;
  273. if (blk_is_sg(blk)) {
  274. driver = "scsi-generic";
  275. } else {
  276. dinfo = blk_legacy_dinfo(blk);
  277. if (dinfo && dinfo->media_cd) {
  278. driver = "scsi-cd";
  279. } else {
  280. driver = "scsi-hd";
  281. }
  282. }
  283. dev = qdev_new(driver);
  284. name = g_strdup_printf("legacy[%d]", unit);
  285. object_property_add_child(OBJECT(bus), name, OBJECT(dev));
  286. g_free(name);
  287. qdev_prop_set_uint32(dev, "scsi-id", unit);
  288. if (bootindex >= 0) {
  289. object_property_set_int(OBJECT(dev), "bootindex", bootindex,
  290. &error_abort);
  291. }
  292. if (object_property_find(OBJECT(dev), "removable")) {
  293. qdev_prop_set_bit(dev, "removable", removable);
  294. }
  295. if (serial && object_property_find(OBJECT(dev), "serial")) {
  296. qdev_prop_set_string(dev, "serial", serial);
  297. }
  298. if (!qdev_prop_set_drive_err(dev, "drive", blk, errp)) {
  299. object_unparent(OBJECT(dev));
  300. return NULL;
  301. }
  302. if (!object_property_set_bool(OBJECT(dev), "share-rw", share_rw, errp)) {
  303. object_unparent(OBJECT(dev));
  304. return NULL;
  305. }
  306. qdev_prop_set_enum(dev, "rerror", rerror);
  307. qdev_prop_set_enum(dev, "werror", werror);
  308. if (!qdev_realize_and_unref(dev, &bus->qbus, errp)) {
  309. object_unparent(OBJECT(dev));
  310. return NULL;
  311. }
  312. return SCSI_DEVICE(dev);
  313. }
  314. void scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
  315. {
  316. Location loc;
  317. DriveInfo *dinfo;
  318. int unit;
  319. loc_push_none(&loc);
  320. for (unit = 0; unit <= bus->info->max_target; unit++) {
  321. dinfo = drive_get(IF_SCSI, bus->busnr, unit);
  322. if (dinfo == NULL) {
  323. continue;
  324. }
  325. qemu_opts_loc_restore(dinfo->opts);
  326. scsi_bus_legacy_add_drive(bus, blk_by_legacy_dinfo(dinfo),
  327. unit, false, -1, false,
  328. BLOCKDEV_ON_ERROR_AUTO,
  329. BLOCKDEV_ON_ERROR_AUTO,
  330. NULL, &error_fatal);
  331. }
  332. loc_pop(&loc);
  333. }
  334. static int32_t scsi_invalid_field(SCSIRequest *req, uint8_t *buf)
  335. {
  336. scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
  337. scsi_req_complete(req, CHECK_CONDITION);
  338. return 0;
  339. }
  340. static const struct SCSIReqOps reqops_invalid_field = {
  341. .size = sizeof(SCSIRequest),
  342. .send_command = scsi_invalid_field
  343. };
  344. /* SCSIReqOps implementation for invalid commands. */
  345. static int32_t scsi_invalid_command(SCSIRequest *req, uint8_t *buf)
  346. {
  347. scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
  348. scsi_req_complete(req, CHECK_CONDITION);
  349. return 0;
  350. }
  351. static const struct SCSIReqOps reqops_invalid_opcode = {
  352. .size = sizeof(SCSIRequest),
  353. .send_command = scsi_invalid_command
  354. };
  355. /* SCSIReqOps implementation for unit attention conditions. */
  356. static int32_t scsi_unit_attention(SCSIRequest *req, uint8_t *buf)
  357. {
  358. if (req->dev->unit_attention.key == UNIT_ATTENTION) {
  359. scsi_req_build_sense(req, req->dev->unit_attention);
  360. } else if (req->bus->unit_attention.key == UNIT_ATTENTION) {
  361. scsi_req_build_sense(req, req->bus->unit_attention);
  362. }
  363. scsi_req_complete(req, CHECK_CONDITION);
  364. return 0;
  365. }
  366. static const struct SCSIReqOps reqops_unit_attention = {
  367. .size = sizeof(SCSIRequest),
  368. .send_command = scsi_unit_attention
  369. };
  370. /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
  371. an invalid LUN. */
  372. typedef struct SCSITargetReq SCSITargetReq;
  373. struct SCSITargetReq {
  374. SCSIRequest req;
  375. int len;
  376. uint8_t *buf;
  377. int buf_len;
  378. };
  379. static void store_lun(uint8_t *outbuf, int lun)
  380. {
  381. if (lun < 256) {
  382. /* Simple logical unit addressing method*/
  383. outbuf[0] = 0;
  384. outbuf[1] = lun;
  385. } else {
  386. /* Flat space addressing method */
  387. outbuf[0] = 0x40 | (lun >> 8);
  388. outbuf[1] = (lun & 255);
  389. }
  390. }
  391. static bool scsi_target_emulate_report_luns(SCSITargetReq *r)
  392. {
  393. BusChild *kid;
  394. int channel, id;
  395. uint8_t tmp[8] = {0};
  396. int len = 0;
  397. GByteArray *buf;
  398. if (r->req.cmd.xfer < 16) {
  399. return false;
  400. }
  401. if (r->req.cmd.buf[2] > 2) {
  402. return false;
  403. }
  404. /* reserve space for 63 LUNs*/
  405. buf = g_byte_array_sized_new(512);
  406. channel = r->req.dev->channel;
  407. id = r->req.dev->id;
  408. /* add size (will be updated later to correct value */
  409. g_byte_array_append(buf, tmp, 8);
  410. len += 8;
  411. /* add LUN0 */
  412. g_byte_array_append(buf, tmp, 8);
  413. len += 8;
  414. WITH_RCU_READ_LOCK_GUARD() {
  415. QTAILQ_FOREACH_RCU(kid, &r->req.bus->qbus.children, sibling) {
  416. DeviceState *qdev = kid->child;
  417. SCSIDevice *dev = SCSI_DEVICE(qdev);
  418. if (dev->channel == channel && dev->id == id && dev->lun != 0) {
  419. store_lun(tmp, dev->lun);
  420. g_byte_array_append(buf, tmp, 8);
  421. len += 8;
  422. }
  423. }
  424. }
  425. r->buf_len = len;
  426. r->buf = g_byte_array_free(buf, FALSE);
  427. r->len = MIN(len, r->req.cmd.xfer & ~7);
  428. /* store the LUN list length */
  429. stl_be_p(&r->buf[0], len - 8);
  430. return true;
  431. }
  432. static bool scsi_target_emulate_inquiry(SCSITargetReq *r)
  433. {
  434. assert(r->req.dev->lun != r->req.lun);
  435. scsi_target_alloc_buf(&r->req, SCSI_INQUIRY_LEN);
  436. if (r->req.cmd.buf[1] & 0x2) {
  437. /* Command support data - optional, not implemented */
  438. return false;
  439. }
  440. if (r->req.cmd.buf[1] & 0x1) {
  441. /* Vital product data */
  442. uint8_t page_code = r->req.cmd.buf[2];
  443. r->buf[r->len++] = page_code ; /* this page */
  444. r->buf[r->len++] = 0x00;
  445. switch (page_code) {
  446. case 0x00: /* Supported page codes, mandatory */
  447. {
  448. int pages;
  449. pages = r->len++;
  450. r->buf[r->len++] = 0x00; /* list of supported pages (this page) */
  451. r->buf[pages] = r->len - pages - 1; /* number of pages */
  452. break;
  453. }
  454. default:
  455. return false;
  456. }
  457. /* done with EVPD */
  458. assert(r->len < r->buf_len);
  459. r->len = MIN(r->req.cmd.xfer, r->len);
  460. return true;
  461. }
  462. /* Standard INQUIRY data */
  463. if (r->req.cmd.buf[2] != 0) {
  464. return false;
  465. }
  466. /* PAGE CODE == 0 */
  467. r->len = MIN(r->req.cmd.xfer, SCSI_INQUIRY_LEN);
  468. memset(r->buf, 0, r->len);
  469. if (r->req.lun != 0) {
  470. r->buf[0] = TYPE_NO_LUN;
  471. } else {
  472. r->buf[0] = TYPE_NOT_PRESENT | TYPE_INACTIVE;
  473. r->buf[2] = 5; /* Version */
  474. r->buf[3] = 2 | 0x10; /* HiSup, response data format */
  475. r->buf[4] = r->len - 5; /* Additional Length = (Len - 1) - 4 */
  476. r->buf[7] = 0x10 | (r->req.bus->info->tcq ? 0x02 : 0); /* Sync, TCQ. */
  477. memcpy(&r->buf[8], "QEMU ", 8);
  478. memcpy(&r->buf[16], "QEMU TARGET ", 16);
  479. pstrcpy((char *) &r->buf[32], 4, qemu_hw_version());
  480. }
  481. return true;
  482. }
  483. static size_t scsi_sense_len(SCSIRequest *req)
  484. {
  485. if (req->dev->type == TYPE_SCANNER)
  486. return SCSI_SENSE_LEN_SCANNER;
  487. else
  488. return SCSI_SENSE_LEN;
  489. }
  490. static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf)
  491. {
  492. SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
  493. int fixed_sense = (req->cmd.buf[1] & 1) == 0;
  494. if (req->lun != 0 &&
  495. buf[0] != INQUIRY && buf[0] != REQUEST_SENSE) {
  496. scsi_req_build_sense(req, SENSE_CODE(LUN_NOT_SUPPORTED));
  497. scsi_req_complete(req, CHECK_CONDITION);
  498. return 0;
  499. }
  500. switch (buf[0]) {
  501. case REPORT_LUNS:
  502. if (!scsi_target_emulate_report_luns(r)) {
  503. goto illegal_request;
  504. }
  505. break;
  506. case INQUIRY:
  507. if (!scsi_target_emulate_inquiry(r)) {
  508. goto illegal_request;
  509. }
  510. break;
  511. case REQUEST_SENSE:
  512. scsi_target_alloc_buf(&r->req, scsi_sense_len(req));
  513. if (req->lun != 0) {
  514. const struct SCSISense sense = SENSE_CODE(LUN_NOT_SUPPORTED);
  515. r->len = scsi_build_sense_buf(r->buf, req->cmd.xfer,
  516. sense, fixed_sense);
  517. } else {
  518. r->len = scsi_device_get_sense(r->req.dev, r->buf,
  519. MIN(req->cmd.xfer, r->buf_len),
  520. fixed_sense);
  521. }
  522. if (r->req.dev->sense_is_ua) {
  523. scsi_device_unit_attention_reported(req->dev);
  524. r->req.dev->sense_len = 0;
  525. r->req.dev->sense_is_ua = false;
  526. }
  527. break;
  528. case TEST_UNIT_READY:
  529. break;
  530. default:
  531. scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
  532. scsi_req_complete(req, CHECK_CONDITION);
  533. return 0;
  534. illegal_request:
  535. scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
  536. scsi_req_complete(req, CHECK_CONDITION);
  537. return 0;
  538. }
  539. if (!r->len) {
  540. scsi_req_complete(req, GOOD);
  541. }
  542. return r->len;
  543. }
  544. static void scsi_target_read_data(SCSIRequest *req)
  545. {
  546. SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
  547. uint32_t n;
  548. n = r->len;
  549. if (n > 0) {
  550. r->len = 0;
  551. scsi_req_data(&r->req, n);
  552. } else {
  553. scsi_req_complete(&r->req, GOOD);
  554. }
  555. }
  556. static uint8_t *scsi_target_get_buf(SCSIRequest *req)
  557. {
  558. SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
  559. return r->buf;
  560. }
  561. static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len)
  562. {
  563. SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
  564. r->buf = g_malloc(len);
  565. r->buf_len = len;
  566. return r->buf;
  567. }
  568. static void scsi_target_free_buf(SCSIRequest *req)
  569. {
  570. SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
  571. g_free(r->buf);
  572. }
  573. static const struct SCSIReqOps reqops_target_command = {
  574. .size = sizeof(SCSITargetReq),
  575. .send_command = scsi_target_send_command,
  576. .read_data = scsi_target_read_data,
  577. .get_buf = scsi_target_get_buf,
  578. .free_req = scsi_target_free_buf,
  579. };
  580. SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
  581. uint32_t tag, uint32_t lun, void *hba_private)
  582. {
  583. SCSIRequest *req;
  584. SCSIBus *bus = scsi_bus_from_device(d);
  585. BusState *qbus = BUS(bus);
  586. const int memset_off = offsetof(SCSIRequest, sense)
  587. + sizeof(req->sense);
  588. req = g_malloc(reqops->size);
  589. memset((uint8_t *)req + memset_off, 0, reqops->size - memset_off);
  590. req->refcount = 1;
  591. req->bus = bus;
  592. req->dev = d;
  593. req->tag = tag;
  594. req->lun = lun;
  595. req->hba_private = hba_private;
  596. req->status = -1;
  597. req->ops = reqops;
  598. object_ref(OBJECT(d));
  599. object_ref(OBJECT(qbus->parent));
  600. notifier_list_init(&req->cancel_notifiers);
  601. trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
  602. return req;
  603. }
  604. SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
  605. uint8_t *buf, void *hba_private)
  606. {
  607. SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
  608. const SCSIReqOps *ops;
  609. SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(d);
  610. SCSIRequest *req;
  611. SCSICommand cmd = { .len = 0 };
  612. int ret;
  613. if ((d->unit_attention.key == UNIT_ATTENTION ||
  614. bus->unit_attention.key == UNIT_ATTENTION) &&
  615. (buf[0] != INQUIRY &&
  616. buf[0] != REPORT_LUNS &&
  617. buf[0] != GET_CONFIGURATION &&
  618. buf[0] != GET_EVENT_STATUS_NOTIFICATION &&
  619. /*
  620. * If we already have a pending unit attention condition,
  621. * report this one before triggering another one.
  622. */
  623. !(buf[0] == REQUEST_SENSE && d->sense_is_ua))) {
  624. ops = &reqops_unit_attention;
  625. } else if (lun != d->lun ||
  626. buf[0] == REPORT_LUNS ||
  627. (buf[0] == REQUEST_SENSE && d->sense_len)) {
  628. ops = &reqops_target_command;
  629. } else {
  630. ops = NULL;
  631. }
  632. if (ops != NULL || !sc->parse_cdb) {
  633. ret = scsi_req_parse_cdb(d, &cmd, buf);
  634. } else {
  635. ret = sc->parse_cdb(d, &cmd, buf, hba_private);
  636. }
  637. if (ret != 0) {
  638. trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]);
  639. req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private);
  640. } else {
  641. assert(cmd.len != 0);
  642. trace_scsi_req_parsed(d->id, lun, tag, buf[0],
  643. cmd.mode, cmd.xfer);
  644. if (cmd.lba != -1) {
  645. trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0],
  646. cmd.lba);
  647. }
  648. if (cmd.xfer > INT32_MAX) {
  649. req = scsi_req_alloc(&reqops_invalid_field, d, tag, lun, hba_private);
  650. } else if (ops) {
  651. req = scsi_req_alloc(ops, d, tag, lun, hba_private);
  652. } else {
  653. req = scsi_device_alloc_req(d, tag, lun, buf, hba_private);
  654. }
  655. }
  656. req->cmd = cmd;
  657. req->resid = req->cmd.xfer;
  658. switch (buf[0]) {
  659. case INQUIRY:
  660. trace_scsi_inquiry(d->id, lun, tag, cmd.buf[1], cmd.buf[2]);
  661. break;
  662. case TEST_UNIT_READY:
  663. trace_scsi_test_unit_ready(d->id, lun, tag);
  664. break;
  665. case REPORT_LUNS:
  666. trace_scsi_report_luns(d->id, lun, tag);
  667. break;
  668. case REQUEST_SENSE:
  669. trace_scsi_request_sense(d->id, lun, tag);
  670. break;
  671. default:
  672. break;
  673. }
  674. return req;
  675. }
  676. uint8_t *scsi_req_get_buf(SCSIRequest *req)
  677. {
  678. return req->ops->get_buf(req);
  679. }
  680. static void scsi_clear_unit_attention(SCSIRequest *req)
  681. {
  682. SCSISense *ua;
  683. if (req->dev->unit_attention.key != UNIT_ATTENTION &&
  684. req->bus->unit_attention.key != UNIT_ATTENTION) {
  685. return;
  686. }
  687. /*
  688. * If an INQUIRY command enters the enabled command state,
  689. * the device server shall [not] clear any unit attention condition;
  690. * See also MMC-6, paragraphs 6.5 and 6.6.2.
  691. */
  692. if (req->cmd.buf[0] == INQUIRY ||
  693. req->cmd.buf[0] == GET_CONFIGURATION ||
  694. req->cmd.buf[0] == GET_EVENT_STATUS_NOTIFICATION) {
  695. return;
  696. }
  697. if (req->dev->unit_attention.key == UNIT_ATTENTION) {
  698. ua = &req->dev->unit_attention;
  699. } else {
  700. ua = &req->bus->unit_attention;
  701. }
  702. /*
  703. * If a REPORT LUNS command enters the enabled command state, [...]
  704. * the device server shall clear any pending unit attention condition
  705. * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
  706. */
  707. if (req->cmd.buf[0] == REPORT_LUNS &&
  708. !(ua->asc == SENSE_CODE(REPORTED_LUNS_CHANGED).asc &&
  709. ua->ascq == SENSE_CODE(REPORTED_LUNS_CHANGED).ascq)) {
  710. return;
  711. }
  712. *ua = SENSE_CODE(NO_SENSE);
  713. }
  714. int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len)
  715. {
  716. int ret;
  717. assert(len >= 14);
  718. if (!req->sense_len) {
  719. return 0;
  720. }
  721. ret = scsi_convert_sense(req->sense, req->sense_len, buf, len, true);
  722. /*
  723. * FIXME: clearing unit attention conditions upon autosense should be done
  724. * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
  725. * (SAM-5, 5.14).
  726. *
  727. * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
  728. * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
  729. * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
  730. */
  731. if (req->dev->sense_is_ua) {
  732. scsi_device_unit_attention_reported(req->dev);
  733. req->dev->sense_len = 0;
  734. req->dev->sense_is_ua = false;
  735. }
  736. return ret;
  737. }
  738. int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed)
  739. {
  740. return scsi_convert_sense(dev->sense, dev->sense_len, buf, len, fixed);
  741. }
  742. void scsi_req_build_sense(SCSIRequest *req, SCSISense sense)
  743. {
  744. trace_scsi_req_build_sense(req->dev->id, req->lun, req->tag,
  745. sense.key, sense.asc, sense.ascq);
  746. req->sense_len = scsi_build_sense(req->sense, sense);
  747. }
  748. static void scsi_req_enqueue_internal(SCSIRequest *req)
  749. {
  750. assert(!req->enqueued);
  751. scsi_req_ref(req);
  752. if (req->bus->info->get_sg_list) {
  753. req->sg = req->bus->info->get_sg_list(req);
  754. } else {
  755. req->sg = NULL;
  756. }
  757. req->enqueued = true;
  758. QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
  759. }
  760. int32_t scsi_req_enqueue(SCSIRequest *req)
  761. {
  762. int32_t rc;
  763. assert(!req->retry);
  764. scsi_req_enqueue_internal(req);
  765. scsi_req_ref(req);
  766. rc = req->ops->send_command(req, req->cmd.buf);
  767. scsi_req_unref(req);
  768. return rc;
  769. }
  770. static void scsi_req_dequeue(SCSIRequest *req)
  771. {
  772. trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
  773. req->retry = false;
  774. if (req->enqueued) {
  775. QTAILQ_REMOVE(&req->dev->requests, req, next);
  776. req->enqueued = false;
  777. scsi_req_unref(req);
  778. }
  779. }
  780. static int scsi_get_performance_length(int num_desc, int type, int data_type)
  781. {
  782. /* MMC-6, paragraph 6.7. */
  783. switch (type) {
  784. case 0:
  785. if ((data_type & 3) == 0) {
  786. /* Each descriptor is as in Table 295 - Nominal performance. */
  787. return 16 * num_desc + 8;
  788. } else {
  789. /* Each descriptor is as in Table 296 - Exceptions. */
  790. return 6 * num_desc + 8;
  791. }
  792. case 1:
  793. case 4:
  794. case 5:
  795. return 8 * num_desc + 8;
  796. case 2:
  797. return 2048 * num_desc + 8;
  798. case 3:
  799. return 16 * num_desc + 8;
  800. default:
  801. return 8;
  802. }
  803. }
  804. static int ata_passthrough_xfer_unit(SCSIDevice *dev, uint8_t *buf)
  805. {
  806. int byte_block = (buf[2] >> 2) & 0x1;
  807. int type = (buf[2] >> 4) & 0x1;
  808. int xfer_unit;
  809. if (byte_block) {
  810. if (type) {
  811. xfer_unit = dev->blocksize;
  812. } else {
  813. xfer_unit = 512;
  814. }
  815. } else {
  816. xfer_unit = 1;
  817. }
  818. return xfer_unit;
  819. }
  820. static int ata_passthrough_12_xfer(SCSIDevice *dev, uint8_t *buf)
  821. {
  822. int length = buf[2] & 0x3;
  823. int xfer;
  824. int unit = ata_passthrough_xfer_unit(dev, buf);
  825. switch (length) {
  826. case 0:
  827. case 3: /* USB-specific. */
  828. default:
  829. xfer = 0;
  830. break;
  831. case 1:
  832. xfer = buf[3];
  833. break;
  834. case 2:
  835. xfer = buf[4];
  836. break;
  837. }
  838. return xfer * unit;
  839. }
  840. static int ata_passthrough_16_xfer(SCSIDevice *dev, uint8_t *buf)
  841. {
  842. int extend = buf[1] & 0x1;
  843. int length = buf[2] & 0x3;
  844. int xfer;
  845. int unit = ata_passthrough_xfer_unit(dev, buf);
  846. switch (length) {
  847. case 0:
  848. case 3: /* USB-specific. */
  849. default:
  850. xfer = 0;
  851. break;
  852. case 1:
  853. xfer = buf[4];
  854. xfer |= (extend ? buf[3] << 8 : 0);
  855. break;
  856. case 2:
  857. xfer = buf[6];
  858. xfer |= (extend ? buf[5] << 8 : 0);
  859. break;
  860. }
  861. return xfer * unit;
  862. }
  863. static int scsi_req_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
  864. {
  865. cmd->xfer = scsi_cdb_xfer(buf);
  866. switch (buf[0]) {
  867. case TEST_UNIT_READY:
  868. case REWIND:
  869. case START_STOP:
  870. case SET_CAPACITY:
  871. case WRITE_FILEMARKS:
  872. case WRITE_FILEMARKS_16:
  873. case SPACE:
  874. case RESERVE:
  875. case RELEASE:
  876. case ERASE:
  877. case ALLOW_MEDIUM_REMOVAL:
  878. case SEEK_10:
  879. case SYNCHRONIZE_CACHE:
  880. case SYNCHRONIZE_CACHE_16:
  881. case LOCATE_16:
  882. case LOCK_UNLOCK_CACHE:
  883. case SET_CD_SPEED:
  884. case SET_LIMITS:
  885. case WRITE_LONG_10:
  886. case UPDATE_BLOCK:
  887. case RESERVE_TRACK:
  888. case SET_READ_AHEAD:
  889. case PRE_FETCH:
  890. case PRE_FETCH_16:
  891. case ALLOW_OVERWRITE:
  892. cmd->xfer = 0;
  893. break;
  894. case VERIFY_10:
  895. case VERIFY_12:
  896. case VERIFY_16:
  897. if ((buf[1] & 2) == 0) {
  898. cmd->xfer = 0;
  899. } else if ((buf[1] & 4) != 0) {
  900. cmd->xfer = 1;
  901. }
  902. cmd->xfer *= dev->blocksize;
  903. break;
  904. case MODE_SENSE:
  905. break;
  906. case WRITE_SAME_10:
  907. case WRITE_SAME_16:
  908. cmd->xfer = buf[1] & 1 ? 0 : dev->blocksize;
  909. break;
  910. case READ_CAPACITY_10:
  911. cmd->xfer = 8;
  912. break;
  913. case READ_BLOCK_LIMITS:
  914. cmd->xfer = 6;
  915. break;
  916. case SEND_VOLUME_TAG:
  917. /* GPCMD_SET_STREAMING from multimedia commands. */
  918. if (dev->type == TYPE_ROM) {
  919. cmd->xfer = buf[10] | (buf[9] << 8);
  920. } else {
  921. cmd->xfer = buf[9] | (buf[8] << 8);
  922. }
  923. break;
  924. case WRITE_6:
  925. /* length 0 means 256 blocks */
  926. if (cmd->xfer == 0) {
  927. cmd->xfer = 256;
  928. }
  929. /* fall through */
  930. case WRITE_10:
  931. case WRITE_VERIFY_10:
  932. case WRITE_12:
  933. case WRITE_VERIFY_12:
  934. case WRITE_16:
  935. case WRITE_VERIFY_16:
  936. cmd->xfer *= dev->blocksize;
  937. break;
  938. case READ_6:
  939. case READ_REVERSE:
  940. /* length 0 means 256 blocks */
  941. if (cmd->xfer == 0) {
  942. cmd->xfer = 256;
  943. }
  944. /* fall through */
  945. case READ_10:
  946. case READ_12:
  947. case READ_16:
  948. cmd->xfer *= dev->blocksize;
  949. break;
  950. case FORMAT_UNIT:
  951. /* MMC mandates the parameter list to be 12-bytes long. Parameters
  952. * for block devices are restricted to the header right now. */
  953. if (dev->type == TYPE_ROM && (buf[1] & 16)) {
  954. cmd->xfer = 12;
  955. } else {
  956. cmd->xfer = (buf[1] & 16) == 0 ? 0 : (buf[1] & 32 ? 8 : 4);
  957. }
  958. break;
  959. case INQUIRY:
  960. case RECEIVE_DIAGNOSTIC:
  961. case SEND_DIAGNOSTIC:
  962. cmd->xfer = buf[4] | (buf[3] << 8);
  963. break;
  964. case READ_CD:
  965. case READ_BUFFER:
  966. case WRITE_BUFFER:
  967. case SEND_CUE_SHEET:
  968. cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
  969. break;
  970. case PERSISTENT_RESERVE_OUT:
  971. cmd->xfer = ldl_be_p(&buf[5]) & 0xffffffffULL;
  972. break;
  973. case ERASE_12:
  974. if (dev->type == TYPE_ROM) {
  975. /* MMC command GET PERFORMANCE. */
  976. cmd->xfer = scsi_get_performance_length(buf[9] | (buf[8] << 8),
  977. buf[10], buf[1] & 0x1f);
  978. }
  979. break;
  980. case MECHANISM_STATUS:
  981. case READ_DVD_STRUCTURE:
  982. case SEND_DVD_STRUCTURE:
  983. case MAINTENANCE_OUT:
  984. case MAINTENANCE_IN:
  985. if (dev->type == TYPE_ROM) {
  986. /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
  987. cmd->xfer = buf[9] | (buf[8] << 8);
  988. }
  989. break;
  990. case ATA_PASSTHROUGH_12:
  991. if (dev->type == TYPE_ROM) {
  992. /* BLANK command of MMC */
  993. cmd->xfer = 0;
  994. } else {
  995. cmd->xfer = ata_passthrough_12_xfer(dev, buf);
  996. }
  997. break;
  998. case ATA_PASSTHROUGH_16:
  999. cmd->xfer = ata_passthrough_16_xfer(dev, buf);
  1000. break;
  1001. }
  1002. return 0;
  1003. }
  1004. static int scsi_req_stream_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
  1005. {
  1006. switch (buf[0]) {
  1007. /* stream commands */
  1008. case ERASE_12:
  1009. case ERASE_16:
  1010. cmd->xfer = 0;
  1011. break;
  1012. case READ_6:
  1013. case READ_REVERSE:
  1014. case RECOVER_BUFFERED_DATA:
  1015. case WRITE_6:
  1016. cmd->xfer = buf[4] | (buf[3] << 8) | (buf[2] << 16);
  1017. if (buf[1] & 0x01) { /* fixed */
  1018. cmd->xfer *= dev->blocksize;
  1019. }
  1020. break;
  1021. case READ_16:
  1022. case READ_REVERSE_16:
  1023. case VERIFY_16:
  1024. case WRITE_16:
  1025. cmd->xfer = buf[14] | (buf[13] << 8) | (buf[12] << 16);
  1026. if (buf[1] & 0x01) { /* fixed */
  1027. cmd->xfer *= dev->blocksize;
  1028. }
  1029. break;
  1030. case REWIND:
  1031. case LOAD_UNLOAD:
  1032. cmd->xfer = 0;
  1033. break;
  1034. case SPACE_16:
  1035. cmd->xfer = buf[13] | (buf[12] << 8);
  1036. break;
  1037. case READ_POSITION:
  1038. switch (buf[1] & 0x1f) /* operation code */ {
  1039. case SHORT_FORM_BLOCK_ID:
  1040. case SHORT_FORM_VENDOR_SPECIFIC:
  1041. cmd->xfer = 20;
  1042. break;
  1043. case LONG_FORM:
  1044. cmd->xfer = 32;
  1045. break;
  1046. case EXTENDED_FORM:
  1047. cmd->xfer = buf[8] | (buf[7] << 8);
  1048. break;
  1049. default:
  1050. return -1;
  1051. }
  1052. break;
  1053. case FORMAT_UNIT:
  1054. cmd->xfer = buf[4] | (buf[3] << 8);
  1055. break;
  1056. /* generic commands */
  1057. default:
  1058. return scsi_req_xfer(cmd, dev, buf);
  1059. }
  1060. return 0;
  1061. }
  1062. static int scsi_req_medium_changer_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
  1063. {
  1064. switch (buf[0]) {
  1065. /* medium changer commands */
  1066. case EXCHANGE_MEDIUM:
  1067. case INITIALIZE_ELEMENT_STATUS:
  1068. case INITIALIZE_ELEMENT_STATUS_WITH_RANGE:
  1069. case MOVE_MEDIUM:
  1070. case POSITION_TO_ELEMENT:
  1071. cmd->xfer = 0;
  1072. break;
  1073. case READ_ELEMENT_STATUS:
  1074. cmd->xfer = buf[9] | (buf[8] << 8) | (buf[7] << 16);
  1075. break;
  1076. /* generic commands */
  1077. default:
  1078. return scsi_req_xfer(cmd, dev, buf);
  1079. }
  1080. return 0;
  1081. }
  1082. static int scsi_req_scanner_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
  1083. {
  1084. switch (buf[0]) {
  1085. /* Scanner commands */
  1086. case OBJECT_POSITION:
  1087. cmd->xfer = 0;
  1088. break;
  1089. case SCAN:
  1090. cmd->xfer = buf[4];
  1091. break;
  1092. case READ_10:
  1093. case SEND:
  1094. case GET_WINDOW:
  1095. case SET_WINDOW:
  1096. cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
  1097. break;
  1098. default:
  1099. /* GET_DATA_BUFFER_STATUS xfer handled by scsi_req_xfer */
  1100. return scsi_req_xfer(cmd, dev, buf);
  1101. }
  1102. return 0;
  1103. }
  1104. static void scsi_cmd_xfer_mode(SCSICommand *cmd)
  1105. {
  1106. if (!cmd->xfer) {
  1107. cmd->mode = SCSI_XFER_NONE;
  1108. return;
  1109. }
  1110. switch (cmd->buf[0]) {
  1111. case WRITE_6:
  1112. case WRITE_10:
  1113. case WRITE_VERIFY_10:
  1114. case WRITE_12:
  1115. case WRITE_VERIFY_12:
  1116. case WRITE_16:
  1117. case WRITE_VERIFY_16:
  1118. case VERIFY_10:
  1119. case VERIFY_12:
  1120. case VERIFY_16:
  1121. case COPY:
  1122. case COPY_VERIFY:
  1123. case COMPARE:
  1124. case CHANGE_DEFINITION:
  1125. case LOG_SELECT:
  1126. case MODE_SELECT:
  1127. case MODE_SELECT_10:
  1128. case SEND_DIAGNOSTIC:
  1129. case WRITE_BUFFER:
  1130. case FORMAT_UNIT:
  1131. case REASSIGN_BLOCKS:
  1132. case SEARCH_EQUAL:
  1133. case SEARCH_HIGH:
  1134. case SEARCH_LOW:
  1135. case UPDATE_BLOCK:
  1136. case WRITE_LONG_10:
  1137. case WRITE_SAME_10:
  1138. case WRITE_SAME_16:
  1139. case UNMAP:
  1140. case SEARCH_HIGH_12:
  1141. case SEARCH_EQUAL_12:
  1142. case SEARCH_LOW_12:
  1143. case MEDIUM_SCAN:
  1144. case SEND_VOLUME_TAG:
  1145. case SEND_CUE_SHEET:
  1146. case SEND_DVD_STRUCTURE:
  1147. case PERSISTENT_RESERVE_OUT:
  1148. case MAINTENANCE_OUT:
  1149. case SET_WINDOW:
  1150. case SCAN:
  1151. /* SCAN conflicts with START_STOP. START_STOP has cmd->xfer set to 0 for
  1152. * non-scanner devices, so we only get here for SCAN and not for START_STOP.
  1153. */
  1154. cmd->mode = SCSI_XFER_TO_DEV;
  1155. break;
  1156. case ATA_PASSTHROUGH_12:
  1157. case ATA_PASSTHROUGH_16:
  1158. /* T_DIR */
  1159. cmd->mode = (cmd->buf[2] & 0x8) ?
  1160. SCSI_XFER_FROM_DEV : SCSI_XFER_TO_DEV;
  1161. break;
  1162. default:
  1163. cmd->mode = SCSI_XFER_FROM_DEV;
  1164. break;
  1165. }
  1166. }
  1167. int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf)
  1168. {
  1169. int rc;
  1170. int len;
  1171. cmd->lba = -1;
  1172. len = scsi_cdb_length(buf);
  1173. if (len < 0) {
  1174. return -1;
  1175. }
  1176. cmd->len = len;
  1177. switch (dev->type) {
  1178. case TYPE_TAPE:
  1179. rc = scsi_req_stream_xfer(cmd, dev, buf);
  1180. break;
  1181. case TYPE_MEDIUM_CHANGER:
  1182. rc = scsi_req_medium_changer_xfer(cmd, dev, buf);
  1183. break;
  1184. case TYPE_SCANNER:
  1185. rc = scsi_req_scanner_length(cmd, dev, buf);
  1186. break;
  1187. default:
  1188. rc = scsi_req_xfer(cmd, dev, buf);
  1189. break;
  1190. }
  1191. if (rc != 0)
  1192. return rc;
  1193. memcpy(cmd->buf, buf, cmd->len);
  1194. scsi_cmd_xfer_mode(cmd);
  1195. cmd->lba = scsi_cmd_lba(cmd);
  1196. return 0;
  1197. }
  1198. void scsi_device_report_change(SCSIDevice *dev, SCSISense sense)
  1199. {
  1200. SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
  1201. scsi_device_set_ua(dev, sense);
  1202. if (bus->info->change) {
  1203. bus->info->change(bus, dev, sense);
  1204. }
  1205. }
  1206. SCSIRequest *scsi_req_ref(SCSIRequest *req)
  1207. {
  1208. assert(req->refcount > 0);
  1209. req->refcount++;
  1210. return req;
  1211. }
  1212. void scsi_req_unref(SCSIRequest *req)
  1213. {
  1214. assert(req->refcount > 0);
  1215. if (--req->refcount == 0) {
  1216. BusState *qbus = req->dev->qdev.parent_bus;
  1217. SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, qbus);
  1218. if (bus->info->free_request && req->hba_private) {
  1219. bus->info->free_request(bus, req->hba_private);
  1220. }
  1221. if (req->ops->free_req) {
  1222. req->ops->free_req(req);
  1223. }
  1224. object_unref(OBJECT(req->dev));
  1225. object_unref(OBJECT(qbus->parent));
  1226. g_free(req);
  1227. }
  1228. }
  1229. /* Tell the device that we finished processing this chunk of I/O. It
  1230. will start the next chunk or complete the command. */
  1231. void scsi_req_continue(SCSIRequest *req)
  1232. {
  1233. if (req->io_canceled) {
  1234. trace_scsi_req_continue_canceled(req->dev->id, req->lun, req->tag);
  1235. return;
  1236. }
  1237. trace_scsi_req_continue(req->dev->id, req->lun, req->tag);
  1238. if (req->cmd.mode == SCSI_XFER_TO_DEV) {
  1239. req->ops->write_data(req);
  1240. } else {
  1241. req->ops->read_data(req);
  1242. }
  1243. }
  1244. /* Called by the devices when data is ready for the HBA. The HBA should
  1245. start a DMA operation to read or fill the device's data buffer.
  1246. Once it completes, calling scsi_req_continue will restart I/O. */
  1247. void scsi_req_data(SCSIRequest *req, int len)
  1248. {
  1249. uint8_t *buf;
  1250. if (req->io_canceled) {
  1251. trace_scsi_req_data_canceled(req->dev->id, req->lun, req->tag, len);
  1252. return;
  1253. }
  1254. trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
  1255. assert(req->cmd.mode != SCSI_XFER_NONE);
  1256. if (!req->sg) {
  1257. req->resid -= len;
  1258. req->bus->info->transfer_data(req, len);
  1259. return;
  1260. }
  1261. /* If the device calls scsi_req_data and the HBA specified a
  1262. * scatter/gather list, the transfer has to happen in a single
  1263. * step. */
  1264. assert(!req->dma_started);
  1265. req->dma_started = true;
  1266. buf = scsi_req_get_buf(req);
  1267. if (req->cmd.mode == SCSI_XFER_FROM_DEV) {
  1268. req->resid = dma_buf_read(buf, len, req->sg);
  1269. } else {
  1270. req->resid = dma_buf_write(buf, len, req->sg);
  1271. }
  1272. scsi_req_continue(req);
  1273. }
  1274. void scsi_req_print(SCSIRequest *req)
  1275. {
  1276. FILE *fp = stderr;
  1277. int i;
  1278. fprintf(fp, "[%s id=%d] %s",
  1279. req->dev->qdev.parent_bus->name,
  1280. req->dev->id,
  1281. scsi_command_name(req->cmd.buf[0]));
  1282. for (i = 1; i < req->cmd.len; i++) {
  1283. fprintf(fp, " 0x%02x", req->cmd.buf[i]);
  1284. }
  1285. switch (req->cmd.mode) {
  1286. case SCSI_XFER_NONE:
  1287. fprintf(fp, " - none\n");
  1288. break;
  1289. case SCSI_XFER_FROM_DEV:
  1290. fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
  1291. break;
  1292. case SCSI_XFER_TO_DEV:
  1293. fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
  1294. break;
  1295. default:
  1296. fprintf(fp, " - Oops\n");
  1297. break;
  1298. }
  1299. }
  1300. void scsi_req_complete(SCSIRequest *req, int status)
  1301. {
  1302. assert(req->status == -1);
  1303. req->status = status;
  1304. assert(req->sense_len <= sizeof(req->sense));
  1305. if (status == GOOD) {
  1306. req->sense_len = 0;
  1307. }
  1308. if (req->sense_len) {
  1309. memcpy(req->dev->sense, req->sense, req->sense_len);
  1310. req->dev->sense_len = req->sense_len;
  1311. req->dev->sense_is_ua = (req->ops == &reqops_unit_attention);
  1312. } else {
  1313. req->dev->sense_len = 0;
  1314. req->dev->sense_is_ua = false;
  1315. }
  1316. /*
  1317. * Unit attention state is now stored in the device's sense buffer
  1318. * if the HBA didn't do autosense. Clear the pending unit attention
  1319. * flags.
  1320. */
  1321. scsi_clear_unit_attention(req);
  1322. scsi_req_ref(req);
  1323. scsi_req_dequeue(req);
  1324. req->bus->info->complete(req, req->status, req->resid);
  1325. /* Cancelled requests might end up being completed instead of cancelled */
  1326. notifier_list_notify(&req->cancel_notifiers, req);
  1327. scsi_req_unref(req);
  1328. }
  1329. /* Called by the devices when the request is canceled. */
  1330. void scsi_req_cancel_complete(SCSIRequest *req)
  1331. {
  1332. assert(req->io_canceled);
  1333. if (req->bus->info->cancel) {
  1334. req->bus->info->cancel(req);
  1335. }
  1336. notifier_list_notify(&req->cancel_notifiers, req);
  1337. scsi_req_unref(req);
  1338. }
  1339. /* Cancel @req asynchronously. @notifier is added to @req's cancellation
  1340. * notifier list, the bus will be notified the requests cancellation is
  1341. * completed.
  1342. * */
  1343. void scsi_req_cancel_async(SCSIRequest *req, Notifier *notifier)
  1344. {
  1345. trace_scsi_req_cancel(req->dev->id, req->lun, req->tag);
  1346. if (notifier) {
  1347. notifier_list_add(&req->cancel_notifiers, notifier);
  1348. }
  1349. if (req->io_canceled) {
  1350. /* A blk_aio_cancel_async is pending; when it finishes,
  1351. * scsi_req_cancel_complete will be called and will
  1352. * call the notifier we just added. Just wait for that.
  1353. */
  1354. assert(req->aiocb);
  1355. return;
  1356. }
  1357. /* Dropped in scsi_req_cancel_complete. */
  1358. scsi_req_ref(req);
  1359. scsi_req_dequeue(req);
  1360. req->io_canceled = true;
  1361. if (req->aiocb) {
  1362. blk_aio_cancel_async(req->aiocb);
  1363. } else {
  1364. scsi_req_cancel_complete(req);
  1365. }
  1366. }
  1367. void scsi_req_cancel(SCSIRequest *req)
  1368. {
  1369. trace_scsi_req_cancel(req->dev->id, req->lun, req->tag);
  1370. if (!req->enqueued) {
  1371. return;
  1372. }
  1373. assert(!req->io_canceled);
  1374. /* Dropped in scsi_req_cancel_complete. */
  1375. scsi_req_ref(req);
  1376. scsi_req_dequeue(req);
  1377. req->io_canceled = true;
  1378. if (req->aiocb) {
  1379. blk_aio_cancel(req->aiocb);
  1380. } else {
  1381. scsi_req_cancel_complete(req);
  1382. }
  1383. }
  1384. static int scsi_ua_precedence(SCSISense sense)
  1385. {
  1386. if (sense.key != UNIT_ATTENTION) {
  1387. return INT_MAX;
  1388. }
  1389. if (sense.asc == 0x29 && sense.ascq == 0x04) {
  1390. /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
  1391. return 1;
  1392. } else if (sense.asc == 0x3F && sense.ascq == 0x01) {
  1393. /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
  1394. return 2;
  1395. } else if (sense.asc == 0x29 && (sense.ascq == 0x05 || sense.ascq == 0x06)) {
  1396. /* These two go with "all others". */
  1397. ;
  1398. } else if (sense.asc == 0x29 && sense.ascq <= 0x07) {
  1399. /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
  1400. * POWER ON OCCURRED = 1
  1401. * SCSI BUS RESET OCCURRED = 2
  1402. * BUS DEVICE RESET FUNCTION OCCURRED = 3
  1403. * I_T NEXUS LOSS OCCURRED = 7
  1404. */
  1405. return sense.ascq;
  1406. } else if (sense.asc == 0x2F && sense.ascq == 0x01) {
  1407. /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
  1408. return 8;
  1409. }
  1410. return (sense.asc << 8) | sense.ascq;
  1411. }
  1412. void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense)
  1413. {
  1414. int prec1, prec2;
  1415. if (sense.key != UNIT_ATTENTION) {
  1416. return;
  1417. }
  1418. trace_scsi_device_set_ua(sdev->id, sdev->lun, sense.key,
  1419. sense.asc, sense.ascq);
  1420. /*
  1421. * Override a pre-existing unit attention condition, except for a more
  1422. * important reset condition.
  1423. */
  1424. prec1 = scsi_ua_precedence(sdev->unit_attention);
  1425. prec2 = scsi_ua_precedence(sense);
  1426. if (prec2 < prec1) {
  1427. sdev->unit_attention = sense;
  1428. }
  1429. }
  1430. void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense)
  1431. {
  1432. SCSIRequest *req;
  1433. aio_context_acquire(blk_get_aio_context(sdev->conf.blk));
  1434. while (!QTAILQ_EMPTY(&sdev->requests)) {
  1435. req = QTAILQ_FIRST(&sdev->requests);
  1436. scsi_req_cancel_async(req, NULL);
  1437. }
  1438. blk_drain(sdev->conf.blk);
  1439. aio_context_release(blk_get_aio_context(sdev->conf.blk));
  1440. scsi_device_set_ua(sdev, sense);
  1441. }
  1442. static char *scsibus_get_dev_path(DeviceState *dev)
  1443. {
  1444. SCSIDevice *d = SCSI_DEVICE(dev);
  1445. DeviceState *hba = dev->parent_bus->parent;
  1446. char *id;
  1447. char *path;
  1448. id = qdev_get_dev_path(hba);
  1449. if (id) {
  1450. path = g_strdup_printf("%s/%d:%d:%d", id, d->channel, d->id, d->lun);
  1451. } else {
  1452. path = g_strdup_printf("%d:%d:%d", d->channel, d->id, d->lun);
  1453. }
  1454. g_free(id);
  1455. return path;
  1456. }
  1457. static char *scsibus_get_fw_dev_path(DeviceState *dev)
  1458. {
  1459. SCSIDevice *d = SCSI_DEVICE(dev);
  1460. return g_strdup_printf("channel@%x/%s@%x,%x", d->channel,
  1461. qdev_fw_name(dev), d->id, d->lun);
  1462. }
  1463. /* SCSI request list. For simplicity, pv points to the whole device */
  1464. static int put_scsi_requests(QEMUFile *f, void *pv, size_t size,
  1465. const VMStateField *field, QJSON *vmdesc)
  1466. {
  1467. SCSIDevice *s = pv;
  1468. SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus);
  1469. SCSIRequest *req;
  1470. QTAILQ_FOREACH(req, &s->requests, next) {
  1471. assert(!req->io_canceled);
  1472. assert(req->status == -1);
  1473. assert(req->enqueued);
  1474. qemu_put_sbyte(f, req->retry ? 1 : 2);
  1475. qemu_put_buffer(f, req->cmd.buf, sizeof(req->cmd.buf));
  1476. qemu_put_be32s(f, &req->tag);
  1477. qemu_put_be32s(f, &req->lun);
  1478. if (bus->info->save_request) {
  1479. bus->info->save_request(f, req);
  1480. }
  1481. if (req->ops->save_request) {
  1482. req->ops->save_request(f, req);
  1483. }
  1484. }
  1485. qemu_put_sbyte(f, 0);
  1486. return 0;
  1487. }
  1488. static int get_scsi_requests(QEMUFile *f, void *pv, size_t size,
  1489. const VMStateField *field)
  1490. {
  1491. SCSIDevice *s = pv;
  1492. SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus);
  1493. int8_t sbyte;
  1494. while ((sbyte = qemu_get_sbyte(f)) > 0) {
  1495. uint8_t buf[SCSI_CMD_BUF_SIZE];
  1496. uint32_t tag;
  1497. uint32_t lun;
  1498. SCSIRequest *req;
  1499. qemu_get_buffer(f, buf, sizeof(buf));
  1500. qemu_get_be32s(f, &tag);
  1501. qemu_get_be32s(f, &lun);
  1502. req = scsi_req_new(s, tag, lun, buf, NULL);
  1503. req->retry = (sbyte == 1);
  1504. if (bus->info->load_request) {
  1505. req->hba_private = bus->info->load_request(f, req);
  1506. }
  1507. if (req->ops->load_request) {
  1508. req->ops->load_request(f, req);
  1509. }
  1510. /* Just restart it later. */
  1511. scsi_req_enqueue_internal(req);
  1512. /* At this point, the request will be kept alive by the reference
  1513. * added by scsi_req_enqueue_internal, so we can release our reference.
  1514. * The HBA of course will add its own reference in the load_request
  1515. * callback if it needs to hold on the SCSIRequest.
  1516. */
  1517. scsi_req_unref(req);
  1518. }
  1519. return 0;
  1520. }
  1521. static const VMStateInfo vmstate_info_scsi_requests = {
  1522. .name = "scsi-requests",
  1523. .get = get_scsi_requests,
  1524. .put = put_scsi_requests,
  1525. };
  1526. static bool scsi_sense_state_needed(void *opaque)
  1527. {
  1528. SCSIDevice *s = opaque;
  1529. return s->sense_len > SCSI_SENSE_BUF_SIZE_OLD;
  1530. }
  1531. static const VMStateDescription vmstate_scsi_sense_state = {
  1532. .name = "SCSIDevice/sense",
  1533. .version_id = 1,
  1534. .minimum_version_id = 1,
  1535. .needed = scsi_sense_state_needed,
  1536. .fields = (VMStateField[]) {
  1537. VMSTATE_UINT8_SUB_ARRAY(sense, SCSIDevice,
  1538. SCSI_SENSE_BUF_SIZE_OLD,
  1539. SCSI_SENSE_BUF_SIZE - SCSI_SENSE_BUF_SIZE_OLD),
  1540. VMSTATE_END_OF_LIST()
  1541. }
  1542. };
  1543. const VMStateDescription vmstate_scsi_device = {
  1544. .name = "SCSIDevice",
  1545. .version_id = 1,
  1546. .minimum_version_id = 1,
  1547. .fields = (VMStateField[]) {
  1548. VMSTATE_UINT8(unit_attention.key, SCSIDevice),
  1549. VMSTATE_UINT8(unit_attention.asc, SCSIDevice),
  1550. VMSTATE_UINT8(unit_attention.ascq, SCSIDevice),
  1551. VMSTATE_BOOL(sense_is_ua, SCSIDevice),
  1552. VMSTATE_UINT8_SUB_ARRAY(sense, SCSIDevice, 0, SCSI_SENSE_BUF_SIZE_OLD),
  1553. VMSTATE_UINT32(sense_len, SCSIDevice),
  1554. {
  1555. .name = "requests",
  1556. .version_id = 0,
  1557. .field_exists = NULL,
  1558. .size = 0, /* ouch */
  1559. .info = &vmstate_info_scsi_requests,
  1560. .flags = VMS_SINGLE,
  1561. .offset = 0,
  1562. },
  1563. VMSTATE_END_OF_LIST()
  1564. },
  1565. .subsections = (const VMStateDescription*[]) {
  1566. &vmstate_scsi_sense_state,
  1567. NULL
  1568. }
  1569. };
  1570. static Property scsi_props[] = {
  1571. DEFINE_PROP_UINT32("channel", SCSIDevice, channel, 0),
  1572. DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1),
  1573. DEFINE_PROP_UINT32("lun", SCSIDevice, lun, -1),
  1574. DEFINE_PROP_END_OF_LIST(),
  1575. };
  1576. static void scsi_device_class_init(ObjectClass *klass, void *data)
  1577. {
  1578. DeviceClass *k = DEVICE_CLASS(klass);
  1579. set_bit(DEVICE_CATEGORY_STORAGE, k->categories);
  1580. k->bus_type = TYPE_SCSI_BUS;
  1581. k->realize = scsi_qdev_realize;
  1582. k->unrealize = scsi_qdev_unrealize;
  1583. device_class_set_props(k, scsi_props);
  1584. }
  1585. static void scsi_dev_instance_init(Object *obj)
  1586. {
  1587. DeviceState *dev = DEVICE(obj);
  1588. SCSIDevice *s = SCSI_DEVICE(dev);
  1589. device_add_bootindex_property(obj, &s->conf.bootindex,
  1590. "bootindex", NULL,
  1591. &s->qdev);
  1592. }
  1593. static const TypeInfo scsi_device_type_info = {
  1594. .name = TYPE_SCSI_DEVICE,
  1595. .parent = TYPE_DEVICE,
  1596. .instance_size = sizeof(SCSIDevice),
  1597. .abstract = true,
  1598. .class_size = sizeof(SCSIDeviceClass),
  1599. .class_init = scsi_device_class_init,
  1600. .instance_init = scsi_dev_instance_init,
  1601. };
  1602. static void scsi_bus_class_init(ObjectClass *klass, void *data)
  1603. {
  1604. BusClass *k = BUS_CLASS(klass);
  1605. HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
  1606. k->get_dev_path = scsibus_get_dev_path;
  1607. k->get_fw_dev_path = scsibus_get_fw_dev_path;
  1608. k->check_address = scsi_bus_check_address;
  1609. hc->unplug = qdev_simple_device_unplug_cb;
  1610. }
  1611. static const TypeInfo scsi_bus_info = {
  1612. .name = TYPE_SCSI_BUS,
  1613. .parent = TYPE_BUS,
  1614. .instance_size = sizeof(SCSIBus),
  1615. .class_init = scsi_bus_class_init,
  1616. .interfaces = (InterfaceInfo[]) {
  1617. { TYPE_HOTPLUG_HANDLER },
  1618. { }
  1619. }
  1620. };
  1621. static void scsi_register_types(void)
  1622. {
  1623. type_register_static(&scsi_bus_info);
  1624. type_register_static(&scsi_device_type_info);
  1625. }
  1626. type_init(scsi_register_types)