LCOV - code coverage report
Current view: top level - spdk/lib/util - dif.c (source / functions) Hit Total Coverage
Test: Combined Lines: 1262 1377 91.6 %
Date: 2024-11-20 07:42:03 Functions: 105 107 98.1 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 575 2674 21.5 %

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

Generated by: LCOV version 1.15