scsi: Add helper functions to convert LUN ID between structure and integer

SPDK iSCSI target didn't convert LUN ID from integer to structure
when it sends R2T PDUs. The next patch will fix the issue. Introducing
helper functions into SCSI library and using them will be clean. Hence
this patch adds two helper functions to convert LUN ID between structure
and integer.

The logic of helper functions is derived simply from the current
implementation in SPDK.

Change-Id: I114b546cfcb44109d6cd131a1fa972f4d6bfea38
Signed-off-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/449962
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
This commit is contained in:
Shuhei Matsumoto 2019-04-03 11:38:22 +09:00 committed by Jim Harris
parent 9f7582c3a5
commit 12ab86e24d
4 changed files with 63 additions and 17 deletions

View File

@ -540,6 +540,24 @@ bool spdk_scsi_lun_get_dif_ctx(struct spdk_scsi_lun *lun, uint8_t *cdb, uint32_t
*/
void spdk_scsi_port_set_iscsi_transport_id(struct spdk_scsi_port *port,
char *iscsi_name, uint64_t isid);
/**
* Convert LUN ID from integer to LUN format
*
* \param lun_id Integer LUN ID
*
* \return LUN format of LUN ID
*/
uint64_t spdk_scsi_lun_id_int_to_fmt(int lun_id);
/**
* Convert LUN ID from LUN format to integer
*
* \param fmt_lun LUN format of LUN ID
*
* \return integer LUN ID
*/
int spdk_scsi_lun_id_fmt_to_int(uint64_t fmt_lun);
#ifdef __cplusplus
}
#endif

View File

@ -66,4 +66,45 @@ SPDK_TRACE_REGISTER_FN(scsi_trace, "scsi", TRACE_GROUP_SCSI)
OWNER_SCSI_DEV, OBJECT_SCSI_TASK, 0, 0, "");
}
uint64_t
spdk_scsi_lun_id_int_to_fmt(int lun_id)
{
uint64_t fmt_lun, method;
if (SPDK_SCSI_DEV_MAX_LUN <= 0x0100) {
/* below 256 */
method = 0x00U;
fmt_lun = (method & 0x03U) << 62;
fmt_lun |= ((uint64_t)lun_id & 0x00ffU) << 48;
} else if (SPDK_SCSI_DEV_MAX_LUN <= 0x4000) {
/* below 16384 */
method = 0x01U;
fmt_lun = (method & 0x03U) << 62;
fmt_lun |= ((uint64_t)lun_id & 0x3fffU) << 48;
} else {
/* XXX */
fmt_lun = 0;
}
return fmt_lun;
}
int
spdk_scsi_lun_id_fmt_to_int(uint64_t fmt_lun)
{
uint64_t method;
int lun_i;
method = (fmt_lun >> 62) & 0x03U;
fmt_lun = fmt_lun >> 48;
if (method == 0x00U) {
lun_i = (int)(fmt_lun & 0x00ffU);
} else if (method == 0x01U) {
lun_i = (int)(fmt_lun & 0x3fffU);
} else {
lun_i = 0xffffU;
}
return lun_i;
}
SPDK_LOG_REGISTER_COMPONENT("scsi", SPDK_LOG_SCSI)

View File

@ -107,7 +107,7 @@ bdev_scsi_report_luns(struct spdk_scsi_lun *lun,
int sel, uint8_t *data, int alloc_len)
{
struct spdk_scsi_dev *dev;
uint64_t fmt_lun, lun_id, method;
uint64_t fmt_lun;
int hlen, len = 0;
int i;
@ -143,22 +143,7 @@ bdev_scsi_report_luns(struct spdk_scsi_lun *lun,
return -1;
}
lun_id = (uint64_t)i;
if (SPDK_SCSI_DEV_MAX_LUN <= 0x0100) {
/* below 256 */
method = 0x00U;
fmt_lun = (method & 0x03U) << 62;
fmt_lun |= (lun_id & 0x00ffU) << 48;
} else if (SPDK_SCSI_DEV_MAX_LUN <= 0x4000) {
/* below 16384 */
method = 0x01U;
fmt_lun = (method & 0x03U) << 62;
fmt_lun |= (lun_id & 0x3fffU) << 48;
} else {
/* XXX */
fmt_lun = 0;
}
fmt_lun = spdk_scsi_lun_id_int_to_fmt(i);
/* LUN */
to_be64(&data[hlen + len], fmt_lun);

View File

@ -111,6 +111,8 @@ spdk_scsi_lun_complete_task(struct spdk_scsi_lun *lun, struct spdk_scsi_task *ta
DEFINE_STUB_V(spdk_scsi_lun_complete_reset_task,
(struct spdk_scsi_lun *lun, struct spdk_scsi_task *task));
DEFINE_STUB(spdk_scsi_lun_id_int_to_fmt, uint64_t, (int lun_id), 0);
static void
ut_put_task(struct spdk_scsi_task *task)
{