scsi-bus.c 48 KB

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