LCOV - code coverage report
Current view: top level - spdk/lib/util - dif.c (source / functions) Hit Total Coverage
Test: Combined Lines: 1001 1251 80.0 %
Date: 2024-12-16 05:26:51 Functions: 93 94 98.9 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 451 2400 18.8 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2022 Intel Corporation.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk/dif.h"
       7                 :            : #include "spdk/crc16.h"
       8                 :            : #include "spdk/crc32.h"
       9                 :            : #include "spdk/crc64.h"
      10                 :            : #include "spdk/endian.h"
      11                 :            : #include "spdk/log.h"
      12                 :            : #include "spdk/util.h"
      13                 :            : 
      14                 :            : #define REFTAG_MASK_16 0x00000000FFFFFFFF
      15                 :            : #define REFTAG_MASK_32 0xFFFFFFFFFFFFFFFF
      16                 :            : #define REFTAG_MASK_64 0x0000FFFFFFFFFFFF
      17                 :            : 
      18                 :            : /* The variable size Storage Tag and Reference Tag is not supported yet,
      19                 :            :  * so the maximum size of the Reference Tag is assumed.
      20                 :            :  */
      21                 :            : struct spdk_dif {
      22                 :            :         union {
      23                 :            :                 struct {
      24                 :            :                         uint16_t guard;
      25                 :            :                         uint16_t app_tag;
      26                 :            :                         uint32_t stor_ref_space;
      27                 :            :                 } g16;
      28                 :            :                 struct {
      29                 :            :                         uint32_t guard;
      30                 :            :                         uint16_t app_tag;
      31                 :            :                         uint16_t stor_ref_space_p1;
      32                 :            :                         uint64_t stor_ref_space_p2;
      33                 :            :                 } g32;
      34                 :            :                 struct {
      35                 :            :                         uint64_t guard;
      36                 :            :                         uint16_t app_tag;
      37                 :            :                         uint16_t stor_ref_space_p1;
      38                 :            :                         uint32_t stor_ref_space_p2;
      39                 :            :                 } g64;
      40                 :            :         };
      41                 :            : };
      42                 :            : SPDK_STATIC_ASSERT(SPDK_SIZEOF_MEMBER(struct spdk_dif, g16) == 8, "Incorrect size");
      43                 :            : SPDK_STATIC_ASSERT(SPDK_SIZEOF_MEMBER(struct spdk_dif, g32) == 16, "Incorrect size");
      44                 :            : SPDK_STATIC_ASSERT(SPDK_SIZEOF_MEMBER(struct spdk_dif, g64) == 16, "Incorrect size");
      45                 :            : 
      46                 :            : /* Context to iterate or create a iovec array.
      47                 :            :  * Each sgl is either iterated or created at a time.
      48                 :            :  */
      49                 :            : struct _dif_sgl {
      50                 :            :         /* Current iovec in the iteration or creation */
      51                 :            :         struct iovec *iov;
      52                 :            : 
      53                 :            :         /* Remaining count of iovecs in the iteration or creation. */
      54                 :            :         int iovcnt;
      55                 :            : 
      56                 :            :         /* Current offset in the iovec */
      57                 :            :         uint32_t iov_offset;
      58                 :            : 
      59                 :            :         /* Size of the created iovec array in bytes */
      60                 :            :         uint32_t total_size;
      61                 :            : };
      62                 :            : 
      63                 :            : static inline void
      64                 :    8349202 : _dif_sgl_init(struct _dif_sgl *s, struct iovec *iovs, int iovcnt)
      65                 :            : {
      66   [ #  #  #  # ]:    8349202 :         s->iov = iovs;
      67   [ #  #  #  # ]:    8349202 :         s->iovcnt = iovcnt;
      68   [ #  #  #  # ]:    8349202 :         s->iov_offset = 0;
      69   [ #  #  #  # ]:    8349202 :         s->total_size = 0;
      70                 :    8349202 : }
      71                 :            : 
      72                 :            : static void
      73                 :  151498531 : _dif_sgl_advance(struct _dif_sgl *s, uint32_t step)
      74                 :            : {
      75   [ #  #  #  # ]:  151498531 :         s->iov_offset += step;
      76   [ +  +  #  #  :  157339457 :         while (s->iovcnt != 0) {
                   #  # ]
      77   [ +  +  #  #  :  151680745 :                 if (s->iov_offset < s->iov->iov_len) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
      78                 :  145839819 :                         break;
      79                 :            :                 }
      80                 :            : 
      81   [ #  #  #  #  :    5840926 :                 s->iov_offset -= s->iov->iov_len;
          #  #  #  #  #  
                #  #  # ]
      82   [ #  #  #  # ]:    5840926 :                 s->iov++;
      83   [ #  #  #  # ]:    5840926 :                 s->iovcnt--;
      84                 :            :         }
      85                 :  151498531 : }
      86                 :            : 
      87                 :            : static inline void
      88                 :  118042386 : _dif_sgl_get_buf(struct _dif_sgl *s, uint8_t **_buf, uint32_t *_buf_len)
      89                 :            : {
      90         [ +  - ]:  118042386 :         if (_buf != NULL) {
      91   [ #  #  #  #  :  118042386 :                 *_buf = (uint8_t *)s->iov->iov_base + s->iov_offset;
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
      92                 :          0 :         }
      93         [ +  + ]:  118042386 :         if (_buf_len != NULL) {
      94   [ #  #  #  #  :   93435254 :                 *_buf_len = s->iov->iov_len - s->iov_offset;
          #  #  #  #  #  
             #  #  #  #  
                      # ]
      95                 :          0 :         }
      96                 :  118042386 : }
      97                 :            : 
      98                 :            : static inline bool
      99                 :   31247008 : _dif_sgl_append(struct _dif_sgl *s, uint8_t *data, uint32_t data_len)
     100                 :            : {
     101   [ -  +  #  #  :   31247008 :         assert(s->iovcnt > 0);
             #  #  #  # ]
     102   [ #  #  #  #  :   31247008 :         s->iov->iov_base = data;
             #  #  #  # ]
     103   [ #  #  #  #  :   31247008 :         s->iov->iov_len = data_len;
             #  #  #  # ]
     104   [ #  #  #  # ]:   31247008 :         s->total_size += data_len;
     105   [ #  #  #  # ]:   31247008 :         s->iov++;
     106   [ #  #  #  # ]:   31247008 :         s->iovcnt--;
     107                 :            : 
     108   [ +  +  #  #  :   31247008 :         if (s->iovcnt > 0) {
                   #  # ]
     109                 :   30844826 :                 return true;
     110                 :            :         } else {
     111                 :     402182 :                 return false;
     112                 :            :         }
     113                 :          0 : }
     114                 :            : 
     115                 :            : static inline bool
     116                 :   31173236 : _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         [ +  + ]:   62018062 :         while (data_len != 0) {
     122                 :   31247008 :                 _dif_sgl_get_buf(src, &buf, &buf_len);
     123         [ #  # ]:   31247008 :                 buf_len = spdk_min(buf_len, data_len);
     124                 :            : 
     125         [ +  + ]:   31247008 :                 if (!_dif_sgl_append(dst, buf, buf_len)) {
     126                 :     402182 :                         return false;
     127                 :            :                 }
     128                 :            : 
     129                 :   30844826 :                 _dif_sgl_advance(src, buf_len);
     130                 :   30844826 :                 data_len -= buf_len;
     131                 :            :         }
     132                 :            : 
     133                 :   30771054 :         return true;
     134                 :          0 : }
     135                 :            : 
     136                 :            : /* This function must be used before starting iteration. */
     137                 :            : static bool
     138                 :    2705552 : _dif_sgl_is_bytes_multiple(struct _dif_sgl *s, uint32_t bytes)
     139                 :            : {
     140                 :            :         int i;
     141                 :            : 
     142   [ +  +  #  #  :    5375751 :         for (i = 0; i < s->iovcnt; i++) {
             #  #  #  # ]
     143   [ +  +  +  +  :    2708369 :                 if (s->iov[i].iov_len % bytes) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     144                 :      38170 :                         return false;
     145                 :            :                 }
     146                 :          0 :         }
     147                 :            : 
     148                 :    2667382 :         return true;
     149                 :          0 : }
     150                 :            : 
     151                 :            : /* This function must be used before starting iteration. */
     152                 :            : static bool
     153                 :    6431815 : _dif_sgl_is_valid(struct _dif_sgl *s, uint32_t bytes)
     154                 :            : {
     155                 :    6431815 :         uint64_t total = 0;
     156                 :            :         int i;
     157                 :            : 
     158   [ +  +  #  #  :   13341453 :         for (i = 0; i < s->iovcnt; i++) {
             #  #  #  # ]
     159   [ #  #  #  #  :    6909638 :                 total += s->iov[i].iov_len;
          #  #  #  #  #  
                      # ]
     160                 :          0 :         }
     161                 :            : 
     162                 :    6431815 :         return total >= bytes;
     163                 :            : }
     164                 :            : 
     165                 :            : static void
     166                 :        216 : _dif_sgl_copy(struct _dif_sgl *to, struct _dif_sgl *from)
     167                 :            : {
     168   [ -  +  -  + ]:        216 :         memcpy(to, from, sizeof(struct _dif_sgl));
     169                 :        216 : }
     170                 :            : 
     171                 :            : static bool
     172                 :    2583354 : _dif_is_disabled(enum spdk_dif_type dif_type)
     173                 :            : {
     174         [ +  + ]:    2583354 :         if (dif_type == SPDK_DIF_DISABLE) {
     175                 :     184568 :                 return true;
     176                 :            :         } else {
     177                 :    2398786 :                 return false;
     178                 :            :         }
     179                 :          0 : }
     180                 :            : 
     181                 :            : static inline size_t
     182                 :   78106457 : _dif_size(enum spdk_dif_pi_format dif_pi_format)
     183                 :            : {
     184                 :            :         uint8_t size;
     185                 :            : 
     186         [ +  + ]:   78106457 :         if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
     187                 :   78098981 :                 size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g16);
     188         [ +  + ]:       7476 :         } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
     189                 :       3801 :                 size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g32);
     190                 :          0 :         } else {
     191                 :       3675 :                 size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g64);
     192                 :            :         }
     193                 :            : 
     194                 :   78106457 :         return size;
     195                 :            : }
     196                 :            : 
     197                 :            : static uint32_t
     198                 :    1514164 : _get_guard_interval(uint32_t block_size, uint32_t md_size, bool dif_loc, bool md_interleave,
     199                 :            :                     size_t dif_size)
     200                 :            : {
     201   [ +  +  #  # ]:    1514164 :         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   [ +  +  #  # ]:    1512796 :                 if (md_interleave) {
     208                 :    1328018 :                         return block_size - dif_size;
     209                 :            :                 } else {
     210                 :     184778 :                         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   [ +  +  #  # ]:       1368 :                 if (md_interleave) {
     218                 :       1044 :                         return block_size - md_size;
     219                 :            :                 } else {
     220                 :        324 :                         return 0;
     221                 :            :                 }
     222                 :            :         }
     223                 :          0 : }
     224                 :            : 
     225                 :            : static inline uint8_t
     226                 :        540 : _dif_guard_size(enum spdk_dif_pi_format dif_pi_format)
     227                 :            : {
     228                 :            :         uint8_t size;
     229                 :            : 
     230         [ +  + ]:        540 :         if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
     231                 :        180 :                 size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g16.guard);
     232         [ +  + ]:        360 :         } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
     233                 :        180 :                 size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g32.guard);
     234                 :          0 :         } else {
     235                 :        180 :                 size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g64.guard);
     236                 :            :         }
     237                 :            : 
     238                 :        540 :         return size;
     239                 :            : }
     240                 :            : 
     241                 :            : static inline void
     242                 :   24630697 : _dif_set_guard(struct spdk_dif *dif, uint64_t guard, enum spdk_dif_pi_format dif_pi_format)
     243                 :            : {
     244         [ +  + ]:   24630697 :         if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
     245   [ #  #  #  #  :   24627529 :                 to_be16(&(dif->g16.guard), (uint16_t)guard);
                   #  # ]
     246         [ +  + ]:       3168 :         } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
     247   [ #  #  #  #  :       1611 :                 to_be32(&(dif->g32.guard), (uint32_t)guard);
                   #  # ]
     248                 :          0 :         } else {
     249   [ #  #  #  #  :       1557 :                 to_be64(&(dif->g64.guard), guard);
                   #  # ]
     250                 :            :         }
     251                 :   24630697 : }
     252                 :            : 
     253                 :            : static inline uint64_t
     254                 :   20412085 : _dif_get_guard(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
     255                 :            : {
     256                 :            :         uint64_t guard;
     257                 :            : 
     258         [ +  + ]:   20412085 :         if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
     259   [ #  #  #  #  :   20409442 :                 guard = (uint64_t)from_be16(&(dif->g16.guard));
                   #  # ]
     260         [ +  + ]:       2643 :         } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
     261   [ #  #  #  #  :       1320 :                 guard = (uint64_t)from_be32(&(dif->g32.guard));
                   #  # ]
     262                 :          0 :         } else {
     263   [ #  #  #  #  :       1323 :                 guard = from_be64(&(dif->g64.guard));
                   #  # ]
     264                 :            :         }
     265                 :            : 
     266                 :   20412085 :         return guard;
     267                 :            : }
     268                 :            : 
     269                 :            : static inline uint64_t
     270                 :   47247870 : _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         [ +  + ]:   47247870 :         if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
     276                 :   47239495 :                 guard = (uint64_t)spdk_crc16_t10dif((uint16_t)guard_seed, buf, buf_len);
     277         [ +  + ]:       8375 :         } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
     278                 :       4231 :                 guard = (uint64_t)spdk_crc32c_nvme(buf, buf_len, guard_seed);
     279                 :          0 :         } else {
     280                 :       4144 :                 guard = spdk_crc64_nvme(buf, buf_len, guard_seed);
     281                 :            :         }
     282                 :            : 
     283                 :   47247870 :         return guard;
     284                 :            : }
     285                 :            : 
     286                 :            : static uint64_t
     287                 :   25024910 : 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                 :   25024910 :         uint64_t guard = guard_seed;
     291                 :       1776 :         uint32_t offset, end, buf_len;
     292                 :       1776 :         uint8_t *buf;
     293                 :            : 
     294                 :   25024910 :         offset = start;
     295   [ #  #  #  #  :   25024910 :         end = start + spdk_min(len, ctx->guard_interval - start);
          #  #  #  #  #  
                      # ]
     296                 :            : 
     297         [ +  + ]:   50124333 :         while (offset < end) {
     298                 :   25099423 :                 _dif_sgl_get_buf(sgl, &buf, &buf_len);
     299         [ #  # ]:   25099423 :                 buf_len = spdk_min(buf_len, end - offset);
     300                 :            : 
     301   [ +  -  #  #  :   25099423 :                 if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
     302   [ #  #  #  # ]:   25099423 :                         guard = _dif_generate_guard(guard, buf, buf_len, ctx->dif_pi_format);
     303                 :          0 :                 }
     304                 :            : 
     305                 :   25099423 :                 _dif_sgl_advance(sgl, buf_len);
     306                 :   25099423 :                 offset += buf_len;
     307                 :            :         }
     308                 :            : 
     309                 :   25024910 :         return guard;
     310                 :            : }
     311                 :            : 
     312                 :            : static inline uint64_t
     313                 :    2454125 : _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         [ +  + ]:    2454125 :         if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
     319                 :    2452377 :                 guard = (uint64_t)spdk_crc16_t10dif_copy((uint16_t)guard_seed, dst, src, buf_len);
     320         [ +  + ]:       1748 :         } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
     321   [ -  +  -  + ]:        874 :                 memcpy(dst, src, buf_len);
     322                 :        874 :                 guard = (uint64_t)spdk_crc32c_nvme(src, buf_len, guard_seed);
     323                 :          0 :         } else {
     324   [ -  +  -  + ]:        874 :                 memcpy(dst, src, buf_len);
     325                 :        874 :                 guard = spdk_crc64_nvme(src, buf_len, guard_seed);
     326                 :            :         }
     327                 :            : 
     328                 :    2454125 :         return guard;
     329                 :            : }
     330                 :            : 
     331                 :            : static uint64_t
     332                 :        402 : _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                 :        402 :         uint32_t offset = 0, src_len, dst_len, buf_len;
     337                 :        402 :         uint8_t *src, *dst;
     338                 :            : 
     339         [ +  + ]:       1248 :         while (offset < data_len) {
     340                 :        846 :                 _dif_sgl_get_buf(src_sgl, &src, &src_len);
     341                 :        846 :                 _dif_sgl_get_buf(dst_sgl, &dst, &dst_len);
     342         [ #  # ]:        846 :                 buf_len = spdk_min(src_len, dst_len);
     343         [ #  # ]:        846 :                 buf_len = spdk_min(buf_len, data_len - offset);
     344                 :            : 
     345                 :        846 :                 guard = _dif_generate_guard_copy(guard, dst, src, buf_len, dif_pi_format);
     346                 :            : 
     347                 :        846 :                 _dif_sgl_advance(src_sgl, buf_len);
     348                 :        846 :                 _dif_sgl_advance(dst_sgl, buf_len);
     349                 :        846 :                 offset += buf_len;
     350                 :            :         }
     351                 :            : 
     352                 :        402 :         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                 :        360 : _dif_apptag_offset(enum spdk_dif_pi_format dif_pi_format)
     377                 :            : {
     378                 :        360 :         return _dif_guard_size(dif_pi_format);
     379                 :            : }
     380                 :            : 
     381                 :            : static inline uint8_t
     382                 :        360 : _dif_apptag_size(void)
     383                 :            : {
     384                 :        360 :         return SPDK_SIZEOF_MEMBER(struct spdk_dif, g16.app_tag);
     385                 :            : }
     386                 :            : 
     387                 :            : static inline void
     388                 :    9044307 : _dif_set_apptag(struct spdk_dif *dif, uint16_t app_tag, enum spdk_dif_pi_format dif_pi_format)
     389                 :            : {
     390         [ +  + ]:    9044307 :         if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
     391   [ #  #  #  #  :    9041160 :                 to_be16(&(dif->g16.app_tag), app_tag);
                   #  # ]
     392         [ +  + ]:       3147 :         } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
     393   [ #  #  #  #  :       1602 :                 to_be16(&(dif->g32.app_tag), app_tag);
                   #  # ]
     394                 :          0 :         } else {
     395   [ #  #  #  #  :       1545 :                 to_be16(&(dif->g64.app_tag), app_tag);
                   #  # ]
     396                 :            :         }
     397                 :    9044307 : }
     398                 :            : 
     399                 :            : static inline uint16_t
     400                 :   25239982 : _dif_get_apptag(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
     401                 :            : {
     402                 :            :         uint16_t app_tag;
     403                 :            : 
     404         [ +  + ]:   25239982 :         if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
     405   [ #  #  #  #  :   25233919 :                 app_tag = from_be16(&(dif->g16.app_tag));
                   #  # ]
     406         [ +  + ]:       6063 :         } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
     407   [ #  #  #  #  :       3033 :                 app_tag = from_be16(&(dif->g32.app_tag));
                   #  # ]
     408                 :          0 :         } else {
     409   [ #  #  #  #  :       3030 :                 app_tag = from_be16(&(dif->g64.app_tag));
                   #  # ]
     410                 :            :         }
     411                 :            : 
     412                 :   25239982 :         return app_tag;
     413                 :            : }
     414                 :            : 
     415                 :            : static inline bool
     416                 :   20414707 : _dif_apptag_ignore(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
     417                 :            : {
     418                 :   20414707 :         return _dif_get_apptag(dif, dif_pi_format) == SPDK_DIF_APPTAG_IGNORE;
     419                 :            : }
     420                 :            : 
     421                 :            : static inline uint8_t
     422                 :        180 : _dif_reftag_offset(enum spdk_dif_pi_format dif_pi_format)
     423                 :            : {
     424                 :            :         uint8_t offset;
     425                 :            : 
     426         [ +  + ]:        180 :         if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
     427                 :         60 :                 offset = _dif_apptag_offset(dif_pi_format) + _dif_apptag_size();
     428         [ +  + ]:        120 :         } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
     429                 :         60 :                 offset = _dif_apptag_offset(dif_pi_format) + _dif_apptag_size()
     430                 :          0 :                          + SPDK_SIZEOF_MEMBER(struct spdk_dif, g32.stor_ref_space_p1);
     431                 :          0 :         } else {
     432                 :         60 :                 offset = _dif_apptag_offset(dif_pi_format) + _dif_apptag_size();
     433                 :            :         }
     434                 :            : 
     435                 :        180 :         return offset;
     436                 :            : }
     437                 :            : 
     438                 :            : static inline uint8_t
     439                 :        180 : _dif_reftag_size(enum spdk_dif_pi_format dif_pi_format)
     440                 :            : {
     441                 :            :         uint8_t size;
     442                 :            : 
     443         [ +  + ]:        180 :         if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
     444                 :         60 :                 size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g16.stor_ref_space);
     445         [ +  + ]:        120 :         } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
     446                 :         60 :                 size = SPDK_SIZEOF_MEMBER(struct spdk_dif, g32.stor_ref_space_p2);
     447                 :          0 :         } else {
     448                 :         60 :                 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                 :        180 :         return size;
     453                 :            : }
     454                 :            : 
     455                 :            : static inline void
     456                 :   15194562 : _dif_set_reftag(struct spdk_dif *dif, uint64_t ref_tag, enum spdk_dif_pi_format dif_pi_format)
     457                 :            : {
     458         [ +  + ]:   15194562 :         if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
     459   [ #  #  #  #  :   15191133 :                 to_be32(&(dif->g16.stor_ref_space), (uint32_t)ref_tag);
                   #  # ]
     460         [ +  + ]:       3429 :         } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
     461   [ #  #  #  #  :       1740 :                 to_be64(&(dif->g32.stor_ref_space_p2), ref_tag);
                   #  # ]
     462                 :          0 :         } else {
     463   [ #  #  #  #  :       1689 :                 to_be16(&(dif->g64.stor_ref_space_p1), (uint16_t)(ref_tag >> 32));
             #  #  #  # ]
     464   [ #  #  #  #  :       1689 :                 to_be32(&(dif->g64.stor_ref_space_p2), (uint32_t)ref_tag);
                   #  # ]
     465                 :            :         }
     466                 :   15194562 : }
     467                 :            : 
     468                 :            : static inline uint64_t
     469                 :   10975128 : _dif_get_reftag(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
     470                 :            : {
     471                 :            :         uint64_t ref_tag;
     472                 :            : 
     473         [ +  + ]:   10975128 :         if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
     474   [ #  #  #  #  :   10972470 :                 ref_tag = (uint64_t)from_be32(&(dif->g16.stor_ref_space));
                   #  # ]
     475         [ +  + ]:       2658 :         } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
     476   [ #  #  #  #  :       1329 :                 ref_tag = from_be64(&(dif->g32.stor_ref_space_p2));
                   #  # ]
     477                 :          0 :         } else {
     478   [ #  #  #  #  :       1329 :                 ref_tag = (uint64_t)from_be16(&(dif->g64.stor_ref_space_p1));
                   #  # ]
     479         [ #  # ]:       1329 :                 ref_tag <<= 32;
     480   [ #  #  #  #  :       1329 :                 ref_tag |= (uint64_t)from_be32(&(dif->g64.stor_ref_space_p2));
                   #  # ]
     481                 :            :         }
     482                 :            : 
     483                 :   10975128 :         return ref_tag;
     484                 :            : }
     485                 :            : 
     486                 :            : static inline bool
     487                 :   10974906 : _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                 :   10974906 :         _ref_tag = _dif_get_reftag(dif, dif_pi_format);
     494                 :            : 
     495         [ +  + ]:   10974906 :         if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
     496                 :   10972380 :                 match = (_ref_tag == (ref_tag & REFTAG_MASK_16));
     497         [ +  + ]:       2526 :         } else if (dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
     498                 :       1263 :                 match = (_ref_tag == ref_tag);
     499                 :          0 :         } else {
     500                 :       1263 :                 match = (_ref_tag == (ref_tag & REFTAG_MASK_64));
     501                 :            :         }
     502                 :            : 
     503         [ #  # ]:   10974906 :         return match;
     504                 :            : }
     505                 :            : 
     506                 :            : static inline bool
     507                 :         21 : _dif_reftag_ignore(struct spdk_dif *dif, enum spdk_dif_pi_format dif_pi_format)
     508                 :            : {
     509                 :         21 :         return _dif_reftag_match(dif, REFTAG_MASK_32, dif_pi_format);
     510                 :            : }
     511                 :            : 
     512                 :            : static bool
     513                 :   20414707 : _dif_ignore(struct spdk_dif *dif, const struct spdk_dif_ctx *ctx)
     514                 :            : {
     515   [ +  +  -  #  :   20414707 :         switch (ctx->dif_type) {
                #  #  # ]
     516                 :   10977576 :         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   [ +  +  #  #  :   10977576 :                 if (_dif_apptag_ignore(dif, ctx->dif_pi_format)) {
                   #  # ]
     522                 :        114 :                         return true;
     523                 :            :                 }
     524                 :   10977462 :                 break;
     525                 :    9437131 :         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   [ +  +  +  +  :    9437152 :                 if (_dif_apptag_ignore(dif, ctx->dif_pi_format) &&
             #  #  #  # ]
     532   [ #  #  #  # ]:         21 :                     _dif_reftag_ignore(dif, ctx->dif_pi_format)) {
     533                 :         12 :                         return true;
     534                 :            :                 }
     535                 :    9437119 :                 break;
     536                 :          0 :         default:
     537                 :          0 :                 break;
     538                 :            :         }
     539                 :            : 
     540                 :   20414581 :         return false;
     541                 :          0 : }
     542                 :            : 
     543                 :            : static bool
     544                 :    1514167 : _dif_pi_format_is_valid(enum spdk_dif_pi_format dif_pi_format)
     545                 :            : {
     546         [ +  + ]:    1514167 :         switch (dif_pi_format) {
     547                 :    1514164 :         case SPDK_DIF_PI_FORMAT_16:
     548                 :            :         case SPDK_DIF_PI_FORMAT_32:
     549                 :            :         case SPDK_DIF_PI_FORMAT_64:
     550                 :    1514164 :                 return true;
     551                 :          3 :         default:
     552                 :          3 :                 return false;
     553                 :            :         }
     554                 :          0 : }
     555                 :            : 
     556                 :            : static bool
     557                 :    1514170 : _dif_type_is_valid(enum spdk_dif_type dif_type)
     558                 :            : {
     559         [ +  + ]:    1514170 :         switch (dif_type) {
     560                 :    1514167 :         case SPDK_DIF_DISABLE:
     561                 :            :         case SPDK_DIF_TYPE1:
     562                 :            :         case SPDK_DIF_TYPE2:
     563                 :            :         case SPDK_DIF_TYPE3:
     564                 :    1514167 :                 return true;
     565                 :          3 :         default:
     566                 :          3 :                 return false;
     567                 :            :         }
     568                 :          0 : }
     569                 :            : 
     570                 :            : int
     571                 :    1514155 : 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                 :    1514155 :         enum spdk_dif_pi_format dif_pi_format = SPDK_DIF_PI_FORMAT_16;
     578                 :            : 
     579         [ +  - ]:    1514155 :         if (opts != NULL) {
     580   [ -  +  #  #  :    1514155 :                 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   [ #  #  #  # ]:    1514155 :                 dif_pi_format = opts->dif_pi_format;
     586                 :          0 :         }
     587                 :            : 
     588         [ -  + ]:    1514155 :         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         [ +  + ]:    1514155 :         if (md_size < _dif_size(dif_pi_format)) {
     594                 :         30 :                 SPDK_ERRLOG("Metadata size is smaller than DIF size.\n");
     595                 :         30 :                 return -EINVAL;
     596                 :            :         }
     597                 :            : 
     598   [ +  +  #  # ]:    1514125 :         if (md_interleave) {
     599         [ -  + ]:    1329008 :                 if (block_size < md_size) {
     600                 :          0 :                         SPDK_ERRLOG("Block size is smaller than DIF size.\n");
     601                 :          0 :                         return -EINVAL;
     602                 :            :                 }
     603                 :    1329008 :                 data_block_size = block_size - md_size;
     604                 :          0 :         } else {
     605                 :     185117 :                 data_block_size = block_size;
     606                 :            :         }
     607                 :            : 
     608         [ +  + ]:    1514125 :         if (data_block_size == 0) {
     609                 :          6 :                 SPDK_ERRLOG("Zero data block size is not allowed\n");
     610                 :          6 :                 return -EINVAL;
     611                 :            :         }
     612                 :            : 
     613         [ +  + ]:    1514119 :         if (dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
     614   [ -  +  #  # ]:    1513183 :                 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                 :          0 :         } else {
     619   [ +  +  #  # ]:        936 :                 if ((data_block_size % 4096) != 0) {
     620                 :         18 :                         SPDK_ERRLOG("Data block size should be a multiple of 4kB\n");
     621                 :         18 :                         return -EINVAL;
     622                 :            :                 }
     623                 :            :         }
     624                 :            : 
     625   [ #  #  #  # ]:    1514101 :         ctx->block_size = block_size;
     626   [ #  #  #  # ]:    1514101 :         ctx->md_size = md_size;
     627   [ #  #  #  #  :    1514101 :         ctx->md_interleave = md_interleave;
                   #  # ]
     628   [ #  #  #  # ]:    1514101 :         ctx->dif_pi_format = dif_pi_format;
     629   [ #  #  #  #  :    1514101 :         ctx->guard_interval = _get_guard_interval(block_size, md_size, dif_loc, md_interleave,
             #  #  #  # ]
     630   [ #  #  #  # ]:    1514101 :                               _dif_size(ctx->dif_pi_format));
     631   [ #  #  #  # ]:    1514101 :         ctx->dif_type = dif_type;
     632   [ #  #  #  # ]:    1514101 :         ctx->dif_flags = dif_flags;
     633   [ #  #  #  # ]:    1514101 :         ctx->init_ref_tag = init_ref_tag;
     634   [ #  #  #  # ]:    1514101 :         ctx->apptag_mask = apptag_mask;
     635   [ #  #  #  # ]:    1514101 :         ctx->app_tag = app_tag;
     636   [ #  #  #  # ]:    1514101 :         ctx->data_offset = data_offset;
     637   [ -  +  #  #  :    1514101 :         ctx->ref_tag_offset = data_offset / data_block_size;
                   #  # ]
     638   [ #  #  #  # ]:    1514101 :         ctx->last_guard = guard_seed;
     639   [ #  #  #  # ]:    1514101 :         ctx->guard_seed = guard_seed;
     640   [ #  #  #  # ]:    1514101 :         ctx->remapped_init_ref_tag = 0;
     641                 :            : 
     642                 :    1514101 :         return 0;
     643                 :          0 : }
     644                 :            : 
     645                 :            : void
     646                 :    1253678 : spdk_dif_ctx_set_data_offset(struct spdk_dif_ctx *ctx, uint32_t data_offset)
     647                 :            : {
     648                 :            :         uint32_t data_block_size;
     649                 :            : 
     650   [ +  +  +  -  :    1253678 :         if (ctx->md_interleave) {
             #  #  #  # ]
     651   [ #  #  #  #  :    1253678 :                 data_block_size = ctx->block_size - ctx->md_size;
             #  #  #  # ]
     652                 :          0 :         } else {
     653   [ #  #  #  # ]:          0 :                 data_block_size = ctx->block_size;
     654                 :            :         }
     655                 :            : 
     656   [ #  #  #  # ]:    1253678 :         ctx->data_offset = data_offset;
     657   [ -  +  #  #  :    1253678 :         ctx->ref_tag_offset = data_offset / data_block_size;
                   #  # ]
     658                 :    1253678 : }
     659                 :            : 
     660                 :            : void
     661                 :         72 : spdk_dif_ctx_set_remapped_init_ref_tag(struct spdk_dif_ctx *ctx,
     662                 :            :                                        uint32_t remapped_init_ref_tag)
     663                 :            : {
     664   [ #  #  #  # ]:         72 :         ctx->remapped_init_ref_tag = remapped_init_ref_tag;
     665                 :         72 : }
     666                 :            : 
     667                 :            : static void
     668                 :   24632227 : _dif_generate(void *_dif, uint64_t guard, uint32_t offset_blocks,
     669                 :            :               const struct spdk_dif_ctx *ctx)
     670                 :            : {
     671                 :   24632227 :         struct spdk_dif *dif = _dif;
     672                 :            :         uint64_t ref_tag;
     673                 :            : 
     674   [ +  +  #  #  :   24632227 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
     675   [ #  #  #  # ]:   24630697 :                 _dif_set_guard(dif, guard, ctx->dif_pi_format);
     676                 :          0 :         }
     677                 :            : 
     678   [ +  +  #  #  :   24632227 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_APPTAG_CHECK) {
             #  #  #  # ]
     679   [ #  #  #  #  :    9044307 :                 _dif_set_apptag(dif, ctx->app_tag, ctx->dif_pi_format);
             #  #  #  # ]
     680                 :          0 :         }
     681                 :            : 
     682   [ +  +  #  #  :   24632227 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_REFTAG_CHECK) {
             #  #  #  # ]
     683                 :            :                 /* For type 1 and 2, the reference tag is incremented for each
     684                 :            :                  * subsequent logical block. For type 3, the reference tag
     685                 :            :                  * remains the same as the initial reference tag.
     686                 :            :                  */
     687   [ +  +  #  #  :   15193746 :                 if (ctx->dif_type != SPDK_DIF_TYPE3) {
                   #  # ]
     688   [ #  #  #  #  :   15193725 :                         ref_tag = ctx->init_ref_tag + ctx->ref_tag_offset + offset_blocks;
             #  #  #  # ]
     689                 :          0 :                 } else {
     690   [ #  #  #  #  :         21 :                         ref_tag = ctx->init_ref_tag + ctx->ref_tag_offset;
             #  #  #  # ]
     691                 :            :                 }
     692                 :            : 
     693                 :            :                 /* Overwrite reference tag if initialization reference tag is SPDK_DIF_REFTAG_IGNORE */
     694   [ +  +  #  #  :   15193746 :                 if (ctx->init_ref_tag == SPDK_DIF_REFTAG_IGNORE) {
                   #  # ]
     695   [ +  -  #  #  :          6 :                         if (ctx->dif_pi_format == SPDK_DIF_PI_FORMAT_16) {
                   #  # ]
     696                 :          6 :                                 ref_tag = REFTAG_MASK_16;
     697   [ #  #  #  #  :          0 :                         } else if (ctx->dif_pi_format == SPDK_DIF_PI_FORMAT_32) {
                   #  # ]
     698                 :          0 :                                 ref_tag = REFTAG_MASK_32;
     699                 :          0 :                         } else {
     700                 :          0 :                                 ref_tag = REFTAG_MASK_64;
     701                 :            :                         }
     702                 :          0 :                 }
     703                 :            : 
     704   [ #  #  #  # ]:   15193746 :                 _dif_set_reftag(dif, ref_tag, ctx->dif_pi_format);
     705                 :          0 :         }
     706                 :   24632227 : }
     707                 :            : 
     708                 :            : static void
     709                 :    1450355 : dif_generate(struct _dif_sgl *sgl, uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
     710                 :            : {
     711                 :            :         uint32_t offset_blocks;
     712                 :     291611 :         uint8_t *buf;
     713                 :    1450355 :         uint64_t guard = 0;
     714                 :            : 
     715         [ +  + ]:   14188140 :         for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
     716                 :   12737785 :                 _dif_sgl_get_buf(sgl, &buf, NULL);
     717                 :            : 
     718   [ +  +  #  #  :   12737785 :                 if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
     719   [ #  #  #  #  :   12737335 :                         guard = _dif_generate_guard(ctx->guard_seed, buf, ctx->guard_interval, ctx->dif_pi_format);
          #  #  #  #  #  
                #  #  # ]
     720                 :          0 :                 }
     721                 :            : 
     722   [ #  #  #  #  :   12737785 :                 _dif_generate(buf + ctx->guard_interval, guard, offset_blocks, ctx);
                   #  # ]
     723                 :            : 
     724   [ #  #  #  # ]:   12737785 :                 _dif_sgl_advance(sgl, ctx->block_size);
     725                 :          0 :         }
     726                 :    1450355 : }
     727                 :            : 
     728                 :            : static void
     729                 :    9437542 : dif_store_split(struct _dif_sgl *sgl, struct spdk_dif *dif,
     730                 :            :                 const struct spdk_dif_ctx *ctx)
     731                 :            : {
     732                 :    9437542 :         uint32_t offset = 0, rest_md_len, buf_len;
     733                 :        870 :         uint8_t *buf;
     734                 :            : 
     735   [ #  #  #  #  :    9437542 :         rest_md_len = ctx->block_size - ctx->guard_interval;
             #  #  #  # ]
     736                 :            : 
     737         [ +  + ]:   18875696 :         while (offset < rest_md_len) {
     738                 :    9438154 :                 _dif_sgl_get_buf(sgl, &buf, &buf_len);
     739                 :            : 
     740   [ +  +  #  #  :    9438154 :                 if (offset < _dif_size(ctx->dif_pi_format)) {
                   #  # ]
     741   [ +  +  #  #  :    9437806 :                         buf_len = spdk_min(buf_len, _dif_size(ctx->dif_pi_format) - offset);
          #  #  #  #  #  
                      # ]
     742   [ -  +  -  +  :    9437806 :                         memcpy(buf, (uint8_t *)dif + offset, buf_len);
                   #  # ]
     743                 :          0 :                 } else {
     744         [ #  # ]:        348 :                         buf_len = spdk_min(buf_len, rest_md_len - offset);
     745                 :            :                 }
     746                 :            : 
     747                 :    9438154 :                 _dif_sgl_advance(sgl, buf_len);
     748                 :    9438154 :                 offset += buf_len;
     749                 :            :         }
     750                 :    9437542 : }
     751                 :            : 
     752                 :            : static uint64_t
     753                 :    9437458 : _dif_generate_split(struct _dif_sgl *sgl, uint32_t offset_in_block, uint32_t data_len,
     754                 :            :                     uint64_t guard, uint32_t offset_blocks, const struct spdk_dif_ctx *ctx)
     755                 :            : {
     756                 :    9437458 :         struct spdk_dif dif = {};
     757                 :            : 
     758   [ -  +  #  #  :    9437458 :         assert(offset_in_block < ctx->guard_interval);
             #  #  #  # ]
     759   [ +  +  -  +  :    9437458 :         assert(offset_in_block + data_len < ctx->guard_interval ||
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     760                 :            :                offset_in_block + data_len == ctx->block_size);
     761                 :            : 
     762                 :            :         /* Compute CRC over split logical block data. */
     763                 :    9437458 :         guard = dif_generate_guard_split(guard, sgl, offset_in_block, data_len, ctx);
     764                 :            : 
     765   [ +  +  #  #  :    9437458 :         if (offset_in_block + data_len < ctx->guard_interval) {
                   #  # ]
     766                 :        117 :                 return guard;
     767                 :            :         }
     768                 :            : 
     769                 :            :         /* If a whole logical block data is parsed, generate DIF
     770                 :            :          * and save it to the temporary DIF area.
     771                 :            :          */
     772                 :    9437341 :         _dif_generate(&dif, guard, offset_blocks, ctx);
     773                 :            : 
     774                 :            :         /* Copy generated DIF field to the split DIF field, and then
     775                 :            :          * skip metadata field after DIF field (if any).
     776                 :            :          */
     777                 :    9437341 :         dif_store_split(sgl, &dif, ctx);
     778                 :            : 
     779   [ +  -  #  #  :    9437341 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
     780   [ #  #  #  # ]:    9437341 :                 guard = ctx->guard_seed;
     781                 :          0 :         }
     782                 :            : 
     783                 :    9437341 :         return guard;
     784                 :          0 : }
     785                 :            : 
     786                 :            : static void
     787                 :      37318 : dif_generate_split(struct _dif_sgl *sgl, uint32_t num_blocks,
     788                 :            :                    const struct spdk_dif_ctx *ctx)
     789                 :            : {
     790                 :            :         uint32_t offset_blocks;
     791                 :      37318 :         uint64_t guard = 0;
     792                 :            : 
     793   [ +  -  #  #  :      37318 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
     794   [ #  #  #  # ]:      37318 :                 guard = ctx->guard_seed;
     795                 :          0 :         }
     796                 :            : 
     797         [ +  + ]:    9474521 :         for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
     798   [ #  #  #  # ]:    9437203 :                 _dif_generate_split(sgl, 0, ctx->block_size, guard, offset_blocks, ctx);
     799                 :          0 :         }
     800                 :      37318 : }
     801                 :            : 
     802                 :            : int
     803                 :    1487649 : spdk_dif_generate(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
     804                 :            :                   const struct spdk_dif_ctx *ctx)
     805                 :            : {
     806                 :     292043 :         struct _dif_sgl sgl;
     807                 :            : 
     808                 :    1487649 :         _dif_sgl_init(&sgl, iovs, iovcnt);
     809                 :            : 
     810   [ -  +  #  #  :    1487649 :         if (!_dif_sgl_is_valid(&sgl, ctx->block_size * num_blocks)) {
                   #  # ]
     811                 :          0 :                 SPDK_ERRLOG("Size of iovec array is not valid.\n");
     812                 :          0 :                 return -EINVAL;
     813                 :            :         }
     814                 :            : 
     815   [ +  +  #  #  :    1487649 :         if (_dif_is_disabled(ctx->dif_type)) {
                   #  # ]
     816                 :          3 :                 return 0;
     817                 :            :         }
     818                 :            : 
     819   [ +  +  #  #  :    1487646 :         if (_dif_sgl_is_bytes_multiple(&sgl, ctx->block_size)) {
                   #  # ]
     820                 :    1450328 :                 dif_generate(&sgl, num_blocks, ctx);
     821                 :          0 :         } else {
     822                 :      37318 :                 dif_generate_split(&sgl, num_blocks, ctx);
     823                 :            :         }
     824                 :            : 
     825                 :    1487646 :         return 0;
     826                 :          0 : }
     827                 :            : 
     828                 :            : static void
     829                 :        837 : _dif_error_set(struct spdk_dif_error *err_blk, uint8_t err_type,
     830                 :            :                uint64_t expected, uint64_t actual, uint32_t err_offset)
     831                 :            : {
     832         [ +  + ]:        837 :         if (err_blk) {
     833   [ #  #  #  # ]:        810 :                 err_blk->err_type = err_type;
     834   [ #  #  #  # ]:        810 :                 err_blk->expected = expected;
     835   [ #  #  #  # ]:        810 :                 err_blk->actual = actual;
     836   [ #  #  #  # ]:        810 :                 err_blk->err_offset = err_offset;
     837                 :          0 :         }
     838                 :        837 : }
     839                 :            : 
     840                 :            : static bool
     841                 :   20413576 : _dif_reftag_check(struct spdk_dif *dif, const struct spdk_dif_ctx *ctx,
     842                 :            :                   uint64_t expected_reftag, uint32_t offset_blocks, struct spdk_dif_error *err_blk)
     843                 :            : {
     844                 :            :         uint64_t reftag;
     845                 :            : 
     846   [ +  +  #  #  :   20413576 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_REFTAG_CHECK) {
             #  #  #  # ]
     847   [ +  -  -  #  :   10974885 :                 switch (ctx->dif_type) {
                #  #  # ]
     848                 :   10974885 :                 case SPDK_DIF_TYPE1:
     849                 :            :                 case SPDK_DIF_TYPE2:
     850                 :            :                         /* Compare the DIF Reference Tag field to the passed Reference Tag.
     851                 :            :                          * The passed Reference Tag will be the least significant 4 bytes
     852                 :            :                          * or 8 bytes (depending on the PI format)
     853                 :            :                          * of the LBA when Type 1 is used, and application specific value
     854                 :            :                          * if Type 2 is used.
     855                 :            :                          */
     856   [ +  +  #  #  :   10974885 :                         if (!_dif_reftag_match(dif, expected_reftag, ctx->dif_pi_format)) {
                   #  # ]
     857   [ #  #  #  # ]:        216 :                                 reftag = _dif_get_reftag(dif, ctx->dif_pi_format);
     858                 :        216 :                                 _dif_error_set(err_blk, SPDK_DIF_REFTAG_ERROR, expected_reftag,
     859                 :          0 :                                                reftag, offset_blocks);
     860                 :        216 :                                 SPDK_ERRLOG("Failed to compare Ref Tag: LBA=%" PRIu64 "," \
     861                 :            :                                             " Expected=%lx, Actual=%lx\n",
     862                 :            :                                             expected_reftag, expected_reftag, reftag);
     863                 :        216 :                                 return false;
     864                 :            :                         }
     865                 :   10974669 :                         break;
     866                 :          0 :                 case SPDK_DIF_TYPE3:
     867                 :            :                         /* For Type 3, computed Reference Tag remains unchanged.
     868                 :            :                          * Hence ignore the Reference Tag field.
     869                 :            :                          */
     870                 :          0 :                         break;
     871                 :          0 :                 default:
     872                 :          0 :                         break;
     873                 :            :                 }
     874                 :          0 :         }
     875                 :            : 
     876                 :   20413360 :         return true;
     877                 :          0 : }
     878                 :            : 
     879                 :            : static int
     880                 :   20413891 : _dif_verify(void *_dif, uint64_t guard, uint32_t offset_blocks,
     881                 :            :             const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk)
     882                 :            : {
     883                 :   20413891 :         struct spdk_dif *dif = _dif;
     884                 :            :         uint64_t _guard;
     885                 :            :         uint16_t _app_tag;
     886                 :            :         uint64_t ref_tag;
     887                 :            : 
     888         [ +  + ]:   20413891 :         if (_dif_ignore(dif, ctx)) {
     889                 :        126 :                 return 0;
     890                 :            :         }
     891                 :            : 
     892                 :            :         /* For type 1 and 2, the reference tag is incremented for each
     893                 :            :          * subsequent logical block. For type 3, the reference tag
     894                 :            :          * remains the same as the initial reference tag.
     895                 :            :          */
     896   [ +  +  #  #  :   20413765 :         if (ctx->dif_type != SPDK_DIF_TYPE3) {
                   #  # ]
     897   [ #  #  #  #  :   10976646 :                 ref_tag = ctx->init_ref_tag + ctx->ref_tag_offset + offset_blocks;
             #  #  #  # ]
     898                 :          0 :         } else {
     899   [ #  #  #  #  :    9437119 :                 ref_tag = ctx->init_ref_tag + ctx->ref_tag_offset;
             #  #  #  # ]
     900                 :            :         }
     901                 :            : 
     902   [ +  +  #  #  :   20413765 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
     903                 :            :                 /* Compare the DIF Guard field to the CRC computed over the logical
     904                 :            :                  * block data.
     905                 :            :                  */
     906   [ #  #  #  # ]:   20412055 :                 _guard = _dif_get_guard(dif, ctx->dif_pi_format);
     907         [ +  + ]:   20412055 :                 if (_guard != guard) {
     908                 :        387 :                         _dif_error_set(err_blk, SPDK_DIF_GUARD_ERROR, _guard, guard,
     909                 :          0 :                                        offset_blocks);
     910                 :        387 :                         SPDK_ERRLOG("Failed to compare Guard: LBA=%" PRIu64 "," \
     911                 :            :                                     "  Expected=%lx, Actual=%lx\n",
     912                 :            :                                     ref_tag, _guard, guard);
     913                 :        387 :                         return -1;
     914                 :            :                 }
     915                 :          0 :         }
     916                 :            : 
     917   [ +  +  #  #  :   20413378 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_APPTAG_CHECK) {
             #  #  #  # ]
     918                 :            :                 /* Compare unmasked bits in the DIF Application Tag field to the
     919                 :            :                  * passed Application Tag.
     920                 :            :                  */
     921   [ #  #  #  # ]:    4825269 :                 _app_tag = _dif_get_apptag(dif, ctx->dif_pi_format);
     922   [ +  +  #  #  :    4825269 :                 if ((_app_tag & ctx->apptag_mask) != (ctx->app_tag & ctx->apptag_mask)) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     923   [ #  #  #  # ]:        234 :                         _dif_error_set(err_blk, SPDK_DIF_APPTAG_ERROR, ctx->app_tag,
     924   [ #  #  #  # ]:        234 :                                        (_app_tag & ctx->apptag_mask), offset_blocks);
     925   [ #  #  #  #  :        234 :                         SPDK_ERRLOG("Failed to compare App Tag: LBA=%" PRIu64 "," \
             #  #  #  # ]
     926                 :            :                                     "  Expected=%x, Actual=%x\n",
     927                 :            :                                     ref_tag, ctx->app_tag, (_app_tag & ctx->apptag_mask));
     928                 :        234 :                         return -1;
     929                 :            :                 }
     930                 :          0 :         }
     931                 :            : 
     932         [ +  + ]:   20413144 :         if (!_dif_reftag_check(dif, ctx, ref_tag, offset_blocks, err_blk)) {
     933                 :        216 :                 return -1;
     934                 :            :         }
     935                 :            : 
     936                 :   20412928 :         return 0;
     937                 :          0 : }
     938                 :            : 
     939                 :            : static int
     940                 :     337361 : dif_verify(struct _dif_sgl *sgl, uint32_t num_blocks,
     941                 :            :            const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk)
     942                 :            : {
     943                 :            :         uint32_t offset_blocks;
     944                 :            :         int rc;
     945                 :     121613 :         uint8_t *buf;
     946                 :     337361 :         uint64_t guard = 0;
     947                 :            : 
     948         [ +  + ]:    3035064 :         for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
     949                 :    2697829 :                 _dif_sgl_get_buf(sgl, &buf, NULL);
     950                 :            : 
     951   [ +  +  #  #  :    2697829 :                 if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
     952   [ #  #  #  #  :    2697241 :                         guard = _dif_generate_guard(ctx->guard_seed, buf, ctx->guard_interval, ctx->dif_pi_format);
          #  #  #  #  #  
                #  #  # ]
     953                 :          0 :                 }
     954                 :            : 
     955   [ #  #  #  #  :    2697829 :                 rc = _dif_verify(buf + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
                   #  # ]
     956         [ +  + ]:    2697829 :                 if (rc != 0) {
     957                 :        126 :                         return rc;
     958                 :            :                 }
     959                 :            : 
     960   [ #  #  #  # ]:    2697703 :                 _dif_sgl_advance(sgl, ctx->block_size);
     961                 :          0 :         }
     962                 :            : 
     963                 :     337235 :         return 0;
     964                 :          0 : }
     965                 :            : 
     966                 :            : static void
     967                 :   15587206 : dif_load_split(struct _dif_sgl *sgl, struct spdk_dif *dif,
     968                 :            :                const struct spdk_dif_ctx *ctx)
     969                 :            : {
     970                 :   15587206 :         uint32_t offset = 0, rest_md_len, buf_len;
     971                 :        744 :         uint8_t *buf;
     972                 :            : 
     973   [ #  #  #  #  :   15587206 :         rest_md_len = ctx->block_size - ctx->guard_interval;
             #  #  #  # ]
     974                 :            : 
     975         [ +  + ]:   31175015 :         while (offset < rest_md_len) {
     976                 :   15587809 :                 _dif_sgl_get_buf(sgl, &buf, &buf_len);
     977                 :            : 
     978   [ +  +  #  #  :   15587809 :                 if (offset < _dif_size(ctx->dif_pi_format)) {
                   #  # ]
     979   [ +  +  #  #  :   15587461 :                         buf_len = spdk_min(buf_len, _dif_size(ctx->dif_pi_format) - offset);
          #  #  #  #  #  
                      # ]
     980   [ -  +  -  +  :   15587461 :                         memcpy((uint8_t *)dif + offset, buf, buf_len);
                   #  # ]
     981                 :          0 :                 } else {
     982         [ #  # ]:        348 :                         buf_len = spdk_min(buf_len, rest_md_len - offset);
     983                 :            :                 }
     984                 :            : 
     985                 :   15587809 :                 _dif_sgl_advance(sgl, buf_len);
     986                 :   15587809 :                 offset += buf_len;
     987                 :            :         }
     988                 :   15587206 : }
     989                 :            : 
     990                 :            : static int
     991                 :   15587050 : _dif_verify_split(struct _dif_sgl *sgl, uint32_t offset_in_block, uint32_t data_len,
     992                 :            :                   uint64_t *_guard, uint32_t offset_blocks,
     993                 :            :                   const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk)
     994                 :            : {
     995         [ #  # ]:   15587050 :         uint64_t guard = *_guard;
     996                 :   15587050 :         struct spdk_dif dif = {};
     997                 :            :         int rc;
     998                 :            : 
     999   [ -  +  #  # ]:   15587050 :         assert(_guard != NULL);
    1000   [ -  +  #  #  :   15587050 :         assert(offset_in_block < ctx->guard_interval);
             #  #  #  # ]
    1001   [ +  +  -  +  :   15587050 :         assert(offset_in_block + data_len < ctx->guard_interval ||
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1002                 :            :                offset_in_block + data_len == ctx->block_size);
    1003                 :            : 
    1004                 :   15587050 :         guard = dif_generate_guard_split(guard, sgl, offset_in_block, data_len, ctx);
    1005                 :            : 
    1006   [ +  +  #  #  :   15587050 :         if (offset_in_block + data_len < ctx->guard_interval) {
                   #  # ]
    1007         [ #  # ]:         45 :                 *_guard = guard;
    1008                 :         45 :                 return 0;
    1009                 :            :         }
    1010                 :            : 
    1011                 :   15587005 :         dif_load_split(sgl, &dif, ctx);
    1012                 :            : 
    1013                 :   15587005 :         rc = _dif_verify(&dif, guard, offset_blocks, ctx, err_blk);
    1014         [ +  + ]:   15587005 :         if (rc != 0) {
    1015                 :        360 :                 return rc;
    1016                 :            :         }
    1017                 :            : 
    1018   [ +  -  #  #  :   15586645 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
    1019   [ #  #  #  # ]:   15586645 :                 guard = ctx->guard_seed;
    1020                 :          0 :         }
    1021                 :            : 
    1022         [ #  # ]:   15586645 :         *_guard = guard;
    1023                 :   15586645 :         return 0;
    1024                 :          0 : }
    1025                 :            : 
    1026                 :            : static int
    1027                 :        450 : dif_verify_split(struct _dif_sgl *sgl, uint32_t num_blocks,
    1028                 :            :                  const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk)
    1029                 :            : {
    1030                 :            :         uint32_t offset_blocks;
    1031                 :        450 :         uint64_t guard = 0;
    1032                 :            :         int rc;
    1033                 :            : 
    1034   [ +  -  #  #  :        450 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
    1035   [ #  #  #  # ]:        450 :                 guard = ctx->guard_seed;
    1036                 :          0 :         }
    1037                 :            : 
    1038         [ +  + ]:        597 :         for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
    1039   [ #  #  #  # ]:        507 :                 rc = _dif_verify_split(sgl, 0, ctx->block_size, &guard, offset_blocks,
    1040                 :          0 :                                        ctx, err_blk);
    1041         [ +  + ]:        507 :                 if (rc != 0) {
    1042                 :        360 :                         return rc;
    1043                 :            :                 }
    1044                 :          0 :         }
    1045                 :            : 
    1046                 :         90 :         return 0;
    1047                 :          0 : }
    1048                 :            : 
    1049                 :            : int
    1050                 :     337787 : spdk_dif_verify(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
    1051                 :            :                 const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk)
    1052                 :            : {
    1053                 :     122039 :         struct _dif_sgl sgl;
    1054                 :            : 
    1055                 :     337787 :         _dif_sgl_init(&sgl, iovs, iovcnt);
    1056                 :            : 
    1057   [ -  +  #  #  :     337787 :         if (!_dif_sgl_is_valid(&sgl, ctx->block_size * num_blocks)) {
                   #  # ]
    1058                 :          0 :                 SPDK_ERRLOG("Size of iovec array is not valid.\n");
    1059                 :          0 :                 return -EINVAL;
    1060                 :            :         }
    1061                 :            : 
    1062   [ +  +  #  #  :     337787 :         if (_dif_is_disabled(ctx->dif_type)) {
                   #  # ]
    1063                 :          3 :                 return 0;
    1064                 :            :         }
    1065                 :            : 
    1066   [ +  +  #  #  :     337784 :         if (_dif_sgl_is_bytes_multiple(&sgl, ctx->block_size)) {
                   #  # ]
    1067                 :     337334 :                 return dif_verify(&sgl, num_blocks, ctx, err_blk);
    1068                 :            :         } else {
    1069                 :        450 :                 return dif_verify_split(&sgl, num_blocks, ctx, err_blk);
    1070                 :            :         }
    1071                 :          0 : }
    1072                 :            : 
    1073                 :            : static uint32_t
    1074                 :         27 : dif_update_crc32c(struct _dif_sgl *sgl, uint32_t num_blocks,
    1075                 :            :                   uint32_t crc32c,  const struct spdk_dif_ctx *ctx)
    1076                 :            : {
    1077                 :            :         uint32_t offset_blocks;
    1078                 :         27 :         uint8_t *buf;
    1079                 :            : 
    1080         [ +  + ]:        135 :         for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
    1081                 :        108 :                 _dif_sgl_get_buf(sgl, &buf, NULL);
    1082                 :            : 
    1083   [ #  #  #  #  :        108 :                 crc32c = spdk_crc32c_update(buf, ctx->block_size - ctx->md_size, crc32c);
             #  #  #  # ]
    1084                 :            : 
    1085   [ #  #  #  # ]:        108 :                 _dif_sgl_advance(sgl, ctx->block_size);
    1086                 :          0 :         }
    1087                 :            : 
    1088                 :         27 :         return crc32c;
    1089                 :            : }
    1090                 :            : 
    1091                 :            : static uint32_t
    1092                 :    6009460 : _dif_update_crc32c_split(struct _dif_sgl *sgl, uint32_t offset_in_block, uint32_t data_len,
    1093                 :            :                          uint32_t crc32c, const struct spdk_dif_ctx *ctx)
    1094                 :            : {
    1095                 :        153 :         uint32_t data_block_size, buf_len;
    1096                 :        153 :         uint8_t *buf;
    1097                 :            : 
    1098   [ #  #  #  #  :    6009460 :         data_block_size = ctx->block_size - ctx->md_size;
             #  #  #  # ]
    1099                 :            : 
    1100   [ -  +  #  #  :    6009460 :         assert(offset_in_block + data_len <= ctx->block_size);
             #  #  #  # ]
    1101                 :            : 
    1102         [ +  + ]:   18051916 :         while (data_len != 0) {
    1103                 :   12042456 :                 _dif_sgl_get_buf(sgl, &buf, &buf_len);
    1104         [ #  # ]:   12042456 :                 buf_len = spdk_min(buf_len, data_len);
    1105                 :            : 
    1106         [ +  + ]:   12042456 :                 if (offset_in_block < data_block_size) {
    1107         [ #  # ]:    6032987 :                         buf_len = spdk_min(buf_len, data_block_size - offset_in_block);
    1108                 :    6032987 :                         crc32c = spdk_crc32c_update(buf, buf_len, crc32c);
    1109                 :          0 :                 }
    1110                 :            : 
    1111                 :   12042456 :                 _dif_sgl_advance(sgl, buf_len);
    1112                 :   12042456 :                 offset_in_block += buf_len;
    1113                 :   12042456 :                 data_len -= buf_len;
    1114                 :            :         }
    1115                 :            : 
    1116                 :    6009460 :         return crc32c;
    1117                 :            : }
    1118                 :            : 
    1119                 :            : static uint32_t
    1120                 :         18 : dif_update_crc32c_split(struct _dif_sgl *sgl, uint32_t num_blocks,
    1121                 :            :                         uint32_t crc32c, const struct spdk_dif_ctx *ctx)
    1122                 :            : {
    1123                 :            :         uint32_t offset_blocks;
    1124                 :            : 
    1125         [ +  + ]:         90 :         for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
    1126   [ #  #  #  # ]:         72 :                 crc32c = _dif_update_crc32c_split(sgl, 0, ctx->block_size, crc32c, ctx);
    1127                 :          0 :         }
    1128                 :            : 
    1129                 :         18 :         return crc32c;
    1130                 :            : }
    1131                 :            : 
    1132                 :            : int
    1133                 :         45 : spdk_dif_update_crc32c(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
    1134                 :            :                        uint32_t *_crc32c, const struct spdk_dif_ctx *ctx)
    1135                 :            : {
    1136                 :         45 :         struct _dif_sgl sgl;
    1137                 :            : 
    1138         [ -  + ]:         45 :         if (_crc32c == NULL) {
    1139                 :          0 :                 return -EINVAL;
    1140                 :            :         }
    1141                 :            : 
    1142                 :         45 :         _dif_sgl_init(&sgl, iovs, iovcnt);
    1143                 :            : 
    1144   [ -  +  #  #  :         45 :         if (!_dif_sgl_is_valid(&sgl, ctx->block_size * num_blocks)) {
                   #  # ]
    1145                 :          0 :                 SPDK_ERRLOG("Size of iovec array is not valid.\n");
    1146                 :          0 :                 return -EINVAL;
    1147                 :            :         }
    1148                 :            : 
    1149   [ +  +  #  #  :         45 :         if (_dif_sgl_is_bytes_multiple(&sgl, ctx->block_size)) {
                   #  # ]
    1150   [ #  #  #  # ]:         27 :                 *_crc32c = dif_update_crc32c(&sgl, num_blocks, *_crc32c, ctx);
    1151                 :          0 :         } else {
    1152   [ #  #  #  # ]:         18 :                 *_crc32c = dif_update_crc32c_split(&sgl, num_blocks, *_crc32c, ctx);
    1153                 :            :         }
    1154                 :            : 
    1155                 :         45 :         return 0;
    1156                 :          0 : }
    1157                 :            : 
    1158                 :            : static void
    1159                 :    2452916 : _dif_generate_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
    1160                 :            :                    uint32_t offset_blocks, const struct spdk_dif_ctx *ctx)
    1161                 :            : {
    1162                 :            :         uint32_t data_block_size;
    1163                 :     860436 :         uint8_t *src, *dst;
    1164                 :    2452916 :         uint64_t guard = 0;
    1165                 :            : 
    1166   [ #  #  #  #  :    2452916 :         data_block_size = ctx->block_size - ctx->md_size;
             #  #  #  # ]
    1167                 :            : 
    1168                 :    2452916 :         _dif_sgl_get_buf(src_sgl, &src, NULL);
    1169                 :    2452916 :         _dif_sgl_get_buf(dst_sgl, &dst, NULL);
    1170                 :            : 
    1171   [ +  +  #  #  :    2452916 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
    1172   [ #  #  #  # ]:    2452442 :                 guard = _dif_generate_guard_copy(ctx->guard_seed, dst, src, data_block_size,
    1173   [ #  #  #  # ]:    2452442 :                                                  ctx->dif_pi_format);
    1174         [ #  # ]:    2452442 :                 guard = _dif_generate_guard(guard, dst + data_block_size,
    1175   [ #  #  #  #  :    2452442 :                                             ctx->guard_interval - data_block_size, ctx->dif_pi_format);
             #  #  #  # ]
    1176                 :          0 :         } else {
    1177   [ -  +  -  + ]:        474 :                 memcpy(dst, src, data_block_size);
    1178                 :            :         }
    1179                 :            : 
    1180   [ #  #  #  #  :    2452916 :         _dif_generate(dst + ctx->guard_interval, guard, offset_blocks, ctx);
                   #  # ]
    1181                 :            : 
    1182                 :    2452916 :         _dif_sgl_advance(src_sgl, data_block_size);
    1183   [ #  #  #  # ]:    2452916 :         _dif_sgl_advance(dst_sgl, ctx->block_size);
    1184                 :    2452916 : }
    1185                 :            : 
    1186                 :            : static void
    1187                 :     306598 : dif_generate_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
    1188                 :            :                   uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
    1189                 :            : {
    1190                 :            :         uint32_t offset_blocks;
    1191                 :            : 
    1192         [ +  + ]:    2759514 :         for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
    1193                 :    2452916 :                 _dif_generate_copy(src_sgl, dst_sgl, offset_blocks, ctx);
    1194                 :          0 :         }
    1195                 :     306598 : }
    1196                 :            : 
    1197                 :            : static void
    1198                 :        201 : _dif_generate_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
    1199                 :            :                          uint32_t offset_blocks, const struct spdk_dif_ctx *ctx)
    1200                 :            : {
    1201                 :            :         uint32_t data_block_size;
    1202                 :        201 :         uint64_t guard = 0;
    1203                 :        201 :         struct spdk_dif dif = {};
    1204                 :            : 
    1205   [ #  #  #  #  :        201 :         data_block_size = ctx->block_size - ctx->md_size;
             #  #  #  # ]
    1206                 :            : 
    1207   [ +  -  #  #  :        201 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
    1208   [ #  #  #  # ]:        201 :                 guard = _dif_generate_guard_copy_split(ctx->guard_seed, dst_sgl, src_sgl,
    1209   [ #  #  #  # ]:        201 :                                                        data_block_size, ctx->dif_pi_format);
    1210                 :        201 :                 guard = dif_generate_guard_split(guard, dst_sgl, data_block_size,
    1211   [ #  #  #  # ]:        201 :                                                  ctx->guard_interval - data_block_size, ctx);
    1212                 :          0 :         } else {
    1213                 :          0 :                 _data_copy_split(dst_sgl, src_sgl, data_block_size);
    1214   [ #  #  #  # ]:          0 :                 _dif_sgl_advance(dst_sgl, ctx->guard_interval - data_block_size);
    1215                 :            :         }
    1216                 :            : 
    1217                 :        201 :         _dif_generate(&dif, guard, offset_blocks, ctx);
    1218                 :            : 
    1219                 :        201 :         dif_store_split(dst_sgl, &dif, ctx);
    1220                 :        201 : }
    1221                 :            : 
    1222                 :            : static void
    1223                 :         93 : dif_generate_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
    1224                 :            :                         uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
    1225                 :            : {
    1226                 :            :         uint32_t offset_blocks;
    1227                 :            : 
    1228         [ +  + ]:        294 :         for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
    1229                 :        201 :                 _dif_generate_copy_split(src_sgl, dst_sgl, offset_blocks, ctx);
    1230                 :          0 :         }
    1231                 :         93 : }
    1232                 :            : 
    1233                 :            : static void
    1234                 :         36 : _dif_disable_insert_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
    1235                 :            :                          const struct spdk_dif_ctx *ctx)
    1236                 :            : {
    1237                 :         36 :         uint32_t offset = 0, src_len, dst_len, buf_len, data_block_size;
    1238                 :         36 :         uint8_t *src, *dst;
    1239                 :            : 
    1240   [ #  #  #  #  :         36 :         data_block_size = ctx->block_size - ctx->md_size;
             #  #  #  # ]
    1241                 :            : 
    1242         [ +  + ]:         96 :         while (offset < data_block_size) {
    1243                 :         60 :                 _dif_sgl_get_buf(src_sgl, &src, &src_len);
    1244                 :         60 :                 _dif_sgl_get_buf(dst_sgl, &dst, &dst_len);
    1245         [ #  # ]:         60 :                 buf_len = spdk_min(src_len, dst_len);
    1246         [ #  # ]:         60 :                 buf_len = spdk_min(buf_len, data_block_size - offset);
    1247                 :            : 
    1248   [ -  +  -  + ]:         60 :                 memcpy(dst, src, buf_len);
    1249                 :            : 
    1250                 :         60 :                 _dif_sgl_advance(src_sgl, buf_len);
    1251                 :         60 :                 _dif_sgl_advance(dst_sgl, buf_len);
    1252                 :         60 :                 offset += buf_len;
    1253                 :            :         }
    1254                 :            : 
    1255   [ #  #  #  # ]:         36 :         _dif_sgl_advance(dst_sgl, ctx->md_size);
    1256                 :         36 : }
    1257                 :            : 
    1258                 :            : static void
    1259                 :          9 : dif_disable_insert_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
    1260                 :            :                         uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
    1261                 :            : {
    1262                 :            :         uint32_t offset_blocks;
    1263                 :            : 
    1264         [ +  + ]:         45 :         for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
    1265                 :         36 :                 _dif_disable_insert_copy(src_sgl, dst_sgl, ctx);
    1266                 :          0 :         }
    1267                 :          9 : }
    1268                 :            : 
    1269                 :            : int
    1270                 :     306703 : spdk_dif_generate_copy(struct iovec *iovs, int iovcnt, struct iovec *bounce_iovs,
    1271                 :            :                        int bounce_iovcnt, uint32_t num_blocks,
    1272                 :            :                        const struct spdk_dif_ctx *ctx)
    1273                 :            : {
    1274                 :     107641 :         struct _dif_sgl src_sgl, dst_sgl;
    1275                 :            :         uint32_t data_block_size;
    1276                 :            : 
    1277                 :     306703 :         _dif_sgl_init(&src_sgl, iovs, iovcnt);
    1278                 :     306703 :         _dif_sgl_init(&dst_sgl, bounce_iovs, bounce_iovcnt);
    1279                 :            : 
    1280   [ #  #  #  #  :     306703 :         data_block_size = ctx->block_size - ctx->md_size;
             #  #  #  # ]
    1281                 :            : 
    1282   [ +  -  #  # ]:     306703 :         if (!_dif_sgl_is_valid(&src_sgl, data_block_size * num_blocks) ||
    1283   [ +  +  #  # ]:     306703 :             !_dif_sgl_is_valid(&dst_sgl, ctx->block_size * num_blocks)) {
    1284                 :          3 :                 SPDK_ERRLOG("Size of iovec arrays are not valid.\n");
    1285                 :          3 :                 return -EINVAL;
    1286                 :            :         }
    1287                 :            : 
    1288   [ +  +  #  #  :     306700 :         if (_dif_is_disabled(ctx->dif_type)) {
                   #  # ]
    1289                 :          9 :                 dif_disable_insert_copy(&src_sgl, &dst_sgl, num_blocks, ctx);
    1290                 :          9 :                 return 0;
    1291                 :            :         }
    1292                 :            : 
    1293   [ +  +  +  - ]:     613289 :         if (_dif_sgl_is_bytes_multiple(&src_sgl, data_block_size) &&
    1294   [ #  #  #  # ]:     306598 :             _dif_sgl_is_bytes_multiple(&dst_sgl, ctx->block_size)) {
    1295                 :     306598 :                 dif_generate_copy(&src_sgl, &dst_sgl, num_blocks, ctx);
    1296                 :          0 :         } else {
    1297                 :         93 :                 dif_generate_copy_split(&src_sgl, &dst_sgl, num_blocks, ctx);
    1298                 :            :         }
    1299                 :            : 
    1300                 :     306691 :         return 0;
    1301                 :          0 : }
    1302                 :            : 
    1303                 :            : static int
    1304                 :       1389 : _dif_verify_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
    1305                 :            :                  uint32_t offset_blocks, const struct spdk_dif_ctx *ctx,
    1306                 :            :                  struct spdk_dif_error *err_blk)
    1307                 :            : {
    1308                 :            :         uint32_t data_block_size;
    1309                 :       1287 :         uint8_t *src, *dst;
    1310                 :            :         int rc;
    1311                 :       1389 :         uint64_t guard = 0;
    1312                 :            : 
    1313   [ #  #  #  #  :       1389 :         data_block_size = ctx->block_size - ctx->md_size;
             #  #  #  # ]
    1314                 :            : 
    1315                 :       1389 :         _dif_sgl_get_buf(src_sgl, &src, NULL);
    1316                 :       1389 :         _dif_sgl_get_buf(dst_sgl, &dst, NULL);
    1317                 :            : 
    1318   [ +  +  #  #  :       1389 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
    1319   [ #  #  #  # ]:        837 :                 guard = _dif_generate_guard_copy(ctx->guard_seed, dst, src, data_block_size,
    1320   [ #  #  #  # ]:        837 :                                                  ctx->dif_pi_format);
    1321         [ #  # ]:        837 :                 guard = _dif_generate_guard(guard, src + data_block_size,
    1322   [ #  #  #  #  :        837 :                                             ctx->guard_interval - data_block_size, ctx->dif_pi_format);
             #  #  #  # ]
    1323                 :          0 :         } else {
    1324   [ -  +  -  + ]:        552 :                 memcpy(dst, src, data_block_size);
    1325                 :            :         }
    1326                 :            : 
    1327   [ #  #  #  #  :       1389 :         rc = _dif_verify(src + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
                   #  # ]
    1328         [ +  + ]:       1389 :         if (rc != 0) {
    1329                 :         81 :                 return rc;
    1330                 :            :         }
    1331                 :            : 
    1332   [ #  #  #  # ]:       1308 :         _dif_sgl_advance(src_sgl, ctx->block_size);
    1333                 :       1308 :         _dif_sgl_advance(dst_sgl, data_block_size);
    1334                 :            : 
    1335                 :       1308 :         return 0;
    1336                 :          0 : }
    1337                 :            : 
    1338                 :            : static int
    1339                 :        195 : dif_verify_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
    1340                 :            :                 uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
    1341                 :            :                 struct spdk_dif_error *err_blk)
    1342                 :            : {
    1343                 :            :         uint32_t offset_blocks;
    1344                 :            :         int rc;
    1345                 :            : 
    1346         [ +  + ]:       1503 :         for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
    1347                 :       1389 :                 rc = _dif_verify_copy(src_sgl, dst_sgl, offset_blocks, ctx, err_blk);
    1348         [ +  + ]:       1389 :                 if (rc != 0) {
    1349                 :         81 :                         return rc;
    1350                 :            :                 }
    1351                 :          0 :         }
    1352                 :            : 
    1353                 :        114 :         return 0;
    1354                 :          0 : }
    1355                 :            : 
    1356                 :            : static int
    1357                 :        201 : _dif_verify_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
    1358                 :            :                        uint32_t offset_blocks, const struct spdk_dif_ctx *ctx,
    1359                 :            :                        struct spdk_dif_error *err_blk)
    1360                 :            : {
    1361                 :            :         uint32_t data_block_size;
    1362                 :        201 :         uint64_t guard = 0;
    1363                 :        201 :         struct spdk_dif dif = {};
    1364                 :            : 
    1365   [ #  #  #  #  :        201 :         data_block_size = ctx->block_size - ctx->md_size;
             #  #  #  # ]
    1366                 :            : 
    1367   [ +  -  #  #  :        201 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
    1368   [ #  #  #  # ]:        201 :                 guard = _dif_generate_guard_copy_split(ctx->guard_seed, dst_sgl, src_sgl,
    1369   [ #  #  #  # ]:        201 :                                                        data_block_size, ctx->dif_pi_format);
    1370                 :        201 :                 guard = dif_generate_guard_split(guard, src_sgl, data_block_size,
    1371   [ #  #  #  # ]:        201 :                                                  ctx->guard_interval - data_block_size, ctx);
    1372                 :          0 :         } else {
    1373                 :          0 :                 _data_copy_split(dst_sgl, src_sgl, data_block_size);
    1374   [ #  #  #  # ]:          0 :                 _dif_sgl_advance(src_sgl, ctx->guard_interval - data_block_size);
    1375                 :            :         }
    1376                 :            : 
    1377                 :        201 :         dif_load_split(src_sgl, &dif, ctx);
    1378                 :            : 
    1379                 :        201 :         return _dif_verify(&dif, guard, offset_blocks, ctx, err_blk);
    1380                 :            : }
    1381                 :            : 
    1382                 :            : static int
    1383                 :         93 : dif_verify_copy_split(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
    1384                 :            :                       uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
    1385                 :            :                       struct spdk_dif_error *err_blk)
    1386                 :            : {
    1387                 :            :         uint32_t offset_blocks;
    1388                 :            :         int rc;
    1389                 :            : 
    1390         [ +  + ]:        222 :         for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
    1391                 :        201 :                 rc = _dif_verify_copy_split(src_sgl, dst_sgl, offset_blocks, ctx, err_blk);
    1392         [ +  + ]:        201 :                 if (rc != 0) {
    1393                 :         72 :                         return rc;
    1394                 :            :                 }
    1395                 :          0 :         }
    1396                 :            : 
    1397                 :         21 :         return 0;
    1398                 :          0 : }
    1399                 :            : 
    1400                 :            : static void
    1401                 :         36 : _dif_disable_strip_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
    1402                 :            :                         const struct spdk_dif_ctx *ctx)
    1403                 :            : {
    1404                 :         36 :         uint32_t offset = 0, src_len, dst_len, buf_len, data_block_size;
    1405                 :         36 :         uint8_t *src, *dst;
    1406                 :            : 
    1407   [ #  #  #  #  :         36 :         data_block_size = ctx->block_size - ctx->md_size;
             #  #  #  # ]
    1408                 :            : 
    1409         [ +  + ]:         96 :         while (offset < data_block_size) {
    1410                 :         60 :                 _dif_sgl_get_buf(src_sgl, &src, &src_len);
    1411                 :         60 :                 _dif_sgl_get_buf(dst_sgl, &dst, &dst_len);
    1412         [ #  # ]:         60 :                 buf_len = spdk_min(src_len, dst_len);
    1413         [ #  # ]:         60 :                 buf_len = spdk_min(buf_len, data_block_size - offset);
    1414                 :            : 
    1415   [ -  +  -  + ]:         60 :                 memcpy(dst, src, buf_len);
    1416                 :            : 
    1417                 :         60 :                 _dif_sgl_advance(src_sgl, buf_len);
    1418                 :         60 :                 _dif_sgl_advance(dst_sgl, buf_len);
    1419                 :         60 :                 offset += buf_len;
    1420                 :            :         }
    1421                 :            : 
    1422   [ #  #  #  # ]:         36 :         _dif_sgl_advance(src_sgl, ctx->md_size);
    1423                 :         36 : }
    1424                 :            : 
    1425                 :            : static void
    1426                 :          9 : dif_disable_strip_copy(struct _dif_sgl *src_sgl, struct _dif_sgl *dst_sgl,
    1427                 :            :                        uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
    1428                 :            : {
    1429                 :            :         uint32_t offset_blocks;
    1430                 :            : 
    1431         [ +  + ]:         45 :         for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
    1432                 :         36 :                 _dif_disable_strip_copy(src_sgl, dst_sgl, ctx);
    1433                 :          0 :         }
    1434                 :          9 : }
    1435                 :            : 
    1436                 :            : int
    1437                 :        297 : spdk_dif_verify_copy(struct iovec *iovs, int iovcnt, struct iovec *bounce_iovs,
    1438                 :            :                      int bounce_iovcnt, uint32_t num_blocks,
    1439                 :            :                      const struct spdk_dif_ctx *ctx,
    1440                 :            :                      struct spdk_dif_error *err_blk)
    1441                 :            : {
    1442                 :        279 :         struct _dif_sgl src_sgl, dst_sgl;
    1443                 :            :         uint32_t data_block_size;
    1444                 :            : 
    1445                 :        297 :         _dif_sgl_init(&src_sgl, bounce_iovs, bounce_iovcnt);
    1446                 :        297 :         _dif_sgl_init(&dst_sgl, iovs, iovcnt);
    1447                 :            : 
    1448   [ #  #  #  #  :        297 :         data_block_size = ctx->block_size - ctx->md_size;
             #  #  #  # ]
    1449                 :            : 
    1450   [ +  -  #  # ]:        297 :         if (!_dif_sgl_is_valid(&dst_sgl, data_block_size * num_blocks) ||
    1451   [ -  +  #  # ]:        297 :             !_dif_sgl_is_valid(&src_sgl, ctx->block_size * num_blocks)) {
    1452                 :          0 :                 SPDK_ERRLOG("Size of iovec arrays are not valid\n");
    1453                 :          0 :                 return -EINVAL;
    1454                 :            :         }
    1455                 :            : 
    1456   [ +  +  #  #  :        297 :         if (_dif_is_disabled(ctx->dif_type)) {
                   #  # ]
    1457                 :          9 :                 dif_disable_strip_copy(&src_sgl, &dst_sgl, num_blocks, ctx);
    1458                 :          9 :                 return 0;
    1459                 :            :         }
    1460                 :            : 
    1461   [ +  +  +  - ]:        483 :         if (_dif_sgl_is_bytes_multiple(&dst_sgl, data_block_size) &&
    1462   [ #  #  #  # ]:        195 :             _dif_sgl_is_bytes_multiple(&src_sgl, ctx->block_size)) {
    1463                 :        195 :                 return dif_verify_copy(&src_sgl, &dst_sgl, num_blocks, ctx, err_blk);
    1464                 :            :         } else {
    1465                 :         93 :                 return dif_verify_copy_split(&src_sgl, &dst_sgl, num_blocks, ctx, err_blk);
    1466                 :            :         }
    1467                 :          0 : }
    1468                 :            : 
    1469                 :            : static void
    1470                 :        720 : _bit_flip(uint8_t *buf, uint32_t flip_bit)
    1471                 :            : {
    1472                 :            :         uint8_t byte;
    1473                 :            : 
    1474         [ #  # ]:        720 :         byte = *buf;
    1475   [ -  +  #  # ]:        720 :         byte ^= 1 << flip_bit;
    1476         [ #  # ]:        720 :         *buf = byte;
    1477                 :        720 : }
    1478                 :            : 
    1479                 :            : static int
    1480                 :        720 : _dif_inject_error(struct _dif_sgl *sgl,
    1481                 :            :                   uint32_t block_size, uint32_t num_blocks,
    1482                 :            :                   uint32_t inject_offset_blocks,
    1483                 :            :                   uint32_t inject_offset_bytes,
    1484                 :            :                   uint32_t inject_offset_bits)
    1485                 :            : {
    1486                 :        720 :         uint32_t offset_in_block, buf_len;
    1487                 :        720 :         uint8_t *buf;
    1488                 :            : 
    1489                 :        720 :         _dif_sgl_advance(sgl, block_size * inject_offset_blocks);
    1490                 :            : 
    1491                 :        720 :         offset_in_block = 0;
    1492                 :            : 
    1493         [ +  - ]:        991 :         while (offset_in_block < block_size) {
    1494                 :        991 :                 _dif_sgl_get_buf(sgl, &buf, &buf_len);
    1495         [ #  # ]:        991 :                 buf_len = spdk_min(buf_len, block_size - offset_in_block);
    1496                 :            : 
    1497   [ +  -  #  # ]:        991 :                 if (inject_offset_bytes >= offset_in_block &&
    1498         [ +  + ]:        991 :                     inject_offset_bytes < offset_in_block + buf_len) {
    1499         [ #  # ]:        720 :                         buf += inject_offset_bytes - offset_in_block;
    1500                 :        720 :                         _bit_flip(buf, inject_offset_bits);
    1501                 :        720 :                         return 0;
    1502                 :            :                 }
    1503                 :            : 
    1504                 :        271 :                 _dif_sgl_advance(sgl, buf_len);
    1505                 :        271 :                 offset_in_block += buf_len;
    1506                 :            :         }
    1507                 :            : 
    1508                 :          0 :         return -1;
    1509                 :          0 : }
    1510                 :            : 
    1511                 :            : static int
    1512                 :        720 : dif_inject_error(struct _dif_sgl *sgl, uint32_t block_size, uint32_t num_blocks,
    1513                 :            :                  uint32_t start_inject_bytes, uint32_t inject_range_bytes,
    1514                 :            :                  uint32_t *inject_offset)
    1515                 :            : {
    1516                 :            :         uint32_t inject_offset_blocks, inject_offset_bytes, inject_offset_bits;
    1517                 :            :         uint32_t offset_blocks;
    1518                 :            :         int rc;
    1519                 :            : 
    1520                 :        720 :         srand(time(0));
    1521                 :            : 
    1522         [ -  + ]:        720 :         inject_offset_blocks = rand() % num_blocks;
    1523         [ -  + ]:        720 :         inject_offset_bytes = start_inject_bytes + (rand() % inject_range_bytes);
    1524         [ #  # ]:        720 :         inject_offset_bits = rand() % 8;
    1525                 :            : 
    1526         [ +  - ]:       2088 :         for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
    1527         [ +  + ]:       2088 :                 if (offset_blocks == inject_offset_blocks) {
    1528                 :        720 :                         rc = _dif_inject_error(sgl, block_size, num_blocks,
    1529                 :          0 :                                                inject_offset_blocks,
    1530                 :          0 :                                                inject_offset_bytes,
    1531                 :          0 :                                                inject_offset_bits);
    1532         [ +  - ]:        720 :                         if (rc == 0) {
    1533         [ #  # ]:        720 :                                 *inject_offset = inject_offset_blocks;
    1534                 :          0 :                         }
    1535                 :        720 :                         return rc;
    1536                 :            :                 }
    1537                 :          0 :         }
    1538                 :            : 
    1539                 :          0 :         return -1;
    1540                 :          0 : }
    1541                 :            : 
    1542                 :            : int
    1543                 :        576 : spdk_dif_inject_error(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
    1544                 :            :                       const struct spdk_dif_ctx *ctx, uint32_t inject_flags,
    1545                 :            :                       uint32_t *inject_offset)
    1546                 :            : {
    1547                 :        576 :         struct _dif_sgl sgl;
    1548                 :            :         int rc;
    1549                 :            : 
    1550                 :        576 :         _dif_sgl_init(&sgl, iovs, iovcnt);
    1551                 :            : 
    1552   [ -  +  #  #  :        576 :         if (!_dif_sgl_is_valid(&sgl, ctx->block_size * num_blocks)) {
                   #  # ]
    1553                 :          0 :                 SPDK_ERRLOG("Size of iovec array is not valid.\n");
    1554                 :          0 :                 return -EINVAL;
    1555                 :            :         }
    1556                 :            : 
    1557         [ +  + ]:        576 :         if (inject_flags & SPDK_DIF_REFTAG_ERROR) {
    1558   [ #  #  #  # ]:        192 :                 rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
    1559   [ #  #  #  #  :        144 :                                       ctx->guard_interval + _dif_reftag_offset(ctx->dif_pi_format),
             #  #  #  # ]
    1560   [ #  #  #  # ]:        144 :                                       _dif_reftag_size(ctx->dif_pi_format),
    1561                 :          0 :                                       inject_offset);
    1562         [ -  + ]:        144 :                 if (rc != 0) {
    1563                 :          0 :                         SPDK_ERRLOG("Failed to inject error to Reference Tag.\n");
    1564                 :          0 :                         return rc;
    1565                 :            :                 }
    1566                 :          0 :         }
    1567                 :            : 
    1568         [ +  + ]:        576 :         if (inject_flags & SPDK_DIF_APPTAG_ERROR) {
    1569   [ #  #  #  # ]:        192 :                 rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
    1570   [ #  #  #  #  :        144 :                                       ctx->guard_interval + _dif_apptag_offset(ctx->dif_pi_format),
             #  #  #  # ]
    1571                 :        144 :                                       _dif_apptag_size(),
    1572                 :          0 :                                       inject_offset);
    1573         [ -  + ]:        144 :                 if (rc != 0) {
    1574                 :          0 :                         SPDK_ERRLOG("Failed to inject error to Application Tag.\n");
    1575                 :          0 :                         return rc;
    1576                 :            :                 }
    1577                 :          0 :         }
    1578         [ +  + ]:        576 :         if (inject_flags & SPDK_DIF_GUARD_ERROR) {
    1579   [ #  #  #  # ]:        144 :                 rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
    1580   [ #  #  #  # ]:        144 :                                       ctx->guard_interval,
    1581   [ #  #  #  # ]:        144 :                                       _dif_guard_size(ctx->dif_pi_format),
    1582                 :          0 :                                       inject_offset);
    1583         [ -  + ]:        144 :                 if (rc != 0) {
    1584                 :          0 :                         SPDK_ERRLOG("Failed to inject error to Guard.\n");
    1585                 :          0 :                         return rc;
    1586                 :            :                 }
    1587                 :          0 :         }
    1588                 :            : 
    1589         [ +  + ]:        576 :         if (inject_flags & SPDK_DIF_DATA_ERROR) {
    1590                 :            :                 /* If the DIF information is contained within the last 8/16 bytes of
    1591                 :            :                  * metadata (depending on the PI format), then the CRC covers all metadata
    1592                 :            :                  * bytes up to but excluding the last 8/16 bytes. But error injection does not
    1593                 :            :                  * cover these metadata because classification is not determined yet.
    1594                 :            :                  *
    1595                 :            :                  * Note: Error injection to data block is expected to be detected as
    1596                 :            :                  * guard error.
    1597                 :            :                  */
    1598   [ #  #  #  # ]:        144 :                 rc = dif_inject_error(&sgl, ctx->block_size, num_blocks,
    1599                 :            :                                       0,
    1600   [ #  #  #  #  :        144 :                                       ctx->block_size - ctx->md_size,
             #  #  #  # ]
    1601                 :          0 :                                       inject_offset);
    1602         [ -  + ]:        144 :                 if (rc != 0) {
    1603                 :          0 :                         SPDK_ERRLOG("Failed to inject error to data block.\n");
    1604                 :          0 :                         return rc;
    1605                 :            :                 }
    1606                 :          0 :         }
    1607                 :            : 
    1608                 :        576 :         return 0;
    1609                 :          0 : }
    1610                 :            : 
    1611                 :            : static void
    1612                 :        342 : dix_generate(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
    1613                 :            :              uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
    1614                 :            : {
    1615                 :        342 :         uint32_t offset_blocks = 0;
    1616                 :        238 :         uint8_t *data_buf, *md_buf;
    1617                 :            :         uint64_t guard;
    1618                 :            : 
    1619         [ +  + ]:       4038 :         while (offset_blocks < num_blocks) {
    1620                 :       3696 :                 _dif_sgl_get_buf(data_sgl, &data_buf, NULL);
    1621                 :       3696 :                 _dif_sgl_get_buf(md_sgl, &md_buf, NULL);
    1622                 :            : 
    1623                 :       3696 :                 guard = 0;
    1624   [ +  +  #  #  :       3696 :                 if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
    1625   [ #  #  #  #  :       3090 :                         guard = _dif_generate_guard(ctx->guard_seed, data_buf, ctx->block_size,
             #  #  #  # ]
    1626   [ #  #  #  # ]:       3090 :                                                     ctx->dif_pi_format);
    1627   [ #  #  #  # ]:       3090 :                         guard = _dif_generate_guard(guard, md_buf, ctx->guard_interval,
    1628   [ #  #  #  # ]:       3090 :                                                     ctx->dif_pi_format);
    1629                 :          0 :                 }
    1630                 :            : 
    1631   [ #  #  #  #  :       3696 :                 _dif_generate(md_buf + ctx->guard_interval, guard, offset_blocks, ctx);
                   #  # ]
    1632                 :            : 
    1633   [ #  #  #  # ]:       3696 :                 _dif_sgl_advance(data_sgl, ctx->block_size);
    1634   [ #  #  #  # ]:       3696 :                 _dif_sgl_advance(md_sgl, ctx->md_size);
    1635                 :       3696 :                 offset_blocks++;
    1636                 :            :         }
    1637                 :        342 : }
    1638                 :            : 
    1639                 :            : static void
    1640                 :        225 : _dix_generate_split(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
    1641                 :            :                     uint32_t offset_blocks, const struct spdk_dif_ctx *ctx)
    1642                 :            : {
    1643                 :        225 :         uint32_t offset_in_block, data_buf_len;
    1644                 :        225 :         uint8_t *data_buf, *md_buf;
    1645                 :        225 :         uint64_t guard = 0;
    1646                 :            : 
    1647                 :        225 :         _dif_sgl_get_buf(md_sgl, &md_buf, NULL);
    1648                 :            : 
    1649   [ +  -  #  #  :        225 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
    1650   [ #  #  #  # ]:        225 :                 guard = ctx->guard_seed;
    1651                 :          0 :         }
    1652                 :        225 :         offset_in_block = 0;
    1653                 :            : 
    1654   [ +  +  #  #  :        693 :         while (offset_in_block < ctx->block_size) {
                   #  # ]
    1655                 :        468 :                 _dif_sgl_get_buf(data_sgl, &data_buf, &data_buf_len);
    1656   [ #  #  #  #  :        468 :                 data_buf_len = spdk_min(data_buf_len, ctx->block_size - offset_in_block);
          #  #  #  #  #  
                      # ]
    1657                 :            : 
    1658   [ +  -  #  #  :        468 :                 if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
    1659                 :        468 :                         guard = _dif_generate_guard(guard, data_buf, data_buf_len,
    1660   [ #  #  #  # ]:        468 :                                                     ctx->dif_pi_format);
    1661                 :          0 :                 }
    1662                 :            : 
    1663                 :        468 :                 _dif_sgl_advance(data_sgl, data_buf_len);
    1664                 :        468 :                 offset_in_block += data_buf_len;
    1665                 :            :         }
    1666                 :            : 
    1667   [ +  -  #  #  :        225 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
    1668   [ #  #  #  # ]:        225 :                 guard = _dif_generate_guard(guard, md_buf, ctx->guard_interval,
    1669   [ #  #  #  # ]:        225 :                                             ctx->dif_pi_format);
    1670                 :          0 :         }
    1671                 :            : 
    1672   [ #  #  #  # ]:        225 :         _dif_sgl_advance(md_sgl, ctx->md_size);
    1673                 :            : 
    1674   [ #  #  #  #  :        225 :         _dif_generate(md_buf + ctx->guard_interval, guard, offset_blocks, ctx);
                   #  # ]
    1675                 :        225 : }
    1676                 :            : 
    1677                 :            : static void
    1678                 :         99 : dix_generate_split(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
    1679                 :            :                    uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
    1680                 :            : {
    1681                 :            :         uint32_t offset_blocks;
    1682                 :            : 
    1683         [ +  + ]:        324 :         for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
    1684                 :        225 :                 _dix_generate_split(data_sgl, md_sgl, offset_blocks, ctx);
    1685                 :          0 :         }
    1686                 :         99 : }
    1687                 :            : 
    1688                 :            : int
    1689                 :      15673 : spdk_dix_generate(struct iovec *iovs, int iovcnt, struct iovec *md_iov,
    1690                 :            :                   uint32_t num_blocks, const struct spdk_dif_ctx *ctx)
    1691                 :            : {
    1692                 :      15569 :         struct _dif_sgl data_sgl, md_sgl;
    1693                 :            : 
    1694                 :      15673 :         _dif_sgl_init(&data_sgl, iovs, iovcnt);
    1695                 :      15673 :         _dif_sgl_init(&md_sgl, md_iov, 1);
    1696                 :            : 
    1697   [ +  -  #  #  :      15673 :         if (!_dif_sgl_is_valid(&data_sgl, ctx->block_size * num_blocks) ||
             #  #  #  # ]
    1698   [ -  +  #  # ]:      15673 :             !_dif_sgl_is_valid(&md_sgl, ctx->md_size * num_blocks)) {
    1699                 :          0 :                 SPDK_ERRLOG("Size of iovec array is not valid.\n");
    1700                 :          0 :                 return -EINVAL;
    1701                 :            :         }
    1702                 :            : 
    1703   [ +  +  #  #  :      15673 :         if (_dif_is_disabled(ctx->dif_type)) {
                   #  # ]
    1704                 :      15232 :                 return 0;
    1705                 :            :         }
    1706                 :            : 
    1707   [ +  +  #  #  :        441 :         if (_dif_sgl_is_bytes_multiple(&data_sgl, ctx->block_size)) {
                   #  # ]
    1708                 :        342 :                 dix_generate(&data_sgl, &md_sgl, num_blocks, ctx);
    1709                 :          0 :         } else {
    1710                 :         99 :                 dix_generate_split(&data_sgl, &md_sgl, num_blocks, ctx);
    1711                 :            :         }
    1712                 :            : 
    1713                 :        441 :         return 0;
    1714                 :          0 : }
    1715                 :            : 
    1716                 :            : static int
    1717                 :     265765 : dix_verify(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
    1718                 :            :            uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
    1719                 :            :            struct spdk_dif_error *err_blk)
    1720                 :            : {
    1721                 :     265765 :         uint32_t offset_blocks = 0;
    1722                 :      95869 :         uint8_t *data_buf, *md_buf;
    1723                 :            :         uint64_t guard;
    1724                 :            :         int rc;
    1725                 :            : 
    1726         [ +  + ]:    2392845 :         while (offset_blocks < num_blocks) {
    1727                 :    2127179 :                 _dif_sgl_get_buf(data_sgl, &data_buf, NULL);
    1728                 :    2127179 :                 _dif_sgl_get_buf(md_sgl, &md_buf, NULL);
    1729                 :            : 
    1730                 :    2127179 :                 guard = 0;
    1731   [ +  +  #  #  :    2127179 :                 if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
    1732   [ #  #  #  #  :    2126513 :                         guard = _dif_generate_guard(ctx->guard_seed, data_buf, ctx->block_size,
             #  #  #  # ]
    1733   [ #  #  #  # ]:    2126513 :                                                     ctx->dif_pi_format);
    1734   [ #  #  #  # ]:    2126513 :                         guard = _dif_generate_guard(guard, md_buf, ctx->guard_interval,
    1735   [ #  #  #  # ]:    2126513 :                                                     ctx->dif_pi_format);
    1736                 :          0 :                 }
    1737                 :            : 
    1738   [ #  #  #  #  :    2127179 :                 rc = _dif_verify(md_buf + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
                   #  # ]
    1739         [ +  + ]:    2127179 :                 if (rc != 0) {
    1740                 :         99 :                         return rc;
    1741                 :            :                 }
    1742                 :            : 
    1743   [ #  #  #  # ]:    2127080 :                 _dif_sgl_advance(data_sgl, ctx->block_size);
    1744   [ #  #  #  # ]:    2127080 :                 _dif_sgl_advance(md_sgl, ctx->md_size);
    1745                 :    2127080 :                 offset_blocks++;
    1746                 :            :         }
    1747                 :            : 
    1748                 :     265666 :         return 0;
    1749                 :          0 : }
    1750                 :            : 
    1751                 :            : static int
    1752                 :        225 : _dix_verify_split(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
    1753                 :            :                   uint32_t offset_blocks, const struct spdk_dif_ctx *ctx,
    1754                 :            :                   struct spdk_dif_error *err_blk)
    1755                 :            : {
    1756                 :        225 :         uint32_t offset_in_block, data_buf_len;
    1757                 :        225 :         uint8_t *data_buf, *md_buf;
    1758                 :        225 :         uint64_t guard = 0;
    1759                 :            : 
    1760                 :        225 :         _dif_sgl_get_buf(md_sgl, &md_buf, NULL);
    1761                 :            : 
    1762   [ +  -  #  #  :        225 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
    1763   [ #  #  #  # ]:        225 :                 guard = ctx->guard_seed;
    1764                 :          0 :         }
    1765                 :        225 :         offset_in_block = 0;
    1766                 :            : 
    1767   [ +  +  #  #  :        693 :         while (offset_in_block < ctx->block_size) {
                   #  # ]
    1768                 :        468 :                 _dif_sgl_get_buf(data_sgl, &data_buf, &data_buf_len);
    1769   [ #  #  #  #  :        468 :                 data_buf_len = spdk_min(data_buf_len, ctx->block_size - offset_in_block);
          #  #  #  #  #  
                      # ]
    1770                 :            : 
    1771   [ +  -  #  #  :        468 :                 if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
    1772                 :        468 :                         guard = _dif_generate_guard(guard, data_buf, data_buf_len,
    1773   [ #  #  #  # ]:        468 :                                                     ctx->dif_pi_format);
    1774                 :          0 :                 }
    1775                 :            : 
    1776                 :        468 :                 _dif_sgl_advance(data_sgl, data_buf_len);
    1777                 :        468 :                 offset_in_block += data_buf_len;
    1778                 :            :         }
    1779                 :            : 
    1780   [ +  -  #  #  :        225 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
    1781   [ #  #  #  # ]:        225 :                 guard = _dif_generate_guard(guard, md_buf, ctx->guard_interval,
    1782   [ #  #  #  # ]:        225 :                                             ctx->dif_pi_format);
    1783                 :          0 :         }
    1784                 :            : 
    1785   [ #  #  #  # ]:        225 :         _dif_sgl_advance(md_sgl, ctx->md_size);
    1786                 :            : 
    1787   [ #  #  #  #  :        225 :         return _dif_verify(md_buf + ctx->guard_interval, guard, offset_blocks, ctx, err_blk);
                   #  # ]
    1788                 :            : }
    1789                 :            : 
    1790                 :            : static int
    1791                 :         99 : dix_verify_split(struct _dif_sgl *data_sgl, struct _dif_sgl *md_sgl,
    1792                 :            :                  uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
    1793                 :            :                  struct spdk_dif_error *err_blk)
    1794                 :            : {
    1795                 :            :         uint32_t offset_blocks;
    1796                 :            :         int rc;
    1797                 :            : 
    1798         [ +  + ]:        252 :         for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
    1799                 :        225 :                 rc = _dix_verify_split(data_sgl, md_sgl, offset_blocks, ctx, err_blk);
    1800         [ +  + ]:        225 :                 if (rc != 0) {
    1801                 :         72 :                         return rc;
    1802                 :            :                 }
    1803                 :          0 :         }
    1804                 :            : 
    1805                 :         27 :         return 0;
    1806                 :          0 : }
    1807                 :            : 
    1808                 :            : int
    1809                 :     435176 : spdk_dix_verify(struct iovec *iovs, int iovcnt, struct iovec *md_iov,
    1810                 :            :                 uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
    1811                 :            :                 struct spdk_dif_error *err_blk)
    1812                 :            : {
    1813                 :     265280 :         struct _dif_sgl data_sgl, md_sgl;
    1814                 :            : 
    1815   [ -  +  #  #  :     435176 :         if (md_iov->iov_base == NULL) {
                   #  # ]
    1816                 :          0 :                 SPDK_ERRLOG("Metadata buffer is NULL.\n");
    1817                 :          0 :                 return -EINVAL;
    1818                 :            :         }
    1819                 :            : 
    1820                 :     435176 :         _dif_sgl_init(&data_sgl, iovs, iovcnt);
    1821                 :     435176 :         _dif_sgl_init(&md_sgl, md_iov, 1);
    1822                 :            : 
    1823   [ +  -  #  #  :     435176 :         if (!_dif_sgl_is_valid(&data_sgl, ctx->block_size * num_blocks) ||
             #  #  #  # ]
    1824   [ -  +  #  # ]:     435176 :             !_dif_sgl_is_valid(&md_sgl, ctx->md_size * num_blocks)) {
    1825                 :          0 :                 SPDK_ERRLOG("Size of iovec array is not valid.\n");
    1826                 :          0 :                 return -EINVAL;
    1827                 :            :         }
    1828                 :            : 
    1829   [ +  +  #  #  :     435176 :         if (_dif_is_disabled(ctx->dif_type)) {
                   #  # ]
    1830                 :     169312 :                 return 0;
    1831                 :            :         }
    1832                 :            : 
    1833   [ +  +  #  #  :     265864 :         if (_dif_sgl_is_bytes_multiple(&data_sgl, ctx->block_size)) {
                   #  # ]
    1834                 :     265765 :                 return dix_verify(&data_sgl, &md_sgl, num_blocks, ctx, err_blk);
    1835                 :            :         } else {
    1836                 :         99 :                 return dix_verify_split(&data_sgl, &md_sgl, num_blocks, ctx, err_blk);
    1837                 :            :         }
    1838                 :          0 : }
    1839                 :            : 
    1840                 :            : int
    1841                 :        144 : spdk_dix_inject_error(struct iovec *iovs, int iovcnt, struct iovec *md_iov,
    1842                 :            :                       uint32_t num_blocks, const struct spdk_dif_ctx *ctx,
    1843                 :            :                       uint32_t inject_flags, uint32_t *inject_offset)
    1844                 :            : {
    1845                 :        144 :         struct _dif_sgl data_sgl, md_sgl;
    1846                 :            :         int rc;
    1847                 :            : 
    1848                 :        144 :         _dif_sgl_init(&data_sgl, iovs, iovcnt);
    1849                 :        144 :         _dif_sgl_init(&md_sgl, md_iov, 1);
    1850                 :            : 
    1851   [ +  -  #  #  :        144 :         if (!_dif_sgl_is_valid(&data_sgl, ctx->block_size * num_blocks) ||
             #  #  #  # ]
    1852   [ -  +  #  # ]:        144 :             !_dif_sgl_is_valid(&md_sgl, ctx->md_size * num_blocks)) {
    1853                 :          0 :                 SPDK_ERRLOG("Size of iovec array is not valid.\n");
    1854                 :          0 :                 return -EINVAL;
    1855                 :            :         }
    1856                 :            : 
    1857         [ +  + ]:        144 :         if (inject_flags & SPDK_DIF_REFTAG_ERROR) {
    1858   [ #  #  #  # ]:         48 :                 rc = dif_inject_error(&md_sgl, ctx->md_size, num_blocks,
    1859   [ #  #  #  #  :         36 :                                       ctx->guard_interval + _dif_reftag_offset(ctx->dif_pi_format),
             #  #  #  # ]
    1860   [ #  #  #  # ]:         36 :                                       _dif_reftag_size(ctx->dif_pi_format),
    1861                 :          0 :                                       inject_offset);
    1862         [ -  + ]:         36 :                 if (rc != 0) {
    1863                 :          0 :                         SPDK_ERRLOG("Failed to inject error to Reference Tag.\n");
    1864                 :          0 :                         return rc;
    1865                 :            :                 }
    1866                 :          0 :         }
    1867                 :            : 
    1868         [ +  + ]:        144 :         if (inject_flags & SPDK_DIF_APPTAG_ERROR) {
    1869   [ #  #  #  # ]:         48 :                 rc = dif_inject_error(&md_sgl, ctx->md_size, num_blocks,
    1870   [ #  #  #  #  :         36 :                                       ctx->guard_interval + _dif_apptag_offset(ctx->dif_pi_format),
             #  #  #  # ]
    1871                 :         36 :                                       _dif_apptag_size(),
    1872                 :          0 :                                       inject_offset);
    1873         [ -  + ]:         36 :                 if (rc != 0) {
    1874                 :          0 :                         SPDK_ERRLOG("Failed to inject error to Application Tag.\n");
    1875                 :          0 :                         return rc;
    1876                 :            :                 }
    1877                 :          0 :         }
    1878                 :            : 
    1879         [ +  + ]:        144 :         if (inject_flags & SPDK_DIF_GUARD_ERROR) {
    1880   [ #  #  #  # ]:         36 :                 rc = dif_inject_error(&md_sgl, ctx->md_size, num_blocks,
    1881   [ #  #  #  # ]:         36 :                                       ctx->guard_interval,
    1882   [ #  #  #  # ]:         36 :                                       _dif_guard_size(ctx->dif_pi_format),
    1883                 :          0 :                                       inject_offset);
    1884         [ -  + ]:         36 :                 if (rc != 0) {
    1885                 :          0 :                         SPDK_ERRLOG("Failed to inject error to Guard.\n");
    1886                 :          0 :                         return rc;
    1887                 :            :                 }
    1888                 :          0 :         }
    1889                 :            : 
    1890         [ +  + ]:        144 :         if (inject_flags & SPDK_DIF_DATA_ERROR) {
    1891                 :            :                 /* Note: Error injection to data block is expected to be detected
    1892                 :            :                  * as guard error.
    1893                 :            :                  */
    1894   [ #  #  #  # ]:         36 :                 rc = dif_inject_error(&data_sgl, ctx->block_size, num_blocks,
    1895                 :            :                                       0,
    1896   [ #  #  #  # ]:         36 :                                       ctx->block_size,
    1897                 :          0 :                                       inject_offset);
    1898         [ -  + ]:         36 :                 if (rc != 0) {
    1899                 :          0 :                         SPDK_ERRLOG("Failed to inject error to Guard.\n");
    1900                 :          0 :                         return rc;
    1901                 :            :                 }
    1902                 :          0 :         }
    1903                 :            : 
    1904                 :        144 :         return 0;
    1905                 :          0 : }
    1906                 :            : 
    1907                 :            : static uint32_t
    1908                 :  105538493 : _to_next_boundary(uint32_t offset, uint32_t boundary)
    1909                 :            : {
    1910         [ -  + ]:  105538493 :         return boundary - (offset % boundary);
    1911                 :            : }
    1912                 :            : 
    1913                 :            : static uint32_t
    1914                 :    9345575 : _to_size_with_md(uint32_t size, uint32_t data_block_size, uint32_t block_size)
    1915                 :            : {
    1916   [ -  +  -  + ]:    9345575 :         return (size / data_block_size) * block_size + (size % data_block_size);
    1917                 :            : }
    1918                 :            : 
    1919                 :            : int
    1920                 :    1917306 : spdk_dif_set_md_interleave_iovs(struct iovec *iovs, int iovcnt,
    1921                 :            :                                 struct iovec *buf_iovs, int buf_iovcnt,
    1922                 :            :                                 uint32_t data_offset, uint32_t data_len,
    1923                 :            :                                 uint32_t *_mapped_len,
    1924                 :            :                                 const struct spdk_dif_ctx *ctx)
    1925                 :            : {
    1926                 :            :         uint32_t data_block_size, data_unalign, buf_len, buf_offset, len;
    1927                 :        114 :         struct _dif_sgl dif_sgl;
    1928                 :        114 :         struct _dif_sgl buf_sgl;
    1929                 :            : 
    1930   [ +  -  +  -  :    1917306 :         if (iovs == NULL || iovcnt == 0 || buf_iovs == NULL || buf_iovcnt == 0) {
             +  -  -  + ]
    1931                 :          0 :                 return -EINVAL;
    1932                 :            :         }
    1933                 :            : 
    1934   [ #  #  #  #  :    1917306 :         data_block_size = ctx->block_size - ctx->md_size;
             #  #  #  # ]
    1935                 :            : 
    1936   [ -  +  #  #  :    1917306 :         data_unalign = ctx->data_offset % data_block_size;
                   #  # ]
    1937                 :            : 
    1938                 :    1917306 :         buf_len = _to_size_with_md(data_unalign + data_offset + data_len, data_block_size,
    1939   [ #  #  #  # ]:    1917306 :                                    ctx->block_size);
    1940                 :    1917306 :         buf_len -= data_unalign;
    1941                 :            : 
    1942                 :    1917306 :         _dif_sgl_init(&dif_sgl, iovs, iovcnt);
    1943                 :    1917306 :         _dif_sgl_init(&buf_sgl, buf_iovs, buf_iovcnt);
    1944                 :            : 
    1945         [ +  + ]:    1917306 :         if (!_dif_sgl_is_valid(&buf_sgl, buf_len)) {
    1946                 :          3 :                 SPDK_ERRLOG("Buffer overflow will occur.\n");
    1947                 :          3 :                 return -ERANGE;
    1948                 :            :         }
    1949                 :            : 
    1950   [ #  #  #  # ]:    1917303 :         buf_offset = _to_size_with_md(data_unalign + data_offset, data_block_size, ctx->block_size);
    1951                 :    1917303 :         buf_offset -= data_unalign;
    1952                 :            : 
    1953                 :    1917303 :         _dif_sgl_advance(&buf_sgl, buf_offset);
    1954                 :            : 
    1955         [ +  + ]:   32688357 :         while (data_len != 0) {
    1956   [ +  +  #  #  :   31173236 :                 len = spdk_min(data_len, _to_next_boundary(ctx->data_offset + data_offset, data_block_size));
          #  #  #  #  #  
                      # ]
    1957         [ +  + ]:   31173236 :                 if (!_dif_sgl_append_split(&dif_sgl, &buf_sgl, len)) {
    1958                 :     402182 :                         break;
    1959                 :            :                 }
    1960   [ #  #  #  # ]:   30771054 :                 _dif_sgl_advance(&buf_sgl, ctx->md_size);
    1961                 :   30771054 :                 data_offset += len;
    1962                 :   30771054 :                 data_len -= len;
    1963                 :            :         }
    1964                 :            : 
    1965         [ +  - ]:    1917303 :         if (_mapped_len != NULL) {
    1966   [ #  #  #  # ]:    1917303 :                 *_mapped_len = dif_sgl.total_size;
    1967                 :          0 :         }
    1968                 :            : 
    1969   [ #  #  #  # ]:    1917303 :         return iovcnt - dif_sgl.iovcnt;
    1970                 :          0 : }
    1971                 :            : 
    1972                 :            : static int
    1973                 :    1170069 : _dif_sgl_setup_stream(struct _dif_sgl *sgl, uint32_t *_buf_offset, uint32_t *_buf_len,
    1974                 :            :                       uint32_t data_offset, uint32_t data_len,
    1975                 :            :                       const struct spdk_dif_ctx *ctx)
    1976                 :            : {
    1977                 :            :         uint32_t data_block_size, data_unalign, buf_len, buf_offset;
    1978                 :            : 
    1979   [ #  #  #  #  :    1170069 :         data_block_size = ctx->block_size - ctx->md_size;
             #  #  #  # ]
    1980                 :            : 
    1981   [ -  +  #  #  :    1170069 :         data_unalign = ctx->data_offset % data_block_size;
                   #  # ]
    1982                 :            : 
    1983                 :            :         /* If the last data block is complete, DIF of the data block is
    1984                 :            :          * inserted or verified in this turn.
    1985                 :            :          */
    1986                 :    1170069 :         buf_len = _to_size_with_md(data_unalign + data_offset + data_len, data_block_size,
    1987   [ #  #  #  # ]:    1170069 :                                    ctx->block_size);
    1988                 :    1170069 :         buf_len -= data_unalign;
    1989                 :            : 
    1990         [ +  + ]:    1170069 :         if (!_dif_sgl_is_valid(sgl, buf_len)) {
    1991                 :          9 :                 return -ERANGE;
    1992                 :            :         }
    1993                 :            : 
    1994   [ #  #  #  # ]:    1170060 :         buf_offset = _to_size_with_md(data_unalign + data_offset, data_block_size, ctx->block_size);
    1995                 :    1170060 :         buf_offset -= data_unalign;
    1996                 :            : 
    1997                 :    1170060 :         _dif_sgl_advance(sgl, buf_offset);
    1998                 :    1170060 :         buf_len -= buf_offset;
    1999                 :            : 
    2000                 :    1170060 :         buf_offset += data_unalign;
    2001                 :            : 
    2002         [ #  # ]:    1170060 :         *_buf_offset = buf_offset;
    2003         [ #  # ]:    1170060 :         *_buf_len = buf_len;
    2004                 :            : 
    2005                 :    1170060 :         return 0;
    2006                 :          0 : }
    2007                 :            : 
    2008                 :            : int
    2009                 :        147 : spdk_dif_generate_stream(struct iovec *iovs, int iovcnt,
    2010                 :            :                          uint32_t data_offset, uint32_t data_len,
    2011                 :            :                          struct spdk_dif_ctx *ctx)
    2012                 :            : {
    2013                 :        147 :         uint32_t buf_len = 0, buf_offset = 0;
    2014                 :            :         uint32_t len, offset_in_block, offset_blocks;
    2015                 :        147 :         uint64_t guard = 0;
    2016                 :        147 :         struct _dif_sgl sgl;
    2017                 :            :         int rc;
    2018                 :            : 
    2019   [ +  -  -  + ]:        147 :         if (iovs == NULL || iovcnt == 0) {
    2020                 :          0 :                 return -EINVAL;
    2021                 :            :         }
    2022                 :            : 
    2023   [ +  -  #  #  :        147 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
    2024   [ #  #  #  # ]:        147 :                 guard = ctx->last_guard;
    2025                 :          0 :         }
    2026                 :            : 
    2027                 :        147 :         _dif_sgl_init(&sgl, iovs, iovcnt);
    2028                 :            : 
    2029                 :        147 :         rc = _dif_sgl_setup_stream(&sgl, &buf_offset, &buf_len, data_offset, data_len, ctx);
    2030         [ +  + ]:        147 :         if (rc != 0) {
    2031                 :          9 :                 return rc;
    2032                 :            :         }
    2033                 :            : 
    2034         [ +  + ]:        366 :         while (buf_len != 0) {
    2035   [ +  +  #  #  :        228 :                 len = spdk_min(buf_len, _to_next_boundary(buf_offset, ctx->block_size));
          #  #  #  #  #  
                      # ]
    2036   [ -  +  #  #  :        228 :                 offset_in_block = buf_offset % ctx->block_size;
                   #  # ]
    2037   [ -  +  #  #  :        228 :                 offset_blocks = buf_offset / ctx->block_size;
                   #  # ]
    2038                 :            : 
    2039                 :        228 :                 guard = _dif_generate_split(&sgl, offset_in_block, len, guard, offset_blocks, ctx);
    2040                 :            : 
    2041                 :        228 :                 buf_len -= len;
    2042                 :        228 :                 buf_offset += len;
    2043                 :            :         }
    2044                 :            : 
    2045   [ +  -  #  #  :        138 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
    2046   [ #  #  #  # ]:        138 :                 ctx->last_guard = guard;
    2047                 :          0 :         }
    2048                 :            : 
    2049                 :        138 :         return 0;
    2050                 :          0 : }
    2051                 :            : 
    2052                 :            : int
    2053                 :     958623 : spdk_dif_verify_stream(struct iovec *iovs, int iovcnt,
    2054                 :            :                        uint32_t data_offset, uint32_t data_len,
    2055                 :            :                        struct spdk_dif_ctx *ctx,
    2056                 :            :                        struct spdk_dif_error *err_blk)
    2057                 :            : {
    2058                 :     958623 :         uint32_t buf_len = 0, buf_offset = 0;
    2059                 :            :         uint32_t len, offset_in_block, offset_blocks;
    2060                 :     958623 :         uint64_t guard = 0;
    2061                 :         27 :         struct _dif_sgl sgl;
    2062                 :     958623 :         int rc = 0;
    2063                 :            : 
    2064   [ +  -  -  + ]:     958623 :         if (iovs == NULL || iovcnt == 0) {
    2065                 :          0 :                 return -EINVAL;
    2066                 :            :         }
    2067                 :            : 
    2068   [ +  -  #  #  :     958623 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
    2069   [ #  #  #  # ]:     958623 :                 guard = ctx->last_guard;
    2070                 :          0 :         }
    2071                 :            : 
    2072                 :     958623 :         _dif_sgl_init(&sgl, iovs, iovcnt);
    2073                 :            : 
    2074                 :     958623 :         rc = _dif_sgl_setup_stream(&sgl, &buf_offset, &buf_len, data_offset, data_len, ctx);
    2075         [ -  + ]:     958623 :         if (rc != 0) {
    2076                 :          0 :                 return rc;
    2077                 :            :         }
    2078                 :            : 
    2079         [ +  + ]:   16545139 :         while (buf_len != 0) {
    2080   [ +  +  #  #  :   15586516 :                 len = spdk_min(buf_len, _to_next_boundary(buf_offset, ctx->block_size));
          #  #  #  #  #  
                      # ]
    2081   [ -  +  #  #  :   15586516 :                 offset_in_block = buf_offset % ctx->block_size;
                   #  # ]
    2082   [ -  +  #  #  :   15586516 :                 offset_blocks = buf_offset / ctx->block_size;
                   #  # ]
    2083                 :            : 
    2084                 :   15586516 :                 rc = _dif_verify_split(&sgl, offset_in_block, len, &guard, offset_blocks,
    2085                 :          0 :                                        ctx, err_blk);
    2086         [ -  + ]:   15586516 :                 if (rc != 0) {
    2087                 :          0 :                         goto error;
    2088                 :            :                 }
    2089                 :            : 
    2090                 :   15586516 :                 buf_len -= len;
    2091                 :   15586516 :                 buf_offset += len;
    2092                 :            :         }
    2093                 :            : 
    2094   [ -  +  #  #  :     958623 :         if (ctx->dif_flags & SPDK_DIF_FLAGS_GUARD_CHECK) {
             #  #  #  # ]
    2095   [ #  #  #  # ]:     958623 :                 ctx->last_guard = guard;
    2096                 :          0 :         }
    2097                 :         27 : error:
    2098                 :     958623 :         return rc;
    2099                 :          0 : }
    2100                 :            : 
    2101                 :            : int
    2102                 :     211299 : spdk_dif_update_crc32c_stream(struct iovec *iovs, int iovcnt,
    2103                 :            :                               uint32_t data_offset, uint32_t data_len,
    2104                 :            :                               uint32_t *_crc32c, const struct spdk_dif_ctx *ctx)
    2105                 :            : {
    2106                 :     211299 :         uint32_t buf_len = 0, buf_offset = 0, len, offset_in_block;
    2107                 :            :         uint32_t crc32c;
    2108                 :         27 :         struct _dif_sgl sgl;
    2109                 :            :         int rc;
    2110                 :            : 
    2111   [ +  -  -  + ]:     211299 :         if (iovs == NULL || iovcnt == 0) {
    2112                 :          0 :                 return -EINVAL;
    2113                 :            :         }
    2114                 :            : 
    2115         [ #  # ]:     211299 :         crc32c = *_crc32c;
    2116                 :     211299 :         _dif_sgl_init(&sgl, iovs, iovcnt);
    2117                 :            : 
    2118                 :     211299 :         rc = _dif_sgl_setup_stream(&sgl, &buf_offset, &buf_len, data_offset, data_len, ctx);
    2119         [ -  + ]:     211299 :         if (rc != 0) {
    2120                 :          0 :                 return rc;
    2121                 :            :         }
    2122                 :            : 
    2123         [ +  + ]:    6220660 :         while (buf_len != 0) {
    2124   [ +  +  #  #  :    6009361 :                 len = spdk_min(buf_len, _to_next_boundary(buf_offset, ctx->block_size));
          #  #  #  #  #  
                      # ]
    2125   [ -  +  #  #  :    6009361 :                 offset_in_block = buf_offset % ctx->block_size;
                   #  # ]
    2126                 :            : 
    2127                 :    6009361 :                 crc32c = _dif_update_crc32c_split(&sgl, offset_in_block, len, crc32c, ctx);
    2128                 :            : 
    2129                 :    6009361 :                 buf_len -= len;
    2130                 :    6009361 :                 buf_offset += len;
    2131                 :            :         }
    2132                 :            : 
    2133         [ #  # ]:     211299 :         *_crc32c = crc32c;
    2134                 :            : 
    2135                 :     211299 :         return 0;
    2136                 :          0 : }
    2137                 :            : 
    2138                 :            : void
    2139                 :    1253582 : spdk_dif_get_range_with_md(uint32_t data_offset, uint32_t data_len,
    2140                 :            :                            uint32_t *_buf_offset, uint32_t *_buf_len,
    2141                 :            :                            const struct spdk_dif_ctx *ctx)
    2142                 :            : {
    2143                 :            :         uint32_t data_block_size, data_unalign, buf_offset, buf_len;
    2144                 :            : 
    2145   [ -  +  -  +  :    1253582 :         if (!ctx->md_interleave) {
             #  #  #  # ]
    2146                 :          0 :                 buf_offset = data_offset;
    2147                 :          0 :                 buf_len = data_len;
    2148                 :          0 :         } else {
    2149   [ #  #  #  #  :    1253582 :                 data_block_size = ctx->block_size - ctx->md_size;
             #  #  #  # ]
    2150                 :            : 
    2151         [ -  + ]:    1253582 :                 data_unalign = data_offset % data_block_size;
    2152                 :            : 
    2153   [ #  #  #  # ]:    1253582 :                 buf_offset = _to_size_with_md(data_offset, data_block_size, ctx->block_size);
    2154   [ #  #  #  # ]:    1253582 :                 buf_len = _to_size_with_md(data_unalign + data_len, data_block_size, ctx->block_size) -
    2155                 :          0 :                           data_unalign;
    2156                 :            :         }
    2157                 :            : 
    2158         [ +  - ]:    1253582 :         if (_buf_offset != NULL) {
    2159         [ #  # ]:    1253582 :                 *_buf_offset = buf_offset;
    2160                 :          0 :         }
    2161                 :            : 
    2162         [ +  - ]:    1253582 :         if (_buf_len != NULL) {
    2163         [ #  # ]:    1253582 :                 *_buf_len = buf_len;
    2164                 :          0 :         }
    2165                 :    1253582 : }
    2166                 :            : 
    2167                 :            : uint32_t
    2168                 :     663673 : spdk_dif_get_length_with_md(uint32_t data_len, const struct spdk_dif_ctx *ctx)
    2169                 :            : {
    2170                 :            :         uint32_t data_block_size;
    2171                 :            : 
    2172   [ -  +  -  +  :     663673 :         if (!ctx->md_interleave) {
             #  #  #  # ]
    2173                 :          0 :                 return data_len;
    2174                 :            :         } else {
    2175   [ #  #  #  #  :     663673 :                 data_block_size = ctx->block_size - ctx->md_size;
             #  #  #  # ]
    2176                 :            : 
    2177   [ #  #  #  # ]:     663673 :                 return _to_size_with_md(data_len, data_block_size, ctx->block_size);
    2178                 :            :         }
    2179                 :          0 : }
    2180                 :            : 
    2181                 :            : static int
    2182                 :        216 : _dif_remap_ref_tag(struct _dif_sgl *sgl, uint32_t offset_blocks,
    2183                 :            :                    const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk,
    2184                 :            :                    bool check_ref_tag)
    2185                 :            : {
    2186                 :        216 :         uint32_t offset, buf_len;
    2187                 :        216 :         uint64_t expected = 0, remapped;
    2188                 :        216 :         uint8_t *buf;
    2189                 :        216 :         struct _dif_sgl tmp_sgl;
    2190                 :        216 :         struct spdk_dif dif;
    2191                 :            : 
    2192                 :            :         /* Fast forward to DIF field. */
    2193   [ #  #  #  # ]:        216 :         _dif_sgl_advance(sgl, ctx->guard_interval);
    2194                 :        216 :         _dif_sgl_copy(&tmp_sgl, sgl);
    2195                 :            : 
    2196                 :            :         /* Copy the split DIF field to the temporary DIF buffer */
    2197                 :        216 :         offset = 0;
    2198   [ +  +  #  #  :        486 :         while (offset < _dif_size(ctx->dif_pi_format)) {
                   #  # ]
    2199                 :        270 :                 _dif_sgl_get_buf(sgl, &buf, &buf_len);
    2200   [ +  +  #  #  :        270 :                 buf_len = spdk_min(buf_len, _dif_size(ctx->dif_pi_format) - offset);
          #  #  #  #  #  
                      # ]
    2201                 :            : 
    2202   [ -  +  -  +  :        270 :                 memcpy((uint8_t *)&dif + offset, buf, buf_len);
                   #  # ]
    2203                 :            : 
    2204                 :        270 :                 _dif_sgl_advance(sgl, buf_len);
    2205                 :        270 :                 offset += buf_len;
    2206                 :            :         }
    2207                 :            : 
    2208         [ -  + ]:        216 :         if (_dif_ignore(&dif, ctx)) {
    2209                 :          0 :                 goto end;
    2210                 :            :         }
    2211                 :            : 
    2212                 :            :         /* For type 1 and 2, the Reference Tag is incremented for each
    2213                 :            :          * subsequent logical block. For type 3, the Reference Tag
    2214                 :            :          * remains the same as the initial Reference Tag.
    2215                 :            :          */
    2216   [ +  -  #  #  :        216 :         if (ctx->dif_type != SPDK_DIF_TYPE3) {
                   #  # ]
    2217   [ #  #  #  #  :        216 :                 expected = ctx->init_ref_tag + ctx->ref_tag_offset + offset_blocks;
             #  #  #  # ]
    2218   [ #  #  #  #  :        216 :                 remapped = ctx->remapped_init_ref_tag + ctx->ref_tag_offset + offset_blocks;
             #  #  #  # ]
    2219                 :          0 :         } else {
    2220   [ #  #  #  # ]:          0 :                 remapped = ctx->remapped_init_ref_tag;
    2221                 :            :         }
    2222                 :            : 
    2223                 :            :         /* Verify the stored Reference Tag. */
    2224   [ +  -  -  +  :        216 :         if (check_ref_tag && !_dif_reftag_check(&dif, ctx, expected, offset_blocks, err_blk)) {
                   #  # ]
    2225                 :          0 :                 return -1;
    2226                 :            :         }
    2227                 :            : 
    2228                 :            :         /* Update the stored Reference Tag to the remapped one. */
    2229   [ #  #  #  # ]:        216 :         _dif_set_reftag(&dif, remapped, ctx->dif_pi_format);
    2230                 :            : 
    2231                 :        216 :         offset = 0;
    2232   [ +  +  #  #  :        486 :         while (offset < _dif_size(ctx->dif_pi_format)) {
                   #  # ]
    2233                 :        270 :                 _dif_sgl_get_buf(&tmp_sgl, &buf, &buf_len);
    2234   [ +  +  #  #  :        270 :                 buf_len = spdk_min(buf_len, _dif_size(ctx->dif_pi_format) - offset);
          #  #  #  #  #  
                      # ]
    2235                 :            : 
    2236   [ -  +  -  +  :        270 :                 memcpy(buf, (uint8_t *)&dif + offset, buf_len);
                   #  # ]
    2237                 :            : 
    2238                 :        270 :                 _dif_sgl_advance(&tmp_sgl, buf_len);
    2239                 :        270 :                 offset += buf_len;
    2240                 :            :         }
    2241                 :            : 
    2242                 :        216 : end:
    2243   [ #  #  #  #  :        216 :         _dif_sgl_advance(sgl, ctx->block_size - ctx->guard_interval - _dif_size(ctx->dif_pi_format));
          #  #  #  #  #  
                #  #  # ]
    2244                 :            : 
    2245                 :        216 :         return 0;
    2246                 :          0 : }
    2247                 :            : 
    2248                 :            : int
    2249                 :         36 : spdk_dif_remap_ref_tag(struct iovec *iovs, int iovcnt, uint32_t num_blocks,
    2250                 :            :                        const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk,
    2251                 :            :                        bool check_ref_tag)
    2252                 :            : {
    2253                 :         36 :         struct _dif_sgl sgl;
    2254                 :            :         uint32_t offset_blocks;
    2255                 :            :         int rc;
    2256                 :            : 
    2257                 :         36 :         _dif_sgl_init(&sgl, iovs, iovcnt);
    2258                 :            : 
    2259   [ -  +  #  #  :         36 :         if (!_dif_sgl_is_valid(&sgl, ctx->block_size * num_blocks)) {
                   #  # ]
    2260                 :          0 :                 SPDK_ERRLOG("Size of iovec array is not valid.\n");
    2261                 :          0 :                 return -EINVAL;
    2262                 :            :         }
    2263                 :            : 
    2264   [ -  +  #  #  :         36 :         if (_dif_is_disabled(ctx->dif_type)) {
                   #  # ]
    2265                 :          0 :                 return 0;
    2266                 :            :         }
    2267                 :            : 
    2268   [ -  +  #  #  :         36 :         if (!(ctx->dif_flags & SPDK_DIF_FLAGS_REFTAG_CHECK)) {
             #  #  #  # ]
    2269                 :          0 :                 return 0;
    2270                 :            :         }
    2271                 :            : 
    2272         [ +  + ]:        252 :         for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
    2273         [ #  # ]:        216 :                 rc = _dif_remap_ref_tag(&sgl, offset_blocks, ctx, err_blk, check_ref_tag);
    2274         [ -  + ]:        216 :                 if (rc != 0) {
    2275                 :          0 :                         return rc;
    2276                 :            :                 }
    2277                 :          0 :         }
    2278                 :            : 
    2279                 :         36 :         return 0;
    2280                 :          0 : }
    2281                 :            : 
    2282                 :            : static int
    2283                 :        600 : _dix_remap_ref_tag(struct _dif_sgl *md_sgl, uint32_t offset_blocks,
    2284                 :            :                    const struct spdk_dif_ctx *ctx, struct spdk_dif_error *err_blk,
    2285                 :            :                    bool check_ref_tag)
    2286                 :            : {
    2287                 :        600 :         uint64_t expected = 0, remapped;
    2288                 :        600 :         uint8_t *md_buf;
    2289                 :            :         struct spdk_dif *dif;
    2290                 :            : 
    2291                 :        600 :         _dif_sgl_get_buf(md_sgl, &md_buf, NULL);
    2292                 :            : 
    2293   [ #  #  #  #  :        600 :         dif = (struct spdk_dif *)(md_buf + ctx->guard_interval);
                   #  # ]
    2294                 :            : 
    2295         [ -  + ]:        600 :         if (_dif_ignore(dif, ctx)) {
    2296                 :          0 :                 goto end;
    2297                 :            :         }
    2298                 :            : 
    2299                 :            :         /* For type 1 and 2, the Reference Tag is incremented for each
    2300                 :            :          * subsequent logical block. For type 3, the Reference Tag
    2301                 :            :          * remains the same as the initialReference Tag.
    2302                 :            :          */
    2303   [ +  -  #  #  :        600 :         if (ctx->dif_type != SPDK_DIF_TYPE3) {
                   #  # ]
    2304   [ #  #  #  #  :        600 :                 expected = ctx->init_ref_tag + ctx->ref_tag_offset + offset_blocks;
             #  #  #  # ]
    2305   [ #  #  #  #  :        600 :                 remapped = ctx->remapped_init_ref_tag + ctx->ref_tag_offset + offset_blocks;
             #  #  #  # ]
    2306                 :          0 :         } else {
    2307   [ #  #  #  # ]:          0 :                 remapped = ctx->remapped_init_ref_tag;
    2308                 :            :         }
    2309                 :            : 
    2310                 :            :         /* Verify the stored Reference Tag. */
    2311   [ +  +  -  +  :        600 :         if (check_ref_tag && !_dif_reftag_check(dif, ctx, expected, offset_blocks, err_blk)) {
                   #  # ]
    2312                 :          0 :                 return -1;
    2313                 :            :         }
    2314                 :            : 
    2315                 :            :         /* Update the stored Reference Tag to the remapped one. */
    2316   [ #  #  #  # ]:        600 :         _dif_set_reftag(dif, remapped, ctx->dif_pi_format);
    2317                 :            : 
    2318                 :        600 : end:
    2319   [ #  #  #  # ]:        600 :         _dif_sgl_advance(md_sgl, ctx->md_size);
    2320                 :            : 
    2321                 :        600 :         return 0;
    2322                 :          0 : }
    2323                 :            : 
    2324                 :            : int
    2325                 :         36 : spdk_dix_remap_ref_tag(struct iovec *md_iov, uint32_t num_blocks,
    2326                 :            :                        const struct spdk_dif_ctx *ctx,
    2327                 :            :                        struct spdk_dif_error *err_blk,
    2328                 :            :                        bool check_ref_tag)
    2329                 :            : {
    2330                 :         36 :         struct _dif_sgl md_sgl;
    2331                 :            :         uint32_t offset_blocks;
    2332                 :            :         int rc;
    2333                 :            : 
    2334                 :         36 :         _dif_sgl_init(&md_sgl, md_iov, 1);
    2335                 :            : 
    2336   [ -  +  #  #  :         36 :         if (!_dif_sgl_is_valid(&md_sgl, ctx->md_size * num_blocks)) {
                   #  # ]
    2337                 :          0 :                 SPDK_ERRLOG("Size of metadata iovec array is not valid.\n");
    2338                 :          0 :                 return -EINVAL;
    2339                 :            :         }
    2340                 :            : 
    2341   [ -  +  #  #  :         36 :         if (_dif_is_disabled(ctx->dif_type)) {
                   #  # ]
    2342                 :          0 :                 return 0;
    2343                 :            :         }
    2344                 :            : 
    2345   [ -  +  #  #  :         36 :         if (!(ctx->dif_flags & SPDK_DIF_FLAGS_REFTAG_CHECK)) {
             #  #  #  # ]
    2346                 :          0 :                 return 0;
    2347                 :            :         }
    2348                 :            : 
    2349         [ +  + ]:        636 :         for (offset_blocks = 0; offset_blocks < num_blocks; offset_blocks++) {
    2350         [ #  # ]:        600 :                 rc = _dix_remap_ref_tag(&md_sgl, offset_blocks, ctx, err_blk, check_ref_tag);
    2351         [ -  + ]:        600 :                 if (rc != 0) {
    2352                 :          0 :                         return rc;
    2353                 :            :                 }
    2354                 :          0 :         }
    2355                 :            : 
    2356                 :         36 :         return 0;
    2357                 :          0 : }

Generated by: LCOV version 1.15