1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (C) 2012-2013 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 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/param.h> 30 #include <sys/bio.h> 31 #include <sys/bus.h> 32 #include <sys/conf.h> 33 #include <sys/disk.h> 34 #include <sys/fcntl.h> 35 #include <sys/ioccom.h> 36 #include <sys/malloc.h> 37 #include <sys/module.h> 38 #include <sys/proc.h> 39 #include <sys/systm.h> 40 41 #include <dev/pci/pcivar.h> 42 43 #include <geom/geom.h> 44 45 #include "nvme_private.h" 46 #include "nvme_linux.h" 47 48 static void nvme_bio_child_inbed(struct bio *parent, int bio_error); 49 static void nvme_bio_child_done(void *arg, 50 const struct nvme_completion *cpl); 51 static uint32_t nvme_get_num_segments(uint64_t addr, uint64_t size, 52 uint32_t alignment); 53 static void nvme_free_child_bios(int num_bios, 54 struct bio **child_bios); 55 static struct bio ** nvme_allocate_child_bios(int num_bios); 56 static struct bio ** nvme_construct_child_bios(struct bio *bp, 57 uint32_t alignment, 58 int *num_bios); 59 static int nvme_ns_split_bio(struct nvme_namespace *ns, 60 struct bio *bp, 61 uint32_t alignment); 62 63 static int 64 nvme_ns_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int flag, 65 struct thread *td) 66 { 67 struct nvme_namespace *ns; 68 struct nvme_controller *ctrlr; 69 struct nvme_pt_command *pt; 70 71 ns = cdev->si_drv1; 72 ctrlr = ns->ctrlr; 73 74 switch (cmd) { 75 case NVME_IO_TEST: 76 case NVME_BIO_TEST: 77 nvme_ns_test(ns, cmd, arg); 78 break; 79 case NVME_PASSTHROUGH_CMD: 80 pt = (struct nvme_pt_command *)arg; 81 return (nvme_ctrlr_passthrough_cmd(ctrlr, pt, ns->id, 82 1 /* is_user_buffer */, 0 /* is_admin_cmd */)); 83 case NVME_GET_NSID: 84 { 85 struct nvme_get_nsid *gnsid = (struct nvme_get_nsid *)arg; 86 strlcpy(gnsid->cdev, device_get_nameunit(ctrlr->dev), 87 sizeof(gnsid->cdev)); 88 gnsid->nsid = ns->id; 89 break; 90 } 91 case DIOCGIDENT: { 92 uint8_t *sn = arg; 93 nvme_ctrlr_get_ident(ctrlr, sn); 94 break; 95 } 96 case DIOCGMEDIASIZE: 97 *(off_t *)arg = (off_t)nvme_ns_get_size(ns); 98 break; 99 case DIOCGSECTORSIZE: 100 *(u_int *)arg = nvme_ns_get_sector_size(ns); 101 break; 102 /* Linux Compatible (see nvme_linux.h) */ 103 case NVME_IOCTL_ID: 104 td->td_retval[0] = ns->id; 105 return (0); 106 107 case NVME_IOCTL_ADMIN_CMD: 108 case NVME_IOCTL_IO_CMD: { 109 struct nvme_passthru_cmd *npc = (struct nvme_passthru_cmd *)arg; 110 111 return (nvme_ctrlr_linux_passthru_cmd(ctrlr, npc, ns->id, true, 112 cmd == NVME_IOCTL_ADMIN_CMD)); 113 } 114 default: 115 return (ENOTTY); 116 } 117 118 return (0); 119 } 120 121 static int 122 nvme_ns_open(struct cdev *dev __unused, int flags, int fmt __unused, 123 struct thread *td) 124 { 125 int error = 0; 126 127 if (flags & FWRITE) 128 error = securelevel_gt(td->td_ucred, 0); 129 130 return (error); 131 } 132 133 static int 134 nvme_ns_close(struct cdev *dev __unused, int flags, int fmt __unused, 135 struct thread *td) 136 { 137 return (0); 138 } 139 140 static void 141 nvme_ns_strategy_done(void *arg, const struct nvme_completion *cpl) 142 { 143 struct bio *bp = arg; 144 145 /* 146 * TODO: add more extensive translation of NVMe status codes 147 * to different bio error codes (i.e. EIO, EINVAL, etc.) 148 */ 149 if (nvme_completion_is_error(cpl)) { 150 bp->bio_error = EIO; 151 bp->bio_flags |= BIO_ERROR; 152 bp->bio_resid = bp->bio_bcount; 153 } else 154 bp->bio_resid = 0; 155 156 biodone(bp); 157 } 158 159 static void 160 nvme_ns_strategy(struct bio *bp) 161 { 162 struct nvme_namespace *ns; 163 int err; 164 165 ns = bp->bio_dev->si_drv1; 166 err = nvme_ns_bio_process(ns, bp, nvme_ns_strategy_done); 167 168 if (err) { 169 bp->bio_error = err; 170 bp->bio_flags |= BIO_ERROR; 171 bp->bio_resid = bp->bio_bcount; 172 biodone(bp); 173 } 174 175 } 176 177 static struct cdevsw nvme_ns_cdevsw = { 178 .d_version = D_VERSION, 179 .d_flags = D_DISK, 180 .d_read = physread, 181 .d_write = physwrite, 182 .d_open = nvme_ns_open, 183 .d_close = nvme_ns_close, 184 .d_strategy = nvme_ns_strategy, 185 .d_ioctl = nvme_ns_ioctl 186 }; 187 188 uint32_t 189 nvme_ns_get_max_io_xfer_size(struct nvme_namespace *ns) 190 { 191 return ns->ctrlr->max_xfer_size; 192 } 193 194 uint32_t 195 nvme_ns_get_sector_size(struct nvme_namespace *ns) 196 { 197 uint8_t flbas_fmt, lbads; 198 199 flbas_fmt = NVMEV(NVME_NS_DATA_FLBAS_FORMAT, ns->data.flbas); 200 lbads = NVMEV(NVME_NS_DATA_LBAF_LBADS, ns->data.lbaf[flbas_fmt]); 201 202 return (1 << lbads); 203 } 204 205 uint64_t 206 nvme_ns_get_num_sectors(struct nvme_namespace *ns) 207 { 208 return (ns->data.nsze); 209 } 210 211 uint64_t 212 nvme_ns_get_size(struct nvme_namespace *ns) 213 { 214 return (nvme_ns_get_num_sectors(ns) * nvme_ns_get_sector_size(ns)); 215 } 216 217 uint32_t 218 nvme_ns_get_flags(struct nvme_namespace *ns) 219 { 220 return (ns->flags); 221 } 222 223 const char * 224 nvme_ns_get_serial_number(struct nvme_namespace *ns) 225 { 226 return ((const char *)ns->ctrlr->cdata.sn); 227 } 228 229 const char * 230 nvme_ns_get_model_number(struct nvme_namespace *ns) 231 { 232 return ((const char *)ns->ctrlr->cdata.mn); 233 } 234 235 const struct nvme_namespace_data * 236 nvme_ns_get_data(struct nvme_namespace *ns) 237 { 238 return (&ns->data); 239 } 240 241 uint32_t 242 nvme_ns_get_stripesize(struct nvme_namespace *ns) 243 { 244 uint32_t ss; 245 246 if (NVMEV(NVME_NS_DATA_NSFEAT_NPVALID, ns->data.nsfeat) != 0) { 247 ss = nvme_ns_get_sector_size(ns); 248 if (ns->data.npwa != 0) 249 return ((ns->data.npwa + 1) * ss); 250 else if (ns->data.npwg != 0) 251 return ((ns->data.npwg + 1) * ss); 252 } 253 return (ns->boundary); 254 } 255 256 static void 257 nvme_ns_bio_done(void *arg, const struct nvme_completion *status) 258 { 259 struct bio *bp = arg; 260 nvme_cb_fn_t bp_cb_fn; 261 262 bp_cb_fn = bp->bio_driver1; 263 264 if (bp->bio_driver2) 265 free(bp->bio_driver2, M_NVME); 266 267 if (nvme_completion_is_error(status)) { 268 bp->bio_flags |= BIO_ERROR; 269 if (bp->bio_error == 0) 270 bp->bio_error = EIO; 271 } 272 273 if ((bp->bio_flags & BIO_ERROR) == 0) 274 bp->bio_resid = 0; 275 else 276 bp->bio_resid = bp->bio_bcount; 277 278 bp_cb_fn(bp, status); 279 } 280 281 static void 282 nvme_bio_child_inbed(struct bio *parent, int bio_error) 283 { 284 struct nvme_completion parent_cpl; 285 int children, inbed; 286 287 if (bio_error != 0) { 288 parent->bio_flags |= BIO_ERROR; 289 parent->bio_error = bio_error; 290 } 291 292 /* 293 * atomic_fetchadd will return value before adding 1, so we still 294 * must add 1 to get the updated inbed number. Save bio_children 295 * before incrementing to guard against race conditions when 296 * two children bios complete on different queues. 297 */ 298 children = atomic_load_acq_int(&parent->bio_children); 299 inbed = atomic_fetchadd_int(&parent->bio_inbed, 1) + 1; 300 if (inbed == children) { 301 bzero(&parent_cpl, sizeof(parent_cpl)); 302 if (parent->bio_flags & BIO_ERROR) { 303 parent_cpl.status &= ~NVMEM(NVME_STATUS_SC); 304 parent_cpl.status |= NVMEF(NVME_STATUS_SC, 305 NVME_SC_DATA_TRANSFER_ERROR); 306 } 307 nvme_ns_bio_done(parent, &parent_cpl); 308 } 309 } 310 311 static void 312 nvme_bio_child_done(void *arg, const struct nvme_completion *cpl) 313 { 314 struct bio *child = arg; 315 struct bio *parent; 316 int bio_error; 317 318 parent = child->bio_parent; 319 g_destroy_bio(child); 320 bio_error = nvme_completion_is_error(cpl) ? EIO : 0; 321 nvme_bio_child_inbed(parent, bio_error); 322 } 323 324 static uint32_t 325 nvme_get_num_segments(uint64_t addr, uint64_t size, uint32_t align) 326 { 327 uint32_t num_segs, offset, remainder; 328 329 if (align == 0) 330 return (1); 331 332 KASSERT((align & (align - 1)) == 0, ("alignment not power of 2\n")); 333 334 num_segs = size / align; 335 remainder = size & (align - 1); 336 offset = addr & (align - 1); 337 if (remainder > 0 || offset > 0) 338 num_segs += 1 + (remainder + offset - 1) / align; 339 return (num_segs); 340 } 341 342 static void 343 nvme_free_child_bios(int num_bios, struct bio **child_bios) 344 { 345 int i; 346 347 for (i = 0; i < num_bios; i++) { 348 if (child_bios[i] != NULL) 349 g_destroy_bio(child_bios[i]); 350 } 351 352 free(child_bios, M_NVME); 353 } 354 355 static struct bio ** 356 nvme_allocate_child_bios(int num_bios) 357 { 358 struct bio **child_bios; 359 int err = 0, i; 360 361 child_bios = malloc(num_bios * sizeof(struct bio *), M_NVME, M_NOWAIT); 362 if (child_bios == NULL) 363 return (NULL); 364 365 for (i = 0; i < num_bios; i++) { 366 child_bios[i] = g_new_bio(); 367 if (child_bios[i] == NULL) 368 err = ENOMEM; 369 } 370 371 if (err == ENOMEM) { 372 nvme_free_child_bios(num_bios, child_bios); 373 return (NULL); 374 } 375 376 return (child_bios); 377 } 378 379 static struct bio ** 380 nvme_construct_child_bios(struct bio *bp, uint32_t alignment, int *num_bios) 381 { 382 struct bio **child_bios; 383 struct bio *child; 384 uint64_t cur_offset; 385 caddr_t data; 386 uint32_t rem_bcount; 387 int i; 388 struct vm_page **ma; 389 uint32_t ma_offset; 390 391 *num_bios = nvme_get_num_segments(bp->bio_offset, bp->bio_bcount, 392 alignment); 393 child_bios = nvme_allocate_child_bios(*num_bios); 394 if (child_bios == NULL) 395 return (NULL); 396 397 bp->bio_children = *num_bios; 398 bp->bio_inbed = 0; 399 cur_offset = bp->bio_offset; 400 rem_bcount = bp->bio_bcount; 401 data = bp->bio_data; 402 ma_offset = bp->bio_ma_offset; 403 ma = bp->bio_ma; 404 405 for (i = 0; i < *num_bios; i++) { 406 child = child_bios[i]; 407 child->bio_parent = bp; 408 child->bio_cmd = bp->bio_cmd; 409 child->bio_offset = cur_offset; 410 child->bio_bcount = min(rem_bcount, 411 alignment - (cur_offset & (alignment - 1))); 412 child->bio_flags = bp->bio_flags; 413 if (bp->bio_flags & BIO_UNMAPPED) { 414 child->bio_ma_offset = ma_offset; 415 child->bio_ma = ma; 416 child->bio_ma_n = 417 nvme_get_num_segments(child->bio_ma_offset, 418 child->bio_bcount, PAGE_SIZE); 419 ma_offset = (ma_offset + child->bio_bcount) & 420 PAGE_MASK; 421 ma += child->bio_ma_n; 422 if (ma_offset != 0) 423 ma -= 1; 424 } else { 425 child->bio_data = data; 426 data += child->bio_bcount; 427 } 428 cur_offset += child->bio_bcount; 429 rem_bcount -= child->bio_bcount; 430 } 431 432 return (child_bios); 433 } 434 435 static int 436 nvme_ns_split_bio(struct nvme_namespace *ns, struct bio *bp, 437 uint32_t alignment) 438 { 439 struct bio *child; 440 struct bio **child_bios; 441 int err, i, num_bios; 442 443 child_bios = nvme_construct_child_bios(bp, alignment, &num_bios); 444 if (child_bios == NULL) 445 return (ENOMEM); 446 447 counter_u64_add(ns->ctrlr->alignment_splits, 1); 448 for (i = 0; i < num_bios; i++) { 449 child = child_bios[i]; 450 err = nvme_ns_bio_process(ns, child, nvme_bio_child_done); 451 if (err != 0) { 452 nvme_bio_child_inbed(bp, err); 453 g_destroy_bio(child); 454 } 455 } 456 457 free(child_bios, M_NVME); 458 return (0); 459 } 460 461 int 462 nvme_ns_bio_process(struct nvme_namespace *ns, struct bio *bp, 463 nvme_cb_fn_t cb_fn) 464 { 465 struct nvme_dsm_range *dsm_range; 466 uint32_t num_bios; 467 int err; 468 469 bp->bio_driver1 = cb_fn; 470 471 if (ns->boundary > 0 && 472 (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) { 473 num_bios = nvme_get_num_segments(bp->bio_offset, 474 bp->bio_bcount, ns->boundary); 475 if (num_bios > 1) 476 return (nvme_ns_split_bio(ns, bp, ns->boundary)); 477 } 478 479 switch (bp->bio_cmd) { 480 case BIO_READ: 481 err = nvme_ns_cmd_read_bio(ns, bp, nvme_ns_bio_done, bp); 482 break; 483 case BIO_WRITE: 484 err = nvme_ns_cmd_write_bio(ns, bp, nvme_ns_bio_done, bp); 485 break; 486 case BIO_FLUSH: 487 err = nvme_ns_cmd_flush(ns, nvme_ns_bio_done, bp); 488 break; 489 case BIO_DELETE: 490 dsm_range = 491 malloc(sizeof(struct nvme_dsm_range), M_NVME, 492 M_ZERO | M_NOWAIT); 493 if (!dsm_range) { 494 err = ENOMEM; 495 break; 496 } 497 dsm_range->length = 498 htole32(bp->bio_bcount/nvme_ns_get_sector_size(ns)); 499 dsm_range->starting_lba = 500 htole64(bp->bio_offset/nvme_ns_get_sector_size(ns)); 501 bp->bio_driver2 = dsm_range; 502 err = nvme_ns_cmd_deallocate(ns, dsm_range, 1, 503 nvme_ns_bio_done, bp); 504 if (err != 0) 505 free(dsm_range, M_NVME); 506 break; 507 default: 508 err = EOPNOTSUPP; 509 break; 510 } 511 512 return (err); 513 } 514 515 int 516 nvme_ns_ioctl_process(struct nvme_namespace *ns, u_long cmd, caddr_t arg, 517 int flag, struct thread *td) 518 { 519 return (nvme_ns_ioctl(ns->cdev, cmd, arg, flag, td)); 520 } 521 522 int 523 nvme_ns_construct(struct nvme_namespace *ns, uint32_t id, 524 struct nvme_controller *ctrlr) 525 { 526 struct make_dev_args md_args; 527 struct nvme_completion_poll_status status; 528 int res; 529 int unit; 530 uint8_t flbas_fmt; 531 uint8_t vwc_present; 532 533 ns->ctrlr = ctrlr; 534 ns->id = id; 535 536 /* 537 * Namespaces are reconstructed after a controller reset, so check 538 * to make sure we only call mtx_init once on each mtx. 539 * 540 * TODO: Move this somewhere where it gets called at controller 541 * construction time, which is not invoked as part of each 542 * controller reset. 543 */ 544 if (!mtx_initialized(&ns->lock)) 545 mtx_init(&ns->lock, "nvme ns lock", NULL, MTX_DEF); 546 547 status.done = 0; 548 nvme_ctrlr_cmd_identify_namespace(ctrlr, id, &ns->data, 549 nvme_completion_poll_cb, &status); 550 nvme_completion_poll(&status); 551 if (nvme_completion_is_error(&status.cpl)) { 552 nvme_printf(ctrlr, "nvme_identify_namespace failed\n"); 553 return (ENXIO); 554 } 555 556 /* Convert data to host endian */ 557 nvme_namespace_data_swapbytes(&ns->data); 558 559 /* 560 * If the size of is zero, chances are this isn't a valid 561 * namespace (eg one that's not been configured yet). The 562 * standard says the entire id will be zeros, so this is a 563 * cheap way to test for that. 564 */ 565 if (ns->data.nsze == 0) 566 return (ENXIO); 567 568 flbas_fmt = NVMEV(NVME_NS_DATA_FLBAS_FORMAT, ns->data.flbas); 569 570 /* 571 * Note: format is a 0-based value, so > is appropriate here, 572 * not >=. 573 */ 574 if (flbas_fmt > ns->data.nlbaf) { 575 nvme_printf(ctrlr, 576 "lba format %d exceeds number supported (%d)\n", 577 flbas_fmt, ns->data.nlbaf + 1); 578 return (ENXIO); 579 } 580 581 /* 582 * Older Intel devices (like the PC35xxx and P45xx series) advertise in 583 * vendor specific space an alignment that improves performance. If 584 * present use for the stripe size. NVMe 1.3 standardized this as 585 * NOIOB, and newer Intel drives use that. 586 */ 587 if ((ctrlr->quirks & QUIRK_INTEL_ALIGNMENT) != 0) { 588 if (ctrlr->cdata.vs[3] != 0) 589 ns->boundary = 590 1 << (ctrlr->cdata.vs[3] + NVME_MPS_SHIFT + 591 NVME_CAP_HI_MPSMIN(ctrlr->cap_hi)); 592 else 593 ns->boundary = 0; 594 } else { 595 ns->boundary = ns->data.noiob * nvme_ns_get_sector_size(ns); 596 } 597 598 if (nvme_ctrlr_has_dataset_mgmt(&ctrlr->cdata)) 599 ns->flags |= NVME_NS_DEALLOCATE_SUPPORTED; 600 601 vwc_present = NVMEV(NVME_CTRLR_DATA_VWC_PRESENT, ctrlr->cdata.vwc); 602 if (vwc_present) 603 ns->flags |= NVME_NS_FLUSH_SUPPORTED; 604 605 /* 606 * cdev may have already been created, if we are reconstructing the 607 * namespace after a controller-level reset. 608 */ 609 if (ns->cdev != NULL) 610 return (0); 611 612 /* 613 * Namespace IDs start at 1, so we need to subtract 1 to create a 614 * correct unit number. 615 */ 616 unit = device_get_unit(ctrlr->dev) * NVME_MAX_NAMESPACES + ns->id - 1; 617 618 make_dev_args_init(&md_args); 619 md_args.mda_devsw = &nvme_ns_cdevsw; 620 md_args.mda_unit = unit; 621 md_args.mda_mode = 0600; 622 md_args.mda_si_drv1 = ns; 623 res = make_dev_s(&md_args, &ns->cdev, "%sn%d", 624 device_get_nameunit(ctrlr->dev), ns->id); 625 if (res != 0) 626 return (ENXIO); 627 ns->cdev->si_drv2 = make_dev_alias(ns->cdev, "%sns%d", 628 device_get_nameunit(ctrlr->dev), ns->id); 629 ns->cdev->si_flags |= SI_UNMAPPED; 630 631 return (0); 632 } 633 634 void 635 nvme_ns_destruct(struct nvme_namespace *ns) 636 { 637 if (ns->cdev != NULL) { 638 if (ns->cdev->si_drv2 != NULL) 639 destroy_dev(ns->cdev->si_drv2); 640 destroy_dev(ns->cdev); 641 } 642 } 643