2018-11-27 05:23:38 +00:00
|
|
|
/*-
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "spdk/dif.h"
|
|
|
|
#include "spdk/crc16.h"
|
2019-05-29 07:01:39 +00:00
|
|
|
#include "spdk/crc32.h"
|
2018-11-27 05:23:38 +00:00
|
|
|
#include "spdk/endian.h"
|
|
|
|
#include "spdk/log.h"
|
|
|
|
#include "spdk/util.h"
|
|
|
|
|
2019-05-15 07:27:17 +00:00
|
|
|
/* Context to iterate or create a iovec array.
|
|
|
|
* Each sgl is either iterated or created at a time.
|
|
|
|
*/
|
2019-05-14 23:19:27 +00:00
|
|
|
struct _dif_sgl {
|
2019-05-28 04:25:26 +00:00
|
|
|
/* Current iovec in the iteration or creation */
|
2018-12-19 01:11:48 +00:00
|
|
|
struct iovec *iov;
|
|
|
|
|
2019-05-28 04:25:26 +00:00
|
|
|
/* Remaining count of iovecs in the iteration or creation. */
|
2018-12-19 01:11:48 +00:00
|
|
|
int iovcnt;
|
|
|
|
|
|
|
|
/* Current offset in the iovec */
|
|
|
|
uint32_t iov_offset;
|
2019-05-15 07:27:17 +00:00
|
|
|
|
|
|
|
/* Size of the created iovec array in bytes */
|
|
|
|
uint32_t total_size;
|
2018-12-19 01:11:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline void
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_init(struct _dif_sgl *s, struct iovec *iovs, int iovcnt)
|
2018-12-19 01:11:48 +00:00
|
|
|
{
|
2019-05-14 23:19:27 +00:00
|
|
|
s->iov = iovs;
|
|
|
|
s->iovcnt = iovcnt;
|
|
|
|
s->iov_offset = 0;
|
2019-05-15 07:27:17 +00:00
|
|
|
s->total_size = 0;
|
2018-12-19 01:11:48 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 09:11:16 +00:00
|
|
|
static void
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_advance(struct _dif_sgl *s, uint32_t step)
|
2018-12-19 01:11:48 +00:00
|
|
|
{
|
2019-05-14 23:19:27 +00:00
|
|
|
s->iov_offset += step;
|
2019-05-29 09:11:16 +00:00
|
|
|
while (s->iovcnt != 0) {
|
|
|
|
if (s->iov_offset < s->iov->iov_len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->iov_offset -= s->iov->iov_len;
|
2019-05-14 23:19:27 +00:00
|
|
|
s->iov++;
|
|
|
|
s->iovcnt--;
|
2018-12-19 01:11:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_get_buf(struct _dif_sgl *s, void **_buf, uint32_t *_buf_len)
|
2018-12-19 01:11:48 +00:00
|
|
|
{
|
|
|
|
if (_buf != NULL) {
|
2019-05-14 23:19:27 +00:00
|
|
|
*_buf = s->iov->iov_base + s->iov_offset;
|
2018-12-19 01:11:48 +00:00
|
|
|
}
|
|
|
|
if (_buf_len != NULL) {
|
2019-05-14 23:19:27 +00:00
|
|
|
*_buf_len = s->iov->iov_len - s->iov_offset;
|
2018-12-19 01:11:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 07:27:17 +00:00
|
|
|
static inline bool
|
|
|
|
_dif_sgl_append(struct _dif_sgl *s, uint8_t *data, uint32_t data_len)
|
|
|
|
{
|
|
|
|
assert(s->iovcnt > 0);
|
|
|
|
s->iov->iov_base = data;
|
|
|
|
s->iov->iov_len = data_len;
|
|
|
|
s->total_size += data_len;
|
|
|
|
s->iov++;
|
|
|
|
s->iovcnt--;
|
|
|
|
|
|
|
|
if (s->iovcnt > 0) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 22:46:42 +00:00
|
|
|
static inline bool
|
|
|
|
_dif_sgl_append_split(struct _dif_sgl *dst, struct _dif_sgl *src, uint32_t data_len)
|
|
|
|
{
|
|
|
|
uint8_t *buf;
|
|
|
|
uint32_t buf_len;
|
|
|
|
|
|
|
|
while (data_len != 0) {
|
|
|
|
_dif_sgl_get_buf(src, (void *)&buf, &buf_len);
|
|
|
|
buf_len = spdk_min(buf_len, data_len);
|
|
|
|
|
|
|
|
if (!_dif_sgl_append(dst, buf, buf_len)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_dif_sgl_advance(src, buf_len);
|
|
|
|
data_len -= buf_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-15 00:42:34 +00:00
|
|
|
/* This function must be used before starting iteration. */
|
2018-11-27 05:23:38 +00:00
|
|
|
static bool
|
2019-05-15 00:42:34 +00:00
|
|
|
_dif_sgl_is_bytes_multiple(struct _dif_sgl *s, uint32_t bytes)
|
2018-11-27 05:23:38 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2019-05-15 00:42:34 +00:00
|
|
|
for (i = 0; i < s->iovcnt; i++) {
|
|
|
|
if (s->iov[i].iov_len % bytes) {
|
2018-11-27 05:23:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-15 00:42:34 +00:00
|
|
|
/* This function must be used before starting iteration. */
|
2018-11-27 05:23:38 +00:00
|
|
|
static bool
|
2019-05-15 00:42:34 +00:00
|
|
|
_dif_sgl_is_valid(struct _dif_sgl *s, uint32_t bytes)
|
2018-11-27 05:23:38 +00:00
|
|
|
{
|
|
|
|
uint64_t total = 0;
|
|
|
|
int i;
|
|
|
|
|
2019-05-15 00:42:34 +00:00
|
|
|
for (i = 0; i < s->iovcnt; i++) {
|
|
|
|
total += s->iov[i].iov_len;
|
2018-11-27 05:23:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return total >= bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
_dif_type_is_valid(enum spdk_dif_type dif_type, uint32_t dif_flags)
|
|
|
|
{
|
|
|
|
switch (dif_type) {
|
|
|
|
case SPDK_DIF_TYPE1:
|
|
|
|
case SPDK_DIF_TYPE2:
|
2018-12-27 22:57:26 +00:00
|
|
|
case SPDK_DIF_DISABLE:
|
2018-11-27 05:23:38 +00:00
|
|
|
break;
|
|
|
|
case SPDK_DIF_TYPE3:
|
2019-02-04 23:54:15 +00:00
|
|
|
if (dif_flags & SPDK_DIF_FLAGS_REFTAG_CHECK) {
|
2018-11-27 05:23:38 +00:00
|
|
|
SPDK_ERRLOG("Reference Tag should not be checked for Type 3\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SPDK_ERRLOG("Unknown DIF Type: %d\n", dif_type);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-27 22:57:26 +00:00
|
|
|
static bool
|
|
|
|
_dif_is_disabled(enum spdk_dif_type dif_type)
|
|
|
|
{
|
|
|
|
if (dif_type == SPDK_DIF_DISABLE) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 23:07:08 +00:00
|
|
|
|
2018-11-27 05:23:38 +00:00
|
|
|
static uint32_t
|
2019-01-08 23:07:08 +00:00
|
|
|
_get_guard_interval(uint32_t block_size, uint32_t md_size, bool dif_loc, bool md_interleave)
|
2018-11-27 05:23:38 +00:00
|
|
|
{
|
2019-01-16 06:40:59 +00:00
|
|
|
if (!dif_loc) {
|
2018-11-27 05:23:38 +00:00
|
|
|
/* For metadata formats with more than 8 bytes, if the DIF is
|
|
|
|
* contained in the last 8 bytes of metadata, then the CRC
|
|
|
|
* covers all metadata up to but excluding these last 8 bytes.
|
|
|
|
*/
|
2019-01-08 23:07:08 +00:00
|
|
|
if (md_interleave) {
|
|
|
|
return block_size - sizeof(struct spdk_dif);
|
|
|
|
} else {
|
|
|
|
return md_size - sizeof(struct spdk_dif);
|
|
|
|
}
|
2018-11-27 05:23:38 +00:00
|
|
|
} else {
|
|
|
|
/* For metadata formats with more than 8 bytes, if the DIF is
|
|
|
|
* contained in the first 8 bytes of metadata, then the CRC
|
|
|
|
* does not cover any metadata.
|
|
|
|
*/
|
2019-01-08 23:07:08 +00:00
|
|
|
if (md_interleave) {
|
|
|
|
return block_size - md_size;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-11-27 05:23:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 00:20:35 +00:00
|
|
|
int
|
|
|
|
spdk_dif_ctx_init(struct spdk_dif_ctx *ctx, uint32_t block_size, uint32_t md_size,
|
2019-01-08 23:07:08 +00:00
|
|
|
bool md_interleave, bool dif_loc, enum spdk_dif_type dif_type, uint32_t dif_flags,
|
2019-02-13 06:17:49 +00:00
|
|
|
uint32_t init_ref_tag, uint16_t apptag_mask, uint16_t app_tag,
|
2019-06-04 05:52:24 +00:00
|
|
|
uint32_t data_offset, uint16_t guard_seed)
|
2019-01-07 00:20:35 +00:00
|
|
|
{
|
2019-06-04 05:52:24 +00:00
|
|
|
uint32_t data_block_size;
|
|
|
|
|
2019-01-07 00:20:35 +00:00
|
|
|
if (md_size < sizeof(struct spdk_dif)) {
|
|
|
|
SPDK_ERRLOG("Metadata size is smaller than DIF size.\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-01-08 23:07:08 +00:00
|
|
|
if (md_interleave) {
|
|
|
|
if (block_size < md_size) {
|
|
|
|
SPDK_ERRLOG("Block size is smaller than DIF size.\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2019-06-04 05:52:24 +00:00
|
|
|
data_block_size = block_size - md_size;
|
2019-01-08 23:07:08 +00:00
|
|
|
} else {
|
|
|
|
if (block_size == 0 || (block_size % 512) != 0) {
|
|
|
|
SPDK_ERRLOG("Zero block size is not allowed\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2019-06-04 05:52:24 +00:00
|
|
|
data_block_size = block_size;
|
2019-01-07 00:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!_dif_type_is_valid(dif_type, dif_flags)) {
|
|
|
|
SPDK_ERRLOG("DIF type is invalid.\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->block_size = block_size;
|
|
|
|
ctx->md_size = md_size;
|
2019-01-08 23:07:08 +00:00
|
|
|
ctx->guard_interval = _get_guard_interval(block_size, md_size, dif_loc, md_interleave);
|
2019-01-07 00:20:35 +00:00
|
|
|
ctx->dif_type = dif_type;
|
|
|
|
ctx->dif_flags = dif_flags;
|
|
|
|
ctx->init_ref_tag = init_ref_tag;
|
|
|
|
ctx->apptag_mask = apptag_mask;
|
|
|
|
ctx->app_tag = app_tag;
|
2019-06-04 05:52:24 +00:00
|
|
|
ctx->data_offset = data_offset;
|
|
|
|
ctx->ref_tag_offset = data_offset / data_block_size;
|
2019-02-13 06:17:49 +00:00
|
|
|
ctx->guard_seed = guard_seed;
|
2019-01-07 00:20:35 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-27 05:23:38 +00:00
|
|
|
static void
|
2019-01-07 00:20:35 +00:00
|
|
|
_dif_generate(void *_dif, uint16_t guard, uint32_t offset_blocks,
|
|
|
|
const struct spdk_dif_ctx *ctx)
|
2018-11-27 05:23:38 +00:00
|
|
|
{
|
|
|
|
struct spdk_dif *dif = _dif;
|
2019-01-07 00:20:35 +00:00
|
|
|
uint32_t ref_tag;
|
2018-11-27 05:23:38 +00:00
|
|
|
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
2018-11-27 05:23:38 +00:00
|
|
|
to_be16(&dif->guard, guard);
|
|
|
|
}
|
|
|
|
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_APPTAG_CHECK) {
|
2019-01-07 00:20:35 +00:00
|
|
|
to_be16(&dif->app_tag, ctx->app_tag);
|
2018-11-27 05:23:38 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_REFTAG_CHECK) {
|
2019-01-07 00:20:35 +00:00
|
|
|
/* For type 1 and 2, the reference tag is incremented for each
|
|
|
|
* subsequent logical block. For type 3, the reference tag
|
|
|
|
* remains the same as the initial reference tag.
|
|
|
|
*/
|
|
|
|
if (ctx->dif_type != SPDK_DIF_TYPE3) {
|
2019-06-04 05:52:24 +00:00
|
|
|
ref_tag = ctx->init_ref_tag + ctx->ref_tag_offset + offset_blocks;
|
2019-01-07 00:20:35 +00:00
|
|
|
} else {
|
2019-06-04 05:52:24 +00:00
|
|
|
ref_tag = ctx->init_ref_tag + ctx->ref_tag_offset;
|
2019-01-07 00:20:35 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 05:23:38 +00:00
|
|
|
to_be32(&dif->ref_tag, ref_tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-05-15 00:05:13 +00:00
|
|
|
dif_generate(struct _dif_sgl *sgl, uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
|
2018-11-27 05:23:38 +00:00
|
|
|
{
|
2019-05-15 00:05:13 +00:00
|
|
|
uint32_t offset_blocks = 0;
|
2018-11-27 05:23:38 +00:00
|
|
|
void *buf;
|
|
|
|
uint16_t guard = 0;
|
|
|
|
|
2019-01-10 11:52:53 +00:00
|
|
|
while (offset_blocks < num_blocks) {
|
2019-05-15 00:05:13 +00:00
|
|
|
_dif_sgl_get_buf(sgl, &buf, NULL);
|
2018-11-27 05:23:38 +00:00
|
|
|
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
2019-02-13 06:17:49 +00:00
|
|
|
guard = spdk_crc16_t10dif(ctx->guard_seed, buf, ctx->guard_interval);
|
2018-11-27 05:23:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 00:20:35 +00:00
|
|
|
_dif_generate(buf + ctx->guard_interval, guard, offset_blocks, ctx);
|
2018-11-27 05:23:38 +00:00
|
|
|
|
2019-05-15 00:05:13 +00:00
|
|
|
_dif_sgl_advance(sgl, ctx->block_size);
|
2018-11-27 05:23:38 +00:00
|
|
|
offset_blocks++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 01:26:36 +00:00
|
|
|
static void
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_generate_split(struct _dif_sgl *sgl, uint32_t offset_blocks,
|
2019-01-07 00:20:35 +00:00
|
|
|
const struct spdk_dif_ctx *ctx)
|
2018-12-19 01:26:36 +00:00
|
|
|
{
|
|
|
|
uint32_t offset_in_block, offset_in_dif, buf_len;
|
|
|
|
void *buf;
|
2019-02-13 06:17:49 +00:00
|
|
|
uint16_t guard = 0;
|
2018-12-19 01:26:36 +00:00
|
|
|
struct spdk_dif dif = {};
|
|
|
|
|
2019-02-13 06:17:49 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
|
|
|
guard = ctx->guard_seed;
|
|
|
|
}
|
2018-12-19 01:26:36 +00:00
|
|
|
offset_in_block = 0;
|
|
|
|
|
2019-01-10 11:52:53 +00:00
|
|
|
while (offset_in_block < ctx->block_size) {
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_get_buf(sgl, &buf, &buf_len);
|
2018-12-19 01:26:36 +00:00
|
|
|
|
2019-01-07 00:20:35 +00:00
|
|
|
if (offset_in_block < ctx->guard_interval) {
|
|
|
|
buf_len = spdk_min(buf_len, ctx->guard_interval - offset_in_block);
|
2018-12-19 01:26:36 +00:00
|
|
|
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
2018-12-19 01:26:36 +00:00
|
|
|
/* Compute CRC over split logical block data. */
|
|
|
|
guard = spdk_crc16_t10dif(guard, buf, buf_len);
|
|
|
|
}
|
|
|
|
|
2019-01-07 00:20:35 +00:00
|
|
|
if (offset_in_block + buf_len == ctx->guard_interval) {
|
2018-12-19 01:26:36 +00:00
|
|
|
/* If a whole logical block data is parsed, generate DIF
|
|
|
|
* and save it to the temporary DIF area.
|
|
|
|
*/
|
2019-01-07 00:20:35 +00:00
|
|
|
_dif_generate(&dif, guard, offset_blocks, ctx);
|
2018-12-19 01:26:36 +00:00
|
|
|
}
|
2019-01-07 00:20:35 +00:00
|
|
|
} else if (offset_in_block < ctx->guard_interval + sizeof(struct spdk_dif)) {
|
2018-12-19 01:26:36 +00:00
|
|
|
/* Copy generated DIF to the split DIF field. */
|
2019-01-07 00:20:35 +00:00
|
|
|
offset_in_dif = offset_in_block - ctx->guard_interval;
|
2018-12-19 01:26:36 +00:00
|
|
|
buf_len = spdk_min(buf_len, sizeof(struct spdk_dif) - offset_in_dif);
|
|
|
|
|
|
|
|
memcpy(buf, ((uint8_t *)&dif) + offset_in_dif, buf_len);
|
|
|
|
} else {
|
|
|
|
/* Skip metadata field after DIF field. */
|
2019-01-07 00:20:35 +00:00
|
|
|
buf_len = spdk_min(buf_len, ctx->block_size - offset_in_block);
|
2018-12-19 01:26:36 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_advance(sgl, buf_len);
|
2018-12-19 01:26:36 +00:00
|
|
|
offset_in_block += buf_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 05:23:38 +00:00
|
|
|
static void
|
2019-05-15 00:05:13 +00:00
|
|
|
dif_generate_split(struct _dif_sgl *sgl, uint32_t num_blocks,
|
2019-01-07 00:20:35 +00:00
|
|
|
const struct spdk_dif_ctx *ctx)
|
2018-11-27 05:23:38 +00:00
|
|
|
{
|
2019-05-15 00:13:46 +00:00
|
|
|
uint32_t offset_blocks;
|
2018-11-27 05:23:38 +00:00
|
|
|
|
2019-05-15 00:13:46 +00:00
|
|
|
for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
|
2019-05-15 00:05:13 +00:00
|
|
|
_dif_generate_split(sgl, offset_blocks, ctx);
|
2018-11-27 05:23:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-01-07 00:20:35 +00:00
|
|
|
spdk_dif_generate(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
|
|
|
|
const struct spdk_dif_ctx *ctx)
|
2018-11-27 05:23:38 +00:00
|
|
|
{
|
2019-05-15 00:05:13 +00:00
|
|
|
struct _dif_sgl sgl;
|
|
|
|
|
|
|
|
_dif_sgl_init(&sgl, iovs, iovcnt);
|
|
|
|
|
2019-05-15 00:42:34 +00:00
|
|
|
if (!_dif_sgl_is_valid(&sgl, ctx->block_size * num_blocks)) {
|
2018-12-27 23:04:26 +00:00
|
|
|
SPDK_ERRLOG("Size of iovec array is not valid.\n");
|
2018-11-27 05:23:38 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-01-07 00:20:35 +00:00
|
|
|
if (_dif_is_disabled(ctx->dif_type)) {
|
2018-12-27 22:57:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-15 00:42:34 +00:00
|
|
|
if (_dif_sgl_is_bytes_multiple(&sgl, ctx->block_size)) {
|
2019-05-15 00:05:13 +00:00
|
|
|
dif_generate(&sgl, num_blocks, ctx);
|
2018-11-27 05:23:38 +00:00
|
|
|
} else {
|
2019-05-15 00:05:13 +00:00
|
|
|
dif_generate_split(&sgl, num_blocks, ctx);
|
2018-11-27 05:23:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-28 01:18:21 +00:00
|
|
|
static void
|
|
|
|
_dif_error_set(struct spdk_dif_error *err_blk, uint8_t err_type,
|
|
|
|
uint32_t expected, uint32_t actual, uint32_t err_offset)
|
|
|
|
{
|
|
|
|
if (err_blk) {
|
|
|
|
err_blk->err_type = err_type;
|
|
|
|
err_blk->expected = expected;
|
|
|
|
err_blk->actual = actual;
|
|
|
|
err_blk->err_offset = err_offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 05:23:38 +00:00
|
|
|
static int
|
2019-01-07 00:20:35 +00:00
|
|
|
_dif_verify(void *_dif, uint16_t guard, uint32_t offset_blocks,
|
2018-12-28 01:18:21 +00:00
|
|
|
const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk)
|
2018-11-27 05:23:38 +00:00
|
|
|
{
|
|
|
|
struct spdk_dif *dif = _dif;
|
|
|
|
uint16_t _guard;
|
|
|
|
uint16_t _app_tag;
|
2019-01-07 00:20:35 +00:00
|
|
|
uint32_t ref_tag, _ref_tag;
|
2018-11-27 05:23:38 +00:00
|
|
|
|
2019-01-07 00:20:35 +00:00
|
|
|
switch (ctx->dif_type) {
|
2018-11-27 05:23:38 +00:00
|
|
|
case SPDK_DIF_TYPE1:
|
|
|
|
case SPDK_DIF_TYPE2:
|
|
|
|
/* If Type 1 or 2 is used, then all DIF checks are disabled when
|
|
|
|
* the Application Tag is 0xFFFF.
|
|
|
|
*/
|
|
|
|
if (dif->app_tag == 0xFFFF) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SPDK_DIF_TYPE3:
|
|
|
|
/* If Type 3 is used, then all DIF checks are disabled when the
|
|
|
|
* Application Tag is 0xFFFF and the Reference Tag is 0xFFFFFFFF.
|
|
|
|
*/
|
|
|
|
if (dif->app_tag == 0xFFFF && dif->ref_tag == 0xFFFFFFFF) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-12-27 22:57:26 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2018-11-27 05:23:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 00:20:35 +00:00
|
|
|
/* For type 1 and 2, the reference tag is incremented for each
|
|
|
|
* subsequent logical block. For type 3, the reference tag
|
|
|
|
* remains the same as the initial reference tag.
|
|
|
|
*/
|
|
|
|
if (ctx->dif_type != SPDK_DIF_TYPE3) {
|
2019-06-04 05:52:24 +00:00
|
|
|
ref_tag = ctx->init_ref_tag + ctx->ref_tag_offset + offset_blocks;
|
2019-01-07 00:20:35 +00:00
|
|
|
} else {
|
2019-06-04 05:52:24 +00:00
|
|
|
ref_tag = ctx->init_ref_tag + ctx->ref_tag_offset;
|
2019-01-07 00:20:35 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
2018-11-27 05:23:38 +00:00
|
|
|
/* Compare the DIF Guard field to the CRC computed over the logical
|
|
|
|
* block data.
|
|
|
|
*/
|
|
|
|
_guard = from_be16(&dif->guard);
|
|
|
|
if (_guard != guard) {
|
2018-12-28 01:18:21 +00:00
|
|
|
_dif_error_set(err_blk, SPDK_DIF_GUARD_ERROR, _guard, guard,
|
|
|
|
offset_blocks);
|
2018-11-27 05:23:38 +00:00
|
|
|
SPDK_ERRLOG("Failed to compare Guard: LBA=%" PRIu32 "," \
|
|
|
|
" Expected=%x, Actual=%x\n",
|
|
|
|
ref_tag, _guard, guard);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_APPTAG_CHECK) {
|
2018-11-27 05:23:38 +00:00
|
|
|
/* Compare unmasked bits in the DIF Application Tag field to the
|
|
|
|
* passed Application Tag.
|
|
|
|
*/
|
|
|
|
_app_tag = from_be16(&dif->app_tag);
|
2019-01-07 00:20:35 +00:00
|
|
|
if ((_app_tag & ctx->apptag_mask) != ctx->app_tag) {
|
2018-12-28 01:18:21 +00:00
|
|
|
_dif_error_set(err_blk, SPDK_DIF_APPTAG_ERROR, ctx->app_tag,
|
|
|
|
(_app_tag & ctx->apptag_mask), offset_blocks);
|
2018-11-27 05:23:38 +00:00
|
|
|
SPDK_ERRLOG("Failed to compare App Tag: LBA=%" PRIu32 "," \
|
|
|
|
" Expected=%x, Actual=%x\n",
|
2019-01-07 00:20:35 +00:00
|
|
|
ref_tag, ctx->app_tag, (_app_tag & ctx->apptag_mask));
|
2018-11-27 05:23:38 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_REFTAG_CHECK) {
|
2019-01-07 00:20:35 +00:00
|
|
|
switch (ctx->dif_type) {
|
2018-11-27 05:23:38 +00:00
|
|
|
case SPDK_DIF_TYPE1:
|
|
|
|
case SPDK_DIF_TYPE2:
|
|
|
|
/* Compare the DIF Reference Tag field to the passed Reference Tag.
|
|
|
|
* The passed Reference Tag will be the least significant 4 bytes
|
|
|
|
* of the LBA when Type 1 is used, and application specific value
|
|
|
|
* if Type 2 is used,
|
|
|
|
*/
|
|
|
|
_ref_tag = from_be32(&dif->ref_tag);
|
|
|
|
if (_ref_tag != ref_tag) {
|
2018-12-28 01:18:21 +00:00
|
|
|
_dif_error_set(err_blk, SPDK_DIF_REFTAG_ERROR, ref_tag,
|
|
|
|
_ref_tag, offset_blocks);
|
2018-11-27 05:23:38 +00:00
|
|
|
SPDK_ERRLOG("Failed to compare Ref Tag: LBA=%" PRIu32 "," \
|
|
|
|
" Expected=%x, Actual=%x\n",
|
|
|
|
ref_tag, ref_tag, _ref_tag);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SPDK_DIF_TYPE3:
|
|
|
|
/* For Type 3, computed Reference Tag remains unchanged.
|
|
|
|
* Hence ignore the Reference Tag field.
|
|
|
|
*/
|
|
|
|
break;
|
2018-12-27 22:57:26 +00:00
|
|
|
default:
|
|
|
|
break;
|
2018-11-27 05:23:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-05-15 00:05:13 +00:00
|
|
|
dif_verify(struct _dif_sgl *sgl, uint32_t num_blocks,
|
2018-12-28 01:18:21 +00:00
|
|
|
const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk)
|
2018-11-27 05:23:38 +00:00
|
|
|
{
|
2019-05-15 00:05:13 +00:00
|
|
|
uint32_t offset_blocks = 0;
|
2018-12-19 01:11:48 +00:00
|
|
|
int rc;
|
2018-11-27 05:23:38 +00:00
|
|
|
void *buf;
|
|
|
|
uint16_t guard = 0;
|
|
|
|
|
2019-01-10 11:52:53 +00:00
|
|
|
while (offset_blocks < num_blocks) {
|
2019-05-15 00:05:13 +00:00
|
|
|
_dif_sgl_get_buf(sgl, &buf, NULL);
|
2018-11-27 05:23:38 +00:00
|
|
|
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
2019-02-13 06:17:49 +00:00
|
|
|
guard = spdk_crc16_t10dif(ctx->guard_seed, buf, ctx->guard_interval);
|
2018-11-27 05:23:38 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 01:18:21 +00:00
|
|
|
rc = _dif_verify(buf + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
|
2018-11-27 05:23:38 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-05-15 00:05:13 +00:00
|
|
|
_dif_sgl_advance(sgl, ctx->block_size);
|
2018-11-27 05:23:38 +00:00
|
|
|
offset_blocks++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-19 01:26:36 +00:00
|
|
|
static int
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_verify_split(struct _dif_sgl *sgl, uint32_t offset_blocks,
|
2018-12-28 01:18:21 +00:00
|
|
|
const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk)
|
2018-12-19 01:26:36 +00:00
|
|
|
{
|
|
|
|
uint32_t offset_in_block, offset_in_dif, buf_len;
|
|
|
|
void *buf;
|
2019-02-13 06:17:49 +00:00
|
|
|
uint16_t guard = 0;
|
2018-12-19 01:26:36 +00:00
|
|
|
struct spdk_dif dif = {};
|
|
|
|
|
2019-02-13 06:17:49 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
|
|
|
guard = ctx->guard_seed;
|
|
|
|
}
|
2018-12-19 01:26:36 +00:00
|
|
|
offset_in_block = 0;
|
|
|
|
|
2019-01-10 11:52:53 +00:00
|
|
|
while (offset_in_block < ctx->block_size) {
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_get_buf(sgl, &buf, &buf_len);
|
2018-12-19 01:26:36 +00:00
|
|
|
|
2019-01-07 00:20:35 +00:00
|
|
|
if (offset_in_block < ctx->guard_interval) {
|
|
|
|
buf_len = spdk_min(buf_len, ctx->guard_interval - offset_in_block);
|
2018-12-19 01:26:36 +00:00
|
|
|
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
2018-12-19 01:26:36 +00:00
|
|
|
/* Compute CRC over split logical block data. */
|
|
|
|
guard = spdk_crc16_t10dif(guard, buf, buf_len);
|
|
|
|
}
|
2019-01-07 00:20:35 +00:00
|
|
|
} else if (offset_in_block < ctx->guard_interval + sizeof(struct spdk_dif)) {
|
2018-12-19 01:26:36 +00:00
|
|
|
/* Copy the split DIF field to the temporary DIF buffer. */
|
2019-01-07 00:20:35 +00:00
|
|
|
offset_in_dif = offset_in_block - ctx->guard_interval;
|
2018-12-19 01:26:36 +00:00
|
|
|
buf_len = spdk_min(buf_len, sizeof(struct spdk_dif) - offset_in_dif);
|
|
|
|
|
|
|
|
memcpy((uint8_t *)&dif + offset_in_dif, buf, buf_len);
|
|
|
|
} else {
|
|
|
|
/* Skip metadata field after DIF field. */
|
2019-01-07 00:20:35 +00:00
|
|
|
buf_len = spdk_min(buf_len, ctx->block_size - offset_in_block);
|
2018-12-19 01:26:36 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_advance(sgl, buf_len);
|
2018-12-19 01:26:36 +00:00
|
|
|
offset_in_block += buf_len;
|
|
|
|
}
|
|
|
|
|
2018-12-28 01:18:21 +00:00
|
|
|
return _dif_verify(&dif, guard, offset_blocks, ctx, err_blk);
|
2018-12-19 01:26:36 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 05:23:38 +00:00
|
|
|
static int
|
2019-05-15 00:05:13 +00:00
|
|
|
dif_verify_split(struct _dif_sgl *sgl, uint32_t num_blocks,
|
2018-12-28 01:18:21 +00:00
|
|
|
const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk)
|
2018-11-27 05:23:38 +00:00
|
|
|
{
|
2019-05-15 00:13:46 +00:00
|
|
|
uint32_t offset_blocks;
|
2018-12-19 01:11:48 +00:00
|
|
|
int rc;
|
2018-11-27 05:23:38 +00:00
|
|
|
|
2019-05-15 00:13:46 +00:00
|
|
|
for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
|
2019-05-15 00:05:13 +00:00
|
|
|
rc = _dif_verify_split(sgl, offset_blocks, ctx, err_blk);
|
2018-11-27 05:23:38 +00:00
|
|
|
if (rc != 0) {
|
2018-12-19 01:26:36 +00:00
|
|
|
return rc;
|
2018-11-27 05:23:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-01-07 00:20:35 +00:00
|
|
|
spdk_dif_verify(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
|
2018-12-28 01:18:21 +00:00
|
|
|
const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk)
|
2018-11-27 05:23:38 +00:00
|
|
|
{
|
2019-05-15 00:05:13 +00:00
|
|
|
struct _dif_sgl sgl;
|
|
|
|
|
|
|
|
_dif_sgl_init(&sgl, iovs, iovcnt);
|
|
|
|
|
2019-05-15 00:42:34 +00:00
|
|
|
if (!_dif_sgl_is_valid(&sgl, ctx->block_size * num_blocks)) {
|
2018-12-27 23:04:26 +00:00
|
|
|
SPDK_ERRLOG("Size of iovec array is not valid.\n");
|
2018-11-27 05:23:38 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-01-07 00:20:35 +00:00
|
|
|
if (_dif_is_disabled(ctx->dif_type)) {
|
2018-12-27 22:57:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-15 00:42:34 +00:00
|
|
|
if (_dif_sgl_is_bytes_multiple(&sgl, ctx->block_size)) {
|
2019-05-15 00:05:13 +00:00
|
|
|
return dif_verify(&sgl, num_blocks, ctx, err_blk);
|
2018-11-27 05:23:38 +00:00
|
|
|
} else {
|
2019-05-15 00:05:13 +00:00
|
|
|
return dif_verify_split(&sgl, num_blocks, ctx, err_blk);
|
2018-11-27 05:23:38 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-18 01:51:22 +00:00
|
|
|
|
2019-05-29 07:01:39 +00:00
|
|
|
static uint32_t
|
|
|
|
dif_update_crc32c(struct _dif_sgl *sgl, uint32_t num_blocks,
|
|
|
|
uint32_t crc32c, const struct spdk_dif_ctx *ctx)
|
|
|
|
{
|
|
|
|
uint32_t offset_blocks;
|
|
|
|
void *buf;
|
|
|
|
|
|
|
|
for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
|
|
|
|
_dif_sgl_get_buf(sgl, &buf, NULL);
|
|
|
|
|
|
|
|
crc32c = spdk_crc32c_update(buf, ctx->block_size - ctx->md_size, crc32c);
|
|
|
|
|
|
|
|
_dif_sgl_advance(sgl, ctx->block_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return crc32c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t
|
|
|
|
_dif_update_crc32c_split(struct _dif_sgl *sgl, uint32_t crc32c,
|
|
|
|
const struct spdk_dif_ctx *ctx)
|
|
|
|
{
|
|
|
|
uint32_t data_block_size, offset_in_block, buf_len;
|
|
|
|
void *buf;
|
|
|
|
|
|
|
|
data_block_size = ctx->block_size - ctx->md_size;
|
|
|
|
offset_in_block = 0;
|
|
|
|
|
|
|
|
while (offset_in_block < ctx->block_size) {
|
|
|
|
_dif_sgl_get_buf(sgl, &buf, &buf_len);
|
|
|
|
|
|
|
|
if (offset_in_block < data_block_size) {
|
|
|
|
buf_len = spdk_min(buf_len, data_block_size - offset_in_block);
|
|
|
|
crc32c = spdk_crc32c_update(buf, buf_len, crc32c);
|
|
|
|
} else {
|
|
|
|
buf_len = spdk_min(buf_len, ctx->block_size - offset_in_block);
|
|
|
|
}
|
|
|
|
|
|
|
|
_dif_sgl_advance(sgl, buf_len);
|
|
|
|
offset_in_block += buf_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return crc32c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t
|
|
|
|
dif_update_crc32c_split(struct _dif_sgl *sgl, uint32_t num_blocks,
|
|
|
|
uint32_t crc32c, const struct spdk_dif_ctx *ctx)
|
|
|
|
{
|
|
|
|
uint32_t offset_blocks;
|
|
|
|
|
|
|
|
for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
|
|
|
|
crc32c = _dif_update_crc32c_split(sgl, crc32c, ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
return crc32c;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_dif_update_crc32c(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
|
|
|
|
uint32_t *_crc32c, const struct spdk_dif_ctx *ctx)
|
|
|
|
{
|
|
|
|
struct _dif_sgl sgl;
|
|
|
|
|
|
|
|
if (_crc32c == NULL) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
_dif_sgl_init(&sgl, iovs, iovcnt);
|
|
|
|
|
|
|
|
if (!_dif_sgl_is_valid(&sgl, ctx->block_size * num_blocks)) {
|
|
|
|
SPDK_ERRLOG("Size of iovec array is not valid.\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_dif_sgl_is_bytes_multiple(&sgl, ctx->block_size)) {
|
|
|
|
*_crc32c = dif_update_crc32c(&sgl, num_blocks, *_crc32c, ctx);
|
|
|
|
} else {
|
|
|
|
*_crc32c = dif_update_crc32c_split(&sgl, num_blocks, *_crc32c, ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-28 01:25:02 +00:00
|
|
|
static void
|
2019-05-15 00:05:13 +00:00
|
|
|
dif_generate_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
|
2018-12-28 01:25:02 +00:00
|
|
|
uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
|
|
|
|
{
|
2019-05-15 00:05:13 +00:00
|
|
|
uint32_t offset_blocks = 0, data_block_size;
|
2018-12-28 01:25:02 +00:00
|
|
|
void *src, *dst;
|
|
|
|
uint16_t guard;
|
|
|
|
|
|
|
|
data_block_size = ctx->block_size - ctx->md_size;
|
|
|
|
|
2019-01-10 11:52:53 +00:00
|
|
|
while (offset_blocks < num_blocks) {
|
2019-05-15 00:05:13 +00:00
|
|
|
_dif_sgl_get_buf(src_sgl, &src, NULL);
|
|
|
|
_dif_sgl_get_buf(dst_sgl, &dst, NULL);
|
2018-12-28 01:25:02 +00:00
|
|
|
|
|
|
|
guard = 0;
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
2019-02-13 06:17:49 +00:00
|
|
|
guard = spdk_crc16_t10dif_copy(ctx->guard_seed, dst, src, data_block_size);
|
2018-12-28 01:25:02 +00:00
|
|
|
guard = spdk_crc16_t10dif(guard, dst + data_block_size,
|
|
|
|
ctx->guard_interval - data_block_size);
|
|
|
|
} else {
|
|
|
|
memcpy(dst, src, data_block_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
_dif_generate(dst + ctx->guard_interval, guard, offset_blocks, ctx);
|
|
|
|
|
2019-05-15 00:05:13 +00:00
|
|
|
_dif_sgl_advance(src_sgl, data_block_size);
|
|
|
|
_dif_sgl_advance(dst_sgl, ctx->block_size);
|
2018-12-28 01:25:02 +00:00
|
|
|
offset_blocks++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_generate_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
|
2018-12-28 01:25:02 +00:00
|
|
|
uint32_t offset_blocks, const struct spdk_dif_ctx *ctx)
|
|
|
|
{
|
|
|
|
uint32_t offset_in_block, src_len, data_block_size;
|
2019-02-13 06:17:49 +00:00
|
|
|
uint16_t guard = 0;
|
2018-12-28 01:25:02 +00:00
|
|
|
void *src, *dst;
|
|
|
|
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_get_buf(dst_sgl, &dst, NULL);
|
2018-12-28 01:25:02 +00:00
|
|
|
|
|
|
|
data_block_size = ctx->block_size - ctx->md_size;
|
|
|
|
|
2019-02-13 06:17:49 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
|
|
|
guard = ctx->guard_seed;
|
|
|
|
}
|
2018-12-28 01:25:02 +00:00
|
|
|
offset_in_block = 0;
|
|
|
|
|
2019-01-10 11:52:53 +00:00
|
|
|
while (offset_in_block < data_block_size) {
|
2018-12-28 01:25:02 +00:00
|
|
|
/* Compute CRC over split logical block data and copy
|
|
|
|
* data to bounce buffer.
|
|
|
|
*/
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_get_buf(src_sgl, &src, &src_len);
|
2018-12-28 01:25:02 +00:00
|
|
|
src_len = spdk_min(src_len, data_block_size - offset_in_block);
|
|
|
|
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
2018-12-28 01:25:02 +00:00
|
|
|
guard = spdk_crc16_t10dif_copy(guard, dst + offset_in_block,
|
|
|
|
src, src_len);
|
|
|
|
} else {
|
|
|
|
memcpy(dst + offset_in_block, src, src_len);
|
|
|
|
}
|
|
|
|
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_advance(src_sgl, src_len);
|
2018-12-28 01:25:02 +00:00
|
|
|
offset_in_block += src_len;
|
|
|
|
}
|
|
|
|
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
2018-12-28 01:25:02 +00:00
|
|
|
guard = spdk_crc16_t10dif(guard, dst + data_block_size,
|
|
|
|
ctx->guard_interval - data_block_size);
|
|
|
|
}
|
|
|
|
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_advance(dst_sgl, ctx->block_size);
|
2018-12-28 01:25:02 +00:00
|
|
|
|
|
|
|
_dif_generate(dst + ctx->guard_interval, guard, offset_blocks, ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-05-15 00:05:13 +00:00
|
|
|
dif_generate_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
|
2018-12-28 01:25:02 +00:00
|
|
|
uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
|
|
|
|
{
|
2019-05-15 00:13:46 +00:00
|
|
|
uint32_t offset_blocks;
|
2018-12-28 01:25:02 +00:00
|
|
|
|
2019-05-15 00:13:46 +00:00
|
|
|
for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
|
2019-05-15 00:05:13 +00:00
|
|
|
_dif_generate_copy_split(src_sgl, dst_sgl, offset_blocks, ctx);
|
2018-12-28 01:25:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_dif_generate_copy(struct iovec *iovs, int iovcnt, struct iovec *bounce_iov,
|
|
|
|
uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
|
|
|
|
{
|
2019-05-15 00:05:13 +00:00
|
|
|
struct _dif_sgl src_sgl, dst_sgl;
|
2018-12-28 01:25:02 +00:00
|
|
|
uint32_t data_block_size;
|
|
|
|
|
2019-05-15 00:05:13 +00:00
|
|
|
_dif_sgl_init(&src_sgl, iovs, iovcnt);
|
|
|
|
_dif_sgl_init(&dst_sgl, bounce_iov, 1);
|
|
|
|
|
2018-12-28 01:25:02 +00:00
|
|
|
data_block_size = ctx->block_size - ctx->md_size;
|
|
|
|
|
2019-05-15 00:42:34 +00:00
|
|
|
if (!_dif_sgl_is_valid(&src_sgl, data_block_size * num_blocks) ||
|
|
|
|
!_dif_sgl_is_valid(&dst_sgl, ctx->block_size * num_blocks)) {
|
2018-12-28 01:25:02 +00:00
|
|
|
SPDK_ERRLOG("Size of iovec arrays are not valid.\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_dif_is_disabled(ctx->dif_type)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-15 00:42:34 +00:00
|
|
|
if (_dif_sgl_is_bytes_multiple(&src_sgl, data_block_size)) {
|
2019-05-15 00:05:13 +00:00
|
|
|
dif_generate_copy(&src_sgl, &dst_sgl, num_blocks, ctx);
|
2018-12-28 01:25:02 +00:00
|
|
|
} else {
|
2019-05-15 00:05:13 +00:00
|
|
|
dif_generate_copy_split(&src_sgl, &dst_sgl, num_blocks, ctx);
|
2018-12-28 01:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-05-15 00:05:13 +00:00
|
|
|
dif_verify_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
|
2018-12-28 01:32:13 +00:00
|
|
|
uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
|
|
|
|
struct spdk_dif_error *err_blk)
|
2018-12-28 01:25:02 +00:00
|
|
|
{
|
2019-05-15 00:05:13 +00:00
|
|
|
uint32_t offset_blocks = 0, data_block_size;
|
2018-12-28 01:25:02 +00:00
|
|
|
void *src, *dst;
|
|
|
|
int rc;
|
|
|
|
uint16_t guard;
|
|
|
|
|
|
|
|
data_block_size = ctx->block_size - ctx->md_size;
|
|
|
|
|
2019-01-10 11:52:53 +00:00
|
|
|
while (offset_blocks < num_blocks) {
|
2019-05-15 00:05:13 +00:00
|
|
|
_dif_sgl_get_buf(src_sgl, &src, NULL);
|
|
|
|
_dif_sgl_get_buf(dst_sgl, &dst, NULL);
|
2018-12-28 01:25:02 +00:00
|
|
|
|
|
|
|
guard = 0;
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
2019-02-13 06:17:49 +00:00
|
|
|
guard = spdk_crc16_t10dif_copy(ctx->guard_seed, dst, src, data_block_size);
|
2018-12-28 01:25:02 +00:00
|
|
|
guard = spdk_crc16_t10dif(guard, src + data_block_size,
|
|
|
|
ctx->guard_interval - data_block_size);
|
|
|
|
} else {
|
|
|
|
memcpy(dst, src, data_block_size);
|
|
|
|
}
|
|
|
|
|
2018-12-28 01:32:13 +00:00
|
|
|
rc = _dif_verify(src + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
|
2018-12-28 01:25:02 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-05-15 00:05:13 +00:00
|
|
|
_dif_sgl_advance(src_sgl, ctx->block_size);
|
|
|
|
_dif_sgl_advance(dst_sgl, data_block_size);
|
2018-12-28 01:25:02 +00:00
|
|
|
offset_blocks++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_verify_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
|
2018-12-28 01:32:13 +00:00
|
|
|
uint32_t offset_blocks, const struct spdk_dif_ctx *ctx,
|
|
|
|
struct spdk_dif_error *err_blk)
|
2018-12-28 01:25:02 +00:00
|
|
|
{
|
|
|
|
uint32_t offset_in_block, dst_len, data_block_size;
|
2019-02-13 06:17:49 +00:00
|
|
|
uint16_t guard = 0;
|
2018-12-28 01:25:02 +00:00
|
|
|
void *src, *dst;
|
|
|
|
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_get_buf(src_sgl, &src, NULL);
|
2018-12-28 01:25:02 +00:00
|
|
|
|
|
|
|
data_block_size = ctx->block_size - ctx->md_size;
|
|
|
|
|
2019-02-13 06:17:49 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
|
|
|
guard = ctx->guard_seed;
|
|
|
|
}
|
2018-12-28 01:25:02 +00:00
|
|
|
offset_in_block = 0;
|
|
|
|
|
|
|
|
while (offset_in_block < data_block_size) {
|
|
|
|
/* Compute CRC over split logical block data and copy
|
|
|
|
* data to bounce buffer.
|
|
|
|
*/
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_get_buf(dst_sgl, &dst, &dst_len);
|
2018-12-28 01:25:02 +00:00
|
|
|
dst_len = spdk_min(dst_len, data_block_size - offset_in_block);
|
|
|
|
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
2018-12-28 01:25:02 +00:00
|
|
|
guard = spdk_crc16_t10dif_copy(guard, dst,
|
|
|
|
src + offset_in_block, dst_len);
|
|
|
|
} else {
|
|
|
|
memcpy(dst, src + offset_in_block, dst_len);
|
|
|
|
}
|
|
|
|
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_advance(dst_sgl, dst_len);
|
2018-12-28 01:25:02 +00:00
|
|
|
offset_in_block += dst_len;
|
|
|
|
}
|
|
|
|
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
2018-12-28 01:25:02 +00:00
|
|
|
guard = spdk_crc16_t10dif(guard, src + data_block_size,
|
|
|
|
ctx->guard_interval - data_block_size);
|
|
|
|
}
|
|
|
|
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_advance(src_sgl, ctx->block_size);
|
2018-12-28 01:25:02 +00:00
|
|
|
|
2018-12-28 01:32:13 +00:00
|
|
|
return _dif_verify(src + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
|
2018-12-28 01:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-05-15 00:05:13 +00:00
|
|
|
dif_verify_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
|
2018-12-28 01:32:13 +00:00
|
|
|
uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
|
|
|
|
struct spdk_dif_error *err_blk)
|
2018-12-28 01:25:02 +00:00
|
|
|
{
|
2019-05-15 00:13:46 +00:00
|
|
|
uint32_t offset_blocks;
|
2018-12-28 01:25:02 +00:00
|
|
|
int rc;
|
|
|
|
|
2019-05-15 00:13:46 +00:00
|
|
|
for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
|
2019-05-15 00:05:13 +00:00
|
|
|
rc = _dif_verify_copy_split(src_sgl, dst_sgl, offset_blocks, ctx, err_blk);
|
2018-12-28 01:25:02 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_dif_verify_copy(struct iovec *iovs, int iovcnt, struct iovec *bounce_iov,
|
2018-12-28 01:32:13 +00:00
|
|
|
uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
|
|
|
|
struct spdk_dif_error *err_blk)
|
2018-12-28 01:25:02 +00:00
|
|
|
{
|
2019-05-15 00:05:13 +00:00
|
|
|
struct _dif_sgl src_sgl, dst_sgl;
|
2018-12-28 01:25:02 +00:00
|
|
|
uint32_t data_block_size;
|
|
|
|
|
2019-05-15 00:05:13 +00:00
|
|
|
_dif_sgl_init(&src_sgl, bounce_iov, 1);
|
|
|
|
_dif_sgl_init(&dst_sgl, iovs, iovcnt);
|
|
|
|
|
2018-12-28 01:25:02 +00:00
|
|
|
data_block_size = ctx->block_size - ctx->md_size;
|
|
|
|
|
2019-05-15 00:42:34 +00:00
|
|
|
if (!_dif_sgl_is_valid(&dst_sgl, data_block_size * num_blocks) ||
|
|
|
|
!_dif_sgl_is_valid(&src_sgl, ctx->block_size * num_blocks)) {
|
2018-12-28 01:25:02 +00:00
|
|
|
SPDK_ERRLOG("Size of iovec arrays are not valid\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_dif_is_disabled(ctx->dif_type)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-15 00:42:34 +00:00
|
|
|
if (_dif_sgl_is_bytes_multiple(&dst_sgl, data_block_size)) {
|
2019-05-15 00:05:13 +00:00
|
|
|
return dif_verify_copy(&src_sgl, &dst_sgl, num_blocks, ctx, err_blk);
|
2018-12-28 01:25:02 +00:00
|
|
|
} else {
|
2019-05-15 00:05:13 +00:00
|
|
|
return dif_verify_copy_split(&src_sgl, &dst_sgl, num_blocks, ctx, err_blk);
|
2018-12-28 01:25:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 01:51:22 +00:00
|
|
|
static void
|
|
|
|
_bit_flip(uint8_t *buf, uint32_t flip_bit)
|
|
|
|
{
|
|
|
|
uint8_t byte;
|
|
|
|
|
|
|
|
byte = *buf;
|
|
|
|
byte ^= 1 << flip_bit;
|
|
|
|
*buf = byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-05-15 00:05:13 +00:00
|
|
|
_dif_inject_error(struct _dif_sgl *sgl,
|
2018-12-18 01:51:22 +00:00
|
|
|
uint32_t block_size, uint32_t num_blocks,
|
|
|
|
uint32_t inject_offset_blocks,
|
|
|
|
uint32_t inject_offset_bytes,
|
|
|
|
uint32_t inject_offset_bits)
|
|
|
|
{
|
|
|
|
uint32_t offset_in_block, buf_len;
|
|
|
|
void *buf;
|
|
|
|
|
2019-05-29 09:11:16 +00:00
|
|
|
_dif_sgl_advance(sgl, block_size * inject_offset_blocks);
|
2018-12-18 01:51:22 +00:00
|
|
|
|
|
|
|
offset_in_block = 0;
|
|
|
|
|
2019-01-10 11:52:53 +00:00
|
|
|
while (offset_in_block < block_size) {
|
2019-05-15 00:05:13 +00:00
|
|
|
_dif_sgl_get_buf(sgl, &buf, &buf_len);
|
2018-12-18 01:51:22 +00:00
|
|
|
buf_len = spdk_min(buf_len, block_size - offset_in_block);
|
|
|
|
|
|
|
|
if (inject_offset_bytes >= offset_in_block &&
|
|
|
|
inject_offset_bytes < offset_in_block + buf_len) {
|
|
|
|
buf += inject_offset_bytes - offset_in_block;
|
|
|
|
_bit_flip(buf, inject_offset_bits);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-15 00:05:13 +00:00
|
|
|
_dif_sgl_advance(sgl, buf_len);
|
2018-12-18 01:51:22 +00:00
|
|
|
offset_in_block += buf_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-05-15 00:05:13 +00:00
|
|
|
dif_inject_error(struct _dif_sgl *sgl, uint32_t block_size, uint32_t num_blocks,
|
2018-12-28 01:18:21 +00:00
|
|
|
uint32_t start_inject_bytes, uint32_t inject_range_bytes,
|
|
|
|
uint32_t *inject_offset)
|
2018-12-18 01:51:22 +00:00
|
|
|
{
|
|
|
|
uint32_t inject_offset_blocks, inject_offset_bytes, inject_offset_bits;
|
|
|
|
uint32_t offset_blocks;
|
2018-12-28 01:18:21 +00:00
|
|
|
int rc;
|
2018-12-18 01:51:22 +00:00
|
|
|
|
|
|
|
srand(time(0));
|
|
|
|
|
|
|
|
inject_offset_blocks = rand() % num_blocks;
|
|
|
|
inject_offset_bytes = start_inject_bytes + (rand() % inject_range_bytes);
|
2019-01-08 10:25:37 +00:00
|
|
|
inject_offset_bits = rand() % 8;
|
2018-12-18 01:51:22 +00:00
|
|
|
|
|
|
|
for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
|
|
|
|
if (offset_blocks == inject_offset_blocks) {
|
2019-05-15 00:05:13 +00:00
|
|
|
rc = _dif_inject_error(sgl, block_size, num_blocks,
|
2018-12-28 01:18:21 +00:00
|
|
|
inject_offset_blocks,
|
|
|
|
inject_offset_bytes,
|
|
|
|
inject_offset_bits);
|
|
|
|
if (rc == 0) {
|
|
|
|
*inject_offset = inject_offset_blocks;
|
|
|
|
}
|
|
|
|
return rc;
|
2018-12-18 01:51:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define _member_size(type, member) sizeof(((type *)0)->member)
|
|
|
|
|
|
|
|
int
|
2019-01-07 00:20:35 +00:00
|
|
|
spdk_dif_inject_error(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
|
2018-12-28 01:18:21 +00:00
|
|
|
const struct spdk_dif_ctx *ctx, uint32_t inject_flags,
|
|
|
|
uint32_t *inject_offset)
|
2018-12-18 01:51:22 +00:00
|
|
|
{
|
2019-05-15 00:05:13 +00:00
|
|
|
struct _dif_sgl sgl;
|
2018-12-18 01:51:22 +00:00
|
|
|
int rc;
|
|
|
|
|
2019-05-15 00:05:13 +00:00
|
|
|
_dif_sgl_init(&sgl, iovs, iovcnt);
|
|
|
|
|
2019-05-15 00:42:34 +00:00
|
|
|
if (!_dif_sgl_is_valid(&sgl, ctx->block_size * num_blocks)) {
|
2018-12-18 01:51:22 +00:00
|
|
|
SPDK_ERRLOG("Size of iovec array is not valid.\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inject_flags & SPDK_DIF_REFTAG_ERROR) {
|
2019-05-15 00:05:13 +00:00
|
|
|
rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
|
2019-01-07 00:20:35 +00:00
|
|
|
ctx->guard_interval + offsetof(struct spdk_dif, ref_tag),
|
2018-12-28 01:18:21 +00:00
|
|
|
_member_size(struct spdk_dif, ref_tag),
|
|
|
|
inject_offset);
|
2018-12-18 01:51:22 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("Failed to inject error to Reference Tag.\n");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inject_flags & SPDK_DIF_APPTAG_ERROR) {
|
2019-05-15 00:05:13 +00:00
|
|
|
rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
|
2019-01-07 00:20:35 +00:00
|
|
|
ctx->guard_interval + offsetof(struct spdk_dif, app_tag),
|
2018-12-28 01:18:21 +00:00
|
|
|
_member_size(struct spdk_dif, app_tag),
|
|
|
|
inject_offset);
|
2018-12-18 01:51:22 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("Failed to inject error to Application Tag.\n");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (inject_flags & SPDK_DIF_GUARD_ERROR) {
|
2019-05-15 00:05:13 +00:00
|
|
|
rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
|
2019-01-07 00:20:35 +00:00
|
|
|
ctx->guard_interval,
|
2018-12-28 01:18:21 +00:00
|
|
|
_member_size(struct spdk_dif, guard),
|
|
|
|
inject_offset);
|
2018-12-18 01:51:22 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("Failed to inject error to Guard.\n");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inject_flags & SPDK_DIF_DATA_ERROR) {
|
|
|
|
/* If the DIF information is contained within the last 8 bytes of
|
|
|
|
* metadata, then the CRC covers all metadata bytes up to but excluding
|
|
|
|
* the last 8 bytes. But error injection does not cover these metadata
|
|
|
|
* because classification is not determined yet.
|
2018-12-28 01:18:21 +00:00
|
|
|
*
|
|
|
|
* Note: Error injection to data block is expected to be detected as
|
|
|
|
* guard error.
|
2018-12-18 01:51:22 +00:00
|
|
|
*/
|
2019-05-15 00:05:13 +00:00
|
|
|
rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
|
2018-12-18 01:51:22 +00:00
|
|
|
0,
|
2018-12-28 01:18:21 +00:00
|
|
|
ctx->block_size - ctx->md_size,
|
|
|
|
inject_offset);
|
2018-12-18 01:51:22 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("Failed to inject error to data block.\n");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-01-08 23:07:08 +00:00
|
|
|
|
|
|
|
static void
|
2019-05-15 00:05:13 +00:00
|
|
|
dix_generate(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
|
2019-01-08 23:07:08 +00:00
|
|
|
uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
|
|
|
|
{
|
2019-05-15 00:05:13 +00:00
|
|
|
uint32_t offset_blocks = 0;
|
2019-01-08 23:07:08 +00:00
|
|
|
uint16_t guard;
|
|
|
|
void *data_buf, *md_buf;
|
|
|
|
|
2019-01-10 11:52:53 +00:00
|
|
|
while (offset_blocks < num_blocks) {
|
2019-05-15 00:05:13 +00:00
|
|
|
_dif_sgl_get_buf(data_sgl, &data_buf, NULL);
|
|
|
|
_dif_sgl_get_buf(md_sgl, &md_buf, NULL);
|
2019-01-08 23:07:08 +00:00
|
|
|
|
|
|
|
guard = 0;
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
2019-02-13 06:17:49 +00:00
|
|
|
guard = spdk_crc16_t10dif(ctx->guard_seed, data_buf, ctx->block_size);
|
2019-01-08 23:07:08 +00:00
|
|
|
guard = spdk_crc16_t10dif(guard, md_buf, ctx->guard_interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
_dif_generate(md_buf + ctx->guard_interval, guard, offset_blocks, ctx);
|
|
|
|
|
2019-05-15 00:05:13 +00:00
|
|
|
_dif_sgl_advance(data_sgl, ctx->block_size);
|
|
|
|
_dif_sgl_advance(md_sgl, ctx->md_size);
|
2019-01-08 23:07:08 +00:00
|
|
|
offset_blocks++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-05-14 23:19:27 +00:00
|
|
|
_dix_generate_split(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
|
2019-01-08 23:07:08 +00:00
|
|
|
uint32_t offset_blocks, const struct spdk_dif_ctx *ctx)
|
|
|
|
{
|
|
|
|
uint32_t offset_in_block, data_buf_len;
|
2019-02-13 06:17:49 +00:00
|
|
|
uint16_t guard = 0;
|
2019-01-08 23:07:08 +00:00
|
|
|
void *data_buf, *md_buf;
|
|
|
|
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_get_buf(md_sgl, &md_buf, NULL);
|
2019-01-08 23:07:08 +00:00
|
|
|
|
2019-02-13 06:17:49 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
|
|
|
guard = ctx->guard_seed;
|
|
|
|
}
|
2019-01-08 23:07:08 +00:00
|
|
|
offset_in_block = 0;
|
|
|
|
|
2019-01-10 11:52:53 +00:00
|
|
|
while (offset_in_block < ctx->block_size) {
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_get_buf(data_sgl, &data_buf, &data_buf_len);
|
2019-01-08 23:07:08 +00:00
|
|
|
data_buf_len = spdk_min(data_buf_len, ctx->block_size - offset_in_block);
|
|
|
|
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
2019-01-08 23:07:08 +00:00
|
|
|
guard = spdk_crc16_t10dif(guard, data_buf, data_buf_len);
|
|
|
|
}
|
|
|
|
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_advance(data_sgl, data_buf_len);
|
2019-01-08 23:07:08 +00:00
|
|
|
offset_in_block += data_buf_len;
|
|
|
|
}
|
|
|
|
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
2019-01-10 06:14:42 +00:00
|
|
|
guard = spdk_crc16_t10dif(guard, md_buf, ctx->guard_interval);
|
|
|
|
}
|
|
|
|
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_advance(md_sgl, ctx->md_size);
|
2019-01-08 23:07:08 +00:00
|
|
|
|
|
|
|
_dif_generate(md_buf + ctx->guard_interval, guard, offset_blocks, ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-05-15 00:05:13 +00:00
|
|
|
dix_generate_split(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
|
2019-01-08 23:07:08 +00:00
|
|
|
uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
|
|
|
|
{
|
2019-05-15 00:13:46 +00:00
|
|
|
uint32_t offset_blocks;
|
2019-01-08 23:07:08 +00:00
|
|
|
|
2019-05-15 00:13:46 +00:00
|
|
|
for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
|
2019-05-15 00:05:13 +00:00
|
|
|
_dix_generate_split(data_sgl, md_sgl, offset_blocks, ctx);
|
2019-01-08 23:07:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_dix_generate(struct iovec *iovs, int iovcnt, struct iovec *md_iov,
|
|
|
|
uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
|
|
|
|
{
|
2019-05-15 00:05:13 +00:00
|
|
|
struct _dif_sgl data_sgl, md_sgl;
|
|
|
|
|
|
|
|
_dif_sgl_init(&data_sgl, iovs, iovcnt);
|
|
|
|
_dif_sgl_init(&md_sgl, md_iov, 1);
|
|
|
|
|
2019-05-15 00:42:34 +00:00
|
|
|
if (!_dif_sgl_is_valid(&data_sgl, ctx->block_size * num_blocks) ||
|
|
|
|
!_dif_sgl_is_valid(&md_sgl, ctx->md_size * num_blocks)) {
|
2019-01-08 23:07:08 +00:00
|
|
|
SPDK_ERRLOG("Size of iovec array is not valid.\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_dif_is_disabled(ctx->dif_type)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-15 00:42:34 +00:00
|
|
|
if (_dif_sgl_is_bytes_multiple(&data_sgl, ctx->block_size)) {
|
2019-05-15 00:05:13 +00:00
|
|
|
dix_generate(&data_sgl, &md_sgl, num_blocks, ctx);
|
2019-01-08 23:07:08 +00:00
|
|
|
} else {
|
2019-05-15 00:05:13 +00:00
|
|
|
dix_generate_split(&data_sgl, &md_sgl, num_blocks, ctx);
|
2019-01-08 23:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-05-15 00:05:13 +00:00
|
|
|
dix_verify(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
|
2019-01-08 09:08:08 +00:00
|
|
|
uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
|
|
|
|
struct spdk_dif_error *err_blk)
|
2019-01-08 23:07:08 +00:00
|
|
|
{
|
2019-05-15 00:05:13 +00:00
|
|
|
uint32_t offset_blocks = 0;
|
2019-01-08 23:07:08 +00:00
|
|
|
uint16_t guard;
|
|
|
|
void *data_buf, *md_buf;
|
|
|
|
int rc;
|
|
|
|
|
2019-01-10 11:52:53 +00:00
|
|
|
while (offset_blocks < num_blocks) {
|
2019-05-15 00:05:13 +00:00
|
|
|
_dif_sgl_get_buf(data_sgl, &data_buf, NULL);
|
|
|
|
_dif_sgl_get_buf(md_sgl, &md_buf, NULL);
|
2019-01-08 23:07:08 +00:00
|
|
|
|
|
|
|
guard = 0;
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
2019-02-13 06:17:49 +00:00
|
|
|
guard = spdk_crc16_t10dif(ctx->guard_seed, data_buf, ctx->block_size);
|
2019-01-08 23:07:08 +00:00
|
|
|
guard = spdk_crc16_t10dif(guard, md_buf, ctx->guard_interval);
|
|
|
|
}
|
|
|
|
|
2019-01-08 09:08:08 +00:00
|
|
|
rc = _dif_verify(md_buf + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
|
2019-01-08 23:07:08 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-05-15 00:05:13 +00:00
|
|
|
_dif_sgl_advance(data_sgl, ctx->block_size);
|
|
|
|
_dif_sgl_advance(md_sgl, ctx->md_size);
|
2019-01-08 23:07:08 +00:00
|
|
|
offset_blocks++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-05-14 23:19:27 +00:00
|
|
|
_dix_verify_split(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
|
2019-01-08 09:08:08 +00:00
|
|
|
uint32_t offset_blocks, const struct spdk_dif_ctx *ctx,
|
|
|
|
struct spdk_dif_error *err_blk)
|
2019-01-08 23:07:08 +00:00
|
|
|
{
|
|
|
|
uint32_t offset_in_block, data_buf_len;
|
2019-02-13 06:17:49 +00:00
|
|
|
uint16_t guard = 0;
|
2019-01-08 23:07:08 +00:00
|
|
|
void *data_buf, *md_buf;
|
|
|
|
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_get_buf(md_sgl, &md_buf, NULL);
|
2019-01-08 23:07:08 +00:00
|
|
|
|
2019-02-13 06:17:49 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
|
|
|
guard = ctx->guard_seed;
|
|
|
|
}
|
2019-01-08 23:07:08 +00:00
|
|
|
offset_in_block = 0;
|
|
|
|
|
2019-01-10 11:52:53 +00:00
|
|
|
while (offset_in_block < ctx->block_size) {
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_get_buf(data_sgl, &data_buf, &data_buf_len);
|
2019-01-08 23:07:08 +00:00
|
|
|
data_buf_len = spdk_min(data_buf_len, ctx->block_size - offset_in_block);
|
|
|
|
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
2019-01-08 23:07:08 +00:00
|
|
|
guard = spdk_crc16_t10dif(guard, data_buf, data_buf_len);
|
|
|
|
}
|
|
|
|
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_advance(data_sgl, data_buf_len);
|
2019-01-08 23:07:08 +00:00
|
|
|
offset_in_block += data_buf_len;
|
|
|
|
}
|
|
|
|
|
2019-02-04 23:54:15 +00:00
|
|
|
if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
|
2019-01-10 06:14:42 +00:00
|
|
|
guard = spdk_crc16_t10dif(guard, md_buf, ctx->guard_interval);
|
|
|
|
}
|
|
|
|
|
2019-05-14 23:19:27 +00:00
|
|
|
_dif_sgl_advance(md_sgl, ctx->md_size);
|
2019-01-08 23:07:08 +00:00
|
|
|
|
2019-01-08 09:08:08 +00:00
|
|
|
return _dif_verify(md_buf + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
|
2019-01-08 23:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-05-15 00:05:13 +00:00
|
|
|
dix_verify_split(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
|
2019-01-08 09:08:08 +00:00
|
|
|
uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
|
|
|
|
struct spdk_dif_error *err_blk)
|
2019-01-08 23:07:08 +00:00
|
|
|
{
|
2019-05-15 00:13:46 +00:00
|
|
|
uint32_t offset_blocks;
|
2019-01-08 23:07:08 +00:00
|
|
|
int rc;
|
|
|
|
|
2019-05-15 00:13:46 +00:00
|
|
|
for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
|
2019-05-15 00:05:13 +00:00
|
|
|
rc = _dix_verify_split(data_sgl, md_sgl, offset_blocks, ctx, err_blk);
|
2019-01-08 23:07:08 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
spdk_dix_verify(struct iovec *iovs, int iovcnt, struct iovec *md_iov,
|
2019-01-08 09:08:08 +00:00
|
|
|
uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
|
|
|
|
struct spdk_dif_error *err_blk)
|
2019-01-08 23:07:08 +00:00
|
|
|
{
|
2019-05-15 00:05:13 +00:00
|
|
|
struct _dif_sgl data_sgl, md_sgl;
|
|
|
|
|
|
|
|
_dif_sgl_init(&data_sgl, iovs, iovcnt);
|
|
|
|
_dif_sgl_init(&md_sgl, md_iov, 1);
|
|
|
|
|
2019-05-15 00:42:34 +00:00
|
|
|
if (!_dif_sgl_is_valid(&data_sgl, ctx->block_size * num_blocks) ||
|
|
|
|
!_dif_sgl_is_valid(&md_sgl, ctx->md_size * num_blocks)) {
|
2019-01-08 23:07:08 +00:00
|
|
|
SPDK_ERRLOG("Size of iovec array is not valid.\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_dif_is_disabled(ctx->dif_type)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-15 00:42:34 +00:00
|
|
|
if (_dif_sgl_is_bytes_multiple(&data_sgl, ctx->block_size)) {
|
2019-05-15 00:05:13 +00:00
|
|
|
return dix_verify(&data_sgl, &md_sgl, num_blocks, ctx, err_blk);
|
2019-01-08 23:07:08 +00:00
|
|
|
} else {
|
2019-05-15 00:05:13 +00:00
|
|
|
return dix_verify_split(&data_sgl, &md_sgl, num_blocks, ctx, err_blk);
|
2019-01-08 23:07:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-08 09:08:08 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
spdk_dix_inject_error(struct iovec *iovs, int iovcnt, struct iovec *md_iov,
|
|
|
|
uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
|
|
|
|
uint32_t inject_flags, uint32_t *inject_offset)
|
|
|
|
{
|
2019-05-15 00:05:13 +00:00
|
|
|
struct _dif_sgl data_sgl, md_sgl;
|
2019-01-08 09:08:08 +00:00
|
|
|
int rc;
|
|
|
|
|
2019-05-15 00:05:13 +00:00
|
|
|
_dif_sgl_init(&data_sgl, iovs, iovcnt);
|
|
|
|
_dif_sgl_init(&md_sgl, md_iov, 1);
|
|
|
|
|
2019-05-15 00:42:34 +00:00
|
|
|
if (!_dif_sgl_is_valid(&data_sgl, ctx->block_size * num_blocks) ||
|
|
|
|
!_dif_sgl_is_valid(&md_sgl, ctx->md_size * num_blocks)) {
|
2019-01-08 09:08:08 +00:00
|
|
|
SPDK_ERRLOG("Size of iovec array is not valid.\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inject_flags & SPDK_DIF_REFTAG_ERROR) {
|
2019-05-15 00:05:13 +00:00
|
|
|
rc = dif_inject_error(&md_sgl, ctx->md_size, num_blocks,
|
2019-01-08 09:08:08 +00:00
|
|
|
ctx->guard_interval + offsetof(struct spdk_dif, ref_tag),
|
|
|
|
_member_size(struct spdk_dif, ref_tag),
|
|
|
|
inject_offset);
|
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("Failed to inject error to Reference Tag.\n");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inject_flags & SPDK_DIF_APPTAG_ERROR) {
|
2019-05-15 00:05:13 +00:00
|
|
|
rc = dif_inject_error(&md_sgl, ctx->md_size, num_blocks,
|
2019-01-08 09:08:08 +00:00
|
|
|
ctx->guard_interval + offsetof(struct spdk_dif, app_tag),
|
|
|
|
_member_size(struct spdk_dif, app_tag),
|
|
|
|
inject_offset);
|
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("Failed to inject error to Application Tag.\n");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inject_flags & SPDK_DIF_GUARD_ERROR) {
|
2019-05-15 00:05:13 +00:00
|
|
|
rc = dif_inject_error(&md_sgl, ctx->md_size, num_blocks,
|
2019-01-08 09:08:08 +00:00
|
|
|
ctx->guard_interval,
|
|
|
|
_member_size(struct spdk_dif, guard),
|
|
|
|
inject_offset);
|
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("Failed to inject error to Guard.\n");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inject_flags & SPDK_DIF_DATA_ERROR) {
|
|
|
|
/* Note: Error injection to data block is expected to be detected
|
|
|
|
* as guard error.
|
|
|
|
*/
|
2019-05-15 00:05:13 +00:00
|
|
|
rc = dif_inject_error(&data_sgl, ctx->block_size, num_blocks,
|
2019-01-08 09:08:08 +00:00
|
|
|
0,
|
|
|
|
ctx->block_size,
|
|
|
|
inject_offset);
|
|
|
|
if (rc != 0) {
|
|
|
|
SPDK_ERRLOG("Failed to inject error to Guard.\n");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-02-26 06:14:53 +00:00
|
|
|
|
2019-06-12 08:26:08 +00:00
|
|
|
static uint32_t
|
|
|
|
_to_next_boundary(uint32_t offset, uint32_t boundary)
|
|
|
|
{
|
|
|
|
return boundary - (offset % boundary);
|
|
|
|
}
|
|
|
|
|
2019-06-12 22:31:40 +00:00
|
|
|
int
|
|
|
|
spdk_dif_set_md_interleave_iovs(struct iovec *iovs, int iovcnt,
|
|
|
|
struct iovec *buf_iovs, int buf_iovcnt,
|
|
|
|
uint32_t data_offset, uint32_t data_len,
|
|
|
|
uint32_t *_mapped_len,
|
|
|
|
const struct spdk_dif_ctx *ctx)
|
2019-02-26 06:14:53 +00:00
|
|
|
{
|
2019-06-12 23:51:06 +00:00
|
|
|
uint32_t data_block_size, buf_len, buf_offset, len;
|
2019-06-12 22:31:40 +00:00
|
|
|
struct _dif_sgl dif_sgl;
|
|
|
|
struct _dif_sgl buf_sgl;
|
2019-02-26 06:14:53 +00:00
|
|
|
|
2019-06-12 22:31:40 +00:00
|
|
|
if (iovs == NULL || iovcnt == 0 || buf_iovs == NULL || buf_iovcnt == 0) {
|
|
|
|
return -EINVAL;
|
2019-02-26 06:14:53 +00:00
|
|
|
}
|
|
|
|
|
2019-06-12 22:31:40 +00:00
|
|
|
data_block_size = ctx->block_size - ctx->md_size;
|
2019-02-26 06:14:53 +00:00
|
|
|
|
2019-06-12 23:51:06 +00:00
|
|
|
buf_len = ((data_offset + data_len) / data_block_size) * ctx->block_size +
|
|
|
|
((data_offset + data_len) % data_block_size);
|
2019-05-08 22:07:39 +00:00
|
|
|
|
|
|
|
_dif_sgl_init(&dif_sgl, iovs, iovcnt);
|
|
|
|
_dif_sgl_init(&buf_sgl, buf_iovs, buf_iovcnt);
|
|
|
|
|
2019-06-12 23:51:06 +00:00
|
|
|
if (!_dif_sgl_is_valid(&buf_sgl, buf_len)) {
|
2019-05-08 22:07:39 +00:00
|
|
|
SPDK_ERRLOG("Buffer overflow will occur.\n");
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
2019-06-12 23:51:06 +00:00
|
|
|
buf_offset = (data_offset / data_block_size) * ctx->block_size +
|
|
|
|
(data_offset % data_block_size);
|
2019-05-08 22:07:39 +00:00
|
|
|
|
2019-06-12 23:51:06 +00:00
|
|
|
_dif_sgl_advance(&buf_sgl, buf_offset);
|
2019-05-08 22:07:39 +00:00
|
|
|
|
2019-06-12 08:26:08 +00:00
|
|
|
while (data_len != 0) {
|
|
|
|
len = spdk_min(data_len, _to_next_boundary(data_offset, data_block_size));
|
2019-05-08 22:07:39 +00:00
|
|
|
|
2019-06-12 08:26:08 +00:00
|
|
|
if (!_dif_sgl_append_split(&dif_sgl, &buf_sgl, len)) {
|
2019-06-12 22:46:42 +00:00
|
|
|
break;
|
2019-05-08 22:07:39 +00:00
|
|
|
}
|
2019-05-29 09:11:16 +00:00
|
|
|
_dif_sgl_advance(&buf_sgl, ctx->md_size);
|
2019-06-12 08:26:08 +00:00
|
|
|
data_offset += len;
|
|
|
|
data_len -= len;
|
2019-05-08 22:07:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_mapped_len != NULL) {
|
|
|
|
*_mapped_len = dif_sgl.total_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return iovcnt - dif_sgl.iovcnt;
|
|
|
|
}
|
|
|
|
|
2019-06-12 22:33:40 +00:00
|
|
|
int
|
|
|
|
spdk_dif_generate_stream(struct iovec *iovs, int iovcnt,
|
|
|
|
uint32_t data_offset, uint32_t data_len,
|
|
|
|
const struct spdk_dif_ctx *ctx)
|
2019-03-01 08:49:23 +00:00
|
|
|
{
|
|
|
|
uint32_t data_block_size, offset_blocks, num_blocks, i;
|
2019-06-12 22:33:40 +00:00
|
|
|
struct _dif_sgl sgl;
|
2019-03-01 08:49:23 +00:00
|
|
|
|
2019-06-12 22:33:40 +00:00
|
|
|
if (iovs == NULL || iovcnt == 0) {
|
|
|
|
return -EINVAL;
|
2019-03-01 08:49:23 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 01:22:55 +00:00
|
|
|
data_block_size = ctx->block_size - ctx->md_size;
|
|
|
|
|
2019-05-30 01:18:11 +00:00
|
|
|
offset_blocks = data_offset / data_block_size;
|
|
|
|
data_len += data_offset % data_block_size;
|
2019-05-09 01:22:55 +00:00
|
|
|
|
2019-05-30 01:18:11 +00:00
|
|
|
data_offset = offset_blocks * ctx->block_size;
|
|
|
|
num_blocks = data_len / data_block_size;
|
2019-05-09 01:22:55 +00:00
|
|
|
|
|
|
|
_dif_sgl_init(&sgl, iovs, iovcnt);
|
|
|
|
|
2019-05-30 01:18:11 +00:00
|
|
|
if (!_dif_sgl_is_valid(&sgl, data_offset + num_blocks * ctx->block_size)) {
|
2019-05-09 01:22:55 +00:00
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
2019-05-30 01:18:11 +00:00
|
|
|
_dif_sgl_advance(&sgl, data_offset);
|
2019-05-09 01:22:55 +00:00
|
|
|
|
|
|
|
for (i = 0; i < num_blocks; i++) {
|
|
|
|
_dif_generate_split(&sgl, offset_blocks + i, ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|