Commit bd4ac74e authored by Daniel Verkamp's avatar Daniel Verkamp
Browse files

scsi: import SCSI/blockdev translation layer



Change-Id: Ie96943f40ea8be4156d55bc5eeacc567743cf9d6
Signed-off-by: default avatarDaniel Verkamp <daniel.verkamp@intel.com>
parent 87796053
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -63,6 +63,7 @@ time test/lib/ioat/ioat.sh
time test/lib/json/json.sh
time test/lib/jsonrpc/jsonrpc.sh
time test/lib/log/log.sh
time test/lib/scsi/scsi.sh

timing_exit lib

include/spdk/scsi.h

0 → 100644
+209 −0
Original line number Diff line number Diff line
/*-
 *   BSD LICENSE
 *
 *   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.
 */

/**
 * \file
 * SCSI to blockdev translation layer
 */

#ifndef SPDK_SCSI_H
#define SPDK_SCSI_H

#include <stdbool.h>
#include <stdint.h>
#include <sys/uio.h>

#include "spdk/queue.h"
#include "spdk/event.h"

#define SPDK_SCSI_MAX_DEVS			1024
#define SPDK_SCSI_DEV_MAX_LUN			64
#define SPDK_SCSI_DEV_MAX_PORTS			4
#define SPDK_SCSI_DEV_MAX_NAME			255

#define SPDK_SCSI_PORT_MAX_NAME_LENGTH		255

#define SPDK_SCSI_LUN_MAX_NAME_LENGTH		16

enum spdk_scsi_data_dir {
	SPDK_SCSI_DIR_NONE = 0,
	SPDK_SCSI_DIR_TO_DEV = 1,
	SPDK_SCSI_DIR_FROM_DEV = 2,
};

enum spdk_scsi_task_func {
	SPDK_SCSI_TASK_FUNC_ABORT_TASK = 0,
	SPDK_SCSI_TASK_FUNC_ABORT_TASK_SET,
	SPDK_SCSI_TASK_FUNC_CLEAR_TASK_SET,
	SPDK_SCSI_TASK_FUNC_LUN_RESET,
};

enum spdk_scsi_task_type {
	SPDK_SCSI_TASK_TYPE_CMD = 0,
	SPDK_SCSI_TASK_TYPE_MANAGE,
};

struct spdk_scsi_task {
	uint8_t				type;
	uint8_t				status;
	uint8_t				function; /* task mgmt function */
	uint8_t				response; /* task mgmt response */
	struct spdk_scsi_lun		*lun;
	struct spdk_scsi_port		*target_port;
	struct spdk_scsi_port		*initiator_port;
	spdk_event_t			cb_event;

	uint32_t ref;
	uint32_t id;
	uint32_t transfer_len;
	uint32_t data_out_cnt;
	uint32_t dxfer_dir;
	uint32_t desired_data_transfer_length;
	/* Only valid for Read/Write */
	uint32_t bytes_completed;
	uint32_t length;

	/**
	 * Amount of data actually transferred.  Can be less than requested
	 *  transfer_len - i.e. SCSI INQUIRY.
	 */
	uint32_t data_transferred;
	uint32_t alloc_len;

	uint64_t offset;
	struct iovec iov;
	struct spdk_scsi_task *parent;

	void (*free_fn)(struct spdk_scsi_task *);

	uint8_t *cdb;
	uint8_t *iobuf;

	uint8_t sense_data[32];
	size_t sense_data_len;

	uint8_t *rbuf; /* read buffer */
	void *blockdev_io;

	TAILQ_ENTRY(spdk_scsi_task) scsi_link;

	/*
	 * Pointer to scsi task owner's outstanding
	 * task counter. Inc/Dec by get/put task functions.
	 * Note: in the future, we could consider replacing this
	 * with an owner-provided task management fuction that
	 * could perform protocol specific task mangement
	 * operations (such as tracking outstanding tasks).
	 */
	uint32_t *owner_task_ctr;

	uint32_t abort_id;
	TAILQ_HEAD(subtask_list, spdk_scsi_task) subtask_list;
};

struct spdk_scsi_port {
	struct spdk_scsi_dev	*dev;
	uint64_t		id;
	uint16_t		index;
	char			name[SPDK_SCSI_PORT_MAX_NAME_LENGTH];
};

struct spdk_scsi_dev {
	int			id;
	int			is_allocated;

	char			name[SPDK_SCSI_DEV_MAX_NAME];

	int			maxlun;
	struct spdk_scsi_lun	*lun[SPDK_SCSI_DEV_MAX_LUN];

	int			num_ports;
	struct spdk_scsi_port	port[SPDK_SCSI_DEV_MAX_PORTS];
};

void spdk_scsi_dev_destruct(struct spdk_scsi_dev *dev);
void spdk_scsi_dev_queue_mgmt_task(struct spdk_scsi_dev *dev, struct spdk_scsi_task *task);
void spdk_scsi_dev_queue_task(struct spdk_scsi_dev *dev, struct spdk_scsi_task *task);
int spdk_scsi_dev_add_port(struct spdk_scsi_dev *dev, uint64_t id, const char *name);
struct spdk_scsi_port *spdk_scsi_dev_find_port_by_id(struct spdk_scsi_dev *dev, uint64_t id);
void spdk_scsi_dev_print(struct spdk_scsi_dev *dev);

/**

\brief Constructs a SCSI device object using the given parameters.

\param name Name for the SCSI device.
\param queue_depth Queue depth for the SCSI device.  This queue depth is
		   a combined queue depth for all LUNs in the device.
\param lun_list List of LUN objects for the SCSI device.  Caller is
		responsible for managing the memory containing this list.
\param lun_id_list List of LUN IDs for the LUN in this SCSI device.  Caller is
		   responsible for managing the memory containing this list.
		   lun_id_list[x] is the LUN ID for lun_list[x].
\param num_luns Number of entries in lun_list and lun_id_list.
\return The constructed spdk_scsi_dev object.

*/
struct spdk_scsi_dev *spdk_scsi_dev_construct(const char *name,
		char *lun_name_list[],
		int *lun_id_list,
		int num_luns);

void spdk_scsi_dev_delete_lun(struct spdk_scsi_dev *dev, struct spdk_scsi_lun *lun);


int spdk_scsi_port_construct(struct spdk_scsi_port *port, uint64_t id,
			     uint16_t index, const char *name);


void spdk_scsi_task_construct(struct spdk_scsi_task *task, uint32_t *owner_task_ctr,
			      struct spdk_scsi_task *parent);
void spdk_put_task(struct spdk_scsi_task *task);
void spdk_scsi_task_alloc_data(struct spdk_scsi_task *task, uint32_t alloc_len,
			       uint8_t **data);
int spdk_scsi_task_build_sense_data(struct spdk_scsi_task *task, int sk, int asc,
				    int ascq);
void spdk_scsi_task_set_check_condition(struct spdk_scsi_task *task, int sk,
					int asc, int ascq);

static inline struct spdk_scsi_task *
spdk_scsi_task_get_primary(struct spdk_scsi_task *task)
{
	if (task->parent) {
		return task->parent;
	} else {
		return task;
	}
}

#endif /* SPDK_SCSI_H */
+1 −1
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@
SPDK_ROOT_DIR := $(abspath $(CURDIR)/..)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk

DIRS-y += bdev conf copy cunit event json jsonrpc log memory rpc trace util nvme nvmf ioat
DIRS-y += bdev conf copy cunit event json jsonrpc log memory rpc trace util nvme nvmf scsi ioat

.PHONY: all clean $(DIRS-y)

lib/scsi/Makefile

0 → 100644
+41 −0
Original line number Diff line number Diff line
#
#  BSD LICENSE
#
#  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.
#

SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk

CFLAGS += $(DPDK_INC)
C_SRCS = dev.c lun.c lun_db.c port.c scsi.c scsi_bdev.c scsi_rpc.c task.c
LIBNAME = scsi

include $(SPDK_ROOT_DIR)/mk/spdk.lib.mk

lib/scsi/dev.c

0 → 100644
+258 −0
Original line number Diff line number Diff line
/*-
 *   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"

static struct spdk_scsi_dev g_devs[SPDK_SCSI_MAX_DEVS];

struct spdk_scsi_dev *
spdk_scsi_dev_get_list(void)
{
	return g_devs;
}

static struct spdk_scsi_dev *
allocate_dev(void)
{
	struct spdk_scsi_dev *dev;
	int i;

	for (i = 0; i < SPDK_SCSI_MAX_DEVS; i++) {
		dev = &g_devs[i];
		if (!dev->is_allocated) {
			memset(dev, 0, sizeof(*dev));
			dev->id = i;
			dev->is_allocated = 1;
			return dev;
		}
	}

	return NULL;
}

static void
free_dev(struct spdk_scsi_dev *dev)
{
	dev->is_allocated = 0;
}

void
spdk_scsi_dev_destruct(struct spdk_scsi_dev *dev)
{
	int i;

	if (dev == NULL) {
		return;
	}

	for (i = 0; i < dev->maxlun; i++) {
		if (dev->lun[i] == NULL) {
			continue;
		}

		spdk_scsi_lun_unclaim(dev->lun[i]);
		dev->lun[i] = NULL;
	}

	free_dev(dev);
}

static void
spdk_scsi_dev_add_lun(struct spdk_scsi_dev *dev,
		      struct spdk_scsi_lun *lun, int id)
{
	spdk_scsi_lun_claim(lun);
	lun->id = id;
	lun->dev = dev;
	dev->lun[id] = lun;
	if (dev->maxlun <= id) {
		dev->maxlun = id + 1;
	}
}

void
spdk_scsi_dev_delete_lun(struct spdk_scsi_dev *dev,
			 struct spdk_scsi_lun *lun)
{
	int i;
	int maxlun = 0;

	for (i = 0; i < dev->maxlun; i++) {
		if (dev->lun[i] && dev->lun[i] == lun)
			dev->lun[i] = NULL;
	}

	for (i = 0; i < dev->maxlun; i++) {
		if (dev->lun[i]) {
			if (maxlun <= dev->lun[i]->id) {
				maxlun = dev->lun[i]->id + 1;
			}
		}
	}
	dev->maxlun = maxlun;
}

/* This typedef exists to work around an astyle 2.05 bug.
 * Remove it when astyle is fixed.
*/
typedef struct spdk_scsi_dev _spdk_scsi_dev;

_spdk_scsi_dev *
spdk_scsi_dev_construct(const char *name, char *lun_name_list[], int *lun_id_list, int num_luns)
{
	struct spdk_scsi_dev *dev;
	struct spdk_bdev *bdev;
	struct spdk_scsi_lun *lun;
	int i;

	if (num_luns == 0) {
		SPDK_ERRLOG("device %s: no LUNs specified\n", name);
		return NULL;
	}

	if (lun_id_list[0] != 0) {
		SPDK_ERRLOG("device %s: no LUN 0 specified\n", name);
		return NULL;
	}

	for (i = 0; i < num_luns; i++) {
		if (lun_name_list[i] == NULL) {
			SPDK_ERRLOG("NULL spdk_scsi_lun for LUN %d\n",
				    lun_id_list[i]);
			return NULL;
		}
	}

	dev = allocate_dev();
	if (dev == NULL) {
		return NULL;
	}

	strncpy(dev->name, name, SPDK_SCSI_DEV_MAX_NAME);

	dev->num_ports = 0;
	dev->maxlun = 0;

	for (i = 0; i < num_luns; i++) {
		bdev = spdk_bdev_db_get_by_name(lun_name_list[i]);
		if (bdev == NULL) {
			free_dev(dev);
			return NULL;
		}

		lun = spdk_scsi_lun_construct(bdev->name, bdev);
		if (lun == NULL) {
			free_dev(dev);
			return NULL;
		}

		spdk_scsi_dev_add_lun(dev, lun, lun_id_list[i]);
	}

	return dev;
}

void
spdk_scsi_dev_queue_mgmt_task(struct spdk_scsi_dev *dev,
			      struct spdk_scsi_task *task)
{
	assert(task != NULL);

	spdk_scsi_lun_task_mgmt_execute(task);
}

void
spdk_scsi_dev_queue_task(struct spdk_scsi_dev *dev,
			 struct spdk_scsi_task *task)
{
	assert(task != NULL);

	/* ready to enqueue, disk is valid for LUN access */
	spdk_scsi_lun_append_task(task->lun, task);
	spdk_scsi_lun_execute_tasks(task->lun);
}

int
spdk_scsi_dev_add_port(struct spdk_scsi_dev *dev, uint64_t id, const char *name)
{
	struct spdk_scsi_port *port;
	int rc;

	if (dev->num_ports == SPDK_SCSI_DEV_MAX_PORTS) {
		SPDK_ERRLOG("device already has %d ports\n", SPDK_SCSI_DEV_MAX_PORTS);
		return -1;
	}

	port = &dev->port[dev->num_ports];

	rc = spdk_scsi_port_construct(port, id, dev->num_ports, name);
	if (rc != 0) {
		return rc;
	}

	dev->num_ports++;
	return 0;
}

struct spdk_scsi_port *
spdk_scsi_dev_find_port_by_id(struct spdk_scsi_dev *dev, uint64_t id)
{
	int i;

	for (i = 0; i < dev->num_ports; i++) {
		if (dev->port[i].id == id) {
			return &dev->port[i];
		}
	}

	/* No matching port found. */
	return NULL;
}

void
spdk_scsi_dev_print(struct spdk_scsi_dev *dev)
{
	struct spdk_scsi_lun *lun;
	int i;

	printf("device %d HDD UNIT\n", dev->id);

	for (i = 0; i < dev->maxlun; i++) {
		lun = dev->lun[i];
		if (lun == NULL)
			continue;
		printf("device %d: LUN%d %s\n", dev->id, i, lun->name);
	}
}
Loading