1 /*- 2 * Copyright (C) 2012-2013 Intel Corporation 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/bio.h> 32 #include <sys/bus.h> 33 #include <sys/conf.h> 34 #include <sys/disk.h> 35 #include <sys/fcntl.h> 36 #include <sys/ioccom.h> 37 #include <sys/malloc.h> 38 #include <sys/module.h> 39 #include <sys/proc.h> 40 #include <sys/systm.h> 41 42 #include <dev/pci/pcivar.h> 43 44 #include <geom/geom.h> 45 46 #include "nvme_private.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 DIOCGMEDIASIZE: 84 *(off_t *)arg = (off_t)nvme_ns_get_size(ns); 85 break; 86 case DIOCGSECTORSIZE: 87 *(u_int *)arg = nvme_ns_get_sector_size(ns); 88 break; 89 default: 90 return (ENOTTY); 91 } 92 93 return (0); 94 } 95 96 static int 97 nvme_ns_open(struct cdev *dev __unused, int flags, int fmt __unused, 98 struct thread *td) 99 { 100 int error = 0; 101 102 if (flags & FWRITE) 103 error = securelevel_gt(td->td_ucred, 0); 104 105 return (error); 106 } 107 108 static int 109 nvme_ns_close(struct cdev *dev __unused, int flags, int fmt __unused, 110 struct thread *td) 111 { 112 113 return (0); 114 } 115 116 static void 117 nvme_ns_strategy_done(void *arg, const struct nvme_completion *cpl) 118 { 119 struct bio *bp = arg; 120 121 /* 122 * TODO: add more extensive translation of NVMe status codes 123 * to different bio error codes (i.e. EIO, EINVAL, etc.) 124 */ 125 if (nvme_completion_is_error(cpl)) { 126 bp->bio_error = EIO; 127 bp->bio_flags |= BIO_ERROR; 128 bp->bio_resid = bp->bio_bcount; 129 } else 130 bp->bio_resid = 0; 131 132 biodone(bp); 133 } 134 135 static void 136 nvme_ns_strategy(struct bio *bp) 137 { 138 struct nvme_namespace *ns; 139 int err; 140 141 ns = bp->bio_dev->si_drv1; 142 err = nvme_ns_bio_process(ns, bp, nvme_ns_strategy_done); 143 144 if (err) { 145 bp->bio_error = err; 146 bp->bio_flags |= BIO_ERROR; 147 bp->bio_resid = bp->bio_bcount; 148 biodone(bp); 149 } 150 151 } 152 153 static struct cdevsw nvme_ns_cdevsw = { 154 .d_version = D_VERSION, 155 .d_flags = D_DISK, 156 .d_read = physread, 157 .d_write = physwrite, 158 .d_open = nvme_ns_open, 159 .d_close = nvme_ns_close, 160 .d_strategy = nvme_ns_strategy, 161 .d_ioctl = nvme_ns_ioctl 162 }; 163 164 uint32_t 165 nvme_ns_get_max_io_xfer_size(struct nvme_namespace *ns) 166 { 167 return ns->ctrlr->max_xfer_size; 168 } 169 170 uint32_t 171 nvme_ns_get_sector_size(struct nvme_namespace *ns) 172 { 173 return (1 << ns->data.lbaf[ns->data.flbas.format].lbads); 174 } 175 176 uint64_t 177 nvme_ns_get_num_sectors(struct nvme_namespace *ns) 178 { 179 return (ns->data.nsze); 180 } 181 182 uint64_t 183 nvme_ns_get_size(struct nvme_namespace *ns) 184 { 185 return (nvme_ns_get_num_sectors(ns) * nvme_ns_get_sector_size(ns)); 186 } 187 188 uint32_t 189 nvme_ns_get_flags(struct nvme_namespace *ns) 190 { 191 return (ns->flags); 192 } 193 194 const char * 195 nvme_ns_get_serial_number(struct nvme_namespace *ns) 196 { 197 return ((const char *)ns->ctrlr->cdata.sn); 198 } 199 200 const char * 201 nvme_ns_get_model_number(struct nvme_namespace *ns) 202 { 203 return ((const char *)ns->ctrlr->cdata.mn); 204 } 205 206 const struct nvme_namespace_data * 207 nvme_ns_get_data(struct nvme_namespace *ns) 208 { 209 210 return (&ns->data); 211 } 212 213 static void 214 nvme_ns_bio_done(void *arg, const struct nvme_completion *status) 215 { 216 struct bio *bp = arg; 217 nvme_cb_fn_t bp_cb_fn; 218 219 bp_cb_fn = bp->bio_driver1; 220 221 if (bp->bio_driver2) 222 free(bp->bio_driver2, M_NVME); 223 224 if (nvme_completion_is_error(status)) { 225 bp->bio_flags |= BIO_ERROR; 226 if (bp->bio_error == 0) 227 bp->bio_error = EIO; 228 } 229 230 if ((bp->bio_flags & BIO_ERROR) == 0) 231 bp->bio_resid = 0; 232 else 233 bp->bio_resid = bp->bio_bcount; 234 235 bp_cb_fn(bp, status); 236 } 237 238 static void 239 nvme_bio_child_inbed(struct bio *parent, int bio_error) 240 { 241 struct nvme_completion parent_cpl; 242 int inbed; 243 244 if (bio_error != 0) { 245 parent->bio_flags |= BIO_ERROR; 246 parent->bio_error = bio_error; 247 } 248 249 /* 250 * atomic_fetchadd will return value before adding 1, so we still 251 * must add 1 to get the updated inbed number. 252 */ 253 inbed = atomic_fetchadd_int(&parent->bio_inbed, 1) + 1; 254 if (inbed == parent->bio_children) { 255 bzero(&parent_cpl, sizeof(parent_cpl)); 256 if (parent->bio_flags & BIO_ERROR) 257 parent_cpl.status.sc = NVME_SC_DATA_TRANSFER_ERROR; 258 nvme_ns_bio_done(parent, &parent_cpl); 259 } 260 } 261 262 static void 263 nvme_bio_child_done(void *arg, const struct nvme_completion *cpl) 264 { 265 struct bio *child = arg; 266 struct bio *parent; 267 int bio_error; 268 269 parent = child->bio_parent; 270 g_destroy_bio(child); 271 bio_error = nvme_completion_is_error(cpl) ? EIO : 0; 272 nvme_bio_child_inbed(parent, bio_error); 273 } 274 275 static uint32_t 276 nvme_get_num_segments(uint64_t addr, uint64_t size, uint32_t align) 277 { 278 uint32_t num_segs, offset, remainder; 279 280 if (align == 0) 281 return (1); 282 283 KASSERT((align & (align - 1)) == 0, ("alignment not power of 2\n")); 284 285 num_segs = size / align; 286 remainder = size & (align - 1); 287 offset = addr & (align - 1); 288 if (remainder > 0 || offset > 0) 289 num_segs += 1 + (remainder + offset - 1) / align; 290 return (num_segs); 291 } 292 293 static void 294 nvme_free_child_bios(int num_bios, struct bio **child_bios) 295 { 296 int i; 297 298 for (i = 0; i < num_bios; i++) { 299 if (child_bios[i] != NULL) 300 g_destroy_bio(child_bios[i]); 301 } 302 303 free(child_bios, M_NVME); 304 } 305 306 static struct bio ** 307 nvme_allocate_child_bios(int num_bios) 308 { 309 struct bio **child_bios; 310 int err = 0, i; 311 312 child_bios = malloc(num_bios * sizeof(struct bio *), M_NVME, M_NOWAIT); 313 if (child_bios == NULL) 314 return (NULL); 315 316 for (i = 0; i < num_bios; i++) { 317 child_bios[i] = g_new_bio(); 318 if (child_bios[i] == NULL) 319 err = ENOMEM; 320 } 321 322 if (err == ENOMEM) { 323 nvme_free_child_bios(num_bios, child_bios); 324 return (NULL); 325 } 326 327 return (child_bios); 328 } 329 330 static struct bio ** 331 nvme_construct_child_bios(struct bio *bp, uint32_t alignment, int *num_bios) 332 { 333 struct bio **child_bios; 334 struct bio *child; 335 uint64_t cur_offset; 336 caddr_t data; 337 uint32_t rem_bcount; 338 int i; 339 #ifdef NVME_UNMAPPED_BIO_SUPPORT 340 struct vm_page **ma; 341 uint32_t ma_offset; 342 #endif 343 344 *num_bios = nvme_get_num_segments(bp->bio_offset, bp->bio_bcount, 345 alignment); 346 child_bios = nvme_allocate_child_bios(*num_bios); 347 if (child_bios == NULL) 348 return (NULL); 349 350 bp->bio_children = *num_bios; 351 bp->bio_inbed = 0; 352 cur_offset = bp->bio_offset; 353 rem_bcount = bp->bio_bcount; 354 data = bp->bio_data; 355 #ifdef NVME_UNMAPPED_BIO_SUPPORT 356 ma_offset = bp->bio_ma_offset; 357 ma = bp->bio_ma; 358 #endif 359 360 for (i = 0; i < *num_bios; i++) { 361 child = child_bios[i]; 362 child->bio_parent = bp; 363 child->bio_cmd = bp->bio_cmd; 364 child->bio_offset = cur_offset; 365 child->bio_bcount = min(rem_bcount, 366 alignment - (cur_offset & (alignment - 1))); 367 child->bio_flags = bp->bio_flags; 368 #ifdef NVME_UNMAPPED_BIO_SUPPORT 369 if (bp->bio_flags & BIO_UNMAPPED) { 370 child->bio_ma_offset = ma_offset; 371 child->bio_ma = ma; 372 child->bio_ma_n = 373 nvme_get_num_segments(child->bio_ma_offset, 374 child->bio_bcount, PAGE_SIZE); 375 ma_offset = (ma_offset + child->bio_bcount) & 376 PAGE_MASK; 377 ma += child->bio_ma_n; 378 if (ma_offset != 0) 379 ma -= 1; 380 } else 381 #endif 382 { 383 child->bio_data = data; 384 data += child->bio_bcount; 385 } 386 cur_offset += child->bio_bcount; 387 rem_bcount -= child->bio_bcount; 388 } 389 390 return (child_bios); 391 } 392 393 static int 394 nvme_ns_split_bio(struct nvme_namespace *ns, struct bio *bp, 395 uint32_t alignment) 396 { 397 struct bio *child; 398 struct bio **child_bios; 399 int err, i, num_bios; 400 401 child_bios = nvme_construct_child_bios(bp, alignment, &num_bios); 402 if (child_bios == NULL) 403 return (ENOMEM); 404 405 for (i = 0; i < num_bios; i++) { 406 child = child_bios[i]; 407 err = nvme_ns_bio_process(ns, child, nvme_bio_child_done); 408 if (err != 0) { 409 nvme_bio_child_inbed(bp, err); 410 g_destroy_bio(child); 411 } 412 } 413 414 free(child_bios, M_NVME); 415 return (0); 416 } 417 418 int 419 nvme_ns_bio_process(struct nvme_namespace *ns, struct bio *bp, 420 nvme_cb_fn_t cb_fn) 421 { 422 struct nvme_dsm_range *dsm_range; 423 uint32_t num_bios; 424 int err; 425 426 bp->bio_driver1 = cb_fn; 427 428 if (ns->stripesize > 0 && 429 (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) { 430 num_bios = nvme_get_num_segments(bp->bio_offset, 431 bp->bio_bcount, ns->stripesize); 432 if (num_bios > 1) 433 return (nvme_ns_split_bio(ns, bp, ns->stripesize)); 434 } 435 436 switch (bp->bio_cmd) { 437 case BIO_READ: 438 err = nvme_ns_cmd_read_bio(ns, bp, nvme_ns_bio_done, bp); 439 break; 440 case BIO_WRITE: 441 err = nvme_ns_cmd_write_bio(ns, bp, nvme_ns_bio_done, bp); 442 break; 443 case BIO_FLUSH: 444 err = nvme_ns_cmd_flush(ns, nvme_ns_bio_done, bp); 445 break; 446 case BIO_DELETE: 447 dsm_range = 448 malloc(sizeof(struct nvme_dsm_range), M_NVME, 449 M_ZERO | M_WAITOK); 450 dsm_range->length = 451 bp->bio_bcount/nvme_ns_get_sector_size(ns); 452 dsm_range->starting_lba = 453 bp->bio_offset/nvme_ns_get_sector_size(ns); 454 bp->bio_driver2 = dsm_range; 455 err = nvme_ns_cmd_deallocate(ns, dsm_range, 1, 456 nvme_ns_bio_done, bp); 457 if (err != 0) 458 free(dsm_range, M_NVME); 459 break; 460 default: 461 err = EIO; 462 break; 463 } 464 465 return (err); 466 } 467 468 int 469 nvme_ns_construct(struct nvme_namespace *ns, uint16_t id, 470 struct nvme_controller *ctrlr) 471 { 472 struct nvme_completion_poll_status status; 473 int unit; 474 475 ns->ctrlr = ctrlr; 476 ns->id = id; 477 ns->stripesize = 0; 478 479 if (pci_get_devid(ctrlr->dev) == 0x09538086 && ctrlr->cdata.vs[3] != 0) 480 ns->stripesize = 481 (1 << ctrlr->cdata.vs[3]) * ctrlr->min_page_size; 482 483 /* 484 * Namespaces are reconstructed after a controller reset, so check 485 * to make sure we only call mtx_init once on each mtx. 486 * 487 * TODO: Move this somewhere where it gets called at controller 488 * construction time, which is not invoked as part of each 489 * controller reset. 490 */ 491 if (!mtx_initialized(&ns->lock)) 492 mtx_init(&ns->lock, "nvme ns lock", NULL, MTX_DEF); 493 494 status.done = FALSE; 495 nvme_ctrlr_cmd_identify_namespace(ctrlr, id, &ns->data, 496 nvme_completion_poll_cb, &status); 497 while (status.done == FALSE) 498 DELAY(5); 499 if (nvme_completion_is_error(&status.cpl)) { 500 nvme_printf(ctrlr, "nvme_identify_namespace failed\n"); 501 return (ENXIO); 502 } 503 504 /* 505 * Note: format is a 0-based value, so > is appropriate here, 506 * not >=. 507 */ 508 if (ns->data.flbas.format > ns->data.nlbaf) { 509 printf("lba format %d exceeds number supported (%d)\n", 510 ns->data.flbas.format, ns->data.nlbaf+1); 511 return (1); 512 } 513 514 if (ctrlr->cdata.oncs.dsm) 515 ns->flags |= NVME_NS_DEALLOCATE_SUPPORTED; 516 517 if (ctrlr->cdata.vwc.present) 518 ns->flags |= NVME_NS_FLUSH_SUPPORTED; 519 520 /* 521 * cdev may have already been created, if we are reconstructing the 522 * namespace after a controller-level reset. 523 */ 524 if (ns->cdev != NULL) 525 return (0); 526 527 /* 528 * Namespace IDs start at 1, so we need to subtract 1 to create a 529 * correct unit number. 530 */ 531 unit = device_get_unit(ctrlr->dev) * NVME_MAX_NAMESPACES + ns->id - 1; 532 533 /* 534 * MAKEDEV_ETERNAL was added in r210923, for cdevs that will never 535 * be destroyed. This avoids refcounting on the cdev object. 536 * That should be OK case here, as long as we're not supporting PCIe 537 * surprise removal nor namespace deletion. 538 */ 539 #ifdef MAKEDEV_ETERNAL_KLD 540 ns->cdev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &nvme_ns_cdevsw, unit, 541 NULL, UID_ROOT, GID_WHEEL, 0600, "nvme%dns%d", 542 device_get_unit(ctrlr->dev), ns->id); 543 #else 544 ns->cdev = make_dev_credf(0, &nvme_ns_cdevsw, unit, 545 NULL, UID_ROOT, GID_WHEEL, 0600, "nvme%dns%d", 546 device_get_unit(ctrlr->dev), ns->id); 547 #endif 548 #ifdef NVME_UNMAPPED_BIO_SUPPORT 549 ns->cdev->si_flags |= SI_UNMAPPED; 550 #endif 551 552 if (ns->cdev != NULL) 553 ns->cdev->si_drv1 = ns; 554 555 return (0); 556 } 557 558 void nvme_ns_destruct(struct nvme_namespace *ns) 559 { 560 561 if (ns->cdev != NULL) 562 destroy_dev(ns->cdev); 563 } 564