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 : 7525615 : _dif_sgl_init(struct _dif_sgl *s, struct iovec *iovs, int iovcnt)
65 : : {
66 [ # # # # ]: 7525615 : s->iov = iovs;
67 [ # # # # ]: 7525615 : s->iovcnt = iovcnt;
68 [ # # # # ]: 7525615 : s->iov_offset = 0;
69 [ # # # # ]: 7525615 : s->total_size = 0;
70 : 7525615 : }
71 : :
72 : : static void
73 : 141041119 : _dif_sgl_advance(struct _dif_sgl *s, uint32_t step)
74 : : {
75 [ # # # # ]: 141041119 : s->iov_offset += step;
76 [ + + # # : 146323441 : while (s->iovcnt != 0) {
# # ]
77 [ + + # # : 141221563 : if (s->iov_offset < s->iov->iov_len) {
# # # # #
# # # #
# ]
78 : 135939241 : break;
79 : : }
80 : :
81 [ # # # # : 5282322 : s->iov_offset -= s->iov->iov_len;
# # # # #
# # # ]
82 [ # # # # ]: 5282322 : s->iov++;
83 [ # # # # ]: 5282322 : s->iovcnt--;
84 : : }
85 : 141041119 : }
86 : :
87 : : static inline void
88 : 110549439 : _dif_sgl_get_buf(struct _dif_sgl *s, uint8_t **_buf, uint32_t *_buf_len)
89 : : {
90 [ + - ]: 110549439 : if (_buf != NULL) {
91 [ # # # # : 110549439 : *_buf = (uint8_t *)s->iov->iov_base + s->iov_offset;
# # # # #
# # # # #
# # ]
92 : 15233 : }
93 [ + + ]: 110549439 : if (_buf_len != NULL) {
94 [ # # # # : 87417214 : *_buf_len = s->iov->iov_len - s->iov_offset;
# # # # #
# # # #
# ]
95 : 8636 : }
96 : 110549439 : }
97 : :
98 : : static inline bool
99 : 28641272 : _dif_sgl_append(struct _dif_sgl *s, uint8_t *data, uint32_t data_len)
100 : : {
101 [ + + # # : 28641272 : assert(s->iovcnt > 0);
# # # # ]
102 [ # # # # : 28641272 : s->iov->iov_base = data;
# # # # ]
103 [ # # # # : 28641272 : s->iov->iov_len = data_len;
# # # # ]
104 [ # # # # ]: 28641272 : s->total_size += data_len;
105 [ # # # # ]: 28641272 : s->iov++;
106 [ # # # # ]: 28641272 : s->iovcnt--;
107 : :
108 [ + + # # : 28641272 : if (s->iovcnt > 0) {
# # ]
109 : 28255150 : return true;
110 : : } else {
111 : 386122 : return false;
112 : : }
113 : 120 : }
114 : :
115 : : static inline bool
116 : 28569884 : _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 [ + + ]: 56825034 : while (data_len != 0) {
122 : 28641272 : _dif_sgl_get_buf(src, &buf, &buf_len);
123 [ + + ]: 28641272 : buf_len = spdk_min(buf_len, data_len);
124 : :
125 [ + + ]: 28641272 : if (!_dif_sgl_append(dst, buf, buf_len)) {
126 : 386122 : return false;
127 : : }
128 : :
129 : 28255150 : _dif_sgl_advance(src, buf_len);
130 : 28255150 : data_len -= buf_len;
131 : : }
132 : :
133 : 28183762 : return true;
134 : 104 : }
135 : :
136 : : /* This function must be used before starting iteration. */
137 : : static bool
138 : 2519348 : _dif_sgl_is_bytes_multiple(struct _dif_sgl *s, uint32_t bytes)
139 : : {
140 : : int i;
141 : :
142 [ + + # # : 5005026 : for (i = 0; i < s->iovcnt; i++) {
# # # # ]
143 [ + + + + : 2523084 : if (s->iov[i].iov_len % bytes) {
# # # # #
# # # #
# ]
144 : 37406 : return false;
145 : : }
146 : 1468 : }
147 : :
148 : 2481942 : return true;
149 : 985 : }
150 : :
151 : : /* This function must be used before starting iteration. */
152 : : static bool
153 : 5868045 : _dif_sgl_is_valid(struct _dif_sgl *s, uint32_t bytes)
154 : : {
155 : 5868045 : uint64_t total = 0;
156 : : int i;
157 : :
158 [ + + # # : 12203105 : for (i = 0; i < s->iovcnt; i++) {
# # # # ]
159 [ # # # # : 6335060 : total += s->iov[i].iov_len;
# # # # #
# ]
160 : 6430 : }
161 : :
162 : 5868045 : 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 : 2414495 : _dif_is_disabled(enum spdk_dif_type dif_type)
173 : : {
174 [ + + ]: 2414495 : if (dif_type == SPDK_DIF_DISABLE) {
175 : 188704 : return true;
176 : : } else {
177 : 2225791 : return false;
178 : : }
179 : 890 : }
180 : :
181 : : static inline size_t
182 : 72810264 : _dif_size(enum spdk_dif_pi_format dif_pi_format)
183 : : {
184 : : uint8_t size;
185 : :
186 [ + + ]: 72810264 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
187 : 72800464 : size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g16);
188 [ + + ]: 11231 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
189 : 4984 : size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g32);
190 : 1239 : } else {
191 : 4816 : size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g64);
192 : : }
193 : :
194 : 72810264 : return size;
195 : : }
196 : :
197 : : static uint32_t
198 : 1278131 : _get_guard_interval(uint32_t block_size, uint32_t md_size, bool dif_loc, bool md_interleave,
199 : : size_t dif_size)
200 : : {
201 [ + + # # ]: 1278131 : 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 [ + + # # ]: 1276556 : if (md_interleave) {
208 : 1087572 : return block_size - dif_size;
209 : : } else {
210 : 188984 : 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 [ + + # # ]: 1575 : if (md_interleave) {
218 : 1213 : return block_size - md_size;
219 : : } else {
220 : 362 : return 0;
221 : : }
222 : : }
223 : 521 : }
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 : 22955002 : _dif_set_guard(struct spdk_dif *dif, uint64_t guard, enum spdk_dif_pi_format dif_pi_format)
243 : : {
244 [ + + ]: 22955002 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
245 [ # # # # : 22950778 : to_be16(&(dif->g16.guard), (uint16_t)guard);
# # ]
246 [ + + ]: 5009 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
247 [ # # # # : 2148 : to_be32(&(dif->g32.guard), (uint32_t)guard);
# # ]
248 : 537 : } else {
249 [ # # # # : 2076 : to_be64(&(dif->g64.guard), guard);
# # ]
250 : : }
251 : 22955002 : }
252 : :
253 : : static inline uint64_t
254 : 19059219 : _dif_get_guard(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
255 : : {
256 : : uint64_t guard;
257 : :
258 [ + + ]: 19059219 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
259 [ # # # # : 19055999 : guard = (uint64_t)from_be16(&(dif->g16.guard));
# # ]
260 [ + + ]: 4129 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
261 [ # # # # : 1608 : guard = (uint64_t)from_be32(&(dif->g32.guard));
# # ]
262 : 432 : } else {
263 [ # # # # : 1612 : guard = from_be64(&(dif->g64.guard));
# # ]
264 : : }
265 : :
266 : 19059219 : return guard;
267 : : }
268 : :
269 : : static inline uint64_t
270 : 44267104 : _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 [ + + ]: 44267104 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
276 : 44256372 : guard = (uint64_t)spdk_crc16_t10dif((uint16_t)guard_seed, buf, buf_len);
277 [ + + ]: 13569 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
278 : 5424 : guard = (uint64_t)spdk_crc32c_nvme(buf, buf_len, guard_seed);
279 : 1393 : } else {
280 : 5308 : guard = spdk_crc64_nvme(buf, buf_len, guard_seed);
281 : : }
282 : :
283 : 44267104 : return guard;
284 : : }
285 : :
286 : : static uint64_t
287 : 23416502 : 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 : 23416502 : uint64_t guard = guard_seed;
291 : 1728 : uint32_t offset, end, buf_len;
292 : 1728 : uint8_t *buf;
293 : :
294 : 23416502 : offset = start;
295 [ + + # # : 23416502 : end = start + spdk_min(len, ctx->guard_interval - start);
# # # # #
# ]
296 : :
297 [ + + ]: 46905416 : while (offset < end) {
298 : 23488914 : _dif_sgl_get_buf(sgl, &buf, &buf_len);
299 [ + + ]: 23488914 : buf_len = spdk_min(buf_len, end - offset);
300 : :
301 [ + + # # : 23488914 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
302 [ # # # # ]: 23488914 : guard = _dif_generate_guard(guard, buf, buf_len, ctx->dif_pi_format);
303 : 843 : }
304 : :
305 : 23488914 : _dif_sgl_advance(sgl, buf_len);
306 : 23488914 : offset += buf_len;
307 : : }
308 : :
309 : 23416502 : return guard;
310 : : }
311 : :
312 : : static inline uint64_t
313 : 2348283 : _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 [ + + ]: 2348283 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
319 : 2346107 : guard = (uint64_t)spdk_crc16_t10dif_copy((uint16_t)guard_seed, dst, src, buf_len);
320 [ + + ]: 2466 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
321 [ - + - + ]: 1088 : memcpy(dst, src, buf_len);
322 : 1088 : guard = (uint64_t)spdk_crc32c_nvme(src, buf_len, guard_seed);
323 : 278 : } else {
324 [ - + - + ]: 1088 : memcpy(dst, src, buf_len);
325 : 1088 : guard = spdk_crc64_nvme(src, buf_len, guard_seed);
326 : : }
327 : :
328 : 2348283 : return guard;
329 : : }
330 : :
331 : : static uint64_t
332 : 464 : _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 : 464 : uint32_t offset = 0, src_len, dst_len, buf_len;
337 : 354 : uint8_t *src, *dst;
338 : :
339 [ + + ]: 1448 : while (offset < data_len) {
340 : 984 : _dif_sgl_get_buf(src_sgl, &src, &src_len);
341 : 984 : _dif_sgl_get_buf(dst_sgl, &dst, &dst_len);
342 [ + + ]: 984 : buf_len = spdk_min(src_len, dst_len);
343 [ + + ]: 984 : buf_len = spdk_min(buf_len, data_len - offset);
344 : :
345 : 984 : guard = _dif_generate_guard_copy(guard, dst, src, buf_len, dif_pi_format);
346 : :
347 : 984 : _dif_sgl_advance(src_sgl, buf_len);
348 : 984 : _dif_sgl_advance(dst_sgl, buf_len);
349 : 984 : offset += buf_len;
350 : : }
351 : :
352 : 464 : 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 : 8670332 : _dif_set_apptag(struct spdk_dif *dif, uint16_t app_tag, enum spdk_dif_pi_format dif_pi_format)
389 : : {
390 [ + + ]: 8670332 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
391 [ # # # # : 8666136 : to_be16(&(dif->g16.app_tag), app_tag);
# # ]
392 [ + + ]: 4980 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
393 [ # # # # : 2136 : to_be16(&(dif->g32.app_tag), app_tag);
# # ]
394 : 534 : } else {
395 [ # # # # : 2060 : to_be16(&(dif->g64.app_tag), app_tag);
# # ]
396 : : }
397 : 8670332 : }
398 : :
399 : : static inline uint16_t
400 : 23836486 : _dif_get_apptag(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
401 : : {
402 : : uint16_t app_tag;
403 : :
404 [ + + ]: 23836486 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
405 [ # # # # : 23829010 : app_tag = from_be16(&(dif->g16.app_tag));
# # ]
406 [ + + ]: 9543 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
407 [ # # # # : 3740 : app_tag = from_be16(&(dif->g32.app_tag));
# # ]
408 : 995 : } else {
409 [ # # # # : 3736 : app_tag = from_be16(&(dif->g64.app_tag));
# # ]
410 : : }
411 : :
412 : 23836486 : return app_tag;
413 : : }
414 : :
415 : : static inline bool
416 : 19062481 : _dif_apptag_ignore(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
417 : : {
418 : 19062481 : 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 : 13826328 : _dif_set_reftag(struct spdk_dif *dif, uint64_t ref_tag, enum spdk_dif_pi_format dif_pi_format)
457 : : {
458 [ + + ]: 13826328 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
459 [ # # # # : 13821756 : to_be32(&(dif->g16.stor_ref_space), (uint32_t)ref_tag);
# # ]
460 [ + + ]: 5531 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
461 [ # # # # : 2320 : to_be64(&(dif->g32.stor_ref_space_p2), ref_tag);
# # ]
462 : 580 : } else {
463 [ # # # # : 2252 : to_be16(&(dif->g64.stor_ref_space_p1), (uint16_t)(ref_tag >> 32));
# # # # ]
464 [ # # # # : 2252 : to_be32(&(dif->g64.stor_ref_space_p2), (uint32_t)ref_tag);
# # ]
465 : : }
466 : 13826328 : }
467 : :
468 : : static inline uint64_t
469 : 9929476 : _dif_get_reftag(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
470 : : {
471 : : uint64_t ref_tag;
472 : :
473 [ + + ]: 9929476 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
474 [ # # # # : 9926236 : ref_tag = (uint64_t)from_be32(&(dif->g16.stor_ref_space));
# # ]
475 [ + + ]: 4158 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
476 [ # # # # : 1620 : ref_tag = from_be64(&(dif->g32.stor_ref_space_p2));
# # ]
477 : 435 : } else {
478 [ # # # # : 1620 : ref_tag = (uint64_t)from_be16(&(dif->g64.stor_ref_space_p1));
# # ]
479 [ # # ]: 1620 : ref_tag <<= 32;
480 [ # # # # : 1620 : ref_tag |= (uint64_t)from_be32(&(dif->g64.stor_ref_space_p2));
# # ]
481 : : }
482 : :
483 : 9929476 : return ref_tag;
484 : : }
485 : :
486 : : static inline bool
487 : 9929186 : _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 : 9929186 : _ref_tag = _dif_get_reftag(dif, dif_pi_format);
494 : :
495 [ + + ]: 9929186 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
496 : 9926122 : match = (_ref_tag == (ref_tag & REFTAG_MASK_16));
497 [ + + ]: 3958 : } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
498 : 1532 : match = (_ref_tag == ref_tag);
499 : 413 : } else {
500 : 1532 : match = (_ref_tag == (ref_tag & REFTAG_MASK_64));
501 : : }
502 : :
503 [ # # ]: 9929186 : 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 : 19062481 : _dif_ignore(struct spdk_dif *dif, const struct spdk_dif_ctx *ctx)
514 : : {
515 [ + + + # : 19062481 : switch (ctx->dif_type) {
# # # ]
516 : 9930136 : 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 [ + + # # : 9932543 : if (_dif_apptag_ignore(dif, ctx->dif_pi_format)) {
# # ]
522 : 120 : return true;
523 : : }
524 : 9932423 : break;
525 : 9129931 : 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 [ + + + + : 9129959 : 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 : 9129922 : break;
536 : 0 : default:
537 : 0 : break;
538 : : }
539 : :
540 : 19062345 : return false;
541 : 2414 : }
542 : :
543 : : static bool
544 : 1278135 : _dif_pi_format_is_valid(enum spdk_dif_pi_format dif_pi_format)
545 : : {
546 [ + + ]: 1278135 : switch (dif_pi_format) {
547 : 1277610 : case SPDK_DIF_PI_FORMAT_16:
548 : : case SPDK_DIF_PI_FORMAT_32:
549 : : case SPDK_DIF_PI_FORMAT_64:
550 : 1278131 : return true;
551 : 3 : default:
552 : 4 : return false;
553 : : }
554 : 522 : }
555 : :
556 : : static bool
557 : 1278139 : _dif_type_is_valid(enum spdk_dif_type dif_type)
558 : : {
559 [ + + ]: 1278139 : switch (dif_type) {
560 : 1277613 : case SPDK_DIF_DISABLE:
561 : : case SPDK_DIF_TYPE1:
562 : : case SPDK_DIF_TYPE2:
563 : : case SPDK_DIF_TYPE3:
564 : 1278135 : return true;
565 : 3 : default:
566 : 4 : return false;
567 : : }
568 : 523 : }
569 : :
570 : : int
571 : 1278119 : 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 : 1278119 : enum spdk_dif_pi_format dif_pi_format = SPDK_DIF_PI_FORMAT_16;
578 : :
579 [ + + ]: 1278119 : if (opts != NULL) {
580 [ + + # # : 1278119 : 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 [ # # # # ]: 1278119 : dif_pi_format = opts->dif_pi_format;
586 : 518 : }
587 : :
588 [ + + ]: 1278119 : 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 [ + + ]: 1278119 : 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 [ + + # # ]: 1278079 : if (md_interleave) {
599 [ - + ]: 1088713 : if (block_size < md_size) {
600 : 0 : SPDK_ERRLOG("Block size is smaller than DIF size.\n");
601 : 0 : return -EINVAL;
602 : : }
603 : 1088713 : data_block_size = block_size - md_size;
604 : 387 : } else {
605 : 189366 : data_block_size = block_size;
606 : : }
607 : :
608 [ + + ]: 1278079 : if (data_block_size == 0) {
609 : 8 : SPDK_ERRLOG("Zero data block size is not allowed\n");
610 : 8 : return -EINVAL;
611 : : }
612 : :
613 [ + + ]: 1278071 : if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
614 [ + + # # ]: 1276823 : 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 [ + + # # ]: 1248 : 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 [ # # # # ]: 1278047 : ctx->block_size = block_size;
626 [ # # # # ]: 1278047 : ctx->md_size = md_size;
627 [ # # # # : 1278047 : ctx->md_interleave = md_interleave;
# # ]
628 [ # # # # ]: 1278047 : ctx->dif_pi_format = dif_pi_format;
629 [ # # # # : 1278547 : ctx->guard_interval = _get_guard_interval(block_size, md_size, dif_loc, md_interleave,
# # # # ]
630 [ # # # # ]: 1278047 : _dif_size(ctx->dif_pi_format));
631 [ # # # # ]: 1278047 : ctx->dif_type = dif_type;
632 [ # # # # ]: 1278047 : ctx->dif_flags = dif_flags;
633 [ # # # # ]: 1278047 : ctx->init_ref_tag = init_ref_tag;
634 [ # # # # ]: 1278047 : ctx->apptag_mask = apptag_mask;
635 [ # # # # ]: 1278047 : ctx->app_tag = app_tag;
636 [ # # # # ]: 1278047 : ctx->data_offset = data_offset;
637 [ - + # # : 1278047 : ctx->ref_tag_offset = data_offset / data_block_size;
# # ]
638 [ # # # # ]: 1278047 : ctx->last_guard = guard_seed;
639 [ # # # # ]: 1278047 : ctx->guard_seed = guard_seed;
640 [ # # # # ]: 1278047 : ctx->remapped_init_ref_tag = 0;
641 : :
642 : 1278047 : return 0;
643 : 518 : }
644 : :
645 : : void
646 : 1114179 : spdk_dif_ctx_set_data_offset(struct spdk_dif_ctx *ctx, uint32_t data_offset)
647 : : {
648 : : uint32_t data_block_size;
649 : :
650 [ + + + - : 1114179 : if (ctx->md_interleave) {
# # # # ]
651 [ # # # # : 1114179 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
652 : 42 : } else {
653 [ # # # # ]: 0 : data_block_size = ctx->block_size;
654 : : }
655 : :
656 [ # # # # ]: 1114179 : ctx->data_offset = data_offset;
657 [ - + # # : 1114179 : ctx->ref_tag_offset = data_offset / data_block_size;
# # ]
658 : 1114179 : }
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 : 22956898 : _dif_generate(void *_dif, uint64_t guard, uint32_t offset_blocks,
669 : : const struct spdk_dif_ctx *ctx)
670 : : {
671 : 22956898 : struct spdk_dif *dif = _dif;
672 : : uint64_t ref_tag;
673 : :
674 [ + + # # : 22956898 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
675 [ # # # # ]: 22955002 : _dif_set_guard(dif, guard, ctx->dif_pi_format);
676 : 1841 : }
677 : :
678 [ + + # # : 22956898 : if (ctx->dif_flags & SPDK_DIF_FLAGS_APPTAG_CHECK) {
# # # # ]
679 [ # # # # : 8670332 : _dif_set_apptag(dif, ctx->app_tag, ctx->dif_pi_format);
# # # # ]
680 : 1833 : }
681 : :
682 [ + + # # : 22956898 : 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 [ + + # # : 13825240 : if (ctx->dif_type != SPDK_DIF_TYPE3) {
# # ]
688 [ # # # # : 13825212 : ref_tag = ctx->init_ref_tag + ctx->ref_tag_offset + offset_blocks;
# # # # ]
689 : 1823 : } 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 [ + + # # : 13825240 : 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 [ # # # # ]: 13825240 : _dif_set_reftag(dif, ref_tag, ctx->dif_pi_format);
705 : 1830 : }
706 : 22956898 : }
707 : :
708 : : static void
709 : 1297650 : dif_generate(struct _dif_sgl *sgl, uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
710 : : {
711 : : uint32_t offset_blocks;
712 : 232635 : uint8_t *buf;
713 : 1297650 : uint64_t guard = 0;
714 : :
715 [ + + ]: 12772172 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
716 : 11474522 : _dif_sgl_get_buf(sgl, &buf, NULL);
717 : :
718 [ + + # # : 11474522 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
719 [ # # # # : 11473970 : guard = _dif_generate_guard(ctx->guard_seed, buf, ctx->guard_interval, ctx->dif_pi_format);
# # # # #
# # # ]
720 : 475 : }
721 : :
722 [ # # # # : 11474522 : _dif_generate(buf + ctx->guard_interval, guard, offset_blocks, ctx);
# # ]
723 : :
724 [ # # # # ]: 11474522 : _dif_sgl_advance(sgl, ctx->block_size);
725 : 577 : }
726 : 1297650 : }
727 : :
728 : : static void
729 : 9130632 : dif_store_split(struct _dif_sgl *sgl, struct spdk_dif *dif,
730 : : const struct spdk_dif_ctx *ctx)
731 : : {
732 : 9130632 : uint32_t offset = 0, rest_md_len, buf_len;
733 : 870 : uint8_t *buf;
734 : :
735 [ # # # # : 9130632 : rest_md_len = ctx->block_size - ctx->guard_interval;
# # # # ]
736 : :
737 [ + + ]: 18262080 : while (offset < rest_md_len) {
738 : 9131448 : _dif_sgl_get_buf(sgl, &buf, &buf_len);
739 : :
740 [ + + # # : 9131448 : if (offset < _dif_size(ctx->dif_pi_format)) {
# # ]
741 [ + + # # : 9130984 : buf_len = spdk_min(buf_len, _dif_size(ctx->dif_pi_format) - offset);
# # # # #
# ]
742 [ - + - + : 9130984 : memcpy(buf, (uint8_t *)dif + offset, buf_len);
# # ]
743 : 378 : } else {
744 [ + + ]: 464 : buf_len = spdk_min(buf_len, rest_md_len - offset);
745 : : }
746 : :
747 : 9131448 : _dif_sgl_advance(sgl, buf_len);
748 : 9131448 : offset += buf_len;
749 : : }
750 : 9130632 : }
751 : :
752 : : static uint64_t
753 : 9130520 : _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 : 9130520 : struct spdk_dif dif = {};
757 : :
758 [ + + # # : 9130520 : assert(offset_in_block < ctx->guard_interval);
# # # # ]
759 [ + + - + : 9130520 : 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 : 9130520 : guard = dif_generate_guard_split(guard, sgl, offset_in_block, data_len, ctx);
764 : :
765 [ + + # # : 9130520 : 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 : 9130364 : _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 : 9130364 : dif_store_split(sgl, &dif, ctx);
778 : :
779 [ + + # # : 9130364 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
780 [ # # # # ]: 9130364 : guard = ctx->guard_seed;
781 : 223 : }
782 : :
783 : 9130364 : return guard;
784 : 262 : }
785 : :
786 : : static void
787 : 36270 : 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 : 36270 : uint64_t guard = 0;
792 : :
793 [ + + # # : 36270 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
794 [ # # # # ]: 36270 : guard = ctx->guard_seed;
795 : 152 : }
796 : :
797 [ + + ]: 9166450 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
798 [ # # # # ]: 9130180 : _dif_generate_split(sgl, 0, ctx->block_size, guard, offset_blocks, ctx);
799 : 177 : }
800 : 36270 : }
801 : :
802 : : int
803 : 1333888 : spdk_dif_generate(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
804 : : const struct spdk_dif_ctx *ctx)
805 : : {
806 : 233067 : struct _dif_sgl sgl;
807 : :
808 : 1333888 : _dif_sgl_init(&sgl, iovs, iovcnt);
809 : :
810 [ - + # # : 1333888 : 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 [ + + # # : 1333888 : if (_dif_is_disabled(ctx->dif_type)) {
# # ]
816 : 4 : return 0;
817 : : }
818 : :
819 [ + + # # : 1333884 : if (_dif_sgl_is_bytes_multiple(&sgl, ctx->block_size)) {
# # ]
820 : 1297614 : dif_generate(&sgl, num_blocks, ctx);
821 : 91 : } else {
822 : 36270 : dif_generate_split(&sgl, num_blocks, ctx);
823 : : }
824 : :
825 : 1333884 : 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 : 19061016 : _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 [ + + # # : 19061016 : if (ctx->dif_flags & SPDK_DIF_FLAGS_REFTAG_CHECK) {
# # # # ]
847 [ + - - # : 9929158 : switch (ctx->dif_type) {
# # # ]
848 : 9927445 : 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 [ + + # # : 9929158 : 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 : 9928876 : 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 : 1647 : }
875 : :
876 : 19060734 : return true;
877 : 2080 : }
878 : :
879 : : static int
880 : 19061393 : _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 : 19061393 : struct spdk_dif *dif = _dif;
884 : : uint64_t _guard;
885 : : uint16_t _app_tag;
886 : : uint64_t ref_tag;
887 : :
888 [ + + ]: 19061393 : 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 [ + + # # : 19061257 : if (ctx->dif_type != SPDK_DIF_TYPE3) {
# # ]
897 [ # # # # : 9931335 : ref_tag = ctx->init_ref_tag + ctx->ref_tag_offset + offset_blocks;
# # # # ]
898 : 2129 : } else {
899 [ # # # # : 9129922 : ref_tag = ctx->init_ref_tag + ctx->ref_tag_offset;
# # # # ]
900 : : }
901 : :
902 [ + + # # : 19061257 : 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 [ # # # # ]: 19059179 : _guard = _dif_get_guard(dif, ctx->dif_pi_format);
907 [ + + ]: 19059179 : 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 : 1640 : }
916 : :
917 [ + + # # : 19060746 : 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 [ # # # # ]: 4773997 : _app_tag = _dif_get_apptag(dif, ctx->dif_pi_format);
922 [ + + # # : 4773997 : 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 : 1568 : }
931 : :
932 [ + + ]: 19060440 : if (!_dif_reftag_check(dif, ctx, ref_tag, offset_blocks, err_blk)) {
933 : 282 : return -1;
934 : : }
935 : :
936 : 19060158 : return 0;
937 : 2142 : }
938 : :
939 : : static int
940 : 324858 : 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 : 101229 : uint8_t *buf;
946 : 324858 : uint64_t guard = 0;
947 : :
948 [ + + ]: 2922038 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
949 : 2597343 : _dif_sgl_get_buf(sgl, &buf, NULL);
950 : :
951 [ + + # # : 2597343 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
952 [ # # # # : 2596651 : guard = _dif_generate_guard(ctx->guard_seed, buf, ctx->guard_interval, ctx->dif_pi_format);
# # # # #
# # # ]
953 : 418 : }
954 : :
955 [ # # # # : 2597343 : rc = _dif_verify(buf + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
# # ]
956 [ + + ]: 2597343 : if (rc != 0) {
957 : 163 : return rc;
958 : : }
959 : :
960 [ # # # # ]: 2597180 : _dif_sgl_advance(sgl, ctx->block_size);
961 : 485 : }
962 : :
963 : 324695 : return 0;
964 : 105 : }
965 : :
966 : : static void
967 : 14285654 : dif_load_split(struct _dif_sgl *sgl, struct spdk_dif *dif,
968 : : const struct spdk_dif_ctx *ctx)
969 : : {
970 : 14285654 : uint32_t offset = 0, rest_md_len, buf_len;
971 : 696 : uint8_t *buf;
972 : :
973 [ # # # # : 14285654 : rest_md_len = ctx->block_size - ctx->guard_interval;
# # # # ]
974 : :
975 [ + + ]: 28572076 : while (offset < rest_md_len) {
976 : 14286422 : _dif_sgl_get_buf(sgl, &buf, &buf_len);
977 : :
978 [ + + # # : 14286422 : if (offset < _dif_size(ctx->dif_pi_format)) {
# # ]
979 [ + + # # : 14285994 : buf_len = spdk_min(buf_len, _dif_size(ctx->dif_pi_format) - offset);
# # # # #
# ]
980 [ - + - + : 14285994 : memcpy((uint8_t *)dif + offset, buf, buf_len);
# # ]
981 : 309 : } else {
982 [ + + ]: 428 : buf_len = spdk_min(buf_len, rest_md_len - offset);
983 : : }
984 : :
985 : 14286422 : _dif_sgl_advance(sgl, buf_len);
986 : 14286422 : offset += buf_len;
987 : : }
988 : 14285654 : }
989 : :
990 : : static int
991 : 14285518 : _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 [ # # ]: 14285518 : uint64_t guard = *_guard;
996 : 14285518 : struct spdk_dif dif = {};
997 : : int rc;
998 : :
999 [ + + # # ]: 14285518 : assert(_guard != NULL);
1000 [ + + # # : 14285518 : assert(offset_in_block < ctx->guard_interval);
# # # # ]
1001 [ + + - + : 14285518 : assert(offset_in_block + data_len < ctx->guard_interval ||
# # # # #
# # # #
# ]
1002 : : offset_in_block + data_len == ctx->block_size);
1003 : :
1004 : 14285518 : guard = dif_generate_guard_split(guard, sgl, offset_in_block, data_len, ctx);
1005 : :
1006 [ + + # # : 14285518 : if (offset_in_block + data_len < ctx->guard_interval) {
# # ]
1007 [ # # ]: 60 : *_guard = guard;
1008 : 60 : return 0;
1009 : : }
1010 : :
1011 : 14285458 : dif_load_split(sgl, &dif, ctx);
1012 : :
1013 : 14285458 : rc = _dif_verify(&dif, guard, offset_blocks, ctx, err_blk);
1014 [ + + ]: 14285458 : if (rc != 0) {
1015 : 480 : return rc;
1016 : : }
1017 : :
1018 [ + + # # : 14284978 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1019 [ # # # # ]: 14284978 : guard = ctx->guard_seed;
1020 : 61 : }
1021 : :
1022 [ # # ]: 14284978 : *_guard = guard;
1023 : 14284978 : 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 : 325426 : 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 : 101655 : struct _dif_sgl sgl;
1054 : :
1055 : 325426 : _dif_sgl_init(&sgl, iovs, iovcnt);
1056 : :
1057 [ - + # # : 325426 : 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 [ + + # # : 325426 : if (_dif_is_disabled(ctx->dif_type)) {
# # ]
1063 : 4 : return 0;
1064 : : }
1065 : :
1066 [ + + # # : 325422 : if (_dif_sgl_is_bytes_multiple(&sgl, ctx->block_size)) {
# # ]
1067 : 324822 : 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 : 5909671 : _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 [ # # # # : 5909671 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1099 : :
1100 [ + + # # : 5909671 : assert(offset_in_block + data_len <= ctx->block_size);
# # # # ]
1101 : :
1102 [ + + ]: 17752180 : while (data_len != 0) {
1103 : 11842509 : _dif_sgl_get_buf(sgl, &buf, &buf_len);
1104 [ + + ]: 11842509 : buf_len = spdk_min(buf_len, data_len);
1105 : :
1106 [ + + ]: 11842509 : if (offset_in_block < data_block_size) {
1107 [ + + ]: 5932826 : buf_len = spdk_min(buf_len, data_block_size - offset_in_block);
1108 : 5932826 : crc32c = spdk_crc32c_update(buf, buf_len, crc32c);
1109 : 69 : }
1110 : :
1111 : 11842509 : _dif_sgl_advance(sgl, buf_len);
1112 : 11842509 : offset_in_block += buf_len;
1113 : 11842509 : data_len -= buf_len;
1114 : : }
1115 : :
1116 : 5909671 : 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 : 2346912 : _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 : 698132 : uint8_t *src, *dst;
1164 : 2346912 : uint64_t guard = 0;
1165 : :
1166 [ # # # # : 2346912 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1167 : :
1168 : 2346912 : _dif_sgl_get_buf(src_sgl, &src, NULL);
1169 : 2346912 : _dif_sgl_get_buf(dst_sgl, &dst, NULL);
1170 : :
1171 [ + + # # : 2346912 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1172 [ # # # # ]: 2346630 : guard = _dif_generate_guard_copy(ctx->guard_seed, dst, src, data_block_size,
1173 [ # # # # ]: 2346288 : ctx->dif_pi_format);
1174 [ # # ]: 2346630 : guard = _dif_generate_guard(guard, dst + data_block_size,
1175 [ # # # # : 2346288 : ctx->guard_interval - data_block_size, ctx->dif_pi_format);
# # # # ]
1176 : 342 : } else {
1177 [ - + - + ]: 624 : memcpy(dst, src, data_block_size);
1178 : : }
1179 : :
1180 [ # # # # : 2346912 : _dif_generate(dst + ctx->guard_interval, guard, offset_blocks, ctx);
# # ]
1181 : :
1182 : 2346912 : _dif_sgl_advance(src_sgl, data_block_size);
1183 [ # # # # ]: 2346912 : _dif_sgl_advance(dst_sgl, ctx->block_size);
1184 : 2346912 : }
1185 : :
1186 : : static void
1187 : 293342 : 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 [ + + ]: 2640254 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1193 : 2346912 : _dif_insert_copy(src_sgl, dst_sgl, offset_blocks, ctx);
1194 : 492 : }
1195 : 293342 : }
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 : 293481 : _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 [ # # # # : 293481 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1276 : :
1277 [ + - - + ]: 293481 : if (!_dif_sgl_is_valid(src_sgl, data_block_size * num_blocks) ||
1278 [ + + # # ]: 293481 : !_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 [ + + # # : 293478 : 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 [ + + + - ]: 586752 : if (_dif_sgl_is_bytes_multiple(src_sgl, data_block_size) &&
1289 [ # # # # ]: 293342 : _dif_sgl_is_bytes_multiple(dst_sgl, ctx->block_size)) {
1290 : 293342 : 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 : 293466 : return 0;
1296 : 90 : }
1297 : :
1298 : : int
1299 : 293481 : spdk_dif_generate_copy(struct iovec *iovs, int iovcnt, struct iovec *bounce_iovs,
1300 : : int bounce_iovcnt, uint32_t num_blocks,
1301 : : const struct spdk_dif_ctx *ctx)
1302 : : {
1303 : 87353 : struct _dif_sgl src_sgl, dst_sgl;
1304 : :
1305 : 293481 : _dif_sgl_init(&src_sgl, iovs, iovcnt);
1306 : 293481 : _dif_sgl_init(&dst_sgl, bounce_iovs, bounce_iovcnt);
1307 : :
1308 [ - + - - : 293481 : if (!(ctx->dif_flags & SPDK_DIF_FLAGS_NVME_PRACT) ||
# # # # #
# ]
1309 [ # # # # : 0 : ctx->md_size == _dif_size(ctx->dif_pi_format)) {
# # # # ]
1310 : 293481 : return _spdk_dif_insert_copy(&src_sgl, &dst_sgl, num_blocks, ctx);
1311 : : } else {
1312 : 0 : return -ENOTSUP;
1313 : : }
1314 : 90 : }
1315 : :
1316 : : static int
1317 : 1713 : _dif_strip_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1318 : : uint32_t offset_blocks, const struct spdk_dif_ctx *ctx,
1319 : : struct spdk_dif_error *err_blk)
1320 : : {
1321 : : uint32_t data_block_size;
1322 : 1191 : uint8_t *src, *dst;
1323 : : int rc;
1324 : 1713 : uint64_t guard = 0;
1325 : :
1326 [ # # # # : 1713 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1327 : :
1328 : 1713 : _dif_sgl_get_buf(src_sgl, &src, NULL);
1329 : 1713 : _dif_sgl_get_buf(dst_sgl, &dst, NULL);
1330 : :
1331 [ + + # # : 1713 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1332 [ # # # # ]: 1281 : guard = _dif_generate_guard_copy(ctx->guard_seed, dst, src, data_block_size,
1333 [ # # # # ]: 1011 : ctx->dif_pi_format);
1334 [ # # ]: 1281 : guard = _dif_generate_guard(guard, src + data_block_size,
1335 [ # # # # : 1011 : ctx->guard_interval - data_block_size, ctx->dif_pi_format);
# # # # ]
1336 : 270 : } else {
1337 [ - + - + ]: 702 : memcpy(dst, src, data_block_size);
1338 : : }
1339 : :
1340 [ # # # # : 1713 : rc = _dif_verify(src + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
# # ]
1341 [ + + ]: 1713 : if (rc != 0) {
1342 : 105 : return rc;
1343 : : }
1344 : :
1345 [ # # # # ]: 1608 : _dif_sgl_advance(src_sgl, ctx->block_size);
1346 : 1608 : _dif_sgl_advance(dst_sgl, data_block_size);
1347 : :
1348 : 1608 : return 0;
1349 : 420 : }
1350 : :
1351 : : static int
1352 : 251 : dif_strip_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1353 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
1354 : : struct spdk_dif_error *err_blk)
1355 : : {
1356 : : uint32_t offset_blocks;
1357 : : int rc;
1358 : :
1359 [ + + ]: 1859 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1360 : 1713 : rc = _dif_strip_copy(src_sgl, dst_sgl, offset_blocks, ctx, err_blk);
1361 [ + + ]: 1713 : if (rc != 0) {
1362 : 105 : return rc;
1363 : : }
1364 : 396 : }
1365 : :
1366 : 146 : return 0;
1367 : 56 : }
1368 : :
1369 : : static int
1370 : 196 : _dif_strip_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1371 : : uint32_t offset_blocks, const struct spdk_dif_ctx *ctx,
1372 : : struct spdk_dif_error *err_blk)
1373 : : {
1374 : : uint32_t data_block_size;
1375 : 196 : uint64_t guard = 0;
1376 : 196 : struct spdk_dif dif = {};
1377 : :
1378 [ # # # # : 196 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1379 : :
1380 [ + - # # : 196 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1381 [ # # # # ]: 239 : guard = _dif_generate_guard_copy_split(ctx->guard_seed, dst_sgl, src_sgl,
1382 [ # # # # ]: 196 : data_block_size, ctx->dif_pi_format);
1383 : 239 : guard = dif_generate_guard_split(guard, src_sgl, data_block_size,
1384 [ # # # # ]: 196 : ctx->guard_interval - data_block_size, ctx);
1385 : 43 : } else {
1386 : 0 : _data_copy_split(dst_sgl, src_sgl, data_block_size);
1387 [ # # # # ]: 0 : _dif_sgl_advance(src_sgl, ctx->guard_interval - data_block_size);
1388 : : }
1389 : :
1390 : 196 : dif_load_split(src_sgl, &dif, ctx);
1391 : :
1392 : 196 : return _dif_verify(&dif, guard, offset_blocks, ctx, err_blk);
1393 : : }
1394 : :
1395 : : static int
1396 : 124 : dif_strip_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1397 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
1398 : : struct spdk_dif_error *err_blk)
1399 : : {
1400 : : uint32_t offset_blocks;
1401 : : int rc;
1402 : :
1403 [ + + ]: 224 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1404 : 196 : rc = _dif_strip_copy_split(src_sgl, dst_sgl, offset_blocks, ctx, err_blk);
1405 [ + + ]: 196 : if (rc != 0) {
1406 : 96 : return rc;
1407 : : }
1408 : 19 : }
1409 : :
1410 : 28 : return 0;
1411 : 31 : }
1412 : :
1413 : : static void
1414 : 48 : _dif_disable_strip_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1415 : : const struct spdk_dif_ctx *ctx)
1416 : : {
1417 : 48 : uint32_t offset = 0, src_len, dst_len, buf_len, data_block_size;
1418 : 36 : uint8_t *src, *dst;
1419 : :
1420 [ # # # # : 48 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1421 : :
1422 [ + + ]: 128 : while (offset < data_block_size) {
1423 : 80 : _dif_sgl_get_buf(src_sgl, &src, &src_len);
1424 : 80 : _dif_sgl_get_buf(dst_sgl, &dst, &dst_len);
1425 [ + + ]: 80 : buf_len = spdk_min(src_len, dst_len);
1426 [ + + ]: 80 : buf_len = spdk_min(buf_len, data_block_size - offset);
1427 : :
1428 [ - + - + ]: 80 : memcpy(dst, src, buf_len);
1429 : :
1430 : 80 : _dif_sgl_advance(src_sgl, buf_len);
1431 : 80 : _dif_sgl_advance(dst_sgl, buf_len);
1432 : 80 : offset += buf_len;
1433 : : }
1434 : :
1435 [ # # # # ]: 48 : _dif_sgl_advance(src_sgl, ctx->md_size);
1436 : 48 : }
1437 : :
1438 : : static void
1439 : 12 : dif_disable_strip_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1440 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
1441 : : {
1442 : : uint32_t offset_blocks;
1443 : :
1444 [ + + ]: 60 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1445 : 48 : _dif_disable_strip_copy(src_sgl, dst_sgl, ctx);
1446 : 12 : }
1447 : 12 : }
1448 : :
1449 : : static int
1450 : 387 : _spdk_dif_strip_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
1451 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
1452 : : struct spdk_dif_error *err_blk)
1453 : : {
1454 : : uint32_t data_block_size;
1455 : :
1456 [ # # # # : 387 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1457 : :
1458 [ + - - + ]: 387 : if (!_dif_sgl_is_valid(dst_sgl, data_block_size * num_blocks) ||
1459 [ - + # # ]: 387 : !_dif_sgl_is_valid(src_sgl, ctx->block_size * num_blocks)) {
1460 : 0 : SPDK_ERRLOG("Size of iovec arrays are not valid\n");
1461 : 0 : return -EINVAL;
1462 : : }
1463 : :
1464 [ + + # # : 387 : if (_dif_is_disabled(ctx->dif_type)) {
# # ]
1465 : 12 : dif_disable_strip_copy(src_sgl, dst_sgl, num_blocks, ctx);
1466 : 12 : return 0;
1467 : : }
1468 : :
1469 [ + + + - ]: 570 : if (_dif_sgl_is_bytes_multiple(dst_sgl, data_block_size) &&
1470 [ # # # # ]: 251 : _dif_sgl_is_bytes_multiple(src_sgl, ctx->block_size)) {
1471 : 251 : return dif_strip_copy(src_sgl, dst_sgl, num_blocks, ctx, err_blk);
1472 : : } else {
1473 : 124 : return dif_strip_copy_split(src_sgl, dst_sgl, num_blocks, ctx, err_blk);
1474 : : }
1475 : 90 : }
1476 : :
1477 : : int
1478 : 387 : spdk_dif_verify_copy(struct iovec *iovs, int iovcnt, struct iovec *bounce_iovs,
1479 : : int bounce_iovcnt, uint32_t num_blocks,
1480 : : const struct spdk_dif_ctx *ctx,
1481 : : struct spdk_dif_error *err_blk)
1482 : : {
1483 : 279 : struct _dif_sgl src_sgl, dst_sgl;
1484 : :
1485 : 387 : _dif_sgl_init(&src_sgl, bounce_iovs, bounce_iovcnt);
1486 : 387 : _dif_sgl_init(&dst_sgl, iovs, iovcnt);
1487 : :
1488 [ - + - - : 387 : if (!(ctx->dif_flags & SPDK_DIF_FLAGS_NVME_PRACT) ||
# # # # #
# ]
1489 [ # # # # : 0 : ctx->md_size == _dif_size(ctx->dif_pi_format)) {
# # # # ]
1490 : 387 : return _spdk_dif_strip_copy(&src_sgl, &dst_sgl, num_blocks, ctx, err_blk);
1491 : : } else {
1492 : 0 : return -ENOTSUP;
1493 : : }
1494 : 90 : }
1495 : :
1496 : : static void
1497 : 960 : _bit_flip(uint8_t *buf, uint32_t flip_bit)
1498 : : {
1499 : : uint8_t byte;
1500 : :
1501 [ # # ]: 960 : byte = *buf;
1502 [ - + # # ]: 960 : byte ^= 1 << flip_bit;
1503 [ # # ]: 960 : *buf = byte;
1504 : 960 : }
1505 : :
1506 : : static int
1507 : 960 : _dif_inject_error(struct _dif_sgl *sgl,
1508 : : uint32_t block_size, uint32_t num_blocks,
1509 : : uint32_t inject_offset_blocks,
1510 : : uint32_t inject_offset_bytes,
1511 : : uint32_t inject_offset_bits)
1512 : : {
1513 : 720 : uint32_t offset_in_block, buf_len;
1514 : 720 : uint8_t *buf;
1515 : :
1516 : 960 : _dif_sgl_advance(sgl, block_size * inject_offset_blocks);
1517 : :
1518 : 960 : offset_in_block = 0;
1519 : :
1520 [ + - ]: 1281 : while (offset_in_block < block_size) {
1521 : 1281 : _dif_sgl_get_buf(sgl, &buf, &buf_len);
1522 [ + + ]: 1281 : buf_len = spdk_min(buf_len, block_size - offset_in_block);
1523 : :
1524 [ + - + + ]: 1281 : if (inject_offset_bytes >= offset_in_block &&
1525 [ + + ]: 1281 : inject_offset_bytes < offset_in_block + buf_len) {
1526 [ # # ]: 960 : buf += inject_offset_bytes - offset_in_block;
1527 : 960 : _bit_flip(buf, inject_offset_bits);
1528 : 960 : return 0;
1529 : : }
1530 : :
1531 : 321 : _dif_sgl_advance(sgl, buf_len);
1532 : 321 : offset_in_block += buf_len;
1533 : : }
1534 : :
1535 : 0 : return -1;
1536 : 240 : }
1537 : :
1538 : : static int
1539 : 960 : dif_inject_error(struct _dif_sgl *sgl, uint32_t block_size, uint32_t num_blocks,
1540 : : uint32_t start_inject_bytes, uint32_t inject_range_bytes,
1541 : : uint32_t *inject_offset)
1542 : : {
1543 : : uint32_t inject_offset_blocks, inject_offset_bytes, inject_offset_bits;
1544 : : uint32_t offset_blocks;
1545 : : int rc;
1546 : :
1547 : 960 : srand(time(0));
1548 : :
1549 [ - + ]: 960 : inject_offset_blocks = rand() % num_blocks;
1550 [ - + ]: 960 : inject_offset_bytes = start_inject_bytes + (rand() % inject_range_bytes);
1551 [ # # ]: 960 : inject_offset_bits = rand() % 8;
1552 : :
1553 [ + - ]: 2328 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1554 [ + + ]: 2328 : if (offset_blocks == inject_offset_blocks) {
1555 : 1200 : rc = _dif_inject_error(sgl, block_size, num_blocks,
1556 : 240 : inject_offset_blocks,
1557 : 240 : inject_offset_bytes,
1558 : 240 : inject_offset_bits);
1559 [ + + ]: 960 : if (rc == 0) {
1560 [ # # ]: 960 : *inject_offset = inject_offset_blocks;
1561 : 240 : }
1562 : 960 : return rc;
1563 : : }
1564 : 432 : }
1565 : :
1566 : 0 : return -1;
1567 : 240 : }
1568 : :
1569 : : int
1570 : 768 : spdk_dif_inject_error(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
1571 : : const struct spdk_dif_ctx *ctx, uint32_t inject_flags,
1572 : : uint32_t *inject_offset)
1573 : : {
1574 : 576 : struct _dif_sgl sgl;
1575 : : int rc;
1576 : :
1577 : 768 : _dif_sgl_init(&sgl, iovs, iovcnt);
1578 : :
1579 [ - + # # : 768 : if (!_dif_sgl_is_valid(&sgl, ctx->block_size * num_blocks)) {
# # ]
1580 : 0 : SPDK_ERRLOG("Size of iovec array is not valid.\n");
1581 : 0 : return -EINVAL;
1582 : : }
1583 : :
1584 [ + + ]: 768 : if (inject_flags & SPDK_DIF_REFTAG_ERROR) {
1585 [ # # # # ]: 288 : rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
1586 [ # # # # : 192 : ctx->guard_interval + _dif_reftag_offset(ctx->dif_pi_format),
# # # # ]
1587 [ # # # # ]: 192 : _dif_reftag_size(ctx->dif_pi_format),
1588 : 48 : inject_offset);
1589 [ - + ]: 192 : if (rc != 0) {
1590 : 0 : SPDK_ERRLOG("Failed to inject error to Reference Tag.\n");
1591 : 0 : return rc;
1592 : : }
1593 : 48 : }
1594 : :
1595 [ + + ]: 768 : if (inject_flags & SPDK_DIF_APPTAG_ERROR) {
1596 [ # # # # ]: 288 : rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
1597 [ # # # # : 192 : ctx->guard_interval + _dif_apptag_offset(ctx->dif_pi_format),
# # # # ]
1598 : 192 : _dif_apptag_size(),
1599 : 48 : inject_offset);
1600 [ - + ]: 192 : if (rc != 0) {
1601 : 0 : SPDK_ERRLOG("Failed to inject error to Application Tag.\n");
1602 : 0 : return rc;
1603 : : }
1604 : 48 : }
1605 [ + + ]: 768 : if (inject_flags & SPDK_DIF_GUARD_ERROR) {
1606 [ # # # # ]: 240 : rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
1607 [ # # # # ]: 192 : ctx->guard_interval,
1608 [ # # # # ]: 192 : _dif_guard_size(ctx->dif_pi_format),
1609 : 48 : inject_offset);
1610 [ - + ]: 192 : if (rc != 0) {
1611 : 0 : SPDK_ERRLOG("Failed to inject error to Guard.\n");
1612 : 0 : return rc;
1613 : : }
1614 : 48 : }
1615 : :
1616 [ + + ]: 768 : if (inject_flags & SPDK_DIF_DATA_ERROR) {
1617 : : /* If the DIF information is contained within the last 8/16 bytes of
1618 : : * metadata (depending on the PI format), then the CRC covers all metadata
1619 : : * bytes up to but excluding the last 8/16 bytes. But error injection does not
1620 : : * cover these metadata because classification is not determined yet.
1621 : : *
1622 : : * Note: Error injection to data block is expected to be detected as
1623 : : * guard error.
1624 : : */
1625 [ # # # # ]: 240 : rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
1626 : : 0,
1627 [ # # # # : 192 : ctx->block_size - ctx->md_size,
# # # # ]
1628 : 48 : inject_offset);
1629 [ + + ]: 192 : if (rc != 0) {
1630 : 0 : SPDK_ERRLOG("Failed to inject error to data block.\n");
1631 : 0 : return rc;
1632 : : }
1633 : 48 : }
1634 : :
1635 : 768 : return 0;
1636 : 192 : }
1637 : :
1638 : : static void
1639 : 404 : dix_generate(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
1640 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
1641 : : {
1642 : 404 : uint32_t offset_blocks = 0;
1643 : 238 : uint8_t *data_buf, *md_buf;
1644 : : uint64_t guard;
1645 : :
1646 [ + + ]: 4852 : while (offset_blocks < num_blocks) {
1647 : 4448 : _dif_sgl_get_buf(data_sgl, &data_buf, NULL);
1648 : 4448 : _dif_sgl_get_buf(md_sgl, &md_buf, NULL);
1649 : :
1650 : 4448 : guard = 0;
1651 [ + + # # : 4448 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1652 [ # # # # : 4366 : guard = _dif_generate_guard(ctx->guard_seed, data_buf, ctx->block_size,
# # # # ]
1653 [ # # # # ]: 3728 : ctx->dif_pi_format);
1654 [ # # # # ]: 4366 : guard = _dif_generate_guard(guard, md_buf, ctx->guard_interval,
1655 [ # # # # ]: 3728 : ctx->dif_pi_format);
1656 : 638 : }
1657 : :
1658 [ # # # # : 4448 : _dif_generate(md_buf + ctx->guard_interval, guard, offset_blocks, ctx);
# # ]
1659 : :
1660 [ # # # # ]: 4448 : _dif_sgl_advance(data_sgl, ctx->block_size);
1661 [ # # # # ]: 4448 : _dif_sgl_advance(md_sgl, ctx->md_size);
1662 : 4448 : offset_blocks++;
1663 : : }
1664 : 404 : }
1665 : :
1666 : : static void
1667 : 300 : _dix_generate_split(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
1668 : : uint32_t offset_blocks, const struct spdk_dif_ctx *ctx)
1669 : : {
1670 : 225 : uint32_t offset_in_block, data_buf_len;
1671 : 225 : uint8_t *data_buf, *md_buf;
1672 : 300 : uint64_t guard = 0;
1673 : :
1674 : 300 : _dif_sgl_get_buf(md_sgl, &md_buf, NULL);
1675 : :
1676 [ + + # # : 300 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1677 [ # # # # ]: 300 : guard = ctx->guard_seed;
1678 : 75 : }
1679 : 300 : offset_in_block = 0;
1680 : :
1681 [ + + # # : 924 : while (offset_in_block < ctx->block_size) {
# # ]
1682 : 624 : _dif_sgl_get_buf(data_sgl, &data_buf, &data_buf_len);
1683 [ + + # # : 624 : data_buf_len = spdk_min(data_buf_len, ctx->block_size - offset_in_block);
# # # # #
# ]
1684 : :
1685 [ + + # # : 624 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1686 : 780 : guard = _dif_generate_guard(guard, data_buf, data_buf_len,
1687 [ # # # # ]: 624 : ctx->dif_pi_format);
1688 : 156 : }
1689 : :
1690 : 624 : _dif_sgl_advance(data_sgl, data_buf_len);
1691 : 624 : offset_in_block += data_buf_len;
1692 : : }
1693 : :
1694 [ + + # # : 300 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1695 [ # # # # ]: 375 : guard = _dif_generate_guard(guard, md_buf, ctx->guard_interval,
1696 [ # # # # ]: 300 : ctx->dif_pi_format);
1697 : 75 : }
1698 : :
1699 [ # # # # ]: 300 : _dif_sgl_advance(md_sgl, ctx->md_size);
1700 : :
1701 [ # # # # : 300 : _dif_generate(md_buf + ctx->guard_interval, guard, offset_blocks, ctx);
# # ]
1702 : 300 : }
1703 : :
1704 : : static void
1705 : 132 : dix_generate_split(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
1706 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
1707 : : {
1708 : : uint32_t offset_blocks;
1709 : :
1710 [ + + ]: 432 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1711 : 300 : _dix_generate_split(data_sgl, md_sgl, offset_blocks, ctx);
1712 : 75 : }
1713 : 132 : }
1714 : :
1715 : : int
1716 : 16088 : spdk_dix_generate(struct iovec *iovs, int iovcnt, struct iovec *md_iov,
1717 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
1718 : : {
1719 : 15889 : struct _dif_sgl data_sgl, md_sgl;
1720 : :
1721 : 16088 : _dif_sgl_init(&data_sgl, iovs, iovcnt);
1722 : 16088 : _dif_sgl_init(&md_sgl, md_iov, 1);
1723 : :
1724 [ + - - + : 16088 : if (!_dif_sgl_is_valid(&data_sgl, ctx->block_size * num_blocks) ||
# # # # ]
1725 [ - + # # ]: 16088 : !_dif_sgl_is_valid(&md_sgl, ctx->md_size * num_blocks)) {
1726 : 0 : SPDK_ERRLOG("Size of iovec array is not valid.\n");
1727 : 0 : return -EINVAL;
1728 : : }
1729 : :
1730 [ + + # # : 16088 : if (_dif_is_disabled(ctx->dif_type)) {
# # ]
1731 : 15552 : return 0;
1732 : : }
1733 : :
1734 [ + + # # : 536 : if (_dif_sgl_is_bytes_multiple(&data_sgl, ctx->block_size)) {
# # ]
1735 : 404 : dix_generate(&data_sgl, &md_sgl, num_blocks, ctx);
1736 : 62 : } else {
1737 : 132 : dix_generate_split(&data_sgl, &md_sgl, num_blocks, ctx);
1738 : : }
1739 : :
1740 : 536 : return 0;
1741 : 95 : }
1742 : :
1743 : : static int
1744 : 271880 : dix_verify(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
1745 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
1746 : : struct spdk_dif_error *err_blk)
1747 : : {
1748 : 271880 : uint32_t offset_blocks = 0;
1749 : 90301 : uint8_t *data_buf, *md_buf;
1750 : : uint64_t guard;
1751 : : int rc;
1752 : :
1753 [ + + ]: 2448128 : while (offset_blocks < num_blocks) {
1754 : 2176371 : _dif_sgl_get_buf(data_sgl, &data_buf, NULL);
1755 : 2176371 : _dif_sgl_get_buf(md_sgl, &md_buf, NULL);
1756 : :
1757 : 2176371 : guard = 0;
1758 [ + + # # : 2176371 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1759 [ # # # # : 2176381 : guard = _dif_generate_guard(ctx->guard_seed, data_buf, ctx->block_size,
# # # # ]
1760 [ # # # # ]: 2175591 : ctx->dif_pi_format);
1761 [ # # # # ]: 2176381 : guard = _dif_generate_guard(guard, md_buf, ctx->guard_interval,
1762 [ # # # # ]: 2175591 : ctx->dif_pi_format);
1763 : 790 : }
1764 : :
1765 [ # # # # : 2176371 : rc = _dif_verify(md_buf + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
# # ]
1766 [ + + ]: 2176371 : if (rc != 0) {
1767 : 123 : return rc;
1768 : : }
1769 : :
1770 [ # # # # ]: 2176248 : _dif_sgl_advance(data_sgl, ctx->block_size);
1771 [ # # # # ]: 2176248 : _dif_sgl_advance(md_sgl, ctx->md_size);
1772 : 2176248 : offset_blocks++;
1773 : : }
1774 : :
1775 : 271757 : return 0;
1776 : 67 : }
1777 : :
1778 : : static int
1779 : 228 : _dix_verify_split(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
1780 : : uint32_t offset_blocks, const struct spdk_dif_ctx *ctx,
1781 : : struct spdk_dif_error *err_blk)
1782 : : {
1783 : 177 : uint32_t offset_in_block, data_buf_len;
1784 : 177 : uint8_t *data_buf, *md_buf;
1785 : 228 : uint64_t guard = 0;
1786 : :
1787 : 228 : _dif_sgl_get_buf(md_sgl, &md_buf, NULL);
1788 : :
1789 [ + + # # : 228 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1790 [ # # # # ]: 228 : guard = ctx->guard_seed;
1791 : 51 : }
1792 : 228 : offset_in_block = 0;
1793 : :
1794 [ + + # # : 708 : while (offset_in_block < ctx->block_size) {
# # ]
1795 : 480 : _dif_sgl_get_buf(data_sgl, &data_buf, &data_buf_len);
1796 [ + + # # : 480 : data_buf_len = spdk_min(data_buf_len, ctx->block_size - offset_in_block);
# # # # #
# ]
1797 : :
1798 [ + + # # : 480 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1799 : 588 : guard = _dif_generate_guard(guard, data_buf, data_buf_len,
1800 [ # # # # ]: 480 : ctx->dif_pi_format);
1801 : 108 : }
1802 : :
1803 : 480 : _dif_sgl_advance(data_sgl, data_buf_len);
1804 : 480 : offset_in_block += data_buf_len;
1805 : : }
1806 : :
1807 [ + + # # : 228 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
1808 [ # # # # ]: 279 : guard = _dif_generate_guard(guard, md_buf, ctx->guard_interval,
1809 [ # # # # ]: 228 : ctx->dif_pi_format);
1810 : 51 : }
1811 : :
1812 [ # # # # ]: 228 : _dif_sgl_advance(md_sgl, ctx->md_size);
1813 : :
1814 [ # # # # : 228 : return _dif_verify(md_buf + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
# # ]
1815 : : }
1816 : :
1817 : : static int
1818 : 132 : dix_verify_split(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
1819 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
1820 : : struct spdk_dif_error *err_blk)
1821 : : {
1822 : : uint32_t offset_blocks;
1823 : : int rc;
1824 : :
1825 [ + + ]: 264 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
1826 : 228 : rc = _dix_verify_split(data_sgl, md_sgl, offset_blocks, ctx, err_blk);
1827 [ + + ]: 228 : if (rc != 0) {
1828 : 96 : return rc;
1829 : : }
1830 : 27 : }
1831 : :
1832 : 36 : return 0;
1833 : 33 : }
1834 : :
1835 : : int
1836 : 445132 : spdk_dix_verify(struct iovec *iovs, int iovcnt, struct iovec *md_iov,
1837 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
1838 : : struct spdk_dif_error *err_blk)
1839 : : {
1840 : 263520 : struct _dif_sgl data_sgl, md_sgl;
1841 : :
1842 [ + + # # : 445132 : if (md_iov->iov_base == NULL) {
# # ]
1843 : 0 : SPDK_ERRLOG("Metadata buffer is NULL.\n");
1844 : 0 : return -EINVAL;
1845 : : }
1846 : :
1847 : 445132 : _dif_sgl_init(&data_sgl, iovs, iovcnt);
1848 : 445132 : _dif_sgl_init(&md_sgl, md_iov, 1);
1849 : :
1850 [ + - - + : 445132 : if (!_dif_sgl_is_valid(&data_sgl, ctx->block_size * num_blocks) ||
# # # # ]
1851 [ - + # # ]: 445132 : !_dif_sgl_is_valid(&md_sgl, ctx->md_size * num_blocks)) {
1852 : 0 : SPDK_ERRLOG("Size of iovec array is not valid.\n");
1853 : 0 : return -EINVAL;
1854 : : }
1855 : :
1856 [ + + # # : 445132 : if (_dif_is_disabled(ctx->dif_type)) {
# # ]
1857 : 173120 : return 0;
1858 : : }
1859 : :
1860 [ + + # # : 272012 : if (_dif_sgl_is_bytes_multiple(&data_sgl, ctx->block_size)) {
# # ]
1861 : 271880 : return dix_verify(&data_sgl, &md_sgl, num_blocks, ctx, err_blk);
1862 : : } else {
1863 : 132 : return dix_verify_split(&data_sgl, &md_sgl, num_blocks, ctx, err_blk);
1864 : : }
1865 : 100 : }
1866 : :
1867 : : int
1868 : 192 : spdk_dix_inject_error(struct iovec *iovs, int iovcnt, struct iovec *md_iov,
1869 : : uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
1870 : : uint32_t inject_flags, uint32_t *inject_offset)
1871 : : {
1872 : 144 : struct _dif_sgl data_sgl, md_sgl;
1873 : : int rc;
1874 : :
1875 : 192 : _dif_sgl_init(&data_sgl, iovs, iovcnt);
1876 : 192 : _dif_sgl_init(&md_sgl, md_iov, 1);
1877 : :
1878 [ + - - + : 192 : if (!_dif_sgl_is_valid(&data_sgl, ctx->block_size * num_blocks) ||
# # # # ]
1879 [ - + # # ]: 192 : !_dif_sgl_is_valid(&md_sgl, ctx->md_size * num_blocks)) {
1880 : 0 : SPDK_ERRLOG("Size of iovec array is not valid.\n");
1881 : 0 : return -EINVAL;
1882 : : }
1883 : :
1884 [ + + ]: 192 : if (inject_flags & SPDK_DIF_REFTAG_ERROR) {
1885 [ # # # # ]: 72 : rc = dif_inject_error(&md_sgl, ctx->md_size, num_blocks,
1886 [ # # # # : 48 : ctx->guard_interval + _dif_reftag_offset(ctx->dif_pi_format),
# # # # ]
1887 [ # # # # ]: 48 : _dif_reftag_size(ctx->dif_pi_format),
1888 : 12 : inject_offset);
1889 [ - + ]: 48 : if (rc != 0) {
1890 : 0 : SPDK_ERRLOG("Failed to inject error to Reference Tag.\n");
1891 : 0 : return rc;
1892 : : }
1893 : 12 : }
1894 : :
1895 [ + + ]: 192 : if (inject_flags & SPDK_DIF_APPTAG_ERROR) {
1896 [ # # # # ]: 72 : rc = dif_inject_error(&md_sgl, ctx->md_size, num_blocks,
1897 [ # # # # : 48 : ctx->guard_interval + _dif_apptag_offset(ctx->dif_pi_format),
# # # # ]
1898 : 48 : _dif_apptag_size(),
1899 : 12 : inject_offset);
1900 [ - + ]: 48 : if (rc != 0) {
1901 : 0 : SPDK_ERRLOG("Failed to inject error to Application Tag.\n");
1902 : 0 : return rc;
1903 : : }
1904 : 12 : }
1905 : :
1906 [ + + ]: 192 : if (inject_flags & SPDK_DIF_GUARD_ERROR) {
1907 [ # # # # ]: 60 : rc = dif_inject_error(&md_sgl, ctx->md_size, num_blocks,
1908 [ # # # # ]: 48 : ctx->guard_interval,
1909 [ # # # # ]: 48 : _dif_guard_size(ctx->dif_pi_format),
1910 : 12 : inject_offset);
1911 [ - + ]: 48 : if (rc != 0) {
1912 : 0 : SPDK_ERRLOG("Failed to inject error to Guard.\n");
1913 : 0 : return rc;
1914 : : }
1915 : 12 : }
1916 : :
1917 [ + + ]: 192 : if (inject_flags & SPDK_DIF_DATA_ERROR) {
1918 : : /* Note: Error injection to data block is expected to be detected
1919 : : * as guard error.
1920 : : */
1921 [ # # # # ]: 60 : rc = dif_inject_error(&data_sgl, ctx->block_size, num_blocks,
1922 : : 0,
1923 [ # # # # ]: 48 : ctx->block_size,
1924 : 12 : inject_offset);
1925 [ + + ]: 48 : if (rc != 0) {
1926 : 0 : SPDK_ERRLOG("Failed to inject error to Guard.\n");
1927 : 0 : return rc;
1928 : : }
1929 : 12 : }
1930 : :
1931 : 192 : return 0;
1932 : 48 : }
1933 : :
1934 : : static uint32_t
1935 : 97528814 : _to_next_boundary(uint32_t offset, uint32_t boundary)
1936 : : {
1937 [ - + ]: 97528814 : return boundary - (offset % boundary);
1938 : : }
1939 : :
1940 : : static uint32_t
1941 : 8159723 : _to_size_with_md(uint32_t size, uint32_t data_block_size, uint32_t block_size)
1942 : : {
1943 [ - + - + ]: 8159723 : return (size / data_block_size) * block_size + (size % data_block_size);
1944 : : }
1945 : :
1946 : : int
1947 : 1657462 : spdk_dif_set_md_interleave_iovs(struct iovec *iovs, int iovcnt,
1948 : : struct iovec *buf_iovs, int buf_iovcnt,
1949 : : uint32_t data_offset, uint32_t data_len,
1950 : : uint32_t *_mapped_len,
1951 : : const struct spdk_dif_ctx *ctx)
1952 : : {
1953 : : uint32_t data_block_size, data_unalign, buf_len, buf_offset, len;
1954 : 114 : struct _dif_sgl dif_sgl;
1955 : 114 : struct _dif_sgl buf_sgl;
1956 : :
1957 [ + - + - : 1657462 : if (iovs == NULL || iovcnt == 0 || buf_iovs == NULL || buf_iovcnt == 0) {
+ - - + ]
1958 : 0 : return -EINVAL;
1959 : : }
1960 : :
1961 [ # # # # : 1657462 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
1962 : :
1963 [ - + # # : 1657462 : data_unalign = ctx->data_offset % data_block_size;
# # ]
1964 : :
1965 : 1657500 : buf_len = _to_size_with_md(data_unalign + data_offset + data_len, data_block_size,
1966 [ # # # # ]: 1657462 : ctx->block_size);
1967 : 1657462 : buf_len -= data_unalign;
1968 : :
1969 : 1657462 : _dif_sgl_init(&dif_sgl, iovs, iovcnt);
1970 : 1657462 : _dif_sgl_init(&buf_sgl, buf_iovs, buf_iovcnt);
1971 : :
1972 [ + + ]: 1657462 : if (!_dif_sgl_is_valid(&buf_sgl, buf_len)) {
1973 : 4 : SPDK_ERRLOG("Buffer overflow will occur.\n");
1974 : 4 : return -ERANGE;
1975 : : }
1976 : :
1977 [ # # # # ]: 1657458 : buf_offset = _to_size_with_md(data_unalign + data_offset, data_block_size, ctx->block_size);
1978 : 1657458 : buf_offset -= data_unalign;
1979 : :
1980 : 1657458 : _dif_sgl_advance(&buf_sgl, buf_offset);
1981 : :
1982 [ + + ]: 29841220 : while (data_len != 0) {
1983 [ + + # # : 28569884 : len = spdk_min(data_len, _to_next_boundary(ctx->data_offset + data_offset, data_block_size));
# # # # #
# ]
1984 [ + + ]: 28569884 : if (!_dif_sgl_append_split(&dif_sgl, &buf_sgl, len)) {
1985 : 386122 : break;
1986 : : }
1987 [ # # # # ]: 28183762 : _dif_sgl_advance(&buf_sgl, ctx->md_size);
1988 : 28183762 : data_offset += len;
1989 : 28183762 : data_len -= len;
1990 : : }
1991 : :
1992 [ + + ]: 1657458 : if (_mapped_len != NULL) {
1993 [ # # # # ]: 1657458 : *_mapped_len = dif_sgl.total_size;
1994 : 37 : }
1995 : :
1996 [ # # # # ]: 1657458 : return iovcnt - dif_sgl.iovcnt;
1997 : 38 : }
1998 : :
1999 : : static int
2000 : 1036685 : _dif_sgl_setup_stream(struct _dif_sgl *sgl, uint32_t *_buf_offset, uint32_t *_buf_len,
2001 : : uint32_t data_offset, uint32_t data_len,
2002 : : const struct spdk_dif_ctx *ctx)
2003 : : {
2004 : : uint32_t data_block_size, data_unalign, buf_len, buf_offset;
2005 : :
2006 [ # # # # : 1036685 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
2007 : :
2008 [ - + # # : 1036685 : data_unalign = ctx->data_offset % data_block_size;
# # ]
2009 : :
2010 : : /* If the last data block is complete, DIF of the data block is
2011 : : * inserted or verified in this turn.
2012 : : */
2013 : 1036752 : buf_len = _to_size_with_md(data_unalign + data_offset + data_len, data_block_size,
2014 [ # # # # ]: 1036685 : ctx->block_size);
2015 : 1036685 : buf_len -= data_unalign;
2016 : :
2017 [ + + ]: 1036685 : if (!_dif_sgl_is_valid(sgl, buf_len)) {
2018 : 12 : return -ERANGE;
2019 : : }
2020 : :
2021 [ # # # # ]: 1036673 : buf_offset = _to_size_with_md(data_unalign + data_offset, data_block_size, ctx->block_size);
2022 : 1036673 : buf_offset -= data_unalign;
2023 : :
2024 : 1036673 : _dif_sgl_advance(sgl, buf_offset);
2025 : 1036673 : buf_len -= buf_offset;
2026 : :
2027 : 1036673 : buf_offset += data_unalign;
2028 : :
2029 [ # # ]: 1036673 : *_buf_offset = buf_offset;
2030 [ # # ]: 1036673 : *_buf_len = buf_len;
2031 : :
2032 : 1036673 : return 0;
2033 : 67 : }
2034 : :
2035 : : int
2036 : 196 : spdk_dif_generate_stream(struct iovec *iovs, int iovcnt,
2037 : : uint32_t data_offset, uint32_t data_len,
2038 : : struct spdk_dif_ctx *ctx)
2039 : : {
2040 : 196 : uint32_t buf_len = 0, buf_offset = 0;
2041 : : uint32_t len, offset_in_block, offset_blocks;
2042 : 196 : uint64_t guard = 0;
2043 : 147 : struct _dif_sgl sgl;
2044 : : int rc;
2045 : :
2046 [ + - - + ]: 196 : if (iovs == NULL || iovcnt == 0) {
2047 : 0 : return -EINVAL;
2048 : : }
2049 : :
2050 [ + + # # : 196 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
2051 [ # # # # ]: 196 : guard = ctx->last_guard;
2052 : 49 : }
2053 : :
2054 : 196 : _dif_sgl_init(&sgl, iovs, iovcnt);
2055 : :
2056 : 196 : rc = _dif_sgl_setup_stream(&sgl, &buf_offset, &buf_len, data_offset, data_len, ctx);
2057 [ + + ]: 196 : if (rc != 0) {
2058 : 12 : return rc;
2059 : : }
2060 : :
2061 [ + + ]: 488 : while (buf_len != 0) {
2062 [ + + # # : 304 : len = spdk_min(buf_len, _to_next_boundary(buf_offset, ctx->block_size));
# # # # #
# ]
2063 [ - + # # : 304 : offset_in_block = buf_offset % ctx->block_size;
# # ]
2064 [ - + # # : 304 : offset_blocks = buf_offset / ctx->block_size;
# # ]
2065 : :
2066 : 304 : guard = _dif_generate_split(&sgl, offset_in_block, len, guard, offset_blocks, ctx);
2067 : :
2068 : 304 : buf_len -= len;
2069 : 304 : buf_offset += len;
2070 : : }
2071 : :
2072 [ + + # # : 184 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
2073 [ # # # # ]: 184 : ctx->last_guard = guard;
2074 : 46 : }
2075 : :
2076 : 184 : return 0;
2077 : 49 : }
2078 : :
2079 : : int
2080 : 828691 : spdk_dif_verify_stream(struct iovec *iovs, int iovcnt,
2081 : : uint32_t data_offset, uint32_t data_len,
2082 : : struct spdk_dif_ctx *ctx,
2083 : : struct spdk_dif_error *err_blk)
2084 : : {
2085 : 828691 : uint32_t buf_len = 0, buf_offset = 0;
2086 : : uint32_t len, offset_in_block, offset_blocks;
2087 : 828691 : uint64_t guard = 0;
2088 : 27 : struct _dif_sgl sgl;
2089 : 828691 : int rc = 0;
2090 : :
2091 [ + - - + ]: 828691 : if (iovs == NULL || iovcnt == 0) {
2092 : 0 : return -EINVAL;
2093 : : }
2094 : :
2095 [ + + # # : 828691 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
2096 [ # # # # ]: 828691 : guard = ctx->last_guard;
2097 : 9 : }
2098 : :
2099 : 828691 : _dif_sgl_init(&sgl, iovs, iovcnt);
2100 : :
2101 : 828691 : rc = _dif_sgl_setup_stream(&sgl, &buf_offset, &buf_len, data_offset, data_len, ctx);
2102 [ - + ]: 828691 : if (rc != 0) {
2103 : 0 : return rc;
2104 : : }
2105 : :
2106 [ + + ]: 15113497 : while (buf_len != 0) {
2107 [ + + # # : 14284806 : len = spdk_min(buf_len, _to_next_boundary(buf_offset, ctx->block_size));
# # # # #
# ]
2108 [ - + # # : 14284806 : offset_in_block = buf_offset % ctx->block_size;
# # ]
2109 [ - + # # : 14284806 : offset_blocks = buf_offset / ctx->block_size;
# # ]
2110 : :
2111 : 14284824 : rc = _dif_verify_split(&sgl, offset_in_block, len, &guard, offset_blocks,
2112 : 18 : ctx, err_blk);
2113 [ + + ]: 14284806 : if (rc != 0) {
2114 : 0 : goto error;
2115 : : }
2116 : :
2117 : 14284806 : buf_len -= len;
2118 : 14284806 : buf_offset += len;
2119 : : }
2120 : :
2121 [ - + # # : 828700 : if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
# # # # ]
2122 [ # # # # ]: 828691 : ctx->last_guard = guard;
2123 : 9 : }
2124 : 27 : error:
2125 : 828691 : return rc;
2126 : 9 : }
2127 : :
2128 : : int
2129 : 207798 : spdk_dif_update_crc32c_stream(struct iovec *iovs, int iovcnt,
2130 : : uint32_t data_offset, uint32_t data_len,
2131 : : uint32_t *_crc32c, const struct spdk_dif_ctx *ctx)
2132 : : {
2133 : 207798 : uint32_t buf_len = 0, buf_offset = 0, len, offset_in_block;
2134 : : uint32_t crc32c;
2135 : 27 : struct _dif_sgl sgl;
2136 : : int rc;
2137 : :
2138 [ + - - + ]: 207798 : if (iovs == NULL || iovcnt == 0) {
2139 : 0 : return -EINVAL;
2140 : : }
2141 : :
2142 [ # # ]: 207798 : crc32c = *_crc32c;
2143 : 207798 : _dif_sgl_init(&sgl, iovs, iovcnt);
2144 : :
2145 : 207798 : rc = _dif_sgl_setup_stream(&sgl, &buf_offset, &buf_len, data_offset, data_len, ctx);
2146 [ + + ]: 207798 : if (rc != 0) {
2147 : 0 : return rc;
2148 : : }
2149 : :
2150 [ + + ]: 6117337 : while (buf_len != 0) {
2151 [ + + # # : 5909539 : len = spdk_min(buf_len, _to_next_boundary(buf_offset, ctx->block_size));
# # # # #
# ]
2152 [ - + # # : 5909539 : offset_in_block = buf_offset % ctx->block_size;
# # ]
2153 : :
2154 : 5909539 : crc32c = _dif_update_crc32c_split(&sgl, offset_in_block, len, crc32c, ctx);
2155 : :
2156 : 5909539 : buf_len -= len;
2157 : 5909539 : buf_offset += len;
2158 : : }
2159 : :
2160 [ # # ]: 207798 : *_crc32c = crc32c;
2161 : :
2162 : 207798 : return 0;
2163 : 9 : }
2164 : :
2165 : : void
2166 : 1114051 : spdk_dif_get_range_with_md(uint32_t data_offset, uint32_t data_len,
2167 : : uint32_t *_buf_offset, uint32_t *_buf_len,
2168 : : const struct spdk_dif_ctx *ctx)
2169 : : {
2170 : : uint32_t data_block_size, data_unalign, buf_offset, buf_len;
2171 : :
2172 [ + + - + : 1114051 : if (!ctx->md_interleave) {
# # # # ]
2173 : 0 : buf_offset = data_offset;
2174 : 0 : buf_len = data_len;
2175 : 0 : } else {
2176 [ # # # # : 1114051 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
2177 : :
2178 [ - + ]: 1114051 : data_unalign = data_offset % data_block_size;
2179 : :
2180 [ # # # # ]: 1114051 : buf_offset = _to_size_with_md(data_offset, data_block_size, ctx->block_size);
2181 [ # # # # ]: 1114061 : buf_len = _to_size_with_md(data_unalign + data_len, data_block_size, ctx->block_size) -
2182 : 10 : data_unalign;
2183 : : }
2184 : :
2185 [ + + ]: 1114051 : if (_buf_offset != NULL) {
2186 [ # # ]: 1114051 : *_buf_offset = buf_offset;
2187 : 10 : }
2188 : :
2189 [ + + ]: 1114051 : if (_buf_len != NULL) {
2190 [ # # ]: 1114051 : *_buf_len = buf_len;
2191 : 10 : }
2192 : 1114051 : }
2193 : :
2194 : : uint32_t
2195 : 543343 : spdk_dif_get_length_with_md(uint32_t data_len, const struct spdk_dif_ctx *ctx)
2196 : : {
2197 : : uint32_t data_block_size;
2198 : :
2199 [ - + - + : 543343 : if (!ctx->md_interleave) {
# # # # ]
2200 : 0 : return data_len;
2201 : : } else {
2202 [ # # # # : 543343 : data_block_size = ctx->block_size - ctx->md_size;
# # # # ]
2203 : :
2204 [ # # # # ]: 543343 : return _to_size_with_md(data_len, data_block_size, ctx->block_size);
2205 : : }
2206 : 11 : }
2207 : :
2208 : : static int
2209 : 288 : _dif_remap_ref_tag(struct _dif_sgl *sgl, uint32_t offset_blocks,
2210 : : const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk,
2211 : : bool check_ref_tag)
2212 : : {
2213 : 216 : uint32_t offset, buf_len;
2214 : 288 : uint64_t expected = 0, remapped;
2215 : 216 : uint8_t *buf;
2216 : 216 : struct _dif_sgl tmp_sgl;
2217 : 216 : struct spdk_dif dif;
2218 : :
2219 : : /* Fast forward to DIF field. */
2220 [ # # # # ]: 288 : _dif_sgl_advance(sgl, ctx->guard_interval);
2221 : 288 : _dif_sgl_copy(&tmp_sgl, sgl);
2222 : :
2223 : : /* Copy the split DIF field to the temporary DIF buffer */
2224 : 288 : offset = 0;
2225 [ + + # # : 648 : while (offset < _dif_size(ctx->dif_pi_format)) {
# # ]
2226 : 360 : _dif_sgl_get_buf(sgl, &buf, &buf_len);
2227 [ + + # # : 360 : buf_len = spdk_min(buf_len, _dif_size(ctx->dif_pi_format) - offset);
# # # # #
# ]
2228 : :
2229 [ - + - + : 360 : memcpy((uint8_t *)&dif + offset, buf, buf_len);
# # ]
2230 : :
2231 : 360 : _dif_sgl_advance(sgl, buf_len);
2232 : 360 : offset += buf_len;
2233 : : }
2234 : :
2235 [ - + ]: 288 : if (_dif_ignore(&dif, ctx)) {
2236 : 0 : goto end;
2237 : : }
2238 : :
2239 : : /* For type 1 and 2, the Reference Tag is incremented for each
2240 : : * subsequent logical block. For type 3, the Reference Tag
2241 : : * remains the same as the initial Reference Tag.
2242 : : */
2243 [ + - # # : 288 : if (ctx->dif_type != SPDK_DIF_TYPE3) {
# # ]
2244 [ # # # # : 288 : expected = ctx->init_ref_tag + ctx->ref_tag_offset + offset_blocks;
# # # # ]
2245 [ # # # # : 288 : remapped = ctx->remapped_init_ref_tag + ctx->ref_tag_offset + offset_blocks;
# # # # ]
2246 : 72 : } else {
2247 [ # # # # ]: 0 : remapped = ctx->remapped_init_ref_tag;
2248 : : }
2249 : :
2250 : : /* Verify the stored Reference Tag. */
2251 [ + - + + : 288 : if (check_ref_tag && !_dif_reftag_check(&dif, ctx, expected, offset_blocks, err_blk)) {
# # ]
2252 : 0 : return -1;
2253 : : }
2254 : :
2255 : : /* Update the stored Reference Tag to the remapped one. */
2256 [ # # # # ]: 288 : _dif_set_reftag(&dif, remapped, ctx->dif_pi_format);
2257 : :
2258 : 288 : offset = 0;
2259 [ + + # # : 648 : while (offset < _dif_size(ctx->dif_pi_format)) {
# # ]
2260 : 360 : _dif_sgl_get_buf(&tmp_sgl, &buf, &buf_len);
2261 [ + + # # : 360 : buf_len = spdk_min(buf_len, _dif_size(ctx->dif_pi_format) - offset);
# # # # #
# ]
2262 : :
2263 [ - + - + : 360 : memcpy(buf, (uint8_t *)&dif + offset, buf_len);
# # ]
2264 : :
2265 : 360 : _dif_sgl_advance(&tmp_sgl, buf_len);
2266 : 360 : offset += buf_len;
2267 : : }
2268 : :
2269 : 216 : end:
2270 [ # # # # : 288 : _dif_sgl_advance(sgl, ctx->block_size - ctx->guard_interval - _dif_size(ctx->dif_pi_format));
# # # # #
# # # ]
2271 : :
2272 : 288 : return 0;
2273 : 72 : }
2274 : :
2275 : : int
2276 : 48 : spdk_dif_remap_ref_tag(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
2277 : : const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk,
2278 : : bool check_ref_tag)
2279 : : {
2280 : 36 : struct _dif_sgl sgl;
2281 : : uint32_t offset_blocks;
2282 : : int rc;
2283 : :
2284 : 48 : _dif_sgl_init(&sgl, iovs, iovcnt);
2285 : :
2286 [ - + # # : 48 : if (!_dif_sgl_is_valid(&sgl, ctx->block_size * num_blocks)) {
# # ]
2287 : 0 : SPDK_ERRLOG("Size of iovec array is not valid.\n");
2288 : 0 : return -EINVAL;
2289 : : }
2290 : :
2291 [ - + # # : 48 : if (_dif_is_disabled(ctx->dif_type)) {
# # ]
2292 : 0 : return 0;
2293 : : }
2294 : :
2295 [ + + # # : 48 : if (!(ctx->dif_flags & SPDK_DIF_FLAGS_REFTAG_CHECK)) {
# # # # ]
2296 : 0 : return 0;
2297 : : }
2298 : :
2299 [ + + ]: 336 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
2300 [ # # ]: 288 : rc = _dif_remap_ref_tag(&sgl, offset_blocks, ctx, err_blk, check_ref_tag);
2301 [ + + ]: 288 : if (rc != 0) {
2302 : 0 : return rc;
2303 : : }
2304 : 72 : }
2305 : :
2306 : 48 : return 0;
2307 : 12 : }
2308 : :
2309 : : static int
2310 : 800 : _dix_remap_ref_tag(struct _dif_sgl *md_sgl, uint32_t offset_blocks,
2311 : : const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk,
2312 : : bool check_ref_tag)
2313 : : {
2314 : 800 : uint64_t expected = 0, remapped;
2315 : 600 : uint8_t *md_buf;
2316 : : struct spdk_dif *dif;
2317 : :
2318 : 800 : _dif_sgl_get_buf(md_sgl, &md_buf, NULL);
2319 : :
2320 [ # # # # : 800 : dif = (struct spdk_dif *)(md_buf + ctx->guard_interval);
# # ]
2321 : :
2322 [ - + ]: 800 : if (_dif_ignore(dif, ctx)) {
2323 : 0 : goto end;
2324 : : }
2325 : :
2326 : : /* For type 1 and 2, the Reference Tag is incremented for each
2327 : : * subsequent logical block. For type 3, the Reference Tag
2328 : : * remains the same as the initialReference Tag.
2329 : : */
2330 [ + - # # : 800 : if (ctx->dif_type != SPDK_DIF_TYPE3) {
# # ]
2331 [ # # # # : 800 : expected = ctx->init_ref_tag + ctx->ref_tag_offset + offset_blocks;
# # # # ]
2332 [ # # # # : 800 : remapped = ctx->remapped_init_ref_tag + ctx->ref_tag_offset + offset_blocks;
# # # # ]
2333 : 200 : } else {
2334 [ # # # # ]: 0 : remapped = ctx->remapped_init_ref_tag;
2335 : : }
2336 : :
2337 : : /* Verify the stored Reference Tag. */
2338 [ + + + + : 800 : if (check_ref_tag && !_dif_reftag_check(dif, ctx, expected, offset_blocks, err_blk)) {
# # ]
2339 : 0 : return -1;
2340 : : }
2341 : :
2342 : : /* Update the stored Reference Tag to the remapped one. */
2343 [ # # # # ]: 800 : _dif_set_reftag(dif, remapped, ctx->dif_pi_format);
2344 : :
2345 : 600 : end:
2346 [ # # # # ]: 800 : _dif_sgl_advance(md_sgl, ctx->md_size);
2347 : :
2348 : 800 : return 0;
2349 : 200 : }
2350 : :
2351 : : int
2352 : 48 : spdk_dix_remap_ref_tag(struct iovec *md_iov, uint32_t num_blocks,
2353 : : const struct spdk_dif_ctx *ctx,
2354 : : struct spdk_dif_error *err_blk,
2355 : : bool check_ref_tag)
2356 : : {
2357 : 36 : struct _dif_sgl md_sgl;
2358 : : uint32_t offset_blocks;
2359 : : int rc;
2360 : :
2361 : 48 : _dif_sgl_init(&md_sgl, md_iov, 1);
2362 : :
2363 [ - + # # : 48 : if (!_dif_sgl_is_valid(&md_sgl, ctx->md_size * num_blocks)) {
# # ]
2364 : 0 : SPDK_ERRLOG("Size of metadata iovec array is not valid.\n");
2365 : 0 : return -EINVAL;
2366 : : }
2367 : :
2368 [ - + # # : 48 : if (_dif_is_disabled(ctx->dif_type)) {
# # ]
2369 : 0 : return 0;
2370 : : }
2371 : :
2372 [ + + # # : 48 : if (!(ctx->dif_flags & SPDK_DIF_FLAGS_REFTAG_CHECK)) {
# # # # ]
2373 : 0 : return 0;
2374 : : }
2375 : :
2376 [ + + ]: 848 : for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
2377 [ # # ]: 800 : rc = _dix_remap_ref_tag(&md_sgl, offset_blocks, ctx, err_blk, check_ref_tag);
2378 [ + + ]: 800 : if (rc != 0) {
2379 : 0 : return rc;
2380 : : }
2381 : 200 : }
2382 : :
2383 : 48 : return 0;
2384 : 12 : }
|