LCOV - code coverage report
Current view: top level - spdk/lib/nvmf - ctrlr_bdev.c (source / functions) Hit Total Coverage
Test: Combined Lines: 468 539 86.8 %
Date: 2024-07-12 17:08:57 Functions: 30 33 90.9 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 136 190 71.6 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2017 Intel Corporation. All rights reserved.
       3                 :            :  *   Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved.
       4                 :            :  *   Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
       5                 :            :  */
       6                 :            : 
       7                 :            : #include "spdk/stdinc.h"
       8                 :            : 
       9                 :            : #include "nvmf_internal.h"
      10                 :            : 
      11                 :            : #include "spdk/bdev.h"
      12                 :            : #include "spdk/endian.h"
      13                 :            : #include "spdk/thread.h"
      14                 :            : #include "spdk/likely.h"
      15                 :            : #include "spdk/nvme.h"
      16                 :            : #include "spdk/nvmf_cmd.h"
      17                 :            : #include "spdk/nvmf_spec.h"
      18                 :            : #include "spdk/trace.h"
      19                 :            : #include "spdk/scsi_spec.h"
      20                 :            : #include "spdk/string.h"
      21                 :            : #include "spdk/util.h"
      22                 :            : 
      23                 :            : #include "spdk/log.h"
      24                 :            : 
      25                 :            : static bool
      26                 :       2510 : nvmf_subsystem_bdev_io_type_supported(struct spdk_nvmf_subsystem *subsystem,
      27                 :            :                                       enum spdk_bdev_io_type io_type)
      28                 :            : {
      29                 :            :         struct spdk_nvmf_ns *ns;
      30                 :            : 
      31         [ +  + ]:       4463 :         for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL;
      32                 :       1953 :              ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) {
      33         [ -  + ]:       2237 :                 if (ns->bdev == NULL) {
      34                 :          0 :                         continue;
      35                 :            :                 }
      36                 :            : 
      37         [ +  + ]:       2237 :                 if (!spdk_bdev_io_type_supported(ns->bdev, io_type)) {
      38   [ -  +  -  + ]:        284 :                         SPDK_DEBUGLOG(nvmf,
      39                 :            :                                       "Subsystem %s namespace %u (%s) does not support io_type %d\n",
      40                 :            :                                       spdk_nvmf_subsystem_get_nqn(subsystem),
      41                 :            :                                       ns->opts.nsid, spdk_bdev_get_name(ns->bdev), (int)io_type);
      42                 :        284 :                         return false;
      43                 :            :                 }
      44                 :            :         }
      45                 :            : 
      46   [ -  +  -  + ]:       2226 :         SPDK_DEBUGLOG(nvmf, "All devices in Subsystem %s support io_type %d\n",
      47                 :            :                       spdk_nvmf_subsystem_get_nqn(subsystem), (int)io_type);
      48                 :       2226 :         return true;
      49                 :            : }
      50                 :            : 
      51                 :            : bool
      52                 :       1255 : nvmf_ctrlr_dsm_supported(struct spdk_nvmf_ctrlr *ctrlr)
      53                 :            : {
      54                 :       1255 :         return nvmf_subsystem_bdev_io_type_supported(ctrlr->subsys, SPDK_BDEV_IO_TYPE_UNMAP);
      55                 :            : }
      56                 :            : 
      57                 :            : bool
      58                 :       1255 : nvmf_ctrlr_write_zeroes_supported(struct spdk_nvmf_ctrlr *ctrlr)
      59                 :            : {
      60                 :       1255 :         return nvmf_subsystem_bdev_io_type_supported(ctrlr->subsys, SPDK_BDEV_IO_TYPE_WRITE_ZEROES);
      61                 :            : }
      62                 :            : 
      63                 :            : bool
      64                 :          0 : nvmf_ctrlr_copy_supported(struct spdk_nvmf_ctrlr *ctrlr)
      65                 :            : {
      66                 :          0 :         return nvmf_subsystem_bdev_io_type_supported(ctrlr->subsys, SPDK_BDEV_IO_TYPE_COPY);
      67                 :            : }
      68                 :            : 
      69                 :            : static void
      70                 :   19118966 : nvmf_bdev_ctrlr_complete_cmd(struct spdk_bdev_io *bdev_io, bool success,
      71                 :            :                              void *cb_arg)
      72                 :            : {
      73                 :   19118966 :         struct spdk_nvmf_request        *req = cb_arg;
      74                 :   19118966 :         struct spdk_nvme_cpl            *response = &req->rsp->nvme_cpl;
      75                 :   19118966 :         int                             sc = 0, sct = 0;
      76                 :   19118966 :         uint32_t                        cdw0 = 0;
      77                 :            : 
      78         [ +  + ]:   19118966 :         if (spdk_unlikely(req->first_fused)) {
      79                 :       3107 :                 struct spdk_nvmf_request        *first_req = req->first_fused_req;
      80                 :       3107 :                 struct spdk_nvme_cpl            *first_response = &first_req->rsp->nvme_cpl;
      81                 :       3107 :                 int                             first_sc = 0, first_sct = 0;
      82                 :            : 
      83                 :            :                 /* get status for both operations */
      84                 :       3107 :                 spdk_bdev_io_get_nvme_fused_status(bdev_io, &cdw0, &first_sct, &first_sc, &sct, &sc);
      85                 :       3107 :                 first_response->cdw0 = cdw0;
      86                 :       3107 :                 first_response->status.sc = first_sc;
      87                 :       3107 :                 first_response->status.sct = first_sct;
      88                 :            : 
      89                 :            :                 /* first request should be completed */
      90                 :       3107 :                 spdk_nvmf_request_complete(first_req);
      91                 :       3107 :                 req->first_fused_req = NULL;
      92                 :       3107 :                 req->first_fused = false;
      93                 :            :         } else {
      94                 :   19115859 :                 spdk_bdev_io_get_nvme_status(bdev_io, &cdw0, &sct, &sc);
      95                 :            :         }
      96                 :            : 
      97                 :   19118966 :         response->cdw0 = cdw0;
      98                 :   19118966 :         response->status.sc = sc;
      99                 :   19118966 :         response->status.sct = sct;
     100                 :            : 
     101                 :   19118966 :         spdk_nvmf_request_complete(req);
     102                 :   19118966 :         spdk_bdev_free_io(bdev_io);
     103                 :   19118966 : }
     104                 :            : 
     105                 :            : static void
     106                 :         16 : nvmf_bdev_ctrlr_complete_admin_cmd(struct spdk_bdev_io *bdev_io, bool success,
     107                 :            :                                    void *cb_arg)
     108                 :            : {
     109                 :         16 :         struct spdk_nvmf_request *req = cb_arg;
     110                 :            : 
     111         [ +  + ]:         16 :         if (req->cmd_cb_fn) {
     112                 :          4 :                 req->cmd_cb_fn(req);
     113                 :            :         }
     114                 :            : 
     115                 :         16 :         nvmf_bdev_ctrlr_complete_cmd(bdev_io, success, req);
     116                 :         16 : }
     117                 :            : 
     118                 :            : void
     119                 :       2576 : nvmf_bdev_ctrlr_identify_ns(struct spdk_nvmf_ns *ns, struct spdk_nvme_ns_data *nsdata,
     120                 :            :                             bool dif_insert_or_strip)
     121                 :            : {
     122                 :       2576 :         struct spdk_bdev *bdev = ns->bdev;
     123                 :            :         uint64_t num_blocks;
     124                 :            :         uint32_t phys_blocklen;
     125                 :            :         uint32_t max_copy;
     126                 :            : 
     127                 :       2576 :         num_blocks = spdk_bdev_get_num_blocks(bdev);
     128                 :            : 
     129                 :       2576 :         nsdata->nsze = num_blocks;
     130                 :       2576 :         nsdata->ncap = num_blocks;
     131                 :       2576 :         nsdata->nuse = num_blocks;
     132                 :       2576 :         nsdata->nlbaf = 0;
     133                 :       2576 :         nsdata->flbas.format = 0;
     134                 :       2576 :         nsdata->flbas.msb_format = 0;
     135                 :       2576 :         nsdata->nacwu = spdk_bdev_get_acwu(bdev) - 1; /* nacwu is 0-based */
     136         [ +  + ]:       2576 :         if (!dif_insert_or_strip) {
     137                 :       2540 :                 nsdata->lbaf[0].ms = spdk_bdev_get_md_size(bdev);
     138                 :       2540 :                 nsdata->lbaf[0].lbads = spdk_u32log2(spdk_bdev_get_block_size(bdev));
     139         [ +  + ]:       2540 :                 if (nsdata->lbaf[0].ms != 0) {
     140                 :          6 :                         nsdata->flbas.extended = 1;
     141                 :          6 :                         nsdata->mc.extended = 1;
     142                 :          6 :                         nsdata->mc.pointer = 0;
     143                 :          6 :                         nsdata->dps.md_start = spdk_bdev_is_dif_head_of_md(bdev);
     144                 :            :                         /* NVMf library doesn't process PRACT and PRCHK flags, we
     145                 :            :                          * leave the use of extended LBA buffer to users.
     146                 :            :                          */
     147                 :          6 :                         nsdata->dps.pit = SPDK_NVME_FMT_NVM_PROTECTION_DISABLE;
     148                 :            :                 }
     149                 :            :         } else {
     150                 :         36 :                 nsdata->lbaf[0].ms = 0;
     151                 :         36 :                 nsdata->lbaf[0].lbads = spdk_u32log2(spdk_bdev_get_data_block_size(bdev));
     152                 :            :         }
     153                 :            : 
     154                 :       2576 :         phys_blocklen = spdk_bdev_get_physical_block_size(bdev);
     155         [ -  + ]:       2576 :         assert(phys_blocklen > 0);
     156                 :            :         /* Linux driver uses min(nawupf, npwg) to set physical_block_size */
     157                 :       2576 :         nsdata->nsfeat.optperf = 1;
     158                 :       2576 :         nsdata->nsfeat.ns_atomic_write_unit = 1;
     159         [ -  + ]:       2576 :         nsdata->npwg = (phys_blocklen >> nsdata->lbaf[0].lbads) - 1;
     160                 :       2576 :         nsdata->nawupf = nsdata->npwg;
     161                 :       2576 :         nsdata->npwa = nsdata->npwg;
     162                 :       2576 :         nsdata->npdg = nsdata->npwg;
     163                 :       2576 :         nsdata->npda = nsdata->npwg;
     164                 :            : 
     165         [ +  - ]:       2576 :         if (spdk_bdev_get_write_unit_size(bdev) == 1) {
     166                 :       2576 :                 nsdata->noiob = spdk_bdev_get_optimal_io_boundary(bdev);
     167                 :            :         }
     168                 :       2576 :         nsdata->nmic.can_share = 1;
     169         [ +  + ]:       2576 :         if (nvmf_ns_is_ptpl_capable(ns)) {
     170                 :         12 :                 nsdata->nsrescap.rescap.persist = 1;
     171                 :            :         }
     172                 :       2576 :         nsdata->nsrescap.rescap.write_exclusive = 1;
     173                 :       2576 :         nsdata->nsrescap.rescap.exclusive_access = 1;
     174                 :       2576 :         nsdata->nsrescap.rescap.write_exclusive_reg_only = 1;
     175                 :       2576 :         nsdata->nsrescap.rescap.exclusive_access_reg_only = 1;
     176                 :       2576 :         nsdata->nsrescap.rescap.write_exclusive_all_reg = 1;
     177                 :       2576 :         nsdata->nsrescap.rescap.exclusive_access_all_reg = 1;
     178                 :       2576 :         nsdata->nsrescap.rescap.ignore_existing_key = 1;
     179                 :            : 
     180                 :            :         SPDK_STATIC_ASSERT(sizeof(nsdata->nguid) == sizeof(ns->opts.nguid), "size mismatch");
     181                 :       2576 :         memcpy(nsdata->nguid, ns->opts.nguid, sizeof(nsdata->nguid));
     182                 :            : 
     183                 :            :         SPDK_STATIC_ASSERT(sizeof(nsdata->eui64) == sizeof(ns->opts.eui64), "size mismatch");
     184                 :       2576 :         memcpy(&nsdata->eui64, ns->opts.eui64, sizeof(nsdata->eui64));
     185                 :            : 
     186                 :            :         /* For now we support just one source range for copy command */
     187                 :       2576 :         nsdata->msrc = 0;
     188                 :            : 
     189                 :       2576 :         max_copy = spdk_bdev_get_max_copy(bdev);
     190   [ +  +  -  + ]:       2576 :         if (max_copy == 0 || max_copy > UINT16_MAX) {
     191                 :            :                 /* Zero means copy size is unlimited */
     192                 :       2077 :                 nsdata->mcl = UINT16_MAX;
     193                 :       2077 :                 nsdata->mssrl = UINT16_MAX;
     194                 :            :         } else {
     195                 :        499 :                 nsdata->mcl = max_copy;
     196                 :        499 :                 nsdata->mssrl = max_copy;
     197                 :            :         }
     198                 :       2576 : }
     199                 :            : 
     200                 :            : static void
     201                 :   19796774 : nvmf_bdev_ctrlr_get_rw_params(const struct spdk_nvme_cmd *cmd, uint64_t *start_lba,
     202                 :            :                               uint64_t *num_blocks)
     203                 :            : {
     204                 :            :         /* SLBA: CDW10 and CDW11 */
     205                 :   19796774 :         *start_lba = from_le64(&cmd->cdw10);
     206                 :            : 
     207                 :            :         /* NLB: CDW12 bits 15:00, 0's based */
     208                 :   19796774 :         *num_blocks = (from_le32(&cmd->cdw12) & 0xFFFFu) + 1;
     209                 :   19796774 : }
     210                 :            : 
     211                 :            : static bool
     212                 :   19793697 : nvmf_bdev_ctrlr_lba_in_range(uint64_t bdev_num_blocks, uint64_t io_start_lba,
     213                 :            :                              uint64_t io_num_blocks)
     214                 :            : {
     215         [ +  + ]:   19793697 :         if (io_start_lba + io_num_blocks > bdev_num_blocks ||
     216         [ +  + ]:   19768883 :             io_start_lba + io_num_blocks < io_start_lba) {
     217                 :      24826 :                 return false;
     218                 :            :         }
     219                 :            : 
     220                 :   19768871 :         return true;
     221                 :            : }
     222                 :            : 
     223                 :            : static void
     224                 :      83888 : nvmf_ctrlr_process_io_cmd_resubmit(void *arg)
     225                 :            : {
     226                 :      83888 :         struct spdk_nvmf_request *req = arg;
     227                 :            :         int rc;
     228                 :            : 
     229                 :      83888 :         rc = nvmf_ctrlr_process_io_cmd(req);
     230         [ -  + ]:      83888 :         if (rc == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) {
     231                 :          0 :                 spdk_nvmf_request_complete(req);
     232                 :            :         }
     233                 :      83888 : }
     234                 :            : 
     235                 :            : static void
     236                 :          0 : nvmf_ctrlr_process_admin_cmd_resubmit(void *arg)
     237                 :            : {
     238                 :          0 :         struct spdk_nvmf_request *req = arg;
     239                 :            :         int rc;
     240                 :            : 
     241                 :          0 :         rc = nvmf_ctrlr_process_admin_cmd(req);
     242         [ #  # ]:          0 :         if (rc == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) {
     243                 :          0 :                 spdk_nvmf_request_complete(req);
     244                 :            :         }
     245                 :          0 : }
     246                 :            : 
     247                 :            : static void
     248                 :     129484 : nvmf_bdev_ctrl_queue_io(struct spdk_nvmf_request *req, struct spdk_bdev *bdev,
     249                 :            :                         struct spdk_io_channel *ch, spdk_bdev_io_wait_cb cb_fn, void *cb_arg)
     250                 :            : {
     251                 :            :         int rc;
     252                 :            : 
     253                 :     129484 :         req->bdev_io_wait.bdev = bdev;
     254                 :     129484 :         req->bdev_io_wait.cb_fn = cb_fn;
     255                 :     129484 :         req->bdev_io_wait.cb_arg = cb_arg;
     256                 :            : 
     257                 :     129484 :         rc = spdk_bdev_queue_io_wait(bdev, ch, &req->bdev_io_wait);
     258         [ -  + ]:     129484 :         if (rc != 0) {
     259                 :          0 :                 assert(false);
     260                 :            :         }
     261                 :     129484 :         req->qpair->group->stat.pending_bdev_io++;
     262                 :     129484 : }
     263                 :            : 
     264                 :            : bool
     265                 :          0 : nvmf_bdev_zcopy_enabled(struct spdk_bdev *bdev)
     266                 :            : {
     267                 :          0 :         return spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_ZCOPY);
     268                 :            : }
     269                 :            : 
     270                 :            : int
     271                 :   11937768 : nvmf_bdev_ctrlr_read_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
     272                 :            :                          struct spdk_io_channel *ch, struct spdk_nvmf_request *req)
     273                 :            : {
     274                 :   47229867 :         struct spdk_bdev_ext_io_opts opts = {
     275                 :            :                 .size = SPDK_SIZEOF(&opts, accel_sequence),
     276                 :   11937768 :                 .memory_domain = req->memory_domain,
     277                 :   11937768 :                 .memory_domain_ctx = req->memory_domain_ctx,
     278                 :   11937768 :                 .accel_sequence = req->accel_sequence,
     279                 :            :         };
     280                 :   11937768 :         uint64_t bdev_num_blocks = spdk_bdev_get_num_blocks(bdev);
     281                 :   11937768 :         uint32_t block_size = spdk_bdev_get_block_size(bdev);
     282                 :   11937768 :         struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
     283                 :   11937768 :         struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
     284                 :     173735 :         uint64_t start_lba;
     285                 :     173735 :         uint64_t num_blocks;
     286                 :            :         int rc;
     287                 :            : 
     288                 :   11937768 :         nvmf_bdev_ctrlr_get_rw_params(cmd, &start_lba, &num_blocks);
     289                 :            : 
     290         [ +  + ]:   11937768 :         if (spdk_unlikely(!nvmf_bdev_ctrlr_lba_in_range(bdev_num_blocks, start_lba, num_blocks))) {
     291                 :       5623 :                 SPDK_ERRLOG("end of media\n");
     292                 :       5623 :                 rsp->status.sct = SPDK_NVME_SCT_GENERIC;
     293                 :       5623 :                 rsp->status.sc = SPDK_NVME_SC_LBA_OUT_OF_RANGE;
     294                 :       5623 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     295                 :            :         }
     296                 :            : 
     297         [ -  + ]:   11932145 :         if (spdk_unlikely(num_blocks * block_size > req->length)) {
     298                 :          0 :                 SPDK_ERRLOG("Read NLB %" PRIu64 " * block size %" PRIu32 " > SGL length %" PRIu32 "\n",
     299                 :            :                             num_blocks, block_size, req->length);
     300                 :          0 :                 rsp->status.sct = SPDK_NVME_SCT_GENERIC;
     301                 :          0 :                 rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID;
     302                 :          0 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     303                 :            :         }
     304                 :            : 
     305         [ -  + ]:   11932145 :         assert(!spdk_nvmf_request_using_zcopy(req));
     306                 :            : 
     307                 :   11932145 :         rc = spdk_bdev_readv_blocks_ext(desc, ch, req->iov, req->iovcnt, start_lba, num_blocks,
     308                 :            :                                         nvmf_bdev_ctrlr_complete_cmd, req, &opts);
     309         [ +  + ]:   11932145 :         if (spdk_unlikely(rc)) {
     310         [ +  - ]:      40973 :                 if (rc == -ENOMEM) {
     311                 :      40973 :                         nvmf_bdev_ctrl_queue_io(req, bdev, ch, nvmf_ctrlr_process_io_cmd_resubmit, req);
     312                 :      40973 :                         return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     313                 :            :                 }
     314                 :          0 :                 rsp->status.sct = SPDK_NVME_SCT_GENERIC;
     315                 :          0 :                 rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
     316                 :          0 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     317                 :            :         }
     318                 :            : 
     319                 :   11891172 :         return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     320                 :            : }
     321                 :            : 
     322                 :            : int
     323                 :    7261457 : nvmf_bdev_ctrlr_write_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
     324                 :            :                           struct spdk_io_channel *ch, struct spdk_nvmf_request *req)
     325                 :            : {
     326                 :   28597313 :         struct spdk_bdev_ext_io_opts opts = {
     327                 :            :                 .size = SPDK_SIZEOF(&opts, accel_sequence),
     328                 :    7261457 :                 .memory_domain = req->memory_domain,
     329                 :    7261457 :                 .memory_domain_ctx = req->memory_domain_ctx,
     330                 :    7261457 :                 .accel_sequence = req->accel_sequence,
     331                 :            :         };
     332                 :    7261457 :         uint64_t bdev_num_blocks = spdk_bdev_get_num_blocks(bdev);
     333                 :    7261457 :         uint32_t block_size = spdk_bdev_get_block_size(bdev);
     334                 :    7261457 :         struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
     335                 :    7261457 :         struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
     336                 :     149505 :         uint64_t start_lba;
     337                 :     149505 :         uint64_t num_blocks;
     338                 :            :         int rc;
     339                 :            : 
     340                 :    7261457 :         nvmf_bdev_ctrlr_get_rw_params(cmd, &start_lba, &num_blocks);
     341                 :            : 
     342         [ +  + ]:    7261457 :         if (spdk_unlikely(!nvmf_bdev_ctrlr_lba_in_range(bdev_num_blocks, start_lba, num_blocks))) {
     343                 :       5694 :                 SPDK_ERRLOG("end of media\n");
     344                 :       5694 :                 rsp->status.sct = SPDK_NVME_SCT_GENERIC;
     345                 :       5694 :                 rsp->status.sc = SPDK_NVME_SC_LBA_OUT_OF_RANGE;
     346                 :       5694 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     347                 :            :         }
     348                 :            : 
     349         [ -  + ]:    7255763 :         if (spdk_unlikely(num_blocks * block_size > req->length)) {
     350                 :          0 :                 SPDK_ERRLOG("Write NLB %" PRIu64 " * block size %" PRIu32 " > SGL length %" PRIu32 "\n",
     351                 :            :                             num_blocks, block_size, req->length);
     352                 :          0 :                 rsp->status.sct = SPDK_NVME_SCT_GENERIC;
     353                 :          0 :                 rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID;
     354                 :          0 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     355                 :            :         }
     356                 :            : 
     357         [ -  + ]:    7255763 :         assert(!spdk_nvmf_request_using_zcopy(req));
     358                 :            : 
     359                 :    7255763 :         rc = spdk_bdev_writev_blocks_ext(desc, ch, req->iov, req->iovcnt, start_lba, num_blocks,
     360                 :            :                                          nvmf_bdev_ctrlr_complete_cmd, req, &opts);
     361         [ +  + ]:    7255763 :         if (spdk_unlikely(rc)) {
     362         [ +  - ]:      42915 :                 if (rc == -ENOMEM) {
     363                 :      42915 :                         nvmf_bdev_ctrl_queue_io(req, bdev, ch, nvmf_ctrlr_process_io_cmd_resubmit, req);
     364                 :      42915 :                         return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     365                 :            :                 }
     366                 :          0 :                 rsp->status.sct = SPDK_NVME_SCT_GENERIC;
     367                 :          0 :                 rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
     368                 :          0 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     369                 :            :         }
     370                 :            : 
     371                 :    7212848 :         return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     372                 :            : }
     373                 :            : 
     374                 :            : int
     375                 :       5536 : nvmf_bdev_ctrlr_compare_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
     376                 :            :                             struct spdk_io_channel *ch, struct spdk_nvmf_request *req)
     377                 :            : {
     378                 :       5536 :         uint64_t bdev_num_blocks = spdk_bdev_get_num_blocks(bdev);
     379                 :       5536 :         uint32_t block_size = spdk_bdev_get_block_size(bdev);
     380                 :       5536 :         struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
     381                 :       5536 :         struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
     382                 :         20 :         uint64_t start_lba;
     383                 :         20 :         uint64_t num_blocks;
     384                 :            :         int rc;
     385                 :            : 
     386                 :       5536 :         nvmf_bdev_ctrlr_get_rw_params(cmd, &start_lba, &num_blocks);
     387                 :            : 
     388         [ +  + ]:       5536 :         if (spdk_unlikely(!nvmf_bdev_ctrlr_lba_in_range(bdev_num_blocks, start_lba, num_blocks))) {
     389                 :       5518 :                 SPDK_ERRLOG("end of media\n");
     390                 :       5518 :                 rsp->status.sct = SPDK_NVME_SCT_GENERIC;
     391                 :       5518 :                 rsp->status.sc = SPDK_NVME_SC_LBA_OUT_OF_RANGE;
     392                 :       5518 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     393                 :            :         }
     394                 :            : 
     395         [ +  + ]:         18 :         if (spdk_unlikely(num_blocks * block_size > req->length)) {
     396                 :          6 :                 SPDK_ERRLOG("Compare NLB %" PRIu64 " * block size %" PRIu32 " > SGL length %" PRIu32 "\n",
     397                 :            :                             num_blocks, block_size, req->length);
     398                 :          6 :                 rsp->status.sct = SPDK_NVME_SCT_GENERIC;
     399                 :          6 :                 rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID;
     400                 :          6 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     401                 :            :         }
     402                 :            : 
     403                 :         12 :         rc = spdk_bdev_comparev_blocks(desc, ch, req->iov, req->iovcnt, start_lba, num_blocks,
     404                 :            :                                        nvmf_bdev_ctrlr_complete_cmd, req);
     405         [ +  + ]:         12 :         if (spdk_unlikely(rc)) {
     406         [ -  + ]:          6 :                 if (rc == -ENOMEM) {
     407                 :          0 :                         nvmf_bdev_ctrl_queue_io(req, bdev, ch, nvmf_ctrlr_process_io_cmd_resubmit, req);
     408                 :          0 :                         return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     409                 :            :                 }
     410                 :          6 :                 rsp->status.sct = SPDK_NVME_SCT_GENERIC;
     411                 :          6 :                 rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
     412                 :          6 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     413                 :            :         }
     414                 :            : 
     415                 :          6 :         return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     416                 :            : }
     417                 :            : 
     418                 :            : int
     419                 :       3131 : nvmf_bdev_ctrlr_compare_and_write_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
     420                 :            :                                       struct spdk_io_channel *ch, struct spdk_nvmf_request *cmp_req, struct spdk_nvmf_request *write_req)
     421                 :            : {
     422                 :       3131 :         uint64_t bdev_num_blocks = spdk_bdev_get_num_blocks(bdev);
     423                 :       3131 :         uint32_t block_size = spdk_bdev_get_block_size(bdev);
     424                 :       3131 :         struct spdk_nvme_cmd *cmp_cmd = &cmp_req->cmd->nvme_cmd;
     425                 :       3131 :         struct spdk_nvme_cmd *write_cmd = &write_req->cmd->nvme_cmd;
     426                 :       3131 :         struct spdk_nvme_cpl *rsp = &write_req->rsp->nvme_cpl;
     427                 :         20 :         uint64_t write_start_lba, cmp_start_lba;
     428                 :         20 :         uint64_t write_num_blocks, cmp_num_blocks;
     429                 :            :         int rc;
     430                 :            : 
     431                 :       3131 :         nvmf_bdev_ctrlr_get_rw_params(cmp_cmd, &cmp_start_lba, &cmp_num_blocks);
     432                 :       3131 :         nvmf_bdev_ctrlr_get_rw_params(write_cmd, &write_start_lba, &write_num_blocks);
     433                 :            : 
     434   [ +  -  +  + ]:       3131 :         if (spdk_unlikely(write_start_lba != cmp_start_lba || write_num_blocks != cmp_num_blocks)) {
     435                 :          6 :                 SPDK_ERRLOG("Fused command start lba / num blocks mismatch\n");
     436                 :          6 :                 rsp->status.sct = SPDK_NVME_SCT_GENERIC;
     437                 :          6 :                 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
     438                 :          6 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     439                 :            :         }
     440                 :            : 
     441         [ +  + ]:       3125 :         if (spdk_unlikely(!nvmf_bdev_ctrlr_lba_in_range(bdev_num_blocks, write_start_lba,
     442                 :            :                           write_num_blocks))) {
     443                 :          6 :                 SPDK_ERRLOG("end of media\n");
     444                 :          6 :                 rsp->status.sct = SPDK_NVME_SCT_GENERIC;
     445                 :          6 :                 rsp->status.sc = SPDK_NVME_SC_LBA_OUT_OF_RANGE;
     446                 :          6 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     447                 :            :         }
     448                 :            : 
     449         [ +  + ]:       3119 :         if (spdk_unlikely(write_num_blocks * block_size > write_req->length)) {
     450                 :          6 :                 SPDK_ERRLOG("Write NLB %" PRIu64 " * block size %" PRIu32 " > SGL length %" PRIu32 "\n",
     451                 :            :                             write_num_blocks, block_size, write_req->length);
     452                 :          6 :                 rsp->status.sct = SPDK_NVME_SCT_GENERIC;
     453                 :          6 :                 rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID;
     454                 :          6 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     455                 :            :         }
     456                 :            : 
     457                 :       3113 :         rc = spdk_bdev_comparev_and_writev_blocks(desc, ch, cmp_req->iov, cmp_req->iovcnt, write_req->iov,
     458                 :       3113 :                         write_req->iovcnt, write_start_lba, write_num_blocks, nvmf_bdev_ctrlr_complete_cmd, write_req);
     459         [ -  + ]:       3113 :         if (spdk_unlikely(rc)) {
     460         [ #  # ]:          0 :                 if (rc == -ENOMEM) {
     461                 :          0 :                         nvmf_bdev_ctrl_queue_io(cmp_req, bdev, ch, nvmf_ctrlr_process_io_cmd_resubmit, cmp_req);
     462                 :          0 :                         nvmf_bdev_ctrl_queue_io(write_req, bdev, ch, nvmf_ctrlr_process_io_cmd_resubmit, write_req);
     463                 :          0 :                         return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     464                 :            :                 }
     465                 :          0 :                 rsp->status.sct = SPDK_NVME_SCT_GENERIC;
     466                 :          0 :                 rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
     467                 :          0 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     468                 :            :         }
     469                 :            : 
     470                 :       3113 :         return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     471                 :            : }
     472                 :            : 
     473                 :            : int
     474                 :      11465 : nvmf_bdev_ctrlr_write_zeroes_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
     475                 :            :                                  struct spdk_io_channel *ch, struct spdk_nvmf_request *req)
     476                 :            : {
     477                 :      11465 :         uint64_t bdev_num_blocks = spdk_bdev_get_num_blocks(bdev);
     478                 :      11465 :         struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
     479                 :      11465 :         struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
     480                 :      11465 :         uint64_t max_write_zeroes_size = req->qpair->ctrlr->subsys->max_write_zeroes_size_kib;
     481                 :         20 :         uint64_t start_lba;
     482                 :         20 :         uint64_t num_blocks;
     483                 :            :         int rc;
     484                 :            : 
     485                 :      11465 :         nvmf_bdev_ctrlr_get_rw_params(cmd, &start_lba, &num_blocks);
     486   [ +  +  +  +  :      11465 :         if (spdk_unlikely(max_write_zeroes_size > 0 &&
                   +  - ]
     487                 :            :                           num_blocks > (max_write_zeroes_size << 10) / spdk_bdev_get_block_size(bdev))) {
     488                 :          6 :                 SPDK_ERRLOG("invalid write zeroes size, should not exceed %" PRIu64 "Kib\n", max_write_zeroes_size);
     489                 :          6 :                 rsp->status.sct = SPDK_NVME_SCT_GENERIC;
     490                 :          6 :                 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD;
     491                 :          6 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     492                 :            :         }
     493                 :            : 
     494         [ +  + ]:      11459 :         if (spdk_unlikely(!nvmf_bdev_ctrlr_lba_in_range(bdev_num_blocks, start_lba, num_blocks))) {
     495                 :       7943 :                 SPDK_ERRLOG("end of media\n");
     496                 :       7943 :                 rsp->status.sct = SPDK_NVME_SCT_GENERIC;
     497                 :       7943 :                 rsp->status.sc = SPDK_NVME_SC_LBA_OUT_OF_RANGE;
     498                 :       7943 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     499                 :            :         }
     500                 :            : 
     501                 :       3516 :         rc = spdk_bdev_write_zeroes_blocks(desc, ch, start_lba, num_blocks,
     502                 :            :                                            nvmf_bdev_ctrlr_complete_cmd, req);
     503         [ +  + ]:       3516 :         if (spdk_unlikely(rc)) {
     504         [ -  + ]:          6 :                 if (rc == -ENOMEM) {
     505                 :          0 :                         nvmf_bdev_ctrl_queue_io(req, bdev, ch, nvmf_ctrlr_process_io_cmd_resubmit, req);
     506                 :          0 :                         return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     507                 :            :                 }
     508                 :          6 :                 rsp->status.sct = SPDK_NVME_SCT_GENERIC;
     509                 :          6 :                 rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
     510                 :          6 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     511                 :            :         }
     512                 :            : 
     513                 :       3510 :         return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     514                 :            : }
     515                 :            : 
     516                 :            : int
     517                 :       8330 : nvmf_bdev_ctrlr_flush_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
     518                 :            :                           struct spdk_io_channel *ch, struct spdk_nvmf_request *req)
     519                 :            : {
     520                 :       8330 :         struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
     521                 :            :         int rc;
     522                 :            : 
     523                 :            :         /* As for NVMeoF controller, SPDK always set volatile write
     524                 :            :          * cache bit to 1, return success for those block devices
     525                 :            :          * which can't support FLUSH command.
     526                 :            :          */
     527         [ +  + ]:       8330 :         if (!spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_FLUSH)) {
     528                 :          6 :                 response->status.sct = SPDK_NVME_SCT_GENERIC;
     529                 :          6 :                 response->status.sc = SPDK_NVME_SC_SUCCESS;
     530                 :          6 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     531                 :            :         }
     532                 :            : 
     533                 :       8324 :         rc = spdk_bdev_flush_blocks(desc, ch, 0, spdk_bdev_get_num_blocks(bdev),
     534                 :            :                                     nvmf_bdev_ctrlr_complete_cmd, req);
     535         [ +  + ]:       8324 :         if (spdk_unlikely(rc)) {
     536         [ -  + ]:          6 :                 if (rc == -ENOMEM) {
     537                 :          0 :                         nvmf_bdev_ctrl_queue_io(req, bdev, ch, nvmf_ctrlr_process_io_cmd_resubmit, req);
     538                 :          0 :                         return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     539                 :            :                 }
     540                 :          6 :                 response->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
     541                 :          6 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     542                 :            :         }
     543                 :       8318 :         return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     544                 :            : }
     545                 :            : 
     546                 :            : struct nvmf_bdev_ctrlr_unmap {
     547                 :            :         struct spdk_nvmf_request        *req;
     548                 :            :         uint32_t                        count;
     549                 :            :         struct spdk_bdev_desc           *desc;
     550                 :            :         struct spdk_bdev                *bdev;
     551                 :            :         struct spdk_io_channel          *ch;
     552                 :            :         uint32_t                        range_index;
     553                 :            : };
     554                 :            : 
     555                 :            : static void
     556                 :      45616 : nvmf_bdev_ctrlr_unmap_cpl(struct spdk_bdev_io *bdev_io, bool success,
     557                 :            :                           void *cb_arg)
     558                 :            : {
     559                 :      45616 :         struct nvmf_bdev_ctrlr_unmap *unmap_ctx = cb_arg;
     560                 :      45616 :         struct spdk_nvmf_request        *req = unmap_ctx->req;
     561                 :      45616 :         struct spdk_nvme_cpl            *response = &req->rsp->nvme_cpl;
     562                 :          0 :         int                             sc, sct;
     563                 :          0 :         uint32_t                        cdw0;
     564                 :            : 
     565                 :      45616 :         unmap_ctx->count--;
     566                 :            : 
     567         [ +  - ]:      45616 :         if (response->status.sct == SPDK_NVME_SCT_GENERIC &&
     568         [ +  - ]:      45616 :             response->status.sc == SPDK_NVME_SC_SUCCESS) {
     569                 :      45616 :                 spdk_bdev_io_get_nvme_status(bdev_io, &cdw0, &sct, &sc);
     570                 :      45616 :                 response->cdw0 = cdw0;
     571                 :      45616 :                 response->status.sc = sc;
     572                 :      45616 :                 response->status.sct = sct;
     573                 :            :         }
     574                 :            : 
     575         [ +  - ]:      45616 :         if (unmap_ctx->count == 0) {
     576                 :      45616 :                 spdk_nvmf_request_complete(req);
     577                 :      45616 :                 free(unmap_ctx);
     578                 :            :         }
     579                 :      45616 :         spdk_bdev_free_io(bdev_io);
     580                 :      45616 : }
     581                 :            : 
     582                 :            : static int nvmf_bdev_ctrlr_unmap(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
     583                 :            :                                  struct spdk_io_channel *ch, struct spdk_nvmf_request *req,
     584                 :            :                                  struct nvmf_bdev_ctrlr_unmap *unmap_ctx);
     585                 :            : static void
     586                 :      45584 : nvmf_bdev_ctrlr_unmap_resubmit(void *arg)
     587                 :            : {
     588                 :      45584 :         struct nvmf_bdev_ctrlr_unmap *unmap_ctx = arg;
     589                 :      45584 :         struct spdk_nvmf_request *req = unmap_ctx->req;
     590                 :      45584 :         struct spdk_bdev_desc *desc = unmap_ctx->desc;
     591                 :      45584 :         struct spdk_bdev *bdev = unmap_ctx->bdev;
     592                 :      45584 :         struct spdk_io_channel *ch = unmap_ctx->ch;
     593                 :            : 
     594                 :      45584 :         nvmf_bdev_ctrlr_unmap(bdev, desc, ch, req, unmap_ctx);
     595                 :      45584 : }
     596                 :            : 
     597                 :            : static int
     598                 :      94051 : nvmf_bdev_ctrlr_unmap(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
     599                 :            :                       struct spdk_io_channel *ch, struct spdk_nvmf_request *req,
     600                 :            :                       struct nvmf_bdev_ctrlr_unmap *unmap_ctx)
     601                 :            : {
     602                 :            :         uint16_t nr, i;
     603                 :      94051 :         struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
     604                 :      94051 :         struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
     605                 :      94051 :         uint64_t max_discard_size = req->qpair->ctrlr->subsys->max_discard_size_kib;
     606                 :      94051 :         uint32_t block_size = spdk_bdev_get_block_size(bdev);
     607                 :          0 :         struct spdk_iov_xfer ix;
     608                 :            :         uint64_t lba;
     609                 :            :         uint32_t lba_count;
     610                 :            :         int rc;
     611                 :            : 
     612                 :      94051 :         nr = cmd->cdw10_bits.dsm.nr + 1;
     613         [ +  + ]:      94051 :         if (nr * sizeof(struct spdk_nvme_dsm_range) > req->length) {
     614                 :       2851 :                 SPDK_ERRLOG("Dataset Management number of ranges > SGL length\n");
     615                 :       2851 :                 response->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID;
     616                 :       2851 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     617                 :            :         }
     618                 :            : 
     619         [ +  + ]:      91200 :         if (unmap_ctx == NULL) {
     620                 :      45616 :                 unmap_ctx = calloc(1, sizeof(*unmap_ctx));
     621         [ -  + ]:      45616 :                 if (!unmap_ctx) {
     622                 :          0 :                         response->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
     623                 :          0 :                         return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     624                 :            :                 }
     625                 :            : 
     626                 :      45616 :                 unmap_ctx->req = req;
     627                 :      45616 :                 unmap_ctx->desc = desc;
     628                 :      45616 :                 unmap_ctx->ch = ch;
     629                 :      45616 :                 unmap_ctx->bdev = bdev;
     630                 :            : 
     631                 :      45616 :                 response->status.sct = SPDK_NVME_SCT_GENERIC;
     632                 :      45616 :                 response->status.sc = SPDK_NVME_SC_SUCCESS;
     633                 :            :         } else {
     634                 :      45584 :                 unmap_ctx->count--;  /* dequeued */
     635                 :            :         }
     636                 :            : 
     637                 :      91200 :         spdk_iov_xfer_init(&ix, req->iov, req->iovcnt);
     638                 :            : 
     639         [ +  + ]:     136816 :         for (i = unmap_ctx->range_index; i < nr; i++) {
     640                 :      91200 :                 struct spdk_nvme_dsm_range dsm_range = { 0 };
     641                 :            : 
     642                 :      91200 :                 spdk_iov_xfer_to_buf(&ix, &dsm_range, sizeof(dsm_range));
     643                 :            : 
     644                 :      91200 :                 lba = dsm_range.starting_lba;
     645                 :      91200 :                 lba_count = dsm_range.length;
     646   [ -  +  -  -  :      91200 :                 if (max_discard_size > 0 && lba_count > (max_discard_size << 10) / block_size) {
                   -  - ]
     647                 :          0 :                         SPDK_ERRLOG("invalid unmap size, should not exceed %" PRIu64 "Kib\n", max_discard_size);
     648                 :          0 :                         response->status.sct = SPDK_NVME_SCT_GENERIC;
     649                 :          0 :                         response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
     650                 :      45584 :                         return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     651                 :            :                 }
     652                 :            : 
     653                 :      91200 :                 unmap_ctx->count++;
     654                 :            : 
     655                 :      91200 :                 rc = spdk_bdev_unmap_blocks(desc, ch, lba, lba_count,
     656                 :            :                                             nvmf_bdev_ctrlr_unmap_cpl, unmap_ctx);
     657         [ +  + ]:      91200 :                 if (rc) {
     658         [ +  - ]:      45584 :                         if (rc == -ENOMEM) {
     659                 :      45584 :                                 nvmf_bdev_ctrl_queue_io(req, bdev, ch, nvmf_bdev_ctrlr_unmap_resubmit, unmap_ctx);
     660                 :            :                                 /* Unmap was not yet submitted to bdev */
     661                 :            :                                 /* unmap_ctx->count will be decremented when the request is dequeued */
     662                 :      45584 :                                 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     663                 :            :                         }
     664                 :          0 :                         response->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
     665                 :          0 :                         unmap_ctx->count--;
     666                 :            :                         /* We can't return here - we may have to wait for any other
     667                 :            :                                 * unmaps already sent to complete */
     668                 :          0 :                         break;
     669                 :            :                 }
     670                 :      45616 :                 unmap_ctx->range_index++;
     671                 :            :         }
     672                 :            : 
     673         [ -  + ]:      45616 :         if (unmap_ctx->count == 0) {
     674                 :          0 :                 free(unmap_ctx);
     675                 :          0 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     676                 :            :         }
     677                 :            : 
     678                 :      45616 :         return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     679                 :            : }
     680                 :            : 
     681                 :            : int
     682                 :      51242 : nvmf_bdev_ctrlr_dsm_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
     683                 :            :                         struct spdk_io_channel *ch, struct spdk_nvmf_request *req)
     684                 :            : {
     685                 :      51242 :         struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
     686                 :      51242 :         struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
     687                 :            : 
     688         [ +  + ]:      51242 :         if (cmd->cdw11_bits.dsm.ad) {
     689                 :      48467 :                 return nvmf_bdev_ctrlr_unmap(bdev, desc, ch, req, NULL);
     690                 :            :         }
     691                 :            : 
     692                 :       2775 :         response->status.sct = SPDK_NVME_SCT_GENERIC;
     693                 :       2775 :         response->status.sc = SPDK_NVME_SC_SUCCESS;
     694                 :       2775 :         return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     695                 :            : }
     696                 :            : 
     697                 :            : int
     698                 :       5574 : nvmf_bdev_ctrlr_copy_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
     699                 :            :                          struct spdk_io_channel *ch, struct spdk_nvmf_request *req)
     700                 :            : {
     701                 :       5574 :         struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd;
     702                 :       5574 :         struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl;
     703                 :       5574 :         uint64_t sdlba = ((uint64_t)cmd->cdw11 << 32) + cmd->cdw10;
     704                 :       5574 :         struct spdk_nvme_scc_source_range range = { 0 };
     705                 :         25 :         struct spdk_iov_xfer ix;
     706                 :            :         int rc;
     707                 :            : 
     708   [ -  +  -  + ]:       5574 :         SPDK_DEBUGLOG(nvmf, "Copy command: SDLBA %lu, NR %u, desc format %u, PRINFOR %u, "
     709                 :            :                       "DTYPE %u, STCW %u, PRINFOW %u, FUA %u, LR %u\n",
     710                 :            :                       sdlba,
     711                 :            :                       cmd->cdw12_bits.copy.nr,
     712                 :            :                       cmd->cdw12_bits.copy.df,
     713                 :            :                       cmd->cdw12_bits.copy.prinfor,
     714                 :            :                       cmd->cdw12_bits.copy.dtype,
     715                 :            :                       cmd->cdw12_bits.copy.stcw,
     716                 :            :                       cmd->cdw12_bits.copy.prinfow,
     717                 :            :                       cmd->cdw12_bits.copy.fua,
     718                 :            :                       cmd->cdw12_bits.copy.lr);
     719                 :            : 
     720         [ +  + ]:       5574 :         if (spdk_unlikely(req->length != (cmd->cdw12_bits.copy.nr + 1) *
     721                 :            :                           sizeof(struct spdk_nvme_scc_source_range))) {
     722                 :       5537 :                 response->status.sct = SPDK_NVME_SCT_GENERIC;
     723                 :       5537 :                 response->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID;
     724                 :       5537 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     725                 :            :         }
     726                 :            : 
     727                 :            :         /*
     728                 :            :          * We support only one source range, and rely on this with the xfer
     729                 :            :          * below.
     730                 :            :          */
     731         [ +  + ]:         37 :         if (cmd->cdw12_bits.copy.nr > 0) {
     732                 :          6 :                 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC;
     733                 :          6 :                 response->status.sc = SPDK_NVME_SC_CMD_SIZE_LIMIT_SIZE_EXCEEDED;
     734                 :          6 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     735                 :            :         }
     736                 :            : 
     737         [ +  + ]:         31 :         if (cmd->cdw12_bits.copy.df != 0) {
     738                 :          6 :                 response->status.sct = SPDK_NVME_SCT_GENERIC;
     739                 :          6 :                 response->status.sc = SPDK_NVME_SC_INVALID_FIELD;
     740                 :          6 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     741                 :            :         }
     742                 :            : 
     743                 :         25 :         spdk_iov_xfer_init(&ix, req->iov, req->iovcnt);
     744                 :         25 :         spdk_iov_xfer_to_buf(&ix, &range, sizeof(range));
     745                 :            : 
     746                 :         25 :         rc = spdk_bdev_copy_blocks(desc, ch, sdlba, range.slba, range.nlb + 1,
     747                 :            :                                    nvmf_bdev_ctrlr_complete_cmd, req);
     748         [ +  + ]:         25 :         if (spdk_unlikely(rc)) {
     749         [ -  + ]:          6 :                 if (rc == -ENOMEM) {
     750                 :          0 :                         nvmf_bdev_ctrl_queue_io(req, bdev, ch, nvmf_ctrlr_process_io_cmd_resubmit, req);
     751                 :          0 :                         return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     752                 :            :                 }
     753                 :            : 
     754                 :          6 :                 response->status.sct = SPDK_NVME_SCT_GENERIC;
     755                 :          6 :                 response->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
     756                 :          6 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     757                 :            :         }
     758                 :            : 
     759                 :         19 :         return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     760                 :            : }
     761                 :            : 
     762                 :            : int
     763                 :    1513118 : nvmf_bdev_ctrlr_nvme_passthru_io(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
     764                 :            :                                  struct spdk_io_channel *ch, struct spdk_nvmf_request *req)
     765                 :            : {
     766                 :            :         int rc;
     767                 :            : 
     768                 :    1513118 :         rc = spdk_bdev_nvme_iov_passthru_md(desc, ch, &req->cmd->nvme_cmd, req->iov, req->iovcnt,
     769                 :    1513118 :                                             req->length, NULL, 0, nvmf_bdev_ctrlr_complete_cmd, req);
     770                 :            : 
     771         [ +  + ]:    1513118 :         if (spdk_unlikely(rc)) {
     772         [ +  + ]:    1513106 :                 if (rc == -ENOMEM) {
     773                 :          6 :                         nvmf_bdev_ctrl_queue_io(req, bdev, ch, nvmf_ctrlr_process_io_cmd_resubmit, req);
     774                 :          6 :                         return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     775                 :            :                 }
     776                 :    1513100 :                 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
     777                 :    1513100 :                 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE;
     778                 :    1513100 :                 req->rsp->nvme_cpl.status.dnr = 1;
     779                 :    1513100 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     780                 :            :         }
     781                 :            : 
     782                 :         12 :         return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     783                 :            : }
     784                 :            : 
     785                 :            : int
     786                 :         28 : spdk_nvmf_bdev_ctrlr_nvme_passthru_admin(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
     787                 :            :                 struct spdk_io_channel *ch, struct spdk_nvmf_request *req,
     788                 :            :                 spdk_nvmf_nvme_passthru_cmd_cb cb_fn)
     789                 :            : {
     790                 :            :         int rc;
     791                 :            : 
     792         [ -  + ]:         28 :         if (spdk_unlikely(req->iovcnt > 1)) {
     793                 :          0 :                 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
     794                 :          0 :                 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
     795                 :          0 :                 req->rsp->nvme_cpl.status.dnr = 1;
     796                 :          0 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     797                 :            :         }
     798                 :            : 
     799                 :         28 :         req->cmd_cb_fn = cb_fn;
     800                 :            : 
     801                 :         28 :         rc = spdk_bdev_nvme_admin_passthru(desc, ch, &req->cmd->nvme_cmd, req->iov[0].iov_base, req->length,
     802                 :            :                                            nvmf_bdev_ctrlr_complete_admin_cmd, req);
     803         [ +  + ]:         28 :         if (spdk_unlikely(rc)) {
     804         [ +  + ]:         12 :                 if (rc == -ENOMEM) {
     805                 :          6 :                         nvmf_bdev_ctrl_queue_io(req, bdev, ch, nvmf_ctrlr_process_admin_cmd_resubmit, req);
     806                 :          6 :                         return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     807                 :            :                 }
     808                 :          6 :                 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC;
     809         [ +  - ]:          6 :                 if (rc == -ENOTSUP) {
     810                 :          6 :                         req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE;
     811                 :            :                 } else {
     812                 :          0 :                         req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
     813                 :            :                 }
     814                 :            : 
     815                 :          6 :                 req->rsp->nvme_cpl.status.dnr = 1;
     816                 :          6 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     817                 :            :         }
     818                 :            : 
     819                 :         16 :         return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     820                 :            : }
     821                 :            : 
     822                 :            : static void
     823                 :      49402 : nvmf_bdev_ctrlr_complete_abort_cmd(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
     824                 :            : {
     825                 :      49402 :         struct spdk_nvmf_request *req = cb_arg;
     826                 :            : 
     827         [ +  + ]:      49402 :         if (success) {
     828                 :      46881 :                 req->rsp->nvme_cpl.cdw0 &= ~1U;
     829                 :            :         }
     830                 :            : 
     831                 :      49402 :         spdk_nvmf_request_complete(req);
     832                 :      49402 :         spdk_bdev_free_io(bdev_io);
     833                 :      49402 : }
     834                 :            : 
     835                 :            : int
     836                 :      49402 : spdk_nvmf_bdev_ctrlr_abort_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
     837                 :            :                                struct spdk_io_channel *ch, struct spdk_nvmf_request *req,
     838                 :            :                                struct spdk_nvmf_request *req_to_abort)
     839                 :            : {
     840                 :            :         int rc;
     841                 :            : 
     842         [ -  + ]:      49402 :         assert((req->rsp->nvme_cpl.cdw0 & 1U) != 0);
     843                 :            : 
     844                 :      49402 :         rc = spdk_bdev_abort(desc, ch, req_to_abort, nvmf_bdev_ctrlr_complete_abort_cmd, req);
     845         [ +  - ]:      49402 :         if (spdk_likely(rc == 0)) {
     846                 :      49402 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     847         [ #  # ]:          0 :         } else if (rc == -ENOMEM) {
     848                 :          0 :                 nvmf_bdev_ctrl_queue_io(req, bdev, ch, nvmf_ctrlr_process_admin_cmd_resubmit, req);
     849                 :          0 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     850                 :            :         } else {
     851                 :          0 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     852                 :            :         }
     853                 :            : }
     854                 :            : 
     855                 :            : bool
     856                 :     564786 : nvmf_bdev_ctrlr_get_dif_ctx(struct spdk_bdev *bdev, struct spdk_nvme_cmd *cmd,
     857                 :            :                             struct spdk_dif_ctx *dif_ctx)
     858                 :            : {
     859                 :     564786 :         uint32_t init_ref_tag, dif_check_flags = 0;
     860                 :            :         int rc;
     861                 :         10 :         struct spdk_dif_ctx_init_ext_opts dif_opts;
     862                 :            : 
     863         [ +  + ]:     564786 :         if (spdk_bdev_get_md_size(bdev) == 0) {
     864                 :          6 :                 return false;
     865                 :            :         }
     866                 :            : 
     867                 :            :         /* Initial Reference Tag is the lower 32 bits of the start LBA. */
     868                 :     564780 :         init_ref_tag = (uint32_t)from_le64(&cmd->cdw10);
     869                 :            : 
     870         [ +  + ]:     564780 :         if (spdk_bdev_is_dif_check_enabled(bdev, SPDK_DIF_CHECK_TYPE_REFTAG)) {
     871                 :     535097 :                 dif_check_flags |= SPDK_DIF_FLAGS_REFTAG_CHECK;
     872                 :            :         }
     873                 :            : 
     874         [ +  + ]:     564780 :         if (spdk_bdev_is_dif_check_enabled(bdev, SPDK_DIF_CHECK_TYPE_GUARD)) {
     875                 :     564774 :                 dif_check_flags |= SPDK_DIF_FLAGS_GUARD_CHECK;
     876                 :            :         }
     877                 :            : 
     878                 :     564780 :         dif_opts.size = SPDK_SIZEOF(&dif_opts, dif_pi_format);
     879                 :     564780 :         dif_opts.dif_pi_format = SPDK_DIF_PI_FORMAT_16;
     880                 :    1129560 :         rc = spdk_dif_ctx_init(dif_ctx,
     881                 :            :                                spdk_bdev_get_block_size(bdev),
     882                 :            :                                spdk_bdev_get_md_size(bdev),
     883                 :     564780 :                                spdk_bdev_is_md_interleaved(bdev),
     884                 :     564780 :                                spdk_bdev_is_dif_head_of_md(bdev),
     885                 :            :                                spdk_bdev_get_dif_type(bdev),
     886                 :            :                                dif_check_flags,
     887                 :            :                                init_ref_tag, 0, 0, 0, 0, &dif_opts);
     888                 :            : 
     889                 :     564780 :         return (rc == 0) ? true : false;
     890                 :            : }
     891                 :            : 
     892                 :            : static void
     893                 :     574262 : nvmf_bdev_ctrlr_zcopy_start_complete(struct spdk_bdev_io *bdev_io, bool success,
     894                 :            :                                      void *cb_arg)
     895                 :            : {
     896                 :     574262 :         struct spdk_nvmf_request        *req = cb_arg;
     897                 :          0 :         struct iovec *iov;
     898                 :     574262 :         int iovcnt = 0;
     899                 :            : 
     900         [ +  + ]:     574262 :         if (spdk_unlikely(!success)) {
     901                 :      11264 :                 int                     sc = 0, sct = 0;
     902                 :      11264 :                 uint32_t                cdw0 = 0;
     903                 :      11264 :                 struct spdk_nvme_cpl    *response = &req->rsp->nvme_cpl;
     904                 :      11264 :                 spdk_bdev_io_get_nvme_status(bdev_io, &cdw0, &sct, &sc);
     905                 :            : 
     906                 :      11264 :                 response->cdw0 = cdw0;
     907                 :      11264 :                 response->status.sc = sc;
     908                 :      11264 :                 response->status.sct = sct;
     909                 :            : 
     910                 :      11264 :                 spdk_bdev_free_io(bdev_io);
     911                 :      11264 :                 spdk_nvmf_request_complete(req);
     912                 :      11264 :                 return;
     913                 :            :         }
     914                 :            : 
     915                 :     562998 :         spdk_bdev_io_get_iovec(bdev_io, &iov, &iovcnt);
     916                 :            : 
     917         [ -  + ]:     562998 :         assert(iovcnt <= NVMF_REQ_MAX_BUFFERS);
     918         [ -  + ]:     562998 :         assert(iovcnt > 0);
     919                 :            : 
     920                 :     562998 :         req->iovcnt = iovcnt;
     921                 :            : 
     922         [ -  + ]:     562998 :         assert(req->iov == iov);
     923                 :            : 
     924                 :     562998 :         req->zcopy_bdev_io = bdev_io; /* Preserve the bdev_io for the end zcopy */
     925                 :            : 
     926                 :     562998 :         spdk_nvmf_request_complete(req);
     927                 :            :         /* Don't free the bdev_io here as it is needed for the END ZCOPY */
     928                 :            : }
     929                 :            : 
     930                 :            : int
     931                 :     574280 : nvmf_bdev_ctrlr_zcopy_start(struct spdk_bdev *bdev,
     932                 :            :                             struct spdk_bdev_desc *desc,
     933                 :            :                             struct spdk_io_channel *ch,
     934                 :            :                             struct spdk_nvmf_request *req)
     935                 :            : {
     936                 :     574280 :         struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl;
     937                 :     574280 :         uint64_t bdev_num_blocks = spdk_bdev_get_num_blocks(bdev);
     938                 :     574280 :         uint32_t block_size = spdk_bdev_get_block_size(bdev);
     939                 :         15 :         uint64_t start_lba;
     940                 :         15 :         uint64_t num_blocks;
     941                 :            :         int rc;
     942                 :            : 
     943                 :     574280 :         nvmf_bdev_ctrlr_get_rw_params(&req->cmd->nvme_cmd, &start_lba, &num_blocks);
     944                 :            : 
     945         [ +  + ]:     574280 :         if (spdk_unlikely(!nvmf_bdev_ctrlr_lba_in_range(bdev_num_blocks, start_lba, num_blocks))) {
     946                 :          6 :                 SPDK_ERRLOG("end of media\n");
     947                 :          6 :                 rsp->status.sct = SPDK_NVME_SCT_GENERIC;
     948                 :          6 :                 rsp->status.sc = SPDK_NVME_SC_LBA_OUT_OF_RANGE;
     949                 :          6 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     950                 :            :         }
     951                 :            : 
     952         [ +  + ]:     574274 :         if (spdk_unlikely(num_blocks * block_size > req->length)) {
     953                 :          6 :                 SPDK_ERRLOG("Read NLB %" PRIu64 " * block size %" PRIu32 " > SGL length %" PRIu32 "\n",
     954                 :            :                             num_blocks, block_size, req->length);
     955                 :          6 :                 rsp->status.sct = SPDK_NVME_SCT_GENERIC;
     956                 :          6 :                 rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID;
     957                 :          6 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     958                 :            :         }
     959                 :            : 
     960                 :     574268 :         bool populate = (req->cmd->nvme_cmd.opc == SPDK_NVME_OPC_READ) ? true : false;
     961                 :            : 
     962                 :     574268 :         rc = spdk_bdev_zcopy_start(desc, ch, req->iov, req->iovcnt, start_lba,
     963                 :            :                                    num_blocks, populate, nvmf_bdev_ctrlr_zcopy_start_complete, req);
     964         [ -  + ]:     574268 :         if (spdk_unlikely(rc != 0)) {
     965         [ #  # ]:          0 :                 if (rc == -ENOMEM) {
     966                 :          0 :                         nvmf_bdev_ctrl_queue_io(req, bdev, ch, nvmf_ctrlr_process_io_cmd_resubmit, req);
     967                 :          0 :                         return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     968                 :            :                 }
     969                 :          0 :                 rsp->status.sct = SPDK_NVME_SCT_GENERIC;
     970                 :          0 :                 rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
     971                 :          0 :                 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE;
     972                 :            :         }
     973                 :            : 
     974                 :     574268 :         return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS;
     975                 :            : }
     976                 :            : 
     977                 :            : static void
     978                 :     562998 : nvmf_bdev_ctrlr_zcopy_end_complete(struct spdk_bdev_io *bdev_io, bool success,
     979                 :            :                                    void *cb_arg)
     980                 :            : {
     981                 :     562998 :         struct spdk_nvmf_request        *req = cb_arg;
     982                 :            : 
     983         [ +  + ]:     562998 :         if (spdk_unlikely(!success)) {
     984                 :      10967 :                 int                     sc = 0, sct = 0;
     985                 :      10967 :                 uint32_t                cdw0 = 0;
     986                 :      10967 :                 struct spdk_nvme_cpl    *response = &req->rsp->nvme_cpl;
     987                 :      10967 :                 spdk_bdev_io_get_nvme_status(bdev_io, &cdw0, &sct, &sc);
     988                 :            : 
     989                 :      10967 :                 response->cdw0 = cdw0;
     990                 :      10967 :                 response->status.sc = sc;
     991                 :      10967 :                 response->status.sct = sct;
     992                 :            :         }
     993                 :            : 
     994                 :     562998 :         spdk_bdev_free_io(bdev_io);
     995                 :     562998 :         req->zcopy_bdev_io = NULL;
     996                 :     562998 :         spdk_nvmf_request_complete(req);
     997                 :     562998 : }
     998                 :            : 
     999                 :            : void
    1000                 :     562998 : nvmf_bdev_ctrlr_zcopy_end(struct spdk_nvmf_request *req, bool commit)
    1001                 :            : {
    1002                 :            :         int rc __attribute__((unused));
    1003                 :            : 
    1004                 :     562998 :         rc = spdk_bdev_zcopy_end(req->zcopy_bdev_io, commit, nvmf_bdev_ctrlr_zcopy_end_complete, req);
    1005                 :            : 
    1006                 :            :         /* The only way spdk_bdev_zcopy_end() can fail is if we pass a bdev_io type that isn't ZCOPY */
    1007         [ -  + ]:     562998 :         assert(rc == 0);
    1008                 :     562998 : }

Generated by: LCOV version 1.14