2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include <arpa/inet.h>
23 #include "iscsi-private.h"
24 #include "scsi-lowlevel.h"
25 #include "dlinklist.h"
27 struct iscsi_scsi_cbdata {
28 struct iscsi_scsi_cbdata *prev, *next;
29 iscsi_command_cb callback;
31 struct scsi_task *task;
34 void iscsi_free_scsi_cbdata(struct iscsi_scsi_cbdata *scsi_cbdata)
36 if (scsi_cbdata == NULL) {
39 if (scsi_cbdata->task == NULL) {
40 scsi_free_scsi_task(scsi_cbdata->task);
41 scsi_cbdata->task = NULL;
46 static void iscsi_scsi_response_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
48 struct iscsi_scsi_cbdata *scsi_cbdata = (struct iscsi_scsi_cbdata *)private_data;
49 struct scsi_task *task = command_data;
52 case ISCSI_STATUS_CHECK_CONDITION:
53 scsi_cbdata->callback(iscsi, ISCSI_STATUS_CHECK_CONDITION, task, scsi_cbdata->private_data);
57 case ISCSI_STATUS_GOOD:
58 scsi_cbdata->callback(iscsi, ISCSI_STATUS_GOOD, task, scsi_cbdata->private_data);
61 printf("Cant handle scsi status %d yet\n", status);
62 scsi_cbdata->callback(iscsi, ISCSI_STATUS_ERROR, task, scsi_cbdata->private_data);
67 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)
69 struct iscsi_pdu *pdu;
70 struct iscsi_scsi_cbdata *scsi_cbdata;
74 printf("trying to send command on NULL context\n");
75 scsi_free_scsi_task(task);
79 if (iscsi->session_type != ISCSI_SESSION_NORMAL) {
80 printf("Trying to send command on discovery session\n");
81 scsi_free_scsi_task(task);
85 if (iscsi->is_loggedin == 0) {
86 printf("Trying to send command while not logged in\n");
87 scsi_free_scsi_task(task);
91 scsi_cbdata = malloc(sizeof(struct iscsi_scsi_cbdata));
92 if (scsi_cbdata == NULL) {
93 printf("failed to allocate scsi cbdata\n");
94 scsi_free_scsi_task(task);
97 bzero(scsi_cbdata, sizeof(struct iscsi_scsi_cbdata));
98 scsi_cbdata->task = task;
99 scsi_cbdata->callback = cb;
100 scsi_cbdata->private_data = private_data;
102 pdu = iscsi_allocate_pdu(iscsi, ISCSI_PDU_SCSI_REQUEST, ISCSI_PDU_SCSI_RESPONSE);
104 printf("Failed to allocate text pdu\n");
105 iscsi_free_scsi_cbdata(scsi_cbdata);
108 pdu->scsi_cbdata = scsi_cbdata;
111 flags = ISCSI_PDU_SCSI_FINAL|ISCSI_PDU_SCSI_ATTR_SIMPLE;
112 switch (task->xfer_dir) {
116 flags |= ISCSI_PDU_SCSI_READ;
118 case SCSI_XFER_WRITE:
119 flags |= ISCSI_PDU_SCSI_WRITE;
121 printf("DATA-OUT command but data == NULL\n");
122 iscsi_free_pdu(iscsi, pdu);
125 if (data->size != task->expxferlen) {
126 printf("data size:%d is not same as expected data transfer length:%d\n", data->size, task->expxferlen);
127 iscsi_free_pdu(iscsi, pdu);
130 if (iscsi_pdu_add_data(iscsi, pdu, data->data, data->size) != 0) {
131 printf("Failed to add outdata to the pdu\n");
132 iscsi_free_pdu(iscsi, pdu);
138 iscsi_pdu_set_pduflags(pdu, flags);
141 iscsi_pdu_set_lun(pdu, lun);
144 iscsi_pdu_set_expxferlen(pdu, task->expxferlen);
147 iscsi_pdu_set_cmdsn(pdu, iscsi->cmdsn);
148 pdu->cmdsn = iscsi->cmdsn;
152 iscsi_pdu_set_expstatsn(pdu, iscsi->statsn+1);
155 iscsi_pdu_set_cdb(pdu, task);
157 pdu->callback = iscsi_scsi_response_cb;
158 pdu->private_data = scsi_cbdata;
160 if (iscsi_queue_pdu(iscsi, pdu) != 0) {
161 printf("failed to queue iscsi scsi pdu\n");
162 iscsi_free_pdu(iscsi, pdu);
170 int iscsi_process_scsi_reply(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size)
172 int statsn, flags, response, status;
173 struct iscsi_scsi_cbdata *scsi_cbdata = pdu->scsi_cbdata;
174 struct scsi_task *task = scsi_cbdata->task;
176 statsn = ntohl(*(uint32_t *)&hdr[24]);
177 if (statsn > (int)iscsi->statsn) {
178 iscsi->statsn = statsn;
182 if ((flags&ISCSI_PDU_DATA_FINAL) == 0) {
183 printf("scsi response pdu but Final bit is not set: 0x%02x\n", flags);
184 pdu->callback(iscsi, ISCSI_STATUS_ERROR, task, pdu->private_data);
187 if ((flags&ISCSI_PDU_DATA_ACK_REQUESTED) != 0) {
188 printf("scsi response asked for ACK 0x%02x\n", flags);
189 pdu->callback(iscsi, ISCSI_STATUS_ERROR, task, pdu->private_data);
192 /* for now, we ignore all overflow/underflow flags. We just print/log them so we can tweak
193 * libiscsi to not generate under/over flows
195 if ((flags&ISCSI_PDU_DATA_BIDIR_OVERFLOW) != 0) {
196 printf("scsi response contains bidir overflow 0x%02x\n", flags);
198 if ((flags&ISCSI_PDU_DATA_BIDIR_UNDERFLOW) != 0) {
199 printf("scsi response contains bidir underflow 0x%02x\n", flags);
201 if ((flags&ISCSI_PDU_DATA_RESIDUAL_OVERFLOW) != 0) {
202 printf("scsi response contains residual overflow 0x%02x\n", flags);
204 if ((flags&ISCSI_PDU_DATA_RESIDUAL_UNDERFLOW) != 0) {
205 printf("scsi response contains residual underflow 0x%02x\n", flags);
210 if (response != 0x00) {
211 printf("scsi reply response field:%d\n", response);
217 case ISCSI_STATUS_GOOD:
218 task->datain.data = pdu->indata.data;
219 task->datain.size = pdu->indata.size;
221 pdu->callback(iscsi, ISCSI_STATUS_GOOD, &pdu->indata, pdu->private_data);
223 case ISCSI_STATUS_CHECK_CONDITION:
224 task->datain.data = discard_const(hdr + ISCSI_HEADER_SIZE);
225 task->datain.size = size - ISCSI_HEADER_SIZE;
227 task->sense.error_type = task->datain.data[2] & 0x7f;
228 task->sense.key = task->datain.data[4] & 0x0f;
229 task->sense.ascq = ntohs(*(uint16_t *)&(task->datain.data[14]));
231 pdu->callback(iscsi, ISCSI_STATUS_CHECK_CONDITION, task, pdu->private_data);
234 printf("Unknown status :%d\n", status);
236 pdu->callback(iscsi, ISCSI_STATUS_ERROR, task, pdu->private_data);
243 int iscsi_process_scsi_data_in(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, const unsigned char *hdr, int size, int *is_finished)
245 int statsn, flags, status;
246 struct iscsi_scsi_cbdata *scsi_cbdata = pdu->scsi_cbdata;
247 struct scsi_task *task = scsi_cbdata->task;
250 statsn = ntohl(*(uint32_t *)&hdr[24]);
251 if (statsn > (int)iscsi->statsn) {
252 iscsi->statsn = statsn;
256 if ((flags&ISCSI_PDU_DATA_ACK_REQUESTED) != 0) {
257 printf("scsi response asked for ACK 0x%02x\n", flags);
258 pdu->callback(iscsi, ISCSI_STATUS_ERROR, task, pdu->private_data);
261 /* for now, we ignore all overflow/underflow flags. We just print/log them so we can tweak
262 * libiscsi to not generate under/over flows
264 if ((flags&ISCSI_PDU_DATA_RESIDUAL_OVERFLOW) != 0) {
265 printf("scsi response contains residual overflow 0x%02x\n", flags);
267 if ((flags&ISCSI_PDU_DATA_RESIDUAL_UNDERFLOW) != 0) {
268 printf("scsi response contains residual underflow 0x%02x\n", flags);
271 dsl = ntohl(*(uint32_t *)&hdr[4])&0x00ffffff;
273 if (dsl > size - ISCSI_HEADER_SIZE) {
274 printf ("dsl is :%d, while buffser size if %d\n", dsl, size - ISCSI_HEADER_SIZE);
277 if (iscsi_add_data(&pdu->indata, discard_const(hdr + ISCSI_HEADER_SIZE), dsl, 0) != 0) {
278 printf("failed to add data to pdu in buffer\n");
283 if ((flags&ISCSI_PDU_DATA_FINAL) == 0) {
284 printf("scsi data-in without Final bit: 0x%02x\n", flags);
287 if ((flags&ISCSI_PDU_DATA_CONTAINS_STATUS) == 0) {
288 printf("scsi data-in without Status bit: 0x%02x\n", flags);
292 if (*is_finished == 0) {
297 /* this was the final data-in packet in the sequence and it has the s-bit set, so invoke the
301 task->datain.data = pdu->indata.data;
302 task->datain.size = pdu->indata.size;
304 pdu->callback(iscsi, status, task, pdu->private_data);
316 int iscsi_testunitready_async(struct iscsi_context *iscsi, int lun, iscsi_command_cb cb, void *private_data)
318 struct scsi_task *task;
321 if ((task = scsi_cdb_testunitready()) == NULL) {
322 printf("Failed to create testunitready cdb\n");
325 ret = iscsi_scsi_command_async(iscsi, lun, task, cb, NULL, private_data);
331 int iscsi_reportluns_async(struct iscsi_context *iscsi, iscsi_command_cb cb, int report_type, int alloc_len, void *private_data)
333 struct scsi_task *task;
336 if (alloc_len < 16) {
337 printf("Minimum allowed alloc len for reportluns is 16. You specified %d\n", alloc_len);
341 if ((task = scsi_reportluns_cdb(report_type, alloc_len)) == NULL) {
342 printf("Failed to create reportluns cdb\n");
345 /* report luns are always sent to lun 0 */
346 ret = iscsi_scsi_command_async(iscsi, 0, task, cb, NULL, private_data);
351 int iscsi_inquiry_async(struct iscsi_context *iscsi, int lun, iscsi_command_cb cb, int evpd, int page_code, int maxsize, void *private_data)
353 struct scsi_task *task;
356 if ((task = scsi_cdb_inquiry(evpd, page_code, maxsize)) == NULL) {
357 printf("Failed to create inquiry cdb\n");
360 ret = iscsi_scsi_command_async(iscsi, lun, task, cb, NULL, private_data);
365 int iscsi_readcapacity10_async(struct iscsi_context *iscsi, int lun, iscsi_command_cb cb, int lba, int pmi, void *private_data)
367 struct scsi_task *task;
370 if ((task = scsi_cdb_readcapacity10(lba, pmi)) == NULL) {
371 printf("Failed to create readcapacity10 cdb\n");
374 ret = iscsi_scsi_command_async(iscsi, lun, task, cb, NULL, private_data);
379 int iscsi_read10_async(struct iscsi_context *iscsi, int lun, iscsi_command_cb cb, int lba, int datalen, int blocksize, void *private_data)
381 struct scsi_task *task;
384 if (datalen % blocksize != 0) {
385 printf("datalen:%d is not a multiple of the blocksize:%d\n", datalen, blocksize);
389 if ((task = scsi_cdb_read10(lba, datalen, blocksize)) == NULL) {
390 printf("Failed to create read10 cdb\n");
393 ret = iscsi_scsi_command_async(iscsi, lun, task, cb, NULL, private_data);
399 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)
401 struct scsi_task *task;
402 struct iscsi_data outdata;
405 if (datalen % blocksize != 0) {
406 printf("datalen:%d is not a multiple of the blocksize:%d\n", datalen, blocksize);
410 if ((task = scsi_cdb_write10(lba, datalen, fua, fuanv, blocksize)) == NULL) {
411 printf("Failed to create read10 cdb\n");
416 outdata.size = datalen;
418 ret = iscsi_scsi_command_async(iscsi, lun, task, cb, &outdata, private_data);
423 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)
425 struct scsi_task *task;
428 if ((task = scsi_cdb_modesense6(dbd, pc, page_code, sub_page_code, alloc_len)) == NULL) {
429 printf("Failed to create modesense6 cdb\n");
432 ret = iscsi_scsi_command_async(iscsi, lun, task, cb, NULL, private_data);