Branch data Line data Source code
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 : 7277810 : _dif_sgl_init(struct _dif_sgl *s, struct iovec *iovs, int iovcnt)
65 : : {
66 [ # # # # ]: 7277810 : s->iov = iovs;
67 [ # # # # ]: 7277810 : s->iovcnt = iovcnt;
68 [ # # # # ]: 7277810 : s->iov_offset = 0;
69 [ # # # # ]: 7277810 : s->total_size = 0;
70 : 7277810 : }
71 : :
72 : : static void
73 : 124269136 : _dif_sgl_advance(struct _dif_sgl *s, uint32_t step)
74 : : {
75 [ # # # # ]: 124269136 : s->iov_offset += step;
76 [ + + # # : 129465252 : while (s->iovcnt != 0) {
# # ]
77 [ + + # # : 124415697 : if (s->iov_offset < s->iov->iov_len) {
# # # # #
# # # #
# ]
78 : 119219581 : break;
79 : : }
80 : :
81 [ # # # # : 5196116 : s->iov_offset -= s->iov->iov_len;
# # # # #
# # # ]
82 [ # # # # ]: 5196116 : s->iov++;
83 [ # # # # ]: 5196116 : s->iovcnt--;
84 : : }
85 : 124269136 : }
86 : :
87 : : static inline void
88 : 96994470 : _dif_sgl_get_buf(struct _dif_sgl *s, uint8_t **_buf, uint32_t *_buf_len)
89 : : {
90 [ + - ]: 96994470 : if (_buf != NULL) {
91 [ # # # # : 96994470 : *_buf = (uint8_t *)s->iov->iov_base + s->iov_offset;
# # # # #
# # # # #
# # ]
92 : 16624 : }
93 [ + + ]: 96994470 : if (_buf_len != NULL) {
94 [ # # # # : 74405437 : *_buf_len = s->iov->iov_len - s->iov_offset;
# # # # #
# # # #
# ]
95 : 9883 : }
96 : 96994470 : }
97 : :
98 : : static inline bool
99 : 25333628 : _dif_sgl_append(struct _dif_sgl *s, uint8_t *data, uint32_t data_len)
100 : : {
101 [ + + # # : 25333628 : assert(s->iovcnt > 0);
# # # # ]
102 [ # # # # : 25333628 : s->iov->iov_base = data;
# # # # ]
103 [ # # # # : 25333628 : s->iov->iov_len = data_len;
# # # # ]
104 [ # # # # ]: 25333628 : s->total_size += data_len;
105 [ # # # # ]: 25333628 : s->iov++;
106 [ # # # # ]: 25333628 : s->iovcnt--;
107 : :
108 [ + + # # : 25333628 : if (s->iovcnt > 0) {
# # ]
109 : 25025114 : return true;
110 : : } else {
111 : 308514 : return false;
112 : : }
113 : 120 : }
114 : :
115 : : static inline bool
116 : 25277036 : _dif_sgl_append_split(struct _dif_sgl *dst, struct _dif_sgl *src, uint32_t data_len)
117 : : {
118 : 312 : uint8_t *buf;
119 : 312 : uint32_t buf_len;
120 : :
121 [ + + ]: 50302150 : while (data_len != 0) {
122 : 25333628 : _dif_sgl_get_buf(src, &buf, &buf_len);
123 [ + + ]: 25333628 : buf_len = spdk_min(buf_len, data_len);
124 : :
125 [ + + ]: 25333628 : if (!_dif_sgl_append(dst, buf, buf_len)) {
126 : 308514 : return false;
127 : : }
128 : :
129 : 25025114 : _dif_sgl_advance(src, buf_len);
130 : 25025114 : data_len -= buf_len;
131 : : }
132 : :
133 : 24968522 : return true;
134 : 104 : }
135 : :
136 : : /* This function must be used before starting iteration. */
137 : : static bool
138 : 2481719 : _dif_sgl_is_bytes_multiple(struct _dif_sgl *s, uint32_t bytes)
139 : : {
140 : : int i;
141 : :
142 [ + + # # : 4937662 : for (i = 0; i < s->iovcnt; i++) {
# # # # ]
143 [ + + + + : 2485967 : if (s->iov[i].iov_len % bytes) {
# # # # #
# # # #
# ]
144 : 30024 : return false;
145 : : }
146 : 1660 : }
147 : :
148 : 2451695 : return true;
149 : 1053 : }
150 : :
151 : : /* This function must be used before starting iteration. */
152 : : static bool
153 : 5642634 : _dif_sgl_is_valid(struct _dif_sgl *s, uint32_t bytes)
154 : : {
155 : 5642634 : uint64_t total = 0;
156 : : int i;
157 : :
158 [ + + # # : 11659548 : for (i = 0; i < s->iovcnt; i++) {
# # # # ]
159 [ # # # # : 6016914 : total += s->iov[i].iov_len;
# # # # #
# ]
160 : 6882 : }
161 : :
162 : 5642634 : return total >= bytes;
163 : : }
164 : :
165 : : static void
166 : 288 : _dif_sgl_copy(struct _dif_sgl *to, struct _dif_sgl *from)
167 : : {
168 [ - + - + ]: 288 : memcpy(to, from, sizeof(struct _dif_sgl));
169 : 288 : }
170 : :
171 : : static bool
172 : 2344290 : _dif_is_disabled(enum spdk_dif_type dif_type)
173 : : {
174 [ + + ]: 2344290 : if (dif_type == SPDK_DIF_DISABLE) {
175 : 140832 : return true;
176 : : } else {
177 : 2203458 : return false;
178 : : }
179 : 926 : }
180 : :
181 : : static inline size_t
182 : 62286024 : _dif_size(enum spdk_dif_pi_format dif_pi_format)
183 : : {
184 : : uint8_t size;
185 : :
186 [ + + ]: 62286024 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
187 : 62275732 : size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g16);
188 [ + + ]: 11723 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
189 : 5236 : size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g32);
190 : 1302 : } else {
191 : 5056 : size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g64);
192 : : }
193 : :
194 : 62286024 : return size;
195 : : }
196 : :
197 : : static uint32_t
198 : 1326309 : _get_guard_interval(uint32_t block_size, uint32_t md_size, bool dif_loc, bool md_interleave,
199 : : size_t dif_size)
200 : : {
201 [ + + # # ]: 1326309 : 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 [ + + # # ]: 1324702 : if (md_interleave) {
208 : 1183590 : return block_size - dif_size;
209 : : } else {
210 : 141112 : 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 [ + + # # ]: 1607 : if (md_interleave) {
218 : 1245 : return block_size - md_size;
219 : : } else {
220 : 362 : return 0;
221 : : }
222 : : }
223 : 540 : }
224 : :
225 : : static inline uint8_t
226 : 720 : _dif_guard_size(enum spdk_dif_pi_format dif_pi_format)
227 : : {
228 : : uint8_t size;
229 : :
230 [ + + ]: 720 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
231 : 240 : size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g16.guard);
232 [ + + ]: 540 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
233 : 240 : size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g32.guard);
234 : 60 : } else {
235 : 240 : size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g64.guard);
236 : : }
237 : :
238 : 720 : return size;
239 : : }
240 : :
241 : : static inline void
242 : 20887466 : _dif_set_guard(struct spdk_dif *dif, uint64_t guard, enum spdk_dif_pi_format dif_pi_format)
243 : : {
244 [ + + ]: 20887466 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
245 [ # # # # : 20881810 : to_be16(&(dif->g16.guard), (uint16_t)guard);
# # ]
246 [ + + ]: 6553 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
247 [ # # # # : 2872 : to_be32(&(dif->g32.guard), (uint32_t)guard);
# # ]
248 : 718 : } else {
249 [ # # # # : 2784 : to_be64(&(dif->g64.guard), guard);
# # ]
250 : : }
251 : 20887466 : }
252 : :
253 : : static inline uint64_t
254 : 17271971 : _dif_get_guard(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
255 : : {
256 : : uint64_t guard;
257 : :
258 [ + + ]: 17271971 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
259 [ # # # # : 17269063 : guard = (uint64_t)from_be16(&(dif->g16.guard));
# # ]
260 [ + + ]: 3769 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
261 [ # # # # : 1452 : guard = (uint64_t)from_be32(&(dif->g32.guard));
# # ]
262 : 401 : } else {
263 [ # # # # : 1456 : guard = from_be64(&(dif->g64.guard));
# # ]
264 : : }
265 : :
266 : 17271971 : return guard;
267 : : }
268 : :
269 : : static inline uint64_t
270 : 40290900 : _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 [ + + ]: 40290900 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
276 : 40280776 : guard = (uint64_t)spdk_crc16_t10dif((uint16_t)guard_seed, buf, buf_len);
277 [ + + ]: 12897 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
278 : 5120 : guard = (uint64_t)spdk_crc32c_nvme(buf, buf_len, guard_seed);
279 : 1329 : } else {
280 : 5004 : guard = spdk_crc64_nvme(buf, buf_len, guard_seed);
281 : : }
282 : :
283 : 40290900 : return guard;
284 : : }
285 : :
286 : : static uint64_t
287 : 19876190 : 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 : 19876190 : uint64_t guard = guard_seed;
291 : 1728 : uint32_t offset, end, buf_len;
292 : 1728 : uint8_t *buf;
293 : :
294 : 19876190 : offset = start;
295 [ + + # # : 19876190 : end = start + spdk_min(len, ctx->guard_interval - start);
# # # # #
# ]
296 : :
297 [ + + ]: 39809996 : while (offset < end) {
298 : 19933806 : _dif_sgl_get_buf(sgl, &buf, &buf_len);
299 [ + + ]: 19933806 : buf_len = spdk_min(buf_len, end - offset);
300 : :
301 [ + + # # : 19933806 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
302 [ # # # # ]: 19933806 : guard = _dif_generate_guard(guard, buf, buf_len, ctx->dif_pi_format);
303 : 843 : }
304 : :
305 : 19933806 : _dif_sgl_advance(sgl, buf_len);
306 : 19933806 : offset += buf_len;
307 : : }
308 : :
309 : 19876190 : return guard;
310 : : }
311 : :
312 : : static inline uint64_t
313 : 2224923 : _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 [ + + ]: 2224923 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
319 : 2222635 : guard = (uint64_t)spdk_crc16_t10dif_copy((uint16_t)guard_seed, dst, src, buf_len);
320 [ + + ]: 2562 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
321 [ - + - + ]: 1144 : memcpy(dst, src, buf_len);
322 : 1144 : guard = (uint64_t)spdk_crc32c_nvme(src, buf_len, guard_seed);
323 : 296 : } else {
324 [ - + - + ]: 1144 : memcpy(dst, src, buf_len);
325 : 1144 : guard = spdk_crc64_nvme(src, buf_len, guard_seed);
326 : : }
327 : :
328 : 2224923 : return guard;
329 : : }
330 : :
331 : : static uint64_t
332 : 528 : _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 : 528 : uint32_t offset = 0, src_len, dst_len, buf_len;
337 : 402 : uint8_t *src, *dst;
338 : :
339 [ + + ]: 1592 : while (offset < data_len) {
340 : 1064 : _dif_sgl_get_buf(src_sgl, &src, &src_len);
341 : 1064 : _dif_sgl_get_buf(dst_sgl, &dst, &dst_len);
342 [ + + ]: 1064 : buf_len = spdk_min(src_len, dst_len);
343 [ + + ]: 1064 : buf_len = spdk_min(buf_len, data_len - offset);
344 : :
345 : 1064 : guard = _dif_generate_guard_copy(guard, dst, src, buf_len, dif_pi_format);
346 : :
347 : 1064 : _dif_sgl_advance(src_sgl, buf_len);
348 : 1064 : _dif_sgl_advance(dst_sgl, buf_len);
349 : 1064 : offset += buf_len;
350 : : }
351 : :
352 : 528 : return guard;
353 : : }
354 : :
355 : : static void
356 : 0 : _data_copy_split(struct _dif_sgl *dst_sgl, struct _dif_sgl *src_sgl, uint32_t data_len)
357 : : {
358 : 0 : uint32_t offset = 0, src_len, dst_len, buf_len;
359 : 0 : uint8_t *src, *dst;
360 : :
361 [ # # ]: 0 : while (offset < data_len) {
362 : 0 : _dif_sgl_get_buf(src_sgl, &src, &src_len);
363 : 0 : _dif_sgl_get_buf(dst_sgl, &dst, &dst_len);
364 [ # # ]: 0 : buf_len = spdk_min(src_len, dst_len);
365 [ # # ]: 0 : buf_len = spdk_min(buf_len, data_len - offset);
366 : :
367 [ # # # # ]: 0 : memcpy(dst, src, buf_len);
368 : :
369 : 0 : _dif_sgl_advance(src_sgl, buf_len);
370 : 0 : _dif_sgl_advance(dst_sgl, buf_len);
371 : 0 : offset += buf_len;
372 : : }
373 : 0 : }
374 : :
375 : : static inline uint8_t
376 : 480 : _dif_apptag_offset(enum spdk_dif_pi_format dif_pi_format)
377 : : {
378 : 480 : return _dif_guard_size(dif_pi_format);
379 : : }
380 : :
381 : : static inline uint8_t
382 : 480 : _dif_apptag_size(void)
383 : : {
384 : 480 : return SPDK_SIZEOF_MEMBER(struct spdk_dif, g16.app_tag);
385 : : }
386 : :
387 : : static inline void
388 : 20887466 : _dif_set_apptag(struct spdk_dif *dif, uint16_t app_tag, enum spdk_dif_pi_format dif_pi_format)
389 : : {
390 [ + + ]: 20887466 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
391 [ # # # # : 20881810 : to_be16(&(dif->g16.app_tag), app_tag);
# # ]
392 [ + + ]: 6553 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
393 [ # # # # : 2872 : to_be16(&(dif->g32.app_tag), app_tag);
# # ]
394 : 718 : } else {
395 [ # # # # : 2784 : to_be16(&(dif->g64.app_tag), app_tag);
# # ]
396 : : }
397 : 20887466 : }
398 : :
399 : : static inline uint16_t
400 : 21908686 : _dif_get_apptag(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
401 : : {
402 : : uint16_t app_tag;
403 : :
404 [ + + ]: 21908686 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
405 [ # # # # : 21901562 : app_tag = from_be16(&(dif->g16.app_tag));
# # ]
406 [ + + ]: 9095 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
407 [ # # # # : 3564 : app_tag = from_be16(&(dif->g32.app_tag));
# # ]
408 : 967 : } else {
409 [ # # # # : 3560 : app_tag = from_be16(&(dif->g64.app_tag));
# # ]
410 : : }
411 : :
412 : 21908686 : return app_tag;
413 : : }
414 : :
415 : : static inline bool
416 : 17275513 : _dif_apptag_ignore(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
417 : : {
418 : 17275513 : return _dif_get_apptag(dif, dif_pi_format) == SPDK_DIF_APPTAG_IGNORE;
419 : : }
420 : :
421 : : static inline uint8_t
422 : 240 : _dif_reftag_offset(enum spdk_dif_pi_format dif_pi_format)
423 : : {
424 : : uint8_t offset;
425 : :
426 [ + + ]: 240 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
427 : 80 : offset = _dif_apptag_offset(dif_pi_format) + _dif_apptag_size();
428 [ + + ]: 180 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
429 : 100 : offset = _dif_apptag_offset(dif_pi_format) + _dif_apptag_size()
430 : 20 : + SPDK_SIZEOF_MEMBER(struct spdk_dif, g32.stor_ref_space_p1);
431 : 20 : } else {
432 : 80 : offset = _dif_apptag_offset(dif_pi_format) + _dif_apptag_size();
433 : : }
434 : :
435 : 240 : return offset;
436 : : }
437 : :
438 : : static inline uint8_t
439 : 240 : _dif_reftag_size(enum spdk_dif_pi_format dif_pi_format)
440 : : {
441 : : uint8_t size;
442 : :
443 [ + + ]: 240 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
444 : 80 : size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g16.stor_ref_space);
445 [ + + ]: 180 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
446 : 80 : size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g32.stor_ref_space_p2);
447 : 20 : } else {
448 : 80 : 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 : 240 : return size;
453 : : }
454 : :
455 : : static inline void
456 : 20888554 : _dif_set_reftag(struct spdk_dif *dif, uint64_t ref_tag, enum spdk_dif_pi_format dif_pi_format)
457 : : {
458 [ + + ]: 20888554 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
459 [ # # # # : 20882514 : to_be32(&(dif->g16.stor_ref_space), (uint32_t)ref_tag);
# # ]
460 [ + + ]: 7113 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
461 [ # # # # : 3064 : to_be64(&(dif->g32.stor_ref_space_p2), ref_tag);
# # ]
462 : 766 : } else {
463 [ # # # # : 2976 : to_be16(&(dif->g64.stor_ref_space_p1), (uint16_t)(ref_tag >> 32));
# # # # ]
464 [ # # # # : 2976 : to_be32(&(dif->g64.stor_ref_space_p2), (uint32_t)ref_tag);
# # ]
465 : : }
466 : 20888554 : }
467 : :
468 : : static inline uint64_t
469 : 10036108 : _dif_get_reftag(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
470 : : {
471 : : uint64_t ref_tag;
472 : :
473 [ + + ]: 10036108 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
474 [ # # # # : 10033188 : ref_tag = (uint64_t)from_be32(&(dif->g16.stor_ref_space));
# # ]
475 [ + + ]: 3790 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
476 [ # # # # : 1460 : ref_tag = from_be64(&(dif->g32.stor_ref_space_p2));
# # ]
477 : 403 : } else {
478 [ # # # # : 1460 : ref_tag = (uint64_t)from_be16(&(dif->g64.stor_ref_space_p1));
# # ]
479 [ # # ]: 1460 : ref_tag <<= 32;
480 [ # # # # : 1460 : ref_tag |= (uint64_t)from_be32(&(dif->g64.stor_ref_space_p2));
# # ]
481 : : }
482 : :
483 : 10036108 : return ref_tag;
484 : : }
485 : :
486 : : static inline bool
487 : 10035818 : _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 : 10035818 : _ref_tag = _dif_get_reftag(dif, dif_pi_format);
494 : :
495 [ + + ]: 10035818 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
496 : 10033074 : match = (_ref_tag == (ref_tag & REFTAG_MASK_16));
497 [ + + ]: 3590 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
498 : 1372 : match = (_ref_tag == ref_tag);
499 : 381 : } else {
500 : 1372 : match = (_ref_tag == (ref_tag & REFTAG_MASK_64));
501 : : }
502 : :
503 [ # # ]: 10035818 : return match;
504 : : }
505 : :
506 : : static inline bool
507 : 28 : _dif_reftag_ignore(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
508 : : {
509 : 28 : return _dif_reftag_match(dif, REFTAG_MASK_32, dif_pi_format);
510 : : }
511 : :
512 : : static bool
513 : 17275513 : _dif_ignore(struct spdk_dif *dif, const struct spdk_dif_ctx *ctx)
514 : : {
515 [ + + + # : 17275513 : switch (ctx->dif_type) {
# # # ]
516 : 10037096 : 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 [ + + # # : 10039463 : if (_dif_apptag_ignore(dif, ctx->dif_pi_format)) {
# # ]
522 : 112 : return true;
523 : : }
524 : 10039351 : break;
525 : 7236043 : 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 [ + + + + : 7236071 : if (_dif_apptag_ignore(dif, ctx->dif_pi_format) &&
# # # # ]
532 [ # # # # ]: 28 : _dif_reftag_ignore(dif, ctx->dif_pi_format)) {
533 : 16 : return true;
534 : : }
535 : 7236034 : break;
536 : 0 : default:
537 : 0 : break;
538 : : }
539 : :
540 : 17275385 : return false;
541 : 2374 : }
542 : :
543 : : static bool
544 : 1326317 : _dif_pi_format_is_valid(enum spdk_dif_pi_format dif_pi_format)
545 : : {
546 [ + + ]: 1326317 : switch (dif_pi_format) {
547 : 1325772 : case SPDK_DIF_PI_FORMAT_16:
548 : : case SPDK_DIF_PI_FORMAT_32:
549 : : case SPDK_DIF_PI_FORMAT_64:
550 : 1326313 : return true;
551 : 3 : default:
552 : 4 : return false;
553 : : }
554 : 542 : }
555 : :
556 : : static bool
557 : 1326321 : _dif_type_is_valid(enum spdk_dif_type dif_type)
558 : : {
559 [ + + ]: 1326321 : switch (dif_type) {
560 : 1325775 : case SPDK_DIF_DISABLE:
561 : : case SPDK_DIF_TYPE1:
562 : : case SPDK_DIF_TYPE2:
563 : : case SPDK_DIF_TYPE3:
564 : 1326317 : return true;
565 : 3 : default:
566 : 4 : return false;
567 : : }
568 : 543 : }
569 : :
570 : : int
571 : 1326301 : 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 : 1326301 : enum spdk_dif_pi_format dif_pi_format = SPDK_DIF_PI_FORMAT_16;
578 : :
579 [ + + ]: 1326301 : if (opts != NULL) {
580 [ + + # # : 1326301 : if (!_dif_pi_format_is_valid(opts->dif_pi_format)) {
# # ]
581 : 0 : SPDK_ERRLOG("No valid DIF PI format provided.\n");
582 : 0 : return -EINVAL;
583 : : }
584 : :
585 [ # # # # ]: 1326301 : dif_pi_format = opts->dif_pi_format;
586 : 538 : }
587 : :
588 [ + + ]: 1326301 : if (!_dif_type_is_valid(dif_type)) {
589 : 0 : SPDK_ERRLOG("No valid DIF type was provided.\n");
590 : 0 : return -EINVAL;
591 : : }
592 : :
593 [ + + ]: 1326301 : if (md_size < _dif_size(dif_pi_format)) {
594 : 44 : SPDK_ERRLOG("Metadata size is smaller than DIF size.\n");
595 : 44 : return -EINVAL;
596 : : }
597 : :
598 [ + + # # ]: 1326257 : if (md_interleave) {
599 [ - + ]: 1184763 : if (block_size < md_size) {
600 : 0 : SPDK_ERRLOG("Block size is smaller than DIF size.\n");
601 : 0 : return -EINVAL;
602 : : }
603 : 1184763 : data_block_size = block_size - md_size;
604 : 406 : } else {
605 : 141494 : data_block_size = block_size;
606 : : }
607 : :
608 [ + + ]: 1326257 : if (data_block_size == 0) {
609 : 8 : SPDK_ERRLOG("Zero data block size is not allowed\n");
610 : 8 : return -EINVAL;
611 : : }
612 : :
613 [ + + ]: 1326249 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
614 [ + + # # ]: 1324925 : if ((data_block_size % 512) != 0) {
615 : 0 : SPDK_ERRLOG("Data block size should be a multiple of 512B\n");
616 : 0 : return -EINVAL;
617 : : }
618 : 194 : } else {
619 [ + + # # ]: 1324 : if ((data_block_size % 4096) != 0) {
620 : 24 : SPDK_ERRLOG("Data block size should be a multiple of 4kB\n");
621 : 24 : return -EINVAL;
622 : : }
623 : : }
624 : :
625 [ # # # # ]: 1326225 : ctx->block_size = block_size;
626 [ # # # # ]: 1326225 : ctx->md_size = md_size;
627 [ # # # # : 1326225 : ctx->md_interleave = md_interleave;
# # ]
628 [ # # # # ]: 1326225 : ctx->dif_pi_format = dif_pi_format;
629 [ # # # # : 1326744 : ctx->guard_interval = _get_guard_interval(block_size, md_size, dif_loc, md_interleave,
# # # # ]
630 [ # # # # ]: 1326225 : _dif_size(ctx->dif_pi_format));
631 [ # # # # ]: 1326225 : ctx->dif_type = dif_type;
632 [ # # # # ]: 1326225 : ctx->dif_flags = dif_flags;
633 [ # # # # ]: 1326225 : ctx->init_ref_tag = init_ref_tag;
634 [ # # # # ]: 1326225 : ctx->apptag_mask = apptag_mask;
635 [ # # # # ]: 1326225 : ctx->app_tag = app_tag;
636 [ # # # # ]: 1326225 : ctx->data_offset = data_offset;
637 [ - + # # : 1326225 : ctx->ref_tag_offset = data_offset / data_block_size;
# # ]
638 [ # # # # ]: 1326225 : ctx->last_guard = guard_seed;
639 [ # # # # ]: 1326225 : ctx->guard_seed = guard_seed;
640 [ # # # # ]: 1326225 : ctx->remapped_init_ref_tag = 0;
641 : :
642 : 1326225 : return 0;
643 : 538 : }
644 : :
645 : : void
646 : 1043798 : spdk_dif_ctx_set_data_offset(struct spdk_dif_ctx *ctx, uint32_t data_offset)
647 : : {
648 : : uint32_t data_block_size;
649 : :
650 [ + + + - : 1043798 : if (ctx->md_interleave) {
# # # # ]
651 [ # # # # : 1043798 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
652 : 42 : } else {
653 [ # # # # ]: 0 : data_block_size = ctx->block_size;
654 : : }
655 : :
656 [ # # # # ]: 1043798 : ctx->data_offset = data_offset;
657 [ - + # # : 1043798 : ctx->ref_tag_offset = data_offset / data_block_size;
# # ]
658 : 1043798 : }
659 : :
660 : : void
661 : 96 : spdk_dif_ctx_set_remapped_init_ref_tag(struct spdk_dif_ctx *ctx,
662 : : uint32_t remapped_init_ref_tag)
663 : : {
664 [ # # # # ]: 96 : ctx->remapped_init_ref_tag = remapped_init_ref_tag;
665 : 96 : }
666 : :
667 : : static void
668 : 20887466 : _dif_generate(void *_dif, uint64_t guard, uint32_t offset_blocks,
669 : : const struct spdk_dif_ctx *ctx)
670 : : {
671 : 20887466 : struct spdk_dif *dif = _dif;
672 : : uint64_t ref_tag;
673 : :
674 [ + + # # : 20887466 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
675 [ # # # # ]: 20885282 : _dif_set_guard(dif, guard, ctx->dif_pi_format);
676 : 1873 : } else {
677 [ # # # # ]: 2184 : _dif_set_guard(dif, 0, ctx->dif_pi_format);
678 : : }
679 : :
680 [ + + # # : 20887466 : if (ctx->dif_flags & SPDK_DIF_FLAGS_APPTAG_CHECK) {
# # # # ]
681 [ # # # # : 8247036 : _dif_set_apptag(dif, ctx->app_tag, ctx->dif_pi_format);
# # # # ]
682 : 1865 : } else {
683 [ # # # # ]: 12640430 : _dif_set_apptag(dif, 0, ctx->dif_pi_format);
684 : : }
685 : :
686 [ + + # # : 20887466 : if (ctx->dif_flags & SPDK_DIF_FLAGS_REFTAG_CHECK) {
# # # # ]
687 : : /* For type 1 and 2, the reference tag is incremented for each
688 : : * subsequent logical block. For type 3, the reference tag
689 : : * remains the same as the initial reference tag.
690 : : */
691 [ + + # # : 13649408 : if (ctx->dif_type != SPDK_DIF_TYPE3) {
# # ]
692 [ # # # # : 13649380 : ref_tag = ctx->init_ref_tag + ctx->ref_tag_offset + offset_blocks;
# # # # ]
693 : 1855 : } else {
694 [ # # # # : 28 : ref_tag = ctx->init_ref_tag + ctx->ref_tag_offset;
# # # # ]
695 : : }
696 : :
697 : : /* Overwrite reference tag if initialization reference tag is SPDK_DIF_REFTAG_IGNORE */
698 [ + + # # : 13649408 : if (ctx->init_ref_tag == SPDK_DIF_REFTAG_IGNORE) {
# # ]
699 [ + + # # : 8 : if (ctx->dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
# # ]
700 : 8 : ref_tag = REFTAG_MASK_16;
701 [ # # # # : 2 : } else if (ctx->dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
# # ]
702 : 0 : ref_tag = REFTAG_MASK_32;
703 : 0 : } else {
704 : 0 : ref_tag = REFTAG_MASK_64;
705 : : }
706 : 2 : }
707 : :
708 [ # # # # ]: 13649408 : _dif_set_reftag(dif, ref_tag, ctx->dif_pi_format);
709 : 1862 : } else {
710 [ # # # # ]: 7238058 : _dif_set_reftag(dif, 0, ctx->dif_pi_format);
711 : : }
712 : 20887466 : }
713 : :
714 : : static void
715 : 1315531 : dif_generate(struct _dif_sgl *sgl, uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
716 : : {
717 : : uint32_t offset_blocks;
718 : 227515 : uint8_t *buf;
719 : 1315531 : uint64_t guard = 0;
720 : :
721 [ + + ]: 12737485 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
722 : 11421954 : _dif_sgl_get_buf(sgl, &buf, NULL);
723 : :
724 [ + + # # : 11421954 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
725 [ # # # # : 11421402 : guard = _dif_generate_guard(ctx->guard_seed, buf, ctx->guard_interval, ctx->dif_pi_format);
# # # # #
# # # ]
726 : 475 : }
727 : :
728 [ # # # # : 11421954 : _dif_generate(buf + ctx->guard_interval, guard, offset_blocks, ctx);
# # ]
729 : :
730 [ # # # # ]: 11421954 : _dif_sgl_advance(sgl, ctx->block_size);
731 : 577 : }
732 : 1315531 : }
733 : :
734 : : static void
735 : 7236776 : dif_store_split(struct _dif_sgl *sgl, struct spdk_dif *dif,
736 : : const struct spdk_dif_ctx *ctx)
737 : : {
738 : 7236776 : uint32_t offset = 0, rest_md_len, buf_len;
739 : 894 : uint8_t *buf;
740 : :
741 [ # # # # : 7236776 : rest_md_len = ctx->block_size - ctx->guard_interval;
# # # # ]
742 : :
743 [ + + ]: 14474368 : while (offset < rest_md_len) {
744 : 7237592 : _dif_sgl_get_buf(sgl, &buf, &buf_len);
745 : :
746 [ + + # # : 7237592 : if (offset < _dif_size(ctx->dif_pi_format)) {
# # ]
747 [ + + # # : 7237128 : buf_len = spdk_min(buf_len, _dif_size(ctx->dif_pi_format) - offset);
# # # # #
# ]
748 [ - + - + : 7237128 : memcpy(buf, (uint8_t *)dif + offset, buf_len);
# # ]
749 : 386 : } else {
750 [ + + ]: 464 : buf_len = spdk_min(buf_len, rest_md_len - offset);
751 : : }
752 : :
753 : 7237592 : _dif_sgl_advance(sgl, buf_len);
754 : 7237592 : offset += buf_len;
755 : : }
756 : 7236776 : }
757 : :
758 : : static uint64_t
759 : 7236632 : _dif_generate_split(struct _dif_sgl *sgl, uint32_t offset_in_block, uint32_t data_len,
760 : : uint64_t guard, uint32_t offset_blocks, const struct spdk_dif_ctx *ctx)
761 : : {
762 : 7236632 : struct spdk_dif dif = {};
763 : :
764 [ + + # # : 7236632 : assert(offset_in_block < ctx->guard_interval);
# # # # ]
765 [ + + - + : 7236632 : assert(offset_in_block + data_len < ctx->guard_interval ||
# # # # #
# # # #
# ]
766 : : offset_in_block + data_len == ctx->block_size);
767 : :
768 : : /* Compute CRC over split logical block data. */
769 : 7236632 : guard = dif_generate_guard_split(guard, sgl, offset_in_block, data_len, ctx);
770 : :
771 [ + + # # : 7236632 : if (offset_in_block + data_len < ctx->guard_interval) {
# # ]
772 : 156 : return guard;
773 : : }
774 : :
775 : : /* If a whole logical block data is parsed, generate DIF
776 : : * and save it to the temporary DIF area.
777 : : */
778 : 7236476 : _dif_generate(&dif, guard, offset_blocks, ctx);
779 : :
780 : : /* Copy generated DIF field to the split DIF field, and then
781 : : * skip metadata field after DIF field (if any).
782 : : */
783 : 7236476 : dif_store_split(sgl, &dif, ctx);
784 : :
785 [ + + # # : 7236476 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
786 [ # # # # ]: 7236476 : guard = ctx->guard_seed;
787 : 223 : }
788 : :
789 : 7236476 : return guard;
790 : 262 : }
791 : :
792 : : static void
793 : 28872 : dif_generate_split(struct _dif_sgl *sgl, uint32_t num_blocks,
794 : : const struct spdk_dif_ctx *ctx)
795 : : {
796 : : uint32_t offset_blocks;
797 : 28872 : uint64_t guard = 0;
798 : :
799 [ + + # # : 28872 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
800 [ # # # # ]: 28872 : guard = ctx->guard_seed;
801 : 152 : }
802 : :
803 [ + + ]: 7265164 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
804 [ # # # # ]: 7236292 : _dif_generate_split(sgl, 0, ctx->block_size, guard, offset_blocks, ctx);
805 : 177 : }
806 : 28872 : }
807 : :
808 : : int
809 : 1344371 : spdk_dif_generate(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
810 : : const struct spdk_dif_ctx *ctx)
811 : : {
812 : 227947 : struct _dif_sgl sgl;
813 : :
814 : 1344371 : _dif_sgl_init(&sgl, iovs, iovcnt);
815 : :
816 [ - + # # : 1344371 : if (!_dif_sgl_is_valid(&sgl, ctx->block_size * num_blocks)) {
# # ]
817 : 0 : SPDK_ERRLOG("Size of iovec array is not valid.\n");
818 : 0 : return -EINVAL;
819 : : }
820 : :
821 [ + + # # : 1344371 : if (_dif_is_disabled(ctx->dif_type)) {
# # ]
822 : 4 : return 0;
823 : : }
824 : :
825 [ + + # # : 1344367 : if (_dif_sgl_is_bytes_multiple(&sgl, ctx->block_size)) {
# # ]
826 : 1315495 : dif_generate(&sgl, num_blocks, ctx);
827 : 91 : } else {
828 : 28872 : dif_generate_split(&sgl, num_blocks, ctx);
829 : : }
830 : :
831 : 1344367 : return 0;
832 : 244 : }
833 : :
834 : : static void
835 : 1099 : _dif_error_set(struct spdk_dif_error *err_blk, uint8_t err_type,
836 : : uint64_t expected, uint64_t actual, uint32_t err_offset)
837 : : {
838 [ + + ]: 1099 : if (err_blk) {
839 [ # # # # ]: 1063 : err_blk->err_type = err_type;
840 [ # # # # ]: 1063 : err_blk->expected = expected;
841 [ # # # # ]: 1063 : err_blk->actual = actual;
842 [ # # # # ]: 1063 : err_blk->err_offset = err_offset;
843 : 253 : }
844 : 1099 : }
845 : :
846 : : static bool
847 : 17274056 : _dif_reftag_check(struct spdk_dif *dif, const struct spdk_dif_ctx *ctx,
848 : : uint64_t expected_reftag, uint32_t offset_blocks, struct spdk_dif_error *err_blk)
849 : : {
850 : : uint64_t reftag;
851 : :
852 [ + + # # : 17274056 : if (ctx->dif_flags & SPDK_DIF_FLAGS_REFTAG_CHECK) {
# # # # ]
853 [ + - - # : 10035790 : switch (ctx->dif_type) {
# # # ]
854 : 10034189 : case SPDK_DIF_TYPE1:
855 : : case SPDK_DIF_TYPE2:
856 : : /* Compare the DIF Reference Tag field to the passed Reference Tag.
857 : : * The passed Reference Tag will be the least significant 4 bytes
858 : : * or 8 bytes (depending on the PI format)
859 : : * of the LBA when Type 1 is used, and application specific value
860 : : * if Type 2 is used.
861 : : */
862 [ + + # # : 10035790 : if (!_dif_reftag_match(dif, expected_reftag, ctx->dif_pi_format)) {
# # ]
863 [ # # # # ]: 282 : reftag = _dif_get_reftag(dif, ctx->dif_pi_format);
864 : 348 : _dif_error_set(err_blk, SPDK_DIF_REFTAG_ERROR, expected_reftag,
865 : 66 : reftag, offset_blocks);
866 : 282 : SPDK_ERRLOG("Failed to compare Ref Tag: LBA=%" PRIu64 "," \
867 : : " Expected=%lx, Actual=%lx\n",
868 : : expected_reftag, expected_reftag, reftag);
869 : 282 : return false;
870 : : }
871 : 10035508 : break;
872 : 0 : case SPDK_DIF_TYPE3:
873 : : /* For Type 3, computed Reference Tag remains unchanged.
874 : : * Hence ignore the Reference Tag field.
875 : : */
876 : 0 : break;
877 : 0 : default:
878 : 0 : break;
879 : : }
880 : 1535 : }
881 : :
882 : 17273774 : return true;
883 : 2042 : }
884 : :
885 : : static int
886 : 17274425 : _dif_verify(void *_dif, uint64_t guard, uint32_t offset_blocks,
887 : : const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk)
888 : : {
889 : 17274425 : struct spdk_dif *dif = _dif;
890 : : uint64_t _guard;
891 : : uint16_t _app_tag;
892 : : uint64_t ref_tag;
893 : :
894 [ + + ]: 17274425 : if (_dif_ignore(dif, ctx)) {
895 : 128 : return 0;
896 : : }
897 : :
898 : : /* For type 1 and 2, the reference tag is incremented for each
899 : : * subsequent logical block. For type 3, the reference tag
900 : : * remains the same as the initial reference tag.
901 : : */
902 [ + + # # : 17274297 : if (ctx->dif_type != SPDK_DIF_TYPE3) {
# # ]
903 [ # # # # : 10038263 : ref_tag = ctx->init_ref_tag + ctx->ref_tag_offset + offset_blocks;
# # # # ]
904 : 2091 : } else {
905 [ # # # # : 7236034 : ref_tag = ctx->init_ref_tag + ctx->ref_tag_offset;
# # # # ]
906 : : }
907 : :
908 [ + + # # : 17274297 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
909 : : /* Compare the DIF Guard field to the CRC computed over the logical
910 : : * block data.
911 : : */
912 [ # # # # ]: 17271931 : _guard = _dif_get_guard(dif, ctx->dif_pi_format);
913 [ + + ]: 17271931 : if (_guard != guard) {
914 : 635 : _dif_error_set(err_blk, SPDK_DIF_GUARD_ERROR, _guard, guard,
915 : 124 : offset_blocks);
916 : 511 : SPDK_ERRLOG("Failed to compare Guard: LBA=%" PRIu64 "," \
917 : : " Expected=%lx, Actual=%lx\n",
918 : : ref_tag, _guard, guard);
919 : 511 : return -1;
920 : : }
921 : 1530 : }
922 : :
923 [ + + # # : 17273786 : if (ctx->dif_flags & SPDK_DIF_FLAGS_APPTAG_CHECK) {
# # # # ]
924 : : /* Compare unmasked bits in the DIF Application Tag field to the
925 : : * passed Application Tag.
926 : : */
927 [ # # # # ]: 4633165 : _app_tag = _dif_get_apptag(dif, ctx->dif_pi_format);
928 [ + + # # : 4633165 : if ((_app_tag & ctx->apptag_mask) != (ctx->app_tag & ctx->apptag_mask)) {
# # # # #
# # # #
# ]
929 [ # # # # ]: 378 : _dif_error_set(err_blk, SPDK_DIF_APPTAG_ERROR, ctx->app_tag,
930 [ # # # # ]: 306 : (_app_tag & ctx->apptag_mask), offset_blocks);
931 [ # # # # : 306 : SPDK_ERRLOG("Failed to compare App Tag: LBA=%" PRIu64 "," \
# # # # ]
932 : : " Expected=%x, Actual=%x\n",
933 : : ref_tag, ctx->app_tag, (_app_tag & ctx->apptag_mask));
934 : 306 : return -1;
935 : : }
936 : 1456 : }
937 : :
938 [ + + ]: 17273480 : if (!_dif_reftag_check(dif, ctx, ref_tag, offset_blocks, err_blk)) {
939 : 282 : return -1;
940 : : }
941 : :
942 : 17273198 : return 0;
943 : 2102 : }
944 : :
945 : : static int
946 : 320314 : dif_verify(struct _dif_sgl *sgl, uint32_t num_blocks,
947 : : const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk)
948 : : {
949 : : uint32_t offset_blocks;
950 : : int rc;
951 : 107405 : uint8_t *buf;
952 : 320314 : uint64_t guard = 0;
953 : :
954 [ + + ]: 2880950 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
955 : 2560799 : _dif_sgl_get_buf(sgl, &buf, NULL);
956 : :
957 [ + + # # : 2560799 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
958 [ # # # # : 2560107 : guard = _dif_generate_guard(ctx->guard_seed, buf, ctx->guard_interval, ctx->dif_pi_format);
# # # # #
# # # ]
959 : 370 : }
960 : :
961 [ # # # # : 2560799 : rc = _dif_verify(buf + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
# # ]
962 [ + + ]: 2560799 : if (rc != 0) {
963 : 163 : return rc;
964 : : }
965 : :
966 [ # # # # ]: 2560636 : _dif_sgl_advance(sgl, ctx->block_size);
967 : 437 : }
968 : :
969 : 320151 : return 0;
970 : 105 : }
971 : :
972 : : static void
973 : 12639262 : dif_load_split(struct _dif_sgl *sgl, struct spdk_dif *dif,
974 : : const struct spdk_dif_ctx *ctx)
975 : : {
976 : 12639262 : uint32_t offset = 0, rest_md_len, buf_len;
977 : 720 : uint8_t *buf;
978 : :
979 [ # # # # : 12639262 : rest_md_len = ctx->block_size - ctx->guard_interval;
# # # # ]
980 : :
981 [ + + ]: 25279292 : while (offset < rest_md_len) {
982 : 12640030 : _dif_sgl_get_buf(sgl, &buf, &buf_len);
983 : :
984 [ + + # # : 12640030 : if (offset < _dif_size(ctx->dif_pi_format)) {
# # ]
985 [ + + # # : 12639602 : buf_len = spdk_min(buf_len, _dif_size(ctx->dif_pi_format) - offset);
# # # # #
# ]
986 [ - + - + : 12639602 : memcpy((uint8_t *)dif + offset, buf, buf_len);
# # ]
987 : 317 : } else {
988 [ + + ]: 428 : buf_len = spdk_min(buf_len, rest_md_len - offset);
989 : : }
990 : :
991 : 12640030 : _dif_sgl_advance(sgl, buf_len);
992 : 12640030 : offset += buf_len;
993 : : }
994 : 12639262 : }
995 : :
996 : : static int
997 : 12639094 : _dif_verify_split(struct _dif_sgl *sgl, uint32_t offset_in_block, uint32_t data_len,
998 : : uint64_t *_guard, uint32_t offset_blocks,
999 : : const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk)
1000 : : {
1001 [ # # ]: 12639094 : uint64_t guard = *_guard;
1002 : 12639094 : struct spdk_dif dif = {};
1003 : : int rc;
1004 : :
1005 [ + + # # ]: 12639094 : assert(_guard != NULL);
1006 [ + + # # : 12639094 : assert(offset_in_block < ctx->guard_interval);
# # # # ]
1007 [ + + - + : 12639094 : assert(offset_in_block + data_len < ctx->guard_interval ||
# # # # #
# # # #
# ]
1008 : : offset_in_block + data_len == ctx->block_size);
1009 : :
1010 : 12639094 : guard = dif_generate_guard_split(guard, sgl, offset_in_block, data_len, ctx);
1011 : :
1012 [ + + # # : 12639094 : if (offset_in_block + data_len < ctx->guard_interval) {
# # ]
1013 [ # # ]: 60 : *_guard = guard;
1014 : 60 : return 0;
1015 : : }
1016 : :
1017 : 12639034 : dif_load_split(sgl, &dif, ctx);
1018 : :
1019 : 12639034 : rc = _dif_verify(&dif, guard, offset_blocks, ctx, err_blk);
1020 [ + + ]: 12639034 : if (rc != 0) {
1021 : 480 : return rc;
1022 : : }
1023 : :
1024 [ + + # # : 12638554 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1025 [ # # # # ]: 12638554 : guard = ctx->guard_seed;
1026 : 61 : }
1027 : :
1028 [ # # ]: 12638554 : *_guard = guard;
1029 : 12638554 : return 0;
1030 : 196 : }
1031 : :
1032 : : static int
1033 : 600 : dif_verify_split(struct _dif_sgl *sgl, uint32_t num_blocks,
1034 : : const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk)
1035 : : {
1036 : : uint32_t offset_blocks;
1037 : 600 : uint64_t guard = 0;
1038 : : int rc;
1039 : :
1040 [ + + # # : 600 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1041 [ # # # # ]: 600 : guard = ctx->guard_seed;
1042 : 150 : }
1043 : :
1044 [ + + ]: 796 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1045 [ # # # # ]: 845 : rc = _dif_verify_split(sgl, 0, ctx->block_size, &guard, offset_blocks,
1046 : 169 : ctx, err_blk);
1047 [ + + ]: 676 : if (rc != 0) {
1048 : 480 : return rc;
1049 : : }
1050 : 49 : }
1051 : :
1052 : 120 : return 0;
1053 : 150 : }
1054 : :
1055 : : int
1056 : 320882 : spdk_dif_verify(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
1057 : : const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk)
1058 : : {
1059 : 107831 : struct _dif_sgl sgl;
1060 : :
1061 : 320882 : _dif_sgl_init(&sgl, iovs, iovcnt);
1062 : :
1063 [ - + # # : 320882 : if (!_dif_sgl_is_valid(&sgl, ctx->block_size * num_blocks)) {
# # ]
1064 : 0 : SPDK_ERRLOG("Size of iovec array is not valid.\n");
1065 : 0 : return -EINVAL;
1066 : : }
1067 : :
1068 [ + + # # : 320882 : if (_dif_is_disabled(ctx->dif_type)) {
# # ]
1069 : 4 : return 0;
1070 : : }
1071 : :
1072 [ + + # # : 320878 : if (_dif_sgl_is_bytes_multiple(&sgl, ctx->block_size)) {
# # ]
1073 : 320278 : return dif_verify(&sgl, num_blocks, ctx, err_blk);
1074 : : } else {
1075 : 600 : return dif_verify_split(&sgl, num_blocks, ctx, err_blk);
1076 : : }
1077 : 247 : }
1078 : :
1079 : : static uint32_t
1080 : 36 : dif_update_crc32c(struct _dif_sgl *sgl, uint32_t num_blocks,
1081 : : uint32_t crc32c, const struct spdk_dif_ctx *ctx)
1082 : : {
1083 : : uint32_t offset_blocks;
1084 : 27 : uint8_t *buf;
1085 : :
1086 [ + + ]: 180 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1087 : 144 : _dif_sgl_get_buf(sgl, &buf, NULL);
1088 : :
1089 [ # # # # : 144 : crc32c = spdk_crc32c_update(buf, ctx->block_size - ctx->md_size, crc32c);
# # # # ]
1090 : :
1091 [ # # # # ]: 144 : _dif_sgl_advance(sgl, ctx->block_size);
1092 : 36 : }
1093 : :
1094 : 36 : return crc32c;
1095 : : }
1096 : :
1097 : : static uint32_t
1098 : 4605351 : _dif_update_crc32c_split(struct _dif_sgl *sgl, uint32_t offset_in_block, uint32_t data_len,
1099 : : uint32_t crc32c, const struct spdk_dif_ctx *ctx)
1100 : : {
1101 : 153 : uint32_t data_block_size, buf_len;
1102 : 153 : uint8_t *buf;
1103 : :
1104 [ # # # # : 4605351 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1105 : :
1106 [ + + # # : 4605351 : assert(offset_in_block + data_len <= ctx->block_size);
# # # # ]
1107 : :
1108 [ + + ]: 13834125 : while (data_len != 0) {
1109 : 9228774 : _dif_sgl_get_buf(sgl, &buf, &buf_len);
1110 [ + + ]: 9228774 : buf_len = spdk_min(buf_len, data_len);
1111 : :
1112 [ + + ]: 9228774 : if (offset_in_block < data_block_size) {
1113 [ + + ]: 4623411 : buf_len = spdk_min(buf_len, data_block_size - offset_in_block);
1114 : 4623411 : crc32c = spdk_crc32c_update(buf, buf_len, crc32c);
1115 : 69 : }
1116 : :
1117 : 9228774 : _dif_sgl_advance(sgl, buf_len);
1118 : 9228774 : offset_in_block += buf_len;
1119 : 9228774 : data_len -= buf_len;
1120 : : }
1121 : :
1122 : 4605351 : return crc32c;
1123 : : }
1124 : :
1125 : : static uint32_t
1126 : 24 : dif_update_crc32c_split(struct _dif_sgl *sgl, uint32_t num_blocks,
1127 : : uint32_t crc32c, const struct spdk_dif_ctx *ctx)
1128 : : {
1129 : : uint32_t offset_blocks;
1130 : :
1131 [ + + ]: 120 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1132 [ # # # # ]: 96 : crc32c = _dif_update_crc32c_split(sgl, 0, ctx->block_size, crc32c, ctx);
1133 : 24 : }
1134 : :
1135 : 24 : return crc32c;
1136 : : }
1137 : :
1138 : : int
1139 : 60 : spdk_dif_update_crc32c(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
1140 : : uint32_t *_crc32c, const struct spdk_dif_ctx *ctx)
1141 : : {
1142 : 45 : struct _dif_sgl sgl;
1143 : :
1144 [ + + ]: 60 : if (_crc32c == NULL) {
1145 : 0 : return -EINVAL;
1146 : : }
1147 : :
1148 : 60 : _dif_sgl_init(&sgl, iovs, iovcnt);
1149 : :
1150 [ + + # # : 60 : if (!_dif_sgl_is_valid(&sgl, ctx->block_size * num_blocks)) {
# # ]
1151 : 0 : SPDK_ERRLOG("Size of iovec array is not valid.\n");
1152 : 0 : return -EINVAL;
1153 : : }
1154 : :
1155 [ + + # # : 60 : if (_dif_sgl_is_bytes_multiple(&sgl, ctx->block_size)) {
# # ]
1156 [ # # # # ]: 36 : *_crc32c = dif_update_crc32c(&sgl, num_blocks, *_crc32c, ctx);
1157 : 9 : } else {
1158 [ # # # # ]: 24 : *_crc32c = dif_update_crc32c_split(&sgl, num_blocks, *_crc32c, ctx);
1159 : : }
1160 : :
1161 : 60 : return 0;
1162 : 15 : }
1163 : :
1164 : : static void
1165 : 2223520 : _dif_insert_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1166 : : uint32_t offset_blocks, const struct spdk_dif_ctx *ctx)
1167 : : {
1168 : : uint32_t data_block_size;
1169 : 664340 : uint8_t *src, *dst;
1170 : 2223520 : uint64_t guard = 0;
1171 : :
1172 [ # # # # : 2223520 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1173 : :
1174 : 2223520 : _dif_sgl_get_buf(src_sgl, &src, NULL);
1175 : 2223520 : _dif_sgl_get_buf(dst_sgl, &dst, NULL);
1176 : :
1177 [ + + # # : 2223520 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1178 [ # # # # ]: 2223238 : guard = _dif_generate_guard_copy(ctx->guard_seed, dst, src, data_block_size,
1179 [ # # # # ]: 2222896 : ctx->dif_pi_format);
1180 [ # # ]: 2223238 : guard = _dif_generate_guard(guard, dst + data_block_size,
1181 [ # # # # : 2222896 : ctx->guard_interval - data_block_size, ctx->dif_pi_format);
# # # # ]
1182 : 342 : } else {
1183 [ - + - + ]: 624 : memcpy(dst, src, data_block_size);
1184 : : }
1185 : :
1186 [ # # # # : 2223520 : _dif_generate(dst + ctx->guard_interval, guard, offset_blocks, ctx);
# # ]
1187 : :
1188 : 2223520 : _dif_sgl_advance(src_sgl, data_block_size);
1189 [ # # # # ]: 2223520 : _dif_sgl_advance(dst_sgl, ctx->block_size);
1190 : 2223520 : }
1191 : :
1192 : : static void
1193 : 277918 : dif_insert_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1194 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
1195 : : {
1196 : : uint32_t offset_blocks;
1197 : :
1198 [ + + ]: 2501438 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1199 : 2223520 : _dif_insert_copy(src_sgl, dst_sgl, offset_blocks, ctx);
1200 : 492 : }
1201 : 277918 : }
1202 : :
1203 : : static void
1204 : 268 : _dif_insert_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1205 : : uint32_t offset_blocks, const struct spdk_dif_ctx *ctx)
1206 : : {
1207 : : uint32_t data_block_size;
1208 : 268 : uint64_t guard = 0;
1209 : 268 : struct spdk_dif dif = {};
1210 : :
1211 [ # # # # : 268 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1212 : :
1213 [ + - # # : 268 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1214 [ # # # # ]: 335 : guard = _dif_generate_guard_copy_split(ctx->guard_seed, dst_sgl, src_sgl,
1215 [ # # # # ]: 268 : data_block_size, ctx->dif_pi_format);
1216 : 335 : guard = dif_generate_guard_split(guard, dst_sgl, data_block_size,
1217 [ # # # # ]: 268 : ctx->guard_interval - data_block_size, ctx);
1218 : 67 : } else {
1219 : 0 : _data_copy_split(dst_sgl, src_sgl, data_block_size);
1220 [ # # # # ]: 0 : _dif_sgl_advance(dst_sgl, ctx->guard_interval - data_block_size);
1221 : : }
1222 : :
1223 : 268 : _dif_generate(&dif, guard, offset_blocks, ctx);
1224 : :
1225 : 268 : dif_store_split(dst_sgl, &dif, ctx);
1226 : 268 : }
1227 : :
1228 : : static void
1229 : 124 : dif_insert_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1230 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
1231 : : {
1232 : : uint32_t offset_blocks;
1233 : :
1234 [ + + ]: 392 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1235 : 268 : _dif_insert_copy_split(src_sgl, dst_sgl, offset_blocks, ctx);
1236 : 67 : }
1237 : 124 : }
1238 : :
1239 : : static void
1240 : 48 : _dif_disable_insert_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1241 : : const struct spdk_dif_ctx *ctx)
1242 : : {
1243 : 48 : uint32_t offset = 0, src_len, dst_len, buf_len, data_block_size;
1244 : 36 : uint8_t *src, *dst;
1245 : :
1246 [ # # # # : 48 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1247 : :
1248 [ + + ]: 128 : while (offset < data_block_size) {
1249 : 80 : _dif_sgl_get_buf(src_sgl, &src, &src_len);
1250 : 80 : _dif_sgl_get_buf(dst_sgl, &dst, &dst_len);
1251 [ + + ]: 80 : buf_len = spdk_min(src_len, dst_len);
1252 [ + + ]: 80 : buf_len = spdk_min(buf_len, data_block_size - offset);
1253 : :
1254 [ - + - + ]: 80 : memcpy(dst, src, buf_len);
1255 : :
1256 : 80 : _dif_sgl_advance(src_sgl, buf_len);
1257 : 80 : _dif_sgl_advance(dst_sgl, buf_len);
1258 : 80 : offset += buf_len;
1259 : : }
1260 : :
1261 [ # # # # ]: 48 : _dif_sgl_advance(dst_sgl, ctx->md_size);
1262 : 48 : }
1263 : :
1264 : : static void
1265 : 12 : dif_disable_insert_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1266 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
1267 : : {
1268 : : uint32_t offset_blocks;
1269 : :
1270 [ + + ]: 60 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1271 : 48 : _dif_disable_insert_copy(src_sgl, dst_sgl, ctx);
1272 : 12 : }
1273 : 12 : }
1274 : :
1275 : : static int
1276 : 278057 : _spdk_dif_insert_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1277 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
1278 : : {
1279 : : uint32_t data_block_size;
1280 : :
1281 [ # # # # : 278057 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1282 : :
1283 [ + - - + ]: 278057 : if (!_dif_sgl_is_valid(src_sgl, data_block_size * num_blocks) ||
1284 [ + + # # ]: 278057 : !_dif_sgl_is_valid(dst_sgl, ctx->block_size * num_blocks)) {
1285 : 3 : SPDK_ERRLOG("Size of iovec arrays are not valid.\n");
1286 : 3 : return -EINVAL;
1287 : : }
1288 : :
1289 [ + + # # : 278054 : if (_dif_is_disabled(ctx->dif_type)) {
# # ]
1290 : 12 : dif_disable_insert_copy(src_sgl, dst_sgl, num_blocks, ctx);
1291 : 12 : return 0;
1292 : : }
1293 : :
1294 [ + + + - ]: 555904 : if (_dif_sgl_is_bytes_multiple(src_sgl, data_block_size) &&
1295 [ # # # # ]: 277918 : _dif_sgl_is_bytes_multiple(dst_sgl, ctx->block_size)) {
1296 : 277918 : dif_insert_copy(src_sgl, dst_sgl, num_blocks, ctx);
1297 : 56 : } else {
1298 : 124 : dif_insert_copy_split(src_sgl, dst_sgl, num_blocks, ctx);
1299 : : }
1300 : :
1301 : 278042 : return 0;
1302 : 90 : }
1303 : :
1304 : : static void
1305 : 384 : _dif_overwrite_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1306 : : uint32_t offset_blocks, const struct spdk_dif_ctx *ctx)
1307 : : {
1308 : 288 : uint8_t *src, *dst;
1309 : 384 : uint64_t guard = 0;
1310 : :
1311 : 384 : _dif_sgl_get_buf(src_sgl, &src, NULL);
1312 : 384 : _dif_sgl_get_buf(dst_sgl, &dst, NULL);
1313 : :
1314 [ + + # # : 384 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1315 [ # # # # : 120 : guard = _dif_generate_guard_copy(ctx->guard_seed, dst, src, ctx->guard_interval,
# # # # ]
1316 [ # # # # ]: 96 : ctx->dif_pi_format);
1317 : 24 : } else {
1318 [ - + - + : 288 : memcpy(dst, src, ctx->guard_interval);
# # # # ]
1319 : : }
1320 : :
1321 [ # # # # : 384 : _dif_generate(dst + ctx->guard_interval, guard, offset_blocks, ctx);
# # ]
1322 : :
1323 [ # # # # ]: 384 : _dif_sgl_advance(src_sgl, ctx->block_size);
1324 [ # # # # ]: 384 : _dif_sgl_advance(dst_sgl, ctx->block_size);
1325 : 384 : }
1326 : :
1327 : : static void
1328 : 64 : dif_overwrite_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1329 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
1330 : : {
1331 : : uint32_t offset_blocks;
1332 : :
1333 [ + + ]: 448 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1334 : 384 : _dif_overwrite_copy(src_sgl, dst_sgl, offset_blocks, ctx);
1335 : 96 : }
1336 : 64 : }
1337 : :
1338 : : static void
1339 : 32 : _dif_overwrite_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1340 : : uint32_t offset_blocks, const struct spdk_dif_ctx *ctx)
1341 : : {
1342 : 32 : uint64_t guard = 0;
1343 : 32 : struct spdk_dif dif = {};
1344 : :
1345 [ + - # # : 32 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1346 [ # # # # ]: 40 : guard = _dif_generate_guard_copy_split(ctx->guard_seed, dst_sgl, src_sgl,
1347 [ # # # # : 32 : ctx->guard_interval, ctx->dif_pi_format);
# # # # ]
1348 : 8 : } else {
1349 [ # # # # ]: 0 : _data_copy_split(dst_sgl, src_sgl, ctx->guard_interval);
1350 : : }
1351 : :
1352 [ # # # # : 32 : _dif_sgl_advance(src_sgl, ctx->block_size - ctx->guard_interval);
# # # # ]
1353 : :
1354 : 32 : _dif_generate(&dif, guard, offset_blocks, ctx);
1355 : 32 : dif_store_split(dst_sgl, &dif, ctx);
1356 : 32 : }
1357 : :
1358 : : static void
1359 : 8 : dif_overwrite_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1360 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
1361 : : {
1362 : : uint32_t offset_blocks;
1363 : :
1364 [ + + ]: 40 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1365 : 32 : _dif_overwrite_copy_split(src_sgl, dst_sgl, offset_blocks, ctx);
1366 : 8 : }
1367 : 8 : }
1368 : :
1369 : : static void
1370 : 0 : dif_disable_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1371 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
1372 : : {
1373 [ # # # # ]: 0 : _data_copy_split(dst_sgl, src_sgl, ctx->block_size * num_blocks);
1374 : 0 : }
1375 : :
1376 : : static int
1377 : 72 : _spdk_dif_overwrite_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1378 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
1379 : : {
1380 [ + - - + : 72 : if (!_dif_sgl_is_valid(src_sgl, ctx->block_size * num_blocks) ||
# # # # ]
1381 [ - + # # ]: 72 : !_dif_sgl_is_valid(dst_sgl, ctx->block_size * num_blocks)) {
1382 : 0 : SPDK_ERRLOG("Size of iovec arrays are not valid.\n");
1383 : 0 : return -EINVAL;
1384 : : }
1385 : :
1386 [ - + # # : 72 : if (_dif_is_disabled(ctx->dif_type)) {
# # ]
1387 : 0 : dif_disable_copy(src_sgl, dst_sgl, num_blocks, ctx);
1388 : 0 : return 0;
1389 : : }
1390 : :
1391 [ + + + - : 120 : if (_dif_sgl_is_bytes_multiple(src_sgl, ctx->block_size) &&
# # # # ]
1392 [ # # # # ]: 64 : _dif_sgl_is_bytes_multiple(dst_sgl, ctx->block_size)) {
1393 : 64 : dif_overwrite_copy(src_sgl, dst_sgl, num_blocks, ctx);
1394 : 16 : } else {
1395 : 8 : dif_overwrite_copy_split(src_sgl, dst_sgl, num_blocks, ctx);
1396 : : }
1397 : :
1398 : 72 : return 0;
1399 : 18 : }
1400 : :
1401 : : int
1402 : 278129 : spdk_dif_generate_copy(struct iovec *iovs, int iovcnt, struct iovec *bounce_iovs,
1403 : : int bounce_iovcnt, uint32_t num_blocks,
1404 : : const struct spdk_dif_ctx *ctx)
1405 : : {
1406 : 83183 : struct _dif_sgl src_sgl, dst_sgl;
1407 : :
1408 : 278129 : _dif_sgl_init(&src_sgl, iovs, iovcnt);
1409 : 278129 : _dif_sgl_init(&dst_sgl, bounce_iovs, bounce_iovcnt);
1410 : :
1411 [ + + - + : 278183 : if (!(ctx->dif_flags & SPDK_DIF_FLAGS_NVME_PRACT) ||
# # # # #
# ]
1412 [ # # # # : 72 : ctx->md_size == _dif_size(ctx->dif_pi_format)) {
# # # # ]
1413 : 278057 : return _spdk_dif_insert_copy(&src_sgl, &dst_sgl, num_blocks, ctx);
1414 : : } else {
1415 : 72 : return _spdk_dif_overwrite_copy(&src_sgl, &dst_sgl, num_blocks, ctx);
1416 : : }
1417 : 108 : }
1418 : :
1419 : : static int
1420 : 1473 : _dif_strip_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1421 : : uint32_t offset_blocks, const struct spdk_dif_ctx *ctx,
1422 : : struct spdk_dif_error *err_blk)
1423 : : {
1424 : : uint32_t data_block_size;
1425 : 999 : uint8_t *src, *dst;
1426 : : int rc;
1427 : 1473 : uint64_t guard = 0;
1428 : :
1429 [ # # # # : 1473 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1430 : :
1431 : 1473 : _dif_sgl_get_buf(src_sgl, &src, NULL);
1432 : 1473 : _dif_sgl_get_buf(dst_sgl, &dst, NULL);
1433 : :
1434 [ + + # # : 1473 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1435 [ # # # # ]: 993 : guard = _dif_generate_guard_copy(ctx->guard_seed, dst, src, data_block_size,
1436 [ # # # # ]: 771 : ctx->dif_pi_format);
1437 [ # # ]: 993 : guard = _dif_generate_guard(guard, src + data_block_size,
1438 [ # # # # : 771 : ctx->guard_interval - data_block_size, ctx->dif_pi_format);
# # # # ]
1439 : 222 : } else {
1440 [ - + - + ]: 702 : memcpy(dst, src, data_block_size);
1441 : : }
1442 : :
1443 [ # # # # : 1473 : rc = _dif_verify(src + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
# # ]
1444 [ + + ]: 1473 : if (rc != 0) {
1445 : 105 : return rc;
1446 : : }
1447 : :
1448 [ # # # # ]: 1368 : _dif_sgl_advance(src_sgl, ctx->block_size);
1449 : 1368 : _dif_sgl_advance(dst_sgl, data_block_size);
1450 : :
1451 : 1368 : return 0;
1452 : 372 : }
1453 : :
1454 : : static int
1455 : 251 : dif_strip_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1456 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
1457 : : struct spdk_dif_error *err_blk)
1458 : : {
1459 : : uint32_t offset_blocks;
1460 : : int rc;
1461 : :
1462 [ + + ]: 1619 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1463 : 1473 : rc = _dif_strip_copy(src_sgl, dst_sgl, offset_blocks, ctx, err_blk);
1464 [ + + ]: 1473 : if (rc != 0) {
1465 : 105 : return rc;
1466 : : }
1467 : 348 : }
1468 : :
1469 : 146 : return 0;
1470 : 56 : }
1471 : :
1472 : : static int
1473 : 196 : _dif_strip_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1474 : : uint32_t offset_blocks, const struct spdk_dif_ctx *ctx,
1475 : : struct spdk_dif_error *err_blk)
1476 : : {
1477 : : uint32_t data_block_size;
1478 : 196 : uint64_t guard = 0;
1479 : 196 : struct spdk_dif dif = {};
1480 : :
1481 [ # # # # : 196 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1482 : :
1483 [ + - # # : 196 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1484 [ # # # # ]: 239 : guard = _dif_generate_guard_copy_split(ctx->guard_seed, dst_sgl, src_sgl,
1485 [ # # # # ]: 196 : data_block_size, ctx->dif_pi_format);
1486 : 239 : guard = dif_generate_guard_split(guard, src_sgl, data_block_size,
1487 [ # # # # ]: 196 : ctx->guard_interval - data_block_size, ctx);
1488 : 43 : } else {
1489 : 0 : _data_copy_split(dst_sgl, src_sgl, data_block_size);
1490 [ # # # # ]: 0 : _dif_sgl_advance(src_sgl, ctx->guard_interval - data_block_size);
1491 : : }
1492 : :
1493 : 196 : dif_load_split(src_sgl, &dif, ctx);
1494 : :
1495 : 196 : return _dif_verify(&dif, guard, offset_blocks, ctx, err_blk);
1496 : : }
1497 : :
1498 : : static int
1499 : 124 : dif_strip_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1500 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
1501 : : struct spdk_dif_error *err_blk)
1502 : : {
1503 : : uint32_t offset_blocks;
1504 : : int rc;
1505 : :
1506 [ + + ]: 224 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1507 : 196 : rc = _dif_strip_copy_split(src_sgl, dst_sgl, offset_blocks, ctx, err_blk);
1508 [ + + ]: 196 : if (rc != 0) {
1509 : 96 : return rc;
1510 : : }
1511 : 19 : }
1512 : :
1513 : 28 : return 0;
1514 : 31 : }
1515 : :
1516 : : static void
1517 : 48 : _dif_disable_strip_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1518 : : const struct spdk_dif_ctx *ctx)
1519 : : {
1520 : 48 : uint32_t offset = 0, src_len, dst_len, buf_len, data_block_size;
1521 : 36 : uint8_t *src, *dst;
1522 : :
1523 [ # # # # : 48 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1524 : :
1525 [ + + ]: 128 : while (offset < data_block_size) {
1526 : 80 : _dif_sgl_get_buf(src_sgl, &src, &src_len);
1527 : 80 : _dif_sgl_get_buf(dst_sgl, &dst, &dst_len);
1528 [ + + ]: 80 : buf_len = spdk_min(src_len, dst_len);
1529 [ + + ]: 80 : buf_len = spdk_min(buf_len, data_block_size - offset);
1530 : :
1531 [ - + - + ]: 80 : memcpy(dst, src, buf_len);
1532 : :
1533 : 80 : _dif_sgl_advance(src_sgl, buf_len);
1534 : 80 : _dif_sgl_advance(dst_sgl, buf_len);
1535 : 80 : offset += buf_len;
1536 : : }
1537 : :
1538 [ # # # # ]: 48 : _dif_sgl_advance(src_sgl, ctx->md_size);
1539 : 48 : }
1540 : :
1541 : : static void
1542 : 12 : dif_disable_strip_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1543 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
1544 : : {
1545 : : uint32_t offset_blocks;
1546 : :
1547 [ + + ]: 60 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1548 : 48 : _dif_disable_strip_copy(src_sgl, dst_sgl, ctx);
1549 : 12 : }
1550 : 12 : }
1551 : :
1552 : : static int
1553 : 387 : _spdk_dif_strip_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1554 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
1555 : : struct spdk_dif_error *err_blk)
1556 : : {
1557 : : uint32_t data_block_size;
1558 : :
1559 [ # # # # : 387 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1560 : :
1561 [ + - - + ]: 387 : if (!_dif_sgl_is_valid(dst_sgl, data_block_size * num_blocks) ||
1562 [ - + # # ]: 387 : !_dif_sgl_is_valid(src_sgl, ctx->block_size * num_blocks)) {
1563 : 0 : SPDK_ERRLOG("Size of iovec arrays are not valid\n");
1564 : 0 : return -EINVAL;
1565 : : }
1566 : :
1567 [ + + # # : 387 : if (_dif_is_disabled(ctx->dif_type)) {
# # ]
1568 : 12 : dif_disable_strip_copy(src_sgl, dst_sgl, num_blocks, ctx);
1569 : 12 : return 0;
1570 : : }
1571 : :
1572 [ + + + - ]: 570 : if (_dif_sgl_is_bytes_multiple(dst_sgl, data_block_size) &&
1573 [ # # # # ]: 251 : _dif_sgl_is_bytes_multiple(src_sgl, ctx->block_size)) {
1574 : 251 : return dif_strip_copy(src_sgl, dst_sgl, num_blocks, ctx, err_blk);
1575 : : } else {
1576 : 124 : return dif_strip_copy_split(src_sgl, dst_sgl, num_blocks, ctx, err_blk);
1577 : : }
1578 : 90 : }
1579 : :
1580 : : static int
1581 : 384 : _dif_verify_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1582 : : uint32_t offset_blocks, const struct spdk_dif_ctx *ctx,
1583 : : struct spdk_dif_error *err_blk)
1584 : : {
1585 : 288 : uint8_t *src, *dst;
1586 : : int rc;
1587 : 384 : uint64_t guard = 0;
1588 : :
1589 : 384 : _dif_sgl_get_buf(src_sgl, &src, NULL);
1590 : 384 : _dif_sgl_get_buf(dst_sgl, &dst, NULL);
1591 : :
1592 [ + + # # : 384 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1593 [ # # # # : 120 : guard = _dif_generate_guard_copy(ctx->guard_seed, dst, src, ctx->guard_interval,
# # # # ]
1594 [ # # # # ]: 96 : ctx->dif_pi_format);
1595 : 24 : } else {
1596 [ - + - + : 288 : memcpy(dst, src, ctx->guard_interval);
# # # # ]
1597 : : }
1598 : :
1599 [ # # # # : 384 : rc = _dif_verify(src + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
# # ]
1600 [ + + ]: 384 : if (rc != 0) {
1601 : 0 : return rc;
1602 : : }
1603 : :
1604 [ # # # # ]: 384 : _dif_sgl_advance(src_sgl, ctx->block_size);
1605 [ # # # # ]: 384 : _dif_sgl_advance(dst_sgl, ctx->block_size);
1606 : :
1607 : 384 : return 0;
1608 : 96 : }
1609 : :
1610 : : static int
1611 : 64 : dif_verify_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1612 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
1613 : : struct spdk_dif_error *err_blk)
1614 : : {
1615 : : uint32_t offset_blocks;
1616 : : int rc;
1617 : :
1618 [ + + ]: 448 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1619 : 384 : rc = _dif_verify_copy(src_sgl, dst_sgl, offset_blocks, ctx, err_blk);
1620 [ - + ]: 384 : if (rc != 0) {
1621 : 0 : return rc;
1622 : : }
1623 : 96 : }
1624 : :
1625 : 64 : return 0;
1626 : 16 : }
1627 : :
1628 : : static int
1629 : 32 : _dif_verify_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1630 : : uint32_t offset_blocks, const struct spdk_dif_ctx *ctx,
1631 : : struct spdk_dif_error *err_blk)
1632 : : {
1633 : 32 : uint64_t guard = 0;
1634 : 32 : struct spdk_dif dif = {};
1635 : :
1636 [ + - # # : 32 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1637 [ # # # # ]: 40 : guard = _dif_generate_guard_copy_split(ctx->guard_seed, dst_sgl, src_sgl,
1638 [ # # # # : 32 : ctx->guard_interval, ctx->dif_pi_format);
# # # # ]
1639 : 8 : } else {
1640 [ # # # # ]: 0 : _data_copy_split(dst_sgl, src_sgl, ctx->guard_interval);
1641 : : }
1642 : :
1643 : 32 : dif_load_split(src_sgl, &dif, ctx);
1644 [ # # # # : 32 : _dif_sgl_advance(dst_sgl, ctx->block_size - ctx->guard_interval);
# # # # ]
1645 : :
1646 : 32 : return _dif_verify(&dif, guard, offset_blocks, ctx, err_blk);
1647 : : }
1648 : :
1649 : : static int
1650 : 8 : dif_verify_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1651 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
1652 : : struct spdk_dif_error *err_blk)
1653 : : {
1654 : : uint32_t offset_blocks;
1655 : : int rc;
1656 : :
1657 [ + + ]: 40 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1658 : 32 : rc = _dif_verify_copy_split(src_sgl, dst_sgl, offset_blocks, ctx, err_blk);
1659 [ - + ]: 32 : if (rc != 0) {
1660 : 0 : return rc;
1661 : : }
1662 : 8 : }
1663 : :
1664 : 8 : return 0;
1665 : 2 : }
1666 : :
1667 : : static int
1668 : 72 : _spdk_dif_verify_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1669 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
1670 : : struct spdk_dif_error *err_blk)
1671 : : {
1672 [ + - - + : 72 : if (!_dif_sgl_is_valid(dst_sgl, ctx->block_size * num_blocks) ||
# # # # ]
1673 [ - + # # ]: 72 : !_dif_sgl_is_valid(src_sgl, ctx->block_size * num_blocks)) {
1674 : 0 : SPDK_ERRLOG("Size of iovec arrays are not valid\n");
1675 : 0 : return -EINVAL;
1676 : : }
1677 : :
1678 [ - + # # : 72 : if (_dif_is_disabled(ctx->dif_type)) {
# # ]
1679 : 0 : dif_disable_copy(src_sgl, dst_sgl, num_blocks, ctx);
1680 : 0 : return 0;
1681 : : }
1682 : :
1683 [ + + + - : 120 : if (_dif_sgl_is_bytes_multiple(dst_sgl, ctx->block_size) &&
# # # # ]
1684 [ # # # # ]: 64 : _dif_sgl_is_bytes_multiple(src_sgl, ctx->block_size)) {
1685 : 64 : return dif_verify_copy(src_sgl, dst_sgl, num_blocks, ctx, err_blk);
1686 : : } else {
1687 : 8 : return dif_verify_copy_split(src_sgl, dst_sgl, num_blocks, ctx, err_blk);
1688 : : }
1689 : 18 : }
1690 : :
1691 : : int
1692 : 459 : spdk_dif_verify_copy(struct iovec *iovs, int iovcnt, struct iovec *bounce_iovs,
1693 : : int bounce_iovcnt, uint32_t num_blocks,
1694 : : const struct spdk_dif_ctx *ctx,
1695 : : struct spdk_dif_error *err_blk)
1696 : : {
1697 : 333 : struct _dif_sgl src_sgl, dst_sgl;
1698 : :
1699 : 459 : _dif_sgl_init(&src_sgl, bounce_iovs, bounce_iovcnt);
1700 : 459 : _dif_sgl_init(&dst_sgl, iovs, iovcnt);
1701 : :
1702 [ + + - + : 513 : if (!(ctx->dif_flags & SPDK_DIF_FLAGS_NVME_PRACT) ||
# # # # #
# ]
1703 [ # # # # : 72 : ctx->md_size == _dif_size(ctx->dif_pi_format)) {
# # # # ]
1704 : 387 : return _spdk_dif_strip_copy(&src_sgl, &dst_sgl, num_blocks, ctx, err_blk);
1705 : : } else {
1706 : 72 : return _spdk_dif_verify_copy(&src_sgl, &dst_sgl, num_blocks, ctx, err_blk);
1707 : : }
1708 : 108 : }
1709 : :
1710 : : static void
1711 : 960 : _bit_flip(uint8_t *buf, uint32_t flip_bit)
1712 : : {
1713 : : uint8_t byte;
1714 : :
1715 [ # # ]: 960 : byte = *buf;
1716 [ - + # # ]: 960 : byte ^= 1 << flip_bit;
1717 [ # # ]: 960 : *buf = byte;
1718 : 960 : }
1719 : :
1720 : : static int
1721 : 960 : _dif_inject_error(struct _dif_sgl *sgl,
1722 : : uint32_t block_size, uint32_t num_blocks,
1723 : : uint32_t inject_offset_blocks,
1724 : : uint32_t inject_offset_bytes,
1725 : : uint32_t inject_offset_bits)
1726 : : {
1727 : 720 : uint32_t offset_in_block, buf_len;
1728 : 720 : uint8_t *buf;
1729 : :
1730 : 960 : _dif_sgl_advance(sgl, block_size * inject_offset_blocks);
1731 : :
1732 : 960 : offset_in_block = 0;
1733 : :
1734 [ + - ]: 1315 : while (offset_in_block < block_size) {
1735 : 1315 : _dif_sgl_get_buf(sgl, &buf, &buf_len);
1736 [ + + ]: 1315 : buf_len = spdk_min(buf_len, block_size - offset_in_block);
1737 : :
1738 [ + - + + ]: 1315 : if (inject_offset_bytes >= offset_in_block &&
1739 [ + + ]: 1315 : inject_offset_bytes < offset_in_block + buf_len) {
1740 [ # # ]: 960 : buf += inject_offset_bytes - offset_in_block;
1741 : 960 : _bit_flip(buf, inject_offset_bits);
1742 : 960 : return 0;
1743 : : }
1744 : :
1745 : 355 : _dif_sgl_advance(sgl, buf_len);
1746 : 355 : offset_in_block += buf_len;
1747 : : }
1748 : :
1749 : 0 : return -1;
1750 : 240 : }
1751 : :
1752 : : static int
1753 : 960 : dif_inject_error(struct _dif_sgl *sgl, uint32_t block_size, uint32_t num_blocks,
1754 : : uint32_t start_inject_bytes, uint32_t inject_range_bytes,
1755 : : uint32_t *inject_offset)
1756 : : {
1757 : : uint32_t inject_offset_blocks, inject_offset_bytes, inject_offset_bits;
1758 : : uint32_t offset_blocks;
1759 : : int rc;
1760 : :
1761 : 960 : srand(time(0));
1762 : :
1763 [ - + ]: 960 : inject_offset_blocks = rand() % num_blocks;
1764 [ - + ]: 960 : inject_offset_bytes = start_inject_bytes + (rand() % inject_range_bytes);
1765 [ # # ]: 960 : inject_offset_bits = rand() % 8;
1766 : :
1767 [ + - ]: 1656 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1768 [ + + ]: 1656 : if (offset_blocks == inject_offset_blocks) {
1769 : 1200 : rc = _dif_inject_error(sgl, block_size, num_blocks,
1770 : 240 : inject_offset_blocks,
1771 : 240 : inject_offset_bytes,
1772 : 240 : inject_offset_bits);
1773 [ + + ]: 960 : if (rc == 0) {
1774 [ # # ]: 960 : *inject_offset = inject_offset_blocks;
1775 : 240 : }
1776 : 960 : return rc;
1777 : : }
1778 : 288 : }
1779 : :
1780 : 0 : return -1;
1781 : 240 : }
1782 : :
1783 : : int
1784 : 768 : spdk_dif_inject_error(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
1785 : : const struct spdk_dif_ctx *ctx, uint32_t inject_flags,
1786 : : uint32_t *inject_offset)
1787 : : {
1788 : 576 : struct _dif_sgl sgl;
1789 : : int rc;
1790 : :
1791 : 768 : _dif_sgl_init(&sgl, iovs, iovcnt);
1792 : :
1793 [ - + # # : 768 : if (!_dif_sgl_is_valid(&sgl, ctx->block_size * num_blocks)) {
# # ]
1794 : 0 : SPDK_ERRLOG("Size of iovec array is not valid.\n");
1795 : 0 : return -EINVAL;
1796 : : }
1797 : :
1798 [ + + ]: 768 : if (inject_flags & SPDK_DIF_REFTAG_ERROR) {
1799 [ # # # # ]: 288 : rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
1800 [ # # # # : 192 : ctx->guard_interval + _dif_reftag_offset(ctx->dif_pi_format),
# # # # ]
1801 [ # # # # ]: 192 : _dif_reftag_size(ctx->dif_pi_format),
1802 : 48 : inject_offset);
1803 [ - + ]: 192 : if (rc != 0) {
1804 : 0 : SPDK_ERRLOG("Failed to inject error to Reference Tag.\n");
1805 : 0 : return rc;
1806 : : }
1807 : 48 : }
1808 : :
1809 [ + + ]: 768 : if (inject_flags & SPDK_DIF_APPTAG_ERROR) {
1810 [ # # # # ]: 288 : rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
1811 [ # # # # : 192 : ctx->guard_interval + _dif_apptag_offset(ctx->dif_pi_format),
# # # # ]
1812 : 192 : _dif_apptag_size(),
1813 : 48 : inject_offset);
1814 [ - + ]: 192 : if (rc != 0) {
1815 : 0 : SPDK_ERRLOG("Failed to inject error to Application Tag.\n");
1816 : 0 : return rc;
1817 : : }
1818 : 48 : }
1819 [ + + ]: 768 : if (inject_flags & SPDK_DIF_GUARD_ERROR) {
1820 [ # # # # ]: 240 : rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
1821 [ # # # # ]: 192 : ctx->guard_interval,
1822 [ # # # # ]: 192 : _dif_guard_size(ctx->dif_pi_format),
1823 : 48 : inject_offset);
1824 [ - + ]: 192 : if (rc != 0) {
1825 : 0 : SPDK_ERRLOG("Failed to inject error to Guard.\n");
1826 : 0 : return rc;
1827 : : }
1828 : 48 : }
1829 : :
1830 [ + + ]: 768 : if (inject_flags & SPDK_DIF_DATA_ERROR) {
1831 : : /* If the DIF information is contained within the last 8/16 bytes of
1832 : : * metadata (depending on the PI format), then the CRC covers all metadata
1833 : : * bytes up to but excluding the last 8/16 bytes. But error injection does not
1834 : : * cover these metadata because classification is not determined yet.
1835 : : *
1836 : : * Note: Error injection to data block is expected to be detected as
1837 : : * guard error.
1838 : : */
1839 [ # # # # ]: 240 : rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
1840 : : 0,
1841 [ # # # # : 192 : ctx->block_size - ctx->md_size,
# # # # ]
1842 : 48 : inject_offset);
1843 [ + + ]: 192 : if (rc != 0) {
1844 : 0 : SPDK_ERRLOG("Failed to inject error to data block.\n");
1845 : 0 : return rc;
1846 : : }
1847 : 48 : }
1848 : :
1849 : 768 : return 0;
1850 : 192 : }
1851 : :
1852 : : static void
1853 : 404 : dix_generate(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
1854 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
1855 : : {
1856 : 404 : uint32_t offset_blocks = 0;
1857 : 238 : uint8_t *data_buf, *md_buf;
1858 : : uint64_t guard;
1859 : :
1860 [ + + ]: 4852 : while (offset_blocks < num_blocks) {
1861 : 4448 : _dif_sgl_get_buf(data_sgl, &data_buf, NULL);
1862 : 4448 : _dif_sgl_get_buf(md_sgl, &md_buf, NULL);
1863 : :
1864 : 4448 : guard = 0;
1865 [ + + # # : 4448 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1866 [ # # # # : 4366 : guard = _dif_generate_guard(ctx->guard_seed, data_buf, ctx->block_size,
# # # # ]
1867 [ # # # # ]: 3728 : ctx->dif_pi_format);
1868 [ # # # # ]: 4366 : guard = _dif_generate_guard(guard, md_buf, ctx->guard_interval,
1869 [ # # # # ]: 3728 : ctx->dif_pi_format);
1870 : 638 : }
1871 : :
1872 [ # # # # : 4448 : _dif_generate(md_buf + ctx->guard_interval, guard, offset_blocks, ctx);
# # ]
1873 : :
1874 [ # # # # ]: 4448 : _dif_sgl_advance(data_sgl, ctx->block_size);
1875 [ # # # # ]: 4448 : _dif_sgl_advance(md_sgl, ctx->md_size);
1876 : 4448 : offset_blocks++;
1877 : : }
1878 : 404 : }
1879 : :
1880 : : static void
1881 : 300 : _dix_generate_split(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
1882 : : uint32_t offset_blocks, const struct spdk_dif_ctx *ctx)
1883 : : {
1884 : 225 : uint32_t offset_in_block, data_buf_len;
1885 : 225 : uint8_t *data_buf, *md_buf;
1886 : 300 : uint64_t guard = 0;
1887 : :
1888 : 300 : _dif_sgl_get_buf(md_sgl, &md_buf, NULL);
1889 : :
1890 [ + + # # : 300 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1891 [ # # # # ]: 300 : guard = ctx->guard_seed;
1892 : 75 : }
1893 : 300 : offset_in_block = 0;
1894 : :
1895 [ + + # # : 924 : while (offset_in_block < ctx->block_size) {
# # ]
1896 : 624 : _dif_sgl_get_buf(data_sgl, &data_buf, &data_buf_len);
1897 [ + + # # : 624 : data_buf_len = spdk_min(data_buf_len, ctx->block_size - offset_in_block);
# # # # #
# ]
1898 : :
1899 [ + + # # : 624 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1900 : 780 : guard = _dif_generate_guard(guard, data_buf, data_buf_len,
1901 [ # # # # ]: 624 : ctx->dif_pi_format);
1902 : 156 : }
1903 : :
1904 : 624 : _dif_sgl_advance(data_sgl, data_buf_len);
1905 : 624 : offset_in_block += data_buf_len;
1906 : : }
1907 : :
1908 [ + + # # : 300 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1909 [ # # # # ]: 375 : guard = _dif_generate_guard(guard, md_buf, ctx->guard_interval,
1910 [ # # # # ]: 300 : ctx->dif_pi_format);
1911 : 75 : }
1912 : :
1913 [ # # # # ]: 300 : _dif_sgl_advance(md_sgl, ctx->md_size);
1914 : :
1915 [ # # # # : 300 : _dif_generate(md_buf + ctx->guard_interval, guard, offset_blocks, ctx);
# # ]
1916 : 300 : }
1917 : :
1918 : : static void
1919 : 132 : dix_generate_split(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
1920 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
1921 : : {
1922 : : uint32_t offset_blocks;
1923 : :
1924 [ + + ]: 432 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1925 : 300 : _dix_generate_split(data_sgl, md_sgl, offset_blocks, ctx);
1926 : 75 : }
1927 : 132 : }
1928 : :
1929 : : int
1930 : 11544 : spdk_dix_generate(struct iovec *iovs, int iovcnt, struct iovec *md_iov,
1931 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
1932 : : {
1933 : 11345 : struct _dif_sgl data_sgl, md_sgl;
1934 : :
1935 : 11544 : _dif_sgl_init(&data_sgl, iovs, iovcnt);
1936 : 11544 : _dif_sgl_init(&md_sgl, md_iov, 1);
1937 : :
1938 [ + - - + : 11544 : if (!_dif_sgl_is_valid(&data_sgl, ctx->block_size * num_blocks) ||
# # # # ]
1939 [ - + # # ]: 11544 : !_dif_sgl_is_valid(&md_sgl, ctx->md_size * num_blocks)) {
1940 : 0 : SPDK_ERRLOG("Size of iovec array is not valid.\n");
1941 : 0 : return -EINVAL;
1942 : : }
1943 : :
1944 [ + + # # : 11544 : if (_dif_is_disabled(ctx->dif_type)) {
# # ]
1945 : 11008 : return 0;
1946 : : }
1947 : :
1948 [ + + # # : 536 : if (_dif_sgl_is_bytes_multiple(&data_sgl, ctx->block_size)) {
# # ]
1949 : 404 : dix_generate(&data_sgl, &md_sgl, num_blocks, ctx);
1950 : 62 : } else {
1951 : 132 : dix_generate_split(&data_sgl, &md_sgl, num_blocks, ctx);
1952 : : }
1953 : :
1954 : 536 : return 0;
1955 : 95 : }
1956 : :
1957 : : static int
1958 : 258888 : dix_verify(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
1959 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
1960 : : struct spdk_dif_error *err_blk)
1961 : : {
1962 : 258888 : uint32_t offset_blocks = 0;
1963 : 85757 : uint8_t *data_buf, *md_buf;
1964 : : uint64_t guard;
1965 : : int rc;
1966 : :
1967 [ + + ]: 2330960 : while (offset_blocks < num_blocks) {
1968 : 2072195 : _dif_sgl_get_buf(data_sgl, &data_buf, NULL);
1969 : 2072195 : _dif_sgl_get_buf(md_sgl, &md_buf, NULL);
1970 : :
1971 : 2072195 : guard = 0;
1972 [ + + # # : 2072195 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1973 [ # # # # : 2072157 : guard = _dif_generate_guard(ctx->guard_seed, data_buf, ctx->block_size,
# # # # ]
1974 [ # # # # ]: 2071415 : ctx->dif_pi_format);
1975 [ # # # # ]: 2072157 : guard = _dif_generate_guard(guard, md_buf, ctx->guard_interval,
1976 [ # # # # ]: 2071415 : ctx->dif_pi_format);
1977 : 742 : }
1978 : :
1979 [ # # # # : 2072195 : rc = _dif_verify(md_buf + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
# # ]
1980 [ + + ]: 2072195 : if (rc != 0) {
1981 : 123 : return rc;
1982 : : }
1983 : :
1984 [ # # # # ]: 2072072 : _dif_sgl_advance(data_sgl, ctx->block_size);
1985 [ # # # # ]: 2072072 : _dif_sgl_advance(md_sgl, ctx->md_size);
1986 : 2072072 : offset_blocks++;
1987 : : }
1988 : :
1989 : 258765 : return 0;
1990 : 67 : }
1991 : :
1992 : : static int
1993 : 228 : _dix_verify_split(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
1994 : : uint32_t offset_blocks, const struct spdk_dif_ctx *ctx,
1995 : : struct spdk_dif_error *err_blk)
1996 : : {
1997 : 177 : uint32_t offset_in_block, data_buf_len;
1998 : 177 : uint8_t *data_buf, *md_buf;
1999 : 228 : uint64_t guard = 0;
2000 : :
2001 : 228 : _dif_sgl_get_buf(md_sgl, &md_buf, NULL);
2002 : :
2003 [ + + # # : 228 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
2004 [ # # # # ]: 228 : guard = ctx->guard_seed;
2005 : 51 : }
2006 : 228 : offset_in_block = 0;
2007 : :
2008 [ + + # # : 708 : while (offset_in_block < ctx->block_size) {
# # ]
2009 : 480 : _dif_sgl_get_buf(data_sgl, &data_buf, &data_buf_len);
2010 [ + + # # : 480 : data_buf_len = spdk_min(data_buf_len, ctx->block_size - offset_in_block);
# # # # #
# ]
2011 : :
2012 [ + + # # : 480 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
2013 : 588 : guard = _dif_generate_guard(guard, data_buf, data_buf_len,
2014 [ # # # # ]: 480 : ctx->dif_pi_format);
2015 : 108 : }
2016 : :
2017 : 480 : _dif_sgl_advance(data_sgl, data_buf_len);
2018 : 480 : offset_in_block += data_buf_len;
2019 : : }
2020 : :
2021 [ + + # # : 228 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
2022 [ # # # # ]: 279 : guard = _dif_generate_guard(guard, md_buf, ctx->guard_interval,
2023 [ # # # # ]: 228 : ctx->dif_pi_format);
2024 : 51 : }
2025 : :
2026 [ # # # # ]: 228 : _dif_sgl_advance(md_sgl, ctx->md_size);
2027 : :
2028 [ # # # # : 228 : return _dif_verify(md_buf + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
# # ]
2029 : : }
2030 : :
2031 : : static int
2032 : 132 : dix_verify_split(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
2033 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
2034 : : struct spdk_dif_error *err_blk)
2035 : : {
2036 : : uint32_t offset_blocks;
2037 : : int rc;
2038 : :
2039 [ + + ]: 264 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
2040 : 228 : rc = _dix_verify_split(data_sgl, md_sgl, offset_blocks, ctx, err_blk);
2041 [ + + ]: 228 : if (rc != 0) {
2042 : 96 : return rc;
2043 : : }
2044 : 27 : }
2045 : :
2046 : 36 : return 0;
2047 : 33 : }
2048 : :
2049 : : int
2050 : 388812 : spdk_dix_verify(struct iovec *iovs, int iovcnt, struct iovec *md_iov,
2051 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
2052 : : struct spdk_dif_error *err_blk)
2053 : : {
2054 : 215648 : struct _dif_sgl data_sgl, md_sgl;
2055 : :
2056 [ + + # # : 388812 : if (md_iov->iov_base == NULL) {
# # ]
2057 : 0 : SPDK_ERRLOG("Metadata buffer is NULL.\n");
2058 : 0 : return -EINVAL;
2059 : : }
2060 : :
2061 : 388812 : _dif_sgl_init(&data_sgl, iovs, iovcnt);
2062 : 388812 : _dif_sgl_init(&md_sgl, md_iov, 1);
2063 : :
2064 [ + - - + : 388812 : if (!_dif_sgl_is_valid(&data_sgl, ctx->block_size * num_blocks) ||
# # # # ]
2065 [ - + # # ]: 388812 : !_dif_sgl_is_valid(&md_sgl, ctx->md_size * num_blocks)) {
2066 : 0 : SPDK_ERRLOG("Size of iovec array is not valid.\n");
2067 : 0 : return -EINVAL;
2068 : : }
2069 : :
2070 [ + + # # : 388812 : if (_dif_is_disabled(ctx->dif_type)) {
# # ]
2071 : 129792 : return 0;
2072 : : }
2073 : :
2074 [ + + # # : 259020 : if (_dif_sgl_is_bytes_multiple(&data_sgl, ctx->block_size)) {
# # ]
2075 : 258888 : return dix_verify(&data_sgl, &md_sgl, num_blocks, ctx, err_blk);
2076 : : } else {
2077 : 132 : return dix_verify_split(&data_sgl, &md_sgl, num_blocks, ctx, err_blk);
2078 : : }
2079 : 100 : }
2080 : :
2081 : : int
2082 : 192 : spdk_dix_inject_error(struct iovec *iovs, int iovcnt, struct iovec *md_iov,
2083 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
2084 : : uint32_t inject_flags, uint32_t *inject_offset)
2085 : : {
2086 : 144 : struct _dif_sgl data_sgl, md_sgl;
2087 : : int rc;
2088 : :
2089 : 192 : _dif_sgl_init(&data_sgl, iovs, iovcnt);
2090 : 192 : _dif_sgl_init(&md_sgl, md_iov, 1);
2091 : :
2092 [ + - - + : 192 : if (!_dif_sgl_is_valid(&data_sgl, ctx->block_size * num_blocks) ||
# # # # ]
2093 [ - + # # ]: 192 : !_dif_sgl_is_valid(&md_sgl, ctx->md_size * num_blocks)) {
2094 : 0 : SPDK_ERRLOG("Size of iovec array is not valid.\n");
2095 : 0 : return -EINVAL;
2096 : : }
2097 : :
2098 [ + + ]: 192 : if (inject_flags & SPDK_DIF_REFTAG_ERROR) {
2099 [ # # # # ]: 72 : rc = dif_inject_error(&md_sgl, ctx->md_size, num_blocks,
2100 [ # # # # : 48 : ctx->guard_interval + _dif_reftag_offset(ctx->dif_pi_format),
# # # # ]
2101 [ # # # # ]: 48 : _dif_reftag_size(ctx->dif_pi_format),
2102 : 12 : inject_offset);
2103 [ - + ]: 48 : if (rc != 0) {
2104 : 0 : SPDK_ERRLOG("Failed to inject error to Reference Tag.\n");
2105 : 0 : return rc;
2106 : : }
2107 : 12 : }
2108 : :
2109 [ + + ]: 192 : if (inject_flags & SPDK_DIF_APPTAG_ERROR) {
2110 [ # # # # ]: 72 : rc = dif_inject_error(&md_sgl, ctx->md_size, num_blocks,
2111 [ # # # # : 48 : ctx->guard_interval + _dif_apptag_offset(ctx->dif_pi_format),
# # # # ]
2112 : 48 : _dif_apptag_size(),
2113 : 12 : inject_offset);
2114 [ - + ]: 48 : if (rc != 0) {
2115 : 0 : SPDK_ERRLOG("Failed to inject error to Application Tag.\n");
2116 : 0 : return rc;
2117 : : }
2118 : 12 : }
2119 : :
2120 [ + + ]: 192 : if (inject_flags & SPDK_DIF_GUARD_ERROR) {
2121 [ # # # # ]: 60 : rc = dif_inject_error(&md_sgl, ctx->md_size, num_blocks,
2122 [ # # # # ]: 48 : ctx->guard_interval,
2123 [ # # # # ]: 48 : _dif_guard_size(ctx->dif_pi_format),
2124 : 12 : inject_offset);
2125 [ - + ]: 48 : if (rc != 0) {
2126 : 0 : SPDK_ERRLOG("Failed to inject error to Guard.\n");
2127 : 0 : return rc;
2128 : : }
2129 : 12 : }
2130 : :
2131 [ + + ]: 192 : if (inject_flags & SPDK_DIF_DATA_ERROR) {
2132 : : /* Note: Error injection to data block is expected to be detected
2133 : : * as guard error.
2134 : : */
2135 [ # # # # ]: 60 : rc = dif_inject_error(&data_sgl, ctx->block_size, num_blocks,
2136 : : 0,
2137 [ # # # # ]: 48 : ctx->block_size,
2138 : 12 : inject_offset);
2139 [ + + ]: 48 : if (rc != 0) {
2140 : 0 : SPDK_ERRLOG("Failed to inject error to Guard.\n");
2141 : 0 : return rc;
2142 : : }
2143 : 12 : }
2144 : :
2145 : 192 : return 0;
2146 : 48 : }
2147 : :
2148 : : static uint32_t
2149 : 85041630 : _to_next_boundary(uint32_t offset, uint32_t boundary)
2150 : : {
2151 [ - + ]: 85041630 : return boundary - (offset % boundary);
2152 : : }
2153 : :
2154 : : static uint32_t
2155 : 7908056 : _to_size_with_md(uint32_t size, uint32_t data_block_size, uint32_t block_size)
2156 : : {
2157 [ - + - + ]: 7908056 : return (size / data_block_size) * block_size + (size % data_block_size);
2158 : : }
2159 : :
2160 : : int
2161 : 1635068 : spdk_dif_set_md_interleave_iovs(struct iovec *iovs, int iovcnt,
2162 : : struct iovec *buf_iovs, int buf_iovcnt,
2163 : : uint32_t data_offset, uint32_t data_len,
2164 : : uint32_t *_mapped_len,
2165 : : const struct spdk_dif_ctx *ctx)
2166 : : {
2167 : : uint32_t data_block_size, data_unalign, buf_len, buf_offset, len;
2168 : 114 : struct _dif_sgl dif_sgl;
2169 : 114 : struct _dif_sgl buf_sgl;
2170 : :
2171 [ + - + - : 1635068 : if (iovs == NULL || iovcnt == 0 || buf_iovs == NULL || buf_iovcnt == 0) {
+ - - + ]
2172 : 0 : return -EINVAL;
2173 : : }
2174 : :
2175 [ # # # # : 1635068 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
2176 : :
2177 [ - + # # : 1635068 : data_unalign = ctx->data_offset % data_block_size;
# # ]
2178 : :
2179 : 1635106 : buf_len = _to_size_with_md(data_unalign + data_offset + data_len, data_block_size,
2180 [ # # # # ]: 1635068 : ctx->block_size);
2181 : 1635068 : buf_len -= data_unalign;
2182 : :
2183 : 1635068 : _dif_sgl_init(&dif_sgl, iovs, iovcnt);
2184 : 1635068 : _dif_sgl_init(&buf_sgl, buf_iovs, buf_iovcnt);
2185 : :
2186 [ + + ]: 1635068 : if (!_dif_sgl_is_valid(&buf_sgl, buf_len)) {
2187 : 4 : SPDK_ERRLOG("Buffer overflow will occur.\n");
2188 : 4 : return -ERANGE;
2189 : : }
2190 : :
2191 [ # # # # ]: 1635064 : buf_offset = _to_size_with_md(data_unalign + data_offset, data_block_size, ctx->block_size);
2192 : 1635064 : buf_offset -= data_unalign;
2193 : :
2194 : 1635064 : _dif_sgl_advance(&buf_sgl, buf_offset);
2195 : :
2196 [ + + ]: 26603586 : while (data_len != 0) {
2197 [ + + # # : 25277036 : len = spdk_min(data_len, _to_next_boundary(ctx->data_offset + data_offset, data_block_size));
# # # # #
# ]
2198 [ + + ]: 25277036 : if (!_dif_sgl_append_split(&dif_sgl, &buf_sgl, len)) {
2199 : 308514 : break;
2200 : : }
2201 [ # # # # ]: 24968522 : _dif_sgl_advance(&buf_sgl, ctx->md_size);
2202 : 24968522 : data_offset += len;
2203 : 24968522 : data_len -= len;
2204 : : }
2205 : :
2206 [ + + ]: 1635064 : if (_mapped_len != NULL) {
2207 [ # # # # ]: 1635064 : *_mapped_len = dif_sgl.total_size;
2208 : 37 : }
2209 : :
2210 [ # # # # ]: 1635064 : return iovcnt - dif_sgl.iovcnt;
2211 : 38 : }
2212 : :
2213 : : static int
2214 : 979633 : _dif_sgl_setup_stream(struct _dif_sgl *sgl, uint32_t *_buf_offset, uint32_t *_buf_len,
2215 : : uint32_t data_offset, uint32_t data_len,
2216 : : const struct spdk_dif_ctx *ctx)
2217 : : {
2218 : : uint32_t data_block_size, data_unalign, buf_len, buf_offset;
2219 : :
2220 [ # # # # : 979633 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
2221 : :
2222 [ - + # # : 979633 : data_unalign = ctx->data_offset % data_block_size;
# # ]
2223 : :
2224 : : /* If the last data block is complete, DIF of the data block is
2225 : : * inserted or verified in this turn.
2226 : : */
2227 : 979700 : buf_len = _to_size_with_md(data_unalign + data_offset + data_len, data_block_size,
2228 [ # # # # ]: 979633 : ctx->block_size);
2229 : 979633 : buf_len -= data_unalign;
2230 : :
2231 [ + + ]: 979633 : if (!_dif_sgl_is_valid(sgl, buf_len)) {
2232 : 12 : return -ERANGE;
2233 : : }
2234 : :
2235 [ # # # # ]: 979621 : buf_offset = _to_size_with_md(data_unalign + data_offset, data_block_size, ctx->block_size);
2236 : 979621 : buf_offset -= data_unalign;
2237 : :
2238 : 979621 : _dif_sgl_advance(sgl, buf_offset);
2239 : 979621 : buf_len -= buf_offset;
2240 : :
2241 : 979621 : buf_offset += data_unalign;
2242 : :
2243 [ # # ]: 979621 : *_buf_offset = buf_offset;
2244 [ # # ]: 979621 : *_buf_len = buf_len;
2245 : :
2246 : 979621 : return 0;
2247 : 67 : }
2248 : :
2249 : : int
2250 : 196 : spdk_dif_generate_stream(struct iovec *iovs, int iovcnt,
2251 : : uint32_t data_offset, uint32_t data_len,
2252 : : struct spdk_dif_ctx *ctx)
2253 : : {
2254 : 196 : uint32_t buf_len = 0, buf_offset = 0;
2255 : : uint32_t len, offset_in_block, offset_blocks;
2256 : 196 : uint64_t guard = 0;
2257 : 147 : struct _dif_sgl sgl;
2258 : : int rc;
2259 : :
2260 [ + - - + ]: 196 : if (iovs == NULL || iovcnt == 0) {
2261 : 0 : return -EINVAL;
2262 : : }
2263 : :
2264 [ + + # # : 196 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
2265 [ # # # # ]: 196 : guard = ctx->last_guard;
2266 : 49 : }
2267 : :
2268 : 196 : _dif_sgl_init(&sgl, iovs, iovcnt);
2269 : :
2270 : 196 : rc = _dif_sgl_setup_stream(&sgl, &buf_offset, &buf_len, data_offset, data_len, ctx);
2271 [ + + ]: 196 : if (rc != 0) {
2272 : 12 : return rc;
2273 : : }
2274 : :
2275 [ + + ]: 488 : while (buf_len != 0) {
2276 [ + + # # : 304 : len = spdk_min(buf_len, _to_next_boundary(buf_offset, ctx->block_size));
# # # # #
# ]
2277 [ - + # # : 304 : offset_in_block = buf_offset % ctx->block_size;
# # ]
2278 [ - + # # : 304 : offset_blocks = buf_offset / ctx->block_size;
# # ]
2279 : :
2280 : 304 : guard = _dif_generate_split(&sgl, offset_in_block, len, guard, offset_blocks, ctx);
2281 : :
2282 : 304 : buf_len -= len;
2283 : 304 : buf_offset += len;
2284 : : }
2285 : :
2286 [ + + # # : 184 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
2287 [ # # # # ]: 184 : ctx->last_guard = guard;
2288 : 46 : }
2289 : :
2290 : 184 : return 0;
2291 : 49 : }
2292 : :
2293 : : int
2294 : 817494 : spdk_dif_verify_stream(struct iovec *iovs, int iovcnt,
2295 : : uint32_t data_offset, uint32_t data_len,
2296 : : struct spdk_dif_ctx *ctx,
2297 : : struct spdk_dif_error *err_blk)
2298 : : {
2299 : 817494 : uint32_t buf_len = 0, buf_offset = 0;
2300 : : uint32_t len, offset_in_block, offset_blocks;
2301 : 817494 : uint64_t guard = 0;
2302 : 27 : struct _dif_sgl sgl;
2303 : 817494 : int rc = 0;
2304 : :
2305 [ + - - + ]: 817494 : if (iovs == NULL || iovcnt == 0) {
2306 : 0 : return -EINVAL;
2307 : : }
2308 : :
2309 [ + + # # : 817494 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
2310 [ # # # # ]: 817494 : guard = ctx->last_guard;
2311 : 9 : }
2312 : :
2313 : 817494 : _dif_sgl_init(&sgl, iovs, iovcnt);
2314 : :
2315 : 817494 : rc = _dif_sgl_setup_stream(&sgl, &buf_offset, &buf_len, data_offset, data_len, ctx);
2316 [ - + ]: 817494 : if (rc != 0) {
2317 : 0 : return rc;
2318 : : }
2319 : :
2320 [ + + ]: 13455876 : while (buf_len != 0) {
2321 [ + + # # : 12638382 : len = spdk_min(buf_len, _to_next_boundary(buf_offset, ctx->block_size));
# # # # #
# ]
2322 [ - + # # : 12638382 : offset_in_block = buf_offset % ctx->block_size;
# # ]
2323 [ - + # # : 12638382 : offset_blocks = buf_offset / ctx->block_size;
# # ]
2324 : :
2325 : 12638400 : rc = _dif_verify_split(&sgl, offset_in_block, len, &guard, offset_blocks,
2326 : 18 : ctx, err_blk);
2327 [ + + ]: 12638382 : if (rc != 0) {
2328 : 0 : goto error;
2329 : : }
2330 : :
2331 : 12638382 : buf_len -= len;
2332 : 12638382 : buf_offset += len;
2333 : : }
2334 : :
2335 [ - + # # : 817503 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
2336 [ # # # # ]: 817494 : ctx->last_guard = guard;
2337 : 9 : }
2338 : 27 : error:
2339 : 817494 : return rc;
2340 : 9 : }
2341 : :
2342 : : int
2343 : 161943 : spdk_dif_update_crc32c_stream(struct iovec *iovs, int iovcnt,
2344 : : uint32_t data_offset, uint32_t data_len,
2345 : : uint32_t *_crc32c, const struct spdk_dif_ctx *ctx)
2346 : : {
2347 : 161943 : uint32_t buf_len = 0, buf_offset = 0, len, offset_in_block;
2348 : : uint32_t crc32c;
2349 : 27 : struct _dif_sgl sgl;
2350 : : int rc;
2351 : :
2352 [ + - - + ]: 161943 : if (iovs == NULL || iovcnt == 0) {
2353 : 0 : return -EINVAL;
2354 : : }
2355 : :
2356 [ # # ]: 161943 : crc32c = *_crc32c;
2357 : 161943 : _dif_sgl_init(&sgl, iovs, iovcnt);
2358 : :
2359 : 161943 : rc = _dif_sgl_setup_stream(&sgl, &buf_offset, &buf_len, data_offset, data_len, ctx);
2360 [ + + ]: 161943 : if (rc != 0) {
2361 : 0 : return rc;
2362 : : }
2363 : :
2364 [ + + ]: 4767162 : while (buf_len != 0) {
2365 [ + + # # : 4605219 : len = spdk_min(buf_len, _to_next_boundary(buf_offset, ctx->block_size));
# # # # #
# ]
2366 [ - + # # : 4605219 : offset_in_block = buf_offset % ctx->block_size;
# # ]
2367 : :
2368 : 4605219 : crc32c = _dif_update_crc32c_split(&sgl, offset_in_block, len, crc32c, ctx);
2369 : :
2370 : 4605219 : buf_len -= len;
2371 : 4605219 : buf_offset += len;
2372 : : }
2373 : :
2374 [ # # ]: 161943 : *_crc32c = crc32c;
2375 : :
2376 : 161943 : return 0;
2377 : 9 : }
2378 : :
2379 : : void
2380 : 1043670 : spdk_dif_get_range_with_md(uint32_t data_offset, uint32_t data_len,
2381 : : uint32_t *_buf_offset, uint32_t *_buf_len,
2382 : : const struct spdk_dif_ctx *ctx)
2383 : : {
2384 : : uint32_t data_block_size, data_unalign, buf_offset, buf_len;
2385 : :
2386 [ + + - + : 1043670 : if (!ctx->md_interleave) {
# # # # ]
2387 : 0 : buf_offset = data_offset;
2388 : 0 : buf_len = data_len;
2389 : 0 : } else {
2390 [ # # # # : 1043670 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
2391 : :
2392 [ - + ]: 1043670 : data_unalign = data_offset % data_block_size;
2393 : :
2394 [ # # # # ]: 1043670 : buf_offset = _to_size_with_md(data_offset, data_block_size, ctx->block_size);
2395 [ # # # # ]: 1043680 : buf_len = _to_size_with_md(data_unalign + data_len, data_block_size, ctx->block_size) -
2396 : 10 : data_unalign;
2397 : : }
2398 : :
2399 [ + + ]: 1043670 : if (_buf_offset != NULL) {
2400 [ # # ]: 1043670 : *_buf_offset = buf_offset;
2401 : 10 : }
2402 : :
2403 [ + + ]: 1043670 : if (_buf_len != NULL) {
2404 [ # # ]: 1043670 : *_buf_len = buf_len;
2405 : 10 : }
2406 : 1043670 : }
2407 : :
2408 : : uint32_t
2409 : 591330 : spdk_dif_get_length_with_md(uint32_t data_len, const struct spdk_dif_ctx *ctx)
2410 : : {
2411 : : uint32_t data_block_size;
2412 : :
2413 [ - + - + : 591330 : if (!ctx->md_interleave) {
# # # # ]
2414 : 0 : return data_len;
2415 : : } else {
2416 [ # # # # : 591330 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
2417 : :
2418 [ # # # # ]: 591330 : return _to_size_with_md(data_len, data_block_size, ctx->block_size);
2419 : : }
2420 : 11 : }
2421 : :
2422 : : static int
2423 : 288 : _dif_remap_ref_tag(struct _dif_sgl *sgl, uint32_t offset_blocks,
2424 : : const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk,
2425 : : bool check_ref_tag)
2426 : : {
2427 : 216 : uint32_t offset, buf_len;
2428 : 288 : uint64_t expected = 0, remapped;
2429 : 216 : uint8_t *buf;
2430 : 216 : struct _dif_sgl tmp_sgl;
2431 : 216 : struct spdk_dif dif;
2432 : :
2433 : : /* Fast forward to DIF field. */
2434 [ # # # # ]: 288 : _dif_sgl_advance(sgl, ctx->guard_interval);
2435 : 288 : _dif_sgl_copy(&tmp_sgl, sgl);
2436 : :
2437 : : /* Copy the split DIF field to the temporary DIF buffer */
2438 : 288 : offset = 0;
2439 [ + + # # : 648 : while (offset < _dif_size(ctx->dif_pi_format)) {
# # ]
2440 : 360 : _dif_sgl_get_buf(sgl, &buf, &buf_len);
2441 [ + + # # : 360 : buf_len = spdk_min(buf_len, _dif_size(ctx->dif_pi_format) - offset);
# # # # #
# ]
2442 : :
2443 [ - + - + : 360 : memcpy((uint8_t *)&dif + offset, buf, buf_len);
# # ]
2444 : :
2445 : 360 : _dif_sgl_advance(sgl, buf_len);
2446 : 360 : offset += buf_len;
2447 : : }
2448 : :
2449 [ - + ]: 288 : if (_dif_ignore(&dif, ctx)) {
2450 : 0 : goto end;
2451 : : }
2452 : :
2453 : : /* For type 1 and 2, the Reference Tag is incremented for each
2454 : : * subsequent logical block. For type 3, the Reference Tag
2455 : : * remains the same as the initial Reference Tag.
2456 : : */
2457 [ + - # # : 288 : if (ctx->dif_type != SPDK_DIF_TYPE3) {
# # ]
2458 [ # # # # : 288 : expected = ctx->init_ref_tag + ctx->ref_tag_offset + offset_blocks;
# # # # ]
2459 [ # # # # : 288 : remapped = ctx->remapped_init_ref_tag + ctx->ref_tag_offset + offset_blocks;
# # # # ]
2460 : 72 : } else {
2461 [ # # # # ]: 0 : remapped = ctx->remapped_init_ref_tag;
2462 : : }
2463 : :
2464 : : /* Verify the stored Reference Tag. */
2465 [ + - + + : 288 : if (check_ref_tag && !_dif_reftag_check(&dif, ctx, expected, offset_blocks, err_blk)) {
# # ]
2466 : 0 : return -1;
2467 : : }
2468 : :
2469 : : /* Update the stored Reference Tag to the remapped one. */
2470 [ # # # # ]: 288 : _dif_set_reftag(&dif, remapped, ctx->dif_pi_format);
2471 : :
2472 : 288 : offset = 0;
2473 [ + + # # : 648 : while (offset < _dif_size(ctx->dif_pi_format)) {
# # ]
2474 : 360 : _dif_sgl_get_buf(&tmp_sgl, &buf, &buf_len);
2475 [ + + # # : 360 : buf_len = spdk_min(buf_len, _dif_size(ctx->dif_pi_format) - offset);
# # # # #
# ]
2476 : :
2477 [ - + - + : 360 : memcpy(buf, (uint8_t *)&dif + offset, buf_len);
# # ]
2478 : :
2479 : 360 : _dif_sgl_advance(&tmp_sgl, buf_len);
2480 : 360 : offset += buf_len;
2481 : : }
2482 : :
2483 : 216 : end:
2484 [ # # # # : 288 : _dif_sgl_advance(sgl, ctx->block_size - ctx->guard_interval - _dif_size(ctx->dif_pi_format));
# # # # #
# # # ]
2485 : :
2486 : 288 : return 0;
2487 : 72 : }
2488 : :
2489 : : int
2490 : 48 : spdk_dif_remap_ref_tag(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
2491 : : const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk,
2492 : : bool check_ref_tag)
2493 : : {
2494 : 36 : struct _dif_sgl sgl;
2495 : : uint32_t offset_blocks;
2496 : : int rc;
2497 : :
2498 : 48 : _dif_sgl_init(&sgl, iovs, iovcnt);
2499 : :
2500 [ - + # # : 48 : if (!_dif_sgl_is_valid(&sgl, ctx->block_size * num_blocks)) {
# # ]
2501 : 0 : SPDK_ERRLOG("Size of iovec array is not valid.\n");
2502 : 0 : return -EINVAL;
2503 : : }
2504 : :
2505 [ - + # # : 48 : if (_dif_is_disabled(ctx->dif_type)) {
# # ]
2506 : 0 : return 0;
2507 : : }
2508 : :
2509 [ + + # # : 48 : if (!(ctx->dif_flags & SPDK_DIF_FLAGS_REFTAG_CHECK)) {
# # # # ]
2510 : 0 : return 0;
2511 : : }
2512 : :
2513 [ + + ]: 336 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
2514 [ # # ]: 288 : rc = _dif_remap_ref_tag(&sgl, offset_blocks, ctx, err_blk, check_ref_tag);
2515 [ + + ]: 288 : if (rc != 0) {
2516 : 0 : return rc;
2517 : : }
2518 : 72 : }
2519 : :
2520 : 48 : return 0;
2521 : 12 : }
2522 : :
2523 : : static int
2524 : 800 : _dix_remap_ref_tag(struct _dif_sgl *md_sgl, uint32_t offset_blocks,
2525 : : const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk,
2526 : : bool check_ref_tag)
2527 : : {
2528 : 800 : uint64_t expected = 0, remapped;
2529 : 600 : uint8_t *md_buf;
2530 : : struct spdk_dif *dif;
2531 : :
2532 : 800 : _dif_sgl_get_buf(md_sgl, &md_buf, NULL);
2533 : :
2534 [ # # # # : 800 : dif = (struct spdk_dif *)(md_buf + ctx->guard_interval);
# # ]
2535 : :
2536 [ - + ]: 800 : if (_dif_ignore(dif, ctx)) {
2537 : 0 : goto end;
2538 : : }
2539 : :
2540 : : /* For type 1 and 2, the Reference Tag is incremented for each
2541 : : * subsequent logical block. For type 3, the Reference Tag
2542 : : * remains the same as the initialReference Tag.
2543 : : */
2544 [ + - # # : 800 : if (ctx->dif_type != SPDK_DIF_TYPE3) {
# # ]
2545 [ # # # # : 800 : expected = ctx->init_ref_tag + ctx->ref_tag_offset + offset_blocks;
# # # # ]
2546 [ # # # # : 800 : remapped = ctx->remapped_init_ref_tag + ctx->ref_tag_offset + offset_blocks;
# # # # ]
2547 : 200 : } else {
2548 [ # # # # ]: 0 : remapped = ctx->remapped_init_ref_tag;
2549 : : }
2550 : :
2551 : : /* Verify the stored Reference Tag. */
2552 [ + + + + : 800 : if (check_ref_tag && !_dif_reftag_check(dif, ctx, expected, offset_blocks, err_blk)) {
# # ]
2553 : 0 : return -1;
2554 : : }
2555 : :
2556 : : /* Update the stored Reference Tag to the remapped one. */
2557 [ # # # # ]: 800 : _dif_set_reftag(dif, remapped, ctx->dif_pi_format);
2558 : :
2559 : 600 : end:
2560 [ # # # # ]: 800 : _dif_sgl_advance(md_sgl, ctx->md_size);
2561 : :
2562 : 800 : return 0;
2563 : 200 : }
2564 : :
2565 : : int
2566 : 48 : spdk_dix_remap_ref_tag(struct iovec *md_iov, uint32_t num_blocks,
2567 : : const struct spdk_dif_ctx *ctx,
2568 : : struct spdk_dif_error *err_blk,
2569 : : bool check_ref_tag)
2570 : : {
2571 : 36 : struct _dif_sgl md_sgl;
2572 : : uint32_t offset_blocks;
2573 : : int rc;
2574 : :
2575 : 48 : _dif_sgl_init(&md_sgl, md_iov, 1);
2576 : :
2577 [ - + # # : 48 : if (!_dif_sgl_is_valid(&md_sgl, ctx->md_size * num_blocks)) {
# # ]
2578 : 0 : SPDK_ERRLOG("Size of metadata iovec array is not valid.\n");
2579 : 0 : return -EINVAL;
2580 : : }
2581 : :
2582 [ - + # # : 48 : if (_dif_is_disabled(ctx->dif_type)) {
# # ]
2583 : 0 : return 0;
2584 : : }
2585 : :
2586 [ + + # # : 48 : if (!(ctx->dif_flags & SPDK_DIF_FLAGS_REFTAG_CHECK)) {
# # # # ]
2587 : 0 : return 0;
2588 : : }
2589 : :
2590 [ + + ]: 848 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
2591 [ # # ]: 800 : rc = _dix_remap_ref_tag(&md_sgl, offset_blocks, ctx, err_blk, check_ref_tag);
2592 [ + + ]: 800 : if (rc != 0) {
2593 : 0 : return rc;
2594 : : }
2595 : 200 : }
2596 : :
2597 : 48 : return 0;
2598 : 12 : }
|