LCOV - code coverage report
Current view: top level - spdk/lib/util - dif.c (source / functions) Hit Total Coverage
Test: Combined Lines: 1264 1379 91.7 %
Date: 2024-12-09 11:32:52 Functions: 105 108 97.2 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 582 2674 21.8 %

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

Generated by: LCOV version 1.14