scsi-bus.c 51 KB

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