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