/share/examples/scsi_target/scsi_cmds.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 813 lines · 656 code · 82 blank · 75 comment · 140 complexity · e3879a0092c72402bcd8b3114899d406 MD5 · raw file

  1. /*
  2. * SCSI Disk Emulator
  3. *
  4. * Copyright (c) 2002 Nate Lawson.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions, and the following disclaimer,
  12. * without modification, immediately at the beginning of the file.
  13. * 2. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  20. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. *
  28. * $FreeBSD$
  29. */
  30. #include <stdio.h>
  31. #include <stddef.h>
  32. #include <stdarg.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <err.h>
  36. #include <aio.h>
  37. #include <unistd.h>
  38. #include <assert.h>
  39. #include <sys/param.h>
  40. #include <sys/types.h>
  41. #include <cam/cam.h>
  42. #include <cam/cam_ccb.h>
  43. #include <cam/scsi/scsi_all.h>
  44. #include <cam/scsi/scsi_targetio.h>
  45. #include "scsi_target.h"
  46. typedef int targ_start_func(struct ccb_accept_tio *, struct ccb_scsiio *);
  47. typedef void targ_done_func(struct ccb_accept_tio *, struct ccb_scsiio *,
  48. io_ops);
  49. #ifndef REPORT_LUNS
  50. #define REPORT_LUNS 0xa0
  51. #endif
  52. struct targ_cdb_handlers {
  53. u_int8_t cmd;
  54. targ_start_func *start;
  55. targ_done_func *done;
  56. #define ILLEGAL_CDB 0xFF
  57. };
  58. static targ_start_func tcmd_inquiry;
  59. static targ_start_func tcmd_req_sense;
  60. static targ_start_func tcmd_rd_cap;
  61. #ifdef READ_16
  62. static targ_start_func tcmd_rd_cap16;
  63. #endif
  64. static targ_start_func tcmd_rdwr;
  65. static targ_start_func tcmd_rdwr_decode;
  66. static targ_done_func tcmd_rdwr_done;
  67. static targ_start_func tcmd_null_ok;
  68. static targ_start_func tcmd_illegal_req;
  69. static int start_io(struct ccb_accept_tio *atio,
  70. struct ccb_scsiio *ctio, int dir);
  71. static int init_inquiry(u_int16_t req_flags, u_int16_t sim_flags);
  72. static struct initiator_state *
  73. tcmd_get_istate(u_int init_id);
  74. static void cdb_debug(u_int8_t *cdb, const char *msg, ...);
  75. static struct targ_cdb_handlers cdb_handlers[] = {
  76. { READ_10, tcmd_rdwr, tcmd_rdwr_done },
  77. { WRITE_10, tcmd_rdwr, tcmd_rdwr_done },
  78. { READ_6, tcmd_rdwr, tcmd_rdwr_done },
  79. { WRITE_6, tcmd_rdwr, tcmd_rdwr_done },
  80. { INQUIRY, tcmd_inquiry, NULL },
  81. { REQUEST_SENSE, tcmd_req_sense, NULL },
  82. { READ_CAPACITY, tcmd_rd_cap, NULL },
  83. { TEST_UNIT_READY, tcmd_null_ok, NULL },
  84. { START_STOP_UNIT, tcmd_null_ok, NULL },
  85. { SYNCHRONIZE_CACHE, tcmd_null_ok, NULL },
  86. { MODE_SENSE_6, tcmd_illegal_req, NULL },
  87. { MODE_SELECT_6, tcmd_illegal_req, NULL },
  88. { REPORT_LUNS, tcmd_illegal_req, NULL },
  89. #ifdef READ_16
  90. { READ_16, tcmd_rdwr, tcmd_rdwr_done },
  91. { WRITE_16, tcmd_rdwr, tcmd_rdwr_done },
  92. { SERVICE_ACTION_IN, tcmd_rd_cap16, NULL },
  93. #endif
  94. { ILLEGAL_CDB, NULL, NULL }
  95. };
  96. static struct scsi_inquiry_data inq_data;
  97. static struct initiator_state istates[MAX_INITIATORS];
  98. extern int debug;
  99. extern off_t volume_size;
  100. extern u_int sector_size;
  101. extern size_t buf_size;
  102. cam_status
  103. tcmd_init(u_int16_t req_inq_flags, u_int16_t sim_inq_flags)
  104. {
  105. struct initiator_state *istate;
  106. int i, ret;
  107. /* Initialize our inquiry data */
  108. ret = init_inquiry(req_inq_flags, sim_inq_flags);
  109. if (ret != 0)
  110. return (ret);
  111. /* We start out life with a UA to indicate power-on/reset. */
  112. for (i = 0; i < MAX_INITIATORS; i++) {
  113. istate = tcmd_get_istate(i);
  114. bzero(istate, sizeof(*istate));
  115. istate->pending_ua = UA_POWER_ON;
  116. }
  117. return (0);
  118. }
  119. /* Caller allocates CTIO, sets its init_id
  120. return 0 if done, 1 if more processing needed
  121. on 0, caller sets SEND_STATUS */
  122. int
  123. tcmd_handle(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio, io_ops event)
  124. {
  125. static struct targ_cdb_handlers *last_cmd;
  126. struct initiator_state *istate;
  127. struct atio_descr *a_descr;
  128. int ret;
  129. if (debug) {
  130. warnx("tcmd_handle atio %p ctio %p atioflags %#x", atio, ctio,
  131. atio->ccb_h.flags);
  132. }
  133. ret = 0;
  134. a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
  135. /* Do a full lookup if one-behind cache failed */
  136. if (last_cmd == NULL || last_cmd->cmd != a_descr->cdb[0]) {
  137. struct targ_cdb_handlers *h;
  138. for (h = cdb_handlers; h->cmd != ILLEGAL_CDB; h++) {
  139. if (a_descr->cdb[0] == h->cmd)
  140. break;
  141. }
  142. last_cmd = h;
  143. }
  144. /* call completion and exit */
  145. if (event != ATIO_WORK) {
  146. if (last_cmd->done != NULL)
  147. last_cmd->done(atio, ctio, event);
  148. else
  149. free_ccb((union ccb *)ctio);
  150. return (1);
  151. }
  152. if (last_cmd->cmd == ILLEGAL_CDB) {
  153. if (event != ATIO_WORK) {
  154. warnx("no done func for %#x???", a_descr->cdb[0]);
  155. abort();
  156. }
  157. /* Not found, return illegal request */
  158. warnx("cdb %#x not handled", a_descr->cdb[0]);
  159. tcmd_illegal_req(atio, ctio);
  160. send_ccb((union ccb *)ctio, /*priority*/1);
  161. return (0);
  162. }
  163. istate = tcmd_get_istate(ctio->init_id);
  164. if (istate == NULL) {
  165. tcmd_illegal_req(atio, ctio);
  166. send_ccb((union ccb *)ctio, /*priority*/1);
  167. return (0);
  168. }
  169. if (istate->pending_ca == 0 && istate->pending_ua != 0 &&
  170. a_descr->cdb[0] != INQUIRY) {
  171. tcmd_sense(ctio->init_id, ctio, SSD_KEY_UNIT_ATTENTION,
  172. 0x29, istate->pending_ua == UA_POWER_ON ? 1 : 2);
  173. istate->pending_ca = CA_UNIT_ATTN;
  174. if (debug) {
  175. cdb_debug(a_descr->cdb, "UA active for %u: ",
  176. atio->init_id);
  177. }
  178. send_ccb((union ccb *)ctio, /*priority*/1);
  179. return (0);
  180. }
  181. /* Store current CA and UA for later */
  182. istate->orig_ua = istate->pending_ua;
  183. istate->orig_ca = istate->pending_ca;
  184. /*
  185. * As per SAM2, any command that occurs
  186. * after a CA is reported, clears the CA. We must
  187. * also clear the UA condition, if any, that caused
  188. * the CA to occur assuming the UA is not for a
  189. * persistent condition.
  190. */
  191. istate->pending_ca = CA_NONE;
  192. if (istate->orig_ca == CA_UNIT_ATTN)
  193. istate->pending_ua = UA_NONE;
  194. /* If we have a valid handler, call start or completion function */
  195. if (last_cmd->cmd != ILLEGAL_CDB) {
  196. ret = last_cmd->start(atio, ctio);
  197. /* XXX hack */
  198. if (last_cmd->start != tcmd_rdwr) {
  199. a_descr->init_req += ctio->dxfer_len;
  200. send_ccb((union ccb *)ctio, /*priority*/1);
  201. }
  202. }
  203. return (ret);
  204. }
  205. static struct initiator_state *
  206. tcmd_get_istate(u_int init_id)
  207. {
  208. if (init_id >= MAX_INITIATORS) {
  209. warnx("illegal init_id %d, max %d", init_id, MAX_INITIATORS - 1);
  210. return (NULL);
  211. } else {
  212. return (&istates[init_id]);
  213. }
  214. }
  215. void
  216. tcmd_sense(u_int init_id, struct ccb_scsiio *ctio, u_int8_t flags,
  217. u_int8_t asc, u_int8_t ascq)
  218. {
  219. struct initiator_state *istate;
  220. struct scsi_sense_data_fixed *sense;
  221. /* Set our initiator's istate */
  222. istate = tcmd_get_istate(init_id);
  223. if (istate == NULL)
  224. return;
  225. istate->pending_ca |= CA_CMD_SENSE; /* XXX set instead of or? */
  226. sense = (struct scsi_sense_data_fixed *)&istate->sense_data;
  227. bzero(sense, sizeof(*sense));
  228. sense->error_code = SSD_CURRENT_ERROR;
  229. sense->flags = flags;
  230. sense->add_sense_code = asc;
  231. sense->add_sense_code_qual = ascq;
  232. sense->extra_len =
  233. offsetof(struct scsi_sense_data_fixed, sense_key_spec[2]) -
  234. offsetof(struct scsi_sense_data_fixed, extra_len);
  235. /* Fill out the supplied CTIO */
  236. if (ctio != NULL) {
  237. bcopy(sense, &ctio->sense_data, sizeof(*sense));
  238. ctio->sense_len = sizeof(*sense); /* XXX */
  239. ctio->ccb_h.flags &= ~CAM_DIR_MASK;
  240. ctio->ccb_h.flags |= CAM_DIR_NONE | CAM_SEND_SENSE |
  241. CAM_SEND_STATUS;
  242. ctio->dxfer_len = 0;
  243. ctio->scsi_status = SCSI_STATUS_CHECK_COND;
  244. }
  245. }
  246. void
  247. tcmd_ua(u_int init_id, ua_types new_ua)
  248. {
  249. struct initiator_state *istate;
  250. u_int start, end;
  251. if (init_id == CAM_TARGET_WILDCARD) {
  252. start = 0;
  253. end = MAX_INITIATORS - 1;
  254. } else {
  255. start = end = init_id;
  256. }
  257. for (; start <= end; start++) {
  258. istate = tcmd_get_istate(start);
  259. if (istate == NULL)
  260. break;
  261. istate->pending_ua = new_ua;
  262. }
  263. }
  264. static int
  265. tcmd_inquiry(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)
  266. {
  267. struct scsi_inquiry *inq;
  268. struct atio_descr *a_descr;
  269. struct initiator_state *istate;
  270. struct scsi_sense_data_fixed *sense;
  271. a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
  272. inq = (struct scsi_inquiry *)a_descr->cdb;
  273. if (debug)
  274. cdb_debug(a_descr->cdb, "INQUIRY from %u: ", atio->init_id);
  275. /*
  276. * Validate the command. We don't support any VPD pages, so
  277. * complain if EVPD or CMDDT is set.
  278. */
  279. istate = tcmd_get_istate(ctio->init_id);
  280. sense = (struct scsi_sense_data_fixed *)&istate->sense_data;
  281. if ((inq->byte2 & SI_EVPD) != 0) {
  282. tcmd_illegal_req(atio, ctio);
  283. sense->sense_key_spec[0] = SSD_SCS_VALID | SSD_FIELDPTR_CMD |
  284. SSD_BITPTR_VALID | /*bit value*/1;
  285. sense->sense_key_spec[1] = 0;
  286. sense->sense_key_spec[2] =
  287. offsetof(struct scsi_inquiry, byte2);
  288. } else if (inq->page_code != 0) {
  289. tcmd_illegal_req(atio, ctio);
  290. sense->sense_key_spec[0] = SSD_SCS_VALID | SSD_FIELDPTR_CMD;
  291. sense->sense_key_spec[1] = 0;
  292. sense->sense_key_spec[2] =
  293. offsetof(struct scsi_inquiry, page_code);
  294. } else {
  295. bcopy(&inq_data, ctio->data_ptr, sizeof(inq_data));
  296. ctio->dxfer_len = inq_data.additional_length + 4;
  297. ctio->dxfer_len = min(ctio->dxfer_len,
  298. scsi_2btoul(inq->length));
  299. ctio->ccb_h.flags |= CAM_DIR_IN | CAM_SEND_STATUS;
  300. ctio->scsi_status = SCSI_STATUS_OK;
  301. }
  302. return (0);
  303. }
  304. /* Initialize the inquiry response structure with the requested flags */
  305. static int
  306. init_inquiry(u_int16_t req_flags, u_int16_t sim_flags)
  307. {
  308. struct scsi_inquiry_data *inq;
  309. inq = &inq_data;
  310. bzero(inq, sizeof(*inq));
  311. inq->device = T_DIRECT | (SID_QUAL_LU_CONNECTED << 5);
  312. #ifdef SCSI_REV_SPC
  313. inq->version = SCSI_REV_SPC; /* was 2 */
  314. #else
  315. inq->version = SCSI_REV_3; /* was 2 */
  316. #endif
  317. /*
  318. * XXX cpi.hba_inquiry doesn't support Addr16 so we give the
  319. * user what they want if they ask for it.
  320. */
  321. if ((req_flags & SID_Addr16) != 0) {
  322. sim_flags |= SID_Addr16;
  323. warnx("Not sure SIM supports Addr16 but enabling it anyway");
  324. }
  325. /* Advertise only what the SIM can actually support */
  326. req_flags &= sim_flags;
  327. scsi_ulto2b(req_flags, &inq->spc2_flags);
  328. inq->response_format = 2; /* SCSI2 Inquiry Format */
  329. inq->additional_length = SHORT_INQUIRY_LENGTH -
  330. offsetof(struct scsi_inquiry_data, additional_length);
  331. bcopy("FreeBSD ", inq->vendor, SID_VENDOR_SIZE);
  332. bcopy("Emulated Disk ", inq->product, SID_PRODUCT_SIZE);
  333. bcopy("0.1 ", inq->revision, SID_REVISION_SIZE);
  334. return (0);
  335. }
  336. static int
  337. tcmd_req_sense(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)
  338. {
  339. struct scsi_request_sense *rsense;
  340. struct scsi_sense_data_fixed *sense;
  341. struct initiator_state *istate;
  342. size_t dlen;
  343. struct atio_descr *a_descr;
  344. a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
  345. rsense = (struct scsi_request_sense *)a_descr->cdb;
  346. istate = tcmd_get_istate(ctio->init_id);
  347. sense = (struct scsi_sense_data_fixed *)&istate->sense_data;
  348. if (debug) {
  349. cdb_debug(a_descr->cdb, "REQ SENSE from %u: ", atio->init_id);
  350. warnx("Sending sense: %#x %#x %#x", sense->flags,
  351. sense->add_sense_code, sense->add_sense_code_qual);
  352. }
  353. if (istate->orig_ca == 0) {
  354. tcmd_sense(ctio->init_id, NULL, SSD_KEY_NO_SENSE, 0, 0);
  355. warnx("REQUEST SENSE from %u but no pending CA!",
  356. ctio->init_id);
  357. }
  358. bcopy(sense, ctio->data_ptr, sizeof(struct scsi_sense_data));
  359. dlen = offsetof(struct scsi_sense_data_fixed, extra_len) +
  360. sense->extra_len + 1;
  361. ctio->dxfer_len = min(dlen, SCSI_CDB6_LEN(rsense->length));
  362. ctio->ccb_h.flags |= CAM_DIR_IN | CAM_SEND_STATUS;
  363. ctio->scsi_status = SCSI_STATUS_OK;
  364. return (0);
  365. }
  366. static int
  367. tcmd_rd_cap(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)
  368. {
  369. struct scsi_read_capacity_data *srp;
  370. struct atio_descr *a_descr;
  371. uint32_t vsize;
  372. a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
  373. srp = (struct scsi_read_capacity_data *)ctio->data_ptr;
  374. if (volume_size > 0xffffffff)
  375. vsize = 0xffffffff;
  376. else
  377. vsize = (uint32_t)(volume_size - 1);
  378. if (debug) {
  379. cdb_debug(a_descr->cdb, "READ CAP from %u (%u, %u): ",
  380. atio->init_id, vsize, sector_size);
  381. }
  382. bzero(srp, sizeof(*srp));
  383. scsi_ulto4b(vsize, srp->addr);
  384. scsi_ulto4b(sector_size, srp->length);
  385. ctio->dxfer_len = sizeof(*srp);
  386. ctio->ccb_h.flags |= CAM_DIR_IN | CAM_SEND_STATUS;
  387. ctio->scsi_status = SCSI_STATUS_OK;
  388. return (0);
  389. }
  390. #ifdef READ_16
  391. static int
  392. tcmd_rd_cap16(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)
  393. {
  394. struct scsi_read_capacity_16 *scsi_cmd;
  395. struct scsi_read_capacity_data_long *srp;
  396. struct atio_descr *a_descr;
  397. a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
  398. scsi_cmd = (struct scsi_read_capacity_16 *)a_descr->cdb;
  399. srp = (struct scsi_read_capacity_data_long *)ctio->data_ptr;
  400. if (scsi_cmd->service_action != SRC16_SERVICE_ACTION) {
  401. tcmd_illegal_req(atio, ctio);
  402. return (0);
  403. }
  404. if (debug) {
  405. cdb_debug(a_descr->cdb, "READ CAP16 from %u (%u, %u): ",
  406. atio->init_id, volume_size - 1, sector_size);
  407. }
  408. bzero(srp, sizeof(*srp));
  409. scsi_u64to8b(volume_size - 1, srp->addr);
  410. scsi_ulto4b(sector_size, srp->length);
  411. ctio->dxfer_len = sizeof(*srp);
  412. ctio->ccb_h.flags |= CAM_DIR_IN | CAM_SEND_STATUS;
  413. ctio->scsi_status = SCSI_STATUS_OK;
  414. return (0);
  415. }
  416. #endif
  417. static int
  418. tcmd_rdwr(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)
  419. {
  420. struct atio_descr *a_descr;
  421. struct ctio_descr *c_descr;
  422. int ret;
  423. a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
  424. c_descr = (struct ctio_descr *)ctio->ccb_h.targ_descr;
  425. /* Command needs to be decoded */
  426. if ((a_descr->flags & CAM_DIR_MASK) == CAM_DIR_BOTH) {
  427. if (debug)
  428. warnx("Calling rdwr_decode");
  429. ret = tcmd_rdwr_decode(atio, ctio);
  430. if (ret == 0) {
  431. send_ccb((union ccb *)ctio, /*priority*/1);
  432. return (0);
  433. }
  434. }
  435. ctio->ccb_h.flags |= a_descr->flags;
  436. /* Call appropriate work function */
  437. if ((a_descr->flags & CAM_DIR_IN) != 0) {
  438. ret = start_io(atio, ctio, CAM_DIR_IN);
  439. if (debug)
  440. warnx("Starting %p DIR_IN @" OFF_FMT ":%u",
  441. a_descr, c_descr->offset, a_descr->targ_req);
  442. } else {
  443. ret = start_io(atio, ctio, CAM_DIR_OUT);
  444. if (debug)
  445. warnx("Starting %p DIR_OUT @" OFF_FMT ":%u",
  446. a_descr, c_descr->offset, a_descr->init_req);
  447. }
  448. return (ret);
  449. }
  450. static int
  451. tcmd_rdwr_decode(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)
  452. {
  453. uint64_t blkno;
  454. uint32_t count;
  455. struct atio_descr *a_descr;
  456. u_int8_t *cdb;
  457. a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
  458. cdb = a_descr->cdb;
  459. if (debug)
  460. cdb_debug(cdb, "R/W from %u: ", atio->init_id);
  461. switch (cdb[0]) {
  462. case READ_6:
  463. case WRITE_6:
  464. {
  465. struct scsi_rw_6 *rw_6 = (struct scsi_rw_6 *)cdb;
  466. blkno = scsi_3btoul(rw_6->addr);
  467. count = rw_6->length;
  468. break;
  469. }
  470. case READ_10:
  471. case WRITE_10:
  472. {
  473. struct scsi_rw_10 *rw_10 = (struct scsi_rw_10 *)cdb;
  474. blkno = scsi_4btoul(rw_10->addr);
  475. count = scsi_2btoul(rw_10->length);
  476. break;
  477. }
  478. #ifdef READ_16
  479. case READ_16:
  480. case WRITE_16:
  481. {
  482. struct scsi_rw_16 *rw_16 = (struct scsi_rw_16 *)cdb;
  483. blkno = scsi_8btou64(rw_16->addr);
  484. count = scsi_4btoul(rw_16->length);
  485. break;
  486. }
  487. #endif
  488. default:
  489. tcmd_illegal_req(atio, ctio);
  490. return (0);
  491. }
  492. if (blkno + count > volume_size) {
  493. warnx("Attempt to access past end of volume");
  494. tcmd_sense(ctio->init_id, ctio,
  495. SSD_KEY_ILLEGAL_REQUEST, 0x21, 0);
  496. return (0);
  497. }
  498. /* Get an (overall) data length and set direction */
  499. a_descr->base_off = ((off_t)blkno) * sector_size;
  500. a_descr->total_len = count * sector_size;
  501. if (a_descr->total_len == 0) {
  502. if (debug)
  503. warnx("r/w 0 blocks @ blkno " OFF_FMT, blkno);
  504. tcmd_null_ok(atio, ctio);
  505. return (0);
  506. } else if (cdb[0] == WRITE_6 || cdb[0] == WRITE_10) {
  507. a_descr->flags |= CAM_DIR_OUT;
  508. if (debug)
  509. warnx("write %u blocks @ blkno " OFF_FMT, count, blkno);
  510. } else {
  511. a_descr->flags |= CAM_DIR_IN;
  512. if (debug)
  513. warnx("read %u blocks @ blkno " OFF_FMT, count, blkno);
  514. }
  515. return (1);
  516. }
  517. static int
  518. start_io(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio, int dir)
  519. {
  520. struct atio_descr *a_descr;
  521. struct ctio_descr *c_descr;
  522. int ret;
  523. /* Set up common structures */
  524. a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
  525. c_descr = (struct ctio_descr *)ctio->ccb_h.targ_descr;
  526. if (dir == CAM_DIR_IN) {
  527. c_descr->offset = a_descr->base_off + a_descr->targ_req;
  528. ctio->dxfer_len = a_descr->total_len - a_descr->targ_req;
  529. } else {
  530. c_descr->offset = a_descr->base_off + a_descr->init_req;
  531. ctio->dxfer_len = a_descr->total_len - a_descr->init_req;
  532. }
  533. ctio->dxfer_len = min(ctio->dxfer_len, buf_size);
  534. assert(ctio->dxfer_len >= 0);
  535. c_descr->aiocb.aio_offset = c_descr->offset;
  536. c_descr->aiocb.aio_nbytes = ctio->dxfer_len;
  537. /* If DIR_IN, start read from target, otherwise begin CTIO xfer. */
  538. ret = 1;
  539. if (dir == CAM_DIR_IN) {
  540. if (notaio) {
  541. if (debug)
  542. warnx("read sync %lu @ block " OFF_FMT,
  543. (unsigned long)
  544. (ctio->dxfer_len / sector_size),
  545. c_descr->offset / sector_size);
  546. if (lseek(c_descr->aiocb.aio_fildes,
  547. c_descr->aiocb.aio_offset, SEEK_SET) < 0) {
  548. perror("lseek");
  549. err(1, "lseek");
  550. }
  551. if (read(c_descr->aiocb.aio_fildes,
  552. (void *)c_descr->aiocb.aio_buf,
  553. ctio->dxfer_len) != ctio->dxfer_len) {
  554. err(1, "read");
  555. }
  556. } else {
  557. if (debug)
  558. warnx("read async %lu @ block " OFF_FMT,
  559. (unsigned long)
  560. (ctio->dxfer_len / sector_size),
  561. c_descr->offset / sector_size);
  562. if (aio_read(&c_descr->aiocb) < 0) {
  563. err(1, "aio_read"); /* XXX */
  564. }
  565. }
  566. a_descr->targ_req += ctio->dxfer_len;
  567. /* if we're done, we can mark the CCB as to send status */
  568. if (a_descr->targ_req == a_descr->total_len) {
  569. ctio->ccb_h.flags |= CAM_SEND_STATUS;
  570. ctio->scsi_status = SCSI_STATUS_OK;
  571. ret = 0;
  572. }
  573. if (notaio)
  574. tcmd_rdwr_done(atio, ctio, AIO_DONE);
  575. } else {
  576. if (a_descr->targ_ack == a_descr->total_len)
  577. tcmd_null_ok(atio, ctio);
  578. a_descr->init_req += ctio->dxfer_len;
  579. if (a_descr->init_req == a_descr->total_len &&
  580. ctio->dxfer_len > 0) {
  581. /*
  582. * If data phase done, remove atio from workq.
  583. * The completion handler will call work_atio to
  584. * send the final status.
  585. */
  586. ret = 0;
  587. }
  588. send_ccb((union ccb *)ctio, /*priority*/1);
  589. }
  590. return (ret);
  591. }
  592. static void
  593. tcmd_rdwr_done(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio,
  594. io_ops event)
  595. {
  596. struct atio_descr *a_descr;
  597. struct ctio_descr *c_descr;
  598. a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
  599. c_descr = (struct ctio_descr *)ctio->ccb_h.targ_descr;
  600. switch (event) {
  601. case AIO_DONE:
  602. if (!notaio && aio_return(&c_descr->aiocb) < 0) {
  603. warn("aio_return error");
  604. /* XXX */
  605. tcmd_sense(ctio->init_id, ctio,
  606. SSD_KEY_MEDIUM_ERROR, 0, 0);
  607. send_ccb((union ccb *)ctio, /*priority*/1);
  608. break;
  609. }
  610. a_descr->targ_ack += ctio->dxfer_len;
  611. if ((a_descr->flags & CAM_DIR_IN) != 0) {
  612. if (debug) {
  613. if (notaio)
  614. warnx("sending CTIO for AIO read");
  615. else
  616. warnx("sending CTIO for sync read");
  617. }
  618. a_descr->init_req += ctio->dxfer_len;
  619. send_ccb((union ccb *)ctio, /*priority*/1);
  620. } else {
  621. /* Use work function to send final status */
  622. if (a_descr->init_req == a_descr->total_len)
  623. work_atio(atio);
  624. if (debug)
  625. warnx("AIO done freeing CTIO");
  626. free_ccb((union ccb *)ctio);
  627. }
  628. break;
  629. case CTIO_DONE:
  630. switch (ctio->ccb_h.status & CAM_STATUS_MASK) {
  631. case CAM_REQ_CMP:
  632. break;
  633. case CAM_REQUEUE_REQ:
  634. warnx("requeueing request");
  635. if ((a_descr->flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
  636. if (aio_write(&c_descr->aiocb) < 0) {
  637. err(1, "aio_write"); /* XXX */
  638. }
  639. } else {
  640. if (aio_read(&c_descr->aiocb) < 0) {
  641. err(1, "aio_read"); /* XXX */
  642. }
  643. }
  644. return;
  645. default:
  646. errx(1, "CTIO failed, status %#x", ctio->ccb_h.status);
  647. }
  648. a_descr->init_ack += ctio->dxfer_len;
  649. if ((a_descr->flags & CAM_DIR_MASK) == CAM_DIR_OUT &&
  650. ctio->dxfer_len > 0) {
  651. a_descr->targ_req += ctio->dxfer_len;
  652. if (notaio) {
  653. if (debug)
  654. warnx("write sync %lu @ block "
  655. OFF_FMT, (unsigned long)
  656. (ctio->dxfer_len / sector_size),
  657. c_descr->offset / sector_size);
  658. if (lseek(c_descr->aiocb.aio_fildes,
  659. c_descr->aiocb.aio_offset, SEEK_SET) < 0) {
  660. perror("lseek");
  661. err(1, "lseek");
  662. }
  663. if (write(c_descr->aiocb.aio_fildes,
  664. (void *) c_descr->aiocb.aio_buf,
  665. ctio->dxfer_len) != ctio->dxfer_len) {
  666. err(1, "write");
  667. }
  668. tcmd_rdwr_done(atio, ctio, AIO_DONE);
  669. } else {
  670. if (debug)
  671. warnx("write async %lu @ block "
  672. OFF_FMT, (unsigned long)
  673. (ctio->dxfer_len / sector_size),
  674. c_descr->offset / sector_size);
  675. if (aio_write(&c_descr->aiocb) < 0) {
  676. err(1, "aio_write"); /* XXX */
  677. }
  678. }
  679. } else {
  680. if (debug)
  681. warnx("CTIO done freeing CTIO");
  682. free_ccb((union ccb *)ctio);
  683. }
  684. break;
  685. default:
  686. warnx("Unknown completion code %d", event);
  687. abort();
  688. /* NOTREACHED */
  689. }
  690. }
  691. /* Simple ok message used by TUR, SYNC_CACHE, etc. */
  692. static int
  693. tcmd_null_ok(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)
  694. {
  695. if (debug) {
  696. struct atio_descr *a_descr;
  697. a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
  698. cdb_debug(a_descr->cdb, "Sending null ok to %u : ", atio->init_id);
  699. }
  700. ctio->dxfer_len = 0;
  701. ctio->ccb_h.flags &= ~CAM_DIR_MASK;
  702. ctio->ccb_h.flags |= CAM_DIR_NONE | CAM_SEND_STATUS;
  703. ctio->scsi_status = SCSI_STATUS_OK;
  704. return (0);
  705. }
  706. /* Simple illegal request message used by MODE SENSE, etc. */
  707. static int
  708. tcmd_illegal_req(struct ccb_accept_tio *atio, struct ccb_scsiio *ctio)
  709. {
  710. if (debug) {
  711. struct atio_descr *a_descr;
  712. a_descr = (struct atio_descr *)atio->ccb_h.targ_descr;
  713. cdb_debug(a_descr->cdb, "Sending ill req to %u: ", atio->init_id);
  714. }
  715. tcmd_sense(atio->init_id, ctio, SSD_KEY_ILLEGAL_REQUEST,
  716. /*asc*/0x24, /*ascq*/0);
  717. return (0);
  718. }
  719. static void
  720. cdb_debug(u_int8_t *cdb, const char *msg, ...)
  721. {
  722. char msg_buf[512];
  723. int len;
  724. va_list ap;
  725. va_start(ap, msg);
  726. vsnprintf(msg_buf, sizeof(msg_buf), msg, ap);
  727. va_end(ap);
  728. len = strlen(msg_buf);
  729. scsi_cdb_string(cdb, msg_buf + len, sizeof(msg_buf) - len);
  730. warnx("%s", msg_buf);
  731. }