2016-07-27 16:32:00 +00:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
|
|
|
|
* Copyright (c) Intel Corporation.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* * Neither the name of Intel Corporation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "scsi_internal.h"
|
2016-08-02 17:43:00 +00:00
|
|
|
#include "spdk/endian.h"
|
2016-08-17 20:35:18 +00:00
|
|
|
#include "spdk/env.h"
|
2017-05-01 20:42:29 +00:00
|
|
|
#include "spdk/util.h"
|
2016-07-27 16:32:00 +00:00
|
|
|
|
2018-06-06 01:04:43 +00:00
|
|
|
static void
|
2019-10-28 06:32:02 +00:00
|
|
|
scsi_task_free_data(struct spdk_scsi_task *task)
|
|
|
|
{
|
|
|
|
if (task->alloc_len != 0) {
|
|
|
|
spdk_dma_free(task->iov.iov_base);
|
|
|
|
task->alloc_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
task->iov.iov_base = NULL;
|
|
|
|
task->iov.iov_len = 0;
|
|
|
|
}
|
2018-06-06 01:04:43 +00:00
|
|
|
|
2016-07-27 16:32:00 +00:00
|
|
|
void
|
2016-09-28 06:49:06 +00:00
|
|
|
spdk_scsi_task_put(struct spdk_scsi_task *task)
|
2016-07-27 16:32:00 +00:00
|
|
|
{
|
|
|
|
if (!task) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-07 22:17:18 +00:00
|
|
|
assert(task->ref > 0);
|
2016-07-27 16:32:00 +00:00
|
|
|
task->ref--;
|
|
|
|
|
|
|
|
if (task->ref == 0) {
|
2017-07-13 04:08:53 +00:00
|
|
|
struct spdk_bdev_io *bdev_io = task->bdev_io;
|
2016-07-27 16:32:00 +00:00
|
|
|
|
|
|
|
if (bdev_io) {
|
|
|
|
spdk_bdev_free_io(bdev_io);
|
|
|
|
}
|
|
|
|
|
2019-03-28 07:43:37 +00:00
|
|
|
scsi_task_free_data(task);
|
2016-07-27 16:32:00 +00:00
|
|
|
|
|
|
|
task->free_fn(task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-05-13 00:16:44 +00:00
|
|
|
spdk_scsi_task_construct(struct spdk_scsi_task *task,
|
2017-06-08 20:45:41 +00:00
|
|
|
spdk_scsi_task_cpl cpl_fn,
|
2018-01-04 21:03:34 +00:00
|
|
|
spdk_scsi_task_free free_fn)
|
2016-07-27 16:32:00 +00:00
|
|
|
{
|
2017-06-08 20:45:41 +00:00
|
|
|
assert(task != NULL);
|
|
|
|
assert(cpl_fn != NULL);
|
2017-05-12 23:55:16 +00:00
|
|
|
assert(free_fn != NULL);
|
2017-06-08 20:45:41 +00:00
|
|
|
|
|
|
|
task->cpl_fn = cpl_fn;
|
2017-05-12 23:55:16 +00:00
|
|
|
task->free_fn = free_fn;
|
|
|
|
|
2016-07-27 16:32:00 +00:00
|
|
|
task->ref++;
|
|
|
|
|
2016-10-04 15:10:35 +00:00
|
|
|
/*
|
|
|
|
* Pre-fill the iov_buffers to point to the embedded iov
|
|
|
|
*/
|
2016-11-04 18:45:54 +00:00
|
|
|
assert(task->iov.iov_base == NULL);
|
2016-10-04 15:10:35 +00:00
|
|
|
task->iovs = &task->iov;
|
|
|
|
task->iovcnt = 1;
|
2016-07-27 16:32:00 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 01:04:43 +00:00
|
|
|
static void *
|
2019-03-28 07:43:37 +00:00
|
|
|
scsi_task_alloc_data(struct spdk_scsi_task *task, uint32_t alloc_len)
|
2016-07-27 16:32:00 +00:00
|
|
|
{
|
2016-11-04 18:45:54 +00:00
|
|
|
assert(task->alloc_len == 0);
|
|
|
|
|
2017-05-25 18:21:30 +00:00
|
|
|
task->iov.iov_base = spdk_dma_zmalloc(alloc_len, 0, NULL);
|
2016-11-14 19:14:12 +00:00
|
|
|
task->iov.iov_len = alloc_len;
|
|
|
|
task->alloc_len = alloc_len;
|
|
|
|
|
|
|
|
return task->iov.iov_base;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_scsi_task_scatter_data(struct spdk_scsi_task *task, const void *src, size_t buf_len)
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
size_t buf_left = buf_len;
|
|
|
|
int i;
|
|
|
|
struct iovec *iovs = task->iovs;
|
|
|
|
const uint8_t *pos;
|
|
|
|
|
2017-12-07 23:23:48 +00:00
|
|
|
if (buf_len == 0) {
|
2016-11-14 19:14:12 +00:00
|
|
|
return 0;
|
2017-12-07 23:23:48 +00:00
|
|
|
}
|
2016-11-14 19:14:12 +00:00
|
|
|
|
|
|
|
if (task->iovcnt == 1 && iovs[0].iov_base == NULL) {
|
2019-03-28 07:43:37 +00:00
|
|
|
scsi_task_alloc_data(task, buf_len);
|
2016-11-14 19:14:12 +00:00
|
|
|
iovs[0] = task->iov;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < task->iovcnt; i++) {
|
|
|
|
assert(iovs[i].iov_base != NULL);
|
|
|
|
len += iovs[i].iov_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len < buf_len) {
|
|
|
|
spdk_scsi_task_set_status(task, SPDK_SCSI_STATUS_CHECK_CONDITION,
|
|
|
|
SPDK_SCSI_SENSE_ILLEGAL_REQUEST,
|
|
|
|
SPDK_SCSI_ASC_INVALID_FIELD_IN_CDB,
|
|
|
|
SPDK_SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = src;
|
|
|
|
|
|
|
|
for (i = 0; i < task->iovcnt; i++) {
|
2017-05-01 20:42:29 +00:00
|
|
|
len = spdk_min(iovs[i].iov_len, buf_left);
|
2016-11-14 19:14:12 +00:00
|
|
|
buf_left -= len;
|
|
|
|
memcpy(iovs[i].iov_base, pos, len);
|
|
|
|
pos += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
spdk_scsi_task_gather_data(struct spdk_scsi_task *task, int *len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct iovec *iovs = task->iovs;
|
|
|
|
size_t buf_len = 0;
|
|
|
|
uint8_t *buf, *pos;
|
|
|
|
|
|
|
|
for (i = 0; i < task->iovcnt; i++) {
|
2021-03-04 23:53:24 +00:00
|
|
|
/* It is OK for iov_base to be NULL if iov_len is 0. */
|
|
|
|
assert(iovs[i].iov_base != NULL || iovs[i].iov_len == 0);
|
2016-11-14 19:14:12 +00:00
|
|
|
buf_len += iovs[i].iov_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf_len == 0) {
|
|
|
|
*len = 0;
|
2016-11-04 18:45:54 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-04-19 14:39:54 +00:00
|
|
|
buf = calloc(1, buf_len);
|
2016-11-14 19:14:12 +00:00
|
|
|
if (buf == NULL) {
|
|
|
|
*len = -1;
|
|
|
|
return NULL;
|
2016-07-27 16:32:00 +00:00
|
|
|
}
|
2016-11-04 18:45:54 +00:00
|
|
|
|
2016-11-14 19:14:12 +00:00
|
|
|
pos = buf;
|
|
|
|
for (i = 0; i < task->iovcnt; i++) {
|
|
|
|
memcpy(pos, iovs[i].iov_base, iovs[i].iov_len);
|
|
|
|
pos += iovs[i].iov_len;
|
|
|
|
}
|
2016-11-04 18:45:54 +00:00
|
|
|
|
2016-11-14 19:14:12 +00:00
|
|
|
*len = buf_len;
|
|
|
|
return buf;
|
2016-11-04 18:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
spdk_scsi_task_set_data(struct spdk_scsi_task *task, void *data, uint32_t len)
|
|
|
|
{
|
|
|
|
assert(task->iovcnt == 1);
|
|
|
|
assert(task->alloc_len == 0);
|
|
|
|
|
|
|
|
task->iovs[0].iov_base = data;
|
|
|
|
task->iovs[0].iov_len = len;
|
2016-07-27 16:32:00 +00:00
|
|
|
}
|
|
|
|
|
2016-10-09 07:03:43 +00:00
|
|
|
void
|
2016-07-27 16:32:00 +00:00
|
|
|
spdk_scsi_task_build_sense_data(struct spdk_scsi_task *task, int sk, int asc, int ascq)
|
|
|
|
{
|
|
|
|
uint8_t *cp;
|
|
|
|
int resp_code;
|
|
|
|
|
|
|
|
resp_code = 0x70; /* Current + Fixed format */
|
|
|
|
|
|
|
|
/* Sense Data */
|
2016-10-31 17:39:59 +00:00
|
|
|
cp = task->sense_data;
|
2016-07-27 16:32:00 +00:00
|
|
|
|
|
|
|
/* VALID(7) RESPONSE CODE(6-0) */
|
|
|
|
cp[0] = 0x80 | resp_code;
|
|
|
|
/* Obsolete */
|
|
|
|
cp[1] = 0;
|
|
|
|
/* FILEMARK(7) EOM(6) ILI(5) SENSE KEY(3-0) */
|
|
|
|
cp[2] = sk & 0xf;
|
|
|
|
/* INFORMATION */
|
|
|
|
memset(&cp[3], 0, 4);
|
2016-10-09 07:03:43 +00:00
|
|
|
|
2016-07-27 16:32:00 +00:00
|
|
|
/* ADDITIONAL SENSE LENGTH */
|
2016-10-09 07:03:43 +00:00
|
|
|
cp[7] = 10;
|
2016-07-27 16:32:00 +00:00
|
|
|
|
|
|
|
/* COMMAND-SPECIFIC INFORMATION */
|
|
|
|
memset(&cp[8], 0, 4);
|
|
|
|
/* ADDITIONAL SENSE CODE */
|
|
|
|
cp[12] = asc;
|
|
|
|
/* ADDITIONAL SENSE CODE QUALIFIER */
|
|
|
|
cp[13] = ascq;
|
|
|
|
/* FIELD REPLACEABLE UNIT CODE */
|
|
|
|
cp[14] = 0;
|
|
|
|
|
|
|
|
/* SKSV(7) SENSE KEY SPECIFIC(6-0,7-0,7-0) */
|
|
|
|
cp[15] = 0;
|
|
|
|
cp[16] = 0;
|
|
|
|
cp[17] = 0;
|
|
|
|
|
|
|
|
/* SenseLength */
|
2016-10-31 17:39:59 +00:00
|
|
|
task->sense_data_len = 18;
|
2016-07-27 16:32:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-10-28 19:56:36 +00:00
|
|
|
spdk_scsi_task_set_status(struct spdk_scsi_task *task, int sc, int sk,
|
|
|
|
int asc, int ascq)
|
2016-07-27 16:32:00 +00:00
|
|
|
{
|
2017-01-18 21:52:50 +00:00
|
|
|
if (sc == SPDK_SCSI_STATUS_CHECK_CONDITION) {
|
|
|
|
spdk_scsi_task_build_sense_data(task, sk, asc, ascq);
|
|
|
|
}
|
2016-10-28 19:56:36 +00:00
|
|
|
task->status = sc;
|
2016-07-27 16:32:00 +00:00
|
|
|
}
|
2018-06-11 04:20:23 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
spdk_scsi_task_copy_status(struct spdk_scsi_task *dst,
|
|
|
|
struct spdk_scsi_task *src)
|
|
|
|
{
|
|
|
|
memcpy(dst->sense_data, src->sense_data, src->sense_data_len);
|
|
|
|
dst->sense_data_len = src->sense_data_len;
|
|
|
|
dst->status = src->status;
|
|
|
|
}
|
2018-12-04 13:17:28 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
spdk_scsi_task_process_null_lun(struct spdk_scsi_task *task)
|
|
|
|
{
|
|
|
|
uint8_t buffer[36];
|
|
|
|
uint32_t allocation_len;
|
|
|
|
uint32_t data_len;
|
|
|
|
|
|
|
|
task->length = task->transfer_len;
|
|
|
|
if (task->cdb[0] == SPDK_SPC_INQUIRY) {
|
|
|
|
/*
|
|
|
|
* SPC-4 states that INQUIRY commands to an unsupported LUN
|
|
|
|
* must be served with PERIPHERAL QUALIFIER = 0x3 and
|
|
|
|
* PERIPHERAL DEVICE TYPE = 0x1F.
|
|
|
|
*/
|
|
|
|
data_len = sizeof(buffer);
|
|
|
|
|
|
|
|
memset(buffer, 0, data_len);
|
|
|
|
/* PERIPHERAL QUALIFIER(7-5) PERIPHERAL DEVICE TYPE(4-0) */
|
|
|
|
buffer[0] = 0x03 << 5 | 0x1f;
|
|
|
|
/* ADDITIONAL LENGTH */
|
|
|
|
buffer[4] = data_len - 5;
|
|
|
|
|
|
|
|
allocation_len = from_be16(&task->cdb[3]);
|
|
|
|
if (spdk_scsi_task_scatter_data(task, buffer, spdk_min(allocation_len, data_len)) >= 0) {
|
|
|
|
task->data_transferred = data_len;
|
|
|
|
task->status = SPDK_SCSI_STATUS_GOOD;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* LOGICAL UNIT NOT SUPPORTED */
|
|
|
|
spdk_scsi_task_set_status(task, SPDK_SCSI_STATUS_CHECK_CONDITION,
|
|
|
|
SPDK_SCSI_SENSE_ILLEGAL_REQUEST,
|
|
|
|
SPDK_SCSI_ASC_LOGICAL_UNIT_NOT_SUPPORTED,
|
|
|
|
SPDK_SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
|
|
|
|
task->data_transferred = 0;
|
|
|
|
}
|
|
|
|
}
|
2018-12-04 13:24:34 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
spdk_scsi_task_process_abort(struct spdk_scsi_task *task)
|
|
|
|
{
|
|
|
|
spdk_scsi_task_set_status(task, SPDK_SCSI_STATUS_CHECK_CONDITION,
|
|
|
|
SPDK_SCSI_SENSE_ABORTED_COMMAND,
|
|
|
|
SPDK_SCSI_ASC_NO_ADDITIONAL_SENSE,
|
|
|
|
SPDK_SCSI_ASCQ_CAUSE_NOT_REPORTABLE);
|
|
|
|
}
|