1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk/stdinc.h" 35 36 #include "nvmf_internal.h" 37 38 #include "spdk/bdev.h" 39 #include "spdk/endian.h" 40 #include "spdk/io_channel.h" 41 #include "spdk/likely.h" 42 #include "spdk/nvme.h" 43 #include "spdk/nvmf_spec.h" 44 #include "spdk/trace.h" 45 #include "spdk/scsi_spec.h" 46 #include "spdk/string.h" 47 #include "spdk/util.h" 48 49 #include "spdk_internal/log.h" 50 51 static bool 52 spdk_nvmf_subsystem_bdev_io_type_supported(struct spdk_nvmf_subsystem *subsystem, 53 enum spdk_bdev_io_type io_type) 54 { 55 struct spdk_nvmf_ns *ns; 56 57 for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL; 58 ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) { 59 if (ns->bdev == NULL) { 60 continue; 61 } 62 63 if (!spdk_bdev_io_type_supported(ns->bdev, io_type)) { 64 SPDK_DEBUGLOG(SPDK_LOG_NVMF, 65 "Subsystem %s namespace %u (%s) does not support io_type %d\n", 66 spdk_nvmf_subsystem_get_nqn(subsystem), 67 ns->opts.nsid, spdk_bdev_get_name(ns->bdev), (int)io_type); 68 return false; 69 } 70 } 71 72 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "All devices in Subsystem %s support io_type %d\n", 73 spdk_nvmf_subsystem_get_nqn(subsystem), (int)io_type); 74 return true; 75 } 76 77 bool 78 spdk_nvmf_ctrlr_dsm_supported(struct spdk_nvmf_ctrlr *ctrlr) 79 { 80 return spdk_nvmf_subsystem_bdev_io_type_supported(ctrlr->subsys, SPDK_BDEV_IO_TYPE_UNMAP); 81 } 82 83 bool 84 spdk_nvmf_ctrlr_write_zeroes_supported(struct spdk_nvmf_ctrlr *ctrlr) 85 { 86 return spdk_nvmf_subsystem_bdev_io_type_supported(ctrlr->subsys, SPDK_BDEV_IO_TYPE_WRITE_ZEROES); 87 } 88 89 static void 90 nvmf_bdev_ctrlr_complete_cmd(struct spdk_bdev_io *bdev_io, bool success, 91 void *cb_arg) 92 { 93 struct spdk_nvmf_request *req = cb_arg; 94 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 95 int sc, sct; 96 97 spdk_bdev_io_get_nvme_status(bdev_io, &sct, &sc); 98 response->status.sc = sc; 99 response->status.sct = sct; 100 101 spdk_nvmf_request_complete(req); 102 spdk_bdev_free_io(bdev_io); 103 } 104 105 int 106 spdk_nvmf_bdev_ctrlr_identify_ns(struct spdk_nvmf_ns *ns, struct spdk_nvme_ns_data *nsdata) 107 { 108 struct spdk_bdev *bdev = ns->bdev; 109 uint64_t num_blocks; 110 111 num_blocks = spdk_bdev_get_num_blocks(bdev); 112 113 nsdata->nsze = num_blocks; 114 nsdata->ncap = num_blocks; 115 nsdata->nuse = num_blocks; 116 nsdata->nlbaf = 0; 117 nsdata->flbas.format = 0; 118 nsdata->lbaf[0].lbads = spdk_u32log2(spdk_bdev_get_block_size(bdev)); 119 nsdata->noiob = spdk_bdev_get_optimal_io_boundary(bdev); 120 nsdata->nmic.can_share = 1; 121 122 SPDK_STATIC_ASSERT(sizeof(nsdata->nguid) == sizeof(ns->opts.nguid), "size mismatch"); 123 memcpy(nsdata->nguid, ns->opts.nguid, sizeof(nsdata->nguid)); 124 125 SPDK_STATIC_ASSERT(sizeof(nsdata->eui64) == sizeof(ns->opts.eui64), "size mismatch"); 126 memcpy(&nsdata->eui64, ns->opts.eui64, sizeof(nsdata->eui64)); 127 128 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 129 } 130 131 static void 132 nvmf_bdev_ctrlr_get_rw_params(const struct spdk_nvme_cmd *cmd, uint64_t *start_lba, 133 uint64_t *num_blocks) 134 { 135 /* SLBA: CDW10 and CDW11 */ 136 *start_lba = from_le64(&cmd->cdw10); 137 138 /* NLB: CDW12 bits 15:00, 0's based */ 139 *num_blocks = (from_le32(&cmd->cdw12) & 0xFFFFu) + 1; 140 } 141 142 static bool 143 nvmf_bdev_ctrlr_lba_in_range(uint64_t bdev_num_blocks, uint64_t io_start_lba, 144 uint64_t io_num_blocks) 145 { 146 if (io_start_lba + io_num_blocks > bdev_num_blocks || 147 io_start_lba + io_num_blocks < io_start_lba) { 148 return false; 149 } 150 151 return true; 152 } 153 154 static int 155 nvmf_bdev_ctrlr_read_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 156 struct spdk_io_channel *ch, struct spdk_nvmf_request *req) 157 { 158 uint64_t bdev_num_blocks = spdk_bdev_get_num_blocks(bdev); 159 uint32_t block_size = spdk_bdev_get_block_size(bdev); 160 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 161 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 162 uint64_t start_lba; 163 uint64_t num_blocks; 164 165 nvmf_bdev_ctrlr_get_rw_params(cmd, &start_lba, &num_blocks); 166 167 if (spdk_unlikely(!nvmf_bdev_ctrlr_lba_in_range(bdev_num_blocks, start_lba, num_blocks))) { 168 SPDK_ERRLOG("end of media\n"); 169 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 170 rsp->status.sc = SPDK_NVME_SC_LBA_OUT_OF_RANGE; 171 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 172 } 173 174 if (spdk_unlikely(num_blocks * block_size > req->length)) { 175 SPDK_ERRLOG("Read NLB %" PRIu64 " * block size %" PRIu32 " > SGL length %" PRIu32 "\n", 176 num_blocks, block_size, req->length); 177 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 178 rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID; 179 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 180 } 181 182 spdk_trace_record(TRACE_NVMF_LIB_READ_START, 0, 0, (uint64_t)req, 0); 183 if (spdk_unlikely(spdk_bdev_read_blocks(desc, ch, req->data, start_lba, num_blocks, 184 nvmf_bdev_ctrlr_complete_cmd, req))) { 185 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 186 rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 187 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 188 } 189 190 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 191 } 192 193 static int 194 nvmf_bdev_ctrlr_write_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 195 struct spdk_io_channel *ch, struct spdk_nvmf_request *req) 196 { 197 uint64_t bdev_num_blocks = spdk_bdev_get_num_blocks(bdev); 198 uint32_t block_size = spdk_bdev_get_block_size(bdev); 199 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 200 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 201 uint64_t start_lba; 202 uint64_t num_blocks; 203 204 nvmf_bdev_ctrlr_get_rw_params(cmd, &start_lba, &num_blocks); 205 206 if (spdk_unlikely(!nvmf_bdev_ctrlr_lba_in_range(bdev_num_blocks, start_lba, num_blocks))) { 207 SPDK_ERRLOG("end of media\n"); 208 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 209 rsp->status.sc = SPDK_NVME_SC_LBA_OUT_OF_RANGE; 210 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 211 } 212 213 if (spdk_unlikely(num_blocks * block_size > req->length)) { 214 SPDK_ERRLOG("Write NLB %" PRIu64 " * block size %" PRIu32 " > SGL length %" PRIu32 "\n", 215 num_blocks, block_size, req->length); 216 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 217 rsp->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID; 218 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 219 } 220 221 spdk_trace_record(TRACE_NVMF_LIB_WRITE_START, 0, 0, (uint64_t)req, 0); 222 if (spdk_unlikely(spdk_bdev_write_blocks(desc, ch, req->data, start_lba, num_blocks, 223 nvmf_bdev_ctrlr_complete_cmd, req))) { 224 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 225 rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 226 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 227 } 228 229 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 230 } 231 232 static int 233 nvmf_bdev_ctrlr_write_zeroes_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 234 struct spdk_io_channel *ch, struct spdk_nvmf_request *req) 235 { 236 uint64_t bdev_num_blocks = spdk_bdev_get_num_blocks(bdev); 237 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 238 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 239 uint64_t start_lba; 240 uint64_t num_blocks; 241 242 nvmf_bdev_ctrlr_get_rw_params(cmd, &start_lba, &num_blocks); 243 244 if (spdk_unlikely(!nvmf_bdev_ctrlr_lba_in_range(bdev_num_blocks, start_lba, num_blocks))) { 245 SPDK_ERRLOG("end of media\n"); 246 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 247 rsp->status.sc = SPDK_NVME_SC_LBA_OUT_OF_RANGE; 248 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 249 } 250 251 spdk_trace_record(TRACE_NVMF_LIB_WRITE_START, 0, 0, (uint64_t)req, 0); 252 if (spdk_unlikely(spdk_bdev_write_zeroes_blocks(desc, ch, start_lba, num_blocks, 253 nvmf_bdev_ctrlr_complete_cmd, req))) { 254 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 255 rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 256 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 257 } 258 259 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 260 } 261 262 static int 263 nvmf_bdev_ctrlr_flush_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 264 struct spdk_io_channel *ch, struct spdk_nvmf_request *req) 265 { 266 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 267 268 /* As for NVMeoF controller, SPDK always set volatile write 269 * cache bit to 1, return success for those block devices 270 * which can't support FLUSH command. 271 */ 272 if (!spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_FLUSH)) { 273 response->status.sct = SPDK_NVME_SCT_GENERIC; 274 response->status.sc = SPDK_NVME_SC_SUCCESS; 275 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 276 } 277 278 if (spdk_bdev_flush_blocks(desc, ch, 0, spdk_bdev_get_num_blocks(bdev), 279 nvmf_bdev_ctrlr_complete_cmd, req)) { 280 response->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 281 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 282 } 283 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 284 } 285 286 struct nvmf_virtual_ctrlr_unmap { 287 struct spdk_nvmf_request *req; 288 uint32_t count; 289 }; 290 291 static void 292 nvmf_virtual_ctrlr_dsm_cpl(struct spdk_bdev_io *bdev_io, bool success, 293 void *cb_arg) 294 { 295 struct nvmf_virtual_ctrlr_unmap *unmap_ctx = cb_arg; 296 struct spdk_nvmf_request *req = unmap_ctx->req; 297 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 298 int sc, sct; 299 300 unmap_ctx->count--; 301 302 if (response->status.sct == SPDK_NVME_SCT_GENERIC && 303 response->status.sc == SPDK_NVME_SC_SUCCESS) { 304 spdk_bdev_io_get_nvme_status(bdev_io, &sct, &sc); 305 response->status.sc = sc; 306 response->status.sct = sct; 307 } 308 309 if (unmap_ctx->count == 0) { 310 spdk_nvmf_request_complete(req); 311 free(unmap_ctx); 312 } 313 spdk_bdev_free_io(bdev_io); 314 } 315 316 static int 317 nvmf_bdev_ctrlr_dsm_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 318 struct spdk_io_channel *ch, struct spdk_nvmf_request *req) 319 { 320 uint32_t attribute; 321 uint16_t nr, i; 322 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 323 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 324 325 nr = ((cmd->cdw10 & 0x000000ff) + 1); 326 if (nr * sizeof(struct spdk_nvme_dsm_range) > req->length) { 327 SPDK_ERRLOG("Dataset Management number of ranges > SGL length\n"); 328 response->status.sc = SPDK_NVME_SC_DATA_SGL_LENGTH_INVALID; 329 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 330 } 331 332 attribute = cmd->cdw11 & 0x00000007; 333 if (attribute & SPDK_NVME_DSM_ATTR_DEALLOCATE) { 334 struct nvmf_virtual_ctrlr_unmap *unmap_ctx; 335 struct spdk_nvme_dsm_range *dsm_range; 336 uint64_t lba; 337 uint32_t lba_count; 338 339 unmap_ctx = calloc(1, sizeof(*unmap_ctx)); 340 if (!unmap_ctx) { 341 response->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 342 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 343 } 344 345 unmap_ctx->req = req; 346 347 response->status.sct = SPDK_NVME_SCT_GENERIC; 348 response->status.sc = SPDK_NVME_SC_SUCCESS; 349 350 dsm_range = (struct spdk_nvme_dsm_range *)req->data; 351 for (i = 0; i < nr; i++) { 352 lba = dsm_range[i].starting_lba; 353 lba_count = dsm_range[i].length; 354 355 unmap_ctx->count++; 356 357 if (spdk_bdev_unmap_blocks(desc, ch, lba, lba_count, 358 nvmf_virtual_ctrlr_dsm_cpl, unmap_ctx)) { 359 response->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 360 unmap_ctx->count--; 361 /* We can't return here - we may have to wait for any other 362 * unmaps already sent to complete */ 363 break; 364 } 365 } 366 367 if (unmap_ctx->count == 0) { 368 free(unmap_ctx); 369 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 370 } 371 372 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 373 } 374 375 response->status.sct = SPDK_NVME_SCT_GENERIC; 376 response->status.sc = SPDK_NVME_SC_SUCCESS; 377 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 378 } 379 380 static int 381 nvmf_bdev_ctrlr_nvme_passthru_io(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 382 struct spdk_io_channel *ch, struct spdk_nvmf_request *req) 383 { 384 if (spdk_bdev_nvme_io_passthru(desc, ch, &req->cmd->nvme_cmd, req->data, req->length, 385 nvmf_bdev_ctrlr_complete_cmd, req)) { 386 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 387 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE; 388 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 389 } 390 391 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 392 } 393 394 int 395 spdk_nvmf_ctrlr_process_io_cmd(struct spdk_nvmf_request *req) 396 { 397 uint32_t nsid; 398 struct spdk_nvmf_ns *ns; 399 struct spdk_bdev *bdev; 400 struct spdk_bdev_desc *desc; 401 struct spdk_io_channel *ch; 402 struct spdk_nvmf_poll_group *group = req->qpair->group; 403 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 404 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 405 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 406 407 /* pre-set response details for this command */ 408 response->status.sc = SPDK_NVME_SC_SUCCESS; 409 nsid = cmd->nsid; 410 411 if (spdk_unlikely(ctrlr == NULL)) { 412 SPDK_ERRLOG("I/O command sent before CONNECT\n"); 413 response->status.sct = SPDK_NVME_SCT_GENERIC; 414 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 415 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 416 } 417 418 if (spdk_unlikely(ctrlr->vcprop.cc.bits.en != 1)) { 419 SPDK_ERRLOG("I/O command sent to disabled controller\n"); 420 response->status.sct = SPDK_NVME_SCT_GENERIC; 421 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 422 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 423 } 424 425 ns = _spdk_nvmf_subsystem_get_ns(ctrlr->subsys, nsid); 426 if (ns == NULL || ns->bdev == NULL) { 427 SPDK_ERRLOG("Unsuccessful query for nsid %u\n", cmd->nsid); 428 response->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 429 response->status.dnr = 1; 430 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 431 } 432 433 bdev = ns->bdev; 434 desc = ns->desc; 435 ch = group->sgroups[ctrlr->subsys->id].channels[nsid - 1]; 436 switch (cmd->opc) { 437 case SPDK_NVME_OPC_READ: 438 return nvmf_bdev_ctrlr_read_cmd(bdev, desc, ch, req); 439 case SPDK_NVME_OPC_WRITE: 440 return nvmf_bdev_ctrlr_write_cmd(bdev, desc, ch, req); 441 case SPDK_NVME_OPC_WRITE_ZEROES: 442 return nvmf_bdev_ctrlr_write_zeroes_cmd(bdev, desc, ch, req); 443 case SPDK_NVME_OPC_FLUSH: 444 return nvmf_bdev_ctrlr_flush_cmd(bdev, desc, ch, req); 445 case SPDK_NVME_OPC_DATASET_MANAGEMENT: 446 return nvmf_bdev_ctrlr_dsm_cmd(bdev, desc, ch, req); 447 default: 448 return nvmf_bdev_ctrlr_nvme_passthru_io(bdev, desc, ch, req); 449 } 450 } 451