LCOV - code coverage report
Current view: top level - spdk/lib/util - dif.c (source / functions) Hit Total Coverage
Test: Combined Lines: 956 1043 91.7 %
Date: 2024-07-13 01:21:49 Functions: 82 82 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 452 602 75.1 %

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

Generated by: LCOV version 1.14