xref: /spdk/lib/util/dif.c (revision 4586880f596e61c5a599d0766bb47c004bbd2dd6)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2022 Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 #include "spdk/dif.h"
7 #include "spdk/crc16.h"
8 #include "spdk/crc32.h"
9 #include "spdk/crc64.h"
10 #include "spdk/endian.h"
11 #include "spdk/log.h"
12 #include "spdk/util.h"
13 
14 #define REFTAG_MASK_16 0x00000000FFFFFFFF
15 #define REFTAG_MASK_32 0xFFFFFFFFFFFFFFFF
16 #define REFTAG_MASK_64 0x0000FFFFFFFFFFFF
17 
18 /* The variable size Storage Tag and Reference Tag is not supported yet,
19  * so the maximum size of the Reference Tag is assumed.
20  */
21 struct spdk_dif {
22 	union {
23 		struct {
24 			uint16_t guard;
25 			uint16_t app_tag;
26 			uint32_t stor_ref_space;
27 		} g16;
28 		struct {
29 			uint32_t guard;
30 			uint16_t app_tag;
31 			uint16_t stor_ref_space_p1;
32 			uint64_t stor_ref_space_p2;
33 		} g32;
34 		struct {
35 			uint64_t guard;
36 			uint16_t app_tag;
37 			uint16_t stor_ref_space_p1;
38 			uint32_t stor_ref_space_p2;
39 		} g64;
40 	};
41 };
42 SPDK_STATIC_ASSERT(SPDK_SIZEOF_MEMBER(struct spdk_dif, g16) == 8, "Incorrect size");
43 SPDK_STATIC_ASSERT(SPDK_SIZEOF_MEMBER(struct spdk_dif, g32) == 16, "Incorrect size");
44 SPDK_STATIC_ASSERT(SPDK_SIZEOF_MEMBER(struct spdk_dif, g64) == 16, "Incorrect size");
45 
46 /* Context to iterate or create a iovec array.
47  * Each sgl is either iterated or created at a time.
48  */
49 struct _dif_sgl {
50 	/* Current iovec in the iteration or creation */
51 	struct iovec *iov;
52 
53 	/* Remaining count of iovecs in the iteration or creation. */
54 	int iovcnt;
55 
56 	/* Current offset in the iovec */
57 	uint32_t iov_offset;
58 
59 	/* Size of the created iovec array in bytes */
60 	uint32_t total_size;
61 };
62 
63 static inline void
64 _dif_sgl_init(struct _dif_sgl *s, struct iovec *iovs, int iovcnt)
65 {
66 	s->iov = iovs;
67 	s->iovcnt = iovcnt;
68 	s->iov_offset = 0;
69 	s->total_size = 0;
70 }
71 
72 static void
73 _dif_sgl_advance(struct _dif_sgl *s, uint32_t step)
74 {
75 	s->iov_offset += step;
76 	while (s->iovcnt != 0) {
77 		if (s->iov_offset < s->iov->iov_len) {
78 			break;
79 		}
80 
81 		s->iov_offset -= s->iov->iov_len;
82 		s->iov++;
83 		s->iovcnt--;
84 	}
85 }
86 
87 static inline void
88 _dif_sgl_get_buf(struct _dif_sgl *s, uint8_t **_buf, uint32_t *_buf_len)
89 {
90 	if (_buf != NULL) {
91 		*_buf = (uint8_t *)s->iov->iov_base + s->iov_offset;
92 	}
93 	if (_buf_len != NULL) {
94 		*_buf_len = s->iov->iov_len - s->iov_offset;
95 	}
96 }
97 
98 static inline bool
99 _dif_sgl_append(struct _dif_sgl *s, uint8_t *data, uint32_t data_len)
100 {
101 	assert(s->iovcnt > 0);
102 	s->iov->iov_base = data;
103 	s->iov->iov_len = data_len;
104 	s->total_size += data_len;
105 	s->iov++;
106 	s->iovcnt--;
107 
108 	if (s->iovcnt > 0) {
109 		return true;
110 	} else {
111 		return false;
112 	}
113 }
114 
115 static inline bool
116 _dif_sgl_append_split(struct _dif_sgl *dst, struct _dif_sgl *src, uint32_t data_len)
117 {
118 	uint8_t *buf;
119 	uint32_t buf_len;
120 
121 	while (data_len != 0) {
122 		_dif_sgl_get_buf(src, &buf, &buf_len);
123 		buf_len = spdk_min(buf_len, data_len);
124 
125 		if (!_dif_sgl_append(dst, buf, buf_len)) {
126 			return false;
127 		}
128 
129 		_dif_sgl_advance(src, buf_len);
130 		data_len -= buf_len;
131 	}
132 
133 	return true;
134 }
135 
136 /* This function must be used before starting iteration. */
137 static bool
138 _dif_sgl_is_bytes_multiple(struct _dif_sgl *s, uint32_t bytes)
139 {
140 	int i;
141 
142 	for (i = 0; i < s->iovcnt; i++) {
143 		if (s->iov[i].iov_len % bytes) {
144 			return false;
145 		}
146 	}
147 
148 	return true;
149 }
150 
151 /* This function must be used before starting iteration. */
152 static bool
153 _dif_sgl_is_valid(struct _dif_sgl *s, uint32_t bytes)
154 {
155 	uint64_t total = 0;
156 	int i;
157 
158 	for (i = 0; i < s->iovcnt; i++) {
159 		total += s->iov[i].iov_len;
160 	}
161 
162 	return total >= bytes;
163 }
164 
165 static void
166 _dif_sgl_copy(struct _dif_sgl *to, struct _dif_sgl *from)
167 {
168 	memcpy(to, from, sizeof(struct _dif_sgl));
169 }
170 
171 static bool
172 _dif_is_disabled(enum spdk_dif_type dif_type)
173 {
174 	if (dif_type == SPDK_DIF_DISABLE) {
175 		return true;
176 	} else {
177 		return false;
178 	}
179 }
180 
181 static inline size_t
182 _dif_size(enum spdk_dif_pi_format dif_pi_format)
183 {
184 	uint8_t size;
185 
186 	if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
187 		size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g16);
188 	} else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
189 		size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g32);
190 	} else {
191 		size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g64);
192 	}
193 
194 	return size;
195 }
196 
197 static uint32_t
198 _get_guard_interval(uint32_t block_size, uint32_t md_size, bool dif_loc, bool md_interleave,
199 		    size_t dif_size)
200 {
201 	if (!dif_loc) {
202 		/* For metadata formats with more than 8/16 bytes (depending on
203 		 * the PI format), if the DIF is contained in the last 8/16 bytes
204 		 * of metadata, then the CRC covers all metadata up to but excluding
205 		 * these last 8/16 bytes.
206 		 */
207 		if (md_interleave) {
208 			return block_size - dif_size;
209 		} else {
210 			return md_size - dif_size;
211 		}
212 	} else {
213 		/* For metadata formats with more than 8/16 bytes (depending on
214 		 * the PI format), if the DIF is contained in the first 8/16 bytes
215 		 * of metadata, then the CRC does not cover any metadata.
216 		 */
217 		if (md_interleave) {
218 			return block_size - md_size;
219 		} else {
220 			return 0;
221 		}
222 	}
223 }
224 
225 static inline uint8_t
226 _dif_guard_size(enum spdk_dif_pi_format dif_pi_format)
227 {
228 	uint8_t size;
229 
230 	if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
231 		size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g16.guard);
232 	} else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
233 		size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g32.guard);
234 	} else {
235 		size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g64.guard);
236 	}
237 
238 	return size;
239 }
240 
241 static inline void
242 _dif_set_guard(struct spdk_dif *dif, uint64_t guard, enum spdk_dif_pi_format dif_pi_format)
243 {
244 	if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
245 		to_be16(&(dif->g16.guard), (uint16_t)guard);
246 	} else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
247 		to_be32(&(dif->g32.guard), (uint32_t)guard);
248 	} else {
249 		to_be64(&(dif->g64.guard), guard);
250 	}
251 }
252 
253 static inline uint64_t
254 _dif_get_guard(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
255 {
256 	uint64_t guard;
257 
258 	if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
259 		guard = (uint64_t)from_be16(&(dif->g16.guard));
260 	} else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
261 		guard = (uint64_t)from_be32(&(dif->g32.guard));
262 	} else {
263 		guard = from_be64(&(dif->g64.guard));
264 	}
265 
266 	return guard;
267 }
268 
269 static inline uint64_t
270 _dif_generate_guard(uint64_t guard_seed, void *buf, size_t buf_len,
271 		    enum spdk_dif_pi_format dif_pi_format)
272 {
273 	uint64_t guard;
274 
275 	if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
276 		guard = (uint64_t)spdk_crc16_t10dif((uint16_t)guard_seed, buf, buf_len);
277 	} else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
278 		guard = (uint64_t)spdk_crc32c_nvme(buf, buf_len, guard_seed);
279 	} else {
280 		guard = spdk_crc64_nvme(buf, buf_len, guard_seed);
281 	}
282 
283 	return guard;
284 }
285 
286 static uint64_t
287 dif_generate_guard_split(uint64_t guard_seed, struct _dif_sgl *sgl, uint32_t start,
288 			 uint32_t len, const struct spdk_dif_ctx *ctx)
289 {
290 	uint64_t guard = guard_seed;
291 	uint32_t offset, end, buf_len;
292 	uint8_t *buf;
293 
294 	offset = start;
295 	end = start + spdk_min(len, ctx->guard_interval - start);
296 
297 	while (offset < end) {
298 		_dif_sgl_get_buf(sgl, &buf, &buf_len);
299 		buf_len = spdk_min(buf_len, end - offset);
300 
301 		if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
302 			guard = _dif_generate_guard(guard, buf, buf_len, ctx->dif_pi_format);
303 		}
304 
305 		_dif_sgl_advance(sgl, buf_len);
306 		offset += buf_len;
307 	}
308 
309 	return guard;
310 }
311 
312 static inline uint64_t
313 _dif_generate_guard_copy(uint64_t guard_seed, void *dst, void *src, size_t buf_len,
314 			 enum spdk_dif_pi_format dif_pi_format)
315 {
316 	uint64_t guard;
317 
318 	if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
319 		guard = (uint64_t)spdk_crc16_t10dif_copy((uint16_t)guard_seed, dst, src, buf_len);
320 	} else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
321 		memcpy(dst, src, buf_len);
322 		guard = (uint64_t)spdk_crc32c_nvme(src, buf_len, guard_seed);
323 	} else {
324 		memcpy(dst, src, buf_len);
325 		guard = spdk_crc64_nvme(src, buf_len, guard_seed);
326 	}
327 
328 	return guard;
329 }
330 
331 static uint64_t
332 _dif_generate_guard_copy_split(uint64_t guard, struct _dif_sgl *dst_sgl,
333 			       struct _dif_sgl *src_sgl, uint32_t data_len,
334 			       enum spdk_dif_pi_format dif_pi_format)
335 {
336 	uint32_t offset = 0, src_len, dst_len, buf_len;
337 	uint8_t *src, *dst;
338 
339 	while (offset < data_len) {
340 		_dif_sgl_get_buf(src_sgl, &src, &src_len);
341 		_dif_sgl_get_buf(dst_sgl, &dst, &dst_len);
342 		buf_len = spdk_min(src_len, dst_len);
343 		buf_len = spdk_min(buf_len, data_len - offset);
344 
345 		guard = _dif_generate_guard_copy(guard, dst, src, buf_len, dif_pi_format);
346 
347 		_dif_sgl_advance(src_sgl, buf_len);
348 		_dif_sgl_advance(dst_sgl, buf_len);
349 		offset += buf_len;
350 	}
351 
352 	return guard;
353 }
354 
355 static void
356 _data_copy_split(struct _dif_sgl *dst_sgl, struct _dif_sgl *src_sgl, uint32_t data_len)
357 {
358 	uint32_t offset = 0, src_len, dst_len, buf_len;
359 	uint8_t *src, *dst;
360 
361 	while (offset < data_len) {
362 		_dif_sgl_get_buf(src_sgl, &src, &src_len);
363 		_dif_sgl_get_buf(dst_sgl, &dst, &dst_len);
364 		buf_len = spdk_min(src_len, dst_len);
365 		buf_len = spdk_min(buf_len, data_len - offset);
366 
367 		memcpy(dst, src, buf_len);
368 
369 		_dif_sgl_advance(src_sgl, buf_len);
370 		_dif_sgl_advance(dst_sgl, buf_len);
371 		offset += buf_len;
372 	}
373 }
374 
375 static inline uint8_t
376 _dif_apptag_offset(enum spdk_dif_pi_format dif_pi_format)
377 {
378 	return _dif_guard_size(dif_pi_format);
379 }
380 
381 static inline uint8_t
382 _dif_apptag_size(void)
383 {
384 	return SPDK_SIZEOF_MEMBER(struct spdk_dif, g16.app_tag);
385 }
386 
387 static inline void
388 _dif_set_apptag(struct spdk_dif *dif, uint16_t app_tag, enum spdk_dif_pi_format dif_pi_format)
389 {
390 	if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
391 		to_be16(&(dif->g16.app_tag), app_tag);
392 	} else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
393 		to_be16(&(dif->g32.app_tag), app_tag);
394 	} else {
395 		to_be16(&(dif->g64.app_tag), app_tag);
396 	}
397 }
398 
399 static inline uint16_t
400 _dif_get_apptag(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
401 {
402 	uint16_t app_tag;
403 
404 	if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
405 		app_tag = from_be16(&(dif->g16.app_tag));
406 	} else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
407 		app_tag = from_be16(&(dif->g32.app_tag));
408 	} else {
409 		app_tag = from_be16(&(dif->g64.app_tag));
410 	}
411 
412 	return app_tag;
413 }
414 
415 static inline bool
416 _dif_apptag_ignore(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
417 {
418 	return _dif_get_apptag(dif, dif_pi_format) == SPDK_DIF_APPTAG_IGNORE;
419 }
420 
421 static inline uint8_t
422 _dif_reftag_offset(enum spdk_dif_pi_format dif_pi_format)
423 {
424 	uint8_t offset;
425 
426 	if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
427 		offset = _dif_apptag_offset(dif_pi_format) + _dif_apptag_size();
428 	} else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
429 		offset = _dif_apptag_offset(dif_pi_format) + _dif_apptag_size()
430 			 + SPDK_SIZEOF_MEMBER(struct spdk_dif, g32.stor_ref_space_p1);
431 	} else {
432 		offset = _dif_apptag_offset(dif_pi_format) + _dif_apptag_size();
433 	}
434 
435 	return offset;
436 }
437 
438 static inline uint8_t
439 _dif_reftag_size(enum spdk_dif_pi_format dif_pi_format)
440 {
441 	uint8_t size;
442 
443 	if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
444 		size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g16.stor_ref_space);
445 	} else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
446 		size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g32.stor_ref_space_p2);
447 	} else {
448 		size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g64.stor_ref_space_p1) +
449 		       SPDK_SIZEOF_MEMBER(struct spdk_dif, g64.stor_ref_space_p2);
450 	}
451 
452 	return size;
453 }
454 
455 static inline void
456 _dif_set_reftag(struct spdk_dif *dif, uint64_t ref_tag, enum spdk_dif_pi_format dif_pi_format)
457 {
458 	if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
459 		to_be32(&(dif->g16.stor_ref_space), (uint32_t)ref_tag);
460 	} else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
461 		to_be64(&(dif->g32.stor_ref_space_p2), ref_tag);
462 	} else {
463 		to_be16(&(dif->g64.stor_ref_space_p1), (uint16_t)(ref_tag >> 32));
464 		to_be32(&(dif->g64.stor_ref_space_p2), (uint32_t)ref_tag);
465 	}
466 }
467 
468 static inline uint64_t
469 _dif_get_reftag(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
470 {
471 	uint64_t ref_tag;
472 
473 	if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
474 		ref_tag = (uint64_t)from_be32(&(dif->g16.stor_ref_space));
475 	} else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
476 		ref_tag = from_be64(&(dif->g32.stor_ref_space_p2));
477 	} else {
478 		ref_tag = (uint64_t)from_be16(&(dif->g64.stor_ref_space_p1));
479 		ref_tag <<= 32;
480 		ref_tag |= (uint64_t)from_be32(&(dif->g64.stor_ref_space_p2));
481 	}
482 
483 	return ref_tag;
484 }
485 
486 static inline bool
487 _dif_reftag_match(struct spdk_dif *dif, uint64_t ref_tag,
488 		  enum spdk_dif_pi_format dif_pi_format)
489 {
490 	uint64_t _ref_tag;
491 	bool match;
492 
493 	_ref_tag = _dif_get_reftag(dif, dif_pi_format);
494 
495 	if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
496 		match = (_ref_tag == (ref_tag & REFTAG_MASK_16));
497 	} else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
498 		match = (_ref_tag == ref_tag);
499 	} else {
500 		match = (_ref_tag == (ref_tag & REFTAG_MASK_64));
501 	}
502 
503 	return match;
504 }
505 
506 static inline bool
507 _dif_reftag_ignore(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
508 {
509 	return _dif_reftag_match(dif, REFTAG_MASK_32, dif_pi_format);
510 }
511 
512 static bool
513 _dif_ignore(struct spdk_dif *dif, const struct spdk_dif_ctx *ctx)
514 {
515 	switch (ctx->dif_type) {
516 	case SPDK_DIF_TYPE1:
517 	case SPDK_DIF_TYPE2:
518 		/* If Type 1 or 2 is used, then all DIF checks are disabled when
519 		 * the Application Tag is 0xFFFF.
520 		 */
521 		if (_dif_apptag_ignore(dif, ctx->dif_pi_format)) {
522 			return true;
523 		}
524 		break;
525 	case SPDK_DIF_TYPE3:
526 		/* If Type 3 is used, then all DIF checks are disabled when the
527 		 * Application Tag is 0xFFFF and the Reference Tag is 0xFFFFFFFF
528 		 * or 0xFFFFFFFFFFFFFFFF depending on the PI format.
529 		 */
530 
531 		if (_dif_apptag_ignore(dif, ctx->dif_pi_format) &&
532 		    _dif_reftag_ignore(dif, ctx->dif_pi_format)) {
533 			return true;
534 		}
535 		break;
536 	default:
537 		break;
538 	}
539 
540 	return false;
541 }
542 
543 static bool
544 _dif_pi_format_is_valid(enum spdk_dif_pi_format dif_pi_format)
545 {
546 	switch (dif_pi_format) {
547 	case SPDK_DIF_PI_FORMAT_16:
548 	case SPDK_DIF_PI_FORMAT_32:
549 	case SPDK_DIF_PI_FORMAT_64:
550 		return true;
551 	default:
552 		return false;
553 	}
554 }
555 
556 static bool
557 _dif_type_is_valid(enum spdk_dif_type dif_type)
558 {
559 	switch (dif_type) {
560 	case SPDK_DIF_DISABLE:
561 	case SPDK_DIF_TYPE1:
562 	case SPDK_DIF_TYPE2:
563 	case SPDK_DIF_TYPE3:
564 		return true;
565 	default:
566 		return false;
567 	}
568 }
569 
570 int
571 spdk_dif_ctx_init(struct spdk_dif_ctx *ctx, uint32_t block_size, uint32_t md_size,
572 		  bool md_interleave, bool dif_loc, enum spdk_dif_type dif_type, uint32_t dif_flags,
573 		  uint32_t init_ref_tag, uint16_t apptag_mask, uint16_t app_tag,
574 		  uint32_t data_offset, uint64_t guard_seed, struct spdk_dif_ctx_init_ext_opts *opts)
575 {
576 	uint32_t data_block_size;
577 	enum spdk_dif_pi_format dif_pi_format = SPDK_DIF_PI_FORMAT_16;
578 
579 	if (opts != NULL) {
580 		if (!_dif_pi_format_is_valid(opts->dif_pi_format)) {
581 			SPDK_ERRLOG("No valid DIF PI format provided.\n");
582 			return -EINVAL;
583 		}
584 
585 		dif_pi_format = opts->dif_pi_format;
586 	}
587 
588 	if (!_dif_type_is_valid(dif_type)) {
589 		SPDK_ERRLOG("No valid DIF type was provided.\n");
590 		return -EINVAL;
591 	}
592 
593 	if (md_size < _dif_size(dif_pi_format)) {
594 		SPDK_ERRLOG("Metadata size is smaller than DIF size.\n");
595 		return -EINVAL;
596 	}
597 
598 	if (md_interleave) {
599 		if (block_size < md_size) {
600 			SPDK_ERRLOG("Block size is smaller than DIF size.\n");
601 			return -EINVAL;
602 		}
603 		data_block_size = block_size - md_size;
604 	} else {
605 		data_block_size = block_size;
606 	}
607 
608 	if (data_block_size == 0) {
609 		SPDK_ERRLOG("Zero data block size is not allowed\n");
610 		return -EINVAL;
611 	}
612 
613 	if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
614 		if ((data_block_size % 512) != 0) {
615 			SPDK_ERRLOG("Data block size should be a multiple of 512B\n");
616 			return -EINVAL;
617 		}
618 	} else {
619 		if ((data_block_size % 4096) != 0) {
620 			SPDK_ERRLOG("Data block size should be a multiple of 4kB\n");
621 			return -EINVAL;
622 		}
623 	}
624 
625 	ctx->block_size = block_size;
626 	ctx->md_size = md_size;
627 	ctx->md_interleave = md_interleave;
628 	ctx->dif_pi_format = dif_pi_format;
629 	ctx->guard_interval = _get_guard_interval(block_size, md_size, dif_loc, md_interleave,
630 			      _dif_size(ctx->dif_pi_format));
631 	ctx->dif_type = dif_type;
632 	ctx->dif_flags = dif_flags;
633 	ctx->init_ref_tag = init_ref_tag;
634 	ctx->apptag_mask = apptag_mask;
635 	ctx->app_tag = app_tag;
636 	ctx->data_offset = data_offset;
637 	ctx->ref_tag_offset = data_offset / data_block_size;
638 	ctx->last_guard = guard_seed;
639 	ctx->guard_seed = guard_seed;
640 	ctx->remapped_init_ref_tag = 0;
641 
642 	return 0;
643 }
644 
645 void
646 spdk_dif_ctx_set_data_offset(struct spdk_dif_ctx *ctx, uint32_t data_offset)
647 {
648 	uint32_t data_block_size;
649 
650 	if (ctx->md_interleave) {
651 		data_block_size = ctx->block_size - ctx->md_size;
652 	} else {
653 		data_block_size = ctx->block_size;
654 	}
655 
656 	ctx->data_offset = data_offset;
657 	ctx->ref_tag_offset = data_offset / data_block_size;
658 }
659 
660 void
661 spdk_dif_ctx_set_remapped_init_ref_tag(struct spdk_dif_ctx *ctx,
662 				       uint32_t remapped_init_ref_tag)
663 {
664 	ctx->remapped_init_ref_tag = remapped_init_ref_tag;
665 }
666 
667 static void
668 _dif_generate(void *_dif, uint64_t guard, uint32_t offset_blocks,
669 	      const struct spdk_dif_ctx *ctx)
670 {
671 	struct spdk_dif *dif = _dif;
672 	uint64_t ref_tag;
673 
674 	if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
675 		_dif_set_guard(dif, guard, ctx->dif_pi_format);
676 	}
677 
678 	if (ctx->dif_flags & SPDK_DIF_FLAGS_APPTAG_CHECK) {
679 		_dif_set_apptag(dif, ctx->app_tag, ctx->dif_pi_format);
680 	}
681 
682 	if (ctx->dif_flags & SPDK_DIF_FLAGS_REFTAG_CHECK) {
683 		/* For type 1 and 2, the reference tag is incremented for each
684 		 * subsequent logical block. For type 3, the reference tag
685 		 * remains the same as the initial reference tag.
686 		 */
687 		if (ctx->dif_type != SPDK_DIF_TYPE3) {
688 			ref_tag = ctx->init_ref_tag + ctx->ref_tag_offset + offset_blocks;
689 		} else {
690 			ref_tag = ctx->init_ref_tag + ctx->ref_tag_offset;
691 		}
692 
693 		/* Overwrite reference tag if initialization reference tag is SPDK_DIF_REFTAG_IGNORE */
694 		if (ctx->init_ref_tag == SPDK_DIF_REFTAG_IGNORE) {
695 			if (ctx->dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
696 				ref_tag = REFTAG_MASK_16;
697 			} else if (ctx->dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
698 				ref_tag = REFTAG_MASK_32;
699 			} else {
700 				ref_tag = REFTAG_MASK_64;
701 			}
702 		}
703 
704 		_dif_set_reftag(dif, ref_tag, ctx->dif_pi_format);
705 	}
706 }
707 
708 static void
709 dif_generate(struct _dif_sgl *sgl, uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
710 {
711 	uint32_t offset_blocks;
712 	uint8_t *buf;
713 	uint64_t guard = 0;
714 
715 	for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
716 		_dif_sgl_get_buf(sgl, &buf, NULL);
717 
718 		if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
719 			guard = _dif_generate_guard(ctx->guard_seed, buf, ctx->guard_interval, ctx->dif_pi_format);
720 		}
721 
722 		_dif_generate(buf + ctx->guard_interval, guard, offset_blocks, ctx);
723 
724 		_dif_sgl_advance(sgl, ctx->block_size);
725 	}
726 }
727 
728 static void
729 dif_store_split(struct _dif_sgl *sgl, struct spdk_dif *dif,
730 		const struct spdk_dif_ctx *ctx)
731 {
732 	uint32_t offset = 0, rest_md_len, buf_len;
733 	uint8_t *buf;
734 
735 	rest_md_len = ctx->block_size - ctx->guard_interval;
736 
737 	while (offset < rest_md_len) {
738 		_dif_sgl_get_buf(sgl, &buf, &buf_len);
739 
740 		if (offset < _dif_size(ctx->dif_pi_format)) {
741 			buf_len = spdk_min(buf_len, _dif_size(ctx->dif_pi_format) - offset);
742 			memcpy(buf, (uint8_t *)dif + offset, buf_len);
743 		} else {
744 			buf_len = spdk_min(buf_len, rest_md_len - offset);
745 		}
746 
747 		_dif_sgl_advance(sgl, buf_len);
748 		offset += buf_len;
749 	}
750 }
751 
752 static uint64_t
753 _dif_generate_split(struct _dif_sgl *sgl, uint32_t offset_in_block, uint32_t data_len,
754 		    uint64_t guard, uint32_t offset_blocks, const struct spdk_dif_ctx *ctx)
755 {
756 	struct spdk_dif dif = {};
757 
758 	assert(offset_in_block < ctx->guard_interval);
759 	assert(offset_in_block + data_len < ctx->guard_interval ||
760 	       offset_in_block + data_len == ctx->block_size);
761 
762 	/* Compute CRC over split logical block data. */
763 	guard = dif_generate_guard_split(guard, sgl, offset_in_block, data_len, ctx);
764 
765 	if (offset_in_block + data_len < ctx->guard_interval) {
766 		return guard;
767 	}
768 
769 	/* If a whole logical block data is parsed, generate DIF
770 	 * and save it to the temporary DIF area.
771 	 */
772 	_dif_generate(&dif, guard, offset_blocks, ctx);
773 
774 	/* Copy generated DIF field to the split DIF field, and then
775 	 * skip metadata field after DIF field (if any).
776 	 */
777 	dif_store_split(sgl, &dif, ctx);
778 
779 	if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
780 		guard = ctx->guard_seed;
781 	}
782 
783 	return guard;
784 }
785 
786 static void
787 dif_generate_split(struct _dif_sgl *sgl, uint32_t num_blocks,
788 		   const struct spdk_dif_ctx *ctx)
789 {
790 	uint32_t offset_blocks;
791 	uint64_t guard = 0;
792 
793 	if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
794 		guard = ctx->guard_seed;
795 	}
796 
797 	for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
798 		_dif_generate_split(sgl, 0, ctx->block_size, guard, offset_blocks, ctx);
799 	}
800 }
801 
802 int
803 spdk_dif_generate(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
804 		  const struct spdk_dif_ctx *ctx)
805 {
806 	struct _dif_sgl sgl;
807 
808 	_dif_sgl_init(&sgl, iovs, iovcnt);
809 
810 	if (!_dif_sgl_is_valid(&sgl, ctx->block_size * num_blocks)) {
811 		SPDK_ERRLOG("Size of iovec array is not valid.\n");
812 		return -EINVAL;
813 	}
814 
815 	if (_dif_is_disabled(ctx->dif_type)) {
816 		return 0;
817 	}
818 
819 	if (_dif_sgl_is_bytes_multiple(&sgl, ctx->block_size)) {
820 		dif_generate(&sgl, num_blocks, ctx);
821 	} else {
822 		dif_generate_split(&sgl, num_blocks, ctx);
823 	}
824 
825 	return 0;
826 }
827 
828 static void
829 _dif_error_set(struct spdk_dif_error *err_blk, uint8_t err_type,
830 	       uint64_t expected, uint64_t actual, uint32_t err_offset)
831 {
832 	if (err_blk) {
833 		err_blk->err_type = err_type;
834 		err_blk->expected = expected;
835 		err_blk->actual = actual;
836 		err_blk->err_offset = err_offset;
837 	}
838 }
839 
840 static bool
841 _dif_reftag_check(struct spdk_dif *dif, const struct spdk_dif_ctx *ctx,
842 		  uint64_t expected_reftag, uint32_t offset_blocks, struct spdk_dif_error *err_blk)
843 {
844 	uint64_t reftag;
845 
846 	if (ctx->dif_flags & SPDK_DIF_FLAGS_REFTAG_CHECK) {
847 		switch (ctx->dif_type) {
848 		case SPDK_DIF_TYPE1:
849 		case SPDK_DIF_TYPE2:
850 			/* Compare the DIF Reference Tag field to the passed Reference Tag.
851 			 * The passed Reference Tag will be the least significant 4 bytes
852 			 * or 8 bytes (depending on the PI format)
853 			 * of the LBA when Type 1 is used, and application specific value
854 			 * if Type 2 is used.
855 			 */
856 			if (!_dif_reftag_match(dif, expected_reftag, ctx->dif_pi_format)) {
857 				reftag = _dif_get_reftag(dif, ctx->dif_pi_format);
858 				_dif_error_set(err_blk, SPDK_DIF_REFTAG_ERROR, expected_reftag,
859 					       reftag, offset_blocks);
860 				SPDK_ERRLOG("Failed to compare Ref Tag: LBA=%" PRIu64 "," \
861 					    " Expected=%lx, Actual=%lx\n",
862 					    expected_reftag, expected_reftag, reftag);
863 				return false;
864 			}
865 			break;
866 		case SPDK_DIF_TYPE3:
867 			/* For Type 3, computed Reference Tag remains unchanged.
868 			 * Hence ignore the Reference Tag field.
869 			 */
870 			break;
871 		default:
872 			break;
873 		}
874 	}
875 
876 	return true;
877 }
878 
879 static int
880 _dif_verify(void *_dif, uint64_t guard, uint32_t offset_blocks,
881 	    const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk)
882 {
883 	struct spdk_dif *dif = _dif;
884 	uint64_t _guard;
885 	uint16_t _app_tag;
886 	uint64_t ref_tag;
887 
888 	if (_dif_ignore(dif, ctx)) {
889 		return 0;
890 	}
891 
892 	/* For type 1 and 2, the reference tag is incremented for each
893 	 * subsequent logical block. For type 3, the reference tag
894 	 * remains the same as the initial reference tag.
895 	 */
896 	if (ctx->dif_type != SPDK_DIF_TYPE3) {
897 		ref_tag = ctx->init_ref_tag + ctx->ref_tag_offset + offset_blocks;
898 	} else {
899 		ref_tag = ctx->init_ref_tag + ctx->ref_tag_offset;
900 	}
901 
902 	if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
903 		/* Compare the DIF Guard field to the CRC computed over the logical
904 		 * block data.
905 		 */
906 		_guard = _dif_get_guard(dif, ctx->dif_pi_format);
907 		if (_guard != guard) {
908 			_dif_error_set(err_blk, SPDK_DIF_GUARD_ERROR, _guard, guard,
909 				       offset_blocks);
910 			SPDK_ERRLOG("Failed to compare Guard: LBA=%" PRIu64 "," \
911 				    "  Expected=%lx, Actual=%lx\n",
912 				    ref_tag, _guard, guard);
913 			return -1;
914 		}
915 	}
916 
917 	if (ctx->dif_flags & SPDK_DIF_FLAGS_APPTAG_CHECK) {
918 		/* Compare unmasked bits in the DIF Application Tag field to the
919 		 * passed Application Tag.
920 		 */
921 		_app_tag = _dif_get_apptag(dif, ctx->dif_pi_format);
922 		if ((_app_tag & ctx->apptag_mask) != (ctx->app_tag & ctx->apptag_mask)) {
923 			_dif_error_set(err_blk, SPDK_DIF_APPTAG_ERROR, ctx->app_tag,
924 				       (_app_tag & ctx->apptag_mask), offset_blocks);
925 			SPDK_ERRLOG("Failed to compare App Tag: LBA=%" PRIu64 "," \
926 				    "  Expected=%x, Actual=%x\n",
927 				    ref_tag, ctx->app_tag, (_app_tag & ctx->apptag_mask));
928 			return -1;
929 		}
930 	}
931 
932 	if (!_dif_reftag_check(dif, ctx, ref_tag, offset_blocks, err_blk)) {
933 		return -1;
934 	}
935 
936 	return 0;
937 }
938 
939 static int
940 dif_verify(struct _dif_sgl *sgl, uint32_t num_blocks,
941 	   const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk)
942 {
943 	uint32_t offset_blocks;
944 	int rc;
945 	uint8_t *buf;
946 	uint64_t guard = 0;
947 
948 	for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
949 		_dif_sgl_get_buf(sgl, &buf, NULL);
950 
951 		if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
952 			guard = _dif_generate_guard(ctx->guard_seed, buf, ctx->guard_interval, ctx->dif_pi_format);
953 		}
954 
955 		rc = _dif_verify(buf + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
956 		if (rc != 0) {
957 			return rc;
958 		}
959 
960 		_dif_sgl_advance(sgl, ctx->block_size);
961 	}
962 
963 	return 0;
964 }
965 
966 static void
967 dif_load_split(struct _dif_sgl *sgl, struct spdk_dif *dif,
968 	       const struct spdk_dif_ctx *ctx)
969 {
970 	uint32_t offset = 0, rest_md_len, buf_len;
971 	uint8_t *buf;
972 
973 	rest_md_len = ctx->block_size - ctx->guard_interval;
974 
975 	while (offset < rest_md_len) {
976 		_dif_sgl_get_buf(sgl, &buf, &buf_len);
977 
978 		if (offset < _dif_size(ctx->dif_pi_format)) {
979 			buf_len = spdk_min(buf_len, _dif_size(ctx->dif_pi_format) - offset);
980 			memcpy((uint8_t *)dif + offset, buf, buf_len);
981 		} else {
982 			buf_len = spdk_min(buf_len, rest_md_len - offset);
983 		}
984 
985 		_dif_sgl_advance(sgl, buf_len);
986 		offset += buf_len;
987 	}
988 }
989 
990 static int
991 _dif_verify_split(struct _dif_sgl *sgl, uint32_t offset_in_block, uint32_t data_len,
992 		  uint64_t *_guard, uint32_t offset_blocks,
993 		  const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk)
994 {
995 	uint64_t guard = *_guard;
996 	struct spdk_dif dif = {};
997 	int rc;
998 
999 	assert(_guard != NULL);
1000 	assert(offset_in_block < ctx->guard_interval);
1001 	assert(offset_in_block + data_len < ctx->guard_interval ||
1002 	       offset_in_block + data_len == ctx->block_size);
1003 
1004 	guard = dif_generate_guard_split(guard, sgl, offset_in_block, data_len, ctx);
1005 
1006 	if (offset_in_block + data_len < ctx->guard_interval) {
1007 		*_guard = guard;
1008 		return 0;
1009 	}
1010 
1011 	dif_load_split(sgl, &dif, ctx);
1012 
1013 	rc = _dif_verify(&dif, guard, offset_blocks, ctx, err_blk);
1014 	if (rc != 0) {
1015 		return rc;
1016 	}
1017 
1018 	if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
1019 		guard = ctx->guard_seed;
1020 	}
1021 
1022 	*_guard = guard;
1023 	return 0;
1024 }
1025 
1026 static int
1027 dif_verify_split(struct _dif_sgl *sgl, uint32_t num_blocks,
1028 		 const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk)
1029 {
1030 	uint32_t offset_blocks;
1031 	uint64_t guard = 0;
1032 	int rc;
1033 
1034 	if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
1035 		guard = ctx->guard_seed;
1036 	}
1037 
1038 	for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1039 		rc = _dif_verify_split(sgl, 0, ctx->block_size, &guard, offset_blocks,
1040 				       ctx, err_blk);
1041 		if (rc != 0) {
1042 			return rc;
1043 		}
1044 	}
1045 
1046 	return 0;
1047 }
1048 
1049 int
1050 spdk_dif_verify(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
1051 		const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk)
1052 {
1053 	struct _dif_sgl sgl;
1054 
1055 	_dif_sgl_init(&sgl, iovs, iovcnt);
1056 
1057 	if (!_dif_sgl_is_valid(&sgl, ctx->block_size * num_blocks)) {
1058 		SPDK_ERRLOG("Size of iovec array is not valid.\n");
1059 		return -EINVAL;
1060 	}
1061 
1062 	if (_dif_is_disabled(ctx->dif_type)) {
1063 		return 0;
1064 	}
1065 
1066 	if (_dif_sgl_is_bytes_multiple(&sgl, ctx->block_size)) {
1067 		return dif_verify(&sgl, num_blocks, ctx, err_blk);
1068 	} else {
1069 		return dif_verify_split(&sgl, num_blocks, ctx, err_blk);
1070 	}
1071 }
1072 
1073 static uint32_t
1074 dif_update_crc32c(struct _dif_sgl *sgl, uint32_t num_blocks,
1075 		  uint32_t crc32c,  const struct spdk_dif_ctx *ctx)
1076 {
1077 	uint32_t offset_blocks;
1078 	uint8_t *buf;
1079 
1080 	for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1081 		_dif_sgl_get_buf(sgl, &buf, NULL);
1082 
1083 		crc32c = spdk_crc32c_update(buf, ctx->block_size - ctx->md_size, crc32c);
1084 
1085 		_dif_sgl_advance(sgl, ctx->block_size);
1086 	}
1087 
1088 	return crc32c;
1089 }
1090 
1091 static uint32_t
1092 _dif_update_crc32c_split(struct _dif_sgl *sgl, uint32_t offset_in_block, uint32_t data_len,
1093 			 uint32_t crc32c, const struct spdk_dif_ctx *ctx)
1094 {
1095 	uint32_t data_block_size, buf_len;
1096 	uint8_t *buf;
1097 
1098 	data_block_size = ctx->block_size - ctx->md_size;
1099 
1100 	assert(offset_in_block + data_len <= ctx->block_size);
1101 
1102 	while (data_len != 0) {
1103 		_dif_sgl_get_buf(sgl, &buf, &buf_len);
1104 		buf_len = spdk_min(buf_len, data_len);
1105 
1106 		if (offset_in_block < data_block_size) {
1107 			buf_len = spdk_min(buf_len, data_block_size - offset_in_block);
1108 			crc32c = spdk_crc32c_update(buf, buf_len, crc32c);
1109 		}
1110 
1111 		_dif_sgl_advance(sgl, buf_len);
1112 		offset_in_block += buf_len;
1113 		data_len -= buf_len;
1114 	}
1115 
1116 	return crc32c;
1117 }
1118 
1119 static uint32_t
1120 dif_update_crc32c_split(struct _dif_sgl *sgl, uint32_t num_blocks,
1121 			uint32_t crc32c, const struct spdk_dif_ctx *ctx)
1122 {
1123 	uint32_t offset_blocks;
1124 
1125 	for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1126 		crc32c = _dif_update_crc32c_split(sgl, 0, ctx->block_size, crc32c, ctx);
1127 	}
1128 
1129 	return crc32c;
1130 }
1131 
1132 int
1133 spdk_dif_update_crc32c(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
1134 		       uint32_t *_crc32c, const struct spdk_dif_ctx *ctx)
1135 {
1136 	struct _dif_sgl sgl;
1137 
1138 	if (_crc32c == NULL) {
1139 		return -EINVAL;
1140 	}
1141 
1142 	_dif_sgl_init(&sgl, iovs, iovcnt);
1143 
1144 	if (!_dif_sgl_is_valid(&sgl, ctx->block_size * num_blocks)) {
1145 		SPDK_ERRLOG("Size of iovec array is not valid.\n");
1146 		return -EINVAL;
1147 	}
1148 
1149 	if (_dif_sgl_is_bytes_multiple(&sgl, ctx->block_size)) {
1150 		*_crc32c = dif_update_crc32c(&sgl, num_blocks, *_crc32c, ctx);
1151 	} else {
1152 		*_crc32c = dif_update_crc32c_split(&sgl, num_blocks, *_crc32c, ctx);
1153 	}
1154 
1155 	return 0;
1156 }
1157 
1158 static void
1159 _dif_generate_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1160 		   uint32_t offset_blocks, const struct spdk_dif_ctx *ctx)
1161 {
1162 	uint32_t data_block_size;
1163 	uint8_t *src, *dst;
1164 	uint64_t guard = 0;
1165 
1166 	data_block_size = ctx->block_size - ctx->md_size;
1167 
1168 	_dif_sgl_get_buf(src_sgl, &src, NULL);
1169 	_dif_sgl_get_buf(dst_sgl, &dst, NULL);
1170 
1171 	if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
1172 		guard = _dif_generate_guard_copy(ctx->guard_seed, dst, src, data_block_size,
1173 						 ctx->dif_pi_format);
1174 		guard = _dif_generate_guard(guard, dst + data_block_size,
1175 					    ctx->guard_interval - data_block_size, ctx->dif_pi_format);
1176 	} else {
1177 		memcpy(dst, src, data_block_size);
1178 	}
1179 
1180 	_dif_generate(dst + ctx->guard_interval, guard, offset_blocks, ctx);
1181 
1182 	_dif_sgl_advance(src_sgl, data_block_size);
1183 	_dif_sgl_advance(dst_sgl, ctx->block_size);
1184 }
1185 
1186 static void
1187 dif_generate_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1188 		  uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
1189 {
1190 	uint32_t offset_blocks;
1191 
1192 	for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1193 		_dif_generate_copy(src_sgl, dst_sgl, offset_blocks, ctx);
1194 	}
1195 }
1196 
1197 static void
1198 _dif_generate_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1199 			 uint32_t offset_blocks, const struct spdk_dif_ctx *ctx)
1200 {
1201 	uint32_t data_block_size;
1202 	uint64_t guard = 0;
1203 	struct spdk_dif dif = {};
1204 
1205 	data_block_size = ctx->block_size - ctx->md_size;
1206 
1207 	if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
1208 		guard = _dif_generate_guard_copy_split(ctx->guard_seed, dst_sgl, src_sgl,
1209 						       data_block_size, ctx->dif_pi_format);
1210 		guard = dif_generate_guard_split(guard, dst_sgl, data_block_size,
1211 						 ctx->guard_interval - data_block_size, ctx);
1212 	} else {
1213 		_data_copy_split(dst_sgl, src_sgl, data_block_size);
1214 		_dif_sgl_advance(dst_sgl, ctx->guard_interval - data_block_size);
1215 	}
1216 
1217 	_dif_generate(&dif, guard, offset_blocks, ctx);
1218 
1219 	dif_store_split(dst_sgl, &dif, ctx);
1220 }
1221 
1222 static void
1223 dif_generate_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1224 			uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
1225 {
1226 	uint32_t offset_blocks;
1227 
1228 	for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1229 		_dif_generate_copy_split(src_sgl, dst_sgl, offset_blocks, ctx);
1230 	}
1231 }
1232 
1233 int
1234 spdk_dif_generate_copy(struct iovec *iovs, int iovcnt, struct iovec *bounce_iovs,
1235 		       int bounce_iovcnt, uint32_t num_blocks,
1236 		       const struct spdk_dif_ctx *ctx)
1237 {
1238 	struct _dif_sgl src_sgl, dst_sgl;
1239 	uint32_t data_block_size;
1240 
1241 	_dif_sgl_init(&src_sgl, iovs, iovcnt);
1242 	_dif_sgl_init(&dst_sgl, bounce_iovs, bounce_iovcnt);
1243 
1244 	data_block_size = ctx->block_size - ctx->md_size;
1245 
1246 	if (!_dif_sgl_is_valid(&src_sgl, data_block_size * num_blocks) ||
1247 	    !_dif_sgl_is_valid(&dst_sgl, ctx->block_size * num_blocks)) {
1248 		SPDK_ERRLOG("Size of iovec arrays are not valid.\n");
1249 		return -EINVAL;
1250 	}
1251 
1252 	if (_dif_is_disabled(ctx->dif_type)) {
1253 		return 0;
1254 	}
1255 
1256 	if (_dif_sgl_is_bytes_multiple(&src_sgl, data_block_size) &&
1257 	    _dif_sgl_is_bytes_multiple(&dst_sgl, ctx->block_size)) {
1258 		dif_generate_copy(&src_sgl, &dst_sgl, num_blocks, ctx);
1259 	} else {
1260 		dif_generate_copy_split(&src_sgl, &dst_sgl, num_blocks, ctx);
1261 	}
1262 
1263 	return 0;
1264 }
1265 
1266 static int
1267 _dif_verify_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1268 		 uint32_t offset_blocks, const struct spdk_dif_ctx *ctx,
1269 		 struct spdk_dif_error *err_blk)
1270 {
1271 	uint32_t data_block_size;
1272 	uint8_t *src, *dst;
1273 	int rc;
1274 	uint64_t guard = 0;
1275 
1276 	data_block_size = ctx->block_size - ctx->md_size;
1277 
1278 	_dif_sgl_get_buf(src_sgl, &src, NULL);
1279 	_dif_sgl_get_buf(dst_sgl, &dst, NULL);
1280 
1281 	if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
1282 		guard = _dif_generate_guard_copy(ctx->guard_seed, dst, src, data_block_size,
1283 						 ctx->dif_pi_format);
1284 		guard = _dif_generate_guard(guard, src + data_block_size,
1285 					    ctx->guard_interval - data_block_size, ctx->dif_pi_format);
1286 	} else {
1287 		memcpy(dst, src, data_block_size);
1288 	}
1289 
1290 	rc = _dif_verify(src + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
1291 	if (rc != 0) {
1292 		return rc;
1293 	}
1294 
1295 	_dif_sgl_advance(src_sgl, ctx->block_size);
1296 	_dif_sgl_advance(dst_sgl, data_block_size);
1297 
1298 	return 0;
1299 }
1300 
1301 static int
1302 dif_verify_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1303 		uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
1304 		struct spdk_dif_error *err_blk)
1305 {
1306 	uint32_t offset_blocks;
1307 	int rc;
1308 
1309 	for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1310 		rc = _dif_verify_copy(src_sgl, dst_sgl, offset_blocks, ctx, err_blk);
1311 		if (rc != 0) {
1312 			return rc;
1313 		}
1314 	}
1315 
1316 	return 0;
1317 }
1318 
1319 static int
1320 _dif_verify_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1321 		       uint32_t offset_blocks, const struct spdk_dif_ctx *ctx,
1322 		       struct spdk_dif_error *err_blk)
1323 {
1324 	uint32_t data_block_size;
1325 	uint64_t guard = 0;
1326 	struct spdk_dif dif = {};
1327 
1328 	data_block_size = ctx->block_size - ctx->md_size;
1329 
1330 	if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
1331 		guard = _dif_generate_guard_copy_split(ctx->guard_seed, dst_sgl, src_sgl,
1332 						       data_block_size, ctx->dif_pi_format);
1333 		guard = dif_generate_guard_split(guard, src_sgl, data_block_size,
1334 						 ctx->guard_interval - data_block_size, ctx);
1335 	} else {
1336 		_data_copy_split(dst_sgl, src_sgl, data_block_size);
1337 		_dif_sgl_advance(src_sgl, ctx->guard_interval - data_block_size);
1338 	}
1339 
1340 	dif_load_split(src_sgl, &dif, ctx);
1341 
1342 	return _dif_verify(&dif, guard, offset_blocks, ctx, err_blk);
1343 }
1344 
1345 static int
1346 dif_verify_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1347 		      uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
1348 		      struct spdk_dif_error *err_blk)
1349 {
1350 	uint32_t offset_blocks;
1351 	int rc;
1352 
1353 	for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1354 		rc = _dif_verify_copy_split(src_sgl, dst_sgl, offset_blocks, ctx, err_blk);
1355 		if (rc != 0) {
1356 			return rc;
1357 		}
1358 	}
1359 
1360 	return 0;
1361 }
1362 
1363 int
1364 spdk_dif_verify_copy(struct iovec *iovs, int iovcnt, struct iovec *bounce_iovs,
1365 		     int bounce_iovcnt, uint32_t num_blocks,
1366 		     const struct spdk_dif_ctx *ctx,
1367 		     struct spdk_dif_error *err_blk)
1368 {
1369 	struct _dif_sgl src_sgl, dst_sgl;
1370 	uint32_t data_block_size;
1371 
1372 	_dif_sgl_init(&src_sgl, bounce_iovs, bounce_iovcnt);
1373 	_dif_sgl_init(&dst_sgl, iovs, iovcnt);
1374 
1375 	data_block_size = ctx->block_size - ctx->md_size;
1376 
1377 	if (!_dif_sgl_is_valid(&dst_sgl, data_block_size * num_blocks) ||
1378 	    !_dif_sgl_is_valid(&src_sgl, ctx->block_size * num_blocks)) {
1379 		SPDK_ERRLOG("Size of iovec arrays are not valid\n");
1380 		return -EINVAL;
1381 	}
1382 
1383 	if (_dif_is_disabled(ctx->dif_type)) {
1384 		return 0;
1385 	}
1386 
1387 	if (_dif_sgl_is_bytes_multiple(&dst_sgl, data_block_size) &&
1388 	    _dif_sgl_is_bytes_multiple(&src_sgl, ctx->block_size)) {
1389 		return dif_verify_copy(&src_sgl, &dst_sgl, num_blocks, ctx, err_blk);
1390 	} else {
1391 		return dif_verify_copy_split(&src_sgl, &dst_sgl, num_blocks, ctx, err_blk);
1392 	}
1393 }
1394 
1395 static void
1396 _bit_flip(uint8_t *buf, uint32_t flip_bit)
1397 {
1398 	uint8_t byte;
1399 
1400 	byte = *buf;
1401 	byte ^= 1 << flip_bit;
1402 	*buf = byte;
1403 }
1404 
1405 static int
1406 _dif_inject_error(struct _dif_sgl *sgl,
1407 		  uint32_t block_size, uint32_t num_blocks,
1408 		  uint32_t inject_offset_blocks,
1409 		  uint32_t inject_offset_bytes,
1410 		  uint32_t inject_offset_bits)
1411 {
1412 	uint32_t offset_in_block, buf_len;
1413 	uint8_t *buf;
1414 
1415 	_dif_sgl_advance(sgl, block_size * inject_offset_blocks);
1416 
1417 	offset_in_block = 0;
1418 
1419 	while (offset_in_block < block_size) {
1420 		_dif_sgl_get_buf(sgl, &buf, &buf_len);
1421 		buf_len = spdk_min(buf_len, block_size - offset_in_block);
1422 
1423 		if (inject_offset_bytes >= offset_in_block &&
1424 		    inject_offset_bytes < offset_in_block + buf_len) {
1425 			buf += inject_offset_bytes - offset_in_block;
1426 			_bit_flip(buf, inject_offset_bits);
1427 			return 0;
1428 		}
1429 
1430 		_dif_sgl_advance(sgl, buf_len);
1431 		offset_in_block += buf_len;
1432 	}
1433 
1434 	return -1;
1435 }
1436 
1437 static int
1438 dif_inject_error(struct _dif_sgl *sgl, uint32_t block_size, uint32_t num_blocks,
1439 		 uint32_t start_inject_bytes, uint32_t inject_range_bytes,
1440 		 uint32_t *inject_offset)
1441 {
1442 	uint32_t inject_offset_blocks, inject_offset_bytes, inject_offset_bits;
1443 	uint32_t offset_blocks;
1444 	int rc;
1445 
1446 	srand(time(0));
1447 
1448 	inject_offset_blocks = rand() % num_blocks;
1449 	inject_offset_bytes = start_inject_bytes + (rand() % inject_range_bytes);
1450 	inject_offset_bits = rand() % 8;
1451 
1452 	for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1453 		if (offset_blocks == inject_offset_blocks) {
1454 			rc = _dif_inject_error(sgl, block_size, num_blocks,
1455 					       inject_offset_blocks,
1456 					       inject_offset_bytes,
1457 					       inject_offset_bits);
1458 			if (rc == 0) {
1459 				*inject_offset = inject_offset_blocks;
1460 			}
1461 			return rc;
1462 		}
1463 	}
1464 
1465 	return -1;
1466 }
1467 
1468 int
1469 spdk_dif_inject_error(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
1470 		      const struct spdk_dif_ctx *ctx, uint32_t inject_flags,
1471 		      uint32_t *inject_offset)
1472 {
1473 	struct _dif_sgl sgl;
1474 	int rc;
1475 
1476 	_dif_sgl_init(&sgl, iovs, iovcnt);
1477 
1478 	if (!_dif_sgl_is_valid(&sgl, ctx->block_size * num_blocks)) {
1479 		SPDK_ERRLOG("Size of iovec array is not valid.\n");
1480 		return -EINVAL;
1481 	}
1482 
1483 	if (inject_flags & SPDK_DIF_REFTAG_ERROR) {
1484 		rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
1485 				      ctx->guard_interval + _dif_reftag_offset(ctx->dif_pi_format),
1486 				      _dif_reftag_size(ctx->dif_pi_format),
1487 				      inject_offset);
1488 		if (rc != 0) {
1489 			SPDK_ERRLOG("Failed to inject error to Reference Tag.\n");
1490 			return rc;
1491 		}
1492 	}
1493 
1494 	if (inject_flags & SPDK_DIF_APPTAG_ERROR) {
1495 		rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
1496 				      ctx->guard_interval + _dif_apptag_offset(ctx->dif_pi_format),
1497 				      _dif_apptag_size(),
1498 				      inject_offset);
1499 		if (rc != 0) {
1500 			SPDK_ERRLOG("Failed to inject error to Application Tag.\n");
1501 			return rc;
1502 		}
1503 	}
1504 	if (inject_flags & SPDK_DIF_GUARD_ERROR) {
1505 		rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
1506 				      ctx->guard_interval,
1507 				      _dif_guard_size(ctx->dif_pi_format),
1508 				      inject_offset);
1509 		if (rc != 0) {
1510 			SPDK_ERRLOG("Failed to inject error to Guard.\n");
1511 			return rc;
1512 		}
1513 	}
1514 
1515 	if (inject_flags & SPDK_DIF_DATA_ERROR) {
1516 		/* If the DIF information is contained within the last 8/16 bytes of
1517 		 * metadata (depending on the PI format), then the CRC covers all metadata
1518 		 * bytes up to but excluding the last 8/16 bytes. But error injection does not
1519 		 * cover these metadata because classification is not determined yet.
1520 		 *
1521 		 * Note: Error injection to data block is expected to be detected as
1522 		 * guard error.
1523 		 */
1524 		rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
1525 				      0,
1526 				      ctx->block_size - ctx->md_size,
1527 				      inject_offset);
1528 		if (rc != 0) {
1529 			SPDK_ERRLOG("Failed to inject error to data block.\n");
1530 			return rc;
1531 		}
1532 	}
1533 
1534 	return 0;
1535 }
1536 
1537 static void
1538 dix_generate(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
1539 	     uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
1540 {
1541 	uint32_t offset_blocks = 0;
1542 	uint8_t *data_buf, *md_buf;
1543 	uint64_t guard;
1544 
1545 	while (offset_blocks < num_blocks) {
1546 		_dif_sgl_get_buf(data_sgl, &data_buf, NULL);
1547 		_dif_sgl_get_buf(md_sgl, &md_buf, NULL);
1548 
1549 		guard = 0;
1550 		if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
1551 			guard = _dif_generate_guard(ctx->guard_seed, data_buf, ctx->block_size,
1552 						    ctx->dif_pi_format);
1553 			guard = _dif_generate_guard(guard, md_buf, ctx->guard_interval,
1554 						    ctx->dif_pi_format);
1555 		}
1556 
1557 		_dif_generate(md_buf + ctx->guard_interval, guard, offset_blocks, ctx);
1558 
1559 		_dif_sgl_advance(data_sgl, ctx->block_size);
1560 		_dif_sgl_advance(md_sgl, ctx->md_size);
1561 		offset_blocks++;
1562 	}
1563 }
1564 
1565 static void
1566 _dix_generate_split(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
1567 		    uint32_t offset_blocks, const struct spdk_dif_ctx *ctx)
1568 {
1569 	uint32_t offset_in_block, data_buf_len;
1570 	uint8_t *data_buf, *md_buf;
1571 	uint64_t guard = 0;
1572 
1573 	_dif_sgl_get_buf(md_sgl, &md_buf, NULL);
1574 
1575 	if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
1576 		guard = ctx->guard_seed;
1577 	}
1578 	offset_in_block = 0;
1579 
1580 	while (offset_in_block < ctx->block_size) {
1581 		_dif_sgl_get_buf(data_sgl, &data_buf, &data_buf_len);
1582 		data_buf_len = spdk_min(data_buf_len, ctx->block_size - offset_in_block);
1583 
1584 		if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
1585 			guard = _dif_generate_guard(guard, data_buf, data_buf_len,
1586 						    ctx->dif_pi_format);
1587 		}
1588 
1589 		_dif_sgl_advance(data_sgl, data_buf_len);
1590 		offset_in_block += data_buf_len;
1591 	}
1592 
1593 	if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
1594 		guard = _dif_generate_guard(guard, md_buf, ctx->guard_interval,
1595 					    ctx->dif_pi_format);
1596 	}
1597 
1598 	_dif_sgl_advance(md_sgl, ctx->md_size);
1599 
1600 	_dif_generate(md_buf + ctx->guard_interval, guard, offset_blocks, ctx);
1601 }
1602 
1603 static void
1604 dix_generate_split(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
1605 		   uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
1606 {
1607 	uint32_t offset_blocks;
1608 
1609 	for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1610 		_dix_generate_split(data_sgl, md_sgl, offset_blocks, ctx);
1611 	}
1612 }
1613 
1614 int
1615 spdk_dix_generate(struct iovec *iovs, int iovcnt, struct iovec *md_iov,
1616 		  uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
1617 {
1618 	struct _dif_sgl data_sgl, md_sgl;
1619 
1620 	_dif_sgl_init(&data_sgl, iovs, iovcnt);
1621 	_dif_sgl_init(&md_sgl, md_iov, 1);
1622 
1623 	if (!_dif_sgl_is_valid(&data_sgl, ctx->block_size * num_blocks) ||
1624 	    !_dif_sgl_is_valid(&md_sgl, ctx->md_size * num_blocks)) {
1625 		SPDK_ERRLOG("Size of iovec array is not valid.\n");
1626 		return -EINVAL;
1627 	}
1628 
1629 	if (_dif_is_disabled(ctx->dif_type)) {
1630 		return 0;
1631 	}
1632 
1633 	if (_dif_sgl_is_bytes_multiple(&data_sgl, ctx->block_size)) {
1634 		dix_generate(&data_sgl, &md_sgl, num_blocks, ctx);
1635 	} else {
1636 		dix_generate_split(&data_sgl, &md_sgl, num_blocks, ctx);
1637 	}
1638 
1639 	return 0;
1640 }
1641 
1642 static int
1643 dix_verify(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
1644 	   uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
1645 	   struct spdk_dif_error *err_blk)
1646 {
1647 	uint32_t offset_blocks = 0;
1648 	uint8_t *data_buf, *md_buf;
1649 	uint64_t guard;
1650 	int rc;
1651 
1652 	while (offset_blocks < num_blocks) {
1653 		_dif_sgl_get_buf(data_sgl, &data_buf, NULL);
1654 		_dif_sgl_get_buf(md_sgl, &md_buf, NULL);
1655 
1656 		guard = 0;
1657 		if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
1658 			guard = _dif_generate_guard(ctx->guard_seed, data_buf, ctx->block_size,
1659 						    ctx->dif_pi_format);
1660 			guard = _dif_generate_guard(guard, md_buf, ctx->guard_interval,
1661 						    ctx->dif_pi_format);
1662 		}
1663 
1664 		rc = _dif_verify(md_buf + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
1665 		if (rc != 0) {
1666 			return rc;
1667 		}
1668 
1669 		_dif_sgl_advance(data_sgl, ctx->block_size);
1670 		_dif_sgl_advance(md_sgl, ctx->md_size);
1671 		offset_blocks++;
1672 	}
1673 
1674 	return 0;
1675 }
1676 
1677 static int
1678 _dix_verify_split(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
1679 		  uint32_t offset_blocks, const struct spdk_dif_ctx *ctx,
1680 		  struct spdk_dif_error *err_blk)
1681 {
1682 	uint32_t offset_in_block, data_buf_len;
1683 	uint8_t *data_buf, *md_buf;
1684 	uint64_t guard = 0;
1685 
1686 	_dif_sgl_get_buf(md_sgl, &md_buf, NULL);
1687 
1688 	if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
1689 		guard = ctx->guard_seed;
1690 	}
1691 	offset_in_block = 0;
1692 
1693 	while (offset_in_block < ctx->block_size) {
1694 		_dif_sgl_get_buf(data_sgl, &data_buf, &data_buf_len);
1695 		data_buf_len = spdk_min(data_buf_len, ctx->block_size - offset_in_block);
1696 
1697 		if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
1698 			guard = _dif_generate_guard(guard, data_buf, data_buf_len,
1699 						    ctx->dif_pi_format);
1700 		}
1701 
1702 		_dif_sgl_advance(data_sgl, data_buf_len);
1703 		offset_in_block += data_buf_len;
1704 	}
1705 
1706 	if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
1707 		guard = _dif_generate_guard(guard, md_buf, ctx->guard_interval,
1708 					    ctx->dif_pi_format);
1709 	}
1710 
1711 	_dif_sgl_advance(md_sgl, ctx->md_size);
1712 
1713 	return _dif_verify(md_buf + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
1714 }
1715 
1716 static int
1717 dix_verify_split(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
1718 		 uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
1719 		 struct spdk_dif_error *err_blk)
1720 {
1721 	uint32_t offset_blocks;
1722 	int rc;
1723 
1724 	for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1725 		rc = _dix_verify_split(data_sgl, md_sgl, offset_blocks, ctx, err_blk);
1726 		if (rc != 0) {
1727 			return rc;
1728 		}
1729 	}
1730 
1731 	return 0;
1732 }
1733 
1734 int
1735 spdk_dix_verify(struct iovec *iovs, int iovcnt, struct iovec *md_iov,
1736 		uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
1737 		struct spdk_dif_error *err_blk)
1738 {
1739 	struct _dif_sgl data_sgl, md_sgl;
1740 
1741 	if (md_iov->iov_base == NULL) {
1742 		SPDK_ERRLOG("Metadata buffer is NULL.\n");
1743 		return -EINVAL;
1744 	}
1745 
1746 	_dif_sgl_init(&data_sgl, iovs, iovcnt);
1747 	_dif_sgl_init(&md_sgl, md_iov, 1);
1748 
1749 	if (!_dif_sgl_is_valid(&data_sgl, ctx->block_size * num_blocks) ||
1750 	    !_dif_sgl_is_valid(&md_sgl, ctx->md_size * num_blocks)) {
1751 		SPDK_ERRLOG("Size of iovec array is not valid.\n");
1752 		return -EINVAL;
1753 	}
1754 
1755 	if (_dif_is_disabled(ctx->dif_type)) {
1756 		return 0;
1757 	}
1758 
1759 	if (_dif_sgl_is_bytes_multiple(&data_sgl, ctx->block_size)) {
1760 		return dix_verify(&data_sgl, &md_sgl, num_blocks, ctx, err_blk);
1761 	} else {
1762 		return dix_verify_split(&data_sgl, &md_sgl, num_blocks, ctx, err_blk);
1763 	}
1764 }
1765 
1766 int
1767 spdk_dix_inject_error(struct iovec *iovs, int iovcnt, struct iovec *md_iov,
1768 		      uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
1769 		      uint32_t inject_flags, uint32_t *inject_offset)
1770 {
1771 	struct _dif_sgl data_sgl, md_sgl;
1772 	int rc;
1773 
1774 	_dif_sgl_init(&data_sgl, iovs, iovcnt);
1775 	_dif_sgl_init(&md_sgl, md_iov, 1);
1776 
1777 	if (!_dif_sgl_is_valid(&data_sgl, ctx->block_size * num_blocks) ||
1778 	    !_dif_sgl_is_valid(&md_sgl, ctx->md_size * num_blocks)) {
1779 		SPDK_ERRLOG("Size of iovec array is not valid.\n");
1780 		return -EINVAL;
1781 	}
1782 
1783 	if (inject_flags & SPDK_DIF_REFTAG_ERROR) {
1784 		rc = dif_inject_error(&md_sgl, ctx->md_size, num_blocks,
1785 				      ctx->guard_interval + _dif_reftag_offset(ctx->dif_pi_format),
1786 				      _dif_reftag_size(ctx->dif_pi_format),
1787 				      inject_offset);
1788 		if (rc != 0) {
1789 			SPDK_ERRLOG("Failed to inject error to Reference Tag.\n");
1790 			return rc;
1791 		}
1792 	}
1793 
1794 	if (inject_flags & SPDK_DIF_APPTAG_ERROR) {
1795 		rc = dif_inject_error(&md_sgl, ctx->md_size, num_blocks,
1796 				      ctx->guard_interval + _dif_apptag_offset(ctx->dif_pi_format),
1797 				      _dif_apptag_size(),
1798 				      inject_offset);
1799 		if (rc != 0) {
1800 			SPDK_ERRLOG("Failed to inject error to Application Tag.\n");
1801 			return rc;
1802 		}
1803 	}
1804 
1805 	if (inject_flags & SPDK_DIF_GUARD_ERROR) {
1806 		rc = dif_inject_error(&md_sgl, ctx->md_size, num_blocks,
1807 				      ctx->guard_interval,
1808 				      _dif_guard_size(ctx->dif_pi_format),
1809 				      inject_offset);
1810 		if (rc != 0) {
1811 			SPDK_ERRLOG("Failed to inject error to Guard.\n");
1812 			return rc;
1813 		}
1814 	}
1815 
1816 	if (inject_flags & SPDK_DIF_DATA_ERROR) {
1817 		/* Note: Error injection to data block is expected to be detected
1818 		 * as guard error.
1819 		 */
1820 		rc = dif_inject_error(&data_sgl, ctx->block_size, num_blocks,
1821 				      0,
1822 				      ctx->block_size,
1823 				      inject_offset);
1824 		if (rc != 0) {
1825 			SPDK_ERRLOG("Failed to inject error to Guard.\n");
1826 			return rc;
1827 		}
1828 	}
1829 
1830 	return 0;
1831 }
1832 
1833 static uint32_t
1834 _to_next_boundary(uint32_t offset, uint32_t boundary)
1835 {
1836 	return boundary - (offset % boundary);
1837 }
1838 
1839 static uint32_t
1840 _to_size_with_md(uint32_t size, uint32_t data_block_size, uint32_t block_size)
1841 {
1842 	return (size / data_block_size) * block_size + (size % data_block_size);
1843 }
1844 
1845 int
1846 spdk_dif_set_md_interleave_iovs(struct iovec *iovs, int iovcnt,
1847 				struct iovec *buf_iovs, int buf_iovcnt,
1848 				uint32_t data_offset, uint32_t data_len,
1849 				uint32_t *_mapped_len,
1850 				const struct spdk_dif_ctx *ctx)
1851 {
1852 	uint32_t data_block_size, data_unalign, buf_len, buf_offset, len;
1853 	struct _dif_sgl dif_sgl;
1854 	struct _dif_sgl buf_sgl;
1855 
1856 	if (iovs == NULL || iovcnt == 0 || buf_iovs == NULL || buf_iovcnt == 0) {
1857 		return -EINVAL;
1858 	}
1859 
1860 	data_block_size = ctx->block_size - ctx->md_size;
1861 
1862 	data_unalign = ctx->data_offset % data_block_size;
1863 
1864 	buf_len = _to_size_with_md(data_unalign + data_offset + data_len, data_block_size,
1865 				   ctx->block_size);
1866 	buf_len -= data_unalign;
1867 
1868 	_dif_sgl_init(&dif_sgl, iovs, iovcnt);
1869 	_dif_sgl_init(&buf_sgl, buf_iovs, buf_iovcnt);
1870 
1871 	if (!_dif_sgl_is_valid(&buf_sgl, buf_len)) {
1872 		SPDK_ERRLOG("Buffer overflow will occur.\n");
1873 		return -ERANGE;
1874 	}
1875 
1876 	buf_offset = _to_size_with_md(data_unalign + data_offset, data_block_size, ctx->block_size);
1877 	buf_offset -= data_unalign;
1878 
1879 	_dif_sgl_advance(&buf_sgl, buf_offset);
1880 
1881 	while (data_len != 0) {
1882 		len = spdk_min(data_len, _to_next_boundary(ctx->data_offset + data_offset, data_block_size));
1883 		if (!_dif_sgl_append_split(&dif_sgl, &buf_sgl, len)) {
1884 			break;
1885 		}
1886 		_dif_sgl_advance(&buf_sgl, ctx->md_size);
1887 		data_offset += len;
1888 		data_len -= len;
1889 	}
1890 
1891 	if (_mapped_len != NULL) {
1892 		*_mapped_len = dif_sgl.total_size;
1893 	}
1894 
1895 	return iovcnt - dif_sgl.iovcnt;
1896 }
1897 
1898 static int
1899 _dif_sgl_setup_stream(struct _dif_sgl *sgl, uint32_t *_buf_offset, uint32_t *_buf_len,
1900 		      uint32_t data_offset, uint32_t data_len,
1901 		      const struct spdk_dif_ctx *ctx)
1902 {
1903 	uint32_t data_block_size, data_unalign, buf_len, buf_offset;
1904 
1905 	data_block_size = ctx->block_size - ctx->md_size;
1906 
1907 	data_unalign = ctx->data_offset % data_block_size;
1908 
1909 	/* If the last data block is complete, DIF of the data block is
1910 	 * inserted or verified in this turn.
1911 	 */
1912 	buf_len = _to_size_with_md(data_unalign + data_offset + data_len, data_block_size,
1913 				   ctx->block_size);
1914 	buf_len -= data_unalign;
1915 
1916 	if (!_dif_sgl_is_valid(sgl, buf_len)) {
1917 		return -ERANGE;
1918 	}
1919 
1920 	buf_offset = _to_size_with_md(data_unalign + data_offset, data_block_size, ctx->block_size);
1921 	buf_offset -= data_unalign;
1922 
1923 	_dif_sgl_advance(sgl, buf_offset);
1924 	buf_len -= buf_offset;
1925 
1926 	buf_offset += data_unalign;
1927 
1928 	*_buf_offset = buf_offset;
1929 	*_buf_len = buf_len;
1930 
1931 	return 0;
1932 }
1933 
1934 int
1935 spdk_dif_generate_stream(struct iovec *iovs, int iovcnt,
1936 			 uint32_t data_offset, uint32_t data_len,
1937 			 struct spdk_dif_ctx *ctx)
1938 {
1939 	uint32_t buf_len = 0, buf_offset = 0;
1940 	uint32_t len, offset_in_block, offset_blocks;
1941 	uint64_t guard = 0;
1942 	struct _dif_sgl sgl;
1943 	int rc;
1944 
1945 	if (iovs == NULL || iovcnt == 0) {
1946 		return -EINVAL;
1947 	}
1948 
1949 	if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
1950 		guard = ctx->last_guard;
1951 	}
1952 
1953 	_dif_sgl_init(&sgl, iovs, iovcnt);
1954 
1955 	rc = _dif_sgl_setup_stream(&sgl, &buf_offset, &buf_len, data_offset, data_len, ctx);
1956 	if (rc != 0) {
1957 		return rc;
1958 	}
1959 
1960 	while (buf_len != 0) {
1961 		len = spdk_min(buf_len, _to_next_boundary(buf_offset, ctx->block_size));
1962 		offset_in_block = buf_offset % ctx->block_size;
1963 		offset_blocks = buf_offset / ctx->block_size;
1964 
1965 		guard = _dif_generate_split(&sgl, offset_in_block, len, guard, offset_blocks, ctx);
1966 
1967 		buf_len -= len;
1968 		buf_offset += len;
1969 	}
1970 
1971 	if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
1972 		ctx->last_guard = guard;
1973 	}
1974 
1975 	return 0;
1976 }
1977 
1978 int
1979 spdk_dif_verify_stream(struct iovec *iovs, int iovcnt,
1980 		       uint32_t data_offset, uint32_t data_len,
1981 		       struct spdk_dif_ctx *ctx,
1982 		       struct spdk_dif_error *err_blk)
1983 {
1984 	uint32_t buf_len = 0, buf_offset = 0;
1985 	uint32_t len, offset_in_block, offset_blocks;
1986 	uint64_t guard = 0;
1987 	struct _dif_sgl sgl;
1988 	int rc = 0;
1989 
1990 	if (iovs == NULL || iovcnt == 0) {
1991 		return -EINVAL;
1992 	}
1993 
1994 	if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
1995 		guard = ctx->last_guard;
1996 	}
1997 
1998 	_dif_sgl_init(&sgl, iovs, iovcnt);
1999 
2000 	rc = _dif_sgl_setup_stream(&sgl, &buf_offset, &buf_len, data_offset, data_len, ctx);
2001 	if (rc != 0) {
2002 		return rc;
2003 	}
2004 
2005 	while (buf_len != 0) {
2006 		len = spdk_min(buf_len, _to_next_boundary(buf_offset, ctx->block_size));
2007 		offset_in_block = buf_offset % ctx->block_size;
2008 		offset_blocks = buf_offset / ctx->block_size;
2009 
2010 		rc = _dif_verify_split(&sgl, offset_in_block, len, &guard, offset_blocks,
2011 				       ctx, err_blk);
2012 		if (rc != 0) {
2013 			goto error;
2014 		}
2015 
2016 		buf_len -= len;
2017 		buf_offset += len;
2018 	}
2019 
2020 	if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
2021 		ctx->last_guard = guard;
2022 	}
2023 error:
2024 	return rc;
2025 }
2026 
2027 int
2028 spdk_dif_update_crc32c_stream(struct iovec *iovs, int iovcnt,
2029 			      uint32_t data_offset, uint32_t data_len,
2030 			      uint32_t *_crc32c, const struct spdk_dif_ctx *ctx)
2031 {
2032 	uint32_t buf_len = 0, buf_offset = 0, len, offset_in_block;
2033 	uint32_t crc32c;
2034 	struct _dif_sgl sgl;
2035 	int rc;
2036 
2037 	if (iovs == NULL || iovcnt == 0) {
2038 		return -EINVAL;
2039 	}
2040 
2041 	crc32c = *_crc32c;
2042 	_dif_sgl_init(&sgl, iovs, iovcnt);
2043 
2044 	rc = _dif_sgl_setup_stream(&sgl, &buf_offset, &buf_len, data_offset, data_len, ctx);
2045 	if (rc != 0) {
2046 		return rc;
2047 	}
2048 
2049 	while (buf_len != 0) {
2050 		len = spdk_min(buf_len, _to_next_boundary(buf_offset, ctx->block_size));
2051 		offset_in_block = buf_offset % ctx->block_size;
2052 
2053 		crc32c = _dif_update_crc32c_split(&sgl, offset_in_block, len, crc32c, ctx);
2054 
2055 		buf_len -= len;
2056 		buf_offset += len;
2057 	}
2058 
2059 	*_crc32c = crc32c;
2060 
2061 	return 0;
2062 }
2063 
2064 void
2065 spdk_dif_get_range_with_md(uint32_t data_offset, uint32_t data_len,
2066 			   uint32_t *_buf_offset, uint32_t *_buf_len,
2067 			   const struct spdk_dif_ctx *ctx)
2068 {
2069 	uint32_t data_block_size, data_unalign, buf_offset, buf_len;
2070 
2071 	if (!ctx->md_interleave) {
2072 		buf_offset = data_offset;
2073 		buf_len = data_len;
2074 	} else {
2075 		data_block_size = ctx->block_size - ctx->md_size;
2076 
2077 		data_unalign = data_offset % data_block_size;
2078 
2079 		buf_offset = _to_size_with_md(data_offset, data_block_size, ctx->block_size);
2080 		buf_len = _to_size_with_md(data_unalign + data_len, data_block_size, ctx->block_size) -
2081 			  data_unalign;
2082 	}
2083 
2084 	if (_buf_offset != NULL) {
2085 		*_buf_offset = buf_offset;
2086 	}
2087 
2088 	if (_buf_len != NULL) {
2089 		*_buf_len = buf_len;
2090 	}
2091 }
2092 
2093 uint32_t
2094 spdk_dif_get_length_with_md(uint32_t data_len, const struct spdk_dif_ctx *ctx)
2095 {
2096 	uint32_t data_block_size;
2097 
2098 	if (!ctx->md_interleave) {
2099 		return data_len;
2100 	} else {
2101 		data_block_size = ctx->block_size - ctx->md_size;
2102 
2103 		return _to_size_with_md(data_len, data_block_size, ctx->block_size);
2104 	}
2105 }
2106 
2107 static int
2108 _dif_remap_ref_tag(struct _dif_sgl *sgl, uint32_t offset_blocks,
2109 		   const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk,
2110 		   bool check_ref_tag)
2111 {
2112 	uint32_t offset, buf_len;
2113 	uint64_t expected = 0, remapped;
2114 	uint8_t *buf;
2115 	struct _dif_sgl tmp_sgl;
2116 	struct spdk_dif dif;
2117 
2118 	/* Fast forward to DIF field. */
2119 	_dif_sgl_advance(sgl, ctx->guard_interval);
2120 	_dif_sgl_copy(&tmp_sgl, sgl);
2121 
2122 	/* Copy the split DIF field to the temporary DIF buffer */
2123 	offset = 0;
2124 	while (offset < _dif_size(ctx->dif_pi_format)) {
2125 		_dif_sgl_get_buf(sgl, &buf, &buf_len);
2126 		buf_len = spdk_min(buf_len, _dif_size(ctx->dif_pi_format) - offset);
2127 
2128 		memcpy((uint8_t *)&dif + offset, buf, buf_len);
2129 
2130 		_dif_sgl_advance(sgl, buf_len);
2131 		offset += buf_len;
2132 	}
2133 
2134 	if (_dif_ignore(&dif, ctx)) {
2135 		goto end;
2136 	}
2137 
2138 	/* For type 1 and 2, the Reference Tag is incremented for each
2139 	 * subsequent logical block. For type 3, the Reference Tag
2140 	 * remains the same as the initial Reference Tag.
2141 	 */
2142 	if (ctx->dif_type != SPDK_DIF_TYPE3) {
2143 		expected = ctx->init_ref_tag + ctx->ref_tag_offset + offset_blocks;
2144 		remapped = ctx->remapped_init_ref_tag + ctx->ref_tag_offset + offset_blocks;
2145 	} else {
2146 		remapped = ctx->remapped_init_ref_tag;
2147 	}
2148 
2149 	/* Verify the stored Reference Tag. */
2150 	if (check_ref_tag && !_dif_reftag_check(&dif, ctx, expected, offset_blocks, err_blk)) {
2151 		return -1;
2152 	}
2153 
2154 	/* Update the stored Reference Tag to the remapped one. */
2155 	_dif_set_reftag(&dif, remapped, ctx->dif_pi_format);
2156 
2157 	offset = 0;
2158 	while (offset < _dif_size(ctx->dif_pi_format)) {
2159 		_dif_sgl_get_buf(&tmp_sgl, &buf, &buf_len);
2160 		buf_len = spdk_min(buf_len, _dif_size(ctx->dif_pi_format) - offset);
2161 
2162 		memcpy(buf, (uint8_t *)&dif + offset, buf_len);
2163 
2164 		_dif_sgl_advance(&tmp_sgl, buf_len);
2165 		offset += buf_len;
2166 	}
2167 
2168 end:
2169 	_dif_sgl_advance(sgl, ctx->block_size - ctx->guard_interval - _dif_size(ctx->dif_pi_format));
2170 
2171 	return 0;
2172 }
2173 
2174 int
2175 spdk_dif_remap_ref_tag(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
2176 		       const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk,
2177 		       bool check_ref_tag)
2178 {
2179 	struct _dif_sgl sgl;
2180 	uint32_t offset_blocks;
2181 	int rc;
2182 
2183 	_dif_sgl_init(&sgl, iovs, iovcnt);
2184 
2185 	if (!_dif_sgl_is_valid(&sgl, ctx->block_size * num_blocks)) {
2186 		SPDK_ERRLOG("Size of iovec array is not valid.\n");
2187 		return -EINVAL;
2188 	}
2189 
2190 	if (_dif_is_disabled(ctx->dif_type)) {
2191 		return 0;
2192 	}
2193 
2194 	if (!(ctx->dif_flags & SPDK_DIF_FLAGS_REFTAG_CHECK)) {
2195 		return 0;
2196 	}
2197 
2198 	for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
2199 		rc = _dif_remap_ref_tag(&sgl, offset_blocks, ctx, err_blk, check_ref_tag);
2200 		if (rc != 0) {
2201 			return rc;
2202 		}
2203 	}
2204 
2205 	return 0;
2206 }
2207 
2208 static int
2209 _dix_remap_ref_tag(struct _dif_sgl *md_sgl, uint32_t offset_blocks,
2210 		   const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk,
2211 		   bool check_ref_tag)
2212 {
2213 	uint64_t expected = 0, remapped;
2214 	uint8_t *md_buf;
2215 	struct spdk_dif *dif;
2216 
2217 	_dif_sgl_get_buf(md_sgl, &md_buf, NULL);
2218 
2219 	dif = (struct spdk_dif *)(md_buf + ctx->guard_interval);
2220 
2221 	if (_dif_ignore(dif, ctx)) {
2222 		goto end;
2223 	}
2224 
2225 	/* For type 1 and 2, the Reference Tag is incremented for each
2226 	 * subsequent logical block. For type 3, the Reference Tag
2227 	 * remains the same as the initialReference Tag.
2228 	 */
2229 	if (ctx->dif_type != SPDK_DIF_TYPE3) {
2230 		expected = ctx->init_ref_tag + ctx->ref_tag_offset + offset_blocks;
2231 		remapped = ctx->remapped_init_ref_tag + ctx->ref_tag_offset + offset_blocks;
2232 	} else {
2233 		remapped = ctx->remapped_init_ref_tag;
2234 	}
2235 
2236 	/* Verify the stored Reference Tag. */
2237 	if (check_ref_tag && !_dif_reftag_check(dif, ctx, expected, offset_blocks, err_blk)) {
2238 		return -1;
2239 	}
2240 
2241 	/* Update the stored Reference Tag to the remapped one. */
2242 	_dif_set_reftag(dif, remapped, ctx->dif_pi_format);
2243 
2244 end:
2245 	_dif_sgl_advance(md_sgl, ctx->md_size);
2246 
2247 	return 0;
2248 }
2249 
2250 int
2251 spdk_dix_remap_ref_tag(struct iovec *md_iov, uint32_t num_blocks,
2252 		       const struct spdk_dif_ctx *ctx,
2253 		       struct spdk_dif_error *err_blk,
2254 		       bool check_ref_tag)
2255 {
2256 	struct _dif_sgl md_sgl;
2257 	uint32_t offset_blocks;
2258 	int rc;
2259 
2260 	_dif_sgl_init(&md_sgl, md_iov, 1);
2261 
2262 	if (!_dif_sgl_is_valid(&md_sgl, ctx->md_size * num_blocks)) {
2263 		SPDK_ERRLOG("Size of metadata iovec array is not valid.\n");
2264 		return -EINVAL;
2265 	}
2266 
2267 	if (_dif_is_disabled(ctx->dif_type)) {
2268 		return 0;
2269 	}
2270 
2271 	if (!(ctx->dif_flags & SPDK_DIF_FLAGS_REFTAG_CHECK)) {
2272 		return 0;
2273 	}
2274 
2275 	for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
2276 		rc = _dix_remap_ref_tag(&md_sgl, offset_blocks, ctx, err_blk, check_ref_tag);
2277 		if (rc != 0) {
2278 			return rc;
2279 		}
2280 	}
2281 
2282 	return 0;
2283 }
2284