scsi-bus.c 53 KB

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