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 : 8248795 : _dif_sgl_init(struct _dif_sgl *s, struct iovec *iovs, int iovcnt)
65 : : {
66 [ # # # # ]: 8248795 : s->iov = iovs;
67 [ # # # # ]: 8248795 : s->iovcnt = iovcnt;
68 [ # # # # ]: 8248795 : s->iov_offset = 0;
69 [ # # # # ]: 8248795 : s->total_size = 0;
70 : 8248795 : }
71 : :
72 : : static void
73 : 155315303 : _dif_sgl_advance(struct _dif_sgl *s, uint32_t step)
74 : : {
75 [ # # # # ]: 155315303 : s->iov_offset += step;
76 [ + + # # : 161129286 : while (s->iovcnt != 0) {
# # ]
77 [ + + # # : 155508666 : if (s->iov_offset < s->iov->iov_len) {
# # # # #
# # # #
# ]
78 : 149694683 : break;
79 : : }
80 : :
81 [ # # # # : 5813983 : s->iov_offset -= s->iov->iov_len;
# # # # #
# # # ]
82 [ # # # # ]: 5813983 : s->iov++;
83 [ # # # # ]: 5813983 : s->iovcnt--;
84 : : }
85 : 155315303 : }
86 : :
87 : : static inline void
88 : 120713592 : _dif_sgl_get_buf(struct _dif_sgl *s, uint8_t **_buf, uint32_t *_buf_len)
89 : : {
90 [ + - ]: 120713592 : if (_buf != NULL) {
91 [ # # # # : 120713592 : *_buf = (uint8_t *)s->iov->iov_base + s->iov_offset;
# # # # #
# # # # #
# # ]
92 : 16450 : }
93 [ + + ]: 120713592 : if (_buf_len != NULL) {
94 [ # # # # : 96840027 : *_buf_len = s->iov->iov_len - s->iov_offset;
# # # # #
# # # #
# ]
95 : 10045 : }
96 : 120713592 : }
97 : :
98 : : static inline bool
99 : 32345828 : _dif_sgl_append(struct _dif_sgl *s, uint8_t *data, uint32_t data_len)
100 : : {
101 [ + + # # : 32345828 : assert(s->iovcnt > 0);
# # # # ]
102 [ # # # # : 32345828 : s->iov->iov_base = data;
# # # # ]
103 [ # # # # : 32345828 : s->iov->iov_len = data_len;
# # # # ]
104 [ # # # # ]: 32345828 : s->total_size += data_len;
105 [ # # # # ]: 32345828 : s->iov++;
106 [ # # # # ]: 32345828 : s->iovcnt--;
107 : :
108 [ + + # # : 32345828 : if (s->iovcnt > 0) {
# # ]
109 : 31928906 : return true;
110 : : } else {
111 : 416922 : return false;
112 : : }
113 : 120 : }
114 : :
115 : : static inline bool
116 : 32269260 : _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 [ + + ]: 64198166 : while (data_len != 0) {
122 : 32345828 : _dif_sgl_get_buf(src, &buf, &buf_len);
123 [ + + ]: 32345828 : buf_len = spdk_min(buf_len, data_len);
124 : :
125 [ + + ]: 32345828 : if (!_dif_sgl_append(dst, buf, buf_len)) {
126 : 416922 : return false;
127 : : }
128 : :
129 : 31928906 : _dif_sgl_advance(src, buf_len);
130 : 31928906 : data_len -= buf_len;
131 : : }
132 : :
133 : 31852338 : return true;
134 : 104 : }
135 : :
136 : : /* This function must be used before starting iteration. */
137 : : static bool
138 : 2622931 : _dif_sgl_is_bytes_multiple(struct _dif_sgl *s, uint32_t bytes)
139 : : {
140 : : int i;
141 : :
142 [ + + # # : 5210098 : for (i = 0; i < s->iovcnt; i++) {
# # # # ]
143 [ + + + + : 2627179 : if (s->iov[i].iov_len % bytes) {
# # # # #
# # # #
# ]
144 : 40012 : return false;
145 : : }
146 : 1660 : }
147 : :
148 : 2582919 : return true;
149 : 1053 : }
150 : :
151 : : /* This function must be used before starting iteration. */
152 : : static bool
153 : 6284411 : _dif_sgl_is_valid(struct _dif_sgl *s, uint32_t bytes)
154 : : {
155 : 6284411 : uint64_t total = 0;
156 : : int i;
157 : :
158 [ + + # # : 13069383 : for (i = 0; i < s->iovcnt; i++) {
# # # # ]
159 [ # # # # : 6784972 : total += s->iov[i].iov_len;
# # # # #
# ]
160 : 6882 : }
161 : :
162 : 6284411 : 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 : 2467598 : _dif_is_disabled(enum spdk_dif_type dif_type)
173 : : {
174 [ + + ]: 2467598 : if (dif_type == SPDK_DIF_DISABLE) {
175 : 122032 : return true;
176 : : } else {
177 : 2345566 : return false;
178 : : }
179 : 926 : }
180 : :
181 : : static inline size_t
182 : 80746428 : _dif_size(enum spdk_dif_pi_format dif_pi_format)
183 : : {
184 : : uint8_t size;
185 : :
186 [ + + ]: 80746428 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
187 : 80736080 : size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g16);
188 [ + + ]: 11807 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
189 : 5264 : size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g32);
190 : 1330 : } else {
191 : 5084 : size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g64);
192 : : }
193 : :
194 : 80746428 : return size;
195 : : }
196 : :
197 : : static uint32_t
198 : 1476909 : _get_guard_interval(uint32_t block_size, uint32_t md_size, bool dif_loc, bool md_interleave,
199 : : size_t dif_size)
200 : : {
201 [ + + # # ]: 1476909 : 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 [ + + # # ]: 1475302 : if (md_interleave) {
208 : 1352990 : return block_size - dif_size;
209 : : } else {
210 : 122312 : 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 : 24898394 : _dif_set_guard(struct spdk_dif *dif, uint64_t guard, enum spdk_dif_pi_format dif_pi_format)
243 : : {
244 [ + + ]: 24898394 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
245 [ # # # # : 24892738 : 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 : 24898394 : }
252 : :
253 : : static inline uint64_t
254 : 20713201 : _dif_get_guard(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
255 : : {
256 : : uint64_t guard;
257 : :
258 [ + + ]: 20713201 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
259 [ # # # # : 20709823 : guard = (uint64_t)from_be16(&(dif->g16.guard));
# # ]
260 [ + + ]: 4183 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
261 [ # # # # : 1682 : guard = (uint64_t)from_be32(&(dif->g32.guard));
# # ]
262 : 345 : } else {
263 [ # # # # : 1696 : guard = from_be64(&(dif->g64.guard));
# # ]
264 : : }
265 : :
266 : 20713201 : return guard;
267 : : }
268 : :
269 : : static inline uint64_t
270 : 47655510 : _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 [ + + ]: 47655510 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
276 : 47644748 : guard = (uint64_t)spdk_crc16_t10dif((uint16_t)guard_seed, buf, buf_len);
277 [ + + ]: 13467 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
278 : 5434 : guard = (uint64_t)spdk_crc32c_nvme(buf, buf_len, guard_seed);
279 : 1261 : } else {
280 : 5328 : guard = spdk_crc64_nvme(buf, buf_len, guard_seed);
281 : : }
282 : :
283 : 47655510 : return guard;
284 : : }
285 : :
286 : : static uint64_t
287 : 25929254 : 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 : 25929254 : uint64_t guard = guard_seed;
291 : 1728 : uint32_t offset, end, buf_len;
292 : 1728 : uint8_t *buf;
293 : :
294 : 25929254 : offset = start;
295 [ + + # # : 25929254 : end = start + spdk_min(len, ctx->guard_interval - start);
# # # # #
# ]
296 : :
297 [ + + ]: 51936088 : while (offset < end) {
298 : 26006834 : _dif_sgl_get_buf(sgl, &buf, &buf_len);
299 [ + + ]: 26006834 : buf_len = spdk_min(buf_len, end - offset);
300 : :
301 [ + + # # : 26006834 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
302 [ # # # # ]: 26006834 : guard = _dif_generate_guard(guard, buf, buf_len, ctx->dif_pi_format);
303 : 855 : }
304 : :
305 : 26006834 : _dif_sgl_advance(sgl, buf_len);
306 : 26006834 : offset += buf_len;
307 : : }
308 : :
309 : 25929254 : return guard;
310 : : }
311 : :
312 : : static inline uint64_t
313 : 2217969 : _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 [ + + ]: 2217969 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
319 : 2215515 : guard = (uint64_t)spdk_crc16_t10dif_copy((uint16_t)guard_seed, dst, src, buf_len);
320 [ + + ]: 2720 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
321 [ - + - + ]: 1222 : memcpy(dst, src, buf_len);
322 : 1222 : guard = (uint64_t)spdk_crc32c_nvme(src, buf_len, guard_seed);
323 : 288 : } else {
324 [ - + - + ]: 1232 : memcpy(dst, src, buf_len);
325 : 1232 : guard = spdk_crc64_nvme(src, buf_len, guard_seed);
326 : : }
327 : :
328 : 2217969 : return guard;
329 : : }
330 : :
331 : : static uint64_t
332 : 552 : _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 : 552 : uint32_t offset = 0, src_len, dst_len, buf_len;
337 : 402 : uint8_t *src, *dst;
338 : :
339 [ + + ]: 1664 : while (offset < data_len) {
340 : 1112 : _dif_sgl_get_buf(src_sgl, &src, &src_len);
341 : 1112 : _dif_sgl_get_buf(dst_sgl, &dst, &dst_len);
342 [ + + ]: 1112 : buf_len = spdk_min(src_len, dst_len);
343 [ + + ]: 1112 : buf_len = spdk_min(buf_len, data_len - offset);
344 : :
345 : 1112 : guard = _dif_generate_guard_copy(guard, dst, src, buf_len, dif_pi_format);
346 : :
347 : 1112 : _dif_sgl_advance(src_sgl, buf_len);
348 : 1112 : _dif_sgl_advance(dst_sgl, buf_len);
349 : 1112 : offset += buf_len;
350 : : }
351 : :
352 : 552 : 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 : 24898394 : _dif_set_apptag(struct spdk_dif *dif, uint16_t app_tag, enum spdk_dif_pi_format dif_pi_format)
389 : : {
390 [ + + ]: 24898394 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
391 [ # # # # : 24892738 : 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 : 24898394 : }
398 : :
399 : : static inline uint16_t
400 : 25295034 : _dif_get_apptag(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
401 : : {
402 : : uint16_t app_tag;
403 : :
404 [ + + ]: 25295034 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
405 [ # # # # : 25286970 : app_tag = from_be16(&(dif->g16.app_tag));
# # ]
406 [ + + ]: 9923 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
407 [ # # # # : 4024 : app_tag = from_be16(&(dif->g32.app_tag));
# # ]
408 : 855 : } else {
409 [ # # # # : 4040 : app_tag = from_be16(&(dif->g64.app_tag));
# # ]
410 : : }
411 : :
412 : 25295034 : return app_tag;
413 : : }
414 : :
415 : : static inline bool
416 : 20716743 : _dif_apptag_ignore(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
417 : : {
418 : 20716743 : 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 : 24899482 : _dif_set_reftag(struct spdk_dif *dif, uint64_t ref_tag, enum spdk_dif_pi_format dif_pi_format)
457 : : {
458 [ + + ]: 24899482 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
459 [ # # # # : 24893442 : 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 : 24899482 : }
467 : :
468 : : static inline uint64_t
469 : 10920410 : _dif_get_reftag(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
470 : : {
471 : : uint64_t ref_tag;
472 : :
473 [ + + ]: 10920410 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
474 [ # # # # : 10917020 : ref_tag = (uint64_t)from_be32(&(dif->g16.stor_ref_space));
# # ]
475 [ + + ]: 4204 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
476 [ # # # # : 1690 : ref_tag = from_be64(&(dif->g32.stor_ref_space_p2));
# # ]
477 : 347 : } else {
478 [ # # # # : 1700 : ref_tag = (uint64_t)from_be16(&(dif->g64.stor_ref_space_p1));
# # ]
479 [ # # ]: 1700 : ref_tag <<= 32;
480 [ # # # # : 1700 : ref_tag |= (uint64_t)from_be32(&(dif->g64.stor_ref_space_p2));
# # ]
481 : : }
482 : :
483 : 10920410 : return ref_tag;
484 : : }
485 : :
486 : : static inline bool
487 : 10920120 : _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 : 10920120 : _ref_tag = _dif_get_reftag(dif, dif_pi_format);
494 : :
495 [ + + ]: 10920120 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
496 : 10916906 : match = (_ref_tag == (ref_tag & REFTAG_MASK_16));
497 [ + + ]: 4004 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
498 : 1602 : match = (_ref_tag == ref_tag);
499 : 325 : } else {
500 : 1612 : match = (_ref_tag == (ref_tag & REFTAG_MASK_64));
501 : : }
502 : :
503 [ # # ]: 10920120 : 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 : 20716743 : _dif_ignore(struct spdk_dif *dif, const struct spdk_dif_ctx *ctx)
514 : : {
515 [ + + + # : 20716743 : switch (ctx->dif_type) {
# # # ]
516 : 10921566 : 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 [ + + # # : 10923765 : if (_dif_apptag_ignore(dif, ctx->dif_pi_format)) {
# # ]
522 : 112 : return true;
523 : : }
524 : 10923653 : break;
525 : 9792971 : 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 [ + + + + : 9792999 : 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 : 9792962 : break;
536 : 0 : default:
537 : 0 : break;
538 : : }
539 : :
540 : 20716615 : return false;
541 : 2206 : }
542 : :
543 : : static bool
544 : 1476917 : _dif_pi_format_is_valid(enum spdk_dif_pi_format dif_pi_format)
545 : : {
546 [ + + ]: 1476917 : switch (dif_pi_format) {
547 : 1476372 : case SPDK_DIF_PI_FORMAT_16:
548 : : case SPDK_DIF_PI_FORMAT_32:
549 : : case SPDK_DIF_PI_FORMAT_64:
550 : 1476913 : return true;
551 : 3 : default:
552 : 4 : return false;
553 : : }
554 : 542 : }
555 : :
556 : : static bool
557 : 1476921 : _dif_type_is_valid(enum spdk_dif_type dif_type)
558 : : {
559 [ + + ]: 1476921 : switch (dif_type) {
560 : 1476375 : case SPDK_DIF_DISABLE:
561 : : case SPDK_DIF_TYPE1:
562 : : case SPDK_DIF_TYPE2:
563 : : case SPDK_DIF_TYPE3:
564 : 1476917 : return true;
565 : 3 : default:
566 : 4 : return false;
567 : : }
568 : 543 : }
569 : :
570 : : int
571 : 1476901 : 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 : 1476901 : enum spdk_dif_pi_format dif_pi_format = SPDK_DIF_PI_FORMAT_16;
578 : :
579 [ + + ]: 1476901 : if (opts != NULL) {
580 [ + + # # : 1476901 : 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 [ # # # # ]: 1476901 : dif_pi_format = opts->dif_pi_format;
586 : 538 : }
587 : :
588 [ + + ]: 1476901 : 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 [ + + ]: 1476901 : 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 [ + + # # ]: 1476857 : if (md_interleave) {
599 [ - + ]: 1354163 : if (block_size < md_size) {
600 : 0 : SPDK_ERRLOG("Block size is smaller than DIF size.\n");
601 : 0 : return -EINVAL;
602 : : }
603 : 1354163 : data_block_size = block_size - md_size;
604 : 406 : } else {
605 : 122694 : data_block_size = block_size;
606 : : }
607 : :
608 [ + + ]: 1476857 : if (data_block_size == 0) {
609 : 8 : SPDK_ERRLOG("Zero data block size is not allowed\n");
610 : 8 : return -EINVAL;
611 : : }
612 : :
613 [ + + ]: 1476849 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
614 [ + + # # ]: 1475525 : 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 [ # # # # ]: 1476825 : ctx->block_size = block_size;
626 [ # # # # ]: 1476825 : ctx->md_size = md_size;
627 [ # # # # : 1476825 : ctx->md_interleave = md_interleave;
# # ]
628 [ # # # # ]: 1476825 : ctx->dif_pi_format = dif_pi_format;
629 [ # # # # : 1477344 : ctx->guard_interval = _get_guard_interval(block_size, md_size, dif_loc, md_interleave,
# # # # ]
630 [ # # # # ]: 1476825 : _dif_size(ctx->dif_pi_format));
631 [ # # # # ]: 1476825 : ctx->dif_type = dif_type;
632 [ # # # # ]: 1476825 : ctx->dif_flags = dif_flags;
633 [ # # # # ]: 1476825 : ctx->init_ref_tag = init_ref_tag;
634 [ # # # # ]: 1476825 : ctx->apptag_mask = apptag_mask;
635 [ # # # # ]: 1476825 : ctx->app_tag = app_tag;
636 [ # # # # ]: 1476825 : ctx->data_offset = data_offset;
637 [ - + # # : 1476825 : ctx->ref_tag_offset = data_offset / data_block_size;
# # ]
638 [ # # # # ]: 1476825 : ctx->last_guard = guard_seed;
639 [ # # # # ]: 1476825 : ctx->guard_seed = guard_seed;
640 [ # # # # ]: 1476825 : ctx->remapped_init_ref_tag = 0;
641 : :
642 : 1476825 : return 0;
643 : 538 : }
644 : :
645 : : void
646 : 1288306 : spdk_dif_ctx_set_data_offset(struct spdk_dif_ctx *ctx, uint32_t data_offset)
647 : : {
648 : : uint32_t data_block_size;
649 : :
650 [ + + + - : 1288306 : if (ctx->md_interleave) {
# # # # ]
651 [ # # # # : 1288306 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
652 : 42 : } else {
653 [ # # # # ]: 0 : data_block_size = ctx->block_size;
654 : : }
655 : :
656 [ # # # # ]: 1288306 : ctx->data_offset = data_offset;
657 [ - + # # : 1288306 : ctx->ref_tag_offset = data_offset / data_block_size;
# # ]
658 : 1288306 : }
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 : 24898394 : _dif_generate(void *_dif, uint64_t guard, uint32_t offset_blocks,
669 : : const struct spdk_dif_ctx *ctx)
670 : : {
671 : 24898394 : struct spdk_dif *dif = _dif;
672 : : uint64_t ref_tag;
673 : :
674 [ + + # # : 24898394 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
675 [ # # # # ]: 24896210 : _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 [ + + # # : 24898394 : if (ctx->dif_flags & SPDK_DIF_FLAGS_APPTAG_CHECK) {
# # # # ]
681 [ # # # # : 8761852 : _dif_set_apptag(dif, ctx->app_tag, ctx->dif_pi_format);
# # # # ]
682 : 1865 : } else {
683 [ # # # # ]: 16136542 : _dif_set_apptag(dif, 0, ctx->dif_pi_format);
684 : : }
685 : :
686 [ + + # # : 24898394 : 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 [ + + # # : 15103408 : if (ctx->dif_type != SPDK_DIF_TYPE3) {
# # ]
692 [ # # # # : 15103380 : 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 [ + + # # : 15103408 : 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 [ # # # # ]: 15103408 : _dif_set_reftag(dif, ref_tag, ctx->dif_pi_format);
709 : 1862 : } else {
710 [ # # # # ]: 9794986 : _dif_set_reftag(dif, 0, ctx->dif_pi_format);
711 : : }
712 : 24898394 : }
713 : :
714 : : static void
715 : 1455491 : dif_generate(struct _dif_sgl *sgl, uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
716 : : {
717 : : uint32_t offset_blocks;
718 : 295931 : uint8_t *buf;
719 : 1455491 : uint64_t guard = 0;
720 : :
721 [ + + ]: 14338613 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
722 : 12883122 : _dif_sgl_get_buf(sgl, &buf, NULL);
723 : :
724 [ + + # # : 12883122 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
725 [ # # # # : 12882570 : guard = _dif_generate_guard(ctx->guard_seed, buf, ctx->guard_interval, ctx->dif_pi_format);
# # # # #
# # # ]
726 : 475 : }
727 : :
728 [ # # # # : 12883122 : _dif_generate(buf + ctx->guard_interval, guard, offset_blocks, ctx);
# # ]
729 : :
730 [ # # # # ]: 12883122 : _dif_sgl_advance(sgl, ctx->block_size);
731 : 577 : }
732 : 1455491 : }
733 : :
734 : : static void
735 : 9793704 : dif_store_split(struct _dif_sgl *sgl, struct spdk_dif *dif,
736 : : const struct spdk_dif_ctx *ctx)
737 : : {
738 : 9793704 : uint32_t offset = 0, rest_md_len, buf_len;
739 : 894 : uint8_t *buf;
740 : :
741 [ # # # # : 9793704 : rest_md_len = ctx->block_size - ctx->guard_interval;
# # # # ]
742 : :
743 [ + + ]: 19588224 : while (offset < rest_md_len) {
744 : 9794520 : _dif_sgl_get_buf(sgl, &buf, &buf_len);
745 : :
746 [ + + # # : 9794520 : if (offset < _dif_size(ctx->dif_pi_format)) {
# # ]
747 [ + + # # : 9794056 : buf_len = spdk_min(buf_len, _dif_size(ctx->dif_pi_format) - offset);
# # # # #
# ]
748 [ - + - + : 9794056 : 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 : 9794520 : _dif_sgl_advance(sgl, buf_len);
754 : 9794520 : offset += buf_len;
755 : : }
756 : 9793704 : }
757 : :
758 : : static uint64_t
759 : 9793560 : _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 : 9793560 : struct spdk_dif dif = {};
763 : :
764 [ + + # # : 9793560 : assert(offset_in_block < ctx->guard_interval);
# # # # ]
765 [ + + - + : 9793560 : 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 : 9793560 : guard = dif_generate_guard_split(guard, sgl, offset_in_block, data_len, ctx);
770 : :
771 [ + + # # : 9793560 : 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 : 9793404 : _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 : 9793404 : dif_store_split(sgl, &dif, ctx);
784 : :
785 [ + + # # : 9793404 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
786 [ # # # # ]: 9793404 : guard = ctx->guard_seed;
787 : 223 : }
788 : :
789 : 9793404 : return guard;
790 : 262 : }
791 : :
792 : : static void
793 : 38860 : 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 : 38860 : uint64_t guard = 0;
798 : :
799 [ + + # # : 38860 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
800 [ # # # # ]: 38860 : guard = ctx->guard_seed;
801 : 152 : }
802 : :
803 [ + + ]: 9832080 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
804 [ # # # # ]: 9793220 : _dif_generate_split(sgl, 0, ctx->block_size, guard, offset_blocks, ctx);
805 : 177 : }
806 : 38860 : }
807 : :
808 : : int
809 : 1494319 : spdk_dif_generate(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
810 : : const struct spdk_dif_ctx *ctx)
811 : : {
812 : 296363 : struct _dif_sgl sgl;
813 : :
814 : 1494319 : _dif_sgl_init(&sgl, iovs, iovcnt);
815 : :
816 [ - + # # : 1494319 : 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 [ + + # # : 1494319 : if (_dif_is_disabled(ctx->dif_type)) {
# # ]
822 : 4 : return 0;
823 : : }
824 : :
825 [ + + # # : 1494315 : if (_dif_sgl_is_bytes_multiple(&sgl, ctx->block_size)) {
# # ]
826 : 1455455 : dif_generate(&sgl, num_blocks, ctx);
827 : 91 : } else {
828 : 38860 : dif_generate_split(&sgl, num_blocks, ctx);
829 : : }
830 : :
831 : 1494315 : 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 : 20715286 : _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 [ + + # # : 20715286 : if (ctx->dif_flags & SPDK_DIF_FLAGS_REFTAG_CHECK) {
# # # # ]
853 [ + - - # : 10920092 : switch (ctx->dif_type) {
# # # ]
854 : 10918659 : 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 [ + + # # : 10920092 : 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 : 10919810 : 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 : 1367 : }
881 : :
882 : 20715004 : return true;
883 : 1874 : }
884 : :
885 : : static int
886 : 20715655 : _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 : 20715655 : struct spdk_dif *dif = _dif;
890 : : uint64_t _guard;
891 : : uint16_t _app_tag;
892 : : uint64_t ref_tag;
893 : :
894 [ + + ]: 20715655 : 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 [ + + # # : 20715527 : if (ctx->dif_type != SPDK_DIF_TYPE3) {
# # ]
903 [ # # # # : 10922565 : ref_tag = ctx->init_ref_tag + ctx->ref_tag_offset + offset_blocks;
# # # # ]
904 : 1923 : } else {
905 [ # # # # : 9792962 : ref_tag = ctx->init_ref_tag + ctx->ref_tag_offset;
# # # # ]
906 : : }
907 : :
908 [ + + # # : 20715527 : 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 [ # # # # ]: 20713161 : _guard = _dif_get_guard(dif, ctx->dif_pi_format);
913 [ + + ]: 20713161 : 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 : 1362 : }
922 : :
923 [ + + # # : 20715016 : 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 [ # # # # ]: 4578283 : _app_tag = _dif_get_apptag(dif, ctx->dif_pi_format);
928 [ + + # # : 4578283 : 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 : 1288 : }
937 : :
938 [ + + ]: 20714710 : if (!_dif_reftag_check(dif, ctx, ref_tag, offset_blocks, err_blk)) {
939 : 282 : return -1;
940 : : }
941 : :
942 : 20714428 : return 0;
943 : 1934 : }
944 : :
945 : : static int
946 : 326842 : 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 : 117389 : uint8_t *buf;
952 : 326842 : uint64_t guard = 0;
953 : :
954 [ + + ]: 2939942 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
955 : 2613263 : _dif_sgl_get_buf(sgl, &buf, NULL);
956 : :
957 [ + + # # : 2613263 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
958 [ # # # # : 2612571 : guard = _dif_generate_guard(ctx->guard_seed, buf, ctx->guard_interval, ctx->dif_pi_format);
# # # # #
# # # ]
959 : 298 : }
960 : :
961 [ # # # # : 2613263 : rc = _dif_verify(buf + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
# # ]
962 [ + + ]: 2613263 : if (rc != 0) {
963 : 163 : return rc;
964 : : }
965 : :
966 [ # # # # ]: 2613100 : _dif_sgl_advance(sgl, ctx->block_size);
967 : 365 : }
968 : :
969 : 326679 : return 0;
970 : 105 : }
971 : :
972 : : static void
973 : 16135398 : dif_load_split(struct _dif_sgl *sgl, struct spdk_dif *dif,
974 : : const struct spdk_dif_ctx *ctx)
975 : : {
976 : 16135398 : uint32_t offset = 0, rest_md_len, buf_len;
977 : 720 : uint8_t *buf;
978 : :
979 [ # # # # : 16135398 : rest_md_len = ctx->block_size - ctx->guard_interval;
# # # # ]
980 : :
981 [ + + ]: 32271576 : while (offset < rest_md_len) {
982 : 16136178 : _dif_sgl_get_buf(sgl, &buf, &buf_len);
983 : :
984 [ + + # # : 16136178 : if (offset < _dif_size(ctx->dif_pi_format)) {
# # ]
985 [ + + # # : 16135738 : buf_len = spdk_min(buf_len, _dif_size(ctx->dif_pi_format) - offset);
# # # # #
# ]
986 [ - + - + : 16135738 : memcpy((uint8_t *)dif + offset, buf, buf_len);
# # ]
987 : 341 : } else {
988 [ + + ]: 440 : buf_len = spdk_min(buf_len, rest_md_len - offset);
989 : : }
990 : :
991 : 16136178 : _dif_sgl_advance(sgl, buf_len);
992 : 16136178 : offset += buf_len;
993 : : }
994 : 16135398 : }
995 : :
996 : : static int
997 : 16135206 : _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 [ # # ]: 16135206 : uint64_t guard = *_guard;
1002 : 16135206 : struct spdk_dif dif = {};
1003 : : int rc;
1004 : :
1005 [ + + # # ]: 16135206 : assert(_guard != NULL);
1006 [ + + # # : 16135206 : assert(offset_in_block < ctx->guard_interval);
# # # # ]
1007 [ + + - + : 16135206 : assert(offset_in_block + data_len < ctx->guard_interval ||
# # # # #
# # # #
# ]
1008 : : offset_in_block + data_len == ctx->block_size);
1009 : :
1010 : 16135206 : guard = dif_generate_guard_split(guard, sgl, offset_in_block, data_len, ctx);
1011 : :
1012 [ + + # # : 16135206 : if (offset_in_block + data_len < ctx->guard_interval) {
# # ]
1013 [ # # ]: 60 : *_guard = guard;
1014 : 60 : return 0;
1015 : : }
1016 : :
1017 : 16135146 : dif_load_split(sgl, &dif, ctx);
1018 : :
1019 : 16135146 : rc = _dif_verify(&dif, guard, offset_blocks, ctx, err_blk);
1020 [ + + ]: 16135146 : if (rc != 0) {
1021 : 480 : return rc;
1022 : : }
1023 : :
1024 [ + + # # : 16134666 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1025 [ # # # # ]: 16134666 : guard = ctx->guard_seed;
1026 : 61 : }
1027 : :
1028 [ # # ]: 16134666 : *_guard = guard;
1029 : 16134666 : 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 : 327410 : 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 : 117815 : struct _dif_sgl sgl;
1060 : :
1061 : 327410 : _dif_sgl_init(&sgl, iovs, iovcnt);
1062 : :
1063 [ - + # # : 327410 : 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 [ + + # # : 327410 : if (_dif_is_disabled(ctx->dif_type)) {
# # ]
1069 : 4 : return 0;
1070 : : }
1071 : :
1072 [ + + # # : 327406 : if (_dif_sgl_is_bytes_multiple(&sgl, ctx->block_size)) {
# # ]
1073 : 326806 : 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 : 6250151 : _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 [ # # # # : 6250151 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1105 : :
1106 [ + + # # : 6250151 : assert(offset_in_block + data_len <= ctx->block_size);
# # # # ]
1107 : :
1108 [ + + ]: 18774950 : while (data_len != 0) {
1109 : 12524799 : _dif_sgl_get_buf(sgl, &buf, &buf_len);
1110 [ + + ]: 12524799 : buf_len = spdk_min(buf_len, data_len);
1111 : :
1112 [ + + ]: 12524799 : if (offset_in_block < data_block_size) {
1113 [ + + ]: 6274636 : buf_len = spdk_min(buf_len, data_block_size - offset_in_block);
1114 : 6274636 : crc32c = spdk_crc32c_update(buf, buf_len, crc32c);
1115 : 69 : }
1116 : :
1117 : 12524799 : _dif_sgl_advance(sgl, buf_len);
1118 : 12524799 : offset_in_block += buf_len;
1119 : 12524799 : data_len -= buf_len;
1120 : : }
1121 : :
1122 : 6250151 : 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 : 2216352 : _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 : 676116 : uint8_t *src, *dst;
1170 : 2216352 : uint64_t guard = 0;
1171 : :
1172 [ # # # # : 2216352 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1173 : :
1174 : 2216352 : _dif_sgl_get_buf(src_sgl, &src, NULL);
1175 : 2216352 : _dif_sgl_get_buf(dst_sgl, &dst, NULL);
1176 : :
1177 [ + + # # : 2216352 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1178 [ # # # # ]: 2216070 : guard = _dif_generate_guard_copy(ctx->guard_seed, dst, src, data_block_size,
1179 [ # # # # ]: 2215728 : ctx->dif_pi_format);
1180 [ # # ]: 2216070 : guard = _dif_generate_guard(guard, dst + data_block_size,
1181 [ # # # # : 2215728 : ctx->guard_interval - data_block_size, ctx->dif_pi_format);
# # # # ]
1182 : 342 : } else {
1183 [ - + - + ]: 624 : memcpy(dst, src, data_block_size);
1184 : : }
1185 : :
1186 [ # # # # : 2216352 : _dif_generate(dst + ctx->guard_interval, guard, offset_blocks, ctx);
# # ]
1187 : :
1188 : 2216352 : _dif_sgl_advance(src_sgl, data_block_size);
1189 [ # # # # ]: 2216352 : _dif_sgl_advance(dst_sgl, ctx->block_size);
1190 : 2216352 : }
1191 : :
1192 : : static void
1193 : 277022 : 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 [ + + ]: 2493374 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1199 : 2216352 : _dif_insert_copy(src_sgl, dst_sgl, offset_blocks, ctx);
1200 : 492 : }
1201 : 277022 : }
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 : 277161 : _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 [ # # # # : 277161 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1282 : :
1283 [ + - - + ]: 277161 : if (!_dif_sgl_is_valid(src_sgl, data_block_size * num_blocks) ||
1284 [ + + # # ]: 277161 : !_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 [ + + # # : 277158 : 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 [ + + + - ]: 554112 : if (_dif_sgl_is_bytes_multiple(src_sgl, data_block_size) &&
1295 [ # # # # ]: 277022 : _dif_sgl_is_bytes_multiple(dst_sgl, ctx->block_size)) {
1296 : 277022 : 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 : 277146 : 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 : 277233 : 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 : 84655 : struct _dif_sgl src_sgl, dst_sgl;
1407 : :
1408 : 277233 : _dif_sgl_init(&src_sgl, iovs, iovcnt);
1409 : 277233 : _dif_sgl_init(&dst_sgl, bounce_iovs, bounce_iovcnt);
1410 : :
1411 [ + + - + : 277287 : if (!(ctx->dif_flags & SPDK_DIF_FLAGS_NVME_PRACT) ||
# # # # #
# ]
1412 [ # # # # : 72 : ctx->md_size == _dif_size(ctx->dif_pi_format)) {
# # # # ]
1413 : 277161 : 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 : 1639 : _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 : 1237 : uint8_t *src, *dst;
1426 : : int rc;
1427 : 1639 : uint64_t guard = 0;
1428 : :
1429 [ # # # # : 1639 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1430 : :
1431 : 1639 : _dif_sgl_get_buf(src_sgl, &src, NULL);
1432 : 1639 : _dif_sgl_get_buf(dst_sgl, &dst, NULL);
1433 : :
1434 [ + + # # : 1639 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1435 [ # # # # ]: 1087 : guard = _dif_generate_guard_copy(ctx->guard_seed, dst, src, data_block_size,
1436 [ # # # # ]: 937 : ctx->dif_pi_format);
1437 [ # # ]: 1087 : guard = _dif_generate_guard(guard, src + data_block_size,
1438 [ # # # # : 937 : ctx->guard_interval - data_block_size, ctx->dif_pi_format);
# # # # ]
1439 : 150 : } else {
1440 [ - + - + ]: 702 : memcpy(dst, src, data_block_size);
1441 : : }
1442 : :
1443 [ # # # # : 1639 : rc = _dif_verify(src + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
# # ]
1444 [ + + ]: 1639 : if (rc != 0) {
1445 : 105 : return rc;
1446 : : }
1447 : :
1448 [ # # # # ]: 1534 : _dif_sgl_advance(src_sgl, ctx->block_size);
1449 : 1534 : _dif_sgl_advance(dst_sgl, data_block_size);
1450 : :
1451 : 1534 : return 0;
1452 : 300 : }
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 [ + + ]: 1785 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1463 : 1639 : rc = _dif_strip_copy(src_sgl, dst_sgl, offset_blocks, ctx, err_blk);
1464 [ + + ]: 1639 : if (rc != 0) {
1465 : 105 : return rc;
1466 : : }
1467 : 276 : }
1468 : :
1469 : 146 : return 0;
1470 : 56 : }
1471 : :
1472 : : static int
1473 : 220 : _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 : 220 : uint64_t guard = 0;
1479 : 220 : struct spdk_dif dif = {};
1480 : :
1481 [ # # # # : 220 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1482 : :
1483 [ + - # # : 220 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1484 [ # # # # ]: 287 : guard = _dif_generate_guard_copy_split(ctx->guard_seed, dst_sgl, src_sgl,
1485 [ # # # # ]: 220 : data_block_size, ctx->dif_pi_format);
1486 : 287 : guard = dif_generate_guard_split(guard, src_sgl, data_block_size,
1487 [ # # # # ]: 220 : ctx->guard_interval - data_block_size, ctx);
1488 : 67 : } 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 : 220 : dif_load_split(src_sgl, &dif, ctx);
1494 : :
1495 : 220 : 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 [ + + ]: 248 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1507 : 220 : rc = _dif_strip_copy_split(src_sgl, dst_sgl, offset_blocks, ctx, err_blk);
1508 [ + + ]: 220 : if (rc != 0) {
1509 : 96 : return rc;
1510 : : }
1511 : 43 : }
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 [ + - ]: 1306 : while (offset_in_block < block_size) {
1735 : 1306 : _dif_sgl_get_buf(sgl, &buf, &buf_len);
1736 [ + + ]: 1306 : buf_len = spdk_min(buf_len, block_size - offset_in_block);
1737 : :
1738 [ + - + + ]: 1306 : if (inject_offset_bytes >= offset_in_block &&
1739 [ + + ]: 1306 : 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 : 346 : _dif_sgl_advance(sgl, buf_len);
1746 : 346 : 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 [ + - ]: 2326 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1768 [ + + ]: 2326 : 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 : 120 : }
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 : 10904 : 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 : 10705 : struct _dif_sgl data_sgl, md_sgl;
1934 : :
1935 : 10904 : _dif_sgl_init(&data_sgl, iovs, iovcnt);
1936 : 10904 : _dif_sgl_init(&md_sgl, md_iov, 1);
1937 : :
1938 [ + - - + : 10904 : if (!_dif_sgl_is_valid(&data_sgl, ctx->block_size * num_blocks) ||
# # # # ]
1939 [ - + # # ]: 10904 : !_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 [ + + # # : 10904 : if (_dif_is_disabled(ctx->dif_type)) {
# # ]
1945 : 10368 : 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 : 245416 : 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 : 245416 : uint32_t offset_blocks = 0;
1963 : 76829 : uint8_t *data_buf, *md_buf;
1964 : : uint64_t guard;
1965 : : int rc;
1966 : :
1967 [ + + ]: 2209928 : while (offset_blocks < num_blocks) {
1968 : 1964635 : _dif_sgl_get_buf(data_sgl, &data_buf, NULL);
1969 : 1964635 : _dif_sgl_get_buf(md_sgl, &md_buf, NULL);
1970 : :
1971 : 1964635 : guard = 0;
1972 [ + + # # : 1964635 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1973 [ # # # # : 1964525 : guard = _dif_generate_guard(ctx->guard_seed, data_buf, ctx->block_size,
# # # # ]
1974 [ # # # # ]: 1963855 : ctx->dif_pi_format);
1975 [ # # # # ]: 1964525 : guard = _dif_generate_guard(guard, md_buf, ctx->guard_interval,
1976 [ # # # # ]: 1963855 : ctx->dif_pi_format);
1977 : 670 : }
1978 : :
1979 [ # # # # : 1964635 : rc = _dif_verify(md_buf + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
# # ]
1980 [ + + ]: 1964635 : if (rc != 0) {
1981 : 123 : return rc;
1982 : : }
1983 : :
1984 [ # # # # ]: 1964512 : _dif_sgl_advance(data_sgl, ctx->block_size);
1985 [ # # # # ]: 1964512 : _dif_sgl_advance(md_sgl, ctx->md_size);
1986 : 1964512 : offset_blocks++;
1987 : : }
1988 : :
1989 : 245293 : return 0;
1990 : 67 : }
1991 : :
1992 : : static int
1993 : 252 : _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 : 252 : uint64_t guard = 0;
2000 : :
2001 : 252 : _dif_sgl_get_buf(md_sgl, &md_buf, NULL);
2002 : :
2003 [ + + # # : 252 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
2004 [ # # # # ]: 252 : guard = ctx->guard_seed;
2005 : 75 : }
2006 : 252 : offset_in_block = 0;
2007 : :
2008 [ + + # # : 780 : while (offset_in_block < ctx->block_size) {
# # ]
2009 : 528 : _dif_sgl_get_buf(data_sgl, &data_buf, &data_buf_len);
2010 [ + + # # : 528 : data_buf_len = spdk_min(data_buf_len, ctx->block_size - offset_in_block);
# # # # #
# ]
2011 : :
2012 [ + + # # : 528 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
2013 : 684 : guard = _dif_generate_guard(guard, data_buf, data_buf_len,
2014 [ # # # # ]: 528 : ctx->dif_pi_format);
2015 : 156 : }
2016 : :
2017 : 528 : _dif_sgl_advance(data_sgl, data_buf_len);
2018 : 528 : offset_in_block += data_buf_len;
2019 : : }
2020 : :
2021 [ + + # # : 252 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
2022 [ # # # # ]: 327 : guard = _dif_generate_guard(guard, md_buf, ctx->guard_interval,
2023 [ # # # # ]: 252 : ctx->dif_pi_format);
2024 : 75 : }
2025 : :
2026 [ # # # # ]: 252 : _dif_sgl_advance(md_sgl, ctx->md_size);
2027 : :
2028 [ # # # # : 252 : 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 [ + + ]: 288 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
2040 : 252 : rc = _dix_verify_split(data_sgl, md_sgl, offset_blocks, ctx, err_blk);
2041 [ + + ]: 252 : if (rc != 0) {
2042 : 96 : return rc;
2043 : : }
2044 : 51 : }
2045 : :
2046 : 36 : return 0;
2047 : 33 : }
2048 : :
2049 : : int
2050 : 357180 : 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 : 188560 : struct _dif_sgl data_sgl, md_sgl;
2055 : :
2056 [ + + # # : 357180 : if (md_iov->iov_base == NULL) {
# # ]
2057 : 0 : SPDK_ERRLOG("Metadata buffer is NULL.\n");
2058 : 0 : return -EINVAL;
2059 : : }
2060 : :
2061 : 357180 : _dif_sgl_init(&data_sgl, iovs, iovcnt);
2062 : 357180 : _dif_sgl_init(&md_sgl, md_iov, 1);
2063 : :
2064 [ + - - + : 357180 : if (!_dif_sgl_is_valid(&data_sgl, ctx->block_size * num_blocks) ||
# # # # ]
2065 [ - + # # ]: 357180 : !_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 [ + + # # : 357180 : if (_dif_is_disabled(ctx->dif_type)) {
# # ]
2071 : 111632 : return 0;
2072 : : }
2073 : :
2074 [ + + # # : 245548 : if (_dif_sgl_is_bytes_multiple(&data_sgl, ctx->block_size)) {
# # ]
2075 : 245416 : 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 : 109307902 : _to_next_boundary(uint32_t offset, uint32_t boundary)
2150 : : {
2151 [ - + ]: 109307902 : return boundary - (offset % boundary);
2152 : : }
2153 : :
2154 : : static uint32_t
2155 : 9585046 : _to_size_with_md(uint32_t size, uint32_t data_block_size, uint32_t block_size)
2156 : : {
2157 [ - + - + ]: 9585046 : return (size / data_block_size) * block_size + (size % data_block_size);
2158 : : }
2159 : :
2160 : : int
2161 : 1964276 : 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 [ + - + - : 1964276 : if (iovs == NULL || iovcnt == 0 || buf_iovs == NULL || buf_iovcnt == 0) {
+ - - + ]
2172 : 0 : return -EINVAL;
2173 : : }
2174 : :
2175 [ # # # # : 1964276 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
2176 : :
2177 [ - + # # : 1964276 : data_unalign = ctx->data_offset % data_block_size;
# # ]
2178 : :
2179 : 1964314 : buf_len = _to_size_with_md(data_unalign + data_offset + data_len, data_block_size,
2180 [ # # # # ]: 1964276 : ctx->block_size);
2181 : 1964276 : buf_len -= data_unalign;
2182 : :
2183 : 1964276 : _dif_sgl_init(&dif_sgl, iovs, iovcnt);
2184 : 1964276 : _dif_sgl_init(&buf_sgl, buf_iovs, buf_iovcnt);
2185 : :
2186 [ + + ]: 1964276 : 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 [ # # # # ]: 1964272 : buf_offset = _to_size_with_md(data_unalign + data_offset, data_block_size, ctx->block_size);
2192 : 1964272 : buf_offset -= data_unalign;
2193 : :
2194 : 1964272 : _dif_sgl_advance(&buf_sgl, buf_offset);
2195 : :
2196 [ + + ]: 33816610 : while (data_len != 0) {
2197 [ + + # # : 32269260 : len = spdk_min(data_len, _to_next_boundary(ctx->data_offset + data_offset, data_block_size));
# # # # #
# ]
2198 [ + + ]: 32269260 : if (!_dif_sgl_append_split(&dif_sgl, &buf_sgl, len)) {
2199 : 416922 : break;
2200 : : }
2201 [ # # # # ]: 31852338 : _dif_sgl_advance(&buf_sgl, ctx->md_size);
2202 : 31852338 : data_offset += len;
2203 : 31852338 : data_len -= len;
2204 : : }
2205 : :
2206 [ + + ]: 1964272 : if (_mapped_len != NULL) {
2207 [ # # # # ]: 1964272 : *_mapped_len = dif_sgl.total_size;
2208 : 37 : }
2209 : :
2210 [ # # # # ]: 1964272 : return iovcnt - dif_sgl.iovcnt;
2211 : 38 : }
2212 : :
2213 : : static int
2214 : 1202062 : _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 [ # # # # : 1202062 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
2221 : :
2222 [ - + # # : 1202062 : 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 : 1202129 : buf_len = _to_size_with_md(data_unalign + data_offset + data_len, data_block_size,
2228 [ # # # # ]: 1202062 : ctx->block_size);
2229 : 1202062 : buf_len -= data_unalign;
2230 : :
2231 [ + + ]: 1202062 : if (!_dif_sgl_is_valid(sgl, buf_len)) {
2232 : 12 : return -ERANGE;
2233 : : }
2234 : :
2235 [ # # # # ]: 1202050 : buf_offset = _to_size_with_md(data_unalign + data_offset, data_block_size, ctx->block_size);
2236 : 1202050 : buf_offset -= data_unalign;
2237 : :
2238 : 1202050 : _dif_sgl_advance(sgl, buf_offset);
2239 : 1202050 : buf_len -= buf_offset;
2240 : :
2241 : 1202050 : buf_offset += data_unalign;
2242 : :
2243 [ # # ]: 1202050 : *_buf_offset = buf_offset;
2244 [ # # ]: 1202050 : *_buf_len = buf_len;
2245 : :
2246 : 1202050 : 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 : 982098 : 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 : 982098 : uint32_t buf_len = 0, buf_offset = 0;
2300 : : uint32_t len, offset_in_block, offset_blocks;
2301 : 982098 : uint64_t guard = 0;
2302 : 27 : struct _dif_sgl sgl;
2303 : 982098 : int rc = 0;
2304 : :
2305 [ + - - + ]: 982098 : if (iovs == NULL || iovcnt == 0) {
2306 : 0 : return -EINVAL;
2307 : : }
2308 : :
2309 [ + + # # : 982098 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
2310 [ # # # # ]: 982098 : guard = ctx->last_guard;
2311 : 9 : }
2312 : :
2313 : 982098 : _dif_sgl_init(&sgl, iovs, iovcnt);
2314 : :
2315 : 982098 : rc = _dif_sgl_setup_stream(&sgl, &buf_offset, &buf_len, data_offset, data_len, ctx);
2316 [ - + ]: 982098 : if (rc != 0) {
2317 : 0 : return rc;
2318 : : }
2319 : :
2320 [ + + ]: 17116592 : while (buf_len != 0) {
2321 [ + + # # : 16134494 : len = spdk_min(buf_len, _to_next_boundary(buf_offset, ctx->block_size));
# # # # #
# ]
2322 [ - + # # : 16134494 : offset_in_block = buf_offset % ctx->block_size;
# # ]
2323 [ - + # # : 16134494 : offset_blocks = buf_offset / ctx->block_size;
# # ]
2324 : :
2325 : 16134512 : rc = _dif_verify_split(&sgl, offset_in_block, len, &guard, offset_blocks,
2326 : 18 : ctx, err_blk);
2327 [ + + ]: 16134494 : if (rc != 0) {
2328 : 0 : goto error;
2329 : : }
2330 : :
2331 : 16134494 : buf_len -= len;
2332 : 16134494 : buf_offset += len;
2333 : : }
2334 : :
2335 [ - + # # : 982107 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
2336 [ # # # # ]: 982098 : ctx->last_guard = guard;
2337 : 9 : }
2338 : 27 : error:
2339 : 982098 : return rc;
2340 : 9 : }
2341 : :
2342 : : int
2343 : 219768 : 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 : 219768 : 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 [ + - - + ]: 219768 : if (iovs == NULL || iovcnt == 0) {
2353 : 0 : return -EINVAL;
2354 : : }
2355 : :
2356 [ # # ]: 219768 : crc32c = *_crc32c;
2357 : 219768 : _dif_sgl_init(&sgl, iovs, iovcnt);
2358 : :
2359 : 219768 : rc = _dif_sgl_setup_stream(&sgl, &buf_offset, &buf_len, data_offset, data_len, ctx);
2360 [ + + ]: 219768 : if (rc != 0) {
2361 : 0 : return rc;
2362 : : }
2363 : :
2364 [ + + ]: 6469787 : while (buf_len != 0) {
2365 [ + + # # : 6250019 : len = spdk_min(buf_len, _to_next_boundary(buf_offset, ctx->block_size));
# # # # #
# ]
2366 [ - + # # : 6250019 : offset_in_block = buf_offset % ctx->block_size;
# # ]
2367 : :
2368 : 6250019 : crc32c = _dif_update_crc32c_split(&sgl, offset_in_block, len, crc32c, ctx);
2369 : :
2370 : 6250019 : buf_len -= len;
2371 : 6250019 : buf_offset += len;
2372 : : }
2373 : :
2374 [ # # ]: 219768 : *_crc32c = crc32c;
2375 : :
2376 : 219768 : return 0;
2377 : 9 : }
2378 : :
2379 : : void
2380 : 1288178 : 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 [ + + - + : 1288178 : if (!ctx->md_interleave) {
# # # # ]
2387 : 0 : buf_offset = data_offset;
2388 : 0 : buf_len = data_len;
2389 : 0 : } else {
2390 [ # # # # : 1288178 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
2391 : :
2392 [ - + ]: 1288178 : data_unalign = data_offset % data_block_size;
2393 : :
2394 [ # # # # ]: 1288178 : buf_offset = _to_size_with_md(data_offset, data_block_size, ctx->block_size);
2395 [ # # # # ]: 1288188 : buf_len = _to_size_with_md(data_unalign + data_len, data_block_size, ctx->block_size) -
2396 : 10 : data_unalign;
2397 : : }
2398 : :
2399 [ + + ]: 1288178 : if (_buf_offset != NULL) {
2400 [ # # ]: 1288178 : *_buf_offset = buf_offset;
2401 : 10 : }
2402 : :
2403 [ + + ]: 1288178 : if (_buf_len != NULL) {
2404 [ # # ]: 1288178 : *_buf_len = buf_len;
2405 : 10 : }
2406 : 1288178 : }
2407 : :
2408 : : uint32_t
2409 : 676030 : 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 [ - + - + : 676030 : if (!ctx->md_interleave) {
# # # # ]
2414 : 0 : return data_len;
2415 : : } else {
2416 [ # # # # : 676030 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
2417 : :
2418 [ # # # # ]: 676030 : 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 : }
|