scsi-command.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <arpa/inet.h>
  18. #include "iscsi.h"
  19. #include "iscsi-private.h"
  20. #include "scsi-lowlevel.h"
  21. #include "dlinklist.h"
  22. struct iscsi_scsi_cbdata {
  23. struct iscsi_scsi_cbdata *prev, *next;
  24. iscsi_command_cb callback;
  25. void *private_data;
  26. struct scsi_task *task;
  27. };
  28. void iscsi_free_scsi_cbdata(struct iscsi_scsi_cbdata *scsi_cbdata)
  29. {
  30. if (scsi_cbdata == NULL) {
  31. return;
  32. }
  33. if (scsi_cbdata->task == NULL) {
  34. scsi_free_scsi_task(scsi_cbdata->task);
  35. scsi_cbdata->task = NULL;
  36. }
  37. free(scsi_cbdata);
  38. }
  39. static void iscsi_scsi_response_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
  40. {
  41. struct iscsi_scsi_cbdata *scsi_cbdata = (struct iscsi_scsi_cbdata *)private_data;
  42. struct scsi_task *task = command_data;
  43. switch (status) {
  44. case ISCSI_STATUS_CHECK_CONDITION:
  45. scsi_cbdata->callback(iscsi, ISCSI_STATUS_CHECK_CONDITION, task, scsi_cbdata->private_data);
  46. return;
  47. case ISCSI_STATUS_GOOD:
  48. scsi_cbdata->callback(iscsi, ISCSI_STATUS_GOOD, task, scsi_cbdata->private_data);
  49. return;
  50. default:
  51. printf("Cant handle scsi status %d yet\n", status);
  52. scsi_cbdata->callback(iscsi, ISCSI_STATUS_ERROR, task, scsi_cbdata->private_data);
  53. }
  54. }
  55. static int iscsi_scsi_command_async(struct iscsi_context *iscsi, int lun, struct scsi_task *task, iscsi_command_cb cb, struct iscsi_data *data, void *private_data)
  56. {
  57. struct iscsi_pdu *pdu;
  58. struct iscsi_scsi_cbdata *scsi_cbdata;
  59. int flags;
  60. if (iscsi == NULL) {
  61. printf("trying to send command on NULL context\n");
  62. scsi_free_scsi_task(task);
  63. return -1;
  64. }
  65. if (iscsi->session_type != ISCSI_SESSION_NORMAL) {
  66. printf("Trying to send command on discovery session\n");
  67. scsi_free_scsi_task(task);
  68. return -2;
  69. }
  70. if (iscsi->is_loggedin == 0) {
  71. printf("Trying to send command while not logged in\n");
  72. scsi_free_scsi_task(task);
  73. return -3;
  74. }
  75. scsi_cbdata = malloc(sizeof(struct iscsi_scsi_cbdata));
  76. if (scsi_cbdata == NULL) {
  77. printf("failed to allocate scsi cbdata\n");
  78. scsi_free_scsi_task(task);
  79. return -4;
  80. }
  81. bzero(scsi_cbdata, sizeof(struct iscsi_scsi_cbdata));
  82. scsi_cbdata->task = task;
  83. scsi_cbdata->callback = cb;
  84. scsi_cbdata->private_data = private_data;
  85. pdu = iscsi_allocate_pdu(iscsi, ISCSI_PDU_SCSI_REQUEST, ISCSI_PDU_SCSI_RESPONSE);
  86. if (pdu == NULL) {
  87. printf("Failed to allocate text pdu\n");
  88. iscsi_free_scsi_cbdata(scsi_cbdata);
  89. return -5;
  90. }
  91. pdu->scsi_cbdata = scsi_cbdata;
  92. /* flags */
  93. flags = ISCSI_PDU_SCSI_FINAL|ISCSI_PDU_SCSI_ATTR_SIMPLE;
  94. switch (task->xfer_dir) {
  95. case SCSI_XFER_NONE:
  96. break;
  97. case SCSI_XFER_READ:
  98. flags |= ISCSI_PDU_SCSI_READ;
  99. break;
  100. case SCSI_XFER_WRITE:
  101. flags |= ISCSI_PDU_SCSI_WRITE;
  102. if (data == NULL) {
  103. printf("DATA-OUT command but data == NULL\n");
  104. iscsi_free_pdu(iscsi, pdu);
  105. return -5;
  106. }
  107. if (data->size != task->expxferlen) {
  108. printf("data size:%d is not same as expected data transfer length:%d\n", data->size, task->expxferlen);
  109. iscsi_free_pdu(iscsi, pdu);
  110. return -7;
  111. }
  112. if (iscsi_pdu_add_data(iscsi, pdu, data->data, data->size) != 0) {
  113. printf("Failed to add outdata to the pdu\n");
  114. iscsi_free_pdu(iscsi, pdu);
  115. return -6;
  116. }
  117. break;
  118. }
  119. iscsi_pdu_set_pduflags(pdu, flags);
  120. /* lun */
  121. iscsi_pdu_set_lun(pdu, lun);
  122. /* expxferlen */
  123. iscsi_pdu_set_expxferlen(pdu, task->expxferlen);
  124. /* cmdsn */
  125. iscsi_pdu_set_cmdsn(pdu, iscsi->cmdsn);
  126. pdu->cmdsn = iscsi->cmdsn;
  127. iscsi->cmdsn++;
  128. /* exp statsn */
  129. iscsi_pdu_set_expstatsn(pdu, iscsi->statsn+1);
  130. /* cdb */
  131. iscsi_pdu_set_cdb(pdu, task);
  132. pdu->callback = iscsi_scsi_response_cb;
  133. pdu->private_data = scsi_cbdata;
  134. if (iscsi_queue_pdu(iscsi, pdu) != 0) {
  135. printf("failed to queue iscsi scsi pdu\n");
  136. iscsi_free_pdu(iscsi, pdu);
  137. return -6;
  138. }
  139. return 0;
  140. }
  141. int iscsi_process_scsi_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size)
  142. {
  143. int statsn, flags, response, status;
  144. struct iscsi_scsi_cbdata *scsi_cbdata = pdu->scsi_cbdata;
  145. struct scsi_task *task = scsi_cbdata->task;
  146. statsn = ntohl(*(uint32_t *)&hdr[24]);
  147. if (statsn > (int)iscsi->statsn) {
  148. iscsi->statsn = statsn;
  149. }
  150. flags = hdr[1];
  151. if ((flags&ISCSI_PDU_DATA_FINAL) == 0) {
  152. printf("scsi response pdu but Final bit is not set: 0x%02x\n", flags);
  153. pdu->callback(iscsi, ISCSI_STATUS_ERROR, task, pdu->private_data);
  154. return -1;
  155. }
  156. if ((flags&ISCSI_PDU_DATA_ACK_REQUESTED) != 0) {
  157. printf("scsi response asked for ACK 0x%02x\n", flags);
  158. pdu->callback(iscsi, ISCSI_STATUS_ERROR, task, pdu->private_data);
  159. return -1;
  160. }
  161. /* for now, we ignore all overflow/underflow flags. We just print/log them so we can tweak
  162. * libiscsi to not generate under/over flows
  163. */
  164. if ((flags&ISCSI_PDU_DATA_BIDIR_OVERFLOW) != 0) {
  165. printf("scsi response contains bidir overflow 0x%02x\n", flags);
  166. }
  167. if ((flags&ISCSI_PDU_DATA_BIDIR_UNDERFLOW) != 0) {
  168. printf("scsi response contains bidir underflow 0x%02x\n", flags);
  169. }
  170. if ((flags&ISCSI_PDU_DATA_RESIDUAL_OVERFLOW) != 0) {
  171. printf("scsi response contains residual overflow 0x%02x\n", flags);
  172. }
  173. if ((flags&ISCSI_PDU_DATA_RESIDUAL_UNDERFLOW) != 0) {
  174. printf("scsi response contains residual underflow 0x%02x\n", flags);
  175. }
  176. response = hdr[2];
  177. if (response != 0x00) {
  178. printf("scsi reply response field:%d\n", response);
  179. }
  180. status = hdr[3];
  181. switch (status) {
  182. case ISCSI_STATUS_GOOD:
  183. task->datain.data = pdu->indata.data;
  184. task->datain.size = pdu->indata.size;
  185. pdu->callback(iscsi, ISCSI_STATUS_GOOD, &pdu->indata, pdu->private_data);
  186. break;
  187. case ISCSI_STATUS_CHECK_CONDITION:
  188. task->datain.data = discard_const(hdr + ISCSI_HEADER_SIZE);
  189. task->datain.size = size - ISCSI_HEADER_SIZE;
  190. task->sense.error_type = task->datain.data[2] & 0x7f;
  191. task->sense.key = task->datain.data[4] & 0x0f;
  192. task->sense.ascq = ntohs(*(uint16_t *)&(task->datain.data[14]));
  193. pdu->callback(iscsi, ISCSI_STATUS_CHECK_CONDITION, task, pdu->private_data);
  194. break;
  195. default:
  196. printf("Unknown status :%d\n", status);
  197. pdu->callback(iscsi, ISCSI_STATUS_ERROR, task, pdu->private_data);
  198. return -1;
  199. }
  200. return 0;
  201. }
  202. int iscsi_process_scsi_data_in(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size, int *is_finished)
  203. {
  204. int statsn, flags, status;
  205. struct iscsi_scsi_cbdata *scsi_cbdata = pdu->scsi_cbdata;
  206. struct scsi_task *task = scsi_cbdata->task;
  207. int dsl;
  208. statsn = ntohl(*(uint32_t *)&hdr[24]);
  209. if (statsn > (int)iscsi->statsn) {
  210. iscsi->statsn = statsn;
  211. }
  212. flags = hdr[1];
  213. if ((flags&ISCSI_PDU_DATA_ACK_REQUESTED) != 0) {
  214. printf("scsi response asked for ACK 0x%02x\n", flags);
  215. pdu->callback(iscsi, ISCSI_STATUS_ERROR, task, pdu->private_data);
  216. return -1;
  217. }
  218. /* for now, we ignore all overflow/underflow flags. We just print/log them so we can tweak
  219. * libiscsi to not generate under/over flows
  220. */
  221. if ((flags&ISCSI_PDU_DATA_RESIDUAL_OVERFLOW) != 0) {
  222. printf("scsi response contains residual overflow 0x%02x\n", flags);
  223. }
  224. if ((flags&ISCSI_PDU_DATA_RESIDUAL_UNDERFLOW) != 0) {
  225. printf("scsi response contains residual underflow 0x%02x\n", flags);
  226. }
  227. dsl = ntohl(*(uint32_t *)&hdr[4])&0x00ffffff;
  228. if (dsl > size - ISCSI_HEADER_SIZE) {
  229. printf ("dsl is :%d, while buffser size if %d\n", dsl, size - ISCSI_HEADER_SIZE);
  230. }
  231. if (iscsi_add_data(&pdu->indata, discard_const(hdr + ISCSI_HEADER_SIZE), dsl, 0) != 0) {
  232. printf("failed to add data to pdu in buffer\n");
  233. return -3;
  234. }
  235. if ((flags&ISCSI_PDU_DATA_FINAL) == 0) {
  236. printf("scsi data-in without Final bit: 0x%02x\n", flags);
  237. *is_finished = 0;
  238. }
  239. if ((flags&ISCSI_PDU_DATA_CONTAINS_STATUS) == 0) {
  240. printf("scsi data-in without Status bit: 0x%02x\n", flags);
  241. *is_finished = 0;
  242. }
  243. if (*is_finished == 0) {
  244. return 0;
  245. }
  246. /* this was the final data-in packet in the sequence and it has the s-bit set, so invoke the
  247. * callback.
  248. */
  249. status = hdr[3];
  250. task->datain.data = pdu->indata.data;
  251. task->datain.size = pdu->indata.size;
  252. pdu->callback(iscsi, status, task, pdu->private_data);
  253. return 0;
  254. }
  255. /*
  256. * SCSI commands
  257. */
  258. int iscsi_testunitready_async(struct iscsi_context *iscsi, int lun, iscsi_command_cb cb, void *private_data)
  259. {
  260. struct scsi_task *task;
  261. int ret;
  262. if ((task = scsi_cdb_testunitready()) == NULL) {
  263. printf("Failed to create testunitready cdb\n");
  264. return -1;
  265. }
  266. ret = iscsi_scsi_command_async(iscsi, lun, task, cb, NULL, private_data);
  267. return ret;
  268. }
  269. int iscsi_reportluns_async(struct iscsi_context *iscsi, iscsi_command_cb cb, int report_type, int alloc_len, void *private_data)
  270. {
  271. struct scsi_task *task;
  272. int ret;
  273. if (alloc_len < 16) {
  274. printf("Minimum allowed alloc len for reportluns is 16. You specified %d\n", alloc_len);
  275. return -1;
  276. }
  277. if ((task = scsi_reportluns_cdb(report_type, alloc_len)) == NULL) {
  278. printf("Failed to create reportluns cdb\n");
  279. return -2;
  280. }
  281. /* report luns are always sent to lun 0 */
  282. ret = iscsi_scsi_command_async(iscsi, 0, task, cb, NULL, private_data);
  283. return ret;
  284. }
  285. int iscsi_inquiry_async(struct iscsi_context *iscsi, int lun, iscsi_command_cb cb, int evpd, int page_code, int maxsize, void *private_data)
  286. {
  287. struct scsi_task *task;
  288. int ret;
  289. if ((task = scsi_cdb_inquiry(evpd, page_code, maxsize)) == NULL) {
  290. printf("Failed to create inquiry cdb\n");
  291. return -1;
  292. }
  293. ret = iscsi_scsi_command_async(iscsi, lun, task, cb, NULL, private_data);
  294. return ret;
  295. }
  296. int iscsi_readcapacity10_async(struct iscsi_context *iscsi, int lun, iscsi_command_cb cb, int lba, int pmi, void *private_data)
  297. {
  298. struct scsi_task *task;
  299. int ret;
  300. if ((task = scsi_cdb_readcapacity10(lba, pmi)) == NULL) {
  301. printf("Failed to create readcapacity10 cdb\n");
  302. return -1;
  303. }
  304. ret = iscsi_scsi_command_async(iscsi, lun, task, cb, NULL, private_data);
  305. return ret;
  306. }
  307. int iscsi_read10_async(struct iscsi_context *iscsi, int lun, iscsi_command_cb cb, int lba, int datalen, int blocksize, void *private_data)
  308. {
  309. struct scsi_task *task;
  310. int ret;
  311. if (datalen % blocksize != 0) {
  312. printf("datalen:%d is not a multiple of the blocksize:%d\n", datalen, blocksize);
  313. return -1;
  314. }
  315. if ((task = scsi_cdb_read10(lba, datalen, blocksize)) == NULL) {
  316. printf("Failed to create read10 cdb\n");
  317. return -2;
  318. }
  319. ret = iscsi_scsi_command_async(iscsi, lun, task, cb, NULL, private_data);
  320. return ret;
  321. }
  322. int iscsi_write10_async(struct iscsi_context *iscsi, int lun, iscsi_command_cb cb, unsigned char *data, int datalen, int lba, int fua, int fuanv, int blocksize, void *private_data)
  323. {
  324. struct scsi_task *task;
  325. struct iscsi_data outdata;
  326. int ret;
  327. if (datalen % blocksize != 0) {
  328. printf("datalen:%d is not a multiple of the blocksize:%d\n", datalen, blocksize);
  329. return -1;
  330. }
  331. if ((task = scsi_cdb_write10(lba, datalen, fua, fuanv, blocksize)) == NULL) {
  332. printf("Failed to create read10 cdb\n");
  333. return -2;
  334. }
  335. outdata.data = data;
  336. outdata.size = datalen;
  337. ret = iscsi_scsi_command_async(iscsi, lun, task, cb, &outdata, private_data);
  338. return ret;
  339. }
  340. int iscsi_modesense6_async(struct iscsi_context *iscsi, int lun, iscsi_command_cb cb, int dbd, int pc, int page_code, int sub_page_code, unsigned char alloc_len, void *private_data)
  341. {
  342. struct scsi_task *task;
  343. int ret;
  344. if ((task = scsi_cdb_modesense6(dbd, pc, page_code, sub_page_code, alloc_len)) == NULL) {
  345. printf("Failed to create modesense6 cdb\n");
  346. return -2;
  347. }
  348. ret = iscsi_scsi_command_async(iscsi, lun, task, cb, NULL, private_data);
  349. return ret;
  350. }